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