| 1 |
|
|---|
| 2 |
|
|---|
| 3 |
|
|---|
| 4 |
|
|---|
| 5 |
|
|---|
| 6 |
|
|---|
| 7 |
|
|---|
| 8 |
|
|---|
| 9 |
|
|---|
| 10 |
|
|---|
| 11 |
|
|---|
| 12 |
|
|---|
| 13 |
|
|---|
| 14 |
|
|---|
| 15 |
|
|---|
| 16 |
|
|---|
| 17 |
|
|---|
| 18 |
|
|---|
| 19 |
|
|---|
| 20 |
|
|---|
| 21 |
|
|---|
| 22 |
|
|---|
| 23 |
|
|---|
| 24 |
|
|---|
| 25 |
|
|---|
| 26 |
|
|---|
| 27 |
|
|---|
| 28 |
|
|---|
| 29 |
|
|---|
| 30 |
(eval-when-compile (require 'cl)) |
|---|
| 31 |
(provide 'reftex-toc) |
|---|
| 32 |
(require 'reftex) |
|---|
| 33 |
|
|---|
| 34 |
|
|---|
| 35 |
(defvar reftex-toc-map (make-sparse-keymap) |
|---|
| 36 |
"Keymap used for *toc* buffer.") |
|---|
| 37 |
|
|---|
| 38 |
(defvar reftex-toc-menu) |
|---|
| 39 |
(eval-when-compile (defvar zmacs-regions)) |
|---|
| 40 |
(defvar reftex-last-window-height nil) |
|---|
| 41 |
(defvar reftex-last-window-width nil) |
|---|
| 42 |
(defvar reftex-toc-include-labels-indicator nil) |
|---|
| 43 |
(defvar reftex-toc-include-index-indicator nil) |
|---|
| 44 |
(defvar reftex-toc-max-level-indicator nil) |
|---|
| 45 |
|
|---|
| 46 |
(defun reftex-toc-mode () |
|---|
| 47 |
"Major mode for managing Table of Contents for LaTeX files. |
|---|
| 48 |
This buffer was created with RefTeX. |
|---|
| 49 |
Press `?' for a summary of important key bindings. |
|---|
| 50 |
|
|---|
| 51 |
Here are all local bindings. |
|---|
| 52 |
|
|---|
| 53 |
\\{reftex-toc-map}" |
|---|
| 54 |
(interactive) |
|---|
| 55 |
(kill-all-local-variables) |
|---|
| 56 |
(setq major-mode 'reftex-toc-mode |
|---|
| 57 |
mode-name "TOC") |
|---|
| 58 |
(use-local-map reftex-toc-map) |
|---|
| 59 |
(set (make-local-variable 'transient-mark-mode) t) |
|---|
| 60 |
(set (make-local-variable 'zmacs-regions) t) |
|---|
| 61 |
(set (make-local-variable 'revert-buffer-function) 'reftex-toc-revert) |
|---|
| 62 |
(set (make-local-variable 'reftex-toc-include-labels-indicator) "") |
|---|
| 63 |
(set (make-local-variable 'reftex-toc-max-level-indicator) |
|---|
| 64 |
(if (= reftex-toc-max-level 100) |
|---|
| 65 |
"ALL" |
|---|
| 66 |
(int-to-string reftex-toc-max-level))) |
|---|
| 67 |
(setq mode-line-format |
|---|
| 68 |
(list "---- " 'mode-line-buffer-identification |
|---|
| 69 |
" " 'global-mode-string " (" mode-name ")" |
|---|
| 70 |
" L<" 'reftex-toc-include-labels-indicator ">" |
|---|
| 71 |
" I<" 'reftex-toc-include-index-indicator ">" |
|---|
| 72 |
" T<" 'reftex-toc-max-level-indicator ">" |
|---|
| 73 |
" -%-")) |
|---|
| 74 |
(setq truncate-lines t) |
|---|
| 75 |
(when (featurep 'xemacs) |
|---|
| 76 |
|
|---|
| 77 |
(make-local-hook 'post-command-hook) |
|---|
| 78 |
(make-local-hook 'pre-command-hook)) |
|---|
| 79 |
(make-local-variable 'reftex-last-follow-point) |
|---|
| 80 |
(add-hook 'post-command-hook 'reftex-toc-post-command-hook nil t) |
|---|
| 81 |
(add-hook 'pre-command-hook 'reftex-toc-pre-command-hook nil t) |
|---|
| 82 |
(easy-menu-add reftex-toc-menu reftex-toc-map) |
|---|
| 83 |
(run-hooks 'reftex-toc-mode-hook)) |
|---|
| 84 |
|
|---|
| 85 |
(defvar reftex-last-toc-file nil |
|---|
| 86 |
"Stores the file name from which `reftex-toc' was called. For redo command.") |
|---|
| 87 |
|
|---|
| 88 |
|
|---|
| 89 |
(defvar reftex-toc-return-marker (make-marker) |
|---|
| 90 |
"Marker which makes it possible to return from toc to old position.") |
|---|
| 91 |
|
|---|
| 92 |
(defconst reftex-toc-help |
|---|
| 93 |
" AVAILABLE KEYS IN TOC BUFFER |
|---|
| 94 |
============================ |
|---|
| 95 |
n / p next-line / previous-line |
|---|
| 96 |
SPC Show the corresponding location of the LaTeX document. |
|---|
| 97 |
TAB Goto the location and keep the *toc* window. |
|---|
| 98 |
RET Goto the location and hide the *toc* window (also on mouse-2). |
|---|
| 99 |
< / > Promote / Demote section, or all sections in region. |
|---|
| 100 |
C-c > Display Index. With prefix arg, restrict index to current section. |
|---|
| 101 |
q / k Hide/Kill *toc* buffer, return to position of reftex-toc command. |
|---|
| 102 |
l i c F Toggle display of [l]abels, [i]ndex, [c]ontext, [F]ile borders. |
|---|
| 103 |
t Change maximum toc depth (e.g. `3 t' hides levels greater than 3). |
|---|
| 104 |
f / g Toggle follow mode / Refresh *toc* buffer. |
|---|
| 105 |
a / d Toggle auto recenter / Toggle dedicated frame |
|---|
| 106 |
r / C-u r Reparse the LaTeX document / Reparse entire LaTeX document. |
|---|
| 107 |
. In other window, show position from where `reftex-toc' was called. |
|---|
| 108 |
M-% Global search and replace to rename label at point. |
|---|
| 109 |
x Switch to TOC of external document (with LaTeX package `xr'). |
|---|
| 110 |
z Jump to a specific section (e.g. '3 z' goes to section 3).") |
|---|
| 111 |
|
|---|
| 112 |
(defun reftex-toc (&optional rebuild reuse) |
|---|
| 113 |
"Show the table of contents for the current document. |
|---|
| 114 |
When called with a raw C-u prefix, rescan the document first." |
|---|
| 115 |
|
|---|
| 116 |
|
|---|
| 117 |
|
|---|
| 118 |
|
|---|
| 119 |
(interactive) |
|---|
| 120 |
|
|---|
| 121 |
(if (or (not (string= reftex-last-toc-master (reftex-TeX-master-file))) |
|---|
| 122 |
current-prefix-arg) |
|---|
| 123 |
(reftex-erase-buffer "*toc*")) |
|---|
| 124 |
|
|---|
| 125 |
(setq reftex-last-toc-file (buffer-file-name)) |
|---|
| 126 |
(setq reftex-last-toc-master (reftex-TeX-master-file)) |
|---|
| 127 |
|
|---|
| 128 |
(set-marker reftex-toc-return-marker (point)) |
|---|
| 129 |
|
|---|
| 130 |
|
|---|
| 131 |
(if reftex-toc-follow-mode |
|---|
| 132 |
(setq reftex-toc-follow-mode 1)) |
|---|
| 133 |
|
|---|
| 134 |
(and reftex-toc-include-index-entries |
|---|
| 135 |
(reftex-ensure-index-support)) |
|---|
| 136 |
(or reftex-support-index |
|---|
| 137 |
(setq reftex-toc-include-index-entries nil)) |
|---|
| 138 |
|
|---|
| 139 |
|
|---|
| 140 |
(reftex-access-scan-info current-prefix-arg) |
|---|
| 141 |
|
|---|
| 142 |
(let* ((this-buf (current-buffer)) |
|---|
| 143 |
(docstruct-symbol reftex-docstruct-symbol) |
|---|
| 144 |
(xr-data (assq 'xr (symbol-value reftex-docstruct-symbol))) |
|---|
| 145 |
(xr-alist (cons (cons "" (buffer-file-name)) (nth 1 xr-data))) |
|---|
| 146 |
(here-I-am (if (boundp 'reftex-rebuilding-toc) |
|---|
| 147 |
(get 'reftex-toc :reftex-data) |
|---|
| 148 |
(car (reftex-where-am-I)))) |
|---|
| 149 |
(unsplittable (if (fboundp 'frame-property) |
|---|
| 150 |
(frame-property (selected-frame) 'unsplittable) |
|---|
| 151 |
(frame-parameter (selected-frame) 'unsplittable))) |
|---|
| 152 |
offset toc-window) |
|---|
| 153 |
|
|---|
| 154 |
(if (setq toc-window (get-buffer-window |
|---|
| 155 |
"*toc*" |
|---|
| 156 |
(if reuse 'visible))) |
|---|
| 157 |
(select-window toc-window) |
|---|
| 158 |
(when (or (not reftex-toc-keep-other-windows) |
|---|
| 159 |
(< (window-height) (* 2 window-min-height))) |
|---|
| 160 |
(delete-other-windows)) |
|---|
| 161 |
|
|---|
| 162 |
(setq reftex-last-window-width (window-width) |
|---|
| 163 |
reftex-last-window-height (window-height)) |
|---|
| 164 |
|
|---|
| 165 |
(unless unsplittable |
|---|
| 166 |
(if reftex-toc-split-windows-horizontally |
|---|
| 167 |
(split-window-horizontally |
|---|
| 168 |
(floor (* (window-width) |
|---|
| 169 |
reftex-toc-split-windows-fraction))) |
|---|
| 170 |
(split-window-vertically |
|---|
| 171 |
(floor (* (window-height) |
|---|
| 172 |
reftex-toc-split-windows-fraction))))) |
|---|
| 173 |
|
|---|
| 174 |
(let ((default-major-mode 'reftex-toc-mode)) |
|---|
| 175 |
(switch-to-buffer "*toc*"))) |
|---|
| 176 |
|
|---|
| 177 |
(or (eq major-mode 'reftex-toc-mode) (reftex-toc-mode)) |
|---|
| 178 |
(set (make-local-variable 'reftex-docstruct-symbol) docstruct-symbol) |
|---|
| 179 |
(setq reftex-toc-include-labels-indicator |
|---|
| 180 |
(if (eq reftex-toc-include-labels t) |
|---|
| 181 |
"ALL" |
|---|
| 182 |
reftex-toc-include-labels)) |
|---|
| 183 |
(setq reftex-toc-include-index-indicator |
|---|
| 184 |
(if (eq reftex-toc-include-index-entries t) |
|---|
| 185 |
"ALL" |
|---|
| 186 |
reftex-toc-include-index-entries)) |
|---|
| 187 |
|
|---|
| 188 |
(cond |
|---|
| 189 |
((= (buffer-size) 0) |
|---|
| 190 |
|
|---|
| 191 |
(message "Building *toc* buffer...") |
|---|
| 192 |
|
|---|
| 193 |
(setq buffer-read-only nil) |
|---|
| 194 |
(insert (format |
|---|
| 195 |
"TABLE-OF-CONTENTS on %s |
|---|
| 196 |
SPC=view TAB=goto RET=goto+hide [q]uit [r]escan [l]abels [f]ollow [x]r [?]Help |
|---|
| 197 |
------------------------------------------------------------------------------ |
|---|
| 198 |
" (abbreviate-file-name reftex-last-toc-master))) |
|---|
| 199 |
|
|---|
| 200 |
(if (reftex-use-fonts) |
|---|
| 201 |
(put-text-property (point-min) (point) 'face reftex-toc-header-face)) |
|---|
| 202 |
(put-text-property (point-min) (point) 'intangible t) |
|---|
| 203 |
(put-text-property (point-min) (1+ (point-min)) 'xr-alist xr-alist) |
|---|
| 204 |
|
|---|
| 205 |
(setq offset |
|---|
| 206 |
(reftex-insert-docstruct |
|---|
| 207 |
this-buf |
|---|
| 208 |
t |
|---|
| 209 |
reftex-toc-include-labels |
|---|
| 210 |
reftex-toc-include-index-entries |
|---|
| 211 |
reftex-toc-include-file-boundaries |
|---|
| 212 |
reftex-toc-include-context |
|---|
| 213 |
nil |
|---|
| 214 |
nil |
|---|
| 215 |
here-I-am |
|---|
| 216 |
"" |
|---|
| 217 |
t |
|---|
| 218 |
)) |
|---|
| 219 |
|
|---|
| 220 |
(run-hooks 'reftex-display-copied-context-hook) |
|---|
| 221 |
(message "Building *toc* buffer...done.") |
|---|
| 222 |
(setq buffer-read-only t)) |
|---|
| 223 |
(t |
|---|
| 224 |
|
|---|
| 225 |
(setq offset |
|---|
| 226 |
(or (reftex-get-offset this-buf here-I-am |
|---|
| 227 |
(if reftex-toc-include-labels " " nil) |
|---|
| 228 |
t |
|---|
| 229 |
reftex-toc-include-index-entries |
|---|
| 230 |
reftex-toc-include-file-boundaries) |
|---|
| 231 |
(reftex-last-assoc-before-elt |
|---|
| 232 |
'toc here-I-am |
|---|
| 233 |
(symbol-value reftex-docstruct-symbol)))) |
|---|
| 234 |
(put 'reftex-toc :reftex-line 3) |
|---|
| 235 |
(goto-line 3) |
|---|
| 236 |
(beginning-of-line))) |
|---|
| 237 |
|
|---|
| 238 |
|
|---|
| 239 |
(reftex-find-start-point (point) offset (get 'reftex-toc :reftex-line)) |
|---|
| 240 |
(setq reftex-last-follow-point (point)))) |
|---|
| 241 |
|
|---|
| 242 |
(defun reftex-toc-recenter (&optional arg) |
|---|
| 243 |
"Display the TOC window and highlight line corresponding to current position." |
|---|
| 244 |
(interactive "P") |
|---|
| 245 |
(let ((buf (current-buffer)) |
|---|
| 246 |
(frame (selected-frame))) |
|---|
| 247 |
(reftex-toc arg t) |
|---|
| 248 |
(if (= (count-lines 1 (point)) 2) |
|---|
| 249 |
(let ((current-prefix-arg nil)) |
|---|
| 250 |
(select-window (get-buffer-window buf frame)) |
|---|
| 251 |
(reftex-toc nil t))) |
|---|
| 252 |
(and (> (point) 1) |
|---|
| 253 |
(not (get-text-property (point) 'intangible)) |
|---|
| 254 |
(memq reftex-highlight-selection '(cursor both)) |
|---|
| 255 |
(reftex-highlight 2 |
|---|
| 256 |
(or (previous-single-property-change |
|---|
| 257 |
(min (point-max) (1+ (point))) :data) |
|---|
| 258 |
(point-min)) |
|---|
| 259 |
(or (next-single-property-change (point) :data) |
|---|
| 260 |
(point-max)))) |
|---|
| 261 |
(select-window (get-buffer-window buf frame)))) |
|---|
| 262 |
|
|---|
| 263 |
(defun reftex-toc-pre-command-hook () |
|---|
| 264 |
|
|---|
| 265 |
(reftex-unhighlight 0) |
|---|
| 266 |
) |
|---|
| 267 |
|
|---|
| 268 |
(defun reftex-toc-post-command-hook () |
|---|
| 269 |
|
|---|
| 270 |
(when (get-text-property (point) :data) |
|---|
| 271 |
(put 'reftex-toc :reftex-data (get-text-property (point) :data)) |
|---|
| 272 |
(and (> (point) 1) |
|---|
| 273 |
(not (get-text-property (point) 'intangible)) |
|---|
| 274 |
(memq reftex-highlight-selection '(cursor both)) |
|---|
| 275 |
(reftex-highlight 2 |
|---|
| 276 |
(or (previous-single-property-change (1+ (point)) :data) |
|---|
| 277 |
(point-min)) |
|---|
| 278 |
(or (next-single-property-change (point) :data) |
|---|
| 279 |
(point-max))))) |
|---|
| 280 |
(if (integerp reftex-toc-follow-mode) |
|---|
| 281 |
|
|---|
| 282 |
(setq reftex-toc-follow-mode t) |
|---|
| 283 |
(and (not (reftex-toc-dframe-p)) |
|---|
| 284 |
reftex-toc-follow-mode |
|---|
| 285 |
(not (equal reftex-last-follow-point (point))) |
|---|
| 286 |
|
|---|
| 287 |
(setq reftex-last-follow-point (point)) |
|---|
| 288 |
(condition-case nil |
|---|
| 289 |
(reftex-toc-visit-location nil (not reftex-revisit-to-follow)) |
|---|
| 290 |
(error t))))) |
|---|
| 291 |
|
|---|
| 292 |
(defun reftex-re-enlarge () |
|---|
| 293 |
|
|---|
| 294 |
(if reftex-toc-split-windows-horizontally |
|---|
| 295 |
(enlarge-window-horizontally |
|---|
| 296 |
(max 0 (- (or reftex-last-window-width (window-width)) |
|---|
| 297 |
(window-width)))) |
|---|
| 298 |
(enlarge-window |
|---|
| 299 |
(max 0 (- (or reftex-last-window-height (window-height)) |
|---|
| 300 |
(window-height)))))) |
|---|
| 301 |
|
|---|
| 302 |
(defun reftex-toc-dframe-p (&optional frame error) |
|---|
| 303 |
|
|---|
| 304 |
|
|---|
| 305 |
(setq frame (or frame (selected-frame))) |
|---|
| 306 |
(let ((res (equal |
|---|
| 307 |
(if (fboundp 'frame-property) |
|---|
| 308 |
(frame-property frame 'name) |
|---|
| 309 |
(frame-parameter frame 'name)) |
|---|
| 310 |
"RefTeX TOC Frame"))) |
|---|
| 311 |
(if (and res error) |
|---|
| 312 |
(error "This frame is view-only. Use `C-c =' to create toc window for commands")) |
|---|
| 313 |
res)) |
|---|
| 314 |
|
|---|
| 315 |
(defun reftex-toc-show-help () |
|---|
| 316 |
"Show a summary of special key bindings." |
|---|
| 317 |
(interactive) |
|---|
| 318 |
(reftex-toc-dframe-p nil 'error) |
|---|
| 319 |
(with-output-to-temp-buffer "*RefTeX Help*" |
|---|
| 320 |
(princ reftex-toc-help)) |
|---|
| 321 |
(reftex-enlarge-to-fit "*RefTeX Help*" t) |
|---|
| 322 |
|
|---|
| 323 |
(if reftex-toc-follow-mode |
|---|
| 324 |
(setq reftex-toc-follow-mode 1))) |
|---|
| 325 |
|
|---|
| 326 |
(defun reftex-toc-next (&optional arg) |
|---|
| 327 |
"Move to next selectable item." |
|---|
| 328 |
(interactive "p") |
|---|
| 329 |
(if (boundp 'zmacs-region-stays) (setq zmacs-region-stays t)) |
|---|
| 330 |
(setq reftex-callback-fwd t) |
|---|
| 331 |
(or (eobp) (forward-char 1)) |
|---|
| 332 |
(goto-char (or (next-single-property-change (point) :data) |
|---|
| 333 |
(point)))) |
|---|
| 334 |
(defun reftex-toc-previous (&optional arg) |
|---|
| 335 |
"Move to previous selectable item." |
|---|
| 336 |
(interactive "p") |
|---|
| 337 |
(if (boundp 'zmacs-region-stays) (setq zmacs-region-stays t)) |
|---|
| 338 |
(setq reftex-callback-fwd nil) |
|---|
| 339 |
(goto-char (or (previous-single-property-change (point) :data) |
|---|
| 340 |
(point)))) |
|---|
| 341 |
(defun reftex-toc-next-heading (&optional arg) |
|---|
| 342 |
"Move to next table of contentes line." |
|---|
| 343 |
(interactive "p") |
|---|
| 344 |
(if (boundp 'zmacs-region-stays) (setq zmacs-region-stays t)) |
|---|
| 345 |
(end-of-line) |
|---|
| 346 |
(re-search-forward "^ " nil t arg) |
|---|
| 347 |
(beginning-of-line)) |
|---|
| 348 |
(defun reftex-toc-previous-heading (&optional arg) |
|---|
| 349 |
"Move to previous table of contentes line." |
|---|
| 350 |
(interactive "p") |
|---|
| 351 |
(if (boundp 'zmacs-region-stays) (setq zmacs-region-stays t)) |
|---|
| 352 |
(re-search-backward "^ " nil t arg)) |
|---|
| 353 |
(defun reftex-toc-toggle-follow () |
|---|
| 354 |
"Toggle follow (other window follows with context)." |
|---|
| 355 |
(interactive) |
|---|
| 356 |
(setq reftex-last-follow-point -1) |
|---|
| 357 |
(setq reftex-toc-follow-mode (not reftex-toc-follow-mode))) |
|---|
| 358 |
(defun reftex-toc-toggle-file-boundary () |
|---|
| 359 |
"Toggle inclusion of file boundaries in *toc* buffer." |
|---|
| 360 |
(interactive) |
|---|
| 361 |
(setq reftex-toc-include-file-boundaries |
|---|
| 362 |
(not reftex-toc-include-file-boundaries)) |
|---|
| 363 |
(reftex-toc-revert)) |
|---|
| 364 |
(defun reftex-toc-toggle-labels (arg) |
|---|
| 365 |
"Toggle inclusion of labels in *toc* buffer. |
|---|
| 366 |
With prefix ARG, prompt for a label type and include only labels of |
|---|
| 367 |
that specific type." |
|---|
| 368 |
(interactive "P") |
|---|
| 369 |
(setq reftex-toc-include-labels |
|---|
| 370 |
(if arg (reftex-query-label-type) |
|---|
| 371 |
(not reftex-toc-include-labels))) |
|---|
| 372 |
(reftex-toc-revert)) |
|---|
| 373 |
(defun reftex-toc-toggle-index (arg) |
|---|
| 374 |
"Toggle inclusion of index in *toc* buffer. |
|---|
| 375 |
With prefix arg, prompt for an index tag and include only entries of that |
|---|
| 376 |
specific index." |
|---|
| 377 |
(interactive "P") |
|---|
| 378 |
(setq reftex-toc-include-index-entries |
|---|
| 379 |
(if arg (reftex-index-select-tag) |
|---|
| 380 |
(not reftex-toc-include-index-entries))) |
|---|
| 381 |
(reftex-toc-revert)) |
|---|
| 382 |
(defun reftex-toc-toggle-context () |
|---|
| 383 |
"Toggle inclusion of label context in *toc* buffer. |
|---|
| 384 |
Label context is only displayed when the labels are there as well." |
|---|
| 385 |
(interactive) |
|---|
| 386 |
(setq reftex-toc-include-context (not reftex-toc-include-context)) |
|---|
| 387 |
(reftex-toc-revert)) |
|---|
| 388 |
(defun reftex-toc-max-level (arg) |
|---|
| 389 |
"Set the maximum level of toc lines in this buffer to value of prefix ARG. |
|---|
| 390 |
When no prefix is given, set the max level to a large number, so that all |
|---|
| 391 |
levels are shown. For eaxample, to set the level to 3, type `3 m'." |
|---|
| 392 |
(interactive "P") |
|---|
| 393 |
(setq reftex-toc-max-level (if arg |
|---|
| 394 |
(prefix-numeric-value arg) |
|---|
| 395 |
100)) |
|---|
| 396 |
(setq reftex-toc-max-level-indicator |
|---|
| 397 |
(if arg (int-to-string reftex-toc-max-level) "ALL")) |
|---|
| 398 |
(reftex-toc-revert)) |
|---|
| 399 |
(defun reftex-toc-view-line () |
|---|
| 400 |
"View document location in other window." |
|---|
| 401 |
(interactive) |
|---|
| 402 |
(reftex-toc-dframe-p nil 'error) |
|---|
| 403 |
(reftex-toc-visit-location)) |
|---|
| 404 |
(defun reftex-toc-goto-line-and-hide () |
|---|
| 405 |
"Go to document location in other window. Hide the *toc* window." |
|---|
| 406 |
(interactive) |
|---|
| 407 |
(reftex-toc-dframe-p nil 'error) |
|---|
| 408 |
(reftex-toc-visit-location 'hide)) |
|---|
| 409 |
(defun reftex-toc-goto-line () |
|---|
| 410 |
"Go to document location in other window. *toc* window stays." |
|---|
| 411 |
(interactive) |
|---|
| 412 |
(reftex-toc-dframe-p nil 'error) |
|---|
| 413 |
(reftex-toc-visit-location t)) |
|---|
| 414 |
(defun reftex-toc-mouse-goto-line-and-hide (ev) |
|---|
| 415 |
"Go to document location in other window. Hide the *toc* window." |
|---|
| 416 |
(interactive "e") |
|---|
| 417 |
(mouse-set-point ev) |
|---|
| 418 |
(reftex-toc-dframe-p nil 'error) |
|---|
| 419 |
(reftex-toc-visit-location 'hide)) |
|---|
| 420 |
(defun reftex-toc-show-calling-point () |
|---|
| 421 |
"Show point where reftex-toc was called from." |
|---|
| 422 |
(interactive) |
|---|
| 423 |
(reftex-toc-dframe-p nil 'error) |
|---|
| 424 |
(let ((this-window (selected-window))) |
|---|
| 425 |
(unwind-protect |
|---|
| 426 |
(progn |
|---|
| 427 |
(switch-to-buffer-other-window |
|---|
| 428 |
(marker-buffer reftex-toc-return-marker)) |
|---|
| 429 |
(goto-char (marker-position reftex-toc-return-marker)) |
|---|
| 430 |
(recenter '(4))) |
|---|
| 431 |
(select-window this-window)))) |
|---|
| 432 |
(defun reftex-toc-quit () |
|---|
| 433 |
"Hide the *toc* window and do not move point. |
|---|
| 434 |
If the toc window is the only window on the dedicated TOC frame, the frame |
|---|
| 435 |
is destroyed." |
|---|
| 436 |
(interactive) |
|---|
| 437 |
(if (and (one-window-p) |
|---|
| 438 |
(reftex-toc-dframe-p) |
|---|
| 439 |
(> (length (frame-list)) 1)) |
|---|
| 440 |
(delete-frame) |
|---|
| 441 |
(or (one-window-p) (delete-window)) |
|---|
| 442 |
(switch-to-buffer (marker-buffer reftex-toc-return-marker)) |
|---|
| 443 |
(reftex-re-enlarge) |
|---|
| 444 |
(goto-char (or (marker-position reftex-toc-return-marker) (point))))) |
|---|
| 445 |
(defun reftex-toc-quit-and-kill () |
|---|
| 446 |
"Kill the *toc* buffer." |
|---|
| 447 |
(interactive) |
|---|
| 448 |
(kill-buffer "*toc*") |
|---|
| 449 |
(or (one-window-p) (delete-window)) |
|---|
| 450 |
(switch-to-buffer (marker-buffer reftex-toc-return-marker)) |
|---|
| 451 |
(reftex-re-enlarge) |
|---|
| 452 |
(goto-char (marker-position reftex-toc-return-marker))) |
|---|
| 453 |
(defun reftex-toc-display-index (&optional arg) |
|---|
| 454 |
"Display the index buffer for the current document. |
|---|
| 455 |
This works just like `reftex-display-index' from a LaTeX buffer. |
|---|
| 456 |
With prefix arg 1, restrict index to the section at point." |
|---|
| 457 |
(interactive "P") |
|---|
| 458 |
(reftex-toc-dframe-p nil 'error) |
|---|
| 459 |
(let ((data (get-text-property (point) :data)) |
|---|
| 460 |
(docstruct (symbol-value reftex-docstruct-symbol)) |
|---|
| 461 |
bor eor restr) |
|---|
| 462 |
(when (equal arg 2) |
|---|
| 463 |
(setq bor (reftex-last-assoc-before-elt 'toc data docstruct) |
|---|
| 464 |
eor (assoc 'toc (cdr (memq bor docstruct))) |
|---|
| 465 |
restr (list (nth 6 bor) bor eor))) |
|---|
| 466 |
(reftex-toc-goto-line) |
|---|
| 467 |
(reftex-display-index (if restr nil arg) restr))) |
|---|
| 468 |
|
|---|
| 469 |
|
|---|
| 470 |
(defun reftex-toc-rescan (&rest ignore) |
|---|
| 471 |
"Regenerate the *toc* buffer by reparsing file of section at point." |
|---|
| 472 |
(interactive) |
|---|
| 473 |
(if (and reftex-enable-partial-scans |
|---|
| 474 |
(null current-prefix-arg)) |
|---|
| 475 |
(let* ((data (get-text-property (point) :data)) |
|---|
| 476 |
(what (car data)) |
|---|
| 477 |
(file (cond ((eq what 'toc) (nth 3 data)) |
|---|
| 478 |
((memq what '(eof bof file-error)) (nth 1 data)) |
|---|
| 479 |
((stringp what) (nth 3 data)) |
|---|
| 480 |
((eq what 'index) (nth 3 data)))) |
|---|
| 481 |
(line (+ (count-lines (point-min) (point)) (if (bolp) 1 0)))) |
|---|
| 482 |
(if (not file) |
|---|
| 483 |
(error "Don't know which file to rescan. Try `C-u r'") |
|---|
| 484 |
(put 'reftex-toc :reftex-line line) |
|---|
| 485 |
(switch-to-buffer-other-window |
|---|
| 486 |
(reftex-get-file-buffer-force file)) |
|---|
| 487 |
(setq current-prefix-arg '(4)) |
|---|
| 488 |
(let ((reftex-rebuilding-toc t)) |
|---|
| 489 |
(reftex-toc)))) |
|---|
| 490 |
(reftex-toc-Rescan)) |
|---|
| 491 |
(reftex-kill-temporary-buffers)) |
|---|
| 492 |
|
|---|
| 493 |
(defun reftex-toc-Rescan (&rest ignore) |
|---|
| 494 |
"Regenerate the *toc* buffer by reparsing the entire document." |
|---|
| 495 |
(interactive) |
|---|
| 496 |
(let* ((line (+ (count-lines (point-min) (point)) (if (bolp) 1 0)))) |
|---|
| 497 |
(put 'reftex-toc :reftex-line line)) |
|---|
| 498 |
(switch-to-buffer-other-window |
|---|
| 499 |
(reftex-get-file-buffer-force reftex-last-toc-file)) |
|---|
| 500 |
(setq current-prefix-arg '(16)) |
|---|
| 501 |
(let ((reftex-rebuilding-toc t)) |
|---|
| 502 |
(reftex-toc))) |
|---|
| 503 |
|
|---|
| 504 |
(defun reftex-toc-revert (&rest ignore) |
|---|
| 505 |
"Regenerate the *toc* from the internal lists." |
|---|
| 506 |
(interactive) |
|---|
| 507 |
(let ((unsplittable |
|---|
| 508 |
(if (fboundp 'frame-property) |
|---|
| 509 |
(frame-property (selected-frame) 'unsplittable) |
|---|
| 510 |
(frame-parameter (selected-frame) 'unsplittable))) |
|---|
| 511 |
(reftex-rebuilding-toc t)) |
|---|
| 512 |
(if unsplittable |
|---|
| 513 |
(switch-to-buffer |
|---|
| 514 |
(reftex-get-file-buffer-force reftex-last-toc-file)) |
|---|
| 515 |
(switch-to-buffer-other-window |
|---|
| 516 |
(reftex-get-file-buffer-force reftex-last-toc-file)))) |
|---|
| 517 |
(reftex-erase-buffer "*toc*") |
|---|
| 518 |
(setq current-prefix-arg nil) |
|---|
| 519 |
(reftex-toc t)) |
|---|
| 520 |
|
|---|
| 521 |
(defun reftex-toc-external (&rest ignore) |
|---|
| 522 |
"Switch to table of contents of an external document." |
|---|
| 523 |
(interactive) |
|---|
| 524 |
(reftex-toc-dframe-p nil 'error) |
|---|
| 525 |
(let* ((old-buf (current-buffer)) |
|---|
| 526 |
(xr-alist (get-text-property 1 'xr-alist)) |
|---|
| 527 |
(xr-index (reftex-select-external-document |
|---|
| 528 |
xr-alist 0))) |
|---|
| 529 |
(switch-to-buffer-other-window (or (reftex-get-file-buffer-force |
|---|
| 530 |
(cdr (nth xr-index xr-alist))) |
|---|
| 531 |
(error "Cannot switch document"))) |
|---|
| 532 |
(reftex-toc) |
|---|
| 533 |
(if (equal old-buf (current-buffer)) |
|---|
| 534 |
(message "") |
|---|
| 535 |
(message "Switched document")))) |
|---|
| 536 |
|
|---|
| 537 |
(defun reftex-toc-jump (arg) |
|---|
| 538 |
"Jump to a specific section. E.g. '3 z' jumps to section 3. |
|---|
| 539 |
Useful for large TOC's." |
|---|
| 540 |
(interactive "P") |
|---|
| 541 |
(goto-char (point-min)) |
|---|
| 542 |
(re-search-forward |
|---|
| 543 |
(concat "^ *" (number-to-string (if (numberp arg) arg 1)) " ") |
|---|
| 544 |
nil t) |
|---|
| 545 |
(beginning-of-line)) |
|---|
| 546 |
|
|---|
| 547 |
|
|---|
| 548 |
|
|---|
| 549 |
(defvar delta) |
|---|
| 550 |
(defvar mpos) |
|---|
| 551 |
(defvar pro-or-de) |
|---|
| 552 |
(defvar start-pos) |
|---|
| 553 |
(defvar start-line) |
|---|
| 554 |
(defvar mark-line) |
|---|
| 555 |
|
|---|
| 556 |
(defun reftex-toc-demote (&optional arg) |
|---|
| 557 |
"Demote section at point. If region is active, apply to all in region." |
|---|
| 558 |
(interactive "p") |
|---|
| 559 |
(reftex-toc-do-promote 1)) |
|---|
| 560 |
(defun reftex-toc-promote (&optional arg) |
|---|
| 561 |
"Promote section at point. If region is active, apply to all in region." |
|---|
| 562 |
(interactive "p") |
|---|
| 563 |
(reftex-toc-do-promote -1)) |
|---|
| 564 |
(defun reftex-toc-do-promote (delta) |
|---|
| 565 |
"Workhorse for reftex-toc-promote and reftex-to-demote. |
|---|
| 566 |
Changes the level of sections in the current region, or just the section at |
|---|
| 567 |
point." |
|---|
| 568 |
|
|---|
| 569 |
|
|---|
| 570 |
|
|---|
| 571 |
(let* ((start-line (+ (count-lines (point-min) (point)) |
|---|
| 572 |
(if (bolp) 1 0))) |
|---|
| 573 |
(mark-line (if (reftex-region-active-p) |
|---|
| 574 |
(save-excursion (goto-char (mark)) |
|---|
| 575 |
(+ (count-lines (point-min) (point)) |
|---|
| 576 |
(if (bolp) 1 0))))) |
|---|
| 577 |
(start-pos (point)) |
|---|
| 578 |
(pro-or-de (if (> delta 0) "de" "pro")) |
|---|
| 579 |
beg end entries data sections nsec mpos msg) |
|---|
| 580 |
(setq msg |
|---|
| 581 |
(catch 'exit |
|---|
| 582 |
(if (reftex-region-active-p) |
|---|
| 583 |
|
|---|
| 584 |
|
|---|
| 585 |
(if (not (reftex-toc-check-docstruct)) |
|---|
| 586 |
(reftex-toc-load-all-files-for-promotion) |
|---|
| 587 |
(setq beg (min (point) (mark)) |
|---|
| 588 |
end (max (point) (mark)))) |
|---|
| 589 |
(setq beg (point) end (point))) |
|---|
| 590 |
(goto-char beg) |
|---|
| 591 |
(while (and (setq data (get-text-property (point) :data)) |
|---|
| 592 |
(<= (point) end)) |
|---|
| 593 |
(if (eq (car data) 'toc) (push (cons data (point)) entries)) |
|---|
| 594 |
(goto-char (or (next-single-property-change (point) :data) |
|---|
| 595 |
(point-max)))) |
|---|
| 596 |
(setq entries (nreverse entries)) |
|---|
| 597 |
|
|---|
| 598 |
(goto-char start-pos) |
|---|
| 599 |
(setq sections (reftex-toc-extract-section-number (car entries))) |
|---|
| 600 |
(if (> (setq nsec (length entries)) 1) |
|---|
| 601 |
(setq sections |
|---|
| 602 |
(concat sections "-" |
|---|
| 603 |
(reftex-toc-extract-section-number |
|---|
| 604 |
(nth (1- nsec) entries))))) |
|---|
| 605 |
|
|---|
| 606 |
(setq entries (mapcar 'reftex-toc-promote-prepare entries)) |
|---|
| 607 |
|
|---|
| 608 |
(if (or (not reftex-toc-confirm-promotion) |
|---|
| 609 |
(and (integerp reftex-toc-confirm-promotion) |
|---|
| 610 |
(< nsec reftex-toc-confirm-promotion)) |
|---|
| 611 |
(yes-or-no-p |
|---|
| 612 |
(format "%s %d toc-entr%s (section%s %s)? " |
|---|
| 613 |
(if (< delta 0) "Promote" "Demote") |
|---|
| 614 |
nsec |
|---|
| 615 |
(if (= 1 nsec) "y" "ies") |
|---|
| 616 |
(if (= 1 nsec) "" "s") |
|---|
| 617 |
sections))) |
|---|
| 618 |
nil |
|---|
| 619 |
(error "Abort")) |
|---|
| 620 |
|
|---|
| 621 |
(mapcar 'reftex-toc-promote-action entries) |
|---|
| 622 |
|
|---|
| 623 |
(save-window-excursion |
|---|
| 624 |
(reftex-toc-Rescan)) |
|---|
| 625 |
(reftex-toc-restore-region start-line mark-line) |
|---|
| 626 |
(message "%d section%s %smoted" |
|---|
| 627 |
nsec (if (= 1 nsec) "" "s") pro-or-de) |
|---|
| 628 |
nil)) |
|---|
| 629 |
(if msg (progn (ding) (message msg))))) |
|---|
| 630 |
|
|---|
| 631 |
|
|---|
| 632 |
(defun reftex-toc-restore-region (point-line &optional mark-line) |
|---|
| 633 |
(if mark-line |
|---|
| 634 |
(progn (goto-line mark-line) |
|---|
| 635 |
(setq mpos (point)))) |
|---|
| 636 |
(if point-line (goto-line point-line)) |
|---|
| 637 |
(if mark-line |
|---|
| 638 |
(progn |
|---|
| 639 |
(set-mark mpos) |
|---|
| 640 |
(if (fboundp 'zmacs-activate-region) |
|---|
| 641 |
(zmacs-activate-region) |
|---|
| 642 |
(setq mark-active t |
|---|
| 643 |
deactivate-mark nil))))) |
|---|
| 644 |
|
|---|
| 645 |
(defvar name1) |
|---|
| 646 |
(defvar dummy) |
|---|
| 647 |
(defvar dummy2) |
|---|
| 648 |
|
|---|
| 649 |
(defun reftex-toc-promote-prepare (x) |
|---|
| 650 |
"Look at a toc entry and see if we could pro/demote it. |
|---|
| 651 |
Expects the level change DELTA to be dynamically scoped into this function. |
|---|
| 652 |
This function prepares everything for the changes, but does not do it. |
|---|
| 653 |
The return value is a list with information needed when doing the |
|---|
| 654 |
promotion/demotion later." |
|---|
| 655 |
(let* ((data (car x)) |
|---|
| 656 |
(toc-point (cdr x)) |
|---|
| 657 |
(marker (nth 4 data)) |
|---|
| 658 |
(literal (nth 7 data)) |
|---|
| 659 |
(load nil) |
|---|
| 660 |
(name nil) |
|---|
| 661 |
|
|---|
| 662 |
|
|---|
| 663 |
(name1 |
|---|
| 664 |
(if (and (markerp marker) (marker-buffer marker)) |
|---|
| 665 |
|
|---|
| 666 |
(progn |
|---|
| 667 |
(save-excursion |
|---|
| 668 |
|
|---|
| 669 |
(set-buffer (marker-buffer marker)) |
|---|
| 670 |
(goto-char (marker-position marker)) |
|---|
| 671 |
(if (looking-at (regexp-quote literal)) |
|---|
| 672 |
|
|---|
| 673 |
(progn |
|---|
| 674 |
(beginning-of-line 1) |
|---|
| 675 |
(if (looking-at reftex-section-regexp) |
|---|
| 676 |
(setq name (reftex-match-string 2)) |
|---|
| 677 |
(error "Something is wrong! Contact maintainer!"))) |
|---|
| 678 |
|
|---|
| 679 |
|
|---|
| 680 |
|
|---|
| 681 |
(setq load t))) |
|---|
| 682 |
|
|---|
| 683 |
(if load (reftex-toc-load-all-files-for-promotion))) |
|---|
| 684 |
|
|---|
| 685 |
(reftex-toc-load-all-files-for-promotion))) |
|---|
| 686 |
(level (cdr (assoc name reftex-section-levels-all))) |
|---|
| 687 |
(dummy (if (not (integerp level)) |
|---|
| 688 |
(progn |
|---|
| 689 |
(goto-char toc-point) |
|---|
| 690 |
(error "Cannot %smote special sections" pro-or-de)))) |
|---|
| 691 |
|
|---|
| 692 |
(newlevel (if (>= level 0) (+ delta level) (- level delta))) |
|---|
| 693 |
(dummy2 (if (or (and (>= level 0) (= newlevel -1)) |
|---|
| 694 |
(and (< level 0) (= newlevel 0))) |
|---|
| 695 |
(error "Cannot %smote \\%s" pro-or-de name))) |
|---|
| 696 |
(newname (reftex-toc-newhead-from-alist newlevel name |
|---|
| 697 |
reftex-section-levels-all))) |
|---|
| 698 |
(if (and name newname) |
|---|
| 699 |
(list data name newname toc-point) |
|---|
| 700 |
(goto-char toc-point) |
|---|
| 701 |
(error "Cannot %smote \\%s" pro-or-de name)))) |
|---|
| 702 |
|
|---|
| 703 |
(defun reftex-toc-promote-action (x) |
|---|
| 704 |
"Change the level of a toc entry. |
|---|
| 705 |
DELTA and PRO-OR-DE are assumed to be dynamically scoped into this function." |
|---|
| 706 |
(let* ((data (car x)) |
|---|
| 707 |
(name (nth 1 x)) |
|---|
| 708 |
(newname (nth 2 x)) |
|---|
| 709 |
(marker (nth 4 data))) |
|---|
| 710 |
(save-excursion |
|---|
| 711 |
(set-buffer (marker-buffer marker)) |
|---|
| 712 |
(goto-char (marker-position marker)) |
|---|
| 713 |
(if (looking-at (concat "\\([ \t]*\\\\\\)" (regexp-quote name))) |
|---|
| 714 |
(replace-match (concat "\\1" newname)) |
|---|
| 715 |
(error "Fatal error during %smotion" pro-or-de))))) |
|---|
| 716 |
|
|---|
| 717 |
(defun reftex-toc-extract-section-number (entry) |
|---|
| 718 |
"Get the numbering of a toc entry, for message purposes." |
|---|
| 719 |
(if (string-match "\\s-*\\(\\S-+\\)" (nth 2 (car entry))) |
|---|
| 720 |
(match-string 1 (nth 2 (car entry))) |
|---|
| 721 |
"?")) |
|---|
| 722 |
|
|---|
| 723 |
(defun reftex-toc-newhead-from-alist (nlevel head alist) |
|---|
| 724 |
"Get new heading with level NLEVEL from ALIST. |
|---|
| 725 |
If there are no such entries, return nil. |
|---|
| 726 |
If there are several different entries with same new level, choose |
|---|
| 727 |
the one with the smallest distance to the assocation of HEAD in the alist. |
|---|
| 728 |
This makes it possible for promotion to work several sets of headings, |
|---|
| 729 |
if these sets are sorted blocks in the alist." |
|---|
| 730 |
(let* ((al alist) |
|---|
| 731 |
(ass (assoc head al)) |
|---|
| 732 |
(pos (length (memq ass al))) |
|---|
| 733 |
dist (mindist 1000) newhead) |
|---|
| 734 |
(while al |
|---|
| 735 |
(if (equal (cdar al) nlevel) |
|---|
| 736 |
(progn |
|---|
| 737 |
(setq dist (abs (- (length al) pos))) |
|---|
| 738 |
(if (< dist mindist) |
|---|
| 739 |
(setq newhead (car (car al)) |
|---|
| 740 |
mindist dist)))) |
|---|
| 741 |
(setq al (cdr al))) |
|---|
| 742 |
newhead)) |
|---|
| 743 |
|
|---|
| 744 |
(defun reftex-toc-check-docstruct () |
|---|
| 745 |
"Check if the current docstruct is fully up to date and all files visited." |
|---|
| 746 |
|
|---|
| 747 |
(let ((docstruct (symbol-value reftex-docstruct-symbol)) |
|---|
| 748 |
entry marker empoint) |
|---|
| 749 |
(catch 'exit |
|---|
| 750 |
(while (setq entry (pop docstruct)) |
|---|
| 751 |
(if (eq (car entry) 'toc) |
|---|
| 752 |
(progn |
|---|
| 753 |
(setq marker (nth 4 entry) |
|---|
| 754 |
empoint (nth 8 entry)) |
|---|
| 755 |
(if (not (and (markerp marker) |
|---|
| 756 |
(marker-buffer marker) |
|---|
| 757 |
(= (marker-position marker) empoint))) |
|---|
| 758 |
(throw 'exit nil))))) |
|---|
| 759 |
t))) |
|---|
| 760 |
|
|---|
| 761 |
(defun reftex-toc-load-all-files-for-promotion () |
|---|
| 762 |
"Make sure all files of the document are being visited by buffers, |
|---|
| 763 |
and that the scanning info is absolutely up to date. |
|---|
| 764 |
We do this by rescanning with reftex-keep-temporary-buffers bound to t. |
|---|
| 765 |
The variable PRO-OR-DE is assumed to be dynamically scoped into this function. |
|---|
| 766 |
When finished, we exit with an error message." |
|---|
| 767 |
(let ((reftex-keep-temporary-buffers t)) |
|---|
| 768 |
(reftex-toc-Rescan) |
|---|
| 769 |
(reftex-toc-restore-region start-line mark-line) |
|---|
| 770 |
(throw 'exit |
|---|
| 771 |
"TOC had to be updated first. Please check selection and repeat the command."))) |
|---|
| 772 |
|
|---|
| 773 |
(defun reftex-toc-rename-label () |
|---|
| 774 |
"Rename the currently selected label in the *TOC* buffer. |
|---|
| 775 |
This launches a global search and replace in order to rename a label. |
|---|
| 776 |
Renaming a label is hardly ever necessary - the only exeption is after |
|---|
| 777 |
promotion/demotion in connection with a package like fancyref, where the |
|---|
| 778 |
label prefix determines the wording of a reference." |
|---|
| 779 |
(interactive) |
|---|
| 780 |
(let* ((toc (get-text-property (point) :data)) |
|---|
| 781 |
(label (car toc)) newlabel) |
|---|
| 782 |
(if (not (stringp label)) |
|---|
| 783 |
(error "This is not a label entry.")) |
|---|
| 784 |
(setq newlabel (read-string (format "Rename label \"%s\" to:" label))) |
|---|
| 785 |
(if (assoc newlabel (symbol-value reftex-docstruct-symbol)) |
|---|
| 786 |
(if (not (y-or-n-p |
|---|
| 787 |
(format "Label '%s' exists. Use anyway? " label))) |
|---|
| 788 |
(error "Abort"))) |
|---|
| 789 |
(save-excursion |
|---|
| 790 |
(save-window-excursion |
|---|
| 791 |
(reftex-toc-visit-location t) |
|---|
| 792 |
(condition-case nil |
|---|
| 793 |
(reftex-query-replace-document |
|---|
| 794 |
(concat "{" (regexp-quote label) "}") |
|---|
| 795 |
(format "{%s}" newlabel)) |
|---|
| 796 |
(error t)))) |
|---|
| 797 |
(reftex-toc-rescan))) |
|---|
| 798 |
|
|---|
| 799 |
|
|---|
| 800 |
(defun reftex-toc-visit-location (&optional final no-revisit) |
|---|
| 801 |
|
|---|
| 802 |
|
|---|
| 803 |
|
|---|
| 804 |
|
|---|
| 805 |
|
|---|
| 806 |
|
|---|
| 807 |
|
|---|
| 808 |
|
|---|
| 809 |
|
|---|
| 810 |
(let* ((toc (get-text-property (point) :data)) |
|---|
| 811 |
(toc-window (selected-window)) |
|---|
| 812 |
show-window show-buffer match) |
|---|
| 813 |
|
|---|
| 814 |
(unless toc (error "Don't know which toc line to visit")) |
|---|
| 815 |
|
|---|