1 ;;; reftex-toc.el --- RefTeX's table of contents mode
3 ;; Copyright (C) 1997-2000, 2003-2017 Free Software Foundation, Inc.
5 ;; Author: Carsten Dominik <dominik@science.uva.nl>
6 ;; Maintainer: auctex-devel@gnu.org
8 ;; This file is part of GNU Emacs.
10 ;; GNU Emacs is free software: you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation, either version 3 of the License, or
13 ;; (at your option) any later version.
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>.
31 (define-obsolete-variable-alias 'reftex-toc-map
'reftex-toc-mode-map
"24.1")
32 (defvar reftex-toc-mode-map
33 (let ((map (make-sparse-keymap)))
35 (define-key map
(if (featurep 'xemacs
) [(button2)] [(mouse-2)])
36 'reftex-toc-mouse-goto-line-and-hide
)
37 (define-key map
[follow-link
] 'mouse-face
)
39 (substitute-key-definition
40 'next-line
'reftex-toc-next map global-map
)
41 (substitute-key-definition
42 'previous-line
'reftex-toc-previous map global-map
)
44 (define-key map
"n" 'reftex-toc-next
)
45 (define-key map
"p" 'reftex-toc-previous
)
46 (define-key map
"?" 'reftex-toc-show-help
)
47 (define-key map
" " 'reftex-toc-view-line
)
48 (define-key map
"\C-m" 'reftex-toc-goto-line-and-hide
)
49 (define-key map
"\C-i" 'reftex-toc-goto-line
)
50 (define-key map
"\C-c>" 'reftex-toc-display-index
)
51 (define-key map
"r" 'reftex-toc-rescan
)
52 (define-key map
"R" 'reftex-toc-Rescan
)
53 (define-key map
"q" 'reftex-toc-quit
) ;
54 (define-key map
"k" 'reftex-toc-quit-and-kill
)
55 (define-key map
"f" 'reftex-toc-toggle-follow
) ;
56 (define-key map
"a" 'reftex-toggle-auto-toc-recenter
)
57 (define-key map
"d" 'reftex-toc-toggle-dedicated-frame
)
58 (define-key map
"F" 'reftex-toc-toggle-file-boundary
)
59 (define-key map
"i" 'reftex-toc-toggle-index
)
60 (define-key map
"l" 'reftex-toc-toggle-labels
)
61 (define-key map
"t" 'reftex-toc-max-level
)
62 (define-key map
"c" 'reftex-toc-toggle-context
)
63 ;; (define-key map "%" 'reftex-toc-toggle-commented)
64 (define-key map
"\M-%" 'reftex-toc-rename-label
)
65 (define-key map
"x" 'reftex-toc-external
)
66 (define-key map
"z" 'reftex-toc-jump
)
67 (define-key map
"." 'reftex-toc-show-calling-point
)
68 (define-key map
"\C-c\C-n" 'reftex-toc-next-heading
)
69 (define-key map
"\C-c\C-p" 'reftex-toc-previous-heading
)
70 (define-key map
">" 'reftex-toc-demote
)
71 (define-key map
"<" 'reftex-toc-promote
)
75 "Menu for Table of Contents buffer"
77 ["Show Location" reftex-toc-view-line t
]
78 ["Go To Location" reftex-toc-goto-line t
]
79 ["Exit & Go To Location" reftex-toc-goto-line-and-hide t
]
80 ["Show Calling Point" reftex-toc-show-calling-point t
]
81 ["Quit" reftex-toc-quit t
]
84 ["Promote" reftex-toc-promote t
]
85 ["Demote" reftex-toc-demote t
]
86 ["Rename Label" reftex-toc-rename-label t
])
88 ["Index" reftex-toc-display-index t
]
89 ["External Document TOC " reftex-toc-external t
]
92 ["Rebuilt *toc* Buffer" revert-buffer t
]
93 ["Rescan One File" reftex-toc-rescan reftex-enable-partial-scans
]
94 ["Rescan Entire Document" reftex-toc-Rescan t
])
97 ["File Boundaries" reftex-toc-toggle-file-boundary
:style toggle
98 :selected reftex-toc-include-file-boundaries
]
99 ["Labels" reftex-toc-toggle-labels
:style toggle
100 :selected reftex-toc-include-labels
]
101 ["Index Entries" reftex-toc-toggle-index
:style toggle
102 :selected reftex-toc-include-index-entries
]
103 ["Context" reftex-toc-toggle-context
:style toggle
104 :selected reftex-toc-include-context
]
106 ["Follow Mode" reftex-toc-toggle-follow
:style toggle
107 :selected reftex-toc-follow-mode
]
108 ["Auto Recenter" reftex-toggle-auto-toc-recenter
:style toggle
109 :selected reftex-toc-auto-recenter-timer
]
110 ["Dedicated Frame" reftex-toc-toggle-dedicated-frame t
])
112 ["Help" reftex-toc-show-help t
]))
115 "Keymap used for *toc* buffer.")
117 (defvar reftex-toc-menu
)
118 (defvar reftex-last-window-height nil
)
119 (defvar reftex-last-window-width nil
)
120 (defvar reftex-toc-include-labels-indicator nil
)
121 (defvar reftex-toc-include-index-indicator nil
)
122 (defvar reftex-toc-max-level-indicator nil
)
124 (define-derived-mode reftex-toc-mode special-mode
"TOC"
125 "Major mode for managing Table of Contents for LaTeX files.
126 This buffer was created with RefTeX.
127 Press `?' for a summary of important key bindings.
129 Here are all local bindings.
131 \\{reftex-toc-mode-map}"
132 (set (make-local-variable 'transient-mark-mode
) t
)
133 (when (featurep 'xemacs
)
134 (set (make-local-variable 'zmacs-regions
) t
))
135 (set (make-local-variable 'revert-buffer-function
) 'reftex-toc-revert
)
136 (set (make-local-variable 'reftex-toc-include-labels-indicator
) "")
137 (set (make-local-variable 'reftex-toc-max-level-indicator
)
138 (if (= reftex-toc-max-level
100)
140 (int-to-string reftex-toc-max-level
)))
141 (setq mode-line-format
142 (list "---- " 'mode-line-buffer-identification
143 " " 'global-mode-string
" (" mode-name
")"
144 " L<" 'reftex-toc-include-labels-indicator
">"
145 " I<" 'reftex-toc-include-index-indicator
">"
146 " T<" 'reftex-toc-max-level-indicator
">"
148 (setq truncate-lines t
)
149 (when (featurep 'xemacs
)
150 ;; XEmacs needs the call to make-local-hook
151 (make-local-hook 'post-command-hook
)
152 (make-local-hook 'pre-command-hook
))
153 (make-local-variable 'reftex-last-follow-point
)
154 (add-hook 'post-command-hook
'reftex-toc-post-command-hook nil t
)
155 (add-hook 'pre-command-hook
'reftex-toc-pre-command-hook nil t
)
156 (easy-menu-add reftex-toc-menu reftex-toc-mode-map
))
158 (defvar reftex-last-toc-file nil
159 "Stores the file name from which `reftex-toc' was called. For redo command.")
162 (defvar reftex-toc-return-marker
(make-marker)
163 "Marker which makes it possible to return from TOC to old position.")
165 (defconst reftex-toc-help
166 " AVAILABLE KEYS IN TOC BUFFER
167 ============================
168 n / p next-line / previous-line
169 SPC Show the corresponding location of the LaTeX document.
170 TAB Goto the location and keep the TOC window.
171 RET Goto the location and hide the TOC window (also on mouse-2).
172 < / > Promote / Demote section, or all sections in region.
173 C-c > Display Index. With prefix arg, restrict index to current section.
174 q / k Hide/Kill *toc* buffer, return to position of reftex-toc command.
175 l i c F Toggle display of [l]abels, [i]ndex, [c]ontext, [F]ile borders.
176 t Change maximum toc depth (e.g. `3 t' hides levels greater than 3).
177 f / g Toggle follow mode / Refresh *toc* buffer.
178 a / d Toggle auto recenter / Toggle dedicated frame
179 r / C-u r Reparse the LaTeX document / Reparse entire LaTeX document.
180 . In other window, show position from where `reftex-toc' was called.
181 M-% Global search and replace to rename label at point.
182 x Switch to TOC of external document (with LaTeX package `xr').
183 z Jump to a specific section (e.g. '3 z' goes to section 3).")
185 (defvar reftex--rebuilding-toc nil
)
188 (defun reftex-toc (&optional _rebuild reuse
)
189 ;; FIXME: Get rid of the `rebuild' argument.
190 "Show the table of contents for the current document.
191 When called with a raw C-u prefix, rescan the document first."
193 ;; The REUSE argument means, search all visible frames for a window
194 ;; displaying the toc window. If yes, reuse this window.
198 (if (or (not (string= reftex-last-toc-master
(reftex-TeX-master-file)))
199 ;; FIXME: use (interactive "P") to receive current-prefix-arg as
200 ;; an argument instead of using the var here, which forces us to set
201 ;; current-prefix-arg in the callers.
203 (reftex-erase-buffer "*toc*"))
205 (setq reftex-last-toc-file
(buffer-file-name))
206 (setq reftex-last-toc-master
(reftex-TeX-master-file))
208 (set-marker reftex-toc-return-marker
(point))
210 ;; If follow mode is active, arrange to delay it one command
211 (if reftex-toc-follow-mode
212 (setq reftex-toc-follow-mode
1))
214 (and reftex-toc-include-index-entries
215 (reftex-ensure-index-support))
216 (or reftex-support-index
217 (setq reftex-toc-include-index-entries nil
))
219 ;; Ensure access to scanning info and rescan buffer if prefix arg is '(4)
220 (reftex-access-scan-info current-prefix-arg
)
222 (let* ((this-buf (current-buffer))
223 (docstruct-symbol reftex-docstruct-symbol
)
224 (xr-data (assq 'xr
(symbol-value reftex-docstruct-symbol
)))
225 (xr-alist (cons (cons "" (buffer-file-name)) (nth 1 xr-data
)))
226 (here-I-am (if reftex--rebuilding-toc
227 (get 'reftex-toc
:reftex-data
)
228 (car (reftex-where-am-I))))
229 (unsplittable (if (fboundp 'frame-property
)
230 (frame-property (selected-frame) 'unsplittable
)
231 (frame-parameter nil
'unsplittable
)))
234 (if (setq toc-window
(get-buffer-window
236 (if reuse
'visible
)))
237 (select-window toc-window
)
238 (when (or (not reftex-toc-keep-other-windows
)
239 (< (window-height) (* 2 window-min-height
)))
240 (delete-other-windows))
242 (setq reftex-last-window-width
(window-total-width)
243 reftex-last-window-height
(window-height)) ; remember
246 (if reftex-toc-split-windows-horizontally
248 (floor (* (window-total-width)
249 reftex-toc-split-windows-fraction
)))
251 (floor (* (window-height)
252 reftex-toc-split-windows-fraction
)))))
254 (switch-to-buffer "*toc*"))
256 (or (eq major-mode
'reftex-toc-mode
) (reftex-toc-mode))
257 (set (make-local-variable 'reftex-docstruct-symbol
) docstruct-symbol
)
258 (setq reftex-toc-include-labels-indicator
259 (if (eq reftex-toc-include-labels t
)
261 reftex-toc-include-labels
))
262 (setq reftex-toc-include-index-indicator
263 (if (eq reftex-toc-include-index-entries t
)
265 reftex-toc-include-index-entries
))
269 ;; buffer is empty - fill it with the table of contents
270 (message "Building *toc* buffer...")
272 (setq buffer-read-only nil
)
274 "TABLE-OF-CONTENTS on %s
275 SPC=view TAB=goto RET=goto+hide [q]uit [r]escan [l]abels [f]ollow [x]r [?]Help
276 ------------------------------------------------------------------------------
277 " (abbreviate-file-name reftex-last-toc-master
)))
279 (if (reftex-use-fonts)
280 (put-text-property (point-min) (point) 'font-lock-face reftex-toc-header-face
))
281 (if (fboundp 'cursor-intangible-mode
)
282 (cursor-intangible-mode 1)
283 ;; If `cursor-intangible' is not available, fallback on the old
284 ;; intrusive `intangible' property.
285 (put-text-property (point-min) (point) 'intangible t
))
286 (add-text-properties (point-min) (point)
287 '(cursor-intangible t
288 front-sticky
(cursor-intangible)
289 rear-nonsticky
(cursor-intangible)))
290 (put-text-property (point-min) (1+ (point-min)) 'xr-alist xr-alist
)
293 (reftex-insert-docstruct
296 reftex-toc-include-labels
297 reftex-toc-include-index-entries
298 reftex-toc-include-file-boundaries
299 reftex-toc-include-context
307 (run-hooks 'reftex-display-copied-context-hook
)
308 (message "Building *toc* buffer...done.")
309 (setq buffer-read-only t
))
311 ;; Only compute the offset
313 (or (reftex-get-offset this-buf here-I-am
314 (if reftex-toc-include-labels
" " nil
)
316 reftex-toc-include-index-entries
317 reftex-toc-include-file-boundaries
)
318 (reftex-last-assoc-before-elt
320 (symbol-value reftex-docstruct-symbol
))))
321 (put 'reftex-toc
:reftex-line
3)
322 (goto-char (point-min))
325 ;; Find the correct starting point
326 (reftex-find-start-point (point) offset
(get 'reftex-toc
:reftex-line
))
327 (setq reftex-last-follow-point
(point))))
330 (defun reftex-toc-recenter (&optional arg
)
331 "Display the TOC window and highlight line corresponding to current position."
333 (let ((buf (current-buffer))
334 (frame (selected-frame)))
336 (if (= (count-lines 1 (point)) 2)
337 (let ((current-prefix-arg nil
))
338 (select-window (get-buffer-window buf frame
))
340 (and (> (point) 1) ;FIXME: Is this point-min or do we care about narrowing?
341 (not (get-text-property (point) 'cursor-intangible
))
342 (memq reftex-highlight-selection
'(cursor both
))
344 (or (previous-single-property-change
345 (min (point-max) (1+ (point))) :data
)
347 (or (next-single-property-change (point) :data
)
349 (select-window (get-buffer-window buf frame
))))
351 (defun reftex-toc-pre-command-hook ()
352 ;; used as pre command hook in *toc* buffer
353 (reftex-unhighlight 0)
356 (defun reftex-toc-post-command-hook ()
357 ;; used in the post-command-hook for the *toc* buffer
358 ;; FIXME: Lots of redundancy with reftex-index-post-command-hook!
359 (when (get-text-property (point) :data
)
360 (put 'reftex-toc
:reftex-data
(get-text-property (point) :data
))
361 (and (> (point) 1) ;FIXME: Is this point-min or do we care about narrowing?
362 (not (get-text-property (point) 'cursor-intangible
))
363 (memq reftex-highlight-selection
'(cursor both
))
365 (or (previous-single-property-change (1+ (point)) :data
)
367 (or (next-single-property-change (point) :data
)
369 (if (integerp reftex-toc-follow-mode
)
370 ;; remove delayed action
371 (setq reftex-toc-follow-mode t
)
372 (and (not (reftex-toc-dframe-p))
373 reftex-toc-follow-mode
374 (not (equal reftex-last-follow-point
(point)))
375 ;; show context in other window
376 (setq reftex-last-follow-point
(point))
378 (reftex-toc-visit-location nil
(not reftex-revisit-to-follow
))
381 (defun reftex-re-enlarge ()
382 "Enlarge window to a remembered size."
383 ;; FIXME: reftex-last-window-width might be the width of another window on
384 ;; another frame, so the enlarge-window call might make no sense.
385 ;; We should just use `quit-window' instead nowadays.
386 (let ((count (if reftex-toc-split-windows-horizontally
387 (- (or reftex-last-window-width
(window-total-width))
388 (window-total-width))
389 (- (or reftex-last-window-height
(window-height))
392 (with-demoted-errors ;E.g. the window might be the root window!
393 (enlarge-window count reftex-toc-split-windows-horizontally
)))))
395 (defun reftex-toc-dframe-p (&optional frame error
)
396 ;; Check if FRAME is the dedicated TOC frame.
397 ;; If yes, and ERROR is non-nil, throw an error.
398 (setq frame
(or frame
(selected-frame)))
400 (if (fboundp 'frame-property
)
401 (frame-property frame
'name
)
402 (frame-parameter frame
'name
))
403 "RefTeX TOC Frame")))
405 (error "This frame is view-only. Use `C-c =' to create TOC window for commands"))
408 (defun reftex-toc-show-help ()
409 "Show a summary of special key bindings."
411 (reftex-toc-dframe-p nil
'error
)
412 (with-output-to-temp-buffer "*RefTeX Help*"
413 (princ reftex-toc-help
))
414 (reftex-enlarge-to-fit "*RefTeX Help*" t
)
415 ;; If follow mode is active, arrange to delay it one command
416 (if reftex-toc-follow-mode
417 (setq reftex-toc-follow-mode
1)))
419 (defun reftex-toc-next (&optional _arg
)
420 "Move to next selectable item."
422 (when (featurep 'xemacs
) (setq zmacs-region-stays t
))
423 (setq reftex-callback-fwd t
)
424 (or (eobp) (forward-char 1))
425 (goto-char (or (next-single-property-change (point) :data
)
427 (defun reftex-toc-previous (&optional _arg
)
428 "Move to previous selectable item."
430 (when (featurep 'xemacs
) (setq zmacs-region-stays t
))
431 (setq reftex-callback-fwd nil
)
432 (goto-char (or (previous-single-property-change (point) :data
)
434 (defun reftex-toc-next-heading (&optional arg
)
435 "Move to next table of contents line."
437 (when (featurep 'xemacs
) (setq zmacs-region-stays t
))
439 (re-search-forward "^ " nil t arg
)
441 (defun reftex-toc-previous-heading (&optional arg
)
442 "Move to previous table of contents line."
444 (when (featurep 'xemacs
) (setq zmacs-region-stays t
))
445 (re-search-backward "^ " nil t arg
))
446 (defun reftex-toc-toggle-follow ()
447 "Toggle follow (other window follows with context)."
449 (setq reftex-last-follow-point -
1)
450 (setq reftex-toc-follow-mode
(not reftex-toc-follow-mode
)))
451 (defun reftex-toc-toggle-file-boundary ()
452 "Toggle inclusion of file boundaries in *toc* buffer."
454 (setq reftex-toc-include-file-boundaries
455 (not reftex-toc-include-file-boundaries
))
457 (defun reftex-toc-toggle-labels (arg)
458 "Toggle inclusion of labels in *toc* buffer.
459 With prefix ARG, prompt for a label type and include only labels of
462 (setq reftex-toc-include-labels
463 (if arg
(reftex-query-label-type)
464 (not reftex-toc-include-labels
)))
466 (defun reftex-toc-toggle-index (arg)
467 "Toggle inclusion of index in *toc* buffer.
468 With prefix arg, prompt for an index tag and include only entries of that
471 (setq reftex-toc-include-index-entries
472 (if arg
(reftex-index-select-tag)
473 (not reftex-toc-include-index-entries
)))
475 (defun reftex-toc-toggle-context ()
476 "Toggle inclusion of label context in *toc* buffer.
477 Label context is only displayed when the labels are there as well."
479 (setq reftex-toc-include-context
(not reftex-toc-include-context
))
481 (defun reftex-toc-max-level (arg)
482 "Set the maximum level of TOC lines in this buffer to value of prefix ARG.
483 When no prefix is given, set the max level to a large number, so that all
484 levels are shown. For example, to set the level to 3, type `3 m'."
486 (setq reftex-toc-max-level
(if arg
487 (prefix-numeric-value arg
)
489 (setq reftex-toc-max-level-indicator
490 (if arg
(int-to-string reftex-toc-max-level
) "ALL"))
492 (defun reftex-toc-view-line ()
493 "View document location in other window."
495 (reftex-toc-dframe-p nil
'error
)
496 (reftex-toc-visit-location))
497 (defun reftex-toc-goto-line-and-hide ()
498 "Go to document location in other window. Hide the TOC window."
500 (reftex-toc-dframe-p nil
'error
)
501 (reftex-toc-visit-location 'hide
))
502 (defun reftex-toc-goto-line ()
503 "Go to document location in other window. TOC window stays."
505 (reftex-toc-dframe-p nil
'error
)
506 (reftex-toc-visit-location t
))
507 (defun reftex-toc-mouse-goto-line-and-hide (ev)
508 "Go to document location in other window. Hide the TOC window."
511 (reftex-toc-dframe-p nil
'error
)
512 (reftex-toc-visit-location 'hide
))
513 (defun reftex-toc-show-calling-point ()
514 "Show point where `reftex-toc' was called from."
516 (reftex-toc-dframe-p nil
'error
)
517 (let ((this-window (selected-window)))
520 (switch-to-buffer-other-window
521 (marker-buffer reftex-toc-return-marker
))
522 (goto-char (marker-position reftex-toc-return-marker
))
524 (select-window this-window
))))
525 (defun reftex-toc-quit ()
526 "Hide the TOC window and do not move point.
527 If the TOC window is the only window on the dedicated TOC frame, the frame
530 (if (and (one-window-p)
531 (reftex-toc-dframe-p)
532 (> (length (frame-list)) 1))
534 (or (one-window-p) (delete-window))
535 (switch-to-buffer (marker-buffer reftex-toc-return-marker
))
537 (goto-char (or (marker-position reftex-toc-return-marker
) (point)))))
538 (defun reftex-toc-quit-and-kill ()
539 "Kill the *toc* buffer."
541 (kill-buffer "*toc*")
542 (or (one-window-p) (delete-window))
543 (switch-to-buffer (marker-buffer reftex-toc-return-marker
))
545 (goto-char (marker-position reftex-toc-return-marker
)))
546 (defun reftex-toc-display-index (&optional arg
)
547 "Display the index buffer for the current document.
548 This works just like `reftex-display-index' from a LaTeX buffer.
549 With prefix arg 1, restrict index to the section at point."
551 (reftex-toc-dframe-p nil
'error
)
552 (let ((data (get-text-property (point) :data
))
553 (docstruct (symbol-value reftex-docstruct-symbol
))
556 (setq bor
(reftex-last-assoc-before-elt 'toc data docstruct
)
557 eor
(assoc 'toc
(cdr (memq bor docstruct
)))
558 restr
(list (nth 6 bor
) bor eor
)))
559 (reftex-toc-goto-line)
560 (reftex-display-index (if restr nil arg
) restr
)))
562 ;; Rescanning the document and rebuilding the TOC buffer.
563 (defun reftex-toc-rescan (&rest _
)
564 "Regenerate the *toc* buffer by reparsing file of section at point."
566 (if (and reftex-enable-partial-scans
567 (null current-prefix-arg
))
568 (let* ((data (get-text-property (point) :data
))
570 (file (cond ((eq what
'toc
) (nth 3 data
))
571 ((memq what
'(eof bof file-error
)) (nth 1 data
))
572 ((stringp what
) (nth 3 data
))
573 ((eq what
'index
) (nth 3 data
))))
574 (line (+ (count-lines (point-min) (point)) (if (bolp) 1 0))))
576 (error "Don't know which file to rescan. Try `C-u r'")
577 (put 'reftex-toc
:reftex-line line
)
578 (switch-to-buffer-other-window
579 (reftex-get-file-buffer-force file
))
580 (setq current-prefix-arg
'(4))
581 (let ((reftex--rebuilding-toc t
))
584 (reftex-kill-temporary-buffers))
586 (defun reftex-toc-Rescan (&rest _
)
587 "Regenerate the *toc* buffer by reparsing the entire document."
589 (let* ((line (+ (count-lines (point-min) (point)) (if (bolp) 1 0))))
590 (put 'reftex-toc
:reftex-line line
))
591 (switch-to-buffer-other-window
592 (reftex-get-file-buffer-force reftex-last-toc-file
))
593 (setq current-prefix-arg
'(16))
594 (let ((reftex--rebuilding-toc t
))
597 (defun reftex-toc-revert (&rest _
)
598 "Regenerate the TOC from the internal lists."
601 (if (fboundp 'frame-property
)
602 (frame-property (selected-frame) 'unsplittable
)
603 (frame-parameter nil
'unsplittable
)))
604 (reftex--rebuilding-toc t
))
607 (reftex-get-file-buffer-force reftex-last-toc-file
))
608 (switch-to-buffer-other-window
609 (reftex-get-file-buffer-force reftex-last-toc-file
))))
610 (reftex-erase-buffer "*toc*")
611 (setq current-prefix-arg nil
)
614 (defun reftex-toc-external (&rest _
)
615 "Switch to table of contents of an external document."
617 (reftex-toc-dframe-p nil
'error
)
618 (let* ((old-buf (current-buffer))
619 (xr-alist (get-text-property 1 'xr-alist
))
620 (xr-index (reftex-select-external-document
622 (switch-to-buffer-other-window (or (reftex-get-file-buffer-force
623 (cdr (nth xr-index xr-alist
)))
624 (error "Cannot switch document")))
626 (if (equal old-buf
(current-buffer))
628 (message "Switched document"))))
630 (defun reftex-toc-jump (arg)
631 "Jump to a specific section. E.g. '3 z' jumps to section 3.
632 Useful for large TOCs."
634 (goto-char (point-min))
636 (concat "^ *" (number-to-string (if (numberp arg
) arg
1)) " ")
640 ;; Promotion/Demotion stuff
642 (defvar reftex--pro-or-de
)
643 (defvar reftex--start-line
)
644 (defvar reftex--mark-line
)
646 (defun reftex-toc-demote (&optional _arg
)
647 "Demote section at point. If region is active, apply to all in region."
649 (reftex-toc-do-promote 1))
650 (defun reftex-toc-promote (&optional _arg
)
651 "Promote section at point. If region is active, apply to all in region."
653 (reftex-toc-do-promote -
1))
654 (defun reftex-toc-do-promote (delta)
655 "Workhorse for `reftex-toc-promote' and `reftex-toc-demote'.
656 Changes the level of sections in the current region, or just the section at
658 ;; We should not do anything unless we are sure this is going to work for
659 ;; each section in the region. Therefore we first collect information and
661 (let* ((reftex--start-line (+ (count-lines (point-min) (point))
664 (if (reftex-region-active-p)
665 (save-excursion (goto-char (mark))
666 (+ (count-lines (point-min) (point))
669 (reftex--pro-or-de (if (> delta
0) "de" "pro"))
670 beg end entries data sections nsec msg
)
673 (if (reftex-region-active-p)
674 ;; A region is dangerous, check if we have a brand new scan,
675 ;; to make sure we are not missing any section statements.
676 (if (not (reftex-toc-check-docstruct))
677 (reftex-toc-load-all-files-for-promotion) ;; exits
678 (setq beg
(min (point) (mark))
679 end
(max (point) (mark))))
680 (setq beg
(point) end
(point)))
682 (while (and (setq data
(get-text-property (point) :data
))
684 (if (eq (car data
) 'toc
) (push (cons data
(point)) entries
))
685 (goto-char (or (next-single-property-change (point) :data
)
687 (setq entries
(nreverse entries
))
688 ;; Get the relevant section numbers, for an informative message
689 (goto-char start-pos
)
690 (setq sections
(reftex-toc-extract-section-number (car entries
)))
691 (if (> (setq nsec
(length entries
)) 1)
694 (reftex-toc-extract-section-number
695 (nth (1- nsec
) entries
)))))
696 ;; Run through the list and prepare the changes.
697 (setq entries
(mapcar
698 (lambda (e) (reftex-toc-promote-prepare e delta
))
700 ;; Ask for permission
701 (if (or (not reftex-toc-confirm-promotion
) ; never confirm
702 (and (integerp reftex-toc-confirm-promotion
) ; confirm if many
703 (< nsec reftex-toc-confirm-promotion
))
705 (format "%s %d toc-entr%s (section%s %s)? "
706 (if (< delta
0) "Promote" "Demote")
708 (if (= 1 nsec
) "y" "ies")
709 (if (= 1 nsec
) "" "s")
711 nil
; we have permission, do nothing
712 (error "Abort")) ; abort, we don't have permission
714 (mapc 'reftex-toc-promote-action entries
)
715 ;; Rescan the document and rebuilt the toc buffer
716 (save-window-excursion
718 (reftex-toc-restore-region reftex--start-line reftex--mark-line
)
719 (message "%d section%s %smoted"
720 nsec
(if (= 1 nsec
) "" "s") reftex--pro-or-de
)
722 (if msg
(progn (ding) (message "%s" msg
)))))
725 (defun reftex-toc-restore-region (point-line &optional mark-line
)
728 (goto-char (point-min))
729 (forward-line (1- mark-line
))
732 (goto-char (point-min))
733 (forward-line (1- point-line
)))
736 (if (featurep 'xemacs
)
737 (zmacs-activate-region)
739 deactivate-mark nil
)))))
741 (defun reftex-toc-promote-prepare (x delta
)
742 "Look at a TOC entry and see if we could pro/demote it.
743 This function prepares everything for the change, but does not do it.
744 The return value is a list with information needed when doing the
745 promotion/demotion later. DELTA is the level change."
746 (let* ((data (car x
))
748 (marker (nth 4 data
))
749 (literal (nth 7 data
))
752 ;; Here follows some paranoid code to make very sure we are not
753 ;; going to break anything
755 (if (and (markerp marker
) (marker-buffer marker
))
756 ;; Buffer is still live and we have the marker.
758 (with-current-buffer (marker-buffer marker
)
759 ;; Goto the buffer and check of section is unchanged
760 (goto-char (marker-position marker
))
761 (if (looking-at (regexp-quote literal
))
762 ;; OK, get the makro name
764 (beginning-of-line 1)
765 (if (looking-at reftex-section-regexp
)
766 (setq name
(reftex-match-string 2))
767 (error "Something is wrong! Contact maintainer!")))
768 ;; Section has changed, request scan and loading
769 ;; We use a variable to delay until after the safe-exc.
770 ;; because otherwise we lose the region.
772 ;; Scan document and load all files, this exits command
773 (if load
(reftex-toc-load-all-files-for-promotion))) ; exits
774 ;; We don't have a live marker: scan and load files.
775 (reftex-toc-load-all-files-for-promotion)))
776 (level (cdr (assoc name reftex-section-levels-all
)))
777 (_ (if (not (integerp level
))
779 (goto-char toc-point
)
780 (error "Cannot %smote special sections" reftex--pro-or-de
))))
781 (newlevel (if (>= level
0) (+ delta level
) (- level delta
)))
782 (_ (if (or (and (>= level
0) (= newlevel -
1))
783 (and (< level
0) (= newlevel
0)))
784 (error "Cannot %smote \\%s" reftex--pro-or-de name
)))
785 (newname (reftex-toc-newhead-from-alist newlevel name
786 reftex-section-levels-all
)))
787 (if (and name newname
)
788 (list data name newname toc-point
)
789 (goto-char toc-point
)
790 (error "Cannot %smote \\%s" reftex--pro-or-de name
))))
792 (defun reftex-toc-promote-action (x)
793 "Change the level of a TOC entry.
794 `reftex--pro-or-de' is assumed to be dynamically scoped into this function."
795 (let* ((data (car x
))
798 (marker (nth 4 data
)))
799 (with-current-buffer (marker-buffer marker
)
800 (goto-char (marker-position marker
))
801 (if (looking-at (concat "\\([ \t]*" reftex-section-pre-regexp
"\\)" (regexp-quote name
)))
802 (replace-match (concat "\\1" newname
))
803 (error "Fatal error during %smotion" reftex--pro-or-de
)))))
805 (defun reftex-toc-extract-section-number (entry)
806 "Get the numbering of a TOC entry, for message purposes."
807 (if (string-match "\\s-*\\(\\S-+\\)" (nth 2 (car entry
)))
808 (match-string 1 (nth 2 (car entry
)))
811 (defun reftex-toc-newhead-from-alist (nlevel head alist
)
812 "Get new heading with level NLEVEL from ALIST.
813 If there are no such entries, return nil.
814 If there are several different entries with same new level, choose the
815 one with the smallest distance to the association of HEAD in the alist.
816 This makes it possible for promotion to work several sets of headings,
817 if these sets are sorted blocks in the alist."
819 (ass (assoc head al
))
820 (pos (length (memq ass al
)))
821 dist
(mindist 1000) newhead
)
823 (if (equal (cdar al
) nlevel
)
825 (setq dist
(abs (- (length al
) pos
)))
827 (setq newhead
(car (car al
))
832 (defun reftex-toc-check-docstruct ()
833 "Check if the current docstruct is fully up to date and all files visited."
834 ;; We do this by checking if all sections are on the right position
835 (let ((docstruct (symbol-value reftex-docstruct-symbol
))
836 entry marker empoint
)
838 (while (setq entry
(pop docstruct
))
839 (if (eq (car entry
) 'toc
)
841 (setq marker
(nth 4 entry
)
842 empoint
(nth 8 entry
))
843 (if (not (and (markerp marker
)
844 (marker-buffer marker
)
845 (= (marker-position marker
) empoint
)))
846 (throw 'exit nil
)))))
849 (defun reftex-toc-load-all-files-for-promotion ()
850 "Make sure all files of the document are being visited by buffers,
851 and that the scanning info is absolutely up to date.
852 We do this by rescanning with `reftex-keep-temporary-buffers' bound to t.
853 The variable `reftex--pro-or-de' is assumed to be dynamically scoped into this function.
854 When finished, we exit with an error message."
855 (let ((reftex-keep-temporary-buffers t
))
857 (reftex-toc-restore-region reftex--start-line reftex--mark-line
)
859 "TOC had to be updated first. Please check selection and repeat the command.")))
861 (defun reftex-toc-rename-label ()
862 "Rename the currently selected label in the *toc* buffer.
863 This launches a global search and replace in order to rename a label.
864 Renaming a label is hardly ever necessary - the only exception is after
865 promotion/demotion in connection with a package like fancyref, where the
866 label prefix determines the wording of a reference."
868 (let* ((toc (get-text-property (point) :data
))
869 (label (car toc
)) newlabel
)
870 (if (not (stringp label
))
871 (error "This is not a label entry"))
872 (setq newlabel
(read-string (format "Rename label \"%s\" to:" label
)))
873 (if (assoc newlabel
(symbol-value reftex-docstruct-symbol
))
875 (format-message "Label `%s' exists. Use anyway? " label
)))
878 (save-window-excursion
879 (reftex-toc-visit-location t
)
881 (reftex-query-replace-document
882 (concat "{" (regexp-quote label
) "}")
883 (format "{%s}" newlabel
))
885 (reftex-toc-rescan)))
888 (defun reftex-toc-visit-location (&optional final no-revisit
)
889 ;; Visit the tex file corresponding to the TOC entry on the current line.
890 ;; If FINAL is t, stay there
891 ;; If FINAL is 'hide, hide the TOC window.
892 ;; Otherwise, move cursor back into TOC window.
893 ;; NO-REVISIT means don't visit files, just use live buffers.
894 ;; This function is pretty clever about finding back a section heading,
895 ;; even if the buffer is not live, or things like outline, x-symbol etc.
898 (let* ((toc (get-text-property (point) :data
))
899 (toc-window (selected-window))
902 (unless toc
(error "Don't know which TOC line to visit"))
908 (setq match
(reftex-toc-find-section toc no-revisit
)))
910 ((eq (car toc
) 'index
)
912 (setq match
(reftex-index-show-entry toc no-revisit
)))
914 ((memq (car toc
) '(bof eof
))
917 (let ((where (car toc
))
919 (if (or (not no-revisit
) (reftex-get-buffer-visiting file
))
921 (switch-to-buffer-other-window
922 (reftex-get-file-buffer-force file nil
))
923 (goto-char (if (eq where
'bof
) (point-min) (point-max))))
924 (message "%s" reftex-no-follow-message
) nil
))))
928 (setq match
(reftex-show-label-location toc reftex-callback-fwd
932 (select-window toc-window
)
933 (error "Cannot find location"))
935 ;; Use the `final' parameter to decide what to do next.
938 (with-selected-window toc-window
939 (reftex-unhighlight 0)))
941 (let ((window (selected-window))
942 (buffer (window-buffer)))
943 (unless (eq window toc-window
) ;FIXME: Can this happen?
944 (with-selected-window toc-window
945 (reftex-unhighlight 0)
946 (or (one-window-p) (delete-window))))
947 ;; If window is still live, buffer is already visible
948 ;; so let's not make it visible in yet-another-window.
949 (unless (window-live-p window
)
950 ;; FIXME: How could window not be live?
951 (pop-to-buffer-same-window buffer
))
952 (reftex-re-enlarge)))
954 (unless (eq (selected-frame) (window-frame toc-window
))
955 ;; Make sure `toc-window' is not just selected but has focus.
956 (select-frame-set-input-focus (window-frame toc-window
)))
957 (select-window toc-window
)))))
959 (defun reftex-toc-find-section (toc &optional no-revisit
)
960 (let* ((file (nth 3 toc
))
963 (literal (nth 7 toc
))
964 (emergency-point (nth 8 toc
))
967 ((and (markerp marker
) (marker-buffer marker
))
968 ;; Buffer is still live and we have the marker. Should be easy.
969 (switch-to-buffer-other-window (marker-buffer marker
))
971 (goto-char (marker-position marker
))
972 (or (looking-at (regexp-quote literal
))
973 (looking-at (reftex-make-regexp-allow-for-ctrl-m literal
))
974 (looking-at (reftex-make-desperate-section-regexp literal
))
975 (looking-at (concat "\\\\"
979 reftex-section-levels-all
)))
981 ((or (not no-revisit
)
982 (reftex-get-buffer-visiting file
))
983 ;; Marker is lost. Use the backup method.
984 (switch-to-buffer-other-window
985 (reftex-get-file-buffer-force file nil
))
986 (goto-char (or emergency-point
(point-min)))
987 (or (looking-at (regexp-quote literal
))
988 (let ((len (length literal
)))
989 (or (reftex-nearest-match (regexp-quote literal
) len
)
990 (reftex-nearest-match
991 (reftex-make-regexp-allow-for-ctrl-m literal
) len
)
992 (reftex-nearest-match
993 (reftex-make-desperate-section-regexp literal
) len
)))))
994 (t (message "%s" reftex-no-follow-message
) nil
))))
996 (goto-char (match-beginning 0))
997 (if (not (= (point) (point-max))) (recenter 1))
998 (reftex-highlight 0 (match-beginning 0) (match-end 0) (current-buffer)))
1001 (defun reftex-make-desperate-section-regexp (old)
1002 ;; Return a regexp which will still match a section statement even if
1003 ;; x-symbol or isotex or the like have been at work in the mean time.
1004 (let* ((n (1+ (string-match "[[{]" old
)))
1005 (new (regexp-quote (substring old
0 (1+ (string-match "[[{]" old
)))))
1006 (old (substring old n
)))
1007 (while (string-match
1008 "\\([\r\n]\\)\\|\\(\\`\\|[ \t\n\r]\\)\\([a-zA-Z0-9]+\\)\\([ \t\n\r]\\|}\\'\\)"
1010 (if (match-beginning 1)
1011 (setq new
(concat new
"[^\n\r]*[\n\r]"))
1012 (setq new
(concat new
"[^\n\r]*" (match-string 3 old
))))
1013 (setq old
(substring old
(match-end 0))))
1016 ;; Auto recentering of TOC window
1018 (defun reftex-recenter-toc-when-idle ()
1019 (and (> (buffer-size) 5)
1021 (not (active-minibuffer-window))
1022 (fboundp 'reftex-toc-mode
)
1023 (get-buffer-window "*toc*" 'visible
)
1024 (string= reftex-last-toc-master
(reftex-TeX-master-file))
1025 (let (current-prefix-arg)
1026 (reftex-toc-recenter))))
1029 (defun reftex-toggle-auto-toc-recenter ()
1030 "Toggle the automatic recentering of the TOC window.
1031 When active, leaving point idle will make the TOC window jump to the correct
1034 (if reftex-toc-auto-recenter-timer
1036 (if (featurep 'xemacs
)
1037 (delete-itimer reftex-toc-auto-recenter-timer
)
1038 (cancel-timer reftex-toc-auto-recenter-timer
))
1039 (setq reftex-toc-auto-recenter-timer nil
)
1040 (message "Automatic recentering of TOC window was turned off"))
1041 (setq reftex-toc-auto-recenter-timer
1042 (if (featurep 'xemacs
)
1043 (start-itimer "RefTeX Idle Timer for recenter"
1044 'reftex-recenter-toc-when-idle
1045 reftex-idle-time reftex-idle-time t
)
1046 (run-with-idle-timer
1047 reftex-idle-time t
'reftex-recenter-toc-when-idle
)))
1048 (message "Automatic recentering of TOC window was turned on")))
1050 (defun reftex-toc-toggle-dedicated-frame ()
1051 "Toggle the display of a separate frame for the TOC.
1052 This frame is not used by the `reftex-toc' commands, but it is useful to
1053 always show the current section in connection with the option
1054 `reftex-auto-recenter-toc'."
1057 (let* ((frames (frame-list)) frame
1058 (get-frame-prop-func (if (fboundp 'frame-property
)
1061 (while (setq frame
(pop frames
))
1062 (if (equal (funcall get-frame-prop-func frame
'name
)
1065 (delete-frame frame
)
1066 (throw 'exit nil
))))
1067 (reftex-make-separate-toc-frame))))
1069 (defun reftex-make-separate-toc-frame ()
1070 ;; Create a new fame showing only the TOC buffer.
1071 (let ((current-frame (selected-frame))
1072 (current-window (selected-window))
1073 (current-toc-window (get-buffer-window "*toc*" 'visible
))
1075 (if (and current-toc-window
1076 (not (equal (selected-frame) (window-frame current-toc-window
))))
1078 (setq current-toc-frame
1080 '((name .
"RefTeX TOC Frame")
1081 (height .
20) (width .
60)
1084 (default-toolbar-visible-p . nil
)
1085 (menubar-visible-p . nil
)
1086 (horizontal-scrollbar-visible-p . nil
))))
1087 (select-frame current-toc-frame
)
1088 (switch-to-buffer "*toc*")
1089 (select-frame current-frame
)
1090 (cond ((fboundp 'x-focus-frame
)
1091 (x-focus-frame current-frame
))
1092 ((and (featurep 'xemacs
) ; `focus-frame' is a nop in Emacs.
1093 (fboundp 'focus-frame
))
1094 (focus-frame current-frame
)))
1095 (select-window current-window
)
1096 (when (eq reftex-auto-recenter-toc
'frame
)
1097 (unless reftex-toc-auto-recenter-timer
1098 (reftex-toggle-auto-toc-recenter))
1099 (add-hook 'delete-frame-hook
'reftex-toc-delete-frame-hook
)))))
1101 (defun reftex-toc-delete-frame-hook (frame)
1102 (if (and reftex-toc-auto-recenter-timer
1103 (reftex-toc-dframe-p frame
))
1105 (reftex-toggle-auto-toc-recenter))))
1107 ;;; reftex-toc.el ends here
1110 ;; generated-autoload-file: "reftex-loaddefs.el"