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