Commit | Line | Data |
---|---|---|
3afbc435 | 1 | ;;; reftex.el --- minor mode for doing \label, \ref, \cite, \index in LaTeX |
ea6a015d | 2 | ;; Copyright (c) 1997, 1998, 1999, 2000, 2003 Free Software Foundation, Inc. |
a7ec1775 | 3 | |
6fbeb429 | 4 | ;; Author: Carsten Dominik <dominik@science.uva.nl> |
7ac7387b | 5 | ;; Version: 4.18 |
3afbc435 | 6 | ;; Keywords: tex |
a7ec1775 | 7 | |
2b12cfbf | 8 | ;; This file is part of GNU Emacs. |
a7ec1775 RS |
9 | |
10 | ;; GNU Emacs is free software; you can redistribute it and/or modify | |
11 | ;; it under the terms of the GNU General Public License as published by | |
12 | ;; the Free Software Foundation; either version 2, or (at your option) | |
13 | ;; any later version. | |
14 | ||
15 | ;; GNU Emacs is distributed in the hope that it will be useful, | |
16 | ;; but WITHOUT ANY WARRANTY; without even the implied warranty of | |
17 | ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
18 | ;; GNU General Public License for more details. | |
19 | ||
20 | ;; You should have received a copy of the GNU General Public License | |
21 | ;; along with GNU Emacs; see the file COPYING. If not, write to the | |
22 | ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330, | |
23 | ;; Boston, MA 02111-1307, USA. | |
24 | ||
25 | ;;--------------------------------------------------------------------------- | |
26 | ;; | |
27 | ;;; Commentary: | |
12a382d4 | 28 | ;; |
1c25ed90 CD |
29 | ;; RefTeX is a minor mode with distinct support for \ref, \label, \cite, |
30 | ;; and \index commands in (multi-file) LaTeX documents. | |
f9ad2e24 CD |
31 | ;; - A table of contents provides easy access to any part of a document. |
32 | ;; - Labels are created semi-automatically. | |
33 | ;; - Definition context of labels is provided when creating a reference. | |
34 | ;; - Citations are simplified with efficient database lookup. | |
7c4d13cc | 35 | ;; - Text phrases can be collected in a file, for later global indexing. |
1c25ed90 | 36 | ;; - The index preview buffer helps to check and edit index entries. |
a7ec1775 | 37 | ;; |
2faef409 | 38 | ;; |
a6611c0d CD |
39 | ;; INSTALLATION |
40 | ;; ------------ | |
41 | ;; | |
f9ad2e24 CD |
42 | ;; - If this file is part of an X/Emacs distribution, it is installed. |
43 | ;; - For XEmacs 21.x, you need to install the RefTeX plug-in package | |
44 | ;; available from the XEmacs distribution sites. | |
45 | ;; - If you have downloaded this file from the maintainers webpage, follow | |
46 | ;; the instructions in the INSTALL file of the distrubution. | |
a6611c0d | 47 | ;; |
f9ad2e24 | 48 | ;; To turn RefTeX Mode on and off in a buffer, use `M-x reftex-mode'. |
a7ec1775 | 49 | ;; |
f9ad2e24 CD |
50 | ;; To turn on RefTeX Mode for all LaTeX files, add the following lines |
51 | ;; to your .emacs file: | |
a7ec1775 | 52 | ;; |
f9ad2e24 CD |
53 | ;; (add-hook 'LaTeX-mode-hook 'turn-on-reftex) ; AUCTeX LaTeX mode |
54 | ;; (add-hook 'latex-mode-hook 'turn-on-reftex) ; Emacs latex mode | |
a7ec1775 | 55 | ;; |
396e0b08 | 56 | ;; |
2faef409 RS |
57 | ;; DOCUMENTATION |
58 | ;; ------------- | |
396e0b08 | 59 | ;; |
a6611c0d CD |
60 | ;; See below for a short summary of how to use RefTeX. |
61 | ;; | |
2faef409 | 62 | ;; There is an extensive texinfo document describing RefTeX in detail. |
f9ad2e24 | 63 | ;; One way to view this documentation is `M-x reftex-info RET'. |
a7ec1775 | 64 | ;; |
a6611c0d | 65 | ;; The documentation in various formats is also available at |
396e0b08 | 66 | ;; |
2faef409 | 67 | ;; http://www.strw.leidenuniv.nl/~dominik/Tools/ |
396e0b08 | 68 | ;; |
2faef409 | 69 | ;;--------------------------------------------------------------------------- |
f9ad2e24 | 70 | ;; |
7c4d13cc CD |
71 | ;; Introduction |
72 | ;; ************ | |
12a382d4 | 73 | ;; |
7c4d13cc CD |
74 | ;; RefTeX is a specialized package for support of labels, references, |
75 | ;; citations, and the index in LaTeX. RefTeX wraps itself round 4 LaTeX | |
76 | ;; macros: `\label', `\ref', `\cite', and `\index'. Using these macros | |
77 | ;; usually requires looking up different parts of the document and | |
78 | ;; searching through BibTeX database files. RefTeX automates these | |
79 | ;; time-consuming tasks almost entirely. It also provides functions to | |
80 | ;; display the structure of a document and to move around in this | |
81 | ;; structure quickly. | |
12a382d4 | 82 | ;; |
7c4d13cc CD |
83 | ;; *Note Imprint::, for information about who to contact for help, bug |
84 | ;; reports or suggestions. | |
12a382d4 | 85 | ;; |
7c4d13cc CD |
86 | ;; Environment |
87 | ;; =========== | |
12a382d4 | 88 | ;; |
7c4d13cc CD |
89 | ;; RefTeX needs to access all files which are part of a multifile |
90 | ;; document, and the BibTeX database files requested by the | |
91 | ;; `\bibliography' command. To find these files, RefTeX will require a | |
92 | ;; search path, i.e. a list of directories to check. Normally this list | |
93 | ;; is stored in the environment variables `TEXINPUTS' and `BIBINPUTS' | |
94 | ;; which are also used by RefTeX. However, on some systems these | |
95 | ;; variables do not contain the full search path. If RefTeX does not work | |
96 | ;; for you because it cannot find some files, read *Note Finding Files::. | |
12a382d4 | 97 | ;; |
7c4d13cc CD |
98 | ;; Entering RefTeX Mode |
99 | ;; ==================== | |
12a382d4 | 100 | ;; |
7c4d13cc CD |
101 | ;; To turn RefTeX Mode on and off in a particular buffer, use `M-x |
102 | ;; reftex-mode'. To turn on RefTeX Mode for all LaTeX files, add the | |
103 | ;; following lines to your `.emacs' file: | |
12a382d4 | 104 | ;; |
7c4d13cc CD |
105 | ;; (add-hook 'LaTeX-mode-hook 'turn-on-reftex) ; with AUCTeX LaTeX mode |
106 | ;; (add-hook 'latex-mode-hook 'turn-on-reftex) ; with Emacs latex mode | |
12a382d4 | 107 | ;; |
2faef409 RS |
108 | ;; RefTeX in a Nutshell |
109 | ;; ==================== | |
12a382d4 | 110 | ;; |
f9ad2e24 CD |
111 | ;; 1. Table of Contents |
112 | ;; Typing `C-c =' (`reftex-toc') will show a table of contents of the | |
1c25ed90 CD |
113 | ;; document. This buffer can display sections, labels and index |
114 | ;; entries defined in the document. From the buffer, you can jump | |
115 | ;; quickly to every part of your document. Press `?' to get help. | |
12a382d4 | 116 | ;; |
f9ad2e24 | 117 | ;; 2. Labels and References |
1c25ed90 CD |
118 | ;; RefTeX helps to create unique labels and to find the correct key |
119 | ;; for references quickly. It distinguishes labels for different | |
120 | ;; environments, knows about all standard environments (and many | |
121 | ;; others), and can be configured to recognize any additional labeled | |
122 | ;; environments you have defined yourself (variable | |
123 | ;; `reftex-label-alist'). | |
12a382d4 | 124 | ;; |
1c25ed90 CD |
125 | ;; * Creating Labels |
126 | ;; Type `C-c (' (`reftex-label') to insert a label at point. | |
127 | ;; RefTeX will either | |
128 | ;; - derive a label from context (default for section labels) | |
129 | ;; - prompt for a label string (default for figures and | |
130 | ;; tables) or | |
131 | ;; - insert a simple label made of a prefix and a number (all | |
132 | ;; other environments) | |
12a382d4 | 133 | ;; |
1c25ed90 CD |
134 | ;; Which labels are created how is configurable with the variable |
135 | ;; `reftex-insert-label-flags'. | |
12a382d4 | 136 | ;; |
1c25ed90 CD |
137 | ;; * Referencing Labels |
138 | ;; To make a reference, type `C-c )' (`reftex-reference'). This | |
139 | ;; shows an outline of the document with all labels of a certain | |
140 | ;; type (figure, equation,...) and some label context. | |
141 | ;; Selecting a label inserts a `\ref{LABEL}' macro into the | |
142 | ;; original buffer. | |
12a382d4 | 143 | ;; |
f9ad2e24 CD |
144 | ;; 3. Citations |
145 | ;; Typing `C-c [' (`reftex-citation') will let you specify a regular | |
146 | ;; expression to search in current BibTeX database files (as | |
147 | ;; specified in the `\bibliography' command) and pull out a list of | |
7c4d13cc | 148 | ;; matches for you to choose from. The list is _formatted_ and |
f9ad2e24 | 149 | ;; sorted. The selected article is referenced as `\cite{KEY}' (see |
1c25ed90 CD |
150 | ;; the variable `reftex-cite-format' if you want to insert different |
151 | ;; macros). | |
12a382d4 | 152 | ;; |
1c25ed90 CD |
153 | ;; 4. Index Support |
154 | ;; RefTeX helps to enter index entries. It also compiles all entries | |
155 | ;; into an alphabetically sorted `*Index*' buffer which you can use | |
156 | ;; to check and edit the entries. RefTeX knows about the standard | |
157 | ;; index macros and can be configured to recognize any additional | |
158 | ;; macros you have defined (`reftex-index-macros'). Multiple indices | |
159 | ;; are supported. | |
12a382d4 | 160 | ;; |
1c25ed90 | 161 | ;; * Creating Index Entries |
7c4d13cc CD |
162 | ;; To index the current selection or the word at point, type |
163 | ;; `C-c /' (`reftex-index-selection-or-word'). The default macro | |
164 | ;; `reftex-index-default-macro' will be used. For a more | |
165 | ;; complex entry type `C-c <' (`reftex-index'), select any of | |
166 | ;; the index macros and enter the arguments with completion. | |
12a382d4 | 167 | ;; |
7c4d13cc CD |
168 | ;; * The Index Phrases File (Delayed Indexing) |
169 | ;; Type `C-c \' (`reftex-index-phrase-selection-or-word') to add | |
170 | ;; the current word or selection to a special _index phrase | |
171 | ;; file_. RefTeX can later search the document for occurrences | |
172 | ;; of these phrases and let you interactively index the matches. | |
12a382d4 | 173 | ;; |
7c4d13cc | 174 | ;; * Displaying and Editing the Index |
1c25ed90 CD |
175 | ;; To display the compiled index in a special buffer, type `C-c |
176 | ;; >' (`reftex-display-index'). From that buffer you can check | |
7c4d13cc | 177 | ;; and edit all entries. |
12a382d4 | 178 | ;; |
1c25ed90 | 179 | ;; 5. Viewing Cross-References |
921759ee CD |
180 | ;; When point is on the KEY argument of a cross-referencing macro |
181 | ;; (`\label', `\ref', `\cite', `\bibitem', `\index', and variations) | |
182 | ;; or inside a BibTeX database entry, you can press `C-c &' | |
183 | ;; (`reftex-view-crossref') to display corresponding locations in the | |
184 | ;; document and associated BibTeX database files. | |
185 | ;; When the enclosing macro is `\cite' or `\ref' and no other message | |
186 | ;; occupies the echo area, information about the citation or label | |
1c25ed90 | 187 | ;; will automatically be displayed in the echo area. |
12a382d4 | 188 | ;; |
1c25ed90 CD |
189 | ;; 6. Multifile Documents |
190 | ;; Multifile Documents are fully supported. The included files must | |
191 | ;; have a file variable `TeX-master' or `tex-main-file' pointing to | |
192 | ;; the master file. RefTeX provides cross-referencing information | |
193 | ;; from all parts of the document, and across document borders | |
194 | ;; (`xr.sty'). | |
12a382d4 | 195 | ;; |
1c25ed90 | 196 | ;; 7. Document Parsing |
f9ad2e24 CD |
197 | ;; RefTeX needs to parse the document in order to find labels and |
198 | ;; other information. It does it automatically once and updates its | |
1c25ed90 CD |
199 | ;; list internally when `reftex-label' and `reftex-index' are used. |
200 | ;; To enforce reparsing, call any of the commands described above | |
201 | ;; with a raw `C-u' prefix, or press the `r' key in the label | |
202 | ;; selection buffer, the table of contents buffer, or the index | |
203 | ;; buffer. | |
12a382d4 | 204 | ;; |
1c25ed90 CD |
205 | ;; 8. AUCTeX |
206 | ;; If your major LaTeX mode is AUCTeX, RefTeX can cooperate with it | |
207 | ;; (see variable `reftex-plug-into-AUCTeX'). AUCTeX contains style | |
208 | ;; files which trigger appropriate settings in RefTeX, so that for | |
209 | ;; many of the popular LaTeX packages no additional customizations | |
210 | ;; will be necessary. | |
12a382d4 | 211 | ;; |
1c25ed90 CD |
212 | ;; 9. Useful Settings |
213 | ;; To make RefTeX faster for large documents, try these: | |
f9ad2e24 CD |
214 | ;; (setq reftex-enable-partial-scans t) |
215 | ;; (setq reftex-save-parse-info t) | |
216 | ;; (setq reftex-use-multiple-selection-buffers t) | |
12a382d4 | 217 | ;; |
1c25ed90 | 218 | ;; To integrate with AUCTeX, use |
f9ad2e24 | 219 | ;; (setq reftex-plug-into-AUCTeX t) |
12a382d4 | 220 | ;; |
1c25ed90 CD |
221 | ;; To make your own LaTeX macro definitions known to RefTeX, |
222 | ;; customize the variables | |
223 | ;; `reftex-label-alist' (for label macros/environments) | |
224 | ;; `reftex-section-levels' (for sectioning commands) | |
225 | ;; `reftex-cite-format' (for `\cite'-like macros) | |
226 | ;; `reftex-index-macros' (for `\index'-like macros) | |
227 | ;; `reftex-index-default-macro' (to set the default macro) | |
228 | ;; If you have a large number of macros defined, you may want to write | |
229 | ;; an AUCTeX style file to support them with both AUCTeX and RefTeX. | |
12a382d4 | 230 | ;; |
1c25ed90 CD |
231 | ;; 10. Where Next? |
232 | ;; Go ahead and use RefTeX. Use its menus until you have picked up | |
233 | ;; the key bindings. For an overview of what you can do in each of | |
234 | ;; the different special buffers, press `?'. Read the manual if you | |
7c4d13cc CD |
235 | ;; get stuck, of if you are curious what else might be available. |
236 | ;; The first part of the manual explains in a tutorial way how to use | |
237 | ;; and customize RefTeX. The second part is a command and variable | |
238 | ;; reference. | |
12a382d4 | 239 | ;; |
a7ec1775 RS |
240 | ;;--------------------------------------------------------------------------- |
241 | ;; | |
242 | ;; AUTHOR | |
396e0b08 | 243 | ;; ====== |
a7ec1775 RS |
244 | ;; |
245 | ;; Carsten Dominik <dominik@strw.LeidenUniv.nl> | |
246 | ;; | |
247 | ;; with contributions from Stephen Eglen | |
248 | ;; | |
f9ad2e24 CD |
249 | ;; RefTeX is bundled with Emacs and available as a plug-in package for |
250 | ;; XEmacs 21.x. If you need to install it yourself, you can find a | |
251 | ;; distribution at | |
a7ec1775 RS |
252 | ;; |
253 | ;; http://www.strw.leidenuniv.nl/~dominik/Tools/ | |
2faef409 | 254 | ;; |
a7ec1775 RS |
255 | ;; THANKS TO: |
256 | ;; --------- | |
2faef409 RS |
257 | ;; Thanks to the people on the Net who have used RefTeX and helped |
258 | ;; developing it with their reports. In particular thanks to | |
a7ec1775 | 259 | ;; |
f9ad2e24 | 260 | ;; Fran Burstall, Alastair Burt, Soren Dayton, Stephen Eglen, |
51d628c8 | 261 | ;; Karl Eichwalder, Peter Galbraith, Dieter Kraft, Kai Grossjohann, |
7c4d13cc CD |
262 | ;; Frank Harrell, Adrian Lanz, Rory Molinari, Stefan Monnier, |
263 | ;; Laurent Mugnier, Sudeep Kumar Palat, Daniel Polani, Robin Socha, | |
264 | ;; Richard Stanton, Allan Strand, Jan Vroonhof, Christoph Wedler, | |
265 | ;; Alan Williams. | |
a7ec1775 | 266 | ;; |
2faef409 | 267 | ;; Finally thanks to Uwe Bolick who first got me (some years ago) into |
f9ad2e24 | 268 | ;; supporting LaTeX labels and references with an editor (which was |
2faef409 | 269 | ;; MicroEmacs at the time). |
025bb635 | 270 | ;; |
a6611c0d | 271 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
c52bdfca | 272 | ;; |
a6611c0d | 273 | ;;;;;; |
a7ec1775 RS |
274 | \f |
275 | ;;; Code: | |
276 | ||
396e0b08 KH |
277 | (eval-when-compile (require 'cl)) |
278 | ||
a7ec1775 | 279 | ;; Stuff that needs to be there when we use defcustom |
a7ec1775 RS |
280 | (require 'custom) |
281 | ||
282 | (defvar reftex-tables-dirty t | |
283 | "Flag showing if tables need to be re-computed.") | |
284 | ||
1c25ed90 CD |
285 | ;;; ========================================================================= |
286 | ;;; | |
287 | ;;; Configuration variables | |
396e0b08 | 288 | |
1c25ed90 | 289 | (require 'reftex-vars) |
a7ec1775 | 290 | |
a7ec1775 | 291 | |
b849548d | 292 | ;;; ========================================================================= |
a7ec1775 RS |
293 | ;;; |
294 | ;;; Define the formal stuff for a minor mode named RefTeX. | |
295 | ;;; | |
296 | ||
7ac7387b | 297 | (defconst reftex-version "RefTeX version 4.18" |
a6611c0d | 298 | "Version string for RefTeX.") |
baec1250 | 299 | |
a7ec1775 | 300 | (defvar reftex-mode nil |
b849548d | 301 | "Determines if RefTeX mode is active.") |
a7ec1775 RS |
302 | (make-variable-buffer-local 'reftex-mode) |
303 | ||
304 | (defvar reftex-mode-map (make-sparse-keymap) | |
b849548d | 305 | "Keymap for RefTeX mode.") |
a7ec1775 RS |
306 | |
307 | (defvar reftex-mode-menu nil) | |
1c25ed90 | 308 | (defvar reftex-syntax-table nil) |
6fbeb429 | 309 | (defvar reftex-syntax-table-for-bib nil) |
1c25ed90 | 310 | |
70c06e39 | 311 | (unless reftex-syntax-table |
d29c87c1 | 312 | (setq reftex-syntax-table (copy-syntax-table)) |
70c06e39 CD |
313 | (modify-syntax-entry ?\( "." reftex-syntax-table) |
314 | (modify-syntax-entry ?\) "." reftex-syntax-table)) | |
12a382d4 | 315 | |
70c06e39 | 316 | (unless reftex-syntax-table-for-bib |
d29c87c1 CD |
317 | (setq reftex-syntax-table-for-bib |
318 | (copy-syntax-table reftex-syntax-table)) | |
70c06e39 CD |
319 | (modify-syntax-entry ?\' "." reftex-syntax-table-for-bib) |
320 | (modify-syntax-entry ?\" "." reftex-syntax-table-for-bib) | |
321 | (modify-syntax-entry ?\[ "." reftex-syntax-table-for-bib) | |
322 | (modify-syntax-entry ?\] "." reftex-syntax-table-for-bib)) | |
323 | ||
3b919c9f CD |
324 | ;; The following definitions are out of place, but I need them here |
325 | ;; to make the compilation of reftex-mode not complain. | |
1c25ed90 CD |
326 | (defvar reftex-auto-view-crossref-timer nil |
327 | "The timer used for auto-view-crossref.") | |
3b919c9f CD |
328 | (defvar reftex-toc-auto-recenter-timer nil |
329 | "The idle timer used to recenter the toc window.") | |
a7ec1775 RS |
330 | |
331 | ;;;###autoload | |
332 | (defun turn-on-reftex () | |
b849548d | 333 | "Turn on RefTeX mode." |
a7ec1775 RS |
334 | (reftex-mode t)) |
335 | ||
336 | ;;;###autoload | |
337 | (defun reftex-mode (&optional arg) | |
fba437e6 | 338 | "Minor mode with distinct support for \\label, \\ref and \\cite in LaTeX. |
a7ec1775 | 339 | |
1c25ed90 CD |
340 | \\<reftex-mode-map>A Table of Contents of the entire (multifile) document with browsing |
341 | capabilities is available with `\\[reftex-toc]'. | |
342 | ||
a7ec1775 RS |
343 | Labels can be created with `\\[reftex-label]' and referenced with `\\[reftex-reference]'. |
344 | When referencing, you get a menu with all labels of a given type and | |
c52bdfca | 345 | context of the label definition. The selected label is inserted as a |
fba437e6 | 346 | \\ref macro. |
a7ec1775 | 347 | |
396e0b08 | 348 | Citations can be made with `\\[reftex-citation]' which will use a regular expression |
a7ec1775 | 349 | to pull out a *formatted* list of articles from your BibTeX |
c52bdfca | 350 | database. The selected citation is inserted as a \\cite macro. |
a7ec1775 | 351 | |
1c25ed90 CD |
352 | Index entries can be made with `\\[reftex-index-selection-or-word]' which indexes the word at point |
353 | or the current selection. More general index entries are created with | |
354 | `\\[reftex-index]'. `\\[reftex-display-index]' displays the compiled index. | |
a7ec1775 | 355 | |
c52bdfca | 356 | Most command have help available on the fly. This help is accessed by |
a7ec1775 RS |
357 | pressing `?' to any prompt mentioning this feature. |
358 | ||
f9ad2e24 CD |
359 | Extensive documentation about RefTeX is available in Info format. |
360 | You can view this information with `\\[reftex-info]'. | |
c52bdfca | 361 | |
a7ec1775 | 362 | \\{reftex-mode-map} |
396e0b08 KH |
363 | Under X, these and other functions will also be available as `Ref' menu |
364 | on the menu bar. | |
a7ec1775 RS |
365 | |
366 | ------------------------------------------------------------------------------" | |
367 | ||
368 | (interactive "P") | |
369 | (setq reftex-mode (not (or (and (null arg) reftex-mode) | |
370 | (<= (prefix-numeric-value arg) 0)))) | |
371 | ||
a7ec1775 RS |
372 | (if reftex-mode |
373 | (progn | |
f9ad2e24 | 374 | ;; Mode was turned on |
396e0b08 | 375 | (easy-menu-add reftex-mode-menu) |
b849548d CD |
376 | (and reftex-plug-into-AUCTeX |
377 | (reftex-plug-into-AUCTeX)) | |
f9ad2e24 CD |
378 | (unless (get 'reftex-auto-view-crossref 'initialized) |
379 | (and reftex-auto-view-crossref | |
380 | (reftex-toggle-auto-view-crossref)) | |
381 | (put 'reftex-auto-view-crossref 'initialized t)) | |
3b919c9f CD |
382 | (unless (get 'reftex-auto-recenter-toc 'initialized) |
383 | (and reftex-auto-recenter-toc | |
384 | (reftex-toggle-auto-toc-recenter)) | |
385 | (put 'reftex-auto-recenter-toc 'initialized t)) | |
d29c87c1 CD |
386 | |
387 | ;; Prepare the special syntax tables. | |
388 | (setq reftex-syntax-table (copy-syntax-table (syntax-table))) | |
389 | (modify-syntax-entry ?\( "." reftex-syntax-table) | |
390 | (modify-syntax-entry ?\) "." reftex-syntax-table) | |
12a382d4 | 391 | |
d29c87c1 CD |
392 | (setq reftex-syntax-table-for-bib |
393 | (copy-syntax-table reftex-syntax-table)) | |
394 | (modify-syntax-entry ?\' "." reftex-syntax-table-for-bib) | |
395 | (modify-syntax-entry ?\" "." reftex-syntax-table-for-bib) | |
396 | (modify-syntax-entry ?\[ "." reftex-syntax-table-for-bib) | |
397 | (modify-syntax-entry ?\] "." reftex-syntax-table-for-bib) | |
398 | ||
396e0b08 | 399 | (run-hooks 'reftex-mode-hook)) |
f9ad2e24 | 400 | ;; Mode was turned off |
a7ec1775 | 401 | (easy-menu-remove reftex-mode-menu))) |
396e0b08 | 402 | |
b849548d CD |
403 | (if (fboundp 'add-minor-mode) |
404 | ;; Use it so that we get the extras | |
405 | (progn | |
921759ee CD |
406 | (put 'reftex-mode :included '(memq major-mode '(latex-mode tex-mode))) |
407 | (put 'reftex-mode :menu-tag "RefTeX Mode") | |
b849548d CD |
408 | (add-minor-mode 'reftex-mode " Ref" reftex-mode-map)) |
409 | ;; The standard way | |
410 | (unless (assoc 'reftex-mode minor-mode-alist) | |
396e0b08 | 411 | (push '(reftex-mode " Ref") minor-mode-alist)) |
b849548d CD |
412 | (unless (assoc 'reftex-mode minor-mode-map-alist) |
413 | (push (cons 'reftex-mode reftex-mode-map) minor-mode-map-alist))) | |
a7ec1775 | 414 | |
1c25ed90 CD |
415 | (defvar reftex-docstruct-symbol) |
416 | (defun reftex-kill-buffer-hook () | |
417 | "Save RefTeX's parse file for this buffer if the information has changed." | |
418 | ;; Save the parsing information if it was modified. | |
419 | ;; This function should be installed in `kill-buffer-hook'. | |
420 | ;; We are careful to make sure nothing goes wring in this function. | |
421 | (when (and (boundp 'reftex-mode) reftex-mode | |
422 | (boundp 'reftex-save-parse-info) reftex-save-parse-info | |
423 | (boundp 'reftex-docstruct-symbol) reftex-docstruct-symbol | |
424 | (symbol-value reftex-docstruct-symbol) | |
425 | (get reftex-docstruct-symbol 'modified)) | |
426 | ;; Write the file. | |
427 | (condition-case nil | |
428 | (reftex-access-parse-file 'write) | |
429 | (error nil)))) | |
430 | ||
431 | (defun reftex-kill-emacs-hook () | |
432 | "Call `reftex-kill-buffer-hook' on all buffers." | |
433 | ;; This function should be installed in `kill-emacs-hook'. | |
434 | (save-excursion | |
435 | (mapcar (lambda (buf) | |
436 | (set-buffer buf) | |
437 | (reftex-kill-buffer-hook)) | |
438 | (buffer-list)))) | |
439 | ||
b849548d | 440 | ;;; ========================================================================= |
c52bdfca RS |
441 | ;;; |
442 | ;;; Silence warnings about variables in other packages. | |
61eec392 DL |
443 | (eval-when-compile |
444 | (defvar TeX-master) | |
445 | (defvar LaTeX-section-hook) | |
446 | (defvar LaTeX-label-function) | |
447 | (defvar tex-main-file) | |
448 | (defvar outline-minor-mode) | |
449 | (defvar font-lock-mode) | |
450 | (defvar font-lock-fontify-region-function) | |
451 | (defvar font-lock-syntactic-keywords)) | |
c52bdfca | 452 | |
b849548d | 453 | ;;; ========================================================================= |
a7ec1775 | 454 | ;;; |
b849548d | 455 | ;;; Multibuffer Variables |
a7ec1775 | 456 | ;;; |
b849548d | 457 | ;;; Technical notes: These work as follows: We keep just one list |
a7ec1775 | 458 | ;;; of labels for each master file - this can save a lot of memory. |
c52bdfca | 459 | ;;; `reftex-master-index-list' is an alist which connects the true file name |
a7ec1775 | 460 | ;;; of each master file with the symbols holding the information on that |
c52bdfca | 461 | ;;; document. Each buffer has local variables which point to these symbols. |
a7ec1775 | 462 | |
a7ec1775 RS |
463 | ;; List of variables which handle the multifile stuff. |
464 | ;; This list is used to tie, untie, and reset these symbols. | |
465 | (defconst reftex-multifile-symbols | |
396e0b08 | 466 | '(reftex-docstruct-symbol)) |
a7ec1775 | 467 | |
c52bdfca | 468 | ;; Alist connecting master file names with the corresponding lisp symbols. |
a7ec1775 RS |
469 | (defvar reftex-master-index-list nil) |
470 | ||
c52bdfca | 471 | ;; Last index used for a master file. |
a7ec1775 RS |
472 | (defvar reftex-multifile-index 0) |
473 | ||
a7ec1775 | 474 | ;; Variable holding the symbol with the label list of the document. |
396e0b08 KH |
475 | (defvar reftex-docstruct-symbol nil) |
476 | (make-variable-buffer-local 'reftex-docstruct-symbol) | |
a7ec1775 RS |
477 | |
478 | (defun reftex-next-multifile-index () | |
479 | ;; Return the next free index for multifile symbols. | |
396e0b08 | 480 | (incf reftex-multifile-index)) |
a7ec1775 RS |
481 | |
482 | (defun reftex-tie-multifile-symbols () | |
483 | ;; Tie the buffer-local symbols to globals connected with the master file. | |
484 | ;; If the symbols for the current master file do not exist, they are created. | |
485 | ||
486 | (let* ((master (file-truename (reftex-TeX-master-file))) | |
487 | (index (assoc master reftex-master-index-list)) | |
488 | (symlist reftex-multifile-symbols) | |
b849548d | 489 | symbol symname newflag) |
c52bdfca | 490 | ;; Find the correct index. |
a7ec1775 RS |
491 | (if index |
492 | ;; symbols do exist | |
493 | (setq index (cdr index)) | |
c52bdfca | 494 | ;; Get a new index and add info to the alist. |
a7ec1775 | 495 | (setq index (reftex-next-multifile-index) |
396e0b08 KH |
496 | newflag t) |
497 | (push (cons master index) reftex-master-index-list)) | |
a7ec1775 | 498 | |
c52bdfca | 499 | ;; Get/create symbols and tie them. |
a7ec1775 RS |
500 | (while symlist |
501 | (setq symbol (car symlist) | |
502 | symlist (cdr symlist) | |
503 | symname (symbol-name symbol)) | |
504 | (set symbol (intern (concat symname "-" (int-to-string index)))) | |
921759ee | 505 | (put (symbol-value symbol) :master-index index) |
c52bdfca | 506 | ;; Initialize if new symbols. |
1c25ed90 CD |
507 | (when newflag |
508 | (set (symbol-value symbol) nil) | |
509 | (put (symbol-value symbol) 'reftex-index-macros-style '(default)))) | |
a7ec1775 | 510 | |
c52bdfca | 511 | ;; Return t if the symbols did already exist, nil when we've made them. |
a7ec1775 RS |
512 | (not newflag))) |
513 | ||
514 | (defun reftex-untie-multifile-symbols () | |
515 | ;; Remove ties from multifile symbols, so that next use makes new ones. | |
516 | (let ((symlist reftex-multifile-symbols) | |
517 | (symbol nil)) | |
518 | (while symlist | |
519 | (setq symbol (car symlist) | |
520 | symlist (cdr symlist)) | |
521 | (set symbol nil)))) | |
522 | ||
523 | (defun reftex-TeX-master-file () | |
524 | ;; Return the name of the master file associated with the current buffer. | |
1c25ed90 | 525 | ;; When AUCTeX is loaded, we will use it's more sophisticated method. |
025bb635 RS |
526 | ;; We also support the default TeX and LaTeX modes by checking for a |
527 | ;; variable tex-main-file. | |
a7ec1775 RS |
528 | (let |
529 | ((master | |
530 | (cond | |
c52bdfca | 531 | ((fboundp 'TeX-master-file) ; AUCTeX is loaded. Use its mechanism. |
12a382d4 | 532 | (condition-case nil |
f9ad2e24 CD |
533 | (TeX-master-file t) |
534 | (error (buffer-file-name)))) | |
11b4a0d2 | 535 | ((fboundp 'tex-main-file) (tex-main-file)) ; Emacs LaTeX mode |
a7ec1775 RS |
536 | ((boundp 'TeX-master) ; The variable is defined - lets use it. |
537 | (cond | |
538 | ((eq TeX-master t) | |
539 | (buffer-file-name)) | |
540 | ((eq TeX-master 'shared) | |
541 | (setq TeX-master (read-file-name "Master file: " | |
542 | nil nil t nil))) | |
543 | (TeX-master) | |
544 | (t | |
545 | (setq TeX-master (read-file-name "Master file: " | |
546 | nil nil t nil))))) | |
547 | ((boundp 'tex-main-file) | |
c52bdfca | 548 | ;; This is the variable from the default TeX modes. |
a7ec1775 RS |
549 | (cond |
550 | ((stringp tex-main-file) | |
551 | ;; ok, this must be it | |
552 | tex-main-file) | |
553 | (t | |
c52bdfca | 554 | ;; In this case, the buffer is its own master. |
a7ec1775 RS |
555 | (buffer-file-name)))) |
556 | (t | |
c52bdfca | 557 | ;; Know nothing about master file. Assume this is a master file. |
a7ec1775 RS |
558 | (buffer-file-name))))) |
559 | (cond | |
560 | ((null master) | |
4c7ddcfa | 561 | (error "Need a filename for this buffer, please save it first")) |
a7ec1775 RS |
562 | ((or (file-exists-p (concat master ".tex")) |
563 | (reftex-get-buffer-visiting (concat master ".tex"))) | |
564 | ;; Ahh, an extra .tex was missing... | |
565 | (setq master (concat master ".tex"))) | |
a6611c0d CD |
566 | ((or (file-exists-p master) |
567 | (reftex-get-buffer-visiting master)) | |
568 | ;; We either see the file, or have a buffer on it. OK. | |
569 | ) | |
a7ec1775 | 570 | (t |
b849548d | 571 | ;; Use buffer file name. |
12a382d4 | 572 | (setq master (buffer-file-name)))) |
a7ec1775 RS |
573 | (expand-file-name master))) |
574 | ||
1c25ed90 CD |
575 | (defun reftex-is-multi () |
576 | ;; Tell if this is a multifile document. When not sure, say yes. | |
577 | (let ((entry (assq 'is-multi (symbol-value reftex-docstruct-symbol)))) | |
578 | (if entry | |
579 | (nth 1 entry) | |
580 | t))) | |
581 | ||
582 | (defun reftex-set-cite-format (value) | |
583 | "Set the document-local value of `reftex-cite-format'. | |
584 | When such a value exists, it overwrites the setting given with | |
585 | `reftex-cite-format'. See the documentation of `reftex-cite-format' | |
586 | for possible values. This function should be used from AUCTeX style files." | |
587 | (unless reftex-docstruct-symbol | |
588 | (reftex-tie-multifile-symbols)) | |
589 | (when (and reftex-docstruct-symbol | |
590 | (symbolp reftex-docstruct-symbol)) | |
591 | (put reftex-docstruct-symbol 'reftex-cite-format value))) | |
592 | ||
593 | (defun reftex-get-cite-format () | |
594 | ;; Return the current citation format. Either the document-local value in | |
595 | ;; reftex-cite-format-symbol, or the global value in reftex-cite-format. | |
596 | (if (and reftex-docstruct-symbol | |
597 | (symbolp reftex-docstruct-symbol) | |
598 | (get reftex-docstruct-symbol 'reftex-cite-format)) | |
599 | (get reftex-docstruct-symbol 'reftex-cite-format) | |
600 | reftex-cite-format)) | |
601 | ||
602 | (defun reftex-add-index-macros (entry-list) | |
603 | "Add index macro descriptions to `reftex-index-macros-style'. | |
604 | The format of ENTRY-LIST is exactly like `reftex-index-macros'. See there | |
605 | for details. | |
606 | This function makes it possible to support RefTeX from AUCTeX style files. | |
607 | The entries in ENTRY-LIST will be processed after the user settings in | |
608 | `reftex-index-entries', and before the defaults. Any changes made to | |
609 | `reftex-label-alist-style' will raise a flag to the effect that | |
610 | the label information is recompiled on next use." | |
611 | (unless reftex-docstruct-symbol | |
612 | (reftex-tie-multifile-symbols)) | |
613 | (when (and reftex-docstruct-symbol | |
614 | (symbolp reftex-docstruct-symbol)) | |
615 | (let ((list (get reftex-docstruct-symbol 'reftex-index-macros-style)) | |
616 | entry changed) | |
617 | (while entry-list | |
618 | (setq entry (pop entry-list)) | |
619 | ;; When it is a symbol, remove all other symbols | |
620 | (and (symbolp entry) | |
621 | (not (memq entry list)) | |
622 | (setq list (reftex-remove-symbols-from-list list))) | |
623 | ;; Add to list unless already member | |
624 | (unless (member entry list) | |
625 | (setq reftex-tables-dirty t | |
626 | changed t) | |
627 | (push entry list))) | |
628 | (when changed | |
629 | (put reftex-docstruct-symbol 'reftex-index-macros-style list))))) | |
630 | ||
b849548d CD |
631 | ;;; ========================================================================= |
632 | ;;; | |
1c25ed90 | 633 | ;;; Functions to compile the tables, reset the mode etc. |
b849548d CD |
634 | |
635 | ;; The following constants are derived from `reftex-label-alist'. | |
636 | ||
637 | ;; Prompt used for label type queries directed to the user. | |
6fbeb429 | 638 | (defvar reftex-type-query-prompt nil) |
b849548d CD |
639 | |
640 | ;; Help string for label type queries. | |
6fbeb429 | 641 | (defvar reftex-type-query-help nil) |
b849548d CD |
642 | |
643 | ;; Alist relating label type to reference format. | |
6fbeb429 | 644 | (defvar reftex-typekey-to-format-alist nil) |
b849548d | 645 | |
1c25ed90 | 646 | ;; Alist relating label type to label prefix. |
6fbeb429 | 647 | (defvar reftex-typekey-to-prefix-alist nil) |
b849548d CD |
648 | |
649 | ;; Alist relating environments or macros to label type and context regexp. | |
6fbeb429 | 650 | (defvar reftex-env-or-mac-alist nil) |
b849548d | 651 | |
1c25ed90 | 652 | ;; List of special environment parser functions |
6fbeb429 | 653 | (defvar reftex-special-env-parsers nil) |
1c25ed90 | 654 | |
b849548d | 655 | ;; List of macros carrying a label. |
6fbeb429 | 656 | (defvar reftex-label-mac-list nil) |
b849548d CD |
657 | |
658 | ;; List of environments carrying a label. | |
6fbeb429 | 659 | (defvar reftex-label-env-list nil) |
b849548d CD |
660 | |
661 | ;; List of all typekey letters in use. | |
6fbeb429 | 662 | (defvar reftex-typekey-list nil) |
b849548d CD |
663 | |
664 | ;; Alist relating magic words to a label type. | |
6fbeb429 | 665 | (defvar reftex-words-to-typekey-alist nil) |
b849548d CD |
666 | |
667 | ;; The last list-of-labels entry used in a reference. | |
668 | (defvar reftex-last-used-reference (list nil nil nil nil)) | |
669 | ||
1c25ed90 | 670 | ;; Alist relating index macros to other info. |
6fbeb429 | 671 | (defvar reftex-key-to-index-macro-alist nil) |
1c25ed90 | 672 | ;; Prompt for index macro queries |
6fbeb429 | 673 | (defvar reftex-query-index-macro-prompt nil) |
1c25ed90 | 674 | ;; Help string for index macro queries |
6fbeb429 | 675 | (defvar reftex-query-index-macro-help nil) |
1c25ed90 | 676 | |
b849548d | 677 | ;; The message when follow-mode is suspended |
6fbeb429 | 678 | (defvar reftex-no-follow-message |
b849548d | 679 | "No follow-mode into unvisited file. Press SPC to visit it.") |
6fbeb429 | 680 | (defvar reftex-no-info-message |
b849548d CD |
681 | "%s: info not available, use `\\[reftex-view-crossref]' to get it.") |
682 | ||
b849548d CD |
683 | ;; Global variables used for communication between functions. |
684 | (defvar reftex-default-context-position nil) | |
685 | (defvar reftex-location-start nil) | |
686 | (defvar reftex-call-back-to-this-buffer nil) | |
687 | (defvar reftex-select-return-marker (make-marker)) | |
688 | (defvar reftex-active-toc nil) | |
689 | (defvar reftex-tex-path nil) | |
690 | (defvar reftex-bib-path nil) | |
1c25ed90 | 691 | (defvar reftex-select-marked nil) |
b849548d CD |
692 | (defvar reftex-last-follow-point nil) |
693 | (defvar reftex-latex-syntax-table nil) | |
694 | (defvar reftex-prefix nil) | |
695 | (defvar reftex-section-levels-all nil) | |
696 | (defvar reftex-buffers-with-changed-invisibility nil) | |
f9ad2e24 | 697 | (defvar reftex-callback-fwd t) |
1c25ed90 CD |
698 | (defvar reftex-last-toc-master nil |
699 | "Stores the name of the tex file that `reftex-toc' was last run on.") | |
1c25ed90 CD |
700 | ;; Marker for return point from recursive edit |
701 | (defvar reftex-recursive-edit-marker (make-marker)) | |
b849548d CD |
702 | |
703 | ;; List of buffers created temporarily for lookup, which should be killed. | |
704 | (defvar reftex-buffers-to-kill nil) | |
705 | ||
706 | ;; Regexp to find anything. | |
707 | (defvar reftex-section-regexp nil) | |
708 | (defvar reftex-section-or-include-regexp nil) | |
1c25ed90 CD |
709 | (defvar reftex-index-macro-regexp nil) |
710 | (defvar reftex-index-level-re nil) | |
711 | (defvar reftex-index-key-end-re nil) | |
712 | (defvar reftex-find-index-entry-regexp-format nil) | |
b849548d | 713 | (defvar reftex-everything-regexp nil) |
1c25ed90 CD |
714 | (defvar reftex-everything-regexp-no-index nil) |
715 | (defvar reftex-index-re nil) | |
921759ee | 716 | (defvar reftex-find-citation-regexp-format |
d8fb2015 | 717 | "\\\\\\([a-zA-Z]*cite[*a-zA-Z]*\\*?\\|bibentry\\)\\(\\[[^]]*\\]\\|{[^}]*}\\)*{\\([^}]*,\\)?\\(%s\\)[},]") |
921759ee | 718 | (defvar reftex-find-reference-format |
1c25ed90 | 719 | "\\\\\\(ref[a-zA-Z]*\\|[a-zA-Z]*ref\\(range\\)?\\)\\*?\\(\\[[^]]*\\]\\|{[^}]*}\\)*{\\(%s\\)}") |
921759ee | 720 | (defvar reftex-macros-with-labels nil) |
1c25ed90 CD |
721 | (defvar reftex-macros-with-index nil) |
722 | (defvar reftex-index-macro-alist nil) | |
b849548d CD |
723 | (defvar reftex-find-label-regexp-format nil) |
724 | (defvar reftex-find-label-regexp-format2 nil) | |
725 | ||
b849548d CD |
726 | (defvar reftex-memory nil |
727 | "Memorizes old variable values to indicate changes in these variables.") | |
728 | ||
1c25ed90 CD |
729 | ;; A list of all variables in the cache. |
730 | ;; The cache is used to save the compiled versions of some variables. | |
12a382d4 | 731 | (defconst reftex-cache-variables |
1c25ed90 | 732 | '(reftex-memory ;; This MUST ALWAYS be the first! |
12a382d4 | 733 | |
1c25ed90 CD |
734 | ;; Outline |
735 | reftex-section-levels-all | |
b849548d | 736 | |
1c25ed90 | 737 | ;; Labels |
12a382d4 | 738 | reftex-env-or-mac-alist |
1c25ed90 CD |
739 | reftex-special-env-parsers |
740 | reftex-macros-with-labels | |
741 | reftex-label-mac-list | |
742 | reftex-label-env-list | |
743 | reftex-typekey-list | |
744 | reftex-typekey-to-format-alist | |
745 | reftex-typekey-to-prefix-alist | |
746 | reftex-words-to-typekey-alist | |
747 | reftex-type-query-prompt | |
748 | reftex-type-query-help | |
749 | ||
750 | ;; Index | |
751 | reftex-index-macro-alist | |
752 | reftex-macros-with-index | |
753 | reftex-query-index-macro-prompt | |
12a382d4 | 754 | reftex-query-index-macro-help |
1c25ed90 CD |
755 | reftex-key-to-index-macro-alist |
756 | ||
757 | ;; Regular expressions | |
758 | reftex-section-regexp | |
759 | reftex-section-or-include-regexp | |
760 | reftex-index-re | |
761 | reftex-everything-regexp | |
762 | reftex-everything-regexp-no-index | |
763 | reftex-find-label-regexp-format | |
764 | reftex-find-label-regexp-format2 | |
765 | reftex-find-index-entry-regexp-format | |
766 | )) | |
b849548d | 767 | |
1c25ed90 CD |
768 | (defun reftex-ensure-compiled-variables () |
769 | ;; Recompile the label alist when necessary | |
770 | (let* ((mem reftex-memory) | |
771 | (cache (get reftex-docstruct-symbol 'reftex-cache)) | |
772 | (cmem (car cache)) | |
773 | (alist reftex-label-alist) | |
774 | (levels (get reftex-docstruct-symbol 'reftex-section-levels)) | |
775 | (style (get reftex-docstruct-symbol 'reftex-label-alist-style)) | |
776 | (default reftex-default-label-alist-entries) | |
777 | (index reftex-index-macros) | |
778 | (istyle (get reftex-docstruct-symbol 'reftex-index-macros-style))) | |
779 | (cond | |
780 | (reftex-tables-dirty (reftex-compile-variables)) | |
781 | ((and (eq alist (nth 0 mem)) | |
782 | (eq levels (nth 1 mem)) | |
783 | (eq style (nth 2 mem)) | |
784 | (eq default (nth 3 mem)) | |
785 | (eq index (nth 4 mem)) | |
786 | (eq istyle (nth 5 mem)))) ;; everything is OK | |
787 | ((and (eq alist (nth 0 cmem)) | |
788 | (eq levels (nth 1 cmem)) | |
789 | (eq style (nth 2 cmem)) | |
790 | (eq default (nth 2 cmem)) | |
791 | (eq index (nth 4 cmem)) | |
792 | (eq istyle (nth 5 cmem))) | |
793 | ;; restore the cache | |
794 | (message "Restoring cache") | |
795 | (mapcar (lambda (sym) (set sym (pop cache))) reftex-cache-variables)) | |
796 | (t (reftex-compile-variables))))) | |
a7ec1775 | 797 | |
1c25ed90 | 798 | (defun reftex-reset-mode () |
12a382d4 | 799 | "Reset RefTeX Mode. |
1c25ed90 CD |
800 | This will re-compile the configuration information and remove all |
801 | current scanning information and the parse file to enforce a rescan | |
802 | on next use." | |
396e0b08 | 803 | (interactive) |
396e0b08 | 804 | |
1c25ed90 CD |
805 | ;; Reset the file search path variables |
806 | (loop for prop in '(status master-dir recursive-path rec-type) do | |
807 | (put 'reftex-tex-path prop nil) | |
808 | (put 'reftex-bib-path prop nil)) | |
396e0b08 | 809 | |
1c25ed90 CD |
810 | ;; Kill temporary buffers associated with RefTeX - just in case they |
811 | ;; were not cleaned up properly | |
812 | (save-excursion | |
813 | (let ((buffer-list '("*RefTeX Help*" "*RefTeX Select*" | |
814 | "*Duplicate Labels*" "*toc*" " *RefTeX-scratch*")) | |
815 | buf) | |
816 | (while (setq buf (pop buffer-list)) | |
817 | (if (get-buffer buf) | |
818 | (kill-buffer buf)))) | |
819 | (reftex-erase-all-selection-and-index-buffers)) | |
396e0b08 | 820 | |
1c25ed90 CD |
821 | ;; Make sure the current document will be rescanned soon. |
822 | (reftex-reset-scanning-information) | |
396e0b08 | 823 | |
1c25ed90 CD |
824 | ;; Remove any parse info file |
825 | (reftex-access-parse-file 'kill) | |
396e0b08 | 826 | |
1c25ed90 CD |
827 | ;; Plug functions into AUCTeX if the user option says so. |
828 | (and reftex-plug-into-AUCTeX | |
829 | (reftex-plug-into-AUCTeX)) | |
396e0b08 | 830 | |
1c25ed90 | 831 | (reftex-compile-variables)) |
396e0b08 | 832 | |
1c25ed90 CD |
833 | (defun reftex-reset-scanning-information () |
834 | "Reset the symbols containing information from buffer scanning. | |
835 | This enforces rescanning the buffer on next use." | |
836 | (if (string= reftex-last-toc-master (reftex-TeX-master-file)) | |
837 | (reftex-erase-buffer "*toc*")) | |
838 | (let ((symlist reftex-multifile-symbols) | |
839 | symbol) | |
840 | (while symlist | |
841 | (setq symbol (car symlist) | |
842 | symlist (cdr symlist)) | |
843 | (if (and (symbolp (symbol-value symbol)) | |
844 | (not (null (symbol-value symbol)))) | |
845 | (set (symbol-value symbol) nil))))) | |
b849548d | 846 | |
1c25ed90 CD |
847 | (defun reftex-erase-all-selection-and-index-buffers () |
848 | ;; Remove all selection buffers associated with current document. | |
12a382d4 | 849 | (mapcar |
1c25ed90 CD |
850 | (lambda (type) |
851 | (reftex-erase-buffer (reftex-make-selection-buffer-name type))) | |
852 | reftex-typekey-list) | |
853 | ;; Kill all index buffers | |
12a382d4 | 854 | (mapcar |
1c25ed90 CD |
855 | (lambda (tag) |
856 | (reftex-kill-buffer (reftex-make-index-buffer-name tag))) | |
857 | (cdr (assoc 'index-tags (symbol-value reftex-docstruct-symbol))))) | |
396e0b08 | 858 | |
1c25ed90 CD |
859 | (defun reftex-compile-variables () |
860 | ;; Compile the information in reftex-label-alist & Co. | |
396e0b08 | 861 | |
1c25ed90 | 862 | (message "Compiling label environment definitions...") |
396e0b08 | 863 | |
1c25ed90 CD |
864 | ;; Update AUCTeX style information |
865 | (when (and (featurep 'tex-site) (fboundp 'TeX-update-style)) | |
866 | (condition-case nil (TeX-update-style) (error nil))) | |
396e0b08 | 867 | |
1c25ed90 CD |
868 | ;; Record that we have done this, and what we have used. |
869 | (setq reftex-tables-dirty nil) | |
12a382d4 | 870 | (setq reftex-memory |
1c25ed90 CD |
871 | (list reftex-label-alist |
872 | (get reftex-docstruct-symbol 'reftex-section-levels) | |
873 | (get reftex-docstruct-symbol 'reftex-label-alist-style) | |
874 | reftex-default-label-alist-entries | |
875 | reftex-index-macros | |
876 | (get reftex-docstruct-symbol 'reftex-index-macros-style))) | |
396e0b08 | 877 | |
1c25ed90 CD |
878 | ;; Compile information in reftex-label-alist |
879 | (let ((all (reftex-uniquify-by-car | |
880 | (reftex-splice-symbols-into-list | |
881 | (append reftex-label-alist | |
882 | (get reftex-docstruct-symbol | |
883 | 'reftex-label-alist-style) | |
884 | reftex-default-label-alist-entries) | |
885 | reftex-label-alist-builtin) | |
886 | '(nil))) | |
887 | (all-index (reftex-uniquify-by-car | |
888 | (reftex-splice-symbols-into-list | |
12a382d4 | 889 | (append reftex-index-macros |
1c25ed90 CD |
890 | (get reftex-docstruct-symbol |
891 | 'reftex-index-macros-style) | |
892 | '(default)) | |
893 | reftex-index-macros-builtin))) | |
894 | entry env-or-mac typekeychar typekey prefix context word | |
895 | fmt reffmt labelfmt wordlist qh-list macros-with-labels | |
896 | nargs nlabel opt-args cell sum i | |
7c4d13cc | 897 | macro verify repeat nindex tag key toc-level toc-levels) |
b849548d | 898 | |
1c25ed90 CD |
899 | (setq reftex-words-to-typekey-alist nil |
900 | reftex-typekey-list nil | |
901 | reftex-typekey-to-format-alist nil | |
902 | reftex-typekey-to-prefix-alist nil | |
903 | reftex-env-or-mac-alist nil | |
904 | reftex-label-env-list nil | |
905 | reftex-label-mac-list nil) | |
906 | (while all | |
907 | (catch 'next-entry | |
908 | (setq entry (car all) | |
909 | env-or-mac (car entry) | |
910 | entry (cdr entry) | |
911 | all (cdr all)) | |
912 | (if (null env-or-mac) | |
913 | (setq env-or-mac "")) | |
914 | (if (stringp (car entry)) | |
915 | ;; This is before version 2.00 - convert entry to new format | |
916 | ;; This is just to keep old users happy | |
917 | (setq entry (cons (string-to-char (car entry)) | |
918 | (cons (concat (car entry) ":") | |
919 | (cdr entry))))) | |
920 | (setq typekeychar (nth 0 entry) | |
921 | typekey (if typekeychar (char-to-string typekeychar) nil) | |
922 | prefix (nth 1 entry) | |
923 | fmt (nth 2 entry) | |
924 | context (nth 3 entry) | |
7c4d13cc CD |
925 | wordlist (nth 4 entry) |
926 | toc-level (nth 5 entry)) | |
1c25ed90 CD |
927 | (if (stringp wordlist) |
928 | ;; This is before version 2.04 - convert to new format | |
929 | (setq wordlist (nthcdr 4 entry))) | |
b849548d | 930 | |
1c25ed90 CD |
931 | (if (and (stringp fmt) |
932 | (string-match "@" fmt)) | |
933 | ;; Special syntax for specifying a label format | |
934 | (setq fmt (split-string fmt "@+")) | |
935 | (setq fmt (list "\\label{%s}" fmt))) | |
936 | (setq labelfmt (car fmt) | |
937 | reffmt (nth 1 fmt)) | |
938 | ;; Note a new typekey | |
939 | (if typekey | |
940 | (add-to-list 'reftex-typekey-list typekey)) | |
941 | (if (and typekey prefix | |
942 | (not (assoc typekey reftex-typekey-to-prefix-alist))) | |
943 | (add-to-list 'reftex-typekey-to-prefix-alist | |
944 | (cons typekey prefix))) | |
945 | ;; Check if this is a macro or environment | |
946 | (cond | |
947 | ((symbolp env-or-mac) | |
948 | ;; A special parser function | |
949 | (unless (fboundp env-or-mac) | |
12a382d4 | 950 | (message "Warning: %s does not seem to be a valid function" |
1c25ed90 CD |
951 | env-or-mac)) |
952 | (setq nargs nil nlabel nil opt-args nil) | |
953 | (add-to-list 'reftex-special-env-parsers env-or-mac) | |
954 | (setq env-or-mac (symbol-name env-or-mac))) | |
955 | ((string-match "\\`\\\\" env-or-mac) | |
956 | ;; It's a macro | |
957 | (let ((result (reftex-parse-args env-or-mac))) | |
958 | (setq env-or-mac (or (first result) env-or-mac) | |
959 | nargs (second result) | |
960 | nlabel (third result) | |
961 | opt-args (fourth result)) | |
962 | (if nlabel (add-to-list 'macros-with-labels env-or-mac))) | |
963 | (if typekey (add-to-list 'reftex-label-mac-list env-or-mac))) | |
964 | (t | |
965 | ;; It's an environment | |
966 | (setq nargs nil nlabel nil opt-args nil) | |
967 | (cond ((string= env-or-mac "any")) | |
968 | ((string= env-or-mac "")) | |
969 | ((string= env-or-mac "section")) | |
970 | (t | |
7c4d13cc CD |
971 | (add-to-list 'reftex-label-env-list env-or-mac) |
972 | (if toc-level | |
973 | (let ((string (format "begin{%s}" env-or-mac))) | |
974 | (or (assoc string toc-levels) | |
975 | (push (cons string toc-level) toc-levels)))))))) | |
1c25ed90 CD |
976 | ;; Translate some special context cases |
977 | (when (assq context reftex-default-context-regexps) | |
12a382d4 JB |
978 | (setq context |
979 | (format | |
1c25ed90 CD |
980 | (cdr (assq context reftex-default-context-regexps)) |
981 | (regexp-quote env-or-mac)))) | |
982 | ;; See if this is the first format for this typekey | |
983 | (and reffmt | |
984 | (not (assoc typekey reftex-typekey-to-format-alist)) | |
985 | (push (cons typekey reffmt) reftex-typekey-to-format-alist)) | |
986 | ;; See if this is the first definition for this env-or-mac | |
987 | (and (not (string= env-or-mac "any")) | |
988 | (not (string= env-or-mac "")) | |
989 | (not (assoc env-or-mac reftex-env-or-mac-alist)) | |
990 | (push (list env-or-mac typekey context labelfmt | |
991 | nargs nlabel opt-args) | |
992 | reftex-env-or-mac-alist)) | |
993 | ;; Are the magic words regular expressions? Quote normal words. | |
994 | (if (eq (car wordlist) 'regexp) | |
995 | (setq wordlist (cdr wordlist)) | |
996 | (setq wordlist (mapcar 'regexp-quote wordlist))) | |
997 | ;; Remember the first association of each word. | |
998 | (while (stringp (setq word (pop wordlist))) | |
999 | (or (assoc word reftex-words-to-typekey-alist) | |
1000 | (push (cons word typekey) reftex-words-to-typekey-alist))) | |
1001 | (cond | |
1002 | ((string= "" env-or-mac) nil) | |
1003 | ((setq cell (assoc typekey qh-list)) | |
1004 | (push env-or-mac (cdr cell))) | |
1005 | (typekey | |
1006 | (push (list typekey env-or-mac) qh-list))))) | |
2faef409 | 1007 | |
1c25ed90 CD |
1008 | (setq reftex-typekey-to-prefix-alist |
1009 | (nreverse reftex-typekey-to-prefix-alist)) | |
a7ec1775 | 1010 | |
1c25ed90 | 1011 | ;; Prepare the typekey query prompt and help string. |
12a382d4 | 1012 | (setq qh-list |
1c25ed90 CD |
1013 | (sort qh-list |
1014 | (lambda (x1 x2) | |
1015 | (string< (downcase (car x1)) (downcase (car x2)))))) | |
1016 | (setq reftex-type-query-prompt | |
1017 | (concat "Label type: [" | |
1018 | (mapconcat (lambda(x) (format "%s" (car x))) | |
1019 | qh-list "") | |
1020 | "]")) | |
1021 | ;; In the help string, we need to wrap lines... | |
1022 | (setq reftex-type-query-help | |
12a382d4 | 1023 | (concat |
1c25ed90 CD |
1024 | "SELECT A LABEL TYPE:\n--------------------\n" |
1025 | (mapconcat | |
1026 | (lambda(x) | |
1027 | (setq sum 0) | |
1028 | (format " [%s] %s" | |
1029 | (car x) | |
1030 | (mapconcat (lambda(env) | |
1031 | (setq sum (+ sum (length env))) | |
1032 | (if (< sum 60) | |
1033 | env | |
1034 | (setq sum 0) | |
1035 | (concat "\n " env))) | |
1036 | (cdr x) " "))) | |
1037 | qh-list "\n"))) | |
a7ec1775 | 1038 | |
1c25ed90 CD |
1039 | ;; Convert magic words to regular expressions. We make regular expressions |
1040 | ;; which allow for some chars from the ref format to be in the buffer. | |
1041 | ;; These characters will be seen and removed. | |
1042 | (setq reftex-words-to-typekey-alist | |
12a382d4 | 1043 | (mapcar |
1c25ed90 CD |
1044 | (lambda (x) |
1045 | (setq word (car x) | |
1046 | typekey (cdr x) | |
1047 | fmt (cdr (assoc typekey reftex-typekey-to-format-alist))) | |
1048 | (setq word (concat "\\W\\(" word "[ \t\n\r]*\\)\\(")) | |
1049 | (setq i 0) | |
1050 | (while (and (< i 10) ; maximum number of format chars allowed | |
1051 | (< i (length fmt)) | |
1052 | (not (member (aref fmt i) '(?%)))) | |
1053 | (setq word (concat word "\\|" (regexp-quote | |
1054 | (substring fmt 0 (1+ i))))) | |
1055 | (incf i)) | |
1056 | (cons (concat word "\\)\\=") typekey)) | |
1057 | (nreverse reftex-words-to-typekey-alist))) | |
a7ec1775 | 1058 | |
1c25ed90 CD |
1059 | ;; Parse the index macros |
1060 | (setq reftex-index-macro-alist nil | |
1061 | reftex-key-to-index-macro-alist nil | |
1062 | reftex-macros-with-index nil) | |
1063 | (while all-index | |
1064 | (setq entry (car all-index) | |
1065 | macro (car entry) | |
1066 | tag (nth 1 entry) | |
1067 | key (nth 2 entry) | |
1068 | prefix (or (nth 3 entry) "") | |
1069 | verify (nth 4 entry) | |
7c4d13cc CD |
1070 | ;; For repeat, we need to be compatible with older code |
1071 | ;; This information used to be given only for the default macro, | |
1072 | ;; but later we required to have it for *every* index macro | |
1073 | repeat (cond ((> (length entry) 5) (nth 5 entry)) | |
1074 | ((and (eq key (car reftex-index-default-macro)) | |
1075 | (> (length reftex-index-default-macro) 2)) | |
1076 | ;; User has old setting - respect it | |
1077 | (nth 2 reftex-index-default-macro)) | |
1078 | (t t)) | |
1079 | all-index (cdr all-index)) | |
1c25ed90 CD |
1080 | (let ((result (reftex-parse-args macro))) |
1081 | (setq macro (or (first result) macro) | |
1082 | nargs (second result) | |
1083 | nindex (third result) | |
1084 | opt-args (fourth result)) | |
1085 | (unless (member macro reftex-macros-with-index) | |
7c4d13cc CD |
1086 | ;; 0 1 2 3 4 5 6 7 |
1087 | (push (list macro tag prefix verify nargs nindex opt-args repeat) | |
1c25ed90 CD |
1088 | reftex-index-macro-alist) |
1089 | (or (assoc key reftex-key-to-index-macro-alist) | |
1090 | (push (list key macro) reftex-key-to-index-macro-alist)) | |
1091 | (push macro reftex-macros-with-index)))) | |
1092 | ;; Make the prompt and help string for index macros query | |
1093 | (setq reftex-key-to-index-macro-alist | |
1094 | (sort reftex-key-to-index-macro-alist | |
1095 | (lambda (a b) (< (downcase (car a)) (downcase (car b)))))) | |
12a382d4 | 1096 | (setq reftex-query-index-macro-prompt |
1c25ed90 CD |
1097 | (concat "Index macro: [" |
1098 | (mapconcat (lambda (x) (char-to-string (car x))) | |
1099 | reftex-key-to-index-macro-alist "") | |
1100 | "]")) | |
1101 | (setq i 0 | |
1102 | reftex-query-index-macro-help | |
12a382d4 | 1103 | (concat |
1c25ed90 CD |
1104 | "SELECT A MACRO:\n---------------\n" |
1105 | (mapconcat | |
1106 | (lambda(x) | |
12a382d4 | 1107 | (format "[%c] %-20.20s%s" (car x) (nth 1 x) |
1c25ed90 CD |
1108 | (if (= 0 (mod (incf i) 3)) "\n" ""))) |
1109 | reftex-key-to-index-macro-alist ""))) | |
2faef409 | 1110 | |
1c25ed90 CD |
1111 | ;; Make the full list of section levels |
1112 | (setq reftex-section-levels-all | |
7c4d13cc CD |
1113 | (append toc-levels |
1114 | (get reftex-docstruct-symbol 'reftex-section-levels) | |
1c25ed90 | 1115 | reftex-section-levels)) |
a7ec1775 | 1116 | |
1c25ed90 | 1117 | ;; Calculate the regular expressions |
6fbeb429 CD |
1118 | (let* ( |
1119 | ; (wbol "\\(\\`\\|[\n\r]\\)[ \t]*") | |
7ac7387b | 1120 | (wbol "\\(^\\)[ \t]*") ; Need to keep the empty group because |
12a382d4 | 1121 | ;;; because match number are hard coded |
1c25ed90 | 1122 | (label-re "\\\\label{\\([^}]*\\)}") |
12a382d4 | 1123 | (include-re (concat wbol |
6fbeb429 | 1124 | "\\\\\\(" |
12a382d4 | 1125 | (mapconcat 'identity |
6fbeb429 CD |
1126 | reftex-include-file-commands "\\|") |
1127 | "\\)[{ \t]+\\([^} \t\n\r]+\\)")) | |
1c25ed90 CD |
1128 | (section-re |
1129 | (concat wbol "\\\\\\(" | |
46177d54 CD |
1130 | (mapconcat (lambda (x) (regexp-quote (car x))) |
1131 | reftex-section-levels-all "\\|") | |
d8fb2015 | 1132 | "\\)\\*?\\(\\[[^]]*\\]\\)?[[{ \t\r\n]")) |
1c25ed90 CD |
1133 | (appendix-re (concat wbol "\\(\\\\appendix\\)")) |
1134 | (macro-re | |
1135 | (if macros-with-labels | |
1136 | (concat "\\(" | |
1137 | (mapconcat 'regexp-quote macros-with-labels "\\|") | |
1138 | "\\)[[{]") | |
1139 | "")) | |
1140 | (index-re | |
1141 | (concat "\\(" | |
1142 | (mapconcat 'regexp-quote reftex-macros-with-index "\\|") | |
1143 | "\\)[[{]")) | |
1144 | (find-index-re-format | |
1145 | (concat "\\(" | |
1146 | (mapconcat 'regexp-quote reftex-macros-with-index "\\|") | |
1147 | "\\)\\([[{][^]}]*[]}]\\)*[[{]\\(%s\\)[]}]")) | |
1148 | (find-label-re-format | |
1149 | (concat "\\(" | |
1150 | (mapconcat 'regexp-quote (append '("\\label") | |
1151 | macros-with-labels) "\\|") | |
1152 | "\\)\\([[{][^]}]*[]}]\\)*[[{]\\(%s\\)[]}]")) | |
1153 | (index-level-re | |
1154 | (regexp-quote (nth 0 reftex-index-special-chars))) | |
1155 | (index-key-end-re ;; ^]- not allowed | |
1156 | (concat "[^" (nth 3 reftex-index-special-chars) "]" | |
1157 | "[" (nth 1 reftex-index-special-chars) | |
1158 | (nth 2 reftex-index-special-chars) "]")) | |
1159 | ) | |
1160 | (setq reftex-section-regexp section-re | |
1161 | reftex-section-or-include-regexp | |
1162 | (concat section-re "\\|" include-re) | |
1163 | reftex-everything-regexp | |
1164 | (concat label-re "\\|" section-re "\\|" include-re | |
1165 | "\\|" appendix-re | |
1166 | "\\|" index-re | |
1167 | (if macros-with-labels "\\|" "") macro-re) | |
1168 | reftex-everything-regexp-no-index | |
1169 | (concat label-re "\\|" section-re "\\|" include-re | |
1170 | "\\|" appendix-re | |
1171 | "\\|" "\\(\\\\6\\\\3\\\\1\\)" ; This is unlikely to match | |
1172 | (if macros-with-labels "\\|" "") macro-re) | |
1173 | reftex-index-re index-re | |
1174 | reftex-index-level-re index-level-re | |
1175 | reftex-index-key-end-re index-key-end-re | |
1176 | reftex-macros-with-labels macros-with-labels | |
1177 | reftex-find-index-entry-regexp-format find-index-re-format | |
1178 | reftex-find-label-regexp-format find-label-re-format | |
12a382d4 | 1179 | reftex-find-label-regexp-format2 |
1c25ed90 CD |
1180 | "\\([]} \t\n\r]\\)\\([[{]\\)\\(%s\\)[]}]") |
1181 | (message "Compiling label environment definitions...done"))) | |
1182 | (put reftex-docstruct-symbol 'reftex-cache | |
1183 | (mapcar 'symbol-value reftex-cache-variables))) | |
29d593f8 | 1184 | |
1c25ed90 CD |
1185 | (defun reftex-parse-args (macro) |
1186 | ;; Return a list of macro name, nargs, arg-nr which is label and a list of | |
1187 | ;; optional argument indices. | |
1188 | (if (string-match "[[{]\\*?[]}]" macro) | |
1189 | (progn | |
1190 | (let ((must-match (substring macro 0 (match-beginning 0))) | |
1191 | (args (substring macro (match-beginning 0))) | |
1192 | opt-list nlabel (cnt 0)) | |
1193 | (while (string-match "\\`[[{]\\(\\*\\)?[]}]" args) | |
1194 | (incf cnt) | |
1195 | (when (eq ?\[ (string-to-char args)) | |
1196 | (push cnt opt-list)) | |
1197 | (when (and (match-end 1) | |
1198 | (not nlabel)) | |
1199 | (setq nlabel cnt)) | |
1200 | (setq args (substring args (match-end 0)))) | |
1201 | (list must-match cnt nlabel opt-list))) | |
1202 | nil)) | |
a7ec1775 | 1203 | |
b849548d | 1204 | ;;; ========================================================================= |
a7ec1775 | 1205 | ;;; |
1c25ed90 | 1206 | ;;; Accessing the parse information |
a7ec1775 | 1207 | |
1c25ed90 CD |
1208 | (defun reftex-access-scan-info (&optional rescan file) |
1209 | "Ensure access to the scanning info for the current file." | |
1210 | ;; When the multifile symbols are not yet tied, | |
1211 | ;; tie them. When they are empty or RESCAN is non-nil, scan the document. | |
1212 | ;; But, when RESCAN is -1, don't rescan even if docstruct is empty. | |
1213 | ;; When FILE is non-nil, parse only from that file. | |
a6611c0d | 1214 | |
1c25ed90 CD |
1215 | ;; Make sure we have the symbols tied |
1216 | (if (eq reftex-docstruct-symbol nil) | |
1217 | ;; Symbols are not yet tied: Tie them. | |
1218 | (reftex-tie-multifile-symbols)) | |
921759ee | 1219 | |
1c25ed90 | 1220 | (reftex-ensure-compiled-variables) |
a7ec1775 | 1221 | |
1c25ed90 CD |
1222 | (when (or (null (symbol-value reftex-docstruct-symbol)) |
1223 | (member rescan '(t 1 (4) (16)))) | |
1224 | ;; The docstruct will change: Remove selection buffers. | |
1225 | (save-excursion | |
1226 | (reftex-erase-buffer "*toc*") | |
1227 | (reftex-erase-all-selection-and-index-buffers))) | |
a7ec1775 | 1228 | |
1c25ed90 CD |
1229 | (if (and (null (symbol-value reftex-docstruct-symbol)) |
1230 | (not (member rescan '(t 1 (4) (16)))) | |
1231 | reftex-save-parse-info) | |
1232 | ;; Try to read the stuff from a file | |
1233 | (reftex-access-parse-file 'read)) | |
b849548d | 1234 | |
b849548d | 1235 | (cond |
1c25ed90 CD |
1236 | ((equal rescan -1)) ;; We are not allowed to scan. |
1237 | ((not (symbol-value reftex-docstruct-symbol)) | |
1238 | ;; Scan the whole document | |
1239 | (reftex-do-parse 1 file)) | |
1240 | ((member rescan '(t 1 (4) (16))) | |
1241 | ;; Scan whatever was required by the caller. | |
1242 | (reftex-do-parse rescan file)))) | |
b849548d | 1243 | |
2c9deec1 | 1244 | ;;;###autoload |
11b4a0d2 CD |
1245 | (defun reftex-scanning-info-available-p () |
1246 | "Is the scanning info about the current document available?" | |
1247 | (unless reftex-docstruct-symbol | |
1248 | (reftex-tie-multifile-symbols)) | |
1249 | (and (symbolp reftex-docstruct-symbol) | |
1250 | (symbol-value reftex-docstruct-symbol) | |
1251 | t)) | |
12a382d4 | 1252 | |
1c25ed90 CD |
1253 | (defun reftex-silence-toc-markers (list n) |
1254 | ;; Set all toc markers in the first N entries in list to nil | |
1255 | (while (and list (> (decf n) -1)) | |
1256 | (and (eq (car (car list)) 'toc) | |
1257 | (markerp (nth 4 (car list))) | |
1258 | (set-marker (nth 4 (car list)) nil)) | |
1259 | (pop list))) | |
b849548d | 1260 | |
1c25ed90 CD |
1261 | (defun reftex-access-parse-file (action) |
1262 | "Perform ACTION on the parse file (the .rel file). | |
1263 | Valid actions are: readable, restore, read, kill, write." | |
1264 | (let* ((list (symbol-value reftex-docstruct-symbol)) | |
1265 | (docstruct-symbol reftex-docstruct-symbol) | |
1266 | (master (reftex-TeX-master-file)) | |
1267 | (enable-local-variables nil) | |
1268 | (file (if (string-match "\\.[a-zA-Z]+\\'" master) | |
12a382d4 | 1269 | (concat (substring master 0 (match-beginning 0)) |
9f286482 CD |
1270 | reftex-parse-file-extension) |
1271 | (concat master reftex-parse-file-extension)))) | |
1c25ed90 CD |
1272 | (cond |
1273 | ((eq action 'readable) | |
1274 | (file-readable-p file)) | |
1275 | ((eq action 'restore) | |
1276 | (put reftex-docstruct-symbol 'modified nil) | |
1277 | (if (eq reftex-docstruct-symbol nil) | |
1278 | ;; Symbols are not yet tied: Tie them. | |
1279 | (reftex-tie-multifile-symbols)) | |
1280 | (if (file-exists-p file) | |
1281 | ;; load the file and return t for success | |
1282 | (condition-case nil | |
1283 | (progn (load-file file) t) | |
1284 | (error (set reftex-docstruct-symbol nil) | |
1285 | (error "Error while loading file %s" file))) | |
1286 | ;; Throw an exception if the file does not exist | |
1287 | (error "No restore file %s" file))) | |
1288 | ((eq action 'read) | |
1289 | (put reftex-docstruct-symbol 'modified nil) | |
1290 | (if (file-exists-p file) | |
1291 | ;; load the file and return t for success | |
1292 | (condition-case nil | |
1293 | (progn | |
1294 | (load-file file) | |
1295 | (reftex-check-parse-consistency) | |
f9ad2e24 | 1296 | t) |
1c25ed90 CD |
1297 | (error (message "Error while restoring file %s" file) |
1298 | (set reftex-docstruct-symbol nil) | |
1299 | nil)) | |
1300 | ;; return nil for failure, but no exception | |
1301 | nil)) | |
1302 | ((eq action 'kill) | |
1303 | ;; Remove the file | |
1304 | (when (and (file-exists-p file) (file-writable-p file)) | |
1305 | (message "Unlinking file %s" file) | |
1306 | (delete-file file))) | |
1307 | (t | |
1308 | (put docstruct-symbol 'modified nil) | |
1309 | (save-excursion | |
1310 | (if (file-writable-p file) | |
1311 | (progn | |
1312 | (message "Writing parse file %s" (abbreviate-file-name file)) | |
1313 | (find-file file) | |
1314 | (erase-buffer) | |
1315 | (insert (format ";; RefTeX parse info file\n")) | |
1316 | (insert (format ";; File: %s\n" master)) | |
1317 | (insert (format ";; User: %s (%s)\n\n" | |
1318 | (user-login-name) (user-full-name))) | |
1319 | (insert "(set reftex-docstruct-symbol '(\n\n") | |
1320 | (let ((standard-output (current-buffer))) | |
1321 | (mapcar | |
1322 | (lambda (x) | |
1323 | (cond ((eq (car x) 'toc) | |
1324 | ;; A toc entry. Do not save the marker. | |
1325 | ;; Save the markers position at position 8 | |
1326 | (print (list 'toc "toc" (nth 2 x) (nth 3 x) | |
1327 | nil (nth 5 x) (nth 6 x) (nth 7 x) | |
1328 | (or (and (markerp (nth 4 x)) | |
1329 | (marker-position (nth 4 x))) | |
1330 | (nth 8 x))))) | |
1331 | ((and (not (eq t reftex-support-index)) | |
1332 | (eq (car x) 'index)) | |
1333 | ;; Don't save index entries | |
1334 | ) | |
1335 | (t (print x)))) | |
1336 | list)) | |
1337 | (insert "))\n\n") | |
1338 | (save-buffer 0) | |
1339 | (kill-buffer (current-buffer))) | |
1340 | (error "Cannot write to file %s" file))) | |
1341 | t)))) | |
921759ee | 1342 | |
1c25ed90 CD |
1343 | (defun reftex-check-parse-consistency () |
1344 | ;; Check if parse file is consistent, throw an error if not. | |
1345 | ||
1346 | ;; Check if the master is the same: when moving a document, this will see it. | |
1347 | (let* ((real-master (reftex-TeX-master-file)) | |
12a382d4 | 1348 | (parsed-master |
1c25ed90 CD |
1349 | (nth 1 (assq 'bof (symbol-value reftex-docstruct-symbol))))) |
1350 | (unless (string= (file-truename real-master) (file-truename parsed-master)) | |
1351 | (message "Master file name in load file is different: %s versus %s" | |
1352 | parsed-master real-master) | |
1353 | (error "Master file name error"))) | |
1354 | ||
1355 | ;; Check for the existence of all document files | |
1356 | ;;; (let* ((all (symbol-value reftex-docstruct-symbol))) | |
1357 | ;;; (while all | |
1358 | ;;; (when (and (eq (car (car all)) 'bof) | |
1359 | ;;; (not (file-regular-p (nth 1 (car all))))) | |
1360 | ;;; (message "File %s in saved parse info not avalable" (cdr (car all))) | |
1361 | ;;; (error "File not found")) | |
1362 | ;;; (setq all (cdr all)))) | |
1363 | ) | |
1364 | ||
1365 | (defun reftex-select-external-document (xr-alist xr-index) | |
1366 | ;; Return index of an external document. | |
1367 | (let* ((len (length xr-alist)) (highest (1- (+ ?0 len))) | |
12a382d4 | 1368 | (prompt (format "[%c-%c] Select TAB: Read prefix with completion" |
1c25ed90 CD |
1369 | ?0 highest)) |
1370 | key prefix) | |
1371 | (cond | |
1372 | ((= len 1) | |
1373 | (message "No external documents available") | |
1374 | (ding) (sit-for 1) 0) | |
1375 | ((= len 2) | |
1376 | (- 1 xr-index)) | |
1377 | (t | |
1378 | (save-excursion | |
12a382d4 | 1379 | (let* ((length (apply 'max (mapcar |
1c25ed90 CD |
1380 | (lambda(x) (length (car x))) xr-alist))) |
1381 | (fmt (format " [%%c] %%-%ds %%s\n" length)) | |
1382 | (n (1- ?0))) | |
1383 | (setq key | |
1384 | (reftex-select-with-char | |
1385 | prompt | |
1386 | (concat | |
1387 | "SELECT EXTERNAL DOCUMENT\n------------------------\n" | |
1388 | (mapconcat | |
12a382d4 | 1389 | (lambda (x) |
1c25ed90 CD |
1390 | (format fmt (incf n) (or (car x) "") |
1391 | (abbreviate-file-name (cdr x)))) | |
1392 | xr-alist "")) | |
1393 | nil t)) | |
1394 | (cond | |
1395 | ((and (>= key ?0) (<= key highest)) (- key ?0)) | |
1396 | ((= key ?\C-i) | |
1397 | (setq prefix (completing-read "Prefix: " xr-alist nil t)) | |
1398 | (- len (length (memq (assoc prefix xr-alist) xr-alist)))) | |
1399 | (t (error "Illegal document selection [%c]" key))))))))) | |
b849548d CD |
1400 | |
1401 | ;;; ========================================================================= | |
1402 | ;;; | |
1403 | ;;; Finding files | |
1404 | ||
f9ad2e24 CD |
1405 | (defun reftex-locate-file (file type master-dir &optional die) |
1406 | "Find FILE of type TYPE in MASTER-DIR or on the path associcted with TYPE. | |
1407 | If the file does not have any of the legal extensions for TYPE, | |
1408 | try first the default extension and only then the naked file name. | |
1409 | When DIE is non-nil, throw an error if file not found." | |
1410 | (let* ((rec-values (if reftex-search-unrecursed-path-first '(nil t) '(t))) | |
1411 | (extensions (cdr (assoc type reftex-file-extensions))) | |
1412 | (def-ext (car extensions)) | |
12a382d4 | 1413 | (ext-re (concat "\\(" |
f9ad2e24 CD |
1414 | (mapconcat 'regexp-quote extensions "\\|") |
1415 | "\\)\\'")) | |
1416 | (files (if (string-match ext-re file) | |
1417 | (cons file nil) | |
1418 | (cons (concat file def-ext) file))) | |
1419 | path old-path file1) | |
51d628c8 CD |
1420 | (cond |
1421 | ((file-name-absolute-p file) | |
12a382d4 JB |
1422 | (setq file1 |
1423 | (or | |
f9ad2e24 CD |
1424 | (and (car files) (file-regular-p (car files)) (car files)) |
1425 | (and (cdr files) (file-regular-p (cdr files)) (cdr files))))) | |
51d628c8 | 1426 | ((and reftex-use-external-file-finders |
f9ad2e24 CD |
1427 | (assoc type reftex-external-file-finders)) |
1428 | (setq file1 (reftex-find-file-externally file type master-dir))) | |
51d628c8 | 1429 | (t |
b849548d | 1430 | (while (and (null file1) rec-values) |
a6611c0d | 1431 | (setq path (reftex-access-search-path |
f9ad2e24 | 1432 | type (pop rec-values) master-dir file)) |
b849548d CD |
1433 | (if (or (null old-path) |
1434 | (not (eq old-path path))) | |
1435 | (setq old-path path | |
1436 | path (cons master-dir path) | |
f9ad2e24 | 1437 | file1 (or (and (car files) |
12a382d4 | 1438 | (reftex-find-file-on-path |
f9ad2e24 CD |
1439 | (car files) path master-dir)) |
1440 | (and (cdr files) | |
12a382d4 | 1441 | (reftex-find-file-on-path |
f9ad2e24 | 1442 | (cdr files) path master-dir)))))))) |
b849548d CD |
1443 | (cond (file1 file1) |
1444 | (die (error "No such file: %s" file) nil) | |
1445 | (t (message "No such file: %s (ignored)" file) nil)))) | |
1446 | ||
51d628c8 CD |
1447 | (defun reftex-find-file-externally (file type &optional master-dir) |
1448 | ;; Use external program to find FILE. | |
f9ad2e24 | 1449 | ;; The program is taken from `reftex-external-file-finders'. |
51d628c8 CD |
1450 | ;; Interprete relative path definitions starting from MASTER-DIR. |
1451 | (let ((default-directory (or master-dir default-directory)) | |
1452 | (prg (cdr (assoc type reftex-external-file-finders))) | |
1453 | out) | |
1454 | (if (string-match "%f" prg) | |
1455 | (setq prg (replace-match file t t prg))) | |
1456 | (setq out (apply 'reftex-process-string (split-string prg))) | |
1c25ed90 | 1457 | (if (string-match "[ \t\n]+\\'" out) ; chomp |
51d628c8 CD |
1458 | (setq out (replace-match "" nil nil out))) |
1459 | (cond ((equal out "") nil) | |
1c25ed90 | 1460 | ((file-regular-p out) (expand-file-name out master-dir)) |
51d628c8 CD |
1461 | (t nil)))) |
1462 | ||
1463 | (defun reftex-process-string (program &rest args) | |
1464 | "Execute PROGRAM with arguments ARGS and return its STDOUT as a string." | |
1c25ed90 CD |
1465 | (let ((calling-dir default-directory)) ; remember default directory |
1466 | (with-output-to-string | |
1467 | (with-current-buffer standard-output | |
1468 | (let ((default-directory calling-dir)) ; set default directory | |
1469 | (apply 'call-process program nil '(t nil) nil args)))))) | |
51d628c8 | 1470 | |
f9ad2e24 CD |
1471 | (defun reftex-access-search-path (type &optional recurse master-dir file) |
1472 | ;; Access path from environment variables. TYPE is either "tex" or "bib". | |
b849548d CD |
1473 | ;; When RECURSE is t, expand path elements ending in `//' recursively. |
1474 | ;; Relative path elements are left as they are. However, relative recursive | |
1475 | ;; elements are expanded with MASTER-DIR as default directory. | |
1476 | ;; The expanded path is cached for the next search. | |
1477 | ;; FILE is just for the progress message. | |
1478 | ;; Returns the derived path. | |
f9ad2e24 | 1479 | (let* ((pathvar (intern (concat "reftex-" type "-path")))) |
b849548d | 1480 | (when (null (get pathvar 'status)) |
f9ad2e24 CD |
1481 | ;; Get basic path |
1482 | (set pathvar | |
1483 | (reftex-uniq | |
1484 | (reftex-parse-colon-path | |
1485 | (mapconcat | |
12a382d4 | 1486 | (lambda(x) |
f9ad2e24 CD |
1487 | (if (string-match "^!" x) |
1488 | (apply 'reftex-process-string | |
1489 | (split-string (substring x 1))) | |
1490 | (or (getenv x) x))) | |
1491 | ;; For consistency, the next line should look like this: | |
1492 | ;; (cdr (assoc type reftex-path-environment)) | |
1493 | ;; However, historically we have separate options for the | |
1494 | ;; environment variables, so we have to do this: | |
12a382d4 | 1495 | (symbol-value (intern (concat "reftex-" type |
f9ad2e24 CD |
1496 | "path-environment-variables"))) |
1497 | path-separator)))) | |
b849548d CD |
1498 | (put pathvar 'status 'split) |
1499 | ;; Check if we have recursive elements | |
1500 | (let ((path (symbol-value pathvar)) dir rec) | |
1501 | (while (setq dir (pop path)) | |
1502 | (when (string= (substring dir -2) "//") | |
1503 | (if (file-name-absolute-p dir) | |
1504 | (setq rec (or rec 'absolute)) | |
1505 | (setq rec 'relative)))) | |
1506 | (put pathvar 'rec-type rec))) | |
1507 | ||
1508 | (if recurse | |
1509 | ;; Return the recursive expansion of the path | |
1510 | (cond | |
1511 | ((not (get pathvar 'rec-type)) | |
1512 | ;; Path does not contain recursive elements - use simple path | |
1513 | (symbol-value pathvar)) | |
1514 | ((or (not (get pathvar 'recursive-path)) | |
1515 | (and (eq (get pathvar 'rec-type) 'relative) | |
1516 | (not (equal master-dir (get pathvar 'master-dir))))) | |
1517 | ;; Either: We don't have a recursive expansion yet. | |
1518 | ;; or: Relative recursive path elements need to be expanded | |
1519 | ;; relative to new default directory | |
f9ad2e24 | 1520 | (message "Expanding search path to find %s file: %s ..." type file) |
12a382d4 | 1521 | (put pathvar 'recursive-path |
b849548d CD |
1522 | (reftex-expand-path (symbol-value pathvar) master-dir)) |
1523 | (put pathvar 'master-dir master-dir) | |
1524 | (get pathvar 'recursive-path)) | |
12a382d4 | 1525 | (t |
b849548d CD |
1526 | ;; Recursive path computed earlier is still OK. |
1527 | (get pathvar 'recursive-path))) | |
1528 | ;; The simple path was requested | |
1529 | (symbol-value pathvar)))) | |
1530 | ||
1531 | (defun reftex-find-file-on-path (file path &optional def-dir) | |
1532 | ;; Find FILE along the directory list PATH. | |
1533 | ;; DEF-DIR is the default directory for expanding relative path elements. | |
1534 | (catch 'exit | |
1535 | (when (file-name-absolute-p file) | |
a6611c0d | 1536 | (if (file-regular-p file) |
b849548d CD |
1537 | (throw 'exit file) |
1538 | (throw 'exit nil))) | |
f9ad2e24 | 1539 | (let* ((thepath path) file1 dir) |
b849548d CD |
1540 | (while (setq dir (pop thepath)) |
1541 | (when (string= (substring dir -2) "//") | |
1542 | (setq dir (substring dir 0 -1))) | |
1543 | (setq file1 (expand-file-name file (expand-file-name dir def-dir))) | |
a6611c0d | 1544 | (if (file-regular-p file1) |
b849548d CD |
1545 | (throw 'exit file1))) |
1546 | ;; No such file | |
1547 | nil))) | |
1548 | ||
1549 | (defun reftex-parse-colon-path (path) | |
1550 | ;; Like parse-colon-parse, but // or /~ are left alone. | |
1551 | ;; Trailing ! or !! will be converted into `//' (emTeX convention) | |
1552 | (mapcar | |
1553 | (lambda (dir) | |
12a382d4 | 1554 | (if (string-match "\\(//+\\|/*!+\\)\\'" dir) |
b849548d CD |
1555 | (setq dir (replace-match "//" t t dir))) |
1556 | (file-name-as-directory dir)) | |
1557 | (delete "" (split-string path (concat path-separator "+"))))) | |
1558 | ||
1559 | (defun reftex-expand-path (path &optional default-dir) | |
1560 | ;; Expand parts of path ending in `//' recursively into directory list. | |
1561 | ;; Relative recursive path elements are expanded relative to DEFAULT-DIR. | |
1562 | (let (path1 dir recursive) | |
1563 | (while (setq dir (pop path)) | |
1564 | (if (setq recursive (string= (substring dir -2) "//")) | |
1565 | (setq dir (substring dir 0 -1))) | |
1566 | (if (and recursive | |
1567 | (not (file-name-absolute-p dir))) | |
1568 | (setq dir (expand-file-name dir default-dir))) | |
1569 | (if recursive | |
1570 | ;; Expand recursively | |
1571 | (setq path1 (append (reftex-recursive-directory-list dir) path1)) | |
1572 | ;; Keep unchanged | |
1573 | (push dir path1))) | |
1574 | (nreverse path1))) | |
a7ec1775 | 1575 | |
b849548d CD |
1576 | (defun reftex-recursive-directory-list (dir) |
1577 | ;; Return a list of all directories below DIR, including DIR itself | |
1578 | (let ((path (list dir)) path1 file files) | |
1579 | (while (setq dir (pop path)) | |
1580 | (when (file-directory-p dir) | |
1581 | (setq files (nreverse (directory-files dir t "[^.]"))) | |
1582 | (while (setq file (pop files)) | |
12a382d4 | 1583 | (if (file-directory-p file) |
b849548d CD |
1584 | (push (file-name-as-directory file) path))) |
1585 | (push dir path1))) | |
1586 | path1)) | |
a7ec1775 | 1587 | |
f9ad2e24 CD |
1588 | (defun reftex-uniq (list) |
1589 | (let (new) | |
1590 | (while list | |
1591 | (or (member (car list) new) | |
1592 | (push (car list) new)) | |
1593 | (pop list)) | |
1594 | (nreverse new))) | |
1595 | ||
1c25ed90 CD |
1596 | |
1597 | ;;; ========================================================================= | |
1598 | ;;; | |
1599 | ;;; Parser functions | |
1600 | ||
1601 | (autoload 'reftex-parse-one "reftex-parse" | |
1602 | "Re-parse this file." t) | |
1603 | (autoload 'reftex-parse-all "reftex-parse" | |
1604 | "Re-parse entire document." t) | |
1605 | (autoload 'reftex-do-parse "reftex-parse") | |
1606 | (autoload 'reftex-where-am-I "reftex-parse") | |
1607 | (autoload 'reftex-init-section-numbers "reftex-parse") | |
1608 | (autoload 'reftex-section-info "reftex-parse") | |
1609 | (autoload 'reftex-section-number "reftex-parse") | |
1610 | (autoload 'reftex-what-macro "reftex-parse") | |
1611 | (autoload 'reftex-what-macro-safe "reftex-parse") | |
1612 | (autoload 'reftex-index-info "reftex-parse") | |
1613 | (autoload 'reftex-index-info-safe "reftex-parse") | |
1614 | (autoload 'reftex-short-context "reftex-parse") | |
1615 | (autoload 'reftex-what-environment "reftex-parse") | |
1616 | (autoload 'reftex-what-special-env "reftex-parse") | |
1617 | (autoload 'reftex-move-over-touching-args "reftex-parse") | |
1618 | (autoload 'reftex-notice-new "reftex-parse") | |
1619 | (autoload 'reftex-nth-arg "reftex-parse") | |
1620 | (autoload 'reftex-locate-bibliography-files "reftex-parse") | |
1c25ed90 CD |
1621 | (autoload 'reftex-ensure-index-support "reftex-parse") |
1622 | (autoload 'reftex-everything-regexp "reftex-parse") | |
1623 | ||
1624 | ||
1625 | ;;; ========================================================================= | |
1626 | ;;; | |
1627 | ;;; Labels and References | |
1628 | ||
1629 | (autoload 'reftex-label-location "reftex-ref") | |
1630 | (autoload 'reftex-label-info-update "reftex-ref") | |
1631 | (autoload 'reftex-label-info "reftex-ref") | |
1632 | (autoload 'reftex-label "reftex-ref" | |
1633 | "Insert a unique label." t) | |
1634 | (autoload 'reftex-reference "reftex-ref" | |
1635 | "Make a LaTeX reference." t) | |
70d797cd CD |
1636 | (autoload 'reftex-varioref-vref "reftex-ref" |
1637 | "Make a varioref reference." t) | |
1638 | (autoload 'reftex-fancyref-fref "reftex-ref" | |
1639 | "Make a fancyref \\fref reference." t) | |
1640 | (autoload 'reftex-fancyref-Fref "reftex-ref" | |
1641 | "Make a fancyref \\Fref reference." t) | |
1c25ed90 CD |
1642 | (autoload 'reftex-show-label-location "reftex-ref") |
1643 | (autoload 'reftex-query-label-type "reftex-ref") | |
3b919c9f CD |
1644 | (autoload 'reftex-goto-label "reftex-ref" |
1645 | "Prompt for label name and go to that location." t) | |
1c25ed90 CD |
1646 | |
1647 | ;;; ========================================================================= | |
1648 | ;;; | |
1649 | ;;; Table of contents | |
1650 | ||
1651 | (autoload 'reftex-toc "reftex-toc" | |
3b919c9f CD |
1652 | "Show the table of contents for the current document." t) |
1653 | (autoload 'reftex-toc-recenter "reftex-toc" | |
1654 | "Display the TOC window and highlight line corresponding to current position." t) | |
1655 | (autoload 'reftex-toggle-auto-toc-recenter "reftex-toc" t) | |
1c25ed90 CD |
1656 | |
1657 | ||
1658 | ;;; ========================================================================= | |
1659 | ;;; | |
1660 | ;;; BibTeX citations. | |
1661 | ||
d8fb2015 CD |
1662 | (autoload 'reftex-citep "reftex-cite") |
1663 | (autoload 'reftex-citet "reftex-cite") | |
1c25ed90 CD |
1664 | (autoload 'reftex-make-cite-echo-string "reftex-cite") |
1665 | (autoload 'reftex-get-bibfile-list "reftex-cite") | |
1666 | (autoload 'reftex-pop-to-bibtex-entry "reftex-cite") | |
1667 | (autoload 'reftex-end-of-bib-entry "reftex-cite") | |
1668 | (autoload 'reftex-parse-bibtex-entry "reftex-cite") | |
1669 | (autoload 'reftex-citation "reftex-cite" | |
1670 | "Make a citation using BibTeX database files." t) | |
1671 | (autoload 'reftex-default-bibliography "reftex-cite") | |
20cd3579 | 1672 | (autoload 'reftex-bib-or-thebib "reftex-cite") |
1c25ed90 CD |
1673 | |
1674 | ;;; ========================================================================= | |
1675 | ;;; | |
1676 | ;;; Selection | |
1677 | ||
1678 | (autoload 'reftex-select-label-mode "reftex-sel") | |
1679 | (autoload 'reftex-select-bib-mode "reftex-sel") | |
1680 | (autoload 'reftex-find-start-point "reftex-sel") | |
1681 | (autoload 'reftex-insert-docstruct "reftex-sel") | |
1682 | (autoload 'reftex-get-offset "reftex-sel") | |
1683 | (autoload 'reftex-select-item "reftex-sel") | |
1684 | ||
1685 | ||
1686 | ;;; ========================================================================= | |
1687 | ;;; | |
1688 | ;;; Index support | |
1689 | ||
1690 | (autoload 'reftex-index "reftex-index" | |
1691 | "Query for an index macro and insert it along with its argments." t) | |
1692 | (autoload 'reftex-index-selection-or-word "reftex-index" | |
1693 | "Put selection or the word near point into the default index macro." t) | |
7c4d13cc CD |
1694 | (autoload 'reftex-index-phrase-selection-or-word "reftex-index" |
1695 | "Put selection or the word near point into Index Phrases File." t) | |
1c25ed90 CD |
1696 | (autoload 'reftex-display-index "reftex-index" |
1697 | "Display a buffer with an index compiled from the current document." t) | |
7c4d13cc CD |
1698 | (autoload 'reftex-index-visit-phrases-buffer "reftex-index" |
1699 | "Visit the Index Phrases File." t) | |
1700 | (autoload 'reftex-index-phrases-mode "reftex-index" | |
1701 | "Major mode for managing the Index phrases of a LaTeX document." t) | |
1c25ed90 CD |
1702 | (autoload 'reftex-index-complete-tag "reftex-index") |
1703 | (autoload 'reftex-index-complete-key "reftex-index") | |
1704 | (autoload 'reftex-index-show-entry "reftex-index") | |
1705 | (autoload 'reftex-index-select-tag "reftex-index") | |
1706 | ||
1707 | ||
1708 | ;;; ========================================================================= | |
1709 | ;;; | |
1710 | ;;; View cross references | |
1711 | ||
df543367 | 1712 | (autoload 'reftex-view-crossref "reftex-dcr" |
1c25ed90 | 1713 | "View cross reference of \\ref or \\cite macro at point." t) |
df543367 | 1714 | (autoload 'reftex-mouse-view-crossref "reftex-dcr" |
1c25ed90 | 1715 | "View cross reference of \\ref or \\cite macro where you click." t) |
df543367 CD |
1716 | (autoload 'reftex-toggle-auto-view-crossref "reftex-dcr") |
1717 | (autoload 'reftex-view-crossref-from-bibtex "reftex-dcr" | |
1c25ed90 CD |
1718 | "View location in a LaTeX document which cites the BibTeX entry at point." t) |
1719 | ||
1720 | ||
1721 | ;;; ========================================================================= | |
1722 | ;;; | |
1723 | ;;; Operations on entire Multifile documents | |
1724 | ||
1725 | (autoload 'reftex-create-tags-file "reftex-global" | |
1726 | "Create TAGS file by running `etags' on the current document." t) | |
1727 | (autoload 'reftex-grep-document "reftex-global" | |
1728 | "Run grep query through all files related to this document." t) | |
1729 | (autoload 'reftex-search-document "reftex-global" | |
1730 | "Regexp search through all files of the current TeX document." t) | |
1731 | (autoload 'reftex-query-replace-document "reftex-global" | |
1732 | "Run a query-replace-regexp of FROM with TO over the entire TeX document." t) | |
1733 | (autoload 'reftex-find-duplicate-labels "reftex-global" | |
1734 | "Produce a list of all duplicate labels in the document." t) | |
1735 | (autoload 'reftex-change-label "reftex-global" | |
1736 | "Query replace FROM with TO in all \\label and \\ref commands." t) | |
1737 | (autoload 'reftex-renumber-simple-labels "reftex-global" | |
1738 | "Renumber all simple labels in the document to make them sequentially." t) | |
1739 | (autoload 'reftex-save-all-document-buffers "reftex-global" | |
1740 | "Save all documents associated with the current document." t) | |
1741 | ||
1742 | ||
1743 | ;;; ========================================================================= | |
1744 | ;;; | |
1745 | ;;; AUCTeX Interface | |
1746 | ||
1747 | (autoload 'reftex-arg-label "reftex-auc") | |
1748 | (autoload 'reftex-arg-cite "reftex-auc") | |
1749 | (autoload 'reftex-arg-index-tag "reftex-auc") | |
1750 | (autoload 'reftex-arg-index "reftex-auc") | |
1751 | (autoload 'reftex-plug-into-AUCTeX "reftex-auc") | |
1752 | (autoload 'reftex-toggle-plug-into-AUCTeX "reftex-auc" | |
1753 | "Toggle Interface between AUCTeX and RefTeX on and off." t) | |
1754 | (autoload 'reftex-add-label-environments "reftex-auc") | |
7c4d13cc | 1755 | (autoload 'reftex-add-to-label-alist "reftex-auc") |
1c25ed90 CD |
1756 | (autoload 'reftex-add-section-levels "reftex-auc") |
1757 | (autoload 'reftex-notice-new-section "reftex-auc") | |
1758 | ||
b849548d CD |
1759 | ;;; ========================================================================= |
1760 | ;;; | |
1761 | ;;; Some generally useful functions | |
a7ec1775 | 1762 | |
1c25ed90 CD |
1763 | (defun reftex-typekey-check (typekey conf-variable &optional n) |
1764 | ;; Check if CONF-VARIABLE is true or contains TYPEKEY | |
1765 | (and n (setq conf-variable (nth n conf-variable))) | |
1766 | (or (eq conf-variable t) | |
1767 | (and (stringp conf-variable) | |
1768 | (string-match (concat "[" conf-variable "]") typekey)))) | |
1769 | ||
1770 | (defun reftex-check-recursive-edit () | |
1771 | ;; Check if we are already in a recursive edit. Abort with helpful | |
1772 | ;; message if so. | |
1773 | (if (marker-position reftex-recursive-edit-marker) | |
1774 | (error | |
1775 | (substitute-command-keys | |
1776 | "In unfinished selection process. Finish, or abort with \\[abort-recursive-edit]")))) | |
1777 | ||
1778 | (defun reftex-in-comment () | |
1779 | (save-excursion | |
1780 | (skip-chars-backward "^%\n\r") | |
1781 | (eq (preceding-char) ?%))) | |
1782 | ||
a7ec1775 RS |
1783 | (defun reftex-no-props (string) |
1784 | ;; Return STRING with all text properties removed | |
1785 | (and (stringp string) | |
1786 | (set-text-properties 0 (length string) nil string)) | |
1787 | string) | |
1788 | ||
396e0b08 KH |
1789 | (defun reftex-match-string (n) |
1790 | ;; Match string without properties | |
1791 | (when (match-beginning n) | |
1792 | (buffer-substring-no-properties (match-beginning n) (match-end n)))) | |
1793 | ||
2faef409 | 1794 | (defun reftex-kill-buffer (buffer) |
29d593f8 | 1795 | ;; Kill buffer if it exists. |
2faef409 RS |
1796 | (and (setq buffer (get-buffer buffer)) |
1797 | (kill-buffer buffer))) | |
1798 | ||
29d593f8 KH |
1799 | (defun reftex-erase-buffer (&optional buffer) |
1800 | ;; Erase BUFFER if it exists. BUFFER defaults to current buffer. | |
1801 | ;; This even erases read-only buffers. | |
1802 | (cond | |
1803 | ((null buffer) | |
1804 | ;; erase current buffer | |
1805 | (let ((buffer-read-only nil)) (erase-buffer))) | |
1806 | ((setq buffer (get-buffer buffer)) | |
1807 | ;; buffer exists | |
1808 | (save-excursion | |
1809 | (set-buffer buffer) | |
1810 | (let ((buffer-read-only nil)) (erase-buffer)))))) | |
2faef409 | 1811 | |
396e0b08 KH |
1812 | (defun reftex-this-word (&optional class) |
1813 | ;; Grab the word around point. | |
1814 | (setq class (or class "-a-zA-Z0-9:_/.*;|")) | |
1815 | (save-excursion | |
1816 | (buffer-substring-no-properties | |
1817 | (progn (skip-chars-backward class) (point)) | |
1818 | (progn (skip-chars-forward class) (point))))) | |
1819 | ||
7c4d13cc CD |
1820 | (defun reftex-number (n unit &optional ending) |
1821 | (if (and (integerp n) (stringp unit)) | |
1822 | (format "%d %s%s" n unit (if (= n 1) "" (or ending "s"))) | |
1823 | "")) | |
1824 | ||
b849548d CD |
1825 | (defun reftex-all-assq (key list) |
1826 | ;; Return a list of all associations of KEY in LIST. Comparison with eq. | |
1827 | (let (rtn) | |
1828 | (while (setq list (memq (assq key list) list)) | |
1829 | (push (car list) rtn) | |
1830 | (pop list)) | |
1831 | (nreverse rtn))) | |
1832 | ||
1833 | (defun reftex-all-assoc-string (key list) | |
1834 | ;; Return a list of all associations of KEY in LIST. Comparison with string=. | |
1835 | (let (rtn) | |
1836 | (while list | |
1837 | (if (string= (car (car list)) key) | |
1838 | (push (car list) rtn)) | |
1839 | (pop list)) | |
1840 | (nreverse rtn))) | |
1841 | ||
1c25ed90 | 1842 | (defun reftex-last-assoc-before-elt (key elt list &optional exclusive) |
b849548d CD |
1843 | ;; Find the last association of KEY in LIST before or at ELT |
1844 | ;; ELT is found in LIST with equal, not eq. | |
1845 | ;; Returns nil when either KEY or elt are not found in LIST. | |
1c25ed90 | 1846 | ;; When EXCLUSIVE is non-nil, ELT cannot be the return value. |
b849548d | 1847 | ;; On success, returns the association. |
1c25ed90 | 1848 | (let* ((elt (car (member elt list))) (ex (not exclusive)) ass last-ass) |
b849548d CD |
1849 | (while (and (setq ass (assoc key list)) |
1850 | (setq list (memq ass list)) | |
1c25ed90 | 1851 | (or ex (not (eq elt (car list)))) |
b849548d CD |
1852 | (memq elt list)) |
1853 | (setq last-ass ass | |
1854 | list (cdr list))) | |
1855 | last-ass)) | |
1856 | ||
1c25ed90 CD |
1857 | (defun reftex-sublist-nth (list nth predicate &optional completion) |
1858 | ;; Make a list of the NTH elements of all members of LIST which | |
1859 | ;; fulfill PREDICATE. | |
1860 | ;; When COMPLETION is non-nil, make all elements of the resulting | |
1861 | ;; list also a list, so that the result can be used for completion. | |
1862 | (let (rtn) | |
1863 | (while list | |
1864 | (if (funcall predicate (car list)) | |
1865 | (push (if completion | |
12a382d4 | 1866 | (list (nth nth (car list))) |
1c25ed90 CD |
1867 | (nth nth (car list))) |
1868 | rtn)) | |
1869 | (setq list (cdr list))) | |
1870 | (nreverse rtn))) | |
1871 | ||
1872 | (defun reftex-make-selection-buffer-name (type &optional index) | |
1873 | ;; Make unique name for a selection buffer. | |
1874 | (format " *RefTeX[%s][%d]*" | |
1875 | type (or index (get reftex-docstruct-symbol :master-index) 0))) | |
1876 | ||
1877 | (defun reftex-make-index-buffer-name (tag &optional cnt) | |
1878 | ;; Make unique name for an index buffer. | |
1879 | (format "*Index[%s][%d]*" | |
1880 | tag (or cnt (get reftex-docstruct-symbol :master-index) 0))) | |
1881 | ||
396e0b08 | 1882 | (defun reftex-truncate (string ncols &optional ellipses padding) |
f9ad2e24 CD |
1883 | ;; Truncate STRING to NCOLS characters. |
1884 | ;; When PADDING is non-nil, and string is shorter than NCOLS, fill with | |
1885 | ;; white space to NCOLS characters. When ELLIPSES is non-nil and the | |
1886 | ;; string needs to be truncated, replace last 3 characters by dots. | |
396e0b08 | 1887 | (setq string |
f9ad2e24 CD |
1888 | (if (<= (length string) ncols) |
1889 | string | |
1890 | (if ellipses | |
1891 | (concat (substring string 0 (- ncols 3)) "...") | |
1892 | (substring string 0 ncols)))) | |
396e0b08 KH |
1893 | (if padding |
1894 | (format (format "%%-%ds" ncols) string) | |
1895 | string)) | |
1896 | ||
1c25ed90 | 1897 | (defun reftex-nearest-match (regexp &optional max-length) |
b849548d CD |
1898 | ;; Find the nearest match of REGEXP. Set the match data. |
1899 | ;; If POS is given, calculate distances relative to it. | |
1900 | ;; Return nil if there is no match. | |
1c25ed90 | 1901 | (let ((pos (point)) |
12a382d4 | 1902 | (dist (or max-length (length regexp))) |
1c25ed90 CD |
1903 | match1 match2 match) |
1904 | (goto-char (min (+ pos dist) (point-max))) | |
b849548d CD |
1905 | (when (re-search-backward regexp nil t) |
1906 | (setq match1 (match-data))) | |
1c25ed90 | 1907 | (goto-char (max (- pos dist) (point-min))) |
b849548d CD |
1908 | (when (re-search-forward regexp nil t) |
1909 | (setq match2 (match-data))) | |
1c25ed90 | 1910 | (goto-char pos) |
b849548d CD |
1911 | (setq match |
1912 | (cond | |
1913 | ((not match1) match2) | |
1914 | ((not match2) match1) | |
1915 | ((< (abs (- pos (car match1))) (abs (- pos (car match2)))) match1) | |
1916 | (t match2))) | |
1917 | (if match (progn (set-match-data match) t) nil))) | |
1918 | ||
1919 | (defun reftex-auto-mode-alist () | |
1920 | ;; Return an `auto-mode-alist' with only the .gz (etc) thingies. | |
1921 | ;; Stolen from gnus nnheader. | |
1922 | (let ((alist auto-mode-alist) | |
1923 | out) | |
1924 | (while alist | |
1925 | (when (listp (cdr (car alist))) | |
1926 | (push (car alist) out)) | |
1927 | (pop alist)) | |
1928 | (nreverse out))) | |
1929 | ||
921759ee CD |
1930 | (defun reftex-window-height () |
1931 | (if (fboundp 'window-displayed-height) | |
1932 | (window-displayed-height) | |
1933 | (window-height))) | |
1934 | ||
b849548d CD |
1935 | (defun reftex-enlarge-to-fit (buf2 &optional keep-current) |
1936 | ;; Enlarge other window displaying buffer to show whole buffer if possible. | |
1937 | ;; If KEEP-CURRENT in non-nil, current buffer must remain visible. | |
1938 | (let* ((win1 (selected-window)) | |
1939 | (buf1 (current-buffer)) | |
921759ee | 1940 | (win2 (get-buffer-window buf2))) ;; Only on current frame. |
b849548d CD |
1941 | (when win2 |
1942 | (select-window win2) | |
1943 | (unless (and (pos-visible-in-window-p 1) | |
1944 | (pos-visible-in-window-p (point-max))) | |
1945 | (enlarge-window (1+ (- (count-lines 1 (point-max)) | |
921759ee | 1946 | (reftex-window-height)))))) |
b849548d CD |
1947 | (cond |
1948 | ((window-live-p win1) (select-window win1)) | |
1949 | (keep-current | |
1950 | ;; we must have the old buffer! | |
1951 | (switch-to-buffer-other-window buf1) | |
1952 | (shrink-window (- (window-height) window-min-height)))))) | |
1953 | ||
1954 | (defun reftex-select-with-char (prompt help-string &optional delay-time scroll) | |
1955 | ;; Offer to select something with PROMPT and, after DELAY-TIME seconds, | |
1956 | ;; also with HELP-STRING. | |
1957 | ;; When SCROLL is non-nil, use SPC and DEL to scroll help window. | |
1958 | (let ((char ?\?)) | |
1959 | (save-window-excursion | |
1960 | (catch 'exit | |
1961 | (message (concat prompt " (?=Help)")) | |
1962 | (when (or (sit-for (or delay-time 0)) | |
1963 | (= ?\? (setq char (read-char-exclusive)))) | |
921759ee CD |
1964 | (reftex-kill-buffer "*RefTeX Select*") |
1965 | (switch-to-buffer-other-window "*RefTeX Select*") | |
1966 | (insert help-string) | |
1967 | (goto-char 1) | |
ea6a015d | 1968 | (unless (and (pos-visible-in-window-p (point-min)) |
921759ee | 1969 | (pos-visible-in-window-p (point-max))) |
ea6a015d | 1970 | (enlarge-window (1+ (- (count-lines (point-min) (point-max)) |
921759ee | 1971 | (reftex-window-height))))) |
b849548d | 1972 | (setq truncate-lines t)) |
ea6a015d | 1973 | (if (and (pos-visible-in-window-p (point-min)) |
1c25ed90 CD |
1974 | (pos-visible-in-window-p (point-max))) |
1975 | nil | |
1976 | (setq prompt (concat prompt (if scroll " (SPC/DEL=Scroll)" "")))) | |
b849548d CD |
1977 | (message prompt) |
1978 | (and (equal char ?\?) (setq char (read-char-exclusive))) | |
1979 | (while t | |
1980 | (cond ((equal char ?\C-g) (keyboard-quit)) | |
1981 | ((equal char ?\?)) | |
1982 | ((and scroll (equal char ?\ )) | |
1983 | (condition-case nil (scroll-up) (error nil)) | |
1984 | (message prompt)) | |
1985 | ((and scroll (equal char ?\C-? )) | |
1986 | (condition-case nil (scroll-down) (error nil)) | |
1987 | (message prompt)) | |
12a382d4 | 1988 | (t (message "") |
1c25ed90 | 1989 | (throw 'exit char))) |
b849548d | 1990 | (setq char (read-char-exclusive))))))) |
12a382d4 | 1991 | |
b849548d CD |
1992 | |
1993 | (defun reftex-make-regexp-allow-for-ctrl-m (string) | |
1994 | ;; convert STRING into a regexp, allowing ^M for \n and vice versa | |
1995 | (let ((start -2)) | |
1996 | (setq string (regexp-quote string)) | |
1997 | (while (setq start (string-match "[\n\r]" string (+ 3 start))) | |
1998 | (setq string (replace-match "[\n\r]" nil t string))) | |
1999 | string)) | |
2000 | ||
2001 | (defun reftex-get-buffer-visiting (file) | |
2002 | ;; return a buffer visiting FILE | |
2003 | (cond | |
2004 | ((boundp 'find-file-compare-truenames) ; XEmacs | |
2005 | (let ((find-file-compare-truenames t)) | |
2006 | (get-file-buffer file))) | |
2007 | ((fboundp 'find-buffer-visiting) ; Emacs | |
2008 | (find-buffer-visiting file)) | |
2009 | (t (error "This should not happen (reftex-get-buffer-visiting)")))) | |
2010 | ||
2011 | ;; Define `current-message' for compatibility with XEmacs prior to 20.4 | |
2012 | (defvar message-stack) | |
2013 | (if (and (featurep 'xemacs) | |
2014 | (not (fboundp 'current-message))) | |
2015 | (defun current-message (&optional frame) | |
2016 | (cdr (car message-stack)))) | |
2017 | ||
2018 | (defun reftex-visited-files (list) | |
2019 | ;; Takes a list of filenames and returns the buffers of those already visited | |
2020 | (delq nil (mapcar (lambda (x) (if (reftex-get-buffer-visiting x) x nil)) | |
2021 | list))) | |
2022 | ||
2023 | (defun reftex-get-file-buffer-force (file &optional mark-to-kill) | |
2024 | ;; Return a buffer visiting file. Make one, if necessary. | |
2025 | ;; If neither such a buffer nor the file exist, return nil. | |
2026 | ;; If MARK-TO-KILL is t and there is no live buffer, visit the file with | |
2027 | ;; initializations according to `reftex-initialize-temporary-buffers', | |
2028 | ;; and mark the buffer to be killed after use. | |
2029 | ||
2030 | (let ((buf (reftex-get-buffer-visiting file))) | |
2031 | ||
2032 | (cond (buf | |
2033 | ;; We have it already as a buffer - just return it | |
2034 | buf) | |
2035 | ||
2036 | ((file-readable-p file) | |
2037 | ;; At least there is such a file and we can read it. | |
2038 | ||
2039 | (if (or (not mark-to-kill) | |
2040 | (eq t reftex-initialize-temporary-buffers)) | |
2041 | ||
2042 | ;; Visit the file with full magic | |
2043 | (setq buf (find-file-noselect file)) | |
2044 | ||
2045 | ;; Else: Visit the file just briefly, without or | |
2046 | ;; with limited Magic | |
2047 | ||
2048 | ;; The magic goes away | |
2049 | (let ((format-alist nil) | |
2050 | (auto-mode-alist (reftex-auto-mode-alist)) | |
2051 | (default-major-mode 'fundamental-mode) | |
2052 | (enable-local-variables nil) | |
2053 | (after-insert-file-functions nil)) | |
2054 | (setq buf (find-file-noselect file))) | |
2055 | ||
2056 | ;; Is there a hook to run? | |
2057 | (when (listp reftex-initialize-temporary-buffers) | |
2058 | (save-excursion | |
2059 | (set-buffer buf) | |
2060 | (run-hooks 'reftex-initialize-temporary-buffers)))) | |
2061 | ||
2062 | ;; Lets see if we got a license to kill :-| | |
2063 | (and mark-to-kill | |
1c25ed90 | 2064 | (add-to-list 'reftex-buffers-to-kill buf)) |
a7ec1775 | 2065 | |
1c25ed90 CD |
2066 | ;; Return the new buffer |
2067 | buf) | |
396e0b08 | 2068 | |
1c25ed90 CD |
2069 | ;; If no such file exists, return nil |
2070 | (t nil)))) | |
396e0b08 | 2071 | |
1c25ed90 CD |
2072 | (defun reftex-kill-temporary-buffers (&optional buffer) |
2073 | ;; Kill all buffers in the list reftex-kill-temporary-buffers. | |
2074 | (cond | |
2075 | (buffer | |
2076 | (when (member buffer reftex-buffers-to-kill) | |
2077 | (kill-buffer buffer) | |
2078 | (setq reftex-buffers-to-kill | |
2079 | (delete buffer reftex-buffers-to-kill)))) | |
2080 | (t | |
2081 | (while (setq buffer (pop reftex-buffers-to-kill)) | |
2082 | (when (bufferp buffer) | |
2083 | (and (buffer-modified-p buffer) | |
2084 | (y-or-n-p (format "Save file %s? " | |
2085 | (buffer-file-name buffer))) | |
2086 | (save-excursion | |
2087 | (set-buffer buffer) | |
2088 | (save-buffer))) | |
2089 | (kill-buffer buffer)) | |
2090 | (pop reftex-buffers-to-kill))))) | |
396e0b08 | 2091 | |
1c25ed90 CD |
2092 | (defun reftex-splice-symbols-into-list (list alist) |
2093 | ;; Splice the association in ALIST of any symbols in LIST into the list. | |
2094 | ;; Return new list. | |
2095 | (let (rtn tmp) | |
2096 | (while list | |
2097 | (while (and (not (null (car list))) ;; keep list elements nil | |
2098 | (symbolp (car list))) | |
2099 | (setq tmp (car list)) | |
2100 | (cond | |
2101 | ((assoc tmp alist) | |
2102 | (setq list (append (nth 2 (assoc tmp alist)) (cdr list)))) | |
2103 | (t | |
2104 | (error "Cannot treat symbol %s in reftex-label-alist" | |
2105 | (symbol-name tmp))))) | |
2106 | (push (pop list) rtn)) | |
2107 | (nreverse rtn))) | |
396e0b08 | 2108 | |
1c25ed90 CD |
2109 | (defun reftex-remove-symbols-from-list (list) |
2110 | ;; Remove all symbols from list | |
2111 | (let (rtn) | |
2112 | (while list | |
2113 | (unless (symbolp (car list)) | |
2114 | (push (car list) rtn)) | |
2115 | (setq list (cdr list))) | |
2116 | (nreverse rtn))) | |
b849548d | 2117 | |
48ffe14f CD |
2118 | (defun reftex-uniquify (list) |
2119 | ;; Return a list of all elements in LIST, but each only once | |
2120 | (let (new elm) | |
2121 | (while list | |
2122 | (setq elm (pop list)) | |
2123 | (unless (member elm new) | |
2124 | (push elm new))) | |
2125 | (nreverse new))) | |
2126 | ||
1c25ed90 CD |
2127 | (defun reftex-uniquify-by-car (alist &optional keep-list) |
2128 | ;; Return a list of all elements in ALIST, but each car only once. | |
2129 | ;; Elements of KEEP-LIST are not removed even if duplicate. | |
2130 | (let (new elm) | |
2131 | (while alist | |
2132 | (setq elm (pop alist)) | |
2133 | (if (or (member (car elm) keep-list) | |
2134 | (not (assoc (car elm) new))) | |
2135 | (push elm new))) | |
2136 | (nreverse new))) | |
b849548d | 2137 | |
1c25ed90 CD |
2138 | (defun reftex-abbreviate-title (string) |
2139 | (reftex-convert-string string "[-~ \t\n\r,;]" nil t t | |
2140 | 5 40 nil 1 " " (nth 5 reftex-derive-label-parameters))) | |
b849548d | 2141 | |
1c25ed90 CD |
2142 | (defun reftex-convert-string (string split-re illegal-re dot keep-fp |
2143 | nwords maxchar illegal abbrev sep | |
2144 | ignore-words &optional downcase) | |
2145 | "Convert a string (a sentence) to something shorter. | |
2146 | SPLIT-RE is the regular expression used to split the string into words. | |
2147 | ILLEGAL-RE matches characters which are illegal in the final string. | |
2148 | DOT t means add dots to abbreviated words. | |
2149 | KEEP-FP t means to keep a final punctuation when applicable. | |
2150 | NWORDS Number of words to use. | |
2151 | MAXCHAR Maximum number of characters in the final string. | |
2152 | ILLEGAL nil: Throw away any words containing stuff matched with ILLEGAL-RE. | |
2153 | t: Throw away only the matched part, not the whole word. | |
2154 | ABBREV nil: Never abbreviate words. | |
2155 | t: Always abbreviate words (see `reftex-abbrev-parameters'). | |
2156 | not t and not nil: Abbreviate words if necessary to shorten | |
2157 | string below MAXCHAR. | |
2158 | SEP String separating different words in the output string. | |
2159 | IGNORE-WORDS List of words which should be removed from the string." | |
396e0b08 | 2160 | |
1c25ed90 CD |
2161 | (let* ((words0 (split-string string (or split-re "[ \t\n\r]"))) |
2162 | (reftex-label-illegal-re (or illegal-re "\000")) | |
2163 | (abbrev-re (concat | |
2164 | "\\`\\(" | |
2165 | (make-string (nth 0 reftex-abbrev-parameters) ?.) | |
2166 | "[" (nth 2 reftex-abbrev-parameters) "]*" | |
2167 | "\\)" | |
2168 | "[" (nth 3 reftex-abbrev-parameters) "]" | |
2169 | (make-string (1- (nth 1 reftex-abbrev-parameters)) ?.))) | |
2170 | words word) | |
396e0b08 | 2171 | |
1c25ed90 CD |
2172 | ;; Remove words from the ignore list or with funny characters |
2173 | (while (setq word (pop words0)) | |
2174 | (if downcase (setq word (downcase word))) | |
2175 | (cond | |
2176 | ((member (downcase word) ignore-words)) | |
2177 | ((string-match reftex-label-illegal-re word) | |
2178 | (when illegal | |
2179 | (while (string-match reftex-label-illegal-re word) | |
2180 | (setq word (replace-match "" nil nil word))) | |
2181 | (push word words))) | |
2182 | (t | |
2183 | (push word words)))) | |
2184 | (setq words (nreverse words)) | |
2185 | ||
2186 | ;; Restrict number of words | |
2187 | (if (> (length words) nwords) | |
2188 | (setcdr (nthcdr (1- nwords) words) nil)) | |
12a382d4 | 2189 | |
1c25ed90 CD |
2190 | ;; First, try to use all words |
2191 | (setq string (mapconcat 'identity words sep)) | |
12a382d4 | 2192 | |
1c25ed90 CD |
2193 | ;; Abbreviate words if enforced by user settings or string length |
2194 | (if (or (eq t abbrev) | |
2195 | (and abbrev | |
2196 | (> (length string) maxchar))) | |
2197 | (setq words | |
2198 | (mapcar | |
2199 | (lambda (w) (if (string-match abbrev-re w) | |
2200 | (if dot | |
2201 | (concat (match-string 1 w) ".") | |
2202 | (match-string 1 w)) | |
2203 | w)) | |
2204 | words) | |
2205 | string (mapconcat 'identity words sep))) | |
b849548d | 2206 | |
1c25ed90 CD |
2207 | ;; Shorten if still to long |
2208 | (setq string | |
2209 | (if (> (length string) maxchar) | |
2210 | (substring string 0 maxchar) | |
2211 | string)) | |
396e0b08 | 2212 | |
1c25ed90 CD |
2213 | ;; Delete the final punctuation, if any |
2214 | (if (and (not keep-fp) (string-match "\\s.+\\'" string)) | |
2215 | (setq string (replace-match "" nil nil string))) | |
2216 | string)) | |
396e0b08 | 2217 | |
1c25ed90 CD |
2218 | (defun reftex-nicify-text (text) |
2219 | ;; Make TEXT nice for inclusion as context into label menu. | |
2220 | ;; 1. remove line breaks and extra white space | |
2221 | (while (string-match "[\n\r\t]\\|[ \t][ \t]+" text) | |
2222 | (setq text (replace-match " " nil t text))) | |
2223 | ;; 2. cut before the next `\end{' or `\item' or `\\' | |
2224 | (if (string-match "\\(\\\\end{\\|\\\\item\\|\\\\\\\\\\).*" text) | |
2225 | (setq text (replace-match "" nil t text))) | |
2226 | ;; 3. kill the embedded label | |
2227 | (if (string-match "\\\\label{[^}]*}" text) | |
2228 | (setq text (replace-match "" nil t text))) | |
2229 | ;; 4. remove leading garbage | |
2230 | (if (string-match "\\`[ }]+" text) | |
2231 | (setq text (replace-match "" nil t text))) | |
2232 | ;; 5. limit length | |
2233 | (cond | |
2234 | ((> (length text) 100) (substring text 0 100)) | |
2235 | ((= (length text) 0) (make-string 1 ?\ )) | |
2236 | (t text))) | |
396e0b08 | 2237 | |
b849548d CD |
2238 | ;;; ========================================================================= |
2239 | ;;; | |
1c25ed90 | 2240 | ;;; Fontification and Highlighting |
a7ec1775 | 2241 | |
1c25ed90 CD |
2242 | (defun reftex-use-fonts () |
2243 | ;; Return t if we can and want to use fonts. | |
2244 | (and window-system | |
2245 | reftex-use-fonts | |
2246 | (featurep 'font-lock))) | |
a7ec1775 | 2247 | |
1c25ed90 CD |
2248 | (defun reftex-refontify () |
2249 | ;; Return t if we need to refontify context | |
2250 | (and (reftex-use-fonts) | |
2251 | (or (eq t reftex-refontify-context) | |
2252 | (and (eq 1 reftex-refontify-context) | |
2253 | ;; Test of we use the font-lock version of x-symbol | |
2254 | (and (featurep 'x-symbol-tex) (not (boundp 'x-symbol-mode))))))) | |
a7ec1775 | 2255 | |
1c25ed90 CD |
2256 | (defvar font-lock-defaults-computed) |
2257 | (defun reftex-fontify-select-label-buffer (parent-buffer) | |
2258 | ;; Fontify the `*RefTeX Select*' buffer. Buffer is temporarily renamed to | |
2259 | ;; start with none-SPC char, beacuse Font-Lock otherwise refuses operation. | |
2260 | (run-hook-with-args 'reftex-pre-refontification-functions | |
2261 | parent-buffer 'reftex-ref) | |
2262 | (let* ((oldname (buffer-name)) | |
2263 | (newname (concat "Fontify-me-" oldname))) | |
2264 | (unwind-protect | |
b849548d | 2265 | (progn |
1c25ed90 CD |
2266 | ;; Rename buffer temporarily to start w/o space (because of font-lock) |
2267 | (rename-buffer newname t) | |
2268 | (cond | |
2269 | ((fboundp 'font-lock-default-fontify-region) | |
2270 | ;; Good: we have the indirection functions | |
2271 | (set (make-local-variable 'font-lock-fontify-region-function) | |
2272 | 'reftex-select-font-lock-fontify-region) | |
2273 | (let ((major-mode 'latex-mode)) | |
2274 | (font-lock-mode 1))) | |
2275 | ((fboundp 'font-lock-set-defaults-1) | |
2276 | ;; Looks like the XEmacs font-lock stuff. | |
2277 | ;; FIXME: this is still kind of a hack, but it works. | |
2278 | (set (make-local-variable 'font-lock-keywords) nil) | |
2279 | (let ((major-mode 'latex-mode) | |
2280 | (font-lock-defaults-computed nil)) | |
2281 | (font-lock-set-defaults-1) | |
2282 | (reftex-select-font-lock-fontify-region (point-min) (point-max)))) | |
2283 | (t | |
12a382d4 | 2284 | ;; Oops? |
1c25ed90 CD |
2285 | (message "Sorry: cannot refontify RefTeX Select buffer.")))) |
2286 | (rename-buffer oldname)))) | |
a7ec1775 | 2287 | |
1c25ed90 CD |
2288 | (defun reftex-select-font-lock-fontify-region (beg end &optional loudly) |
2289 | ;; Fontify a region, but only lines starting with a dot. | |
2290 | (let ((func (if (fboundp 'font-lock-default-fontify-region) | |
2291 | 'font-lock-default-fontify-region | |
2292 | 'font-lock-fontify-region)) | |
2293 | beg1 end1) | |
2294 | (goto-char beg) | |
2295 | (while (re-search-forward "^\\." end t) | |
2296 | (setq beg1 (point) end1 (progn (skip-chars-forward "^\n") (point))) | |
2297 | (funcall func beg1 end1 nil) | |
2298 | (goto-char end1)))) | |
a7ec1775 | 2299 | |
1c25ed90 | 2300 | (defun reftex-select-font-lock-unfontify (&rest ignore) t) |
a7ec1775 | 2301 | |
1c25ed90 CD |
2302 | (defun reftex-verified-face (&rest faces) |
2303 | ;; Return the first valid face in FACES, or nil if none is valid. | |
2304 | ;; Also, when finding a nil element in FACES, return nil. This | |
2305 | ;; function is just a safety net to catch name changes of builtin | |
2306 | ;; fonts. Currently it is only used for reftex-label-face, which has | |
2307 | ;; as default font-lock-reference-face, which was recently renamed | |
2308 | ;; to font-lock-constant-face. | |
2309 | (let (face) | |
2310 | (catch 'exit | |
2311 | (while (setq face (pop faces)) | |
2312 | (if (featurep 'xemacs) | |
2313 | (if (find-face face) (throw 'exit face)) | |
2314 | (if (facep face) (throw 'exit face))))))) | |
a7ec1775 | 2315 | |
1c25ed90 CD |
2316 | ;; Highlighting uses overlays. For XEmacs, we need the emulation. |
2317 | (if (featurep 'xemacs) (require 'overlay)) | |
396e0b08 | 2318 | |
1c25ed90 | 2319 | ;; We keep a vector with several different overlays to do our highlighting. |
3b919c9f | 2320 | (defvar reftex-highlight-overlays [nil nil nil]) |
b849548d | 2321 | |
1c25ed90 CD |
2322 | ;; Initialize the overlays |
2323 | (aset reftex-highlight-overlays 0 (make-overlay 1 1)) | |
12a382d4 | 2324 | (overlay-put (aref reftex-highlight-overlays 0) |
1c25ed90 CD |
2325 | 'face 'highlight) |
2326 | (aset reftex-highlight-overlays 1 (make-overlay 1 1)) | |
2327 | (overlay-put (aref reftex-highlight-overlays 1) | |
2328 | 'face reftex-cursor-selected-face) | |
3b919c9f CD |
2329 | (aset reftex-highlight-overlays 2 (make-overlay 1 1)) |
2330 | (overlay-put (aref reftex-highlight-overlays 2) | |
2331 | 'face reftex-cursor-selected-face) | |
b849548d | 2332 | |
1c25ed90 CD |
2333 | ;; Two functions for activating and deactivation highlight overlays |
2334 | (defun reftex-highlight (index begin end &optional buffer) | |
2335 | "Highlight a region with overlay INDEX." | |
2336 | (move-overlay (aref reftex-highlight-overlays index) | |
2337 | begin end (or buffer (current-buffer)))) | |
2338 | (defun reftex-unhighlight (index) | |
2339 | "Detach overlay INDEX." | |
2340 | (delete-overlay (aref reftex-highlight-overlays index))) | |
2faef409 | 2341 | |
1c25ed90 CD |
2342 | (defun reftex-highlight-shall-die () |
2343 | ;; Function used in pre-command-hook to remove highlights. | |
2344 | (remove-hook 'pre-command-hook 'reftex-highlight-shall-die) | |
2345 | (reftex-unhighlight 0)) | |
b849548d CD |
2346 | |
2347 | ;;; ========================================================================= | |
2348 | ;;; | |
12a382d4 | 2349 | ;;; Keybindings |
a7ec1775 | 2350 | |
b849548d CD |
2351 | ;; The default bindings in the mode map. |
2352 | (loop for x in | |
7c4d13cc | 2353 | '(("\C-c=" . reftex-toc) |
3b919c9f | 2354 | ("\C-c-" . reftex-toc-recenter) |
7c4d13cc CD |
2355 | ("\C-c(" . reftex-label) |
2356 | ("\C-c)" . reftex-reference) | |
2357 | ("\C-c[" . reftex-citation) | |
2358 | ("\C-c<" . reftex-index) | |
2359 | ("\C-c>" . reftex-display-index) | |
2360 | ("\C-c/" . reftex-index-selection-or-word) | |
2361 | ("\C-c\\" . reftex-index-phrase-selection-or-word) | |
2362 | ("\C-c|" . reftex-index-visit-phrases-buffer) | |
2363 | ("\C-c&" . reftex-view-crossref)) | |
b849548d | 2364 | do (define-key reftex-mode-map (car x) (cdr x))) |
a7ec1775 | 2365 | |
1c25ed90 CD |
2366 | ;; Bind `reftex-mouse-view-crossref' only when the key is still free |
2367 | (if (featurep 'xemacs) | |
2368 | (unless (key-binding [(shift button2)]) | |
12a382d4 | 2369 | (define-key reftex-mode-map [(shift button2)] |
1c25ed90 CD |
2370 | 'reftex-mouse-view-crossref)) |
2371 | (unless (key-binding [(shift mouse-2)]) | |
12a382d4 | 2372 | (define-key reftex-mode-map [(shift mouse-2)] |
1c25ed90 CD |
2373 | 'reftex-mouse-view-crossref))) |
2374 | ||
2375 | ;; Bind `reftex-view-crossref-from-bibtex' in BibTeX mode map | |
2376 | (eval-after-load | |
921759ee CD |
2377 | "bibtex" |
2378 | '(define-key bibtex-mode-map "\C-c&" 'reftex-view-crossref-from-bibtex)) | |
2379 | ||
a7ec1775 | 2380 | ;; If the user requests so, she can have a few more bindings: |
b849548d CD |
2381 | (when reftex-extra-bindings |
2382 | (loop for x in | |
2383 | '(("\C-ct" . reftex-toc) | |
2384 | ("\C-cl" . reftex-label) | |
2385 | ("\C-cr" . reftex-reference) | |
2386 | ("\C-cc" . reftex-citation) | |
2387 | ("\C-cv" . reftex-view-crossref) | |
2388 | ("\C-cg" . reftex-grep-document) | |
2389 | ("\C-cs" . reftex-search-document)) | |
2390 | do (define-key reftex-mode-map (car x) (cdr x)))) | |
2391 | ||
b849548d CD |
2392 | ;;; ========================================================================= |
2393 | ;;; | |
2394 | ;;; Menu | |
a7ec1775 RS |
2395 | |
2396 | ;; Define a menu for the menu bar if Emacs is running under X | |
2397 | ||
2398 | (require 'easymenu) | |
2399 | ||
f9ad2e24 | 2400 | (easy-menu-define reftex-mode-menu reftex-mode-map |
a7ec1775 | 2401 | "Menu used in RefTeX mode" |
396e0b08 | 2402 | `("Ref" |
206c6f82 | 2403 | ["Table of Contents" reftex-toc t] |
3b919c9f | 2404 | ["Recenter TOC" reftex-toc-recenter t] |
1c25ed90 | 2405 | "--" |
206c6f82 RS |
2406 | ["\\label" reftex-label t] |
2407 | ["\\ref" reftex-reference t] | |
2408 | ["\\cite" reftex-citation t] | |
7c4d13cc CD |
2409 | ("\\index" |
2410 | ["\\index" reftex-index t] | |
2411 | ["\\index{THIS}" reftex-index-selection-or-word t] | |
2412 | "--" | |
2413 | ["Add THIS to Index Phrases" reftex-index-phrase-selection-or-word t] | |
2414 | ["Visit Phrase Buffer" reftex-index-visit-phrases-buffer t] | |
2415 | ["Apply Phrases to Region" reftex-index-phrases-apply-to-region t] | |
2416 | "--" | |
2417 | ["Display the Index" reftex-display-index t]) | |
1c25ed90 | 2418 | "--" |
7c4d13cc | 2419 | ["View Crossref" reftex-view-crossref t] |
1c25ed90 | 2420 | "--" |
396e0b08 | 2421 | ("Parse Document" |
1c25ed90 CD |
2422 | ["One File" reftex-parse-one reftex-enable-partial-scans] |
2423 | ["Entire Document" reftex-parse-all t] | |
baec1250 | 2424 | ["Save to File" (reftex-access-parse-file 'write) |
396e0b08 | 2425 | (> (length (symbol-value reftex-docstruct-symbol)) 0)] |
3b919c9f | 2426 | ["Restore from File" (reftex-access-parse-file 'restore) t]) |
2faef409 | 2427 | ("Global Actions" |
baec1250 KH |
2428 | ["Search Whole Document" reftex-search-document t] |
2429 | ["Replace in Document" reftex-query-replace-document t] | |
2430 | ["Grep on Document" reftex-grep-document t] | |
1c25ed90 | 2431 | "--" |
3b919c9f | 2432 | ["Goto Label" reftex-goto-label t] |
baec1250 | 2433 | ["Find Duplicate Labels" reftex-find-duplicate-labels t] |
2faef409 | 2434 | ["Change Label and Refs" reftex-change-label t] |
b849548d | 2435 | ["Renumber Simple Labels" reftex-renumber-simple-labels t] |
1c25ed90 | 2436 | "--" |
1c25ed90 CD |
2437 | ["Create TAGS File" reftex-create-tags-file t] |
2438 | "--" | |
f9ad2e24 | 2439 | ["Save Document" reftex-save-all-document-buffers t]) |
1c25ed90 | 2440 | "--" |
b849548d | 2441 | ("Options" |
921759ee CD |
2442 | "PARSER" |
2443 | ["Partial Scans" | |
2444 | (setq reftex-enable-partial-scans (not reftex-enable-partial-scans)) | |
2445 | :style toggle :selected reftex-enable-partial-scans] | |
2446 | ["Auto-Save Parse Info" | |
2447 | (setq reftex-save-parse-info (not reftex-save-parse-info)) | |
2448 | :style toggle :selected reftex-save-parse-info] | |
1c25ed90 | 2449 | "--" |
3b919c9f CD |
2450 | "TOC RECENTER" |
2451 | ["Automatic Recenter" reftex-toggle-auto-toc-recenter | |
2452 | :style toggle :selected reftex-toc-auto-recenter-timer] | |
2453 | "--" | |
921759ee CD |
2454 | "CROSSREF INFO" |
2455 | ["Automatic Info" reftex-toggle-auto-view-crossref | |
2456 | :style toggle :selected reftex-auto-view-crossref-timer] | |
2457 | ["...in Echo Area" (setq reftex-auto-view-crossref t) | |
2458 | :style radio :selected (eq reftex-auto-view-crossref t)] | |
2459 | ["...in Other Window" (setq reftex-auto-view-crossref 'window) | |
2460 | :style radio :selected (eq reftex-auto-view-crossref 'window)] | |
1c25ed90 | 2461 | "--" |
921759ee CD |
2462 | "MISC" |
2463 | ["AUC TeX Interface" reftex-toggle-plug-into-AUCTeX | |
2464 | :style toggle :selected reftex-plug-into-AUCTeX]) | |
2465 | ("Reference Style" | |
1c25ed90 CD |
2466 | ["Default" (setq reftex-vref-is-default nil |
2467 | reftex-fref-is-default nil) | |
12a382d4 | 2468 | :style radio :selected (not (or reftex-vref-is-default |
1c25ed90 CD |
2469 | reftex-fref-is-default))] |
2470 | ["Varioref" (setq reftex-vref-is-default t | |
2471 | reftex-fref-is-default nil) | |
2472 | :style radio :selected reftex-vref-is-default] | |
2473 | ["Fancyref" (setq reftex-fref-is-default t | |
2474 | reftex-vref-is-default nil) | |
2475 | :style radio :selected reftex-fref-is-default]) | |
921759ee CD |
2476 | ("Citation Style" |
2477 | ,@(mapcar | |
1c25ed90 CD |
2478 | (lambda (x) |
2479 | (vector | |
2480 | (capitalize (symbol-name (car x))) | |
2481 | (list 'reftex-set-cite-format (list 'quote (car x))) | |
2482 | :style 'radio :selected | |
2483 | (list 'eq (list 'reftex-get-cite-format) (list 'quote (car x))))) | |
921759ee | 2484 | reftex-cite-format-builtin) |
1c25ed90 | 2485 | "--" |
921759ee CD |
2486 | "Sort Database Matches" |
2487 | ["Not" (setq reftex-sort-bibtex-matches nil) | |
2488 | :style radio :selected (eq reftex-sort-bibtex-matches nil)] | |
2489 | ["by Author" (setq reftex-sort-bibtex-matches 'author) | |
2490 | :style radio :selected (eq reftex-sort-bibtex-matches 'author)] | |
2491 | ["by Year" (setq reftex-sort-bibtex-matches 'year) | |
2492 | :style radio :selected (eq reftex-sort-bibtex-matches 'year)] | |
2493 | ["by Year, reversed" (setq reftex-sort-bibtex-matches 'reverse-year) | |
2494 | :style radio :selected (eq reftex-sort-bibtex-matches 'reverse-year)]) | |
1c25ed90 CD |
2495 | ("Index Style" |
2496 | ,@(mapcar | |
2497 | (lambda (x) | |
2498 | (vector | |
2499 | (capitalize (symbol-name (car x))) | |
2500 | (list 'reftex-add-index-macros (list 'list (list 'quote (car x)))) | |
2501 | :style 'radio :selected | |
2502 | (list 'memq (list 'quote (car x)) | |
12a382d4 | 2503 | (list 'get 'reftex-docstruct-symbol |
1c25ed90 CD |
2504 | (list 'quote 'reftex-index-macros-style))))) |
2505 | reftex-index-macros-builtin)) | |
3b919c9f CD |
2506 | "--" |
2507 | ["Reset RefTeX Mode" reftex-reset-mode t] | |
1c25ed90 | 2508 | "--" |
2faef409 | 2509 | ("Customize" |
f9ad2e24 | 2510 | ["Browse RefTeX Group" reftex-customize t] |
1c25ed90 | 2511 | "--" |
12a382d4 | 2512 | ["Build Full Customize Menu" reftex-create-customize-menu |
2faef409 | 2513 | (fboundp 'customize-menu-create)]) |
2faef409 RS |
2514 | ("Documentation" |
2515 | ["Info" reftex-info t] | |
2516 | ["Commentary" reftex-show-commentary t]))) | |
a7ec1775 | 2517 | |
b849548d CD |
2518 | (defun reftex-customize () |
2519 | "Call the customize function with reftex as argument." | |
2520 | (interactive) | |
2521 | (customize-browse 'reftex)) | |
2522 | ||
2523 | (defun reftex-create-customize-menu () | |
2524 | "Create a full customization menu for RefTeX, insert it into the menu." | |
2525 | (interactive) | |
2526 | (if (fboundp 'customize-menu-create) | |
2527 | (progn | |
12a382d4 | 2528 | (easy-menu-change |
b849548d CD |
2529 | '("Ref") "Customize" |
2530 | `(["Browse RefTeX group" reftex-customize t] | |
1c25ed90 | 2531 | "--" |
b849548d CD |
2532 | ,(customize-menu-create 'reftex) |
2533 | ["Set" Custom-set t] | |
2534 | ["Save" Custom-save t] | |
2535 | ["Reset to Current" Custom-reset-current t] | |
2536 | ["Reset to Saved" Custom-reset-saved t] | |
2537 | ["Reset to Standard Settings" Custom-reset-standard t])) | |
2538 | (message "\"Ref\"-menu now contains full customization menu")) | |
2539 | (error "Cannot expand menu (outdated version of cus-edit.el)"))) | |
2540 | ||
2541 | (defun reftex-show-commentary () | |
2542 | "Use the finder to view the file documentation from `reftex.el'." | |
2543 | (interactive) | |
2544 | (require 'finder) | |
2545 | (finder-commentary "reftex.el")) | |
2546 | ||
7c4d13cc CD |
2547 | (defun reftex-info (&optional node) |
2548 | "Read documentation for RefTeX in the info system. | |
2549 | With optional NODE, go directly to that node." | |
b849548d CD |
2550 | (interactive) |
2551 | (require 'info) | |
7c4d13cc | 2552 | (Info-goto-node (format "(reftex)%s" (or node "")))) |
b849548d | 2553 | |
b849548d CD |
2554 | ;;; Install the kill-buffer and kill-emacs hooks ------------------------------ |
2555 | ||
2556 | (add-hook 'kill-buffer-hook 'reftex-kill-buffer-hook) | |
2557 | (add-hook 'kill-emacs-hook 'reftex-kill-emacs-hook) | |
2558 | ||
a7ec1775 RS |
2559 | ;;; Run Hook ------------------------------------------------------------------ |
2560 | ||
2561 | (run-hooks 'reftex-load-hook) | |
2562 | ||
2563 | ;;; That's it! ---------------------------------------------------------------- | |
2564 | ||
6b94c6ad | 2565 | (setq reftex-tables-dirty t) ; in case this file is evaluated by hand |
12a382d4 | 2566 | (provide 'reftex) |
396e0b08 | 2567 | |
a7ec1775 RS |
2568 | ;;;============================================================================ |
2569 | ||
c52bdfca | 2570 | ;;; reftex.el ends here |