Commit | Line | Data |
---|---|---|
1a9461d0 | 1 | ;;; reftex-ref.el - Code to create labels and references with RefTeX |
9f286482 | 2 | ;; Copyright (c) 1997, 1998, 1999, 2000 Free Software Foundation, Inc. |
3ba2590f RS |
3 | |
4 | ;; Author: Carsten Dominik <dominik@strw.LeidenUniv.nl> | |
d8fb2015 | 5 | ;; Version: 4.14 |
9f286482 | 6 | ;; |
3ba2590f RS |
7 | |
8 | ;; This file is part of GNU Emacs. | |
9 | ||
10 | ;; GNU Emacs is free software; you can redistribute it and/or modify | |
11 | ;; it under the terms of the GNU General Public License as published by | |
12 | ;; the Free Software Foundation; either version 2, or (at your option) | |
13 | ;; any later version. | |
14 | ||
15 | ;; GNU Emacs is distributed in the hope that it will be useful, | |
16 | ;; but WITHOUT ANY WARRANTY; without even the implied warranty of | |
17 | ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
18 | ;; GNU General Public License for more details. | |
19 | ||
20 | ;; You should have received a copy of the GNU General Public License | |
21 | ;; along with GNU Emacs; see the file COPYING. If not, write to the | |
22 | ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330, | |
23 | ;; Boston, MA 02111-1307, USA. | |
1a9461d0 | 24 | |
7c4d13cc | 25 | (eval-when-compile (require 'cl)) |
1a9461d0 CD |
26 | (provide 'reftex-ref) |
27 | (require 'reftex) | |
28 | ;;; | |
29 | ||
30 | (defun reftex-label-location (&optional bound) | |
31 | "Return the environment or macro which determines the label type at point. | |
32 | If optional BOUND is an integer, limit backward searches to that point." | |
33 | ||
34 | (let* ((loc1 (reftex-what-macro reftex-label-mac-list bound)) | |
35 | (loc2 (reftex-what-environment reftex-label-env-list bound)) | |
36 | (loc3 (reftex-what-special-env 1 bound)) | |
37 | (p1 (or (cdr loc1) 0)) | |
38 | (p2 (or (cdr loc2) 0)) | |
39 | (p3 (or (cdr loc3) 0)) | |
40 | (pmax (max p1 p2 p3))) | |
41 | ||
42 | (setq reftex-location-start pmax) | |
43 | (cond | |
44 | ((= p1 pmax) | |
45 | ;; A macro. Default context after macro name. | |
46 | (setq reftex-default-context-position (+ p1 (length (car loc1)))) | |
47 | (or (car loc1) "section")) | |
48 | ((= p2 pmax) | |
49 | ;; An environment. Default context after \begin{name}. | |
50 | (setq reftex-default-context-position (+ p2 8 (length (car loc2)))) | |
51 | (or (car loc2) "section")) | |
52 | ((= p3 pmax) | |
53 | ;; A special. Default context right there. | |
54 | (setq reftex-default-context-position p3) | |
55 | (setq loc3 (car loc3)) | |
56 | (cond ((null loc3) "section") | |
57 | ((symbolp loc3) (symbol-name loc3)) | |
58 | ((stringp loc3) loc3) | |
59 | (t "section"))) | |
60 | (t ;; This should not happen, I think? | |
61 | "section")))) | |
62 | ||
63 | (defun reftex-label-info-update (cell) | |
64 | ;; Update information about just one label in a different file. | |
65 | ;; CELL contains the old info list | |
66 | (let* ((label (nth 0 cell)) | |
67 | (typekey (nth 1 cell)) | |
68 | ;; (text (nth 2 cell)) | |
69 | (file (nth 3 cell)) | |
70 | (comment (nth 4 cell)) | |
71 | (note (nth 5 cell)) | |
72 | (buf (reftex-get-file-buffer-force | |
73 | file (not (eq t reftex-keep-temporary-buffers))))) | |
74 | (if (not buf) | |
75 | (list label typekey "" file comment "LOST LABEL. RESCAN TO FIX.") | |
76 | (save-excursion | |
77 | (set-buffer buf) | |
78 | (save-restriction | |
79 | (widen) | |
80 | (goto-char 1) | |
81 | ||
82 | (if (or (re-search-forward | |
83 | (format reftex-find-label-regexp-format | |
84 | (regexp-quote label)) nil t) | |
85 | (re-search-forward | |
86 | (format reftex-find-label-regexp-format2 | |
87 | (regexp-quote label)) nil t)) | |
88 | ||
89 | (progn | |
90 | (backward-char 1) | |
91 | (append (reftex-label-info label file) (list note))) | |
92 | (list label typekey "" file "LOST LABEL. RESCAN TO FIX."))))))) | |
93 | ||
94 | (defun reftex-label-info (label &optional file bound derive env-or-mac) | |
95 | ;; Return info list on LABEL at point. | |
96 | (let* ((env-or-mac (or env-or-mac (reftex-label-location bound))) | |
97 | (typekey (nth 1 (assoc env-or-mac reftex-env-or-mac-alist))) | |
98 | (file (or file (buffer-file-name))) | |
99 | (parse (nth 2 (assoc env-or-mac reftex-env-or-mac-alist))) | |
100 | (text (reftex-short-context env-or-mac parse reftex-location-start | |
101 | derive)) | |
102 | (in-comment (reftex-in-comment))) | |
103 | (list label typekey text file in-comment))) | |
104 | ||
105 | ;;; Creating labels --------------------------------------------------------- | |
106 | ||
107 | (defun reftex-label (&optional environment no-insert) | |
108 | "Insert a unique label. Return the label. | |
109 | If ENVIRONMENT is given, don't bother to find out yourself. | |
110 | If NO-INSERT is non-nil, do not insert label into buffer. | |
111 | With prefix arg, force to rescan document first. | |
112 | When you are prompted to enter or confirm a label, and you reply with | |
113 | just the prefix or an empty string, no label at all will be inserted. | |
114 | A new label is also recorded into the label list. | |
115 | This function is controlled by the settings of reftex-insert-label-flags." | |
116 | ||
117 | (interactive) | |
118 | ||
119 | ;; Ensure access to scanning info and rescan buffer if prefix are is '(4). | |
120 | (reftex-access-scan-info current-prefix-arg) | |
121 | ||
122 | ;; Find out what kind of environment this is and abort if necessary. | |
123 | (if (or (not environment) | |
124 | (not (assoc environment reftex-env-or-mac-alist))) | |
125 | (setq environment (reftex-label-location))) | |
126 | (unless environment | |
127 | (error "Can't figure out what kind of label should be inserted")) | |
128 | ||
129 | ;; Ok, go ahead. | |
130 | (catch 'exit | |
131 | (let* ((entry (assoc environment reftex-env-or-mac-alist)) | |
132 | (typekey (nth 1 entry)) | |
133 | (format (nth 3 entry)) | |
134 | (macro-cell (reftex-what-macro 1)) | |
135 | (entry1 (assoc (car macro-cell) reftex-env-or-mac-alist)) | |
136 | label naked prefix valid default force-prompt rescan-is-useful) | |
137 | (when (and (or (nth 5 entry) (nth 5 entry1)) | |
138 | (memq (preceding-char) '(?\[ ?\{))) | |
139 | ;; This is an argument of a label macro. Insert naked label. | |
140 | (setq naked t format "%s")) | |
141 | ||
142 | (setq prefix (or (cdr (assoc typekey reftex-typekey-to-prefix-alist)) | |
143 | (concat typekey "-"))) | |
144 | ;; Replace any escapes in the prefix | |
145 | (setq prefix (reftex-replace-prefix-escapes prefix)) | |
146 | ||
147 | ;; Make a default label. | |
148 | (cond | |
149 | ||
150 | ((reftex-typekey-check typekey (nth 0 reftex-insert-label-flags)) | |
151 | ;; Derive a label from context. | |
152 | (setq reftex-active-toc (reftex-last-assoc-before-elt | |
153 | 'toc (car (reftex-where-am-I)) | |
154 | (symbol-value reftex-docstruct-symbol))) | |
155 | (setq default (reftex-no-props | |
156 | (nth 2 (reftex-label-info " " nil nil t)))) | |
157 | ;; Catch the cases where the is actually no context available. | |
158 | (if (or (string-match "NO MATCH FOR CONTEXT REGEXP" default) | |
159 | (string-match "ILLEGAL VALUE OF PARSE" default) | |
160 | (string-match "SECTION HEADING NOT FOUND" default) | |
161 | (string-match "HOOK ERROR" default) | |
162 | (string-match "^[ \t]*$" default)) | |
163 | (setq default prefix | |
164 | force-prompt t) ; need to prompt | |
165 | (setq default | |
166 | (concat prefix | |
167 | (funcall reftex-string-to-label-function default))) | |
168 | ||
169 | ;; Make it unique. | |
170 | (setq default (reftex-uniquify-label default nil "-")))) | |
171 | ||
172 | ((reftex-typekey-check typekey (nth 1 reftex-insert-label-flags)) | |
173 | ;; Minimal default: the user will be prompted. | |
174 | (setq default prefix)) | |
175 | ||
176 | (t | |
177 | ;; Make an automatic label. | |
178 | (setq default (reftex-uniquify-label prefix t)))) | |
179 | ||
180 | ;; Should we ask the user? | |
181 | (if (or (reftex-typekey-check typekey | |
182 | (nth 1 reftex-insert-label-flags)) ; prompt | |
183 | force-prompt) | |
184 | ||
185 | (while (not valid) | |
186 | ;; iterate until we get a legal label | |
187 | ||
188 | (setq label (read-string | |
189 | (if naked "Naked Label: " "Label: ") | |
190 | default)) | |
191 | ||
192 | ;; Lets make sure that this is a legal label | |
193 | (cond | |
194 | ||
195 | ((string-match (concat "\\`\\(" (regexp-quote prefix) | |
196 | "\\)?[ \t]*\\'") | |
197 | label) | |
198 | ;; No label at all, please | |
199 | (message "No label inserted.") | |
200 | (throw 'exit nil)) | |
201 | ||
202 | ;; Test if label contains strange characters | |
203 | ((string-match reftex-label-illegal-re label) | |
204 | (message "Label \"%s\" contains illegal characters" label) | |
205 | (ding) | |
206 | (sit-for 2)) | |
207 | ||
208 | ;; Look it up in the label list | |
209 | ((setq entry (assoc label | |
210 | (symbol-value reftex-docstruct-symbol))) | |
211 | (ding) | |
212 | (if (y-or-n-p | |
213 | (format "Label '%s' exists. Use anyway? " label)) | |
214 | (setq valid t))) | |
215 | ||
216 | ;; Label is ok | |
217 | (t | |
218 | (setq valid t)))) | |
219 | (setq label default)) | |
220 | ||
221 | ;; Insert the label into the label list | |
222 | (let* ((here-I-am-info | |
223 | (save-excursion | |
224 | (if (and (or naked no-insert) | |
225 | (integerp (cdr macro-cell))) | |
226 | (goto-char (cdr macro-cell))) | |
227 | (reftex-where-am-I))) | |
228 | (here-I-am (car here-I-am-info)) | |
229 | (note (if (cdr here-I-am-info) | |
230 | "" | |
231 | "POSITION UNCERTAIN. RESCAN TO FIX.")) | |
232 | (file (buffer-file-name)) | |
233 | (text nil) | |
234 | (tail (memq here-I-am (symbol-value reftex-docstruct-symbol)))) | |
235 | ||
236 | (or (cdr here-I-am-info) (setq rescan-is-useful t)) | |
237 | ||
238 | (when tail | |
239 | (push (list label typekey text file nil note) (cdr tail)) | |
240 | (put reftex-docstruct-symbol 'modified t))) | |
241 | ||
242 | ;; Insert the label into the buffer | |
243 | (unless no-insert | |
244 | (insert | |
245 | (if reftex-format-label-function | |
246 | (funcall reftex-format-label-function label format) | |
247 | (format format label))) | |
248 | (if (and reftex-plug-into-AUCTeX | |
249 | (fboundp 'LaTeX-add-labels)) | |
250 | ;; Tell AUCTeX about this | |
251 | (LaTeX-add-labels label))) | |
252 | ||
253 | ;; Delete the corresponding selection buffers to force update on next use. | |
254 | (when reftex-auto-update-selection-buffers | |
255 | (reftex-erase-buffer (reftex-make-selection-buffer-name typekey)) | |
256 | (reftex-erase-buffer (reftex-make-selection-buffer-name " "))) | |
257 | ||
258 | (when (and rescan-is-useful reftex-allow-automatic-rescan) | |
259 | (reftex-parse-one)) | |
260 | ||
261 | ;; return value of the function is the label | |
262 | label))) | |
263 | ||
264 | (defun reftex-string-to-label (string) | |
265 | "Convert a string (a sentence) to a label. | |
266 | Uses `reftex-derive-label-parameters' and `reftex-label-illegal-re'. It | |
267 | also applies `reftex-translate-to-ascii-function' to the string." | |
268 | (when (and reftex-translate-to-ascii-function | |
269 | (fboundp reftex-translate-to-ascii-function)) | |
270 | (setq string (funcall reftex-translate-to-ascii-function string))) | |
271 | (apply 'reftex-convert-string string | |
272 | "[-~ \t\n\r,;]+" reftex-label-illegal-re nil nil | |
273 | reftex-derive-label-parameters)) | |
274 | ||
275 | (defun reftex-latin1-to-ascii (string) | |
276 | ;; Translate the upper 128 chars in the Latin-1 charset to ASCII equivalents | |
277 | (let ((tab "@@@@@@@@@@@@@@@@@@'@@@@@@@@@@@@@ icLxY|S\"ca<--R-o|23'uq..1o>423?AAAAAAACEEEEIIIIDNOOOOOXOUUUUYP3aaaaaaaceeeeiiiidnooooo:ouuuuypy") | |
278 | (emacsp (not (featurep 'xemacs)))) | |
279 | (mapconcat | |
280 | (lambda (c) | |
281 | (cond ((and (> c 127) (< c 256)) ; 8 bit Latin-1 | |
282 | (char-to-string (aref tab (- c 128)))) | |
283 | ((and emacsp ; Not for XEmacs | |
284 | (> c 2175) (< c 2304)) ; Mule Latin-1 | |
285 | (char-to-string (aref tab (- c 2176)))) | |
286 | (t (char-to-string c)))) | |
287 | string ""))) | |
288 | ||
289 | (defun reftex-replace-prefix-escapes (prefix) | |
290 | ;; Replace %escapes in a label prefix | |
291 | (save-match-data | |
292 | (let (letter (num 0) replace) | |
293 | (while (string-match "\\%\\([a-zA-Z]\\)" prefix num) | |
294 | (setq letter (match-string 1 prefix)) | |
295 | (setq replace | |
296 | (cond | |
297 | ((equal letter "f") | |
298 | (file-name-sans-extension | |
299 | (file-name-nondirectory (buffer-file-name)))) | |
300 | ((equal letter "F") | |
301 | (let ((masterdir (file-name-directory (reftex-TeX-master-file))) | |
302 | (file (file-name-sans-extension (buffer-file-name)))) | |
303 | (if (string-match (concat "\\`" (regexp-quote masterdir)) | |
304 | file) | |
305 | (substring file (length masterdir)) | |
306 | file))) | |
307 | ((equal letter "u") | |
308 | (or (user-login-name) "")) | |
309 | ((equal letter "S") | |
8b12dca9 | 310 | (let* (macro level-exp level) |
1a9461d0 CD |
311 | (save-excursion |
312 | (save-match-data | |
313 | (when (re-search-backward reftex-section-regexp nil t) | |
314 | (setq macro (reftex-match-string 2) | |
8b12dca9 CD |
315 | level-exp (cdr (assoc macro reftex-section-levels-all)) |
316 | level (if (symbolp level-exp) | |
317 | (abs (save-match-data | |
318 | (funcall level-exp))) | |
319 | (abs level-exp)))) | |
1a9461d0 CD |
320 | (cdr (or (assoc macro reftex-section-prefixes) |
321 | (assoc level reftex-section-prefixes) | |
322 | (assq t reftex-section-prefixes) | |
323 | (list t "sec:"))))))) | |
324 | (t ""))) | |
325 | (setq num (1- (+ (match-beginning 1) (length replace))) | |
326 | prefix (replace-match replace nil nil prefix))) | |
327 | prefix))) | |
328 | ||
329 | (defun reftex-uniquify-label (label &optional force separator) | |
330 | ;; Make label unique by appending a number. | |
331 | ;; Optional FORCE means, force appending a number, even if label is unique. | |
332 | ;; Optional SEPARATOR is a string to stick between label and number. | |
333 | ||
334 | ;; Ensure access to scanning info | |
335 | (reftex-access-scan-info) | |
336 | ||
337 | (cond | |
338 | ((and (not force) | |
339 | (not (assoc label (symbol-value reftex-docstruct-symbol)))) | |
340 | label) | |
341 | (t | |
342 | (let* ((label-numbers (assq 'label-numbers | |
343 | (symbol-value reftex-docstruct-symbol))) | |
344 | (label-numbers-alist (cdr label-numbers)) | |
345 | (cell (or (assoc label label-numbers-alist) | |
346 | (car (setcdr label-numbers | |
347 | (cons (cons label 0) | |
348 | label-numbers-alist))))) | |
349 | (num (1+ (cdr cell))) | |
350 | (sep (or separator ""))) | |
351 | (while (assoc (concat label sep (int-to-string num)) | |
352 | (symbol-value reftex-docstruct-symbol)) | |
353 | (incf num)) | |
354 | (setcdr cell num) | |
355 | (concat label sep (int-to-string num)))))) | |
356 | ||
357 | ;;; Referencing labels ------------------------------------------------------ | |
358 | ||
359 | ;; Help string for the reference label menu | |
360 | (defconst reftex-select-label-prompt | |
361 | "Select: [n]ext [p]revious [r]escan [ ]context e[x]tern [q]uit RET [?]HELP+more") | |
362 | ||
363 | (defconst reftex-select-label-help | |
364 | " n / p Go to next/previous label (Cursor motion works as well) | |
365 | C-c C-n/p Go to next/previous section heading. | |
366 | b / l Jump back to previous selection / Reuse last referenced label. | |
367 | g / s Update menu / Switch label type. | |
368 | r / C-u r Reparse document / Reparse entire document. | |
369 | x Switch to label menu of external document (with LaTeX package `xr'). | |
370 | F t c Toggle: [F]ile borders, [t]able of contents, [c]ontext | |
371 | # % Toggle: [#] label counters, [%] labels in comments | |
372 | SPC / f Show full context in other window / Toggle follow mode. | |
373 | . Show insertion point in other window. | |
374 | v / V Toggle \\ref <-> \\vref / Rotate \\ref <=> \\fref <=> \\Fref | |
375 | TAB Enter a label with completion. | |
376 | m , - + Mark entry. `,-+' also assign a separator. | |
377 | a / A Put all marked entries into one/many \\ref commands. | |
378 | q / RET Quit without referencing / Accept current label (also on mouse-2).") | |
379 | ||
380 | (defun reftex-reference (&optional type no-insert cut) | |
381 | "Make a LaTeX reference. Look only for labels of a certain TYPE. | |
382 | With prefix arg, force to rescan buffer for labels. This should only be | |
383 | necessary if you have recently entered labels yourself without using | |
384 | reftex-label. Rescanning of the buffer can also be requested from the | |
385 | label selection menu. | |
386 | The function returns the selected label or nil. | |
387 | If NO-INSERT is non-nil, do not insert \\ref command, just return label. | |
388 | When called with 2 C-u prefix args, disable magic word recognition." | |
389 | ||
390 | (interactive) | |
391 | ||
392 | ;; check for active recursive edits | |
393 | (reftex-check-recursive-edit) | |
394 | ||
395 | ;; Ensure access to scanning info and rescan buffer if prefix are is '(4) | |
396 | (reftex-access-scan-info current-prefix-arg) | |
397 | ||
398 | (unless type | |
399 | ;; guess type from context | |
400 | (if (and reftex-guess-label-type | |
401 | (setq type (reftex-guess-label-type))) | |
402 | (setq cut (cdr type) | |
403 | type (car type)) | |
404 | (setq type (reftex-query-label-type)))) | |
405 | ||
406 | (let* ((refstyle | |
407 | (cond ((reftex-typekey-check type reftex-vref-is-default) "\\vref") | |
408 | ((reftex-typekey-check type reftex-fref-is-default) "\\fref") | |
409 | (t "\\ref"))) | |
410 | (reftex-format-ref-function reftex-format-ref-function) | |
411 | (form "\\ref{%s}") | |
412 | label labels sep sep1) | |
413 | ||
414 | ;; Have the user select a label | |
415 | (set-marker reftex-select-return-marker (point)) | |
416 | (setq labels (save-excursion | |
417 | (reftex-offer-label-menu type))) | |
418 | (reftex-ensure-compiled-variables) | |
419 | (set-marker reftex-select-return-marker nil) | |
420 | ;; If the first entry is the symbol 'concat, concat all all labels. | |
421 | ;; We keep the cdr of the first label for typekey etc information. | |
422 | (if (eq (car labels) 'concat) | |
423 | (setq labels (list (list (mapconcat 'car (cdr labels) ",") | |
424 | (cdr (nth 1 labels)))))) | |
425 | (setq type (nth 1 (car labels)) | |
426 | form (or (cdr (assoc type reftex-typekey-to-format-alist)) | |
427 | form)) | |
428 | ||
429 | (cond | |
430 | (no-insert | |
431 | ;; Just return the first label | |
432 | (car (car labels))) | |
433 | ((null labels) | |
434 | (message "Quit") | |
435 | nil) | |
436 | (t | |
437 | (while labels | |
438 | (setq label (car (car labels)) | |
439 | sep (nth 2 (car labels)) | |
440 | sep1 (cdr (assoc sep reftex-multiref-punctuation)) | |
441 | labels (cdr labels)) | |
442 | (when cut | |
443 | (backward-delete-char cut) | |
444 | (setq cut nil)) | |
445 | ||
446 | ;; remove ~ if we do already have a space | |
447 | (when (and (= ?~ (string-to-char form)) | |
448 | (member (preceding-char) '(?\ ?\t ?\n))) | |
449 | (setq form (substring form 1))) | |
450 | ;; do we have a special format? | |
451 | (setq reftex-format-ref-function | |
452 | (cond | |
453 | ((string= refstyle "\\vref") 'reftex-format-vref) | |
454 | ((string= refstyle "\\fref") 'reftex-format-fref) | |
455 | ((string= refstyle "\\Fref") 'reftex-format-Fref) | |
456 | (t reftex-format-ref-function))) | |
457 | ;; ok, insert the reference | |
458 | (if sep1 (insert sep1)) | |
459 | (insert | |
460 | (if reftex-format-ref-function | |
461 | (funcall reftex-format-ref-function label form) | |
462 | (format form label label))) | |
463 | ;; take out the initial ~ for good | |
464 | (and (= ?~ (string-to-char form)) | |
465 | (setq form (substring form 1)))) | |
466 | (message "") | |
467 | label)))) | |
468 | ||
469 | (defun reftex-guess-label-type () | |
470 | ;; Examine context to guess what a \ref might want to reference. | |
471 | (let ((words reftex-words-to-typekey-alist) | |
472 | (case-fold-search t) | |
473 | (bound (max (point-min) (- (point) 35))) | |
474 | matched cell) | |
475 | (save-excursion | |
476 | (while (and (setq cell (pop words)) | |
477 | (not (setq matched | |
478 | (re-search-backward (car cell) bound t)))))) | |
479 | (if matched | |
480 | (cons (cdr cell) (- (match-end 0) (match-end 1))) | |
481 | nil))) | |
482 | ||
483 | (defvar reftex-select-label-map) | |
484 | (defun reftex-offer-label-menu (typekey) | |
485 | ;; Offer a menu with the appropriate labels. | |
486 | (let* ((buf (current-buffer)) | |
487 | (xr-data (assq 'xr (symbol-value reftex-docstruct-symbol))) | |
488 | (xr-alist (cons (cons "" (buffer-file-name)) (nth 1 xr-data))) | |
489 | (xr-index 0) | |
490 | (here-I-am (car (reftex-where-am-I))) | |
491 | (here-I-am1 here-I-am) | |
492 | (toc (reftex-typekey-check typekey reftex-label-menu-flags 0)) | |
493 | (files (reftex-typekey-check typekey reftex-label-menu-flags 7)) | |
494 | (context (not (reftex-typekey-check | |
495 | typekey reftex-label-menu-flags 3))) | |
496 | (counter (reftex-typekey-check | |
497 | typekey reftex-label-menu-flags 2)) | |
498 | (follow (reftex-typekey-check | |
499 | typekey reftex-label-menu-flags 4)) | |
500 | (commented (nth 5 reftex-label-menu-flags)) | |
501 | (prefix "") | |
502 | selection-buffers | |
503 | offset rtn key data last-data entries) | |
504 | ||
505 | (unwind-protect | |
506 | (catch 'exit | |
507 | (while t | |
508 | (save-window-excursion | |
509 | (delete-other-windows) | |
510 | (setq reftex-call-back-to-this-buffer buf | |
511 | reftex-latex-syntax-table (syntax-table)) | |
512 | (let ((default-major-mode 'reftex-select-label-mode)) | |
513 | (if reftex-use-multiple-selection-buffers | |
514 | (switch-to-buffer-other-window | |
515 | (save-excursion | |
516 | (set-buffer buf) | |
517 | (reftex-make-selection-buffer-name typekey))) | |
518 | (switch-to-buffer-other-window "*RefTeX Select*") | |
519 | (reftex-erase-buffer))) | |
520 | (unless (eq major-mode 'reftex-select-label-mode) | |
521 | (reftex-select-label-mode)) | |
522 | (add-to-list 'selection-buffers (current-buffer)) | |
523 | (setq truncate-lines t) | |
524 | (setq mode-line-format | |
525 | (list "---- " 'mode-line-buffer-identification | |
526 | " " 'global-mode-string " (" mode-name ")" | |
527 | " S<" 'refstyle ">" | |
528 | " -%-")) | |
529 | (cond | |
530 | ((= 0 (buffer-size)) | |
531 | (let ((buffer-read-only nil)) | |
532 | (message "Creating Selection Buffer...") | |
533 | (setq offset (reftex-insert-docstruct | |
534 | buf | |
535 | toc | |
536 | typekey | |
537 | nil ; index | |
538 | files | |
539 | context | |
540 | counter | |
541 | commented | |
542 | (or here-I-am offset) | |
543 | prefix | |
544 | nil ; no a toc buffer | |
545 | )))) | |
546 | (here-I-am | |
547 | (setq offset (reftex-get-offset buf here-I-am typekey))) | |
548 | (t (setq offset t))) | |
549 | (setq buffer-read-only t) | |
550 | (setq offset (or offset t)) | |
551 | ||
552 | (setq here-I-am nil) ; turn off determination of offset | |
553 | (setq rtn | |
554 | (reftex-select-item | |
555 | reftex-select-label-prompt | |
556 | reftex-select-label-help | |
557 | reftex-select-label-map | |
558 | offset | |
559 | 'reftex-show-label-location follow)) | |
560 | (setq key (car rtn) | |
561 | data (nth 1 rtn) | |
562 | last-data (nth 2 rtn) | |
563 | offset t) | |
564 | (unless key (throw 'exit nil)) | |
565 | (cond | |
566 | ((eq key ?g) | |
567 | ;; update buffer | |
568 | (reftex-erase-buffer)) | |
569 | ((or (eq key ?r) | |
570 | (eq key ?R)) | |
571 | ;; rescan buffer | |
572 | (and current-prefix-arg (setq key ?R)) | |
573 | (reftex-erase-buffer) | |
574 | (reftex-reparse-document buf last-data key)) | |
575 | ((eq key ?c) | |
576 | ;; toggle context mode | |
577 | (reftex-erase-buffer) | |
578 | (setq context (not context))) | |
579 | ((eq key ?s) | |
580 | ;; switch type | |
581 | (setq here-I-am here-I-am1) | |
582 | (setq typekey (reftex-query-label-type))) | |
583 | ((eq key ?t) | |
7c4d13cc | 584 | ;; toggle table of contents display, or change depth |
1a9461d0 | 585 | (reftex-erase-buffer) |
7c4d13cc CD |
586 | (if current-prefix-arg |
587 | (setq reftex-toc-max-level (prefix-numeric-value | |
588 | current-prefix-arg)) | |
589 | (setq toc (not toc)))) | |
1a9461d0 CD |
590 | ((eq key ?F) |
591 | ;; toggle display of included file borders | |
592 | (reftex-erase-buffer) | |
593 | (setq files (not files))) | |
594 | ((eq key ?#) | |
595 | ;; toggle counter display | |
596 | (reftex-erase-buffer) | |
597 | (setq counter (not counter))) | |
598 | ((eq key ?%) | |
599 | ;; toggle display of commented labels | |
600 | (reftex-erase-buffer) | |
601 | (setq commented (not commented))) | |
602 | ((eq key ?l) | |
603 | ;; reuse the last referenced label again | |
604 | (setq entries reftex-last-used-reference) | |
605 | (throw 'exit t)) | |
606 | ((eq key ?x) | |
607 | ;; select an external document | |
608 | (setq xr-index (reftex-select-external-document | |
609 | xr-alist xr-index)) | |
610 | (setq buf (or (reftex-get-file-buffer-force | |
611 | (cdr (nth xr-index xr-alist))) | |
612 | (error "Cannot switch document")) | |
613 | prefix (or (car (nth xr-index xr-alist)) "")) | |
614 | (set-buffer buf) | |
615 | (reftex-access-scan-info)) | |
616 | ((stringp key) | |
617 | (setq entries | |
618 | (list | |
619 | (list | |
620 | (or (assoc key (symbol-value reftex-docstruct-symbol)) | |
621 | (list key typekey))))) | |
622 | (throw 'exit t)) | |
623 | ((memq key '(?a ?A return)) | |
624 | (cond | |
625 | (reftex-select-marked | |
626 | (setq entries (nreverse reftex-select-marked))) | |
627 | (data | |
628 | (setq entries (list (list data)))) | |
629 | (t (setq entries nil))) | |
630 | (when entries | |
631 | (if (equal key ?a) (push 'concat entries)) | |
632 | (setq reftex-last-used-reference entries)) | |
633 | (set-buffer buf) | |
634 | (throw 'exit t)) | |
635 | (t (error "This should not happen (reftex-offer-label-menu)")))))) | |
636 | (save-excursion | |
637 | (while reftex-buffers-with-changed-invisibility | |
638 | (set-buffer (car (car reftex-buffers-with-changed-invisibility))) | |
639 | (setq buffer-invisibility-spec | |
640 | (cdr (pop reftex-buffers-with-changed-invisibility))))) | |
641 | (mapcar (lambda (buf) (and (buffer-live-p buf) (bury-buffer buf))) | |
642 | selection-buffers) | |
643 | (reftex-kill-temporary-buffers)) | |
644 | ;; Add the prefixes, put together the relevant information in the form | |
645 | ;; (LABEL TYPEKEY SEPERATOR) and return a list of those. | |
646 | (mapcar (lambda (x) | |
647 | (if (listp x) | |
648 | (list (concat prefix (car (car x))) | |
649 | (nth 1 (car x)) | |
650 | (nth 2 x)) | |
651 | x)) | |
652 | entries))) | |
653 | ||
654 | (defun reftex-reparse-document (&optional buffer data key) | |
655 | ;; Rescan the document. | |
656 | (save-window-excursion | |
657 | (save-excursion | |
658 | (if buffer | |
659 | (if (not (bufferp buffer)) | |
660 | (error "No such buffer %s" (buffer-name buffer)) | |
661 | (set-buffer buffer))) | |
662 | (let ((arg (if (eq key ?R) '(16) '(4))) | |
663 | (file (nth 3 data))) | |
664 | (reftex-access-scan-info arg file))))) | |
665 | ||
666 | (defun reftex-query-label-type () | |
667 | ;; Ask for label type | |
668 | (let ((key (reftex-select-with-char | |
669 | reftex-type-query-prompt reftex-type-query-help 3))) | |
670 | (unless (member (char-to-string key) reftex-typekey-list) | |
671 | (error "No such label type: %s" (char-to-string key))) | |
672 | (char-to-string key))) | |
673 | ||
674 | (defun reftex-show-label-location (data forward no-revisit | |
675 | &optional stay error) | |
676 | ;; View the definition site of a label in another window. | |
677 | ;; DATA is an entry from the docstruct list. | |
678 | ;; FORWARD indicates if the label is likely forward from current point. | |
679 | ;; NO-REVISIT means do not load a file to show this label. | |
680 | ;; STAY means leave the new window selected. | |
681 | ;; ERROR means throw an error exception when the label cannot be found. | |
682 | ;; If ERROR is nil, the return value of this function indicates success. | |
683 | (let* ((this-window (selected-window)) | |
684 | (errorf (if error 'error 'message)) | |
685 | label file buffer re found) | |
686 | ||
687 | (catch 'exit | |
688 | (setq label (nth 0 data) | |
689 | file (nth 3 data)) | |
690 | ||
691 | (unless file | |
692 | (funcall errorf "Unknown label - reparse might help") | |
693 | (throw 'exit nil)) | |
694 | ||
695 | ;; Goto the file in another window | |
696 | (setq buffer | |
697 | (if no-revisit | |
698 | (reftex-get-buffer-visiting file) | |
699 | (reftex-get-file-buffer-force | |
700 | file (not reftex-keep-temporary-buffers)))) | |
701 | (if buffer | |
702 | ;; good - the file is available | |
703 | (switch-to-buffer-other-window buffer) | |
704 | ;; we have got a problem here. The file does not exist. | |
705 | ;; Let' get out of here.. | |
706 | (funcall errorf "Label %s not found" label) | |
707 | (throw 'exit nil)) | |
708 | ||
709 | ;; search for that label | |
710 | (setq re (format reftex-find-label-regexp-format (regexp-quote label))) | |
711 | (setq found | |
712 | (if forward | |
713 | (re-search-forward re nil t) | |
714 | (re-search-backward re nil t))) | |
715 | (unless found | |
716 | (goto-char (point-min)) | |
717 | (unless (setq found (re-search-forward re nil t)) | |
718 | ;; Ooops. Must be in a macro with distributed args. | |
719 | (setq found | |
720 | (re-search-forward | |
721 | (format reftex-find-label-regexp-format2 | |
722 | (regexp-quote label)) nil t)))) | |
723 | (if (match-end 3) | |
724 | (progn | |
725 | (reftex-highlight 0 (match-beginning 3) (match-end 3)) | |
726 | (reftex-show-entry (match-beginning 3) (match-end 3)) | |
727 | (recenter '(4)) | |
728 | (unless stay (select-window this-window))) | |
729 | (select-window this-window) | |
730 | (funcall errorf "Label %s not found" label)) | |
731 | found))) | |
732 | ||
733 | (defvar font-lock-mode) | |
734 | (defun reftex-show-entry (beg-hlt end-hlt) | |
735 | ;; Show entry if point is hidden | |
736 | (let* ((n (/ (reftex-window-height) 2)) | |
737 | (beg (save-excursion | |
738 | (re-search-backward "[\n\r]" nil 1 n) (point))) | |
739 | (end (save-excursion | |
740 | (re-search-forward "[\n\r]" nil 1 n) (point)))) | |
741 | (cond | |
742 | ((and (boundp 'buffer-invisibility-spec) buffer-invisibility-spec | |
743 | (get-char-property (1+ beg-hlt) 'invisible)) | |
744 | ;; Invisible with text properties. That is easy to change. | |
745 | (push (cons (current-buffer) buffer-invisibility-spec) | |
746 | reftex-buffers-with-changed-invisibility) | |
747 | (setq buffer-invisibility-spec nil)) | |
748 | ((string-match "\r" (buffer-substring beg end)) | |
749 | ;; Invisible with selective display. We need to copy it. | |
750 | (let ((string (buffer-substring-no-properties beg end))) | |
751 | (switch-to-buffer "*RefTeX Context Copy*") | |
752 | (setq buffer-read-only nil) | |
753 | (erase-buffer) | |
754 | (insert string) | |
755 | (subst-char-in-region (point-min) (point-max) ?\r ?\n t) | |
756 | (goto-char (- beg-hlt beg)) | |
757 | (reftex-highlight 0 (1+ (- beg-hlt beg)) (1+ (- end-hlt beg))) | |
758 | (if (reftex-refontify) | |
759 | (when (or (not (eq major-mode 'latex-mode)) | |
760 | (not font-lock-mode)) | |
761 | (latex-mode) | |
762 | (run-hook-with-args | |
763 | 'reftex-pre-refontification-functions | |
764 | reftex-call-back-to-this-buffer 'reftex-hidden) | |
765 | (turn-on-font-lock)) | |
766 | (when (or (not (eq major-mode 'fundamental-mode)) | |
767 | font-lock-mode) | |
768 | (fundamental-mode))) | |
769 | (run-hooks 'reftex-display-copied-context-hook) | |
770 | (setq buffer-read-only t)))))) | |
771 | ||
772 | (defun reftex-varioref-vref () | |
773 | "Insert a reference using the `\vref' macro from the varioref package." | |
774 | (interactive) | |
775 | (let ((reftex-format-ref-function 'reftex-format-vref)) | |
776 | (reftex-reference))) | |
777 | (defun reftex-fancyref-fref () | |
778 | "Insert a reference using the `\fref' macro from the fancyref package." | |
779 | (interactive) | |
780 | (let ((reftex-format-ref-function 'reftex-format-fref) | |
781 | ;;(reftex-guess-label-type nil) ;FIXME do we want this???? | |
782 | ) | |
783 | (reftex-reference))) | |
784 | (defun reftex-fancyref-Fref () | |
785 | "Insert a reference using the `\Fref' macro from the fancyref package." | |
786 | (interactive) | |
787 | (let ((reftex-format-ref-function 'reftex-format-Fref) | |
788 | ;;(reftex-guess-label-type nil) ;FIXME do we want this???? | |
789 | ) | |
790 | (reftex-reference))) | |
791 | ||
792 | (defun reftex-format-vref (label fmt) | |
793 | (while (string-match "\\\\ref{" fmt) | |
794 | (setq fmt (replace-match "\\vref{" t t fmt))) | |
795 | (format fmt label label)) | |
796 | (defun reftex-format-Fref (label def-fmt) | |
797 | (format "\\Fref{%s}" label)) | |
798 | (defun reftex-format-fref (label def-fmt) | |
799 | (format "\\fref{%s}" label)) | |
800 | ||
801 | ;;; reftex-ref.el ends here |