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