reftex-ref.el 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886
  1. ;;; reftex-ref.el --- code to create labels and references with RefTeX
  2. ;; Copyright (C) 1997-2015 Free Software Foundation, Inc.
  3. ;; Author: Carsten Dominik <dominik@science.uva.nl>
  4. ;; Maintainer: auctex-devel@gnu.org
  5. ;; This file is part of GNU Emacs.
  6. ;; GNU Emacs is free software: you can redistribute it and/or modify
  7. ;; it under the terms of the GNU General Public License as published by
  8. ;; the Free Software Foundation, either version 3 of the License, or
  9. ;; (at your option) any later version.
  10. ;; GNU Emacs is distributed in the hope that it will be useful,
  11. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. ;; GNU General Public License for more details.
  14. ;; You should have received a copy of the GNU General Public License
  15. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  16. ;;; Commentary:
  17. ;;; Code:
  18. (eval-when-compile (require 'cl))
  19. (require 'reftex)
  20. (require 'reftex-parse)
  21. ;;;###autoload
  22. (defun reftex-label-location (&optional bound)
  23. "Return the environment or macro which determines the label type at point.
  24. If optional BOUND is an integer, limit backward searches to that point."
  25. (let* ((loc1 (reftex-what-macro reftex-label-mac-list bound))
  26. (loc2 (reftex-what-environment reftex-label-env-list bound))
  27. (loc3 (reftex-what-special-env 1 bound))
  28. (p1 (or (cdr loc1) 0))
  29. (p2 (or (cdr loc2) 0))
  30. (p3 (or (cdr loc3) 0))
  31. (pmax (max p1 p2 p3)))
  32. (setq reftex-location-start pmax)
  33. (cond
  34. ((= p1 pmax)
  35. ;; A macro. Default context after macro name.
  36. (setq reftex-default-context-position (+ p1 (length (car loc1))))
  37. (or (car loc1) "section"))
  38. ((= p2 pmax)
  39. ;; An environment. Default context after \begin{name}.
  40. (setq reftex-default-context-position (+ p2 8 (length (car loc2))))
  41. (or (car loc2) "section"))
  42. ((= p3 pmax)
  43. ;; A special. Default context right there.
  44. (setq reftex-default-context-position p3)
  45. (setq loc3 (car loc3))
  46. (cond ((null loc3) "section")
  47. ((symbolp loc3) (symbol-name loc3))
  48. ((stringp loc3) loc3)
  49. (t "section")))
  50. (t ;; This should not happen, I think?
  51. "section"))))
  52. ;;;###autoload
  53. (defun reftex-label-info-update (cell)
  54. ;; Update information about just one label in a different file.
  55. ;; CELL contains the old info list
  56. (let* ((label (nth 0 cell))
  57. (typekey (nth 1 cell))
  58. ;; (text (nth 2 cell))
  59. (file (nth 3 cell))
  60. (comment (nth 4 cell))
  61. (note (nth 5 cell))
  62. (buf (reftex-get-file-buffer-force
  63. file (not (eq t reftex-keep-temporary-buffers)))))
  64. (if (not buf)
  65. (list label typekey "" file comment "LOST LABEL. RESCAN TO FIX.")
  66. (with-current-buffer buf
  67. (save-restriction
  68. (widen)
  69. (goto-char 1)
  70. (if (or (re-search-forward
  71. (format reftex-find-label-regexp-format
  72. (regexp-quote label)) nil t)
  73. (re-search-forward
  74. (format reftex-find-label-regexp-format2
  75. (regexp-quote label)) nil t))
  76. (progn
  77. (backward-char 1)
  78. (append (reftex-label-info label file) (list note)))
  79. (list label typekey "" file "LOST LABEL. RESCAN TO FIX.")))))))
  80. ;;;###autoload
  81. (defun reftex-label-info (label &optional file bound derive env-or-mac)
  82. ;; Return info list on LABEL at point.
  83. (let* ((prefix (if (string-match "^[a-zA-Z0-9]+:" label)
  84. (match-string 0 label)))
  85. (typekey (cdr (assoc prefix reftex-prefix-to-typekey-alist)))
  86. (file (or file (buffer-file-name)))
  87. (trust reftex-trust-label-prefix)
  88. (in-comment (reftex-in-comment)))
  89. (if (and typekey
  90. (cond ((eq trust t) t)
  91. ((null trust) nil)
  92. ((stringp trust) (string-match trust typekey))
  93. ((listp trust) (member typekey trust))
  94. (t nil)))
  95. (list label typekey
  96. (reftex-nicify-text (reftex-context-substring))
  97. file in-comment)
  98. (let* ((env-or-mac (or env-or-mac (reftex-label-location bound)))
  99. (typekey (nth 1 (assoc env-or-mac reftex-env-or-mac-alist)))
  100. (parse (nth 2 (assoc env-or-mac reftex-env-or-mac-alist)))
  101. (text (reftex-short-context env-or-mac parse reftex-location-start
  102. derive)))
  103. (list label typekey text file in-comment)))))
  104. ;;; Creating labels ---------------------------------------------------------
  105. ;;;###autoload
  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-message "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-base))
  271. ((equal letter "F")
  272. (let ((masterdir (file-name-directory (reftex-TeX-master-file)))
  273. (file (file-name-sans-extension (buffer-file-name))))
  274. (if (string-match (concat "\\`" (regexp-quote masterdir))
  275. file)
  276. (substring file (length masterdir))
  277. file)))
  278. ((equal letter "m")
  279. (file-name-base (reftex-TeX-master-file)))
  280. ((equal letter "M")
  281. (file-name-nondirectory
  282. (substring (file-name-directory (reftex-TeX-master-file))
  283. 0 -1)))
  284. ((equal letter "u")
  285. (or (user-login-name) ""))
  286. ((equal letter "S")
  287. (let* (macro level-exp level)
  288. (save-excursion
  289. (save-match-data
  290. (when (re-search-backward reftex-section-regexp nil t)
  291. (setq macro (reftex-match-string 2)
  292. level-exp (cdr (assoc macro reftex-section-levels-all))
  293. level (if (symbolp level-exp)
  294. (abs (save-match-data
  295. (funcall level-exp)))
  296. (abs level-exp))))
  297. (cdr (or (assoc macro reftex-section-prefixes)
  298. (assoc level reftex-section-prefixes)
  299. (assq t reftex-section-prefixes)
  300. (list t "sec:")))))))
  301. (t ""))))
  302. (setq num (1- (+ (match-beginning 1) (length replace)))
  303. prefix (replace-match replace nil nil prefix)))
  304. prefix)))
  305. (defun reftex-uniquify-label (label &optional force separator)
  306. ;; Make label unique by appending a number.
  307. ;; Optional FORCE means, force appending a number, even if label is unique.
  308. ;; Optional SEPARATOR is a string to stick between label and number.
  309. ;; Ensure access to scanning info
  310. (reftex-access-scan-info)
  311. (cond
  312. ((and (not force)
  313. (not (assoc label (symbol-value reftex-docstruct-symbol))))
  314. label)
  315. (t
  316. (let* ((label-numbers (assq 'label-numbers
  317. (symbol-value reftex-docstruct-symbol)))
  318. (label-numbers-alist (cdr label-numbers))
  319. (cell (or (assoc label label-numbers-alist)
  320. (car (setcdr label-numbers
  321. (cons (cons label 0)
  322. label-numbers-alist)))))
  323. (num (1+ (cdr cell)))
  324. (sep (or separator "")))
  325. (while (assoc (concat label sep (int-to-string num))
  326. (symbol-value reftex-docstruct-symbol))
  327. (incf num))
  328. (setcdr cell num)
  329. (concat label sep (int-to-string num))))))
  330. ;;; Referencing labels ------------------------------------------------------
  331. ;; Help string for the reference label menu
  332. (defconst reftex-select-label-prompt
  333. "Select: [n]ext [p]revious [r]escan [ ]context e[x]tern [q]uit RET [?]HELP+more")
  334. (defconst reftex-select-label-help
  335. " n / p Go to next/previous label (Cursor motion works as well)
  336. C-c C-n/p Go to next/previous section heading.
  337. b / l Jump back to previous selection / Reuse last referenced label.
  338. z Jump to a specific section, e.g. '3 z' jumps to section 3.
  339. g / s Update menu / Switch label type.
  340. r / C-u r Reparse document / Reparse entire document.
  341. x Switch to label menu of external document (with LaTeX package `xr').
  342. F t c Toggle: [F]ile borders, [t]able of contents, [c]ontext
  343. # % Toggle: [#] label counters, [%] labels in comments
  344. SPC / f Show full context in other window / Toggle follow mode.
  345. . Show insertion point in other window.
  346. v / V Toggle \\ref <-> \\vref / Rotate \\ref <=> \\fref <=> \\Fref
  347. TAB Enter a label with completion.
  348. m , - + Mark entry. `,-+' also assign a separator.
  349. a / A Put all marked entries into one/many \\ref commands.
  350. q / RET Quit without referencing / Accept current label (also on mouse-2).")
  351. ;;;###autoload
  352. (defun reftex-reference (&optional type no-insert cut)
  353. "Make a LaTeX reference. Look only for labels of a certain TYPE.
  354. With prefix arg, force to rescan buffer for labels. This should only be
  355. necessary if you have recently entered labels yourself without using
  356. reftex-label. Rescanning of the buffer can also be requested from the
  357. label selection menu.
  358. The function returns the selected label or nil.
  359. If NO-INSERT is non-nil, do not insert \\ref command, just return label.
  360. When called with 2 C-u prefix args, disable magic word recognition."
  361. (interactive)
  362. ;; Check for active recursive edits
  363. (reftex-check-recursive-edit)
  364. ;; Ensure access to scanning info and rescan buffer if prefix is '(4)
  365. (reftex-access-scan-info current-prefix-arg)
  366. (let ((reftex-refstyle (when (and (boundp 'reftex-refstyle) reftex-refstyle)
  367. reftex-refstyle))
  368. (reftex-format-ref-function reftex-format-ref-function)
  369. (form "\\ref{%s}")
  370. label labels sep sep1 style-alist)
  371. (unless reftex-refstyle
  372. (if reftex-ref-macro-prompt
  373. (progn
  374. ;; Build a temporary list which handles more easily.
  375. (dolist (elt reftex-ref-style-alist)
  376. (when (member (car elt) (reftex-ref-style-list))
  377. (mapc (lambda (x)
  378. (add-to-list 'style-alist (cons (cadr x) (car x)) t))
  379. (nth 2 elt))))
  380. ;; Prompt the user for the macro.
  381. (let ((key (reftex-select-with-char
  382. "" (concat "SELECT A REFERENCE FORMAT\n\n"
  383. (mapconcat
  384. (lambda (x)
  385. (format "[%c] %s %s" (car x)
  386. (if (> (car x) 31) " " "")
  387. (cdr x)))
  388. style-alist "\n")))))
  389. (setq reftex-refstyle (cdr (assoc key style-alist)))
  390. (unless reftex-refstyle
  391. (error "No reference macro associated with key `%c'" key))))
  392. ;; Get the first macro from `reftex-ref-style-alist' which
  393. ;; matches the first entry in the list of active styles.
  394. (setq reftex-refstyle
  395. (or (caar (nth 2 (assoc (car (reftex-ref-style-list))
  396. reftex-ref-style-alist)))
  397. ;; Use the first entry in r-r-s-a as a last resort.
  398. (caar (nth 2 (car reftex-ref-style-alist)))))))
  399. (unless type
  400. ;; Guess type from context
  401. (if (and reftex-guess-label-type
  402. (setq type (reftex-guess-label-type)))
  403. (setq cut (cdr type)
  404. type (car type))
  405. (setq type (reftex-query-label-type))))
  406. ;; Have the user select a label
  407. (set-marker reftex-select-return-marker (point))
  408. (setq labels (save-excursion
  409. (reftex-offer-label-menu type)))
  410. (reftex-ensure-compiled-variables)
  411. (set-marker reftex-select-return-marker nil)
  412. ;; If the first entry is the symbol 'concat, concat all labels.
  413. ;; We keep the cdr of the first label for typekey etc information.
  414. (if (eq (car labels) 'concat)
  415. (setq labels (list (list (mapconcat 'car (cdr labels) ",")
  416. (cdr (nth 1 labels))))))
  417. (setq type (nth 1 (car labels))
  418. form (or (cdr (assoc type reftex-typekey-to-format-alist))
  419. form))
  420. (cond
  421. (no-insert
  422. ;; Just return the first label
  423. (car (car labels)))
  424. ((null labels)
  425. (message "Quit")
  426. nil)
  427. (t
  428. (while labels
  429. (setq label (car (car labels))
  430. sep (nth 2 (car labels))
  431. sep1 (cdr (assoc sep reftex-multiref-punctuation))
  432. labels (cdr labels))
  433. (when cut
  434. (backward-delete-char cut)
  435. (setq cut nil))
  436. ;; remove ~ if we do already have a space
  437. (when (and (= ?~ (string-to-char form))
  438. (member (preceding-char) '(?\ ?\t ?\n ?~)))
  439. (setq form (substring form 1)))
  440. ;; do we have a special format?
  441. (unless (string= reftex-refstyle "\\ref")
  442. (setq reftex-format-ref-function 'reftex-format-special))
  443. ;; ok, insert the reference
  444. (if sep1 (insert sep1))
  445. (insert
  446. (if reftex-format-ref-function
  447. (funcall reftex-format-ref-function label form reftex-refstyle)
  448. (format form label label)))
  449. ;; take out the initial ~ for good
  450. (and (= ?~ (string-to-char form))
  451. (setq form (substring form 1))))
  452. (message "")
  453. label))))
  454. (defun reftex-guess-label-type ()
  455. ;; Examine context to guess what a \ref might want to reference.
  456. (let ((words reftex-words-to-typekey-alist)
  457. (case-fold-search t)
  458. (bound (max (point-min) (- (point) 35)))
  459. matched cell)
  460. (save-excursion
  461. (while (and (setq cell (pop words))
  462. (not (setq matched
  463. (re-search-backward (car cell) bound t))))))
  464. (if matched
  465. (cons (cdr cell) (- (match-end 0) (match-end 1)))
  466. nil)))
  467. (defvar reftex-select-label-map)
  468. (defun reftex-offer-label-menu (typekey)
  469. ;; Offer a menu with the appropriate labels.
  470. (let* ((buf (current-buffer))
  471. (xr-data (assq 'xr (symbol-value reftex-docstruct-symbol)))
  472. (xr-alist (cons (cons "" (buffer-file-name)) (nth 1 xr-data)))
  473. (xr-index 0)
  474. (here-I-am (car (reftex-where-am-I)))
  475. (here-I-am1 here-I-am)
  476. (toc (reftex-typekey-check typekey reftex-label-menu-flags 0))
  477. (files (reftex-typekey-check typekey reftex-label-menu-flags 7))
  478. (context (not (reftex-typekey-check
  479. typekey reftex-label-menu-flags 3)))
  480. (counter (reftex-typekey-check
  481. typekey reftex-label-menu-flags 2))
  482. (follow (reftex-typekey-check
  483. typekey reftex-label-menu-flags 4))
  484. (commented (nth 5 reftex-label-menu-flags))
  485. (prefix "")
  486. selection-buffers
  487. offset rtn key data last-data entries)
  488. (unwind-protect
  489. (catch 'exit
  490. (while t
  491. (save-window-excursion
  492. (delete-other-windows)
  493. (setq reftex-call-back-to-this-buffer buf
  494. reftex-latex-syntax-table (syntax-table))
  495. (if reftex-use-multiple-selection-buffers
  496. (switch-to-buffer-other-window
  497. (with-current-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<" 'reftex-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. (setq here-I-am nil) ; turn off determination of offset
  533. (setq rtn
  534. (reftex-select-item
  535. reftex-select-label-prompt
  536. reftex-select-label-help
  537. reftex-select-label-map
  538. offset
  539. 'reftex-show-label-location follow))
  540. (setq key (car rtn)
  541. data (nth 1 rtn)
  542. last-data (nth 2 rtn)
  543. offset t)
  544. (unless key (throw 'exit nil))
  545. (cond
  546. ((eq key ?g)
  547. ;; update buffer
  548. (reftex-erase-buffer))
  549. ((or (eq key ?r)
  550. (eq key ?R))
  551. ;; rescan buffer
  552. (and current-prefix-arg (setq key ?R))
  553. (reftex-erase-buffer)
  554. (reftex-reparse-document buf last-data key))
  555. ((eq key ?c)
  556. ;; toggle context mode
  557. (reftex-erase-buffer)
  558. (setq context (not context)))
  559. ((eq key ?s)
  560. ;; switch type
  561. (setq here-I-am here-I-am1)
  562. (setq typekey (reftex-query-label-type)))
  563. ((eq key ?t)
  564. ;; toggle table of contents display, or change depth
  565. (reftex-erase-buffer)
  566. (if current-prefix-arg
  567. (setq reftex-toc-max-level (prefix-numeric-value
  568. current-prefix-arg))
  569. (setq toc (not toc))))
  570. ((eq key ?F)
  571. ;; toggle display of included file borders
  572. (reftex-erase-buffer)
  573. (setq files (not files)))
  574. ((eq key ?#)
  575. ;; toggle counter display
  576. (reftex-erase-buffer)
  577. (setq counter (not counter)))
  578. ((eq key ?%)
  579. ;; toggle display of commented labels
  580. (reftex-erase-buffer)
  581. (setq commented (not commented)))
  582. ((eq key ?l)
  583. ;; reuse the last referenced label again
  584. (setq entries reftex-last-used-reference)
  585. (throw 'exit t))
  586. ((eq key ?x)
  587. ;; select an external document
  588. (setq xr-index (reftex-select-external-document
  589. xr-alist xr-index))
  590. (setq buf (or (reftex-get-file-buffer-force
  591. (cdr (nth xr-index xr-alist)))
  592. (error "Cannot switch document"))
  593. prefix (or (car (nth xr-index xr-alist)) ""))
  594. (set-buffer buf)
  595. (reftex-access-scan-info))
  596. ((stringp key)
  597. (setq entries
  598. (list
  599. (list
  600. (or (assoc key (symbol-value reftex-docstruct-symbol))
  601. (list key typekey)))))
  602. (throw 'exit t))
  603. ((memq key '(?a ?A return))
  604. (cond
  605. (reftex-select-marked
  606. (setq entries (nreverse reftex-select-marked)))
  607. (data
  608. (setq entries (list (list data))))
  609. (t (setq entries nil)))
  610. (when entries
  611. (if (equal key ?a) (push 'concat entries))
  612. (setq reftex-last-used-reference entries))
  613. (set-buffer buf)
  614. (throw 'exit t))
  615. (t (error "This should not happen (reftex-offer-label-menu)"))))))
  616. (save-excursion
  617. (while reftex-buffers-with-changed-invisibility
  618. (set-buffer (car (car reftex-buffers-with-changed-invisibility)))
  619. (setq buffer-invisibility-spec
  620. (cdr (pop reftex-buffers-with-changed-invisibility)))))
  621. (mapc (lambda (buf) (and (buffer-live-p buf) (bury-buffer buf)))
  622. selection-buffers)
  623. (reftex-kill-temporary-buffers))
  624. ;; Add the prefixes, put together the relevant information in the form
  625. ;; (LABEL TYPEKEY SEPARATOR) and return a list of those.
  626. (mapcar (lambda (x)
  627. (if (listp x)
  628. (list (concat prefix (car (car x)))
  629. (nth 1 (car x))
  630. (nth 2 x))
  631. x))
  632. entries)))
  633. (defun reftex-reparse-document (&optional buffer data key)
  634. ;; Rescan the document.
  635. (save-window-excursion
  636. (save-excursion
  637. (if buffer
  638. (if (not (bufferp buffer))
  639. (error "No such buffer %s" (buffer-name buffer))
  640. (set-buffer buffer)))
  641. (let ((arg (if (eq key ?R) '(16) '(4)))
  642. (file (nth 3 data)))
  643. (reftex-access-scan-info arg file)))))
  644. ;;;###autoload
  645. (defun reftex-query-label-type ()
  646. ;; Ask for label type
  647. (let ((key (reftex-select-with-char
  648. reftex-type-query-prompt reftex-type-query-help 3)))
  649. (unless (member (char-to-string key) reftex-typekey-list)
  650. (error "No such label type: %s" (char-to-string key)))
  651. (char-to-string key)))
  652. ;;;###autoload
  653. (defun reftex-show-label-location (data forward no-revisit
  654. &optional stay error)
  655. ;; View the definition site of a label in another window.
  656. ;; DATA is an entry from the docstruct list.
  657. ;; FORWARD indicates if the label is likely forward from current point.
  658. ;; NO-REVISIT means do not load a file to show this label.
  659. ;; STAY means leave the new window selected.
  660. ;; ERROR means throw an error exception when the label cannot be found.
  661. ;; If ERROR is nil, the return value of this function indicates success.
  662. (let* ((this-window (selected-window))
  663. (errorf (if error 'error 'message))
  664. label file buffer re found)
  665. (catch 'exit
  666. (setq label (nth 0 data)
  667. file (nth 3 data))
  668. (unless file
  669. (funcall errorf "Unknown label - reparse might help")
  670. (throw 'exit nil))
  671. ;; Goto the file in another window
  672. (setq buffer
  673. (if no-revisit
  674. (reftex-get-buffer-visiting file)
  675. (reftex-get-file-buffer-force
  676. file (not reftex-keep-temporary-buffers))))
  677. (if buffer
  678. ;; good - the file is available
  679. (switch-to-buffer-other-window buffer)
  680. ;; we have got a problem here. The file does not exist.
  681. ;; Let' get out of here..
  682. (funcall errorf "Label %s not found" label)
  683. (throw 'exit nil))
  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. (defvar font-lock-mode)
  708. (defun reftex-show-entry (beg-hlt end-hlt)
  709. ;; Show entry if point is hidden
  710. (let* ((n (/ (reftex-window-height) 2))
  711. (beg (save-excursion
  712. (re-search-backward "[\n\r]" nil 1 n) (point)))
  713. (end (save-excursion
  714. (re-search-forward "[\n\r]" nil 1 n) (point))))
  715. (cond
  716. ((and (boundp 'buffer-invisibility-spec) buffer-invisibility-spec
  717. (get-char-property (1+ beg-hlt) 'invisible))
  718. ;; Invisible with text properties. That is easy to change.
  719. (push (cons (current-buffer) buffer-invisibility-spec)
  720. reftex-buffers-with-changed-invisibility)
  721. (setq buffer-invisibility-spec nil))
  722. ((string-match "\r" (buffer-substring beg end))
  723. ;; Invisible with selective display. We need to copy it.
  724. (let ((string (buffer-substring-no-properties beg end)))
  725. (switch-to-buffer "*RefTeX Context Copy*")
  726. (setq buffer-read-only nil)
  727. (erase-buffer)
  728. (insert string)
  729. (subst-char-in-region (point-min) (point-max) ?\r ?\n t)
  730. (goto-char (- beg-hlt beg))
  731. (reftex-highlight 0 (1+ (- beg-hlt beg)) (1+ (- end-hlt beg)))
  732. (if (reftex-refontify)
  733. (when (or (not (eq major-mode 'latex-mode))
  734. (not font-lock-mode))
  735. (latex-mode)
  736. (run-hook-with-args
  737. 'reftex-pre-refontification-functions
  738. reftex-call-back-to-this-buffer 'reftex-hidden)
  739. (turn-on-font-lock))
  740. (when (or (not (eq major-mode 'fundamental-mode))
  741. font-lock-mode)
  742. (fundamental-mode)))
  743. (run-hooks 'reftex-display-copied-context-hook)
  744. (setq buffer-read-only t))))))
  745. ;; Generate functions for direct insertion of specific referencing
  746. ;; macros. The functions are named `reftex-<package>-<macro>',
  747. ;; e.g. `reftex-varioref-vref'.
  748. (dolist (elt reftex-ref-style-alist)
  749. (when (stringp (nth 1 elt))
  750. (dolist (item (nth 2 elt))
  751. (let ((macro (car item))
  752. (package (nth 1 elt)))
  753. (eval `(defun ,(intern (format "reftex-%s-%s" package
  754. (substring macro 1 (length macro)))) ()
  755. ,(format "Insert a reference using the `%s' macro from the %s \
  756. package.\n\nThis is a generated function."
  757. macro package)
  758. (interactive)
  759. (let ((reftex-refstyle ,macro))
  760. (reftex-reference))))))))
  761. (defun reftex-format-special (label fmt refstyle)
  762. "Apply selected reference style to format FMT and add LABEL.
  763. Replace any occurrences of \"\\ref\" with REFSTYLE."
  764. ;; Replace instances of \ref in `fmt' with the special reference
  765. ;; style selected by the user.
  766. (cond
  767. ((while (string-match "\\(\\\\ref\\)[ \t]*{" fmt)
  768. (setq fmt (replace-match refstyle t t fmt 1))))
  769. ((string-match "\\(\\\\[[:alpha:]]+\\)[ \t]*{" fmt)
  770. (setq fmt (replace-match refstyle t t fmt 1))))
  771. (format fmt label))
  772. ;;;###autoload
  773. (defun reftex-goto-label (&optional other-window)
  774. "Prompt for a label (with completion) and jump to the location of this label.
  775. Optional prefix argument OTHER-WINDOW goes to the label in another window."
  776. (interactive "P")
  777. (reftex-access-scan-info)
  778. (let* ((wcfg (current-window-configuration))
  779. (docstruct (symbol-value reftex-docstruct-symbol))
  780. ;; If point is inside a \ref{} or \pageref{}, use that as
  781. ;; default value.
  782. (default (when (looking-back "\\\\\\(?:page\\)?ref{[-a-zA-Z0-9_*.:]*"
  783. (line-beginning-position))
  784. (reftex-this-word "-a-zA-Z0-9_*.:")))
  785. (label (completing-read (if default
  786. (format "Label (default %s): " default)
  787. "Label: ")
  788. docstruct
  789. (lambda (x) (stringp (car x))) t nil nil
  790. default))
  791. (selection (assoc label docstruct))
  792. (where (progn
  793. (reftex-show-label-location selection t nil 'stay)
  794. (point-marker))))
  795. (unless other-window
  796. (set-window-configuration wcfg)
  797. (switch-to-buffer (marker-buffer where))
  798. (goto-char where))
  799. (reftex-unhighlight 0)))
  800. (provide 'reftex-ref)
  801. ;;; reftex-ref.el ends here
  802. ;; Local Variables:
  803. ;; generated-autoload-file: "reftex.el"
  804. ;; End: