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