1 ;;; reftex-cite.el --- creating citations with RefTeX
3 ;; Copyright (C) 1997-2015 Free Software Foundation, Inc.
5 ;; Author: Carsten Dominik <dominik@science.uva.nl>
6 ;; Maintainer: auctex-devel@gnu.org
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 3 of the License, or
13 ;; (at your option) any later version.
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. If not, see <http://www.gnu.org/licenses/>.
27 (eval-when-compile (require 'cl
))
31 ;;; Variables and constants
32 (defvar reftex-cite-regexp-hist nil
33 "The history list of regular expressions used for citations")
35 (defconst reftex-citation-prompt
36 "Select: [n]ext [p]revious [r]estrict [ ]full_entry [q]uit RET [?]Help+more"
37 "Prompt and help string for citation selection")
39 (defconst reftex-citation-help
40 " n / p Go to next/previous entry (Cursor motion works as well).
41 g / r Start over with new regexp / Refine with additional regexp.
42 SPC Show full database entry in other window.
43 f Toggle follow mode: Other window will follow with full db entry.
44 . Show insertion point.
45 q Quit without inserting \\cite macro into buffer.
46 TAB Enter citation key with completion.
47 RET Accept current entry (also on mouse-2) and create \\cite macro.
48 m / u Mark/Unmark the entry.
49 e / E Create BibTeX file with all (marked/unmarked) entries
50 a / A Put all (marked) entries into one/many \\cite commands.")
53 (defmacro reftex-with-special-syntax-for-bib
(&rest body
)
54 `(let ((saved-syntax (syntax-table)))
57 (set-syntax-table reftex-syntax-table-for-bib
)
59 (set-syntax-table saved-syntax
))))
62 (defun reftex-default-bibliography ()
63 "Return the expanded value of variable `reftex-default-bibliography'.
64 The expanded value is cached."
65 (unless (eq (get 'reftex-default-bibliography
:reftex-raw
)
66 reftex-default-bibliography
)
67 (put 'reftex-default-bibliography
:reftex-expanded
68 (reftex-locate-bibliography-files
69 default-directory reftex-default-bibliography
))
70 (put 'reftex-default-bibliography
:reftex-raw
71 reftex-default-bibliography
))
72 (get 'reftex-default-bibliography
:reftex-expanded
))
75 (defun reftex-bib-or-thebib ()
76 "Test if BibTeX or \begin{thebibliography} should be used for the citation.
77 Find the bof of the current file"
78 (let* ((docstruct (symbol-value reftex-docstruct-symbol
))
79 (rest (or (member (list 'bof
(buffer-file-name)) docstruct
)
81 (bib (assq 'bib rest
))
82 (thebib (assq 'thebib rest
))
83 (bibmem (memq bib rest
))
84 (thebibmem (memq thebib rest
)))
85 (when (not (or thebib bib
))
86 (setq bib
(assq 'bib docstruct
)
87 thebib
(assq 'thebib docstruct
)
88 bibmem
(memq bib docstruct
)
89 thebibmem
(memq thebib docstruct
)))
90 (if (> (length bibmem
) (length thebibmem
))
92 (if thebib
'thebib nil
))))
95 (defun reftex-get-bibfile-list ()
96 "Return list of bibfiles for current document.
97 When using the chapterbib or bibunits package you should either
98 use the same database files everywhere, or separate parts using
99 different databases into different files (included into the mater file).
100 Then this function will return the applicable database files."
102 ;; Ensure access to scanning info
103 (reftex-access-scan-info)
105 ;; Try inside this file (and its includes)
106 (cdr (reftex-last-assoc-before-elt
107 'bib
(list 'eof
(buffer-file-name))
108 (member (list 'bof
(buffer-file-name))
109 (symbol-value reftex-docstruct-symbol
))))
110 ;; Try after the beginning of this file
111 (cdr (assq 'bib
(member (list 'bof
(buffer-file-name))
112 (symbol-value reftex-docstruct-symbol
))))
113 ;; Anywhere in the entire document
114 (cdr (assq 'bib
(symbol-value reftex-docstruct-symbol
)))
115 (error "\\bibliography statement missing or .bib files not found")))
117 ;;; Find a certain reference in any of the BibTeX files.
119 (defun reftex-pop-to-bibtex-entry (key file-list
&optional mark-to-kill
120 highlight item return
)
121 "Find BibTeX KEY in any file in FILE-LIST in another window.
122 If MARK-TO-KILL is non-nil, mark new buffer to kill.
123 If HIGHLIGHT is non-nil, highlight the match.
124 If ITEM in non-nil, search for bibitem instead of database entry.
125 If RETURN is non-nil, just return the entry and restore point."
128 (concat "\\\\bibitem[ \t]*\\(\\[[^]]*\\]\\)?[ \t]*{"
129 (regexp-quote key
) "}")
130 (concat "@\\(?:\\w\\|\\s_\\)+[ \t\n\r]*[{(][ \t\n\r]*"
131 (regexp-quote key
) "[, \t\r\n}]")))
132 (buffer-conf (current-buffer))
137 (setq file
(car file-list
)
138 file-list
(cdr file-list
))
139 (unless (setq buf
(reftex-get-file-buffer-force file mark-to-kill
))
140 (error "No such file %s" file
))
142 (setq oldpos
(point))
144 (goto-char (point-min))
145 (if (not (re-search-forward re nil t
))
146 (goto-char oldpos
) ;; restore previous position of point
147 (goto-char (match-beginning 0))
150 ;; Just return the relevant entry
151 (if item
(goto-char (match-end 0)))
152 (setq return
(buffer-substring
153 (point) (reftex-end-of-bib-entry item
)))
154 (goto-char oldpos
) ;; restore point.
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
)))))
169 (defun reftex-end-of-bib-entry (item)
175 "\\\\bibitem\\|\\end{thebibliography}")
176 (1- (match-beginning 0)))
177 (progn (forward-list 1) (point)))
178 (error (min (point-max) (+ 300 (point)))))))
180 ;;; 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
)
230 (unless (verify-visited-file-modtime buffer1
)
232 (format "File %s changed on disk. Reread from disk? "
233 (file-name-nondirectory
234 (buffer-file-name buffer1
))))
235 (with-current-buffer buffer1
(revert-buffer t t
)))))
238 (reftex-with-special-syntax-for-bib
240 (goto-char (point-min))
241 (while (re-search-forward first-re nil t
)
243 (setq key-point
(point))
244 (unless (re-search-backward "\\(\\`\\|[\n\r]\\)[ \t]*\
245 @\\(\\(?:\\w\\|\\s_\\)+\\)[ \t\n\r]*[{(]" nil t
)
246 (throw 'search-again nil
))
247 (setq start-point
(point))
248 (goto-char (match-end 0))
251 (error (goto-char key-point
)
252 (throw 'search-again nil
)))
253 (setq end-point
(point))
255 ;; Ignore @string, @comment and @c entries or things
257 (when (or (string= (downcase (match-string 2)) "string")
258 (string= (downcase (match-string 2)) "comment")
259 (string= (downcase (match-string 2)) "c")
260 (< (point) key-point
)) ; this means match not in {}
261 (goto-char key-point
)
262 (throw 'search-again nil
))
264 ;; Well, we have got a match
265 ;;(setq entry (concat
266 ;; (buffer-substring start-point (point)) "\n"))
267 (setq entry
(buffer-substring start-point
(point)))
269 ;; Check if other regexp match as well
270 (setq re-list rest-re
)
272 (unless (string-match (car re-list
) entry
)
274 (throw 'search-again nil
))
277 (setq alist
(reftex-parse-bibtex-entry
278 nil start-point end-point
))
279 (push (cons "&entry" entry
) alist
)
281 ;; check for crossref entries
282 (if (assoc "crossref" alist
)
285 alist
(reftex-get-crossref-alist alist
))))
288 (push (cons "&formatted" (reftex-format-bib-entry alist
))
291 ;; make key the first element
292 (push (reftex-get-bib-field "&key" alist
) alist
)
294 ;; add it to the list
295 (push alist found-list
)))))
296 (reftex-kill-temporary-buffers))))
297 (setq found-list
(nreverse found-list
))
301 ((eq 'author reftex-sort-bibtex-matches
)
302 (sort found-list
'reftex-bib-sort-author
))
303 ((eq 'year reftex-sort-bibtex-matches
)
304 (sort found-list
'reftex-bib-sort-year
))
305 ((eq 'reverse-year reftex-sort-bibtex-matches
)
306 (sort found-list
'reftex-bib-sort-year-reverse
))
309 (defun reftex-bib-sort-author (e1 e2
)
310 "Compare bib entries E1 and E2 by author.
311 The name of the first different author/editor is used."
312 (let ((al1 (reftex-get-bib-names "author" e1
))
313 (al2 (reftex-get-bib-names "author" e2
)))
314 (while (and al1 al2
(string= (car al1
) (car al2
)))
317 (if (and (stringp (car al1
))
319 (string< (car al1
) (car al2
))
320 (not (stringp (car al1
))))))
322 (defun reftex-bib-sort-year (e1 e2
)
323 "Compare bib entries E1 and E2 by year in ascending order."
324 (< (string-to-number (or (cdr (assoc "year" e1
)) "0"))
325 (string-to-number (or (cdr (assoc "year" e2
)) "0"))))
327 (defun reftex-bib-sort-year-reverse (e1 e2
)
328 "Compare bib entries E1 and E2 by year in descending order."
329 (> (string-to-number (or (cdr (assoc "year" e1
)) "0"))
330 (string-to-number (or (cdr (assoc "year" e2
)) "0"))))
332 (defun reftex-get-crossref-alist (entry)
333 "Return the alist from a crossref ENTRY."
334 (let ((crkey (cdr (assoc "crossref" entry
)))
339 (if (re-search-forward
340 (concat "@\\w+[{(][ \t\n\r]*" (regexp-quote crkey
)
341 "[ \t\n\r]*,") nil t
)
343 (setq start
(match-beginning 0))
347 (reftex-parse-bibtex-entry nil start
(point)))
350 ;; Parse the bibliography environment
351 (defun reftex-extract-bib-entries-from-thebibliography (files)
352 "Extract bib-entries from the \begin{thebibliography} environment.
353 Parsing is not as good as for the BibTeX database stuff.
354 The environment should be located in FILES."
355 (let* (start end buf entries re re-list file default
)
357 (error "Need file name to find thebibliography environment"))
358 (while (setq file
(pop files
))
359 (setq buf
(reftex-get-file-buffer-force
360 file
(not reftex-keep-temporary-buffers
)))
362 (error "No such file %s" file
))
363 (message "Scanning thebibliography environment in %s" file
)
365 (with-current-buffer buf
369 (goto-char (point-min))
370 (while (re-search-forward
371 "\\(\\`\\|[\n\r]\\)[ \t]*\\\\begin{thebibliography}" nil t
)
372 (beginning-of-line 2)
374 (if (re-search-forward
375 "\\(\\`\\|[\n\r]\\)[ \t]*\\\\end{thebibliography}" nil t
)
377 (beginning-of-line 1)
379 (when (and start end
)
382 (mapcar 'reftex-parse-bibitem
385 (buffer-substring-no-properties
387 "[ \t\n\r]*\\\\bibitem[ \t]*\
388 \\(\\[[^]]*]\\)*\[ \t]*"))))))
391 (error "No bibitems found"))
393 ;; Read a regexp, completing on known citation keys.
394 (setq default
(regexp-quote (reftex-get-bibkey-default)))
399 "Regex { && Regex...}: "
402 (if (fboundp 'LaTeX-bibitem-list
)
404 (cdr (assoc 'bibview-cache
405 (symbol-value reftex-docstruct-symbol
))))
407 nil nil nil
'reftex-cite-regexp-hist
)
410 (if (or (null re-list
) (equal re-list
'("")))
411 (setq re-list
(list default
)))
413 (if (string-match "\\`[ \t]*\\'" (car re-list
))
414 (error "Empty regular expression"))
416 (while (and (setq re
(pop re-list
)) entries
)
420 (if (string-match re
(cdr (assoc "&entry" x
)))
426 (push (cons "&formatted" (reftex-format-bibitem x
)) x
)
427 (push (reftex-get-bib-field "&key" x
) x
)
433 (defun reftex-get-bibkey-default ()
434 "Return the word before the cursor.
435 If the cursor is in a citation macro, return the word before the macro."
436 (let* ((macro (reftex-what-macro 1)))
438 (if (and macro
(string-match "cite" (car macro
)))
439 (goto-char (cdr macro
)))
440 (skip-chars-backward "^a-zA-Z0-9")
441 (reftex-this-word))))
443 ;;; Parse and format individual entries
444 (defun reftex-get-bib-names (field entry
)
445 "Return a list with the author or editor names in ENTRY.
446 If FIELD is empty try \"editor\" field."
447 (let ((names (reftex-get-bib-field field entry
)))
449 (setq names
(reftex-get-bib-field "editor" entry
)))
450 (while (string-match "\\band\\b[ \t]*" names
)
451 (setq names
(replace-match "\n" nil t names
)))
452 (while (string-match "[\\.a-zA-Z\\-]+\\.[ \t]*\\|,.*\\|[{}]+" names
)
453 (setq names
(replace-match "" nil t names
)))
454 (while (string-match "^[ \t]+\\|[ \t]+$" names
)
455 (setq names
(replace-match "" nil t names
)))
456 (while (string-match "[ \t][ \t]+" names
)
457 (setq names
(replace-match " " nil t names
)))
458 (split-string names
"\n")))
461 (defun reftex-parse-bibtex-entry (entry &optional from to raw
)
463 If ENTRY is nil then parse the entry in current buffer between FROM and TO.
464 If RAW is non-nil, keep double quotes/curly braces delimiting fields."
465 (let (alist key start field
)
470 (set-buffer (get-buffer-create " *RefTeX-scratch*"))
472 (set-syntax-table reftex-syntax-table-for-bib
)
476 (if (and from to
) (narrow-to-region from to
)))
477 (goto-char (point-min))
479 (if (re-search-forward "@\\(\\(?:\\w\\|\\s_\\)+\\)[ \t\n\r]*\
480 \[{(][ \t\n\r]*\\([^ \t\n\r,]+\\)" nil t
)
483 (cons "&type" (downcase (reftex-match-string 1)))
484 (cons "&key" (reftex-match-string 2)))))
485 (while (re-search-forward "\\(\\(?:\\w\\|-\\)+\\)[ \t\n\r]*=[ \t\n\r]*"
487 (setq key
(downcase (reftex-match-string 1)))
489 ((= (following-char) ?
{)
500 ((= (following-char) ?
\")
507 (setq start
(point))))
508 (while (and (search-forward "\"" nil t
)
509 (= ?
\\ (char-after (- (point) 2))))))
512 (re-search-forward "[ \t]*[\n\r,}]" nil
1)))
513 ;; extract field value, ignore trailing comma if in RAW mode
514 (let ((stop (if (and raw
(not (= (char-after (1- (point))) ?
,)))
517 (setq field
(buffer-substring-no-properties start stop
)))
518 ;; remove extra whitespace
519 (while (string-match "[\n\t\r]\\|[ \t][ \t]+" field
)
520 (setq field
(replace-match " " nil t field
)))
521 (push (cons key field
) alist
))))
524 (defun reftex-get-bib-field (fieldname entry
&optional format
)
525 "Extract the field FIELDNAME from ENTRY.
526 If FORMAT is non-nil `format' entry accordingly."
527 (let ((cell (assoc fieldname entry
)))
530 (format format
(cdr cell
))
534 (defun reftex-format-bib-entry (entry)
535 "Format a BibTeX ENTRY so that it is nice to look at."
537 ((auth-list (reftex-get-bib-names "author" entry
))
538 (authors (mapconcat 'identity auth-list
", "))
539 (year (reftex-get-bib-field "year" entry
))
540 (title (reftex-get-bib-field "title" entry
))
541 (type (reftex-get-bib-field "&type" entry
))
542 (key (reftex-get-bib-field "&key" entry
))
545 ((equal type
"article")
546 (concat (reftex-get-bib-field "journal" entry
) " "
547 (reftex-get-bib-field "volume" entry
) ", "
548 (reftex-get-bib-field "pages" entry
)))
550 (concat "book (" (reftex-get-bib-field "publisher" entry
) ")"))
551 ((equal type
"phdthesis")
552 (concat "PhD: " (reftex-get-bib-field "school" entry
)))
553 ((equal type
"mastersthesis")
554 (concat "Master: " (reftex-get-bib-field "school" entry
)))
555 ((equal type
"inbook")
556 (concat "Chap: " (reftex-get-bib-field "chapter" entry
)
557 ", pp. " (reftex-get-bib-field "pages" entry
)))
558 ((or (equal type
"conference")
559 (equal type
"incollection")
560 (equal type
"inproceedings"))
561 (reftex-get-bib-field "booktitle" entry
"in: %s"))
563 (setq authors
(reftex-truncate authors
30 t t
))
564 (when (reftex-use-fonts)
565 (put-text-property 0 (length key
) 'face reftex-label-face
567 (put-text-property 0 (length authors
) 'face reftex-bib-author-face
569 (put-text-property 0 (length year
) 'face reftex-bib-year-face
571 (put-text-property 0 (length title
) 'face reftex-bib-title-face
573 (put-text-property 0 (length extra
) 'face reftex-bib-extra-face
575 (concat key
"\n " authors
" " year
" " extra
"\n " title
"\n\n")))
577 (defun reftex-parse-bibitem (item)
578 "Parse a \bibitem entry in ITEM."
579 (let ((key "") (text ""))
580 (when (string-match "\\`{\\([^}]+\\)}\\([^\000]*\\)" item
)
581 (setq key
(match-string 1 item
)
582 text
(match-string 2 item
)))
583 ;; Clean up the text a little bit
584 (while (string-match "[\n\r\t]\\|[ \t][ \t]+" text
)
585 (setq text
(replace-match " " nil t text
)))
586 (if (string-match "\\`[ \t]+" text
)
587 (setq text
(replace-match "" nil t text
)))
591 (cons "&entry" (concat key
" " text
)))))
593 (defun reftex-format-bibitem (item)
594 "Format a \bibitem entry in ITEM so that it is (relatively) nice to look at."
595 (let ((text (reftex-get-bib-field "&text" item
))
596 (key (reftex-get-bib-field "&key" item
))
599 ;; Wrap the text into several lines.
600 (while (and (> (length text
) 70)
601 (string-match " " (substring text
60)))
602 (push (substring text
0 (+ 60 (match-beginning 0))) lines
)
603 (setq text
(substring text
(+ 61 (match-beginning 0)))))
605 (setq text
(mapconcat 'identity
(nreverse lines
) "\n "))
607 (when (reftex-use-fonts)
608 (put-text-property 0 (length text
) 'face reftex-bib-author-face text
))
609 (concat key
"\n " text
"\n\n")))
613 ;; NB this is a global autoload - see reftex.el.
615 (defun reftex-citation (&optional no-insert format-key
)
616 "Make a citation using BibTeX database files.
617 After prompting for a regular expression, scans the buffers with
618 bibtex entries (taken from the \\bibliography command) and offers the
619 matching entries for selection. The selected entry is formatted according
620 to `reftex-cite-format' and inserted into the buffer.
622 If NO-INSERT is non-nil, nothing is inserted, only the selected key returned.
624 FORMAT-KEY can be used to pre-select a citation format.
626 When called with a `C-u' prefix, prompt for optional arguments in
627 cite macros. When called with a numeric prefix, make that many
628 citations. When called with point inside the braces of a `\\cite'
629 command, it will add another key, ignoring the value of
630 `reftex-cite-format'.
632 The regular expression uses an expanded syntax: && is interpreted as `and'.
633 Thus, `aaaa&&bbb' matches entries which contain both `aaaa' and `bbb'.
634 While entering the regexp, completion on knows citation keys is possible.
635 `=' is a good regular expression to match all entries in all files."
638 ;; check for recursive edit
639 (reftex-check-recursive-edit)
641 ;; This function may also be called outside reftex-mode.
642 ;; Thus look for the scanning info only if in reftex-mode.
645 (reftex-access-scan-info nil
))
647 ;; Call reftex-do-citation, but protected
649 (reftex-do-citation current-prefix-arg no-insert format-key
)
650 (reftex-kill-temporary-buffers)))
652 (defun reftex-do-citation (&optional arg no-insert format-key
)
653 "This really does the work of `reftex-citation'."
654 (let* ((format (reftex-figure-out-cite-format arg no-insert format-key
))
655 (docstruct-symbol reftex-docstruct-symbol
)
656 (selected-entries (reftex-offer-bib-menu))
657 (insert-entries selected-entries
)
658 entry string cite-view
)
660 (unless selected-entries
(error "Quit"))
662 (if (stringp selected-entries
)
664 (setq insert-entries
(list (list selected-entries
665 (cons "&key" selected-entries
)))
666 selected-entries nil
)
667 ;; It makes sense to compute the cite-view strings.
670 (when (eq (car selected-entries
) 'concat
)
671 ;; All keys go into a single command - we need to trick a little
672 ;; FIXME: Unfortunately, this means that commenting does not work right.
673 (pop selected-entries
)
674 (let ((concat-keys (mapconcat 'car selected-entries
675 reftex-cite-key-separator
)))
677 (list (list concat-keys
(cons "&key" concat-keys
))))))
681 ;; We shall insert this into the buffer...
682 (message "Formatting...")
684 (while (setq entry
(pop insert-entries
))
685 ;; Format the citation and insert it
686 (setq string
(if reftex-format-cite-function
687 (funcall reftex-format-cite-function
688 (reftex-get-bib-field "&key" entry
)
690 (reftex-format-citation entry format
)))
691 (when (or (eq reftex-cite-prompt-optional-args t
)
692 (and reftex-cite-prompt-optional-args
694 (let ((start 0) (nth 0) value
)
695 (while (setq start
(string-match "\\[\\]" string start
))
696 (setq value
(save-match-data
697 (read-string (format "Optional argument %d: "
698 (setq nth
(1+ nth
))))))
699 (setq string
(replace-match (concat "[" value
"]") t t string
))
700 (setq start
(1+ start
)))))
701 ;; Should we cleanup empty optional arguments?
702 ;; if the first is empty, it can be removed. If the second is empty,
703 ;; it has to go. If there is only a single arg and empty, it can go
705 (when reftex-cite-cleanup-optional-args
707 ((string-match "\\([a-zA-Z0-9]\\)\\[\\]{" string
)
708 (setq string
(replace-match "\\1{" nil nil string
)))
709 ((string-match "\\[\\]\\(\\[[a-zA-Z0-9., ]+\\]\\)" string
)
710 (setq string
(replace-match "\\1" nil nil string
)))
711 ((string-match "\\[\\]\\[\\]" string
)
712 (setq string
(replace-match "" t t string
)))))
715 ;; Reposition cursor?
716 (when (string-match "\\?" string
)
717 (search-backward "?")
721 (when (and reftex-mode
722 (fboundp 'LaTeX-add-bibitems
)
723 reftex-plug-into-AUCTeX
)
724 (apply 'LaTeX-add-bibitems
(mapcar 'car selected-entries
)))
726 ;; Produce the cite-view strings
727 (when (and reftex-mode reftex-cache-cite-echo cite-view
)
728 (mapc (lambda (entry)
729 (reftex-make-cite-echo-string entry docstruct-symbol
))
734 (set-marker reftex-select-return-marker nil
)
735 (reftex-kill-buffer "*RefTeX Select*")
737 ;; Check if the prefix arg was numeric, and call recursively
741 (skip-chars-backward "}")
743 (reftex-do-citation arg
))
746 ;; Return the citation key
747 (mapcar 'car selected-entries
)))
749 (defun reftex-figure-out-cite-format (arg &optional no-insert format-key
)
750 "Check if there is already a cite command at point and change cite format
751 in order to only add another reference in the same cite command."
752 (let ((macro (car (reftex-what-macro 1)))
753 (cite-format-value (reftex-get-cite-format))
757 ;; Format does not really matter because nothing will be inserted.
760 ((and (stringp macro
)
761 (string-match "\\`\\\\cite\\|cite\\'" macro
))
762 ;; We are already inside a cite macro
763 (if (or (not arg
) (not (listp arg
)))
766 (if (member (preceding-char) '(?\
{ ?
,))
768 reftex-cite-key-separator
)
770 (if (member (following-char) '(?\
} ?
,))
772 reftex-cite-key-separator
)))
775 ;; Figure out the correct format
777 (if (and (symbolp cite-format-value
)
778 (assq cite-format-value reftex-cite-format-builtin
))
779 (nth 2 (assq cite-format-value reftex-cite-format-builtin
))
784 (reftex-select-with-char
785 "" (concat "SELECT A CITATION FORMAT\n\n"
788 (format "[%c] %s %s" (car x
)
789 (if (> (car x
) 31) " " "")
792 (if (assq key format
)
793 (setq format
(cdr (assq key format
)))
794 (error "No citation format associated with key `%c'" key
)))))
798 (defun reftex-citep ()
799 "Call `reftex-citation' with a format selector `?p'."
801 (reftex-citation nil ?p
))
804 (defun reftex-citet ()
805 "Call `reftex-citation' with a format selector `?t'."
807 (reftex-citation nil ?t
))
809 (defvar reftex-select-bib-map
)
810 (defun reftex-offer-bib-menu ()
811 "Offer bib menu and return list of selected items."
812 (let ((bibtype (reftex-bib-or-thebib))
813 found-list rtn key data selected-entries
)
821 ; ((assq 'bib (symbol-value reftex-docstruct-symbol))
822 ;; using BibTeX database files.
823 (reftex-extract-bib-entries (reftex-get-bibfile-list)))
824 ((eq bibtype
'thebib
)
825 ; ((assq 'thebib (symbol-value reftex-docstruct-symbol))
826 ;; using thebibliography environment.
827 (reftex-extract-bib-entries-from-thebibliography
831 'thebib
(symbol-value reftex-docstruct-symbol
))))))
832 (reftex-default-bibliography
833 (message "Using default bibliography")
834 (reftex-extract-bib-entries (reftex-default-bibliography)))
835 (t (error "No valid bibliography in this document, and no default available"))))
838 (error "Sorry, no matches found"))
840 ;; Remember where we came from
841 (setq reftex-call-back-to-this-buffer
(current-buffer))
842 (set-marker reftex-select-return-marker
(point))
845 (save-window-excursion
846 (delete-other-windows)
847 (reftex-kill-buffer "*RefTeX Select*")
848 (switch-to-buffer-other-window "*RefTeX Select*")
849 (unless (eq major-mode
'reftex-select-bib-mode
)
850 (reftex-select-bib-mode))
851 (let ((buffer-read-only nil
))
853 (reftex-insert-bib-matches found-list
))
854 (setq buffer-read-only t
)
855 (if (= 0 (buffer-size))
856 (error "No matches found"))
857 (setq truncate-lines t
)
862 reftex-citation-prompt
864 reftex-select-bib-map
866 'reftex-bibtex-selection-callback nil
))
869 (unless key
(throw 'done t
))
875 ;; Restrict with new regular expression
876 (setq found-list
(reftex-restrict-bib-matches found-list
))
877 (let ((buffer-read-only nil
))
879 (reftex-insert-bib-matches found-list
))
883 (setq selected-entries
884 (if reftex-select-marked
885 (mapcar 'car
(nreverse reftex-select-marked
))
889 ;; Take all (marked), and push the symbol 'concat
890 (setq selected-entries
892 (if reftex-select-marked
893 (mapcar 'car
(nreverse reftex-select-marked
))
897 ;; Take all (marked), and push the symbol 'concat
898 (reftex-extract-bib-file found-list reftex-select-marked
)
899 (setq selected-entries
"BibTeX database file created")
902 ;; Take all (marked), and push the symbol 'concat
903 (reftex-extract-bib-file found-list reftex-select-marked
905 (setq selected-entries
"BibTeX database file created")
910 (setq selected-entries
911 (if reftex-select-marked
913 (mapcar 'car
(nreverse reftex-select-marked
)))
914 (if data
(list data
) nil
)))
917 ;; Got this one with completion
918 (setq selected-entries key
)
924 (defun reftex-restrict-bib-matches (found-list)
925 "Limit FOUND-LIST with more regular expressions."
926 (let ((re-list (split-string (read-string
927 "RegExp [ && RegExp...]: "
928 nil
'reftex-cite-regexp-hist
)
930 (found-list-r found-list
)
932 (while (setq re
(pop re-list
))
938 re
(cdr (assoc "&entry" x
)))
947 (defun reftex-extract-bib-file (all &optional marked complement
)
948 "Limit FOUND-LIST with more regular expressions."
949 (let ((file (read-file-name "File to create: ")))
950 (find-file-other-window file
)
951 (if (> (buffer-size) 0)
953 (format "Overwrite non-empty file %s? " file
))
960 (if (or (and (assoc x marked
) (not complement
))
961 (and (not (assoc x marked
)) complement
))
962 (cdr (assoc "&entry" x
))
964 (cdr (assoc "&entry" x
))))
966 (insert (mapconcat 'identity all
"\n\n"))
968 (goto-char (point-min))))
970 (defun reftex-insert-bib-matches (list)
971 "Insert the bib matches and number them correctly."
973 (if (memq reftex-highlight-selection
'(mouse both
))
974 reftex-mouse-selected-face
979 (setq tmp
(cdr (assoc "&formatted" x
))
981 (put-text-property 0 len
:data x tmp
)
982 (put-text-property 0 (1- len
) 'mouse-face mouse-face tmp
)
985 (run-hooks 'reftex-display-copied-context-hook
))
987 (defun reftex-format-names (namelist n
)
988 (let (last (len (length namelist
)))
989 (if (= n
0) (setq n len
))
992 ((= 1 len
) (car namelist
))
993 ((> len n
) (concat (car namelist
) (nth 2 reftex-cite-punctuation
)))
996 last
(nth (1- n
) namelist
))
997 (setcdr (nthcdr (- n
2) namelist
) nil
)
999 (mapconcat 'identity namelist
(nth 0 reftex-cite-punctuation
))
1000 (nth 1 reftex-cite-punctuation
)
1003 (defun reftex-format-citation (entry format
)
1004 "Format a citation from the info in the BibTeX ENTRY according to FORMAT."
1005 (unless (stringp format
) (setq format
"\\cite{%l}"))
1007 (if (and reftex-comment-citations
1008 (string-match "%l" reftex-cite-comment-format
))
1009 (error "reftex-cite-comment-format contains invalid %%l"))
1011 (while (string-match
1012 "\\(\\`\\|[^%]\\)\\(\\(%\\([0-9]*\\)\\([a-zA-Z]\\)\\)[.,;: ]*\\)"
1014 (let ((n (string-to-number (match-string 4 format
)))
1015 (l (string-to-char (match-string 5 format
)))
1021 (reftex-get-bib-field "&key" entry
)
1022 (if reftex-comment-citations
1023 reftex-cite-comment-format
1025 ((= l ?a
) (reftex-format-names
1026 (reftex-get-bib-names "author" entry
)
1028 ((= l ?A
) (car (reftex-get-bib-names "author" entry
)))
1029 ((= l ?b
) (reftex-get-bib-field "booktitle" entry
"in: %s"))
1030 ((= l ?B
) (reftex-abbreviate-title
1031 (reftex-get-bib-field "booktitle" entry
"in: %s")))
1032 ((= l ?c
) (reftex-get-bib-field "chapter" entry
))
1033 ((= l ?d
) (reftex-get-bib-field "edition" entry
))
1034 ((= l ?e
) (reftex-format-names
1035 (reftex-get-bib-names "editor" entry
)
1037 ((= l ?E
) (car (reftex-get-bib-names "editor" entry
)))
1038 ((= l ?h
) (reftex-get-bib-field "howpublished" entry
))
1039 ((= l ?i
) (reftex-get-bib-field "institution" entry
))
1040 ((= l ?j
) (reftex-get-bib-field "journal" entry
))
1041 ((= l ?k
) (reftex-get-bib-field "key" entry
))
1042 ((= l ?m
) (reftex-get-bib-field "month" entry
))
1043 ((= l ?n
) (reftex-get-bib-field "number" entry
))
1044 ((= l ?N
) (reftex-get-bib-field "note" entry
))
1045 ((= l ?o
) (reftex-get-bib-field "organization" entry
))
1046 ((= l ?p
) (reftex-get-bib-field "pages" entry
))
1047 ((= l ?P
) (car (split-string
1048 (reftex-get-bib-field "pages" entry
)
1050 ((= l ?s
) (reftex-get-bib-field "school" entry
))
1051 ((= l ?u
) (reftex-get-bib-field "publisher" entry
))
1052 ((= l ?U
) (reftex-get-bib-field "url" entry
))
1053 ((= l ?r
) (reftex-get-bib-field "address" entry
))
1054 ((= l ?t
) (reftex-get-bib-field "title" entry
))
1055 ((= l ?T
) (reftex-abbreviate-title
1056 (reftex-get-bib-field "title" entry
)))
1057 ((= l ?v
) (reftex-get-bib-field "volume" entry
))
1058 ((= l ?y
) (reftex-get-bib-field "year" entry
)))))
1060 (if (string= rpl
"")
1061 (setq b
(match-beginning 2) e
(match-end 2))
1062 (setq b
(match-beginning 3) e
(match-end 3)))
1063 (setq format
(concat (substring format
0 b
) rpl
(substring format e
)))))
1064 (while (string-match "%%" format
)
1065 (setq format
(replace-match "%" t t format
)))
1066 (while (string-match "[ ,.;:]*%<" format
)
1067 (setq format
(replace-match "" t t format
)))
1071 (defun reftex-make-cite-echo-string (entry docstruct-symbol
)
1072 "Format a bibtex ENTRY for the echo area and cache the result."
1073 (let* ((key (reftex-get-bib-field "&key" entry
))
1075 (let* ((reftex-cite-punctuation '(" " " & " " etal.")))
1076 (reftex-format-citation entry reftex-cite-view-format
)))
1077 (cache (assq 'bibview-cache
(symbol-value docstruct-symbol
)))
1078 (cache-entry (assoc key
(cdr cache
))))
1080 ;; This docstruct has no cache - make one.
1081 (set docstruct-symbol
(cons (cons 'bibview-cache nil
)
1082 (symbol-value docstruct-symbol
))))
1083 (when reftex-cache-cite-echo
1084 (setq key
(copy-sequence key
))
1085 (set-text-properties 0 (length key
) nil key
)
1086 (set-text-properties 0 (length string
) nil string
)
1088 (unless (string= (cdr cache-entry
) string
)
1089 (setcdr cache-entry string
)
1090 (put reftex-docstruct-symbol
'modified t
))
1091 (push (cons key string
) (cdr cache
))
1092 (put reftex-docstruct-symbol
'modified t
)))
1095 (defun reftex-bibtex-selection-callback (data ignore no-revisit
)
1096 "Callback function to be called from the BibTeX selection, in
1097 order to display context. This function is relatively slow and not
1098 recommended for follow mode. It works OK for individual lookups."
1099 (let ((win (selected-window))
1100 (key (reftex-get-bib-field "&key" data
))
1101 bibfile-list item bibtype
)
1104 (with-current-buffer reftex-call-back-to-this-buffer
1105 (setq bibtype
(reftex-bib-or-thebib))
1108 ; ((assq 'bib (symbol-value reftex-docstruct-symbol))
1109 (setq bibfile-list
(reftex-get-bibfile-list)))
1110 ((eq bibtype
'thebib
)
1111 ; ((assq 'thebib (symbol-value reftex-docstruct-symbol))
1116 'thebib
(symbol-value reftex-docstruct-symbol
))))
1118 (reftex-default-bibliography
1119 (setq bibfile-list
(reftex-default-bibliography)))
1120 (t (ding) (throw 'exit nil
))))
1123 (setq bibfile-list
(reftex-visited-files bibfile-list
)))
1126 (reftex-pop-to-bibtex-entry
1127 key bibfile-list
(not reftex-keep-temporary-buffers
) t item
)
1130 (select-window win
)))
1132 ;;; Global BibTeX file
1133 (defun reftex-all-used-citation-keys ()
1134 (reftex-access-scan-info)
1135 (let ((files (reftex-all-document-files)) file keys kk k
)
1136 (save-current-buffer
1137 (while (setq file
(pop files
))
1138 (set-buffer (reftex-get-file-buffer-force file
'mark
))
1142 (goto-char (point-min))
1143 (while (re-search-forward "\\(?:^\\|\\=\\)[^%\n\r]*?\\\\\\(bibentry\\|[a-zA-Z]*cite[a-zA-Z]*\\)\\(\\[[^]]*\\]\\)?{\\([^}]+\\)}" nil t
)
1144 (setq kk
(match-string-no-properties 3))
1145 (while (string-match "%.*\n?" kk
)
1146 (setq kk
(replace-match "" t t kk
)))
1147 (setq kk
(split-string kk
"[, \t\r\n]+"))
1148 (while (setq k
(pop kk
))
1150 (setq keys
(cons k keys
)))))))))
1151 (reftex-kill-temporary-buffers)
1154 (defun reftex-get-string-refs (alist)
1155 "Return a list of BibTeX @string references that appear as values in ALIST."
1156 (reftex-remove-if (lambda (x) (string-match "^\\([\"{]\\|[0-9]+$\\)" x
))
1157 ;; get list of values, discard keys
1159 ;; remove &key and &type entries
1160 (reftex-remove-if (lambda (pair)
1161 (string-match "^&" (car pair
)))
1165 (defun reftex-create-bibtex-file (bibfile)
1166 "Create a new BibTeX database BIBFILE with all entries referenced in document.
1167 The command prompts for a filename and writes the collected
1168 entries to that file. Only entries referenced in the current
1169 document with any \\cite-like macros are used. The sequence in
1170 the new file is the same as it was in the old database.
1172 Entries referenced from other entries must appear after all
1173 referencing entries.
1175 You can define strings to be used as header or footer for the
1176 created files in the variables `reftex-create-bibtex-header' or
1177 `reftex-create-bibtex-footer' respectively."
1178 (interactive "FNew BibTeX file: ")
1179 (let ((keys (reftex-all-used-citation-keys))
1180 (files (reftex-get-bibfile-list))
1181 file key entries beg end entry string-keys string-entries
)
1182 (save-current-buffer
1183 (dolist (file files
)
1184 (set-buffer (reftex-get-file-buffer-force file
'mark
))
1185 (reftex-with-special-syntax-for-bib
1189 (goto-char (point-min))
1190 (while (re-search-forward "^[ \t]*@\\(?:\\w\\|\\s_\\)+[ \t\n\r]*\
1191 \[{(][ \t\n\r]*\\([^ \t\n\r,]+\\)" nil t
)
1192 (setq key
(match-string 1)
1193 beg
(match-beginning 0)
1195 (goto-char (match-beginning 1))
1198 (error (goto-char (match-end 0))))
1200 (when (member key keys
)
1201 (setq entry
(buffer-substring beg end
)
1202 entries
(cons entry entries
)
1203 keys
(delete key keys
))
1205 ;; check for crossref entries
1206 (let* ((attr-list (reftex-parse-bibtex-entry nil beg end
))
1207 (xref-key (cdr (assoc "crossref" attr-list
))))
1208 (if xref-key
(pushnew xref-key keys
)))
1209 ;; check for string references
1210 (let* ((raw-fields (reftex-parse-bibtex-entry nil beg end t
))
1211 (string-fields (reftex-get-string-refs raw-fields
)))
1212 (dolist (skey string-fields
)
1213 (unless (member skey string-keys
)
1214 (push skey string-keys
)))))))))))
1215 ;; second pass: grab @string references
1217 (save-current-buffer
1218 (dolist (file files
)
1219 (set-buffer (reftex-get-file-buffer-force file
'mark
))
1220 (reftex-with-special-syntax-for-bib
1224 (goto-char (point-min))
1225 (while (re-search-forward
1226 "^[ \t]*@[Ss][Tt][Rr][Ii][Nn][Gg][ \t]*{[ \t]*\\([^ \t\r\n]+\\)"
1228 (setq key
(match-string 1)
1229 beg
(match-beginning 0)
1231 (goto-char (match-beginning 1))
1234 (error (goto-char (match-end 0))))
1236 (when (member key string-keys
)
1237 (setq entry
(buffer-substring beg end
)
1238 string-entries
(cons entry string-entries
)
1239 string-keys
(delete key string-keys
))))))))))
1240 (find-file-other-window bibfile
)
1241 (if (> (buffer-size) 0)
1242 (unless (yes-or-no-p
1243 (format "Overwrite non-empty file %s? " bibfile
))
1246 (if reftex-create-bibtex-header
(insert reftex-create-bibtex-header
"\n\n"))
1247 (insert (mapconcat 'identity
(reverse string-entries
) "\n\n"))
1248 (if string-entries
(insert "\n\n\n"))
1249 (insert (mapconcat 'identity
(reverse entries
) "\n\n"))
1250 (if reftex-create-bibtex-footer
(insert "\n\n" reftex-create-bibtex-footer
))
1251 (goto-char (point-min))
1253 (message "%d entries extracted and copied to new database"
1256 (provide 'reftex-cite
)
1257 ;;; reftex-cite.el ends here
1260 ;; generated-autoload-file: "reftex.el"