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