reftex-ref.el 35 KB

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