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