comparison lisp/modes/reftex.el @ 207:e45d5e7c476e r20-4b2

Import from CVS: tag r20-4b2
author cvs
date Mon, 13 Aug 2007 10:03:52 +0200
parents a2f645c6b9f8
children
comparison
equal deleted inserted replaced
206:d3e9274cbc4e 207:e45d5e7c476e
1 ;; reftex.el --- Minor mode for doing \label, \ref and \cite in LaTeX 1 ;; reftex.el --- Minor mode for doing \label, \ref and \cite in LaTeX
2
3 ;; Copyright (c) 1997 Free Software Foundation, Inc. 2 ;; Copyright (c) 1997 Free Software Foundation, Inc.
4 3
4 ;; Version: 3.7
5 ;; Author: Carsten Dominik <dominik@strw.LeidenUniv.nl> 5 ;; Author: Carsten Dominik <dominik@strw.LeidenUniv.nl>
6 ;; Keywords: tex 6 ;; Keywords: tex
7 7
8 ;; This file is part of GNU Emacs. 8 ;; This file is part of GNU Emacs.
9 9
28 ;; 28 ;;
29 ;; RefTeX is a minor mode with distinct support for \ref, \label and 29 ;; RefTeX is a minor mode with distinct support for \ref, \label and
30 ;; \cite commands in (multi-file) LaTeX documents. 30 ;; \cite commands in (multi-file) LaTeX documents.
31 ;; Labels are created semi-automatically. Definition context of labels is 31 ;; Labels are created semi-automatically. Definition context of labels is
32 ;; provided when creating a reference. Citations are simplified with 32 ;; provided when creating a reference. Citations are simplified with
33 ;; efficient database lookup. 33 ;; efficient database lookup. A table of contents buffer provides easy
34 ;; access to any part of a document.
34 ;; 35 ;;
35 ;; To turn RefTeX Minor Mode on and off in a particular buffer, use 36 ;; To turn RefTeX Minor Mode on and off in a particular buffer, use
36 ;; `M-x reftex-mode'. 37 ;; `M-x reftex-mode'.
37 ;; 38 ;;
38 ;; To turn on RefTeX Minor Mode for all LaTeX files, add one of the 39 ;; To turn on RefTeX Minor Mode for all LaTeX files, add one of the
39 ;; following lines to your .emacs file: 40 ;; following lines to your .emacs file:
40 ;; 41 ;;
41 ;; (add-hook 'LaTeX-mode-hook 'turn-on-reftex) ; with AUCTeX LaTeX mode 42 ;; (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 ;; (add-hook 'latex-mode-hook 'turn-on-reftex) ; with Emacs latex mode
43 ;; 44 ;;
44 ;; For key bindings, see further down in this documentation. 45 ;; For default key bindings, see further down in this documentation.
45 ;; 46 ;;
46 ;;--------------------------------------------------------------------------- 47 ;;---------------------------------------------------------------------------
47 ;; 48 ;;
49 ;; CONTENTS
50 ;; --------
51 ;;
52 ;; Overview............................ All you need to know to get started.
53 ;;
54 ;; Configuration....................... How to configure RefTeX.
55 ;; Configuration Examples........... Tutorial examples.
56 ;; Hooks............................ Available hooks.
57 ;; Configuration Variables.......... Complete listing.
58 ;; Key Bindings........................ A list of default bindings.
59 ;; Multifile Documents................. Documents spread over many files.
60 ;; References to Other Documents....... RefTeX and the LaTeX package `xr'.
61 ;; Optimizations for Large Documents... How to improve speed and memory use.
62 ;; Related Packages.................... Other Emacs packages.
63 ;; Known Bugs and Work-Arounds......... First aid.
64 ;; Author.............................. Who wrote RefTeX and who helped.
65 ;; History............................. What was new in which version.
66 ;;---------------------------------------------------------------------------
67 ;;
48 ;; OVERVIEW 68 ;; OVERVIEW
49 ;; 69 ;; ========
50 ;; 1. USING \label AND \ref. Labels and references are one of the 70 ;;
51 ;; strong points of LaTeX. But, in documents with hundreds of 71 ;; 1. USING \label AND \ref. Labels and references are one of the strong
52 ;; equations, figures, tables etc. it becomes quickly impossible to 72 ;; points of LaTeX. But, in documents with hundreds of equations,
53 ;; find good label names and to actually remember them. Then, also 73 ;; figures, tables etc. it becomes quickly impossible to find good label
54 ;; completion of labels in not enough. One actually needs to see the 74 ;; names and to actually remember them. Then, also completion of labels
55 ;; context of the label definition to find the right one. 75 ;; is not enough. One actually needs to see the context of the label
56 ;; 76 ;; definition to find the right one.
57 ;; - RefTeX distinguishes labels for different environments. It 77 ;;
58 ;; always knows if a certain label references a figure, table 78 ;; - RefTeX distinguishes labels for different environments. It always
59 ;; etc.. You can configure RefTeX to recognize any additional 79 ;; knows if a certain label references a figure, table etc.. You can
60 ;; labeled environments you might have defined yourself. 80 ;; configure RefTeX to recognize any additional labeled environments
81 ;; you have defined yourself.
61 ;; 82 ;;
62 ;; - RefTeX defines automatically unique labels. Type `C-c (' 83 ;; - RefTeX defines automatically unique labels. Type `C-c ('
63 ;; (reftex-label) to insert a label at point. RefTeX will either 84 ;; (`reftex-label') to insert a label at point. RefTeX will either
64 ;; - derive a label from context (default for section labels) 85 ;; - derive a label from context (default for section labels)
65 ;; - insert a simple label consisting of a prefix and a number 86 ;; - insert a simple label consisting of a prefix and a number
66 ;; (default for equations and enumerate items) or 87 ;; (default for equations,enumerate items, and footnotes) or
67 ;; - prompt for a label string (figures and tables). 88 ;; - prompt for a label string (figures and tables).
68 ;; Which labels are created how can be controlled with the variable 89 ;; Which labels are created how can be controlled with the variable
69 ;; `reftex-insert-label-flags'. 90 ;; `reftex-insert-label-flags'.
70 ;; 91 ;;
71 ;; - Referencing labels is a snap and I promise you'll love it. 92 ;; - Referencing labels is a snap and I promise you'll love it. In
72 ;; In order to make a reference, type `C-c )' (`reftex-reference'). 93 ;; order to make a reference, type `C-c )' (`reftex-reference'). This
73 ;; This shows an outline of the documents with all labels of a 94 ;; shows an outline of the document with all labels of a certain type
74 ;; certain type (figure, equation,...) and context of the label 95 ;; (figure, equation,...) and context of the label definition.
75 ;; definition. Selecting one of the labels inserts a \ref macro 96 ;; Selecting one of the labels inserts a \ref macro into the original
76 ;; into the original buffer. Online help during the selection is 97 ;; buffer. Online help during the selection is available with `?'.
77 ;; available with `?'. 98 ;;
78 ;; 99 ;; 2. CITATIONS. After typing `C-c [' (`reftex-citation'), RefTeX will let
79 ;; 2. CITATIONS. After typing `C-c [' (`reftex-citation'), RefTeX will 100 ;; you specify a regexp to search in current BibTeX database files (as
80 ;; let you specify a regexp to search in current BibTeX database files 101 ;; specified in the \bibliography command) and pull out a formatted list
81 ;; (as specified in the \bibliography command) and pull out a formatted 102 ;; of matches for you to choose from. The list is *formatted* and
82 ;; list of matches for you to choose from. The list is *formatted* and 103 ;; sorted, thus much easier to read than the raw database entries. The
83 ;; thus much easier to read than the raw database entries. It can also 104 ;; text inserted into the buffer is by default just `\cite{KEY}', but
84 ;; be sorted. The text inserted into the buffer is by default just 105 ;; can also contain author names and the year in a configurable way.
85 ;; `\cite{KEY}', but can also contain author names and the year in a 106 ;; See documentation of the variable `reftex-cite-format'.
86 ;; configurable way. See documentation of the variable 107 ;;
87 ;; `reftex-cite-format'. 108 ;; 3. TABLE OF CONTENTS. Typing `C-c =' (`reftex-toc') will show a table
88 ;; 109 ;; of contents of the document. From that buffer, you can jump quickly
89 ;; 3. TABLE OF CONTENTS. Typing `C-c =' (`reftex-toc') will show 110 ;; to every part of your document. This is similar to imenu, only it
90 ;; a table of contents of the document. From that buffer, you can 111 ;; works for entire multifile documents and uses the keyboard rather
91 ;; jump quickly to every part of your document. This is similar to 112 ;; than the mouse. The initial version of this function was contributed
92 ;; imenu, only it works for entire multifile documents and uses the 113 ;; by Stephen Eglen.
93 ;; keyboard rather than the mouse. The initial version of this 114 ;;
94 ;; function was contributed by Stephen Eglen. 115 ;; 4. MULTIFILE DOCUMENTS are fully supported by RefTeX. Such documents
95 ;; 116 ;; consist of a master file and many other files being included via
96 ;; 4. MULTIFILE DOCUMENTS are supported in the same way as by AUCTeX. 117 ;; \input or \include. RefTeX will provide cross referencing
97 ;; I.e. if a source file is not a full LaTeX document by itself, 118 ;; information from all files which are part of the document. See
98 ;; but included by another file, you may specify the name of 119 ;; `RefTeX and Multifile Documents' further down in the documentation
99 ;; the (top level) master file in a local variable section at the 120 ;; for more information on this topic.
100 ;; end of the source file, like so: 121 ;;
101 ;; 122 ;; 5. DOCUMENT PARSING. RefTeX needs to parse the document in order to
102 ;; %%% Local Variables: 123 ;; find labels and other information. It will do it automatically once,
103 ;; %%% TeX-master: my_master.tex 124 ;; when you start working with a document. Re-parsing should not be
104 ;; %%% End: 125 ;; necessary too often since RefTeX updates its lists internally when
105 ;; 126 ;; you make a new label with `reftex-label'. To enforce reparsing,
106 ;; This will only take effect when you load the file next time or when 127 ;; call any of the functions `reftex-citation', `reftex-label',
107 ;; you reset RefTeX with M-x reftex-reset-mode. 128 ;; `reftex-reference', `reftex-toc' with a raw C-u prefix, or press the
108 ;; 129 ;; `r' key in the label menu and table of contents buffer.
109 ;; RefTeX will also recognize the file variable tex-main-file. This 130 ;;---------------------------------------------------------------------------
110 ;; variable is used by the Emacs TeX modes and works just like 131 ;;
111 ;; AUCTeX's TeX-master variable. See the documentation of your TeX/LaTeX
112 ;; modes.
113 ;;
114 ;; RefTeX knows about all files related to a document via input and
115 ;; include. It provides functions to run regular expression searches and
116 ;; replaces over the entire document and to create a TAGS file.
117 ;;
118 ;; 5. DOCUMENT PARSING. RefTeX needs to parse the document in order to find
119 ;; labels and other information. It will do it automatically once, when
120 ;; you start working with a document. If you need to enforce reparsing
121 ;; later, call any of the functions `reftex-citation', `reftex-label',
122 ;; `reftex-reference', `reftex-toc' with a raw C-u prefix.
123 ;;
124 ;;-------------------------------------------------------------------------
125 ;;
126 ;; CONFIGURATION 132 ;; CONFIGURATION
127 ;; 133 ;; =============
128 ;; RefTeX contains many configurable options which change the way it works. 134 ;;
129 ;; 135 ;; RefTeX needs to be configured if you use labels to mark environments
130 ;; Most importantly, RefTeX needs to be configured if you use labels to 136 ;; defined by yourself (e.g. with `\newenvironment') or in packages not
131 ;; mark non-standard environments. RefTeX always understands LaTeX section 137 ;; included in the standard LaTeX distribution. RefTeX's default settings
132 ;; commands and the following environments: figure, figure*, 138 ;; make it recognize practically all labeled environments and macros
133 ;; sidewaysfigure, table, table*, sidewaystable, equation, eqnarray, 139 ;; discussed in `The LaTeX Companion' by Goossens, Mittelbach & Samarin,
134 ;; enumerate. For everythings else, it needs to be configured. 140 ;; Addison-Wesley 1994. These are:
135 ;; 141 ;;
136 ;; A good way to configure RefTeX is with the custom.el package by Per 142 ;; - figure, figure*, table, table*, equation, eqnarray, enumerate,
137 ;; Abrahamsen, shipped with Emacs 20 and XEmacs 19.15. To do this, just 143 ;; the \footnote macro (this is the LaTeX core stuff)
138 ;; say `M-x reftex-customize'. This will not work with older versions 144 ;; - align, gather, multline, flalign, alignat, xalignat, xxalignat,
139 ;; of custom.el. 145 ;; subequations (from AMS-LaTeX's amsmath.sty package)
140 ;; 146 ;; - the \endnote macro (from endnotes.sty)
141 ;; Here is a complete list of the RefTeX configuration variables with 147 ;; - Beqnarray (fancybox.sty)
142 ;; their default settings. You could copy this list to your .emacs file 148 ;; - floatingfig (floatfig.sty)
143 ;; and change whatever is necessary. Each variable has an extensive 149 ;; - longtable (longtable.sty)
144 ;; documentation string. Look it up for more information! 150 ;; - figwindow, tabwindow (picinpar.sty)
145 ;; 151 ;; - sidewaysfigure, sidewaystable (rotating.sty)
146 ;; ;; Configuration Variables and User Options for RefTeX ------------------ 152 ;; - subfigure, subfigure*, the \subfigure macro (subfigure.sty)
147 ;; ;; Support for \label and \ref -------------------------------------- 153 ;; - supertabular (supertab.sty)
148 ;; (setq reftex-label-alist nil) 154 ;; - wrapfigure (wrapfig.sty)
149 ;; (setq reftex-default-label-alist-entries '(Sideways LaTeX)) 155 ;;
150 ;; (setq reftex-use-text-after-label-as-context nil) 156 ;; If you want to use any other labeled environments or macros, you need
151 ;; ;; Label insertion 157 ;; to configure RefTeX.
152 ;; (setq reftex-insert-label-flags '("s" "sft")) 158 ;;
153 ;; (setq reftex-derive-label-parameters '(3 20 t 1 "-" 159 ;; Per Abrahamsens custom.el package provides a simple way to do
154 ;; ("the" "on" "in" "off" "a" "for" "by" "of" "and" "is"))) 160 ;; configuration. To try it out, use `M-x reftex-customize'.
155 ;; (setq reftex-label-illegal-re "[\000-\040\177-\377\\\\#$%&~^_{}]") 161 ;;
156 ;; (setq reftex-abbrev-parameters '(4 2 "^saeiou" "aeiou")) 162 ;; CONFIGURATION EXAMPLES
157 ;; ;; Label referencing 163 ;; ----------------------
158 ;; (setq reftex-label-menu-flags '(t t nil nil nil nil)) 164 ;;
159 ;; (setq reftex-guess-label-type t) 165 ;; Suppose you are working with AMS-LaTeX amsmath package (with its math
160 ;; ;; BibteX citation configuration ---------------------------------------- 166 ;; environments like `align', `multline' etc.). RefTeX is preconfigured to
161 ;; (setq reftex-bibpath-environment-variables '("BIBINPUTS" "TEXBIB")) 167 ;; recognize these - so there is nothing you have to do.
162 ;; (setq reftex-bibfile-ignore-list nil) 168 ;;
163 ;; (setq reftex-sort-bibtex-matches 'reverse-year) 169 ;; Suppose you are also using `\newtheorem' in LaTeX in order to define two
164 ;; (setq reftex-cite-format 'reftex-cite-format-default) 170 ;; new environments `theorem' and `axiom'
165 ;; ;; Table of contents configuration --------------------------------------
166 ;; (setq reftex-toc-follow-mode nil)
167 ;; ;; Miscellaneous configurations -----------------------------------------
168 ;; (setq reftex-extra-bindings nil)
169 ;; (setq reftex-plug-into-AUCTeX nil)
170 ;; (setq reftex-use-fonts t)
171 ;; (setq reftex-keep-temporary-buffers t)
172 ;; (setq reftex-auto-show-entry t)
173 ;;
174 ;; CONFIGURATION EXAMPLES:
175 ;; =======================
176 ;;
177 ;; Suppose you are working with AMS-LaTeX amsmath package (with its math
178 ;; environments like `align', `multiline' etc.). Here is how you would
179 ;; configure RefTeX to recognize these environments:
180 ;;
181 ;; (setq reftex-label-alist '(AMSTeX))
182 ;;
183 ;; This is very easy since RefTeX has builtin support for AMS-LaTeX.
184 ;; Suppose, however, you are also
185 ;;
186 ;; - using "\newtheorem" in LaTeX in order to define two new environments
187 ;; "Theorem" and "Axiom" like this:
188 ;; 171 ;;
189 ;; \newtheorem{axiom}{Axiom} 172 ;; \newtheorem{axiom}{Axiom}
190 ;; \newtheorem{theorem}{Theorem} 173 ;; \newtheorem{theorem}{Theorem}
191 ;; 174 ;;
192 ;; - making your figures not directly with the figure environment, but with 175 ;; to be used like this:
193 ;; a macro like 176 ;;
194 ;; 177 ;; \begin{axiom}
195 ;; \newcommand{\myfig}[4][tbp]{ 178 ;; \label{ax:first}
196 ;; \begin{figure}[#1] 179 ;; ....
197 ;; \epsimp[#4]{#2} 180 ;; \end{axiom}
198 ;; \caption{#3} 181 ;;
199 ;; \end{figure}} 182 ;; So we need to tell RefTeX that `theorem' and `axiom' are new labeled
200 ;; 183 ;; environments which define their own label categories. Here is how:
201 ;; which would be called like 184 ;;
202 ;; 185 ;; (setq reftex-label-alist
203 ;; \myfig{filename}{\label{fig:13} caption text}{1} 186 ;; '(("axiom" ?a "ax:" "~\\ref{%s}" nil ("Axiom" "Ax."))
204 ;; 187 ;; ("theorem" ?h "thr:" "~\\ref{%s}" t ("Theorem" "Theor." "Th."))))
205 ;; Here is how to tell RefTeX to also recognize Theorem and Axiom as 188 ;;
206 ;; labeled environments, and that any labels defined inside the \myfig 189 ;; The type indicator characters ?a and ?h are used for prompts when RefTeX
207 ;; macro are figure labels: 190 ;; queries for a label type. Note that `h' was chosen for `theorem' since
208 ;; 191 ;; `t' is already taken by `table'. Note that also `s', `f', `e', `i', `n'
209 ;; (setq reftex-label-alist 192 ;; are already used for standard environments.
210 ;; '(AMSTeX
211 ;; ("axiom" ?a "ax:" "~\\ref{%s}" nil ("Axiom" "Ax."))
212 ;; ("theorem" ?h "thr:" "~\\ref{%s}" t ("Theorem" "Theor." "Th."))
213 ;; ("\\myfig" ?f "fig:" nil t)))
214 ;;
215 ;; The type indicator characters ?a and ?h are used for prompts when
216 ;; RefTeX queries for a label type. Note that "h" was chosen for "theorem"
217 ;; since "t" is already taken by "table". Note that also "s", "f", "e", "n"
218 ;; are taken by the standard environments.
219 ;; The automatic labels for Axioms and Theorems will look like "ax:23" or 193 ;; The automatic labels for Axioms and Theorems will look like "ax:23" or
220 ;; "thr:24". 194 ;; "thr:24".
221 ;; The "\ref{%s}" is a format string indicating how to insert references to 195 ;; The "\ref{%s}" is a format string indicating how to insert references to
222 ;; these labels. The nil format in the \myfig entry means to use the same 196 ;; these labels.
223 ;; format as other figure labels. 197 ;; The next item indicates how to grab context of the label definition.
224 ;; The next item indicates how to grab context of the label definition. 198 ;; - t means to get it from a default location (from the beginning of a
225 ;; - t means to get it from a default location (from the beginning of a \macro 199 ;; \macro or after the \begin statement). t is *not* a good choice for
226 ;; or after the \begin statement). t is *not* a good choice for eqnarray 200 ;; eqnarray and similar environments.
227 ;; and similar environments.
228 ;; - nil means to use the text right after the label definition. 201 ;; - nil means to use the text right after the label definition.
229 ;; - For more complex ways of getting context, see the docstring of 202 ;; - For more complex ways of getting context, see the docstring of
230 ;; `reftex-label-alist'. 203 ;; `reftex-label-alist'.
231 ;; The strings at the end of each entry are used to guess the correct label 204 ;; 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 205 ;; 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 206 ;; write: "As we have shown in Theorem" and then press `C-c )', RefTeX will
234 ;; know that you are looking for a Theorem label and restrict the labels in 207 ;; know that you are looking for a theorem label and restrict the menu to
235 ;; the menu to only these labels without even asking. 208 ;; only these labels without even asking.
236 ;; See also the documentation string of the variable `reftex-label-alist'. 209 ;;
237 ;; 210 ;; Depending on how you would like the label insertion and selection for
238 ;; Depending on how you would like the label insertion and selection for the 211 ;; the new environments to work, you might want to add the letters "a" and
239 ;; new environments to work, you might want to add the letters "a" and "h" 212 ;; "h" to some of the flags in the following variables:
240 ;; to some of the flags in the following variables: 213 ;;
241 ;; 214 ;; reftex-insert-label-flags reftex-label-menu-flags
242 ;; reftex-insert-label-flags 215 ;;
243 ;; reftex-label-menu-flags 216 ;; Suppose you want to make figures not directly with the figure
244 ;; 217 ;; environment, but with a macro like
245 ;; The individual flags in these variables can be set to t or nil to enable or 218 ;;
246 ;; disable the feature for all label types. They may also contain a string of 219 ;; \newcommand{\myfig}[5][tbp]{%
247 ;; label type letters in order to turn on the feature for those types only. 220 ;; \begin{figure}[#1]
221 ;; \epsimp[#5]{#2}
222 ;; \caption{#3}
223 ;; \label{#4}
224 ;; \end{figure}}
225 ;;
226 ;; which would be called like
227 ;;
228 ;; \myfig[htp]{filename}{caption text}{label}{1}
229 ;;
230 ;; Now we also need to tell RefTeX that the 4th argument of the \myfig
231 ;; macro is a figure label, and where to find the context.
232 ;;
233 ;; (setq reftex-label-alist
234 ;; '(("axiom" ?a "ax:" "~\\ref{%s}" nil ("Axiom" "Ax."))
235 ;; ("theorem" ?h "thr:" "~\\ref{%s}" t ("Theorem" "Theor." "Th."))
236 ;; ("\\myfig[]{}{}{*}{}" ?f nil nil 3)))
237 ;;
238 ;; The empty pairs of brackets indicate the different arguments of the
239 ;; \myfig macro. The `*' marks the label argument. `?f' indicates that
240 ;; this is a figure label which will be listed together with labels from
241 ;; normal figure environments. The nil entries for prefix and reference
242 ;; format mean to use the defaults for figure labels. The `3' for the
243 ;; context method means to grab the 3rd macro argument - the caption.
244 ;;
245 ;; As a side effect of this configuration, `reftex-label' will now insert
246 ;; the required naked label (without the \label macro) when point is
247 ;; directly after the opening parenthesis of a \myfig macro argument.
248 ;; 248 ;;
249 ;; ----- 249 ;; -----
250 ;; If you are writing in a language different from english you might want to 250 ;;
251 ;; add magic words for that language. Here is a German example: 251 ;; If you are writing in a language different from English you might want
252 ;; 252 ;; to add magic words for that language. Here is a German example:
253 ;; (setq reftex-label-alist 253 ;;
254 ;; (setq reftex-label-alist
254 ;; '((nil ?s nil nil nil ("Kapitel" "Kap." "Abschnitt" "Teil")) 255 ;; '((nil ?s nil nil nil ("Kapitel" "Kap." "Abschnitt" "Teil"))
255 ;; (nil ?e nil nil nil ("Gleichung" "Gl.")) 256 ;; (nil ?e nil nil nil ("Gleichung" "Gl."))
256 ;; (nil ?t nil nil nil ("Tabelle")) 257 ;; (nil ?t nil nil nil ("Tabelle"))
257 ;; (nil ?f nil nil nil ("Figur" "Abbildung" "Abb.")) 258 ;; (nil ?f nil nil nil ("Figur" "Abbildung" "Abb."))
258 ;; (nil ?n nil nil nil ("Punkt")))) 259 ;; (nil ?n nil nil nil ("Anmerkung" "Anm."))
260 ;; (nil ?i nil nil nil ("Punkt"))))
259 ;; 261 ;;
260 ;; Using nil as first item in each entry makes sure that this entry does 262 ;; Using nil as first item in each entry makes sure that this entry does
261 ;; not replace the original entry for that label type. 263 ;; not replace the original entry for that label type, but just adds magic
264 ;; words.
265 ;;
266 ;; -----
267 ;;
268 ;; Normally, RefTeX inserts equation references with parenthesis like
269 ;; "~(\ref{KEY})". If you want to change this to square brackets, use
270 ;;
271 ;; (setq reftex-label-alist '((nil ?e nil "~[\\ref{%s}]" nil nil)))
272 ;;
273 ;; In order to use the AMS-LaTeX \eqref macro instead, either of the
274 ;; following lines does the job.
275 ;;
276 ;; (setq reftex-label-alist '((nil ?e nil "~\\eqref{%s}" nil nil)))
277 ;; (setq reftex-label-alist '(AMSTeX))
278 ;;
279 ;; ----
280 ;;
281 ;; By default, citations are inserted simply as \cite{KEY}. You can have
282 ;; more complex citation commands with many available packages, most
283 ;; notably the harvard and natbib packages. RefTeX can be configured to
284 ;; support these and other styles by setting the variable
285 ;; `reftex-cite-format'. E.g., for the natbib package you would use
286 ;;
287 ;; (setq reftex-cite-format 'natbib)
288 ;;
289 ;; This can also be done as a file variable. For the full list of builtin
290 ;; options, try `M-x customize-variable RET reftex-cite-format RET'.
262 ;; 291 ;;
263 ;; HOOKS 292 ;; HOOKS
264 ;; ----- 293 ;; -----
265 ;; Loading reftex.el runs the hook `reftex-load-hook'. 294 ;; - Loading reftex.el runs the hook `reftex-load-hook'.
266 ;; Turning on reftex-mode runs `reftex-mode-hook'. 295 ;; - Turning on reftex-mode runs `reftex-mode-hook'.
267 ;; 296 ;; - Files visited literally are processed with
297 ;; `reftex-initialize-temporary-buffers' if that is a list of functions.
298 ;;
299 ;; CONFIGURATION VARIABLES
300 ;; -----------------------
301 ;;
302 ;; The best way to learn about all configuration variables is via the
303 ;; browser interface of the custom library. For reference, I am giving
304 ;; here a complete list.
305 ;;
306 ;; ;; Defining label environments
307 ;; reftex-default-label-alist-entries
308 ;; reftex-label-alist
309 ;; reftex-section-levels
310 ;; reftex-default-context-regexps
311 ;; reftex-use-text-after-label-as-context
312 ;; ;; Label insertion
313 ;; reftex-insert-label-flags
314 ;; reftex-derive-label-parameters
315 ;; reftex-label-illegal-re
316 ;; reftex-abbrev-parameters
317 ;; ;; Label referencing
318 ;; reftex-label-menu-flags
319 ;; reftex-level-indent
320 ;; reftex-refontify-context
321 ;; reftex-guess-label-type
322 ;; ;; BibteX citation configuration
323 ;; reftex-bibpath-environment-variables
324 ;; reftex-bibfile-ignore-list
325 ;; reftex-sort-bibtex-matches
326 ;; reftex-cite-format
327 ;; reftex-comment-citations
328 ;; reftex-cite-comment-format
329 ;; reftex-cite-punctuation
330 ;; ;; Table of contents configuration
331 ;; reftex-toc-follow-mode
332 ;; ;; Fine-tuning the parser
333 ;; reftex-keep-temporary-buffers
334 ;; reftex-initialize-temporary-buffers
335 ;; reftex-enable-partial-scans
336 ;; reftex-save-parse-info
337 ;; ;; Miscellaneous configurations
338 ;; reftex-extra-bindings
339 ;; reftex-plug-into-AUCTeX
340 ;; reftex-use-fonts
341 ;; reftex-auto-show-entry
342 ;; reftex-load-hook
343 ;; reftex-mode-hook
268 ;;------------------------------------------------------------------------- 344 ;;-------------------------------------------------------------------------
269 ;; 345 ;;
270 ;; KEY BINDINGS 346 ;; KEY BINDINGS
271 ;; 347 ;; ============
272 ;; All important functions of RefTeX can be reached from its menu which 348 ;;
273 ;; is installed in the menu bar as "Ref" menu. Only the more frequently used 349 ;; All RefTeX commands can be reached from its menu, the `Ref' menu on the
274 ;; functions have key bindings. 350 ;; menu bar. More frequently used commands have key bindings:
275 ;;
276 ;; Here is the default set of keybindings from RefTeX.
277 ;; 351 ;;
278 ;; C-c = reftex-toc 352 ;; C-c = reftex-toc
279 ;; C-c ( reftex-label 353 ;; C-c ( reftex-label
280 ;; C-c ) reftex-reference 354 ;; C-c ) reftex-reference
281 ;; C-c [ reftex-citation 355 ;; C-c [ reftex-citation
282 ;; C-c & reftex-view-crossref 356 ;; C-c & reftex-view-crossref
283 ;; 357 ;;
284 ;; I've used these bindings in order to avoid interfering with AUCTeX's 358 ;; These keys are chosen to avoid interfering with AUCTeX's settings.
285 ;; settings. Personally, I also bind some functions in the C-c LETTER 359 ;; Personally, I also bind some functions in the C-c LETTER map for
286 ;; map for easier access: 360 ;; easier access:
287 ;; 361 ;;
288 ;; C-c t reftex-toc 362 ;; C-c t reftex-toc
289 ;; C-c l reftex-label 363 ;; C-c l reftex-label
290 ;; C-c r reftex-reference 364 ;; C-c r reftex-reference
291 ;; C-c c reftex-citation 365 ;; C-c c reftex-citation
292 ;; C-c v reftex-view-crossref 366 ;; C-c v reftex-view-crossref
293 ;; C-c s reftex-search-document 367 ;; C-c s reftex-search-document
294 ;; C-c g reftex-grep-document 368 ;; C-c g reftex-grep-document
295 ;; 369 ;;
296 ;; If you want to copy those as well, set in your .emacs file: 370 ;; If you want to copy those as well, set in your .emacs file:
297 ;; 371 ;;
298 ;; (setq reftex-extra-bindings t) 372 ;; (setq reftex-extra-bindings t)
299 ;; 373 ;;
300 ;; It is possible to bind the function for viewing cross references to a 374 ;; It is possible to bind the function for viewing cross references to a
301 ;; mouse event. Something like the following in .emacs will do the trick: 375 ;; mouse event. Something like the following will do the trick:
302 ;; 376 ;;
303 ;; (add-hook 'reftex-load-hook 377 ;; (add-hook 'reftex-load-hook
304 ;; '(lambda () 378 ;; '(lambda ()
305 ;; (define-key reftex-mode-map [(alt mouse-1)] 379 ;; (define-key reftex-mode-map [(shift mouse-2)]
306 ;; 'reftex-mouse-view-crossref))) 380 ;; 'reftex-mouse-view-crossref)))
307 ;;
308 ;;------------------------------------------------------------------------- 381 ;;-------------------------------------------------------------------------
309 ;; 382 ;;
383 ;; REFTEX AND MULTIFILE DOCUMENTS
384 ;; ==============================
385 ;;
386 ;; The following is relevant when using RefTeX for multi-file documents:
387 ;;
388 ;; o RefTeX has full support for multifile documents. You can edit parts
389 ;; of several (multifile) documents at the same time without conflicts.
390 ;; RefTeX provides functions to run `grep', `search' and `query-replace'
391 ;; on all files which are part of a multifile document.
392 ;;
393 ;; o All files belonging to a multifile document should have a File
394 ;; Variable (`TeX-master' for AUCTeX or `tex-main-file' for the standard
395 ;; Emacs LaTeX mode) set to the name of the master file. See the
396 ;; documentation of your (La)TeX mode and the Emacs documentation on
397 ;; file variables: [Emacs/Customization/Variables/File Variables].
398 ;;
399 ;; o The context of a label definition must be found in the same file as
400 ;; the label itself in order to be processed correctly by RefTeX. The
401 ;; only exception is that section labels referring to a section statement
402 ;; outside the current file can still use that section title as context.
403 ;;-------------------------------------------------------------------------
404 ;;
405 ;; REFERENCES TO OTHER DOCUMENTS
406 ;; =============================
407 ;;
408 ;; RefTeX supports the LaTeX package `xr', which makes it possible to
409 ;; reference labels defined in another document. See the documentation on
410 ;; `xr' for details.
411 ;; When the document is set up to work with `xr', you can use the `x' key
412 ;; in the reference label menu to switch to the label menu of an external
413 ;; document and select any labels from there. In the *toc* buffer, the
414 ;; `x' key can be used to switch to the table of contents of an external
415 ;; document.
416 ;;
417 ;; For this kind of inter-document cross references, saving of parsing
418 ;; information can mean a large speed-up.
419 ;;
420 ;; (setq reftex-save-parse-info t)
421 ;;
422 ;;-------------------------------------------------------------------------
423 ;;
424 ;; OPTIMIZATIONS FOR LARGE DOCUMENTS
425 ;; =================================
426 ;;
427 ;; The default settings of RefTeX ensure a safe ride for beginners and
428 ;; casual users. However, when using RefTeX for a large project and/or on
429 ;; a small computer, there are ways to improve speed or memory usage.
430 ;;
431 ;; o RefTeX will load other parts of a multifile document as well as BibTeX
432 ;; database files for lookup purposes. These buffers are kept, so that
433 ;; subsequent use of the same files is fast. If you can't afford keeping
434 ;; these buffers around, and if you can live with a speed penalty, try
435 ;;
436 ;; (setq reftex-keep-temporary-buffers nil)
437 ;;
438 ;; o The `C-u' prefix on the major RefTeX commands `reftex-label',
439 ;; `reftex-reference', `reftex-citation' and `reftex-toc' initiates
440 ;; re-parsing of the entire document in order to update the parsing
441 ;; information. For a large document this can be unnecessary, in
442 ;; particular if only one file has changed. RefTeX can be configured to
443 ;; do partial scans instead of full ones. `C-u' re-parsing then does
444 ;; apply only to the current buffer and files included from it.
445 ;; Likewise, the `r' key in both the label menu and the table-of-contents
446 ;; buffer will only prompt scanning of the file in which the label or
447 ;; section macro near the cursor was defined. Re-parsing of the entire
448 ;; document is still available by using `C-u C-u' as a prefix, or the
449 ;; capital `R' key in the menus. To use this feature, try
450 ;;
451 ;; (setq reftex-enable-partial-scans t)
452 ;;
453 ;; o Even with partial scans enabled, RefTeX still has to make one full
454 ;; scan, when you start working with a document. To avoid this, parsing
455 ;; information can stored in a file. The file `MASTER.rel' is used for
456 ;; storing information about a document with master file `MASTER.tex'.
457 ;; It is written each time RefTeX parses (part of) the document, and
458 ;; restored when you begin working with a document in a new editing
459 ;; session. To use this feature, put into .emacs:
460 ;;
461 ;; (setq reftex-save-parse-info t)
462 ;;----------------------------------------------------------------------------
463 ;;
310 ;; RELATED PACKAGES 464 ;; RELATED PACKAGES
465 ;; ================
311 ;; 466 ;;
312 ;; AUCTeX 467 ;; AUCTeX
313 ;; ------ 468 ;; ------
314 ;; If you are writing any TeX or LaTeX documents with Emacs, you should 469 ;; If you are writing TeX or LaTeX documents with Emacs, you should have
315 ;; have a look at AUCTeX, the definitive package to work with TeX and LaTeX. 470 ;; a look at AUCTeX, the definitive package to work with TeX and LaTeX.
316 ;; Information on AUCTeX can be found here: 471 ;; Information on AUCTeX can be found here:
317 ;; 472 ;;
318 ;; http://www.sunsite.auc.dk/auctex/ 473 ;; http://www.sunsite.auc.dk/auctex/
319 ;; 474 ;;
320 ;; Instead of using the RefTeX functions described above directly, you 475 ;; Instead of using the RefTeX functions described above directly, you can
321 ;; can also use them indirectly through AUCTeX (>9.7p). RefTeX provides 476 ;; also use them indirectly, through AUCTeX (version 9.8a or later).
322 ;; several interface functions which can be used as replacement for 477 ;; RefTeX provides several interface functions which can be used as
323 ;; corresponding AUCTeX functions dealing with labels and citations. 478 ;; replacement for corresponding AUCTeX functions dealing with labels and
324 ;; In this way you can work normally with AUCTeX and use RefTeX 479 ;; citations. In this way you can work normally with AUCTeX and use RefTeX
325 ;; internals to create and complete labels and citation keys. 480 ;; internals to create and complete labels and citation keys.
326 ;; 481 ;;
327 ;; `reftex-label' can be used as the `LaTeX-label-function' which does 482 ;; `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. 483 ;; label insertion when new environments are created with `C-c C-e'.
329 ;; 484 ;;
330 ;; `reftex-arg-label', `reftex-arg-ref' and `reftex-arg-cite' can replace 485 ;; `reftex-arg-label', `reftex-arg-ref' and `reftex-arg-cite' can replace
331 ;; the corresponding `TeX-arg-...' functions. E.g. when you insert a 486 ;; the corresponding `TeX-arg-...' functions. E.g. when you insert a label
332 ;; label macro with `C-c RET label RET', RefTeX will be transparently used 487 ;; macro with `C-c RET label RET', RefTeX will be transparently used to
333 ;; to create the label. 488 ;; create the label.
334 ;; 489 ;;
335 ;; In order to plug all 4 functions into AUCTeX, use in .emacs: 490 ;; In order to plug all 4 functions into AUCTeX, use:
336 ;; 491 ;;
337 ;; (setq reftex-plug-into-AUCTeX t) 492 ;; (setq reftex-plug-into-AUCTeX t)
338 ;; 493 ;;
339 ;; You may also choose to plug in only some of these functions. The 494 ;; You may also choose to plug in only some of these functions. See the
340 ;; following setting will leave TeX-arg-cite as it was while replacing 495 ;; docstring of `reftex-plug-into-AUCTeX'.
341 ;; the other 3 AUCTeX functions:
342 ;;
343 ;; (setq reftex-plug-into-AUCTeX '(t t t nil))
344 ;; 496 ;;
345 ;; AUCTeX can support RefTeX via style files. A style file may contain 497 ;; AUCTeX can support RefTeX via style files. A style file may contain
346 ;; calls to `reftex-add-to-label-alist' which defines additions to 498 ;; calls to `reftex-add-to-label-alist' which defines additions to
347 ;; `reftex-label-alist'. The argument taken by this function must have 499 ;; `reftex-label-alist'. The argument taken by this function must have the
348 ;; the same format as `reftex-label-alist'. The `amsmath.el' style file 500 ;; same format as `reftex-label-alist'. The `amsmath.el' style file of
349 ;; of AUCTeX (>9.7p) for example contains the following: 501 ;; AUCTeX (>9.7p) for example contains the following:
350 ;; 502 ;;
351 ;; (TeX-add-style-hook "amsmath" 503 ;; (TeX-add-style-hook "amsmath"
352 ;; (function 504 ;; (function
353 ;; (lambda () 505 ;; (lambda ()
354 ;; (if (featurep 'reftex) 506 ;; (if (featurep 'reftex)
360 ;; (TeX-add-style-hook "myprop" 512 ;; (TeX-add-style-hook "myprop"
361 ;; (function 513 ;; (function
362 ;; (lambda () 514 ;; (lambda ()
363 ;; (if (featurep 'reftex) 515 ;; (if (featurep 'reftex)
364 ;; (reftex-add-to-label-alist 516 ;; (reftex-add-to-label-alist
365 ;; '(("proposition" ?p "prop:" "~\\ref{%s}" t 517 ;; '(("proposition" ?p "prop:" "~\\ref{%s}" t
366 ;; ("Proposition" "Prop.")))))))) 518 ;; ("Proposition" "Prop."))))))))
367 ;; 519 ;;
368 ;; Bib-cite.el 520 ;; Bib-cite.el
369 ;; ----------- 521 ;; -----------
370 ;; Once you have written a document with labels, refs and citations, 522 ;; Once you have written a document with labels, refs and citations, it can
371 ;; it can be nice to read such a file like a hypertext document. 523 ;; be nice to read such a file like a hypertext document. RefTeX has some
372 ;; RefTeX has some support for that (`reftex-view-crossref', 524 ;; support for that (`reftex-view-crossref', `reftex-search-document'). A
373 ;; `reftex-search-document'). A more elegant interface with mouse 525 ;; more elegant interface with mouse support and links into Hyperbole is
374 ;; support and links into Hyperbole is provided (among other things) 526 ;; provided (among other things) by Peter S. Galbraith's `bib-cite.el'.
375 ;; by Peter S. Galbraith's `bib-cite.el'. There is some overlap in the 527 ;; There is some overlap in the functionalities of Bib-cite and RefTeX.
376 ;; functionalities of Bib-cite and RefTeX. Bib-cite.el comes bundled 528 ;; Bib-cite.el comes bundled with AUCTeX. You can also get the latest
377 ;; with AUCTeX. You can also get the latest version from 529 ;; version from
378 ;; 530 ;;
379 ;; ftp://ftp.phys.ocean.dal.ca/users/rhogee/elisp/bib-cite.el 531 ;; ftp://ftp.phys.ocean.dal.ca/users/rhogee/elisp/bib-cite.el
380 ;; 532 ;;---------------------------------------------------------------------------
381 ;;------------------------------------------------------------------------- 533 ;;
382 ;; 534 ;; KNOWN BUGS AND WORK-AROUNDS
383 ;; PERFORMANCE ISSUES 535 ;; ===========================
384 ;; 536 ;;
385 ;; 1. RefTeX will load other parts of a multifile document as well as BibTeX 537 ;; o \input, \include, \bibliography and \section (etc.) statements have
386 ;; database files for lookup purposes. These buffers are kept, so that 538 ;; to be first on a line (except for white space).
387 ;; subsequent lookup in the same files is fast. For large documents and 539 ;;
388 ;; large BibTeX databases, this can use up a lot of memory. If you have 540 ;; o RefTeX sees also labels in regions commented out and will refuse to
389 ;; more time than memory, try the following option, which will remove 541 ;; make duplicates of such a label. This is considered to be a feature.
390 ;; buffers created for lookup after use. 542 ;;
391 ;; 543 ;; o When using partial scans (`reftex-enable-partial-scans'), the section
392 ;; (setq reftex-keep-temporary-buffers nil) 544 ;; numbers in the table of contents may eventually become wrong. A full
393 ;; 545 ;; scan will fix this.
394 ;; 2. Parsing the document for labels and their context can be slow. 546 ;;
395 ;; Therefore, RefTeX does it just once automatically. Further parsing 547 ;; o RefTeX keeps only a global copy of the configuration variables.
396 ;; happens only on user request 548 ;; Also, any additions from style files go into a global variable.
397 ;; - with a raw C-u prefix arg to any of the functions `reftex-label',
398 ;; `reftex-reference', `reftex-citation', `reftex-toc'.
399 ;; - with the `r' key from the label selection menu or the *toc* buffer.
400 ;;
401 ;; *** If you use `reftex-label' to create labels, the list will be
402 ;; *** updated internally, so that no extra parsing is required.
403 ;;
404 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
405 ;;
406 ;; KNOWN BUGS
407 ;;
408 ;; o If you change `reftex-label-alist' in an editing session, you need to
409 ;; reset reftex with `M-x reftex-reset-mode' in order to make these
410 ;; changes effective. Changes introduced with the function
411 ;; `reftex-add-to-label-alist' as well as changes applied from the
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
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
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 549 ;; Practically, this should not be a problem. Theoretically, it could
436 ;; give conflicts if two documents used environments with identical 550 ;; give conflicts if two documents used environments with identical
437 ;; names, but different associated label types. 551 ;; names, but different associated label types.
438 ;; 552 ;;
439 ;; o Input, include, bibliography and section statements have to be first 553 ;; o When using packages which make the buffer representation of a file
440 ;; on a line (except for white space) in order to be seen by reftex. 554 ;; different from its disk representation (e.g. x-symbol, isotex,
441 ;; 555 ;; iso-cvt) you may find that RefTeX's parsing information sometimes
442 ;; o When the document is scanned, RefTeX creates a large buffer containing 556 ;; reflects the disk state of a file. This happens only in *unvisited*
443 ;; the entire document instead of scanning the individual files one by 557 ;; parts of a multifile document, because RefTeX visits these files
444 ;; one. This is necessary since a file might not contain the context 558 ;; literally for speed reasons. Then both short context and section
445 ;; needed by RefTeX. 559 ;; headings may look different from what you usually see on your screen.
446 ;; 560 ;; In rare cases `reftex-toc' may have problems to jump to an affected
447 ;; o If you have two identical section headings in the same file, 561 ;; section heading. There are three possible ways to deal with this:
448 ;; `reftex-toc' will only let you jump to the first one because it searches 562 ;;
449 ;; for the section heading from the beginning of the file. You can work 563 ;; - (setq reftex-keep-temporary-buffers t)
450 ;; around this by changing one of the section titles in a way LaTeX does 564 ;; This implies that RefTeX will load all parts of a multifile
451 ;; not see, e.g. with extra white space. RefTeX will distinguish 565 ;; document into Emacs (i.e. there will be no temporary buffers).
452 ;; \section{Introduction} from \section{ Introduction}. 566 ;; - (setq reftex-initialize-temporary-buffers t)
453 ;; 567 ;; This means full initialization of temporary buffers. It involves
454 ;; o RefTeX sees also labels in regions commented out and will refuse to 568 ;; a penalty when the same file is used for lookup often.
455 ;; make duplicates of such a label. This is considered to be a feature. 569 ;; - Set `reftex-initialize-temporary-buffers' to a list of hook
456 ;; 570 ;; functions doing a minimal initialization.
457 ;; o When RefTeX tries to show a window full of context from inside a 571 ;;
458 ;; section hidden with `outline-minor-mode', it will unhide that section. 572 ;; You might also want to check the variable `reftex-refontify-context'.
459 ;; This change will not be reversed automatically. 573 ;;
460 ;; 574 ;; o Some nasty :-# packages use an additional argument to a \begin macro
575 ;; to specify a label. E.g. Lamport's "pf.sty" uses both
576 ;;
577 ;; \step{LABEL}{CLAIM} and \begin{step+}{LABEL}
578 ;; CLAIM
579 ;; \end{step+}
580 ;;
581 ;; We need to trick RefTeX into swallowing this:
582 ;;
583 ;; ;; Configuration for Lamport's pf.sty
584 ;; (setq reftex-label-alist
585 ;; '(("\\step{*}{}" ?p "st:" "~\\stepref{%s}" 2 ("Step" "St."))
586 ;; ("\\begin{step+}{*}" ?p "st:" "~\\stepref{%s}" 1000)))
587 ;;
588 ;; The first line is just a normal configuration for a macro. For the
589 ;; `step+' environment we actually tell RefTeX to look for the *macro*
590 ;; "\begin{step+}" and interprete the *first* argument (which in reality
591 ;; is a second argument to the macro \begin) as a label of type ?p.
592 ;; Argument count for this macro starts only after the {step+}, also
593 ;; when specifying how to get context.
594 ;;
595 ;; o In XEmacs 19.15, the overlay library has a bug. RefTeX does not
596 ;; suffer from it, but since it loads the library, other packages like
597 ;; GNUS will switch from extents to overlays and hit the bug. Upgrade
598 ;; to XEmacs 20, or fix the overlay library (in line 180 of overlay.el,
599 ;; change `(list before after)' to `(cons before after)').
461 ;;--------------------------------------------------------------------------- 600 ;;---------------------------------------------------------------------------
462 ;; 601 ;;
463 ;; TO DO
464 ;;
465 ;; I think I am pretty much done with this one...
466 ;;
467 ;;---------------------------------------------------------------------------
468 ;;
469 ;; AUTHOR 602 ;; AUTHOR
603 ;; ======
470 ;; 604 ;;
471 ;; Carsten Dominik <dominik@strw.LeidenUniv.nl> 605 ;; Carsten Dominik <dominik@strw.LeidenUniv.nl>
472 ;; 606 ;;
473 ;; with contributions from Stephen Eglen 607 ;; with contributions from Stephen Eglen
474 ;; 608 ;;
478 ;; ftp://strw.leidenuniv.nl/pub/dominik/ 612 ;; ftp://strw.leidenuniv.nl/pub/dominik/
479 ;; 613 ;;
480 ;; THANKS TO: 614 ;; THANKS TO:
481 ;; --------- 615 ;; ---------
482 ;; At least the following people have invested time to test and bug-fix 616 ;; 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. 617 ;; reftex.el. Some have send patches for fixes or new features, or came
618 ;; up with useful ideas.
484 ;; 619 ;;
485 ;; Stephen Eglen <stephene@cogs.susx.ac.uk> 620 ;; Stephen Eglen <stephene@cogs.susx.ac.uk>
486 ;; F.E.Burstall <F.E.Burstall@maths.bath.ac.uk> 621 ;; F.E. Burstall <F.E.Burstall@maths.bath.ac.uk>
487 ;; Karl Eichwalder <ke@ke.Central.DE> 622 ;; Karl Eichwalder <ke@ke.Central.DE>
488 ;; Laurent Mugnier <mugnier@onera.fr> 623 ;; Laurent Mugnier <mugnier@onera.fr>
489 ;; Rory Molinari <molinari@yunt.math.lsa.umich.edu> 624 ;; Rory Molinari <molinari@yunt.math.lsa.umich.edu>
490 ;; Soren Dayton <csdayton@cs.uchicago.edu> 625 ;; Soren Dayton <csdayton@cs.uchicago.edu>
491 ;; Daniel Polani <polani@Informatik.Uni-Mainz.DE> 626 ;; Daniel Polani <polani@Informatik.Uni-Mainz.DE>
492 ;; Allan Strand <astrand@trillium.NMSU.Edu> 627 ;; Allan Strand <astrand@trillium.NMSU.Edu>
493 ;; 628 ;; Adrian Lanz <lanz@waho.ethz.ch>
494 ;; The view crossref feature was inspired by the similar function in 629 ;; Jan Vroonhof <vroonhof@math.ethz.ch>
630 ;; Alastair Burt <alastair.burt@dfki.de>
631 ;; Dieter Kraft <dkraft@acm.org>
632 ;; Robin S. Socha <r.socha@franck.pc.uni-koeln.de>
633 ;;
634 ;; The view crossref feature was inspired by the similar function in
495 ;; Peter S. Galbraith's bib-cite.el. 635 ;; Peter S. Galbraith's bib-cite.el.
496 ;; 636 ;;
497 ;; Finally thanks to Uwe Bolick <bolick@physik.tu-berlin.de> who first 637 ;; Finally thanks to Uwe Bolick <bolick@physik.tu-berlin.de> who first
498 ;; got me (some years ago) into supporting LaTeX labels and references 638 ;; got me (some years ago) into supporting LaTeX labels and references
499 ;; with an Editor (which was MicroEmacs at the time). 639 ;; with an Editor (which was MicroEmacs at the time).
500 ;; 640 ;;
501 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 641 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
502 ;; 642 ;;
643 ;; HISTORY
644 ;; =======
645 ;;
646 ;; Here are the more important changes made to RefTeX since initial release.
647 ;; Minor bug fixes are not mentioned.
648 ;;
649 ;; Version 1.00
650 ;; - released on 7 Jan 1997.
651 ;; Version 1.04
652 ;; - Macros as wrappers, AMSTeX support, delayed context parsing for
653 ;; new labels.
654 ;; Version 1.05
655 ;; - XEmacs port.
656 ;; Version 1.07
657 ;; - RefTeX gets its own menu.
658 ;; Version 1.09
659 ;; - Support for tex-main-file, an analogue for TeX-master.
660 ;; - MS-DOS support.
661 ;; Version 2.00
662 ;; - Labels can be derived from context (default for sections).
663 ;; - Configuration of label insertion and label referencing revised.
664 ;; - Crossref fields in BibTeX database entries.
665 ;; - `reftex-toc' introduced (thanks to Stephen Eglen).
666 ;; Version 2.03
667 ;; - Figure*, table*, Sidewaysfigure/table added to default environments.
668 ;; - `reftex-bibfile-ignore-list' introduced (thanks to Rory Molinari).
669 ;; - New functions `reftex-arg-label', `reftex-arg-ref', `reftex-arg-cite'.
670 ;; - Emacs/XEmacs compatibility reworked. XEmacs 19.15 now is required.
671 ;; - `reftex-add-to-label-alist' (to be called from AUCTeX style files).
672 ;; - Finding context with a hook function.
673 ;; - Sorting BibTeX entries (new variable: `reftex-sort-bibtex-matches').
674 ;; Version 2.05
675 ;; - Support for `custom.el'.
676 ;; - New function `reftex-grep-document' (thanks to Stephen Eglen).
677 ;; Version 2.07
678 ;; - New functions `reftex-search-document', `reftex-query-replace-document'
679 ;; Version 2.11
680 ;; - Submitted for inclusion to Emacs and XEmacs.
681 ;; Version 2.14
682 ;; - Variable `reftex-plug-into-AUCTeX' simplifies cooperation with AUCTeX.
683 ;; Version 2.17
684 ;; - Label prefix expands % escapes with current file name and other stuff.
685 ;; - Citation format now with % escapes. This is not backward compatible!
686 ;; - TEXINPUTS variable recognized when looking for input files.
687 ;; - Context can be the nth argument of a macro.
688 ;; - Searching in the select buffer is now possible (C-s and C-r).
689 ;; - Display and derive-label can use two different context methods.
690 ;; - AMSmath xalignat and xxalignat added.
691 ;; - THIS IS THE VERSION DISTRIBUTED WITH EMACS 20.1 and 20.2
692 ;; Version 3.00
693 ;; - RefTeX should work better for very large projects:
694 ;; - The new parser works without creating a master buffer.
695 ;; - Rescanning can be limited to a part of a multifile document.
696 ;; - Information from the parser can be stored in a file.
697 ;; - RefTeX can deal with macros having a naked label as an argument.
698 ;; - Macros may have white space and newlines between arguments.
699 ;; - Multiple identical section headings no longer confuse `reftex-toc'.
700 ;; - RefTeX should work correctly in combination with buffer-altering
701 ;; packages like outline, folding, x-symbol, iso-cvt, isotex, etc.
702 ;; - All labeled environments discussed in `The LaTeX Companion' by
703 ;; Goossens, Mittelbach & Samarin, Addison-Wesley 1994) are part of
704 ;; RefTeX's defaults.
705 ;; Version 3.03
706 ;; - Support for the LaTeX package `xr', for inter-document references.
707 ;; - A few (minor) Mule-related changes.
708 ;; - Fixed bug which could cause HUGE .rel files.
709 ;; - Search for input and .bib files with recursive path definitions.
710 ;; Version 3.04
711 ;; - Fixed BUG in the `xr' support.
712 ;; Version 3.05
713 ;; - Compatibility code now first checks for XEmacs feature.
714 ;; Version 3.07
715 ;; - `Ref' menu improved.
716 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
717 ;;
718 ;;;;;;
503 719
504 ;;; Code: 720 ;;; Code:
721
722 (eval-when-compile (require 'cl))
505 723
506 ;; Stuff that needs to be there when we use defcustom 724 ;; Stuff that needs to be there when we use defcustom
507 ;; -------------------------------------------------- 725 ;; --------------------------------------------------
508 726
509 (require 'custom) 727 (require 'custom)
514 (eval-and-compile 732 (eval-and-compile
515 (defun reftex-set-dirty (symbol value) 733 (defun reftex-set-dirty (symbol value)
516 (setq reftex-tables-dirty t) 734 (setq reftex-tables-dirty t)
517 (set symbol value))) 735 (set symbol value)))
518 736
737 (eval-and-compile
738 (defmacro reftex-fp (n)
739 (if (fboundp 'forward-point)
740 (list 'forward-point n)
741 (list '+ '(point) n))))
742
519 ;;; Begin of Configuration Section ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 743 ;;; Begin of Configuration Section ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
744
745 ;; Define the two constants which are needed during compilation
746
747 (eval-and-compile
748 (defconst reftex-label-alist-builtin
749 '(
750 ;; Some aliases, mostly for backward compatibility
751 (Sideways "Alias for -->rotating" (rotating))
752 (AMSTeX "amsmath with eqref macro"
753 ((nil ?e nil "~\\eqref{%s}")
754 amsmath))
755
756 ;; Individual package defaults
757 (amsmath "AMS-LaTeX math environments"
758 (("align" ?e nil nil eqnarray-like)
759 ("gather" ?e nil nil eqnarray-like)
760 ("multline" ?e nil nil t)
761 ("flalign" ?e nil nil eqnarray-like)
762 ("alignat" ?e nil nil alignat-like)
763 ("xalignat" ?e nil nil alignat-like)
764 ("xxalignat" ?e nil nil alignat-like)
765 ("subequations" ?e nil nil t)))
766
767 (endnotes "The \\endnote macro"
768 (("\\endnote[]{}" ?n nil nil 2 ("Endnote"))))
769
770 (fancybox "The Beqnarray environment"
771 (("Beqnarray" ?e nil nil eqnarray-like)))
772
773 (floatfig "The floatingfigure environment"
774 (("floatingfigure" ?f nil nil caption)))
775
776 (longtable "The longtable environment"
777 (("longtable" ?t nil nil caption)))
778
779 (picinpar "The figwindow and tabwindow environments"
780 (("figwindow" ?f nil nil 1)
781 ("tabwindow" ?f nil nil 1)))
782
783 (rotating "Sidewaysfigure and table"
784 (("sidewaysfigure" ?f nil nil caption)
785 ("sidewaystable" ?t nil nil caption)))
786
787 (subfigure "Subfigure environments/macro"
788 (("subfigure" ?f nil nil caption)
789 ("subfigure*" ?f nil nil caption)
790 ("\\subfigure[]{}" ?f nil nil 1)))
791
792 (supertab "Supertabular environment"
793 (("supertabular" ?t nil nil "\\tablecaption{")))
794
795 (wrapfig "The wrapfigure environment"
796 (("wrapfigure" ?f nil nil caption)))
797
798 ;; The LaTeX core stuff
799 (LaTeX "LaTeX default environments"
800 (("section" ?s "sec:" "~\\ref{%s}" (nil . t)
801 ("Part" "Chapter" "Chap." "Section" "Sec." "Sect." "Paragraph" "Par."
802 "\\S" "Teil" "Kapitel" "Kap." "Abschnitt" ))
803
804 ("enumerate" ?i "item:" "~\\ref{%s}" item
805 ("Item" "Punkt"))
806
807 ("equation" ?e "eq:" "~(\\ref{%s})" t
808 ("Equation" "Eq." "Eqn." "Gleichung" "Gl."))
809 ("eqnarray" ?e "eq:" nil eqnarray-like)
810
811 ("figure" ?f "fig:" "~\\ref{%s}" caption
812 ("Figure" "Fig." "Abbildung" "Abb."))
813 ("figure*" ?f nil nil caption)
814
815 ("table" ?t "tab:" "~\\ref{%s}" caption
816 ("Table" "Tab." "Tabelle"))
817 ("table*" ?t nil nil caption)
818
819 ("\\footnote[]{}" ?n "note:" "~\\ref{%s}" 2
820 ("Footnote" "Note"))
821
822 ("any" ?\ " " "\\ref{%s}" nil)))
823
824 )
825 "The default label environment descriptions.
826 Lower-case symbols correspond to a style file of the same name in the LaTeX
827 distribution. Mixed-case symbols are convenience aliases.")
828
829 (defconst reftex-cite-format-builtin
830 '(
831 (default "Default macro \\cite{%l}"
832 "\\cite{%l}")
833 (natbib "The Natbib package"
834 ((?\C-m . "\\cite{%l}")
835 (?t . "\\citet{%l}")
836 (?T . "\\citet*{%l}")
837 (?p . "\\citep{%l}")
838 (?P . "\\citep*{%l}")
839 (?e . "\\citep[e.g.][]{%l}")
840 (?a . "\\citeauthor{%l}")
841 (?y . "\\citeyear{%l}")))
842 (harvard "The Harvard package"
843 ((?\C-m . "\\cite{%l}")
844 (?p . "\\cite{%l}")
845 (?t . "\\citeasnoun{%l}")
846 (?n . "\\citeasnoun{%l}")
847 (?s . "\\possessivecite{%l}")
848 (?e . "\\citeaffixed{%l}{?}")
849 (?y . "\\citeyear{%l}")
850 (?a . "\\citename{%l}")))
851 (chicago "The Chicago package"
852 ((?\C-m . "\\cite{%l}")
853 (?t . "\\citeN{%l}")
854 (?T . "\\shortciteN{%l}")
855 (?p . "\\cite{%l}")
856 (?P . "\\shortcite{%l}")
857 (?a . "\\citeA{%l}")
858 (?A . "\\shortciteA{%l}")
859 (?y . "\\citeyear{key}")))
860 (astron "The Astron package"
861 ((?\C-m . "\\cite{%l}")
862 (?p . "\\cite{%l}" )
863 (?t . "%2a (\\cite{%l})")))
864 (author-year "Do-it-yourself Author-year"
865 ((?\C-m . "\\cite{%l}")
866 (?t . "%2a (%y)\\nocite{%l}")
867 (?p . "(%2a %y\\nocite{%l})")))
868 (locally "Full info in parenthesis"
869 "(%2a %y, %j %v, %P, %e: %b, %u, %s %<)")
870 ;; undocumented feature: `%<' kills white space and punctuation locally.
871 )
872 "Builtin versions of for the citation format.
873 The following conventions are valid for all alist entries:
874 `?\C-m' should always point to a straight \\cite{%l} macro.
875 `?t' should point to a textual citation (citation as a noun).
876 `?p' should point to a parenthetical citation.")
877 )
520 878
521 ;; Configuration Variables and User Options for RefTeX ------------------ 879 ;; Configuration Variables and User Options for RefTeX ------------------
522 880
523 (defgroup reftex nil 881 (defgroup reftex nil
524 "LaTeX label and citation support." 882 "LaTeX label and citation support."
525 :tag "RefTeX" 883 :tag "RefTeX"
526 :link '(url-link :tag "Home Page" "http://strw.leidenuniv.nl/~dominik/Tools/") 884 :link '(url-link :tag "Home Page"
885 "http://strw.leidenuniv.nl/~dominik/Tools/")
886 :link '(emacs-commentary-link :tag "Commentary in reftex.el" "reftex.el")
527 :prefix "reftex-" 887 :prefix "reftex-"
528 :group 'tex) 888 :group 'tex)
529 889
530 (defun reftex-customize () 890 (defun reftex-customize ()
531 "Call the customize function with reftex as argument." 891 "Call the customize function with reftex as argument."
532 (interactive) 892 (interactive)
533 (if (fboundp 'customize-group) 893 ;; Depending on the customize version we can call different functions.
534 (customize-group 'reftex) 894 (cond
535 (customize 'reftex))) 895 ((fboundp 'customize-browse)
896 (customize-browse 'reftex))
897 ((fboundp 'customize-group)
898 (customize-group 'reftex))
899 ((fboundp 'customize)
900 (customize 'reftex))
901 (t (error "Custom.el not available"))))
902
903 (defun reftex-show-commentary ()
904 "Use the finder to view the file documentation from `reftex.el'."
905 (interactive)
906 (require 'finder)
907 (finder-commentary "reftex.el"))
536 908
537 ;; Support for \label and \ref -------------------------------------- 909 ;; Support for \label and \ref --------------------------------------
538 910
539 (defgroup reftex-label-support nil 911 (defgroup reftex-label-support nil
540 "Support for creation, insertion and referencing of labels in LaTeX." 912 "Support for creation, insertion and referencing of labels in LaTeX."
541 :prefix "reftex-"
542 :group 'reftex) 913 :group 'reftex)
543 914
544 (defgroup reftex-defining-label-environments nil 915 (defgroup reftex-defining-label-environments nil
545 "Definition of environments and macros to do with label." 916 "Definition of environments and macros to do with label."
546 :prefix "reftex-"
547 :group 'reftex-label-support) 917 :group 'reftex-label-support)
548 918
919 ;; Make a constant for the customization stuff
920 (eval-and-compile
921 (defconst reftex-tmp
922 '((const :tag "Default position" t)
923 (const :tag "After label" nil)
924 (number :tag "Macro arg nr" 1)
925 (regexp :tag "Regexp" "")
926 (const :tag "Caption in float" caption)
927 (const :tag "Item in list" item)
928 (const :tag "Eqnarray-like" eqnarray-like)
929 (const :tag "Alignat-like" alignat-like)
930 (symbol :tag "Function" my-func))))
931
932 (defcustom reftex-default-label-alist-entries
933 '(amsmath endnotes fancybox floatfig longtable picinpar
934 rotating subfigure supertab wrapfig LaTeX)
935 "Default label alist specifications. LaTeX should be the last entry.
936 This list describes the default label environments RefTeX should always use.
937 It is probably a mistake to remove the LaTeX symbol from this list.
938
939 The options include:
940 LaTeX The standard LaTeX environments.
941 Sideways The sidewaysfigure and sidewaystable environments.
942 AMSTeX The math environments in the AMS-LaTeX amsmath package.
943
944 For the full list of options, try
945
946 M-x customize-variable RET reftex-default-label-alist-entries RET."
947 :group 'reftex-defining-label-environments
948 :set 'reftex-set-dirty
949 :type `(set
950 :indent 4
951 :inline t
952 :greedy t
953 ,@(mapcar
954 (function
955 (lambda (x)
956 (list 'const ':tag (concat (symbol-name (nth 0 x))
957 ": " (nth 1 x))
958 (nth 0 x))))
959 reftex-label-alist-builtin)))
549 960
550 (defcustom reftex-label-alist nil 961 (defcustom reftex-label-alist nil
551 "Alist with information on environments for \\label-\\ref use. 962 "Alist with information on environments for \\label-\\ref use.
552 See the definition of `reftex-label-alist-builtin' for examples. This variable 963
553 should define additions and changes to the default. The only things you MUST 964 This docstring is easier to understand after reading the configuration
554 NOT change is that `?s' is the type indicator for section labels and SPACE is 965 examples in `reftex.el'. Looking at the builtin defaults in the constant
555 for the `any' label type. These are hard-coded at other places in the code. 966 `reftex-label-alist-builtin' may also be instructive.
556 967
557 Changes to this variable after RefTeX has been loaded become only 968 Set this variable to define additions and changes to the default. The only
558 effective when RefTeX is reset with \\[reftex-reset-mode]. 969 things you MUST NOT change is that `?s' is the type indicator for section
559 970 labels, and SPC for the `any' label type. These are hard-coded at other
560 Each list entry is a list describing an environment or macro carrying a 971 places in the code.
561 label. The elements of each list entry are: 972
973 Each list entry describes either an environment carrying a counter for use
974 with \\label and \\ref, or a LaTeX macro defining a label as (or inside)
975 one of its arguments. The elements of each list entry are:
562 976
563 0. Name of the environment (like \"table\") or macro (like \"\\\\myfig\"). 977 0. Name of the environment (like \"table\") or macro (like \"\\\\myfig\").
978 For macros, indicate the macro arguments for best results, as in
979 \"\\\\myfig[]{}{}{*}{}\". Use square brackets for optional arguments,
980 a star to mark the label argument, if any. The macro does not have to
981 have a label argument - you could also use \\label{..} inside one of
982 its arguments.
564 Special names: `section' for section labels, `any' to define a group 983 Special names: `section' for section labels, `any' to define a group
565 which contains all labels. 984 which contains all labels.
566 This may also be nil if this entry is only meant to change some settings 985 This may also be nil if the entry is only meant to change some settings
567 associated with the type indicator character (see below). 986 associated with the type indicator character (see below).
568 987
569 1. Type indicator character, like `?t'. 988 1. Type indicator character, like `?t', must be a printable ASCII character.
570 The type indicator is a single character used in prompts for 989 The type indicator is a single character which defines a label type.
571 label types. It must be a printable character. The same character 990 Any label inside the environment or macro is assumed to belong to this
572 may occur several times in this list, to cover cases in which different 991 type. The same character may occur several times in this list, to cover
573 environments carry the same label type (like equation and eqnarray). 992 cases in which different environments carry the same label type (like
993 `equation' and `eqnarray').
574 994
575 2. Label prefix string, like \"tab:\". 995 2. Label prefix string, like \"tab:\".
576 The prefix is a short string used as the start of a label. It may be the 996 The prefix is a short string used as the start of a label. It may be the
577 empty string. 997 empty string. The prefix may contain the following `%' escapes:
578 998 %f Current file name with directory and extension stripped.
579 3. Format string for reference insert in buffer. Each `%s' will be replaced 999 %F Current file name relative to directory of master file.
580 by the label (several `%s' can be there to do this: 1000 %u User login name, on systems which support this.
581 \"\\ref{%s} on page~\\pageref{%s}\"). 1001
1002 Example: In a file `intro.tex', \"eq:%f:\" will become \"eq:intro:\").
1003
1004 3. Format string for reference insert in buffer. `%s' will be replaced by
1005 the label.
582 When the format starts with `~', whitespace before point will be removed 1006 When the format starts with `~', whitespace before point will be removed
583 so that the reference cannot be separated from the word before it. 1007 so that the reference cannot be separated from the word before it.
584 1008
585 4. Indication on how to find the short context. 1009 4. Indication on how to find the short context.
586 - If nil, use the text following the \\label{...} macro. 1010 - If nil, use the text following the \\label{...} macro.
587 - If t, use 1011 - If t, use
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. 1012 - the section heading for section labels.
591 - the begin of the macro for macros. 1013 - text following the \\begin{...} statement of environments.
1014 (not a good choice for environments like eqnarray or enumerate,
1015 where one has several labels in a single environment).
1016 - text after the macro name (stearting with the first arg) for macros.
1017 - If an integer, use the nth argument of the macro. As a special case,
1018 1000 means to get text after the last macro argument.
592 - If a string, use as regexp to search *backward* from the label. Context 1019 - 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 1020 is then the text following the end of the match. E.g. putting this to
594 \"\\\\\\\\caption{\" will use the beginning of the caption in a figure 1021 \"\\\\\\\\caption[[{]\" will use the caption in a figure or table
595 or table environment. 1022 environment.
596 \"\\\\\\\\begin{eqnarray}\\\\|\\\\\\\\\\\\\\\\\" works for eqnarrays. 1023 \"\\\\\\\\begin{eqnarray}\\\\|\\\\\\\\\\\\\\\\\" works for eqnarrays.
1024 - If any of `caption', `item', `eqnarray-like', `alignat-like', this
1025 symbol will internally be translated into an appropriate regexp
1026 (see also the variable `reftex-default-context-regexps').
597 - If a function, call this function with the name of the environment/macro 1027 - If a function, call this function with the name of the environment/macro
598 as argument. On call, point will be just after the \\label macro. The 1028 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 1029 function is expected to return a suitable context string. It should
600 throw an exception (error) when failing to find context. 1030 throw an exception (error) when failing to find context.
601 Consider the following example, which would return the 10 characters 1031 As an example, here is a function returning the 10 chars following
602 following the label as context: 1032 the label macro as context:
603 1033
604 (defun my-context-function (env-or-mac) 1034 (defun my-context-function (env-or-mac)
605 (if (> (point-max) (+ 10 (point))) 1035 (if (> (point-max) (+ 10 (point)))
606 (buffer-substring (point) (+ 10 (point))) 1036 (buffer-substring (point) (+ 10 (point)))
607 (error \"Buffer too small\"))) 1037 (error \"Buffer too small\")))
608 1038
1039 Label context is used in two ways by RefTeX: For display in the label
1040 menu, and to derive a label string. If you want to use a different
1041 method for each of these, specify them as a dotted pair.
1042 E.g. `(nil . t)' uses the text after the label (nil) for display, and
1043 text from the default position (t) to derive a label string. This is
1044 actually used for section labels.
1045
609 Setting the variable `reftex-use-text-after-label-as-context' to t 1046 Setting the variable `reftex-use-text-after-label-as-context' to t
610 overrides the setting here. 1047 overrides the setting here.
611 1048
612 5. List of magic words which identify a reference to be of this type. 1049 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 1050 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 1051 `reftex-reference', the label list offered will be automatically
615 to labels of the correct type. 1052 restricted to labels of the correct type.
616 1053
617 If the type indicator characters of two or more entries are the same, RefTeX 1054 If the type indicator characters of two or more entries are the same, RefTeX
618 will use 1055 will use
619 - the first non-nil format and prefix 1056 - the first non-nil format and prefix
620 - the magic words of all involved entries. 1057 - the magic words of all involved entries.
621 1058
622 Any list entry may also be a symbol. If that has an association in 1059 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 1060 `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 1061 list. However, builtin defaults should normally be set here but with the
625 `reftex.el'." 1062 variable `reftex-default-label-alist-entries."
626 :group 'reftex-defining-label-environments 1063 :group 'reftex-defining-label-environments
627 :set 'reftex-set-dirty 1064 :set 'reftex-set-dirty
628 :type '(list 1065 :type
629 :convert-widget 1066 `(repeat
630 (lambda (widget) 1067 (choice
631 (let* 1068 :value ("" ?a nil nil nil nil)
632 ((args 1069 (list :tag "Detailed label alist entry"
633 (list 1070 :value ("" ?a nil nil nil nil)
634 `(repeat 1071 (choice :tag "Environment or \\macro "
635 :inline t 1072 (const :tag "Ignore, just use typekey" nil)
636 (radio 1073 (string ""))
637 :value ("" ?a nil nil t nil) 1074 (character :tag "Typekey character " ?a)
638 (choice 1075 (choice :tag "Label prefix string "
639 :tag "Builtin" 1076 (const :tag "Default" nil)
640 :value AMSTeX 1077 (string :tag "String" "lab:"))
641 ,@(mapcar (function (lambda (x) 1078 (choice :tag "Label reference format"
642 (list 'const ':tag (nth 1 x) (car x)))) 1079 (const :tag "Default" nil)
643 reftex-label-alist-builtin)) 1080 (string :tag "String" "~\\ref{%s}"))
644 (list :tag "Detailed custom entry" 1081 (choice :tag "Context"
645 (choice :tag "Environment or \\macro " 1082 (choice
646 (const :tag "Ignore, just use typekey" nil) 1083 :tag "1 method"
647 (string "")) 1084 ,@reftex-tmp)
648 (character :tag "Typekey character " ?a) 1085 (cons :tag "Split methods"
649 (choice :tag "Label prefix string " 1086 (choice
650 (const :tag "Copy from similar label type" nil) 1087 :tag " Display context "
651 (string :tag "Specify here" "lab:")) 1088 ,@reftex-tmp)
652 (choice :tag "Label reference format" 1089 (choice
653 (const :tag "Copy from similar label type" nil) 1090 :tag " Derive label context"
654 (string :tag "Specify here" "~\\ref{%s}")) 1091 ,@reftex-tmp)))
655 (choice :tag "Grab context method " 1092 (repeat :tag "List of Magic Words" (string)))
656 (const :tag "Default position" t) 1093 (choice
657 (const :tag "After label" nil) 1094 :tag "Package"
658 (regexp :tag "Regular expression" "") 1095 :value AMSTeX
659 (symbol :tag "Function" my-context-function)) 1096 ,@(mapcar
660 (repeat :tag "List of Magic Words" (string)))))))) 1097 (function
661 (widget-put widget :args args) 1098 (lambda (x)
662 widget)))) 1099 (list 'const ':tag (concat (symbol-name (nth 0 x))); ": " (nth 1 x))
663 1100 (nth 0 x))))
664 (defcustom reftex-default-label-alist-entries '(Sideways LaTeX) 1101 reftex-label-alist-builtin)))))
665 "Default label alist specifications. LaTeX should be the last entry. 1102
666 This list describes the default label environments RefTeX should always use 1103 ;; LaTeX section commands and level numbers
667 in addition to the specifications in reftex-label-alist. It is probably a 1104 (defcustom reftex-section-levels
668 mistake to remove the LaTeX symbol from this list. 1105 '(
669 1106 ("part" . 0)
670 The options include: 1107 ("chapter" . 1)
671 LaTeX The standard LaTeX environments 1108 ("section" . 2)
672 Sideways The sidewaysfigure and sidewaystable environments 1109 ("subsection" . 3)
673 AMSTeX The math environments in the AMS_LaTeX amsmath package 1110 ("subsubsection" . 4)
674 1111 ("paragraph" . 5)
675 For the full list of options, see the constant `reftex-label-alist-builtin'. 1112 ("subparagraph" . 6)
676 Better still, try 1113 ("subsubparagraph" . 7)
677 1114 )
678 M-x customize-variable RET reftex-default-label-alist-entries RET." 1115 "Commands and levels used for defining sections in the document.
1116 The car of each cons cell is the name of the section macro. The cdr is a
1117 number indicating its level."
679 :group 'reftex-defining-label-environments 1118 :group 'reftex-defining-label-environments
680 :set 'reftex-set-dirty 1119 :set 'reftex-set-dirty
681 :type '(list :indent 4 1120 :type '(repeat
682 :convert-widget 1121 (cons (string :tag "sectioning macro" "")
683 (lambda (widget) 1122 (number :tag "level " 0))))
684 (let* ((args 1123
685 (list 1124 (defcustom reftex-default-context-regexps
686 `(checklist 1125 '((caption . "\\\\\\(rot\\)?caption\\*?[[{]")
687 :inline t 1126 (item . "\\\\item\\(\\[[^]]*\\]\\)?")
688 ,@(reverse 1127 (eqnarray-like . "\\\\begin{%s}\\|\\\\\\\\")
689 (mapcar (lambda (x) 1128 (alignat-like . "\\\\begin{%s}{[0-9]*}\\|\\\\\\\\"))
690 (list 'const ':tag (nth 1 x) (car x))) 1129 "Alist with default regular expressions for finding context.
691 reftex-label-alist-builtin)))))) 1130 The form (format regexp (regexp-quote environment)) is used to calculate
692 (widget-put widget :args args) 1131 the final regular expression - so %s will be replaced with the environment
693 widget)))) 1132 or macro."
694 1133 :group 'reftex-defining-label-environments
1134 :type '(repeat (cons (symbol) (regexp))))
1135
695 (defcustom reftex-use-text-after-label-as-context nil 1136 (defcustom reftex-use-text-after-label-as-context nil
696 "*t means, grab context from directly after the \\label{..} macro. 1137 "*t means, grab context from directly after the \\label{..} macro.
697 This is the fastest method for obtaining context of the label definition, but 1138 This is the fastest method for obtaining context of the label definition, but
698 requires discipline when placing labels. Setting this variable to t takes 1139 requires discipline when placing labels. Setting this variable to t takes
699 precedence over the individual settings in `reftex-label-alist'. 1140 precedence over the individual settings in `reftex-label-alist'.
700 This variable may be set to t, nil, or a string of label type letters 1141 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." 1142 indicating the label types for which it should be true."
702 :group 'reftex-defining-label-environments 1143 :group 'reftex-defining-label-environments
703 :set 'reftex-set-dirty 1144 :set 'reftex-set-dirty
704 :type '(choice 1145 :type '(choice
705 (const :tag "on" t) (const :tag "off" nil) 1146 (const :tag "on" t) (const :tag "off" nil)
706 (string :tag "Selected label types"))) 1147 (string :tag "Selected label types")))
707 1148
708 ;; Label insertion 1149 ;; Label insertion
709 1150
710 (defgroup reftex-making-and-inserting-labels nil 1151 (defgroup reftex-making-and-inserting-labels nil
711 "Options on how to create new labels." 1152 "Options on how to create new labels."
712 :prefix "reftex-"
713 :group 'reftex-label-support) 1153 :group 'reftex-label-support)
714 1154
715 (defcustom reftex-insert-label-flags '("s" "sft") 1155 (defcustom reftex-insert-label-flags '("s" "sft")
716 "Flags governing label insertion. First flag DERIVE, second flag PROMPT. 1156 "Flags governing label insertion. First flag DERIVE, second flag PROMPT.
717 1157
718 If DERIVE is t, RefTeX will try to derive a sensible label from context. 1158 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. 1159 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 1160 The conversion of the context to a legal label is governed by the
721 specifications given in `reftex-derive-label-parameters'. 1161 specifications given in `reftex-derive-label-parameters'.
722 If RefTeX fails to derive a label, it will prompt the user. 1162 If RefTeX fails to derive a label, it will prompt the user.
1163 If DERIVE is nil, the label generated will consist of the prefix and a
1164 unique number, like `eq:23'.
723 1165
724 If PROMPT is t, the user will be prompted for a label string. The prompt will 1166 If PROMPT is t, the user will be prompted for a label string. The prompt will
725 already contain the prefix, and (if DERIVE is t) a default label derived from 1167 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 1168 context. When PROMPT is nil, the default label will be inserted without
727 query. 1169 query.
744 without confirmation for everything else." 1186 without confirmation for everything else."
745 :group 'reftex-making-and-inserting-labels 1187 :group 'reftex-making-and-inserting-labels
746 :type '(list (choice :tag "Derive label from context" 1188 :type '(list (choice :tag "Derive label from context"
747 (const :tag "always" t) 1189 (const :tag "always" t)
748 (const :tag "never" nil) 1190 (const :tag "never" nil)
749 (string :tag "for selected label types" "")) 1191 (string :tag "selected label types" ""))
750 (choice :tag "Prompt for label string " 1192 (choice :tag "Prompt for label string "
751 :entry-format " %b %v" 1193 :entry-format " %b %v"
752 (const :tag "always" t) 1194 (const :tag "always" t)
753 (const :tag "never" nil) 1195 (const :tag "never" nil)
754 (string :tag "for selected label types" "")))) 1196 (string :tag "selected label types" ""))))
755 1197
756 (defcustom reftex-derive-label-parameters '(3 20 t 1 "-" ; continue 1198 (defcustom reftex-derive-label-parameters '(3 20 t 1 "-"
757 ("the" "on" "in" "off" "a" "for" "by" "of" "and" "is")) 1199 ("the" "on" "in" "off" "a" "for" "by" "of" "and" "is"))
758 "Parameters for converting a string into a label. 1200 "Parameters for converting a string into a label.
759 NWORDS Number of words to use. 1201 NWORDS Number of words to use.
760 MAXCHAR Maximum number of characters in a label string. 1202 MAXCHAR Maximum number of characters in a label string.
761 ILLEGAL nil: Throw away any words containing characters illegal in labels. 1203 ILLEGAL nil: Throw away any words containing characters illegal in labels.
766 label string below MAXCHAR. 1208 label string below MAXCHAR.
767 SEPARATOR String separating different words in the label. 1209 SEPARATOR String separating different words in the label.
768 IGNOREWORDS List of words which should not be part of labels." 1210 IGNOREWORDS List of words which should not be part of labels."
769 :group 'reftex-making-and-inserting-labels 1211 :group 'reftex-making-and-inserting-labels
770 :type '(list (integer :tag "Number of words " 3) 1212 :type '(list (integer :tag "Number of words " 3)
771 (integer :tag "Maximum label length " 20) 1213 (integer :tag "Maximum label length " 20)
772 (choice :tag "Illegal characters in words" 1214 (choice :tag "Illegal characters in words"
773 (const :tag "throw away entire word" nil) 1215 (const :tag "throw away entire word" nil)
774 (const :tag "throw away single chars" t)) 1216 (const :tag "throw away single chars" t))
775 (choice :tag "Abbreviate words " 1217 (choice :tag "Abbreviate words "
776 (const :tag "never" nil) 1218 (const :tag "never" nil)
777 (const :tag "always" t) 1219 (const :tag "always" t)
778 (const :tag "when label is too long" 1)) 1220 (const :tag "when label is too long" 1))
779 (string :tag "Separator between words " "-") 1221 (string :tag "Separator between words " "-")
780 (repeat :tag "Ignore words" 1222 (repeat :tag "Ignore words"
781 :entry-format " %i %d %v" 1223 :entry-format " %i %d %v"
782 (string :tag "")))) 1224 (string :tag ""))))
783 1225
784 (defcustom reftex-label-illegal-re "[\000-\040\177-\377\\\\#$%&~^_{}]" 1226 (defcustom reftex-label-illegal-re "[\000-\040\177-\377\\\\#$%&~^_{}]"
785 "Regexp matching characters not legal in labels. 1227 "Regexp matching characters not legal in labels.
786 For historic reasons, this character class comes *with* the [] brackets." 1228 For historic reasons, this character class comes *with* the [] brackets."
787 :group 'reftex-making-and-inserting-labels 1229 :group 'reftex-making-and-inserting-labels
788 :type '(regexp :tag "Character class")) 1230 :type '(regexp :tag "Character class"))
793 MIN-KILL Minimum number of characters to remove when abbreviating words. 1235 MIN-KILL Minimum number of characters to remove when abbreviating words.
794 BEFORE Character class before abbrev point in word. 1236 BEFORE Character class before abbrev point in word.
795 AFTER Character class after abbrev point in word." 1237 AFTER Character class after abbrev point in word."
796 :group 'reftex-making-and-inserting-labels 1238 :group 'reftex-making-and-inserting-labels
797 :type '(list 1239 :type '(list
798 (integer :tag "Minimum chars per word" 4) 1240 (integer :tag "Minimum chars per word" 4)
799 (integer :tag "Shorten by at least " 2) 1241 (integer :tag "Shorten by at least " 2)
800 (string :tag "cut before char class " "^saeiou") 1242 (string :tag "cut before char class " "^saeiou")
801 (string :tag "cut after char class " "aeiou"))) 1243 (string :tag "cut after char class " "aeiou")))
802
803 1244
804 ;; Label referencing 1245 ;; Label referencing
805 1246
806 (defgroup reftex-referencing-labels nil 1247 (defgroup reftex-referencing-labels nil
807 "Options on how to reference labels." 1248 "Options on how to reference labels."
808 :prefix "reftex-"
809 :group 'reftex-label-support) 1249 :group 'reftex-label-support)
810 1250
811 (defcustom reftex-label-menu-flags '(t t nil nil nil nil) 1251 (eval-and-compile
812 "*List of flags governing the label menu makeup. 1252 (defconst reftex-tmp
1253 '((const :tag "on" t)
1254 (const :tag "off" nil)
1255 (string :tag "Selected label types"))))
1256
1257 (defcustom reftex-label-menu-flags '(t t nil nil nil nil t nil)
1258 "List of flags governing the label menu makeup.
813 The flags are: 1259 The flags are:
814 1260
815 TABLE-OF-CONTENTS Show the labels embedded in a table of context. 1261 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. 1262 SECTION-NUMBERS Include section numbers (like 4.1.3) in table of contents.
817 COUNTERS Show counters. This just numbers the labels in the menu. 1263 COUNTERS Show counters. This just numbers the labels in the menu.
818 NO-CONTEXT Non-nil means do NOT show the short context. 1264 NO-CONTEXT Non-nil means do NOT show the short context.
819 FOLLOW Follow full context in other window. 1265 FOLLOW Follow full context in other window.
820 SHOW-COMMENTED Show labels from regions which are commented out. RefTeX 1266 SHOW-COMMENTED Show labels from regions which are commented out.
821 sees these labels, but does not normally show them. 1267 MATCH-IN-TOC Searches in label menu will also match in toc lines.
1268 SHOW FILES Show Begin and end of included files.
822 1269
823 Each of these flags can be set to t or nil, or to a string of type letters 1270 Each of these flags can be set to t or nil, or to a string of type letters
824 indicating the label types for which it should be true. These strings work 1271 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 1272 like character classes in regular expressions. Thus, setting one of the
826 flags to \"sf\" makes the flag true for section and figure labels, nil 1273 flags to \"sf\" makes the flag true for section and figure labels, nil
828 1275
829 Most options can also be switched from the label menu itself - so if you 1276 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 1277 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." 1278 get one interactively during selection from the label menu."
832 :group 'reftex-referencing-labels 1279 :group 'reftex-referencing-labels
833 :type '(list 1280 :type
834 (choice :tag "Embed in table of contents " 1281 `(list
835 (const :tag "on" t) (const :tag "off" nil) 1282 (choice :tag "Embed in table of contents " ,@reftex-tmp)
836 (string :tag "Selected label types")) 1283 (choice :tag "Show section numbers " ,@reftex-tmp)
837 (choice :tag "Show section numbers " 1284 (choice :tag "Show individual counters " ,@reftex-tmp)
838 (const :tag "on" t) (const :tag "off" nil)) 1285 (choice :tag "Hide short context " ,@reftex-tmp)
839 (choice :tag "Show individual counters " 1286 (choice :tag "Follow context in other window " ,@reftex-tmp)
840 (const :tag "on" t) (const :tag "off" nil) 1287 (choice :tag "Show commented labels " ,@reftex-tmp)
841 (string :tag "Selected label types")) 1288 (choice :tag "Searches match in toc lines " ,@reftex-tmp)
842 (choice :tag "Hide short context " 1289 (choice :tag "Show begin/end of included files" ,@reftex-tmp)))
843 (const :tag "on" t) (const :tag "off" nil) 1290
844 (string :tag "Selected label types")) 1291 (defcustom reftex-level-indent 2
845 (choice :tag "Follow context in other window" 1292 "*Number of spaces to be used for indentation per section level."
846 (const :tag "on" t) (const :tag "off" nil) 1293 :group 'reftex-referencing-labels
847 (string :tag "Selected label types")) 1294 :type '(integer))
848 (choice :tag "Show commented labels " 1295
849 (const :tag "on" t) (const :tag "off" nil) 1296 (defcustom reftex-refontify-context 1
850 (string :tag "Selected label types")))) 1297 "*Non-nil means, re-fontify the context in the label menu with font-lock.
851 1298 This slightly slows down the creation of the label menu. It is only necessay
1299 when you definitely want the context fontified.
1300
1301 This option may have 3 different values:
1302 nil Never refontify.
1303 t Always refontify.
1304 1 Refontify when absolutly necessary, e.g. when with the x-symbol package.
1305 The option is ignored when `reftex-use-fonts' is nil."
1306 :group 'reftex-referencing-labels
1307 :type '(choice
1308 (const :tag "Never" nil)
1309 (const :tag "Always" t)
1310 (const :tag "When necessary" 1)))
852 1311
853 (defcustom reftex-guess-label-type t 1312 (defcustom reftex-guess-label-type t
854 "*Non-nil means, `reftex-reference' will try to guess the label type. 1313 "*Non-nil means, `reftex-reference' will try to guess the label type.
855 To do that, RefTeX will look at the word before the cursor and compare it with 1314 To do that, RefTeX will look at the word before the cursor and compare it with
856 the words given in `reftex-label-alist'. When it finds a match, RefTeX will 1315 the words given in `reftex-label-alist'. When it finds a match, RefTeX will
861 1320
862 ;; BibteX citation configuration ---------------------------------------- 1321 ;; BibteX citation configuration ----------------------------------------
863 1322
864 (defgroup reftex-citation-support nil 1323 (defgroup reftex-citation-support nil
865 "Support for referencing bibliographic data with BibTeX." 1324 "Support for referencing bibliographic data with BibTeX."
866 :prefix "reftex-"
867 :group 'reftex) 1325 :group 'reftex)
868 1326
869 (defcustom reftex-bibpath-environment-variables '("BIBINPUTS" "TEXBIB") 1327 (defcustom reftex-bibpath-environment-variables '("BIBINPUTS" "TEXBIB")
870 "*List of env vars which might contain the path to BibTeX database files." 1328 "*List of env vars which might contain the path to BibTeX database files."
871 :group 'reftex-citation-support 1329 :group 'reftex-citation-support
872 :set 'reftex-set-dirty 1330 :set 'reftex-set-dirty
873 :type '(repeat (string :tag "Environment variable"))) 1331 :type '(repeat (string :tag "Environment variable")))
874 1332
875 (defcustom reftex-bibfile-ignore-list nil 1333 (defcustom reftex-bibfile-ignore-list nil
876 "List of files in \\bibliography{..} RefTeX should *not* parse. 1334 "*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 1335 The file names have to be in the exact same form as in the bibliography
878 macro - i.e. without the `.bib' extension. 1336 macro - i.e. without the `.bib' extension.
879 Intended for files which contain only `@string' macro definitions and the 1337 Intended for files which contain only `@string' macro definitions and the
880 like, which are ignored by RefTeX anyway." 1338 like, which are ignored by RefTeX anyway."
881 :group 'reftex-citation-support 1339 :group 'reftex-citation-support
889 'author Sort entries by author name. 1347 'author Sort entries by author name.
890 'year Sort entries by increasing year. 1348 'year Sort entries by increasing year.
891 'reverse-year Sort entries by decreasing year." 1349 'reverse-year Sort entries by decreasing year."
892 :group 'reftex-citation-support 1350 :group 'reftex-citation-support
893 :type '(choice (const :tag "not" nil) 1351 :type '(choice (const :tag "not" nil)
894 (const :tag "by author" author) 1352 (const :tag "by author" author)
895 (const :tag "by year" year) 1353 (const :tag "by year" year)
896 (const :tag "by year, reversed" reverse-year))) 1354 (const :tag "by year, reversed" reverse-year)))
897 1355
898 (defcustom reftex-cite-format 'reftex-cite-format-default 1356 (defcustom reftex-cite-format 'default
899 "Defines the format of citations to be inserted into the buffer. 1357 "*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 1358 It can be a string or an alist. In the simplest case this is just
901 and a list of strings in the car. In the simplest case, this can just 1359 the string \"\\cite{%l}\", which is also the default. See the
902 be the string \"\\cite{KEY}\", which is also the default. See the 1360 definition of `reftex-cite-format-builtin' for more complex examples.
903 definition of the `reftex-cite-format-XXXX' constants for more complex 1361
904 examples. 1362 If `reftex-cite-format' is a string, it will be used as the format.
905 If `reftex-cite-format' is a string, it will be used as the format. 1363 In the format, the following percent escapes will be expanded.
906 In the format, AUTHOR will be replaced by the last name of the 1364
907 author, YEAR will be replaced by the year and KEY by the citation 1365 %l The BibTeX label of the citation.
908 key. If AUTHOR is present several times, it will be replaced with 1366 %a List of author names, see also `reftex-cite-punctuation.
909 successive author names. 1367 %2a Like %a, but abbreviate more than 2 authors like Jones et al.
910 See the constant `reftex-cite-format-default' for an example. 1368 %A First author name only.
911 If `reftex-cite-format' is a list of strings, the string used will 1369 %e Works like %a, but on list of editor names. (%2e and %E work a well)
912 depend upon the number of authors of the article. No authors means, 1370
913 the first string will be used; 1 author means, the second string will 1371 It is also possible to access all other BibTeX database fields:
914 be used etc.. The last string in the list will be used for all articles 1372 %b booktitle %c chapter %d edition %h howpublished
915 with too many authors. See `reftex-cite-format-1-author-simple' for an 1373 %i institution %j journal %k key %m month
916 example. 1374 %n number %o organization %p pages %P first page
917 If `reftex-cite-format' is a list of cons cells, the car of each cell 1375 %r address %s school %u publisher %t title
918 needs to be a character. When a selected reference is accepted by 1376 %v volume %y year
919 pressing that key, the cdr of the associated list will be used as 1377
920 described above. See `reftex-cite-format-2-authors' for an example. 1378 Usually, only %l is needed. Try, however, (setq reftex-comment-citations t).
1379
1380 If `reftex-cite-format' is an alist of characters and strings, the user
1381 will be prompted for a character to select one of the possible format
1382 strings.
921 In order to configure this variable, you can either set 1383 In order to configure this variable, you can either set
922 `reftex-cite-format' directly yourself or set it to the SYMBOL of one of 1384 `reftex-cite-format' directly yourself or set it to the SYMBOL of one of
923 the predefined constants. E.g.: 1385 the predefined styles (see `reftex-cite-format-builtin'). E.g.:
924 (setq reftex-cite-format 'reftex-cite-format-2-authors)" 1386 (setq reftex-cite-format 'harvard)"
925 :group 'reftex-citation-support 1387 :group 'reftex-citation-support
926 :type 1388 :type
927 '(choice 1389 `(choice
928 (choice :tag "symbolic defaults" 1390 :format "%{%t%}: \n%[Value Menu%] %v"
929 :value reftex-cite-format-default 1391 (radio :tag "Symbolic Builtins"
930 (const reftex-cite-format-default) 1392 :indent 4
931 (const reftex-cite-format-1-author-simple) 1393 :value default
932 (const reftex-cite-format-2-authors)) 1394 ,@(mapcar
933 (string :tag "format string" "\\cite{KEY}") 1395 (function
934 (repeat :tag "list of strings" 1396 (lambda (x)
935 :value ("\cite{KEY}" "AUTHOR \cite{KEY}" "AUTHOR and AUTHOR \cite{KEY}") 1397 (list 'const ':tag (concat (symbol-name (nth 0 x))
936 (string :tag "format string" "")) 1398 ": " (nth 1 x))
937 (repeat :tag "key-ed lists of strings" 1399 (nth 0 x))))
938 :value ((? 1400 reftex-cite-format-builtin))
939 . ("\cite{KEY}" "AUTHOR \cite{KEY}" "AUTHOR and AUTHOR \cite{KEY}"))) 1401 (string :tag "format string" "\\cite{%l}")
940 (cons :tag "Enter a keyed list of format strings" 1402 (repeat :tag "key-ed format strings"
941 (character :tag "Key character " ? 1403 :value ((?\r . "\\cite{%l}")
942 ) 1404 (?t . "\\cite{%l}") (?p . "\\cite{%l}"))
943 (repeat 1405 (cons (character :tag "Key character" ?\r)
944 (string :tag "format string" "")))))) 1406 (string :tag "Format string" "")))))
1407
1408 (defcustom reftex-comment-citations nil
1409 "*Non-nil means add a comment for each citation describing the full entry.
1410 The comment is formatted according to `reftex-cite-comment-format'."
1411 :group 'reftex-citation-support
1412 :type '(boolean))
1413
1414 (defcustom reftex-cite-comment-format
1415 "%% %2a %y, %j %v, %P, %e: %b, %u, %s %<\n"
1416 "Citation format used for commented citations. Must NOT contain %l."
1417 :group 'reftex-citation-support
1418 :type '(string))
1419
1420 (defcustom reftex-cite-punctuation '(", " " \\& " " {\\it et al.}")
1421 "Punctuation for formatting of name lists in citations.
1422 This is a list of 3 strings.
1423 1. normal names separator, like \", \" in Jones, Brown and Miller
1424 2. final names separator, like \" and \" in Jones, Brown and Miller
1425 3. The \"et al\" string, like \" {...}\" in Jones {\\it et al.}"
1426 :group 'reftex-citation-support
1427 :type '(list
1428 (string :tag "Separator for names ")
1429 (string :tag "Separator for last name in list")
1430 (string :tag "string used as et al. ")))
945 1431
946 ;; Table of contents configuration -------------------------------------- 1432 ;; Table of contents configuration --------------------------------------
947 1433
948 (defgroup reftex-table-of-contents-browser nil 1434 (defgroup reftex-table-of-contents-browser nil
949 "A multifile table of contents browser." 1435 "A multifile table of contents browser."
950 :prefix "reftex-"
951 :group 'reftex) 1436 :group 'reftex)
952 1437
953 (defcustom reftex-toc-follow-mode nil 1438 (defcustom reftex-toc-follow-mode nil
954 "Non-nil means, point in *toc* buffer will cause other window to follow. 1439 "*Non-nil means, point in *toc* buffer will cause other window to follow.
955 The other window will show the corresponding part of the document. 1440 The other window will show the corresponding part of the document.
956 This flag can be toggled from within the *toc* buffer with the `f' key." 1441 This flag can be toggled from within the *toc* buffer with the `f' key."
957 :group 'reftex-table-of-contents-browser 1442 :group 'reftex-table-of-contents-browser
958 :type '(boolean)) 1443 :type '(boolean))
959 1444
1445 ;; Tuning the parser ----------------------------------------------------
1446
1447 (defgroup reftex-optimizations-for-large-documents nil
1448 "Configuration of parser speed and memory usage."
1449 :group 'reftex)
1450
1451 (defcustom reftex-keep-temporary-buffers 1
1452 "*Non-nil means, keep buffers created for parsing and lookup.
1453 RefTeX sometimes needs to visit files related to the current document.
1454 We distinguish files visited for
1455 PARSING: Parts of a multifile document loaded when (re)-parsing the document.
1456 LOOKUP: BibTeX database files and TeX files loaded to find a reference,
1457 to display label context, etc.
1458 The created buffers can be kept for later use, or be thrown away immediately
1459 after use, depending on the value of this variable:
1460
1461 nil Throw away as much as possible.
1462 t Keep everything.
1463 1 Throw away buffers created for parsing, but keep the ones created
1464 for lookup.
1465
1466 If a buffer is to be kept, the file is visited normally (which is potentially
1467 slow but will happen only once).
1468 If a buffer is to be thrown away, the initialization of the buffer depends
1469 upon the variable `reftex-initialize-temporary-buffers'."
1470 :group 'reftex-miscellaneous-configurations
1471 :type '(choice
1472 (const :tag "Throw away everything" nil)
1473 (const :tag "Keep everything" t)
1474 (const :tag "Keep lookup buffers only" 1)))
1475
1476 (defcustom reftex-initialize-temporary-buffers nil
1477 "*Non-nil means do initializations even when visiting file temporarily.
1478 When nil, RefTeX may turn off find-file hooks and other stuff to briefly
1479 visit a file.
1480 When t, the full default initializations are done (find-file-hook etc.).
1481 Instead of t or nil, this variable may also be a list of hook functions to
1482 do a minimal initialization."
1483 :group 'reftex-miscellaneous-configurations
1484 :type '(choice
1485 (const :tag "Read files literally" nil)
1486 (const :tag "Fully initialize buffers" t)
1487 (repeat :tag "Hook functions" :value (nil)
1488 (function-item))))
1489
1490 (defcustom reftex-enable-partial-scans nil
1491 "*Non-nil means, re-parse only 1 file when asked to re-parse.
1492 Re-parsing is normally requested with a `C-u' prefix to many RefTeX commands,
1493 or with the `r' key in menus. When this option is t in a multifile document,
1494 we will only parse the current buffer, or the file associated with the label
1495 or section heading near point in a menu. Requesting re-parsing of an entire
1496 multifile document then requires a `C-u C-u' prefix or the capital `R' key
1497 in menus."
1498 :group 'reftex-optimizations-for-large-documents
1499 :type 'boolean)
1500
1501 (defcustom reftex-save-parse-info nil
1502 "*Non-nil means, save information gathered with parsing in a file.
1503 The file MASTER.rel in the same directory as MASTER.tex is used to save the
1504 information. When this variable is t,
1505 - accessing the parsing information for the first time in an editing session
1506 will read that file (if available) instead of parsing the document.
1507 - each time (part of) the document is rescanned, a new version of the file
1508 is written."
1509 :group 'reftex-optimizations-for-large-documents
1510 :type 'boolean)
1511
960 ;; Miscellaneous configurations ----------------------------------------- 1512 ;; Miscellaneous configurations -----------------------------------------
961 1513
962 (defgroup reftex-miscellaneous-configurations nil 1514 (defgroup reftex-miscellaneous-configurations nil
963 "Collection of further configurations." 1515 "Collection of further configurations."
964 :prefix "reftex-"
965 :group 'reftex) 1516 :group 'reftex)
966 1517
967 (defcustom reftex-extra-bindings nil 1518 (defcustom reftex-extra-bindings nil
968 "Non-nil means, make additional key bindings on startup. 1519 "Non-nil means, make additional key bindings on startup.
969 These extra bindings are located in the users `C-c letter' map." 1520 These extra bindings are located in the users `C-c letter' map."
970 :group 'reftex-miscellaneous-configurations 1521 :group 'reftex-miscellaneous-configurations
971 :type '(boolean)) 1522 :type '(boolean))
972 1523
973 (defcustom reftex-plug-into-AUCTeX nil 1524 (defcustom reftex-plug-into-AUCTeX nil
974 "Plug-in flags for AUCTeX interface. 1525 "*Plug-in flags for AUCTeX interface.
975 This variable is a list of 4 boolean flags. When a flag is non-nil, it 1526 This variable is a list of 4 boolean flags. When a flag is non-nil, it
976 means: 1527 means:
977 1528
978 Flag 1: use `reftex-label' as `LaTeX-label-function'. 1529 Flag 1: use `reftex-label' as `LaTeX-label-function'.
979 Flag 2: use `reftex-arg-label' as `TeX-arg-label' 1530 Flag 2: use `reftex-arg-label' as `TeX-arg-label'
989 See the AUCTeX documentation for more information. 1540 See the AUCTeX documentation for more information.
990 RefTeX uses `fset' to take over the function calls. Changing the variable 1541 RefTeX uses `fset' to take over the function calls. Changing the variable
991 may require a restart of Emacs in order to become effective." 1542 may require a restart of Emacs in order to become effective."
992 :group 'reftex-miscellaneous-configurations 1543 :group 'reftex-miscellaneous-configurations
993 :type '(choice (const :tag "No plug-ins" nil) 1544 :type '(choice (const :tag "No plug-ins" nil)
994 (const :tag "All possible plug-ins" t) 1545 (const :tag "All possible plug-ins" t)
995 (list 1546 (list
996 :tag "Individual choice" 1547 :tag "Individual choice"
997 :value (nil nil nil nil) 1548 :value (nil nil nil nil)
998 (boolean :tag "Use reftex-label as LaTeX-label-function") 1549 (boolean :tag "Use reftex-label as LaTeX-label-function")
999 (boolean :tag "Use reftex-arg-label as TeX-arg-label ") 1550 (boolean :tag "Use reftex-arg-label as TeX-arg-label ")
1000 (boolean :tag "Use reftex-arg-ref as TeX-arg-ref ") 1551 (boolean :tag "Use reftex-arg-ref as TeX-arg-ref ")
1001 (boolean :tag "Use reftex-arg-cite as TeX-arg-cite ") 1552 (boolean :tag "Use reftex-arg-cite as TeX-arg-cite ")
1002 ))) 1553 )))
1003 1554
1004 (defcustom reftex-use-fonts t 1555 (defcustom reftex-use-fonts t
1005 "*Non-nil means, use fonts in label menu and on-the-fly help. 1556 "*Non-nil means, use fonts in label menu and on-the-fly help.
1006 Font-lock must be loaded as well to actually get fontified display." 1557 Font-lock must be loaded as well to actually get fontified display."
1007 :group 'reftex-miscellaneous-configurations 1558 :group 'reftex-miscellaneous-configurations
1008 :type '(boolean)) 1559 :type '(boolean))
1009 1560
1010 (defcustom reftex-keep-temporary-buffers t 1561 (defcustom reftex-auto-show-entry 'copy
1011 "*Non-nil means, keep any TeX and BibTeX files loaded for lookup. 1562 "*Non-nil means, do something when context in other window is hidden.
1012 Nil means, kill it immediately after use unless it was already an existing 1563 Some modes like `outline-mode' or `folding-mode' hide parts of buffers.
1013 buffer before the lookup happened. It is faster to keep the buffers, but can 1564 When RefTeX is asked to show context for a label definition, and the context
1014 use a lot of memory, depending on the size of your database and document." 1565 is invisible, it can unhide that section permanently (value t), or copy the
1566 context to a temporary buffer (value 'copy)."
1015 :group 'reftex-miscellaneous-configurations 1567 :group 'reftex-miscellaneous-configurations
1016 :type '(boolean)) 1568 :type '(radio :value copy
1017 1569 :indent 4
1018 (defcustom reftex-auto-show-entry t 1570 (const :tag "Do nothing" nil)
1019 "*Non-nil means, showing context in another window may unhide a section. 1571 (const :tag "Unhide section permanently" t)
1020 This is important when using outline-minor-mode. If the context to be shown 1572 (const :tag "Copy context to show" copy)))
1021 is in a hidden section, RefTeX will issue a \"show-entry\" command in order 1573
1022 to show it. This is not reversed when the label is selected - so the section 1574 (defcustom reftex-load-hook nil
1023 remains shown after command completion." 1575 "Hook which is being run when loading reftex.el."
1024 :group 'reftex-miscellaneous-configurations 1576 :group 'reftex-miscellaneous-configurations
1025 :type '(boolean)) 1577 :type 'hook)
1026 1578
1579 (defcustom reftex-mode-hook nil
1580 "Hook which is being run when turning on RefTeX mode."
1581 :group 'reftex-miscellaneous-configurations
1582 :type 'hook)
1027 1583
1028 ;;; End of Configuration Section ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 1584 ;;; End of Configuration Section ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1029 1585
1030 ;;;=========================================================================== 1586 ;;;===========================================================================
1031 ;;; 1587 ;;;
1032 ;;; Define the formal stuff for a minor mode named RefTeX. 1588 ;;; Define the formal stuff for a minor mode named RefTeX.
1033 ;;; 1589 ;;;
1034 1590
1035 (defconst reftex-version "2.14 for Emacs distribution." 1591 (defconst reftex-version "RefTeX version 3.7"
1036 "Version string for RefTeX.") 1592 "Version string for RefTeX.")
1037 1593
1038 (defvar reftex-mode nil 1594 (defvar reftex-mode nil
1039 "Determines if RefTeX minor mode is active.") 1595 "Determines if RefTeX minor mode is active.")
1040 (make-variable-buffer-local 'reftex-mode) 1596 (make-variable-buffer-local 'reftex-mode)
1056 Labels can be created with `\\[reftex-label]' and referenced with `\\[reftex-reference]'. 1612 Labels can be created with `\\[reftex-label]' and referenced with `\\[reftex-reference]'.
1057 When referencing, you get a menu with all labels of a given type and 1613 When referencing, you get a menu with all labels of a given type and
1058 context of the label definition. The selected label is inserted as a 1614 context of the label definition. The selected label is inserted as a
1059 \\ref macro. 1615 \\ref macro.
1060 1616
1061 Citations can be made with `\\[reftex-citation]' which will use a regular expression 1617 Citations can be made with `\\[reftex-citation]' which will use a regular expression
1062 to pull out a *formatted* list of articles from your BibTeX 1618 to pull out a *formatted* list of articles from your BibTeX
1063 database. The selected citation is inserted as a \\cite macro. 1619 database. The selected citation is inserted as a \\cite macro.
1064 1620
1065 A Table of Contents of the entire (multifile) document with browsing 1621 A Table of Contents of the entire (multifile) document with browsing
1066 capabilities is available with `\\[reftex-toc]'. 1622 capabilities is available with `\\[reftex-toc]'.
1067 1623
1068 Most command have help available on the fly. This help is accessed by 1624 Most command have help available on the fly. This help is accessed by
1069 pressing `?' to any prompt mentioning this feature. 1625 pressing `?' to any prompt mentioning this feature.
1070 1626
1071 Extensive documentation about reftex is in the file header of `reftex.el'. 1627 Extensive documentation about RefTeX is in the file header of `reftex.el'.
1628 You can view this information with `\\[reftex-show-commentary]'.
1072 1629
1073 \\{reftex-mode-map} 1630 \\{reftex-mode-map}
1074 Under X, these functions will also be available in a menu on the menu bar. 1631 Under X, these and other functions will also be available as `Ref' menu
1632 on the menu bar.
1075 1633
1076 ------------------------------------------------------------------------------" 1634 ------------------------------------------------------------------------------"
1077 1635
1078 (interactive "P") 1636 (interactive "P")
1079 (setq reftex-mode (not (or (and (null arg) reftex-mode) 1637 (setq reftex-mode (not (or (and (null arg) reftex-mode)
1080 (<= (prefix-numeric-value arg) 0)))) 1638 (<= (prefix-numeric-value arg) 0))))
1081 1639
1082 ; Add or remove the menu, and run the hook 1640 ; Add or remove the menu, and run the hook
1083 (if reftex-mode 1641 (if reftex-mode
1084 (progn 1642 (progn
1085 (easy-menu-add reftex-mode-menu) 1643 (easy-menu-add reftex-mode-menu)
1086 (reftex-plug-into-AUCTeX) 1644 (reftex-plug-into-AUCTeX)
1087 (run-hooks 'reftex-mode-hook)) 1645 (run-hooks 'reftex-mode-hook))
1088 (easy-menu-remove reftex-mode-menu))) 1646 (easy-menu-remove reftex-mode-menu)))
1089 1647
1090 (or (assoc 'reftex-mode minor-mode-alist) 1648 (or (assoc 'reftex-mode minor-mode-alist)
1091 (setq minor-mode-alist 1649 (push '(reftex-mode " Ref") minor-mode-alist))
1092 (cons '(reftex-mode " Ref") minor-mode-alist)))
1093 1650
1094 (or (assoc 'reftex-mode minor-mode-map-alist) 1651 (or (assoc 'reftex-mode minor-mode-map-alist)
1095 (setq minor-mode-map-alist 1652 (push (cons 'reftex-mode reftex-mode-map) minor-mode-map-alist))
1096 (cons (cons 'reftex-mode reftex-mode-map)
1097 minor-mode-map-alist)))
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107 1653
1108 ;;; =========================================================================== 1654 ;;; ===========================================================================
1109 ;;; 1655 ;;;
1110 ;;; Silence warnings about variables in other packages. 1656 ;;; Silence warnings about variables in other packages.
1111 (defvar TeX-master) 1657 (defvar TeX-master)
1119 ;;; ----------------------------- 1665 ;;; -----------------------------
1120 ;;; 1666 ;;;
1121 ;;; AUCTeX 1667 ;;; AUCTeX
1122 ;;; ------ 1668 ;;; ------
1123 1669
1124 (defun reftex-arg-label (optional &optional prompt definition) 1670 (defun reftex-arg-label (optional &optional prompt definition)
1125 "Use `reftex-label' to create label. Insert it with `TeX-argument-insert'. 1671 "Use `reftex-label' to create label. Insert it with `TeX-argument-insert'.
1126 This function is intended for AUCTeX macro support." 1672 This function is intended for AUCTeX macro support."
1127 (let ((label (reftex-label nil t))) 1673 (let ((label (reftex-label nil t)))
1128 (if (and definition (not (string-equal "" label))) 1674 (if (and definition (not (string-equal "" label)))
1129 (LaTeX-add-labels label)) 1675 (LaTeX-add-labels label))
1130 (TeX-argument-insert label optional optional))) 1676 (TeX-argument-insert label optional optional)))
1131 1677
1132 (defun reftex-arg-ref (optional &optional prompt definition) 1678 (defun reftex-arg-ref (optional &optional prompt definition)
1133 "Use `reftex-reference' to select label. Insert with `TeX-argument-insert'. 1679 "Use `reftex-reference' to select label. Insert with `TeX-argument-insert'.
1134 This function is intended for AUCTeX macro support." 1680 This function is intended for AUCTeX macro support."
1135 (let ((label (reftex-reference nil t))) 1681 (let ((label (reftex-reference nil t)))
1136 (if (and definition (not (string-equal "" label))) 1682 (if (and definition (not (string-equal "" label)))
1137 (LaTeX-add-labels label)) 1683 (LaTeX-add-labels label))
1138 (TeX-argument-insert label optional optional))) 1684 (TeX-argument-insert label optional optional)))
1139 1685
1140 (defun reftex-arg-cite (optional &optional prompt definition) 1686 (defun reftex-arg-cite (optional &optional prompt definition)
1141 "Use reftex-citation to select a key. Insert with `TeX-argument-insert'. 1687 "Use reftex-citation to select a key. Insert with `TeX-argument-insert'.
1142 This function is intended for AUCTeX macro support." 1688 This function is intended for AUCTeX macro support."
1143 (let ((key (reftex-citation nil t))) 1689 (let ((key (reftex-citation t)))
1144 (TeX-argument-insert (or key "") optional optional))) 1690 (TeX-argument-insert (or key "") optional optional)))
1145 1691
1146 (defun reftex-plug-into-AUCTeX () 1692 (defun reftex-plug-into-AUCTeX ()
1147 ;; Replace AucTeX functions with RefTeX functions. 1693 ;; Replace AucTeX functions with RefTeX functions.
1148 ;; Which functions are replaced is controlled by the variable 1694 ;; Which functions are replaced is controlled by the variable
1149 ;; `reftex-plug-into-AUCTeX'. 1695 ;; `reftex-plug-into-AUCTeX'.
1150 (let ((flags 1696 (let ((flags
1151 (cond ((eq reftex-plug-into-AUCTeX t) '(t t t t)) 1697 (cond ((eq reftex-plug-into-AUCTeX t) '(t t t t))
1152 ((eq reftex-plug-into-AUCTeX nil) '(nil nil nil nil)) 1698 ((eq reftex-plug-into-AUCTeX nil) '(nil nil nil nil))
1153 (t reftex-plug-into-AUCTeX)))) 1699 (t reftex-plug-into-AUCTeX))))
1154 1700
1155 (and (nth 0 flags) 1701 (and (nth 0 flags)
1156 (boundp 'LaTeX-label-function) 1702 (boundp 'LaTeX-label-function)
1157 (setq LaTeX-label-function 'reftex-label)) 1703 (setq LaTeX-label-function 'reftex-label))
1158 1704
1159 (and (nth 1 flags) 1705 (and (nth 1 flags)
1160 (fboundp 'TeX-arg-label) 1706 (fboundp 'TeX-arg-label)
1161 (fset 'TeX-arg-label 'reftex-arg-label)) 1707 (fset 'TeX-arg-label 'reftex-arg-label))
1162 1708
1163 (and (nth 2 flags) 1709 (and (nth 2 flags)
1164 (fboundp 'TeX-arg-ref) 1710 (fboundp 'TeX-arg-ref)
1165 (fset 'TeX-arg-ref 'reftex-arg-ref)) 1711 (fset 'TeX-arg-ref 'reftex-arg-ref))
1166 1712
1167 (and (nth 3 flags) 1713 (and (nth 3 flags)
1168 (fboundp 'TeX-arg-cite) 1714 (fboundp 'TeX-arg-cite)
1169 (fset 'TeX-arg-cite 'reftex-arg-cite)))) 1715 (fset 'TeX-arg-cite 'reftex-arg-cite))))
1170 1716
1171 1717
1172 (defvar reftex-label-alist-external-add-ons nil 1718 (defvar reftex-label-alist-external-add-ons nil
1173 "List of label alist entries added with reftex-add-to-label-alist.") 1719 "List of label alist entries added with reftex-add-to-label-alist.")
1174 1720
1175 ;;;###autoload
1176 (defun reftex-add-to-label-alist (entry-list) 1721 (defun reftex-add-to-label-alist (entry-list)
1177 "Add label environment descriptions to `reftex-label-alist-external-add-ons'. 1722 "Add label environment descriptions to `reftex-label-alist-external-add-ons'.
1178 The format of ENTRY-LIST is exactly like `reftex-label-alist'. See there 1723 The format of ENTRY-LIST is exactly like `reftex-label-alist'. See there
1179 for details. 1724 for details.
1180 This function makes it possible to support RefTeX from AUCTeX style files. 1725 This function makes it possible to support RefTeX from AUCTeX style files.
1181 The entries in ENTRY-LIST will be processed after the user settings in 1726 The entries in ENTRY-LIST will be processed after the user settings in
1182 `reftex-label-alist', and before the defaults (specified in 1727 `reftex-label-alist', and before the defaults (specified in
1183 `reftex-default-label-alist-entries'). Any changes made to 1728 `reftex-default-label-alist-entries'). Any changes made to
1185 mode reset is done on the next occasion." 1730 mode reset is done on the next occasion."
1186 (let (entry) 1731 (let (entry)
1187 (while entry-list 1732 (while entry-list
1188 (setq entry (car entry-list) 1733 (setq entry (car entry-list)
1189 entry-list (cdr entry-list)) 1734 entry-list (cdr entry-list))
1190 (if (not (member entry reftex-label-alist-external-add-ons)) 1735 (unless (member entry reftex-label-alist-external-add-ons)
1191 (setq reftex-tables-dirty t 1736 (setq reftex-tables-dirty t)
1192 reftex-label-alist-external-add-ons 1737 (push entry reftex-label-alist-external-add-ons)))))
1193 (cons entry reftex-label-alist-external-add-ons))))))
1194 1738
1195 ;;; =========================================================================== 1739 ;;; ===========================================================================
1196 ;;; 1740 ;;;
1197 ;;; Multifile support 1741 ;;; Multifile support
1198 ;;; 1742 ;;;
1203 ;;; document. Each buffer has local variables which point to these symbols. 1747 ;;; document. Each buffer has local variables which point to these symbols.
1204 1748
1205 ;; List of variables which handle the multifile stuff. 1749 ;; List of variables which handle the multifile stuff.
1206 ;; This list is used to tie, untie, and reset these symbols. 1750 ;; This list is used to tie, untie, and reset these symbols.
1207 (defconst reftex-multifile-symbols 1751 (defconst reftex-multifile-symbols
1208 '(reftex-label-numbers-symbol reftex-list-of-labels-symbol 1752 '(reftex-docstruct-symbol))
1209 reftex-bibfile-list-symbol))
1210 1753
1211 ;; Alist connecting master file names with the corresponding lisp symbols. 1754 ;; Alist connecting master file names with the corresponding lisp symbols.
1212 (defvar reftex-master-index-list nil) 1755 (defvar reftex-master-index-list nil)
1213 1756
1214 ;; Last index used for a master file. 1757 ;; Last index used for a master file.
1215 (defvar reftex-multifile-index 0) 1758 (defvar reftex-multifile-index 0)
1216 1759
1217 ;; Alist connecting a master file with all included files.
1218 (defvar reftex-master-include-list nil)
1219
1220 ;; Variable holding the symbol with current value of label postfix.
1221 (defvar reftex-label-numbers-symbol nil )
1222 (make-variable-buffer-local 'reftex-label-numbers-symbol)
1223
1224 ;; Variable holding the symbol with the label list of the document. 1760 ;; Variable holding the symbol with the label list of the document.
1225 ;; Each element of the label list is again a list with the following elements: 1761 (defvar reftex-docstruct-symbol nil)
1226 ;; 0: One character label type indicator. 1762 (make-variable-buffer-local 'reftex-docstruct-symbol)
1227 ;; 1: Short context to put into label menu.
1228 ;; 2: The label.
1229 ;; 3: The name of the file where the label is defined.
1230 (defvar reftex-list-of-labels-symbol nil)
1231 (make-variable-buffer-local 'reftex-list-of-labels-symbol)
1232
1233 ;; Variable holding the symbol with a list of library files for this document.
1234 (defvar reftex-bibfile-list-symbol nil)
1235 (make-variable-buffer-local 'reftex-bibfile-list-symbol)
1236 1763
1237 (defun reftex-next-multifile-index () 1764 (defun reftex-next-multifile-index ()
1238 ;; Return the next free index for multifile symbols. 1765 ;; Return the next free index for multifile symbols.
1239 (setq reftex-multifile-index (1+ reftex-multifile-index))) 1766 (incf reftex-multifile-index))
1240 1767
1241 (defun reftex-tie-multifile-symbols () 1768 (defun reftex-tie-multifile-symbols ()
1242 ;; Tie the buffer-local symbols to globals connected with the master file. 1769 ;; Tie the buffer-local symbols to globals connected with the master file.
1243 ;; If the symbols for the current master file do not exist, they are created. 1770 ;; If the symbols for the current master file do not exist, they are created.
1244 1771
1252 (if index 1779 (if index
1253 ;; symbols do exist 1780 ;; symbols do exist
1254 (setq index (cdr index)) 1781 (setq index (cdr index))
1255 ;; Get a new index and add info to the alist. 1782 ;; Get a new index and add info to the alist.
1256 (setq index (reftex-next-multifile-index) 1783 (setq index (reftex-next-multifile-index)
1257 reftex-master-index-list (cons 1784 newflag t)
1258 (cons master index) 1785 (push (cons master index) reftex-master-index-list))
1259 reftex-master-index-list)
1260 newflag t))
1261 1786
1262 ;; Get/create symbols and tie them. 1787 ;; Get/create symbols and tie them.
1263 (while symlist 1788 (while symlist
1264 (setq symbol (car symlist) 1789 (setq symbol (car symlist)
1265 symlist (cdr symlist) 1790 symlist (cdr symlist)
1328 (t 1853 (t
1329 ;; Something is wrong here. Throw an exception. 1854 ;; Something is wrong here. Throw an exception.
1330 (error "No such master file %s" master))) 1855 (error "No such master file %s" master)))
1331 (expand-file-name master))) 1856 (expand-file-name master)))
1332 1857
1333 (defun reftex-make-master-buffer (master-file mode) 1858 (defun reftex-parse-one ()
1334 "Make a master buffer which contains the MASTER-FILE and all includes. 1859 "Re-parse this file."
1335 This is to prepare a buffer containing the entire document in correct
1336 sequence for parsing. Therefore it will even expand includes which are
1337 commented out.
1338 The function returns the number of input/include files not found."
1339
1340 (interactive "fmaster file: ")
1341 (let ((not-found 0) file file-list tmp (font-lock-maximum-size 1))
1342 (switch-to-buffer "*reftex-master.tex*")
1343 (erase-buffer)
1344 (if (not (eq major-mode mode))
1345 (funcall mode))
1346 ;; First insert the master file.
1347 (if (not (file-exists-p master-file))
1348 (error "No such master file: %s" master-file))
1349 (reftex-insert-buffer-or-file master-file)
1350 (subst-char-in-region (point-min) (point-max) ?\r ?\n t)
1351 (setq file-list (cons master-file file-list))
1352 (goto-char 1)
1353 ;; Remember from which file these lines came.
1354 (put-text-property (point-min) (point-max) 'file
1355 (expand-file-name master-file))
1356 ;; Make the default directory that of the master file.
1357 ;; All input and include stuff works relative to that directory.
1358 (cd (file-name-directory (expand-file-name master-file)))
1359 ;; Now find recursively all include/input statements and expand them.
1360 (while (re-search-forward
1361 "^[ \t]*\\\\\\(include\\|input\\){\\([^}\n]+\\)}" nil t)
1362 (setq file (reftex-no-props (match-string 2)))
1363 (if (not (and (> (length file) 4)
1364 (string= (substring file -4) ".tex")))
1365 (setq file (concat file ".tex")))
1366 (if (file-exists-p file)
1367 (progn
1368 (replace-match
1369 (format "\n%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% START OF %s FILE: %s\n%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% END OF %s FILE: %s\n"
1370 (match-string 1) file
1371 (match-string 1) file))
1372 (beginning-of-line 0)
1373 (narrow-to-region (point) (point))
1374 ;; Insert the file.
1375 (reftex-insert-buffer-or-file file)
1376 (subst-char-in-region (point-min) (point-max) ?\r ?\n t)
1377 (setq file-list (cons (expand-file-name file) file-list))
1378 ;; Remember from which file these lines came.
1379 (put-text-property (point-min) (point-max)
1380 'file (expand-file-name file))
1381 (goto-char (point-min))
1382 (widen))
1383 (message "Input/include file %s not found. Ignored. Continuing..."
1384 file)
1385 (setq not-found (1+ not-found))))
1386 (setq file-list (nreverse file-list))
1387 (while (setq tmp (assoc (car file-list) reftex-master-include-list))
1388 (setq reftex-master-include-list (delq tmp reftex-master-include-list)))
1389 (setq reftex-master-include-list (cons file-list reftex-master-include-list))
1390 not-found))
1391
1392 (defun reftex-insert-buffer-or-file (file)
1393 "If there is a buffer associated with FILE, insert it - otherwise the FILE."
1394 (let ((buffer (reftex-get-buffer-visiting file)))
1395 (if buffer
1396 (let (beg end beg1 end1)
1397 (save-excursion
1398 ;; Make sure we get the whole buffer.
1399 (set-buffer buffer)
1400 (setq beg (point-min) end (point-max))
1401 (widen)
1402 (setq beg1 (point-min) end1 (point-max)))
1403 (insert-buffer-substring buffer beg1 end1)
1404 (save-excursion
1405 (set-buffer buffer)
1406 (narrow-to-region beg end)))
1407 (insert-file-contents file))))
1408
1409
1410 (defun reftex-parse-document (&optional buffer)
1411 "Rescan the document."
1412 (interactive) 1860 (interactive)
1413 (save-window-excursion 1861 (let ((reftex-enable-partial-scans t))
1414 (save-excursion 1862 (reftex-access-scan-info '(4))))
1415 (if buffer 1863
1416 (if (not (bufferp buffer)) 1864 (defun reftex-parse-all ()
1417 (error "No such buffer %s" (buffer-name buffer)) 1865 "Re-parse entire document."
1418 (set-buffer buffer))) 1866 (interactive)
1419 (reftex-access-scan-info t)))) 1867 (reftex-access-scan-info '(16)))
1420 1868
1421 (defun reftex-access-scan-info (&optional rescan) 1869 (defun reftex-all-document-files (&optional relative)
1422 ;; Access the scanning info. When the multifile symbols are not yet tied, 1870 ;; Return a list of all files belonging to the current document.
1423 ;; tie them. When they are have to be created, do a buffer scan to 1871 ;; When RELATIVE is non-nil, give file names relative to directory
1424 ;; fill them. 1872 ;; of master file.
1425 1873 (let* ((all (symbol-value reftex-docstruct-symbol))
1426 ;; If RESCAN is non-nil, enforce document scanning 1874 (master-dir (file-name-directory (reftex-TeX-master-file)))
1427 1875 (re (concat "\\`" (regexp-quote master-dir)))
1428 (catch 'exit 1876 file-list tmp file)
1429 (let ((rescan (or (equal rescan t) (equal rescan '(4))))) 1877 (while (setq tmp (assoc 'bof all))
1430 1878 (setq file (nth 1 tmp)
1431 ;; Reset the mode if we had changes from style hooks. 1879 all (cdr (memq tmp all)))
1432 (and reftex-tables-dirty 1880 (and relative
1433 (reftex-reset-mode)) 1881 (string-match re file)
1434 1882 (setq file (substring file (match-end 0))))
1435 (if (eq reftex-list-of-labels-symbol nil) 1883 (push file file-list))
1436 ;; Symbols are not yet tied: Tie them and see if they are set. 1884 (nreverse file-list)))
1437 (reftex-tie-multifile-symbols))
1438
1439 (if (and (symbol-value reftex-list-of-labels-symbol)
1440 (not rescan))
1441 ;; Lists do already exist and we don't need to rescan.
1442 ;; Return from here.
1443 (throw 'exit t))
1444
1445 ;; We need to rescan
1446 ;; =================
1447
1448 (unwind-protect
1449 (save-window-excursion
1450 (save-excursion
1451
1452 ;; Do the scanning.
1453
1454 (let ((label-list-symbol reftex-list-of-labels-symbol)
1455 (label-numbers-symbol reftex-label-numbers-symbol)
1456 (bibfile-list-symbol reftex-bibfile-list-symbol))
1457
1458 (message "Creating master buffer...")
1459 (reftex-make-master-buffer (reftex-TeX-master-file) major-mode)
1460
1461 (message "Scanning document...")
1462
1463 (reftex-scan-buffer-for-labels
1464 label-numbers-symbol label-list-symbol)
1465
1466 (reftex-scan-buffer-for-bibliography-statement
1467 bibfile-list-symbol)
1468
1469 (message "Scanning document... done"))))
1470
1471 (if (get-buffer "*reftex-master.tex*")
1472 (kill-buffer "*reftex-master.tex*"))))))
1473 1885
1474 (defun reftex-create-tags-file () 1886 (defun reftex-create-tags-file ()
1475 "Create TAGS file by running `etags' on the current document. 1887 "Create TAGS file by running `etags' on the current document.
1476 The TAGS file is also immediately visited with `visit-tags-table'." 1888 The TAGS file is also immediately visited with `visit-tags-table'."
1477 (interactive) 1889 (interactive)
1478 (reftex-access-scan-info current-prefix-arg) 1890 (reftex-access-scan-info current-prefix-arg)
1479 (let* ((master (reftex-TeX-master-file)) 1891 (let* ((master (reftex-TeX-master-file))
1480 (files (assoc master reftex-master-include-list)) 1892 (files (reftex-all-document-files))
1481 (cmd (format "etags %s" (mapconcat 'identity files " ")))) 1893 (cmd (format "etags %s" (mapconcat 'identity files " "))))
1482 (save-excursion 1894 (save-excursion
1483 (set-buffer (reftex-get-buffer-visiting master)) 1895 (set-buffer (reftex-get-buffer-visiting master))
1484 (message "Running etags to create TAGS file...") 1896 (message "Running etags to create TAGS file...")
1485 (shell-command cmd) 1897 (shell-command cmd)
1486 (visit-tags-table "TAGS")))) 1898 (visit-tags-table "TAGS"))))
1495 With prefix arg, force to rescan document. 1907 With prefix arg, force to rescan document.
1496 This works also without an active TAGS table." 1908 This works also without an active TAGS table."
1497 1909
1498 (interactive 1910 (interactive
1499 (list (read-from-minibuffer "Run grep on document (like this): " 1911 (list (read-from-minibuffer "Run grep on document (like this): "
1500 reftex-grep-command nil nil 1912 reftex-grep-command nil nil
1501 'reftex-grep-history))) 1913 'reftex-grep-history)))
1502 (reftex-access-scan-info current-prefix-arg) 1914 (reftex-access-scan-info current-prefix-arg)
1503 (let* ((master (reftex-TeX-master-file)) 1915 (let* ((files (reftex-all-document-files t))
1504 (default-directory (file-name-directory master)) 1916 (cmd (format
1505 (re (format "\\`%s\\(.*\\)" (regexp-quote 1917 "%s %s" grep-cmd
1506 (expand-file-name default-directory)))) 1918 (mapconcat 'identity files " "))))
1507 (files (assoc master reftex-master-include-list))
1508 (cmd (format
1509 "%s %s" grep-cmd
1510 (mapconcat (function (lambda (x)
1511 (if (string-match re x)
1512 (match-string 1 x)
1513 x)))
1514 files " "))))
1515 (grep cmd))) 1919 (grep cmd)))
1516 1920
1517 (defun reftex-search-document (&optional regexp) 1921 (defun reftex-search-document (&optional regexp)
1518 "Regexp search through all files of the current TeX document. 1922 "Regexp search through all files of the current TeX document.
1519 Starts always in the master file. Stops when a match is found. 1923 Starts always in the master file. Stops when a match is found.
1520 To continue searching for next match, use command \\[tags-loop-continue]. 1924 To continue searching for next match, use command \\[tags-loop-continue].
1521 This works also without an active TAGS table." 1925 This works also without an active TAGS table."
1522 (interactive) 1926 (interactive)
1523 (let ((default (reftex-this-word))) 1927 (let ((default (reftex-this-word)))
1524 (if (not regexp) 1928 (unless regexp
1525 (setq regexp (read-string (format "Search regexp in document [%s]: " 1929 (setq regexp (read-string (format "Search regexp in document [%s]: "
1526 default)))) 1930 default))))
1527 (if (string= regexp "") (setq regexp (regexp-quote default))) 1931 (if (string= regexp "") (setq regexp (regexp-quote default)))
1528 1932
1529 (reftex-access-scan-info current-prefix-arg) 1933 (reftex-access-scan-info current-prefix-arg)
1530 (tags-search regexp (list 'assoc (reftex-TeX-master-file) 1934 (tags-search regexp (list 'reftex-all-document-files))))
1531 'reftex-master-include-list))))
1532 1935
1533 (defun reftex-query-replace-document (&optional from to delimited) 1936 (defun reftex-query-replace-document (&optional from to delimited)
1534 "Run a query-replace-regexp of FROM with TO over the entire TeX document. 1937 "Run a query-replace-regexp of FROM with TO over the entire TeX document.
1535 Third arg DELIMITED (prefix arg) means replace only word-delimited matches. 1938 Third arg DELIMITED (prefix arg) means replace only word-delimited matches.
1536 If you exit (\\[keyboard-quit] or ESC), you can resume the query replace 1939 If you exit (\\[keyboard-quit] or ESC), you can resume the query replace
1537 with the command \\[tags-loop-continue]. 1940 with the command \\[tags-loop-continue].
1538 This works also without an active TAGS table." 1941 This works also without an active TAGS table."
1539 (interactive) 1942 (interactive)
1540 (let ((default (reftex-this-word))) 1943 (let ((default (reftex-this-word)))
1541 (if (not from) 1944 (unless from
1542 (progn 1945 (setq from (read-string (format "Replace regexp in document [%s]: "
1543 (setq from (read-string (format "Replace regexp in document [%s]: " 1946 default)))
1544 default))) 1947 (if (string= from "") (setq from (regexp-quote default))))
1545 (if (string= from "") (setq from (regexp-quote default))))) 1948 (unless to
1546 (if (not to) 1949 (setq to (read-string (format "Replace regexp %s with: " from))))
1547 (setq to (read-string (format "Replace regexp %s with: " from))))
1548 (reftex-access-scan-info current-prefix-arg) 1950 (reftex-access-scan-info current-prefix-arg)
1549 (tags-query-replace from to (or delimited current-prefix-arg) 1951 (tags-query-replace from to (or delimited current-prefix-arg)
1550 (list 'assoc (reftex-TeX-master-file) 1952 (list 'reftex-all-document-files))))
1551 'reftex-master-include-list))))
1552 1953
1553 (defun reftex-change-label (&optional from to) 1954 (defun reftex-change-label (&optional from to)
1554 "Query replace FROM with TO in all \\label and \\ref commands. 1955 "Query replace FROM with TO in all \\label and \\ref commands.
1555 Works on the entire multifile document. 1956 Works on the entire multifile document.
1556 If you exit (\\[keyboard-quit] or ESC), you can resume the query replace 1957 If you exit (\\[keyboard-quit] or ESC), you can resume the query replace
1557 with the command \\[tags-loop-continue]. 1958 with the command \\[tags-loop-continue].
1558 This works also without an active TAGS table." 1959 This works also without an active TAGS table."
1559 (interactive) 1960 (interactive)
1560 (let ((default (reftex-this-word "-a-zA-Z0-9_*.:"))) 1961 (let ((default (reftex-this-word "-a-zA-Z0-9_*.:")))
1561 (if (not from) 1962 (unless from
1562 (setq from (read-string (format "Replace label globally [%s]: " 1963 (setq from (read-string (format "Replace label globally [%s]: "
1563 default)))) 1964 default))))
1564 (if (string= from "") (setq from default)) 1965 (if (string= from "") (setq from default))
1565 (if (not to) 1966 (unless to
1566 (setq to (read-string (format "Replace label %s with: " 1967 (setq to (read-string (format "Replace label %s with: "
1567 from)))) 1968 from))))
1568 (reftex-query-replace-document 1969 (reftex-query-replace-document
1569 (concat "\\\\\\(label\\|[a-z]*ref\\){" (regexp-quote from) "}") 1970 (concat "\\\\\\(label\\|[a-z]*ref\\){" (regexp-quote from) "}")
1570 (format "\\\\\\1{%s}" to)))) 1971 (format "\\\\\\1{%s}" to))))
1571 1972
1572 (defun reftex-this-word (&optional class)
1573 ;; Grab the word around point.
1574 (setq class (or class "-a-zA-Z0-9:_/.*;|"))
1575 (save-excursion
1576 (buffer-substring-no-properties
1577 (progn (skip-chars-backward class) (point))
1578 (progn (skip-chars-forward class) (point)))))
1579
1580 ;;; =========================================================================== 1973 ;;; ===========================================================================
1581 ;;; 1974 ;;;
1582 ;;; Functions to create and reference automatic labels. 1975 ;;; Functions to create and reference automatic labels.
1583 1976
1584 ;; The following constants are derived from `reftex-label-alist'. 1977 ;; The following constants are derived from `reftex-label-alist'.
1614 (defvar reftex-last-used-reference (list nil nil nil nil)) 2007 (defvar reftex-last-used-reference (list nil nil nil nil))
1615 2008
1616 ;; The regular expression used to abbreviate words. 2009 ;; The regular expression used to abbreviate words.
1617 (defconst reftex-abbrev-regexp 2010 (defconst reftex-abbrev-regexp
1618 (concat 2011 (concat
1619 "^\\(" 2012 "\\`\\("
1620 (make-string (nth 0 reftex-abbrev-parameters) ?.) 2013 (make-string (nth 0 reftex-abbrev-parameters) ?.)
1621 "[" (nth 2 reftex-abbrev-parameters) "]*" 2014 "[" (nth 2 reftex-abbrev-parameters) "]*"
1622 "\\)" 2015 "\\)"
1623 "[" (nth 3 reftex-abbrev-parameters) "]" 2016 "[" (nth 3 reftex-abbrev-parameters) "]"
1624 (make-string (1- (nth 1 reftex-abbrev-parameters)) ?.))) 2017 (make-string (1- (nth 1 reftex-abbrev-parameters)) ?.)))
1625 2018
1626 ;; Global variables used for communication between functions. 2019 ;; Global variables used for communication between functions.
1627 (defvar reftex-default-context-position nil) 2020 (defvar reftex-default-context-position nil)
1628 (defvar reftex-location-start nil) 2021 (defvar reftex-location-start nil)
1629 (defvar reftex-call-back-to-this-buffer nil) 2022 (defvar reftex-call-back-to-this-buffer nil)
2023 (defvar reftex-active-toc nil)
2024 (defvar reftex-tex-path nil)
2025 (defvar reftex-bib-path nil)
2026
2027 ;; Internal list with index numbers of labels in the selection menu
2028 (defvar reftex-label-index-list)
1630 2029
1631 ;; List of buffers created temporarily for lookup, which should be killed. 2030 ;; List of buffers created temporarily for lookup, which should be killed.
1632 (defvar reftex-buffers-to-kill nil) 2031 (defvar reftex-buffers-to-kill nil)
1633 2032
1634 ;; The regexp used to find section statements. 2033 ;; Regexp to find section statements. Computed from reftex-section-levels.
1635 (defconst reftex-section-regexp "^[ ]*\\\\\\(part\\|chapter\\|section\\|subsection\\|subsubsection\\|paragraph\\|subparagraph\\|subsubparagraph\\)\\*?\\(\\[[^]]*\\]\\)?{") 2034 (defvar reftex-section-regexp nil)
1636 2035 (defvar reftex-section-or-include-regexp nil)
1637 ;; LaTeX section commands and level numbers 2036 (defvar reftex-everything-regexp nil)
1638 (defconst reftex-section-levels 2037 (defvar reftex-find-label-regexp-format nil)
1639 '( 2038 (defvar reftex-find-label-regexp-format2 nil)
1640 ("part" . 0) 2039
1641 ("chapter" . 1) 2040 ;; The parser functions ----------------------------------
1642 ("section" . 2) 2041
1643 ("subsection" . 3) 2042 (defvar reftex-memory nil
1644 ("subsubsection" . 4) 2043 "Memorizes old variable values to indicate changes in these variables.")
1645 ("paragraph" . 5) 2044
1646 ("subparagraph" . 6) 2045 (defun reftex-access-scan-info (&optional rescan file)
1647 ("subsubparagraph" . 7) 2046 ;; Access the scanning info. When the multifile symbols are not yet tied,
1648 )) 2047 ;; tie them. When they are empty or RESCAN is non-nil, scan the document.
2048
2049 ;; Reset the mode if we had changes to important variables.
2050 (when (or reftex-tables-dirty
2051 (not (eq reftex-label-alist (nth 0 reftex-memory)))
2052 (not (eq reftex-label-alist-external-add-ons
2053 (nth 1 reftex-memory)))
2054 (not (eq reftex-default-label-alist-entries
2055 (nth 2 reftex-memory))))
2056 (reftex-reset-mode))
2057
2058 (if (eq reftex-docstruct-symbol nil)
2059 ;; Symbols are not yet tied: Tie them.
2060 (reftex-tie-multifile-symbols))
2061
2062 (if (and (null (symbol-value reftex-docstruct-symbol))
2063 reftex-save-parse-info)
2064 ;; Try to read the stuff from a file
2065 (reftex-access-parse-file 'read))
2066
2067 (cond
2068 ((not (symbol-value reftex-docstruct-symbol))
2069 (reftex-do-parse 1 file))
2070 ((member rescan '(t 1 (4) (16)))
2071 (reftex-do-parse rescan file))))
2072
2073 (defun reftex-do-parse (rescan &optional file)
2074 ;; Access the scanning info. When the multifile symbols are not yet tied,
2075 ;; tie them. When they are have to be created, do a buffer scan to
2076 ;; fill them.
2077
2078 ;; If RESCAN is non-nil, enforce document scanning
2079
2080 ;; Normalize the rescan argument
2081 (setq rescan (cond ((eq rescan t) t)
2082 ((eq rescan 1) 1)
2083 ((equal rescan '(4)) t)
2084 ((equal rescan '(16)) 1)
2085 (t 1)))
2086
2087 ;; Partial scans only when allowed
2088 (unless reftex-enable-partial-scans
2089 (setq rescan 1))
2090
2091 ;; Do the scanning.
2092
2093 (let* ((old-list (symbol-value reftex-docstruct-symbol))
2094 (master (reftex-TeX-master-file))
2095 (master-dir (file-name-as-directory (file-name-directory master)))
2096 (file (or file (buffer-file-name)))
2097 from-file
2098 docstruct tmp)
2099
2100 ;; Make sure replacement is really an option here
2101 (when (and (eq rescan t)
2102 (not (and (member (list 'bof file) old-list)
2103 (member (list 'eof file) old-list))))
2104 (message "Scanning whole document (no file section %s)" file)
2105 (setq rescan 1))
2106 (when (string= file master)
2107 (message "Scanning whole document (%s is master)" file)
2108 (setq rescan 1))
2109
2110 ;; From which file do we start?
2111 (setq from-file
2112 (cond ((eq rescan t) (or file master))
2113 ((eq rescan 1) master)
2114 (t (error "horrible!!"))))
2115
2116 ;; Find active toc entry and initialize section-numbers
2117 (setq reftex-active-toc
2118 (reftex-last-assoc-before-elt
2119 'toc (list 'bof from-file) old-list))
2120 (reftex-init-section-numbers reftex-active-toc)
2121
2122 (if (eq rescan 1)
2123 (message "Scanning entire document...")
2124 (message "Scanning document from %s..." from-file))
2125
2126 (save-window-excursion
2127 (save-excursion
2128 (unwind-protect
2129 (setq docstruct
2130 (reftex-parse-from-file
2131 from-file docstruct master-dir))
2132 (reftex-kill-temporary-buffers))))
2133
2134 (message "Scanning document... done")
2135
2136 ;; Turn the list around.
2137 (setq docstruct (nreverse docstruct))
2138
2139 ;; Set or insert
2140 (setq docstruct (reftex-replace-label-list-segment
2141 old-list docstruct (eq rescan 1)))
2142
2143 ;; Add all missing information
2144 (unless (assq 'label-numbers docstruct)
2145 (push (cons 'label-numbers nil) docstruct))
2146 (unless (assq 'master-dir docstruct)
2147 (push (cons 'master-dir master-dir) docstruct))
2148 (let* ((bof1 (memq (assq 'bof docstruct) docstruct))
2149 (bof2 (assq 'bof (cdr bof1)))
2150 (is-multi (not (not (and bof1 bof2))))
2151 (entry (or (assq 'is-multi docstruct)
2152 (car (push (list 'is-multi is-multi) docstruct)))))
2153 (setcdr entry (cons is-multi nil)))
2154 (unless (assq 'xr docstruct)
2155 (let* ((allxr (reftex-all-assq 'xr-doc docstruct))
2156 (alist (mapcar
2157 '(lambda (x)
2158 (if (setq tmp (reftex-find-tex-file (nth 2 x)
2159 master-dir))
2160 (cons (nth 1 x) tmp)
2161 (message "Can't find external document %s"
2162 (nth 2 x))
2163 nil))
2164 allxr))
2165 (alist (delete nil alist))
2166 (allprefix (delete nil (mapcar 'car alist)))
2167 (regexp (concat "\\`\\(" (mapconcat 'identity allprefix "\\|")
2168 "\\)")))
2169 (push (list 'xr alist regexp) docstruct)))
2170
2171 (set reftex-docstruct-symbol docstruct)
2172
2173 ;; Save the parsing informtion into a file?
2174 (if reftex-save-parse-info
2175 (reftex-access-parse-file 'write))))
2176
2177 (defun reftex-is-multi ()
2178 ;; Tell if this is a multifile document. When not sure, say yes.
2179 (let ((entry (assq 'is-multi (symbol-value reftex-docstruct-symbol))))
2180 (if entry
2181 (nth 1 entry)
2182 t)))
2183
2184 (defun reftex-parse-from-file (file docstruct master-dir)
2185 ;; Scan the buffer for labels and save them in a list.
2186 (let ((regexp reftex-everything-regexp)
2187 (bound 0)
2188 file-found tmp
2189 (level 1)
2190 (highest-level 100)
2191 toc-entry next-buf)
2192
2193 (catch 'exit
2194 (setq file-found (reftex-find-tex-file file master-dir))
2195 (unless file-found
2196 (push (list 'file-error file) docstruct)
2197 (throw 'exit nil))
2198
2199 (save-excursion
2200
2201 (message "Scanning file %s" file)
2202 (set-buffer
2203 (setq next-buf
2204 (reftex-get-file-buffer-force
2205 file-found
2206 (not (eq t reftex-keep-temporary-buffers)))))
2207
2208 ;; Begin of file mark
2209 (setq file (buffer-file-name))
2210 (push (list 'bof file) docstruct)
2211
2212 (save-excursion
2213 (save-restriction
2214 (widen)
2215 (goto-char 1)
2216
2217 (while (re-search-forward regexp nil t)
2218
2219 (cond
2220
2221 ((match-end 1)
2222 ;; It is a label
2223 (push (reftex-label-info (reftex-match-string 1) file bound)
2224 docstruct))
2225
2226 ((match-end 3)
2227 ;; It is a section
2228 (setq bound (point))
2229
2230 ;; Insert in List
2231 (setq toc-entry (reftex-section-info file))
2232 (setq level (nth 5 toc-entry))
2233 (setq highest-level (min highest-level level))
2234 (if (= level highest-level)
2235 (message
2236 "Scanning %s %s ..."
2237 (car (nth level reftex-section-levels))
2238 (nth 6 toc-entry)))
2239
2240 (push toc-entry docstruct)
2241 (setq reftex-active-toc toc-entry))
2242
2243 ((match-end 7)
2244 ;; It's an include or input
2245 (setq docstruct
2246 (reftex-parse-from-file
2247 (reftex-match-string 7)
2248 docstruct master-dir)))
2249
2250 ((match-end 8)
2251 ;; A macro with label
2252 (save-excursion
2253 (let* ((mac (reftex-match-string 8))
2254 (label (progn (goto-char (match-end 8))
2255 (save-match-data
2256 (reftex-no-props
2257 (reftex-nth-arg-wrapper
2258 mac)))))
2259 (entry (progn (goto-char (match-end 0))
2260 (reftex-label-info
2261 label file bound mac))))
2262 (push entry docstruct))))
2263 (t (error "This should not happen (reftex-parse-from-file)")))
2264 )
2265
2266
2267 ;; Find bibliography statement
2268 (when (setq tmp (reftex-locate-bibliography-files master-dir))
2269 (push (cons 'bib tmp) docstruct))
2270
2271 ;; Find external document specifications
2272 (goto-char 1)
2273 (while (re-search-forward "[\n\r][ \t]*\\\\externaldocument\\(\\[\\([^]]*\\)\\]\\)?{\\([^}]+\\)}" nil t)
2274 (push (list 'xr-doc (reftex-match-string 2)
2275 (reftex-match-string 3))
2276 docstruct))
2277
2278 ;; End of file mark
2279 (push (list 'eof file) docstruct))))
2280
2281 ;; Kill the scanned buffer
2282 (reftex-kill-temporary-buffers next-buf))
2283
2284 ;; Return the list
2285 docstruct))
2286
2287 (defun reftex-locate-bibliography-files (master-dir)
2288 ;; Scan buffer for bibliography macro and return file list.
2289 (let (file-list)
2290 (save-excursion
2291 (goto-char (point-min))
2292 (if (re-search-forward
2293 "\\(\\`\\|[\n\r]\\)[ \t]*\\\\bibliography{[ \t]*\\([^}]+\\)" nil t)
2294 (setq file-list
2295 (mapcar '(lambda (x) (concat x ".bib"))
2296 (reftex-delete-list
2297 reftex-bibfile-ignore-list
2298 (split-string
2299 (reftex-match-string 2)
2300 "[ \t\n\r]*,[ \t\n\r]*")))))
2301 (delete nil
2302 (mapcar
2303 (function
2304 (lambda (file) (reftex-find-bib-file file master-dir)))
2305 file-list)))))
2306
2307 (defun reftex-last-assoc-before-elt (key elt list)
2308 ;; Find the last association of KEY in LIST before or at ELT
2309 ;; ELT is found in LIST with equal, not eq.
2310 ;; Returns nil when either KEY or elt are not found in LIST.
2311 ;; On success, returns the association.
2312 (let* ((elt (car (member elt list))) ass last-ass)
2313
2314 (while (and (setq ass (assoc key list))
2315 (setq list (memq ass list))
2316 (memq elt list))
2317 (setq last-ass ass
2318 list (cdr list)))
2319 last-ass))
2320
2321 (defun reftex-replace-label-list-segment (old insert &optional entirely)
2322 ;; Replace the segment in OLD which corresponds to INSERT.
2323 ;; Works with side effects, directly changes old.
2324 ;; If entirely is t, just return INSERT.
2325 ;; This function also makes sure the old toc markers do not point anywhere.
2326
2327 (cond
2328 (entirely
2329 (reftex-silence-toc-markers old (length old))
2330 insert)
2331 (t (let* ((new old)
2332 (file (nth 1 (car insert)))
2333 (eof-list (member (list 'eof file) old))
2334 (bof-list (member (list 'bof file) old))
2335 n)
2336 (if (not (and bof-list eof-list))
2337 (error "Cannot splice")
2338 ;; Splice
2339 (reftex-silence-toc-markers bof-list (- (length bof-list)
2340 (length eof-list)))
2341 (setq n (- (length old) (length bof-list)))
2342 (setcdr (nthcdr n new) (cdr insert))
2343 (setcdr (nthcdr (1- (length new)) new) (cdr eof-list)))
2344 new))))
2345
2346 (defun reftex-silence-toc-markers (list n)
2347 ;; Set all markers in list to nil
2348 (while (and list (> (decf n) -1))
2349 (and (eq (car (car list)) 'toc)
2350 (markerp (nth 4 (car list)))
2351 (set-marker (nth 4 (car list)) nil))
2352 (pop list)))
2353
2354 (defun reftex-access-parse-file (action)
2355 (let* ((list (symbol-value reftex-docstruct-symbol))
2356 (master (reftex-TeX-master-file))
2357 (enable-local-variables nil)
2358 (file (if (string-match "\\.[a-zA-Z]+\\'" master)
2359 (concat (substring master 0 (match-beginning 0)) ".rel")
2360 (concat master ".rel"))))
2361 (cond
2362 ((eq action 'readable)
2363 (file-readable-p file))
2364 ((eq action 'restore)
2365 (if (eq reftex-docstruct-symbol nil)
2366 ;; Symbols are not yet tied: Tie them.
2367 (reftex-tie-multifile-symbols))
2368 (if (file-exists-p file)
2369 ;; load the file and return t for success
2370 (progn (load-file file) t)
2371 ;; return nil for failure
2372 nil))
2373 ((eq action 'read)
2374 (if (file-exists-p file)
2375 ;; load the file and return t for success
2376 (progn (load-file file) t)
2377 ;; return nil for failure
2378 nil))
2379 (t
2380 (save-excursion
2381 (if (file-writable-p file)
2382 (progn
2383 (message "Writing parse file %s" (abbreviate-file-name file))
2384 (find-file file)
2385 (erase-buffer)
2386 (insert (format ";; RefTeX parse info file\n"))
2387 (insert (format ";; File: %s\n" master))
2388 (insert (format ";; Date: %s\n"
2389 (format-time-string "%D %T"
2390 (current-time))))
2391 (insert (format ";; User: %s (%s)\n\n"
2392 (user-login-name) (user-full-name)))
2393 (insert "(set reftex-docstruct-symbol '(\n\n")
2394 (let ((standard-output (current-buffer)))
2395 (mapcar
2396 (function
2397 (lambda (x)
2398 (cond ((eq (car x) 'toc)
2399 ;; A toc entry. Do not save the marker.
2400 ;; Save the markers position at position 8
2401 (print (list 'toc "toc" (nth 2 x) (nth 3 x)
2402 nil (nth 5 x) (nth 6 x) (nth 7 x)
2403 (or (and (markerp (nth 4 x))
2404 (marker-position (nth 4 x)))
2405 (nth 8 x)))))
2406 (t (print x)))))
2407 list))
2408 (insert "))")
2409 (save-buffer 0)
2410 (kill-buffer (current-buffer)))
2411 (error "Cannot write to file %s" file)))
2412 t))))
2413
2414 ;; Creating labels --------------
1649 2415
1650 (defun reftex-label (&optional environment no-insert) 2416 (defun reftex-label (&optional environment no-insert)
1651 "Insert a unique label. Return the label. 2417 "Insert a unique label. Return the label.
1652 If ENVIRONMENT is given, don't bother to find out yourself. 2418 If ENVIRONMENT is given, don't bother to find out yourself.
1653 If NO-INSERT is non-nil, do not insert label into buffer. 2419 If NO-INSERT is non-nil, do not insert label into buffer.
1662 2428
1663 ;; Find out what kind of environment this is and abort if necessary. 2429 ;; Find out what kind of environment this is and abort if necessary.
1664 (if (or (not environment) 2430 (if (or (not environment)
1665 (not (assoc environment reftex-env-or-mac-alist))) 2431 (not (assoc environment reftex-env-or-mac-alist)))
1666 (setq environment (reftex-label-location))) 2432 (setq environment (reftex-label-location)))
1667 (if (not environment) 2433 (unless environment
1668 (error "Can't figure out what kind of label should be inserted")) 2434 (error "Can't figure out what kind of label should be inserted"))
1669 2435
1670 ;; Ok, go ahead. 2436 ;; Ok, go ahead.
1671 (let (label num typekey prefix entry cell lab valid default force-prompt) 2437 (let* ((entry (assoc environment reftex-env-or-mac-alist))
1672 (setq typekey (nth 1 (assoc environment 2438 (typekey (nth 1 entry))
1673 reftex-env-or-mac-alist))) 2439 (format (nth 3 entry))
2440 label prefix valid default force-prompt)
2441 (when (and (eq (string-to-char environment) ?\\)
2442 (nth 4 entry)
2443 (memq (preceding-char) '(?\[ ?\{)))
2444 (setq format "%s"))
2445
1674 (setq prefix (or (cdr (assoc typekey reftex-typekey-to-prefix-alist)) 2446 (setq prefix (or (cdr (assoc typekey reftex-typekey-to-prefix-alist))
1675 (concat typekey "-"))) 2447 (concat typekey "-")))
2448 ;; Replace any escapes in the prefix
2449 (setq prefix (reftex-replace-prefix-escapes prefix))
1676 2450
1677 ;; Make a default label. 2451 ;; Make a default label.
1678 (cond 2452 (cond
1679 2453
1680 ((reftex-typekey-check typekey (nth 0 reftex-insert-label-flags)) 2454 ((reftex-typekey-check typekey (nth 0 reftex-insert-label-flags))
1681 ;; Derive a label from context. 2455 ;; Derive a label from context.
1682 (setq default (nth 2 (reftex-label-info " "))) 2456 (setq reftex-active-toc (reftex-last-assoc-before-elt
2457 'toc (car (reftex-where-am-I))
2458 (symbol-value reftex-docstruct-symbol)))
2459 (setq default (reftex-no-props
2460 (nth 2 (reftex-label-info " " nil nil t))))
1683 ;; Catch the cases where the is actually no context available. 2461 ;; Catch the cases where the is actually no context available.
1684 (if (or (string-match "NO MATCH FOR CONTEXT REGEXP" default) 2462 (if (or (string-match "NO MATCH FOR CONTEXT REGEXP" default)
1685 (string-match "ILLEGAL VALUE OF PARSE" default) 2463 (string-match "ILLEGAL VALUE OF PARSE" default)
1686 (string-match "SECTION HEADING NOT FOUND" default) 2464 (string-match "SECTION HEADING NOT FOUND" default)
1687 (string-match "HOOK ERROR" default) 2465 (string-match "HOOK ERROR" default)
1689 (setq default prefix 2467 (setq default prefix
1690 force-prompt t) ; need to prompt 2468 force-prompt t) ; need to prompt
1691 (setq default (concat prefix (reftex-string-to-label default))) 2469 (setq default (concat prefix (reftex-string-to-label default)))
1692 2470
1693 ;; Make it unique. 2471 ;; Make it unique.
1694 (setq label default) 2472 (setq default (reftex-uniquify-label default nil "-"))))
1695 (setq num 1)
1696 (while (assoc label (symbol-value reftex-list-of-labels-symbol))
1697 (setq label (concat default "-" (setq num (1+ num)))))
1698 (setq default label)))
1699 2473
1700 ((reftex-typekey-check typekey (nth 1 reftex-insert-label-flags)) ; prompt 2474 ((reftex-typekey-check typekey (nth 1 reftex-insert-label-flags)) ; prompt
1701 ;; Minimal default: the user will be prompted. 2475 ;; Minimal default: the user will be prompted.
1702 (setq default prefix)) 2476 (setq default prefix))
1703 2477
1704 (t 2478 (t
1705 ;; Make an automatic label. 2479 ;; Make an automatic label.
1706 (while (assoc 2480 (setq default (reftex-uniquify-label prefix t))))
1707 (setq default (concat prefix (reftex-next-label-number typekey)))
1708 (symbol-value reftex-list-of-labels-symbol)))))
1709 2481
1710 ;; Should we ask the user? 2482 ;; Should we ask the user?
1711 (if (or (reftex-typekey-check typekey 2483 (if (or (reftex-typekey-check typekey
1712 (nth 1 reftex-insert-label-flags)) ; prompt 2484 (nth 1 reftex-insert-label-flags)) ; prompt
1713 force-prompt) 2485 force-prompt)
1714 2486
1715 (while (not valid) 2487 (while (not valid)
1716 ;; iterate until we get a legal label 2488 ;; iterate until we get a legal label
1717 2489
1718 (setq label (read-string "Label: " default)) 2490 (setq label (read-string
2491 (if (string= format "%s") "Naked Label: " "Label: ")
2492 default))
1719 2493
1720 ;; Lets make sure that this is a legal label 2494 ;; Lets make sure that this is a legal label
1721 (cond 2495 (cond
1722 2496
1723 ;; Test if label contains strange characters 2497 ;; Test if label contains strange characters
1726 (ding) 2500 (ding)
1727 (sit-for 2)) 2501 (sit-for 2))
1728 2502
1729 ;; Look it up in the label list 2503 ;; Look it up in the label list
1730 ((setq entry (assoc label 2504 ((setq entry (assoc label
1731 (symbol-value reftex-list-of-labels-symbol))) 2505 (symbol-value reftex-docstruct-symbol)))
1732 (message "Label \"%s\" exists in file %s" label (nth 3 entry)) 2506 (message "Label \"%s\" exists in file %s" label (nth 3 entry))
1733 (ding) 2507 (ding)
1734 (sit-for 2)) 2508 (sit-for 2))
1735 2509
1736 ;; Label is ok 2510 ;; Label is ok
1737 (t 2511 (t
1738 (setq valid t)))) 2512 (setq valid t))))
1739 (setq label default)) 2513 (setq label default))
1740 2514
1741 ;; Insert the label
1742 (if (not no-insert)
1743 (insert "\\label{" label "}"))
1744
1745 ;; Insert the label into the label list 2515 ;; Insert the label into the label list
1746 (if (symbol-value reftex-list-of-labels-symbol) 2516 (let* ((here-I-am-info (reftex-where-am-I))
1747 (let ((cnt 0) 2517 (here-I-am (car here-I-am-info))
1748 (pos (point)) 2518 (note (if (cdr here-I-am-info)
1749 (all (symbol-value reftex-list-of-labels-symbol)) 2519 ""
1750 (look-for nil) 2520 "POSITION UNCERTAIN. RESCAN TO FIX."))
1751 (note nil) 2521 (file (buffer-file-name))
1752 (text nil) 2522 (text nil)
1753 (file (buffer-file-name))) 2523 (tail (memq here-I-am (symbol-value reftex-docstruct-symbol))))
1754 2524
1755 ;; find the previous label in order to know where to insert new label 2525 (if tail
1756 ;; into label list 2526 (setcdr tail (cons (list label typekey text file note)
1757 (save-excursion 2527 (cdr tail)))))
1758 (if (re-search-backward "\\\\label{\\([^}]+\\)}" nil 1 2) 2528
1759 (setq look-for (reftex-no-props (match-string 1)))) 2529 ;; Insert the label into the buffer
1760 (if (or (re-search-forward 2530 (unless no-insert
1761 "\\\\\\(include\\|input\\){[^}\n]+}" pos t) 2531 (insert (format format label)))
1762 (re-search-forward reftex-section-regexp pos t) 2532
1763 (null look-for))
1764 (setq note "POSITION UNCERTAIN. RESCAN TO FIX.")))
1765 (if (not look-for)
1766 (set reftex-list-of-labels-symbol
1767 (cons (list label typekey text file note)
1768 (symbol-value reftex-list-of-labels-symbol)))
1769 (while all
1770 (setq cell (car all)
1771 all (cdr all)
1772 cnt (1+ cnt)
1773 lab (nth 0 cell))
1774 (if (string= lab look-for)
1775 (progn
1776 (setcdr
1777 (nthcdr (1- cnt)
1778 (symbol-value reftex-list-of-labels-symbol))
1779 (cons (list label typekey text file note)
1780 (nthcdr
1781 cnt (symbol-value reftex-list-of-labels-symbol))))
1782 ;; to end the loop, set all to nil
1783 (setq all nil)))))))
1784 ;; return value of the function is the label 2533 ;; return value of the function is the label
1785 label)) 2534 label))
1786 2535
1787 (defun reftex-string-to-label (string) 2536 (defun reftex-string-to-label (string)
1788 ;; Convert a string (a sentence) to a label. 2537 ;; Convert a string (a sentence) to a label.
1789 ;; 2538 ;;
1790 ;; Uses reftex-derive-label-parameters and reftex-abbrev-parameters 2539 ;; Uses reftex-derive-label-parameters and reftex-abbrev-parameters
1791 ;; 2540 ;;
1792 2541
1793 (let* ((words0 (reftex-split "[- \t\n\r]+" 2542 (let* ((words0 (split-string string "[- \t\n\r]+"))
1794 (reftex-no-props string)))
1795 (ignore-words (nth 5 reftex-derive-label-parameters)) 2543 (ignore-words (nth 5 reftex-derive-label-parameters))
1796 words word) 2544 words word)
1797 2545
1798 ;; remove words from the ignore list or with funny characters 2546 ;; remove words from the ignore list or with funny characters
1799 (while words0 2547 (while (setq word (pop words0))
1800 (setq word (car words0) words0 (cdr words0))
1801 (cond 2548 (cond
1802 ((member (downcase word) ignore-words)) 2549 ((member (downcase word) ignore-words))
1803 ((string-match reftex-label-illegal-re word) 2550 ((string-match reftex-label-illegal-re word)
1804 (if (nth 2 reftex-derive-label-parameters) 2551 (when (nth 2 reftex-derive-label-parameters)
1805 (progn 2552 (while (string-match reftex-label-illegal-re word)
1806 (while (string-match reftex-label-illegal-re word) 2553 (setq word (replace-match "" nil nil word)))
1807 (setq word (replace-match "" nil nil word))) 2554 (push word words)))
1808 (setq words (cons word words)))))
1809 (t 2555 (t
1810 (setq words (cons word words))))) 2556 (push word words))))
1811 (setq words (nreverse words)) 2557 (setq words (nreverse words))
1812 2558
1813 ;; restrict number of words 2559 ;; restrict number of words
1814 (if (> (length words) (nth 0 reftex-derive-label-parameters)) 2560 (if (> (length words) (nth 0 reftex-derive-label-parameters))
1815 (setcdr (nthcdr (1- (nth 0 reftex-derive-label-parameters)) words) nil)) 2561 (setcdr (nthcdr (1- (nth 0 reftex-derive-label-parameters)) words) nil))
1836 (if (> (length string) (nth 1 reftex-derive-label-parameters)) 2582 (if (> (length string) (nth 1 reftex-derive-label-parameters))
1837 (substring string 0 (nth 1 reftex-derive-label-parameters)) 2583 (substring string 0 (nth 1 reftex-derive-label-parameters))
1838 string)) 2584 string))
1839 2585
1840 ;; Delete the final punctuation, if any 2586 ;; Delete the final punctuation, if any
1841 (if (string-match "[^a-zA-Z0-9]+$" string) 2587 (if (string-match "[^a-zA-Z0-9]+\\'" string)
1842 (setq string (replace-match "" nil nil string))) 2588 (setq string (replace-match "" nil nil string)))
1843 string)) 2589 string))
2590
2591 (defun reftex-replace-prefix-escapes (prefix)
2592 ;; Replace %escapes in a label prefix
2593 (save-match-data
2594 (let (letter (num 0) replace)
2595 (while (string-match "\\%\\([a-zA-Z]\\)" prefix num)
2596 (setq letter (match-string 1 prefix))
2597 (setq replace
2598 (cond
2599 ((equal letter "f")
2600 (file-name-sans-extension
2601 (file-name-nondirectory (buffer-file-name))))
2602 ((equal letter "F")
2603 (let ((masterdir (file-name-directory (reftex-TeX-master-file)))
2604 (file (file-name-sans-extension (buffer-file-name))))
2605 (if (string-match (concat "\\`" (regexp-quote masterdir))
2606 file)
2607 (substring file (length masterdir))
2608 file)))
2609 ((equal letter "u")
2610 (or (user-login-name) ""))
2611 (t "")))
2612 (setq num (1- (+ (match-beginning 1) (length replace)))
2613 prefix (replace-match replace nil nil prefix)))
2614 prefix)))
1844 2615
1845 (defun reftex-label-location (&optional bound) 2616 (defun reftex-label-location (&optional bound)
1846 ;; Return the environment or macro which determines the label type at point. 2617 ;; Return the environment or macro which determines the label type at point.
1847 ;; If optional BOUND is an integer, limit backward searches to that point. 2618 ;; If optional BOUND is an integer, limit backward searches to that point.
1848 2619
1850 (loc2 (reftex-what-environment reftex-label-env-list bound)) 2621 (loc2 (reftex-what-environment reftex-label-env-list bound))
1851 (p1 (or (cdr loc1) 0)) 2622 (p1 (or (cdr loc1) 0))
1852 (p2 (or (cdr loc2) 0))) 2623 (p2 (or (cdr loc2) 0)))
1853 2624
1854 (setq reftex-location-start (max p1 p2)) 2625 (setq reftex-location-start (max p1 p2))
1855 (if (> p1 p2) 2626 (if (>= p1 p2)
1856 (progn 2627 (progn
1857 (setq reftex-default-context-position p1) 2628 (setq reftex-default-context-position (+ p1 (length (car loc1))))
1858 (car loc1)) 2629 (or (car loc1) "section"))
1859 (setq reftex-default-context-position 2630 (setq reftex-default-context-position (+ p2 8 (length (car loc2))))
1860 (+ p2 8 (length (car loc2))))
1861 (or (car loc2) "section")))) 2631 (or (car loc2) "section"))))
1862 2632
1863 2633 (defun reftex-uniquify-label (label &optional force separator)
1864 (defun reftex-next-label-number (type) 2634 ;; Make label unique by appending a number.
1865 ;; Increment value of automatic labels in current buffer. Return new value. 2635 ;; Optional FORCE means, force appending a number, even if label is unique.
2636 ;; Optional SEPARATOR is a string to stick between label and number.
1866 2637
1867 ;; Ensure access to scanning info 2638 ;; Ensure access to scanning info
1868 (reftex-access-scan-info) 2639 (reftex-access-scan-info)
1869 2640
1870 (let ((n (cdr (assoc type (symbol-value reftex-label-numbers-symbol))))) 2641 (cond
1871 (if (not (integerp n)) 2642 ((and (not force)
1872 ;; oops - type not known - make one here 2643 (not (assoc label (symbol-value reftex-docstruct-symbol))))
1873 (progn 2644 label)
1874 (set reftex-label-numbers-symbol 2645 (t
1875 (cons (cons type 0) 2646 (let* ((label-numbers (assq 'label-numbers
1876 (symbol-value reftex-label-numbers-symbol))) 2647 (symbol-value reftex-docstruct-symbol)))
1877 (setq n 0))) 2648 (label-numbers-alist (cdr label-numbers))
1878 (setq n (1+ n)) 2649 (cell (or (assoc label label-numbers-alist)
1879 (setcdr (assoc type (symbol-value reftex-label-numbers-symbol)) n) 2650 (car (setcdr label-numbers
1880 n)) 2651 (cons (cons label 0)
2652 label-numbers-alist)))))
2653 (num (1+ (cdr cell)))
2654 (sep (or separator "")))
2655 (while (assoc (concat label sep (int-to-string num))
2656 (symbol-value reftex-docstruct-symbol))
2657 (incf num))
2658 (setcdr cell num)
2659 (concat label sep (int-to-string num))))))
1881 2660
1882 ;; Help string for the reference label menu 2661 ;; Help string for the reference label menu
1883 (defconst reftex-reference-label-help 2662 (defconst reftex-select-label-prompt
2663 "Select: [n]ext [p]revious [r]escan [ ]context e[x]tern [q]uit RET [?]HELP+more")
2664
2665 (defconst reftex-select-label-help
1884 " AVAILABLE KEYS IN REFERENCE LABEL MENU 2666 " AVAILABLE KEYS IN REFERENCE LABEL MENU
1885 ====================================== 2667 --------------------------------------
1886 n / p Go to next/previous label (Cursor motion works as well) 2668 n / p Go to next/previous label (Cursor motion works as well)
1887 r / s Rescan document for labels / Switch label type 2669 C-s / C-r Search forward/backward. Use repeated C-s/C-r as in isearch.
1888 t / # Toggle table of contents / Toggle counter mode 2670 r / s Reparse document / Switch label type
1889 c Toggle display of short context 2671 x Switch to label menu of external document (with LaTeX package `xr')
1890 SPACE Show full context for current label in other window 2672 t i c # % Toggle: [i]ncl. file borders, [t]able of contents, [c]ontext
1891 f Toggle follow mode: other window will follow context 2673 [#] label counters, [%] labels in comments
1892 a / q Use last referenced label / Quit without accepting label 2674 SPC Show full context for current label in other window
1893 ? / C-r Display this help message / Recursive Edit into other window 2675 f Toggle follow mode: other window will follow context
1894 RETURN Accept current label") 2676 l / q Reuse last referenced label / Quit without accepting label
2677 e Recursive Edit into other window
2678 RET Accept current label")
1895 2679
1896 (defun reftex-reference (&optional type no-insert) 2680 (defun reftex-reference (&optional type no-insert)
1897 "Make a LaTeX reference. Look only for labels of a certain TYPE. 2681 "Make a LaTeX reference. Look only for labels of a certain TYPE.
1898 With prefix arg, force to rescan buffer for labels. This should only be 2682 With prefix arg, force to rescan buffer for labels. This should only be
1899 necessary if you have recently entered labels yourself without using 2683 necessary if you have recently entered labels yourself without using
1909 (reftex-check-recursive-edit) 2693 (reftex-check-recursive-edit)
1910 2694
1911 ;; Ensure access to scanning info and rescan buffer if prefix are is '(4) 2695 ;; Ensure access to scanning info and rescan buffer if prefix are is '(4)
1912 (reftex-access-scan-info current-prefix-arg) 2696 (reftex-access-scan-info current-prefix-arg)
1913 2697
1914 (if (not type) 2698 (unless type
1915 ;; guess type from context 2699 ;; guess type from context
1916 (if (and reftex-guess-label-type 2700 (if (and reftex-guess-label-type
1917 (not (= 16 (prefix-numeric-value current-prefix-arg))) 2701 (setq type (assoc (downcase (reftex-word-before-point))
1918 (setq type (assoc (downcase (reftex-word-before-point)) 2702 reftex-words-to-typekey-alist)))
1919 reftex-words-to-typekey-alist))) 2703 (setq type (cdr type))
1920 (setq type (cdr type)) 2704 (setq type (reftex-query-label-type))))
1921 (setq type (reftex-query-label-type))))
1922 2705
1923 (let (label pair 2706 (let (label pair
1924 (form (or (cdr (assoc type reftex-typekey-to-format-alist)) 2707 (form (or (cdr (assoc type reftex-typekey-to-format-alist))
1925 "\\ref{%s}"))) 2708 "\\ref{%s}")))
1926 2709
1941 (message "")) 2724 (message ""))
1942 (message "Quit")) 2725 (message "Quit"))
1943 ;; return the label 2726 ;; return the label
1944 label)) 2727 label))
1945 2728
1946 (defun reftex-goto-label (&optional arg)
1947 "Go to a LaTeX label. With prefix ARG, go to label in another window."
1948 (interactive "P")
1949 (let (type label file pair)
1950 (if (not type)
1951 (setq type (reftex-query-label-type)))
1952
1953 (setq pair (reftex-offer-label-menu type)
1954 label (car pair)
1955 file (cdr pair))
1956 (if (and label file (file-exists-p file))
1957 (progn
1958 (if arg
1959 (find-file-other-window file)
1960 (find-file file))
1961 (goto-char (point-min))
1962 (if (not (search-forward (concat "\\label{" label "}") nil t))
1963 (error "No such label found: %s" label)
1964 (reftex-highlight 0 (match-beginning 0) (match-end 0))
1965 (add-hook 'pre-command-hook 'reftex-highlight-shall-die)))
1966 (message "Quit")
1967 nil)))
1968
1969 ;; Internal list with index numbers of labels in the selection menu
1970 (defvar reftex-label-index-list nil)
1971
1972 (defun reftex-offer-label-menu (typekey) 2729 (defun reftex-offer-label-menu (typekey)
1973 ;; Offer a menu with the appropriate labels. Return (label . file). 2730 ;; Offer a menu with the appropriate labels. Return (label . file).
1974 (let* ((buf (current-buffer)) 2731 (let* ((buf (current-buffer))
1975 (near-label (reftex-find-nearby-label)) 2732 (xr-data (assq 'xr (symbol-value reftex-docstruct-symbol)))
2733 (xr-alist (cons (cons "" (buffer-file-name)) (nth 1 xr-data)))
2734 (xr-index 0)
2735 (here-I-am (car (reftex-where-am-I)))
1976 (toc (reftex-typekey-check typekey reftex-label-menu-flags 0)) 2736 (toc (reftex-typekey-check typekey reftex-label-menu-flags 0))
2737 (files (reftex-typekey-check typekey reftex-label-menu-flags 7))
1977 (context (not (reftex-typekey-check 2738 (context (not (reftex-typekey-check
1978 typekey reftex-label-menu-flags 3))) 2739 typekey reftex-label-menu-flags 3)))
1979 (counter (reftex-typekey-check 2740 (counter (reftex-typekey-check
1980 typekey reftex-label-menu-flags 2)) 2741 typekey reftex-label-menu-flags 2))
1981 (follow (reftex-typekey-check 2742 (follow (reftex-typekey-check
1982 typekey reftex-label-menu-flags 4)) 2743 typekey reftex-label-menu-flags 4))
1983 offset rtn key cnt entry) 2744 (commented (nth 5 reftex-label-menu-flags))
1984 2745 (match-everywhere (reftex-typekey-check
1985 (setq reftex-call-back-to-this-buffer buf) 2746 typekey reftex-label-menu-flags 6))
2747 (prefix "")
2748 offset rtn key cnt last-cnt entry)
2749
1986 (setq entry (cons nil nil)) 2750 (setq entry (cons nil nil))
1987 2751
2752 ;; The following unwind-protect kills temporary buffers after use
1988 (unwind-protect 2753 (unwind-protect
1989 (catch 'exit 2754 (catch 'exit
1990 (while t 2755 (while t
1991 (save-window-excursion 2756 (save-window-excursion
2757 (setq reftex-call-back-to-this-buffer buf)
1992 (switch-to-buffer-other-window "*RefTeX Select*") 2758 (switch-to-buffer-other-window "*RefTeX Select*")
1993 (erase-buffer) 2759 (erase-buffer)
1994 (setq truncate-lines t) 2760 (setq truncate-lines t)
1995 (setq reftex-label-index-list (reftex-make-and-insert-label-list 2761 (setq mode-line-format
1996 typekey buf toc context counter 2762 (list "---- " 'mode-line-buffer-identification
1997 near-label)) 2763 " " (abbreviate-file-name
1998 (setq near-label "_ ") ; turn off search for near label 2764 (buffer-file-name buf))
2765 " -%-"))
2766
2767 (setq reftex-label-index-list
2768 (reftex-make-and-insert-label-list
2769 typekey buf toc files context counter commented
2770 here-I-am prefix))
2771 (setq here-I-am nil) ; turn off determination of offset
2772 ;; use only when searched
1999 (setq offset (or (car reftex-label-index-list) offset)) 2773 (setq offset (or (car reftex-label-index-list) offset))
2000 ;; use only when searched
2001 (setq reftex-label-index-list (cdr reftex-label-index-list))
2002 ;; only this is the true list 2774 ;; only this is the true list
2003 (if (not reftex-label-index-list) 2775 (pop reftex-label-index-list)
2004 (error "No labels of type \"%s\"" typekey))
2005 (setq rtn 2776 (setq rtn
2006 (reftex-select-item 2777 (reftex-select-item
2007 nil 2778 reftex-select-label-prompt
2008 "Label: [n]ext [p]rev [r]escan [t]oc [ ]context [q]uit RETURN [?]HELP+more"
2009 "^>" 2779 "^>"
2010 "\n[^.]"
2011 2 2780 2
2012 reftex-reference-label-help 2781 reftex-select-label-help
2013 '(?r ?g ?c ?t ?s ?# ?a) 2782 '(?r ?R ?g ?c ?t ?s ?# ?i ?l ?% ?x)
2014 offset 2783 offset
2015 'reftex-select-label-callback follow)) 2784 'reftex-select-label-callback follow
2016 (setq key (car rtn) 2785 match-everywhere))
2017 cnt (cdr rtn) 2786 (setq key (car rtn)
2018 offset (1+ cnt)) 2787 cnt (nth 1 rtn)
2019 (if (not key) (throw 'exit nil)) 2788 last-cnt (nth 2 rtn)
2789 offset (1+ (or cnt last-cnt 0)))
2790 (unless key (throw 'exit nil))
2020 (cond 2791 (cond
2021 ((or (equal key ?r) 2792 ((or (eq key ?r)
2022 (equal key ?g)) 2793 (eq key ?R)
2794 (eq key ?g))
2023 ;; rescan buffer 2795 ;; rescan buffer
2024 (reftex-parse-document buf)) 2796 (reftex-parse-document buf (or cnt last-cnt) key))
2025 ((equal key ?c) 2797 ((eq key ?c)
2026 ;; toggle context mode 2798 ;; toggle context mode
2027 (setq context (not context))) 2799 (setq context (not context)))
2028 ((equal key ?s) 2800 ((eq key ?s)
2029 ;; switch type 2801 ;; switch type
2030 (setq typekey (reftex-query-label-type))) 2802 (setq typekey (reftex-query-label-type)))
2031 ((equal key ?t) 2803 ((eq key ?t)
2032 ;; toggle tabel of contents display 2804 ;; toggle tabel of contents display
2033 (setq toc (not toc))) 2805 (setq toc (not toc)))
2034 ((equal key ?#) 2806 ((eq key ?i)
2807 ;; toggle display of included file borders
2808 (setq files (not files)))
2809 ((eq key ?#)
2035 ;; toggle counter display 2810 ;; toggle counter display
2036 (setq counter (not counter))) 2811 (setq counter (not counter)))
2037 ((equal key ?a) 2812 ((eq key ?%)
2813 ;; toggle display of commented labels
2814 (setq commented (not commented)))
2815 ((eq key ?l)
2038 ;; reuse the last referenced label again 2816 ;; reuse the last referenced label again
2039 (setq entry reftex-last-used-reference) 2817 (setq entry reftex-last-used-reference)
2040 (throw 'exit t)) 2818 (throw 'exit t))
2819 ((eq key ?x)
2820 ;; select an external document
2821 (setq xr-index (reftex-select-external-document
2822 xr-alist xr-index))
2823 (setq buf (or (reftex-get-file-buffer-force
2824 (cdr (nth xr-index xr-alist)))
2825 (error "Cannot switch document"))
2826 prefix (or (car (nth xr-index xr-alist)) "")
2827 offset nil))
2041 (t 2828 (t
2042 (set-buffer buf) 2829 (set-buffer buf)
2043 (setq entry (nth (nth cnt reftex-label-index-list) 2830 (if cnt
2044 (symbol-value reftex-list-of-labels-symbol))) 2831 (progn
2045 (setq reftex-last-used-reference entry) 2832 (setq entry (nth (nth cnt reftex-label-index-list)
2833 (symbol-value reftex-docstruct-symbol)))
2834 (setq reftex-last-used-reference entry))
2835 (setq entry nil))
2046 (throw 'exit t)))))) 2836 (throw 'exit t))))))
2047 (kill-buffer "*RefTeX Select*") 2837 (kill-buffer "*RefTeX Select*")
2838 (and (get-buffer "*RefTeX Context Copy*")
2839 (kill-buffer "*RefTeX Context Copy*"))
2048 (reftex-kill-temporary-buffers)) 2840 (reftex-kill-temporary-buffers))
2049 (cons (reftex-no-props (nth 0 entry)) (nth 3 entry)))) 2841 (cons (if (nth 0 entry) (concat prefix (nth 0 entry)) nil)
2050 2842 (nth 3 entry))))
2051 ;; Indentation for table of context lines in the menu 2843
2052 (defconst reftex-toc-indent " ") 2844 (defun reftex-select-external-document (xr-alist xr-index)
2053 ;; Indentation for the lines containing the label 2845 ;; Return index of an external document.
2054 (defconst reftex-label-indent "> ") 2846 (cond
2055 ;; Indentation for context lines 2847 ((= (length xr-alist) 1)
2056 (defconst reftex-context-indent ". ") 2848 (message "No external douments available")
2057 ;; Indentation per section level 2849 (ding) 0)
2058 (defvar reftex-level-indent 2 2850 ((= (length xr-alist) 2)
2059 "*Number of spaces to be used for indentation per section level. 2851 (- 1 xr-index))
2060 With more indentation, the label menu looks nicer, but shows less context. 2852 (t
2061 Changing this is only fully operational after the next buffer scan.") 2853 (save-window-excursion
2062 2854 (let* ((fmt " [%c] %-5s %s\n") (n (1- ?0)) key)
2063 (defun reftex-make-and-insert-label-list (typekey0 buf toc context 2855 (with-output-to-temp-buffer "*RefTeX Select*"
2064 counter near-label) 2856 (princ
2857 (concat "Select a document by pressing a number key:\n KEY PREFIX DOCUMENT\n----------------------\n"
2858 (mapconcat '(lambda (x)
2859 (format fmt (incf n) (or (car x) "")
2860 (abbreviate-file-name (cdr x))))
2861 xr-alist ""))))
2862 (setq key (read-char))
2863 (if (< (- key ?1) (length xr-alist))
2864 (- key ?0)
2865 (error "Illegal document selection [%c]" key)))))))
2866
2867 (defun reftex-make-and-insert-label-list
2868 (typekey0 buf toc files context counter show-commented here-I-am xr-prefix)
2065 ;; Insert a menu of all labels in buffer BUF into current buffer. 2869 ;; Insert a menu of all labels in buffer BUF into current buffer.
2066 ;; Return the list of labels, with the index of NEAR-LABEL as extra car. 2870 ;; Return the list of labels, with the index of HERE-I-AM as extra car.
2067 (let (ins-list index-list offset) 2871 (let* ((font (reftex-use-fonts))
2872 (refont (reftex-refontify))
2873 (cnt 0)
2874 (index -1)
2875 (toc-indent " ")
2876 (label-indent
2877 (concat "> "
2878 (if toc (make-string (* 7 reftex-level-indent) ?\ ) "")))
2879 (context-indent
2880 (concat ". "
2881 (if toc (make-string (* 7 reftex-level-indent) ?\ ) "")))
2882 all cell text label typekey note comment master-dir-re
2883 index-list offset docstruct-symbol from from1 to)
2884
2885 ;; Pop to buffer buf to get the correct buffer-local variables
2068 (save-excursion 2886 (save-excursion
2069 (set-buffer buf) 2887 (set-buffer buf)
2070 (let* ((all nil) 2888
2071 (font (reftex-use-fonts)) 2889 ;; Ensure access to scanning info
2072 (cnt 0) 2890 (reftex-access-scan-info)
2073 (file nil) 2891
2074 (index -1) 2892 (setq docstruct-symbol reftex-docstruct-symbol
2075 (toc-indent reftex-toc-indent) 2893 all (symbol-value reftex-docstruct-symbol)
2076 (label-indent 2894 reftex-active-toc nil
2077 (concat reftex-label-indent 2895 master-dir-re
2078 (if toc (make-string (* 7 reftex-level-indent) ?\ ) ""))) 2896 (concat "\\`" (regexp-quote
2079 (context-indent 2897 (file-name-directory (reftex-TeX-master-file))))))
2080 (concat reftex-context-indent 2898
2081 (if toc (make-string (* 7 reftex-level-indent) ?\ ) ""))) 2899 (when refont
2082 cell text label typekey note comment) 2900 ;; Calculate font-lock-defaults as in LaTeX mode.
2083 2901 (make-local-variable 'font-lock-defaults)
2084 ; Ensure access to scanning info 2902 (setq font-lock-defaults nil)
2085 (reftex-access-scan-info) 2903 (let ((major-mode 'latex-mode))
2086 2904 (font-lock-set-defaults))
2087 (setq all (symbol-value reftex-list-of-labels-symbol)) 2905 ;; The following is only needed for XEmacs, but does not hurt Emacs.
2088 2906 (setq font-lock-mode nil))
2089 (while all 2907
2090 2908 ;; Walk the docstruct and insert the appropriate stuff
2091 (setq index (1+ index) 2909
2092 cell (car all) 2910 (while (setq cell (pop all))
2093 all (cdr all)) 2911
2094 2912 (incf index)
2095 (if (null (nth 2 cell)) 2913 (setq from (point))
2096 ;; No context yet. Quick update 2914
2097 (progn 2915 (if (eq cell here-I-am) (setq offset (1+ cnt)))
2098 (setq cell (reftex-label-info-update cell)) 2916
2099 (setcar (nthcdr index 2917 (cond
2100 (symbol-value reftex-list-of-labels-symbol)) 2918
2101 cell))) 2919 ((memq (car cell) '(bib label-numbers master-dir is-multi
2102 2920 xr xr-doc)))
2103 ;; in the following setq we *copy* the label, since we will change 2921 ;; These are currently ignored
2104 ;; its properties, and we cannot have any properties in the list 2922
2105 ;; (because of assoc searches) 2923 ((memq (car cell) '(bof eof file-error))
2106 (setq label (copy-sequence (nth 0 cell)) 2924 ;; Beginning or end of a file
2107 typekey (nth 1 cell) 2925 (when files
2108 text (nth 2 cell) 2926 (insert
2109 file (nth 3 cell) 2927 " " (if (string-match master-dir-re (nth 1 cell))
2110 note (nth 4 cell) 2928 (substring (nth 1 cell) (match-end 0))
2111 comment (get-text-property 0 'in-comment text)) 2929 (nth 1 cell))
2112 2930 (cond ((eq (car cell) 'bof) " starts here\n")
2113 (if (string= label near-label) 2931 ((eq (car cell) 'eof) " ends here\n")
2114 (setq offset (1+ cnt))) 2932 ((eq (car cell) 'file-error) " was not found\n")))
2115 2933 (when font
2116 (cond 2934 (put-text-property from (point)
2117 ((and toc (string= typekey "toc")) 2935 'face 'font-lock-function-name-face))))
2118 (setq ins-list 2936
2119 (cons (concat toc-indent text "\n") 2937 ((eq (car cell) 'toc)
2120 ins-list))) 2938 ;; a table of contents entry
2121 ((string= typekey "toc")) 2939 (when toc
2122 ((and (or (string= typekey typekey0) (string= typekey0 " ")) 2940 (setq reftex-active-toc cell)
2123 (or (nth 5 reftex-label-menu-flags) ; show-commented? 2941 (insert (concat toc-indent (nth 2 cell) "\n"))))
2124 (null comment))) 2942
2125 (setq cnt (1+ cnt)) 2943 ((stringp (car cell))
2126 (if comment (setq label (concat "% " label))) 2944 ;; a label
2127 (if font 2945 (when (null (nth 2 cell))
2128 (put-text-property 2946 ;; No context yet. Quick update.
2129 0 (length label) 2947 (setq cell (reftex-label-info-update cell))
2130 'face 2948 (setcar (nthcdr index (symbol-value docstruct-symbol))
2131 (if comment 2949 cell))
2132 'font-lock-comment-face 2950
2133 'font-lock-reference-face) 2951 (setq label (car cell)
2134 label)) 2952 typekey (nth 1 cell)
2135 (setq index-list (cons index index-list)) 2953 text (nth 2 cell)
2136 (setq ins-list 2954 note (nth 4 cell)
2137 (cons (concat 2955 comment (get-text-property 0 'in-comment text))
2138 label-indent 2956
2139 label 2957 (when (and (or (string= typekey typekey0) (string= typekey0 " "))
2140 (if counter (format " (%d) " cnt)) 2958 (or show-commented (null comment)))
2141 (if comment " LABEL IS COMMENTED OUT ") 2959
2142 (if note (concat " " note) "") 2960 ;; Yes we want this one
2143 "\n" 2961 (incf cnt)
2144 (if context (concat context-indent text "\n"))) 2962 (push index index-list)
2145 ins-list)))) 2963
2146 ))) 2964 (setq label (concat xr-prefix label))
2147 2965 (when comment (setq label (concat "% " label)))
2148 (apply 'insert (nreverse ins-list)) 2966 (insert label-indent label)
2967 (when font
2968 (put-text-property
2969 (- (point) (length label)) (point)
2970 'face (if comment
2971 'font-lock-comment-face
2972 'font-lock-reference-face)))
2973
2974 (insert (if counter (format " (%d) " cnt) "")
2975 (if comment " LABEL IS COMMENTED OUT " "")
2976 (if note (concat " " note) "")
2977 "\n")
2978 (setq to (point))
2979
2980 (when context
2981 (setq from1 to)
2982 (insert context-indent text "\n")
2983 (setq to (point))
2984 (when refont
2985 (font-lock-fontify-region from1 to)
2986 (goto-char to)))
2987 (put-text-property from to 'cnt (1- cnt))
2988 (goto-char to)))))
2989
2990 ;; Return the index list
2149 (cons offset (nreverse index-list)))) 2991 (cons offset (nreverse index-list))))
2992
2993 (defun reftex-parse-document (&optional buffer cnt key)
2994 "Rescan the document."
2995 (interactive)
2996 (save-window-excursion
2997 (save-excursion
2998 (if buffer
2999 (if (not (bufferp buffer))
3000 (error "No such buffer %s" (buffer-name buffer))
3001 (set-buffer buffer)))
3002 (let ((arg (if (eq key ?R) '(16) '(4)))
3003 (file (if cnt
3004 (nth 3
3005 (nth (nth cnt reftex-label-index-list)
3006 (symbol-value reftex-docstruct-symbol))))))
3007 (reftex-access-scan-info arg file)))))
2150 3008
2151 (defun reftex-query-label-type () 3009 (defun reftex-query-label-type ()
2152 ;; Ask for label type 3010 ;; Ask for label type
2153 (message reftex-type-query-prompt) 3011 (message reftex-type-query-prompt)
2154 (let ((key (read-char))) 3012 (let ((key (read-char)))
2155 (if (equal key ?\?) 3013 (when (eq key ?\?)
2156 (progn 3014 (save-window-excursion
2157 (save-window-excursion 3015 (with-output-to-temp-buffer "*RefTeX Help*"
2158 (with-output-to-temp-buffer "*RefTeX Help*" 3016 (princ reftex-type-query-help))
2159 (princ reftex-type-query-help)) 3017 (setq key (read-char))
2160 (setq key (read-char)) 3018 (kill-buffer "*RefTeX Help*")))
2161 (kill-buffer "*RefTeX Help*")))) 3019 (unless (member (char-to-string key) reftex-typekey-list)
2162 (if (not (member (char-to-string key) reftex-typekey-list)) 3020 (error "No such label type: %s" (char-to-string key)))
2163 (error "No such label type: %s" (char-to-string key)))
2164 (char-to-string key))) 3021 (char-to-string key)))
2165
2166 (defun reftex-find-nearby-label ()
2167 ;; Find a nearby label.
2168 (save-excursion
2169 (if (or (re-search-backward "\\\\label{\\([^}]+\\)}" nil t)
2170 (re-search-forward "\\\\label{\\([^}]+\\)}" nil t))
2171 (reftex-no-props (match-string 1))
2172 nil)))
2173 3022
2174 ;; Variable holding the vector with section numbers 3023 ;; Variable holding the vector with section numbers
2175 (defvar reftex-section-numbers [0 0 0 0 0 0 0 0]) 3024 (defvar reftex-section-numbers [0 0 0 0 0 0 0 0])
2176 3025
2177 (defun reftex-scan-buffer-for-labels (label-numbers-symbol label-list-symbol) 3026 (defun reftex-section-info (file)
2178 ;; Scan the buffer for labels and save them in a list. 3027 ;; Return a section entry for the current match.
2179 (save-excursion 3028 ;; Carefull: This function expects the match-data to be still in place!
2180 (let ((regexp (concat "\\\\label{\\([^}]*\\)}" "\\|" 3029 (let* ((marker (set-marker (make-marker) (1- (match-beginning 3))))
2181 reftex-section-regexp)) 3030 (macro (reftex-match-string 3))
2182 (font (reftex-use-fonts)) 3031 (star (= ?* (char-after (match-end 3))))
2183 (bound 0) 3032 (level (cdr (assoc macro reftex-section-levels)))
2184 (highest-level 100) 3033 (section-number (reftex-section-number
2185 file (level 1) star text text1 label section-number macro find) 3034 reftex-section-numbers level star))
2186 (set label-list-symbol nil) 3035 (text1 (save-match-data (save-excursion (reftex-context-substring))))
2187 (goto-char 0) 3036 (literal (buffer-substring-no-properties
2188 3037 (1- (match-beginning 3))
2189 ;; reset label numbers 3038 (min (point-max) (+ (match-end 0) (length text1) 1))))
2190 (set label-numbers-symbol 3039 ;; Literal can be too short since text1 too short. No big problem.
2191 (mapcar '(lambda(x) (cons x 0)) reftex-typekey-list)) 3040 (text (reftex-nicify-text text1)))
2192 3041
2193 ;; reset section numbers 3042 ;; Add section number and indentation
2194 (reftex-section-number reftex-section-numbers -1) 3043 (setq text
2195 3044 (concat
2196 (while (re-search-forward regexp nil t) 3045 (make-string (* reftex-level-indent level) ?\ )
2197 (setq file (get-text-property (match-beginning 0) 'file)) 3046 (if (nth 1 reftex-label-menu-flags) ; section number flag
2198 (if (string= (buffer-substring (match-beginning 0) 3047 (concat section-number " "))
2199 (+ 7 (match-beginning 0))) "\\label{") 3048 text))
2200 ;; It is a label 3049 ;; Fontify
2201 (progn 3050 (if (reftex-use-fonts)
2202 (setq label (reftex-no-props (match-string 1))) 3051 (put-text-property 0 (length text)
2203 (set label-list-symbol 3052 'face 'font-lock-comment-face text))
2204 (cons (reftex-label-info label file bound) 3053 (list 'toc "toc" text file marker level section-number
2205 (symbol-value label-list-symbol)))) 3054 literal (marker-position marker))))
2206
2207 ;; It is a section
2208 (setq bound (point))
2209 (setq star (= ?* (char-after (match-end 2))))
2210 (setq find (buffer-substring-no-properties
2211 (1- (match-beginning 2)) (match-end 0)))
2212 (setq macro (reftex-no-props (match-string 2)))
2213 (setq level (cdr (assoc macro reftex-section-levels)))
2214
2215 (setq section-number (reftex-section-number
2216 reftex-section-numbers level star))
2217 (setq highest-level (min highest-level level))
2218 (if (= level highest-level)
2219 (message
2220 "Scanning %s %s ..."
2221 (car (nth level reftex-section-levels))
2222 section-number))
2223
2224 ;; get the title
2225 (save-match-data
2226 (setq text1 (reftex-context-substring))
2227 (setq text (reftex-nicify-text text1)))
2228
2229 (setq find (reftex-allow-for-ctrl-m (concat find text1)))
2230
2231 ;; add section number and indentation
2232 (setq text
2233 (concat
2234 (make-string (* reftex-level-indent level) ?\ )
2235 (if (nth 1 reftex-label-menu-flags) ; section number flag
2236 (concat section-number " "))
2237 text))
2238 ;; fontify
2239 (if font (put-text-property 0 (length text)
2240 'face 'font-lock-comment-face text))
2241
2242 ;; insert in list
2243 (set label-list-symbol
2244 (cons (list nil "toc" text file find)
2245 (symbol-value label-list-symbol)))))
2246 (set label-list-symbol
2247 (nreverse (symbol-value label-list-symbol))))))
2248
2249 3055
2250 (defun reftex-label-info-update (cell) 3056 (defun reftex-label-info-update (cell)
2251 ;; Update information about just one label in a different file. 3057 ;; Update information about just one label in a different file.
2252 ;; CELL contains the old info list 3058 ;; CELL contains the old info list
2253 (let* ((label (nth 0 cell)) 3059 (let* ((label (nth 0 cell))
2254 (typekey (nth 1 cell)) 3060 (typekey (nth 1 cell))
2255 (text (nth 2 cell)) 3061 ;; (text (nth 2 cell))
2256 (file (nth 3 cell)) 3062 (file (nth 3 cell))
2257 (note (nth 4 cell)) 3063 (note (nth 4 cell))
2258 (buf (reftex-get-file-buffer-force 3064 (buf (reftex-get-file-buffer-force
2259 file (not reftex-keep-temporary-buffers)))) 3065 file (not (eq t reftex-keep-temporary-buffers)))))
2260 (if (not buf) 3066 (if (not buf)
2261 (list label typekey "" file "LOST LABEL. RESCAN TO FIX.") 3067 (list label typekey "" file "LOST LABEL. RESCAN TO FIX.")
2262 (save-excursion 3068 (save-excursion
2263 (set-buffer buf) 3069 (set-buffer buf)
2264 (save-restriction 3070 (save-restriction
2265 (widen) 3071 (widen)
2266 (goto-char 1) 3072 (goto-char 1)
2267 3073
2268 (if (re-search-forward (concat "\\\\label{" (regexp-quote label) "}") 3074 (if (or (re-search-forward
2269 nil t) 3075 (format reftex-find-label-regexp-format
2270 (append (reftex-label-info label file) (list note)) 3076 (regexp-quote label)) nil t)
3077 (re-search-forward
3078 (format reftex-find-label-regexp-format2
3079 (regexp-quote label)) nil t))
3080
3081 (progn
3082 (backward-char 1)
3083 (append (reftex-label-info label file) (list note)))
2271 (list label typekey "" file "LOST LABEL. RESCAN TO FIX."))))))) 3084 (list label typekey "" file "LOST LABEL. RESCAN TO FIX.")))))))
2272 3085
2273 (defun reftex-label-info (label &optional file bound) 3086 (defun reftex-label-info (label &optional file bound derive env-or-mac)
2274 ;; Return info list on LABEL at point. 3087 ;; Return info list on LABEL at point.
2275 (let* ((env-or-mac (reftex-label-location bound)) 3088 (let* ((env-or-mac (or env-or-mac (reftex-label-location bound)))
2276 (typekey (nth 1 (assoc env-or-mac reftex-env-or-mac-alist))) 3089 (typekey (nth 1 (assoc env-or-mac reftex-env-or-mac-alist)))
2277 (file (or file (buffer-file-name))) 3090 (file (or file (buffer-file-name)))
2278 (parse (if (reftex-typekey-check 3091 (parse (if (reftex-typekey-check
2279 typekey reftex-use-text-after-label-as-context) 3092 typekey reftex-use-text-after-label-as-context)
2280 nil 3093 nil
2281 (nth 2 (assoc env-or-mac reftex-env-or-mac-alist)))) 3094 (nth 2 (assoc env-or-mac reftex-env-or-mac-alist))))
2282 (text (reftex-short-context env-or-mac parse reftex-location-start))) 3095 (text (reftex-short-context env-or-mac parse reftex-location-start
3096 derive)))
2283 (if (reftex-in-comment) 3097 (if (reftex-in-comment)
2284 (put-text-property 0 1 'in-comment t text)) 3098 (put-text-property 0 1 'in-comment t text))
2285 (list label typekey text file))) 3099 (list label typekey text file)))
2286 3100
2287 (defun reftex-in-comment () 3101 (defun reftex-in-comment ()
2288 (save-excursion 3102 (save-excursion
2289 (skip-chars-backward "^%\n\r") 3103 (skip-chars-backward "^%\n\r")
2290 (= (preceding-char) ?%))) 3104 (eq (preceding-char) ?%)))
2291 3105
2292 (defun reftex-short-context (env parse &optional bound) 3106 (defun reftex-short-context (env parse &optional bound derive)
2293 ;; Get about one line of useful context for the label definition at point. 3107 ;; Get about one line of useful context for the label definition at point.
3108
3109 (if (consp parse)
3110 (setq parse (if derive (cdr parse) (car parse))))
2294 3111
2295 (reftex-nicify-text 3112 (reftex-nicify-text
2296 3113
2297 (cond 3114 (cond
2298 3115
2302 3119
2303 ((eq parse t) 3120 ((eq parse t)
2304 (if (string= env "section") 3121 (if (string= env "section")
2305 ;; special treatment for section labels 3122 ;; special treatment for section labels
2306 (save-excursion 3123 (save-excursion
2307 (if (re-search-backward reftex-section-regexp (point-min) t) 3124 (if (and (re-search-backward reftex-section-or-include-regexp
3125 (point-min) t)
3126 (match-end 2))
2308 (progn 3127 (progn
2309 (goto-char (match-end 0)) 3128 (goto-char (match-end 0))
2310 (reftex-context-substring)) 3129 (reftex-context-substring))
2311 "SECTION HEADING NOT FOUND")) 3130 (if reftex-active-toc
3131 (progn
3132 (string-match "{\\([^}]*\\)" (nth 7 reftex-active-toc))
3133 (match-string 1 (nth 7 reftex-active-toc)))
3134 "SECTION HEADING NOT FOUND")))
2312 (save-excursion 3135 (save-excursion
2313 (goto-char reftex-default-context-position) 3136 (goto-char reftex-default-context-position)
3137 (unless (eq (string-to-char env) ?\\)
3138 (reftex-move-over-touching-args))
2314 (reftex-context-substring)))) 3139 (reftex-context-substring))))
2315 3140
2316 ((stringp parse) 3141 ((stringp parse)
2317 (save-excursion 3142 (save-excursion
2318 (if (re-search-backward parse bound t) 3143 (if (re-search-backward parse bound t)
2319 (progn 3144 (progn
2320 (goto-char (match-end 0)) 3145 (goto-char (match-end 0))
2321 (reftex-context-substring)) 3146 (reftex-context-substring))
2322 "NO MATCH FOR CONTEXT REGEXP"))) 3147 "NO MATCH FOR CONTEXT REGEXP")))
3148
3149 ((integerp parse)
3150 (or (save-excursion
3151 (goto-char reftex-default-context-position)
3152 (reftex-nth-arg
3153 parse
3154 (nth 6 (assoc env reftex-env-or-mac-alist))))
3155 ""))
3156
2323 ((fboundp parse) 3157 ((fboundp parse)
2324 ;; A hook function. Call it. 3158 ;; A hook function. Call it.
2325 (save-excursion 3159 (save-excursion
2326 (condition-case error-var 3160 (condition-case error-var
2327 (funcall parse env) 3161 (funcall parse env)
2328 ('error (format "HOOK ERROR: %s" (cdr error-var)))))) 3162 (error (format "HOOK ERROR: %s" (cdr error-var))))))
2329 (t 3163 (t
2330 "ILLEGAL VALUE OF PARSE")))) 3164 "ILLEGAL VALUE OF PARSE"))))
3165
3166 (defun reftex-where-am-I ()
3167 ;; Return the docstruct entry above point. Actually returns a cons
3168 ;; cell in which the cdr is a flag indicating if the information is
3169 ;; exact (t) or approximate (nil).
3170 (interactive)
3171
3172 (let ((docstruct (symbol-value reftex-docstruct-symbol))
3173 (cnt 0) rtn
3174 found)
3175 (save-excursion
3176 (while (not rtn)
3177 (incf cnt)
3178 (setq found (re-search-backward reftex-everything-regexp nil t))
3179 (setq rtn
3180 (cond
3181 ((not found)
3182 ;; no match
3183 (or
3184 (car (member (list 'bof (buffer-file-name)) docstruct))
3185 (not (setq cnt 2))
3186 (assq 'bof docstruct) ;; for safety reasons
3187 'corrupted))
3188 ((match-end 1)
3189 ;; Label
3190 (assoc (reftex-match-string 1)
3191 (symbol-value reftex-docstruct-symbol)))
3192 ((match-end 3)
3193 ;; Section
3194 (goto-char (1- (match-beginning 3)))
3195 (let* ((list (member (list 'bof (buffer-file-name))
3196 docstruct))
3197 (endelt (car (member (list 'eof (buffer-file-name))
3198 list)))
3199 rtn1)
3200 (while (and list (not (eq endelt (car list))))
3201 (if (and (eq (car (car list)) 'toc)
3202 (string= (buffer-file-name)
3203 (nth 3 (car list))))
3204 (cond
3205 ((equal (point)
3206 (or (and (markerp (nth 4 (car list)))
3207 (marker-position (nth 4 (car list))))
3208 (nth 8 (car list))))
3209 ;; Fits with marker position or recorded position
3210 (setq rtn1 (car list) list nil))
3211 ((looking-at (reftex-make-regexp-allow-for-ctrl-m
3212 (nth 7 (car list))))
3213 ;; Same title
3214 (setq rtn1 (car list) list nil cnt 2))))
3215 (pop list))
3216 rtn1))
3217 ((match-end 7)
3218 ;; Input or include...
3219 (car
3220 (member (list 'eof (reftex-find-tex-file
3221 (reftex-match-string 7)
3222 (cons
3223 (cdr (assq 'master-dir docstruct))
3224 reftex-tex-path)))
3225 docstruct)))
3226 ((match-end 8)
3227 (save-excursion
3228 (goto-char (match-end 8))
3229 (assoc (reftex-no-props
3230 (reftex-nth-arg-wrapper
3231 (reftex-match-string 8)))
3232 (symbol-value reftex-docstruct-symbol))))
3233 (t
3234 (error "This should not happen (reftex-where-am-I)"))))))
3235 (cons rtn (eq cnt 1))))
3236
3237 (defun reftex-parse-args (macro)
3238 ;; Return a list of macro name, nargs, arg-nr which is label and a list of
3239 ;; optional argument indices.
3240 (if (string-match "[[{]\\*?[]}]" macro)
3241 (progn
3242 (let ((must-match (substring macro 0 (match-beginning 0)))
3243 (args (substring macro (match-beginning 0)))
3244 opt-list nlabel (cnt 0))
3245 (while (string-match "\\`[[{]\\(\\*\\)?[]}]" args)
3246 (incf cnt)
3247 (when (eq ?\[ (string-to-char args))
3248 (push cnt opt-list))
3249 (when (and (match-end 1)
3250 (not nlabel))
3251 (setq nlabel cnt))
3252 (setq args (substring args (match-end 0))))
3253 (list must-match cnt nlabel opt-list)))
3254 nil))
3255
3256 (defsubst reftex-move-to-next-arg (&optional ignore)
3257 ;; Assuming that we are at the end of a macro name or a macro argument,
3258 ;; move forward to the opening parenthesis of the next argument.
3259 ;; This function understands the splitting of macros over several lines
3260 ;; in TeX.
3261 (cond
3262 ;; Just to be quick:
3263 ((memq (following-char) '(?\[ ?\{)))
3264 ;; Do a search
3265 ((looking-at "[ \t]*[\n\r]?\\([ \t]*%[^\n\r]*[\n\r]\\)*[ \t]*[[{]")
3266 (goto-char (1- (match-end 0)))
3267 t)
3268 (t nil)))
3269
3270 (defsubst reftex-move-to-previous-arg (&optional bound)
3271 ;; Assuming that we are in front of a macro argument,
3272 ;; move backward to the closing parenthesis of the previous argument.
3273 ;; This function understands the splitting of macros over several lines
3274 ;; in TeX.
3275 (cond
3276 ;; Just to be quick:
3277 ((memq (preceding-char) '(?\] ?\})))
3278 ;; Do a search
3279 ((re-search-backward
3280 "[]}][ \t]*[\n\r]?\\([ \t]*%[^\n\r]*[\n\r]\\)*[ \t]*\\=" bound t)
3281 (goto-char (1+ (match-beginning 0)))
3282 t)
3283 (t nil)))
3284
3285 (defun reftex-nth-arg-wrapper (key)
3286 (let ((entry (assoc key reftex-env-or-mac-alist)))
3287 (reftex-nth-arg (nth 5 entry) (nth 6 entry))))
3288
3289 (defun reftex-nth-arg (n &optional opt-args)
3290 ;; Return the nth following {} or [] parentheses content.
3291 ;; OPT-ARGS is a list of argument numbers which are optional.
3292
3293 ;; If we are sitting at a macro start, skip to end of macro name.
3294 (and (eq (following-char) ?\\) (skip-chars-forward "a-zA-Z*\\\\"))
3295
3296 (if (= n 1000)
3297 ;; Special case: Skip all touching arguments
3298 (progn
3299 (reftex-move-over-touching-args)
3300 (reftex-context-substring))
3301
3302 ;; Do the real thing.
3303 (let ((cnt 1))
3304
3305 (when (reftex-move-to-next-arg)
3306
3307 (while (< cnt n)
3308 (while (and (member cnt opt-args)
3309 (eq (following-char) ?\{))
3310 (incf cnt))
3311 (when (< cnt n)
3312 (unless (and (condition-case nil
3313 (or (forward-list 1) t)
3314 (error nil))
3315 (reftex-move-to-next-arg)
3316 (incf cnt))
3317 (setq cnt 1000))))
3318
3319 (while (and (memq cnt opt-args)
3320 (eq (following-char) ?\{))
3321 (incf cnt)))
3322 (if (and (= n cnt)
3323 (> (skip-chars-forward "{\\[") 0))
3324 (reftex-context-substring)
3325 nil))))
3326
3327 (defun reftex-move-over-touching-args ()
3328 (condition-case nil
3329 (while (memq (following-char) '(?\[ ?\{))
3330 (forward-list 1))
3331 (error nil)))
2331 3332
2332 (defun reftex-context-substring () 3333 (defun reftex-context-substring ()
2333 ;; Return up to 100 chars from point 3334 ;; Return up to 100 chars from point
2334 ;; When point is just after a { or [, limit string to matching parenthesis 3335 ;; When point is just after a { or [, limit string to matching parenthesis
2335 (cond 3336 (cond
2336 ((or (= (preceding-char) ?\{) 3337 ((or (= (preceding-char) ?\{)
2337 (= (preceding-char) ?\[)) 3338 (= (preceding-char) ?\[))
2338 ;; inside a list - get only the list, with modified syntax to be perfect 3339 ;; Inside a list - get only the list.
2339 (buffer-substring 3340 (buffer-substring-no-properties
2340 (point) 3341 (point)
2341 (min (+ 100 (point)) 3342 (min (reftex-fp 150)
2342 (point-max) 3343 (point-max)
2343 (condition-case nil 3344 (condition-case nil
2344 (progn 3345 (progn
2345 (up-list 1) 3346 (up-list 1)
2346 (1- (point))) 3347 (1- (point)))
2347 ('error (point-max)))))) 3348 (error (point-max))))))
2348 (t 3349 (t
2349 ;; no list - just grab 100 characters 3350 ;; no list - just grab 100 characters
2350 (buffer-substring (point) (min (+ 100 (point)) (point-max)))))) 3351 (buffer-substring-no-properties (point) (min (reftex-fp 150) (point-max))))))
3352
3353 (defun reftex-init-section-numbers (&optional toc-entry)
3354 ;; Initialize the section numbers with zeros or with what is found
3355 ;; in the toc entry.
3356 (let* ((level (or (nth 5 toc-entry) -1))
3357 (numbers (nreverse (split-string (or (nth 6 toc-entry) "") "\\.")))
3358 (depth (1- (length reftex-section-numbers)))
3359 (i depth))
3360 (while (>= i 0)
3361 (if (> i level)
3362 (aset reftex-section-numbers i 0)
3363 (aset reftex-section-numbers i (string-to-int (or (car numbers) "0")))
3364 (pop numbers))
3365 (decf i))))
2351 3366
2352 (defun reftex-section-number (section-numbers &optional level star) 3367 (defun reftex-section-number (section-numbers &optional level star)
2353 ;; Return a string with the current section number. 3368 ;; Return a string with the current section number.
2354 ;; When LEVEL is non-nil, increase section numbers on that level. 3369 ;; When LEVEL is non-nil, increase section numbers on that level.
2355 (let* ((depth 6) idx n (string "")) 3370 (let* ((depth (1- (length section-numbers))) idx n (string ""))
2356 (if level 3371 (when level
2357 (progn 3372 (when (and (> level -1) (not star))
2358 (if (and (> level -1) (not star)) 3373 (aset section-numbers level (1+ (aref section-numbers level))))
2359 (aset section-numbers level (1+ (aref section-numbers level)))) 3374 (setq idx (1+ level))
2360 (setq idx (1+ level)) 3375 (while (<= idx depth)
2361 (while (<= idx depth) 3376 (aset section-numbers idx 0)
2362 (aset section-numbers idx 0) 3377 (incf idx)))
2363 (setq idx (1+ idx)))))
2364 (setq idx 0) 3378 (setq idx 0)
2365 (while (<= idx depth) 3379 (while (<= idx depth)
2366 (setq n (aref section-numbers idx)) 3380 (setq n (aref section-numbers idx))
2367 (setq string (concat string (if (not (string= string "")) "." "") 3381 (setq string (concat string (if (not (string= string "")) "." "")
2368 (int-to-string n))) 3382 (int-to-string n)))
2369 (setq idx (1+ idx))) 3383 (incf idx))
2370 (save-match-data 3384 (save-match-data
2371 (if (string-match "\\`\\(0\\.\\)+" string) 3385 (if (string-match "\\`\\(0\\.\\)+" string)
2372 (setq string (replace-match "" nil nil string))) 3386 (setq string (replace-match "" nil nil string)))
2373 (if (string-match "\\(\\.0\\)+\\'" string) 3387 (if (string-match "\\(\\.0\\)+\\'" string)
2374 (setq string (replace-match "" nil nil string)))) 3388 (setq string (replace-match "" nil nil string))))
2375 (if star 3389 (if star
2376 (concat (make-string (1- (length string)) ?\ ) "*") 3390 (concat (make-string (1- (length string)) ?\ ) "*")
2377 string))) 3391 string)))
2378 3392
2379 ;; A variable to remember the index of the last label context shown 3393 ;; A variable to remember the index of the last label context shown
2380 (defvar reftex-last-cnt 0) 3394 (defvar reftex-last-cnt 0)
2381 3395
2382 (defun reftex-select-label-callback (cnt) 3396 (defun reftex-select-label-callback (cnt)
2383 ;; Callback function called from the label selection in order to 3397 ;; Callback function called from the label selection in order to
2384 ;; show context in another window 3398 ;; show context in another window
2385 (let* ((this-window (selected-window)) 3399 (let* ((this-window (selected-window))
2386 index entry label file buffer) 3400 index entry label file buffer re)
2387 ;; pop to original buffer in order to get correct variables 3401 ;; pop to original buffer in order to get correct variables
2388 (catch 'exit 3402 (catch 'exit
2389 (save-excursion 3403 (save-excursion
2390 (set-buffer reftex-call-back-to-this-buffer) 3404 (set-buffer reftex-call-back-to-this-buffer)
2391 (setq index (nth (or cnt 1) reftex-label-index-list) 3405 (setq index (nth (or cnt 1) reftex-label-index-list)
2392 entry (nth index (symbol-value reftex-list-of-labels-symbol)) 3406 entry (nth index (symbol-value reftex-docstruct-symbol))
2393 label (nth 0 entry) 3407 label (nth 0 entry)
2394 file (nth 3 entry))) 3408 file (nth 3 entry)))
2395 3409
2396 ;; goto the file in another window 3410 ;; goto the file in another window
2397 (setq buffer (reftex-get-file-buffer-force 3411 (setq buffer (reftex-get-file-buffer-force
2404 (ding) 3418 (ding)
2405 (throw 'exit nil)) 3419 (throw 'exit nil))
2406 3420
2407 3421
2408 ;; search for that label 3422 ;; search for that label
2409 (if (not (and (integerp cnt) 3423 (setq re (format reftex-find-label-regexp-format (regexp-quote label)))
2410 (integerp reftex-last-cnt) 3424 (unless (and (integerp cnt)
2411 (if (> cnt reftex-last-cnt) 3425 (integerp reftex-last-cnt)
2412 (search-forward (concat "\\label{" label "}") nil t) 3426 (if (> cnt reftex-last-cnt)
2413 (search-backward (concat "\\label{" label "}") nil t)))) 3427 (re-search-forward re nil t)
2414 (progn 3428 (re-search-backward re nil t)))
2415 (goto-char 1) 3429 (goto-char (point-min))
2416 (search-forward (concat "\\label{" label "}") nil t))) 3430 (unless (re-search-forward re nil t)
2417 (reftex-highlight 0 (match-beginning 0) (match-end 0)) 3431 ;; Ooops. Must be in a macro with distributed args.
2418 (reftex-show-entry) 3432 (re-search-forward (format reftex-find-label-regexp-format2
2419 (recenter (/ (window-height) 2)) 3433 (regexp-quote label)) nil t)))
3434 (when (match-end 3)
3435 (reftex-highlight 0 (match-beginning 3) (match-end 3))
3436 (reftex-show-entry (- (point) (match-beginning 3))
3437 (- (point) (match-end 3)))
3438 (recenter (/ (window-height) 2)))
2420 (select-window this-window)))) 3439 (select-window this-window))))
2421 3440
2422 (defun reftex-pop-to-label (label file-list &optional mark-to-kill highlight) 3441 (defun reftex-pop-to-label (label file-list &optional mark-to-kill highlight)
2423 ;; Find LABEL in any file in FILE-LIST in another window. 3442 ;; Find LABEL in any file in FILE-LIST in another window.
2424 ;; If mark-to-kill is non-nil, mark new buffer for killing. 3443 ;; If mark-to-kill is non-nil, mark new buffer for killing.
2425 ;; If HIGHLIGHT is non-nil, highlight the label definition. 3444 ;; If HIGHLIGHT is non-nil, highlight the label definition.
2426 (let* ((re (concat "\\\\label{" (regexp-quote label) "}")) 3445 (let* ((re1 (format reftex-find-label-regexp-format (regexp-quote label)))
3446 (re2 (format reftex-find-label-regexp-format2 (regexp-quote label)))
3447 (re-list (list re1 re2)) re
3448 (file-list-1 file-list)
2427 file buf) 3449 file buf)
2428 (catch 'exit 3450 (catch 'exit
2429 (while file-list 3451 (while (setq re (pop re-list))
2430 (setq file (car file-list) 3452 (setq file-list file-list-1)
2431 file-list (cdr file-list)) 3453 (while (setq file (pop file-list))
2432 (if (not (setq buf (reftex-get-file-buffer-force file mark-to-kill))) 3454 (unless (setq buf (reftex-get-file-buffer-force file mark-to-kill))
2433 (error "No such file %s" file)) 3455 (error "No such file %s" file))
2434 (set-buffer buf) 3456 (set-buffer buf)
2435 (widen) 3457 (widen)
2436 (goto-char (point-min)) 3458 (goto-char (point-min))
2437 (if (re-search-forward re nil t) 3459 (when (re-search-forward re nil t)
2438 (progn 3460 (switch-to-buffer-other-window buf)
2439 (switch-to-buffer-other-window buf) 3461 (goto-char (match-beginning 0))
2440 (goto-char (match-beginning 0)) 3462 (recenter (/ (window-height) 2))
2441 (recenter (/ (window-height) 2)) 3463 (if highlight
2442 (if highlight 3464 (reftex-highlight 0 (match-beginning 3) (match-end 3)))
2443 (reftex-highlight 0 (match-beginning 0) (match-end 0))) 3465 (throw 'exit (selected-window)))))
2444 (throw 'exit (selected-window)))))
2445 (error "Label %s not found" label)))) 3466 (error "Label %s not found" label))))
2446 3467
2447 (defun reftex-find-duplicate-labels () 3468 (defun reftex-find-duplicate-labels ()
2448 "Produce a list of all duplicate labels in the document." 3469 "Produce a list of all duplicate labels in the document."
2449 3470
2451 3472
2452 ;; Rescan the document to make sure 3473 ;; Rescan the document to make sure
2453 (reftex-access-scan-info t) 3474 (reftex-access-scan-info t)
2454 3475
2455 (let ((master (reftex-TeX-master-file)) 3476 (let ((master (reftex-TeX-master-file))
2456 (dlist 3477 (cnt 0)
3478 (dlist
2457 (mapcar 3479 (mapcar
2458 '(lambda(x) 3480 '(lambda(x)
2459 (let (x1) 3481 (let (x1)
2460 (cond 3482 (cond
2461 ((car x) 3483 ((memq (car x)
3484 '(toc bof eof bib label-numbers xr xr-doc
3485 master-dir file-error is-multi))
3486 nil)
3487 (t
2462 (setq x1 (reftex-all-assoc-string 3488 (setq x1 (reftex-all-assoc-string
2463 (car x) (symbol-value reftex-list-of-labels-symbol))) 3489 (car x) (symbol-value reftex-docstruct-symbol)))
2464 (if (< 1 (length x1)) 3490 (if (< 1 (length x1))
2465 (append (list (reftex-no-props (car x))) 3491 (append (list (car x))
2466 (mapcar '(lambda(x) 3492 (mapcar '(lambda(x)
2467 (abbreviate-file-name (nth 3 x))) x1)) 3493 (abbreviate-file-name (nth 3 x))) x1))
2468 (list nil))) 3494 (list nil))))))
2469 (t nil)))) 3495 (reftex-uniquify (symbol-value reftex-docstruct-symbol)))))
2470 (reftex-uniquify (symbol-value reftex-list-of-labels-symbol))))) 3496
2471 (setq dlist (reftex-uniquify dlist)) 3497 (setq dlist (reftex-uniquify dlist))
2472 (if (null dlist) (error "No duplicate labels in document")) 3498 (if (null dlist) (error "No duplicate labels in document"))
2473 (switch-to-buffer-other-window "*Duplicate Labels*") 3499 (switch-to-buffer-other-window "*Duplicate Labels*")
2474 (make-local-variable 'TeX-master) 3500 (make-local-variable 'TeX-master)
2475 (setq TeX-master master) 3501 (setq TeX-master master)
2476 (erase-buffer) 3502 (erase-buffer)
2477 (insert " MULTIPLE LABELS IN CURRENT DOCUMENT:\n") 3503 (insert " MULTIPLE LABELS IN CURRENT DOCUMENT:\n")
2478 (insert " Move point to label and type `M-x reftex-change-label'\n" 3504 (insert
2479 " This will run a query-replace on the label and its references\n") 3505 " Move point to label and type `r' to run a query-replace on the label\n"
3506 " and its references. Type `q' to exit this buffer.\n\n")
2480 (insert " LABEL FILE\n") 3507 (insert " LABEL FILE\n")
2481 (insert " -------------------------------------------------------------\n") 3508 (insert " -------------------------------------------------------------\n")
3509 (use-local-map (make-sparse-keymap))
3510 (local-set-key [?q] '(lambda () (interactive)
3511 (kill-buffer (current-buffer)) (delete-window)))
3512 (local-set-key [?r] 'reftex-change-label)
2482 (while dlist 3513 (while dlist
2483 (if (and (car (car dlist)) 3514 (when (and (car (car dlist))
2484 (cdr (car dlist))) 3515 (cdr (car dlist)))
2485 (progn 3516 (incf cnt)
2486 (insert (mapconcat '(lambda(x) x) (car dlist) "\n ") "\n"))) 3517 (insert (mapconcat '(lambda(x) x) (car dlist) "\n ") "\n"))
2487 (setq dlist (cdr dlist))) 3518 (pop dlist))
2488 (goto-char (point-min)))) 3519 (goto-char (point-min))
3520 (when (= cnt 0)
3521 (kill-buffer (current-buffer))
3522 (delete-window)
3523 (message "Document does not contain duplicate labels."))))
3524
3525 (defun reftex-all-assq (key list)
3526 ;; Return a list of all associations of KEY in LIST. Comparison with string=
3527 (let (rtn)
3528 (while (setq list (memq (assq key list) list))
3529 (push (car list) rtn)
3530 (pop list))
3531 (nreverse rtn)))
2489 3532
2490 (defun reftex-all-assoc-string (key list) 3533 (defun reftex-all-assoc-string (key list)
2491 ;; Return a list of all associations of KEY in LIST. Comparison with string= 3534 ;; Return a list of all associations of KEY in LIST. Comparison with string=
2492 (let (rtn) 3535 (let (rtn)
2493 (while list 3536 (while list
2494 (if (string= (car (car list)) key) 3537 (if (string= (car (car list)) key)
2495 (setq rtn (cons (car list) rtn))) 3538 (push (car list) rtn))
2496 (setq list (cdr list))) 3539 (pop list))
2497 (nreverse rtn))) 3540 (nreverse rtn)))
2498 3541
2499 (defun reftex-kill-temporary-buffers () 3542 (defun reftex-kill-temporary-buffers (&optional buffer)
2500 ;; Kill all buffers in the list reftex-kill-temporary-buffers. 3543 ;; Kill all buffers in the list reftex-kill-temporary-buffers.
2501 (while reftex-buffers-to-kill 3544 (cond
2502 (if (bufferp (car reftex-buffers-to-kill)) 3545 (buffer
2503 (progn 3546 (when (member buffer reftex-buffers-to-kill)
2504 (and (buffer-modified-p (car reftex-buffers-to-kill)) 3547 (kill-buffer buffer)
2505 (y-or-n-p (format "Save file %s? " 3548 (setq reftex-buffers-to-kill
2506 (buffer-file-name 3549 (delete buffer reftex-buffers-to-kill))))
2507 (car reftex-buffers-to-kill)))) 3550 (t
2508 (save-excursion 3551 (while (setq buffer (pop reftex-buffers-to-kill))
2509 (set-buffer (car reftex-buffers-to-kill)) 3552 (when (bufferp buffer)
2510 (save-buffer))) 3553 (and (buffer-modified-p buffer)
2511 (kill-buffer (car reftex-buffers-to-kill)))) 3554 (y-or-n-p (format "Save file %s? "
2512 (setq reftex-buffers-to-kill (cdr reftex-buffers-to-kill)))) 3555 (buffer-file-name buffer)))
2513 3556 (save-excursion
2514 (defun reftex-show-entry () 3557 (set-buffer buffer)
3558 (save-buffer)))
3559 (kill-buffer buffer))
3560 (pop reftex-buffers-to-kill)))))
3561
3562 (defun reftex-show-entry (beg-hlt end-hlt)
2515 ;; Show entry if point is hidden by outline mode 3563 ;; Show entry if point is hidden by outline mode
2516 (let ((pos (point))) 3564 (let* ((pos (point))
3565 (n (/ (window-height) 2))
3566 (beg (save-excursion
3567 (re-search-backward "[\n\r]" nil 1 n) (point)))
3568 (end (save-excursion
3569 (re-search-forward "[\n\r]" nil 1 n) (point))))
2517 (if (and reftex-auto-show-entry 3570 (if (and reftex-auto-show-entry
2518 (boundp 'outline-minor-mode) 3571 (string-match
2519 outline-minor-mode 3572 "\r" (buffer-substring beg end)))
2520 (looking-at "[^\n\r]*\r")) 3573 (cond
2521 (progn 3574 ((eq t reftex-auto-show-entry)
2522 (outline-back-to-heading) 3575 (subst-char-in-region
2523 (show-entry) 3576 (save-excursion (search-backward "\n" nil t) (point))
2524 (goto-char pos))))) 3577 (save-excursion (search-forward "\n" nil t) (point))
2525 3578 ?\r ?\n t))
3579 ((eq reftex-auto-show-entry 'copy)
3580 (let ((string (buffer-substring beg end)))
3581 (switch-to-buffer "*RefTeX Context Copy*")
3582 (setq buffer-read-only nil)
3583 (erase-buffer)
3584 (insert string)
3585 (subst-char-in-region (point-min) (point-max) ?\r ?\n t)
3586 (goto-char (- pos beg))
3587 (reftex-highlight 0 (1+ (- (point) beg-hlt))
3588 (1+ (- (point) end-hlt)))
3589 (when (reftex-refontify)
3590 (make-local-variable 'font-lock-defaults)
3591 (setq font-lock-defaults nil)
3592 (let ((major-mode 'latex-mode))
3593 (font-lock-set-defaults)
3594 (font-lock-fontify-buffer)))
3595 (setq buffer-read-only t)))
3596 ))))
2526 3597
2527 (defun reftex-nicify-text (text) 3598 (defun reftex-nicify-text (text)
2528 ;; Make TEXT nice for inclusion into label menu 3599 ;; Make TEXT nice for inclusion as context into label menu
2529 (while (string-match "[\n\r\t]\\|[ \t][ \t]+" text) ; remove extra whitespace 3600 (while (string-match "[\n\r\t]\\|[ \t][ \t]+" text) ; remove extra whitespace
2530 (setq text (replace-match " " nil t text))) 3601 (setq text (replace-match " " nil t text)))
2531 (if (string-match "\\\\end{.*" text) ; nothing beyond \end{ 3602 (if (string-match "\\\\end{.*" text) ; nothing beyond \end{
2532 (setq text (replace-match "" nil t text))) 3603 (setq text (replace-match "" nil t text)))
2533 (if (string-match "\\\\label{[^}]*}" text) ; kill the label 3604 (if (string-match "\\\\label{[^}]*}" text) ; kill the label
2534 (setq text (replace-match "" nil t text))) 3605 (setq text (replace-match "" nil t text)))
2535 (if (string-match "^ +" text) ; leading whitespace 3606 (if (string-match "\\`[ }]+" text) ; leading whitespace, `}'
2536 (setq text (replace-match "" nil t text))) 3607 (setq text (replace-match "" nil t text)))
2537 (cond 3608 (cond
2538 ((> (length text) 100) ; not to long 3609 ((> (length text) 100) (substring text 0 100))
2539 (setq text (substring text 0 100))) 3610 ((= (length text) 0) " ")
2540 ((= (length text) 0) ; not empty 3611 (t text)))
2541 (setq text " ")))
2542 text)
2543 3612
2544 (defun reftex-typekey-check (typekey conf-variable &optional n) 3613 (defun reftex-typekey-check (typekey conf-variable &optional n)
2545 ;; Check if CONF-VARIABLE is true or contains TYPEKEY 3614 ;; Check if CONF-VARIABLE is true or contains TYPEKEY
2546 (and n (setq conf-variable (nth n conf-variable))) 3615 (and n (setq conf-variable (nth n conf-variable)))
2547 (or (equal conf-variable t) 3616 (or (eq conf-variable t)
2548 (and (stringp conf-variable) 3617 (and (stringp conf-variable)
2549 (string-match (concat "[" conf-variable "]") typekey)))) 3618 (string-match (concat "[" conf-variable "]") typekey))))
2550 3619
2551 ;;; =========================================================================== 3620 ;;; ===========================================================================
2552 ;;; 3621 ;;;
2553 ;;; Table of contents (contributed from Stephen Eglen, changed by C. Dominik) 3622 ;;; Table of contents
2554 3623
2555 ;; We keep at most one *toc* buffer - it is easy to make them 3624 ;; We keep at most one *toc* buffer - it is easy to make them
2556 3625
2557 (defvar reftex-last-toc-master nil 3626 (defvar reftex-last-toc-master nil
2558 "Stores the name of the tex file that `reftex-toc' was last run on.") 3627 "Stores the name of the tex file that `reftex-toc' was last run on.")
2559 3628
2560 (defvar reftex-last-toc-file nil 3629 (defvar reftex-last-toc-file nil
2561 "Stores the file name from which `reftex-toc' was called. For redo command.") 3630 "Stores the file name from which `reftex-toc' was called. For redo command.")
2562 3631
3632 (defvar reftex-last-window-height nil)
3633
2563 (defvar reftex-toc-return-marker (make-marker) 3634 (defvar reftex-toc-return-marker (make-marker)
2564 "Marker which makes it possible to return from toc to old position.") 3635 "Marker which makes it possible to return from toc to old position.")
2565 3636
3637 (defconst reftex-toc-help
3638 " AVAILABLE KEYS IN TOC BUFFER
3639 ============================
3640 SPC Show the corresponding section of the LaTeX document.
3641 TAB Goto the section.
3642 RET Goto the section and hide the *toc* buffer (also on mouse-2).
3643 q / Q Hide/Kill *toc* buffer, return to position of last reftex-toc command.
3644 f Toggle follow mode on and off.
3645 r / g Reparse the LaTeX document.
3646 x Switch to TOC of external document (with LaTeX package `xr').")
3647
2566 (defun reftex-toc () 3648 (defun reftex-toc ()
2567 "Show the table of contents for the current document. 3649 "Show the table of contents for the current document.
2568 To see the corresponding part of the LaTeX document, use within the
2569 *toc* buffer:
2570
2571 SPC Show the corresponding section of the LaTeX document.
2572 RET Goto the section and hide the *toc* buffer.
2573 q Hide the *toc* window and return to position of last reftex-toc command.
2574 Q Kill the *toc* buffer and return to position of last reftex-toc command.
2575 f Toggle follow mode on and off.
2576 r Reparse the LaTeX document.
2577 g Revert buffer (like `r').
2578
2579 When called with a raw C-u prefix, rescan the document first." 3650 When called with a raw C-u prefix, rescan the document first."
2580 3651
2581 (interactive) 3652 (interactive)
2582 3653
2583 (and (not (string= reftex-last-toc-master (reftex-TeX-master-file))) 3654 (if (or (not (string= reftex-last-toc-master (reftex-TeX-master-file)))
2584 (get-buffer "*toc*") 3655 current-prefix-arg)
2585 (kill-buffer "*toc*")) 3656 (reftex-empty-toc-buffer))
2586 3657
2587 (setq reftex-last-toc-file (buffer-file-name)) 3658 (setq reftex-last-toc-file (buffer-file-name))
2588 (setq reftex-last-toc-master (reftex-TeX-master-file)) 3659 (setq reftex-last-toc-master (reftex-TeX-master-file))
2589 3660
2590 (set-marker reftex-toc-return-marker (point)) 3661 (set-marker reftex-toc-return-marker (point))
2591 3662
2592 ;; if follow mode is active, arrange to delay it one command 3663 ;; If follow mode is active, arrange to delay it one command
2593 (if reftex-toc-follow-mode 3664 (if reftex-toc-follow-mode
2594 (setq reftex-toc-follow-mode 1)) 3665 (setq reftex-toc-follow-mode 1))
2595 3666
2596 (if (and current-prefix-arg
2597 (get-buffer "*toc*"))
2598 (kill-buffer "*toc*"))
2599
2600 ;; Ensure access to scanning info and rescan buffer if prefix are is '(4) 3667 ;; Ensure access to scanning info and rescan buffer if prefix are is '(4)
2601 (reftex-access-scan-info current-prefix-arg) 3668 (reftex-access-scan-info current-prefix-arg)
2602 3669
2603 (let* ((all (symbol-value reftex-list-of-labels-symbol)) 3670 (let* ((all (symbol-value reftex-docstruct-symbol))
3671 (xr-data (assq 'xr all))
3672 (xr-alist (cons (cons "" (buffer-file-name)) (nth 1 xr-data)))
2604 (where (reftex-nearest-section)) 3673 (where (reftex-nearest-section))
2605 toc toc1 cell label file find startpos) 3674 toc1 cell startpos)
2606 3675
2607 (if (and (get-buffer "*toc*") 3676 (if (get-buffer-window "*toc*")
2608 (get-buffer-window (get-buffer "*toc*"))) 3677 (select-window (get-buffer-window "*toc*"))
2609 (select-window (get-buffer-window (get-buffer "*toc*"))) 3678 (setq reftex-last-window-height (window-height)) ; remember
2610 (delete-other-windows) 3679 (split-window-vertically)
2611 (switch-to-buffer-other-window (current-buffer)) 3680 (switch-to-buffer (get-buffer-create "*toc*")))
2612 (switch-to-buffer-other-window (get-buffer-create "*toc*")))
2613 3681
2614 (cond 3682 (cond
2615 ;; buffer is empty - fill it with the table of contents 3683 ;; buffer is empty - fill it with the table of contents
2616 ((= (buffer-size) 0) 3684 ((= (buffer-size) 0)
2617 3685
2618 (local-set-key " " 'reftex-toc-view-line) 3686 (local-set-key "?" 'reftex-toc-show-help)
2619 (local-set-key "\C-m" 'reftex-toc-goto-line-and-hide) 3687 (local-set-key " " 'reftex-toc-view-line)
2620 (local-set-key "r" 'reftex-toc-redo) 3688 (local-set-key "\C-m" 'reftex-toc-goto-line-and-hide)
2621 (local-set-key "g" 'revert-buffer) 3689 (local-set-key "\C-i" 'reftex-toc-goto-line)
2622 (local-set-key "q" 'reftex-toc-quit) 3690 (local-set-key "r" 'reftex-toc-redo)
2623 (local-set-key "Q" 'reftex-toc-quit-and-kill) 3691 (local-set-key "R" 'reftex-toc-Redo)
2624 (local-set-key "f" 'reftex-toc-toggle-follow) 3692 (local-set-key "g" 'revert-buffer)
3693 (local-set-key "q" 'reftex-toc-quit)
3694 (local-set-key "Q" 'reftex-toc-quit-and-kill)
3695 (local-set-key "f" 'reftex-toc-toggle-follow)
3696 (local-set-key "x" 'reftex-toc-external)
3697 (local-set-key [(mouse-2)] 'reftex-toc-mouse-goto-line-and-hide); Emacs
3698 (local-set-key [(button2)] 'reftex-toc-mouse-goto-line-and-hide); XEmacs
2625 (make-local-variable 'revert-buffer-function) 3699 (make-local-variable 'revert-buffer-function)
2626 (setq revert-buffer-function 'reftex-toc-redo) 3700 (setq revert-buffer-function 'reftex-toc-redo)
2627 (setq truncate-lines t) 3701 (setq truncate-lines t)
2628 (make-local-hook 'post-command-hook) 3702 (make-local-hook 'post-command-hook)
2629 (make-local-hook 'pre-command-hook) 3703 (make-local-hook 'pre-command-hook)
2630 (setq post-command-hook '(reftex-toc-post-command-hook)) 3704 (setq post-command-hook '(reftex-toc-post-command-hook))
2631 (setq pre-command-hook '(reftex-toc-pre-command-hook)) 3705 (setq pre-command-hook '(reftex-toc-pre-command-hook))
2632 3706
2633 (insert (format 3707 (insert (format
2634 "TABLE-OF-CONTENTS on %s 3708 "TABLE-OF-CONTENTS on %s
2635 MENU: SPC=view RET=goto [q]uit [Q]uit+kill [r]escan [f]ollow-mode 3709 SPC=view TAB=goto RET=goto+hide [q]uit [r]escan [f]ollow-mode e[x]tern [?]Help
2636 ------------------------------------------------------------------------------- 3710 -------------------------------------------------------------------------------
2637 " (abbreviate-file-name reftex-last-toc-master))) 3711 " (abbreviate-file-name reftex-last-toc-master)))
2638 (setq startpos (point)) 3712 (setq startpos (point))
2639 3713
2640 (if (reftex-use-fonts) 3714 (if (reftex-use-fonts)
2641 (put-text-property 1 (point) 'face 'font-lock-keyword-face)) 3715 (put-text-property 1 (point) 'face 'font-lock-keyword-face))
2642 (put-text-property 1 (point) 'intangible t) 3716 (put-text-property 1 (point) 'intangible t)
3717 (put-text-property 1 2 'xr-alist xr-alist)
2643 3718
2644 (while all 3719 (while all
2645 (setq cell (car all) 3720 (setq cell (car all)
2646 all (cdr all)) 3721 all (cdr all))
2647 (setq label (nth 0 cell) 3722 (when (eq (car cell) 'toc)
2648 toc (nth 2 cell) 3723 (setq toc1 (concat (nth 2 cell) "\n"))
2649 file (nth 3 cell) 3724 (put-text-property 0 (length toc1) 'toc cell toc1)
2650 find (nth 4 cell)) 3725 (insert toc1)))
2651 (if (not label)
2652 (progn
2653 (setq toc1 (concat toc "\n"))
2654 (put-text-property 0 (length toc1)
2655 'file file toc1)
2656 (put-text-property 0 (length toc1)
2657 'find find toc1)
2658 (insert toc1)
2659 )))
2660 3726
2661 (backward-delete-char 1) 3727 (backward-delete-char 1)
2662 3728
2663 (setq buffer-read-only t)) 3729 (setq buffer-read-only t))
2664 (t 3730 (t
2668 3734
2669 ;; Find the correct section 3735 ;; Find the correct section
2670 (goto-char (point-max)) 3736 (goto-char (point-max))
2671 (beginning-of-line) 3737 (beginning-of-line)
2672 (while (and (> (point) startpos) 3738 (while (and (> (point) startpos)
2673 (or (not (string= (get-text-property (point) 'file) 3739 (not (eq (get-text-property (point) 'toc) where)))
2674 (car where)))
2675 (not (string= (get-text-property (point) 'find)
2676 (cdr where)))))
2677 (beginning-of-line 0)))) 3740 (beginning-of-line 0))))
2678 3741
2679 (defun reftex-nearest-section () 3742 (defun reftex-nearest-section ()
2680 ;; Return (file . find) of nearest section command 3743 ;; Return (file . find) of nearest section command
2681 (let (cell label rest) 3744 (let* ((here-I-am (car (reftex-where-am-I))))
2682 (save-excursion 3745 (reftex-last-assoc-before-elt
2683 (cond 3746 'toc here-I-am (symbol-value reftex-docstruct-symbol))))
2684 ;; Try to find a section heading
2685 ((or (re-search-backward reftex-section-regexp nil t)
2686 (re-search-forward reftex-section-regexp nil t))
2687 (goto-char (match-end 0))
2688 (cons (buffer-file-name)
2689 (reftex-allow-for-ctrl-m
2690 (concat (buffer-substring-no-properties
2691 (1- (match-beginning 1)) (match-end 0))
2692 (reftex-context-substring)))))
2693 ;; Try to find a label
2694 ((and (or (re-search-backward "\\\\label{\\([^}]+\\)}" nil t)
2695 (re-search-forward "\\\\label{\\([^}]+\\)}" nil t))
2696 (setq label (reftex-no-props (match-string 1)))
2697 (setq cell (assoc label (symbol-value
2698 reftex-list-of-labels-symbol)))
2699 (setq rest (memq cell (symbol-value reftex-list-of-labels-symbol)))
2700 (setq cell (car (memq (assoc nil rest) rest)))
2701 (null (car cell)))
2702 (cons (nth 3 cell) (nth 4 cell)))
2703 (t nil)))))
2704 3747
2705 (defun reftex-toc-pre-command-hook () 3748 (defun reftex-toc-pre-command-hook ()
2706 ;; used as pre command hook in *toc* buffer 3749 ;; used as pre command hook in *toc* buffer
2707 (reftex-unhighlight 0) 3750 (reftex-unhighlight 0)
2708 (reftex-unhighlight 1)) 3751 (reftex-unhighlight 1))
2709 3752
2710 (defun reftex-toc-post-command-hook () 3753 (defun reftex-toc-post-command-hook ()
2711 ;; used in the post-command-hook for the *toc* buffer 3754 ;; used in the post-command-hook for the *toc* buffer
2712 (and (> (point) 1) 3755 (and (> (point) 1)
2713 (save-excursion 3756 (save-excursion
2714 (reftex-highlight 1 3757 (reftex-highlight 1
2715 (progn (beginning-of-line) (point)) 3758 (progn (beginning-of-line) (point))
2716 (progn (end-of-line) (point))))) 3759 (progn (end-of-line) (point)))))
2717 (cond 3760 (cond
2718 ((integerp reftex-toc-follow-mode) 3761 ((integerp reftex-toc-follow-mode)
2719 ;; remove delayed action 3762 ;; remove delayed action
2720 (setq reftex-toc-follow-mode t)) 3763 (setq reftex-toc-follow-mode t))
2721 (reftex-toc-follow-mode 3764 (reftex-toc-follow-mode
2722 ;; show context in other window 3765 ;; show context in other window
2723 (condition-case nil 3766 (condition-case nil
2724 (reftex-toc-visit-line) 3767 (reftex-toc-visit-line)
2725 ('error t))))) 3768 (error (ding) t)))))
3769
3770 (defun reftex-empty-toc-buffer ()
3771 (if (get-buffer "*toc*")
3772 (save-excursion
3773 (set-buffer "*toc*")
3774 (setq buffer-read-only nil)
3775 (erase-buffer))))
3776
3777 (defun reftex-re-enlarge ()
3778 (enlarge-window
3779 (max 0 (- (or reftex-last-window-height (window-height))
3780 (window-height)))))
3781
3782 (defun reftex-toc-show-help ()
3783 (interactive)
3784 (with-output-to-temp-buffer "*RefTeX Help*"
3785 (princ reftex-toc-help))
3786 ;; If follow mode is active, arrange to delay it one command
3787 (if reftex-toc-follow-mode
3788 (setq reftex-toc-follow-mode 1)))
2726 3789
2727 (defun reftex-toc-toggle-follow () 3790 (defun reftex-toc-toggle-follow ()
2728 "Toggle toc-follow mode. 3791 "Toggle toc-follow mode.
2729 (It is not really a mode, just a flag)." 3792 (It is not really a mode, just a flag)."
2730 (interactive) 3793 (interactive)
2735 (reftex-toc-visit-line)) 3798 (reftex-toc-visit-line))
2736 (defun reftex-toc-goto-line-and-hide () 3799 (defun reftex-toc-goto-line-and-hide ()
2737 "Go to document location in other window. Hide the *toc* window." 3800 "Go to document location in other window. Hide the *toc* window."
2738 (interactive) 3801 (interactive)
2739 (reftex-toc-visit-line 'hide)) 3802 (reftex-toc-visit-line 'hide))
3803 (defun reftex-toc-goto-line ()
3804 "Go to document location in other window. Hide the *toc* window."
3805 (interactive)
3806 (reftex-toc-visit-line t))
3807 (defun reftex-toc-mouse-goto-line-and-hide (ev)
3808 "Go to document location in other window. Hide the *toc* window."
3809 (interactive "e")
3810 (mouse-set-point ev)
3811 (reftex-toc-visit-line 'hide))
2740 (defun reftex-toc-quit () 3812 (defun reftex-toc-quit ()
2741 "Hide the *toc* window and do not move point." 3813 "Hide the *toc* window and do not move point."
2742 (interactive) 3814 (interactive)
2743 (delete-window) 3815 (or (one-window-p) (delete-window))
2744 (switch-to-buffer (marker-buffer reftex-toc-return-marker)) 3816 (switch-to-buffer (marker-buffer reftex-toc-return-marker))
2745 (goto-char (marker-position reftex-toc-return-marker))) 3817 (reftex-re-enlarge)
3818 (goto-char (or (marker-position reftex-toc-return-marker) (point))))
2746 (defun reftex-toc-quit-and-kill () 3819 (defun reftex-toc-quit-and-kill ()
2747 "Kill the *toc* buffer." 3820 "Kill the *toc* buffer."
2748 (interactive) 3821 (interactive)
2749 (kill-buffer "*toc*") 3822 (kill-buffer "*toc*")
2750 (delete-window) 3823 (or (one-window-p) (delete-window))
2751 (switch-to-buffer (marker-buffer reftex-toc-return-marker)) 3824 (switch-to-buffer (marker-buffer reftex-toc-return-marker))
3825 (reftex-re-enlarge)
2752 (goto-char (marker-position reftex-toc-return-marker))) 3826 (goto-char (marker-position reftex-toc-return-marker)))
2753 (defun reftex-toc-redo (&rest ignore) 3827 (defun reftex-toc-redo (&rest ignore)
2754 "Regenerate the *toc* buffer. Call only from within the *toc* buffer" 3828 "Regenerate the *toc* buffer by reparsing file of last reftex-toc command."
2755 (interactive) 3829 (interactive)
2756 (switch-to-buffer (reftex-get-file-buffer-force reftex-last-toc-file)) 3830 (if reftex-enable-partial-scans
2757 (delete-other-windows) 3831 (let ((file (nth 3 (get-text-property (point) 'toc))))
2758 (setq current-prefix-arg '(4)) 3832 (if (not file)
3833 (error "Don't know which file to rescan. Try `R'.")
3834 (switch-to-buffer-other-window
3835 (reftex-get-file-buffer-force file))
3836 (setq current-prefix-arg '(4))
3837 (reftex-toc)))
3838 (reftex-toc-Redo))
3839 (reftex-kill-temporary-buffers))
3840 (defun reftex-toc-Redo (&rest ignore)
3841 "Regenerate the *toc* buffer by reparsing the entire document."
3842 (interactive)
3843 (switch-to-buffer-other-window
3844 (reftex-get-file-buffer-force reftex-last-toc-file))
3845 (setq current-prefix-arg '(16))
2759 (reftex-toc)) 3846 (reftex-toc))
3847 (defun reftex-toc-external (&rest ignore)
3848 "Switch to table of contents of an external document."
3849 (interactive)
3850 (let* ((xr-alist (get-text-property 1 'xr-alist))
3851 (xr-index (reftex-select-external-document
3852 xr-alist 0)))
3853 (switch-to-buffer-other-window (or (reftex-get-file-buffer-force
3854 (cdr (nth xr-index xr-alist)))
3855 (error "Cannot switch document")))
3856 (reftex-toc)))
2760 3857
2761 (defun reftex-toc-visit-line (&optional final) 3858 (defun reftex-toc-visit-line (&optional final)
2762 ;; Visit the tex file corresponding to the toc entry on the current line. 3859 ;; Visit the tex file corresponding to the toc entry on the current line.
2763 ;; If FINAL is t, stay there 3860 ;; If FINAL is t, stay there
2764 ;; If FINAL is 'hide, hide the *toc* window. 3861 ;; If FINAL is 'hide, hide the *toc* window.
2765 ;; Otherwise, move cursor back into *toc* window 3862 ;; Otherwise, move cursor back into *toc* window.
2766 3863 ;; This function is pretty clever about finding back a section heading,
2767 (let (file find beg end (toc-window (selected-window)) show-window) 3864 ;; even if the buffer is not live, or things like outline, x-symbol etc.
2768 (save-excursion 3865 ;; have been active.
2769 (beginning-of-line) 3866
2770 (setq beg (point)) 3867 (let* ((toc (get-text-property (point) 'toc))
2771 (end-of-line) 3868 (file (nth 3 toc))
2772 (setq end (point))) 3869 (marker (nth 4 toc))
2773 3870 (level (nth 5 toc))
2774 ;; get the file and the search string 3871 (literal (nth 7 toc))
2775 (setq file (get-text-property (point) 'file)) 3872 (emergency-point (nth 8 toc))
2776 (setq find (get-text-property (point) 'find)) 3873 (toc-window (selected-window))
2777 (if (or (not file) (not find)) 3874 show-window show-buffer match)
2778 (error "Cannot visit line")) 3875
2779 3876 (unless toc (error "Don't know which toc line to visit"))
2780 (switch-to-buffer-other-window (reftex-get-file-buffer-force file)) 3877
2781 (setq show-window (selected-window)) 3878 (setq match
2782 (goto-char (point-min)) 3879 (cond
2783 3880 ((and (markerp marker) (marker-buffer marker))
2784 (if (not (re-search-forward find nil t)) 3881 ;; Buffer is still live and we have the marker. Should be easy.
2785 (error "Cannot visit line")) 3882 (switch-to-buffer-other-window (marker-buffer marker))
2786 3883 (goto-char (marker-position marker))
2787 (setq beg (match-beginning 0) 3884 (or (looking-at (regexp-quote literal))
2788 end (match-end 0)) 3885 (looking-at (reftex-make-regexp-allow-for-ctrl-m literal))
2789 3886 (looking-at (reftex-make-desparate-section-regexp literal))
2790 (goto-char beg) 3887 (looking-at (concat "\\\\"
3888 (regexp-quote
3889 (car (rassq level reftex-section-levels)))
3890 "[[{]"))))
3891 (t
3892 ;; Marker is lost. Use the backup method.
3893 (switch-to-buffer-other-window
3894 (reftex-get-file-buffer-force file nil))
3895 (goto-char (or emergency-point (point-min)))
3896 (or (looking-at (regexp-quote literal))
3897 (let ((pos (point)))
3898 (re-search-backward "\\`\\|[\r\n][ \t]*[\r\n]" nil t)
3899 (or (reftex-nearest-match (regexp-quote literal) pos)
3900 (reftex-nearest-match
3901 (reftex-make-regexp-allow-for-ctrl-m literal) pos)
3902 (reftex-nearest-match
3903 (reftex-make-desparate-section-regexp literal) pos)))))
3904 ))
3905
3906 (setq show-window (selected-window)
3907 show-buffer (current-buffer))
3908
3909 (unless match
3910 (select-window toc-window)
3911 (error "Cannot find line"))
3912
3913 (goto-char (match-beginning 0))
2791 (recenter 1) 3914 (recenter 1)
2792 (reftex-highlight 0 beg end (current-buffer)) 3915 (reftex-highlight 0 (match-beginning 0) (match-end 0) (current-buffer))
2793 3916
2794 (select-window toc-window) 3917 (select-window toc-window)
2795 3918
2796 ;; use the `final' parameter to decide what to do next 3919 ;; use the `final' parameter to decide what to do next
2797 (cond 3920 (cond
2798 ((equal final t) 3921 ((eq final t)
2799 (reftex-unhighlight 0) 3922 (reftex-unhighlight 0)
2800 (select-window show-window)) 3923 (select-window show-window))
2801 ((eq final 'hide) 3924 ((eq final 'hide)
2802 (reftex-unhighlight 0) 3925 (reftex-unhighlight 0)
2803 (delete-window)) 3926 (or (one-window-p) (delete-window))
3927 (switch-to-buffer show-buffer)
3928 (reftex-re-enlarge))
2804 (t nil)))) 3929 (t nil))))
2805 3930
2806 ;;; =========================================================================== 3931 ;;; ===========================================================================
2807 ;;; 3932 ;;;
2808 ;;; BibTeX citations. 3933 ;;; BibTeX citations.
2809 3934
2810 ;; Variables and constants 3935 ;; Variables and constants
2811 3936
2812 ;; Define variable to silence compiler warnings 3937 ;; Define variable to silence compiler warnings
2813 (defvar reftex-found-list) 3938 (defvar reftex-found-list)
2814 3939 (defvar reftex-cite-format-builtin)
2815 ;; Internal variable, but used from different functions
2816 (defvar reftex-cite-format1 nil)
2817 3940
2818 ;; The history list of regular expressions used for citations 3941 ;; The history list of regular expressions used for citations
2819 (defvar reftex-cite-regexp-hist nil) 3942 (defvar reftex-cite-regexp-hist nil)
2820 3943
2821 ;; Help string for citation selection 3944 ;; Prompt and help string for citation selection
3945 (defconst reftex-citation-prompt
3946 "Select: [n]ext [p]revious [r]estrict [ ]full_entry [q]uit RET [?]Help+more")
3947
2822 (defconst reftex-citation-help 3948 (defconst reftex-citation-help
2823 "AVAILABLE KEYS IN MAKE CITATION MENU 3949 "AVAILABLE KEYS IN MAKE CITATION MENU
2824 --------------------------------------- 3950 ---------------------------------------
2825 n / p Go to next/previous entry (Cursor motion works as well). 3951 n / p Go to next/previous entry (Cursor motion works as well).
2826 r Restrict selection with another regexp. 3952 C-s / C-r Search forward/backward. Use repeated C-s/C-r as in isearch.
2827 SPACE Show full database entry in other window. 3953 g / r Start over with new regexp / Restrict with additional regexp.
2828 f Toggle follow mode: Other window will follow with full db entry. 3954 SPC Show full database entry in other window.
2829 q Quit without inserting \\cite macro into buffer. 3955 f Toggle follow mode: Other window will follow with full db entry.
2830 ? Display this help message. 3956 q Quit without inserting \\cite macro into buffer.
2831 C-r Recursive edit into other window. 3957 e Recursive edit into other window.
2832 RETURN ... Accept current entry and insert in format according to 3958 RET / a Accept current entry / Accept all entries.")
2833 `reftex-cite-format'")
2834
2835 (defconst reftex-cite-format-default "\\cite{KEY}"
2836 "The default value for reftex-cite-format.
2837 Uses the string version of `reftex-cite-format'.")
2838
2839 (defconst reftex-cite-format-1-author-simple
2840 '( "\\cite{KEY}" "AUTHOR \\cite{KEY}" "AUTHOR {\it et al.} \\cite{KEY}")
2841 "Value for reftex-cite format establishing a simple citation with name
2842 of the first author.
2843 Uses the list version of `reftex-cite-format'.")
2844
2845 (defconst reftex-cite-format-2-authors
2846 '((?\C-m
2847 . ( "\\cite{KEY}" "AUTHOR \\cite{KEY}"
2848 "AUTHOR \\& AUTHOR \\cite{KEY}" "AUTHOR \\etal{} \\cite{KEY}"))
2849 (?\,
2850 . ("\\cite{KEY}" "AUTHOR, \\cite{KEY}"
2851 "AUTHOR \\& AUTHOR, \\cite{KEY}" "AUTHOR \\etal{}, \\cite{KEY}"))
2852 (?\;
2853 . ("\\cite{KEY}" "AUTHOR; \\cite{KEY}"
2854 "AUTHOR \\& AUTHOR; \\cite{KEY}" "AUTHOR \\etal{}; \\cite{KEY}"))
2855 (?\:
2856 . ("\\cite{KEY}" "AUTHOR: \\cite{KEY}"
2857 "AUTHOR \\& AUTHOR: \\cite{KEY}" "AUTHOR \\etal{}: \\cite{KEY}"))
2858 (?\(
2859 . ("(\\cite{KEY})" "AUTHOR (\\cite{KEY})"
2860 "AUTHOR \\& AUTHOR (\\cite{KEY})" "AUTHOR \\etal{} (\\cite{KEY})"))
2861 (?\[
2862 . ("[\\cite{KEY}]" "AUTHOR [\\cite{KEY}]"
2863 "AUTHOR \\& AUTHOR [\\cite{KEY}]" "AUTHOR \\etal{} [\\cite{KEY}]")))
2864 "Value for `reftex-cite-format' that estabishes an Author/Year citation
2865 where the year is supplied from BibTeX. Depending on which character
2866 is used during selection to accept the label, an extra ,;: or pair of
2867 parenthesis will be inserted.
2868 Uses the list-of-cons-cells version of `reftex-cite-format'.")
2869 3959
2870 ;; Find bibtex files 3960 ;; Find bibtex files
2871 3961
2872 (defun reftex-get-bibfile-list () 3962 (defun reftex-get-bibfile-list ()
2873 ;; Return list of bibfiles for current document 3963 ;; Return list of bibfiles for current document.
3964 ;; When using the chapterbib or bibunits package you should either
3965 ;; use the same database files everywhere, or separate parts using
3966 ;; different databases into different files (included into the mater file).
3967 ;; Then this function will return the applicable database files.
2874 3968
2875 ;; Ensure access to scanning info 3969 ;; Ensure access to scanning info
2876 (reftex-access-scan-info) 3970 (reftex-access-scan-info)
2877 3971 (or
2878 (or (symbol-value reftex-bibfile-list-symbol) 3972 ;; Try inside this file (and its includes)
2879 (error "No BibTeX files to parse. Add \\bibliography statment to document and reparse."))) 3973 (cdr (reftex-last-assoc-before-elt
2880 3974 'bib (list 'eof (buffer-file-name))
2881 (defun reftex-scan-buffer-for-bibliography-statement (bib-list-symbol) 3975 (member (list 'bof (buffer-file-name))
2882 ;; Scan buffer for bibliography macro and store file list in bib-list-symbol. 3976 (symbol-value reftex-docstruct-symbol))))
2883 (let (file-list dir-list) 3977 ;; Try after the beginning of this file
2884 (setq dir-list 3978 (cdr (assq 'bib (member (list 'bof (buffer-file-name))
2885 (reftex-split 3979 (symbol-value reftex-docstruct-symbol))))
2886 (concat path-separator "+") 3980 ;; Anywhere in the entire document
2887 (mapconcat '(lambda(x) 3981 (cdr (assq 'bib (symbol-value reftex-docstruct-symbol)))
2888 (if (getenv x) (getenv x) "")) 3982 (error "\\bibliography statment missing or .bib files not found.")))
2889 reftex-bibpath-environment-variables 3983
2890 path-separator))) 3984 (defun reftex-find-tex-file (file master-dir &optional die)
2891 (goto-char (point-min)) 3985 ;; Find FILE in MASTER-DIR or on reftex-tex-path.
2892 (if (re-search-forward "^[ \t]*\\\\bibliography{[ \t]*\\([^}]+\\)" nil t) 3986 ;; FILE may be given without the .tex extension.
2893 (progn 3987 (reftex-access-search-path "tex")
2894 (setq dir-list 3988 (let* ((path (cons master-dir reftex-tex-path))
2895 (cons (file-name-directory 3989 file1)
2896 (get-text-property (match-beginning 0) 'file)) 3990 (setq file1
2897 dir-list)) 3991 (or (reftex-find-file-on-path (concat file ".tex") path)
2898 (setq file-list 3992 (reftex-find-file-on-path file path)))
2899 (mapcar '(lambda (x) (concat x ".bib")) 3993 (unless file1
2900 (reftex-delete-list 3994 (reftex-access-search-path "tex" t file)
2901 reftex-bibfile-ignore-list 3995 (setq path (cons master-dir reftex-tex-path))
2902 (reftex-split 3996 (setq file1
2903 "[ \t\n]*,[ \t\n]*" 3997 (or (reftex-find-file-on-path (concat file ".tex") path)
2904 (reftex-no-props (match-string 1))))))) 3998 (reftex-find-file-on-path file path))))
2905 (message "No \\bibliography command in document.")) 3999 (cond (file1 file1)
2906 (set bib-list-symbol 4000 (die (error "No such file: %s" file) nil)
2907 (if file-list 4001 (t (message "No such file: %s (ignored)" file) nil))))
2908 (reftex-find-files-on-path file-list dir-list 4002
2909 "While parsing \\bibliography:") 4003 (defun reftex-find-bib-file (file master-dir &optional die)
2910 nil)))) 4004 ;; Find FILE in MASTER-DIR or on reftex-bib-path
2911 4005 (reftex-access-search-path "bib")
2912 (defun reftex-find-files-on-path (file-list path-list &optional error-string) 4006 (let ((file1 (reftex-find-file-on-path
2913 ;; Search for all files in FILE-LIST on the PATH-LIST. Return absolute names. 4007 file (cons master-dir reftex-bib-path))))
2914 ;; A missing file throws an exception with the error message ERROR-STRING. 4008 (unless file1
2915 (let (found-list found file) 4009 (reftex-access-search-path "bib" t file)
2916 (while file-list 4010 (setq file1 (reftex-find-file-on-path
2917 (setq file (car file-list) 4011 file (cons master-dir reftex-bib-path))))
2918 file-list (cdr file-list) 4012 (cond (file1 file1)
2919 found nil) 4013 (die (error "No such file: %s" file) nil)
2920 (if (file-name-absolute-p file) 4014 (t (message "No such file: %s (ignored)" file) nil))))
2921 (setq found (expand-file-name file))
2922 (let ((dirs path-list))
2923 (while (and dirs (not found))
2924 (if (and (not (string= (car dirs) ""))
2925 (file-exists-p (expand-file-name file (car dirs))))
2926 (setq found (expand-file-name file (car dirs))))
2927 (setq dirs (cdr dirs)))))
2928 (if (and found
2929 (file-exists-p found))
2930 (add-to-list 'found-list (expand-file-name found))
2931 (error "%s No such file %s."
2932 (or error-string "") file)))
2933 (nreverse found-list)))
2934 4015
2935 ;; Find a certain reference in any of the BibTeX files. 4016 ;; Find a certain reference in any of the BibTeX files.
2936 4017
2937 (defun reftex-pop-to-bibtex-entry (key file-list 4018 (defun reftex-pop-to-bibtex-entry (key file-list
2938 &optional mark-to-kill highlight) 4019 &optional mark-to-kill highlight)
2939 ;; Find BibTeX KEY in any file in FILE-LIST in another window. 4020 ;; Find BibTeX KEY in any file in FILE-LIST in another window.
2940 ;; If mark-to-kill is non-nil, mark new buffer to kill." 4021 ;; If mark-to-kill is non-nil, mark new buffer to kill."
2941 4022
2942 (let* ((re (concat "@[a-zA-Z]+[ \t\n\r]*[{(][ \t\n\r]*" (regexp-quote key) "[ \t\n\r,]")) 4023 (let* ((re (concat "@[a-zA-Z]+[ \t\n\r]*[{(][ \t\n\r]*" (regexp-quote key) "[ \t\n\r,]"))
2943 (window-conf (current-window-configuration)) 4024 (window-conf (current-window-configuration))
2945 (catch 'exit 4026 (catch 'exit
2946 (switch-to-buffer-other-window (current-buffer)) 4027 (switch-to-buffer-other-window (current-buffer))
2947 (while file-list 4028 (while file-list
2948 (setq file (car file-list) 4029 (setq file (car file-list)
2949 file-list (cdr file-list)) 4030 file-list (cdr file-list))
2950 (if (not (setq buf (reftex-get-file-buffer-force file mark-to-kill))) 4031 (unless (setq buf (reftex-get-file-buffer-force file mark-to-kill))
2951 (error "No such file %s" file)) 4032 (error "No such file %s" file))
2952 (switch-to-buffer buf) 4033 (switch-to-buffer buf)
2953 (widen) 4034 (widen)
2954 (goto-char 0) 4035 (goto-char (point-min))
2955 (if (re-search-forward re nil t) 4036 (when (re-search-forward re nil t)
2956 (progn 4037 (goto-char (match-beginning 0))
2957 (goto-char (match-beginning 0)) 4038 (recenter 0)
2958 (recenter 0) 4039 (if highlight
2959 (if highlight 4040 (reftex-highlight 0 (match-beginning 0) (match-end 0)))
2960 (reftex-highlight 0 (match-beginning 0) (match-end 0))) 4041 (throw 'exit (selected-window))))
2961 (throw 'exit (selected-window)))))
2962 (set-window-configuration window-conf) 4042 (set-window-configuration window-conf)
2963 (beep) 4043 (beep)
2964 (message "No BibTeX entry with citation key %s" key)))) 4044 (message "No BibTeX entry with citation key %s" key))))
2965 4045
2966 ;; Parse bibtex buffers 4046 ;; Parse bibtex buffers
2968 (defun reftex-extract-bib-entries (buffers &optional get-word) 4048 (defun reftex-extract-bib-entries (buffers &optional get-word)
2969 ;; Extract bib entries which match regexps from BUFFERS. 4049 ;; Extract bib entries which match regexps from BUFFERS.
2970 ;; BUFFERS is a list of buffers or file names. 4050 ;; BUFFERS is a list of buffers or file names.
2971 ;; Return list with entries." 4051 ;; Return list with entries."
2972 (let* (re-list first-re rest-re 4052 (let* (re-list first-re rest-re
2973 ;; avoid fontification of lookup buffers
2974 (lazy-lock-minimum-size 1)
2975 (buffer-list (if (listp buffers) buffers (list buffers))) 4053 (buffer-list (if (listp buffers) buffers (list buffers)))
2976 found-list entry buffer1 buffer alist 4054 found-list entry buffer1 buffer alist
2977 key-point start-point end-point) 4055 key-point start-point end-point)
2978 4056
2979 (setq re-list (reftex-split "[ \t]*&&[ \t]*" 4057 (setq re-list (split-string
2980 (read-string "RegExp [ && RegExp...]: " 4058 (read-string "RegExp [ && RegExp...]: "
2981 nil 'reftex-cite-regexp-hist))) 4059 nil 'reftex-cite-regexp-hist)
4060 "[ \t]*&&[ \t]*"))
2982 4061
2983 (setq first-re (car re-list) ; We'll use the first re to find things, 4062 (setq first-re (car re-list) ; We'll use the first re to find things,
2984 rest-re (cdr re-list)) ; the other to narrow down. 4063 rest-re (cdr re-list)) ; the other to narrow down.
2985 (if (string-match "\\`[ \t]*\\'" first-re) 4064 (if (string-match "\\`[ \t]*\\'" first-re)
2986 (error "Empty regular expression")) 4065 (error "Empty regular expression"))
2987 4066
2988 (save-excursion 4067 (save-excursion
2989 (save-window-excursion 4068 (save-window-excursion
2990 4069
2991 ;; walk through all bibtex files 4070 ;; Walk through all bibtex files
2992 (while buffer-list 4071 (while buffer-list
2993 (setq buffer (car buffer-list) 4072 (setq buffer (car buffer-list)
2994 buffer-list (cdr buffer-list)) 4073 buffer-list (cdr buffer-list))
2995 (if (and (bufferp buffer) 4074 (if (and (bufferp buffer)
2996 (buffer-live-p buffer)) 4075 (buffer-live-p buffer))
3000 (if (not buffer1) 4079 (if (not buffer1)
3001 (error "Cannot find BibTeX file %s" buffer) 4080 (error "Cannot find BibTeX file %s" buffer)
3002 (message "Scanning bibliography database %s" buffer1)) 4081 (message "Scanning bibliography database %s" buffer1))
3003 4082
3004 (set-buffer buffer1) 4083 (set-buffer buffer1)
3005 (save-excursion 4084 (save-excursion
3006 (goto-char (point-min)) 4085 (goto-char (point-min))
3007 (while (re-search-forward first-re nil t) 4086 (while (re-search-forward first-re nil t)
3008 (catch 'search-again 4087 (catch 'search-again
3009 (setq key-point (point)) 4088 (setq key-point (point))
3010 (if (not (re-search-backward 4089 (unless (re-search-backward
3011 "^[ \t]*@\\([a-zA-Z]+\\)[ \t\n\r]*[{(]" nil t)) 4090 "\\(\\`\\|[\n\r]\\)[ \t]*@\\([a-zA-Z]+\\)[ \t\n\r]*[{(]" nil t)
3012 (throw 'search-again nil)) 4091 (throw 'search-again nil))
3013 (setq start-point (point)) 4092 (setq start-point (point))
3014 (goto-char (match-end 0)) 4093 (goto-char (match-end 0))
3015 (condition-case nil 4094 (condition-case nil
3016 (up-list 1) 4095 (up-list 1)
3017 ('error (goto-char key-point) 4096 (error (goto-char key-point)
3018 (throw 'search-again nil))) 4097 (throw 'search-again nil)))
3019 (setq end-point (point)) 4098 (setq end-point (point))
3020 4099
3021 ;; Ignore @string, @comment and @c entries or things 4100 ;; Ignore @string, @comment and @c entries or things
3022 ;; outside entries 4101 ;; outside entries
3023 (if (or (string= (downcase (match-string 1)) "string") 4102 (when (or (string= (downcase (match-string 2)) "string")
3024 (string= (downcase (match-string 1)) "comment") 4103 (string= (downcase (match-string 2)) "comment")
3025 (string= (downcase (match-string 1)) "c") 4104 (string= (downcase (match-string 2)) "c")
3026 (< (point) key-point)) ; this means match not in {} 4105 (< (point) key-point)) ; this means match not in {}
3027 (progn 4106 (goto-char key-point)
3028 (goto-char key-point) 4107 (throw 'search-again nil))
3029 (throw 'search-again nil))) 4108
3030 4109 ;; Well, we have got a match
3031 ;; Well, we have got a match 4110 (setq entry (concat
3032 (setq entry (concat 4111 (buffer-substring start-point (point)) "\n"))
3033 (buffer-substring start-point (point)) "\n")) 4112
3034 4113 ;; Check if other regexp match as well
3035 ;; Check if other regexp match as well 4114 (setq re-list rest-re)
3036 (setq re-list rest-re) 4115 (while re-list
3037 (while re-list 4116 (unless (string-match (car re-list) entry)
3038 (if (not (string-match (car re-list) entry)) 4117 ;; nope - move on
3039 ;; nope - move on 4118 (throw 'search-again nil))
3040 (throw 'search-again nil)) 4119 (pop re-list))
3041 (setq re-list (cdr re-list))) 4120
3042 4121 (setq alist (reftex-parse-bibtex-entry
3043 (setq alist (reftex-parse-bibtex-entry 4122 nil start-point end-point))
3044 nil start-point end-point)) 4123 (push (cons "&entry" entry) alist)
3045 (setq alist (cons (cons "&entry" entry) alist)) 4124
3046 4125 ;; check for crossref entries
3047 ;; check for crossref entries 4126 (if (assoc "crossref" alist)
3048 (if (assoc "crossref" alist) 4127 (setq alist
3049 (setq alist 4128 (append
3050 (append 4129 alist (reftex-get-crossref-alist alist))))
3051 alist (reftex-get-crossref-alist alist)))) 4130
3052 4131 ;; format the entry
3053 ;; format the entry 4132 (push (cons "&formatted" (reftex-format-bib-entry alist))
3054 (setq alist 4133 alist)
3055 (cons 4134
3056 (cons "&formatted" 4135 ;; add it to the list
3057 (reftex-format-bib-entry alist)) 4136 (push alist found-list))))
3058 alist))
3059
3060 ;; add it to the list
3061 (setq found-list (cons alist found-list)))))
3062 (reftex-kill-temporary-buffers)))) 4137 (reftex-kill-temporary-buffers))))
3063 (setq found-list (nreverse found-list)) 4138 (setq found-list (nreverse found-list))
3064 4139
3065 ;; Sorting 4140 ;; Sorting
3066 (cond 4141 (cond
3067 ((eq 'author reftex-sort-bibtex-matches) 4142 ((eq 'author reftex-sort-bibtex-matches)
3068 (sort found-list 'reftex-bib-sort-author)) 4143 (sort found-list 'reftex-bib-sort-author))
3069 ((eq 'year reftex-sort-bibtex-matches) 4144 ((eq 'year reftex-sort-bibtex-matches)
3070 (sort found-list 'reftex-bib-sort-year)) 4145 (sort found-list 'reftex-bib-sort-year))
3071 ((eq 'reverse-year reftex-sort-bibtex-matches) 4146 ((eq 'reverse-year reftex-sort-bibtex-matches)
3072 (sort found-list 'reftex-bib-sort-year-reverse)) 4147 (sort found-list 'reftex-bib-sort-year-reverse))
3073 (t found-list)))) 4148 (t found-list))))
3074 4149
3075 (defun reftex-bib-sort-author (e1 e2) 4150 (defun reftex-bib-sort-author (e1 e2)
3076 (let ((al1 (reftex-get-bib-authors e1)) (al2 (reftex-get-bib-authors e2))) 4151 (let ((al1 (reftex-get-bib-names "author" e1))
4152 (al2 (reftex-get-bib-names "author" e2)))
3077 (while (and al1 al2 (string= (car al1) (car al2))) 4153 (while (and al1 al2 (string= (car al1) (car al2)))
3078 (setq al1 (cdr al1) 4154 (pop al1)
3079 al2 (cdr al2))) 4155 (pop al2))
3080 (if (and (stringp (car al1)) 4156 (if (and (stringp (car al1))
3081 (stringp (car al2))) 4157 (stringp (car al2)))
3082 (string< (car al1) (car al2)) 4158 (string< (car al1) (car al2))
3083 (not (stringp (car al1)))))) 4159 (not (stringp (car al1))))))
3084 4160
3085 (defun reftex-bib-sort-year (e1 e2) 4161 (defun reftex-bib-sort-year (e1 e2)
3086 (< (string-to-int (cdr (assoc "year" e1))) 4162 (< (string-to-int (cdr (assoc "year" e1)))
3087 (string-to-int (cdr (assoc "year" e2))))) 4163 (string-to-int (cdr (assoc "year" e2)))))
3096 start) 4172 start)
3097 (save-excursion 4173 (save-excursion
3098 (save-restriction 4174 (save-restriction
3099 (widen) 4175 (widen)
3100 (if (re-search-forward 4176 (if (re-search-forward
3101 (concat "@\\w+[{(][ \t\n\r]*" (regexp-quote crkey) "[ \t\n\r]*,") nil t) 4177 (concat "@\\w+[{(][ \t\n\r]*" (regexp-quote crkey)
4178 "[ \t\n\r]*,") nil t)
3102 (progn 4179 (progn
3103 (setq start (match-beginning 0)) 4180 (setq start (match-beginning 0))
3104 (condition-case nil 4181 (condition-case nil
3105 (up-list 1) 4182 (up-list 1)
3106 ('error nil)) 4183 (error nil))
3107 (reftex-parse-bibtex-entry nil start (point))) 4184 (reftex-parse-bibtex-entry nil start (point)))
3108 nil))))) 4185 nil)))))
3109 4186
3110 ;; Parse and format individual entries 4187 ;; Parse and format individual entries
3111 4188
3112 (defun reftex-get-bib-authors (entry) 4189 (defun reftex-get-bib-names (field entry)
3113 ;; Return a list with the author names in ENTRY 4190 ;; Return a list with the author or editor anmes in ENTRY
3114 (let (authors) 4191 (let ((names (reftex-get-bib-field field entry)))
3115 (setq authors (reftex-get-bib-field "author" entry)) 4192 (if (equal "" names)
3116 (if (equal "" authors) 4193 (setq names (reftex-get-bib-field "editor" entry)))
3117 (setq authors (reftex-get-bib-field "editor" entry))) 4194 (while (string-match "\\band\\b[ \t]*" names)
3118 (while (string-match "\\band\\b[ \t]*" authors) 4195 (setq names (replace-match "\n" nil t names)))
3119 (setq authors (replace-match "\n" nil t authors))) 4196 (while (string-match "[\\.a-zA-Z\\-]+\\.[ \t]*\\|,.*\\|[{}]+" names)
3120 (while (string-match "[\\.a-zA-Z\\-]+\\.[ \t]*\\|,.*\\|[{}]+" authors) 4197 (setq names (replace-match "" nil t names)))
3121 (setq authors (replace-match "" nil t authors))) 4198 (while (string-match "^[ \t]+\\|[ \t]+$" names)
3122 (while (string-match "^[ \t]+\\|[ \t]+$" authors) 4199 (setq names (replace-match "" nil t names)))
3123 (setq authors (replace-match "" nil t authors))) 4200 (while (string-match "[ \t][ \t]+" names)
3124 (while (string-match "[ \t][ \t]+" authors) 4201 (setq names (replace-match " " nil t names)))
3125 (setq authors (replace-match " " nil t authors))) 4202 (split-string names "\n")))
3126 (reftex-split "\n" authors)))
3127 4203
3128 (defun reftex-parse-bibtex-entry (entry &optional from to) 4204 (defun reftex-parse-bibtex-entry (entry &optional from to)
3129 (let (alist key start field) 4205 (let (alist key start field)
3130 (save-excursion 4206 (save-excursion
3131 (save-restriction 4207 (save-restriction
3137 (insert entry)) 4213 (insert entry))
3138 (widen) 4214 (widen)
3139 (narrow-to-region from to)) 4215 (narrow-to-region from to))
3140 (goto-char (point-min)) 4216 (goto-char (point-min))
3141 4217
3142 (if (re-search-forward 4218 (if (re-search-forward
3143 "@\\(\\w+\\)[ \t\n\r]*[{(][ \t\n\r]*\\([^ \t\n\r,]+\\)" nil t) 4219 "@\\(\\w+\\)[ \t\n\r]*[{(][ \t\n\r]*\\([^ \t\n\r,]+\\)" nil t)
3144 (setq alist 4220 (setq alist
3145 (list 4221 (list
3146 (cons "&type" (downcase (reftex-no-props (match-string 1)))) 4222 (cons "&type" (downcase (reftex-match-string 1)))
3147 (cons "&key" (reftex-no-props (match-string 2)))))) 4223 (cons "&key" (reftex-match-string 2)))))
3148 (while (re-search-forward "\\(\\w+\\)[ \t\n\r]*=[ \t\n\r]*" nil t) 4224 (while (re-search-forward "\\(\\w+\\)[ \t\n\r]*=[ \t\n\r]*" nil t)
3149 (setq key (reftex-no-props (downcase (match-string 1)))) 4225 (setq key (downcase (reftex-match-string 1)))
3150 (cond 4226 (cond
3151 ((= (following-char) ?{) 4227 ((= (following-char) ?{)
3152 (forward-char 1) 4228 (forward-char 1)
3153 (setq start (point)) 4229 (setq start (point))
3154 (condition-case nil 4230 (condition-case nil
3155 (up-list 1) 4231 (up-list 1)
3156 ('error nil))) 4232 (error nil)))
3157 ((= (following-char) ?\") 4233 ((= (following-char) ?\")
3158 (forward-char 1) 4234 (forward-char 1)
3159 (setq start (point)) 4235 (setq start (point))
3160 (while (and (search-forward "\"" nil t) 4236 (while (and (search-forward "\"" nil t)
3161 (= ?\\ (char-after (- (point) 2)))))) 4237 (= ?\\ (char-after (- (point) 2))))))
3170 (if (string-match "^[ \t{]+" field) 4246 (if (string-match "^[ \t{]+" field)
3171 (setq field (replace-match "" nil t field))) 4247 (setq field (replace-match "" nil t field)))
3172 ;; remove trailing garbage 4248 ;; remove trailing garbage
3173 (if (string-match "[ \t}]+$" field) 4249 (if (string-match "[ \t}]+$" field)
3174 (setq field (replace-match "" nil t field))) 4250 (setq field (replace-match "" nil t field)))
3175 (setq alist (cons (cons key field) alist))) 4251 (push (cons key field) alist))))
3176 alist)))) 4252 alist))
3177 4253
3178 (defun reftex-get-bib-field (fieldname entry) 4254 (defun reftex-get-bib-field (fieldname entry)
3179 ;; Extract the field FIELDNAME from an ENTRY 4255 ;; Extract the field FIELDNAME from an ENTRY
3180 (or (cdr (assoc fieldname entry)) 4256 (or (cdr (assoc fieldname entry))
3181 "")) 4257 ""))
3182 4258
3183 (defun reftex-format-bib-entry (entry) 4259 (defun reftex-format-bib-entry (entry)
3184 ;; Format a BibTeX ENTRY so that it is nice to look at 4260 ;; Format a BibTeX ENTRY so that it is nice to look at
3185 (let* 4261 (let*
3186 ((rtn nil) 4262 ((auth-list (reftex-get-bib-names "author" entry))
3187 (auth-list (reftex-get-bib-authors entry))
3188 (authors (mapconcat '(lambda (x) x) auth-list ", ")) 4263 (authors (mapconcat '(lambda (x) x) auth-list ", "))
3189 (year (reftex-get-bib-field "year" entry)) 4264 (year (reftex-get-bib-field "year" entry))
3190 (title (reftex-get-bib-field "title" entry)) 4265 (title (reftex-get-bib-field "title" entry))
3191 (type (reftex-get-bib-field "&type" entry)) 4266 (type (reftex-get-bib-field "&type" entry))
3192 (key (reftex-get-bib-field "&key" entry)) 4267 (key (reftex-get-bib-field "&key" entry))
3208 ((or (equal type "conference") 4283 ((or (equal type "conference")
3209 (equal type "incollection") 4284 (equal type "incollection")
3210 (equal type "inproceedings")) 4285 (equal type "inproceedings"))
3211 (concat "in: " (reftex-get-bib-field "booktitle" entry))) 4286 (concat "in: " (reftex-get-bib-field "booktitle" entry)))
3212 (t "")))) 4287 (t ""))))
3213 (setq authors 4288 (setq authors (reftex-truncate authors 30 t t))
3214 (if (> (length authors) 30) 4289 (when (reftex-use-fonts)
3215 (concat (substring authors 0 27) "...") 4290 (put-text-property 0 (length authors) 'face 'font-lock-keyword-face
3216 (format "%-30s" authors)) 4291 authors)
3217 title 4292 (put-text-property 0 (length title) 'face 'font-lock-comment-face
3218 (if (> (length title) 70) 4293 title)
3219 (concat (substring title 0 67) "...") 4294 (put-text-property 0 (length extra) 'face 'font-lock-reference-face
3220 (format "%-70s" title)) 4295 extra))
3221 extra 4296 (concat key "\n " authors " " year " " extra "\n " title "\n\n")))
3222 (if (> (length extra) 40)
3223 (concat (substring extra 0 37) "...")
3224 (format "%-40s" extra)))
3225 (if (reftex-use-fonts)
3226 (progn
3227 (put-text-property 0 (length authors) 'face 'font-lock-keyword-face
3228 authors)
3229 (put-text-property 0 (length title) 'face 'font-lock-comment-face
3230 title)
3231 (put-text-property 0 (length extra) 'face 'font-lock-reference-face
3232 extra)))
3233 (setq rtn (concat key "\n " authors " " year " " extra
3234 "\n " title "\n\n"))
3235 rtn))
3236 4297
3237 ;; Make a citation 4298 ;; Make a citation
3238 4299
3239 (defun reftex-citation (&optional arg no-insert) 4300 (defun reftex-citation (&optional no-insert)
3240 "Make a citation using BibTeX database files. 4301 "Make a citation using BibTeX database files.
3241 After asking for a Regular Expression, it scans the buffers with 4302 After asking for a Regular Expression, it scans the buffers with
3242 bibtex entries (taken from the \\bibliography command) and offers the 4303 bibtex entries (taken from the \\bibliography command) and offers the
3243 matching entries for selection. The selected entry is formated according 4304 matching entries for selection. The selected entry is formated according
3244 to `reftex-cite-format' and inserted into the buffer. 4305 to `reftex-cite-format' and inserted into the buffer.
3250 When called with a numeric prefix, that many citations will be made and all 4311 When called with a numeric prefix, that many citations will be made and all
3251 put into the same \\cite command. 4312 put into the same \\cite command.
3252 When called with just C-u as prefix, enforces rescan of buffer for 4313 When called with just C-u as prefix, enforces rescan of buffer for
3253 bibliography statement (e.g. if it was changed)." 4314 bibliography statement (e.g. if it was changed)."
3254 4315
3255 (interactive "P") 4316 (interactive)
3256 4317
3257 ;; check for recursive edit 4318 ;; check for recursive edit
3258 (reftex-check-recursive-edit) 4319 (reftex-check-recursive-edit)
3259 4320
3260 ;; if there is just 1 C-u prefix arg, force to rescan buffer 4321 ;; if there is just 1 C-u prefix arg, force to rescan buffer
3261 (if (and current-prefix-arg 4322 (reftex-access-scan-info current-prefix-arg)
3262 (listp current-prefix-arg) 4323
3263 (= 4 (prefix-numeric-value arg))) 4324 ;; Call reftex-do-citation, but protected
3264 (reftex-reset-scanning-information)) 4325 (unwind-protect
3265 4326 (reftex-do-citation current-prefix-arg no-insert)
3266 ;; check if there is already a cite command at point and change cite format 4327 (reftex-kill-temporary-buffers)))
4328
4329 (defun reftex-do-citation (&optional arg no-insert)
4330 ;; This really does the work of reftex-citation.
4331
4332 ;; Check if there is already a cite command at point and change cite format
3267 ;; in order to only add another reference in the same cite command. 4333 ;; in order to only add another reference in the same cite command.
3268 (let ((pos (point))) 4334 (let (key format (macro (car (car (reftex-what-macro t)))))
3269 (search-backward "\\" (point-min) 1) 4335 (if (and (stringp macro)
3270 (if (and (looking-at "\\\\[a-zA-Z]*cite\\*?\\(\\[[^]]*\\]\\)*{\\([^}]*\\)") 4336 (string-match "\\`\\\\cite\\|cite\\'" macro))
3271 (>= (match-end 0) pos)
3272 (>= pos (match-beginning 2)))
3273 (progn 4337 (progn
3274 (goto-char pos)
3275 (cond 4338 (cond
3276 ((or (not arg) 4339 ((or (not arg)
3277 (not (listp arg))) 4340 (not (listp arg)))
3278 (setq reftex-cite-format1 4341 (setq format
3279 (concat 4342 (concat
3280 (if (not (or (= (preceding-char) ?{) 4343 (if (not (or (= (preceding-char) ?{)
3281 (= (preceding-char) ?,))) 4344 (= (preceding-char) ?,)))
3282 "," 4345 ","
3283 "") 4346 "")
3284 "KEY" 4347 "%l"
3285 (if (not (or (= (following-char) ?}) 4348 (if (not (or (= (following-char) ?})
3286 (= (following-char) ?,))) 4349 (= (following-char) ?,)))
3287 "," 4350 ","
3288 "")))) 4351 ""))))
3289 (t 4352 (t
3290 (setq reftex-cite-format1 "KEY")))) 4353 (setq format "%l"))))
3291 (setq reftex-cite-format1 4354 ;; else: figure out the correct format
3292 (if (symbolp reftex-cite-format) 4355 (setq format
3293 (symbol-value reftex-cite-format) 4356 (cond
3294 reftex-cite-format)) 4357 ((stringp reftex-cite-format) reftex-cite-format)
3295 (goto-char pos))) 4358 ((and (symbolp reftex-cite-format)
3296 4359 (assq reftex-cite-format reftex-cite-format-builtin))
3297 (let* (key entry cnt rtn ins-string re-list re 4360 (nth 2 (assq reftex-cite-format reftex-cite-format-builtin)))
3298 ;; scan bibtex files 4361 (t reftex-cite-format)))
3299 (lazy-lock-minimum-size 1) 4362 (if (listp format)
3300 (reftex-found-list (reftex-extract-bib-entries 4363 (save-window-excursion
3301 (reftex-get-bibfile-list))) 4364 (with-output-to-temp-buffer "*RefTeX Select*"
3302 (found-list-r nil) 4365 (princ "SELECT A CITATION FORMAT\n\n")
3303 (accept-keys 4366 (princ
3304 (if (and (listp reftex-cite-format1) 4367 (mapconcat
3305 (listp (car reftex-cite-format1))) 4368 (function (lambda (x)
3306 (mapcar 'car reftex-cite-format1) 4369 (format "[%c] %s %s" (car x)
3307 '(?\C-m)))) 4370 (if (> (car x) 31) " " "")
3308 (if (not reftex-found-list) 4371 (cdr x))))
4372 format "\n")))
4373 (setq key (read-char))
4374 (if (assq key format)
4375 (setq format (cdr (assq key format)))
4376 (error "No citation format associated with key `%c'" key)))))
4377
4378 (let* (entry cnt rtn ins-string re-list re
4379 ;; scan bibtex files
4380 (reftex-found-list (reftex-extract-bib-entries
4381 (reftex-get-bibfile-list)))
4382 (found-list-r nil))
4383 (unless reftex-found-list
3309 (error "Sorry, no matches found")) 4384 (error "Sorry, no matches found"))
3310 4385
3311 ;; remember where we came from 4386 ;; remember where we came from
3312 (setq reftex-call-back-to-this-buffer (current-buffer)) 4387 (setq reftex-call-back-to-this-buffer (current-buffer))
3313 4388
3314 ;; offer selection 4389 ;; offer selection
3315 (save-window-excursion 4390 (save-window-excursion
3316 (switch-to-buffer-other-window "*RefTeX Select*") 4391 (switch-to-buffer-other-window "*RefTeX Select*")
3317 (erase-buffer) 4392 (erase-buffer)
3318 (mapcar '(lambda (x) (insert (cdr (assoc "&formatted" x)))) 4393 (reftex-insert-bib-matches reftex-found-list)
3319 reftex-found-list) 4394 (if (= 0 (buffer-size))
3320 (if (= 0 (buffer-size)) 4395 (error "Sorry, no matches found"))
3321 (error "Sorry, no matches found")) 4396 (setq truncate-lines t)
3322 (setq truncate-lines t) 4397 (goto-char 1)
3323 (goto-char 1) 4398 (if (catch 'exit
3324 (if (catch 'exit 4399 (while t
3325 (while t 4400 (setq rtn
3326 (setq rtn 4401 (reftex-select-item
3327 (reftex-select-item 4402 reftex-citation-prompt
3328 nil 4403 "^[^ \t\n\r]"
3329 (concat 4404 4
3330 "Select: [n]ext [p]rev [r]estrict [q]uit [?]Help ||" 4405 reftex-citation-help
3331 " RETURN " 4406 '(?r ?a ?g ?\C-m)
3332 (condition-case nil 4407 nil
3333 (mapconcat 'char-to-string accept-keys " ") 4408 'reftex-bibtex-selection-callback nil))
3334 (error (error "Illegal reftex-cite-format")))) 4409 (setq key (car rtn)
3335 "^[^ \t\n]" 4410 cnt (nth 1 rtn))
3336 "\n\n" 4411 (unless key (throw 'exit nil))
3337 4 4412 (cond
3338 reftex-citation-help 4413 ((eq key ?g)
3339 (cons ?r accept-keys) 4414 (setq reftex-found-list
3340 nil 4415 (save-excursion
3341 'reftex-bibtex-selection-callback nil)) 4416 (set-buffer reftex-call-back-to-this-buffer)
3342 (setq key (car rtn) 4417 (reftex-extract-bib-entries
3343 cnt (cdr rtn)) 4418 (reftex-get-bibfile-list))))
3344 (if (not key) (throw 'exit nil)) 4419 (erase-buffer)
4420 (reftex-insert-bib-matches reftex-found-list)
4421 (if (= 0 (buffer-size))
4422 (error "Sorry, no matches found"))
4423 (goto-char 1))
4424
4425 ((eq key ?r)
4426 ;; restrict with new regular expression
4427 (setq re-list
4428 (split-string (read-string
4429 "RegExp [ && RegExp...]: "
4430 nil 'reftex-cite-regexp-hist)
4431 "[ \t]*&&[ \t]*"))
4432 (while re-list
4433 (setq re (car re-list)
4434 re-list (cdr re-list))
4435 (setq found-list-r
4436 (delete ""
4437 (mapcar
4438 '(lambda (x)
4439 (if (string-match
4440 re (cdr (assoc "&entry" x)))
4441 x
4442 ""))
4443 reftex-found-list))))
4444 (if found-list-r
4445 (setq reftex-found-list found-list-r)
4446 (ding))
4447 (erase-buffer)
4448 (reftex-insert-bib-matches reftex-found-list)
4449 (goto-char 1))
4450 ((eq key ?a)
4451 (setq entry 'all)
4452 (throw 'exit t))
4453 ((or (eq key ?\C-m)
4454 (eq key 'return))
4455 (if cnt
4456 (setq entry (nth cnt reftex-found-list))
4457 (setq entry nil))
4458 (throw 'exit t))
4459 (t
4460 (ding)))))
4461 (progn
4462 ;; format the entry
4463 (if (eq entry 'all)
4464 (setq ins-string
4465 (mapconcat
4466 '(lambda (entry)
4467 (reftex-format-citation entry format))
4468 reftex-found-list "\n"))
4469 (setq ins-string (reftex-format-citation entry format))))
4470 (setq ins-string "")
4471 (message "Quit")))
4472 (kill-buffer "*RefTeX Select*")
4473
4474 (unless no-insert
4475 (insert ins-string)
4476 (when (string-match "\\?" ins-string)
4477 (search-backward "?")
4478 (delete-char 1)))
4479 (message "")
4480
4481 ;; Check if the prefix arg was numeric, and call recursively
4482 (when (and (integerp arg)
4483 (> arg 1)
4484 (re-search-backward
4485 "\\\\\\([a-zA-Z]*cite\\|cite[a-zA-Z]*\\)\\**\\(\\[[^]]*\\]\\)*{\\([^}]*\\)" nil t))
4486 (goto-char (match-end 0))
4487 (decf arg)
4488 (reftex-do-citation arg))
4489
4490 ;; Return the citation key
4491 (or (eq entry 'all)
4492 (reftex-get-bib-field "&key" entry)))))
4493
4494 (defun reftex-insert-bib-matches (list)
4495 ;; Insert the bib matches and number them correctly
4496 (let ((cnt -1) tmp)
4497 (mapcar '(lambda (x)
4498 (setq tmp (cdr (assoc "&formatted" x)))
4499 (incf cnt)
4500 (put-text-property 0 (length tmp) 'cnt cnt tmp)
4501 (insert tmp))
4502 list)))
4503
4504 (defun reftex-format-names (namelist n)
4505 (interactive)
4506 (let (last (len (length namelist)))
4507 (cond
4508 ((= 1 len) (car namelist))
4509 ((> len n) (concat (car namelist) (nth 2 reftex-cite-punctuation)))
4510 (t
4511 (setq n (min len n)
4512 last (nth (1- n) namelist))
4513 (setcdr (nthcdr (- n 2) namelist) nil)
4514 (concat
4515 (mapconcat 'identity namelist (nth 0 reftex-cite-punctuation))
4516 (nth 1 reftex-cite-punctuation)
4517 last)))))
4518
4519 (defun reftex-format-citation (entry format)
4520 ;; Format a citation from the info in the BibTeX ENTRY
4521
4522 (unless (stringp format) (setq format "\\cite{%l}"))
4523
4524 (if (and reftex-comment-citations
4525 (string-match "%l" reftex-cite-comment-format))
4526 (error "reftex-cite-comment-format contains illeagal %%l"))
4527
4528 (while (string-match
4529 "\\(\\`\\|[^%]\\)\\(\\(%\\([0-9]*\\)\\([a-zA-Z]\\)\\)[.,;: ]*\\)"
4530 format)
4531 (let ((n (string-to-int (match-string 4 format)))
4532 (l (string-to-char (match-string 5 format)))
4533 rpl b e)
4534 (save-match-data
4535 (setq rpl
3345 (cond 4536 (cond
3346 ((equal key ?r) 4537 ((= l ?l) (concat
3347 ;; restrict with new regular expression 4538 (reftex-get-bib-field "&key" entry)
3348 (setq re-list 4539 (if reftex-comment-citations
3349 (reftex-split "[ \t]*&&[ \t]*" 4540 reftex-cite-comment-format
3350 (read-string "RegExp [ && RegExp...]: " 4541 "")))
3351 nil 'reftex-cite-regexp-hist))) 4542 ((= l ?a) (reftex-format-names
3352 (while re-list 4543 (reftex-get-bib-names "author" entry)
3353 (setq re (car re-list) 4544 (or n 2)))
3354 re-list (cdr re-list)) 4545 ((= l ?A) (car (reftex-get-bib-names "author" entry)))
3355 (setq found-list-r 4546 ((= l ?b) (reftex-get-bib-field "booktitle" entry))
3356 (delete "" 4547 ((= l ?c) (reftex-get-bib-field "chapter" entry))
3357 (mapcar 4548 ((= l ?d) (reftex-get-bib-field "edition" entry))
3358 '(lambda (x) 4549 ((= l ?e) (reftex-format-names
3359 (if (string-match re 4550 (reftex-get-bib-names "editor" entry)
3360 (cdr (assoc "&entry" x))) 4551 (or n 2)))
3361 x 4552 ((= l ?E) (car (reftex-get-bib-names "editor" entry)))
3362 "")) 4553 ((= l ?h) (reftex-get-bib-field "howpublished" entry))
3363 reftex-found-list)))) 4554 ((= l ?i) (reftex-get-bib-field "institution" entry))
3364 (if found-list-r 4555 ((= l ?j) (reftex-get-bib-field "journal" entry))
3365 (setq reftex-found-list found-list-r) 4556 ((= l ?k) (reftex-get-bib-field "key" entry))
3366 (ding)) 4557 ((= l ?m) (reftex-get-bib-field "month" entry))
3367 (erase-buffer) 4558 ((= l ?n) (reftex-get-bib-field "number" entry))
3368 (mapcar '(lambda (x) (insert (cdr (assoc "&formatted" x)))) 4559 ((= l ?o) (reftex-get-bib-field "organization" entry))
3369 reftex-found-list) 4560 ((= l ?p) (reftex-get-bib-field "pages" entry))
3370 (goto-char 1)) 4561 ((= l ?P) (car (split-string
3371 ((or (member key accept-keys) 4562 (reftex-get-bib-field "pages" entry)
3372 (equal key ?\C-m) 4563 "[- .]+")))
3373 (equal key 'return)) 4564 ((= l ?s) (reftex-get-bib-field "school" entry))
3374 (setq entry (nth cnt reftex-found-list)) 4565 ((= l ?u) (reftex-get-bib-field "publisher" entry))
3375 (throw 'exit t)) 4566 ((= l ?r) (reftex-get-bib-field "address" entry))
3376 (t 4567 ((= l ?t) (reftex-get-bib-field "title" entry))
3377 (ding))))) 4568 ((= l ?v) (reftex-get-bib-field "volume" entry))
3378 (progn 4569 ((= l ?y) (reftex-get-bib-field "year" entry)))))
3379 ;; format the entry 4570
3380 (if (not (integerp key)) (setq key ?\C-m)) 4571 (if (string= rpl "")
3381 (setq ins-string (reftex-format-citation entry key))) 4572 (setq b (match-beginning 2) e (match-end 2))
3382 (setq ins-string "") 4573 (setq b (match-beginning 3) e (match-end 3)))
3383 (message "Quit"))) 4574 (setq format (concat (substring format 0 b) rpl (substring format e)))))
3384 (kill-buffer "*RefTeX Select*") 4575 (while (string-match "%%" format)
3385 4576 (setq format (replace-match "%" t t format)))
3386 (if (not no-insert) 4577 (while (string-match "[ ,.;:]*%<" format)
3387 (insert ins-string)) 4578 (setq format (replace-match "" t t format)))
3388 (message "") 4579 format)
3389 4580
3390 ;; Check if the prefix arg was numeric, and call reftex-citation recursively 4581 ;; This is slow and not recommended for follow mode
3391 (if (and (integerp arg)
3392 (> arg 1)
3393 (re-search-backward
3394 "\\\\[a-zA-Z]*cite\\*?\\(\\[[^]]*\\]\\)*{\\([^}]*\\)" nil t))
3395 (progn
3396 (goto-char (match-end 0))
3397 (setq arg (1- arg))
3398 (reftex-citation arg))
3399 (reftex-kill-temporary-buffers))
3400 ;; Return the citation key
3401 (reftex-get-bib-field "&key" entry)))
3402
3403 (defun reftex-format-citation (entry key)
3404 ;; Format a citation from the info in the BibTeX ENTRY
3405 (let* ((cite-key (reftex-get-bib-field "&key" entry))
3406 (year (reftex-get-bib-field "year" entry))
3407 (auth-list (reftex-get-bib-authors entry))
3408 (nauthors (length auth-list))
3409 format)
3410
3411 (save-excursion
3412 ;; Find the correct format
3413 (if (and (listp reftex-cite-format1)
3414 (listp (car reftex-cite-format1)))
3415 (if (integerp (car (car reftex-cite-format1)))
3416 (if (assoc key reftex-cite-format1)
3417 (setq format (cdr (assoc key reftex-cite-format1)))
3418 (if (or (equal key ?\C-m)
3419 (equal key 'return))
3420 (setq format (cdr (car reftex-cite-format1)))
3421 (error "Error in reftex-cite-format")))
3422 (error "Error in reftex-cite-format"))
3423 (setq format reftex-cite-format1))
3424
3425 (if (listp format)
3426 (let ((nn (min nauthors (1- (length format)))))
3427 (while (and (> nn 0) (string= "" (nth nn format)))
3428 (setq nn (1- nn)))
3429 (setq format (nth nn format))))
3430 (if (stringp format)
3431 (setq format format)
3432 (setq format "\\cite{KEY}"))
3433
3434 ;; Insert the author names
3435 (while (string-match "\\bAUTHOR\\b" format)
3436 (setq format (replace-match (car auth-list) t t format))
3437 (setq auth-list (cdr auth-list)))
3438 (while (string-match "\\bKEY\\b" format)
3439 (setq format (replace-match cite-key t t format)))
3440 (while (string-match "\\bYEAR\\b" format)
3441 (setq format (replace-match year t t format)))
3442 format)))
3443
3444 ;; this is slow and not recommended for follow mode
3445 (defun reftex-bibtex-selection-callback (cnt) 4582 (defun reftex-bibtex-selection-callback (cnt)
3446 ;; Callback function to be called from the BibTeX selection, in 4583 ;; Callback function to be called from the BibTeX selection, in
3447 ;; order to display context. This function is relatively slow and not 4584 ;; order to display context. This function is relatively slow and not
3448 ;; recommended for follow mode, just for individual lookups. 4585 ;; recommended for follow mode, just for individual lookups.
3449 (let ((win (selected-window)) 4586 (let ((win (selected-window))
3468 (if (marker-position reftex-recursive-edit-marker) 4605 (if (marker-position reftex-recursive-edit-marker)
3469 (error 4606 (error
3470 (substitute-command-keys 4607 (substitute-command-keys
3471 "In unfinished recursive edit. Finish (\\[exit-recursive-edit]) or abort (\\[abort-recursive-edit]).")))) 4608 "In unfinished recursive edit. Finish (\\[exit-recursive-edit]) or abort (\\[abort-recursive-edit])."))))
3472 4609
3473 (defun reftex-select-item (buffer prompt next-re end-re size help-string 4610 (defun reftex-select-item (prompt next-re size help-string
3474 event-list &optional offset 4611 event-list &optional offset
3475 call-back cb-flag) 4612 call-back cb-flag match-everywhere)
3476 ;; Select an item from the buffer BUFFER. Show PROMPT to user, find 4613 ;; Select an item. Show PROMPT to user, find next item with NEXT-RE
3477 ;; next item with NEXT-RE regular expression, return on any of the 4614 ;; regular expression, return on any of the events listed in
3478 ;; events listed in EVENT-LIST. The function returns the event along 4615 ;; EVENT-LIST. The function returns the event along with an integer
3479 ;; with an integer indicating which item was selected. When OFFSET is 4616 ;; indicating which item was selected. When OFFSET is specified,
3480 ;; specified, starts at that item in the list. When CALL-BACK is 4617 ;; starts at that item in the list. When CALL-BACK is given, it is a
3481 ;; given, it is a function which is called with the match of the 4618 ;; function which is called with the index of the element.
3482 ;; NEXT-RE match and the index of the element. 4619
3483 (let* (key key-sq b e ev cnt cmd 4620 (let* (key key-sq b e ev cnt last-cnt cmd skip-callback
3484 (offset1 (or offset 1))) 4621 (search-str "") tmp search-start matched forward mini-map last-key
4622 (offset1 (or offset 1)) win1 win2)
4623
4624 ;; Set up a minibuffer keymap for the search stuff
4625 (setq mini-map (copy-keymap minibuffer-local-map))
4626 (define-key mini-map "\C-s"
4627 '(lambda () (interactive) (setq forward t) (exit-minibuffer)))
4628 (define-key mini-map "\C-r"
4629 '(lambda () (interactive) (setq forward nil) (exit-minibuffer)))
4630 (define-key mini-map "\C-m" 'exit-minibuffer)
4631
3485 (setq ev 4632 (setq ev
3486 (catch 'exit 4633 (catch 'exit
3487 (save-window-excursion 4634 (save-window-excursion
3488 (if buffer
3489 (switch-to-buffer-other-window buffer))
3490 (if (= 0 (buffer-size))
3491 (throw 'exit nil))
3492 (setq truncate-lines t) 4635 (setq truncate-lines t)
3493 (goto-char 1) 4636 (goto-char 1)
3494 (if (not (re-search-forward next-re nil t offset1)) 4637 (unless (re-search-forward next-re nil t offset1)
3495 (progn ; in case the offset is illegal 4638 ;; in case the offset is illegal
3496 (setq offset1 1) 4639 (setq offset1 1)
3497 (if (not (re-search-forward next-re nil t offset1)) 4640 (re-search-forward next-re nil t offset1))
3498 (throw 'exit nil))))
3499 (beginning-of-line 1) 4641 (beginning-of-line 1)
3500 (setq cnt (if offset1 (1- offset1) 0))
3501 (while t 4642 (while t
3502 (if (and cb-flag call-back) 4643 (setq last-cnt (or cnt last-cnt))
4644 (setq cnt (get-text-property (point) 'cnt))
4645 (if (and cnt cb-flag call-back (not skip-callback))
3503 (funcall call-back cnt)) 4646 (funcall call-back cnt))
3504 (setq b (point) 4647 (setq skip-callback nil)
3505 e (save-excursion 4648 (if cnt
3506 (save-match-data 4649 (setq b (or (previous-single-property-change
3507 (re-search-forward end-re nil 1)) 4650 (1+ (point)) 'cnt)
3508 (point))) 4651 (point-min))
4652 e (or (next-single-property-change
4653 (point) 'cnt)
4654 (point-max)))
4655 (setq b (point) e (point)))
3509 (reftex-highlight 1 b e) 4656 (reftex-highlight 1 b e)
3510 (if (or (not (pos-visible-in-window-p b)) 4657 (if (or (not (pos-visible-in-window-p b))
3511 (not (pos-visible-in-window-p e))) 4658 (not (pos-visible-in-window-p e)))
3512 (recenter (/ (window-height) 2))) 4659 (recenter (/ (window-height) 2)))
3513 (setq key-sq (read-key-sequence prompt)) 4660 (setq key-sq (read-key-sequence prompt))
4661 (setq last-key key)
3514 (setq key (car 4662 (setq key (car
3515 (cond 4663 (cond
4664 ((fboundp 'event-to-character) ; XEmacs
4665 (mapcar 'event-to-character key-sq))
3516 ((fboundp 'listify-key-sequence) ; Emacs 4666 ((fboundp 'listify-key-sequence) ; Emacs
3517 (listify-key-sequence key-sq)) 4667 (listify-key-sequence key-sq))
3518 ((fboundp 'event-to-character) ; XEmacs
3519 (mapcar 'event-to-character key-sq))
3520 (t (error "Please report this problem to dominik@strw.leidenuniv.nl"))))) 4668 (t (error "Please report this problem to dominik@strw.leidenuniv.nl")))))
3521 4669
3522 (setq cmd (key-binding key-sq)) 4670 (setq cmd (key-binding key-sq))
3523 4671
4672 (reftex-unhighlight 2)
3524 (reftex-unhighlight 0) 4673 (reftex-unhighlight 0)
3525 4674
3526 (cond 4675 (cond
3527 4676
3528 ((or (equal key ?n) 4677 ;; Single line motions
3529 (equal key ?\C-i) 4678 ((or (eq key ?n)
3530 (equal cmd 'next-line)) 4679 (eq key ?\C-i)
3531 (if (re-search-forward next-re nil t 2) 4680 (eq cmd 'next-line))
3532 (setq cnt (1+ cnt))) 4681 (or (eobp) (forward-char 1))
4682 (re-search-forward next-re nil t 1)
3533 (beginning-of-line 1)) 4683 (beginning-of-line 1))
3534 4684 ((or (eq key ?p)
3535 ((equal cmd 'scroll-up) 4685 (eq cmd 'previous-line))
3536 (setq cnt (1- cnt)) 4686 (re-search-backward next-re nil t))
4687
4688 ;; Page motions
4689 ((eq cmd 'scroll-up)
3537 (while (and (pos-visible-in-window-p) 4690 (while (and (pos-visible-in-window-p)
3538 (re-search-forward next-re nil t)) 4691 (re-search-forward next-re nil t)))
3539 (setq cnt (1+ cnt)))
3540 (beginning-of-line 1) 4692 (beginning-of-line 1)
3541 (recenter 1)) 4693 (recenter 1))
3542 4694 ((eq cmd 'scroll-down)
3543 ((or (equal key ?p)
3544 (equal cmd 'previous-line))
3545 (if (re-search-backward next-re nil t)
3546 (setq cnt (1- cnt))))
3547
3548 ((equal cmd 'scroll-down)
3549 (while (and (pos-visible-in-window-p) 4695 (while (and (pos-visible-in-window-p)
3550 (re-search-backward next-re nil t)) 4696 (re-search-backward next-re nil t)))
3551 (setq cnt (1- cnt)))
3552 (recenter (- (window-height) size 2))) 4697 (recenter (- (window-height) size 2)))
3553 4698
3554 ((equal key ?q) 4699 ;; Begin and end of buffer
4700 ((eq cmd 'beginning-of-buffer)
4701 (goto-char (point-min))
4702 (re-search-forward next-re nil t)
4703 (beginning-of-line 1))
4704 ((eq cmd 'end-of-buffer)
4705 (goto-char (point-max))
4706 (re-search-backward next-re nil t))
4707
4708 ;; Exit
4709 ((eq key ?q)
3555 (throw 'exit nil)) 4710 (throw 'exit nil))
3556 4711 ((eq key ?\C-g)
3557 ((equal key ?\C-g) 4712 (if (or (eq last-key ?\C-s) (eq last-key ?\C-r))
3558 (bury-buffer) 4713 (ding)
3559 (error "Abort")) 4714 (bury-buffer)
3560 4715 (error "Abort")))
3561 ((or (equal key ?\C-m) 4716 ((or (eq key ?\C-m)
3562 (equal key 'return) 4717 (eq key 'return)
3563 (equal cmd 'newline)) 4718 (eq cmd 'newline))
3564 (throw 'exit 'return)) 4719 (throw 'exit 'return))
3565 4720 ((memq key event-list)
3566 ((or (equal key ?C) ; backward compatibility 4721 (throw 'exit key))
3567 (equal key ?f)) 4722
4723 ;; Callback
4724 ((or (eq key ?C) ; backward compatibility
4725 (eq key ?f))
3568 (setq cb-flag (not cb-flag))) 4726 (setq cb-flag (not cb-flag)))
3569 4727 ((eq key ?\ )
3570 ((equal key ?\ ) 4728 (if cnt (funcall call-back cnt) (ding)))
3571 (funcall call-back cnt)) 4729
3572 4730 ;; Help
3573 ((equal key ?\?) 4731 ((eq key ?\?)
3574 (save-window-excursion 4732 (with-output-to-temp-buffer "*RefTeX Help*"
3575 (with-output-to-temp-buffer "*RefTeX Help*" 4733 (princ help-string))
3576 (princ help-string)) 4734 (setq win1 (selected-window)
3577 (setq unread-command-events 4735 win2 (get-buffer-window "*RefTeX Help*" t))
3578 (cons 4736 (select-window win2)
3579 (cond 4737 (unless (and (pos-visible-in-window-p 1)
3580 ((fboundp 'read-event) ; Emacs 4738 (pos-visible-in-window-p (point-max)))
3581 (read-event)) 4739 (enlarge-window (1+ (- (count-lines 1 (point-max))
3582 ((fboundp 'next-command-event) ; XEmacs 4740 (window-height)))))
3583 (next-command-event)) 4741 (select-window win1)
3584 (t (error "Please report this problem to dominik@strw.leidenuniv.nl"))) 4742 (setq skip-callback t))
3585 nil))) 4743
3586 (kill-buffer "*RefTeX Help*")) 4744 ;; Searching
3587 4745 ((or (setq forward (eq key ?\C-s)) (eq key ?\C-r))
3588 ((equal key ?\C-r) 4746 (if (or (and (not (eq last-key ?\C-s))
3589 ;; sje - code copied from ispell.el for 4747 (not (eq last-key ?\C-r)))
3590 ;; performing recursive edit 4748 (string= search-str ""))
4749 (setq tmp ; get a new string
4750 (read-from-minibuffer
4751 (if (string= search-str "")
4752 "Search: "
4753 (format "Search [%s]:" search-str))
4754 nil mini-map)
4755 search-str (if (string= tmp "")
4756 search-str tmp)))
4757 (setq search-start (point))
4758 (and (not (string= search-str ""))
4759 (progn
4760 (while
4761 (and (setq matched
4762 (if forward
4763 (search-forward search-str nil 1)
4764 (search-backward search-str nil 1)))
4765 (or (>= (save-excursion
4766 (goto-char (match-beginning 0))
4767 (current-column))
4768 (window-width))
4769 (not (or (get-text-property (point) 'cnt)
4770 match-everywhere)))))
4771 (if matched
4772 (reftex-highlight 2 (match-beginning 0)
4773 (match-end 0))
4774 (ding)
4775 (goto-char search-start)))))
4776
4777 ;; Recursive edit
4778 ((eq key ?e)
3591 (set-marker reftex-recursive-edit-marker (point)) 4779 (set-marker reftex-recursive-edit-marker (point))
3592 (unwind-protect 4780 (unwind-protect
3593 (progn 4781 (progn
3594 (save-window-excursion 4782 (save-window-excursion
3595 (save-excursion 4783 (save-excursion
3596 (other-window 1) 4784 (other-window 1)
3597 (message 4785 (message
3598 (substitute-command-keys 4786 (substitute-command-keys
3599 "Recursive edit. Return to selection with \\[exit-recursive-edit]")) 4787 "Recursive edit. Return to selection with \\[exit-recursive-edit]"))
3600 (recursive-edit))) 4788 (recursive-edit)))
3601 (if (not (equal (marker-buffer 4789 (unless (equal (marker-buffer
3602 reftex-recursive-edit-marker) 4790 reftex-recursive-edit-marker)
3603 (current-buffer))) 4791 (current-buffer))
3604 (error 4792 (error "Cannot continue RefTeX from this buffer."))
3605 "Cannot continue RefTeX from this buffer."))
3606 (goto-char reftex-recursive-edit-marker)) 4793 (goto-char reftex-recursive-edit-marker))
3607 (set-marker reftex-recursive-edit-marker nil))) 4794 (set-marker reftex-recursive-edit-marker nil)))
3608 4795
3609 ((member key event-list)
3610 (throw 'exit key))
3611 (t 4796 (t
3612 (ding))))))) 4797 (ding)))))))
4798 (and (get-buffer "*RefTeX Help*") (kill-buffer "*RefTeX Help*"))
3613 (message "") 4799 (message "")
3614 (cons ev cnt))) 4800 (list ev cnt last-cnt)))
3615 4801
3616 ;;; =========================================================================== 4802 ;;; ===========================================================================
3617 ;;; 4803 ;;;
3618 ;;; View cross references 4804 ;;; View cross references
3619 4805
3622 If the macro at point is a \\ref, show the corresponding label definition. 4808 If the macro at point is a \\ref, show the corresponding label definition.
3623 If it is a \\cite, show the BibTeX database entry. 4809 If it is a \\cite, show the BibTeX database entry.
3624 If there is no such macro at point, search forward to find one. 4810 If there is no such macro at point, search forward to find one.
3625 When you call this function several times in direct successtion, point will 4811 When you call this function several times in direct successtion, point will
3626 move to view subsequent cross references further down in the buffer. 4812 move to view subsequent cross references further down in the buffer.
4813 To cope with the plethora of variations in packages, this function
4814 assumes any macro either starting with ending in `ref' or `cite' to contain
4815 cross references.
3627 With argument, actually select the window showing the cross reference." 4816 With argument, actually select the window showing the cross reference."
3628 4817
3629 (interactive "P") 4818 (interactive "P")
3630 4819
3631 ;; See where we are. 4820 ;; See where we are.
3632 (let* ((pos (point)) 4821 (let* ((re "\\\\\\([a-z]*\\(cite\\|ref\\)\\|\\(cite\\|ref\\)[a-z]*\\)\\**\\(\\[[^{}]*\\]\\)?{")
3633 (re "\\\\[a-z]*\\(cite\\|ref\\)\\(\\[[^{}]*\\]\\)?{\\([^}]+\\)}") 4822 (macro (car (car (reftex-what-macro t))))
3634 (my-window (get-buffer-window (current-buffer))) 4823 (this-word (reftex-this-word "*a-zA-Z\\\\"))
3635 pop-window cmd args macro label key-start point) 4824 (my-window (selected-window))
3636 4825 pop-window cmd args point)
3637 (if (save-excursion 4826
3638 (forward-char 1) 4827 (if (and macro
3639 (and (search-backward "\\" nil t) 4828 (string-match "\\`\\\\cite\\|\\`\\\\ref\\|cite\\'\\|ref\\'"
3640 (looking-at re) 4829 macro))
3641 (< pos (match-end 0)))) 4830 (and (setq macro (match-string 0 macro))
3642 (setq macro (match-string 1) 4831 (string-match "\\`\\\\" macro)
3643 key-start (match-beginning 3))) 4832 (setq macro (substring macro 1)))
4833 (setq macro nil))
3644 4834
3645 (if (and macro (eq last-command this-command)) 4835 (if (and macro (eq last-command this-command))
3646 (if (and (string= macro "cite") 4836 (if (string= macro "cite")
3647 (skip-chars-forward "^}, \t\n\r") 4837 (progn
3648 (= (following-char) ?,)) 4838 (skip-chars-forward "^},%")
3649 (setq key-start (1+ (point))) 4839 (while (and (eq (following-char) ?%)
3650 (setq macro nil))) 4840 (or (beginning-of-line 2) t)
3651 4841 (skip-chars-forward " \t\n\r")))
3652 (if (not macro) 4842 (skip-chars-forward ",")
3653 (if (re-search-forward re nil t) 4843 (if (eq (following-char) ?})
3654 (setq macro (match-string 1) 4844 (setq macro nil)))
3655 key-start (match-beginning 3)) 4845 (setq macro nil)))
3656 (error "No further cross references in buffer"))) 4846
3657 4847 (if (and (not macro)
3658 (goto-char key-start) 4848 (or (not (string-match "\\`\\\\" this-word))
4849 (eq (following-char) ?\\)
4850 (search-backward "\\" nil t)
4851 t))
4852 (if (interactive-p)
4853 ;; Only move far if this function was called directly
4854 (and (re-search-forward re nil t)
4855 (setq macro (or (match-string 2) (match-string 3))))
4856 ;; The macro needs to be at point
4857 (and (looking-at re)
4858 (setq macro (or (match-string 2) (match-string 3)))
4859 (goto-char (match-end 0)))))
4860
4861
4862 (unless macro
4863 (error "No cross reference to display"))
3659 4864
3660 ;; Ensure access to scanning info 4865 ;; Ensure access to scanning info
3661 (reftex-access-scan-info) 4866 (reftex-access-scan-info)
3662 4867
3663 (cond 4868 (cond
3664 ((string= macro "cite") 4869 ((string= macro "cite")
3665 (setq cmd 'reftex-pop-to-bibtex-entry 4870 (setq cmd 'reftex-pop-to-bibtex-entry
3666 args (list 4871 args (list
3667 (reftex-no-props (reftex-this-word "^{},")) 4872 (reftex-this-word "^{},%\n\r")
3668 (reftex-get-bibfile-list) nil t))) 4873 (reftex-get-bibfile-list) nil t)))
3669 ((string= macro "ref") 4874 ((string= macro "ref")
3670 (let ((label (reftex-no-props (reftex-this-word "^{}"))) 4875 (let* ((label (reftex-this-word "^{}%\n\r"))
3671 (entry (assoc label (symbol-value reftex-list-of-labels-symbol)))) 4876 (xr-data (assoc 'xr (symbol-value reftex-docstruct-symbol)))
4877 (xr-re (nth 2 xr-data))
4878 (entry (assoc label (symbol-value reftex-docstruct-symbol))))
4879 (if (and (not entry) (string-match xr-re label))
4880 ;; Label is defined in external document
4881 (save-excursion
4882 (save-match-data
4883 (set-buffer
4884 (or (reftex-get-file-buffer-force
4885 (cdr (assoc (match-string 1 label) (nth 1 xr-data))))
4886 (error "Problem with external label %s" label))))
4887 (setq label (substring label (match-end 1)))
4888 (reftex-access-scan-info)
4889 (setq entry
4890 (assoc label (symbol-value reftex-docstruct-symbol)))))
3672 (if entry 4891 (if entry
3673 (setq cmd 'reftex-pop-to-label 4892 (setq cmd 'reftex-pop-to-label
3674 args (list label (list (nth 3 entry)) nil t)) 4893 args (list label (list (nth 3 entry)) nil t))
3675 (error "Label %s not known - reparse document might help" label)))) 4894 (error "Label %s not known - reparse document might help" label))))
3676 (t (error "This should not happen"))) 4895 (t (error "This should not happen (reftex-view-crossref)")))
3677 (setq point (point)) 4896 (setq point (point))
3678 (apply cmd args) 4897 (apply cmd args)
3679 (setq pop-window (selected-window)) 4898 (setq pop-window (selected-window))
3680 (add-hook 'pre-command-hook 'reftex-highlight-shall-die) 4899 (add-hook 'pre-command-hook 'reftex-highlight-shall-die)
3681 (select-window my-window) 4900 (select-window my-window)
3688 If it is a \\cite, show the BibTeX database entry. 4907 If it is a \\cite, show the BibTeX database entry.
3689 If there is no such macro at point, search forward to find one. 4908 If there is no such macro at point, search forward to find one.
3690 With argument, actually select the window showing the cross reference." 4909 With argument, actually select the window showing the cross reference."
3691 (interactive "e") 4910 (interactive "e")
3692 (mouse-set-point ev) 4911 (mouse-set-point ev)
4912 (setq last-command 'self-insert-command) ;; make sure we do not move!
3693 (reftex-view-crossref current-prefix-arg)) 4913 (reftex-view-crossref current-prefix-arg))
3694 4914
3695 ;;; =========================================================================== 4915 ;;; ===========================================================================
3696 ;;; 4916 ;;;
3697 ;;; Functions that check out the surroundings 4917 ;;; Functions that check out the surroundings
3716 (catch 'exit 4936 (catch 'exit
3717 (if (null which) (throw 'exit nil)) 4937 (if (null which) (throw 'exit nil))
3718 (let ((bound (or bound (save-excursion (re-search-backward 4938 (let ((bound (or bound (save-excursion (re-search-backward
3719 reftex-section-regexp nil 1) 4939 reftex-section-regexp nil 1)
3720 (point)))) 4940 (point))))
3721 pos cmd-list cmd) 4941 pos cmd-list cmd cnt cnt-opt entry)
3722 (save-restriction 4942 (save-restriction
3723 (save-excursion 4943 (save-excursion
3724 (narrow-to-region (max 1 bound) (point-max)) 4944 (narrow-to-region (max 1 bound) (point-max))
3725 ;; move back out of the current parenthesis 4945 ;; move back out of the current parenthesis
3726 (while (condition-case nil 4946 (while (condition-case nil
3727 (progn (up-list -1) t) 4947 (progn (up-list -1) t)
3728 (error nil)) 4948 (error nil))
4949 (setq cnt 1 cnt-opt 0)
3729 ;; move back over any touching sexps 4950 ;; move back over any touching sexps
3730 (while (or (= (preceding-char) ?\]) 4951 (while (and (reftex-move-to-previous-arg bound)
3731 (= (preceding-char) ?\})) 4952 (condition-case nil
3732 (backward-sexp)) 4953 (progn (backward-sexp) t)
4954 (error nil)))
4955 (if (eq (following-char) ?\[) (incf cnt-opt))
4956 (incf cnt))
3733 (setq pos (point)) 4957 (setq pos (point))
3734 (if (and (or (= (following-char) ?\[) 4958 (when (and (or (= (following-char) ?\[)
3735 (= (following-char) ?\{)) 4959 (= (following-char) ?\{))
3736 (and (re-search-backward "\\(\\\\[a-zA-Z]+\\)" nil t) 4960 (re-search-backward "\\\\[*a-zA-Z]+\\=" nil t))
3737 (= (match-end 0) pos))) 4961 (setq cmd (reftex-match-string 0))
3738 (progn 4962 (when (looking-at "\\\\begin{[^}]*}")
3739 (setq cmd (buffer-substring-no-properties 4963 (setq cmd (reftex-match-string 0)
3740 (match-beginning 0) (match-end 0))) 4964 cnt (1- cnt)))
3741 (if (eq t which) 4965 ;; This does ignore optional arguments. Very hard to fix.
3742 (setq cmd-list (cons (cons cmd (point)) cmd-list)) 4966 (when (setq entry (assoc cmd reftex-env-or-mac-alist))
3743 (if (member cmd which) 4967 (if (> cnt (or (nth 4 entry) 100))
3744 (throw 'exit (cons cmd (point))))))) 4968 (setq cmd nil)))
4969 (cond
4970 ((null cmd))
4971 ((eq t which)
4972 (push (cons cmd (point)) cmd-list))
4973 ((member cmd which)
4974 (throw 'exit (cons cmd (point))))))
3745 (goto-char pos))) 4975 (goto-char pos)))
3746 (nreverse cmd-list))))) 4976 (nreverse cmd-list)))))
3747 4977
3748 (defun reftex-what-environment (which &optional bound) 4978 (defun reftex-what-environment (which &optional bound)
3749 ;; Find out if point is inside a LaTeX environment. 4979 ;; Find out if point is inside a LaTeX environment.
3774 ((string= (match-string 1) "end") 5004 ((string= (match-string 1) "end")
3775 (add-to-list 'end-list env)) 5005 (add-to-list 'end-list env))
3776 ((member env end-list) 5006 ((member env end-list)
3777 (setq end-list (delete env end-list))) 5007 (setq end-list (delete env end-list)))
3778 ((eq t which) 5008 ((eq t which)
3779 (setq env-list (cons (cons env (point)) env-list))) 5009 (push (cons env (point)) env-list))
3780 ((member env which) 5010 ((member env which)
3781 (throw 'exit (cons env (point)))))) 5011 (throw 'exit (cons env (point))))))
3782 (nreverse env-list))))) 5012 (nreverse env-list)))))
3783 5013
3784 (defun reftex-word-before-point () 5014 (defun reftex-word-before-point ()
3785 ;; Return the word before point. Word means here: 5015 ;; Return the word before point. Word means here:
3786 ;; Consists of [a-zA-Z0-9.:] and ends at point or whitespace. 5016 ;; Consists of [a-zA-Z0-9.:] and ends at point or whitespace.
3787 (let ((pos (point))) 5017 (let ((pos (point)))
3788 (save-excursion 5018 (save-excursion
3789 (re-search-backward "[^ \t\n\r]" (point-min) 1) 5019 (re-search-backward "[^ \t\n\r]" (point-min) 1)
3790 (setq pos (1+ (point))) 5020 (setq pos (min (1+ (point)) (point-max)))
3791 (if (re-search-backward "[^a-zA-Z0-9\\\.:]" (point-min) 1) 5021 (if (re-search-backward "[^a-zA-Z0-9\\\.:]" (point-min) 1)
3792 (forward-char 1)) 5022 (forward-char 1))
3793 (buffer-substring-no-properties (point) pos)))) 5023 (buffer-substring-no-properties (point) pos))))
3794 5024
3795 ;; ============================================================================ 5025 ;; ============================================================================
3800 ;; Return STRING with all text properties removed 5030 ;; Return STRING with all text properties removed
3801 (and (stringp string) 5031 (and (stringp string)
3802 (set-text-properties 0 (length string) nil string)) 5032 (set-text-properties 0 (length string) nil string))
3803 string) 5033 string)
3804 5034
3805 (defun reftex-split (regexp string) 5035 (defun reftex-match-string (n)
3806 ;; Split like perl 5036 ;; Match string without properties
3807 (let ((start 0) list) 5037 (when (match-beginning n)
3808 (while (string-match regexp string start) 5038 (buffer-substring-no-properties (match-beginning n) (match-end n))))
3809 (setq list (cons (substring string start (match-beginning 0)) list)) 5039
3810 (setq start (match-end 0))) 5040 (defun reftex-this-word (&optional class)
3811 (setq list (nreverse (cons (substring string start) list))))) 5041 ;; Grab the word around point.
3812 5042 (setq class (or class "-a-zA-Z0-9:_/.*;|"))
3813 (defun reftex-allow-for-ctrl-m (string) 5043 (save-excursion
3814 ;; convert STRING into a regexp, allowing ^M for \n 5044 (buffer-substring-no-properties
5045 (progn (skip-chars-backward class) (point))
5046 (progn (skip-chars-forward class) (point)))))
5047
5048 (defvar enable-multibyte-characters)
5049 (defun reftex-truncate (string ncols &optional ellipses padding)
5050 ;; Truncate a string to NCHAR characters.
5051 ;; Works fast with ASCII and correctly with Mule characters.
5052 ;; When ELLIPSES is non-nil, put three dots at the end of the string.
5053 (setq string
5054 (cond
5055 ((and (boundp 'enable-multibyte-characters)
5056 enable-multibyte-characters)
5057 (if (<= (string-width string) ncols)
5058 string
5059 (if ellipses
5060 (concat (truncate-string-to-width string (- ncols 3)) "...")
5061 (truncate-string-to-width string ncols))))
5062 (t
5063 (if (<= (length string) ncols)
5064 string
5065 (if ellipses
5066 (concat (substring string 0 (- ncols 3)) "...")
5067 (substring string 0 ncols))))))
5068 (if padding
5069 (format (format "%%-%ds" ncols) string)
5070 string))
5071
5072 (defun reftex-nearest-match (regexp &optional pos)
5073 ;; Find the nearest match of REGEXP. Set the match data.
5074 ;; If POS is given, calculate distances relative to it.
5075 ;; Return nil if there is no match.
5076 (let ((start (point)) (pos (or pos (point))) match1 match2 match)
5077 (goto-char start)
5078 (when (re-search-backward regexp nil t)
5079 (setq match1 (match-data)))
5080 (goto-char start)
5081 (when (re-search-forward regexp nil t)
5082 (setq match2 (match-data)))
5083 (goto-char start)
5084 (setq match
5085 (cond
5086 ((not match1) match2)
5087 ((not match2) match1)
5088 ((< (abs (- pos (car match1))) (abs (- pos (car match2)))) match1)
5089 (t match2)))
5090 (if match (progn (store-match-data match) t) nil)))
5091
5092 (defun reftex-auto-mode-alist ()
5093 ;; Return an `auto-mode-alist' with only the .gz (etc) thingies.
5094 ;; Stolen from gnus nnheader.
5095 (let ((alist auto-mode-alist)
5096 out)
5097 (while alist
5098 (when (listp (cdr (car alist)))
5099 (push (car alist) out))
5100 (pop alist))
5101 (nreverse out)))
5102
5103 (defun reftex-access-search-path (which &optional recurse file)
5104 ;; Access path from environment variables. WHICH is either "tex" or "bib".
5105 ;; When RECURSE is t, expand recursive paths, ending in double slash
5106 ;; FILE is just for the message.
5107 (let* ((pathvar (intern (concat "reftex-" which "-path")))
5108 (status (get pathvar 'status)))
5109 (cond
5110 ((eq status 'recursed))
5111 ((and status (null recurse)))
5112 ((null status)
5113 (let ((env-vars (if (equal which "tex") (list "TEXINPUTS")
5114 reftex-bibpath-environment-variables)))
5115 (set pathvar (reftex-parse-colon-path
5116 (mapconcat '(lambda(x) (or (getenv x) ""))
5117 env-vars path-separator))))
5118 (put pathvar 'status 'split))
5119 ((and (eq 'split status) recurse)
5120 (message "Expanding search path to find %s file: %s ..." which file)
5121 (set pathvar (reftex-expand-path (symbol-value pathvar)))
5122 (put pathvar 'status 'recursed)))))
5123
5124 (defun reftex-find-file-on-path (file path)
5125 ;; Find FILE along the directory list PATH.
5126 (catch 'exit
5127 (when (file-name-absolute-p file)
5128 (if (file-exists-p file)
5129 (throw 'exit file)
5130 (throw 'exit nil)))
5131 (let* ((thepath path) file1 dir
5132 (doubleslash (concat "/" "/")))
5133 (while (setq dir (pop thepath))
5134 (when (string= (substring dir -2) doubleslash)
5135 (setq dir (substring dir 0 -1)))
5136 (setq file1 (expand-file-name file dir))
5137 (if (file-exists-p file1)
5138 (throw 'exit file1)))
5139 ;; No such file
5140 nil)))
5141
5142 (defun reftex-parse-colon-path (path)
5143 ;; Like parse-colon-parse, but // or /~ have no effects.
5144 (mapcar 'file-name-as-directory
5145 (delete "" (split-string path (concat path-separator "+")))))
5146
5147 (defun reftex-expand-path (path)
5148 ;; Expand parts of path ending in a double slash
5149 (let (path1 dir dirs (doubleslash (concat "/" "/")))
5150 (while (setq dir (pop path))
5151 (if (string= (substring dir -2) doubleslash)
5152 (progn
5153 (setq dir (substring dir 0 -1))
5154 (setq dirs (reftex-recursive-directory-list dir))
5155 (setq path1 (append dirs path1)))
5156 (push dir path1)))
5157 (nreverse path1)))
5158
5159 (defun reftex-recursive-directory-list (dir)
5160 (let ((path (list dir)) dirs path1)
5161 (while (setq dir (pop path))
5162 (setq dirs
5163 (delete nil
5164 (mapcar (function
5165 (lambda (x)
5166 (if (and (file-directory-p x)
5167 (not (string-match "/\\.+\\'" x)))
5168 (file-name-as-directory x)
5169 nil)))
5170 (directory-files dir t))))
5171 (setq path (append dirs path))
5172 (push dir path1))
5173 path1))
5174
5175 (defun reftex-make-regexp-allow-for-ctrl-m (string)
5176 ;; convert STRING into a regexp, allowing ^M for \n and vice versa
3815 (let ((start -2)) 5177 (let ((start -2))
3816 (setq string (regexp-quote string)) 5178 (setq string (regexp-quote string))
3817 (while (setq start (string-match "[\n\r]" string (+ 3 start))) 5179 (while (setq start (string-match "[\n\r]" string (+ 3 start)))
3818 (setq string (replace-match "[\n\r]" nil t string))) 5180 (setq string (replace-match "[\n\r]" nil t string)))
3819 string)) 5181 string))
5182
5183 (defun reftex-make-desparate-section-regexp (old)
5184 ;; Return a regexp which will still match a section statement even if
5185 ;; x-symbol or isotex or the like have been at work in the mean time.
5186 (let* ((n (1+ (string-match "[[{]" old)))
5187 (new (regexp-quote (substring old 0 (1+ (string-match "[[{]" old)))))
5188 (old (substring old n)))
5189 (while (string-match
5190 "\\([\r\n]\\)\\|\\(\\`\\|[ \t\n\r]\\)\\([a-zA-Z0-9]+\\)\\([ \t\n\r]\\|}\\'\\)"
5191 old)
5192 (if (match-beginning 1)
5193 (setq new (concat new "[^\n\r]*[\n\r]"))
5194 (setq new (concat new "[^\n\r]*" (match-string 3 old))))
5195 (setq old (substring old (match-end 0))))
5196 new))
3820 5197
3821 (defun reftex-delete-list (elt-list list) 5198 (defun reftex-delete-list (elt-list list)
3822 ;; like delete, but with a list of things to delete 5199 ;; like delete, but with a list of things to delete
3823 ;; (original code from Rory Molinari) 5200 ;; (original code from Rory Molinari)
3824 (while elt-list 5201 (while elt-list
3827 list) 5204 list)
3828 5205
3829 (defun reftex-get-buffer-visiting (file) 5206 (defun reftex-get-buffer-visiting (file)
3830 ;; return a buffer visiting FILE 5207 ;; return a buffer visiting FILE
3831 (cond 5208 (cond
3832 ((fboundp 'find-buffer-visiting) ; Emacs
3833 (find-buffer-visiting file))
3834 ((boundp 'find-file-compare-truenames) ; XEmacs 5209 ((boundp 'find-file-compare-truenames) ; XEmacs
3835 (let ((find-file-compare-truenames t)) 5210 (let ((find-file-compare-truenames t))
3836 (get-file-buffer file))) 5211 (get-file-buffer file)))
5212 ((fboundp 'find-buffer-visiting) ; Emacs
5213 (find-buffer-visiting file))
3837 (t (error "Please report this problem to dominik@strw.leidenuniv.nl")))) 5214 (t (error "Please report this problem to dominik@strw.leidenuniv.nl"))))
3838 5215
3839 (defun reftex-get-file-buffer-force (file &optional mark-to-kill) 5216 (defun reftex-get-file-buffer-force (file &optional mark-to-kill)
3840 ;; Return a buffer visiting file. Make one, if necessary. 5217 ;; Return a buffer visiting file. Make one, if necessary.
3841 ;; If neither such a buffer no the file exist, return nil. 5218 ;; If neither such a buffer nor the file exist, return nil.
3842 ;; If MARK-TO-KILL in non-nil, put any new buffers into the kill list." 5219 ;; If MARK-TO-KILL is t and there is no live buffer, load the file with
5220 ;; initializations according to `reftex-initialize-temporary-buffers',
5221 ;; and mark the buffer to be killed after use.
3843 5222
3844 (let ((buf (reftex-get-buffer-visiting file))) 5223 (let ((buf (reftex-get-buffer-visiting file)))
3845 (cond 5224
3846 (buf buf) 5225 (cond (buf
3847 ((file-exists-p file) 5226 ;; We have it already as a buffer - just return it
3848 (setq buf (find-file-noselect file)) 5227 buf)
3849 (if mark-to-kill 5228
3850 (add-to-list 'reftex-buffers-to-kill buf)) 5229 ((file-readable-p file)
3851 buf) 5230 ;; At least there is such a file and we can read it.
3852 (t nil)))) 5231
5232 (if (or (not mark-to-kill)
5233 (eq t reftex-initialize-temporary-buffers))
5234
5235 ;; Visit the file with full magic
5236 (setq buf (find-file-noselect file))
5237
5238 ;; Else: Visit the file just briefly, without or
5239 ;; with limited Magic
5240
5241 ;; The magic goes away
5242 (let ((format-alist nil)
5243 (auto-mode-alist (reftex-auto-mode-alist))
5244 (default-major-mode 'fundamental-mode)
5245 (after-insert-file-functions nil))
5246 (setq buf (find-file-noselect file)))
5247
5248 ;; Is there a hook to run?
5249 (when (listp reftex-initialize-temporary-buffers)
5250 (save-excursion
5251 (set-buffer buf)
5252 (run-hooks 'reftex-initialize-temporary-buffers))))
5253
5254 ;; Lets see if we got a license to kill :-|
5255 (and mark-to-kill
5256 (add-to-list 'reftex-buffers-to-kill buf))
5257
5258 ;; Return the new buffer
5259 buf)
5260
5261 ;; If no such file exists, return nil
5262 (t nil))))
3853 5263
3854 (defun reftex-splice-symbols-into-list (list alist) 5264 (defun reftex-splice-symbols-into-list (list alist)
3855 ;; Splice the association in ALIST of any symbols in LIST into the list. 5265 ;; Splice the association in ALIST of any symbols in LIST into the list.
3856 ;; Return new list. 5266 ;; Return new list.
3857 (let (rtn tmp) 5267 (let (rtn tmp)
3858 (while list 5268 (while list
3859 (while (and (not (null (car list))) 5269 (while (and (not (null (car list))) ;; keep list elements nil
3860 (symbolp (car list))) 5270 (symbolp (car list)))
3861 (setq tmp (car list)) 5271 (setq tmp (car list))
3862 (cond 5272 (cond
3863 ((assoc tmp alist) 5273 ((assoc tmp alist)
3864 (setq list (append (cdr (cdr (assoc tmp alist))) (cdr list)))) 5274 (setq list (append (nth 2 (assoc tmp alist)) (cdr list))))
3865 (t 5275 (t
3866 (error "Cannot treat symbol %s in reftex-label-alist" 5276 (error "Cannot treat symbol %s in reftex-label-alist"
3867 (symbol-name tmp))))) 5277 (symbol-name tmp)))))
3868 (setq rtn (cons (car list) rtn) 5278 (push (pop list) rtn))
3869 list (cdr list)))
3870 (nreverse rtn))) 5279 (nreverse rtn)))
3871 5280
3872 (defun reftex-uniquify (alist &optional keep-list) 5281 (defun reftex-uniquify (alist &optional keep-list)
3873 ;; Return a list of all elements in ALIST, but each car only once. 5282 ;; Return a list of all elements in ALIST, but each car only once.
3874 ;; Elements of KEEP-LIST are not removed even if duplicate. 5283 ;; Elements of KEEP-LIST are not removed even if duplicate.
3875 (let (new elm) 5284 (let (new elm)
3876 (while alist 5285 (while alist
3877 (setq elm (car alist) 5286 (setq elm (pop alist))
3878 alist (cdr alist))
3879 (if (or (member (car elm) keep-list) 5287 (if (or (member (car elm) keep-list)
3880 (not (assoc (car elm) new))) 5288 (not (assoc (car elm) new)))
3881 (setq new (cons elm new)))) 5289 (push elm new)))
3882 (setq new (nreverse new)) 5290 (nreverse new)))
3883 new))
3884 5291
3885 (defun reftex-use-fonts () 5292 (defun reftex-use-fonts ()
3886 ;; Return t if we can and want to use fonts. 5293 ;; Return t if we can and want to use fonts.
3887 (and window-system 5294 (and window-system
3888 reftex-use-fonts 5295 reftex-use-fonts
3889 (boundp 'font-lock-keyword-face))) 5296 (featurep 'font-lock)))
5297
5298 (defun reftex-refontify ()
5299 ;; Return t if we need to refontify context
5300 (and (reftex-use-fonts)
5301 (or (eq t reftex-refontify-context)
5302 (and (eq 1 reftex-refontify-context)
5303 (or (featurep 'x-symbol))))))
3890 5304
3891 ;; Highlighting uses overlays. If this is for XEmacs, we need to load 5305 ;; Highlighting uses overlays. If this is for XEmacs, we need to load
3892 ;; the overlay library, available in version 19.15 5306 ;; the overlay library, available in version 19.15
3893 (and (not (fboundp 'make-overlay)) 5307 (and (not (fboundp 'make-overlay))
3894 (condition-case nil 5308 (condition-case nil
3895 (require 'overlay) 5309 (require 'overlay)
3896 ('error 5310 (error
3897 (error "RefTeX needs overlay emulation (available in XEmacs 19.15)")))) 5311 (error "RefTeX needs overlay emulation (available in XEmacs 19.15)"))))
3898 5312
3899 ;; We keep a vector with several different overlays to do our highlighting. 5313 ;; We keep a vector with several different overlays to do our highlighting.
3900 (defvar reftex-highlight-overlays [nil nil]) 5314 (defvar reftex-highlight-overlays [nil nil nil])
3901 5315
3902 ;; Initialize the overlays 5316 ;; Initialize the overlays
3903 (aset reftex-highlight-overlays 0 (make-overlay 1 1)) 5317 (aset reftex-highlight-overlays 0 (make-overlay 1 1))
3904 (overlay-put (aref reftex-highlight-overlays 0) 'face 'highlight) 5318 (overlay-put (aref reftex-highlight-overlays 0) 'face 'highlight)
3905 (aset reftex-highlight-overlays 1 (make-overlay 1 1)) 5319 (aset reftex-highlight-overlays 1 (make-overlay 1 1))
3906 (overlay-put (aref reftex-highlight-overlays 1) 'face 'highlight) 5320 (overlay-put (aref reftex-highlight-overlays 1) 'face 'highlight)
5321 (aset reftex-highlight-overlays 2 (make-overlay 1 1))
5322 (overlay-put (aref reftex-highlight-overlays 2) 'face
5323 (if (string-match "XEmacs" emacs-version) 'zmacs-region 'region))
3907 5324
3908 ;; Two functions for activating and deactivation highlight overlays 5325 ;; Two functions for activating and deactivation highlight overlays
3909 (defun reftex-highlight (index begin end &optional buffer) 5326 (defun reftex-highlight (index begin end &optional buffer)
3910 "Highlight a region with overlay INDEX." 5327 "Highlight a region with overlay INDEX."
3911 (move-overlay (aref reftex-highlight-overlays index) 5328 (move-overlay (aref reftex-highlight-overlays index)
3919 (remove-hook 'pre-command-hook 'reftex-highlight-shall-die) 5336 (remove-hook 'pre-command-hook 'reftex-highlight-shall-die)
3920 (reftex-unhighlight 0)) 5337 (reftex-unhighlight 0))
3921 5338
3922 ;;; --------------------------------------------------------------------------- 5339 ;;; ---------------------------------------------------------------------------
3923 ;;; 5340 ;;;
3924 ;;; Cursor position after insertion of forms
3925
3926 (defun reftex-position-cursor ()
3927 ;; Search back to question mark, delete it, leave point there
3928 (if (search-backward "\?" (- (point) 100) t)
3929 (delete-char 1)))
3930
3931 (defun reftex-item ()
3932 "Insert an \\item and provide a label if the environments supports that."
3933 (interactive)
3934 (let ((env (car
3935 (reftex-what-environment '("itemize" "enumerate" "eqnarray")))))
3936
3937 (if (and env (not (bolp))) (newline))
3938
3939 (cond
3940
3941 ((string= env "eqnarray")
3942 (if (not (bolp))
3943 (newline))
3944 (reftex-label env)
3945 (insert "\n & & ")
3946 (beginning-of-line 1))
3947
3948 ((string= env "itemize")
3949 (newline)
3950 (insert "\\item "))
3951
3952 ((string= env "enumerate")
3953 (newline)
3954 (insert "\\item")
3955 (reftex-label env)
3956 (insert " "))
3957 (t
3958 (error "\\item command does not make sense here...")))))
3959
3960 ;;; ---------------------------------------------------------------------------
3961 ;;; ---------------------------------------------------------------------------
3962 ;;; ---------------------------------------------------------------------------
3963 ;;;
3964 ;;; Data Section: Definition of large constants
3965
3966
3967 (defconst reftex-label-alist-builtin
3968 '(
3969 (LaTeX
3970 "LaTeX default environments"
3971 ("section" ?s "sec:" "~\\ref{%s}" t
3972 ("Part" "Chapter" "Chap." "Section" "Sec." "Sect." "Paragraph" "Par."
3973 "\\S" "Teil" "Kapitel" "Kap." "Abschnitt" ))
3974
3975 ("enumerate" ?n "item:" "~\\ref{%s}" "\\\\item\\(\\[[^]]*\\]\\)?"
3976 ("Item" "Punkt"))
3977
3978 ("equation" ?e "eq:" "~(\\ref{%s})" t
3979 ("Equation" "Eq." "Eqn." "Gleichung" "Gl."))
3980 ("eqnarray" ?e "eq:" nil "\\\\begin{eqnarray}\\|\\\\\\\\")
3981
3982 ("figure" ?f "fig:" "~\\ref{%s}" "\\\\caption\\(\\[[^]]*\\]\\)?{"
3983 ("Figure" "Fig." "Abbildung" "Abb."))
3984 ("figure*" ?f nil nil "\\\\caption\\(\\[[^]]*\\]\\)?{")
3985
3986 ("table" ?t "tab:" "~\\ref{%s}" "\\\\caption\\(\\[[^]]*\\]\\)?{"
3987 ("Table" "Tab." "Tabelle"))
3988 ("table*" ?t nil nil "\\\\caption\\(\\[[^]]*\\]\\)?{")
3989
3990 ("any" ?\ " " "\\ref{%s}" nil))
3991
3992 (Sideways
3993 "Sidewaysfigure and sidewaystable"
3994 ("sidewaysfigure" ?f nil nil "\\\\caption\\(\\[[^]]*\\]\\)?{")
3995 ("sidewaystable" ?t nil nil "\\\\caption\\(\\[[^]]*\\]\\)?{"))
3996
3997 (Subfigure
3998 "Subfigure environments and macro"
3999 ("subfigure" ?f nil nil "\\\\caption\\(\\[[^]]*\\]\\)?{")
4000 ("subfigure*" ?f nil nil "\\\\caption\\(\\[[^]]*\\]\\)?{")
4001 ("\\subfigure" ?f nil nil "\\\\subfigure[[{]"))
4002
4003 (AMSTeX
4004 "AMS-LaTeX: amsmath package environents"
4005 ("align" ?e "eq:" "~\\eqref{%s}" "\\\\begin{align}\\|\\\\\\\\")
4006 ("gather" ?e "eq:" nil "\\\\begin{gather}\\|\\\\\\\\")
4007 ("multline" ?e "eq:" nil t)
4008 ("flalign" ?e "eq:" nil "\\\\begin{flalign}\\|\\\\\\\\")
4009 ("alignat" ?e "eq:" nil "\\\\begin{alignat}{[0-9]*}\\|\\\\\\\\"))
4010
4011 (AASTeX
4012 "AAS deluxetable environment"
4013 ("deluxetable" ?t "tab:" nil "\\\\caption{")))
4014 "The default label environment descriptions.")
4015
4016 ;;; ---------------------------------------------------------------------------
4017 ;;;
4018 ;;; Functions to compile the tables, reset the mode etc. 5341 ;;; Functions to compile the tables, reset the mode etc.
4019 5342
4020 (defun reftex-reset-mode () 5343 (defun reftex-reset-mode ()
4021 "Reset RefTeX Mode. Required to implement changes to some list variables. 5344 "Reset RefTeX Mode. Required to implement changes to some list variables.
4022 This function will compile the information in `reftex-label-alist' and similar 5345 This function will compile the information in `reftex-label-alist' and similar
4023 variables. It is called when RefTeX is first used, and after changes to 5346 variables. It is called when RefTeX is first used, and after changes to
4024 these variables via `reftex-add-to-label-alist'." 5347 these variables."
4025 (interactive) 5348 (interactive)
4026 5349
4027 ;; Record that we have done this 5350 ;; Record that we have done this
4028 (setq reftex-tables-dirty nil) 5351 (setq reftex-tables-dirty nil)
5352 (setq reftex-memory
5353 (list reftex-label-alist reftex-label-alist-external-add-ons
5354 reftex-default-label-alist-entries))
5355
5356 ;; Reset the file search path variables
5357 (put 'reftex-tex-path 'status nil)
5358 (put 'reftex-bib-path 'status nil)
4029 5359
4030 ;; Kill temporary buffers associated with RefTeX - just in case they 5360 ;; Kill temporary buffers associated with RefTeX - just in case they
4031 ;; were not cleaned up properly 5361 ;; were not cleaned up properly
4032 (let ((buffer-list '("*reftex-master.tex*" "*RefTeX Help*" "*RefTeX Select*" 5362 (let ((buffer-list '("*RefTeX Master*" "*RefTeX Help*" "*RefTeX Select*"
4033 "*Duplicate Labels*" "*toc*" "*RefTeX-scratch*"))) 5363 "*Duplicate Labels*" "*toc*" "*RefTeX-scratch*"))
4034 (while buffer-list 5364 buf)
4035 (if (get-buffer (car buffer-list)) 5365 (while (setq buf (pop buffer-list))
4036 (kill-buffer (car buffer-list))) 5366 (if (get-buffer buf)
4037 (setq buffer-list (cdr buffer-list)))) 5367 (kill-buffer buf))))
4038 5368
4039 ;; Plug functions into AUCTeX if the user option says so 5369 ;; Make sure the current document will be rescanned soon.
5370 (reftex-reset-scanning-information)
5371
5372 ;; Plug functions into AUCTeX if the user option says so.
4040 (reftex-plug-into-AUCTeX) 5373 (reftex-plug-into-AUCTeX)
4041 5374
4042 ;; To update buffer-local variables
4043 (hack-local-variables)
4044 (message "updating internal tables...") 5375 (message "updating internal tables...")
4045 (reftex-compute-ref-cite-tables) 5376 (reftex-compute-ref-cite-tables)
4046 (message "updating internal tables... done") 5377 (message "updating internal tables... done"))
4047 (reftex-reset-scanning-information))
4048 5378
4049 (defun reftex-reset-scanning-information () 5379 (defun reftex-reset-scanning-information ()
4050 "Reset the symbols containing information from buffer scanning. 5380 "Reset the symbols containing information from buffer scanning.
4051 This enforces rescanning the buffer on next use." 5381 This enforces rescanning the buffer on next use."
4052 (if (and (string= reftex-last-toc-master (reftex-TeX-master-file)) 5382 (if (string= reftex-last-toc-master (reftex-TeX-master-file))
4053 (get-buffer "*toc*")) 5383 (reftex-empty-toc-buffer))
4054 (kill-buffer "*toc*"))
4055 (let ((symlist reftex-multifile-symbols) 5384 (let ((symlist reftex-multifile-symbols)
4056 symbol) 5385 symbol)
4057 (while symlist 5386 (while symlist
4058 (setq symbol (car symlist) 5387 (setq symbol (car symlist)
4059 symlist (cdr symlist)) 5388 symlist (cdr symlist))
4066 5395
4067 (interactive) 5396 (interactive)
4068 5397
4069 ;; Compile information in reftex-label-alist 5398 ;; Compile information in reftex-label-alist
4070 (let ((tmp (reftex-uniquify (reftex-splice-symbols-into-list 5399 (let ((tmp (reftex-uniquify (reftex-splice-symbols-into-list
4071 (append 5400 (append
4072 reftex-label-alist 5401 reftex-label-alist
4073 reftex-label-alist-external-add-ons 5402 reftex-label-alist-external-add-ons
4074 reftex-default-label-alist-entries) 5403 reftex-default-label-alist-entries)
4075 reftex-label-alist-builtin) 5404 reftex-label-alist-builtin)
4076 '(nil))) 5405 '(nil)))
4077 entry env-or-mac typekeychar typekey prefix regexp 5406 entry env-or-mac typekeychar typekey prefix context word
4078 fmt wordlist cmd qh-list) 5407 fmt reffmt labelfmt wordlist qh-list macros-with-labels
5408 nargs nlabel opt-args cell sum)
4079 5409
4080 (setq reftex-words-to-typekey-alist nil 5410 (setq reftex-words-to-typekey-alist nil
4081 reftex-typekey-list nil 5411 reftex-typekey-list nil
4082 reftex-typekey-to-format-alist nil 5412 reftex-typekey-to-format-alist nil
4083 reftex-typekey-to-prefix-alist nil 5413 reftex-typekey-to-prefix-alist nil
4084 reftex-env-or-mac-alist nil 5414 reftex-env-or-mac-alist nil
4085 reftex-label-env-list nil 5415 reftex-label-env-list nil
4086 reftex-label-mac-list nil) 5416 reftex-label-mac-list nil)
4087 (while tmp 5417 (while tmp
4088 (catch 'next-entry 5418 (catch 'next-entry
4089 (setq entry (car tmp) 5419 (setq entry (car tmp)
4090 env-or-mac (car entry) 5420 env-or-mac (car entry)
4091 entry (cdr entry) 5421 entry (cdr entry)
4092 tmp (cdr tmp)) 5422 tmp (cdr tmp))
4093 (if (null env-or-mac) 5423 (if (null env-or-mac)
4094 (setq env-or-mac "")) 5424 (setq env-or-mac ""))
4095 (if (stringp (car entry)) 5425 (if (stringp (car entry))
4096 ;; This is before version 2.00 - convert entry to new format 5426 ;; This is before version 2.00 - convert entry to new format
4097 ;; This is just to keep old users happy 5427 ;; This is just to keep old users happy
4098 (setq entry (cons (string-to-char (car entry)) 5428 (setq entry (cons (string-to-char (car entry))
4099 (cons (concat (car entry) ":") 5429 (cons (concat (car entry) ":")
4100 (cdr entry))))) 5430 (cdr entry)))))
4101 (setq typekeychar (nth 0 entry) 5431 (setq typekeychar (nth 0 entry)
4102 typekey (char-to-string typekeychar) 5432 typekey (char-to-string typekeychar)
4103 prefix (nth 1 entry) 5433 prefix (nth 1 entry)
4104 fmt (nth 2 entry) 5434 fmt (nth 2 entry)
4105 regexp (nth 3 entry) 5435 context (nth 3 entry)
4106 wordlist (nth 4 entry)) 5436 wordlist (nth 4 entry))
4107 (if (stringp wordlist) 5437 (if (stringp wordlist)
4108 ;; This is before version 2.04 - convert to new format 5438 ;; This is before version 2.04 - convert to new format
4109 (setq wordlist (nthcdr 4 entry))) 5439 (setq wordlist (nthcdr 4 entry)))
4110 (if typekey 5440
4111 (add-to-list 'reftex-typekey-list typekey)) 5441 (if (and (stringp fmt)
4112 (if (and typekey prefix) 5442 (string-match "@" fmt))
4113 (add-to-list 'reftex-typekey-to-prefix-alist (cons typekey prefix))) 5443 ;; special syntax for specifying a label format
4114 (cond 5444 (setq fmt (split-string fmt "@+"))
4115 ((string-match "\\`\\\\" env-or-mac) 5445 (setq fmt (list "\\label{%s}" fmt)))
4116 ;; It's a macro 5446 (setq labelfmt (car fmt)
4117 (add-to-list 'reftex-label-mac-list env-or-mac)) 5447 reffmt (nth 1 fmt))
4118 (t 5448 (if typekey
4119 (or (string= env-or-mac "any") 5449 (add-to-list 'reftex-typekey-list typekey))
4120 (string= env-or-mac "") 5450 (if (and typekey prefix
4121 (add-to-list 'reftex-label-env-list env-or-mac)))) 5451 (not (assoc typekey reftex-typekey-to-prefix-alist)))
4122 (and fmt 5452 (add-to-list 'reftex-typekey-to-prefix-alist
4123 (not (assoc typekey reftex-typekey-to-format-alist)) 5453 (cons typekey prefix)))
4124 (setq reftex-typekey-to-format-alist 5454 (cond
4125 (cons (cons typekey fmt) 5455 ((string-match "\\`\\\\" env-or-mac)
4126 reftex-typekey-to-format-alist))) 5456 ;; It's a macro
4127 (and (not (string= env-or-mac "any")) 5457 (let ((result (reftex-parse-args env-or-mac)))
4128 (not (string= env-or-mac "")) 5458 (setq env-or-mac (or (first result) env-or-mac)
4129 (not (assoc env-or-mac reftex-env-or-mac-alist)) 5459 nargs (second result)
4130 (setq reftex-env-or-mac-alist 5460 nlabel (third result)
4131 (cons (list env-or-mac typekey regexp) 5461 opt-args (fourth result))
4132 reftex-env-or-mac-alist))) 5462 (if nlabel (add-to-list 'macros-with-labels env-or-mac)))
4133 (while (and wordlist (stringp (car wordlist))) 5463 (add-to-list 'reftex-label-mac-list env-or-mac))
4134 (or (assoc (car wordlist) reftex-words-to-typekey-alist) 5464 (t
4135 (setq reftex-words-to-typekey-alist 5465 (setq nargs nil nlabel nil opt-args nil)
4136 (cons (cons (downcase (car wordlist)) typekey) 5466 (cond ((string= env-or-mac "any"))
4137 reftex-words-to-typekey-alist))) 5467 ((string= env-or-mac ""))
4138 (setq wordlist (cdr wordlist))) 5468 ((string= env-or-mac "section"))
4139 (cond 5469 (t
4140 ((string= "" env-or-mac) nil) 5470 (add-to-list 'reftex-label-env-list env-or-mac)
4141 ((assoc typekey qh-list) 5471 ;; Translate some special context cases
4142 (setcdr (assoc typekey qh-list) 5472 (when (assq context reftex-default-context-regexps)
4143 (concat (cdr (assoc typekey qh-list)) " " env-or-mac))) 5473 (setq context
4144 (t 5474 (format
4145 (setq qh-list (cons (cons typekey env-or-mac) qh-list)))))) 5475 (cdr (assq context reftex-default-context-regexps))
4146 5476 (regexp-quote env-or-mac))))))))
4147 (setq qh-list (nreverse qh-list)) 5477 (and reffmt
5478 (not (assoc typekey reftex-typekey-to-format-alist))
5479 (push (cons typekey reffmt) reftex-typekey-to-format-alist))
5480 (and (not (string= env-or-mac "any"))
5481 (not (string= env-or-mac ""))
5482 (not (assoc env-or-mac reftex-env-or-mac-alist))
5483 (push (list env-or-mac typekey context labelfmt
5484 nargs nlabel opt-args)
5485 reftex-env-or-mac-alist))
5486 (while (and (setq word (pop wordlist))
5487 (stringp word))
5488 (setq word (downcase word))
5489 (or (assoc word reftex-words-to-typekey-alist)
5490 (push (cons word typekey) reftex-words-to-typekey-alist)))
5491 (cond
5492 ((string= "" env-or-mac) nil)
5493 ((setq cell (assoc typekey qh-list))
5494 (push env-or-mac (cdr cell)))
5495 (t
5496 (push (list typekey env-or-mac) qh-list)))))
5497
5498 (setq qh-list (sort qh-list '(lambda (x1 x2) (string< (car x1) (car x2)))))
4148 (setq reftex-typekey-to-prefix-alist 5499 (setq reftex-typekey-to-prefix-alist
4149 (nreverse reftex-typekey-to-prefix-alist)) 5500 (nreverse reftex-typekey-to-prefix-alist))
4150 (setq reftex-type-query-prompt 5501 (setq reftex-type-query-prompt
4151 (concat "Label type: " 5502 (concat "Label type: "
4152 (mapconcat '(lambda(x) 5503 (mapconcat '(lambda(x)
4153 (format "[%s]" (car x))) 5504 (format "[%s]" (car x)))
4154 qh-list " ") 5505 qh-list " ")
4155 " (?=Help)")) 5506 " (?=Help)"))
4156 (setq reftex-type-query-help 5507 (setq reftex-type-query-help
4157 (concat "SELECT A LABEL TYPE:\n--------------------\n" 5508 (concat "SELECT A LABEL TYPE:\n--------------------\n"
4158 (mapconcat '(lambda(x) 5509 (mapconcat
4159 (format " [%s] %s" 5510 '(lambda(x)
4160 (car x) (cdr x))) 5511 (setq sum 0)
4161 qh-list "\n"))))) 5512 (format " [%s] %s"
5513 (car x)
5514 (mapconcat
5515 '(lambda(x)
5516 (setq sum (+ sum (length x)))
5517 (if (< sum 60)
5518 x
5519 (setq sum 0)
5520 (concat "\n " x)))
5521 (cdr x) " ")))
5522 qh-list "\n")))
5523
5524 ;; Calculate the regular expressions
5525 (let ((label-re "\\\\label{\\([^}]*\\)}")
5526 (include-re "\\(\\`\\|[\n\r]\\)[ \t]*\\\\\\(include\\|input\\)[{ \t]+\\([^} \t\n\r]+\\)")
5527 (section-re
5528 (concat "\\(\\`\\|[\n\r]\\)[ \t]*\\\\\\("
5529 (mapconcat 'car reftex-section-levels "\\|")
5530 "\\)\\*?\\(\\[[^]]*\\]\\)?{"))
5531 (macro-re
5532 (if macros-with-labels
5533 (concat "\\("
5534 (mapconcat 'regexp-quote macros-with-labels "\\|")
5535 "\\)[[{]")
5536 ""))
5537 (find-label-re-format
5538 (concat "\\("
5539 (mapconcat 'regexp-quote (append '("\\label")
5540 macros-with-labels) "\\|")
5541 "\\)\\([[{][^]}]*[]}]\\)*[[{]\\(%s\\)[]}]")))
5542 (setq reftex-section-regexp section-re
5543 reftex-section-or-include-regexp
5544 (concat section-re "\\|" include-re)
5545 reftex-everything-regexp
5546 (concat label-re "\\|" section-re "\\|" include-re
5547 (if macros-with-labels "\\|" "") macro-re)
5548 reftex-find-label-regexp-format find-label-re-format
5549 reftex-find-label-regexp-format2
5550 "\\([]} \t\n\r]\\)\\([[{]\\)\\(%s\\)[]}]"))))
4162 5551
4163 ;;; Keybindings -------------------------------------------------------------- 5552 ;;; Keybindings --------------------------------------------------------------
4164 5553
4165 (define-key reftex-mode-map "\C-c-" 'reftex-item)
4166 (define-key reftex-mode-map "\C-c=" 'reftex-toc) 5554 (define-key reftex-mode-map "\C-c=" 'reftex-toc)
4167 (define-key reftex-mode-map "\C-c(" 'reftex-label) 5555 (define-key reftex-mode-map "\C-c(" 'reftex-label)
4168 (define-key reftex-mode-map "\C-c)" 'reftex-reference) 5556 (define-key reftex-mode-map "\C-c)" 'reftex-reference)
4169 (define-key reftex-mode-map "\C-c[" 'reftex-citation) 5557 (define-key reftex-mode-map "\C-c[" 'reftex-citation)
4170 (define-key reftex-mode-map "\C-c&" 'reftex-view-crossref) 5558 (define-key reftex-mode-map "\C-c&" 'reftex-view-crossref)
4184 5572
4185 ;; Define a menu for the menu bar if Emacs is running under X 5573 ;; Define a menu for the menu bar if Emacs is running under X
4186 5574
4187 (require 'easymenu) 5575 (require 'easymenu)
4188 5576
4189 (easy-menu-define 5577 (easy-menu-define
4190 reftex-mode-menu reftex-mode-map 5578 reftex-mode-menu reftex-mode-map
4191 "Menu used in RefTeX mode" 5579 "Menu used in RefTeX mode"
4192 '("Ref" 5580 `("Ref"
4193 ["Table of Contents" reftex-toc t] 5581 ["Table of Contents" reftex-toc t]
4194 "----" 5582 "----"
4195 ["\\label" reftex-label t] 5583 ["\\label" reftex-label t]
4196 ["\\ref" reftex-reference t] 5584 ["\\ref" reftex-reference t]
4197 ["\\cite" reftex-citation t] 5585 ["\\cite" reftex-citation t]
4198 ["View crossref" reftex-view-crossref t] 5586 ["View Crossref" reftex-view-crossref t]
4199 "----" 5587 "----"
4200 ("Search and Replace" 5588 ("Parse Document"
4201 ["Search whole document" reftex-search-document t] 5589 ["Only this File" reftex-parse-one t]
4202 ["Replace in document" reftex-query-replace-document t] 5590 ["Entire Document" reftex-parse-all (reftex-is-multi)]
4203 ["Grep on document" reftex-grep-document t] 5591 ["Save to File" (reftex-access-parse-file 'write)
5592 (> (length (symbol-value reftex-docstruct-symbol)) 0)]
5593 ["Restore from File" (reftex-access-parse-file 'restore)
5594 (reftex-access-parse-file 'readable)]
4204 "----" 5595 "----"
4205 ["Find duplicate labels" reftex-find-duplicate-labels t] 5596 ["Enable Partial Scans"
4206 ["Change label and refs" reftex-change-label t] 5597 (setq reftex-enable-partial-scans (not reftex-enable-partial-scans))
5598 :style toggle :selected reftex-enable-partial-scans]
5599 ["Auto-Save Parse Info"
5600 (setq reftex-save-parse-info (not reftex-save-parse-info))
5601 :style toggle :selected reftex-save-parse-info]
5602 "---"
5603 ["Reset RefTeX Mode" reftex-reset-mode t])
5604 ("Multifile"
5605 ["Search Whole Document" reftex-search-document t]
5606 ["Replace in Document" reftex-query-replace-document t]
5607 ["Grep on Document" reftex-grep-document t]
4207 "----" 5608 "----"
4208 ["Create TAGS file" reftex-create-tags-file t]) 5609 ["Create TAGS File" reftex-create-tags-file t]
5610 "----"
5611 ["Find Duplicate Labels" reftex-find-duplicate-labels t]
5612 ["Change Label and Refs" reftex-change-label t])
5613 ("Citation Options"
5614 "Citation Style"
5615 ,@(mapcar
5616 (function
5617 (lambda (x)
5618 (vector
5619 (symbol-name (car x))
5620 (list 'setq 'reftex-cite-format (list 'quote (car x)))
5621 :style 'radio :selected
5622 (list 'eq 'reftex-cite-format (list 'quote (car x))))))
5623 reftex-cite-format-builtin)
5624 "----"
5625 "Bibinfo in Comments"
5626 ["Attach Comments"
5627 (setq reftex-comment-citations (not reftex-comment-citations))
5628 :style toggle :selected reftex-comment-citations]
5629 "---"
5630 "Sort Database Matches"
5631 ["by Author" (setq reftex-sort-bibtex-matches 'author)
5632 :style radio :selected (eq reftex-sort-bibtex-matches 'author)]
5633 ["by Year" (setq reftex-sort-bibtex-matches 'year)
5634 :style radio :selected (eq reftex-sort-bibtex-matches 'year)]
5635 ["by Year, reversed" (setq reftex-sort-bibtex-matches 'reverse-year)
5636 :style radio :selected (eq reftex-sort-bibtex-matches 'reverse-year)]
5637 ["Not" (setq reftex-sort-bibtex-matches nil)
5638 :style radio :selected (eq reftex-sort-bibtex-matches nil)])
4209 "----" 5639 "----"
4210 ["Parse document" reftex-parse-document t] 5640 ["Customize RefTeX" reftex-customize t]
4211 ["Reset RefTeX Mode" reftex-reset-mode t] 5641 "----"
4212 ["Customize RefTeX" reftex-customize t])) 5642 ["Show Documentation" reftex-show-commentary t]))
4213 5643
4214 ;;; Run Hook ------------------------------------------------------------------ 5644 ;;; Run Hook ------------------------------------------------------------------
4215 5645
4216 (run-hooks 'reftex-load-hook) 5646 (run-hooks 'reftex-load-hook)
4217 5647
4218 ;;; That's it! ---------------------------------------------------------------- 5648 ;;; That's it! ----------------------------------------------------------------
5649
5650 (provide 'reftex)
4219 5651
4220 ; Make sure tabels are compiled 5652 ; Make sure tabels are compiled
4221 (message "updating internal tables...") 5653 (message "updating internal tables...")
4222 (reftex-compute-ref-cite-tables) 5654 (reftex-compute-ref-cite-tables)
4223 (message "updating internal tables...done") 5655 (message "updating internal tables...done")
4224 (setq reftex-tables-dirty nil) 5656 (setq reftex-tables-dirty nil)
4225 5657
4226 (provide 'reftex)
4227
4228 ;;;============================================================================ 5658 ;;;============================================================================
4229 5659
4230 ;;; reftex.el end here 5660 ;;; reftex.el ends here
5661