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