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