(files): New group `files'.
[bpt/emacs.git] / lisp / textmodes / reftex.el
CommitLineData
fba437e6 1;; reftex.el --- Minor mode for doing \label, \ref and \cite in LaTeX
f52b232e 2
a7ec1775
RS
3;; Copyright (c) 1997 Free Software Foundation, Inc.
4
5;; Author: Carsten Dominik <dominik@strw.LeidenUniv.nl>
6;; Keywords: tex
7
8;; This file is part of GNU Emacs.
9
10;; GNU Emacs is free software; you can redistribute it and/or modify
11;; it under the terms of the GNU General Public License as published by
12;; the Free Software Foundation; either version 2, or (at your option)
13;; any later version.
14
15;; GNU Emacs is distributed in the hope that it will be useful,
16;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18;; GNU General Public License for more details.
19
20;; You should have received a copy of the GNU General Public License
21;; along with GNU Emacs; see the file COPYING. If not, write to the
22;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23;; Boston, MA 02111-1307, USA.
24
25;;---------------------------------------------------------------------------
26;;
27;;; Commentary:
28;;
fba437e6
RS
29;; RefTeX is a minor mode with distinct support for \ref, \label and
30;; \cite commands in (multi-file) LaTeX documents.
a7ec1775
RS
31;; Labels are created semi-automatically. Definition context of labels is
32;; provided when creating a reference. Citations are simplified with
33;; efficient database lookup.
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;;
44;; For key bindings, see further down in this documentation.
45;;
46;;---------------------------------------------------------------------------
47;;
48;; OVERVIEW
49;;
fba437e6 50;; 1. USING \label AND \ref. Labels and references are one of the
a7ec1775
RS
51;; strong points of LaTeX. But, in documents with hundreds of
52;; equations, figures, tables etc. it becomes quickly impossible to
53;; find good label names and to actually remember them. Then, also
54;; completion of labels in not enough. One actually needs to see the
55;; context of the label definition to find the right one.
56;;
57;; - RefTeX distinguishes labels for different environments. It
58;; always knows if a certain label references a figure, table
59;; etc. You can configure RefTeX to recognize any additional
60;; labeled environments you might have defined yourself.
61;;
62;; - RefTeX defines automatically unique labels. Type `C-c ('
63;; (reftex-label) to insert a label at point. RefTeX will either
64;; - derive a label from context (default for section labels)
65;; - insert a simple label consisting of a prefix and a number
66;; (default for equations and enumerate items) or
67;; - prompt for a label string (figures and tables)
68;; Which labels are created how can be controlled with the variable
69;; reftex-insert-label-flags.
70;;
71;; - Referencing labels is a snap and I promise you'll love it.
72;; In order to make a reference, type `C-c )' (reftex-reference).
73;; This shows an outline of the documents with all labels of a
74;; certain type (figure, equation,...) and context of the label
fba437e6 75;; definition. Selecting one of the labels inserts a \ref macro
a7ec1775
RS
76;; into the original buffer. Online help during the selection is
77;; available with `?'.
78;;
79;; 2. CITATIONS. After typing `C-c [' (reftex-citation), RefTeX will
80;; let you specify a regexp to search in current BibTeX database files
81;; (as specified in the \bibliography command) and pull out a formatted
82;; list of matches for you to choose from. The list is *formatted* and
83;; thus much easier to read than the raw database entries. It can also
84;; be sorted. The text inserted into the buffer is by default just
85;; `\cite{KEY}', but can also contain author names and the year in a
86;; configurable way. See documentation of the variable
87;; reftex-cite-format.
88;;
89;; 3. TABLE OF CONTENTS. Typing `C-c =' (reftex-toc) will show
90;; a table of contents of the document. From that buffer, you can
91;; jump quickly to every part of your document. This is similar to
92;; imenu, only it works for entire multifile documents and uses the
93;; keyboard rather than the mouse. The initial version of this
94;; function was contributed by Stephen Eglen.
95;;
96;; 4. MULTIFILE DOCUMENTS are supported in the same way as by AUCTeX.
97;; I.e. if a source file is not a full LaTeX document by itself,
98;; but included by another file, you may specify the name of
99;; the (top level) master file in a local variable section at the
100;; end of the source file, like so:
101;;
102;; %%% Local Variables:
103;; %%% TeX-master: my_master.tex
104;; %%% End:
105;;
106;; This will only take effect when you load the file next time or when
107;; you reset RefTeX with M-x reftex-reset-mode.
108;;
109;; RefTeX will also recognize the file variable tex-main-file. This
110;; variable is used by the Emacs TeX modes and works just like
111;; AUCTeX's TeX-master variable. See the documentation of your TeX/LaTeX
112;; modes.
113;;
114;; RefTeX knows about all files related to a document via input and
115;; include. It provides functions to run regular expression searches and
116;; replaces over the entire document and to create a TAGS file.
117;;
118;; 5. DOCUMENT PARSING. RefTeX needs to parse the document in order to find
119;; labels and other information. It will do it automatically once, when
120;; you start working with a document. If you need to enforce reparsing
121;; later, call any of the functions reftex-citation, reftex-label,
122;; reftex-reference, reftex-toc with a raw C-u prefix.
123;;
124;;-------------------------------------------------------------------------
125;;
126;; CONFIGURATION
127;;
128;; RefTeX contains many configurable options which change the way it works.
129;;
130;; Most importantly, RefTeX needs to be configured if you use labels to
131;; mark non-standard environments. RefTeX always understands LaTeX section
132;; commands and the following environments: figure, figure*,
133;; sidewaysfigure, table, table*, sidewaystable, equation, eqnarray,
134;; enumerate. For everythings else, it needs to be configured.
135;;
136;; A good way to configure RefTeX is with the custom.el package by Per
137;; Abrahamsen, shipped with Emacs 20 and XEmacs 19.15. To do this, just
138;; say `M-x reftex-customize'. This will not work with older versions
139;; of custom.el.
140;;
141;; Here is a complete list of the RefTeX configuration variables with
142;; their default settings. You could copy this list to your .emacs file
143;; and change whatever is necessary. Each variable has an extensive
144;; documentation string. Look it up for more information!
145;;
146;; ;; Configuration Variables and User Options for RefTeX ------------------
025bb635 147;; ;; Support for \label and \ref --------------------------------------
a7ec1775
RS
148;; (setq reftex-label-alist nil)
149;; (setq reftex-default-label-alist-entries '(Sideways LaTeX))
150;; (setq reftex-use-text-after-label-as-context nil)
151;; ;; Label insertion
152;; (setq reftex-insert-label-flags '("s" "sft"))
153;; (setq reftex-derive-label-parameters '(3 20 t 1 "-"
154;; ("the" "on" "in" "off" "a" "for" "by" "of" "and" "is")))
155;; (setq reftex-label-illegal-re "[\000-\040\177-\377\\\\#$%&~^_{}]")
156;; (setq reftex-abbrev-parameters '(4 2 "^saeiou" "aeiou"))
157;; ;; Label referencing
158;; (setq reftex-label-menu-flags '(t t nil nil nil nil))
159;; (setq reftex-guess-label-type t)
160;; ;; BibteX citation configuration ----------------------------------------
161;; (setq reftex-bibpath-environment-variables '("BIBINPUTS" "TEXBIB"))
162;; (setq reftex-bibfile-ignore-list nil)
163;; (setq reftex-sort-bibtex-matches 'reverse-year)
164;; (setq reftex-cite-format 'reftex-cite-format-default)
165;; ;; Table of contents configuration --------------------------------------
166;; (setq reftex-toc-follow-mode nil)
167;; ;; Miscellaneous configurations -----------------------------------------
168;; (setq reftex-extra-bindings nil)
169;; (setq reftex-use-fonts t)
170;; (setq reftex-keep-temporary-buffers t)
171;; (setq reftex-auto-show-entry t)
172;;
173;; CONFIGURATION EXAMPLES:
174;; =======================
175;;
176;; Suppose you are working with AMS-LaTeX amsmath package (with its math
177;; environments like `align', `multiline' etc.). Here is how you would
178;; configure RefTeX to recognize these environments:
179;;
180;; (setq reftex-label-alist '(AMSTeX))
181;;
182;; This is very easy since RefTeX has builtin support for AMS-LaTeX.
183;; Suppose, however, you are also
184;;
185;; - using "\newtheorem" in LaTeX in order to define two new environments
186;; "Theorem" and "Axiom" like this:
187;;
188;; \newtheorem{axiom}{Axiom}
189;; \newtheorem{theorem}{Theorem}
190;;
191;; - making your figures not directly with the figure environment, but with
192;; a macro like
193;;
194;; \newcommand{\myfig}[4][tbp]{
195;; \begin{figure}[#1]
196;; \epsimp[#4]{#2}
197;; \caption{#3}
198;; \end{figure}}
199;;
200;; which would be called like
201;;
202;; \myfig{filename}{\label{fig:13} caption text}{1}
203;;
204;; Here is how to tell RefTeX to also recognize Theorem and Axiom as
205;; labeled environments, and that any labels defined inside the \myfig
206;; macro are figure labels:
207;;
208;; (setq reftex-label-alist
209;; '(AMSTeX
210;; ("axiom" ?a "ax:" "~\\ref{%s}" nil ("Axiom" "Ax."))
211;; ("theorem" ?h "thr:" "~\\ref{%s}" t ("Theorem" "Theor." "Th."))
212;; ("\\myfig" ?f "fig:" nil t)))
213;;
214;; The type indicator characters ?a and ?h are used for prompts when
215;; RefTeX queries for a label type. Note that "h" was chosen for "theorem"
216;; since "t" is already taken by "table". Note that also "s", "f", "e", "n"
217;; are taken by the standard environments.
218;; The automatic labels for Axioms and Theorems will look like "ax:23" or
219;; "thr:24".
220;; The "\ref{%s}" is a format string indicating how to insert references to
221;; these labels. The nil format in the \myfig entry means to use the same
222;; format as other figure labels.
223;; The next item indicates how to grab context of the label definition.
224;; - t means to get it from a default location (from the beginning of a \macro
fba437e6 225;; or after the \begin statement). t is *not* a good choice for eqnarray
a7ec1775
RS
226;; and similar environments.
227;; - nil means to use the text right after the label definition.
228;; - For more complex ways of getting context, see the docstring of
229;; reftex-label-alist.
230;; The strings at the end of each entry are used to guess the correct label
231;; type from the word before point when creating a reference. E.g. if you
232;; write: "as we have shown in Theorem" and then press `C-)', RefTeX will
233;; know that you are looking for a Theorem label and restrict the labels in
234;; the menu to only these labels without even asking.
235;; See also the documentation string of the variable reftex-label-alist.
236;;
237;; Depending on how you would like the label insertion and selection for the
238;; new environments to work, you might want to add the letters "a" and "h"
239;; to some of the flags in the following variables:
240;;
241;; reftex-insert-label-flags
242;; reftex-label-menu-flags
243;;
244;; The individual flags in these variables can be set to t or nil to enable or
245;; disable the feature for all label types. They may also contain a string of
246;; label type letters in order to turn on the feature for those types only.
247;;
248;; -----
249;; If you are writing in a language different from english you might want to
250;; add magic words for that language. Here is a German example:
251;;
252;; (setq reftex-label-alist
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."))
257;; (nil ?n nil nil nil ("Punkt"))))
258;;
259;; Using `nil' as first item in each entry makes sure that this entry does
260;; not replace the original entry for that label type.
261;;
262;; HOOKS
263;; -----
264;; Loading reftex.el runs the hook reftex-load-hook. Turning on reftex-mode
265;; runs reftex-mode-hook.
266;;
267;;-------------------------------------------------------------------------
268;;
269;; KEY BINDINGS
270;;
271;; All important functions of RefTeX can be reached from its menu which
272;; is installed in the menu bar as "Ref" menu. Only the more frequently used
273;; functions have key bindings.
274;;
275;; Here is the default set of keybindings from RefTeX.
276;;
277;; C-c = reftex-toc
278;; C-c ( reftex-label
279;; C-c ) reftex-reference
280;; C-c [ reftex-citation
281;; C-c & reftex-view-crossref
282;;
283;; I've used these bindings in order to avoid interfering with AUCTeX's
284;; settings. Personally, I also bind some functions in the C-c LETTER
285;; map for easier access:
286;;
287;; C-c t reftex-toc
288;; C-c l reftex-label
289;; C-c r reftex-reference
290;; C-c c reftex-citation
291;; C-c v reftex-view-crossref
292;; C-c s reftex-search-document
293;; C-c g reftex-grep-document
294;;
295;; If you want to copy those as well, set in your .emacs file:
296;;
297;; (setq reftex-extra-bindings t)
298;;
299;; It is possible to bind the function for viewing cross references to a
300;; mouse event. Something like the following in .emacs will do the trick:
301;;
302;; (add-hook 'reftex-load-hook
303;; '(lambda ()
304;; (define-key reftex-mode-map [(alt mouse-1)]
305;; 'reftex-mouse-view-crossref)))
306;;
307;;-------------------------------------------------------------------------
308;;
309;; RELATED PACKAGES
310;;
311;; AUCTeX
312;; ------
313;; If you are writing any TeX or LaTeX documents with Emacs, you should
314;; have a look at AUCTeX, the definitive package to work with TeX and LaTeX.
315;; Information on AUCTeX can be found here:
316;;
317;; http://www.sunsite.auc.dk/auctex/
318;;
319;; AUCTeX version 9.7f and later can be configured to delegate label
320;; insertion to RefTeX. Do do that, say in your .emacs file
321;;
322;; (setq LaTeX-label-function 'reftex-label)
323;;
324;; RefTeX also provides functions which can replace TeX-arg-label and
325;; TeX-arg-cite in AUCTeX. These functions are compatible with the originals,
326;; but use RefTeX internals to create and select labels and citation keys.
327;; There are 3 functions: reftex-arg-label, reftex-arg-ref, reftex-arg-cite.
328;;
329;; AUCTeX can support RefTeX via style files. A style file may contain
330;; calls to reftex-add-to-label-alist which defines additions to
025bb635
RS
331;; reftex-label-alist. The argument taken by this function must have
332;; the same format as reftex-label-alist. E.g. the amsmath.el style file
333;; of AUCTeX contains the following:
334;;
335;; (TeX-add-style-hook "amsmath"
336;; (function
337;; (lambda ()
338;; (if (featurep 'reftex)
339;; (reftex-add-to-label-alist '(AMSTeX))))))
340;;
341;; while a package `myprop' defining a proposition environment with
342;; \newtheorem might use
343;;
344;; (TeX-add-style-hook "myprop"
345;; (function
346;; (lambda ()
347;; (if (featurep 'reftex)
348;; (reftex-add-to-label-alist
349;; '(("proposition" ?p "prop:" "~\\ref{%s}" t
350;; ("Proposition" "Prop."))))))))
a7ec1775
RS
351;;
352;; Bib-cite.el
353;; -----------
354;; Once you have written a document with labels, refs and citations, it can be
355;; nice to read such a file like a hypertext document. RefTeX has some support
356;; for that (reftex-view-crossref, reftex-search-document). A more elegant
357;; interface with mouse support and links into Hyperbole is provided (among
358;; other things) by Peter S. Galbraith's bib-cite.el. There is some overlap in
359;; the functionalities of bib-cite and RefTeX. Bib-cite.el comes bundled with
360;; AUCTeX. You can also get the latest version from
361;;
362;; ftp://ftp.phys.ocean.dal.ca/users/rhogee/elisp/bib-cite.el
363;;
364;;-------------------------------------------------------------------------
365;;
366;; PERFORMANCE ISSUES
367;;
368;; 1. RefTeX will load other parts of a multifile document as well as BibTeX
369;; database files for lookup purposes. These buffers are kept, so that
370;; subsequent lookup in the same files is fast. For large documents and
371;; large BibTeX databases, this can use up a lot of memory. If you have
372;; more time than memory, try the following option, which will remove
373;; buffers created for lookup after use.
374;;
375;; (setq reftex-keep-temporary-buffers nil)
376;;
377;; 2. Parsing the document for labels and their context can be slow.
378;; Therefore, RefTeX does it just once automatically. Further parsing
379;; happens only on user request
380;; - with a raw C-u prefix arg to any of the functions reftex-label,
381;; reftex-reference, reftex-citation, reftex-toc.
382;; - with the `r' key from the label selection menu or the *toc* buffer.
383;;
384;; *** If you use reftex-label to create labels, the list will be updated
385;; *** internally, so that no extra parsing is required.
386;;
387;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
388;;
389;; KNOWN BUGS
390;;
391;; o If you change reftex-label-alist in an editing session, you need to
392;; reset reftex with `M-x reftex-reset-mode' in order to make these
393;; changes effective. Changes introduced with the function
394;; reftex-add-to-label-alist as well as changes applied from the
395;; customization buffer automatically trigger a reset.
396;;
397;; o At times the short context shown by RefTeX may not be what you want.
398;; In particular, eqnarray environments can be difficult to
399;; parse. RefTeX's default behavior for eqnarrays is to scan backwards to
400;; either a double backslash or the beginning of the environment. If this
401;; gives unsatisfactory results, make it a habit to place the label
402;; *before* each equation
403;;
404;; \begin{eqnarray}
405;; \label{eq:1}
406;; E = \gamma m c^2 \\
407;; \label{eq:2}
408;; \gamma = \sqrt{1-v^2/c^2}
409;; \end{eqnarray}
410;;
411;; and turn off parsing for context in equation and eqnarray environments
412;; with
413;;
414;; (setq reftex-use-text-after-label-as-context "e").
415;;
416;; o RefTeX keeps only one global copy of the configuration variables.
417;; Also any additions from style files go into a global variable.
418;; Practically, this should not be a problem. Theoretically, it could
419;; give conflicts if two documents used environments with identical
420;; names, but different associated label types.
421;;
422;; o Input, include, bibliography and section statements have to be first
423;; on a line (except for white space) in order to be seen by reftex.
424;;
425;; o When the document is scanned, RefTeX creates a large buffer containing
426;; the entire document instead of scanning the individual files one by
427;; one. This is necessary since a file might not contain the context
428;; needed by RefTeX.
429;;
430;; o If you have two identical section headings in the same file,
431;; reftex-toc will only let you jump to the first one because it searches
432;; for the section heading from the beginning of the file. You can work
433;; around this by changing one of the section titles in a way LaTeX does
434;; not see, e.g. with extra white space. RefTeX will distinguish
435;; \section{Introduction} from \section{ Introduction}.
436;;
437;; o RefTeX sees also labels in regions commented out and will refuse to
438;; make duplicates of such a label. This is considered to be a feature.
439;;
440;; o When RefTeX tries to show a window full of context from inside a
441;; section hidden with outline-minor-mode, it will unhide that section.
442;; This change will not be reversed automatically.
443;;
444;;---------------------------------------------------------------------------
445;;
446;; TO DO
447;;
448;; I think I am pretty much done with this one...
449;;
450;;---------------------------------------------------------------------------
451;;
452;; AUTHOR
453;;
454;; Carsten Dominik <dominik@strw.LeidenUniv.nl>
455;;
456;; with contributions from Stephen Eglen
457;;
458;; The newest version of RefTeX can be found at
459;;
460;; http://www.strw.leidenuniv.nl/~dominik/Tools/
461;; ftp://strw.leidenuniv.nl/pub/dominik/
462;;
463;; THANKS TO:
464;; ---------
465;; At least the following people have invested time to test and bug-fix
466;; reftex.el. Some have send patches for fixes or new features.
467;;
468;; Stephen Eglen <stephene@cogs.susx.ac.uk>
469;; F.E.Burstall <F.E.Burstall@maths.bath.ac.uk>
470;; Karl Eichwalder <ke@ke.Central.DE>
471;; Laurent Mugnier <mugnier@onera.fr>
472;; Rory Molinari <molinari@yunt.math.lsa.umich.edu>
473;; Soren Dayton <csdayton@cs.uchicago.edu>
474;; Daniel Polani <polani@Informatik.Uni-Mainz.DE>
475;; Allan Strand <astrand@trillium.NMSU.Edu>
476;;
477;; The view crossref feature was inspired by the similar function in
478;; Peter S. Galbraith's bib-cite.el.
479;;
480;; Finally thanks to Uwe Bolick <bolick@physik.tu-berlin.de> who first
481;; got me (some years ago) into supporting LaTeX labels and references
482;; with an Editor (which was MicroEmacs at the time).
025bb635 483;;
a7ec1775
RS
484\f
485;;; Code:
486
487;; Stuff that needs to be there when we use defcustom
488;; --------------------------------------------------
489
490(require 'custom)
491
492(defvar reftex-tables-dirty t
493 "Flag showing if tables need to be re-computed.")
494
495(eval-and-compile
496 (defun reftex-set-dirty (symbol value)
497 (setq reftex-tables-dirty t)
498 (set symbol value)))
499
500;;; Begin of Configuration Section ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
501
502;; Configuration Variables and User Options for RefTeX ------------------
503
504(defgroup reftex nil
505 "LaTeX label and citation support."
506 :tag "RefTeX"
507 :link '(url-link :tag "Home Page" "http://strw.leidenuniv.nl/~dominik/Tools/")
508 :prefix "reftex-"
509 :group 'tex)
510
511(defun reftex-customize ()
512 "Call the customize function with reftex as argument."
513 (interactive)
514 (if (fboundp 'customize-group)
515 (customize-group 'reftex)
516 (customize 'reftex)))
517
fba437e6 518;; Support for \label and \ref --------------------------------------
a7ec1775
RS
519
520(defgroup reftex-label-support nil
521 "Support for creation, insertion and referencing of labels in LaTeX"
522 :group 'reftex)
523
524(defgroup reftex-defining-label-environments nil
525 "Definition of environments and macros to do with label"
526 :group 'reftex-label-support)
527
528
529(defcustom reftex-label-alist nil
fba437e6 530 "Alist with information on environments for \\label-\\ref use.
f52b232e 531See the definition of `reftex-label-alist-builtin' for examples. This variable
a7ec1775 532should define additions and changes to the default. The only things you MUST
f52b232e
RS
533NOT change is that `?s' is the type indicator for section labels and SPACE is
534for the 'any' label type. These are hard-coded at other places in the code.
a7ec1775 535
f52b232e 536Changes to this variable after RefTeX has been loaded only become
a7ec1775
RS
537effective when RefTeX is reset with \\[reftex-reset-mode].
538
539Each list entry is a list describing an environment or macro carrying a
f52b232e 540label. The elements of each list entry are:
a7ec1775
RS
541
5420. Name of the environment (like \"table\") or macro (like \"\\\\myfig\").
543 Special names: `section' for section labels, `any' to define a group
544 which contains all labels.
545 This may also be nil if this entry is only meant to change some settings
546 associated with the type indicator character (see below).
547
5481. Type indicator character, like ?t.
549 The type indicator is a single character used in prompts for
f52b232e 550 label types. It must be a printable character. The same character
a7ec1775
RS
551 may occur several times in this list, to cover cases in which different
552 environments carry the same label type (like equation and eqnarray).
553
5542. Label prefix string, like \"tab:\".
f52b232e 555 The prefix is a short string used as the start of a label. It may be the
a7ec1775
RS
556 empty string.
557
f52b232e
RS
5583. Format string for reference insert in buffer. Each `%s' will be
559 replaced by the label (you can use more than one several `%s', so
560 that you can set this to: \"\\ref{%s} on page~\\pageref{%s}\").
561 When the format starts with `~', whitespace before point will be
562 removed so that the reference cannot be separated from the word
563 before it.
a7ec1775
RS
564
5654. Indication on how to find the short context.
f52b232e
RS
566 - If nil, use the text following the \\label{...} macro.
567 - If t, use
a7ec1775
RS
568 - text following the \\begin{...} statement of environments
569 (not a good choice in in eqnarray or enumerate environments!)
570 - the section heading for section labels.
571 - the begin of the macro for macros.
f52b232e
RS
572 - If a string, use as regexp to search *backward* from the label. Context
573 is then the text following the end of the match. E.g. putting this to
a7ec1775 574 \"\\\\\\\\caption{\" will use the beginning of the caption in a figure
f52b232e 575 or table environment. \"\\\\\\\\begin{eqnarray}\\\\|\\\\\\\\\\\\\\\\\"
a7ec1775
RS
576 works for eqnarrays.
577 - If a function, call this function with the name of the environment/macro
f52b232e
RS
578 as argument. On call, point will be just after the \\label macro. The
579 function is expected to return a suitable context string. It should
a7ec1775
RS
580 throw an exception (error) when failing to find context.
581 Consider the following example, which would return the 10 characters
582 following the label as context:
583
584 (defun my-context-function (env-or-mac)
585 (if (> (point-max) (+ 10 (point)))
586 (buffer-substring (point) (+ 10 (point)))
587 (error \"Buffer too small\")))
588
f52b232e
RS
589 Setting the variable `reftex-use-text-after-label-as-context' to t
590 overrides the setting here.
a7ec1775 591
f52b232e 5925. List of magic words which identify a reference to be of this type. If the
a7ec1775 593 word before point is equal to one of these words when calling
f52b232e 594 `reftex-reference', the label list offered will be automatically restricted
a7ec1775
RS
595 to labels of the correct type.
596
597If the type indicator characters of two or more entries are the same, RefTeX
598will use
599 - the first non-nil format and prefix
600 - the magic words of all involved entries.
601
f52b232e 602Any list entry may also be a symbol. If that has an association in
a7ec1775 603reftex-label-alist-builtin, the cdr of that association is spliced into the
f52b232e
RS
604list. See the AMSTeX configuration example in the comment section of
605`reftex.el'."
a7ec1775
RS
606 :group 'reftex-defining-label-environments
607 :set 'reftex-set-dirty
608 :type '(list
609 :convert-widget
610 (lambda (widget)
611 (let*
612 ((args
613 (list
614 `(repeat
615 :inline t
616 (radio
617 :value ("" ?a nil nil t nil)
618 (choice
619 :tag "Builtin"
620 :value AMSTeX
621 ,@(mapcar (function (lambda (x)
622 (list 'const ':tag (nth 1 x) (car x))))
623 reftex-label-alist-builtin))
624 (list :tag "Detailed custom entry"
625 (choice :tag "Environment or \\macro "
626 (const :tag "Ignore, just use typekey" nil)
627 (string ""))
628 (character :tag "Typekey character " ?a)
629 (choice :tag "Label prefix string "
630 (const :tag "Copy from similar label type" nil)
631 (string :tag "Specify here" "lab:"))
632 (choice :tag "Label reference format"
633 (const :tag "Copy from similar label type" nil)
634 (string :tag "Specify here" "~\\ref{%s}"))
635 (choice :tag "Grab context method "
636 (const :tag "Default position" t)
637 (const :tag "After label" nil)
638 (regexp :tag "Regular expression" "")
639 (symbol :tag "Function" my-context-function))
640 (repeat :tag "List of Magic Words" (string))))))))
641 (widget-put widget :args args)
642 widget))))
643
644(defcustom reftex-default-label-alist-entries '(Sideways LaTeX)
645 "Default label alist specifications. LaTeX should be the last entry.
646This list describes the default label environments RefTeX should always use in
647addition to the specifications in reftex-label-alist. It is probably a
648mistake to remove the LaTeX symbol from this list.
649
025bb635 650The options include:
a7ec1775
RS
651LaTeX The standard LaTeX environments
652Sideways The sidewaysfigure and sidewaystable environments
653AMSTeX The math environments in the AMS_LaTeX amsmath package
025bb635
RS
654AAS The deluxetable environment from the American Astronomical Society
655
656For the full list of options, see the constant reftex-label-alist-builtin.
657Better still, try
658
659M-x customize-variable RET reftex-default-label-alist-entries RET."
a7ec1775
RS
660 :group 'reftex-defining-label-environments
661 :set 'reftex-set-dirty
662 :type '(list :indent 4
663 :convert-widget
664 (lambda (widget)
665 (let* ((args
666 (list
667 `(checklist
668 :inline t
669 ,@(reverse
670 (mapcar (lambda (x)
671 (list 'const ':tag (nth 1 x) (car x)))
672 reftex-label-alist-builtin))))))
673 (widget-put widget :args args)
674 widget))))
675
676(defcustom reftex-use-text-after-label-as-context nil
677 "*t means, grab context from directly after the \\label{..} macro.
678This is the fastest method for obtaining context of the label definition, but
679requires discipline when placing labels. Setting this variable to t takes
680precedence over the individual settings in reftex-label-alist.
681This variable may be set to t, nil, or a string of label type letters
682indicating the label types for which it should be true."
683 :group 'reftex-defining-label-environments
684 :set 'reftex-set-dirty
685 :type '(choice
686 (const :tag "on" t) (const :tag "off" nil)
687 (string :tag "Selected label types")))
688
689;; Label insertion
690
691(defgroup reftex-making-and-inserting-labels nil
692 "Options on how to create new labels"
693 :group 'reftex-label-support)
694
695(defcustom reftex-insert-label-flags '("s" "sft")
696 "Flags governing label insertion. First flag DERIVE, second flag PROMPT.
697
698If DERIVE is t, RefTeX will try to derive a sensible label from context.
699A section label for example will be derived from the section heading.
700The conversion of the context to a legal label is governed by the
701specifications given in reftex-derive-label-parameters.
702If RefTeX fails to derive a label, it will prompt the user.
703
704If PROMPT is t, the user will be prompted for a label string. The prompt will
705already contain the prefix, and (if DERIVE is t) a default label derived from
706context. When PROMPT is nil, the default label will be inserted without
707query.
708
709So the combination of DERIVE and PROMPT controls label insertion. Here is a
710table describing all four possibilities:
711
712DERIVE PROMPT ACTION
713-------------------------------------------------------------------------
714 nil nil Insert simple label, like eq:22 or sec:13. No query.
715 nil t Prompt for label
716 t nil Derive a label from context and insert without query
717 t t Derive a label from context and prompt for confirmation
718
719Each flag may be set to t, nil, or a string of label type letters
720indicating the label types for which it should be true.
721Thus, the combination may be set differently for each label type. The
722default settings \"s\" and \"sft\" mean: Derive section labels from headings
723(with confirmation). Prompt for figure and table labels. Use simple labels
724without confirmation for everything else."
725 :group 'reftex-making-and-inserting-labels
726 :type '(list (choice :tag "Derive label from context"
727 (const :tag "always" t)
728 (const :tag "never" nil)
729 (string :tag "for selected label types" ""))
730 (choice :tag "Prompt for label string "
731 :entry-format " %b %v"
732 (const :tag "always" t)
733 (const :tag "never" nil)
734 (string :tag "for selected label types" ""))))
735
736(defcustom reftex-derive-label-parameters '(3 20 t 1 "-" ; continue
737 ("the" "on" "in" "off" "a" "for" "by" "of" "and" "is"))
738 "Parameters for converting a string into a label.
739NWORDS Number of words to use.
740MAXCHAR Maximum number of characters in a label string.
741ILLEGAL nil: Throw away any words containing characters illegal in labels.
742 t: Throw away only the illegal characters, not the whole word.
743ABBREV nil: Never abbreviate words.
744 t: Always abbreviate words (see reftex-abbrev-parameters).
745 not t and not nil: Abbreviate words if necessary to shorten
746 label string below MAXCHAR.
747SEPARATOR String separating different words in the label
748IGNOREWORDS List of words which should not be part of labels"
749 :group 'reftex-making-and-inserting-labels
750 :type '(list (integer :tag "Number of words " 3)
751 (integer :tag "Maximum label length " 20)
752 (choice :tag "Illegal characters in words"
753 (const :tag "throw away entire word" nil)
754 (const :tag "throw away single chars" t))
755 (choice :tag "Abbreviate words "
756 (const :tag "never" nil)
757 (const :tag "always" t)
758 (const :tag "when label is too long" 1))
759 (string :tag "Separator between words " "-")
760 (repeat :tag "Ignore words"
761 :entry-format " %i %d %v"
762 (string :tag ""))))
763
764(defcustom reftex-label-illegal-re "[\000-\040\177-\377\\\\#$%&~^_{}]"
765 "Regexp matching characters not legal in labels.
766For historic reasons, this character class comes *with* the [] brackets."
767 :group 'reftex-making-and-inserting-labels
768 :type '(regexp :tag "Character class"))
769
770(defcustom reftex-abbrev-parameters '(4 2 "^saeiou" "aeiou")
771 "Parameters for abbreviation of words.
772MIN-CHARS minimum number of characters remaining after abbreviation
773MIN-KILL minimum number of characters to remove when abbreviating words
774BEFORE character class before abbrev point in word
775AFTER character class after abbrev point in word"
776 :group 'reftex-making-and-inserting-labels
777 :type '(list
778 (integer :tag "Minimum chars per word" 4)
779 (integer :tag "Shorten by at least " 2)
780 (string :tag "cut before char class " "^saeiou")
781 (string :tag "cut after char class " "aeiou")))
782
783
784;; Label referencing
785
786(defgroup reftex-referencing-labels nil
787 "Options on how to reference labels"
788 :group 'reftex-label-support)
789
790(defcustom reftex-label-menu-flags '(t t nil nil nil nil)
791 "*List of flags governing the label menu makeup.
792The flags are:
793
794TABLE-OF-CONTENTS Show the labels embedded in a table of context.
795SECTION-NUMBERS Include section numbers (like 4.1.3) in table of contents.
796COUNTERS Show counters. This just numbers the labels in the menu.
797NO-CONTEXT Non-nil means do NOT show the short context.
798FOLLOW follow full context in other window.
799SHOW-COMMENTED Show labels from regions which are commented out. RefTeX
800 sees these labels, but does not normally show them.
801
802Each of these flags can be set to t or nil, or to a string of type letters
803indicating the label types for which it should be true. These strings work
804like character classes in regular expressions. Thus, setting one of the
805flags to \"sf\" makes the flag true for section and figure labels, nil
806for everything else. Setting it to \"^ft\" makes it the other way round.
807
808Most options can also be switched from the label menu itself - so if you
809decide here to not have a table of contents in the label menu, you can still
810get one interactively during selection from the label menu."
811 :group 'reftex-referencing-labels
812 :type '(list
813 (choice :tag "Embed in table of contents "
814 (const :tag "on" t) (const :tag "off" nil)
815 (string :tag "Selected label types"))
816 (choice :tag "Show section numbers "
817 (const :tag "on" t) (const :tag "off" nil))
818 (choice :tag "Show individual counters "
819 (const :tag "on" t) (const :tag "off" nil)
820 (string :tag "Selected label types"))
821 (choice :tag "Hide short context "
822 (const :tag "on" t) (const :tag "off" nil)
823 (string :tag "Selected label types"))
824 (choice :tag "Follow context in other window"
825 (const :tag "on" t) (const :tag "off" nil)
826 (string :tag "Selected label types"))
827 (choice :tag "Show commented labels "
828 (const :tag "on" t) (const :tag "off" nil)
829 (string :tag "Selected label types"))))
830
831
832(defcustom reftex-guess-label-type t
833 "*Non-nil means, reftex-reference will try to guess the label type.
834To do that, RefTeX will look at the word before the cursor and compare it with
835the words given in reftex-label-alist. When it finds a match, RefTeX will
836immediately offer the correct label menu - otherwise it will prompt you for
837a label type. If you set this variable to nil, RefTeX will always prompt."
838 :group 'reftex-referencing-labels
839 :type '(boolean))
840
841;; BibteX citation configuration ----------------------------------------
842
843(defgroup reftex-citation-support nil
844 "Support for referencing bibliographic data with BibTeX"
845 :group 'reftex)
846
847(defcustom reftex-bibpath-environment-variables '("BIBINPUTS" "TEXBIB")
848 "*List of env vars which might contain the path to BibTeX database files."
849 :group 'reftex-citation-support
850 :set 'reftex-set-dirty
851 :type '(repeat (string :tag "Environment variable")))
852
853(defcustom reftex-bibfile-ignore-list nil
854 "List of files in \\bibliography{..} RefTeX should *not* parse.
855The file names have to be in the exact same form as in the bibliography
856macro - i.e. without the .bib extension.
857Intended for files which contain only `@string' macro definitions and the
858like, which are ignored by RefTeX anyway."
859 :group 'reftex-citation-support
860 :set 'reftex-set-dirty
861 :type '(repeat (string :tag "File name")))
862
863(defcustom reftex-sort-bibtex-matches 'reverse-year
864 "*Sorting of the entries found in BibTeX databases by reftex-citation.
865Possible values:
866nil Do not sort entries.
867'author Sort entries by author name.
868'year Sort entries by increasing year.
869'reverse-year Sort entries by decreasing year."
870 :group 'reftex-citation-support
871 :type '(choice (const :tag "not" nil)
872 (const :tag "by author" author)
873 (const :tag "by year" year)
874 (const :tag "by year, reversed" reverse-year)))
875
876(defcustom reftex-cite-format 'reftex-cite-format-default
877 "Defines the format of citations to be inserted into the buffer.
878It can be a string, a list of strings, or an alist with characters as keys
879and a list of strings in the car. In the simplest case, this can just
880be the string \"\\cite{KEY}\", which is also the default. See the
881definition of the reftex-cite-format-XXXX constants for more complex
882examples.
883 If reftex-cite-format is a string, it will be used as the format. In
884the format, AUTHOR will be replaced by the last name of the
885author, YEAR will be replaced by the year and KEY by the citation
886key. If AUTHOR is present several times, it will be replaced with
887successive author names.
888See the constant reftex-cite-format-default for an example.
889 If reftex-cite-format is a list of strings, the string used will
890depend upon the number of authors of the article. No authors means,
891the first string will be used, 1 author means, the second string will
892be used etc. The last string in the list will be used for all articles
893with too many authors. See reftex-cite-format-1-author-simple for an
894example.
895 If reftex-cite-format is a list of cons cells, the car of each cell
896needs to be a character. When a selected reference is accepted by
897pressing that key, the cdr of the associated list will be used as
898described above. See reftex-cite-format-2-authors for an example.
899 In order to configure this variable, you can either set
900reftex-cite-format directly yourself or set it to the SYMBOL of one of
901the predefined constants. E.g.:
902(setq reftex-cite-format 'reftex-cite-format-2-authors)"
903 :group 'reftex-citation-support
904 :type
905'(choice
906 (choice :tag "symbolic defaults"
907 :value reftex-cite-format-default
908 (const reftex-cite-format-default)
909 (const reftex-cite-format-1-author-simple)
910 (const reftex-cite-format-2-authors))
911 (string :tag "format string" "\\cite{KEY}")
912 (repeat :tag "list of strings"
913 :value ("\cite{KEY}" "AUTHOR \cite{KEY}" "AUTHOR and AUTHOR \cite{KEY}")
914 (string :tag "format string" ""))
915 (repeat :tag "key-ed lists of strings"
916 :value ((?\r . ("\cite{KEY}" "AUTHOR \cite{KEY}" "AUTHOR and AUTHOR \cite{KEY}")))
917 (cons :tag "Enter a keyed list of format strings"
918 (character :tag "Key character " ?\r)
919 (repeat
920 (string :tag "format string" ""))))))
921
922;; Table of contents configuration --------------------------------------
923
924(defgroup reftex-table-of-contents-browser nil
925 "A multifile table of contents browser."
926 :group 'reftex)
927
928(defcustom reftex-toc-follow-mode nil
929 "Non-nil means, point in *toc* buffer will cause other window to follow.
930The other window will show the corresponding part of the document.
931This flag can be toggled from within the *toc* buffer with the `f' key."
932 :group 'reftex-table-of-contents-browser
933 :type '(boolean))
934
935;; Miscellaneous configurations -----------------------------------------
936
937(defgroup reftex-miscellaneous-configurations nil
938 "Collection of further configurations"
939 :group 'reftex)
940
941(defcustom reftex-extra-bindings nil
942 "Non-nil means, make additional key bindings on startup.
943These extra bindings are located in the users C-c letter map."
944 :group 'reftex-miscellaneous-configurations
945 :type '(boolean))
946
947(defcustom reftex-use-fonts t
948 "*Non-nil means, use fonts in label menu and on-the-fly help.
949Font-lock must be loaded as well to actually get fontified display."
950 :group 'reftex-miscellaneous-configurations
951 :type '(boolean))
952
953(defcustom reftex-keep-temporary-buffers t
954 "*Non-nil means, keep any TeX and BibTeX files loaded for lookup.
955Nil means, kill it immediately after use unless it was already an existing
956buffer before the lookup happened. It is faster to keep the buffers, but can
957use a lot of memory, depending on the size of your database and document."
958 :group 'reftex-miscellaneous-configurations
959 :type '(boolean))
960
961(defcustom reftex-auto-show-entry t
962 "*Non-nil means, showing context in another window may unhide a section.
963This is important when using outline-minor-mode. If the context to be shown
964is in a hidden section, RefTeX will issue a \"show-entry\" command in order
965to show it. This is not reversed when the label is selected - so the section
966remains shown after command completion."
967 :group 'reftex-miscellaneous-configurations
968 :type '(boolean))
969
970
971;;; End of Configuration Section ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
972
973;;;===========================================================================
974;;;
975;;; Define the formal stuff for a minor mode named RefTeX.
976;;;
977
978(defvar reftex-mode nil
979 "Determines if RefTeX minor mode is active.")
980(make-variable-buffer-local 'reftex-mode)
981
982(defvar reftex-mode-map (make-sparse-keymap)
983 "Keymap for RefTeX minor mode.")
984
985(defvar reftex-mode-menu nil)
986
987;;;###autoload
988(defun turn-on-reftex ()
989 "Turn on RefTeX minor mode."
990 (reftex-mode t))
991
992;;;###autoload
993(defun reftex-mode (&optional arg)
fba437e6 994 "Minor mode with distinct support for \\label, \\ref and \\cite in LaTeX.
a7ec1775
RS
995
996Labels can be created with `\\[reftex-label]' and referenced with `\\[reftex-reference]'.
997When referencing, you get a menu with all labels of a given type and
998context of the label definition. The selected label is inserted as a
fba437e6 999\\ref macro.
a7ec1775
RS
1000
1001Citations can be made with `\\[reftex-citation]' which will use a regular expression
1002to pull out a *formatted* list of articles from your BibTeX
fba437e6 1003database. The selected citation is inserted as a \\cite macro.
a7ec1775
RS
1004
1005A Table of Contents of the entire (multifile) document with browsing
1006capabilities is available with `\\[reftex-toc]'.
1007
1008Most command have help available on the fly. This help is accessed by
1009pressing `?' to any prompt mentioning this feature.
1010
1011\\{reftex-mode-map}
1012Under X, these functions will be available also in a menu on the menu bar.
1013
1014------------------------------------------------------------------------------"
1015
1016 (interactive "P")
1017 (setq reftex-mode (not (or (and (null arg) reftex-mode)
1018 (<= (prefix-numeric-value arg) 0))))
1019
1020 ; Add or remove the menu, and run the hook
1021 (if reftex-mode
1022 (progn
1023 (easy-menu-add reftex-mode-menu)
1024 (run-hooks 'reftex-mode-hook))
1025 (easy-menu-remove reftex-mode-menu)))
1026
1027(or (assoc 'reftex-mode minor-mode-alist)
1028 (setq minor-mode-alist
1029 (cons '(reftex-mode " Ref") minor-mode-alist)))
1030
1031(or (assoc 'reftex-mode minor-mode-map-alist)
1032 (setq minor-mode-map-alist
1033 (cons (cons 'reftex-mode reftex-mode-map)
1034 minor-mode-map-alist)))
1035
1036
1037;;; ===========================================================================
1038;;;
1039;;; Interfaces for other packages
1040;;; -----------------------------
1041;;;
1042;;; AUCTeX
1043;;; ------
1044
1045(defun reftex-arg-label (optional &optional prompt definition)
1046 "Use reftex-label to create a label and insert it with TeX-argument-insert.
1047This function is intended for AUCTeX macro support."
1048 (let ((label (reftex-label nil t)))
1049 (if (and definition (not (string-equal "" label)))
1050 (LaTeX-add-labels label))
1051 (TeX-argument-insert label optional optional)))
1052
1053(defun reftex-arg-ref (optional &optional prompt definition)
1054 "Use reftex-reference to select a label, insert it with TeX-argument-insert.
1055This function is intended for AUCTeX macro support."
1056 (let ((label (reftex-reference nil t)))
1057 (if (and definition (not (string-equal "" label)))
1058 (LaTeX-add-labels label))
1059 (TeX-argument-insert label optional optional)))
1060
1061(defun reftex-arg-cite (optional &optional prompt definition)
1062 "Use reftex-citation to select a key, insert it with TeX-argument-insert.
1063This function is intended for AUCTeX macro support."
1064 (let ((key (reftex-citation nil t)))
1065 (TeX-argument-insert (or key "") optional optional)))
1066
1067(defvar reftex-label-alist-external-add-ons nil
1068 "List of label alist entries added with reftex-add-to-label-alist.")
1069
1070;;;###autoload
1071(defun reftex-add-to-label-alist (entry-list)
1072 "Add label environment descriptions to reftex-label-alist-external-add-ons.
1073The format of ENTRY-LIST is exactly like reftex-label-alist. See there
1074for details.
1075This function makes it possible to support RefTeX from AUCTeX style files.
1076The entries in ENTRY-LIST will be processed after the user settings in
1077reftex-label-alist, and before the defaults (specified in
1078reftex-default-label-alist-entries). Any changes made to
1079reftex-label-alist-external-add-ons will raise a flag to the effect that a
1080mode reset is done on the next occasion."
1081 (let (entry)
1082 (while entry-list
1083 (setq entry (car entry-list)
1084 entry-list (cdr entry-list))
1085 (if (not (member entry reftex-label-alist-external-add-ons))
1086 (setq reftex-tables-dirty t
1087 reftex-label-alist-external-add-ons
1088 (cons entry reftex-label-alist-external-add-ons))))))
1089
1090;;; ===========================================================================
1091;;;
1092;;; Multifile support
1093;;;
1094;;; Technical notes: Multifile works as follows: We keep just one list
1095;;; of labels for each master file - this can save a lot of memory.
1096;;; reftex-master-index-list is an alist which connects the true file name
1097;;; of each master file with the symbols holding the information on that
1098;;; document. Each buffer has local variables which point to these symbols.
1099
a7ec1775
RS
1100;; List of variables which handle the multifile stuff.
1101;; This list is used to tie, untie, and reset these symbols.
1102(defconst reftex-multifile-symbols
1103 '(reftex-label-numbers-symbol reftex-list-of-labels-symbol
1104 reftex-bibfile-list-symbol))
1105
f52b232e 1106;; Silence warnings about TeX-master, which is defined in AUCTeX.
025bb635
RS
1107(defvar TeX-master)
1108
f52b232e
RS
1109;; Silence additional warnings.
1110(defvar tex-main-file)
1111(defvar outline-minor-mode)
1112
1113;; Alist connecting master file names with the corresponding Lisp symbols.
a7ec1775
RS
1114(defvar reftex-master-index-list nil)
1115
1116;; Last index used for a master file
1117(defvar reftex-multifile-index 0)
1118
1119;; Alist connecting a master file with all included files.
1120;; This information is not yet used, just collected.
1121(defvar reftex-master-include-list nil)
1122
1123;; Variable holding the symbol with current value of label postfix
1124(defvar reftex-label-numbers-symbol nil )
1125(make-variable-buffer-local 'reftex-label-numbers-symbol)
1126
1127;; Variable holding the symbol with the label list of the document.
1128;; Each element of the label list is again a list with the following elements:
1129;; 0: One character label type indicator
1130;; 1: Short context to put into label menu
1131;; 2: The label
1132;; 3: The name of the file where the label is defined
1133(defvar reftex-list-of-labels-symbol nil)
1134(make-variable-buffer-local 'reftex-list-of-labels-symbol)
1135
1136;; Variable holding the symbol with a list of library files for this document
1137(defvar reftex-bibfile-list-symbol nil)
1138(make-variable-buffer-local 'reftex-bibfile-list-symbol)
1139
1140(defun reftex-next-multifile-index ()
1141 ;; Return the next free index for multifile symbols.
1142 (setq reftex-multifile-index (1+ reftex-multifile-index)))
1143
1144(defun reftex-tie-multifile-symbols ()
1145 ;; Tie the buffer-local symbols to globals connected with the master file.
1146 ;; If the symbols for the current master file do not exist, they are created.
1147
1148 (let* ((master (file-truename (reftex-TeX-master-file)))
1149 (index (assoc master reftex-master-index-list))
1150 (symlist reftex-multifile-symbols)
1151 (symbol nil)
1152 (symname nil)
1153 (newflag nil))
1154 ;; find the correct index
1155 (if index
1156 ;; symbols do exist
1157 (setq index (cdr index))
1158 ;; get a new index and add info to the alist
1159 (setq index (reftex-next-multifile-index)
1160 reftex-master-index-list (cons
1161 (cons master index)
1162 reftex-master-index-list)
1163 newflag t))
1164
1165 ;; get/create symbols and tie them
1166 (while symlist
1167 (setq symbol (car symlist)
1168 symlist (cdr symlist)
1169 symname (symbol-name symbol))
1170 (set symbol (intern (concat symname "-" (int-to-string index))))
1171 ;; initialize if new symbols
1172 (if newflag (set (symbol-value symbol) nil)))
1173
1174 ;; Return t if the symbols did already exist, nil when we've made them
1175 (not newflag)))
1176
1177(defun reftex-untie-multifile-symbols ()
1178 ;; Remove ties from multifile symbols, so that next use makes new ones.
1179 (let ((symlist reftex-multifile-symbols)
1180 (symbol nil))
1181 (while symlist
1182 (setq symbol (car symlist)
1183 symlist (cdr symlist))
1184 (set symbol nil))))
1185
1186(defun reftex-TeX-master-file ()
1187 ;; Return the name of the master file associated with the current buffer.
1188 ;; When AUCTeX is loaded, we will use it's more sophisticated method.
025bb635
RS
1189 ;; We also support the default TeX and LaTeX modes by checking for a
1190 ;; variable tex-main-file.
a7ec1775
RS
1191
1192 (let
1193 ((master
1194 (cond
1195 ((fboundp 'TeX-master-file) ; AUCTeX is loaded. Use its mechanism.
1196 (TeX-master-file t))
1197 ((boundp 'TeX-master) ; The variable is defined - lets use it.
1198 (cond
1199 ((eq TeX-master t)
1200 (buffer-file-name))
1201 ((eq TeX-master 'shared)
1202 (setq TeX-master (read-file-name "Master file: "
1203 nil nil t nil)))
1204 (TeX-master)
1205 (t
1206 (setq TeX-master (read-file-name "Master file: "
1207 nil nil t nil)))))
1208 ((boundp 'tex-main-file)
1209 ;; This is the variable from the default TeX modes
1210 (cond
1211 ((stringp tex-main-file)
1212 ;; ok, this must be it
1213 tex-main-file)
1214 (t
1215 ;; In this case, the buffer is its own master
1216 (buffer-file-name))))
1217 (t
1218 ;; Know nothing about master file. Assume this is a master file.
1219 (buffer-file-name)))))
1220 (cond
1221 ((null master)
1222 (error "Need a filename for this buffer. Please save it first."))
1223 ((or (file-exists-p master)
1224 (reftex-get-buffer-visiting master))
1225 ;; We either see the file, or have a buffer on it. OK.
1226 )
1227 ((or (file-exists-p (concat master ".tex"))
1228 (reftex-get-buffer-visiting (concat master ".tex")))
1229 ;; Ahh, an extra .tex was missing...
1230 (setq master (concat master ".tex")))
1231 (t
1232 ;; Something is wrong here. Throw an exception.
1233 (error "No such master file %s" master)))
1234 (expand-file-name master)))
1235
1236(defun reftex-make-master-buffer (master-file mode)
1237 "Make a master buffer which contains the MASTER-FILE and all includes.
1238This is to prepare a buffer containing the entire document in correct
1239sequence for parsing. Therefore it will even expand includes which are
1240commented out.
1241The function returns the number of input/include files not found."
1242
1243 (interactive "fmaster file: ")
1244 (let ((not-found 0) file file-list tmp (font-lock-maximum-size 1))
1245 (switch-to-buffer "*reftex-master.tex*")
1246 (erase-buffer)
1247 (if (not (eq major-mode mode))
1248 (funcall mode))
1249 ;; first insert the master file
1250 (if (not (file-exists-p master-file))
1251 (error "No such master file: %s" master-file))
1252 (reftex-insert-buffer-or-file master-file)
1253 (subst-char-in-region (point-min) (point-max) ?\r ?\n t)
1254 (setq file-list (cons master-file file-list))
1255 (goto-char 1)
1256 ;; remember from which file these lines came
1257 (put-text-property (point-min) (point-max) 'file
1258 (expand-file-name master-file))
1259 ;; Now find recursively all include/input statements and expand them
1260 (while (re-search-forward
1261 "^[ \t]*\\\\\\(include\\|input\\){\\([^}\n]+\\)}" nil t)
1262 ;; Change default directory, so that relative fine names work correctly
1263 (setq file (reftex-no-props (match-string 2)))
1264 (save-match-data
1265 (cd (file-name-directory
1266 (get-text-property (match-beginning 0) 'file)))
1267 (if (not (string-match "\\.tex$" file))
1268 (setq file (concat file ".tex"))))
1269 (if (file-exists-p file)
1270 (progn
1271 (replace-match
1272 (format "\n%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% START OF %s FILE: %s\n%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% END OF %s FILE: %s\n"
1273 (match-string 1) file
1274 (match-string 1) file))
1275 (beginning-of-line 0)
1276 (narrow-to-region (point) (point))
1277 ;; insert the file
1278 (reftex-insert-buffer-or-file file)
1279 (subst-char-in-region (point-min) (point-max) ?\r ?\n t)
1280 (setq file-list (cons (expand-file-name file) file-list))
1281 ;; remember from which file these lines came
1282 (put-text-property (point-min) (point-max)
1283 'file (expand-file-name file))
1284 (goto-char (point-min))
1285 (widen))
1286 (message "Input/include file %s not found. Ignored. Continuing..."
1287 file)
1288 (setq not-found (1+ not-found))))
1289 (setq file-list (nreverse file-list))
1290 (while (setq tmp (assoc (car file-list) reftex-master-include-list))
1291 (setq reftex-master-include-list (delq tmp reftex-master-include-list)))
1292 (setq reftex-master-include-list (cons file-list reftex-master-include-list))
1293 not-found))
1294
1295(defun reftex-insert-buffer-or-file (file)
1296 "If there is a buffer associated with FILE, insert it - otherwise the FILE."
1297 (let ((buffer (reftex-get-buffer-visiting file)))
1298 (if buffer
1299 (let (beg end beg1 end1)
1300 (save-excursion
1301 ;; make sure we get the whole buffer
1302 (set-buffer buffer)
1303 (setq beg (point-min) end (point-max))
1304 (widen)
1305 (setq beg1 (point-min) end1 (point-max)))
1306 (insert-buffer-substring buffer beg1 end1)
1307 (save-excursion
1308 (set-buffer buffer)
1309 (narrow-to-region beg end)))
1310 (insert-file-contents file))))
1311
1312
1313(defun reftex-parse-document (&optional buffer)
1314 "Rescan the document."
1315 (interactive)
1316 (save-window-excursion
1317 (save-excursion
1318 (if buffer
1319 (if (not (bufferp buffer))
1320 (error "No such buffer %s" (buffer-name buffer))
1321 (set-buffer buffer)))
1322 (reftex-access-scan-info t))))
1323
1324(defun reftex-access-scan-info (&optional rescan)
1325 ;; Access the scanning info. When the multifile symbols are not yet tied,
1326 ;; tie them. When they are have to be created, do a buffer scan to
1327 ;; fill them.
1328
1329 ;; If RESCAN is non-nil, enforce document scanning
1330
1331 (catch 'exit
1332 (let ((rescan (or (equal rescan t) (equal rescan '(4)))))
1333
1334 ;; Reset the mode if we had changes from style hooks
1335 (and reftex-tables-dirty
1336 (reftex-reset-mode))
1337
1338 (if (eq reftex-list-of-labels-symbol nil)
1339 ;; Symbols are not yet tied: Tie them and see if they are set
1340 (reftex-tie-multifile-symbols))
1341
1342 (if (and (symbol-value reftex-list-of-labels-symbol)
1343 (not rescan))
1344 ;; Lists do already exist and we don't need to rescan.
1345 ;; Return from here.
1346 (throw 'exit t))
1347
1348 ;; We need to rescan
1349 ;; =================
1350
1351 (unwind-protect
1352 (save-window-excursion
1353 (save-excursion
1354
1355 ;; do the scanning
1356
1357 (let ((label-list-symbol reftex-list-of-labels-symbol)
1358 (label-numbers-symbol reftex-label-numbers-symbol)
1359 (bibfile-list-symbol reftex-bibfile-list-symbol))
1360
1361 (message "Creating master buffer...")
1362 (reftex-make-master-buffer (reftex-TeX-master-file) major-mode)
1363
1364 (message "Scanning document...")
1365
1366 (reftex-scan-buffer-for-labels
1367 label-numbers-symbol label-list-symbol)
1368
1369 (reftex-scan-buffer-for-bibliography-statement
1370 bibfile-list-symbol)
1371
1372 (message "Scanning document... done"))))
1373
1374 (if (get-buffer "*reftex-master.tex*")
1375 (kill-buffer "*reftex-master.tex*"))))))
1376
1377(defun reftex-create-tags-file ()
1378 "Create TAGS file by running `etags' on the current document.
1379The TAGS file is also immediately visited with `visit-tags-table."
1380 (interactive)
1381 (reftex-access-scan-info current-prefix-arg)
1382 (let* ((master (reftex-TeX-master-file))
1383 (files (assoc master reftex-master-include-list))
1384 (cmd (format "etags %s" (mapconcat 'identity files " "))))
1385 (save-excursion
1386 (set-buffer (reftex-get-buffer-visiting master))
1387 (message "Running etags to create TAGS file...")
1388 (shell-command cmd)
1389 (visit-tags-table "TAGS"))))
1390
1391;; History of grep commands.
1392(defvar reftex-grep-history nil)
1393(defvar reftex-grep-command "grep -n "
1394 "Last grep command used in \\[reftex-grep-document]; default for next grep.")
1395
1396(defun reftex-grep-document (grep-cmd)
1397 "Run grep query through all files related to this document.
1398With prefix arg, force to rescan document.
1399This works also without an active TAGS table."
1400
1401 (interactive
1402 (list (read-from-minibuffer "Run grep on document (like this): "
1403 reftex-grep-command nil nil
1404 'reftex-grep-history)))
1405 (reftex-access-scan-info current-prefix-arg)
1406 (let* ((master (reftex-TeX-master-file))
1407 (default-directory (file-name-directory master))
1408 (re (format "\\`%s\\(.*\\)" (regexp-quote
1409 (expand-file-name default-directory))))
1410 (files (assoc master reftex-master-include-list))
1411 (cmd (format
1412 "%s %s" grep-cmd
1413 (mapconcat (function (lambda (x)
1414 (if (string-match re x)
1415 (match-string 1 x)
1416 x)))
1417 files " "))))
1418 (grep cmd)))
1419
1420(defun reftex-search-document (&optional regexp)
1421 "Regexp search through all files of the current TeX document.
1422Starts always in the master file. Stops when a match is found.
1423To continue searching for next match, use command \\[tags-loop-continue].
1424This works also without an active TAGS table."
1425 (interactive)
1426 (let ((default (reftex-this-word)))
1427 (if (not regexp)
1428 (setq regexp (read-string (format "Search regexp in document [%s]: "
1429 default))))
1430 (if (string= regexp "") (setq regexp (regexp-quote default)))
1431
1432 (reftex-access-scan-info current-prefix-arg)
1433 (tags-search regexp (list 'assoc (reftex-TeX-master-file)
1434 'reftex-master-include-list))))
1435
1436(defun reftex-query-replace-document (&optional from to delimited)
1437 "Run a query-replace-regexp of FROM with TO over the entire TeX document.
1438Third arg DELIMITED (prefix arg) means replace only word-delimited matches.
1439If you exit (\\[keyboard-quit] or ESC), you can resume the query replace
1440with the command \\[tags-loop-continue].
1441This works also without an active TAGS table."
1442 (interactive)
1443 (let ((default (reftex-this-word)))
1444 (if (not from)
1445 (progn
1446 (setq from (read-string (format "Replace regexp in document [%s]: "
1447 default)))
1448 (if (string= from "") (setq from (regexp-quote default)))))
1449 (if (not to)
1450 (setq to (read-string (format "Replace regexp %s with: " from))))
1451 (reftex-access-scan-info current-prefix-arg)
1452 (tags-query-replace from to (or delimited current-prefix-arg)
1453 (list 'assoc (reftex-TeX-master-file)
1454 'reftex-master-include-list))))
1455
1456(defun reftex-change-label (&optional from to)
fba437e6 1457 "Query replace FROM with TO in all \\label and \\ref commands.
a7ec1775
RS
1458Works on the entire multifile document.
1459If you exit (\\[keyboard-quit] or ESC), you can resume the query replace
1460with the command \\[tags-loop-continue].
1461This works also without an active TAGS table."
1462 (interactive)
1463 (let ((default (reftex-this-word "-a-zA-Z0-9_*.:")))
1464 (if (not from)
1465 (setq from (read-string (format "Replace label globally [%s]: "
1466 default))))
1467 (if (string= from "") (setq from default))
1468 (if (not to)
1469 (setq to (read-string (format "Replace label %s with: "
1470 from))))
1471 (reftex-query-replace-document
1472 (concat "\\\\\\(label\\|[a-z]*ref\\){" (regexp-quote from) "}")
1473 (format "\\\\\\1{%s}" to))))
1474
1475(defun reftex-this-word (&optional class)
1476;; grab the word around point
1477 (setq class (or class "-a-zA-Z0-9:_/.*;|"))
1478 (save-excursion
1479 (buffer-substring-no-properties
1480 (progn (skip-chars-backward class) (point))
1481 (progn (skip-chars-forward class) (point)))))
1482
1483;;; ===========================================================================
1484;;;
1485;;; Functions to create and reference automatic labels
1486
1487;; The following constants are derived from reftex-label-alist
1488
1489;; Prompt used for label type querys directed to the user
1490(defconst reftex-type-query-prompt nil)
1491
1492;; Help string for label type querys
1493(defconst reftex-type-query-help nil)
1494
1495;; Alist relating label type to reference format
1496(defconst reftex-typekey-to-format-alist nil)
1497
1498;; Alist relating label type to label affix
1499(defconst reftex-typekey-to-prefix-alist nil)
1500
1501;; Alist relating environments or macros to label type and context regexp
1502(defconst reftex-env-or-mac-alist nil)
1503
1504;; List of macros carrying a label
1505(defconst reftex-label-mac-list nil)
1506
1507;; List of environments carrying a label
1508(defconst reftex-label-env-list nil)
1509
1510;; List of all typekey letters in use
1511(defconst reftex-typekey-list nil)
1512
1513;; Alist relating magic words to a label type
1514(defconst reftex-words-to-typekey-alist nil)
1515
1516;; The last list-of-labels entry used in a reference
1517(defvar reftex-last-used-reference (list nil nil nil nil))
1518
1519;; The regular expression used to abbreviate words
1520(defconst reftex-abbrev-regexp
1521 (concat
1522 "^\\("
1523 (make-string (nth 0 reftex-abbrev-parameters) ?.)
1524 "[" (nth 2 reftex-abbrev-parameters) "]*"
1525 "\\)"
1526 "[" (nth 3 reftex-abbrev-parameters) "]"
1527 (make-string (1- (nth 1 reftex-abbrev-parameters)) ?.)))
1528
1529;; Global variables used for communication between functions
1530(defvar reftex-default-context-position nil)
1531(defvar reftex-location-start nil)
1532(defvar reftex-call-back-to-this-buffer nil)
1533
1534;; List of buffers created temporarily for lookup, which should be killed
1535(defvar reftex-buffers-to-kill nil)
1536
1537;; The regexp used to find section statements
1538(defconst reftex-section-regexp "^[ ]*\\\\\\(part\\|chapter\\|section\\|subsection\\|subsubsection\\|paragraph\\|subparagraph\\|subsubparagraph\\)\\*?\\(\\[[^]]*\\]\\)?{")
1539
1540;; LaTeX section commands and level numbers
1541(defconst reftex-section-levels
1542 '(
1543 ("part" . 0)
1544 ("chapter" . 1)
1545 ("section" . 2)
1546 ("subsection" . 3)
1547 ("subsubsection" . 4)
1548 ("paragraph" . 5)
1549 ("subparagraph" . 6)
1550 ("subsubparagraph" . 7)
1551 ))
1552
1553(defun reftex-label (&optional environment no-insert)
1554 "Insert a unique label. Return the label.
1555If ENVIRONMENT is given, don't bother to find out yourself.
1556If NO-INSERT is non-nil, do not insert label into buffer.
1557With prefix arg, force to rescan document first.
1558The label is also inserted into the label list.
1559This function is controlled by the settings of reftex-insert-label-flags."
1560
1561 (interactive)
1562
1563 ;; Ensure access to scanning info and rescan buffer if prefix are is '(4)
1564 (reftex-access-scan-info current-prefix-arg)
1565
1566 ;; Find out what kind of environment this is and abort if necessary
1567 (if (or (not environment)
1568 (not (assoc environment reftex-env-or-mac-alist)))
1569 (setq environment (reftex-label-location)))
1570 (if (not environment)
1571 (error "Can't figure out what kind of label should be inserted"))
1572
1573 ;; Ok, go ahead
1574 (let (label num typekey prefix entry cell lab valid default force-prompt)
1575 (setq typekey (nth 1 (assoc environment
1576 reftex-env-or-mac-alist)))
1577 (setq prefix (or (cdr (assoc typekey reftex-typekey-to-prefix-alist))
1578 (concat typekey "-")))
1579
1580 ;; make a default label
1581 (cond
1582
1583 ((reftex-typekey-check typekey (nth 0 reftex-insert-label-flags))
1584 ;; derive a label from context
1585 (setq default (nth 2 (reftex-label-info " ")))
1586 ;; catch the cases where the is actually no context available
1587 (if (or (string-match "NO MATCH FOR CONTEXT REGEXP" default)
1588 (string-match "ILLEGAL VALUE OF PARSE" default)
1589 (string-match "SECTION HEADING NOT FOUND" default)
1590 (string-match "HOOK ERROR" default)
1591 (string-match "^[ \t]*$" default))
1592 (setq default prefix
1593 force-prompt t) ; need to prompt
1594 (setq default (concat prefix (reftex-string-to-label default)))
1595
1596 ;; make it unique
1597 (setq label default)
1598 (setq num 1)
1599 (while (assoc label (symbol-value reftex-list-of-labels-symbol))
1600 (setq label (concat default "-" (setq num (1+ num)))))
1601 (setq default label)))
1602
1603 ((reftex-typekey-check typekey (nth 1 reftex-insert-label-flags)) ; prompt
1604 ;; Minimal default: the user will be prompted
1605 (setq default prefix))
1606
1607 (t
1608 ;; make an automatic label
1609 (while (assoc
1610 (setq default (concat prefix (reftex-next-label-number typekey)))
1611 (symbol-value reftex-list-of-labels-symbol)))))
1612
1613 ;; Should we ask the user?
1614 (if (or (reftex-typekey-check typekey
1615 (nth 1 reftex-insert-label-flags)) ; prompt
1616 force-prompt)
1617
1618 (while (not valid)
1619 ;; iterate until we get a legal label
1620
1621 (setq label (read-string "Label: " default))
1622
1623 ;; Lets make sure that this is a legal label
1624 (cond
1625
1626 ;; Test if label contains strange characters
1627 ((string-match reftex-label-illegal-re label)
1628 (message "Label \"%s\" contains illegal characters" label)
1629 (ding)
1630 (sit-for 2))
1631
1632 ;; Look it up in the label list
1633 ((setq entry (assoc label
1634 (symbol-value reftex-list-of-labels-symbol)))
1635 (message "Label \"%s\" exists in file %s" label (nth 3 entry))
1636 (ding)
1637 (sit-for 2))
1638
1639 ;; Label is ok
1640 (t
1641 (setq valid t))))
1642 (setq label default))
1643
1644 ;; Insert the label
1645 (if (not no-insert)
1646 (insert "\\label{" label "}"))
1647
1648 ;; Insert the label into the label list
1649 (if (symbol-value reftex-list-of-labels-symbol)
1650 (let ((cnt 0)
1651 (pos (point))
1652 (all (symbol-value reftex-list-of-labels-symbol))
1653 (look-for nil)
1654 (note nil)
1655 (text nil)
1656 (file (buffer-file-name)))
1657
1658 ;; find the previous label in order to know where to insert new label
1659 ;; into label list
1660 (save-excursion
1661 (if (re-search-backward "\\\\label{\\([^}]+\\)}" nil 1 2)
1662 (setq look-for (reftex-no-props (match-string 1))))
1663 (if (or (re-search-forward
1664 "\\\\\\(include\\|input\\){[^}\n]+}" pos t)
1665 (re-search-forward reftex-section-regexp pos t)
1666 (null look-for))
1667 (setq note "POSITION UNCERTAIN. RESCAN TO FIX.")))
1668 (if (not look-for)
1669 (set reftex-list-of-labels-symbol
1670 (cons (list label typekey text file note)
1671 (symbol-value reftex-list-of-labels-symbol)))
1672 (while all
1673 (setq cell (car all)
1674 all (cdr all)
1675 cnt (1+ cnt)
1676 lab (nth 0 cell))
1677 (if (string= lab look-for)
1678 (progn
1679 (setcdr
1680 (nthcdr (1- cnt)
1681 (symbol-value reftex-list-of-labels-symbol))
1682 (cons (list label typekey text file note)
1683 (nthcdr
1684 cnt (symbol-value reftex-list-of-labels-symbol))))
1685 ;; to end the loop, set all to nil
1686 (setq all nil)))))))
1687 ;; return value of the function is the label
1688 label))
1689
1690(defun reftex-string-to-label (string)
1691 ;; Convert a string (a sentence) to a label.
1692 ;;
1693 ;; Uses reftex-derive-label-parameters and reftex-abbrev-parameters
1694 ;;
1695
1696 (let* ((words0 (reftex-split "[- \t\n\r]+"
1697 (reftex-no-props string)))
1698 (ignore-words (nth 5 reftex-derive-label-parameters))
1699 words word)
1700
1701 ;; remove words from the ignore list or with funny characters
1702 (while words0
1703 (setq word (car words0) words0 (cdr words0))
1704 (cond
1705 ((member (downcase word) ignore-words))
1706 ((string-match reftex-label-illegal-re word)
1707 (if (nth 2 reftex-derive-label-parameters)
1708 (progn
1709 (while (string-match reftex-label-illegal-re word)
1710 (setq word (replace-match "" nil nil word)))
1711 (setq words (cons word words)))))
1712 (t
1713 (setq words (cons word words)))))
1714 (setq words (nreverse words))
1715
1716 ;; restrict number of words
1717 (if (> (length words) (nth 0 reftex-derive-label-parameters))
1718 (setcdr (nthcdr (1- (nth 0 reftex-derive-label-parameters)) words) nil))
1719
1720 ;; First, try to use all words
1721 (setq string (mapconcat '(lambda(w) w) words
1722 (nth 4 reftex-derive-label-parameters)))
1723
1724 ;; Abbreviate words if enforced by user settings or string length
1725 (if (or (eq t (nth 3 reftex-derive-label-parameters))
1726 (and (nth 3 reftex-derive-label-parameters)
1727 (> (length string) (nth 1 reftex-derive-label-parameters))))
1728 (setq words
1729 (mapcar
1730 '(lambda (w) (if (string-match reftex-abbrev-regexp w)
1731 (match-string 1 w)
1732 w))
1733 words)
1734 string (mapconcat '(lambda(w) w) words
1735 (nth 4 reftex-derive-label-parameters))))
1736
1737 ;; Shorten if still to long
1738 (setq string
1739 (if (> (length string) (nth 1 reftex-derive-label-parameters))
1740 (substring string 0 (nth 1 reftex-derive-label-parameters))
1741 string))
1742
1743 ;; Delete the final punctuation, if any
1744 (if (string-match "[^a-zA-Z0-9]+$" string)
1745 (setq string (replace-match "" nil nil string)))
1746 string))
1747
1748(defun reftex-label-location (&optional bound)
1749 ;; Return the environment or macro which determines the label type at point.
1750 ;; If optional BOUND is an integer, limit backward searches to that point.
1751
1752 (let* ((loc1 (reftex-what-macro reftex-label-mac-list bound))
1753 (loc2 (reftex-what-environment reftex-label-env-list bound))
1754 (p1 (or (cdr loc1) 0))
1755 (p2 (or (cdr loc2) 0)))
1756
1757 (setq reftex-location-start (max p1 p2))
1758 (if (> p1 p2)
1759 (progn
1760 (setq reftex-default-context-position p1)
1761 (car loc1))
1762 (setq reftex-default-context-position
1763 (+ p2 8 (length (car loc2))))
1764 (or (car loc2) "section"))))
1765
1766
1767(defun reftex-next-label-number (type)
1768 ;; Increment value of automatic labels in current buffer. Return new value.
1769
1770 ;; Ensure access to scanning info
1771 (reftex-access-scan-info)
1772
1773 (let ((n (cdr (assoc type (symbol-value reftex-label-numbers-symbol)))))
1774 (if (not (integerp n))
1775 ;; oops - type not known - make one here
1776 (progn
1777 (set reftex-label-numbers-symbol
1778 (cons (cons type 0)
1779 (symbol-value reftex-label-numbers-symbol)))
1780 (setq n 0)))
1781 (setq n (1+ n))
1782 (setcdr (assoc type (symbol-value reftex-label-numbers-symbol)) n)
1783 n))
1784
1785;; Help string for the reference label menu
1786(defconst reftex-reference-label-help
1787 " AVAILABLE KEYS IN REFERENCE LABEL MENU
1788 ======================================
1789 n / p Go to next/previous label (Cursor motion works as well)
1790 r / s Rescan document for labels / Switch label type
1791 t / # Toggle table of contents / Toggle counter mode
1792 c Toggle display of short context
1793 SPACE Show full context for current label in other window
1794 f Toggle follow mode: other window will follow context
1795 a / q Use last referenced label / Quit without accepting label
1796 ? / C-r Display this help message / Recursive Edit into other window
1797 RETURN Accept current label")
1798
1799(defun reftex-reference (&optional type no-insert)
1800 "Make a LaTeX reference. Look only for labels of a certain TYPE.
1801With prefix arg, force to rescan buffer for labels. This should only be
1802necessary if you have recently entered labels yourself without using
1803reftex-label. Rescanning of the buffer can also be requested from the
1804label selection menu.
1805The function returns the selected label or nil.
fba437e6 1806If NO-INSERT is non-nil, do not insert \\ref command, just return label.
a7ec1775
RS
1807When called with 2 C-u prefix args, disable magic word recognition."
1808
1809 (interactive)
1810
1811 ;; check for active recursive edits
1812 (reftex-check-recursive-edit)
1813
1814 ;; Ensure access to scanning info and rescan buffer if prefix are is '(4)
1815 (reftex-access-scan-info current-prefix-arg)
1816
1817 (if (not type)
1818 ;; guess type from context
1819 (if (and reftex-guess-label-type
1820 (not (= 16 (prefix-numeric-value current-prefix-arg)))
1821 (setq type (assoc (downcase (reftex-word-before-point))
1822 reftex-words-to-typekey-alist)))
1823 (setq type (cdr type))
1824 (setq type (reftex-query-label-type))))
1825
1826 (let (label pair
1827 (form (or (cdr (assoc type reftex-typekey-to-format-alist))
1828 "\\ref{%s}")))
1829
1830 ;; Have the user select a label
1831 (setq pair (reftex-offer-label-menu type))
1832 (setq label (car pair))
1833
1834 (if (and label
1835 (not no-insert))
1836 (progn
1837 ;; do we need to remove spaces?
1838 (if (string= "~" (substring form 0 1))
1839 (while (or (= (preceding-char) ?\ )
1840 (= (preceding-char) ?\C-i))
1841 (backward-delete-char 1)))
1842 ;; ok, insert the reference
1843 (insert (format form label label))
1844 (message ""))
1845 (message "Quit"))
1846 ;; return the label
1847 label))
1848
1849(defun reftex-goto-label (&optional arg)
1850 "Go to a LaTeX label. With prefix ARG: go to label in another window."
1851 (interactive "P")
1852 (let (type label file pair)
1853 (if (not type)
1854 (setq type (reftex-query-label-type)))
1855
1856 (setq pair (reftex-offer-label-menu type)
1857 label (car pair)
1858 file (cdr pair))
1859 (if (and label file (file-exists-p file))
1860 (progn
1861 (if arg
1862 (find-file-other-window file)
1863 (find-file file))
1864 (goto-char (point-min))
1865 (if (not (search-forward (concat "\\label{" label "}") nil t))
1866 (error "No such label found: %s" label)
1867 (reftex-highlight 0 (match-beginning 0) (match-end 0))
1868 (add-hook 'pre-command-hook 'reftex-highlight-shall-die)))
1869 (message "Quit")
1870 nil)))
1871
1872;; Internal list with index numbers of labels in the selection menu
1873(defvar reftex-label-index-list nil)
1874
1875(defun reftex-offer-label-menu (typekey)
1876 ;; Offer a menu with the appropriate labels. Return (label . file).
1877 (let* ((buf (current-buffer))
1878 (near-label (reftex-find-nearby-label))
1879 (toc (reftex-typekey-check typekey reftex-label-menu-flags 0))
1880 (context (not (reftex-typekey-check
1881 typekey reftex-label-menu-flags 3)))
1882 (counter (reftex-typekey-check
1883 typekey reftex-label-menu-flags 2))
1884 (follow (reftex-typekey-check
1885 typekey reftex-label-menu-flags 4))
1886 offset rtn key cnt entry)
1887
1888 (setq reftex-call-back-to-this-buffer buf)
1889 (setq entry (cons nil nil))
1890
1891 (unwind-protect
1892 (catch 'exit
1893 (while t
1894 (save-window-excursion
1895 (switch-to-buffer-other-window "*RefTeX Select*")
1896 (erase-buffer)
1897 (setq truncate-lines t)
1898 (setq reftex-label-index-list (reftex-make-and-insert-label-list
1899 typekey buf toc context counter
1900 near-label))
1901 (setq near-label "_ ") ; turn off search for near label
1902 (setq offset (or (car reftex-label-index-list) offset))
1903 ;; use only when searched
1904 (setq reftex-label-index-list (cdr reftex-label-index-list))
1905 ;; only this is the true list
1906 (if (not reftex-label-index-list)
1907 (error "No labels of type \"%s\"" typekey))
1908 (setq rtn
1909 (reftex-select-item
1910 nil
1911 "Label: [n]ext [p]rev [r]escan [t]oc [ ]context [q]uit RETURN [?]HELP+more"
1912 "^>"
1913 "\n[^.]"
1914 2
1915 reftex-reference-label-help
1916 '(?r ?c ?t ?s ?# ?a)
1917 offset
1918 'reftex-select-label-callback follow))
1919 (setq key (car rtn)
1920 cnt (cdr rtn)
1921 offset (1+ cnt))
1922 (if (not key) (throw 'exit nil))
1923 (cond
1924 ((equal key ?r)
1925 ;; rescan buffer
1926 (reftex-parse-document buf))
1927 ((equal key ?c)
1928 ;; toggle context mode
1929 (setq context (not context)))
1930 ((equal key ?s)
1931 ;; switch type
1932 (setq typekey (reftex-query-label-type)))
1933 ((equal key ?t)
1934 ;; toggle tabel of contents display
1935 (setq toc (not toc)))
1936 ((equal key ?#)
1937 ;; toggle counter display
1938 (setq counter (not counter)))
1939 ((equal key ?a)
1940 ;; reuse the last referenced label again
1941 (setq entry reftex-last-used-reference)
1942 (throw 'exit t))
1943 (t
1944 (set-buffer buf)
1945 (setq entry (nth (nth cnt reftex-label-index-list)
1946 (symbol-value reftex-list-of-labels-symbol)))
1947 (setq reftex-last-used-reference entry)
1948 (throw 'exit t))))))
1949 (kill-buffer "*RefTeX Select*")
1950 (reftex-kill-temporary-buffers))
1951 (cons (reftex-no-props (nth 0 entry)) (nth 3 entry))))
1952
1953;; Indentation for table of context lines in the menu
1954(defconst reftex-toc-indent " ")
1955;; Indentation for the lines containing the label
1956(defconst reftex-label-indent "> ")
1957;; Indentation for context lines
1958(defconst reftex-context-indent ". ")
1959;; Indentation per section level
1960(defvar reftex-level-indent 2
1961 "*Number of spaces to be used for indentation per section level.
1962With more indentation, the label menu looks nicer, but shows less context.
1963Changing this is only fully operational after the next buffer scan.")
1964
1965(defun reftex-make-and-insert-label-list (typekey0 buf toc context
1966 counter near-label)
1967 ;; Insert a menu of all labels in buffer BUF into current buffer.
1968 ;; Return the list of labels, with the index of NEAR-LABEL as extra car.
1969 (let (ins-list index-list offset)
1970 (save-excursion
1971 (set-buffer buf)
1972 (let* ((all nil)
1973 (font (reftex-use-fonts))
1974 (cnt 0)
1975 (file nil)
1976 (index -1)
1977 (toc-indent reftex-toc-indent)
1978 (label-indent
1979 (concat reftex-label-indent
1980 (if toc (make-string (* 7 reftex-level-indent) ?\ ) "")))
1981 (context-indent
1982 (concat reftex-context-indent
1983 (if toc (make-string (* 7 reftex-level-indent) ?\ ) "")))
1984 cell text label typekey note comment)
1985
1986 ; Ensure access to scanning info
1987 (reftex-access-scan-info)
1988
1989 (setq all (symbol-value reftex-list-of-labels-symbol))
1990
1991 (while all
1992
1993 (setq index (1+ index)
1994 cell (car all)
1995 all (cdr all))
1996
1997 (if (null (nth 2 cell))
1998 ;; No context yet. Quick update
1999 (progn
2000 (setq cell (reftex-label-info-update cell))
2001 (setcar (nthcdr index
2002 (symbol-value reftex-list-of-labels-symbol))
2003 cell)))
2004
2005 ;; in the following setq we *copy* the label, since we will change
2006 ;; its properties, and we cannot have any properties in the list
2007 ;; (because of assoc searches)
2008 (setq label (copy-sequence (nth 0 cell))
2009 typekey (nth 1 cell)
2010 text (nth 2 cell)
2011 file (nth 3 cell)
2012 note (nth 4 cell)
2013 comment (get-text-property 0 'in-comment text))
2014
2015 (if (string= label near-label)
2016 (setq offset (1+ cnt)))
2017
2018 (cond
2019 ((and toc (string= typekey "toc"))
2020 (setq ins-list
2021 (cons (concat toc-indent text "\n")
2022 ins-list)))
2023 ((string= typekey "toc"))
2024 ((and (or (string= typekey typekey0) (string= typekey0 " "))
2025 (or (nth 5 reftex-label-menu-flags) ; show-commented?
2026 (null comment)))
2027 (setq cnt (1+ cnt))
2028 (if comment (setq label (concat "% " label)))
2029 (if font
2030 (put-text-property
2031 0 (length label)
2032 'face
2033 (if comment
2034 'font-lock-comment-face
2035 'font-lock-reference-face)
2036 label))
2037 (setq index-list (cons index index-list))
2038 (setq ins-list
2039 (cons (concat
2040 label-indent
2041 label
2042 (if counter (format " (%d) " cnt))
2043 (if comment " LABEL IS COMMENTED OUT ")
2044 (if note (concat " " note) "")
2045 "\n"
2046 (if context (concat context-indent text "\n")))
2047 ins-list))))
2048 )))
2049
2050 (apply 'insert (nreverse ins-list))
2051 (cons offset (nreverse index-list))))
2052
2053(defun reftex-query-label-type ()
2054 ;; Ask for label type
2055 (message reftex-type-query-prompt)
2056 (let ((key (read-char)))
2057 (if (equal key ?\?)
2058 (progn
2059 (save-window-excursion
2060 (with-output-to-temp-buffer "*RefTeX Help*"
2061 (princ reftex-type-query-help))
2062 (setq key (read-char))
2063 (kill-buffer "*RefTeX Help*"))))
2064 (if (not (member (char-to-string key) reftex-typekey-list))
2065 (error "No such label type: %s" (char-to-string key)))
2066 (char-to-string key)))
2067
2068(defun reftex-find-nearby-label ()
2069 ;; Find a nearby label.
2070 (save-excursion
2071 (if (or (re-search-backward "\\\\label{\\([^}]+\\)}" nil t)
2072 (re-search-forward "\\\\label{\\([^}]+\\)}" nil t))
2073 (reftex-no-props (match-string 1))
2074 nil)))
2075
2076;; Variable holding the vector with section numbers
2077(defvar reftex-section-numbers [0 0 0 0 0 0 0 0])
2078
2079(defun reftex-scan-buffer-for-labels (label-numbers-symbol label-list-symbol)
2080 ;; Scan the buffer for labels and save them in a list.
2081 (save-excursion
2082 (let ((regexp (concat "\\\\label{\\([^}]*\\)}" "\\|"
2083 reftex-section-regexp))
2084 (font (reftex-use-fonts))
2085 (bound 0)
2086 (highest-level 100)
2087 file (level 1) start star text text1 label section-number macro find)
2088 (set label-list-symbol nil)
2089 (goto-char 0)
2090
2091 ;; reset label numbers
2092 (set label-numbers-symbol
2093 (mapcar '(lambda(x) (cons x 0)) reftex-typekey-list))
2094
2095 ;; reset section numbers
2096 (reftex-section-number reftex-section-numbers -1)
2097
2098 (while (re-search-forward regexp nil t)
2099 (setq file (get-text-property (match-beginning 0) 'file))
2100 (if (string= (buffer-substring (match-beginning 0)
2101 (+ 7 (match-beginning 0))) "\\label{")
2102 ;; It is a label
2103 (progn
2104 (setq label (reftex-no-props (match-string 1)))
2105 (set label-list-symbol
2106 (cons (reftex-label-info label file bound)
2107 (symbol-value label-list-symbol))))
2108
2109 ;; It is a section
2110 (setq bound (point))
2111 (setq star (= ?* (char-after (match-end 2))))
2112 (setq find (buffer-substring-no-properties
2113 (1- (match-beginning 2)) (match-end 0)))
2114 (setq macro (reftex-no-props (match-string 2)))
2115 (setq level (cdr (assoc macro reftex-section-levels)))
2116
2117 (setq section-number (reftex-section-number
2118 reftex-section-numbers level star))
2119 (setq highest-level (min highest-level level))
2120 (if (= level highest-level)
2121 (message
2122 "Scanning %s %s ..."
2123 (car (nth level reftex-section-levels))
2124 section-number))
2125
2126 ;; get the title
2127 (save-match-data
2128 (setq text1 (reftex-context-substring))
2129 (setq text (reftex-nicify-text text1)))
2130
2131 (setq find (reftex-allow-for-ctrl-m (concat find text1)))
2132
2133 ;; add section number and indentation
2134 (setq text
2135 (concat
2136 (make-string (* reftex-level-indent level) ?\ )
2137 (if (nth 1 reftex-label-menu-flags) ; section number flag
2138 (concat section-number " "))
2139 text))
2140 ;; fontify
2141 (if font (put-text-property 0 (length text)
2142 'face 'font-lock-comment-face text))
2143
2144 ;; insert in list
2145 (set label-list-symbol
2146 (cons (list nil "toc" text file find)
2147 (symbol-value label-list-symbol)))))
2148 (set label-list-symbol
2149 (nreverse (symbol-value label-list-symbol))))))
2150
2151
2152(defun reftex-label-info-update (cell)
2153 ;; Update information about just one label in a different file.
2154 ;; CELL contains the old info list
2155 (let* ((label (nth 0 cell))
2156 (typekey (nth 1 cell))
2157 (text (nth 2 cell))
2158 (file (nth 3 cell))
2159 (note (nth 4 cell))
2160 (buf (reftex-get-file-buffer-force
2161 file (not reftex-keep-temporary-buffers))))
2162 (if (not buf)
2163 (list label typekey "" file "LOST LABEL. RESCAN TO FIX.")
2164 (save-excursion
2165 (set-buffer buf)
2166 (save-restriction
2167 (widen)
2168 (goto-char 1)
2169
2170 (if (re-search-forward (concat "\\\\label{" (regexp-quote label) "}")
2171 nil t)
2172 (append (reftex-label-info label file) (list note))
2173 (list label typekey "" file "LOST LABEL. RESCAN TO FIX.")))))))
2174
2175(defun reftex-label-info (label &optional file bound)
2176 ;; Return info list on LABEL at point.
2177 (let* ((env-or-mac (reftex-label-location bound))
2178 (typekey (nth 1 (assoc env-or-mac reftex-env-or-mac-alist)))
2179 (file (or file (buffer-file-name)))
2180 (parse (if (reftex-typekey-check
2181 typekey reftex-use-text-after-label-as-context)
2182 nil
2183 (nth 2 (assoc env-or-mac reftex-env-or-mac-alist))))
2184 (text (reftex-short-context env-or-mac parse reftex-location-start)))
2185 (if (reftex-in-comment)
2186 (put-text-property 0 1 'in-comment t text))
2187 (list label typekey text file)))
2188
2189(defun reftex-in-comment ()
2190 (save-excursion
2191 (skip-chars-backward "^%\n\r")
2192 (= (preceding-char) ?%)))
2193
2194(defun reftex-short-context (env parse &optional bound)
2195 ;; Get about one line of useful context for the label definition at point.
2196
2197 (reftex-nicify-text
2198
2199 (cond
2200
2201 ((null parse)
2202 (save-excursion
2203 (reftex-context-substring)))
2204
2205 ((eq parse t)
2206 (if (string= env "section")
2207 ;; special treatment for section labels
2208 (save-excursion
2209 (if (re-search-backward reftex-section-regexp (point-min) t)
2210 (progn
2211 (goto-char (match-end 0))
2212 (reftex-context-substring))
2213 "SECTION HEADING NOT FOUND"))
2214 (save-excursion
2215 (goto-char reftex-default-context-position)
2216 (reftex-context-substring))))
2217
2218 ((stringp parse)
2219 (save-excursion
2220 (if (re-search-backward parse bound t)
2221 (progn
2222 (goto-char (match-end 0))
2223 (reftex-context-substring))
2224 "NO MATCH FOR CONTEXT REGEXP")))
2225 ((fboundp parse)
2226 ;; A hook function. Call it.
2227 (save-excursion
2228 (condition-case error-var
2229 (funcall parse env)
2230 ('error (format "HOOK ERROR: %s" (cdr error-var))))))
2231 (t
2232 "ILLEGAL VALUE OF PARSE"))))
2233
2234(defun reftex-context-substring ()
2235 ;; Return up to 100 chars from point
2236 ;; When point is just after a { or [, limit string to matching parenthesis
2237 (cond
2238 ((or (= (preceding-char) ?\{)
2239 (= (preceding-char) ?\[))
2240 ;; inside a list - get only the list, with modified syntax to be perfect
2241 (buffer-substring
2242 (point)
2243 (min (+ 100 (point))
2244 (point-max)
2245 (condition-case nil
2246 (progn
2247 (up-list 1)
2248 (1- (point)))
2249 ('error (point-max))))))
2250 (t
2251 ;; no list - just grab 100 characters
2252 (buffer-substring (point) (min (+ 100 (point)) (point-max))))))
2253
2254(defun reftex-section-number (section-numbers &optional level star)
2255 ;; Return a string with the current section number.
2256 ;; When LEVEL is non-nil, increase section numbers on that level.
2257 (let* ((depth 6) idx n (string ""))
2258 (if level
2259 (progn
2260 (if (and (> level -1) (not star))
2261 (aset section-numbers level (1+ (aref section-numbers level))))
2262 (setq idx (1+ level))
2263 (while (<= idx depth)
2264 (aset section-numbers idx 0)
2265 (setq idx (1+ idx)))))
2266 (setq idx 0)
2267 (while (<= idx depth)
2268 (setq n (aref section-numbers idx))
2269 (setq string (concat string (if (not (string= string "")) "." "")
2270 (int-to-string n)))
2271 (setq idx (1+ idx)))
2272 (save-match-data
2273 (if (string-match "\\`\\(0\\.\\)+" string)
2274 (setq string (replace-match "" nil nil string)))
2275 (if (string-match "\\(\\.0\\)+\\'" string)
2276 (setq string (replace-match "" nil nil string))))
2277 (if star
2278 (concat (make-string (1- (length string)) ?\ ) "*")
2279 string)))
2280
2281;; A variable to remember the index of the last label context shown
2282(defvar reftex-last-cnt 0)
2283
2284(defun reftex-select-label-callback (cnt)
2285 ;; Callback function called from the label selection in order to
2286 ;; show context in another window
2287 (let* ((this-window (selected-window))
2288 index entry label file buffer)
2289 ;; pop to original buffer in order to get correct variables
2290 (catch 'exit
2291 (save-excursion
2292 (set-buffer reftex-call-back-to-this-buffer)
2293 (setq index (nth (or cnt 1) reftex-label-index-list)
2294 entry (nth index (symbol-value reftex-list-of-labels-symbol))
2295 label (nth 0 entry)
2296 file (nth 3 entry)))
2297
2298 ;; goto the file in another window
2299 (setq buffer (reftex-get-file-buffer-force
2300 file (not reftex-keep-temporary-buffers)))
2301 (if buffer
2302 ;; good - the file is available
2303 (switch-to-buffer-other-window buffer)
2304 ;; we have got a problem here. The file does not exist.
2305 ;; Let' get out of here..
2306 (ding)
2307 (throw 'exit nil))
2308
2309
2310 ;; search for that label
2311 (if (not (and (integerp cnt)
2312 (integerp reftex-last-cnt)
2313 (if (> cnt reftex-last-cnt)
2314 (search-forward (concat "\\label{" label "}") nil t)
2315 (search-backward (concat "\\label{" label "}") nil t))))
2316 (progn
2317 (goto-char 1)
2318 (search-forward (concat "\\label{" label "}") nil t)))
2319 (reftex-highlight 0 (match-beginning 0) (match-end 0))
2320 (reftex-show-entry)
2321 (recenter (/ (window-height) 2))
2322 (select-window this-window))))
2323
2324(defun reftex-pop-to-label (label file-list &optional mark-to-kill highlight)
2325 ;; Find LABEL in any file in FILE-LIST in another window.
2326 ;; If mark-to-kill is non-nil, mark new buffer for killing.
2327 ;; If HIGHLIGHT is non-nil, highlight the label definition.
2328 (let* ((re (concat "\\\\label{" (regexp-quote label) "}"))
2329 file buf)
2330 (catch 'exit
2331 (while file-list
2332 (setq file (car file-list)
2333 file-list (cdr file-list))
2334 (if (not (setq buf (reftex-get-file-buffer-force file mark-to-kill)))
2335 (error "No such file %s" file))
2336 (set-buffer buf)
2337 (widen)
2338 (goto-char (point-min))
2339 (if (re-search-forward re nil t)
2340 (progn
2341 (switch-to-buffer-other-window buf)
2342 (goto-char (match-beginning 0))
2343 (recenter (/ (window-height) 2))
2344 (if highlight
2345 (reftex-highlight 0 (match-beginning 0) (match-end 0)))
2346 (throw 'exit (selected-window)))))
2347 (error "Label %s not found" label))))
2348
2349(defun reftex-find-duplicate-labels ()
2350 "Produce a list of all duplicate labels in the document."
2351
2352 (interactive)
2353
2354 ;; Rescan the document to make sure
2355 (reftex-access-scan-info t)
2356
2357 (let ((master (reftex-TeX-master-file))
2358 (dlist
2359 (mapcar
2360 '(lambda(x)
2361 (let (x1)
2362 (cond
2363 ((car x)
2364 (setq x1 (reftex-all-assoc-string
2365 (car x) (symbol-value reftex-list-of-labels-symbol)))
2366 (if (< 1 (length x1))
2367 (append (list (reftex-no-props (car x)))
2368 (mapcar '(lambda(x)
2369 (abbreviate-file-name (nth 3 x))) x1))
2370 (list nil)))
2371 (t nil))))
2372 (reftex-uniquify (symbol-value reftex-list-of-labels-symbol)))))
2373 (setq dlist (reftex-uniquify dlist))
2374 (if (null dlist) (error "No duplicate labels in document"))
025bb635 2375 (switch-to-buffer-other-window "*Duplicate Labels*")
a7ec1775
RS
2376 (make-local-variable 'TeX-master)
2377 (setq TeX-master master)
2378 (erase-buffer)
2379 (insert " MULTIPLE LABELS IN CURRENT DOCUMENT:\n")
2380 (insert " Move point to label and type `M-x reftex-change-label'\n"
2381 " This will run a query-replace on the label and its references\n")
2382 (insert " LABEL FILE\n")
2383 (insert " -------------------------------------------------------------\n")
2384 (while dlist
2385 (if (and (car (car dlist))
2386 (cdr (car dlist)))
2387 (progn
2388 (insert (mapconcat '(lambda(x) x) (car dlist) "\n ") "\n")))
2389 (setq dlist (cdr dlist)))
2390 (goto-char (point-min))))
2391
2392(defun reftex-all-assoc-string (key list)
2393 ;; Return a list of all associations of KEY in LIST. Comparison with string=
2394 (let (rtn)
2395 (while list
2396 (if (string= (car (car list)) key)
2397 (setq rtn (cons (car list) rtn)))
2398 (setq list (cdr list)))
2399 (nreverse rtn)))
2400
2401(defun reftex-kill-temporary-buffers ()
2402 ;; Kill all buffers in the list reftex-kill-temporary-buffers.
2403 (while reftex-buffers-to-kill
2404 (if (bufferp (car reftex-buffers-to-kill))
2405 (progn
2406 (and (buffer-modified-p (car reftex-buffers-to-kill))
2407 (y-or-n-p (format "Save file %s? "
2408 (buffer-file-name
2409 (car reftex-buffers-to-kill))))
2410 (save-excursion
2411 (set-buffer (car reftex-buffers-to-kill))
2412 (save-buffer)))
2413 (kill-buffer (car reftex-buffers-to-kill))))
2414 (setq reftex-buffers-to-kill (cdr reftex-buffers-to-kill))))
2415
2416(defun reftex-show-entry ()
2417 ;; Show entry if point is hidden by outline mode
2418 (let ((pos (point)))
2419 (if (and reftex-auto-show-entry
2420 (boundp 'outline-minor-mode)
2421 outline-minor-mode
2422 (looking-at "[^\n\r]*\r"))
2423 (progn
2424 (outline-back-to-heading)
2425 (show-entry)
2426 (goto-char pos)))))
2427
2428
2429(defun reftex-nicify-text (text)
2430 ;; Make TEXT nice for inclusion into label menu
2431 (while (string-match "[\n\r\t]\\|[ \t][ \t]+" text) ; remove extra whitespace
2432 (setq text (replace-match " " nil t text)))
2433 (if (string-match "\\\\end{.*" text) ; nothing beyond \end{
2434 (setq text (replace-match "" nil t text)))
2435 (if (string-match "\\\\label{[^}]*}" text) ; kill the label
2436 (setq text (replace-match "" nil t text)))
2437 (if (string-match "^ +" text) ; leading whitespace
2438 (setq text (replace-match "" nil t text)))
2439 (cond
2440 ((> (length text) 100) ; not to long
2441 (setq text (substring text 0 100)))
2442 ((= (length text) 0) ; not empty
2443 (setq text " ")))
2444 text)
2445
2446(defun reftex-typekey-check (typekey conf-variable &optional n)
2447 ;; Check if CONF-VARIABLE is true or contains TYPEKEY
2448 (and n (setq conf-variable (nth n conf-variable)))
2449 (or (equal conf-variable t)
2450 (and (stringp conf-variable)
2451 (string-match (concat "[" conf-variable "]") typekey))))
2452
2453;;; ===========================================================================
2454;;;
2455;;; Table of contents (contributed from Stephen Eglen, changed by C. Dominik)
2456
2457;; We keep at most one *toc* buffer - it is easy to make them
2458
2459(defvar reftex-last-toc-master nil
2460 "Stores the name of the tex file that `reftex-toc' was last run on.")
2461
2462(defvar reftex-last-toc-file nil
2463 "Stores the file name from which `reftex-toc' was called. For redo command.")
2464
2465(defvar reftex-toc-return-marker (make-marker)
2466 "Marker which makes it possible to return from toc to old position.")
2467
2468(defun reftex-toc ()
2469 "Show the table of contents for the current document.
2470To see the corresponding part of the LaTeX document, use within the
2471*toc* buffer:
2472
2473SPC Show the corresponding section of the LaTeX document
2474RET Goto the section and hide the *toc* buffer
2475q Hide the *toc* window and return to position of last reftex-toc command
2476Q Kill the *toc* buffer and return to position of last reftex-toc command
2477f Toggle follow mode on and off
2478
2479When called with a raw C-u prefix, rescan the document first."
2480
2481 (interactive)
2482
2483 (and (not (string= reftex-last-toc-master (reftex-TeX-master-file)))
2484 (get-buffer "*toc*")
2485 (kill-buffer "*toc*"))
2486
2487 (setq reftex-last-toc-file (buffer-file-name))
2488 (setq reftex-last-toc-master (reftex-TeX-master-file))
2489
2490 (set-marker reftex-toc-return-marker (point))
2491
2492 ;; if follow mode is active, arrange to delay it one command
2493 (if reftex-toc-follow-mode
2494 (setq reftex-toc-follow-mode 1))
2495
2496 (if (and current-prefix-arg
2497 (get-buffer "*toc*"))
2498 (kill-buffer "*toc*"))
2499
2500 ;; Ensure access to scanning info and rescan buffer if prefix are is '(4)
2501 (reftex-access-scan-info current-prefix-arg)
2502
2503 (let* ((all (symbol-value reftex-list-of-labels-symbol))
2504 (where (reftex-nearest-section))
2505 toc toc1 cell label file find startpos)
2506
2507 (if (and (get-buffer "*toc*")
2508 (get-buffer-window (get-buffer "*toc*")))
2509 (select-window (get-buffer-window (get-buffer "*toc*")))
2510 (delete-other-windows)
2511 (switch-to-buffer-other-window (current-buffer))
2512 (switch-to-buffer-other-window (get-buffer-create "*toc*")))
2513
2514 (cond
2515 ;; buffer is empty - fill it with the table of contents
2516 ((= (buffer-size) 0)
2517
2518 (local-set-key " " 'reftex-toc-view-line)
2519 (local-set-key "\C-m" 'reftex-toc-goto-line-and-hide)
2520 (local-set-key "r" 'reftex-toc-redo)
2521 (local-set-key "q" 'reftex-toc-quit)
2522 (local-set-key "Q" 'reftex-toc-quit-and-kill)
2523 (local-set-key "f" 'reftex-toc-toggle-follow)
2524 (setq truncate-lines t)
2525 (make-local-hook 'post-command-hook)
2526 (make-local-hook 'pre-command-hook)
2527 (setq post-command-hook '(reftex-toc-post-command-hook))
2528 (setq pre-command-hook '(reftex-toc-pre-command-hook))
2529
2530 (insert (format
2531"TABLE-OF-CONTENTS on %s
2532MENU: SPC=view RET=goto [q]uit [Q]uit+kill [r]escan [f]ollow-mode on/off
2533-------------------------------------------------------------------------------
2534" (abbreviate-file-name reftex-last-toc-master)))
2535 (setq startpos (point))
2536
2537 (if (reftex-use-fonts)
2538 (put-text-property 1 (point) 'face 'font-lock-keyword-face))
2539 (put-text-property 1 (point) 'intangible t)
2540
2541 (while all
2542 (setq cell (car all)
2543 all (cdr all))
2544 (setq label (nth 0 cell)
2545 toc (nth 2 cell)
2546 file (nth 3 cell)
2547 find (nth 4 cell))
2548 (if (not label)
2549 (progn
2550 (setq toc1 (concat toc "\n"))
2551 (put-text-property 0 (length toc1)
2552 'file file toc1)
2553 (put-text-property 0 (length toc1)
2554 'find find toc1)
2555 (insert toc1)
2556 )))
2557
2558 (backward-delete-char 1)
2559
2560 (setq buffer-read-only t))
2561 (t
2562 (goto-line 3)
2563 (beginning-of-line)
2564 (setq startpos (point))))
2565
2566 ;; Find the correct section
2567 (goto-char (point-max))
2568 (beginning-of-line)
2569 (while (and (> (point) startpos)
2570 (or (not (string= (get-text-property (point) 'file)
2571 (car where)))
2572 (not (string= (get-text-property (point) 'find)
2573 (cdr where)))))
2574 (beginning-of-line 0))))
2575
2576(defun reftex-nearest-section ()
2577 ;; Return (file . find) of nearest section command
2578 (let (cell label rest)
2579 (save-excursion
2580 (cond
2581 ;; Try to find a section heading
2582 ((or (re-search-backward reftex-section-regexp nil t)
2583 (re-search-forward reftex-section-regexp nil t))
2584 (goto-char (match-end 0))
2585 (cons (buffer-file-name)
2586 (reftex-allow-for-ctrl-m
2587 (concat (buffer-substring-no-properties
2588 (1- (match-beginning 1)) (match-end 0))
2589 (reftex-context-substring)))))
2590 ;; Try to find a label
2591 ((and (or (re-search-backward "\\\\label{\\([^}]+\\)}" nil t)
2592 (re-search-forward "\\\\label{\\([^}]+\\)}" nil t))
2593 (setq label (reftex-no-props (match-string 1)))
2594 (setq cell (assoc label (symbol-value
2595 reftex-list-of-labels-symbol)))
2596 (setq rest (memq cell (symbol-value reftex-list-of-labels-symbol)))
2597 (setq cell (car (memq (assoc nil rest) rest)))
2598 (null (car cell)))
2599 (cons (nth 3 cell) (nth 4 cell)))
2600 (t nil)))))
2601
2602(defun reftex-toc-pre-command-hook ()
2603 ;; used as pre command hook in *toc* buffer
2604 (reftex-unhighlight 0)
2605 (reftex-unhighlight 1))
2606
2607(defun reftex-toc-post-command-hook ()
2608 ;; used in the post-command-hook for the *toc* buffer
2609 (and (> (point) 1)
2610 (save-excursion
2611 (reftex-highlight 1
2612 (progn (beginning-of-line) (point))
2613 (progn (end-of-line) (point)))))
2614 (cond
2615 ((integerp reftex-toc-follow-mode)
2616 ;; remove delayed action
2617 (setq reftex-toc-follow-mode t))
2618 (reftex-toc-follow-mode
2619 ;; show context in other window
2620 (condition-case nil
2621 (reftex-toc-visit-line)
2622 ('error t)))))
2623
2624(defun reftex-toc-toggle-follow ()
2625 "Toggle toc-follow mode.
2626(it is not really a mode, just a flag)."
2627 (interactive)
2628 (setq reftex-toc-follow-mode (not reftex-toc-follow-mode)))
2629(defun reftex-toc-view-line ()
2630 "View document location in other window."
2631 (interactive)
2632 (reftex-toc-visit-line))
2633(defun reftex-toc-goto-line-and-hide ()
2634 "Go to document location in other window. Hide the *toc* window."
2635 (interactive)
2636 (reftex-toc-visit-line 'hide))
2637(defun reftex-toc-quit ()
2638 "Hide the *toc* window and do not move point."
2639 (interactive)
2640 (delete-window)
2641 (switch-to-buffer (marker-buffer reftex-toc-return-marker))
2642 (goto-char (marker-position reftex-toc-return-marker)))
2643(defun reftex-toc-quit-and-kill ()
2644 "Kill the *toc* buffer."
2645 (interactive)
2646 (kill-buffer "*toc*")
2647 (delete-window)
2648 (switch-to-buffer (marker-buffer reftex-toc-return-marker))
2649 (goto-char (marker-position reftex-toc-return-marker)))
2650(defun reftex-toc-redo ()
2651 "Regenerate the *toc* buffer. Call only from within the *toc* buffer"
2652 (interactive)
2653 (switch-to-buffer (reftex-get-file-buffer-force reftex-last-toc-file))
2654 (delete-other-windows)
2655 (setq current-prefix-arg '(4))
2656 (reftex-toc))
2657
2658(defun reftex-toc-visit-line (&optional final)
2659 ;; Visit the tex file corresponding to the toc entry on the current line.
2660 ;; If FINAL is t, stay there
2661 ;; If FINAL is 'hide, hide the *toc* window.
2662 ;; Otherwise, move cursor back into *toc* window
2663
2664 (let (file find beg end (toc-window (selected-window)) show-window)
2665 (save-excursion
2666 (beginning-of-line)
2667 (setq beg (point))
2668 (end-of-line)
2669 (setq end (point)))
2670
2671 ;; get the file and the search string
2672 (setq file (get-text-property (point) 'file))
2673 (setq find (get-text-property (point) 'find))
2674 (if (or (not file) (not find))
2675 (error "Cannot visit line"))
2676
2677 (switch-to-buffer-other-window (reftex-get-file-buffer-force file))
2678 (setq show-window (selected-window))
2679 (goto-char (point-min))
2680
2681 (if (not (re-search-forward find nil t))
2682 (error "Cannot visit line"))
2683
2684 (setq beg (match-beginning 0)
2685 end (match-end 0))
2686
2687 (goto-char beg)
2688 (recenter 1)
2689 (reftex-highlight 0 beg end (current-buffer))
2690
2691 (select-window toc-window)
2692
2693 ;; use the `final' parameter to decide what to do next
2694 (cond
2695 ((equal final t)
2696 (reftex-unhighlight 0)
2697 (select-window show-window))
2698 ((eq final 'hide)
2699 (reftex-unhighlight 0)
2700 (delete-window))
2701 (t nil))))
2702
2703;;; ===========================================================================
2704;;;
2705;;; BibTeX citations.
2706
2707;; Variables and constants
2708
025bb635
RS
2709;; Define variable to silence compiler warnings
2710(defvar reftex-found-list)
2711
a7ec1775
RS
2712;; Internal variable, but used from different functions
2713(defvar reftex-cite-format1 nil)
2714
2715;; The history list of regular expressions used for citations
2716(defvar reftex-cite-regexp-hist nil)
2717
2718;; Help string for citation selection
2719(defconst reftex-citation-help
2720 "AVAILABLE KEYS IN MAKE CITATION MENU
2721---------------------------------------
2722 n / p Go to next/previous entry (Cursor motion works as well)
2723 r restrict selection with another regexp
2724 SPACE Show full database entry in other window
2725 f Toggle follow mode: Other window will follow with full db entry
2726 q Quit without inserting \\cite macro into buffer
2727 ? Display this help message
2728 C-r Recursive edit into other window
2729 RETURN ... Accept current entry and insert in format according to
2730 reftex-cite-format")
2731
2732(defconst reftex-cite-format-default "\\cite{KEY}"
2733 "The default value for reftex-cite-format.
2734Uses the string version of scitex-cite-format.")
2735
2736(defconst reftex-cite-format-1-author-simple
2737 '( "\\cite{KEY}" "AUTHOR \\cite{KEY}" "AUTHOR {\it et al.} \\cite{KEY}")
2738 "Value for reftex-cite format establishing a simple citation with name
2739of the first author.
2740Uses the list version of reftex-cite-format.")
2741
2742(defconst reftex-cite-format-2-authors
2743 '((?\C-m
2744 . ( "\\cite{KEY}" "AUTHOR \\cite{KEY}"
2745 "AUTHOR \\& AUTHOR \\cite{KEY}" "AUTHOR \\etal{} \\cite{KEY}"))
2746 (?\,
2747 . ("\\cite{KEY}" "AUTHOR, \\cite{KEY}"
2748 "AUTHOR \\& AUTHOR, \\cite{KEY}" "AUTHOR \\etal{}, \\cite{KEY}"))
2749 (?\;
2750 . ("\\cite{KEY}" "AUTHOR; \\cite{KEY}"
2751 "AUTHOR \\& AUTHOR; \\cite{KEY}" "AUTHOR \\etal{}; \\cite{KEY}"))
2752 (?\:
2753 . ("\\cite{KEY}" "AUTHOR: \\cite{KEY}"
2754 "AUTHOR \\& AUTHOR: \\cite{KEY}" "AUTHOR \\etal{}: \\cite{KEY}"))
2755 (?\(
2756 . ("(\\cite{KEY})" "AUTHOR (\\cite{KEY})"
2757 "AUTHOR \\& AUTHOR (\\cite{KEY})" "AUTHOR \\etal{} (\\cite{KEY})"))
2758 (?\[
2759 . ("[\\cite{KEY}]" "AUTHOR [\\cite{KEY}]"
2760 "AUTHOR \\& AUTHOR [\\cite{KEY}]" "AUTHOR \\etal{} [\\cite{KEY}]")))
2761 "Value for reftex-cite-format that estabishes an Author/Year citation
2762where the year is supplied from BibTeX. Depending on which character
2763is used during selection to accept the label, an extra ,;: or pair of
2764parenthesis will be inserted.
2765Uses the list-of-cons-cells version of reftex-cite-format.")
2766
2767;; Find bibtex files
2768
2769(defun reftex-get-bibfile-list ()
2770 ;; Return list of bibfiles for current document
2771
2772 ;; Ensure access to scanning info
2773 (reftex-access-scan-info)
2774
2775 (or (symbol-value reftex-bibfile-list-symbol)
2776 (error "No BibTeX files to parse. Add \\bibliography statment to document and reparse.")))
2777
2778(defun reftex-scan-buffer-for-bibliography-statement (bib-list-symbol)
2779 ;; Scan buffer for bibliography macro and store file list in bib-list-symbol.
2780 (let (file-list dir-list)
2781 (setq dir-list
2782 (reftex-split
2783 (concat path-separator "+")
2784 (mapconcat '(lambda(x)
2785 (if (getenv x) (getenv x) ""))
2786 reftex-bibpath-environment-variables
2787 path-separator)))
2788 (goto-char (point-min))
2789 (if (re-search-forward "^[ \t]*\\\\bibliography{[ \t]*\\([^}]+\\)" nil t)
2790 (progn
2791 (setq dir-list
2792 (cons (file-name-directory
2793 (get-text-property (match-beginning 0) 'file))
2794 dir-list))
2795 (setq file-list
2796 (mapcar '(lambda (x) (concat x ".bib"))
2797 (reftex-delete-list
2798 reftex-bibfile-ignore-list
2799 (reftex-split
2800 "[ \t\n]*,[ \t\n]*"
2801 (reftex-no-props (match-string 1)))))))
2802 (message "No \\bibliography command in document."))
2803 (set bib-list-symbol
2804 (if file-list
2805 (reftex-find-files-on-path file-list dir-list
fba437e6 2806 "While parsing \\bibliography:")
a7ec1775
RS
2807 nil))))
2808
2809(defun reftex-find-files-on-path (file-list path-list &optional error-string)
2810 ;; Search for all files in FILE-LIST on the PATH-LIST. Return absolute names.
2811 ;; A missing file throws an exception with the error message ERROR-STRING.
2812 (let (found-list found file)
2813 (while file-list
2814 (setq file (car file-list)
2815 file-list (cdr file-list)
2816 found nil)
2817 (if (file-name-absolute-p file)
2818 (setq found (expand-file-name file))
2819 (let ((dirs path-list))
2820 (while (and dirs (not found))
2821 (if (and (not (string= (car dirs) ""))
2822 (file-exists-p (expand-file-name file (car dirs))))
2823 (setq found (expand-file-name file (car dirs))))
2824 (setq dirs (cdr dirs)))))
2825 (if (and found
2826 (file-exists-p found))
2827 (add-to-list 'found-list (expand-file-name found))
2828 (error "%s No such file %s."
2829 (or error-string "") file)))
2830 (nreverse found-list)))
2831
2832;; Find a certain reference in any of the BibTeX files.
2833
2834(defun reftex-pop-to-bibtex-entry (key file-list
2835 &optional mark-to-kill highlight)
2836 ;; Find BibTeX KEY in any file in FILE-LIST in another window.
2837 ;; If mark-to-kill is non-nil, mark new buffer to kill."
2838
2839 (let* ((re (concat "@[a-zA-Z]+[ \t\n\r]*{[ \t\n\r]*" (regexp-quote key) "[ \t\n\r,]"))
2840 (window-conf (current-window-configuration))
2841 file buf)
2842 (catch 'exit
2843 (switch-to-buffer-other-window (current-buffer))
2844 (while file-list
2845 (setq file (car file-list)
2846 file-list (cdr file-list))
2847 (if (not (setq buf (reftex-get-file-buffer-force file mark-to-kill)))
2848 (error "No such file %s" file))
2849 (switch-to-buffer buf)
2850 (widen)
2851 (goto-char 0)
2852 (if (re-search-forward re nil t)
2853 (progn
2854 (goto-char (match-beginning 0))
2855 (recenter 0)
2856 (if highlight
2857 (reftex-highlight 0 (match-beginning 0) (match-end 0)))
2858 (throw 'exit (selected-window)))))
2859 (set-window-configuration window-conf)
2860 (beep)
2861 (message "No BibTeX entry with citation key %s" key))))
2862
2863;; Parse bibtex buffers
2864
2865(defun reftex-extract-bib-entries (buffers &optional get-word)
2866 ;; Extract bib entries which match regexps from BUFFERS.
2867 ;; BUFFERS is a list of buffers or file names.
2868 ;; Return list with entries."
2869 (let* (re-list first-re rest-re
2870 ;; avoid fontification of lookup buffers
2871 (lazy-lock-minimum-size 1)
2872 (buffer-list (if (listp buffers) buffers (list buffers)))
2873 found-list entry buffer1 buffer alist
2874 key-point start-point end-point)
2875
2876 (setq re-list (reftex-split "[ \t]*&&[ \t]*"
2877 (read-string "RegExp [ && RegExp...]: "
2878 nil 'reftex-cite-regexp-hist)))
2879
2880 (setq first-re (car re-list) ; We'll use the first re to find things,
2881 rest-re (cdr re-list)) ; the other to narrow down.
2882 (if (string-match "\\`[ \t]*\\'" first-re)
2883 (error "Empty regular expression"))
2884
2885 (save-excursion
2886 (save-window-excursion
2887
2888 ;; walk through all bibtex files
2889 (while buffer-list
2890 (setq buffer (car buffer-list)
2891 buffer-list (cdr buffer-list))
2892 (if (and (bufferp buffer)
2893 (buffer-live-p buffer))
2894 (setq buffer1 buffer)
2895 (setq buffer1 (reftex-get-file-buffer-force
2896 buffer (not reftex-keep-temporary-buffers))))
2897 (if (not buffer1)
2898 (error "Cannot find BibTeX file %s" buffer)
2899 (message "Scanning bibliography database %s" buffer1))
2900
2901 (set-buffer buffer1)
2902 (save-excursion
2903 (goto-char (point-min))
2904 (while (re-search-forward first-re nil t)
2905 (catch 'search-again
2906 (setq key-point (point))
2907 (if (not (re-search-backward
2908 "^[ \t]*@\\([a-zA-Z]+\\)[ \t\n\r]*{" nil t))
2909 (throw 'search-again nil))
2910 (setq start-point (point))
2911 (goto-char (match-end 0))
2912 (condition-case nil
2913 (up-list 1)
2914 ('error (goto-char key-point)
2915 (throw 'search-again nil)))
2916 (setq end-point (point))
2917
2918 ;; Ignore @string, @comment and @c entries or things
2919 ;; outside entries
2920 (if (or (string= (downcase (match-string 1)) "string")
2921 (string= (downcase (match-string 1)) "comment")
2922 (string= (downcase (match-string 1)) "c")
2923 (< (point) key-point)) ; this means match not in {}
2924 (progn
2925 (goto-char key-point)
2926 (throw 'search-again nil)))
2927
2928 ;; Well, we have got a match
2929 (setq entry (concat
2930 (buffer-substring start-point (point)) "\n"))
2931
2932 ;; Check if other regexp match as well
2933 (setq re-list rest-re)
2934 (while re-list
2935 (if (not (string-match (car re-list) entry))
2936 ;; nope - move on
2937 (throw 'search-again nil))
2938 (setq re-list (cdr re-list)))
2939
2940 (setq alist (reftex-parse-bibtex-entry
2941 nil start-point end-point))
2942 (setq alist (cons (cons "&entry" entry) alist))
2943
2944 ;; check for crossref entries
2945 (if (assoc "crossref" alist)
2946 (setq alist
2947 (append
2948 alist (reftex-get-crossref-alist alist))))
2949
2950 ;; format the entry
2951 (setq alist
2952 (cons
2953 (cons "&formatted"
2954 (reftex-format-bib-entry alist))
2955 alist))
2956
2957 ;; add it to the list
2958 (setq found-list (cons alist found-list)))))
2959 (reftex-kill-temporary-buffers))))
2960 (setq found-list (nreverse found-list))
2961
2962 ;; Sorting
2963 (cond
2964 ((eq 'author reftex-sort-bibtex-matches)
2965 (sort found-list 'reftex-bib-sort-author))
2966 ((eq 'year reftex-sort-bibtex-matches)
2967 (sort found-list 'reftex-bib-sort-year))
2968 ((eq 'reverse-year reftex-sort-bibtex-matches)
2969 (sort found-list 'reftex-bib-sort-year-reverse))
2970 (t found-list))))
2971
2972(defun reftex-bib-sort-author (e1 e2)
2973 (let ((al1 (reftex-get-bib-authors e1)) (al2 (reftex-get-bib-authors e2)))
2974 (while (and al1 al2 (string= (car al1) (car al2)))
2975 (setq al1 (cdr al1)
2976 al2 (cdr al2)))
2977 (if (and (stringp (car al1))
2978 (stringp (car al2)))
2979 (string< (car al1) (car al2))
2980 (not (stringp (car al1))))))
2981
2982(defun reftex-bib-sort-year (e1 e2)
2983 (< (string-to-int (cdr (assoc "year" e1)))
2984 (string-to-int (cdr (assoc "year" e2)))))
2985
2986(defun reftex-bib-sort-year-reverse (e1 e2)
2987 (> (string-to-int (or (cdr (assoc "year" e1)) "0"))
2988 (string-to-int (or (cdr (assoc "year" e2)) "0"))))
2989
2990(defun reftex-get-crossref-alist (entry)
2991 ;; return the alist from a crossref entry
2992 (let ((crkey (cdr (assoc "crossref" entry)))
2993 start)
2994 (save-excursion
2995 (save-restriction
2996 (widen)
2997 (if (re-search-forward
2998 (concat "@\\w+{[ \t\n\r]*" (regexp-quote crkey) "[ \t\n\r]*,") nil t)
2999 (progn
3000 (setq start (match-beginning 0))
3001 (condition-case nil
3002 (up-list 1)
3003 ('error nil))
3004 (reftex-parse-bibtex-entry nil start (point)))
3005 nil)))))
3006
3007;; Parse and format individual entries
3008
3009(defun reftex-get-bib-authors (entry)
3010 ;; Return a list with the author names in ENTRY
3011 (let (authors)
3012 (setq authors (reftex-get-bib-field "author" entry))
3013 (if (equal "" authors)
3014 (setq authors (reftex-get-bib-field "editor" entry)))
3015 (while (string-match "\\band\\b[ \t]*" authors)
3016 (setq authors (replace-match "\n" nil t authors)))
3017 (while (string-match "[\\.a-zA-Z\\-]+\\.[ \t]*\\|,.*\\|[{}]+" authors)
3018 (setq authors (replace-match "" nil t authors)))
3019 (while (string-match "^[ \t]+\\|[ \t]+$" authors)
3020 (setq authors (replace-match "" nil t authors)))
3021 (while (string-match "[ \t][ \t]+" authors)
3022 (setq authors (replace-match " " nil t authors)))
3023 (reftex-split "\n" authors)))
3024
3025(defun reftex-parse-bibtex-entry (entry &optional from to)
3026 (let (alist key start field)
3027 (save-excursion
3028 (save-restriction
3029 (if entry
3030 (progn
3031 (switch-to-buffer "*RefTeX-scratch*")
3032 (fundamental-mode)
3033 (erase-buffer)
3034 (insert entry))
3035 (widen)
3036 (narrow-to-region from to))
3037 (goto-char (point-min))
3038
3039 (if (re-search-forward
3040 "@\\(\\w+\\)[ \t\n\r]*{[ \t\n\r]*\\([^ \t\n\r,]+\\)" nil t)
3041 (setq alist
3042 (list
3043 (cons "&type" (downcase (reftex-no-props (match-string 1))))
3044 (cons "&key" (reftex-no-props (match-string 2))))))
3045 (while (re-search-forward "\\(\\w+\\)[ \t\n\r]*=[ \t\n\r]*" nil t)
3046 (setq key (reftex-no-props (downcase (match-string 1))))
3047 (cond
3048 ((= (following-char) ?{)
3049 (forward-char 1)
3050 (setq start (point))
3051 (condition-case nil
3052 (up-list 1)
3053 ('error nil)))
3054 ((= (following-char) ?\")
3055 (forward-char 1)
3056 (setq start (point))
3057 (while (and (search-forward "\"" nil t)
3058 (= ?\\ (char-after (- (point) 2))))))
3059 (t
3060 (setq start (point))
3061 (re-search-forward "[ \t\n\r,}]" nil 1)))
3062 (setq field (buffer-substring-no-properties start (1- (point))))
3063 ;; remove extra whitesp
3064 (while (string-match "[\n\t\r]\\|[ \t][ \t]+" field)
3065 (setq field (replace-match " " nil t field)))
3066 ;; remove leading garbage
3067 (if (string-match "^[ \t{]+" field)
3068 (setq field (replace-match "" nil t field)))
3069 ;; remove trailing garbage
3070 (if (string-match "[ \t}]+$" field)
3071 (setq field (replace-match "" nil t field)))
3072 (setq alist (cons (cons key field) alist)))
3073 alist))))
3074
3075(defun reftex-get-bib-field (fieldname entry)
3076 ;; Extract the field FIELDNAME from an ENTRY
3077 (or (cdr (assoc fieldname entry))
3078 ""))
3079
3080(defun reftex-format-bib-entry (entry)
3081 ;; Format a BibTeX ENTRY so that it is nice to look at
3082 (let*
3083 ((rtn nil)
3084 (auth-list (reftex-get-bib-authors entry))
3085 (authors (mapconcat '(lambda (x) x) auth-list ", "))
3086 (year (reftex-get-bib-field "year" entry))
3087 (title (reftex-get-bib-field "title" entry))
3088 (type (reftex-get-bib-field "&type" entry))
3089 (key (reftex-get-bib-field "&key" entry))
3090 (extra
3091 (cond
3092 ((equal type "article")
3093 (concat (reftex-get-bib-field "journal" entry) " "
3094 (reftex-get-bib-field "volume" entry) ", "
3095 (reftex-get-bib-field "pages" entry)))
3096 ((equal type "book")
3097 (concat "book (" (reftex-get-bib-field "publisher" entry) ")"))
3098 ((equal type "phdthesis")
3099 (concat "PhD: " (reftex-get-bib-field "school" entry)))
3100 ((equal type "mastersthesis")
3101 (concat "Master: " (reftex-get-bib-field "school" entry)))
3102 ((equal type "inbook")
3103 (concat "Chap: " (reftex-get-bib-field "chapter" entry)
3104 ", pp. " (reftex-get-bib-field "pages" entry)))
3105 ((or (equal type "conference")
3106 (equal type "incollection")
3107 (equal type "inproceedings"))
3108 (concat "in: " (reftex-get-bib-field "booktitle" entry)))
3109 (t ""))))
3110 (setq authors
3111 (if (> (length authors) 30)
3112 (concat (substring authors 0 27) "...")
3113 (format "%-30s" authors))
3114 title
3115 (if (> (length title) 70)
3116 (concat (substring title 0 67) "...")
3117 (format "%-70s" title))
3118 extra
3119 (if (> (length extra) 40)
3120 (concat (substring extra 0 37) "...")
3121 (format "%-40s" extra)))
3122 (if (reftex-use-fonts)
3123 (progn
3124 (put-text-property 0 (length authors) 'face 'font-lock-keyword-face
3125 authors)
3126 (put-text-property 0 (length title) 'face 'font-lock-comment-face
3127 title)
3128 (put-text-property 0 (length extra) 'face 'font-lock-reference-face
3129 extra)))
3130 (setq rtn (concat key "\n " authors " " year " " extra
3131 "\n " title "\n\n"))
3132 rtn))
3133
3134;; Make a citation
3135
3136(defun reftex-citation (&optional arg no-insert)
3137 "Make a citation unsing BibTeX database files.
3138After asking for a Regular Expression, it scans the buffers with
3139bibtex entries (taken from the \\bibliography command) and offers the
3140matching entries for selection. The selected entry is formated according
3141to reftex-cite-format and inserted into the buffer.
3142If NO-INSERT is non-nil, nothing is inserted, only the selected key returned.
3143The regular expression uses an expanded syntax: && is interpreted as 'and'.
3144Thus, aaaa&&bbb matches entries which contain both aaaa and bbb.
fba437e6 3145When this function is called with point inside the braces of a \\cite
a7ec1775
RS
3146command, it will add another key, ignoring the value of reftex-cite-format.
3147When called with a numeric prefix, that many citations will be made and all
fba437e6 3148put into the same \\cite command.
a7ec1775
RS
3149When called with just C-u as prefix, enforces rescan of buffer for
3150bibliography statement (e.g. if it was changed)."
3151
3152 (interactive "P")
3153
3154 ;; check for recursive edit
3155 (reftex-check-recursive-edit)
3156
3157 ;; if there is just 1 C-u prefix arg, force to rescan buffer
3158 (if (and current-prefix-arg
3159 (listp current-prefix-arg)
3160 (= 4 (prefix-numeric-value arg)))
3161 (reftex-reset-scanning-information))
3162
3163 ;; check if there is already a cite command at point and change cite format
3164 ;; in order to only add another reference in the same cite command.
3165 (let ((pos (point)))
3166 (search-backward "\\" (point-min) 1)
3167 (if (and (looking-at "\\\\[a-zA-Z]*cite\\*?\\(\\[[^]]*\\]\\)*{\\([^}]*\\)")
3168 (>= (match-end 0) pos)
3169 (>= pos (match-beginning 2)))
3170 (progn
3171 (goto-char pos)
3172 (cond
3173 ((or (not arg)
3174 (not (listp arg)))
3175 (setq reftex-cite-format1
3176 (concat
3177 (if (not (or (= (preceding-char) ?{)
3178 (= (preceding-char) ?,)))
3179 ","
3180 "")
3181 "KEY"
3182 (if (not (or (= (following-char) ?})
3183 (= (following-char) ?,)))
3184 ","
3185 ""))))
3186 (t
3187 (setq reftex-cite-format1 "KEY"))))
3188 (setq reftex-cite-format1
3189 (if (symbolp reftex-cite-format)
3190 (symbol-value reftex-cite-format)
3191 reftex-cite-format))
3192 (goto-char pos)))
3193
3194 (let* (key entry cnt rtn ins-string re-list re
3195 ;; scan bibtex files
3196 (lazy-lock-minimum-size 1)
025bb635
RS
3197 (reftex-found-list (reftex-extract-bib-entries
3198 (reftex-get-bibfile-list)))
a7ec1775
RS
3199 (found-list-r nil)
3200 (accept-keys
3201 (if (and (listp reftex-cite-format1)
3202 (listp (car reftex-cite-format1)))
3203 (mapcar 'car reftex-cite-format1)
3204 '(?\C-m))))
025bb635 3205 (if (not reftex-found-list)
a7ec1775
RS
3206 (error "Sorry, no matches found"))
3207
3208 ;; remember where we came from
3209 (setq reftex-call-back-to-this-buffer (current-buffer))
3210
3211 ;; offer selection
3212 (save-window-excursion
3213 (switch-to-buffer-other-window "*RefTeX Select*")
3214 (erase-buffer)
3215 (mapcar '(lambda (x) (insert (cdr (assoc "&formatted" x))))
025bb635 3216 reftex-found-list)
a7ec1775
RS
3217 (if (= 0 (buffer-size))
3218 (error "Sorry, no matches found"))
3219 (setq truncate-lines t)
3220 (goto-char 1)
3221 (if (catch 'exit
3222 (while t
3223 (setq rtn
3224 (reftex-select-item
3225 nil
3226 (concat
3227 "Select: [n]ext [p]rev [r]estrict [q]uit [?]Help ||"
3228 " RETURN "
3229 (condition-case nil
3230 (mapconcat 'char-to-string accept-keys " ")
3231 (error (error "Illegal reftex-cite-format"))))
3232 "^[^ \t\n]"
3233 "\n\n"
3234 4
3235 reftex-citation-help
3236 (cons ?r accept-keys)
3237 nil
3238 'reftex-bibtex-selection-callback nil))
3239 (setq key (car rtn)
3240 cnt (cdr rtn))
3241 (if (not key) (throw 'exit nil))
3242 (cond
3243 ((equal key ?r)
3244 ;; restrict with new regular expression
3245 (setq re-list
3246 (reftex-split "[ \t]*&&[ \t]*"
3247 (read-string "RegExp [ && RegExp...]: "
3248 nil 'reftex-cite-regexp-hist)))
3249 (while re-list
3250 (setq re (car re-list)
3251 re-list (cdr re-list))
3252 (setq found-list-r
3253 (delete ""
3254 (mapcar
3255 '(lambda (x)
3256 (if (string-match re
3257 (cdr (assoc "&entry" x)))
3258 x
3259 ""))
025bb635 3260 reftex-found-list))))
a7ec1775 3261 (if found-list-r
025bb635 3262 (setq reftex-found-list found-list-r)
a7ec1775
RS
3263 (ding))
3264 (erase-buffer)
3265 (mapcar '(lambda (x) (insert (cdr (assoc "&formatted" x))))
025bb635 3266 reftex-found-list)
a7ec1775
RS
3267 (goto-char 1))
3268 ((or (member key accept-keys)
3269 (equal key ?\C-m)
3270 (equal key 'return))
025bb635 3271 (setq entry (nth cnt reftex-found-list))
a7ec1775
RS
3272 (throw 'exit t))
3273 (t
3274 (ding)))))
3275 (progn
3276 ;; format the entry
3277 (if (not (integerp key)) (setq key ?\C-m))
3278 (setq ins-string (reftex-format-citation entry key)))
3279 (setq ins-string "")
3280 (message "Quit")))
3281 (kill-buffer "*RefTeX Select*")
3282
3283 (if (not no-insert)
3284 (insert ins-string))
3285 (message "")
3286
3287 ;; Check if the prefix arg was numeric, and call reftex-citation recursively
3288 (if (and (integerp arg)
3289 (> arg 1)
3290 (re-search-backward
3291 "\\\\[a-zA-Z]*cite\\*?\\(\\[[^]]*\\]\\)*{\\([^}]*\\)" nil t))
3292 (progn
3293 (goto-char (match-end 0))
3294 (setq arg (1- arg))
3295 (reftex-citation arg))
3296 (reftex-kill-temporary-buffers))
3297 ;; Return the citation key
3298 (reftex-get-bib-field "&key" entry)))
3299
3300(defun reftex-format-citation (entry key)
3301 ;; Format a citation from the info in the BibTeX ENTRY
3302 (let* ((cite-key (reftex-get-bib-field "&key" entry))
3303 (year (reftex-get-bib-field "year" entry))
3304 (auth-list (reftex-get-bib-authors entry))
3305 (nauthors (length auth-list))
3306 format)
3307
3308 (save-excursion
3309 ;; Find the correct format
3310 (if (and (listp reftex-cite-format1)
3311 (listp (car reftex-cite-format1)))
3312 (if (integerp (car (car reftex-cite-format1)))
3313 (if (assoc key reftex-cite-format1)
3314 (setq format (cdr (assoc key reftex-cite-format1)))
3315 (if (or (equal key ?\C-m)
3316 (equal key 'return))
3317 (setq format (cdr (car reftex-cite-format1)))
3318 (error "Error in reftex-cite-format")))
3319 (error "Error in reftex-cite-format"))
3320 (setq format reftex-cite-format1))
3321
3322 (if (listp format)
3323 (let ((nn (min nauthors (1- (length format)))))
3324 (while (and (> nn 0) (string= "" (nth nn format)))
3325 (setq nn (1- nn)))
3326 (setq format (nth nn format))))
3327 (if (stringp format)
3328 (setq format format)
3329 (setq format "\\cite{KEY}"))
3330
3331 ;; Insert the author names
3332 (while (string-match "\\bAUTHOR\\b" format)
3333 (setq format (replace-match (car auth-list) t t format))
3334 (setq auth-list (cdr auth-list)))
3335 (while (string-match "\\bKEY\\b" format)
3336 (setq format (replace-match cite-key t t format)))
3337 (while (string-match "\\bYEAR\\b" format)
3338 (setq format (replace-match year t t format)))
3339 format)))
3340
3341;; this is slow and not recommended for follow mode
3342(defun reftex-bibtex-selection-callback (cnt)
3343 ;; Callback function to be called from the BibTeX selection, in
3344 ;; order to display context. This function is relatively slow and not
3345 ;; recommended for follow mode, just for individual lookups.
a7ec1775 3346 (let ((win (selected-window))
025bb635 3347 (key (reftex-get-bib-field "&key" (nth cnt reftex-found-list)))
a7ec1775
RS
3348 (bibfile-list (save-excursion
3349 (set-buffer reftex-call-back-to-this-buffer)
3350 (reftex-get-bibfile-list))))
3351 (reftex-pop-to-bibtex-entry key bibfile-list
3352 (not reftex-keep-temporary-buffers) t)
3353 (select-window win)))
3354
3355;;; ===========================================================================
3356;;;
3357;;; Here is the routine used for selection
3358
3359;; Marker for return point from recursive edit
3360(defvar reftex-recursive-edit-marker (make-marker))
3361
3362(defun reftex-check-recursive-edit ()
3363 ;; Check if we are already in a recursive edit. Abort with helpful
3364 ;; message if so.
3365 (if (marker-position reftex-recursive-edit-marker)
3366 (error
3367 (substitute-command-keys
3368 "In unfinished recursive edit. Finish (\\[exit-recursive-edit]) or abort (\\[abort-recursive-edit])."))))
3369
3370(defun reftex-select-item (buffer prompt next-re end-re size help-string
3371 event-list &optional offset
3372 call-back cb-flag)
3373;; Select an item from the buffer BUFFER. Show PROMPT to user, find
3374;; next item with NEXT-RE regular expression, return on any of the
3375;; events listed in EVENT-LIST. The function returns the event along
3376;; with an integer indicating which item was selected. When OFFSET is
3377;; specified, starts at that item in the list. When CALL-BACK is
3378;; given, it is a function which is called with the match of the
3379;; NEXT-RE match and the index of the element.
3380 (let* (key key-sq b e ev cnt cmd
3381 (offset1 (or offset 1)))
3382 (setq ev
3383 (catch 'exit
3384 (save-window-excursion
3385 (if buffer
3386 (switch-to-buffer-other-window buffer))
3387 (if (= 0 (buffer-size))
3388 (throw 'exit nil))
3389 (setq truncate-lines t)
3390 (goto-char 1)
3391 (if (not (re-search-forward next-re nil t offset1))
3392 (progn ; in case the offset is illegal
3393 (setq offset1 1)
3394 (if (not (re-search-forward next-re nil t offset1))
3395 (throw 'exit nil))))
3396 (beginning-of-line 1)
3397 (setq cnt (if offset1 (1- offset1) 0))
3398 (while t
3399 (if (and cb-flag call-back)
3400 (funcall call-back cnt))
3401 (setq b (point)
3402 e (save-excursion
3403 (save-match-data
3404 (re-search-forward end-re nil 1))
3405 (point)))
3406 (reftex-highlight 1 b e)
3407 (if (or (not (pos-visible-in-window-p b))
3408 (not (pos-visible-in-window-p e)))
3409 (recenter (/ (window-height) 2)))
3410 (setq key-sq (read-key-sequence prompt))
3411 (setq key (car
3412 (cond
3413 ((fboundp 'listify-key-sequence) ; Emacs
3414 (listify-key-sequence key-sq))
3415 ((fboundp 'event-to-character) ; XEmacs
3416 (mapcar 'event-to-character key-sq))
3417 (t (error "Please report this problem to dominik@strw.leidenuniv.nl")))))
3418
3419 (setq cmd (key-binding key-sq))
3420
3421 (reftex-unhighlight 0)
3422
3423 (cond
3424
3425 ((or (equal key ?n)
3426 (equal key ?\C-i)
3427 (equal cmd 'next-line))
3428 (if (re-search-forward next-re nil t 2)
3429 (setq cnt (1+ cnt)))
3430 (beginning-of-line 1))
3431
3432 ((equal cmd 'scroll-up)
3433 (setq cnt (1- cnt))
3434 (while (and (pos-visible-in-window-p)
3435 (re-search-forward next-re nil t))
3436 (setq cnt (1+ cnt)))
3437 (beginning-of-line 1)
3438 (recenter 1))
3439
3440 ((or (equal key ?p)
3441 (equal cmd 'previous-line))
3442 (if (re-search-backward next-re nil t)
3443 (setq cnt (1- cnt))))
3444
3445 ((equal cmd 'scroll-down)
3446 (while (and (pos-visible-in-window-p)
3447 (re-search-backward next-re nil t))
3448 (setq cnt (1- cnt)))
3449 (recenter (- (window-height) size 2)))
3450
3451 ((equal key ?q)
3452 (throw 'exit nil))
3453
3454 ((equal key ?\C-g)
3455 (bury-buffer)
3456 (error "Abort"))
3457
3458 ((or (equal key ?\C-m)
3459 (equal key 'return)
3460 (equal cmd 'newline))
3461 (throw 'exit 'return))
3462
3463 ((or (equal key ?C) ; backward compatibility
3464 (equal key ?f))
3465 (setq cb-flag (not cb-flag)))
3466
3467 ((equal key ?\ )
3468 (funcall call-back cnt))
3469
3470 ((equal key ?\?)
3471 (save-window-excursion
3472 (with-output-to-temp-buffer "*RefTeX Help*"
3473 (princ help-string))
3474 (setq unread-command-events
3475 (cons
3476 (cond
3477 ((fboundp 'read-event) ; Emacs
3478 (read-event))
3479 ((fboundp 'next-command-event) ; XEmacs
3480 (next-command-event))
3481 (t (error "Please report this problem to dominik@strw.leidenuniv.nl")))
3482 nil)))
3483 (kill-buffer "*RefTeX Help*"))
3484
3485 ((equal key ?\C-r)
3486 ;; sje - code copied from ispell.el for
3487 ;; performing recursive edit
3488 (set-marker reftex-recursive-edit-marker (point))
3489 (unwind-protect
3490 (progn
3491 (save-window-excursion
3492 (save-excursion
3493 (other-window 1)
3494 (message
3495 (substitute-command-keys
3496 "Recursive edit. Return to selection with \\[exit-recursive-edit]"))
3497 (recursive-edit)))
3498 (if (not (equal (marker-buffer
3499 reftex-recursive-edit-marker)
3500 (current-buffer)))
3501 (error
3502 "Cannot continue RefTeX from this buffer."))
3503 (goto-char reftex-recursive-edit-marker))
3504 (set-marker reftex-recursive-edit-marker nil)))
3505
3506 ((member key event-list)
3507 (throw 'exit key))
3508 (t
3509 (ding)))))))
3510 (message "")
3511 (cons ev cnt)))
3512
3513;;; ===========================================================================
3514;;;
3515;;; View cross references
3516
3517(defun reftex-view-crossref (&optional arg)
fba437e6
RS
3518 "View cross reference of \\ref or \\cite macro at point.
3519If the macro at point is a \\ref, show the corresponding label definition.
3520If it is a \\cite, show the BibTeX database entry.
a7ec1775
RS
3521If there is no such macro at point, search forward to find one.
3522When you call this function several times in direct successtion, point will
3523move to view subsequent cross references further down in the buffer.
3524With argument, actually select the window showing the cross reference."
3525
3526 (interactive "P")
3527
3528 ;; See where we are.
3529 (let* ((pos (point))
3530 (re "\\\\[a-z]*\\(cite\\|ref\\)\\(\\[[^{}]*\\]\\)?{\\([^}]+\\)}")
3531 (my-window (get-buffer-window (current-buffer)))
3532 pop-window cmd args macro label entry key-start point)
3533
3534 (if (save-excursion
3535 (forward-char 1)
3536 (and (search-backward "\\" nil t)
3537 (looking-at re)
3538 (< pos (match-end 0))))
3539 (setq macro (match-string 1)
3540 key-start (match-beginning 3)))
3541
3542 (if (and macro (eq last-command this-command))
3543 (if (and (string= macro "cite")
3544 (skip-chars-forward "^}, \t\n\r")
3545 (= (following-char) ?,))
3546 (setq key-start (1+ (point)))
3547 (setq macro nil)))
3548
3549 (if (not macro)
3550 (if (re-search-forward re nil t)
3551 (setq macro (match-string 1)
3552 key-start (match-beginning 3))
3553 (error "No further cross references in buffer")))
3554
3555 (goto-char key-start)
3556
3557 ;; Ensure access to scanning info
3558 (reftex-access-scan-info)
3559
3560 (cond
3561 ((string= macro "cite")
3562 (setq cmd 'reftex-pop-to-bibtex-entry
3563 args (list
3564 (reftex-no-props (reftex-this-word "^{},"))
3565 (reftex-get-bibfile-list) nil t)))
3566 ((string= macro "ref")
3567 (let ((label (reftex-no-props (reftex-this-word "^{}")))
3568 (entry (assoc label (symbol-value reftex-list-of-labels-symbol))))
3569 (if entry
3570 (setq cmd 'reftex-pop-to-label
3571 args (list label (list (nth 3 entry)) nil t))
3572 (error "Label %s not known - reparse document might help" label))))
3573 (t (error "This should not happen")))
3574 (setq point (point))
3575 (apply cmd args)
3576 (setq pop-window (selected-window))
3577 (add-hook 'pre-command-hook 'reftex-highlight-shall-die)
3578 (select-window my-window)
3579 (goto-char point)
3580 (and arg (select-window pop-window))))
3581
3582(defun reftex-mouse-view-crossref (ev)
fba437e6
RS
3583 "View cross reference of \\ref or \\cite macro where you click.
3584If the macro at point is a \\ref, show the corresponding label definition.
3585If it is a \\cite, show the BibTeX database entry.
a7ec1775 3586If there is no such macro at point, search forward to find one.
a7ec1775
RS
3587With argument, actually select the window showing the cross reference."
3588 (interactive "e")
3589 (mouse-set-point ev)
3590 (reftex-view-crossref current-prefix-arg))
3591
3592;;; ===========================================================================
3593;;;
3594;;; Functions that check out the surroundings
3595
3596(defun reftex-what-macro (which &optional bound)
3597 ;; Find out if point is within the arguments of any TeX-macro.
3598 ;; The return value is either (\"\\\\macro\" . (point)) or a list of them.
3599
3600 ;; If WHICH is nil, immediately return nil.
3601 ;; If WHICH is t, return list of all macros enclosing point.
3602 ;; If WHICH is a list of macros, look only for those macros and return the
3603 ;; name of the first macro in this list found to enclose point.
3604 ;; If the optional BOUND is an integer, bound backwards directed
3605 ;; searches to this point. If it is nil, limit to nearest \\section -
3606 ;; like statement.
3607
3608 ;; This function is pretty stable, but can be fooled if the text contains
3609 ;; things like \\macro{aa}{bb} where \\macro is defined to take only one
3610 ;; argument. As RefTeX cannot know this, the string \"bb\" would still be
3611 ;; considered an argument of macro \\macro.
3612
3613 (catch 'exit
3614 (if (null which) (throw 'exit nil))
3615 (let ((bound (or bound (save-excursion (re-search-backward
3616 reftex-section-regexp nil 1)
3617 (point))))
3618 pos cmd-list cmd)
3619 (save-restriction
3620 (save-excursion
3621 (narrow-to-region (max 1 bound) (point-max))
3622 ;; move back out of the current parenthesis
3623 (while (condition-case nil
3624 (progn (up-list -1) t)
3625 (error nil))
3626 ;; move back over any touching sexps
3627 (while (or (= (preceding-char) ?\])
3628 (= (preceding-char) ?\}))
3629 (backward-sexp))
3630 (setq pos (point))
3631 (if (and (or (= (following-char) ?\[)
3632 (= (following-char) ?\{))
3633 (and (re-search-backward "\\(\\\\[a-zA-Z]+\\)" nil t)
3634 (= (match-end 0) pos)))
3635 (progn
3636 (setq cmd (buffer-substring-no-properties
3637 (match-beginning 0) (match-end 0)))
3638 (if (eq t which)
3639 (setq cmd-list (cons (cons cmd (point)) cmd-list))
3640 (if (member cmd which)
3641 (throw 'exit (cons cmd (point)))))))
3642 (goto-char pos)))
3643 (nreverse cmd-list)))))
3644
3645(defun reftex-what-environment (which &optional bound)
3646 ;; Find out if point is inside a LaTeX environment.
3647 ;; The return value is (e.g.) either (\"equation\" . (point)) or a list of
3648 ;; them.
3649
3650 ;; If WHICH is nil, immediately return nil.
3651 ;; If WHICH is t, return list of all environments enclosing point.
3652 ;; If WHICH is a list of environments, look only for those environments and
3653 ;; return the name of the first environment in this list found to enclose
3654 ;; point.
3655
3656 ;; If the optional BOUND is an integer, bound backwards directed searches to
3657 ;; this point. If it is nil, limit to nearest \\section - like statement.
3658
3659 (catch 'exit
3660 (save-excursion
3661 (if (null which) (throw 'exit nil))
3662 (let ((bound (or bound (save-excursion (re-search-backward
3663 reftex-section-regexp nil 1)
3664 (point))))
3665 env-list end-list env)
3666 (while (re-search-backward "\\\\\\(begin\\|end\\){\\([^}]+\\)}"
3667 bound t)
3668 (setq env (buffer-substring-no-properties
3669 (match-beginning 2) (match-end 2)))
3670 (cond
3671 ((string= (match-string 1) "end")
3672 (add-to-list 'end-list env))
3673 ((member env end-list)
3674 (setq end-list (delete env end-list)))
3675 ((eq t which)
3676 (setq env-list (cons (cons env (point)) env-list)))
3677 ((member env which)
3678 (throw 'exit (cons env (point))))))
3679 (nreverse env-list)))))
3680
3681(defun reftex-word-before-point ()
3682 ;; Return the word before point. Word means here:
3683 ;; Consists of [a-zA-Z0-9.:] and ends at point or whitespace.
3684 (let ((pos (point)))
3685 (save-excursion
3686 (re-search-backward "[^ \t\n\r]" (point-min) 1)
3687 (setq pos (1+ (point)))
3688 (if (re-search-backward "[^a-zA-Z0-9\\\.:]" (point-min) 1)
3689 (forward-char 1))
3690 (buffer-substring-no-properties (point) pos))))
3691
3692;; ============================================================================
3693;;
3694;; Some generally useful functions
3695
3696(defun reftex-no-props (string)
3697 ;; Return STRING with all text properties removed
3698 (and (stringp string)
3699 (set-text-properties 0 (length string) nil string))
3700 string)
3701
3702(defun reftex-split (regexp string)
3703 ;; Split like perl
3704 (let ((start 0) list)
3705 (while (string-match regexp string start)
3706 (setq list (cons (substring string start (match-beginning 0)) list))
3707 (setq start (match-end 0)))
3708 (setq list (nreverse (cons (substring string start) list)))))
3709
3710(defun reftex-allow-for-ctrl-m (string)
3711 ;; convert STRING into a regexp, allowing ^M for \n
3712 (let ((start -2))
3713 (setq string (regexp-quote string))
3714 (while (setq start (string-match "[\n\r]" string (+ 3 start)))
3715 (setq string (replace-match "[\n\r]" nil t string)))
3716 string))
3717
3718(defun reftex-delete-list (elt-list list)
3719 ;; like delete, but with a list of things to delete
3720 ;; (original code from Rory Molinari)
3721 (while elt-list
3722 (setq list (delete (car elt-list) list)
3723 elt-list (cdr elt-list)))
3724 list)
3725
3726(defun reftex-get-buffer-visiting (file)
3727 ;; return a buffer visiting FILE
3728 (cond
3729 ((fboundp 'find-buffer-visiting) ; Emacs
3730 (find-buffer-visiting file))
3731 ((boundp 'find-file-compare-truenames) ; XEmacs
3732 (let ((find-file-compare-truenames t))
3733 (get-file-buffer file)))
3734 (t (error "Please report this problem to dominik@strw.leidenuniv.nl"))))
3735
3736(defun reftex-get-file-buffer-force (file &optional mark-to-kill)
3737 ;; Return a buffer visiting file. Make one, if necessary.
3738 ;; If neither such a buffer no the file exist, return nil.
3739 ;; If MARK-TO-KILL in non-nil, put any new buffers into the kill list."
3740
3741 (let ((buf (reftex-get-buffer-visiting file)))
3742 (cond
3743 (buf buf)
3744 ((file-exists-p file)
3745 (setq buf (find-file-noselect file))
3746 (if mark-to-kill
3747 (add-to-list 'reftex-buffers-to-kill buf))
3748 buf)
3749 (t nil))))
3750
3751(defun reftex-splice-symbols-into-list (list alist)
3752 ;; Splice the association in ALIST of any symbols in LIST into the list.
3753 ;; Return new list.
3754 (let (rtn tmp)
3755 (while list
3756 (while (and (not (null (car list)))
3757 (symbolp (car list)))
3758 (setq tmp (car list))
3759 (cond
3760 ((assoc tmp alist)
3761 (setq list (append (cdr (cdr (assoc tmp alist))) (cdr list))))
3762 (t
3763 (error "Cannot treat symbol %s in reftex-label-alist"
3764 (symbol-name tmp)))))
3765 (setq rtn (cons (car list) rtn)
3766 list (cdr list)))
3767 (nreverse rtn)))
3768
3769(defun reftex-uniquify (alist &optional keep-list)
3770 ;; Return a list of all elements in ALIST, but each car only once
3771 ;; Elements of KEEP-LIST are not removed even if duplicate
3772 (let (new elm)
3773 (while alist
3774 (setq elm (car alist)
3775 alist (cdr alist))
3776 (if (or (member (car elm) keep-list)
3777 (not (assoc (car elm) new)))
3778 (setq new (cons elm new))))
3779 (setq new (nreverse new))
3780 new))
3781
3782(defun reftex-use-fonts ()
3783 ;; Return t if we can and want to use fonts
3784 (and window-system
3785 reftex-use-fonts
3786 (boundp 'font-lock-keyword-face)))
3787
3788;; Highlighting uses overlays. If this is for XEmacs, we need to load
3789;; the overlay library, available in version 19.15
3790(and (not (fboundp 'make-overlay))
3791 (condition-case nil
3792 (require 'overlay)
3793 ('error
3794 (error "RefTeX needs overlay emulation (available in XEmacs 19.15)"))))
3795
3796;; We keep a vector with several different overlays to do our highlighting.
3797(defvar reftex-highlight-overlays [nil nil])
3798
3799;; Initialize the overlays
3800(aset reftex-highlight-overlays 0 (make-overlay 1 1))
3801(overlay-put (aref reftex-highlight-overlays 0) 'face 'highlight)
3802(aset reftex-highlight-overlays 1 (make-overlay 1 1))
3803(overlay-put (aref reftex-highlight-overlays 1) 'face 'highlight)
3804
3805;; Two functions for activating and deactivation highlight overlays
3806(defun reftex-highlight (index begin end &optional buffer)
3807 "Highlight a region with overlay INDEX."
3808 (move-overlay (aref reftex-highlight-overlays index)
3809 begin end (or buffer (current-buffer))))
3810(defun reftex-unhighlight (index)
3811 "Detatch overlay INDEX."
3812 (delete-overlay (aref reftex-highlight-overlays index)))
3813
3814(defun reftex-highlight-shall-die ()
3815 ;; Function used in pre-command-hook to remove highlights
3816 (remove-hook 'pre-command-hook 'reftex-highlight-shall-die)
3817 (reftex-unhighlight 0))
3818
3819;;; ---------------------------------------------------------------------------
3820;;;
3821;;; Cursor position after insertion of forms
3822
3823(defun reftex-position-cursor ()
3824 ;; Search back to question mark, delete it, leave point there
3825 (if (search-backward "\?" (- (point) 100) t)
3826 (delete-char 1)))
3827
3828(defun reftex-item ()
3829 "Insert an \\item and provide a label if the environments supports that."
3830 (interactive)
3831 (let ((env (car
3832 (reftex-what-environment '("itemize" "enumerate" "eqnarray")))))
3833
3834 (if (and env (not (bolp))) (newline))
3835
3836 (cond
3837
3838 ((string= env "eqnarray")
3839 (if (not (bolp))
3840 (newline))
3841 (reftex-label env)
3842 (insert "\n & & ")
3843 (beginning-of-line 1))
3844
3845 ((string= env "itemize")
3846 (newline)
3847 (insert "\\item "))
3848
3849 ((string= env "enumerate")
3850 (newline)
3851 (insert "\\item")
3852 (reftex-label env)
3853 (insert " "))
3854 (t
3855 (error "\\item command does not make sense here...")))))
3856
3857;;; ---------------------------------------------------------------------------
3858;;; ---------------------------------------------------------------------------
3859;;; ---------------------------------------------------------------------------
3860;;;
3861;;; Data Section: Definition of large constants
3862
3863
3864(defconst reftex-label-alist-builtin
3865 '(
3866 (LaTeX
3867 "LaTeX default environments"
3868 ("section" ?s "sec:" "~\\ref{%s}" t
3869 ("Part" "Chapter" "Chap." "Section" "Sec." "Sect." "Paragraph" "Par."
3870 "\\S" "Teil" "Kapitel" "Kap." "Abschnitt" ))
3871
3872 ("enumerate" ?n "item:" "~\\ref{%s}" "\\\\item\\(\\[[^]]*\\]\\)?"
3873 ("Item" "Punkt"))
3874
3875 ("equation" ?e "eq:" "~(\\ref{%s})" t
3876 ("Equation" "Eq." "Eqn." "Gleichung" "Gl."))
3877 ("eqnarray" ?e "eq:" nil "\\\\begin{eqnarray}\\|\\\\\\\\")
3878
3879 ("figure" ?f "fig:" "~\\ref{%s}" "\\\\caption\\(\\[[^]]*\\]\\)?{"
3880 ("Figure" "Fig." "Abbildung" "Abb."))
3881 ("figure*" ?f nil nil "\\\\caption\\(\\[[^]]*\\]\\)?{")
3882
3883 ("table" ?t "tab:" "~\\ref{%s}" "\\\\caption\\(\\[[^]]*\\]\\)?{"
3884 ("Table" "Tab." "Tabelle"))
3885 ("table*" ?t nil nil "\\\\caption\\(\\[[^]]*\\]\\)?{")
3886
3887 ("any" ?\ " " "\\ref{%s}" nil))
3888
3889 (Sideways
3890 "Sidewaysfigure and sidewaystable"
3891 ("sidewaysfigure" ?f nil nil "\\\\caption\\(\\[[^]]*\\]\\)?{")
3892 ("sidewaystable" ?t nil nil "\\\\caption\\(\\[[^]]*\\]\\)?{"))
3893
025bb635
RS
3894 (Subfigure
3895 "Subfigure environment and macro"
3896 ("subfigure" ?f nil nil "\\\\caption\\(\\[[^]]*\\]\\)?{")
3897 ("\\subfigure" ?f nil nil "\\\\subfigure[[{]"))
3898
a7ec1775
RS
3899 (AMSTeX
3900 "AMS-LaTeX: amsmath package environents"
3901 ("align" ?e "eq:" "~\\eqref{%s}" "\\\\begin{align}\\|\\\\\\\\")
3902 ("gather" ?e "eq:" nil "\\\\begin{gather}\\|\\\\\\\\")
3903 ("multline" ?e "eq:" nil t)
3904 ("flalign" ?e "eq:" nil "\\\\begin{flalign}\\|\\\\\\\\")
3905 ("alignat" ?e "eq:" nil "\\\\begin{alignat}{[0-9]*}\\|\\\\\\\\"))
3906
3907 (AASTeX
3908 "AAS deluxetable environment"
3909 ("deluxetable" ?t "tab:" nil "\\\\caption{")))
3910 "The default label environment descriptions.")
3911
3912;;; ---------------------------------------------------------------------------
3913;;;
3914;;; Functions to compile the tables, reset the mode etc.
3915
3916(defun reftex-reset-mode ()
3917 "Reset RefTeX Mode. Required to implement changes to some list variables.
3918This function will compile the information in reftex-label-alist and similar
3919variables. It is called when RefTeX is first used, and after changes to
3920these variables via reftex-add-to-label-alist."
3921 (interactive)
3922
025bb635 3923 ;; Record that we have done this
a7ec1775
RS
3924 (setq reftex-tables-dirty nil)
3925
025bb635
RS
3926 ;; Kill temporary buffers associated with RefTeX - just in case they
3927 ;; were not cleaned up properly
3928 (let ((buffer-list '("*reftex-master.tex*" "*RefTeX Help*" "*RefTeX Select*"
3929 "*Duplicate Labels*" "*toc*" "*RefTeX-scratch*")))
3930 (while buffer-list
3931 (if (get-buffer (car buffer-list))
3932 (kill-buffer (car buffer-list)))
3933 (setq buffer-list (cdr buffer-list))))
3934
a7ec1775
RS
3935 ;; To update buffer-local variables
3936 (hack-local-variables)
3937 (message "updating internal tables...")
3938 (reftex-compute-ref-cite-tables)
3939 (message "updating internal tables... done")
3940 (reftex-reset-scanning-information))
3941
3942(defun reftex-reset-scanning-information ()
3943 "Reset the symbols containing information from buffer scanning.
3944This enforces rescanning the buffer on next use."
3945 (if (and (string= reftex-last-toc-master (reftex-TeX-master-file))
3946 (get-buffer "*toc*"))
3947 (kill-buffer "*toc*"))
3948 (let ((symlist reftex-multifile-symbols)
3949 symbol)
3950 (while symlist
3951 (setq symbol (car symlist)
3952 symlist (cdr symlist))
3953 (if (and (symbolp (symbol-value symbol))
3954 (not (null (symbol-value symbol))))
3955 (set (symbol-value symbol) nil)))))
3956
3957(defun reftex-compute-ref-cite-tables ()
3958 ;; Update ref and cite tables
3959
3960 (interactive)
3961
3962 ;; Compile information in reftex-label-alist
3963 (let ((tmp (reftex-uniquify (reftex-splice-symbols-into-list
3964 (append
3965 reftex-label-alist
3966 reftex-label-alist-external-add-ons
3967 reftex-default-label-alist-entries)
3968 reftex-label-alist-builtin)
3969 '(nil)))
3970 entry env-or-mac typekeychar typekey prefix regexp
3971 fmt wordlist cmd qh-list)
3972
3973 (setq reftex-words-to-typekey-alist nil
3974 reftex-typekey-list nil
3975 reftex-typekey-to-format-alist nil
3976 reftex-typekey-to-prefix-alist nil
3977 reftex-env-or-mac-alist nil
3978 reftex-label-env-list nil
3979 reftex-label-mac-list nil)
3980 (while tmp
3981 (catch 'next-entry
3982 (setq entry (car tmp)
3983 env-or-mac (car entry)
3984 entry (cdr entry)
3985 tmp (cdr tmp))
3986 (if (null env-or-mac)
3987 (setq env-or-mac ""))
3988 (if (stringp (car entry))
3989 ;; This is before version 2.00 - convert entry to new format
3990 ;; This is just to keep old users happy
3991 (setq entry (cons (string-to-char (car entry))
3992 (cons (concat (car entry) ":")
3993 (cdr entry)))))
3994 (setq typekeychar (nth 0 entry)
3995 typekey (char-to-string typekeychar)
3996 prefix (nth 1 entry)
3997 fmt (nth 2 entry)
3998 regexp (nth 3 entry)
3999 wordlist (nth 4 entry))
4000 (if (stringp wordlist)
4001 ;; This is before version 2.04 - convert to new format
4002 (setq wordlist (nthcdr 4 entry)))
4003 (if typekey
4004 (add-to-list 'reftex-typekey-list typekey))
4005 (if (and typekey prefix)
4006 (add-to-list 'reftex-typekey-to-prefix-alist (cons typekey prefix)))
4007 (cond
4008 ((string-match "\\`\\\\" env-or-mac)
4009 ;; It's a macro
4010 (add-to-list 'reftex-label-mac-list env-or-mac))
4011 (t
4012 (or (string= env-or-mac "any")
4013 (string= env-or-mac "")
4014 (add-to-list 'reftex-label-env-list env-or-mac))))
4015 (and fmt
4016 (not (assoc typekey reftex-typekey-to-format-alist))
4017 (setq reftex-typekey-to-format-alist
4018 (cons (cons typekey fmt)
4019 reftex-typekey-to-format-alist)))
4020 (and (not (string= env-or-mac "any"))
4021 (not (string= env-or-mac ""))
4022 (not (assoc env-or-mac reftex-env-or-mac-alist))
4023 (setq reftex-env-or-mac-alist
4024 (cons (list env-or-mac typekey regexp)
4025 reftex-env-or-mac-alist)))
4026 (while (and wordlist (stringp (car wordlist)))
4027 (or (assoc (car wordlist) reftex-words-to-typekey-alist)
4028 (setq reftex-words-to-typekey-alist
4029 (cons (cons (downcase (car wordlist)) typekey)
4030 reftex-words-to-typekey-alist)))
4031 (setq wordlist (cdr wordlist)))
4032 (cond
4033 ((string= "" env-or-mac) nil)
4034 ((assoc typekey qh-list)
4035 (setcdr (assoc typekey qh-list)
4036 (concat (cdr (assoc typekey qh-list)) " " env-or-mac)))
4037 (t
4038 (setq qh-list (cons (cons typekey env-or-mac) qh-list))))))
4039
4040 (setq qh-list (nreverse qh-list))
4041 (setq reftex-typekey-to-prefix-alist
4042 (nreverse reftex-typekey-to-prefix-alist))
4043 (setq reftex-type-query-prompt
4044 (concat "Label type: "
4045 (mapconcat '(lambda(x)
4046 (format "[%s]" (car x)))
4047 qh-list " ")
4048 " (?=Help)"))
4049 (setq reftex-type-query-help
4050 (concat "SELECT A LABEL TYPE:\n--------------------\n"
4051 (mapconcat '(lambda(x)
4052 (format " [%s] %s"
4053 (car x) (cdr x)))
4054 qh-list "\n")))))
4055
4056;;; Keybindings --------------------------------------------------------------
4057
4058(define-key reftex-mode-map "\C-c-" 'reftex-item)
4059(define-key reftex-mode-map "\C-c=" 'reftex-toc)
4060(define-key reftex-mode-map "\C-c(" 'reftex-label)
4061(define-key reftex-mode-map "\C-c)" 'reftex-reference)
4062(define-key reftex-mode-map "\C-c[" 'reftex-citation)
4063(define-key reftex-mode-map "\C-c&" 'reftex-view-crossref)
4064
4065;; If the user requests so, she can have a few more bindings:
4066(cond
4067 (reftex-extra-bindings
4068 (define-key reftex-mode-map "\C-ct" 'reftex-toc)
4069 (define-key reftex-mode-map "\C-cl" 'reftex-label)
4070 (define-key reftex-mode-map "\C-cr" 'reftex-reference)
4071 (define-key reftex-mode-map "\C-cc" 'reftex-citation)
4072 (define-key reftex-mode-map "\C-cv" 'reftex-view-crossref)
4073 (define-key reftex-mode-map "\C-cg" 'reftex-grep-document)
4074 (define-key reftex-mode-map "\C-cs" 'reftex-search-document)))
4075
4076;;; Menus --------------------------------------------------------------------
4077
4078;; Define a menu for the menu bar if Emacs is running under X
4079
4080(require 'easymenu)
4081
4082(easy-menu-define
4083 reftex-mode-menu reftex-mode-map
4084 "Menu used in RefTeX mode"
4085 '("Ref"
4086 ["Table of Contents" reftex-toc t]
4087 "----"
025bb635
RS
4088 ["\\label" reftex-label t]
4089 ["\\ref" reftex-reference t]
4090 ["\\cite" reftex-citation t]
a7ec1775
RS
4091 ["View crossref" reftex-view-crossref t]
4092 "----"
4093 ("Search and Replace"
4094 ["Search whole document" reftex-search-document t]
4095 ["Replace in document" reftex-query-replace-document t]
4096 ["Grep on document" reftex-grep-document t]
4097 "----"
4098 ["Find duplicate labels" reftex-find-duplicate-labels t]
4099 ["Change label and refs" reftex-change-label t]
4100 "----"
4101 ["Create TAGS file" reftex-create-tags-file t])
4102 "----"
4103 ["Parse document" reftex-parse-document t]
4104 ["Reset RefTeX Mode" reftex-reset-mode t]
4105 ["Customize RefTeX" reftex-customize t]))
4106
4107;;; Run Hook ------------------------------------------------------------------
4108
4109(run-hooks 'reftex-load-hook)
4110
4111;;; That's it! ----------------------------------------------------------------
4112
4113; Make sure tabels are compiled
4114(message "updating internal tables...")
4115(reftex-compute-ref-cite-tables)
4116(setq reftex-tables-dirty nil)
4117
4118(provide 'reftex)
4119
4120;;;============================================================================
4121
4122;;; reftex.el end here