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