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