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