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