1 ;;; reftex-toc.el --- RefTeX's table of contents mode
3 ;; Copyright (C) 1997-2000, 2003-2014 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 <http://www.gnu.org/licenses/>.
27 (eval-when-compile (require 'cl
))
32 (define-obsolete-variable-alias 'reftex-toc-map
'reftex-toc-mode-map
"24.1")
33 (defvar reftex-toc-mode-map
34 (let ((map (make-sparse-keymap)))
36 (define-key map
(if (featurep 'xemacs
) [(button2)] [(mouse-2)])
37 'reftex-toc-mouse-goto-line-and-hide
)
38 (define-key map
[follow-link
] 'mouse-face
)
40 (substitute-key-definition
41 'next-line
'reftex-toc-next map global-map
)
42 (substitute-key-definition
43 'previous-line
'reftex-toc-previous map global-map
)
46 '(("n" . reftex-toc-next
)
47 ("p" . reftex-toc-previous
)
48 ("?" . reftex-toc-show-help
)
49 (" " . reftex-toc-view-line
)
50 ("\C-m" . reftex-toc-goto-line-and-hide
)
51 ("\C-i" . reftex-toc-goto-line
)
52 ("\C-c>" . reftex-toc-display-index
)
53 ("r" . reftex-toc-rescan
)
54 ("R" . reftex-toc-Rescan
)
56 ("q" . reftex-toc-quit
) ;
57 ("k" . reftex-toc-quit-and-kill
)
58 ("f" . reftex-toc-toggle-follow
) ;
59 ("a" . reftex-toggle-auto-toc-recenter
)
60 ("d" . reftex-toc-toggle-dedicated-frame
)
61 ("F" . reftex-toc-toggle-file-boundary
)
62 ("i" . reftex-toc-toggle-index
)
63 ("l" . reftex-toc-toggle-labels
)
64 ("t" . reftex-toc-max-level
)
65 ("c" . reftex-toc-toggle-context
)
66 ;; ("%" . reftex-toc-toggle-commented)
67 ("\M-%" . reftex-toc-rename-label
)
68 ("x" . reftex-toc-external
)
69 ("z" . reftex-toc-jump
)
70 ("." . reftex-toc-show-calling-point
)
71 ("\C-c\C-n" . reftex-toc-next-heading
)
72 ("\C-c\C-p" . reftex-toc-previous-heading
)
73 (">" . reftex-toc-demote
)
74 ("<" . reftex-toc-promote
))
75 do
(define-key map
(car x
) (cdr x
)))
77 (loop for key across
"0123456789" do
78 (define-key map
(vector (list key
)) 'digit-argument
))
79 (define-key map
"-" 'negative-argument
)
83 "Menu for Table of Contents buffer"
85 ["Show Location" reftex-toc-view-line t
]
86 ["Go To Location" reftex-toc-goto-line t
]
87 ["Exit & Go To Location" reftex-toc-goto-line-and-hide t
]
88 ["Show Calling Point" reftex-toc-show-calling-point t
]
89 ["Quit" reftex-toc-quit t
]
92 ["Promote" reftex-toc-promote t
]
93 ["Demote" reftex-toc-demote t
]
94 ["Rename Label" reftex-toc-rename-label t
])
96 ["Index" reftex-toc-display-index t
]
97 ["External Document TOC " reftex-toc-external t
]
100 ["Rebuilt *toc* Buffer" revert-buffer t
]
101 ["Rescan One File" reftex-toc-rescan reftex-enable-partial-scans
]
102 ["Rescan Entire Document" reftex-toc-Rescan t
])
105 ["File Boundaries" reftex-toc-toggle-file-boundary
:style toggle
106 :selected reftex-toc-include-file-boundaries
]
107 ["Labels" reftex-toc-toggle-labels
:style toggle
108 :selected reftex-toc-include-labels
]
109 ["Index Entries" reftex-toc-toggle-index
:style toggle
110 :selected reftex-toc-include-index-entries
]
111 ["Context" reftex-toc-toggle-context
:style toggle
112 :selected reftex-toc-include-context
]
114 ["Follow Mode" reftex-toc-toggle-follow
:style toggle
115 :selected reftex-toc-follow-mode
]
116 ["Auto Recenter" reftex-toggle-auto-toc-recenter
:style toggle
117 :selected reftex-toc-auto-recenter-timer
]
118 ["Dedicated Frame" reftex-toc-toggle-dedicated-frame t
])
120 ["Help" reftex-toc-show-help t
]))
123 "Keymap used for *toc* buffer.")
125 (defvar reftex-toc-menu
)
126 (defvar reftex-last-window-height nil
)
127 (defvar reftex-last-window-width nil
)
128 (defvar reftex-toc-include-labels-indicator nil
)
129 (defvar reftex-toc-include-index-indicator nil
)
130 (defvar reftex-toc-max-level-indicator nil
)
132 (define-derived-mode reftex-toc-mode fundamental-mode
"TOC"
133 "Major mode for managing Table of Contents for LaTeX files.
134 This buffer was created with RefTeX.
135 Press `?' for a summary of important key bindings.
137 Here are all local bindings.
139 \\{reftex-toc-mode-map}"
140 (set (make-local-variable 'transient-mark-mode
) t
)
141 (when (featurep 'xemacs
)
142 (set (make-local-variable 'zmacs-regions
) t
))
143 (set (make-local-variable 'revert-buffer-function
) 'reftex-toc-revert
)
144 (set (make-local-variable 'reftex-toc-include-labels-indicator
) "")
145 (set (make-local-variable 'reftex-toc-max-level-indicator
)
146 (if (= reftex-toc-max-level
100)
148 (int-to-string reftex-toc-max-level
)))
149 (setq mode-line-format
150 (list "---- " 'mode-line-buffer-identification
151 " " 'global-mode-string
" (" mode-name
")"
152 " L<" 'reftex-toc-include-labels-indicator
">"
153 " I<" 'reftex-toc-include-index-indicator
">"
154 " T<" 'reftex-toc-max-level-indicator
">"
156 (setq truncate-lines t
)
157 (when (featurep 'xemacs
)
158 ;; XEmacs needs the call to make-local-hook
159 (make-local-hook 'post-command-hook
)
160 (make-local-hook 'pre-command-hook
))
161 (make-local-variable 'reftex-last-follow-point
)
162 (add-hook 'post-command-hook
'reftex-toc-post-command-hook nil t
)
163 (add-hook 'pre-command-hook
'reftex-toc-pre-command-hook nil t
)
164 (easy-menu-add reftex-toc-menu reftex-toc-mode-map
))
166 (defvar reftex-last-toc-file nil
167 "Stores the file name from which `reftex-toc' was called. For redo command.")
170 (defvar reftex-toc-return-marker
(make-marker)
171 "Marker which makes it possible to return from TOC to old position.")
173 (defconst reftex-toc-help
174 " AVAILABLE KEYS IN TOC BUFFER
175 ============================
176 n / p next-line / previous-line
177 SPC Show the corresponding location of the LaTeX document.
178 TAB Goto the location and keep the TOC window.
179 RET Goto the location and hide the TOC window (also on mouse-2).
180 < / > Promote / Demote section, or all sections in region.
181 C-c > Display Index. With prefix arg, restrict index to current section.
182 q / k Hide/Kill *toc* buffer, return to position of reftex-toc command.
183 l i c F Toggle display of [l]abels, [i]ndex, [c]ontext, [F]ile borders.
184 t Change maximum toc depth (e.g. `3 t' hides levels greater than 3).
185 f / g Toggle follow mode / Refresh *toc* buffer.
186 a / d Toggle auto recenter / Toggle dedicated frame
187 r / C-u r Reparse the LaTeX document / Reparse entire LaTeX document.
188 . In other window, show position from where `reftex-toc' was called.
189 M-% Global search and replace to rename label at point.
190 x Switch to TOC of external document (with LaTeX package `xr').
191 z Jump to a specific section (e.g. '3 z' goes to section 3).")
193 (defun reftex-toc (&optional rebuild reuse
)
194 "Show the table of contents for the current document.
195 When called with a raw C-u prefix, rescan the document first."
197 ;; The REUSE argument means, search all visible frames for a window
198 ;; displaying the toc window. If yes, reuse this window.
202 (if (or (not (string= reftex-last-toc-master
(reftex-TeX-master-file)))
204 (reftex-erase-buffer "*toc*"))
206 (setq reftex-last-toc-file
(buffer-file-name))
207 (setq reftex-last-toc-master
(reftex-TeX-master-file))
209 (set-marker reftex-toc-return-marker
(point))
211 ;; If follow mode is active, arrange to delay it one command
212 (if reftex-toc-follow-mode
213 (setq reftex-toc-follow-mode
1))
215 (and reftex-toc-include-index-entries
216 (reftex-ensure-index-support))
217 (or reftex-support-index
218 (setq reftex-toc-include-index-entries nil
))
220 ;; Ensure access to scanning info and rescan buffer if prefix are is '(4)
221 (reftex-access-scan-info current-prefix-arg
)
223 (let* ((this-buf (current-buffer))
224 (docstruct-symbol reftex-docstruct-symbol
)
225 (xr-data (assq 'xr
(symbol-value reftex-docstruct-symbol
)))
226 (xr-alist (cons (cons "" (buffer-file-name)) (nth 1 xr-data
)))
227 (here-I-am (if (boundp 'reftex-rebuilding-toc
)
228 (get 'reftex-toc
:reftex-data
)
229 (car (reftex-where-am-I))))
230 (unsplittable (if (fboundp 'frame-property
)
231 (frame-property (selected-frame) 'unsplittable
)
232 (frame-parameter nil
'unsplittable
)))
235 (if (setq toc-window
(get-buffer-window
237 (if reuse
'visible
)))
238 (select-window toc-window
)
239 (when (or (not reftex-toc-keep-other-windows
)
240 (< (window-height) (* 2 window-min-height
)))
241 (delete-other-windows))
243 (setq reftex-last-window-width
(window-width)
244 reftex-last-window-height
(window-height)) ; remember
247 (if reftex-toc-split-windows-horizontally
249 (floor (* (window-width)
250 reftex-toc-split-windows-fraction
)))
252 (floor (* (window-height)
253 reftex-toc-split-windows-fraction
)))))
255 (switch-to-buffer "*toc*"))
257 (or (eq major-mode
'reftex-toc-mode
) (reftex-toc-mode))
258 (set (make-local-variable 'reftex-docstruct-symbol
) docstruct-symbol
)
259 (setq reftex-toc-include-labels-indicator
260 (if (eq reftex-toc-include-labels t
)
262 reftex-toc-include-labels
))
263 (setq reftex-toc-include-index-indicator
264 (if (eq reftex-toc-include-index-entries t
)
266 reftex-toc-include-index-entries
))
270 ;; buffer is empty - fill it with the table of contents
271 (message "Building *toc* buffer...")
273 (setq buffer-read-only nil
)
275 "TABLE-OF-CONTENTS on %s
276 SPC=view TAB=goto RET=goto+hide [q]uit [r]escan [l]abels [f]ollow [x]r [?]Help
277 ------------------------------------------------------------------------------
278 " (abbreviate-file-name reftex-last-toc-master
)))
280 (if (reftex-use-fonts)
281 (put-text-property (point-min) (point) 'face reftex-toc-header-face
))
282 (put-text-property (point-min) (point) 'intangible t
)
283 (put-text-property (point-min) (1+ (point-min)) 'xr-alist xr-alist
)
286 (reftex-insert-docstruct
289 reftex-toc-include-labels
290 reftex-toc-include-index-entries
291 reftex-toc-include-file-boundaries
292 reftex-toc-include-context
300 (run-hooks 'reftex-display-copied-context-hook
)
301 (message "Building *toc* buffer...done.")
302 (setq buffer-read-only t
))
304 ;; Only compute the offset
306 (or (reftex-get-offset this-buf here-I-am
307 (if reftex-toc-include-labels
" " nil
)
309 reftex-toc-include-index-entries
310 reftex-toc-include-file-boundaries
)
311 (reftex-last-assoc-before-elt
313 (symbol-value reftex-docstruct-symbol
))))
314 (put 'reftex-toc
:reftex-line
3)
315 (goto-char (point-min))
318 ;; Find the correct starting point
319 (reftex-find-start-point (point) offset
(get 'reftex-toc
:reftex-line
))
320 (setq reftex-last-follow-point
(point))))
322 (defun reftex-toc-recenter (&optional arg
)
323 "Display the TOC window and highlight line corresponding to current position."
325 (let ((buf (current-buffer))
326 (frame (selected-frame)))
328 (if (= (count-lines 1 (point)) 2)
329 (let ((current-prefix-arg nil
))
330 (select-window (get-buffer-window buf frame
))
333 (not (get-text-property (point) 'intangible
))
334 (memq reftex-highlight-selection
'(cursor both
))
336 (or (previous-single-property-change
337 (min (point-max) (1+ (point))) :data
)
339 (or (next-single-property-change (point) :data
)
341 (select-window (get-buffer-window buf frame
))))
343 (defun reftex-toc-pre-command-hook ()
344 ;; used as pre command hook in *toc* buffer
345 (reftex-unhighlight 0)
348 (defun reftex-toc-post-command-hook ()
349 ;; used in the post-command-hook for the *toc* buffer
350 (when (get-text-property (point) :data
)
351 (put 'reftex-toc
:reftex-data
(get-text-property (point) :data
))
353 (not (get-text-property (point) 'intangible
))
354 (memq reftex-highlight-selection
'(cursor both
))
356 (or (previous-single-property-change (1+ (point)) :data
)
358 (or (next-single-property-change (point) :data
)
360 (if (integerp reftex-toc-follow-mode
)
361 ;; remove delayed action
362 (setq reftex-toc-follow-mode t
)
363 (and (not (reftex-toc-dframe-p))
364 reftex-toc-follow-mode
365 (not (equal reftex-last-follow-point
(point)))
366 ;; show context in other window
367 (setq reftex-last-follow-point
(point))
369 (reftex-toc-visit-location nil
(not reftex-revisit-to-follow
))
372 (defun reftex-re-enlarge ()
373 "Enlarge window to a remembered size."
374 (let ((count (if reftex-toc-split-windows-horizontally
375 (- (or reftex-last-window-width
(window-width))
377 (- (or reftex-last-window-height
(window-height))
380 (enlarge-window count reftex-toc-split-windows-horizontally
))))
382 (defun reftex-toc-dframe-p (&optional frame error
)
383 ;; Check if FRAME is the dedicated TOC frame.
384 ;; If yes, and ERROR is non-nil, throw an error.
385 (setq frame
(or frame
(selected-frame)))
387 (if (fboundp 'frame-property
)
388 (frame-property frame
'name
)
389 (frame-parameter frame
'name
))
390 "RefTeX TOC Frame")))
392 (error "This frame is view-only. Use `C-c =' to create TOC window for commands"))
395 (defun reftex-toc-show-help ()
396 "Show a summary of special key bindings."
398 (reftex-toc-dframe-p nil
'error
)
399 (with-output-to-temp-buffer "*RefTeX Help*"
400 (princ reftex-toc-help
))
401 (reftex-enlarge-to-fit "*RefTeX Help*" t
)
402 ;; If follow mode is active, arrange to delay it one command
403 (if reftex-toc-follow-mode
404 (setq reftex-toc-follow-mode
1)))
406 (defun reftex-toc-next (&optional arg
)
407 "Move to next selectable item."
409 (when (featurep 'xemacs
) (setq zmacs-region-stays t
))
410 (setq reftex-callback-fwd t
)
411 (or (eobp) (forward-char 1))
412 (goto-char (or (next-single-property-change (point) :data
)
414 (defun reftex-toc-previous (&optional arg
)
415 "Move to previous selectable item."
417 (when (featurep 'xemacs
) (setq zmacs-region-stays t
))
418 (setq reftex-callback-fwd nil
)
419 (goto-char (or (previous-single-property-change (point) :data
)
421 (defun reftex-toc-next-heading (&optional arg
)
422 "Move to next table of contents line."
424 (when (featurep 'xemacs
) (setq zmacs-region-stays t
))
426 (re-search-forward "^ " nil t arg
)
428 (defun reftex-toc-previous-heading (&optional arg
)
429 "Move to previous table of contents line."
431 (when (featurep 'xemacs
) (setq zmacs-region-stays t
))
432 (re-search-backward "^ " nil t arg
))
433 (defun reftex-toc-toggle-follow ()
434 "Toggle follow (other window follows with context)."
436 (setq reftex-last-follow-point -
1)
437 (setq reftex-toc-follow-mode
(not reftex-toc-follow-mode
)))
438 (defun reftex-toc-toggle-file-boundary ()
439 "Toggle inclusion of file boundaries in *toc* buffer."
441 (setq reftex-toc-include-file-boundaries
442 (not reftex-toc-include-file-boundaries
))
444 (defun reftex-toc-toggle-labels (arg)
445 "Toggle inclusion of labels in *toc* buffer.
446 With prefix ARG, prompt for a label type and include only labels of
449 (setq reftex-toc-include-labels
450 (if arg
(reftex-query-label-type)
451 (not reftex-toc-include-labels
)))
453 (defun reftex-toc-toggle-index (arg)
454 "Toggle inclusion of index in *toc* buffer.
455 With prefix arg, prompt for an index tag and include only entries of that
458 (setq reftex-toc-include-index-entries
459 (if arg
(reftex-index-select-tag)
460 (not reftex-toc-include-index-entries
)))
462 (defun reftex-toc-toggle-context ()
463 "Toggle inclusion of label context in *toc* buffer.
464 Label context is only displayed when the labels are there as well."
466 (setq reftex-toc-include-context
(not reftex-toc-include-context
))
468 (defun reftex-toc-max-level (arg)
469 "Set the maximum level of TOC lines in this buffer to value of prefix ARG.
470 When no prefix is given, set the max level to a large number, so that all
471 levels are shown. For example, to set the level to 3, type `3 m'."
473 (setq reftex-toc-max-level
(if arg
474 (prefix-numeric-value arg
)
476 (setq reftex-toc-max-level-indicator
477 (if arg
(int-to-string reftex-toc-max-level
) "ALL"))
479 (defun reftex-toc-view-line ()
480 "View document location in other window."
482 (reftex-toc-dframe-p nil
'error
)
483 (reftex-toc-visit-location))
484 (defun reftex-toc-goto-line-and-hide ()
485 "Go to document location in other window. Hide the TOC window."
487 (reftex-toc-dframe-p nil
'error
)
488 (reftex-toc-visit-location 'hide
))
489 (defun reftex-toc-goto-line ()
490 "Go to document location in other window. TOC window stays."
492 (reftex-toc-dframe-p nil
'error
)
493 (reftex-toc-visit-location t
))
494 (defun reftex-toc-mouse-goto-line-and-hide (ev)
495 "Go to document location in other window. Hide the TOC window."
498 (reftex-toc-dframe-p nil
'error
)
499 (reftex-toc-visit-location 'hide
))
500 (defun reftex-toc-show-calling-point ()
501 "Show point where `reftex-toc' was called from."
503 (reftex-toc-dframe-p nil
'error
)
504 (let ((this-window (selected-window)))
507 (switch-to-buffer-other-window
508 (marker-buffer reftex-toc-return-marker
))
509 (goto-char (marker-position reftex-toc-return-marker
))
511 (select-window this-window
))))
512 (defun reftex-toc-quit ()
513 "Hide the TOC window and do not move point.
514 If the TOC window is the only window on the dedicated TOC frame, the frame
517 (if (and (one-window-p)
518 (reftex-toc-dframe-p)
519 (> (length (frame-list)) 1))
521 (or (one-window-p) (delete-window))
522 (switch-to-buffer (marker-buffer reftex-toc-return-marker
))
524 (goto-char (or (marker-position reftex-toc-return-marker
) (point)))))
525 (defun reftex-toc-quit-and-kill ()
526 "Kill the *toc* buffer."
528 (kill-buffer "*toc*")
529 (or (one-window-p) (delete-window))
530 (switch-to-buffer (marker-buffer reftex-toc-return-marker
))
532 (goto-char (marker-position reftex-toc-return-marker
)))
533 (defun reftex-toc-display-index (&optional arg
)
534 "Display the index buffer for the current document.
535 This works just like `reftex-display-index' from a LaTeX buffer.
536 With prefix arg 1, restrict index to the section at point."
538 (reftex-toc-dframe-p nil
'error
)
539 (let ((data (get-text-property (point) :data
))
540 (docstruct (symbol-value reftex-docstruct-symbol
))
543 (setq bor
(reftex-last-assoc-before-elt 'toc data docstruct
)
544 eor
(assoc 'toc
(cdr (memq bor docstruct
)))
545 restr
(list (nth 6 bor
) bor eor
)))
546 (reftex-toc-goto-line)
547 (reftex-display-index (if restr nil arg
) restr
)))
549 ;; Rescanning the document and rebuilding the TOC buffer.
550 (defun reftex-toc-rescan (&rest ignore
)
551 "Regenerate the *toc* buffer by reparsing file of section at point."
553 (if (and reftex-enable-partial-scans
554 (null current-prefix-arg
))
555 (let* ((data (get-text-property (point) :data
))
557 (file (cond ((eq what
'toc
) (nth 3 data
))
558 ((memq what
'(eof bof file-error
)) (nth 1 data
))
559 ((stringp what
) (nth 3 data
))
560 ((eq what
'index
) (nth 3 data
))))
561 (line (+ (count-lines (point-min) (point)) (if (bolp) 1 0))))
563 (error "Don't know which file to rescan. Try `C-u r'")
564 (put 'reftex-toc
:reftex-line line
)
565 (switch-to-buffer-other-window
566 (reftex-get-file-buffer-force file
))
567 (setq current-prefix-arg
'(4))
568 (let ((reftex-rebuilding-toc t
))
571 (reftex-kill-temporary-buffers))
573 (defun reftex-toc-Rescan (&rest ignore
)
574 "Regenerate the *toc* buffer by reparsing the entire document."
576 (let* ((line (+ (count-lines (point-min) (point)) (if (bolp) 1 0))))
577 (put 'reftex-toc
:reftex-line line
))
578 (switch-to-buffer-other-window
579 (reftex-get-file-buffer-force reftex-last-toc-file
))
580 (setq current-prefix-arg
'(16))
581 (let ((reftex-rebuilding-toc t
))
584 (defun reftex-toc-revert (&rest ignore
)
585 "Regenerate the TOC from the internal lists."
588 (if (fboundp 'frame-property
)
589 (frame-property (selected-frame) 'unsplittable
)
590 (frame-parameter nil
'unsplittable
)))
591 (reftex-rebuilding-toc t
))
594 (reftex-get-file-buffer-force reftex-last-toc-file
))
595 (switch-to-buffer-other-window
596 (reftex-get-file-buffer-force reftex-last-toc-file
))))
597 (reftex-erase-buffer "*toc*")
598 (setq current-prefix-arg nil
)
601 (defun reftex-toc-external (&rest ignore
)
602 "Switch to table of contents of an external document."
604 (reftex-toc-dframe-p nil
'error
)
605 (let* ((old-buf (current-buffer))
606 (xr-alist (get-text-property 1 'xr-alist
))
607 (xr-index (reftex-select-external-document
609 (switch-to-buffer-other-window (or (reftex-get-file-buffer-force
610 (cdr (nth xr-index xr-alist
)))
611 (error "Cannot switch document")))
613 (if (equal old-buf
(current-buffer))
615 (message "Switched document"))))
617 (defun reftex-toc-jump (arg)
618 "Jump to a specific section. E.g. '3 z' jumps to section 3.
619 Useful for large TOCs."
621 (goto-char (point-min))
623 (concat "^ *" (number-to-string (if (numberp arg
) arg
1)) " ")
627 ;; Promotion/Demotion stuff
634 (defun reftex-toc-demote (&optional arg
)
635 "Demote section at point. If region is active, apply to all in region."
637 (reftex-toc-do-promote 1))
638 (defun reftex-toc-promote (&optional arg
)
639 "Promote section at point. If region is active, apply to all in region."
641 (reftex-toc-do-promote -
1))
642 (defun reftex-toc-do-promote (delta)
643 "Workhorse for `reftex-toc-promote' and `reftex-toc-demote'.
644 Changes the level of sections in the current region, or just the section at
646 ;; We should not do anything unless we are sure this is going to work for
647 ;; each section in the region. Therefore we first collect information and
649 (let* ((start-line (+ (count-lines (point-min) (point))
651 (mark-line (if (reftex-region-active-p)
652 (save-excursion (goto-char (mark))
653 (+ (count-lines (point-min) (point))
656 (pro-or-de (if (> delta
0) "de" "pro"))
657 beg end entries data sections nsec msg
)
660 (if (reftex-region-active-p)
661 ;; A region is dangerous, check if we have a brand new scan,
662 ;; to make sure we are not missing any section statements.
663 (if (not (reftex-toc-check-docstruct))
664 (reftex-toc-load-all-files-for-promotion) ;; exits
665 (setq beg
(min (point) (mark))
666 end
(max (point) (mark))))
667 (setq beg
(point) end
(point)))
669 (while (and (setq data
(get-text-property (point) :data
))
671 (if (eq (car data
) 'toc
) (push (cons data
(point)) entries
))
672 (goto-char (or (next-single-property-change (point) :data
)
674 (setq entries
(nreverse entries
))
675 ;; Get the relevant section numbers, for an informative message
676 (goto-char start-pos
)
677 (setq sections
(reftex-toc-extract-section-number (car entries
)))
678 (if (> (setq nsec
(length entries
)) 1)
681 (reftex-toc-extract-section-number
682 (nth (1- nsec
) entries
)))))
683 ;; Run through the list and prepare the changes.
684 (setq entries
(mapcar
685 (lambda (e) (reftex-toc-promote-prepare e delta
))
687 ;; Ask for permission
688 (if (or (not reftex-toc-confirm-promotion
) ; never confirm
689 (and (integerp reftex-toc-confirm-promotion
) ; confirm if many
690 (< nsec reftex-toc-confirm-promotion
))
692 (format "%s %d toc-entr%s (section%s %s)? "
693 (if (< delta
0) "Promote" "Demote")
695 (if (= 1 nsec
) "y" "ies")
696 (if (= 1 nsec
) "" "s")
698 nil
; we have permission, do nothing
699 (error "Abort")) ; abort, we don't have permission
701 (mapc 'reftex-toc-promote-action entries
)
702 ;; Rescan the document and rebuilt the toc buffer
703 (save-window-excursion
705 (reftex-toc-restore-region start-line mark-line
)
706 (message "%d section%s %smoted"
707 nsec
(if (= 1 nsec
) "" "s") pro-or-de
)
709 (if msg
(progn (ding) (message "%s" msg
)))))
712 (defun reftex-toc-restore-region (point-line &optional mark-line
)
715 (goto-char (point-min))
716 (forward-line (1- mark-line
))
719 (goto-char (point-min))
720 (forward-line (1- point-line
)))
723 (if (featurep 'xemacs
)
724 (zmacs-activate-region)
726 deactivate-mark nil
)))))
728 (defun reftex-toc-promote-prepare (x delta
)
729 "Look at a TOC entry and see if we could pro/demote it.
730 This function prepares everything for the change, but does not do it.
731 The return value is a list with information needed when doing the
732 promotion/demotion later. DELTA is the level change."
733 (let* ((data (car x
))
735 (marker (nth 4 data
))
736 (literal (nth 7 data
))
739 ;; Here follows some paranoid code to make very sure we are not
740 ;; going to break anything
742 (if (and (markerp marker
) (marker-buffer marker
))
743 ;; Buffer is still live and we have the marker.
745 (with-current-buffer (marker-buffer marker
)
746 ;; Goto the buffer and check of section is unchanged
747 (goto-char (marker-position marker
))
748 (if (looking-at (regexp-quote literal
))
749 ;; OK, get the makro name
751 (beginning-of-line 1)
752 (if (looking-at reftex-section-regexp
)
753 (setq name
(reftex-match-string 2))
754 (error "Something is wrong! Contact maintainer!")))
755 ;; Section has changed, request scan and loading
756 ;; We use a variable to delay until after the safe-exc.
757 ;; because otherwise we lose the region.
759 ;; Scan document and load all files, this exits command
760 (if load
(reftex-toc-load-all-files-for-promotion))) ; exits
761 ;; We don't have a live marker: scan and load files.
762 (reftex-toc-load-all-files-for-promotion)))
763 (level (cdr (assoc name reftex-section-levels-all
)))
764 (dummy (if (not (integerp level
))
766 (goto-char toc-point
)
767 (error "Cannot %smote special sections" pro-or-de
))))
768 (newlevel (if (>= level
0) (+ delta level
) (- level delta
)))
769 (dummy2 (if (or (and (>= level
0) (= newlevel -
1))
770 (and (< level
0) (= newlevel
0)))
771 (error "Cannot %smote \\%s" pro-or-de name
)))
772 (newname (reftex-toc-newhead-from-alist newlevel name
773 reftex-section-levels-all
)))
774 (if (and name newname
)
775 (list data name newname toc-point
)
776 (goto-char toc-point
)
777 (error "Cannot %smote \\%s" pro-or-de name
))))
779 (defun reftex-toc-promote-action (x)
780 "Change the level of a TOC entry.
781 PRO-OR-DE is assumed to be dynamically scoped into this function."
782 (let* ((data (car x
))
785 (marker (nth 4 data
)))
786 (with-current-buffer (marker-buffer marker
)
787 (goto-char (marker-position marker
))
788 (if (looking-at (concat "\\([ \t]*" reftex-section-pre-regexp
"\\)" (regexp-quote name
)))
789 (replace-match (concat "\\1" newname
))
790 (error "Fatal error during %smotion" pro-or-de
)))))
792 (defun reftex-toc-extract-section-number (entry)
793 "Get the numbering of a TOC entry, for message purposes."
794 (if (string-match "\\s-*\\(\\S-+\\)" (nth 2 (car entry
)))
795 (match-string 1 (nth 2 (car entry
)))
798 (defun reftex-toc-newhead-from-alist (nlevel head alist
)
799 "Get new heading with level NLEVEL from ALIST.
800 If there are no such entries, return nil.
801 If there are several different entries with same new level, choose the
802 one with the smallest distance to the association of HEAD in the alist.
803 This makes it possible for promotion to work several sets of headings,
804 if these sets are sorted blocks in the alist."
806 (ass (assoc head al
))
807 (pos (length (memq ass al
)))
808 dist
(mindist 1000) newhead
)
810 (if (equal (cdar al
) nlevel
)
812 (setq dist
(abs (- (length al
) pos
)))
814 (setq newhead
(car (car al
))
819 (defun reftex-toc-check-docstruct ()
820 "Check if the current docstruct is fully up to date and all files visited."
821 ;; We do this by checking if all sections are on the right position
822 (let ((docstruct (symbol-value reftex-docstruct-symbol
))
823 entry marker empoint
)
825 (while (setq entry
(pop docstruct
))
826 (if (eq (car entry
) 'toc
)
828 (setq marker
(nth 4 entry
)
829 empoint
(nth 8 entry
))
830 (if (not (and (markerp marker
)
831 (marker-buffer marker
)
832 (= (marker-position marker
) empoint
)))
833 (throw 'exit nil
)))))
836 (defun reftex-toc-load-all-files-for-promotion ()
837 "Make sure all files of the document are being visited by buffers,
838 and that the scanning info is absolutely up to date.
839 We do this by rescanning with `reftex-keep-temporary-buffers' bound to t.
840 The variable PRO-OR-DE is assumed to be dynamically scoped into this function.
841 When finished, we exit with an error message."
842 (let ((reftex-keep-temporary-buffers t
))
844 (reftex-toc-restore-region start-line mark-line
)
846 "TOC had to be updated first. Please check selection and repeat the command.")))
848 (defun reftex-toc-rename-label ()
849 "Rename the currently selected label in the *toc* buffer.
850 This launches a global search and replace in order to rename a label.
851 Renaming a label is hardly ever necessary - the only exception is after
852 promotion/demotion in connection with a package like fancyref, where the
853 label prefix determines the wording of a reference."
855 (let* ((toc (get-text-property (point) :data
))
856 (label (car toc
)) newlabel
)
857 (if (not (stringp label
))
858 (error "This is not a label entry"))
859 (setq newlabel
(read-string (format "Rename label \"%s\" to:" label
)))
860 (if (assoc newlabel
(symbol-value reftex-docstruct-symbol
))
862 (format "Label '%s' exists. Use anyway? " label
)))
865 (save-window-excursion
866 (reftex-toc-visit-location t
)
868 (reftex-query-replace-document
869 (concat "{" (regexp-quote label
) "}")
870 (format "{%s}" newlabel
))
872 (reftex-toc-rescan)))
875 (defun reftex-toc-visit-location (&optional final no-revisit
)
876 ;; Visit the tex file corresponding to the TOC entry on the current line.
877 ;; If FINAL is t, stay there
878 ;; If FINAL is 'hide, hide the TOC window.
879 ;; Otherwise, move cursor back into TOC window.
880 ;; NO-REVISIT means don't visit files, just use live buffers.
881 ;; This function is pretty clever about finding back a section heading,
882 ;; even if the buffer is not live, or things like outline, x-symbol etc.
885 (let* ((toc (get-text-property (point) :data
))
886 (toc-window (selected-window))
887 show-window show-buffer match
)
889 (unless toc
(error "Don't know which TOC line to visit"))
895 (setq match
(reftex-toc-find-section toc no-revisit
)))
897 ((eq (car toc
) 'index
)
899 (setq match
(reftex-index-show-entry toc no-revisit
)))
901 ((memq (car toc
) '(bof eof
))
904 (let ((where (car toc
))
906 (if (or (not no-revisit
) (reftex-get-buffer-visiting file
))
908 (switch-to-buffer-other-window
909 (reftex-get-file-buffer-force file nil
))
910 (goto-char (if (eq where
'bof
) (point-min) (point-max))))
911 (message "%s" reftex-no-follow-message
) nil
))))
915 (setq match
(reftex-show-label-location toc reftex-callback-fwd
918 (setq show-window
(selected-window)
919 show-buffer
(current-buffer))
922 (select-window toc-window
)
923 (error "Cannot find location"))
925 (select-window toc-window
)
927 ;; use the `final' parameter to decide what to do next
930 (reftex-unhighlight 0)
931 (select-window show-window
))
933 (reftex-unhighlight 0)
934 (or (one-window-p) (delete-window))
935 ;; If `show-window' is still live, show-buffer is already visible
936 ;; so let's not make it visible in yet-another-window.
937 (if (window-live-p show-window
)
938 (set-buffer show-buffer
)
939 (switch-to-buffer show-buffer
))
943 (defun reftex-toc-find-section (toc &optional no-revisit
)
944 (let* ((file (nth 3 toc
))
947 (literal (nth 7 toc
))
948 (emergency-point (nth 8 toc
))
951 ((and (markerp marker
) (marker-buffer marker
))
952 ;; Buffer is still live and we have the marker. Should be easy.
953 (switch-to-buffer-other-window (marker-buffer marker
))
955 (goto-char (marker-position marker
))
956 (or (looking-at (regexp-quote literal
))
957 (looking-at (reftex-make-regexp-allow-for-ctrl-m literal
))
958 (looking-at (reftex-make-desperate-section-regexp literal
))
959 (looking-at (concat "\\\\"
963 reftex-section-levels-all
)))
965 ((or (not no-revisit
)
966 (reftex-get-buffer-visiting file
))
967 ;; Marker is lost. Use the backup method.
968 (switch-to-buffer-other-window
969 (reftex-get-file-buffer-force file nil
))
970 (goto-char (or emergency-point
(point-min)))
971 (or (looking-at (regexp-quote literal
))
972 (let ((len (length literal
)))
973 (or (reftex-nearest-match (regexp-quote literal
) len
)
974 (reftex-nearest-match
975 (reftex-make-regexp-allow-for-ctrl-m literal
) len
)
976 (reftex-nearest-match
977 (reftex-make-desperate-section-regexp literal
) len
)))))
978 (t (message "%s" reftex-no-follow-message
) nil
))))
980 (goto-char (match-beginning 0))
981 (if (not (= (point) (point-max))) (recenter 1))
982 (reftex-highlight 0 (match-beginning 0) (match-end 0) (current-buffer)))
985 (defun reftex-make-desperate-section-regexp (old)
986 ;; Return a regexp which will still match a section statement even if
987 ;; x-symbol or isotex or the like have been at work in the mean time.
988 (let* ((n (1+ (string-match "[[{]" old
)))
989 (new (regexp-quote (substring old
0 (1+ (string-match "[[{]" old
)))))
990 (old (substring old n
)))
992 "\\([\r\n]\\)\\|\\(\\`\\|[ \t\n\r]\\)\\([a-zA-Z0-9]+\\)\\([ \t\n\r]\\|}\\'\\)"
994 (if (match-beginning 1)
995 (setq new
(concat new
"[^\n\r]*[\n\r]"))
996 (setq new
(concat new
"[^\n\r]*" (match-string 3 old
))))
997 (setq old
(substring old
(match-end 0))))
1000 ;; Auto recentering of TOC window
1002 (defun reftex-recenter-toc-when-idle ()
1003 (and (> (buffer-size) 5)
1005 (not (active-minibuffer-window))
1006 (fboundp 'reftex-toc-mode
)
1007 (get-buffer-window "*toc*" 'visible
)
1008 (string= reftex-last-toc-master
(reftex-TeX-master-file))
1009 (let (current-prefix-arg)
1010 (reftex-toc-recenter))))
1012 (defun reftex-toggle-auto-toc-recenter ()
1013 "Toggle the automatic recentering of the TOC window.
1014 When active, leaving point idle will make the TOC window jump to the correct
1017 (if reftex-toc-auto-recenter-timer
1019 (if (featurep 'xemacs
)
1020 (delete-itimer reftex-toc-auto-recenter-timer
)
1021 (cancel-timer reftex-toc-auto-recenter-timer
))
1022 (setq reftex-toc-auto-recenter-timer nil
)
1023 (message "Automatic recentering of TOC window was turned off"))
1024 (setq reftex-toc-auto-recenter-timer
1025 (if (featurep 'xemacs
)
1026 (start-itimer "RefTeX Idle Timer for recenter"
1027 'reftex-recenter-toc-when-idle
1028 reftex-idle-time reftex-idle-time t
)
1029 (run-with-idle-timer
1030 reftex-idle-time t
'reftex-recenter-toc-when-idle
)))
1031 (message "Automatic recentering of TOC window was turned on")))
1033 (defun reftex-toc-toggle-dedicated-frame ()
1034 "Toggle the display of a separate frame for the TOC.
1035 This frame is not used by the `reftex-toc' commands, but it is useful to
1036 always show the current section in connection with the option
1037 `reftex-auto-recenter-toc'."
1040 (let* ((frames (frame-list)) frame
1041 (get-frame-prop-func (if (fboundp 'frame-property
)
1044 (while (setq frame
(pop frames
))
1045 (if (equal (funcall get-frame-prop-func frame
'name
)
1048 (delete-frame frame
)
1049 (throw 'exit nil
))))
1050 (reftex-make-separate-toc-frame))))
1052 (defun reftex-make-separate-toc-frame ()
1053 ;; Create a new fame showing only the TOC buffer.
1054 (let ((current-frame (selected-frame))
1055 (current-window (selected-window))
1056 (current-toc-window (get-buffer-window "*toc*" 'visible
))
1058 (if (and current-toc-window
1059 (not (equal (selected-frame) (window-frame current-toc-window
))))
1061 (setq current-toc-frame
1063 '((name .
"RefTeX TOC Frame")
1064 (height .
20) (width .
60)
1067 (default-toolbar-visible-p . nil
)
1068 (menubar-visible-p . nil
)
1069 (horizontal-scrollbar-visible-p . nil
))))
1070 (select-frame current-toc-frame
)
1071 (switch-to-buffer "*toc*")
1072 (select-frame current-frame
)
1073 (cond ((fboundp 'x-focus-frame
)
1074 (x-focus-frame current-frame
))
1075 ((and (featurep 'xemacs
) ; `focus-frame' is a nop in Emacs.
1076 (fboundp 'focus-frame
))
1077 (focus-frame current-frame
)))
1078 (select-window current-window
)
1079 (when (eq reftex-auto-recenter-toc
'frame
)
1080 (unless reftex-toc-auto-recenter-timer
1081 (reftex-toggle-auto-toc-recenter))
1082 (add-hook 'delete-frame-hook
'reftex-toc-delete-frame-hook
)))))
1084 (defun reftex-toc-delete-frame-hook (frame)
1085 (if (and reftex-toc-auto-recenter-timer
1086 (reftex-toc-dframe-p frame
))
1088 (reftex-toggle-auto-toc-recenter))))
1090 ;;; reftex-toc.el ends here