scheme interaction mode
[bpt/emacs.git] / lisp / textmodes / reftex.el
CommitLineData
3afbc435 1;;; reftex.el --- minor mode for doing \label, \ref, \cite, \index in LaTeX
ba318903 2;; Copyright (C) 1997-2000, 2003-2014 Free Software Foundation, Inc.
a7ec1775 3
6fbeb429 4;; Author: Carsten Dominik <dominik@science.uva.nl>
ce545621 5;; Maintainer: auctex-devel@gnu.org
3afbc435 6;; Keywords: tex
a7ec1775 7
2b12cfbf 8;; This file is part of GNU Emacs.
a7ec1775 9
1fecc8fe 10;; GNU Emacs is free software: you can redistribute it and/or modify
a7ec1775 11;; it under the terms of the GNU General Public License as published by
1fecc8fe
GM
12;; the Free Software Foundation, either version 3 of the License, or
13;; (at your option) any later version.
a7ec1775
RS
14
15;; GNU Emacs is distributed in the hope that it will be useful,
16;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18;; GNU General Public License for more details.
19
20;; You should have received a copy of the GNU General Public License
1fecc8fe 21;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
a7ec1775 22
a7ec1775 23;;; Commentary:
4f595e15 24
1c25ed90
CD
25;; RefTeX is a minor mode with distinct support for \ref, \label, \cite,
26;; and \index commands in (multi-file) LaTeX documents.
f9ad2e24
CD
27;; - A table of contents provides easy access to any part of a document.
28;; - Labels are created semi-automatically.
29;; - Definition context of labels is provided when creating a reference.
30;; - Citations are simplified with efficient database lookup.
7c4d13cc 31;; - Text phrases can be collected in a file, for later global indexing.
1c25ed90 32;; - The index preview buffer helps to check and edit index entries.
a7ec1775 33;;
4f595e15 34;; There is an extensive Texinfo document describing RefTeX in detail.
f9ad2e24 35;; One way to view this documentation is `M-x reftex-info RET'.
a7ec1775 36;;
a6611c0d 37;; The documentation in various formats is also available at
396e0b08 38;;
4f595e15 39;; http://www.gnu.org/software/auctex/manual/reftex.index.html
a7ec1775 40;;
f9ad2e24
CD
41;; RefTeX is bundled with Emacs and available as a plug-in package for
42;; XEmacs 21.x. If you need to install it yourself, you can find a
43;; distribution at
a7ec1775 44;;
4f595e15 45;; http://www.gnu.org/software/auctex/reftex.html
2faef409 46;;
4f595e15
RA
47;; RefTeX was written by Carsten Dominik <dominik@science.uva.nl> with
48;; contributions from Stephen Eglen. It is currently maintained by
49;; the AUCTeX project.
50
a7ec1775
RS
51;;; Code:
52
396e0b08
KH
53(eval-when-compile (require 'cl))
54
a7ec1775 55;; Stuff that needs to be there when we use defcustom
a7ec1775
RS
56(require 'custom)
57
729f1525
DN
58(require 'easymenu)
59
a7ec1775
RS
60(defvar reftex-tables-dirty t
61 "Flag showing if tables need to be re-computed.")
62
3666daf6
CD
63(eval-and-compile
64 (defun reftex-set-dirty (symbol value)
65 (setq reftex-tables-dirty t)
66 (set symbol value)))
67
68
4f595e15 69;; Configuration variables
1c25ed90 70(require 'reftex-vars)
a7ec1775 71
a7ec1775 72
41116c5a
GM
73;;; Autoloads - see end for automatic autoloads
74
75;; We autoload tons of functions from these files, but some have
76;; a single function that needs to be globally autoloaded.
77;; The alternative is to use a Makefile rule + distinct autoload
78;; cookie (eg ;;;###reftex-autoload) for internal autoloads,
79;; as eg calendar/ does. But that seemed like overkill for 4 functions.
80
81;;;###autoload(autoload 'reftex-citation "reftex-cite" nil t)
82;;;###autoload(autoload 'reftex-all-document-files "reftex-parse")
83;;;###autoload(autoload 'reftex-isearch-minor-mode "reftex-global" nil t)
84;;;###autoload(autoload 'reftex-index-phrases-mode "reftex-index" nil t)
85
86;; Generated functions.
91dd4dc4
DN
87(autoload 'reftex-varioref-vref "reftex-ref"
88 "Make a varioref reference." t)
89(autoload 'reftex-fancyref-fref "reftex-ref"
90 "Make a fancyref \\fref reference." t)
91(autoload 'reftex-fancyref-Fref "reftex-ref"
92 "Make a fancyref \\Fref reference." t)
4f595e15
RA
93
94;;; =========================================================================
95;;;
96;;; Define the formal stuff for a minor mode named RefTeX.
97;;;
98
99(defconst reftex-version emacs-version
100 "Version string for RefTeX.")
101
102(defvar reftex-mode-map (make-sparse-keymap)
103 "Keymap for RefTeX mode.")
104
105(defvar reftex-mode-menu nil)
106(defvar reftex-syntax-table nil)
107(defvar reftex-syntax-table-for-bib nil)
108
109(unless reftex-syntax-table
110 (setq reftex-syntax-table (copy-syntax-table))
111 (modify-syntax-entry ?\( "." reftex-syntax-table)
112 (modify-syntax-entry ?\) "." reftex-syntax-table))
113
114(unless reftex-syntax-table-for-bib
115 (setq reftex-syntax-table-for-bib (copy-syntax-table))
116 (modify-syntax-entry ?\' "." reftex-syntax-table-for-bib)
117 (modify-syntax-entry ?\" "." reftex-syntax-table-for-bib)
118 (modify-syntax-entry ?\[ "." reftex-syntax-table-for-bib)
119 (modify-syntax-entry ?\] "." reftex-syntax-table-for-bib))
120
121;; The following definitions are out of place, but I need them here
122;; to make the compilation of reftex-mode not complain.
123(defvar reftex-auto-view-crossref-timer nil
124 "The timer used for auto-view-crossref.")
125(defvar reftex-toc-auto-recenter-timer nil
126 "The idle timer used to recenter the toc window.")
127
a7ec1775
RS
128;;;###autoload
129(defun turn-on-reftex ()
b849548d 130 "Turn on RefTeX mode."
a7ec1775
RS
131 (reftex-mode t))
132
56eb0904
SM
133(put 'reftex-mode :included '(memq major-mode '(latex-mode tex-mode)))
134(put 'reftex-mode :menu-tag "RefTeX Mode")
a7ec1775 135;;;###autoload
56eb0904 136(define-minor-mode reftex-mode
4f595e15 137 "Minor mode with distinct support for \\label, \\ref and \\cite in LaTeX.
a7ec1775 138
1c25ed90
CD
139\\<reftex-mode-map>A Table of Contents of the entire (multifile) document with browsing
140capabilities is available with `\\[reftex-toc]'.
141
a7ec1775
RS
142Labels can be created with `\\[reftex-label]' and referenced with `\\[reftex-reference]'.
143When referencing, you get a menu with all labels of a given type and
c52bdfca 144context of the label definition. The selected label is inserted as a
fba437e6 145\\ref macro.
a7ec1775 146
396e0b08 147Citations can be made with `\\[reftex-citation]' which will use a regular expression
a7ec1775 148to pull out a *formatted* list of articles from your BibTeX
c52bdfca 149database. The selected citation is inserted as a \\cite macro.
a7ec1775 150
1c25ed90
CD
151Index entries can be made with `\\[reftex-index-selection-or-word]' which indexes the word at point
152or the current selection. More general index entries are created with
153`\\[reftex-index]'. `\\[reftex-display-index]' displays the compiled index.
a7ec1775 154
c52bdfca 155Most command have help available on the fly. This help is accessed by
a7ec1775
RS
156pressing `?' to any prompt mentioning this feature.
157
f9ad2e24
CD
158Extensive documentation about RefTeX is available in Info format.
159You can view this information with `\\[reftex-info]'.
c52bdfca 160
a7ec1775 161\\{reftex-mode-map}
396e0b08
KH
162Under X, these and other functions will also be available as `Ref' menu
163on the menu bar.
a7ec1775
RS
164
165------------------------------------------------------------------------------"
56eb0904 166 :lighter " Ref" :keymap reftex-mode-map
a7ec1775
RS
167 (if reftex-mode
168 (progn
3666daf6 169 ;; Mode was turned on
396e0b08 170 (easy-menu-add reftex-mode-menu)
3666daf6
CD
171 (and reftex-plug-into-AUCTeX
172 (reftex-plug-into-AUCTeX))
173 (unless (get 'reftex-auto-view-crossref 'initialized)
174 (and reftex-auto-view-crossref
175 (reftex-toggle-auto-view-crossref))
176 (put 'reftex-auto-view-crossref 'initialized t))
177 (unless (get 'reftex-auto-recenter-toc 'initialized)
178 (and (eq reftex-auto-recenter-toc t)
179 (reftex-toggle-auto-toc-recenter))
180 (put 'reftex-auto-recenter-toc 'initialized t))
181
182 ;; Prepare the special syntax tables.
183 (setq reftex-syntax-table (copy-syntax-table (syntax-table)))
184 (modify-syntax-entry ?\( "." reftex-syntax-table)
185 (modify-syntax-entry ?\) "." reftex-syntax-table)
a2e43f12 186
4f595e15 187 (setq reftex-syntax-table-for-bib (copy-syntax-table))
3666daf6
CD
188 (modify-syntax-entry ?\' "." reftex-syntax-table-for-bib)
189 (modify-syntax-entry ?\" "." reftex-syntax-table-for-bib)
190 (modify-syntax-entry ?\[ "." reftex-syntax-table-for-bib)
e2cb57f5
VB
191 (modify-syntax-entry ?\] "." reftex-syntax-table-for-bib)
192
193 (run-hooks 'reftex-mode-hook))
f9ad2e24 194 ;; Mode was turned off
a7ec1775 195 (easy-menu-remove reftex-mode-menu)))
396e0b08 196
1c25ed90
CD
197(defvar reftex-docstruct-symbol)
198(defun reftex-kill-buffer-hook ()
199 "Save RefTeX's parse file for this buffer if the information has changed."
200 ;; Save the parsing information if it was modified.
201 ;; This function should be installed in `kill-buffer-hook'.
845fc5e5 202 ;; We are careful to make sure nothing goes wrong in this function.
1c25ed90 203 (when (and (boundp 'reftex-mode) reftex-mode
3666daf6
CD
204 (boundp 'reftex-save-parse-info) reftex-save-parse-info
205 (boundp 'reftex-docstruct-symbol) reftex-docstruct-symbol
206 (symbol-value reftex-docstruct-symbol)
207 (get reftex-docstruct-symbol 'modified))
1c25ed90
CD
208 ;; Write the file.
209 (condition-case nil
3666daf6 210 (reftex-access-parse-file 'write)
1c25ed90
CD
211 (error nil))))
212
213(defun reftex-kill-emacs-hook ()
214 "Call `reftex-kill-buffer-hook' on all buffers."
215 ;; This function should be installed in `kill-emacs-hook'.
216 (save-excursion
217 (mapcar (lambda (buf)
3666daf6
CD
218 (set-buffer buf)
219 (reftex-kill-buffer-hook))
220 (buffer-list))))
1c25ed90 221
b849548d 222;;; =========================================================================
c52bdfca
RS
223;;;
224;;; Silence warnings about variables in other packages.
0c86715d
DN
225(defvar TeX-master)
226(defvar LaTeX-section-hook)
227(defvar LaTeX-label-function)
228(defvar tex-main-file)
229(defvar outline-minor-mode)
230(defvar font-lock-mode)
231(defvar font-lock-keywords)
232(defvar font-lock-fontify-region-function)
c52bdfca 233
b849548d 234;;; =========================================================================
a7ec1775 235;;;
b849548d 236;;; Multibuffer Variables
a7ec1775 237;;;
56eb0904
SM
238;; Technical notes: These work as follows: We keep just one list
239;; of labels for each master file - this can save a lot of memory.
240;; `reftex-master-index-list' is an alist which connects the true file name
241;; of each master file with the symbols holding the information on that
242;; document. Each buffer has local variables which point to these symbols.
a7ec1775 243
a7ec1775
RS
244;; List of variables which handle the multifile stuff.
245;; This list is used to tie, untie, and reset these symbols.
246(defconst reftex-multifile-symbols
396e0b08 247 '(reftex-docstruct-symbol))
a7ec1775 248
c52bdfca 249;; Alist connecting master file names with the corresponding lisp symbols.
a7ec1775
RS
250(defvar reftex-master-index-list nil)
251
c52bdfca 252;; Last index used for a master file.
a7ec1775
RS
253(defvar reftex-multifile-index 0)
254
a7ec1775 255;; Variable holding the symbol with the label list of the document.
396e0b08
KH
256(defvar reftex-docstruct-symbol nil)
257(make-variable-buffer-local 'reftex-docstruct-symbol)
a7ec1775
RS
258
259(defun reftex-next-multifile-index ()
260 ;; Return the next free index for multifile symbols.
396e0b08 261 (incf reftex-multifile-index))
a7ec1775
RS
262
263(defun reftex-tie-multifile-symbols ()
4f595e15
RA
264 "Tie the buffer-local symbols to globals connected with the master file.
265If the symbols for the current master file do not exist, they are created."
a7ec1775
RS
266 (let* ((master (file-truename (reftex-TeX-master-file)))
267 (index (assoc master reftex-master-index-list))
268 (symlist reftex-multifile-symbols)
b849548d 269 symbol symname newflag)
c52bdfca 270 ;; Find the correct index.
a7ec1775 271 (if index
4f595e15 272 ;; Symbols do exist
a7ec1775 273 (setq index (cdr index))
c52bdfca 274 ;; Get a new index and add info to the alist.
a7ec1775 275 (setq index (reftex-next-multifile-index)
396e0b08
KH
276 newflag t)
277 (push (cons master index) reftex-master-index-list))
a7ec1775 278
c52bdfca 279 ;; Get/create symbols and tie them.
a7ec1775
RS
280 (while symlist
281 (setq symbol (car symlist)
282 symlist (cdr symlist)
283 symname (symbol-name symbol))
284 (set symbol (intern (concat symname "-" (int-to-string index))))
921759ee 285 (put (symbol-value symbol) :master-index index)
c52bdfca 286 ;; Initialize if new symbols.
1c25ed90 287 (when newflag
3666daf6 288 (set (symbol-value symbol) nil)
4f595e15
RA
289 (put (symbol-value symbol) 'reftex-index-macros-style '(default))
290 (put (symbol-value symbol) 'reftex-ref-style-list
291 reftex-ref-style-default-list)))
a7ec1775 292
c52bdfca 293 ;; Return t if the symbols did already exist, nil when we've made them.
a7ec1775
RS
294 (not newflag)))
295
296(defun reftex-untie-multifile-symbols ()
4f595e15 297 "Remove ties from multifile symbols, so that next use makes new ones."
a7ec1775
RS
298 (let ((symlist reftex-multifile-symbols)
299 (symbol nil))
300 (while symlist
301 (setq symbol (car symlist)
302 symlist (cdr symlist))
303 (set symbol nil))))
304
305(defun reftex-TeX-master-file ()
306 ;; Return the name of the master file associated with the current buffer.
1c25ed90 307 ;; When AUCTeX is loaded, we will use it's more sophisticated method.
025bb635
RS
308 ;; We also support the default TeX and LaTeX modes by checking for a
309 ;; variable tex-main-file.
a7ec1775
RS
310 (let
311 ((master
312 (cond
c52bdfca 313 ((fboundp 'TeX-master-file) ; AUCTeX is loaded. Use its mechanism.
a2e43f12 314 (condition-case nil
3666daf6 315 (TeX-master-file t)
de74083d 316 (error (buffer-file-name))))
3666daf6 317 ((fboundp 'tex-main-file) (tex-main-file)) ; Emacs LaTeX mode
c7015153 318 ((boundp 'TeX-master) ; The variable is defined - let's use it.
a7ec1775
RS
319 (cond
320 ((eq TeX-master t)
321 (buffer-file-name))
322 ((eq TeX-master 'shared)
323 (setq TeX-master (read-file-name "Master file: "
324 nil nil t nil)))
325 (TeX-master)
326 (t
327 (setq TeX-master (read-file-name "Master file: "
328 nil nil t nil)))))
329 ((boundp 'tex-main-file)
c52bdfca 330 ;; This is the variable from the default TeX modes.
a7ec1775
RS
331 (cond
332 ((stringp tex-main-file)
333 ;; ok, this must be it
334 tex-main-file)
335 (t
c52bdfca 336 ;; In this case, the buffer is its own master.
a7ec1775
RS
337 (buffer-file-name))))
338 (t
c52bdfca 339 ;; Know nothing about master file. Assume this is a master file.
a7ec1775
RS
340 (buffer-file-name)))))
341 (cond
342 ((null master)
3666daf6 343 (error "Need a filename for this buffer, please save it first"))
a7ec1775
RS
344 ((or (file-exists-p (concat master ".tex"))
345 (reftex-get-buffer-visiting (concat master ".tex")))
346 ;; Ahh, an extra .tex was missing...
347 (setq master (concat master ".tex")))
a6611c0d
CD
348 ((or (file-exists-p master)
349 (reftex-get-buffer-visiting master))
350 ;; We either see the file, or have a buffer on it. OK.
351 )
a7ec1775 352 (t
b849548d 353 ;; Use buffer file name.
7b07114a 354 (setq master (buffer-file-name))))
a7ec1775
RS
355 (expand-file-name master)))
356
1c25ed90
CD
357(defun reftex-is-multi ()
358 ;; Tell if this is a multifile document. When not sure, say yes.
359 (let ((entry (assq 'is-multi (symbol-value reftex-docstruct-symbol))))
360 (if entry
361 (nth 1 entry)
362 t)))
363
364(defun reftex-set-cite-format (value)
365 "Set the document-local value of `reftex-cite-format'.
366When such a value exists, it overwrites the setting given with
367`reftex-cite-format'. See the documentation of `reftex-cite-format'
368for possible values. This function should be used from AUCTeX style files."
369 (unless reftex-docstruct-symbol
370 (reftex-tie-multifile-symbols))
371 (when (and reftex-docstruct-symbol
3666daf6 372 (symbolp reftex-docstruct-symbol))
1c25ed90
CD
373 (put reftex-docstruct-symbol 'reftex-cite-format value)))
374
375(defun reftex-get-cite-format ()
376 ;; Return the current citation format. Either the document-local value in
377 ;; reftex-cite-format-symbol, or the global value in reftex-cite-format.
378 (if (and reftex-docstruct-symbol
3666daf6
CD
379 (symbolp reftex-docstruct-symbol)
380 (get reftex-docstruct-symbol 'reftex-cite-format))
1c25ed90
CD
381 (get reftex-docstruct-symbol 'reftex-cite-format)
382 reftex-cite-format))
383
384(defun reftex-add-index-macros (entry-list)
385 "Add index macro descriptions to `reftex-index-macros-style'.
386The format of ENTRY-LIST is exactly like `reftex-index-macros'. See there
387for details.
388This function makes it possible to support RefTeX from AUCTeX style files.
389The entries in ENTRY-LIST will be processed after the user settings in
390`reftex-index-entries', and before the defaults. Any changes made to
4f595e15 391`reftex-index-macros-style' will raise a flag to the effect that
1c25ed90
CD
392the label information is recompiled on next use."
393 (unless reftex-docstruct-symbol
394 (reftex-tie-multifile-symbols))
395 (when (and reftex-docstruct-symbol
3666daf6 396 (symbolp reftex-docstruct-symbol))
1c25ed90 397 (let ((list (get reftex-docstruct-symbol 'reftex-index-macros-style))
3666daf6 398 entry changed)
1c25ed90 399 (while entry-list
3666daf6
CD
400 (setq entry (pop entry-list))
401 ;; When it is a symbol, remove all other symbols
402 (and (symbolp entry)
403 (not (memq entry list))
404 (setq list (reftex-remove-symbols-from-list list)))
405 ;; Add to list unless already member
406 (unless (member entry list)
407 (setq reftex-tables-dirty t
408 changed t)
409 (push entry list)))
1c25ed90 410 (when changed
3666daf6 411 (put reftex-docstruct-symbol 'reftex-index-macros-style list)))))
1c25ed90 412
4f595e15
RA
413(defun reftex-ref-style-activate (style)
414 "Activate the referencing style STYLE."
415 (reftex-ref-style-toggle style 'activate))
416
417(defun reftex-ref-style-toggle (style &optional action)
418 "Activate or deactivate the referencing style STYLE.
419With the optional argument ACTION a certain action can be forced.
420The symbol `activate' will activate the style and `deactivate'
421will deactivate it."
422 (unless reftex-docstruct-symbol
423 (reftex-tie-multifile-symbols))
424 (when (and reftex-docstruct-symbol
425 (symbolp reftex-docstruct-symbol))
426 (let ((list (get reftex-docstruct-symbol 'reftex-ref-style-list))
427 changed)
428 (cond ((eq action 'activate)
429 (unless (member style list)
430 (setq reftex-tables-dirty t
431 changed t)
432 (add-to-list 'list style t)))
433 ((eq action 'deactivate)
434 (when (member style list)
435 (setq reftex-tables-dirty t
436 changed t)
96172128 437 (setq list (delete style list))))
4f595e15
RA
438 (t
439 (if (member style list)
440 (delete style list)
441 (add-to-list 'list style t))
442 (setq reftex-tables-dirty t
443 changed t)))
444 (when changed
445 (put reftex-docstruct-symbol 'reftex-ref-style-list list)))))
446
447(defun reftex-ref-style-list ()
448 "Return the list of referencing styles to be active at the moment."
449 ;; Initialize the value of `reftex-ref-style-list' and tie it to the
450 ;; docstruct symbol if necessary.
451 (unless reftex-docstruct-symbol
452 (reftex-tie-multifile-symbols))
453 (if (and reftex-docstruct-symbol
454 (symbolp reftex-docstruct-symbol)
455 (get reftex-docstruct-symbol 'reftex-ref-style-list))
456 (get reftex-docstruct-symbol 'reftex-ref-style-list)
457 reftex-ref-style-default-list))
458
b849548d
CD
459;;; =========================================================================
460;;;
1c25ed90 461;;; Functions to compile the tables, reset the mode etc.
b849548d
CD
462
463;; The following constants are derived from `reftex-label-alist'.
464
465;; Prompt used for label type queries directed to the user.
6fbeb429 466(defvar reftex-type-query-prompt nil)
b849548d
CD
467
468;; Help string for label type queries.
6fbeb429 469(defvar reftex-type-query-help nil)
b849548d
CD
470
471;; Alist relating label type to reference format.
6fbeb429 472(defvar reftex-typekey-to-format-alist nil)
b849548d 473
1c25ed90 474;; Alist relating label type to label prefix.
6fbeb429 475(defvar reftex-typekey-to-prefix-alist nil)
b849548d
CD
476
477;; Alist relating environments or macros to label type and context regexp.
6fbeb429 478(defvar reftex-env-or-mac-alist nil)
b849548d 479
1c25ed90 480;; List of special environment parser functions
6fbeb429 481(defvar reftex-special-env-parsers nil)
1c25ed90 482
b849548d 483;; List of macros carrying a label.
6fbeb429 484(defvar reftex-label-mac-list nil)
b849548d
CD
485
486;; List of environments carrying a label.
6fbeb429 487(defvar reftex-label-env-list nil)
b849548d
CD
488
489;; List of all typekey letters in use.
6fbeb429 490(defvar reftex-typekey-list nil)
b849548d
CD
491
492;; Alist relating magic words to a label type.
6fbeb429 493(defvar reftex-words-to-typekey-alist nil)
f3c18bd0
CD
494;; Alist relating label prefixes to a label type.
495(defvar reftex-prefix-to-typekey-alist nil)
b849548d
CD
496
497;; The last list-of-labels entry used in a reference.
498(defvar reftex-last-used-reference (list nil nil nil nil))
499
1c25ed90 500;; Alist relating index macros to other info.
6fbeb429 501(defvar reftex-key-to-index-macro-alist nil)
1c25ed90 502;; Prompt for index macro queries
6fbeb429 503(defvar reftex-query-index-macro-prompt nil)
1c25ed90 504;; Help string for index macro queries
6fbeb429 505(defvar reftex-query-index-macro-help nil)
1c25ed90 506
b849548d 507;; The message when follow-mode is suspended
6fbeb429 508(defvar reftex-no-follow-message
b849548d 509 "No follow-mode into unvisited file. Press SPC to visit it.")
6fbeb429 510(defvar reftex-no-info-message
b849548d
CD
511 "%s: info not available, use `\\[reftex-view-crossref]' to get it.")
512
b849548d
CD
513;; Global variables used for communication between functions.
514(defvar reftex-default-context-position nil)
515(defvar reftex-location-start nil)
516(defvar reftex-call-back-to-this-buffer nil)
517(defvar reftex-select-return-marker (make-marker))
518(defvar reftex-active-toc nil)
519(defvar reftex-tex-path nil)
520(defvar reftex-bib-path nil)
1c25ed90 521(defvar reftex-select-marked nil)
b849548d
CD
522(defvar reftex-last-follow-point nil)
523(defvar reftex-latex-syntax-table nil)
524(defvar reftex-prefix nil)
525(defvar reftex-section-levels-all nil)
526(defvar reftex-buffers-with-changed-invisibility nil)
f9ad2e24 527(defvar reftex-callback-fwd t)
1c25ed90
CD
528(defvar reftex-last-toc-master nil
529 "Stores the name of the tex file that `reftex-toc' was last run on.")
1c25ed90
CD
530;; Marker for return point from recursive edit
531(defvar reftex-recursive-edit-marker (make-marker))
b849548d
CD
532
533;; List of buffers created temporarily for lookup, which should be killed.
534(defvar reftex-buffers-to-kill nil)
535
536;; Regexp to find anything.
537(defvar reftex-section-regexp nil)
538(defvar reftex-section-or-include-regexp nil)
1c25ed90
CD
539(defvar reftex-index-macro-regexp nil)
540(defvar reftex-index-level-re nil)
541(defvar reftex-index-key-end-re nil)
542(defvar reftex-find-index-entry-regexp-format nil)
b849548d 543(defvar reftex-everything-regexp nil)
1c25ed90
CD
544(defvar reftex-everything-regexp-no-index nil)
545(defvar reftex-index-re nil)
921759ee 546(defvar reftex-find-citation-regexp-format
d8fb2015 547 "\\\\\\([a-zA-Z]*cite[*a-zA-Z]*\\*?\\|bibentry\\)\\(\\[[^]]*\\]\\|{[^}]*}\\)*{\\([^}]*,\\)?\\(%s\\)[},]")
921759ee 548(defvar reftex-find-reference-format
1c25ed90 549 "\\\\\\(ref[a-zA-Z]*\\|[a-zA-Z]*ref\\(range\\)?\\)\\*?\\(\\[[^]]*\\]\\|{[^}]*}\\)*{\\(%s\\)}")
921759ee 550(defvar reftex-macros-with-labels nil)
1c25ed90
CD
551(defvar reftex-macros-with-index nil)
552(defvar reftex-index-macro-alist nil)
b849548d
CD
553(defvar reftex-find-label-regexp-format nil)
554(defvar reftex-find-label-regexp-format2 nil)
555
e2cb57f5
VB
556;; Constants for making RefTeX open to Texinfo hooking
557(defvar reftex-section-pre-regexp "\\\\")
558;; Including `\' as a character to be matched at the end of the regexp
559;; will allow stuff like \begin{foo}\label{bar} to be matched. This
560;; will make the parser to advance one char too much. Therefore
561;; `reftex-parse-from-file' will step one char back if a section is
562;; found.
563(defvar reftex-section-post-regexp "\\*?\\(\\[[^]]*\\]\\)?[[{ \t\r\n\\]")
564(defvar reftex-section-info-function 'reftex-section-info)
565
b849548d
CD
566(defvar reftex-memory nil
567 "Memorizes old variable values to indicate changes in these variables.")
568
1c25ed90
CD
569;; A list of all variables in the cache.
570;; The cache is used to save the compiled versions of some variables.
a2e43f12 571(defconst reftex-cache-variables
1c25ed90 572 '(reftex-memory ;; This MUST ALWAYS be the first!
a2e43f12 573
1c25ed90
CD
574 ;; Outline
575 reftex-section-levels-all
b849548d 576
1c25ed90 577 ;; Labels
a2e43f12 578 reftex-env-or-mac-alist
1c25ed90
CD
579 reftex-special-env-parsers
580 reftex-macros-with-labels
581 reftex-label-mac-list
582 reftex-label-env-list
583 reftex-typekey-list
584 reftex-typekey-to-format-alist
585 reftex-typekey-to-prefix-alist
586 reftex-words-to-typekey-alist
f3c18bd0 587 reftex-prefix-to-typekey-alist
1c25ed90
CD
588 reftex-type-query-prompt
589 reftex-type-query-help
590
591 ;; Index
592 reftex-index-macro-alist
593 reftex-macros-with-index
594 reftex-query-index-macro-prompt
a2e43f12 595 reftex-query-index-macro-help
1c25ed90
CD
596 reftex-key-to-index-macro-alist
597
598 ;; Regular expressions
599 reftex-section-regexp
600 reftex-section-or-include-regexp
601 reftex-index-re
602 reftex-everything-regexp
603 reftex-everything-regexp-no-index
604 reftex-find-label-regexp-format
605 reftex-find-label-regexp-format2
606 reftex-find-index-entry-regexp-format
607))
b849548d 608
1c25ed90
CD
609(defun reftex-ensure-compiled-variables ()
610 ;; Recompile the label alist when necessary
611 (let* ((mem reftex-memory)
3666daf6
CD
612 (cache (get reftex-docstruct-symbol 'reftex-cache))
613 (cmem (car cache))
614 (alist reftex-label-alist)
615 (levels (get reftex-docstruct-symbol 'reftex-section-levels))
616 (style (get reftex-docstruct-symbol 'reftex-label-alist-style))
617 (default reftex-default-label-alist-entries)
618 (index reftex-index-macros)
619 (istyle (get reftex-docstruct-symbol 'reftex-index-macros-style)))
1c25ed90
CD
620 (cond
621 (reftex-tables-dirty (reftex-compile-variables))
622 ((and (eq alist (nth 0 mem))
3666daf6
CD
623 (eq levels (nth 1 mem))
624 (eq style (nth 2 mem))
625 (eq default (nth 3 mem))
626 (eq index (nth 4 mem))
627 (eq istyle (nth 5 mem)))) ;; everything is OK
1c25ed90 628 ((and (eq alist (nth 0 cmem))
3666daf6
CD
629 (eq levels (nth 1 cmem))
630 (eq style (nth 2 cmem))
631 (eq default (nth 2 cmem))
632 (eq index (nth 4 cmem))
633 (eq istyle (nth 5 cmem)))
1c25ed90
CD
634 ;; restore the cache
635 (message "Restoring cache")
636 (mapcar (lambda (sym) (set sym (pop cache))) reftex-cache-variables))
637 (t (reftex-compile-variables)))))
a7ec1775 638
1c25ed90 639(defun reftex-reset-mode ()
a2e43f12 640 "Reset RefTeX Mode.
1c25ed90
CD
641This will re-compile the configuration information and remove all
642current scanning information and the parse file to enforce a rescan
643on next use."
396e0b08 644 (interactive)
396e0b08 645
1c25ed90
CD
646 ;; Reset the file search path variables
647 (loop for prop in '(status master-dir recursive-path rec-type) do
3666daf6
CD
648 (put 'reftex-tex-path prop nil)
649 (put 'reftex-bib-path prop nil))
396e0b08 650
1c25ed90
CD
651 ;; Kill temporary buffers associated with RefTeX - just in case they
652 ;; were not cleaned up properly
653 (save-excursion
654 (let ((buffer-list '("*RefTeX Help*" "*RefTeX Select*"
3666daf6
CD
655 "*Duplicate Labels*" "*toc*" " *RefTeX-scratch*"))
656 buf)
1c25ed90 657 (while (setq buf (pop buffer-list))
3666daf6
CD
658 (if (get-buffer buf)
659 (kill-buffer buf))))
1c25ed90 660 (reftex-erase-all-selection-and-index-buffers))
396e0b08 661
1c25ed90
CD
662 ;; Make sure the current document will be rescanned soon.
663 (reftex-reset-scanning-information)
396e0b08 664
1c25ed90
CD
665 ;; Remove any parse info file
666 (reftex-access-parse-file 'kill)
396e0b08 667
1c25ed90
CD
668 ;; Plug functions into AUCTeX if the user option says so.
669 (and reftex-plug-into-AUCTeX
670 (reftex-plug-into-AUCTeX))
396e0b08 671
1c25ed90 672 (reftex-compile-variables))
396e0b08 673
3666daf6 674;;;###autoload
1c25ed90
CD
675(defun reftex-reset-scanning-information ()
676 "Reset the symbols containing information from buffer scanning.
677This enforces rescanning the buffer on next use."
678 (if (string= reftex-last-toc-master (reftex-TeX-master-file))
679 (reftex-erase-buffer "*toc*"))
680 (let ((symlist reftex-multifile-symbols)
681 symbol)
682 (while symlist
683 (setq symbol (car symlist)
684 symlist (cdr symlist))
685 (if (and (symbolp (symbol-value symbol))
686 (not (null (symbol-value symbol))))
687 (set (symbol-value symbol) nil)))))
b849548d 688
1c25ed90
CD
689(defun reftex-erase-all-selection-and-index-buffers ()
690 ;; Remove all selection buffers associated with current document.
4b33469e 691 (mapc
1c25ed90
CD
692 (lambda (type)
693 (reftex-erase-buffer (reftex-make-selection-buffer-name type)))
694 reftex-typekey-list)
695 ;; Kill all index buffers
4b33469e 696 (mapc
1c25ed90
CD
697 (lambda (tag)
698 (reftex-kill-buffer (reftex-make-index-buffer-name tag)))
699 (cdr (assoc 'index-tags (symbol-value reftex-docstruct-symbol)))))
396e0b08 700
1c25ed90
CD
701(defun reftex-compile-variables ()
702 ;; Compile the information in reftex-label-alist & Co.
396e0b08 703
1c25ed90 704 (message "Compiling label environment definitions...")
396e0b08 705
1c25ed90
CD
706 ;; Update AUCTeX style information
707 (when (and (featurep 'tex-site) (fboundp 'TeX-update-style))
708 (condition-case nil (TeX-update-style) (error nil)))
396e0b08 709
1c25ed90
CD
710 ;; Record that we have done this, and what we have used.
711 (setq reftex-tables-dirty nil)
a2e43f12 712 (setq reftex-memory
3666daf6
CD
713 (list reftex-label-alist
714 (get reftex-docstruct-symbol 'reftex-section-levels)
715 (get reftex-docstruct-symbol 'reftex-label-alist-style)
716 reftex-default-label-alist-entries
717 reftex-index-macros
718 (get reftex-docstruct-symbol 'reftex-index-macros-style)))
396e0b08 719
1c25ed90
CD
720 ;; Compile information in reftex-label-alist
721 (let ((all (reftex-uniquify-by-car
3666daf6
CD
722 (reftex-splice-symbols-into-list
723 (append reftex-label-alist
724 (get reftex-docstruct-symbol
725 'reftex-label-alist-style)
726 reftex-default-label-alist-entries)
727 reftex-label-alist-builtin)
728 '(nil)))
729 (all-index (reftex-uniquify-by-car
730 (reftex-splice-symbols-into-list
a2e43f12 731 (append reftex-index-macros
3666daf6
CD
732 (get reftex-docstruct-symbol
733 'reftex-index-macros-style)
734 '(default))
735 reftex-index-macros-builtin)))
1c25ed90
CD
736 entry env-or-mac typekeychar typekey prefix context word
737 fmt reffmt labelfmt wordlist qh-list macros-with-labels
738 nargs nlabel opt-args cell sum i
3666daf6 739 macro verify repeat nindex tag key toc-level toc-levels)
b849548d 740
1c25ed90 741 (setq reftex-words-to-typekey-alist nil
a2e43f12 742 reftex-prefix-to-typekey-alist
f3c18bd0 743 '(("sec:" . "s") ("cha:" . "s") ("chap:" . "s"))
1c25ed90
CD
744 reftex-typekey-list nil
745 reftex-typekey-to-format-alist nil
746 reftex-typekey-to-prefix-alist nil
747 reftex-env-or-mac-alist nil
748 reftex-label-env-list nil
749 reftex-label-mac-list nil)
750 (while all
751 (catch 'next-entry
752 (setq entry (car all)
753 env-or-mac (car entry)
754 entry (cdr entry)
755 all (cdr all))
756 (if (null env-or-mac)
757 (setq env-or-mac ""))
758 (if (stringp (car entry))
759 ;; This is before version 2.00 - convert entry to new format
760 ;; This is just to keep old users happy
761 (setq entry (cons (string-to-char (car entry))
762 (cons (concat (car entry) ":")
763 (cdr entry)))))
764 (setq typekeychar (nth 0 entry)
765 typekey (if typekeychar (char-to-string typekeychar) nil)
766 prefix (nth 1 entry)
767 fmt (nth 2 entry)
768 context (nth 3 entry)
7c4d13cc 769 wordlist (nth 4 entry)
3666daf6 770 toc-level (nth 5 entry))
1c25ed90
CD
771 (if (stringp wordlist)
772 ;; This is before version 2.04 - convert to new format
773 (setq wordlist (nthcdr 4 entry)))
b849548d 774
1c25ed90
CD
775 (if (and (stringp fmt)
776 (string-match "@" fmt))
777 ;; Special syntax for specifying a label format
778 (setq fmt (split-string fmt "@+"))
779 (setq fmt (list "\\label{%s}" fmt)))
780 (setq labelfmt (car fmt)
781 reffmt (nth 1 fmt))
3666daf6 782 ;; Note a new typekey
1c25ed90
CD
783 (if typekey
784 (add-to-list 'reftex-typekey-list typekey))
f3c18bd0
CD
785 (if (and typekey prefix
786 (not (assoc prefix reftex-prefix-to-typekey-alist)))
787 (add-to-list 'reftex-prefix-to-typekey-alist
788 (cons prefix typekey)))
1c25ed90
CD
789 (if (and typekey prefix
790 (not (assoc typekey reftex-typekey-to-prefix-alist)))
791 (add-to-list 'reftex-typekey-to-prefix-alist
792 (cons typekey prefix)))
3666daf6 793 ;; Check if this is a macro or environment
1c25ed90 794 (cond
3666daf6
CD
795 ((symbolp env-or-mac)
796 ;; A special parser function
797 (unless (fboundp env-or-mac)
a2e43f12 798 (message "Warning: %s does not seem to be a valid function"
3666daf6 799 env-or-mac))
1c25ed90 800 (setq nargs nil nlabel nil opt-args nil)
3666daf6
CD
801 (add-to-list 'reftex-special-env-parsers env-or-mac)
802 (setq env-or-mac (symbol-name env-or-mac)))
1c25ed90
CD
803 ((string-match "\\`\\\\" env-or-mac)
804 ;; It's a macro
805 (let ((result (reftex-parse-args env-or-mac)))
806 (setq env-or-mac (or (first result) env-or-mac)
3666daf6 807 nargs (second result)
1c25ed90
CD
808 nlabel (third result)
809 opt-args (fourth result))
810 (if nlabel (add-to-list 'macros-with-labels env-or-mac)))
3666daf6 811 (if typekey (add-to-list 'reftex-label-mac-list env-or-mac)))
1c25ed90 812 (t
3666daf6 813 ;; It's an environment
1c25ed90
CD
814 (setq nargs nil nlabel nil opt-args nil)
815 (cond ((string= env-or-mac "any"))
816 ((string= env-or-mac ""))
817 ((string= env-or-mac "section"))
818 (t
7c4d13cc 819 (add-to-list 'reftex-label-env-list env-or-mac)
3666daf6
CD
820 (if toc-level
821 (let ((string (format "begin{%s}" env-or-mac)))
822 (or (assoc string toc-levels)
823 (push (cons string toc-level) toc-levels))))))))
824 ;; Translate some special context cases
825 (when (assq context reftex-default-context-regexps)
a2e43f12
GM
826 (setq context
827 (format
3666daf6
CD
828 (cdr (assq context reftex-default-context-regexps))
829 (regexp-quote env-or-mac))))
830 ;; See if this is the first format for this typekey
1c25ed90
CD
831 (and reffmt
832 (not (assoc typekey reftex-typekey-to-format-alist))
833 (push (cons typekey reffmt) reftex-typekey-to-format-alist))
3666daf6 834 ;; See if this is the first definition for this env-or-mac
1c25ed90
CD
835 (and (not (string= env-or-mac "any"))
836 (not (string= env-or-mac ""))
837 (not (assoc env-or-mac reftex-env-or-mac-alist))
838 (push (list env-or-mac typekey context labelfmt
3666daf6 839 nargs nlabel opt-args)
1c25ed90 840 reftex-env-or-mac-alist))
3666daf6
CD
841 ;; Are the magic words regular expressions? Quote normal words.
842 (if (eq (car wordlist) 'regexp)
843 (setq wordlist (cdr wordlist))
844 (setq wordlist (mapcar 'regexp-quote wordlist)))
845 ;; Remember the first association of each word.
1c25ed90
CD
846 (while (stringp (setq word (pop wordlist)))
847 (or (assoc word reftex-words-to-typekey-alist)
848 (push (cons word typekey) reftex-words-to-typekey-alist)))
849 (cond
850 ((string= "" env-or-mac) nil)
851 ((setq cell (assoc typekey qh-list))
852 (push env-or-mac (cdr cell)))
853 (typekey
854 (push (list typekey env-or-mac) qh-list)))))
2faef409 855
1c25ed90
CD
856 (setq reftex-typekey-to-prefix-alist
857 (nreverse reftex-typekey-to-prefix-alist))
a7ec1775 858
1c25ed90 859 ;; Prepare the typekey query prompt and help string.
a2e43f12 860 (setq qh-list
3666daf6
CD
861 (sort qh-list
862 (lambda (x1 x2)
863 (string< (downcase (car x1)) (downcase (car x2))))))
1c25ed90
CD
864 (setq reftex-type-query-prompt
865 (concat "Label type: ["
866 (mapconcat (lambda(x) (format "%s" (car x)))
867 qh-list "")
868 "]"))
869 ;; In the help string, we need to wrap lines...
870 (setq reftex-type-query-help
a2e43f12 871 (concat
3666daf6
CD
872 "SELECT A LABEL TYPE:\n--------------------\n"
873 (mapconcat
874 (lambda(x)
875 (setq sum 0)
876 (format " [%s] %s"
877 (car x)
878 (mapconcat (lambda(env)
879 (setq sum (+ sum (length env)))
880 (if (< sum 60)
881 env
882 (setq sum 0)
883 (concat "\n " env)))
884 (cdr x) " ")))
885 qh-list "\n")))
a7ec1775 886
1c25ed90
CD
887 ;; Convert magic words to regular expressions. We make regular expressions
888 ;; which allow for some chars from the ref format to be in the buffer.
889 ;; These characters will be seen and removed.
890 (setq reftex-words-to-typekey-alist
a2e43f12 891 (mapcar
3666daf6
CD
892 (lambda (x)
893 (setq word (car x)
894 typekey (cdr x)
895 fmt (cdr (assoc typekey reftex-typekey-to-format-alist)))
896 (setq word (concat "\\W\\(" word "[ \t\n\r]*\\)\\("))
897 (setq i 0)
898 (while (and (< i 10) ; maximum number of format chars allowed
899 (< i (length fmt))
900 (not (member (aref fmt i) '(?%))))
901 (setq word (concat word "\\|" (regexp-quote
902 (substring fmt 0 (1+ i)))))
903 (incf i))
904 (cons (concat word "\\)\\=") typekey))
905 (nreverse reftex-words-to-typekey-alist)))
a7ec1775 906
1c25ed90
CD
907 ;; Parse the index macros
908 (setq reftex-index-macro-alist nil
3666daf6
CD
909 reftex-key-to-index-macro-alist nil
910 reftex-macros-with-index nil)
1c25ed90
CD
911 (while all-index
912 (setq entry (car all-index)
3666daf6
CD
913 macro (car entry)
914 tag (nth 1 entry)
915 key (nth 2 entry)
916 prefix (or (nth 3 entry) "")
917 verify (nth 4 entry)
918 ;; For repeat, we need to be compatible with older code
919 ;; This information used to be given only for the default macro,
920 ;; but later we required to have it for *every* index macro
921 repeat (cond ((> (length entry) 5) (nth 5 entry))
922 ((and (eq key (car reftex-index-default-macro))
923 (> (length reftex-index-default-macro) 2))
924 ;; User has old setting - respect it
925 (nth 2 reftex-index-default-macro))
926 (t t))
927 all-index (cdr all-index))
1c25ed90 928 (let ((result (reftex-parse-args macro)))
3666daf6
CD
929 (setq macro (or (first result) macro)
930 nargs (second result)
931 nindex (third result)
932 opt-args (fourth result))
933 (unless (member macro reftex-macros-with-index)
934 ;; 0 1 2 3 4 5 6 7
935 (push (list macro tag prefix verify nargs nindex opt-args repeat)
936 reftex-index-macro-alist)
937 (or (assoc key reftex-key-to-index-macro-alist)
938 (push (list key macro) reftex-key-to-index-macro-alist))
939 (push macro reftex-macros-with-index))))
1c25ed90
CD
940 ;; Make the prompt and help string for index macros query
941 (setq reftex-key-to-index-macro-alist
3666daf6
CD
942 (sort reftex-key-to-index-macro-alist
943 (lambda (a b) (< (downcase (car a)) (downcase (car b))))))
a2e43f12 944 (setq reftex-query-index-macro-prompt
3666daf6
CD
945 (concat "Index macro: ["
946 (mapconcat (lambda (x) (char-to-string (car x)))
947 reftex-key-to-index-macro-alist "")
948 "]"))
1c25ed90 949 (setq i 0
3666daf6 950 reftex-query-index-macro-help
a2e43f12 951 (concat
3666daf6
CD
952 "SELECT A MACRO:\n---------------\n"
953 (mapconcat
954 (lambda(x)
a2e43f12 955 (format "[%c] %-20.20s%s" (car x) (nth 1 x)
3666daf6
CD
956 (if (= 0 (mod (incf i) 3)) "\n" "")))
957 reftex-key-to-index-macro-alist "")))
2faef409 958
1c25ed90
CD
959 ;; Make the full list of section levels
960 (setq reftex-section-levels-all
3666daf6
CD
961 (append toc-levels
962 (get reftex-docstruct-symbol 'reftex-section-levels)
963 reftex-section-levels))
a7ec1775 964
1c25ed90 965 ;; Calculate the regular expressions
6fbeb429 966 (let* (
3666daf6 967; (wbol "\\(\\`\\|[\n\r]\\)[ \t]*")
4f595e15
RA
968 (wbol "\\(^\\)[ \t]*") ; Need to keep the empty group because
969 ; match numbers are hard coded
86332df2 970 (label-re (concat "\\(?:"
4d9a0979 971 (mapconcat 'identity reftex-label-regexps "\\|")
86332df2 972 "\\)"))
a2e43f12 973 (include-re (concat wbol
3666daf6 974 "\\\\\\("
a2e43f12 975 (mapconcat 'identity
3666daf6
CD
976 reftex-include-file-commands "\\|")
977 "\\)[{ \t]+\\([^} \t\n\r]+\\)"))
978 (section-re
e2cb57f5 979 (concat wbol reftex-section-pre-regexp "\\("
3666daf6
CD
980 (mapconcat (lambda (x) (regexp-quote (car x)))
981 reftex-section-levels-all "\\|")
e2cb57f5 982 "\\)" reftex-section-post-regexp))
3666daf6
CD
983 (appendix-re (concat wbol "\\(\\\\appendix\\)"))
984 (macro-re
985 (if macros-with-labels
986 (concat "\\("
987 (mapconcat 'regexp-quote macros-with-labels "\\|")
988 "\\)[[{]")
989 ""))
990 (index-re
991 (concat "\\("
992 (mapconcat 'regexp-quote reftex-macros-with-index "\\|")
993 "\\)[[{]"))
994 (find-index-re-format
995 (concat "\\("
996 (mapconcat 'regexp-quote reftex-macros-with-index "\\|")
997 "\\)\\([[{][^]}]*[]}]\\)*[[{]\\(%s\\)[]}]"))
998 (find-label-re-format
999 (concat "\\("
cc436baa 1000 "label[[:space:]]*=[[:space:]]*"
86332df2 1001 "\\|"
3666daf6
CD
1002 (mapconcat 'regexp-quote (append '("\\label")
1003 macros-with-labels) "\\|")
1004 "\\)\\([[{][^]}]*[]}]\\)*[[{]\\(%s\\)[]}]"))
1005 (index-level-re
1006 (regexp-quote (nth 0 reftex-index-special-chars)))
1007 (index-key-end-re ;; ^]- not allowed
1008 (concat "[^" (nth 3 reftex-index-special-chars) "]"
1009 "[" (nth 1 reftex-index-special-chars)
1010 (nth 2 reftex-index-special-chars) "]"))
1011 )
1c25ed90
CD
1012 (setq reftex-section-regexp section-re
1013 reftex-section-or-include-regexp
1014 (concat section-re "\\|" include-re)
1015 reftex-everything-regexp
1016 (concat label-re "\\|" section-re "\\|" include-re
3666daf6
CD
1017 "\\|" appendix-re
1018 "\\|" index-re
1c25ed90
CD
1019 (if macros-with-labels "\\|" "") macro-re)
1020 reftex-everything-regexp-no-index
1021 (concat label-re "\\|" section-re "\\|" include-re
3666daf6
CD
1022 "\\|" appendix-re
1023 "\\|" "\\(\\\\6\\\\3\\\\1\\)" ; This is unlikely to match
1c25ed90 1024 (if macros-with-labels "\\|" "") macro-re)
3666daf6
CD
1025 reftex-index-re index-re
1026 reftex-index-level-re index-level-re
1027 reftex-index-key-end-re index-key-end-re
1028 reftex-macros-with-labels macros-with-labels
1029 reftex-find-index-entry-regexp-format find-index-re-format
1c25ed90 1030 reftex-find-label-regexp-format find-label-re-format
a2e43f12 1031 reftex-find-label-regexp-format2
3666daf6 1032 "\\([]} \t\n\r]\\)\\([[{]\\)\\(%s\\)[]}]")
1c25ed90
CD
1033 (message "Compiling label environment definitions...done")))
1034 (put reftex-docstruct-symbol 'reftex-cache
1035 (mapcar 'symbol-value reftex-cache-variables)))
29d593f8 1036
1c25ed90
CD
1037(defun reftex-parse-args (macro)
1038 ;; Return a list of macro name, nargs, arg-nr which is label and a list of
1039 ;; optional argument indices.
1040 (if (string-match "[[{]\\*?[]}]" macro)
1041 (progn
1042 (let ((must-match (substring macro 0 (match-beginning 0)))
1043 (args (substring macro (match-beginning 0)))
1044 opt-list nlabel (cnt 0))
1045 (while (string-match "\\`[[{]\\(\\*\\)?[]}]" args)
1046 (incf cnt)
1047 (when (eq ?\[ (string-to-char args))
1048 (push cnt opt-list))
1049 (when (and (match-end 1)
3666daf6
CD
1050 (not nlabel))
1051 (setq nlabel cnt))
1c25ed90
CD
1052 (setq args (substring args (match-end 0))))
1053 (list must-match cnt nlabel opt-list)))
1054 nil))
a7ec1775 1055
b849548d 1056;;; =========================================================================
a7ec1775 1057;;;
1c25ed90 1058;;; Accessing the parse information
a7ec1775 1059
1c25ed90
CD
1060(defun reftex-access-scan-info (&optional rescan file)
1061 "Ensure access to the scanning info for the current file."
1062 ;; When the multifile symbols are not yet tied,
1063 ;; tie them. When they are empty or RESCAN is non-nil, scan the document.
1064 ;; But, when RESCAN is -1, don't rescan even if docstruct is empty.
1065 ;; When FILE is non-nil, parse only from that file.
a6611c0d 1066
489632ba
CD
1067 ;; Error out in a buffer without a file.
1068 (if (and reftex-mode
1069 (not (buffer-file-name)))
88c31d68 1070 (error "RefTeX works only in buffers visiting a file"))
489632ba 1071
1c25ed90
CD
1072 ;; Make sure we have the symbols tied
1073 (if (eq reftex-docstruct-symbol nil)
1074 ;; Symbols are not yet tied: Tie them.
1075 (reftex-tie-multifile-symbols))
921759ee 1076
1c25ed90 1077 (reftex-ensure-compiled-variables)
a7ec1775 1078
1c25ed90 1079 (when (or (null (symbol-value reftex-docstruct-symbol))
3666daf6 1080 (member rescan '(t 1 (4) (16))))
1c25ed90
CD
1081 ;; The docstruct will change: Remove selection buffers.
1082 (save-excursion
1083 (reftex-erase-buffer "*toc*")
1084 (reftex-erase-all-selection-and-index-buffers)))
a7ec1775 1085
1c25ed90 1086 (if (and (null (symbol-value reftex-docstruct-symbol))
3666daf6 1087 (not (member rescan '(t 1 (4) (16))))
1c25ed90
CD
1088 reftex-save-parse-info)
1089 ;; Try to read the stuff from a file
1090 (reftex-access-parse-file 'read))
b849548d 1091
b849548d 1092 (cond
1c25ed90
CD
1093 ((equal rescan -1)) ;; We are not allowed to scan.
1094 ((not (symbol-value reftex-docstruct-symbol))
1095 ;; Scan the whole document
1096 (reftex-do-parse 1 file))
1097 ((member rescan '(t 1 (4) (16)))
1098 ;; Scan whatever was required by the caller.
1099 (reftex-do-parse rescan file))))
b849548d 1100
11b4a0d2
CD
1101(defun reftex-scanning-info-available-p ()
1102 "Is the scanning info about the current document available?"
1103 (unless reftex-docstruct-symbol
1104 (reftex-tie-multifile-symbols))
1105 (and (symbolp reftex-docstruct-symbol)
1106 (symbol-value reftex-docstruct-symbol)
1107 t))
a2e43f12 1108
1c25ed90
CD
1109(defun reftex-silence-toc-markers (list n)
1110 ;; Set all toc markers in the first N entries in list to nil
1111 (while (and list (> (decf n) -1))
1112 (and (eq (car (car list)) 'toc)
1113 (markerp (nth 4 (car list)))
1114 (set-marker (nth 4 (car list)) nil))
1115 (pop list)))
b849548d 1116
1c25ed90
CD
1117(defun reftex-access-parse-file (action)
1118 "Perform ACTION on the parse file (the .rel file).
1119Valid actions are: readable, restore, read, kill, write."
1120 (let* ((list (symbol-value reftex-docstruct-symbol))
3666daf6 1121 (docstruct-symbol reftex-docstruct-symbol)
1c25ed90 1122 (master (reftex-TeX-master-file))
3666daf6 1123 (enable-local-variables nil)
1c25ed90 1124 (file (if (string-match "\\.[a-zA-Z]+\\'" master)
a2e43f12 1125 (concat (substring master 0 (match-beginning 0))
3666daf6 1126 reftex-parse-file-extension)
9f286482 1127 (concat master reftex-parse-file-extension))))
1c25ed90
CD
1128 (cond
1129 ((eq action 'readable)
1130 (file-readable-p file))
1131 ((eq action 'restore)
1132 (put reftex-docstruct-symbol 'modified nil)
1133 (if (eq reftex-docstruct-symbol nil)
1134 ;; Symbols are not yet tied: Tie them.
1135 (reftex-tie-multifile-symbols))
1136 (if (file-exists-p file)
1137 ;; load the file and return t for success
3666daf6
CD
1138 (condition-case nil
1139 (progn (load-file file) t)
1140 (error (set reftex-docstruct-symbol nil)
1141 (error "Error while loading file %s" file)))
1c25ed90
CD
1142 ;; Throw an exception if the file does not exist
1143 (error "No restore file %s" file)))
1144 ((eq action 'read)
1145 (put reftex-docstruct-symbol 'modified nil)
1146 (if (file-exists-p file)
1147 ;; load the file and return t for success
3666daf6
CD
1148 (condition-case nil
1149 (progn
1150 (load-file file)
1151 (reftex-check-parse-consistency)
1152 t)
1153 (error (message "Error while restoring file %s" file)
1154 (set reftex-docstruct-symbol nil)
1155 nil))
1c25ed90
CD
1156 ;; return nil for failure, but no exception
1157 nil))
1158 ((eq action 'kill)
1159 ;; Remove the file
1160 (when (and (file-exists-p file) (file-writable-p file))
3666daf6
CD
1161 (message "Unlinking file %s" file)
1162 (delete-file file)))
1c25ed90
CD
1163 (t
1164 (put docstruct-symbol 'modified nil)
1165 (save-excursion
1166 (if (file-writable-p file)
49bbf1b9 1167 (with-temp-file file
1c25ed90 1168 (message "Writing parse file %s" (abbreviate-file-name file))
1c25ed90
CD
1169 (insert (format ";; RefTeX parse info file\n"))
1170 (insert (format ";; File: %s\n" master))
1171 (insert (format ";; User: %s (%s)\n\n"
1172 (user-login-name) (user-full-name)))
1173 (insert "(set reftex-docstruct-symbol '(\n\n")
1174 (let ((standard-output (current-buffer)))
4b33469e 1175 (mapc
3666daf6
CD
1176 (lambda (x)
1177 (cond ((eq (car x) 'toc)
1178 ;; A toc entry. Do not save the marker.
1179 ;; Save the markers position at position 8
1180 (print (list 'toc "toc" (nth 2 x) (nth 3 x)
1181 nil (nth 5 x) (nth 6 x) (nth 7 x)
1182 (or (and (markerp (nth 4 x))
1183 (marker-position (nth 4 x)))
1184 (nth 8 x)))))
1185 ((and (not (eq t reftex-support-index))
1186 (eq (car x) 'index))
1187 ;; Don't save index entries
1188 )
1189 (t (print x))))
1c25ed90 1190 list))
49bbf1b9 1191 (insert "))\n\n"))
1c25ed90
CD
1192 (error "Cannot write to file %s" file)))
1193 t))))
921759ee 1194
1c25ed90
CD
1195(defun reftex-check-parse-consistency ()
1196 ;; Check if parse file is consistent, throw an error if not.
1197
1198 ;; Check if the master is the same: when moving a document, this will see it.
1199 (let* ((real-master (reftex-TeX-master-file))
a2e43f12 1200 (parsed-master
3666daf6 1201 (nth 1 (assq 'bof (symbol-value reftex-docstruct-symbol)))))
1c25ed90
CD
1202 (unless (string= (file-truename real-master) (file-truename parsed-master))
1203 (message "Master file name in load file is different: %s versus %s"
3666daf6 1204 parsed-master real-master)
1c25ed90
CD
1205 (error "Master file name error")))
1206
1207 ;; Check for the existence of all document files
1208;;; (let* ((all (symbol-value reftex-docstruct-symbol)))
1209;;; (while all
1210;;; (when (and (eq (car (car all)) 'bof)
3666daf6 1211;;; (not (file-regular-p (nth 1 (car all)))))
a7ef684b 1212;;; (message "File %s in saved parse info not available" (cdr (car all)))
3666daf6 1213;;; (error "File not found"))
1c25ed90
CD
1214;;; (setq all (cdr all))))
1215 )
1216
1217(defun reftex-select-external-document (xr-alist xr-index)
1218 ;; Return index of an external document.
1219 (let* ((len (length xr-alist)) (highest (1- (+ ?0 len)))
a2e43f12 1220 (prompt (format "[%c-%c] Select TAB: Read prefix with completion"
3666daf6
CD
1221 ?0 highest))
1222 key prefix)
1c25ed90
CD
1223 (cond
1224 ((= len 1)
1225 (message "No external documents available")
1226 (ding) (sit-for 1) 0)
1227 ((= len 2)
1228 (- 1 xr-index))
1229 (t
1230 (save-excursion
a2e43f12 1231 (let* ((length (apply 'max (mapcar
3666daf6
CD
1232 (lambda(x) (length (car x))) xr-alist)))
1233 (fmt (format " [%%c] %%-%ds %%s\n" length))
1234 (n (1- ?0)))
1235 (setq key
1236 (reftex-select-with-char
1237 prompt
1238 (concat
1239 "SELECT EXTERNAL DOCUMENT\n------------------------\n"
1240 (mapconcat
a2e43f12 1241 (lambda (x)
3666daf6
CD
1242 (format fmt (incf n) (or (car x) "")
1243 (abbreviate-file-name (cdr x))))
1244 xr-alist ""))
1245 nil t))
1246 (cond
1247 ((and (>= key ?0) (<= key highest)) (- key ?0))
1248 ((= key ?\C-i)
1249 (setq prefix (completing-read "Prefix: " xr-alist nil t))
1250 (- len (length (memq (assoc prefix xr-alist) xr-alist))))
5181ff9f 1251 (t (error "Invalid document selection [%c]" key)))))))))
b849548d
CD
1252
1253;;; =========================================================================
1254;;;
1255;;; Finding files
1256
f9ad2e24 1257(defun reftex-locate-file (file type master-dir &optional die)
91af3942 1258 "Find FILE of type TYPE in MASTER-DIR or on the path associated with TYPE.
4af58480
CD
1259If the file does not have any of the valid extensions for TYPE,
1260try first the default extension and only then the naked file name.
1261When DIE is non-nil, throw an error if file not found."
1262 (let* ((rec-values (if reftex-search-unrecursed-path-first '(nil t) '(t)))
1263 (extensions (cdr (assoc type reftex-file-extensions)))
1264 (def-ext (car extensions))
a2e43f12 1265 (ext-re (concat "\\("
4af58480
CD
1266 (mapconcat 'regexp-quote extensions "\\|")
1267 "\\)\\'"))
1268 (files (if (string-match ext-re file)
1269 (cons file nil)
1270 (if reftex-try-all-extensions
1271 (append (mapcar (lambda (x) (concat file x))
1272 extensions)
1273 (list file))
1274 (list (concat file def-ext) file))))
1275 path old-path file1 f fs)
1276 (cond
1277 ((file-name-absolute-p file)
1278 (while (setq f (pop files))
1279 (if (file-regular-p f)
1280 (setq file1 f files nil))))
1281 ((and reftex-use-external-file-finders
1282 (assoc type reftex-external-file-finders))
1283 (setq file1 (reftex-find-file-externally file type master-dir)))
1284 (t
1285 (while (and (null file1) rec-values)
1286 (setq path (reftex-access-search-path
1287 type (pop rec-values) master-dir file))
1288 (setq fs files)
1289 (while (and (null file1) (setq f (pop fs)))
1290 (when (or (null old-path)
1291 (not (eq old-path path)))
1292 (setq old-path path
1293 path (cons master-dir path))
1294 (setq file1 (reftex-find-file-on-path f path master-dir)))))))
1295 (cond (file1 file1)
1296 (die (error "No such file: %s" file) nil)
1297 (t (message "No such file: %s (ignored)" file) nil))))
1298
51d628c8
CD
1299(defun reftex-find-file-externally (file type &optional master-dir)
1300 ;; Use external program to find FILE.
f9ad2e24 1301 ;; The program is taken from `reftex-external-file-finders'.
c7015153 1302 ;; Interpret relative path definitions starting from MASTER-DIR.
51d628c8 1303 (let ((default-directory (or master-dir default-directory))
3666daf6
CD
1304 (prg (cdr (assoc type reftex-external-file-finders)))
1305 out)
51d628c8 1306 (if (string-match "%f" prg)
3666daf6 1307 (setq prg (replace-match file t t prg)))
51d628c8 1308 (setq out (apply 'reftex-process-string (split-string prg)))
1c25ed90 1309 (if (string-match "[ \t\n]+\\'" out) ; chomp
3666daf6 1310 (setq out (replace-match "" nil nil out)))
51d628c8 1311 (cond ((equal out "") nil)
3666daf6
CD
1312 ((file-regular-p out) (expand-file-name out master-dir))
1313 (t nil))))
51d628c8
CD
1314
1315(defun reftex-process-string (program &rest args)
1316 "Execute PROGRAM with arguments ARGS and return its STDOUT as a string."
1c25ed90
CD
1317 (let ((calling-dir default-directory)) ; remember default directory
1318 (with-output-to-string
1319 (with-current-buffer standard-output
3666daf6
CD
1320 (let ((default-directory calling-dir)) ; set default directory
1321 (apply 'call-process program nil '(t nil) nil args))))))
51d628c8 1322
f9ad2e24
CD
1323(defun reftex-access-search-path (type &optional recurse master-dir file)
1324 ;; Access path from environment variables. TYPE is either "tex" or "bib".
b849548d
CD
1325 ;; When RECURSE is t, expand path elements ending in `//' recursively.
1326 ;; Relative path elements are left as they are. However, relative recursive
1327 ;; elements are expanded with MASTER-DIR as default directory.
1328 ;; The expanded path is cached for the next search.
1329 ;; FILE is just for the progress message.
1330 ;; Returns the derived path.
f9ad2e24 1331 (let* ((pathvar (intern (concat "reftex-" type "-path"))))
b849548d 1332 (when (null (get pathvar 'status))
f9ad2e24
CD
1333 ;; Get basic path
1334 (set pathvar
3a1e8128 1335 (reftex-uniquify
3666daf6
CD
1336 (reftex-parse-colon-path
1337 (mapconcat
a2e43f12 1338 (lambda(x)
3666daf6
CD
1339 (if (string-match "^!" x)
1340 (apply 'reftex-process-string
1341 (split-string (substring x 1)))
1342 (or (getenv x) x)))
1343 ;; For consistency, the next line should look like this:
1344 ;; (cdr (assoc type reftex-path-environment))
1345 ;; However, historically we have separate options for the
1346 ;; environment variables, so we have to do this:
a2e43f12 1347 (symbol-value (intern (concat "reftex-" type
3666daf6
CD
1348 "path-environment-variables")))
1349 path-separator))))
b849548d
CD
1350 (put pathvar 'status 'split)
1351 ;; Check if we have recursive elements
1352 (let ((path (symbol-value pathvar)) dir rec)
3666daf6
CD
1353 (while (setq dir (pop path))
1354 (when (string= (substring dir -2) "//")
1355 (if (file-name-absolute-p dir)
1356 (setq rec (or rec 'absolute))
1357 (setq rec 'relative))))
1358 (put pathvar 'rec-type rec)))
b849548d
CD
1359
1360 (if recurse
3666daf6
CD
1361 ;; Return the recursive expansion of the path
1362 (cond
1363 ((not (get pathvar 'rec-type))
1364 ;; Path does not contain recursive elements - use simple path
1365 (symbol-value pathvar))
1366 ((or (not (get pathvar 'recursive-path))
1367 (and (eq (get pathvar 'rec-type) 'relative)
1368 (not (equal master-dir (get pathvar 'master-dir)))))
1369 ;; Either: We don't have a recursive expansion yet.
1370 ;; or: Relative recursive path elements need to be expanded
1371 ;; relative to new default directory
1372 (message "Expanding search path to find %s file: %s ..." type file)
a2e43f12 1373 (put pathvar 'recursive-path
3666daf6
CD
1374 (reftex-expand-path (symbol-value pathvar) master-dir))
1375 (put pathvar 'master-dir master-dir)
1376 (get pathvar 'recursive-path))
a2e43f12 1377 (t
3666daf6
CD
1378 ;; Recursive path computed earlier is still OK.
1379 (get pathvar 'recursive-path)))
b849548d
CD
1380 ;; The simple path was requested
1381 (symbol-value pathvar))))
1382
1383(defun reftex-find-file-on-path (file path &optional def-dir)
1384 ;; Find FILE along the directory list PATH.
1385 ;; DEF-DIR is the default directory for expanding relative path elements.
1386 (catch 'exit
1387 (when (file-name-absolute-p file)
a6611c0d 1388 (if (file-regular-p file)
3666daf6
CD
1389 (throw 'exit file)
1390 (throw 'exit nil)))
f9ad2e24 1391 (let* ((thepath path) file1 dir)
b849548d 1392 (while (setq dir (pop thepath))
3666daf6
CD
1393 (when (string= (substring dir -2) "//")
1394 (setq dir (substring dir 0 -1)))
1395 (setq file1 (expand-file-name file (expand-file-name dir def-dir)))
1396 (if (file-regular-p file1)
1397 (throw 'exit file1)))
b849548d
CD
1398 ;; No such file
1399 nil)))
1400
1401(defun reftex-parse-colon-path (path)
1402 ;; Like parse-colon-parse, but // or /~ are left alone.
1403 ;; Trailing ! or !! will be converted into `//' (emTeX convention)
1404 (mapcar
1405 (lambda (dir)
a2e43f12 1406 (if (string-match "\\(//+\\|/*!+\\)\\'" dir)
3666daf6 1407 (setq dir (replace-match "//" t t dir)))
b849548d
CD
1408 (file-name-as-directory dir))
1409 (delete "" (split-string path (concat path-separator "+")))))
1410
1411(defun reftex-expand-path (path &optional default-dir)
1412 ;; Expand parts of path ending in `//' recursively into directory list.
1413 ;; Relative recursive path elements are expanded relative to DEFAULT-DIR.
1414 (let (path1 dir recursive)
1415 (while (setq dir (pop path))
1416 (if (setq recursive (string= (substring dir -2) "//"))
3666daf6 1417 (setq dir (substring dir 0 -1)))
b849548d 1418 (if (and recursive
3666daf6
CD
1419 (not (file-name-absolute-p dir)))
1420 (setq dir (expand-file-name dir default-dir)))
b849548d 1421 (if recursive
3666daf6
CD
1422 ;; Expand recursively
1423 (setq path1 (append (reftex-recursive-directory-list dir) path1))
1424 ;; Keep unchanged
1425 (push dir path1)))
b849548d 1426 (nreverse path1)))
a7ec1775 1427
b849548d
CD
1428(defun reftex-recursive-directory-list (dir)
1429 ;; Return a list of all directories below DIR, including DIR itself
1430 (let ((path (list dir)) path1 file files)
1431 (while (setq dir (pop path))
1432 (when (file-directory-p dir)
3666daf6
CD
1433 (setq files (nreverse (directory-files dir t "[^.]")))
1434 (while (setq file (pop files))
a2e43f12 1435 (if (file-directory-p file)
3666daf6
CD
1436 (push (file-name-as-directory file) path)))
1437 (push dir path1)))
b849548d 1438 path1))
a7ec1775 1439
b849548d
CD
1440;;; =========================================================================
1441;;;
1442;;; Some generally useful functions
a7ec1775 1443
1c25ed90
CD
1444(defun reftex-typekey-check (typekey conf-variable &optional n)
1445 ;; Check if CONF-VARIABLE is true or contains TYPEKEY
1446 (and n (setq conf-variable (nth n conf-variable)))
1447 (or (eq conf-variable t)
1448 (and (stringp conf-variable)
1449 (string-match (concat "[" conf-variable "]") typekey))))
1450
1451(defun reftex-check-recursive-edit ()
1452 ;; Check if we are already in a recursive edit. Abort with helpful
1453 ;; message if so.
1454 (if (marker-position reftex-recursive-edit-marker)
1455 (error
1456 (substitute-command-keys
1457 "In unfinished selection process. Finish, or abort with \\[abort-recursive-edit]"))))
1458
1459(defun reftex-in-comment ()
4f595e15 1460 "Return non-nil if point is in a comment."
1c25ed90 1461 (save-excursion
4f595e15
RA
1462 (save-match-data
1463 (let ((pos (point)))
1464 (beginning-of-line)
1465 (re-search-forward
1466 (or comment-start-skip
1467 ;; The parser may open files in fundamental mode if
1468 ;; `reftex-initialize-temporary-buffers' is nil, so here
1469 ;; is a default suitable for plain TeX and LaTeX.
1470 "\\(\\(^\\|[^\\\n]\\)\\(\\\\\\\\\\)*\\)\\(%+[ \t]*\\)")
1471 pos t)))))
1c25ed90 1472
a7ec1775
RS
1473(defun reftex-no-props (string)
1474 ;; Return STRING with all text properties removed
1475 (and (stringp string)
1476 (set-text-properties 0 (length string) nil string))
1477 string)
1478
396e0b08
KH
1479(defun reftex-match-string (n)
1480 ;; Match string without properties
1481 (when (match-beginning n)
1482 (buffer-substring-no-properties (match-beginning n) (match-end n))))
1483
3666daf6 1484(defun reftex-region-active-p ()
f93df073
RS
1485 "Should we operate on an active region?"
1486 (if (fboundp 'use-region-p)
1487 (use-region-p)
1488 ;; For XEmacs.
1489 (region-active-p)))
3666daf6 1490
2faef409 1491(defun reftex-kill-buffer (buffer)
29d593f8 1492 ;; Kill buffer if it exists.
2faef409
RS
1493 (and (setq buffer (get-buffer buffer))
1494 (kill-buffer buffer)))
1495
29d593f8
KH
1496(defun reftex-erase-buffer (&optional buffer)
1497 ;; Erase BUFFER if it exists. BUFFER defaults to current buffer.
1498 ;; This even erases read-only buffers.
1499 (cond
1500 ((null buffer)
1501 ;; erase current buffer
1502 (let ((buffer-read-only nil)) (erase-buffer)))
1503 ((setq buffer (get-buffer buffer))
1504 ;; buffer exists
9a529312
SM
1505 (with-current-buffer buffer
1506 (let ((inhibit-read-only t)) (erase-buffer))))))
2faef409 1507
396e0b08
KH
1508(defun reftex-this-word (&optional class)
1509 ;; Grab the word around point.
1510 (setq class (or class "-a-zA-Z0-9:_/.*;|"))
1511 (save-excursion
1512 (buffer-substring-no-properties
1513 (progn (skip-chars-backward class) (point))
1514 (progn (skip-chars-forward class) (point)))))
1515
7c4d13cc
CD
1516(defun reftex-number (n unit &optional ending)
1517 (if (and (integerp n) (stringp unit))
1518 (format "%d %s%s" n unit (if (= n 1) "" (or ending "s")))
1519 ""))
1520
b849548d
CD
1521(defun reftex-all-assq (key list)
1522 ;; Return a list of all associations of KEY in LIST. Comparison with eq.
1523 (let (rtn)
1524 (while (setq list (memq (assq key list) list))
1525 (push (car list) rtn)
1526 (pop list))
1527 (nreverse rtn)))
1528
1529(defun reftex-all-assoc-string (key list)
1530 ;; Return a list of all associations of KEY in LIST. Comparison with string=.
1531 (let (rtn)
1532 (while list
1533 (if (string= (car (car list)) key)
1534 (push (car list) rtn))
1535 (pop list))
1536 (nreverse rtn)))
1537
1c25ed90 1538(defun reftex-last-assoc-before-elt (key elt list &optional exclusive)
b849548d
CD
1539 ;; Find the last association of KEY in LIST before or at ELT
1540 ;; ELT is found in LIST with equal, not eq.
1541 ;; Returns nil when either KEY or elt are not found in LIST.
1c25ed90 1542 ;; When EXCLUSIVE is non-nil, ELT cannot be the return value.
b849548d 1543 ;; On success, returns the association.
1c25ed90 1544 (let* ((elt (car (member elt list))) (ex (not exclusive)) ass last-ass)
b849548d
CD
1545 (while (and (setq ass (assoc key list))
1546 (setq list (memq ass list))
3666daf6 1547 (or ex (not (eq elt (car list))))
b849548d
CD
1548 (memq elt list))
1549 (setq last-ass ass
1550 list (cdr list)))
1551 last-ass))
1552
1c25ed90
CD
1553(defun reftex-sublist-nth (list nth predicate &optional completion)
1554 ;; Make a list of the NTH elements of all members of LIST which
1555 ;; fulfill PREDICATE.
1556 ;; When COMPLETION is non-nil, make all elements of the resulting
1557 ;; list also a list, so that the result can be used for completion.
1558 (let (rtn)
1559 (while list
1560 (if (funcall predicate (car list))
3666daf6 1561 (push (if completion
a2e43f12 1562 (list (nth nth (car list)))
3666daf6
CD
1563 (nth nth (car list)))
1564 rtn))
1c25ed90
CD
1565 (setq list (cdr list)))
1566 (nreverse rtn)))
1567
1568(defun reftex-make-selection-buffer-name (type &optional index)
1569 ;; Make unique name for a selection buffer.
1570 (format " *RefTeX[%s][%d]*"
3666daf6 1571 type (or index (get reftex-docstruct-symbol :master-index) 0)))
1c25ed90
CD
1572
1573(defun reftex-make-index-buffer-name (tag &optional cnt)
1574 ;; Make unique name for an index buffer.
1575 (format "*Index[%s][%d]*"
3666daf6 1576 tag (or cnt (get reftex-docstruct-symbol :master-index) 0)))
1c25ed90 1577
396e0b08 1578(defun reftex-truncate (string ncols &optional ellipses padding)
f9ad2e24
CD
1579 ;; Truncate STRING to NCOLS characters.
1580 ;; When PADDING is non-nil, and string is shorter than NCOLS, fill with
1581 ;; white space to NCOLS characters. When ELLIPSES is non-nil and the
1582 ;; string needs to be truncated, replace last 3 characters by dots.
396e0b08 1583 (setq string
3666daf6
CD
1584 (if (<= (length string) ncols)
1585 string
1586 (if ellipses
1587 (concat (substring string 0 (- ncols 3)) "...")
1588 (substring string 0 ncols))))
396e0b08
KH
1589 (if padding
1590 (format (format "%%-%ds" ncols) string)
1591 string))
1592
1c25ed90 1593(defun reftex-nearest-match (regexp &optional max-length)
b849548d
CD
1594 ;; Find the nearest match of REGEXP. Set the match data.
1595 ;; If POS is given, calculate distances relative to it.
1596 ;; Return nil if there is no match.
1c25ed90 1597 (let ((pos (point))
a2e43f12 1598 (dist (or max-length (length regexp)))
3666daf6 1599 match1 match2 match)
1c25ed90 1600 (goto-char (min (+ pos dist) (point-max)))
b849548d
CD
1601 (when (re-search-backward regexp nil t)
1602 (setq match1 (match-data)))
1c25ed90 1603 (goto-char (max (- pos dist) (point-min)))
b849548d
CD
1604 (when (re-search-forward regexp nil t)
1605 (setq match2 (match-data)))
1c25ed90 1606 (goto-char pos)
b849548d
CD
1607 (setq match
1608 (cond
1609 ((not match1) match2)
1610 ((not match2) match1)
1611 ((< (abs (- pos (car match1))) (abs (- pos (car match2)))) match1)
1612 (t match2)))
1613 (if match (progn (set-match-data match) t) nil)))
1614
1615(defun reftex-auto-mode-alist ()
1616 ;; Return an `auto-mode-alist' with only the .gz (etc) thingies.
1617 ;; Stolen from gnus nnheader.
1618 (let ((alist auto-mode-alist)
1619 out)
1620 (while alist
1621 (when (listp (cdr (car alist)))
1622 (push (car alist) out))
1623 (pop alist))
1624 (nreverse out)))
1625
921759ee
CD
1626(defun reftex-window-height ()
1627 (if (fboundp 'window-displayed-height)
1628 (window-displayed-height)
1629 (window-height)))
1630
b849548d
CD
1631(defun reftex-enlarge-to-fit (buf2 &optional keep-current)
1632 ;; Enlarge other window displaying buffer to show whole buffer if possible.
1633 ;; If KEEP-CURRENT in non-nil, current buffer must remain visible.
1634 (let* ((win1 (selected-window))
3666daf6
CD
1635 (buf1 (current-buffer))
1636 (win2 (get-buffer-window buf2))) ;; Only on current frame.
b849548d
CD
1637 (when win2
1638 (select-window win2)
3666daf6
CD
1639 (unless (and (pos-visible-in-window-p (point-min))
1640 (pos-visible-in-window-p (point-max)))
1641 (enlarge-window (1+ (- (count-lines (point-min) (point-max))
1642 (reftex-window-height))))))
b849548d
CD
1643 (cond
1644 ((window-live-p win1) (select-window win1))
1645 (keep-current
1646 ;; we must have the old buffer!
1647 (switch-to-buffer-other-window buf1)
1648 (shrink-window (- (window-height) window-min-height))))))
1649
1650(defun reftex-select-with-char (prompt help-string &optional delay-time scroll)
1651 ;; Offer to select something with PROMPT and, after DELAY-TIME seconds,
1652 ;; also with HELP-STRING.
1653 ;; When SCROLL is non-nil, use SPC and DEL to scroll help window.
1654 (let ((char ?\?))
1655 (save-window-excursion
1656 (catch 'exit
274f1353 1657 (message "%s (?=Help)" prompt)
3666daf6
CD
1658 (when (or (sit-for (or delay-time 0))
1659 (= ?\? (setq char (read-char-exclusive))))
1660 (reftex-kill-buffer "*RefTeX Select*")
1661 (switch-to-buffer-other-window "*RefTeX Select*")
1662 (insert help-string)
1663 (goto-char 1)
1664 (unless (and (pos-visible-in-window-p (point-min))
1665 (pos-visible-in-window-p (point-max)))
1666 (enlarge-window (1+ (- (count-lines (point-min) (point-max))
1667 (reftex-window-height)))))
1668 (setq truncate-lines t))
1669 (if (and (pos-visible-in-window-p (point-min))
1670 (pos-visible-in-window-p (point-max)))
1671 nil
1672 (setq prompt (concat prompt (if scroll " (SPC/DEL=Scroll)" ""))))
274f1353 1673 (message "%s" prompt)
3666daf6
CD
1674 (and (equal char ?\?) (setq char (read-char-exclusive)))
1675 (while t
1676 (cond ((equal char ?\C-g) (keyboard-quit))
1677 ((equal char ?\?))
1678 ((and scroll (equal char ?\ ))
1679 (condition-case nil (scroll-up) (error nil))
274f1353 1680 (message "%s" prompt))
3666daf6
CD
1681 ((and scroll (equal char ?\C-? ))
1682 (condition-case nil (scroll-down) (error nil))
274f1353 1683 (message "%s" prompt))
a2e43f12 1684 (t (message "")
4f595e15 1685 (reftex-kill-buffer "*RefTeX Select*")
3666daf6
CD
1686 (throw 'exit char)))
1687 (setq char (read-char-exclusive)))))))
a2e43f12 1688
b849548d
CD
1689
1690(defun reftex-make-regexp-allow-for-ctrl-m (string)
1691 ;; convert STRING into a regexp, allowing ^M for \n and vice versa
1692 (let ((start -2))
1693 (setq string (regexp-quote string))
1694 (while (setq start (string-match "[\n\r]" string (+ 3 start)))
1695 (setq string (replace-match "[\n\r]" nil t string)))
1696 string))
1697
1698(defun reftex-get-buffer-visiting (file)
1699 ;; return a buffer visiting FILE
1700 (cond
1701 ((boundp 'find-file-compare-truenames) ; XEmacs
1702 (let ((find-file-compare-truenames t))
1703 (get-file-buffer file)))
1704 ((fboundp 'find-buffer-visiting) ; Emacs
1705 (find-buffer-visiting file))
1706 (t (error "This should not happen (reftex-get-buffer-visiting)"))))
1707
1708;; Define `current-message' for compatibility with XEmacs prior to 20.4
1709(defvar message-stack)
1710(if (and (featurep 'xemacs)
3666daf6 1711 (not (fboundp 'current-message)))
b849548d
CD
1712 (defun current-message (&optional frame)
1713 (cdr (car message-stack))))
1714
1715(defun reftex-visited-files (list)
1716 ;; Takes a list of filenames and returns the buffers of those already visited
1717 (delq nil (mapcar (lambda (x) (if (reftex-get-buffer-visiting x) x nil))
3666daf6 1718 list)))
b849548d
CD
1719
1720(defun reftex-get-file-buffer-force (file &optional mark-to-kill)
1721 ;; Return a buffer visiting file. Make one, if necessary.
1722 ;; If neither such a buffer nor the file exist, return nil.
1723 ;; If MARK-TO-KILL is t and there is no live buffer, visit the file with
1724 ;; initializations according to `reftex-initialize-temporary-buffers',
1725 ;; and mark the buffer to be killed after use.
1726
1727 (let ((buf (reftex-get-buffer-visiting file)))
1728
1729 (cond (buf
1730 ;; We have it already as a buffer - just return it
1731 buf)
1732
1733 ((file-readable-p file)
1734 ;; At least there is such a file and we can read it.
1735
1736 (if (or (not mark-to-kill)
1737 (eq t reftex-initialize-temporary-buffers))
1738
1739 ;; Visit the file with full magic
1740 (setq buf (find-file-noselect file))
1741
1742 ;; Else: Visit the file just briefly, without or
1743 ;; with limited Magic
1744
1745 ;; The magic goes away
14acf2f5
SM
1746 (letf ((format-alist nil)
1747 (auto-mode-alist (reftex-auto-mode-alist))
1748 ((default-value 'major-mode) 'fundamental-mode)
1749 (enable-local-variables nil)
1750 (after-insert-file-functions nil))
b849548d
CD
1751 (setq buf (find-file-noselect file)))
1752
1753 ;; Is there a hook to run?
1754 (when (listp reftex-initialize-temporary-buffers)
9a529312 1755 (with-current-buffer buf
b849548d
CD
1756 (run-hooks 'reftex-initialize-temporary-buffers))))
1757
c7015153 1758 ;; Let's see if we got a license to kill :-|
3666daf6
CD
1759 (and mark-to-kill
1760 (add-to-list 'reftex-buffers-to-kill buf))
a7ec1775 1761
1c25ed90
CD
1762 ;; Return the new buffer
1763 buf)
396e0b08 1764
1c25ed90
CD
1765 ;; If no such file exists, return nil
1766 (t nil))))
396e0b08 1767
1c25ed90
CD
1768(defun reftex-kill-temporary-buffers (&optional buffer)
1769 ;; Kill all buffers in the list reftex-kill-temporary-buffers.
1770 (cond
1771 (buffer
1772 (when (member buffer reftex-buffers-to-kill)
1773 (kill-buffer buffer)
1774 (setq reftex-buffers-to-kill
1775 (delete buffer reftex-buffers-to-kill))))
1776 (t
1777 (while (setq buffer (pop reftex-buffers-to-kill))
1778 (when (bufferp buffer)
1779 (and (buffer-modified-p buffer)
1780 (y-or-n-p (format "Save file %s? "
1781 (buffer-file-name buffer)))
9a529312 1782 (with-current-buffer buffer
1c25ed90
CD
1783 (save-buffer)))
1784 (kill-buffer buffer))
1785 (pop reftex-buffers-to-kill)))))
396e0b08 1786
1c25ed90
CD
1787(defun reftex-splice-symbols-into-list (list alist)
1788 ;; Splice the association in ALIST of any symbols in LIST into the list.
1789 ;; Return new list.
1790 (let (rtn tmp)
1791 (while list
1792 (while (and (not (null (car list))) ;; keep list elements nil
1793 (symbolp (car list)))
1794 (setq tmp (car list))
1795 (cond
1796 ((assoc tmp alist)
1797 (setq list (append (nth 2 (assoc tmp alist)) (cdr list))))
1798 (t
1799 (error "Cannot treat symbol %s in reftex-label-alist"
1800 (symbol-name tmp)))))
1801 (push (pop list) rtn))
1802 (nreverse rtn)))
396e0b08 1803
1c25ed90
CD
1804(defun reftex-remove-symbols-from-list (list)
1805 ;; Remove all symbols from list
1806 (let (rtn)
1807 (while list
1808 (unless (symbolp (car list))
3666daf6 1809 (push (car list) rtn))
1c25ed90
CD
1810 (setq list (cdr list)))
1811 (nreverse rtn)))
b849548d 1812
4f595e15
RA
1813(defun reftex-uniquify (list &optional sort)
1814 ;; Return a list of all strings in LIST, but each only once, keeping order
1815 ;; unless SORT is set (faster!).
1816 (setq list (copy-sequence list))
1817 (if sort
1818 (progn
1819 (setq list (sort list 'string<))
1820 (let ((p list))
1821 (while (cdr p)
1822 (if (string= (car p) (car (cdr p)))
1823 (setcdr p (cdr (cdr p)))
1824 (setq p (cdr p)))))
1825 list)
1826 (let ((p list) lst elt)
1827 ;; push all sublists into lst in reverse(!) order
1828 (while p
1829 (push p lst)
1830 (setq p (cdr p)))
1831 ;; sort all sublists
1832 (setq lst (sort lst (lambda (x1 x2) (string< (car x1) (car x2)))))
1833 (while (cdr lst)
1834 (setq elt (car (car lst)))
1835 ;; for equal elements in the sorted sublist, replace the
1836 ;; last(!) original list member with nil
1837 (when (string= elt (car (cadr lst)))
1838 (setcar (pop lst) nil)
1839 (while (and (cdr lst) (string= elt (car (cadr lst))))
1840 (setcar (pop lst) nil)))
1841 (pop lst)))
1842 ;; weed out all nils and return.
1843 (delq nil list)))
1844
1845(defun reftex-uniquify-by-car (alist &optional keep-list sort)
1c25ed90
CD
1846 ;; Return a list of all elements in ALIST, but each car only once.
1847 ;; Elements of KEEP-LIST are not removed even if duplicate.
4f595e15
RA
1848 ;; The order is kept unless SORT is set (faster!).
1849 (setq keep-list (sort (copy-sequence keep-list) #'string<)
1850 alist (copy-sequence alist))
1851 (if sort
1852 (let (lst elt)
1853 (setq alist (sort alist (lambda(a b) (string< (car a) (car b)))))
1854 (setq lst alist)
1855 (while (cdr lst)
1856 (setq elt (car (car lst)))
1857 (when (string= elt (car (cadr lst)))
1858 (while (and keep-list (string< (car keep-list) elt))
1859 (pop keep-list))
1860 (if (and keep-list (string= elt (car keep-list)))
1861 (progn
1862 (pop lst)
1863 (while (and (cdr lst)
1864 (string= elt (car (cadr lst))))
1865 (pop lst)))
1866 (setcdr lst (cdr (cdr lst)))
1867 (while (and (cdr lst)
1868 (string= elt (car (cadr lst))))
1869 (setcdr lst (cdr (cdr lst))))))
1870 (pop lst))
1871 alist)
1872 (let ((p alist) lst elt)
1873 (while p
1874 (push p lst)
1875 (setq p (cdr p)))
1876 (setq lst (sort lst (lambda(a b) (string< (car (car a))
1877 (car (car b))))))
1878 (while (cdr lst)
1879 (setq elt (car (car (car lst))))
1880 (when (string= elt (car (car (cadr lst))))
1881 (while (and keep-list (string< (car keep-list) elt))
1882 (pop keep-list))
1883 (if (and keep-list (string= elt (car keep-list)))
1884 (progn
1885 (pop lst)
1886 (while (and (cdr lst)
1887 (string= elt (car (car (cadr lst)))))
1888 (pop lst)))
1889 (setcar (pop lst) nil)
1890 (while (and (cdr lst)
1891 (string= elt (car (car (cadr lst)))))
1892 (setcar (pop lst) nil))))
1893 (pop lst)))
1894 (delq nil alist)))
1895
1896(defun reftex-remove-if (predicate list)
1897 "Nondestructively remove all items from LIST which satisfy PREDICATE."
1898 (let (result)
1899 (dolist (elt list (nreverse result))
1900 (unless (funcall predicate elt)
1901 (push elt result)))))
b849548d 1902
1c25ed90
CD
1903(defun reftex-abbreviate-title (string)
1904 (reftex-convert-string string "[-~ \t\n\r,;]" nil t t
3666daf6 1905 5 40 nil 1 " " (nth 5 reftex-derive-label-parameters)))
b849548d 1906
5181ff9f
WL
1907(defun reftex-convert-string (string split-re invalid-re dot keep-fp
1908 nwords maxchar invalid abbrev sep
3666daf6 1909 ignore-words &optional downcase)
1c25ed90
CD
1910 "Convert a string (a sentence) to something shorter.
1911SPLIT-RE is the regular expression used to split the string into words.
5181ff9f 1912INVALID-RE matches characters which are invalid in the final string.
1c25ed90
CD
1913DOT t means add dots to abbreviated words.
1914KEEP-FP t means to keep a final punctuation when applicable.
1915NWORDS Number of words to use.
1916MAXCHAR Maximum number of characters in the final string.
5181ff9f 1917INVALID nil: Throw away any words containing stuff matched with INVALID-RE.
1c25ed90
CD
1918 t: Throw away only the matched part, not the whole word.
1919ABBREV nil: Never abbreviate words.
1920 t: Always abbreviate words (see `reftex-abbrev-parameters').
1921 not t and not nil: Abbreviate words if necessary to shorten
1922 string below MAXCHAR.
1923SEP String separating different words in the output string.
1924IGNORE-WORDS List of words which should be removed from the string."
396e0b08 1925
1c25ed90 1926 (let* ((words0 (split-string string (or split-re "[ \t\n\r]")))
5181ff9f 1927 (reftex-label-illegal-re (or invalid-re "\000"))
3666daf6
CD
1928 (abbrev-re (concat
1929 "\\`\\("
1930 (make-string (nth 0 reftex-abbrev-parameters) ?.)
1931 "[" (nth 2 reftex-abbrev-parameters) "]*"
1932 "\\)"
1933 "[" (nth 3 reftex-abbrev-parameters) "]"
1934 (make-string (1- (nth 1 reftex-abbrev-parameters)) ?.)))
1c25ed90 1935 words word)
396e0b08 1936
1c25ed90
CD
1937 ;; Remove words from the ignore list or with funny characters
1938 (while (setq word (pop words0))
1939 (if downcase (setq word (downcase word)))
1940 (cond
1941 ((member (downcase word) ignore-words))
1942 ((string-match reftex-label-illegal-re word)
5181ff9f 1943 (when invalid
1c25ed90
CD
1944 (while (string-match reftex-label-illegal-re word)
1945 (setq word (replace-match "" nil nil word)))
1946 (push word words)))
1947 (t
1948 (push word words))))
1949 (setq words (nreverse words))
1950
1951 ;; Restrict number of words
1952 (if (> (length words) nwords)
1953 (setcdr (nthcdr (1- nwords) words) nil))
a2e43f12 1954
1c25ed90
CD
1955 ;; First, try to use all words
1956 (setq string (mapconcat 'identity words sep))
a2e43f12 1957
1c25ed90
CD
1958 ;; Abbreviate words if enforced by user settings or string length
1959 (if (or (eq t abbrev)
1960 (and abbrev
1961 (> (length string) maxchar)))
1962 (setq words
1963 (mapcar
3666daf6
CD
1964 (lambda (w) (if (string-match abbrev-re w)
1965 (if dot
1966 (concat (match-string 1 w) ".")
1967 (match-string 1 w))
1968 w))
1c25ed90
CD
1969 words)
1970 string (mapconcat 'identity words sep)))
b849548d 1971
1c25ed90
CD
1972 ;; Shorten if still to long
1973 (setq string
1974 (if (> (length string) maxchar)
1975 (substring string 0 maxchar)
1976 string))
396e0b08 1977
1c25ed90
CD
1978 ;; Delete the final punctuation, if any
1979 (if (and (not keep-fp) (string-match "\\s.+\\'" string))
1980 (setq string (replace-match "" nil nil string)))
1981 string))
396e0b08 1982
1c25ed90
CD
1983(defun reftex-nicify-text (text)
1984 ;; Make TEXT nice for inclusion as context into label menu.
1985 ;; 1. remove line breaks and extra white space
1986 (while (string-match "[\n\r\t]\\|[ \t][ \t]+" text)
1987 (setq text (replace-match " " nil t text)))
1988 ;; 2. cut before the next `\end{' or `\item' or `\\'
1989 (if (string-match "\\(\\\\end{\\|\\\\item\\|\\\\\\\\\\).*" text)
1990 (setq text (replace-match "" nil t text)))
1991 ;; 3. kill the embedded label
1992 (if (string-match "\\\\label{[^}]*}" text)
1993 (setq text (replace-match "" nil t text)))
1994 ;; 4. remove leading garbage
1995 (if (string-match "\\`[ }]+" text)
1996 (setq text (replace-match "" nil t text)))
1997 ;; 5. limit length
1998 (cond
1999 ((> (length text) 100) (substring text 0 100))
2000 ((= (length text) 0) (make-string 1 ?\ ))
2001 (t text)))
396e0b08 2002
4f595e15 2003
b849548d
CD
2004;;; =========================================================================
2005;;;
1c25ed90 2006;;; Fontification and Highlighting
a7ec1775 2007
1c25ed90
CD
2008(defun reftex-use-fonts ()
2009 ;; Return t if we can and want to use fonts.
a3d6972f 2010 (and ; window-system
1c25ed90
CD
2011 reftex-use-fonts
2012 (featurep 'font-lock)))
a7ec1775 2013
1c25ed90
CD
2014(defun reftex-refontify ()
2015 ;; Return t if we need to refontify context
2016 (and (reftex-use-fonts)
2017 (or (eq t reftex-refontify-context)
2018 (and (eq 1 reftex-refontify-context)
3666daf6
CD
2019 ;; Test of we use the font-lock version of x-symbol
2020 (and (featurep 'x-symbol-tex) (not (boundp 'x-symbol-mode)))))))
a7ec1775 2021
1c25ed90
CD
2022(defvar font-lock-defaults-computed)
2023(defun reftex-fontify-select-label-buffer (parent-buffer)
2024 ;; Fontify the `*RefTeX Select*' buffer. Buffer is temporarily renamed to
91af3942 2025 ;; start with none-SPC char, because Font-Lock otherwise refuses operation.
1c25ed90 2026 (run-hook-with-args 'reftex-pre-refontification-functions
3666daf6 2027 parent-buffer 'reftex-ref)
1c25ed90 2028 (let* ((oldname (buffer-name))
3666daf6 2029 (newname (concat "Fontify-me-" oldname)))
1c25ed90 2030 (unwind-protect
3666daf6
CD
2031 (progn
2032 ;; Rename buffer temporarily to start w/o space (because of font-lock)
2033 (rename-buffer newname t)
2034 (cond
2035 ((fboundp 'font-lock-default-fontify-region)
2036 ;; Good: we have the indirection functions
2037 (set (make-local-variable 'font-lock-fontify-region-function)
2038 'reftex-select-font-lock-fontify-region)
2039 (let ((major-mode 'latex-mode))
2040 (font-lock-mode 1)))
2041 ((fboundp 'font-lock-set-defaults-1)
2042 ;; Looks like the XEmacs font-lock stuff.
2043 ;; FIXME: this is still kind of a hack, but it works.
2044 (set (make-local-variable 'font-lock-keywords) nil)
2045 (let ((major-mode 'latex-mode)
2046 (font-lock-defaults-computed nil))
2047 (font-lock-set-defaults-1)
2048 (reftex-select-font-lock-fontify-region (point-min) (point-max))))
2049 (t
a2e43f12 2050 ;; Oops?
3666daf6 2051 (message "Sorry: cannot refontify RefTeX Select buffer."))))
1c25ed90 2052 (rename-buffer oldname))))
a7ec1775 2053
1c25ed90
CD
2054(defun reftex-select-font-lock-fontify-region (beg end &optional loudly)
2055 ;; Fontify a region, but only lines starting with a dot.
2056 (let ((func (if (fboundp 'font-lock-default-fontify-region)
3666daf6
CD
2057 'font-lock-default-fontify-region
2058 'font-lock-fontify-region))
2059 beg1 end1)
1c25ed90
CD
2060 (goto-char beg)
2061 (while (re-search-forward "^\\." end t)
2062 (setq beg1 (point) end1 (progn (skip-chars-forward "^\n") (point)))
2063 (funcall func beg1 end1 nil)
2064 (goto-char end1))))
a7ec1775 2065
1c25ed90 2066(defun reftex-select-font-lock-unfontify (&rest ignore) t)
a7ec1775 2067
1c25ed90
CD
2068(defun reftex-verified-face (&rest faces)
2069 ;; Return the first valid face in FACES, or nil if none is valid.
2070 ;; Also, when finding a nil element in FACES, return nil. This
2071 ;; function is just a safety net to catch name changes of builtin
6c27f0f8 2072 ;; fonts. Currently it is only used for reftex-label-face.
1c25ed90
CD
2073 (let (face)
2074 (catch 'exit
2075 (while (setq face (pop faces))
3666daf6
CD
2076 (if (featurep 'xemacs)
2077 (if (find-face face) (throw 'exit face))
2078 (if (facep face) (throw 'exit face)))))))
a7ec1775 2079
3a1e8128 2080;; Highlighting uses overlays. For XEmacs, we use extends.
91dd4dc4
DN
2081(defalias 'reftex-make-overlay
2082 (if (featurep 'xemacs) 'make-extent 'make-overlay))
2083(defalias 'reftex-overlay-put
2084 (if (featurep 'xemacs) 'set-extent-property 'overlay-put))
2085(defalias 'reftex-move-overlay
2086 (if (featurep 'xemacs) 'set-extent-endpoints 'move-overlay))
2087(defalias 'reftex-delete-overlay
2088 (if (featurep 'xemacs) 'detach-extent 'delete-overlay))
396e0b08 2089
1c25ed90 2090;; We keep a vector with several different overlays to do our highlighting.
3b919c9f 2091(defvar reftex-highlight-overlays [nil nil nil])
b849548d 2092
1c25ed90 2093;; Initialize the overlays
3a1e8128 2094(aset reftex-highlight-overlays 0 (reftex-make-overlay 1 1))
a2e43f12 2095(reftex-overlay-put (aref reftex-highlight-overlays 0)
3666daf6 2096 'face 'highlight)
3a1e8128
CD
2097(aset reftex-highlight-overlays 1 (reftex-make-overlay 1 1))
2098(reftex-overlay-put (aref reftex-highlight-overlays 1)
3666daf6 2099 'face reftex-cursor-selected-face)
3a1e8128
CD
2100(aset reftex-highlight-overlays 2 (reftex-make-overlay 1 1))
2101(reftex-overlay-put (aref reftex-highlight-overlays 2)
3666daf6 2102 'face reftex-cursor-selected-face)
b849548d 2103
1c25ed90
CD
2104;; Two functions for activating and deactivation highlight overlays
2105(defun reftex-highlight (index begin end &optional buffer)
2106 "Highlight a region with overlay INDEX."
3a1e8128 2107 (reftex-move-overlay (aref reftex-highlight-overlays index)
1c25ed90
CD
2108 begin end (or buffer (current-buffer))))
2109(defun reftex-unhighlight (index)
2110 "Detach overlay INDEX."
3a1e8128 2111 (reftex-delete-overlay (aref reftex-highlight-overlays index)))
2faef409 2112
1c25ed90
CD
2113(defun reftex-highlight-shall-die ()
2114 ;; Function used in pre-command-hook to remove highlights.
2115 (remove-hook 'pre-command-hook 'reftex-highlight-shall-die)
2116 (reftex-unhighlight 0))
b849548d
CD
2117
2118;;; =========================================================================
2119;;;
a2e43f12 2120;;; Keybindings
a7ec1775 2121
b849548d
CD
2122;; The default bindings in the mode map.
2123(loop for x in
7c4d13cc 2124 '(("\C-c=" . reftex-toc)
3666daf6
CD
2125 ("\C-c-" . reftex-toc-recenter)
2126 ("\C-c(" . reftex-label)
2127 ("\C-c)" . reftex-reference)
2128 ("\C-c[" . reftex-citation)
2129 ("\C-c<" . reftex-index)
2130 ("\C-c>" . reftex-display-index)
2131 ("\C-c/" . reftex-index-selection-or-word)
2132 ("\C-c\\" . reftex-index-phrase-selection-or-word)
2133 ("\C-c|" . reftex-index-visit-phrases-buffer)
2134 ("\C-c&" . reftex-view-crossref))
b849548d 2135 do (define-key reftex-mode-map (car x) (cdr x)))
a7ec1775 2136
1c25ed90
CD
2137;; Bind `reftex-mouse-view-crossref' only when the key is still free
2138(if (featurep 'xemacs)
2139 (unless (key-binding [(shift button2)])
a2e43f12 2140 (define-key reftex-mode-map [(shift button2)]
3666daf6 2141 'reftex-mouse-view-crossref))
1c25ed90 2142 (unless (key-binding [(shift mouse-2)])
a2e43f12 2143 (define-key reftex-mode-map [(shift mouse-2)]
1c25ed90
CD
2144 'reftex-mouse-view-crossref)))
2145
f2f426ca
GM
2146(defvar bibtex-mode-map)
2147
1c25ed90
CD
2148;; Bind `reftex-view-crossref-from-bibtex' in BibTeX mode map
2149(eval-after-load
921759ee
CD
2150 "bibtex"
2151 '(define-key bibtex-mode-map "\C-c&" 'reftex-view-crossref-from-bibtex))
2152
21417224
GM
2153;; For most of these commands there are already bindings in place.
2154;; Setting `reftex-extra-bindings' really is only there to spare users
2155;; the hassle of defining bindings in the user space themselves. This
2156;; is why they violate the key binding recommendations.
b849548d 2157(when reftex-extra-bindings
4f595e15
RA
2158 (loop for x in
2159 '(("\C-ct" . reftex-toc)
2160 ("\C-cl" . reftex-label)
2161 ("\C-cr" . reftex-reference)
2162 ("\C-cc" . reftex-citation)
2163 ("\C-cv" . reftex-view-crossref)
2164 ("\C-cg" . reftex-grep-document)
2165 ("\C-cs" . reftex-search-document))
2166 do (define-key reftex-mode-map (car x) (cdr x))))
b849548d 2167
b849548d
CD
2168;;; =========================================================================
2169;;;
2170;;; Menu
a7ec1775
RS
2171
2172;; Define a menu for the menu bar if Emacs is running under X
2173
57157296
CD
2174(defvar reftex-isearch-minor-mode nil)
2175(make-variable-buffer-local 'reftex-isearch-minor-mode)
2176
f9ad2e24 2177(easy-menu-define reftex-mode-menu reftex-mode-map
a7ec1775 2178 "Menu used in RefTeX mode"
396e0b08 2179 `("Ref"
206c6f82 2180 ["Table of Contents" reftex-toc t]
3b919c9f 2181 ["Recenter TOC" reftex-toc-recenter t]
1c25ed90 2182 "--"
206c6f82
RS
2183 ["\\label" reftex-label t]
2184 ["\\ref" reftex-reference t]
2185 ["\\cite" reftex-citation t]
7c4d13cc
CD
2186 ("\\index"
2187 ["\\index" reftex-index t]
2188 ["\\index{THIS}" reftex-index-selection-or-word t]
2189 "--"
2190 ["Add THIS to Index Phrases" reftex-index-phrase-selection-or-word t]
2191 ["Visit Phrase Buffer" reftex-index-visit-phrases-buffer t]
2192 ["Apply Phrases to Region" reftex-index-phrases-apply-to-region t]
2193 "--"
2194 ["Display the Index" reftex-display-index t])
1c25ed90 2195 "--"
7c4d13cc 2196 ["View Crossref" reftex-view-crossref t]
1c25ed90 2197 "--"
396e0b08 2198 ("Parse Document"
1c25ed90
CD
2199 ["One File" reftex-parse-one reftex-enable-partial-scans]
2200 ["Entire Document" reftex-parse-all t]
baec1250 2201 ["Save to File" (reftex-access-parse-file 'write)
396e0b08 2202 (> (length (symbol-value reftex-docstruct-symbol)) 0)]
3b919c9f 2203 ["Restore from File" (reftex-access-parse-file 'restore) t])
2faef409 2204 ("Global Actions"
baec1250 2205 ["Search Whole Document" reftex-search-document t]
f3c18bd0 2206 ["Search Again" tags-loop-continue t]
baec1250
KH
2207 ["Replace in Document" reftex-query-replace-document t]
2208 ["Grep on Document" reftex-grep-document t]
1c25ed90 2209 "--"
3b919c9f 2210 ["Goto Label" reftex-goto-label t]
baec1250 2211 ["Find Duplicate Labels" reftex-find-duplicate-labels t]
2faef409 2212 ["Change Label and Refs" reftex-change-label t]
b849548d 2213 ["Renumber Simple Labels" reftex-renumber-simple-labels t]
1c25ed90 2214 "--"
f3c18bd0
CD
2215 ["Create BibTeX File" reftex-create-bibtex-file t]
2216 "--"
1c25ed90
CD
2217 ["Create TAGS File" reftex-create-tags-file t]
2218 "--"
f9ad2e24 2219 ["Save Document" reftex-save-all-document-buffers t])
1c25ed90 2220 "--"
b849548d 2221 ("Options"
921759ee
CD
2222 "PARSER"
2223 ["Partial Scans"
2224 (setq reftex-enable-partial-scans (not reftex-enable-partial-scans))
2225 :style toggle :selected reftex-enable-partial-scans]
2226 ["Auto-Save Parse Info"
2227 (setq reftex-save-parse-info (not reftex-save-parse-info))
2228 :style toggle :selected reftex-save-parse-info]
1c25ed90 2229 "--"
3b919c9f
CD
2230 "TOC RECENTER"
2231 ["Automatic Recenter" reftex-toggle-auto-toc-recenter
2232 :style toggle :selected reftex-toc-auto-recenter-timer]
2233 "--"
921759ee
CD
2234 "CROSSREF INFO"
2235 ["Automatic Info" reftex-toggle-auto-view-crossref
2236 :style toggle :selected reftex-auto-view-crossref-timer]
2237 ["...in Echo Area" (setq reftex-auto-view-crossref t)
2238 :style radio :selected (eq reftex-auto-view-crossref t)]
2239 ["...in Other Window" (setq reftex-auto-view-crossref 'window)
2240 :style radio :selected (eq reftex-auto-view-crossref 'window)]
1c25ed90 2241 "--"
921759ee 2242 "MISC"
4f595e15 2243 ["AUCTeX Interface" reftex-toggle-plug-into-AUCTeX
5d4ba9da
CD
2244 :style toggle :selected reftex-plug-into-AUCTeX]
2245 ["isearch whole document" reftex-isearch-minor-mode
2246 :style toggle :selected reftex-isearch-minor-mode])
921759ee 2247 ("Reference Style"
4f595e15
RA
2248 ,@(let (list item)
2249 (dolist (elt reftex-ref-style-alist)
2250 (setq elt (car elt)
2251 item (vector
2252 elt
2253 `(reftex-ref-style-toggle ,elt)
2254 :style 'toggle
2255 :selected `(member ,elt (reftex-ref-style-list))))
2256 (unless (member item list)
2257 (add-to-list 'list item t)))
2258 list))
921759ee
CD
2259 ("Citation Style"
2260 ,@(mapcar
1c25ed90 2261 (lambda (x)
3666daf6
CD
2262 (vector
2263 (capitalize (symbol-name (car x)))
2264 (list 'reftex-set-cite-format (list 'quote (car x)))
2265 :style 'radio :selected
2266 (list 'eq (list 'reftex-get-cite-format) (list 'quote (car x)))))
921759ee 2267 reftex-cite-format-builtin)
1c25ed90 2268 "--"
921759ee
CD
2269 "Sort Database Matches"
2270 ["Not" (setq reftex-sort-bibtex-matches nil)
2271 :style radio :selected (eq reftex-sort-bibtex-matches nil)]
2272 ["by Author" (setq reftex-sort-bibtex-matches 'author)
2273 :style radio :selected (eq reftex-sort-bibtex-matches 'author)]
2274 ["by Year" (setq reftex-sort-bibtex-matches 'year)
2275 :style radio :selected (eq reftex-sort-bibtex-matches 'year)]
2276 ["by Year, reversed" (setq reftex-sort-bibtex-matches 'reverse-year)
2277 :style radio :selected (eq reftex-sort-bibtex-matches 'reverse-year)])
1c25ed90
CD
2278 ("Index Style"
2279 ,@(mapcar
2280 (lambda (x)
3666daf6
CD
2281 (vector
2282 (capitalize (symbol-name (car x)))
2283 (list 'reftex-add-index-macros (list 'list (list 'quote (car x))))
2284 :style 'radio :selected
2285 (list 'memq (list 'quote (car x))
a2e43f12 2286 (list 'get 'reftex-docstruct-symbol
3666daf6 2287 (list 'quote 'reftex-index-macros-style)))))
1c25ed90 2288 reftex-index-macros-builtin))
3b919c9f
CD
2289 "--"
2290 ["Reset RefTeX Mode" reftex-reset-mode t]
1c25ed90 2291 "--"
2faef409 2292 ("Customize"
f9ad2e24 2293 ["Browse RefTeX Group" reftex-customize t]
1c25ed90 2294 "--"
a2e43f12 2295 ["Build Full Customize Menu" reftex-create-customize-menu
2faef409 2296 (fboundp 'customize-menu-create)])
2faef409
RS
2297 ("Documentation"
2298 ["Info" reftex-info t]
2299 ["Commentary" reftex-show-commentary t])))
a7ec1775 2300
b849548d
CD
2301(defun reftex-customize ()
2302 "Call the customize function with reftex as argument."
2303 (interactive)
2304 (customize-browse 'reftex))
2305
2306(defun reftex-create-customize-menu ()
2307 "Create a full customization menu for RefTeX, insert it into the menu."
2308 (interactive)
2309 (if (fboundp 'customize-menu-create)
2310 (progn
a2e43f12 2311 (easy-menu-change
3666daf6
CD
2312 '("Ref") "Customize"
2313 `(["Browse RefTeX group" reftex-customize t]
2314 "--"
2315 ,(customize-menu-create 'reftex)
2316 ["Set" Custom-set t]
2317 ["Save" Custom-save t]
2318 ["Reset to Current" Custom-reset-current t]
2319 ["Reset to Saved" Custom-reset-saved t]
2320 ["Reset to Standard Settings" Custom-reset-standard t]))
2321 (message "\"Ref\"-menu now contains full customization menu"))
b849548d
CD
2322 (error "Cannot expand menu (outdated version of cus-edit.el)")))
2323
4f595e15
RA
2324
2325;;; Misc
2326
b849548d
CD
2327(defun reftex-show-commentary ()
2328 "Use the finder to view the file documentation from `reftex.el'."
2329 (interactive)
b849548d
CD
2330 (finder-commentary "reftex.el"))
2331
7c4d13cc
CD
2332(defun reftex-info (&optional node)
2333 "Read documentation for RefTeX in the info system.
2334With optional NODE, go directly to that node."
b849548d 2335 (interactive)
a2e43f12 2336 (info (format "(reftex)%s" (or node ""))))
b849548d 2337
4f595e15
RA
2338(defun reftex-report-bug ()
2339 "Report a bug in RefTeX.
2340
2341Don't hesitate to report any problems or inaccurate documentation.
2342
2343If you don't have setup sending mail from (X)Emacs, please copy the
2344output buffer into your mail program, as it gives us important
2345information about your RefTeX version and configuration."
2346 (interactive)
2347 (require 'reporter)
2348 (let ((reporter-prompt-for-summary-p "Bug report subject: "))
2349 (reporter-submit-bug-report
e8ba2352 2350 "bug-auctex@gnu.org, bug-gnu-emacs@gnu.org"
4f595e15
RA
2351 reftex-version
2352 (list 'window-system
2353 'reftex-plug-into-AUCTeX)
2354 nil nil
2355 "Remember to cover the basics, that is, what you expected to happen and
2356what in fact did happen.
2357
735135f9 2358Check if the bug is reproducible with an up-to-date version of
4f595e15
RA
2359RefTeX available from http://www.gnu.org/software/auctex/.
2360
2361If the bug is triggered by a specific \(La\)TeX file, you should try
2362to produce a minimal sample file showing the problem and include it
2363in your report.
2364
2365Your bug report will be posted to the AUCTeX bug reporting list.
2366------------------------------------------------------------------------")))
2367
b849548d
CD
2368;;; Install the kill-buffer and kill-emacs hooks ------------------------------
2369
2370(add-hook 'kill-buffer-hook 'reftex-kill-buffer-hook)
845fc5e5
JB
2371(unless noninteractive
2372 (add-hook 'kill-emacs-hook 'reftex-kill-emacs-hook))
b849548d 2373
a7ec1775
RS
2374;;; Run Hook ------------------------------------------------------------------
2375
2376(run-hooks 'reftex-load-hook)
2377
2378;;; That's it! ----------------------------------------------------------------
2379
6b94c6ad 2380(setq reftex-tables-dirty t) ; in case this file is evaluated by hand
41116c5a
GM
2381
2382\f
2383;;; Start of automatically extracted autoloads.
2384\f
2385;;;### (autoloads nil "reftex-auc" "reftex-auc.el" "16f7e4bde671b5faee975a9ff10838e7")
2386;;; Generated autoloads from reftex-auc.el
2387
2388(autoload 'reftex-arg-label "reftex-auc" "\
2389Use `reftex-label', `reftex-reference' or AUCTeX's code to insert label arg.
2390What is being used depends upon `reftex-plug-into-AUCTeX'.
2391
2392\(fn OPTIONAL &optional PROMPT DEFINITION)" nil nil)
2393
2394(autoload 'reftex-arg-cite "reftex-auc" "\
2395Use `reftex-citation' or AUCTeX's code to insert a cite-key macro argument.
2396What is being used depends upon `reftex-plug-into-AUCTeX'.
2397
2398\(fn OPTIONAL &optional PROMPT DEFINITION)" nil nil)
2399
2400(autoload 'reftex-arg-index-tag "reftex-auc" "\
2401Prompt for an index tag with completion.
2402This is the name of an index, not the entry.
2403
2404\(fn OPTIONAL &optional PROMPT &rest ARGS)" nil nil)
2405
2406(autoload 'reftex-arg-index "reftex-auc" "\
2407Prompt for an index entry completing with known entries.
2408Completion is specific for just one index, if the macro or a tag
2409argument identify one of multiple indices.
2410
2411\(fn OPTIONAL &optional PROMPT &rest ARGS)" nil nil)
2412
2413(autoload 'reftex-plug-into-AUCTeX "reftex-auc" "\
2414
2415
2416\(fn)" nil nil)
2417
2418(autoload 'reftex-toggle-plug-into-AUCTeX "reftex-auc" "\
2419Toggle Interface between AUCTeX and RefTeX on and off.
2420
2421\(fn)" t nil)
2422
2423(autoload 'reftex-add-label-environments "reftex-auc" "\
2424Add label environment descriptions to `reftex-label-alist-style'.
2425The format of ENTRY-LIST is exactly like `reftex-label-alist'. See there
2426for details.
2427This function makes it possible to support RefTeX from AUCTeX style files.
2428The entries in ENTRY-LIST will be processed after the user settings in
2429`reftex-label-alist', and before the defaults (specified in
2430`reftex-default-label-alist-entries'). Any changes made to
2431`reftex-label-alist-style' will raise a flag to the effect that
2432the label information is recompiled on next use.
2433
2434\(fn ENTRY-LIST)" nil nil)
2435
2436(defalias 'reftex-add-to-label-alist 'reftex-add-label-environments)
2437
2438(autoload 'reftex-add-section-levels "reftex-auc" "\
2439Add entries to the value of `reftex-section-levels'.
2440The added values are kept local to the current document. The format
2441of ENTRY-LIST is a list of cons cells (\"MACRONAME\" . LEVEL). See
2442`reftex-section-levels' for an example.
2443
2444\(fn ENTRY-LIST)" nil nil)
2445
2446(autoload 'reftex-notice-new-section "reftex-auc" "\
2447
2448
2449\(fn)" nil nil)
2450
2451;;;***
2452\f
2453;;;### (autoloads nil "reftex-cite" "reftex-cite.el" "43a70b713b3cd8a225f03431400e54e6")
2454;;; Generated autoloads from reftex-cite.el
2455
2456(autoload 'reftex-default-bibliography "reftex-cite" "\
2457Return the expanded value of variable `reftex-default-bibliography'.
2458The expanded value is cached.
2459
2460\(fn)" nil nil)
2461
2462(autoload 'reftex-bib-or-thebib "reftex-cite" "\
2463Test if BibTeX or \begin{thebibliography} should be used for the citation.
2464Find the bof of the current file
2465
2466\(fn)" nil nil)
2467
2468(autoload 'reftex-get-bibfile-list "reftex-cite" "\
2469Return list of bibfiles for current document.
2470When using the chapterbib or bibunits package you should either
2471use the same database files everywhere, or separate parts using
2472different databases into different files (included into the mater file).
2473Then this function will return the applicable database files.
2474
2475\(fn)" nil nil)
2476
2477(autoload 'reftex-pop-to-bibtex-entry "reftex-cite" "\
2478Find BibTeX KEY in any file in FILE-LIST in another window.
2479If MARK-TO-KILL is non-nil, mark new buffer to kill.
2480If HIGHLIGHT is non-nil, highlight the match.
2481If ITEM in non-nil, search for bibitem instead of database entry.
2482If RETURN is non-nil, just return the entry and restore point.
2483
2484\(fn KEY FILE-LIST &optional MARK-TO-KILL HIGHLIGHT ITEM RETURN)" nil nil)
2485
2486(autoload 'reftex-end-of-bib-entry "reftex-cite" "\
2487
2488
2489\(fn ITEM)" nil nil)
2490
2491(autoload 'reftex-parse-bibtex-entry "reftex-cite" "\
2492Parse BibTeX ENTRY.
2493If ENTRY is nil then parse the entry in current buffer between FROM and TO.
2494If RAW is non-nil, keep double quotes/curly braces delimiting fields.
2495
2496\(fn ENTRY &optional FROM TO RAW)" nil nil)
2497
2498(autoload 'reftex-citation "reftex-cite" "\
2499Make a citation using BibTeX database files.
2500After prompting for a regular expression, scans the buffers with
2501bibtex entries (taken from the \\bibliography command) and offers the
2502matching entries for selection. The selected entry is formatted according
2503to `reftex-cite-format' and inserted into the buffer.
2504
2505If NO-INSERT is non-nil, nothing is inserted, only the selected key returned.
2506
2507FORMAT-KEY can be used to pre-select a citation format.
2508
2509When called with a `C-u' prefix, prompt for optional arguments in
2510cite macros. When called with a numeric prefix, make that many
2511citations. When called with point inside the braces of a `\\cite'
2512command, it will add another key, ignoring the value of
2513`reftex-cite-format'.
2514
2515The regular expression uses an expanded syntax: && is interpreted as `and'.
2516Thus, `aaaa&&bbb' matches entries which contain both `aaaa' and `bbb'.
2517While entering the regexp, completion on knows citation keys is possible.
2518`=' is a good regular expression to match all entries in all files.
2519
2520\(fn &optional NO-INSERT FORMAT-KEY)" t nil)
2521
2522(autoload 'reftex-citep "reftex-cite" "\
2523Call `reftex-citation' with a format selector `?p'.
2524
2525\(fn)" t nil)
2526
2527(autoload 'reftex-citet "reftex-cite" "\
2528Call `reftex-citation' with a format selector `?t'.
2529
2530\(fn)" t nil)
2531
2532(autoload 'reftex-make-cite-echo-string "reftex-cite" "\
2533Format a bibtex ENTRY for the echo area and cache the result.
2534
2535\(fn ENTRY DOCSTRUCT-SYMBOL)" nil nil)
2536
2537(autoload 'reftex-create-bibtex-file "reftex-cite" "\
2538Create a new BibTeX database BIBFILE with all entries referenced in document.
2539The command prompts for a filename and writes the collected
2540entries to that file. Only entries referenced in the current
2541document with any \\cite-like macros are used. The sequence in
2542the new file is the same as it was in the old database.
2543
2544Entries referenced from other entries must appear after all
2545referencing entries.
2546
2547You can define strings to be used as header or footer for the
2548created files in the variables `reftex-create-bibtex-header' or
2549`reftex-create-bibtex-footer' respectively.
2550
2551\(fn BIBFILE)" t nil)
2552
2553;;;***
2554\f
2555;;;### (autoloads nil "reftex-dcr" "reftex-dcr.el" "e90640fdd78f4404b29729bf7a5fce30")
2556;;; Generated autoloads from reftex-dcr.el
2557
2558(autoload 'reftex-view-crossref "reftex-dcr" "\
2559View cross reference of macro at point. Point must be on the KEY
2560argument. When at a `\\ref' macro, show corresponding `\\label'
2561definition, also in external documents (`xr'). When on a label, show
2562a locations where KEY is referenced. Subsequent calls find additional
2563locations. When on a `\\cite', show the associated `\\bibitem' macro or
2564the BibTeX database entry. When on a `\\bibitem', show a `\\cite' macro
2565which uses this KEY. When on an `\\index', show other locations marked
2566by the same index entry.
2567To define additional cross referencing items, use the option
2568`reftex-view-crossref-extra'. See also `reftex-view-crossref-from-bibtex'.
2569With one or two C-u prefixes, enforce rescanning of the document.
2570With argument 2, select the window showing the cross reference.
2571AUTO-HOW is only for the automatic crossref display and is handed through
2572to the functions `reftex-view-cr-cite' and `reftex-view-cr-ref'.
2573
2574\(fn &optional ARG AUTO-HOW FAIL-QUIETLY)" t nil)
2575
2576(autoload 'reftex-mouse-view-crossref "reftex-dcr" "\
2577View cross reference of \\ref or \\cite macro where you click.
2578If the macro at point is a \\ref, show the corresponding label definition.
2579If it is a \\cite, show the BibTeX database entry.
2580If there is no such macro at point, search forward to find one.
2581With argument, actually select the window showing the cross reference.
2582
2583\(fn EV)" t nil)
2584
2585(autoload 'reftex-toggle-auto-view-crossref "reftex-dcr" "\
2586Toggle the automatic display of crossref information in the echo area.
2587When active, leaving point idle in the argument of a \\ref or \\cite macro
2588will display info in the echo area.
2589
2590\(fn)" t nil)
2591
2592(autoload 'reftex-view-crossref-from-bibtex "reftex-dcr" "\
2593View location in a LaTeX document which cites the BibTeX entry at point.
2594Since BibTeX files can be used by many LaTeX documents, this function
2595prompts upon first use for a buffer in RefTeX mode. To reset this
2596link to a document, call the function with a prefix arg.
2597Calling this function several times find successive citation locations.
2598
2599\(fn &optional ARG)" t nil)
2600
2601;;;***
2602\f
2603;;;### (autoloads nil "reftex-global" "reftex-global.el" "a773b46e8b751bca3a82c991f81b5fdd")
2604;;; Generated autoloads from reftex-global.el
2605
2606(autoload 'reftex-create-tags-file "reftex-global" "\
2607Create TAGS file by running `etags' on the current document.
2608The TAGS file is also immediately visited with `visit-tags-table'.
2609
2610\(fn)" t nil)
2611
2612(autoload 'reftex-grep-document "reftex-global" "\
2613Run grep query through all files related to this document.
2614With prefix arg, force to rescan document.
2615No active TAGS table is required.
2616
2617\(fn GREP-CMD)" t nil)
2618
2619(autoload 'reftex-search-document "reftex-global" "\
2620Regexp search through all files of the current document.
2621Starts always in the master file. Stops when a match is found.
2622To continue searching for next match, use command \\[tags-loop-continue].
2623No active TAGS table is required.
2624
2625\(fn &optional REGEXP)" t nil)
2626
2627(autoload 'reftex-query-replace-document "reftex-global" "\
2628Do `query-replace-regexp' of FROM with TO over the entire document.
2629Third arg DELIMITED (prefix arg) means replace only word-delimited matches.
2630If you exit (\\[keyboard-quit], RET or q), you can resume the query replace
2631with the command \\[tags-loop-continue].
2632No active TAGS table is required.
2633
2634\(fn &optional FROM TO DELIMITED)" t nil)
2635
2636(autoload 'reftex-find-duplicate-labels "reftex-global" "\
2637Produce a list of all duplicate labels in the document.
2638
2639\(fn)" t nil)
2640
2641(autoload 'reftex-change-label "reftex-global" "\
2642Run `query-replace-regexp' of FROM with TO in all macro arguments.
2643Works on the entire multifile document.
2644If you exit (\\[keyboard-quit], RET or q), you can resume the query replace
2645with the command \\[tags-loop-continue].
2646No active TAGS table is required.
2647
2648\(fn &optional FROM TO)" t nil)
2649
2650(autoload 'reftex-renumber-simple-labels "reftex-global" "\
2651Renumber all simple labels in the document to make them sequentially.
2652Simple labels are the ones created by RefTeX, consisting only of the
2653prefix and a number. After the command completes, all these labels will
2654have sequential numbers throughout the document. Any references to
2655the labels will be changed as well. For this, RefTeX looks at the
2656arguments of any macros which either start or end in the string `ref'.
2657This command should be used with care, in particular in multifile
2658documents. You should not use it if another document refers to this
2659one with the `xr' package.
2660
2661\(fn)" t nil)
2662
2663(autoload 'reftex-save-all-document-buffers "reftex-global" "\
2664Save all documents associated with the current document.
2665The function is useful after a global action like replacing or renumbering
2666labels.
2667
2668\(fn)" t nil)
2669
2670(autoload 'reftex-isearch-minor-mode "reftex-global" "\
2671When on, isearch searches the whole document, not only the current file.
2672This minor mode allows isearch to search through all the files of
2673the current TeX document.
2674
2675With no argument, this command toggles
2676`reftex-isearch-minor-mode'. With a prefix argument ARG, turn
2677`reftex-isearch-minor-mode' on if ARG is positive, otherwise turn it off.
2678
2679\(fn &optional ARG)" t nil)
2680
2681;;;***
2682\f
2683;;;### (autoloads nil "reftex-index" "reftex-index.el" "ccf53e4cdf1d7280e4cdb54d23e81854")
2684;;; Generated autoloads from reftex-index.el
2685
2686(autoload 'reftex-index-selection-or-word "reftex-index" "\
2687Put selection or the word near point into the default index macro.
2688This uses the information in `reftex-index-default-macro' to make an index
2689entry. The phrase indexed is the current selection or the word near point.
2690When called with one `C-u' prefix, let the user have a chance to edit the
2691index entry. When called with 2 `C-u' as prefix, also ask for the index
2692macro and other stuff.
2693When called inside TeX math mode as determined by the `texmathp.el' library
2694which is part of AUCTeX, the string is first processed with the
2695`reftex-index-math-format', which see.
2696
2697\(fn &optional ARG PHRASE)" t nil)
2698
2699(autoload 'reftex-index "reftex-index" "\
2700Query for an index macro and insert it along with its arguments.
2701The index macros available are those defined in `reftex-index-macro' or
2702by a call to `reftex-add-index-macros', typically from an AUCTeX style file.
2703RefteX provides completion for the index tag and the index key, and
2704will prompt for other arguments.
2705
2706\(fn &optional CHAR KEY TAG SEL NO-INSERT)" t nil)
2707
2708(autoload 'reftex-index-complete-tag "reftex-index" "\
2709
2710
2711\(fn &optional ITAG OPT-ARGS)" nil nil)
2712
2713(autoload 'reftex-index-select-tag "reftex-index" "\
2714
2715
2716\(fn)" nil nil)
2717
2718(autoload 'reftex-index-complete-key "reftex-index" "\
2719
2720
2721\(fn &optional TAG OPTIONAL INITIAL)" nil nil)
2722
2723(autoload 'reftex-index-show-entry "reftex-index" "\
2724
2725
2726\(fn DATA &optional NO-REVISIT)" nil nil)
2727
2728(autoload 'reftex-display-index "reftex-index" "\
2729Display a buffer with an index compiled from the current document.
2730When the document has multiple indices, first prompts for the correct one.
2731When index support is turned off, offer to turn it on.
2732With one or two `C-u' prefixes, rescan document first.
2733With prefix 2, restrict index to current document section.
2734With prefix 3, restrict index to region.
2735
2736\(fn &optional TAG OVERRIDING-RESTRICTION REDO &rest LOCATIONS)" t nil)
2737
2738(autoload 'reftex-index-phrase-selection-or-word "reftex-index" "\
2739Add current selection or word at point to the phrases buffer.
2740When you are in transient-mark-mode and the region is active, the
2741selection will be used - otherwise the word at point.
2742You get a chance to edit the entry in the phrases buffer - finish with
2743`C-c C-c'.
2744
2745\(fn ARG)" t nil)
2746
2747(autoload 'reftex-index-visit-phrases-buffer "reftex-index" "\
2748Switch to the phrases buffer, initialize if empty.
2749
2750\(fn)" t nil)
2751
2752(autoload 'reftex-index-phrases-mode "reftex-index" "\
2753Major mode for managing the Index phrases of a LaTeX document.
2754This buffer was created with RefTeX.
2755
2756To insert new phrases, use
2757 - `C-c \\' in the LaTeX document to copy selection or word
2758 - `\\[reftex-index-new-phrase]' in the phrases buffer.
2759
2760To index phrases use one of:
2761
2762\\[reftex-index-this-phrase] index current phrase
2763\\[reftex-index-next-phrase] index next phrase (or N with prefix arg)
2764\\[reftex-index-all-phrases] index all phrases
2765\\[reftex-index-remaining-phrases] index current and following phrases
2766\\[reftex-index-region-phrases] index the phrases in the region
2767
2768You can sort the phrases in this buffer with \\[reftex-index-sort-phrases].
2769To display information about the phrase at point, use \\[reftex-index-phrases-info].
2770
2771For more information see the RefTeX User Manual.
2772
2773Here are all local bindings.
2774
2775\\{reftex-index-phrases-mode-map}
2776
2777\(fn)" t nil)
2778
2779;;;***
2780\f
9a214b98 2781;;;### (autoloads nil "reftex-parse" "reftex-parse.el" "69a531bd0ac3f97f076b7dda4ec2304d")
41116c5a
GM
2782;;; Generated autoloads from reftex-parse.el
2783
2784(autoload 'reftex-parse-one "reftex-parse" "\
2785Re-parse this file.
2786
2787\(fn)" t nil)
2788
2789(autoload 'reftex-parse-all "reftex-parse" "\
2790Re-parse entire document.
2791
2792\(fn)" t nil)
2793
2794(autoload 'reftex-do-parse "reftex-parse" "\
2795Do a document rescan.
2796When allowed, do only a partial scan from FILE.
2797
2798\(fn RESCAN &optional FILE)" nil nil)
2799
2800(autoload 'reftex-everything-regexp "reftex-parse" "\
2801
2802
2803\(fn)" nil nil)
2804
2805(autoload 'reftex-all-document-files "reftex-parse" "\
2806Return a list of all files belonging to the current document.
2807When RELATIVE is non-nil, give file names relative to directory
2808of master file.
2809
2810\(fn &optional RELATIVE)" nil nil)
2811
2812(autoload 'reftex-locate-bibliography-files "reftex-parse" "\
2813Scan buffer for bibliography macros and return file list.
2814
2815\(fn MASTER-DIR &optional FILES)" nil nil)
2816
2817(autoload 'reftex-section-info "reftex-parse" "\
2818Return a section entry for the current match.
2819Careful: This function expects the match-data to be still in place!
2820
2821\(fn FILE)" nil nil)
2822
2823(autoload 'reftex-ensure-index-support "reftex-parse" "\
2824When index support is turned off, ask to turn it on and
2825set the current prefix argument so that `reftex-access-scan-info'
2826will rescan the entire document.
2827
2828\(fn &optional ABORT)" nil nil)
2829
2830(autoload 'reftex-index-info-safe "reftex-parse" "\
2831
2832
2833\(fn FILE)" nil nil)
2834
2835(autoload 'reftex-index-info "reftex-parse" "\
2836Return an index entry for the current match.
2837Careful: This function expects the match-data to be still in place!
2838
2839\(fn FILE)" nil nil)
2840
2841(autoload 'reftex-short-context "reftex-parse" "\
2842Get about one line of useful context for the label definition at point.
2843
2844\(fn ENV PARSE &optional BOUND DERIVE)" nil nil)
2845
2846(autoload 'reftex-where-am-I "reftex-parse" "\
2847Return the docstruct entry above point.
2848Actually returns a cons cell in which the cdr is a flag indicating
2849if the information is exact (t) or approximate (nil).
2850
2851\(fn)" nil nil)
2852
2853(autoload 'reftex-notice-new "reftex-parse" "\
2854Hook to handshake with RefTeX after something new has been inserted.
2855
2856\(fn &optional N FORCE)" nil nil)
2857
2858(autoload 'reftex-what-macro-safe "reftex-parse" "\
2859Call `reftex-what-macro' with special syntax table.
2860
2861\(fn WHICH &optional BOUND)" nil nil)
2862
2863(autoload 'reftex-what-macro "reftex-parse" "\
2864Find out if point is within the arguments of any TeX-macro.
2865The return value is either (\"\\macro\" . (point)) or a list of them.
2866
2867If WHICH is nil, immediately return nil.
2868If WHICH is 1, return innermost enclosing macro.
2869If WHICH is t, return list of all macros enclosing point.
2870If WHICH is a list of macros, look only for those macros and return the
2871 name of the first macro in this list found to enclose point.
2872If the optional BOUND is an integer, bound backwards directed
2873 searches to this point. If it is nil, limit to nearest \\section -
2874 like statement.
2875
2876This function is pretty stable, but can be fooled if the text contains
2877things like \\macro{aa}{bb} where \\macro is defined to take only one
2878argument. As RefTeX cannot know this, the string \"bb\" would still be
2879considered an argument of macro \\macro.
2880
2881\(fn WHICH &optional BOUND)" nil nil)
2882
2883(autoload 'reftex-what-environment "reftex-parse" "\
2884Find out if point is inside a LaTeX environment.
2885The return value is (e.g.) either (\"equation\" . (point)) or a list of
2886them.
2887
2888If WHICH is nil, immediately return nil.
2889If WHICH is 1, return innermost enclosing environment.
2890If WHICH is t, return list of all environments enclosing point.
2891If WHICH is a list of environments, look only for those environments and
2892 return the name of the first environment in this list found to enclose
2893 point.
2894
2895If the optional BOUND is an integer, bound backwards directed searches to
2896this point. If it is nil, limit to nearest \\section - like statement.
2897
2898\(fn WHICH &optional BOUND)" nil nil)
2899
2900(autoload 'reftex-what-special-env "reftex-parse" "\
2901Run the special environment parsers and return the matches.
2902
2903The return value is (e.g.) either (\"my-parser-function\" . (point))
2904or a list of them.
2905
2906If WHICH is nil, immediately return nil.
2907If WHICH is 1, return innermost enclosing environment.
2908If WHICH is t, return list of all environments enclosing point.
2909If WHICH is a list of environments, look only for those environments and
2910 return the name of the first environment in this list found to enclose
2911 point.
2912
2913\(fn WHICH &optional BOUND)" nil nil)
2914
2915(autoload 'reftex-nth-arg "reftex-parse" "\
2916Return the Nth following {} or [] parentheses content.
2917OPT-ARGS is a list of argument numbers which are optional.
2918
2919\(fn N &optional OPT-ARGS)" nil nil)
2920
2921(autoload 'reftex-move-over-touching-args "reftex-parse" "\
2922
2923
2924\(fn)" nil nil)
2925
2926(autoload 'reftex-init-section-numbers "reftex-parse" "\
2927Initialize the section numbers with zeros or with what is found in the TOC-ENTRY.
2928
2929\(fn &optional TOC-ENTRY APPENDIX)" nil nil)
2930
2931(autoload 'reftex-section-number "reftex-parse" "\
2932Return a string with the current section number.
2933When LEVEL is non-nil, increase section numbers on that level.
2934
2935\(fn &optional LEVEL STAR)" nil nil)
2936
2937;;;***
2938\f
cae9b33d 2939;;;### (autoloads nil "reftex-ref" "reftex-ref.el" "3ad5bfbb747c7f8c5cd5b30e3b6fc342")
41116c5a
GM
2940;;; Generated autoloads from reftex-ref.el
2941
2942(autoload 'reftex-label-location "reftex-ref" "\
2943Return the environment or macro which determines the label type at point.
2944If optional BOUND is an integer, limit backward searches to that point.
2945
2946\(fn &optional BOUND)" nil nil)
2947
2948(autoload 'reftex-label-info-update "reftex-ref" "\
2949
2950
2951\(fn CELL)" nil nil)
2952
2953(autoload 'reftex-label-info "reftex-ref" "\
2954
2955
2956\(fn LABEL &optional FILE BOUND DERIVE ENV-OR-MAC)" nil nil)
2957
2958(autoload 'reftex-label "reftex-ref" "\
2959Insert a unique label. Return the label.
2960If ENVIRONMENT is given, don't bother to find out yourself.
2961If NO-INSERT is non-nil, do not insert label into buffer.
2962With prefix arg, force to rescan document first.
2963When you are prompted to enter or confirm a label, and you reply with
2964just the prefix or an empty string, no label at all will be inserted.
2965A new label is also recorded into the label list.
2966This function is controlled by the settings of reftex-insert-label-flags.
2967
2968\(fn &optional ENVIRONMENT NO-INSERT)" t nil)
2969
2970(autoload 'reftex-reference "reftex-ref" "\
2971Make a LaTeX reference. Look only for labels of a certain TYPE.
2972With prefix arg, force to rescan buffer for labels. This should only be
2973necessary if you have recently entered labels yourself without using
2974reftex-label. Rescanning of the buffer can also be requested from the
2975label selection menu.
2976The function returns the selected label or nil.
2977If NO-INSERT is non-nil, do not insert \\ref command, just return label.
2978When called with 2 C-u prefix args, disable magic word recognition.
2979
2980\(fn &optional TYPE NO-INSERT CUT)" t nil)
2981
2982(autoload 'reftex-query-label-type "reftex-ref" "\
2983
2984
2985\(fn)" nil nil)
2986
2987(autoload 'reftex-show-label-location "reftex-ref" "\
2988
2989
2990\(fn DATA FORWARD NO-REVISIT &optional STAY ERROR)" nil nil)
2991
2992(autoload 'reftex-goto-label "reftex-ref" "\
2993Prompt for a label (with completion) and jump to the location of this label.
2994Optional prefix argument OTHER-WINDOW goes to the label in another window.
2995
2996\(fn &optional OTHER-WINDOW)" t nil)
2997
2998;;;***
2999\f
3000;;;### (autoloads nil "reftex-sel" "reftex-sel.el" "086c2dd94aedc95620c5e972ad4c741a")
3001;;; Generated autoloads from reftex-sel.el
3002
3003(autoload 'reftex-select-label-mode "reftex-sel" "\
3004Major mode for selecting a label in a LaTeX document.
3005This buffer was created with RefTeX.
3006It only has a meaningful keymap when you are in the middle of a
3007selection process.
3008To select a label, move the cursor to it and press RET.
3009Press `?' for a summary of important key bindings.
3010
3011During a selection process, these are the local bindings.
3012
3013\\{reftex-select-label-mode-map}
3014
3015\(fn)" t nil)
3016
3017(autoload 'reftex-select-bib-mode "reftex-sel" "\
3018Major mode for selecting a citation key in a LaTeX document.
3019This buffer was created with RefTeX.
3020It only has a meaningful keymap when you are in the middle of a
3021selection process.
3022In order to select a citation, move the cursor to it and press RET.
3023Press `?' for a summary of important key bindings.
3024
3025During a selection process, these are the local bindings.
3026
3027\\{reftex-select-label-mode-map}
3028
3029\(fn)" t nil)
3030
3031(autoload 'reftex-get-offset "reftex-sel" "\
3032
3033
3034\(fn BUF HERE-AM-I &optional TYPEKEY TOC INDEX FILE)" nil nil)
3035
3036(autoload 'reftex-insert-docstruct "reftex-sel" "\
3037
3038
3039\(fn BUF TOC LABELS INDEX-ENTRIES FILES CONTEXT COUNTER SHOW-COMMENTED HERE-I-AM XR-PREFIX TOC-BUFFER)" nil nil)
3040
3041(autoload 'reftex-find-start-point "reftex-sel" "\
3042
3043
3044\(fn FALLBACK &rest LOCATIONS)" nil nil)
3045
3046(autoload 'reftex-select-item "reftex-sel" "\
3047
3048
3049\(fn REFTEX-SELECT-PROMPT HELP-STRING KEYMAP &optional OFFSET CALL-BACK CB-FLAG)" nil nil)
3050
3051;;;***
3052\f
ba8b8451 3053;;;### (autoloads nil "reftex-toc" "reftex-toc.el" "0364fac43b02eee087ee4cbe37f7f76c")
41116c5a
GM
3054;;; Generated autoloads from reftex-toc.el
3055
3056(autoload 'reftex-toc "reftex-toc" "\
3057Show the table of contents for the current document.
3058When called with a raw C-u prefix, rescan the document first.
3059
3060\(fn &optional REBUILD REUSE)" t nil)
3061
3062(autoload 'reftex-toc-recenter "reftex-toc" "\
3063Display the TOC window and highlight line corresponding to current position.
3064
3065\(fn &optional ARG)" t nil)
3066
3067(autoload 'reftex-toggle-auto-toc-recenter "reftex-toc" "\
3068Toggle the automatic recentering of the TOC window.
3069When active, leaving point idle will make the TOC window jump to the correct
3070section.
3071
3072\(fn)" t nil)
3073
3074;;;***
3075\f
3076;;; End of automatically extracted autoloads.
3077
a2e43f12 3078(provide 'reftex)
396e0b08 3079
c52bdfca 3080;;; reftex.el ends here