1 ;;; reftex-cite.el --- creating citations with RefTeX
2 ;; Copyright (c) 1997, 1998, 1999, 2000, 2003, 2004, 2005
3 ;; Free Software Foundation, Inc.
5 ;; Author: Carsten Dominik <dominik@science.uva.nl>
8 ;; This file is part of GNU Emacs.
10 ;; GNU Emacs is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING. If not, write to the
22 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, USA.
29 (eval-when-compile (require 'cl
))
30 (provide 'reftex-cite
)
34 ;; Variables and constants
36 ;; The history list of regular expressions used for citations
37 (defvar reftex-cite-regexp-hist nil
)
39 ;; Prompt and help string for citation selection
40 (defconst reftex-citation-prompt
41 "Select: [n]ext [p]revious [r]estrict [ ]full_entry [q]uit RET [?]Help+more")
43 (defconst reftex-citation-help
44 " n / p Go to next/previous entry (Cursor motion works as well).
45 g / r Start over with new regexp / Refine with additional regexp.
46 SPC Show full database entry in other window.
47 f Toggle follow mode: Other window will follow with full db entry.
48 . Show insertion point.
49 q Quit without inserting \\cite macro into buffer.
50 TAB Enter citation key with completion.
51 RET Accept current entry (also on mouse-2) and create \\cite macro.
52 m / u Mark/Unmark the entry.
53 e / E Create BibTeX file with all (marked/unmarked) entries
54 a / A Put all (marked) entries into one/many \\cite commands.")
58 (defmacro reftex-with-special-syntax-for-bib
(&rest body
)
59 `(let ((saved-syntax (syntax-table)))
62 (set-syntax-table reftex-syntax-table-for-bib
)
64 (set-syntax-table saved-syntax
))))
66 (defun reftex-default-bibliography ()
67 ;; Return the expanded value of `reftex-default-bibliography'.
68 ;; The expanded value is cached.
69 (unless (eq (get 'reftex-default-bibliography
:reftex-raw
)
70 reftex-default-bibliography
)
71 (put 'reftex-default-bibliography
:reftex-expanded
72 (reftex-locate-bibliography-files
73 default-directory reftex-default-bibliography
))
74 (put 'reftex-default-bibliography
:reftex-raw
75 reftex-default-bibliography
))
76 (get 'reftex-default-bibliography
:reftex-expanded
))
78 (defun reftex-bib-or-thebib ()
79 ;; Tests if BibTeX or \begin{tehbibliography} should be used for the
81 ;; Find the bof of the current file
82 (let* ((docstruct (symbol-value reftex-docstruct-symbol
))
83 (rest (or (member (list 'bof
(buffer-file-name)) docstruct
)
85 (bib (assq 'bib rest
))
86 (thebib (assq 'thebib rest
))
87 (bibmem (memq bib rest
))
88 (thebibmem (memq thebib rest
)))
89 (when (not (or thebib bib
))
90 (setq bib
(assq 'bib docstruct
)
91 thebib
(assq 'thebib docstruct
)
92 bibmem
(memq bib docstruct
)
93 thebibmem
(memq thebib docstruct
)))
94 (if (> (length bibmem
) (length thebibmem
))
96 (if thebib
'thebib nil
))))
98 (defun reftex-get-bibfile-list ()
99 ;; Return list of bibfiles for current document.
100 ;; When using the chapterbib or bibunits package you should either
101 ;; use the same database files everywhere, or separate parts using
102 ;; different databases into different files (included into the mater file).
103 ;; Then this function will return the applicable database files.
105 ;; Ensure access to scanning info
106 (reftex-access-scan-info)
108 ;; Try inside this file (and its includes)
109 (cdr (reftex-last-assoc-before-elt
110 'bib
(list 'eof
(buffer-file-name))
111 (member (list 'bof
(buffer-file-name))
112 (symbol-value reftex-docstruct-symbol
))))
113 ;; Try after the beginning of this file
114 (cdr (assq 'bib
(member (list 'bof
(buffer-file-name))
115 (symbol-value reftex-docstruct-symbol
))))
116 ;; Anywhere in the entire document
117 (cdr (assq 'bib
(symbol-value reftex-docstruct-symbol
)))
118 (error "\\bibliography statement missing or .bib files not found")))
120 ;; Find a certain reference in any of the BibTeX files.
122 (defun reftex-pop-to-bibtex-entry (key file-list
&optional mark-to-kill
123 highlight item return
)
124 ;; Find BibTeX KEY in any file in FILE-LIST in another window.
125 ;; If MARK-TO-KILL is non-nil, mark new buffer to kill.
126 ;; If HIGHLIGHT is non-nil, highlight the match.
127 ;; If ITEM in non-nil, search for bibitem instead of database entry.
128 ;; If RETURN is non-nil, just return the entry.
132 (concat "\\\\bibitem\\(\\[[^]]*\\]\\)?{" (regexp-quote key
) "}")
133 (concat "@[a-zA-Z]+[ \t\n\r]*[{(][ \t\n\r]*" (regexp-quote key
)
135 (buffer-conf (current-buffer))
140 (setq file
(car file-list
)
141 file-list
(cdr file-list
))
142 (unless (setq buf
(reftex-get-file-buffer-force file mark-to-kill
))
143 (error "No such file %s" file
))
146 (goto-char (point-min))
147 (when (re-search-forward re nil t
)
148 (goto-char (match-beginning 0))
151 ;; Just return the relevant entry
152 (if item
(goto-char (match-end 0)))
153 (setq return
(buffer-substring
154 (point) (reftex-end-of-bib-entry item
)))
155 (set-buffer buffer-conf
)
156 (throw 'exit return
))
157 (switch-to-buffer-other-window buf
)
161 (reftex-highlight 0 (match-beginning 0) (match-end 0)))
162 (throw 'exit
(selected-window))))
163 (set-buffer buffer-conf
)
165 (error "No \\bibitem with citation key %s" key
)
166 (error "No BibTeX entry with citation key %s" key
)))))
168 (defun reftex-end-of-bib-entry (item)
174 "\\\\bibitem\\|\\end{thebibliography}")
175 (1- (match-beginning 0)))
176 (progn (forward-list 1) (point)))
177 (error (min (point-max) (+ 300 (point)))))))
179 ;; Parse bibtex buffers
181 (defun reftex-extract-bib-entries (buffers)
182 ;; Extract bib entries which match regexps from BUFFERS.
183 ;; BUFFERS is a list of buffers or file names.
184 ;; Return list with entries."
185 (let* (re-list first-re rest-re
186 (buffer-list (if (listp buffers
) buffers
(list buffers
)))
187 found-list entry buffer1 buffer alist
188 key-point start-point end-point default
)
190 ;; Read a regexp, completing on known citation keys.
191 (setq default
(regexp-quote (reftex-get-bibkey-default)))
196 "Regex { && Regex...}: "
199 (if (fboundp 'LaTeX-bibitem-list
)
201 (cdr (assoc 'bibview-cache
202 (symbol-value reftex-docstruct-symbol
))))
204 nil nil nil
'reftex-cite-regexp-hist
)
207 (if (or (null re-list
) (equal re-list
'("")))
208 (setq re-list
(list default
)))
210 (setq first-re
(car re-list
) ; We'll use the first re to find things,
211 rest-re
(cdr re-list
)) ; the others to narrow down.
212 (if (string-match "\\`[ \t]*\\'" (or first-re
""))
213 (error "Empty regular expression"))
216 (save-window-excursion
218 ;; Walk through all bibtex files
220 (setq buffer
(car buffer-list
)
221 buffer-list
(cdr buffer-list
))
222 (if (and (bufferp buffer
)
223 (buffer-live-p buffer
))
224 (setq buffer1 buffer
)
225 (setq buffer1
(reftex-get-file-buffer-force
226 buffer
(not reftex-keep-temporary-buffers
))))
228 (message "No such BibTeX file %s (ignored)" buffer
)
229 (message "Scanning bibliography database %s" buffer1
))
232 (reftex-with-special-syntax-for-bib
234 (goto-char (point-min))
235 (while (re-search-forward first-re nil t
)
237 (setq key-point
(point))
238 (unless (re-search-backward
239 "\\(\\`\\|[\n\r]\\)[ \t]*@\\([a-zA-Z]+\\)[ \t\n\r]*[{(]" nil t
)
240 (throw 'search-again nil
))
241 (setq start-point
(point))
242 (goto-char (match-end 0))
245 (error (goto-char key-point
)
246 (throw 'search-again nil
)))
247 (setq end-point
(point))
249 ;; Ignore @string, @comment and @c entries or things
251 (when (or (string= (downcase (match-string 2)) "string")
252 (string= (downcase (match-string 2)) "comment")
253 (string= (downcase (match-string 2)) "c")
254 (< (point) key-point
)) ; this means match not in {}
255 (goto-char key-point
)
256 (throw 'search-again nil
))
258 ;; Well, we have got a match
259 ;;(setq entry (concat
260 ;; (buffer-substring start-point (point)) "\n"))
261 (setq entry
(buffer-substring start-point
(point)))
263 ;; Check if other regexp match as well
264 (setq re-list rest-re
)
266 (unless (string-match (car re-list
) entry
)
268 (throw 'search-again nil
))
271 (setq alist
(reftex-parse-bibtex-entry
272 nil start-point end-point
))
273 (push (cons "&entry" entry
) alist
)
275 ;; check for crossref entries
276 (if (assoc "crossref" alist
)
279 alist
(reftex-get-crossref-alist alist
))))
282 (push (cons "&formatted" (reftex-format-bib-entry alist
))
285 ;; make key the first element
286 (push (reftex-get-bib-field "&key" alist
) alist
)
288 ;; add it to the list
289 (push alist found-list
)))))
290 (reftex-kill-temporary-buffers))))
291 (setq found-list
(nreverse found-list
))
295 ((eq 'author reftex-sort-bibtex-matches
)
296 (sort found-list
'reftex-bib-sort-author
))
297 ((eq 'year reftex-sort-bibtex-matches
)
298 (sort found-list
'reftex-bib-sort-year
))
299 ((eq 'reverse-year reftex-sort-bibtex-matches
)
300 (sort found-list
'reftex-bib-sort-year-reverse
))
303 (defun reftex-bib-sort-author (e1 e2
)
304 (let ((al1 (reftex-get-bib-names "author" e1
))
305 (al2 (reftex-get-bib-names "author" e2
)))
306 (while (and al1 al2
(string= (car al1
) (car al2
)))
309 (if (and (stringp (car al1
))
311 (string< (car al1
) (car al2
))
312 (not (stringp (car al1
))))))
314 (defun reftex-bib-sort-year (e1 e2
)
315 (< (string-to-number (or (cdr (assoc "year" e1
)) "0"))
316 (string-to-number (or (cdr (assoc "year" e2
)) "0"))))
318 (defun reftex-bib-sort-year-reverse (e1 e2
)
319 (> (string-to-number (or (cdr (assoc "year" e1
)) "0"))
320 (string-to-number (or (cdr (assoc "year" e2
)) "0"))))
322 (defun reftex-get-crossref-alist (entry)
323 ;; return the alist from a crossref entry
324 (let ((crkey (cdr (assoc "crossref" entry
)))
329 (if (re-search-forward
330 (concat "@\\w+[{(][ \t\n\r]*" (regexp-quote crkey
)
331 "[ \t\n\r]*,") nil t
)
333 (setq start
(match-beginning 0))
337 (reftex-parse-bibtex-entry nil start
(point)))
340 ;; Parse the bibliography environment
341 (defun reftex-extract-bib-entries-from-thebibliography (files)
342 ;; Extract bib-entries from the \begin{thebibliography} environment.
343 ;; Parsing is not as good as for the BibTeX database stuff.
344 ;; The environment should be located in file FILE.
346 (let* (start end buf entries re re-list file default
)
348 (error "Need file name to find thebibliography environment"))
349 (while (setq file
(pop files
))
350 (setq buf
(reftex-get-file-buffer-force
351 file
(not reftex-keep-temporary-buffers
)))
353 (error "No such file %s" file
))
354 (message "Scanning thebibliography environment in %s" file
)
360 (goto-char (point-min))
361 (while (re-search-forward
362 "\\(\\`\\|[\n\r]\\)[ \t]*\\\\begin{thebibliography}" nil t
)
363 (beginning-of-line 2)
365 (if (re-search-forward
366 "\\(\\`\\|[\n\r]\\)[ \t]*\\\\end{thebibliography}" nil t
)
368 (beginning-of-line 1)
370 (when (and start end
)
373 (mapcar 'reftex-parse-bibitem
376 (buffer-substring-no-properties start end
)
377 "[ \t\n\r]*\\\\bibitem\\(\\[[^]]*]\\)*"))))))
380 (error "No bibitems found"))
382 ;; Read a regexp, completing on known citation keys.
383 (setq default
(regexp-quote (reftex-get-bibkey-default)))
388 "Regex { && Regex...}: "
391 (if (fboundp 'LaTeX-bibitem-list
)
393 (cdr (assoc 'bibview-cache
394 (symbol-value reftex-docstruct-symbol
))))
396 nil nil nil
'reftex-cite-regexp-hist
)
399 (if (or (null re-list
) (equal re-list
'("")))
400 (setq re-list
(list default
)))
402 (if (string-match "\\`[ \t]*\\'" (car re-list
))
403 (error "Empty regular expression"))
405 (while (and (setq re
(pop re-list
)) entries
)
409 (if (string-match re
(cdr (assoc "&entry" x
)))
415 (push (cons "&formatted" (reftex-format-bibitem x
)) x
)
416 (push (reftex-get-bib-field "&key" x
) x
)
422 (defun reftex-get-bibkey-default ()
423 ;; Return the word before the cursor. If the cursor is in a
424 ;; citation macro, return the word before the macro.
425 (let* ((macro (reftex-what-macro 1)))
427 (if (and macro
(string-match "cite" (car macro
)))
428 (goto-char (cdr macro
)))
429 (skip-chars-backward "^a-zA-Z0-9")
430 (reftex-this-word))))
432 ;; Parse and format individual entries
434 (defun reftex-get-bib-names (field entry
)
435 ;; Return a list with the author or editor names in ENTRY
436 (let ((names (reftex-get-bib-field field entry
)))
438 (setq names
(reftex-get-bib-field "editor" entry
)))
439 (while (string-match "\\band\\b[ \t]*" names
)
440 (setq names
(replace-match "\n" nil t names
)))
441 (while (string-match "[\\.a-zA-Z\\-]+\\.[ \t]*\\|,.*\\|[{}]+" names
)
442 (setq names
(replace-match "" nil t names
)))
443 (while (string-match "^[ \t]+\\|[ \t]+$" names
)
444 (setq names
(replace-match "" nil t names
)))
445 (while (string-match "[ \t][ \t]+" names
)
446 (setq names
(replace-match " " nil t names
)))
447 (split-string names
"\n")))
449 (defun reftex-parse-bibtex-entry (entry &optional from to
)
450 (let (alist key start field
)
455 (set-buffer (get-buffer-create " *RefTeX-scratch*"))
457 (set-syntax-table reftex-syntax-table-for-bib
)
461 (narrow-to-region from to
))
462 (goto-char (point-min))
464 (if (re-search-forward
465 "@\\(\\w+\\)[ \t\n\r]*[{(][ \t\n\r]*\\([^ \t\n\r,]+\\)" nil t
)
468 (cons "&type" (downcase (reftex-match-string 1)))
469 (cons "&key" (reftex-match-string 2)))))
470 (while (re-search-forward "\\(\\w+\\)[ \t\n\r]*=[ \t\n\r]*" nil t
)
471 (setq key
(downcase (reftex-match-string 1)))
473 ((= (following-char) ?
{)
479 ((= (following-char) ?
\")
482 (while (and (search-forward "\"" nil t
)
483 (= ?
\\ (char-after (- (point) 2))))))
486 (re-search-forward "[ \t]*[\n\r,}]" nil
1)))
487 (setq field
(buffer-substring-no-properties start
(1- (point))))
488 ;; remove extra whitespace
489 (while (string-match "[\n\t\r]\\|[ \t][ \t]+" field
)
490 (setq field
(replace-match " " nil t field
)))
491 ;; remove leading garbage
492 (if (string-match "^[ \t{]+" field
)
493 (setq field
(replace-match "" nil t field
)))
494 ;; remove trailing garbage
495 (if (string-match "[ \t}]+$" field
)
496 (setq field
(replace-match "" nil t field
)))
497 (push (cons key field
) alist
))))
500 (defun reftex-get-bib-field (fieldname entry
&optional format
)
501 ;; Extract the field FIELDNAME from an ENTRY
502 (let ((cell (assoc fieldname entry
)))
505 (format format
(cdr cell
))
509 (defun reftex-format-bib-entry (entry)
510 ;; Format a BibTeX ENTRY so that it is nice to look at
512 ((auth-list (reftex-get-bib-names "author" entry
))
513 (authors (mapconcat 'identity auth-list
", "))
514 (year (reftex-get-bib-field "year" entry
))
515 (title (reftex-get-bib-field "title" entry
))
516 (type (reftex-get-bib-field "&type" entry
))
517 (key (reftex-get-bib-field "&key" entry
))
520 ((equal type
"article")
521 (concat (reftex-get-bib-field "journal" entry
) " "
522 (reftex-get-bib-field "volume" entry
) ", "
523 (reftex-get-bib-field "pages" entry
)))
525 (concat "book (" (reftex-get-bib-field "publisher" entry
) ")"))
526 ((equal type
"phdthesis")
527 (concat "PhD: " (reftex-get-bib-field "school" entry
)))
528 ((equal type
"mastersthesis")
529 (concat "Master: " (reftex-get-bib-field "school" entry
)))
530 ((equal type
"inbook")
531 (concat "Chap: " (reftex-get-bib-field "chapter" entry
)
532 ", pp. " (reftex-get-bib-field "pages" entry
)))
533 ((or (equal type
"conference")
534 (equal type
"incollection")
535 (equal type
"inproceedings"))
536 (reftex-get-bib-field "booktitle" entry
"in: %s"))
538 (setq authors
(reftex-truncate authors
30 t t
))
539 (when (reftex-use-fonts)
540 (put-text-property 0 (length key
) 'face
541 (reftex-verified-face reftex-label-face
542 'font-lock-constant-face
543 'font-lock-reference-face
)
545 (put-text-property 0 (length authors
) 'face reftex-bib-author-face
547 (put-text-property 0 (length year
) 'face reftex-bib-year-face
549 (put-text-property 0 (length title
) 'face reftex-bib-title-face
551 (put-text-property 0 (length extra
) 'face reftex-bib-extra-face
553 (concat key
"\n " authors
" " year
" " extra
"\n " title
"\n\n")))
555 (defun reftex-parse-bibitem (item)
556 ;; Parse a \bibitem entry
557 (let ((key "") (text ""))
558 (when (string-match "\\`{\\([^}]+\\)}\\([^\000]*\\)" item
)
559 (setq key
(match-string 1 item
)
560 text
(match-string 2 item
)))
561 ;; Clean up the text a little bit
562 (while (string-match "[\n\r\t]\\|[ \t][ \t]+" text
)
563 (setq text
(replace-match " " nil t text
)))
564 (if (string-match "\\`[ \t]+" text
)
565 (setq text
(replace-match "" nil t text
)))
569 (cons "&entry" (concat key
" " text
)))))
571 (defun reftex-format-bibitem (item)
572 ;; Format a \bibitem entry so that it is (relatively) nice to look at.
573 (let ((text (reftex-get-bib-field "&text" item
))
574 (key (reftex-get-bib-field "&key" item
))
577 ;; Wrap the text into several lines.
578 (while (and (> (length text
) 70)
579 (string-match " " (substring text
60)))
580 (push (substring text
0 (+ 60 (match-beginning 0))) lines
)
581 (setq text
(substring text
(+ 61 (match-beginning 0)))))
583 (setq text
(mapconcat 'identity
(nreverse lines
) "\n "))
585 (when (reftex-use-fonts)
586 (put-text-property 0 (length text
) 'face reftex-bib-author-face text
))
587 (concat key
"\n " text
"\n\n")))
592 (defun reftex-citation (&optional no-insert format-key
)
593 "Make a citation using BibTeX database files.
594 After prompting for a regular expression, scans the buffers with
595 bibtex entries (taken from the \\bibliography command) and offers the
596 matching entries for selection. The selected entry is formatted according
597 to `reftex-cite-format' and inserted into the buffer.
599 If NO-INSERT is non-nil, nothing is inserted, only the selected key returned.
601 FORMAT-KEY can be used to pre-select a citation format.
603 When called with a `C-u' prefix, prompt for optional arguments in
604 cite macros. When called with a numeric prefix, make that many
605 citations. When called with point inside the braces of a `\\cite'
606 command, it will add another key, ignoring the value of
607 `reftex-cite-format'.
609 The regular expression uses an expanded syntax: && is interpreted as `and'.
610 Thus, `aaaa&&bbb' matches entries which contain both `aaaa' and `bbb'.
611 While entering the regexp, completion on knows citation keys is possible.
612 `=' is a good regular expression to match all entries in all files."
616 ;; check for recursive edit
617 (reftex-check-recursive-edit)
619 ;; This function may also be called outside reftex-mode.
620 ;; Thus look for the scanning info only if in reftex-mode.
623 (reftex-access-scan-info nil
))
625 ;; Call reftex-do-citation, but protected
627 (reftex-do-citation current-prefix-arg no-insert format-key
)
628 (reftex-kill-temporary-buffers)))
630 (defun reftex-do-citation (&optional arg no-insert format-key
)
631 ;; This really does the work of reftex-citation.
633 (let* ((format (reftex-figure-out-cite-format arg no-insert format-key
))
635 (docstruct-symbol reftex-docstruct-symbol
)
636 (selected-entries (reftex-offer-bib-menu))
637 (insert-entries selected-entries
)
638 entry string cite-view
)
640 (when (stringp selected-entries
)
641 (error selected-entries
))
642 (unless selected-entries
(error "Quit"))
644 (if (stringp selected-entries
)
646 (setq selected-entries nil
647 insert-entries
(list (list selected-entries
648 (cons "&key" selected-entries
))))
649 ;; It makes sense to compute the cite-view strings.
652 (when (eq (car selected-entries
) 'concat
)
653 ;; All keys go into a single command - we need to trick a little
654 ;; FIXME: Unfortunately, this meens that commenting does not work right.
655 (pop selected-entries
)
656 (let ((concat-keys (mapconcat 'car selected-entries
",")))
658 (list (list concat-keys
(cons "&key" concat-keys
))))))
662 ;; We shall insert this into the buffer...
663 (message "Formatting...")
665 (while (setq entry
(pop insert-entries
))
666 ;; Format the citation and insert it
667 (setq string
(if reftex-format-cite-function
668 (funcall reftex-format-cite-function
669 (reftex-get-bib-field "&key" entry
)
671 (reftex-format-citation entry format
)))
672 (when (or (eq reftex-cite-prompt-optional-args t
)
673 (and reftex-cite-prompt-optional-args
675 (let ((start 0) (nth 0) value
)
676 (while (setq start
(string-match "\\[\\]" string start
))
677 (setq value
(read-string (format "Optional argument %d: "
678 (setq nth
(1+ nth
)))))
679 (setq string
(replace-match (concat "[" value
"]") t t string
))
680 (setq start
(1+ start
)))))
681 ;; Should we cleanup empty optional arguments?
682 ;; if the first is empty, it can be removed. If the second is empty,
683 ;; it has to go. If there is only a single arg and empty, it can go
685 (when reftex-cite-cleanup-optional-args
687 ((string-match "\\([a-zA-Z0-9]\\)\\[\\]{" string
)
688 (setq string
(replace-match "\\1{" nil nil string
)))
689 ((string-match "\\[\\]\\(\\[[a-zA-Z0-9., ]+\\]\\)" string
)
690 (setq string
(replace-match "\\1" nil nil string
)))
691 ((string-match "\\[\\]\\[\\]" string
)
692 (setq string
(replace-match "" t t string
)))))
695 ;; Reposition cursor?
696 (when (string-match "\\?" string
)
697 (search-backward "?")
701 (when (and reftex-mode
702 (fboundp 'LaTeX-add-bibitems
)
703 reftex-plug-into-AUCTeX
)
704 (apply 'LaTeX-add-bibitems
(mapcar 'car selected-entries
)))
706 ;; Produce the cite-view strings
707 (when (and reftex-mode reftex-cache-cite-echo cite-view
)
708 (mapcar (lambda (entry)
709 (reftex-make-cite-echo-string entry docstruct-symbol
))
714 (set-marker reftex-select-return-marker nil
)
715 (reftex-kill-buffer "*RefTeX Select*")
717 ;; Check if the prefix arg was numeric, and call recursively
721 (skip-chars-backward "}")
723 (reftex-do-citation arg
))
726 ;; Return the citation key
727 (car (car selected-entries
))))
729 (defun reftex-figure-out-cite-format (arg &optional no-insert format-key
)
730 ;; Check if there is already a cite command at point and change cite format
731 ;; in order to only add another reference in the same cite command.
732 (let ((macro (car (reftex-what-macro 1)))
733 (cite-format-value (reftex-get-cite-format))
737 ;; Format does not really matter because nothing will be inserted.
740 ((and (stringp macro
)
741 (string-match "\\`\\\\cite\\|cite\\'" macro
))
742 ;; We are already inside a cite macro
743 (if (or (not arg
) (not (listp arg
)))
746 (if (member (preceding-char) '(?\
{ ?
,)) "" ",")
748 (if (member (following-char) '(?\
} ?
,)) "" ",")))
751 ;; Figure out the correct format
753 (if (and (symbolp cite-format-value
)
754 (assq cite-format-value reftex-cite-format-builtin
))
755 (nth 2 (assq cite-format-value reftex-cite-format-builtin
))
760 (reftex-select-with-char
761 "" (concat "SELECT A CITATION FORMAT\n\n"
764 (format "[%c] %s %s" (car x
)
765 (if (> (car x
) 31) " " "")
768 (if (assq key format
)
769 (setq format
(cdr (assq key format
)))
770 (error "No citation format associated with key `%c'" key
)))))
773 (defun reftex-citep ()
774 "Call `reftex-citation' with a format selector `?p'."
776 (reftex-citation nil ?p
))
778 (defun reftex-citet ()
779 "Call `reftex-citation' with a format selector `?t'."
781 (reftex-citation nil ?t
))
783 (defvar reftex-select-bib-map
)
784 (defun reftex-offer-bib-menu ()
785 ;; Offer bib menu and return list of selected items
787 (let ((bibtype (reftex-bib-or-thebib))
788 found-list rtn key data selected-entries
)
796 ; ((assq 'bib (symbol-value reftex-docstruct-symbol))
797 ;; using BibTeX database files.
798 (reftex-extract-bib-entries (reftex-get-bibfile-list)))
799 ((eq bibtype
'thebib
)
800 ; ((assq 'thebib (symbol-value reftex-docstruct-symbol))
801 ;; using thebibliography environment.
802 (reftex-extract-bib-entries-from-thebibliography
806 'thebib
(symbol-value reftex-docstruct-symbol
))))))
807 (reftex-default-bibliography
808 (message "Using default bibliography")
809 (reftex-extract-bib-entries (reftex-default-bibliography)))
810 (t (error "No valid bibliography in this document, and no default available"))))
813 (error "Sorry, no matches found"))
815 ;; Remember where we came from
816 (setq reftex-call-back-to-this-buffer
(current-buffer))
817 (set-marker reftex-select-return-marker
(point))
820 (save-window-excursion
821 (delete-other-windows)
822 (let ((default-major-mode 'reftex-select-bib-mode
))
823 (reftex-kill-buffer "*RefTeX Select*")
824 (switch-to-buffer-other-window "*RefTeX Select*")
825 (unless (eq major-mode
'reftex-select-bib-mode
)
826 (reftex-select-bib-mode))
827 (let ((buffer-read-only nil
))
829 (reftex-insert-bib-matches found-list
)))
830 (setq buffer-read-only t
)
831 (if (= 0 (buffer-size))
832 (error "No matches found"))
833 (setq truncate-lines t
)
838 reftex-citation-prompt
840 reftex-select-bib-map
842 'reftex-bibtex-selection-callback nil
))
845 (unless key
(throw 'done t
))
851 ;; Restrict with new regular expression
852 (setq found-list
(reftex-restrict-bib-matches found-list
))
853 (let ((buffer-read-only nil
))
855 (reftex-insert-bib-matches found-list
))
859 (setq selected-entries
860 (if reftex-select-marked
861 (mapcar 'car
(nreverse reftex-select-marked
))
865 ;; Take all (marked), and push the symbol 'concat
866 (setq selected-entries
868 (if reftex-select-marked
869 (mapcar 'car
(nreverse reftex-select-marked
))
873 ;; Take all (marked), and push the symbol 'concat
874 (reftex-extract-bib-file found-list reftex-select-marked
)
875 (setq selected-entries
"BibTeX database file created")
878 ;; Take all (marked), and push the symbol 'concat
879 (reftex-extract-bib-file found-list reftex-select-marked
881 (setq selected-entries
"BibTeX database file created")
886 (setq selected-entries
887 (if reftex-select-marked
889 (mapcar 'car
(nreverse reftex-select-marked
)))
890 (if data
(list data
) nil
)))
893 ;; Got this one with completion
894 (setq selected-entries key
)
900 (defun reftex-restrict-bib-matches (found-list)
901 ;; Limit FOUND-LIST with more regular expressions
902 (let ((re-list (split-string (read-string
903 "RegExp [ && RegExp...]: "
904 nil
'reftex-cite-regexp-hist
)
906 (found-list-r found-list
)
908 (while (setq re
(pop re-list
))
914 re
(cdr (assoc "&entry" x
)))
923 (defun reftex-extract-bib-file (all &optional marked complement
)
924 ;; Limit FOUND-LIST with more regular expressions
925 (let ((file (read-file-name "File to create: ")))
926 (find-file-other-window file
)
927 (if (> (buffer-size) 0)
929 (format "Overwrite non-empty file %s? " file
))
936 (if (or (and (assoc x marked
) (not complement
))
937 (and (not (assoc x marked
)) complement
))
938 (cdr (assoc "&entry" x
))
940 (cdr (assoc "&entry" x
))))
942 (insert (mapconcat 'identity all
"\n\n"))
944 (goto-char (point-min))))
946 (defun reftex-insert-bib-matches (list)
947 ;; Insert the bib matches and number them correctly
949 (if (memq reftex-highlight-selection
'(mouse both
))
950 reftex-mouse-selected-face
955 (setq tmp
(cdr (assoc "&formatted" x
))
957 (put-text-property 0 len
:data x tmp
)
958 (put-text-property 0 (1- len
) 'mouse-face mouse-face tmp
)
961 (run-hooks 'reftex-display-copied-context-hook
))
963 (defun reftex-format-names (namelist n
)
964 (let (last (len (length namelist
)))
965 (if (= n
0) (setq n len
))
968 ((= 1 len
) (car namelist
))
969 ((> len n
) (concat (car namelist
) (nth 2 reftex-cite-punctuation
)))
972 last
(nth (1- n
) namelist
))
973 (setcdr (nthcdr (- n
2) namelist
) nil
)
975 (mapconcat 'identity namelist
(nth 0 reftex-cite-punctuation
))
976 (nth 1 reftex-cite-punctuation
)
979 (defun reftex-format-citation (entry format
)
980 ;; Format a citation from the info in the BibTeX ENTRY
982 (unless (stringp format
) (setq format
"\\cite{%l}"))
984 (if (and reftex-comment-citations
985 (string-match "%l" reftex-cite-comment-format
))
986 (error "reftex-cite-comment-format contains invalid %%l"))
989 "\\(\\`\\|[^%]\\)\\(\\(%\\([0-9]*\\)\\([a-zA-Z]\\)\\)[.,;: ]*\\)"
991 (let ((n (string-to-number (match-string 4 format
)))
992 (l (string-to-char (match-string 5 format
)))
998 (reftex-get-bib-field "&key" entry
)
999 (if reftex-comment-citations
1000 reftex-cite-comment-format
1002 ((= l ?a
) (reftex-format-names
1003 (reftex-get-bib-names "author" entry
)
1005 ((= l ?A
) (car (reftex-get-bib-names "author" entry
)))
1006 ((= l ?b
) (reftex-get-bib-field "booktitle" entry
"in: %s"))
1007 ((= l ?B
) (reftex-abbreviate-title
1008 (reftex-get-bib-field "booktitle" entry
"in: %s")))
1009 ((= l ?c
) (reftex-get-bib-field "chapter" entry
))
1010 ((= l ?d
) (reftex-get-bib-field "edition" entry
))
1011 ((= l ?e
) (reftex-format-names
1012 (reftex-get-bib-names "editor" entry
)
1014 ((= l ?E
) (car (reftex-get-bib-names "editor" entry
)))
1015 ((= l ?h
) (reftex-get-bib-field "howpublished" entry
))
1016 ((= l ?i
) (reftex-get-bib-field "institution" entry
))
1017 ((= l ?j
) (reftex-get-bib-field "journal" entry
))
1018 ((= l ?k
) (reftex-get-bib-field "key" entry
))
1019 ((= l ?m
) (reftex-get-bib-field "month" entry
))
1020 ((= l ?n
) (reftex-get-bib-field "number" entry
))
1021 ((= l ?o
) (reftex-get-bib-field "organization" entry
))
1022 ((= l ?p
) (reftex-get-bib-field "pages" entry
))
1023 ((= l ?P
) (car (split-string
1024 (reftex-get-bib-field "pages" entry
)
1026 ((= l ?s
) (reftex-get-bib-field "school" entry
))
1027 ((= l ?u
) (reftex-get-bib-field "publisher" entry
))
1028 ((= l ?r
) (reftex-get-bib-field "address" entry
))
1029 ((= l ?t
) (reftex-get-bib-field "title" entry
))
1030 ((= l ?T
) (reftex-abbreviate-title
1031 (reftex-get-bib-field "title" entry
)))
1032 ((= l ?v
) (reftex-get-bib-field "volume" entry
))
1033 ((= l ?y
) (reftex-get-bib-field "year" entry
)))))
1035 (if (string= rpl
"")
1036 (setq b
(match-beginning 2) e
(match-end 2))
1037 (setq b
(match-beginning 3) e
(match-end 3)))
1038 (setq format
(concat (substring format
0 b
) rpl
(substring format e
)))))
1039 (while (string-match "%%" format
)
1040 (setq format
(replace-match "%" t t format
)))
1041 (while (string-match "[ ,.;:]*%<" format
)
1042 (setq format
(replace-match "" t t format
)))
1045 (defun reftex-make-cite-echo-string (entry docstruct-symbol
)
1046 ;; Format a bibtex entry for the echo area and cache the result.
1047 (let* ((key (reftex-get-bib-field "&key" entry
))
1049 (let* ((reftex-cite-punctuation '(" " " & " " etal.")))
1050 (reftex-format-citation entry reftex-cite-view-format
)))
1051 (cache (assq 'bibview-cache
(symbol-value docstruct-symbol
)))
1052 (cache-entry (assoc key
(cdr cache
))))
1054 ;; This docstruct has no cache - make one.
1055 (set docstruct-symbol
(cons (cons 'bibview-cache nil
)
1056 (symbol-value docstruct-symbol
))))
1057 (when reftex-cache-cite-echo
1058 (setq key
(copy-sequence key
))
1059 (set-text-properties 0 (length key
) nil key
)
1060 (set-text-properties 0 (length string
) nil string
)
1062 (unless (string= (cdr cache-entry
) string
)
1063 (setcdr cache-entry string
)
1064 (put reftex-docstruct-symbol
'modified t
))
1065 (push (cons key string
) (cdr cache
))
1066 (put reftex-docstruct-symbol
'modified t
)))
1069 (defun reftex-bibtex-selection-callback (data ignore no-revisit
)
1070 ;; Callback function to be called from the BibTeX selection, in
1071 ;; order to display context. This function is relatively slow and not
1072 ;; recommended for follow mode. It works OK for individual lookups.
1073 (let ((win (selected-window))
1074 (key (reftex-get-bib-field "&key" data
))
1075 bibfile-list item bibtype
)
1079 (set-buffer reftex-call-back-to-this-buffer
)
1080 (setq bibtype
(reftex-bib-or-thebib))
1083 ; ((assq 'bib (symbol-value reftex-docstruct-symbol))
1084 (setq bibfile-list
(reftex-get-bibfile-list)))
1085 ((eq bibtype
'thebib
)
1086 ; ((assq 'thebib (symbol-value reftex-docstruct-symbol))
1091 'thebib
(symbol-value reftex-docstruct-symbol
))))
1093 (reftex-default-bibliography
1094 (setq bibfile-list
(reftex-default-bibliography)))
1095 (t (ding) (throw 'exit nil
))))
1098 (setq bibfile-list
(reftex-visited-files bibfile-list
)))
1101 (reftex-pop-to-bibtex-entry
1102 key bibfile-list
(not reftex-keep-temporary-buffers
) t item
)
1105 (select-window win
)))
1107 ;;; Global BibTeX file
1108 (defun reftex-all-used-citation-keys ()
1109 (reftex-access-scan-info)
1110 (let ((files (reftex-all-document-files)) file keys kkk kk k
)
1112 (while (setq file
(pop files
))
1113 (set-buffer (reftex-get-file-buffer-force file
'mark
))
1117 (goto-char (point-min))
1118 (while (re-search-forward "^[^%\n\r]*\\\\\\(bibentry\\|[a-zA-Z]*cite[a-zA-Z]*\\)\\(\\[[^\\]]*\\]\\)?{\\([^}]+\\)}" nil t
)
1119 (setq kk
(match-string-no-properties 3))
1120 (while (string-match "%.*\n?" kk
)
1121 (setq kk
(replace-match "" t t kk
)))
1122 (setq kk
(split-string kk
"[, \t\r\n]+"))
1123 (while (setq k
(pop kk
))
1125 (setq keys
(cons k keys
)))))))))
1126 (reftex-kill-temporary-buffers)
1129 (defun reftex-create-bibtex-file (bibfile)
1130 "Create a new BibTeX database file with all entries referenced in document.
1131 The command prompts for a filename and writes the collected entries to
1132 that file. Only entries referenced in the current document with
1133 any \\cite-like macros are used.
1134 The sequence in the new file is the same as it was in the old database."
1135 (interactive "FNew BibTeX file: ")
1136 (let ((keys (reftex-all-used-citation-keys))
1137 (files (reftex-get-bibfile-list))
1138 file key entries beg end entry
)
1140 (while (setq file
(pop files
))
1141 (set-buffer (reftex-get-file-buffer-force file
'mark
))
1142 (reftex-with-special-syntax-for-bib
1146 (goto-char (point-min))
1147 (while (re-search-forward
1148 "^[ \t]*@[a-zA-Z]+[ \t]*{\\([^ \t\r\n]+\\),"
1150 (setq key
(match-string 1)
1151 beg
(match-beginning 0)
1153 (goto-char (match-beginning 1))
1156 (error (goto-char (match-end 0))))
1158 (when (member key keys
)
1159 (setq entry
(buffer-substring beg end
)
1160 entries
(cons entry entries
)
1161 keys
(delete key keys
)))))))))
1162 (find-file-other-window bibfile
)
1163 (if (> (buffer-size) 0)
1164 (unless (yes-or-no-p
1165 (format "Overwrite non-empty file %s? " bibfile
))
1168 (insert (mapconcat 'identity
(reverse entries
) "\n\n"))
1169 (goto-char (point-min))
1171 (message "%d entries extracted and copied to new database"
1175 ;;; arch-tag: d53d0a5a-ab32-4b52-a846-2a7c3527cd89
1176 ;;; reftex-cite.el ends here