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