Allow 'browse-url-emacs' to fetch URL in the selected window
[emacs.git] / lisp / textmodes / reftex-toc.el
blob9c158a5f569efc64db4948ba59cbd6bd0a17fd15
1 ;;; reftex-toc.el --- RefTeX's table of contents mode
3 ;; Copyright (C) 1997-2000, 2003-2018 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/>.
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 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)))
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 ;; 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))
390 (window-height)))))
391 (when (> count 0)
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)))
399 (let ((res (equal
400 (if (fboundp 'frame-property)
401 (frame-property frame 'name)
402 (frame-parameter frame 'name))
403 "RefTeX TOC Frame")))
404 (if (and res error)
405 (error "This frame is view-only. Use `C-c =' to create TOC window for commands"))
406 res))
408 (defun reftex-toc-show-help ()
409 "Show a summary of special key bindings."
410 (interactive)
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."
421 (interactive)
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)
426 (point))))
427 (defun reftex-toc-previous (&optional _arg)
428 "Move to previous selectable item."
429 (interactive)
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)
433 (point))))
434 (defun reftex-toc-next-heading (&optional arg)
435 "Move to next table of contents line."
436 (interactive "p")
437 (when (featurep 'xemacs) (setq zmacs-region-stays t))
438 (end-of-line)
439 (re-search-forward "^ " nil t arg)
440 (beginning-of-line))
441 (defun reftex-toc-previous-heading (&optional arg)
442 "Move to previous table of contents line."
443 (interactive "p")
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)."
448 (interactive)
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."
453 (interactive)
454 (setq reftex-toc-include-file-boundaries
455 (not reftex-toc-include-file-boundaries))
456 (reftex-toc-revert))
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
460 that specific type."
461 (interactive "P")
462 (setq reftex-toc-include-labels
463 (if arg (reftex-query-label-type)
464 (not reftex-toc-include-labels)))
465 (reftex-toc-revert))
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
469 specific index."
470 (interactive "P")
471 (setq reftex-toc-include-index-entries
472 (if arg (reftex-index-select-tag)
473 (not reftex-toc-include-index-entries)))
474 (reftex-toc-revert))
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."
478 (interactive)
479 (setq reftex-toc-include-context (not reftex-toc-include-context))
480 (reftex-toc-revert))
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'."
485 (interactive "P")
486 (setq reftex-toc-max-level (if arg
487 (prefix-numeric-value arg)
488 100))
489 (setq reftex-toc-max-level-indicator
490 (if arg (int-to-string reftex-toc-max-level) "ALL"))
491 (reftex-toc-revert))
492 (defun reftex-toc-view-line ()
493 "View document location in other window."
494 (interactive)
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."
499 (interactive)
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."
504 (interactive)
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."
509 (interactive "e")
510 (mouse-set-point ev)
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."
515 (interactive)
516 (reftex-toc-dframe-p nil 'error)
517 (let ((this-window (selected-window)))
518 (unwind-protect
519 (progn
520 (switch-to-buffer-other-window
521 (marker-buffer reftex-toc-return-marker))
522 (goto-char (marker-position reftex-toc-return-marker))
523 (recenter '(4)))
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
528 is destroyed."
529 (interactive)
530 (if (and (one-window-p)
531 (reftex-toc-dframe-p)
532 (> (length (frame-list)) 1))
533 (delete-frame)
534 (or (one-window-p) (delete-window))
535 (switch-to-buffer (marker-buffer reftex-toc-return-marker))
536 (reftex-re-enlarge)
537 (goto-char (or (marker-position reftex-toc-return-marker) (point)))))
538 (defun reftex-toc-quit-and-kill ()
539 "Kill the *toc* buffer."
540 (interactive)
541 (kill-buffer "*toc*")
542 (or (one-window-p) (delete-window))
543 (switch-to-buffer (marker-buffer reftex-toc-return-marker))
544 (reftex-re-enlarge)
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."
550 (interactive "P")
551 (reftex-toc-dframe-p nil 'error)
552 (let ((data (get-text-property (point) :data))
553 (docstruct (symbol-value reftex-docstruct-symbol))
554 bor eor restr)
555 (when (equal arg 2)
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."
565 (interactive)
566 (if (and reftex-enable-partial-scans
567 (null current-prefix-arg))
568 (let* ((data (get-text-property (point) :data))
569 (what (car 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))))
575 (if (not file)
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))
582 (reftex-toc))))
583 (reftex-toc-Rescan))
584 (reftex-kill-temporary-buffers))
586 (defun reftex-toc-Rescan (&rest _)
587 "Regenerate the *toc* buffer by reparsing the entire document."
588 (interactive)
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))
595 (reftex-toc)))
597 (defun reftex-toc-revert (&rest _)
598 "Regenerate the TOC from the internal lists."
599 (interactive)
600 (let ((unsplittable
601 (if (fboundp 'frame-property)
602 (frame-property (selected-frame) 'unsplittable)
603 (frame-parameter nil 'unsplittable)))
604 (reftex--rebuilding-toc t))
605 (if unsplittable
606 (switch-to-buffer
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)
612 (reftex-toc t t))
614 (defun reftex-toc-external (&rest _)
615 "Switch to table of contents of an external document."
616 (interactive)
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
621 xr-alist 0)))
622 (switch-to-buffer-other-window (or (reftex-get-file-buffer-force
623 (cdr (nth xr-index xr-alist)))
624 (error "Cannot switch document")))
625 (reftex-toc)
626 (if (equal old-buf (current-buffer))
627 (message "")
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."
633 (interactive "P")
634 (goto-char (point-min))
635 (re-search-forward
636 (concat "^ *" (number-to-string (if (numberp arg) arg 1)) " ")
637 nil t)
638 (beginning-of-line))
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."
648 (interactive)
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."
652 (interactive)
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
657 point."
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
660 ;; test.
661 (let* ((reftex--start-line (+ (count-lines (point-min) (point))
662 (if (bolp) 1 0)))
663 (reftex--mark-line
664 (if (reftex-region-active-p)
665 (save-excursion (goto-char (mark))
666 (+ (count-lines (point-min) (point))
667 (if (bolp) 1 0)))))
668 (start-pos (point))
669 (reftex--pro-or-de (if (> delta 0) "de" "pro"))
670 beg end entries data sections nsec msg)
671 (setq msg
672 (catch 'exit
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)))
681 (goto-char beg)
682 (while (and (setq data (get-text-property (point) :data))
683 (<= (point) end))
684 (if (eq (car data) 'toc) (push (cons data (point)) entries))
685 (goto-char (or (next-single-property-change (point) :data)
686 (point-max))))
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)
692 (setq sections
693 (concat sections "-"
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))
699 entries))
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))
704 (yes-or-no-p ; ask
705 (format "%s %d toc-entr%s (section%s %s)? "
706 (if (< delta 0) "Promote" "Demote")
707 nsec
708 (if (= 1 nsec) "y" "ies")
709 (if (= 1 nsec) "" "s")
710 sections)))
711 nil ; we have permission, do nothing
712 (error "Abort")) ; abort, we don't have permission
713 ;; Do the changes
714 (mapc 'reftex-toc-promote-action entries)
715 ;; Rescan the document and rebuilt the toc buffer
716 (save-window-excursion
717 (reftex-toc-Rescan))
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)
721 nil))
722 (if msg (progn (ding) (message "%s" msg)))))
725 (defun reftex-toc-restore-region (point-line &optional mark-line)
726 (let ((mpos
727 (when mark-line
728 (goto-char (point-min))
729 (forward-line (1- mark-line))
730 (point))))
731 (when point-line
732 (goto-char (point-min))
733 (forward-line (1- point-line)))
734 (when mpos
735 (set-mark mpos)
736 (if (featurep 'xemacs)
737 (zmacs-activate-region)
738 (setq mark-active t
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))
747 (toc-point (cdr x))
748 (marker (nth 4 data))
749 (literal (nth 7 data))
750 (load nil)
751 (name nil)
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.
757 (progn
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
763 (progn
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.
771 (setq load t)))
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))
778 (progn
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))
796 (name (nth 1 x))
797 (newname (nth 2 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)))
809 "?"))
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."
818 (let* ((al alist)
819 (ass (assoc head al))
820 (pos (length (memq ass al)))
821 dist (mindist 1000) newhead)
822 (while al
823 (if (equal (cdar al) nlevel)
824 (progn
825 (setq dist (abs (- (length al) pos)))
826 (if (< dist mindist)
827 (setq newhead (car (car al))
828 mindist dist))))
829 (setq al (cdr al)))
830 newhead))
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)
837 (catch 'exit
838 (while (setq entry (pop docstruct))
839 (if (eq (car entry) 'toc)
840 (progn
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)))))
847 t)))
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))
856 (reftex-toc-Rescan)
857 (reftex-toc-restore-region reftex--start-line reftex--mark-line)
858 (throw 'exit
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."
867 (interactive)
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))
874 (if (not (y-or-n-p
875 (format-message "Label `%s' exists. Use anyway? " label)))
876 (error "Abort")))
877 (save-excursion
878 (save-window-excursion
879 (reftex-toc-visit-location t)
880 (condition-case nil
881 (reftex-query-replace-document
882 (concat "{" (regexp-quote label) "}")
883 (format "{%s}" newlabel))
884 (error t))))
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.
896 ;; have been active.
898 (let* ((toc (get-text-property (point) :data))
899 (toc-window (selected-window))
900 match)
902 (unless toc (error "Don't know which TOC line to visit"))
904 (cond
906 ((eq (car toc) 'toc)
907 ;; a toc entry
908 (setq match (reftex-toc-find-section toc no-revisit)))
910 ((eq (car toc) 'index)
911 ;; an index entry
912 (setq match (reftex-index-show-entry toc no-revisit)))
914 ((memq (car toc) '(bof eof))
915 ;; A file entry
916 (setq match
917 (let ((where (car toc))
918 (file (nth 1 toc)))
919 (if (or (not no-revisit) (reftex-get-buffer-visiting file))
920 (progn
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))))
926 ((stringp (car toc))
927 ;; a label
928 (setq match (reftex-show-label-location toc reftex-callback-fwd
929 no-revisit t))))
931 (unless match
932 (select-window toc-window)
933 (error "Cannot find location"))
935 ;; Use the `final' parameter to decide what to do next.
936 (cond
937 ((eq final t)
938 (with-selected-window toc-window
939 (reftex-unhighlight 0)))
940 ((eq final 'hide)
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))
961 (marker (nth 4 toc))
962 (level (nth 5 toc))
963 (literal (nth 7 toc))
964 (emergency-point (nth 8 toc))
965 (match
966 (cond
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))
970 (push-mark nil)
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 "\\\\"
976 (regexp-quote
977 (car
978 (rassq level
979 reftex-section-levels-all)))
980 "[[{]?"))))
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))))
995 (when match
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)))
999 match))
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]\\|}\\'\\)"
1009 old)
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))))
1014 new))
1016 ;; Auto recentering of TOC window
1018 (defun reftex-recenter-toc-when-idle ()
1019 (and (> (buffer-size) 5)
1020 reftex-mode
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))))
1028 ;;;###autoload
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
1032 section."
1033 (interactive)
1034 (if reftex-toc-auto-recenter-timer
1035 (progn
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'."
1055 (interactive)
1056 (catch 'exit
1057 (let* ((frames (frame-list)) frame
1058 (get-frame-prop-func (if (fboundp 'frame-property)
1059 'frame-property
1060 'frame-parameter)))
1061 (while (setq frame (pop frames))
1062 (if (equal (funcall get-frame-prop-func frame 'name)
1063 "RefTeX TOC Frame")
1064 (progn
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))
1074 current-toc-frame)
1075 (if (and current-toc-window
1076 (not (equal (selected-frame) (window-frame current-toc-window))))
1078 (setq current-toc-frame
1079 (make-frame
1080 '((name . "RefTeX TOC Frame")
1081 (height . 20) (width . 60)
1082 (unsplittable . t)
1083 (minibuffer . nil)
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-functions '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))
1104 (progn
1105 (reftex-toggle-auto-toc-recenter))))
1107 ;;; reftex-toc.el ends here
1109 ;; Local Variables:
1110 ;; generated-autoload-file: "reftex-loaddefs.el"
1111 ;; End: