reftex-toc.el 45 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108
  1. ;;; reftex-toc.el --- RefTeX's table of contents mode
  2. ;; Copyright (C) 1997-2000, 2003-2017 Free Software Foundation, Inc.
  3. ;; Author: Carsten Dominik <dominik@science.uva.nl>
  4. ;; Maintainer: auctex-devel@gnu.org
  5. ;; This file is part of GNU Emacs.
  6. ;; GNU Emacs is free software: you can redistribute it and/or modify
  7. ;; it under the terms of the GNU General Public License as published by
  8. ;; the Free Software Foundation, either version 3 of the License, or
  9. ;; (at your option) any later version.
  10. ;; GNU Emacs is distributed in the hope that it will be useful,
  11. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. ;; GNU General Public License for more details.
  14. ;; You should have received a copy of the GNU General Public License
  15. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  16. ;;; Commentary:
  17. ;;; Code:
  18. (provide 'reftex-toc)
  19. (require 'reftex)
  20. ;;;
  21. (define-obsolete-variable-alias 'reftex-toc-map 'reftex-toc-mode-map "24.1")
  22. (defvar reftex-toc-mode-map
  23. (let ((map (make-sparse-keymap)))
  24. (define-key map (if (featurep 'xemacs) [(button2)] [(mouse-2)])
  25. 'reftex-toc-mouse-goto-line-and-hide)
  26. (define-key map [follow-link] 'mouse-face)
  27. (substitute-key-definition
  28. 'next-line 'reftex-toc-next map global-map)
  29. (substitute-key-definition
  30. 'previous-line 'reftex-toc-previous map global-map)
  31. (define-key map "n" 'reftex-toc-next)
  32. (define-key map "p" 'reftex-toc-previous)
  33. (define-key map "?" 'reftex-toc-show-help)
  34. (define-key map " " 'reftex-toc-view-line)
  35. (define-key map "\C-m" 'reftex-toc-goto-line-and-hide)
  36. (define-key map "\C-i" 'reftex-toc-goto-line)
  37. (define-key map "\C-c>" 'reftex-toc-display-index)
  38. (define-key map "r" 'reftex-toc-rescan)
  39. (define-key map "R" 'reftex-toc-Rescan)
  40. (define-key map "q" 'reftex-toc-quit) ;
  41. (define-key map "k" 'reftex-toc-quit-and-kill)
  42. (define-key map "f" 'reftex-toc-toggle-follow) ;
  43. (define-key map "a" 'reftex-toggle-auto-toc-recenter)
  44. (define-key map "d" 'reftex-toc-toggle-dedicated-frame)
  45. (define-key map "F" 'reftex-toc-toggle-file-boundary)
  46. (define-key map "i" 'reftex-toc-toggle-index)
  47. (define-key map "l" 'reftex-toc-toggle-labels)
  48. (define-key map "t" 'reftex-toc-max-level)
  49. (define-key map "c" 'reftex-toc-toggle-context)
  50. ;; (define-key map "%" 'reftex-toc-toggle-commented)
  51. (define-key map "\M-%" 'reftex-toc-rename-label)
  52. (define-key map "x" 'reftex-toc-external)
  53. (define-key map "z" 'reftex-toc-jump)
  54. (define-key map "." 'reftex-toc-show-calling-point)
  55. (define-key map "\C-c\C-n" 'reftex-toc-next-heading)
  56. (define-key map "\C-c\C-p" 'reftex-toc-previous-heading)
  57. (define-key map ">" 'reftex-toc-demote)
  58. (define-key map "<" 'reftex-toc-promote)
  59. (easy-menu-define
  60. reftex-toc-menu map
  61. "Menu for Table of Contents buffer"
  62. '("TOC"
  63. ["Show Location" reftex-toc-view-line t]
  64. ["Go To Location" reftex-toc-goto-line t]
  65. ["Exit & Go To Location" reftex-toc-goto-line-and-hide t]
  66. ["Show Calling Point" reftex-toc-show-calling-point t]
  67. ["Quit" reftex-toc-quit t]
  68. "--"
  69. ("Edit"
  70. ["Promote" reftex-toc-promote t]
  71. ["Demote" reftex-toc-demote t]
  72. ["Rename Label" reftex-toc-rename-label t])
  73. "--"
  74. ["Index" reftex-toc-display-index t]
  75. ["External Document TOC " reftex-toc-external t]
  76. "--"
  77. ("Update"
  78. ["Rebuilt *toc* Buffer" revert-buffer t]
  79. ["Rescan One File" reftex-toc-rescan reftex-enable-partial-scans]
  80. ["Rescan Entire Document" reftex-toc-Rescan t])
  81. ("Options"
  82. "TOC Items"
  83. ["File Boundaries" reftex-toc-toggle-file-boundary :style toggle
  84. :selected reftex-toc-include-file-boundaries]
  85. ["Labels" reftex-toc-toggle-labels :style toggle
  86. :selected reftex-toc-include-labels]
  87. ["Index Entries" reftex-toc-toggle-index :style toggle
  88. :selected reftex-toc-include-index-entries]
  89. ["Context" reftex-toc-toggle-context :style toggle
  90. :selected reftex-toc-include-context]
  91. "--"
  92. ["Follow Mode" reftex-toc-toggle-follow :style toggle
  93. :selected reftex-toc-follow-mode]
  94. ["Auto Recenter" reftex-toggle-auto-toc-recenter :style toggle
  95. :selected reftex-toc-auto-recenter-timer]
  96. ["Dedicated Frame" reftex-toc-toggle-dedicated-frame t])
  97. "--"
  98. ["Help" reftex-toc-show-help t]))
  99. map)
  100. "Keymap used for *toc* buffer.")
  101. (defvar reftex-toc-menu)
  102. (defvar reftex-last-window-height nil)
  103. (defvar reftex-last-window-width nil)
  104. (defvar reftex-toc-include-labels-indicator nil)
  105. (defvar reftex-toc-include-index-indicator nil)
  106. (defvar reftex-toc-max-level-indicator nil)
  107. (define-derived-mode reftex-toc-mode special-mode "TOC"
  108. "Major mode for managing Table of Contents for LaTeX files.
  109. This buffer was created with RefTeX.
  110. Press `?' for a summary of important key bindings.
  111. Here are all local bindings.
  112. \\{reftex-toc-mode-map}"
  113. (set (make-local-variable 'transient-mark-mode) t)
  114. (when (featurep 'xemacs)
  115. (set (make-local-variable 'zmacs-regions) t))
  116. (set (make-local-variable 'revert-buffer-function) 'reftex-toc-revert)
  117. (set (make-local-variable 'reftex-toc-include-labels-indicator) "")
  118. (set (make-local-variable 'reftex-toc-max-level-indicator)
  119. (if (= reftex-toc-max-level 100)
  120. "ALL"
  121. (int-to-string reftex-toc-max-level)))
  122. (setq mode-line-format
  123. (list "---- " 'mode-line-buffer-identification
  124. " " 'global-mode-string " (" mode-name ")"
  125. " L<" 'reftex-toc-include-labels-indicator ">"
  126. " I<" 'reftex-toc-include-index-indicator ">"
  127. " T<" 'reftex-toc-max-level-indicator ">"
  128. " -%-"))
  129. (setq truncate-lines t)
  130. (when (featurep 'xemacs)
  131. ;; XEmacs needs the call to make-local-hook
  132. (make-local-hook 'post-command-hook)
  133. (make-local-hook 'pre-command-hook))
  134. (make-local-variable 'reftex-last-follow-point)
  135. (add-hook 'post-command-hook 'reftex-toc-post-command-hook nil t)
  136. (add-hook 'pre-command-hook 'reftex-toc-pre-command-hook nil t)
  137. (easy-menu-add reftex-toc-menu reftex-toc-mode-map))
  138. (defvar reftex-last-toc-file nil
  139. "Stores the file name from which `reftex-toc' was called. For redo command.")
  140. (defvar reftex-toc-return-marker (make-marker)
  141. "Marker which makes it possible to return from TOC to old position.")
  142. (defconst reftex-toc-help
  143. " AVAILABLE KEYS IN TOC BUFFER
  144. ============================
  145. n / p next-line / previous-line
  146. SPC Show the corresponding location of the LaTeX document.
  147. TAB Goto the location and keep the TOC window.
  148. RET Goto the location and hide the TOC window (also on mouse-2).
  149. < / > Promote / Demote section, or all sections in region.
  150. C-c > Display Index. With prefix arg, restrict index to current section.
  151. q / k Hide/Kill *toc* buffer, return to position of reftex-toc command.
  152. l i c F Toggle display of [l]abels, [i]ndex, [c]ontext, [F]ile borders.
  153. t Change maximum toc depth (e.g. `3 t' hides levels greater than 3).
  154. f / g Toggle follow mode / Refresh *toc* buffer.
  155. a / d Toggle auto recenter / Toggle dedicated frame
  156. r / C-u r Reparse the LaTeX document / Reparse entire LaTeX document.
  157. . In other window, show position from where `reftex-toc' was called.
  158. M-% Global search and replace to rename label at point.
  159. x Switch to TOC of external document (with LaTeX package `xr').
  160. z Jump to a specific section (e.g. '3 z' goes to section 3).")
  161. (defvar reftex--rebuilding-toc nil)
  162. ;;;###autoload
  163. (defun reftex-toc (&optional _rebuild reuse)
  164. ;; FIXME: Get rid of the `rebuild' argument.
  165. "Show the table of contents for the current document.
  166. When called with a raw C-u prefix, rescan the document first."
  167. ;; The REUSE argument means, search all visible frames for a window
  168. ;; displaying the toc window. If yes, reuse this window.
  169. (interactive)
  170. (if (or (not (string= reftex-last-toc-master (reftex-TeX-master-file)))
  171. ;; FIXME: use (interactive "P") to receive current-prefix-arg as
  172. ;; an argument instead of using the var here, which forces us to set
  173. ;; current-prefix-arg in the callers.
  174. current-prefix-arg)
  175. (reftex-erase-buffer "*toc*"))
  176. (setq reftex-last-toc-file (buffer-file-name))
  177. (setq reftex-last-toc-master (reftex-TeX-master-file))
  178. (set-marker reftex-toc-return-marker (point))
  179. ;; If follow mode is active, arrange to delay it one command
  180. (if reftex-toc-follow-mode
  181. (setq reftex-toc-follow-mode 1))
  182. (and reftex-toc-include-index-entries
  183. (reftex-ensure-index-support))
  184. (or reftex-support-index
  185. (setq reftex-toc-include-index-entries nil))
  186. ;; Ensure access to scanning info and rescan buffer if prefix are is '(4)
  187. (reftex-access-scan-info current-prefix-arg)
  188. (let* ((this-buf (current-buffer))
  189. (docstruct-symbol reftex-docstruct-symbol)
  190. (xr-data (assq 'xr (symbol-value reftex-docstruct-symbol)))
  191. (xr-alist (cons (cons "" (buffer-file-name)) (nth 1 xr-data)))
  192. (here-I-am (if reftex--rebuilding-toc
  193. (get 'reftex-toc :reftex-data)
  194. (car (reftex-where-am-I))))
  195. (unsplittable (if (fboundp 'frame-property)
  196. (frame-property (selected-frame) 'unsplittable)
  197. (frame-parameter nil 'unsplittable)))
  198. offset toc-window)
  199. (if (setq toc-window (get-buffer-window
  200. "*toc*"
  201. (if reuse 'visible)))
  202. (select-window toc-window)
  203. (when (or (not reftex-toc-keep-other-windows)
  204. (< (window-height) (* 2 window-min-height)))
  205. (delete-other-windows))
  206. (setq reftex-last-window-width (window-total-width)
  207. reftex-last-window-height (window-height)) ; remember
  208. (unless unsplittable
  209. (if reftex-toc-split-windows-horizontally
  210. (split-window-right
  211. (floor (* (window-total-width)
  212. reftex-toc-split-windows-fraction)))
  213. (split-window-below
  214. (floor (* (window-height)
  215. reftex-toc-split-windows-fraction)))))
  216. (switch-to-buffer "*toc*"))
  217. (or (eq major-mode 'reftex-toc-mode) (reftex-toc-mode))
  218. (set (make-local-variable 'reftex-docstruct-symbol) docstruct-symbol)
  219. (setq reftex-toc-include-labels-indicator
  220. (if (eq reftex-toc-include-labels t)
  221. "ALL"
  222. reftex-toc-include-labels))
  223. (setq reftex-toc-include-index-indicator
  224. (if (eq reftex-toc-include-index-entries t)
  225. "ALL"
  226. reftex-toc-include-index-entries))
  227. (cond
  228. ((= (buffer-size) 0)
  229. ;; buffer is empty - fill it with the table of contents
  230. (message "Building *toc* buffer...")
  231. (setq buffer-read-only nil)
  232. (insert (format
  233. "TABLE-OF-CONTENTS on %s
  234. SPC=view TAB=goto RET=goto+hide [q]uit [r]escan [l]abels [f]ollow [x]r [?]Help
  235. ------------------------------------------------------------------------------
  236. " (abbreviate-file-name reftex-last-toc-master)))
  237. (if (reftex-use-fonts)
  238. (put-text-property (point-min) (point) 'font-lock-face reftex-toc-header-face))
  239. (if (fboundp 'cursor-intangible-mode)
  240. (cursor-intangible-mode 1)
  241. ;; If `cursor-intangible' is not available, fallback on the old
  242. ;; intrusive `intangible' property.
  243. (put-text-property (point-min) (point) 'intangible t))
  244. (add-text-properties (point-min) (point)
  245. '(cursor-intangible t
  246. front-sticky (cursor-intangible)
  247. rear-nonsticky (cursor-intangible)))
  248. (put-text-property (point-min) (1+ (point-min)) 'xr-alist xr-alist)
  249. (setq offset
  250. (reftex-insert-docstruct
  251. this-buf
  252. t ; include TOC
  253. reftex-toc-include-labels
  254. reftex-toc-include-index-entries
  255. reftex-toc-include-file-boundaries
  256. reftex-toc-include-context
  257. nil ; counter
  258. nil ; commented
  259. here-I-am
  260. "" ; xr-prefix
  261. t ; a TOC buffer
  262. ))
  263. (run-hooks 'reftex-display-copied-context-hook)
  264. (message "Building *toc* buffer...done.")
  265. (setq buffer-read-only t))
  266. (t
  267. ;; Only compute the offset
  268. (setq offset
  269. (or (reftex-get-offset this-buf here-I-am
  270. (if reftex-toc-include-labels " " nil)
  271. t
  272. reftex-toc-include-index-entries
  273. reftex-toc-include-file-boundaries)
  274. (reftex-last-assoc-before-elt
  275. 'toc here-I-am
  276. (symbol-value reftex-docstruct-symbol))))
  277. (put 'reftex-toc :reftex-line 3)
  278. (goto-char (point-min))
  279. (forward-line 2)))
  280. ;; Find the correct starting point
  281. (reftex-find-start-point (point) offset (get 'reftex-toc :reftex-line))
  282. (setq reftex-last-follow-point (point))))
  283. ;;;###autoload
  284. (defun reftex-toc-recenter (&optional arg)
  285. "Display the TOC window and highlight line corresponding to current position."
  286. (interactive "P")
  287. (let ((buf (current-buffer))
  288. (frame (selected-frame)))
  289. (reftex-toc arg t)
  290. (if (= (count-lines 1 (point)) 2)
  291. (let ((current-prefix-arg nil))
  292. (select-window (get-buffer-window buf frame))
  293. (reftex-toc nil t)))
  294. (and (> (point) 1) ;FIXME: Is this point-min or do we care about narrowing?
  295. (not (get-text-property (point) 'cursor-intangible))
  296. (memq reftex-highlight-selection '(cursor both))
  297. (reftex-highlight 2
  298. (or (previous-single-property-change
  299. (min (point-max) (1+ (point))) :data)
  300. (point-min))
  301. (or (next-single-property-change (point) :data)
  302. (point-max))))
  303. (select-window (get-buffer-window buf frame))))
  304. (defun reftex-toc-pre-command-hook ()
  305. ;; used as pre command hook in *toc* buffer
  306. (reftex-unhighlight 0)
  307. )
  308. (defun reftex-toc-post-command-hook ()
  309. ;; used in the post-command-hook for the *toc* buffer
  310. ;; FIXME: Lots of redundancy with reftex-index-post-command-hook!
  311. (when (get-text-property (point) :data)
  312. (put 'reftex-toc :reftex-data (get-text-property (point) :data))
  313. (and (> (point) 1) ;FIXME: Is this point-min or do we care about narrowing?
  314. (not (get-text-property (point) 'cursor-intangible))
  315. (memq reftex-highlight-selection '(cursor both))
  316. (reftex-highlight 2
  317. (or (previous-single-property-change (1+ (point)) :data)
  318. (point-min))
  319. (or (next-single-property-change (point) :data)
  320. (point-max)))))
  321. (if (integerp reftex-toc-follow-mode)
  322. ;; remove delayed action
  323. (setq reftex-toc-follow-mode t)
  324. (and (not (reftex-toc-dframe-p))
  325. reftex-toc-follow-mode
  326. (not (equal reftex-last-follow-point (point)))
  327. ;; show context in other window
  328. (setq reftex-last-follow-point (point))
  329. (condition-case nil
  330. (reftex-toc-visit-location nil (not reftex-revisit-to-follow))
  331. (error t)))))
  332. (defun reftex-re-enlarge ()
  333. "Enlarge window to a remembered size."
  334. (let ((count (if reftex-toc-split-windows-horizontally
  335. (- (or reftex-last-window-width (window-total-width))
  336. (window-total-width))
  337. (- (or reftex-last-window-height (window-height))
  338. (window-height)))))
  339. (when (> count 0)
  340. (enlarge-window count reftex-toc-split-windows-horizontally))))
  341. (defun reftex-toc-dframe-p (&optional frame error)
  342. ;; Check if FRAME is the dedicated TOC frame.
  343. ;; If yes, and ERROR is non-nil, throw an error.
  344. (setq frame (or frame (selected-frame)))
  345. (let ((res (equal
  346. (if (fboundp 'frame-property)
  347. (frame-property frame 'name)
  348. (frame-parameter frame 'name))
  349. "RefTeX TOC Frame")))
  350. (if (and res error)
  351. (error "This frame is view-only. Use `C-c =' to create TOC window for commands"))
  352. res))
  353. (defun reftex-toc-show-help ()
  354. "Show a summary of special key bindings."
  355. (interactive)
  356. (reftex-toc-dframe-p nil 'error)
  357. (with-output-to-temp-buffer "*RefTeX Help*"
  358. (princ reftex-toc-help))
  359. (reftex-enlarge-to-fit "*RefTeX Help*" t)
  360. ;; If follow mode is active, arrange to delay it one command
  361. (if reftex-toc-follow-mode
  362. (setq reftex-toc-follow-mode 1)))
  363. (defun reftex-toc-next (&optional _arg)
  364. "Move to next selectable item."
  365. (interactive)
  366. (when (featurep 'xemacs) (setq zmacs-region-stays t))
  367. (setq reftex-callback-fwd t)
  368. (or (eobp) (forward-char 1))
  369. (goto-char (or (next-single-property-change (point) :data)
  370. (point))))
  371. (defun reftex-toc-previous (&optional _arg)
  372. "Move to previous selectable item."
  373. (interactive)
  374. (when (featurep 'xemacs) (setq zmacs-region-stays t))
  375. (setq reftex-callback-fwd nil)
  376. (goto-char (or (previous-single-property-change (point) :data)
  377. (point))))
  378. (defun reftex-toc-next-heading (&optional arg)
  379. "Move to next table of contents line."
  380. (interactive "p")
  381. (when (featurep 'xemacs) (setq zmacs-region-stays t))
  382. (end-of-line)
  383. (re-search-forward "^ " nil t arg)
  384. (beginning-of-line))
  385. (defun reftex-toc-previous-heading (&optional arg)
  386. "Move to previous table of contents line."
  387. (interactive "p")
  388. (when (featurep 'xemacs) (setq zmacs-region-stays t))
  389. (re-search-backward "^ " nil t arg))
  390. (defun reftex-toc-toggle-follow ()
  391. "Toggle follow (other window follows with context)."
  392. (interactive)
  393. (setq reftex-last-follow-point -1)
  394. (setq reftex-toc-follow-mode (not reftex-toc-follow-mode)))
  395. (defun reftex-toc-toggle-file-boundary ()
  396. "Toggle inclusion of file boundaries in *toc* buffer."
  397. (interactive)
  398. (setq reftex-toc-include-file-boundaries
  399. (not reftex-toc-include-file-boundaries))
  400. (reftex-toc-revert))
  401. (defun reftex-toc-toggle-labels (arg)
  402. "Toggle inclusion of labels in *toc* buffer.
  403. With prefix ARG, prompt for a label type and include only labels of
  404. that specific type."
  405. (interactive "P")
  406. (setq reftex-toc-include-labels
  407. (if arg (reftex-query-label-type)
  408. (not reftex-toc-include-labels)))
  409. (reftex-toc-revert))
  410. (defun reftex-toc-toggle-index (arg)
  411. "Toggle inclusion of index in *toc* buffer.
  412. With prefix arg, prompt for an index tag and include only entries of that
  413. specific index."
  414. (interactive "P")
  415. (setq reftex-toc-include-index-entries
  416. (if arg (reftex-index-select-tag)
  417. (not reftex-toc-include-index-entries)))
  418. (reftex-toc-revert))
  419. (defun reftex-toc-toggle-context ()
  420. "Toggle inclusion of label context in *toc* buffer.
  421. Label context is only displayed when the labels are there as well."
  422. (interactive)
  423. (setq reftex-toc-include-context (not reftex-toc-include-context))
  424. (reftex-toc-revert))
  425. (defun reftex-toc-max-level (arg)
  426. "Set the maximum level of TOC lines in this buffer to value of prefix ARG.
  427. When no prefix is given, set the max level to a large number, so that all
  428. levels are shown. For example, to set the level to 3, type `3 m'."
  429. (interactive "P")
  430. (setq reftex-toc-max-level (if arg
  431. (prefix-numeric-value arg)
  432. 100))
  433. (setq reftex-toc-max-level-indicator
  434. (if arg (int-to-string reftex-toc-max-level) "ALL"))
  435. (reftex-toc-revert))
  436. (defun reftex-toc-view-line ()
  437. "View document location in other window."
  438. (interactive)
  439. (reftex-toc-dframe-p nil 'error)
  440. (reftex-toc-visit-location))
  441. (defun reftex-toc-goto-line-and-hide ()
  442. "Go to document location in other window. Hide the TOC window."
  443. (interactive)
  444. (reftex-toc-dframe-p nil 'error)
  445. (reftex-toc-visit-location 'hide))
  446. (defun reftex-toc-goto-line ()
  447. "Go to document location in other window. TOC window stays."
  448. (interactive)
  449. (reftex-toc-dframe-p nil 'error)
  450. (reftex-toc-visit-location t))
  451. (defun reftex-toc-mouse-goto-line-and-hide (ev)
  452. "Go to document location in other window. Hide the TOC window."
  453. (interactive "e")
  454. (mouse-set-point ev)
  455. (reftex-toc-dframe-p nil 'error)
  456. (reftex-toc-visit-location 'hide))
  457. (defun reftex-toc-show-calling-point ()
  458. "Show point where `reftex-toc' was called from."
  459. (interactive)
  460. (reftex-toc-dframe-p nil 'error)
  461. (let ((this-window (selected-window)))
  462. (unwind-protect
  463. (progn
  464. (switch-to-buffer-other-window
  465. (marker-buffer reftex-toc-return-marker))
  466. (goto-char (marker-position reftex-toc-return-marker))
  467. (recenter '(4)))
  468. (select-window this-window))))
  469. (defun reftex-toc-quit ()
  470. "Hide the TOC window and do not move point.
  471. If the TOC window is the only window on the dedicated TOC frame, the frame
  472. is destroyed."
  473. (interactive)
  474. (if (and (one-window-p)
  475. (reftex-toc-dframe-p)
  476. (> (length (frame-list)) 1))
  477. (delete-frame)
  478. (or (one-window-p) (delete-window))
  479. (switch-to-buffer (marker-buffer reftex-toc-return-marker))
  480. (reftex-re-enlarge)
  481. (goto-char (or (marker-position reftex-toc-return-marker) (point)))))
  482. (defun reftex-toc-quit-and-kill ()
  483. "Kill the *toc* buffer."
  484. (interactive)
  485. (kill-buffer "*toc*")
  486. (or (one-window-p) (delete-window))
  487. (switch-to-buffer (marker-buffer reftex-toc-return-marker))
  488. (reftex-re-enlarge)
  489. (goto-char (marker-position reftex-toc-return-marker)))
  490. (defun reftex-toc-display-index (&optional arg)
  491. "Display the index buffer for the current document.
  492. This works just like `reftex-display-index' from a LaTeX buffer.
  493. With prefix arg 1, restrict index to the section at point."
  494. (interactive "P")
  495. (reftex-toc-dframe-p nil 'error)
  496. (let ((data (get-text-property (point) :data))
  497. (docstruct (symbol-value reftex-docstruct-symbol))
  498. bor eor restr)
  499. (when (equal arg 2)
  500. (setq bor (reftex-last-assoc-before-elt 'toc data docstruct)
  501. eor (assoc 'toc (cdr (memq bor docstruct)))
  502. restr (list (nth 6 bor) bor eor)))
  503. (reftex-toc-goto-line)
  504. (reftex-display-index (if restr nil arg) restr)))
  505. ;; Rescanning the document and rebuilding the TOC buffer.
  506. (defun reftex-toc-rescan (&rest _)
  507. "Regenerate the *toc* buffer by reparsing file of section at point."
  508. (interactive)
  509. (if (and reftex-enable-partial-scans
  510. (null current-prefix-arg))
  511. (let* ((data (get-text-property (point) :data))
  512. (what (car data))
  513. (file (cond ((eq what 'toc) (nth 3 data))
  514. ((memq what '(eof bof file-error)) (nth 1 data))
  515. ((stringp what) (nth 3 data))
  516. ((eq what 'index) (nth 3 data))))
  517. (line (+ (count-lines (point-min) (point)) (if (bolp) 1 0))))
  518. (if (not file)
  519. (error "Don't know which file to rescan. Try `C-u r'")
  520. (put 'reftex-toc :reftex-line line)
  521. (switch-to-buffer-other-window
  522. (reftex-get-file-buffer-force file))
  523. (setq current-prefix-arg '(4))
  524. (let ((reftex--rebuilding-toc t))
  525. (reftex-toc))))
  526. (reftex-toc-Rescan))
  527. (reftex-kill-temporary-buffers))
  528. (defun reftex-toc-Rescan (&rest _)
  529. "Regenerate the *toc* buffer by reparsing the entire document."
  530. (interactive)
  531. (let* ((line (+ (count-lines (point-min) (point)) (if (bolp) 1 0))))
  532. (put 'reftex-toc :reftex-line line))
  533. (switch-to-buffer-other-window
  534. (reftex-get-file-buffer-force reftex-last-toc-file))
  535. (setq current-prefix-arg '(16))
  536. (let ((reftex--rebuilding-toc t))
  537. (reftex-toc)))
  538. (defun reftex-toc-revert (&rest _)
  539. "Regenerate the TOC from the internal lists."
  540. (interactive)
  541. (let ((unsplittable
  542. (if (fboundp 'frame-property)
  543. (frame-property (selected-frame) 'unsplittable)
  544. (frame-parameter nil 'unsplittable)))
  545. (reftex--rebuilding-toc t))
  546. (if unsplittable
  547. (switch-to-buffer
  548. (reftex-get-file-buffer-force reftex-last-toc-file))
  549. (switch-to-buffer-other-window
  550. (reftex-get-file-buffer-force reftex-last-toc-file))))
  551. (reftex-erase-buffer "*toc*")
  552. (setq current-prefix-arg nil)
  553. (reftex-toc t t))
  554. (defun reftex-toc-external (&rest _)
  555. "Switch to table of contents of an external document."
  556. (interactive)
  557. (reftex-toc-dframe-p nil 'error)
  558. (let* ((old-buf (current-buffer))
  559. (xr-alist (get-text-property 1 'xr-alist))
  560. (xr-index (reftex-select-external-document
  561. xr-alist 0)))
  562. (switch-to-buffer-other-window (or (reftex-get-file-buffer-force
  563. (cdr (nth xr-index xr-alist)))
  564. (error "Cannot switch document")))
  565. (reftex-toc)
  566. (if (equal old-buf (current-buffer))
  567. (message "")
  568. (message "Switched document"))))
  569. (defun reftex-toc-jump (arg)
  570. "Jump to a specific section. E.g. '3 z' jumps to section 3.
  571. Useful for large TOCs."
  572. (interactive "P")
  573. (goto-char (point-min))
  574. (re-search-forward
  575. (concat "^ *" (number-to-string (if (numberp arg) arg 1)) " ")
  576. nil t)
  577. (beginning-of-line))
  578. ;; Promotion/Demotion stuff
  579. (defvar reftex--pro-or-de)
  580. (defvar reftex--start-line)
  581. (defvar reftex--mark-line)
  582. (defun reftex-toc-demote (&optional _arg)
  583. "Demote section at point. If region is active, apply to all in region."
  584. (interactive)
  585. (reftex-toc-do-promote 1))
  586. (defun reftex-toc-promote (&optional _arg)
  587. "Promote section at point. If region is active, apply to all in region."
  588. (interactive)
  589. (reftex-toc-do-promote -1))
  590. (defun reftex-toc-do-promote (delta)
  591. "Workhorse for `reftex-toc-promote' and `reftex-toc-demote'.
  592. Changes the level of sections in the current region, or just the section at
  593. point."
  594. ;; We should not do anything unless we are sure this is going to work for
  595. ;; each section in the region. Therefore we first collect information and
  596. ;; test.
  597. (let* ((reftex--start-line (+ (count-lines (point-min) (point))
  598. (if (bolp) 1 0)))
  599. (reftex--mark-line
  600. (if (reftex-region-active-p)
  601. (save-excursion (goto-char (mark))
  602. (+ (count-lines (point-min) (point))
  603. (if (bolp) 1 0)))))
  604. (start-pos (point))
  605. (reftex--pro-or-de (if (> delta 0) "de" "pro"))
  606. beg end entries data sections nsec msg)
  607. (setq msg
  608. (catch 'exit
  609. (if (reftex-region-active-p)
  610. ;; A region is dangerous, check if we have a brand new scan,
  611. ;; to make sure we are not missing any section statements.
  612. (if (not (reftex-toc-check-docstruct))
  613. (reftex-toc-load-all-files-for-promotion) ;; exits
  614. (setq beg (min (point) (mark))
  615. end (max (point) (mark))))
  616. (setq beg (point) end (point)))
  617. (goto-char beg)
  618. (while (and (setq data (get-text-property (point) :data))
  619. (<= (point) end))
  620. (if (eq (car data) 'toc) (push (cons data (point)) entries))
  621. (goto-char (or (next-single-property-change (point) :data)
  622. (point-max))))
  623. (setq entries (nreverse entries))
  624. ;; Get the relevant section numbers, for an informative message
  625. (goto-char start-pos)
  626. (setq sections (reftex-toc-extract-section-number (car entries)))
  627. (if (> (setq nsec (length entries)) 1)
  628. (setq sections
  629. (concat sections "-"
  630. (reftex-toc-extract-section-number
  631. (nth (1- nsec) entries)))))
  632. ;; Run through the list and prepare the changes.
  633. (setq entries (mapcar
  634. (lambda (e) (reftex-toc-promote-prepare e delta))
  635. entries))
  636. ;; Ask for permission
  637. (if (or (not reftex-toc-confirm-promotion) ; never confirm
  638. (and (integerp reftex-toc-confirm-promotion) ; confirm if many
  639. (< nsec reftex-toc-confirm-promotion))
  640. (yes-or-no-p ; ask
  641. (format "%s %d toc-entr%s (section%s %s)? "
  642. (if (< delta 0) "Promote" "Demote")
  643. nsec
  644. (if (= 1 nsec) "y" "ies")
  645. (if (= 1 nsec) "" "s")
  646. sections)))
  647. nil ; we have permission, do nothing
  648. (error "Abort")) ; abort, we don't have permission
  649. ;; Do the changes
  650. (mapc 'reftex-toc-promote-action entries)
  651. ;; Rescan the document and rebuilt the toc buffer
  652. (save-window-excursion
  653. (reftex-toc-Rescan))
  654. (reftex-toc-restore-region reftex--start-line reftex--mark-line)
  655. (message "%d section%s %smoted"
  656. nsec (if (= 1 nsec) "" "s") reftex--pro-or-de)
  657. nil))
  658. (if msg (progn (ding) (message "%s" msg)))))
  659. (defun reftex-toc-restore-region (point-line &optional mark-line)
  660. (let ((mpos
  661. (when mark-line
  662. (goto-char (point-min))
  663. (forward-line (1- mark-line))
  664. (point))))
  665. (when point-line
  666. (goto-char (point-min))
  667. (forward-line (1- point-line)))
  668. (when mpos
  669. (set-mark mpos)
  670. (if (featurep 'xemacs)
  671. (zmacs-activate-region)
  672. (setq mark-active t
  673. deactivate-mark nil)))))
  674. (defun reftex-toc-promote-prepare (x delta)
  675. "Look at a TOC entry and see if we could pro/demote it.
  676. This function prepares everything for the change, but does not do it.
  677. The return value is a list with information needed when doing the
  678. promotion/demotion later. DELTA is the level change."
  679. (let* ((data (car x))
  680. (toc-point (cdr x))
  681. (marker (nth 4 data))
  682. (literal (nth 7 data))
  683. (load nil)
  684. (name nil)
  685. ;; Here follows some paranoid code to make very sure we are not
  686. ;; going to break anything
  687. (_
  688. (if (and (markerp marker) (marker-buffer marker))
  689. ;; Buffer is still live and we have the marker.
  690. (progn
  691. (with-current-buffer (marker-buffer marker)
  692. ;; Goto the buffer and check of section is unchanged
  693. (goto-char (marker-position marker))
  694. (if (looking-at (regexp-quote literal))
  695. ;; OK, get the makro name
  696. (progn
  697. (beginning-of-line 1)
  698. (if (looking-at reftex-section-regexp)
  699. (setq name (reftex-match-string 2))
  700. (error "Something is wrong! Contact maintainer!")))
  701. ;; Section has changed, request scan and loading
  702. ;; We use a variable to delay until after the safe-exc.
  703. ;; because otherwise we lose the region.
  704. (setq load t)))
  705. ;; Scan document and load all files, this exits command
  706. (if load (reftex-toc-load-all-files-for-promotion))) ; exits
  707. ;; We don't have a live marker: scan and load files.
  708. (reftex-toc-load-all-files-for-promotion)))
  709. (level (cdr (assoc name reftex-section-levels-all)))
  710. (_ (if (not (integerp level))
  711. (progn
  712. (goto-char toc-point)
  713. (error "Cannot %smote special sections" reftex--pro-or-de))))
  714. (newlevel (if (>= level 0) (+ delta level) (- level delta)))
  715. (_ (if (or (and (>= level 0) (= newlevel -1))
  716. (and (< level 0) (= newlevel 0)))
  717. (error "Cannot %smote \\%s" reftex--pro-or-de name)))
  718. (newname (reftex-toc-newhead-from-alist newlevel name
  719. reftex-section-levels-all)))
  720. (if (and name newname)
  721. (list data name newname toc-point)
  722. (goto-char toc-point)
  723. (error "Cannot %smote \\%s" reftex--pro-or-de name))))
  724. (defun reftex-toc-promote-action (x)
  725. "Change the level of a TOC entry.
  726. `reftex--pro-or-de' is assumed to be dynamically scoped into this function."
  727. (let* ((data (car x))
  728. (name (nth 1 x))
  729. (newname (nth 2 x))
  730. (marker (nth 4 data)))
  731. (with-current-buffer (marker-buffer marker)
  732. (goto-char (marker-position marker))
  733. (if (looking-at (concat "\\([ \t]*" reftex-section-pre-regexp "\\)" (regexp-quote name)))
  734. (replace-match (concat "\\1" newname))
  735. (error "Fatal error during %smotion" reftex--pro-or-de)))))
  736. (defun reftex-toc-extract-section-number (entry)
  737. "Get the numbering of a TOC entry, for message purposes."
  738. (if (string-match "\\s-*\\(\\S-+\\)" (nth 2 (car entry)))
  739. (match-string 1 (nth 2 (car entry)))
  740. "?"))
  741. (defun reftex-toc-newhead-from-alist (nlevel head alist)
  742. "Get new heading with level NLEVEL from ALIST.
  743. If there are no such entries, return nil.
  744. If there are several different entries with same new level, choose the
  745. one with the smallest distance to the association of HEAD in the alist.
  746. This makes it possible for promotion to work several sets of headings,
  747. if these sets are sorted blocks in the alist."
  748. (let* ((al alist)
  749. (ass (assoc head al))
  750. (pos (length (memq ass al)))
  751. dist (mindist 1000) newhead)
  752. (while al
  753. (if (equal (cdar al) nlevel)
  754. (progn
  755. (setq dist (abs (- (length al) pos)))
  756. (if (< dist mindist)
  757. (setq newhead (car (car al))
  758. mindist dist))))
  759. (setq al (cdr al)))
  760. newhead))
  761. (defun reftex-toc-check-docstruct ()
  762. "Check if the current docstruct is fully up to date and all files visited."
  763. ;; We do this by checking if all sections are on the right position
  764. (let ((docstruct (symbol-value reftex-docstruct-symbol))
  765. entry marker empoint)
  766. (catch 'exit
  767. (while (setq entry (pop docstruct))
  768. (if (eq (car entry) 'toc)
  769. (progn
  770. (setq marker (nth 4 entry)
  771. empoint (nth 8 entry))
  772. (if (not (and (markerp marker)
  773. (marker-buffer marker)
  774. (= (marker-position marker) empoint)))
  775. (throw 'exit nil)))))
  776. t)))
  777. (defun reftex-toc-load-all-files-for-promotion ()
  778. "Make sure all files of the document are being visited by buffers,
  779. and that the scanning info is absolutely up to date.
  780. We do this by rescanning with `reftex-keep-temporary-buffers' bound to t.
  781. The variable `reftex--pro-or-de' is assumed to be dynamically scoped into this function.
  782. When finished, we exit with an error message."
  783. (let ((reftex-keep-temporary-buffers t))
  784. (reftex-toc-Rescan)
  785. (reftex-toc-restore-region reftex--start-line reftex--mark-line)
  786. (throw 'exit
  787. "TOC had to be updated first. Please check selection and repeat the command.")))
  788. (defun reftex-toc-rename-label ()
  789. "Rename the currently selected label in the *toc* buffer.
  790. This launches a global search and replace in order to rename a label.
  791. Renaming a label is hardly ever necessary - the only exception is after
  792. promotion/demotion in connection with a package like fancyref, where the
  793. label prefix determines the wording of a reference."
  794. (interactive)
  795. (let* ((toc (get-text-property (point) :data))
  796. (label (car toc)) newlabel)
  797. (if (not (stringp label))
  798. (error "This is not a label entry"))
  799. (setq newlabel (read-string (format "Rename label \"%s\" to:" label)))
  800. (if (assoc newlabel (symbol-value reftex-docstruct-symbol))
  801. (if (not (y-or-n-p
  802. (format-message "Label `%s' exists. Use anyway? " label)))
  803. (error "Abort")))
  804. (save-excursion
  805. (save-window-excursion
  806. (reftex-toc-visit-location t)
  807. (condition-case nil
  808. (reftex-query-replace-document
  809. (concat "{" (regexp-quote label) "}")
  810. (format "{%s}" newlabel))
  811. (error t))))
  812. (reftex-toc-rescan)))
  813. (defun reftex-toc-visit-location (&optional final no-revisit)
  814. ;; Visit the tex file corresponding to the TOC entry on the current line.
  815. ;; If FINAL is t, stay there
  816. ;; If FINAL is 'hide, hide the TOC window.
  817. ;; Otherwise, move cursor back into TOC window.
  818. ;; NO-REVISIT means don't visit files, just use live buffers.
  819. ;; This function is pretty clever about finding back a section heading,
  820. ;; even if the buffer is not live, or things like outline, x-symbol etc.
  821. ;; have been active.
  822. (let* ((toc (get-text-property (point) :data))
  823. (toc-window (selected-window))
  824. match)
  825. (unless toc (error "Don't know which TOC line to visit"))
  826. (cond
  827. ((eq (car toc) 'toc)
  828. ;; a toc entry
  829. (setq match (reftex-toc-find-section toc no-revisit)))
  830. ((eq (car toc) 'index)
  831. ;; an index entry
  832. (setq match (reftex-index-show-entry toc no-revisit)))
  833. ((memq (car toc) '(bof eof))
  834. ;; A file entry
  835. (setq match
  836. (let ((where (car toc))
  837. (file (nth 1 toc)))
  838. (if (or (not no-revisit) (reftex-get-buffer-visiting file))
  839. (progn
  840. (switch-to-buffer-other-window
  841. (reftex-get-file-buffer-force file nil))
  842. (goto-char (if (eq where 'bof) (point-min) (point-max))))
  843. (message "%s" reftex-no-follow-message) nil))))
  844. ((stringp (car toc))
  845. ;; a label
  846. (setq match (reftex-show-label-location toc reftex-callback-fwd
  847. no-revisit t))))
  848. (unless match
  849. (select-window toc-window)
  850. (error "Cannot find location"))
  851. ;; Use the `final' parameter to decide what to do next.
  852. (cond
  853. ((eq final t)
  854. (with-selected-window toc-window
  855. (reftex-unhighlight 0)))
  856. ((eq final 'hide)
  857. (let ((window (selected-window))
  858. (buffer (window-buffer)))
  859. (unless (eq window toc-window) ;FIXME: Can this happen?
  860. (with-selected-window toc-window
  861. (reftex-unhighlight 0)
  862. (or (one-window-p) (delete-window))))
  863. ;; If window is still live, buffer is already visible
  864. ;; so let's not make it visible in yet-another-window.
  865. (unless (window-live-p window)
  866. ;; FIXME: How could window not be live?
  867. (pop-to-buffer-same-window buffer))
  868. (reftex-re-enlarge)))
  869. (t
  870. (unless (eq (selected-frame) (window-frame toc-window))
  871. ;; Make sure `toc-window' is not just selected but has focus.
  872. (select-frame-set-input-focus (window-frame toc-window)))
  873. (select-window toc-window)))))
  874. (defun reftex-toc-find-section (toc &optional no-revisit)
  875. (let* ((file (nth 3 toc))
  876. (marker (nth 4 toc))
  877. (level (nth 5 toc))
  878. (literal (nth 7 toc))
  879. (emergency-point (nth 8 toc))
  880. (match
  881. (cond
  882. ((and (markerp marker) (marker-buffer marker))
  883. ;; Buffer is still live and we have the marker. Should be easy.
  884. (switch-to-buffer-other-window (marker-buffer marker))
  885. (push-mark nil)
  886. (goto-char (marker-position marker))
  887. (or (looking-at (regexp-quote literal))
  888. (looking-at (reftex-make-regexp-allow-for-ctrl-m literal))
  889. (looking-at (reftex-make-desperate-section-regexp literal))
  890. (looking-at (concat "\\\\"
  891. (regexp-quote
  892. (car
  893. (rassq level
  894. reftex-section-levels-all)))
  895. "[[{]?"))))
  896. ((or (not no-revisit)
  897. (reftex-get-buffer-visiting file))
  898. ;; Marker is lost. Use the backup method.
  899. (switch-to-buffer-other-window
  900. (reftex-get-file-buffer-force file nil))
  901. (goto-char (or emergency-point (point-min)))
  902. (or (looking-at (regexp-quote literal))
  903. (let ((len (length literal)))
  904. (or (reftex-nearest-match (regexp-quote literal) len)
  905. (reftex-nearest-match
  906. (reftex-make-regexp-allow-for-ctrl-m literal) len)
  907. (reftex-nearest-match
  908. (reftex-make-desperate-section-regexp literal) len)))))
  909. (t (message "%s" reftex-no-follow-message) nil))))
  910. (when match
  911. (goto-char (match-beginning 0))
  912. (if (not (= (point) (point-max))) (recenter 1))
  913. (reftex-highlight 0 (match-beginning 0) (match-end 0) (current-buffer)))
  914. match))
  915. (defun reftex-make-desperate-section-regexp (old)
  916. ;; Return a regexp which will still match a section statement even if
  917. ;; x-symbol or isotex or the like have been at work in the mean time.
  918. (let* ((n (1+ (string-match "[[{]" old)))
  919. (new (regexp-quote (substring old 0 (1+ (string-match "[[{]" old)))))
  920. (old (substring old n)))
  921. (while (string-match
  922. "\\([\r\n]\\)\\|\\(\\`\\|[ \t\n\r]\\)\\([a-zA-Z0-9]+\\)\\([ \t\n\r]\\|}\\'\\)"
  923. old)
  924. (if (match-beginning 1)
  925. (setq new (concat new "[^\n\r]*[\n\r]"))
  926. (setq new (concat new "[^\n\r]*" (match-string 3 old))))
  927. (setq old (substring old (match-end 0))))
  928. new))
  929. ;; Auto recentering of TOC window
  930. (defun reftex-recenter-toc-when-idle ()
  931. (and (> (buffer-size) 5)
  932. reftex-mode
  933. (not (active-minibuffer-window))
  934. (fboundp 'reftex-toc-mode)
  935. (get-buffer-window "*toc*" 'visible)
  936. (string= reftex-last-toc-master (reftex-TeX-master-file))
  937. (let (current-prefix-arg)
  938. (reftex-toc-recenter))))
  939. ;;;###autoload
  940. (defun reftex-toggle-auto-toc-recenter ()
  941. "Toggle the automatic recentering of the TOC window.
  942. When active, leaving point idle will make the TOC window jump to the correct
  943. section."
  944. (interactive)
  945. (if reftex-toc-auto-recenter-timer
  946. (progn
  947. (if (featurep 'xemacs)
  948. (delete-itimer reftex-toc-auto-recenter-timer)
  949. (cancel-timer reftex-toc-auto-recenter-timer))
  950. (setq reftex-toc-auto-recenter-timer nil)
  951. (message "Automatic recentering of TOC window was turned off"))
  952. (setq reftex-toc-auto-recenter-timer
  953. (if (featurep 'xemacs)
  954. (start-itimer "RefTeX Idle Timer for recenter"
  955. 'reftex-recenter-toc-when-idle
  956. reftex-idle-time reftex-idle-time t)
  957. (run-with-idle-timer
  958. reftex-idle-time t 'reftex-recenter-toc-when-idle)))
  959. (message "Automatic recentering of TOC window was turned on")))
  960. (defun reftex-toc-toggle-dedicated-frame ()
  961. "Toggle the display of a separate frame for the TOC.
  962. This frame is not used by the `reftex-toc' commands, but it is useful to
  963. always show the current section in connection with the option
  964. `reftex-auto-recenter-toc'."
  965. (interactive)
  966. (catch 'exit
  967. (let* ((frames (frame-list)) frame
  968. (get-frame-prop-func (if (fboundp 'frame-property)
  969. 'frame-property
  970. 'frame-parameter)))
  971. (while (setq frame (pop frames))
  972. (if (equal (funcall get-frame-prop-func frame 'name)
  973. "RefTeX TOC Frame")
  974. (progn
  975. (delete-frame frame)
  976. (throw 'exit nil))))
  977. (reftex-make-separate-toc-frame))))
  978. (defun reftex-make-separate-toc-frame ()
  979. ;; Create a new fame showing only the TOC buffer.
  980. (let ((current-frame (selected-frame))
  981. (current-window (selected-window))
  982. (current-toc-window (get-buffer-window "*toc*" 'visible))
  983. current-toc-frame)
  984. (if (and current-toc-window
  985. (not (equal (selected-frame) (window-frame current-toc-window))))
  986. nil
  987. (setq current-toc-frame
  988. (make-frame
  989. '((name . "RefTeX TOC Frame")
  990. (height . 20) (width . 60)
  991. (unsplittable . t)
  992. (minibuffer . nil)
  993. (default-toolbar-visible-p . nil)
  994. (menubar-visible-p . nil)
  995. (horizontal-scrollbar-visible-p . nil))))
  996. (select-frame current-toc-frame)
  997. (switch-to-buffer "*toc*")
  998. (select-frame current-frame)
  999. (cond ((fboundp 'x-focus-frame)
  1000. (x-focus-frame current-frame))
  1001. ((and (featurep 'xemacs) ; `focus-frame' is a nop in Emacs.
  1002. (fboundp 'focus-frame))
  1003. (focus-frame current-frame)))
  1004. (select-window current-window)
  1005. (when (eq reftex-auto-recenter-toc 'frame)
  1006. (unless reftex-toc-auto-recenter-timer
  1007. (reftex-toggle-auto-toc-recenter))
  1008. (add-hook 'delete-frame-hook 'reftex-toc-delete-frame-hook)))))
  1009. (defun reftex-toc-delete-frame-hook (frame)
  1010. (if (and reftex-toc-auto-recenter-timer
  1011. (reftex-toc-dframe-p frame))
  1012. (progn
  1013. (reftex-toggle-auto-toc-recenter))))
  1014. ;;; reftex-toc.el ends here
  1015. ;; Local Variables:
  1016. ;; generated-autoload-file: "reftex-loaddefs.el"
  1017. ;; End: