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