155
|
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 ((?
. ("\cite{KEY}" "AUTHOR \cite{KEY}" "AUTHOR and AUTHOR \cite{KEY}")))
|
|
909 (cons :tag "Enter a keyed list of format strings"
|
|
910 (character :tag "Key character " ?
)
|
|
911 (repeat
|
|
912 (string :tag "format string" ""))))))
|
|
913
|
|
914 ;; Table of contents configuration --------------------------------------
|
|
915
|
|
916 (defgroup reftex-table-of-contents-browser nil
|
|
917 "A multifile table of contents browser."
|
|
918 :group 'reftex)
|
|
919
|
|
920 (defcustom reftex-toc-follow-mode nil
|
|
921 "Non-nil means, point in *toc* buffer will cause other window to follow.
|
|
922 The other window will show the corresponding part of the document.
|
|
923 This flag can be toggled from within the *toc* buffer with the `f' key."
|
|
924 :group 'reftex-table-of-contents-browser
|
|
925 :type '(boolean))
|
|
926
|
|
927 ;; Miscellaneous configurations -----------------------------------------
|
|
928
|
|
929 (defgroup reftex-miscellaneous-configurations nil
|
|
930 "Collection of further configurations"
|
|
931 :group 'reftex)
|
|
932
|
|
933 (defcustom reftex-extra-bindings nil
|
|
934 "Non-nil means, make additional key bindings on startup.
|
|
935 These extra bindings are located in the users C-c letter map."
|
|
936 :group 'reftex-miscellaneous-configurations
|
|
937 :type '(boolean))
|
|
938
|
|
939 (defcustom reftex-use-fonts t
|
|
940 "*Non-nil means, use fonts in label menu and on-the-fly help.
|
|
941 Font-lock must be loaded as well to actually get fontified display."
|
|
942 :group 'reftex-miscellaneous-configurations
|
|
943 :type '(boolean))
|
|
944
|
|
945 (defcustom reftex-keep-temporary-buffers t
|
|
946 "*Non-nil means, keep any TeX and BibTeX files loaded for lookup.
|
|
947 Nil means, kill it immediately after use unless it was already an existing
|
|
948 buffer before the lookup happened. It is faster to keep the buffers, but can
|
|
949 use a lot of memory, depending on the size of your database and document."
|
|
950 :group 'reftex-miscellaneous-configurations
|
|
951 :type '(boolean))
|
|
952
|
|
953 (defcustom reftex-auto-show-entry t
|
|
954 "*Non-nil means, showing context in another window may unhide a section.
|
|
955 This is important when using outline-minor-mode. If the context to be shown
|
|
956 is in a hidden section, RefTeX will issue a \"show-entry\" command in order
|
|
957 to show it. This is not reversed when the label is selected - so the section
|
|
958 remains shown after command completion."
|
|
959 :group 'reftex-miscellaneous-configurations
|
|
960 :type '(boolean))
|
|
961
|
|
962
|
|
963 ;;; End of Configuration Section ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
964
|
|
965 ;;;===========================================================================
|
|
966 ;;;
|
|
967 ;;; Define the formal stuff for a minor mode named RefTeX.
|
|
968 ;;;
|
|
969
|
|
970 (defvar reftex-mode nil
|
|
971 "Determines if RefTeX minor mode is active.")
|
|
972 (make-variable-buffer-local 'reftex-mode)
|
|
973
|
|
974 (defvar reftex-mode-map (make-sparse-keymap)
|
|
975 "Keymap for RefTeX minor mode.")
|
|
976
|
|
977 (defvar reftex-mode-menu nil)
|
|
978
|
|
979 ;;;###autoload
|
|
980 (defun turn-on-reftex ()
|
|
981 "Turn on RefTeX minor mode."
|
|
982 (reftex-mode t))
|
|
983
|
|
984 ;;;###autoload
|
|
985 (defun reftex-mode (&optional arg)
|
|
986 "Minor mode with distinct support for \\label, \\ref and \\cite in LaTeX.
|
|
987
|
|
988 Labels can be created with `\\[reftex-label]' and referenced with `\\[reftex-reference]'.
|
|
989 When referencing, you get a menu with all labels of a given type and
|
|
990 context of the label definition. The selected label is inserted as a
|
|
991 \\ref macro.
|
|
992
|
|
993 Citations can be made with `\\[reftex-citation]' which will use a regular expression
|
|
994 to pull out a *formatted* list of articles from your BibTeX
|
|
995 database. The selected citation is inserted as a \\cite macro.
|
|
996
|
|
997 A Table of Contents of the entire (multifile) document with browsing
|
|
998 capabilities is available with `\\[reftex-toc]'.
|
|
999
|
|
1000 Most command have help available on the fly. This help is accessed by
|
|
1001 pressing `?' to any prompt mentioning this feature.
|
|
1002
|
|
1003 \\{reftex-mode-map}
|
|
1004 Under X, these functions will be available also in a menu on the menu bar.
|
|
1005
|
|
1006 ------------------------------------------------------------------------------"
|
|
1007
|
|
1008 (interactive "P")
|
|
1009 (setq reftex-mode (not (or (and (null arg) reftex-mode)
|
|
1010 (<= (prefix-numeric-value arg) 0))))
|
|
1011
|
|
1012 ; Add or remove the menu, and run the hook
|
|
1013 (if reftex-mode
|
|
1014 (progn
|
|
1015 (easy-menu-add reftex-mode-menu)
|
|
1016 (run-hooks 'reftex-mode-hook))
|
|
1017 (easy-menu-remove reftex-mode-menu)))
|
|
1018
|
|
1019 (or (assoc 'reftex-mode minor-mode-alist)
|
|
1020 (setq minor-mode-alist
|
|
1021 (cons '(reftex-mode " Ref") minor-mode-alist)))
|
|
1022
|
|
1023 (or (assoc 'reftex-mode minor-mode-map-alist)
|
|
1024 (setq minor-mode-map-alist
|
|
1025 (cons (cons 'reftex-mode reftex-mode-map)
|
|
1026 minor-mode-map-alist)))
|
|
1027
|
|
1028
|
|
1029 ;;; ===========================================================================
|
|
1030 ;;;
|
|
1031 ;;; Interfaces for other packages
|
|
1032 ;;; -----------------------------
|
|
1033 ;;;
|
|
1034 ;;; AUCTeX
|
|
1035 ;;; ------
|
|
1036
|
|
1037 (defun reftex-arg-label (optional &optional prompt definition)
|
|
1038 "Use reftex-label to create a label and insert it with TeX-argument-insert.
|
|
1039 This function is intended for AUCTeX macro support."
|
|
1040 (let ((label (reftex-label nil t)))
|
|
1041 (if (and definition (not (string-equal "" label)))
|
|
1042 (LaTeX-add-labels label))
|
|
1043 (TeX-argument-insert label optional optional)))
|
|
1044
|
|
1045 (defun reftex-arg-ref (optional &optional prompt definition)
|
|
1046 "Use reftex-reference to select a label, insert it with TeX-argument-insert.
|
|
1047 This function is intended for AUCTeX macro support."
|
|
1048 (let ((label (reftex-reference nil t)))
|
|
1049 (if (and definition (not (string-equal "" label)))
|
|
1050 (LaTeX-add-labels label))
|
|
1051 (TeX-argument-insert label optional optional)))
|
|
1052
|
|
1053 (defun reftex-arg-cite (optional &optional prompt definition)
|
|
1054 "Use reftex-citation to select a key, insert it with TeX-argument-insert.
|
|
1055 This function is intended for AUCTeX macro support."
|
|
1056 (let ((key (reftex-citation nil t)))
|
|
1057 (TeX-argument-insert (or key "") optional optional)))
|
|
1058
|
|
1059 (defvar reftex-label-alist-external-add-ons nil
|
|
1060 "List of label alist entries added with reftex-add-to-label-alist.")
|
|
1061
|
|
1062 ;;;###autoload
|
|
1063 (defun reftex-add-to-label-alist (entry-list)
|
|
1064 "Add label environment descriptions to reftex-label-alist-external-add-ons.
|
|
1065 The format of ENTRY-LIST is exactly like reftex-label-alist. See there
|
|
1066 for details.
|
|
1067 This function makes it possible to support RefTeX from AUCTeX style files.
|
|
1068 The entries in ENTRY-LIST will be processed after the user settings in
|
|
1069 reftex-label-alist, and before the defaults (specified in
|
|
1070 reftex-default-label-alist-entries). Any changes made to
|
|
1071 reftex-label-alist-external-add-ons will raise a flag to the effect that a
|
|
1072 mode reset is done on the next occasion."
|
|
1073 (let (entry)
|
|
1074 (while entry-list
|
|
1075 (setq entry (car entry-list)
|
|
1076 entry-list (cdr entry-list))
|
|
1077 (if (not (member entry reftex-label-alist-external-add-ons))
|
|
1078 (setq reftex-tables-dirty t
|
|
1079 reftex-label-alist-external-add-ons
|
|
1080 (cons entry reftex-label-alist-external-add-ons))))))
|
|
1081
|
|
1082 ;;; ===========================================================================
|
|
1083 ;;;
|
|
1084 ;;; Multifile support
|
|
1085 ;;;
|
|
1086 ;;; Technical notes: Multifile works as follows: We keep just one list
|
|
1087 ;;; of labels for each master file - this can save a lot of memory.
|
|
1088 ;;; reftex-master-index-list is an alist which connects the true file name
|
|
1089 ;;; of each master file with the symbols holding the information on that
|
|
1090 ;;; document. Each buffer has local variables which point to these symbols.
|
|
1091
|
|
1092
|
|
1093 ;; List of variables which handle the multifile stuff.
|
|
1094 ;; This list is used to tie, untie, and reset these symbols.
|
|
1095 (defconst reftex-multifile-symbols
|
|
1096 '(reftex-label-numbers-symbol reftex-list-of-labels-symbol
|
|
1097 reftex-bibfile-list-symbol))
|
|
1098
|
|
1099 ;; Alist connecting master file names with the corresponding lisp symbols
|
|
1100 (defvar reftex-master-index-list nil)
|
|
1101
|
|
1102 ;; Last index used for a master file
|
|
1103 (defvar reftex-multifile-index 0)
|
|
1104
|
|
1105 ;; Alist connecting a master file with all included files.
|
|
1106 ;; This information is not yet used, just collected.
|
|
1107 (defvar reftex-master-include-list nil)
|
|
1108
|
|
1109 ;; Variable holding the symbol with current value of label postfix
|
|
1110 (defvar reftex-label-numbers-symbol nil )
|
|
1111 (make-variable-buffer-local 'reftex-label-numbers-symbol)
|
|
1112
|
|
1113 ;; Variable holding the symbol with the label list of the document.
|
|
1114 ;; Each element of the label list is again a list with the following elements:
|
|
1115 ;; 0: One character label type indicator
|
|
1116 ;; 1: Short context to put into label menu
|
|
1117 ;; 2: The label
|
|
1118 ;; 3: The name of the file where the label is defined
|
|
1119 (defvar reftex-list-of-labels-symbol nil)
|
|
1120 (make-variable-buffer-local 'reftex-list-of-labels-symbol)
|
|
1121
|
|
1122 ;; Variable holding the symbol with a list of library files for this document
|
|
1123 (defvar reftex-bibfile-list-symbol nil)
|
|
1124 (make-variable-buffer-local 'reftex-bibfile-list-symbol)
|
|
1125
|
|
1126 (defun reftex-next-multifile-index ()
|
|
1127 ;; Return the next free index for multifile symbols.
|
|
1128 (setq reftex-multifile-index (1+ reftex-multifile-index)))
|
|
1129
|
|
1130 (defun reftex-tie-multifile-symbols ()
|
|
1131 ;; Tie the buffer-local symbols to globals connected with the master file.
|
|
1132 ;; If the symbols for the current master file do not exist, they are created.
|
|
1133
|
|
1134 (let* ((master (file-truename (reftex-TeX-master-file)))
|
|
1135 (index (assoc master reftex-master-index-list))
|
|
1136 (symlist reftex-multifile-symbols)
|
|
1137 (symbol nil)
|
|
1138 (symname nil)
|
|
1139 (newflag nil))
|
|
1140 ;; find the correct index
|
|
1141 (if index
|
|
1142 ;; symbols do exist
|
|
1143 (setq index (cdr index))
|
|
1144 ;; get a new index and add info to the alist
|
|
1145 (setq index (reftex-next-multifile-index)
|
|
1146 reftex-master-index-list (cons
|
|
1147 (cons master index)
|
|
1148 reftex-master-index-list)
|
|
1149 newflag t))
|
|
1150
|
|
1151 ;; get/create symbols and tie them
|
|
1152 (while symlist
|
|
1153 (setq symbol (car symlist)
|
|
1154 symlist (cdr symlist)
|
|
1155 symname (symbol-name symbol))
|
|
1156 (set symbol (intern (concat symname "-" (int-to-string index))))
|
|
1157 ;; initialize if new symbols
|
|
1158 (if newflag (set (symbol-value symbol) nil)))
|
|
1159
|
|
1160 ;; Return t if the symbols did already exist, nil when we've made them
|
|
1161 (not newflag)))
|
|
1162
|
|
1163 (defun reftex-untie-multifile-symbols ()
|
|
1164 ;; Remove ties from multifile symbols, so that next use makes new ones.
|
|
1165 (let ((symlist reftex-multifile-symbols)
|
|
1166 (symbol nil))
|
|
1167 (while symlist
|
|
1168 (setq symbol (car symlist)
|
|
1169 symlist (cdr symlist))
|
|
1170 (set symbol nil))))
|
|
1171
|
|
1172 (defun reftex-TeX-master-file ()
|
|
1173 ;; Return the name of the master file associated with the current buffer.
|
|
1174 ;; When AUCTeX is loaded, we will use it's more sophisticated method.
|
|
1175 ;; We also support the default TeX and LaTeX modes by checking for a
|
|
1176 ;; variable tex-main-file.
|
|
1177
|
|
1178 (let
|
|
1179 ((master
|
|
1180 (cond
|
|
1181 ((fboundp 'TeX-master-file) ; AUCTeX is loaded. Use its mechanism.
|
|
1182 (TeX-master-file t))
|
|
1183 ((boundp 'TeX-master) ; The variable is defined - lets use it.
|
|
1184 (cond
|
|
1185 ((eq TeX-master t)
|
|
1186 (buffer-file-name))
|
|
1187 ((eq TeX-master 'shared)
|
|
1188 (setq TeX-master (read-file-name "Master file: "
|
|
1189 nil nil t nil)))
|
|
1190 (TeX-master)
|
|
1191 (t
|
|
1192 (setq TeX-master (read-file-name "Master file: "
|
|
1193 nil nil t nil)))))
|
|
1194 ((boundp 'tex-main-file)
|
|
1195 ;; This is the variable from the default TeX modes
|
|
1196 (cond
|
|
1197 ((stringp tex-main-file)
|
|
1198 ;; ok, this must be it
|
|
1199 tex-main-file)
|
|
1200 (t
|
|
1201 ;; In this case, the buffer is its own master
|
|
1202 (buffer-file-name))))
|
|
1203 (t
|
|
1204 ;; Know nothing about master file. Assume this is a master file.
|
|
1205 (buffer-file-name)))))
|
|
1206 (cond
|
|
1207 ((null master)
|
|
1208 (error "Need a filename for this buffer. Please save it first."))
|
|
1209 ((or (file-exists-p master)
|
|
1210 (reftex-get-buffer-visiting master))
|
|
1211 ;; We either see the file, or have a buffer on it. OK.
|
|
1212 )
|
|
1213 ((or (file-exists-p (concat master ".tex"))
|
|
1214 (reftex-get-buffer-visiting (concat master ".tex")))
|
|
1215 ;; Ahh, an extra .tex was missing...
|
|
1216 (setq master (concat master ".tex")))
|
|
1217 (t
|
|
1218 ;; Something is wrong here. Throw an exception.
|
|
1219 (error "No such master file %s" master)))
|
|
1220 (expand-file-name master)))
|
|
1221
|
|
1222 (defun reftex-make-master-buffer (master-file mode)
|
|
1223 "Make a master buffer which contains the MASTER-FILE and all includes.
|
|
1224 This is to prepare a buffer containing the entire document in correct
|
|
1225 sequence for parsing. Therefore it will even expand includes which are
|
|
1226 commented out.
|
|
1227 The function returns the number of input/include files not found."
|
|
1228
|
|
1229 (interactive "fmaster file: ")
|
|
1230 (let ((not-found 0) file file-list tmp (font-lock-maximum-size 1))
|
|
1231 (switch-to-buffer "*reftex-master.tex*")
|
|
1232 (erase-buffer)
|
|
1233 (if (not (eq major-mode mode))
|
|
1234 (funcall mode))
|
|
1235 ;; first insert the master file
|
|
1236 (if (not (file-exists-p master-file))
|
|
1237 (error "No such master file: %s" master-file))
|
|
1238 (reftex-insert-buffer-or-file master-file)
|
|
1239 (subst-char-in-region (point-min) (point-max) ?\r ?\n t)
|
|
1240 (setq file-list (cons master-file file-list))
|
|
1241 (goto-char 1)
|
|
1242 ;; remember from which file these lines came
|
|
1243 (put-text-property (point-min) (point-max) 'file
|
|
1244 (expand-file-name master-file))
|
|
1245 ;; Now find recursively all include/input statements and expand them
|
|
1246 (while (re-search-forward
|
|
1247 "^[ \t]*\\\\\\(include\\|input\\){\\([^}\n]+\\)}" nil t)
|
|
1248 ;; Change default directory, so that relative fine names work correctly
|
|
1249 (setq file (reftex-no-props (match-string 2)))
|
|
1250 (save-match-data
|
|
1251 (cd (file-name-directory
|
|
1252 (get-text-property (match-beginning 0) 'file)))
|
|
1253 (if (not (string-match "\\.tex$" file))
|
|
1254 (setq file (concat file ".tex"))))
|
|
1255 (if (file-exists-p file)
|
|
1256 (progn
|
|
1257 (replace-match
|
|
1258 (format "\n%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% START OF %s FILE: %s\n%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% END OF %s FILE: %s\n"
|
|
1259 (match-string 1) file
|
|
1260 (match-string 1) file))
|
|
1261 (beginning-of-line 0)
|
|
1262 (narrow-to-region (point) (point))
|
|
1263 ;; insert the file
|
|
1264 (reftex-insert-buffer-or-file file)
|
|
1265 (subst-char-in-region (point-min) (point-max) ?\r ?\n t)
|
|
1266 (setq file-list (cons (expand-file-name file) file-list))
|
|
1267 ;; remember from which file these lines came
|
|
1268 (put-text-property (point-min) (point-max)
|
|
1269 'file (expand-file-name file))
|
|
1270 (goto-char (point-min))
|
|
1271 (widen))
|
|
1272 (message "Input/include file %s not found. Ignored. Continuing..."
|
|
1273 file)
|
|
1274 (setq not-found (1+ not-found))))
|
|
1275 (setq file-list (nreverse file-list))
|
|
1276 (while (setq tmp (assoc (car file-list) reftex-master-include-list))
|
|
1277 (setq reftex-master-include-list (delq tmp reftex-master-include-list)))
|
|
1278 (setq reftex-master-include-list (cons file-list reftex-master-include-list))
|
|
1279 not-found))
|
|
1280
|
|
1281 (defun reftex-insert-buffer-or-file (file)
|
|
1282 "If there is a buffer associated with FILE, insert it - otherwise the FILE."
|
|
1283 (let ((buffer (reftex-get-buffer-visiting file)))
|
|
1284 (if buffer
|
|
1285 (let (beg end beg1 end1)
|
|
1286 (save-excursion
|
|
1287 ;; make sure we get the whole buffer
|
|
1288 (set-buffer buffer)
|
|
1289 (setq beg (point-min) end (point-max))
|
|
1290 (widen)
|
|
1291 (setq beg1 (point-min) end1 (point-max)))
|
|
1292 (insert-buffer-substring buffer beg1 end1)
|
|
1293 (save-excursion
|
|
1294 (set-buffer buffer)
|
|
1295 (narrow-to-region beg end)))
|
|
1296 (insert-file-contents file))))
|
|
1297
|
|
1298
|
|
1299 (defun reftex-parse-document (&optional buffer)
|
|
1300 "Rescan the document."
|
|
1301 (interactive)
|
|
1302 (save-window-excursion
|
|
1303 (save-excursion
|
|
1304 (if buffer
|
|
1305 (if (not (bufferp buffer))
|
|
1306 (error "No such buffer %s" (buffer-name buffer))
|
|
1307 (set-buffer buffer)))
|
|
1308 (reftex-access-scan-info t))))
|
|
1309
|
|
1310 (defun reftex-access-scan-info (&optional rescan)
|
|
1311 ;; Access the scanning info. When the multifile symbols are not yet tied,
|
|
1312 ;; tie them. When they are have to be created, do a buffer scan to
|
|
1313 ;; fill them.
|
|
1314
|
|
1315 ;; If RESCAN is non-nil, enforce document scanning
|
|
1316
|
|
1317 (catch 'exit
|
|
1318 (let ((rescan (or (equal rescan t) (equal rescan '(4)))))
|
|
1319
|
|
1320 ;; Reset the mode if we had changes from style hooks
|
|
1321 (and reftex-tables-dirty
|
|
1322 (reftex-reset-mode))
|
|
1323
|
|
1324 (if (eq reftex-list-of-labels-symbol nil)
|
|
1325 ;; Symbols are not yet tied: Tie them and see if they are set
|
|
1326 (reftex-tie-multifile-symbols))
|
|
1327
|
|
1328 (if (and (symbol-value reftex-list-of-labels-symbol)
|
|
1329 (not rescan))
|
|
1330 ;; Lists do already exist and we don't need to rescan.
|
|
1331 ;; Return from here.
|
|
1332 (throw 'exit t))
|
|
1333
|
|
1334 ;; We need to rescan
|
|
1335 ;; =================
|
|
1336
|
|
1337 (unwind-protect
|
|
1338 (save-window-excursion
|
|
1339 (save-excursion
|
|
1340
|
|
1341 ;; do the scanning
|
|
1342
|
|
1343 (let ((label-list-symbol reftex-list-of-labels-symbol)
|
|
1344 (label-numbers-symbol reftex-label-numbers-symbol)
|
|
1345 (bibfile-list-symbol reftex-bibfile-list-symbol))
|
|
1346
|
|
1347 (message "Creating master buffer...")
|
|
1348 (reftex-make-master-buffer (reftex-TeX-master-file) major-mode)
|
|
1349
|
|
1350 (message "Scanning document...")
|
|
1351
|
|
1352 (reftex-scan-buffer-for-labels
|
|
1353 label-numbers-symbol label-list-symbol)
|
|
1354
|
|
1355 (reftex-scan-buffer-for-bibliography-statement
|
|
1356 bibfile-list-symbol)
|
|
1357
|
|
1358 (message "Scanning document... done"))))
|
|
1359
|
|
1360 (if (get-buffer "*reftex-master.tex*")
|
|
1361 (kill-buffer "*reftex-master.tex*"))))))
|
|
1362
|
|
1363 (defun reftex-create-tags-file ()
|
|
1364 "Create TAGS file by running `etags' on the current document.
|
|
1365 The TAGS file is also immediately visited with `visit-tags-table."
|
|
1366 (interactive)
|
|
1367 (reftex-access-scan-info current-prefix-arg)
|
|
1368 (let* ((master (reftex-TeX-master-file))
|
|
1369 (files (assoc master reftex-master-include-list))
|
|
1370 (cmd (format "etags %s" (mapconcat 'identity files " "))))
|
|
1371 (save-excursion
|
|
1372 (set-buffer (reftex-get-buffer-visiting master))
|
|
1373 (message "Running etags to create TAGS file...")
|
|
1374 (shell-command cmd)
|
|
1375 (visit-tags-table "TAGS"))))
|
|
1376
|
|
1377 ;; History of grep commands.
|
|
1378 (defvar reftex-grep-history nil)
|
|
1379 (defvar reftex-grep-command "grep -n "
|
|
1380 "Last grep command used in \\[reftex-grep-document]; default for next grep.")
|
|
1381
|
|
1382 (defun reftex-grep-document (grep-cmd)
|
|
1383 "Run grep query through all files related to this document.
|
|
1384 With prefix arg, force to rescan document.
|
|
1385 This works also without an active TAGS table."
|
|
1386
|
|
1387 (interactive
|
|
1388 (list (read-from-minibuffer "Run grep on document (like this): "
|
|
1389 reftex-grep-command nil nil
|
|
1390 'reftex-grep-history)))
|
|
1391 (reftex-access-scan-info current-prefix-arg)
|
|
1392 (let* ((master (reftex-TeX-master-file))
|
|
1393 (default-directory (file-name-directory master))
|
|
1394 (re (format "\\`%s\\(.*\\)" (regexp-quote
|
|
1395 (expand-file-name default-directory))))
|
|
1396 (files (assoc master reftex-master-include-list))
|
|
1397 (cmd (format
|
|
1398 "%s %s" grep-cmd
|
|
1399 (mapconcat (function (lambda (x)
|
|
1400 (if (string-match re x)
|
|
1401 (match-string 1 x)
|
|
1402 x)))
|
|
1403 files " "))))
|
|
1404 (grep cmd)))
|
|
1405
|
|
1406 (defun reftex-search-document (&optional regexp)
|
|
1407 "Regexp search through all files of the current TeX document.
|
|
1408 Starts always in the master file. Stops when a match is found.
|
|
1409 To continue searching for next match, use command \\[tags-loop-continue].
|
|
1410 This works also without an active TAGS table."
|
|
1411 (interactive)
|
|
1412 (let ((default (reftex-this-word)))
|
|
1413 (if (not regexp)
|
|
1414 (setq regexp (read-string (format "Search regexp in document [%s]: "
|
|
1415 default))))
|
|
1416 (if (string= regexp "") (setq regexp (regexp-quote default)))
|
|
1417
|
|
1418 (reftex-access-scan-info current-prefix-arg)
|
|
1419 (tags-search regexp (list 'assoc (reftex-TeX-master-file)
|
|
1420 'reftex-master-include-list))))
|
|
1421
|
|
1422 (defun reftex-query-replace-document (&optional from to delimited)
|
|
1423 "Run a query-replace-regexp of FROM with TO over the entire TeX document.
|
|
1424 Third arg DELIMITED (prefix arg) means replace only word-delimited matches.
|
|
1425 If you exit (\\[keyboard-quit] or ESC), you can resume the query replace
|
|
1426 with the command \\[tags-loop-continue].
|
|
1427 This works also without an active TAGS table."
|
|
1428 (interactive)
|
|
1429 (let ((default (reftex-this-word)))
|
|
1430 (if (not from)
|
|
1431 (progn
|
|
1432 (setq from (read-string (format "Replace regexp in document [%s]: "
|
|
1433 default)))
|
|
1434 (if (string= from "") (setq from (regexp-quote default)))))
|
|
1435 (if (not to)
|
|
1436 (setq to (read-string (format "Replace regexp %s with: " from))))
|
|
1437 (reftex-access-scan-info current-prefix-arg)
|
|
1438 (tags-query-replace from to (or delimited current-prefix-arg)
|
|
1439 (list 'assoc (reftex-TeX-master-file)
|
|
1440 'reftex-master-include-list))))
|
|
1441
|
|
1442 (defun reftex-change-label (&optional from to)
|
|
1443 "Query replace FROM with TO in all \\label and \\ref commands.
|
|
1444 Works on the entire multifile document.
|
|
1445 If you exit (\\[keyboard-quit] or ESC), you can resume the query replace
|
|
1446 with the command \\[tags-loop-continue].
|
|
1447 This works also without an active TAGS table."
|
|
1448 (interactive)
|
|
1449 (let ((default (reftex-this-word "-a-zA-Z0-9_*.:")))
|
|
1450 (if (not from)
|
|
1451 (setq from (read-string (format "Replace label globally [%s]: "
|
|
1452 default))))
|
|
1453 (if (string= from "") (setq from default))
|
|
1454 (if (not to)
|
|
1455 (setq to (read-string (format "Replace label %s with: "
|
|
1456 from))))
|
|
1457 (reftex-query-replace-document
|
|
1458 (concat "\\\\\\(label\\|[a-z]*ref\\){" (regexp-quote from) "}")
|
|
1459 (format "\\\\\\1{%s}" to))))
|
|
1460
|
|
1461 (defun reftex-this-word (&optional class)
|
|
1462 ;; grab the word around point
|
|
1463 (setq class (or class "-a-zA-Z0-9:_/.*;|"))
|
|
1464 (save-excursion
|
|
1465 (buffer-substring-no-properties
|
|
1466 (progn (skip-chars-backward class) (point))
|
|
1467 (progn (skip-chars-forward class) (point)))))
|
|
1468
|
|
1469 ;;; ===========================================================================
|
|
1470 ;;;
|
|
1471 ;;; Functions to create and reference automatic labels
|
|
1472
|
|
1473 ;; The following constants are derived from reftex-label-alist
|
|
1474
|
|
1475 ;; Prompt used for label type querys directed to the user
|
|
1476 (defconst reftex-type-query-prompt nil)
|
|
1477
|
|
1478 ;; Help string for label type querys
|
|
1479 (defconst reftex-type-query-help nil)
|
|
1480
|
|
1481 ;; Alist relating label type to reference format
|
|
1482 (defconst reftex-typekey-to-format-alist nil)
|
|
1483
|
|
1484 ;; Alist relating label type to label affix
|
|
1485 (defconst reftex-typekey-to-prefix-alist nil)
|
|
1486
|
|
1487 ;; Alist relating environments or macros to label type and context regexp
|
|
1488 (defconst reftex-env-or-mac-alist nil)
|
|
1489
|
|
1490 ;; List of macros carrying a label
|
|
1491 (defconst reftex-label-mac-list nil)
|
|
1492
|
|
1493 ;; List of environments carrying a label
|
|
1494 (defconst reftex-label-env-list nil)
|
|
1495
|
|
1496 ;; List of all typekey letters in use
|
|
1497 (defconst reftex-typekey-list nil)
|
|
1498
|
|
1499 ;; Alist relating magic words to a label type
|
|
1500 (defconst reftex-words-to-typekey-alist nil)
|
|
1501
|
|
1502 ;; The last list-of-labels entry used in a reference
|
|
1503 (defvar reftex-last-used-reference (list nil nil nil nil))
|
|
1504
|
|
1505 ;; The regular expression used to abbreviate words
|
|
1506 (defconst reftex-abbrev-regexp
|
|
1507 (concat
|
|
1508 "^\\("
|
|
1509 (make-string (nth 0 reftex-abbrev-parameters) ?.)
|
|
1510 "[" (nth 2 reftex-abbrev-parameters) "]*"
|
|
1511 "\\)"
|
|
1512 "[" (nth 3 reftex-abbrev-parameters) "]"
|
|
1513 (make-string (1- (nth 1 reftex-abbrev-parameters)) ?.)))
|
|
1514
|
|
1515 ;; Global variables used for communication between functions
|
|
1516 (defvar reftex-default-context-position nil)
|
|
1517 (defvar reftex-location-start nil)
|
|
1518 (defvar reftex-call-back-to-this-buffer nil)
|
|
1519
|
|
1520 ;; List of buffers created temporarily for lookup, which should be killed
|
|
1521 (defvar reftex-buffers-to-kill nil)
|
|
1522
|
|
1523 ;; The regexp used to find section statements
|
|
1524 (defconst reftex-section-regexp "^[ ]*\\\\\\(part\\|chapter\\|section\\|subsection\\|subsubsection\\|paragraph\\|subparagraph\\|subsubparagraph\\)\\*?\\(\\[[^]]*\\]\\)?{")
|
|
1525
|
|
1526 ;; LaTeX section commands and level numbers
|
|
1527 (defconst reftex-section-levels
|
|
1528 '(
|
|
1529 ("part" . 0)
|
|
1530 ("chapter" . 1)
|
|
1531 ("section" . 2)
|
|
1532 ("subsection" . 3)
|
|
1533 ("subsubsection" . 4)
|
|
1534 ("paragraph" . 5)
|
|
1535 ("subparagraph" . 6)
|
|
1536 ("subsubparagraph" . 7)
|
|
1537 ))
|
|
1538
|
|
1539 (defun reftex-label (&optional environment no-insert)
|
|
1540 "Insert a unique label. Return the label.
|
|
1541 If ENVIRONMENT is given, don't bother to find out yourself.
|
|
1542 If NO-INSERT is non-nil, do not insert label into buffer.
|
|
1543 With prefix arg, force to rescan document first.
|
|
1544 The label is also inserted into the label list.
|
|
1545 This function is controlled by the settings of reftex-insert-label-flags."
|
|
1546
|
|
1547 (interactive)
|
|
1548
|
|
1549 ;; Ensure access to scanning info and rescan buffer if prefix are is '(4)
|
|
1550 (reftex-access-scan-info current-prefix-arg)
|
|
1551
|
|
1552 ;; Find out what kind of environment this is and abort if necessary
|
|
1553 (if (or (not environment)
|
|
1554 (not (assoc environment reftex-env-or-mac-alist)))
|
|
1555 (setq environment (reftex-label-location)))
|
|
1556 (if (not environment)
|
|
1557 (error "Can't figure out what kind of label should be inserted"))
|
|
1558
|
|
1559 ;; Ok, go ahead
|
|
1560 (let (label num typekey prefix entry cell lab valid default force-prompt)
|
|
1561 (setq typekey (nth 1 (assoc environment
|
|
1562 reftex-env-or-mac-alist)))
|
|
1563 (setq prefix (or (cdr (assoc typekey reftex-typekey-to-prefix-alist))
|
|
1564 (concat typekey "-")))
|
|
1565
|
|
1566 ;; make a default label
|
|
1567 (cond
|
|
1568
|
|
1569 ((reftex-typekey-check typekey (nth 0 reftex-insert-label-flags))
|
|
1570 ;; derive a label from context
|
|
1571 (setq default (nth 2 (reftex-label-info " ")))
|
|
1572 ;; catch the cases where the is actually no context available
|
|
1573 (if (or (string-match "NO MATCH FOR CONTEXT REGEXP" default)
|
|
1574 (string-match "ILLEGAL VALUE OF PARSE" default)
|
|
1575 (string-match "SECTION HEADING NOT FOUND" default)
|
|
1576 (string-match "HOOK ERROR" default)
|
|
1577 (string-match "^[ \t]*$" default))
|
|
1578 (setq default prefix
|
|
1579 force-prompt t) ; need to prompt
|
|
1580 (setq default (concat prefix (reftex-string-to-label default)))
|
|
1581
|
|
1582 ;; make it unique
|
|
1583 (setq label default)
|
|
1584 (setq num 1)
|
|
1585 (while (assoc label (symbol-value reftex-list-of-labels-symbol))
|
|
1586 (setq label (concat default "-" (setq num (1+ num)))))
|
|
1587 (setq default label)))
|
|
1588
|
|
1589 ((reftex-typekey-check typekey (nth 1 reftex-insert-label-flags)) ; prompt
|
|
1590 ;; Minimal default: the user will be prompted
|
|
1591 (setq default prefix))
|
|
1592
|
|
1593 (t
|
|
1594 ;; make an automatic label
|
|
1595 (while (assoc
|
|
1596 (setq default (concat prefix (reftex-next-label-number typekey)))
|
|
1597 (symbol-value reftex-list-of-labels-symbol)))))
|
|
1598
|
|
1599 ;; Should we ask the user?
|
|
1600 (if (or (reftex-typekey-check typekey
|
|
1601 (nth 1 reftex-insert-label-flags)) ; prompt
|
|
1602 force-prompt)
|
|
1603
|
|
1604 (while (not valid)
|
|
1605 ;; iterate until we get a legal label
|
|
1606
|
|
1607 (setq label (read-string "Label: " default))
|
|
1608
|
|
1609 ;; Lets make sure that this is a legal label
|
|
1610 (cond
|
|
1611
|
|
1612 ;; Test if label contains strange characters
|
|
1613 ((string-match reftex-label-illegal-re label)
|
|
1614 (message "Label \"%s\" contains illegal characters" label)
|
|
1615 (ding)
|
|
1616 (sit-for 2))
|
|
1617
|
|
1618 ;; Look it up in the label list
|
|
1619 ((setq entry (assoc label
|
|
1620 (symbol-value reftex-list-of-labels-symbol)))
|
|
1621 (message "Label \"%s\" exists in file %s" label (nth 3 entry))
|
|
1622 (ding)
|
|
1623 (sit-for 2))
|
|
1624
|
|
1625 ;; Label is ok
|
|
1626 (t
|
|
1627 (setq valid t))))
|
|
1628 (setq label default))
|
|
1629
|
|
1630 ;; Insert the label
|
|
1631 (if (not no-insert)
|
|
1632 (insert "\\label{" label "}"))
|
|
1633
|
|
1634 ;; Insert the label into the label list
|
|
1635 (if (symbol-value reftex-list-of-labels-symbol)
|
|
1636 (let ((cnt 0)
|
|
1637 (pos (point))
|
|
1638 (all (symbol-value reftex-list-of-labels-symbol))
|
|
1639 (look-for nil)
|
|
1640 (note nil)
|
|
1641 (text nil)
|
|
1642 (file (buffer-file-name)))
|
|
1643
|
|
1644 ;; find the previous label in order to know where to insert new label
|
|
1645 ;; into label list
|
|
1646 (save-excursion
|
|
1647 (if (re-search-backward "\\\\label{\\([^}]+\\)}" nil 1 2)
|
|
1648 (setq look-for (reftex-no-props (match-string 1))))
|
|
1649 (if (or (re-search-forward
|
|
1650 "\\\\\\(include\\|input\\){[^}\n]+}" pos t)
|
|
1651 (re-search-forward reftex-section-regexp pos t)
|
|
1652 (null look-for))
|
|
1653 (setq note "POSITION UNCERTAIN. RESCAN TO FIX.")))
|
|
1654 (if (not look-for)
|
|
1655 (set reftex-list-of-labels-symbol
|
|
1656 (cons (list label typekey text file note)
|
|
1657 (symbol-value reftex-list-of-labels-symbol)))
|
|
1658 (while all
|
|
1659 (setq cell (car all)
|
|
1660 all (cdr all)
|
|
1661 cnt (1+ cnt)
|
|
1662 lab (nth 0 cell))
|
|
1663 (if (string= lab look-for)
|
|
1664 (progn
|
|
1665 (setcdr
|
|
1666 (nthcdr (1- cnt)
|
|
1667 (symbol-value reftex-list-of-labels-symbol))
|
|
1668 (cons (list label typekey text file note)
|
|
1669 (nthcdr
|
|
1670 cnt (symbol-value reftex-list-of-labels-symbol))))
|
|
1671 ;; to end the loop, set all to nil
|
|
1672 (setq all nil)))))))
|
|
1673 ;; return value of the function is the label
|
|
1674 label))
|
|
1675
|
|
1676 (defun reftex-string-to-label (string)
|
|
1677 ;; Convert a string (a sentence) to a label.
|
|
1678 ;;
|
|
1679 ;; Uses reftex-derive-label-parameters and reftex-abbrev-parameters
|
|
1680 ;;
|
|
1681
|
|
1682 (let* ((words0 (reftex-split "[- \t\n\r]+"
|
|
1683 (reftex-no-props string)))
|
|
1684 (ignore-words (nth 5 reftex-derive-label-parameters))
|
|
1685 words word)
|
|
1686
|
|
1687 ;; remove words from the ignore list or with funny characters
|
|
1688 (while words0
|
|
1689 (setq word (car words0) words0 (cdr words0))
|
|
1690 (cond
|
|
1691 ((member (downcase word) ignore-words))
|
|
1692 ((string-match reftex-label-illegal-re word)
|
|
1693 (if (nth 2 reftex-derive-label-parameters)
|
|
1694 (progn
|
|
1695 (while (string-match reftex-label-illegal-re word)
|
|
1696 (setq word (replace-match "" nil nil word)))
|
|
1697 (setq words (cons word words)))))
|
|
1698 (t
|
|
1699 (setq words (cons word words)))))
|
|
1700 (setq words (nreverse words))
|
|
1701
|
|
1702 ;; restrict number of words
|
|
1703 (if (> (length words) (nth 0 reftex-derive-label-parameters))
|
|
1704 (setcdr (nthcdr (1- (nth 0 reftex-derive-label-parameters)) words) nil))
|
|
1705
|
|
1706 ;; First, try to use all words
|
|
1707 (setq string (mapconcat '(lambda(w) w) words
|
|
1708 (nth 4 reftex-derive-label-parameters)))
|
|
1709
|
|
1710 ;; Abbreviate words if enforced by user settings or string length
|
|
1711 (if (or (eq t (nth 3 reftex-derive-label-parameters))
|
|
1712 (and (nth 3 reftex-derive-label-parameters)
|
|
1713 (> (length string) (nth 1 reftex-derive-label-parameters))))
|
|
1714 (setq words
|
|
1715 (mapcar
|
|
1716 '(lambda (w) (if (string-match reftex-abbrev-regexp w)
|
|
1717 (match-string 1 w)
|
|
1718 w))
|
|
1719 words)
|
|
1720 string (mapconcat '(lambda(w) w) words
|
|
1721 (nth 4 reftex-derive-label-parameters))))
|
|
1722
|
|
1723 ;; Shorten if still to long
|
|
1724 (setq string
|
|
1725 (if (> (length string) (nth 1 reftex-derive-label-parameters))
|
|
1726 (substring string 0 (nth 1 reftex-derive-label-parameters))
|
|
1727 string))
|
|
1728
|
|
1729 ;; Delete the final punctuation, if any
|
|
1730 (if (string-match "[^a-zA-Z0-9]+$" string)
|
|
1731 (setq string (replace-match "" nil nil string)))
|
|
1732 string))
|
|
1733
|
|
1734 (defun reftex-label-location (&optional bound)
|
|
1735 ;; Return the environment or macro which determines the label type at point.
|
|
1736 ;; If optional BOUND is an integer, limit backward searches to that point.
|
|
1737
|
|
1738 (let* ((loc1 (reftex-what-macro reftex-label-mac-list bound))
|
|
1739 (loc2 (reftex-what-environment reftex-label-env-list bound))
|
|
1740 (p1 (or (cdr loc1) 0))
|
|
1741 (p2 (or (cdr loc2) 0)))
|
|
1742
|
|
1743 (setq reftex-location-start (max p1 p2))
|
|
1744 (if (> p1 p2)
|
|
1745 (progn
|
|
1746 (setq reftex-default-context-position p1)
|
|
1747 (car loc1))
|
|
1748 (setq reftex-default-context-position
|
|
1749 (+ p2 8 (length (car loc2))))
|
|
1750 (or (car loc2) "section"))))
|
|
1751
|
|
1752
|
|
1753 (defun reftex-next-label-number (type)
|
|
1754 ;; Increment value of automatic labels in current buffer. Return new value.
|
|
1755
|
|
1756 ;; Ensure access to scanning info
|
|
1757 (reftex-access-scan-info)
|
|
1758
|
|
1759 (let ((n (cdr (assoc type (symbol-value reftex-label-numbers-symbol)))))
|
|
1760 (if (not (integerp n))
|
|
1761 ;; oops - type not known - make one here
|
|
1762 (progn
|
|
1763 (set reftex-label-numbers-symbol
|
|
1764 (cons (cons type 0)
|
|
1765 (symbol-value reftex-label-numbers-symbol)))
|
|
1766 (setq n 0)))
|
|
1767 (setq n (1+ n))
|
|
1768 (setcdr (assoc type (symbol-value reftex-label-numbers-symbol)) n)
|
|
1769 n))
|
|
1770
|
|
1771 ;; Help string for the reference label menu
|
|
1772 (defconst reftex-reference-label-help
|
|
1773 " AVAILABLE KEYS IN REFERENCE LABEL MENU
|
|
1774 ======================================
|
|
1775 n / p Go to next/previous label (Cursor motion works as well)
|
|
1776 r / s Rescan document for labels / Switch label type
|
|
1777 t / # Toggle table of contents / Toggle counter mode
|
|
1778 c Toggle display of short context
|
|
1779 SPACE Show full context for current label in other window
|
|
1780 f Toggle follow mode: other window will follow context
|
|
1781 a / q Use last referenced label / Quit without accepting label
|
|
1782 ? / C-r Display this help message / Recursive Edit into other window
|
|
1783 RETURN Accept current label")
|
|
1784
|
|
1785 (defun reftex-reference (&optional type no-insert)
|
|
1786 "Make a LaTeX reference. Look only for labels of a certain TYPE.
|
|
1787 With prefix arg, force to rescan buffer for labels. This should only be
|
|
1788 necessary if you have recently entered labels yourself without using
|
|
1789 reftex-label. Rescanning of the buffer can also be requested from the
|
|
1790 label selection menu.
|
|
1791 The function returns the selected label or nil.
|
|
1792 If NO-INSERT is non-nil, do not insert \\ref command, just return label.
|
|
1793 When called with 2 C-u prefix args, disable magic word recognition."
|
|
1794
|
|
1795 (interactive)
|
|
1796
|
|
1797 ;; check for active recursive edits
|
|
1798 (reftex-check-recursive-edit)
|
|
1799
|
|
1800 ;; Ensure access to scanning info and rescan buffer if prefix are is '(4)
|
|
1801 (reftex-access-scan-info current-prefix-arg)
|
|
1802
|
|
1803 (if (not type)
|
|
1804 ;; guess type from context
|
|
1805 (if (and reftex-guess-label-type
|
|
1806 (not (= 16 (prefix-numeric-value current-prefix-arg)))
|
|
1807 (setq type (assoc (downcase (reftex-word-before-point))
|
|
1808 reftex-words-to-typekey-alist)))
|
|
1809 (setq type (cdr type))
|
|
1810 (setq type (reftex-query-label-type))))
|
|
1811
|
|
1812 (let (label pair
|
|
1813 (form (or (cdr (assoc type reftex-typekey-to-format-alist))
|
|
1814 "\\ref{%s}")))
|
|
1815
|
|
1816 ;; Have the user select a label
|
|
1817 (setq pair (reftex-offer-label-menu type))
|
|
1818 (setq label (car pair))
|
|
1819
|
|
1820 (if (and label
|
|
1821 (not no-insert))
|
|
1822 (progn
|
|
1823 ;; do we need to remove spaces?
|
|
1824 (if (string= "~" (substring form 0 1))
|
|
1825 (while (or (= (preceding-char) ?\ )
|
|
1826 (= (preceding-char) ?\C-i))
|
|
1827 (backward-delete-char 1)))
|
|
1828 ;; ok, insert the reference
|
|
1829 (insert (format form label label))
|
|
1830 (message ""))
|
|
1831 (message "Quit"))
|
|
1832 ;; return the label
|
|
1833 label))
|
|
1834
|
|
1835 (defun reftex-goto-label (&optional arg)
|
|
1836 "Go to a LaTeX label. With prefix ARG: go to label in another window."
|
|
1837 (interactive "P")
|
|
1838 (let (type label file pair)
|
|
1839 (if (not type)
|
|
1840 (setq type (reftex-query-label-type)))
|
|
1841
|
|
1842 (setq pair (reftex-offer-label-menu type)
|
|
1843 label (car pair)
|
|
1844 file (cdr pair))
|
|
1845 (if (and label file (file-exists-p file))
|
|
1846 (progn
|
|
1847 (if arg
|
|
1848 (find-file-other-window file)
|
|
1849 (find-file file))
|
|
1850 (goto-char (point-min))
|
|
1851 (if (not (search-forward (concat "\\label{" label "}") nil t))
|
|
1852 (error "No such label found: %s" label)
|
|
1853 (reftex-highlight 0 (match-beginning 0) (match-end 0))
|
|
1854 (add-hook 'pre-command-hook 'reftex-highlight-shall-die)))
|
|
1855 (message "Quit")
|
|
1856 nil)))
|
|
1857
|
|
1858 ;; Internal list with index numbers of labels in the selection menu
|
|
1859 (defvar reftex-label-index-list nil)
|
|
1860
|
|
1861 (defun reftex-offer-label-menu (typekey)
|
|
1862 ;; Offer a menu with the appropriate labels. Return (label . file).
|
|
1863 (let* ((buf (current-buffer))
|
|
1864 (near-label (reftex-find-nearby-label))
|
|
1865 (toc (reftex-typekey-check typekey reftex-label-menu-flags 0))
|
|
1866 (context (not (reftex-typekey-check
|
|
1867 typekey reftex-label-menu-flags 3)))
|
|
1868 (counter (reftex-typekey-check
|
|
1869 typekey reftex-label-menu-flags 2))
|
|
1870 (follow (reftex-typekey-check
|
|
1871 typekey reftex-label-menu-flags 4))
|
|
1872 offset rtn key cnt entry)
|
|
1873
|
|
1874 (setq reftex-call-back-to-this-buffer buf)
|
|
1875 (setq entry (cons nil nil))
|
|
1876
|
|
1877 (unwind-protect
|
|
1878 (catch 'exit
|
|
1879 (while t
|
|
1880 (save-window-excursion
|
|
1881 (switch-to-buffer-other-window "*RefTeX Select*")
|
|
1882 (erase-buffer)
|
|
1883 (setq truncate-lines t)
|
|
1884 (setq reftex-label-index-list (reftex-make-and-insert-label-list
|
|
1885 typekey buf toc context counter
|
|
1886 near-label))
|
|
1887 (setq near-label "_ ") ; turn off search for near label
|
|
1888 (setq offset (or (car reftex-label-index-list) offset))
|
|
1889 ;; use only when searched
|
|
1890 (setq reftex-label-index-list (cdr reftex-label-index-list))
|
|
1891 ;; only this is the true list
|
|
1892 (if (not reftex-label-index-list)
|
|
1893 (error "No labels of type \"%s\"" typekey))
|
|
1894 (setq rtn
|
|
1895 (reftex-select-item
|
|
1896 nil
|
|
1897 "Label: [n]ext [p]rev [r]escan [t]oc [ ]context [q]uit RETURN [?]HELP+more"
|
|
1898 "^>"
|
|
1899 "\n[^.]"
|
|
1900 2
|
|
1901 reftex-reference-label-help
|
|
1902 '(?r ?c ?t ?s ?# ?a)
|
|
1903 offset
|
|
1904 'reftex-select-label-callback follow))
|
|
1905 (setq key (car rtn)
|
|
1906 cnt (cdr rtn)
|
|
1907 offset (1+ cnt))
|
|
1908 (if (not key) (throw 'exit nil))
|
|
1909 (cond
|
|
1910 ((equal key ?r)
|
|
1911 ;; rescan buffer
|
|
1912 (reftex-parse-document buf))
|
|
1913 ((equal key ?c)
|
|
1914 ;; toggle context mode
|
|
1915 (setq context (not context)))
|
|
1916 ((equal key ?s)
|
|
1917 ;; switch type
|
|
1918 (setq typekey (reftex-query-label-type)))
|
|
1919 ((equal key ?t)
|
|
1920 ;; toggle tabel of contents display
|
|
1921 (setq toc (not toc)))
|
|
1922 ((equal key ?#)
|
|
1923 ;; toggle counter display
|
|
1924 (setq counter (not counter)))
|
|
1925 ((equal key ?a)
|
|
1926 ;; reuse the last referenced label again
|
|
1927 (setq entry reftex-last-used-reference)
|
|
1928 (throw 'exit t))
|
|
1929 (t
|
|
1930 (set-buffer buf)
|
|
1931 (setq entry (nth (nth cnt reftex-label-index-list)
|
|
1932 (symbol-value reftex-list-of-labels-symbol)))
|
|
1933 (setq reftex-last-used-reference entry)
|
|
1934 (throw 'exit t))))))
|
|
1935 (kill-buffer "*RefTeX Select*")
|
|
1936 (reftex-kill-temporary-buffers))
|
|
1937 (cons (reftex-no-props (nth 0 entry)) (nth 3 entry))))
|
|
1938
|
|
1939 ;; Indentation for table of context lines in the menu
|
|
1940 (defconst reftex-toc-indent " ")
|
|
1941 ;; Indentation for the lines containing the label
|
|
1942 (defconst reftex-label-indent "> ")
|
|
1943 ;; Indentation for context lines
|
|
1944 (defconst reftex-context-indent ". ")
|
|
1945 ;; Indentation per section level
|
|
1946 (defvar reftex-level-indent 2
|
|
1947 "*Number of spaces to be used for indentation per section level.
|
|
1948 With more indentation, the label menu looks nicer, but shows less context.
|
|
1949 Changing this is only fully operational after the next buffer scan.")
|
|
1950
|
|
1951 (defun reftex-make-and-insert-label-list (typekey0 buf toc context
|
|
1952 counter near-label)
|
|
1953 ;; Insert a menu of all labels in buffer BUF into current buffer.
|
|
1954 ;; Return the list of labels, with the index of NEAR-LABEL as extra car.
|
|
1955 (let (ins-list index-list offset)
|
|
1956 (save-excursion
|
|
1957 (set-buffer buf)
|
|
1958 (let* ((all nil)
|
|
1959 (font (reftex-use-fonts))
|
|
1960 (cnt 0)
|
|
1961 (file nil)
|
|
1962 (index -1)
|
|
1963 (toc-indent reftex-toc-indent)
|
|
1964 (label-indent
|
|
1965 (concat reftex-label-indent
|
|
1966 (if toc (make-string (* 7 reftex-level-indent) ?\ ) "")))
|
|
1967 (context-indent
|
|
1968 (concat reftex-context-indent
|
|
1969 (if toc (make-string (* 7 reftex-level-indent) ?\ ) "")))
|
|
1970 cell text label typekey note comment)
|
|
1971
|
|
1972 ; Ensure access to scanning info
|
|
1973 (reftex-access-scan-info)
|
|
1974
|
|
1975 (setq all (symbol-value reftex-list-of-labels-symbol))
|
|
1976
|
|
1977 (while all
|
|
1978
|
|
1979 (setq index (1+ index)
|
|
1980 cell (car all)
|
|
1981 all (cdr all))
|
|
1982
|
|
1983 (if (null (nth 2 cell))
|
|
1984 ;; No context yet. Quick update
|
|
1985 (progn
|
|
1986 (setq cell (reftex-label-info-update cell))
|
|
1987 (setcar (nthcdr index
|
|
1988 (symbol-value reftex-list-of-labels-symbol))
|
|
1989 cell)))
|
|
1990
|
|
1991 ;; in the following setq we *copy* the label, since we will change
|
|
1992 ;; its properties, and we cannot have any properties in the list
|
|
1993 ;; (because of assoc searches)
|
|
1994 (setq label (copy-sequence (nth 0 cell))
|
|
1995 typekey (nth 1 cell)
|
|
1996 text (nth 2 cell)
|
|
1997 file (nth 3 cell)
|
|
1998 note (nth 4 cell)
|
|
1999 comment (get-text-property 0 'in-comment text))
|
|
2000
|
|
2001 (if (string= label near-label)
|
|
2002 (setq offset (1+ cnt)))
|
|
2003
|
|
2004 (cond
|
|
2005 ((and toc (string= typekey "toc"))
|
|
2006 (setq ins-list
|
|
2007 (cons (concat toc-indent text "\n")
|
|
2008 ins-list)))
|
|
2009 ((string= typekey "toc"))
|
|
2010 ((and (or (string= typekey typekey0) (string= typekey0 " "))
|
|
2011 (or (nth 5 reftex-label-menu-flags) ; show-commented?
|
|
2012 (null comment)))
|
|
2013 (setq cnt (1+ cnt))
|
|
2014 (if comment (setq label (concat "% " label)))
|
|
2015 (if font
|
|
2016 (put-text-property
|
|
2017 0 (length label)
|
|
2018 'face
|
|
2019 (if comment
|
|
2020 'font-lock-comment-face
|
|
2021 'font-lock-reference-face)
|
|
2022 label))
|
|
2023 (setq index-list (cons index index-list))
|
|
2024 (setq ins-list
|
|
2025 (cons (concat
|
|
2026 label-indent
|
|
2027 label
|
|
2028 (if counter (format " (%d) " cnt))
|
|
2029 (if comment " LABEL IS COMMENTED OUT ")
|
|
2030 (if note (concat " " note) "")
|
|
2031 "\n"
|
|
2032 (if context (concat context-indent text "\n")))
|
|
2033 ins-list))))
|
|
2034 )))
|
|
2035
|
|
2036 (apply 'insert (nreverse ins-list))
|
|
2037 (cons offset (nreverse index-list))))
|
|
2038
|
|
2039 (defun reftex-query-label-type ()
|
|
2040 ;; Ask for label type
|
|
2041 (message reftex-type-query-prompt)
|
|
2042 (let ((key (read-char)))
|
|
2043 (if (equal key ?\?)
|
|
2044 (progn
|
|
2045 (save-window-excursion
|
|
2046 (with-output-to-temp-buffer "*RefTeX Help*"
|
|
2047 (princ reftex-type-query-help))
|
|
2048 (setq key (read-char))
|
|
2049 (kill-buffer "*RefTeX Help*"))))
|
|
2050 (if (not (member (char-to-string key) reftex-typekey-list))
|
|
2051 (error "No such label type: %s" (char-to-string key)))
|
|
2052 (char-to-string key)))
|
|
2053
|
|
2054 (defun reftex-find-nearby-label ()
|
|
2055 ;; Find a nearby label.
|
|
2056 (save-excursion
|
|
2057 (if (or (re-search-backward "\\\\label{\\([^}]+\\)}" nil t)
|
|
2058 (re-search-forward "\\\\label{\\([^}]+\\)}" nil t))
|
|
2059 (reftex-no-props (match-string 1))
|
|
2060 nil)))
|
|
2061
|
|
2062 ;; Variable holding the vector with section numbers
|
|
2063 (defvar reftex-section-numbers [0 0 0 0 0 0 0 0])
|
|
2064
|
|
2065 (defun reftex-scan-buffer-for-labels (label-numbers-symbol label-list-symbol)
|
|
2066 ;; Scan the buffer for labels and save them in a list.
|
|
2067 (save-excursion
|
|
2068 (let ((regexp (concat "\\\\label{\\([^}]*\\)}" "\\|"
|
|
2069 reftex-section-regexp))
|
|
2070 (font (reftex-use-fonts))
|
|
2071 (bound 0)
|
|
2072 (highest-level 100)
|
|
2073 file (level 1) start star text text1 label section-number macro find)
|
|
2074 (set label-list-symbol nil)
|
|
2075 (goto-char 0)
|
|
2076
|
|
2077 ;; reset label numbers
|
|
2078 (set label-numbers-symbol
|
|
2079 (mapcar '(lambda(x) (cons x 0)) reftex-typekey-list))
|
|
2080
|
|
2081 ;; reset section numbers
|
|
2082 (reftex-section-number reftex-section-numbers -1)
|
|
2083
|
|
2084 (while (re-search-forward regexp nil t)
|
|
2085 (setq file (get-text-property (match-beginning 0) 'file))
|
|
2086 (if (string= (buffer-substring (match-beginning 0)
|
|
2087 (+ 7 (match-beginning 0))) "\\label{")
|
|
2088 ;; It is a label
|
|
2089 (progn
|
|
2090 (setq label (reftex-no-props (match-string 1)))
|
|
2091 (set label-list-symbol
|
|
2092 (cons (reftex-label-info label file bound)
|
|
2093 (symbol-value label-list-symbol))))
|
|
2094
|
|
2095 ;; It is a section
|
|
2096 (setq bound (point))
|
|
2097 (setq star (= ?* (char-after (match-end 2))))
|
|
2098 (setq find (buffer-substring-no-properties
|
|
2099 (1- (match-beginning 2)) (match-end 0)))
|
|
2100 (setq macro (reftex-no-props (match-string 2)))
|
|
2101 (setq level (cdr (assoc macro reftex-section-levels)))
|
|
2102
|
|
2103 (setq section-number (reftex-section-number
|
|
2104 reftex-section-numbers level star))
|
|
2105 (setq highest-level (min highest-level level))
|
|
2106 (if (= level highest-level)
|
|
2107 (message
|
|
2108 "Scanning %s %s ..."
|
|
2109 (car (nth level reftex-section-levels))
|
|
2110 section-number))
|
|
2111
|
|
2112 ;; get the title
|
|
2113 (save-match-data
|
|
2114 (setq text1 (reftex-context-substring))
|
|
2115 (setq text (reftex-nicify-text text1)))
|
|
2116
|
|
2117 (setq find (reftex-allow-for-ctrl-m (concat find text1)))
|
|
2118
|
|
2119 ;; add section number and indentation
|
|
2120 (setq text
|
|
2121 (concat
|
|
2122 (make-string (* reftex-level-indent level) ?\ )
|
|
2123 (if (nth 1 reftex-label-menu-flags) ; section number flag
|
|
2124 (concat section-number " "))
|
|
2125 text))
|
|
2126 ;; fontify
|
|
2127 (if font (put-text-property 0 (length text)
|
|
2128 'face 'font-lock-comment-face text))
|
|
2129
|
|
2130 ;; insert in list
|
|
2131 (set label-list-symbol
|
|
2132 (cons (list nil "toc" text file find)
|
|
2133 (symbol-value label-list-symbol)))))
|
|
2134 (set label-list-symbol
|
|
2135 (nreverse (symbol-value label-list-symbol))))))
|
|
2136
|
|
2137
|
|
2138 (defun reftex-label-info-update (cell)
|
|
2139 ;; Update information about just one label in a different file.
|
|
2140 ;; CELL contains the old info list
|
|
2141 (let* ((label (nth 0 cell))
|
|
2142 (typekey (nth 1 cell))
|
|
2143 (text (nth 2 cell))
|
|
2144 (file (nth 3 cell))
|
|
2145 (note (nth 4 cell))
|
|
2146 (buf (reftex-get-file-buffer-force
|
|
2147 file (not reftex-keep-temporary-buffers))))
|
|
2148 (if (not buf)
|
|
2149 (list label typekey "" file "LOST LABEL. RESCAN TO FIX.")
|
|
2150 (save-excursion
|
|
2151 (set-buffer buf)
|
|
2152 (save-restriction
|
|
2153 (widen)
|
|
2154 (goto-char 1)
|
|
2155
|
|
2156 (if (re-search-forward (concat "\\\\label{" (regexp-quote label) "}")
|
|
2157 nil t)
|
|
2158 (append (reftex-label-info label file) (list note))
|
|
2159 (list label typekey "" file "LOST LABEL. RESCAN TO FIX.")))))))
|
|
2160
|
|
2161 (defun reftex-label-info (label &optional file bound)
|
|
2162 ;; Return info list on LABEL at point.
|
|
2163 (let* ((env-or-mac (reftex-label-location bound))
|
|
2164 (typekey (nth 1 (assoc env-or-mac reftex-env-or-mac-alist)))
|
|
2165 (file (or file (buffer-file-name)))
|
|
2166 (parse (if (reftex-typekey-check
|
|
2167 typekey reftex-use-text-after-label-as-context)
|
|
2168 nil
|
|
2169 (nth 2 (assoc env-or-mac reftex-env-or-mac-alist))))
|
|
2170 (text (reftex-short-context env-or-mac parse reftex-location-start)))
|
|
2171 (if (reftex-in-comment)
|
|
2172 (put-text-property 0 1 'in-comment t text))
|
|
2173 (list label typekey text file)))
|
|
2174
|
|
2175 (defun reftex-in-comment ()
|
|
2176 (save-excursion
|
|
2177 (skip-chars-backward "^%\n\r")
|
|
2178 (= (preceding-char) ?%)))
|
|
2179
|
|
2180 (defun reftex-short-context (env parse &optional bound)
|
|
2181 ;; Get about one line of useful context for the label definition at point.
|
|
2182
|
|
2183 (reftex-nicify-text
|
|
2184
|
|
2185 (cond
|
|
2186
|
|
2187 ((null parse)
|
|
2188 (save-excursion
|
|
2189 (reftex-context-substring)))
|
|
2190
|
|
2191 ((eq parse t)
|
|
2192 (if (string= env "section")
|
|
2193 ;; special treatment for section labels
|
|
2194 (save-excursion
|
|
2195 (if (re-search-backward reftex-section-regexp (point-min) t)
|
|
2196 (progn
|
|
2197 (goto-char (match-end 0))
|
|
2198 (reftex-context-substring))
|
|
2199 "SECTION HEADING NOT FOUND"))
|
|
2200 (save-excursion
|
|
2201 (goto-char reftex-default-context-position)
|
|
2202 (reftex-context-substring))))
|
|
2203
|
|
2204 ((stringp parse)
|
|
2205 (save-excursion
|
|
2206 (if (re-search-backward parse bound t)
|
|
2207 (progn
|
|
2208 (goto-char (match-end 0))
|
|
2209 (reftex-context-substring))
|
|
2210 "NO MATCH FOR CONTEXT REGEXP")))
|
|
2211 ((fboundp parse)
|
|
2212 ;; A hook function. Call it.
|
|
2213 (save-excursion
|
|
2214 (condition-case error-var
|
|
2215 (funcall parse env)
|
|
2216 ('error (format "HOOK ERROR: %s" (cdr error-var))))))
|
|
2217 (t
|
|
2218 "ILLEGAL VALUE OF PARSE"))))
|
|
2219
|
|
2220 (defun reftex-context-substring ()
|
|
2221 ;; Return up to 100 chars from point
|
|
2222 ;; When point is just after a { or [, limit string to matching parenthesis
|
|
2223 (cond
|
|
2224 ((or (= (preceding-char) ?\{)
|
|
2225 (= (preceding-char) ?\[))
|
|
2226 ;; inside a list - get only the list, with modified syntax to be perfect
|
|
2227 (buffer-substring
|
|
2228 (point)
|
|
2229 (min (+ 100 (point))
|
|
2230 (point-max)
|
|
2231 (condition-case nil
|
|
2232 (progn
|
|
2233 (up-list 1)
|
|
2234 (1- (point)))
|
|
2235 ('error (point-max))))))
|
|
2236 (t
|
|
2237 ;; no list - just grab 100 characters
|
|
2238 (buffer-substring (point) (min (+ 100 (point)) (point-max))))))
|
|
2239
|
|
2240 (defun reftex-section-number (section-numbers &optional level star)
|
|
2241 ;; Return a string with the current section number.
|
|
2242 ;; When LEVEL is non-nil, increase section numbers on that level.
|
|
2243 (let* ((depth 6) idx n (string ""))
|
|
2244 (if level
|
|
2245 (progn
|
|
2246 (if (and (> level -1) (not star))
|
|
2247 (aset section-numbers level (1+ (aref section-numbers level))))
|
|
2248 (setq idx (1+ level))
|
|
2249 (while (<= idx depth)
|
|
2250 (aset section-numbers idx 0)
|
|
2251 (setq idx (1+ idx)))))
|
|
2252 (setq idx 0)
|
|
2253 (while (<= idx depth)
|
|
2254 (setq n (aref section-numbers idx))
|
|
2255 (setq string (concat string (if (not (string= string "")) "." "")
|
|
2256 (int-to-string n)))
|
|
2257 (setq idx (1+ idx)))
|
|
2258 (save-match-data
|
|
2259 (if (string-match "\\`\\(0\\.\\)+" string)
|
|
2260 (setq string (replace-match "" nil nil string)))
|
|
2261 (if (string-match "\\(\\.0\\)+\\'" string)
|
|
2262 (setq string (replace-match "" nil nil string))))
|
|
2263 (if star
|
|
2264 (concat (make-string (1- (length string)) ?\ ) "*")
|
|
2265 string)))
|
|
2266
|
|
2267 ;; A variable to remember the index of the last label context shown
|
|
2268 (defvar reftex-last-cnt 0)
|
|
2269
|
|
2270 (defun reftex-select-label-callback (cnt)
|
|
2271 ;; Callback function called from the label selection in order to
|
|
2272 ;; show context in another window
|
|
2273 (let* ((this-window (selected-window))
|
|
2274 index entry label file buffer)
|
|
2275 ;; pop to original buffer in order to get correct variables
|
|
2276 (catch 'exit
|
|
2277 (save-excursion
|
|
2278 (set-buffer reftex-call-back-to-this-buffer)
|
|
2279 (setq index (nth (or cnt 1) reftex-label-index-list)
|
|
2280 entry (nth index (symbol-value reftex-list-of-labels-symbol))
|
|
2281 label (nth 0 entry)
|
|
2282 file (nth 3 entry)))
|
|
2283
|
|
2284 ;; goto the file in another window
|
|
2285 (setq buffer (reftex-get-file-buffer-force
|
|
2286 file (not reftex-keep-temporary-buffers)))
|
|
2287 (if buffer
|
|
2288 ;; good - the file is available
|
|
2289 (switch-to-buffer-other-window buffer)
|
|
2290 ;; we have got a problem here. The file does not exist.
|
|
2291 ;; Let' get out of here..
|
|
2292 (ding)
|
|
2293 (throw 'exit nil))
|
|
2294
|
|
2295
|
|
2296 ;; search for that label
|
|
2297 (if (not (and (integerp cnt)
|
|
2298 (integerp reftex-last-cnt)
|
|
2299 (if (> cnt reftex-last-cnt)
|
|
2300 (search-forward (concat "\\label{" label "}") nil t)
|
|
2301 (search-backward (concat "\\label{" label "}") nil t))))
|
|
2302 (progn
|
|
2303 (goto-char 1)
|
|
2304 (search-forward (concat "\\label{" label "}") nil t)))
|
|
2305 (reftex-highlight 0 (match-beginning 0) (match-end 0))
|
|
2306 (reftex-show-entry)
|
|
2307 (recenter (/ (window-height) 2))
|
|
2308 (select-window this-window))))
|
|
2309
|
|
2310 (defun reftex-pop-to-label (label file-list &optional mark-to-kill highlight)
|
|
2311 ;; Find LABEL in any file in FILE-LIST in another window.
|
|
2312 ;; If mark-to-kill is non-nil, mark new buffer for killing.
|
|
2313 ;; If HIGHLIGHT is non-nil, highlight the label definition.
|
|
2314 (let* ((re (concat "\\\\label{" (regexp-quote label) "}"))
|
|
2315 file buf)
|
|
2316 (catch 'exit
|
|
2317 (while file-list
|
|
2318 (setq file (car file-list)
|
|
2319 file-list (cdr file-list))
|
|
2320 (if (not (setq buf (reftex-get-file-buffer-force file mark-to-kill)))
|
|
2321 (error "No such file %s" file))
|
|
2322 (set-buffer buf)
|
|
2323 (widen)
|
|
2324 (goto-char (point-min))
|
|
2325 (if (re-search-forward re nil t)
|
|
2326 (progn
|
|
2327 (switch-to-buffer-other-window buf)
|
|
2328 (goto-char (match-beginning 0))
|
|
2329 (recenter (/ (window-height) 2))
|
|
2330 (if highlight
|
|
2331 (reftex-highlight 0 (match-beginning 0) (match-end 0)))
|
|
2332 (throw 'exit (selected-window)))))
|
|
2333 (error "Label %s not found" label))))
|
|
2334
|
|
2335 (defun reftex-find-duplicate-labels ()
|
|
2336 "Produce a list of all duplicate labels in the document."
|
|
2337
|
|
2338 (interactive)
|
|
2339
|
|
2340 ;; Rescan the document to make sure
|
|
2341 (reftex-access-scan-info t)
|
|
2342
|
|
2343 (let ((master (reftex-TeX-master-file))
|
|
2344 (dlist
|
|
2345 (mapcar
|
|
2346 '(lambda(x)
|
|
2347 (let (x1)
|
|
2348 (cond
|
|
2349 ((car x)
|
|
2350 (setq x1 (reftex-all-assoc-string
|
|
2351 (car x) (symbol-value reftex-list-of-labels-symbol)))
|
|
2352 (if (< 1 (length x1))
|
|
2353 (append (list (reftex-no-props (car x)))
|
|
2354 (mapcar '(lambda(x)
|
|
2355 (abbreviate-file-name (nth 3 x))) x1))
|
|
2356 (list nil)))
|
|
2357 (t nil))))
|
|
2358 (reftex-uniquify (symbol-value reftex-list-of-labels-symbol)))))
|
|
2359 (setq dlist (reftex-uniquify dlist))
|
|
2360 (if (null dlist) (error "No duplicate labels in document"))
|
|
2361 (switch-to-buffer-other-window "*Help*")
|
|
2362 (make-local-variable 'TeX-master)
|
|
2363 (setq TeX-master master)
|
|
2364 (erase-buffer)
|
|
2365 (insert " MULTIPLE LABELS IN CURRENT DOCUMENT:\n")
|
|
2366 (insert " Move point to label and type `M-x reftex-change-label'\n"
|
|
2367 " This will run a query-replace on the label and its references\n")
|
|
2368 (insert " LABEL FILE\n")
|
|
2369 (insert " -------------------------------------------------------------\n")
|
|
2370 (while dlist
|
|
2371 (if (and (car (car dlist))
|
|
2372 (cdr (car dlist)))
|
|
2373 (progn
|
|
2374 (insert (mapconcat '(lambda(x) x) (car dlist) "\n ") "\n")))
|
|
2375 (setq dlist (cdr dlist)))
|
|
2376 (goto-char (point-min))))
|
|
2377
|
|
2378 (defun reftex-all-assoc-string (key list)
|
|
2379 ;; Return a list of all associations of KEY in LIST. Comparison with string=
|
|
2380 (let (rtn)
|
|
2381 (while list
|
|
2382 (if (string= (car (car list)) key)
|
|
2383 (setq rtn (cons (car list) rtn)))
|
|
2384 (setq list (cdr list)))
|
|
2385 (nreverse rtn)))
|
|
2386
|
|
2387 (defun reftex-kill-temporary-buffers ()
|
|
2388 ;; Kill all buffers in the list reftex-kill-temporary-buffers.
|
|
2389 (while reftex-buffers-to-kill
|
|
2390 (if (bufferp (car reftex-buffers-to-kill))
|
|
2391 (progn
|
|
2392 (and (buffer-modified-p (car reftex-buffers-to-kill))
|
|
2393 (y-or-n-p (format "Save file %s? "
|
|
2394 (buffer-file-name
|
|
2395 (car reftex-buffers-to-kill))))
|
|
2396 (save-excursion
|
|
2397 (set-buffer (car reftex-buffers-to-kill))
|
|
2398 (save-buffer)))
|
|
2399 (kill-buffer (car reftex-buffers-to-kill))))
|
|
2400 (setq reftex-buffers-to-kill (cdr reftex-buffers-to-kill))))
|
|
2401
|
|
2402 (defun reftex-show-entry ()
|
|
2403 ;; Show entry if point is hidden by outline mode
|
|
2404 (let ((pos (point)))
|
|
2405 (if (and reftex-auto-show-entry
|
|
2406 (boundp 'outline-minor-mode)
|
|
2407 outline-minor-mode
|
|
2408 (looking-at "[^\n\r]*\r"))
|
|
2409 (progn
|
|
2410 (outline-back-to-heading)
|
|
2411 (show-entry)
|
|
2412 (goto-char pos)))))
|
|
2413
|
|
2414
|
|
2415 (defun reftex-nicify-text (text)
|
|
2416 ;; Make TEXT nice for inclusion into label menu
|
|
2417 (while (string-match "[\n\r\t]\\|[ \t][ \t]+" text) ; remove extra whitespace
|
|
2418 (setq text (replace-match " " nil t text)))
|
|
2419 (if (string-match "\\\\end{.*" text) ; nothing beyond \end{
|
|
2420 (setq text (replace-match "" nil t text)))
|
|
2421 (if (string-match "\\\\label{[^}]*}" text) ; kill the label
|
|
2422 (setq text (replace-match "" nil t text)))
|
|
2423 (if (string-match "^ +" text) ; leading whitespace
|
|
2424 (setq text (replace-match "" nil t text)))
|
|
2425 (cond
|
|
2426 ((> (length text) 100) ; not to long
|
|
2427 (setq text (substring text 0 100)))
|
|
2428 ((= (length text) 0) ; not empty
|
|
2429 (setq text " ")))
|
|
2430 text)
|
|
2431
|
|
2432 (defun reftex-typekey-check (typekey conf-variable &optional n)
|
|
2433 ;; Check if CONF-VARIABLE is true or contains TYPEKEY
|
|
2434 (and n (setq conf-variable (nth n conf-variable)))
|
|
2435 (or (equal conf-variable t)
|
|
2436 (and (stringp conf-variable)
|
|
2437 (string-match (concat "[" conf-variable "]") typekey))))
|
|
2438
|
|
2439 ;;; ===========================================================================
|
|
2440 ;;;
|
|
2441 ;;; Table of contents (contributed from Stephen Eglen, changed by C. Dominik)
|
|
2442
|
|
2443 ;; We keep at most one *toc* buffer - it is easy to make them
|
|
2444
|
|
2445 (defvar reftex-last-toc-master nil
|
|
2446 "Stores the name of the tex file that `reftex-toc' was last run on.")
|
|
2447
|
|
2448 (defvar reftex-last-toc-file nil
|
|
2449 "Stores the file name from which `reftex-toc' was called. For redo command.")
|
|
2450
|
|
2451 (defvar reftex-toc-return-marker (make-marker)
|
|
2452 "Marker which makes it possible to return from toc to old position.")
|
|
2453
|
|
2454 (defun reftex-toc ()
|
|
2455 "Show the table of contents for the current document.
|
|
2456 To see the corresponding part of the LaTeX document, use within the
|
|
2457 *toc* buffer:
|
|
2458
|
|
2459 SPC Show the corresponding section of the LaTeX document
|
|
2460 RET Goto the section and hide the *toc* buffer
|
|
2461 q Hide the *toc* window and return to position of last reftex-toc command
|
|
2462 Q Kill the *toc* buffer and return to position of last reftex-toc command
|
|
2463 f Toggle follow mode on and off
|
|
2464
|
|
2465 When called with a raw C-u prefix, rescan the document first."
|
|
2466
|
|
2467 (interactive)
|
|
2468
|
|
2469 (and (not (string= reftex-last-toc-master (reftex-TeX-master-file)))
|
|
2470 (get-buffer "*toc*")
|
|
2471 (kill-buffer "*toc*"))
|
|
2472
|
|
2473 (setq reftex-last-toc-file (buffer-file-name))
|
|
2474 (setq reftex-last-toc-master (reftex-TeX-master-file))
|
|
2475
|
|
2476 (set-marker reftex-toc-return-marker (point))
|
|
2477
|
|
2478 ;; if follow mode is active, arrange to delay it one command
|
|
2479 (if reftex-toc-follow-mode
|
|
2480 (setq reftex-toc-follow-mode 1))
|
|
2481
|
|
2482 (if (and current-prefix-arg
|
|
2483 (get-buffer "*toc*"))
|
|
2484 (kill-buffer "*toc*"))
|
|
2485
|
|
2486 ;; Ensure access to scanning info and rescan buffer if prefix are is '(4)
|
|
2487 (reftex-access-scan-info current-prefix-arg)
|
|
2488
|
|
2489 (let* ((all (symbol-value reftex-list-of-labels-symbol))
|
|
2490 (where (reftex-nearest-section))
|
|
2491 toc toc1 cell label file find startpos)
|
|
2492
|
|
2493 (if (and (get-buffer "*toc*")
|
|
2494 (get-buffer-window (get-buffer "*toc*")))
|
|
2495 (select-window (get-buffer-window (get-buffer "*toc*")))
|
|
2496 (delete-other-windows)
|
|
2497 (switch-to-buffer-other-window (current-buffer))
|
|
2498 (switch-to-buffer-other-window (get-buffer-create "*toc*")))
|
|
2499
|
|
2500 (cond
|
|
2501 ;; buffer is empty - fill it with the table of contents
|
|
2502 ((= (buffer-size) 0)
|
|
2503
|
|
2504 (local-set-key " " 'reftex-toc-view-line)
|
|
2505 (local-set-key "\C-m" 'reftex-toc-goto-line-and-hide)
|
|
2506 (local-set-key "r" 'reftex-toc-redo)
|
|
2507 (local-set-key "q" 'reftex-toc-quit)
|
|
2508 (local-set-key "Q" 'reftex-toc-quit-and-kill)
|
|
2509 (local-set-key "f" 'reftex-toc-toggle-follow)
|
|
2510 (setq truncate-lines t)
|
|
2511 (make-local-hook 'post-command-hook)
|
|
2512 (make-local-hook 'pre-command-hook)
|
|
2513 (setq post-command-hook '(reftex-toc-post-command-hook))
|
|
2514 (setq pre-command-hook '(reftex-toc-pre-command-hook))
|
|
2515
|
|
2516 (insert (format
|
|
2517 "TABLE-OF-CONTENTS on %s
|
|
2518 MENU: SPC=view RET=goto [q]uit [Q]uit+kill [r]escan [f]ollow-mode on/off
|
|
2519 -------------------------------------------------------------------------------
|
|
2520 " (abbreviate-file-name reftex-last-toc-master)))
|
|
2521 (setq startpos (point))
|
|
2522
|
|
2523 (if (reftex-use-fonts)
|
|
2524 (put-text-property 1 (point) 'face 'font-lock-keyword-face))
|
|
2525 (put-text-property 1 (point) 'intangible t)
|
|
2526
|
|
2527 (while all
|
|
2528 (setq cell (car all)
|
|
2529 all (cdr all))
|
|
2530 (setq label (nth 0 cell)
|
|
2531 toc (nth 2 cell)
|
|
2532 file (nth 3 cell)
|
|
2533 find (nth 4 cell))
|
|
2534 (if (not label)
|
|
2535 (progn
|
|
2536 (setq toc1 (concat toc "\n"))
|
|
2537 (put-text-property 0 (length toc1)
|
|
2538 'file file toc1)
|
|
2539 (put-text-property 0 (length toc1)
|
|
2540 'find find toc1)
|
|
2541 (insert toc1)
|
|
2542 )))
|
|
2543
|
|
2544 (backward-delete-char 1)
|
|
2545
|
|
2546 (setq buffer-read-only t))
|
|
2547 (t
|
|
2548 (goto-line 3)
|
|
2549 (beginning-of-line)
|
|
2550 (setq startpos (point))))
|
|
2551
|
|
2552 ;; Find the correct section
|
|
2553 (goto-char (point-max))
|
|
2554 (beginning-of-line)
|
|
2555 (while (and (> (point) startpos)
|
|
2556 (or (not (string= (get-text-property (point) 'file)
|
|
2557 (car where)))
|
|
2558 (not (string= (get-text-property (point) 'find)
|
|
2559 (cdr where)))))
|
|
2560 (beginning-of-line 0))))
|
|
2561
|
|
2562 (defun reftex-nearest-section ()
|
|
2563 ;; Return (file . find) of nearest section command
|
|
2564 (let (cell label rest)
|
|
2565 (save-excursion
|
|
2566 (cond
|
|
2567 ;; Try to find a section heading
|
|
2568 ((or (re-search-backward reftex-section-regexp nil t)
|
|
2569 (re-search-forward reftex-section-regexp nil t))
|
|
2570 (goto-char (match-end 0))
|
|
2571 (cons (buffer-file-name)
|
|
2572 (reftex-allow-for-ctrl-m
|
|
2573 (concat (buffer-substring-no-properties
|
|
2574 (1- (match-beginning 1)) (match-end 0))
|
|
2575 (reftex-context-substring)))))
|
|
2576 ;; Try to find a label
|
|
2577 ((and (or (re-search-backward "\\\\label{\\([^}]+\\)}" nil t)
|
|
2578 (re-search-forward "\\\\label{\\([^}]+\\)}" nil t))
|
|
2579 (setq label (reftex-no-props (match-string 1)))
|
|
2580 (setq cell (assoc label (symbol-value
|
|
2581 reftex-list-of-labels-symbol)))
|
|
2582 (setq rest (memq cell (symbol-value reftex-list-of-labels-symbol)))
|
|
2583 (setq cell (car (memq (assoc nil rest) rest)))
|
|
2584 (null (car cell)))
|
|
2585 (cons (nth 3 cell) (nth 4 cell)))
|
|
2586 (t nil)))))
|
|
2587
|
|
2588 (defun reftex-toc-pre-command-hook ()
|
|
2589 ;; used as pre command hook in *toc* buffer
|
|
2590 (reftex-unhighlight 0)
|
|
2591 (reftex-unhighlight 1))
|
|
2592
|
|
2593 (defun reftex-toc-post-command-hook ()
|
|
2594 ;; used in the post-command-hook for the *toc* buffer
|
|
2595 (and (> (point) 1)
|
|
2596 (save-excursion
|
|
2597 (reftex-highlight 1
|
|
2598 (progn (beginning-of-line) (point))
|
|
2599 (progn (end-of-line) (point)))))
|
|
2600 (cond
|
|
2601 ((integerp reftex-toc-follow-mode)
|
|
2602 ;; remove delayed action
|
|
2603 (setq reftex-toc-follow-mode t))
|
|
2604 (reftex-toc-follow-mode
|
|
2605 ;; show context in other window
|
|
2606 (condition-case nil
|
|
2607 (reftex-toc-visit-line)
|
|
2608 ('error t)))))
|
|
2609
|
|
2610 (defun reftex-toc-toggle-follow ()
|
|
2611 "Toggle toc-follow mode.
|
|
2612 (it is not really a mode, just a flag)."
|
|
2613 (interactive)
|
|
2614 (setq reftex-toc-follow-mode (not reftex-toc-follow-mode)))
|
|
2615 (defun reftex-toc-view-line ()
|
|
2616 "View document location in other window."
|
|
2617 (interactive)
|
|
2618 (reftex-toc-visit-line))
|
|
2619 (defun reftex-toc-goto-line-and-hide ()
|
|
2620 "Go to document location in other window. Hide the *toc* window."
|
|
2621 (interactive)
|
|
2622 (reftex-toc-visit-line 'hide))
|
|
2623 (defun reftex-toc-quit ()
|
|
2624 "Hide the *toc* window and do not move point."
|
|
2625 (interactive)
|
|
2626 (delete-window)
|
|
2627 (switch-to-buffer (marker-buffer reftex-toc-return-marker))
|
|
2628 (goto-char (marker-position reftex-toc-return-marker)))
|
|
2629 (defun reftex-toc-quit-and-kill ()
|
|
2630 "Kill the *toc* buffer."
|
|
2631 (interactive)
|
|
2632 (kill-buffer "*toc*")
|
|
2633 (delete-window)
|
|
2634 (switch-to-buffer (marker-buffer reftex-toc-return-marker))
|
|
2635 (goto-char (marker-position reftex-toc-return-marker)))
|
|
2636 (defun reftex-toc-redo ()
|
|
2637 "Regenerate the *toc* buffer. Call only from within the *toc* buffer"
|
|
2638 (interactive)
|
|
2639 (switch-to-buffer (reftex-get-file-buffer-force reftex-last-toc-file))
|
|
2640 (delete-other-windows)
|
|
2641 (setq current-prefix-arg '(4))
|
|
2642 (reftex-toc))
|
|
2643
|
|
2644 (defun reftex-toc-visit-line (&optional final)
|
|
2645 ;; Visit the tex file corresponding to the toc entry on the current line.
|
|
2646 ;; If FINAL is t, stay there
|
|
2647 ;; If FINAL is 'hide, hide the *toc* window.
|
|
2648 ;; Otherwise, move cursor back into *toc* window
|
|
2649
|
|
2650 (let (file find beg end (toc-window (selected-window)) show-window)
|
|
2651 (save-excursion
|
|
2652 (beginning-of-line)
|
|
2653 (setq beg (point))
|
|
2654 (end-of-line)
|
|
2655 (setq end (point)))
|
|
2656
|
|
2657 ;; get the file and the search string
|
|
2658 (setq file (get-text-property (point) 'file))
|
|
2659 (setq find (get-text-property (point) 'find))
|
|
2660 (if (or (not file) (not find))
|
|
2661 (error "Cannot visit line"))
|
|
2662
|
|
2663 (switch-to-buffer-other-window (reftex-get-file-buffer-force file))
|
|
2664 (setq show-window (selected-window))
|
|
2665 (goto-char (point-min))
|
|
2666
|
|
2667 (if (not (re-search-forward find nil t))
|
|
2668 (error "Cannot visit line"))
|
|
2669
|
|
2670 (setq beg (match-beginning 0)
|
|
2671 end (match-end 0))
|
|
2672
|
|
2673 (goto-char beg)
|
|
2674 (recenter 1)
|
|
2675 (reftex-highlight 0 beg end (current-buffer))
|
|
2676
|
|
2677 (select-window toc-window)
|
|
2678
|
|
2679 ;; use the `final' parameter to decide what to do next
|
|
2680 (cond
|
|
2681 ((equal final t)
|
|
2682 (reftex-unhighlight 0)
|
|
2683 (select-window show-window))
|
|
2684 ((eq final 'hide)
|
|
2685 (reftex-unhighlight 0)
|
|
2686 (delete-window))
|
|
2687 (t nil))))
|
|
2688
|
|
2689 ;;; ===========================================================================
|
|
2690 ;;;
|
|
2691 ;;; BibTeX citations.
|
|
2692
|
|
2693 ;; Variables and constants
|
|
2694
|
|
2695 ;; Internal variable, but used from different functions
|
|
2696 (defvar reftex-cite-format1 nil)
|
|
2697
|
|
2698 ;; The history list of regular expressions used for citations
|
|
2699 (defvar reftex-cite-regexp-hist nil)
|
|
2700
|
|
2701 ;; Help string for citation selection
|
|
2702 (defconst reftex-citation-help
|
|
2703 "AVAILABLE KEYS IN MAKE CITATION MENU
|
|
2704 ---------------------------------------
|
|
2705 n / p Go to next/previous entry (Cursor motion works as well)
|
|
2706 r restrict selection with another regexp
|
|
2707 SPACE Show full database entry in other window
|
|
2708 f Toggle follow mode: Other window will follow with full db entry
|
|
2709 q Quit without inserting \\cite macro into buffer
|
|
2710 ? Display this help message
|
|
2711 C-r Recursive edit into other window
|
|
2712 RETURN ... Accept current entry and insert in format according to
|
|
2713 reftex-cite-format")
|
|
2714
|
|
2715 (defconst reftex-cite-format-default "\\cite{KEY}"
|
|
2716 "The default value for reftex-cite-format.
|
|
2717 Uses the string version of scitex-cite-format.")
|
|
2718
|
|
2719 (defconst reftex-cite-format-1-author-simple
|
|
2720 '( "\\cite{KEY}" "AUTHOR \\cite{KEY}" "AUTHOR {\it et al.} \\cite{KEY}")
|
|
2721 "Value for reftex-cite format establishing a simple citation with name
|
|
2722 of the first author.
|
|
2723 Uses the list version of reftex-cite-format.")
|
|
2724
|
|
2725 (defconst reftex-cite-format-2-authors
|
|
2726 '((?\C-m
|
|
2727 . ( "\\cite{KEY}" "AUTHOR \\cite{KEY}"
|
|
2728 "AUTHOR \\& AUTHOR \\cite{KEY}" "AUTHOR \\etal{} \\cite{KEY}"))
|
|
2729 (?\,
|
|
2730 . ("\\cite{KEY}" "AUTHOR, \\cite{KEY}"
|
|
2731 "AUTHOR \\& AUTHOR, \\cite{KEY}" "AUTHOR \\etal{}, \\cite{KEY}"))
|
|
2732 (?\;
|
|
2733 . ("\\cite{KEY}" "AUTHOR; \\cite{KEY}"
|
|
2734 "AUTHOR \\& AUTHOR; \\cite{KEY}" "AUTHOR \\etal{}; \\cite{KEY}"))
|
|
2735 (?\:
|
|
2736 . ("\\cite{KEY}" "AUTHOR: \\cite{KEY}"
|
|
2737 "AUTHOR \\& AUTHOR: \\cite{KEY}" "AUTHOR \\etal{}: \\cite{KEY}"))
|
|
2738 (?\(
|
|
2739 . ("(\\cite{KEY})" "AUTHOR (\\cite{KEY})"
|
|
2740 "AUTHOR \\& AUTHOR (\\cite{KEY})" "AUTHOR \\etal{} (\\cite{KEY})"))
|
|
2741 (?\[
|
|
2742 . ("[\\cite{KEY}]" "AUTHOR [\\cite{KEY}]"
|
|
2743 "AUTHOR \\& AUTHOR [\\cite{KEY}]" "AUTHOR \\etal{} [\\cite{KEY}]")))
|
|
2744 "Value for reftex-cite-format that estabishes an Author/Year citation
|
|
2745 where the year is supplied from BibTeX. Depending on which character
|
|
2746 is used during selection to accept the label, an extra ,;: or pair of
|
|
2747 parenthesis will be inserted.
|
|
2748 Uses the list-of-cons-cells version of reftex-cite-format.")
|
|
2749
|
|
2750 ;; Find bibtex files
|
|
2751
|
|
2752 (defun reftex-get-bibfile-list ()
|
|
2753 ;; Return list of bibfiles for current document
|
|
2754
|
|
2755 ;; Ensure access to scanning info
|
|
2756 (reftex-access-scan-info)
|
|
2757
|
|
2758 (or (symbol-value reftex-bibfile-list-symbol)
|
|
2759 (error "No BibTeX files to parse. Add \\bibliography statment to document and reparse.")))
|
|
2760
|
|
2761 (defun reftex-scan-buffer-for-bibliography-statement (bib-list-symbol)
|
|
2762 ;; Scan buffer for bibliography macro and store file list in bib-list-symbol.
|
|
2763 (let (file-list dir-list)
|
|
2764 (setq dir-list
|
|
2765 (reftex-split
|
|
2766 (concat path-separator "+")
|
|
2767 (mapconcat '(lambda(x)
|
|
2768 (if (getenv x) (getenv x) ""))
|
|
2769 reftex-bibpath-environment-variables
|
|
2770 path-separator)))
|
|
2771 (goto-char (point-min))
|
|
2772 (if (re-search-forward "^[ \t]*\\\\bibliography{[ \t]*\\([^}]+\\)" nil t)
|
|
2773 (progn
|
|
2774 (setq dir-list
|
|
2775 (cons (file-name-directory
|
|
2776 (get-text-property (match-beginning 0) 'file))
|
|
2777 dir-list))
|
|
2778 (setq file-list
|
|
2779 (mapcar '(lambda (x) (concat x ".bib"))
|
|
2780 (reftex-delete-list
|
|
2781 reftex-bibfile-ignore-list
|
|
2782 (reftex-split
|
|
2783 "[ \t\n]*,[ \t\n]*"
|
|
2784 (reftex-no-props (match-string 1)))))))
|
|
2785 (message "No \\bibliography command in document."))
|
|
2786 (set bib-list-symbol
|
|
2787 (if file-list
|
|
2788 (reftex-find-files-on-path file-list dir-list
|
|
2789 "While parsing \\bibliography:")
|
|
2790 nil))))
|
|
2791
|
|
2792 (defun reftex-find-files-on-path (file-list path-list &optional error-string)
|
|
2793 ;; Search for all files in FILE-LIST on the PATH-LIST. Return absolute names.
|
|
2794 ;; A missing file throws an exception with the error message ERROR-STRING.
|
|
2795 (let (found-list found file)
|
|
2796 (while file-list
|
|
2797 (setq file (car file-list)
|
|
2798 file-list (cdr file-list)
|
|
2799 found nil)
|
|
2800 (if (file-name-absolute-p file)
|
|
2801 (setq found (expand-file-name file))
|
|
2802 (let ((dirs path-list))
|
|
2803 (while (and dirs (not found))
|
|
2804 (if (and (not (string= (car dirs) ""))
|
|
2805 (file-exists-p (expand-file-name file (car dirs))))
|
|
2806 (setq found (expand-file-name file (car dirs))))
|
|
2807 (setq dirs (cdr dirs)))))
|
|
2808 (if (and found
|
|
2809 (file-exists-p found))
|
|
2810 (add-to-list 'found-list (expand-file-name found))
|
|
2811 (error "%s No such file %s."
|
|
2812 (or error-string "") file)))
|
|
2813 (nreverse found-list)))
|
|
2814
|
|
2815 ;; Find a certain reference in any of the BibTeX files.
|
|
2816
|
|
2817 (defun reftex-pop-to-bibtex-entry (key file-list
|
|
2818 &optional mark-to-kill highlight)
|
|
2819 ;; Find BibTeX KEY in any file in FILE-LIST in another window.
|
|
2820 ;; If mark-to-kill is non-nil, mark new buffer to kill."
|
|
2821
|
|
2822 (let* ((re (concat "@[a-zA-Z]+[ \t\n\r]*{[ \t\n\r]*" (regexp-quote key) "[ \t\n\r,]"))
|
|
2823 (window-conf (current-window-configuration))
|
|
2824 file buf)
|
|
2825 (catch 'exit
|
|
2826 (switch-to-buffer-other-window (current-buffer))
|
|
2827 (while file-list
|
|
2828 (setq file (car file-list)
|
|
2829 file-list (cdr file-list))
|
|
2830 (if (not (setq buf (reftex-get-file-buffer-force file mark-to-kill)))
|
|
2831 (error "No such file %s" file))
|
|
2832 (switch-to-buffer buf)
|
|
2833 (widen)
|
|
2834 (goto-char 0)
|
|
2835 (if (re-search-forward re nil t)
|
|
2836 (progn
|
|
2837 (goto-char (match-beginning 0))
|
|
2838 (recenter 0)
|
|
2839 (if highlight
|
|
2840 (reftex-highlight 0 (match-beginning 0) (match-end 0)))
|
|
2841 (throw 'exit (selected-window)))))
|
|
2842 (set-window-configuration window-conf)
|
|
2843 (beep)
|
|
2844 (message "No BibTeX entry with citation key %s" key))))
|
|
2845
|
|
2846 ;; Parse bibtex buffers
|
|
2847
|
|
2848 (defun reftex-extract-bib-entries (buffers &optional get-word)
|
|
2849 ;; Extract bib entries which match regexps from BUFFERS.
|
|
2850 ;; BUFFERS is a list of buffers or file names.
|
|
2851 ;; Return list with entries."
|
|
2852 (let* (re-list first-re rest-re
|
|
2853 ;; avoid fontification of lookup buffers
|
|
2854 (lazy-lock-minimum-size 1)
|
|
2855 (buffer-list (if (listp buffers) buffers (list buffers)))
|
|
2856 found-list entry buffer1 buffer alist
|
|
2857 key-point start-point end-point)
|
|
2858
|
|
2859 (setq re-list (reftex-split "[ \t]*&&[ \t]*"
|
|
2860 (read-string "RegExp [ && RegExp...]: "
|
|
2861 nil 'reftex-cite-regexp-hist)))
|
|
2862
|
|
2863 (setq first-re (car re-list) ; We'll use the first re to find things,
|
|
2864 rest-re (cdr re-list)) ; the other to narrow down.
|
|
2865 (if (string-match "\\`[ \t]*\\'" first-re)
|
|
2866 (error "Empty regular expression"))
|
|
2867
|
|
2868 (save-excursion
|
|
2869 (save-window-excursion
|
|
2870
|
|
2871 ;; walk through all bibtex files
|
|
2872 (while buffer-list
|
|
2873 (setq buffer (car buffer-list)
|
|
2874 buffer-list (cdr buffer-list))
|
|
2875 (if (and (bufferp buffer)
|
|
2876 (buffer-live-p buffer))
|
|
2877 (setq buffer1 buffer)
|
|
2878 (setq buffer1 (reftex-get-file-buffer-force
|
|
2879 buffer (not reftex-keep-temporary-buffers))))
|
|
2880 (if (not buffer1)
|
|
2881 (error "Cannot find BibTeX file %s" buffer)
|
|
2882 (message "Scanning bibliography database %s" buffer1))
|
|
2883
|
|
2884 (set-buffer buffer1)
|
|
2885 (save-excursion
|
|
2886 (goto-char (point-min))
|
|
2887 (while (re-search-forward first-re nil t)
|
|
2888 (catch 'search-again
|
|
2889 (setq key-point (point))
|
|
2890 (if (not (re-search-backward
|
|
2891 "^[ \t]*@\\([a-zA-Z]+\\)[ \t\n\r]*{" nil t))
|
|
2892 (throw 'search-again nil))
|
|
2893 (setq start-point (point))
|
|
2894 (goto-char (match-end 0))
|
|
2895 (condition-case nil
|
|
2896 (up-list 1)
|
|
2897 ('error (goto-char key-point)
|
|
2898 (throw 'search-again nil)))
|
|
2899 (setq end-point (point))
|
|
2900
|
|
2901 ;; Ignore @string, @comment and @c entries or things
|
|
2902 ;; outside entries
|
|
2903 (if (or (string= (downcase (match-string 1)) "string")
|
|
2904 (string= (downcase (match-string 1)) "comment")
|
|
2905 (string= (downcase (match-string 1)) "c")
|
|
2906 (< (point) key-point)) ; this means match not in {}
|
|
2907 (progn
|
|
2908 (goto-char key-point)
|
|
2909 (throw 'search-again nil)))
|
|
2910
|
|
2911 ;; Well, we have got a match
|
|
2912 (setq entry (concat
|
|
2913 (buffer-substring start-point (point)) "\n"))
|
|
2914
|
|
2915 ;; Check if other regexp match as well
|
|
2916 (setq re-list rest-re)
|
|
2917 (while re-list
|
|
2918 (if (not (string-match (car re-list) entry))
|
|
2919 ;; nope - move on
|
|
2920 (throw 'search-again nil))
|
|
2921 (setq re-list (cdr re-list)))
|
|
2922
|
|
2923 (setq alist (reftex-parse-bibtex-entry
|
|
2924 nil start-point end-point))
|
|
2925 (setq alist (cons (cons "&entry" entry) alist))
|
|
2926
|
|
2927 ;; check for crossref entries
|
|
2928 (if (assoc "crossref" alist)
|
|
2929 (setq alist
|
|
2930 (append
|
|
2931 alist (reftex-get-crossref-alist alist))))
|
|
2932
|
|
2933 ;; format the entry
|
|
2934 (setq alist
|
|
2935 (cons
|
|
2936 (cons "&formatted"
|
|
2937 (reftex-format-bib-entry alist))
|
|
2938 alist))
|
|
2939
|
|
2940 ;; add it to the list
|
|
2941 (setq found-list (cons alist found-list)))))
|
|
2942 (reftex-kill-temporary-buffers))))
|
|
2943 (setq found-list (nreverse found-list))
|
|
2944
|
|
2945 ;; Sorting
|
|
2946 (cond
|
|
2947 ((eq 'author reftex-sort-bibtex-matches)
|
|
2948 (sort found-list 'reftex-bib-sort-author))
|
|
2949 ((eq 'year reftex-sort-bibtex-matches)
|
|
2950 (sort found-list 'reftex-bib-sort-year))
|
|
2951 ((eq 'reverse-year reftex-sort-bibtex-matches)
|
|
2952 (sort found-list 'reftex-bib-sort-year-reverse))
|
|
2953 (t found-list))))
|
|
2954
|
|
2955 (defun reftex-bib-sort-author (e1 e2)
|
|
2956 (let ((al1 (reftex-get-bib-authors e1)) (al2 (reftex-get-bib-authors e2)))
|
|
2957 (while (and al1 al2 (string= (car al1) (car al2)))
|
|
2958 (setq al1 (cdr al1)
|
|
2959 al2 (cdr al2)))
|
|
2960 (if (and (stringp (car al1))
|
|
2961 (stringp (car al2)))
|
|
2962 (string< (car al1) (car al2))
|
|
2963 (not (stringp (car al1))))))
|
|
2964
|
|
2965 (defun reftex-bib-sort-year (e1 e2)
|
|
2966 (< (string-to-int (cdr (assoc "year" e1)))
|
|
2967 (string-to-int (cdr (assoc "year" e2)))))
|
|
2968
|
|
2969 (defun reftex-bib-sort-year-reverse (e1 e2)
|
|
2970 (> (string-to-int (or (cdr (assoc "year" e1)) "0"))
|
|
2971 (string-to-int (or (cdr (assoc "year" e2)) "0"))))
|
|
2972
|
|
2973 (defun reftex-get-crossref-alist (entry)
|
|
2974 ;; return the alist from a crossref entry
|
|
2975 (let ((crkey (cdr (assoc "crossref" entry)))
|
|
2976 start)
|
|
2977 (save-excursion
|
|
2978 (save-restriction
|
|
2979 (widen)
|
|
2980 (if (re-search-forward
|
|
2981 (concat "@\\w+{[ \t\n\r]*" (regexp-quote crkey) "[ \t\n\r]*,") nil t)
|
|
2982 (progn
|
|
2983 (setq start (match-beginning 0))
|
|
2984 (condition-case nil
|
|
2985 (up-list 1)
|
|
2986 ('error nil))
|
|
2987 (reftex-parse-bibtex-entry nil start (point)))
|
|
2988 nil)))))
|
|
2989
|
|
2990 ;; Parse and format individual entries
|
|
2991
|
|
2992 (defun reftex-get-bib-authors (entry)
|
|
2993 ;; Return a list with the author names in ENTRY
|
|
2994 (let (authors)
|
|
2995 (setq authors (reftex-get-bib-field "author" entry))
|
|
2996 (if (equal "" authors)
|
|
2997 (setq authors (reftex-get-bib-field "editor" entry)))
|
|
2998 (while (string-match "\\band\\b[ \t]*" authors)
|
|
2999 (setq authors (replace-match "\n" nil t authors)))
|
|
3000 (while (string-match "[\\.a-zA-Z\\-]+\\.[ \t]*\\|,.*\\|[{}]+" authors)
|
|
3001 (setq authors (replace-match "" nil t authors)))
|
|
3002 (while (string-match "^[ \t]+\\|[ \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 (reftex-split "\n" authors)))
|
|
3007
|
|
3008 (defun reftex-parse-bibtex-entry (entry &optional from to)
|
|
3009 (let (alist key start field)
|
|
3010 (save-excursion
|
|
3011 (save-restriction
|
|
3012 (if entry
|
|
3013 (progn
|
|
3014 (switch-to-buffer "*RefTeX-scratch*")
|
|
3015 (fundamental-mode)
|
|
3016 (erase-buffer)
|
|
3017 (insert entry))
|
|
3018 (widen)
|
|
3019 (narrow-to-region from to))
|
|
3020 (goto-char (point-min))
|
|
3021
|
|
3022 (if (re-search-forward
|
|
3023 "@\\(\\w+\\)[ \t\n\r]*{[ \t\n\r]*\\([^ \t\n\r,]+\\)" nil t)
|
|
3024 (setq alist
|
|
3025 (list
|
|
3026 (cons "&type" (downcase (reftex-no-props (match-string 1))))
|
|
3027 (cons "&key" (reftex-no-props (match-string 2))))))
|
|
3028 (while (re-search-forward "\\(\\w+\\)[ \t\n\r]*=[ \t\n\r]*" nil t)
|
|
3029 (setq key (reftex-no-props (downcase (match-string 1))))
|
|
3030 (cond
|
|
3031 ((= (following-char) ?{)
|
|
3032 (forward-char 1)
|
|
3033 (setq start (point))
|
|
3034 (condition-case nil
|
|
3035 (up-list 1)
|
|
3036 ('error nil)))
|
|
3037 ((= (following-char) ?\")
|
|
3038 (forward-char 1)
|
|
3039 (setq start (point))
|
|
3040 (while (and (search-forward "\"" nil t)
|
|
3041 (= ?\\ (char-after (- (point) 2))))))
|
|
3042 (t
|
|
3043 (setq start (point))
|
|
3044 (re-search-forward "[ \t\n\r,}]" nil 1)))
|
|
3045 (setq field (buffer-substring-no-properties start (1- (point))))
|
|
3046 ;; remove extra whitesp
|
|
3047 (while (string-match "[\n\t\r]\\|[ \t][ \t]+" field)
|
|
3048 (setq field (replace-match " " nil t field)))
|
|
3049 ;; remove leading garbage
|
|
3050 (if (string-match "^[ \t{]+" field)
|
|
3051 (setq field (replace-match "" nil t field)))
|
|
3052 ;; remove trailing garbage
|
|
3053 (if (string-match "[ \t}]+$" field)
|
|
3054 (setq field (replace-match "" nil t field)))
|
|
3055 (setq alist (cons (cons key field) alist)))
|
|
3056 alist))))
|
|
3057
|
|
3058 (defun reftex-get-bib-field (fieldname entry)
|
|
3059 ;; Extract the field FIELDNAME from an ENTRY
|
|
3060 (or (cdr (assoc fieldname entry))
|
|
3061 ""))
|
|
3062
|
|
3063 (defun reftex-format-bib-entry (entry)
|
|
3064 ;; Format a BibTeX ENTRY so that it is nice to look at
|
|
3065 (let*
|
|
3066 ((rtn nil)
|
|
3067 (auth-list (reftex-get-bib-authors entry))
|
|
3068 (authors (mapconcat '(lambda (x) x) auth-list ", "))
|
|
3069 (year (reftex-get-bib-field "year" entry))
|
|
3070 (title (reftex-get-bib-field "title" entry))
|
|
3071 (type (reftex-get-bib-field "&type" entry))
|
|
3072 (key (reftex-get-bib-field "&key" entry))
|
|
3073 (extra
|
|
3074 (cond
|
|
3075 ((equal type "article")
|
|
3076 (concat (reftex-get-bib-field "journal" entry) " "
|
|
3077 (reftex-get-bib-field "volume" entry) ", "
|
|
3078 (reftex-get-bib-field "pages" entry)))
|
|
3079 ((equal type "book")
|
|
3080 (concat "book (" (reftex-get-bib-field "publisher" entry) ")"))
|
|
3081 ((equal type "phdthesis")
|
|
3082 (concat "PhD: " (reftex-get-bib-field "school" entry)))
|
|
3083 ((equal type "mastersthesis")
|
|
3084 (concat "Master: " (reftex-get-bib-field "school" entry)))
|
|
3085 ((equal type "inbook")
|
|
3086 (concat "Chap: " (reftex-get-bib-field "chapter" entry)
|
|
3087 ", pp. " (reftex-get-bib-field "pages" entry)))
|
|
3088 ((or (equal type "conference")
|
|
3089 (equal type "incollection")
|
|
3090 (equal type "inproceedings"))
|
|
3091 (concat "in: " (reftex-get-bib-field "booktitle" entry)))
|
|
3092 (t ""))))
|
|
3093 (setq authors
|
|
3094 (if (> (length authors) 30)
|
|
3095 (concat (substring authors 0 27) "...")
|
|
3096 (format "%-30s" authors))
|
|
3097 title
|
|
3098 (if (> (length title) 70)
|
|
3099 (concat (substring title 0 67) "...")
|
|
3100 (format "%-70s" title))
|
|
3101 extra
|
|
3102 (if (> (length extra) 40)
|
|
3103 (concat (substring extra 0 37) "...")
|
|
3104 (format "%-40s" extra)))
|
|
3105 (if (reftex-use-fonts)
|
|
3106 (progn
|
|
3107 (put-text-property 0 (length authors) 'face 'font-lock-keyword-face
|
|
3108 authors)
|
|
3109 (put-text-property 0 (length title) 'face 'font-lock-comment-face
|
|
3110 title)
|
|
3111 (put-text-property 0 (length extra) 'face 'font-lock-reference-face
|
|
3112 extra)))
|
|
3113 (setq rtn (concat key "\n " authors " " year " " extra
|
|
3114 "\n " title "\n\n"))
|
|
3115 rtn))
|
|
3116
|
|
3117 ;; Make a citation
|
|
3118
|
|
3119 (defun reftex-citation (&optional arg no-insert)
|
|
3120 "Make a citation unsing BibTeX database files.
|
|
3121 After asking for a Regular Expression, it scans the buffers with
|
|
3122 bibtex entries (taken from the \\bibliography command) and offers the
|
|
3123 matching entries for selection. The selected entry is formated according
|
|
3124 to reftex-cite-format and inserted into the buffer.
|
|
3125 If NO-INSERT is non-nil, nothing is inserted, only the selected key returned.
|
|
3126 The regular expression uses an expanded syntax: && is interpreted as 'and'.
|
|
3127 Thus, aaaa&&bbb matches entries which contain both aaaa and bbb.
|
|
3128 When this function is called with point inside the braces of a \\cite
|
|
3129 command, it will add another key, ignoring the value of reftex-cite-format.
|
|
3130 When called with a numeric prefix, that many citations will be made and all
|
|
3131 put into the same \\cite command.
|
|
3132 When called with just C-u as prefix, enforces rescan of buffer for
|
|
3133 bibliography statement (e.g. if it was changed)."
|
|
3134
|
|
3135 (interactive "P")
|
|
3136
|
|
3137 ;; check for recursive edit
|
|
3138 (reftex-check-recursive-edit)
|
|
3139
|
|
3140 ;; if there is just 1 C-u prefix arg, force to rescan buffer
|
|
3141 (if (and current-prefix-arg
|
|
3142 (listp current-prefix-arg)
|
|
3143 (= 4 (prefix-numeric-value arg)))
|
|
3144 (reftex-reset-scanning-information))
|
|
3145
|
|
3146 ;; check if there is already a cite command at point and change cite format
|
|
3147 ;; in order to only add another reference in the same cite command.
|
|
3148 (let ((pos (point)))
|
|
3149 (search-backward "\\" (point-min) 1)
|
|
3150 (if (and (looking-at "\\\\[a-zA-Z]*cite\\*?\\(\\[[^]]*\\]\\)*{\\([^}]*\\)")
|
|
3151 (>= (match-end 0) pos)
|
|
3152 (>= pos (match-beginning 2)))
|
|
3153 (progn
|
|
3154 (goto-char pos)
|
|
3155 (cond
|
|
3156 ((or (not arg)
|
|
3157 (not (listp arg)))
|
|
3158 (setq reftex-cite-format1
|
|
3159 (concat
|
|
3160 (if (not (or (= (preceding-char) ?{)
|
|
3161 (= (preceding-char) ?,)))
|
|
3162 ","
|
|
3163 "")
|
|
3164 "KEY"
|
|
3165 (if (not (or (= (following-char) ?})
|
|
3166 (= (following-char) ?,)))
|
|
3167 ","
|
|
3168 ""))))
|
|
3169 (t
|
|
3170 (setq reftex-cite-format1 "KEY"))))
|
|
3171 (setq reftex-cite-format1
|
|
3172 (if (symbolp reftex-cite-format)
|
|
3173 (symbol-value reftex-cite-format)
|
|
3174 reftex-cite-format))
|
|
3175 (goto-char pos)))
|
|
3176
|
|
3177 (let* (key entry cnt rtn ins-string re-list re
|
|
3178 ;; scan bibtex files
|
|
3179 (lazy-lock-minimum-size 1)
|
|
3180 (found-list (reftex-extract-bib-entries
|
|
3181 (reftex-get-bibfile-list)))
|
|
3182 (found-list-r nil)
|
|
3183 (accept-keys
|
|
3184 (if (and (listp reftex-cite-format1)
|
|
3185 (listp (car reftex-cite-format1)))
|
|
3186 (mapcar 'car reftex-cite-format1)
|
|
3187 '(?\C-m))))
|
|
3188 (if (not found-list)
|
|
3189 (error "Sorry, no matches found"))
|
|
3190
|
|
3191 ;; remember where we came from
|
|
3192 (setq reftex-call-back-to-this-buffer (current-buffer))
|
|
3193
|
|
3194 ;; offer selection
|
|
3195 (save-window-excursion
|
|
3196 (switch-to-buffer-other-window "*RefTeX Select*")
|
|
3197 (erase-buffer)
|
|
3198 (mapcar '(lambda (x) (insert (cdr (assoc "&formatted" x))))
|
|
3199 found-list)
|
|
3200 (if (= 0 (buffer-size))
|
|
3201 (error "Sorry, no matches found"))
|
|
3202 (setq truncate-lines t)
|
|
3203 (goto-char 1)
|
|
3204 (if (catch 'exit
|
|
3205 (while t
|
|
3206 (setq rtn
|
|
3207 (reftex-select-item
|
|
3208 nil
|
|
3209 (concat
|
|
3210 "Select: [n]ext [p]rev [r]estrict [q]uit [?]Help ||"
|
|
3211 " RETURN "
|
|
3212 (condition-case nil
|
|
3213 (mapconcat 'char-to-string accept-keys " ")
|
|
3214 (error (error "Illegal reftex-cite-format"))))
|
|
3215 "^[^ \t\n]"
|
|
3216 "\n\n"
|
|
3217 4
|
|
3218 reftex-citation-help
|
|
3219 (cons ?r accept-keys)
|
|
3220 nil
|
|
3221 'reftex-bibtex-selection-callback nil))
|
|
3222 (setq key (car rtn)
|
|
3223 cnt (cdr rtn))
|
|
3224 (if (not key) (throw 'exit nil))
|
|
3225 (cond
|
|
3226 ((equal key ?r)
|
|
3227 ;; restrict with new regular expression
|
|
3228 (setq re-list
|
|
3229 (reftex-split "[ \t]*&&[ \t]*"
|
|
3230 (read-string "RegExp [ && RegExp...]: "
|
|
3231 nil 'reftex-cite-regexp-hist)))
|
|
3232 (while re-list
|
|
3233 (setq re (car re-list)
|
|
3234 re-list (cdr re-list))
|
|
3235 (setq found-list-r
|
|
3236 (delete ""
|
|
3237 (mapcar
|
|
3238 '(lambda (x)
|
|
3239 (if (string-match re
|
|
3240 (cdr (assoc "&entry" x)))
|
|
3241 x
|
|
3242 ""))
|
|
3243 found-list))))
|
|
3244 (if found-list-r
|
|
3245 (setq found-list found-list-r)
|
|
3246 (ding))
|
|
3247 (erase-buffer)
|
|
3248 (mapcar '(lambda (x) (insert (cdr (assoc "&formatted" x))))
|
|
3249 found-list)
|
|
3250 (goto-char 1))
|
|
3251 ((or (member key accept-keys)
|
|
3252 (equal key ?\C-m)
|
|
3253 (equal key 'return))
|
|
3254 (setq entry (nth cnt found-list))
|
|
3255 (throw 'exit t))
|
|
3256 (t
|
|
3257 (ding)))))
|
|
3258 (progn
|
|
3259 ;; format the entry
|
|
3260 (if (not (integerp key)) (setq key ?\C-m))
|
|
3261 (setq ins-string (reftex-format-citation entry key)))
|
|
3262 (setq ins-string "")
|
|
3263 (message "Quit")))
|
|
3264 (kill-buffer "*RefTeX Select*")
|
|
3265
|
|
3266 (if (not no-insert)
|
|
3267 (insert ins-string))
|
|
3268 (message "")
|
|
3269
|
|
3270 ;; Check if the prefix arg was numeric, and call reftex-citation recursively
|
|
3271 (if (and (integerp arg)
|
|
3272 (> arg 1)
|
|
3273 (re-search-backward
|
|
3274 "\\\\[a-zA-Z]*cite\\*?\\(\\[[^]]*\\]\\)*{\\([^}]*\\)" nil t))
|
|
3275 (progn
|
|
3276 (goto-char (match-end 0))
|
|
3277 (setq arg (1- arg))
|
|
3278 (reftex-citation arg))
|
|
3279 (reftex-kill-temporary-buffers))
|
|
3280 ;; Return the citation key
|
|
3281 (reftex-get-bib-field "&key" entry)))
|
|
3282
|
|
3283 (defun reftex-format-citation (entry key)
|
|
3284 ;; Format a citation from the info in the BibTeX ENTRY
|
|
3285 (let* ((cite-key (reftex-get-bib-field "&key" entry))
|
|
3286 (year (reftex-get-bib-field "year" entry))
|
|
3287 (auth-list (reftex-get-bib-authors entry))
|
|
3288 (nauthors (length auth-list))
|
|
3289 format)
|
|
3290
|
|
3291 (save-excursion
|
|
3292 ;; Find the correct format
|
|
3293 (if (and (listp reftex-cite-format1)
|
|
3294 (listp (car reftex-cite-format1)))
|
|
3295 (if (integerp (car (car reftex-cite-format1)))
|
|
3296 (if (assoc key reftex-cite-format1)
|
|
3297 (setq format (cdr (assoc key reftex-cite-format1)))
|
|
3298 (if (or (equal key ?\C-m)
|
|
3299 (equal key 'return))
|
|
3300 (setq format (cdr (car reftex-cite-format1)))
|
|
3301 (error "Error in reftex-cite-format")))
|
|
3302 (error "Error in reftex-cite-format"))
|
|
3303 (setq format reftex-cite-format1))
|
|
3304
|
|
3305 (if (listp format)
|
|
3306 (let ((nn (min nauthors (1- (length format)))))
|
|
3307 (while (and (> nn 0) (string= "" (nth nn format)))
|
|
3308 (setq nn (1- nn)))
|
|
3309 (setq format (nth nn format))))
|
|
3310 (if (stringp format)
|
|
3311 (setq format format)
|
|
3312 (setq format "\\cite{KEY}"))
|
|
3313
|
|
3314 ;; Insert the author names
|
|
3315 (while (string-match "\\bAUTHOR\\b" format)
|
|
3316 (setq format (replace-match (car auth-list) t t format))
|
|
3317 (setq auth-list (cdr auth-list)))
|
|
3318 (while (string-match "\\bKEY\\b" format)
|
|
3319 (setq format (replace-match cite-key t t format)))
|
|
3320 (while (string-match "\\bYEAR\\b" format)
|
|
3321 (setq format (replace-match year t t format)))
|
|
3322 format)))
|
|
3323
|
|
3324 ;; this is slow and not recommended for follow mode
|
|
3325 (defun reftex-bibtex-selection-callback (cnt)
|
|
3326 ;; Callback function to be called from the BibTeX selection, in
|
|
3327 ;; order to display context. This function is relatively slow and not
|
|
3328 ;; recommended for follow mode, just for individual lookups.
|
|
3329 ;; When compiled, this gives a warning about found-list. However,
|
|
3330 ;; the calling function binds found-list with let.
|
|
3331 (let ((win (selected-window))
|
|
3332 (key (reftex-get-bib-field "&key" (nth cnt found-list)))
|
|
3333 (bibfile-list (save-excursion
|
|
3334 (set-buffer reftex-call-back-to-this-buffer)
|
|
3335 (reftex-get-bibfile-list))))
|
|
3336 (reftex-pop-to-bibtex-entry key bibfile-list
|
|
3337 (not reftex-keep-temporary-buffers) t)
|
|
3338 (select-window win)))
|
|
3339
|
|
3340 ;;; ===========================================================================
|
|
3341 ;;;
|
|
3342 ;;; Here is the routine used for selection
|
|
3343
|
|
3344 ;; Marker for return point from recursive edit
|
|
3345 (defvar reftex-recursive-edit-marker (make-marker))
|
|
3346
|
|
3347 (defun reftex-check-recursive-edit ()
|
|
3348 ;; Check if we are already in a recursive edit. Abort with helpful
|
|
3349 ;; message if so.
|
|
3350 (if (marker-position reftex-recursive-edit-marker)
|
|
3351 (error
|
|
3352 (substitute-command-keys
|
|
3353 "In unfinished recursive edit. Finish (\\[exit-recursive-edit]) or abort (\\[abort-recursive-edit])."))))
|
|
3354
|
|
3355 (defun reftex-select-item (buffer prompt next-re end-re size help-string
|
|
3356 event-list &optional offset
|
|
3357 call-back cb-flag)
|
|
3358 ;; Select an item from the buffer BUFFER. Show PROMPT to user, find
|
|
3359 ;; next item with NEXT-RE regular expression, return on any of the
|
|
3360 ;; events listed in EVENT-LIST. The function returns the event along
|
|
3361 ;; with an integer indicating which item was selected. When OFFSET is
|
|
3362 ;; specified, starts at that item in the list. When CALL-BACK is
|
|
3363 ;; given, it is a function which is called with the match of the
|
|
3364 ;; NEXT-RE match and the index of the element.
|
|
3365 (let* (key key-sq b e ev cnt cmd
|
|
3366 (offset1 (or offset 1)))
|
|
3367 (setq ev
|
|
3368 (catch 'exit
|
|
3369 (save-window-excursion
|
|
3370 (if buffer
|
|
3371 (switch-to-buffer-other-window buffer))
|
|
3372 (if (= 0 (buffer-size))
|
|
3373 (throw 'exit nil))
|
|
3374 (setq truncate-lines t)
|
|
3375 (goto-char 1)
|
|
3376 (if (not (re-search-forward next-re nil t offset1))
|
|
3377 (progn ; in case the offset is illegal
|
|
3378 (setq offset1 1)
|
|
3379 (if (not (re-search-forward next-re nil t offset1))
|
|
3380 (throw 'exit nil))))
|
|
3381 (beginning-of-line 1)
|
|
3382 (setq cnt (if offset1 (1- offset1) 0))
|
|
3383 (while t
|
|
3384 (if (and cb-flag call-back)
|
|
3385 (funcall call-back cnt))
|
|
3386 (setq b (point)
|
|
3387 e (save-excursion
|
|
3388 (save-match-data
|
|
3389 (re-search-forward end-re nil 1))
|
|
3390 (point)))
|
|
3391 (reftex-highlight 1 b e)
|
|
3392 (if (or (not (pos-visible-in-window-p b))
|
|
3393 (not (pos-visible-in-window-p e)))
|
|
3394 (recenter (/ (window-height) 2)))
|
|
3395 (setq key-sq (read-key-sequence prompt))
|
|
3396 (setq key (car
|
|
3397 (cond
|
|
3398 ((fboundp 'listify-key-sequence) ; Emacs
|
|
3399 (listify-key-sequence key-sq))
|
|
3400 ((fboundp 'event-to-character) ; XEmacs
|
|
3401 (mapcar 'event-to-character key-sq))
|
|
3402 (t (error "Please report this problem to dominik@strw.leidenuniv.nl")))))
|
|
3403
|
|
3404 (setq cmd (key-binding key-sq))
|
|
3405
|
|
3406 (reftex-unhighlight 0)
|
|
3407
|
|
3408 (cond
|
|
3409
|
|
3410 ((or (equal key ?n)
|
|
3411 (equal key ?\C-i)
|
|
3412 (equal cmd 'next-line))
|
|
3413 (if (re-search-forward next-re nil t 2)
|
|
3414 (setq cnt (1+ cnt)))
|
|
3415 (beginning-of-line 1))
|
|
3416
|
|
3417 ((equal cmd 'scroll-up)
|
|
3418 (setq cnt (1- cnt))
|
|
3419 (while (and (pos-visible-in-window-p)
|
|
3420 (re-search-forward next-re nil t))
|
|
3421 (setq cnt (1+ cnt)))
|
|
3422 (beginning-of-line 1)
|
|
3423 (recenter 1))
|
|
3424
|
|
3425 ((or (equal key ?p)
|
|
3426 (equal cmd 'previous-line))
|
|
3427 (if (re-search-backward next-re nil t)
|
|
3428 (setq cnt (1- cnt))))
|
|
3429
|
|
3430 ((equal cmd 'scroll-down)
|
|
3431 (while (and (pos-visible-in-window-p)
|
|
3432 (re-search-backward next-re nil t))
|
|
3433 (setq cnt (1- cnt)))
|
|
3434 (recenter (- (window-height) size 2)))
|
|
3435
|
|
3436 ((equal key ?q)
|
|
3437 (throw 'exit nil))
|
|
3438
|
|
3439 ((equal key ?\C-g)
|
|
3440 (bury-buffer)
|
|
3441 (error "Abort"))
|
|
3442
|
|
3443 ((or (equal key ?\C-m)
|
|
3444 (equal key 'return)
|
|
3445 (equal cmd 'newline))
|
|
3446 (throw 'exit 'return))
|
|
3447
|
|
3448 ((or (equal key ?C) ; backward compatibility
|
|
3449 (equal key ?f))
|
|
3450 (setq cb-flag (not cb-flag)))
|
|
3451
|
|
3452 ((equal key ?\ )
|
|
3453 (funcall call-back cnt))
|
|
3454
|
|
3455 ((equal key ?\?)
|
|
3456 (save-window-excursion
|
|
3457 (with-output-to-temp-buffer "*RefTeX Help*"
|
|
3458 (princ help-string))
|
|
3459 (setq unread-command-events
|
|
3460 (cons
|
|
3461 (cond
|
|
3462 ((fboundp 'read-event) ; Emacs
|
|
3463 (read-event))
|
|
3464 ((fboundp 'next-command-event) ; XEmacs
|
|
3465 (next-command-event))
|
|
3466 (t (error "Please report this problem to dominik@strw.leidenuniv.nl")))
|
|
3467 nil)))
|
|
3468 (kill-buffer "*RefTeX Help*"))
|
|
3469
|
|
3470 ((equal key ?\C-r)
|
|
3471 ;; sje - code copied from ispell.el for
|
|
3472 ;; performing recursive edit
|
|
3473 (set-marker reftex-recursive-edit-marker (point))
|
|
3474 (unwind-protect
|
|
3475 (progn
|
|
3476 (save-window-excursion
|
|
3477 (save-excursion
|
|
3478 (other-window 1)
|
|
3479 (message
|
|
3480 (substitute-command-keys
|
|
3481 "Recursive edit. Return to selection with \\[exit-recursive-edit]"))
|
|
3482 (recursive-edit)))
|
|
3483 (if (not (equal (marker-buffer
|
|
3484 reftex-recursive-edit-marker)
|
|
3485 (current-buffer)))
|
|
3486 (error
|
|
3487 "Cannot continue RefTeX from this buffer."))
|
|
3488 (goto-char reftex-recursive-edit-marker))
|
|
3489 (set-marker reftex-recursive-edit-marker nil)))
|
|
3490
|
|
3491 ((member key event-list)
|
|
3492 (throw 'exit key))
|
|
3493 (t
|
|
3494 (ding)))))))
|
|
3495 (message "")
|
|
3496 (cons ev cnt)))
|
|
3497
|
|
3498 ;;; ===========================================================================
|
|
3499 ;;;
|
|
3500 ;;; View cross references
|
|
3501
|
|
3502 (defun reftex-view-crossref (&optional arg)
|
|
3503 "View cross reference of \\ref or \\cite macro at point.
|
|
3504 If the macro at point is a \\ref, show the corresponding label definition.
|
|
3505 If it is a \\cite, show the BibTeX database entry.
|
|
3506 If there is no such macro at point, search forward to find one.
|
|
3507 When you call this function several times in direct successtion, point will
|
|
3508 move to view subsequent cross references further down in the buffer.
|
|
3509 With argument, actually select the window showing the cross reference."
|
|
3510
|
|
3511 (interactive "P")
|
|
3512
|
|
3513 ;; See where we are.
|
|
3514 (let* ((pos (point))
|
|
3515 (re "\\\\[a-z]*\\(cite\\|ref\\)\\(\\[[^{}]*\\]\\)?{\\([^}]+\\)}")
|
|
3516 (my-window (get-buffer-window (current-buffer)))
|
|
3517 pop-window cmd args macro label entry key-start point)
|
|
3518
|
|
3519 (if (save-excursion
|
|
3520 (forward-char 1)
|
|
3521 (and (search-backward "\\" nil t)
|
|
3522 (looking-at re)
|
|
3523 (< pos (match-end 0))))
|
|
3524 (setq macro (match-string 1)
|
|
3525 key-start (match-beginning 3)))
|
|
3526
|
|
3527 (if (and macro (eq last-command this-command))
|
|
3528 (if (and (string= macro "cite")
|
|
3529 (skip-chars-forward "^}, \t\n\r")
|
|
3530 (= (following-char) ?,))
|
|
3531 (setq key-start (1+ (point)))
|
|
3532 (setq macro nil)))
|
|
3533
|
|
3534 (if (not macro)
|
|
3535 (if (re-search-forward re nil t)
|
|
3536 (setq macro (match-string 1)
|
|
3537 key-start (match-beginning 3))
|
|
3538 (error "No further cross references in buffer")))
|
|
3539
|
|
3540 (goto-char key-start)
|
|
3541
|
|
3542 ;; Ensure access to scanning info
|
|
3543 (reftex-access-scan-info)
|
|
3544
|
|
3545 (cond
|
|
3546 ((string= macro "cite")
|
|
3547 (setq cmd 'reftex-pop-to-bibtex-entry
|
|
3548 args (list
|
|
3549 (reftex-no-props (reftex-this-word "^{},"))
|
|
3550 (reftex-get-bibfile-list) nil t)))
|
|
3551 ((string= macro "ref")
|
|
3552 (let ((label (reftex-no-props (reftex-this-word "^{}")))
|
|
3553 (entry (assoc label (symbol-value reftex-list-of-labels-symbol))))
|
|
3554 (if entry
|
|
3555 (setq cmd 'reftex-pop-to-label
|
|
3556 args (list label (list (nth 3 entry)) nil t))
|
|
3557 (error "Label %s not known - reparse document might help" label))))
|
|
3558 (t (error "This should not happen")))
|
|
3559 (setq point (point))
|
|
3560 (apply cmd args)
|
|
3561 (setq pop-window (selected-window))
|
|
3562 (add-hook 'pre-command-hook 'reftex-highlight-shall-die)
|
|
3563 (select-window my-window)
|
|
3564 (goto-char point)
|
|
3565 (and arg (select-window pop-window))))
|
|
3566
|
|
3567 (defun reftex-mouse-view-crossref (ev)
|
|
3568 "View cross reference of \\ref or \\cite macro where you click.
|
|
3569 If the macro at point is a \\ref, show the corresponding label definition.
|
|
3570 If it is a \\cite, show the BibTeX database entry.
|
|
3571 If there is no such macro at point, search forward to find one.
|
|
3572 With argument, actually select the window showing the cross reference."
|
|
3573 (interactive "e")
|
|
3574 (mouse-set-point ev)
|
|
3575 (reftex-view-crossref current-prefix-arg))
|
|
3576
|
|
3577 ;;; ===========================================================================
|
|
3578 ;;;
|
|
3579 ;;; Functions that check out the surroundings
|
|
3580
|
|
3581 (defun reftex-what-macro (which &optional bound)
|
|
3582 ;; Find out if point is within the arguments of any TeX-macro.
|
|
3583 ;; The return value is either (\"\\\\macro\" . (point)) or a list of them.
|
|
3584
|
|
3585 ;; If WHICH is nil, immediately return nil.
|
|
3586 ;; If WHICH is t, return list of all macros enclosing point.
|
|
3587 ;; If WHICH is a list of macros, look only for those macros and return the
|
|
3588 ;; name of the first macro in this list found to enclose point.
|
|
3589 ;; If the optional BOUND is an integer, bound backwards directed
|
|
3590 ;; searches to this point. If it is nil, limit to nearest \\section -
|
|
3591 ;; like statement.
|
|
3592
|
|
3593 ;; This function is pretty stable, but can be fooled if the text contains
|
|
3594 ;; things like \\macro{aa}{bb} where \\macro is defined to take only one
|
|
3595 ;; argument. As RefTeX cannot know this, the string \"bb\" would still be
|
|
3596 ;; considered an argument of macro \\macro.
|
|
3597
|
|
3598 (catch 'exit
|
|
3599 (if (null which) (throw 'exit nil))
|
|
3600 (let ((bound (or bound (save-excursion (re-search-backward
|
|
3601 reftex-section-regexp nil 1)
|
|
3602 (point))))
|
|
3603 pos cmd-list cmd)
|
|
3604 (save-restriction
|
|
3605 (save-excursion
|
|
3606 (narrow-to-region (max 1 bound) (point-max))
|
|
3607 ;; move back out of the current parenthesis
|
|
3608 (while (condition-case nil
|
|
3609 (progn (up-list -1) t)
|
|
3610 (error nil))
|
|
3611 ;; move back over any touching sexps
|
|
3612 (while (or (= (preceding-char) ?\])
|
|
3613 (= (preceding-char) ?\}))
|
|
3614 (backward-sexp))
|
|
3615 (setq pos (point))
|
|
3616 (if (and (or (= (following-char) ?\[)
|
|
3617 (= (following-char) ?\{))
|
|
3618 (and (re-search-backward "\\(\\\\[a-zA-Z]+\\)" nil t)
|
|
3619 (= (match-end 0) pos)))
|
|
3620 (progn
|
|
3621 (setq cmd (buffer-substring-no-properties
|
|
3622 (match-beginning 0) (match-end 0)))
|
|
3623 (if (eq t which)
|
|
3624 (setq cmd-list (cons (cons cmd (point)) cmd-list))
|
|
3625 (if (member cmd which)
|
|
3626 (throw 'exit (cons cmd (point)))))))
|
|
3627 (goto-char pos)))
|
|
3628 (nreverse cmd-list)))))
|
|
3629
|
|
3630 (defun reftex-what-environment (which &optional bound)
|
|
3631 ;; Find out if point is inside a LaTeX environment.
|
|
3632 ;; The return value is (e.g.) either (\"equation\" . (point)) or a list of
|
|
3633 ;; them.
|
|
3634
|
|
3635 ;; If WHICH is nil, immediately return nil.
|
|
3636 ;; If WHICH is t, return list of all environments enclosing point.
|
|
3637 ;; If WHICH is a list of environments, look only for those environments and
|
|
3638 ;; return the name of the first environment in this list found to enclose
|
|
3639 ;; point.
|
|
3640
|
|
3641 ;; If the optional BOUND is an integer, bound backwards directed searches to
|
|
3642 ;; this point. If it is nil, limit to nearest \\section - like statement.
|
|
3643
|
|
3644 (catch 'exit
|
|
3645 (save-excursion
|
|
3646 (if (null which) (throw 'exit nil))
|
|
3647 (let ((bound (or bound (save-excursion (re-search-backward
|
|
3648 reftex-section-regexp nil 1)
|
|
3649 (point))))
|
|
3650 env-list end-list env)
|
|
3651 (while (re-search-backward "\\\\\\(begin\\|end\\){\\([^}]+\\)}"
|
|
3652 bound t)
|
|
3653 (setq env (buffer-substring-no-properties
|
|
3654 (match-beginning 2) (match-end 2)))
|
|
3655 (cond
|
|
3656 ((string= (match-string 1) "end")
|
|
3657 (add-to-list 'end-list env))
|
|
3658 ((member env end-list)
|
|
3659 (setq end-list (delete env end-list)))
|
|
3660 ((eq t which)
|
|
3661 (setq env-list (cons (cons env (point)) env-list)))
|
|
3662 ((member env which)
|
|
3663 (throw 'exit (cons env (point))))))
|
|
3664 (nreverse env-list)))))
|
|
3665
|
|
3666 (defun reftex-word-before-point ()
|
|
3667 ;; Return the word before point. Word means here:
|
|
3668 ;; Consists of [a-zA-Z0-9.:] and ends at point or whitespace.
|
|
3669 (let ((pos (point)))
|
|
3670 (save-excursion
|
|
3671 (re-search-backward "[^ \t\n\r]" (point-min) 1)
|
|
3672 (setq pos (1+ (point)))
|
|
3673 (if (re-search-backward "[^a-zA-Z0-9\\\.:]" (point-min) 1)
|
|
3674 (forward-char 1))
|
|
3675 (buffer-substring-no-properties (point) pos))))
|
|
3676
|
|
3677 ;; ============================================================================
|
|
3678 ;;
|
|
3679 ;; Some generally useful functions
|
|
3680
|
|
3681 (defun reftex-no-props (string)
|
|
3682 ;; Return STRING with all text properties removed
|
|
3683 (and (stringp string)
|
|
3684 (set-text-properties 0 (length string) nil string))
|
|
3685 string)
|
|
3686
|
|
3687 (defun reftex-split (regexp string)
|
|
3688 ;; Split like perl
|
|
3689 (let ((start 0) list)
|
|
3690 (while (string-match regexp string start)
|
|
3691 (setq list (cons (substring string start (match-beginning 0)) list))
|
|
3692 (setq start (match-end 0)))
|
|
3693 (setq list (nreverse (cons (substring string start) list)))))
|
|
3694
|
|
3695 (defun reftex-allow-for-ctrl-m (string)
|
|
3696 ;; convert STRING into a regexp, allowing ^M for \n
|
|
3697 (let ((start -2))
|
|
3698 (setq string (regexp-quote string))
|
|
3699 (while (setq start (string-match "[\n\r]" string (+ 3 start)))
|
|
3700 (setq string (replace-match "[\n\r]" nil t string)))
|
|
3701 string))
|
|
3702
|
|
3703 (defun reftex-delete-list (elt-list list)
|
|
3704 ;; like delete, but with a list of things to delete
|
|
3705 ;; (original code from Rory Molinari)
|
|
3706 (while elt-list
|
|
3707 (setq list (delete (car elt-list) list)
|
|
3708 elt-list (cdr elt-list)))
|
|
3709 list)
|
|
3710
|
|
3711 (defun reftex-get-buffer-visiting (file)
|
|
3712 ;; return a buffer visiting FILE
|
|
3713 (cond
|
|
3714 ((fboundp 'find-buffer-visiting) ; Emacs
|
|
3715 (find-buffer-visiting file))
|
|
3716 ((boundp 'find-file-compare-truenames) ; XEmacs
|
|
3717 (let ((find-file-compare-truenames t))
|
|
3718 (get-file-buffer file)))
|
|
3719 (t (error "Please report this problem to dominik@strw.leidenuniv.nl"))))
|
|
3720
|
|
3721 (defun reftex-get-file-buffer-force (file &optional mark-to-kill)
|
|
3722 ;; Return a buffer visiting file. Make one, if necessary.
|
|
3723 ;; If neither such a buffer no the file exist, return nil.
|
|
3724 ;; If MARK-TO-KILL in non-nil, put any new buffers into the kill list."
|
|
3725
|
|
3726 (let ((buf (reftex-get-buffer-visiting file)))
|
|
3727 (cond
|
|
3728 (buf buf)
|
|
3729 ((file-exists-p file)
|
|
3730 (setq buf (find-file-noselect file))
|
|
3731 (if mark-to-kill
|
|
3732 (add-to-list 'reftex-buffers-to-kill buf))
|
|
3733 buf)
|
|
3734 (t nil))))
|
|
3735
|
|
3736 (defun reftex-splice-symbols-into-list (list alist)
|
|
3737 ;; Splice the association in ALIST of any symbols in LIST into the list.
|
|
3738 ;; Return new list.
|
|
3739 (let (rtn tmp)
|
|
3740 (while list
|
|
3741 (while (and (not (null (car list)))
|
|
3742 (symbolp (car list)))
|
|
3743 (setq tmp (car list))
|
|
3744 (cond
|
|
3745 ((assoc tmp alist)
|
|
3746 (setq list (append (cdr (cdr (assoc tmp alist))) (cdr list))))
|
|
3747 (t
|
|
3748 (error "Cannot treat symbol %s in reftex-label-alist"
|
|
3749 (symbol-name tmp)))))
|
|
3750 (setq rtn (cons (car list) rtn)
|
|
3751 list (cdr list)))
|
|
3752 (nreverse rtn)))
|
|
3753
|
|
3754 (defun reftex-uniquify (alist &optional keep-list)
|
|
3755 ;; Return a list of all elements in ALIST, but each car only once
|
|
3756 ;; Elements of KEEP-LIST are not removed even if duplicate
|
|
3757 (let (new elm)
|
|
3758 (while alist
|
|
3759 (setq elm (car alist)
|
|
3760 alist (cdr alist))
|
|
3761 (if (or (member (car elm) keep-list)
|
|
3762 (not (assoc (car elm) new)))
|
|
3763 (setq new (cons elm new))))
|
|
3764 (setq new (nreverse new))
|
|
3765 new))
|
|
3766
|
|
3767 (defun reftex-use-fonts ()
|
|
3768 ;; Return t if we can and want to use fonts
|
|
3769 (and window-system
|
|
3770 reftex-use-fonts
|
|
3771 (boundp 'font-lock-keyword-face)))
|
|
3772
|
|
3773 ;; Highlighting uses overlays. If this is for XEmacs, we need to load
|
|
3774 ;; the overlay library, available in version 19.15
|
|
3775 (and (not (fboundp 'make-overlay))
|
|
3776 (condition-case nil
|
|
3777 (require 'overlay)
|
|
3778 ('error
|
|
3779 (error "RefTeX needs overlay emulation (available in XEmacs 19.15)"))))
|
|
3780
|
|
3781 ;; We keep a vector with several different overlays to do our highlighting.
|
|
3782 (defvar reftex-highlight-overlays [nil nil])
|
|
3783
|
|
3784 ;; Initialize the overlays
|
|
3785 (aset reftex-highlight-overlays 0 (make-overlay 1 1))
|
|
3786 (overlay-put (aref reftex-highlight-overlays 0) 'face 'highlight)
|
|
3787 (aset reftex-highlight-overlays 1 (make-overlay 1 1))
|
|
3788 (overlay-put (aref reftex-highlight-overlays 1) 'face 'highlight)
|
|
3789
|
|
3790 ;; Two functions for activating and deactivation highlight overlays
|
|
3791 (defun reftex-highlight (index begin end &optional buffer)
|
|
3792 "Highlight a region with overlay INDEX."
|
|
3793 (move-overlay (aref reftex-highlight-overlays index)
|
|
3794 begin end (or buffer (current-buffer))))
|
|
3795 (defun reftex-unhighlight (index)
|
|
3796 "Detatch overlay INDEX."
|
|
3797 (delete-overlay (aref reftex-highlight-overlays index)))
|
|
3798
|
|
3799 (defun reftex-highlight-shall-die ()
|
|
3800 ;; Function used in pre-command-hook to remove highlights
|
|
3801 (remove-hook 'pre-command-hook 'reftex-highlight-shall-die)
|
|
3802 (reftex-unhighlight 0))
|
|
3803
|
|
3804 ;;; ---------------------------------------------------------------------------
|
|
3805 ;;;
|
|
3806 ;;; Cursor position after insertion of forms
|
|
3807
|
|
3808 (defun reftex-position-cursor ()
|
|
3809 ;; Search back to question mark, delete it, leave point there
|
|
3810 (if (search-backward "\?" (- (point) 100) t)
|
|
3811 (delete-char 1)))
|
|
3812
|
|
3813 (defun reftex-item ()
|
|
3814 "Insert an \\item and provide a label if the environments supports that."
|
|
3815 (interactive)
|
|
3816 (let ((env (car
|
|
3817 (reftex-what-environment '("itemize" "enumerate" "eqnarray")))))
|
|
3818
|
|
3819 (if (and env (not (bolp))) (newline))
|
|
3820
|
|
3821 (cond
|
|
3822
|
|
3823 ((string= env "eqnarray")
|
|
3824 (if (not (bolp))
|
|
3825 (newline))
|
|
3826 (reftex-label env)
|
|
3827 (insert "\n & & ")
|
|
3828 (beginning-of-line 1))
|
|
3829
|
|
3830 ((string= env "itemize")
|
|
3831 (newline)
|
|
3832 (insert "\\item "))
|
|
3833
|
|
3834 ((string= env "enumerate")
|
|
3835 (newline)
|
|
3836 (insert "\\item")
|
|
3837 (reftex-label env)
|
|
3838 (insert " "))
|
|
3839 (t
|
|
3840 (error "\\item command does not make sense here...")))))
|
|
3841
|
|
3842 ;;; ---------------------------------------------------------------------------
|
|
3843 ;;; ---------------------------------------------------------------------------
|
|
3844 ;;; ---------------------------------------------------------------------------
|
|
3845 ;;;
|
|
3846 ;;; Data Section: Definition of large constants
|
|
3847
|
|
3848
|
|
3849 (defconst reftex-label-alist-builtin
|
|
3850 '(
|
|
3851 (LaTeX
|
|
3852 "LaTeX default environments"
|
|
3853 ("section" ?s "sec:" "~\\ref{%s}" t
|
|
3854 ("Part" "Chapter" "Chap." "Section" "Sec." "Sect." "Paragraph" "Par."
|
|
3855 "\\S" "Teil" "Kapitel" "Kap." "Abschnitt" ))
|
|
3856
|
|
3857 ("enumerate" ?n "item:" "~\\ref{%s}" "\\\\item\\(\\[[^]]*\\]\\)?"
|
|
3858 ("Item" "Punkt"))
|
|
3859
|
|
3860 ("equation" ?e "eq:" "~(\\ref{%s})" t
|
|
3861 ("Equation" "Eq." "Eqn." "Gleichung" "Gl."))
|
|
3862 ("eqnarray" ?e "eq:" nil "\\\\begin{eqnarray}\\|\\\\\\\\")
|
|
3863
|
|
3864 ("figure" ?f "fig:" "~\\ref{%s}" "\\\\caption\\(\\[[^]]*\\]\\)?{"
|
|
3865 ("Figure" "Fig." "Abbildung" "Abb."))
|
|
3866 ("figure*" ?f nil nil "\\\\caption\\(\\[[^]]*\\]\\)?{")
|
|
3867
|
|
3868 ("table" ?t "tab:" "~\\ref{%s}" "\\\\caption\\(\\[[^]]*\\]\\)?{"
|
|
3869 ("Table" "Tab." "Tabelle"))
|
|
3870 ("table*" ?t nil nil "\\\\caption\\(\\[[^]]*\\]\\)?{")
|
|
3871
|
|
3872 ("any" ?\ " " "\\ref{%s}" nil))
|
|
3873
|
|
3874 (Sideways
|
|
3875 "Sidewaysfigure and sidewaystable"
|
|
3876 ("sidewaysfigure" ?f nil nil "\\\\caption\\(\\[[^]]*\\]\\)?{")
|
|
3877 ("sidewaystable" ?t nil nil "\\\\caption\\(\\[[^]]*\\]\\)?{"))
|
|
3878
|
|
3879 (AMSTeX
|
|
3880 "AMS-LaTeX: amsmath package environents"
|
|
3881 ("align" ?e "eq:" "~\\eqref{%s}" "\\\\begin{align}\\|\\\\\\\\")
|
|
3882 ("gather" ?e "eq:" nil "\\\\begin{gather}\\|\\\\\\\\")
|
|
3883 ("multline" ?e "eq:" nil t)
|
|
3884 ("flalign" ?e "eq:" nil "\\\\begin{flalign}\\|\\\\\\\\")
|
|
3885 ("alignat" ?e "eq:" nil "\\\\begin{alignat}{[0-9]*}\\|\\\\\\\\"))
|
|
3886
|
|
3887 (AASTeX
|
|
3888 "AAS deluxetable environment"
|
|
3889 ("deluxetable" ?t "tab:" nil "\\\\caption{")))
|
|
3890 "The default label environment descriptions.")
|
|
3891
|
|
3892 ;;; ---------------------------------------------------------------------------
|
|
3893 ;;;
|
|
3894 ;;; Functions to compile the tables, reset the mode etc.
|
|
3895
|
|
3896 (defun reftex-reset-mode ()
|
|
3897 "Reset RefTeX Mode. Required to implement changes to some list variables.
|
|
3898 This function will compile the information in reftex-label-alist and similar
|
|
3899 variables. It is called when RefTeX is first used, and after changes to
|
|
3900 these variables via reftex-add-to-label-alist."
|
|
3901 (interactive)
|
|
3902
|
|
3903 ; record that we have done this
|
|
3904 (setq reftex-tables-dirty nil)
|
|
3905
|
|
3906 ;; To update buffer-local variables
|
|
3907 (hack-local-variables)
|
|
3908 (message "updating internal tables...")
|
|
3909 (reftex-compute-ref-cite-tables)
|
|
3910 (message "updating internal tables... done")
|
|
3911 (reftex-reset-scanning-information))
|
|
3912
|
|
3913 (defun reftex-reset-scanning-information ()
|
|
3914 "Reset the symbols containing information from buffer scanning.
|
|
3915 This enforces rescanning the buffer on next use."
|
|
3916 (if (and (string= reftex-last-toc-master (reftex-TeX-master-file))
|
|
3917 (get-buffer "*toc*"))
|
|
3918 (kill-buffer "*toc*"))
|
|
3919 (let ((symlist reftex-multifile-symbols)
|
|
3920 symbol)
|
|
3921 (while symlist
|
|
3922 (setq symbol (car symlist)
|
|
3923 symlist (cdr symlist))
|
|
3924 (if (and (symbolp (symbol-value symbol))
|
|
3925 (not (null (symbol-value symbol))))
|
|
3926 (set (symbol-value symbol) nil)))))
|
|
3927
|
|
3928 (defun reftex-compute-ref-cite-tables ()
|
|
3929 ;; Update ref and cite tables
|
|
3930
|
|
3931 (interactive)
|
|
3932
|
|
3933 ;; Compile information in reftex-label-alist
|
|
3934 (let ((tmp (reftex-uniquify (reftex-splice-symbols-into-list
|
|
3935 (append
|
|
3936 reftex-label-alist
|
|
3937 reftex-label-alist-external-add-ons
|
|
3938 reftex-default-label-alist-entries)
|
|
3939 reftex-label-alist-builtin)
|
|
3940 '(nil)))
|
|
3941 entry env-or-mac typekeychar typekey prefix regexp
|
|
3942 fmt wordlist cmd qh-list)
|
|
3943
|
|
3944 (setq reftex-words-to-typekey-alist nil
|
|
3945 reftex-typekey-list nil
|
|
3946 reftex-typekey-to-format-alist nil
|
|
3947 reftex-typekey-to-prefix-alist nil
|
|
3948 reftex-env-or-mac-alist nil
|
|
3949 reftex-label-env-list nil
|
|
3950 reftex-label-mac-list nil)
|
|
3951 (while tmp
|
|
3952 (catch 'next-entry
|
|
3953 (setq entry (car tmp)
|
|
3954 env-or-mac (car entry)
|
|
3955 entry (cdr entry)
|
|
3956 tmp (cdr tmp))
|
|
3957 (if (null env-or-mac)
|
|
3958 (setq env-or-mac ""))
|
|
3959 (if (stringp (car entry))
|
|
3960 ;; This is before version 2.00 - convert entry to new format
|
|
3961 ;; This is just to keep old users happy
|
|
3962 (setq entry (cons (string-to-char (car entry))
|
|
3963 (cons (concat (car entry) ":")
|
|
3964 (cdr entry)))))
|
|
3965 (setq typekeychar (nth 0 entry)
|
|
3966 typekey (char-to-string typekeychar)
|
|
3967 prefix (nth 1 entry)
|
|
3968 fmt (nth 2 entry)
|
|
3969 regexp (nth 3 entry)
|
|
3970 wordlist (nth 4 entry))
|
|
3971 (if (stringp wordlist)
|
|
3972 ;; This is before version 2.04 - convert to new format
|
|
3973 (setq wordlist (nthcdr 4 entry)))
|
|
3974 (if typekey
|
|
3975 (add-to-list 'reftex-typekey-list typekey))
|
|
3976 (if (and typekey prefix)
|
|
3977 (add-to-list 'reftex-typekey-to-prefix-alist (cons typekey prefix)))
|
|
3978 (cond
|
|
3979 ((string-match "\\`\\\\" env-or-mac)
|
|
3980 ;; It's a macro
|
|
3981 (add-to-list 'reftex-label-mac-list env-or-mac))
|
|
3982 (t
|
|
3983 (or (string= env-or-mac "any")
|
|
3984 (string= env-or-mac "")
|
|
3985 (add-to-list 'reftex-label-env-list env-or-mac))))
|
|
3986 (and fmt
|
|
3987 (not (assoc typekey reftex-typekey-to-format-alist))
|
|
3988 (setq reftex-typekey-to-format-alist
|
|
3989 (cons (cons typekey fmt)
|
|
3990 reftex-typekey-to-format-alist)))
|
|
3991 (and (not (string= env-or-mac "any"))
|
|
3992 (not (string= env-or-mac ""))
|
|
3993 (not (assoc env-or-mac reftex-env-or-mac-alist))
|
|
3994 (setq reftex-env-or-mac-alist
|
|
3995 (cons (list env-or-mac typekey regexp)
|
|
3996 reftex-env-or-mac-alist)))
|
|
3997 (while (and wordlist (stringp (car wordlist)))
|
|
3998 (or (assoc (car wordlist) reftex-words-to-typekey-alist)
|
|
3999 (setq reftex-words-to-typekey-alist
|
|
4000 (cons (cons (downcase (car wordlist)) typekey)
|
|
4001 reftex-words-to-typekey-alist)))
|
|
4002 (setq wordlist (cdr wordlist)))
|
|
4003 (cond
|
|
4004 ((string= "" env-or-mac) nil)
|
|
4005 ((assoc typekey qh-list)
|
|
4006 (setcdr (assoc typekey qh-list)
|
|
4007 (concat (cdr (assoc typekey qh-list)) " " env-or-mac)))
|
|
4008 (t
|
|
4009 (setq qh-list (cons (cons typekey env-or-mac) qh-list))))))
|
|
4010
|
|
4011 (setq qh-list (nreverse qh-list))
|
|
4012 (setq reftex-typekey-to-prefix-alist
|
|
4013 (nreverse reftex-typekey-to-prefix-alist))
|
|
4014 (setq reftex-type-query-prompt
|
|
4015 (concat "Label type: "
|
|
4016 (mapconcat '(lambda(x)
|
|
4017 (format "[%s]" (car x)))
|
|
4018 qh-list " ")
|
|
4019 " (?=Help)"))
|
|
4020 (setq reftex-type-query-help
|
|
4021 (concat "SELECT A LABEL TYPE:\n--------------------\n"
|
|
4022 (mapconcat '(lambda(x)
|
|
4023 (format " [%s] %s"
|
|
4024 (car x) (cdr x)))
|
|
4025 qh-list "\n")))))
|
|
4026
|
|
4027 ;;; Keybindings --------------------------------------------------------------
|
|
4028
|
|
4029 (define-key reftex-mode-map "\C-c-" 'reftex-item)
|
|
4030 (define-key reftex-mode-map "\C-c=" 'reftex-toc)
|
|
4031 (define-key reftex-mode-map "\C-c(" 'reftex-label)
|
|
4032 (define-key reftex-mode-map "\C-c)" 'reftex-reference)
|
|
4033 (define-key reftex-mode-map "\C-c[" 'reftex-citation)
|
|
4034 (define-key reftex-mode-map "\C-c&" 'reftex-view-crossref)
|
|
4035
|
|
4036 ;; If the user requests so, she can have a few more bindings:
|
|
4037 (cond
|
|
4038 (reftex-extra-bindings
|
|
4039 (define-key reftex-mode-map "\C-ct" 'reftex-toc)
|
|
4040 (define-key reftex-mode-map "\C-cl" 'reftex-label)
|
|
4041 (define-key reftex-mode-map "\C-cr" 'reftex-reference)
|
|
4042 (define-key reftex-mode-map "\C-cc" 'reftex-citation)
|
|
4043 (define-key reftex-mode-map "\C-cv" 'reftex-view-crossref)
|
|
4044 (define-key reftex-mode-map "\C-cg" 'reftex-grep-document)
|
|
4045 (define-key reftex-mode-map "\C-cs" 'reftex-search-document)))
|
|
4046
|
|
4047 ;;; Menus --------------------------------------------------------------------
|
|
4048
|
|
4049 ;; Define a menu for the menu bar if Emacs is running under X
|
|
4050
|
|
4051 (require 'easymenu)
|
|
4052
|
|
4053 (easy-menu-define
|
|
4054 reftex-mode-menu reftex-mode-map
|
|
4055 "Menu used in RefTeX mode"
|
|
4056 '("Ref"
|
|
4057 ["Table of Contents" reftex-toc t]
|
|
4058 "----"
|
|
4059 ["\\label" reftex-label t]
|
|
4060 ["\\ref" reftex-reference t]
|
|
4061 ["\\cite" reftex-citation t]
|
|
4062 ["View crossref" reftex-view-crossref t]
|
|
4063 "----"
|
|
4064 ("Search and Replace"
|
|
4065 ["Search whole document" reftex-search-document t]
|
|
4066 ["Replace in document" reftex-query-replace-document t]
|
|
4067 ["Grep on document" reftex-grep-document t]
|
|
4068 "----"
|
|
4069 ["Find duplicate labels" reftex-find-duplicate-labels t]
|
|
4070 ["Change label and refs" reftex-change-label t]
|
|
4071 "----"
|
|
4072 ["Create TAGS file" reftex-create-tags-file t])
|
|
4073 "----"
|
|
4074 ["Parse document" reftex-parse-document t]
|
|
4075 ["Reset RefTeX Mode" reftex-reset-mode t]
|
|
4076 ["Customize RefTeX" reftex-customize t]))
|
|
4077
|
|
4078 ;;; Run Hook ------------------------------------------------------------------
|
|
4079
|
|
4080 (run-hooks 'reftex-load-hook)
|
|
4081
|
|
4082 ;;; That's it! ----------------------------------------------------------------
|
|
4083
|
|
4084 ; Make sure tabels are compiled
|
|
4085 (message "updating internal tables...")
|
|
4086 (reftex-compute-ref-cite-tables)
|
|
4087 (setq reftex-tables-dirty nil)
|
|
4088
|
|
4089 (provide 'reftex)
|
|
4090
|
|
4091 ;;;============================================================================
|
|
4092
|
|
4093 ;;; reftex.el end here
|