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