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