reftex-cite.el 46 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177
  1. ;;; reftex-cite.el --- creating citations 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-cite)
  22. (require 'reftex)
  23. ;;;
  24. ;; Variables and constants
  25. ;; The history list of regular expressions used for citations
  26. (defvar reftex-cite-regexp-hist nil)
  27. ;; Prompt and help string for citation selection
  28. (defconst reftex-citation-prompt
  29. "Select: [n]ext [p]revious [r]estrict [ ]full_entry [q]uit RET [?]Help+more")
  30. (defconst reftex-citation-help
  31. " n / p Go to next/previous entry (Cursor motion works as well).
  32. g / r Start over with new regexp / Refine with additional regexp.
  33. SPC Show full database entry in other window.
  34. f Toggle follow mode: Other window will follow with full db entry.
  35. . Show insertion point.
  36. q Quit without inserting \\cite macro into buffer.
  37. TAB Enter citation key with completion.
  38. RET Accept current entry (also on mouse-2) and create \\cite macro.
  39. m / u Mark/Unmark the entry.
  40. e / E Create BibTeX file with all (marked/unmarked) entries
  41. a / A Put all (marked) entries into one/many \\cite commands.")
  42. ;; Find bibtex files
  43. (defmacro reftex-with-special-syntax-for-bib (&rest body)
  44. `(let ((saved-syntax (syntax-table)))
  45. (unwind-protect
  46. (progn
  47. (set-syntax-table reftex-syntax-table-for-bib)
  48. ,@body)
  49. (set-syntax-table saved-syntax))))
  50. (defun reftex-default-bibliography ()
  51. ;; Return the expanded value of `reftex-default-bibliography'.
  52. ;; The expanded value is cached.
  53. (unless (eq (get 'reftex-default-bibliography :reftex-raw)
  54. reftex-default-bibliography)
  55. (put 'reftex-default-bibliography :reftex-expanded
  56. (reftex-locate-bibliography-files
  57. default-directory reftex-default-bibliography))
  58. (put 'reftex-default-bibliography :reftex-raw
  59. reftex-default-bibliography))
  60. (get 'reftex-default-bibliography :reftex-expanded))
  61. (defun reftex-bib-or-thebib ()
  62. ;; Tests if BibTeX or \begin{thebibliography} should be used for the
  63. ;; citation
  64. ;; Find the bof of the current file
  65. (let* ((docstruct (symbol-value reftex-docstruct-symbol))
  66. (rest (or (member (list 'bof (buffer-file-name)) docstruct)
  67. docstruct))
  68. (bib (assq 'bib rest))
  69. (thebib (assq 'thebib rest))
  70. (bibmem (memq bib rest))
  71. (thebibmem (memq thebib rest)))
  72. (when (not (or thebib bib))
  73. (setq bib (assq 'bib docstruct)
  74. thebib (assq 'thebib docstruct)
  75. bibmem (memq bib docstruct)
  76. thebibmem (memq thebib docstruct)))
  77. (if (> (length bibmem) (length thebibmem))
  78. (if bib 'bib nil)
  79. (if thebib 'thebib nil))))
  80. (defun reftex-get-bibfile-list ()
  81. ;; Return list of bibfiles for current document.
  82. ;; When using the chapterbib or bibunits package you should either
  83. ;; use the same database files everywhere, or separate parts using
  84. ;; different databases into different files (included into the mater file).
  85. ;; Then this function will return the applicable database files.
  86. ;; Ensure access to scanning info
  87. (reftex-access-scan-info)
  88. (or
  89. ;; Try inside this file (and its includes)
  90. (cdr (reftex-last-assoc-before-elt
  91. 'bib (list 'eof (buffer-file-name))
  92. (member (list 'bof (buffer-file-name))
  93. (symbol-value reftex-docstruct-symbol))))
  94. ;; Try after the beginning of this file
  95. (cdr (assq 'bib (member (list 'bof (buffer-file-name))
  96. (symbol-value reftex-docstruct-symbol))))
  97. ;; Anywhere in the entire document
  98. (cdr (assq 'bib (symbol-value reftex-docstruct-symbol)))
  99. (error "\\bibliography statement missing or .bib files not found")))
  100. ;; Find a certain reference in any of the BibTeX files.
  101. (defun reftex-pop-to-bibtex-entry (key file-list &optional mark-to-kill
  102. highlight item return)
  103. ;; Find BibTeX KEY in any file in FILE-LIST in another window.
  104. ;; If MARK-TO-KILL is non-nil, mark new buffer to kill.
  105. ;; If HIGHLIGHT is non-nil, highlight the match.
  106. ;; If ITEM in non-nil, search for bibitem instead of database entry.
  107. ;; If RETURN is non-nil, just return the entry and restore point.
  108. (let* ((re
  109. (if item
  110. (concat "\\\\bibitem\\(\\[[^]]*\\]\\)?{" (regexp-quote key) "}")
  111. (concat "@[a-zA-Z]+[ \t\n\r]*[{(][ \t\n\r]*" (regexp-quote key)
  112. "[, \t\r\n}]")))
  113. (buffer-conf (current-buffer))
  114. file buf pos oldpos)
  115. (catch 'exit
  116. (while file-list
  117. (setq file (car file-list)
  118. file-list (cdr file-list))
  119. (unless (setq buf (reftex-get-file-buffer-force file mark-to-kill))
  120. (error "No such file %s" file))
  121. (set-buffer buf)
  122. (setq oldpos (point))
  123. (widen)
  124. (goto-char (point-min))
  125. (if (not (re-search-forward re nil t))
  126. (goto-char oldpos) ;; restore previous position of point
  127. (goto-char (match-beginning 0))
  128. (setq pos (point))
  129. (when return
  130. ;; Just return the relevant entry
  131. (if item (goto-char (match-end 0)))
  132. (setq return (buffer-substring
  133. (point) (reftex-end-of-bib-entry item)))
  134. (goto-char oldpos) ;; restore point.
  135. (set-buffer buffer-conf)
  136. (throw 'exit return))
  137. (switch-to-buffer-other-window buf)
  138. (goto-char pos)
  139. (recenter 0)
  140. (if highlight
  141. (reftex-highlight 0 (match-beginning 0) (match-end 0)))
  142. (throw 'exit (selected-window))))
  143. (set-buffer buffer-conf)
  144. (if item
  145. (error "No \\bibitem with citation key %s" key)
  146. (error "No BibTeX entry with citation key %s" key)))))
  147. (defun reftex-end-of-bib-entry (item)
  148. (save-excursion
  149. (condition-case nil
  150. (if item
  151. (progn (end-of-line)
  152. (re-search-forward
  153. "\\\\bibitem\\|\\end{thebibliography}")
  154. (1- (match-beginning 0)))
  155. (progn (forward-list 1) (point)))
  156. (error (min (point-max) (+ 300 (point)))))))
  157. ;; Parse bibtex buffers
  158. (defun reftex-extract-bib-entries (buffers)
  159. ;; Extract bib entries which match regexps from BUFFERS.
  160. ;; BUFFERS is a list of buffers or file names.
  161. ;; Return list with entries."
  162. (let* (re-list first-re rest-re
  163. (buffer-list (if (listp buffers) buffers (list buffers)))
  164. found-list entry buffer1 buffer alist
  165. key-point start-point end-point default)
  166. ;; Read a regexp, completing on known citation keys.
  167. (setq default (regexp-quote (reftex-get-bibkey-default)))
  168. (setq re-list
  169. (split-string
  170. (completing-read
  171. (concat
  172. "Regex { && Regex...}: "
  173. "[" default "]: ")
  174. (if reftex-mode
  175. (if (fboundp 'LaTeX-bibitem-list)
  176. (LaTeX-bibitem-list)
  177. (cdr (assoc 'bibview-cache
  178. (symbol-value reftex-docstruct-symbol))))
  179. nil)
  180. nil nil nil 'reftex-cite-regexp-hist)
  181. "[ \t]*&&[ \t]*"))
  182. (if (or (null re-list ) (equal re-list '("")))
  183. (setq re-list (list default)))
  184. (setq first-re (car re-list) ; We'll use the first re to find things,
  185. rest-re (cdr re-list)) ; the others to narrow down.
  186. (if (string-match "\\`[ \t]*\\'" (or first-re ""))
  187. (error "Empty regular expression"))
  188. (save-excursion
  189. (save-window-excursion
  190. ;; Walk through all bibtex files
  191. (while buffer-list
  192. (setq buffer (car buffer-list)
  193. buffer-list (cdr buffer-list))
  194. (if (and (bufferp buffer)
  195. (buffer-live-p buffer))
  196. (setq buffer1 buffer)
  197. (setq buffer1 (reftex-get-file-buffer-force
  198. buffer (not reftex-keep-temporary-buffers))))
  199. (if (not buffer1)
  200. (message "No such BibTeX file %s (ignored)" buffer)
  201. (message "Scanning bibliography database %s" buffer1))
  202. (set-buffer buffer1)
  203. (reftex-with-special-syntax-for-bib
  204. (save-excursion
  205. (goto-char (point-min))
  206. (while (re-search-forward first-re nil t)
  207. (catch 'search-again
  208. (setq key-point (point))
  209. (unless (re-search-backward
  210. "\\(\\`\\|[\n\r]\\)[ \t]*@\\([a-zA-Z]+\\)[ \t\n\r]*[{(]" nil t)
  211. (throw 'search-again nil))
  212. (setq start-point (point))
  213. (goto-char (match-end 0))
  214. (condition-case nil
  215. (up-list 1)
  216. (error (goto-char key-point)
  217. (throw 'search-again nil)))
  218. (setq end-point (point))
  219. ;; Ignore @string, @comment and @c entries or things
  220. ;; outside entries
  221. (when (or (string= (downcase (match-string 2)) "string")
  222. (string= (downcase (match-string 2)) "comment")
  223. (string= (downcase (match-string 2)) "c")
  224. (< (point) key-point)) ; this means match not in {}
  225. (goto-char key-point)
  226. (throw 'search-again nil))
  227. ;; Well, we have got a match
  228. ;;(setq entry (concat
  229. ;; (buffer-substring start-point (point)) "\n"))
  230. (setq entry (buffer-substring start-point (point)))
  231. ;; Check if other regexp match as well
  232. (setq re-list rest-re)
  233. (while re-list
  234. (unless (string-match (car re-list) entry)
  235. ;; nope - move on
  236. (throw 'search-again nil))
  237. (pop re-list))
  238. (setq alist (reftex-parse-bibtex-entry
  239. nil start-point end-point))
  240. (push (cons "&entry" entry) alist)
  241. ;; check for crossref entries
  242. (if (assoc "crossref" alist)
  243. (setq alist
  244. (append
  245. alist (reftex-get-crossref-alist alist))))
  246. ;; format the entry
  247. (push (cons "&formatted" (reftex-format-bib-entry alist))
  248. alist)
  249. ;; make key the first element
  250. (push (reftex-get-bib-field "&key" alist) alist)
  251. ;; add it to the list
  252. (push alist found-list)))))
  253. (reftex-kill-temporary-buffers))))
  254. (setq found-list (nreverse found-list))
  255. ;; Sorting
  256. (cond
  257. ((eq 'author reftex-sort-bibtex-matches)
  258. (sort found-list 'reftex-bib-sort-author))
  259. ((eq 'year reftex-sort-bibtex-matches)
  260. (sort found-list 'reftex-bib-sort-year))
  261. ((eq 'reverse-year reftex-sort-bibtex-matches)
  262. (sort found-list 'reftex-bib-sort-year-reverse))
  263. (t found-list))))
  264. (defun reftex-bib-sort-author (e1 e2)
  265. (let ((al1 (reftex-get-bib-names "author" e1))
  266. (al2 (reftex-get-bib-names "author" e2)))
  267. (while (and al1 al2 (string= (car al1) (car al2)))
  268. (pop al1)
  269. (pop al2))
  270. (if (and (stringp (car al1))
  271. (stringp (car al2)))
  272. (string< (car al1) (car al2))
  273. (not (stringp (car al1))))))
  274. (defun reftex-bib-sort-year (e1 e2)
  275. (< (string-to-number (or (cdr (assoc "year" e1)) "0"))
  276. (string-to-number (or (cdr (assoc "year" e2)) "0"))))
  277. (defun reftex-bib-sort-year-reverse (e1 e2)
  278. (> (string-to-number (or (cdr (assoc "year" e1)) "0"))
  279. (string-to-number (or (cdr (assoc "year" e2)) "0"))))
  280. (defun reftex-get-crossref-alist (entry)
  281. ;; return the alist from a crossref entry
  282. (let ((crkey (cdr (assoc "crossref" entry)))
  283. start)
  284. (save-excursion
  285. (save-restriction
  286. (widen)
  287. (if (re-search-forward
  288. (concat "@\\w+[{(][ \t\n\r]*" (regexp-quote crkey)
  289. "[ \t\n\r]*,") nil t)
  290. (progn
  291. (setq start (match-beginning 0))
  292. (condition-case nil
  293. (up-list 1)
  294. (error nil))
  295. (reftex-parse-bibtex-entry nil start (point)))
  296. nil)))))
  297. ;; Parse the bibliography environment
  298. (defun reftex-extract-bib-entries-from-thebibliography (files)
  299. ;; Extract bib-entries from the \begin{thebibliography} environment.
  300. ;; Parsing is not as good as for the BibTeX database stuff.
  301. ;; The environment should be located in file FILE.
  302. (let* (start end buf entries re re-list file default)
  303. (unless files
  304. (error "Need file name to find thebibliography environment"))
  305. (while (setq file (pop files))
  306. (setq buf (reftex-get-file-buffer-force
  307. file (not reftex-keep-temporary-buffers)))
  308. (unless buf
  309. (error "No such file %s" file))
  310. (message "Scanning thebibliography environment in %s" file)
  311. (with-current-buffer buf
  312. (save-excursion
  313. (save-restriction
  314. (widen)
  315. (goto-char (point-min))
  316. (while (re-search-forward
  317. "\\(\\`\\|[\n\r]\\)[ \t]*\\\\begin{thebibliography}" nil t)
  318. (beginning-of-line 2)
  319. (setq start (point))
  320. (if (re-search-forward
  321. "\\(\\`\\|[\n\r]\\)[ \t]*\\\\end{thebibliography}" nil t)
  322. (progn
  323. (beginning-of-line 1)
  324. (setq end (point))))
  325. (when (and start end)
  326. (setq entries
  327. (append entries
  328. (mapcar 'reftex-parse-bibitem
  329. (delete ""
  330. (split-string
  331. (buffer-substring-no-properties
  332. start end)
  333. "[ \t\n\r]*\\\\bibitem[ \t]*\
  334. \\(\\[[^]]*]\\)*\[ \t]*"))))))
  335. (goto-char end))))))
  336. (unless entries
  337. (error "No bibitems found"))
  338. ;; Read a regexp, completing on known citation keys.
  339. (setq default (regexp-quote (reftex-get-bibkey-default)))
  340. (setq re-list
  341. (split-string
  342. (completing-read
  343. (concat
  344. "Regex { && Regex...}: "
  345. "[" default "]: ")
  346. (if reftex-mode
  347. (if (fboundp 'LaTeX-bibitem-list)
  348. (LaTeX-bibitem-list)
  349. (cdr (assoc 'bibview-cache
  350. (symbol-value reftex-docstruct-symbol))))
  351. nil)
  352. nil nil nil 'reftex-cite-regexp-hist)
  353. "[ \t]*&&[ \t]*"))
  354. (if (or (null re-list ) (equal re-list '("")))
  355. (setq re-list (list default)))
  356. (if (string-match "\\`[ \t]*\\'" (car re-list))
  357. (error "Empty regular expression"))
  358. (while (and (setq re (pop re-list)) entries)
  359. (setq entries
  360. (delq nil (mapcar
  361. (lambda (x)
  362. (if (string-match re (cdr (assoc "&entry" x)))
  363. x nil))
  364. entries))))
  365. (setq entries
  366. (mapcar
  367. (lambda (x)
  368. (push (cons "&formatted" (reftex-format-bibitem x)) x)
  369. (push (reftex-get-bib-field "&key" x) x)
  370. x)
  371. entries))
  372. entries))
  373. (defun reftex-get-bibkey-default ()
  374. ;; Return the word before the cursor. If the cursor is in a
  375. ;; citation macro, return the word before the macro.
  376. (let* ((macro (reftex-what-macro 1)))
  377. (save-excursion
  378. (if (and macro (string-match "cite" (car macro)))
  379. (goto-char (cdr macro)))
  380. (skip-chars-backward "^a-zA-Z0-9")
  381. (reftex-this-word))))
  382. ;; Parse and format individual entries
  383. (defun reftex-get-bib-names (field entry)
  384. ;; Return a list with the author or editor names in ENTRY
  385. (let ((names (reftex-get-bib-field field entry)))
  386. (if (equal "" names)
  387. (setq names (reftex-get-bib-field "editor" entry)))
  388. (while (string-match "\\band\\b[ \t]*" names)
  389. (setq names (replace-match "\n" nil t names)))
  390. (while (string-match "[\\.a-zA-Z\\-]+\\.[ \t]*\\|,.*\\|[{}]+" names)
  391. (setq names (replace-match "" nil t names)))
  392. (while (string-match "^[ \t]+\\|[ \t]+$" names)
  393. (setq names (replace-match "" nil t names)))
  394. (while (string-match "[ \t][ \t]+" names)
  395. (setq names (replace-match " " nil t names)))
  396. (split-string names "\n")))
  397. (defun reftex-parse-bibtex-entry (entry &optional from to)
  398. (let (alist key start field)
  399. (save-excursion
  400. (save-restriction
  401. (if entry
  402. (progn
  403. (set-buffer (get-buffer-create " *RefTeX-scratch*"))
  404. (fundamental-mode)
  405. (set-syntax-table reftex-syntax-table-for-bib)
  406. (erase-buffer)
  407. (insert entry))
  408. (widen)
  409. (narrow-to-region from to))
  410. (goto-char (point-min))
  411. (if (re-search-forward
  412. "@\\(\\w+\\)[ \t\n\r]*[{(][ \t\n\r]*\\([^ \t\n\r,]+\\)" nil t)
  413. (setq alist
  414. (list
  415. (cons "&type" (downcase (reftex-match-string 1)))
  416. (cons "&key" (reftex-match-string 2)))))
  417. (while (re-search-forward "\\(\\w+\\)[ \t\n\r]*=[ \t\n\r]*" nil t)
  418. (setq key (downcase (reftex-match-string 1)))
  419. (cond
  420. ((= (following-char) ?{)
  421. (forward-char 1)
  422. (setq start (point))
  423. (condition-case nil
  424. (up-list 1)
  425. (error nil)))
  426. ((= (following-char) ?\")
  427. (forward-char 1)
  428. (setq start (point))
  429. (while (and (search-forward "\"" nil t)
  430. (= ?\\ (char-after (- (point) 2))))))
  431. (t
  432. (setq start (point))
  433. (re-search-forward "[ \t]*[\n\r,}]" nil 1)))
  434. (setq field (buffer-substring-no-properties start (1- (point))))
  435. ;; remove extra whitespace
  436. (while (string-match "[\n\t\r]\\|[ \t][ \t]+" field)
  437. (setq field (replace-match " " nil t field)))
  438. ;; remove leading garbage
  439. (if (string-match "^[ \t{]+" field)
  440. (setq field (replace-match "" nil t field)))
  441. ;; remove trailing garbage
  442. (if (string-match "[ \t}]+$" field)
  443. (setq field (replace-match "" nil t field)))
  444. (push (cons key field) alist))))
  445. alist))
  446. (defun reftex-get-bib-field (fieldname entry &optional format)
  447. ;; Extract the field FIELDNAME from an ENTRY
  448. (let ((cell (assoc fieldname entry)))
  449. (if cell
  450. (if format
  451. (format format (cdr cell))
  452. (cdr cell))
  453. "")))
  454. (defun reftex-format-bib-entry (entry)
  455. ;; Format a BibTeX ENTRY so that it is nice to look at
  456. (let*
  457. ((auth-list (reftex-get-bib-names "author" entry))
  458. (authors (mapconcat 'identity auth-list ", "))
  459. (year (reftex-get-bib-field "year" entry))
  460. (title (reftex-get-bib-field "title" entry))
  461. (type (reftex-get-bib-field "&type" entry))
  462. (key (reftex-get-bib-field "&key" entry))
  463. (extra
  464. (cond
  465. ((equal type "article")
  466. (concat (reftex-get-bib-field "journal" entry) " "
  467. (reftex-get-bib-field "volume" entry) ", "
  468. (reftex-get-bib-field "pages" entry)))
  469. ((equal type "book")
  470. (concat "book (" (reftex-get-bib-field "publisher" entry) ")"))
  471. ((equal type "phdthesis")
  472. (concat "PhD: " (reftex-get-bib-field "school" entry)))
  473. ((equal type "mastersthesis")
  474. (concat "Master: " (reftex-get-bib-field "school" entry)))
  475. ((equal type "inbook")
  476. (concat "Chap: " (reftex-get-bib-field "chapter" entry)
  477. ", pp. " (reftex-get-bib-field "pages" entry)))
  478. ((or (equal type "conference")
  479. (equal type "incollection")
  480. (equal type "inproceedings"))
  481. (reftex-get-bib-field "booktitle" entry "in: %s"))
  482. (t ""))))
  483. (setq authors (reftex-truncate authors 30 t t))
  484. (when (reftex-use-fonts)
  485. (put-text-property 0 (length key) 'face
  486. (reftex-verified-face reftex-label-face
  487. 'font-lock-constant-face
  488. 'font-lock-reference-face)
  489. key)
  490. (put-text-property 0 (length authors) 'face reftex-bib-author-face
  491. authors)
  492. (put-text-property 0 (length year) 'face reftex-bib-year-face
  493. year)
  494. (put-text-property 0 (length title) 'face reftex-bib-title-face
  495. title)
  496. (put-text-property 0 (length extra) 'face reftex-bib-extra-face
  497. extra))
  498. (concat key "\n " authors " " year " " extra "\n " title "\n\n")))
  499. (defun reftex-parse-bibitem (item)
  500. ;; Parse a \bibitem entry
  501. (let ((key "") (text ""))
  502. (when (string-match "\\`{\\([^}]+\\)}\\([^\000]*\\)" item)
  503. (setq key (match-string 1 item)
  504. text (match-string 2 item)))
  505. ;; Clean up the text a little bit
  506. (while (string-match "[\n\r\t]\\|[ \t][ \t]+" text)
  507. (setq text (replace-match " " nil t text)))
  508. (if (string-match "\\`[ \t]+" text)
  509. (setq text (replace-match "" nil t text)))
  510. (list
  511. (cons "&key" key)
  512. (cons "&text" text)
  513. (cons "&entry" (concat key " " text)))))
  514. (defun reftex-format-bibitem (item)
  515. ;; Format a \bibitem entry so that it is (relatively) nice to look at.
  516. (let ((text (reftex-get-bib-field "&text" item))
  517. (key (reftex-get-bib-field "&key" item))
  518. (lines nil))
  519. ;; Wrap the text into several lines.
  520. (while (and (> (length text) 70)
  521. (string-match " " (substring text 60)))
  522. (push (substring text 0 (+ 60 (match-beginning 0))) lines)
  523. (setq text (substring text (+ 61 (match-beginning 0)))))
  524. (push text lines)
  525. (setq text (mapconcat 'identity (nreverse lines) "\n "))
  526. (when (reftex-use-fonts)
  527. (put-text-property 0 (length text) 'face reftex-bib-author-face text))
  528. (concat key "\n " text "\n\n")))
  529. ;; Make a citation
  530. ;;;###autoload
  531. (defun reftex-citation (&optional no-insert format-key)
  532. "Make a citation using BibTeX database files.
  533. After prompting for a regular expression, scans the buffers with
  534. bibtex entries (taken from the \\bibliography command) and offers the
  535. matching entries for selection. The selected entry is formatted according
  536. to `reftex-cite-format' and inserted into the buffer.
  537. If NO-INSERT is non-nil, nothing is inserted, only the selected key returned.
  538. FORMAT-KEY can be used to pre-select a citation format.
  539. When called with a `C-u' prefix, prompt for optional arguments in
  540. cite macros. When called with a numeric prefix, make that many
  541. citations. When called with point inside the braces of a `\\cite'
  542. command, it will add another key, ignoring the value of
  543. `reftex-cite-format'.
  544. The regular expression uses an expanded syntax: && is interpreted as `and'.
  545. Thus, `aaaa&&bbb' matches entries which contain both `aaaa' and `bbb'.
  546. While entering the regexp, completion on knows citation keys is possible.
  547. `=' is a good regular expression to match all entries in all files."
  548. (interactive)
  549. ;; check for recursive edit
  550. (reftex-check-recursive-edit)
  551. ;; This function may also be called outside reftex-mode.
  552. ;; Thus look for the scanning info only if in reftex-mode.
  553. (when reftex-mode
  554. (reftex-access-scan-info nil))
  555. ;; Call reftex-do-citation, but protected
  556. (unwind-protect
  557. (reftex-do-citation current-prefix-arg no-insert format-key)
  558. (reftex-kill-temporary-buffers)))
  559. (defun reftex-do-citation (&optional arg no-insert format-key)
  560. ;; This really does the work of reftex-citation.
  561. (let* ((format (reftex-figure-out-cite-format arg no-insert format-key))
  562. (docstruct-symbol reftex-docstruct-symbol)
  563. (selected-entries (reftex-offer-bib-menu))
  564. (insert-entries selected-entries)
  565. entry string cite-view)
  566. (when (stringp selected-entries)
  567. (error selected-entries))
  568. (unless selected-entries (error "Quit"))
  569. (if (stringp selected-entries)
  570. ;; Nonexistent entry
  571. (setq selected-entries nil
  572. insert-entries (list (list selected-entries
  573. (cons "&key" selected-entries))))
  574. ;; It makes sense to compute the cite-view strings.
  575. (setq cite-view t))
  576. (when (eq (car selected-entries) 'concat)
  577. ;; All keys go into a single command - we need to trick a little
  578. ;; FIXME: Unfortunately, this means that commenting does not work right.
  579. (pop selected-entries)
  580. (let ((concat-keys (mapconcat 'car selected-entries ",")))
  581. (setq insert-entries
  582. (list (list concat-keys (cons "&key" concat-keys))))))
  583. (unless no-insert
  584. ;; We shall insert this into the buffer...
  585. (message "Formatting...")
  586. (while (setq entry (pop insert-entries))
  587. ;; Format the citation and insert it
  588. (setq string (if reftex-format-cite-function
  589. (funcall reftex-format-cite-function
  590. (reftex-get-bib-field "&key" entry)
  591. format)
  592. (reftex-format-citation entry format)))
  593. (when (or (eq reftex-cite-prompt-optional-args t)
  594. (and reftex-cite-prompt-optional-args
  595. (equal arg '(4))))
  596. (let ((start 0) (nth 0) value)
  597. (while (setq start (string-match "\\[\\]" string start))
  598. (setq value (read-string (format "Optional argument %d: "
  599. (setq nth (1+ nth)))))
  600. (setq string (replace-match (concat "[" value "]") t t string))
  601. (setq start (1+ start)))))
  602. ;; Should we cleanup empty optional arguments?
  603. ;; if the first is empty, it can be removed. If the second is empty,
  604. ;; it has to go. If there is only a single arg and empty, it can go
  605. ;; as well.
  606. (when reftex-cite-cleanup-optional-args
  607. (cond
  608. ((string-match "\\([a-zA-Z0-9]\\)\\[\\]{" string)
  609. (setq string (replace-match "\\1{" nil nil string)))
  610. ((string-match "\\[\\]\\(\\[[a-zA-Z0-9., ]+\\]\\)" string)
  611. (setq string (replace-match "\\1" nil nil string)))
  612. ((string-match "\\[\\]\\[\\]" string)
  613. (setq string (replace-match "" t t string)))))
  614. (insert string))
  615. ;; Reposition cursor?
  616. (when (string-match "\\?" string)
  617. (search-backward "?")
  618. (delete-char 1))
  619. ;; Tell AUCTeX
  620. (when (and reftex-mode
  621. (fboundp 'LaTeX-add-bibitems)
  622. reftex-plug-into-AUCTeX)
  623. (apply 'LaTeX-add-bibitems (mapcar 'car selected-entries)))
  624. ;; Produce the cite-view strings
  625. (when (and reftex-mode reftex-cache-cite-echo cite-view)
  626. (mapc (lambda (entry)
  627. (reftex-make-cite-echo-string entry docstruct-symbol))
  628. selected-entries))
  629. (message ""))
  630. (set-marker reftex-select-return-marker nil)
  631. (reftex-kill-buffer "*RefTeX Select*")
  632. ;; Check if the prefix arg was numeric, and call recursively
  633. (when (integerp arg)
  634. (if (> arg 1)
  635. (progn
  636. (skip-chars-backward "}")
  637. (decf arg)
  638. (reftex-do-citation arg))
  639. (forward-char 1)))
  640. ;; Return the citation key
  641. (car (car selected-entries))))
  642. (defun reftex-figure-out-cite-format (arg &optional no-insert format-key)
  643. ;; Check if there is already a cite command at point and change cite format
  644. ;; in order to only add another reference in the same cite command.
  645. (let ((macro (car (reftex-what-macro 1)))
  646. (cite-format-value (reftex-get-cite-format))
  647. key format)
  648. (cond
  649. (no-insert
  650. ;; Format does not really matter because nothing will be inserted.
  651. (setq format "%l"))
  652. ((and (stringp macro)
  653. (string-match "\\`\\\\cite\\|cite\\'" macro))
  654. ;; We are already inside a cite macro
  655. (if (or (not arg) (not (listp arg)))
  656. (setq format
  657. (concat
  658. (if (member (preceding-char) '(?\{ ?,)) "" ",")
  659. "%l"
  660. (if (member (following-char) '(?\} ?,)) "" ",")))
  661. (setq format "%l")))
  662. (t
  663. ;; Figure out the correct format
  664. (setq format
  665. (if (and (symbolp cite-format-value)
  666. (assq cite-format-value reftex-cite-format-builtin))
  667. (nth 2 (assq cite-format-value reftex-cite-format-builtin))
  668. cite-format-value))
  669. (when (listp format)
  670. (setq key
  671. (or format-key
  672. (reftex-select-with-char
  673. "" (concat "SELECT A CITATION FORMAT\n\n"
  674. (mapconcat
  675. (lambda (x)
  676. (format "[%c] %s %s" (car x)
  677. (if (> (car x) 31) " " "")
  678. (cdr x)))
  679. format "\n")))))
  680. (if (assq key format)
  681. (setq format (cdr (assq key format)))
  682. (error "No citation format associated with key `%c'" key)))))
  683. format))
  684. (defun reftex-citep ()
  685. "Call `reftex-citation' with a format selector `?p'."
  686. (interactive)
  687. (reftex-citation nil ?p))
  688. (defun reftex-citet ()
  689. "Call `reftex-citation' with a format selector `?t'."
  690. (interactive)
  691. (reftex-citation nil ?t))
  692. (defvar reftex-select-bib-map)
  693. (defun reftex-offer-bib-menu ()
  694. ;; Offer bib menu and return list of selected items
  695. (let ((bibtype (reftex-bib-or-thebib))
  696. found-list rtn key data selected-entries)
  697. (while
  698. (not
  699. (catch 'done
  700. ;; Scan bibtex files
  701. (setq found-list
  702. (cond
  703. ((eq bibtype 'bib)
  704. ; ((assq 'bib (symbol-value reftex-docstruct-symbol))
  705. ;; using BibTeX database files.
  706. (reftex-extract-bib-entries (reftex-get-bibfile-list)))
  707. ((eq bibtype 'thebib)
  708. ; ((assq 'thebib (symbol-value reftex-docstruct-symbol))
  709. ;; using thebibliography environment.
  710. (reftex-extract-bib-entries-from-thebibliography
  711. (reftex-uniquify
  712. (mapcar 'cdr
  713. (reftex-all-assq
  714. 'thebib (symbol-value reftex-docstruct-symbol))))))
  715. (reftex-default-bibliography
  716. (message "Using default bibliography")
  717. (reftex-extract-bib-entries (reftex-default-bibliography)))
  718. (t (error "No valid bibliography in this document, and no default available"))))
  719. (unless found-list
  720. (error "Sorry, no matches found"))
  721. ;; Remember where we came from
  722. (setq reftex-call-back-to-this-buffer (current-buffer))
  723. (set-marker reftex-select-return-marker (point))
  724. ;; Offer selection
  725. (save-window-excursion
  726. (delete-other-windows)
  727. (reftex-kill-buffer "*RefTeX Select*")
  728. (switch-to-buffer-other-window "*RefTeX Select*")
  729. (unless (eq major-mode 'reftex-select-bib-mode)
  730. (reftex-select-bib-mode))
  731. (let ((buffer-read-only nil))
  732. (erase-buffer)
  733. (reftex-insert-bib-matches found-list))
  734. (setq buffer-read-only t)
  735. (if (= 0 (buffer-size))
  736. (error "No matches found"))
  737. (setq truncate-lines t)
  738. (goto-char 1)
  739. (while t
  740. (setq rtn
  741. (reftex-select-item
  742. reftex-citation-prompt
  743. reftex-citation-help
  744. reftex-select-bib-map
  745. nil
  746. 'reftex-bibtex-selection-callback nil))
  747. (setq key (car rtn)
  748. data (nth 1 rtn))
  749. (unless key (throw 'done t))
  750. (cond
  751. ((eq key ?g)
  752. ;; Start over
  753. (throw 'done nil))
  754. ((eq key ?r)
  755. ;; Restrict with new regular expression
  756. (setq found-list (reftex-restrict-bib-matches found-list))
  757. (let ((buffer-read-only nil))
  758. (erase-buffer)
  759. (reftex-insert-bib-matches found-list))
  760. (goto-char 1))
  761. ((eq key ?A)
  762. ;; Take all (marked)
  763. (setq selected-entries
  764. (if reftex-select-marked
  765. (mapcar 'car (nreverse reftex-select-marked))
  766. found-list))
  767. (throw 'done t))
  768. ((eq key ?a)
  769. ;; Take all (marked), and push the symbol 'concat
  770. (setq selected-entries
  771. (cons 'concat
  772. (if reftex-select-marked
  773. (mapcar 'car (nreverse reftex-select-marked))
  774. found-list)))
  775. (throw 'done t))
  776. ((eq key ?e)
  777. ;; Take all (marked), and push the symbol 'concat
  778. (reftex-extract-bib-file found-list reftex-select-marked)
  779. (setq selected-entries "BibTeX database file created")
  780. (throw 'done t))
  781. ((eq key ?E)
  782. ;; Take all (marked), and push the symbol 'concat
  783. (reftex-extract-bib-file found-list reftex-select-marked
  784. 'complement)
  785. (setq selected-entries "BibTeX database file created")
  786. (throw 'done t))
  787. ((or (eq key ?\C-m)
  788. (eq key 'return))
  789. ;; Take selected
  790. (setq selected-entries
  791. (if reftex-select-marked
  792. (cons 'concat
  793. (mapcar 'car (nreverse reftex-select-marked)))
  794. (if data (list data) nil)))
  795. (throw 'done t))
  796. ((stringp key)
  797. ;; Got this one with completion
  798. (setq selected-entries key)
  799. (throw 'done t))
  800. (t
  801. (ding))))))))
  802. selected-entries))
  803. (defun reftex-restrict-bib-matches (found-list)
  804. ;; Limit FOUND-LIST with more regular expressions
  805. (let ((re-list (split-string (read-string
  806. "RegExp [ && RegExp...]: "
  807. nil 'reftex-cite-regexp-hist)
  808. "[ \t]*&&[ \t]*"))
  809. (found-list-r found-list)
  810. re)
  811. (while (setq re (pop re-list))
  812. (setq found-list-r
  813. (delq nil
  814. (mapcar
  815. (lambda (x)
  816. (if (string-match
  817. re (cdr (assoc "&entry" x)))
  818. x
  819. nil))
  820. found-list-r))))
  821. (if found-list-r
  822. found-list-r
  823. (ding)
  824. found-list)))
  825. (defun reftex-extract-bib-file (all &optional marked complement)
  826. ;; Limit FOUND-LIST with more regular expressions
  827. (let ((file (read-file-name "File to create: ")))
  828. (find-file-other-window file)
  829. (if (> (buffer-size) 0)
  830. (unless (yes-or-no-p
  831. (format "Overwrite non-empty file %s? " file))
  832. (error "Abort")))
  833. (erase-buffer)
  834. (setq all (delq nil
  835. (mapcar
  836. (lambda (x)
  837. (if marked
  838. (if (or (and (assoc x marked) (not complement))
  839. (and (not (assoc x marked)) complement))
  840. (cdr (assoc "&entry" x))
  841. nil)
  842. (cdr (assoc "&entry" x))))
  843. all)))
  844. (insert (mapconcat 'identity all "\n\n"))
  845. (save-buffer)
  846. (goto-char (point-min))))
  847. (defun reftex-insert-bib-matches (list)
  848. ;; Insert the bib matches and number them correctly
  849. (let ((mouse-face
  850. (if (memq reftex-highlight-selection '(mouse both))
  851. reftex-mouse-selected-face
  852. nil))
  853. tmp len)
  854. (mapc
  855. (lambda (x)
  856. (setq tmp (cdr (assoc "&formatted" x))
  857. len (length tmp))
  858. (put-text-property 0 len :data x tmp)
  859. (put-text-property 0 (1- len) 'mouse-face mouse-face tmp)
  860. (insert tmp))
  861. list))
  862. (run-hooks 'reftex-display-copied-context-hook))
  863. (defun reftex-format-names (namelist n)
  864. (let (last (len (length namelist)))
  865. (if (= n 0) (setq n len))
  866. (cond
  867. ((< len 1) "")
  868. ((= 1 len) (car namelist))
  869. ((> len n) (concat (car namelist) (nth 2 reftex-cite-punctuation)))
  870. (t
  871. (setq n (min len n)
  872. last (nth (1- n) namelist))
  873. (setcdr (nthcdr (- n 2) namelist) nil)
  874. (concat
  875. (mapconcat 'identity namelist (nth 0 reftex-cite-punctuation))
  876. (nth 1 reftex-cite-punctuation)
  877. last)))))
  878. (defun reftex-format-citation (entry format)
  879. ;; Format a citation from the info in the BibTeX ENTRY
  880. (unless (stringp format) (setq format "\\cite{%l}"))
  881. (if (and reftex-comment-citations
  882. (string-match "%l" reftex-cite-comment-format))
  883. (error "reftex-cite-comment-format contains invalid %%l"))
  884. (while (string-match
  885. "\\(\\`\\|[^%]\\)\\(\\(%\\([0-9]*\\)\\([a-zA-Z]\\)\\)[.,;: ]*\\)"
  886. format)
  887. (let ((n (string-to-number (match-string 4 format)))
  888. (l (string-to-char (match-string 5 format)))
  889. rpl b e)
  890. (save-match-data
  891. (setq rpl
  892. (cond
  893. ((= l ?l) (concat
  894. (reftex-get-bib-field "&key" entry)
  895. (if reftex-comment-citations
  896. reftex-cite-comment-format
  897. "")))
  898. ((= l ?a) (reftex-format-names
  899. (reftex-get-bib-names "author" entry)
  900. (or n 2)))
  901. ((= l ?A) (car (reftex-get-bib-names "author" entry)))
  902. ((= l ?b) (reftex-get-bib-field "booktitle" entry "in: %s"))
  903. ((= l ?B) (reftex-abbreviate-title
  904. (reftex-get-bib-field "booktitle" entry "in: %s")))
  905. ((= l ?c) (reftex-get-bib-field "chapter" entry))
  906. ((= l ?d) (reftex-get-bib-field "edition" entry))
  907. ((= l ?e) (reftex-format-names
  908. (reftex-get-bib-names "editor" entry)
  909. (or n 2)))
  910. ((= l ?E) (car (reftex-get-bib-names "editor" entry)))
  911. ((= l ?h) (reftex-get-bib-field "howpublished" entry))
  912. ((= l ?i) (reftex-get-bib-field "institution" entry))
  913. ((= l ?j) (reftex-get-bib-field "journal" entry))
  914. ((= l ?k) (reftex-get-bib-field "key" entry))
  915. ((= l ?m) (reftex-get-bib-field "month" entry))
  916. ((= l ?n) (reftex-get-bib-field "number" entry))
  917. ((= l ?o) (reftex-get-bib-field "organization" entry))
  918. ((= l ?p) (reftex-get-bib-field "pages" entry))
  919. ((= l ?P) (car (split-string
  920. (reftex-get-bib-field "pages" entry)
  921. "[- .]+")))
  922. ((= l ?s) (reftex-get-bib-field "school" entry))
  923. ((= l ?u) (reftex-get-bib-field "publisher" entry))
  924. ((= l ?r) (reftex-get-bib-field "address" entry))
  925. ((= l ?t) (reftex-get-bib-field "title" entry))
  926. ((= l ?T) (reftex-abbreviate-title
  927. (reftex-get-bib-field "title" entry)))
  928. ((= l ?v) (reftex-get-bib-field "volume" entry))
  929. ((= l ?y) (reftex-get-bib-field "year" entry)))))
  930. (if (string= rpl "")
  931. (setq b (match-beginning 2) e (match-end 2))
  932. (setq b (match-beginning 3) e (match-end 3)))
  933. (setq format (concat (substring format 0 b) rpl (substring format e)))))
  934. (while (string-match "%%" format)
  935. (setq format (replace-match "%" t t format)))
  936. (while (string-match "[ ,.;:]*%<" format)
  937. (setq format (replace-match "" t t format)))
  938. format)
  939. (defun reftex-make-cite-echo-string (entry docstruct-symbol)
  940. ;; Format a bibtex entry for the echo area and cache the result.
  941. (let* ((key (reftex-get-bib-field "&key" entry))
  942. (string
  943. (let* ((reftex-cite-punctuation '(" " " & " " etal.")))
  944. (reftex-format-citation entry reftex-cite-view-format)))
  945. (cache (assq 'bibview-cache (symbol-value docstruct-symbol)))
  946. (cache-entry (assoc key (cdr cache))))
  947. (unless cache
  948. ;; This docstruct has no cache - make one.
  949. (set docstruct-symbol (cons (cons 'bibview-cache nil)
  950. (symbol-value docstruct-symbol))))
  951. (when reftex-cache-cite-echo
  952. (setq key (copy-sequence key))
  953. (set-text-properties 0 (length key) nil key)
  954. (set-text-properties 0 (length string) nil string)
  955. (if cache-entry
  956. (unless (string= (cdr cache-entry) string)
  957. (setcdr cache-entry string)
  958. (put reftex-docstruct-symbol 'modified t))
  959. (push (cons key string) (cdr cache))
  960. (put reftex-docstruct-symbol 'modified t)))
  961. string))
  962. (defun reftex-bibtex-selection-callback (data ignore no-revisit)
  963. ;; Callback function to be called from the BibTeX selection, in
  964. ;; order to display context. This function is relatively slow and not
  965. ;; recommended for follow mode. It works OK for individual lookups.
  966. (let ((win (selected-window))
  967. (key (reftex-get-bib-field "&key" data))
  968. bibfile-list item bibtype)
  969. (catch 'exit
  970. (with-current-buffer reftex-call-back-to-this-buffer
  971. (setq bibtype (reftex-bib-or-thebib))
  972. (cond
  973. ((eq bibtype 'bib)
  974. ; ((assq 'bib (symbol-value reftex-docstruct-symbol))
  975. (setq bibfile-list (reftex-get-bibfile-list)))
  976. ((eq bibtype 'thebib)
  977. ; ((assq 'thebib (symbol-value reftex-docstruct-symbol))
  978. (setq bibfile-list
  979. (reftex-uniquify
  980. (mapcar 'cdr
  981. (reftex-all-assq
  982. 'thebib (symbol-value reftex-docstruct-symbol))))
  983. item t))
  984. (reftex-default-bibliography
  985. (setq bibfile-list (reftex-default-bibliography)))
  986. (t (ding) (throw 'exit nil))))
  987. (when no-revisit
  988. (setq bibfile-list (reftex-visited-files bibfile-list)))
  989. (condition-case nil
  990. (reftex-pop-to-bibtex-entry
  991. key bibfile-list (not reftex-keep-temporary-buffers) t item)
  992. (error (ding))))
  993. (select-window win)))
  994. ;;; Global BibTeX file
  995. (defun reftex-all-used-citation-keys ()
  996. (reftex-access-scan-info)
  997. (let ((files (reftex-all-document-files)) file keys kk k)
  998. (save-current-buffer
  999. (while (setq file (pop files))
  1000. (set-buffer (reftex-get-file-buffer-force file 'mark))
  1001. (save-excursion
  1002. (save-restriction
  1003. (widen)
  1004. (goto-char (point-min))
  1005. (while (re-search-forward "^[^%\n\r]*\\\\\\(bibentry\\|[a-zA-Z]*cite[a-zA-Z]*\\)\\(\\[[^\\]]*\\]\\)?{\\([^}]+\\)}" nil t)
  1006. (setq kk (match-string-no-properties 3))
  1007. (while (string-match "%.*\n?" kk)
  1008. (setq kk (replace-match "" t t kk)))
  1009. (setq kk (split-string kk "[, \t\r\n]+"))
  1010. (while (setq k (pop kk))
  1011. (or (member k keys)
  1012. (setq keys (cons k keys)))))))))
  1013. (reftex-kill-temporary-buffers)
  1014. keys))
  1015. (defun reftex-create-bibtex-file (bibfile)
  1016. "Create a new BibTeX database file with all entries referenced in document.
  1017. The command prompts for a filename and writes the collected entries to
  1018. that file. Only entries referenced in the current document with
  1019. any \\cite-like macros are used.
  1020. The sequence in the new file is the same as it was in the old database."
  1021. (interactive "FNew BibTeX file: ")
  1022. (let ((keys (reftex-all-used-citation-keys))
  1023. (files (reftex-get-bibfile-list))
  1024. file key entries beg end entry)
  1025. (save-current-buffer
  1026. (while (setq file (pop files))
  1027. (set-buffer (reftex-get-file-buffer-force file 'mark))
  1028. (reftex-with-special-syntax-for-bib
  1029. (save-excursion
  1030. (save-restriction
  1031. (widen)
  1032. (goto-char (point-min))
  1033. (while (re-search-forward "^[ \t]*@\\(?:\\w\\|\\s_\\)+[ \t\n\r]*\
  1034. \[{(][ \t\n\r]*\\([^ \t\n\r,]+\\)" nil t)
  1035. (setq key (match-string 1)
  1036. beg (match-beginning 0)
  1037. end (progn
  1038. (goto-char (match-beginning 1))
  1039. (condition-case nil
  1040. (up-list 1)
  1041. (error (goto-char (match-end 0))))
  1042. (point)))
  1043. (when (member key keys)
  1044. (setq entry (buffer-substring beg end)
  1045. entries (cons entry entries)
  1046. keys (delete key keys)))))))))
  1047. (find-file-other-window bibfile)
  1048. (if (> (buffer-size) 0)
  1049. (unless (yes-or-no-p
  1050. (format "Overwrite non-empty file %s? " bibfile))
  1051. (error "Abort")))
  1052. (erase-buffer)
  1053. (insert (mapconcat 'identity (reverse entries) "\n\n"))
  1054. (goto-char (point-min))
  1055. (save-buffer)
  1056. (message "%d entries extracted and copied to new database"
  1057. (length entries))))
  1058. ;;; reftex-cite.el ends here