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