comparison lisp/modes/reftex.el @ 155:43dd3413c7c7 r20-3b4

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