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