Merge branch 'master' into comment-cache
[emacs.git] / lisp / textmodes / reftex-toc.el
blobab49ae8e320411e9bc0be28ddd4f6ec7f570b939
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 <http://www.gnu.org/licenses/>.
23 ;;; Commentary:
25 ;;; Code:
27 (provide 'reftex-toc)
28 (require 'reftex)
29 ;;;
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)
73 (easy-menu-define
74 reftex-toc-menu map
75 "Menu for Table of Contents buffer"
76 '("TOC"
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]
82 "--"
83 ("Edit"
84 ["Promote" reftex-toc-promote t]
85 ["Demote" reftex-toc-demote t]
86 ["Rename Label" reftex-toc-rename-label t])
87 "--"
88 ["Index" reftex-toc-display-index t]
89 ["External Document TOC " reftex-toc-external t]
90 "--"
91 ("Update"
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])
95 ("Options"
96 "TOC Items"
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]
105 "--"
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])
111 "--"
112 ["Help" reftex-toc-show-help t]))
114 map)
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)
139 "ALL"
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 ">"
147 " -%-"))
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)
187 ;;;###autoload
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.
196 (interactive)
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.
202 current-prefix-arg)
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 are 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)))
232 offset toc-window)
234 (if (setq toc-window (get-buffer-window
235 "*toc*"
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
245 (unless unsplittable
246 (if reftex-toc-split-windows-horizontally
247 (split-window-right
248 (floor (* (window-total-width)
249 reftex-toc-split-windows-fraction)))
250 (split-window-below
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)
260 "ALL"
261 reftex-toc-include-labels))
262 (setq reftex-toc-include-index-indicator
263 (if (eq reftex-toc-include-index-entries t)
264 "ALL"
265 reftex-toc-include-index-entries))
267 (cond
268 ((= (buffer-size) 0)
269 ;; buffer is empty - fill it with the table of contents
270 (message "Building *toc* buffer...")
272 (setq buffer-read-only nil)
273 (insert (format
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)
292 (setq offset
293 (reftex-insert-docstruct
294 this-buf
295 t ; include TOC
296 reftex-toc-include-labels
297 reftex-toc-include-index-entries
298 reftex-toc-include-file-boundaries
299 reftex-toc-include-context
300 nil ; counter
301 nil ; commented
302 here-I-am
303 "" ; xr-prefix
304 t ; a TOC buffer
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
312 (setq 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
319 'toc here-I-am
320 (symbol-value reftex-docstruct-symbol))))
321 (put 'reftex-toc :reftex-line 3)
322 (goto-char (point-min))
323 (forward-line 2)))
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))))
329 ;;;###autoload
330 (defun reftex-toc-recenter (&optional arg)
331 "Display the TOC window and highlight line corresponding to current position."
332 (interactive "P")
333 (let ((buf (current-buffer))
334 (frame (selected-frame)))
335 (reftex-toc arg t)
336 (if (= (count-lines 1 (point)) 2)
337 (let ((current-prefix-arg nil))
338 (select-window (get-buffer-window buf frame))
339 (reftex-toc nil t)))
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))
343 (reftex-highlight 2
344 (or (previous-single-property-change
345 (min (point-max) (1+ (point))) :data)
346 (point-min))
347 (or (next-single-property-change (point) :data)
348 (point-max))))
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))
364 (reftex-highlight 2
365 (or (previous-single-property-change (1+ (point)) :data)
366 (point-min))
367 (or (next-single-property-change (point) :data)
368 (point-max)))))
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))
377 (condition-case nil
378 (reftex-toc-visit-location nil (not reftex-revisit-to-follow))
379 (error t)))))
381 (defun reftex-re-enlarge ()
382 "Enlarge window to a remembered size."
383 (let ((count (if reftex-toc-split-windows-horizontally
384 (- (or reftex-last-window-width (window-total-width))
385 (window-total-width))
386 (- (or reftex-last-window-height (window-height))
387 (window-height)))))
388 (when (> count 0)
389 (enlarge-window count reftex-toc-split-windows-horizontally))))
391 (defun reftex-toc-dframe-p (&optional frame error)
392 ;; Check if FRAME is the dedicated TOC frame.
393 ;; If yes, and ERROR is non-nil, throw an error.
394 (setq frame (or frame (selected-frame)))
395 (let ((res (equal
396 (if (fboundp 'frame-property)
397 (frame-property frame 'name)
398 (frame-parameter frame 'name))
399 "RefTeX TOC Frame")))
400 (if (and res error)
401 (error "This frame is view-only. Use `C-c =' to create TOC window for commands"))
402 res))
404 (defun reftex-toc-show-help ()
405 "Show a summary of special key bindings."
406 (interactive)
407 (reftex-toc-dframe-p nil 'error)
408 (with-output-to-temp-buffer "*RefTeX Help*"
409 (princ reftex-toc-help))
410 (reftex-enlarge-to-fit "*RefTeX Help*" t)
411 ;; If follow mode is active, arrange to delay it one command
412 (if reftex-toc-follow-mode
413 (setq reftex-toc-follow-mode 1)))
415 (defun reftex-toc-next (&optional _arg)
416 "Move to next selectable item."
417 (interactive)
418 (when (featurep 'xemacs) (setq zmacs-region-stays t))
419 (setq reftex-callback-fwd t)
420 (or (eobp) (forward-char 1))
421 (goto-char (or (next-single-property-change (point) :data)
422 (point))))
423 (defun reftex-toc-previous (&optional _arg)
424 "Move to previous selectable item."
425 (interactive)
426 (when (featurep 'xemacs) (setq zmacs-region-stays t))
427 (setq reftex-callback-fwd nil)
428 (goto-char (or (previous-single-property-change (point) :data)
429 (point))))
430 (defun reftex-toc-next-heading (&optional arg)
431 "Move to next table of contents line."
432 (interactive "p")
433 (when (featurep 'xemacs) (setq zmacs-region-stays t))
434 (end-of-line)
435 (re-search-forward "^ " nil t arg)
436 (beginning-of-line))
437 (defun reftex-toc-previous-heading (&optional arg)
438 "Move to previous table of contents line."
439 (interactive "p")
440 (when (featurep 'xemacs) (setq zmacs-region-stays t))
441 (re-search-backward "^ " nil t arg))
442 (defun reftex-toc-toggle-follow ()
443 "Toggle follow (other window follows with context)."
444 (interactive)
445 (setq reftex-last-follow-point -1)
446 (setq reftex-toc-follow-mode (not reftex-toc-follow-mode)))
447 (defun reftex-toc-toggle-file-boundary ()
448 "Toggle inclusion of file boundaries in *toc* buffer."
449 (interactive)
450 (setq reftex-toc-include-file-boundaries
451 (not reftex-toc-include-file-boundaries))
452 (reftex-toc-revert))
453 (defun reftex-toc-toggle-labels (arg)
454 "Toggle inclusion of labels in *toc* buffer.
455 With prefix ARG, prompt for a label type and include only labels of
456 that specific type."
457 (interactive "P")
458 (setq reftex-toc-include-labels
459 (if arg (reftex-query-label-type)
460 (not reftex-toc-include-labels)))
461 (reftex-toc-revert))
462 (defun reftex-toc-toggle-index (arg)
463 "Toggle inclusion of index in *toc* buffer.
464 With prefix arg, prompt for an index tag and include only entries of that
465 specific index."
466 (interactive "P")
467 (setq reftex-toc-include-index-entries
468 (if arg (reftex-index-select-tag)
469 (not reftex-toc-include-index-entries)))
470 (reftex-toc-revert))
471 (defun reftex-toc-toggle-context ()
472 "Toggle inclusion of label context in *toc* buffer.
473 Label context is only displayed when the labels are there as well."
474 (interactive)
475 (setq reftex-toc-include-context (not reftex-toc-include-context))
476 (reftex-toc-revert))
477 (defun reftex-toc-max-level (arg)
478 "Set the maximum level of TOC lines in this buffer to value of prefix ARG.
479 When no prefix is given, set the max level to a large number, so that all
480 levels are shown. For example, to set the level to 3, type `3 m'."
481 (interactive "P")
482 (setq reftex-toc-max-level (if arg
483 (prefix-numeric-value arg)
484 100))
485 (setq reftex-toc-max-level-indicator
486 (if arg (int-to-string reftex-toc-max-level) "ALL"))
487 (reftex-toc-revert))
488 (defun reftex-toc-view-line ()
489 "View document location in other window."
490 (interactive)
491 (reftex-toc-dframe-p nil 'error)
492 (reftex-toc-visit-location))
493 (defun reftex-toc-goto-line-and-hide ()
494 "Go to document location in other window. Hide the TOC window."
495 (interactive)
496 (reftex-toc-dframe-p nil 'error)
497 (reftex-toc-visit-location 'hide))
498 (defun reftex-toc-goto-line ()
499 "Go to document location in other window. TOC window stays."
500 (interactive)
501 (reftex-toc-dframe-p nil 'error)
502 (reftex-toc-visit-location t))
503 (defun reftex-toc-mouse-goto-line-and-hide (ev)
504 "Go to document location in other window. Hide the TOC window."
505 (interactive "e")
506 (mouse-set-point ev)
507 (reftex-toc-dframe-p nil 'error)
508 (reftex-toc-visit-location 'hide))
509 (defun reftex-toc-show-calling-point ()
510 "Show point where `reftex-toc' was called from."
511 (interactive)
512 (reftex-toc-dframe-p nil 'error)
513 (let ((this-window (selected-window)))
514 (unwind-protect
515 (progn
516 (switch-to-buffer-other-window
517 (marker-buffer reftex-toc-return-marker))
518 (goto-char (marker-position reftex-toc-return-marker))
519 (recenter '(4)))
520 (select-window this-window))))
521 (defun reftex-toc-quit ()
522 "Hide the TOC window and do not move point.
523 If the TOC window is the only window on the dedicated TOC frame, the frame
524 is destroyed."
525 (interactive)
526 (if (and (one-window-p)
527 (reftex-toc-dframe-p)
528 (> (length (frame-list)) 1))
529 (delete-frame)
530 (or (one-window-p) (delete-window))
531 (switch-to-buffer (marker-buffer reftex-toc-return-marker))
532 (reftex-re-enlarge)
533 (goto-char (or (marker-position reftex-toc-return-marker) (point)))))
534 (defun reftex-toc-quit-and-kill ()
535 "Kill the *toc* buffer."
536 (interactive)
537 (kill-buffer "*toc*")
538 (or (one-window-p) (delete-window))
539 (switch-to-buffer (marker-buffer reftex-toc-return-marker))
540 (reftex-re-enlarge)
541 (goto-char (marker-position reftex-toc-return-marker)))
542 (defun reftex-toc-display-index (&optional arg)
543 "Display the index buffer for the current document.
544 This works just like `reftex-display-index' from a LaTeX buffer.
545 With prefix arg 1, restrict index to the section at point."
546 (interactive "P")
547 (reftex-toc-dframe-p nil 'error)
548 (let ((data (get-text-property (point) :data))
549 (docstruct (symbol-value reftex-docstruct-symbol))
550 bor eor restr)
551 (when (equal arg 2)
552 (setq bor (reftex-last-assoc-before-elt 'toc data docstruct)
553 eor (assoc 'toc (cdr (memq bor docstruct)))
554 restr (list (nth 6 bor) bor eor)))
555 (reftex-toc-goto-line)
556 (reftex-display-index (if restr nil arg) restr)))
558 ;; Rescanning the document and rebuilding the TOC buffer.
559 (defun reftex-toc-rescan (&rest _)
560 "Regenerate the *toc* buffer by reparsing file of section at point."
561 (interactive)
562 (if (and reftex-enable-partial-scans
563 (null current-prefix-arg))
564 (let* ((data (get-text-property (point) :data))
565 (what (car data))
566 (file (cond ((eq what 'toc) (nth 3 data))
567 ((memq what '(eof bof file-error)) (nth 1 data))
568 ((stringp what) (nth 3 data))
569 ((eq what 'index) (nth 3 data))))
570 (line (+ (count-lines (point-min) (point)) (if (bolp) 1 0))))
571 (if (not file)
572 (error "Don't know which file to rescan. Try `C-u r'")
573 (put 'reftex-toc :reftex-line line)
574 (switch-to-buffer-other-window
575 (reftex-get-file-buffer-force file))
576 (setq current-prefix-arg '(4))
577 (let ((reftex--rebuilding-toc t))
578 (reftex-toc))))
579 (reftex-toc-Rescan))
580 (reftex-kill-temporary-buffers))
582 (defun reftex-toc-Rescan (&rest _)
583 "Regenerate the *toc* buffer by reparsing the entire document."
584 (interactive)
585 (let* ((line (+ (count-lines (point-min) (point)) (if (bolp) 1 0))))
586 (put 'reftex-toc :reftex-line line))
587 (switch-to-buffer-other-window
588 (reftex-get-file-buffer-force reftex-last-toc-file))
589 (setq current-prefix-arg '(16))
590 (let ((reftex--rebuilding-toc t))
591 (reftex-toc)))
593 (defun reftex-toc-revert (&rest _)
594 "Regenerate the TOC from the internal lists."
595 (interactive)
596 (let ((unsplittable
597 (if (fboundp 'frame-property)
598 (frame-property (selected-frame) 'unsplittable)
599 (frame-parameter nil 'unsplittable)))
600 (reftex--rebuilding-toc t))
601 (if unsplittable
602 (switch-to-buffer
603 (reftex-get-file-buffer-force reftex-last-toc-file))
604 (switch-to-buffer-other-window
605 (reftex-get-file-buffer-force reftex-last-toc-file))))
606 (reftex-erase-buffer "*toc*")
607 (setq current-prefix-arg nil)
608 (reftex-toc t t))
610 (defun reftex-toc-external (&rest _)
611 "Switch to table of contents of an external document."
612 (interactive)
613 (reftex-toc-dframe-p nil 'error)
614 (let* ((old-buf (current-buffer))
615 (xr-alist (get-text-property 1 'xr-alist))
616 (xr-index (reftex-select-external-document
617 xr-alist 0)))
618 (switch-to-buffer-other-window (or (reftex-get-file-buffer-force
619 (cdr (nth xr-index xr-alist)))
620 (error "Cannot switch document")))
621 (reftex-toc)
622 (if (equal old-buf (current-buffer))
623 (message "")
624 (message "Switched document"))))
626 (defun reftex-toc-jump (arg)
627 "Jump to a specific section. E.g. '3 z' jumps to section 3.
628 Useful for large TOCs."
629 (interactive "P")
630 (goto-char (point-min))
631 (re-search-forward
632 (concat "^ *" (number-to-string (if (numberp arg) arg 1)) " ")
633 nil t)
634 (beginning-of-line))
636 ;; Promotion/Demotion stuff
638 (defvar reftex--pro-or-de)
639 (defvar reftex--start-line)
640 (defvar reftex--mark-line)
642 (defun reftex-toc-demote (&optional _arg)
643 "Demote section at point. If region is active, apply to all in region."
644 (interactive)
645 (reftex-toc-do-promote 1))
646 (defun reftex-toc-promote (&optional _arg)
647 "Promote section at point. If region is active, apply to all in region."
648 (interactive)
649 (reftex-toc-do-promote -1))
650 (defun reftex-toc-do-promote (delta)
651 "Workhorse for `reftex-toc-promote' and `reftex-toc-demote'.
652 Changes the level of sections in the current region, or just the section at
653 point."
654 ;; We should not do anything unless we are sure this is going to work for
655 ;; each section in the region. Therefore we first collect information and
656 ;; test.
657 (let* ((reftex--start-line (+ (count-lines (point-min) (point))
658 (if (bolp) 1 0)))
659 (reftex--mark-line
660 (if (reftex-region-active-p)
661 (save-excursion (goto-char (mark))
662 (+ (count-lines (point-min) (point))
663 (if (bolp) 1 0)))))
664 (start-pos (point))
665 (reftex--pro-or-de (if (> delta 0) "de" "pro"))
666 beg end entries data sections nsec msg)
667 (setq msg
668 (catch 'exit
669 (if (reftex-region-active-p)
670 ;; A region is dangerous, check if we have a brand new scan,
671 ;; to make sure we are not missing any section statements.
672 (if (not (reftex-toc-check-docstruct))
673 (reftex-toc-load-all-files-for-promotion) ;; exits
674 (setq beg (min (point) (mark))
675 end (max (point) (mark))))
676 (setq beg (point) end (point)))
677 (goto-char beg)
678 (while (and (setq data (get-text-property (point) :data))
679 (<= (point) end))
680 (if (eq (car data) 'toc) (push (cons data (point)) entries))
681 (goto-char (or (next-single-property-change (point) :data)
682 (point-max))))
683 (setq entries (nreverse entries))
684 ;; Get the relevant section numbers, for an informative message
685 (goto-char start-pos)
686 (setq sections (reftex-toc-extract-section-number (car entries)))
687 (if (> (setq nsec (length entries)) 1)
688 (setq sections
689 (concat sections "-"
690 (reftex-toc-extract-section-number
691 (nth (1- nsec) entries)))))
692 ;; Run through the list and prepare the changes.
693 (setq entries (mapcar
694 (lambda (e) (reftex-toc-promote-prepare e delta))
695 entries))
696 ;; Ask for permission
697 (if (or (not reftex-toc-confirm-promotion) ; never confirm
698 (and (integerp reftex-toc-confirm-promotion) ; confirm if many
699 (< nsec reftex-toc-confirm-promotion))
700 (yes-or-no-p ; ask
701 (format "%s %d toc-entr%s (section%s %s)? "
702 (if (< delta 0) "Promote" "Demote")
703 nsec
704 (if (= 1 nsec) "y" "ies")
705 (if (= 1 nsec) "" "s")
706 sections)))
707 nil ; we have permission, do nothing
708 (error "Abort")) ; abort, we don't have permission
709 ;; Do the changes
710 (mapc 'reftex-toc-promote-action entries)
711 ;; Rescan the document and rebuilt the toc buffer
712 (save-window-excursion
713 (reftex-toc-Rescan))
714 (reftex-toc-restore-region reftex--start-line reftex--mark-line)
715 (message "%d section%s %smoted"
716 nsec (if (= 1 nsec) "" "s") reftex--pro-or-de)
717 nil))
718 (if msg (progn (ding) (message "%s" msg)))))
721 (defun reftex-toc-restore-region (point-line &optional mark-line)
722 (let ((mpos
723 (when mark-line
724 (goto-char (point-min))
725 (forward-line (1- mark-line))
726 (point))))
727 (when point-line
728 (goto-char (point-min))
729 (forward-line (1- point-line)))
730 (when mpos
731 (set-mark mpos)
732 (if (featurep 'xemacs)
733 (zmacs-activate-region)
734 (setq mark-active t
735 deactivate-mark nil)))))
737 (defun reftex-toc-promote-prepare (x delta)
738 "Look at a TOC entry and see if we could pro/demote it.
739 This function prepares everything for the change, but does not do it.
740 The return value is a list with information needed when doing the
741 promotion/demotion later. DELTA is the level change."
742 (let* ((data (car x))
743 (toc-point (cdr x))
744 (marker (nth 4 data))
745 (literal (nth 7 data))
746 (load nil)
747 (name nil)
748 ;; Here follows some paranoid code to make very sure we are not
749 ;; going to break anything
751 (if (and (markerp marker) (marker-buffer marker))
752 ;; Buffer is still live and we have the marker.
753 (progn
754 (with-current-buffer (marker-buffer marker)
755 ;; Goto the buffer and check of section is unchanged
756 (goto-char (marker-position marker))
757 (if (looking-at (regexp-quote literal))
758 ;; OK, get the makro name
759 (progn
760 (beginning-of-line 1)
761 (if (looking-at reftex-section-regexp)
762 (setq name (reftex-match-string 2))
763 (error "Something is wrong! Contact maintainer!")))
764 ;; Section has changed, request scan and loading
765 ;; We use a variable to delay until after the safe-exc.
766 ;; because otherwise we lose the region.
767 (setq load t)))
768 ;; Scan document and load all files, this exits command
769 (if load (reftex-toc-load-all-files-for-promotion))) ; exits
770 ;; We don't have a live marker: scan and load files.
771 (reftex-toc-load-all-files-for-promotion)))
772 (level (cdr (assoc name reftex-section-levels-all)))
773 (_ (if (not (integerp level))
774 (progn
775 (goto-char toc-point)
776 (error "Cannot %smote special sections" reftex--pro-or-de))))
777 (newlevel (if (>= level 0) (+ delta level) (- level delta)))
778 (_ (if (or (and (>= level 0) (= newlevel -1))
779 (and (< level 0) (= newlevel 0)))
780 (error "Cannot %smote \\%s" reftex--pro-or-de name)))
781 (newname (reftex-toc-newhead-from-alist newlevel name
782 reftex-section-levels-all)))
783 (if (and name newname)
784 (list data name newname toc-point)
785 (goto-char toc-point)
786 (error "Cannot %smote \\%s" reftex--pro-or-de name))))
788 (defun reftex-toc-promote-action (x)
789 "Change the level of a TOC entry.
790 `reftex--pro-or-de' is assumed to be dynamically scoped into this function."
791 (let* ((data (car x))
792 (name (nth 1 x))
793 (newname (nth 2 x))
794 (marker (nth 4 data)))
795 (with-current-buffer (marker-buffer marker)
796 (goto-char (marker-position marker))
797 (if (looking-at (concat "\\([ \t]*" reftex-section-pre-regexp "\\)" (regexp-quote name)))
798 (replace-match (concat "\\1" newname))
799 (error "Fatal error during %smotion" reftex--pro-or-de)))))
801 (defun reftex-toc-extract-section-number (entry)
802 "Get the numbering of a TOC entry, for message purposes."
803 (if (string-match "\\s-*\\(\\S-+\\)" (nth 2 (car entry)))
804 (match-string 1 (nth 2 (car entry)))
805 "?"))
807 (defun reftex-toc-newhead-from-alist (nlevel head alist)
808 "Get new heading with level NLEVEL from ALIST.
809 If there are no such entries, return nil.
810 If there are several different entries with same new level, choose the
811 one with the smallest distance to the association of HEAD in the alist.
812 This makes it possible for promotion to work several sets of headings,
813 if these sets are sorted blocks in the alist."
814 (let* ((al alist)
815 (ass (assoc head al))
816 (pos (length (memq ass al)))
817 dist (mindist 1000) newhead)
818 (while al
819 (if (equal (cdar al) nlevel)
820 (progn
821 (setq dist (abs (- (length al) pos)))
822 (if (< dist mindist)
823 (setq newhead (car (car al))
824 mindist dist))))
825 (setq al (cdr al)))
826 newhead))
828 (defun reftex-toc-check-docstruct ()
829 "Check if the current docstruct is fully up to date and all files visited."
830 ;; We do this by checking if all sections are on the right position
831 (let ((docstruct (symbol-value reftex-docstruct-symbol))
832 entry marker empoint)
833 (catch 'exit
834 (while (setq entry (pop docstruct))
835 (if (eq (car entry) 'toc)
836 (progn
837 (setq marker (nth 4 entry)
838 empoint (nth 8 entry))
839 (if (not (and (markerp marker)
840 (marker-buffer marker)
841 (= (marker-position marker) empoint)))
842 (throw 'exit nil)))))
843 t)))
845 (defun reftex-toc-load-all-files-for-promotion ()
846 "Make sure all files of the document are being visited by buffers,
847 and that the scanning info is absolutely up to date.
848 We do this by rescanning with `reftex-keep-temporary-buffers' bound to t.
849 The variable `reftex--pro-or-de' is assumed to be dynamically scoped into this function.
850 When finished, we exit with an error message."
851 (let ((reftex-keep-temporary-buffers t))
852 (reftex-toc-Rescan)
853 (reftex-toc-restore-region reftex--start-line reftex--mark-line)
854 (throw 'exit
855 "TOC had to be updated first. Please check selection and repeat the command.")))
857 (defun reftex-toc-rename-label ()
858 "Rename the currently selected label in the *toc* buffer.
859 This launches a global search and replace in order to rename a label.
860 Renaming a label is hardly ever necessary - the only exception is after
861 promotion/demotion in connection with a package like fancyref, where the
862 label prefix determines the wording of a reference."
863 (interactive)
864 (let* ((toc (get-text-property (point) :data))
865 (label (car toc)) newlabel)
866 (if (not (stringp label))
867 (error "This is not a label entry"))
868 (setq newlabel (read-string (format "Rename label \"%s\" to:" label)))
869 (if (assoc newlabel (symbol-value reftex-docstruct-symbol))
870 (if (not (y-or-n-p
871 (format-message "Label `%s' exists. Use anyway? " label)))
872 (error "Abort")))
873 (save-excursion
874 (save-window-excursion
875 (reftex-toc-visit-location t)
876 (condition-case nil
877 (reftex-query-replace-document
878 (concat "{" (regexp-quote label) "}")
879 (format "{%s}" newlabel))
880 (error t))))
881 (reftex-toc-rescan)))
884 (defun reftex-toc-visit-location (&optional final no-revisit)
885 ;; Visit the tex file corresponding to the TOC entry on the current line.
886 ;; If FINAL is t, stay there
887 ;; If FINAL is 'hide, hide the TOC window.
888 ;; Otherwise, move cursor back into TOC window.
889 ;; NO-REVISIT means don't visit files, just use live buffers.
890 ;; This function is pretty clever about finding back a section heading,
891 ;; even if the buffer is not live, or things like outline, x-symbol etc.
892 ;; have been active.
894 (let* ((toc (get-text-property (point) :data))
895 (toc-window (selected-window))
896 match)
898 (unless toc (error "Don't know which TOC line to visit"))
900 (cond
902 ((eq (car toc) 'toc)
903 ;; a toc entry
904 (setq match (reftex-toc-find-section toc no-revisit)))
906 ((eq (car toc) 'index)
907 ;; an index entry
908 (setq match (reftex-index-show-entry toc no-revisit)))
910 ((memq (car toc) '(bof eof))
911 ;; A file entry
912 (setq match
913 (let ((where (car toc))
914 (file (nth 1 toc)))
915 (if (or (not no-revisit) (reftex-get-buffer-visiting file))
916 (progn
917 (switch-to-buffer-other-window
918 (reftex-get-file-buffer-force file nil))
919 (goto-char (if (eq where 'bof) (point-min) (point-max))))
920 (message "%s" reftex-no-follow-message) nil))))
922 ((stringp (car toc))
923 ;; a label
924 (setq match (reftex-show-label-location toc reftex-callback-fwd
925 no-revisit t))))
927 (unless match
928 (select-window toc-window)
929 (error "Cannot find location"))
931 ;; Use the `final' parameter to decide what to do next.
932 (cond
933 ((eq final t)
934 (with-selected-window toc-window
935 (reftex-unhighlight 0)))
936 ((eq final 'hide)
937 (let ((window (selected-window))
938 (buffer (window-buffer)))
939 (unless (eq window toc-window) ;FIXME: Can this happen?
940 (with-selected-window toc-window
941 (reftex-unhighlight 0)
942 (or (one-window-p) (delete-window))))
943 ;; If window is still live, buffer is already visible
944 ;; so let's not make it visible in yet-another-window.
945 (unless (window-live-p window)
946 ;; FIXME: How could window not be live?
947 (pop-to-buffer-same-window buffer))
948 (reftex-re-enlarge)))
950 (unless (eq (selected-frame) (window-frame toc-window))
951 ;; Make sure `toc-window' is not just selected but has focus.
952 (select-frame-set-input-focus (window-frame toc-window)))
953 (select-window toc-window)))))
955 (defun reftex-toc-find-section (toc &optional no-revisit)
956 (let* ((file (nth 3 toc))
957 (marker (nth 4 toc))
958 (level (nth 5 toc))
959 (literal (nth 7 toc))
960 (emergency-point (nth 8 toc))
961 (match
962 (cond
963 ((and (markerp marker) (marker-buffer marker))
964 ;; Buffer is still live and we have the marker. Should be easy.
965 (switch-to-buffer-other-window (marker-buffer marker))
966 (push-mark nil)
967 (goto-char (marker-position marker))
968 (or (looking-at (regexp-quote literal))
969 (looking-at (reftex-make-regexp-allow-for-ctrl-m literal))
970 (looking-at (reftex-make-desperate-section-regexp literal))
971 (looking-at (concat "\\\\"
972 (regexp-quote
973 (car
974 (rassq level
975 reftex-section-levels-all)))
976 "[[{]?"))))
977 ((or (not no-revisit)
978 (reftex-get-buffer-visiting file))
979 ;; Marker is lost. Use the backup method.
980 (switch-to-buffer-other-window
981 (reftex-get-file-buffer-force file nil))
982 (goto-char (or emergency-point (point-min)))
983 (or (looking-at (regexp-quote literal))
984 (let ((len (length literal)))
985 (or (reftex-nearest-match (regexp-quote literal) len)
986 (reftex-nearest-match
987 (reftex-make-regexp-allow-for-ctrl-m literal) len)
988 (reftex-nearest-match
989 (reftex-make-desperate-section-regexp literal) len)))))
990 (t (message "%s" reftex-no-follow-message) nil))))
991 (when match
992 (goto-char (match-beginning 0))
993 (if (not (= (point) (point-max))) (recenter 1))
994 (reftex-highlight 0 (match-beginning 0) (match-end 0) (current-buffer)))
995 match))
997 (defun reftex-make-desperate-section-regexp (old)
998 ;; Return a regexp which will still match a section statement even if
999 ;; x-symbol or isotex or the like have been at work in the mean time.
1000 (let* ((n (1+ (string-match "[[{]" old)))
1001 (new (regexp-quote (substring old 0 (1+ (string-match "[[{]" old)))))
1002 (old (substring old n)))
1003 (while (string-match
1004 "\\([\r\n]\\)\\|\\(\\`\\|[ \t\n\r]\\)\\([a-zA-Z0-9]+\\)\\([ \t\n\r]\\|}\\'\\)"
1005 old)
1006 (if (match-beginning 1)
1007 (setq new (concat new "[^\n\r]*[\n\r]"))
1008 (setq new (concat new "[^\n\r]*" (match-string 3 old))))
1009 (setq old (substring old (match-end 0))))
1010 new))
1012 ;; Auto recentering of TOC window
1014 (defun reftex-recenter-toc-when-idle ()
1015 (and (> (buffer-size) 5)
1016 reftex-mode
1017 (not (active-minibuffer-window))
1018 (fboundp 'reftex-toc-mode)
1019 (get-buffer-window "*toc*" 'visible)
1020 (string= reftex-last-toc-master (reftex-TeX-master-file))
1021 (let (current-prefix-arg)
1022 (reftex-toc-recenter))))
1024 ;;;###autoload
1025 (defun reftex-toggle-auto-toc-recenter ()
1026 "Toggle the automatic recentering of the TOC window.
1027 When active, leaving point idle will make the TOC window jump to the correct
1028 section."
1029 (interactive)
1030 (if reftex-toc-auto-recenter-timer
1031 (progn
1032 (if (featurep 'xemacs)
1033 (delete-itimer reftex-toc-auto-recenter-timer)
1034 (cancel-timer reftex-toc-auto-recenter-timer))
1035 (setq reftex-toc-auto-recenter-timer nil)
1036 (message "Automatic recentering of TOC window was turned off"))
1037 (setq reftex-toc-auto-recenter-timer
1038 (if (featurep 'xemacs)
1039 (start-itimer "RefTeX Idle Timer for recenter"
1040 'reftex-recenter-toc-when-idle
1041 reftex-idle-time reftex-idle-time t)
1042 (run-with-idle-timer
1043 reftex-idle-time t 'reftex-recenter-toc-when-idle)))
1044 (message "Automatic recentering of TOC window was turned on")))
1046 (defun reftex-toc-toggle-dedicated-frame ()
1047 "Toggle the display of a separate frame for the TOC.
1048 This frame is not used by the `reftex-toc' commands, but it is useful to
1049 always show the current section in connection with the option
1050 `reftex-auto-recenter-toc'."
1051 (interactive)
1052 (catch 'exit
1053 (let* ((frames (frame-list)) frame
1054 (get-frame-prop-func (if (fboundp 'frame-property)
1055 'frame-property
1056 'frame-parameter)))
1057 (while (setq frame (pop frames))
1058 (if (equal (funcall get-frame-prop-func frame 'name)
1059 "RefTeX TOC Frame")
1060 (progn
1061 (delete-frame frame)
1062 (throw 'exit nil))))
1063 (reftex-make-separate-toc-frame))))
1065 (defun reftex-make-separate-toc-frame ()
1066 ;; Create a new fame showing only the TOC buffer.
1067 (let ((current-frame (selected-frame))
1068 (current-window (selected-window))
1069 (current-toc-window (get-buffer-window "*toc*" 'visible))
1070 current-toc-frame)
1071 (if (and current-toc-window
1072 (not (equal (selected-frame) (window-frame current-toc-window))))
1074 (setq current-toc-frame
1075 (make-frame
1076 '((name . "RefTeX TOC Frame")
1077 (height . 20) (width . 60)
1078 (unsplittable . t)
1079 (minibuffer . nil)
1080 (default-toolbar-visible-p . nil)
1081 (menubar-visible-p . nil)
1082 (horizontal-scrollbar-visible-p . nil))))
1083 (select-frame current-toc-frame)
1084 (switch-to-buffer "*toc*")
1085 (select-frame current-frame)
1086 (cond ((fboundp 'x-focus-frame)
1087 (x-focus-frame current-frame))
1088 ((and (featurep 'xemacs) ; `focus-frame' is a nop in Emacs.
1089 (fboundp 'focus-frame))
1090 (focus-frame current-frame)))
1091 (select-window current-window)
1092 (when (eq reftex-auto-recenter-toc 'frame)
1093 (unless reftex-toc-auto-recenter-timer
1094 (reftex-toggle-auto-toc-recenter))
1095 (add-hook 'delete-frame-hook 'reftex-toc-delete-frame-hook)))))
1097 (defun reftex-toc-delete-frame-hook (frame)
1098 (if (and reftex-toc-auto-recenter-timer
1099 (reftex-toc-dframe-p frame))
1100 (progn
1101 (reftex-toggle-auto-toc-recenter))))
1103 ;;; reftex-toc.el ends here
1105 ;; Local Variables:
1106 ;; generated-autoload-file: "reftex-loaddefs.el"
1107 ;; End: