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