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