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