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