Merge from emacs-24; up to 2014-05-29T17:16:00Z!dmantipov@yandex.ru
[bpt/emacs.git] / lisp / textmodes / reftex-toc.el
CommitLineData
3afbc435 1;;; reftex-toc.el --- RefTeX's table of contents mode
e6ce8c42 2
ba318903 3;; Copyright (C) 1997-2000, 2003-2014 Free Software Foundation, Inc.
3ba2590f 4
6fbeb429 5;; Author: Carsten Dominik <dominik@science.uva.nl>
ce545621 6;; Maintainer: auctex-devel@gnu.org
3ba2590f
RS
7
8;; This file is part of GNU Emacs.
9
1fecc8fe 10;; GNU Emacs is free software: you can redistribute it and/or modify
3ba2590f 11;; it under the terms of the GNU General Public License as published by
1fecc8fe
GM
12;; the Free Software Foundation, either version 3 of the License, or
13;; (at your option) any later version.
3ba2590f
RS
14
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.
19
20;; You should have received a copy of the GNU General Public License
1fecc8fe 21;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
1a9461d0 22
3afbc435
PJ
23;;; Commentary:
24
25;;; Code:
26
7c4d13cc 27(eval-when-compile (require 'cl))
1a9461d0
CD
28(provide 'reftex-toc)
29(require 'reftex)
30;;;
31
e5bd0a28 32(define-obsolete-variable-alias 'reftex-toc-map 'reftex-toc-mode-map "24.1")
4d789d84
SM
33(defvar reftex-toc-mode-map
34 (let ((map (make-sparse-keymap)))
91af3942 35
4d789d84
SM
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)
39
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)
44
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)))
76
77 (loop for key across "0123456789" do
78 (define-key map (vector (list key)) 'digit-argument))
79 (define-key map "-" 'negative-argument)
80
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]))
121
122 map)
1a9461d0
CD
123 "Keymap used for *toc* buffer.")
124
125(defvar reftex-toc-menu)
7b07114a
CD
126(defvar reftex-last-window-height nil)
127(defvar reftex-last-window-width nil)
85a387c4
JB
128(defvar reftex-toc-include-labels-indicator nil)
129(defvar reftex-toc-include-index-indicator nil)
130(defvar reftex-toc-max-level-indicator nil)
1a9461d0 131
4d789d84 132(define-derived-mode reftex-toc-mode fundamental-mode "TOC"
1a9461d0
CD
133 "Major mode for managing Table of Contents for LaTeX files.
134This buffer was created with RefTeX.
135Press `?' for a summary of important key bindings.
136
137Here are all local bindings.
138
4d789d84 139\\{reftex-toc-mode-map}"
3666daf6 140 (set (make-local-variable 'transient-mark-mode) t)
0c86715d
DN
141 (when (featurep 'xemacs)
142 (set (make-local-variable 'zmacs-regions) t))
1a9461d0
CD
143 (set (make-local-variable 'revert-buffer-function) 'reftex-toc-revert)
144 (set (make-local-variable 'reftex-toc-include-labels-indicator) "")
7c4d13cc
CD
145 (set (make-local-variable 'reftex-toc-max-level-indicator)
146 (if (= reftex-toc-max-level 100)
3666daf6
CD
147 "ALL"
148 (int-to-string reftex-toc-max-level)))
1a9461d0 149 (setq mode-line-format
3666daf6
CD
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 " -%-"))
1a9461d0 156 (setq truncate-lines t)
7ac7387b
CD
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))
1a9461d0
CD
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)
4d789d84 164 (easy-menu-add reftex-toc-menu reftex-toc-mode-map))
1a9461d0
CD
165
166(defvar reftex-last-toc-file nil
167 "Stores the file name from which `reftex-toc' was called. For redo command.")
168
1a9461d0
CD
169
170(defvar reftex-toc-return-marker (make-marker)
df85d315 171 "Marker which makes it possible to return from TOC to old position.")
1a9461d0
CD
172
173(defconst reftex-toc-help
174" AVAILABLE KEYS IN TOC BUFFER
175 ============================
176n / p next-line / previous-line
177SPC Show the corresponding location of the LaTeX document.
df85d315
JB
178TAB Goto the location and keep the TOC window.
179RET Goto the location and hide the TOC window (also on mouse-2).
3666daf6 180< / > Promote / Demote section, or all sections in region.
1a9461d0
CD
181C-c > Display Index. With prefix arg, restrict index to current section.
182q / k Hide/Kill *toc* buffer, return to position of reftex-toc command.
183l i c F Toggle display of [l]abels, [i]ndex, [c]ontext, [F]ile borders.
7c4d13cc 184t Change maximum toc depth (e.g. `3 t' hides levels greater than 3).
3666daf6
CD
185f / g Toggle follow mode / Refresh *toc* buffer.
186a / d Toggle auto recenter / Toggle dedicated frame
1a9461d0
CD
187r / C-u r Reparse the LaTeX document / Reparse entire LaTeX document.
188. In other window, show position from where `reftex-toc' was called.
3666daf6 189M-% Global search and replace to rename label at point.
70d797cd 190x Switch to TOC of external document (with LaTeX package `xr').
3666daf6 191z Jump to a specific section (e.g. '3 z' goes to section 3).")
1a9461d0 192
41116c5a 193;;;###autoload
3666daf6 194(defun reftex-toc (&optional rebuild reuse)
1a9461d0
CD
195 "Show the table of contents for the current document.
196When called with a raw C-u prefix, rescan the document first."
197
3666daf6
CD
198;; The REUSE argument means, search all visible frames for a window
199;; displaying the toc window. If yes, reuse this window.
200
1a9461d0
CD
201 (interactive)
202
203 (if (or (not (string= reftex-last-toc-master (reftex-TeX-master-file)))
204 current-prefix-arg)
205 (reftex-erase-buffer "*toc*"))
206
207 (setq reftex-last-toc-file (buffer-file-name))
208 (setq reftex-last-toc-master (reftex-TeX-master-file))
209
210 (set-marker reftex-toc-return-marker (point))
211
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))
215
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))
220
221 ;; Ensure access to scanning info and rescan buffer if prefix are is '(4)
222 (reftex-access-scan-info current-prefix-arg)
223
224 (let* ((this-buf (current-buffer))
3666daf6
CD
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)
3b734d1c 233 (frame-parameter nil 'unsplittable)))
3666daf6
CD
234 offset toc-window)
235
e6ce8c42 236 (if (setq toc-window (get-buffer-window
3666daf6
CD
237 "*toc*"
238 (if reuse 'visible)))
239 (select-window toc-window)
1a9461d0 240 (when (or (not reftex-toc-keep-other-windows)
3666daf6
CD
241 (< (window-height) (* 2 window-min-height)))
242 (delete-other-windows))
6fbeb429 243
0de7d982 244 (setq reftex-last-window-width (window-total-width)
3666daf6
CD
245 reftex-last-window-height (window-height)) ; remember
246
247 (unless unsplittable
248 (if reftex-toc-split-windows-horizontally
2d197ffb 249 (split-window-right
0de7d982 250 (floor (* (window-total-width)
3666daf6 251 reftex-toc-split-windows-fraction)))
2d197ffb 252 (split-window-below
3666daf6
CD
253 (floor (* (window-height)
254 reftex-toc-split-windows-fraction)))))
6fbeb429 255
d63eb0e7 256 (switch-to-buffer "*toc*"))
1a9461d0
CD
257
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
3666daf6
CD
261 (if (eq reftex-toc-include-labels t)
262 "ALL"
263 reftex-toc-include-labels))
1a9461d0 264 (setq reftex-toc-include-index-indicator
3666daf6
CD
265 (if (eq reftex-toc-include-index-entries t)
266 "ALL"
267 reftex-toc-include-index-entries))
1a9461d0
CD
268
269 (cond
270 ((= (buffer-size) 0)
271 ;; buffer is empty - fill it with the table of contents
272 (message "Building *toc* buffer...")
273
274 (setq buffer-read-only nil)
275 (insert (format
276"TABLE-OF-CONTENTS on %s
277SPC=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)))
280
281 (if (reftex-use-fonts)
4a9699d8
SM
282 (put-text-property (point-min) (point) '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)
1a9461d0
CD
285
286 (setq offset
3666daf6
CD
287 (reftex-insert-docstruct
288 this-buf
df85d315 289 t ; include TOC
3666daf6
CD
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
e6ce8c42 296 here-I-am
3666daf6 297 "" ; xr-prefix
df85d315 298 t ; a TOC buffer
3666daf6 299 ))
e6ce8c42 300
1a9461d0
CD
301 (run-hooks 'reftex-display-copied-context-hook)
302 (message "Building *toc* buffer...done.")
303 (setq buffer-read-only t))
304 (t
305 ;; Only compute the offset
306 (setq offset
3666daf6
CD
307 (or (reftex-get-offset this-buf here-I-am
308 (if reftex-toc-include-labels " " nil)
309 t
310 reftex-toc-include-index-entries
311 reftex-toc-include-file-boundaries)
e6ce8c42 312 (reftex-last-assoc-before-elt
3666daf6
CD
313 'toc here-I-am
314 (symbol-value reftex-docstruct-symbol))))
1a9461d0 315 (put 'reftex-toc :reftex-line 3)
e6ce8c42
GM
316 (goto-char (point-min))
317 (forward-line 2)))
1a9461d0
CD
318
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))))
322
41116c5a 323;;;###autoload
3b919c9f
CD
324(defun reftex-toc-recenter (&optional arg)
325 "Display the TOC window and highlight line corresponding to current position."
326 (interactive "P")
3666daf6
CD
327 (let ((buf (current-buffer))
328 (frame (selected-frame)))
329 (reftex-toc arg t)
3b919c9f 330 (if (= (count-lines 1 (point)) 2)
3666daf6
CD
331 (let ((current-prefix-arg nil))
332 (select-window (get-buffer-window buf frame))
333 (reftex-toc nil t)))
3b919c9f 334 (and (> (point) 1)
3666daf6
CD
335 (not (get-text-property (point) 'intangible))
336 (memq reftex-highlight-selection '(cursor both))
337 (reftex-highlight 2
e6ce8c42 338 (or (previous-single-property-change
3666daf6
CD
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))))
3b919c9f 344
1a9461d0
CD
345(defun reftex-toc-pre-command-hook ()
346 ;; used as pre command hook in *toc* buffer
347 (reftex-unhighlight 0)
3b919c9f 348 )
1a9461d0
CD
349
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)
3666daf6
CD
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)))))
1a9461d0
CD
362 (if (integerp reftex-toc-follow-mode)
363 ;; remove delayed action
364 (setq reftex-toc-follow-mode t)
3666daf6
CD
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)))))
1a9461d0
CD
373
374(defun reftex-re-enlarge ()
4f595e15
RA
375 "Enlarge window to a remembered size."
376 (let ((count (if reftex-toc-split-windows-horizontally
0de7d982
JK
377 (- (or reftex-last-window-width (window-total-width))
378 (window-total-width))
4f595e15
RA
379 (- (or reftex-last-window-height (window-height))
380 (window-height)))))
381 (when (> count 0)
382 (enlarge-window count reftex-toc-split-windows-horizontally))))
3666daf6
CD
383
384(defun reftex-toc-dframe-p (&optional frame error)
e6ce8c42 385 ;; Check if FRAME is the dedicated TOC frame.
3666daf6
CD
386 ;; If yes, and ERROR is non-nil, throw an error.
387 (setq frame (or frame (selected-frame)))
e6ce8c42 388 (let ((res (equal
3666daf6
CD
389 (if (fboundp 'frame-property)
390 (frame-property frame 'name)
391 (frame-parameter frame 'name))
392 "RefTeX TOC Frame")))
393 (if (and res error)
df85d315 394 (error "This frame is view-only. Use `C-c =' to create TOC window for commands"))
3666daf6 395 res))
1a9461d0
CD
396
397(defun reftex-toc-show-help ()
398 "Show a summary of special key bindings."
399 (interactive)
3666daf6 400 (reftex-toc-dframe-p nil 'error)
1a9461d0
CD
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)))
407
408(defun reftex-toc-next (&optional arg)
409 "Move to next selectable item."
410 (interactive "p")
a445370f 411 (when (featurep 'xemacs) (setq zmacs-region-stays t))
1a9461d0
CD
412 (setq reftex-callback-fwd t)
413 (or (eobp) (forward-char 1))
e6ce8c42 414 (goto-char (or (next-single-property-change (point) :data)
3666daf6 415 (point))))
1a9461d0
CD
416(defun reftex-toc-previous (&optional arg)
417 "Move to previous selectable item."
418 (interactive "p")
a445370f 419 (when (featurep 'xemacs) (setq zmacs-region-stays t))
1a9461d0
CD
420 (setq reftex-callback-fwd nil)
421 (goto-char (or (previous-single-property-change (point) :data)
3666daf6 422 (point))))
1a9461d0 423(defun reftex-toc-next-heading (&optional arg)
df85d315 424 "Move to next table of contents line."
1a9461d0 425 (interactive "p")
a445370f 426 (when (featurep 'xemacs) (setq zmacs-region-stays t))
1a9461d0
CD
427 (end-of-line)
428 (re-search-forward "^ " nil t arg)
429 (beginning-of-line))
430(defun reftex-toc-previous-heading (&optional arg)
df85d315 431 "Move to previous table of contents line."
1a9461d0 432 (interactive "p")
a445370f 433 (when (featurep 'xemacs) (setq zmacs-region-stays t))
1a9461d0
CD
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
3666daf6 444 (not reftex-toc-include-file-boundaries))
1a9461d0
CD
445 (reftex-toc-revert))
446(defun reftex-toc-toggle-labels (arg)
447 "Toggle inclusion of labels in *toc* buffer.
448With prefix ARG, prompt for a label type and include only labels of
449that specific type."
450 (interactive "P")
e6ce8c42 451 (setq reftex-toc-include-labels
3666daf6
CD
452 (if arg (reftex-query-label-type)
453 (not reftex-toc-include-labels)))
1a9461d0
CD
454 (reftex-toc-revert))
455(defun reftex-toc-toggle-index (arg)
456 "Toggle inclusion of index in *toc* buffer.
457With prefix arg, prompt for an index tag and include only entries of that
458specific index."
459 (interactive "P")
460 (setq reftex-toc-include-index-entries
3666daf6
CD
461 (if arg (reftex-index-select-tag)
462 (not reftex-toc-include-index-entries)))
1a9461d0
CD
463 (reftex-toc-revert))
464(defun reftex-toc-toggle-context ()
465 "Toggle inclusion of label context in *toc* buffer.
466Label 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))
7c4d13cc 470(defun reftex-toc-max-level (arg)
df85d315 471 "Set the maximum level of TOC lines in this buffer to value of prefix ARG.
7c4d13cc 472When no prefix is given, set the max level to a large number, so that all
4c36be58 473levels are shown. For example, to set the level to 3, type `3 m'."
7c4d13cc
CD
474 (interactive "P")
475 (setq reftex-toc-max-level (if arg
3666daf6
CD
476 (prefix-numeric-value arg)
477 100))
7c4d13cc 478 (setq reftex-toc-max-level-indicator
3666daf6 479 (if arg (int-to-string reftex-toc-max-level) "ALL"))
7c4d13cc 480 (reftex-toc-revert))
1a9461d0
CD
481(defun reftex-toc-view-line ()
482 "View document location in other window."
483 (interactive)
3666daf6 484 (reftex-toc-dframe-p nil 'error)
1a9461d0
CD
485 (reftex-toc-visit-location))
486(defun reftex-toc-goto-line-and-hide ()
df85d315 487 "Go to document location in other window. Hide the TOC window."
1a9461d0 488 (interactive)
3666daf6 489 (reftex-toc-dframe-p nil 'error)
1a9461d0
CD
490 (reftex-toc-visit-location 'hide))
491(defun reftex-toc-goto-line ()
df85d315 492 "Go to document location in other window. TOC window stays."
1a9461d0 493 (interactive)
3666daf6 494 (reftex-toc-dframe-p nil 'error)
1a9461d0
CD
495 (reftex-toc-visit-location t))
496(defun reftex-toc-mouse-goto-line-and-hide (ev)
df85d315 497 "Go to document location in other window. Hide the TOC window."
1a9461d0
CD
498 (interactive "e")
499 (mouse-set-point ev)
3666daf6 500 (reftex-toc-dframe-p nil 'error)
1a9461d0
CD
501 (reftex-toc-visit-location 'hide))
502(defun reftex-toc-show-calling-point ()
df85d315 503 "Show point where `reftex-toc' was called from."
1a9461d0 504 (interactive)
3666daf6 505 (reftex-toc-dframe-p nil 'error)
1a9461d0
CD
506 (let ((this-window (selected-window)))
507 (unwind-protect
3666daf6
CD
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)))
1a9461d0
CD
513 (select-window this-window))))
514(defun reftex-toc-quit ()
df85d315
JB
515 "Hide the TOC window and do not move point.
516If the TOC window is the only window on the dedicated TOC frame, the frame
3666daf6 517is destroyed."
1a9461d0 518 (interactive)
3666daf6
CD
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)))))
1a9461d0
CD
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.
537This works just like `reftex-display-index' from a LaTeX buffer.
538With prefix arg 1, restrict index to the section at point."
539 (interactive "P")
3666daf6 540 (reftex-toc-dframe-p nil 'error)
1a9461d0 541 (let ((data (get-text-property (point) :data))
3666daf6
CD
542 (docstruct (symbol-value reftex-docstruct-symbol))
543 bor eor restr)
1a9461d0
CD
544 (when (equal arg 2)
545 (setq bor (reftex-last-assoc-before-elt 'toc data docstruct)
3666daf6
CD
546 eor (assoc 'toc (cdr (memq bor docstruct)))
547 restr (list (nth 6 bor) bor eor)))
1a9461d0
CD
548 (reftex-toc-goto-line)
549 (reftex-display-index (if restr nil arg) restr)))
3666daf6
CD
550
551;; Rescanning the document and rebuilding the TOC buffer.
1a9461d0
CD
552(defun reftex-toc-rescan (&rest ignore)
553 "Regenerate the *toc* buffer by reparsing file of section at point."
554 (interactive)
e6ce8c42 555 (if (and reftex-enable-partial-scans
3666daf6 556 (null current-prefix-arg))
1a9461d0 557 (let* ((data (get-text-property (point) :data))
3666daf6
CD
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))))
1a9461d0
CD
564 (if (not file)
565 (error "Don't know which file to rescan. Try `C-u r'")
3666daf6 566 (put 'reftex-toc :reftex-line line)
1a9461d0
CD
567 (switch-to-buffer-other-window
568 (reftex-get-file-buffer-force file))
3666daf6
CD
569 (setq current-prefix-arg '(4))
570 (let ((reftex-rebuilding-toc t))
571 (reftex-toc))))
1a9461d0
CD
572 (reftex-toc-Rescan))
573 (reftex-kill-temporary-buffers))
3666daf6 574
1a9461d0
CD
575(defun reftex-toc-Rescan (&rest ignore)
576 "Regenerate the *toc* buffer by reparsing the entire document."
577 (interactive)
3b919c9f
CD
578 (let* ((line (+ (count-lines (point-min) (point)) (if (bolp) 1 0))))
579 (put 'reftex-toc :reftex-line line))
1a9461d0
CD
580 (switch-to-buffer-other-window
581 (reftex-get-file-buffer-force reftex-last-toc-file))
582 (setq current-prefix-arg '(16))
3b919c9f
CD
583 (let ((reftex-rebuilding-toc t))
584 (reftex-toc)))
3666daf6 585
1a9461d0 586(defun reftex-toc-revert (&rest ignore)
df85d315 587 "Regenerate the TOC from the internal lists."
1a9461d0 588 (interactive)
e6ce8c42 589 (let ((unsplittable
3666daf6
CD
590 (if (fboundp 'frame-property)
591 (frame-property (selected-frame) 'unsplittable)
3b734d1c 592 (frame-parameter nil 'unsplittable)))
3666daf6
CD
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))))
1a9461d0
CD
599 (reftex-erase-buffer "*toc*")
600 (setq current-prefix-arg nil)
3666daf6
CD
601 (reftex-toc t))
602
1a9461d0
CD
603(defun reftex-toc-external (&rest ignore)
604 "Switch to table of contents of an external document."
605 (interactive)
3666daf6 606 (reftex-toc-dframe-p nil 'error)
1a9461d0 607 (let* ((old-buf (current-buffer))
3666daf6
CD
608 (xr-alist (get-text-property 1 'xr-alist))
609 (xr-index (reftex-select-external-document
610 xr-alist 0)))
1a9461d0 611 (switch-to-buffer-other-window (or (reftex-get-file-buffer-force
3666daf6
CD
612 (cdr (nth xr-index xr-alist)))
613 (error "Cannot switch document")))
1a9461d0
CD
614 (reftex-toc)
615 (if (equal old-buf (current-buffer))
3666daf6 616 (message "")
1a9461d0
CD
617 (message "Switched document"))))
618
70d797cd
CD
619(defun reftex-toc-jump (arg)
620 "Jump to a specific section. E.g. '3 z' jumps to section 3.
df85d315 621Useful for large TOCs."
70d797cd
CD
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))
628
3666daf6
CD
629;; Promotion/Demotion stuff
630
7b07114a
CD
631(defvar pro-or-de)
632(defvar start-pos)
633(defvar start-line)
634(defvar mark-line)
635
3666daf6
CD
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)
df85d315 645 "Workhorse for `reftex-toc-promote' and `reftex-toc-demote'.
3666daf6
CD
646Changes the level of sections in the current region, or just the section at
647point."
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"))
728618ba 659 beg end entries data sections nsec msg)
3666daf6
CD
660 (setq msg
661 (catch 'exit
662 (if (reftex-region-active-p)
91af3942 663 ;; A region is dangerous, check if we have a brand new scan,
3666daf6
CD
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)
e6ce8c42 681 (setq sections
3666daf6
CD
682 (concat sections "-"
683 (reftex-toc-extract-section-number
684 (nth (1- nsec) entries)))))
685 ;; Run through the list and prepare the changes.
c8f3bf36
GM
686 (setq entries (mapcar
687 (lambda (e) (reftex-toc-promote-prepare e delta))
688 entries))
3666daf6
CD
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
17d10974 703 (mapc 'reftex-toc-promote-action entries)
3666daf6
CD
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)
e6ce8c42 708 (message "%d section%s %smoted"
3666daf6
CD
709 nsec (if (= 1 nsec) "" "s") pro-or-de)
710 nil))
274f1353 711 (if msg (progn (ding) (message "%s" msg)))))
3666daf6 712
3666daf6
CD
713
714(defun reftex-toc-restore-region (point-line &optional mark-line)
728618ba
GM
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)))))
3666daf6 729
c8f3bf36 730(defun reftex-toc-promote-prepare (x delta)
df85d315
JB
731 "Look at a TOC entry and see if we could pro/demote it.
732This function prepares everything for the change, but does not do it.
3666daf6 733The return value is a list with information needed when doing the
c8f3bf36 734promotion/demotion later. DELTA is the level change."
3666daf6
CD
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
de3a1fe9 747 (with-current-buffer (marker-buffer marker)
3666daf6 748 ;; Goto the buffer and check of section is unchanged
3666daf6
CD
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))
5a0c3f56 756 (error "Something is wrong! Contact maintainer!")))
3666daf6
CD
757 ;; Section has changed, request scan and loading
758 ;; We use a variable to delay until after the safe-exc.
537b04b9 759 ;; because otherwise we lose the region.
3666daf6
CD
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))))
3666daf6
CD
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))))
780
781(defun reftex-toc-promote-action (x)
df85d315 782 "Change the level of a TOC entry.
c8f3bf36 783PRO-OR-DE is assumed to be dynamically scoped into this function."
3666daf6
CD
784 (let* ((data (car x))
785 (name (nth 1 x))
786 (newname (nth 2 x))
787 (marker (nth 4 data)))
9a529312 788 (with-current-buffer (marker-buffer marker)
3666daf6 789 (goto-char (marker-position marker))
e2cb57f5 790 (if (looking-at (concat "\\([ \t]*" reftex-section-pre-regexp "\\)" (regexp-quote name)))
3666daf6
CD
791 (replace-match (concat "\\1" newname))
792 (error "Fatal error during %smotion" pro-or-de)))))
793
794(defun reftex-toc-extract-section-number (entry)
df85d315 795 "Get the numbering of a TOC entry, for message purposes."
3666daf6
CD
796 (if (string-match "\\s-*\\(\\S-+\\)" (nth 2 (car entry)))
797 (match-string 1 (nth 2 (car entry)))
798 "?"))
799
800(defun reftex-toc-newhead-from-alist (nlevel head alist)
801 "Get new heading with level NLEVEL from ALIST.
802If there are no such entries, return nil.
fa5f7c5f
JB
803If there are several different entries with same new level, choose the
804one with the smallest distance to the association of HEAD in the alist.
3666daf6
CD
805This makes it possible for promotion to work several sets of headings,
806if 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))
820
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)))
837
838(defun reftex-toc-load-all-files-for-promotion ()
839 "Make sure all files of the document are being visited by buffers,
840and that the scanning info is absolutely up to date.
df85d315 841We do this by rescanning with `reftex-keep-temporary-buffers' bound to t.
42187e99 842The variable PRO-OR-DE is assumed to be dynamically scoped into this function.
3666daf6
CD
843When 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
42187e99 848 "TOC had to be updated first. Please check selection and repeat the command.")))
3666daf6
CD
849
850(defun reftex-toc-rename-label ()
df85d315 851 "Rename the currently selected label in the *toc* buffer.
3666daf6 852This launches a global search and replace in order to rename a label.
4c36be58 853Renaming a label is hardly ever necessary - the only exception is after
3666daf6
CD
854promotion/demotion in connection with a package like fancyref, where the
855label 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))
5a0c3f56 860 (error "This is not a label entry"))
3666daf6
CD
861 (setq newlabel (read-string (format "Rename label \"%s\" to:" label)))
862 (if (assoc newlabel (symbol-value reftex-docstruct-symbol))
e6ce8c42 863 (if (not (y-or-n-p
df85d315 864 (format "Label '%s' exists. Use anyway? " label)))
3666daf6
CD
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))
e6ce8c42 873 (error t))))
3666daf6
CD
874 (reftex-toc-rescan)))
875
876
1a9461d0 877(defun reftex-toc-visit-location (&optional final no-revisit)
df85d315 878 ;; Visit the tex file corresponding to the TOC entry on the current line.
1a9461d0 879 ;; If FINAL is t, stay there
df85d315
JB
880 ;; If FINAL is 'hide, hide the TOC window.
881 ;; Otherwise, move cursor back into TOC window.
4a9699d8 882 ;; NO-REVISIT means don't visit files, just use live buffers.
1a9461d0
CD
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.
886
887 (let* ((toc (get-text-property (point) :data))
888 (toc-window (selected-window))
889 show-window show-buffer match)
890
df85d315 891 (unless toc (error "Don't know which TOC line to visit"))
e6ce8c42 892
1a9461d0 893 (cond
e6ce8c42 894
1a9461d0
CD
895 ((eq (car toc) 'toc)
896 ;; a toc entry
897 (setq match (reftex-toc-find-section toc no-revisit)))
898
899 ((eq (car toc) 'index)
900 ;; an index entry
901 (setq match (reftex-index-show-entry toc no-revisit)))
902
903 ((memq (car toc) '(bof eof))
904 ;; A file entry
905 (setq match
3666daf6
CD
906 (let ((where (car toc))
907 (file (nth 1 toc)))
908 (if (or (not no-revisit) (reftex-get-buffer-visiting file))
909 (progn
e6ce8c42 910 (switch-to-buffer-other-window
3666daf6
CD
911 (reftex-get-file-buffer-force file nil))
912 (goto-char (if (eq where 'bof) (point-min) (point-max))))
274f1353 913 (message "%s" reftex-no-follow-message) nil))))
1a9461d0
CD
914
915 ((stringp (car toc))
916 ;; a label
917 (setq match (reftex-show-label-location toc reftex-callback-fwd
3666daf6 918 no-revisit t))))
1a9461d0
CD
919
920 (setq show-window (selected-window)
921 show-buffer (current-buffer))
922
923 (unless match
924 (select-window toc-window)
925 (error "Cannot find location"))
926
927 (select-window toc-window)
928
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))
4a9699d8
SM
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)
3666daf6 940 (set-buffer show-buffer)
4a9699d8 941 (switch-to-buffer show-buffer))
1a9461d0
CD
942 (reftex-re-enlarge))
943 (t nil))))
944
945(defun reftex-toc-find-section (toc &optional no-revisit)
946 (let* ((file (nth 3 toc))
3666daf6
CD
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))
f3c18bd0 956 (push-mark nil)
3666daf6
CD
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
e6ce8c42
GM
963 (car
964 (rassq level
3666daf6
CD
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)))))
274f1353 980 (t (message "%s" reftex-no-follow-message) nil))))
1a9461d0
CD
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))
986
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))
1001
3666daf6 1002;; Auto recentering of TOC window
3b919c9f
CD
1003
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)
3666daf6 1009 (get-buffer-window "*toc*" 'visible)
3b919c9f 1010 (string= reftex-last-toc-master (reftex-TeX-master-file))
3666daf6
CD
1011 (let (current-prefix-arg)
1012 (reftex-toc-recenter))))
3b919c9f 1013
41116c5a 1014;;;###autoload
3b919c9f 1015(defun reftex-toggle-auto-toc-recenter ()
df85d315
JB
1016 "Toggle the automatic recentering of the TOC window.
1017When active, leaving point idle will make the TOC window jump to the correct
3b919c9f
CD
1018section."
1019 (interactive)
1020 (if reftex-toc-auto-recenter-timer
1021 (progn
3666daf6
CD
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)
df85d315 1026 (message "Automatic recentering of TOC window was turned off"))
3b919c9f 1027 (setq reftex-toc-auto-recenter-timer
3666daf6
CD
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)))
df85d315 1034 (message "Automatic recentering of TOC window was turned on")))
3b919c9f 1035
3666daf6
CD
1036(defun reftex-toc-toggle-dedicated-frame ()
1037 "Toggle the display of a separate frame for the TOC.
1038This frame is not used by the `reftex-toc' commands, but it is useful to
1039always 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))))
1054
1055(defun reftex-make-separate-toc-frame ()
df85d315 1056 ;; Create a new fame showing only the TOC buffer.
3666daf6
CD
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))))
1063 nil
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)
df17e41e
GM
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)))
3666daf6
CD
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)))))
1086
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))))
3b919c9f 1092
f3c18bd0 1093;;; reftex-toc.el ends here
41116c5a
GM
1094
1095;; Local Variables:
1096;; generated-autoload-file: "reftex.el"
1097;; End: