comparison lisp/textmodes/reftex-toc.el @ 52170:27340819ef07

Updated to version 4.21
author Carsten Dominik <dominik@science.uva.nl>
date Mon, 11 Aug 2003 12:27:02 +0000
parents 85e525daebfc
children 695cf19ef79e
comparison
equal deleted inserted replaced
52169:b97eb548bcaa 52170:27340819ef07
1 ;;; reftex-toc.el --- RefTeX's table of contents mode 1 ;;; reftex-toc.el --- RefTeX's table of contents mode
2 ;; Copyright (c) 1997, 1998, 1999, 2000, 2003 Free Software Foundation, Inc. 2 ;; Copyright (c) 1997, 1998, 1999, 2000, 2003 Free Software Foundation, Inc.
3 3
4 ;; Author: Carsten Dominik <dominik@science.uva.nl> 4 ;; Author: Carsten Dominik <dominik@science.uva.nl>
5 ;; Version: 4.18 5 ;; Version: 4.21
6 6
7 ;; This file is part of GNU Emacs. 7 ;; This file is part of GNU Emacs.
8 8
9 ;; GNU Emacs is free software; you can redistribute it and/or modify 9 ;; GNU Emacs is free software; you can redistribute it and/or modify
10 ;; it under the terms of the GNU General Public License as published by 10 ;; it under the terms of the GNU General Public License as published by
44 44
45 \\{reftex-toc-map}" 45 \\{reftex-toc-map}"
46 (interactive) 46 (interactive)
47 (kill-all-local-variables) 47 (kill-all-local-variables)
48 (setq major-mode 'reftex-toc-mode 48 (setq major-mode 'reftex-toc-mode
49 mode-name "TOC") 49 mode-name "TOC")
50 (use-local-map reftex-toc-map) 50 (use-local-map reftex-toc-map)
51 (set (make-local-variable 'transient-mark-mode) t)
52 (set (make-local-variable 'zmacs-regions) t)
51 (set (make-local-variable 'revert-buffer-function) 'reftex-toc-revert) 53 (set (make-local-variable 'revert-buffer-function) 'reftex-toc-revert)
52 (set (make-local-variable 'reftex-toc-include-labels-indicator) "") 54 (set (make-local-variable 'reftex-toc-include-labels-indicator) "")
53 (set (make-local-variable 'reftex-toc-max-level-indicator) 55 (set (make-local-variable 'reftex-toc-max-level-indicator)
54 (if (= reftex-toc-max-level 100) 56 (if (= reftex-toc-max-level 100)
55 "ALL" 57 "ALL"
56 (int-to-string reftex-toc-max-level))) 58 (int-to-string reftex-toc-max-level)))
57 (setq mode-line-format 59 (setq mode-line-format
58 (list "---- " 'mode-line-buffer-identification 60 (list "---- " 'mode-line-buffer-identification
59 " " 'global-mode-string " (" mode-name ")" 61 " " 'global-mode-string " (" mode-name ")"
60 " L<" 'reftex-toc-include-labels-indicator ">" 62 " L<" 'reftex-toc-include-labels-indicator ">"
61 " I<" 'reftex-toc-include-index-indicator ">" 63 " I<" 'reftex-toc-include-index-indicator ">"
62 " T<" 'reftex-toc-max-level-indicator ">" 64 " T<" 'reftex-toc-max-level-indicator ">"
63 " -%-")) 65 " -%-"))
64 (setq truncate-lines t) 66 (setq truncate-lines t)
65 (when (featurep 'xemacs) 67 (when (featurep 'xemacs)
66 ;; XEmacs needs the call to make-local-hook 68 ;; XEmacs needs the call to make-local-hook
67 (make-local-hook 'post-command-hook) 69 (make-local-hook 'post-command-hook)
68 (make-local-hook 'pre-command-hook)) 70 (make-local-hook 'pre-command-hook))
89 ============================ 91 ============================
90 n / p next-line / previous-line 92 n / p next-line / previous-line
91 SPC Show the corresponding location of the LaTeX document. 93 SPC Show the corresponding location of the LaTeX document.
92 TAB Goto the location and keep the *toc* window. 94 TAB Goto the location and keep the *toc* window.
93 RET Goto the location and hide the *toc* window (also on mouse-2). 95 RET Goto the location and hide the *toc* window (also on mouse-2).
96 < / > Promote / Demote section, or all sections in region.
94 C-c > Display Index. With prefix arg, restrict index to current section. 97 C-c > Display Index. With prefix arg, restrict index to current section.
95 q / k Hide/Kill *toc* buffer, return to position of reftex-toc command. 98 q / k Hide/Kill *toc* buffer, return to position of reftex-toc command.
96 l i c F Toggle display of [l]abels, [i]ndex, [c]ontext, [F]ile borders. 99 l i c F Toggle display of [l]abels, [i]ndex, [c]ontext, [F]ile borders.
97 t Change maximum toc depth (e.g. `3 t' hides levels greater than 3). 100 t Change maximum toc depth (e.g. `3 t' hides levels greater than 3).
98 f / a / g Toggle follow mode / toggle auto recenter / Refresh *toc* buffer. 101 f / g Toggle follow mode / Refresh *toc* buffer.
102 a / d Toggle auto recenter / Toggle dedicated frame
99 r / C-u r Reparse the LaTeX document / Reparse entire LaTeX document. 103 r / C-u r Reparse the LaTeX document / Reparse entire LaTeX document.
100 . In other window, show position from where `reftex-toc' was called. 104 . In other window, show position from where `reftex-toc' was called.
105 M-% Global search and replace to rename label at point.
101 x Switch to TOC of external document (with LaTeX package `xr'). 106 x Switch to TOC of external document (with LaTeX package `xr').
102 z Jump to a specific section (e.g. '3 z' goes to section 3") 107 z Jump to a specific section (e.g. '3 z' goes to section 3).")
103 108
104 (defun reftex-toc (&optional rebuild) 109 (defun reftex-toc (&optional rebuild reuse)
105 "Show the table of contents for the current document. 110 "Show the table of contents for the current document.
106 When called with a raw C-u prefix, rescan the document first." 111 When called with a raw C-u prefix, rescan the document first."
112
113 ;; The REUSE argument means, search all visible frames for a window
114 ;; displaying the toc window. If yes, reuse this window.
107 115
108 (interactive) 116 (interactive)
109 117
110 (if (or (not (string= reftex-last-toc-master (reftex-TeX-master-file))) 118 (if (or (not (string= reftex-last-toc-master (reftex-TeX-master-file)))
111 current-prefix-arg) 119 current-prefix-arg)
127 135
128 ;; Ensure access to scanning info and rescan buffer if prefix are is '(4) 136 ;; Ensure access to scanning info and rescan buffer if prefix are is '(4)
129 (reftex-access-scan-info current-prefix-arg) 137 (reftex-access-scan-info current-prefix-arg)
130 138
131 (let* ((this-buf (current-buffer)) 139 (let* ((this-buf (current-buffer))
132 (docstruct-symbol reftex-docstruct-symbol) 140 (docstruct-symbol reftex-docstruct-symbol)
133 (xr-data (assq 'xr (symbol-value reftex-docstruct-symbol))) 141 (xr-data (assq 'xr (symbol-value reftex-docstruct-symbol)))
134 (xr-alist (cons (cons "" (buffer-file-name)) (nth 1 xr-data))) 142 (xr-alist (cons (cons "" (buffer-file-name)) (nth 1 xr-data)))
135 (here-I-am (if (boundp 'reftex-rebuilding-toc) 143 (here-I-am (if (boundp 'reftex-rebuilding-toc)
136 (get 'reftex-toc :reftex-data) 144 (get 'reftex-toc :reftex-data)
137 (car (reftex-where-am-I)))) 145 (car (reftex-where-am-I))))
138 offset) 146 (unsplittable (if (fboundp 'frame-property)
139 147 (frame-property (selected-frame) 'unsplittable)
140 (if (get-buffer-window "*toc*") 148 (frame-parameter (selected-frame) 'unsplittable)))
141 (select-window (get-buffer-window "*toc*")) 149 offset toc-window)
150
151 (if (setq toc-window (get-buffer-window
152 "*toc*"
153 (if reuse 'visible)))
154 (select-window toc-window)
142 (when (or (not reftex-toc-keep-other-windows) 155 (when (or (not reftex-toc-keep-other-windows)
143 (< (window-height) (* 2 window-min-height))) 156 (< (window-height) (* 2 window-min-height)))
144 (delete-other-windows)) 157 (delete-other-windows))
145 158
146 (setq reftex-last-window-width (window-width) 159 (setq reftex-last-window-width (window-width)
147 reftex-last-window-height (window-height)) ; remember 160 reftex-last-window-height (window-height)) ; remember
148 (if reftex-toc-split-windows-horizontally 161
149 (split-window-horizontally 162 (unless unsplittable
150 (floor (* (frame-width) reftex-toc-split-windows-horizontally-fraction))) 163 (if reftex-toc-split-windows-horizontally
151 (split-window)) 164 (split-window-horizontally
165 (floor (* (window-width)
166 reftex-toc-split-windows-fraction)))
167 (split-window-vertically
168 (floor (* (window-height)
169 reftex-toc-split-windows-fraction)))))
152 170
153 (let ((default-major-mode 'reftex-toc-mode)) 171 (let ((default-major-mode 'reftex-toc-mode))
154 (switch-to-buffer "*toc*"))) 172 (switch-to-buffer "*toc*")))
155 173
156 (or (eq major-mode 'reftex-toc-mode) (reftex-toc-mode)) 174 (or (eq major-mode 'reftex-toc-mode) (reftex-toc-mode))
157 (set (make-local-variable 'reftex-docstruct-symbol) docstruct-symbol) 175 (set (make-local-variable 'reftex-docstruct-symbol) docstruct-symbol)
158 (setq reftex-toc-include-labels-indicator 176 (setq reftex-toc-include-labels-indicator
159 (if (eq reftex-toc-include-labels t) 177 (if (eq reftex-toc-include-labels t)
160 "ALL" 178 "ALL"
161 reftex-toc-include-labels)) 179 reftex-toc-include-labels))
162 (setq reftex-toc-include-index-indicator 180 (setq reftex-toc-include-index-indicator
163 (if (eq reftex-toc-include-index-entries t) 181 (if (eq reftex-toc-include-index-entries t)
164 "ALL" 182 "ALL"
165 reftex-toc-include-index-entries)) 183 reftex-toc-include-index-entries))
166 184
167 (cond 185 (cond
168 ((= (buffer-size) 0) 186 ((= (buffer-size) 0)
169 ;; buffer is empty - fill it with the table of contents 187 ;; buffer is empty - fill it with the table of contents
170 (message "Building *toc* buffer...") 188 (message "Building *toc* buffer...")
180 (put-text-property (point-min) (point) 'face reftex-toc-header-face)) 198 (put-text-property (point-min) (point) 'face reftex-toc-header-face))
181 (put-text-property (point-min) (point) 'intangible t) 199 (put-text-property (point-min) (point) 'intangible t)
182 (put-text-property (point-min) (1+ (point-min)) 'xr-alist xr-alist) 200 (put-text-property (point-min) (1+ (point-min)) 'xr-alist xr-alist)
183 201
184 (setq offset 202 (setq offset
185 (reftex-insert-docstruct 203 (reftex-insert-docstruct
186 this-buf 204 this-buf
187 t ; include toc 205 t ; include toc
188 reftex-toc-include-labels 206 reftex-toc-include-labels
189 reftex-toc-include-index-entries 207 reftex-toc-include-index-entries
190 reftex-toc-include-file-boundaries 208 reftex-toc-include-file-boundaries
191 reftex-toc-include-context 209 reftex-toc-include-context
192 nil ; counter 210 nil ; counter
193 nil ; commented 211 nil ; commented
194 here-I-am 212 here-I-am
195 "" ; xr-prefix 213 "" ; xr-prefix
196 t ; a toc buffer 214 t ; a toc buffer
197 )) 215 ))
198 216
199 (run-hooks 'reftex-display-copied-context-hook) 217 (run-hooks 'reftex-display-copied-context-hook)
200 (message "Building *toc* buffer...done.") 218 (message "Building *toc* buffer...done.")
201 (setq buffer-read-only t)) 219 (setq buffer-read-only t))
202 (t 220 (t
203 ;; Only compute the offset 221 ;; Only compute the offset
204 (setq offset 222 (setq offset
205 (or (reftex-get-offset this-buf here-I-am 223 (or (reftex-get-offset this-buf here-I-am
206 (if reftex-toc-include-labels " " nil) 224 (if reftex-toc-include-labels " " nil)
207 t 225 t
208 reftex-toc-include-index-entries 226 reftex-toc-include-index-entries
209 reftex-toc-include-file-boundaries) 227 reftex-toc-include-file-boundaries)
210 (reftex-last-assoc-before-elt 228 (reftex-last-assoc-before-elt
211 'toc here-I-am 229 'toc here-I-am
212 (symbol-value reftex-docstruct-symbol)))) 230 (symbol-value reftex-docstruct-symbol))))
213 (put 'reftex-toc :reftex-line 3) 231 (put 'reftex-toc :reftex-line 3)
214 (goto-line 3) 232 (goto-line 3)
215 (beginning-of-line))) 233 (beginning-of-line)))
216 234
217 ;; Find the correct starting point 235 ;; Find the correct starting point
219 (setq reftex-last-follow-point (point)))) 237 (setq reftex-last-follow-point (point))))
220 238
221 (defun reftex-toc-recenter (&optional arg) 239 (defun reftex-toc-recenter (&optional arg)
222 "Display the TOC window and highlight line corresponding to current position." 240 "Display the TOC window and highlight line corresponding to current position."
223 (interactive "P") 241 (interactive "P")
224 (let ((buf (current-buffer))) 242 (let ((buf (current-buffer))
225 (reftex-toc arg) 243 (frame (selected-frame)))
244 (reftex-toc arg t)
226 (if (= (count-lines 1 (point)) 2) 245 (if (= (count-lines 1 (point)) 2)
227 (let ((current-prefix-arg nil)) 246 (let ((current-prefix-arg nil))
228 (select-window (get-buffer-window buf)) 247 (select-window (get-buffer-window buf frame))
229 (reftex-toc nil))) 248 (reftex-toc nil t)))
230 (and (> (point) 1) 249 (and (> (point) 1)
231 (not (get-text-property (point) 'intangible)) 250 (not (get-text-property (point) 'intangible))
232 (memq reftex-highlight-selection '(cursor both)) 251 (memq reftex-highlight-selection '(cursor both))
233 (reftex-highlight 2 252 (reftex-highlight 2
234 (or (previous-single-property-change 253 (or (previous-single-property-change
235 (min (point-max) (1+ (point))) :data) 254 (min (point-max) (1+ (point))) :data)
236 (point-min)) 255 (point-min))
237 (or (next-single-property-change (point) :data) 256 (or (next-single-property-change (point) :data)
238 (point-max)))) 257 (point-max))))
239 (select-window (get-buffer-window buf)))) 258 (select-window (get-buffer-window buf frame))))
240 259
241 (defun reftex-toc-pre-command-hook () 260 (defun reftex-toc-pre-command-hook ()
242 ;; used as pre command hook in *toc* buffer 261 ;; used as pre command hook in *toc* buffer
243 (reftex-unhighlight 0) 262 (reftex-unhighlight 0)
244 ;; (reftex-unhighlight 1) ;; remove highlight on leaving buffer.
245 ) 263 )
246 264
247 (defun reftex-toc-post-command-hook () 265 (defun reftex-toc-post-command-hook ()
248 ;; used in the post-command-hook for the *toc* buffer 266 ;; used in the post-command-hook for the *toc* buffer
249 (when (get-text-property (point) :data) 267 (when (get-text-property (point) :data)
250 (put 'reftex-toc :reftex-data (get-text-property (point) :data)) 268 (put 'reftex-toc :reftex-data (get-text-property (point) :data))
251 (and (> (point) 1) 269 (and (> (point) 1)
252 (not (get-text-property (point) 'intangible)) 270 (not (get-text-property (point) 'intangible))
253 (memq reftex-highlight-selection '(cursor both)) 271 (memq reftex-highlight-selection '(cursor both))
254 (reftex-highlight 2 272 (reftex-highlight 2
255 (or (previous-single-property-change (1+ (point)) :data) 273 (or (previous-single-property-change (1+ (point)) :data)
256 (point-min)) 274 (point-min))
257 (or (next-single-property-change (point) :data) 275 (or (next-single-property-change (point) :data)
258 (point-max))))) 276 (point-max)))))
259 (if (integerp reftex-toc-follow-mode) 277 (if (integerp reftex-toc-follow-mode)
260 ;; remove delayed action 278 ;; remove delayed action
261 (setq reftex-toc-follow-mode t) 279 (setq reftex-toc-follow-mode t)
262 (and reftex-toc-follow-mode 280 (and (not (reftex-toc-dframe-p))
263 (not (equal reftex-last-follow-point (point))) 281 reftex-toc-follow-mode
264 ;; show context in other window 282 (not (equal reftex-last-follow-point (point)))
265 (setq reftex-last-follow-point (point)) 283 ;; show context in other window
266 (condition-case nil 284 (setq reftex-last-follow-point (point))
267 (reftex-toc-visit-location nil (not reftex-revisit-to-follow)) 285 (condition-case nil
268 (error t))))) 286 (reftex-toc-visit-location nil (not reftex-revisit-to-follow))
287 (error t)))))
269 288
270 (defun reftex-re-enlarge () 289 (defun reftex-re-enlarge ()
271 ;; Enlarge window to a remembered size. 290 ;; Enlarge window to a remembered size.
272 (if reftex-toc-split-windows-horizontally 291 (if reftex-toc-split-windows-horizontally
273 (enlarge-window-horizontally 292 (enlarge-window-horizontally
274 (max 0 (- (or reftex-last-window-width (window-width)) 293 (max 0 (- (or reftex-last-window-width (window-width))
275 (window-width)))) 294 (window-width))))
276 (enlarge-window 295 (enlarge-window
277 (max 0 (- (or reftex-last-window-height (window-height)) 296 (max 0 (- (or reftex-last-window-height (window-height))
278 (window-height)))))) 297 (window-height))))))
298
299 (defun reftex-toc-dframe-p (&optional frame error)
300 ;; Check if FRAME is the dedicated TOC frame.
301 ;; If yes, and ERROR is non-nil, throw an error.
302 (setq frame (or frame (selected-frame)))
303 (let ((res (equal
304 (if (fboundp 'frame-property)
305 (frame-property frame 'name)
306 (frame-parameter frame 'name))
307 "RefTeX TOC Frame")))
308 (if (and res error)
309 (error "This frame is view-only. Use `C-c =' to create toc window for commands."))
310 res))
279 311
280 (defun reftex-toc-show-help () 312 (defun reftex-toc-show-help ()
281 "Show a summary of special key bindings." 313 "Show a summary of special key bindings."
282 (interactive) 314 (interactive)
315 (reftex-toc-dframe-p nil 'error)
283 (with-output-to-temp-buffer "*RefTeX Help*" 316 (with-output-to-temp-buffer "*RefTeX Help*"
284 (princ reftex-toc-help)) 317 (princ reftex-toc-help))
285 (reftex-enlarge-to-fit "*RefTeX Help*" t) 318 (reftex-enlarge-to-fit "*RefTeX Help*" t)
286 ;; If follow mode is active, arrange to delay it one command 319 ;; If follow mode is active, arrange to delay it one command
287 (if reftex-toc-follow-mode 320 (if reftex-toc-follow-mode
288 (setq reftex-toc-follow-mode 1))) 321 (setq reftex-toc-follow-mode 1)))
289 322
290 (defun reftex-toc-next (&optional arg) 323 (defun reftex-toc-next (&optional arg)
291 "Move to next selectable item." 324 "Move to next selectable item."
292 (interactive "p") 325 (interactive "p")
326 (if (boundp 'zmacs-region-stays) (setq zmacs-region-stays t))
293 (setq reftex-callback-fwd t) 327 (setq reftex-callback-fwd t)
294 (or (eobp) (forward-char 1)) 328 (or (eobp) (forward-char 1))
295 (goto-char (or (next-single-property-change (point) :data) 329 (goto-char (or (next-single-property-change (point) :data)
296 (point)))) 330 (point))))
297 (defun reftex-toc-previous (&optional arg) 331 (defun reftex-toc-previous (&optional arg)
298 "Move to previous selectable item." 332 "Move to previous selectable item."
299 (interactive "p") 333 (interactive "p")
334 (if (boundp 'zmacs-region-stays) (setq zmacs-region-stays t))
300 (setq reftex-callback-fwd nil) 335 (setq reftex-callback-fwd nil)
301 (goto-char (or (previous-single-property-change (point) :data) 336 (goto-char (or (previous-single-property-change (point) :data)
302 (point)))) 337 (point))))
303 (defun reftex-toc-next-heading (&optional arg) 338 (defun reftex-toc-next-heading (&optional arg)
304 "Move to next table of contentes line." 339 "Move to next table of contentes line."
305 (interactive "p") 340 (interactive "p")
341 (if (boundp 'zmacs-region-stays) (setq zmacs-region-stays t))
306 (end-of-line) 342 (end-of-line)
307 (re-search-forward "^ " nil t arg) 343 (re-search-forward "^ " nil t arg)
308 (beginning-of-line)) 344 (beginning-of-line))
309 (defun reftex-toc-previous-heading (&optional arg) 345 (defun reftex-toc-previous-heading (&optional arg)
310 "Move to previous table of contentes line." 346 "Move to previous table of contentes line."
311 (interactive "p") 347 (interactive "p")
348 (if (boundp 'zmacs-region-stays) (setq zmacs-region-stays t))
312 (re-search-backward "^ " nil t arg)) 349 (re-search-backward "^ " nil t arg))
313 (defun reftex-toc-toggle-follow () 350 (defun reftex-toc-toggle-follow ()
314 "Toggle follow (other window follows with context)." 351 "Toggle follow (other window follows with context)."
315 (interactive) 352 (interactive)
316 (setq reftex-last-follow-point -1) 353 (setq reftex-last-follow-point -1)
317 (setq reftex-toc-follow-mode (not reftex-toc-follow-mode))) 354 (setq reftex-toc-follow-mode (not reftex-toc-follow-mode)))
318 (defun reftex-toc-toggle-file-boundary () 355 (defun reftex-toc-toggle-file-boundary ()
319 "Toggle inclusion of file boundaries in *toc* buffer." 356 "Toggle inclusion of file boundaries in *toc* buffer."
320 (interactive) 357 (interactive)
321 (setq reftex-toc-include-file-boundaries 358 (setq reftex-toc-include-file-boundaries
322 (not reftex-toc-include-file-boundaries)) 359 (not reftex-toc-include-file-boundaries))
323 (reftex-toc-revert)) 360 (reftex-toc-revert))
324 (defun reftex-toc-toggle-labels (arg) 361 (defun reftex-toc-toggle-labels (arg)
325 "Toggle inclusion of labels in *toc* buffer. 362 "Toggle inclusion of labels in *toc* buffer.
326 With prefix ARG, prompt for a label type and include only labels of 363 With prefix ARG, prompt for a label type and include only labels of
327 that specific type." 364 that specific type."
328 (interactive "P") 365 (interactive "P")
329 (setq reftex-toc-include-labels 366 (setq reftex-toc-include-labels
330 (if arg (reftex-query-label-type) 367 (if arg (reftex-query-label-type)
331 (not reftex-toc-include-labels))) 368 (not reftex-toc-include-labels)))
332 (reftex-toc-revert)) 369 (reftex-toc-revert))
333 (defun reftex-toc-toggle-index (arg) 370 (defun reftex-toc-toggle-index (arg)
334 "Toggle inclusion of index in *toc* buffer. 371 "Toggle inclusion of index in *toc* buffer.
335 With prefix arg, prompt for an index tag and include only entries of that 372 With prefix arg, prompt for an index tag and include only entries of that
336 specific index." 373 specific index."
337 (interactive "P") 374 (interactive "P")
338 (setq reftex-toc-include-index-entries 375 (setq reftex-toc-include-index-entries
339 (if arg (reftex-index-select-tag) 376 (if arg (reftex-index-select-tag)
340 (not reftex-toc-include-index-entries))) 377 (not reftex-toc-include-index-entries)))
341 (reftex-toc-revert)) 378 (reftex-toc-revert))
342 (defun reftex-toc-toggle-context () 379 (defun reftex-toc-toggle-context ()
343 "Toggle inclusion of label context in *toc* buffer. 380 "Toggle inclusion of label context in *toc* buffer.
344 Label context is only displayed when the labels are there as well." 381 Label context is only displayed when the labels are there as well."
345 (interactive) 382 (interactive)
349 "Set the maximum level of toc lines in this buffer to value of prefix ARG. 386 "Set the maximum level of toc lines in this buffer to value of prefix ARG.
350 When no prefix is given, set the max level to a large number, so that all 387 When no prefix is given, set the max level to a large number, so that all
351 levels are shown. For eaxample, to set the level to 3, type `3 m'." 388 levels are shown. For eaxample, to set the level to 3, type `3 m'."
352 (interactive "P") 389 (interactive "P")
353 (setq reftex-toc-max-level (if arg 390 (setq reftex-toc-max-level (if arg
354 (prefix-numeric-value arg) 391 (prefix-numeric-value arg)
355 100)) 392 100))
356 (setq reftex-toc-max-level-indicator 393 (setq reftex-toc-max-level-indicator
357 (if arg (int-to-string reftex-toc-max-level) "ALL")) 394 (if arg (int-to-string reftex-toc-max-level) "ALL"))
358 (reftex-toc-revert)) 395 (reftex-toc-revert))
359 (defun reftex-toc-view-line () 396 (defun reftex-toc-view-line ()
360 "View document location in other window." 397 "View document location in other window."
361 (interactive) 398 (interactive)
399 (reftex-toc-dframe-p nil 'error)
362 (reftex-toc-visit-location)) 400 (reftex-toc-visit-location))
363 (defun reftex-toc-goto-line-and-hide () 401 (defun reftex-toc-goto-line-and-hide ()
364 "Go to document location in other window. Hide the *toc* window." 402 "Go to document location in other window. Hide the *toc* window."
365 (interactive) 403 (interactive)
404 (reftex-toc-dframe-p nil 'error)
366 (reftex-toc-visit-location 'hide)) 405 (reftex-toc-visit-location 'hide))
367 (defun reftex-toc-goto-line () 406 (defun reftex-toc-goto-line ()
368 "Go to document location in other window. *toc* window stays." 407 "Go to document location in other window. *toc* window stays."
369 (interactive) 408 (interactive)
409 (reftex-toc-dframe-p nil 'error)
370 (reftex-toc-visit-location t)) 410 (reftex-toc-visit-location t))
371 (defun reftex-toc-mouse-goto-line-and-hide (ev) 411 (defun reftex-toc-mouse-goto-line-and-hide (ev)
372 "Go to document location in other window. Hide the *toc* window." 412 "Go to document location in other window. Hide the *toc* window."
373 (interactive "e") 413 (interactive "e")
374 (mouse-set-point ev) 414 (mouse-set-point ev)
415 (reftex-toc-dframe-p nil 'error)
375 (reftex-toc-visit-location 'hide)) 416 (reftex-toc-visit-location 'hide))
376 (defun reftex-toc-show-calling-point () 417 (defun reftex-toc-show-calling-point ()
377 "Show point where reftex-toc was called from." 418 "Show point where reftex-toc was called from."
378 (interactive) 419 (interactive)
420 (reftex-toc-dframe-p nil 'error)
379 (let ((this-window (selected-window))) 421 (let ((this-window (selected-window)))
380 (unwind-protect 422 (unwind-protect
381 (progn 423 (progn
382 (switch-to-buffer-other-window 424 (switch-to-buffer-other-window
383 (marker-buffer reftex-toc-return-marker)) 425 (marker-buffer reftex-toc-return-marker))
384 (goto-char (marker-position reftex-toc-return-marker)) 426 (goto-char (marker-position reftex-toc-return-marker))
385 (recenter '(4))) 427 (recenter '(4)))
386 (select-window this-window)))) 428 (select-window this-window))))
387 (defun reftex-toc-quit () 429 (defun reftex-toc-quit ()
388 "Hide the *toc* window and do not move point." 430 "Hide the *toc* window and do not move point.
389 (interactive) 431 If the toc window is the only window on the dedicated TOC frame, the frame
390 (or (one-window-p) (delete-window)) 432 is destroyed."
391 (switch-to-buffer (marker-buffer reftex-toc-return-marker)) 433 (interactive)
392 (reftex-re-enlarge) 434 (if (and (one-window-p)
393 (goto-char (or (marker-position reftex-toc-return-marker) (point)))) 435 (reftex-toc-dframe-p)
436 (> (length (frame-list)) 1))
437 (delete-frame)
438 (or (one-window-p) (delete-window))
439 (switch-to-buffer (marker-buffer reftex-toc-return-marker))
440 (reftex-re-enlarge)
441 (goto-char (or (marker-position reftex-toc-return-marker) (point)))))
394 (defun reftex-toc-quit-and-kill () 442 (defun reftex-toc-quit-and-kill ()
395 "Kill the *toc* buffer." 443 "Kill the *toc* buffer."
396 (interactive) 444 (interactive)
397 (kill-buffer "*toc*") 445 (kill-buffer "*toc*")
398 (or (one-window-p) (delete-window)) 446 (or (one-window-p) (delete-window))
402 (defun reftex-toc-display-index (&optional arg) 450 (defun reftex-toc-display-index (&optional arg)
403 "Display the index buffer for the current document. 451 "Display the index buffer for the current document.
404 This works just like `reftex-display-index' from a LaTeX buffer. 452 This works just like `reftex-display-index' from a LaTeX buffer.
405 With prefix arg 1, restrict index to the section at point." 453 With prefix arg 1, restrict index to the section at point."
406 (interactive "P") 454 (interactive "P")
455 (reftex-toc-dframe-p nil 'error)
407 (let ((data (get-text-property (point) :data)) 456 (let ((data (get-text-property (point) :data))
408 (docstruct (symbol-value reftex-docstruct-symbol)) 457 (docstruct (symbol-value reftex-docstruct-symbol))
409 bor eor restr) 458 bor eor restr)
410 (when (equal arg 2) 459 (when (equal arg 2)
411 (setq bor (reftex-last-assoc-before-elt 'toc data docstruct) 460 (setq bor (reftex-last-assoc-before-elt 'toc data docstruct)
412 eor (assoc 'toc (cdr (memq bor docstruct))) 461 eor (assoc 'toc (cdr (memq bor docstruct)))
413 restr (list (nth 6 bor) bor eor))) 462 restr (list (nth 6 bor) bor eor)))
414 (reftex-toc-goto-line) 463 (reftex-toc-goto-line)
415 (reftex-display-index (if restr nil arg) restr))) 464 (reftex-display-index (if restr nil arg) restr)))
465
466 ;; Rescanning the document and rebuilding the TOC buffer.
416 (defun reftex-toc-rescan (&rest ignore) 467 (defun reftex-toc-rescan (&rest ignore)
417 "Regenerate the *toc* buffer by reparsing file of section at point." 468 "Regenerate the *toc* buffer by reparsing file of section at point."
418 (interactive) 469 (interactive)
419 (if (and reftex-enable-partial-scans 470 (if (and reftex-enable-partial-scans
420 (null current-prefix-arg)) 471 (null current-prefix-arg))
421 (let* ((data (get-text-property (point) :data)) 472 (let* ((data (get-text-property (point) :data))
422 (what (car data)) 473 (what (car data))
423 (file (cond ((eq what 'toc) (nth 3 data)) 474 (file (cond ((eq what 'toc) (nth 3 data))
424 ((memq what '(eof bof file-error)) (nth 1 data)) 475 ((memq what '(eof bof file-error)) (nth 1 data))
425 ((stringp what) (nth 3 data)) 476 ((stringp what) (nth 3 data))
426 ((eq what 'index) (nth 3 data)))) 477 ((eq what 'index) (nth 3 data))))
427 (line (+ (count-lines (point-min) (point)) (if (bolp) 1 0)))) 478 (line (+ (count-lines (point-min) (point)) (if (bolp) 1 0))))
428 (if (not file) 479 (if (not file)
429 (error "Don't know which file to rescan. Try `C-u r'") 480 (error "Don't know which file to rescan. Try `C-u r'")
430 (put 'reftex-toc :reftex-line line) 481 (put 'reftex-toc :reftex-line line)
431 (switch-to-buffer-other-window 482 (switch-to-buffer-other-window
432 (reftex-get-file-buffer-force file)) 483 (reftex-get-file-buffer-force file))
433 (setq current-prefix-arg '(4)) 484 (setq current-prefix-arg '(4))
434 (let ((reftex-rebuilding-toc t)) 485 (let ((reftex-rebuilding-toc t))
435 (reftex-toc)))) 486 (reftex-toc))))
436 (reftex-toc-Rescan)) 487 (reftex-toc-Rescan))
437 (reftex-kill-temporary-buffers)) 488 (reftex-kill-temporary-buffers))
489
438 (defun reftex-toc-Rescan (&rest ignore) 490 (defun reftex-toc-Rescan (&rest ignore)
439 "Regenerate the *toc* buffer by reparsing the entire document." 491 "Regenerate the *toc* buffer by reparsing the entire document."
440 (interactive) 492 (interactive)
441 (let* ((line (+ (count-lines (point-min) (point)) (if (bolp) 1 0)))) 493 (let* ((line (+ (count-lines (point-min) (point)) (if (bolp) 1 0))))
442 (put 'reftex-toc :reftex-line line)) 494 (put 'reftex-toc :reftex-line line))
443 (switch-to-buffer-other-window 495 (switch-to-buffer-other-window
444 (reftex-get-file-buffer-force reftex-last-toc-file)) 496 (reftex-get-file-buffer-force reftex-last-toc-file))
445 (setq current-prefix-arg '(16)) 497 (setq current-prefix-arg '(16))
446 (let ((reftex-rebuilding-toc t)) 498 (let ((reftex-rebuilding-toc t))
447 (reftex-toc))) 499 (reftex-toc)))
500
448 (defun reftex-toc-revert (&rest ignore) 501 (defun reftex-toc-revert (&rest ignore)
449 "Regenerate the *toc* from the internal lists." 502 "Regenerate the *toc* from the internal lists."
450 (interactive) 503 (interactive)
451 (switch-to-buffer-other-window 504 (let ((unsplittable
452 (reftex-get-file-buffer-force reftex-last-toc-file)) 505 (if (fboundp 'frame-property)
506 (frame-property (selected-frame) 'unsplittable)
507 (frame-parameter (selected-frame) 'unsplittable)))
508 (reftex-rebuilding-toc t))
509 (if unsplittable
510 (switch-to-buffer
511 (reftex-get-file-buffer-force reftex-last-toc-file))
512 (switch-to-buffer-other-window
513 (reftex-get-file-buffer-force reftex-last-toc-file))))
453 (reftex-erase-buffer "*toc*") 514 (reftex-erase-buffer "*toc*")
454 (setq current-prefix-arg nil) 515 (setq current-prefix-arg nil)
455 (let ((reftex-rebuilding-toc t)) 516 (reftex-toc t))
456 (reftex-toc t))) 517
457 (defun reftex-toc-external (&rest ignore) 518 (defun reftex-toc-external (&rest ignore)
458 "Switch to table of contents of an external document." 519 "Switch to table of contents of an external document."
459 (interactive) 520 (interactive)
521 (reftex-toc-dframe-p nil 'error)
460 (let* ((old-buf (current-buffer)) 522 (let* ((old-buf (current-buffer))
461 (xr-alist (get-text-property 1 'xr-alist)) 523 (xr-alist (get-text-property 1 'xr-alist))
462 (xr-index (reftex-select-external-document 524 (xr-index (reftex-select-external-document
463 xr-alist 0))) 525 xr-alist 0)))
464 (switch-to-buffer-other-window (or (reftex-get-file-buffer-force 526 (switch-to-buffer-other-window (or (reftex-get-file-buffer-force
465 (cdr (nth xr-index xr-alist))) 527 (cdr (nth xr-index xr-alist)))
466 (error "Cannot switch document"))) 528 (error "Cannot switch document")))
467 (reftex-toc) 529 (reftex-toc)
468 (if (equal old-buf (current-buffer)) 530 (if (equal old-buf (current-buffer))
469 (message "") 531 (message "")
470 (message "Switched document")))) 532 (message "Switched document"))))
471 533
472 (defun reftex-toc-jump (arg) 534 (defun reftex-toc-jump (arg)
473 "Jump to a specific section. E.g. '3 z' jumps to section 3. 535 "Jump to a specific section. E.g. '3 z' jumps to section 3.
474 Useful for large TOC's." 536 Useful for large TOC's."
477 (re-search-forward 539 (re-search-forward
478 (concat "^ *" (number-to-string (if (numberp arg) arg 1)) " ") 540 (concat "^ *" (number-to-string (if (numberp arg) arg 1)) " ")
479 nil t) 541 nil t)
480 (beginning-of-line)) 542 (beginning-of-line))
481 543
544 ;; Promotion/Demotion stuff
545
546 (defun reftex-toc-demote (&optional arg)
547 "Demote section at point. If region is active, apply to all in region."
548 (interactive "p")
549 (reftex-toc-do-promote 1))
550 (defun reftex-toc-promote (&optional arg)
551 "Promote section at point. If region is active, apply to all in region."
552 (interactive "p")
553 (reftex-toc-do-promote -1))
554 (defun reftex-toc-do-promote (delta)
555 "Workhorse for reftex-toc-promote and reftex-to-demote.
556 Changes the level of sections in the current region, or just the section at
557 point."
558 ;; We should not do anything unless we are sure this is going to work for
559 ;; each section in the region. Therefore we first collect information and
560 ;; test.
561 (let* ((start-line (+ (count-lines (point-min) (point))
562 (if (bolp) 1 0)))
563 (mark-line (if (reftex-region-active-p)
564 (save-excursion (goto-char (mark))
565 (+ (count-lines (point-min) (point))
566 (if (bolp) 1 0)))))
567 (start-pos (point))
568 (pro-or-de (if (> delta 0) "de" "pro"))
569 beg end entries data sections nsec mpos msg)
570 (setq msg
571 (catch 'exit
572 (if (reftex-region-active-p)
573 ;; A region is dangerous, check if we have a brandnew scan,
574 ;; to make sure we are not missing any section statements.
575 (if (not (reftex-toc-check-docstruct))
576 (reftex-toc-load-all-files-for-promotion) ;; exits
577 (setq beg (min (point) (mark))
578 end (max (point) (mark))))
579 (setq beg (point) end (point)))
580 (goto-char beg)
581 (while (and (setq data (get-text-property (point) :data))
582 (<= (point) end))
583 (if (eq (car data) 'toc) (push (cons data (point)) entries))
584 (goto-char (or (next-single-property-change (point) :data)
585 (point-max))))
586 (setq entries (nreverse entries))
587 ;; Get the relevant section numbers, for an informative message
588 (goto-char start-pos)
589 (setq sections (reftex-toc-extract-section-number (car entries)))
590 (if (> (setq nsec (length entries)) 1)
591 (setq sections
592 (concat sections "-"
593 (reftex-toc-extract-section-number
594 (nth (1- nsec) entries)))))
595 ;; Run through the list and prepare the changes.
596 (setq entries (mapcar 'reftex-toc-promote-prepare entries))
597 ;; Ask for permission
598 (if (or (not reftex-toc-confirm-promotion) ; never confirm
599 (and (integerp reftex-toc-confirm-promotion) ; confirm if many
600 (< nsec reftex-toc-confirm-promotion))
601 (yes-or-no-p ; ask
602 (format "%s %d toc-entr%s (section%s %s)? "
603 (if (< delta 0) "Promote" "Demote")
604 nsec
605 (if (= 1 nsec) "y" "ies")
606 (if (= 1 nsec) "" "s")
607 sections)))
608 nil ; we have permission, do nothing
609 (error "Abort")) ; abort, we don't have permission
610 ;; Do the changes
611 (mapcar 'reftex-toc-promote-action entries)
612 ;; Rescan the document and rebuilt the toc buffer
613 (save-window-excursion
614 (reftex-toc-Rescan))
615 (reftex-toc-restore-region start-line mark-line)
616 (message "%d section%s %smoted"
617 nsec (if (= 1 nsec) "" "s") pro-or-de)
618 nil))
619 (if msg (progn (ding) (message msg)))))
620
621 (defvar delta)
622 (defvar mpos)
623 (defvar pro-or-de)
624 (defvar start-pos)
625 (defvar start-line)
626 (defvar mark-line)
627
628 (defun reftex-toc-restore-region (point-line &optional mark-line)
629 (if mark-line
630 (progn (goto-line mark-line)
631 (setq mpos (point))))
632 (if point-line (goto-line point-line))
633 (if mark-line
634 (progn
635 (set-mark mpos)
636 (if (fboundp 'zmacs-activate-region)
637 (zmacs-activate-region)
638 (setq mark-active t
639 deactivate-mark nil)))))
640
641 (defun reftex-toc-promote-prepare (x)
642 "Look at a toc entry and see if we could pro/demote it.
643 Expects the level change DELTA to be dynamically scoped into this function.
644 This function prepares everything for the changes, but does not do it.
645 The return value is a list with information needed when doing the
646 promotion/demotion later."
647 (let* ((data (car x))
648 (toc-point (cdr x))
649 (marker (nth 4 data))
650 (literal (nth 7 data))
651 (load nil)
652 (name nil)
653 ;; Here follows some paranoid code to make very sure we are not
654 ;; going to break anything
655 (name1 ; dummy
656 (if (and (markerp marker) (marker-buffer marker))
657 ;; Buffer is still live and we have the marker.
658 (progn
659 (save-excursion
660 ;; Goto the buffer and check of section is unchanged
661 (set-buffer (marker-buffer marker))
662 (goto-char (marker-position marker))
663 (if (looking-at (regexp-quote literal))
664 ;; OK, get the makro name
665 (progn
666 (beginning-of-line 1)
667 (if (looking-at reftex-section-regexp)
668 (setq name (reftex-match-string 2))
669 (error "Something is wrong! Contact maintainer!")))
670 ;; Section has changed, request scan and loading
671 ;; We use a variable to delay until after the safe-exc.
672 ;; because otherwise we loose the region.
673 (setq load t)))
674 ;; Scan document and load all files, this exits command
675 (if load (reftex-toc-load-all-files-for-promotion))) ; exits
676 ;; We don't have a live marker: scan and load files.
677 (reftex-toc-load-all-files-for-promotion)))
678 (level (cdr (assoc name reftex-section-levels-all)))
679 (dummy (if (not (integerp level))
680 (progn
681 (goto-char toc-point)
682 (error "Cannot %smote special sections" pro-or-de))))
683 ;; Delta is dynamically scoped into here...
684 (newlevel (if (>= level 0) (+ delta level) (- level delta)))
685 (dummy2 (if (or (and (>= level 0) (= newlevel -1))
686 (and (< level 0) (= newlevel 0)))
687 (error "Cannot %smote \\%s" pro-or-de name)))
688 (newname (reftex-toc-newhead-from-alist newlevel name
689 reftex-section-levels-all)))
690 (if (and name newname)
691 (list data name newname toc-point)
692 (goto-char toc-point)
693 (error "Cannot %smote \\%s" pro-or-de name))))
694
695 (defun reftex-toc-promote-action (x)
696 "Change the level of a toc entry.
697 DELTA and PRO-OR-DE are assumed to be dynamically scoped into this function."
698 (let* ((data (car x))
699 (name (nth 1 x))
700 (newname (nth 2 x))
701 (marker (nth 4 data)))
702 (save-excursion
703 (set-buffer (marker-buffer marker))
704 (goto-char (marker-position marker))
705 (if (looking-at (concat "\\([ \t]*\\\\\\)" (regexp-quote name)))
706 (replace-match (concat "\\1" newname))
707 (error "Fatal error during %smotion" pro-or-de)))))
708
709 (defun reftex-toc-extract-section-number (entry)
710 "Get the numbering of a toc entry, for message purposes."
711 (if (string-match "\\s-*\\(\\S-+\\)" (nth 2 (car entry)))
712 (match-string 1 (nth 2 (car entry)))
713 "?"))
714
715 (defun reftex-toc-newhead-from-alist (nlevel head alist)
716 "Get new heading with level NLEVEL from ALIST.
717 If there are no such entries, return nil.
718 If there are several different entries with same new level, choose
719 the one with the smallest distance to the assocation of HEAD in the alist.
720 This makes it possible for promotion to work several sets of headings,
721 if these sets are sorted blocks in the alist."
722 (let* ((al alist)
723 (ass (assoc head al))
724 (pos (length (memq ass al)))
725 dist (mindist 1000) newhead)
726 (while al
727 (if (equal (cdar al) nlevel)
728 (progn
729 (setq dist (abs (- (length al) pos)))
730 (if (< dist mindist)
731 (setq newhead (car (car al))
732 mindist dist))))
733 (setq al (cdr al)))
734 newhead))
735
736 (defun reftex-toc-check-docstruct ()
737 "Check if the current docstruct is fully up to date and all files visited."
738 ;; We do this by checking if all sections are on the right position
739 (let ((docstruct (symbol-value reftex-docstruct-symbol))
740 entry marker empoint)
741 (catch 'exit
742 (while (setq entry (pop docstruct))
743 (if (eq (car entry) 'toc)
744 (progn
745 (setq marker (nth 4 entry)
746 empoint (nth 8 entry))
747 (if (not (and (markerp marker)
748 (marker-buffer marker)
749 (= (marker-position marker) empoint)))
750 (throw 'exit nil)))))
751 t)))
752
753 (defun reftex-toc-load-all-files-for-promotion ()
754 "Make sure all files of the document are being visited by buffers,
755 and that the scanning info is absolutely up to date.
756 We do this by rescanning with reftex-keep-temporary-buffers bound to t.
757 The variable PRO-OR-DE is assumed to be dynamically scoped into thes function.
758 When finished, we exit with an error message."
759 (let ((reftex-keep-temporary-buffers t))
760 (reftex-toc-Rescan)
761 (reftex-toc-restore-region start-line mark-line)
762 (throw 'exit
763 (format "TOC had to be updated first. Please check selection and repeat the command." pro-or-de))))
764
765 (defun reftex-toc-rename-label ()
766 "Rename the currently selected label in the *TOC* buffer.
767 This launches a global search and replace in order to rename a label.
768 Renaming a label is hardly ever necessary - the only exeption is after
769 promotion/demotion in connection with a package like fancyref, where the
770 label prefix determines the wording of a reference."
771 (interactive)
772 (let* ((toc (get-text-property (point) :data))
773 (label (car toc)) newlabel)
774 (if (not (stringp label))
775 (error "This is not a label entry."))
776 (setq newlabel (read-string (format "Rename label \"%s\" to:" label)))
777 (if (assoc newlabel (symbol-value reftex-docstruct-symbol))
778 (if (not (y-or-n-p
779 (format "Label '%s' exists. Use anyway? " label)))
780 (error "Abort")))
781 (save-excursion
782 (save-window-excursion
783 (reftex-toc-visit-location t)
784 (condition-case nil
785 (reftex-query-replace-document
786 (concat "{" (regexp-quote label) "}")
787 (format "{%s}" newlabel))
788 (error t))))
789 (reftex-toc-rescan)))
790
791
482 (defun reftex-toc-visit-location (&optional final no-revisit) 792 (defun reftex-toc-visit-location (&optional final no-revisit)
483 ;; Visit the tex file corresponding to the toc entry on the current line. 793 ;; Visit the tex file corresponding to the toc entry on the current line.
484 ;; If FINAL is t, stay there 794 ;; If FINAL is t, stay there
485 ;; If FINAL is 'hide, hide the *toc* window. 795 ;; If FINAL is 'hide, hide the *toc* window.
486 ;; Otherwise, move cursor back into *toc* window. 796 ;; Otherwise, move cursor back into *toc* window.
492 (let* ((toc (get-text-property (point) :data)) 802 (let* ((toc (get-text-property (point) :data))
493 (toc-window (selected-window)) 803 (toc-window (selected-window))
494 show-window show-buffer match) 804 show-window show-buffer match)
495 805
496 (unless toc (error "Don't know which toc line to visit")) 806 (unless toc (error "Don't know which toc line to visit"))
497 807
498 (cond 808 (cond
499 809
500 ((eq (car toc) 'toc) 810 ((eq (car toc) 'toc)
501 ;; a toc entry 811 ;; a toc entry
502 (setq match (reftex-toc-find-section toc no-revisit))) 812 (setq match (reftex-toc-find-section toc no-revisit)))
503 813
504 ((eq (car toc) 'index) 814 ((eq (car toc) 'index)
506 (setq match (reftex-index-show-entry toc no-revisit))) 816 (setq match (reftex-index-show-entry toc no-revisit)))
507 817
508 ((memq (car toc) '(bof eof)) 818 ((memq (car toc) '(bof eof))
509 ;; A file entry 819 ;; A file entry
510 (setq match 820 (setq match
511 (let ((where (car toc)) 821 (let ((where (car toc))
512 (file (nth 1 toc))) 822 (file (nth 1 toc)))
513 (if (or (not no-revisit) (reftex-get-buffer-visiting file)) 823 (if (or (not no-revisit) (reftex-get-buffer-visiting file))
514 (progn 824 (progn
515 (switch-to-buffer-other-window 825 (switch-to-buffer-other-window
516 (reftex-get-file-buffer-force file nil)) 826 (reftex-get-file-buffer-force file nil))
517 (goto-char (if (eq where 'bof) (point-min) (point-max)))) 827 (goto-char (if (eq where 'bof) (point-min) (point-max))))
518 (message reftex-no-follow-message) nil)))) 828 (message reftex-no-follow-message) nil))))
519 829
520 ((stringp (car toc)) 830 ((stringp (car toc))
521 ;; a label 831 ;; a label
522 (setq match (reftex-show-label-location toc reftex-callback-fwd 832 (setq match (reftex-show-label-location toc reftex-callback-fwd
523 no-revisit t)))) 833 no-revisit t))))
524 834
525 (setq show-window (selected-window) 835 (setq show-window (selected-window)
526 show-buffer (current-buffer)) 836 show-buffer (current-buffer))
527 837
528 (unless match 838 (unless match
540 (reftex-unhighlight 0) 850 (reftex-unhighlight 0)
541 (or (one-window-p) (delete-window)) 851 (or (one-window-p) (delete-window))
542 ;; If `show-window' is still live, show-buffer is already visible 852 ;; If `show-window' is still live, show-buffer is already visible
543 ;; so let's not make it visible in yet-another-window. 853 ;; so let's not make it visible in yet-another-window.
544 (if (window-live-p show-window) 854 (if (window-live-p show-window)
545 (select-window show-window) 855 (set-buffer show-buffer)
546 (switch-to-buffer show-buffer)) 856 (switch-to-buffer show-buffer))
547 (reftex-re-enlarge)) 857 (reftex-re-enlarge))
548 (t nil)))) 858 (t nil))))
549 859
550 (defun reftex-toc-find-section (toc &optional no-revisit) 860 (defun reftex-toc-find-section (toc &optional no-revisit)
551 (let* ((file (nth 3 toc)) 861 (let* ((file (nth 3 toc))
552 (marker (nth 4 toc)) 862 (marker (nth 4 toc))
553 (level (nth 5 toc)) 863 (level (nth 5 toc))
554 (literal (nth 7 toc)) 864 (literal (nth 7 toc))
555 (emergency-point (nth 8 toc)) 865 (emergency-point (nth 8 toc))
556 (match 866 (match
557 (cond 867 (cond
558 ((and (markerp marker) (marker-buffer marker)) 868 ((and (markerp marker) (marker-buffer marker))
559 ;; Buffer is still live and we have the marker. Should be easy. 869 ;; Buffer is still live and we have the marker. Should be easy.
560 (switch-to-buffer-other-window (marker-buffer marker)) 870 (switch-to-buffer-other-window (marker-buffer marker))
561 (goto-char (marker-position marker)) 871 (goto-char (marker-position marker))
562 (or (looking-at (regexp-quote literal)) 872 (or (looking-at (regexp-quote literal))
563 (looking-at (reftex-make-regexp-allow-for-ctrl-m literal)) 873 (looking-at (reftex-make-regexp-allow-for-ctrl-m literal))
564 (looking-at (reftex-make-desperate-section-regexp literal)) 874 (looking-at (reftex-make-desperate-section-regexp literal))
565 (looking-at (concat "\\\\" 875 (looking-at (concat "\\\\"
566 (regexp-quote 876 (regexp-quote
567 (car 877 (car
568 (rassq level 878 (rassq level
569 reftex-section-levels-all))) 879 reftex-section-levels-all)))
570 "[[{]?")))) 880 "[[{]?"))))
571 ((or (not no-revisit) 881 ((or (not no-revisit)
572 (reftex-get-buffer-visiting file)) 882 (reftex-get-buffer-visiting file))
573 ;; Marker is lost. Use the backup method. 883 ;; Marker is lost. Use the backup method.
574 (switch-to-buffer-other-window 884 (switch-to-buffer-other-window
575 (reftex-get-file-buffer-force file nil)) 885 (reftex-get-file-buffer-force file nil))
576 (goto-char (or emergency-point (point-min))) 886 (goto-char (or emergency-point (point-min)))
577 (or (looking-at (regexp-quote literal)) 887 (or (looking-at (regexp-quote literal))
578 (let ((len (length literal))) 888 (let ((len (length literal)))
579 (or (reftex-nearest-match (regexp-quote literal) len) 889 (or (reftex-nearest-match (regexp-quote literal) len)
580 (reftex-nearest-match 890 (reftex-nearest-match
581 (reftex-make-regexp-allow-for-ctrl-m literal) len) 891 (reftex-make-regexp-allow-for-ctrl-m literal) len)
582 (reftex-nearest-match 892 (reftex-nearest-match
583 (reftex-make-desperate-section-regexp literal) len))))) 893 (reftex-make-desperate-section-regexp literal) len)))))
584 (t (message reftex-no-follow-message) nil)))) 894 (t (message reftex-no-follow-message) nil))))
585 (when match 895 (when match
586 (goto-char (match-beginning 0)) 896 (goto-char (match-beginning 0))
587 (if (not (= (point) (point-max))) (recenter 1)) 897 (if (not (= (point) (point-max))) (recenter 1))
588 (reftex-highlight 0 (match-beginning 0) (match-end 0) (current-buffer))) 898 (reftex-highlight 0 (match-beginning 0) (match-end 0) (current-buffer)))
589 match)) 899 match))
601 (setq new (concat new "[^\n\r]*[\n\r]")) 911 (setq new (concat new "[^\n\r]*[\n\r]"))
602 (setq new (concat new "[^\n\r]*" (match-string 3 old)))) 912 (setq new (concat new "[^\n\r]*" (match-string 3 old))))
603 (setq old (substring old (match-end 0)))) 913 (setq old (substring old (match-end 0))))
604 new)) 914 new))
605 915
916 ;; Auto recentering of TOC window
606 917
607 (defun reftex-recenter-toc-when-idle () 918 (defun reftex-recenter-toc-when-idle ()
608 (and (> (buffer-size) 5) 919 (and (> (buffer-size) 5)
609 reftex-mode 920 reftex-mode
610 (not (active-minibuffer-window)) 921 (not (active-minibuffer-window))
611 (fboundp 'reftex-toc-mode) 922 (fboundp 'reftex-toc-mode)
612 (get-buffer-window "*toc*") 923 (get-buffer-window "*toc*" 'visible)
613 (string= reftex-last-toc-master (reftex-TeX-master-file)) 924 (string= reftex-last-toc-master (reftex-TeX-master-file))
614 (reftex-toc-recenter))) 925 (let (current-prefix-arg)
926 (reftex-toc-recenter))))
615 927
616 (defun reftex-toggle-auto-toc-recenter () 928 (defun reftex-toggle-auto-toc-recenter ()
617 "Toggle the automatic recentering of the toc window. 929 "Toggle the automatic recentering of the toc window.
618 When active, leaving point idle will make the toc window jump to the correct 930 When active, leaving point idle will make the toc window jump to the correct
619 section." 931 section."
620 (interactive) 932 (interactive)
621 (if reftex-toc-auto-recenter-timer 933 (if reftex-toc-auto-recenter-timer
622 (progn 934 (progn
623 (if (featurep 'xemacs) 935 (if (featurep 'xemacs)
624 (delete-itimer reftex-toc-auto-recenter-timer) 936 (delete-itimer reftex-toc-auto-recenter-timer)
625 (cancel-timer reftex-toc-auto-recenter-timer)) 937 (cancel-timer reftex-toc-auto-recenter-timer))
626 (setq reftex-toc-auto-recenter-timer nil) 938 (setq reftex-toc-auto-recenter-timer nil)
627 (message "Automatic recentering of toc buffer was turned off")) 939 (message "Automatic recentering of toc windwo was turned off"))
628 (setq reftex-toc-auto-recenter-timer 940 (setq reftex-toc-auto-recenter-timer
629 (if (featurep 'xemacs) 941 (if (featurep 'xemacs)
630 (start-itimer "RefTeX Idle Timer for recenter" 942 (start-itimer "RefTeX Idle Timer for recenter"
631 'reftex-recenter-toc-when-idle 943 'reftex-recenter-toc-when-idle
632 reftex-idle-time reftex-idle-time t) 944 reftex-idle-time reftex-idle-time t)
633 (run-with-idle-timer 945 (run-with-idle-timer
634 reftex-idle-time t 'reftex-recenter-toc-when-idle))) 946 reftex-idle-time t 'reftex-recenter-toc-when-idle)))
635 (message "Automatic recentering of toc window was turned on"))) 947 (message "Automatic recentering of toc window was turned on")))
636 948
949 (defun reftex-toc-toggle-dedicated-frame ()
950 "Toggle the display of a separate frame for the TOC.
951 This frame is not used by the `reftex-toc' commands, but it is useful to
952 always show the current section in connection with the option
953 `reftex-auto-recenter-toc'."
954 (interactive)
955 (catch 'exit
956 (let* ((frames (frame-list)) frame
957 (get-frame-prop-func (if (fboundp 'frame-property)
958 'frame-property
959 'frame-parameter)))
960 (while (setq frame (pop frames))
961 (if (equal (funcall get-frame-prop-func frame 'name)
962 "RefTeX TOC Frame")
963 (progn
964 (delete-frame frame)
965 (throw 'exit nil))))
966 (reftex-make-separate-toc-frame))))
967
968 (defun reftex-make-separate-toc-frame ()
969 ;; Create a new fame showing only the toc buffer.
970 (let ((current-frame (selected-frame))
971 (current-window (selected-window))
972 (current-toc-window (get-buffer-window "*toc*" 'visible))
973 current-toc-frame)
974 (if (and current-toc-window
975 (not (equal (selected-frame) (window-frame current-toc-window))))
976 nil
977 (setq current-toc-frame
978 (make-frame
979 '((name . "RefTeX TOC Frame")
980 (height . 20) (width . 60)
981 (unsplittable . t)
982 (minibuffer . nil)
983 (default-toolbar-visible-p . nil)
984 (menubar-visible-p . nil)
985 (horizontal-scrollbar-visible-p . nil))))
986 (select-frame current-toc-frame)
987 (switch-to-buffer "*toc*")
988 (select-frame current-frame)
989 (if (fboundp 'focus-frame) (focus-frame current-frame)
990 (if (fboundp 'x-focus-frame) (x-focus-frame current-frame)))
991 (select-window current-window)
992 (when (eq reftex-auto-recenter-toc 'frame)
993 (unless reftex-toc-auto-recenter-timer
994 (reftex-toggle-auto-toc-recenter))
995 (add-hook 'delete-frame-hook 'reftex-toc-delete-frame-hook)))))
996
997 (defun reftex-toc-delete-frame-hook (frame)
998 (if (and reftex-toc-auto-recenter-timer
999 (reftex-toc-dframe-p frame))
1000 (progn
1001 (reftex-toggle-auto-toc-recenter))))
637 1002
638 ;; Table of Contents map 1003 ;; Table of Contents map
639 (define-key reftex-toc-map (if (featurep 'xemacs) [(button2)] [(mouse-2)]) 1004 (define-key reftex-toc-map (if (featurep 'xemacs) [(button2)] [(mouse-2)])
640 'reftex-toc-mouse-goto-line-and-hide) 1005 'reftex-toc-mouse-goto-line-and-hide)
641 1006
643 'next-line 'reftex-toc-next reftex-toc-map global-map) 1008 'next-line 'reftex-toc-next reftex-toc-map global-map)
644 (substitute-key-definition 1009 (substitute-key-definition
645 'previous-line 'reftex-toc-previous reftex-toc-map global-map) 1010 'previous-line 'reftex-toc-previous reftex-toc-map global-map)
646 1011
647 (loop for x in 1012 (loop for x in
648 '(("n" . reftex-toc-next) 1013 '(("n" . reftex-toc-next)
649 ("p" . reftex-toc-previous) 1014 ("p" . reftex-toc-previous)
650 ("?" . reftex-toc-show-help) 1015 ("?" . reftex-toc-show-help)
651 (" " . reftex-toc-view-line) 1016 (" " . reftex-toc-view-line)
652 ("\C-m" . reftex-toc-goto-line-and-hide) 1017 ("\C-m" . reftex-toc-goto-line-and-hide)
653 ("\C-i" . reftex-toc-goto-line) 1018 ("\C-i" . reftex-toc-goto-line)
654 ("\C-c>". reftex-toc-display-index) 1019 ("\C-c>" . reftex-toc-display-index)
655 ("r" . reftex-toc-rescan) 1020 ("r" . reftex-toc-rescan)
656 ("R" . reftex-toc-Rescan) 1021 ("R" . reftex-toc-Rescan)
657 ("g" . revert-buffer) 1022 ("g" . revert-buffer)
658 ("q" . reftex-toc-quit) 1023 ("q" . reftex-toc-quit);
659 ("k" . reftex-toc-quit-and-kill) 1024 ("k" . reftex-toc-quit-and-kill)
660 ("f" . reftex-toc-toggle-follow) 1025 ("f" . reftex-toc-toggle-follow);
661 ("a" . reftex-toggle-auto-toc-recenter) 1026 ("a" . reftex-toggle-auto-toc-recenter)
662 ("F" . reftex-toc-toggle-file-boundary) 1027 ("d" . reftex-toc-toggle-dedicated-frame)
663 ("i" . reftex-toc-toggle-index) 1028 ("F" . reftex-toc-toggle-file-boundary)
664 ("l" . reftex-toc-toggle-labels) 1029 ("i" . reftex-toc-toggle-index)
665 ("t" . reftex-toc-max-level) 1030 ("l" . reftex-toc-toggle-labels)
666 ("c" . reftex-toc-toggle-context) 1031 ("t" . reftex-toc-max-level)
667 ("%" . reftex-toc-toggle-commented) 1032 ("c" . reftex-toc-toggle-context)
668 ("x" . reftex-toc-external) 1033 ; ("%" . reftex-toc-toggle-commented)
669 ("z" . reftex-toc-jump) 1034 ("\M-%" . reftex-toc-rename-label)
670 ("." . reftex-toc-show-calling-point) 1035 ("x" . reftex-toc-external)
671 ("\C-c\C-n" . reftex-toc-next-heading) 1036 ("z" . reftex-toc-jump)
672 ("\C-c\C-p" . reftex-toc-previous-heading)) 1037 ("." . reftex-toc-show-calling-point)
1038 ("\C-c\C-n" . reftex-toc-next-heading)
1039 ("\C-c\C-p" . reftex-toc-previous-heading)
1040 (">" . reftex-toc-demote)
1041 ("<" . reftex-toc-promote))
673 do (define-key reftex-toc-map (car x) (cdr x))) 1042 do (define-key reftex-toc-map (car x) (cdr x)))
674 1043
675 (loop for key across "0123456789" do 1044 (loop for key across "0123456789" do
676 (define-key reftex-toc-map (vector (list key)) 'digit-argument)) 1045 (define-key reftex-toc-map (vector (list key)) 'digit-argument))
677 (define-key reftex-toc-map "-" 'negative-argument) 1046 (define-key reftex-toc-map "-" 'negative-argument)
678 1047
679 (easy-menu-define 1048 (easy-menu-define
680 reftex-toc-menu reftex-toc-map 1049 reftex-toc-menu reftex-toc-map
681 "Menu for Table of Contents buffer" 1050 "Menu for Table of Contents buffer"
682 '("TOC" 1051 '("TOC"
683 ["Show Location" reftex-toc-view-line t] 1052 ["Show Location" reftex-toc-view-line t]
684 ["Go To Location" reftex-toc-goto-line t] 1053 ["Go To Location" reftex-toc-goto-line t]
685 ["Exit & Go To Location" reftex-toc-goto-line-and-hide t] 1054 ["Exit & Go To Location" reftex-toc-goto-line-and-hide t]
686 ["Index" reftex-toc-display-index t] 1055 ["Show Calling Point" reftex-toc-show-calling-point t]
687 ["Quit" reftex-toc-quit t] 1056 ["Quit" reftex-toc-quit t]
688 "--" 1057 "--"
1058 ("Edit"
1059 ["Promote" reftex-toc-promote t]
1060 ["Demote" reftex-toc-demote t]
1061 ["Rename Label" reftex-toc-rename-label t])
1062 "--"
1063 ["Index" reftex-toc-display-index t]
689 ["External Document TOC " reftex-toc-external t] 1064 ["External Document TOC " reftex-toc-external t]
690 "--" 1065 "--"
691 ("Update" 1066 ("Update"
692 ["Rebuilt *toc* Buffer" revert-buffer t] 1067 ["Rebuilt *toc* Buffer" revert-buffer t]
693 ["Rescan One File" reftex-toc-rescan reftex-enable-partial-scans] 1068 ["Rescan One File" reftex-toc-rescan reftex-enable-partial-scans]
701 ["Index Entries" reftex-toc-toggle-index :style toggle 1076 ["Index Entries" reftex-toc-toggle-index :style toggle
702 :selected reftex-toc-include-index-entries] 1077 :selected reftex-toc-include-index-entries]
703 ["Context" reftex-toc-toggle-context :style toggle 1078 ["Context" reftex-toc-toggle-context :style toggle
704 :selected reftex-toc-include-context] 1079 :selected reftex-toc-include-context]
705 "--" 1080 "--"
706 ["Follow Mode" reftex-toc-toggle-follow :style toggle 1081 ["Follow Mode" reftex-toc-toggle-follow :style toggle
707 :selected reftex-toc-follow-mode]) 1082 :selected reftex-toc-follow-mode]
1083 ["Auto Recenter" reftex-toggle-auto-toc-recenter :style toggle
1084 :selected reftex-toc-auto-recenter-timer]
1085 ["Dedicated Frame" reftex-toc-toggle-dedicated-frame t])
708 "--" 1086 "--"
709 ["Help" reftex-toc-show-help t])) 1087 ["Help" reftex-toc-show-help t]))
710 1088
711 1089
712 ;;; reftex-toc.el ends here 1090 ;;; reftex-toc.el ends here