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