(server-process-filter): Run pre-command-hook,
[emacs.git] / lisp / textmodes / reftex.el
blob9c3b90118f1b2db14651d60e26a4b879a4c7fc5d
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.15
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.15"
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 ((fboundp 'tex-main-file) (tex-main-file)) ; Emacs LaTeX mode
510 ((boundp 'TeX-master) ; The variable is defined - lets use it.
511 (cond
512 ((eq TeX-master t)
513 (buffer-file-name))
514 ((eq TeX-master 'shared)
515 (setq TeX-master (read-file-name "Master file: "
516 nil nil t nil)))
517 (TeX-master)
519 (setq TeX-master (read-file-name "Master file: "
520 nil nil t nil)))))
521 ((boundp 'tex-main-file)
522 ;; This is the variable from the default TeX modes.
523 (cond
524 ((stringp tex-main-file)
525 ;; ok, this must be it
526 tex-main-file)
528 ;; In this case, the buffer is its own master.
529 (buffer-file-name))))
531 ;; Know nothing about master file. Assume this is a master file.
532 (buffer-file-name)))))
533 (cond
534 ((null master)
535 (error "Need a filename for this buffer, please save it first"))
536 ((or (file-exists-p (concat master ".tex"))
537 (reftex-get-buffer-visiting (concat master ".tex")))
538 ;; Ahh, an extra .tex was missing...
539 (setq master (concat master ".tex")))
540 ((or (file-exists-p master)
541 (reftex-get-buffer-visiting master))
542 ;; We either see the file, or have a buffer on it. OK.
545 ;; Use buffer file name.
546 (buffer-file-name)))
547 (expand-file-name master)))
549 (defun reftex-is-multi ()
550 ;; Tell if this is a multifile document. When not sure, say yes.
551 (let ((entry (assq 'is-multi (symbol-value reftex-docstruct-symbol))))
552 (if entry
553 (nth 1 entry)
554 t)))
556 (defun reftex-set-cite-format (value)
557 "Set the document-local value of `reftex-cite-format'.
558 When such a value exists, it overwrites the setting given with
559 `reftex-cite-format'. See the documentation of `reftex-cite-format'
560 for possible values. This function should be used from AUCTeX style files."
561 (unless reftex-docstruct-symbol
562 (reftex-tie-multifile-symbols))
563 (when (and reftex-docstruct-symbol
564 (symbolp reftex-docstruct-symbol))
565 (put reftex-docstruct-symbol 'reftex-cite-format value)))
567 (defun reftex-get-cite-format ()
568 ;; Return the current citation format. Either the document-local value in
569 ;; reftex-cite-format-symbol, or the global value in reftex-cite-format.
570 (if (and reftex-docstruct-symbol
571 (symbolp reftex-docstruct-symbol)
572 (get reftex-docstruct-symbol 'reftex-cite-format))
573 (get reftex-docstruct-symbol 'reftex-cite-format)
574 reftex-cite-format))
576 (defun reftex-add-index-macros (entry-list)
577 "Add index macro descriptions to `reftex-index-macros-style'.
578 The format of ENTRY-LIST is exactly like `reftex-index-macros'. See there
579 for details.
580 This function makes it possible to support RefTeX from AUCTeX style files.
581 The entries in ENTRY-LIST will be processed after the user settings in
582 `reftex-index-entries', and before the defaults. Any changes made to
583 `reftex-label-alist-style' will raise a flag to the effect that
584 the label information is recompiled on next use."
585 (unless reftex-docstruct-symbol
586 (reftex-tie-multifile-symbols))
587 (when (and reftex-docstruct-symbol
588 (symbolp reftex-docstruct-symbol))
589 (let ((list (get reftex-docstruct-symbol 'reftex-index-macros-style))
590 entry changed)
591 (while entry-list
592 (setq entry (pop entry-list))
593 ;; When it is a symbol, remove all other symbols
594 (and (symbolp entry)
595 (not (memq entry list))
596 (setq list (reftex-remove-symbols-from-list list)))
597 ;; Add to list unless already member
598 (unless (member entry list)
599 (setq reftex-tables-dirty t
600 changed t)
601 (push entry list)))
602 (when changed
603 (put reftex-docstruct-symbol 'reftex-index-macros-style list)))))
605 ;;; =========================================================================
607 ;;; Functions to compile the tables, reset the mode etc.
609 ;; The following constants are derived from `reftex-label-alist'.
611 ;; Prompt used for label type queries directed to the user.
612 (defconst reftex-type-query-prompt nil)
614 ;; Help string for label type queries.
615 (defconst reftex-type-query-help nil)
617 ;; Alist relating label type to reference format.
618 (defconst reftex-typekey-to-format-alist nil)
620 ;; Alist relating label type to label prefix.
621 (defconst reftex-typekey-to-prefix-alist nil)
623 ;; Alist relating environments or macros to label type and context regexp.
624 (defconst reftex-env-or-mac-alist nil)
626 ;; List of special environment parser functions
627 (defconst reftex-special-env-parsers nil)
629 ;; List of macros carrying a label.
630 (defconst reftex-label-mac-list nil)
632 ;; List of environments carrying a label.
633 (defconst reftex-label-env-list nil)
635 ;; List of all typekey letters in use.
636 (defconst reftex-typekey-list nil)
638 ;; Alist relating magic words to a label type.
639 (defconst reftex-words-to-typekey-alist nil)
641 ;; The last list-of-labels entry used in a reference.
642 (defvar reftex-last-used-reference (list nil nil nil nil))
644 ;; Alist relating index macros to other info.
645 (defconst reftex-key-to-index-macro-alist nil)
646 ;; Prompt for index macro queries
647 (defconst reftex-query-index-macro-prompt nil)
648 ;; Help string for index macro queries
649 (defconst reftex-query-index-macro-help nil)
651 ;; The message when follow-mode is suspended
652 (defconst reftex-no-follow-message
653 "No follow-mode into unvisited file. Press SPC to visit it.")
654 (defconst reftex-no-info-message
655 "%s: info not available, use `\\[reftex-view-crossref]' to get it.")
657 ;; Global variables used for communication between functions.
658 (defvar reftex-default-context-position nil)
659 (defvar reftex-location-start nil)
660 (defvar reftex-call-back-to-this-buffer nil)
661 (defvar reftex-select-return-marker (make-marker))
662 (defvar reftex-active-toc nil)
663 (defvar reftex-tex-path nil)
664 (defvar reftex-bib-path nil)
665 (defvar reftex-select-marked nil)
666 (defvar reftex-last-follow-point nil)
667 (defvar reftex-latex-syntax-table nil)
668 (defvar reftex-prefix nil)
669 (defvar reftex-section-levels-all nil)
670 (defvar reftex-buffers-with-changed-invisibility nil)
671 (defvar reftex-callback-fwd t)
672 (defvar reftex-last-toc-master nil
673 "Stores the name of the tex file that `reftex-toc' was last run on.")
674 (defvar reftex-auto-view-crossref-timer nil
675 "The timer used for auto-view-crossref.")
676 ;; Marker for return point from recursive edit
677 (defvar reftex-recursive-edit-marker (make-marker))
679 ;; List of buffers created temporarily for lookup, which should be killed.
680 (defvar reftex-buffers-to-kill nil)
682 ;; Regexp to find anything.
683 (defvar reftex-section-regexp nil)
684 (defvar reftex-section-or-include-regexp nil)
685 (defvar reftex-index-macro-regexp nil)
686 (defvar reftex-index-level-re nil)
687 (defvar reftex-index-key-end-re nil)
688 (defvar reftex-find-index-entry-regexp-format nil)
689 (defvar reftex-everything-regexp nil)
690 (defvar reftex-everything-regexp-no-index nil)
691 (defvar reftex-index-re nil)
692 (defvar reftex-find-citation-regexp-format
693 "\\\\\\([a-zA-Z]*cite[*a-zA-Z]*\\*?\\|bibentry\\)\\(\\[[^]]*\\]\\|{[^}]*}\\)*{\\([^}]*,\\)?\\(%s\\)[},]")
694 (defvar reftex-find-reference-format
695 "\\\\\\(ref[a-zA-Z]*\\|[a-zA-Z]*ref\\(range\\)?\\)\\*?\\(\\[[^]]*\\]\\|{[^}]*}\\)*{\\(%s\\)}")
696 (defvar reftex-macros-with-labels nil)
697 (defvar reftex-macros-with-index nil)
698 (defvar reftex-index-macro-alist nil)
699 (defvar reftex-find-label-regexp-format nil)
700 (defvar reftex-find-label-regexp-format2 nil)
702 (defvar reftex-memory nil
703 "Memorizes old variable values to indicate changes in these variables.")
705 ;; A list of all variables in the cache.
706 ;; The cache is used to save the compiled versions of some variables.
707 (defconst reftex-cache-variables
708 '(reftex-memory ;; This MUST ALWAYS be the first!
710 ;; Outline
711 reftex-section-levels-all
713 ;; Labels
714 reftex-env-or-mac-alist
715 reftex-special-env-parsers
716 reftex-macros-with-labels
717 reftex-label-mac-list
718 reftex-label-env-list
719 reftex-typekey-list
720 reftex-typekey-to-format-alist
721 reftex-typekey-to-prefix-alist
722 reftex-words-to-typekey-alist
723 reftex-type-query-prompt
724 reftex-type-query-help
726 ;; Index
727 reftex-index-macro-alist
728 reftex-macros-with-index
729 reftex-query-index-macro-prompt
730 reftex-query-index-macro-help
731 reftex-key-to-index-macro-alist
733 ;; Regular expressions
734 reftex-section-regexp
735 reftex-section-or-include-regexp
736 reftex-index-re
737 reftex-everything-regexp
738 reftex-everything-regexp-no-index
739 reftex-find-label-regexp-format
740 reftex-find-label-regexp-format2
741 reftex-find-index-entry-regexp-format
744 (defun reftex-ensure-compiled-variables ()
745 ;; Recompile the label alist when necessary
746 (let* ((mem reftex-memory)
747 (cache (get reftex-docstruct-symbol 'reftex-cache))
748 (cmem (car cache))
749 (alist reftex-label-alist)
750 (levels (get reftex-docstruct-symbol 'reftex-section-levels))
751 (style (get reftex-docstruct-symbol 'reftex-label-alist-style))
752 (default reftex-default-label-alist-entries)
753 (index reftex-index-macros)
754 (istyle (get reftex-docstruct-symbol 'reftex-index-macros-style)))
755 (cond
756 (reftex-tables-dirty (reftex-compile-variables))
757 ((and (eq alist (nth 0 mem))
758 (eq levels (nth 1 mem))
759 (eq style (nth 2 mem))
760 (eq default (nth 3 mem))
761 (eq index (nth 4 mem))
762 (eq istyle (nth 5 mem)))) ;; everything is OK
763 ((and (eq alist (nth 0 cmem))
764 (eq levels (nth 1 cmem))
765 (eq style (nth 2 cmem))
766 (eq default (nth 2 cmem))
767 (eq index (nth 4 cmem))
768 (eq istyle (nth 5 cmem)))
769 ;; restore the cache
770 (message "Restoring cache")
771 (mapcar (lambda (sym) (set sym (pop cache))) reftex-cache-variables))
772 (t (reftex-compile-variables)))))
774 (defun reftex-reset-mode ()
775 "Reset RefTeX Mode.
776 This will re-compile the configuration information and remove all
777 current scanning information and the parse file to enforce a rescan
778 on next use."
779 (interactive)
781 ;; Reset the file search path variables
782 (loop for prop in '(status master-dir recursive-path rec-type) do
783 (put 'reftex-tex-path prop nil)
784 (put 'reftex-bib-path prop nil))
786 ;; Kill temporary buffers associated with RefTeX - just in case they
787 ;; were not cleaned up properly
788 (save-excursion
789 (let ((buffer-list '("*RefTeX Help*" "*RefTeX Select*"
790 "*Duplicate Labels*" "*toc*" " *RefTeX-scratch*"))
791 buf)
792 (while (setq buf (pop buffer-list))
793 (if (get-buffer buf)
794 (kill-buffer buf))))
795 (reftex-erase-all-selection-and-index-buffers))
797 ;; Make sure the current document will be rescanned soon.
798 (reftex-reset-scanning-information)
800 ;; Remove any parse info file
801 (reftex-access-parse-file 'kill)
803 ;; Plug functions into AUCTeX if the user option says so.
804 (and reftex-plug-into-AUCTeX
805 (reftex-plug-into-AUCTeX))
807 (reftex-compile-variables))
809 (defun reftex-reset-scanning-information ()
810 "Reset the symbols containing information from buffer scanning.
811 This enforces rescanning the buffer on next use."
812 (if (string= reftex-last-toc-master (reftex-TeX-master-file))
813 (reftex-erase-buffer "*toc*"))
814 (let ((symlist reftex-multifile-symbols)
815 symbol)
816 (while symlist
817 (setq symbol (car symlist)
818 symlist (cdr symlist))
819 (if (and (symbolp (symbol-value symbol))
820 (not (null (symbol-value symbol))))
821 (set (symbol-value symbol) nil)))))
823 (defun reftex-erase-all-selection-and-index-buffers ()
824 ;; Remove all selection buffers associated with current document.
825 (mapcar
826 (lambda (type)
827 (reftex-erase-buffer (reftex-make-selection-buffer-name type)))
828 reftex-typekey-list)
829 ;; Kill all index buffers
830 (mapcar
831 (lambda (tag)
832 (reftex-kill-buffer (reftex-make-index-buffer-name tag)))
833 (cdr (assoc 'index-tags (symbol-value reftex-docstruct-symbol)))))
835 (defun reftex-compile-variables ()
836 ;; Compile the information in reftex-label-alist & Co.
838 (message "Compiling label environment definitions...")
840 ;; Update AUCTeX style information
841 (when (and (featurep 'tex-site) (fboundp 'TeX-update-style))
842 (condition-case nil (TeX-update-style) (error nil)))
844 ;; Record that we have done this, and what we have used.
845 (setq reftex-tables-dirty nil)
846 (setq reftex-memory
847 (list reftex-label-alist
848 (get reftex-docstruct-symbol 'reftex-section-levels)
849 (get reftex-docstruct-symbol 'reftex-label-alist-style)
850 reftex-default-label-alist-entries
851 reftex-index-macros
852 (get reftex-docstruct-symbol 'reftex-index-macros-style)))
854 ;; Compile information in reftex-label-alist
855 (let ((all (reftex-uniquify-by-car
856 (reftex-splice-symbols-into-list
857 (append reftex-label-alist
858 (get reftex-docstruct-symbol
859 'reftex-label-alist-style)
860 reftex-default-label-alist-entries)
861 reftex-label-alist-builtin)
862 '(nil)))
863 (all-index (reftex-uniquify-by-car
864 (reftex-splice-symbols-into-list
865 (append reftex-index-macros
866 (get reftex-docstruct-symbol
867 'reftex-index-macros-style)
868 '(default))
869 reftex-index-macros-builtin)))
870 entry env-or-mac typekeychar typekey prefix context word
871 fmt reffmt labelfmt wordlist qh-list macros-with-labels
872 nargs nlabel opt-args cell sum i
873 macro verify repeat nindex tag key toc-level toc-levels)
875 (setq reftex-words-to-typekey-alist nil
876 reftex-typekey-list nil
877 reftex-typekey-to-format-alist nil
878 reftex-typekey-to-prefix-alist nil
879 reftex-env-or-mac-alist nil
880 reftex-label-env-list nil
881 reftex-label-mac-list nil)
882 (while all
883 (catch 'next-entry
884 (setq entry (car all)
885 env-or-mac (car entry)
886 entry (cdr entry)
887 all (cdr all))
888 (if (null env-or-mac)
889 (setq env-or-mac ""))
890 (if (stringp (car entry))
891 ;; This is before version 2.00 - convert entry to new format
892 ;; This is just to keep old users happy
893 (setq entry (cons (string-to-char (car entry))
894 (cons (concat (car entry) ":")
895 (cdr entry)))))
896 (setq typekeychar (nth 0 entry)
897 typekey (if typekeychar (char-to-string typekeychar) nil)
898 prefix (nth 1 entry)
899 fmt (nth 2 entry)
900 context (nth 3 entry)
901 wordlist (nth 4 entry)
902 toc-level (nth 5 entry))
903 (if (stringp wordlist)
904 ;; This is before version 2.04 - convert to new format
905 (setq wordlist (nthcdr 4 entry)))
907 (if (and (stringp fmt)
908 (string-match "@" fmt))
909 ;; Special syntax for specifying a label format
910 (setq fmt (split-string fmt "@+"))
911 (setq fmt (list "\\label{%s}" fmt)))
912 (setq labelfmt (car fmt)
913 reffmt (nth 1 fmt))
914 ;; Note a new typekey
915 (if typekey
916 (add-to-list 'reftex-typekey-list typekey))
917 (if (and typekey prefix
918 (not (assoc typekey reftex-typekey-to-prefix-alist)))
919 (add-to-list 'reftex-typekey-to-prefix-alist
920 (cons typekey prefix)))
921 ;; Check if this is a macro or environment
922 (cond
923 ((symbolp env-or-mac)
924 ;; A special parser function
925 (unless (fboundp env-or-mac)
926 (message "Warning: %s does not seem to be a valid function"
927 env-or-mac))
928 (setq nargs nil nlabel nil opt-args nil)
929 (add-to-list 'reftex-special-env-parsers env-or-mac)
930 (setq env-or-mac (symbol-name env-or-mac)))
931 ((string-match "\\`\\\\" env-or-mac)
932 ;; It's a macro
933 (let ((result (reftex-parse-args env-or-mac)))
934 (setq env-or-mac (or (first result) env-or-mac)
935 nargs (second result)
936 nlabel (third result)
937 opt-args (fourth result))
938 (if nlabel (add-to-list 'macros-with-labels env-or-mac)))
939 (if typekey (add-to-list 'reftex-label-mac-list env-or-mac)))
941 ;; It's an environment
942 (setq nargs nil nlabel nil opt-args nil)
943 (cond ((string= env-or-mac "any"))
944 ((string= env-or-mac ""))
945 ((string= env-or-mac "section"))
947 (add-to-list 'reftex-label-env-list env-or-mac)
948 (if toc-level
949 (let ((string (format "begin{%s}" env-or-mac)))
950 (or (assoc string toc-levels)
951 (push (cons string toc-level) toc-levels))))))))
952 ;; Translate some special context cases
953 (when (assq context reftex-default-context-regexps)
954 (setq context
955 (format
956 (cdr (assq context reftex-default-context-regexps))
957 (regexp-quote env-or-mac))))
958 ;; See if this is the first format for this typekey
959 (and reffmt
960 (not (assoc typekey reftex-typekey-to-format-alist))
961 (push (cons typekey reffmt) reftex-typekey-to-format-alist))
962 ;; See if this is the first definition for this env-or-mac
963 (and (not (string= env-or-mac "any"))
964 (not (string= env-or-mac ""))
965 (not (assoc env-or-mac reftex-env-or-mac-alist))
966 (push (list env-or-mac typekey context labelfmt
967 nargs nlabel opt-args)
968 reftex-env-or-mac-alist))
969 ;; Are the magic words regular expressions? Quote normal words.
970 (if (eq (car wordlist) 'regexp)
971 (setq wordlist (cdr wordlist))
972 (setq wordlist (mapcar 'regexp-quote wordlist)))
973 ;; Remember the first association of each word.
974 (while (stringp (setq word (pop wordlist)))
975 (or (assoc word reftex-words-to-typekey-alist)
976 (push (cons word typekey) reftex-words-to-typekey-alist)))
977 (cond
978 ((string= "" env-or-mac) nil)
979 ((setq cell (assoc typekey qh-list))
980 (push env-or-mac (cdr cell)))
981 (typekey
982 (push (list typekey env-or-mac) qh-list)))))
984 (setq reftex-typekey-to-prefix-alist
985 (nreverse reftex-typekey-to-prefix-alist))
987 ;; Prepare the typekey query prompt and help string.
988 (setq qh-list
989 (sort qh-list
990 (lambda (x1 x2)
991 (string< (downcase (car x1)) (downcase (car x2))))))
992 (setq reftex-type-query-prompt
993 (concat "Label type: ["
994 (mapconcat (lambda(x) (format "%s" (car x)))
995 qh-list "")
996 "]"))
997 ;; In the help string, we need to wrap lines...
998 (setq reftex-type-query-help
999 (concat
1000 "SELECT A LABEL TYPE:\n--------------------\n"
1001 (mapconcat
1002 (lambda(x)
1003 (setq sum 0)
1004 (format " [%s] %s"
1005 (car x)
1006 (mapconcat (lambda(env)
1007 (setq sum (+ sum (length env)))
1008 (if (< sum 60)
1010 (setq sum 0)
1011 (concat "\n " env)))
1012 (cdr x) " ")))
1013 qh-list "\n")))
1015 ;; Convert magic words to regular expressions. We make regular expressions
1016 ;; which allow for some chars from the ref format to be in the buffer.
1017 ;; These characters will be seen and removed.
1018 (setq reftex-words-to-typekey-alist
1019 (mapcar
1020 (lambda (x)
1021 (setq word (car x)
1022 typekey (cdr x)
1023 fmt (cdr (assoc typekey reftex-typekey-to-format-alist)))
1024 (setq word (concat "\\W\\(" word "[ \t\n\r]*\\)\\("))
1025 (setq i 0)
1026 (while (and (< i 10) ; maximum number of format chars allowed
1027 (< i (length fmt))
1028 (not (member (aref fmt i) '(?%))))
1029 (setq word (concat word "\\|" (regexp-quote
1030 (substring fmt 0 (1+ i)))))
1031 (incf i))
1032 (cons (concat word "\\)\\=") typekey))
1033 (nreverse reftex-words-to-typekey-alist)))
1035 ;; Parse the index macros
1036 (setq reftex-index-macro-alist nil
1037 reftex-key-to-index-macro-alist nil
1038 reftex-macros-with-index nil)
1039 (while all-index
1040 (setq entry (car all-index)
1041 macro (car entry)
1042 tag (nth 1 entry)
1043 key (nth 2 entry)
1044 prefix (or (nth 3 entry) "")
1045 verify (nth 4 entry)
1046 ;; For repeat, we need to be compatible with older code
1047 ;; This information used to be given only for the default macro,
1048 ;; but later we required to have it for *every* index macro
1049 repeat (cond ((> (length entry) 5) (nth 5 entry))
1050 ((and (eq key (car reftex-index-default-macro))
1051 (> (length reftex-index-default-macro) 2))
1052 ;; User has old setting - respect it
1053 (nth 2 reftex-index-default-macro))
1054 (t t))
1055 all-index (cdr all-index))
1056 (let ((result (reftex-parse-args macro)))
1057 (setq macro (or (first result) macro)
1058 nargs (second result)
1059 nindex (third result)
1060 opt-args (fourth result))
1061 (unless (member macro reftex-macros-with-index)
1062 ;; 0 1 2 3 4 5 6 7
1063 (push (list macro tag prefix verify nargs nindex opt-args repeat)
1064 reftex-index-macro-alist)
1065 (or (assoc key reftex-key-to-index-macro-alist)
1066 (push (list key macro) reftex-key-to-index-macro-alist))
1067 (push macro reftex-macros-with-index))))
1068 ;; Make the prompt and help string for index macros query
1069 (setq reftex-key-to-index-macro-alist
1070 (sort reftex-key-to-index-macro-alist
1071 (lambda (a b) (< (downcase (car a)) (downcase (car b))))))
1072 (setq reftex-query-index-macro-prompt
1073 (concat "Index macro: ["
1074 (mapconcat (lambda (x) (char-to-string (car x)))
1075 reftex-key-to-index-macro-alist "")
1076 "]"))
1077 (setq i 0
1078 reftex-query-index-macro-help
1079 (concat
1080 "SELECT A MACRO:\n---------------\n"
1081 (mapconcat
1082 (lambda(x)
1083 (format "[%c] %-20.20s%s" (car x) (nth 1 x)
1084 (if (= 0 (mod (incf i) 3)) "\n" "")))
1085 reftex-key-to-index-macro-alist "")))
1087 ;; Make the full list of section levels
1088 (setq reftex-section-levels-all
1089 (append toc-levels
1090 (get reftex-docstruct-symbol 'reftex-section-levels)
1091 reftex-section-levels))
1093 ;; Calculate the regular expressions
1094 (let* ((wbol "\\(\\`\\|[\n\r]\\)[ \t]*")
1095 (label-re "\\\\label{\\([^}]*\\)}")
1096 (include-re (concat wbol "\\\\\\(include\\|input\\)[{ \t]+\\([^} \t\n\r]+\\)"))
1097 (section-re
1098 (concat wbol "\\\\\\("
1099 (mapconcat (lambda (x) (regexp-quote (car x)))
1100 reftex-section-levels-all "\\|")
1101 "\\)\\*?\\(\\[[^]]*\\]\\)?[[{ \t\r\n]"))
1102 (appendix-re (concat wbol "\\(\\\\appendix\\)"))
1103 (macro-re
1104 (if macros-with-labels
1105 (concat "\\("
1106 (mapconcat 'regexp-quote macros-with-labels "\\|")
1107 "\\)[[{]")
1108 ""))
1109 (index-re
1110 (concat "\\("
1111 (mapconcat 'regexp-quote reftex-macros-with-index "\\|")
1112 "\\)[[{]"))
1113 (find-index-re-format
1114 (concat "\\("
1115 (mapconcat 'regexp-quote reftex-macros-with-index "\\|")
1116 "\\)\\([[{][^]}]*[]}]\\)*[[{]\\(%s\\)[]}]"))
1117 (find-label-re-format
1118 (concat "\\("
1119 (mapconcat 'regexp-quote (append '("\\label")
1120 macros-with-labels) "\\|")
1121 "\\)\\([[{][^]}]*[]}]\\)*[[{]\\(%s\\)[]}]"))
1122 (index-level-re
1123 (regexp-quote (nth 0 reftex-index-special-chars)))
1124 (index-key-end-re ;; ^]- not allowed
1125 (concat "[^" (nth 3 reftex-index-special-chars) "]"
1126 "[" (nth 1 reftex-index-special-chars)
1127 (nth 2 reftex-index-special-chars) "]"))
1129 (setq reftex-section-regexp section-re
1130 reftex-section-or-include-regexp
1131 (concat section-re "\\|" include-re)
1132 reftex-everything-regexp
1133 (concat label-re "\\|" section-re "\\|" include-re
1134 "\\|" appendix-re
1135 "\\|" index-re
1136 (if macros-with-labels "\\|" "") macro-re)
1137 reftex-everything-regexp-no-index
1138 (concat label-re "\\|" section-re "\\|" include-re
1139 "\\|" appendix-re
1140 "\\|" "\\(\\\\6\\\\3\\\\1\\)" ; This is unlikely to match
1141 (if macros-with-labels "\\|" "") macro-re)
1142 reftex-index-re index-re
1143 reftex-index-level-re index-level-re
1144 reftex-index-key-end-re index-key-end-re
1145 reftex-macros-with-labels macros-with-labels
1146 reftex-find-index-entry-regexp-format find-index-re-format
1147 reftex-find-label-regexp-format find-label-re-format
1148 reftex-find-label-regexp-format2
1149 "\\([]} \t\n\r]\\)\\([[{]\\)\\(%s\\)[]}]")
1150 (message "Compiling label environment definitions...done")))
1151 (put reftex-docstruct-symbol 'reftex-cache
1152 (mapcar 'symbol-value reftex-cache-variables)))
1154 (defun reftex-parse-args (macro)
1155 ;; Return a list of macro name, nargs, arg-nr which is label and a list of
1156 ;; optional argument indices.
1157 (if (string-match "[[{]\\*?[]}]" macro)
1158 (progn
1159 (let ((must-match (substring macro 0 (match-beginning 0)))
1160 (args (substring macro (match-beginning 0)))
1161 opt-list nlabel (cnt 0))
1162 (while (string-match "\\`[[{]\\(\\*\\)?[]}]" args)
1163 (incf cnt)
1164 (when (eq ?\[ (string-to-char args))
1165 (push cnt opt-list))
1166 (when (and (match-end 1)
1167 (not nlabel))
1168 (setq nlabel cnt))
1169 (setq args (substring args (match-end 0))))
1170 (list must-match cnt nlabel opt-list)))
1171 nil))
1173 ;;; =========================================================================
1175 ;;; Accessing the parse information
1177 (defun reftex-access-scan-info (&optional rescan file)
1178 "Ensure access to the scanning info for the current file."
1179 ;; When the multifile symbols are not yet tied,
1180 ;; tie them. When they are empty or RESCAN is non-nil, scan the document.
1181 ;; But, when RESCAN is -1, don't rescan even if docstruct is empty.
1182 ;; When FILE is non-nil, parse only from that file.
1184 ;; Make sure we have the symbols tied
1185 (if (eq reftex-docstruct-symbol nil)
1186 ;; Symbols are not yet tied: Tie them.
1187 (reftex-tie-multifile-symbols))
1189 (reftex-ensure-compiled-variables)
1191 (when (or (null (symbol-value reftex-docstruct-symbol))
1192 (member rescan '(t 1 (4) (16))))
1193 ;; The docstruct will change: Remove selection buffers.
1194 (save-excursion
1195 (reftex-erase-buffer "*toc*")
1196 (reftex-erase-all-selection-and-index-buffers)))
1198 (if (and (null (symbol-value reftex-docstruct-symbol))
1199 (not (member rescan '(t 1 (4) (16))))
1200 reftex-save-parse-info)
1201 ;; Try to read the stuff from a file
1202 (reftex-access-parse-file 'read))
1204 (cond
1205 ((equal rescan -1)) ;; We are not allowed to scan.
1206 ((not (symbol-value reftex-docstruct-symbol))
1207 ;; Scan the whole document
1208 (reftex-do-parse 1 file))
1209 ((member rescan '(t 1 (4) (16)))
1210 ;; Scan whatever was required by the caller.
1211 (reftex-do-parse rescan file))))
1213 (defun reftex-scanning-info-available-p ()
1214 "Is the scanning info about the current document available?"
1215 (unless reftex-docstruct-symbol
1216 (reftex-tie-multifile-symbols))
1217 (and (symbolp reftex-docstruct-symbol)
1218 (symbol-value reftex-docstruct-symbol)
1221 (defun reftex-silence-toc-markers (list n)
1222 ;; Set all toc markers in the first N entries in list to nil
1223 (while (and list (> (decf n) -1))
1224 (and (eq (car (car list)) 'toc)
1225 (markerp (nth 4 (car list)))
1226 (set-marker (nth 4 (car list)) nil))
1227 (pop list)))
1229 (defun reftex-access-parse-file (action)
1230 "Perform ACTION on the parse file (the .rel file).
1231 Valid actions are: readable, restore, read, kill, write."
1232 (let* ((list (symbol-value reftex-docstruct-symbol))
1233 (docstruct-symbol reftex-docstruct-symbol)
1234 (master (reftex-TeX-master-file))
1235 (enable-local-variables nil)
1236 (file (if (string-match "\\.[a-zA-Z]+\\'" master)
1237 (concat (substring master 0 (match-beginning 0))
1238 reftex-parse-file-extension)
1239 (concat master reftex-parse-file-extension))))
1240 (cond
1241 ((eq action 'readable)
1242 (file-readable-p file))
1243 ((eq action 'restore)
1244 (put reftex-docstruct-symbol 'modified nil)
1245 (if (eq reftex-docstruct-symbol nil)
1246 ;; Symbols are not yet tied: Tie them.
1247 (reftex-tie-multifile-symbols))
1248 (if (file-exists-p file)
1249 ;; load the file and return t for success
1250 (condition-case nil
1251 (progn (load-file file) t)
1252 (error (set reftex-docstruct-symbol nil)
1253 (error "Error while loading file %s" file)))
1254 ;; Throw an exception if the file does not exist
1255 (error "No restore file %s" file)))
1256 ((eq action 'read)
1257 (put reftex-docstruct-symbol 'modified nil)
1258 (if (file-exists-p file)
1259 ;; load the file and return t for success
1260 (condition-case nil
1261 (progn
1262 (load-file file)
1263 (reftex-check-parse-consistency)
1265 (error (message "Error while restoring file %s" file)
1266 (set reftex-docstruct-symbol nil)
1267 nil))
1268 ;; return nil for failure, but no exception
1269 nil))
1270 ((eq action 'kill)
1271 ;; Remove the file
1272 (when (and (file-exists-p file) (file-writable-p file))
1273 (message "Unlinking file %s" file)
1274 (delete-file file)))
1276 (put docstruct-symbol 'modified nil)
1277 (save-excursion
1278 (if (file-writable-p file)
1279 (progn
1280 (message "Writing parse file %s" (abbreviate-file-name file))
1281 (find-file file)
1282 (erase-buffer)
1283 (insert (format ";; RefTeX parse info file\n"))
1284 (insert (format ";; File: %s\n" master))
1285 (insert (format ";; User: %s (%s)\n\n"
1286 (user-login-name) (user-full-name)))
1287 (insert "(set reftex-docstruct-symbol '(\n\n")
1288 (let ((standard-output (current-buffer)))
1289 (mapcar
1290 (lambda (x)
1291 (cond ((eq (car x) 'toc)
1292 ;; A toc entry. Do not save the marker.
1293 ;; Save the markers position at position 8
1294 (print (list 'toc "toc" (nth 2 x) (nth 3 x)
1295 nil (nth 5 x) (nth 6 x) (nth 7 x)
1296 (or (and (markerp (nth 4 x))
1297 (marker-position (nth 4 x)))
1298 (nth 8 x)))))
1299 ((and (not (eq t reftex-support-index))
1300 (eq (car x) 'index))
1301 ;; Don't save index entries
1303 (t (print x))))
1304 list))
1305 (insert "))\n\n")
1306 (save-buffer 0)
1307 (kill-buffer (current-buffer)))
1308 (error "Cannot write to file %s" file)))
1309 t))))
1311 (defun reftex-check-parse-consistency ()
1312 ;; Check if parse file is consistent, throw an error if not.
1314 ;; Check if the master is the same: when moving a document, this will see it.
1315 (let* ((real-master (reftex-TeX-master-file))
1316 (parsed-master
1317 (nth 1 (assq 'bof (symbol-value reftex-docstruct-symbol)))))
1318 (unless (string= (file-truename real-master) (file-truename parsed-master))
1319 (message "Master file name in load file is different: %s versus %s"
1320 parsed-master real-master)
1321 (error "Master file name error")))
1323 ;; Check for the existence of all document files
1324 ;;; (let* ((all (symbol-value reftex-docstruct-symbol)))
1325 ;;; (while all
1326 ;;; (when (and (eq (car (car all)) 'bof)
1327 ;;; (not (file-regular-p (nth 1 (car all)))))
1328 ;;; (message "File %s in saved parse info not avalable" (cdr (car all)))
1329 ;;; (error "File not found"))
1330 ;;; (setq all (cdr all))))
1333 (defun reftex-select-external-document (xr-alist xr-index)
1334 ;; Return index of an external document.
1335 (let* ((len (length xr-alist)) (highest (1- (+ ?0 len)))
1336 (prompt (format "[%c-%c] Select TAB: Read prefix with completion"
1337 ?0 highest))
1338 key prefix)
1339 (cond
1340 ((= len 1)
1341 (message "No external documents available")
1342 (ding) (sit-for 1) 0)
1343 ((= len 2)
1344 (- 1 xr-index))
1346 (save-excursion
1347 (let* ((length (apply 'max (mapcar
1348 (lambda(x) (length (car x))) xr-alist)))
1349 (fmt (format " [%%c] %%-%ds %%s\n" length))
1350 (n (1- ?0)))
1351 (setq key
1352 (reftex-select-with-char
1353 prompt
1354 (concat
1355 "SELECT EXTERNAL DOCUMENT\n------------------------\n"
1356 (mapconcat
1357 (lambda (x)
1358 (format fmt (incf n) (or (car x) "")
1359 (abbreviate-file-name (cdr x))))
1360 xr-alist ""))
1361 nil t))
1362 (cond
1363 ((and (>= key ?0) (<= key highest)) (- key ?0))
1364 ((= key ?\C-i)
1365 (setq prefix (completing-read "Prefix: " xr-alist nil t))
1366 (- len (length (memq (assoc prefix xr-alist) xr-alist))))
1367 (t (error "Illegal document selection [%c]" key)))))))))
1369 ;;; =========================================================================
1371 ;;; Finding files
1373 (defun reftex-locate-file (file type master-dir &optional die)
1374 "Find FILE of type TYPE in MASTER-DIR or on the path associcted with TYPE.
1375 If the file does not have any of the legal extensions for TYPE,
1376 try first the default extension and only then the naked file name.
1377 When DIE is non-nil, throw an error if file not found."
1378 (let* ((rec-values (if reftex-search-unrecursed-path-first '(nil t) '(t)))
1379 (extensions (cdr (assoc type reftex-file-extensions)))
1380 (def-ext (car extensions))
1381 (ext-re (concat "\\("
1382 (mapconcat 'regexp-quote extensions "\\|")
1383 "\\)\\'"))
1384 (files (if (string-match ext-re file)
1385 (cons file nil)
1386 (cons (concat file def-ext) file)))
1387 path old-path file1)
1388 (cond
1389 ((file-name-absolute-p file)
1390 (setq file1
1391 (or
1392 (and (car files) (file-regular-p (car files)) (car files))
1393 (and (cdr files) (file-regular-p (cdr files)) (cdr files)))))
1394 ((and reftex-use-external-file-finders
1395 (assoc type reftex-external-file-finders))
1396 (setq file1 (reftex-find-file-externally file type master-dir)))
1398 (while (and (null file1) rec-values)
1399 (setq path (reftex-access-search-path
1400 type (pop rec-values) master-dir file))
1401 (if (or (null old-path)
1402 (not (eq old-path path)))
1403 (setq old-path path
1404 path (cons master-dir path)
1405 file1 (or (and (car files)
1406 (reftex-find-file-on-path
1407 (car files) path master-dir))
1408 (and (cdr files)
1409 (reftex-find-file-on-path
1410 (cdr files) path master-dir))))))))
1411 (cond (file1 file1)
1412 (die (error "No such file: %s" file) nil)
1413 (t (message "No such file: %s (ignored)" file) nil))))
1415 (defun reftex-find-file-externally (file type &optional master-dir)
1416 ;; Use external program to find FILE.
1417 ;; The program is taken from `reftex-external-file-finders'.
1418 ;; Interprete relative path definitions starting from MASTER-DIR.
1419 (let ((default-directory (or master-dir default-directory))
1420 (prg (cdr (assoc type reftex-external-file-finders)))
1421 out)
1422 (if (string-match "%f" prg)
1423 (setq prg (replace-match file t t prg)))
1424 (setq out (apply 'reftex-process-string (split-string prg)))
1425 (if (string-match "[ \t\n]+\\'" out) ; chomp
1426 (setq out (replace-match "" nil nil out)))
1427 (cond ((equal out "") nil)
1428 ((file-regular-p out) (expand-file-name out master-dir))
1429 (t nil))))
1431 (defun reftex-process-string (program &rest args)
1432 "Execute PROGRAM with arguments ARGS and return its STDOUT as a string."
1433 (let ((calling-dir default-directory)) ; remember default directory
1434 (with-output-to-string
1435 (with-current-buffer standard-output
1436 (let ((default-directory calling-dir)) ; set default directory
1437 (apply 'call-process program nil '(t nil) nil args))))))
1439 (defun reftex-access-search-path (type &optional recurse master-dir file)
1440 ;; Access path from environment variables. TYPE is either "tex" or "bib".
1441 ;; When RECURSE is t, expand path elements ending in `//' recursively.
1442 ;; Relative path elements are left as they are. However, relative recursive
1443 ;; elements are expanded with MASTER-DIR as default directory.
1444 ;; The expanded path is cached for the next search.
1445 ;; FILE is just for the progress message.
1446 ;; Returns the derived path.
1447 (let* ((pathvar (intern (concat "reftex-" type "-path"))))
1448 (when (null (get pathvar 'status))
1449 ;; Get basic path
1450 (set pathvar
1451 (reftex-uniq
1452 (reftex-parse-colon-path
1453 (mapconcat
1454 (lambda(x)
1455 (if (string-match "^!" x)
1456 (apply 'reftex-process-string
1457 (split-string (substring x 1)))
1458 (or (getenv x) x)))
1459 ;; For consistency, the next line should look like this:
1460 ;; (cdr (assoc type reftex-path-environment))
1461 ;; However, historically we have separate options for the
1462 ;; environment variables, so we have to do this:
1463 (symbol-value (intern (concat "reftex-" type
1464 "path-environment-variables")))
1465 path-separator))))
1466 (put pathvar 'status 'split)
1467 ;; Check if we have recursive elements
1468 (let ((path (symbol-value pathvar)) dir rec)
1469 (while (setq dir (pop path))
1470 (when (string= (substring dir -2) "//")
1471 (if (file-name-absolute-p dir)
1472 (setq rec (or rec 'absolute))
1473 (setq rec 'relative))))
1474 (put pathvar 'rec-type rec)))
1476 (if recurse
1477 ;; Return the recursive expansion of the path
1478 (cond
1479 ((not (get pathvar 'rec-type))
1480 ;; Path does not contain recursive elements - use simple path
1481 (symbol-value pathvar))
1482 ((or (not (get pathvar 'recursive-path))
1483 (and (eq (get pathvar 'rec-type) 'relative)
1484 (not (equal master-dir (get pathvar 'master-dir)))))
1485 ;; Either: We don't have a recursive expansion yet.
1486 ;; or: Relative recursive path elements need to be expanded
1487 ;; relative to new default directory
1488 (message "Expanding search path to find %s file: %s ..." type file)
1489 (put pathvar 'recursive-path
1490 (reftex-expand-path (symbol-value pathvar) master-dir))
1491 (put pathvar 'master-dir master-dir)
1492 (get pathvar 'recursive-path))
1494 ;; Recursive path computed earlier is still OK.
1495 (get pathvar 'recursive-path)))
1496 ;; The simple path was requested
1497 (symbol-value pathvar))))
1499 (defun reftex-find-file-on-path (file path &optional def-dir)
1500 ;; Find FILE along the directory list PATH.
1501 ;; DEF-DIR is the default directory for expanding relative path elements.
1502 (catch 'exit
1503 (when (file-name-absolute-p file)
1504 (if (file-regular-p file)
1505 (throw 'exit file)
1506 (throw 'exit nil)))
1507 (let* ((thepath path) file1 dir)
1508 (while (setq dir (pop thepath))
1509 (when (string= (substring dir -2) "//")
1510 (setq dir (substring dir 0 -1)))
1511 (setq file1 (expand-file-name file (expand-file-name dir def-dir)))
1512 (if (file-regular-p file1)
1513 (throw 'exit file1)))
1514 ;; No such file
1515 nil)))
1517 (defun reftex-parse-colon-path (path)
1518 ;; Like parse-colon-parse, but // or /~ are left alone.
1519 ;; Trailing ! or !! will be converted into `//' (emTeX convention)
1520 (mapcar
1521 (lambda (dir)
1522 (if (string-match "\\(//+\\|/*!+\\)\\'" dir)
1523 (setq dir (replace-match "//" t t dir)))
1524 (file-name-as-directory dir))
1525 (delete "" (split-string path (concat path-separator "+")))))
1527 (defun reftex-expand-path (path &optional default-dir)
1528 ;; Expand parts of path ending in `//' recursively into directory list.
1529 ;; Relative recursive path elements are expanded relative to DEFAULT-DIR.
1530 (let (path1 dir recursive)
1531 (while (setq dir (pop path))
1532 (if (setq recursive (string= (substring dir -2) "//"))
1533 (setq dir (substring dir 0 -1)))
1534 (if (and recursive
1535 (not (file-name-absolute-p dir)))
1536 (setq dir (expand-file-name dir default-dir)))
1537 (if recursive
1538 ;; Expand recursively
1539 (setq path1 (append (reftex-recursive-directory-list dir) path1))
1540 ;; Keep unchanged
1541 (push dir path1)))
1542 (nreverse path1)))
1544 (defun reftex-recursive-directory-list (dir)
1545 ;; Return a list of all directories below DIR, including DIR itself
1546 (let ((path (list dir)) path1 file files)
1547 (while (setq dir (pop path))
1548 (when (file-directory-p dir)
1549 (setq files (nreverse (directory-files dir t "[^.]")))
1550 (while (setq file (pop files))
1551 (if (file-directory-p file)
1552 (push (file-name-as-directory file) path)))
1553 (push dir path1)))
1554 path1))
1556 (defun reftex-uniq (list)
1557 (let (new)
1558 (while list
1559 (or (member (car list) new)
1560 (push (car list) new))
1561 (pop list))
1562 (nreverse new)))
1565 ;;; =========================================================================
1567 ;;; Parser functions
1569 (autoload 'reftex-parse-one "reftex-parse"
1570 "Re-parse this file." t)
1571 (autoload 'reftex-parse-all "reftex-parse"
1572 "Re-parse entire document." t)
1573 (autoload 'reftex-do-parse "reftex-parse")
1574 (autoload 'reftex-where-am-I "reftex-parse")
1575 (autoload 'reftex-init-section-numbers "reftex-parse")
1576 (autoload 'reftex-section-info "reftex-parse")
1577 (autoload 'reftex-section-number "reftex-parse")
1578 (autoload 'reftex-what-macro "reftex-parse")
1579 (autoload 'reftex-what-macro-safe "reftex-parse")
1580 (autoload 'reftex-index-info "reftex-parse")
1581 (autoload 'reftex-index-info-safe "reftex-parse")
1582 (autoload 'reftex-short-context "reftex-parse")
1583 (autoload 'reftex-what-environment "reftex-parse")
1584 (autoload 'reftex-what-special-env "reftex-parse")
1585 (autoload 'reftex-move-over-touching-args "reftex-parse")
1586 (autoload 'reftex-notice-new "reftex-parse")
1587 (autoload 'reftex-nth-arg "reftex-parse")
1588 (autoload 'reftex-locate-bibliography-files "reftex-parse")
1589 (autoload 'reftex-all-document-files "reftex-parse")
1590 (autoload 'reftex-ensure-index-support "reftex-parse")
1591 (autoload 'reftex-everything-regexp "reftex-parse")
1594 ;;; =========================================================================
1596 ;;; Labels and References
1598 (autoload 'reftex-label-location "reftex-ref")
1599 (autoload 'reftex-label-info-update "reftex-ref")
1600 (autoload 'reftex-label-info "reftex-ref")
1601 (autoload 'reftex-label "reftex-ref"
1602 "Insert a unique label." t)
1603 (autoload 'reftex-reference "reftex-ref"
1604 "Make a LaTeX reference." t)
1605 (autoload 'reftex-show-label-location "reftex-ref")
1606 (autoload 'reftex-query-label-type "reftex-ref")
1609 ;;; =========================================================================
1611 ;;; Table of contents
1613 (autoload 'reftex-toc "reftex-toc"
1614 "Show the table of contents for the current document." t)
1617 ;;; =========================================================================
1619 ;;; BibTeX citations.
1621 (autoload 'reftex-citep "reftex-cite")
1622 (autoload 'reftex-citet "reftex-cite")
1623 (autoload 'reftex-make-cite-echo-string "reftex-cite")
1624 (autoload 'reftex-get-bibfile-list "reftex-cite")
1625 (autoload 'reftex-pop-to-bibtex-entry "reftex-cite")
1626 (autoload 'reftex-end-of-bib-entry "reftex-cite")
1627 (autoload 'reftex-parse-bibtex-entry "reftex-cite")
1628 (autoload 'reftex-citation "reftex-cite"
1629 "Make a citation using BibTeX database files." t)
1630 (autoload 'reftex-default-bibliography "reftex-cite")
1633 ;;; =========================================================================
1635 ;;; Selection
1637 (autoload 'reftex-select-label-mode "reftex-sel")
1638 (autoload 'reftex-select-bib-mode "reftex-sel")
1639 (autoload 'reftex-find-start-point "reftex-sel")
1640 (autoload 'reftex-insert-docstruct "reftex-sel")
1641 (autoload 'reftex-get-offset "reftex-sel")
1642 (autoload 'reftex-select-item "reftex-sel")
1645 ;;; =========================================================================
1647 ;;; Index support
1649 (autoload 'reftex-index "reftex-index"
1650 "Query for an index macro and insert it along with its argments." t)
1651 (autoload 'reftex-index-selection-or-word "reftex-index"
1652 "Put selection or the word near point into the default index macro." t)
1653 (autoload 'reftex-index-phrase-selection-or-word "reftex-index"
1654 "Put selection or the word near point into Index Phrases File." t)
1655 (autoload 'reftex-display-index "reftex-index"
1656 "Display a buffer with an index compiled from the current document." t)
1657 (autoload 'reftex-index-visit-phrases-buffer "reftex-index"
1658 "Visit the Index Phrases File." t)
1659 (autoload 'reftex-index-phrases-mode "reftex-index"
1660 "Major mode for managing the Index phrases of a LaTeX document." t)
1661 (autoload 'reftex-index-complete-tag "reftex-index")
1662 (autoload 'reftex-index-complete-key "reftex-index")
1663 (autoload 'reftex-index-show-entry "reftex-index")
1664 (autoload 'reftex-index-select-tag "reftex-index")
1667 ;;; =========================================================================
1669 ;;; View cross references
1671 (autoload 'reftex-view-crossref "reftex-dcr"
1672 "View cross reference of \\ref or \\cite macro at point." t)
1673 (autoload 'reftex-mouse-view-crossref "reftex-dcr"
1674 "View cross reference of \\ref or \\cite macro where you click." t)
1675 (autoload 'reftex-toggle-auto-view-crossref "reftex-dcr")
1676 (autoload 'reftex-view-crossref-from-bibtex "reftex-dcr"
1677 "View location in a LaTeX document which cites the BibTeX entry at point." t)
1680 ;;; =========================================================================
1682 ;;; Operations on entire Multifile documents
1684 (autoload 'reftex-create-tags-file "reftex-global"
1685 "Create TAGS file by running `etags' on the current document." t)
1686 (autoload 'reftex-grep-document "reftex-global"
1687 "Run grep query through all files related to this document." t)
1688 (autoload 'reftex-search-document "reftex-global"
1689 "Regexp search through all files of the current TeX document." t)
1690 (autoload 'reftex-query-replace-document "reftex-global"
1691 "Run a query-replace-regexp of FROM with TO over the entire TeX document." t)
1692 (autoload 'reftex-find-duplicate-labels "reftex-global"
1693 "Produce a list of all duplicate labels in the document." t)
1694 (autoload 'reftex-change-label "reftex-global"
1695 "Query replace FROM with TO in all \\label and \\ref commands." t)
1696 (autoload 'reftex-renumber-simple-labels "reftex-global"
1697 "Renumber all simple labels in the document to make them sequentially." t)
1698 (autoload 'reftex-save-all-document-buffers "reftex-global"
1699 "Save all documents associated with the current document." t)
1702 ;;; =========================================================================
1704 ;;; AUCTeX Interface
1706 (autoload 'reftex-arg-label "reftex-auc")
1707 (autoload 'reftex-arg-cite "reftex-auc")
1708 (autoload 'reftex-arg-index-tag "reftex-auc")
1709 (autoload 'reftex-arg-index "reftex-auc")
1710 (autoload 'reftex-plug-into-AUCTeX "reftex-auc")
1711 (autoload 'reftex-toggle-plug-into-AUCTeX "reftex-auc"
1712 "Toggle Interface between AUCTeX and RefTeX on and off." t)
1713 (autoload 'reftex-add-label-environments "reftex-auc")
1714 (autoload 'reftex-add-to-label-alist "reftex-auc")
1715 (autoload 'reftex-add-section-levels "reftex-auc")
1716 (autoload 'reftex-notice-new-section "reftex-auc")
1718 ;;; =========================================================================
1720 ;;; Some generally useful functions
1722 (defun reftex-typekey-check (typekey conf-variable &optional n)
1723 ;; Check if CONF-VARIABLE is true or contains TYPEKEY
1724 (and n (setq conf-variable (nth n conf-variable)))
1725 (or (eq conf-variable t)
1726 (and (stringp conf-variable)
1727 (string-match (concat "[" conf-variable "]") typekey))))
1729 (defun reftex-check-recursive-edit ()
1730 ;; Check if we are already in a recursive edit. Abort with helpful
1731 ;; message if so.
1732 (if (marker-position reftex-recursive-edit-marker)
1733 (error
1734 (substitute-command-keys
1735 "In unfinished selection process. Finish, or abort with \\[abort-recursive-edit]"))))
1737 (defun reftex-in-comment ()
1738 (save-excursion
1739 (skip-chars-backward "^%\n\r")
1740 (eq (preceding-char) ?%)))
1742 (defun reftex-no-props (string)
1743 ;; Return STRING with all text properties removed
1744 (and (stringp string)
1745 (set-text-properties 0 (length string) nil string))
1746 string)
1748 (defun reftex-match-string (n)
1749 ;; Match string without properties
1750 (when (match-beginning n)
1751 (buffer-substring-no-properties (match-beginning n) (match-end n))))
1753 (defun reftex-kill-buffer (buffer)
1754 ;; Kill buffer if it exists.
1755 (and (setq buffer (get-buffer buffer))
1756 (kill-buffer buffer)))
1758 (defun reftex-erase-buffer (&optional buffer)
1759 ;; Erase BUFFER if it exists. BUFFER defaults to current buffer.
1760 ;; This even erases read-only buffers.
1761 (cond
1762 ((null buffer)
1763 ;; erase current buffer
1764 (let ((buffer-read-only nil)) (erase-buffer)))
1765 ((setq buffer (get-buffer buffer))
1766 ;; buffer exists
1767 (save-excursion
1768 (set-buffer buffer)
1769 (let ((buffer-read-only nil)) (erase-buffer))))))
1771 (defun reftex-this-word (&optional class)
1772 ;; Grab the word around point.
1773 (setq class (or class "-a-zA-Z0-9:_/.*;|"))
1774 (save-excursion
1775 (buffer-substring-no-properties
1776 (progn (skip-chars-backward class) (point))
1777 (progn (skip-chars-forward class) (point)))))
1779 (defun reftex-number (n unit &optional ending)
1780 (if (and (integerp n) (stringp unit))
1781 (format "%d %s%s" n unit (if (= n 1) "" (or ending "s")))
1782 ""))
1784 (defun reftex-all-assq (key list)
1785 ;; Return a list of all associations of KEY in LIST. Comparison with eq.
1786 (let (rtn)
1787 (while (setq list (memq (assq key list) list))
1788 (push (car list) rtn)
1789 (pop list))
1790 (nreverse rtn)))
1792 (defun reftex-all-assoc-string (key list)
1793 ;; Return a list of all associations of KEY in LIST. Comparison with string=.
1794 (let (rtn)
1795 (while list
1796 (if (string= (car (car list)) key)
1797 (push (car list) rtn))
1798 (pop list))
1799 (nreverse rtn)))
1801 (defun reftex-last-assoc-before-elt (key elt list &optional exclusive)
1802 ;; Find the last association of KEY in LIST before or at ELT
1803 ;; ELT is found in LIST with equal, not eq.
1804 ;; Returns nil when either KEY or elt are not found in LIST.
1805 ;; When EXCLUSIVE is non-nil, ELT cannot be the return value.
1806 ;; On success, returns the association.
1807 (let* ((elt (car (member elt list))) (ex (not exclusive)) ass last-ass)
1808 (while (and (setq ass (assoc key list))
1809 (setq list (memq ass list))
1810 (or ex (not (eq elt (car list))))
1811 (memq elt list))
1812 (setq last-ass ass
1813 list (cdr list)))
1814 last-ass))
1816 (defun reftex-sublist-nth (list nth predicate &optional completion)
1817 ;; Make a list of the NTH elements of all members of LIST which
1818 ;; fulfill PREDICATE.
1819 ;; When COMPLETION is non-nil, make all elements of the resulting
1820 ;; list also a list, so that the result can be used for completion.
1821 (let (rtn)
1822 (while list
1823 (if (funcall predicate (car list))
1824 (push (if completion
1825 (list (nth nth (car list)))
1826 (nth nth (car list)))
1827 rtn))
1828 (setq list (cdr list)))
1829 (nreverse rtn)))
1831 (defun reftex-make-selection-buffer-name (type &optional index)
1832 ;; Make unique name for a selection buffer.
1833 (format " *RefTeX[%s][%d]*"
1834 type (or index (get reftex-docstruct-symbol :master-index) 0)))
1836 (defun reftex-make-index-buffer-name (tag &optional cnt)
1837 ;; Make unique name for an index buffer.
1838 (format "*Index[%s][%d]*"
1839 tag (or cnt (get reftex-docstruct-symbol :master-index) 0)))
1841 (defun reftex-truncate (string ncols &optional ellipses padding)
1842 ;; Truncate STRING to NCOLS characters.
1843 ;; When PADDING is non-nil, and string is shorter than NCOLS, fill with
1844 ;; white space to NCOLS characters. When ELLIPSES is non-nil and the
1845 ;; string needs to be truncated, replace last 3 characters by dots.
1846 (setq string
1847 (if (<= (length string) ncols)
1848 string
1849 (if ellipses
1850 (concat (substring string 0 (- ncols 3)) "...")
1851 (substring string 0 ncols))))
1852 (if padding
1853 (format (format "%%-%ds" ncols) string)
1854 string))
1856 (defun reftex-nearest-match (regexp &optional max-length)
1857 ;; Find the nearest match of REGEXP. Set the match data.
1858 ;; If POS is given, calculate distances relative to it.
1859 ;; Return nil if there is no match.
1860 (let ((pos (point))
1861 (dist (or max-length (length regexp)))
1862 match1 match2 match)
1863 (goto-char (min (+ pos dist) (point-max)))
1864 (when (re-search-backward regexp nil t)
1865 (setq match1 (match-data)))
1866 (goto-char (max (- pos dist) (point-min)))
1867 (when (re-search-forward regexp nil t)
1868 (setq match2 (match-data)))
1869 (goto-char pos)
1870 (setq match
1871 (cond
1872 ((not match1) match2)
1873 ((not match2) match1)
1874 ((< (abs (- pos (car match1))) (abs (- pos (car match2)))) match1)
1875 (t match2)))
1876 (if match (progn (set-match-data match) t) nil)))
1878 (defun reftex-auto-mode-alist ()
1879 ;; Return an `auto-mode-alist' with only the .gz (etc) thingies.
1880 ;; Stolen from gnus nnheader.
1881 (let ((alist auto-mode-alist)
1882 out)
1883 (while alist
1884 (when (listp (cdr (car alist)))
1885 (push (car alist) out))
1886 (pop alist))
1887 (nreverse out)))
1889 (defun reftex-window-height ()
1890 (if (fboundp 'window-displayed-height)
1891 (window-displayed-height)
1892 (window-height)))
1894 (defun reftex-enlarge-to-fit (buf2 &optional keep-current)
1895 ;; Enlarge other window displaying buffer to show whole buffer if possible.
1896 ;; If KEEP-CURRENT in non-nil, current buffer must remain visible.
1897 (let* ((win1 (selected-window))
1898 (buf1 (current-buffer))
1899 (win2 (get-buffer-window buf2))) ;; Only on current frame.
1900 (when win2
1901 (select-window win2)
1902 (unless (and (pos-visible-in-window-p 1)
1903 (pos-visible-in-window-p (point-max)))
1904 (enlarge-window (1+ (- (count-lines 1 (point-max))
1905 (reftex-window-height))))))
1906 (cond
1907 ((window-live-p win1) (select-window win1))
1908 (keep-current
1909 ;; we must have the old buffer!
1910 (switch-to-buffer-other-window buf1)
1911 (shrink-window (- (window-height) window-min-height))))))
1913 (defun reftex-select-with-char (prompt help-string &optional delay-time scroll)
1914 ;; Offer to select something with PROMPT and, after DELAY-TIME seconds,
1915 ;; also with HELP-STRING.
1916 ;; When SCROLL is non-nil, use SPC and DEL to scroll help window.
1917 (let ((char ?\?))
1918 (save-window-excursion
1919 (catch 'exit
1920 (message (concat prompt " (?=Help)"))
1921 (when (or (sit-for (or delay-time 0))
1922 (= ?\? (setq char (read-char-exclusive))))
1923 (reftex-kill-buffer "*RefTeX Select*")
1924 (switch-to-buffer-other-window "*RefTeX Select*")
1925 (insert help-string)
1926 (goto-char 1)
1927 (unless (and (pos-visible-in-window-p 1)
1928 (pos-visible-in-window-p (point-max)))
1929 (enlarge-window (1+ (- (count-lines 1 (point-max))
1930 (reftex-window-height)))))
1931 (setq truncate-lines t))
1932 (if (and (pos-visible-in-window-p 1)
1933 (pos-visible-in-window-p (point-max)))
1935 (setq prompt (concat prompt (if scroll " (SPC/DEL=Scroll)" ""))))
1936 (message prompt)
1937 (and (equal char ?\?) (setq char (read-char-exclusive)))
1938 (while t
1939 (cond ((equal char ?\C-g) (keyboard-quit))
1940 ((equal char ?\?))
1941 ((and scroll (equal char ?\ ))
1942 (condition-case nil (scroll-up) (error nil))
1943 (message prompt))
1944 ((and scroll (equal char ?\C-? ))
1945 (condition-case nil (scroll-down) (error nil))
1946 (message prompt))
1947 (t (message "")
1948 (throw 'exit char)))
1949 (setq char (read-char-exclusive)))))))
1952 (defun reftex-make-regexp-allow-for-ctrl-m (string)
1953 ;; convert STRING into a regexp, allowing ^M for \n and vice versa
1954 (let ((start -2))
1955 (setq string (regexp-quote string))
1956 (while (setq start (string-match "[\n\r]" string (+ 3 start)))
1957 (setq string (replace-match "[\n\r]" nil t string)))
1958 string))
1960 (defun reftex-get-buffer-visiting (file)
1961 ;; return a buffer visiting FILE
1962 (cond
1963 ((boundp 'find-file-compare-truenames) ; XEmacs
1964 (let ((find-file-compare-truenames t))
1965 (get-file-buffer file)))
1966 ((fboundp 'find-buffer-visiting) ; Emacs
1967 (find-buffer-visiting file))
1968 (t (error "This should not happen (reftex-get-buffer-visiting)"))))
1970 ;; Define `current-message' for compatibility with XEmacs prior to 20.4
1971 (defvar message-stack)
1972 (if (and (featurep 'xemacs)
1973 (not (fboundp 'current-message)))
1974 (defun current-message (&optional frame)
1975 (cdr (car message-stack))))
1977 (defun reftex-visited-files (list)
1978 ;; Takes a list of filenames and returns the buffers of those already visited
1979 (delq nil (mapcar (lambda (x) (if (reftex-get-buffer-visiting x) x nil))
1980 list)))
1982 (defun reftex-get-file-buffer-force (file &optional mark-to-kill)
1983 ;; Return a buffer visiting file. Make one, if necessary.
1984 ;; If neither such a buffer nor the file exist, return nil.
1985 ;; If MARK-TO-KILL is t and there is no live buffer, visit the file with
1986 ;; initializations according to `reftex-initialize-temporary-buffers',
1987 ;; and mark the buffer to be killed after use.
1989 (let ((buf (reftex-get-buffer-visiting file)))
1991 (cond (buf
1992 ;; We have it already as a buffer - just return it
1993 buf)
1995 ((file-readable-p file)
1996 ;; At least there is such a file and we can read it.
1998 (if (or (not mark-to-kill)
1999 (eq t reftex-initialize-temporary-buffers))
2001 ;; Visit the file with full magic
2002 (setq buf (find-file-noselect file))
2004 ;; Else: Visit the file just briefly, without or
2005 ;; with limited Magic
2007 ;; The magic goes away
2008 (let ((format-alist nil)
2009 (auto-mode-alist (reftex-auto-mode-alist))
2010 (default-major-mode 'fundamental-mode)
2011 (enable-local-variables nil)
2012 (after-insert-file-functions nil))
2013 (setq buf (find-file-noselect file)))
2015 ;; Is there a hook to run?
2016 (when (listp reftex-initialize-temporary-buffers)
2017 (save-excursion
2018 (set-buffer buf)
2019 (run-hooks 'reftex-initialize-temporary-buffers))))
2021 ;; Lets see if we got a license to kill :-|
2022 (and mark-to-kill
2023 (add-to-list 'reftex-buffers-to-kill buf))
2025 ;; Return the new buffer
2026 buf)
2028 ;; If no such file exists, return nil
2029 (t nil))))
2031 (defun reftex-kill-temporary-buffers (&optional buffer)
2032 ;; Kill all buffers in the list reftex-kill-temporary-buffers.
2033 (cond
2034 (buffer
2035 (when (member buffer reftex-buffers-to-kill)
2036 (kill-buffer buffer)
2037 (setq reftex-buffers-to-kill
2038 (delete buffer reftex-buffers-to-kill))))
2040 (while (setq buffer (pop reftex-buffers-to-kill))
2041 (when (bufferp buffer)
2042 (and (buffer-modified-p buffer)
2043 (y-or-n-p (format "Save file %s? "
2044 (buffer-file-name buffer)))
2045 (save-excursion
2046 (set-buffer buffer)
2047 (save-buffer)))
2048 (kill-buffer buffer))
2049 (pop reftex-buffers-to-kill)))))
2051 (defun reftex-splice-symbols-into-list (list alist)
2052 ;; Splice the association in ALIST of any symbols in LIST into the list.
2053 ;; Return new list.
2054 (let (rtn tmp)
2055 (while list
2056 (while (and (not (null (car list))) ;; keep list elements nil
2057 (symbolp (car list)))
2058 (setq tmp (car list))
2059 (cond
2060 ((assoc tmp alist)
2061 (setq list (append (nth 2 (assoc tmp alist)) (cdr list))))
2063 (error "Cannot treat symbol %s in reftex-label-alist"
2064 (symbol-name tmp)))))
2065 (push (pop list) rtn))
2066 (nreverse rtn)))
2068 (defun reftex-remove-symbols-from-list (list)
2069 ;; Remove all symbols from list
2070 (let (rtn)
2071 (while list
2072 (unless (symbolp (car list))
2073 (push (car list) rtn))
2074 (setq list (cdr list)))
2075 (nreverse rtn)))
2077 (defun reftex-uniquify (list)
2078 ;; Return a list of all elements in LIST, but each only once
2079 (let (new elm)
2080 (while list
2081 (setq elm (pop list))
2082 (unless (member elm new)
2083 (push elm new)))
2084 (nreverse new)))
2086 (defun reftex-uniquify-by-car (alist &optional keep-list)
2087 ;; Return a list of all elements in ALIST, but each car only once.
2088 ;; Elements of KEEP-LIST are not removed even if duplicate.
2089 (let (new elm)
2090 (while alist
2091 (setq elm (pop alist))
2092 (if (or (member (car elm) keep-list)
2093 (not (assoc (car elm) new)))
2094 (push elm new)))
2095 (nreverse new)))
2097 (defun reftex-abbreviate-title (string)
2098 (reftex-convert-string string "[-~ \t\n\r,;]" nil t t
2099 5 40 nil 1 " " (nth 5 reftex-derive-label-parameters)))
2101 (defun reftex-convert-string (string split-re illegal-re dot keep-fp
2102 nwords maxchar illegal abbrev sep
2103 ignore-words &optional downcase)
2104 "Convert a string (a sentence) to something shorter.
2105 SPLIT-RE is the regular expression used to split the string into words.
2106 ILLEGAL-RE matches characters which are illegal in the final string.
2107 DOT t means add dots to abbreviated words.
2108 KEEP-FP t means to keep a final punctuation when applicable.
2109 NWORDS Number of words to use.
2110 MAXCHAR Maximum number of characters in the final string.
2111 ILLEGAL nil: Throw away any words containing stuff matched with ILLEGAL-RE.
2112 t: Throw away only the matched part, not the whole word.
2113 ABBREV nil: Never abbreviate words.
2114 t: Always abbreviate words (see `reftex-abbrev-parameters').
2115 not t and not nil: Abbreviate words if necessary to shorten
2116 string below MAXCHAR.
2117 SEP String separating different words in the output string.
2118 IGNORE-WORDS List of words which should be removed from the string."
2120 (let* ((words0 (split-string string (or split-re "[ \t\n\r]")))
2121 (reftex-label-illegal-re (or illegal-re "\000"))
2122 (abbrev-re (concat
2123 "\\`\\("
2124 (make-string (nth 0 reftex-abbrev-parameters) ?.)
2125 "[" (nth 2 reftex-abbrev-parameters) "]*"
2126 "\\)"
2127 "[" (nth 3 reftex-abbrev-parameters) "]"
2128 (make-string (1- (nth 1 reftex-abbrev-parameters)) ?.)))
2129 words word)
2131 ;; Remove words from the ignore list or with funny characters
2132 (while (setq word (pop words0))
2133 (if downcase (setq word (downcase word)))
2134 (cond
2135 ((member (downcase word) ignore-words))
2136 ((string-match reftex-label-illegal-re word)
2137 (when illegal
2138 (while (string-match reftex-label-illegal-re word)
2139 (setq word (replace-match "" nil nil word)))
2140 (push word words)))
2142 (push word words))))
2143 (setq words (nreverse words))
2145 ;; Restrict number of words
2146 (if (> (length words) nwords)
2147 (setcdr (nthcdr (1- nwords) words) nil))
2149 ;; First, try to use all words
2150 (setq string (mapconcat 'identity words sep))
2152 ;; Abbreviate words if enforced by user settings or string length
2153 (if (or (eq t abbrev)
2154 (and abbrev
2155 (> (length string) maxchar)))
2156 (setq words
2157 (mapcar
2158 (lambda (w) (if (string-match abbrev-re w)
2159 (if dot
2160 (concat (match-string 1 w) ".")
2161 (match-string 1 w))
2163 words)
2164 string (mapconcat 'identity words sep)))
2166 ;; Shorten if still to long
2167 (setq string
2168 (if (> (length string) maxchar)
2169 (substring string 0 maxchar)
2170 string))
2172 ;; Delete the final punctuation, if any
2173 (if (and (not keep-fp) (string-match "\\s.+\\'" string))
2174 (setq string (replace-match "" nil nil string)))
2175 string))
2177 (defun reftex-nicify-text (text)
2178 ;; Make TEXT nice for inclusion as context into label menu.
2179 ;; 1. remove line breaks and extra white space
2180 (while (string-match "[\n\r\t]\\|[ \t][ \t]+" text)
2181 (setq text (replace-match " " nil t text)))
2182 ;; 2. cut before the next `\end{' or `\item' or `\\'
2183 (if (string-match "\\(\\\\end{\\|\\\\item\\|\\\\\\\\\\).*" text)
2184 (setq text (replace-match "" nil t text)))
2185 ;; 3. kill the embedded label
2186 (if (string-match "\\\\label{[^}]*}" text)
2187 (setq text (replace-match "" nil t text)))
2188 ;; 4. remove leading garbage
2189 (if (string-match "\\`[ }]+" text)
2190 (setq text (replace-match "" nil t text)))
2191 ;; 5. limit length
2192 (cond
2193 ((> (length text) 100) (substring text 0 100))
2194 ((= (length text) 0) (make-string 1 ?\ ))
2195 (t text)))
2197 ;;; =========================================================================
2199 ;;; Fontification and Highlighting
2201 (defun reftex-use-fonts ()
2202 ;; Return t if we can and want to use fonts.
2203 (and window-system
2204 reftex-use-fonts
2205 (featurep 'font-lock)))
2207 (defun reftex-refontify ()
2208 ;; Return t if we need to refontify context
2209 (and (reftex-use-fonts)
2210 (or (eq t reftex-refontify-context)
2211 (and (eq 1 reftex-refontify-context)
2212 ;; Test of we use the font-lock version of x-symbol
2213 (and (featurep 'x-symbol-tex) (not (boundp 'x-symbol-mode)))))))
2215 (defvar font-lock-defaults-computed)
2216 (defun reftex-fontify-select-label-buffer (parent-buffer)
2217 ;; Fontify the `*RefTeX Select*' buffer. Buffer is temporarily renamed to
2218 ;; start with none-SPC char, beacuse Font-Lock otherwise refuses operation.
2219 (run-hook-with-args 'reftex-pre-refontification-functions
2220 parent-buffer 'reftex-ref)
2221 (let* ((oldname (buffer-name))
2222 (newname (concat "Fontify-me-" oldname)))
2223 (unwind-protect
2224 (progn
2225 ;; Rename buffer temporarily to start w/o space (because of font-lock)
2226 (rename-buffer newname t)
2227 (cond
2228 ((fboundp 'font-lock-default-fontify-region)
2229 ;; Good: we have the indirection functions
2230 (set (make-local-variable 'font-lock-fontify-region-function)
2231 'reftex-select-font-lock-fontify-region)
2232 (let ((major-mode 'latex-mode))
2233 (font-lock-mode 1)))
2234 ((fboundp 'font-lock-set-defaults-1)
2235 ;; Looks like the XEmacs font-lock stuff.
2236 ;; FIXME: this is still kind of a hack, but it works.
2237 (set (make-local-variable 'font-lock-keywords) nil)
2238 (let ((major-mode 'latex-mode)
2239 (font-lock-defaults-computed nil))
2240 (font-lock-set-defaults-1)
2241 (reftex-select-font-lock-fontify-region (point-min) (point-max))))
2243 ;; Oops?
2244 (message "Sorry: cannot refontify RefTeX Select buffer."))))
2245 (rename-buffer oldname))))
2247 (defun reftex-select-font-lock-fontify-region (beg end &optional loudly)
2248 ;; Fontify a region, but only lines starting with a dot.
2249 (let ((func (if (fboundp 'font-lock-default-fontify-region)
2250 'font-lock-default-fontify-region
2251 'font-lock-fontify-region))
2252 beg1 end1)
2253 (goto-char beg)
2254 (while (re-search-forward "^\\." end t)
2255 (setq beg1 (point) end1 (progn (skip-chars-forward "^\n") (point)))
2256 (funcall func beg1 end1 nil)
2257 (goto-char end1))))
2259 (defun reftex-select-font-lock-unfontify (&rest ignore) t)
2261 (defun reftex-verified-face (&rest faces)
2262 ;; Return the first valid face in FACES, or nil if none is valid.
2263 ;; Also, when finding a nil element in FACES, return nil. This
2264 ;; function is just a safety net to catch name changes of builtin
2265 ;; fonts. Currently it is only used for reftex-label-face, which has
2266 ;; as default font-lock-reference-face, which was recently renamed
2267 ;; to font-lock-constant-face.
2268 (let (face)
2269 (catch 'exit
2270 (while (setq face (pop faces))
2271 (if (featurep 'xemacs)
2272 (if (find-face face) (throw 'exit face))
2273 (if (facep face) (throw 'exit face)))))))
2275 ;; Highlighting uses overlays. For XEmacs, we need the emulation.
2276 (if (featurep 'xemacs) (require 'overlay))
2278 ;; We keep a vector with several different overlays to do our highlighting.
2279 (defvar reftex-highlight-overlays [nil nil])
2281 ;; Initialize the overlays
2282 (aset reftex-highlight-overlays 0 (make-overlay 1 1))
2283 (overlay-put (aref reftex-highlight-overlays 0)
2284 'face 'highlight)
2285 (aset reftex-highlight-overlays 1 (make-overlay 1 1))
2286 (overlay-put (aref reftex-highlight-overlays 1)
2287 'face reftex-cursor-selected-face)
2289 ;; Two functions for activating and deactivation highlight overlays
2290 (defun reftex-highlight (index begin end &optional buffer)
2291 "Highlight a region with overlay INDEX."
2292 (move-overlay (aref reftex-highlight-overlays index)
2293 begin end (or buffer (current-buffer))))
2294 (defun reftex-unhighlight (index)
2295 "Detach overlay INDEX."
2296 (delete-overlay (aref reftex-highlight-overlays index)))
2298 (defun reftex-highlight-shall-die ()
2299 ;; Function used in pre-command-hook to remove highlights.
2300 (remove-hook 'pre-command-hook 'reftex-highlight-shall-die)
2301 (reftex-unhighlight 0))
2303 ;;; =========================================================================
2305 ;;; Keybindings
2307 ;; The default bindings in the mode map.
2308 (loop for x in
2309 '(("\C-c=" . reftex-toc)
2310 ("\C-c(" . reftex-label)
2311 ("\C-c)" . reftex-reference)
2312 ("\C-c[" . reftex-citation)
2313 ("\C-c<" . reftex-index)
2314 ("\C-c>" . reftex-display-index)
2315 ("\C-c/" . reftex-index-selection-or-word)
2316 ("\C-c\\" . reftex-index-phrase-selection-or-word)
2317 ("\C-c|" . reftex-index-visit-phrases-buffer)
2318 ("\C-c&" . reftex-view-crossref))
2319 do (define-key reftex-mode-map (car x) (cdr x)))
2321 ;; Bind `reftex-mouse-view-crossref' only when the key is still free
2322 (if (featurep 'xemacs)
2323 (unless (key-binding [(shift button2)])
2324 (define-key reftex-mode-map [(shift button2)]
2325 'reftex-mouse-view-crossref))
2326 (unless (key-binding [(shift mouse-2)])
2327 (define-key reftex-mode-map [(shift mouse-2)]
2328 'reftex-mouse-view-crossref)))
2330 ;; Bind `reftex-view-crossref-from-bibtex' in BibTeX mode map
2331 (eval-after-load
2332 "bibtex"
2333 '(define-key bibtex-mode-map "\C-c&" 'reftex-view-crossref-from-bibtex))
2335 ;; If the user requests so, she can have a few more bindings:
2336 (when reftex-extra-bindings
2337 (loop for x in
2338 '(("\C-ct" . reftex-toc)
2339 ("\C-cl" . reftex-label)
2340 ("\C-cr" . reftex-reference)
2341 ("\C-cc" . reftex-citation)
2342 ("\C-cv" . reftex-view-crossref)
2343 ("\C-cg" . reftex-grep-document)
2344 ("\C-cs" . reftex-search-document))
2345 do (define-key reftex-mode-map (car x) (cdr x))))
2347 ;;; =========================================================================
2349 ;;; Menu
2351 ;; Define a menu for the menu bar if Emacs is running under X
2353 (require 'easymenu)
2355 (easy-menu-define reftex-mode-menu reftex-mode-map
2356 "Menu used in RefTeX mode"
2357 `("Ref"
2358 ["Table of Contents" reftex-toc t]
2359 "--"
2360 ["\\label" reftex-label t]
2361 ["\\ref" reftex-reference t]
2362 ["\\cite" reftex-citation t]
2363 ("\\index"
2364 ["\\index" reftex-index t]
2365 ["\\index{THIS}" reftex-index-selection-or-word t]
2366 "--"
2367 ["Add THIS to Index Phrases" reftex-index-phrase-selection-or-word t]
2368 ["Visit Phrase Buffer" reftex-index-visit-phrases-buffer t]
2369 ["Apply Phrases to Region" reftex-index-phrases-apply-to-region t]
2370 "--"
2371 ["Display the Index" reftex-display-index t])
2372 "--"
2373 ["View Crossref" reftex-view-crossref t]
2374 "--"
2375 ("Parse Document"
2376 ["One File" reftex-parse-one reftex-enable-partial-scans]
2377 ["Entire Document" reftex-parse-all t]
2378 ["Save to File" (reftex-access-parse-file 'write)
2379 (> (length (symbol-value reftex-docstruct-symbol)) 0)]
2380 ["Restore from File" (reftex-access-parse-file 'restore) t]
2381 "--"
2382 ["Reset RefTeX Mode" reftex-reset-mode t])
2383 ("Global Actions"
2384 ["Search Whole Document" reftex-search-document t]
2385 ["Replace in Document" reftex-query-replace-document t]
2386 ["Grep on Document" reftex-grep-document t]
2387 "--"
2388 ["Find Duplicate Labels" reftex-find-duplicate-labels t]
2389 ["Change Label and Refs" reftex-change-label t]
2390 ["Renumber Simple Labels" reftex-renumber-simple-labels t]
2391 "--"
2392 ["Create TAGS File" reftex-create-tags-file t]
2393 "--"
2394 ["Save Document" reftex-save-all-document-buffers t])
2395 "--"
2396 ("Options"
2397 "PARSER"
2398 ["Partial Scans"
2399 (setq reftex-enable-partial-scans (not reftex-enable-partial-scans))
2400 :style toggle :selected reftex-enable-partial-scans]
2401 ["Auto-Save Parse Info"
2402 (setq reftex-save-parse-info (not reftex-save-parse-info))
2403 :style toggle :selected reftex-save-parse-info]
2404 "--"
2405 "CROSSREF INFO"
2406 ["Automatic Info" reftex-toggle-auto-view-crossref
2407 :style toggle :selected reftex-auto-view-crossref-timer]
2408 ["...in Echo Area" (setq reftex-auto-view-crossref t)
2409 :style radio :selected (eq reftex-auto-view-crossref t)]
2410 ["...in Other Window" (setq reftex-auto-view-crossref 'window)
2411 :style radio :selected (eq reftex-auto-view-crossref 'window)]
2412 "--"
2413 "MISC"
2414 ["AUC TeX Interface" reftex-toggle-plug-into-AUCTeX
2415 :style toggle :selected reftex-plug-into-AUCTeX])
2416 ("Reference Style"
2417 ["Default" (setq reftex-vref-is-default nil
2418 reftex-fref-is-default nil)
2419 :style radio :selected (not (or reftex-vref-is-default
2420 reftex-fref-is-default))]
2421 ["Varioref" (setq reftex-vref-is-default t
2422 reftex-fref-is-default nil)
2423 :style radio :selected reftex-vref-is-default]
2424 ["Fancyref" (setq reftex-fref-is-default t
2425 reftex-vref-is-default nil)
2426 :style radio :selected reftex-fref-is-default])
2427 ("Citation Style"
2428 ,@(mapcar
2429 (lambda (x)
2430 (vector
2431 (capitalize (symbol-name (car x)))
2432 (list 'reftex-set-cite-format (list 'quote (car x)))
2433 :style 'radio :selected
2434 (list 'eq (list 'reftex-get-cite-format) (list 'quote (car x)))))
2435 reftex-cite-format-builtin)
2436 "--"
2437 "Sort Database Matches"
2438 ["Not" (setq reftex-sort-bibtex-matches nil)
2439 :style radio :selected (eq reftex-sort-bibtex-matches nil)]
2440 ["by Author" (setq reftex-sort-bibtex-matches 'author)
2441 :style radio :selected (eq reftex-sort-bibtex-matches 'author)]
2442 ["by Year" (setq reftex-sort-bibtex-matches 'year)
2443 :style radio :selected (eq reftex-sort-bibtex-matches 'year)]
2444 ["by Year, reversed" (setq reftex-sort-bibtex-matches 'reverse-year)
2445 :style radio :selected (eq reftex-sort-bibtex-matches 'reverse-year)])
2446 ("Index Style"
2447 ,@(mapcar
2448 (lambda (x)
2449 (vector
2450 (capitalize (symbol-name (car x)))
2451 (list 'reftex-add-index-macros (list 'list (list 'quote (car x))))
2452 :style 'radio :selected
2453 (list 'memq (list 'quote (car x))
2454 (list 'get 'reftex-docstruct-symbol
2455 (list 'quote 'reftex-index-macros-style)))))
2456 reftex-index-macros-builtin))
2457 "--"
2458 ("Customize"
2459 ["Browse RefTeX Group" reftex-customize t]
2460 "--"
2461 ["Build Full Customize Menu" reftex-create-customize-menu
2462 (fboundp 'customize-menu-create)])
2463 ("Documentation"
2464 ["Info" reftex-info t]
2465 ["Commentary" reftex-show-commentary t])))
2467 (defun reftex-customize ()
2468 "Call the customize function with reftex as argument."
2469 (interactive)
2470 (customize-browse 'reftex))
2472 (defun reftex-create-customize-menu ()
2473 "Create a full customization menu for RefTeX, insert it into the menu."
2474 (interactive)
2475 (if (fboundp 'customize-menu-create)
2476 (progn
2477 (easy-menu-change
2478 '("Ref") "Customize"
2479 `(["Browse RefTeX group" reftex-customize t]
2480 "--"
2481 ,(customize-menu-create 'reftex)
2482 ["Set" Custom-set t]
2483 ["Save" Custom-save t]
2484 ["Reset to Current" Custom-reset-current t]
2485 ["Reset to Saved" Custom-reset-saved t]
2486 ["Reset to Standard Settings" Custom-reset-standard t]))
2487 (message "\"Ref\"-menu now contains full customization menu"))
2488 (error "Cannot expand menu (outdated version of cus-edit.el)")))
2490 (defun reftex-show-commentary ()
2491 "Use the finder to view the file documentation from `reftex.el'."
2492 (interactive)
2493 (require 'finder)
2494 (finder-commentary "reftex.el"))
2496 (defun reftex-info (&optional node)
2497 "Read documentation for RefTeX in the info system.
2498 With optional NODE, go directly to that node."
2499 (interactive)
2500 (require 'info)
2501 (Info-goto-node (format "(reftex)%s" (or node ""))))
2503 ;;; Install the kill-buffer and kill-emacs hooks ------------------------------
2505 (add-hook 'kill-buffer-hook 'reftex-kill-buffer-hook)
2506 (add-hook 'kill-emacs-hook 'reftex-kill-emacs-hook)
2508 ;;; Run Hook ------------------------------------------------------------------
2510 (run-hooks 'reftex-load-hook)
2512 ;;; That's it! ----------------------------------------------------------------
2514 (setq reftex-tables-dirty t) ; in case this file is evaluated by hand
2515 (provide 'reftex)
2517 ;;;============================================================================
2519 ;;; reftex.el ends here