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