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