]> code.delx.au - gnu-emacs/blob - lisp/textmodes/reftex.el
(reftex-with-special-syntax): Bind `case-fold-search' to nil.
[gnu-emacs] / lisp / textmodes / reftex.el
1 ;;; reftex.el --- Minor mode for doing \label, \ref, \cite, \index in LaTeX
2 ;; Copyright (c) 1997, 1998, 1999, 2000 Free Software Foundation, Inc.
3
4 ;; Author: Carsten Dominik <dominik@strw.LeidenUniv.nl>
5 ;; Version: 4.11
6 ;; Keywords: tex
7
8 ;; This file is part of GNU Emacs.
9
10 ;; GNU Emacs is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
13 ;; any later version.
14
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
19
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING. If not, write to the
22 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, USA.
24
25 ;;---------------------------------------------------------------------------
26 ;;
27 ;;; Commentary:
28 ;;
29 ;; RefTeX is a minor mode with distinct support for \ref, \label, \cite,
30 ;; and \index commands in (multi-file) LaTeX documents.
31 ;; - A table of contents provides easy access to any part of a document.
32 ;; - Labels are created semi-automatically.
33 ;; - Definition context of labels is provided when creating a reference.
34 ;; - Citations are simplified with efficient database lookup.
35 ;; - Text phrases can be collected in a file, for later global indexing.
36 ;; - The index preview buffer helps to check and edit index entries.
37 ;;
38 ;;
39 ;; INSTALLATION
40 ;; ------------
41 ;;
42 ;; - If this file is part of an X/Emacs distribution, it is installed.
43 ;; - For XEmacs 21.x, you need to install the RefTeX plug-in package
44 ;; available from the XEmacs distribution sites.
45 ;; - If you have downloaded this file from the maintainers webpage, follow
46 ;; the instructions in the INSTALL file of the distrubution.
47 ;;
48 ;; To turn RefTeX Mode on and off in a buffer, use `M-x reftex-mode'.
49 ;;
50 ;; To turn on RefTeX Mode for all LaTeX files, add the following lines
51 ;; to your .emacs file:
52 ;;
53 ;; (add-hook 'LaTeX-mode-hook 'turn-on-reftex) ; AUCTeX LaTeX mode
54 ;; (add-hook 'latex-mode-hook 'turn-on-reftex) ; Emacs latex mode
55 ;;
56 ;;
57 ;; DOCUMENTATION
58 ;; -------------
59 ;;
60 ;; See below for a short summary of how to use RefTeX.
61 ;;
62 ;; There is an extensive texinfo document describing RefTeX in detail.
63 ;; One way to view this documentation is `M-x reftex-info RET'.
64 ;;
65 ;; The documentation in various formats is also available at
66 ;;
67 ;; http://www.strw.leidenuniv.nl/~dominik/Tools/
68 ;;
69 ;;---------------------------------------------------------------------------
70 ;;
71 ;; Introduction
72 ;; ************
73 ;;
74 ;; RefTeX is a specialized package for support of labels, references,
75 ;; citations, and the index in LaTeX. RefTeX wraps itself round 4 LaTeX
76 ;; macros: `\label', `\ref', `\cite', and `\index'. Using these macros
77 ;; usually requires looking up different parts of the document and
78 ;; searching through BibTeX database files. RefTeX automates these
79 ;; time-consuming tasks almost entirely. It also provides functions to
80 ;; display the structure of a document and to move around in this
81 ;; structure quickly.
82 ;;
83 ;; *Note Imprint::, for information about who to contact for help, bug
84 ;; reports or suggestions.
85 ;;
86 ;; Environment
87 ;; ===========
88 ;;
89 ;; RefTeX needs to access all files which are part of a multifile
90 ;; document, and the BibTeX database files requested by the
91 ;; `\bibliography' command. To find these files, RefTeX will require a
92 ;; search path, i.e. a list of directories to check. Normally this list
93 ;; is stored in the environment variables `TEXINPUTS' and `BIBINPUTS'
94 ;; which are also used by RefTeX. However, on some systems these
95 ;; variables do not contain the full search path. If RefTeX does not work
96 ;; for you because it cannot find some files, read *Note Finding Files::.
97 ;;
98 ;; Entering RefTeX Mode
99 ;; ====================
100 ;;
101 ;; To turn RefTeX Mode on and off in a particular buffer, use `M-x
102 ;; reftex-mode'. To turn on RefTeX Mode for all LaTeX files, add the
103 ;; following lines to your `.emacs' file:
104 ;;
105 ;; (add-hook 'LaTeX-mode-hook 'turn-on-reftex) ; with AUCTeX LaTeX mode
106 ;; (add-hook 'latex-mode-hook 'turn-on-reftex) ; with Emacs latex mode
107 ;;
108 ;; RefTeX in a Nutshell
109 ;; ====================
110 ;;
111 ;; 1. Table of Contents
112 ;; Typing `C-c =' (`reftex-toc') will show a table of contents of the
113 ;; document. This buffer can display sections, labels and index
114 ;; entries defined in the document. From the buffer, you can jump
115 ;; quickly to every part of your document. Press `?' to get help.
116 ;;
117 ;; 2. Labels and References
118 ;; RefTeX helps to create unique labels and to find the correct key
119 ;; for references quickly. It distinguishes labels for different
120 ;; environments, knows about all standard environments (and many
121 ;; others), and can be configured to recognize any additional labeled
122 ;; environments you have defined yourself (variable
123 ;; `reftex-label-alist').
124 ;;
125 ;; * Creating Labels
126 ;; Type `C-c (' (`reftex-label') to insert a label at point.
127 ;; RefTeX will either
128 ;; - derive a label from context (default for section labels)
129 ;; - prompt for a label string (default for figures and
130 ;; tables) or
131 ;; - insert a simple label made of a prefix and a number (all
132 ;; other environments)
133 ;;
134 ;; Which labels are created how is configurable with the variable
135 ;; `reftex-insert-label-flags'.
136 ;;
137 ;; * Referencing Labels
138 ;; To make a reference, type `C-c )' (`reftex-reference'). This
139 ;; shows an outline of the document with all labels of a certain
140 ;; type (figure, equation,...) and some label context.
141 ;; Selecting a label inserts a `\ref{LABEL}' macro into the
142 ;; original buffer.
143 ;;
144 ;; 3. Citations
145 ;; Typing `C-c [' (`reftex-citation') will let you specify a regular
146 ;; expression to search in current BibTeX database files (as
147 ;; specified in the `\bibliography' command) and pull out a list of
148 ;; matches for you to choose from. The list is _formatted_ and
149 ;; sorted. The selected article is referenced as `\cite{KEY}' (see
150 ;; the variable `reftex-cite-format' if you want to insert different
151 ;; macros).
152 ;;
153 ;; 4. Index Support
154 ;; RefTeX helps to enter index entries. It also compiles all entries
155 ;; into an alphabetically sorted `*Index*' buffer which you can use
156 ;; to check and edit the entries. RefTeX knows about the standard
157 ;; index macros and can be configured to recognize any additional
158 ;; macros you have defined (`reftex-index-macros'). Multiple indices
159 ;; are supported.
160 ;;
161 ;; * Creating Index Entries
162 ;; To index the current selection or the word at point, type
163 ;; `C-c /' (`reftex-index-selection-or-word'). The default macro
164 ;; `reftex-index-default-macro' will be used. For a more
165 ;; complex entry type `C-c <' (`reftex-index'), select any of
166 ;; the index macros and enter the arguments with completion.
167 ;;
168 ;; * The Index Phrases File (Delayed Indexing)
169 ;; Type `C-c \' (`reftex-index-phrase-selection-or-word') to add
170 ;; the current word or selection to a special _index phrase
171 ;; file_. RefTeX can later search the document for occurrences
172 ;; of these phrases and let you interactively index the matches.
173 ;;
174 ;; * Displaying and Editing the Index
175 ;; To display the compiled index in a special buffer, type `C-c
176 ;; >' (`reftex-display-index'). From that buffer you can check
177 ;; and edit all entries.
178 ;;
179 ;; 5. Viewing Cross-References
180 ;; When point is on the KEY argument of a cross-referencing macro
181 ;; (`\label', `\ref', `\cite', `\bibitem', `\index', and variations)
182 ;; or inside a BibTeX database entry, you can press `C-c &'
183 ;; (`reftex-view-crossref') to display corresponding locations in the
184 ;; document and associated BibTeX database files.
185 ;; When the enclosing macro is `\cite' or `\ref' and no other message
186 ;; occupies the echo area, information about the citation or label
187 ;; will automatically be displayed in the echo area.
188 ;;
189 ;; 6. Multifile Documents
190 ;; Multifile Documents are fully supported. The included files must
191 ;; have a file variable `TeX-master' or `tex-main-file' pointing to
192 ;; the master file. RefTeX provides cross-referencing information
193 ;; from all parts of the document, and across document borders
194 ;; (`xr.sty').
195 ;;
196 ;; 7. Document Parsing
197 ;; RefTeX needs to parse the document in order to find labels and
198 ;; other information. It does it automatically once and updates its
199 ;; list internally when `reftex-label' and `reftex-index' are used.
200 ;; To enforce reparsing, call any of the commands described above
201 ;; with a raw `C-u' prefix, or press the `r' key in the label
202 ;; selection buffer, the table of contents buffer, or the index
203 ;; buffer.
204 ;;
205 ;; 8. AUCTeX
206 ;; If your major LaTeX mode is AUCTeX, RefTeX can cooperate with it
207 ;; (see variable `reftex-plug-into-AUCTeX'). AUCTeX contains style
208 ;; files which trigger appropriate settings in RefTeX, so that for
209 ;; many of the popular LaTeX packages no additional customizations
210 ;; will be necessary.
211 ;;
212 ;; 9. Useful Settings
213 ;; To make RefTeX faster for large documents, try these:
214 ;; (setq reftex-enable-partial-scans t)
215 ;; (setq reftex-save-parse-info t)
216 ;; (setq reftex-use-multiple-selection-buffers t)
217 ;;
218 ;; To integrate with AUCTeX, use
219 ;; (setq reftex-plug-into-AUCTeX t)
220 ;;
221 ;; To make your own LaTeX macro definitions known to RefTeX,
222 ;; customize the variables
223 ;; `reftex-label-alist' (for label macros/environments)
224 ;; `reftex-section-levels' (for sectioning commands)
225 ;; `reftex-cite-format' (for `\cite'-like macros)
226 ;; `reftex-index-macros' (for `\index'-like macros)
227 ;; `reftex-index-default-macro' (to set the default macro)
228 ;; If you have a large number of macros defined, you may want to write
229 ;; an AUCTeX style file to support them with both AUCTeX and RefTeX.
230 ;;
231 ;; 10. Where Next?
232 ;; Go ahead and use RefTeX. Use its menus until you have picked up
233 ;; the key bindings. For an overview of what you can do in each of
234 ;; the different special buffers, press `?'. Read the manual if you
235 ;; get stuck, of if you are curious what else might be available.
236 ;; The first part of the manual explains in a tutorial way how to use
237 ;; and customize RefTeX. The second part is a command and variable
238 ;; reference.
239 ;;
240 ;;---------------------------------------------------------------------------
241 ;;
242 ;; AUTHOR
243 ;; ======
244 ;;
245 ;; Carsten Dominik <dominik@strw.LeidenUniv.nl>
246 ;;
247 ;; with contributions from Stephen Eglen
248 ;;
249 ;; RefTeX is bundled with Emacs and available as a plug-in package for
250 ;; XEmacs 21.x. If you need to install it yourself, you can find a
251 ;; distribution at
252 ;;
253 ;; http://www.strw.leidenuniv.nl/~dominik/Tools/
254 ;;
255 ;; THANKS TO:
256 ;; ---------
257 ;; Thanks to the people on the Net who have used RefTeX and helped
258 ;; developing it with their reports. In particular thanks to
259 ;;
260 ;; Fran Burstall, Alastair Burt, Soren Dayton, Stephen Eglen,
261 ;; Karl Eichwalder, Peter Galbraith, Dieter Kraft, Kai Grossjohann,
262 ;; Frank Harrell, Adrian Lanz, Rory Molinari, Stefan Monnier,
263 ;; Laurent Mugnier, Sudeep Kumar Palat, Daniel Polani, Robin Socha,
264 ;; Richard Stanton, Allan Strand, Jan Vroonhof, Christoph Wedler,
265 ;; Alan Williams.
266 ;;
267 ;; Finally thanks to Uwe Bolick who first got me (some years ago) into
268 ;; supporting LaTeX labels and references with an editor (which was
269 ;; MicroEmacs at the time).
270 ;;
271 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
272 ;;
273 ;;;;;;
274 \f
275 ;;; Code:
276
277 (eval-when-compile (require 'cl))
278
279 ;; Stuff that needs to be there when we use defcustom
280 (require 'custom)
281
282 (defvar reftex-tables-dirty t
283 "Flag showing if tables need to be re-computed.")
284
285 (eval-and-compile
286 (defun reftex-set-dirty (symbol value)
287 (setq reftex-tables-dirty t)
288 (set symbol value)))
289
290
291 ;;; =========================================================================
292 ;;;
293 ;;; Configuration variables
294
295 (require 'reftex-vars)
296
297
298 ;;; =========================================================================
299 ;;;
300 ;;; Define the formal stuff for a minor mode named RefTeX.
301 ;;;
302
303 (defconst reftex-version "RefTeX version 4.11"
304 "Version string for RefTeX.")
305
306 (defvar reftex-mode nil
307 "Determines if RefTeX mode is active.")
308 (make-variable-buffer-local 'reftex-mode)
309
310 (defvar reftex-mode-map (make-sparse-keymap)
311 "Keymap for RefTeX mode.")
312
313 (defvar reftex-mode-menu nil)
314 (defvar reftex-syntax-table nil)
315
316 (defvar reftex-auto-view-crossref-timer nil
317 "The timer used for auto-view-crossref.")
318
319 ;;;###autoload
320 (defun turn-on-reftex ()
321 "Turn on RefTeX mode."
322 (reftex-mode t))
323
324 ;;;###autoload
325 (defun reftex-mode (&optional arg)
326 "Minor mode with distinct support for \\label, \\ref and \\cite in LaTeX.
327
328 \\<reftex-mode-map>A Table of Contents of the entire (multifile) document with browsing
329 capabilities is available with `\\[reftex-toc]'.
330
331 Labels can be created with `\\[reftex-label]' and referenced with `\\[reftex-reference]'.
332 When referencing, you get a menu with all labels of a given type and
333 context of the label definition. The selected label is inserted as a
334 \\ref macro.
335
336 Citations can be made with `\\[reftex-citation]' which will use a regular expression
337 to pull out a *formatted* list of articles from your BibTeX
338 database. The selected citation is inserted as a \\cite macro.
339
340 Index entries can be made with `\\[reftex-index-selection-or-word]' which indexes the word at point
341 or the current selection. More general index entries are created with
342 `\\[reftex-index]'. `\\[reftex-display-index]' displays the compiled index.
343
344 Most command have help available on the fly. This help is accessed by
345 pressing `?' to any prompt mentioning this feature.
346
347 Extensive documentation about RefTeX is available in Info format.
348 You can view this information with `\\[reftex-info]'.
349
350 \\{reftex-mode-map}
351 Under X, these and other functions will also be available as `Ref' menu
352 on the menu bar.
353
354 ------------------------------------------------------------------------------"
355
356 (interactive "P")
357 (setq reftex-mode (not (or (and (null arg) reftex-mode)
358 (<= (prefix-numeric-value arg) 0))))
359
360 (if reftex-mode
361 (progn
362 ;; Mode was turned on
363 (easy-menu-add reftex-mode-menu)
364 (and reftex-plug-into-AUCTeX
365 (reftex-plug-into-AUCTeX))
366 (unless (get 'reftex-auto-view-crossref 'initialized)
367 (and reftex-auto-view-crossref
368 (reftex-toggle-auto-view-crossref))
369 (put 'reftex-auto-view-crossref 'initialized t))
370 (unless reftex-syntax-table
371 (setq reftex-syntax-table (copy-syntax-table (syntax-table)))
372 (modify-syntax-entry ?\( "." reftex-syntax-table)
373 (modify-syntax-entry ?\) "." reftex-syntax-table))
374 (run-hooks 'reftex-mode-hook))
375 ;; Mode was turned off
376 (easy-menu-remove reftex-mode-menu)))
377
378 (if (fboundp 'add-minor-mode)
379 ;; Use it so that we get the extras
380 (progn
381 (put 'reftex-mode :included '(memq major-mode '(latex-mode tex-mode)))
382 (put 'reftex-mode :menu-tag "RefTeX Mode")
383 (add-minor-mode 'reftex-mode " Ref" reftex-mode-map))
384 ;; The standard way
385 (unless (assoc 'reftex-mode minor-mode-alist)
386 (push '(reftex-mode " Ref") minor-mode-alist))
387 (unless (assoc 'reftex-mode minor-mode-map-alist)
388 (push (cons 'reftex-mode reftex-mode-map) minor-mode-map-alist)))
389
390 (defvar reftex-docstruct-symbol)
391 (defun reftex-kill-buffer-hook ()
392 "Save RefTeX's parse file for this buffer if the information has changed."
393 ;; Save the parsing information if it was modified.
394 ;; This function should be installed in `kill-buffer-hook'.
395 ;; We are careful to make sure nothing goes wring in this function.
396 (when (and (boundp 'reftex-mode) reftex-mode
397 (boundp 'reftex-save-parse-info) reftex-save-parse-info
398 (boundp 'reftex-docstruct-symbol) reftex-docstruct-symbol
399 (symbol-value reftex-docstruct-symbol)
400 (get reftex-docstruct-symbol 'modified))
401 ;; Write the file.
402 (condition-case nil
403 (reftex-access-parse-file 'write)
404 (error nil))))
405
406 (defun reftex-kill-emacs-hook ()
407 "Call `reftex-kill-buffer-hook' on all buffers."
408 ;; This function should be installed in `kill-emacs-hook'.
409 (save-excursion
410 (mapcar (lambda (buf)
411 (set-buffer buf)
412 (reftex-kill-buffer-hook))
413 (buffer-list))))
414
415 ;;; =========================================================================
416 ;;;
417 ;;; Silence warnings about variables in other packages.
418 (defvar TeX-master)
419 (defvar LaTeX-section-hook)
420 (defvar LaTeX-label-function)
421 (defvar tex-main-file)
422 (defvar outline-minor-mode)
423 (defvar font-lock-mode)
424 (defvar font-lock-fontify-region-function)
425 (defvar font-lock-syntactic-keywords)
426
427 ;;; =========================================================================
428 ;;;
429 ;;; Multibuffer Variables
430 ;;;
431 ;;; Technical notes: These work as follows: We keep just one list
432 ;;; of labels for each master file - this can save a lot of memory.
433 ;;; `reftex-master-index-list' is an alist which connects the true file name
434 ;;; of each master file with the symbols holding the information on that
435 ;;; document. Each buffer has local variables which point to these symbols.
436
437 ;; List of variables which handle the multifile stuff.
438 ;; This list is used to tie, untie, and reset these symbols.
439 (defconst reftex-multifile-symbols
440 '(reftex-docstruct-symbol))
441
442 ;; Alist connecting master file names with the corresponding lisp symbols.
443 (defvar reftex-master-index-list nil)
444
445 ;; Last index used for a master file.
446 (defvar reftex-multifile-index 0)
447
448 ;; Variable holding the symbol with the label list of the document.
449 (defvar reftex-docstruct-symbol nil)
450 (make-variable-buffer-local 'reftex-docstruct-symbol)
451
452 (defun reftex-next-multifile-index ()
453 ;; Return the next free index for multifile symbols.
454 (incf reftex-multifile-index))
455
456 (defun reftex-tie-multifile-symbols ()
457 ;; Tie the buffer-local symbols to globals connected with the master file.
458 ;; If the symbols for the current master file do not exist, they are created.
459
460 (let* ((master (file-truename (reftex-TeX-master-file)))
461 (index (assoc master reftex-master-index-list))
462 (symlist reftex-multifile-symbols)
463 symbol symname newflag)
464 ;; Find the correct index.
465 (if index
466 ;; symbols do exist
467 (setq index (cdr index))
468 ;; Get a new index and add info to the alist.
469 (setq index (reftex-next-multifile-index)
470 newflag t)
471 (push (cons master index) reftex-master-index-list))
472
473 ;; Get/create symbols and tie them.
474 (while symlist
475 (setq symbol (car symlist)
476 symlist (cdr symlist)
477 symname (symbol-name symbol))
478 (set symbol (intern (concat symname "-" (int-to-string index))))
479 (put (symbol-value symbol) :master-index index)
480 ;; Initialize if new symbols.
481 (when newflag
482 (set (symbol-value symbol) nil)
483 (put (symbol-value symbol) 'reftex-index-macros-style '(default))))
484
485 ;; Return t if the symbols did already exist, nil when we've made them.
486 (not newflag)))
487
488 (defun reftex-untie-multifile-symbols ()
489 ;; Remove ties from multifile symbols, so that next use makes new ones.
490 (let ((symlist reftex-multifile-symbols)
491 (symbol nil))
492 (while symlist
493 (setq symbol (car symlist)
494 symlist (cdr symlist))
495 (set symbol nil))))
496
497 (defun reftex-TeX-master-file ()
498 ;; Return the name of the master file associated with the current buffer.
499 ;; When AUCTeX is loaded, we will use it's more sophisticated method.
500 ;; We also support the default TeX and LaTeX modes by checking for a
501 ;; variable tex-main-file.
502 (let
503 ((master
504 (cond
505 ((fboundp 'TeX-master-file) ; AUCTeX is loaded. Use its mechanism.
506 (condition-case nil
507 (TeX-master-file t)
508 (error (buffer-file-name))))
509 ((boundp 'TeX-master) ; The variable is defined - lets use it.
510 (cond
511 ((eq TeX-master t)
512 (buffer-file-name))
513 ((eq TeX-master 'shared)
514 (setq TeX-master (read-file-name "Master file: "
515 nil nil t nil)))
516 (TeX-master)
517 (t
518 (setq TeX-master (read-file-name "Master file: "
519 nil nil t nil)))))
520 ((boundp 'tex-main-file)
521 ;; This is the variable from the default TeX modes.
522 (cond
523 ((stringp tex-main-file)
524 ;; ok, this must be it
525 tex-main-file)
526 (t
527 ;; In this case, the buffer is its own master.
528 (buffer-file-name))))
529 (t
530 ;; Know nothing about master file. Assume this is a master file.
531 (buffer-file-name)))))
532 (cond
533 ((null master)
534 (error "Need a filename for this buffer, please save it first"))
535 ((or (file-exists-p (concat master ".tex"))
536 (reftex-get-buffer-visiting (concat master ".tex")))
537 ;; Ahh, an extra .tex was missing...
538 (setq master (concat master ".tex")))
539 ((or (file-exists-p master)
540 (reftex-get-buffer-visiting master))
541 ;; We either see the file, or have a buffer on it. OK.
542 )
543 (t
544 ;; Use buffer file name.
545 (buffer-file-name)))
546 (expand-file-name master)))
547
548 (defun reftex-is-multi ()
549 ;; Tell if this is a multifile document. When not sure, say yes.
550 (let ((entry (assq 'is-multi (symbol-value reftex-docstruct-symbol))))
551 (if entry
552 (nth 1 entry)
553 t)))
554
555 (defun reftex-set-cite-format (value)
556 "Set the document-local value of `reftex-cite-format'.
557 When such a value exists, it overwrites the setting given with
558 `reftex-cite-format'. See the documentation of `reftex-cite-format'
559 for possible values. This function should be used from AUCTeX style files."
560 (unless reftex-docstruct-symbol
561 (reftex-tie-multifile-symbols))
562 (when (and reftex-docstruct-symbol
563 (symbolp reftex-docstruct-symbol))
564 (put reftex-docstruct-symbol 'reftex-cite-format value)))
565
566 (defun reftex-get-cite-format ()
567 ;; Return the current citation format. Either the document-local value in
568 ;; reftex-cite-format-symbol, or the global value in reftex-cite-format.
569 (if (and reftex-docstruct-symbol
570 (symbolp reftex-docstruct-symbol)
571 (get reftex-docstruct-symbol 'reftex-cite-format))
572 (get reftex-docstruct-symbol 'reftex-cite-format)
573 reftex-cite-format))
574
575 (defun reftex-add-index-macros (entry-list)
576 "Add index macro descriptions to `reftex-index-macros-style'.
577 The format of ENTRY-LIST is exactly like `reftex-index-macros'. See there
578 for details.
579 This function makes it possible to support RefTeX from AUCTeX style files.
580 The entries in ENTRY-LIST will be processed after the user settings in
581 `reftex-index-entries', and before the defaults. Any changes made to
582 `reftex-label-alist-style' will raise a flag to the effect that
583 the label information is recompiled on next use."
584 (unless reftex-docstruct-symbol
585 (reftex-tie-multifile-symbols))
586 (when (and reftex-docstruct-symbol
587 (symbolp reftex-docstruct-symbol))
588 (let ((list (get reftex-docstruct-symbol 'reftex-index-macros-style))
589 entry changed)
590 (while entry-list
591 (setq entry (pop entry-list))
592 ;; When it is a symbol, remove all other symbols
593 (and (symbolp entry)
594 (not (memq entry list))
595 (setq list (reftex-remove-symbols-from-list list)))
596 ;; Add to list unless already member
597 (unless (member entry list)
598 (setq reftex-tables-dirty t
599 changed t)
600 (push entry list)))
601 (when changed
602 (put reftex-docstruct-symbol 'reftex-index-macros-style list)))))
603
604 ;;; =========================================================================
605 ;;;
606 ;;; Functions to compile the tables, reset the mode etc.
607
608 ;; The following constants are derived from `reftex-label-alist'.
609
610 ;; Prompt used for label type queries directed to the user.
611 (defconst reftex-type-query-prompt nil)
612
613 ;; Help string for label type queries.
614 (defconst reftex-type-query-help nil)
615
616 ;; Alist relating label type to reference format.
617 (defconst reftex-typekey-to-format-alist nil)
618
619 ;; Alist relating label type to label prefix.
620 (defconst reftex-typekey-to-prefix-alist nil)
621
622 ;; Alist relating environments or macros to label type and context regexp.
623 (defconst reftex-env-or-mac-alist nil)
624
625 ;; List of special environment parser functions
626 (defconst reftex-special-env-parsers nil)
627
628 ;; List of macros carrying a label.
629 (defconst reftex-label-mac-list nil)
630
631 ;; List of environments carrying a label.
632 (defconst reftex-label-env-list nil)
633
634 ;; List of all typekey letters in use.
635 (defconst reftex-typekey-list nil)
636
637 ;; Alist relating magic words to a label type.
638 (defconst reftex-words-to-typekey-alist nil)
639
640 ;; The last list-of-labels entry used in a reference.
641 (defvar reftex-last-used-reference (list nil nil nil nil))
642
643 ;; Alist relating index macros to other info.
644 (defconst reftex-key-to-index-macro-alist nil)
645 ;; Prompt for index macro queries
646 (defconst reftex-query-index-macro-prompt nil)
647 ;; Help string for index macro queries
648 (defconst reftex-query-index-macro-help nil)
649
650 ;; The message when follow-mode is suspended
651 (defconst reftex-no-follow-message
652 "No follow-mode into unvisited file. Press SPC to visit it.")
653 (defconst reftex-no-info-message
654 "%s: info not available, use `\\[reftex-view-crossref]' to get it.")
655
656 ;; Global variables used for communication between functions.
657 (defvar reftex-default-context-position nil)
658 (defvar reftex-location-start nil)
659 (defvar reftex-call-back-to-this-buffer nil)
660 (defvar reftex-select-return-marker (make-marker))
661 (defvar reftex-active-toc nil)
662 (defvar reftex-tex-path nil)
663 (defvar reftex-bib-path nil)
664 (defvar reftex-select-marked nil)
665 (defvar reftex-last-follow-point nil)
666 (defvar reftex-latex-syntax-table nil)
667 (defvar reftex-prefix nil)
668 (defvar reftex-section-levels-all nil)
669 (defvar reftex-buffers-with-changed-invisibility nil)
670 (defvar reftex-callback-fwd t)
671 (defvar reftex-last-toc-master nil
672 "Stores the name of the tex file that `reftex-toc' was last run on.")
673 (defvar reftex-auto-view-crossref-timer nil
674 "The timer used for auto-view-crossref.")
675 ;; Marker for return point from recursive edit
676 (defvar reftex-recursive-edit-marker (make-marker))
677
678 ;; List of buffers created temporarily for lookup, which should be killed.
679 (defvar reftex-buffers-to-kill nil)
680
681 ;; Regexp to find anything.
682 (defvar reftex-section-regexp nil)
683 (defvar reftex-section-or-include-regexp nil)
684 (defvar reftex-index-macro-regexp nil)
685 (defvar reftex-index-level-re nil)
686 (defvar reftex-index-key-end-re nil)
687 (defvar reftex-find-index-entry-regexp-format nil)
688 (defvar reftex-everything-regexp nil)
689 (defvar reftex-everything-regexp-no-index nil)
690 (defvar reftex-index-re nil)
691 (defvar reftex-find-citation-regexp-format
692 "\\\\[a-zA-Z]*cite[*a-zA-Z]*\\*?\\(\\[[^]]*\\]\\|{[^}]*}\\)*{\\([^}]*,\\)?\\(%s\\)[},]")
693 (defvar reftex-find-reference-format
694 "\\\\\\(ref[a-zA-Z]*\\|[a-zA-Z]*ref\\(range\\)?\\)\\*?\\(\\[[^]]*\\]\\|{[^}]*}\\)*{\\(%s\\)}")
695 (defvar reftex-macros-with-labels nil)
696 (defvar reftex-macros-with-index nil)
697 (defvar reftex-index-macro-alist nil)
698 (defvar reftex-find-label-regexp-format nil)
699 (defvar reftex-find-label-regexp-format2 nil)
700
701 (defvar reftex-memory nil
702 "Memorizes old variable values to indicate changes in these variables.")
703
704 ;; A list of all variables in the cache.
705 ;; The cache is used to save the compiled versions of some variables.
706 (defconst reftex-cache-variables
707 '(reftex-memory ;; This MUST ALWAYS be the first!
708
709 ;; Outline
710 reftex-section-levels-all
711
712 ;; Labels
713 reftex-env-or-mac-alist
714 reftex-special-env-parsers
715 reftex-macros-with-labels
716 reftex-label-mac-list
717 reftex-label-env-list
718 reftex-typekey-list
719 reftex-typekey-to-format-alist
720 reftex-typekey-to-prefix-alist
721 reftex-words-to-typekey-alist
722 reftex-type-query-prompt
723 reftex-type-query-help
724
725 ;; Index
726 reftex-index-macro-alist
727 reftex-macros-with-index
728 reftex-query-index-macro-prompt
729 reftex-query-index-macro-help
730 reftex-key-to-index-macro-alist
731
732 ;; Regular expressions
733 reftex-section-regexp
734 reftex-section-or-include-regexp
735 reftex-index-re
736 reftex-everything-regexp
737 reftex-everything-regexp-no-index
738 reftex-find-label-regexp-format
739 reftex-find-label-regexp-format2
740 reftex-find-index-entry-regexp-format
741 ))
742
743 (defun reftex-ensure-compiled-variables ()
744 ;; Recompile the label alist when necessary
745 (let* ((mem reftex-memory)
746 (cache (get reftex-docstruct-symbol 'reftex-cache))
747 (cmem (car cache))
748 (alist reftex-label-alist)
749 (levels (get reftex-docstruct-symbol 'reftex-section-levels))
750 (style (get reftex-docstruct-symbol 'reftex-label-alist-style))
751 (default reftex-default-label-alist-entries)
752 (index reftex-index-macros)
753 (istyle (get reftex-docstruct-symbol 'reftex-index-macros-style)))
754 (cond
755 (reftex-tables-dirty (reftex-compile-variables))
756 ((and (eq alist (nth 0 mem))
757 (eq levels (nth 1 mem))
758 (eq style (nth 2 mem))
759 (eq default (nth 3 mem))
760 (eq index (nth 4 mem))
761 (eq istyle (nth 5 mem)))) ;; everything is OK
762 ((and (eq alist (nth 0 cmem))
763 (eq levels (nth 1 cmem))
764 (eq style (nth 2 cmem))
765 (eq default (nth 2 cmem))
766 (eq index (nth 4 cmem))
767 (eq istyle (nth 5 cmem)))
768 ;; restore the cache
769 (message "Restoring cache")
770 (mapcar (lambda (sym) (set sym (pop cache))) reftex-cache-variables))
771 (t (reftex-compile-variables)))))
772
773 (defun reftex-reset-mode ()
774 "Reset RefTeX Mode.
775 This will re-compile the configuration information and remove all
776 current scanning information and the parse file to enforce a rescan
777 on next use."
778 (interactive)
779
780 ;; Reset the file search path variables
781 (loop for prop in '(status master-dir recursive-path rec-type) do
782 (put 'reftex-tex-path prop nil)
783 (put 'reftex-bib-path prop nil))
784
785 ;; Kill temporary buffers associated with RefTeX - just in case they
786 ;; were not cleaned up properly
787 (save-excursion
788 (let ((buffer-list '("*RefTeX Help*" "*RefTeX Select*"
789 "*Duplicate Labels*" "*toc*" " *RefTeX-scratch*"))
790 buf)
791 (while (setq buf (pop buffer-list))
792 (if (get-buffer buf)
793 (kill-buffer buf))))
794 (reftex-erase-all-selection-and-index-buffers))
795
796 ;; Make sure the current document will be rescanned soon.
797 (reftex-reset-scanning-information)
798
799 ;; Remove any parse info file
800 (reftex-access-parse-file 'kill)
801
802 ;; Plug functions into AUCTeX if the user option says so.
803 (and reftex-plug-into-AUCTeX
804 (reftex-plug-into-AUCTeX))
805
806 (reftex-compile-variables))
807
808 (defun reftex-reset-scanning-information ()
809 "Reset the symbols containing information from buffer scanning.
810 This enforces rescanning the buffer on next use."
811 (if (string= reftex-last-toc-master (reftex-TeX-master-file))
812 (reftex-erase-buffer "*toc*"))
813 (let ((symlist reftex-multifile-symbols)
814 symbol)
815 (while symlist
816 (setq symbol (car symlist)
817 symlist (cdr symlist))
818 (if (and (symbolp (symbol-value symbol))
819 (not (null (symbol-value symbol))))
820 (set (symbol-value symbol) nil)))))
821
822 (defun reftex-erase-all-selection-and-index-buffers ()
823 ;; Remove all selection buffers associated with current document.
824 (mapcar
825 (lambda (type)
826 (reftex-erase-buffer (reftex-make-selection-buffer-name type)))
827 reftex-typekey-list)
828 ;; Kill all index buffers
829 (mapcar
830 (lambda (tag)
831 (reftex-kill-buffer (reftex-make-index-buffer-name tag)))
832 (cdr (assoc 'index-tags (symbol-value reftex-docstruct-symbol)))))
833
834 (defun reftex-compile-variables ()
835 ;; Compile the information in reftex-label-alist & Co.
836
837 (message "Compiling label environment definitions...")
838
839 ;; Update AUCTeX style information
840 (when (and (featurep 'tex-site) (fboundp 'TeX-update-style))
841 (condition-case nil (TeX-update-style) (error nil)))
842
843 ;; Record that we have done this, and what we have used.
844 (setq reftex-tables-dirty nil)
845 (setq reftex-memory
846 (list reftex-label-alist
847 (get reftex-docstruct-symbol 'reftex-section-levels)
848 (get reftex-docstruct-symbol 'reftex-label-alist-style)
849 reftex-default-label-alist-entries
850 reftex-index-macros
851 (get reftex-docstruct-symbol 'reftex-index-macros-style)))
852
853 ;; Compile information in reftex-label-alist
854 (let ((all (reftex-uniquify-by-car
855 (reftex-splice-symbols-into-list
856 (append reftex-label-alist
857 (get reftex-docstruct-symbol
858 'reftex-label-alist-style)
859 reftex-default-label-alist-entries)
860 reftex-label-alist-builtin)
861 '(nil)))
862 (all-index (reftex-uniquify-by-car
863 (reftex-splice-symbols-into-list
864 (append reftex-index-macros
865 (get reftex-docstruct-symbol
866 'reftex-index-macros-style)
867 '(default))
868 reftex-index-macros-builtin)))
869 entry env-or-mac typekeychar typekey prefix context word
870 fmt reffmt labelfmt wordlist qh-list macros-with-labels
871 nargs nlabel opt-args cell sum i
872 macro verify repeat nindex tag key toc-level toc-levels)
873
874 (setq reftex-words-to-typekey-alist nil
875 reftex-typekey-list nil
876 reftex-typekey-to-format-alist nil
877 reftex-typekey-to-prefix-alist nil
878 reftex-env-or-mac-alist nil
879 reftex-label-env-list nil
880 reftex-label-mac-list nil)
881 (while all
882 (catch 'next-entry
883 (setq entry (car all)
884 env-or-mac (car entry)
885 entry (cdr entry)
886 all (cdr all))
887 (if (null env-or-mac)
888 (setq env-or-mac ""))
889 (if (stringp (car entry))
890 ;; This is before version 2.00 - convert entry to new format
891 ;; This is just to keep old users happy
892 (setq entry (cons (string-to-char (car entry))
893 (cons (concat (car entry) ":")
894 (cdr entry)))))
895 (setq typekeychar (nth 0 entry)
896 typekey (if typekeychar (char-to-string typekeychar) nil)
897 prefix (nth 1 entry)
898 fmt (nth 2 entry)
899 context (nth 3 entry)
900 wordlist (nth 4 entry)
901 toc-level (nth 5 entry))
902 (if (stringp wordlist)
903 ;; This is before version 2.04 - convert to new format
904 (setq wordlist (nthcdr 4 entry)))
905
906 (if (and (stringp fmt)
907 (string-match "@" fmt))
908 ;; Special syntax for specifying a label format
909 (setq fmt (split-string fmt "@+"))
910 (setq fmt (list "\\label{%s}" fmt)))
911 (setq labelfmt (car fmt)
912 reffmt (nth 1 fmt))
913 ;; Note a new typekey
914 (if typekey
915 (add-to-list 'reftex-typekey-list typekey))
916 (if (and typekey prefix
917 (not (assoc typekey reftex-typekey-to-prefix-alist)))
918 (add-to-list 'reftex-typekey-to-prefix-alist
919 (cons typekey prefix)))
920 ;; Check if this is a macro or environment
921 (cond
922 ((symbolp env-or-mac)
923 ;; A special parser function
924 (unless (fboundp env-or-mac)
925 (message "Warning: %s does not seem to be a valid function"
926 env-or-mac))
927 (setq nargs nil nlabel nil opt-args nil)
928 (add-to-list 'reftex-special-env-parsers env-or-mac)
929 (setq env-or-mac (symbol-name env-or-mac)))
930 ((string-match "\\`\\\\" env-or-mac)
931 ;; It's a macro
932 (let ((result (reftex-parse-args env-or-mac)))
933 (setq env-or-mac (or (first result) env-or-mac)
934 nargs (second result)
935 nlabel (third result)
936 opt-args (fourth result))
937 (if nlabel (add-to-list 'macros-with-labels env-or-mac)))
938 (if typekey (add-to-list 'reftex-label-mac-list env-or-mac)))
939 (t
940 ;; It's an environment
941 (setq nargs nil nlabel nil opt-args nil)
942 (cond ((string= env-or-mac "any"))
943 ((string= env-or-mac ""))
944 ((string= env-or-mac "section"))
945 (t
946 (add-to-list 'reftex-label-env-list env-or-mac)
947 (if toc-level
948 (let ((string (format "begin{%s}" env-or-mac)))
949 (or (assoc string toc-levels)
950 (push (cons string toc-level) toc-levels))))))))
951 ;; Translate some special context cases
952 (when (assq context reftex-default-context-regexps)
953 (setq context
954 (format
955 (cdr (assq context reftex-default-context-regexps))
956 (regexp-quote env-or-mac))))
957 ;; See if this is the first format for this typekey
958 (and reffmt
959 (not (assoc typekey reftex-typekey-to-format-alist))
960 (push (cons typekey reffmt) reftex-typekey-to-format-alist))
961 ;; See if this is the first definition for this env-or-mac
962 (and (not (string= env-or-mac "any"))
963 (not (string= env-or-mac ""))
964 (not (assoc env-or-mac reftex-env-or-mac-alist))
965 (push (list env-or-mac typekey context labelfmt
966 nargs nlabel opt-args)
967 reftex-env-or-mac-alist))
968 ;; Are the magic words regular expressions? Quote normal words.
969 (if (eq (car wordlist) 'regexp)
970 (setq wordlist (cdr wordlist))
971 (setq wordlist (mapcar 'regexp-quote wordlist)))
972 ;; Remember the first association of each word.
973 (while (stringp (setq word (pop wordlist)))
974 (or (assoc word reftex-words-to-typekey-alist)
975 (push (cons word typekey) reftex-words-to-typekey-alist)))
976 (cond
977 ((string= "" env-or-mac) nil)
978 ((setq cell (assoc typekey qh-list))
979 (push env-or-mac (cdr cell)))
980 (typekey
981 (push (list typekey env-or-mac) qh-list)))))
982
983 (setq reftex-typekey-to-prefix-alist
984 (nreverse reftex-typekey-to-prefix-alist))
985
986 ;; Prepare the typekey query prompt and help string.
987 (setq qh-list
988 (sort qh-list
989 (lambda (x1 x2)
990 (string< (downcase (car x1)) (downcase (car x2))))))
991 (setq reftex-type-query-prompt
992 (concat "Label type: ["
993 (mapconcat (lambda(x) (format "%s" (car x)))
994 qh-list "")
995 "]"))
996 ;; In the help string, we need to wrap lines...
997 (setq reftex-type-query-help
998 (concat
999 "SELECT A LABEL TYPE:\n--------------------\n"
1000 (mapconcat
1001 (lambda(x)
1002 (setq sum 0)
1003 (format " [%s] %s"
1004 (car x)
1005 (mapconcat (lambda(env)
1006 (setq sum (+ sum (length env)))
1007 (if (< sum 60)
1008 env
1009 (setq sum 0)
1010 (concat "\n " env)))
1011 (cdr x) " ")))
1012 qh-list "\n")))
1013
1014 ;; Convert magic words to regular expressions. We make regular expressions
1015 ;; which allow for some chars from the ref format to be in the buffer.
1016 ;; These characters will be seen and removed.
1017 (setq reftex-words-to-typekey-alist
1018 (mapcar
1019 (lambda (x)
1020 (setq word (car x)
1021 typekey (cdr x)
1022 fmt (cdr (assoc typekey reftex-typekey-to-format-alist)))
1023 (setq word (concat "\\W\\(" word "[ \t\n\r]*\\)\\("))
1024 (setq i 0)
1025 (while (and (< i 10) ; maximum number of format chars allowed
1026 (< i (length fmt))
1027 (not (member (aref fmt i) '(?%))))
1028 (setq word (concat word "\\|" (regexp-quote
1029 (substring fmt 0 (1+ i)))))
1030 (incf i))
1031 (cons (concat word "\\)\\=") typekey))
1032 (nreverse reftex-words-to-typekey-alist)))
1033
1034 ;; Parse the index macros
1035 (setq reftex-index-macro-alist nil
1036 reftex-key-to-index-macro-alist nil
1037 reftex-macros-with-index nil)
1038 (while all-index
1039 (setq entry (car all-index)
1040 macro (car entry)
1041 tag (nth 1 entry)
1042 key (nth 2 entry)
1043 prefix (or (nth 3 entry) "")
1044 verify (nth 4 entry)
1045 ;; For repeat, we need to be compatible with older code
1046 ;; This information used to be given only for the default macro,
1047 ;; but later we required to have it for *every* index macro
1048 repeat (cond ((> (length entry) 5) (nth 5 entry))
1049 ((and (eq key (car reftex-index-default-macro))
1050 (> (length reftex-index-default-macro) 2))
1051 ;; User has old setting - respect it
1052 (nth 2 reftex-index-default-macro))
1053 (t t))
1054 all-index (cdr all-index))
1055 (let ((result (reftex-parse-args macro)))
1056 (setq macro (or (first result) macro)
1057 nargs (second result)
1058 nindex (third result)
1059 opt-args (fourth result))
1060 (unless (member macro reftex-macros-with-index)
1061 ;; 0 1 2 3 4 5 6 7
1062 (push (list macro tag prefix verify nargs nindex opt-args repeat)
1063 reftex-index-macro-alist)
1064 (or (assoc key reftex-key-to-index-macro-alist)
1065 (push (list key macro) reftex-key-to-index-macro-alist))
1066 (push macro reftex-macros-with-index))))
1067 ;; Make the prompt and help string for index macros query
1068 (setq reftex-key-to-index-macro-alist
1069 (sort reftex-key-to-index-macro-alist
1070 (lambda (a b) (< (downcase (car a)) (downcase (car b))))))
1071 (setq reftex-query-index-macro-prompt
1072 (concat "Index macro: ["
1073 (mapconcat (lambda (x) (char-to-string (car x)))
1074 reftex-key-to-index-macro-alist "")
1075 "]"))
1076 (setq i 0
1077 reftex-query-index-macro-help
1078 (concat
1079 "SELECT A MACRO:\n---------------\n"
1080 (mapconcat
1081 (lambda(x)
1082 (format "[%c] %-20.20s%s" (car x) (nth 1 x)
1083 (if (= 0 (mod (incf i) 3)) "\n" "")))
1084 reftex-key-to-index-macro-alist "")))
1085
1086 ;; Make the full list of section levels
1087 (setq reftex-section-levels-all
1088 (append toc-levels
1089 (get reftex-docstruct-symbol 'reftex-section-levels)
1090 reftex-section-levels))
1091
1092 ;; Calculate the regular expressions
1093 (let* ((wbol "\\(\\`\\|[\n\r]\\)[ \t]*")
1094 (label-re "\\\\label{\\([^}]*\\)}")
1095 (include-re (concat wbol "\\\\\\(include\\|input\\)[{ \t]+\\([^} \t\n\r]+\\)"))
1096 (section-re
1097 (concat wbol "\\\\\\("
1098 (mapconcat (lambda (x) (regexp-quote (car x)))
1099 reftex-section-levels-all "\\|")
1100 "\\)\\*?\\(\\[[^]]*\\]\\)?{?"))
1101 (appendix-re (concat wbol "\\(\\\\appendix\\)"))
1102 (macro-re
1103 (if macros-with-labels
1104 (concat "\\("
1105 (mapconcat 'regexp-quote macros-with-labels "\\|")
1106 "\\)[[{]")
1107 ""))
1108 (index-re
1109 (concat "\\("
1110 (mapconcat 'regexp-quote reftex-macros-with-index "\\|")
1111 "\\)[[{]"))
1112 (find-index-re-format
1113 (concat "\\("
1114 (mapconcat 'regexp-quote reftex-macros-with-index "\\|")
1115 "\\)\\([[{][^]}]*[]}]\\)*[[{]\\(%s\\)[]}]"))
1116 (find-label-re-format
1117 (concat "\\("
1118 (mapconcat 'regexp-quote (append '("\\label")
1119 macros-with-labels) "\\|")
1120 "\\)\\([[{][^]}]*[]}]\\)*[[{]\\(%s\\)[]}]"))
1121 (index-level-re
1122 (regexp-quote (nth 0 reftex-index-special-chars)))
1123 (index-key-end-re ;; ^]- not allowed
1124 (concat "[^" (nth 3 reftex-index-special-chars) "]"
1125 "[" (nth 1 reftex-index-special-chars)
1126 (nth 2 reftex-index-special-chars) "]"))
1127 )
1128 (setq reftex-section-regexp section-re
1129 reftex-section-or-include-regexp
1130 (concat section-re "\\|" include-re)
1131 reftex-everything-regexp
1132 (concat label-re "\\|" section-re "\\|" include-re
1133 "\\|" appendix-re
1134 "\\|" index-re
1135 (if macros-with-labels "\\|" "") macro-re)
1136 reftex-everything-regexp-no-index
1137 (concat label-re "\\|" section-re "\\|" include-re
1138 "\\|" appendix-re
1139 "\\|" "\\(\\\\6\\\\3\\\\1\\)" ; This is unlikely to match
1140 (if macros-with-labels "\\|" "") macro-re)
1141 reftex-index-re index-re
1142 reftex-index-level-re index-level-re
1143 reftex-index-key-end-re index-key-end-re
1144 reftex-macros-with-labels macros-with-labels
1145 reftex-find-index-entry-regexp-format find-index-re-format
1146 reftex-find-label-regexp-format find-label-re-format
1147 reftex-find-label-regexp-format2
1148 "\\([]} \t\n\r]\\)\\([[{]\\)\\(%s\\)[]}]")
1149 (message "Compiling label environment definitions...done")))
1150 (put reftex-docstruct-symbol 'reftex-cache
1151 (mapcar 'symbol-value reftex-cache-variables)))
1152
1153 (defun reftex-parse-args (macro)
1154 ;; Return a list of macro name, nargs, arg-nr which is label and a list of
1155 ;; optional argument indices.
1156 (if (string-match "[[{]\\*?[]}]" macro)
1157 (progn
1158 (let ((must-match (substring macro 0 (match-beginning 0)))
1159 (args (substring macro (match-beginning 0)))
1160 opt-list nlabel (cnt 0))
1161 (while (string-match "\\`[[{]\\(\\*\\)?[]}]" args)
1162 (incf cnt)
1163 (when (eq ?\[ (string-to-char args))
1164 (push cnt opt-list))
1165 (when (and (match-end 1)
1166 (not nlabel))
1167 (setq nlabel cnt))
1168 (setq args (substring args (match-end 0))))
1169 (list must-match cnt nlabel opt-list)))
1170 nil))
1171
1172 ;;; =========================================================================
1173 ;;;
1174 ;;; Accessing the parse information
1175
1176 (defun reftex-access-scan-info (&optional rescan file)
1177 "Ensure access to the scanning info for the current file."
1178 ;; When the multifile symbols are not yet tied,
1179 ;; tie them. When they are empty or RESCAN is non-nil, scan the document.
1180 ;; But, when RESCAN is -1, don't rescan even if docstruct is empty.
1181 ;; When FILE is non-nil, parse only from that file.
1182
1183 ;; Make sure we have the symbols tied
1184 (if (eq reftex-docstruct-symbol nil)
1185 ;; Symbols are not yet tied: Tie them.
1186 (reftex-tie-multifile-symbols))
1187
1188 (reftex-ensure-compiled-variables)
1189
1190 (when (or (null (symbol-value reftex-docstruct-symbol))
1191 (member rescan '(t 1 (4) (16))))
1192 ;; The docstruct will change: Remove selection buffers.
1193 (save-excursion
1194 (reftex-erase-buffer "*toc*")
1195 (reftex-erase-all-selection-and-index-buffers)))
1196
1197 (if (and (null (symbol-value reftex-docstruct-symbol))
1198 (not (member rescan '(t 1 (4) (16))))
1199 reftex-save-parse-info)
1200 ;; Try to read the stuff from a file
1201 (reftex-access-parse-file 'read))
1202
1203 (cond
1204 ((equal rescan -1)) ;; We are not allowed to scan.
1205 ((not (symbol-value reftex-docstruct-symbol))
1206 ;; Scan the whole document
1207 (reftex-do-parse 1 file))
1208 ((member rescan '(t 1 (4) (16)))
1209 ;; Scan whatever was required by the caller.
1210 (reftex-do-parse rescan file))))
1211
1212 (defun reftex-silence-toc-markers (list n)
1213 ;; Set all toc markers in the first N entries in list to nil
1214 (while (and list (> (decf n) -1))
1215 (and (eq (car (car list)) 'toc)
1216 (markerp (nth 4 (car list)))
1217 (set-marker (nth 4 (car list)) nil))
1218 (pop list)))
1219
1220 (defun reftex-access-parse-file (action)
1221 "Perform ACTION on the parse file (the .rel file).
1222 Valid actions are: readable, restore, read, kill, write."
1223 (let* ((list (symbol-value reftex-docstruct-symbol))
1224 (docstruct-symbol reftex-docstruct-symbol)
1225 (master (reftex-TeX-master-file))
1226 (enable-local-variables nil)
1227 (file (if (string-match "\\.[a-zA-Z]+\\'" master)
1228 (concat (substring master 0 (match-beginning 0))
1229 reftex-parse-file-extension)
1230 (concat master reftex-parse-file-extension))))
1231 (cond
1232 ((eq action 'readable)
1233 (file-readable-p file))
1234 ((eq action 'restore)
1235 (put reftex-docstruct-symbol 'modified nil)
1236 (if (eq reftex-docstruct-symbol nil)
1237 ;; Symbols are not yet tied: Tie them.
1238 (reftex-tie-multifile-symbols))
1239 (if (file-exists-p file)
1240 ;; load the file and return t for success
1241 (condition-case nil
1242 (progn (load-file file) t)
1243 (error (set reftex-docstruct-symbol nil)
1244 (error "Error while loading file %s" file)))
1245 ;; Throw an exception if the file does not exist
1246 (error "No restore file %s" file)))
1247 ((eq action 'read)
1248 (put reftex-docstruct-symbol 'modified nil)
1249 (if (file-exists-p file)
1250 ;; load the file and return t for success
1251 (condition-case nil
1252 (progn
1253 (load-file file)
1254 (reftex-check-parse-consistency)
1255 t)
1256 (error (message "Error while restoring file %s" file)
1257 (set reftex-docstruct-symbol nil)
1258 nil))
1259 ;; return nil for failure, but no exception
1260 nil))
1261 ((eq action 'kill)
1262 ;; Remove the file
1263 (when (and (file-exists-p file) (file-writable-p file))
1264 (message "Unlinking file %s" file)
1265 (delete-file file)))
1266 (t
1267 (put docstruct-symbol 'modified nil)
1268 (save-excursion
1269 (if (file-writable-p file)
1270 (progn
1271 (message "Writing parse file %s" (abbreviate-file-name file))
1272 (find-file file)
1273 (erase-buffer)
1274 (insert (format ";; RefTeX parse info file\n"))
1275 (insert (format ";; File: %s\n" master))
1276 (insert (format ";; User: %s (%s)\n\n"
1277 (user-login-name) (user-full-name)))
1278 (insert "(set reftex-docstruct-symbol '(\n\n")
1279 (let ((standard-output (current-buffer)))
1280 (mapcar
1281 (lambda (x)
1282 (cond ((eq (car x) 'toc)
1283 ;; A toc entry. Do not save the marker.
1284 ;; Save the markers position at position 8
1285 (print (list 'toc "toc" (nth 2 x) (nth 3 x)
1286 nil (nth 5 x) (nth 6 x) (nth 7 x)
1287 (or (and (markerp (nth 4 x))
1288 (marker-position (nth 4 x)))
1289 (nth 8 x)))))
1290 ((and (not (eq t reftex-support-index))
1291 (eq (car x) 'index))
1292 ;; Don't save index entries
1293 )
1294 (t (print x))))
1295 list))
1296 (insert "))\n\n")
1297 (save-buffer 0)
1298 (kill-buffer (current-buffer)))
1299 (error "Cannot write to file %s" file)))
1300 t))))
1301
1302 (defun reftex-check-parse-consistency ()
1303 ;; Check if parse file is consistent, throw an error if not.
1304
1305 ;; Check if the master is the same: when moving a document, this will see it.
1306 (let* ((real-master (reftex-TeX-master-file))
1307 (parsed-master
1308 (nth 1 (assq 'bof (symbol-value reftex-docstruct-symbol)))))
1309 (unless (string= (file-truename real-master) (file-truename parsed-master))
1310 (message "Master file name in load file is different: %s versus %s"
1311 parsed-master real-master)
1312 (error "Master file name error")))
1313
1314 ;; Check for the existence of all document files
1315 ;;; (let* ((all (symbol-value reftex-docstruct-symbol)))
1316 ;;; (while all
1317 ;;; (when (and (eq (car (car all)) 'bof)
1318 ;;; (not (file-regular-p (nth 1 (car all)))))
1319 ;;; (message "File %s in saved parse info not avalable" (cdr (car all)))
1320 ;;; (error "File not found"))
1321 ;;; (setq all (cdr all))))
1322 )
1323
1324 (defun reftex-select-external-document (xr-alist xr-index)
1325 ;; Return index of an external document.
1326 (let* ((len (length xr-alist)) (highest (1- (+ ?0 len)))
1327 (prompt (format "[%c-%c] Select TAB: Read prefix with completion"
1328 ?0 highest))
1329 key prefix)
1330 (cond
1331 ((= len 1)
1332 (message "No external documents available")
1333 (ding) (sit-for 1) 0)
1334 ((= len 2)
1335 (- 1 xr-index))
1336 (t
1337 (save-excursion
1338 (let* ((length (apply 'max (mapcar
1339 (lambda(x) (length (car x))) xr-alist)))
1340 (fmt (format " [%%c] %%-%ds %%s\n" length))
1341 (n (1- ?0)))
1342 (setq key
1343 (reftex-select-with-char
1344 prompt
1345 (concat
1346 "SELECT EXTERNAL DOCUMENT\n------------------------\n"
1347 (mapconcat
1348 (lambda (x)
1349 (format fmt (incf n) (or (car x) "")
1350 (abbreviate-file-name (cdr x))))
1351 xr-alist ""))
1352 nil t))
1353 (cond
1354 ((and (>= key ?0) (<= key highest)) (- key ?0))
1355 ((= key ?\C-i)
1356 (setq prefix (completing-read "Prefix: " xr-alist nil t))
1357 (- len (length (memq (assoc prefix xr-alist) xr-alist))))
1358 (t (error "Illegal document selection [%c]" key)))))))))
1359
1360 ;;; =========================================================================
1361 ;;;
1362 ;;; Finding files
1363
1364 (defun reftex-locate-file (file type master-dir &optional die)
1365 "Find FILE of type TYPE in MASTER-DIR or on the path associcted with TYPE.
1366 If the file does not have any of the legal extensions for TYPE,
1367 try first the default extension and only then the naked file name.
1368 When DIE is non-nil, throw an error if file not found."
1369 (let* ((rec-values (if reftex-search-unrecursed-path-first '(nil t) '(t)))
1370 (extensions (cdr (assoc type reftex-file-extensions)))
1371 (def-ext (car extensions))
1372 (ext-re (concat "\\("
1373 (mapconcat 'regexp-quote extensions "\\|")
1374 "\\)\\'"))
1375 (files (if (string-match ext-re file)
1376 (cons file nil)
1377 (cons (concat file def-ext) file)))
1378 path old-path file1)
1379 (cond
1380 ((file-name-absolute-p file)
1381 (setq file1
1382 (or
1383 (and (car files) (file-regular-p (car files)) (car files))
1384 (and (cdr files) (file-regular-p (cdr files)) (cdr files)))))
1385 ((and reftex-use-external-file-finders
1386 (assoc type reftex-external-file-finders))
1387 (setq file1 (reftex-find-file-externally file type master-dir)))
1388 (t
1389 (while (and (null file1) rec-values)
1390 (setq path (reftex-access-search-path
1391 type (pop rec-values) master-dir file))
1392 (if (or (null old-path)
1393 (not (eq old-path path)))
1394 (setq old-path path
1395 path (cons master-dir path)
1396 file1 (or (and (car files)
1397 (reftex-find-file-on-path
1398 (car files) path master-dir))
1399 (and (cdr files)
1400 (reftex-find-file-on-path
1401 (cdr files) path master-dir))))))))
1402 (cond (file1 file1)
1403 (die (error "No such file: %s" file) nil)
1404 (t (message "No such file: %s (ignored)" file) nil))))
1405
1406 (defun reftex-find-file-externally (file type &optional master-dir)
1407 ;; Use external program to find FILE.
1408 ;; The program is taken from `reftex-external-file-finders'.
1409 ;; Interprete relative path definitions starting from MASTER-DIR.
1410 (let ((default-directory (or master-dir default-directory))
1411 (prg (cdr (assoc type reftex-external-file-finders)))
1412 out)
1413 (if (string-match "%f" prg)
1414 (setq prg (replace-match file t t prg)))
1415 (setq out (apply 'reftex-process-string (split-string prg)))
1416 (if (string-match "[ \t\n]+\\'" out) ; chomp
1417 (setq out (replace-match "" nil nil out)))
1418 (cond ((equal out "") nil)
1419 ((file-regular-p out) (expand-file-name out master-dir))
1420 (t nil))))
1421
1422 (defun reftex-process-string (program &rest args)
1423 "Execute PROGRAM with arguments ARGS and return its STDOUT as a string."
1424 (let ((calling-dir default-directory)) ; remember default directory
1425 (with-output-to-string
1426 (with-current-buffer standard-output
1427 (let ((default-directory calling-dir)) ; set default directory
1428 (apply 'call-process program nil '(t nil) nil args))))))
1429
1430 (defun reftex-access-search-path (type &optional recurse master-dir file)
1431 ;; Access path from environment variables. TYPE is either "tex" or "bib".
1432 ;; When RECURSE is t, expand path elements ending in `//' recursively.
1433 ;; Relative path elements are left as they are. However, relative recursive
1434 ;; elements are expanded with MASTER-DIR as default directory.
1435 ;; The expanded path is cached for the next search.
1436 ;; FILE is just for the progress message.
1437 ;; Returns the derived path.
1438 (let* ((pathvar (intern (concat "reftex-" type "-path"))))
1439 (when (null (get pathvar 'status))
1440 ;; Get basic path
1441 (set pathvar
1442 (reftex-uniq
1443 (reftex-parse-colon-path
1444 (mapconcat
1445 (lambda(x)
1446 (if (string-match "^!" x)
1447 (apply 'reftex-process-string
1448 (split-string (substring x 1)))
1449 (or (getenv x) x)))
1450 ;; For consistency, the next line should look like this:
1451 ;; (cdr (assoc type reftex-path-environment))
1452 ;; However, historically we have separate options for the
1453 ;; environment variables, so we have to do this:
1454 (symbol-value (intern (concat "reftex-" type
1455 "path-environment-variables")))
1456 path-separator))))
1457 (put pathvar 'status 'split)
1458 ;; Check if we have recursive elements
1459 (let ((path (symbol-value pathvar)) dir rec)
1460 (while (setq dir (pop path))
1461 (when (string= (substring dir -2) "//")
1462 (if (file-name-absolute-p dir)
1463 (setq rec (or rec 'absolute))
1464 (setq rec 'relative))))
1465 (put pathvar 'rec-type rec)))
1466
1467 (if recurse
1468 ;; Return the recursive expansion of the path
1469 (cond
1470 ((not (get pathvar 'rec-type))
1471 ;; Path does not contain recursive elements - use simple path
1472 (symbol-value pathvar))
1473 ((or (not (get pathvar 'recursive-path))
1474 (and (eq (get pathvar 'rec-type) 'relative)
1475 (not (equal master-dir (get pathvar 'master-dir)))))
1476 ;; Either: We don't have a recursive expansion yet.
1477 ;; or: Relative recursive path elements need to be expanded
1478 ;; relative to new default directory
1479 (message "Expanding search path to find %s file: %s ..." type file)
1480 (put pathvar 'recursive-path
1481 (reftex-expand-path (symbol-value pathvar) master-dir))
1482 (put pathvar 'master-dir master-dir)
1483 (get pathvar 'recursive-path))
1484 (t
1485 ;; Recursive path computed earlier is still OK.
1486 (get pathvar 'recursive-path)))
1487 ;; The simple path was requested
1488 (symbol-value pathvar))))
1489
1490 (defun reftex-find-file-on-path (file path &optional def-dir)
1491 ;; Find FILE along the directory list PATH.
1492 ;; DEF-DIR is the default directory for expanding relative path elements.
1493 (catch 'exit
1494 (when (file-name-absolute-p file)
1495 (if (file-regular-p file)
1496 (throw 'exit file)
1497 (throw 'exit nil)))
1498 (let* ((thepath path) file1 dir)
1499 (while (setq dir (pop thepath))
1500 (when (string= (substring dir -2) "//")
1501 (setq dir (substring dir 0 -1)))
1502 (setq file1 (expand-file-name file (expand-file-name dir def-dir)))
1503 (if (file-regular-p file1)
1504 (throw 'exit file1)))
1505 ;; No such file
1506 nil)))
1507
1508 (defun reftex-parse-colon-path (path)
1509 ;; Like parse-colon-parse, but // or /~ are left alone.
1510 ;; Trailing ! or !! will be converted into `//' (emTeX convention)
1511 (mapcar
1512 (lambda (dir)
1513 (if (string-match "\\(//+\\|/*!+\\)\\'" dir)
1514 (setq dir (replace-match "//" t t dir)))
1515 (file-name-as-directory dir))
1516 (delete "" (split-string path (concat path-separator "+")))))
1517
1518 (defun reftex-expand-path (path &optional default-dir)
1519 ;; Expand parts of path ending in `//' recursively into directory list.
1520 ;; Relative recursive path elements are expanded relative to DEFAULT-DIR.
1521 (let (path1 dir recursive)
1522 (while (setq dir (pop path))
1523 (if (setq recursive (string= (substring dir -2) "//"))
1524 (setq dir (substring dir 0 -1)))
1525 (if (and recursive
1526 (not (file-name-absolute-p dir)))
1527 (setq dir (expand-file-name dir default-dir)))
1528 (if recursive
1529 ;; Expand recursively
1530 (setq path1 (append (reftex-recursive-directory-list dir) path1))
1531 ;; Keep unchanged
1532 (push dir path1)))
1533 (nreverse path1)))
1534
1535 (defun reftex-recursive-directory-list (dir)
1536 ;; Return a list of all directories below DIR, including DIR itself
1537 (let ((path (list dir)) path1 file files)
1538 (while (setq dir (pop path))
1539 (when (file-directory-p dir)
1540 (setq files (nreverse (directory-files dir t "[^.]")))
1541 (while (setq file (pop files))
1542 (if (file-directory-p file)
1543 (push (file-name-as-directory file) path)))
1544 (push dir path1)))
1545 path1))
1546
1547 (defun reftex-uniq (list)
1548 (let (new)
1549 (while list
1550 (or (member (car list) new)
1551 (push (car list) new))
1552 (pop list))
1553 (nreverse new)))
1554
1555
1556 ;;; =========================================================================
1557 ;;;
1558 ;;; Parser functions
1559
1560 (autoload 'reftex-parse-one "reftex-parse"
1561 "Re-parse this file." t)
1562 (autoload 'reftex-parse-all "reftex-parse"
1563 "Re-parse entire document." t)
1564 (autoload 'reftex-do-parse "reftex-parse")
1565 (autoload 'reftex-where-am-I "reftex-parse")
1566 (autoload 'reftex-init-section-numbers "reftex-parse")
1567 (autoload 'reftex-section-info "reftex-parse")
1568 (autoload 'reftex-section-number "reftex-parse")
1569 (autoload 'reftex-what-macro "reftex-parse")
1570 (autoload 'reftex-what-macro-safe "reftex-parse")
1571 (autoload 'reftex-index-info "reftex-parse")
1572 (autoload 'reftex-index-info-safe "reftex-parse")
1573 (autoload 'reftex-short-context "reftex-parse")
1574 (autoload 'reftex-what-environment "reftex-parse")
1575 (autoload 'reftex-what-special-env "reftex-parse")
1576 (autoload 'reftex-move-over-touching-args "reftex-parse")
1577 (autoload 'reftex-notice-new "reftex-parse")
1578 (autoload 'reftex-nth-arg "reftex-parse")
1579 (autoload 'reftex-locate-bibliography-files "reftex-parse")
1580 (autoload 'reftex-all-document-files "reftex-parse")
1581 (autoload 'reftex-ensure-index-support "reftex-parse")
1582 (autoload 'reftex-everything-regexp "reftex-parse")
1583
1584
1585 ;;; =========================================================================
1586 ;;;
1587 ;;; Labels and References
1588
1589 (autoload 'reftex-label-location "reftex-ref")
1590 (autoload 'reftex-label-info-update "reftex-ref")
1591 (autoload 'reftex-label-info "reftex-ref")
1592 (autoload 'reftex-label "reftex-ref"
1593 "Insert a unique label." t)
1594 (autoload 'reftex-reference "reftex-ref"
1595 "Make a LaTeX reference." t)
1596 (autoload 'reftex-show-label-location "reftex-ref")
1597 (autoload 'reftex-query-label-type "reftex-ref")
1598
1599
1600 ;;; =========================================================================
1601 ;;;
1602 ;;; Table of contents
1603
1604 (autoload 'reftex-toc "reftex-toc"
1605 "Show the table of contents for the current document." t)
1606
1607
1608 ;;; =========================================================================
1609 ;;;
1610 ;;; BibTeX citations.
1611
1612 (autoload 'reftex-make-cite-echo-string "reftex-cite")
1613 (autoload 'reftex-get-bibfile-list "reftex-cite")
1614 (autoload 'reftex-pop-to-bibtex-entry "reftex-cite")
1615 (autoload 'reftex-end-of-bib-entry "reftex-cite")
1616 (autoload 'reftex-parse-bibtex-entry "reftex-cite")
1617 (autoload 'reftex-citation "reftex-cite"
1618 "Make a citation using BibTeX database files." t)
1619 (autoload 'reftex-default-bibliography "reftex-cite")
1620
1621
1622 ;;; =========================================================================
1623 ;;;
1624 ;;; Selection
1625
1626 (autoload 'reftex-select-label-mode "reftex-sel")
1627 (autoload 'reftex-select-bib-mode "reftex-sel")
1628 (autoload 'reftex-find-start-point "reftex-sel")
1629 (autoload 'reftex-insert-docstruct "reftex-sel")
1630 (autoload 'reftex-get-offset "reftex-sel")
1631 (autoload 'reftex-select-item "reftex-sel")
1632
1633
1634 ;;; =========================================================================
1635 ;;;
1636 ;;; Index support
1637
1638 (autoload 'reftex-index "reftex-index"
1639 "Query for an index macro and insert it along with its argments." t)
1640 (autoload 'reftex-index-selection-or-word "reftex-index"
1641 "Put selection or the word near point into the default index macro." t)
1642 (autoload 'reftex-index-phrase-selection-or-word "reftex-index"
1643 "Put selection or the word near point into Index Phrases File." t)
1644 (autoload 'reftex-display-index "reftex-index"
1645 "Display a buffer with an index compiled from the current document." t)
1646 (autoload 'reftex-index-visit-phrases-buffer "reftex-index"
1647 "Visit the Index Phrases File." t)
1648 (autoload 'reftex-index-phrases-mode "reftex-index"
1649 "Major mode for managing the Index phrases of a LaTeX document." t)
1650 (autoload 'reftex-index-complete-tag "reftex-index")
1651 (autoload 'reftex-index-complete-key "reftex-index")
1652 (autoload 'reftex-index-show-entry "reftex-index")
1653 (autoload 'reftex-index-select-tag "reftex-index")
1654
1655
1656 ;;; =========================================================================
1657 ;;;
1658 ;;; View cross references
1659
1660 (autoload 'reftex-view-crossref "reftex-dcr"
1661 "View cross reference of \\ref or \\cite macro at point." t)
1662 (autoload 'reftex-mouse-view-crossref "reftex-dcr"
1663 "View cross reference of \\ref or \\cite macro where you click." t)
1664 (autoload 'reftex-toggle-auto-view-crossref "reftex-dcr")
1665 (autoload 'reftex-view-crossref-from-bibtex "reftex-dcr"
1666 "View location in a LaTeX document which cites the BibTeX entry at point." t)
1667
1668
1669 ;;; =========================================================================
1670 ;;;
1671 ;;; Operations on entire Multifile documents
1672
1673 (autoload 'reftex-create-tags-file "reftex-global"
1674 "Create TAGS file by running `etags' on the current document." t)
1675 (autoload 'reftex-grep-document "reftex-global"
1676 "Run grep query through all files related to this document." t)
1677 (autoload 'reftex-search-document "reftex-global"
1678 "Regexp search through all files of the current TeX document." t)
1679 (autoload 'reftex-query-replace-document "reftex-global"
1680 "Run a query-replace-regexp of FROM with TO over the entire TeX document." t)
1681 (autoload 'reftex-find-duplicate-labels "reftex-global"
1682 "Produce a list of all duplicate labels in the document." t)
1683 (autoload 'reftex-change-label "reftex-global"
1684 "Query replace FROM with TO in all \\label and \\ref commands." t)
1685 (autoload 'reftex-renumber-simple-labels "reftex-global"
1686 "Renumber all simple labels in the document to make them sequentially." t)
1687 (autoload 'reftex-save-all-document-buffers "reftex-global"
1688 "Save all documents associated with the current document." t)
1689
1690
1691 ;;; =========================================================================
1692 ;;;
1693 ;;; AUCTeX Interface
1694
1695 (autoload 'reftex-arg-label "reftex-auc")
1696 (autoload 'reftex-arg-cite "reftex-auc")
1697 (autoload 'reftex-arg-index-tag "reftex-auc")
1698 (autoload 'reftex-arg-index "reftex-auc")
1699 (autoload 'reftex-plug-into-AUCTeX "reftex-auc")
1700 (autoload 'reftex-toggle-plug-into-AUCTeX "reftex-auc"
1701 "Toggle Interface between AUCTeX and RefTeX on and off." t)
1702 (autoload 'reftex-add-label-environments "reftex-auc")
1703 (autoload 'reftex-add-to-label-alist "reftex-auc")
1704 (autoload 'reftex-add-section-levels "reftex-auc")
1705 (autoload 'reftex-notice-new-section "reftex-auc")
1706
1707 ;;; =========================================================================
1708 ;;;
1709 ;;; Some generally useful functions
1710
1711 (defun reftex-typekey-check (typekey conf-variable &optional n)
1712 ;; Check if CONF-VARIABLE is true or contains TYPEKEY
1713 (and n (setq conf-variable (nth n conf-variable)))
1714 (or (eq conf-variable t)
1715 (and (stringp conf-variable)
1716 (string-match (concat "[" conf-variable "]") typekey))))
1717
1718 (defun reftex-check-recursive-edit ()
1719 ;; Check if we are already in a recursive edit. Abort with helpful
1720 ;; message if so.
1721 (if (marker-position reftex-recursive-edit-marker)
1722 (error
1723 (substitute-command-keys
1724 "In unfinished selection process. Finish, or abort with \\[abort-recursive-edit]"))))
1725
1726 (defun reftex-in-comment ()
1727 (save-excursion
1728 (skip-chars-backward "^%\n\r")
1729 (eq (preceding-char) ?%)))
1730
1731 (defun reftex-no-props (string)
1732 ;; Return STRING with all text properties removed
1733 (and (stringp string)
1734 (set-text-properties 0 (length string) nil string))
1735 string)
1736
1737 (defun reftex-match-string (n)
1738 ;; Match string without properties
1739 (when (match-beginning n)
1740 (buffer-substring-no-properties (match-beginning n) (match-end n))))
1741
1742 (defun reftex-kill-buffer (buffer)
1743 ;; Kill buffer if it exists.
1744 (and (setq buffer (get-buffer buffer))
1745 (kill-buffer buffer)))
1746
1747 (defun reftex-erase-buffer (&optional buffer)
1748 ;; Erase BUFFER if it exists. BUFFER defaults to current buffer.
1749 ;; This even erases read-only buffers.
1750 (cond
1751 ((null buffer)
1752 ;; erase current buffer
1753 (let ((buffer-read-only nil)) (erase-buffer)))
1754 ((setq buffer (get-buffer buffer))
1755 ;; buffer exists
1756 (save-excursion
1757 (set-buffer buffer)
1758 (let ((buffer-read-only nil)) (erase-buffer))))))
1759
1760 (defun reftex-this-word (&optional class)
1761 ;; Grab the word around point.
1762 (setq class (or class "-a-zA-Z0-9:_/.*;|"))
1763 (save-excursion
1764 (buffer-substring-no-properties
1765 (progn (skip-chars-backward class) (point))
1766 (progn (skip-chars-forward class) (point)))))
1767
1768 (defun reftex-number (n unit &optional ending)
1769 (if (and (integerp n) (stringp unit))
1770 (format "%d %s%s" n unit (if (= n 1) "" (or ending "s")))
1771 ""))
1772
1773 (defun reftex-all-assq (key list)
1774 ;; Return a list of all associations of KEY in LIST. Comparison with eq.
1775 (let (rtn)
1776 (while (setq list (memq (assq key list) list))
1777 (push (car list) rtn)
1778 (pop list))
1779 (nreverse rtn)))
1780
1781 (defun reftex-all-assoc-string (key list)
1782 ;; Return a list of all associations of KEY in LIST. Comparison with string=.
1783 (let (rtn)
1784 (while list
1785 (if (string= (car (car list)) key)
1786 (push (car list) rtn))
1787 (pop list))
1788 (nreverse rtn)))
1789
1790 (defun reftex-last-assoc-before-elt (key elt list &optional exclusive)
1791 ;; Find the last association of KEY in LIST before or at ELT
1792 ;; ELT is found in LIST with equal, not eq.
1793 ;; Returns nil when either KEY or elt are not found in LIST.
1794 ;; When EXCLUSIVE is non-nil, ELT cannot be the return value.
1795 ;; On success, returns the association.
1796 (let* ((elt (car (member elt list))) (ex (not exclusive)) ass last-ass)
1797 (while (and (setq ass (assoc key list))
1798 (setq list (memq ass list))
1799 (or ex (not (eq elt (car list))))
1800 (memq elt list))
1801 (setq last-ass ass
1802 list (cdr list)))
1803 last-ass))
1804
1805 (defun reftex-sublist-nth (list nth predicate &optional completion)
1806 ;; Make a list of the NTH elements of all members of LIST which
1807 ;; fulfill PREDICATE.
1808 ;; When COMPLETION is non-nil, make all elements of the resulting
1809 ;; list also a list, so that the result can be used for completion.
1810 (let (rtn)
1811 (while list
1812 (if (funcall predicate (car list))
1813 (push (if completion
1814 (list (nth nth (car list)))
1815 (nth nth (car list)))
1816 rtn))
1817 (setq list (cdr list)))
1818 (nreverse rtn)))
1819
1820 (defun reftex-make-selection-buffer-name (type &optional index)
1821 ;; Make unique name for a selection buffer.
1822 (format " *RefTeX[%s][%d]*"
1823 type (or index (get reftex-docstruct-symbol :master-index) 0)))
1824
1825 (defun reftex-make-index-buffer-name (tag &optional cnt)
1826 ;; Make unique name for an index buffer.
1827 (format "*Index[%s][%d]*"
1828 tag (or cnt (get reftex-docstruct-symbol :master-index) 0)))
1829
1830 (defun reftex-truncate (string ncols &optional ellipses padding)
1831 ;; Truncate STRING to NCOLS characters.
1832 ;; When PADDING is non-nil, and string is shorter than NCOLS, fill with
1833 ;; white space to NCOLS characters. When ELLIPSES is non-nil and the
1834 ;; string needs to be truncated, replace last 3 characters by dots.
1835 (setq string
1836 (if (<= (length string) ncols)
1837 string
1838 (if ellipses
1839 (concat (substring string 0 (- ncols 3)) "...")
1840 (substring string 0 ncols))))
1841 (if padding
1842 (format (format "%%-%ds" ncols) string)
1843 string))
1844
1845 (defun reftex-nearest-match (regexp &optional max-length)
1846 ;; Find the nearest match of REGEXP. Set the match data.
1847 ;; If POS is given, calculate distances relative to it.
1848 ;; Return nil if there is no match.
1849 (let ((pos (point))
1850 (dist (or max-length (length regexp)))
1851 match1 match2 match)
1852 (goto-char (min (+ pos dist) (point-max)))
1853 (when (re-search-backward regexp nil t)
1854 (setq match1 (match-data)))
1855 (goto-char (max (- pos dist) (point-min)))
1856 (when (re-search-forward regexp nil t)
1857 (setq match2 (match-data)))
1858 (goto-char pos)
1859 (setq match
1860 (cond
1861 ((not match1) match2)
1862 ((not match2) match1)
1863 ((< (abs (- pos (car match1))) (abs (- pos (car match2)))) match1)
1864 (t match2)))
1865 (if match (progn (set-match-data match) t) nil)))
1866
1867 (defun reftex-auto-mode-alist ()
1868 ;; Return an `auto-mode-alist' with only the .gz (etc) thingies.
1869 ;; Stolen from gnus nnheader.
1870 (let ((alist auto-mode-alist)
1871 out)
1872 (while alist
1873 (when (listp (cdr (car alist)))
1874 (push (car alist) out))
1875 (pop alist))
1876 (nreverse out)))
1877
1878 (defun reftex-window-height ()
1879 (if (fboundp 'window-displayed-height)
1880 (window-displayed-height)
1881 (window-height)))
1882
1883 (defun reftex-enlarge-to-fit (buf2 &optional keep-current)
1884 ;; Enlarge other window displaying buffer to show whole buffer if possible.
1885 ;; If KEEP-CURRENT in non-nil, current buffer must remain visible.
1886 (let* ((win1 (selected-window))
1887 (buf1 (current-buffer))
1888 (win2 (get-buffer-window buf2))) ;; Only on current frame.
1889 (when win2
1890 (select-window win2)
1891 (unless (and (pos-visible-in-window-p 1)
1892 (pos-visible-in-window-p (point-max)))
1893 (enlarge-window (1+ (- (count-lines 1 (point-max))
1894 (reftex-window-height))))))
1895 (cond
1896 ((window-live-p win1) (select-window win1))
1897 (keep-current
1898 ;; we must have the old buffer!
1899 (switch-to-buffer-other-window buf1)
1900 (shrink-window (- (window-height) window-min-height))))))
1901
1902 (defun reftex-select-with-char (prompt help-string &optional delay-time scroll)
1903 ;; Offer to select something with PROMPT and, after DELAY-TIME seconds,
1904 ;; also with HELP-STRING.
1905 ;; When SCROLL is non-nil, use SPC and DEL to scroll help window.
1906 (let ((char ?\?))
1907 (save-window-excursion
1908 (catch 'exit
1909 (message (concat prompt " (?=Help)"))
1910 (when (or (sit-for (or delay-time 0))
1911 (= ?\? (setq char (read-char-exclusive))))
1912 (reftex-kill-buffer "*RefTeX Select*")
1913 (switch-to-buffer-other-window "*RefTeX Select*")
1914 (insert help-string)
1915 (goto-char 1)
1916 (unless (and (pos-visible-in-window-p 1)
1917 (pos-visible-in-window-p (point-max)))
1918 (enlarge-window (1+ (- (count-lines 1 (point-max))
1919 (reftex-window-height)))))
1920 (setq truncate-lines t))
1921 (if (and (pos-visible-in-window-p 1)
1922 (pos-visible-in-window-p (point-max)))
1923 nil
1924 (setq prompt (concat prompt (if scroll " (SPC/DEL=Scroll)" ""))))
1925 (message prompt)
1926 (and (equal char ?\?) (setq char (read-char-exclusive)))
1927 (while t
1928 (cond ((equal char ?\C-g) (keyboard-quit))
1929 ((equal char ?\?))
1930 ((and scroll (equal char ?\ ))
1931 (condition-case nil (scroll-up) (error nil))
1932 (message prompt))
1933 ((and scroll (equal char ?\C-? ))
1934 (condition-case nil (scroll-down) (error nil))
1935 (message prompt))
1936 (t (message "")
1937 (throw 'exit char)))
1938 (setq char (read-char-exclusive)))))))
1939
1940
1941 (defun reftex-make-regexp-allow-for-ctrl-m (string)
1942 ;; convert STRING into a regexp, allowing ^M for \n and vice versa
1943 (let ((start -2))
1944 (setq string (regexp-quote string))
1945 (while (setq start (string-match "[\n\r]" string (+ 3 start)))
1946 (setq string (replace-match "[\n\r]" nil t string)))
1947 string))
1948
1949 (defun reftex-get-buffer-visiting (file)
1950 ;; return a buffer visiting FILE
1951 (cond
1952 ((boundp 'find-file-compare-truenames) ; XEmacs
1953 (let ((find-file-compare-truenames t))
1954 (get-file-buffer file)))
1955 ((fboundp 'find-buffer-visiting) ; Emacs
1956 (find-buffer-visiting file))
1957 (t (error "This should not happen (reftex-get-buffer-visiting)"))))
1958
1959 ;; Define `current-message' for compatibility with XEmacs prior to 20.4
1960 (defvar message-stack)
1961 (if (and (featurep 'xemacs)
1962 (not (fboundp 'current-message)))
1963 (defun current-message (&optional frame)
1964 (cdr (car message-stack))))
1965
1966 (defun reftex-visited-files (list)
1967 ;; Takes a list of filenames and returns the buffers of those already visited
1968 (delq nil (mapcar (lambda (x) (if (reftex-get-buffer-visiting x) x nil))
1969 list)))
1970
1971 (defun reftex-get-file-buffer-force (file &optional mark-to-kill)
1972 ;; Return a buffer visiting file. Make one, if necessary.
1973 ;; If neither such a buffer nor the file exist, return nil.
1974 ;; If MARK-TO-KILL is t and there is no live buffer, visit the file with
1975 ;; initializations according to `reftex-initialize-temporary-buffers',
1976 ;; and mark the buffer to be killed after use.
1977
1978 (let ((buf (reftex-get-buffer-visiting file)))
1979
1980 (cond (buf
1981 ;; We have it already as a buffer - just return it
1982 buf)
1983
1984 ((file-readable-p file)
1985 ;; At least there is such a file and we can read it.
1986
1987 (if (or (not mark-to-kill)
1988 (eq t reftex-initialize-temporary-buffers))
1989
1990 ;; Visit the file with full magic
1991 (setq buf (find-file-noselect file))
1992
1993 ;; Else: Visit the file just briefly, without or
1994 ;; with limited Magic
1995
1996 ;; The magic goes away
1997 (let ((format-alist nil)
1998 (auto-mode-alist (reftex-auto-mode-alist))
1999 (default-major-mode 'fundamental-mode)
2000 (enable-local-variables nil)
2001 (after-insert-file-functions nil))
2002 (setq buf (find-file-noselect file)))
2003
2004 ;; Is there a hook to run?
2005 (when (listp reftex-initialize-temporary-buffers)
2006 (save-excursion
2007 (set-buffer buf)
2008 (run-hooks 'reftex-initialize-temporary-buffers))))
2009
2010 ;; Lets see if we got a license to kill :-|
2011 (and mark-to-kill
2012 (add-to-list 'reftex-buffers-to-kill buf))
2013
2014 ;; Return the new buffer
2015 buf)
2016
2017 ;; If no such file exists, return nil
2018 (t nil))))
2019
2020 (defun reftex-kill-temporary-buffers (&optional buffer)
2021 ;; Kill all buffers in the list reftex-kill-temporary-buffers.
2022 (cond
2023 (buffer
2024 (when (member buffer reftex-buffers-to-kill)
2025 (kill-buffer buffer)
2026 (setq reftex-buffers-to-kill
2027 (delete buffer reftex-buffers-to-kill))))
2028 (t
2029 (while (setq buffer (pop reftex-buffers-to-kill))
2030 (when (bufferp buffer)
2031 (and (buffer-modified-p buffer)
2032 (y-or-n-p (format "Save file %s? "
2033 (buffer-file-name buffer)))
2034 (save-excursion
2035 (set-buffer buffer)
2036 (save-buffer)))
2037 (kill-buffer buffer))
2038 (pop reftex-buffers-to-kill)))))
2039
2040 (defun reftex-splice-symbols-into-list (list alist)
2041 ;; Splice the association in ALIST of any symbols in LIST into the list.
2042 ;; Return new list.
2043 (let (rtn tmp)
2044 (while list
2045 (while (and (not (null (car list))) ;; keep list elements nil
2046 (symbolp (car list)))
2047 (setq tmp (car list))
2048 (cond
2049 ((assoc tmp alist)
2050 (setq list (append (nth 2 (assoc tmp alist)) (cdr list))))
2051 (t
2052 (error "Cannot treat symbol %s in reftex-label-alist"
2053 (symbol-name tmp)))))
2054 (push (pop list) rtn))
2055 (nreverse rtn)))
2056
2057 (defun reftex-remove-symbols-from-list (list)
2058 ;; Remove all symbols from list
2059 (let (rtn)
2060 (while list
2061 (unless (symbolp (car list))
2062 (push (car list) rtn))
2063 (setq list (cdr list)))
2064 (nreverse rtn)))
2065
2066 (defun reftex-uniquify (list)
2067 ;; Return a list of all elements in LIST, but each only once
2068 (let (new elm)
2069 (while list
2070 (setq elm (pop list))
2071 (unless (member elm new)
2072 (push elm new)))
2073 (nreverse new)))
2074
2075 (defun reftex-uniquify-by-car (alist &optional keep-list)
2076 ;; Return a list of all elements in ALIST, but each car only once.
2077 ;; Elements of KEEP-LIST are not removed even if duplicate.
2078 (let (new elm)
2079 (while alist
2080 (setq elm (pop alist))
2081 (if (or (member (car elm) keep-list)
2082 (not (assoc (car elm) new)))
2083 (push elm new)))
2084 (nreverse new)))
2085
2086 (defun reftex-abbreviate-title (string)
2087 (reftex-convert-string string "[-~ \t\n\r,;]" nil t t
2088 5 40 nil 1 " " (nth 5 reftex-derive-label-parameters)))
2089
2090 (defun reftex-convert-string (string split-re illegal-re dot keep-fp
2091 nwords maxchar illegal abbrev sep
2092 ignore-words &optional downcase)
2093 "Convert a string (a sentence) to something shorter.
2094 SPLIT-RE is the regular expression used to split the string into words.
2095 ILLEGAL-RE matches characters which are illegal in the final string.
2096 DOT t means add dots to abbreviated words.
2097 KEEP-FP t means to keep a final punctuation when applicable.
2098 NWORDS Number of words to use.
2099 MAXCHAR Maximum number of characters in the final string.
2100 ILLEGAL nil: Throw away any words containing stuff matched with ILLEGAL-RE.
2101 t: Throw away only the matched part, not the whole word.
2102 ABBREV nil: Never abbreviate words.
2103 t: Always abbreviate words (see `reftex-abbrev-parameters').
2104 not t and not nil: Abbreviate words if necessary to shorten
2105 string below MAXCHAR.
2106 SEP String separating different words in the output string.
2107 IGNORE-WORDS List of words which should be removed from the string."
2108
2109 (let* ((words0 (split-string string (or split-re "[ \t\n\r]")))
2110 (reftex-label-illegal-re (or illegal-re "\000"))
2111 (abbrev-re (concat
2112 "\\`\\("
2113 (make-string (nth 0 reftex-abbrev-parameters) ?.)
2114 "[" (nth 2 reftex-abbrev-parameters) "]*"
2115 "\\)"
2116 "[" (nth 3 reftex-abbrev-parameters) "]"
2117 (make-string (1- (nth 1 reftex-abbrev-parameters)) ?.)))
2118 words word)
2119
2120 ;; Remove words from the ignore list or with funny characters
2121 (while (setq word (pop words0))
2122 (if downcase (setq word (downcase word)))
2123 (cond
2124 ((member (downcase word) ignore-words))
2125 ((string-match reftex-label-illegal-re word)
2126 (when illegal
2127 (while (string-match reftex-label-illegal-re word)
2128 (setq word (replace-match "" nil nil word)))
2129 (push word words)))
2130 (t
2131 (push word words))))
2132 (setq words (nreverse words))
2133
2134 ;; Restrict number of words
2135 (if (> (length words) nwords)
2136 (setcdr (nthcdr (1- nwords) words) nil))
2137
2138 ;; First, try to use all words
2139 (setq string (mapconcat 'identity words sep))
2140
2141 ;; Abbreviate words if enforced by user settings or string length
2142 (if (or (eq t abbrev)
2143 (and abbrev
2144 (> (length string) maxchar)))
2145 (setq words
2146 (mapcar
2147 (lambda (w) (if (string-match abbrev-re w)
2148 (if dot
2149 (concat (match-string 1 w) ".")
2150 (match-string 1 w))
2151 w))
2152 words)
2153 string (mapconcat 'identity words sep)))
2154
2155 ;; Shorten if still to long
2156 (setq string
2157 (if (> (length string) maxchar)
2158 (substring string 0 maxchar)
2159 string))
2160
2161 ;; Delete the final punctuation, if any
2162 (if (and (not keep-fp) (string-match "\\s.+\\'" string))
2163 (setq string (replace-match "" nil nil string)))
2164 string))
2165
2166 (defun reftex-nicify-text (text)
2167 ;; Make TEXT nice for inclusion as context into label menu.
2168 ;; 1. remove line breaks and extra white space
2169 (while (string-match "[\n\r\t]\\|[ \t][ \t]+" text)
2170 (setq text (replace-match " " nil t text)))
2171 ;; 2. cut before the next `\end{' or `\item' or `\\'
2172 (if (string-match "\\(\\\\end{\\|\\\\item\\|\\\\\\\\\\).*" text)
2173 (setq text (replace-match "" nil t text)))
2174 ;; 3. kill the embedded label
2175 (if (string-match "\\\\label{[^}]*}" text)
2176 (setq text (replace-match "" nil t text)))
2177 ;; 4. remove leading garbage
2178 (if (string-match "\\`[ }]+" text)
2179 (setq text (replace-match "" nil t text)))
2180 ;; 5. limit length
2181 (cond
2182 ((> (length text) 100) (substring text 0 100))
2183 ((= (length text) 0) (make-string 1 ?\ ))
2184 (t text)))
2185
2186 ;;; =========================================================================
2187 ;;;
2188 ;;; Fontification and Highlighting
2189
2190 (defun reftex-use-fonts ()
2191 ;; Return t if we can and want to use fonts.
2192 (and window-system
2193 reftex-use-fonts
2194 (featurep 'font-lock)))
2195
2196 (defun reftex-refontify ()
2197 ;; Return t if we need to refontify context
2198 (and (reftex-use-fonts)
2199 (or (eq t reftex-refontify-context)
2200 (and (eq 1 reftex-refontify-context)
2201 ;; Test of we use the font-lock version of x-symbol
2202 (and (featurep 'x-symbol-tex) (not (boundp 'x-symbol-mode)))))))
2203
2204 (defvar font-lock-defaults-computed)
2205 (defun reftex-fontify-select-label-buffer (parent-buffer)
2206 ;; Fontify the `*RefTeX Select*' buffer. Buffer is temporarily renamed to
2207 ;; start with none-SPC char, beacuse Font-Lock otherwise refuses operation.
2208 (run-hook-with-args 'reftex-pre-refontification-functions
2209 parent-buffer 'reftex-ref)
2210 (let* ((oldname (buffer-name))
2211 (newname (concat "Fontify-me-" oldname)))
2212 (unwind-protect
2213 (progn
2214 ;; Rename buffer temporarily to start w/o space (because of font-lock)
2215 (rename-buffer newname t)
2216 (cond
2217 ((fboundp 'font-lock-default-fontify-region)
2218 ;; Good: we have the indirection functions
2219 (set (make-local-variable 'font-lock-fontify-region-function)
2220 'reftex-select-font-lock-fontify-region)
2221 (let ((major-mode 'latex-mode))
2222 (font-lock-mode 1)))
2223 ((fboundp 'font-lock-set-defaults-1)
2224 ;; Looks like the XEmacs font-lock stuff.
2225 ;; FIXME: this is still kind of a hack, but it works.
2226 (set (make-local-variable 'font-lock-keywords) nil)
2227 (let ((major-mode 'latex-mode)
2228 (font-lock-defaults-computed nil))
2229 (font-lock-set-defaults-1)
2230 (reftex-select-font-lock-fontify-region (point-min) (point-max))))
2231 (t
2232 ;; Oops?
2233 (message "Sorry: cannot refontify RefTeX Select buffer."))))
2234 (rename-buffer oldname))))
2235
2236 (defun reftex-select-font-lock-fontify-region (beg end &optional loudly)
2237 ;; Fontify a region, but only lines starting with a dot.
2238 (let ((func (if (fboundp 'font-lock-default-fontify-region)
2239 'font-lock-default-fontify-region
2240 'font-lock-fontify-region))
2241 beg1 end1)
2242 (goto-char beg)
2243 (while (re-search-forward "^\\." end t)
2244 (setq beg1 (point) end1 (progn (skip-chars-forward "^\n") (point)))
2245 (funcall func beg1 end1 nil)
2246 (goto-char end1))))
2247
2248 (defun reftex-select-font-lock-unfontify (&rest ignore) t)
2249
2250 (defun reftex-verified-face (&rest faces)
2251 ;; Return the first valid face in FACES, or nil if none is valid.
2252 ;; Also, when finding a nil element in FACES, return nil. This
2253 ;; function is just a safety net to catch name changes of builtin
2254 ;; fonts. Currently it is only used for reftex-label-face, which has
2255 ;; as default font-lock-reference-face, which was recently renamed
2256 ;; to font-lock-constant-face.
2257 (let (face)
2258 (catch 'exit
2259 (while (setq face (pop faces))
2260 (if (featurep 'xemacs)
2261 (if (find-face face) (throw 'exit face))
2262 (if (facep face) (throw 'exit face)))))))
2263
2264 ;; Highlighting uses overlays. For XEmacs, we need the emulation.
2265 (if (featurep 'xemacs) (require 'overlay))
2266
2267 ;; We keep a vector with several different overlays to do our highlighting.
2268 (defvar reftex-highlight-overlays [nil nil])
2269
2270 ;; Initialize the overlays
2271 (aset reftex-highlight-overlays 0 (make-overlay 1 1))
2272 (overlay-put (aref reftex-highlight-overlays 0)
2273 'face 'highlight)
2274 (aset reftex-highlight-overlays 1 (make-overlay 1 1))
2275 (overlay-put (aref reftex-highlight-overlays 1)
2276 'face reftex-cursor-selected-face)
2277
2278 ;; Two functions for activating and deactivation highlight overlays
2279 (defun reftex-highlight (index begin end &optional buffer)
2280 "Highlight a region with overlay INDEX."
2281 (move-overlay (aref reftex-highlight-overlays index)
2282 begin end (or buffer (current-buffer))))
2283 (defun reftex-unhighlight (index)
2284 "Detach overlay INDEX."
2285 (delete-overlay (aref reftex-highlight-overlays index)))
2286
2287 (defun reftex-highlight-shall-die ()
2288 ;; Function used in pre-command-hook to remove highlights.
2289 (remove-hook 'pre-command-hook 'reftex-highlight-shall-die)
2290 (reftex-unhighlight 0))
2291
2292 ;;; =========================================================================
2293 ;;;
2294 ;;; Keybindings
2295
2296 ;; The default bindings in the mode map.
2297 (loop for x in
2298 '(("\C-c=" . reftex-toc)
2299 ("\C-c(" . reftex-label)
2300 ("\C-c)" . reftex-reference)
2301 ("\C-c[" . reftex-citation)
2302 ("\C-c<" . reftex-index)
2303 ("\C-c>" . reftex-display-index)
2304 ("\C-c/" . reftex-index-selection-or-word)
2305 ("\C-c\\" . reftex-index-phrase-selection-or-word)
2306 ("\C-c|" . reftex-index-visit-phrases-buffer)
2307 ("\C-c&" . reftex-view-crossref))
2308 do (define-key reftex-mode-map (car x) (cdr x)))
2309
2310 ;; Bind `reftex-mouse-view-crossref' only when the key is still free
2311 (if (featurep 'xemacs)
2312 (unless (key-binding [(shift button2)])
2313 (define-key reftex-mode-map [(shift button2)]
2314 'reftex-mouse-view-crossref))
2315 (unless (key-binding [(shift mouse-2)])
2316 (define-key reftex-mode-map [(shift mouse-2)]
2317 'reftex-mouse-view-crossref)))
2318
2319 ;; Bind `reftex-view-crossref-from-bibtex' in BibTeX mode map
2320 (eval-after-load
2321 "bibtex"
2322 '(define-key bibtex-mode-map "\C-c&" 'reftex-view-crossref-from-bibtex))
2323
2324 ;; If the user requests so, she can have a few more bindings:
2325 (when reftex-extra-bindings
2326 (loop for x in
2327 '(("\C-ct" . reftex-toc)
2328 ("\C-cl" . reftex-label)
2329 ("\C-cr" . reftex-reference)
2330 ("\C-cc" . reftex-citation)
2331 ("\C-cv" . reftex-view-crossref)
2332 ("\C-cg" . reftex-grep-document)
2333 ("\C-cs" . reftex-search-document))
2334 do (define-key reftex-mode-map (car x) (cdr x))))
2335
2336 ;;; =========================================================================
2337 ;;;
2338 ;;; Menu
2339
2340 ;; Define a menu for the menu bar if Emacs is running under X
2341
2342 (require 'easymenu)
2343
2344 (easy-menu-define reftex-mode-menu reftex-mode-map
2345 "Menu used in RefTeX mode"
2346 `("Ref"
2347 ["Table of Contents" reftex-toc t]
2348 "--"
2349 ["\\label" reftex-label t]
2350 ["\\ref" reftex-reference t]
2351 ["\\cite" reftex-citation t]
2352 ("\\index"
2353 ["\\index" reftex-index t]
2354 ["\\index{THIS}" reftex-index-selection-or-word t]
2355 "--"
2356 ["Add THIS to Index Phrases" reftex-index-phrase-selection-or-word t]
2357 ["Visit Phrase Buffer" reftex-index-visit-phrases-buffer t]
2358 ["Apply Phrases to Region" reftex-index-phrases-apply-to-region t]
2359 "--"
2360 ["Display the Index" reftex-display-index t])
2361 "--"
2362 ["View Crossref" reftex-view-crossref t]
2363 "--"
2364 ("Parse Document"
2365 ["One File" reftex-parse-one reftex-enable-partial-scans]
2366 ["Entire Document" reftex-parse-all t]
2367 ["Save to File" (reftex-access-parse-file 'write)
2368 (> (length (symbol-value reftex-docstruct-symbol)) 0)]
2369 ["Restore from File" (reftex-access-parse-file 'restore) t]
2370 "--"
2371 ["Reset RefTeX Mode" reftex-reset-mode t])
2372 ("Global Actions"
2373 ["Search Whole Document" reftex-search-document t]
2374 ["Replace in Document" reftex-query-replace-document t]
2375 ["Grep on Document" reftex-grep-document t]
2376 "--"
2377 ["Find Duplicate Labels" reftex-find-duplicate-labels t]
2378 ["Change Label and Refs" reftex-change-label t]
2379 ["Renumber Simple Labels" reftex-renumber-simple-labels t]
2380 "--"
2381 ["Create TAGS File" reftex-create-tags-file t]
2382 "--"
2383 ["Save Document" reftex-save-all-document-buffers t])
2384 "--"
2385 ("Options"
2386 "PARSER"
2387 ["Partial Scans"
2388 (setq reftex-enable-partial-scans (not reftex-enable-partial-scans))
2389 :style toggle :selected reftex-enable-partial-scans]
2390 ["Auto-Save Parse Info"
2391 (setq reftex-save-parse-info (not reftex-save-parse-info))
2392 :style toggle :selected reftex-save-parse-info]
2393 "--"
2394 "CROSSREF INFO"
2395 ["Automatic Info" reftex-toggle-auto-view-crossref
2396 :style toggle :selected reftex-auto-view-crossref-timer]
2397 ["...in Echo Area" (setq reftex-auto-view-crossref t)
2398 :style radio :selected (eq reftex-auto-view-crossref t)]
2399 ["...in Other Window" (setq reftex-auto-view-crossref 'window)
2400 :style radio :selected (eq reftex-auto-view-crossref 'window)]
2401 "--"
2402 "MISC"
2403 ["AUC TeX Interface" reftex-toggle-plug-into-AUCTeX
2404 :style toggle :selected reftex-plug-into-AUCTeX])
2405 ("Reference Style"
2406 ["Default" (setq reftex-vref-is-default nil
2407 reftex-fref-is-default nil)
2408 :style radio :selected (not (or reftex-vref-is-default
2409 reftex-fref-is-default))]
2410 ["Varioref" (setq reftex-vref-is-default t
2411 reftex-fref-is-default nil)
2412 :style radio :selected reftex-vref-is-default]
2413 ["Fancyref" (setq reftex-fref-is-default t
2414 reftex-vref-is-default nil)
2415 :style radio :selected reftex-fref-is-default])
2416 ("Citation Style"
2417 ,@(mapcar
2418 (lambda (x)
2419 (vector
2420 (capitalize (symbol-name (car x)))
2421 (list 'reftex-set-cite-format (list 'quote (car x)))
2422 :style 'radio :selected
2423 (list 'eq (list 'reftex-get-cite-format) (list 'quote (car x)))))
2424 reftex-cite-format-builtin)
2425 "--"
2426 "Sort Database Matches"
2427 ["Not" (setq reftex-sort-bibtex-matches nil)
2428 :style radio :selected (eq reftex-sort-bibtex-matches nil)]
2429 ["by Author" (setq reftex-sort-bibtex-matches 'author)
2430 :style radio :selected (eq reftex-sort-bibtex-matches 'author)]
2431 ["by Year" (setq reftex-sort-bibtex-matches 'year)
2432 :style radio :selected (eq reftex-sort-bibtex-matches 'year)]
2433 ["by Year, reversed" (setq reftex-sort-bibtex-matches 'reverse-year)
2434 :style radio :selected (eq reftex-sort-bibtex-matches 'reverse-year)])
2435 ("Index Style"
2436 ,@(mapcar
2437 (lambda (x)
2438 (vector
2439 (capitalize (symbol-name (car x)))
2440 (list 'reftex-add-index-macros (list 'list (list 'quote (car x))))
2441 :style 'radio :selected
2442 (list 'memq (list 'quote (car x))
2443 (list 'get 'reftex-docstruct-symbol
2444 (list 'quote 'reftex-index-macros-style)))))
2445 reftex-index-macros-builtin))
2446 "--"
2447 ("Customize"
2448 ["Browse RefTeX Group" reftex-customize t]
2449 "--"
2450 ["Build Full Customize Menu" reftex-create-customize-menu
2451 (fboundp 'customize-menu-create)])
2452 ("Documentation"
2453 ["Info" reftex-info t]
2454 ["Commentary" reftex-show-commentary t])))
2455
2456 (defun reftex-customize ()
2457 "Call the customize function with reftex as argument."
2458 (interactive)
2459 (customize-browse 'reftex))
2460
2461 (defun reftex-create-customize-menu ()
2462 "Create a full customization menu for RefTeX, insert it into the menu."
2463 (interactive)
2464 (if (fboundp 'customize-menu-create)
2465 (progn
2466 (easy-menu-change
2467 '("Ref") "Customize"
2468 `(["Browse RefTeX group" reftex-customize t]
2469 "--"
2470 ,(customize-menu-create 'reftex)
2471 ["Set" Custom-set t]
2472 ["Save" Custom-save t]
2473 ["Reset to Current" Custom-reset-current t]
2474 ["Reset to Saved" Custom-reset-saved t]
2475 ["Reset to Standard Settings" Custom-reset-standard t]))
2476 (message "\"Ref\"-menu now contains full customization menu"))
2477 (error "Cannot expand menu (outdated version of cus-edit.el)")))
2478
2479 (defun reftex-show-commentary ()
2480 "Use the finder to view the file documentation from `reftex.el'."
2481 (interactive)
2482 (require 'finder)
2483 (finder-commentary "reftex.el"))
2484
2485 (defun reftex-info (&optional node)
2486 "Read documentation for RefTeX in the info system.
2487 With optional NODE, go directly to that node."
2488 (interactive)
2489 (require 'info)
2490 (Info-goto-node (format "(reftex)%s" (or node ""))))
2491
2492 ;;; Install the kill-buffer and kill-emacs hooks ------------------------------
2493
2494 (add-hook 'kill-buffer-hook 'reftex-kill-buffer-hook)
2495 (add-hook 'kill-emacs-hook 'reftex-kill-emacs-hook)
2496
2497 ;;; Run Hook ------------------------------------------------------------------
2498
2499 (run-hooks 'reftex-load-hook)
2500
2501 ;;; That's it! ----------------------------------------------------------------
2502
2503 (setq reftex-tables-dirty t) ; in case this file is evaluated by hand
2504 (provide 'reftex)
2505
2506 ;;;============================================================================
2507
2508 ;;; reftex.el ends here
2509