(WIDE_CHAR_SUPPORT): New macro.
[emacs.git] / lisp / textmodes / reftex.el
blob9468235a9dfd5b3cead8b5a9b42ac44e1669d024
1 ;;; reftex.el --- Minor mode for doing \label, \ref, \cite, \index in LaTeX
2 ;; Copyright (c) 1997, 1998, 1999, 2000 Free Software Foundation, Inc.
4 ;; Author: Carsten Dominik <dominik@strw.LeidenUniv.nl>
5 ;; Version: 4.14
6 ;; Keywords: tex
8 ;; This file is part of GNU Emacs.
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.
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.
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.
25 ;;---------------------------------------------------------------------------
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.
39 ;; INSTALLATION
40 ;; ------------
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.
48 ;; To turn RefTeX Mode on and off in a buffer, use `M-x reftex-mode'.
50 ;; To turn on RefTeX Mode for all LaTeX files, add the following lines
51 ;; to your .emacs file:
53 ;; (add-hook 'LaTeX-mode-hook 'turn-on-reftex) ; AUCTeX LaTeX mode
54 ;; (add-hook 'latex-mode-hook 'turn-on-reftex) ; Emacs latex mode
57 ;; DOCUMENTATION
58 ;; -------------
60 ;; See below for a short summary of how to use RefTeX.
62 ;; There is an extensive texinfo document describing RefTeX in detail.
63 ;; One way to view this documentation is `M-x reftex-info RET'.
65 ;; The documentation in various formats is also available at
67 ;; http://www.strw.leidenuniv.nl/~dominik/Tools/
69 ;;---------------------------------------------------------------------------
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 ;; ====================
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:
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
108 ;; RefTeX in a Nutshell
109 ;; ====================
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.
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').
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)
134 ;; Which labels are created how is configurable with the variable
135 ;; `reftex-insert-label-flags'.
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.
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).
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.
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.
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.
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.
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.
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').
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.
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.
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)
218 ;; To integrate with AUCTeX, use
219 ;; (setq reftex-plug-into-AUCTeX t)
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.
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.
240 ;;---------------------------------------------------------------------------
242 ;; AUTHOR
243 ;; ======
245 ;; Carsten Dominik <dominik@strw.LeidenUniv.nl>
247 ;; with contributions from Stephen Eglen
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
253 ;; http://www.strw.leidenuniv.nl/~dominik/Tools/
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
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.
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).
271 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
273 ;;;;;;
275 ;;; Code:
277 (eval-when-compile (require 'cl))
279 ;; Stuff that needs to be there when we use defcustom
280 (require 'custom)
282 (defvar reftex-tables-dirty t
283 "Flag showing if tables need to be re-computed.")
285 (eval-and-compile
286 (defun reftex-set-dirty (symbol value)
287 (setq reftex-tables-dirty t)
288 (set symbol value)))
291 ;;; =========================================================================
293 ;;; Configuration variables
295 (require 'reftex-vars)
298 ;;; =========================================================================
300 ;;; Define the formal stuff for a minor mode named RefTeX.
303 (defconst reftex-version "RefTeX version 4.14"
304 "Version string for RefTeX.")
306 (defvar reftex-mode nil
307 "Determines if RefTeX mode is active.")
308 (make-variable-buffer-local 'reftex-mode)
310 (defvar reftex-mode-map (make-sparse-keymap)
311 "Keymap for RefTeX mode.")
313 (defvar reftex-mode-menu nil)
314 (defvar reftex-syntax-table nil)
316 (defvar reftex-auto-view-crossref-timer nil
317 "The timer used for auto-view-crossref.")
319 ;;;###autoload
320 (defun turn-on-reftex ()
321 "Turn on RefTeX mode."
322 (reftex-mode t))
324 ;;;###autoload
325 (defun reftex-mode (&optional arg)
326 "Minor mode with distinct support for \\label, \\ref and \\cite in LaTeX.
328 \\<reftex-mode-map>A Table of Contents of the entire (multifile) document with browsing
329 capabilities is available with `\\[reftex-toc]'.
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.
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.
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.
344 Most command have help available on the fly. This help is accessed by
345 pressing `?' to any prompt mentioning this feature.
347 Extensive documentation about RefTeX is available in Info format.
348 You can view this information with `\\[reftex-info]'.
350 \\{reftex-mode-map}
351 Under X, these and other functions will also be available as `Ref' menu
352 on the menu bar.
354 ------------------------------------------------------------------------------"
356 (interactive "P")
357 (setq reftex-mode (not (or (and (null arg) reftex-mode)
358 (<= (prefix-numeric-value arg) 0))))
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)))
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)))
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))))
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))))
415 ;;; =========================================================================
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)
427 ;;; =========================================================================
429 ;;; Multibuffer Variables
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.
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))
442 ;; Alist connecting master file names with the corresponding lisp symbols.
443 (defvar reftex-master-index-list nil)
445 ;; Last index used for a master file.
446 (defvar reftex-multifile-index 0)
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)
452 (defun reftex-next-multifile-index ()
453 ;; Return the next free index for multifile symbols.
454 (incf reftex-multifile-index))
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.
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))
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))))
485 ;; Return t if the symbols did already exist, nil when we've made them.
486 (not newflag)))
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))))
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)
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)
527 ;; In this case, the buffer is its own master.
528 (buffer-file-name))))
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.
544 ;; Use buffer file name.
545 (buffer-file-name)))
546 (expand-file-name master)))
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)))
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)))
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))
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)))))
604 ;;; =========================================================================
606 ;;; Functions to compile the tables, reset the mode etc.
608 ;; The following constants are derived from `reftex-label-alist'.
610 ;; Prompt used for label type queries directed to the user.
611 (defconst reftex-type-query-prompt nil)
613 ;; Help string for label type queries.
614 (defconst reftex-type-query-help nil)
616 ;; Alist relating label type to reference format.
617 (defconst reftex-typekey-to-format-alist nil)
619 ;; Alist relating label type to label prefix.
620 (defconst reftex-typekey-to-prefix-alist nil)
622 ;; Alist relating environments or macros to label type and context regexp.
623 (defconst reftex-env-or-mac-alist nil)
625 ;; List of special environment parser functions
626 (defconst reftex-special-env-parsers nil)
628 ;; List of macros carrying a label.
629 (defconst reftex-label-mac-list nil)
631 ;; List of environments carrying a label.
632 (defconst reftex-label-env-list nil)
634 ;; List of all typekey letters in use.
635 (defconst reftex-typekey-list nil)
637 ;; Alist relating magic words to a label type.
638 (defconst reftex-words-to-typekey-alist nil)
640 ;; The last list-of-labels entry used in a reference.
641 (defvar reftex-last-used-reference (list nil nil nil nil))
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)
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.")
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))
678 ;; List of buffers created temporarily for lookup, which should be killed.
679 (defvar reftex-buffers-to-kill nil)
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]*\\*?\\|bibentry\\)\\(\\[[^]]*\\]\\|{[^}]*}\\)*{\\([^}]*,\\)?\\(%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)
701 (defvar reftex-memory nil
702 "Memorizes old variable values to indicate changes in these variables.")
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!
709 ;; Outline
710 reftex-section-levels-all
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
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
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
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)))))
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)
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))
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))
796 ;; Make sure the current document will be rescanned soon.
797 (reftex-reset-scanning-information)
799 ;; Remove any parse info file
800 (reftex-access-parse-file 'kill)
802 ;; Plug functions into AUCTeX if the user option says so.
803 (and reftex-plug-into-AUCTeX
804 (reftex-plug-into-AUCTeX))
806 (reftex-compile-variables))
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)))))
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)))))
834 (defun reftex-compile-variables ()
835 ;; Compile the information in reftex-label-alist & Co.
837 (message "Compiling label environment definitions...")
839 ;; Update AUCTeX style information
840 (when (and (featurep 'tex-site) (fboundp 'TeX-update-style))
841 (condition-case nil (TeX-update-style) (error nil)))
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)))
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)
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)))
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)))
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"))
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)))))
983 (setq reftex-typekey-to-prefix-alist
984 (nreverse reftex-typekey-to-prefix-alist))
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)
1009 (setq sum 0)
1010 (concat "\n " env)))
1011 (cdr x) " ")))
1012 qh-list "\n")))
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)))
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 "")))
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))
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 "\\)\\*?\\(\\[[^]]*\\]\\)?[[{ \t\r\n]"))
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) "]"))
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)))
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))
1172 ;;; =========================================================================
1174 ;;; Accessing the parse information
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.
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))
1188 (reftex-ensure-compiled-variables)
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)))
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))
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))))
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)))
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)
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)))
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
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))))
1302 (defun reftex-check-parse-consistency ()
1303 ;; Check if parse file is consistent, throw an error if not.
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")))
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))))
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))
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)))))))))
1360 ;;; =========================================================================
1362 ;;; Finding files
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)))
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))))
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))))
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))))))
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)))
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))
1485 ;; Recursive path computed earlier is still OK.
1486 (get pathvar 'recursive-path)))
1487 ;; The simple path was requested
1488 (symbol-value pathvar))))
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)))
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 "+")))))
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)))
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))
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)))
1556 ;;; =========================================================================
1558 ;;; Parser functions
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")
1585 ;;; =========================================================================
1587 ;;; Labels and References
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")
1600 ;;; =========================================================================
1602 ;;; Table of contents
1604 (autoload 'reftex-toc "reftex-toc"
1605 "Show the table of contents for the current document." t)
1608 ;;; =========================================================================
1610 ;;; BibTeX citations.
1612 (autoload 'reftex-citep "reftex-cite")
1613 (autoload 'reftex-citet "reftex-cite")
1614 (autoload 'reftex-make-cite-echo-string "reftex-cite")
1615 (autoload 'reftex-get-bibfile-list "reftex-cite")
1616 (autoload 'reftex-pop-to-bibtex-entry "reftex-cite")
1617 (autoload 'reftex-end-of-bib-entry "reftex-cite")
1618 (autoload 'reftex-parse-bibtex-entry "reftex-cite")
1619 (autoload 'reftex-citation "reftex-cite"
1620 "Make a citation using BibTeX database files." t)
1621 (autoload 'reftex-default-bibliography "reftex-cite")
1624 ;;; =========================================================================
1626 ;;; Selection
1628 (autoload 'reftex-select-label-mode "reftex-sel")
1629 (autoload 'reftex-select-bib-mode "reftex-sel")
1630 (autoload 'reftex-find-start-point "reftex-sel")
1631 (autoload 'reftex-insert-docstruct "reftex-sel")
1632 (autoload 'reftex-get-offset "reftex-sel")
1633 (autoload 'reftex-select-item "reftex-sel")
1636 ;;; =========================================================================
1638 ;;; Index support
1640 (autoload 'reftex-index "reftex-index"
1641 "Query for an index macro and insert it along with its argments." t)
1642 (autoload 'reftex-index-selection-or-word "reftex-index"
1643 "Put selection or the word near point into the default index macro." t)
1644 (autoload 'reftex-index-phrase-selection-or-word "reftex-index"
1645 "Put selection or the word near point into Index Phrases File." t)
1646 (autoload 'reftex-display-index "reftex-index"
1647 "Display a buffer with an index compiled from the current document." t)
1648 (autoload 'reftex-index-visit-phrases-buffer "reftex-index"
1649 "Visit the Index Phrases File." t)
1650 (autoload 'reftex-index-phrases-mode "reftex-index"
1651 "Major mode for managing the Index phrases of a LaTeX document." t)
1652 (autoload 'reftex-index-complete-tag "reftex-index")
1653 (autoload 'reftex-index-complete-key "reftex-index")
1654 (autoload 'reftex-index-show-entry "reftex-index")
1655 (autoload 'reftex-index-select-tag "reftex-index")
1658 ;;; =========================================================================
1660 ;;; View cross references
1662 (autoload 'reftex-view-crossref "reftex-dcr"
1663 "View cross reference of \\ref or \\cite macro at point." t)
1664 (autoload 'reftex-mouse-view-crossref "reftex-dcr"
1665 "View cross reference of \\ref or \\cite macro where you click." t)
1666 (autoload 'reftex-toggle-auto-view-crossref "reftex-dcr")
1667 (autoload 'reftex-view-crossref-from-bibtex "reftex-dcr"
1668 "View location in a LaTeX document which cites the BibTeX entry at point." t)
1671 ;;; =========================================================================
1673 ;;; Operations on entire Multifile documents
1675 (autoload 'reftex-create-tags-file "reftex-global"
1676 "Create TAGS file by running `etags' on the current document." t)
1677 (autoload 'reftex-grep-document "reftex-global"
1678 "Run grep query through all files related to this document." t)
1679 (autoload 'reftex-search-document "reftex-global"
1680 "Regexp search through all files of the current TeX document." t)
1681 (autoload 'reftex-query-replace-document "reftex-global"
1682 "Run a query-replace-regexp of FROM with TO over the entire TeX document." t)
1683 (autoload 'reftex-find-duplicate-labels "reftex-global"
1684 "Produce a list of all duplicate labels in the document." t)
1685 (autoload 'reftex-change-label "reftex-global"
1686 "Query replace FROM with TO in all \\label and \\ref commands." t)
1687 (autoload 'reftex-renumber-simple-labels "reftex-global"
1688 "Renumber all simple labels in the document to make them sequentially." t)
1689 (autoload 'reftex-save-all-document-buffers "reftex-global"
1690 "Save all documents associated with the current document." t)
1693 ;;; =========================================================================
1695 ;;; AUCTeX Interface
1697 (autoload 'reftex-arg-label "reftex-auc")
1698 (autoload 'reftex-arg-cite "reftex-auc")
1699 (autoload 'reftex-arg-index-tag "reftex-auc")
1700 (autoload 'reftex-arg-index "reftex-auc")
1701 (autoload 'reftex-plug-into-AUCTeX "reftex-auc")
1702 (autoload 'reftex-toggle-plug-into-AUCTeX "reftex-auc"
1703 "Toggle Interface between AUCTeX and RefTeX on and off." t)
1704 (autoload 'reftex-add-label-environments "reftex-auc")
1705 (autoload 'reftex-add-to-label-alist "reftex-auc")
1706 (autoload 'reftex-add-section-levels "reftex-auc")
1707 (autoload 'reftex-notice-new-section "reftex-auc")
1709 ;;; =========================================================================
1711 ;;; Some generally useful functions
1713 (defun reftex-typekey-check (typekey conf-variable &optional n)
1714 ;; Check if CONF-VARIABLE is true or contains TYPEKEY
1715 (and n (setq conf-variable (nth n conf-variable)))
1716 (or (eq conf-variable t)
1717 (and (stringp conf-variable)
1718 (string-match (concat "[" conf-variable "]") typekey))))
1720 (defun reftex-check-recursive-edit ()
1721 ;; Check if we are already in a recursive edit. Abort with helpful
1722 ;; message if so.
1723 (if (marker-position reftex-recursive-edit-marker)
1724 (error
1725 (substitute-command-keys
1726 "In unfinished selection process. Finish, or abort with \\[abort-recursive-edit]"))))
1728 (defun reftex-in-comment ()
1729 (save-excursion
1730 (skip-chars-backward "^%\n\r")
1731 (eq (preceding-char) ?%)))
1733 (defun reftex-no-props (string)
1734 ;; Return STRING with all text properties removed
1735 (and (stringp string)
1736 (set-text-properties 0 (length string) nil string))
1737 string)
1739 (defun reftex-match-string (n)
1740 ;; Match string without properties
1741 (when (match-beginning n)
1742 (buffer-substring-no-properties (match-beginning n) (match-end n))))
1744 (defun reftex-kill-buffer (buffer)
1745 ;; Kill buffer if it exists.
1746 (and (setq buffer (get-buffer buffer))
1747 (kill-buffer buffer)))
1749 (defun reftex-erase-buffer (&optional buffer)
1750 ;; Erase BUFFER if it exists. BUFFER defaults to current buffer.
1751 ;; This even erases read-only buffers.
1752 (cond
1753 ((null buffer)
1754 ;; erase current buffer
1755 (let ((buffer-read-only nil)) (erase-buffer)))
1756 ((setq buffer (get-buffer buffer))
1757 ;; buffer exists
1758 (save-excursion
1759 (set-buffer buffer)
1760 (let ((buffer-read-only nil)) (erase-buffer))))))
1762 (defun reftex-this-word (&optional class)
1763 ;; Grab the word around point.
1764 (setq class (or class "-a-zA-Z0-9:_/.*;|"))
1765 (save-excursion
1766 (buffer-substring-no-properties
1767 (progn (skip-chars-backward class) (point))
1768 (progn (skip-chars-forward class) (point)))))
1770 (defun reftex-number (n unit &optional ending)
1771 (if (and (integerp n) (stringp unit))
1772 (format "%d %s%s" n unit (if (= n 1) "" (or ending "s")))
1773 ""))
1775 (defun reftex-all-assq (key list)
1776 ;; Return a list of all associations of KEY in LIST. Comparison with eq.
1777 (let (rtn)
1778 (while (setq list (memq (assq key list) list))
1779 (push (car list) rtn)
1780 (pop list))
1781 (nreverse rtn)))
1783 (defun reftex-all-assoc-string (key list)
1784 ;; Return a list of all associations of KEY in LIST. Comparison with string=.
1785 (let (rtn)
1786 (while list
1787 (if (string= (car (car list)) key)
1788 (push (car list) rtn))
1789 (pop list))
1790 (nreverse rtn)))
1792 (defun reftex-last-assoc-before-elt (key elt list &optional exclusive)
1793 ;; Find the last association of KEY in LIST before or at ELT
1794 ;; ELT is found in LIST with equal, not eq.
1795 ;; Returns nil when either KEY or elt are not found in LIST.
1796 ;; When EXCLUSIVE is non-nil, ELT cannot be the return value.
1797 ;; On success, returns the association.
1798 (let* ((elt (car (member elt list))) (ex (not exclusive)) ass last-ass)
1799 (while (and (setq ass (assoc key list))
1800 (setq list (memq ass list))
1801 (or ex (not (eq elt (car list))))
1802 (memq elt list))
1803 (setq last-ass ass
1804 list (cdr list)))
1805 last-ass))
1807 (defun reftex-sublist-nth (list nth predicate &optional completion)
1808 ;; Make a list of the NTH elements of all members of LIST which
1809 ;; fulfill PREDICATE.
1810 ;; When COMPLETION is non-nil, make all elements of the resulting
1811 ;; list also a list, so that the result can be used for completion.
1812 (let (rtn)
1813 (while list
1814 (if (funcall predicate (car list))
1815 (push (if completion
1816 (list (nth nth (car list)))
1817 (nth nth (car list)))
1818 rtn))
1819 (setq list (cdr list)))
1820 (nreverse rtn)))
1822 (defun reftex-make-selection-buffer-name (type &optional index)
1823 ;; Make unique name for a selection buffer.
1824 (format " *RefTeX[%s][%d]*"
1825 type (or index (get reftex-docstruct-symbol :master-index) 0)))
1827 (defun reftex-make-index-buffer-name (tag &optional cnt)
1828 ;; Make unique name for an index buffer.
1829 (format "*Index[%s][%d]*"
1830 tag (or cnt (get reftex-docstruct-symbol :master-index) 0)))
1832 (defun reftex-truncate (string ncols &optional ellipses padding)
1833 ;; Truncate STRING to NCOLS characters.
1834 ;; When PADDING is non-nil, and string is shorter than NCOLS, fill with
1835 ;; white space to NCOLS characters. When ELLIPSES is non-nil and the
1836 ;; string needs to be truncated, replace last 3 characters by dots.
1837 (setq string
1838 (if (<= (length string) ncols)
1839 string
1840 (if ellipses
1841 (concat (substring string 0 (- ncols 3)) "...")
1842 (substring string 0 ncols))))
1843 (if padding
1844 (format (format "%%-%ds" ncols) string)
1845 string))
1847 (defun reftex-nearest-match (regexp &optional max-length)
1848 ;; Find the nearest match of REGEXP. Set the match data.
1849 ;; If POS is given, calculate distances relative to it.
1850 ;; Return nil if there is no match.
1851 (let ((pos (point))
1852 (dist (or max-length (length regexp)))
1853 match1 match2 match)
1854 (goto-char (min (+ pos dist) (point-max)))
1855 (when (re-search-backward regexp nil t)
1856 (setq match1 (match-data)))
1857 (goto-char (max (- pos dist) (point-min)))
1858 (when (re-search-forward regexp nil t)
1859 (setq match2 (match-data)))
1860 (goto-char pos)
1861 (setq match
1862 (cond
1863 ((not match1) match2)
1864 ((not match2) match1)
1865 ((< (abs (- pos (car match1))) (abs (- pos (car match2)))) match1)
1866 (t match2)))
1867 (if match (progn (set-match-data match) t) nil)))
1869 (defun reftex-auto-mode-alist ()
1870 ;; Return an `auto-mode-alist' with only the .gz (etc) thingies.
1871 ;; Stolen from gnus nnheader.
1872 (let ((alist auto-mode-alist)
1873 out)
1874 (while alist
1875 (when (listp (cdr (car alist)))
1876 (push (car alist) out))
1877 (pop alist))
1878 (nreverse out)))
1880 (defun reftex-window-height ()
1881 (if (fboundp 'window-displayed-height)
1882 (window-displayed-height)
1883 (window-height)))
1885 (defun reftex-enlarge-to-fit (buf2 &optional keep-current)
1886 ;; Enlarge other window displaying buffer to show whole buffer if possible.
1887 ;; If KEEP-CURRENT in non-nil, current buffer must remain visible.
1888 (let* ((win1 (selected-window))
1889 (buf1 (current-buffer))
1890 (win2 (get-buffer-window buf2))) ;; Only on current frame.
1891 (when win2
1892 (select-window win2)
1893 (unless (and (pos-visible-in-window-p 1)
1894 (pos-visible-in-window-p (point-max)))
1895 (enlarge-window (1+ (- (count-lines 1 (point-max))
1896 (reftex-window-height))))))
1897 (cond
1898 ((window-live-p win1) (select-window win1))
1899 (keep-current
1900 ;; we must have the old buffer!
1901 (switch-to-buffer-other-window buf1)
1902 (shrink-window (- (window-height) window-min-height))))))
1904 (defun reftex-select-with-char (prompt help-string &optional delay-time scroll)
1905 ;; Offer to select something with PROMPT and, after DELAY-TIME seconds,
1906 ;; also with HELP-STRING.
1907 ;; When SCROLL is non-nil, use SPC and DEL to scroll help window.
1908 (let ((char ?\?))
1909 (save-window-excursion
1910 (catch 'exit
1911 (message (concat prompt " (?=Help)"))
1912 (when (or (sit-for (or delay-time 0))
1913 (= ?\? (setq char (read-char-exclusive))))
1914 (reftex-kill-buffer "*RefTeX Select*")
1915 (switch-to-buffer-other-window "*RefTeX Select*")
1916 (insert help-string)
1917 (goto-char 1)
1918 (unless (and (pos-visible-in-window-p 1)
1919 (pos-visible-in-window-p (point-max)))
1920 (enlarge-window (1+ (- (count-lines 1 (point-max))
1921 (reftex-window-height)))))
1922 (setq truncate-lines t))
1923 (if (and (pos-visible-in-window-p 1)
1924 (pos-visible-in-window-p (point-max)))
1926 (setq prompt (concat prompt (if scroll " (SPC/DEL=Scroll)" ""))))
1927 (message prompt)
1928 (and (equal char ?\?) (setq char (read-char-exclusive)))
1929 (while t
1930 (cond ((equal char ?\C-g) (keyboard-quit))
1931 ((equal char ?\?))
1932 ((and scroll (equal char ?\ ))
1933 (condition-case nil (scroll-up) (error nil))
1934 (message prompt))
1935 ((and scroll (equal char ?\C-? ))
1936 (condition-case nil (scroll-down) (error nil))
1937 (message prompt))
1938 (t (message "")
1939 (throw 'exit char)))
1940 (setq char (read-char-exclusive)))))))
1943 (defun reftex-make-regexp-allow-for-ctrl-m (string)
1944 ;; convert STRING into a regexp, allowing ^M for \n and vice versa
1945 (let ((start -2))
1946 (setq string (regexp-quote string))
1947 (while (setq start (string-match "[\n\r]" string (+ 3 start)))
1948 (setq string (replace-match "[\n\r]" nil t string)))
1949 string))
1951 (defun reftex-get-buffer-visiting (file)
1952 ;; return a buffer visiting FILE
1953 (cond
1954 ((boundp 'find-file-compare-truenames) ; XEmacs
1955 (let ((find-file-compare-truenames t))
1956 (get-file-buffer file)))
1957 ((fboundp 'find-buffer-visiting) ; Emacs
1958 (find-buffer-visiting file))
1959 (t (error "This should not happen (reftex-get-buffer-visiting)"))))
1961 ;; Define `current-message' for compatibility with XEmacs prior to 20.4
1962 (defvar message-stack)
1963 (if (and (featurep 'xemacs)
1964 (not (fboundp 'current-message)))
1965 (defun current-message (&optional frame)
1966 (cdr (car message-stack))))
1968 (defun reftex-visited-files (list)
1969 ;; Takes a list of filenames and returns the buffers of those already visited
1970 (delq nil (mapcar (lambda (x) (if (reftex-get-buffer-visiting x) x nil))
1971 list)))
1973 (defun reftex-get-file-buffer-force (file &optional mark-to-kill)
1974 ;; Return a buffer visiting file. Make one, if necessary.
1975 ;; If neither such a buffer nor the file exist, return nil.
1976 ;; If MARK-TO-KILL is t and there is no live buffer, visit the file with
1977 ;; initializations according to `reftex-initialize-temporary-buffers',
1978 ;; and mark the buffer to be killed after use.
1980 (let ((buf (reftex-get-buffer-visiting file)))
1982 (cond (buf
1983 ;; We have it already as a buffer - just return it
1984 buf)
1986 ((file-readable-p file)
1987 ;; At least there is such a file and we can read it.
1989 (if (or (not mark-to-kill)
1990 (eq t reftex-initialize-temporary-buffers))
1992 ;; Visit the file with full magic
1993 (setq buf (find-file-noselect file))
1995 ;; Else: Visit the file just briefly, without or
1996 ;; with limited Magic
1998 ;; The magic goes away
1999 (let ((format-alist nil)
2000 (auto-mode-alist (reftex-auto-mode-alist))
2001 (default-major-mode 'fundamental-mode)
2002 (enable-local-variables nil)
2003 (after-insert-file-functions nil))
2004 (setq buf (find-file-noselect file)))
2006 ;; Is there a hook to run?
2007 (when (listp reftex-initialize-temporary-buffers)
2008 (save-excursion
2009 (set-buffer buf)
2010 (run-hooks 'reftex-initialize-temporary-buffers))))
2012 ;; Lets see if we got a license to kill :-|
2013 (and mark-to-kill
2014 (add-to-list 'reftex-buffers-to-kill buf))
2016 ;; Return the new buffer
2017 buf)
2019 ;; If no such file exists, return nil
2020 (t nil))))
2022 (defun reftex-kill-temporary-buffers (&optional buffer)
2023 ;; Kill all buffers in the list reftex-kill-temporary-buffers.
2024 (cond
2025 (buffer
2026 (when (member buffer reftex-buffers-to-kill)
2027 (kill-buffer buffer)
2028 (setq reftex-buffers-to-kill
2029 (delete buffer reftex-buffers-to-kill))))
2031 (while (setq buffer (pop reftex-buffers-to-kill))
2032 (when (bufferp buffer)
2033 (and (buffer-modified-p buffer)
2034 (y-or-n-p (format "Save file %s? "
2035 (buffer-file-name buffer)))
2036 (save-excursion
2037 (set-buffer buffer)
2038 (save-buffer)))
2039 (kill-buffer buffer))
2040 (pop reftex-buffers-to-kill)))))
2042 (defun reftex-splice-symbols-into-list (list alist)
2043 ;; Splice the association in ALIST of any symbols in LIST into the list.
2044 ;; Return new list.
2045 (let (rtn tmp)
2046 (while list
2047 (while (and (not (null (car list))) ;; keep list elements nil
2048 (symbolp (car list)))
2049 (setq tmp (car list))
2050 (cond
2051 ((assoc tmp alist)
2052 (setq list (append (nth 2 (assoc tmp alist)) (cdr list))))
2054 (error "Cannot treat symbol %s in reftex-label-alist"
2055 (symbol-name tmp)))))
2056 (push (pop list) rtn))
2057 (nreverse rtn)))
2059 (defun reftex-remove-symbols-from-list (list)
2060 ;; Remove all symbols from list
2061 (let (rtn)
2062 (while list
2063 (unless (symbolp (car list))
2064 (push (car list) rtn))
2065 (setq list (cdr list)))
2066 (nreverse rtn)))
2068 (defun reftex-uniquify (list)
2069 ;; Return a list of all elements in LIST, but each only once
2070 (let (new elm)
2071 (while list
2072 (setq elm (pop list))
2073 (unless (member elm new)
2074 (push elm new)))
2075 (nreverse new)))
2077 (defun reftex-uniquify-by-car (alist &optional keep-list)
2078 ;; Return a list of all elements in ALIST, but each car only once.
2079 ;; Elements of KEEP-LIST are not removed even if duplicate.
2080 (let (new elm)
2081 (while alist
2082 (setq elm (pop alist))
2083 (if (or (member (car elm) keep-list)
2084 (not (assoc (car elm) new)))
2085 (push elm new)))
2086 (nreverse new)))
2088 (defun reftex-abbreviate-title (string)
2089 (reftex-convert-string string "[-~ \t\n\r,;]" nil t t
2090 5 40 nil 1 " " (nth 5 reftex-derive-label-parameters)))
2092 (defun reftex-convert-string (string split-re illegal-re dot keep-fp
2093 nwords maxchar illegal abbrev sep
2094 ignore-words &optional downcase)
2095 "Convert a string (a sentence) to something shorter.
2096 SPLIT-RE is the regular expression used to split the string into words.
2097 ILLEGAL-RE matches characters which are illegal in the final string.
2098 DOT t means add dots to abbreviated words.
2099 KEEP-FP t means to keep a final punctuation when applicable.
2100 NWORDS Number of words to use.
2101 MAXCHAR Maximum number of characters in the final string.
2102 ILLEGAL nil: Throw away any words containing stuff matched with ILLEGAL-RE.
2103 t: Throw away only the matched part, not the whole word.
2104 ABBREV nil: Never abbreviate words.
2105 t: Always abbreviate words (see `reftex-abbrev-parameters').
2106 not t and not nil: Abbreviate words if necessary to shorten
2107 string below MAXCHAR.
2108 SEP String separating different words in the output string.
2109 IGNORE-WORDS List of words which should be removed from the string."
2111 (let* ((words0 (split-string string (or split-re "[ \t\n\r]")))
2112 (reftex-label-illegal-re (or illegal-re "\000"))
2113 (abbrev-re (concat
2114 "\\`\\("
2115 (make-string (nth 0 reftex-abbrev-parameters) ?.)
2116 "[" (nth 2 reftex-abbrev-parameters) "]*"
2117 "\\)"
2118 "[" (nth 3 reftex-abbrev-parameters) "]"
2119 (make-string (1- (nth 1 reftex-abbrev-parameters)) ?.)))
2120 words word)
2122 ;; Remove words from the ignore list or with funny characters
2123 (while (setq word (pop words0))
2124 (if downcase (setq word (downcase word)))
2125 (cond
2126 ((member (downcase word) ignore-words))
2127 ((string-match reftex-label-illegal-re word)
2128 (when illegal
2129 (while (string-match reftex-label-illegal-re word)
2130 (setq word (replace-match "" nil nil word)))
2131 (push word words)))
2133 (push word words))))
2134 (setq words (nreverse words))
2136 ;; Restrict number of words
2137 (if (> (length words) nwords)
2138 (setcdr (nthcdr (1- nwords) words) nil))
2140 ;; First, try to use all words
2141 (setq string (mapconcat 'identity words sep))
2143 ;; Abbreviate words if enforced by user settings or string length
2144 (if (or (eq t abbrev)
2145 (and abbrev
2146 (> (length string) maxchar)))
2147 (setq words
2148 (mapcar
2149 (lambda (w) (if (string-match abbrev-re w)
2150 (if dot
2151 (concat (match-string 1 w) ".")
2152 (match-string 1 w))
2154 words)
2155 string (mapconcat 'identity words sep)))
2157 ;; Shorten if still to long
2158 (setq string
2159 (if (> (length string) maxchar)
2160 (substring string 0 maxchar)
2161 string))
2163 ;; Delete the final punctuation, if any
2164 (if (and (not keep-fp) (string-match "\\s.+\\'" string))
2165 (setq string (replace-match "" nil nil string)))
2166 string))
2168 (defun reftex-nicify-text (text)
2169 ;; Make TEXT nice for inclusion as context into label menu.
2170 ;; 1. remove line breaks and extra white space
2171 (while (string-match "[\n\r\t]\\|[ \t][ \t]+" text)
2172 (setq text (replace-match " " nil t text)))
2173 ;; 2. cut before the next `\end{' or `\item' or `\\'
2174 (if (string-match "\\(\\\\end{\\|\\\\item\\|\\\\\\\\\\).*" text)
2175 (setq text (replace-match "" nil t text)))
2176 ;; 3. kill the embedded label
2177 (if (string-match "\\\\label{[^}]*}" text)
2178 (setq text (replace-match "" nil t text)))
2179 ;; 4. remove leading garbage
2180 (if (string-match "\\`[ }]+" text)
2181 (setq text (replace-match "" nil t text)))
2182 ;; 5. limit length
2183 (cond
2184 ((> (length text) 100) (substring text 0 100))
2185 ((= (length text) 0) (make-string 1 ?\ ))
2186 (t text)))
2188 ;;; =========================================================================
2190 ;;; Fontification and Highlighting
2192 (defun reftex-use-fonts ()
2193 ;; Return t if we can and want to use fonts.
2194 (and window-system
2195 reftex-use-fonts
2196 (featurep 'font-lock)))
2198 (defun reftex-refontify ()
2199 ;; Return t if we need to refontify context
2200 (and (reftex-use-fonts)
2201 (or (eq t reftex-refontify-context)
2202 (and (eq 1 reftex-refontify-context)
2203 ;; Test of we use the font-lock version of x-symbol
2204 (and (featurep 'x-symbol-tex) (not (boundp 'x-symbol-mode)))))))
2206 (defvar font-lock-defaults-computed)
2207 (defun reftex-fontify-select-label-buffer (parent-buffer)
2208 ;; Fontify the `*RefTeX Select*' buffer. Buffer is temporarily renamed to
2209 ;; start with none-SPC char, beacuse Font-Lock otherwise refuses operation.
2210 (run-hook-with-args 'reftex-pre-refontification-functions
2211 parent-buffer 'reftex-ref)
2212 (let* ((oldname (buffer-name))
2213 (newname (concat "Fontify-me-" oldname)))
2214 (unwind-protect
2215 (progn
2216 ;; Rename buffer temporarily to start w/o space (because of font-lock)
2217 (rename-buffer newname t)
2218 (cond
2219 ((fboundp 'font-lock-default-fontify-region)
2220 ;; Good: we have the indirection functions
2221 (set (make-local-variable 'font-lock-fontify-region-function)
2222 'reftex-select-font-lock-fontify-region)
2223 (let ((major-mode 'latex-mode))
2224 (font-lock-mode 1)))
2225 ((fboundp 'font-lock-set-defaults-1)
2226 ;; Looks like the XEmacs font-lock stuff.
2227 ;; FIXME: this is still kind of a hack, but it works.
2228 (set (make-local-variable 'font-lock-keywords) nil)
2229 (let ((major-mode 'latex-mode)
2230 (font-lock-defaults-computed nil))
2231 (font-lock-set-defaults-1)
2232 (reftex-select-font-lock-fontify-region (point-min) (point-max))))
2234 ;; Oops?
2235 (message "Sorry: cannot refontify RefTeX Select buffer."))))
2236 (rename-buffer oldname))))
2238 (defun reftex-select-font-lock-fontify-region (beg end &optional loudly)
2239 ;; Fontify a region, but only lines starting with a dot.
2240 (let ((func (if (fboundp 'font-lock-default-fontify-region)
2241 'font-lock-default-fontify-region
2242 'font-lock-fontify-region))
2243 beg1 end1)
2244 (goto-char beg)
2245 (while (re-search-forward "^\\." end t)
2246 (setq beg1 (point) end1 (progn (skip-chars-forward "^\n") (point)))
2247 (funcall func beg1 end1 nil)
2248 (goto-char end1))))
2250 (defun reftex-select-font-lock-unfontify (&rest ignore) t)
2252 (defun reftex-verified-face (&rest faces)
2253 ;; Return the first valid face in FACES, or nil if none is valid.
2254 ;; Also, when finding a nil element in FACES, return nil. This
2255 ;; function is just a safety net to catch name changes of builtin
2256 ;; fonts. Currently it is only used for reftex-label-face, which has
2257 ;; as default font-lock-reference-face, which was recently renamed
2258 ;; to font-lock-constant-face.
2259 (let (face)
2260 (catch 'exit
2261 (while (setq face (pop faces))
2262 (if (featurep 'xemacs)
2263 (if (find-face face) (throw 'exit face))
2264 (if (facep face) (throw 'exit face)))))))
2266 ;; Highlighting uses overlays. For XEmacs, we need the emulation.
2267 (if (featurep 'xemacs) (require 'overlay))
2269 ;; We keep a vector with several different overlays to do our highlighting.
2270 (defvar reftex-highlight-overlays [nil nil])
2272 ;; Initialize the overlays
2273 (aset reftex-highlight-overlays 0 (make-overlay 1 1))
2274 (overlay-put (aref reftex-highlight-overlays 0)
2275 'face 'highlight)
2276 (aset reftex-highlight-overlays 1 (make-overlay 1 1))
2277 (overlay-put (aref reftex-highlight-overlays 1)
2278 'face reftex-cursor-selected-face)
2280 ;; Two functions for activating and deactivation highlight overlays
2281 (defun reftex-highlight (index begin end &optional buffer)
2282 "Highlight a region with overlay INDEX."
2283 (move-overlay (aref reftex-highlight-overlays index)
2284 begin end (or buffer (current-buffer))))
2285 (defun reftex-unhighlight (index)
2286 "Detach overlay INDEX."
2287 (delete-overlay (aref reftex-highlight-overlays index)))
2289 (defun reftex-highlight-shall-die ()
2290 ;; Function used in pre-command-hook to remove highlights.
2291 (remove-hook 'pre-command-hook 'reftex-highlight-shall-die)
2292 (reftex-unhighlight 0))
2294 ;;; =========================================================================
2296 ;;; Keybindings
2298 ;; The default bindings in the mode map.
2299 (loop for x in
2300 '(("\C-c=" . reftex-toc)
2301 ("\C-c(" . reftex-label)
2302 ("\C-c)" . reftex-reference)
2303 ("\C-c[" . reftex-citation)
2304 ("\C-c<" . reftex-index)
2305 ("\C-c>" . reftex-display-index)
2306 ("\C-c/" . reftex-index-selection-or-word)
2307 ("\C-c\\" . reftex-index-phrase-selection-or-word)
2308 ("\C-c|" . reftex-index-visit-phrases-buffer)
2309 ("\C-c&" . reftex-view-crossref))
2310 do (define-key reftex-mode-map (car x) (cdr x)))
2312 ;; Bind `reftex-mouse-view-crossref' only when the key is still free
2313 (if (featurep 'xemacs)
2314 (unless (key-binding [(shift button2)])
2315 (define-key reftex-mode-map [(shift button2)]
2316 'reftex-mouse-view-crossref))
2317 (unless (key-binding [(shift mouse-2)])
2318 (define-key reftex-mode-map [(shift mouse-2)]
2319 'reftex-mouse-view-crossref)))
2321 ;; Bind `reftex-view-crossref-from-bibtex' in BibTeX mode map
2322 (eval-after-load
2323 "bibtex"
2324 '(define-key bibtex-mode-map "\C-c&" 'reftex-view-crossref-from-bibtex))
2326 ;; If the user requests so, she can have a few more bindings:
2327 (when reftex-extra-bindings
2328 (loop for x in
2329 '(("\C-ct" . reftex-toc)
2330 ("\C-cl" . reftex-label)
2331 ("\C-cr" . reftex-reference)
2332 ("\C-cc" . reftex-citation)
2333 ("\C-cv" . reftex-view-crossref)
2334 ("\C-cg" . reftex-grep-document)
2335 ("\C-cs" . reftex-search-document))
2336 do (define-key reftex-mode-map (car x) (cdr x))))
2338 ;;; =========================================================================
2340 ;;; Menu
2342 ;; Define a menu for the menu bar if Emacs is running under X
2344 (require 'easymenu)
2346 (easy-menu-define reftex-mode-menu reftex-mode-map
2347 "Menu used in RefTeX mode"
2348 `("Ref"
2349 ["Table of Contents" reftex-toc t]
2350 "--"
2351 ["\\label" reftex-label t]
2352 ["\\ref" reftex-reference t]
2353 ["\\cite" reftex-citation t]
2354 ("\\index"
2355 ["\\index" reftex-index t]
2356 ["\\index{THIS}" reftex-index-selection-or-word t]
2357 "--"
2358 ["Add THIS to Index Phrases" reftex-index-phrase-selection-or-word t]
2359 ["Visit Phrase Buffer" reftex-index-visit-phrases-buffer t]
2360 ["Apply Phrases to Region" reftex-index-phrases-apply-to-region t]
2361 "--"
2362 ["Display the Index" reftex-display-index t])
2363 "--"
2364 ["View Crossref" reftex-view-crossref t]
2365 "--"
2366 ("Parse Document"
2367 ["One File" reftex-parse-one reftex-enable-partial-scans]
2368 ["Entire Document" reftex-parse-all t]
2369 ["Save to File" (reftex-access-parse-file 'write)
2370 (> (length (symbol-value reftex-docstruct-symbol)) 0)]
2371 ["Restore from File" (reftex-access-parse-file 'restore) t]
2372 "--"
2373 ["Reset RefTeX Mode" reftex-reset-mode t])
2374 ("Global Actions"
2375 ["Search Whole Document" reftex-search-document t]
2376 ["Replace in Document" reftex-query-replace-document t]
2377 ["Grep on Document" reftex-grep-document t]
2378 "--"
2379 ["Find Duplicate Labels" reftex-find-duplicate-labels t]
2380 ["Change Label and Refs" reftex-change-label t]
2381 ["Renumber Simple Labels" reftex-renumber-simple-labels t]
2382 "--"
2383 ["Create TAGS File" reftex-create-tags-file t]
2384 "--"
2385 ["Save Document" reftex-save-all-document-buffers t])
2386 "--"
2387 ("Options"
2388 "PARSER"
2389 ["Partial Scans"
2390 (setq reftex-enable-partial-scans (not reftex-enable-partial-scans))
2391 :style toggle :selected reftex-enable-partial-scans]
2392 ["Auto-Save Parse Info"
2393 (setq reftex-save-parse-info (not reftex-save-parse-info))
2394 :style toggle :selected reftex-save-parse-info]
2395 "--"
2396 "CROSSREF INFO"
2397 ["Automatic Info" reftex-toggle-auto-view-crossref
2398 :style toggle :selected reftex-auto-view-crossref-timer]
2399 ["...in Echo Area" (setq reftex-auto-view-crossref t)
2400 :style radio :selected (eq reftex-auto-view-crossref t)]
2401 ["...in Other Window" (setq reftex-auto-view-crossref 'window)
2402 :style radio :selected (eq reftex-auto-view-crossref 'window)]
2403 "--"
2404 "MISC"
2405 ["AUC TeX Interface" reftex-toggle-plug-into-AUCTeX
2406 :style toggle :selected reftex-plug-into-AUCTeX])
2407 ("Reference Style"
2408 ["Default" (setq reftex-vref-is-default nil
2409 reftex-fref-is-default nil)
2410 :style radio :selected (not (or reftex-vref-is-default
2411 reftex-fref-is-default))]
2412 ["Varioref" (setq reftex-vref-is-default t
2413 reftex-fref-is-default nil)
2414 :style radio :selected reftex-vref-is-default]
2415 ["Fancyref" (setq reftex-fref-is-default t
2416 reftex-vref-is-default nil)
2417 :style radio :selected reftex-fref-is-default])
2418 ("Citation Style"
2419 ,@(mapcar
2420 (lambda (x)
2421 (vector
2422 (capitalize (symbol-name (car x)))
2423 (list 'reftex-set-cite-format (list 'quote (car x)))
2424 :style 'radio :selected
2425 (list 'eq (list 'reftex-get-cite-format) (list 'quote (car x)))))
2426 reftex-cite-format-builtin)
2427 "--"
2428 "Sort Database Matches"
2429 ["Not" (setq reftex-sort-bibtex-matches nil)
2430 :style radio :selected (eq reftex-sort-bibtex-matches nil)]
2431 ["by Author" (setq reftex-sort-bibtex-matches 'author)
2432 :style radio :selected (eq reftex-sort-bibtex-matches 'author)]
2433 ["by Year" (setq reftex-sort-bibtex-matches 'year)
2434 :style radio :selected (eq reftex-sort-bibtex-matches 'year)]
2435 ["by Year, reversed" (setq reftex-sort-bibtex-matches 'reverse-year)
2436 :style radio :selected (eq reftex-sort-bibtex-matches 'reverse-year)])
2437 ("Index Style"
2438 ,@(mapcar
2439 (lambda (x)
2440 (vector
2441 (capitalize (symbol-name (car x)))
2442 (list 'reftex-add-index-macros (list 'list (list 'quote (car x))))
2443 :style 'radio :selected
2444 (list 'memq (list 'quote (car x))
2445 (list 'get 'reftex-docstruct-symbol
2446 (list 'quote 'reftex-index-macros-style)))))
2447 reftex-index-macros-builtin))
2448 "--"
2449 ("Customize"
2450 ["Browse RefTeX Group" reftex-customize t]
2451 "--"
2452 ["Build Full Customize Menu" reftex-create-customize-menu
2453 (fboundp 'customize-menu-create)])
2454 ("Documentation"
2455 ["Info" reftex-info t]
2456 ["Commentary" reftex-show-commentary t])))
2458 (defun reftex-customize ()
2459 "Call the customize function with reftex as argument."
2460 (interactive)
2461 (customize-browse 'reftex))
2463 (defun reftex-create-customize-menu ()
2464 "Create a full customization menu for RefTeX, insert it into the menu."
2465 (interactive)
2466 (if (fboundp 'customize-menu-create)
2467 (progn
2468 (easy-menu-change
2469 '("Ref") "Customize"
2470 `(["Browse RefTeX group" reftex-customize t]
2471 "--"
2472 ,(customize-menu-create 'reftex)
2473 ["Set" Custom-set t]
2474 ["Save" Custom-save t]
2475 ["Reset to Current" Custom-reset-current t]
2476 ["Reset to Saved" Custom-reset-saved t]
2477 ["Reset to Standard Settings" Custom-reset-standard t]))
2478 (message "\"Ref\"-menu now contains full customization menu"))
2479 (error "Cannot expand menu (outdated version of cus-edit.el)")))
2481 (defun reftex-show-commentary ()
2482 "Use the finder to view the file documentation from `reftex.el'."
2483 (interactive)
2484 (require 'finder)
2485 (finder-commentary "reftex.el"))
2487 (defun reftex-info (&optional node)
2488 "Read documentation for RefTeX in the info system.
2489 With optional NODE, go directly to that node."
2490 (interactive)
2491 (require 'info)
2492 (Info-goto-node (format "(reftex)%s" (or node ""))))
2494 ;;; Install the kill-buffer and kill-emacs hooks ------------------------------
2496 (add-hook 'kill-buffer-hook 'reftex-kill-buffer-hook)
2497 (add-hook 'kill-emacs-hook 'reftex-kill-emacs-hook)
2499 ;;; Run Hook ------------------------------------------------------------------
2501 (run-hooks 'reftex-load-hook)
2503 ;;; That's it! ----------------------------------------------------------------
2505 (setq reftex-tables-dirty t) ; in case this file is evaluated by hand
2506 (provide 'reftex)
2508 ;;;============================================================================
2510 ;;; reftex.el ends here