aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorCarsten Dominik1998-08-10 13:53:02 +0000
committerCarsten Dominik1998-08-10 13:53:02 +0000
commitb849548dd41855b0c1bf21543c2cc08af3eeaf94 (patch)
tree63ee9f8b17697c16c9a27eb252dfad9bd899f8f6
parentd6bccf099ec4460b766b7041a9fb16d489ac795c (diff)
downloademacs-b849548dd41855b0c1bf21543c2cc08af3eeaf94.tar.gz
emacs-b849548dd41855b0c1bf21543c2cc08af3eeaf94.zip
(reftex-reparse-document): Renamed from `reftex-parse-document'.
(reftex-string-to-label): Splitting string on "\W+" now (better for other languages). (reftex-expand-path, reftex-find-file-on-path, reftex-access-search-path, reftex-find-tex-file, reftex-find-bib-file): Function rewritten. (reftex-toc-show-insertion-point, reftex-plug-into-AUCTeX-now, reftex-toggle-auto-view-crossref): New commands. (reftex-abbreviate-title, reftex-view-crossref-when-idle, reftex-echo-ref, reftex-echo-cite, reftex-make-cite-echo-string, reftex-verified-face, reftex-new-section-callback, reftex-change-hook, reftex-figure-out-cite-format, reftex-restrict-bib-matches, reftex-ensure-compiled-variables, reftex-select-with-char): New functions. (reftex-delete-list): Function removed (reftex-cache-variables): New constant. (reftex-bibfile-ignore-regexps): Renamed from `reftex-bibfile-ignore-list'. (reftex-locate-bibliography-files): Use changed option `reftex-bibfile-ignore-regexps'. (reftex-reset-mode): Also remove parse file. (reftex-view-crossref): No longer displays when inside comments. (reftex-show-entry): Adapted for new outline mode. (reftex-select-label-mode): set syntax table. (reftex-compile-variables): renamed from reftex-compute-ref-cite-tables. Now caches computed values for different documents. (reftex-label-alist-external-add-ons): Variable removed. Functionality went into a property of the `reftex-docstruct-symbol'. (reftex-add-to-label-alist): Use property `reftex-label-alist-style'. (reftex-set-cite-format, reftex-get-cite-format, (reftex-query-label-type, reftex-select-external-document, reftex-citation): Use `reftex-select-with-char'. (reftex-arg-label,reftex-arg-cite): Now actually works :-). (reftex-parse-bibtex-entry): buffer "*RefTeX-scratch*" renamed to " *RefTeX-scratch*", to make is a hidden buffer. (reftex-make-and-insert-label-list): Checking if note is a string. (reftex-access-parse-file): Catch exceptions while loading the file. (reftex-keep-temporary-buffers, reftex-initialize-temporary-buffers): Customization group changed. (reftex-auto-view-crossref): New option. (reftex-auto-view-crossref-timer): New variable. (reftex-refontify): Adapted for future versions of x-symbol. (reftex-make-and-insert-label-list): Set modification flag when updating an entry. Add face property to toc line. (reftex-do-parse): Set modification flag, do not write parse file. (reftex-label): Set the modification flag when adding to docstruct list. (reftex-access-parse-file): Clear the modification flag. (reftex-kill-buffer-hook, reftex-kill-emacs-hook): New hooks. Installed in `kill-buffer-hook' and `kill-emacs-hook'. (reftex-toc): Add face property to section heading. (reftex-section-info): No longer add face property. (reftex-pop-to-bibtex-entry): Can use echo area for display. (reftex-view-crossref): Additional argument ECHO. (reftex-format-citation): %T and %B for abbreviated titles. (reftex-what-macro): WHICH can also be 1 to get just one enclosing macro. (reftex-customize): Removed check for old custom versions. (reftex-do-citation): cleaned up.
-rw-r--r--lisp/textmodes/reftex.el4889
1 files changed, 2923 insertions, 1966 deletions
diff --git a/lisp/textmodes/reftex.el b/lisp/textmodes/reftex.el
index b37cee9868e..e517900fb08 100644
--- a/lisp/textmodes/reftex.el
+++ b/lisp/textmodes/reftex.el
@@ -33,11 +33,11 @@
33;; access to any part of a document. 33;; access to any part of a document.
34;; 34;;
35;; 35;;
36;; To turn RefTeX Minor Mode on and off in a particular buffer, use 36;; To turn RefTeX Mode on and off in a particular buffer, use
37;; `M-x reftex-mode'. 37;; `M-x reftex-mode'.
38;; 38;;
39;; To turn on RefTeX Minor Mode for all LaTeX files, add one of the 39;; To turn on RefTeX Mode for all LaTeX files, add one of the following
40;; following lines to your .emacs file: 40;; lines to your .emacs file:
41;; 41;;
42;; (add-hook 'LaTeX-mode-hook 'turn-on-reftex) ; with AUCTeX LaTeX mode 42;; (add-hook 'LaTeX-mode-hook 'turn-on-reftex) ; with AUCTeX LaTeX mode
43;; (add-hook 'latex-mode-hook 'turn-on-reftex) ; with Emacs latex mode 43;; (add-hook 'latex-mode-hook 'turn-on-reftex) ; with Emacs latex mode
@@ -60,70 +60,72 @@
60;; RefTeX in a Nutshell 60;; RefTeX in a Nutshell
61;; ==================== 61;; ====================
62;; 62;;
63;; 1. Labels and References 63;; 1. Labels and References
64;; RefTeX distinguishes labels for different environments. It always 64;; RefTeX distinguishes labels for different environments. It knows
65;; knows if a certain label references a figure, table etc.. You can 65;; about all standard environments (and many others), and can be
66;; configure RefTeX to recognize any additional labeled environments 66;; configured to recognize any additional labeled environments you
67;; you have defined yourself (variable REFTEX-LABEL-ALIST). 67;; have defined yourself (variable REFTEX-LABEL-ALIST().
68;; - Creating Labels 68;;
69;; Type `C-c (' (`reftex-label') to insert a label at point. 69;; * Creating Labels
70;; RefTeX will either 70;; Type `C-c (' (`reftex-label') to insert a label at point.
71;; - derive a label from context (default for section labels) 71;; RefTeX will either
72;; 72;; - derive a label from context (default for section labels)
73;; - insert a simple label consisting of a prefix and a number 73;; - prompt for a label string (default for figures and
74;; (default for equations, enumerate items, and footnotes) 74;; tables) or
75;; or 75;; - insert a simple label made of a prefix and a number (all
76;; 76;; other environments).
77;; - prompt for a label string (figures and tables). 77;; Which labels are created how is configurable (variable
78;; 78;; REFTEX-INSERT-LABEL-FLAGS).
79;; Which labels are created how is configurable (variable 79;;
80;; REFTEX-INSERT-LABEL-FLAGS). 80;; * Referencing Labels
81;; 81;; In order to make a reference, type `C-c )'
82;; - Referencing Labels 82;; (`reftex-reference'). This shows an outline of the document
83;; Referencing labels is a snap and I promise you'll love it. 83;; with all labels of a certain type (figure, equation,...) and
84;; In order to make a reference, type `C-c )' 84;; context of the label definition. Selecting a label inserts a
85;; (`reftex-reference'). This shows an outline of the document 85;; `\ref{LABEL}' macro into the original buffer.
86;; with all labels of a certain type (figure, equation,...) and 86;;
87;; context of the label definition. Selecting one of the labels 87;; 2. Citations
88;; inserts a `\ref' macro into the original buffer. Online help 88;; After typing `C-c [' (`reftex-citation'), RefTeX will let you
89;; during the selection is available with `?'. 89;; specify a regular expression to search in current BibTeX database
90;; 90;; files (as specified in the `\bibliography' command) and pull out a
91;; 2. Citations 91;; list of matches for you to choose from. The list is *formatted*
92;; After typing `C-c [' (`reftex-citation'), RefTeX will let you 92;; and sorted. The selected article is referenced as `\cite{KEY}'
93;; specify a regular expression to search in current BibTeX database 93;; (customizable with variable REFTEX-CITE-FORMAT).
94;; files (as specified in the `\bibliography' command) and pull out a 94;;
95;; formatted list of matches for you to choose from. The list is 95;; 3. Viewing Cross References
96;; *formatted* and sorted, thus much easier to read than the raw 96;; When no other message occupies the echo area and point is idle on
97;; database entries. The text inserted into the buffer is by default 97;; the argument of a `\ref' or `\cite' macro, the echo area will
98;; just `\cite{KEY}', but can also contain author names and the year 98;; display information about the citation/cross reference.
99;; in a configurable way (variable REFTEX-CITE-FORMAT). 99;; With point on or anywhere before such a macro, press `C-c &'
100;; 100;; (`reftex-view-crossref'), or click with `S-mouse-2' on the macro
101;; 3. Viewing Cross References 101;; argument. This will display the corresponding label definition or
102;; With point on or anywhere before a `\ref' or `\cite' macro, press 102;; BibTeX database entry in another window.
103;; `C-c &' (`reftex-view-crossref'). This will display the 103;;
104;; corresponding label definition or BibTeX database entry in another 104;; 4. Table of Contents
105;; window. 105;; Typing `C-c =' (`reftex-toc') will show a table of contents of the
106;; 106;; document. From that buffer, you can jump quickly to every part of
107;; 4. Table of Contents 107;; your document.
108;; Typing `C-c =' (`reftex-toc') will show a table of contents of the 108;;
109;; document. From that buffer, you can jump quickly to every part of 109;; 5. Multifile Documents
110;; your document. 110;; Multifile Documents are fully supported. RefTeX will provide cross
111;; 111;; referencing information from all files which are part of the
112;; 5. Multifile Documents 112;; document, and even across document borders (`xr.sty').
113;; Multifile Domuments are fully supported. RefTeX will provide cross 113;;
114;; referencing information from all files which are part of the 114;; 6. Document Parsing
115;; document. You may also use it to reference labels in external 115;; RefTeX needs to parse the document in order to find labels and
116;; documents (in cooperation with the LaTeX package `xr'). 116;; other information. It does it automatically once, when you start
117;; 117;; working with a document. RefTeX updates its lists internally when
118;; 6. Document Parsing 118;; you make a new label with `reftex-label'. To enforce reparsing,
119;; RefTeX needs to parse the document in order to find labels and 119;; call any of the commands described above with a raw `C-u' prefix,
120;; other information. It will do it automatically once, when you 120;; or press the `r' key in the label selection buffer or the table of
121;; start working with a document. Re-parsing should not be necessary 121;; contents buffer.
122;; too often since RefTeX updates its lists internally when you make 122;;
123;; a new label with `reftex-label'. To enforce reparsing, call any 123;; 7. Speed Issues
124;; of the commands described above with a raw `C-u' prefix, or press 124;; Useful settings to make RefTeX faster for large (multifile)
125;; the `r' key in the label selection buffer or the table of contents 125;; documents include:
126;; buffer. 126;; (setq reftex-enable-partial-scans t)
127;; (setq reftex-save-parse-info t)
128;; (setq reftex-use-multiple-selection-buffers t)
127;; 129;;
128;;--------------------------------------------------------------------------- 130;;---------------------------------------------------------------------------
129;; 131;;
@@ -139,8 +141,9 @@
139;; http://www.strw.leidenuniv.nl/~dominik/Tools/ 141;; http://www.strw.leidenuniv.nl/~dominik/Tools/
140;; ftp://strw.leidenuniv.nl/pub/dominik/ 142;; ftp://strw.leidenuniv.nl/pub/dominik/
141;; 143;;
142;; The version at this site is compatible with Emacs 19 - the version 144;; At that site you can also get version 3.22 of RefTeX which is still
143;; distributed with Emacs 20 is not. 145;; compatible with Emacs 19. The file you are reading now as well as the
146;; ones distributed with Emacs 20 are not.
144;; 147;;
145;; THANKS TO: 148;; THANKS TO:
146;; --------- 149;; ---------
@@ -153,9 +156,6 @@
153;; Robin Socha, Richard Stanton, Allan Strand, Jan Vroonhof, 156;; Robin Socha, Richard Stanton, Allan Strand, Jan Vroonhof,
154;; Christoph Wedler. 157;; Christoph Wedler.
155;; 158;;
156;; The view crossref feature was inspired by the similar function in
157;; Peter Galbraith's bib-cite.el.
158;;
159;; Finally thanks to Uwe Bolick who first got me (some years ago) into 159;; Finally thanks to Uwe Bolick who first got me (some years ago) into
160;; supporting LaTeX labels and references with an Editor (which was 160;; supporting LaTeX labels and references with an Editor (which was
161;; MicroEmacs at the time). 161;; MicroEmacs at the time).
@@ -168,8 +168,6 @@
168(eval-when-compile (require 'cl)) 168(eval-when-compile (require 'cl))
169 169
170;; Stuff that needs to be there when we use defcustom 170;; Stuff that needs to be there when we use defcustom
171;; --------------------------------------------------
172
173(require 'custom) 171(require 'custom)
174 172
175(defvar reftex-tables-dirty t 173(defvar reftex-tables-dirty t
@@ -180,13 +178,9 @@
180 (setq reftex-tables-dirty t) 178 (setq reftex-tables-dirty t)
181 (set symbol value))) 179 (set symbol value)))
182 180
183(eval-and-compile 181;;; ======================================================================
184 (defmacro reftex-fp (n) 182;;;
185 (if (fboundp 'forward-point) 183;;; Configuration Section
186 (list 'forward-point n)
187 (list '+ '(point) n))))
188
189;;; Begin of Configuration Section ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
190 184
191;; Define the two constants which are needed during compilation 185;; Define the two constants which are needed during compilation
192 186
@@ -248,30 +242,34 @@
248 ;; The LaTeX core stuff 242 ;; The LaTeX core stuff
249 (LaTeX "LaTeX default environments" 243 (LaTeX "LaTeX default environments"
250 (("section" ?s "sec:" "~\\ref{%s}" (nil . t) 244 (("section" ?s "sec:" "~\\ref{%s}" (nil . t)
251 (regexp "Parts?" "Chapters?" "Chap\\." "Sections?" "Sect?\\." 245 (regexp "parts?" "chapters?" "chap\\." "sections?" "sect?\\."
252 "Paragraphs?" "Par\\." 246 "paragraphs?" "par\\."
253 "\\\\S" "\247" "Teile?" "Kapitel" "Kap\\." "Abschnitte?" 247 "\\\\S" "\247" "Teile?" "Kapitel" "Kap\\." "Abschnitte?"
254 "Appendi\\(x\\|ces\\)" "App\\." "Anh\"?ange?" "Anh\\.")) 248 "appendi\\(x\\|ces\\)" "App\\." "Anh\"?ange?" "Anh\\."))
255 249
256 ("enumerate" ?i "item:" "~\\ref{%s}" item 250 ("enumerate" ?i "item:" "~\\ref{%s}" item
257 (regexp "Items?" "Punkte?")) 251 (regexp "items?" "Punkte?"))
258 252
259 ("equation" ?e "eq:" "~(\\ref{%s})" t 253 ("equation" ?e "eq:" "~(\\ref{%s})" t
260 (regexp "Equations?" "Eqs?\\." "Eqn\\." "Gleichung\\(en\\)?" "Gl\\.")) 254 (regexp "equations?" "eqs?\\." "eqn\\." "Gleichung\\(en\\)?" "Gl\\."))
261 ("eqnarray" ?e "eq:" nil eqnarray-like) 255 ("eqnarray" ?e "eq:" nil eqnarray-like)
262 256
263 ("figure" ?f "fig:" "~\\ref{%s}" caption 257 ("figure" ?f "fig:" "~\\ref{%s}" caption
264 (regexp "Figure[sn]?" "Figs?\\." "Abbildung\\(en\\)?" "Abb\\.")) 258 (regexp "figure?[sn]?" "figs?\\." "Abbildung\\(en\\)?" "Abb\\."))
265 ("figure*" ?f nil nil caption) 259 ("figure*" ?f nil nil caption)
266 260
267 ("table" ?t "tab:" "~\\ref{%s}" caption 261 ("table" ?t "tab:" "~\\ref{%s}" caption
268 (regexp "Tables?" "Tab\\." "Tabellen?")) 262 (regexp "tables?" "tab\\." "Tabellen?"))
269 ("table*" ?t nil nil caption) 263 ("table*" ?t nil nil caption)
270 264
271 ("\\footnote[]{}" ?n "note:" "~\\ref{%s}" 2 265 ("\\footnote[]{}" ?n "note:" "~\\ref{%s}" 2
272 (regexp "Footnotes?" "Notes?")) 266 (regexp "footnotes?" "notes?" "Anmerkung\\(en\\)?" "Anm\\."))
273 267
274 ("any" ?\ " " "~\\ref{%s}" nil))) 268 ("any" ?\ " " "~\\ref{%s}" nil)
269
270 ;; The label macro is hard coded, but it *could* be defined like this:
271 ;;("\\label{*}" nil nil nil nil)
272 ))
275 273
276 ) 274 )
277 "The default label environment descriptions. 275 "The default label environment descriptions.
@@ -289,7 +287,9 @@ distribution. Mixed-case symbols are convenience aliases.")
289 (?p . "\\citep{%l}") 287 (?p . "\\citep{%l}")
290 (?P . "\\citep*{%l}") 288 (?P . "\\citep*{%l}")
291 (?e . "\\citep[e.g.][]{%l}") 289 (?e . "\\citep[e.g.][]{%l}")
290 (?s . "\\citep[see][]{%l}")
292 (?a . "\\citeauthor{%l}") 291 (?a . "\\citeauthor{%l}")
292 (?A . "\\citeauthor*{%l}")
293 (?y . "\\citeyear{%l}"))) 293 (?y . "\\citeyear{%l}")))
294 (harvard "The Harvard package" 294 (harvard "The Harvard package"
295 ((?\C-m . "\\cite{%l}") 295 ((?\C-m . "\\cite{%l}")
@@ -319,9 +319,8 @@ distribution. Mixed-case symbols are convenience aliases.")
319 (?p . "(%2a %y\\nocite{%l})"))) 319 (?p . "(%2a %y\\nocite{%l})")))
320 (locally "Full info in parenthesis" 320 (locally "Full info in parenthesis"
321 "(%2a %y, %j %v, %P, %e: %b, %u, %s %<)") 321 "(%2a %y, %j %v, %P, %e: %b, %u, %s %<)")
322 ;; undocumented feature: `%<' kills white space and punctuation locally.
323 ) 322 )
324 "Builtin versions of for the citation format. 323 "Builtin versions of the citation format.
325The following conventions are valid for all alist entries: 324The following conventions are valid for all alist entries:
326`?\C-m' should always point to a straight \\cite{%l} macro. 325`?\C-m' should always point to a straight \\cite{%l} macro.
327`?t' should point to a textual citation (citation as a noun). 326`?t' should point to a textual citation (citation as a noun).
@@ -339,51 +338,6 @@ The following conventions are valid for all alist entries:
339 :prefix "reftex-" 338 :prefix "reftex-"
340 :group 'tex) 339 :group 'tex)
341 340
342(defun reftex-customize ()
343 "Call the customize function with reftex as argument."
344 (interactive)
345 ;; Depending on the customize version we can call different functions.
346 (cond
347 ((fboundp 'customize-browse)
348 (customize-browse 'reftex))
349 ((fboundp 'customize-group)
350 (customize-group 'reftex))
351 ((fboundp 'customize)
352 (customize 'reftex))
353 (t (error "Custom.el not available"))))
354
355(defun reftex-create-customize-menu ()
356 "Create a full customization menu for RefTeX."
357 (interactive)
358 (if (fboundp 'customize-menu-create)
359 (progn
360 (easy-menu-change
361 '("Ref") "Customize"
362 `(["Browse RefTeX group" reftex-customize t]
363 "---"
364 ,(customize-menu-create 'reftex)
365 ["Set" Custom-set t]
366 ["Save" Custom-save t]
367 ["Reset to Current" Custom-reset-current t]
368 ["Reset to Saved" Custom-reset-saved t]
369 ["Reset to Standard Settings" Custom-reset-standard t]))
370 (message "\"Ref\"-menu now contains full customization menu"))
371 (error "Cannot expand menu (outdated version of cus-edit.el)")))
372
373(defun reftex-show-commentary ()
374 "Use the finder to view the file documentation from `reftex.el'."
375 (interactive)
376 (require 'finder)
377 (finder-commentary "reftex.el"))
378
379(defun reftex-info ()
380 "Read documentation for RefTeX in the info system."
381 (interactive)
382 (require 'info)
383 (Info-goto-node "(reftex)"))
384
385;; Support for \label and \ref --------------------------------------
386
387(defgroup reftex-label-support nil 341(defgroup reftex-label-support nil
388 "Support for creation, insertion and referencing of labels in LaTeX." 342 "Support for creation, insertion and referencing of labels in LaTeX."
389 :group 'reftex) 343 :group 'reftex)
@@ -392,19 +346,6 @@ The following conventions are valid for all alist entries:
392 "Definition of environments and macros to do with label." 346 "Definition of environments and macros to do with label."
393 :group 'reftex-label-support) 347 :group 'reftex-label-support)
394 348
395;; Make a constant for the customization stuff
396(eval-and-compile
397 (defconst reftex-tmp
398 '((const :tag "Default position" t)
399 (const :tag "After label" nil)
400 (number :tag "Macro arg nr" 1)
401 (regexp :tag "Regexp" "")
402 (const :tag "Caption in float" caption)
403 (const :tag "Item in list" item)
404 (const :tag "Eqnarray-like" eqnarray-like)
405 (const :tag "Alignat-like" alignat-like)
406 (symbol :tag "Function" my-func))))
407
408(defcustom reftex-default-label-alist-entries 349(defcustom reftex-default-label-alist-entries
409 '(amsmath endnotes fancybox floatfig longtable picinpar 350 '(amsmath endnotes fancybox floatfig longtable picinpar
410 rotating sidecap subfigure supertab wrapfig LaTeX) 351 rotating sidecap subfigure supertab wrapfig LaTeX)
@@ -467,6 +408,9 @@ one of its arguments. The elements of each list entry are:
467 type. The same character may occur several times in this list, to cover 408 type. The same character may occur several times in this list, to cover
468 cases in which different environments carry the same label type (like 409 cases in which different environments carry the same label type (like
469 `equation' and `eqnarray'). 410 `equation' and `eqnarray').
411 If the type indicator is nil and the macro has a label argument {*},
412 the macro defines neutral labels just like \label. In this case
413 the reminder of this entry is ignored.
470 414
4712. Label prefix string, like \"tab:\". 4152. Label prefix string, like \"tab:\".
472 The prefix is a short string used as the start of a label. It may be the 416 The prefix is a short string used as the start of a label. It may be the
@@ -543,32 +487,33 @@ list. However, builtin defaults should normally be set with the variable
543 :set 'reftex-set-dirty 487 :set 'reftex-set-dirty
544 :type 488 :type
545 `(repeat 489 `(repeat
546 (choice 490 (choice :tag "Package or Detailed "
547 :value ("" ?a nil nil nil nil) 491 :value ("" ?a nil nil nil nil)
548 (list :tag "Detailed label alist entry" 492 (list :tag "Detailed Entry"
549 :value ("" ?a nil nil nil nil) 493 :value ("" ?a nil nil nil nil)
550 (choice :tag "Environment or \\macro " 494 (choice :tag "Environment or \\macro "
551 (const :tag "Ignore, just use typekey" nil) 495 (const :tag "Ignore, just use typekey" nil)
552 (string "")) 496 (string ""))
553 (character :tag "Typekey character " ?a) 497 (choice :tag "Type specification "
498 (const :tag "unspecified, like in \\label" nil)
499 (character :tag "Char " ?a))
554 (choice :tag "Label prefix string " 500 (choice :tag "Label prefix string "
555 (const :tag "Default" nil) 501 (const :tag "Default" nil)
556 (string :tag "String" "lab:")) 502 (string :tag "String" "lab:"))
557 (choice :tag "Label reference format" 503 (choice :tag "Label reference format"
558 (const :tag "Default" nil) 504 (const :tag "Default" nil)
559 (string :tag "String" "~\\ref{%s}")) 505 (string :tag "String" "~\\ref{%s}"))
560 (choice :tag "Context" 506 (choice :tag "Context method "
561 (choice 507 (const :tag "Default position" t)
562 :tag "1 method" 508 (const :tag "After label" nil)
563 ,@reftex-tmp) 509 (number :tag "Macro arg nr" 1)
564 (cons :tag "Split methods" 510 (regexp :tag "Regexp" "")
565 (choice 511 (const :tag "Caption in float" caption)
566 :tag " Display context " 512 (const :tag "Item in list" item)
567 ,@reftex-tmp) 513 (const :tag "Eqnarray-like" eqnarray-like)
568 (choice 514 (const :tag "Alignat-like" alignat-like)
569 :tag " Derive label context" 515 (symbol :tag "Function" my-func))
570 ,@reftex-tmp))) 516 (repeat :tag "Magic words" :extra-offset 2 (string)))
571 (repeat :tag "List of Magic Words" (string)))
572 (choice 517 (choice
573 :tag "Package" 518 :tag "Package"
574 :value AMSTeX 519 :value AMSTeX
@@ -677,8 +622,23 @@ e (equation), n (footnote), plus any definitions in `reftex-label-alist'."
677 (const :tag "never" nil) 622 (const :tag "never" nil)
678 (string :tag "selected label types" "")))) 623 (string :tag "selected label types" ""))))
679 624
625(defcustom reftex-string-to-label-function 'reftex-string-to-label
626 "Function to turn an arbitrary string into a legal label.
627RefTeX's default function uses the variable `reftex-derive-label-parameters'."
628 :group 'reftex-making-and-inserting-labels
629 :type 'symbol)
630
631(defcustom reftex-translate-to-ascii-function nil
632 "Filter function which will process a context string before it is used
633to derive a label from it. A useful application would be to convert ISO or
634Mule characters into something legal in labels. By default, RefTeX just
635removes these characters. X-Symbol (>=2.6) sets this variable to
636`x-symbol-translate-to-ascii'."
637 :group 'reftex-making-and-inserting-labels
638 :type 'symbol)
639
680(defcustom reftex-derive-label-parameters '(3 20 t 1 "-" 640(defcustom reftex-derive-label-parameters '(3 20 t 1 "-"
681 ("the" "on" "in" "off" "a" "for" "by" "of" "and" "is")) 641 ("the" "on" "in" "off" "a" "for" "by" "of" "and" "is" "to") t)
682 "Parameters for converting a string into a label. 642 "Parameters for converting a string into a label.
683NWORDS Number of words to use. 643NWORDS Number of words to use.
684MAXCHAR Maximum number of characters in a label string. 644MAXCHAR Maximum number of characters in a label string.
@@ -689,7 +649,8 @@ ABBREV nil: Never abbreviate words.
689 not t and not nil: Abbreviate words if necessary to shorten 649 not t and not nil: Abbreviate words if necessary to shorten
690 label string below MAXCHAR. 650 label string below MAXCHAR.
691SEPARATOR String separating different words in the label. 651SEPARATOR String separating different words in the label.
692IGNOREWORDS List of words which should not be part of labels." 652IGNOREWORDS List of words which should not be part of labels.
653DOWNCASE t: Downcase words before using them."
693 :group 'reftex-making-and-inserting-labels 654 :group 'reftex-making-and-inserting-labels
694 :type '(list (integer :tag "Number of words " 3) 655 :type '(list (integer :tag "Number of words " 3)
695 (integer :tag "Maximum label length " 20) 656 (integer :tag "Maximum label length " 20)
@@ -703,15 +664,21 @@ IGNOREWORDS List of words which should not be part of labels."
703 (string :tag "Separator between words " "-") 664 (string :tag "Separator between words " "-")
704 (repeat :tag "Ignore words" 665 (repeat :tag "Ignore words"
705 :entry-format " %i %d %v" 666 :entry-format " %i %d %v"
706 (string :tag "")))) 667 (string :tag ""))
668 (option (boolean :tag "Downcase words "))))
707 669
708(defcustom reftex-label-illegal-re "[\000-\040\177-\377\\\\#$%&~^_{}]" 670;; The following rexexp is defined negative and excludes all Mule characters.
671;; A positive way to do it and to allow Mule characters at the same time
672;; vwould be this: "[][\000-\037 !\"#$%&'()*,/<=>?@\\^`{|}~\177]"
673(defcustom reftex-label-illegal-re "[^-a-zA-Z0-9_+=:;,.\300-\377]"
709 "Regexp matching characters not legal in labels. 674 "Regexp matching characters not legal in labels.
710For historic reasons, this character class comes *with* the [] brackets." 675For historic reasons, this character class comes *with* the [] brackets.
676The default value allows the characters \300-\377 because these are the
677european letters in ISO 8859-1."
711 :group 'reftex-making-and-inserting-labels 678 :group 'reftex-making-and-inserting-labels
712 :type '(regexp :tag "Character class")) 679 :type '(regexp :tag "Character class"))
713 680
714(defcustom reftex-abbrev-parameters '(4 2 "^saeiou" "aeiou") 681(defcustom reftex-abbrev-parameters '(4 2 "^aeiou" "aeiou")
715 "Parameters for abbreviation of words. 682 "Parameters for abbreviation of words.
716MIN-CHARS Minimum number of characters remaining after abbreviation. 683MIN-CHARS Minimum number of characters remaining after abbreviation.
717MIN-KILL Minimum number of characters to remove when abbreviating words. 684MIN-KILL Minimum number of characters to remove when abbreviating words.
@@ -731,7 +698,6 @@ The function will be called with two arguments, the LABEL and the DEFAULT
731FORMAT, which usually is `\label{%s}'. The function should return the 698FORMAT, which usually is `\label{%s}'. The function should return the
732string to insert into the buffer." 699string to insert into the buffer."
733 :group 'reftex-making-and-inserting-labels 700 :group 'reftex-making-and-inserting-labels
734 :version "20.3"
735 :type 'function) 701 :type 'function)
736 702
737;; Label referencing 703;; Label referencing
@@ -795,7 +761,7 @@ the label types for which it should be true."
795(defcustom reftex-level-indent 2 761(defcustom reftex-level-indent 2
796 "*Number of spaces to be used for indentation per section level." 762 "*Number of spaces to be used for indentation per section level."
797 :group 'reftex-referencing-labels 763 :group 'reftex-referencing-labels
798 :type '(integer)) 764 :type 'integer)
799 765
800(defcustom reftex-guess-label-type t 766(defcustom reftex-guess-label-type t
801 "*Non-nil means, `reftex-reference' will try to guess the label type. 767 "*Non-nil means, `reftex-reference' will try to guess the label type.
@@ -804,7 +770,7 @@ the words given in `reftex-label-alist'. When it finds a match, RefTeX will
804immediately offer the correct label menu - otherwise it will prompt you for 770immediately offer the correct label menu - otherwise it will prompt you for
805a label type. If you set this variable to nil, RefTeX will always prompt." 771a label type. If you set this variable to nil, RefTeX will always prompt."
806 :group 'reftex-referencing-labels 772 :group 'reftex-referencing-labels
807 :type '(boolean)) 773 :type 'boolean)
808 774
809(defcustom reftex-format-ref-function nil 775(defcustom reftex-format-ref-function nil
810 "Function which produces the string to insert as a reference. 776 "Function which produces the string to insert as a reference.
@@ -814,9 +780,13 @@ The function will be called with two arguments, the LABEL and the DEFAULT
814FORMAT, which normally is `~\ref{%s}'. The function should return the 780FORMAT, which normally is `~\ref{%s}'. The function should return the
815string to insert into the buffer." 781string to insert into the buffer."
816 :group 'reftex-referencing-labels 782 :group 'reftex-referencing-labels
817 :version "20.3"
818 :type 'function) 783 :type 'function)
819 784
785(defcustom reftex-select-label-mode-hook nil
786 "Mode hook for reftex-select-label-mode."
787 :group 'reftex-referencing-labels
788 :type 'hook)
789
820;; BibteX citation configuration ---------------------------------------- 790;; BibteX citation configuration ----------------------------------------
821 791
822(defgroup reftex-citation-support nil 792(defgroup reftex-citation-support nil
@@ -824,21 +794,22 @@ string to insert into the buffer."
824 :group 'reftex) 794 :group 'reftex)
825 795
826(defcustom reftex-bibpath-environment-variables '("BIBINPUTS" "TEXBIB") 796(defcustom reftex-bibpath-environment-variables '("BIBINPUTS" "TEXBIB")
827 "*List of env vars which might contain the path to BibTeX database files." 797 "*List of env vars which might contain the path to BibTeX database files.
798Directories ending in `//' or `!!' will be expanded recursively when necessary
799to find files."
828 :group 'reftex-citation-support 800 :group 'reftex-citation-support
829 :set 'reftex-set-dirty 801 :set 'reftex-set-dirty
830 :type '(repeat (string :tag "Environment variable"))) 802 :type '(repeat (string :tag "Environment variable")))
831 803
832(defcustom reftex-bibfile-ignore-list nil 804(defvar reftex-bibfile-ignore-list nil) ; compatibility
833 "*List of files in \\bibliography{..} RefTeX should *not* parse. 805(defcustom reftex-bibfile-ignore-regexps nil
834The file names have to be in the exact same form as in the bibliography 806 "*List of regular expressions to exclude files in \\bibliography{..}.
835macro - i.e. without the `.bib' extension. 807File names matched by these regexps will not be parsed by RefTeX.
836Intended for files which contain only `@string' macro definitions and the 808Intended for files which contain only `@string' macro definitions and the
837like, which are ignored by RefTeX anyway." 809like, which are ignored by RefTeX anyway."
838 :group 'reftex-citation-support 810 :group 'reftex-citation-support
839 :version "20.3"
840 :set 'reftex-set-dirty 811 :set 'reftex-set-dirty
841 :type '(repeat (string :tag "File name"))) 812 :type '(repeat (regexp)))
842 813
843(defcustom reftex-default-bibliography nil 814(defcustom reftex-default-bibliography nil
844 "*List of BibTeX database file which should be used if none are specified. 815 "*List of BibTeX database file which should be used if none are specified.
@@ -847,7 +818,6 @@ When `reftex-citation' is called from a document which has neither a
847RefTeX will scan these files instead. Intended for using 818RefTeX will scan these files instead. Intended for using
848`reftex-citation' in non-LaTeX files." 819`reftex-citation' in non-LaTeX files."
849 :group 'reftex-citation-support 820 :group 'reftex-citation-support
850 :version "20.3"
851 :type '(repeat (file))) 821 :type '(repeat (file)))
852 822
853(defcustom reftex-sort-bibtex-matches 'reverse-year 823(defcustom reftex-sort-bibtex-matches 'reverse-year
@@ -883,11 +853,16 @@ It is also possible to access all other BibTeX database fields:
883%i institution %j journal %k key %m month 853%i institution %j journal %k key %m month
884%n number %o organization %p pages %P first page 854%n number %o organization %p pages %P first page
885%r address %s school %u publisher %t title 855%r address %s school %u publisher %t title
886%v volume %y year 856%v volume %y year
857%B booktitle, abbreviated %T title, abbreviated
858
859Usually, only %l is needed. The other stuff is mainly for the echo area
860display, and for (setq reftex-comment-citations t).
887 861
888Usually, only %l is needed. Try, however, (setq reftex-comment-citations t). 862%< as a special operator kills punctuation and space around it after the
863string has been formatted.
889 864
890Beware that all this only works when using BibTeX database files. When 865Beware that all this only works with BibTeX database files. When
891citations are made from the \\bibitems in an explicit thebibliography 866citations are made from the \\bibitems in an explicit thebibliography
892environment, only %l is available. 867environment, only %l is available.
893 868
@@ -897,7 +872,7 @@ strings.
897 In order to configure this variable, you can either set 872 In order to configure this variable, you can either set
898`reftex-cite-format' directly yourself or set it to the SYMBOL of one of 873`reftex-cite-format' directly yourself or set it to the SYMBOL of one of
899the predefined styles (see `reftex-cite-format-builtin'). E.g.: 874the predefined styles (see `reftex-cite-format-builtin'). E.g.:
900(setq reftex-cite-format 'harvard)" 875(setq reftex-cite-format 'natbib)"
901 :group 'reftex-citation-support 876 :group 'reftex-citation-support
902 :type 877 :type
903 `(choice 878 `(choice
@@ -923,13 +898,23 @@ the predefined styles (see `reftex-cite-format-builtin'). E.g.:
923 "*Non-nil means add a comment for each citation describing the full entry. 898 "*Non-nil means add a comment for each citation describing the full entry.
924The comment is formatted according to `reftex-cite-comment-format'." 899The comment is formatted according to `reftex-cite-comment-format'."
925 :group 'reftex-citation-support 900 :group 'reftex-citation-support
926 :type '(boolean)) 901 :type 'boolean)
927 902
928(defcustom reftex-cite-comment-format 903(defcustom reftex-cite-comment-format
929 "%% %2a %y, %j %v, %P, %e: %b, %u, %s %<\n" 904 "%% %2a %y, %j %v, %P, %b, %e, %u, %s %<\n"
930 "Citation format used for commented citations. Must NOT contain %l." 905 "Citation format used for commented citations. Must NOT contain %l.
906See the variable `reftex-cite-format' for possible percent escapes."
907 :group 'reftex-citation-support
908 :type 'string)
909
910(defcustom reftex-cite-view-format
911 "%2a %y, %T, %B, %j %v:%P, %s %<"
912 "Citation format used to display citation info in the message area.
913Must NOT contain %l. See the variable `reftex-cite-format' for
914possible percent escapes."
931 :group 'reftex-citation-support 915 :group 'reftex-citation-support
932 :type '(string)) 916 :group 'reftex-viewing-cross-references-and-citations
917 :type 'string)
933 918
934(defcustom reftex-cite-punctuation '(", " " \\& " " {\\it et al.}") 919(defcustom reftex-cite-punctuation '(", " " \\& " " {\\it et al.}")
935 "Punctuation for formatting of name lists in citations. 920 "Punctuation for formatting of name lists in citations.
@@ -951,9 +936,13 @@ The function will be called with two arguments, the CITATION KEY and the
951DEFAULT FORMAT, which is taken from `reftex-cite-format'. The function 936DEFAULT FORMAT, which is taken from `reftex-cite-format'. The function
952should return the string to insert into the buffer." 937should return the string to insert into the buffer."
953 :group 'reftex-citation-support 938 :group 'reftex-citation-support
954 :version "20.3"
955 :type 'function) 939 :type 'function)
956 940
941(defcustom reftex-select-bib-mode-hook nil
942 "Mode hook for reftex-select-bib-mode."
943 :group 'reftex-citation-support
944 :type 'hook)
945
957;; Table of contents configuration -------------------------------------- 946;; Table of contents configuration --------------------------------------
958 947
959(defgroup reftex-table-of-contents-browser nil 948(defgroup reftex-table-of-contents-browser nil
@@ -965,7 +954,60 @@ should return the string to insert into the buffer."
965The other window will show the corresponding part of the document. 954The other window will show the corresponding part of the document.
966This flag can be toggled from within the *toc* buffer with the `f' key." 955This flag can be toggled from within the *toc* buffer with the `f' key."
967 :group 'reftex-table-of-contents-browser 956 :group 'reftex-table-of-contents-browser
968 :type '(boolean)) 957 :type 'boolean)
958
959(defcustom reftex-revisit-to-follow nil
960 "*Non-nil means, follow-mode will revisit files if necessary.
961When nil, follow-mode will be suspended for stuff in unvisited files."
962 :group 'reftex-table-of-contents-browser
963 :group 'reftex-referencing-labels
964 :type 'boolean)
965
966(defcustom reftex-toc-mode-hook nil
967 "Mode hook for reftex-toc-mode."
968 :group 'reftex-table-of-contents-browser
969 :type 'hook)
970
971;; Viewing Cross References and Citations
972(defgroup reftex-viewing-cross-references-and-citations nil
973 "Displaying cross references and citations."
974 :group 'reftex)
975
976(defcustom reftex-auto-view-crossref t
977 "*Non-nil means, initially turn automatic viewing of crossref info on.
978Automatic viewing of crossref info uses the echo area. Whenever point is in
979the argument of a \\ref or \\cite macro, and no other message is being
980displayed, the echo area will display information about that cross reference.
981This feature can be turned on and of from the menu
982(Ref->Options->Crossref Viewing)."
983 :group 'reftex-viewing-cross-references-and-citations
984 :type 'boolean)
985
986(defcustom reftex-idle-time 1.2
987 "*Time (secs) Emacs has to be idle before automatic crossref display is done."
988 :group 'reftex-viewing-cross-references-and-citations
989 :type 'number)
990
991(defcustom reftex-revisit-to-echo nil
992 "*Non-nil means, automatic citation display will revisit files if necessary.
993When nil, citation display in echo area will only be active for cached
994entries and for BibTeX database files with live associated buffers."
995 :group 'reftex-viewing-cross-references-and-citations
996 :type 'boolean)
997
998(defcustom reftex-cache-cite-echo t
999 "*Non-nil means, the information displayed in the echo area for cite macros
1000is cached and even saved along with the parsing information. The cache
1001survives document scans. In order to clear it, use M-x reftex-reset-mode."
1002 :group 'reftex-viewing-cross-references-and-citations
1003 :type 'boolean)
1004
1005(defcustom reftex-display-copied-context-hook nil
1006 "Normal Hook which is run before context is displayed anywhere. Designed
1007for X-Symbol, but may have other uses as well."
1008 :group 'reftex-viewing-cross-references-and-citations
1009 :group 'reftex-referencing-labels
1010 :type 'hook)
969 1011
970;; Tuning the parser ---------------------------------------------------- 1012;; Tuning the parser ----------------------------------------------------
971 1013
@@ -992,8 +1034,7 @@ If a buffer is to be kept, the file is visited normally (which is potentially
992slow but will happen only once). 1034slow but will happen only once).
993If a buffer is to be thrown away, the initialization of the buffer depends 1035If a buffer is to be thrown away, the initialization of the buffer depends
994upon the variable `reftex-initialize-temporary-buffers'." 1036upon the variable `reftex-initialize-temporary-buffers'."
995 :group 'reftex-miscellaneous-configurations 1037 :group 'reftex-optimizations-for-large-documents
996 :version "20.3"
997 :type '(choice 1038 :type '(choice
998 (const :tag "Throw away everything" nil) 1039 (const :tag "Throw away everything" nil)
999 (const :tag "Keep everything" t) 1040 (const :tag "Keep everything" t)
@@ -1006,20 +1047,18 @@ visit a file.
1006When t, the full default initializations are done (find-file-hook etc.). 1047When t, the full default initializations are done (find-file-hook etc.).
1007Instead of t or nil, this variable may also be a list of hook functions to 1048Instead of t or nil, this variable may also be a list of hook functions to
1008do a minimal initialization." 1049do a minimal initialization."
1009 :group 'reftex-miscellaneous-configurations 1050 :group 'reftex-optimizations-for-large-documents
1010 :version "20.3"
1011 :type '(choice 1051 :type '(choice
1012 (const :tag "Read files literally" nil) 1052 (const :tag "Read files literally" nil)
1013 (const :tag "Fully initialize buffers" t) 1053 (const :tag "Fully initialize buffers" t)
1014 (repeat :tag "Hook functions" :value (nil) 1054 (repeat :tag "Hook functions" :value (nil)
1015 (function-item)))) 1055 (function-item))))
1016 1056
1017(defcustom reftex-no-include-regexps '("\\.pstex_t\\'") ; default for x-symbol 1057(defcustom reftex-no-include-regexps '("\\.pstex_t\\'")
1018 "*List of regular expressions to exclude certain input files from parsing. 1058 "*List of regular expressions to exclude certain input files from parsing.
1019If the name of a file included via \\include or \\input is matched by any 1059If the name of a file included via \\include or \\input is matched by any
1020of the regular expressions in this list, that file is not parsed by RefTeX." 1060of the regular expressions in this list, that file is not parsed by RefTeX."
1021 :group 'reftex-optimizations-for-large-documents 1061 :group 'reftex-optimizations-for-large-documents
1022 :version "20.3"
1023 :type '(repeat (regexp))) 1062 :type '(repeat (regexp)))
1024 1063
1025(defcustom reftex-enable-partial-scans nil 1064(defcustom reftex-enable-partial-scans nil
@@ -1031,7 +1070,6 @@ or section heading near point in a menu. Requesting re-parsing of an entire
1031multifile document then requires a `C-u C-u' prefix or the capital `R' key 1070multifile document then requires a `C-u C-u' prefix or the capital `R' key
1032in menus." 1071in menus."
1033 :group 'reftex-optimizations-for-large-documents 1072 :group 'reftex-optimizations-for-large-documents
1034 :version "20.3"
1035 :type 'boolean) 1073 :type 'boolean)
1036 1074
1037(defcustom reftex-allow-automatic-rescan t 1075(defcustom reftex-allow-automatic-rescan t
@@ -1040,7 +1078,6 @@ Currently this applies only to rescanning after label insertion, when
1040the new label cannot be inserted correctly into the internal label 1078the new label cannot be inserted correctly into the internal label
1041list." 1079list."
1042 :group 'reftex-optimizations-for-large-documents 1080 :group 'reftex-optimizations-for-large-documents
1043 :version "20.3"
1044 :type 'boolean) 1081 :type 'boolean)
1045 1082
1046(defcustom reftex-save-parse-info nil 1083(defcustom reftex-save-parse-info nil
@@ -1049,10 +1086,9 @@ The file MASTER.rel in the same directory as MASTER.tex is used to save the
1049information. When this variable is t, 1086information. When this variable is t,
1050- accessing the parsing information for the first time in an editing session 1087- accessing the parsing information for the first time in an editing session
1051 will read that file (if available) instead of parsing the document. 1088 will read that file (if available) instead of parsing the document.
1052- each time (part of) the document is rescanned, a new version of the file 1089- exiting Emacs or killing a buffer in reftex-mode will cause a new version
1053 is written." 1090 of the file to be written."
1054 :group 'reftex-optimizations-for-large-documents 1091 :group 'reftex-optimizations-for-large-documents
1055 :version "20.3"
1056 :type 'boolean) 1092 :type 'boolean)
1057 1093
1058(defcustom reftex-use-multiple-selection-buffers nil 1094(defcustom reftex-use-multiple-selection-buffers nil
@@ -1063,7 +1099,7 @@ selection buffers will be erased (and therefore updated) automatically
1063when new labels in its category are added. See the variable 1099when new labels in its category are added. See the variable
1064`reftex-auto-update-selection-buffers'." 1100`reftex-auto-update-selection-buffers'."
1065 :group 'reftex-optimizations-for-large-documents 1101 :group 'reftex-optimizations-for-large-documents
1066 :version "20.3" 1102 :group 'reftex-referencing-labels
1067 :type 'boolean) 1103 :type 'boolean)
1068 1104
1069(defcustom reftex-auto-update-selection-buffers t 1105(defcustom reftex-auto-update-selection-buffers t
@@ -1075,7 +1111,7 @@ updated by hand, with the `g' key from the label selection process.
1075The value of this variable will only have any effect when 1111The value of this variable will only have any effect when
1076`reftex-use-multiple-selection-buffers' is non-nil." 1112`reftex-use-multiple-selection-buffers' is non-nil."
1077 :group 'reftex-optimizations-for-large-documents 1113 :group 'reftex-optimizations-for-large-documents
1078 :version "20.3" 1114 :group 'reftex-referencing-labels
1079 :type 'boolean) 1115 :type 'boolean)
1080 1116
1081;; Fontification and Faces ---------------------------------------------- 1117;; Fontification and Faces ----------------------------------------------
@@ -1086,9 +1122,10 @@ The value of this variable will only have any effect when
1086 1122
1087(defcustom reftex-use-fonts t 1123(defcustom reftex-use-fonts t
1088 "*Non-nil means, use fonts in *toc* and selection buffers. 1124 "*Non-nil means, use fonts in *toc* and selection buffers.
1089Font-lock must be loaded as well to actually get fontified display." 1125Font-lock must be loaded as well to actually get fontified display.
1126When changing this option, a rescan may be necessary to activate the change."
1090 :group 'reftex-fontification-configurations 1127 :group 'reftex-fontification-configurations
1091 :type '(boolean)) 1128 :type 'boolean)
1092 1129
1093(defcustom reftex-refontify-context 1 1130(defcustom reftex-refontify-context 1
1094 "*Non-nil means, re-fontify the context in the label menu with font-lock. 1131 "*Non-nil means, re-fontify the context in the label menu with font-lock.
@@ -1098,10 +1135,10 @@ when you definitely want the context fontified.
1098This option may have 3 different values: 1135This option may have 3 different values:
1099nil Never refontify. 1136nil Never refontify.
1100t Always refontify. 1137t Always refontify.
11011 Refontify when absolutely necessary, e.g. when with the x-symbol package. 11381 Refontify when absolutely necessary, e.g. when old versions of X-Symbol.
1102The option is ignored when `reftex-use-fonts' is nil." 1139The option is ignored when `reftex-use-fonts' is nil."
1103 :group 'reftex-fontification-configurations 1140 :group 'reftex-fontification-configurations
1104 :version "20.3" 1141 :group 'reftex-referencing-labels
1105 :type '(choice 1142 :type '(choice
1106 (const :tag "Never" nil) 1143 (const :tag "Never" nil)
1107 (const :tag "Always" t) 1144 (const :tag "Always" t)
@@ -1118,9 +1155,11 @@ instead or as well. The variable may have one of these values:
1118 nil No highlighting. 1155 nil No highlighting.
1119 cursor Highlighting is cursor driven. 1156 cursor Highlighting is cursor driven.
1120 mouse Highlighting is mouse driven. 1157 mouse Highlighting is mouse driven.
1121 both Both cursor and mouse trigger highlighting." 1158 both Both cursor and mouse trigger highlighting.
1159
1160Changing this variable requires to rebuild the selection and *toc* buffers
1161to become effective (keys `g' or `r')."
1122 :group 'reftex-fontification-configurations 1162 :group 'reftex-fontification-configurations
1123 :version "20.3"
1124 :type '(choice 1163 :type '(choice
1125 (const :tag "Never" nil) 1164 (const :tag "Never" nil)
1126 (const :tag "Cursor driven" cursor) 1165 (const :tag "Cursor driven" cursor)
@@ -1131,55 +1170,52 @@ instead or as well. The variable may have one of these values:
1131 "Face name to highlight cursor selected item in toc and selection buffers. 1170 "Face name to highlight cursor selected item in toc and selection buffers.
1132See also the variable `reftex-highlight-selection'." 1171See also the variable `reftex-highlight-selection'."
1133 :group 'reftex-fontification-configurations 1172 :group 'reftex-fontification-configurations
1134 :version "20.3"
1135 :type 'symbol) 1173 :type 'symbol)
1136(defcustom reftex-mouse-selected-face 'secondary-selection 1174(defcustom reftex-mouse-selected-face 'secondary-selection
1137 "Face name to highlight mouse selected item in toc and selection buffers. 1175 "Face name to highlight mouse selected item in toc and selection buffers.
1138See also the variable `reftex-highlight-selection'." 1176See also the variable `reftex-highlight-selection'."
1139 :group 'reftex-fontification-configurations 1177 :group 'reftex-fontification-configurations
1140 :version "20.3"
1141 :type 'symbol) 1178 :type 'symbol)
1142(defcustom reftex-file-boundary-face 'font-lock-comment-face 1179(defcustom reftex-file-boundary-face 'font-lock-comment-face
1143 "Face name for file boundaries in selection buffer." 1180 "Face name for file boundaries in selection buffer."
1144 :group 'reftex-fontification-configurations 1181 :group 'reftex-fontification-configurations
1145 :version "20.3"
1146 :type 'symbol) 1182 :type 'symbol)
1147(defcustom reftex-label-face 'font-lock-constant-face 1183(defcustom reftex-label-face 'font-lock-constant-face
1148 "Face name for labels in selection buffer." 1184 "Face name for labels in selection buffer."
1149 :group 'reftex-fontification-configurations 1185 :group 'reftex-fontification-configurations
1150 :version "20.3"
1151 :type 'symbol) 1186 :type 'symbol)
1152(defcustom reftex-section-heading-face 'font-lock-function-name-face 1187(defcustom reftex-section-heading-face 'font-lock-function-name-face
1153 "Face name for section headings in toc and selection buffers." 1188 "Face name for section headings in toc and selection buffers."
1154 :group 'reftex-fontification-configurations 1189 :group 'reftex-fontification-configurations
1155 :version "20.3"
1156 :type 'symbol) 1190 :type 'symbol)
1157(defcustom reftex-toc-header-face 'font-lock-comment-face 1191(defcustom reftex-toc-header-face 'font-lock-comment-face
1158 "Face name for the header of a toc buffer." 1192 "Face name for the header of a toc buffer."
1159 :group 'reftex-fontification-configurations 1193 :group 'reftex-fontification-configurations
1160 :version "20.3"
1161 :type 'symbol) 1194 :type 'symbol)
1162(defcustom reftex-bib-author-face 'font-lock-keyword-face 1195(defcustom reftex-bib-author-face 'font-lock-keyword-face
1163 "Face name for author names in bib selection buffer." 1196 "Face name for author names in bib selection buffer."
1164 :group 'reftex-fontification-configurations 1197 :group 'reftex-fontification-configurations
1165 :version "20.3"
1166 :type 'symbol) 1198 :type 'symbol)
1167(defcustom reftex-bib-year-face 'font-lock-comment-face 1199(defcustom reftex-bib-year-face 'font-lock-comment-face
1168 "Face name for year in bib selection buffer." 1200 "Face name for year in bib selection buffer."
1169 :group 'reftex-fontification-configurations 1201 :group 'reftex-fontification-configurations
1170 :version "20.3"
1171 :type 'symbol) 1202 :type 'symbol)
1172(defcustom reftex-bib-title-face 'font-lock-function-name-face 1203(defcustom reftex-bib-title-face 'font-lock-function-name-face
1173 "Face name for article title in bib selection buffer." 1204 "Face name for article title in bib selection buffer."
1174 :group 'reftex-fontification-configurations 1205 :group 'reftex-fontification-configurations
1175 :version "20.3"
1176 :type 'symbol) 1206 :type 'symbol)
1177(defcustom reftex-bib-extra-face 'font-lock-comment-face 1207(defcustom reftex-bib-extra-face 'font-lock-comment-face
1178 "Face name for bibliographic information in bib selection buffer." 1208 "Face name for bibliographic information in bib selection buffer."
1179 :group 'reftex-fontification-configurations 1209 :group 'reftex-fontification-configurations
1180 :version "20.3"
1181 :type 'symbol) 1210 :type 'symbol)
1182 1211
1212(defcustom reftex-pre-refontification-functions nil
1213 "X-Symbol specific hook.
1214Functions get two arguments, the buffer from where the command started and a
1215symbol indicating in what context the hook is called."
1216 :group 'reftex-fontification-configurations
1217 :type 'hook)
1218
1183;; Miscellaneous configurations ----------------------------------------- 1219;; Miscellaneous configurations -----------------------------------------
1184 1220
1185(defgroup reftex-miscellaneous-configurations nil 1221(defgroup reftex-miscellaneous-configurations nil
@@ -1190,58 +1226,48 @@ See also the variable `reftex-highlight-selection'."
1190 "Non-nil means, make additional key bindings on startup. 1226 "Non-nil means, make additional key bindings on startup.
1191These extra bindings are located in the users `C-c letter' map." 1227These extra bindings are located in the users `C-c letter' map."
1192 :group 'reftex-miscellaneous-configurations 1228 :group 'reftex-miscellaneous-configurations
1193 :type '(boolean)) 1229 :type 'boolean)
1194 1230
1195(defcustom reftex-plug-into-AUCTeX nil 1231(defcustom reftex-plug-into-AUCTeX nil
1196 "*Plug-in flags for AUCTeX interface. 1232 "*Plug-in flags for AUCTeX interface.
1197This variable is a list of 4 boolean flags. When a flag is non-nil, it 1233This variable is a list of 4 boolean flags. When a flag is non-nil,
1198means: 1234RefTeX will
1199 1235
1200 Flag 1: use `reftex-label' as `LaTeX-label-function'. 1236 - supply labels in new sections and environments (flag 1)
1201 Flag 2: use `reftex-arg-label' as `TeX-arg-label' 1237 - supply arguments for macros like `\\label'. (flag 2)
1202 Flag 3: use `reftex-arg-ref' as `TeX-arg-ref' 1238 - supply arguments for macros like `\\ref'. (flag 3)
1203 Flag 4: use `reftex-arg-cite' as `TeX-arg-cite' 1239 - supply arguments for macros like `\\cite'. (flag 4)
1204 1240
1205You may also set the variable itself to t or nil in order to turn all 1241You may also set the variable itself to t or nil in order to turn all
1206plug-ins on or off, respectively. 1242plug-ins on or off, respectively.
1207\\<LaTeX-mode-map>`LaTeX-label-function' is the function used for label insertion when you 1243\\<LaTeX-mode-map>Supplying labels in new sections and environments aplies when creating
1208enter a new environment in AUCTeX with \\[LaTeX-environment]. 1244sections with \\[LaTeX-section] and environments with \\[LaTeX-environment].
1209The `TeX-arg-label' etc. functions are for entering macro arguments during 1245Supplying macro arguments applies when you insert such a macro interactively
1210macro insertion with \\[TeX-insert-macro]. 1246with \\[TeX-insert-macro].
1211See the AUCTeX documentation for more information. 1247See the AUCTeX documentation for more information.
1212RefTeX uses `fset' to take over the function calls. Changing the variable 1248RefTeX uses `fset' to take over the function calls. Changing the variable
1213may require a restart of Emacs in order to become effective." 1249may require a restart of Emacs in order to become effective."
1214 :group 'reftex-miscellaneous-configurations 1250 :group 'reftex-miscellaneous-configurations
1215 :type '(choice (const :tag "No plug-ins" nil) 1251 :group 'LaTeX
1216 (const :tag "All possible plug-ins" t) 1252 :type '(choice
1217 (list 1253 (const :tag "No plug-ins" nil)
1218 :tag "Individual choice" 1254 (const :tag "All possible plug-ins" t)
1219 :value (nil nil nil nil) 1255 (list
1220 (boolean :tag "Use reftex-label as LaTeX-label-function") 1256 :tag "Individual choice"
1221 (boolean :tag "Use reftex-arg-label as TeX-arg-label ") 1257 :value (t t t t)
1222 (boolean :tag "Use reftex-arg-ref as TeX-arg-ref ") 1258 (boolean :tag "supply label in new sections and environments")
1223 (boolean :tag "Use reftex-arg-cite as TeX-arg-cite ") 1259 (boolean :tag "supply argument for macros like `\\label' ")
1224 ))) 1260 (boolean :tag "supply argument for macros like `\\ref' ")
1225 1261 (boolean :tag "supply argument for macros like `\\cite' ")
1226(defcustom reftex-auto-show-entry 'copy 1262 )))
1227 "*Non-nil means, do something when context in other window is hidden. 1263
1228Some modes like `outline-mode' or `folding-mode' hide parts of buffers. 1264(defcustom reftex-allow-detached-macro-args nil
1229When RefTeX is asked to show context for a label definition, and the context 1265 "*Non-nil means, allow arguments of macros to be detached by whitespace.
1230is invisible, it can unhide that section permanently (value t), or copy the 1266When this is t, `aaa' will be considered as argument of \\bb in the following
1231context to a temporary buffer (value 'copy)." 1267construct: \\bbb [xxx] {aaa}."
1232 :group 'reftex-miscellaneous-configurations 1268 :group 'texmathp
1233 :type '(radio :value copy 1269 :type 'boolean)
1234 :indent 4
1235 (const :tag "Do nothing" nil)
1236 (const :tag "Unhide section permanently" t)
1237 (const :tag "Copy context to show" copy)))
1238 1270
1239(defcustom reftex-revisit-to-follow t
1240 "*Non-nil means, follow-mode will revisit files if necessary.
1241When nil, follow-mode will be suspended for stuff in unvisited files."
1242 :group 'reftex-miscellaneous-configurations
1243 :version "20.3"
1244 :type '(boolean))
1245 1271
1246(defcustom reftex-load-hook nil 1272(defcustom reftex-load-hook nil
1247 "Hook which is being run when loading reftex.el." 1273 "Hook which is being run when loading reftex.el."
@@ -1253,27 +1279,25 @@ When nil, follow-mode will be suspended for stuff in unvisited files."
1253 :group 'reftex-miscellaneous-configurations 1279 :group 'reftex-miscellaneous-configurations
1254 :type 'hook) 1280 :type 'hook)
1255 1281
1256;;; End of Configuration Section ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 1282;;; =========================================================================
1257
1258;;;===========================================================================
1259;;; 1283;;;
1260;;; Define the formal stuff for a minor mode named RefTeX. 1284;;; Define the formal stuff for a minor mode named RefTeX.
1261;;; 1285;;;
1262 1286
1263;; This file corresponds to RefTeX version 3.22 1287;; This file corresponds to RefTeX version 3.34
1264 1288
1265(defvar reftex-mode nil 1289(defvar reftex-mode nil
1266 "Determines if RefTeX minor mode is active.") 1290 "Determines if RefTeX mode is active.")
1267(make-variable-buffer-local 'reftex-mode) 1291(make-variable-buffer-local 'reftex-mode)
1268 1292
1269(defvar reftex-mode-map (make-sparse-keymap) 1293(defvar reftex-mode-map (make-sparse-keymap)
1270 "Keymap for RefTeX minor mode.") 1294 "Keymap for RefTeX mode.")
1271 1295
1272(defvar reftex-mode-menu nil) 1296(defvar reftex-mode-menu nil)
1273 1297
1274;;;###autoload 1298;;;###autoload
1275(defun turn-on-reftex () 1299(defun turn-on-reftex ()
1276 "Turn on RefTeX minor mode." 1300 "Turn on RefTeX mode."
1277 (reftex-mode t)) 1301 (reftex-mode t))
1278 1302
1279;;;###autoload 1303;;;###autoload
@@ -1312,108 +1336,39 @@ on the menu bar.
1312 (if reftex-mode 1336 (if reftex-mode
1313 (progn 1337 (progn
1314 (easy-menu-add reftex-mode-menu) 1338 (easy-menu-add reftex-mode-menu)
1315 (reftex-plug-into-AUCTeX) 1339 (and reftex-plug-into-AUCTeX
1340 (reftex-plug-into-AUCTeX))
1316 (run-hooks 'reftex-mode-hook)) 1341 (run-hooks 'reftex-mode-hook))
1317 (easy-menu-remove reftex-mode-menu))) 1342 (easy-menu-remove reftex-mode-menu)))
1318 1343
1319(or (assoc 'reftex-mode minor-mode-alist) 1344(if (fboundp 'add-minor-mode)
1345 ;; Use it so that we get the extras
1346 (progn
1347 (put 'reftex-mode ':included '(memq major-mode '(latex-mode tex-mode)))
1348 (put 'reftex-mode ':menu-tag "RefTeX Mode")
1349 (add-minor-mode 'reftex-mode " Ref" reftex-mode-map))
1350 ;; The standard way
1351 (unless (assoc 'reftex-mode minor-mode-alist)
1320 (push '(reftex-mode " Ref") minor-mode-alist)) 1352 (push '(reftex-mode " Ref") minor-mode-alist))
1353 (unless (assoc 'reftex-mode minor-mode-map-alist)
1354 (push (cons 'reftex-mode reftex-mode-map) minor-mode-map-alist)))
1321 1355
1322(or (assoc 'reftex-mode minor-mode-map-alist) 1356;;; =========================================================================
1323 (push (cons 'reftex-mode reftex-mode-map) minor-mode-map-alist))
1324
1325;;; ===========================================================================
1326;;; 1357;;;
1327;;; Silence warnings about variables in other packages. 1358;;; Silence warnings about variables in other packages.
1328(defvar TeX-master) 1359(defvar TeX-master)
1360(defvar LaTeX-section-hook)
1329(defvar LaTeX-label-function) 1361(defvar LaTeX-label-function)
1330(defvar tex-main-file) 1362(defvar tex-main-file)
1331(defvar outline-minor-mode) 1363(defvar outline-minor-mode)
1332(defvar font-lock-fontify-region-function) 1364(defvar font-lock-fontify-region-function)
1333(defvar font-lock-syntactic-keywords) 1365(defvar font-lock-syntactic-keywords)
1334 1366
1335;;; =========================================================================== 1367;;; =========================================================================
1336;;;
1337;;; Interfaces for other packages
1338;;; -----------------------------
1339;;;
1340;;; AUCTeX
1341;;; ------
1342
1343(defun reftex-arg-label (optional &optional prompt definition)
1344 "Use `reftex-label' to create label. Insert it with `TeX-argument-insert'.
1345This function is intended for AUCTeX macro support."
1346 (let ((label (reftex-label nil t)))
1347 (if (and definition (not (string-equal "" label)))
1348 (LaTeX-add-labels label))
1349 (TeX-argument-insert label optional optional)))
1350
1351(defun reftex-arg-ref (optional &optional prompt definition)
1352 "Use `reftex-reference' to select label. Insert with `TeX-argument-insert'.
1353This function is intended for AUCTeX macro support."
1354 (let ((label (reftex-reference nil t)))
1355 (if (and definition (not (string-equal "" label)))
1356 (LaTeX-add-labels label))
1357 (TeX-argument-insert label optional optional)))
1358
1359(defun reftex-arg-cite (optional &optional prompt definition)
1360 "Use reftex-citation to select a key. Insert with `TeX-argument-insert'.
1361This function is intended for AUCTeX macro support."
1362 (let ((key (reftex-citation t)))
1363 (TeX-argument-insert (or key "") optional optional)))
1364
1365(defun reftex-plug-into-AUCTeX ()
1366 ;; Replace AUCTeX functions with RefTeX functions.
1367 ;; Which functions are replaced is controlled by the variable
1368 ;; `reftex-plug-into-AUCTeX'.
1369 (let ((flags
1370 (cond ((eq reftex-plug-into-AUCTeX t) '(t t t t))
1371 ((eq reftex-plug-into-AUCTeX nil) '(nil nil nil nil))
1372 (t reftex-plug-into-AUCTeX))))
1373
1374 (and (nth 0 flags)
1375 (boundp 'LaTeX-label-function)
1376 (setq LaTeX-label-function 'reftex-label))
1377
1378 (and (nth 1 flags)
1379 (fboundp 'TeX-arg-label)
1380 (fset 'TeX-arg-label 'reftex-arg-label))
1381
1382 (and (nth 2 flags)
1383 (fboundp 'TeX-arg-ref)
1384 (fset 'TeX-arg-ref 'reftex-arg-ref))
1385
1386 (and (nth 3 flags)
1387 (fboundp 'TeX-arg-cite)
1388 (fset 'TeX-arg-cite 'reftex-arg-cite))))
1389
1390
1391(defvar reftex-label-alist-external-add-ons nil
1392 "List of label alist entries added with reftex-add-to-label-alist.")
1393
1394(defun reftex-add-to-label-alist (entry-list)
1395 "Add label environment descriptions to `reftex-label-alist-external-add-ons'.
1396The format of ENTRY-LIST is exactly like `reftex-label-alist'. See there
1397for details.
1398This function makes it possible to support RefTeX from AUCTeX style files.
1399The entries in ENTRY-LIST will be processed after the user settings in
1400`reftex-label-alist', and before the defaults (specified in
1401`reftex-default-label-alist-entries'). Any changes made to
1402`reftex-label-alist-external-add-ons' will raise a flag to the effect that a
1403mode reset is done on the next occasion."
1404 (let (entry)
1405 (while entry-list
1406 (setq entry (car entry-list)
1407 entry-list (cdr entry-list))
1408 (unless (member entry reftex-label-alist-external-add-ons)
1409 (setq reftex-tables-dirty t)
1410 (push entry reftex-label-alist-external-add-ons)))))
1411
1412;;; ===========================================================================
1413;;; 1368;;;
1414;;; Multifile support 1369;;; Multibuffer Variables
1415;;; 1370;;;
1416;;; Technical notes: Multifile works as follows: We keep just one list 1371;;; Technical notes: These work as follows: We keep just one list
1417;;; of labels for each master file - this can save a lot of memory. 1372;;; of labels for each master file - this can save a lot of memory.
1418;;; `reftex-master-index-list' is an alist which connects the true file name 1373;;; `reftex-master-index-list' is an alist which connects the true file name
1419;;; of each master file with the symbols holding the information on that 1374;;; of each master file with the symbols holding the information on that
@@ -1445,9 +1400,7 @@ mode reset is done on the next occasion."
1445 (let* ((master (file-truename (reftex-TeX-master-file))) 1400 (let* ((master (file-truename (reftex-TeX-master-file)))
1446 (index (assoc master reftex-master-index-list)) 1401 (index (assoc master reftex-master-index-list))
1447 (symlist reftex-multifile-symbols) 1402 (symlist reftex-multifile-symbols)
1448 (symbol nil) 1403 symbol symname newflag)
1449 (symname nil)
1450 (newflag nil))
1451 ;; Find the correct index. 1404 ;; Find the correct index.
1452 (if index 1405 (if index
1453 ;; symbols do exist 1406 ;; symbols do exist
@@ -1524,143 +1477,13 @@ mode reset is done on the next occasion."
1524 ;; Ahh, an extra .tex was missing... 1477 ;; Ahh, an extra .tex was missing...
1525 (setq master (concat master ".tex"))) 1478 (setq master (concat master ".tex")))
1526 (t 1479 (t
1527 ;; Something is wrong here. Throw an exception. 1480 ;; Use buffer file name.
1528 (buffer-file-name))) 1481 (buffer-file-name)))
1529; (error "No such master file %s" master)))
1530 (expand-file-name master))) 1482 (expand-file-name master)))
1531 1483
1532(defun reftex-parse-one () 1484;;; =========================================================================
1533 "Re-parse this file."
1534 (interactive)
1535 (let ((reftex-enable-partial-scans t))
1536 (reftex-access-scan-info '(4))))
1537
1538(defun reftex-parse-all ()
1539 "Re-parse entire document."
1540 (interactive)
1541 (reftex-access-scan-info '(16)))
1542
1543(defun reftex-all-document-files (&optional relative)
1544 ;; Return a list of all files belonging to the current document.
1545 ;; When RELATIVE is non-nil, give file names relative to directory
1546 ;; of master file.
1547 (let* ((all (symbol-value reftex-docstruct-symbol))
1548 (master-dir (file-name-directory (reftex-TeX-master-file)))
1549 (re (concat "\\`" (regexp-quote master-dir)))
1550 file-list tmp file)
1551 (while (setq tmp (assoc 'bof all))
1552 (setq file (nth 1 tmp)
1553 all (cdr (memq tmp all)))
1554 (and relative
1555 (string-match re file)
1556 (setq file (substring file (match-end 0))))
1557 (push file file-list))
1558 (nreverse file-list)))
1559
1560(defun reftex-create-tags-file ()
1561 "Create TAGS file by running `etags' on the current document.
1562The TAGS file is also immediately visited with `visit-tags-table'."
1563 (interactive)
1564 (reftex-access-scan-info current-prefix-arg)
1565 (let* ((master (reftex-TeX-master-file))
1566 (files (reftex-all-document-files))
1567 (cmd (format "etags %s" (mapconcat 'identity files " "))))
1568 (save-excursion
1569 (set-buffer (reftex-get-buffer-visiting master))
1570 (message "Running etags to create TAGS file...")
1571 (shell-command cmd)
1572 (visit-tags-table "TAGS"))))
1573
1574;; History of grep commands.
1575(defvar reftex-grep-history nil)
1576(defvar reftex-grep-command "grep -n "
1577 "Last grep command used in \\[reftex-grep-document]; default for next grep.")
1578
1579(defun reftex-grep-document (grep-cmd)
1580 "Run grep query through all files related to this document.
1581With prefix arg, force to rescan document.
1582This works also without an active TAGS table."
1583
1584 (interactive
1585 (list (read-from-minibuffer "Run grep on document (like this): "
1586 reftex-grep-command nil nil
1587 'reftex-grep-history)))
1588 (reftex-access-scan-info current-prefix-arg)
1589 (let* ((files (reftex-all-document-files t))
1590 (cmd (format
1591 "%s %s" grep-cmd
1592 (mapconcat 'identity files " "))))
1593 (grep cmd)))
1594
1595(defun reftex-search-document (&optional regexp)
1596 "Regexp search through all files of the current TeX document.
1597Starts always in the master file. Stops when a match is found.
1598To continue searching for next match, use command \\[tags-loop-continue].
1599This works also without an active TAGS table."
1600 (interactive)
1601 (let ((default (reftex-this-word)))
1602 (unless regexp
1603 (setq regexp (read-string (format "Search regexp in document [%s]: "
1604 default))))
1605 (if (string= regexp "") (setq regexp (regexp-quote default)))
1606
1607 (reftex-access-scan-info current-prefix-arg)
1608 (tags-search regexp (list 'reftex-all-document-files))))
1609
1610(defun reftex-query-replace-document (&optional from to delimited)
1611 "Run a query-replace-regexp of FROM with TO over the entire TeX document.
1612Third arg DELIMITED (prefix arg) means replace only word-delimited matches.
1613If you exit (\\[keyboard-quit] or ESC), you can resume the query replace
1614with the command \\[tags-loop-continue].
1615This works also without an active TAGS table."
1616 (interactive)
1617 (let ((default (reftex-this-word)))
1618 (unless from
1619 (setq from (read-string (format "Replace regexp in document [%s]: "
1620 default)))
1621 (if (string= from "") (setq from (regexp-quote default))))
1622 (unless to
1623 (setq to (read-string (format "Replace regexp %s with: " from))))
1624 (reftex-access-scan-info current-prefix-arg)
1625 (tags-query-replace from to (or delimited current-prefix-arg)
1626 (list 'reftex-all-document-files))))
1627
1628(defun reftex-change-label (&optional from to)
1629 "Query replace FROM with TO in all \\label and \\ref commands.
1630Works on the entire multifile document.
1631If you exit (\\[keyboard-quit] or ESC), you can resume the query replace
1632with the command \\[tags-loop-continue].
1633This works also without an active TAGS table."
1634 (interactive)
1635 (let ((default (reftex-this-word "-a-zA-Z0-9_*.:")))
1636 (unless from
1637 (setq from (read-string (format "Replace label globally [%s]: "
1638 default))))
1639 (if (string= from "") (setq from default))
1640 (unless to
1641 (setq to (read-string (format "Replace label %s with: "
1642 from))))
1643 (reftex-query-replace-document
1644 (concat "\\\\\\(label\\|[a-z]*ref\\){" (regexp-quote from) "}")
1645 (format "\\\\\\1{%s}" to))))
1646
1647(defun reftex-save-all-document-buffers ()
1648 "Save all documents associated with the current document.
1649The function is useful after a global action like replacing or renumbering
1650labels."
1651 (interactive)
1652 (let ((files (reftex-all-document-files))
1653 file buffer)
1654 (save-excursion
1655 (while (setq file (pop files))
1656 (setq buffer (reftex-get-buffer-visiting file))
1657 (when buffer
1658 (set-buffer buffer)
1659 (save-buffer))))))
1660
1661;;; ===========================================================================
1662;;; 1485;;;
1663;;; Functions to create and reference automatic labels. 1486;;; Functions to parse the buffer and to create and reference labels.
1664 1487
1665;; The following constants are derived from `reftex-label-alist'. 1488;; The following constants are derived from `reftex-label-alist'.
1666 1489
@@ -1694,6 +1517,12 @@ labels."
1694;; The last list-of-labels entry used in a reference. 1517;; The last list-of-labels entry used in a reference.
1695(defvar reftex-last-used-reference (list nil nil nil nil)) 1518(defvar reftex-last-used-reference (list nil nil nil nil))
1696 1519
1520;; The message when follow-mode is suspended
1521(defconst reftex-no-follow-message
1522 "No follow-mode into unvisited file. Press SPC to visit it.")
1523(defconst reftex-no-info-message
1524 "%s: info not available, use `\\[reftex-view-crossref]' to get it.")
1525
1697;; The regular expression used to abbreviate words. 1526;; The regular expression used to abbreviate words.
1698(defconst reftex-abbrev-regexp 1527(defconst reftex-abbrev-regexp
1699 (concat 1528 (concat
@@ -1713,41 +1542,40 @@ labels."
1713(defvar reftex-tex-path nil) 1542(defvar reftex-tex-path nil)
1714(defvar reftex-bib-path nil) 1543(defvar reftex-bib-path nil)
1715(defvar reftex-last-follow-point nil) 1544(defvar reftex-last-follow-point nil)
1716(defvar reftex-prefix) 1545(defvar reftex-latex-syntax-table nil)
1546(defvar reftex-prefix nil)
1547(defvar reftex-section-levels-all nil)
1548(defvar reftex-buffers-with-changed-invisibility nil)
1717 1549
1718;; List of buffers created temporarily for lookup, which should be killed. 1550;; List of buffers created temporarily for lookup, which should be killed.
1719(defvar reftex-buffers-to-kill nil) 1551(defvar reftex-buffers-to-kill nil)
1720 1552
1721;; Regexp to find section statements. Computed from reftex-section-levels. 1553;; Regexp to find anything.
1722(defvar reftex-section-regexp nil) 1554(defvar reftex-section-regexp nil)
1723(defvar reftex-section-or-include-regexp nil) 1555(defvar reftex-section-or-include-regexp nil)
1724(defvar reftex-everything-regexp nil) 1556(defvar reftex-everything-regexp nil)
1725(defvar reftex-find-label-regexp-format nil) 1557(defvar reftex-find-label-regexp-format nil)
1726(defvar reftex-find-label-regexp-format2 nil) 1558(defvar reftex-find-label-regexp-format2 nil)
1727 1559
1728;; The parser functions ---------------------------------- 1560;;; The parser functions -----------------------------------------------------
1729 1561
1730(defvar reftex-memory nil 1562(defvar reftex-memory nil
1731 "Memorizes old variable values to indicate changes in these variables.") 1563 "Memorizes old variable values to indicate changes in these variables.")
1732 1564
1733(defun reftex-access-scan-info (&optional rescan file) 1565(defun reftex-access-scan-info (&optional rescan file)
1734 ;; Access the scanning info. When the multifile symbols are not yet tied, 1566 "Ensure access to the scanning info for the current file."
1567 ;; When the multifile symbols are not yet tied,
1735 ;; tie them. When they are empty or RESCAN is non-nil, scan the document. 1568 ;; tie them. When they are empty or RESCAN is non-nil, scan the document.
1569 ;; But, when RESCAN is -1, don't rescan even if docstruct is empty.
1736 ;; When FILE is non-nil, parse only from that file. 1570 ;; When FILE is non-nil, parse only from that file.
1737 1571
1738 ;; Reset the mode if we had changes to important variables. 1572 ;; Make sure we have the symbols tied
1739 (when (or reftex-tables-dirty
1740 (not (eq reftex-label-alist (nth 0 reftex-memory)))
1741 (not (eq reftex-label-alist-external-add-ons
1742 (nth 1 reftex-memory)))
1743 (not (eq reftex-default-label-alist-entries
1744 (nth 2 reftex-memory))))
1745 (reftex-reset-mode))
1746
1747 (if (eq reftex-docstruct-symbol nil) 1573 (if (eq reftex-docstruct-symbol nil)
1748 ;; Symbols are not yet tied: Tie them. 1574 ;; Symbols are not yet tied: Tie them.
1749 (reftex-tie-multifile-symbols)) 1575 (reftex-tie-multifile-symbols))
1750 1576
1577 (reftex-ensure-compiled-variables)
1578
1751 (when (or (null (symbol-value reftex-docstruct-symbol)) 1579 (when (or (null (symbol-value reftex-docstruct-symbol))
1752 (member rescan '(t 1 (4) (16)))) 1580 (member rescan '(t 1 (4) (16))))
1753 ;; The docstruct will change: Remove selection buffers. 1581 ;; The docstruct will change: Remove selection buffers.
@@ -1755,18 +1583,33 @@ labels."
1755 (reftex-erase-all-selection-buffers))) 1583 (reftex-erase-all-selection-buffers)))
1756 1584
1757 (if (and (null (symbol-value reftex-docstruct-symbol)) 1585 (if (and (null (symbol-value reftex-docstruct-symbol))
1586 (not (member rescan '(t 1 (4) (16))))
1758 reftex-save-parse-info) 1587 reftex-save-parse-info)
1759 ;; Try to read the stuff from a file 1588 ;; Try to read the stuff from a file
1760 (reftex-access-parse-file 'read)) 1589 (reftex-access-parse-file 'read))
1761 1590
1762 (cond 1591 (cond
1592 ((equal rescan -1)) ;; We are not allowed to scan.
1763 ((not (symbol-value reftex-docstruct-symbol)) 1593 ((not (symbol-value reftex-docstruct-symbol))
1594 ;; Scan the whole document
1764 (reftex-do-parse 1 file)) 1595 (reftex-do-parse 1 file))
1765 ((member rescan '(t 1 (4) (16))) 1596 ((member rescan '(t 1 (4) (16)))
1597 ;; Scan whatever was required by the caller.
1766 (reftex-do-parse rescan file)))) 1598 (reftex-do-parse rescan file))))
1767 1599
1600(defun reftex-parse-one ()
1601 "Re-parse this file."
1602 (interactive)
1603 (let ((reftex-enable-partial-scans t))
1604 (reftex-access-scan-info '(4))))
1605
1606(defun reftex-parse-all ()
1607 "Re-parse entire document."
1608 (interactive)
1609 (reftex-access-scan-info '(16)))
1610
1768(defun reftex-do-parse (rescan &optional file) 1611(defun reftex-do-parse (rescan &optional file)
1769 ;; Do a rescan. When allowed, do only a partial scan from FILE. 1612 "Do a document rescan. When allowed, do only a partial scan from FILE."
1770 1613
1771 ;; Normalize the rescan argument 1614 ;; Normalize the rescan argument
1772 (setq rescan (cond ((eq rescan t) t) 1615 (setq rescan (cond ((eq rescan t) t)
@@ -1783,25 +1626,28 @@ labels."
1783 1626
1784 (let* ((old-list (symbol-value reftex-docstruct-symbol)) 1627 (let* ((old-list (symbol-value reftex-docstruct-symbol))
1785 (master (reftex-TeX-master-file)) 1628 (master (reftex-TeX-master-file))
1629 (true-master (file-truename master))
1786 (master-dir (file-name-as-directory (file-name-directory master))) 1630 (master-dir (file-name-as-directory (file-name-directory master)))
1787 (file (or file (buffer-file-name))) 1631 (file (or file (buffer-file-name)))
1632 (true-file (file-truename file))
1633 (bibview-cache (assq 'bibview-cache old-list))
1788 from-file appendix docstruct tmp) 1634 from-file appendix docstruct tmp)
1789 1635
1790 ;; Make sure replacement is really an option here 1636 ;; Make sure replacement is really an option here
1791 (when (and (eq rescan t) 1637 (when (and (eq rescan t)
1792 (not (and (member (list 'bof file) old-list) 1638 (not (and (member (list 'bof file) old-list)
1793 (member (list 'eof file) old-list)))) 1639 (member (list 'eof file) old-list))))
1794 (message "Scanning whole document (no file section %s)" file) 1640 ;; Scan whole document because no such file section exists
1795 (setq rescan 1)) 1641 (setq rescan 1))
1796 (when (string= file master) 1642 (when (string= true-file true-master)
1797 (message "Scanning whole document (%s is master)" file) 1643 ;; Scan whole document because this file is the master
1798 (setq rescan 1)) 1644 (setq rescan 1))
1799 1645
1800 ;; From which file do we start? 1646 ;; From which file do we start?
1801 (setq from-file 1647 (setq from-file
1802 (cond ((eq rescan t) (or file master)) 1648 (cond ((eq rescan t) (or file master))
1803 ((eq rescan 1) master) 1649 ((eq rescan 1) master)
1804 (t (error "horrible!!")))) 1650 (t (error "This should not happen (reftex-do-parse)"))))
1805 1651
1806 ;; Find active toc entry and initialize section-numbers 1652 ;; Find active toc entry and initialize section-numbers
1807 (setq reftex-active-toc (reftex-last-assoc-before-elt 1653 (setq reftex-active-toc (reftex-last-assoc-before-elt
@@ -1837,6 +1683,8 @@ labels."
1837 (push (cons 'label-numbers nil) docstruct)) 1683 (push (cons 'label-numbers nil) docstruct))
1838 (unless (assq 'master-dir docstruct) 1684 (unless (assq 'master-dir docstruct)
1839 (push (cons 'master-dir master-dir) docstruct)) 1685 (push (cons 'master-dir master-dir) docstruct))
1686 (unless (assq 'bibview-cache docstruct)
1687 (push (cons 'bibview-cache (cdr bibview-cache)) docstruct))
1840 (let* ((bof1 (memq (assq 'bof docstruct) docstruct)) 1688 (let* ((bof1 (memq (assq 'bof docstruct) docstruct))
1841 (bof2 (assq 'bof (cdr bof1))) 1689 (bof2 (assq 'bof (cdr bof1)))
1842 (is-multi (not (not (and bof1 bof2)))) 1690 (is-multi (not (not (and bof1 bof2))))
@@ -1855,8 +1703,8 @@ labels."
1855 (nth 2 x)) 1703 (nth 2 x))
1856 nil))) 1704 nil)))
1857 allxr)) 1705 allxr))
1858 (alist (delete nil alist)) 1706 (alist (delq nil alist))
1859 (allprefix (delete nil (mapcar 'car alist))) 1707 (allprefix (delq nil (mapcar 'car alist)))
1860 (regexp (if allprefix 1708 (regexp (if allprefix
1861 (concat "\\`\\(" 1709 (concat "\\`\\("
1862 (mapconcat 'identity allprefix "\\|") 1710 (mapconcat 'identity allprefix "\\|")
@@ -1865,17 +1713,7 @@ labels."
1865 (push (list 'xr alist regexp) docstruct))) 1713 (push (list 'xr alist regexp) docstruct)))
1866 1714
1867 (set reftex-docstruct-symbol docstruct) 1715 (set reftex-docstruct-symbol docstruct)
1868 1716 (put reftex-docstruct-symbol 'modified t)))
1869 ;; Save the parsing information into a file?
1870 (if reftex-save-parse-info
1871 (reftex-access-parse-file 'write))))
1872
1873(defun reftex-is-multi ()
1874 ;; Tell if this is a multifile document. When not sure, say yes.
1875 (let ((entry (assq 'is-multi (symbol-value reftex-docstruct-symbol))))
1876 (if entry
1877 (nth 1 entry)
1878 t)))
1879 1717
1880(defun reftex-parse-from-file (file docstruct master-dir) 1718(defun reftex-parse-from-file (file docstruct master-dir)
1881 ;; Scan the buffer for labels and save them in a list. 1719 ;; Scan the buffer for labels and save them in a list.
@@ -1884,10 +1722,14 @@ labels."
1884 file-found tmp include-file 1722 file-found tmp include-file
1885 (level 1) 1723 (level 1)
1886 (highest-level 100) 1724 (highest-level 100)
1887 toc-entry next-buf) 1725 toc-entry next-buf buf)
1888 1726
1889 (catch 'exit 1727 (catch 'exit
1890 (setq file-found (reftex-find-tex-file file master-dir)) 1728 (setq file-found (reftex-find-tex-file file master-dir))
1729 (if (and (not file-found)
1730 (setq buf (reftex-get-buffer-visiting file)))
1731 (setq file-found (buffer-file-name buf)))
1732
1891 (unless file-found 1733 (unless file-found
1892 (push (list 'file-error file) docstruct) 1734 (push (list 'file-error file) docstruct)
1893 (throw 'exit nil)) 1735 (throw 'exit nil))
@@ -1930,7 +1772,7 @@ labels."
1930 (if (= level highest-level) 1772 (if (= level highest-level)
1931 (message 1773 (message
1932 "Scanning %s %s ..." 1774 "Scanning %s %s ..."
1933 (car (nth level reftex-section-levels)) 1775 (car (rassoc level reftex-section-levels))
1934 (nth 6 toc-entry))) 1776 (nth 6 toc-entry)))
1935 1777
1936 (push toc-entry docstruct) 1778 (push toc-entry docstruct)
@@ -1939,13 +1781,10 @@ labels."
1939 ((match-end 7) 1781 ((match-end 7)
1940 ;; It's an include or input 1782 ;; It's an include or input
1941 (setq include-file (reftex-match-string 7)) 1783 (setq include-file (reftex-match-string 7))
1942 (if (delq nil (mapcar 1784 ;; Test if this file should be ignored
1943 (lambda (x) (string-match x include-file)) 1785 (unless (delq nil (mapcar
1944 reftex-no-include-regexps)) 1786 (lambda (x) (string-match x include-file))
1945 (progn 1787 reftex-no-include-regexps))
1946 ;; Ignore the file
1947 (push (list 'bof include-file) docstruct)
1948 (push (list 'eof include-file) docstruct))
1949 ;; Parse it 1788 ;; Parse it
1950 (setq docstruct 1789 (setq docstruct
1951 (reftex-parse-from-file 1790 (reftex-parse-from-file
@@ -1966,14 +1805,19 @@ labels."
1966 (reftex-no-props 1805 (reftex-no-props
1967 (reftex-nth-arg-wrapper 1806 (reftex-nth-arg-wrapper
1968 mac))))) 1807 mac)))))
1969 (entry (progn (goto-char (match-end 0)) 1808 (typekey (nth 1 (assoc mac reftex-env-or-mac-alist)))
1970 (reftex-label-info 1809 (entry (progn (if typekey
1971 label file bound mac)))) 1810 ;; A typing macro
1811 (goto-char (match-end 0))
1812 ;; A newtral macro
1813 (goto-char (match-end 10))
1814 (reftex-move-over-touching-args))
1815 (reftex-label-info
1816 label file bound nil nil))))
1972 (push entry docstruct)))) 1817 (push entry docstruct))))
1973 (t (error "This should not happen (reftex-parse-from-file)"))) 1818 (t (error "This should not happen (reftex-parse-from-file)")))
1974 ) 1819 )
1975 1820
1976
1977 ;; Find bibliography statement 1821 ;; Find bibliography statement
1978 (when (setq tmp (reftex-locate-bibliography-files master-dir)) 1822 (when (setq tmp (reftex-locate-bibliography-files master-dir))
1979 (push (cons 'bib tmp) docstruct)) 1823 (push (cons 'bib tmp) docstruct))
@@ -2001,38 +1845,27 @@ labels."
2001 1845
2002(defun reftex-locate-bibliography-files (master-dir) 1846(defun reftex-locate-bibliography-files (master-dir)
2003 ;; Scan buffer for bibliography macro and return file list. 1847 ;; Scan buffer for bibliography macro and return file list.
2004 (let (file-list) 1848 (let (files)
2005 (save-excursion 1849 (save-excursion
2006 (goto-char (point-min)) 1850 (goto-char (point-min))
2007 (if (re-search-forward 1851 (when (re-search-forward
2008 "\\(\\`\\|[\n\r]\\)[ \t]*\\\\bibliography{[ \t]*\\([^}]+\\)" nil t) 1852 "\\(\\`\\|[\n\r]\\)[ \t]*\\\\bibliography{[ \t]*\\([^}]+\\)" nil t)
2009 (setq file-list 1853 (setq files (split-string (reftex-match-string 2)
2010 (mapcar (function (lambda (x) (concat x ".bib"))) 1854 "[ \t\n\r]*,[ \t\n\r]*"))
2011 (reftex-delete-list 1855 (setq files
2012 reftex-bibfile-ignore-list 1856 (mapcar
2013 (split-string 1857 (lambda (x)
2014 (reftex-match-string 2) 1858 (if (or (member x reftex-bibfile-ignore-list)
2015 "[ \t\n\r]*,[ \t\n\r]*"))))) 1859 (delq nil (mapcar (lambda (re) (string-match re x))
2016 (delete nil 1860 reftex-bibfile-ignore-regexps)))
2017 (mapcar 1861 ;; excluded file
2018 (function 1862 nil
2019 (lambda (file) 1863 ;; find the file
2020 (reftex-find-bib-file file master-dir))) 1864 (reftex-find-bib-file
2021 file-list))))) 1865 (if (string-match "\\.bib\\'" x) x (concat x ".bib"))
2022 1866 master-dir)))
2023(defun reftex-last-assoc-before-elt (key elt list) 1867 files))
2024 ;; Find the last association of KEY in LIST before or at ELT 1868 (delq nil files)))))
2025 ;; ELT is found in LIST with equal, not eq.
2026 ;; Returns nil when either KEY or elt are not found in LIST.
2027 ;; On success, returns the association.
2028 (let* ((elt (car (member elt list))) ass last-ass)
2029
2030 (while (and (setq ass (assoc key list))
2031 (setq list (memq ass list))
2032 (memq elt list))
2033 (setq last-ass ass
2034 list (cdr list)))
2035 last-ass))
2036 1869
2037(defun reftex-replace-label-list-segment (old insert &optional entirely) 1870(defun reftex-replace-label-list-segment (old insert &optional entirely)
2038 ;; Replace the segment in OLD which corresponds to INSERT. 1871 ;; Replace the segment in OLD which corresponds to INSERT.
@@ -2060,7 +1893,7 @@ labels."
2060 new)))) 1893 new))))
2061 1894
2062(defun reftex-silence-toc-markers (list n) 1895(defun reftex-silence-toc-markers (list n)
2063 ;; Set all markers in list to nil 1896 ;; Set all toc markers in the first N entries in list to nil
2064 (while (and list (> (decf n) -1)) 1897 (while (and list (> (decf n) -1))
2065 (and (eq (car (car list)) 'toc) 1898 (and (eq (car (car list)) 'toc)
2066 (markerp (nth 4 (car list))) 1899 (markerp (nth 4 (car list)))
@@ -2068,7 +1901,10 @@ labels."
2068 (pop list))) 1901 (pop list)))
2069 1902
2070(defun reftex-access-parse-file (action) 1903(defun reftex-access-parse-file (action)
1904 "Perform ACTION on the parse file (the .rel file).
1905Valid actions are: readable, restore, read, kill, write."
2071 (let* ((list (symbol-value reftex-docstruct-symbol)) 1906 (let* ((list (symbol-value reftex-docstruct-symbol))
1907 (docstruct-symbol reftex-docstruct-symbol)
2072 (master (reftex-TeX-master-file)) 1908 (master (reftex-TeX-master-file))
2073 (enable-local-variables nil) 1909 (enable-local-variables nil)
2074 (file (if (string-match "\\.[a-zA-Z]+\\'" master) 1910 (file (if (string-match "\\.[a-zA-Z]+\\'" master)
@@ -2078,21 +1914,36 @@ labels."
2078 ((eq action 'readable) 1914 ((eq action 'readable)
2079 (file-readable-p file)) 1915 (file-readable-p file))
2080 ((eq action 'restore) 1916 ((eq action 'restore)
1917 (put reftex-docstruct-symbol 'modified nil)
2081 (if (eq reftex-docstruct-symbol nil) 1918 (if (eq reftex-docstruct-symbol nil)
2082 ;; Symbols are not yet tied: Tie them. 1919 ;; Symbols are not yet tied: Tie them.
2083 (reftex-tie-multifile-symbols)) 1920 (reftex-tie-multifile-symbols))
2084 (if (file-exists-p file) 1921 (if (file-exists-p file)
2085 ;; load the file and return t for success 1922 ;; load the file and return t for success
2086 (progn (load-file file) t) 1923 (condition-case nil
2087 ;; throw an exception 1924 (progn (load-file file) t)
1925 (error (set reftex-docstruct-symbol nil)
1926 (error "Error while loading file %s" file)))
1927 ;; Throw an exception if the file does not exist
2088 (error "No restore file %s" file))) 1928 (error "No restore file %s" file)))
2089 ((eq action 'read) 1929 ((eq action 'read)
1930 (put reftex-docstruct-symbol 'modified nil)
2090 (if (file-exists-p file) 1931 (if (file-exists-p file)
2091 ;; load the file and return t for success 1932 ;; load the file and return t for success
2092 (progn (load-file file) t) 1933 (condition-case nil
1934 (progn (load-file file) t)
1935 (error (message "Error while loading file %s" file)
1936 (set reftex-docstruct-symbol nil)
1937 nil))
2093 ;; return nil for failure, but no exception 1938 ;; return nil for failure, but no exception
2094 nil)) 1939 nil))
1940 ((eq action 'kill)
1941 ;; Remove the file
1942 (when (and (file-exists-p file) (file-writable-p file))
1943 (message "Unlinking file %s" file)
1944 (delete-file file)))
2095 (t 1945 (t
1946 (put docstruct-symbol 'modified nil)
2096 (save-excursion 1947 (save-excursion
2097 (if (file-writable-p file) 1948 (if (file-writable-p file)
2098 (progn 1949 (progn
@@ -2101,9 +1952,6 @@ labels."
2101 (erase-buffer) 1952 (erase-buffer)
2102 (insert (format ";; RefTeX parse info file\n")) 1953 (insert (format ";; RefTeX parse info file\n"))
2103 (insert (format ";; File: %s\n" master)) 1954 (insert (format ";; File: %s\n" master))
2104 (insert (format ";; Date: %s\n"
2105 (format-time-string "%D %T"
2106 (current-time))))
2107 (insert (format ";; User: %s (%s)\n\n" 1955 (insert (format ";; User: %s (%s)\n\n"
2108 (user-login-name) (user-full-name))) 1956 (user-login-name) (user-full-name)))
2109 (insert "(set reftex-docstruct-symbol '(\n\n") 1957 (insert "(set reftex-docstruct-symbol '(\n\n")
@@ -2121,13 +1969,487 @@ labels."
2121 (nth 8 x))))) 1969 (nth 8 x)))))
2122 (t (print x))))) 1970 (t (print x)))))
2123 list)) 1971 list))
2124 (insert "))") 1972 (insert "))\n\n")
2125 (save-buffer 0) 1973 (save-buffer 0)
2126 (kill-buffer (current-buffer))) 1974 (kill-buffer (current-buffer)))
2127 (error "Cannot write to file %s" file))) 1975 (error "Cannot write to file %s" file)))
2128 t)))) 1976 t))))
2129 1977
2130;; Creating labels -------------- 1978(defun reftex-kill-buffer-hook ()
1979 "Save RefTeX's parse file for this buffer if the information has changed."
1980 ;; Save the parsing information if it was modified.
1981 ;; This function should be installed in `kill-buffer-hook'.
1982 ;; We are careful to make sure nothing goes wring in this function.
1983 (when (and (boundp 'reftex-mode) reftex-mode
1984 (boundp 'reftex-save-parse-info) reftex-save-parse-info
1985 (boundp 'reftex-docstruct-symbol) reftex-docstruct-symbol
1986 (symbol-value reftex-docstruct-symbol)
1987 (get reftex-docstruct-symbol 'modified))
1988 ;; Write the file.
1989 (condition-case nil
1990 (reftex-access-parse-file 'write)
1991 (error nil))))
1992
1993(defun reftex-kill-emacs-hook ()
1994 "Call `reftex-kill-buffer-hook' on all buffers."
1995 ;; This function should be installed in `kill-emacs-hook'.
1996 (save-excursion
1997 (mapcar (lambda (buf)
1998 (set-buffer buf)
1999 (reftex-kill-buffer-hook))
2000 (buffer-list))))
2001
2002(defun reftex-section-info (file)
2003 ;; Return a section entry for the current match.
2004 ;; Carefull: This function expects the match-data to be still in place!
2005 (let* ((marker (set-marker (make-marker) (1- (match-beginning 3))))
2006 (macro (reftex-match-string 3))
2007 (star (= ?* (char-after (match-end 3))))
2008 (level (cdr (assoc macro reftex-section-levels-all)))
2009 (section-number (reftex-section-number level star))
2010 (text1 (save-match-data (save-excursion (reftex-context-substring))))
2011 (literal (buffer-substring-no-properties
2012 (1- (match-beginning 3))
2013 (min (point-max) (+ (match-end 0) (length text1) 1))))
2014 ;; Literal can be too short since text1 too short. No big problem.
2015 (text (reftex-nicify-text text1)))
2016
2017 ;; Add section number and indentation
2018 (setq text
2019 (concat
2020 (make-string (* reftex-level-indent level) ?\ )
2021 (if (nth 1 reftex-label-menu-flags) ; section number flag
2022 (concat section-number " "))
2023 text))
2024 (list 'toc "toc" text file marker level section-number
2025 literal (marker-position marker))))
2026
2027(defun reftex-label-info-update (cell)
2028 ;; Update information about just one label in a different file.
2029 ;; CELL contains the old info list
2030 (let* ((label (nth 0 cell))
2031 (typekey (nth 1 cell))
2032 ;; (text (nth 2 cell))
2033 (file (nth 3 cell))
2034 (comment (nth 4 cell))
2035 (note (nth 5 cell))
2036 (buf (reftex-get-file-buffer-force
2037 file (not (eq t reftex-keep-temporary-buffers)))))
2038 (if (not buf)
2039 (list label typekey "" file comment "LOST LABEL. RESCAN TO FIX.")
2040 (save-excursion
2041 (set-buffer buf)
2042 (save-restriction
2043 (widen)
2044 (goto-char 1)
2045
2046 (if (or (re-search-forward
2047 (format reftex-find-label-regexp-format
2048 (regexp-quote label)) nil t)
2049 (re-search-forward
2050 (format reftex-find-label-regexp-format2
2051 (regexp-quote label)) nil t))
2052
2053 (progn
2054 (backward-char 1)
2055 (append (reftex-label-info label file) (list note)))
2056 (list label typekey "" file "LOST LABEL. RESCAN TO FIX.")))))))
2057
2058(defun reftex-label-info (label &optional file bound derive env-or-mac)
2059 ;; Return info list on LABEL at point.
2060 (let* ((env-or-mac (or env-or-mac (reftex-label-location bound)))
2061 (typekey (nth 1 (assoc env-or-mac reftex-env-or-mac-alist)))
2062 (file (or file (buffer-file-name)))
2063 (parse (if (reftex-typekey-check
2064 typekey reftex-use-text-after-label-as-context)
2065 nil
2066 (nth 2 (assoc env-or-mac reftex-env-or-mac-alist))))
2067 (text (reftex-short-context env-or-mac parse reftex-location-start
2068 derive))
2069 (in-comment (reftex-in-comment)))
2070 (list label typekey text file in-comment)))
2071
2072(defun reftex-in-comment ()
2073 (save-excursion
2074 (skip-chars-backward "^%\n\r")
2075 (eq (preceding-char) ?%)))
2076
2077(defun reftex-short-context (env parse &optional bound derive)
2078 ;; Get about one line of useful context for the label definition at point.
2079
2080 (if (consp parse)
2081 (setq parse (if derive (cdr parse) (car parse))))
2082
2083 (reftex-nicify-text
2084
2085 (cond
2086
2087 ((null parse)
2088 (save-excursion
2089 (reftex-context-substring)))
2090
2091 ((eq parse t)
2092 (if (string= env "section")
2093 ;; special treatment for section labels
2094 (save-excursion
2095 (if (and (re-search-backward reftex-section-or-include-regexp
2096 (point-min) t)
2097 (match-end 2))
2098 (progn
2099 (goto-char (match-end 0))
2100 (reftex-context-substring))
2101 (if reftex-active-toc
2102 (progn
2103 (string-match "{\\([^}]*\\)" (nth 7 reftex-active-toc))
2104 (match-string 1 (nth 7 reftex-active-toc)))
2105 "SECTION HEADING NOT FOUND")))
2106 (save-excursion
2107 (goto-char reftex-default-context-position)
2108 (unless (eq (string-to-char env) ?\\)
2109 (reftex-move-over-touching-args))
2110 (reftex-context-substring))))
2111
2112 ((stringp parse)
2113 (save-excursion
2114 (if (re-search-backward parse bound t)
2115 (progn
2116 (goto-char (match-end 0))
2117 (reftex-context-substring))
2118 "NO MATCH FOR CONTEXT REGEXP")))
2119
2120 ((integerp parse)
2121 (or (save-excursion
2122 (goto-char reftex-default-context-position)
2123 (reftex-nth-arg
2124 parse
2125 (nth 6 (assoc env reftex-env-or-mac-alist))))
2126 ""))
2127
2128 ((fboundp parse)
2129 ;; A hook function. Call it.
2130 (save-excursion
2131 (condition-case error-var
2132 (funcall parse env)
2133 (error (format "HOOK ERROR: %s" (cdr error-var))))))
2134 (t
2135 "ILLEGAL VALUE OF PARSE"))))
2136
2137(defun reftex-nicify-text (text)
2138 ;; Make TEXT nice for inclusion as context into label menu
2139 ;; remove line breaks and extra white space
2140 (while (string-match "[\n\r\t]\\|[ \t][ \t]+" text)
2141 (setq text (replace-match " " nil t text)))
2142 ;; cut before the next `\end{' or `\item' or `\\'
2143 (if (string-match "\\(\\\\end{\\|\\\\item\\|\\\\\\\\\\).*" text)
2144 (setq text (replace-match "" nil t text)))
2145 ;; kill the embedded label
2146 (if (string-match "\\\\label{[^}]*}" text)
2147 (setq text (replace-match "" nil t text)))
2148 ;; remove leading garbage
2149 (if (string-match "\\`[ }]+" text)
2150 (setq text (replace-match "" nil t text)))
2151 ;; limit length
2152 (cond
2153 ((> (length text) 100) (substring text 0 100))
2154 ((= (length text) 0) (make-string 1 ?\ ))
2155 (t text)))
2156
2157(defun reftex-where-am-I ()
2158 ;; Return the docstruct entry above point. Actually returns a cons
2159 ;; cell in which the cdr is a flag indicating if the information is
2160 ;; exact (t) or approximate (nil).
2161
2162 (let ((docstruct (symbol-value reftex-docstruct-symbol))
2163 (cnt 0) rtn
2164 found)
2165 (save-excursion
2166 (while (not rtn)
2167 (incf cnt)
2168 (setq found (re-search-backward reftex-everything-regexp nil t))
2169 (setq rtn
2170 (cond
2171 ((not found)
2172 ;; no match
2173 (or
2174 (car (member (list 'bof (buffer-file-name)) docstruct))
2175 (not (setq cnt 2))
2176 (assq 'bof docstruct) ;; for safety reasons
2177 'corrupted))
2178 ((match-end 1)
2179 ;; Label
2180 (assoc (reftex-match-string 1)
2181 (symbol-value reftex-docstruct-symbol)))
2182 ((match-end 3)
2183 ;; Section
2184 (goto-char (1- (match-beginning 3)))
2185 (let* ((list (member (list 'bof (buffer-file-name))
2186 docstruct))
2187 (endelt (car (member (list 'eof (buffer-file-name))
2188 list)))
2189 rtn1)
2190 (while (and list (not (eq endelt (car list))))
2191 (if (and (eq (car (car list)) 'toc)
2192 (string= (buffer-file-name)
2193 (nth 3 (car list))))
2194 (cond
2195 ((equal (point)
2196 (or (and (markerp (nth 4 (car list)))
2197 (marker-position (nth 4 (car list))))
2198 (nth 8 (car list))))
2199 ;; Fits with marker position or recorded position
2200 (setq rtn1 (car list) list nil))
2201 ((looking-at (reftex-make-regexp-allow-for-ctrl-m
2202 (nth 7 (car list))))
2203 ;; Same title
2204 (setq rtn1 (car list) list nil cnt 2))))
2205 (pop list))
2206 rtn1))
2207 ((match-end 7)
2208 ;; Input or include...
2209 (car
2210 (member (list 'eof (reftex-find-tex-file
2211 (reftex-match-string 7)
2212 (cdr (assq 'master-dir docstruct))))
2213 docstruct)))
2214 ((match-end 9)
2215 (assq 'appendix (symbol-value reftex-docstruct-symbol)))
2216 ((match-end 10)
2217 (save-excursion
2218 (goto-char (match-end 10))
2219 (assoc (reftex-no-props
2220 (reftex-nth-arg-wrapper
2221 (reftex-match-string 10)))
2222 (symbol-value reftex-docstruct-symbol))))
2223 (t
2224 (error "This should not happen (reftex-where-am-I)"))))))
2225 (cons rtn (eq cnt 1))))
2226
2227(defun reftex-label-location (&optional bound)
2228 "Return the environment or macro which determines the label type at point.
2229If optional BOUND is an integer, limit backward searches to that point."
2230
2231 (let* ((loc1 (reftex-what-macro reftex-label-mac-list bound))
2232 (loc2 (reftex-what-environment reftex-label-env-list bound))
2233 (p1 (or (cdr loc1) 0))
2234 (p2 (or (cdr loc2) 0)))
2235
2236 (setq reftex-location-start (max p1 p2))
2237 (if (>= p1 p2)
2238 (progn
2239 (setq reftex-default-context-position (+ p1 (length (car loc1))))
2240 (or (car loc1) "section"))
2241 (setq reftex-default-context-position (+ p2 8 (length (car loc2))))
2242 (or (car loc2) "section"))))
2243
2244(defun reftex-parse-args (macro)
2245 ;; Return a list of macro name, nargs, arg-nr which is label and a list of
2246 ;; optional argument indices.
2247 (if (string-match "[[{]\\*?[]}]" macro)
2248 (progn
2249 (let ((must-match (substring macro 0 (match-beginning 0)))
2250 (args (substring macro (match-beginning 0)))
2251 opt-list nlabel (cnt 0))
2252 (while (string-match "\\`[[{]\\(\\*\\)?[]}]" args)
2253 (incf cnt)
2254 (when (eq ?\[ (string-to-char args))
2255 (push cnt opt-list))
2256 (when (and (match-end 1)
2257 (not nlabel))
2258 (setq nlabel cnt))
2259 (setq args (substring args (match-end 0))))
2260 (list must-match cnt nlabel opt-list)))
2261 nil))
2262
2263(defsubst reftex-move-to-next-arg (&optional ignore)
2264 ;; Assuming that we are at the end of a macro name or a macro argument,
2265 ;; move forward to the opening parenthesis of the next argument.
2266 ;; This function understands the splitting of macros over several lines
2267 ;; in TeX.
2268 (cond
2269 ;; Just to be quick:
2270 ((memq (following-char) '(?\[ ?\{)))
2271 ;; Do a search
2272 ((and reftex-allow-detached-macro-args
2273 (looking-at "[ \t]*[\n\r]?\\([ \t]*%[^\n\r]*[\n\r]\\)*[ \t]*[[{]"))
2274 (goto-char (1- (match-end 0)))
2275 t)
2276 (t nil)))
2277
2278(defsubst reftex-move-to-previous-arg (&optional bound)
2279 ;; Assuming that we are in front of a macro argument,
2280 ;; move backward to the closing parenthesis of the previous argument.
2281 ;; This function understands the splitting of macros over several lines
2282 ;; in TeX.
2283 (cond
2284 ;; Just to be quick:
2285 ((memq (preceding-char) '(?\] ?\})))
2286 ;; Do a search
2287 ((and reftex-allow-detached-macro-args
2288 (re-search-backward
2289 "[]}][ \t]*[\n\r]?\\([ \t]*%[^\n\r]*[\n\r]\\)*[ \t]*\\=" bound t))
2290 (goto-char (1+ (match-beginning 0)))
2291 t)
2292 (t nil)))
2293
2294(defun reftex-nth-arg-wrapper (key)
2295 (let ((entry (assoc key reftex-env-or-mac-alist)))
2296 (reftex-nth-arg (nth 5 entry) (nth 6 entry))))
2297
2298(defun reftex-nth-arg (n &optional opt-args)
2299 ;; Return the nth following {} or [] parentheses content.
2300 ;; OPT-ARGS is a list of argument numbers which are optional.
2301
2302 ;; If we are sitting at a macro start, skip to end of macro name.
2303 (and (eq (following-char) ?\\) (skip-chars-forward "a-zA-Z*\\\\"))
2304
2305 (if (= n 1000)
2306 ;; Special case: Skip all touching arguments
2307 (progn
2308 (reftex-move-over-touching-args)
2309 (reftex-context-substring))
2310
2311 ;; Do the real thing.
2312 (let ((cnt 1))
2313
2314 (when (reftex-move-to-next-arg)
2315
2316 (while (< cnt n)
2317 (while (and (member cnt opt-args)
2318 (eq (following-char) ?\{))
2319 (incf cnt))
2320 (when (< cnt n)
2321 (unless (and (condition-case nil
2322 (or (forward-list 1) t)
2323 (error nil))
2324 (reftex-move-to-next-arg)
2325 (incf cnt))
2326 (setq cnt 1000))))
2327
2328 (while (and (memq cnt opt-args)
2329 (eq (following-char) ?\{))
2330 (incf cnt)))
2331 (if (and (= n cnt)
2332 (> (skip-chars-forward "{\\[") 0))
2333 (reftex-context-substring)
2334 nil))))
2335
2336(defun reftex-move-over-touching-args ()
2337 (condition-case nil
2338 (while (memq (following-char) '(?\[ ?\{))
2339 (forward-list 1))
2340 (error nil)))
2341
2342(defun reftex-context-substring ()
2343 ;; Return up to 100 chars from point
2344 ;; When point is just after a { or [, limit string to matching parenthesis
2345 (cond
2346 ((or (= (preceding-char) ?\{)
2347 (= (preceding-char) ?\[))
2348 ;; Inside a list - get only the list.
2349 (buffer-substring-no-properties
2350 (point)
2351 (min (+ (point) 150)
2352 (point-max)
2353 (condition-case nil
2354 (progn
2355 (up-list 1)
2356 (1- (point)))
2357 (error (point-max))))))
2358 (t
2359 ;; no list - just grab 100 characters
2360 (buffer-substring-no-properties (point)
2361 (min (+ (point) 150) (point-max))))))
2362
2363;; Variable holding the vector with section numbers
2364(defvar reftex-section-numbers [0 0 0 0 0 0 0 0])
2365
2366(defun reftex-init-section-numbers (&optional toc-entry appendix)
2367 ;; Initialize the section numbers with zeros or with what is found
2368 ;; in the toc entry.
2369 (let* ((level (or (nth 5 toc-entry) -1))
2370 (numbers (nreverse (split-string (or (nth 6 toc-entry) "") "\\.")))
2371 (depth (1- (length reftex-section-numbers)))
2372 (i depth) number-string)
2373 (while (>= i 0)
2374 (if (> i level)
2375 (aset reftex-section-numbers i 0)
2376 (setq number-string (or (car numbers) "0"))
2377 (if (string-match "\\`[A-Z]\\'" number-string)
2378 (aset reftex-section-numbers i
2379 (- (string-to-char number-string) ?A -1))
2380 (aset reftex-section-numbers i (string-to-int number-string)))
2381 (pop numbers))
2382 (decf i)))
2383 (put 'reftex-section-numbers 'appendix appendix))
2384
2385(defun reftex-section-number (&optional level star)
2386 ;; Return a string with the current section number.
2387 ;; When LEVEL is non-nil, increase section numbers on that level.
2388 (let* ((depth (1- (length reftex-section-numbers))) idx n (string "")
2389 (appendix (get 'reftex-section-numbers 'appendix)))
2390 (when level
2391 (when (and (> level -1) (not star))
2392 (aset reftex-section-numbers
2393 level (1+ (aref reftex-section-numbers level))))
2394 (setq idx (1+ level))
2395 (when (not star)
2396 (while (<= idx depth)
2397 (aset reftex-section-numbers idx 0)
2398 (incf idx))))
2399 (setq idx 0)
2400 (while (<= idx depth)
2401 (setq n (aref reftex-section-numbers idx))
2402 (setq string (concat string (if (not (string= string "")) "." "")
2403 (int-to-string n)))
2404 (incf idx))
2405 (save-match-data
2406 (if (string-match "\\`\\([@0]\\.\\)+" string)
2407 (setq string (replace-match "" nil nil string)))
2408 (if (string-match "\\(\\.0\\)+\\'" string)
2409 (setq string (replace-match "" nil nil string)))
2410 (if (and appendix
2411 (string-match "\\`[0-9]+" string))
2412 (setq string
2413 (concat
2414 (char-to-string
2415 (1- (+ ?A (string-to-int (match-string 0 string)))))
2416 (substring string (match-end 0))))))
2417 (if star
2418 (concat (make-string (1- (length string)) ?\ ) "*")
2419 string)))
2420
2421(defun reftex-is-multi ()
2422 ;; Tell if this is a multifile document. When not sure, say yes.
2423 (let ((entry (assq 'is-multi (symbol-value reftex-docstruct-symbol))))
2424 (if entry
2425 (nth 1 entry)
2426 t)))
2427
2428(defun reftex-typekey-check (typekey conf-variable &optional n)
2429 ;; Check if CONF-VARIABLE is true or contains TYPEKEY
2430 (and n (setq conf-variable (nth n conf-variable)))
2431 (or (eq conf-variable t)
2432 (and (stringp conf-variable)
2433 (string-match (concat "[" conf-variable "]") typekey))))
2434
2435(defun reftex-all-document-files (&optional relative)
2436 "Return a list of all files belonging to the current document.
2437When RELATIVE is non-nil, give file names relative to directory
2438of master file."
2439 (let* ((all (symbol-value reftex-docstruct-symbol))
2440 (master-dir (file-name-directory (reftex-TeX-master-file)))
2441 (re (concat "\\`" (regexp-quote master-dir)))
2442 file-list tmp file)
2443 (while (setq tmp (assoc 'bof all))
2444 (setq file (nth 1 tmp)
2445 all (cdr (memq tmp all)))
2446 (and relative
2447 (string-match re file)
2448 (setq file (substring file (match-end 0))))
2449 (push file file-list))
2450 (nreverse file-list)))
2451
2452;;; Creating labels ---------------------------------------------------------
2131 2453
2132(defun reftex-label (&optional environment no-insert) 2454(defun reftex-label (&optional environment no-insert)
2133 "Insert a unique label. Return the label. 2455 "Insert a unique label. Return the label.
@@ -2156,11 +2478,13 @@ This function is controlled by the settings of reftex-insert-label-flags."
2156 (let* ((entry (assoc environment reftex-env-or-mac-alist)) 2478 (let* ((entry (assoc environment reftex-env-or-mac-alist))
2157 (typekey (nth 1 entry)) 2479 (typekey (nth 1 entry))
2158 (format (nth 3 entry)) 2480 (format (nth 3 entry))
2159 label prefix valid default force-prompt rescan-is-useful) 2481 (macro-cell (reftex-what-macro 1))
2160 (when (and (eq (string-to-char environment) ?\\) 2482 (entry1 (assoc (car macro-cell) reftex-env-or-mac-alist))
2161 (nth 5 entry) 2483 label naked prefix valid default force-prompt rescan-is-useful)
2484 (when (and (or (nth 5 entry) (nth 5 entry1))
2162 (memq (preceding-char) '(?\[ ?\{))) 2485 (memq (preceding-char) '(?\[ ?\{)))
2163 (setq format "%s")) 2486 ;; This is an argument of a label macro. Insert naked label.
2487 (setq naked t format "%s"))
2164 2488
2165 (setq prefix (or (cdr (assoc typekey reftex-typekey-to-prefix-alist)) 2489 (setq prefix (or (cdr (assoc typekey reftex-typekey-to-prefix-alist))
2166 (concat typekey "-"))) 2490 (concat typekey "-")))
@@ -2185,7 +2509,9 @@ This function is controlled by the settings of reftex-insert-label-flags."
2185 (string-match "^[ \t]*$" default)) 2509 (string-match "^[ \t]*$" default))
2186 (setq default prefix 2510 (setq default prefix
2187 force-prompt t) ; need to prompt 2511 force-prompt t) ; need to prompt
2188 (setq default (concat prefix (reftex-string-to-label default))) 2512 (setq default
2513 (concat prefix
2514 (funcall reftex-string-to-label-function default)))
2189 2515
2190 ;; Make it unique. 2516 ;; Make it unique.
2191 (setq default (reftex-uniquify-label default nil "-")))) 2517 (setq default (reftex-uniquify-label default nil "-"))))
@@ -2207,7 +2533,7 @@ This function is controlled by the settings of reftex-insert-label-flags."
2207 ;; iterate until we get a legal label 2533 ;; iterate until we get a legal label
2208 2534
2209 (setq label (read-string 2535 (setq label (read-string
2210 (if (string= format "%s") "Naked Label: " "Label: ") 2536 (if naked "Naked Label: " "Label: ")
2211 default)) 2537 default))
2212 2538
2213 ;; Lets make sure that this is a legal label 2539 ;; Lets make sure that this is a legal label
@@ -2240,7 +2566,12 @@ This function is controlled by the settings of reftex-insert-label-flags."
2240 (setq label default)) 2566 (setq label default))
2241 2567
2242 ;; Insert the label into the label list 2568 ;; Insert the label into the label list
2243 (let* ((here-I-am-info (reftex-where-am-I)) 2569 (let* ((here-I-am-info
2570 (save-excursion
2571 (if (and (or naked no-insert)
2572 (integerp (cdr macro-cell)))
2573 (goto-char (cdr macro-cell)))
2574 (reftex-where-am-I)))
2244 (here-I-am (car here-I-am-info)) 2575 (here-I-am (car here-I-am-info))
2245 (note (if (cdr here-I-am-info) 2576 (note (if (cdr here-I-am-info)
2246 "" 2577 ""
@@ -2251,16 +2582,20 @@ This function is controlled by the settings of reftex-insert-label-flags."
2251 2582
2252 (or (cdr here-I-am-info) (setq rescan-is-useful t)) 2583 (or (cdr here-I-am-info) (setq rescan-is-useful t))
2253 2584
2254 (if tail 2585 (when tail
2255 (setcdr tail (cons (list label typekey text file note) 2586 (push (list label typekey text file nil note) (cdr tail))
2256 (cdr tail))))) 2587 (put reftex-docstruct-symbol 'modified t)))
2257 2588
2258 ;; Insert the label into the buffer 2589 ;; Insert the label into the buffer
2259 (unless no-insert 2590 (unless no-insert
2260 (insert 2591 (insert
2261 (if reftex-format-label-function 2592 (if reftex-format-label-function
2262 (funcall reftex-format-label-function label format) 2593 (funcall reftex-format-label-function label format)
2263 (format format label)))) 2594 (format format label)))
2595 (if (and reftex-plug-into-AUCTeX
2596 (fboundp 'LaTeX-add-labels))
2597 ;; Tell AUCTeX about this
2598 (LaTeX-add-labels label)))
2264 2599
2265 ;; Delete the corresponding selection buffers to force update on next use. 2600 ;; Delete the corresponding selection buffers to force update on next use.
2266 (when reftex-auto-update-selection-buffers 2601 (when reftex-auto-update-selection-buffers
@@ -2274,58 +2609,86 @@ This function is controlled by the settings of reftex-insert-label-flags."
2274 label))) 2609 label)))
2275 2610
2276(defun reftex-string-to-label (string) 2611(defun reftex-string-to-label (string)
2277 ;; Convert a string (a sentence) to a label. 2612 "Convert a string (a sentence) to a label.
2278 ;; 2613Uses `reftex-derive-label-parameters' and `reftex-label-illegal-re'. It
2279 ;; Uses reftex-derive-label-parameters and reftex-abbrev-parameters 2614also applies `reftex-translate-to-ascii-function' to the string."
2280 ;; 2615 (when (and reftex-translate-to-ascii-function
2281 2616 (fboundp reftex-translate-to-ascii-function))
2282 (let* ((words0 (split-string string "[- \t\n\r]+")) 2617 (setq string (funcall reftex-translate-to-ascii-function string)))
2283 (ignore-words (nth 5 reftex-derive-label-parameters)) 2618 (apply 'reftex-convert-string string
2619 "[-~ \t\n\r,;]+" reftex-label-illegal-re nil nil
2620 reftex-derive-label-parameters))
2621
2622(defun reftex-abbreviate-title (string)
2623 (reftex-convert-string string "[-~ \t\n\r,;]" nil t t
2624 5 40 nil 1 " " (nth 5 reftex-derive-label-parameters)))
2625
2626(defun reftex-convert-string (string split-re illegal-re dot keep-fp
2627 nwords maxchar illegal abbrev sep
2628 ignore-words &optional downcase)
2629 "Convert a string (a sentence) to something shorter.
2630SPLIT-RE is the regular expression used to split the string into words.
2631ILLEGAL-RE matches characters which are illegal in the final string.
2632DOT t means add dots to abbreviated words.
2633KEEP-FP t means to keep a final punctuation when applicable.
2634NWORDS Number of words to use.
2635MAXCHAR Maximum number of characters in the final string.
2636ILLEGAL nil: Throw away any words containing stuff matched with ILLEGAL-RE.
2637 t: Throw away only the matched part, not the whole word.
2638ABBREV nil: Never abbreviate words.
2639 t: Always abbreviate words (see `reftex-abbrev-parameters').
2640 not t and not nil: Abbreviate words if necessary to shorten
2641 string below MAXCHAR.
2642SEP String separating different words in the output string.
2643IGNORE-WORDS List of words which should be removed from the string."
2644
2645 (let* ((words0 (split-string string (or split-re "[ \t\n\r]")))
2284 words word) 2646 words word)
2285 2647
2286 ;; remove words from the ignore list or with funny characters 2648 ;; Remove words from the ignore list or with funny characters
2287 (while (setq word (pop words0)) 2649 (while (setq word (pop words0))
2650 (if downcase (setq word (downcase word)))
2288 (cond 2651 (cond
2289 ((member (downcase word) ignore-words)) 2652 ((member (downcase word) ignore-words))
2290 ((string-match reftex-label-illegal-re word) 2653 ((string-match (or illegal-re "\000") word)
2291 (when (nth 2 reftex-derive-label-parameters) 2654 (when illegal
2292 (while (string-match reftex-label-illegal-re word) 2655 (while (string-match illegal-re word)
2293 (setq word (replace-match "" nil nil word))) 2656 (setq word (replace-match "" nil nil word)))
2294 (push word words))) 2657 (push word words)))
2295 (t 2658 (t
2296 (push word words)))) 2659 (push word words))))
2297 (setq words (nreverse words)) 2660 (setq words (nreverse words))
2298 2661
2299 ;; restrict number of words 2662 ;; Restrict number of words
2300 (if (> (length words) (nth 0 reftex-derive-label-parameters)) 2663 (if (> (length words) nwords)
2301 (setcdr (nthcdr (1- (nth 0 reftex-derive-label-parameters)) words) nil)) 2664 (setcdr (nthcdr (1- nwords) words) nil))
2302 2665
2303 ;; First, try to use all words 2666 ;; First, try to use all words
2304 (setq string (mapconcat 'identity words 2667 (setq string (mapconcat 'identity words sep))
2305 (nth 4 reftex-derive-label-parameters))) 2668
2306
2307 ;; Abbreviate words if enforced by user settings or string length 2669 ;; Abbreviate words if enforced by user settings or string length
2308 (if (or (eq t (nth 3 reftex-derive-label-parameters)) 2670 (if (or (eq t abbrev)
2309 (and (nth 3 reftex-derive-label-parameters) 2671 (and abbrev
2310 (> (length string) (nth 1 reftex-derive-label-parameters)))) 2672 (> (length string) maxchar)))
2311 (setq words 2673 (setq words
2312 (mapcar 2674 (mapcar
2313 (function 2675 (function
2314 (lambda (w) (if (string-match reftex-abbrev-regexp w) 2676 (lambda (w) (if (string-match reftex-abbrev-regexp w)
2315 (match-string 1 w) 2677 (if dot
2678 (concat (match-string 1 w) ".")
2679 (match-string 1 w))
2316 w))) 2680 w)))
2317 words) 2681 words)
2318 string (mapconcat 'identity words 2682 string (mapconcat 'identity words sep)))
2319 (nth 4 reftex-derive-label-parameters))))
2320 2683
2321 ;; Shorten if still to long 2684 ;; Shorten if still to long
2322 (setq string 2685 (setq string
2323 (if (> (length string) (nth 1 reftex-derive-label-parameters)) 2686 (if (> (length string) maxchar)
2324 (substring string 0 (nth 1 reftex-derive-label-parameters)) 2687 (substring string 0 maxchar)
2325 string)) 2688 string))
2326 2689
2327 ;; Delete the final punctuation, if any 2690 ;; Delete the final punctuation, if any
2328 (if (string-match "[^a-zA-Z0-9]+\\'" string) 2691 (if (and (not keep-fp) (string-match "\\s.+\\'" string))
2329 (setq string (replace-match "" nil nil string))) 2692 (setq string (replace-match "" nil nil string)))
2330 string)) 2693 string))
2331 2694
@@ -2354,23 +2717,6 @@ This function is controlled by the settings of reftex-insert-label-flags."
2354 prefix (replace-match replace nil nil prefix))) 2717 prefix (replace-match replace nil nil prefix)))
2355 prefix))) 2718 prefix)))
2356 2719
2357(defun reftex-label-location (&optional bound)
2358 ;; Return the environment or macro which determines the label type at point.
2359 ;; If optional BOUND is an integer, limit backward searches to that point.
2360
2361 (let* ((loc1 (reftex-what-macro reftex-label-mac-list bound))
2362 (loc2 (reftex-what-environment reftex-label-env-list bound))
2363 (p1 (or (cdr loc1) 0))
2364 (p2 (or (cdr loc2) 0)))
2365
2366 (setq reftex-location-start (max p1 p2))
2367 (if (>= p1 p2)
2368 (progn
2369 (setq reftex-default-context-position (+ p1 (length (car loc1))))
2370 (or (car loc1) "section"))
2371 (setq reftex-default-context-position (+ p2 8 (length (car loc2))))
2372 (or (car loc2) "section"))))
2373
2374(defun reftex-uniquify-label (label &optional force separator) 2720(defun reftex-uniquify-label (label &optional force separator)
2375 ;; Make label unique by appending a number. 2721 ;; Make label unique by appending a number.
2376 ;; Optional FORCE means, force appending a number, even if label is unique. 2722 ;; Optional FORCE means, force appending a number, even if label is unique.
@@ -2399,6 +2745,8 @@ This function is controlled by the settings of reftex-insert-label-flags."
2399 (setcdr cell num) 2745 (setcdr cell num)
2400 (concat label sep (int-to-string num)))))) 2746 (concat label sep (int-to-string num))))))
2401 2747
2748;;; Referencing labels ------------------------------------------------------
2749
2402;; Help string for the reference label menu 2750;; Help string for the reference label menu
2403(defconst reftex-select-label-prompt 2751(defconst reftex-select-label-prompt
2404 "Select: [n]ext [p]revious [r]escan [ ]context e[x]tern [q]uit RET [?]HELP+more") 2752 "Select: [n]ext [p]revious [r]escan [ ]context e[x]tern [q]uit RET [?]HELP+more")
@@ -2441,6 +2789,8 @@ During a selection process, these are the local bindings.
2441 (make-local-hook 'post-command-hook) 2789 (make-local-hook 'post-command-hook)
2442 (setq major-mode 'reftex-select-label-mode 2790 (setq major-mode 'reftex-select-label-mode
2443 mode-name "RefTeX Select Label") 2791 mode-name "RefTeX Select Label")
2792 (when (syntax-table-p reftex-latex-syntax-table)
2793 (set-syntax-table reftex-latex-syntax-table))
2444 ;; We do not set a local map - reftex-select-item does this. 2794 ;; We do not set a local map - reftex-select-item does this.
2445 (run-hooks 'reftex-select-label-mode-hook)) 2795 (run-hooks 'reftex-select-label-mode-hook))
2446 2796
@@ -2480,6 +2830,7 @@ When called with 2 C-u prefix args, disable magic word recognition."
2480 (set-marker reftex-select-return-marker (point)) 2830 (set-marker reftex-select-return-marker (point))
2481 (setq pair (save-excursion 2831 (setq pair (save-excursion
2482 (reftex-offer-label-menu type))) 2832 (reftex-offer-label-menu type)))
2833 (reftex-ensure-compiled-variables)
2483 (set-marker reftex-select-return-marker nil) 2834 (set-marker reftex-select-return-marker nil)
2484 (setq label (car pair) 2835 (setq label (car pair)
2485 type (cdr pair) 2836 type (cdr pair)
@@ -2510,6 +2861,7 @@ When called with 2 C-u prefix args, disable magic word recognition."
2510 label)) 2861 label))
2511 2862
2512(defun reftex-guess-label-type () 2863(defun reftex-guess-label-type ()
2864 ;; Examine context to guess what a \ref might want to reference.
2513 (let ((words reftex-words-to-typekey-alist) 2865 (let ((words reftex-words-to-typekey-alist)
2514 (case-fold-search t) 2866 (case-fold-search t)
2515 (bound (max (point-min) (- (point) 35))) 2867 (bound (max (point-min) (- (point) 35)))
@@ -2545,13 +2897,13 @@ When called with 2 C-u prefix args, disable magic word recognition."
2545 2897
2546 (setq entry (cons nil nil)) 2898 (setq entry (cons nil nil))
2547 2899
2548 ;; The following unwind-protect kills temporary buffers after use
2549 (unwind-protect 2900 (unwind-protect
2550 (catch 'exit 2901 (catch 'exit
2551 (while t 2902 (while t
2552 (save-window-excursion 2903 (save-window-excursion
2553 (delete-other-windows) 2904 (delete-other-windows)
2554 (setq reftex-call-back-to-this-buffer buf) 2905 (setq reftex-call-back-to-this-buffer buf
2906 reftex-latex-syntax-table (syntax-table))
2555 (let ((default-major-mode 'reftex-select-label-mode)) 2907 (let ((default-major-mode 'reftex-select-label-mode))
2556 (if reftex-use-multiple-selection-buffers 2908 (if reftex-use-multiple-selection-buffers
2557 (switch-to-buffer-other-window 2909 (switch-to-buffer-other-window
@@ -2603,7 +2955,7 @@ When called with 2 C-u prefix args, disable magic word recognition."
2603 (eq key ?R)) 2955 (eq key ?R))
2604 ;; rescan buffer 2956 ;; rescan buffer
2605 (reftex-erase-buffer) 2957 (reftex-erase-buffer)
2606 (reftex-parse-document buf last-data key)) 2958 (reftex-reparse-document buf last-data key))
2607 ((eq key ?c) 2959 ((eq key ?c)
2608 ;; toggle context mode 2960 ;; toggle context mode
2609 (reftex-erase-buffer) 2961 (reftex-erase-buffer)
@@ -2655,11 +3007,15 @@ When called with 2 C-u prefix args, disable magic word recognition."
2655 (setq reftex-last-used-reference entry)) 3007 (setq reftex-last-used-reference entry))
2656 (setq entry nil)) 3008 (setq entry nil))
2657 (throw 'exit t)))))) 3009 (throw 'exit t))))))
3010 (save-excursion
3011 (while reftex-buffers-with-changed-invisibility
3012 (set-buffer (car (car reftex-buffers-with-changed-invisibility)))
3013 (setq buffer-invisibility-spec
3014 (cdr (pop reftex-buffers-with-changed-invisibility)))))
2658 (mapcar (function (lambda (buf) 3015 (mapcar (function (lambda (buf)
2659 (and (buffer-live-p buf) 3016 (and (buffer-live-p buf)
2660 (bury-buffer buf)))) 3017 (bury-buffer buf))))
2661 selection-buffers) 3018 selection-buffers)
2662 (reftex-kill-buffer "*RefTeX Context Copy*")
2663 (reftex-kill-temporary-buffers)) 3019 (reftex-kill-temporary-buffers))
2664 (cons (if (nth 0 entry) (concat prefix (nth 0 entry)) nil) 3020 (cons (if (nth 0 entry) (concat prefix (nth 0 entry)) nil)
2665 (nth 1 entry)))) 3021 (nth 1 entry))))
@@ -2667,51 +3023,55 @@ When called with 2 C-u prefix args, disable magic word recognition."
2667(defun reftex-select-external-document (xr-alist xr-index) 3023(defun reftex-select-external-document (xr-alist xr-index)
2668 ;; Return index of an external document. 3024 ;; Return index of an external document.
2669 (let* ((len (length xr-alist)) (highest (1- (+ ?0 len))) 3025 (let* ((len (length xr-alist)) (highest (1- (+ ?0 len)))
2670 (prompt (format "[%c-%c] Select SPC/DEL: Scroll TAB: Read prefix with completion" ?0 highest)) 3026 (prompt (format "[%c-%c] Select TAB: Read prefix with completion"
3027 ?0 highest))
2671 key prefix) 3028 key prefix)
2672 (cond 3029 (cond
2673 ((= len 1) 3030 ((= len 1)
2674 (message "No external documents available") 3031 (message "No external documents available")
2675 (ding) 0) 3032 (ding) (sit-for 1) 0)
2676 ((= len 2) 3033 ((= len 2)
2677 (- 1 xr-index)) 3034 (- 1 xr-index))
2678 (t 3035 (t
2679 (save-excursion 3036 (save-excursion
2680 (save-window-excursion 3037 (let* ((length (apply 'max (mapcar
2681 (let* ((length (apply 'max (mapcar 3038 (lambda(x) (length (car x))) xr-alist)))
2682 (lambda(x) (length (car x))) xr-alist))) 3039 (fmt (format " [%%c] %%-%ds %%s\n" length))
2683 (fmt (format " [%%c] %%-%ds %%s\n" length)) 3040 (n (1- ?0)))
2684 (n (1- ?0))) 3041 (setq key
2685 (with-output-to-temp-buffer "*RefTeX Select*" 3042 (reftex-select-with-char
2686 (princ "SELECT EXTERNAL DOCUMENT\n------------------------\n") 3043 prompt
2687 (princ 3044 (concat
2688 (mapconcat 3045 "SELECT EXTERNAL DOCUMENT\n------------------------\n"
2689 (function 3046 (mapconcat
2690 (lambda (x) 3047 (function
2691 (format fmt (incf n) (or (car x) "") 3048 (lambda (x)
2692 (abbreviate-file-name (cdr x))))) 3049 (format fmt (incf n) (or (car x) "")
2693 xr-alist "")))) 3050 (abbreviate-file-name (cdr x)))))
2694 (reftex-enlarge-to-fit "*RefTeX Select*") 3051 xr-alist ""))
2695 (select-window (get-buffer-window "*RefTeX Select*")) 3052 nil t))
2696 (setq truncate-lines t) 3053 (cond
2697 (catch 'exit 3054 ((and (>= key ?0) (<= key highest)) (- key ?0))
2698 (while t 3055 ((= key ?\C-i)
2699 (princ prompt) 3056 (setq prefix (completing-read "Prefix: " xr-alist nil t))
2700 (setq key (read-char-exclusive)) 3057 (- len (length (memq (assoc prefix xr-alist) xr-alist))))
2701 (cond 3058 (t (error "Illegal document selection [%c]" key)))))))))
2702 ((= key ?\C-?) (condition-case nil (scroll-down) (error nil))) 3059
2703 ((= key ?\ ) (condition-case nil (scroll-up) (error nil))) 3060(defun reftex-reparse-document (&optional buffer data key)
2704 ((and (>= key ?0) (<= key highest)) 3061 ;; Rescan the document.
2705 (throw 'exit (- key ?0))) 3062 (save-window-excursion
2706 ((= key ?\C-i) 3063 (save-excursion
2707 (setq prefix (completing-read "Prefix: " xr-alist nil t)) 3064 (if buffer
2708 (throw 'exit (- len (length (memq (assoc prefix xr-alist) 3065 (if (not (bufferp buffer))
2709 xr-alist))))) 3066 (error "No such buffer %s" (buffer-name buffer))
2710 (t (error "Illegal document selection [%c]" key))))))))))) 3067 (set-buffer buffer)))
3068 (let ((arg (if (eq key ?R) '(16) '(4)))
3069 (file (nth 3 data)))
3070 (reftex-access-scan-info arg file)))))
2711 3071
2712(defun reftex-make-selection-buffer-name (type &optional index) 3072(defun reftex-make-selection-buffer-name (type &optional index)
2713 ;; Make unique name for a selection buffer. 3073 ;; Make unique name for a selection buffer.
2714 (format "*RefTeX[%s][%d]*" 3074 (format " *RefTeX[%s][%d]*"
2715 type (or index (get reftex-docstruct-symbol ':master-index) 0))) 3075 type (or index (get reftex-docstruct-symbol ':master-index) 0)))
2716 3076
2717(defun reftex-get-offset (buf here-am-I typekey) 3077(defun reftex-get-offset (buf here-am-I typekey)
@@ -2725,7 +3085,7 @@ When called with 2 C-u prefix args, disable magic word recognition."
2725 (not (and (stringp (car entry)) 3085 (not (and (stringp (car entry))
2726 (equal typekey (nth 1 entry)))))) 3086 (equal typekey (nth 1 entry))))))
2727 entry))) 3087 entry)))
2728 3088
2729(defun reftex-make-and-insert-label-list 3089(defun reftex-make-and-insert-label-list
2730 (typekey0 buf toc files context counter show-commented here-I-am xr-prefix) 3090 (typekey0 buf toc files context counter show-commented here-I-am xr-prefix)
2731 ;; Insert a menu of all labels in buffer BUF into current buffer. 3091 ;; Insert a menu of all labels in buffer BUF into current buffer.
@@ -2744,6 +3104,9 @@ When called with 2 C-u prefix args, disable magic word recognition."
2744 (if (memq reftex-highlight-selection '(mouse both)) 3104 (if (memq reftex-highlight-selection '(mouse both))
2745 reftex-mouse-selected-face 3105 reftex-mouse-selected-face
2746 nil)) 3106 nil))
3107 (label-face (reftex-verified-face reftex-label-face
3108 'font-lock-constant-face
3109 'font-lock-reference-face))
2747 all cell text label typekey note comment master-dir-re 3110 all cell text label typekey note comment master-dir-re
2748 offset from to docstruct-symbol) 3111 offset from to docstruct-symbol)
2749 3112
@@ -2779,7 +3142,7 @@ When called with 2 C-u prefix args, disable magic word recognition."
2779 (cond 3142 (cond
2780 3143
2781 ((memq (car cell) '(bib thebib label-numbers appendix 3144 ((memq (car cell) '(bib thebib label-numbers appendix
2782 master-dir is-multi xr xr-doc))) 3145 master-dir bibview-cache is-multi xr xr-doc)))
2783 ;; These are currently ignored 3146 ;; These are currently ignored
2784 3147
2785 ((memq (car cell) '(bof eof file-error)) 3148 ((memq (car cell) '(bof eof file-error))
@@ -2800,19 +3163,25 @@ When called with 2 C-u prefix args, disable magic word recognition."
2800 ;; a table of contents entry 3163 ;; a table of contents entry
2801 (when toc 3164 (when toc
2802 (setq reftex-active-toc cell) 3165 (setq reftex-active-toc cell)
2803 (insert (concat toc-indent (nth 2 cell) "\n")))) 3166 (insert (concat toc-indent (nth 2 cell) "\n"))
3167 (setq to (point))
3168 (when font
3169 (put-text-property from to
3170 'face reftex-section-heading-face))
3171 (goto-char to)))
2804 3172
2805 ((stringp (car cell)) 3173 ((stringp (car cell))
2806 ;; a label 3174 ;; a label
2807 (when (null (nth 2 cell)) 3175 (when (null (nth 2 cell))
2808 ;; No context yet. Quick update. 3176 ;; No context yet. Quick update.
2809 (setcdr cell (cdr (reftex-label-info-update cell)))) 3177 (setcdr cell (cdr (reftex-label-info-update cell)))
3178 (put docstruct-symbol 'modified t))
2810 3179
2811 (setq label (car cell) 3180 (setq label (car cell)
2812 typekey (nth 1 cell) 3181 typekey (nth 1 cell)
2813 text (nth 2 cell) 3182 text (nth 2 cell)
2814 note (nth 4 cell) 3183 comment (nth 4 cell)
2815 comment (get-text-property 0 'in-comment text)) 3184 note (nth 5 cell))
2816 3185
2817 (when (and (or (string= typekey typekey0) (string= typekey0 " ")) 3186 (when (and (or (string= typekey typekey0) (string= typekey0 " "))
2818 (or show-commented (null comment))) 3187 (or show-commented (null comment)))
@@ -2825,15 +3194,17 @@ When called with 2 C-u prefix args, disable magic word recognition."
2825 (when comment (setq label (concat "% " label))) 3194 (when comment (setq label (concat "% " label)))
2826 (insert label-indent label) 3195 (insert label-indent label)
2827 (when font 3196 (when font
3197 (setq to (point))
2828 (put-text-property 3198 (put-text-property
2829 (- (point) (length label)) (point) 3199 (- (point) (length label)) to
2830 'face (if comment 3200 'face (if comment
2831 'font-lock-comment-face 3201 'font-lock-comment-face
2832 reftex-label-face))) 3202 label-face))
3203 (goto-char to))
2833 3204
2834 (insert (if counter (format " (%d) " cnt) "") 3205 (insert (if counter (format " (%d) " cnt) "")
2835 (if comment " LABEL IS COMMENTED OUT " "") 3206 (if comment " LABEL IS COMMENTED OUT " "")
2836 (if note (concat " " note) "") 3207 (if (stringp note) (concat " " note) "")
2837 "\n") 3208 "\n")
2838 (setq to (point)) 3209 (setq to (point))
2839 3210
@@ -2847,416 +3218,18 @@ When called with 2 C-u prefix args, disable magic word recognition."
2847 (goto-char to))))) 3218 (goto-char to)))))
2848 3219
2849 (when (reftex-refontify) 3220 (when (reftex-refontify)
2850 (reftex-fontify-select-label-buffer)) 3221 (reftex-fontify-select-label-buffer buf))
3222 (run-hooks 'reftex-display-copied-context-hook)
2851 offset)) 3223 offset))
2852 3224
2853(defun reftex-parse-document (&optional buffer data key)
2854 "Rescan the document."
2855 (interactive)
2856 (save-window-excursion
2857 (save-excursion
2858 (if buffer
2859 (if (not (bufferp buffer))
2860 (error "No such buffer %s" (buffer-name buffer))
2861 (set-buffer buffer)))
2862 (let ((arg (if (eq key ?R) '(16) '(4)))
2863 (file (nth 3 data)))
2864 (reftex-access-scan-info arg file)))))
2865
2866(defun reftex-query-label-type () 3225(defun reftex-query-label-type ()
2867 ;; Ask for label type 3226 ;; Ask for label type
2868 (message reftex-type-query-prompt) 3227 (let ((key (reftex-select-with-char
2869 (let ((key (read-char))) 3228 reftex-type-query-prompt reftex-type-query-help 3)))
2870 (when (eq key ?\?)
2871 (save-window-excursion
2872 (with-output-to-temp-buffer "*RefTeX Help*"
2873 (princ reftex-type-query-help))
2874 (reftex-enlarge-to-fit "*RefTeX Help*")
2875 (setq key (read-char))
2876 (kill-buffer "*RefTeX Help*")))
2877 (unless (member (char-to-string key) reftex-typekey-list) 3229 (unless (member (char-to-string key) reftex-typekey-list)
2878 (error "No such label type: %s" (char-to-string key))) 3230 (error "No such label type: %s" (char-to-string key)))
2879 (char-to-string key))) 3231 (char-to-string key)))
2880 3232
2881;; Variable holding the vector with section numbers
2882(defvar reftex-section-numbers [0 0 0 0 0 0 0 0])
2883
2884(defun reftex-section-info (file)
2885 ;; Return a section entry for the current match.
2886 ;; Carefull: This function expects the match-data to be still in place!
2887 (let* ((marker (set-marker (make-marker) (1- (match-beginning 3))))
2888 (macro (reftex-match-string 3))
2889 (star (= ?* (char-after (match-end 3))))
2890 (level (cdr (assoc macro reftex-section-levels)))
2891 (section-number (reftex-section-number level star))
2892 (text1 (save-match-data (save-excursion (reftex-context-substring))))
2893 (literal (buffer-substring-no-properties
2894 (1- (match-beginning 3))
2895 (min (point-max) (+ (match-end 0) (length text1) 1))))
2896 ;; Literal can be too short since text1 too short. No big problem.
2897 (text (reftex-nicify-text text1)))
2898
2899 ;; Add section number and indentation
2900 (setq text
2901 (concat
2902 (make-string (* reftex-level-indent level) ?\ )
2903 (if (nth 1 reftex-label-menu-flags) ; section number flag
2904 (concat section-number " "))
2905 text))
2906 ;; Fontify
2907 (if (reftex-use-fonts)
2908 (put-text-property 0 (length text)
2909 'face reftex-section-heading-face text))
2910 (list 'toc "toc" text file marker level section-number
2911 literal (marker-position marker))))
2912
2913(defun reftex-label-info-update (cell)
2914 ;; Update information about just one label in a different file.
2915 ;; CELL contains the old info list
2916 (let* ((label (nth 0 cell))
2917 (typekey (nth 1 cell))
2918 ;; (text (nth 2 cell))
2919 (file (nth 3 cell))
2920 (note (nth 4 cell))
2921 (buf (reftex-get-file-buffer-force
2922 file (not (eq t reftex-keep-temporary-buffers)))))
2923 (if (not buf)
2924 (list label typekey "" file "LOST LABEL. RESCAN TO FIX.")
2925 (save-excursion
2926 (set-buffer buf)
2927 (save-restriction
2928 (widen)
2929 (goto-char 1)
2930
2931 (if (or (re-search-forward
2932 (format reftex-find-label-regexp-format
2933 (regexp-quote label)) nil t)
2934 (re-search-forward
2935 (format reftex-find-label-regexp-format2
2936 (regexp-quote label)) nil t))
2937
2938 (progn
2939 (backward-char 1)
2940 (append (reftex-label-info label file) (list note)))
2941 (list label typekey "" file "LOST LABEL. RESCAN TO FIX.")))))))
2942
2943(defun reftex-label-info (label &optional file bound derive env-or-mac)
2944 ;; Return info list on LABEL at point.
2945 (let* ((env-or-mac (or env-or-mac (reftex-label-location bound)))
2946 (typekey (nth 1 (assoc env-or-mac reftex-env-or-mac-alist)))
2947 (file (or file (buffer-file-name)))
2948 (parse (if (reftex-typekey-check
2949 typekey reftex-use-text-after-label-as-context)
2950 nil
2951 (nth 2 (assoc env-or-mac reftex-env-or-mac-alist))))
2952 (text (reftex-short-context env-or-mac parse reftex-location-start
2953 derive)))
2954 (if (reftex-in-comment)
2955 (put-text-property 0 1 'in-comment t text))
2956 (list label typekey text file)))
2957
2958(defun reftex-in-comment ()
2959 (save-excursion
2960 (skip-chars-backward "^%\n\r")
2961 (eq (preceding-char) ?%)))
2962
2963(defun reftex-short-context (env parse &optional bound derive)
2964 ;; Get about one line of useful context for the label definition at point.
2965
2966 (if (consp parse)
2967 (setq parse (if derive (cdr parse) (car parse))))
2968
2969 (reftex-nicify-text
2970
2971 (cond
2972
2973 ((null parse)
2974 (save-excursion
2975 (reftex-context-substring)))
2976
2977 ((eq parse t)
2978 (if (string= env "section")
2979 ;; special treatment for section labels
2980 (save-excursion
2981 (if (and (re-search-backward reftex-section-or-include-regexp
2982 (point-min) t)
2983 (match-end 2))
2984 (progn
2985 (goto-char (match-end 0))
2986 (reftex-context-substring))
2987 (if reftex-active-toc
2988 (progn
2989 (string-match "{\\([^}]*\\)" (nth 7 reftex-active-toc))
2990 (match-string 1 (nth 7 reftex-active-toc)))
2991 "SECTION HEADING NOT FOUND")))
2992 (save-excursion
2993 (goto-char reftex-default-context-position)
2994 (unless (eq (string-to-char env) ?\\)
2995 (reftex-move-over-touching-args))
2996 (reftex-context-substring))))
2997
2998 ((stringp parse)
2999 (save-excursion
3000 (if (re-search-backward parse bound t)
3001 (progn
3002 (goto-char (match-end 0))
3003 (reftex-context-substring))
3004 "NO MATCH FOR CONTEXT REGEXP")))
3005
3006 ((integerp parse)
3007 (or (save-excursion
3008 (goto-char reftex-default-context-position)
3009 (reftex-nth-arg
3010 parse
3011 (nth 6 (assoc env reftex-env-or-mac-alist))))
3012 ""))
3013
3014 ((fboundp parse)
3015 ;; A hook function. Call it.
3016 (save-excursion
3017 (condition-case error-var
3018 (funcall parse env)
3019 (error (format "HOOK ERROR: %s" (cdr error-var))))))
3020 (t
3021 "ILLEGAL VALUE OF PARSE"))))
3022
3023(defun reftex-where-am-I ()
3024 ;; Return the docstruct entry above point. Actually returns a cons
3025 ;; cell in which the cdr is a flag indicating if the information is
3026 ;; exact (t) or approximate (nil).
3027
3028 (let ((docstruct (symbol-value reftex-docstruct-symbol))
3029 (cnt 0) rtn
3030 found)
3031 (save-excursion
3032 (while (not rtn)
3033 (incf cnt)
3034 (setq found (re-search-backward reftex-everything-regexp nil t))
3035 (setq rtn
3036 (cond
3037 ((not found)
3038 ;; no match
3039 (or
3040 (car (member (list 'bof (buffer-file-name)) docstruct))
3041 (not (setq cnt 2))
3042 (assq 'bof docstruct) ;; for safety reasons
3043 'corrupted))
3044 ((match-end 1)
3045 ;; Label
3046 (assoc (reftex-match-string 1)
3047 (symbol-value reftex-docstruct-symbol)))
3048 ((match-end 3)
3049 ;; Section
3050 (goto-char (1- (match-beginning 3)))
3051 (let* ((list (member (list 'bof (buffer-file-name))
3052 docstruct))
3053 (endelt (car (member (list 'eof (buffer-file-name))
3054 list)))
3055 rtn1)
3056 (while (and list (not (eq endelt (car list))))
3057 (if (and (eq (car (car list)) 'toc)
3058 (string= (buffer-file-name)
3059 (nth 3 (car list))))
3060 (cond
3061 ((equal (point)
3062 (or (and (markerp (nth 4 (car list)))
3063 (marker-position (nth 4 (car list))))
3064 (nth 8 (car list))))
3065 ;; Fits with marker position or recorded position
3066 (setq rtn1 (car list) list nil))
3067 ((looking-at (reftex-make-regexp-allow-for-ctrl-m
3068 (nth 7 (car list))))
3069 ;; Same title
3070 (setq rtn1 (car list) list nil cnt 2))))
3071 (pop list))
3072 rtn1))
3073 ((match-end 7)
3074 ;; Input or include...
3075 (car
3076 (member (list 'eof (reftex-find-tex-file
3077 (reftex-match-string 7)
3078 (cdr (assq 'master-dir docstruct))))
3079 docstruct)))
3080 ((match-end 9)
3081 (assq 'appendix (symbol-value reftex-docstruct-symbol)))
3082 ((match-end 10)
3083 (save-excursion
3084 (goto-char (match-end 10))
3085 (assoc (reftex-no-props
3086 (reftex-nth-arg-wrapper
3087 (reftex-match-string 10)))
3088 (symbol-value reftex-docstruct-symbol))))
3089 (t
3090 (error "This should not happen (reftex-where-am-I)"))))))
3091 (cons rtn (eq cnt 1))))
3092
3093(defun reftex-parse-args (macro)
3094 ;; Return a list of macro name, nargs, arg-nr which is label and a list of
3095 ;; optional argument indices.
3096 (if (string-match "[[{]\\*?[]}]" macro)
3097 (progn
3098 (let ((must-match (substring macro 0 (match-beginning 0)))
3099 (args (substring macro (match-beginning 0)))
3100 opt-list nlabel (cnt 0))
3101 (while (string-match "\\`[[{]\\(\\*\\)?[]}]" args)
3102 (incf cnt)
3103 (when (eq ?\[ (string-to-char args))
3104 (push cnt opt-list))
3105 (when (and (match-end 1)
3106 (not nlabel))
3107 (setq nlabel cnt))
3108 (setq args (substring args (match-end 0))))
3109 (list must-match cnt nlabel opt-list)))
3110 nil))
3111
3112(defsubst reftex-move-to-next-arg (&optional ignore)
3113 ;; Assuming that we are at the end of a macro name or a macro argument,
3114 ;; move forward to the opening parenthesis of the next argument.
3115 ;; This function understands the splitting of macros over several lines
3116 ;; in TeX.
3117 (cond
3118 ;; Just to be quick:
3119 ((memq (following-char) '(?\[ ?\{)))
3120 ;; Do a search
3121 ((looking-at "[ \t]*[\n\r]?\\([ \t]*%[^\n\r]*[\n\r]\\)*[ \t]*[[{]")
3122 (goto-char (1- (match-end 0)))
3123 t)
3124 (t nil)))
3125
3126(defsubst reftex-move-to-previous-arg (&optional bound)
3127 ;; Assuming that we are in front of a macro argument,
3128 ;; move backward to the closing parenthesis of the previous argument.
3129 ;; This function understands the splitting of macros over several lines
3130 ;; in TeX.
3131 (cond
3132 ;; Just to be quick:
3133 ((memq (preceding-char) '(?\] ?\})))
3134 ;; Do a search
3135 ((re-search-backward
3136 "[]}][ \t]*[\n\r]?\\([ \t]*%[^\n\r]*[\n\r]\\)*[ \t]*\\=" bound t)
3137 (goto-char (1+ (match-beginning 0)))
3138 t)
3139 (t nil)))
3140
3141(defun reftex-nth-arg-wrapper (key)
3142 (let ((entry (assoc key reftex-env-or-mac-alist)))
3143 (reftex-nth-arg (nth 5 entry) (nth 6 entry))))
3144
3145(defun reftex-nth-arg (n &optional opt-args)
3146 ;; Return the nth following {} or [] parentheses content.
3147 ;; OPT-ARGS is a list of argument numbers which are optional.
3148
3149 ;; If we are sitting at a macro start, skip to end of macro name.
3150 (and (eq (following-char) ?\\) (skip-chars-forward "a-zA-Z*\\\\"))
3151
3152 (if (= n 1000)
3153 ;; Special case: Skip all touching arguments
3154 (progn
3155 (reftex-move-over-touching-args)
3156 (reftex-context-substring))
3157
3158 ;; Do the real thing.
3159 (let ((cnt 1))
3160
3161 (when (reftex-move-to-next-arg)
3162
3163 (while (< cnt n)
3164 (while (and (member cnt opt-args)
3165 (eq (following-char) ?\{))
3166 (incf cnt))
3167 (when (< cnt n)
3168 (unless (and (condition-case nil
3169 (or (forward-list 1) t)
3170 (error nil))
3171 (reftex-move-to-next-arg)
3172 (incf cnt))
3173 (setq cnt 1000))))
3174
3175 (while (and (memq cnt opt-args)
3176 (eq (following-char) ?\{))
3177 (incf cnt)))
3178 (if (and (= n cnt)
3179 (> (skip-chars-forward "{\\[") 0))
3180 (reftex-context-substring)
3181 nil))))
3182
3183(defun reftex-move-over-touching-args ()
3184 (condition-case nil
3185 (while (memq (following-char) '(?\[ ?\{))
3186 (forward-list 1))
3187 (error nil)))
3188
3189(defun reftex-context-substring ()
3190 ;; Return up to 100 chars from point
3191 ;; When point is just after a { or [, limit string to matching parenthesis
3192 (cond
3193 ((or (= (preceding-char) ?\{)
3194 (= (preceding-char) ?\[))
3195 ;; Inside a list - get only the list.
3196 (buffer-substring-no-properties
3197 (point)
3198 (min (reftex-fp 150)
3199 (point-max)
3200 (condition-case nil
3201 (progn
3202 (up-list 1)
3203 (1- (point)))
3204 (error (point-max))))))
3205 (t
3206 ;; no list - just grab 100 characters
3207 (buffer-substring-no-properties (point) (min (reftex-fp 150) (point-max))))))
3208
3209(defun reftex-init-section-numbers (&optional toc-entry appendix)
3210 ;; Initialize the section numbers with zeros or with what is found
3211 ;; in the toc entry.
3212 (let* ((level (or (nth 5 toc-entry) -1))
3213 (numbers (nreverse (split-string (or (nth 6 toc-entry) "") "\\.")))
3214 (depth (1- (length reftex-section-numbers)))
3215 (i depth) number-string)
3216 (while (>= i 0)
3217 (if (> i level)
3218 (aset reftex-section-numbers i 0)
3219 (setq number-string (or (car numbers) "0"))
3220 (if (string-match "\\`[A-Z]\\'" number-string)
3221 (aset reftex-section-numbers i
3222 (- (string-to-char number-string) ?A -1))
3223 (aset reftex-section-numbers i (string-to-int number-string)))
3224 (pop numbers))
3225 (decf i)))
3226 (put 'reftex-section-numbers 'appendix appendix))
3227
3228(defun reftex-section-number (&optional level star)
3229 ;; Return a string with the current section number.
3230 ;; When LEVEL is non-nil, increase section numbers on that level.
3231 (let* ((depth (1- (length reftex-section-numbers))) idx n (string "")
3232 (appendix (get 'reftex-section-numbers 'appendix)))
3233 (when level
3234 (when (and (> level -1) (not star))
3235 (aset reftex-section-numbers
3236 level (1+ (aref reftex-section-numbers level))))
3237 (setq idx (1+ level))
3238 (while (<= idx depth)
3239 (aset reftex-section-numbers idx 0)
3240 (incf idx)))
3241 (setq idx 0)
3242 (while (<= idx depth)
3243 (setq n (aref reftex-section-numbers idx))
3244 (setq string (concat string (if (not (string= string "")) "." "")
3245 (int-to-string n)))
3246 (incf idx))
3247 (save-match-data
3248 (if (string-match "\\`\\([@0]\\.\\)+" string)
3249 (setq string (replace-match "" nil nil string)))
3250 (if (string-match "\\(\\.0\\)+\\'" string)
3251 (setq string (replace-match "" nil nil string)))
3252 (if (and appendix
3253 (string-match "\\`[0-9]+" string))
3254 (setq string (concat (char-to-string (1- (+ ?A (string-to-int (match-string 0 string))))) (substring string (match-end 0))))))
3255
3256 (if star
3257 (concat (make-string (1- (length string)) ?\ ) "*")
3258 string)))
3259
3260(defun reftex-select-label-callback (data forward no-revisit) 3233(defun reftex-select-label-callback (data forward no-revisit)
3261 ;; Callback function called from the label selection in order to 3234 ;; Callback function called from the label selection in order to
3262 ;; show context in another window 3235 ;; show context in another window
@@ -3270,7 +3243,7 @@ When called with 2 C-u prefix args, disable magic word recognition."
3270 ;; goto the file in another window 3243 ;; goto the file in another window
3271 (setq buffer 3244 (setq buffer
3272 (if no-revisit 3245 (if no-revisit
3273 (get-file-buffer file) 3246 (reftex-get-buffer-visiting file)
3274 (reftex-get-file-buffer-force 3247 (reftex-get-file-buffer-force
3275 file (not reftex-keep-temporary-buffers)))) 3248 file (not reftex-keep-temporary-buffers))))
3276 (if buffer 3249 (if buffer
@@ -3279,6 +3252,7 @@ When called with 2 C-u prefix args, disable magic word recognition."
3279 ;; we have got a problem here. The file does not exist. 3252 ;; we have got a problem here. The file does not exist.
3280 ;; Let' get out of here.. 3253 ;; Let' get out of here..
3281 ;; (ding) 3254 ;; (ding)
3255 (message reftex-no-follow-message)
3282 (throw 'exit nil)) 3256 (throw 'exit nil))
3283 3257
3284 ;; search for that label 3258 ;; search for that label
@@ -3294,9 +3268,9 @@ When called with 2 C-u prefix args, disable magic word recognition."
3294 (re-search-forward (format reftex-find-label-regexp-format2 3268 (re-search-forward (format reftex-find-label-regexp-format2
3295 (regexp-quote label)) nil t))) 3269 (regexp-quote label)) nil t)))
3296 (when (match-end 3) 3270 (when (match-end 3)
3271 (setq reftex-latex-syntax-table (syntax-table))
3297 (reftex-highlight 0 (match-beginning 3) (match-end 3)) 3272 (reftex-highlight 0 (match-beginning 3) (match-end 3))
3298 (reftex-show-entry (- (point) (match-beginning 3)) 3273 (reftex-show-entry (match-beginning 3) (match-end 3))
3299 (- (point) (match-end 3)))
3300 (recenter (/ (window-height) 2))) 3274 (recenter (/ (window-height) 2)))
3301 (select-window this-window)))) 3275 (select-window this-window))))
3302 3276
@@ -3327,166 +3301,46 @@ When called with 2 C-u prefix args, disable magic word recognition."
3327 (throw 'exit (selected-window))))) 3301 (throw 'exit (selected-window)))))
3328 (error "Label %s not found" label)))) 3302 (error "Label %s not found" label))))
3329 3303
3330(defun reftex-find-duplicate-labels () 3304(defvar font-lock-mode)
3331 "Produce a list of all duplicate labels in the document."
3332
3333 (interactive)
3334
3335 ;; Rescan the document to make sure
3336 (reftex-access-scan-info t)
3337
3338 (let ((master (reftex-TeX-master-file))
3339 (cnt 0)
3340 (dlist
3341 (mapcar
3342 (function
3343 (lambda (x)
3344 (let (x1)
3345 (cond
3346 ((memq (car x)
3347 '(toc bof eof bib thebib label-numbers xr xr-doc
3348 master-dir file-error appendix is-multi))
3349 nil)
3350 (t
3351 (setq x1 (reftex-all-assoc-string
3352 (car x) (symbol-value reftex-docstruct-symbol)))
3353 (if (< 1 (length x1))
3354 (append (list (car x))
3355 (mapcar (function
3356 (lambda(x)
3357 (abbreviate-file-name (nth 3 x))))
3358 x1))
3359 (list nil)))))))
3360 (reftex-uniquify (symbol-value reftex-docstruct-symbol)))))
3361
3362 (setq dlist (reftex-uniquify dlist))
3363 (if (null dlist) (error "No duplicate labels in document"))
3364 (switch-to-buffer-other-window "*Duplicate Labels*")
3365 (make-local-variable 'TeX-master)
3366 (setq TeX-master master)
3367 (erase-buffer)
3368 (insert " MULTIPLE LABELS IN CURRENT DOCUMENT:\n")
3369 (insert
3370 " Move point to label and type `r' to run a query-replace on the label\n"
3371 " and its references. Type `q' to exit this buffer.\n\n")
3372 (insert " LABEL FILE\n")
3373 (insert " -------------------------------------------------------------\n")
3374 (use-local-map (make-sparse-keymap))
3375 (local-set-key [?q] (function
3376 (lambda () (interactive)
3377 (kill-buffer (current-buffer)) (delete-window))))
3378 (local-set-key [?r] 'reftex-change-label)
3379 (while dlist
3380 (when (and (car (car dlist))
3381 (cdr (car dlist)))
3382 (incf cnt)
3383 (insert (mapconcat 'identity (car dlist) "\n ") "\n"))
3384 (pop dlist))
3385 (goto-char (point-min))
3386 (when (= cnt 0)
3387 (kill-buffer (current-buffer))
3388 (delete-window)
3389 (message "Document does not contain duplicate labels."))))
3390
3391(defun reftex-all-assq (key list)
3392 ;; Return a list of all associations of KEY in LIST. Comparison with string=
3393 (let (rtn)
3394 (while (setq list (memq (assq key list) list))
3395 (push (car list) rtn)
3396 (pop list))
3397 (nreverse rtn)))
3398
3399(defun reftex-all-assoc-string (key list)
3400 ;; Return a list of all associations of KEY in LIST. Comparison with string=
3401 (let (rtn)
3402 (while list
3403 (if (string= (car (car list)) key)
3404 (push (car list) rtn))
3405 (pop list))
3406 (nreverse rtn)))
3407
3408(defun reftex-kill-temporary-buffers (&optional buffer)
3409 ;; Kill all buffers in the list reftex-kill-temporary-buffers.
3410 (cond
3411 (buffer
3412 (when (member buffer reftex-buffers-to-kill)
3413 (kill-buffer buffer)
3414 (setq reftex-buffers-to-kill
3415 (delete buffer reftex-buffers-to-kill))))
3416 (t
3417 (while (setq buffer (pop reftex-buffers-to-kill))
3418 (when (bufferp buffer)
3419 (and (buffer-modified-p buffer)
3420 (y-or-n-p (format "Save file %s? "
3421 (buffer-file-name buffer)))
3422 (save-excursion
3423 (set-buffer buffer)
3424 (save-buffer)))
3425 (kill-buffer buffer))
3426 (pop reftex-buffers-to-kill)))))
3427
3428(defun reftex-show-entry (beg-hlt end-hlt) 3305(defun reftex-show-entry (beg-hlt end-hlt)
3429 ;; Show entry if point is hidden by outline mode 3306 ;; Show entry if point is hidden
3430 (let* ((pos (point)) 3307 (let* ((n (/ (window-height) 2))
3431 (n (/ (window-height) 2))
3432 (beg (save-excursion 3308 (beg (save-excursion
3433 (re-search-backward "[\n\r]" nil 1 n) (point))) 3309 (re-search-backward "[\n\r]" nil 1 n) (point)))
3434 (end (save-excursion 3310 (end (save-excursion
3435 (re-search-forward "[\n\r]" nil 1 n) (point)))) 3311 (re-search-forward "[\n\r]" nil 1 n) (point))))
3436 (if (and reftex-auto-show-entry 3312 (cond
3437 (string-match 3313 ((and (boundp 'buffer-invisibility-spec) buffer-invisibility-spec
3438 "\r" (buffer-substring beg end))) 3314 (get-char-property (1+ beg-hlt) 'invisible))
3439 (cond 3315 ;; Invisible with text properties. That is easy to change.
3440 ((eq t reftex-auto-show-entry) 3316 (push (cons (current-buffer) buffer-invisibility-spec)
3441 (subst-char-in-region 3317 reftex-buffers-with-changed-invisibility)
3442 (save-excursion (search-backward "\n" nil t) (point)) 3318 (setq buffer-invisibility-spec nil))
3443 (save-excursion (search-forward "\n" nil t) (point)) 3319 ((string-match "\r" (buffer-substring beg end))
3444 ?\r ?\n t)) 3320 ;; Invisible with selective display. We need to copy it.
3445 ((eq reftex-auto-show-entry 'copy) 3321 (let ((string (buffer-substring-no-properties beg end)))
3446 (let ((string (buffer-substring beg end))) 3322 (switch-to-buffer "*RefTeX Context Copy*")
3447 (switch-to-buffer "*RefTeX Context Copy*") 3323 (setq buffer-read-only nil)
3448 (setq buffer-read-only nil) 3324 (erase-buffer)
3449 (erase-buffer) 3325 (insert string)
3450 (insert string) 3326 (subst-char-in-region (point-min) (point-max) ?\r ?\n t)
3451 (subst-char-in-region (point-min) (point-max) ?\r ?\n t) 3327 (goto-char (- beg-hlt beg))
3452 (goto-char (- pos beg)) 3328 (reftex-highlight 0 (1+ (- beg-hlt beg)) (1+ (- end-hlt beg)))
3453 (reftex-highlight 0 (1+ (- (point) beg-hlt)) 3329 (if (reftex-refontify)
3454 (1+ (- (point) end-hlt))) 3330 (when (or (not (eq major-mode 'latex-mode))
3455 (when (reftex-refontify) 3331 (not font-lock-mode))
3456 (set (make-local-variable 'font-lock-defaults) nil) 3332 (latex-mode)
3457 (let ((major-mode 'latex-mode)) 3333 (run-hook-with-args
3458 (font-lock-mode 1))) 3334 'reftex-pre-refontification-functions
3459 (setq buffer-read-only t))) 3335 reftex-call-back-to-this-buffer 'reftex-hidden)
3460 )))) 3336 (turn-on-font-lock))
3461 3337 (when (or (not (eq major-mode 'fundamental-mode))
3462(defun reftex-nicify-text (text) 3338 font-lock-mode)
3463 ;; Make TEXT nice for inclusion as context into label menu 3339 (fundamental-mode)))
3464 ;; remove line breaks and extra white space 3340 (run-hooks 'reftex-display-copied-context-hook)
3465 (while (string-match "[\n\r\t]\\|[ \t][ \t]+" text) 3341 (setq buffer-read-only t))))))
3466 (setq text (replace-match " " nil t text))) 3342
3467 ;; cut before the next `\end{' or `\item' or `\\' 3343;;; =========================================================================
3468 (if (string-match "\\(\\\\end{\\|\\\\item\\|\\\\\\\\\\).*" text)
3469 (setq text (replace-match "" nil t text)))
3470 ;; kill the embedded label
3471 (if (string-match "\\\\label{[^}]*}" text)
3472 (setq text (replace-match "" nil t text)))
3473 ;; remove leading garbage
3474 (if (string-match "\\`[ }]+" text)
3475 (setq text (replace-match "" nil t text)))
3476 ;; limit length
3477 (cond
3478 ((> (length text) 100) (substring text 0 100))
3479 ((= (length text) 0) (make-string 1 ?\ ))
3480 (t text)))
3481
3482(defun reftex-typekey-check (typekey conf-variable &optional n)
3483 ;; Check if CONF-VARIABLE is true or contains TYPEKEY
3484 (and n (setq conf-variable (nth n conf-variable)))
3485 (or (eq conf-variable t)
3486 (and (stringp conf-variable)
3487 (string-match (concat "[" conf-variable "]") typekey))))
3488
3489;;; ===========================================================================
3490;;; 3344;;;
3491;;; Table of contents 3345;;; Table of contents
3492 3346
@@ -3538,6 +3392,7 @@ RET Goto the section and hide the *toc* buffer (also on mouse-2).
3538q / Q Hide/Kill *toc* buffer, return to position of last reftex-toc command. 3392q / Q Hide/Kill *toc* buffer, return to position of last reftex-toc command.
3539f / g Toggle follow mode on and off / Refresh *toc* buffer. 3393f / g Toggle follow mode on and off / Refresh *toc* buffer.
3540r / R Reparse the LaTeX document / Reparse entire LaTeX document. 3394r / R Reparse the LaTeX document / Reparse entire LaTeX document.
3395. In other window, show position from where `reftex-toc' was called.
3541x Switch to TOC of external document (with LaTeX package `xr').") 3396x Switch to TOC of external document (with LaTeX package `xr').")
3542 3397
3543(defun reftex-toc () 3398(defun reftex-toc ()
@@ -3570,6 +3425,7 @@ When called with a raw C-u prefix, rescan the document first."
3570 (if (memq reftex-highlight-selection '(mouse both)) 3425 (if (memq reftex-highlight-selection '(mouse both))
3571 reftex-mouse-selected-face 3426 reftex-mouse-selected-face
3572 nil)) 3427 nil))
3428 (fontify (reftex-use-fonts))
3573 toc1 cell startpos) 3429 toc1 cell startpos)
3574 3430
3575 (if (get-buffer-window "*toc*") 3431 (if (get-buffer-window "*toc*")
@@ -3607,6 +3463,9 @@ SPC=view TAB=goto RET=goto+hide [q]uit [r]escan [f]ollow-mode e[x]tern [?]Help
3607 (when (eq (car cell) 'toc) 3463 (when (eq (car cell) 'toc)
3608 (setq toc1 (concat (nth 2 cell) "\n")) 3464 (setq toc1 (concat (nth 2 cell) "\n"))
3609 (put-text-property 0 (length toc1) 'toc cell toc1) 3465 (put-text-property 0 (length toc1) 'toc cell toc1)
3466 (when fontify
3467 (put-text-property 0 (length toc1)
3468 'face reftex-section-heading-face toc1))
3610 (when mouse-face 3469 (when mouse-face
3611 (put-text-property 0 (1- (length toc1)) 3470 (put-text-property 0 (1- (length toc1))
3612 'mouse-face mouse-face toc1)) 3471 'mouse-face mouse-face toc1))
@@ -3614,6 +3473,7 @@ SPC=view TAB=goto RET=goto+hide [q]uit [r]escan [f]ollow-mode e[x]tern [?]Help
3614 3473
3615 (backward-delete-char 1) 3474 (backward-delete-char 1)
3616 3475
3476 (run-hooks 'reftex-display-copied-context-hook)
3617 (message "Building *toc* buffer...done.") 3477 (message "Building *toc* buffer...done.")
3618 (setq buffer-read-only t)) 3478 (setq buffer-read-only t))
3619 (t 3479 (t
@@ -3661,11 +3521,13 @@ SPC=view TAB=goto RET=goto+hide [q]uit [r]escan [f]ollow-mode e[x]tern [?]Help
3661 (error t))))) 3521 (error t)))))
3662 3522
3663(defun reftex-re-enlarge () 3523(defun reftex-re-enlarge ()
3524 ;; Enlarge windiw to a remembered size
3664 (enlarge-window 3525 (enlarge-window
3665 (max 0 (- (or reftex-last-window-height (window-height)) 3526 (max 0 (- (or reftex-last-window-height (window-height))
3666 (window-height))))) 3527 (window-height)))))
3667 3528
3668(defun reftex-toc-show-help () 3529(defun reftex-toc-show-help ()
3530 "Show a summary of special key bindings."
3669 (interactive) 3531 (interactive)
3670 (with-output-to-temp-buffer "*RefTeX Help*" 3532 (with-output-to-temp-buffer "*RefTeX Help*"
3671 (princ reftex-toc-help)) 3533 (princ reftex-toc-help))
@@ -3674,9 +3536,9 @@ SPC=view TAB=goto RET=goto+hide [q]uit [r]escan [f]ollow-mode e[x]tern [?]Help
3674 (setq reftex-toc-follow-mode 1))) 3536 (setq reftex-toc-follow-mode 1)))
3675 3537
3676(defun reftex-toc-toggle-follow () 3538(defun reftex-toc-toggle-follow ()
3677 "Toggle toc-follow mode. 3539 "Toggle toc-follow mode. (It is not really a mode, just a flag)."
3678(It is not really a mode, just a flag)."
3679 (interactive) 3540 (interactive)
3541 (setq reftex-last-follow-point -1)
3680 (setq reftex-toc-follow-mode (not reftex-toc-follow-mode))) 3542 (setq reftex-toc-follow-mode (not reftex-toc-follow-mode)))
3681(defun reftex-toc-view-line () 3543(defun reftex-toc-view-line ()
3682 "View document location in other window." 3544 "View document location in other window."
@@ -3700,6 +3562,16 @@ SPC=view TAB=goto RET=goto+hide [q]uit [r]escan [f]ollow-mode e[x]tern [?]Help
3700 (interactive "e") 3562 (interactive "e")
3701 (mouse-set-point ev) 3563 (mouse-set-point ev)
3702 (reftex-toc-visit-line 'hide)) 3564 (reftex-toc-visit-line 'hide))
3565(defun reftex-toc-show-insertion-point ()
3566 (interactive)
3567 (let ((this-window (selected-window)))
3568 (unwind-protect
3569 (progn
3570 (switch-to-buffer-other-window
3571 (marker-buffer reftex-toc-return-marker))
3572 (goto-char (marker-position reftex-toc-return-marker))
3573 (recenter (/ (window-height) 2)))
3574 (select-window this-window))))
3703(defun reftex-toc-quit () 3575(defun reftex-toc-quit ()
3704 "Hide the *toc* window and do not move point." 3576 "Hide the *toc* window and do not move point."
3705 (interactive) 3577 (interactive)
@@ -3789,10 +3661,11 @@ SPC=view TAB=goto RET=goto+hide [q]uit [r]escan [f]ollow-mode e[x]tern [?]Help
3789 (looking-at (reftex-make-desperate-section-regexp literal)) 3661 (looking-at (reftex-make-desperate-section-regexp literal))
3790 (looking-at (concat "\\\\" 3662 (looking-at (concat "\\\\"
3791 (regexp-quote 3663 (regexp-quote
3792 (car (rassq level reftex-section-levels))) 3664 (car (rassq level
3665 reftex-section-levels-all)))
3793 "[[{]")))) 3666 "[[{]"))))
3794 ((or (not no-revisit) 3667 ((or (not no-revisit)
3795 (get-file-buffer file)) 3668 (reftex-get-buffer-visiting file))
3796 ;; Marker is lost. Use the backup method. 3669 ;; Marker is lost. Use the backup method.
3797 (switch-to-buffer-other-window 3670 (switch-to-buffer-other-window
3798 (reftex-get-file-buffer-force file nil)) 3671 (reftex-get-file-buffer-force file nil))
@@ -3805,6 +3678,7 @@ SPC=view TAB=goto RET=goto+hide [q]uit [r]escan [f]ollow-mode e[x]tern [?]Help
3805 (reftex-make-regexp-allow-for-ctrl-m literal) pos) 3678 (reftex-make-regexp-allow-for-ctrl-m literal) pos)
3806 (reftex-nearest-match 3679 (reftex-nearest-match
3807 (reftex-make-desperate-section-regexp literal) pos))))) 3680 (reftex-make-desperate-section-regexp literal) pos)))))
3681 (t (message reftex-no-follow-message) nil)
3808 )) 3682 ))
3809 3683
3810 (setq show-window (selected-window) 3684 (setq show-window (selected-window)
@@ -3832,15 +3706,27 @@ SPC=view TAB=goto RET=goto+hide [q]uit [r]escan [f]ollow-mode e[x]tern [?]Help
3832 (reftex-re-enlarge)) 3706 (reftex-re-enlarge))
3833 (t nil)))) 3707 (t nil))))
3834 3708
3835;;; =========================================================================== 3709(defun reftex-make-desperate-section-regexp (old)
3710 ;; Return a regexp which will still match a section statement even if
3711 ;; x-symbol or isotex or the like have been at work in the mean time.
3712 (let* ((n (1+ (string-match "[[{]" old)))
3713 (new (regexp-quote (substring old 0 (1+ (string-match "[[{]" old)))))
3714 (old (substring old n)))
3715 (while (string-match
3716 "\\([\r\n]\\)\\|\\(\\`\\|[ \t\n\r]\\)\\([a-zA-Z0-9]+\\)\\([ \t\n\r]\\|}\\'\\)"
3717 old)
3718 (if (match-beginning 1)
3719 (setq new (concat new "[^\n\r]*[\n\r]"))
3720 (setq new (concat new "[^\n\r]*" (match-string 3 old))))
3721 (setq old (substring old (match-end 0))))
3722 new))
3723
3724;;; =========================================================================
3836;;; 3725;;;
3837;;; BibTeX citations. 3726;;; BibTeX citations.
3838 3727
3839;; Variables and constants 3728;; Variables and constants
3840 3729
3841;; Define variable to silence compiler warnings
3842(defvar reftex-cite-format-builtin)
3843
3844;; The history list of regular expressions used for citations 3730;; The history list of regular expressions used for citations
3845(defvar reftex-cite-regexp-hist nil) 3731(defvar reftex-cite-regexp-hist nil)
3846 3732
@@ -3858,7 +3744,7 @@ SPC=view TAB=goto RET=goto+hide [q]uit [r]escan [f]ollow-mode e[x]tern [?]Help
3858 q Quit without inserting \\cite macro into buffer. 3744 q Quit without inserting \\cite macro into buffer.
3859 TAB Enter citation key with completion. 3745 TAB Enter citation key with completion.
3860 RET Accept current entry (also on mouse-2) 3746 RET Accept current entry (also on mouse-2)
3861 a Accept all entries.") 3747 a / A Put all entries into single \cite / into many cite commands.")
3862 3748
3863(defvar reftex-select-bib-map nil 3749(defvar reftex-select-bib-map nil
3864 "Keymap used for *RefTeX Select* buffer, when selecting a BibTeX entry. 3750 "Keymap used for *RefTeX Select* buffer, when selecting a BibTeX entry.
@@ -3909,55 +3795,21 @@ During a selection process, these are the local bindings.
3909 (cdr (assq 'bib (symbol-value reftex-docstruct-symbol))) 3795 (cdr (assq 'bib (symbol-value reftex-docstruct-symbol)))
3910 (error "\\bibliography statement missing or .bib files not found."))) 3796 (error "\\bibliography statement missing or .bib files not found.")))
3911 3797
3912(defun reftex-find-tex-file (file master-dir &optional die)
3913 ;; Find FILE in MASTER-DIR or on reftex-tex-path.
3914 ;; FILE may be given without the .tex extension.
3915 (cond
3916 ((file-name-absolute-p file)
3917 (if (file-exists-p file) file nil))
3918 (t
3919 (reftex-access-search-path "tex")
3920 (let* ((path (cons master-dir reftex-tex-path))
3921 file1)
3922 (setq file1
3923 (or (reftex-find-file-on-path (concat file ".tex") path)
3924 (reftex-find-file-on-path file path)))
3925 (unless file1
3926 (reftex-access-search-path "tex" t file)
3927 (setq path (cons master-dir reftex-tex-path))
3928 (setq file1
3929 (or (reftex-find-file-on-path (concat file ".tex") path)
3930 (reftex-find-file-on-path file path))))
3931 (cond (file1 file1)
3932 (die (error "No such file: %s" file) nil)
3933 (t (message "No such file: %s (ignored)" file) nil))))))
3934
3935(defun reftex-find-bib-file (file master-dir &optional die)
3936 ;; Find FILE in MASTER-DIR or on reftex-bib-path
3937 (reftex-access-search-path "bib")
3938 (let ((file1 (reftex-find-file-on-path
3939 file (cons master-dir reftex-bib-path))))
3940 (unless file1
3941 (reftex-access-search-path "bib" t file)
3942 (setq file1 (reftex-find-file-on-path
3943 file (cons master-dir reftex-bib-path))))
3944 (cond (file1 file1)
3945 (die (error "No such file: %s" file) nil)
3946 (t (message "No such file: %s (ignored)" file) nil))))
3947
3948;; Find a certain reference in any of the BibTeX files. 3798;; Find a certain reference in any of the BibTeX files.
3949 3799
3950(defun reftex-pop-to-bibtex-entry (key file-list 3800(defun reftex-pop-to-bibtex-entry (key file-list &optional mark-to-kill
3951 &optional mark-to-kill highlight item) 3801 highlight item return)
3952 ;; Find BibTeX KEY in any file in FILE-LIST in another window. 3802 ;; Find BibTeX KEY in any file in FILE-LIST in another window.
3953 ;; If MARK-TO-KILL is non-nil, mark new buffer to kill. 3803 ;; If MARK-TO-KILL is non-nil, mark new buffer to kill.
3954 ;; If HIGHLIGHT is non-nil, highlight the match. 3804 ;; If HIGHLIGHT is non-nil, highlight the match.
3955 ;; If ITEM in non-nil, search for bibitem instead of database entry. 3805 ;; If ITEM in non-nil, search for bibitem instead of database entry.
3806 ;; If ECHO is non-nil, just display short info in echo area.
3956 3807
3957 (let* ((re 3808 (let* ((re
3958 (if item 3809 (if item
3959 (concat "\\\\bibitem\\(\\[[^]]*\\]\\)?{" (regexp-quote key) "}") 3810 (concat "\\\\bibitem\\(\\[[^]]*\\]\\)?{" (regexp-quote key) "}")
3960 (concat "@[a-zA-Z]+[ \t\n\r]*[{(][ \t\n\r]*" (regexp-quote key)))) 3811 (concat "@[a-zA-Z]+[ \t\n\r]*[{(][ \t\n\r]*" (regexp-quote key)
3812 "[, \t\r\n}]")))
3961 (window-conf (current-window-configuration)) 3813 (window-conf (current-window-configuration))
3962 file buf) 3814 file buf)
3963 3815
@@ -3973,18 +3825,34 @@ During a selection process, these are the local bindings.
3973 (goto-char (point-min)) 3825 (goto-char (point-min))
3974 (when (re-search-forward re nil t) 3826 (when (re-search-forward re nil t)
3975 (goto-char (match-beginning 0)) 3827 (goto-char (match-beginning 0))
3828 (when return
3829 ;; Just return the relevant entry
3830 (setq return
3831 (if item
3832 (buffer-substring
3833 (goto-char (match-end 0))
3834 (if (re-search-forward
3835 "\\\\bibitem\\|\\\\end{thebibliography}" nil t)
3836 (match-beginning 0)
3837 (point)))
3838 (buffer-substring (point)
3839 (condition-case nil
3840 (progn (forward-list 1) (point))
3841 (error (point))))))
3842 (set-window-configuration window-conf)
3843 (throw 'exit return))
3976 (recenter 0) 3844 (recenter 0)
3977 (if highlight 3845 (if highlight
3978 (reftex-highlight 0 (match-beginning 0) (match-end 0))) 3846 (reftex-highlight 0 (match-beginning 0) (match-end 0)))
3979 (throw 'exit (selected-window)))) 3847 (throw 'exit (selected-window))))
3980 (set-window-configuration window-conf) 3848 (set-window-configuration window-conf)
3981 (if item 3849 (if item
3982 (message "No \\bibitem with citation key %s" key) 3850 (error "No \\bibitem with citation key %s" key)
3983 (message "No BibTeX entry with citation key %s" key))))) 3851 (error "No BibTeX entry with citation key %s" key)))))
3984 3852
3985;; Parse bibtex buffers 3853;; Parse bibtex buffers
3986 3854
3987(defun reftex-extract-bib-entries (buffers &optional get-word) 3855(defun reftex-extract-bib-entries (buffers)
3988 ;; Extract bib entries which match regexps from BUFFERS. 3856 ;; Extract bib entries which match regexps from BUFFERS.
3989 ;; BUFFERS is a list of buffers or file names. 3857 ;; BUFFERS is a list of buffers or file names.
3990 ;; Return list with entries." 3858 ;; Return list with entries."
@@ -3993,10 +3861,17 @@ During a selection process, these are the local bindings.
3993 found-list entry buffer1 buffer alist 3861 found-list entry buffer1 buffer alist
3994 key-point start-point end-point) 3862 key-point start-point end-point)
3995 3863
3996 (setq re-list (split-string 3864 ;; Read a regexp, completing on known citation keys.
3997 (read-string "RegExp [ && RegExp...]: " 3865 (setq re-list
3998 nil 'reftex-cite-regexp-hist) 3866 (split-string
3999 "[ \t]*&&[ \t]*")) 3867 (completing-read
3868 "RegExp [ && RegExp...]: "
3869 (if (fboundp 'LaTeX-bibitem-list)
3870 (LaTeX-bibitem-list)
3871 (cdr (assoc 'bibview-cache
3872 (symbol-value reftex-docstruct-symbol))))
3873 nil nil nil 'reftex-cite-regexp-hist)
3874 "[ \t]*&&[ \t]*"))
4000 3875
4001 (setq first-re (car re-list) ; We'll use the first re to find things, 3876 (setq first-re (car re-list) ; We'll use the first re to find things,
4002 rest-re (cdr re-list)) ; the others to narrow down. 3877 rest-re (cdr re-list)) ; the others to narrow down.
@@ -4071,6 +3946,9 @@ During a selection process, these are the local bindings.
4071 (push (cons "&formatted" (reftex-format-bib-entry alist)) 3946 (push (cons "&formatted" (reftex-format-bib-entry alist))
4072 alist) 3947 alist)
4073 3948
3949 ;; make key the first element
3950 (push (reftex-get-bib-field "&key" alist) alist)
3951
4074 ;; add it to the list 3952 ;; add it to the list
4075 (push alist found-list)))) 3953 (push alist found-list))))
4076 (reftex-kill-temporary-buffers)))) 3954 (reftex-kill-temporary-buffers))))
@@ -4153,7 +4031,7 @@ During a selection process, these are the local bindings.
4153 (progn 4031 (progn
4154 (beginning-of-line 1) 4032 (beginning-of-line 1)
4155 (setq end (point)))) 4033 (setq end (point))))
4156 (when (and start (point)) 4034 (when (and start end)
4157 (setq entries 4035 (setq entries
4158 (mapcar 'reftex-parse-bibitem 4036 (mapcar 'reftex-parse-bibitem
4159 (delete "" 4037 (delete ""
@@ -4172,18 +4050,18 @@ During a selection process, these are the local bindings.
4172 4050
4173 (while (and (setq re (pop re-list)) entries) 4051 (while (and (setq re (pop re-list)) entries)
4174 (setq entries 4052 (setq entries
4175 (delete nil (mapcar 4053 (delq nil (mapcar
4176 (function 4054 (function
4177 (lambda (x) 4055 (lambda (x)
4178 (if (string-match re (cdr (assoc "&entry" x))) 4056 (if (string-match re (cdr (assoc "&entry" x)))
4179 x nil))) 4057 x nil)))
4180 entries)))) 4058 entries))))
4181
4182 (setq entries 4059 (setq entries
4183 (mapcar 4060 (mapcar
4184 (function
4185 (lambda (x) 4061 (lambda (x)
4186 (cons (cons "&formatted" (reftex-format-bibitem x)) x))) 4062 (push (cons "&formatted" (reftex-format-bibitem x)) x)
4063 (push (reftex-get-bib-field "&key" x) x)
4064 x)
4187 entries)) 4065 entries))
4188 4066
4189 entries)) 4067 entries))
@@ -4211,7 +4089,7 @@ During a selection process, these are the local bindings.
4211 (save-restriction 4089 (save-restriction
4212 (if entry 4090 (if entry
4213 (progn 4091 (progn
4214 (switch-to-buffer "*RefTeX-scratch*") 4092 (set-buffer (get-buffer-create " *RefTeX-scratch*"))
4215 (fundamental-mode) 4093 (fundamental-mode)
4216 (erase-buffer) 4094 (erase-buffer)
4217 (insert entry)) 4095 (insert entry))
@@ -4241,7 +4119,7 @@ During a selection process, these are the local bindings.
4241 (= ?\\ (char-after (- (point) 2)))))) 4119 (= ?\\ (char-after (- (point) 2))))))
4242 (t 4120 (t
4243 (setq start (point)) 4121 (setq start (point))
4244 (re-search-forward "[ \t\n\r,}]" nil 1))) 4122 (re-search-forward "[ \t]*[\n\r,}]" nil 1)))
4245 (setq field (buffer-substring-no-properties start (1- (point)))) 4123 (setq field (buffer-substring-no-properties start (1- (point))))
4246 ;; remove extra whitespace 4124 ;; remove extra whitespace
4247 (while (string-match "[\n\t\r]\\|[ \t][ \t]+" field) 4125 (while (string-match "[\n\t\r]\\|[ \t][ \t]+" field)
@@ -4255,10 +4133,14 @@ During a selection process, these are the local bindings.
4255 (push (cons key field) alist)))) 4133 (push (cons key field) alist))))
4256 alist)) 4134 alist))
4257 4135
4258(defun reftex-get-bib-field (fieldname entry) 4136(defun reftex-get-bib-field (fieldname entry &optional format)
4259 ;; Extract the field FIELDNAME from an ENTRY 4137 ;; Extract the field FIELDNAME from an ENTRY
4260 (or (cdr (assoc fieldname entry)) 4138 (let ((cell (assoc fieldname entry)))
4261 "")) 4139 (if cell
4140 (if format
4141 (format format (cdr cell))
4142 (cdr cell))
4143 "")))
4262 4144
4263(defun reftex-format-bib-entry (entry) 4145(defun reftex-format-bib-entry (entry)
4264 ;; Format a BibTeX ENTRY so that it is nice to look at 4146 ;; Format a BibTeX ENTRY so that it is nice to look at
@@ -4287,10 +4169,15 @@ During a selection process, these are the local bindings.
4287 ((or (equal type "conference") 4169 ((or (equal type "conference")
4288 (equal type "incollection") 4170 (equal type "incollection")
4289 (equal type "inproceedings")) 4171 (equal type "inproceedings"))
4290 (concat "in: " (reftex-get-bib-field "booktitle" entry))) 4172 (reftex-get-bib-field "booktitle" entry "in: %s"))
4291 (t "")))) 4173 (t ""))))
4292 (setq authors (reftex-truncate authors 30 t t)) 4174 (setq authors (reftex-truncate authors 30 t t))
4293 (when (reftex-use-fonts) 4175 (when (reftex-use-fonts)
4176 (put-text-property 0 (length key) 'face
4177 (reftex-verified-face reftex-label-face
4178 'font-lock-constant-face
4179 'font-lock-reference-face)
4180 key)
4294 (put-text-property 0 (length authors) 'face reftex-bib-author-face 4181 (put-text-property 0 (length authors) 'face reftex-bib-author-face
4295 authors) 4182 authors)
4296 (put-text-property 0 (length year) 'face reftex-bib-year-face 4183 (put-text-property 0 (length year) 'face reftex-bib-year-face
@@ -4337,6 +4224,7 @@ During a selection process, these are the local bindings.
4337 4224
4338;; Make a citation 4225;; Make a citation
4339 4226
4227;;;###autoload
4340(defun reftex-citation (&optional no-insert) 4228(defun reftex-citation (&optional no-insert)
4341 "Make a citation using BibTeX database files. 4229 "Make a citation using BibTeX database files.
4342After asking for a Regular Expression, it scans the buffers with 4230After asking for a Regular Expression, it scans the buffers with
@@ -4346,11 +4234,12 @@ to `reftex-cite-format' and inserted into the buffer.
4346If NO-INSERT is non-nil, nothing is inserted, only the selected key returned. 4234If NO-INSERT is non-nil, nothing is inserted, only the selected key returned.
4347The regular expression uses an expanded syntax: && is interpreted as `and'. 4235The regular expression uses an expanded syntax: && is interpreted as `and'.
4348Thus, `aaaa&&bbb' matches entries which contain both `aaaa' and `bbb'. 4236Thus, `aaaa&&bbb' matches entries which contain both `aaaa' and `bbb'.
4237While entering the regexp, completion on knows citation keys is possible.
4349When this function is called with point inside the braces of a \\cite 4238When this function is called with point inside the braces of a \\cite
4350command, it will add another key, ignoring the value of `reftex-cite-format'. 4239command, it will add another key, ignoring the value of `reftex-cite-format'.
4351When called with a numeric prefix, that many citations will be made and all 4240When called with a numeric prefix, that many citations will be made and all
4352put into the same \\cite command. 4241put into the same \\cite command.
4353When called with just C-u as prefix, enforces rescan of buffer for 4242When called with just or two C-u as prefix, enforces rescan of buffer for
4354bibliography statement (e.g. if it was changed)." 4243bibliography statement (e.g. if it was changed)."
4355 4244
4356 (interactive) 4245 (interactive)
@@ -4358,8 +4247,11 @@ bibliography statement (e.g. if it was changed)."
4358 ;; check for recursive edit 4247 ;; check for recursive edit
4359 (reftex-check-recursive-edit) 4248 (reftex-check-recursive-edit)
4360 4249
4361 ;; if there is just 1 C-u prefix arg, force to rescan buffer 4250 ;; This function may also be called outside reftex-mode.
4362 (reftex-access-scan-info current-prefix-arg) 4251 ;; Thus look for the scanning info only if in reftex-mode.
4252
4253 (when reftex-mode
4254 (reftex-access-scan-info current-prefix-arg))
4363 4255
4364 ;; Call reftex-do-citation, but protected 4256 ;; Call reftex-do-citation, but protected
4365 (unwind-protect 4257 (unwind-protect
@@ -4369,203 +4261,238 @@ bibliography statement (e.g. if it was changed)."
4369(defun reftex-do-citation (&optional arg no-insert) 4261(defun reftex-do-citation (&optional arg no-insert)
4370 ;; This really does the work of reftex-citation. 4262 ;; This really does the work of reftex-citation.
4371 4263
4264 (let* ((format (reftex-figure-out-cite-format arg no-insert))
4265 (docstruct-symbol reftex-docstruct-symbol)
4266 (selected-entries (reftex-offer-bib-menu))
4267 (insert-entries selected-entries)
4268 entry string cite-view)
4269
4270 (unless selected-entries (error "Quit"))
4271
4272 (if (stringp selected-entries)
4273 ;; Nonexistent entry
4274 (setq selected-entries nil
4275 insert-entries (list (list selected-entries
4276 (cons "&key" selected-entries))))
4277 ;; It makes sense to compute the cite-view strings.
4278 (setq cite-view t))
4279
4280 (when (eq (car selected-entries) 'concat)
4281 ;; All keys go into a single command - we need to trick a little
4282 (pop selected-entries)
4283 (let ((concat-keys (mapconcat 'car selected-entries ",")))
4284 (setq insert-entries
4285 (list (list concat-keys (cons "&key" concat-keys))))))
4286
4287 (unless no-insert
4288
4289 ;; We shall insert this into the buffer...
4290 (message "Formatting...")
4291
4292 (while (setq entry (pop insert-entries))
4293 ;; Format the citation and insert it
4294 (setq string (if reftex-format-cite-function
4295 (funcall reftex-format-cite-function
4296 (reftex-get-bib-field "&key" entry)
4297 format)
4298 (reftex-format-citation entry format)))
4299 (insert string))
4300
4301 ;; Reposition cursor?
4302 (when (string-match "\\?" string)
4303 (search-backward "?")
4304 (delete-char 1))
4305
4306 ;; Tell AUCTeX
4307 (when (and (fboundp 'LaTeX-add-bibitems) reftex-plug-into-AUCTeX)
4308 (apply 'LaTeX-add-bibitems (mapcar 'car selected-entries)))
4309
4310 ;; Produce the cite-view strings
4311 (when (and reftex-cache-cite-echo cite-view)
4312 (mapcar (lambda (entry)
4313 (reftex-make-cite-echo-string entry docstruct-symbol))
4314 selected-entries))
4315
4316 (message ""))
4317
4318 (set-marker reftex-select-return-marker nil)
4319 (reftex-kill-buffer "*RefTeX Select*")
4320
4321 ;; Check if the prefix arg was numeric, and call recursively
4322 (when (integerp arg)
4323 (if (> arg 1)
4324 (progn
4325 (skip-chars-backward "}")
4326 (decf arg)
4327 (reftex-do-citation arg))
4328 (forward-char 1)))
4329
4330 ;; Return the citation key
4331 (car (car selected-entries))))
4332
4333(defun reftex-figure-out-cite-format (arg no-insert)
4372 ;; Check if there is already a cite command at point and change cite format 4334 ;; Check if there is already a cite command at point and change cite format
4373 ;; in order to only add another reference in the same cite command. 4335 ;; in order to only add another reference in the same cite command.
4374 (let (key format (macro (car (car (reftex-what-macro t))))) 4336 (let ((macro (car (reftex-what-macro 1)))
4375 (if (and (stringp macro) 4337 (cite-format-value (reftex-get-cite-format))
4376 (string-match "\\`\\\\cite\\|cite\\'" macro)) 4338 key format)
4377 (progn 4339 (cond
4378 (cond 4340 (no-insert
4379 ((or (not arg) 4341 ;; Format does not really matter because nothing will be inserted.
4380 (not (listp arg))) 4342 (setq format "%l"))
4381 (setq format 4343
4382 (concat 4344 ((and (stringp macro)
4383 (if (not (or (= (preceding-char) ?{) 4345 (string-match "\\`\\\\cite\\|cite\\'" macro))
4384 (= (preceding-char) ?,))) 4346 ;; We are already inside a cite macro
4385 "," 4347 (if (or (not arg) (not (listp arg)))
4386 "") 4348 (setq format
4387 "%l" 4349 (concat
4388 (if (not (or (= (following-char) ?}) 4350 (if (member (preceding-char) '(?\{ ?,)) "" ",")
4389 (= (following-char) ?,))) 4351 "%l"
4390 "," 4352 (if (member (following-char) '(?\} ?,)) "" ",")))
4391 "")))) 4353 (setq format "%l")))
4392 (t 4354 (t
4393 (setq format "%l")))) 4355 ;; Figure out the correct format
4394 ;; else: figure out the correct format
4395 (setq format 4356 (setq format
4396 (cond 4357 (if (and (symbolp cite-format-value)
4397 ((stringp reftex-cite-format) reftex-cite-format) 4358 (assq cite-format-value reftex-cite-format-builtin))
4398 ((and (symbolp reftex-cite-format) 4359 (nth 2 (assq cite-format-value reftex-cite-format-builtin))
4399 (assq reftex-cite-format reftex-cite-format-builtin)) 4360 cite-format-value))
4400 (nth 2 (assq reftex-cite-format reftex-cite-format-builtin))) 4361 (when (listp format)
4401 (t reftex-cite-format))) 4362 (setq key
4402 (if (listp format) 4363 (reftex-select-with-char
4403 (save-window-excursion 4364 "" (concat "SELECT A CITATION FORMAT\n\n"
4404 (with-output-to-temp-buffer "*RefTeX Select*" 4365 (mapconcat
4405 (princ "SELECT A CITATION FORMAT\n\n") 4366 (lambda (x)
4406 (princ 4367 (format "[%c] %s %s" (car x)
4407 (mapconcat 4368 (if (> (car x) 31) " " "")
4408 (function (lambda (x) 4369 (cdr x)))
4409 (format "[%c] %s %s" (car x) 4370 format "\n"))))
4410 (if (> (car x) 31) " " "") 4371 (if (assq key format)
4411 (cdr x)))) 4372 (setq format (cdr (assq key format)))
4412 format "\n"))) 4373 (error "No citation format associated with key `%c'" key)))))
4413 (reftex-enlarge-to-fit "*RefTeX Select*") 4374 format))
4414 (setq key (read-char)) 4375
4415 (if (assq key format) 4376(defun reftex-get-cite-format ()
4416 (setq format (cdr (assq key format))) 4377 ;; Return the current citation format. Either the document-local value in
4417 (error "No citation format associated with key `%c'" key))))) 4378 ;; reftex-cite-format-symbol, or the global value in reftex-cite-format.
4418 4379 (if (and reftex-docstruct-symbol
4419 (let* (entry data rtn ins-string re-list re found-list found-list-r) 4380 (symbolp reftex-docstruct-symbol)
4420 4381 (get reftex-docstruct-symbol 'reftex-cite-format))
4421 ;; Scan bibtex files 4382 (get reftex-docstruct-symbol 'reftex-cite-format)
4422 (setq found-list 4383 reftex-cite-format))
4423 (cond 4384
4424 ((assq 'bib (symbol-value reftex-docstruct-symbol)) 4385(defun reftex-offer-bib-menu ()
4425 ;; using BibTeX database files. 4386 ;; Offer bib menu and return list of selected items
4426 (reftex-extract-bib-entries (reftex-get-bibfile-list))) 4387
4427 ((assq 'thebib (symbol-value reftex-docstruct-symbol)) 4388 (let (found-list rtn key data selected-entries)
4428 ;; using thebibliography environment. 4389
4429 (reftex-extract-bib-entries-from-thebibliography 4390 (while
4430 (cdr (assq 'thebib (symbol-value reftex-docstruct-symbol))))) 4391 (not
4431 (reftex-default-bibliography 4392 (catch 'done
4432 (message "Using default bibliography") 4393 ;; Scan bibtex files
4433 (reftex-extract-bib-entries reftex-default-bibliography)) 4394 (setq found-list
4434 (t (error "Document does not have a bibliography")))) 4395 (cond
4435 4396 ((assq 'bib (symbol-value reftex-docstruct-symbol))
4436 (unless found-list 4397 ;; using BibTeX database files.
4437 (error "Sorry, no matches found")) 4398 (reftex-extract-bib-entries (reftex-get-bibfile-list)))
4438 4399 ((assq 'thebib (symbol-value reftex-docstruct-symbol))
4439 ;; remember where we came from 4400 ;; using thebibliography environment.
4440 (setq reftex-call-back-to-this-buffer (current-buffer)) 4401 (reftex-extract-bib-entries-from-thebibliography
4441 (set-marker reftex-select-return-marker (point)) 4402 (cdr (assq 'thebib (symbol-value reftex-docstruct-symbol)))))
4442 4403 (reftex-default-bibliography
4443 ;; offer selection 4404 (message "Using default bibliography")
4444 (save-window-excursion 4405 (reftex-extract-bib-entries reftex-default-bibliography))
4445 (delete-other-windows) 4406 (t (error "No valid bibliography in this document, and no default available"))))
4446 (let ((default-major-mode 'reftex-select-bib-mode)) 4407
4447 (reftex-kill-buffer "*RefTeX Select*") 4408 (unless found-list
4448 (switch-to-buffer-other-window "*RefTeX Select*") 4409 (error "Sorry, no matches found"))
4449 (unless (eq major-mode 'reftex-select-bib-mode) 4410
4450 (reftex-select-bib-mode)) 4411 ;; Remember where we came from
4451 (let ((buffer-read-only nil)) 4412 (setq reftex-call-back-to-this-buffer (current-buffer))
4452 (erase-buffer) 4413 (set-marker reftex-select-return-marker (point))
4453 (reftex-insert-bib-matches found-list))) 4414
4454 (setq buffer-read-only t) 4415 ;; Offer selection
4455 (if (= 0 (buffer-size)) 4416 (save-window-excursion
4456 (error "Sorry, no matches found")) 4417 (delete-other-windows)
4457 (setq truncate-lines t) 4418 (let ((default-major-mode 'reftex-select-bib-mode))
4458 (goto-char 1) 4419 (reftex-kill-buffer "*RefTeX Select*")
4459 (if (catch 'exit 4420 (switch-to-buffer-other-window "*RefTeX Select*")
4460 (while t 4421 (unless (eq major-mode 'reftex-select-bib-mode)
4461 (setq rtn 4422 (reftex-select-bib-mode))
4462 (reftex-select-item 4423 (let ((buffer-read-only nil))
4463 reftex-citation-prompt 4424 (erase-buffer)
4464 reftex-citation-help 4425 (reftex-insert-bib-matches found-list)))
4465 reftex-select-bib-map 4426 (setq buffer-read-only t)
4466 nil 4427 (if (= 0 (buffer-size))
4467 'reftex-bibtex-selection-callback nil)) 4428 (error "Sorry, no matches found"))
4468 (setq key (car rtn) 4429 (setq truncate-lines t)
4469 data (nth 1 rtn)) 4430 (goto-char 1)
4470 (unless key (throw 'exit nil)) 4431 (while t
4471 (cond 4432 (setq rtn
4472 ((eq key ?g) 4433 (reftex-select-item
4473 (setq found-list 4434 reftex-citation-prompt
4474 (save-excursion 4435 reftex-citation-help
4475 (set-buffer reftex-call-back-to-this-buffer) 4436 reftex-select-bib-map
4476 (reftex-extract-bib-entries 4437 nil
4477 (reftex-get-bibfile-list)))) 4438 'reftex-bibtex-selection-callback nil))
4478 (let ((buffer-read-only nil)) 4439 (setq key (car rtn)
4479 (erase-buffer) 4440 data (nth 1 rtn))
4480 (reftex-insert-bib-matches found-list)) 4441 (unless key (throw 'done t))
4481 (if (= 0 (buffer-size)) 4442 (cond
4482 (error "Sorry, no matches found")) 4443 ((eq key ?g)
4483 (goto-char 1)) 4444 ;; Start over
4484 4445 (throw 'done nil))
4485 ((eq key ?r) 4446 ((eq key ?r)
4486 ;; restrict with new regular expression 4447 ;; Restrict with new regular expression
4487 (setq re-list 4448 (setq found-list (reftex-restrict-bib-matches found-list))
4488 (split-string (read-string 4449 (let ((buffer-read-only nil))
4489 "RegExp [ && RegExp...]: " 4450 (erase-buffer)
4490 nil 'reftex-cite-regexp-hist) 4451 (reftex-insert-bib-matches found-list))
4491 "[ \t]*&&[ \t]*")) 4452 (goto-char 1))
4492 (while re-list 4453 ((eq key ?A)
4493 (setq re (car re-list) 4454 ;; Take all
4494 re-list (cdr re-list)) 4455 (setq selected-entries found-list)
4495 (setq found-list-r 4456 (throw 'done t))
4496 (delete "" 4457 ((eq key ?a)
4497 (mapcar 4458 ;; Take all
4498 (function 4459 (setq selected-entries (cons 'concat found-list))
4499 (lambda (x) 4460 (throw 'done t))
4500 (if (string-match 4461 ((or (eq key ?\C-m)
4501 re (cdr (assoc "&entry" x))) 4462 (eq key 'return))
4502 x 4463 ;; Take selected
4503 ""))) 4464 (setq selected-entries (if data (list data) nil))
4504 found-list)))) 4465 (throw 'done t))
4505 (if found-list-r 4466 ((stringp key)
4506 (setq found-list found-list-r) 4467 ;; Got this one with completion
4507 (ding)) 4468 (setq selected-entries key)
4508 (let ((buffer-read-only nil)) 4469 (throw 'done t))
4509 (erase-buffer) 4470 (t
4510 (reftex-insert-bib-matches found-list)) 4471 (ding))))))))
4511 (goto-char 1)) 4472 selected-entries))
4512 ((eq key ?a) 4473
4513 (setq entry 'all) 4474(defun reftex-restrict-bib-matches (found-list)
4514 (throw 'exit t)) 4475 ;; Limit FOUND-LIST with more regular expressions
4515 ((or (eq key ?\C-m) 4476 (let ((re-list (split-string (read-string
4516 (eq key 'return)) 4477 "RegExp [ && RegExp...]: "
4517 (if data 4478 nil 'reftex-cite-regexp-hist)
4518 (setq entry data) 4479 "[ \t]*&&[ \t]*"))
4519 (setq entry nil)) 4480 (found-list-r found-list)
4520 (throw 'exit t)) 4481 re)
4521 ((stringp key) 4482 (while (setq re (pop re-list))
4522 (setq entry (list (cons "&key" key))) 4483 (setq found-list-r
4523 (throw 'exit t)) 4484 (delq nil
4524 (t 4485 (mapcar
4525 (ding))))) 4486 (lambda (x)
4526 (progn 4487 (if (string-match
4527 ;; format the entry 4488 re (cdr (assoc "&entry" x)))
4528 (if (eq entry 'all) 4489 x
4529 (setq ins-string 4490 nil))
4530 (mapconcat 4491 found-list-r))))
4531 (function 4492 (if found-list-r
4532 (lambda (entry) 4493 found-list-r
4533 (if reftex-format-cite-function 4494 (ding)
4534 (funcall reftex-format-cite-function 4495 found-list)))
4535 (reftex-get-bib-field "&key" entry)
4536 format)
4537 (reftex-format-citation entry format))))
4538 found-list "\n"))
4539 (setq ins-string
4540 (if reftex-format-cite-function
4541 (funcall reftex-format-cite-function
4542 (reftex-get-bib-field "&key" entry)
4543 format)
4544 (reftex-format-citation entry format)))))
4545 (setq ins-string "")
4546 (message "Quit")))
4547 (set-marker reftex-select-return-marker (point))
4548 (kill-buffer "*RefTeX Select*")
4549
4550 (unless no-insert
4551 (insert ins-string)
4552 (when (string-match "\\?" ins-string)
4553 (search-backward "?")
4554 (delete-char 1)))
4555 (message "")
4556
4557 ;; Check if the prefix arg was numeric, and call recursively
4558 (when (and (integerp arg)
4559 (> arg 1)
4560 (re-search-backward
4561 "\\\\\\([a-zA-Z]*cite\\|cite[a-zA-Z]*\\)\\**\\(\\[[^]]*\\]\\)*{\\([^}]*\\)" nil t))
4562 (goto-char (match-end 0))
4563 (decf arg)
4564 (reftex-do-citation arg))
4565
4566 ;; Return the citation key
4567 (or (eq entry 'all)
4568 (reftex-get-bib-field "&key" entry)))))
4569 4496
4570(defun reftex-insert-bib-matches (list) 4497(defun reftex-insert-bib-matches (list)
4571 ;; Insert the bib matches and number them correctly 4498 ;; Insert the bib matches and number them correctly
@@ -4582,11 +4509,13 @@ bibliography statement (e.g. if it was changed)."
4582 (put-text-property 0 len ':data x tmp) 4509 (put-text-property 0 len ':data x tmp)
4583 (put-text-property 0 (1- len) 'mouse-face mouse-face tmp) 4510 (put-text-property 0 (1- len) 'mouse-face mouse-face tmp)
4584 (insert tmp))) 4511 (insert tmp)))
4585 list))) 4512 list))
4513 (run-hooks 'reftex-display-copied-context-hook))
4586 4514
4587(defun reftex-format-names (namelist n) 4515(defun reftex-format-names (namelist n)
4588 (let (last (len (length namelist))) 4516 (let (last (len (length namelist)))
4589 (cond 4517 (cond
4518 ((< len 1) "")
4590 ((= 1 len) (car namelist)) 4519 ((= 1 len) (car namelist))
4591 ((> len n) (concat (car namelist) (nth 2 reftex-cite-punctuation))) 4520 ((> len n) (concat (car namelist) (nth 2 reftex-cite-punctuation)))
4592 (t 4521 (t
@@ -4625,7 +4554,9 @@ bibliography statement (e.g. if it was changed)."
4625 (reftex-get-bib-names "author" entry) 4554 (reftex-get-bib-names "author" entry)
4626 (or n 2))) 4555 (or n 2)))
4627 ((= l ?A) (car (reftex-get-bib-names "author" entry))) 4556 ((= l ?A) (car (reftex-get-bib-names "author" entry)))
4628 ((= l ?b) (reftex-get-bib-field "booktitle" entry)) 4557 ((= l ?b) (reftex-get-bib-field "booktitle" entry "in: %s"))
4558 ((= l ?B) (reftex-abbreviate-title
4559 (reftex-get-bib-field "booktitle" entry "in: %s")))
4629 ((= l ?c) (reftex-get-bib-field "chapter" entry)) 4560 ((= l ?c) (reftex-get-bib-field "chapter" entry))
4630 ((= l ?d) (reftex-get-bib-field "edition" entry)) 4561 ((= l ?d) (reftex-get-bib-field "edition" entry))
4631 ((= l ?e) (reftex-format-names 4562 ((= l ?e) (reftex-format-names
@@ -4647,6 +4578,8 @@ bibliography statement (e.g. if it was changed)."
4647 ((= l ?u) (reftex-get-bib-field "publisher" entry)) 4578 ((= l ?u) (reftex-get-bib-field "publisher" entry))
4648 ((= l ?r) (reftex-get-bib-field "address" entry)) 4579 ((= l ?r) (reftex-get-bib-field "address" entry))
4649 ((= l ?t) (reftex-get-bib-field "title" entry)) 4580 ((= l ?t) (reftex-get-bib-field "title" entry))
4581 ((= l ?T) (reftex-abbreviate-title
4582 (reftex-get-bib-field "title" entry)))
4650 ((= l ?v) (reftex-get-bib-field "volume" entry)) 4583 ((= l ?v) (reftex-get-bib-field "volume" entry))
4651 ((= l ?y) (reftex-get-bib-field "year" entry))))) 4584 ((= l ?y) (reftex-get-bib-field "year" entry)))))
4652 4585
@@ -4682,17 +4615,16 @@ bibliography statement (e.g. if it was changed)."
4682 (t (ding) (throw 'exit)))) 4615 (t (ding) (throw 'exit))))
4683 4616
4684 (when no-revisit 4617 (when no-revisit
4685 (setq bibfile-list 4618 (setq bibfile-list (reftex-visited-files bibfile-list)))
4686 (delq nil (mapcar (lambda (x)
4687 (if (get-file-buffer x) x nil))
4688 bibfile-list))))
4689
4690 (reftex-pop-to-bibtex-entry
4691 key bibfile-list (not reftex-keep-temporary-buffers) t item))
4692 4619
4620 (condition-case nil
4621 (reftex-pop-to-bibtex-entry
4622 key bibfile-list (not reftex-keep-temporary-buffers) t item)
4623 (error (ding))))
4624
4693 (select-window win))) 4625 (select-window win)))
4694 4626
4695;;; =========================================================================== 4627;;; =========================================================================
4696;;; 4628;;;
4697;;; Here is the routine used for selection 4629;;; Here is the routine used for selection
4698 4630
@@ -4760,7 +4692,8 @@ bibliography statement (e.g. if it was changed)."
4760 (add-hook 'post-command-hook 'reftex-select-post-command-hook nil t) 4692 (add-hook 'post-command-hook 'reftex-select-post-command-hook nil t)
4761 (princ prompt) 4693 (princ prompt)
4762 (set-marker reftex-recursive-edit-marker (point)) 4694 (set-marker reftex-recursive-edit-marker (point))
4763 (run-hooks 'post-command-hook) ;; because XEmacs does not do it 4695 ;; XEmacs does not run post-command-hook here
4696 (and (featurep 'xemacsp) (run-hooks 'post-command-hook))
4764 (recursive-edit)) 4697 (recursive-edit))
4765 4698
4766 (set-marker reftex-recursive-edit-marker nil) 4699 (set-marker reftex-recursive-edit-marker nil)
@@ -4775,6 +4708,7 @@ bibliography statement (e.g. if it was changed)."
4775 (+ (count-lines (point-min) (point)) (if (bolp) 1 0))) 4708 (+ (count-lines (point-min) (point)) (if (bolp) 1 0)))
4776 (set (make-local-variable 'reftex-last-data) last-data) 4709 (set (make-local-variable 'reftex-last-data) last-data)
4777 (reftex-kill-buffer "*RefTeX Help*") 4710 (reftex-kill-buffer "*RefTeX Help*")
4711 (setq callback-fwd (not callback-fwd)) ;; ;-)))
4778 (message "") 4712 (message "")
4779 (list ev data last-data))) 4713 (list ev data last-data)))
4780 4714
@@ -4820,35 +4754,41 @@ bibliography statement (e.g. if it was changed)."
4820 (if (or (not (pos-visible-in-window-p b)) 4754 (if (or (not (pos-visible-in-window-p b))
4821 (not (pos-visible-in-window-p e))) 4755 (not (pos-visible-in-window-p e)))
4822 (recenter (/ (window-height) 2))) 4756 (recenter (/ (window-height) 2)))
4823 (when (and (fboundp 'current-message) 4757 (unless (current-message)
4824 (not (current-message)))
4825 (princ prompt)))) 4758 (princ prompt))))
4826 4759
4827(defun reftex-select-next (&optional arg) 4760(defun reftex-select-next (&optional arg)
4761 "Move to next selectable item."
4828 (interactive "p") 4762 (interactive "p")
4829 (setq callback-fwd t) 4763 (setq callback-fwd t)
4830 (or (eobp) (forward-char 1)) 4764 (or (eobp) (forward-char 1))
4831 (re-search-forward "^[^. \t\n\r]" nil t arg) 4765 (re-search-forward "^[^. \t\n\r]" nil t arg)
4832 (beginning-of-line 1)) 4766 (beginning-of-line 1))
4833(defun reftex-select-previous (&optional arg) 4767(defun reftex-select-previous (&optional arg)
4768 "Move to previous selectable item."
4834 (interactive "p") 4769 (interactive "p")
4835 (setq callback-fwd nil) 4770 (setq callback-fwd nil)
4836 (re-search-backward "^[^. \t\n\r]" nil t arg)) 4771 (re-search-backward "^[^. \t\n\r]" nil t arg))
4837(defun reftex-select-next-heading (&optional arg) 4772(defun reftex-select-next-heading (&optional arg)
4773 "Move to next table of contentes line."
4838 (interactive "p") 4774 (interactive "p")
4839 (end-of-line) 4775 (end-of-line)
4840 (re-search-forward "^ " nil t arg) 4776 (re-search-forward "^ " nil t arg)
4841 (beginning-of-line)) 4777 (beginning-of-line))
4842(defun reftex-select-previous-heading (&optional arg) 4778(defun reftex-select-previous-heading (&optional arg)
4779 "Move to previous table of contentes line."
4843 (interactive "p") 4780 (interactive "p")
4844 (re-search-backward "^ " nil t arg)) 4781 (re-search-backward "^ " nil t arg))
4845(defun reftex-select-quit () 4782(defun reftex-select-quit ()
4783 "Abort selection process."
4846 (interactive) 4784 (interactive)
4847 (throw 'myexit nil)) 4785 (throw 'myexit nil))
4848(defun reftex-select-keyboard-quit () 4786(defun reftex-select-keyboard-quit ()
4787 "Abort selection process."
4849 (interactive) 4788 (interactive)
4850 (throw 'exit t)) 4789 (throw 'exit t))
4851(defun reftex-select-jump-to-previous () 4790(defun reftex-select-jump-to-previous ()
4791 "Jump back to where previous selection process left off."
4852 (interactive) 4792 (interactive)
4853 (let (pos) 4793 (let (pos)
4854 (cond 4794 (cond
@@ -4862,15 +4802,19 @@ bibliography statement (e.g. if it was changed)."
4862 (goto-line reftex-last-line)) 4802 (goto-line reftex-last-line))
4863 (t (ding))))) 4803 (t (ding)))))
4864(defun reftex-select-toggle-follow () 4804(defun reftex-select-toggle-follow ()
4805 "Toggle follow mode: Other window follows with full context."
4865 (interactive) 4806 (interactive)
4807 (setq reftex-last-follow-point -1)
4866 (setq cb-flag (not cb-flag))) 4808 (setq cb-flag (not cb-flag)))
4867(defun reftex-select-toggle-varioref () 4809(defun reftex-select-toggle-varioref ()
4810 "Toggle the macro used for referencing the label between \\ref and \\vref."
4868 (interactive) 4811 (interactive)
4869 (if (string= varioref "\\ref") 4812 (if (string= varioref "\\ref")
4870 (setq varioref "\\vref") 4813 (setq varioref "\\vref")
4871 (setq varioref "\\ref")) 4814 (setq varioref "\\ref"))
4872 (force-mode-line-update)) 4815 (force-mode-line-update))
4873(defun reftex-select-show-insertion-point () 4816(defun reftex-select-show-insertion-point ()
4817 "Show the point from where selection was started in another window."
4874 (interactive) 4818 (interactive)
4875 (let ((this-window (selected-window))) 4819 (let ((this-window (selected-window)))
4876 (unwind-protect 4820 (unwind-protect
@@ -4881,18 +4825,22 @@ bibliography statement (e.g. if it was changed)."
4881 (recenter (/ (window-height) 2))) 4825 (recenter (/ (window-height) 2)))
4882 (select-window this-window)))) 4826 (select-window this-window))))
4883(defun reftex-select-callback () 4827(defun reftex-select-callback ()
4828 "Show full context in another window."
4884 (interactive) 4829 (interactive)
4885 (if data (funcall call-back data callback-fwd nil) (ding))) 4830 (if data (funcall call-back data callback-fwd nil) (ding)))
4886(defun reftex-select-accept () 4831(defun reftex-select-accept ()
4832 "Accept the currently selected item."
4887 (interactive) 4833 (interactive)
4888 (throw 'myexit 'return)) 4834 (throw 'myexit 'return))
4889(defun reftex-select-mouse-accept (ev) 4835(defun reftex-select-mouse-accept (ev)
4836 "Accept the item at the mouse click."
4890 (interactive "e") 4837 (interactive "e")
4891 (mouse-set-point ev) 4838 (mouse-set-point ev)
4892 (setq data (get-text-property (point) ':data)) 4839 (setq data (get-text-property (point) ':data))
4893 (setq last-data (or data last-data)) 4840 (setq last-data (or data last-data))
4894 (throw 'myexit 'return)) 4841 (throw 'myexit 'return))
4895(defun reftex-select-read-label () 4842(defun reftex-select-read-label ()
4843 "Use minibuffer to read a label to reference, with completion."
4896 (interactive) 4844 (interactive)
4897 (let ((label (completing-read 4845 (let ((label (completing-read
4898 "Label: " (symbol-value reftex-docstruct-symbol) 4846 "Label: " (symbol-value reftex-docstruct-symbol)
@@ -4900,24 +4848,29 @@ bibliography statement (e.g. if it was changed)."
4900 (unless (or (equal label "") (equal label reftex-prefix)) 4848 (unless (or (equal label "") (equal label reftex-prefix))
4901 (throw 'myexit label)))) 4849 (throw 'myexit label))))
4902(defun reftex-select-read-cite () 4850(defun reftex-select-read-cite ()
4851 "Use minibuffer to read a citation key with completion."
4903 (interactive) 4852 (interactive)
4904 (let* ((list (mapcar (lambda (x) 4853 (let* ((key (completing-read "Citation key: " found-list))
4905 (cons (reftex-get-bib-field "&key" x) 1)) 4854 (entry (assoc key found-list)))
4906 found-list)) 4855 (cond
4907 (key (completing-read "Citation key: " list))) 4856 ((or (null key) (equal key "")))
4908 (unless (equal key "") 4857 (entry
4909 (throw 'myexit key)))) 4858 (setq data entry)
4859 (setq last-data data)
4860 (throw 'myexit 'return))
4861 (t (throw 'myexit key)))))
4910(defun reftex-select-help () 4862(defun reftex-select-help ()
4863 "Display a summary of the special key bindings."
4911 (interactive) 4864 (interactive)
4912 (with-output-to-temp-buffer "*RefTeX Help*" 4865 (with-output-to-temp-buffer "*RefTeX Help*"
4913 (princ help-string)) 4866 (princ help-string))
4914 (reftex-enlarge-to-fit "*RefTeX Help*" t)) 4867 (reftex-enlarge-to-fit "*RefTeX Help*" t))
4915 4868
4916;;; =========================================================================== 4869;;; =========================================================================
4917;;; 4870;;;
4918;;; View cross references 4871;;; View cross references
4919 4872
4920(defun reftex-view-crossref (&optional arg) 4873(defun reftex-view-crossref (&optional arg stay-put echo)
4921 "View cross reference of \\ref or \\cite macro at point. 4874 "View cross reference of \\ref or \\cite macro at point.
4922If the macro at point is a \\ref, show the corresponding label definition. 4875If the macro at point is a \\ref, show the corresponding label definition.
4923If it is a \\cite, show the BibTeX database entry or the \\bibitem. 4876If it is a \\cite, show the BibTeX database entry or the \\bibitem.
@@ -4932,107 +4885,130 @@ crossreferences in external documents. However, this works correctly only
4932when the \\externaldocument macros are used with the optional label prefix 4885when the \\externaldocument macros are used with the optional label prefix
4933argument. 4886argument.
4934With one or two C-u prefixes, enforce rescanning of the document. 4887With one or two C-u prefixes, enforce rescanning of the document.
4935With argument t or 1, select the window showing the cross reference." 4888With argument t or 1, select the window showing the cross reference.
4889When STAY-PUT is non-nil, do not move.
4890When ECHO is non-nil, use the echo area to display the cross reference."
4936 4891
4937 (interactive "P") 4892 (interactive "P")
4938 4893
4939 ;; See where we are. 4894 ;; See where we are.
4940 (let* ((re "\\\\\\([a-z]*\\(cite\\|ref\\)\\|\\(cite\\|ref\\)[a-z]*\\)\\**\\(\\[[^{}]*\\]\\)?{") 4895 (let* ((re "\\\\\\([a-z]*\\(cite\\|ref\\)\\|\\(cite\\|ref\\)[a-z]*\\)\\**\\(\\[[^{}]*\\]\\)?{")
4941 (macro (car (car (reftex-what-macro t)))) 4896 (macro (car (reftex-what-macro 1)))
4942 (this-word (reftex-this-word "*a-zA-Z\\\\")) 4897 (this-word (reftex-this-word "*a-zA-Z\\\\"))
4943 (my-window (selected-window)) 4898 (my-window (selected-window))
4944 pop-window cmd args point) 4899 pop-window cmd args point)
4945 4900
4946 (if (and macro 4901 (setq reftex-call-back-to-this-buffer (current-buffer))
4947 (string-match "\\`\\\\cite\\|\\`\\\\ref\\|cite\\'\\|ref\\'" 4902
4948 macro)) 4903 (catch 'exit
4949 (and (setq macro (match-string 0 macro)) 4904 (if (and macro
4950 (string-match "\\`\\\\" macro) 4905 (string-match "\\`\\\\cite\\|\\`\\\\ref\\|cite\\'\\|ref\\'"
4951 (setq macro (substring macro 1))) 4906 macro))
4952 (setq macro nil)) 4907 (and (setq macro (match-string 0 macro))
4953 4908 (string-match "\\`\\\\" macro)
4954 (if (and macro 4909 (setq macro (substring macro 1)))
4955 (eq last-command this-command) 4910 (setq macro nil))
4956 (eq last-command 'reftex-view-crossref)) 4911
4957 (if (string= macro "cite") 4912 ;; Exit here if we have no macro and are not allowed to move.
4958 (progn 4913 (if (and stay-put (not macro)) (throw 'exit nil))
4959 (skip-chars-forward "^},%") 4914
4960 (while (and (eq (following-char) ?%) 4915 (if (and macro
4961 (or (beginning-of-line 2) t) 4916 (eq last-command this-command)
4962 (skip-chars-forward " \t\n\r"))) 4917 (eq last-command 'reftex-view-crossref))
4963 (skip-chars-forward ",") 4918 (if (string= macro "cite")
4964 (if (eq (following-char) ?}) 4919 (progn
4965 (setq macro nil))) 4920 (skip-chars-forward "^},%")
4966 (setq macro nil))) 4921 (while (and (eq (following-char) ?%)
4967 4922 (or (beginning-of-line 2) t)
4968 (if (and (not macro) 4923 (skip-chars-forward " \t\n\r")))
4969 (or (not (string-match "\\`\\\\" this-word)) 4924 (skip-chars-forward ",")
4970 (eq (following-char) ?\\) 4925 (if (eq (following-char) ?})
4971 (search-backward "\\" nil t) 4926 (setq macro nil)))
4972 t)) 4927 (setq macro nil)))
4973 (if (interactive-p) 4928
4974 ;; Only move far if this function was called directly 4929 (if (and (not macro)
4975 (and (re-search-forward re nil t) 4930 (or (not (string-match "\\`\\\\" this-word))
4976 (setq macro (or (match-string 2) (match-string 3)))) 4931 (eq (following-char) ?\\)
4977 ;; The macro needs to be at point 4932 (search-backward "\\" nil t)
4978 (and (looking-at re) 4933 t))
4979 (setq macro (or (match-string 2) (match-string 3))) 4934 (if (interactive-p)
4980 (goto-char (match-end 0))))) 4935 ;; Only move far if this function was called directly
4936 (and (re-search-forward re nil t)
4937 (setq macro (or (match-string 2) (match-string 3))))
4938 ;; The macro needs to be at point
4939 (and (looking-at re)
4940 (setq macro (or (match-string 2) (match-string 3)))
4941 (goto-char (match-end 0)))))
4981 4942
4982 4943
4983 (unless macro 4944 (unless macro
4984 (error "No cross reference to display")) 4945 (error "No cross reference to display"))
4985 4946
4986 ;; Ensure access to scanning info 4947 ;; Ensure access to scanning info
4987 (reftex-access-scan-info current-prefix-arg) 4948 (reftex-access-scan-info (or arg current-prefix-arg))
4988 4949
4989 (cond
4990 ((string= macro "cite")
4991 (cond 4950 (cond
4992 ((assq 'bib (symbol-value reftex-docstruct-symbol)) 4951 ((reftex-in-comment)
4993 (setq cmd 'reftex-pop-to-bibtex-entry 4952 (throw 'exit nil))
4994 args (list 4953 ((string= macro "cite")
4995 (reftex-this-word "^{},%\n\r") 4954 (cond
4996 (reftex-get-bibfile-list) nil t))) 4955 ((assq 'bib (symbol-value reftex-docstruct-symbol))
4997 ((assq 'thebib (symbol-value reftex-docstruct-symbol)) 4956 (setq cmd 'reftex-pop-to-bibtex-entry
4998 (setq cmd 'reftex-pop-to-bibtex-entry 4957 args (list
4999 args (list 4958 (reftex-this-word "^{},%\n\r")
5000 (reftex-this-word "^{},%\n\r") 4959 (reftex-get-bibfile-list) nil t nil)))
5001 (list (cdr (assq 'thebib 4960 ((assq 'thebib (symbol-value reftex-docstruct-symbol))
5002 (symbol-value reftex-docstruct-symbol)))) 4961 (setq cmd 'reftex-pop-to-bibtex-entry
5003 nil t t))) 4962 args (list
5004 (t (error "Cannot display crossref\n")))) 4963 (reftex-this-word "^{},%\n\r")
5005 ((string= macro "ref") 4964 (list (cdr (assq 'thebib
5006 (let* ((label (reftex-this-word "^{}%\n\r")) 4965 (symbol-value reftex-docstruct-symbol))))
5007 (xr-data (assoc 'xr (symbol-value reftex-docstruct-symbol))) 4966 nil t t)))
5008 (xr-re (nth 2 xr-data)) 4967 (reftex-default-bibliography
5009 (entry (assoc label (symbol-value reftex-docstruct-symbol)))) 4968 (setq cmd 'reftex-pop-to-bibtex-entry
5010 4969 args (list
5011 (if (and (not entry) (string-match xr-re label)) 4970 (reftex-this-word "^{},%\n\r")
5012 ;; Label is defined in external document 4971 reftex-default-bibliography nil t nil)))
5013 (save-excursion 4972 (echo)
5014 (save-match-data 4973 (t (error "Cannot display crossref\n")))
5015 (set-buffer 4974 (when echo
5016 (or (reftex-get-file-buffer-force 4975 (reftex-echo-cite args)
5017 (cdr (assoc (match-string 1 label) (nth 1 xr-data)))) 4976 (throw 'exit nil)))
5018 (error "Problem with external label %s" label)))) 4977 ((string= macro "ref")
5019 (setq label (substring label (match-end 1))) 4978 (let* ((label (reftex-this-word "^{}%\n\r"))
5020 (reftex-access-scan-info) 4979 (xr-data (assoc 'xr (symbol-value reftex-docstruct-symbol)))
5021 (setq entry 4980 (xr-re (nth 2 xr-data))
5022 (assoc label (symbol-value reftex-docstruct-symbol))))) 4981 (entry (assoc label (symbol-value reftex-docstruct-symbol))))
5023 (if entry 4982
5024 (setq cmd 'reftex-pop-to-label 4983 (if (and (not entry) (stringp label) xr-re (string-match xr-re label))
5025 args (list label (list (nth 3 entry)) nil t)) 4984 ;; Label is defined in external document
5026 (error "Label %s not known - reparse document might help" label)))) 4985 (save-excursion
5027 (t (error "This should not happen (reftex-view-crossref)"))) 4986 (save-match-data
5028 (setq point (point)) 4987 (set-buffer
5029 (apply cmd args) 4988 (or (reftex-get-file-buffer-force
5030 (setq pop-window (selected-window)) 4989 (cdr (assoc (match-string 1 label) (nth 1
5031 (add-hook 'pre-command-hook 'reftex-highlight-shall-die) 4990 xr-data))))
5032 (select-window my-window) 4991 (error "Problem with external label %s" label))))
5033 (goto-char point) 4992 (setq label (substring label (match-end 1)))
5034 (when (or (equal arg t) (equal arg 1)) 4993 (reftex-access-scan-info)
5035 (select-window pop-window)))) 4994 (setq entry
4995 (assoc label (symbol-value reftex-docstruct-symbol)))))
4996 (when echo
4997 (reftex-echo-ref label entry (symbol-value reftex-docstruct-symbol))
4998 (throw 'exit t))
4999 (if entry
5000 (setq cmd 'reftex-pop-to-label
5001 args (list label (list (nth 3 entry)) nil t))
5002 (error "Label %s not known - reparse document might help" label))))
5003 (t (error "This should not happen (reftex-view-crossref)")))
5004 (setq point (point))
5005 (apply cmd args)
5006 (setq pop-window (selected-window))
5007 (add-hook 'pre-command-hook 'reftex-highlight-shall-die)
5008 (select-window my-window)
5009 (goto-char point)
5010 (when (or (equal arg t) (equal arg 1))
5011 (select-window pop-window)))))
5036 5012
5037(defun reftex-mouse-view-crossref (ev) 5013(defun reftex-mouse-view-crossref (ev)
5038 "View cross reference of \\ref or \\cite macro where you click. 5014 "View cross reference of \\ref or \\cite macro where you click.
@@ -5044,7 +5020,143 @@ With argument, actually select the window showing the cross reference."
5044 (mouse-set-point ev) 5020 (mouse-set-point ev)
5045 (reftex-view-crossref current-prefix-arg)) 5021 (reftex-view-crossref current-prefix-arg))
5046 5022
5047;;; =========================================================================== 5023(defvar reftex-auto-view-crossref-timer nil
5024 "The timer used for auto-view-crossref.")
5025
5026(defun reftex-view-crossref-when-idle ()
5027 ;; Display info about crossref at point in echo area.
5028 ;; This function was desigend to work with an idle timer.
5029 ;; We try to get out of here as quickly as possible if the call is useless.
5030 (and reftex-mode
5031 ;; Quick precheck if this might be a relevant spot
5032 (save-excursion
5033 (search-backward "\\" nil t)
5034 (looking-at "\\\\[a-zA-Z]*\\(cite\\|ref\\)"))
5035 ;; Make sure message area is free
5036 (not (current-message))
5037 (condition-case nil
5038 (let ((current-prefix-arg nil))
5039 (reftex-view-crossref -1 t t))
5040 (error nil))))
5041
5042(defun reftex-echo-ref (label entry docstruct)
5043 ;; Display crossref info in echo area.
5044 (cond
5045 ((null docstruct)
5046 (message (substitute-command-keys (format reftex-no-info-message "ref"))))
5047 ((null entry)
5048 (message "ref: unknown label: %s" label))
5049 (t
5050 (when (stringp (nth 2 entry))
5051 (message "ref(%s): %s" (nth 1 entry) (nth 2 entry)))
5052 (let ((buf (get-buffer " *Echo Area*")))
5053 (when buf
5054 (save-excursion
5055 (set-buffer buf)
5056 (run-hooks 'reftex-display-copied-context-hook)))))))
5057
5058(defun reftex-echo-cite (args)
5059 ;; Display citation info in echo area.
5060 (let* ((key (car args))
5061 (cache (assq 'bibview-cache (symbol-value reftex-docstruct-symbol)))
5062 (cache-entry (assoc key (cdr cache)))
5063 (item (nth 4 args))
5064 (files (nth 1 args))
5065 entry string buf)
5066
5067 (if (and reftex-cache-cite-echo cache-entry)
5068 ;; We can just use the cache
5069 (setq string (cdr cache-entry))
5070
5071 ;; Need to look in the database
5072 (unless reftex-revisit-to-echo
5073 (setq files (reftex-visited-files (nth 1 args))))
5074
5075 (setq entry
5076 (condition-case nil
5077 (save-excursion
5078 (reftex-pop-to-bibtex-entry key files nil nil item t))
5079 (error
5080 (if (and files (= (length (nth 1 args)) (length files)))
5081 (message "cite: no such database entry: %s" key)
5082 (message (substitute-command-keys
5083 (format reftex-no-info-message "cite"))))
5084 nil)))
5085 (when entry
5086 (setq entry (if item (reftex-parse-bibitem entry)
5087 (reftex-parse-bibtex-entry entry)))
5088 (setq string (reftex-make-cite-echo-string
5089 entry reftex-docstruct-symbol))))
5090 (unless (or (null string) (equal string ""))
5091 (message "cite: %s" string))
5092 (when (setq buf (get-buffer " *Echo Area*"))
5093 (save-excursion
5094 (set-buffer buf)
5095 (run-hooks 'reftex-display-copied-context-hook)))))
5096
5097(defun reftex-make-cite-echo-string (entry docstruct-symbol)
5098 ;; Format a bibtex entry for the echo area and cache the result.
5099 (let* ((key (reftex-get-bib-field "&key" entry))
5100 (string
5101 (let* ((reftex-cite-punctuation '(" " " & " " etal.")))
5102 (reftex-format-citation entry reftex-cite-view-format)))
5103 (cache (assq 'bibview-cache (symbol-value docstruct-symbol)))
5104 (cache-entry (assoc key (cdr cache))))
5105 (unless cache
5106 ;; This docstruct has no cache - make one.
5107 (set docstruct-symbol (cons (cons 'bibview-cache nil)
5108 (symbol-value docstruct-symbol))))
5109 (when reftex-cache-cite-echo
5110 (setq key (copy-sequence key))
5111 (set-text-properties 0 (length key) nil key)
5112 (set-text-properties 0 (length string) nil string)
5113 (if cache-entry
5114 (unless (string= (cdr cache-entry) string)
5115 (setcdr cache-entry string)
5116 (put reftex-docstruct-symbol 'modified t))
5117 (push (cons key string) (cdr cache))
5118 (put reftex-docstruct-symbol 'modified t)))
5119 string))
5120
5121(defvar reftex-use-itimer-in-xemacs nil
5122 "*Non-nil means use the idle timers in XEmacs for crossref display.
5123Currently, idle timer restart is broken and we use the post-command-hook.")
5124
5125(defun reftex-toggle-auto-view-crossref ()
5126 "Toggle the automatic display of crossref information in the echo area.
5127When active, leaving point idle in the argument of a \\ref or \\cite macro
5128will display info in the echo area."
5129 (interactive)
5130 (if reftex-auto-view-crossref-timer
5131 (progn
5132 (if (featurep 'xemacs)
5133 (if reftex-use-itimer-in-xemacs
5134 (delete-itimer reftex-auto-view-crossref-timer)
5135 (remove-hook 'post-command-hook 'reftex-start-itimer-once))
5136 (cancel-timer reftex-auto-view-crossref-timer))
5137 (setq reftex-auto-view-crossref-timer nil)
5138 (message "Automatic display of crossref information was turned off"))
5139 (setq reftex-auto-view-crossref-timer
5140 (if (featurep 'xemacs)
5141 (if reftex-use-itimer-in-xemacs
5142 (start-itimer "RefTeX Idle Timer"
5143 'reftex-view-crossref-when-idle
5144 reftex-idle-time reftex-idle-time t)
5145 (add-hook 'post-command-hook 'reftex-start-itimer-once)
5146 nil)
5147 (run-with-idle-timer
5148 reftex-idle-time t 'reftex-view-crossref-when-idle)))
5149 (message "Automatic display of crossref information was turned on")))
5150
5151(defun reftex-start-itimer-once ()
5152 (and reftex-mode
5153 (not (itimer-live-p reftex-auto-view-crossref-timer))
5154 (setq reftex-auto-view-crossref-timer
5155 (start-itimer "RefTeX Idle Timer"
5156 'reftex-view-crossref-when-idle
5157 reftex-idle-time nil t))))
5158
5159;;; =========================================================================
5048;;; 5160;;;
5049;;; Functions that check out the surroundings 5161;;; Functions that check out the surroundings
5050 5162
@@ -5053,6 +5165,7 @@ With argument, actually select the window showing the cross reference."
5053 ;; The return value is either ("\\macro" . (point)) or a list of them. 5165 ;; The return value is either ("\\macro" . (point)) or a list of them.
5054 5166
5055 ;; If WHICH is nil, immediately return nil. 5167 ;; If WHICH is nil, immediately return nil.
5168 ;; If WHICH is 1, return innermost enclosing macro.
5056 ;; If WHICH is t, return list of all macros enclosing point. 5169 ;; If WHICH is t, return list of all macros enclosing point.
5057 ;; If WHICH is a list of macros, look only for those macros and return the 5170 ;; If WHICH is a list of macros, look only for those macros and return the
5058 ;; name of the first macro in this list found to enclose point. 5171 ;; name of the first macro in this list found to enclose point.
@@ -5065,7 +5178,7 @@ With argument, actually select the window showing the cross reference."
5065 ;; argument. As RefTeX cannot know this, the string "bb" would still be 5178 ;; argument. As RefTeX cannot know this, the string "bb" would still be
5066 ;; considered an argument of macro \macro. 5179 ;; considered an argument of macro \macro.
5067 5180
5068 (unless reftex-section-regexp (reftex-compute-ref-cite-tables)) 5181 (unless reftex-section-regexp (reftex-compile-variables))
5069 (catch 'exit 5182 (catch 'exit
5070 (if (null which) (throw 'exit nil)) 5183 (if (null which) (throw 'exit nil))
5071 (let ((bound (or bound (save-excursion (re-search-backward 5184 (let ((bound (or bound (save-excursion (re-search-backward
@@ -5103,7 +5216,7 @@ With argument, actually select the window showing the cross reference."
5103 ((null cmd)) 5216 ((null cmd))
5104 ((eq t which) 5217 ((eq t which)
5105 (push (cons cmd (point)) cmd-list)) 5218 (push (cons cmd (point)) cmd-list))
5106 ((member cmd which) 5219 ((or (eq 1 which) (member cmd which))
5107 (throw 'exit (cons cmd (point)))))) 5220 (throw 'exit (cons cmd (point))))))
5108 (goto-char pos))) 5221 (goto-char pos)))
5109 (nreverse cmd-list))))) 5222 (nreverse cmd-list)))))
@@ -5114,6 +5227,7 @@ With argument, actually select the window showing the cross reference."
5114 ;; them. 5227 ;; them.
5115 5228
5116 ;; If WHICH is nil, immediately return nil. 5229 ;; If WHICH is nil, immediately return nil.
5230 ;; If WHICH is 1, return innermost enclosing environment.
5117 ;; If WHICH is t, return list of all environments enclosing point. 5231 ;; If WHICH is t, return list of all environments enclosing point.
5118 ;; If WHICH is a list of environments, look only for those environments and 5232 ;; If WHICH is a list of environments, look only for those environments and
5119 ;; return the name of the first environment in this list found to enclose 5233 ;; return the name of the first environment in this list found to enclose
@@ -5140,13 +5254,158 @@ With argument, actually select the window showing the cross reference."
5140 (setq end-list (delete env end-list))) 5254 (setq end-list (delete env end-list)))
5141 ((eq t which) 5255 ((eq t which)
5142 (push (cons env (point)) env-list)) 5256 (push (cons env (point)) env-list))
5143 ((member env which) 5257 ((or (eq 1 which) (member env which))
5144 (throw 'exit (cons env (point)))))) 5258 (throw 'exit (cons env (point))))))
5145 (nreverse env-list))))) 5259 (nreverse env-list)))))
5146 5260
5147;; ============================================================================ 5261;;; =========================================================================
5148;; 5262;;;
5149;; Some generally useful functions 5263;;; Finding files
5264
5265(defun reftex-find-tex-file (file master-dir &optional die)
5266 ;; Find FILE in MASTER-DIR or on reftex-tex-path.
5267 ;; FILE may be given with or without the .tex extension.
5268 (let ((rec-values '(nil t)) path file1 old-path)
5269 (unless (and (file-name-absolute-p file) (not (file-exists-p file)))
5270 (while (and (null file1) rec-values)
5271 (setq path (reftex-access-search-path
5272 "tex" (pop rec-values) master-dir file))
5273 (if (or (null old-path)
5274 (not (eq old-path path)))
5275 (setq old-path path
5276 path (cons master-dir path)
5277 file1 (or (reftex-find-file-on-path
5278 (concat file ".tex") path master-dir)
5279 (reftex-find-file-on-path file path master-dir))))))
5280 (cond (file1 file1)
5281 (die (error "No such file: %s" file) nil)
5282 (t (message "No such file: %s (ignored)" file) nil))))
5283
5284(defun reftex-find-bib-file (file master-dir &optional die)
5285 ;; Find FILE in MASTER-DIR or on reftex-bib-path
5286 (let ((rec-values '(nil t)) path file1 old-path)
5287 (unless (and (file-name-absolute-p file) (not (file-exists-p file)))
5288 (while (and (null file1) rec-values)
5289 (setq path (reftex-access-search-path
5290 "bib" (pop rec-values) master-dir file))
5291 (if (or (null old-path)
5292 (not (eq old-path path)))
5293 (setq old-path path
5294 path (cons master-dir path)
5295 file1 (reftex-find-file-on-path file path master-dir)))))
5296 (cond (file1 file1)
5297 (die (error "No such file: %s" file) nil)
5298 (t (message "No such file: %s (ignored)" file) nil))))
5299
5300(defun reftex-access-search-path (which &optional recurse master-dir file)
5301 ;; Access path from environment variables. WHICH is either "tex" or "bib".
5302 ;; When RECURSE is t, expand path elements ending in `//' recursively.
5303 ;; Relative path elements are left as they are. However, relative recursive
5304 ;; elements are expanded with MASTER-DIR as default directory.
5305 ;; The expanded path is cached for the next search.
5306 ;; FILE is just for the progress message.
5307 ;; Returns the derived path.
5308 (let* ((pathvar (intern (concat "reftex-" which "-path"))))
5309 (when (null (get pathvar 'status))
5310 ;; Get basic path from environment
5311 (let ((env-vars (if (equal which "tex") (list "TEXINPUTS")
5312 reftex-bibpath-environment-variables)))
5313 (set pathvar (reftex-parse-colon-path
5314 (mapconcat (lambda(x) (or (getenv x) ""))
5315 env-vars path-separator))))
5316 (put pathvar 'status 'split)
5317 ;; Check if we have recursive elements
5318 (let ((path (symbol-value pathvar)) dir rec)
5319 (while (setq dir (pop path))
5320 (when (string= (substring dir -2) "//")
5321 (if (file-name-absolute-p dir)
5322 (setq rec (or rec 'absolute))
5323 (setq rec 'relative))))
5324 (put pathvar 'rec-type rec)))
5325
5326 (if recurse
5327 ;; Return the recursive expansion of the path
5328 (cond
5329 ((not (get pathvar 'rec-type))
5330 ;; Path does not contain recursive elements - use simple path
5331 (symbol-value pathvar))
5332 ((or (not (get pathvar 'recursive-path))
5333 (and (eq (get pathvar 'rec-type) 'relative)
5334 (not (equal master-dir (get pathvar 'master-dir)))))
5335 ;; Either: We don't have a recursive expansion yet.
5336 ;; or: Relative recursive path elements need to be expanded
5337 ;; relative to new default directory
5338 (message "Expanding search path to find %s file: %s ..." which file)
5339 (put pathvar 'recursive-path
5340 (reftex-expand-path (symbol-value pathvar) master-dir))
5341 (put pathvar 'master-dir master-dir)
5342 (get pathvar 'recursive-path))
5343 (t
5344 ;; Recursive path computed earlier is still OK.
5345 (get pathvar 'recursive-path)))
5346 ;; The simple path was requested
5347 (symbol-value pathvar))))
5348
5349(defun reftex-find-file-on-path (file path &optional def-dir)
5350 ;; Find FILE along the directory list PATH.
5351 ;; DEF-DIR is the default directory for expanding relative path elements.
5352 (catch 'exit
5353 (when (file-name-absolute-p file)
5354 (if (and (file-exists-p file) (not (file-directory-p file)))
5355 (throw 'exit file)
5356 (throw 'exit nil)))
5357 (let* ((thepath path) file1 dir )
5358 (while (setq dir (pop thepath))
5359 (when (string= (substring dir -2) "//")
5360 (setq dir (substring dir 0 -1)))
5361 (setq file1 (expand-file-name file (expand-file-name dir def-dir)))
5362 (if (and (file-exists-p file1) (not (file-directory-p file1)))
5363 (throw 'exit file1)))
5364 ;; No such file
5365 nil)))
5366
5367(defun reftex-parse-colon-path (path)
5368 ;; Like parse-colon-parse, but // or /~ are left alone.
5369 ;; Trailing ! or !! will be converted into `//' (emTeX convention)
5370 (mapcar
5371 (lambda (dir)
5372 (if (string-match "\\(//+\\|/*!+\\)\\'" dir)
5373 (setq dir (replace-match "//" t t dir)))
5374 (file-name-as-directory dir))
5375 (delete "" (split-string path (concat path-separator "+")))))
5376
5377(defun reftex-expand-path (path &optional default-dir)
5378 ;; Expand parts of path ending in `//' recursively into directory list.
5379 ;; Relative recursive path elements are expanded relative to DEFAULT-DIR.
5380 (let (path1 dir recursive)
5381 (while (setq dir (pop path))
5382 (if (setq recursive (string= (substring dir -2) "//"))
5383 (setq dir (substring dir 0 -1)))
5384 (if (and recursive
5385 (not (file-name-absolute-p dir)))
5386 (setq dir (expand-file-name dir default-dir)))
5387 (if recursive
5388 ;; Expand recursively
5389 (setq path1 (append (reftex-recursive-directory-list dir) path1))
5390 ;; Keep unchanged
5391 (push dir path1)))
5392 (nreverse path1)))
5393
5394(defun reftex-recursive-directory-list (dir)
5395 ;; Return a list of all directories below DIR, including DIR itself
5396 (let ((path (list dir)) path1 file files)
5397 (while (setq dir (pop path))
5398 (when (file-directory-p dir)
5399 (setq files (nreverse (directory-files dir t "[^.]")))
5400 (while (setq file (pop files))
5401 (if (file-directory-p file)
5402 (push (file-name-as-directory file) path)))
5403 (push dir path1)))
5404 path1))
5405
5406;;; =========================================================================
5407;;;
5408;;; Some generally useful functions
5150 5409
5151(defun reftex-no-props (string) 5410(defun reftex-no-props (string)
5152 ;; Return STRING with all text properties removed 5411 ;; Return STRING with all text properties removed
@@ -5185,15 +5444,48 @@ With argument, actually select the window showing the cross reference."
5185 (progn (skip-chars-backward class) (point)) 5444 (progn (skip-chars-backward class) (point))
5186 (progn (skip-chars-forward class) (point))))) 5445 (progn (skip-chars-forward class) (point)))))
5187 5446
5447(defun reftex-all-assq (key list)
5448 ;; Return a list of all associations of KEY in LIST. Comparison with eq.
5449 (let (rtn)
5450 (while (setq list (memq (assq key list) list))
5451 (push (car list) rtn)
5452 (pop list))
5453 (nreverse rtn)))
5454
5455(defun reftex-all-assoc-string (key list)
5456 ;; Return a list of all associations of KEY in LIST. Comparison with string=.
5457 (let (rtn)
5458 (while list
5459 (if (string= (car (car list)) key)
5460 (push (car list) rtn))
5461 (pop list))
5462 (nreverse rtn)))
5463
5464(defun reftex-last-assoc-before-elt (key elt list)
5465 ;; Find the last association of KEY in LIST before or at ELT
5466 ;; ELT is found in LIST with equal, not eq.
5467 ;; Returns nil when either KEY or elt are not found in LIST.
5468 ;; On success, returns the association.
5469 (let* ((elt (car (member elt list))) ass last-ass)
5470
5471 (while (and (setq ass (assoc key list))
5472 (setq list (memq ass list))
5473 (memq elt list))
5474 (setq last-ass ass
5475 list (cdr list)))
5476 last-ass))
5477
5188(defvar enable-multibyte-characters) 5478(defvar enable-multibyte-characters)
5189(defun reftex-truncate (string ncols &optional ellipses padding) 5479(defun reftex-truncate (string ncols &optional ellipses padding)
5190 ;; Truncate a string to NCHAR characters. 5480 ;; Truncate a string to NCHAR characters.
5191 ;; Works fast with ASCII and correctly with Mule characters. 5481 ;; Works fast with ASCII and correctly with Mule characters.
5192 ;; When ELLIPSES is non-nil, put three dots at the end of the string. 5482 ;; When ELLIPSES is non-nil, put three dots at the end of the string.
5483 ;; When padding is non-nil, fills with white space to NCOLS characters.
5193 (setq string 5484 (setq string
5194 (cond 5485 (cond
5195 ((and (boundp 'enable-multibyte-characters) 5486 ((and (boundp 'enable-multibyte-characters)
5196 enable-multibyte-characters) 5487 enable-multibyte-characters
5488 (fboundp 'truncate-string-to-width))
5197 (if (<= (string-width string) ncols) 5489 (if (<= (string-width string) ncols)
5198 string 5490 string
5199 (if ellipses 5491 (if ellipses
@@ -5241,7 +5533,7 @@ With argument, actually select the window showing the cross reference."
5241 (nreverse out))) 5533 (nreverse out)))
5242 5534
5243(defun reftex-enlarge-to-fit (buf2 &optional keep-current) 5535(defun reftex-enlarge-to-fit (buf2 &optional keep-current)
5244 ;; Enlarge other window displaying buffer without killing current window 5536 ;; Enlarge other window displaying buffer to show whole buffer if possible.
5245 ;; If KEEP-CURRENT in non-nil, current buffer must remain visible. 5537 ;; If KEEP-CURRENT in non-nil, current buffer must remain visible.
5246 (let* ((win1 (selected-window)) 5538 (let* ((win1 (selected-window))
5247 (buf1 (current-buffer)) 5539 (buf1 (current-buffer))
@@ -5259,78 +5551,35 @@ With argument, actually select the window showing the cross reference."
5259 (switch-to-buffer-other-window buf1) 5551 (switch-to-buffer-other-window buf1)
5260 (shrink-window (- (window-height) window-min-height)))))) 5552 (shrink-window (- (window-height) window-min-height))))))
5261 5553
5262(defun reftex-access-search-path (which &optional recurse file) 5554(defun reftex-select-with-char (prompt help-string &optional delay-time scroll)
5263 ;; Access path from environment variables. WHICH is either "tex" or "bib". 5555 ;; Offer to select something with PROMPT and, after DELAY-TIME seconds,
5264 ;; When RECURSE is t, expand recursive paths, ending in double slash. 5556 ;; also with HELP-STRING.
5265 ;; FILE is just for the message. 5557 ;; When SCROLL is non-nil, use SPC and DEL to scroll help window.
5266 (let* ((pathvar (intern (concat "reftex-" which "-path"))) 5558 (let ((char ?\?))
5267 (status (get pathvar 'status))) 5559 (save-window-excursion
5268 (cond 5560 (catch 'exit
5269 ((eq status 'recursed)) 5561 (message (concat prompt " (?=Help)"))
5270 ((and status (null recurse))) 5562 (when (or (sit-for (or delay-time 0))
5271 ((null status) 5563 (= ?\? (setq char (read-char-exclusive))))
5272 (let ((env-vars (if (equal which "tex") (list "TEXINPUTS") 5564 (with-output-to-temp-buffer " *RefTeX Help*"
5273 reftex-bibpath-environment-variables))) 5565 (princ help-string))
5274 (set pathvar (reftex-parse-colon-path 5566 (reftex-enlarge-to-fit " *RefTeX Help*")
5275 (mapconcat (function (lambda(x) (or (getenv x) ""))) 5567 (select-window (get-buffer-window " *RefTeX Help*"))
5276 env-vars path-separator)))) 5568 (setq truncate-lines t))
5277 (put pathvar 'status 'split)) 5569 (setq prompt (concat prompt (if scroll " (SPC/DEL=Scroll)" "")))
5278 ((and (eq 'split status) recurse) 5570 (message prompt)
5279 (message "Expanding search path to find %s file: %s ..." which file) 5571 (and (equal char ?\?) (setq char (read-char-exclusive)))
5280 (set pathvar (reftex-expand-path (symbol-value pathvar))) 5572 (while t
5281 (put pathvar 'status 'recursed))))) 5573 (cond ((equal char ?\C-g) (keyboard-quit))
5282 5574 ((equal char ?\?))
5283(defun reftex-find-file-on-path (file path) 5575 ((and scroll (equal char ?\ ))
5284 ;; Find FILE along the directory list PATH. 5576 (condition-case nil (scroll-up) (error nil))
5285 (catch 'exit 5577 (message prompt))
5286 (when (file-name-absolute-p file) 5578 ((and scroll (equal char ?\C-? ))
5287 (if (file-exists-p file) 5579 (condition-case nil (scroll-down) (error nil))
5288 (throw 'exit file) 5580 (message prompt))
5289 (throw 'exit nil))) 5581 (t (throw 'exit char)))
5290 (let* ((thepath path) file1 dir 5582 (setq char (read-char-exclusive)))))))
5291 (doubleslash (concat "/" "/")))
5292 (while (setq dir (pop thepath))
5293 (when (string= (substring dir -2) doubleslash)
5294 (setq dir (substring dir 0 -1)))
5295 (setq file1 (expand-file-name file (expand-file-name dir)))
5296 (if (file-exists-p file1)
5297 (throw 'exit file1)))
5298 ;; No such file
5299 nil)))
5300
5301(defun reftex-parse-colon-path (path)
5302 ;; Like parse-colon-parse, but // or /~ have no effects.
5303 (mapcar 'file-name-as-directory
5304 (delete "" (split-string path (concat path-separator "+")))))
5305
5306(defun reftex-expand-path (path)
5307 ;; Expand parts of path ending in a double slash
5308 (let (path1 dir dirs (doubleslash (concat "/" "/")))
5309 (while (setq dir (pop path))
5310 (if (string= (substring dir -2) doubleslash)
5311 (progn
5312 (setq dir (substring dir 0 -1))
5313 (setq dirs (reftex-recursive-directory-list dir))
5314 (setq path1 (append dirs path1)))
5315 (push dir path1)))
5316 (nreverse path1)))
5317
5318(defun reftex-recursive-directory-list (dir)
5319 (let ((path (list dir)) dirs path1)
5320 (while (setq dir (pop path))
5321 (setq dirs
5322 (delete nil
5323 (mapcar
5324 (function
5325 (lambda (x)
5326 (if (and (file-directory-p x)
5327 (not (string-match "/\\.+\\'" x)))
5328 (file-name-as-directory x)
5329 nil)))
5330 (directory-files dir t))))
5331 (setq path (append dirs path))
5332 (push dir path1))
5333 path1))
5334 5583
5335(defun reftex-make-regexp-allow-for-ctrl-m (string) 5584(defun reftex-make-regexp-allow-for-ctrl-m (string)
5336 ;; convert STRING into a regexp, allowing ^M for \n and vice versa 5585 ;; convert STRING into a regexp, allowing ^M for \n and vice versa
@@ -5340,29 +5589,6 @@ With argument, actually select the window showing the cross reference."
5340 (setq string (replace-match "[\n\r]" nil t string))) 5589 (setq string (replace-match "[\n\r]" nil t string)))
5341 string)) 5590 string))
5342 5591
5343(defun reftex-make-desperate-section-regexp (old)
5344 ;; Return a regexp which will still match a section statement even if
5345 ;; x-symbol or isotex or the like have been at work in the mean time.
5346 (let* ((n (1+ (string-match "[[{]" old)))
5347 (new (regexp-quote (substring old 0 (1+ (string-match "[[{]" old)))))
5348 (old (substring old n)))
5349 (while (string-match
5350 "\\([\r\n]\\)\\|\\(\\`\\|[ \t\n\r]\\)\\([a-zA-Z0-9]+\\)\\([ \t\n\r]\\|}\\'\\)"
5351 old)
5352 (if (match-beginning 1)
5353 (setq new (concat new "[^\n\r]*[\n\r]"))
5354 (setq new (concat new "[^\n\r]*" (match-string 3 old))))
5355 (setq old (substring old (match-end 0))))
5356 new))
5357
5358(defun reftex-delete-list (elt-list list)
5359 ;; like delete, but with a list of things to delete
5360 ;; (original code from Rory Molinari)
5361 (while elt-list
5362 (setq list (delete (car elt-list) list)
5363 elt-list (cdr elt-list)))
5364 list)
5365
5366(defun reftex-get-buffer-visiting (file) 5592(defun reftex-get-buffer-visiting (file)
5367 ;; return a buffer visiting FILE 5593 ;; return a buffer visiting FILE
5368 (cond 5594 (cond
@@ -5373,10 +5599,22 @@ With argument, actually select the window showing the cross reference."
5373 (find-buffer-visiting file)) 5599 (find-buffer-visiting file))
5374 (t (error "This should not happen (reftex-get-buffer-visiting)")))) 5600 (t (error "This should not happen (reftex-get-buffer-visiting)"))))
5375 5601
5602;; Define `current-message' for compatibility with XEmacs prior to 20.4
5603(defvar message-stack)
5604(if (and (featurep 'xemacs)
5605 (not (fboundp 'current-message)))
5606 (defun current-message (&optional frame)
5607 (cdr (car message-stack))))
5608
5609(defun reftex-visited-files (list)
5610 ;; Takes a list of filenames and returns the buffers of those already visited
5611 (delq nil (mapcar (lambda (x) (if (reftex-get-buffer-visiting x) x nil))
5612 list)))
5613
5376(defun reftex-get-file-buffer-force (file &optional mark-to-kill) 5614(defun reftex-get-file-buffer-force (file &optional mark-to-kill)
5377 ;; Return a buffer visiting file. Make one, if necessary. 5615 ;; Return a buffer visiting file. Make one, if necessary.
5378 ;; If neither such a buffer nor the file exist, return nil. 5616 ;; If neither such a buffer nor the file exist, return nil.
5379 ;; If MARK-TO-KILL is t and there is no live buffer, load the file with 5617 ;; If MARK-TO-KILL is t and there is no live buffer, visit the file with
5380 ;; initializations according to `reftex-initialize-temporary-buffers', 5618 ;; initializations according to `reftex-initialize-temporary-buffers',
5381 ;; and mark the buffer to be killed after use. 5619 ;; and mark the buffer to be killed after use.
5382 5620
@@ -5422,6 +5660,26 @@ With argument, actually select the window showing the cross reference."
5422 ;; If no such file exists, return nil 5660 ;; If no such file exists, return nil
5423 (t nil)))) 5661 (t nil))))
5424 5662
5663(defun reftex-kill-temporary-buffers (&optional buffer)
5664 ;; Kill all buffers in the list reftex-kill-temporary-buffers.
5665 (cond
5666 (buffer
5667 (when (member buffer reftex-buffers-to-kill)
5668 (kill-buffer buffer)
5669 (setq reftex-buffers-to-kill
5670 (delete buffer reftex-buffers-to-kill))))
5671 (t
5672 (while (setq buffer (pop reftex-buffers-to-kill))
5673 (when (bufferp buffer)
5674 (and (buffer-modified-p buffer)
5675 (y-or-n-p (format "Save file %s? "
5676 (buffer-file-name buffer)))
5677 (save-excursion
5678 (set-buffer buffer)
5679 (save-buffer)))
5680 (kill-buffer buffer))
5681 (pop reftex-buffers-to-kill)))))
5682
5425(defun reftex-splice-symbols-into-list (list alist) 5683(defun reftex-splice-symbols-into-list (list alist)
5426 ;; Splice the association in ALIST of any symbols in LIST into the list. 5684 ;; Splice the association in ALIST of any symbols in LIST into the list.
5427 ;; Return new list. 5685 ;; Return new list.
@@ -5439,7 +5697,7 @@ With argument, actually select the window showing the cross reference."
5439 (push (pop list) rtn)) 5697 (push (pop list) rtn))
5440 (nreverse rtn))) 5698 (nreverse rtn)))
5441 5699
5442(defun reftex-uniquify (alist &optional keep-list) 5700(defun reftex-uniquify-by-car (alist &optional keep-list)
5443 ;; Return a list of all elements in ALIST, but each car only once. 5701 ;; Return a list of all elements in ALIST, but each car only once.
5444 ;; Elements of KEEP-LIST are not removed even if duplicate. 5702 ;; Elements of KEEP-LIST are not removed even if duplicate.
5445 (let (new elm) 5703 (let (new elm)
@@ -5450,6 +5708,10 @@ With argument, actually select the window showing the cross reference."
5450 (push elm new))) 5708 (push elm new)))
5451 (nreverse new))) 5709 (nreverse new)))
5452 5710
5711;;; =========================================================================
5712;;;
5713;;; Fontification and Highlighting
5714
5453(defun reftex-use-fonts () 5715(defun reftex-use-fonts ()
5454 ;; Return t if we can and want to use fonts. 5716 ;; Return t if we can and want to use fonts.
5455 (and window-system 5717 (and window-system
@@ -5461,28 +5723,39 @@ With argument, actually select the window showing the cross reference."
5461 (and (reftex-use-fonts) 5723 (and (reftex-use-fonts)
5462 (or (eq t reftex-refontify-context) 5724 (or (eq t reftex-refontify-context)
5463 (and (eq 1 reftex-refontify-context) 5725 (and (eq 1 reftex-refontify-context)
5464 (or (featurep 'x-symbol)))))) 5726 ;; Test of we use the font-lock version of x-symbol
5465 5727 (and (featurep 'x-symbol-tex) (not (boundp 'x-symbol-mode)))))))
5466(defun reftex-fontify-select-label-buffer () 5728
5467 ;; Fontify the `*RefTeX Select*' buffer 5729(defun reftex-fontify-select-label-buffer (parent-buffer)
5468 (cond 5730 ;; Fontify the `*RefTeX Select*' buffer. Buffer is temporarily renamed to
5469 ((fboundp 'font-lock-default-fontify-region) 5731 ;; start with none-SPC char, beacuse Font-Lock otherwise refuses operation.
5470 ;; Good: we have the indirection functions, and can support lazy-lock etc. 5732 (run-hook-with-args 'reftex-pre-refontification-functions
5471 (set (make-local-variable 'font-lock-fontify-region-function) 5733 parent-buffer 'reftex-ref)
5472 'reftex-select-font-lock-fontify-region) 5734 (let* ((oldname (buffer-name))
5473 (let ((major-mode 'latex-mode)) 5735 (newname (concat "Fontify-me-" oldname)))
5474 (font-lock-mode 1))) 5736 (unwind-protect
5475 ((fboundp 'font-lock-set-defaults-1) 5737 (progn
5476 ;; Looks like the XEmacs font-lock stuff. 5738 ;; Rename buffer temporarily to start w/o space (because of font-lock)
5477 ;; FIXME: this is still kind of a hack, but might go away some day. 5739 (rename-buffer newname t)
5478 (set (make-local-variable 'font-lock-keywords) nil) 5740 (cond
5479 (let ((major-mode 'latex-mode) 5741 ((fboundp 'font-lock-default-fontify-region)
5480 (font-lock-defaults-computed nil)) 5742 ;; Good: we have the indirection functions
5481 (font-lock-set-defaults-1) 5743 (set (make-local-variable 'font-lock-fontify-region-function)
5482 (reftex-select-font-lock-fontify-region (point-min) (point-max)))) 5744 'reftex-select-font-lock-fontify-region)
5483 (t 5745 (let ((major-mode 'latex-mode))
5484 ;; Oops? 5746 (font-lock-mode 1)))
5485 (message "Sorry: cannot refontify RefTeX Select buffer.")))) 5747 ((fboundp 'font-lock-set-defaults-1)
5748 ;; Looks like the XEmacs font-lock stuff.
5749 ;; FIXME: this is still kind of a hack.
5750 (set (make-local-variable 'font-lock-keywords) nil)
5751 (let ((major-mode 'latex-mode)
5752 (font-lock-defaults-computed nil))
5753 (font-lock-set-defaults-1)
5754 (reftex-select-font-lock-fontify-region (point-min) (point-max))))
5755 (t
5756 ;; Oops?
5757 (message "Sorry: cannot refontify RefTeX Select buffer."))))
5758 (rename-buffer oldname))))
5486 5759
5487(defun reftex-select-font-lock-fontify-region (beg end &optional loudly) 5760(defun reftex-select-font-lock-fontify-region (beg end &optional loudly)
5488 ;; Fontify a region, but only lines starting with a dot. 5761 ;; Fontify a region, but only lines starting with a dot.
@@ -5496,13 +5769,24 @@ With argument, actually select the window showing the cross reference."
5496 (funcall func beg1 end1 nil) 5769 (funcall func beg1 end1 nil)
5497 (goto-char end1)))) 5770 (goto-char end1))))
5498 5771
5499;; Highlighting uses overlays. If this is for XEmacs, we need to load 5772(defun reftex-select-font-lock-unfontify (&rest ignore) t)
5500;; the overlay library, available in version 19.15 5773
5501(and (not (fboundp 'make-overlay)) 5774(defun reftex-verified-face (&rest faces)
5502 (condition-case nil 5775 ;; Return the first valid face in FACES, or nil if none is valid.
5503 (require 'overlay) 5776 ;; Also, when finding a nil element in FACES, return nil. This
5504 (error 5777 ;; function is just a safety net to catch name changes of builtin
5505 (error "RefTeX needs overlay emulation (available in XEmacs 19.15)")))) 5778 ;; fonts. Currently it is only used for reftex-label-face, which has
5779 ;; as default font-lock-reference-face, which was recently renamed
5780 ;; to font-lock-constant-face.
5781 (let (face)
5782 (catch 'exit
5783 (while (setq face (pop faces))
5784 (if (featurep 'xemacs)
5785 (if (find-face face) (throw 'exit face))
5786 (if (facep face) (throw 'exit face)))))))
5787
5788;; Highlighting uses overlays. For XEmacs, we need the emulation.
5789(if (featurep 'xemacs) (require 'overlay))
5506 5790
5507;; We keep a vector with several different overlays to do our highlighting. 5791;; We keep a vector with several different overlays to do our highlighting.
5508(defvar reftex-highlight-overlays [nil nil]) 5792(defvar reftex-highlight-overlays [nil nil])
@@ -5529,32 +5813,64 @@ With argument, actually select the window showing the cross reference."
5529 (remove-hook 'pre-command-hook 'reftex-highlight-shall-die) 5813 (remove-hook 'pre-command-hook 'reftex-highlight-shall-die)
5530 (reftex-unhighlight 0)) 5814 (reftex-unhighlight 0))
5531 5815
5532;;; --------------------------------------------------------------------------- 5816;;; =========================================================================
5533;;; 5817;;;
5534;;; Functions to compile the tables, reset the mode etc. 5818;;; Functions to compile the tables, reset the mode etc.
5535 5819
5820;; A list of all variables in the cache.
5821;; The cache is used to save the compiled versions of some variables.
5822(defconst reftex-cache-variables
5823 '(reftex-memory ;; This MUST ALWAYS be the first!
5824 reftex-env-or-mac-alist reftex-everything-regexp
5825 reftex-find-label-regexp-format reftex-find-label-regexp-format2
5826 reftex-label-env-list reftex-label-mac-list
5827 reftex-section-or-include-regexp reftex-section-levels-all
5828 reftex-section-regexp reftex-type-query-help
5829 reftex-type-query-prompt reftex-typekey-list
5830 reftex-typekey-to-format-alist reftex-typekey-to-prefix-alist
5831 reftex-words-to-typekey-alist))
5832
5833(defun reftex-ensure-compiled-variables ()
5834 ;; Recompile the label alist when necessary
5835 (let* ((mem reftex-memory)
5836 (cache (get reftex-docstruct-symbol 'reftex-cache))
5837 (cmem (car cache))
5838 (alist reftex-label-alist)
5839 (levels (get reftex-docstruct-symbol 'reftex-section-levels))
5840 (style (get reftex-docstruct-symbol 'reftex-label-alist-style))
5841 (default reftex-default-label-alist-entries))
5842 (cond
5843 (reftex-tables-dirty (reftex-compile-variables))
5844 ((and (eq alist (nth 0 mem))
5845 (eq levels (nth 1 mem))
5846 (eq style (nth 2 mem))
5847 (eq default (nth 3 mem)))) ;; everything is OK
5848 ((and (eq alist (nth 0 cmem))
5849 (eq levels (nth 1 cmem))
5850 (eq style (nth 2 cmem))
5851 (eq default (nth 2 cmem)))
5852 ;; restore the cache
5853 (message "Restoring cache")
5854 (mapcar (lambda (sym) (set sym (pop cache))) reftex-cache-variables))
5855 (t (reftex-compile-variables)))))
5856
5536(defun reftex-reset-mode () 5857(defun reftex-reset-mode ()
5537 "Reset RefTeX Mode. Required to implement changes to some list variables. 5858 "Reset RefTeX Mode.
5538This function will compile the information in `reftex-label-alist' and similar 5859This will re-compile the configuration information and remove all
5539variables. It is called when RefTeX is first used, and after changes to 5860current scanning information and the parse file to enforce a rescan
5540these variables." 5861on next use."
5541 (interactive) 5862 (interactive)
5542 5863
5543 ;; Record that we have done this
5544 (setq reftex-tables-dirty nil)
5545 (setq reftex-memory
5546 (list reftex-label-alist reftex-label-alist-external-add-ons
5547 reftex-default-label-alist-entries))
5548
5549 ;; Reset the file search path variables 5864 ;; Reset the file search path variables
5550 (put 'reftex-tex-path 'status nil) 5865 (loop for prop in '(status master-dir recursive-path rec-type) do
5551 (put 'reftex-bib-path 'status nil) 5866 (put 'reftex-tex-path prop nil)
5867 (put 'reftex-bib-path prop nil))
5552 5868
5553 ;; Kill temporary buffers associated with RefTeX - just in case they 5869 ;; Kill temporary buffers associated with RefTeX - just in case they
5554 ;; were not cleaned up properly 5870 ;; were not cleaned up properly
5555 (save-excursion 5871 (save-excursion
5556 (let ((buffer-list '("*RefTeX Help*" "*RefTeX Select*" 5872 (let ((buffer-list '("*RefTeX Help*" "*RefTeX Select*"
5557 "*Duplicate Labels*" "*toc*" "*RefTeX-scratch*")) 5873 "*Duplicate Labels*" "*toc*" " *RefTeX-scratch*"))
5558 buf) 5874 buf)
5559 (while (setq buf (pop buffer-list)) 5875 (while (setq buf (pop buffer-list))
5560 (if (get-buffer buf) 5876 (if (get-buffer buf)
@@ -5564,12 +5880,14 @@ these variables."
5564 ;; Make sure the current document will be rescanned soon. 5880 ;; Make sure the current document will be rescanned soon.
5565 (reftex-reset-scanning-information) 5881 (reftex-reset-scanning-information)
5566 5882
5883 ;; Remove any parse info file
5884 (reftex-access-parse-file 'kill)
5885
5567 ;; Plug functions into AUCTeX if the user option says so. 5886 ;; Plug functions into AUCTeX if the user option says so.
5568 (reftex-plug-into-AUCTeX) 5887 (and reftex-plug-into-AUCTeX
5888 (reftex-plug-into-AUCTeX))
5569 5889
5570 (message "updating internal tables...") 5890 (reftex-compile-variables))
5571 (reftex-compute-ref-cite-tables)
5572 (message "updating internal tables... done"))
5573 5891
5574(defun reftex-reset-scanning-information () 5892(defun reftex-reset-scanning-information ()
5575 "Reset the symbols containing information from buffer scanning. 5893 "Reset the symbols containing information from buffer scanning.
@@ -5587,29 +5905,39 @@ This enforces rescanning the buffer on next use."
5587 5905
5588(defun reftex-erase-all-selection-buffers () 5906(defun reftex-erase-all-selection-buffers ()
5589 ;; Remove all selection buffers associated with current document. 5907 ;; Remove all selection buffers associated with current document.
5590 (mapcar (function 5908 (mapcar
5591 (lambda (type) 5909 (lambda (type)
5592 (reftex-erase-buffer (reftex-make-selection-buffer-name type)))) 5910 (reftex-erase-buffer (reftex-make-selection-buffer-name type)))
5593 reftex-typekey-list)) 5911 reftex-typekey-list))
5912
5913(defun reftex-compile-variables ()
5914 ;; Compile the information in reftex-label-alist & Co.
5594 5915
5595(defun reftex-compute-ref-cite-tables () 5916 (message "Compiling label environment definitions...")
5596 ;; Update ref and cite tables
5597 5917
5598 ;; Update AUCTeX style information 5918 ;; Update AUCTeX style information
5599 (when (and (featurep 'tex-site) (fboundp 'TeX-update-style)) 5919 (when (and (featurep 'tex-site) (fboundp 'TeX-update-style))
5600 (condition-case nil (TeX-update-style) (error nil))) 5920 (condition-case nil (TeX-update-style) (error nil)))
5601 5921
5922 ;; Record that we have done this, and what we have used.
5923 (setq reftex-tables-dirty nil)
5924 (setq reftex-memory
5925 (list reftex-label-alist
5926 (get reftex-docstruct-symbol 'reftex-section-levels)
5927 (get reftex-docstruct-symbol 'reftex-label-alist-style)
5928 reftex-default-label-alist-entries))
5929
5602 ;; Compile information in reftex-label-alist 5930 ;; Compile information in reftex-label-alist
5603 (let ((tmp (reftex-uniquify (reftex-splice-symbols-into-list 5931 (let ((all (reftex-uniquify-by-car
5604 (append 5932 (reftex-splice-symbols-into-list
5605 reftex-label-alist 5933 (append reftex-label-alist
5606 reftex-label-alist-external-add-ons 5934 (get reftex-docstruct-symbol 'reftex-label-alist-style)
5607 reftex-default-label-alist-entries) 5935 reftex-default-label-alist-entries)
5608 reftex-label-alist-builtin) 5936 reftex-label-alist-builtin)
5609 '(nil))) 5937 '(nil)))
5610 entry env-or-mac typekeychar typekey prefix context word 5938 entry env-or-mac typekeychar typekey prefix context word
5611 fmt reffmt labelfmt wordlist qh-list macros-with-labels 5939 fmt reffmt labelfmt wordlist qh-list macros-with-labels
5612 nargs nlabel opt-args cell sum i words-are-re) 5940 nargs nlabel opt-args cell sum i)
5613 5941
5614 (setq reftex-words-to-typekey-alist nil 5942 (setq reftex-words-to-typekey-alist nil
5615 reftex-typekey-list nil 5943 reftex-typekey-list nil
@@ -5618,12 +5946,12 @@ This enforces rescanning the buffer on next use."
5618 reftex-env-or-mac-alist nil 5946 reftex-env-or-mac-alist nil
5619 reftex-label-env-list nil 5947 reftex-label-env-list nil
5620 reftex-label-mac-list nil) 5948 reftex-label-mac-list nil)
5621 (while tmp 5949 (while all
5622 (catch 'next-entry 5950 (catch 'next-entry
5623 (setq entry (car tmp) 5951 (setq entry (car all)
5624 env-or-mac (car entry) 5952 env-or-mac (car entry)
5625 entry (cdr entry) 5953 entry (cdr entry)
5626 tmp (cdr tmp)) 5954 all (cdr all))
5627 (if (null env-or-mac) 5955 (if (null env-or-mac)
5628 (setq env-or-mac "")) 5956 (setq env-or-mac ""))
5629 (if (stringp (car entry)) 5957 (if (stringp (car entry))
@@ -5633,7 +5961,7 @@ This enforces rescanning the buffer on next use."
5633 (cons (concat (car entry) ":") 5961 (cons (concat (car entry) ":")
5634 (cdr entry))))) 5962 (cdr entry)))))
5635 (setq typekeychar (nth 0 entry) 5963 (setq typekeychar (nth 0 entry)
5636 typekey (char-to-string typekeychar) 5964 typekey (if typekeychar (char-to-string typekeychar) nil)
5637 prefix (nth 1 entry) 5965 prefix (nth 1 entry)
5638 fmt (nth 2 entry) 5966 fmt (nth 2 entry)
5639 context (nth 3 entry) 5967 context (nth 3 entry)
@@ -5644,17 +5972,19 @@ This enforces rescanning the buffer on next use."
5644 5972
5645 (if (and (stringp fmt) 5973 (if (and (stringp fmt)
5646 (string-match "@" fmt)) 5974 (string-match "@" fmt))
5647 ;; special syntax for specifying a label format 5975 ;; Special syntax for specifying a label format
5648 (setq fmt (split-string fmt "@+")) 5976 (setq fmt (split-string fmt "@+"))
5649 (setq fmt (list "\\label{%s}" fmt))) 5977 (setq fmt (list "\\label{%s}" fmt)))
5650 (setq labelfmt (car fmt) 5978 (setq labelfmt (car fmt)
5651 reffmt (nth 1 fmt)) 5979 reffmt (nth 1 fmt))
5980 ;; Note a new typekey
5652 (if typekey 5981 (if typekey
5653 (add-to-list 'reftex-typekey-list typekey)) 5982 (add-to-list 'reftex-typekey-list typekey))
5654 (if (and typekey prefix 5983 (if (and typekey prefix
5655 (not (assoc typekey reftex-typekey-to-prefix-alist))) 5984 (not (assoc typekey reftex-typekey-to-prefix-alist)))
5656 (add-to-list 'reftex-typekey-to-prefix-alist 5985 (add-to-list 'reftex-typekey-to-prefix-alist
5657 (cons typekey prefix))) 5986 (cons typekey prefix)))
5987 ;; Check if this is a macro or environment
5658 (cond 5988 (cond
5659 ((string-match "\\`\\\\" env-or-mac) 5989 ((string-match "\\`\\\\" env-or-mac)
5660 ;; It's a macro 5990 ;; It's a macro
@@ -5664,8 +5994,9 @@ This enforces rescanning the buffer on next use."
5664 nlabel (third result) 5994 nlabel (third result)
5665 opt-args (fourth result)) 5995 opt-args (fourth result))
5666 (if nlabel (add-to-list 'macros-with-labels env-or-mac))) 5996 (if nlabel (add-to-list 'macros-with-labels env-or-mac)))
5667 (add-to-list 'reftex-label-mac-list env-or-mac)) 5997 (if typekey (add-to-list 'reftex-label-mac-list env-or-mac)))
5668 (t 5998 (t
5999 ;; It's an environment
5669 (setq nargs nil nlabel nil opt-args nil) 6000 (setq nargs nil nlabel nil opt-args nil)
5670 (cond ((string= env-or-mac "any")) 6001 (cond ((string= env-or-mac "any"))
5671 ((string= env-or-mac "")) 6002 ((string= env-or-mac ""))
@@ -5678,60 +6009,65 @@ This enforces rescanning the buffer on next use."
5678 (format 6009 (format
5679 (cdr (assq context reftex-default-context-regexps)) 6010 (cdr (assq context reftex-default-context-regexps))
5680 (regexp-quote env-or-mac)))) 6011 (regexp-quote env-or-mac))))
6012 ;; See if this is the first format for this typekey
5681 (and reffmt 6013 (and reffmt
5682 (not (assoc typekey reftex-typekey-to-format-alist)) 6014 (not (assoc typekey reftex-typekey-to-format-alist))
5683 (push (cons typekey reffmt) reftex-typekey-to-format-alist)) 6015 (push (cons typekey reffmt) reftex-typekey-to-format-alist))
6016 ;; See if this is the first definition for this env-or-mac
5684 (and (not (string= env-or-mac "any")) 6017 (and (not (string= env-or-mac "any"))
5685 (not (string= env-or-mac "")) 6018 (not (string= env-or-mac ""))
5686 (not (assoc env-or-mac reftex-env-or-mac-alist)) 6019 (not (assoc env-or-mac reftex-env-or-mac-alist))
5687 (push (list env-or-mac typekey context labelfmt 6020 (push (list env-or-mac typekey context labelfmt
5688 nargs nlabel opt-args) 6021 nargs nlabel opt-args)
5689 reftex-env-or-mac-alist)) 6022 reftex-env-or-mac-alist))
6023 ;; Are the magic words regular expressions? Quote normal words.
5690 (if (eq (car wordlist) 'regexp) 6024 (if (eq (car wordlist) 'regexp)
5691 (setq wordlist (cdr wordlist) 6025 (setq wordlist (cdr wordlist))
5692 words-are-re t) 6026 (setq wordlist (mapcar 'regexp-quote wordlist)))
5693 (setq words-are-re nil)) 6027 ;; Remember the first association of each word.
5694 (while (and (setq word (pop wordlist)) 6028 (while (stringp (setq word (pop wordlist)))
5695 (stringp word))
5696 (setq word (identity (if words-are-re word (regexp-quote word))))
5697 (or (assoc word reftex-words-to-typekey-alist) 6029 (or (assoc word reftex-words-to-typekey-alist)
5698 (push (cons word typekey) reftex-words-to-typekey-alist))) 6030 (push (cons word typekey) reftex-words-to-typekey-alist)))
5699 (cond 6031 (cond
5700 ((string= "" env-or-mac) nil) 6032 ((string= "" env-or-mac) nil)
5701 ((setq cell (assoc typekey qh-list)) 6033 ((setq cell (assoc typekey qh-list))
5702 (push env-or-mac (cdr cell))) 6034 (push env-or-mac (cdr cell)))
5703 (t 6035 (typekey
5704 (push (list typekey env-or-mac) qh-list))))) 6036 (push (list typekey env-or-mac) qh-list)))))
5705 6037
6038 (setq reftex-typekey-to-prefix-alist
6039 (nreverse reftex-typekey-to-prefix-alist))
6040
6041 ;; Prepare the typekey query prompt and help string.
5706 (setq qh-list 6042 (setq qh-list
5707 (sort qh-list (function 6043 (sort qh-list (function
5708 (lambda (x1 x2) (string< (car x1) (car x2)))))) 6044 (lambda (x1 x2) (string< (car x1) (car x2))))))
5709 (setq reftex-typekey-to-prefix-alist
5710 (nreverse reftex-typekey-to-prefix-alist))
5711 (setq reftex-type-query-prompt 6045 (setq reftex-type-query-prompt
5712 (concat "Label type: " 6046 (concat "Label type: ["
5713 (mapconcat (function (lambda(x) (format "[%s]" (car x)))) 6047 (mapconcat (function (lambda(x) (format "%s" (car x))))
5714 qh-list " ") 6048 qh-list "")
5715 " (?=Help)")) 6049 "]"))
6050 ;; In the help string, we need to wrap lines...
5716 (setq reftex-type-query-help 6051 (setq reftex-type-query-help
5717 (concat "SELECT A LABEL TYPE:\n--------------------\n" 6052 (concat
5718 (mapconcat 6053 "SELECT A LABEL TYPE:\n--------------------\n"
5719 (function 6054 (mapconcat
5720 (lambda(x) 6055 (lambda(x)
5721 (setq sum 0) 6056 (setq sum 0)
5722 (format " [%s] %s" 6057 (format " [%s] %s"
5723 (car x) 6058 (car x)
5724 (mapconcat 6059 (mapconcat (lambda(env)
5725 (lambda(x) 6060 (setq sum (+ sum (length env)))
5726 (setq sum (+ sum (length x))) 6061 (if (< sum 60)
5727 (if (< sum 60) 6062 env
5728 x 6063 (setq sum 0)
5729 (setq sum 0) 6064 (concat "\n " env)))
5730 (concat "\n " x))) 6065 (cdr x) " ")))
5731 (cdr x) " ")))) 6066 qh-list "\n")))
5732 qh-list "\n"))) 6067
5733 6068 ;; Convert magic words to regular expressions. We make regular expressions
5734 ;; Convert magic words to regular expressions 6069 ;; which allow for some chars from the ref format to be in the buffer.
6070 ;; These characters will be seen and removed.
5735 (setq reftex-words-to-typekey-alist 6071 (setq reftex-words-to-typekey-alist
5736 (mapcar 6072 (mapcar
5737 (lambda (x) 6073 (lambda (x)
@@ -5740,8 +6076,8 @@ This enforces rescanning the buffer on next use."
5740 fmt (cdr (assoc typekey reftex-typekey-to-format-alist))) 6076 fmt (cdr (assoc typekey reftex-typekey-to-format-alist)))
5741 (setq word (concat "\\W\\(" word "[ \t\n\r]*\\)\\(")) 6077 (setq word (concat "\\W\\(" word "[ \t\n\r]*\\)\\("))
5742 (setq i 0) 6078 (setq i 0)
5743 (while (and (< i 10) 6079 (while (and (< i 10) ; maximum number of format chars allowed
5744 (< i (length fmt)) 6080 (< i (length fmt))
5745 (not (member (aref fmt i) '(?%)))) 6081 (not (member (aref fmt i) '(?%))))
5746 (setq word (concat word "\\|" (regexp-quote 6082 (setq word (concat word "\\|" (regexp-quote
5747 (substring fmt 0 (1+ i))))) 6083 (substring fmt 0 (1+ i)))))
@@ -5749,25 +6085,31 @@ This enforces rescanning the buffer on next use."
5749 (cons (concat word "\\)\\=") typekey)) 6085 (cons (concat word "\\)\\=") typekey))
5750 (nreverse reftex-words-to-typekey-alist))) 6086 (nreverse reftex-words-to-typekey-alist)))
5751 6087
6088 ;; Make the full list of section levels
6089 (setq reftex-section-levels-all
6090 (append (get reftex-docstruct-symbol 'reftex-section-levels)
6091 reftex-section-levels))
6092
5752 ;; Calculate the regular expressions 6093 ;; Calculate the regular expressions
5753 (let ((label-re "\\\\label{\\([^}]*\\)}") 6094 (let* ((wbol "\\(\\`\\|[\n\r]\\)[ \t]*")
5754 (include-re "\\(\\`\\|[\n\r]\\)[ \t]*\\\\\\(include\\|input\\)[{ \t]+\\([^} \t\n\r]+\\)") 6095 (label-re "\\\\label{\\([^}]*\\)}")
5755 (section-re 6096 (include-re (concat wbol "\\\\\\(include\\|input\\)[{ \t]+\\([^} \t\n\r]+\\)"))
5756 (concat "\\(\\`\\|[\n\r]\\)[ \t]*\\\\\\(" 6097 (section-re
5757 (mapconcat 'car reftex-section-levels "\\|") 6098 (concat wbol "\\\\\\("
5758 "\\)\\*?\\(\\[[^]]*\\]\\)?{")) 6099 (mapconcat 'car reftex-section-levels-all "\\|")
5759 (appendix-re "\\(\\`\\|[\n\r]\\)[ \t]*\\(\\\\appendix\\)") 6100 "\\)\\*?\\(\\[[^]]*\\]\\)?{"))
5760 (macro-re 6101 (appendix-re (concat wbol "\\(\\\\appendix\\)"))
5761 (if macros-with-labels 6102 (macro-re
5762 (concat "\\(" 6103 (if macros-with-labels
5763 (mapconcat 'regexp-quote macros-with-labels "\\|") 6104 (concat "\\("
5764 "\\)[[{]") 6105 (mapconcat 'regexp-quote macros-with-labels "\\|")
5765 "")) 6106 "\\)[[{]")
5766 (find-label-re-format 6107 ""))
5767 (concat "\\(" 6108 (find-label-re-format
5768 (mapconcat 'regexp-quote (append '("\\label") 6109 (concat "\\("
5769 macros-with-labels) "\\|") 6110 (mapconcat 'regexp-quote (append '("\\label")
5770 "\\)\\([[{][^]}]*[]}]\\)*[[{]\\(%s\\)[]}]"))) 6111 macros-with-labels) "\\|")
6112 "\\)\\([[{][^]}]*[]}]\\)*[[{]\\(%s\\)[]}]")))
5771 (setq reftex-section-regexp section-re 6113 (setq reftex-section-regexp section-re
5772 reftex-section-or-include-regexp 6114 reftex-section-or-include-regexp
5773 (concat section-re "\\|" include-re) 6115 (concat section-re "\\|" include-re)
@@ -5777,18 +6119,519 @@ This enforces rescanning the buffer on next use."
5777 (if macros-with-labels "\\|" "") macro-re) 6119 (if macros-with-labels "\\|" "") macro-re)
5778 reftex-find-label-regexp-format find-label-re-format 6120 reftex-find-label-regexp-format find-label-re-format
5779 reftex-find-label-regexp-format2 6121 reftex-find-label-regexp-format2
5780 "\\([]} \t\n\r]\\)\\([[{]\\)\\(%s\\)[]}]")))) 6122 "\\([]} \t\n\r]\\)\\([[{]\\)\\(%s\\)[]}]")
6123 (message "Compiling label environment definitions...done")))
6124 (put reftex-docstruct-symbol 'reftex-cache
6125 (mapcar 'symbol-value reftex-cache-variables)))
5781 6126
5782;;; Keybindings -------------------------------------------------------------- 6127;;; =========================================================================
6128;;;
6129;;; Operations on entire Multifile documents
5783 6130
5784(define-key reftex-mode-map "\C-c=" 'reftex-toc) 6131(defun reftex-create-tags-file ()
5785(define-key reftex-mode-map "\C-c(" 'reftex-label) 6132 "Create TAGS file by running `etags' on the current document.
5786(define-key reftex-mode-map "\C-c)" 'reftex-reference) 6133The TAGS file is also immediately visited with `visit-tags-table'."
5787(define-key reftex-mode-map "\C-c[" 'reftex-citation) 6134 (interactive)
5788(define-key reftex-mode-map "\C-c&" 'reftex-view-crossref) 6135 (reftex-access-scan-info current-prefix-arg)
6136 (let* ((master (reftex-TeX-master-file))
6137 (files (reftex-all-document-files))
6138 (cmd (format "etags %s" (mapconcat 'identity files " "))))
6139 (save-excursion
6140 (set-buffer (reftex-get-buffer-visiting master))
6141 (message "Running etags to create TAGS file...")
6142 (shell-command cmd)
6143 (visit-tags-table "TAGS"))))
6144
6145;; History of grep commands.
6146(defvar reftex-grep-history nil)
6147(defvar reftex-grep-command "grep -n "
6148 "Last grep command used in \\[reftex-grep-document]; default for next grep.")
6149
6150(defun reftex-grep-document (grep-cmd)
6151 "Run grep query through all files related to this document.
6152With prefix arg, force to rescan document.
6153This works also without an active TAGS table."
6154
6155 (interactive
6156 (list (read-from-minibuffer "Run grep on document (like this): "
6157 reftex-grep-command nil nil
6158 'reftex-grep-history)))
6159 (reftex-access-scan-info current-prefix-arg)
6160 (let* ((files (reftex-all-document-files t))
6161 (cmd (format
6162 "%s %s" grep-cmd
6163 (mapconcat 'identity files " "))))
6164 (grep cmd)))
6165
6166(defun reftex-search-document (&optional regexp)
6167 "Regexp search through all files of the current TeX document.
6168Starts always in the master file. Stops when a match is found.
6169To continue searching for next match, use command \\[tags-loop-continue].
6170This works also without an active TAGS table."
6171 (interactive)
6172 (let ((default (reftex-this-word)))
6173 (unless regexp
6174 (setq regexp (read-string (format "Search regexp in document [%s]: "
6175 default))))
6176 (if (string= regexp "") (setq regexp (regexp-quote default)))
6177
6178 (reftex-access-scan-info current-prefix-arg)
6179 (tags-search regexp (list 'reftex-all-document-files))))
6180
6181(defun reftex-query-replace-document (&optional from to delimited)
6182 "Run a query-replace-regexp of FROM with TO over the entire TeX document.
6183Third arg DELIMITED (prefix arg) means replace only word-delimited matches.
6184If you exit (\\[keyboard-quit] or ESC), you can resume the query replace
6185with the command \\[tags-loop-continue].
6186This works also without an active TAGS table."
6187 (interactive)
6188 (let ((default (reftex-this-word)))
6189 (unless from
6190 (setq from (read-string (format "Replace regexp in document [%s]: "
6191 default)))
6192 (if (string= from "") (setq from (regexp-quote default))))
6193 (unless to
6194 (setq to (read-string (format "Replace regexp %s with: " from))))
6195 (reftex-access-scan-info current-prefix-arg)
6196 (tags-query-replace from to (or delimited current-prefix-arg)
6197 (list 'reftex-all-document-files))))
6198
6199(defun reftex-find-duplicate-labels ()
6200 "Produce a list of all duplicate labels in the document."
6201
6202 (interactive)
6203
6204 ;; Rescan the document to make sure
6205 (reftex-access-scan-info t)
6206
6207 (let ((master (reftex-TeX-master-file))
6208 (cnt 0)
6209 (dlist
6210 (mapcar
6211 (function
6212 (lambda (x)
6213 (let (x1)
6214 (cond
6215 ((memq (car x)
6216 '(toc bof eof bib thebib label-numbers xr xr-doc
6217 master-dir file-error bibview-cache appendix
6218 is-multi))
6219 nil)
6220 (t
6221 (setq x1 (reftex-all-assoc-string
6222 (car x) (symbol-value reftex-docstruct-symbol)))
6223 (if (< 1 (length x1))
6224 (append (list (car x))
6225 (mapcar (function
6226 (lambda(x)
6227 (abbreviate-file-name (nth 3 x))))
6228 x1))
6229 (list nil)))))))
6230 (reftex-uniquify-by-car (symbol-value reftex-docstruct-symbol)))))
6231
6232 (setq dlist (reftex-uniquify-by-car dlist))
6233 (if (null dlist) (error "No duplicate labels in document"))
6234 (switch-to-buffer-other-window "*Duplicate Labels*")
6235 (make-local-variable 'TeX-master)
6236 (setq TeX-master master)
6237 (erase-buffer)
6238 (insert " MULTIPLE LABELS IN CURRENT DOCUMENT:\n")
6239 (insert
6240 " Move point to label and type `r' to run a query-replace on the label\n"
6241 " and its references. Type `q' to exit this buffer.\n\n")
6242 (insert " LABEL FILE\n")
6243 (insert " -------------------------------------------------------------\n")
6244 (use-local-map (make-sparse-keymap))
6245 (local-set-key [?q] (function
6246 (lambda () "Kill this buffer." (interactive)
6247 (kill-buffer (current-buffer)) (delete-window))))
6248 (local-set-key [?r] 'reftex-change-label)
6249 (while dlist
6250 (when (and (car (car dlist))
6251 (cdr (car dlist)))
6252 (incf cnt)
6253 (insert (mapconcat 'identity (car dlist) "\n ") "\n"))
6254 (pop dlist))
6255 (goto-char (point-min))
6256 (when (= cnt 0)
6257 (kill-buffer (current-buffer))
6258 (delete-window)
6259 (message "Document does not contain duplicate labels."))))
6260
6261(defun reftex-change-label (&optional from to)
6262 "Query replace FROM with TO in all \\label and \\ref commands.
6263Works on the entire multifile document.
6264If you exit (\\[keyboard-quit] or ESC), you can resume the query replace
6265with the command \\[tags-loop-continue].
6266This works also without an active TAGS table."
6267 (interactive)
6268 (let ((default (reftex-this-word "-a-zA-Z0-9_*.:")))
6269 (unless from
6270 (setq from (read-string (format "Replace label globally [%s]: "
6271 default))))
6272 (if (string= from "") (setq from default))
6273 (unless to
6274 (setq to (read-string (format "Replace label %s with: "
6275 from))))
6276 (reftex-query-replace-document
6277 (concat "\\\\\\(label\\|[a-z]*ref\\){" (regexp-quote from) "}")
6278 (format "\\\\\\1{%s}" to))))
6279
6280(defun reftex-renumber-simple-labels ()
6281 "Renumber all simple labels in the document to make them sequentially.
6282Simple labels are the ones created by RefTeX, consisting only of the
6283prefix and a number. After the command completes, all these labels will
6284have sequential numbers throughout the document. Any references to
6285the labels will be changed as well. For this, RefTeX looks at the
6286arguments of any macros which either start or end in the string `ref'.
6287This command should be used with care, in particular in multifile
6288documents. You should not use it if another document refers to this
6289one with the `xr' package."
6290 (interactive)
6291 ;; Resan the entire document
6292 (reftex-access-scan-info 1)
6293 ;; Get some insurance
6294 (if (and (reftex-is-multi)
6295 (not (yes-or-no-p "Replacing all simple labels in multiple files is risky. Continue? ")))
6296 (error "Abort"))
6297 ;; Make the translation list
6298 (let* ((re-core (concat "\\("
6299 (mapconcat 'cdr reftex-typekey-to-prefix-alist "\\|")
6300 "\\)"))
6301 (label-re (concat "\\`" re-core "\\([0-9]+\\)\\'"))
6302 (search-re (concat "{\\(" re-core "\\([0-9]+\\)\\)}"))
6303 (error-fmt "Undefined label or reference %s. Ignore and continue? ")
6304 (label-numbers-alist (mapcar (lambda (x) (cons (cdr x) 0))
6305 reftex-typekey-to-prefix-alist))
6306 (files (reftex-all-document-files))
6307 (list (symbol-value reftex-docstruct-symbol))
6308 translate-alist n entry label new-label nr-cell changed-sequence)
6309
6310 (while (setq entry (pop list))
6311 (when (and (stringp (car entry))
6312 (string-match label-re (car entry)))
6313 (setq label (car entry)
6314 nr-cell (assoc (match-string 1 (car entry))
6315 label-numbers-alist))
6316 (if (assoc label translate-alist)
6317 (error "Duplicate label %s" label))
6318 (setq new-label (concat (match-string 1 (car entry))
6319 (incf (cdr nr-cell))))
6320 (push (cons label new-label) translate-alist)
6321 (or (string= label new-label) (setq changed-sequence t))))
6322
6323 (unless changed-sequence
6324 (error "Simple labels are already in correct sequence"))
6325
6326 ;; Save all document buffers before this operation
6327 (reftex-save-all-document-buffers)
6328
6329 ;; First test to check for erros
6330 (setq n (reftex-translate
6331 files search-re translate-alist error-fmt 'test))
6332
6333 ;; Now the real thing.
6334 (if (yes-or-no-p
6335 (format "Replace %d items at %d places in %d files? "
6336 (length translate-alist) n (length files)))
6337 (progn
6338 (let ((inhibit-quit t)) ;; Do not disturb...
6339 (reftex-translate
6340 files search-re translate-alist error-fmt nil)
6341 (setq quit-flag nil))
6342 (if (and (reftex-is-multi)
6343 (yes-or-no-p "Save entire document? "))
6344 (reftex-save-all-document-buffers))
6345 ;; Rescan again...
6346 (reftex-access-scan-info 1)
6347 (message "Done replacing simple labels."))
6348 (message "No replacements done"))))
6349
6350(defun reftex-translate (files search-re translate-alist error-fmt test)
6351 ;; In FILES, look for SEARCH-RE and replace match 1 of it with
6352 ;; its association in TRANSLATE-ALSIT.
6353 ;; If we do not find an association and TEST is non-nil, query
6354 ;; to ignore the problematic string.
6355 ;; If TEST is nil, it is ignored without query.
6356 ;; Return the number of replacements.
6357 (let ((n 0) file label match-data buf macro pos cell)
6358 (while (setq file (pop files))
6359 (setq buf (reftex-get-file-buffer-force file))
6360 (unless buf
6361 (error "No such file %s" file))
6362 (set-buffer buf)
6363 (save-excursion
6364 (save-restriction
6365 (widen)
6366 (goto-char (point-min))
6367 (while (re-search-forward search-re nil t)
6368 (save-excursion
6369 (backward-char)
6370 (setq label (reftex-match-string 1)
6371 cell (assoc label translate-alist)
6372 match-data (match-data)
6373 macro (reftex-what-macro 1)
6374 pos (cdr macro))
6375 (goto-char (or pos (point)))
6376 (when (and macro
6377 (or (looking-at "\\\\ref")
6378 (looking-at "\\\\[a-zA-Z]*ref[^a-zA-Z]")
6379 (looking-at "\\\\ref[a-zA-Z]*[^a-zA-Z]")
6380 (looking-at (format
6381 reftex-find-label-regexp-format
6382 (regexp-quote label)))))
6383 ;; OK, we should replace it.
6384 (set-match-data match-data)
6385 (cond
6386 ((and test (not cell))
6387 ;; We've got a problem
6388 (unwind-protect
6389 (progn
6390 (reftex-highlight 1 (match-beginning 0) (match-end 0))
6391 (ding)
6392 (or (y-or-n-p (format error-fmt label))
6393 (error "Abort")))
6394 (reftex-unhighlight 1)))
6395 ((and test cell)
6396 (incf n))
6397 ((and (not test) cell)
6398 ;; Replace
6399 (goto-char (match-beginning 1))
6400 (delete-region (match-beginning 1) (match-end 1))
6401 (insert (cdr cell)))
6402 (t nil))))))))
6403 n))
6404
6405(defun reftex-save-all-document-buffers ()
6406 "Save all documents associated with the current document.
6407The function is useful after a global action like replacing or renumbering
6408labels."
6409 (interactive)
6410 (let ((files (reftex-all-document-files))
6411 file buffer)
6412 (save-excursion
6413 (while (setq file (pop files))
6414 (setq buffer (reftex-get-buffer-visiting file))
6415 (when buffer
6416 (set-buffer buffer)
6417 (save-buffer))))))
6418
6419;;; =========================================================================
6420;;;
6421;;; AUCTeX Interface
6422
6423(defun reftex-plug-flag (which)
6424 ;; Tell if a certain flag is set in reftex-plug-into-AUCTeX
6425 (or (eq t reftex-plug-into-AUCTeX)
6426 (and (listp reftex-plug-into-AUCTeX)
6427 (nth which reftex-plug-into-AUCTeX))))
6428
6429(defun reftex-arg-label (optional &optional prompt definition)
6430 "Use `reftex-label', `reftex-reference' or AUCTeX's code to insert label arg.
6431What is being used depends upon `reftex-plug-into-AUCTeX'."
6432 (let (label)
6433 (cond
6434 ((and definition (reftex-plug-flag 1))
6435 ;; Create a new label, with a temporary brace for `reftex-what-macro'
6436 (unwind-protect
6437 (progn (insert "{") (setq label (or (reftex-label nil t) "")))
6438 (delete-backward-char 1)))
6439 ((and (not definition) (reftex-plug-flag 2))
6440 ;; Reference a label with RefTeX
6441 (setq label (reftex-reference nil t)))
6442 (t
6443 ;; AUCTeX's default mechanism
6444 (setq label (completing-read (TeX-argument-prompt optional prompt "Key")
6445 (LaTeX-label-list)))))
6446 (if (and definition (not (string-equal "" label)))
6447 (LaTeX-add-labels label))
6448 (TeX-argument-insert label optional optional)))
6449
6450(defun reftex-arg-cite (optional &optional prompt definition)
6451 "Use `reftex-citation' or AUCTeX's code to insert a cite-key macro argument.
6452What is being used depends upon `reftex-plug-into-AUCTeX'."
6453 (let (items)
6454 (cond
6455 ((and (not definition) (reftex-plug-flag 3))
6456 (setq items (list (or (reftex-citation t) ""))))
6457 (t
6458 (setq prompt (concat (if optional "(Optional) " "")
6459 (if prompt prompt "Add key")
6460 ": (default none) "))
6461 (setq items (multi-prompt "," t prompt (LaTeX-bibitem-list)))))
6462 (apply 'LaTeX-add-bibitems items)
6463 (TeX-argument-insert (mapconcat 'identity items ",") optional optional)))
6464
6465(defun reftex-plug-into-AUCTeX ()
6466 ;; Replace AUCTeX functions with RefTeX functions.
6467 ;; Which functions are replaced is controlled by the variable
6468 ;; `reftex-plug-into-AUCTeX'.
6469
6470 (if (reftex-plug-flag 0)
6471 (setq LaTeX-label-function 'reftex-label)
6472 (setq LaTeX-label-function nil))
6473
6474 (if (and (or (reftex-plug-flag 1) (reftex-plug-flag 2))
6475 (fboundp 'TeX-arg-label))
6476 (fset 'TeX-arg-label 'reftex-arg-label))
6477
6478 (if (and (reftex-plug-flag 3)
6479 (fboundp 'TeX-arg-cite))
6480 (fset 'TeX-arg-cite 'reftex-arg-cite)))
6481
6482(defun reftex-toggle-plug-into-AUCTeX ()
6483 "Toggle Interface between AUCTeX and RefTeX on and off."
6484 (interactive)
6485 (unless (and (featurep 'tex-site) (featurep 'latex))
6486 (error "AUCTeX's LaTeX mode does not seem to be loaded."))
6487 (setq reftex-plug-into-AUCTeX (not reftex-plug-into-AUCTeX))
6488 (reftex-plug-into-AUCTeX)
6489 (if reftex-plug-into-AUCTeX
6490 (message "RefTeX has been plugged into AUCTeX.")
6491 (message "RefTeX no longer interacts with AUCTeX.")))
6492
6493(defun reftex-add-label-environments (entry-list)
6494 "Add label environment descriptions to `reftex-label-alist-style'.
6495The format of ENTRY-LIST is exactly like `reftex-label-alist'. See there
6496for details.
6497This function makes it possible to support RefTeX from AUCTeX style files.
6498The entries in ENTRY-LIST will be processed after the user settings in
6499`reftex-label-alist', and before the defaults (specified in
6500`reftex-default-label-alist-entries'). Any changes made to
6501`reftex-label-alist-style' will raise a flag to the effect that
6502the label information is recompiled on next use."
6503 (unless reftex-docstruct-symbol
6504 (reftex-tie-multifile-symbols))
6505 (when (and reftex-docstruct-symbol
6506 (symbolp reftex-docstruct-symbol))
6507 (let ((list (get reftex-docstruct-symbol 'reftex-label-alist-style))
6508 entry changed)
6509 (while entry-list
6510 (setq entry (pop entry-list))
6511 (unless (member entry list)
6512 (setq reftex-tables-dirty t
6513 changed t)
6514 (push entry list)))
6515 (when changed
6516 (put reftex-docstruct-symbol 'reftex-label-alist-style list)))))
6517(defalias 'reftex-add-to-label-alist 'reftex-add-label-environments)
6518
6519(defun reftex-add-section-levels (entry-list)
6520 "Add entries to the value of `reftex-section-levels'.
6521The added values are kept local to the current document. The format
6522of ENTRY-LIST is a list of cons cells (\"MACRONAME\" . LEVEL). See
6523`reftex-section-levels' for an example."
6524 (unless reftex-docstruct-symbol
6525 (reftex-tie-multifile-symbols))
6526 (when (and reftex-docstruct-symbol
6527 (symbolp reftex-docstruct-symbol))
6528 (let ((list (get reftex-docstruct-symbol 'reftex-section-levels))
6529 entry changed)
6530 (while entry-list
6531 (setq entry (pop entry-list))
6532 (unless (member entry list)
6533 (setq reftex-tables-dirty t
6534 changed t)
6535 (push entry list)))
6536 (when changed
6537 (put reftex-docstruct-symbol 'reftex-section-levels list)))))
6538
6539(defun reftex-set-cite-format (value)
6540 "Set the document-local value of `reftex-cite-format'.
6541When such a value exists, it overwrites the setting given with
6542`reftex-cite-format'. See the documentation of `reftex-cite-format'
6543for possible values. This function should be used from AUCTeX style files."
6544 (unless reftex-docstruct-symbol
6545 (reftex-tie-multifile-symbols))
6546 (when (and reftex-docstruct-symbol
6547 (symbolp reftex-docstruct-symbol))
6548 (put reftex-docstruct-symbol 'reftex-cite-format value)))
6549
6550(defun reftex-notice-new-section ()
6551 "Hook to handshake with RefTeX after a new section has been inserted."
6552 ;; Add a new section to the docstruct list and renumber the
6553 ;; following sections. This hook has to be called immediately after
6554 ;; the new section was inserted into the buffer, and before the
6555 ;; section label is created.
6556
6557 (condition-case nil
6558 (catch 'exit
6559 (unless reftex-mode (throw 'exit nil))
6560 (reftex-access-scan-info)
6561 (let* ((docstruct (symbol-value reftex-docstruct-symbol))
6562 here-am-I appendix tail toc-entry star level
6563 section-number context)
6564
6565 (save-excursion
6566 (when (re-search-backward reftex-section-regexp nil t)
6567
6568 ;; Find where we are
6569 (setq here-am-I (reftex-where-am-I))
6570 (unless (cdr here-am-I) (throw 'exit nil))
6571 (setq reftex-active-toc (reftex-last-assoc-before-elt
6572 'toc (car here-am-I) docstruct)
6573 appendix (reftex-last-assoc-before-elt
6574 'appendix (car here-am-I) docstruct))
6575
6576 ;; Initialize section numbers
6577 (if (eq (car (car here-am-I)) 'appendix)
6578 (reftex-init-section-numbers nil t)
6579 (reftex-init-section-numbers reftex-active-toc appendix))
6580
6581 ;; Match the section command
6582 (when (and (re-search-forward reftex-everything-regexp nil t)
6583 (match-end 3))
6584 (setq star (= ?* (char-after (match-end 3)))
6585 toc-entry (reftex-section-info (buffer-file-name))
6586 level (nth 5 toc-entry)
6587 tail (memq (car here-am-I)
6588 (symbol-value reftex-docstruct-symbol)))
6589 (if tail
6590 ;; Insert the section info
6591 (push toc-entry (cdr tail))
6592 (throw 'exit nil))
6593
6594 ;; We are done unless we use section numbers
6595 (unless (nth 1 reftex-label-menu-flags) (throw 'exit nil))
6596
6597 ;; Update the remaining toc items
6598 (setq tail (cdr tail))
6599 (while (and (setq tail (memq (assq 'toc (cdr tail)) tail))
6600 (setq toc-entry (car tail))
6601 (>= (nth 5 toc-entry) level))
6602 (setq section-number
6603 (reftex-section-number (nth 5 toc-entry) star)
6604 context (nth 2 toc-entry))
6605 (when (string-match "\\`\\([ \t]*\\)\\([.0-9A-Z]+\\)\\(.*\\)"
6606 context)
6607 (when (and (not appendix)
6608 (>= (string-to-char (match-string 2)) ?A))
6609 ;; Just entered the appendex. Get out.
6610 (throw 'exit nil))
6611
6612 ;; Change the section number.
6613 (setf (nth 2 toc-entry)
6614 (concat (match-string 1 context)
6615 section-number
6616 (match-string 3 context))))))))))
6617 (error nil))
6618 )
6619
6620;;; =========================================================================
6621;;;
6622;;; Keybindings
6623
6624;; The default bindings in the mode map.
6625(loop for x in
6626 '(("\C-c=" . reftex-toc)
6627 ("\C-c(" . reftex-label)
6628 ("\C-c)" . reftex-reference)
6629 ("\C-c[" . reftex-citation)
6630 ("\C-c&" . reftex-view-crossref))
6631 do (define-key reftex-mode-map (car x) (cdr x)))
5789 6632
5790;; Bind `reftex-mouse-view-crossref' only when the key is still free 6633;; Bind `reftex-mouse-view-crossref' only when the key is still free
5791(if (string-match "XEmacs" emacs-version) 6634(if (featurep 'xemacs)
5792 (unless (key-binding [(shift button2)]) 6635 (unless (key-binding [(shift button2)])
5793 (define-key reftex-mode-map [(shift button2)] 6636 (define-key reftex-mode-map [(shift button2)]
5794 'reftex-mouse-view-crossref)) 6637 'reftex-mouse-view-crossref))
@@ -5797,19 +6640,19 @@ This enforces rescanning the buffer on next use."
5797 'reftex-mouse-view-crossref))) 6640 'reftex-mouse-view-crossref)))
5798 6641
5799;; If the user requests so, she can have a few more bindings: 6642;; If the user requests so, she can have a few more bindings:
5800(cond 6643(when reftex-extra-bindings
5801 (reftex-extra-bindings 6644 (loop for x in
5802 (define-key reftex-mode-map "\C-ct" 'reftex-toc) 6645 '(("\C-ct" . reftex-toc)
5803 (define-key reftex-mode-map "\C-cl" 'reftex-label) 6646 ("\C-cl" . reftex-label)
5804 (define-key reftex-mode-map "\C-cr" 'reftex-reference) 6647 ("\C-cr" . reftex-reference)
5805 (define-key reftex-mode-map "\C-cc" 'reftex-citation) 6648 ("\C-cc" . reftex-citation)
5806 (define-key reftex-mode-map "\C-cv" 'reftex-view-crossref) 6649 ("\C-cv" . reftex-view-crossref)
5807 (define-key reftex-mode-map "\C-cg" 'reftex-grep-document) 6650 ("\C-cg" . reftex-grep-document)
5808 (define-key reftex-mode-map "\C-cs" 'reftex-search-document))) 6651 ("\C-cs" . reftex-search-document))
5809 6652 do (define-key reftex-mode-map (car x) (cdr x))))
5810;; Selection Keymaps 6653
6654;; Common bindings in reftex-select-label-map and reftex-select-bib-map
5811(let ((map (make-sparse-keymap))) 6655(let ((map (make-sparse-keymap)))
5812 ; First, the common stuff
5813 (substitute-key-definition 6656 (substitute-key-definition
5814 'next-line 'reftex-select-next map global-map) 6657 'next-line 'reftex-select-next map global-map)
5815 (substitute-key-definition 6658 (substitute-key-definition
@@ -5818,69 +6661,93 @@ This enforces rescanning the buffer on next use."
5818 'keyboard-quit 'reftex-select-keyboard-quit map global-map) 6661 'keyboard-quit 'reftex-select-keyboard-quit map global-map)
5819 (substitute-key-definition 6662 (substitute-key-definition
5820 'newline 'reftex-select-accept map global-map) 6663 'newline 'reftex-select-accept map global-map)
5821 (define-key map " " 'reftex-select-callback)
5822 (define-key map "n" 'reftex-select-next)
5823 (define-key map [(down)] 'reftex-select-next)
5824 (define-key map "p" 'reftex-select-previous)
5825 (define-key map [(up)] 'reftex-select-previous)
5826 (define-key map "f" 'reftex-select-toggle-follow)
5827 (define-key map "\C-m" 'reftex-select-accept)
5828 (define-key map [(return)] 'reftex-select-accept)
5829 (define-key map "q" 'reftex-select-quit)
5830 (define-key map "." 'reftex-select-show-insertion-point)
5831 (define-key map "?" 'reftex-select-help)
5832 (if (string-match "XEmacs" emacs-version)
5833 (define-key map [(button2)] 'reftex-select-mouse-accept) ; XEmacs
5834 (define-key map [(mouse-2)] 'reftex-select-mouse-accept)) ; Emacs
5835 6664
6665 (loop for x in
6666 '((" " . reftex-select-callback)
6667 ("n" . reftex-select-next)
6668 ([(down)] . reftex-select-next)
6669 ("p" . reftex-select-previous)
6670 ([(up)] . reftex-select-previous)
6671 ("f" . reftex-select-toggle-follow)
6672 ("\C-m" . reftex-select-accept)
6673 ([(return)] . reftex-select-accept)
6674 ("q" . reftex-select-quit)
6675 ("." . reftex-select-show-insertion-point)
6676 ("?" . reftex-select-help))
6677 do (define-key map (car x) (cdr x)))
6678
6679 ;; The mouse-2 binding
6680 (if (featurep 'xemacs)
6681 (define-key map [(button2)] 'reftex-select-mouse-accept)
6682 (define-key map [(mouse-2)] 'reftex-select-mouse-accept))
6683
6684 ;; Digit arguments
5836 (loop for key across "0123456789" do 6685 (loop for key across "0123456789" do
5837 (define-key map (vector (list key)) 'digit-argument)) 6686 (define-key map (vector (list key)) 'digit-argument))
5838 (define-key map "-" 'negative-argument) 6687 (define-key map "-" 'negative-argument)
5839 6688
6689 ;; Make two maps
5840 (setq reftex-select-label-map map) 6690 (setq reftex-select-label-map map)
5841 (setq reftex-select-bib-map (copy-keymap map)) 6691 (setq reftex-select-bib-map (copy-keymap map)))
5842 6692
5843 ;; Now the rest 6693;; Specific bindings in reftex-select-label-map
5844 6694(loop for key across "cgilrRstx#%" do
5845 (loop for key across "cgilrRstx#%" do 6695 (define-key reftex-select-label-map (vector (list key))
5846 (define-key reftex-select-label-map (vector (list key)) 6696 (list 'lambda '()
5847 (list 'lambda '() '(interactive) (list 'throw '(quote myexit) key)))) 6697 "Press `?' during selection to find out about this key."
5848 (define-key reftex-select-label-map "b" 'reftex-select-jump-to-previous) 6698 '(interactive) (list 'throw '(quote myexit) key))))
5849 (define-key reftex-select-label-map "v" 'reftex-select-toggle-varioref) 6699
5850 (define-key reftex-select-label-map [(tab)] 'reftex-select-read-label) 6700(loop for x in
5851 (define-key reftex-select-label-map "\C-i" 'reftex-select-read-label) 6701 '(("b" . reftex-select-jump-to-previous)
5852 (define-key reftex-select-label-map "\C-c\C-n" 'reftex-select-next-heading) 6702 ("v" . reftex-select-toggle-varioref)
5853 (define-key reftex-select-label-map "\C-c\C-p" 'reftex-select-previous-heading) 6703 ([(tab)] . reftex-select-read-label)
5854 6704 ("\C-i" . reftex-select-read-label)
5855 (loop for key across "grRa" do 6705 ("\C-c\C-n" . reftex-select-next-heading)
5856 (define-key reftex-select-bib-map (vector (list key)) 6706 ("\C-c\C-p" . reftex-select-previous-heading))
5857 (list 'lambda '() '(interactive) (list 'throw '(quote myexit) key)))) 6707 do
5858 (define-key reftex-select-bib-map "\C-i" 'reftex-select-read-cite) 6708 (define-key reftex-select-label-map (car x) (cdr x)))
5859 (define-key reftex-select-bib-map [(tab)] 'reftex-select-read-cite)) 6709
5860 6710;; Specific bindings in reftex-select-bib-map
6711(loop for key across "grRaA" do
6712 (define-key reftex-select-bib-map (vector (list key))
6713 (list 'lambda '()
6714 "Press `?' during selection to find out about this key."
6715 '(interactive) (list 'throw '(quote myexit) key))))
6716
6717(loop for x in
6718 '(("\C-i" . reftex-select-read-cite)
6719 ([(tab)] . reftex-select-read-cite))
6720 do (define-key reftex-select-bib-map (car x) (cdr x)))
6721
5861;; Table of Contents map 6722;; Table of Contents map
5862(let ((map reftex-toc-map)) 6723(if (featurep 'xemacs)
5863 (if (string-match "XEmacs" emacs-version) 6724 (define-key reftex-toc-map [(button2)] 'reftex-toc-mouse-goto-line-and-hide)
5864 (define-key map [(button2)] 'reftex-toc-mouse-goto-line-and-hide);XEmacs 6725 (define-key reftex-toc-map [(mouse-2)] 'reftex-toc-mouse-goto-line-and-hide))
5865 (define-key map [(mouse-2)] 'reftex-toc-mouse-goto-line-and-hide)) ;Emacs 6726
5866 (define-key map "n" 'next-line) 6727(loop for x in
5867 (define-key map "p" 'previous-line) 6728 '(("n" . next-line)
5868 (define-key map "?" 'reftex-toc-show-help) 6729 ("p" . previous-line)
5869 (define-key map " " 'reftex-toc-view-line) 6730 ("?" . reftex-toc-show-help)
5870 (define-key map "\C-m" 'reftex-toc-goto-line-and-hide) 6731 (" " . reftex-toc-view-line)
5871 (define-key map "\C-i" 'reftex-toc-goto-line) 6732 ("\C-m" . reftex-toc-goto-line-and-hide)
5872 (define-key map "r" 'reftex-toc-rescan) 6733 ("\C-i" . reftex-toc-goto-line)
5873 (define-key map "R" 'reftex-toc-Rescan) 6734 ("r" . reftex-toc-rescan)
5874 (define-key map "g" 'revert-buffer) 6735 ("R" . reftex-toc-Rescan)
5875 (define-key map "q" 'reftex-toc-quit) 6736 ("g" . revert-buffer)
5876 (define-key map "Q" 'reftex-toc-quit-and-kill) 6737 ("q" . reftex-toc-quit)
5877 (define-key map "f" 'reftex-toc-toggle-follow) 6738 ("Q" . reftex-toc-quit-and-kill)
5878 (define-key map "x" 'reftex-toc-external) 6739 ("f" . reftex-toc-toggle-follow)
5879 (loop for key across "0123456789" do 6740 ("x" . reftex-toc-external)
5880 (define-key map (vector (list key)) 'digit-argument)) 6741 ("." . reftex-toc-show-insertion-point))
5881 (define-key map "-" 'negative-argument)) 6742 do (define-key reftex-toc-map (car x) (cdr x)))
6743
6744(loop for key across "0123456789" do
6745 (define-key reftex-toc-map (vector (list key)) 'digit-argument))
6746(define-key reftex-toc-map "-" 'negative-argument)
5882 6747
5883;;; Menus -------------------------------------------------------------------- 6748;;; =========================================================================
6749;;;
6750;;; Menu
5884 6751
5885;; Define a menu for the menu bar if Emacs is running under X 6752;; Define a menu for the menu bar if Emacs is running under X
5886 6753
@@ -5891,91 +6758,180 @@ This enforces rescanning the buffer on next use."
5891 "Menu used in RefTeX mode" 6758 "Menu used in RefTeX mode"
5892 `("Ref" 6759 `("Ref"
5893 ["Table of Contents" reftex-toc t] 6760 ["Table of Contents" reftex-toc t]
5894 "----" 6761 "---"
5895 ["\\label" reftex-label t] 6762 ["\\label" reftex-label t]
5896 ["\\ref" reftex-reference t] 6763 ["\\ref" reftex-reference t]
5897 ["\\cite" reftex-citation t] 6764 ["\\cite" reftex-citation t]
5898 ["View Crossref" reftex-view-crossref t] 6765 ["View Crossref" reftex-view-crossref t]
5899 "----" 6766 "---"
5900 ("Parse Document" 6767 ("Parse Document"
5901 ["Only this File" reftex-parse-one t] 6768 ["Only this File" reftex-parse-one t]
5902 ["Entire Document" reftex-parse-all (reftex-is-multi)] 6769 ["Entire Document" reftex-parse-all (reftex-is-multi)]
5903 ["Save to File" (reftex-access-parse-file 'write) 6770 ["Save to File" (reftex-access-parse-file 'write)
5904 (> (length (symbol-value reftex-docstruct-symbol)) 0)] 6771 (> (length (symbol-value reftex-docstruct-symbol)) 0)]
5905 ["Restore from File" (reftex-access-parse-file 'restore) t] 6772 ["Restore from File" (reftex-access-parse-file 'restore) t]
5906 "----"
5907 ["Enable Partial Scans"
5908 (setq reftex-enable-partial-scans (not reftex-enable-partial-scans))
5909 :style toggle :selected reftex-enable-partial-scans]
5910 ["Auto-Save Parse Info"
5911 (setq reftex-save-parse-info (not reftex-save-parse-info))
5912 :style toggle :selected reftex-save-parse-info]
5913 "---" 6773 "---"
5914 ["Reset RefTeX Mode" reftex-reset-mode t]) 6774 ["Reset RefTeX Mode" reftex-reset-mode t])
5915 ("Global Actions" 6775 ("Global Actions"
5916 ["Search Whole Document" reftex-search-document t] 6776 ["Search Whole Document" reftex-search-document t]
5917 ["Replace in Document" reftex-query-replace-document t] 6777 ["Replace in Document" reftex-query-replace-document t]
5918 ["Grep on Document" reftex-grep-document t] 6778 ["Grep on Document" reftex-grep-document t]
5919 "----" 6779 "---"
5920 ["Create TAGS File" reftex-create-tags-file t] 6780 ["Create TAGS File" reftex-create-tags-file t]
5921 "----" 6781 "---"
5922 ["Find Duplicate Labels" reftex-find-duplicate-labels t] 6782 ["Find Duplicate Labels" reftex-find-duplicate-labels t]
5923 ["Change Label and Refs" reftex-change-label t] 6783 ["Change Label and Refs" reftex-change-label t]
5924 ;;;["Renumber Simple Labels" reftex-renumber-simple-labels t] 6784 ["Renumber Simple Labels" reftex-renumber-simple-labels t]
5925 "---" 6785 "---"
5926 ["Save document" reftex-save-all-document-buffers t]) 6786 ["Save document" reftex-save-all-document-buffers t])
5927 "---" 6787 "---"
5928 ("Reference Options" 6788 ("Options"
5929 "Varioref Setting" 6789 ("Table of Contents"
5930 ["Use `\\vref' by Default" 6790 ["Follow Mode" (setq reftex-toc-follow-mode (not reftex-toc-follow-mode))
5931 (setq reftex-vref-is-default (not reftex-vref-is-default)) 6791 :style toggle :selected reftex-toc-follow-mode]
5932 :style toggle :selected reftex-vref-is-default] 6792 ["Follow Mode may visit files"
5933 "---" 6793 (setq reftex-revisit-to-follow (not reftex-revisit-to-follow))
5934 "Selection Buffers" 6794 :style toggle :selected reftex-revisit-to-follow])
5935 ["Use Multiple Buffers" 6795 ("References"
5936 (setq reftex-use-multiple-selection-buffers 6796 ["Guess Label Type"
5937 (not reftex-use-multiple-selection-buffers)) 6797 (setq reftex-guess-label-type (not reftex-guess-label-type))
5938 :style toggle :selected reftex-use-multiple-selection-buffers] 6798 :style toggle :selected reftex-guess-label-type]
5939 ["Auto Update Buffers" 6799 ["Use `\\vref' by Default"
5940 (setq reftex-auto-update-selection-buffers 6800 (setq reftex-vref-is-default (not reftex-vref-is-default))
5941 (not reftex-auto-update-selection-buffers)) 6801 :style toggle :selected reftex-vref-is-default]
5942 :style toggle :selected reftex-auto-update-selection-buffers]) 6802 "---"
5943 ("Citation Options" 6803 "Selection Buffers"
5944 "Citation Style" 6804 ["Use Multiple Buffers"
5945 ,@(mapcar 6805 (setq reftex-use-multiple-selection-buffers
5946 (function 6806 (not reftex-use-multiple-selection-buffers))
5947 (lambda (x) 6807 :style toggle :selected reftex-use-multiple-selection-buffers]
5948 (vector 6808 ["Auto Update Buffers"
5949 (capitalize (symbol-name (car x))) 6809 (setq reftex-auto-update-selection-buffers
5950 (list 'setq 'reftex-cite-format (list 'quote (car x))) 6810 (not reftex-auto-update-selection-buffers))
5951 ':style 'radio ':selected 6811 :style toggle :selected reftex-auto-update-selection-buffers])
5952 (list 'eq 'reftex-cite-format (list 'quote (car x)))))) 6812 ("Citations"
6813 "Citation Style"
6814 ,@(mapcar
6815 (function
6816 (lambda (x)
6817 (vector
6818 (capitalize (symbol-name (car x)))
6819 (list 'reftex-set-cite-format (list 'quote (car x)))
6820 ':style 'radio ':selected
6821 (list 'eq (list 'reftex-get-cite-format) (list 'quote (car x))))))
5953 reftex-cite-format-builtin) 6822 reftex-cite-format-builtin)
5954 "----" 6823 "---"
5955 "Bibinfo in Comments" 6824 "Bibinfo in Comments"
5956 ["Attach Comments" 6825 ["Attach Comments"
5957 (setq reftex-comment-citations (not reftex-comment-citations)) 6826 (setq reftex-comment-citations (not reftex-comment-citations))
5958 :style toggle :selected reftex-comment-citations] 6827 :style toggle :selected reftex-comment-citations]
5959 "---" 6828 "---"
5960 "Sort Database Matches" 6829 "Sort Database Matches"
5961 ["by Author" (setq reftex-sort-bibtex-matches 'author) 6830 ["by Author" (setq reftex-sort-bibtex-matches 'author)
5962 :style radio :selected (eq reftex-sort-bibtex-matches 'author)] 6831 :style radio :selected (eq reftex-sort-bibtex-matches 'author)]
5963 ["by Year" (setq reftex-sort-bibtex-matches 'year) 6832 ["by Year" (setq reftex-sort-bibtex-matches 'year)
5964 :style radio :selected (eq reftex-sort-bibtex-matches 'year)] 6833 :style radio :selected (eq reftex-sort-bibtex-matches 'year)]
5965 ["by Year, reversed" (setq reftex-sort-bibtex-matches 'reverse-year) 6834 ["by Year, reversed" (setq reftex-sort-bibtex-matches 'reverse-year)
5966 :style radio :selected (eq reftex-sort-bibtex-matches 'reverse-year)] 6835 :style radio :selected (eq reftex-sort-bibtex-matches 'reverse-year)]
5967 ["Not" (setq reftex-sort-bibtex-matches nil) 6836 ["Not" (setq reftex-sort-bibtex-matches nil)
5968 :style radio :selected (eq reftex-sort-bibtex-matches nil)]) 6837 :style radio :selected (eq reftex-sort-bibtex-matches nil)])
6838 ("Crossref Viewing"
6839 ["Automatic Info in Echo Area" reftex-toggle-auto-view-crossref
6840 :style toggle :selected reftex-auto-view-crossref-timer]
6841 ["Crossref Echo may visit files"
6842 (setq reftex-revisit-to-echo (not reftex-revisit-to-echo))
6843 :style toggle :selected reftex-revisit-to-echo]
6844 ["Cache Echo strings for \cite"
6845 (setq reftex-cache-cite-echo (not reftex-cache-cite-echo))
6846 :style toggle :selected reftex-cache-cite-echo])
6847 ("Parser"
6848 "Document Scans"
6849 ["Partial Scans"
6850 (setq reftex-enable-partial-scans (not reftex-enable-partial-scans))
6851 :style toggle :selected reftex-enable-partial-scans]
6852 ["Auto-Save Parse Info"
6853 (setq reftex-save-parse-info (not reftex-save-parse-info))
6854 :style toggle :selected reftex-save-parse-info]
6855 ["Automatic Rescans"
6856 (setq reftex-allow-automatic-rescan (not reftex-allow-automatic-rescan))
6857 :style toggle :selected reftex-allow-automatic-rescan]
6858 "---"
6859 "Temporary Buffers"
6860 ["Keep Buffers"
6861 (setq reftex-keep-temporary-buffers (not reftex-keep-temporary-buffers))
6862 :style toggle :selected reftex-keep-temporary-buffers]
6863 ["Initialize when Visiting"
6864 (setq reftex-initialize-temporary-buffers
6865 (not reftex-initialize-temporary-buffers))
6866 :style toggle :selected reftex-initialize-temporary-buffers])
6867 ("AUC TeX"
6868 ["Plug into AUC TeX" reftex-toggle-plug-into-AUCTeX
6869 :style toggle :selected reftex-plug-into-AUCTeX])
6870 ("Fontification"
6871 ["Use Fontification" (setq reftex-use-fonts (not reftex-use-fonts))
6872 :style toggle :selected reftex-use-fonts]
6873 ["Fontify Context Display" (setq reftex-refontify-context
6874 (not (reftex-refontify)))
6875 :style toggle :selected (reftex-refontify)]))
6876 ;;"---"
5969 ("Customize" 6877 ("Customize"
5970 ["Browse RefTeX group" reftex-customize t] 6878 ["Browse RefTeX group" reftex-customize t]
5971 "---" 6879 "---"
5972 ["Build Full Customize Menu" reftex-create-customize-menu 6880 ["Build Full Customize Menu" reftex-create-customize-menu
5973 (fboundp 'customize-menu-create)]) 6881 (fboundp 'customize-menu-create)])
5974 "----" 6882 "---"
5975 ("Documentation" 6883 ("Documentation"
5976 ["Info" reftex-info t] 6884 ["Info" reftex-info t]
5977 ["Commentary" reftex-show-commentary t]))) 6885 ["Commentary" reftex-show-commentary t])))
5978 6886
6887(defun reftex-customize ()
6888 "Call the customize function with reftex as argument."
6889 (interactive)
6890 (customize-browse 'reftex))
6891
6892(defun reftex-create-customize-menu ()
6893 "Create a full customization menu for RefTeX, insert it into the menu."
6894 (interactive)
6895 (if (fboundp 'customize-menu-create)
6896 (progn
6897 (easy-menu-change
6898 '("Ref") "Customize"
6899 `(["Browse RefTeX group" reftex-customize t]
6900 "---"
6901 ,(customize-menu-create 'reftex)
6902 ["Set" Custom-set t]
6903 ["Save" Custom-save t]
6904 ["Reset to Current" Custom-reset-current t]
6905 ["Reset to Saved" Custom-reset-saved t]
6906 ["Reset to Standard Settings" Custom-reset-standard t]))
6907 (message "\"Ref\"-menu now contains full customization menu"))
6908 (error "Cannot expand menu (outdated version of cus-edit.el)")))
6909
6910(defun reftex-show-commentary ()
6911 "Use the finder to view the file documentation from `reftex.el'."
6912 (interactive)
6913 (require 'finder)
6914 (finder-commentary "reftex.el"))
6915
6916(defun reftex-info ()
6917 "Read documentation for RefTeX in the info system."
6918 (interactive)
6919 (require 'info)
6920 (Info-goto-node "(reftex)"))
6921
6922;; Support for \label and \ref --------------------------------------
6923
6924;;; Install the kill-buffer and kill-emacs hooks ------------------------------
6925
6926(add-hook 'kill-buffer-hook 'reftex-kill-buffer-hook)
6927(add-hook 'kill-emacs-hook 'reftex-kill-emacs-hook)
6928
6929;;; Install the idle timer if requested ---------------------------------------
6930
6931(and reftex-auto-view-crossref
6932 (not reftex-auto-view-crossref-timer)
6933 (reftex-toggle-auto-view-crossref))
6934
5979;;; Run Hook ------------------------------------------------------------------ 6935;;; Run Hook ------------------------------------------------------------------
5980 6936
5981(run-hooks 'reftex-load-hook) 6937(run-hooks 'reftex-load-hook)
@@ -5988,3 +6944,4 @@ This enforces rescanning the buffer on next use."
5988;;;============================================================================ 6944;;;============================================================================
5989 6945
5990;;; reftex.el ends here 6946;;; reftex.el ends here
6947