(locate-library): Comment out the code that searches
[bpt/emacs.git] / lisp / textmodes / reftex.el
CommitLineData
fba437e6 1;; reftex.el --- Minor mode for doing \label, \ref and \cite in LaTeX
a7ec1775
RS
2;; Copyright (c) 1997 Free Software Foundation, Inc.
3
4;; Author: Carsten Dominik <dominik@strw.LeidenUniv.nl>
5;; Keywords: tex
6
7;; This file is part of GNU Emacs.
8
9;; GNU Emacs is free software; you can redistribute it and/or modify
10;; it under the terms of the GNU General Public License as published by
11;; the Free Software Foundation; either version 2, or (at your option)
12;; any later version.
13
14;; GNU Emacs is distributed in the hope that it will be useful,
15;; but WITHOUT ANY WARRANTY; without even the implied warranty of
16;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17;; GNU General Public License for more details.
18
19;; You should have received a copy of the GNU General Public License
20;; along with GNU Emacs; see the file COPYING. If not, write to the
21;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
22;; Boston, MA 02111-1307, USA.
23
24;;---------------------------------------------------------------------------
25;;
26;;; Commentary:
27;;
fba437e6
RS
28;; RefTeX is a minor mode with distinct support for \ref, \label and
29;; \cite commands in (multi-file) LaTeX documents.
a7ec1775
RS
30;; Labels are created semi-automatically. Definition context of labels is
31;; provided when creating a reference. Citations are simplified with
396e0b08
KH
32;; efficient database lookup. A table of contents buffer provides easy
33;; access to any part of a document.
a7ec1775
RS
34;;
35;; To turn RefTeX Minor Mode on and off in a particular buffer, use
36;; `M-x reftex-mode'.
37;;
38;; To turn on RefTeX Minor Mode for all LaTeX files, add one of the
39;; following lines to your .emacs file:
40;;
41;; (add-hook 'LaTeX-mode-hook 'turn-on-reftex) ; with AUCTeX LaTeX mode
42;; (add-hook 'latex-mode-hook 'turn-on-reftex) ; with Emacs latex mode
43;;
396e0b08
KH
44;; For default key bindings, see further down in this documentation.
45;;
46;;---------------------------------------------------------------------------
47;;
48;; CONTENTS
49;; --------
50;;
51;; Overview............................ All you need to know to get started.
a7ec1775 52;;
396e0b08
KH
53;; Configuration....................... How to configure RefTeX.
54;; Configuration Examples........... Tutorial examples.
55;; Hooks............................ Available hooks.
56;; Configuration Variables.......... Complete listing.
57;; Key Bindings........................ A list of default bindings.
58;; Multifile Documents................. Documents spread over many files.
59;; References to Other Documents....... RefTeX and the LaTeX package `xr'.
60;; Optimizations for Large Documents... How to improve speed and memory use.
61;; Related Packages.................... Other Emacs packages.
62;; Known Bugs and Work-Arounds......... First aid.
63;; Author.............................. Who wrote RefTeX and who helped.
a7ec1775
RS
64;;---------------------------------------------------------------------------
65;;
66;; OVERVIEW
396e0b08
KH
67;; ========
68;;
69;; 1. USING \label AND \ref. Labels and references are one of the strong
70;; points of LaTeX. But, in documents with hundreds of equations,
71;; figures, tables etc. it becomes quickly impossible to find good label
72;; names and to actually remember them. Then, also completion of labels
73;; is not enough. One actually needs to see the context of the label
74;; definition to find the right one.
75;;
76;; - RefTeX distinguishes labels for different environments. It always
77;; knows if a certain label references a figure, table etc.. You can
78;; configure RefTeX to recognize any additional labeled environments
79;; you have defined yourself.
a7ec1775
RS
80;;
81;; - RefTeX defines automatically unique labels. Type `C-c ('
396e0b08 82;; (`reftex-label') to insert a label at point. RefTeX will either
a7ec1775
RS
83;; - derive a label from context (default for section labels)
84;; - insert a simple label consisting of a prefix and a number
396e0b08 85;; (default for equations,enumerate items, and footnotes) or
c52bdfca 86;; - prompt for a label string (figures and tables).
a7ec1775 87;; Which labels are created how can be controlled with the variable
c52bdfca 88;; `reftex-insert-label-flags'.
a7ec1775 89;;
396e0b08
KH
90;; - Referencing labels is a snap and I promise you'll love it. In
91;; order to make a reference, type `C-c )' (`reftex-reference'). This
92;; shows an outline of the document with all labels of a certain type
93;; (figure, equation,...) and context of the label definition.
94;; Selecting one of the labels inserts a \ref macro into the original
95;; buffer. Online help during the selection is available with `?'.
96;;
97;; 2. CITATIONS. After typing `C-c [' (`reftex-citation'), RefTeX will let
98;; you specify a regexp to search in current BibTeX database files (as
99;; specified in the \bibliography command) and pull out a formatted list
100;; of matches for you to choose from. The list is *formatted* and
101;; sorted, thus much easier to read than the raw database entries. The
102;; text inserted into the buffer is by default just `\cite{KEY}', but
103;; can also contain author names and the year in a configurable way.
104;; See documentation of the variable `reftex-cite-format'.
105;;
206c6f82
RS
106;; 3. TABLE OF CONTENTS. Typing `C-c =' (`reftex-toc') will show a table
107;; of contents of the document. From that buffer, you can jump quickly
108;; to every part of your document. This is similar to imenu, only it
109;; works for entire multifile documents and uses the keyboard rather
110;; than the mouse. The initial version of this function was contributed
111;; by Stephen Eglen.
a7ec1775 112;;
206c6f82
RS
113;; 4. MULTIFILE DOCUMENTS are fully supported by RefTeX. Such documents
114;; consist of a master file and many other files being included via
396e0b08
KH
115;; \input or \include. RefTeX will provide cross referencing
116;; information from all files which are part of the document. See
117;; `RefTeX and Multifile Documents' further down in the documentation
118;; for more information on this topic.
119;;
120;; 5. DOCUMENT PARSING. RefTeX needs to parse the document in order to
121;; find labels and other information. It will do it automatically once,
122;; when you start working with a document. Re-parsing should not be
123;; necessary too often since RefTeX updates its lists internally when
124;; you make a new label with `reftex-label'. To enforce reparsing,
125;; call any of the functions `reftex-citation', `reftex-label',
126;; `reftex-reference', `reftex-toc' with a raw C-u prefix, or press the
127;; `r' key in the label menu and table of contents buffer.
128;;---------------------------------------------------------------------------
a7ec1775 129;;
a7ec1775 130;; CONFIGURATION
396e0b08 131;; =============
a7ec1775 132;;
396e0b08
KH
133;; RefTeX needs to be configured if you use labels to mark environments
134;; defined by yourself (e.g. with `\newenvironment') or in packages not
135;; included in the standard LaTeX distribution. RefTeX's default settings
136;; make it recognize practically all labeled environments and macros
137;; discussed in `The LaTeX Companion' by Goossens, Mittelbach & Samarin,
138;; Addison-Wesley 1994. These are:
139;;
140;; - figure, figure*, table, table*, equation, eqnarray, enumerate,
141;; the \footnote macro (this is the LaTeX core stuff)
142;; - align, gather, multline, flalign, alignat, xalignat, xxalignat,
143;; subequations (from AMS-LaTeX's amsmath.sty package)
144;; - the \endnote macro (from endnotes.sty)
145;; - Beqnarray (fancybox.sty)
146;; - floatingfig (floatfig.sty)
147;; - longtable (longtable.sty)
148;; - figwindow, tabwindow (picinpar.sty)
149;; - sidewaysfigure, sidewaystable (rotating.sty)
150;; - subfigure, subfigure*, the \subfigure macro (subfigure.sty)
151;; - supertabular (supertab.sty)
152;; - wrapfigure (wrapfig.sty)
153;;
154;; If you want to use any other labeled environments or macros, you need
155;; to configure RefTeX.
156;;
157;; Per Abrahamsens custom.el package provides a simple way to do
158;; configuration. To try it out, use `M-x reftex-customize'.
a7ec1775 159;;
396e0b08
KH
160;; CONFIGURATION EXAMPLES
161;; ----------------------
a7ec1775 162;;
396e0b08
KH
163;; Suppose you are working with AMS-LaTeX amsmath package (with its math
164;; environments like `align', `multline' etc.). RefTeX is preconfigured to
165;; recognize these - so there is nothing you have to do.
a7ec1775 166;;
396e0b08
KH
167;; Suppose you are also using `\newtheorem' in LaTeX in order to define two
168;; new environments `theorem' and `axiom'
a7ec1775 169;;
396e0b08
KH
170;; \newtheorem{axiom}{Axiom}
171;; \newtheorem{theorem}{Theorem}
172;;
173;; to be used like this:
a7ec1775 174;;
396e0b08
KH
175;; \begin{axiom}
176;; \label{ax:first}
177;; ....
178;; \end{axiom}
a7ec1775 179;;
396e0b08
KH
180;; So we need to tell RefTeX that `theorem' and `axiom' are new labeled
181;; environments which define their own label categories. Here is how:
a7ec1775 182;;
396e0b08
KH
183;; (setq reftex-label-alist
184;; '(("axiom" ?a "ax:" "~\\ref{%s}" nil ("Axiom" "Ax."))
185;; ("theorem" ?h "thr:" "~\\ref{%s}" t ("Theorem" "Theor." "Th."))))
186;;
187;; The type indicator characters ?a and ?h are used for prompts when RefTeX
188;; queries for a label type. Note that `h' was chosen for `theorem' since
189;; `t' is already taken by `table'. Note that also `s', `f', `e', `i', `n'
190;; are already used for standard environments.
a7ec1775
RS
191;; The automatic labels for Axioms and Theorems will look like "ax:23" or
192;; "thr:24".
193;; The "\ref{%s}" is a format string indicating how to insert references to
396e0b08
KH
194;; these labels.
195;; The next item indicates how to grab context of the label definition.
196;; - t means to get it from a default location (from the beginning of a
197;; \macro or after the \begin statement). t is *not* a good choice for
198;; eqnarray and similar environments.
a7ec1775
RS
199;; - nil means to use the text right after the label definition.
200;; - For more complex ways of getting context, see the docstring of
c52bdfca 201;; `reftex-label-alist'.
a7ec1775
RS
202;; The strings at the end of each entry are used to guess the correct label
203;; type from the word before point when creating a reference. E.g. if you
396e0b08
KH
204;; write: "As we have shown in Theorem" and then press `C-c )', RefTeX will
205;; know that you are looking for a theorem label and restrict the menu to
206;; only these labels without even asking.
207;;
208;; Depending on how you would like the label insertion and selection for
209;; the new environments to work, you might want to add the letters "a" and
210;; "h" to some of the flags in the following variables:
211;;
212;; reftex-insert-label-flags reftex-label-menu-flags
213;;
214;; Suppose you want to make figures not directly with the figure
215;; environment, but with a macro like
216;;
217;; \newcommand{\myfig}[5][tbp]{%
218;; \begin{figure}[#1]
219;; \epsimp[#5]{#2}
220;; \caption{#3}
221;; \label{#4}
222;; \end{figure}}
223;;
224;; which would be called like
225;;
226;; \myfig[htp]{filename}{caption text}{label}{1}
a7ec1775 227;;
396e0b08
KH
228;; Now we also need to tell RefTeX that the 4th argument of the \myfig
229;; macro is a figure label, and where to find the context.
a7ec1775 230;;
396e0b08
KH
231;; (setq reftex-label-alist
232;; '(("axiom" ?a "ax:" "~\\ref{%s}" nil ("Axiom" "Ax."))
233;; ("theorem" ?h "thr:" "~\\ref{%s}" t ("Theorem" "Theor." "Th."))
234;; ("\\myfig[]{}{}{*}{}" ?f nil nil 3)))
a7ec1775 235;;
396e0b08
KH
236;; The empty pairs of brackets indicate the different arguments of the
237;; \myfig macro. The `*' marks the label argument. `?f' indicates that
238;; this is a figure label which will be listed together with labels from
239;; normal figure environments. The nil entries for prefix and reference
240;; format mean to use the defaults for figure labels. The `3' for the
241;; context method means to grab the 3rd macro argument - the caption.
242;;
243;; As a side effect of this configuration, `reftex-label' will now insert
244;; the required naked label (without the \label macro) when point is
245;; directly after the opening parenthesis of a \myfig macro argument.
a7ec1775
RS
246;;
247;; -----
a7ec1775 248;;
396e0b08
KH
249;; If you are writing in a language different from English you might want
250;; to add magic words for that language. Here is a German example:
251;;
252;; (setq reftex-label-alist
a7ec1775
RS
253;; '((nil ?s nil nil nil ("Kapitel" "Kap." "Abschnitt" "Teil"))
254;; (nil ?e nil nil nil ("Gleichung" "Gl."))
255;; (nil ?t nil nil nil ("Tabelle"))
256;; (nil ?f nil nil nil ("Figur" "Abbildung" "Abb."))
396e0b08
KH
257;; (nil ?n nil nil nil ("Anmerkung" "Anm."))
258;; (nil ?i nil nil nil ("Punkt"))))
a7ec1775 259;;
c52bdfca 260;; Using nil as first item in each entry makes sure that this entry does
206c6f82
RS
261;; not replace the original entry for that label type, but just adds magic
262;; words.
263;;
264;; -----
265;;
396e0b08
KH
266;; Normally, RefTeX inserts equation references with parenthesis like
267;; "~(\ref{KEY})". If you want to change this to square brackets, use
268;;
269;; (setq reftex-label-alist '((nil ?e nil "~[\\ref{%s}]" nil nil)))
270;;
271;; In order to use the AMS-LaTeX \eqref macro instead, either of the
272;; following lines does the job.
273;;
274;; (setq reftex-label-alist '((nil ?e nil "~\\eqref{%s}" nil nil)))
275;; (setq reftex-label-alist '(AMSTeX))
276;;
277;; ----
278;;
206c6f82
RS
279;; By default, citations are inserted simply as \cite{KEY}. You can have
280;; more complex citation commands with many available packages, most
281;; notably the harvard and natbib packages. RefTeX can be configured to
396e0b08
KH
282;; support these and other styles by setting the variable
283;; `reftex-cite-format'. E.g., for the natbib package you would use
284;;
206c6f82
RS
285;; (setq reftex-cite-format 'natbib)
286;;
287;; This can also be done as a file variable. For the full list of builtin
288;; options, try `M-x customize-variable RET reftex-cite-format RET'.
a7ec1775
RS
289;;
290;; HOOKS
291;; -----
206c6f82
RS
292;; - Loading reftex.el runs the hook `reftex-load-hook'.
293;; - Turning on reftex-mode runs `reftex-mode-hook'.
396e0b08
KH
294;; - Files visited literally are processed with
295;; `reftex-initialize-temporary-buffers' if that is a list of functions.
296;;
297;; CONFIGURATION VARIABLES
298;; -----------------------
a7ec1775 299;;
396e0b08
KH
300;; The best way to learn about all configuration variables is via the
301;; browser interface of the custom library. For reference, I am giving
302;; here a complete list.
303;;
304;; ;; Defining label environments
305;; reftex-default-label-alist-entries
306;; reftex-label-alist
307;; reftex-use-text-after-label-as-context
308;; reftex-section-levels
309;; reftex-default-context-regexps
310;; ;; Label insertion
311;; reftex-insert-label-flags
312;; reftex-derive-label-parameters
313;; reftex-label-illegal-re
314;; reftex-abbrev-parameters
315;; ;; Label referencing
316;; reftex-label-menu-flags
317;; reftex-level-indent
318;; reftex-refontify-context
319;; reftex-guess-label-type
320;; ;; BibteX citation configuration
321;; reftex-bibpath-environment-variables
322;; reftex-bibfile-ignore-list
323;; reftex-sort-bibtex-matches
324;; reftex-cite-format
325;; reftex-comment-citations
326;; reftex-cite-comment-format
327;; reftex-cite-punctuation
328;; ;; Table of contents configuration
329;; reftex-toc-follow-mode
330;; ;; Fine-tuning the parser
331;; reftex-keep-temporary-buffers
332;; reftex-initialize-temporary-buffers
333;; reftex-enable-partial-scans
334;; reftex-save-parse-info
335;; ;; Miscellaneous configurations
336;; reftex-extra-bindings
337;; reftex-plug-into-AUCTeX
338;; reftex-use-fonts
339;; reftex-auto-show-entry
340;; reftex-load-hook
341;; reftex-mode-hook
a7ec1775
RS
342;;-------------------------------------------------------------------------
343;;
344;; KEY BINDINGS
396e0b08 345;; ============
a7ec1775 346;;
396e0b08
KH
347;; All RefTeX commands can be reached from its menu, the `Ref' menu on the
348;; menu bar. More frequently used commands have key bindings:
a7ec1775
RS
349;;
350;; C-c = reftex-toc
351;; C-c ( reftex-label
352;; C-c ) reftex-reference
353;; C-c [ reftex-citation
354;; C-c & reftex-view-crossref
355;;
396e0b08
KH
356;; These keys are chosen to avoid interfering with AUCTeX's settings.
357;; Personally, I also bind some functions in the C-c LETTER map for
358;; easier access:
a7ec1775
RS
359;;
360;; C-c t reftex-toc
361;; C-c l reftex-label
362;; C-c r reftex-reference
363;; C-c c reftex-citation
364;; C-c v reftex-view-crossref
365;; C-c s reftex-search-document
366;; C-c g reftex-grep-document
367;;
368;; If you want to copy those as well, set in your .emacs file:
396e0b08 369;;
a7ec1775
RS
370;; (setq reftex-extra-bindings t)
371;;
372;; It is possible to bind the function for viewing cross references to a
396e0b08 373;; mouse event. Something like the following will do the trick:
a7ec1775 374;;
396e0b08 375;; (add-hook 'reftex-load-hook
a7ec1775 376;; '(lambda ()
396e0b08 377;; (define-key reftex-mode-map [(shift mouse-2)]
a7ec1775 378;; 'reftex-mouse-view-crossref)))
a7ec1775
RS
379;;-------------------------------------------------------------------------
380;;
396e0b08
KH
381;; REFTEX AND MULTIFILE DOCUMENTS
382;; ==============================
383;;
384;; The following is relevant when using RefTeX for multi-file documents:
385;;
386;; o RefTeX has full support for multifile documents. You can edit parts
387;; of several (multifile) documents at the same time without conflicts.
388;; RefTeX provides functions to run `grep', `search' and `query-replace'
389;; on all files which are part of a multifile document.
390;;
391;; o All files belonging to a multifile document should have a File
392;; Variable (`TeX-master' for AUCTeX or `tex-main-file' for the standard
393;; Emacs LaTeX mode) set to the name of the master file. See the
394;; documentation of your (La)TeX mode and the Emacs documentation on
395;; file variables: [Emacs/Customization/Variables/File Variables].
396;;
397;; o The context of a label definition must be found in the same file as
398;; the label itself in order to be processed correctly by RefTeX. The
399;; only exception is that section labels referring to a section statement
400;; outside the current file can still use that section title as context.
401;;-------------------------------------------------------------------------
402;;
403;; REFERENCES TO OTHER DOCUMENTS
404;; =============================
405;;
406;; RefTeX supports the LaTeX package `xr', which makes it possible to
407;; reference labels defined in another document. See the documentation on
408;; `xr' for details.
409;; When the document is set up to work with `xr', you can use the `x' key
410;; in the reference label menu to switch to the label menu of an external
411;; document and select any labels from there. In the *toc* buffer, the
412;; `x' key can be used to switch to the table of contents of an external
413;; document.
414;;
415;; For this kind of inter-document cross references, saving of parsing
416;; information can mean a large speed-up.
417;;
418;; (setq reftex-save-parse-info t)
419;;
420;;-------------------------------------------------------------------------
421;;
422;; OPTIMIZATIONS FOR LARGE DOCUMENTS
423;; =================================
424;;
425;; The default settings of RefTeX ensure a safe ride for beginners and
426;; casual users. However, when using RefTeX for a large project and/or on
427;; a small computer, there are ways to improve speed and memory usage.
428;;
429;; o RefTeX will load other parts of a multifile document as well as BibTeX
430;; database files for lookup purposes. These buffers are kept, so that
431;; subsequent use of the same files is fast. If you can't afford keeping
432;; these buffers around, and if you can live with a speed penalty, try
433;;
434;; (setq reftex-keep-temporary-buffers nil)
435;;
436;; o The `C-u' prefix on the major RefTeX commands `reftex-label',
437;; `reftex-reference', `reftex-citation' and `reftex-toc' initiates
438;; re-parsing of the entire document in order to update the parsing
439;; information. For a large document this can be unnecessary, in
440;; particular if only one file has changed. RefTeX can be configured to
441;; do partial scans instead of full ones. `C-u' re-parsing then does
442;; apply only to the current buffer and files included from it.
443;; Likewise, the `r' key in both the label menu and the table-of-contents
444;; buffer will only prompt scanning of the file in which the label or
445;; section macro near the cursor was defined. Re-parsing of the entire
446;; document is still available by using `C-u C-u' as a prefix, or the
447;; capital `R' key in the menus. To use this feature, try
448;;
449;; (setq reftex-enable-partial-scans t)
450;;
451;; o Even with partial scans enabled, RefTeX still has to make one full
452;; scan, when you start working with a document. To avoid this, parsing
453;; information can stored in a file. The file `MASTER.rel' is used for
454;; storing information about a document with master file MASTER.tex.
455;; It is written each time RefTeX parses (part of) the document, and
456;; restored when you begin working with a document in a new editing
457;; session. To use this feature, put into .emacs:
458;;
459;; (setq reftex-save-parse-info t)
460;;----------------------------------------------------------------------------
461;;
a7ec1775 462;; RELATED PACKAGES
396e0b08 463;; ================
a7ec1775
RS
464;;
465;; AUCTeX
466;; ------
206c6f82
RS
467;; If you are writing TeX or LaTeX documents with Emacs, you should have
468;; a look at AUCTeX, the definitive package to work with TeX and LaTeX.
a7ec1775
RS
469;; Information on AUCTeX can be found here:
470;;
471;; http://www.sunsite.auc.dk/auctex/
472;;
396e0b08
KH
473;; Instead of using the RefTeX functions described above directly, you can
474;; also use them indirectly, through AUCTeX (version 9.8a or later).
475;; RefTeX provides several interface functions which can be used as
476;; replacement for corresponding AUCTeX functions dealing with labels and
477;; citations. In this way you can work normally with AUCTeX and use RefTeX
c52bdfca 478;; internals to create and complete labels and citation keys.
a7ec1775 479;;
c52bdfca 480;; `reftex-label' can be used as the `LaTeX-label-function' which does
396e0b08 481;; label insertion when new environments are created with `C-c C-e'.
a7ec1775 482;;
c52bdfca 483;; `reftex-arg-label', `reftex-arg-ref' and `reftex-arg-cite' can replace
396e0b08
KH
484;; the corresponding `TeX-arg-...' functions. E.g. when you insert a label
485;; macro with `C-c RET label RET', RefTeX will be transparently used to
486;; create the label.
a7ec1775 487;;
396e0b08 488;; In order to plug all 4 functions into AUCTeX, use:
c52bdfca
RS
489;;
490;; (setq reftex-plug-into-AUCTeX t)
491;;
396e0b08
KH
492;; You may also choose to plug in only some of these functions. See the
493;; docstring of `reftex-plug-into-AUCTeX'.
c52bdfca
RS
494;;
495;; AUCTeX can support RefTeX via style files. A style file may contain
496;; calls to `reftex-add-to-label-alist' which defines additions to
396e0b08
KH
497;; `reftex-label-alist'. The argument taken by this function must have the
498;; same format as `reftex-label-alist'. The `amsmath.el' style file of
499;; AUCTeX (>9.7p) for example contains the following:
025bb635
RS
500;;
501;; (TeX-add-style-hook "amsmath"
502;; (function
503;; (lambda ()
504;; (if (featurep 'reftex)
505;; (reftex-add-to-label-alist '(AMSTeX))))))
506;;
507;; while a package `myprop' defining a proposition environment with
508;; \newtheorem might use
509;;
510;; (TeX-add-style-hook "myprop"
511;; (function
512;; (lambda ()
513;; (if (featurep 'reftex)
514;; (reftex-add-to-label-alist
396e0b08 515;; '(("proposition" ?p "prop:" "~\\ref{%s}" t
025bb635 516;; ("Proposition" "Prop."))))))))
a7ec1775
RS
517;;
518;; Bib-cite.el
519;; -----------
396e0b08
KH
520;; Once you have written a document with labels, refs and citations, it can
521;; be nice to read such a file like a hypertext document. RefTeX has some
522;; support for that (`reftex-view-crossref', `reftex-search-document'). A
523;; more elegant interface with mouse support and links into Hyperbole is
524;; provided (among other things) by Peter S. Galbraith's `bib-cite.el'.
525;; There is some overlap in the functionalities of Bib-cite and RefTeX.
526;; Bib-cite.el comes bundled with AUCTeX. You can also get the latest
527;; version from
a7ec1775
RS
528;;
529;; ftp://ftp.phys.ocean.dal.ca/users/rhogee/elisp/bib-cite.el
396e0b08 530;;---------------------------------------------------------------------------
a7ec1775 531;;
396e0b08
KH
532;; KNOWN BUGS AND WORK-AROUNDS
533;; ===========================
a7ec1775 534;;
396e0b08
KH
535;; o \input, \include, \bibliography and \section (etc.) statements have
536;; to be first on a line (except for white space).
a7ec1775 537;;
396e0b08
KH
538;; o RefTeX sees also labels in regions commented out and will refuse to
539;; make duplicates of such a label. This is considered to be a feature.
a7ec1775 540;;
396e0b08
KH
541;; o When using partial scans (`reftex-enable-partial-scans'), the section
542;; numbers in the table of contents may eventually become wrong. A full
543;; scan will fix this.
a7ec1775 544;;
396e0b08 545;; o RefTeX keeps only a global copy of the configuration variables.
206c6f82 546;; Also, any additions from style files go into a global variable.
a7ec1775
RS
547;; Practically, this should not be a problem. Theoretically, it could
548;; give conflicts if two documents used environments with identical
549;; names, but different associated label types.
206c6f82 550;;
396e0b08
KH
551;; o When using packages which make the buffer representation of a file
552;; different from its disk representation (e.g. x-symbol, isotex,
553;; iso-cvt) you may find that RefTeX's parsing information sometimes
554;; reflects the disk state of a file. This happens only in *unvisited*
555;; parts of a multifile document, because RefTeX visits these files
556;; literally for speed reasons. Then both short context and section
557;; headings may look different from what you usually see on your screen.
558;; In rare cases `reftex-toc' may have problems to jump to an affected
559;; section heading. There are three possible ways to deal with this:
a7ec1775 560;;
396e0b08
KH
561;; - (setq reftex-keep-temporary-buffers t)
562;; This implies that RefTeX will load all parts of a multifile
563;; document into Emacs (i.e. there will be no temporary buffers).
564;; - (setq reftex-initialize-temporary-buffers t)
565;; This means full initialization of temporary buffers. It involves
566;; a penalty when the same file is used for lookup often.
567;; - Set `reftex-initialize-temporary-buffers' to a list of hook
568;; functions doing a minimal initialization.
569;;
570;; You might also want to check the variable `reftex-refontify-context'.
a7ec1775 571;;
396e0b08
KH
572;; o Some nasty :-# packages use an additional argument to a \begin macro
573;; to specify a label. E.g. Lamport's "pf.sty" uses both
a7ec1775 574;;
396e0b08
KH
575;; \step{LABEL}{CLAIM} and \begin{step+}{LABEL}
576;; CLAIM
577;; \end{step+}
206c6f82 578;;
396e0b08 579;; We need to trick RefTeX into swallowing this:
a7ec1775 580;;
396e0b08
KH
581;; ;; Configuration for Lamport's pf.sty
582;; (setq reftex-label-alist
583;; '(("\\step{*}{}" ?p "st:" "~\\stepref{%s}" 2 ("Step" "St."))
584;; ("\\begin{step+}{*}" ?p "st:" "~\\stepref{%s}" 1000)))
585;;
586;; The first line is just a normal configuration for a macro. For the
587;; `step+' environment we actually tell RefTeX to look for the *macro*
588;; "\begin{step+}" and interprete the *first* argument (which in reality
589;; is a second argument to the macro \begin) as a label of type ?p.
590;; Argument count for this macro starts only after the {step+}, also
591;; when specifying how to get context.
592;;
593;; o In XEmacs 19.15, the overlay library has a bug. RefTeX does not
594;; suffer from it, but since it loads the library, other packages like
595;; GNUS will switch from extents to overlays and hit the bug. Upgrade
596;; to XEmacs 20, or fix the overlay library (in line 180 of overlay.el,
597;; change `(list before after)' to `(cons before after)').
a7ec1775
RS
598;;---------------------------------------------------------------------------
599;;
600;; AUTHOR
396e0b08 601;; ======
a7ec1775
RS
602;;
603;; Carsten Dominik <dominik@strw.LeidenUniv.nl>
604;;
605;; with contributions from Stephen Eglen
606;;
607;; The newest version of RefTeX can be found at
608;;
609;; http://www.strw.leidenuniv.nl/~dominik/Tools/
610;; ftp://strw.leidenuniv.nl/pub/dominik/
611;;
612;; THANKS TO:
613;; ---------
614;; At least the following people have invested time to test and bug-fix
396e0b08
KH
615;; reftex.el. Some have send patches for fixes or new features, or came
616;; up with useful ideas.
a7ec1775
RS
617;;
618;; Stephen Eglen <stephene@cogs.susx.ac.uk>
396e0b08 619;; F.E. Burstall <F.E.Burstall@maths.bath.ac.uk>
a7ec1775
RS
620;; Karl Eichwalder <ke@ke.Central.DE>
621;; Laurent Mugnier <mugnier@onera.fr>
622;; Rory Molinari <molinari@yunt.math.lsa.umich.edu>
623;; Soren Dayton <csdayton@cs.uchicago.edu>
624;; Daniel Polani <polani@Informatik.Uni-Mainz.DE>
625;; Allan Strand <astrand@trillium.NMSU.Edu>
206c6f82
RS
626;; Adrian Lanz <lanz@waho.ethz.ch>
627;; Jan Vroonhof <vroonhof@math.ethz.ch>
396e0b08
KH
628;; Alastair Burt <alastair.burt@dfki.de>
629;; Dieter Kraft <dkraft@acm.org>
630;; Robin S. Socha <r.socha@franck.pc.uni-koeln.de>
a7ec1775 631;;
396e0b08 632;; The view crossref feature was inspired by the similar function in
a7ec1775 633;; Peter S. Galbraith's bib-cite.el.
396e0b08 634;;
a7ec1775
RS
635;; Finally thanks to Uwe Bolick <bolick@physik.tu-berlin.de> who first
636;; got me (some years ago) into supporting LaTeX labels and references
637;; with an Editor (which was MicroEmacs at the time).
025bb635 638;;
c52bdfca
RS
639;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
640;;
a7ec1775
RS
641\f
642;;; Code:
643
396e0b08
KH
644(eval-when-compile (require 'cl))
645
a7ec1775
RS
646;; Stuff that needs to be there when we use defcustom
647;; --------------------------------------------------
648
649(require 'custom)
650
651(defvar reftex-tables-dirty t
652 "Flag showing if tables need to be re-computed.")
653
654(eval-and-compile
655 (defun reftex-set-dirty (symbol value)
656 (setq reftex-tables-dirty t)
657 (set symbol value)))
658
396e0b08
KH
659(eval-and-compile
660 (defmacro reftex-fp (n)
661 (if (fboundp 'forward-point)
662 (list 'forward-point n)
663 (list '+ '(point) n))))
664
a7ec1775
RS
665;;; Begin of Configuration Section ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
666
396e0b08
KH
667;; Define the two constants which are needed during compilation
668
669(eval-and-compile
670(defconst reftex-label-alist-builtin
671 '(
672 ;; Some aliases, mostly for backward compatibility
673 (Sideways "Alias for -->rotating" (rotating))
674 (AMSTeX "amsmath with eqref macro"
675 ((nil ?e nil "~\\eqref{%s}")
676 amsmath))
677
678 ;; Individual package defaults
679 (amsmath "AMS-LaTeX math environments"
680 (("align" ?e nil nil eqnarray-like)
681 ("gather" ?e nil nil eqnarray-like)
682 ("multline" ?e nil nil t)
683 ("flalign" ?e nil nil eqnarray-like)
684 ("alignat" ?e nil nil alignat-like)
685 ("xalignat" ?e nil nil alignat-like)
686 ("xxalignat" ?e nil nil alignat-like)
687 ("subequations" ?e nil nil t)))
688
689 (endnotes "The \\endnote macro"
690 (("\\endnote[]{}" ?n nil nil 2 ("Endnote"))))
691
692 (fancybox "The Beqnarray environment"
693 (("Beqnarray" ?e nil nil eqnarray-like)))
694
695 (floatfig "The floatingfigure environment"
696 (("floatingfigure" ?f nil nil caption)))
697
698 (longtable "The longtable environment"
699 (("longtable" ?t nil nil caption)))
700
701 (picinpar "The figwindow and tabwindow environments"
702 (("figwindow" ?f nil nil 1)
703 ("tabwindow" ?f nil nil 1)))
704
705 (rotating "Sidewaysfigure and table"
706 (("sidewaysfigure" ?f nil nil caption)
707 ("sidewaystable" ?t nil nil caption)))
708
709 (subfigure "Subfigure environments/macro"
710 (("subfigure" ?f nil nil caption)
711 ("subfigure*" ?f nil nil caption)
712 ("\\subfigure[]{}" ?f nil nil 1)))
713
714 (supertab "Supertabular environment"
715 (("supertabular" ?t nil nil "\\tablecaption{")))
716
717 (wrapfig "The wrapfigure environment"
718 (("wrapfigure" ?f nil nil caption)))
719
720 ;; The LaTeX core stuff
721 (LaTeX "LaTeX default environments"
722 (("section" ?s "sec:" "~\\ref{%s}" (nil . t)
723 ("Part" "Chapter" "Chap." "Section" "Sec." "Sect." "Paragraph" "Par."
724 "\\S" "Teil" "Kapitel" "Kap." "Abschnitt" ))
725
726 ("enumerate" ?i "item:" "~\\ref{%s}" item
727 ("Item" "Punkt"))
728
729 ("equation" ?e "eq:" "~(\\ref{%s})" t
730 ("Equation" "Eq." "Eqn." "Gleichung" "Gl."))
731 ("eqnarray" ?e "eq:" nil eqnarray-like)
732
733 ("figure" ?f "fig:" "~\\ref{%s}" caption
734 ("Figure" "Fig." "Abbildung" "Abb."))
735 ("figure*" ?f nil nil caption)
736
737 ("table" ?t "tab:" "~\\ref{%s}" caption
738 ("Table" "Tab." "Tabelle"))
739 ("table*" ?t nil nil caption)
740
741 ("\\footnote[]{}" ?n "note:" "~\\ref{%s}" 2
742 ("Footnote" "Note"))
743
744 ("any" ?\ " " "\\ref{%s}" nil)))
745
746 )
747 "The default label environment descriptions.
748Lower-case symbols correspond to a style file of the same name in the LaTeX
749distribution. Mixed-case symbols are convenience aliases.")
750
751(defconst reftex-cite-format-builtin
752 '(
753 (default "Default macro \\cite{%l}"
754 "\\cite{%l}")
755 (natbib "The Natbib package"
756 ((?\C-m . "\\cite{%l}")
757 (?t . "\\citet{%l}")
758 (?T . "\\citet*{%l}")
759 (?p . "\\citep{%l}")
760 (?P . "\\citep*{%l}")
761 (?e . "\\citep[e.g.][]{%l}")
762 (?a . "\\citeauthor{%l}")
763 (?y . "\\citeyear{%l}")))
764 (harvard "The Harvard package"
765 ((?\C-m . "\\cite{%l}")
766 (?p . "\\cite{%l}")
767 (?t . "\\citeasnoun{%l}")
768 (?n . "\\citeasnoun{%l}")
769 (?s . "\\possessivecite{%l}")
770 (?e . "\\citeaffixed{%l}{?}")
771 (?y . "\\citeyear{%l}")
772 (?a . "\\citename{%l}")))
773 (chicago "The Chicago package"
774 ((?\C-m . "\\cite{%l}")
775 (?t . "\\citeN{%l}")
776 (?T . "\\shortciteN{%l}")
777 (?p . "\\cite{%l}")
778 (?P . "\\shortcite{%l}")
779 (?a . "\\citeA{%l}")
780 (?A . "\\shortciteA{%l}")
781 (?y . "\\citeyear{key}")))
782 (astron "The Astron package"
783 ((?\C-m . "\\cite{%l}")
784 (?p . "\\cite{%l}" )
785 (?t . "%2a (\\cite{%l})")))
786 (author-year "Do-it-yourself Author-year"
787 ((?\C-m . "\\cite{%l}")
788 (?t . "%2a (%y)\\nocite{%l}")
789 (?p . "(%2a %y\\nocite{%l})")))
790 (locally "Full info in parenthesis"
791 "(%2a %y, %j %v, %P, %e: %b, %u, %s %<)")
792 ;; undocumented feature: `%<' kills white space and punctuation locally.
793 )
794 "Builtin versions of for the citation format.
795The following conventions are valid for all alist entries:
796`?\C-m' should always point to a straight \\cite{%l} macro.
797`?t' should point to a textual citation (citation as a noun).
798`?p' should point to a parenthetical citation.")
799)
800
a7ec1775
RS
801;; Configuration Variables and User Options for RefTeX ------------------
802
803(defgroup reftex nil
804 "LaTeX label and citation support."
805 :tag "RefTeX"
396e0b08
KH
806 :link '(url-link :tag "Home Page"
807 "http://strw.leidenuniv.nl/~dominik/Tools/")
808 :link '(emacs-commentary-link :tag "Commentary in reftex.el" "reftex.el")
a7ec1775
RS
809 :prefix "reftex-"
810 :group 'tex)
811
812(defun reftex-customize ()
813 "Call the customize function with reftex as argument."
814 (interactive)
206c6f82
RS
815 ;; Depending on the customize version we can call different functions.
816 (cond
817 ((fboundp 'customize-browse)
818 (customize-browse 'reftex))
819 ((fboundp 'customize-group)
820 (customize-group 'reftex))
396e0b08
KH
821 ((fboundp 'customize)
822 (customize 'reftex))
823 (t (error "Custom.el not available"))))
206c6f82
RS
824
825(defun reftex-show-commentary ()
826 "Use the finder to view the file documentation from `reftex.el'."
827 (interactive)
828 (require 'finder)
829 (finder-commentary "reftex.el"))
a7ec1775 830
fba437e6 831;; Support for \label and \ref --------------------------------------
a7ec1775
RS
832
833(defgroup reftex-label-support nil
c52bdfca 834 "Support for creation, insertion and referencing of labels in LaTeX."
a7ec1775
RS
835 :group 'reftex)
836
837(defgroup reftex-defining-label-environments nil
c52bdfca 838 "Definition of environments and macros to do with label."
a7ec1775
RS
839 :group 'reftex-label-support)
840
396e0b08
KH
841;; Make a constant for the customization stuff
842(eval-and-compile
843 (defconst reftex-tmp
844 '((const :tag "Default position" t)
845 (const :tag "After label" nil)
846 (number :tag "Macro arg nr" 1)
847 (regexp :tag "Regexp" "")
848 (const :tag "Caption in float" caption)
849 (const :tag "Item in list" item)
850 (const :tag "Eqnarray-like" eqnarray-like)
851 (const :tag "Alignat-like" alignat-like)
852 (symbol :tag "Function" my-func))))
853
854(defcustom reftex-default-label-alist-entries
855 '(amsmath endnotes fancybox floatfig longtable picinpar
856 rotating subfigure supertab wrapfig LaTeX)
857 "Default label alist specifications. LaTeX should be the last entry.
858This list describes the default label environments RefTeX should always use.
859It is probably a mistake to remove the LaTeX symbol from this list.
860
861The options include:
862LaTeX The standard LaTeX environments.
863Sideways The sidewaysfigure and sidewaystable environments.
864AMSTeX The math environments in the AMS-LaTeX amsmath package.
865
866For the full list of options, try
867
868M-x customize-variable RET reftex-default-label-alist-entries RET."
869 :group 'reftex-defining-label-environments
870 :set 'reftex-set-dirty
871 :type `(set
872 :indent 4
873 :inline t
874 :greedy t
875 ,@(mapcar
876 (function
877 (lambda (x)
878 (list 'const ':tag (concat (symbol-name (nth 0 x))
879 ": " (nth 1 x))
880 (nth 0 x))))
881 reftex-label-alist-builtin)))
882
a7ec1775 883(defcustom reftex-label-alist nil
fba437e6 884 "Alist with information on environments for \\label-\\ref use.
a7ec1775 885
396e0b08
KH
886This docstring is easier to understand after reading the configuration
887examples in `reftex.el'. Looking at the builtin defaults in the constant
888`reftex-label-alist-builtin' may also be instructive.
889
890Set this variable to define additions and changes to the default. The only
891things you MUST NOT change is that `?s' is the type indicator for section
892labels, and SPC for the `any' label type. These are hard-coded at other
893places in the code.
a7ec1775 894
396e0b08
KH
895Each list entry describes either an environment carrying a counter for use
896with \\label and \\ref, or a LaTeX macro defining a label as (or inside)
897one of its arguments. The elements of each list entry are:
a7ec1775
RS
898
8990. Name of the environment (like \"table\") or macro (like \"\\\\myfig\").
396e0b08
KH
900 For macros, indicate the macro arguments for best results, as in
901 \"\\\\myfig[]{}{}{*}{}\". Use square brackets for optional arguments,
902 a star to mark the label argument, if any. The macro does not have to
903 have a label argument - you could also use \\label{..} inside one of
904 its arguments.
a7ec1775
RS
905 Special names: `section' for section labels, `any' to define a group
906 which contains all labels.
396e0b08 907 This may also be nil if the entry is only meant to change some settings
a7ec1775
RS
908 associated with the type indicator character (see below).
909
396e0b08
KH
9101. Type indicator character, like `?t', must be a printable ASCII character.
911 The type indicator is a single character which defines a label type.
912 Any label inside the environment or macro is assumed to belong to this
913 type. The same character may occur several times in this list, to cover
914 cases in which different environments carry the same label type (like
915 `equation' and `eqnarray').
a7ec1775
RS
916
9172. Label prefix string, like \"tab:\".
f52b232e 918 The prefix is a short string used as the start of a label. It may be the
396e0b08 919 empty string. The prefix may contain the following `%' escapes:
206c6f82
RS
920 %f Current file name with directory and extension stripped.
921 %F Current file name relative to directory of master file.
922 %u User login name, on systems which support this.
923
924 Example: In a file `intro.tex', \"eq:%f:\" will become \"eq:intro:\").
a7ec1775 925
206c6f82
RS
9263. Format string for reference insert in buffer. `%s' will be replaced by
927 the label.
c52bdfca
RS
928 When the format starts with `~', whitespace before point will be removed
929 so that the reference cannot be separated from the word before it.
a7ec1775
RS
930
9314. Indication on how to find the short context.
f52b232e
RS
932 - If nil, use the text following the \\label{...} macro.
933 - If t, use
a7ec1775 934 - the section heading for section labels.
396e0b08
KH
935 - text following the \\begin{...} statement of environments.
936 (not a good choice for environments like eqnarray or enumerate,
937 where one has several labels in a single environment).
938 - text after the macro name (stearting with the first arg) for macros.
939 - If an integer, use the nth argument of the macro. As a special case,
940 1000 means to get text after the last macro argument.
f52b232e
RS
941 - If a string, use as regexp to search *backward* from the label. Context
942 is then the text following the end of the match. E.g. putting this to
396e0b08
KH
943 \"\\\\\\\\caption{\" will use the caption in a figure or table
944 environment.
c52bdfca 945 \"\\\\\\\\begin{eqnarray}\\\\|\\\\\\\\\\\\\\\\\" works for eqnarrays.
396e0b08
KH
946 - If any of `caption', `item', `eqnarray-like', `alignat-like', this
947 symbol will internally be translated into an appropriate regexp
948 (see also the variable `reftex-default-context-regexps').
a7ec1775 949 - If a function, call this function with the name of the environment/macro
f52b232e
RS
950 as argument. On call, point will be just after the \\label macro. The
951 function is expected to return a suitable context string. It should
a7ec1775 952 throw an exception (error) when failing to find context.
206c6f82
RS
953 As an example, here is a function returning the 10 chars following
954 the label macro as context:
a7ec1775
RS
955
956 (defun my-context-function (env-or-mac)
957 (if (> (point-max) (+ 10 (point)))
958 (buffer-substring (point) (+ 10 (point)))
959 (error \"Buffer too small\")))
960
206c6f82
RS
961 Label context is used in two ways by RefTeX: For display in the label
962 menu, and to derive a label string. If you want to use a different
963 method for each of these, specify them as a dotted pair.
964 E.g. `(nil . t)' uses the text after the label (nil) for display, and
965 text from the default position (t) to derive a label string. This is
966 actually used for section labels.
967
f52b232e
RS
968 Setting the variable `reftex-use-text-after-label-as-context' to t
969 overrides the setting here.
a7ec1775 970
c52bdfca
RS
9715. List of magic words which identify a reference to be of this type.
972 If the word before point is equal to one of these words when calling
206c6f82
RS
973 `reftex-reference', the label list offered will be automatically
974 restricted to labels of the correct type.
a7ec1775
RS
975
976If the type indicator characters of two or more entries are the same, RefTeX
977will use
978 - the first non-nil format and prefix
979 - the magic words of all involved entries.
980
f52b232e 981Any list entry may also be a symbol. If that has an association in
c52bdfca 982`reftex-label-alist-builtin', the cdr of that association is spliced into the
396e0b08
KH
983list. However, builtin defaults should normally be set here but with the
984variable `reftex-default-label-alist-entries."
a7ec1775
RS
985 :group 'reftex-defining-label-environments
986 :set 'reftex-set-dirty
206c6f82 987 :type
396e0b08
KH
988 `(repeat
989 (choice
990 :value ("" ?a nil nil nil nil)
991 (list :tag "Detailed label alist entry"
992 :value ("" ?a nil nil nil nil)
993 (choice :tag "Environment or \\macro "
994 (const :tag "Ignore, just use typekey" nil)
995 (string ""))
996 (character :tag "Typekey character " ?a)
997 (choice :tag "Label prefix string "
998 (const :tag "Default" nil)
999 (string :tag "String" "lab:"))
1000 (choice :tag "Label reference format"
1001 (const :tag "Default" nil)
1002 (string :tag "String" "~\\ref{%s}"))
1003 (choice :tag "Context"
1004 (choice
1005 :tag "1 method"
1006 ,@reftex-tmp)
1007 (cons :tag "Split methods"
1008 (choice
1009 :tag " Display context "
1010 ,@reftex-tmp)
1011 (choice
1012 :tag " Derive label context"
1013 ,@reftex-tmp)))
1014 (repeat :tag "List of Magic Words" (string)))
1015 (choice
1016 :tag "Package"
206c6f82 1017 :value AMSTeX
396e0b08
KH
1018 ,@(mapcar
1019 (function
1020 (lambda (x)
1021 (list 'const ':tag (concat (symbol-name (nth 0 x))); ": " (nth 1 x))
1022 (nth 0 x))))
1023 reftex-label-alist-builtin)))))
a7ec1775
RS
1024
1025(defcustom reftex-use-text-after-label-as-context nil
1026 "*t means, grab context from directly after the \\label{..} macro.
1027This is the fastest method for obtaining context of the label definition, but
c52bdfca
RS
1028requires discipline when placing labels. Setting this variable to t takes
1029precedence over the individual settings in `reftex-label-alist'.
a7ec1775
RS
1030This variable may be set to t, nil, or a string of label type letters
1031indicating the label types for which it should be true."
1032 :group 'reftex-defining-label-environments
1033 :set 'reftex-set-dirty
1034 :type '(choice
396e0b08
KH
1035 (const :tag "on" t) (const :tag "off" nil)
1036 (string :tag "Selected label types")))
a7ec1775
RS
1037
1038;; Label insertion
1039
1040(defgroup reftex-making-and-inserting-labels nil
c52bdfca 1041 "Options on how to create new labels."
a7ec1775
RS
1042 :group 'reftex-label-support)
1043
1044(defcustom reftex-insert-label-flags '("s" "sft")
c52bdfca 1045 "Flags governing label insertion. First flag DERIVE, second flag PROMPT.
a7ec1775
RS
1046
1047If DERIVE is t, RefTeX will try to derive a sensible label from context.
1048A section label for example will be derived from the section heading.
1049The conversion of the context to a legal label is governed by the
c52bdfca 1050specifications given in `reftex-derive-label-parameters'.
a7ec1775 1051If RefTeX fails to derive a label, it will prompt the user.
396e0b08
KH
1052If DERIVE is nil, the label generated will consist of the prefix and a
1053unique number, like `eq:23'.
a7ec1775 1054
c52bdfca 1055If PROMPT is t, the user will be prompted for a label string. The prompt will
a7ec1775
RS
1056already contain the prefix, and (if DERIVE is t) a default label derived from
1057context. When PROMPT is nil, the default label will be inserted without
1058query.
1059
c52bdfca 1060So the combination of DERIVE and PROMPT controls label insertion. Here is a
a7ec1775
RS
1061table describing all four possibilities:
1062
1063DERIVE PROMPT ACTION
1064-------------------------------------------------------------------------
c52bdfca
RS
1065 nil nil Insert simple label, like eq:22 or sec:13. No query.
1066 nil t Prompt for label.
1067 t nil Derive a label from context and insert without query.
1068 t t Derive a label from context and prompt for confirmation.
a7ec1775
RS
1069
1070Each flag may be set to t, nil, or a string of label type letters
1071indicating the label types for which it should be true.
c52bdfca 1072Thus, the combination may be set differently for each label type. The
a7ec1775 1073default settings \"s\" and \"sft\" mean: Derive section labels from headings
c52bdfca 1074(with confirmation). Prompt for figure and table labels. Use simple labels
a7ec1775
RS
1075without confirmation for everything else."
1076 :group 'reftex-making-and-inserting-labels
1077 :type '(list (choice :tag "Derive label from context"
1078 (const :tag "always" t)
1079 (const :tag "never" nil)
206c6f82 1080 (string :tag "selected label types" ""))
a7ec1775
RS
1081 (choice :tag "Prompt for label string "
1082 :entry-format " %b %v"
1083 (const :tag "always" t)
1084 (const :tag "never" nil)
206c6f82 1085 (string :tag "selected label types" ""))))
a7ec1775 1086
396e0b08 1087(defcustom reftex-derive-label-parameters '(3 20 t 1 "-"
a7ec1775
RS
1088 ("the" "on" "in" "off" "a" "for" "by" "of" "and" "is"))
1089 "Parameters for converting a string into a label.
1090NWORDS Number of words to use.
1091MAXCHAR Maximum number of characters in a label string.
1092ILLEGAL nil: Throw away any words containing characters illegal in labels.
1093 t: Throw away only the illegal characters, not the whole word.
1094ABBREV nil: Never abbreviate words.
c52bdfca 1095 t: Always abbreviate words (see `reftex-abbrev-parameters').
a7ec1775
RS
1096 not t and not nil: Abbreviate words if necessary to shorten
1097 label string below MAXCHAR.
c52bdfca
RS
1098SEPARATOR String separating different words in the label.
1099IGNOREWORDS List of words which should not be part of labels."
a7ec1775
RS
1100 :group 'reftex-making-and-inserting-labels
1101 :type '(list (integer :tag "Number of words " 3)
396e0b08
KH
1102 (integer :tag "Maximum label length " 20)
1103 (choice :tag "Illegal characters in words"
1104 (const :tag "throw away entire word" nil)
1105 (const :tag "throw away single chars" t))
1106 (choice :tag "Abbreviate words "
1107 (const :tag "never" nil)
1108 (const :tag "always" t)
1109 (const :tag "when label is too long" 1))
1110 (string :tag "Separator between words " "-")
1111 (repeat :tag "Ignore words"
1112 :entry-format " %i %d %v"
1113 (string :tag ""))))
1114
a7ec1775
RS
1115(defcustom reftex-label-illegal-re "[\000-\040\177-\377\\\\#$%&~^_{}]"
1116 "Regexp matching characters not legal in labels.
1117For historic reasons, this character class comes *with* the [] brackets."
1118 :group 'reftex-making-and-inserting-labels
1119 :type '(regexp :tag "Character class"))
1120
1121(defcustom reftex-abbrev-parameters '(4 2 "^saeiou" "aeiou")
1122 "Parameters for abbreviation of words.
c52bdfca
RS
1123MIN-CHARS Minimum number of characters remaining after abbreviation.
1124MIN-KILL Minimum number of characters to remove when abbreviating words.
1125BEFORE Character class before abbrev point in word.
1126AFTER Character class after abbrev point in word."
a7ec1775
RS
1127 :group 'reftex-making-and-inserting-labels
1128 :type '(list
396e0b08
KH
1129 (integer :tag "Minimum chars per word" 4)
1130 (integer :tag "Shorten by at least " 2)
1131 (string :tag "cut before char class " "^saeiou")
1132 (string :tag "cut after char class " "aeiou")))
a7ec1775 1133
a7ec1775
RS
1134;; Label referencing
1135
1136(defgroup reftex-referencing-labels nil
c52bdfca 1137 "Options on how to reference labels."
a7ec1775
RS
1138 :group 'reftex-label-support)
1139
396e0b08
KH
1140(eval-and-compile
1141 (defconst reftex-tmp
1142 '((const :tag "on" t)
1143 (const :tag "off" nil)
1144 (string :tag "Selected label types"))))
1145
1146(defcustom reftex-label-menu-flags '(t t nil nil nil nil t nil)
1147 "List of flags governing the label menu makeup.
a7ec1775
RS
1148The flags are:
1149
1150TABLE-OF-CONTENTS Show the labels embedded in a table of context.
1151SECTION-NUMBERS Include section numbers (like 4.1.3) in table of contents.
c52bdfca 1152COUNTERS Show counters. This just numbers the labels in the menu.
a7ec1775 1153NO-CONTEXT Non-nil means do NOT show the short context.
c52bdfca 1154FOLLOW Follow full context in other window.
206c6f82
RS
1155SHOW-COMMENTED Show labels from regions which are commented out.
1156MATCH-IN-TOC Searches in label menu will also match in toc lines.
396e0b08 1157SHOW FILES Show Begin and end of included files.
a7ec1775
RS
1158
1159Each of these flags can be set to t or nil, or to a string of type letters
c52bdfca
RS
1160indicating the label types for which it should be true. These strings work
1161like character classes in regular expressions. Thus, setting one of the
a7ec1775 1162flags to \"sf\" makes the flag true for section and figure labels, nil
c52bdfca 1163for everything else. Setting it to \"^ft\" makes it the other way round.
a7ec1775
RS
1164
1165Most options can also be switched from the label menu itself - so if you
1166decide here to not have a table of contents in the label menu, you can still
1167get one interactively during selection from the label menu."
1168 :group 'reftex-referencing-labels
396e0b08
KH
1169 :type
1170 `(list
1171 (choice :tag "Embed in table of contents " ,@reftex-tmp)
1172 (choice :tag "Show section numbers " ,@reftex-tmp)
1173 (choice :tag "Show individual counters " ,@reftex-tmp)
1174 (choice :tag "Hide short context " ,@reftex-tmp)
1175 (choice :tag "Follow context in other window " ,@reftex-tmp)
1176 (choice :tag "Show commented labels " ,@reftex-tmp)
1177 (choice :tag "Searches match in toc lines " ,@reftex-tmp)
1178 (choice :tag "Show begin/end of included files" ,@reftex-tmp)))
1179
1180(defcustom reftex-level-indent 2
1181 "*Number of spaces to be used for indentation per section level."
1182 :group 'reftex-referencing-labels
1183 :type '(integer))
1184
1185(defcustom reftex-refontify-context 1
1186 "*Non-nil means, re-fontify the context in the label menu with font-lock.
1187This slightly slows down the creation of the label menu. It is only necessay
1188when you definitely want the context fontified.
1189
1190This option may have 3 different values:
1191nil Never refontify.
1192t Always refontify.
11931 Refontify when absolutly necessary, e.g. when with the x-symbol package.
1194The option is ignored when `reftex-use-fonts' is nil."
1195 :group 'reftex-referencing-labels
1196 :type '(choice
1197 (const :tag "Never" nil)
1198 (const :tag "Always" t)
1199 (const :tag "When necessary" 1)))
a7ec1775
RS
1200
1201(defcustom reftex-guess-label-type t
c52bdfca 1202 "*Non-nil means, `reftex-reference' will try to guess the label type.
a7ec1775 1203To do that, RefTeX will look at the word before the cursor and compare it with
c52bdfca 1204the words given in `reftex-label-alist'. When it finds a match, RefTeX will
a7ec1775 1205immediately offer the correct label menu - otherwise it will prompt you for
c52bdfca 1206a label type. If you set this variable to nil, RefTeX will always prompt."
a7ec1775
RS
1207 :group 'reftex-referencing-labels
1208 :type '(boolean))
1209
1210;; BibteX citation configuration ----------------------------------------
1211
1212(defgroup reftex-citation-support nil
c52bdfca 1213 "Support for referencing bibliographic data with BibTeX."
a7ec1775
RS
1214 :group 'reftex)
1215
1216(defcustom reftex-bibpath-environment-variables '("BIBINPUTS" "TEXBIB")
1217 "*List of env vars which might contain the path to BibTeX database files."
1218 :group 'reftex-citation-support
1219 :set 'reftex-set-dirty
1220 :type '(repeat (string :tag "Environment variable")))
1221
1222(defcustom reftex-bibfile-ignore-list nil
396e0b08 1223 "*List of files in \\bibliography{..} RefTeX should *not* parse.
a7ec1775 1224The file names have to be in the exact same form as in the bibliography
c52bdfca 1225macro - i.e. without the `.bib' extension.
a7ec1775
RS
1226Intended for files which contain only `@string' macro definitions and the
1227like, which are ignored by RefTeX anyway."
1228 :group 'reftex-citation-support
1229 :set 'reftex-set-dirty
1230 :type '(repeat (string :tag "File name")))
1231
1232(defcustom reftex-sort-bibtex-matches 'reverse-year
1233 "*Sorting of the entries found in BibTeX databases by reftex-citation.
1234Possible values:
1235nil Do not sort entries.
1236'author Sort entries by author name.
1237'year Sort entries by increasing year.
1238'reverse-year Sort entries by decreasing year."
1239 :group 'reftex-citation-support
1240 :type '(choice (const :tag "not" nil)
396e0b08
KH
1241 (const :tag "by author" author)
1242 (const :tag "by year" year)
1243 (const :tag "by year, reversed" reverse-year)))
a7ec1775 1244
206c6f82 1245(defcustom reftex-cite-format 'default
396e0b08 1246 "*The format of citations to be inserted into the buffer.
206c6f82
RS
1247It can be a string or an alist. In the simplest case this is just
1248the string \"\\cite{%l}\", which is also the default. See the
1249definition of `reftex-cite-format-builtin' for more complex examples.
1250
1251If `reftex-cite-format' is a string, it will be used as the format.
1252In the format, the following percent escapes will be expanded.
1253
1254%l The BibTeX label of the citation.
1255%a List of author names, see also `reftex-cite-punctuation.
1256%2a Like %a, but abbreviate more than 2 authors like Jones et al.
1257%A First author name only.
1258%e Works like %a, but on list of editor names. (%2e and %E work a well)
1259
1260It is also possible to access all other BibTeX database fields:
1261%b booktitle %c chapter %d edition %h howpublished
1262%i institution %j journal %k key %m month
1263%n number %o organization %p pages %P first page
1264%r address %s school %u publisher %t title
1265%v volume %y year
1266
1267Usually, only %l is needed. Try, however, (setq reftex-comment-citations t).
1268
1269If `reftex-cite-format' is an alist of characters and strings, the user
1270will be prompted for a character to select one of the possible format
1271strings.
a7ec1775 1272 In order to configure this variable, you can either set
c52bdfca 1273`reftex-cite-format' directly yourself or set it to the SYMBOL of one of
206c6f82
RS
1274the predefined styles (see `reftex-cite-format-builtin'). E.g.:
1275(setq reftex-cite-format 'harvard)"
396e0b08
KH
1276 :group 'reftex-citation-support
1277 :type
1278 `(choice
1279 :format "%{%t%}: \n%[Value Menu%] %v"
1280 (radio :tag "Symbolic Builtins"
1281 :indent 4
1282 :value default
1283 ,@(mapcar
1284 (function
1285 (lambda (x)
1286 (list 'const ':tag (concat (symbol-name (nth 0 x))
1287 ": " (nth 1 x))
1288 (nth 0 x))))
1289 reftex-cite-format-builtin))
1290 (string :tag "format string" "\\cite{%l}")
1291 (repeat :tag "key-ed format strings"
1292 :value ((?\r . "\\cite{%l}")
1293 (?t . "\\cite{%l}") (?p . "\\cite{%l}"))
1294 (cons (character :tag "Key character" ?\r)
1295 (string :tag "Format string" "")))))
206c6f82
RS
1296
1297(defcustom reftex-comment-citations nil
396e0b08 1298 "*Non-nil means add a comment for each citation describing the full entry.
206c6f82
RS
1299The comment is formatted according to `reftex-cite-comment-format'."
1300 :group 'reftex-citation-support
1301 :type '(boolean))
1302
396e0b08 1303(defcustom reftex-cite-comment-format
206c6f82
RS
1304 "%% %2a %y, %j %v, %P, %e: %b, %u, %s %<\n"
1305 "Citation format used for commented citations. Must NOT contain %l."
1306 :group 'reftex-citation-support
1307 :type '(string))
1308
1309(defcustom reftex-cite-punctuation '(", " " \\& " " {\\it et al.}")
1310 "Punctuation for formatting of name lists in citations.
1311This is a list of 3 strings.
13121. normal names separator, like \", \" in Jones, Brown and Miller
13132. final names separator, like \" and \" in Jones, Brown and Miller
13143. The \"et al\" string, like \" {...}\" in Jones {\\it et al.}"
396e0b08 1315 :group 'reftex-citation-support
206c6f82 1316 :type '(list
396e0b08
KH
1317 (string :tag "Separator for names ")
1318 (string :tag "Separator for last name in list")
1319 (string :tag "string used as et al. ")))
a7ec1775
RS
1320
1321;; Table of contents configuration --------------------------------------
1322
1323(defgroup reftex-table-of-contents-browser nil
1324 "A multifile table of contents browser."
1325 :group 'reftex)
1326
1327(defcustom reftex-toc-follow-mode nil
396e0b08 1328 "*Non-nil means, point in *toc* buffer will cause other window to follow.
a7ec1775
RS
1329The other window will show the corresponding part of the document.
1330This flag can be toggled from within the *toc* buffer with the `f' key."
1331 :group 'reftex-table-of-contents-browser
1332 :type '(boolean))
1333
396e0b08
KH
1334;; Tuning the parser ----------------------------------------------------
1335
1336(defgroup reftex-optimizations-for-large-documents nil
1337 "Configuration of parser speed and memory usage."
1338 :group 'reftex)
1339
1340(defcustom reftex-keep-temporary-buffers 1
1341 "*Non-nil means, keep buffers created for parsing and lookup.
1342RefTeX sometimes needs to visit files related to the current document.
1343We distinguish files visited for
1344PARSING: Parts of a multifile document loaded when (re)-parsing the document.
1345LOOKUP: BibTeX database files and TeX files loaded to find a reference,
1346 to display label context, etc.
1347The created buffers can be kept for later use, or be thrown away immediately
1348after use, depending on the value of this variable:
1349
1350nil Throw away as much as possible.
1351t Keep everything.
13521 Throw away buffers created for parsing, but keep the ones created
1353 for lookup.
1354
1355If a buffer is to be kept, the file is visited normally (which is potentially
1356slow but will happen only once).
1357If a buffer is to be thrown away, the initialization of the buffer depends
1358upon the variable `reftex-initialize-temporary-buffers'."
1359 :group 'reftex-miscellaneous-configurations
1360 :type '(choice
1361 (const :tag "Throw away everything" nil)
1362 (const :tag "Keep everything" t)
1363 (const :tag "Keep lookup buffers only" 1)))
1364
1365(defcustom reftex-initialize-temporary-buffers nil
1366 "*Non-nil means do initializations even when visiting file temporarily.
1367When nil, RefTeX may turn off find-file hooks and other stuff to briefly
1368visit a file.
1369When t, the full default initializations are done (find-file-hook etc.).
1370Instead of t or nil, this variable may also be a list of hook functions to
1371do a minimal initialization."
1372 :group 'reftex-miscellaneous-configurations
1373 :type '(choice
1374 (const :tag "Read files literally" nil)
1375 (const :tag "Fully initialize buffers" t)
1376 (repeat :tag "Hook functions" :value (nil)
1377 (function-item))))
1378
1379(defcustom reftex-enable-partial-scans nil
1380 "*Non-nil means, re-parse only 1 file when asked to re-parse.
1381Re-parsing is normally requested with a `C-u' prefix to many RefTeX commands,
1382or with the `r' key in menus. When this option is t in a multifile document,
1383we will only parse the current buffer, or the file associated with the label
1384or section heading near point in a menu. Requesting re-parsing of an entire
1385multifile document then requires a `C-u C-u' prefix or the capital `R' key
1386in menus."
1387 :group 'reftex-optimizations-for-large-documents
1388 :type 'boolean)
1389
1390(defcustom reftex-save-parse-info nil
1391 "*Non-nil means, save information gathered with parsing in a file.
1392The file MASTER.rel in the same directory as MASTER.tex is used to save the
1393information. When this variable is t,
1394- accessing the parsing information for the first time in an editing session
1395 will read that file (if available) instead of parsing the document.
1396- each time (part of) the document is rescanned, a new version of the file
1397 is written."
1398 :group 'reftex-optimizations-for-large-documents
1399 :type 'boolean)
1400
a7ec1775
RS
1401;; Miscellaneous configurations -----------------------------------------
1402
1403(defgroup reftex-miscellaneous-configurations nil
c52bdfca 1404 "Collection of further configurations."
a7ec1775
RS
1405 :group 'reftex)
1406
1407(defcustom reftex-extra-bindings nil
1408 "Non-nil means, make additional key bindings on startup.
c52bdfca 1409These extra bindings are located in the users `C-c letter' map."
a7ec1775 1410 :group 'reftex-miscellaneous-configurations
396e0b08 1411 :type '(boolean))
a7ec1775 1412
c52bdfca 1413(defcustom reftex-plug-into-AUCTeX nil
396e0b08 1414 "*Plug-in flags for AUCTeX interface.
c52bdfca
RS
1415This variable is a list of 4 boolean flags. When a flag is non-nil, it
1416means:
1417
1418 Flag 1: use `reftex-label' as `LaTeX-label-function'.
1419 Flag 2: use `reftex-arg-label' as `TeX-arg-label'
1420 Flag 3: use `reftex-arg-ref' as `TeX-arg-ref'
1421 Flag 4: use `reftex-arg-cite' as `TeX-arg-cite'
1422
1423You may also set the variable itself to t or nil in order to turn all
1424plug-ins on or off, respectively.
1425\\<LaTeX-mode-map>`LaTeX-label-function' is the function used for label insertion when you
1426enter a new environment in AUCTeX with \\[LaTeX-environment].
1427The `TeX-arg-label' etc. functions are for entering macro arguments during
1428macro insertion with \\[TeX-insert-macro].
1429See the AUCTeX documentation for more information.
1430RefTeX uses `fset' to take over the function calls. Changing the variable
1431may require a restart of Emacs in order to become effective."
1432 :group 'reftex-miscellaneous-configurations
1433 :type '(choice (const :tag "No plug-ins" nil)
396e0b08
KH
1434 (const :tag "All possible plug-ins" t)
1435 (list
1436 :tag "Individual choice"
1437 :value (nil nil nil nil)
1438 (boolean :tag "Use reftex-label as LaTeX-label-function")
1439 (boolean :tag "Use reftex-arg-label as TeX-arg-label ")
1440 (boolean :tag "Use reftex-arg-ref as TeX-arg-ref ")
1441 (boolean :tag "Use reftex-arg-cite as TeX-arg-cite ")
1442 )))
c52bdfca 1443
a7ec1775
RS
1444(defcustom reftex-use-fonts t
1445 "*Non-nil means, use fonts in label menu and on-the-fly help.
1446Font-lock must be loaded as well to actually get fontified display."
1447 :group 'reftex-miscellaneous-configurations
1448 :type '(boolean))
1449
396e0b08
KH
1450(defcustom reftex-auto-show-entry 'copy
1451 "*Non-nil means, do something when context in other window is hidden.
1452Some modes like `outline-mode' or `folding-mode' hide parts of buffers.
1453When RefTeX is asked to show context for a label definition, and the context
1454is invisible, it can unhide that section permanently (value t), or copy the
1455context to a temporary buffer (value 'copy)."
a7ec1775 1456 :group 'reftex-miscellaneous-configurations
396e0b08
KH
1457 :type '(radio :value copy
1458 :indent 4
1459 (const :tag "Do nothing" nil)
1460 (const :tag "Unhide section permanently" t)
1461 (const :tag "Copy context to show" copy)))
1462
1463(defcustom reftex-load-hook nil
1464 "Hook which is being run when loading reftex.el."
1465 :group 'reftex-miscellaneous-configurations
1466 :type 'hook)
a7ec1775 1467
396e0b08
KH
1468(defcustom reftex-mode-hook nil
1469 "Hook which is being run when turning on RefTeX mode."
a7ec1775 1470 :group 'reftex-miscellaneous-configurations
396e0b08 1471 :type 'hook)
a7ec1775 1472
a7ec1775
RS
1473;;; End of Configuration Section ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1474
1475;;;===========================================================================
1476;;;
1477;;; Define the formal stuff for a minor mode named RefTeX.
1478;;;
1479
1480(defvar reftex-mode nil
1481 "Determines if RefTeX minor mode is active.")
1482(make-variable-buffer-local 'reftex-mode)
1483
1484(defvar reftex-mode-map (make-sparse-keymap)
1485 "Keymap for RefTeX minor mode.")
1486
1487(defvar reftex-mode-menu nil)
1488
1489;;;###autoload
1490(defun turn-on-reftex ()
1491 "Turn on RefTeX minor mode."
1492 (reftex-mode t))
1493
1494;;;###autoload
1495(defun reftex-mode (&optional arg)
fba437e6 1496 "Minor mode with distinct support for \\label, \\ref and \\cite in LaTeX.
a7ec1775
RS
1497
1498Labels can be created with `\\[reftex-label]' and referenced with `\\[reftex-reference]'.
1499When referencing, you get a menu with all labels of a given type and
c52bdfca 1500context of the label definition. The selected label is inserted as a
fba437e6 1501\\ref macro.
a7ec1775 1502
396e0b08 1503Citations can be made with `\\[reftex-citation]' which will use a regular expression
a7ec1775 1504to pull out a *formatted* list of articles from your BibTeX
c52bdfca 1505database. The selected citation is inserted as a \\cite macro.
a7ec1775
RS
1506
1507A Table of Contents of the entire (multifile) document with browsing
1508capabilities is available with `\\[reftex-toc]'.
1509
c52bdfca 1510Most command have help available on the fly. This help is accessed by
a7ec1775
RS
1511pressing `?' to any prompt mentioning this feature.
1512
396e0b08
KH
1513Extensive documentation about RefTeX is in the file header of `reftex.el'.
1514You can view this information with `\\[reftex-show-commentary]'.
c52bdfca 1515
a7ec1775 1516\\{reftex-mode-map}
396e0b08
KH
1517Under X, these and other functions will also be available as `Ref' menu
1518on the menu bar.
a7ec1775
RS
1519
1520------------------------------------------------------------------------------"
1521
1522 (interactive "P")
1523 (setq reftex-mode (not (or (and (null arg) reftex-mode)
1524 (<= (prefix-numeric-value arg) 0))))
1525
1526 ; Add or remove the menu, and run the hook
1527 (if reftex-mode
1528 (progn
396e0b08
KH
1529 (easy-menu-add reftex-mode-menu)
1530 (reftex-plug-into-AUCTeX)
1531 (run-hooks 'reftex-mode-hook))
a7ec1775 1532 (easy-menu-remove reftex-mode-menu)))
396e0b08 1533
a7ec1775 1534(or (assoc 'reftex-mode minor-mode-alist)
396e0b08 1535 (push '(reftex-mode " Ref") minor-mode-alist))
a7ec1775
RS
1536
1537(or (assoc 'reftex-mode minor-mode-map-alist)
396e0b08 1538 (push (cons 'reftex-mode reftex-mode-map) minor-mode-map-alist))
c52bdfca
RS
1539
1540;;; ===========================================================================
1541;;;
1542;;; Silence warnings about variables in other packages.
1543(defvar TeX-master)
1544(defvar LaTeX-label-function)
1545(defvar tex-main-file)
1546(defvar outline-minor-mode)
1547
a7ec1775
RS
1548;;; ===========================================================================
1549;;;
1550;;; Interfaces for other packages
1551;;; -----------------------------
1552;;;
1553;;; AUCTeX
1554;;; ------
1555
396e0b08 1556(defun reftex-arg-label (optional &optional prompt definition)
c52bdfca 1557 "Use `reftex-label' to create label. Insert it with `TeX-argument-insert'.
a7ec1775
RS
1558This function is intended for AUCTeX macro support."
1559 (let ((label (reftex-label nil t)))
396e0b08
KH
1560 (if (and definition (not (string-equal "" label)))
1561 (LaTeX-add-labels label))
a7ec1775
RS
1562 (TeX-argument-insert label optional optional)))
1563
396e0b08 1564(defun reftex-arg-ref (optional &optional prompt definition)
c52bdfca 1565 "Use `reftex-reference' to select label. Insert with `TeX-argument-insert'.
a7ec1775
RS
1566This function is intended for AUCTeX macro support."
1567 (let ((label (reftex-reference nil t)))
396e0b08
KH
1568 (if (and definition (not (string-equal "" label)))
1569 (LaTeX-add-labels label))
a7ec1775
RS
1570 (TeX-argument-insert label optional optional)))
1571
396e0b08 1572(defun reftex-arg-cite (optional &optional prompt definition)
c52bdfca 1573 "Use reftex-citation to select a key. Insert with `TeX-argument-insert'.
a7ec1775 1574This function is intended for AUCTeX macro support."
396e0b08 1575 (let ((key (reftex-citation t)))
a7ec1775
RS
1576 (TeX-argument-insert (or key "") optional optional)))
1577
c52bdfca
RS
1578(defun reftex-plug-into-AUCTeX ()
1579 ;; Replace AucTeX functions with RefTeX functions.
1580 ;; Which functions are replaced is controlled by the variable
1581 ;; `reftex-plug-into-AUCTeX'.
396e0b08
KH
1582 (let ((flags
1583 (cond ((eq reftex-plug-into-AUCTeX t) '(t t t t))
1584 ((eq reftex-plug-into-AUCTeX nil) '(nil nil nil nil))
1585 (t reftex-plug-into-AUCTeX))))
c52bdfca
RS
1586
1587 (and (nth 0 flags)
396e0b08
KH
1588 (boundp 'LaTeX-label-function)
1589 (setq LaTeX-label-function 'reftex-label))
c52bdfca
RS
1590
1591 (and (nth 1 flags)
396e0b08
KH
1592 (fboundp 'TeX-arg-label)
1593 (fset 'TeX-arg-label 'reftex-arg-label))
c52bdfca
RS
1594
1595 (and (nth 2 flags)
396e0b08
KH
1596 (fboundp 'TeX-arg-ref)
1597 (fset 'TeX-arg-ref 'reftex-arg-ref))
c52bdfca
RS
1598
1599 (and (nth 3 flags)
396e0b08
KH
1600 (fboundp 'TeX-arg-cite)
1601 (fset 'TeX-arg-cite 'reftex-arg-cite))))
1602
c52bdfca 1603
a7ec1775
RS
1604(defvar reftex-label-alist-external-add-ons nil
1605 "List of label alist entries added with reftex-add-to-label-alist.")
1606
a7ec1775 1607(defun reftex-add-to-label-alist (entry-list)
c52bdfca 1608 "Add label environment descriptions to `reftex-label-alist-external-add-ons'.
396e0b08 1609The format of ENTRY-LIST is exactly like `reftex-label-alist'. See there
a7ec1775
RS
1610for details.
1611This function makes it possible to support RefTeX from AUCTeX style files.
1612The entries in ENTRY-LIST will be processed after the user settings in
c52bdfca
RS
1613`reftex-label-alist', and before the defaults (specified in
1614`reftex-default-label-alist-entries'). Any changes made to
1615`reftex-label-alist-external-add-ons' will raise a flag to the effect that a
a7ec1775
RS
1616mode reset is done on the next occasion."
1617 (let (entry)
1618 (while entry-list
1619 (setq entry (car entry-list)
1620 entry-list (cdr entry-list))
396e0b08
KH
1621 (unless (member entry reftex-label-alist-external-add-ons)
1622 (setq reftex-tables-dirty t)
1623 (push entry reftex-label-alist-external-add-ons)))))
a7ec1775
RS
1624
1625;;; ===========================================================================
1626;;;
1627;;; Multifile support
1628;;;
1629;;; Technical notes: Multifile works as follows: We keep just one list
1630;;; of labels for each master file - this can save a lot of memory.
c52bdfca 1631;;; `reftex-master-index-list' is an alist which connects the true file name
a7ec1775 1632;;; of each master file with the symbols holding the information on that
c52bdfca 1633;;; document. Each buffer has local variables which point to these symbols.
a7ec1775 1634
a7ec1775
RS
1635;; List of variables which handle the multifile stuff.
1636;; This list is used to tie, untie, and reset these symbols.
1637(defconst reftex-multifile-symbols
396e0b08 1638 '(reftex-docstruct-symbol))
a7ec1775 1639
c52bdfca 1640;; Alist connecting master file names with the corresponding lisp symbols.
a7ec1775
RS
1641(defvar reftex-master-index-list nil)
1642
c52bdfca 1643;; Last index used for a master file.
a7ec1775
RS
1644(defvar reftex-multifile-index 0)
1645
a7ec1775 1646;; Variable holding the symbol with the label list of the document.
396e0b08
KH
1647(defvar reftex-docstruct-symbol nil)
1648(make-variable-buffer-local 'reftex-docstruct-symbol)
a7ec1775
RS
1649
1650(defun reftex-next-multifile-index ()
1651 ;; Return the next free index for multifile symbols.
396e0b08 1652 (incf reftex-multifile-index))
a7ec1775
RS
1653
1654(defun reftex-tie-multifile-symbols ()
1655 ;; Tie the buffer-local symbols to globals connected with the master file.
1656 ;; If the symbols for the current master file do not exist, they are created.
1657
1658 (let* ((master (file-truename (reftex-TeX-master-file)))
1659 (index (assoc master reftex-master-index-list))
1660 (symlist reftex-multifile-symbols)
1661 (symbol nil)
1662 (symname nil)
1663 (newflag nil))
c52bdfca 1664 ;; Find the correct index.
a7ec1775
RS
1665 (if index
1666 ;; symbols do exist
1667 (setq index (cdr index))
c52bdfca 1668 ;; Get a new index and add info to the alist.
a7ec1775 1669 (setq index (reftex-next-multifile-index)
396e0b08
KH
1670 newflag t)
1671 (push (cons master index) reftex-master-index-list))
a7ec1775 1672
c52bdfca 1673 ;; Get/create symbols and tie them.
a7ec1775
RS
1674 (while symlist
1675 (setq symbol (car symlist)
1676 symlist (cdr symlist)
1677 symname (symbol-name symbol))
1678 (set symbol (intern (concat symname "-" (int-to-string index))))
c52bdfca 1679 ;; Initialize if new symbols.
a7ec1775
RS
1680 (if newflag (set (symbol-value symbol) nil)))
1681
c52bdfca 1682 ;; Return t if the symbols did already exist, nil when we've made them.
a7ec1775
RS
1683 (not newflag)))
1684
1685(defun reftex-untie-multifile-symbols ()
1686 ;; Remove ties from multifile symbols, so that next use makes new ones.
1687 (let ((symlist reftex-multifile-symbols)
1688 (symbol nil))
1689 (while symlist
1690 (setq symbol (car symlist)
1691 symlist (cdr symlist))
1692 (set symbol nil))))
1693
1694(defun reftex-TeX-master-file ()
1695 ;; Return the name of the master file associated with the current buffer.
1696 ;; When AUCTeX is loaded, we will use it's more sophisticated method.
025bb635
RS
1697 ;; We also support the default TeX and LaTeX modes by checking for a
1698 ;; variable tex-main-file.
a7ec1775
RS
1699
1700 (let
1701 ((master
1702 (cond
c52bdfca 1703 ((fboundp 'TeX-master-file) ; AUCTeX is loaded. Use its mechanism.
a7ec1775
RS
1704 (TeX-master-file t))
1705 ((boundp 'TeX-master) ; The variable is defined - lets use it.
1706 (cond
1707 ((eq TeX-master t)
1708 (buffer-file-name))
1709 ((eq TeX-master 'shared)
1710 (setq TeX-master (read-file-name "Master file: "
1711 nil nil t nil)))
1712 (TeX-master)
1713 (t
1714 (setq TeX-master (read-file-name "Master file: "
1715 nil nil t nil)))))
1716 ((boundp 'tex-main-file)
c52bdfca 1717 ;; This is the variable from the default TeX modes.
a7ec1775
RS
1718 (cond
1719 ((stringp tex-main-file)
1720 ;; ok, this must be it
1721 tex-main-file)
1722 (t
c52bdfca 1723 ;; In this case, the buffer is its own master.
a7ec1775
RS
1724 (buffer-file-name))))
1725 (t
c52bdfca 1726 ;; Know nothing about master file. Assume this is a master file.
a7ec1775
RS
1727 (buffer-file-name)))))
1728 (cond
1729 ((null master)
c52bdfca 1730 (error "Need a filename for this buffer. Please save it first."))
a7ec1775
RS
1731 ((or (file-exists-p master)
1732 (reftex-get-buffer-visiting master))
c52bdfca 1733 ;; We either see the file, or have a buffer on it. OK.
a7ec1775
RS
1734 )
1735 ((or (file-exists-p (concat master ".tex"))
1736 (reftex-get-buffer-visiting (concat master ".tex")))
1737 ;; Ahh, an extra .tex was missing...
1738 (setq master (concat master ".tex")))
1739 (t
c52bdfca 1740 ;; Something is wrong here. Throw an exception.
a7ec1775
RS
1741 (error "No such master file %s" master)))
1742 (expand-file-name master)))
1743
396e0b08
KH
1744(defun reftex-parse-one ()
1745 "Re-parse this file."
a7ec1775 1746 (interactive)
396e0b08
KH
1747 (let ((reftex-enable-partial-scans t))
1748 (reftex-access-scan-info '(4))))
a7ec1775 1749
396e0b08
KH
1750(defun reftex-parse-all ()
1751 "Re-parse entire document."
1752 (interactive)
1753 (reftex-access-scan-info '(16)))
1754
1755(defun reftex-all-document-files (&optional relative)
1756 ;; Return a list of all files belonging to the current document.
1757 ;; When RELATIVE is non-nil, give file names relative to directory
1758 ;; of master file.
1759 (let* ((all (symbol-value reftex-docstruct-symbol))
1760 (master-dir (file-name-directory (reftex-TeX-master-file)))
1761 (re (concat "\\`" (regexp-quote master-dir)))
1762 file-list tmp file)
1763 (while (setq tmp (assoc 'bof all))
1764 (setq file (nth 1 tmp)
1765 all (cdr (memq tmp all)))
1766 (and relative
1767 (string-match re file)
1768 (setq file (substring file (match-end 0))))
1769 (push file file-list))
1770 (nreverse file-list)))
a7ec1775
RS
1771
1772(defun reftex-create-tags-file ()
1773 "Create TAGS file by running `etags' on the current document.
c52bdfca 1774The TAGS file is also immediately visited with `visit-tags-table'."
a7ec1775
RS
1775 (interactive)
1776 (reftex-access-scan-info current-prefix-arg)
1777 (let* ((master (reftex-TeX-master-file))
396e0b08
KH
1778 (files (reftex-all-document-files))
1779 (cmd (format "etags %s" (mapconcat 'identity files " "))))
a7ec1775
RS
1780 (save-excursion
1781 (set-buffer (reftex-get-buffer-visiting master))
1782 (message "Running etags to create TAGS file...")
1783 (shell-command cmd)
1784 (visit-tags-table "TAGS"))))
1785
1786;; History of grep commands.
1787(defvar reftex-grep-history nil)
1788(defvar reftex-grep-command "grep -n "
1789 "Last grep command used in \\[reftex-grep-document]; default for next grep.")
1790
1791(defun reftex-grep-document (grep-cmd)
c52bdfca 1792 "Run grep query through all files related to this document.
a7ec1775
RS
1793With prefix arg, force to rescan document.
1794This works also without an active TAGS table."
1795
1796 (interactive
1797 (list (read-from-minibuffer "Run grep on document (like this): "
396e0b08 1798 reftex-grep-command nil nil
a7ec1775
RS
1799 'reftex-grep-history)))
1800 (reftex-access-scan-info current-prefix-arg)
396e0b08
KH
1801 (let* ((files (reftex-all-document-files t))
1802 (cmd (format
1803 "%s %s" grep-cmd
1804 (mapconcat 'identity files " "))))
a7ec1775
RS
1805 (grep cmd)))
1806
1807(defun reftex-search-document (&optional regexp)
1808 "Regexp search through all files of the current TeX document.
c52bdfca 1809Starts always in the master file. Stops when a match is found.
a7ec1775
RS
1810To continue searching for next match, use command \\[tags-loop-continue].
1811This works also without an active TAGS table."
1812 (interactive)
1813 (let ((default (reftex-this-word)))
396e0b08
KH
1814 (unless regexp
1815 (setq regexp (read-string (format "Search regexp in document [%s]: "
1816 default))))
a7ec1775
RS
1817 (if (string= regexp "") (setq regexp (regexp-quote default)))
1818
1819 (reftex-access-scan-info current-prefix-arg)
396e0b08 1820 (tags-search regexp (list 'reftex-all-document-files))))
a7ec1775
RS
1821
1822(defun reftex-query-replace-document (&optional from to delimited)
1823 "Run a query-replace-regexp of FROM with TO over the entire TeX document.
1824Third arg DELIMITED (prefix arg) means replace only word-delimited matches.
1825If you exit (\\[keyboard-quit] or ESC), you can resume the query replace
1826with the command \\[tags-loop-continue].
1827This works also without an active TAGS table."
1828 (interactive)
1829 (let ((default (reftex-this-word)))
396e0b08
KH
1830 (unless from
1831 (setq from (read-string (format "Replace regexp in document [%s]: "
1832 default)))
1833 (if (string= from "") (setq from (regexp-quote default))))
1834 (unless to
1835 (setq to (read-string (format "Replace regexp %s with: " from))))
a7ec1775
RS
1836 (reftex-access-scan-info current-prefix-arg)
1837 (tags-query-replace from to (or delimited current-prefix-arg)
396e0b08 1838 (list 'reftex-all-document-files))))
a7ec1775
RS
1839
1840(defun reftex-change-label (&optional from to)
fba437e6 1841 "Query replace FROM with TO in all \\label and \\ref commands.
a7ec1775
RS
1842Works on the entire multifile document.
1843If you exit (\\[keyboard-quit] or ESC), you can resume the query replace
1844with the command \\[tags-loop-continue].
1845This works also without an active TAGS table."
1846 (interactive)
1847 (let ((default (reftex-this-word "-a-zA-Z0-9_*.:")))
396e0b08
KH
1848 (unless from
1849 (setq from (read-string (format "Replace label globally [%s]: "
1850 default))))
a7ec1775 1851 (if (string= from "") (setq from default))
396e0b08
KH
1852 (unless to
1853 (setq to (read-string (format "Replace label %s with: "
1854 from))))
a7ec1775
RS
1855 (reftex-query-replace-document
1856 (concat "\\\\\\(label\\|[a-z]*ref\\){" (regexp-quote from) "}")
1857 (format "\\\\\\1{%s}" to))))
1858
a7ec1775
RS
1859;;; ===========================================================================
1860;;;
c52bdfca 1861;;; Functions to create and reference automatic labels.
a7ec1775 1862
c52bdfca 1863;; The following constants are derived from `reftex-label-alist'.
a7ec1775 1864
c52bdfca 1865;; Prompt used for label type querys directed to the user.
a7ec1775
RS
1866(defconst reftex-type-query-prompt nil)
1867
c52bdfca 1868;; Help string for label type querys.
a7ec1775
RS
1869(defconst reftex-type-query-help nil)
1870
c52bdfca 1871;; Alist relating label type to reference format.
a7ec1775
RS
1872(defconst reftex-typekey-to-format-alist nil)
1873
c52bdfca 1874;; Alist relating label type to label affix.
a7ec1775
RS
1875(defconst reftex-typekey-to-prefix-alist nil)
1876
c52bdfca 1877;; Alist relating environments or macros to label type and context regexp.
a7ec1775
RS
1878(defconst reftex-env-or-mac-alist nil)
1879
c52bdfca 1880;; List of macros carrying a label.
a7ec1775
RS
1881(defconst reftex-label-mac-list nil)
1882
c52bdfca 1883;; List of environments carrying a label.
a7ec1775
RS
1884(defconst reftex-label-env-list nil)
1885
c52bdfca 1886;; List of all typekey letters in use.
a7ec1775
RS
1887(defconst reftex-typekey-list nil)
1888
c52bdfca 1889;; Alist relating magic words to a label type.
a7ec1775
RS
1890(defconst reftex-words-to-typekey-alist nil)
1891
c52bdfca 1892;; The last list-of-labels entry used in a reference.
a7ec1775
RS
1893(defvar reftex-last-used-reference (list nil nil nil nil))
1894
c52bdfca 1895;; The regular expression used to abbreviate words.
a7ec1775
RS
1896(defconst reftex-abbrev-regexp
1897 (concat
396e0b08 1898 "\\`\\("
a7ec1775
RS
1899 (make-string (nth 0 reftex-abbrev-parameters) ?.)
1900 "[" (nth 2 reftex-abbrev-parameters) "]*"
1901 "\\)"
1902 "[" (nth 3 reftex-abbrev-parameters) "]"
1903 (make-string (1- (nth 1 reftex-abbrev-parameters)) ?.)))
1904
c52bdfca 1905;; Global variables used for communication between functions.
a7ec1775
RS
1906(defvar reftex-default-context-position nil)
1907(defvar reftex-location-start nil)
1908(defvar reftex-call-back-to-this-buffer nil)
396e0b08
KH
1909(defvar reftex-active-toc nil)
1910(defvar reftex-tex-path nil)
1911(defvar reftex-bib-path nil)
1912
1913;; Internal list with index numbers of labels in the selection menu
1914(defvar reftex-label-index-list)
a7ec1775 1915
c52bdfca 1916;; List of buffers created temporarily for lookup, which should be killed.
a7ec1775
RS
1917(defvar reftex-buffers-to-kill nil)
1918
206c6f82
RS
1919;; Regexp to find section statements. Computed from reftex-section-levels.
1920(defvar reftex-section-regexp nil)
396e0b08
KH
1921(defvar reftex-section-or-include-regexp nil)
1922(defvar reftex-everything-regexp nil)
1923(defvar reftex-find-label-regexp-format nil)
1924(defvar reftex-find-label-regexp-format2 nil)
a7ec1775
RS
1925
1926;; LaTeX section commands and level numbers
206c6f82 1927(defcustom reftex-section-levels
a7ec1775
RS
1928 '(
1929 ("part" . 0)
1930 ("chapter" . 1)
1931 ("section" . 2)
1932 ("subsection" . 3)
1933 ("subsubsection" . 4)
1934 ("paragraph" . 5)
1935 ("subparagraph" . 6)
1936 ("subsubparagraph" . 7)
206c6f82
RS
1937 )
1938 "Commands and levels used for defining sections in the document.
396e0b08 1939The car of each cons cell is the name of the section macro. The cdr is a
206c6f82
RS
1940number indicating its level."
1941 :group 'reftex-defining-label-environments
1942 :set 'reftex-set-dirty
1943 :type '(repeat
396e0b08
KH
1944 (cons (string :tag "sectioning macro" "")
1945 (number :tag "level " 0))))
1946
1947;; The parser functions ----------------------------------
1948
1949(defvar reftex-memory nil
1950 "Memorizes old variable values to indicate changes in these variables.")
1951
1952(defun reftex-access-scan-info (&optional rescan file)
1953 ;; Access the scanning info. When the multifile symbols are not yet tied,
1954 ;; tie them. When they are empty or RESCAN is non-nil, scan the document.
1955
1956 ;; Reset the mode if we had changes to important variables.
1957 (when (or reftex-tables-dirty
1958 (not (eq reftex-label-alist (nth 0 reftex-memory)))
1959 (not (eq reftex-label-alist-external-add-ons
1960 (nth 1 reftex-memory)))
1961 (not (eq reftex-default-label-alist-entries
1962 (nth 2 reftex-memory))))
1963 (reftex-reset-mode))
1964
1965 (if (eq reftex-docstruct-symbol nil)
1966 ;; Symbols are not yet tied: Tie them.
1967 (reftex-tie-multifile-symbols))
1968
1969 (if (and (null (symbol-value reftex-docstruct-symbol))
1970 reftex-save-parse-info)
1971 ;; Try to read the stuff from a file
1972 (reftex-access-parse-file 'read))
1973
1974 (cond
1975 ((not (symbol-value reftex-docstruct-symbol))
1976 (reftex-do-parse 1 file))
1977 ((member rescan '(t 1 (4) (16)))
1978 (reftex-do-parse rescan file))))
1979
1980(defun reftex-do-parse (rescan &optional file)
1981 ;; Access the scanning info. When the multifile symbols are not yet tied,
1982 ;; tie them. When they are have to be created, do a buffer scan to
1983 ;; fill them.
1984
1985 ;; If RESCAN is non-nil, enforce document scanning
1986
1987 ;; Normalize the rescan argument
1988 (setq rescan (cond ((eq rescan t) t)
1989 ((eq rescan 1) 1)
1990 ((equal rescan '(4)) t)
1991 ((equal rescan '(16)) 1)
1992 (t 1)))
1993
1994 ;; Partial scans only when allowed
1995 (unless reftex-enable-partial-scans
1996 (setq rescan 1))
1997
1998 ;; Do the scanning.
1999
2000 (let* ((old-list (symbol-value reftex-docstruct-symbol))
2001 (master (reftex-TeX-master-file))
2002 (master-dir (file-name-as-directory (file-name-directory master)))
2003 (file (or file (buffer-file-name)))
2004 from-file
2005 docstruct tmp)
2006
2007 ;; Make sure replacement is really an option here
2008 (when (and (eq rescan t)
2009 (not (and (member (list 'bof file) old-list)
2010 (member (list 'eof file) old-list))))
2011 (message "Scanning whole document (no file section %s)" file)
2012 (setq rescan 1))
2013 (when (string= file master)
2014 (message "Scanning whole document (%s is master)" file)
2015 (setq rescan 1))
2016
2017 ;; From which file do we start?
2018 (setq from-file
2019 (cond ((eq rescan t) (or file master))
2020 ((eq rescan 1) master)
2021 (t (error "horrible!!"))))
2022
2023 ;; Find active toc entry and initialize section-numbers
2024 (setq reftex-active-toc
2025 (reftex-last-assoc-before-elt
2026 'toc (list 'bof from-file) old-list))
2027 (reftex-init-section-numbers reftex-active-toc)
2028
2029 (if (eq rescan 1)
2030 (message "Scanning entire document...")
2031 (message "Scanning document from %s..." from-file))
2032
2033 (save-window-excursion
2034 (save-excursion
2035 (unwind-protect
2036 (setq docstruct
2037 (reftex-parse-from-file
2038 from-file docstruct master-dir))
2039 (reftex-kill-temporary-buffers))))
2040
2041 (message "Scanning document... done")
2042
2043 ;; Turn the list around.
2044 (setq docstruct (nreverse docstruct))
2045
2046 ;; Set or insert
2047 (setq docstruct (reftex-replace-label-list-segment
2048 old-list docstruct (eq rescan 1)))
2049
2050 ;; Add all missing information
2051 (unless (assq 'label-numbers docstruct)
2052 (push (cons 'label-numbers nil) docstruct))
2053 (unless (assq 'master-dir docstruct)
2054 (push (cons 'master-dir master-dir) docstruct))
2055 (let* ((bof1 (memq (assq 'bof docstruct) docstruct))
2056 (bof2 (assq 'bof (cdr bof1)))
2057 (is-multi (not (not (and bof1 bof2))))
2058 (entry (or (assq 'is-multi docstruct)
2059 (car (push (list 'is-multi is-multi) docstruct)))))
2060 (setcdr entry (cons is-multi nil)))
2061 (unless (assq 'xr docstruct)
2062 (let* ((allxr (reftex-all-assq 'xr-doc docstruct))
2063 (alist (mapcar
2064 '(lambda (x)
2065 (if (setq tmp (reftex-find-tex-file (nth 2 x)
2066 master-dir))
2067 (cons (nth 1 x) tmp)
2068 (message "Can't find external document %s"
2069 (nth 2 x))
2070 nil))
2071 allxr))
2072 (alist (delete nil alist))
2073 (allprefix (delete nil (mapcar 'car alist)))
2074 (regexp (concat "\\`\\(" (mapconcat 'identity allprefix "\\|")
2075 "\\)")))
2076 (push (list 'xr alist regexp) docstruct)))
2077
2078 (set reftex-docstruct-symbol docstruct)
2079
2080 ;; Save the parsing informtion into a file?
2081 (if reftex-save-parse-info
2082 (reftex-access-parse-file 'write))))
2083
2084(defun reftex-is-multi ()
2085 ;; Tell if this is a multifile document. When not sure, say yes.
2086 (let ((entry (assq 'is-multi (symbol-value reftex-docstruct-symbol))))
2087 (if entry
2088 (nth 1 entry)
2089 t)))
2090
2091(defun reftex-parse-from-file (file docstruct master-dir)
2092 ;; Scan the buffer for labels and save them in a list.
2093 (let ((regexp reftex-everything-regexp)
2094 (bound 0)
2095 file-found tmp
2096 (level 1)
2097 (highest-level 100)
2098 toc-entry next-buf)
2099
2100 (catch 'exit
2101 (setq file-found (reftex-find-tex-file file master-dir))
2102 (unless file-found
2103 (push (list 'file-error file) docstruct)
2104 (throw 'exit nil))
2105
2106 (save-excursion
2107
2108 (message "Scanning file %s" file)
2109 (set-buffer
2110 (setq next-buf
2111 (reftex-get-file-buffer-force
2112 file-found
2113 (not (eq t reftex-keep-temporary-buffers)))))
2114
2115 ;; Begin of file mark
2116 (setq file (buffer-file-name))
2117 (push (list 'bof file) docstruct)
2118
2119 (save-excursion
2120 (save-restriction
2121 (widen)
2122 (goto-char 1)
2123
2124 (while (re-search-forward regexp nil t)
2125
2126 (cond
2127
2128 ((match-end 1)
2129 ;; It is a label
2130 (push (reftex-label-info (reftex-match-string 1) file bound)
2131 docstruct))
2132
2133 ((match-end 3)
2134 ;; It is a section
2135 (setq bound (point))
2136
2137 ;; Insert in List
2138 (setq toc-entry (reftex-section-info file))
2139 (setq level (nth 5 toc-entry))
2140 (setq highest-level (min highest-level level))
2141 (if (= level highest-level)
2142 (message
2143 "Scanning %s %s ..."
2144 (car (nth level reftex-section-levels))
2145 (nth 6 toc-entry)))
2146
2147 (push toc-entry docstruct)
2148 (setq reftex-active-toc toc-entry))
2149
2150 ((match-end 7)
2151 ;; It's an include or input
2152 (setq docstruct
2153 (reftex-parse-from-file
2154 (reftex-match-string 7)
2155 docstruct master-dir)))
2156
2157 ((match-end 8)
2158 ;; A macro with label
2159 (save-excursion
2160 (let* ((mac (reftex-match-string 8))
2161 (label (progn (goto-char (match-end 8))
2162 (save-match-data
2163 (reftex-no-props
2164 (reftex-nth-arg-wrapper
2165 mac)))))
2166 (entry (progn (goto-char (match-end 0))
2167 (reftex-label-info
2168 label file bound mac))))
2169 (push entry docstruct))))
2170 (t (error "This should not happen (reftex-parse-from-file)")))
2171 )
2172
2173
2174 ;; Find bibliography statement
2175 (when (setq tmp (reftex-locate-bibliography-files master-dir))
2176 (push (cons 'bib tmp) docstruct))
2177
2178 ;; Find external document specifications
2179 (goto-char 1)
2180 (while (re-search-forward "[\n\r][ \t]*\\\\externaldocument\\(\\[\\([^]]*\\)\\]\\)?{\\([^}]+\\)}" nil t)
2181 (push (list 'xr-doc (reftex-match-string 2)
2182 (reftex-match-string 3))
2183 docstruct))
2184
2185 ;; End of file mark
2186 (push (list 'eof file) docstruct))))
2187
2188 ;; Kill the scanned buffer
2189 (reftex-kill-temporary-buffers next-buf))
2190
2191 ;; Return the list
2192 docstruct))
2193
2194(defun reftex-locate-bibliography-files (master-dir)
2195 ;; Scan buffer for bibliography macro and return file list.
2196 (let (file-list)
2197 (save-excursion
2198 (goto-char (point-min))
2199 (if (re-search-forward
2200 "\\(\\`\\|[\n\r]\\)[ \t]*\\\\bibliography{[ \t]*\\([^}]+\\)" nil t)
2201 (setq file-list
2202 (mapcar '(lambda (x) (concat x ".bib"))
2203 (reftex-delete-list
2204 reftex-bibfile-ignore-list
2205 (split-string
2206 (reftex-match-string 2)
2207 "[ \t\n\r]*,[ \t\n\r]*")))))
2208 (delete nil
2209 (mapcar
2210 (function
2211 (lambda (file) (reftex-find-bib-file file master-dir)))
2212 file-list)))))
2213
2214(defun reftex-last-assoc-before-elt (key elt list)
2215 ;; Find the last association of KEY in LIST before or at ELT
2216 ;; ELT is found in LIST with equal, not eq.
2217 ;; Returns nil when either KEY or elt are not found in LIST.
2218 ;; On success, returns the association.
2219 (let* ((elt (car (member elt list))) ass last-ass)
2220
2221 (while (and (setq ass (assoc key list))
2222 (setq list (memq ass list))
2223 (memq elt list))
2224 (setq last-ass ass
2225 list (cdr list)))
2226 last-ass))
2227
2228(defun reftex-replace-label-list-segment (old insert &optional entirely)
2229 ;; Replace the segment in OLD which corresponds to INSERT.
2230 ;; Works with side effects, directly changes old.
2231 ;; If entirely is t, just return INSERT.
2232 ;; This function also makes sure the old toc markers do not point anywhere.
2233
2234 (cond
2235 (entirely
2236 (reftex-silence-toc-markers old (length old))
2237 insert)
2238 (t (let* ((new old)
2239 (file (nth 1 (car insert)))
2240 (eof-list (member (list 'eof file) old))
2241 (bof-list (member (list 'bof file) old))
2242 n)
2243 (if (not (and bof-list eof-list))
2244 (error "Cannot splice")
2245 ;; Splice
2246 (reftex-silence-toc-markers bof-list (- (length bof-list)
2247 (length eof-list)))
2248 (setq n (- (length old) (length bof-list)))
2249 (setcdr (nthcdr n new) (cdr insert))
2250 (setcdr (nthcdr (1- (length new)) new) (cdr eof-list)))
2251 new))))
2252
2253(defun reftex-silence-toc-markers (list n)
2254 ;; Set all markers in list to nil
2255 (while (and list (> (decf n) -1))
2256 (and (eq (car (car list)) 'toc)
2257 (markerp (nth 4 (car list)))
2258 (set-marker (nth 4 (car list)) nil))
2259 (pop list)))
2260
2261(defun reftex-access-parse-file (action)
2262 (let* ((list (symbol-value reftex-docstruct-symbol))
2263 (master (reftex-TeX-master-file))
2264 (enable-local-variables nil)
2265 (file (if (string-match "\\.[a-zA-Z]+\\'" master)
2266 (concat (substring master 0 (match-beginning 0)) ".rel")
2267 (concat master ".rel"))))
2268 (cond
2269 ((eq action 'readable)
2270 (file-readable-p file))
2271 ((eq action 'restore)
2272 (if (eq reftex-docstruct-symbol nil)
2273 ;; Symbols are not yet tied: Tie them.
2274 (reftex-tie-multifile-symbols))
2275 (if (file-exists-p file)
2276 ;; load the file and return t for success
2277 (progn (load-file file) t)
2278 ;; return nil for failure
2279 nil))
2280 ((eq action 'read)
2281 (if (file-exists-p file)
2282 ;; load the file and return t for success
2283 (progn (load-file file) t)
2284 ;; return nil for failure
2285 nil))
2286 (t
2287 (save-excursion
2288 (if (file-writable-p file)
2289 (progn
2290 (message "Writing parse file %s" (abbreviate-file-name file))
2291 (find-file file)
2292 (erase-buffer)
2293 (insert (format ";; RefTeX parse info file\n"))
2294 (insert (format ";; File: %s\n" master))
2295 (insert (format ";; Date: %s\n"
2296 (format-time-string "%D %T"
2297 (current-time))))
2298 (insert (format ";; User: %s (%s)\n\n"
2299 (user-login-name) (user-full-name)))
2300 (insert "(set reftex-docstruct-symbol '(\n\n")
2301 (let ((standard-output (current-buffer)))
2302 (mapcar
2303 (function
2304 (lambda (x)
2305 (cond ((eq (car x) 'toc)
2306 ;; A toc entry. Do not save the marker.
2307 ;; Save the markers position at position 8
2308 (print (list 'toc "toc" (nth 2 x) (nth 3 x)
2309 nil (nth 5 x) (nth 6 x) (nth 7 x)
2310 (or (and (markerp (nth 4 x))
2311 (marker-position (nth 4 x)))
2312 (nth 8 x)))))
2313 (t (print x)))))
2314 list))
2315 (insert "))")
2316 (save-buffer 0)
2317 (kill-buffer (current-buffer)))
2318 (error "Cannot write to file %s" file)))
2319 t))))
2320
2321;; Creating labels --------------
a7ec1775
RS
2322
2323(defun reftex-label (&optional environment no-insert)
c52bdfca 2324 "Insert a unique label. Return the label.
a7ec1775
RS
2325If ENVIRONMENT is given, don't bother to find out yourself.
2326If NO-INSERT is non-nil, do not insert label into buffer.
2327With prefix arg, force to rescan document first.
2328The label is also inserted into the label list.
2329This function is controlled by the settings of reftex-insert-label-flags."
2330
2331 (interactive)
396e0b08 2332
c52bdfca 2333 ;; Ensure access to scanning info and rescan buffer if prefix are is '(4).
a7ec1775
RS
2334 (reftex-access-scan-info current-prefix-arg)
2335
c52bdfca 2336 ;; Find out what kind of environment this is and abort if necessary.
a7ec1775
RS
2337 (if (or (not environment)
2338 (not (assoc environment reftex-env-or-mac-alist)))
2339 (setq environment (reftex-label-location)))
396e0b08
KH
2340 (unless environment
2341 (error "Can't figure out what kind of label should be inserted"))
a7ec1775 2342
c52bdfca 2343 ;; Ok, go ahead.
396e0b08
KH
2344 (let* ((entry (assoc environment reftex-env-or-mac-alist))
2345 (typekey (nth 1 entry))
2346 (format (nth 3 entry))
2347 label prefix valid default force-prompt)
2348 (when (and (eq (string-to-char environment) ?\\)
2349 (nth 4 entry)
2350 (memq (preceding-char) '(?\[ ?\{)))
2351 (setq format "%s"))
2352
a7ec1775
RS
2353 (setq prefix (or (cdr (assoc typekey reftex-typekey-to-prefix-alist))
2354 (concat typekey "-")))
206c6f82
RS
2355 ;; Replace any escapes in the prefix
2356 (setq prefix (reftex-replace-prefix-escapes prefix))
a7ec1775 2357
c52bdfca 2358 ;; Make a default label.
a7ec1775
RS
2359 (cond
2360
2361 ((reftex-typekey-check typekey (nth 0 reftex-insert-label-flags))
c52bdfca 2362 ;; Derive a label from context.
396e0b08
KH
2363 (setq reftex-active-toc (reftex-last-assoc-before-elt
2364 'toc (car (reftex-where-am-I))
2365 (symbol-value reftex-docstruct-symbol)))
2366 (setq default (reftex-no-props
2367 (nth 2 (reftex-label-info " " nil nil t))))
c52bdfca 2368 ;; Catch the cases where the is actually no context available.
a7ec1775
RS
2369 (if (or (string-match "NO MATCH FOR CONTEXT REGEXP" default)
2370 (string-match "ILLEGAL VALUE OF PARSE" default)
2371 (string-match "SECTION HEADING NOT FOUND" default)
2372 (string-match "HOOK ERROR" default)
2373 (string-match "^[ \t]*$" default))
2374 (setq default prefix
2375 force-prompt t) ; need to prompt
2376 (setq default (concat prefix (reftex-string-to-label default)))
2377
c52bdfca 2378 ;; Make it unique.
396e0b08 2379 (setq default (reftex-uniquify-label default nil "-"))))
a7ec1775
RS
2380
2381 ((reftex-typekey-check typekey (nth 1 reftex-insert-label-flags)) ; prompt
c52bdfca 2382 ;; Minimal default: the user will be prompted.
a7ec1775
RS
2383 (setq default prefix))
2384
2385 (t
c52bdfca 2386 ;; Make an automatic label.
206c6f82 2387 (setq default (reftex-uniquify-label prefix t))))
a7ec1775
RS
2388
2389 ;; Should we ask the user?
2390 (if (or (reftex-typekey-check typekey
2391 (nth 1 reftex-insert-label-flags)) ; prompt
2392 force-prompt)
2393
2394 (while (not valid)
2395 ;; iterate until we get a legal label
2396
396e0b08
KH
2397 (setq label (read-string
2398 (if (string= format "%s") "Naked Label: " "Label: ")
2399 default))
a7ec1775
RS
2400
2401 ;; Lets make sure that this is a legal label
2402 (cond
2403
2404 ;; Test if label contains strange characters
2405 ((string-match reftex-label-illegal-re label)
2406 (message "Label \"%s\" contains illegal characters" label)
2407 (ding)
2408 (sit-for 2))
2409
2410 ;; Look it up in the label list
2411 ((setq entry (assoc label
396e0b08 2412 (symbol-value reftex-docstruct-symbol)))
a7ec1775
RS
2413 (message "Label \"%s\" exists in file %s" label (nth 3 entry))
2414 (ding)
2415 (sit-for 2))
2416
2417 ;; Label is ok
2418 (t
2419 (setq valid t))))
2420 (setq label default))
2421
a7ec1775 2422 ;; Insert the label into the label list
396e0b08
KH
2423 (let* ((here-I-am-info (reftex-where-am-I))
2424 (here-I-am (car here-I-am-info))
2425 (note (if (cdr here-I-am-info)
2426 ""
2427 "POSITION UNCERTAIN. RESCAN TO FIX."))
2428 (file (buffer-file-name))
2429 (text nil)
2430 (tail (memq here-I-am (symbol-value reftex-docstruct-symbol))))
2431
2432 (if tail
2433 (setcdr tail (cons (list label typekey text file note)
2434 (cdr tail)))))
2435
2436 ;; Insert the label into the buffer
2437 (unless no-insert
2438 (insert (format format label)))
2439
a7ec1775
RS
2440 ;; return value of the function is the label
2441 label))
2442
2443(defun reftex-string-to-label (string)
2444 ;; Convert a string (a sentence) to a label.
2445 ;;
2446 ;; Uses reftex-derive-label-parameters and reftex-abbrev-parameters
2447 ;;
2448
396e0b08 2449 (let* ((words0 (split-string string "[- \t\n\r]+"))
a7ec1775
RS
2450 (ignore-words (nth 5 reftex-derive-label-parameters))
2451 words word)
2452
2453 ;; remove words from the ignore list or with funny characters
396e0b08 2454 (while (setq word (pop words0))
a7ec1775
RS
2455 (cond
2456 ((member (downcase word) ignore-words))
2457 ((string-match reftex-label-illegal-re word)
396e0b08
KH
2458 (when (nth 2 reftex-derive-label-parameters)
2459 (while (string-match reftex-label-illegal-re word)
2460 (setq word (replace-match "" nil nil word)))
2461 (push word words)))
a7ec1775 2462 (t
396e0b08 2463 (push word words))))
a7ec1775
RS
2464 (setq words (nreverse words))
2465
2466 ;; restrict number of words
2467 (if (> (length words) (nth 0 reftex-derive-label-parameters))
2468 (setcdr (nthcdr (1- (nth 0 reftex-derive-label-parameters)) words) nil))
2469
2470 ;; First, try to use all words
2471 (setq string (mapconcat '(lambda(w) w) words
2472 (nth 4 reftex-derive-label-parameters)))
2473
2474 ;; Abbreviate words if enforced by user settings or string length
2475 (if (or (eq t (nth 3 reftex-derive-label-parameters))
2476 (and (nth 3 reftex-derive-label-parameters)
2477 (> (length string) (nth 1 reftex-derive-label-parameters))))
2478 (setq words
2479 (mapcar
2480 '(lambda (w) (if (string-match reftex-abbrev-regexp w)
2481 (match-string 1 w)
2482 w))
2483 words)
2484 string (mapconcat '(lambda(w) w) words
2485 (nth 4 reftex-derive-label-parameters))))
2486
2487 ;; Shorten if still to long
2488 (setq string
2489 (if (> (length string) (nth 1 reftex-derive-label-parameters))
2490 (substring string 0 (nth 1 reftex-derive-label-parameters))
2491 string))
2492
2493 ;; Delete the final punctuation, if any
396e0b08 2494 (if (string-match "[^a-zA-Z0-9]+\\'" string)
a7ec1775
RS
2495 (setq string (replace-match "" nil nil string)))
2496 string))
2497
206c6f82
RS
2498(defun reftex-replace-prefix-escapes (prefix)
2499 ;; Replace %escapes in a label prefix
2500 (save-match-data
2501 (let (letter (num 0) replace)
2502 (while (string-match "\\%\\([a-zA-Z]\\)" prefix num)
396e0b08
KH
2503 (setq letter (match-string 1 prefix))
2504 (setq replace
2505 (cond
2506 ((equal letter "f")
2507 (file-name-sans-extension
2508 (file-name-nondirectory (buffer-file-name))))
2509 ((equal letter "F")
2510 (let ((masterdir (file-name-directory (reftex-TeX-master-file)))
2511 (file (file-name-sans-extension (buffer-file-name))))
2512 (if (string-match (concat "\\`" (regexp-quote masterdir))
2513 file)
2514 (substring file (length masterdir))
2515 file)))
2516 ((equal letter "u")
2517 (or (user-login-name) ""))
2518 (t "")))
2519 (setq num (1- (+ (match-beginning 1) (length replace)))
2520 prefix (replace-match replace nil nil prefix)))
206c6f82
RS
2521 prefix)))
2522
a7ec1775
RS
2523(defun reftex-label-location (&optional bound)
2524 ;; Return the environment or macro which determines the label type at point.
2525 ;; If optional BOUND is an integer, limit backward searches to that point.
2526
2527 (let* ((loc1 (reftex-what-macro reftex-label-mac-list bound))
2528 (loc2 (reftex-what-environment reftex-label-env-list bound))
2529 (p1 (or (cdr loc1) 0))
2530 (p2 (or (cdr loc2) 0)))
2531
2532 (setq reftex-location-start (max p1 p2))
396e0b08 2533 (if (>= p1 p2)
a7ec1775 2534 (progn
396e0b08
KH
2535 (setq reftex-default-context-position (+ p1 (length (car loc1))))
2536 (or (car loc1) "section"))
2537 (setq reftex-default-context-position (+ p2 8 (length (car loc2))))
a7ec1775
RS
2538 (or (car loc2) "section"))))
2539
206c6f82
RS
2540(defun reftex-uniquify-label (label &optional force separator)
2541 ;; Make label unique by appending a number.
2542 ;; Optional FORCE means, force appending a number, even if label is unique.
2543 ;; Optional SEPARATOR is a string to stick between label and number.
a7ec1775
RS
2544
2545 ;; Ensure access to scanning info
2546 (reftex-access-scan-info)
396e0b08 2547
206c6f82
RS
2548 (cond
2549 ((and (not force)
396e0b08 2550 (not (assoc label (symbol-value reftex-docstruct-symbol))))
206c6f82
RS
2551 label)
2552 (t
396e0b08
KH
2553 (let* ((label-numbers (assq 'label-numbers
2554 (symbol-value reftex-docstruct-symbol)))
2555 (label-numbers-alist (cdr label-numbers))
2556 (cell (or (assoc label label-numbers-alist)
2557 (car (setcdr label-numbers
2558 (cons (cons label 0)
2559 label-numbers-alist)))))
2560 (num (1+ (cdr cell)))
2561 (sep (or separator "")))
206c6f82 2562 (while (assoc (concat label sep (int-to-string num))
396e0b08
KH
2563 (symbol-value reftex-docstruct-symbol))
2564 (incf num))
206c6f82
RS
2565 (setcdr cell num)
2566 (concat label sep (int-to-string num))))))
a7ec1775
RS
2567
2568;; Help string for the reference label menu
206c6f82 2569(defconst reftex-select-label-prompt
396e0b08 2570 "Select: [n]ext [p]revious [r]escan [ ]context e[x]tern [q]uit RET [?]HELP+more")
206c6f82
RS
2571
2572(defconst reftex-select-label-help
a7ec1775 2573 " AVAILABLE KEYS IN REFERENCE LABEL MENU
396e0b08 2574 --------------------------------------
206c6f82
RS
2575 n / p Go to next/previous label (Cursor motion works as well)
2576 C-s / C-r Search forward/backward. Use repeated C-s/C-r as in isearch.
396e0b08
KH
2577 r / s Reparse document / Switch label type
2578 x Switch to label menu of external document (with LaTeX package `xr')
2579 t i c # % Toggle: [i]ncl. file borders, [t]able of contents, [c]ontext
2580 [#] label counters, [%] labels in comments
206c6f82
RS
2581 SPC Show full context for current label in other window
2582 f Toggle follow mode: other window will follow context
396e0b08
KH
2583 l / q Reuse last referenced label / Quit without accepting label
2584 e Recursive Edit into other window
206c6f82 2585 RET Accept current label")
a7ec1775
RS
2586
2587(defun reftex-reference (&optional type no-insert)
c52bdfca
RS
2588 "Make a LaTeX reference. Look only for labels of a certain TYPE.
2589With prefix arg, force to rescan buffer for labels. This should only be
a7ec1775 2590necessary if you have recently entered labels yourself without using
c52bdfca 2591reftex-label. Rescanning of the buffer can also be requested from the
a7ec1775
RS
2592label selection menu.
2593The function returns the selected label or nil.
fba437e6 2594If NO-INSERT is non-nil, do not insert \\ref command, just return label.
a7ec1775
RS
2595When called with 2 C-u prefix args, disable magic word recognition."
2596
2597 (interactive)
2598
2599 ;; check for active recursive edits
2600 (reftex-check-recursive-edit)
2601
2602 ;; Ensure access to scanning info and rescan buffer if prefix are is '(4)
2603 (reftex-access-scan-info current-prefix-arg)
2604
396e0b08
KH
2605 (unless type
2606 ;; guess type from context
2607 (if (and reftex-guess-label-type
2608 (setq type (assoc (downcase (reftex-word-before-point))
2609 reftex-words-to-typekey-alist)))
2610 (setq type (cdr type))
2611 (setq type (reftex-query-label-type))))
a7ec1775
RS
2612
2613 (let (label pair
2614 (form (or (cdr (assoc type reftex-typekey-to-format-alist))
2615 "\\ref{%s}")))
2616
2617 ;; Have the user select a label
2618 (setq pair (reftex-offer-label-menu type))
2619 (setq label (car pair))
2620
2621 (if (and label
2622 (not no-insert))
2623 (progn
2624 ;; do we need to remove spaces?
2625 (if (string= "~" (substring form 0 1))
2626 (while (or (= (preceding-char) ?\ )
2627 (= (preceding-char) ?\C-i))
2628 (backward-delete-char 1)))
2629 ;; ok, insert the reference
2630 (insert (format form label label))
396e0b08
KH
2631 (message ""))
2632 (message "Quit"))
2633 ;; return the label
2634 label))
a7ec1775
RS
2635
2636(defun reftex-offer-label-menu (typekey)
c52bdfca 2637 ;; Offer a menu with the appropriate labels. Return (label . file).
a7ec1775 2638 (let* ((buf (current-buffer))
396e0b08
KH
2639 (xr-data (assq 'xr (symbol-value reftex-docstruct-symbol)))
2640 (xr-alist (cons (cons "" (buffer-file-name)) (nth 1 xr-data)))
2641 (xr-index 0)
2642 (here-I-am (car (reftex-where-am-I)))
a7ec1775 2643 (toc (reftex-typekey-check typekey reftex-label-menu-flags 0))
396e0b08 2644 (files (reftex-typekey-check typekey reftex-label-menu-flags 7))
a7ec1775
RS
2645 (context (not (reftex-typekey-check
2646 typekey reftex-label-menu-flags 3)))
2647 (counter (reftex-typekey-check
2648 typekey reftex-label-menu-flags 2))
2649 (follow (reftex-typekey-check
2650 typekey reftex-label-menu-flags 4))
396e0b08
KH
2651 (commented (nth 5 reftex-label-menu-flags))
2652 (match-everywhere (reftex-typekey-check
2653 typekey reftex-label-menu-flags 6))
2654 (prefix "")
206c6f82 2655 offset rtn key cnt last-cnt entry)
a7ec1775 2656
a7ec1775
RS
2657 (setq entry (cons nil nil))
2658
396e0b08 2659 ;; The following unwind-protect kills temporary buffers after use
a7ec1775
RS
2660 (unwind-protect
2661 (catch 'exit
2662 (while t
2663 (save-window-excursion
396e0b08 2664 (setq reftex-call-back-to-this-buffer buf)
a7ec1775
RS
2665 (switch-to-buffer-other-window "*RefTeX Select*")
2666 (erase-buffer)
2667 (setq truncate-lines t)
396e0b08
KH
2668 (setq mode-line-format
2669 (list "---- " 'mode-line-buffer-identification
2670 " " (abbreviate-file-name
2671 (buffer-file-name buf))
2672 " -%-"))
2673
2674 (setq reftex-label-index-list
2675 (reftex-make-and-insert-label-list
2676 typekey buf toc files context counter commented
2677 here-I-am prefix))
2678 (setq here-I-am nil) ; turn off determination of offset
a7ec1775 2679 ;; use only when searched
396e0b08 2680 (setq offset (or (car reftex-label-index-list) offset))
a7ec1775 2681 ;; only this is the true list
396e0b08 2682 (pop reftex-label-index-list)
a7ec1775
RS
2683 (setq rtn
2684 (reftex-select-item
396e0b08 2685 reftex-select-label-prompt
a7ec1775 2686 "^>"
a7ec1775 2687 2
206c6f82 2688 reftex-select-label-help
396e0b08 2689 '(?r ?R ?g ?c ?t ?s ?# ?i ?l ?% ?x)
a7ec1775 2690 offset
206c6f82 2691 'reftex-select-label-callback follow
396e0b08 2692 match-everywhere))
206c6f82
RS
2693 (setq key (car rtn)
2694 cnt (nth 1 rtn)
2695 last-cnt (nth 2 rtn)
396e0b08
KH
2696 offset (1+ (or cnt last-cnt 0)))
2697 (unless key (throw 'exit nil))
a7ec1775 2698 (cond
206c6f82 2699 ((or (eq key ?r)
396e0b08
KH
2700 (eq key ?R)
2701 (eq key ?g))
a7ec1775 2702 ;; rescan buffer
396e0b08 2703 (reftex-parse-document buf (or cnt last-cnt) key))
206c6f82 2704 ((eq key ?c)
a7ec1775
RS
2705 ;; toggle context mode
2706 (setq context (not context)))
206c6f82 2707 ((eq key ?s)
a7ec1775
RS
2708 ;; switch type
2709 (setq typekey (reftex-query-label-type)))
206c6f82 2710 ((eq key ?t)
a7ec1775
RS
2711 ;; toggle tabel of contents display
2712 (setq toc (not toc)))
396e0b08
KH
2713 ((eq key ?i)
2714 ;; toggle display of included file borders
2715 (setq files (not files)))
206c6f82 2716 ((eq key ?#)
a7ec1775
RS
2717 ;; toggle counter display
2718 (setq counter (not counter)))
396e0b08
KH
2719 ((eq key ?%)
2720 ;; toggle display of commented labels
2721 (setq commented (not commented)))
206c6f82 2722 ((eq key ?l)
a7ec1775
RS
2723 ;; reuse the last referenced label again
2724 (setq entry reftex-last-used-reference)
2725 (throw 'exit t))
396e0b08
KH
2726 ((eq key ?x)
2727 ;; select an external document
2728 (setq xr-index (reftex-select-external-document
2729 xr-alist xr-index))
2730 (setq buf (or (reftex-get-file-buffer-force
2731 (cdr (nth xr-index xr-alist)) t)
2732 (error "Cannot switch document"))
2733 prefix (or (car (nth xr-index xr-alist)) "")
2734 offset nil))
a7ec1775
RS
2735 (t
2736 (set-buffer buf)
396e0b08
KH
2737 (if cnt
2738 (progn
2739 (setq entry (nth (nth cnt reftex-label-index-list)
2740 (symbol-value reftex-docstruct-symbol)))
2741 (setq reftex-last-used-reference entry))
2742 (setq entry nil))
a7ec1775
RS
2743 (throw 'exit t))))))
2744 (kill-buffer "*RefTeX Select*")
396e0b08
KH
2745 (and (get-buffer "*RefTeX Context Copy*")
2746 (kill-buffer "*RefTeX Context Copy*"))
a7ec1775 2747 (reftex-kill-temporary-buffers))
396e0b08
KH
2748 (cons (if (nth 0 entry) (concat prefix (nth 0 entry)) nil)
2749 (nth 3 entry))))
2750
2751(defun reftex-select-external-document (xr-alist xr-index)
2752 ;; Return index of an external document.
2753 (cond
2754 ((= (length xr-alist) 1)
2755 (ding) 0)
2756 ((= (length xr-alist) 2)
2757 (- 1 xr-index))
2758 (t
2759 (save-window-excursion
2760 (let* ((fmt " [%c] %-5s %s\n") (n (1- ?0)) key)
2761 (with-output-to-temp-buffer "*RefTeX Select*"
2762 (princ
2763 (concat "Select a document by pressing a number key:\n KEY PREFIX DOCUMENT\n----------------------\n"
2764 (mapconcat '(lambda (x)
2765 (format fmt (incf n) (or (car x) "")
2766 (abbreviate-file-name (cdr x))))
2767 xr-alist ""))))
2768 (setq key (read-char))
2769 (if (< (- key ?1) (length xr-alist))
2770 (- key ?0)
2771 (error "Illegal document selection [%c]" key)))))))
2772
2773(defun reftex-make-and-insert-label-list
2774 (typekey0 buf toc files context counter show-commented here-I-am xr-prefix)
a7ec1775 2775 ;; Insert a menu of all labels in buffer BUF into current buffer.
396e0b08
KH
2776 ;; Return the list of labels, with the index of HERE-I-AM as extra car.
2777 (let* ((font (reftex-use-fonts))
2778 (refont (reftex-refontify))
2779 (cnt 0)
2780 (index -1)
2781 (toc-indent " ")
2782 (label-indent
2783 (concat "> "
2784 (if toc (make-string (* 7 reftex-level-indent) ?\ ) "")))
2785 (context-indent
2786 (concat ". "
2787 (if toc (make-string (* 7 reftex-level-indent) ?\ ) "")))
2788 all cell text label typekey note comment master-dir-re
2789 index-list offset docstruct-symbol from from1 to)
2790
2791 ;; Pop to buffer buf to get the correct buffer-local variables
a7ec1775
RS
2792 (save-excursion
2793 (set-buffer buf)
396e0b08
KH
2794
2795 ;; Ensure access to scanning info
2796 (reftex-access-scan-info)
2797
2798 (setq docstruct-symbol reftex-docstruct-symbol
2799 all (symbol-value reftex-docstruct-symbol)
2800 reftex-active-toc nil
2801 master-dir-re
2802 (concat "\\`" (regexp-quote
2803 (file-name-directory (reftex-TeX-master-file))))))
2804
2805 (when refont
2806 ;; Calculate font-lock-defaults as in LaTeX mode.
2807 (make-local-variable 'font-lock-defaults)
2808 (setq font-lock-defaults nil)
2809 (let ((major-mode 'latex-mode))
2810 (font-lock-set-defaults))
2811 ;; The following is only needed for XEmacs, but does not hurt Emacs.
2812 (setq font-lock-mode nil))
2813
2814 ;; Walk the docstruct and insert the appropriate stuff
2815
2816 (while (setq cell (pop all))
2817
2818 (incf index)
2819 (setq from (point))
2820
2821 (if (eq cell here-I-am) (setq offset (1+ cnt)))
2822
2823 (cond
2824
2825 ((memq (car cell) '(bib label-numbers master-dir is-multi
2826 xr xr-doc)))
2827 ;; These are currently ignored
2828
2829 ((memq (car cell) '(bof eof file-error))
2830 ;; Beginning or end of a file
2831 (when files
2832 (insert
2833 " " (if (string-match master-dir-re (nth 1 cell))
2834 (substring (nth 1 cell) (match-end 0))
2835 (nth 1 cell))
2836 (cond ((eq (car cell) 'bof) " starts here\n")
2837 ((eq (car cell) 'eof) " ends here\n")
2838 ((eq (car cell) 'file-error) " was not found\n")))
2839 (when font
2840 (put-text-property from (point)
2841 'face 'font-lock-function-name-face))))
2842
2843 ((eq (car cell) 'toc)
2844 ;; a table of contents entry
2845 (when toc
2846 (setq reftex-active-toc cell)
2847 (insert (concat toc-indent (nth 2 cell) "\n"))))
2848
2849 ((stringp (car cell))
2850 ;; a label
2851 (when (null (nth 2 cell))
2852 ;; No context yet. Quick update.
2853 (setq cell (reftex-label-info-update cell))
2854 (setcar (nthcdr index (symbol-value docstruct-symbol))
2855 cell))
2856
2857 (setq label (car cell)
2858 typekey (nth 1 cell)
2859 text (nth 2 cell)
2860 note (nth 4 cell)
2861 comment (get-text-property 0 'in-comment text))
2862
2863 (when (and (or (string= typekey typekey0) (string= typekey0 " "))
2864 (or show-commented (null comment)))
2865
2866 ;; Yes we want this one
2867 (incf cnt)
2868 (push index index-list)
2869
2870 (setq label (concat xr-prefix label))
2871 (when comment (setq label (concat "% " label)))
2872 (insert label-indent label)
2873 (when font
2874 (put-text-property
2875 (- (point) (length label)) (point)
2876 'face (if comment
2877 'font-lock-comment-face
2878 'font-lock-reference-face)))
2879
2880 (insert (if counter (format " (%d) " cnt) "")
2881 (if comment " LABEL IS COMMENTED OUT " "")
2882 (if note (concat " " note) "")
2883 "\n")
2884 (setq to (point))
2885
2886 (when context
2887 (setq from1 to)
2888 (insert context-indent text "\n")
2889 (setq to (point))
2890 (when refont
2891 (font-lock-fontify-region from1 to)
2892 (goto-char to)))
2893 (put-text-property from to 'cnt (1- cnt))
2894 (goto-char to)))))
2895
2896 ;; Return the index list
a7ec1775
RS
2897 (cons offset (nreverse index-list))))
2898
396e0b08
KH
2899(defun reftex-parse-document (&optional buffer cnt key)
2900 "Rescan the document."
2901 (interactive)
2902 (save-window-excursion
2903 (save-excursion
2904 (if buffer
2905 (if (not (bufferp buffer))
2906 (error "No such buffer %s" (buffer-name buffer))
2907 (set-buffer buffer)))
2908 (let ((arg (if (eq key ?R) '(16) '(4)))
2909 (file (if cnt
2910 (nth 3
2911 (nth (nth cnt reftex-label-index-list)
2912 (symbol-value reftex-docstruct-symbol))))))
2913 (reftex-access-scan-info arg file)))))
2914
a7ec1775
RS
2915(defun reftex-query-label-type ()
2916 ;; Ask for label type
2917 (message reftex-type-query-prompt)
2918 (let ((key (read-char)))
396e0b08
KH
2919 (when (eq key ?\?)
2920 (save-window-excursion
2921 (with-output-to-temp-buffer "*RefTeX Help*"
2922 (princ reftex-type-query-help))
2923 (setq key (read-char))
2924 (kill-buffer "*RefTeX Help*")))
2925 (unless (member (char-to-string key) reftex-typekey-list)
2926 (error "No such label type: %s" (char-to-string key)))
a7ec1775
RS
2927 (char-to-string key)))
2928
a7ec1775
RS
2929;; Variable holding the vector with section numbers
2930(defvar reftex-section-numbers [0 0 0 0 0 0 0 0])
2931
396e0b08
KH
2932(defun reftex-section-info (file)
2933 ;; Return a section entry for the current match.
2934 ;; Carefull: This function expects the match-data to be still in place!
2935 (let* ((marker (set-marker (make-marker) (1- (match-beginning 3))))
2936 (macro (reftex-match-string 3))
2937 (star (= ?* (char-after (match-end 3))))
2938 (level (cdr (assoc macro reftex-section-levels)))
2939 (section-number (reftex-section-number
2940 reftex-section-numbers level star))
2941 (text1 (save-match-data (save-excursion (reftex-context-substring))))
2942 (literal (buffer-substring-no-properties
2943 (1- (match-beginning 3))
2944 (min (point-max) (+ (match-end 0) (length text1) 1))))
2945 ;; Literal can be too short since text1 too short. No big problem.
2946 (text (reftex-nicify-text text1)))
2947
2948 ;; Add section number and indentation
2949 (setq text
2950 (concat
2951 (make-string (* reftex-level-indent level) ?\ )
2952 (if (nth 1 reftex-label-menu-flags) ; section number flag
2953 (concat section-number " "))
2954 text))
2955 ;; Fontify
2956 (if (reftex-use-fonts)
2957 (put-text-property 0 (length text)
2958 'face 'font-lock-comment-face text))
2959 (list 'toc "toc" text file marker level section-number
2960 literal (marker-position marker))))
a7ec1775 2961
a7ec1775
RS
2962(defun reftex-label-info-update (cell)
2963 ;; Update information about just one label in a different file.
2964 ;; CELL contains the old info list
2965 (let* ((label (nth 0 cell))
2966 (typekey (nth 1 cell))
206c6f82 2967 ;; (text (nth 2 cell))
a7ec1775
RS
2968 (file (nth 3 cell))
2969 (note (nth 4 cell))
2970 (buf (reftex-get-file-buffer-force
396e0b08 2971 file (not (eq t reftex-keep-temporary-buffers)))))
a7ec1775 2972 (if (not buf)
c52bdfca 2973 (list label typekey "" file "LOST LABEL. RESCAN TO FIX.")
a7ec1775
RS
2974 (save-excursion
2975 (set-buffer buf)
2976 (save-restriction
2977 (widen)
2978 (goto-char 1)
2979
396e0b08
KH
2980 (if (or (re-search-forward
2981 (format reftex-find-label-regexp-format
2982 (regexp-quote label)) nil t)
2983 (re-search-forward
2984 (format reftex-find-label-regexp-format2
2985 (regexp-quote label)) nil t))
2986
2987 (progn
2988 (backward-char 1)
2989 (append (reftex-label-info label file) (list note)))
c52bdfca 2990 (list label typekey "" file "LOST LABEL. RESCAN TO FIX.")))))))
a7ec1775 2991
396e0b08 2992(defun reftex-label-info (label &optional file bound derive env-or-mac)
a7ec1775 2993 ;; Return info list on LABEL at point.
396e0b08 2994 (let* ((env-or-mac (or env-or-mac (reftex-label-location bound)))
a7ec1775
RS
2995 (typekey (nth 1 (assoc env-or-mac reftex-env-or-mac-alist)))
2996 (file (or file (buffer-file-name)))
2997 (parse (if (reftex-typekey-check
2998 typekey reftex-use-text-after-label-as-context)
2999 nil
3000 (nth 2 (assoc env-or-mac reftex-env-or-mac-alist))))
206c6f82 3001 (text (reftex-short-context env-or-mac parse reftex-location-start
396e0b08 3002 derive)))
a7ec1775
RS
3003 (if (reftex-in-comment)
3004 (put-text-property 0 1 'in-comment t text))
3005 (list label typekey text file)))
3006
3007(defun reftex-in-comment ()
3008 (save-excursion
3009 (skip-chars-backward "^%\n\r")
396e0b08 3010 (eq (preceding-char) ?%)))
a7ec1775 3011
206c6f82 3012(defun reftex-short-context (env parse &optional bound derive)
a7ec1775
RS
3013 ;; Get about one line of useful context for the label definition at point.
3014
206c6f82
RS
3015 (if (consp parse)
3016 (setq parse (if derive (cdr parse) (car parse))))
3017
a7ec1775
RS
3018 (reftex-nicify-text
3019
3020 (cond
3021
3022 ((null parse)
3023 (save-excursion
3024 (reftex-context-substring)))
3025
3026 ((eq parse t)
3027 (if (string= env "section")
3028 ;; special treatment for section labels
3029 (save-excursion
396e0b08
KH
3030 (if (and (re-search-backward reftex-section-or-include-regexp
3031 (point-min) t)
3032 (match-end 2))
a7ec1775
RS
3033 (progn
3034 (goto-char (match-end 0))
3035 (reftex-context-substring))
396e0b08
KH
3036 (if reftex-active-toc
3037 (progn
3038 (string-match "{\\([^}]*\\)" (nth 7 reftex-active-toc))
3039 (match-string 1 (nth 7 reftex-active-toc)))
3040 "SECTION HEADING NOT FOUND")))
a7ec1775 3041 (save-excursion
396e0b08
KH
3042 (goto-char reftex-default-context-position)
3043 (unless (eq (string-to-char env) ?\\)
3044 (reftex-move-over-touching-args))
a7ec1775
RS
3045 (reftex-context-substring))))
3046
3047 ((stringp parse)
3048 (save-excursion
3049 (if (re-search-backward parse bound t)
3050 (progn
3051 (goto-char (match-end 0))
3052 (reftex-context-substring))
3053 "NO MATCH FOR CONTEXT REGEXP")))
206c6f82
RS
3054
3055 ((integerp parse)
396e0b08
KH
3056 (or (save-excursion
3057 (goto-char reftex-default-context-position)
3058 (reftex-nth-arg
3059 parse
3060 (nth 6 (assoc env reftex-env-or-mac-alist))))
3061 ""))
206c6f82 3062
a7ec1775 3063 ((fboundp parse)
c52bdfca 3064 ;; A hook function. Call it.
a7ec1775
RS
3065 (save-excursion
3066 (condition-case error-var
3067 (funcall parse env)
206c6f82 3068 (error (format "HOOK ERROR: %s" (cdr error-var))))))
a7ec1775
RS
3069 (t
3070 "ILLEGAL VALUE OF PARSE"))))
3071
396e0b08
KH
3072(defun reftex-where-am-I ()
3073 ;; Return the docstruct entry above point. Actually returns a cons
3074 ;; cell in which the cdr is a flag indicating if the information is
3075 ;; exact (t) or approximate (nil).
3076 (interactive)
3077
3078 (let ((docstruct (symbol-value reftex-docstruct-symbol))
3079 (cnt 0) rtn
3080 found)
3081 (save-excursion
3082 (while (not rtn)
3083 (incf cnt)
3084 (setq found (re-search-backward reftex-everything-regexp nil t))
3085 (setq rtn
3086 (cond
3087 ((not found)
3088 ;; no match
3089 (or
3090 (car (member (list 'bof (buffer-file-name)) docstruct))
3091 (not (setq cnt 2))
3092 (assq 'bof docstruct) ;; for safety reasons
3093 'corrupted))
3094 ((match-end 1)
3095 ;; Label
3096 (assoc (reftex-match-string 1)
3097 (symbol-value reftex-docstruct-symbol)))
3098 ((match-end 3)
3099 ;; Section
3100 (goto-char (1- (match-beginning 3)))
3101 (let* ((list (member (list 'bof (buffer-file-name))
3102 docstruct))
3103 (endelt (car (member (list 'eof (buffer-file-name))
3104 list)))
3105 rtn1)
3106 (while (and list (not (eq endelt (car list))))
3107 (if (and (eq (car (car list)) 'toc)
3108 (string= (buffer-file-name)
3109 (nth 3 (car list))))
3110 (cond
3111 ((equal (point)
3112 (or (and (markerp (nth 4 (car list)))
3113 (marker-position (nth 4 (car list))))
3114 (nth 8 (car list))))
3115 ;; Fits with marker position or recorded position
3116 (setq rtn1 (car list) list nil))
3117 ((looking-at (reftex-make-regexp-allow-for-ctrl-m
3118 (nth 7 (car list))))
3119 ;; Same title
3120 (setq rtn1 (car list) list nil cnt 2))))
3121 (pop list))
3122 rtn1))
3123 ((match-end 7)
3124 ;; Input or include...
3125 (car
3126 (member (list 'eof (reftex-find-tex-file
3127 (reftex-match-string 7)
3128 (cons
3129 (cdr (assq 'master-dir docstruct))
3130 reftex-tex-path)))
3131 docstruct)))
3132 ((match-end 8)
3133 (save-excursion
3134 (goto-char (match-end 8))
3135 (assoc (reftex-no-props
3136 (reftex-nth-arg-wrapper
3137 (reftex-match-string 8)))
3138 (symbol-value reftex-docstruct-symbol))))
3139 (t
3140 (error "This should not happen (reftex-where-am-I)"))))))
3141 (cons rtn (eq cnt 1))))
3142
3143(defun reftex-parse-args (macro)
3144 ;; Return a list of macro name, nargs, arg-nr which is label and a list of
3145 ;; optional argument indices.
3146 (if (string-match "[[{]\\*?[]}]" macro)
3147 (progn
3148 (let ((must-match (substring macro 0 (match-beginning 0)))
3149 (args (substring macro (match-beginning 0)))
3150 opt-list nlabel (cnt 0))
3151 (while (string-match "\\`[[{]\\(\\*\\)?[]}]" args)
3152 (incf cnt)
3153 (when (eq ?\[ (string-to-char args))
3154 (push cnt opt-list))
3155 (when (and (match-end 1)
3156 (not nlabel))
3157 (setq nlabel cnt))
3158 (setq args (substring args (match-end 0))))
3159 (list must-match cnt nlabel opt-list)))
3160 nil))
3161
3162(defsubst reftex-move-to-next-arg (&optional ignore)
3163 ;; Assuming that we are at the end of a macro name or a macro argument,
3164 ;; move forward to the opening parenthesis of the next argument.
3165 ;; This function understands the splitting of macros over several lines
3166 ;; in TeX.
3167 (cond
3168 ;; Just to be quick:
3169 ((memq (following-char) '(?\[ ?\{)))
3170 ;; Do a search
3171 ((looking-at "[ \t]*[\n\r]?\\([ \t]*%[^\n\r]*[\n\r]\\)*[ \t]*[[{]")
3172 (goto-char (1- (match-end 0)))
3173 t)
3174 (t nil)))
3175
3176(defsubst reftex-move-to-previous-arg (&optional bound)
3177 ;; Assuming that we are in front of a macro argument,
3178 ;; move backward to the closing parenthesis of the previous argument.
3179 ;; This function understands the splitting of macros over several lines
3180 ;; in TeX.
3181 (cond
3182 ;; Just to be quick:
3183 ((memq (preceding-char) '(?\] ?\})))
3184 ;; Do a search
3185 ((re-search-backward
3186 "[]}][ \t]*[\n\r]?\\([ \t]*%[^\n\r]*[\n\r]\\)*[ \t]*\\=" bound t)
3187 (goto-char (1+ (match-beginning 0)))
3188 t)
3189 (t nil)))
3190
3191(defun reftex-nth-arg-wrapper (key)
3192 (let ((entry (assoc key reftex-env-or-mac-alist)))
3193 (reftex-nth-arg (nth 5 entry) (nth 6 entry))))
3194
3195(defun reftex-nth-arg (n &optional opt-args)
206c6f82 3196 ;; Return the nth following {} or [] parentheses content.
396e0b08
KH
3197 ;; OPT-ARGS is a list of argument numbers which are optional.
3198
3199 ;; If we are sitting at a macro start, skip to end of macro name.
206c6f82 3200 (and (eq (following-char) ?\\) (skip-chars-forward "a-zA-Z*\\\\"))
396e0b08
KH
3201
3202 (if (= n 1000)
3203 ;; Special case: Skip all touching arguments
3204 (progn
3205 (reftex-move-over-touching-args)
3206 (reftex-context-substring))
3207
3208 ;; Do the real thing.
3209 (let ((cnt 1))
3210
3211 (when (reftex-move-to-next-arg)
3212
3213 (while (< cnt n)
3214 (while (and (member cnt opt-args)
3215 (eq (following-char) ?\{))
3216 (incf cnt))
3217 (when (< cnt n)
3218 (unless (and (condition-case nil
3219 (or (forward-list 1) t)
3220 (error nil))
3221 (reftex-move-to-next-arg)
3222 (incf cnt))
3223 (setq cnt 1000))))
3224
3225 (while (and (memq cnt opt-args)
3226 (eq (following-char) ?\{))
3227 (incf cnt)))
3228 (if (and (= n cnt)
3229 (> (skip-chars-forward "{\\[") 0))
3230 (reftex-context-substring)
3231 nil))))
3232
3233(defun reftex-move-over-touching-args ()
3234 (condition-case nil
3235 (while (memq (following-char) '(?\[ ?\{))
3236 (forward-list 1))
3237 (error nil)))
206c6f82 3238
a7ec1775
RS
3239(defun reftex-context-substring ()
3240 ;; Return up to 100 chars from point
3241 ;; When point is just after a { or [, limit string to matching parenthesis
3242 (cond
3243 ((or (= (preceding-char) ?\{)
3244 (= (preceding-char) ?\[))
206c6f82 3245 ;; Inside a list - get only the list.
396e0b08 3246 (buffer-substring-no-properties
a7ec1775 3247 (point)
396e0b08
KH
3248 (min (reftex-fp 150)
3249 (point-max)
3250 (condition-case nil
3251 (progn
3252 (up-list 1)
3253 (1- (point)))
3254 (error (point-max))))))
a7ec1775
RS
3255 (t
3256 ;; no list - just grab 100 characters
396e0b08 3257 (buffer-substring-no-properties (point) (min (reftex-fp 150) (point-max))))))
a7ec1775 3258
396e0b08 3259(defun reftex-init-section-numbers (&optional toc-entry)
206c6f82
RS
3260 ;; Initialize the section numbers with zeros or with what is found
3261 ;; in the toc entry.
3262 (let* ((level (or (nth 5 toc-entry) -1))
396e0b08
KH
3263 (numbers (nreverse (split-string (or (nth 6 toc-entry) "") "\\.")))
3264 (depth (1- (length reftex-section-numbers)))
3265 (i depth))
206c6f82
RS
3266 (while (>= i 0)
3267 (if (> i level)
396e0b08
KH
3268 (aset reftex-section-numbers i 0)
3269 (aset reftex-section-numbers i (string-to-int (or (car numbers) "0")))
3270 (pop numbers))
3271 (decf i))))
206c6f82 3272
a7ec1775
RS
3273(defun reftex-section-number (section-numbers &optional level star)
3274 ;; Return a string with the current section number.
3275 ;; When LEVEL is non-nil, increase section numbers on that level.
206c6f82 3276 (let* ((depth (1- (length section-numbers))) idx n (string ""))
396e0b08
KH
3277 (when level
3278 (when (and (> level -1) (not star))
3279 (aset section-numbers level (1+ (aref section-numbers level))))
3280 (setq idx (1+ level))
3281 (while (<= idx depth)
3282 (aset section-numbers idx 0)
3283 (incf idx)))
a7ec1775
RS
3284 (setq idx 0)
3285 (while (<= idx depth)
3286 (setq n (aref section-numbers idx))
3287 (setq string (concat string (if (not (string= string "")) "." "")
3288 (int-to-string n)))
396e0b08 3289 (incf idx))
a7ec1775
RS
3290 (save-match-data
3291 (if (string-match "\\`\\(0\\.\\)+" string)
3292 (setq string (replace-match "" nil nil string)))
3293 (if (string-match "\\(\\.0\\)+\\'" string)
3294 (setq string (replace-match "" nil nil string))))
396e0b08
KH
3295 (if star
3296 (concat (make-string (1- (length string)) ?\ ) "*")
a7ec1775
RS
3297 string)))
3298
3299;; A variable to remember the index of the last label context shown
3300(defvar reftex-last-cnt 0)
3301
3302(defun reftex-select-label-callback (cnt)
3303 ;; Callback function called from the label selection in order to
3304 ;; show context in another window
3305 (let* ((this-window (selected-window))
396e0b08 3306 index entry label file buffer re)
a7ec1775
RS
3307 ;; pop to original buffer in order to get correct variables
3308 (catch 'exit
3309 (save-excursion
3310 (set-buffer reftex-call-back-to-this-buffer)
3311 (setq index (nth (or cnt 1) reftex-label-index-list)
396e0b08 3312 entry (nth index (symbol-value reftex-docstruct-symbol))
a7ec1775
RS
3313 label (nth 0 entry)
3314 file (nth 3 entry)))
3315
3316 ;; goto the file in another window
3317 (setq buffer (reftex-get-file-buffer-force
3318 file (not reftex-keep-temporary-buffers)))
3319 (if buffer
3320 ;; good - the file is available
3321 (switch-to-buffer-other-window buffer)
c52bdfca 3322 ;; we have got a problem here. The file does not exist.
a7ec1775
RS
3323 ;; Let' get out of here..
3324 (ding)
3325 (throw 'exit nil))
3326
3327
3328 ;; search for that label
396e0b08
KH
3329 (setq re (format reftex-find-label-regexp-format (regexp-quote label)))
3330 (unless (and (integerp cnt)
3331 (integerp reftex-last-cnt)
3332 (if (> cnt reftex-last-cnt)
3333 (re-search-forward re nil t)
3334 (re-search-backward re nil t)))
3335 (goto-char (point-min))
3336 (unless (re-search-forward re nil t)
3337 ;; Ooops. Must be in a macro with distributed args.
3338 (re-search-forward (format reftex-find-label-regexp-format2
3339 (regexp-quote label)) nil t)))
3340 (when (match-end 3)
3341 (reftex-highlight 0 (match-beginning 3) (match-end 3))
3342 (reftex-show-entry (- (point) (match-beginning 3))
3343 (- (point) (match-end 3)))
3344 (recenter (/ (window-height) 2)))
a7ec1775
RS
3345 (select-window this-window))))
3346
3347(defun reftex-pop-to-label (label file-list &optional mark-to-kill highlight)
3348 ;; Find LABEL in any file in FILE-LIST in another window.
3349 ;; If mark-to-kill is non-nil, mark new buffer for killing.
3350 ;; If HIGHLIGHT is non-nil, highlight the label definition.
396e0b08
KH
3351 (let* ((re1 (format reftex-find-label-regexp-format (regexp-quote label)))
3352 (re2 (format reftex-find-label-regexp-format2 (regexp-quote label)))
3353 (re-list (list re1 re2)) re
3354 (file-list-1 file-list)
a7ec1775
RS
3355 file buf)
3356 (catch 'exit
396e0b08
KH
3357 (while (setq re (pop re-list))
3358 (setq file-list file-list-1)
3359 (while (setq file (pop file-list))
3360 (unless (setq buf (reftex-get-file-buffer-force file mark-to-kill))
3361 (error "No such file %s" file))
3362 (set-buffer buf)
3363 (widen)
3364 (goto-char (point-min))
3365 (when (re-search-forward re nil t)
3366 (switch-to-buffer-other-window buf)
3367 (goto-char (match-beginning 0))
3368 (recenter (/ (window-height) 2))
3369 (if highlight
3370 (reftex-highlight 0 (match-beginning 3) (match-end 3)))
3371 (throw 'exit (selected-window)))))
a7ec1775
RS
3372 (error "Label %s not found" label))))
3373
3374(defun reftex-find-duplicate-labels ()
3375 "Produce a list of all duplicate labels in the document."
3376
3377 (interactive)
3378
3379 ;; Rescan the document to make sure
3380 (reftex-access-scan-info t)
3381
3382 (let ((master (reftex-TeX-master-file))
396e0b08 3383 (dlist
a7ec1775
RS
3384 (mapcar
3385 '(lambda(x)
3386 (let (x1)
3387 (cond
396e0b08
KH
3388 ((memq (car x)
3389 '(toc bof eof bib label-numbers xr xr-doc
3390 master-dir file-error is-multi))
3391 nil)
206c6f82 3392 (t
a7ec1775 3393 (setq x1 (reftex-all-assoc-string
396e0b08 3394 (car x) (symbol-value reftex-docstruct-symbol)))
a7ec1775 3395 (if (< 1 (length x1))
396e0b08 3396 (append (list (car x))
a7ec1775
RS
3397 (mapcar '(lambda(x)
3398 (abbreviate-file-name (nth 3 x))) x1))
206c6f82 3399 (list nil))))))
396e0b08 3400 (reftex-uniquify (symbol-value reftex-docstruct-symbol)))))
a7ec1775
RS
3401 (setq dlist (reftex-uniquify dlist))
3402 (if (null dlist) (error "No duplicate labels in document"))
025bb635 3403 (switch-to-buffer-other-window "*Duplicate Labels*")
a7ec1775
RS
3404 (make-local-variable 'TeX-master)
3405 (setq TeX-master master)
3406 (erase-buffer)
3407 (insert " MULTIPLE LABELS IN CURRENT DOCUMENT:\n")
3408 (insert " Move point to label and type `M-x reftex-change-label'\n"
396e0b08 3409 " This will run a query-replace on the label and its references\n")
a7ec1775
RS
3410 (insert " LABEL FILE\n")
3411 (insert " -------------------------------------------------------------\n")
3412 (while dlist
396e0b08
KH
3413 (when (and (car (car dlist))
3414 (cdr (car dlist)))
3415 (insert (mapconcat '(lambda(x) x) (car dlist) "\n ") "\n"))
3416 (pop dlist))
a7ec1775
RS
3417 (goto-char (point-min))))
3418
396e0b08
KH
3419(defun reftex-all-assq (key list)
3420 ;; Return a list of all associations of KEY in LIST. Comparison with string=
3421 (let (rtn)
3422 (while (setq list (memq (assq key list) list))
3423 (push (car list) rtn)
3424 (pop list))
3425 (nreverse rtn)))
3426
a7ec1775 3427(defun reftex-all-assoc-string (key list)
c52bdfca 3428 ;; Return a list of all associations of KEY in LIST. Comparison with string=
a7ec1775
RS
3429 (let (rtn)
3430 (while list
3431 (if (string= (car (car list)) key)
396e0b08
KH
3432 (push (car list) rtn))
3433 (pop list))
a7ec1775
RS
3434 (nreverse rtn)))
3435
396e0b08 3436(defun reftex-kill-temporary-buffers (&optional buffer)
a7ec1775 3437 ;; Kill all buffers in the list reftex-kill-temporary-buffers.
396e0b08
KH
3438 (cond
3439 (buffer
3440 (when (member buffer reftex-buffers-to-kill)
3441 (kill-buffer buffer)
3442 (setq reftex-buffers-to-kill
3443 (delete buffer reftex-buffers-to-kill))))
3444 (t
3445 (while (setq buffer (pop reftex-buffers-to-kill))
3446 (when (bufferp buffer)
3447 (and (buffer-modified-p buffer)
3448 (y-or-n-p (format "Save file %s? "
3449 (buffer-file-name buffer)))
3450 (save-excursion
3451 (set-buffer buffer)
3452 (save-buffer)))
3453 (kill-buffer buffer))
3454 (pop reftex-buffers-to-kill)))))
3455
3456(defun reftex-show-entry (beg-hlt end-hlt)
a7ec1775 3457 ;; Show entry if point is hidden by outline mode
396e0b08
KH
3458 (let* ((pos (point))
3459 (n (/ (window-height) 2))
3460 (beg (save-excursion
3461 (re-search-backward "[\n\r]" nil 1 n) (point)))
3462 (end (save-excursion
3463 (re-search-forward "[\n\r]" nil 1 n) (point))))
a7ec1775 3464 (if (and reftex-auto-show-entry
396e0b08
KH
3465 (string-match
3466 "\r" (buffer-substring beg end)))
3467 (cond
3468 ((eq t reftex-auto-show-entry)
3469 (subst-char-in-region
3470 (save-excursion (search-backward "\n" nil t) (point))
3471 (save-excursion (search-forward "\n" nil t) (point))
3472 ?\r ?\n t))
3473 ((eq reftex-auto-show-entry 'copy)
3474 (let ((string (buffer-substring beg end)))
3475 (switch-to-buffer "*RefTeX Context Copy*")
3476 (setq buffer-read-only nil)
3477 (erase-buffer)
3478 (insert string)
3479 (subst-char-in-region (point-min) (point-max) ?\r ?\n t)
3480 (goto-char (- pos beg))
3481 (reftex-highlight 0 (1+ (- (point) beg-hlt))
3482 (1+ (- (point) end-hlt)))
3483 (when (reftex-refontify)
3484 (make-local-variable 'font-lock-defaults)
3485 (setq font-lock-defaults nil)
3486 (let ((major-mode 'latex-mode))
3487 (font-lock-set-defaults)
3488 (font-lock-fontify-buffer)))
3489 (setq buffer-read-only t)))
3490 ))))
a7ec1775
RS
3491
3492(defun reftex-nicify-text (text)
396e0b08 3493 ;; Make TEXT nice for inclusion as context into label menu
a7ec1775
RS
3494 (while (string-match "[\n\r\t]\\|[ \t][ \t]+" text) ; remove extra whitespace
3495 (setq text (replace-match " " nil t text)))
3496 (if (string-match "\\\\end{.*" text) ; nothing beyond \end{
3497 (setq text (replace-match "" nil t text)))
396e0b08 3498 (if (string-match "\\\\label{[^}]*}" text) ; kill the label
a7ec1775 3499 (setq text (replace-match "" nil t text)))
396e0b08 3500 (if (string-match "\\`[ }]+" text) ; leading whitespace, `}'
a7ec1775
RS
3501 (setq text (replace-match "" nil t text)))
3502 (cond
396e0b08
KH
3503 ((> (length text) 100) (substring text 0 100))
3504 ((= (length text) 0) " ")
3505 (t text)))
a7ec1775
RS
3506
3507(defun reftex-typekey-check (typekey conf-variable &optional n)
3508 ;; Check if CONF-VARIABLE is true or contains TYPEKEY
3509 (and n (setq conf-variable (nth n conf-variable)))
206c6f82 3510 (or (eq conf-variable t)
a7ec1775
RS
3511 (and (stringp conf-variable)
3512 (string-match (concat "[" conf-variable "]") typekey))))
3513
3514;;; ===========================================================================
3515;;;
396e0b08 3516;;; Table of contents
a7ec1775
RS
3517
3518;; We keep at most one *toc* buffer - it is easy to make them
3519
3520(defvar reftex-last-toc-master nil
3521 "Stores the name of the tex file that `reftex-toc' was last run on.")
3522
3523(defvar reftex-last-toc-file nil
c52bdfca 3524 "Stores the file name from which `reftex-toc' was called. For redo command.")
a7ec1775 3525
206c6f82
RS
3526(defvar reftex-last-window-height nil)
3527
a7ec1775
RS
3528(defvar reftex-toc-return-marker (make-marker)
3529 "Marker which makes it possible to return from toc to old position.")
3530
206c6f82
RS
3531(defconst reftex-toc-help
3532" AVAILABLE KEYS IN TOC BUFFER
3533 ============================
3534SPC Show the corresponding section of the LaTeX document.
3535TAB Goto the section.
3536RET Goto the section and hide the *toc* buffer (also on mouse-2).
3537q / Q Hide/Kill *toc* buffer, return to position of last reftex-toc command.
3538f Toggle follow mode on and off.
396e0b08
KH
3539r / g Reparse the LaTeX document.
3540x Switch to TOC of external document (with LaTeX package `xr').")
206c6f82 3541
a7ec1775
RS
3542(defun reftex-toc ()
3543 "Show the table of contents for the current document.
a7ec1775
RS
3544When called with a raw C-u prefix, rescan the document first."
3545
3546 (interactive)
3547
206c6f82 3548 (if (or (not (string= reftex-last-toc-master (reftex-TeX-master-file)))
396e0b08 3549 current-prefix-arg)
206c6f82 3550 (reftex-empty-toc-buffer))
a7ec1775
RS
3551
3552 (setq reftex-last-toc-file (buffer-file-name))
3553 (setq reftex-last-toc-master (reftex-TeX-master-file))
3554
3555 (set-marker reftex-toc-return-marker (point))
3556
206c6f82 3557 ;; If follow mode is active, arrange to delay it one command
a7ec1775
RS
3558 (if reftex-toc-follow-mode
3559 (setq reftex-toc-follow-mode 1))
3560
a7ec1775
RS
3561 ;; Ensure access to scanning info and rescan buffer if prefix are is '(4)
3562 (reftex-access-scan-info current-prefix-arg)
3563
396e0b08
KH
3564 (let* ((all (symbol-value reftex-docstruct-symbol))
3565 (xr-data (assq 'xr all))
3566 (xr-alist (cons (cons "" (buffer-file-name)) (nth 1 xr-data)))
a7ec1775 3567 (where (reftex-nearest-section))
396e0b08 3568 toc1 cell startpos)
a7ec1775 3569
206c6f82
RS
3570 (if (get-buffer-window "*toc*")
3571 (select-window (get-buffer-window "*toc*"))
3572 (setq reftex-last-window-height (window-height)) ; remember
3573 (split-window-vertically)
3574 (switch-to-buffer (get-buffer-create "*toc*")))
a7ec1775
RS
3575
3576 (cond
3577 ;; buffer is empty - fill it with the table of contents
3578 ((= (buffer-size) 0)
3579
206c6f82
RS
3580 (local-set-key "?" 'reftex-toc-show-help)
3581 (local-set-key " " 'reftex-toc-view-line)
3582 (local-set-key "\C-m" 'reftex-toc-goto-line-and-hide)
3583 (local-set-key "\C-i" 'reftex-toc-goto-line)
3584 (local-set-key "r" 'reftex-toc-redo)
396e0b08 3585 (local-set-key "R" 'reftex-toc-Redo)
206c6f82
RS
3586 (local-set-key "g" 'revert-buffer)
3587 (local-set-key "q" 'reftex-toc-quit)
3588 (local-set-key "Q" 'reftex-toc-quit-and-kill)
3589 (local-set-key "f" 'reftex-toc-toggle-follow)
396e0b08 3590 (local-set-key "x" 'reftex-toc-external)
206c6f82
RS
3591 (local-set-key [(mouse-2)] 'reftex-toc-mouse-goto-line-and-hide); Emacs
3592 (local-set-key [(button2)] 'reftex-toc-mouse-goto-line-and-hide); XEmacs
c52bdfca
RS
3593 (make-local-variable 'revert-buffer-function)
3594 (setq revert-buffer-function 'reftex-toc-redo)
a7ec1775
RS
3595 (setq truncate-lines t)
3596 (make-local-hook 'post-command-hook)
3597 (make-local-hook 'pre-command-hook)
3598 (setq post-command-hook '(reftex-toc-post-command-hook))
3599 (setq pre-command-hook '(reftex-toc-pre-command-hook))
3600
3601 (insert (format
3602"TABLE-OF-CONTENTS on %s
396e0b08 3603SPC=view TAB=goto RET=goto+hide [q]uit [r]escan [f]ollow-mode e[x]tern [?]Help
a7ec1775
RS
3604-------------------------------------------------------------------------------
3605" (abbreviate-file-name reftex-last-toc-master)))
3606 (setq startpos (point))
3607
3608 (if (reftex-use-fonts)
3609 (put-text-property 1 (point) 'face 'font-lock-keyword-face))
3610 (put-text-property 1 (point) 'intangible t)
396e0b08 3611 (put-text-property 1 2 'xr-alist xr-alist)
a7ec1775
RS
3612
3613 (while all
3614 (setq cell (car all)
3615 all (cdr all))
396e0b08
KH
3616 (when (eq (car cell) 'toc)
3617 (setq toc1 (concat (nth 2 cell) "\n"))
3618 (put-text-property 0 (length toc1) 'toc cell toc1)
3619 (insert toc1)))
a7ec1775
RS
3620
3621 (backward-delete-char 1)
3622
3623 (setq buffer-read-only t))
3624 (t
3625 (goto-line 3)
3626 (beginning-of-line)
3627 (setq startpos (point))))
3628
3629 ;; Find the correct section
3630 (goto-char (point-max))
3631 (beginning-of-line)
3632 (while (and (> (point) startpos)
396e0b08 3633 (not (eq (get-text-property (point) 'toc) where)))
a7ec1775
RS
3634 (beginning-of-line 0))))
3635
3636(defun reftex-nearest-section ()
3637 ;; Return (file . find) of nearest section command
396e0b08
KH
3638 (let* ((here-I-am (car (reftex-where-am-I))))
3639 (reftex-last-assoc-before-elt
3640 'toc here-I-am (symbol-value reftex-docstruct-symbol))))
a7ec1775
RS
3641
3642(defun reftex-toc-pre-command-hook ()
3643 ;; used as pre command hook in *toc* buffer
3644 (reftex-unhighlight 0)
3645 (reftex-unhighlight 1))
3646
3647(defun reftex-toc-post-command-hook ()
3648 ;; used in the post-command-hook for the *toc* buffer
3649 (and (> (point) 1)
3650 (save-excursion
396e0b08
KH
3651 (reftex-highlight 1
3652 (progn (beginning-of-line) (point))
3653 (progn (end-of-line) (point)))))
a7ec1775
RS
3654 (cond
3655 ((integerp reftex-toc-follow-mode)
3656 ;; remove delayed action
3657 (setq reftex-toc-follow-mode t))
3658 (reftex-toc-follow-mode
3659 ;; show context in other window
3660 (condition-case nil
3661 (reftex-toc-visit-line)
206c6f82
RS
3662 (error (ding) t)))))
3663
3664(defun reftex-empty-toc-buffer ()
3665 (if (get-buffer "*toc*")
396e0b08
KH
3666 (save-excursion
3667 (set-buffer "*toc*")
3668 (setq buffer-read-only nil)
3669 (erase-buffer))))
206c6f82
RS
3670
3671(defun reftex-re-enlarge ()
396e0b08
KH
3672 (enlarge-window
3673 (max 0 (- (or reftex-last-window-height (window-height))
3674 (window-height)))))
206c6f82
RS
3675
3676(defun reftex-toc-show-help ()
3677 (interactive)
3678 (with-output-to-temp-buffer "*RefTeX Help*"
3679 (princ reftex-toc-help))
3680 ;; If follow mode is active, arrange to delay it one command
3681 (if reftex-toc-follow-mode
3682 (setq reftex-toc-follow-mode 1)))
a7ec1775
RS
3683
3684(defun reftex-toc-toggle-follow ()
3685 "Toggle toc-follow mode.
c52bdfca 3686(It is not really a mode, just a flag)."
a7ec1775
RS
3687 (interactive)
3688 (setq reftex-toc-follow-mode (not reftex-toc-follow-mode)))
3689(defun reftex-toc-view-line ()
3690 "View document location in other window."
3691 (interactive)
3692 (reftex-toc-visit-line))
3693(defun reftex-toc-goto-line-and-hide ()
c52bdfca 3694 "Go to document location in other window. Hide the *toc* window."
a7ec1775
RS
3695 (interactive)
3696 (reftex-toc-visit-line 'hide))
206c6f82
RS
3697(defun reftex-toc-goto-line ()
3698 "Go to document location in other window. Hide the *toc* window."
3699 (interactive)
3700 (reftex-toc-visit-line t))
3701(defun reftex-toc-mouse-goto-line-and-hide (ev)
3702 "Go to document location in other window. Hide the *toc* window."
3703 (interactive "e")
3704 (mouse-set-point ev)
3705 (reftex-toc-visit-line 'hide))
a7ec1775
RS
3706(defun reftex-toc-quit ()
3707 "Hide the *toc* window and do not move point."
3708 (interactive)
206c6f82 3709 (or (one-window-p) (delete-window))
a7ec1775 3710 (switch-to-buffer (marker-buffer reftex-toc-return-marker))
206c6f82 3711 (reftex-re-enlarge)
396e0b08 3712 (goto-char (or (marker-position reftex-toc-return-marker) (point))))
a7ec1775
RS
3713(defun reftex-toc-quit-and-kill ()
3714 "Kill the *toc* buffer."
3715 (interactive)
3716 (kill-buffer "*toc*")
206c6f82 3717 (or (one-window-p) (delete-window))
a7ec1775 3718 (switch-to-buffer (marker-buffer reftex-toc-return-marker))
206c6f82 3719 (reftex-re-enlarge)
a7ec1775 3720 (goto-char (marker-position reftex-toc-return-marker)))
c52bdfca 3721(defun reftex-toc-redo (&rest ignore)
396e0b08
KH
3722 "Regenerate the *toc* buffer by reparsing file of last reftex-toc command."
3723 (interactive)
3724 (if reftex-enable-partial-scans
3725 (let ((file (nth 3 (get-text-property (point) 'toc))))
3726 (if (not file)
3727 (error "Don't know which file to rescan. Try `R'.")
3728 (switch-to-buffer-other-window
3729 (reftex-get-file-buffer-force file))
3730 (setq current-prefix-arg '(4))
3731 (reftex-toc)))
3732 (reftex-toc-Redo))
3733 (reftex-kill-temporary-buffers))
3734(defun reftex-toc-Redo (&rest ignore)
3735 "Regenerate the *toc* buffer by reparsing the entire document."
a7ec1775 3736 (interactive)
206c6f82
RS
3737 (switch-to-buffer-other-window
3738 (reftex-get-file-buffer-force reftex-last-toc-file))
396e0b08 3739 (setq current-prefix-arg '(16))
a7ec1775 3740 (reftex-toc))
396e0b08
KH
3741(defun reftex-toc-external (&rest ignore)
3742 "Switch to table of contents of an external document."
3743 (interactive)
3744 (let* ((xr-alist (get-text-property 1 'xr-alist))
3745 (xr-index (reftex-select-external-document
3746 xr-alist 0)))
3747 (switch-to-buffer-other-window (or (reftex-get-file-buffer-force
3748 (cdr (nth xr-index xr-alist)))
3749 (error "Cannot switch document")))
3750 (reftex-toc)))
a7ec1775
RS
3751
3752(defun reftex-toc-visit-line (&optional final)
3753 ;; Visit the tex file corresponding to the toc entry on the current line.
3754 ;; If FINAL is t, stay there
3755 ;; If FINAL is 'hide, hide the *toc* window.
396e0b08
KH
3756 ;; Otherwise, move cursor back into *toc* window.
3757 ;; This function is pretty clever about finding back a section heading,
3758 ;; even if the buffer is not live, or things like outline, x-symbol etc.
3759 ;; have been active.
3760
3761 (let* ((toc (get-text-property (point) 'toc))
3762 (file (nth 3 toc))
3763 (marker (nth 4 toc))
3764 (level (nth 5 toc))
3765 (literal (nth 7 toc))
3766 (emergency-point (nth 8 toc))
3767 (toc-window (selected-window))
3768 show-window show-buffer match)
3769
3770 (unless toc (error "Don't know which toc line to visit"))
3771
3772 (setq match
3773 (cond
3774 ((and (markerp marker) (marker-buffer marker))
3775 ;; Buffer is still live and we have the marker. Should be easy.
3776 (switch-to-buffer-other-window (marker-buffer marker))
3777 (goto-char (marker-position marker))
3778 (or (looking-at (regexp-quote literal))
3779 (looking-at (reftex-make-regexp-allow-for-ctrl-m literal))
3780 (looking-at (reftex-make-desparate-section-regexp literal))
3781 (looking-at (concat "\\\\"
3782 (regexp-quote
3783 (car (rassq level reftex-section-levels)))
3784 "[[{]"))))
3785 (t
3786 ;; Marker is lost. Use the backup method.
3787 (switch-to-buffer-other-window
3788 (reftex-get-file-buffer-force file nil))
3789 (goto-char (or emergency-point (point-min)))
3790 (or (looking-at (regexp-quote literal))
3791 (let ((pos (point)))
3792 (re-search-backward "\\`\\|[\r\n][ \t]*[\r\n]" nil t)
3793 (or (reftex-nearest-match (regexp-quote literal) pos)
3794 (reftex-nearest-match
3795 (reftex-make-regexp-allow-for-ctrl-m literal) pos)
3796 (reftex-nearest-match
3797 (reftex-make-desparate-section-regexp literal) pos)))))
3798 ))
3799
3800 (setq show-window (selected-window)
3801 show-buffer (current-buffer))
3802
3803 (unless match
3804 (select-window toc-window)
3805 (error "Cannot find line"))
3806
3807 (goto-char (match-beginning 0))
a7ec1775 3808 (recenter 1)
396e0b08 3809 (reftex-highlight 0 (match-beginning 0) (match-end 0) (current-buffer))
a7ec1775
RS
3810
3811 (select-window toc-window)
3812
3813 ;; use the `final' parameter to decide what to do next
3814 (cond
206c6f82 3815 ((eq final t)
a7ec1775
RS
3816 (reftex-unhighlight 0)
3817 (select-window show-window))
3818 ((eq final 'hide)
3819 (reftex-unhighlight 0)
206c6f82
RS
3820 (or (one-window-p) (delete-window))
3821 (switch-to-buffer show-buffer)
3822 (reftex-re-enlarge))
a7ec1775
RS
3823 (t nil))))
3824
3825;;; ===========================================================================
3826;;;
3827;;; BibTeX citations.
3828
3829;; Variables and constants
3830
025bb635
RS
3831;; Define variable to silence compiler warnings
3832(defvar reftex-found-list)
206c6f82 3833(defvar reftex-cite-format-builtin)
a7ec1775
RS
3834
3835;; The history list of regular expressions used for citations
3836(defvar reftex-cite-regexp-hist nil)
3837
206c6f82
RS
3838;; Prompt and help string for citation selection
3839(defconst reftex-citation-prompt
3840 "Select: [n]ext [p]revious [r]estrict [ ]full_entry [q]uit RET [?]Help+more")
3841
a7ec1775
RS
3842(defconst reftex-citation-help
3843 "AVAILABLE KEYS IN MAKE CITATION MENU
3844---------------------------------------
206c6f82
RS
3845 n / p Go to next/previous entry (Cursor motion works as well).
3846 C-s / C-r Search forward/backward. Use repeated C-s/C-r as in isearch.
3847 g / r Start over with new regexp / Restrict with additional regexp.
3848 SPC Show full database entry in other window.
3849 f Toggle follow mode: Other window will follow with full db entry.
3850 q Quit without inserting \\cite macro into buffer.
396e0b08 3851 e Recursive edit into other window.
206c6f82 3852 RET / a Accept current entry / Accept all entries.")
a7ec1775
RS
3853
3854;; Find bibtex files
3855
3856(defun reftex-get-bibfile-list ()
396e0b08
KH
3857 ;; Return list of bibfiles for current document.
3858 ;; When using the chapterbib or bibunits package you should either
3859 ;; use the same database files everywhere, or separate parts using
3860 ;; different databases into different files (included into the mater file).
3861 ;; Then this function will return the applicable database files.
a7ec1775
RS
3862
3863 ;; Ensure access to scanning info
3864 (reftex-access-scan-info)
396e0b08
KH
3865 (or
3866 ;; Try inside this file (and its includes)
3867 (cdr (reftex-last-assoc-before-elt
3868 'bib (list 'eof (buffer-file-name))
3869 (member (list 'bof (buffer-file-name))
3870 (symbol-value reftex-docstruct-symbol))))
3871 ;; Try after the beginning of this file
3872 (cdr (assq 'bib (member (list 'bof (buffer-file-name))
3873 (symbol-value reftex-docstruct-symbol))))
3874 ;; Anywhere in the entire document
3875 (cdr (assq 'bib (symbol-value reftex-docstruct-symbol)))
3876 (error "\\bibliography statment missing or .bib files not found.")))
3877
3878(defun reftex-find-tex-file (file master-dir &optional die)
3879 ;; Find FILE in MASTER-DIR or on reftex-tex-path.
3880 ;; FILE may be given without the .tex extension.
3881 (reftex-access-search-path "tex")
3882 (let* ((path (cons master-dir reftex-tex-path))
3883 file1)
3884 (setq file1
3885 (or (reftex-find-file-on-path (concat file ".tex") path)
3886 (reftex-find-file-on-path file path)))
3887 (unless file1
3888 (reftex-access-search-path "tex" t file)
3889 (setq path (cons master-dir reftex-tex-path))
3890 (setq file1
3891 (or (reftex-find-file-on-path (concat file ".tex") path)
3892 (reftex-find-file-on-path file path))))
3893 (cond (file1 file1)
3894 (die (error "No such file: %s" file) nil)
3895 (t (message "No such file: %s (ignored)" file) nil))))
3896
3897(defun reftex-find-bib-file (file master-dir &optional die)
3898 ;; Find FILE in MASTER-DIR or on reftex-bib-path
3899 (reftex-access-search-path "bib")
3900 (let ((file1 (reftex-find-file-on-path
3901 file (cons master-dir reftex-bib-path))))
3902 (unless file1
3903 (reftex-access-search-path "bib" t file)
3904 (setq file1 (reftex-find-file-on-path
3905 file (cons master-dir reftex-bib-path))))
3906 (cond (file1 file1)
3907 (die (error "No such file: %s" file) nil)
3908 (t (message "No such file: %s (ignored)" file) nil))))
a7ec1775
RS
3909
3910;; Find a certain reference in any of the BibTeX files.
3911
3912(defun reftex-pop-to-bibtex-entry (key file-list
396e0b08 3913 &optional mark-to-kill highlight)
a7ec1775
RS
3914 ;; Find BibTeX KEY in any file in FILE-LIST in another window.
3915 ;; If mark-to-kill is non-nil, mark new buffer to kill."
3916
c52bdfca 3917 (let* ((re (concat "@[a-zA-Z]+[ \t\n\r]*[{(][ \t\n\r]*" (regexp-quote key) "[ \t\n\r,]"))
a7ec1775
RS
3918 (window-conf (current-window-configuration))
3919 file buf)
3920 (catch 'exit
3921 (switch-to-buffer-other-window (current-buffer))
3922 (while file-list
3923 (setq file (car file-list)
3924 file-list (cdr file-list))
396e0b08
KH
3925 (unless (setq buf (reftex-get-file-buffer-force file mark-to-kill))
3926 (error "No such file %s" file))
a7ec1775
RS
3927 (switch-to-buffer buf)
3928 (widen)
396e0b08
KH
3929 (goto-char (point-min))
3930 (when (re-search-forward re nil t)
3931 (goto-char (match-beginning 0))
3932 (recenter 0)
3933 (if highlight
3934 (reftex-highlight 0 (match-beginning 0) (match-end 0)))
3935 (throw 'exit (selected-window))))
a7ec1775
RS
3936 (set-window-configuration window-conf)
3937 (beep)
3938 (message "No BibTeX entry with citation key %s" key))))
3939
3940;; Parse bibtex buffers
3941
3942(defun reftex-extract-bib-entries (buffers &optional get-word)
3943 ;; Extract bib entries which match regexps from BUFFERS.
3944 ;; BUFFERS is a list of buffers or file names.
3945 ;; Return list with entries."
3946 (let* (re-list first-re rest-re
a7ec1775
RS
3947 (buffer-list (if (listp buffers) buffers (list buffers)))
3948 found-list entry buffer1 buffer alist
3949 key-point start-point end-point)
3950
396e0b08
KH
3951 (setq re-list (split-string
3952 (read-string "RegExp [ && RegExp...]: "
3953 nil 'reftex-cite-regexp-hist)
3954 "[ \t]*&&[ \t]*"))
a7ec1775
RS
3955
3956 (setq first-re (car re-list) ; We'll use the first re to find things,
3957 rest-re (cdr re-list)) ; the other to narrow down.
3958 (if (string-match "\\`[ \t]*\\'" first-re)
3959 (error "Empty regular expression"))
3960
3961 (save-excursion
3962 (save-window-excursion
3963
396e0b08 3964 ;; Walk through all bibtex files
a7ec1775
RS
3965 (while buffer-list
3966 (setq buffer (car buffer-list)
3967 buffer-list (cdr buffer-list))
3968 (if (and (bufferp buffer)
3969 (buffer-live-p buffer))
3970 (setq buffer1 buffer)
3971 (setq buffer1 (reftex-get-file-buffer-force
3972 buffer (not reftex-keep-temporary-buffers))))
3973 (if (not buffer1)
3974 (error "Cannot find BibTeX file %s" buffer)
3975 (message "Scanning bibliography database %s" buffer1))
3976
3977 (set-buffer buffer1)
396e0b08
KH
3978 (save-excursion
3979 (goto-char (point-min))
3980 (while (re-search-forward first-re nil t)
3981 (catch 'search-again
3982 (setq key-point (point))
3983 (unless (re-search-backward
3984 "\\(\\`\\|[\n\r]\\)[ \t]*@\\([a-zA-Z]+\\)[ \t\n\r]*[{(]" nil t)
3985 (throw 'search-again nil))
3986 (setq start-point (point))
3987 (goto-char (match-end 0))
3988 (condition-case nil
3989 (up-list 1)
3990 (error (goto-char key-point)
3991 (throw 'search-again nil)))
3992 (setq end-point (point))
3993
3994 ;; Ignore @string, @comment and @c entries or things
3995 ;; outside entries
3996 (when (or (string= (downcase (match-string 2)) "string")
3997 (string= (downcase (match-string 2)) "comment")
3998 (string= (downcase (match-string 2)) "c")
3999 (< (point) key-point)) ; this means match not in {}
4000 (goto-char key-point)
4001 (throw 'search-again nil))
4002
4003 ;; Well, we have got a match
4004 (setq entry (concat
4005 (buffer-substring start-point (point)) "\n"))
4006
4007 ;; Check if other regexp match as well
4008 (setq re-list rest-re)
4009 (while re-list
4010 (unless (string-match (car re-list) entry)
4011 ;; nope - move on
4012 (throw 'search-again nil))
4013 (pop re-list))
4014
4015 (setq alist (reftex-parse-bibtex-entry
4016 nil start-point end-point))
4017 (push (cons "&entry" entry) alist)
4018
4019 ;; check for crossref entries
4020 (if (assoc "crossref" alist)
4021 (setq alist
4022 (append
4023 alist (reftex-get-crossref-alist alist))))
4024
4025 ;; format the entry
4026 (push (cons "&formatted" (reftex-format-bib-entry alist))
4027 alist)
4028
4029 ;; add it to the list
4030 (push alist found-list))))
a7ec1775
RS
4031 (reftex-kill-temporary-buffers))))
4032 (setq found-list (nreverse found-list))
396e0b08 4033
a7ec1775 4034 ;; Sorting
396e0b08 4035 (cond
a7ec1775
RS
4036 ((eq 'author reftex-sort-bibtex-matches)
4037 (sort found-list 'reftex-bib-sort-author))
4038 ((eq 'year reftex-sort-bibtex-matches)
4039 (sort found-list 'reftex-bib-sort-year))
4040 ((eq 'reverse-year reftex-sort-bibtex-matches)
4041 (sort found-list 'reftex-bib-sort-year-reverse))
4042 (t found-list))))
4043
4044(defun reftex-bib-sort-author (e1 e2)
206c6f82 4045 (let ((al1 (reftex-get-bib-names "author" e1))
396e0b08 4046 (al2 (reftex-get-bib-names "author" e2)))
a7ec1775 4047 (while (and al1 al2 (string= (car al1) (car al2)))
396e0b08
KH
4048 (pop al1)
4049 (pop al2))
a7ec1775 4050 (if (and (stringp (car al1))
396e0b08
KH
4051 (stringp (car al2)))
4052 (string< (car al1) (car al2))
a7ec1775
RS
4053 (not (stringp (car al1))))))
4054
4055(defun reftex-bib-sort-year (e1 e2)
4056 (< (string-to-int (cdr (assoc "year" e1)))
4057 (string-to-int (cdr (assoc "year" e2)))))
4058
4059(defun reftex-bib-sort-year-reverse (e1 e2)
4060 (> (string-to-int (or (cdr (assoc "year" e1)) "0"))
4061 (string-to-int (or (cdr (assoc "year" e2)) "0"))))
4062
4063(defun reftex-get-crossref-alist (entry)
4064 ;; return the alist from a crossref entry
4065 (let ((crkey (cdr (assoc "crossref" entry)))
4066 start)
4067 (save-excursion
4068 (save-restriction
4069 (widen)
4070 (if (re-search-forward
396e0b08
KH
4071 (concat "@\\w+[{(][ \t\n\r]*" (regexp-quote crkey)
4072 "[ \t\n\r]*,") nil t)
a7ec1775
RS
4073 (progn
4074 (setq start (match-beginning 0))
4075 (condition-case nil
4076 (up-list 1)
206c6f82 4077 (error nil))
a7ec1775
RS
4078 (reftex-parse-bibtex-entry nil start (point)))
4079 nil)))))
4080
4081;; Parse and format individual entries
4082
206c6f82
RS
4083(defun reftex-get-bib-names (field entry)
4084 ;; Return a list with the author or editor anmes in ENTRY
4085 (let ((names (reftex-get-bib-field field entry)))
4086 (if (equal "" names)
4087 (setq names (reftex-get-bib-field "editor" entry)))
4088 (while (string-match "\\band\\b[ \t]*" names)
4089 (setq names (replace-match "\n" nil t names)))
4090 (while (string-match "[\\.a-zA-Z\\-]+\\.[ \t]*\\|,.*\\|[{}]+" names)
4091 (setq names (replace-match "" nil t names)))
4092 (while (string-match "^[ \t]+\\|[ \t]+$" names)
4093 (setq names (replace-match "" nil t names)))
4094 (while (string-match "[ \t][ \t]+" names)
4095 (setq names (replace-match " " nil t names)))
396e0b08 4096 (split-string names "\n")))
a7ec1775
RS
4097
4098(defun reftex-parse-bibtex-entry (entry &optional from to)
4099 (let (alist key start field)
4100 (save-excursion
4101 (save-restriction
4102 (if entry
4103 (progn
4104 (switch-to-buffer "*RefTeX-scratch*")
4105 (fundamental-mode)
4106 (erase-buffer)
4107 (insert entry))
4108 (widen)
4109 (narrow-to-region from to))
4110 (goto-char (point-min))
4111
396e0b08 4112 (if (re-search-forward
c52bdfca 4113 "@\\(\\w+\\)[ \t\n\r]*[{(][ \t\n\r]*\\([^ \t\n\r,]+\\)" nil t)
a7ec1775
RS
4114 (setq alist
4115 (list
396e0b08
KH
4116 (cons "&type" (downcase (reftex-match-string 1)))
4117 (cons "&key" (reftex-match-string 2)))))
a7ec1775 4118 (while (re-search-forward "\\(\\w+\\)[ \t\n\r]*=[ \t\n\r]*" nil t)
396e0b08 4119 (setq key (downcase (reftex-match-string 1)))
a7ec1775
RS
4120 (cond
4121 ((= (following-char) ?{)
4122 (forward-char 1)
4123 (setq start (point))
4124 (condition-case nil
4125 (up-list 1)
206c6f82 4126 (error nil)))
a7ec1775
RS
4127 ((= (following-char) ?\")
4128 (forward-char 1)
4129 (setq start (point))
4130 (while (and (search-forward "\"" nil t)
4131 (= ?\\ (char-after (- (point) 2))))))
4132 (t
4133 (setq start (point))
4134 (re-search-forward "[ \t\n\r,}]" nil 1)))
4135 (setq field (buffer-substring-no-properties start (1- (point))))
4136 ;; remove extra whitesp
4137 (while (string-match "[\n\t\r]\\|[ \t][ \t]+" field)
4138 (setq field (replace-match " " nil t field)))
4139 ;; remove leading garbage
4140 (if (string-match "^[ \t{]+" field)
4141 (setq field (replace-match "" nil t field)))
4142 ;; remove trailing garbage
4143 (if (string-match "[ \t}]+$" field)
4144 (setq field (replace-match "" nil t field)))
396e0b08
KH
4145 (push (cons key field) alist))))
4146 alist))
a7ec1775
RS
4147
4148(defun reftex-get-bib-field (fieldname entry)
4149 ;; Extract the field FIELDNAME from an ENTRY
4150 (or (cdr (assoc fieldname entry))
4151 ""))
4152
4153(defun reftex-format-bib-entry (entry)
4154 ;; Format a BibTeX ENTRY so that it is nice to look at
4155 (let*
396e0b08 4156 ((auth-list (reftex-get-bib-names "author" entry))
a7ec1775
RS
4157 (authors (mapconcat '(lambda (x) x) auth-list ", "))
4158 (year (reftex-get-bib-field "year" entry))
4159 (title (reftex-get-bib-field "title" entry))
4160 (type (reftex-get-bib-field "&type" entry))
4161 (key (reftex-get-bib-field "&key" entry))
4162 (extra
4163 (cond
4164 ((equal type "article")
4165 (concat (reftex-get-bib-field "journal" entry) " "
4166 (reftex-get-bib-field "volume" entry) ", "
4167 (reftex-get-bib-field "pages" entry)))
4168 ((equal type "book")
4169 (concat "book (" (reftex-get-bib-field "publisher" entry) ")"))
4170 ((equal type "phdthesis")
4171 (concat "PhD: " (reftex-get-bib-field "school" entry)))
4172 ((equal type "mastersthesis")
4173 (concat "Master: " (reftex-get-bib-field "school" entry)))
4174 ((equal type "inbook")
4175 (concat "Chap: " (reftex-get-bib-field "chapter" entry)
4176 ", pp. " (reftex-get-bib-field "pages" entry)))
4177 ((or (equal type "conference")
4178 (equal type "incollection")
4179 (equal type "inproceedings"))
4180 (concat "in: " (reftex-get-bib-field "booktitle" entry)))
4181 (t ""))))
396e0b08
KH
4182 (setq authors (reftex-truncate authors 30 t t))
4183 (when (reftex-use-fonts)
4184 (put-text-property 0 (length authors) 'face 'font-lock-keyword-face
4185 authors)
4186 (put-text-property 0 (length title) 'face 'font-lock-comment-face
4187 title)
4188 (put-text-property 0 (length extra) 'face 'font-lock-reference-face
4189 extra))
4190 (concat key "\n " authors " " year " " extra "\n " title "\n\n")))
a7ec1775
RS
4191
4192;; Make a citation
4193
396e0b08 4194(defun reftex-citation (&optional no-insert)
c52bdfca 4195 "Make a citation using BibTeX database files.
a7ec1775
RS
4196After asking for a Regular Expression, it scans the buffers with
4197bibtex entries (taken from the \\bibliography command) and offers the
c52bdfca
RS
4198matching entries for selection. The selected entry is formated according
4199to `reftex-cite-format' and inserted into the buffer.
a7ec1775 4200If NO-INSERT is non-nil, nothing is inserted, only the selected key returned.
c52bdfca
RS
4201The regular expression uses an expanded syntax: && is interpreted as `and'.
4202Thus, `aaaa&&bbb' matches entries which contain both `aaaa' and `bbb'.
fba437e6 4203When this function is called with point inside the braces of a \\cite
c52bdfca 4204command, it will add another key, ignoring the value of `reftex-cite-format'.
a7ec1775 4205When called with a numeric prefix, that many citations will be made and all
fba437e6 4206put into the same \\cite command.
a7ec1775
RS
4207When called with just C-u as prefix, enforces rescan of buffer for
4208bibliography statement (e.g. if it was changed)."
4209
396e0b08 4210 (interactive)
a7ec1775
RS
4211
4212 ;; check for recursive edit
4213 (reftex-check-recursive-edit)
4214
4215 ;; if there is just 1 C-u prefix arg, force to rescan buffer
396e0b08
KH
4216 (reftex-access-scan-info current-prefix-arg)
4217
4218 ;; Call reftex-do-citation, but protected
4219 (unwind-protect
4220 (reftex-do-citation current-prefix-arg no-insert)
4221 (reftex-kill-temporary-buffers)))
a7ec1775 4222
396e0b08
KH
4223(defun reftex-do-citation (&optional arg no-insert)
4224 ;; This really does the work of reftex-citation.
4225
4226 ;; Check if there is already a cite command at point and change cite format
a7ec1775 4227 ;; in order to only add another reference in the same cite command.
206c6f82
RS
4228 (let (key format (macro (car (car (reftex-what-macro t)))))
4229 (if (and (stringp macro)
396e0b08 4230 (string-match "\\`\\\\cite\\|cite\\'" macro))
a7ec1775 4231 (progn
a7ec1775
RS
4232 (cond
4233 ((or (not arg)
4234 (not (listp arg)))
206c6f82 4235 (setq format
a7ec1775
RS
4236 (concat
4237 (if (not (or (= (preceding-char) ?{)
4238 (= (preceding-char) ?,)))
4239 ","
4240 "")
206c6f82 4241 "%l"
a7ec1775
RS
4242 (if (not (or (= (following-char) ?})
4243 (= (following-char) ?,)))
4244 ","
4245 ""))))
4246 (t
206c6f82
RS
4247 (setq format "%l"))))
4248 ;; else: figure out the correct format
4249 (setq format
396e0b08
KH
4250 (cond
4251 ((stringp reftex-cite-format) reftex-cite-format)
4252 ((and (symbolp reftex-cite-format)
4253 (assq reftex-cite-format reftex-cite-format-builtin))
4254 (nth 2 (assq reftex-cite-format reftex-cite-format-builtin)))
4255 (t reftex-cite-format)))
206c6f82 4256 (if (listp format)
396e0b08
KH
4257 (save-window-excursion
4258 (with-output-to-temp-buffer "*RefTeX Select*"
4259 (princ "SELECT A CITATION FORMAT\n\n")
4260 (princ
4261 (mapconcat
4262 (function (lambda (x)
4263 (format "[%c] %s %s" (car x)
4264 (if (> (car x) 31) " " "")
4265 (cdr x))))
4266 format "\n")))
4267 (setq key (read-char))
4268 (if (assq key format)
4269 (setq format (cdr (assq key format)))
4270 (error "No citation format associated with key `%c'" key)))))
206c6f82
RS
4271
4272 (let* (entry cnt rtn ins-string re-list re
396e0b08
KH
4273 ;; scan bibtex files
4274 (reftex-found-list (reftex-extract-bib-entries
4275 (reftex-get-bibfile-list)))
4276 (found-list-r nil))
4277 (unless reftex-found-list
4278 (error "Sorry, no matches found"))
206c6f82
RS
4279
4280 ;; remember where we came from
4281 (setq reftex-call-back-to-this-buffer (current-buffer))
4282
4283 ;; offer selection
4284 (save-window-excursion
396e0b08
KH
4285 (switch-to-buffer-other-window "*RefTeX Select*")
4286 (erase-buffer)
4287 (reftex-insert-bib-matches reftex-found-list)
4288 (if (= 0 (buffer-size))
4289 (error "Sorry, no matches found"))
4290 (setq truncate-lines t)
4291 (goto-char 1)
4292 (if (catch 'exit
4293 (while t
4294 (setq rtn
4295 (reftex-select-item
4296 reftex-citation-prompt
4297 "^[^ \t\n\r]"
4298 4
4299 reftex-citation-help
4300 '(?r ?a ?g ?\C-m)
4301 nil
4302 'reftex-bibtex-selection-callback nil))
4303 (setq key (car rtn)
4304 cnt (nth 1 rtn))
4305 (unless key (throw 'exit nil))
4306 (cond
4307 ((eq key ?g)
4308 (setq reftex-found-list
4309 (save-excursion
4310 (set-buffer reftex-call-back-to-this-buffer)
4311 (reftex-extract-bib-entries
4312 (reftex-get-bibfile-list))))
4313 (erase-buffer)
4314 (reftex-insert-bib-matches reftex-found-list)
4315 (if (= 0 (buffer-size))
4316 (error "Sorry, no matches found"))
4317 (goto-char 1))
4318
4319 ((eq key ?r)
4320 ;; restrict with new regular expression
4321 (setq re-list
4322 (split-string (read-string
4323 "RegExp [ && RegExp...]: "
4324 nil 'reftex-cite-regexp-hist)
4325 "[ \t]*&&[ \t]*"))
4326 (while re-list
4327 (setq re (car re-list)
4328 re-list (cdr re-list))
4329 (setq found-list-r
4330 (delete ""
4331 (mapcar
4332 '(lambda (x)
4333 (if (string-match
4334 re (cdr (assoc "&entry" x)))
4335 x
4336 ""))
4337 reftex-found-list))))
4338 (if found-list-r
4339 (setq reftex-found-list found-list-r)
4340 (ding))
4341 (erase-buffer)
4342 (reftex-insert-bib-matches reftex-found-list)
4343 (goto-char 1))
4344 ((eq key ?a)
4345 (setq entry 'all)
4346 (throw 'exit t))
4347 ((or (eq key ?\C-m)
4348 (eq key 'return))
4349 (if cnt
4350 (setq entry (nth cnt reftex-found-list))
4351 (setq entry nil))
4352 (throw 'exit t))
4353 (t
4354 (ding)))))
4355 (progn
4356 ;; format the entry
4357 (if (eq entry 'all)
4358 (setq ins-string
4359 (mapconcat
4360 '(lambda (entry)
4361 (reftex-format-citation entry format))
4362 reftex-found-list "\n"))
4363 (setq ins-string (reftex-format-citation entry format))))
4364 (setq ins-string "")
4365 (message "Quit")))
206c6f82 4366 (kill-buffer "*RefTeX Select*")
a7ec1775 4367
396e0b08
KH
4368 (unless no-insert
4369 (insert ins-string)
4370 (when (string-match "\\?" ins-string)
4371 (search-backward "?")
4372 (delete-char 1)))
206c6f82 4373 (message "")
396e0b08 4374
206c6f82 4375 ;; Check if the prefix arg was numeric, and call recursively
396e0b08
KH
4376 (when (and (integerp arg)
4377 (> arg 1)
4378 (re-search-backward
4379 "\\\\\\([a-zA-Z]*cite\\|cite[a-zA-Z]*\\)\\**\\(\\[[^]]*\\]\\)*{\\([^}]*\\)" nil t))
4380 (goto-char (match-end 0))
4381 (decf arg)
4382 (reftex-do-citation arg))
4383
206c6f82
RS
4384 ;; Return the citation key
4385 (or (eq entry 'all)
396e0b08
KH
4386 (reftex-get-bib-field "&key" entry)))))
4387
206c6f82
RS
4388(defun reftex-insert-bib-matches (list)
4389 ;; Insert the bib matches and number them correctly
4390 (let ((cnt -1) tmp)
4391 (mapcar '(lambda (x)
396e0b08
KH
4392 (setq tmp (cdr (assoc "&formatted" x)))
4393 (incf cnt)
4394 (put-text-property 0 (length tmp) 'cnt cnt tmp)
4395 (insert tmp))
4396 list)))
206c6f82
RS
4397
4398(defun reftex-format-names (namelist n)
4399 (interactive)
4400 (let (last (len (length namelist)))
4401 (cond
4402 ((= 1 len) (car namelist))
4403 ((> len n) (concat (car namelist) (nth 2 reftex-cite-punctuation)))
4404 (t
4405 (setq n (min len n)
396e0b08 4406 last (nth (1- n) namelist))
206c6f82
RS
4407 (setcdr (nthcdr (- n 2) namelist) nil)
4408 (concat
4409 (mapconcat 'identity namelist (nth 0 reftex-cite-punctuation))
4410 (nth 1 reftex-cite-punctuation)
4411 last)))))
4412
4413(defun reftex-format-citation (entry format)
a7ec1775 4414 ;; Format a citation from the info in the BibTeX ENTRY
a7ec1775 4415
396e0b08 4416 (unless (stringp format) (setq format "\\cite{%l}"))
206c6f82
RS
4417
4418 (if (and reftex-comment-citations
396e0b08 4419 (string-match "%l" reftex-cite-comment-format))
206c6f82
RS
4420 (error "reftex-cite-comment-format contains illeagal %%l"))
4421
396e0b08
KH
4422 (while (string-match
4423 "\\(\\`\\|[^%]\\)\\(\\(%\\([0-9]*\\)\\([a-zA-Z]\\)\\)[.,;: ]*\\)"
4424 format)
206c6f82 4425 (let ((n (string-to-int (match-string 4 format)))
396e0b08
KH
4426 (l (string-to-char (match-string 5 format)))
4427 rpl b e)
206c6f82 4428 (save-match-data
396e0b08
KH
4429 (setq rpl
4430 (cond
4431 ((= l ?l) (concat
4432 (reftex-get-bib-field "&key" entry)
4433 (if reftex-comment-citations
4434 reftex-cite-comment-format
4435 "")))
4436 ((= l ?a) (reftex-format-names
4437 (reftex-get-bib-names "author" entry)
4438 (or n 2)))
4439 ((= l ?A) (car (reftex-get-bib-names "author" entry)))
4440 ((= l ?b) (reftex-get-bib-field "booktitle" entry))
4441 ((= l ?c) (reftex-get-bib-field "chapter" entry))
4442 ((= l ?d) (reftex-get-bib-field "edition" entry))
4443 ((= l ?e) (reftex-format-names
4444 (reftex-get-bib-names "editor" entry)
4445 (or n 2)))
4446 ((= l ?E) (car (reftex-get-bib-names "editor" entry)))
4447 ((= l ?h) (reftex-get-bib-field "howpublished" entry))
4448 ((= l ?i) (reftex-get-bib-field "institution" entry))
4449 ((= l ?j) (reftex-get-bib-field "journal" entry))
4450 ((= l ?k) (reftex-get-bib-field "key" entry))
4451 ((= l ?m) (reftex-get-bib-field "month" entry))
4452 ((= l ?n) (reftex-get-bib-field "number" entry))
4453 ((= l ?o) (reftex-get-bib-field "organization" entry))
4454 ((= l ?p) (reftex-get-bib-field "pages" entry))
4455 ((= l ?P) (car (split-string
4456 (reftex-get-bib-field "pages" entry)
4457 "[- .]+")))
4458 ((= l ?s) (reftex-get-bib-field "school" entry))
4459 ((= l ?u) (reftex-get-bib-field "publisher" entry))
4460 ((= l ?r) (reftex-get-bib-field "address" entry))
4461 ((= l ?t) (reftex-get-bib-field "title" entry))
4462 ((= l ?v) (reftex-get-bib-field "volume" entry))
4463 ((= l ?y) (reftex-get-bib-field "year" entry)))))
206c6f82
RS
4464
4465 (if (string= rpl "")
396e0b08
KH
4466 (setq b (match-beginning 2) e (match-end 2))
4467 (setq b (match-beginning 3) e (match-end 3)))
206c6f82
RS
4468 (setq format (concat (substring format 0 b) rpl (substring format e)))))
4469 (while (string-match "%%" format)
4470 (setq format (replace-match "%" t t format)))
4471 (while (string-match "[ ,.;:]*%<" format)
4472 (setq format (replace-match "" t t format)))
4473 format)
a7ec1775 4474
396e0b08 4475;; This is slow and not recommended for follow mode
a7ec1775
RS
4476(defun reftex-bibtex-selection-callback (cnt)
4477 ;; Callback function to be called from the BibTeX selection, in
c52bdfca 4478 ;; order to display context. This function is relatively slow and not
a7ec1775 4479 ;; recommended for follow mode, just for individual lookups.
a7ec1775 4480 (let ((win (selected-window))
025bb635 4481 (key (reftex-get-bib-field "&key" (nth cnt reftex-found-list)))
a7ec1775
RS
4482 (bibfile-list (save-excursion
4483 (set-buffer reftex-call-back-to-this-buffer)
4484 (reftex-get-bibfile-list))))
4485 (reftex-pop-to-bibtex-entry key bibfile-list
4486 (not reftex-keep-temporary-buffers) t)
4487 (select-window win)))
4488
4489;;; ===========================================================================
4490;;;
4491;;; Here is the routine used for selection
4492
4493;; Marker for return point from recursive edit
4494(defvar reftex-recursive-edit-marker (make-marker))
4495
4496(defun reftex-check-recursive-edit ()
c52bdfca 4497 ;; Check if we are already in a recursive edit. Abort with helpful
a7ec1775
RS
4498 ;; message if so.
4499 (if (marker-position reftex-recursive-edit-marker)
4500 (error
4501 (substitute-command-keys
4502 "In unfinished recursive edit. Finish (\\[exit-recursive-edit]) or abort (\\[abort-recursive-edit])."))))
4503
206c6f82 4504(defun reftex-select-item (prompt next-re size help-string
a7ec1775 4505 event-list &optional offset
206c6f82
RS
4506 call-back cb-flag match-everywhere)
4507;; Select an item. Show PROMPT to user, find next item with NEXT-RE
4508;; regular expression, return on any of the events listed in
4509;; EVENT-LIST. The function returns the event along with an integer
4510;; indicating which item was selected. When OFFSET is specified,
4511;; starts at that item in the list. When CALL-BACK is given, it is a
4512;; function which is called with the index of the element.
4513
4514 (let* (key key-sq b e ev cnt last-cnt cmd skip-callback
396e0b08
KH
4515 (search-str "") tmp search-start matched forward mini-map last-key
4516 (offset1 (or offset 1)) win1 win2)
206c6f82
RS
4517
4518 ;; Set up a minibuffer keymap for the search stuff
4519 (setq mini-map (copy-keymap minibuffer-local-map))
396e0b08 4520 (define-key mini-map "\C-s"
206c6f82 4521 '(lambda () (interactive) (setq forward t) (exit-minibuffer)))
396e0b08 4522 (define-key mini-map "\C-r"
206c6f82
RS
4523 '(lambda () (interactive) (setq forward nil) (exit-minibuffer)))
4524 (define-key mini-map "\C-m" 'exit-minibuffer)
4525
a7ec1775
RS
4526 (setq ev
4527 (catch 'exit
4528 (save-window-excursion
a7ec1775
RS
4529 (setq truncate-lines t)
4530 (goto-char 1)
396e0b08
KH
4531 (unless (re-search-forward next-re nil t offset1)
4532 ;; in case the offset is illegal
4533 (setq offset1 1)
4534 (re-search-forward next-re nil t offset1))
a7ec1775 4535 (beginning-of-line 1)
a7ec1775 4536 (while t
396e0b08
KH
4537 (setq last-cnt (or cnt last-cnt))
4538 (setq cnt (get-text-property (point) 'cnt))
206c6f82 4539 (if (and cnt cb-flag call-back (not skip-callback))
a7ec1775 4540 (funcall call-back cnt))
396e0b08
KH
4541 (setq skip-callback nil)
4542 (if cnt
4543 (setq b (or (previous-single-property-change
4544 (1+ (point)) 'cnt)
4545 (point-min))
4546 e (or (next-single-property-change
4547 (point) 'cnt)
4548 (point-max)))
4549 (setq b (point) e (point)))
4550 (reftex-highlight 1 b e)
a7ec1775
RS
4551 (if (or (not (pos-visible-in-window-p b))
4552 (not (pos-visible-in-window-p e)))
4553 (recenter (/ (window-height) 2)))
4554 (setq key-sq (read-key-sequence prompt))
396e0b08 4555 (setq last-key key)
a7ec1775
RS
4556 (setq key (car
4557 (cond
4558 ((fboundp 'listify-key-sequence) ; Emacs
4559 (listify-key-sequence key-sq))
4560 ((fboundp 'event-to-character) ; XEmacs
4561 (mapcar 'event-to-character key-sq))
4562 (t (error "Please report this problem to dominik@strw.leidenuniv.nl")))))
4563
4564 (setq cmd (key-binding key-sq))
4565
396e0b08 4566 (reftex-unhighlight 2)
a7ec1775
RS
4567 (reftex-unhighlight 0)
4568
4569 (cond
4570
396e0b08 4571 ;; Single line motions
206c6f82
RS
4572 ((or (eq key ?n)
4573 (eq key ?\C-i)
4574 (eq cmd 'next-line))
396e0b08 4575 (or (eobp) (forward-char 1))
206c6f82 4576 (re-search-forward next-re nil t 1)
a7ec1775 4577 (beginning-of-line 1))
206c6f82
RS
4578 ((or (eq key ?p)
4579 (eq cmd 'previous-line))
4580 (re-search-backward next-re nil t))
a7ec1775 4581
396e0b08 4582 ;; Page motions
206c6f82 4583 ((eq cmd 'scroll-up)
a7ec1775 4584 (while (and (pos-visible-in-window-p)
206c6f82 4585 (re-search-forward next-re nil t)))
a7ec1775
RS
4586 (beginning-of-line 1)
4587 (recenter 1))
206c6f82 4588 ((eq cmd 'scroll-down)
a7ec1775 4589 (while (and (pos-visible-in-window-p)
206c6f82 4590 (re-search-backward next-re nil t)))
a7ec1775
RS
4591 (recenter (- (window-height) size 2)))
4592
396e0b08
KH
4593 ;; Begin and end of buffer
4594 ((eq cmd 'beginning-of-buffer)
4595 (goto-char (point-min))
4596 (re-search-forward next-re nil t)
4597 (beginning-of-line 1))
4598 ((eq cmd 'end-of-buffer)
4599 (goto-char (point-max))
4600 (re-search-backward next-re nil t))
206c6f82 4601
396e0b08 4602 ;; Exit
206c6f82 4603 ((eq key ?q)
a7ec1775 4604 (throw 'exit nil))
206c6f82 4605 ((eq key ?\C-g)
396e0b08
KH
4606 (if (or (eq last-key ?\C-s) (eq last-key ?\C-r))
4607 (ding)
4608 (bury-buffer)
4609 (error "Abort")))
206c6f82
RS
4610 ((or (eq key ?\C-m)
4611 (eq key 'return)
4612 (eq cmd 'newline))
a7ec1775 4613 (throw 'exit 'return))
206c6f82
RS
4614 ((memq key event-list)
4615 (throw 'exit key))
a7ec1775 4616
396e0b08 4617 ;; Callback
206c6f82
RS
4618 ((or (eq key ?C) ; backward compatibility
4619 (eq key ?f))
a7ec1775 4620 (setq cb-flag (not cb-flag)))
206c6f82 4621 ((eq key ?\ )
396e0b08 4622 (if cnt (funcall call-back cnt) (ding)))
206c6f82 4623
396e0b08 4624 ;; Help
206c6f82 4625 ((eq key ?\?)
396e0b08
KH
4626 (with-output-to-temp-buffer "*RefTeX Help*"
4627 (princ help-string))
4628 (setq win1 (selected-window)
4629 win2 (get-buffer-window "*RefTeX Help*" t))
4630 (select-window win2)
4631 (unless (and (pos-visible-in-window-p 1)
4632 (pos-visible-in-window-p (point-max)))
4633 (enlarge-window (1+ (- (count-lines 1 (point-max))
4634 (window-height)))))
4635 (select-window win1)
4636 (setq skip-callback t))
4637
4638 ;; Searching
4639 ((or (setq forward (eq key ?\C-s)) (eq key ?\C-r))
4640 (if (or (and (not (eq last-key ?\C-s))
4641 (not (eq last-key ?\C-r)))
4642 (string= search-str ""))
4643 (setq tmp ; get a new string
4644 (read-from-minibuffer
4645 (if (string= search-str "")
4646 "Search: "
4647 (format "Search [%s]:" search-str))
4648 nil mini-map)
4649 search-str (if (string= tmp "")
4650 search-str tmp)))
4651 (setq search-start (point))
4652 (and (not (string= search-str ""))
4653 (progn
4654 (while
4655 (and (setq matched
4656 (if forward
4657 (search-forward search-str nil 1)
4658 (search-backward search-str nil 1)))
4659 (or (>= (save-excursion
4660 (goto-char (match-beginning 0))
4661 (current-column))
4662 (window-width))
4663 (not (or (get-text-property (point) 'cnt)
4664 match-everywhere)))))
4665 (if matched
4666 (reftex-highlight 2 (match-beginning 0)
4667 (match-end 0))
4668 (ding)
4669 (goto-char search-start)))))
4670
4671 ;; Recursive edit
4672 ((eq key ?e)
a7ec1775
RS
4673 (set-marker reftex-recursive-edit-marker (point))
4674 (unwind-protect
4675 (progn
4676 (save-window-excursion
4677 (save-excursion
4678 (other-window 1)
4679 (message
4680 (substitute-command-keys
c52bdfca 4681 "Recursive edit. Return to selection with \\[exit-recursive-edit]"))
a7ec1775 4682 (recursive-edit)))
396e0b08
KH
4683 (unless (equal (marker-buffer
4684 reftex-recursive-edit-marker)
4685 (current-buffer))
4686 (error "Cannot continue RefTeX from this buffer."))
a7ec1775
RS
4687 (goto-char reftex-recursive-edit-marker))
4688 (set-marker reftex-recursive-edit-marker nil)))
4689
a7ec1775
RS
4690 (t
4691 (ding)))))))
206c6f82 4692 (and (get-buffer "*RefTeX Help*") (kill-buffer "*RefTeX Help*"))
a7ec1775 4693 (message "")
206c6f82 4694 (list ev cnt last-cnt)))
a7ec1775
RS
4695
4696;;; ===========================================================================
4697;;;
4698;;; View cross references
4699
4700(defun reftex-view-crossref (&optional arg)
fba437e6
RS
4701 "View cross reference of \\ref or \\cite macro at point.
4702If the macro at point is a \\ref, show the corresponding label definition.
4703If it is a \\cite, show the BibTeX database entry.
a7ec1775
RS
4704If there is no such macro at point, search forward to find one.
4705When you call this function several times in direct successtion, point will
4706move to view subsequent cross references further down in the buffer.
206c6f82
RS
4707To cope with the plethora of variations in packages, this function
4708assumes any macro either starting with ending in `ref' or `cite' to contain
4709cross references.
a7ec1775
RS
4710With argument, actually select the window showing the cross reference."
4711
4712 (interactive "P")
4713
4714 ;; See where we are.
206c6f82 4715 (let* ((re "\\\\\\([a-z]*\\(cite\\|ref\\)\\|\\(cite\\|ref\\)[a-z]*\\)\\**\\(\\[[^{}]*\\]\\)?{")
396e0b08
KH
4716 (macro (car (car (reftex-what-macro t))))
4717 (this-word (reftex-this-word "*a-zA-Z\\\\"))
4718 (my-window (selected-window))
4719 pop-window cmd args point)
4720
4721 (if (and macro
4722 (string-match "\\`\\\\cite\\|\\`\\\\ref\\|cite\\'\\|ref\\'"
4723 macro))
4724 (and (setq macro (match-string 0 macro))
4725 (string-match "\\`\\\\" macro)
4726 (setq macro (substring macro 1)))
206c6f82 4727 (setq macro nil))
396e0b08 4728
a7ec1775 4729 (if (and macro (eq last-command this-command))
396e0b08
KH
4730 (if (string= macro "cite")
4731 (progn
4732 (skip-chars-forward "^},%")
4733 (while (and (eq (following-char) ?%)
4734 (or (beginning-of-line 2) t)
4735 (skip-chars-forward " \t\n\r")))
4736 (skip-chars-forward ",")
4737 (if (eq (following-char) ?})
4738 (setq macro nil)))
4739 (setq macro nil)))
4740
4741 (if (and (not macro)
4742 (or (not (string-match "\\`\\\\" this-word))
4743 (eq (following-char) ?\\)
4744 (search-backward "\\" nil t)
4745 t))
4746 (if (interactive-p)
4747 ;; Only move far if this function was called directly
4748 (and (re-search-forward re nil t)
4749 (setq macro (or (match-string 2) (match-string 3))))
4750 ;; The macro needs to be at point
4751 (and (looking-at re)
4752 (setq macro (or (match-string 2) (match-string 3)))
4753 (goto-char (match-end 0)))))
4754
4755
4756 (unless macro
4757 (error "No cross reference to display"))
a7ec1775
RS
4758
4759 ;; Ensure access to scanning info
4760 (reftex-access-scan-info)
4761
396e0b08 4762 (cond
a7ec1775
RS
4763 ((string= macro "cite")
4764 (setq cmd 'reftex-pop-to-bibtex-entry
396e0b08
KH
4765 args (list
4766 (reftex-this-word "^{},%\n\r")
4767 (reftex-get-bibfile-list) nil t)))
a7ec1775 4768 ((string= macro "ref")
396e0b08
KH
4769 (let* ((label (reftex-this-word "^{}%\n\r"))
4770 (xr-data (assoc 'xr (symbol-value reftex-docstruct-symbol)))
4771 (xr-re (nth 2 xr-data))
4772 (entry (assoc label (symbol-value reftex-docstruct-symbol))))
4773 (if (and (not entry) (string-match xr-re label))
4774 ;; Label is defined in external document
4775 (save-excursion
4776 (save-match-data
4777 (set-buffer
4778 (or (reftex-get-file-buffer-force
4779 (cdr (assoc (match-string 1 label) (nth 1 xr-data))))
4780 (error "Problem with external label %s" label))))
4781 (setq label (substring label (match-end 1)))
4782 (reftex-access-scan-info)
4783 (setq entry
4784 (assoc label (symbol-value reftex-docstruct-symbol)))))
a7ec1775
RS
4785 (if entry
4786 (setq cmd 'reftex-pop-to-label
396e0b08
KH
4787 args (list label (list (nth 3 entry)) nil t))
4788 (error "Label %s not known - reparse document might help" label))))
4789 (t (error "This should not happen (reftex-view-crossref)")))
a7ec1775
RS
4790 (setq point (point))
4791 (apply cmd args)
4792 (setq pop-window (selected-window))
4793 (add-hook 'pre-command-hook 'reftex-highlight-shall-die)
4794 (select-window my-window)
4795 (goto-char point)
4796 (and arg (select-window pop-window))))
4797
4798(defun reftex-mouse-view-crossref (ev)
fba437e6
RS
4799 "View cross reference of \\ref or \\cite macro where you click.
4800If the macro at point is a \\ref, show the corresponding label definition.
4801If it is a \\cite, show the BibTeX database entry.
a7ec1775 4802If there is no such macro at point, search forward to find one.
a7ec1775
RS
4803With argument, actually select the window showing the cross reference."
4804 (interactive "e")
4805 (mouse-set-point ev)
206c6f82 4806 (setq last-command 'self-insert-command) ;; make sure we do not move!
a7ec1775
RS
4807 (reftex-view-crossref current-prefix-arg))
4808
4809;;; ===========================================================================
4810;;;
4811;;; Functions that check out the surroundings
4812
4813(defun reftex-what-macro (which &optional bound)
4814 ;; Find out if point is within the arguments of any TeX-macro.
c52bdfca 4815 ;; The return value is either ("\\macro" . (point)) or a list of them.
a7ec1775
RS
4816
4817 ;; If WHICH is nil, immediately return nil.
4818 ;; If WHICH is t, return list of all macros enclosing point.
4819 ;; If WHICH is a list of macros, look only for those macros and return the
4820 ;; name of the first macro in this list found to enclose point.
4821 ;; If the optional BOUND is an integer, bound backwards directed
c52bdfca 4822 ;; searches to this point. If it is nil, limit to nearest \section -
a7ec1775
RS
4823 ;; like statement.
4824
4825 ;; This function is pretty stable, but can be fooled if the text contains
c52bdfca
RS
4826 ;; things like \macro{aa}{bb} where \macro is defined to take only one
4827 ;; argument. As RefTeX cannot know this, the string "bb" would still be
4828 ;; considered an argument of macro \macro.
a7ec1775
RS
4829
4830 (catch 'exit
4831 (if (null which) (throw 'exit nil))
4832 (let ((bound (or bound (save-excursion (re-search-backward
4833 reftex-section-regexp nil 1)
4834 (point))))
396e0b08 4835 pos cmd-list cmd cnt cnt-opt entry)
a7ec1775
RS
4836 (save-restriction
4837 (save-excursion
4838 (narrow-to-region (max 1 bound) (point-max))
4839 ;; move back out of the current parenthesis
4840 (while (condition-case nil
4841 (progn (up-list -1) t)
4842 (error nil))
396e0b08 4843 (setq cnt 1 cnt-opt 0)
a7ec1775 4844 ;; move back over any touching sexps
396e0b08
KH
4845 (while (and (reftex-move-to-previous-arg bound)
4846 (condition-case nil
4847 (progn (backward-sexp) t)
4848 (error nil)))
4849 (if (eq (following-char) ?\[) (incf cnt-opt))
4850 (incf cnt))
a7ec1775 4851 (setq pos (point))
396e0b08
KH
4852 (when (and (or (= (following-char) ?\[)
4853 (= (following-char) ?\{))
4854 (re-search-backward "\\\\[*a-zA-Z]+\\=" nil t))
4855 (setq cmd (reftex-match-string 0))
4856 (when (looking-at "\\\\begin{[^}]*}")
4857 (setq cmd (reftex-match-string 0)
4858 cnt (1- cnt)))
4859 ;; This does ignore optional arguments. Very hard to fix.
4860 (when (setq entry (assoc cmd reftex-env-or-mac-alist))
4861 (if (> cnt (or (nth 4 entry) 100))
4862 (setq cmd nil)))
4863 (cond
4864 ((null cmd))
4865 ((eq t which)
4866 (push (cons cmd (point)) cmd-list))
4867 ((member cmd which)
4868 (throw 'exit (cons cmd (point))))))
a7ec1775
RS
4869 (goto-char pos)))
4870 (nreverse cmd-list)))))
4871
4872(defun reftex-what-environment (which &optional bound)
4873 ;; Find out if point is inside a LaTeX environment.
c52bdfca 4874 ;; The return value is (e.g.) either ("equation" . (point)) or a list of
a7ec1775
RS
4875 ;; them.
4876
4877 ;; If WHICH is nil, immediately return nil.
4878 ;; If WHICH is t, return list of all environments enclosing point.
4879 ;; If WHICH is a list of environments, look only for those environments and
4880 ;; return the name of the first environment in this list found to enclose
4881 ;; point.
4882
4883 ;; If the optional BOUND is an integer, bound backwards directed searches to
c52bdfca 4884 ;; this point. If it is nil, limit to nearest \section - like statement.
a7ec1775
RS
4885
4886 (catch 'exit
4887 (save-excursion
4888 (if (null which) (throw 'exit nil))
4889 (let ((bound (or bound (save-excursion (re-search-backward
4890 reftex-section-regexp nil 1)
4891 (point))))
4892 env-list end-list env)
4893 (while (re-search-backward "\\\\\\(begin\\|end\\){\\([^}]+\\)}"
4894 bound t)
4895 (setq env (buffer-substring-no-properties
4896 (match-beginning 2) (match-end 2)))
4897 (cond
4898 ((string= (match-string 1) "end")
4899 (add-to-list 'end-list env))
4900 ((member env end-list)
4901 (setq end-list (delete env end-list)))
4902 ((eq t which)
396e0b08 4903 (push (cons env (point)) env-list))
a7ec1775
RS
4904 ((member env which)
4905 (throw 'exit (cons env (point))))))
4906 (nreverse env-list)))))
4907
4908(defun reftex-word-before-point ()
c52bdfca 4909 ;; Return the word before point. Word means here:
a7ec1775
RS
4910 ;; Consists of [a-zA-Z0-9.:] and ends at point or whitespace.
4911 (let ((pos (point)))
4912 (save-excursion
4913 (re-search-backward "[^ \t\n\r]" (point-min) 1)
396e0b08 4914 (setq pos (min (1+ (point)) (point-max)))
a7ec1775
RS
4915 (if (re-search-backward "[^a-zA-Z0-9\\\.:]" (point-min) 1)
4916 (forward-char 1))
4917 (buffer-substring-no-properties (point) pos))))
4918
4919;; ============================================================================
4920;;
4921;; Some generally useful functions
4922
4923(defun reftex-no-props (string)
4924 ;; Return STRING with all text properties removed
4925 (and (stringp string)
4926 (set-text-properties 0 (length string) nil string))
4927 string)
4928
396e0b08
KH
4929(defun reftex-match-string (n)
4930 ;; Match string without properties
4931 (when (match-beginning n)
4932 (buffer-substring-no-properties (match-beginning n) (match-end n))))
4933
4934(defun reftex-this-word (&optional class)
4935 ;; Grab the word around point.
4936 (setq class (or class "-a-zA-Z0-9:_/.*;|"))
4937 (save-excursion
4938 (buffer-substring-no-properties
4939 (progn (skip-chars-backward class) (point))
4940 (progn (skip-chars-forward class) (point)))))
4941
4942(defun reftex-truncate (string ncols &optional ellipses padding)
4943 ;; Truncate a string to NCHAR characters.
4944 ;; Works fast with ASCII and correctly with Mule characters.
4945 ;; When ELLIPSES is non-nil, put three dots at the end of the string.
4946 (setq string
4947 (cond
4948 ((and (boundp 'enable-multibyte-characters)
4949 enable-multibyte-characters)
4950 (if (<= (string-width string) ncols)
4951 string
4952 (if ellipses
4953 (concat (truncate-string-to-width string (- ncols 3)) "...")
4954 (truncate-string-to-width string ncols))))
4955 (t
4956 (if (<= (length string) ncols)
4957 string
4958 (if ellipses
4959 (concat (substring string 0 (- ncols 3)) "...")
4960 (substring string 0 ncols))))))
4961 (if padding
4962 (format (format "%%-%ds" ncols) string)
4963 string))
4964
4965(defun reftex-nearest-match (regexp &optional pos)
4966 ;; Find the nearest match of REGEXP. Set the match data.
4967 ;; If POS is given, calculate distances relative to it.
4968 ;; Return nil if there is no match.
4969 (let ((start (point)) (pos (or pos (point))) match1 match2 match)
4970 (goto-char start)
4971 (when (re-search-backward regexp nil t)
4972 (setq match1 (match-data)))
4973 (goto-char start)
4974 (when (re-search-forward regexp nil t)
4975 (setq match2 (match-data)))
4976 (goto-char start)
4977 (setq match
4978 (cond
4979 ((not match1) match2)
4980 ((not match2) match1)
4981 ((< (abs (- pos (car match1))) (abs (- pos (car match2)))) match1)
4982 (t match2)))
4983 (if match (progn (store-match-data match) t) nil)))
4984
4985(defun reftex-auto-mode-alist ()
4986 ;; Return an `auto-mode-alist' with only the .gz (etc) thingies.
4987 ;; Stolen from gnus nnheader.
4988 (let ((alist auto-mode-alist)
4989 out)
4990 (while alist
4991 (when (listp (cdr (car alist)))
4992 (push (car alist) out))
4993 (pop alist))
4994 (nreverse out)))
4995
4996(defun reftex-access-search-path (which &optional recurse file)
4997 ;; Access path from environment variables. WHICH is either "tex" or "bib".
4998 ;; When RECURSE is t, expand recursive paths, ending in double slash
4999 ;; FILE is just for the message.
5000 (let* ((pathvar (intern (concat "reftex-" which "-path")))
5001 (status (get pathvar 'status)))
5002 (cond
5003 ((eq status 'recursed))
5004 ((and status (null recurse)))
5005 ((null status)
5006 (let ((env-vars (if (equal which "tex") (list "TEXINPUTS")
5007 reftex-bibpath-environment-variables)))
5008 (set pathvar (reftex-parse-colon-path
5009 (mapconcat '(lambda(x) (or (getenv x) ""))
5010 env-vars path-separator))))
5011 (put pathvar 'status 'split))
5012 ((and (eq 'split status) recurse)
5013 (message "Expanding search path to find %s file: %s ..." which file)
5014 (set pathvar (reftex-expand-path (symbol-value pathvar)))
5015 (put pathvar 'status 'recursed)))))
5016
5017(defun reftex-find-file-on-path (file path)
5018 ;; Find FILE along the directory list PATH.
5019 (catch 'exit
5020 (when (file-name-absolute-p file)
5021 (if (file-exists-p file)
5022 (throw 'exit file)
5023 (throw 'exit nil)))
5024 (let* ((thepath path) file1 dir
5025 (doubleslash (concat "/" "/")))
5026 (while (setq dir (pop thepath))
5027 (when (string= (substring dir -2) doubleslash)
5028 (setq dir (substring dir 0 -1)))
5029 (setq file1 (expand-file-name file dir))
5030 (if (file-exists-p file1)
5031 (throw 'exit file1)))
5032 ;; No such file
5033 nil)))
5034
5035(defun reftex-parse-colon-path (path)
5036 ;; Like parse-colon-parse, but // or /~ have no effects.
5037 (mapcar 'file-name-as-directory
5038 (delete "" (split-string path (concat path-separator "+")))))
a7ec1775 5039
396e0b08
KH
5040(defun reftex-expand-path (path)
5041 ;; Expand parts of path ending in a double slash
5042 (let (path1 dir dirs (doubleslash (concat "/" "/")))
5043 (while (setq dir (pop path))
5044 (if (string= (substring dir -2) doubleslash)
5045 (progn
5046 (setq dir (substring dir 0 -1))
5047 (setq dirs (reftex-recursive-directory-list dir))
5048 (setq path1 (append dirs path1)))
5049 (push dir path1)))
5050 (nreverse path1)))
5051
5052(defun reftex-recursive-directory-list (dir)
5053 (let ((path (list dir)) dirs path1)
5054 (while (setq dir (pop path))
5055 (setq dirs
5056 (delete nil
5057 (mapcar (function
5058 (lambda (x)
5059 (if (and (file-directory-p x)
5060 (not (string-match "/\\.+\\'" x)))
5061 (file-name-as-directory x)
5062 nil)))
5063 (directory-files dir t))))
5064 (setq path (append dirs path))
5065 (push dir path1))
5066 path1))
5067
5068(defun reftex-make-regexp-allow-for-ctrl-m (string)
5069 ;; convert STRING into a regexp, allowing ^M for \n and vice versa
a7ec1775
RS
5070 (let ((start -2))
5071 (setq string (regexp-quote string))
5072 (while (setq start (string-match "[\n\r]" string (+ 3 start)))
5073 (setq string (replace-match "[\n\r]" nil t string)))
5074 string))
5075
396e0b08
KH
5076(defun reftex-make-desparate-section-regexp (old)
5077 ;; Return a regexp which will still match a section statement even if
5078 ;; x-symbol or isotex or the like have been at work in the mean time.
5079 (let* ((n (1+ (string-match "[[{]" old)))
5080 (new (regexp-quote (substring old 0 (1+ (string-match "[[{]" old)))))
5081 (old (substring old n)))
5082 (while (string-match
5083 "\\([\r\n]\\)\\|\\(\\`\\|[ \t\n\r]\\)\\([a-zA-Z0-9]+\\)\\([ \t\n\r]\\|}\\'\\)"
5084 old)
5085 (if (match-beginning 1)
5086 (setq new (concat new "[^\n\r]*[\n\r]"))
5087 (setq new (concat new "[^\n\r]*" (match-string 3 old))))
5088 (setq old (substring old (match-end 0))))
5089 new))
5090
a7ec1775
RS
5091(defun reftex-delete-list (elt-list list)
5092 ;; like delete, but with a list of things to delete
5093 ;; (original code from Rory Molinari)
5094 (while elt-list
5095 (setq list (delete (car elt-list) list)
5096 elt-list (cdr elt-list)))
5097 list)
5098
5099(defun reftex-get-buffer-visiting (file)
5100 ;; return a buffer visiting FILE
5101 (cond
5102 ((fboundp 'find-buffer-visiting) ; Emacs
5103 (find-buffer-visiting file))
5104 ((boundp 'find-file-compare-truenames) ; XEmacs
5105 (let ((find-file-compare-truenames t))
5106 (get-file-buffer file)))
5107 (t (error "Please report this problem to dominik@strw.leidenuniv.nl"))))
5108
5109(defun reftex-get-file-buffer-force (file &optional mark-to-kill)
c52bdfca 5110 ;; Return a buffer visiting file. Make one, if necessary.
396e0b08
KH
5111 ;; If neither such a buffer nor the file exist, return nil.
5112 ;; If MARK-TO-KILL is t and there is no live buffer, load the file with
5113 ;; initializations according to `reftex-initialize-temporary-buffers',
5114 ;; and mark the buffer to be killed after use.
a7ec1775
RS
5115
5116 (let ((buf (reftex-get-buffer-visiting file)))
396e0b08
KH
5117
5118 (cond (buf
5119 ;; We have it already as a buffer - just return it
5120 buf)
5121
5122 ((file-readable-p file)
5123 ;; At least there is such a file and we can read it.
5124
5125 (if (or (not mark-to-kill)
5126 (eq t reftex-initialize-temporary-buffers))
5127
5128 ;; Visit the file with full magic
5129 (setq buf (find-file-noselect file))
5130
5131 ;; Else: Visit the file just briefly, without or
5132 ;; with limited Magic
5133
5134 ;; The magic goes away
5135 (let ((format-alist nil)
5136 (auto-mode-alist (reftex-auto-mode-alist))
5137 (default-major-mode 'fundamental-mode)
5138 (after-insert-file-functions nil))
5139 (setq buf (find-file-noselect file)))
5140
5141 ;; Is there a hook to run?
5142 (when (listp reftex-initialize-temporary-buffers)
5143 (save-excursion
5144 (set-buffer buf)
5145 (run-hooks 'reftex-initialize-temporary-buffers))))
5146
5147 ;; Lets see if we got a license to kill :-|
5148 (and mark-to-kill
5149 (add-to-list 'reftex-buffers-to-kill buf))
5150
5151 ;; Return the new buffer
5152 buf)
5153
5154 ;; If no such file exists, return nil
5155 (t nil))))
a7ec1775
RS
5156
5157(defun reftex-splice-symbols-into-list (list alist)
5158 ;; Splice the association in ALIST of any symbols in LIST into the list.
5159 ;; Return new list.
5160 (let (rtn tmp)
5161 (while list
396e0b08
KH
5162 (while (and (not (null (car list))) ;; keep list elements nil
5163 (symbolp (car list)))
a7ec1775
RS
5164 (setq tmp (car list))
5165 (cond
5166 ((assoc tmp alist)
206c6f82 5167 (setq list (append (nth 2 (assoc tmp alist)) (cdr list))))
a7ec1775
RS
5168 (t
5169 (error "Cannot treat symbol %s in reftex-label-alist"
5170 (symbol-name tmp)))))
396e0b08 5171 (push (pop list) rtn))
a7ec1775
RS
5172 (nreverse rtn)))
5173
5174(defun reftex-uniquify (alist &optional keep-list)
c52bdfca
RS
5175 ;; Return a list of all elements in ALIST, but each car only once.
5176 ;; Elements of KEEP-LIST are not removed even if duplicate.
a7ec1775
RS
5177 (let (new elm)
5178 (while alist
396e0b08 5179 (setq elm (pop alist))
a7ec1775 5180 (if (or (member (car elm) keep-list)
396e0b08
KH
5181 (not (assoc (car elm) new)))
5182 (push elm new)))
5183 (nreverse new)))
a7ec1775
RS
5184
5185(defun reftex-use-fonts ()
c52bdfca 5186 ;; Return t if we can and want to use fonts.
a7ec1775
RS
5187 (and window-system
5188 reftex-use-fonts
396e0b08
KH
5189 (featurep 'font-lock)))
5190
5191(defun reftex-refontify ()
5192 ;; Return t if we need to refontify context
5193 (and (reftex-use-fonts)
5194 (or (eq t reftex-refontify-context)
5195 (and (eq 1 reftex-refontify-context)
5196 (or (featurep 'x-symbol))))))
a7ec1775 5197
c52bdfca 5198;; Highlighting uses overlays. If this is for XEmacs, we need to load
a7ec1775
RS
5199;; the overlay library, available in version 19.15
5200(and (not (fboundp 'make-overlay))
5201 (condition-case nil
5202 (require 'overlay)
396e0b08 5203 (error
a7ec1775
RS
5204 (error "RefTeX needs overlay emulation (available in XEmacs 19.15)"))))
5205
5206;; We keep a vector with several different overlays to do our highlighting.
206c6f82 5207(defvar reftex-highlight-overlays [nil nil nil])
a7ec1775
RS
5208
5209;; Initialize the overlays
5210(aset reftex-highlight-overlays 0 (make-overlay 1 1))
5211(overlay-put (aref reftex-highlight-overlays 0) 'face 'highlight)
5212(aset reftex-highlight-overlays 1 (make-overlay 1 1))
5213(overlay-put (aref reftex-highlight-overlays 1) 'face 'highlight)
206c6f82 5214(aset reftex-highlight-overlays 2 (make-overlay 1 1))
396e0b08
KH
5215(overlay-put (aref reftex-highlight-overlays 2) 'face
5216 (if (string-match "XEmacs" emacs-version) 'zmacs-region 'region))
a7ec1775
RS
5217
5218;; Two functions for activating and deactivation highlight overlays
5219(defun reftex-highlight (index begin end &optional buffer)
5220 "Highlight a region with overlay INDEX."
5221 (move-overlay (aref reftex-highlight-overlays index)
5222 begin end (or buffer (current-buffer))))
5223(defun reftex-unhighlight (index)
5224 "Detatch overlay INDEX."
5225 (delete-overlay (aref reftex-highlight-overlays index)))
5226
5227(defun reftex-highlight-shall-die ()
c52bdfca 5228 ;; Function used in pre-command-hook to remove highlights.
a7ec1775
RS
5229 (remove-hook 'pre-command-hook 'reftex-highlight-shall-die)
5230 (reftex-unhighlight 0))
5231
a7ec1775
RS
5232;;; ---------------------------------------------------------------------------
5233;;;
5234;;; Functions to compile the tables, reset the mode etc.
5235
5236(defun reftex-reset-mode ()
c52bdfca
RS
5237 "Reset RefTeX Mode. Required to implement changes to some list variables.
5238This function will compile the information in `reftex-label-alist' and similar
5239variables. It is called when RefTeX is first used, and after changes to
396e0b08 5240these variables."
a7ec1775
RS
5241 (interactive)
5242
025bb635 5243 ;; Record that we have done this
a7ec1775 5244 (setq reftex-tables-dirty nil)
396e0b08
KH
5245 (setq reftex-memory
5246 (list reftex-label-alist reftex-label-alist-external-add-ons
5247 reftex-default-label-alist-entries))
5248
5249 ;; Reset the file search path variables
5250 (put 'reftex-tex-path 'status nil)
5251 (put 'reftex-bib-path 'status nil)
a7ec1775 5252
025bb635
RS
5253 ;; Kill temporary buffers associated with RefTeX - just in case they
5254 ;; were not cleaned up properly
206c6f82 5255 (let ((buffer-list '("*RefTeX Master*" "*RefTeX Help*" "*RefTeX Select*"
396e0b08
KH
5256 "*Duplicate Labels*" "*toc*" "*RefTeX-scratch*"))
5257 buf)
5258 (while (setq buf (pop buffer-list))
5259 (if (get-buffer buf)
5260 (kill-buffer buf))))
025bb635 5261
396e0b08 5262 ;; Make sure the current document will be rescanned soon.
206c6f82
RS
5263 (reftex-reset-scanning-information)
5264
396e0b08 5265 ;; Plug functions into AUCTeX if the user option says so.
c52bdfca
RS
5266 (reftex-plug-into-AUCTeX)
5267
a7ec1775
RS
5268 (message "updating internal tables...")
5269 (reftex-compute-ref-cite-tables)
206c6f82 5270 (message "updating internal tables... done"))
a7ec1775
RS
5271
5272(defun reftex-reset-scanning-information ()
5273 "Reset the symbols containing information from buffer scanning.
5274This enforces rescanning the buffer on next use."
206c6f82
RS
5275 (if (string= reftex-last-toc-master (reftex-TeX-master-file))
5276 (reftex-empty-toc-buffer))
a7ec1775
RS
5277 (let ((symlist reftex-multifile-symbols)
5278 symbol)
5279 (while symlist
5280 (setq symbol (car symlist)
5281 symlist (cdr symlist))
5282 (if (and (symbolp (symbol-value symbol))
5283 (not (null (symbol-value symbol))))
5284 (set (symbol-value symbol) nil)))))
5285
396e0b08
KH
5286(defcustom reftex-default-context-regexps
5287 '((caption . "\\\\\\(rot\\)?caption\\*?\\(\\[[^]]*\\]\\)?{")
5288 (item . "\\\\item\\(\\[[^]]*\\]\\)?")
5289 (eqnarray-like . "\\\\begin{%s}\\|\\\\\\\\")
5290 (alignat-like . "\\\\begin{%s}{[0-9]*}\\|\\\\\\\\"))
5291"Alist with default regular expressions for finding context.
5292The form (format regexp (regexp-quote environment)) is used to calculate
5293the final regular expression - so %s will be replaced with the environment
5294or macro."
5295 :group 'reftex-defining-label-environments
5296 :type '(repeat (cons (symbol) (regexp))))
5297
a7ec1775
RS
5298(defun reftex-compute-ref-cite-tables ()
5299 ;; Update ref and cite tables
5300
5301 (interactive)
5302
5303 ;; Compile information in reftex-label-alist
5304 (let ((tmp (reftex-uniquify (reftex-splice-symbols-into-list
396e0b08
KH
5305 (append
5306 reftex-label-alist
5307 reftex-label-alist-external-add-ons
5308 reftex-default-label-alist-entries)
5309 reftex-label-alist-builtin)
5310 '(nil)))
5311 entry env-or-mac typekeychar typekey prefix context word
5312 fmt reffmt labelfmt wordlist qh-list macros-with-labels
5313 nargs nlabel opt-args cell sum)
a7ec1775
RS
5314
5315 (setq reftex-words-to-typekey-alist nil
5316 reftex-typekey-list nil
5317 reftex-typekey-to-format-alist nil
5318 reftex-typekey-to-prefix-alist nil
5319 reftex-env-or-mac-alist nil
5320 reftex-label-env-list nil
5321 reftex-label-mac-list nil)
5322 (while tmp
5323 (catch 'next-entry
396e0b08
KH
5324 (setq entry (car tmp)
5325 env-or-mac (car entry)
5326 entry (cdr entry)
5327 tmp (cdr tmp))
5328 (if (null env-or-mac)
5329 (setq env-or-mac ""))
5330 (if (stringp (car entry))
5331 ;; This is before version 2.00 - convert entry to new format
5332 ;; This is just to keep old users happy
5333 (setq entry (cons (string-to-char (car entry))
5334 (cons (concat (car entry) ":")
5335 (cdr entry)))))
5336 (setq typekeychar (nth 0 entry)
5337 typekey (char-to-string typekeychar)
5338 prefix (nth 1 entry)
5339 fmt (nth 2 entry)
5340 context (nth 3 entry)
5341 wordlist (nth 4 entry))
5342 (if (stringp wordlist)
5343 ;; This is before version 2.04 - convert to new format
5344 (setq wordlist (nthcdr 4 entry)))
5345
5346 (if (and (stringp fmt)
5347 (string-match "@" fmt))
5348 ;; special syntax for specifying a label format
5349 (setq fmt (split-string fmt "@+"))
5350 (setq fmt (list "\\label{%s}" fmt)))
5351 (setq labelfmt (car fmt)
5352 reffmt (nth 1 fmt))
5353 (if typekey
5354 (add-to-list 'reftex-typekey-list typekey))
5355 (if (and typekey prefix
5356 (not (assoc typekey reftex-typekey-to-prefix-alist)))
5357 (add-to-list 'reftex-typekey-to-prefix-alist
5358 (cons typekey prefix)))
5359 (cond
5360 ((string-match "\\`\\\\" env-or-mac)
5361 ;; It's a macro
5362 (let ((result (reftex-parse-args env-or-mac)))
5363 (setq env-or-mac (or (first result) env-or-mac)
5364 nargs (second result)
5365 nlabel (third result)
5366 opt-args (fourth result))
5367 (if nlabel (add-to-list 'macros-with-labels env-or-mac)))
5368 (add-to-list 'reftex-label-mac-list env-or-mac))
5369 (t
5370 (setq nargs nil nlabel nil opt-args nil)
5371 (cond ((string= env-or-mac "any"))
5372 ((string= env-or-mac ""))
5373 ((string= env-or-mac "section"))
5374 (t
5375 (add-to-list 'reftex-label-env-list env-or-mac)
5376 ;; Translate some special context cases
5377 (when (assq context reftex-default-context-regexps)
5378 (setq context
5379 (format
5380 (cdr (assq context reftex-default-context-regexps))
5381 (regexp-quote env-or-mac))))))))
5382 (and reffmt
5383 (not (assoc typekey reftex-typekey-to-format-alist))
5384 (push (cons typekey reffmt) reftex-typekey-to-format-alist))
5385 (and (not (string= env-or-mac "any"))
5386 (not (string= env-or-mac ""))
5387 (not (assoc env-or-mac reftex-env-or-mac-alist))
5388 (push (list env-or-mac typekey context labelfmt
5389 nargs nlabel opt-args)
5390 reftex-env-or-mac-alist))
5391 (while (and (setq word (pop wordlist))
5392 (stringp word))
5393 (setq word (downcase word))
5394 (or (assoc word reftex-words-to-typekey-alist)
5395 (push (cons word typekey) reftex-words-to-typekey-alist)))
5396 (cond
5397 ((string= "" env-or-mac) nil)
5398 ((setq cell (assoc typekey qh-list))
5399 (push env-or-mac (cdr cell)))
5400 (t
5401 (push (list typekey env-or-mac) qh-list)))))
a7ec1775 5402
396e0b08 5403 (setq qh-list (sort qh-list '(lambda (x1 x2) (string< (car x1) (car x2)))))
a7ec1775
RS
5404 (setq reftex-typekey-to-prefix-alist
5405 (nreverse reftex-typekey-to-prefix-alist))
5406 (setq reftex-type-query-prompt
5407 (concat "Label type: "
5408 (mapconcat '(lambda(x)
5409 (format "[%s]" (car x)))
5410 qh-list " ")
5411 " (?=Help)"))
5412 (setq reftex-type-query-help
5413 (concat "SELECT A LABEL TYPE:\n--------------------\n"
396e0b08
KH
5414 (mapconcat
5415 '(lambda(x)
5416 (setq sum 0)
5417 (format " [%s] %s"
5418 (car x)
5419 (mapconcat
5420 '(lambda(x)
5421 (setq sum (+ sum (length x)))
5422 (if (< sum 60)
5423 x
5424 (setq sum 0)
5425 (concat "\n " x)))
5426 (cdr x) " ")))
5427 qh-list "\n")))
5428
5429 ;; Calculate the regular expressions
5430 (let ((label-re "\\\\label{\\([^}]*\\)}")
5431 (include-re "\\(\\`\\|[\n\r]\\)[ \t]*\\\\\\(include\\|input\\)[{ \t]+\\([^} \t\n\r]+\\)")
5432 (section-re
5433 (concat "\\(\\`\\|[\n\r]\\)[ \t]*\\\\\\("
5434 (mapconcat 'car reftex-section-levels "\\|")
5435 "\\)\\*?\\(\\[[^]]*\\]\\)?{"))
5436 (macro-re
5437 (if macros-with-labels
5438 (concat "\\("
5439 (mapconcat 'regexp-quote macros-with-labels "\\|")
5440 "\\)[[{]")
5441 ""))
5442 (find-label-re-format
5443 (concat "\\("
5444 (mapconcat 'regexp-quote (append '("\\label")
5445 macros-with-labels) "\\|")
5446 "\\)\\([[{][^]}]*[]}]\\)*[[{]\\(%s\\)[]}]")))
5447 (setq reftex-section-regexp section-re
5448 reftex-section-or-include-regexp
5449 (concat section-re "\\|" include-re)
5450 reftex-everything-regexp
5451 (concat label-re "\\|" section-re "\\|" include-re
5452 (if macros-with-labels "\\|" "") macro-re)
5453 reftex-find-label-regexp-format find-label-re-format
5454 reftex-find-label-regexp-format2
5455 "\\([]} \t\n\r]\\)\\([[{]\\)\\(%s\\)[]}]"))))
a7ec1775
RS
5456
5457;;; Keybindings --------------------------------------------------------------
5458
a7ec1775
RS
5459(define-key reftex-mode-map "\C-c=" 'reftex-toc)
5460(define-key reftex-mode-map "\C-c(" 'reftex-label)
5461(define-key reftex-mode-map "\C-c)" 'reftex-reference)
5462(define-key reftex-mode-map "\C-c[" 'reftex-citation)
5463(define-key reftex-mode-map "\C-c&" 'reftex-view-crossref)
5464
5465;; If the user requests so, she can have a few more bindings:
5466(cond
5467 (reftex-extra-bindings
5468 (define-key reftex-mode-map "\C-ct" 'reftex-toc)
5469 (define-key reftex-mode-map "\C-cl" 'reftex-label)
5470 (define-key reftex-mode-map "\C-cr" 'reftex-reference)
5471 (define-key reftex-mode-map "\C-cc" 'reftex-citation)
5472 (define-key reftex-mode-map "\C-cv" 'reftex-view-crossref)
5473 (define-key reftex-mode-map "\C-cg" 'reftex-grep-document)
5474 (define-key reftex-mode-map "\C-cs" 'reftex-search-document)))
5475
5476;;; Menus --------------------------------------------------------------------
5477
5478;; Define a menu for the menu bar if Emacs is running under X
5479
5480(require 'easymenu)
5481
396e0b08 5482(easy-menu-define
a7ec1775
RS
5483 reftex-mode-menu reftex-mode-map
5484 "Menu used in RefTeX mode"
396e0b08 5485 `("Ref"
206c6f82 5486 ["Table of Contents" reftex-toc t]
a7ec1775 5487 "----"
206c6f82
RS
5488 ["\\label" reftex-label t]
5489 ["\\ref" reftex-reference t]
5490 ["\\cite" reftex-citation t]
396e0b08 5491 ["View Crossref" reftex-view-crossref t]
a7ec1775 5492 "----"
396e0b08
KH
5493 ("Multifile"
5494 ["Search Whole Document" reftex-search-document t]
5495 ["Replace in Document" reftex-query-replace-document t]
5496 ["Grep on Document" reftex-grep-document t]
5497 "----"
5498 ["Create TAGS File" reftex-create-tags-file t]
a7ec1775 5499 "----"
396e0b08
KH
5500 ["Find Duplicate Labels" reftex-find-duplicate-labels t]
5501 ["Change Label and Refs" reftex-change-label t])
5502 ("Parse Document"
5503 ["Only this File" reftex-parse-one t]
5504 ["Entire Document" reftex-parse-all (reftex-is-multi)]
5505 ["Save to file" (reftex-access-parse-file 'write)
5506 (> (length (symbol-value reftex-docstruct-symbol)) 0)]
5507 ["Restore from File" (reftex-access-parse-file 'restore)
5508 (reftex-access-parse-file 'readable)]
a7ec1775 5509 "----"
396e0b08
KH
5510 ["Turn Auto-Save On" (setq reftex-save-parse-info t)
5511 (not reftex-save-parse-info)]
5512 ["Turn Auto-Save Off" (setq reftex-save-parse-info nil)
5513 reftex-save-parse-info]
5514 "---"
5515 ["Reset RefTeX Mode" reftex-reset-mode t])
a7ec1775 5516 "----"
396e0b08
KH
5517 ["Customize RefTeX" reftex-customize t]
5518 ("Set Citation Format"
5519 ,@(mapcar
5520 (function
5521 (lambda (x)
5522 (vector
5523 (symbol-name (car x))
5524 (list 'setq 'reftex-cite-format (list 'quote (car x)))
5525 (list 'not (list 'eq 'reftex-cite-format
5526 (list 'quote (car x)))))))
5527 reftex-cite-format-builtin)
5528 "----"
5529 ["Turn Comments On" (setq reftex-comment-citations t)
5530 (not reftex-comment-citations)]
5531 ["Turn Comments Off" (setq reftex-comment-citations nil)
5532 reftex-comment-citations])
5533 "----"
5534 ["Show Documentation" reftex-show-commentary t]))
a7ec1775
RS
5535
5536;;; Run Hook ------------------------------------------------------------------
5537
5538(run-hooks 'reftex-load-hook)
5539
5540;;; That's it! ----------------------------------------------------------------
5541
396e0b08
KH
5542(provide 'reftex)
5543
a7ec1775
RS
5544; Make sure tabels are compiled
5545(message "updating internal tables...")
5546(reftex-compute-ref-cite-tables)
c52bdfca 5547(message "updating internal tables...done")
a7ec1775
RS
5548(setq reftex-tables-dirty nil)
5549
a7ec1775
RS
5550;;;============================================================================
5551
c52bdfca 5552;;; reftex.el ends here