1 ;;; reftex-toc.el --- RefTeX's table of contents mode
3 ;; Copyright (C) 1997, 1998, 1999, 2000, 2003, 2004, 2005, 2006, 2007,
4 ;; 2008, 2009, 2010 Free Software Foundation, Inc.
6 ;; Author: Carsten Dominik <dominik@science.uva.nl>
7 ;; Maintainer: auctex-devel@gnu.org
10 ;; This file is part of GNU Emacs.
12 ;; GNU Emacs is free software: you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation, either version 3 of the License, or
15 ;; (at your option) any later version.
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
29 (eval-when-compile (require 'cl
))
34 (defvar reftex-toc-map
(make-sparse-keymap)
35 "Keymap used for *toc* buffer.")
37 (defvar reftex-toc-menu
)
38 (defvar reftex-last-window-height nil
)
39 (defvar reftex-last-window-width nil
)
40 (defvar reftex-toc-include-labels-indicator nil
)
41 (defvar reftex-toc-include-index-indicator nil
)
42 (defvar reftex-toc-max-level-indicator nil
)
44 (defun reftex-toc-mode ()
45 "Major mode for managing Table of Contents for LaTeX files.
46 This buffer was created with RefTeX.
47 Press `?' for a summary of important key bindings.
49 Here are all local bindings.
53 (kill-all-local-variables)
54 (setq major-mode
'reftex-toc-mode
56 (use-local-map reftex-toc-map
)
57 (set (make-local-variable 'transient-mark-mode
) t
)
58 (when (featurep 'xemacs
)
59 (set (make-local-variable 'zmacs-regions
) t
))
60 (set (make-local-variable 'revert-buffer-function
) 'reftex-toc-revert
)
61 (set (make-local-variable 'reftex-toc-include-labels-indicator
) "")
62 (set (make-local-variable 'reftex-toc-max-level-indicator
)
63 (if (= reftex-toc-max-level
100)
65 (int-to-string reftex-toc-max-level
)))
66 (setq mode-line-format
67 (list "---- " 'mode-line-buffer-identification
68 " " 'global-mode-string
" (" mode-name
")"
69 " L<" 'reftex-toc-include-labels-indicator
">"
70 " I<" 'reftex-toc-include-index-indicator
">"
71 " T<" 'reftex-toc-max-level-indicator
">"
73 (setq truncate-lines t
)
74 (when (featurep 'xemacs
)
75 ;; XEmacs needs the call to make-local-hook
76 (make-local-hook 'post-command-hook
)
77 (make-local-hook 'pre-command-hook
))
78 (make-local-variable 'reftex-last-follow-point
)
79 (add-hook 'post-command-hook
'reftex-toc-post-command-hook nil t
)
80 (add-hook 'pre-command-hook
'reftex-toc-pre-command-hook nil t
)
81 (easy-menu-add reftex-toc-menu reftex-toc-map
)
82 (run-hooks 'reftex-toc-mode-hook
))
84 (defvar reftex-last-toc-file nil
85 "Stores the file name from which `reftex-toc' was called. For redo command.")
88 (defvar reftex-toc-return-marker
(make-marker)
89 "Marker which makes it possible to return from toc to old position.")
91 (defconst reftex-toc-help
92 " AVAILABLE KEYS IN TOC BUFFER
93 ============================
94 n / p next-line / previous-line
95 SPC Show the corresponding location of the LaTeX document.
96 TAB Goto the location and keep the *toc* window.
97 RET Goto the location and hide the *toc* window (also on mouse-2).
98 < / > Promote / Demote section, or all sections in region.
99 C-c > Display Index. With prefix arg, restrict index to current section.
100 q / k Hide/Kill *toc* buffer, return to position of reftex-toc command.
101 l i c F Toggle display of [l]abels, [i]ndex, [c]ontext, [F]ile borders.
102 t Change maximum toc depth (e.g. `3 t' hides levels greater than 3).
103 f / g Toggle follow mode / Refresh *toc* buffer.
104 a / d Toggle auto recenter / Toggle dedicated frame
105 r / C-u r Reparse the LaTeX document / Reparse entire LaTeX document.
106 . In other window, show position from where `reftex-toc' was called.
107 M-% Global search and replace to rename label at point.
108 x Switch to TOC of external document (with LaTeX package `xr').
109 z Jump to a specific section (e.g. '3 z' goes to section 3).")
111 (defun reftex-toc (&optional rebuild reuse
)
112 "Show the table of contents for the current document.
113 When called with a raw C-u prefix, rescan the document first."
115 ;; The REUSE argument means, search all visible frames for a window
116 ;; displaying the toc window. If yes, reuse this window.
120 (if (or (not (string= reftex-last-toc-master
(reftex-TeX-master-file)))
122 (reftex-erase-buffer "*toc*"))
124 (setq reftex-last-toc-file
(buffer-file-name))
125 (setq reftex-last-toc-master
(reftex-TeX-master-file))
127 (set-marker reftex-toc-return-marker
(point))
129 ;; If follow mode is active, arrange to delay it one command
130 (if reftex-toc-follow-mode
131 (setq reftex-toc-follow-mode
1))
133 (and reftex-toc-include-index-entries
134 (reftex-ensure-index-support))
135 (or reftex-support-index
136 (setq reftex-toc-include-index-entries nil
))
138 ;; Ensure access to scanning info and rescan buffer if prefix are is '(4)
139 (reftex-access-scan-info current-prefix-arg
)
141 (let* ((this-buf (current-buffer))
142 (docstruct-symbol reftex-docstruct-symbol
)
143 (xr-data (assq 'xr
(symbol-value reftex-docstruct-symbol
)))
144 (xr-alist (cons (cons "" (buffer-file-name)) (nth 1 xr-data
)))
145 (here-I-am (if (boundp 'reftex-rebuilding-toc
)
146 (get 'reftex-toc
:reftex-data
)
147 (car (reftex-where-am-I))))
148 (unsplittable (if (fboundp 'frame-property
)
149 (frame-property (selected-frame) 'unsplittable
)
150 (frame-parameter (selected-frame) 'unsplittable
)))
153 (if (setq toc-window
(get-buffer-window
155 (if reuse
'visible
)))
156 (select-window toc-window
)
157 (when (or (not reftex-toc-keep-other-windows
)
158 (< (window-height) (* 2 window-min-height
)))
159 (delete-other-windows))
161 (setq reftex-last-window-width
(window-width)
162 reftex-last-window-height
(window-height)) ; remember
165 (if reftex-toc-split-windows-horizontally
166 (split-window-horizontally
167 (floor (* (window-width)
168 reftex-toc-split-windows-fraction
)))
169 (split-window-vertically
170 (floor (* (window-height)
171 reftex-toc-split-windows-fraction
)))))
173 (switch-to-buffer "*toc*"))
175 (or (eq major-mode
'reftex-toc-mode
) (reftex-toc-mode))
176 (set (make-local-variable 'reftex-docstruct-symbol
) docstruct-symbol
)
177 (setq reftex-toc-include-labels-indicator
178 (if (eq reftex-toc-include-labels t
)
180 reftex-toc-include-labels
))
181 (setq reftex-toc-include-index-indicator
182 (if (eq reftex-toc-include-index-entries t
)
184 reftex-toc-include-index-entries
))
188 ;; buffer is empty - fill it with the table of contents
189 (message "Building *toc* buffer...")
191 (setq buffer-read-only nil
)
193 "TABLE-OF-CONTENTS on %s
194 SPC=view TAB=goto RET=goto+hide [q]uit [r]escan [l]abels [f]ollow [x]r [?]Help
195 ------------------------------------------------------------------------------
196 " (abbreviate-file-name reftex-last-toc-master
)))
198 (if (reftex-use-fonts)
199 (put-text-property (point-min) (point) 'face reftex-toc-header-face
))
200 (put-text-property (point-min) (point) 'intangible t
)
201 (put-text-property (point-min) (1+ (point-min)) 'xr-alist xr-alist
)
204 (reftex-insert-docstruct
207 reftex-toc-include-labels
208 reftex-toc-include-index-entries
209 reftex-toc-include-file-boundaries
210 reftex-toc-include-context
218 (run-hooks 'reftex-display-copied-context-hook
)
219 (message "Building *toc* buffer...done.")
220 (setq buffer-read-only t
))
222 ;; Only compute the offset
224 (or (reftex-get-offset this-buf here-I-am
225 (if reftex-toc-include-labels
" " nil
)
227 reftex-toc-include-index-entries
228 reftex-toc-include-file-boundaries
)
229 (reftex-last-assoc-before-elt
231 (symbol-value reftex-docstruct-symbol
))))
232 (put 'reftex-toc
:reftex-line
3)
233 (goto-char (point-min))
236 ;; Find the correct starting point
237 (reftex-find-start-point (point) offset
(get 'reftex-toc
:reftex-line
))
238 (setq reftex-last-follow-point
(point))))
240 (defun reftex-toc-recenter (&optional arg
)
241 "Display the TOC window and highlight line corresponding to current position."
243 (let ((buf (current-buffer))
244 (frame (selected-frame)))
246 (if (= (count-lines 1 (point)) 2)
247 (let ((current-prefix-arg nil
))
248 (select-window (get-buffer-window buf frame
))
251 (not (get-text-property (point) 'intangible
))
252 (memq reftex-highlight-selection
'(cursor both
))
254 (or (previous-single-property-change
255 (min (point-max) (1+ (point))) :data
)
257 (or (next-single-property-change (point) :data
)
259 (select-window (get-buffer-window buf frame
))))
261 (defun reftex-toc-pre-command-hook ()
262 ;; used as pre command hook in *toc* buffer
263 (reftex-unhighlight 0)
266 (defun reftex-toc-post-command-hook ()
267 ;; used in the post-command-hook for the *toc* buffer
268 (when (get-text-property (point) :data
)
269 (put 'reftex-toc
:reftex-data
(get-text-property (point) :data
))
271 (not (get-text-property (point) 'intangible
))
272 (memq reftex-highlight-selection
'(cursor both
))
274 (or (previous-single-property-change (1+ (point)) :data
)
276 (or (next-single-property-change (point) :data
)
278 (if (integerp reftex-toc-follow-mode
)
279 ;; remove delayed action
280 (setq reftex-toc-follow-mode t
)
281 (and (not (reftex-toc-dframe-p))
282 reftex-toc-follow-mode
283 (not (equal reftex-last-follow-point
(point)))
284 ;; show context in other window
285 (setq reftex-last-follow-point
(point))
287 (reftex-toc-visit-location nil
(not reftex-revisit-to-follow
))
290 (defun reftex-re-enlarge ()
291 ;; Enlarge window to a remembered size.
292 (if reftex-toc-split-windows-horizontally
293 (enlarge-window-horizontally
294 (max 0 (- (or reftex-last-window-width
(window-width))
297 (max 0 (- (or reftex-last-window-height
(window-height))
300 (defun reftex-toc-dframe-p (&optional frame error
)
301 ;; Check if FRAME is the dedicated TOC frame.
302 ;; If yes, and ERROR is non-nil, throw an error.
303 (setq frame
(or frame
(selected-frame)))
305 (if (fboundp 'frame-property
)
306 (frame-property frame
'name
)
307 (frame-parameter frame
'name
))
308 "RefTeX TOC Frame")))
310 (error "This frame is view-only. Use `C-c =' to create toc window for commands"))
313 (defun reftex-toc-show-help ()
314 "Show a summary of special key bindings."
316 (reftex-toc-dframe-p nil
'error
)
317 (with-output-to-temp-buffer "*RefTeX Help*"
318 (princ reftex-toc-help
))
319 (reftex-enlarge-to-fit "*RefTeX Help*" t
)
320 ;; If follow mode is active, arrange to delay it one command
321 (if reftex-toc-follow-mode
322 (setq reftex-toc-follow-mode
1)))
324 (defun reftex-toc-next (&optional arg
)
325 "Move to next selectable item."
327 (when (featurep 'xemacs
) (setq zmacs-region-stays t
))
328 (setq reftex-callback-fwd t
)
329 (or (eobp) (forward-char 1))
330 (goto-char (or (next-single-property-change (point) :data
)
332 (defun reftex-toc-previous (&optional arg
)
333 "Move to previous selectable item."
335 (when (featurep 'xemacs
) (setq zmacs-region-stays t
))
336 (setq reftex-callback-fwd nil
)
337 (goto-char (or (previous-single-property-change (point) :data
)
339 (defun reftex-toc-next-heading (&optional arg
)
340 "Move to next table of contentes line."
342 (when (featurep 'xemacs
) (setq zmacs-region-stays t
))
344 (re-search-forward "^ " nil t arg
)
346 (defun reftex-toc-previous-heading (&optional arg
)
347 "Move to previous table of contentes line."
349 (when (featurep 'xemacs
) (setq zmacs-region-stays t
))
350 (re-search-backward "^ " nil t arg
))
351 (defun reftex-toc-toggle-follow ()
352 "Toggle follow (other window follows with context)."
354 (setq reftex-last-follow-point -
1)
355 (setq reftex-toc-follow-mode
(not reftex-toc-follow-mode
)))
356 (defun reftex-toc-toggle-file-boundary ()
357 "Toggle inclusion of file boundaries in *toc* buffer."
359 (setq reftex-toc-include-file-boundaries
360 (not reftex-toc-include-file-boundaries
))
362 (defun reftex-toc-toggle-labels (arg)
363 "Toggle inclusion of labels in *toc* buffer.
364 With prefix ARG, prompt for a label type and include only labels of
367 (setq reftex-toc-include-labels
368 (if arg
(reftex-query-label-type)
369 (not reftex-toc-include-labels
)))
371 (defun reftex-toc-toggle-index (arg)
372 "Toggle inclusion of index in *toc* buffer.
373 With prefix arg, prompt for an index tag and include only entries of that
376 (setq reftex-toc-include-index-entries
377 (if arg
(reftex-index-select-tag)
378 (not reftex-toc-include-index-entries
)))
380 (defun reftex-toc-toggle-context ()
381 "Toggle inclusion of label context in *toc* buffer.
382 Label context is only displayed when the labels are there as well."
384 (setq reftex-toc-include-context
(not reftex-toc-include-context
))
386 (defun reftex-toc-max-level (arg)
387 "Set the maximum level of toc lines in this buffer to value of prefix ARG.
388 When no prefix is given, set the max level to a large number, so that all
389 levels are shown. For eaxample, to set the level to 3, type `3 m'."
391 (setq reftex-toc-max-level
(if arg
392 (prefix-numeric-value arg
)
394 (setq reftex-toc-max-level-indicator
395 (if arg
(int-to-string reftex-toc-max-level
) "ALL"))
397 (defun reftex-toc-view-line ()
398 "View document location in other window."
400 (reftex-toc-dframe-p nil
'error
)
401 (reftex-toc-visit-location))
402 (defun reftex-toc-goto-line-and-hide ()
403 "Go to document location in other window. Hide the *toc* window."
405 (reftex-toc-dframe-p nil
'error
)
406 (reftex-toc-visit-location 'hide
))
407 (defun reftex-toc-goto-line ()
408 "Go to document location in other window. *toc* window stays."
410 (reftex-toc-dframe-p nil
'error
)
411 (reftex-toc-visit-location t
))
412 (defun reftex-toc-mouse-goto-line-and-hide (ev)
413 "Go to document location in other window. Hide the *toc* window."
416 (reftex-toc-dframe-p nil
'error
)
417 (reftex-toc-visit-location 'hide
))
418 (defun reftex-toc-show-calling-point ()
419 "Show point where reftex-toc was called from."
421 (reftex-toc-dframe-p nil
'error
)
422 (let ((this-window (selected-window)))
425 (switch-to-buffer-other-window
426 (marker-buffer reftex-toc-return-marker
))
427 (goto-char (marker-position reftex-toc-return-marker
))
429 (select-window this-window
))))
430 (defun reftex-toc-quit ()
431 "Hide the *toc* window and do not move point.
432 If the toc window is the only window on the dedicated TOC frame, the frame
435 (if (and (one-window-p)
436 (reftex-toc-dframe-p)
437 (> (length (frame-list)) 1))
439 (or (one-window-p) (delete-window))
440 (switch-to-buffer (marker-buffer reftex-toc-return-marker
))
442 (goto-char (or (marker-position reftex-toc-return-marker
) (point)))))
443 (defun reftex-toc-quit-and-kill ()
444 "Kill the *toc* buffer."
446 (kill-buffer "*toc*")
447 (or (one-window-p) (delete-window))
448 (switch-to-buffer (marker-buffer reftex-toc-return-marker
))
450 (goto-char (marker-position reftex-toc-return-marker
)))
451 (defun reftex-toc-display-index (&optional arg
)
452 "Display the index buffer for the current document.
453 This works just like `reftex-display-index' from a LaTeX buffer.
454 With prefix arg 1, restrict index to the section at point."
456 (reftex-toc-dframe-p nil
'error
)
457 (let ((data (get-text-property (point) :data
))
458 (docstruct (symbol-value reftex-docstruct-symbol
))
461 (setq bor
(reftex-last-assoc-before-elt 'toc data docstruct
)
462 eor
(assoc 'toc
(cdr (memq bor docstruct
)))
463 restr
(list (nth 6 bor
) bor eor
)))
464 (reftex-toc-goto-line)
465 (reftex-display-index (if restr nil arg
) restr
)))
467 ;; Rescanning the document and rebuilding the TOC buffer.
468 (defun reftex-toc-rescan (&rest ignore
)
469 "Regenerate the *toc* buffer by reparsing file of section at point."
471 (if (and reftex-enable-partial-scans
472 (null current-prefix-arg
))
473 (let* ((data (get-text-property (point) :data
))
475 (file (cond ((eq what
'toc
) (nth 3 data
))
476 ((memq what
'(eof bof file-error
)) (nth 1 data
))
477 ((stringp what
) (nth 3 data
))
478 ((eq what
'index
) (nth 3 data
))))
479 (line (+ (count-lines (point-min) (point)) (if (bolp) 1 0))))
481 (error "Don't know which file to rescan. Try `C-u r'")
482 (put 'reftex-toc
:reftex-line line
)
483 (switch-to-buffer-other-window
484 (reftex-get-file-buffer-force file
))
485 (setq current-prefix-arg
'(4))
486 (let ((reftex-rebuilding-toc t
))
489 (reftex-kill-temporary-buffers))
491 (defun reftex-toc-Rescan (&rest ignore
)
492 "Regenerate the *toc* buffer by reparsing the entire document."
494 (let* ((line (+ (count-lines (point-min) (point)) (if (bolp) 1 0))))
495 (put 'reftex-toc
:reftex-line line
))
496 (switch-to-buffer-other-window
497 (reftex-get-file-buffer-force reftex-last-toc-file
))
498 (setq current-prefix-arg
'(16))
499 (let ((reftex-rebuilding-toc t
))
502 (defun reftex-toc-revert (&rest ignore
)
503 "Regenerate the *toc* from the internal lists."
506 (if (fboundp 'frame-property
)
507 (frame-property (selected-frame) 'unsplittable
)
508 (frame-parameter (selected-frame) 'unsplittable
)))
509 (reftex-rebuilding-toc t
))
512 (reftex-get-file-buffer-force reftex-last-toc-file
))
513 (switch-to-buffer-other-window
514 (reftex-get-file-buffer-force reftex-last-toc-file
))))
515 (reftex-erase-buffer "*toc*")
516 (setq current-prefix-arg nil
)
519 (defun reftex-toc-external (&rest ignore
)
520 "Switch to table of contents of an external document."
522 (reftex-toc-dframe-p nil
'error
)
523 (let* ((old-buf (current-buffer))
524 (xr-alist (get-text-property 1 'xr-alist
))
525 (xr-index (reftex-select-external-document
527 (switch-to-buffer-other-window (or (reftex-get-file-buffer-force
528 (cdr (nth xr-index xr-alist
)))
529 (error "Cannot switch document")))
531 (if (equal old-buf
(current-buffer))
533 (message "Switched document"))))
535 (defun reftex-toc-jump (arg)
536 "Jump to a specific section. E.g. '3 z' jumps to section 3.
537 Useful for large TOC's."
539 (goto-char (point-min))
541 (concat "^ *" (number-to-string (if (numberp arg
) arg
1)) " ")
545 ;; Promotion/Demotion stuff
554 (defun reftex-toc-demote (&optional arg
)
555 "Demote section at point. If region is active, apply to all in region."
557 (reftex-toc-do-promote 1))
558 (defun reftex-toc-promote (&optional arg
)
559 "Promote section at point. If region is active, apply to all in region."
561 (reftex-toc-do-promote -
1))
562 (defun reftex-toc-do-promote (delta)
563 "Workhorse for reftex-toc-promote and reftex-to-demote.
564 Changes the level of sections in the current region, or just the section at
566 ;; We should not do anything unless we are sure this is going to work for
567 ;; each section in the region. Therefore we first collect information and
569 (let* ((start-line (+ (count-lines (point-min) (point))
571 (mark-line (if (reftex-region-active-p)
572 (save-excursion (goto-char (mark))
573 (+ (count-lines (point-min) (point))
576 (pro-or-de (if (> delta
0) "de" "pro"))
577 beg end entries data sections nsec mpos msg
)
580 (if (reftex-region-active-p)
581 ;; A region is dangerous, check if we have a brandnew scan,
582 ;; to make sure we are not missing any section statements.
583 (if (not (reftex-toc-check-docstruct))
584 (reftex-toc-load-all-files-for-promotion) ;; exits
585 (setq beg
(min (point) (mark))
586 end
(max (point) (mark))))
587 (setq beg
(point) end
(point)))
589 (while (and (setq data
(get-text-property (point) :data
))
591 (if (eq (car data
) 'toc
) (push (cons data
(point)) entries
))
592 (goto-char (or (next-single-property-change (point) :data
)
594 (setq entries
(nreverse entries
))
595 ;; Get the relevant section numbers, for an informative message
596 (goto-char start-pos
)
597 (setq sections
(reftex-toc-extract-section-number (car entries
)))
598 (if (> (setq nsec
(length entries
)) 1)
601 (reftex-toc-extract-section-number
602 (nth (1- nsec
) entries
)))))
603 ;; Run through the list and prepare the changes.
604 (setq entries
(mapcar 'reftex-toc-promote-prepare entries
))
605 ;; Ask for permission
606 (if (or (not reftex-toc-confirm-promotion
) ; never confirm
607 (and (integerp reftex-toc-confirm-promotion
) ; confirm if many
608 (< nsec reftex-toc-confirm-promotion
))
610 (format "%s %d toc-entr%s (section%s %s)? "
611 (if (< delta
0) "Promote" "Demote")
613 (if (= 1 nsec
) "y" "ies")
614 (if (= 1 nsec
) "" "s")
616 nil
; we have permission, do nothing
617 (error "Abort")) ; abort, we don't have permission
619 (mapc 'reftex-toc-promote-action entries
)
620 ;; Rescan the document and rebuilt the toc buffer
621 (save-window-excursion
623 (reftex-toc-restore-region start-line mark-line
)
624 (message "%d section%s %smoted"
625 nsec
(if (= 1 nsec
) "" "s") pro-or-de
)
627 (if msg
(progn (ding) (message "%s" msg
)))))
630 (defun reftex-toc-restore-region (point-line &optional mark-line
)
632 (goto-char (point-min))
633 (forward-line (1- mark-line
))
636 (goto-char (point-min))
637 (forward-line (1- point-line
)))
641 (if (featurep 'xemacs
)
642 (zmacs-activate-region)
644 deactivate-mark nil
)))))
650 (defun reftex-toc-promote-prepare (x)
651 "Look at a toc entry and see if we could pro/demote it.
652 Expects the level change DELTA to be dynamically scoped into this function.
653 This function prepares everything for the changes, but does not do it.
654 The return value is a list with information needed when doing the
655 promotion/demotion later."
656 (let* ((data (car x
))
658 (marker (nth 4 data
))
659 (literal (nth 7 data
))
662 ;; Here follows some paranoid code to make very sure we are not
663 ;; going to break anything
665 (if (and (markerp marker
) (marker-buffer marker
))
666 ;; Buffer is still live and we have the marker.
668 (with-current-buffer (marker-buffer marker
)
669 ;; Goto the buffer and check of section is unchanged
670 (goto-char (marker-position marker
))
671 (if (looking-at (regexp-quote literal
))
672 ;; OK, get the makro name
674 (beginning-of-line 1)
675 (if (looking-at reftex-section-regexp
)
676 (setq name
(reftex-match-string 2))
677 (error "Something is wrong! Contact maintainer!")))
678 ;; Section has changed, request scan and loading
679 ;; We use a variable to delay until after the safe-exc.
680 ;; because otherwise we loose the region.
682 ;; Scan document and load all files, this exits command
683 (if load
(reftex-toc-load-all-files-for-promotion))) ; exits
684 ;; We don't have a live marker: scan and load files.
685 (reftex-toc-load-all-files-for-promotion)))
686 (level (cdr (assoc name reftex-section-levels-all
)))
687 (dummy (if (not (integerp level
))
689 (goto-char toc-point
)
690 (error "Cannot %smote special sections" pro-or-de
))))
691 ;; Delta is dynamically scoped into here...
692 (newlevel (if (>= level
0) (+ delta level
) (- level delta
)))
693 (dummy2 (if (or (and (>= level
0) (= newlevel -
1))
694 (and (< level
0) (= newlevel
0)))
695 (error "Cannot %smote \\%s" pro-or-de name
)))
696 (newname (reftex-toc-newhead-from-alist newlevel name
697 reftex-section-levels-all
)))
698 (if (and name newname
)
699 (list data name newname toc-point
)
700 (goto-char toc-point
)
701 (error "Cannot %smote \\%s" pro-or-de name
))))
703 (defun reftex-toc-promote-action (x)
704 "Change the level of a toc entry.
705 DELTA and PRO-OR-DE are assumed to be dynamically scoped into this function."
706 (let* ((data (car x
))
709 (marker (nth 4 data
)))
710 (with-current-buffer (marker-buffer marker
)
711 (goto-char (marker-position marker
))
712 (if (looking-at (concat "\\([ \t]*\\\\\\)" (regexp-quote name
)))
713 (replace-match (concat "\\1" newname
))
714 (error "Fatal error during %smotion" pro-or-de
)))))
716 (defun reftex-toc-extract-section-number (entry)
717 "Get the numbering of a toc entry, for message purposes."
718 (if (string-match "\\s-*\\(\\S-+\\)" (nth 2 (car entry
)))
719 (match-string 1 (nth 2 (car entry
)))
722 (defun reftex-toc-newhead-from-alist (nlevel head alist
)
723 "Get new heading with level NLEVEL from ALIST.
724 If there are no such entries, return nil.
725 If there are several different entries with same new level, choose the
726 one with the smallest distance to the association of HEAD in the alist.
727 This makes it possible for promotion to work several sets of headings,
728 if these sets are sorted blocks in the alist."
730 (ass (assoc head al
))
731 (pos (length (memq ass al
)))
732 dist
(mindist 1000) newhead
)
734 (if (equal (cdar al
) nlevel
)
736 (setq dist
(abs (- (length al
) pos
)))
738 (setq newhead
(car (car al
))
743 (defun reftex-toc-check-docstruct ()
744 "Check if the current docstruct is fully up to date and all files visited."
745 ;; We do this by checking if all sections are on the right position
746 (let ((docstruct (symbol-value reftex-docstruct-symbol
))
747 entry marker empoint
)
749 (while (setq entry
(pop docstruct
))
750 (if (eq (car entry
) 'toc
)
752 (setq marker
(nth 4 entry
)
753 empoint
(nth 8 entry
))
754 (if (not (and (markerp marker
)
755 (marker-buffer marker
)
756 (= (marker-position marker
) empoint
)))
757 (throw 'exit nil
)))))
760 (defun reftex-toc-load-all-files-for-promotion ()
761 "Make sure all files of the document are being visited by buffers,
762 and that the scanning info is absolutely up to date.
763 We do this by rescanning with reftex-keep-temporary-buffers bound to t.
764 The variable PRO-OR-DE is assumed to be dynamically scoped into this function.
765 When finished, we exit with an error message."
766 (let ((reftex-keep-temporary-buffers t
))
768 (reftex-toc-restore-region start-line mark-line
)
770 "TOC had to be updated first. Please check selection and repeat the command.")))
772 (defun reftex-toc-rename-label ()
773 "Rename the currently selected label in the *TOC* buffer.
774 This launches a global search and replace in order to rename a label.
775 Renaming a label is hardly ever necessary - the only exeption is after
776 promotion/demotion in connection with a package like fancyref, where the
777 label prefix determines the wording of a reference."
779 (let* ((toc (get-text-property (point) :data
))
780 (label (car toc
)) newlabel
)
781 (if (not (stringp label
))
782 (error "This is not a label entry"))
783 (setq newlabel
(read-string (format "Rename label \"%s\" to:" label
)))
784 (if (assoc newlabel
(symbol-value reftex-docstruct-symbol
))
786 (format "Label '%s' exists. Use anyway? " label
)))
789 (save-window-excursion
790 (reftex-toc-visit-location t
)
792 (reftex-query-replace-document
793 (concat "{" (regexp-quote label
) "}")
794 (format "{%s}" newlabel
))
796 (reftex-toc-rescan)))
799 (defun reftex-toc-visit-location (&optional final no-revisit
)
800 ;; Visit the tex file corresponding to the toc entry on the current line.
801 ;; If FINAL is t, stay there
802 ;; If FINAL is 'hide, hide the *toc* window.
803 ;; Otherwise, move cursor back into *toc* window.
804 ;; NO-REVISIT means don't visit files, just use live buffers.
805 ;; This function is pretty clever about finding back a section heading,
806 ;; even if the buffer is not live, or things like outline, x-symbol etc.
809 (let* ((toc (get-text-property (point) :data
))
810 (toc-window (selected-window))
811 show-window show-buffer match
)
813 (unless toc
(error "Don't know which toc line to visit"))
819 (setq match
(reftex-toc-find-section toc no-revisit
)))
821 ((eq (car toc
) 'index
)
823 (setq match
(reftex-index-show-entry toc no-revisit
)))
825 ((memq (car toc
) '(bof eof
))
828 (let ((where (car toc
))
830 (if (or (not no-revisit
) (reftex-get-buffer-visiting file
))
832 (switch-to-buffer-other-window
833 (reftex-get-file-buffer-force file nil
))
834 (goto-char (if (eq where
'bof
) (point-min) (point-max))))
835 (message "%s" reftex-no-follow-message
) nil
))))
839 (setq match
(reftex-show-label-location toc reftex-callback-fwd
842 (setq show-window
(selected-window)
843 show-buffer
(current-buffer))
846 (select-window toc-window
)
847 (error "Cannot find location"))
849 (select-window toc-window
)
851 ;; use the `final' parameter to decide what to do next
854 (reftex-unhighlight 0)
855 (select-window show-window
))
857 (reftex-unhighlight 0)
858 (or (one-window-p) (delete-window))
859 ;; If `show-window' is still live, show-buffer is already visible
860 ;; so let's not make it visible in yet-another-window.
861 (if (window-live-p show-window
)
862 (set-buffer show-buffer
)
863 (switch-to-buffer show-buffer
))
867 (defun reftex-toc-find-section (toc &optional no-revisit
)
868 (let* ((file (nth 3 toc
))
871 (literal (nth 7 toc
))
872 (emergency-point (nth 8 toc
))
875 ((and (markerp marker
) (marker-buffer marker
))
876 ;; Buffer is still live and we have the marker. Should be easy.
877 (switch-to-buffer-other-window (marker-buffer marker
))
879 (goto-char (marker-position marker
))
880 (or (looking-at (regexp-quote literal
))
881 (looking-at (reftex-make-regexp-allow-for-ctrl-m literal
))
882 (looking-at (reftex-make-desperate-section-regexp literal
))
883 (looking-at (concat "\\\\"
887 reftex-section-levels-all
)))
889 ((or (not no-revisit
)
890 (reftex-get-buffer-visiting file
))
891 ;; Marker is lost. Use the backup method.
892 (switch-to-buffer-other-window
893 (reftex-get-file-buffer-force file nil
))
894 (goto-char (or emergency-point
(point-min)))
895 (or (looking-at (regexp-quote literal
))
896 (let ((len (length literal
)))
897 (or (reftex-nearest-match (regexp-quote literal
) len
)
898 (reftex-nearest-match
899 (reftex-make-regexp-allow-for-ctrl-m literal
) len
)
900 (reftex-nearest-match
901 (reftex-make-desperate-section-regexp literal
) len
)))))
902 (t (message "%s" reftex-no-follow-message
) nil
))))
904 (goto-char (match-beginning 0))
905 (if (not (= (point) (point-max))) (recenter 1))
906 (reftex-highlight 0 (match-beginning 0) (match-end 0) (current-buffer)))
909 (defun reftex-make-desperate-section-regexp (old)
910 ;; Return a regexp which will still match a section statement even if
911 ;; x-symbol or isotex or the like have been at work in the mean time.
912 (let* ((n (1+ (string-match "[[{]" old
)))
913 (new (regexp-quote (substring old
0 (1+ (string-match "[[{]" old
)))))
914 (old (substring old n
)))
916 "\\([\r\n]\\)\\|\\(\\`\\|[ \t\n\r]\\)\\([a-zA-Z0-9]+\\)\\([ \t\n\r]\\|}\\'\\)"
918 (if (match-beginning 1)
919 (setq new
(concat new
"[^\n\r]*[\n\r]"))
920 (setq new
(concat new
"[^\n\r]*" (match-string 3 old
))))
921 (setq old
(substring old
(match-end 0))))
924 ;; Auto recentering of TOC window
926 (defun reftex-recenter-toc-when-idle ()
927 (and (> (buffer-size) 5)
929 (not (active-minibuffer-window))
930 (fboundp 'reftex-toc-mode
)
931 (get-buffer-window "*toc*" 'visible
)
932 (string= reftex-last-toc-master
(reftex-TeX-master-file))
933 (let (current-prefix-arg)
934 (reftex-toc-recenter))))
936 (defun reftex-toggle-auto-toc-recenter ()
937 "Toggle the automatic recentering of the toc window.
938 When active, leaving point idle will make the toc window jump to the correct
941 (if reftex-toc-auto-recenter-timer
943 (if (featurep 'xemacs
)
944 (delete-itimer reftex-toc-auto-recenter-timer
)
945 (cancel-timer reftex-toc-auto-recenter-timer
))
946 (setq reftex-toc-auto-recenter-timer nil
)
947 (message "Automatic recentering of toc windwo was turned off"))
948 (setq reftex-toc-auto-recenter-timer
949 (if (featurep 'xemacs
)
950 (start-itimer "RefTeX Idle Timer for recenter"
951 'reftex-recenter-toc-when-idle
952 reftex-idle-time reftex-idle-time t
)
954 reftex-idle-time t
'reftex-recenter-toc-when-idle
)))
955 (message "Automatic recentering of toc window was turned on")))
957 (defun reftex-toc-toggle-dedicated-frame ()
958 "Toggle the display of a separate frame for the TOC.
959 This frame is not used by the `reftex-toc' commands, but it is useful to
960 always show the current section in connection with the option
961 `reftex-auto-recenter-toc'."
964 (let* ((frames (frame-list)) frame
965 (get-frame-prop-func (if (fboundp 'frame-property
)
968 (while (setq frame
(pop frames
))
969 (if (equal (funcall get-frame-prop-func frame
'name
)
974 (reftex-make-separate-toc-frame))))
976 (defun reftex-make-separate-toc-frame ()
977 ;; Create a new fame showing only the toc buffer.
978 (let ((current-frame (selected-frame))
979 (current-window (selected-window))
980 (current-toc-window (get-buffer-window "*toc*" 'visible
))
982 (if (and current-toc-window
983 (not (equal (selected-frame) (window-frame current-toc-window
))))
985 (setq current-toc-frame
987 '((name .
"RefTeX TOC Frame")
988 (height .
20) (width .
60)
991 (default-toolbar-visible-p . nil
)
992 (menubar-visible-p . nil
)
993 (horizontal-scrollbar-visible-p . nil
))))
994 (select-frame current-toc-frame
)
995 (switch-to-buffer "*toc*")
996 (select-frame current-frame
)
997 (cond ((fboundp 'x-focus-frame
)
998 (x-focus-frame current-frame
))
999 ((and (featurep 'xemacs
) ; `focus-frame' is a nop in Emacs.
1000 (fboundp 'focus-frame
))
1001 (focus-frame current-frame
)))
1002 (select-window current-window
)
1003 (when (eq reftex-auto-recenter-toc
'frame
)
1004 (unless reftex-toc-auto-recenter-timer
1005 (reftex-toggle-auto-toc-recenter))
1006 (add-hook 'delete-frame-hook
'reftex-toc-delete-frame-hook
)))))
1008 (defun reftex-toc-delete-frame-hook (frame)
1009 (if (and reftex-toc-auto-recenter-timer
1010 (reftex-toc-dframe-p frame
))
1012 (reftex-toggle-auto-toc-recenter))))
1014 ;; Table of Contents map
1015 (define-key reftex-toc-map
(if (featurep 'xemacs
) [(button2)] [(mouse-2)])
1016 'reftex-toc-mouse-goto-line-and-hide
)
1017 (define-key reftex-toc-map
[follow-link
] 'mouse-face
)
1019 (substitute-key-definition
1020 'next-line
'reftex-toc-next reftex-toc-map global-map
)
1021 (substitute-key-definition
1022 'previous-line
'reftex-toc-previous reftex-toc-map global-map
)
1025 '(("n" . reftex-toc-next
)
1026 ("p" . reftex-toc-previous
)
1027 ("?" . reftex-toc-show-help
)
1028 (" " . reftex-toc-view-line
)
1029 ("\C-m" . reftex-toc-goto-line-and-hide
)
1030 ("\C-i" . reftex-toc-goto-line
)
1031 ("\C-c>" . reftex-toc-display-index
)
1032 ("r" . reftex-toc-rescan
)
1033 ("R" . reftex-toc-Rescan
)
1034 ("g" . revert-buffer
)
1035 ("q" . reftex-toc-quit
);
1036 ("k" . reftex-toc-quit-and-kill
)
1037 ("f" . reftex-toc-toggle-follow
);
1038 ("a" . reftex-toggle-auto-toc-recenter
)
1039 ("d" . reftex-toc-toggle-dedicated-frame
)
1040 ("F" . reftex-toc-toggle-file-boundary
)
1041 ("i" . reftex-toc-toggle-index
)
1042 ("l" . reftex-toc-toggle-labels
)
1043 ("t" . reftex-toc-max-level
)
1044 ("c" . reftex-toc-toggle-context
)
1045 ; ("%" . reftex-toc-toggle-commented)
1046 ("\M-%" . reftex-toc-rename-label
)
1047 ("x" . reftex-toc-external
)
1048 ("z" . reftex-toc-jump
)
1049 ("." . reftex-toc-show-calling-point
)
1050 ("\C-c\C-n" . reftex-toc-next-heading
)
1051 ("\C-c\C-p" . reftex-toc-previous-heading
)
1052 (">" . reftex-toc-demote
)
1053 ("<" . reftex-toc-promote
))
1054 do
(define-key reftex-toc-map
(car x
) (cdr x
)))
1056 (loop for key across
"0123456789" do
1057 (define-key reftex-toc-map
(vector (list key
)) 'digit-argument
))
1058 (define-key reftex-toc-map
"-" 'negative-argument
)
1061 reftex-toc-menu reftex-toc-map
1062 "Menu for Table of Contents buffer"
1064 ["Show Location" reftex-toc-view-line t
]
1065 ["Go To Location" reftex-toc-goto-line t
]
1066 ["Exit & Go To Location" reftex-toc-goto-line-and-hide t
]
1067 ["Show Calling Point" reftex-toc-show-calling-point t
]
1068 ["Quit" reftex-toc-quit t
]
1071 ["Promote" reftex-toc-promote t
]
1072 ["Demote" reftex-toc-demote t
]
1073 ["Rename Label" reftex-toc-rename-label t
])
1075 ["Index" reftex-toc-display-index t
]
1076 ["External Document TOC " reftex-toc-external t
]
1079 ["Rebuilt *toc* Buffer" revert-buffer t
]
1080 ["Rescan One File" reftex-toc-rescan reftex-enable-partial-scans
]
1081 ["Rescan Entire Document" reftex-toc-Rescan t
])
1084 ["File Boundaries" reftex-toc-toggle-file-boundary
:style toggle
1085 :selected reftex-toc-include-file-boundaries
]
1086 ["Labels" reftex-toc-toggle-labels
:style toggle
1087 :selected reftex-toc-include-labels
]
1088 ["Index Entries" reftex-toc-toggle-index
:style toggle
1089 :selected reftex-toc-include-index-entries
]
1090 ["Context" reftex-toc-toggle-context
:style toggle
1091 :selected reftex-toc-include-context
]
1093 ["Follow Mode" reftex-toc-toggle-follow
:style toggle
1094 :selected reftex-toc-follow-mode
]
1095 ["Auto Recenter" reftex-toggle-auto-toc-recenter
:style toggle
1096 :selected reftex-toc-auto-recenter-timer
]
1097 ["Dedicated Frame" reftex-toc-toggle-dedicated-frame t
])
1099 ["Help" reftex-toc-show-help t
]))
1102 ;; arch-tag: 92400ce2-0b86-4c89-a606-4ed71acea17e
1103 ;;; reftex-toc.el ends here