Mercurial > emacs
comparison lisp/textmodes/reftex-cite.el @ 52170:27340819ef07
Updated to version 4.21
author | Carsten Dominik <dominik@science.uva.nl> |
---|---|
date | Mon, 11 Aug 2003 12:27:02 +0000 |
parents | 4b3ce62d56f2 |
children | 695cf19ef79e |
comparison
equal
deleted
inserted
replaced
52169:b97eb548bcaa | 52170:27340819ef07 |
---|---|
1 ;;; reftex-cite.el --- creating citations with RefTeX | 1 ;;; reftex-cite.el --- creating citations with RefTeX |
2 ;; Copyright (c) 1997, 1998, 1999, 2000, 2003 Free Software Foundation, Inc. | 2 ;; Copyright (c) 1997, 1998, 1999, 2000, 2003 Free Software Foundation, Inc. |
3 | 3 |
4 ;; Author: Carsten Dominik <dominik@science.uva.nl> | 4 ;; Author: Carsten Dominik <dominik@science.uva.nl> |
5 ;; Version: 4.18 | 5 ;; Version: 4.21 |
6 | 6 |
7 ;; This file is part of GNU Emacs. | 7 ;; This file is part of GNU Emacs. |
8 | 8 |
9 ;; GNU Emacs is free software; you can redistribute it and/or modify | 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 | 10 ;; it under the terms of the GNU General Public License as published by |
55 | 55 |
56 | 56 |
57 (defmacro reftex-with-special-syntax-for-bib (&rest body) | 57 (defmacro reftex-with-special-syntax-for-bib (&rest body) |
58 `(let ((saved-syntax (syntax-table))) | 58 `(let ((saved-syntax (syntax-table))) |
59 (unwind-protect | 59 (unwind-protect |
60 (progn | 60 (progn |
61 (set-syntax-table reftex-syntax-table-for-bib) | 61 (set-syntax-table reftex-syntax-table-for-bib) |
62 ,@body) | 62 ,@body) |
63 (set-syntax-table saved-syntax)))) | 63 (set-syntax-table saved-syntax)))) |
64 | 64 |
65 (defun reftex-default-bibliography () | 65 (defun reftex-default-bibliography () |
66 ;; Return the expanded value of `reftex-default-bibliography'. | 66 ;; Return the expanded value of `reftex-default-bibliography'. |
67 ;; The expanded value is cached. | 67 ;; The expanded value is cached. |
68 (unless (eq (get 'reftex-default-bibliography :reftex-raw) | 68 (unless (eq (get 'reftex-default-bibliography :reftex-raw) |
69 reftex-default-bibliography) | 69 reftex-default-bibliography) |
70 (put 'reftex-default-bibliography :reftex-expanded | 70 (put 'reftex-default-bibliography :reftex-expanded |
71 (reftex-locate-bibliography-files | 71 (reftex-locate-bibliography-files |
72 default-directory reftex-default-bibliography)) | 72 default-directory reftex-default-bibliography)) |
73 (put 'reftex-default-bibliography :reftex-raw | 73 (put 'reftex-default-bibliography :reftex-raw |
74 reftex-default-bibliography)) | 74 reftex-default-bibliography)) |
75 (get 'reftex-default-bibliography :reftex-expanded)) | 75 (get 'reftex-default-bibliography :reftex-expanded)) |
76 | 76 |
77 (defun reftex-bib-or-thebib () | 77 (defun reftex-bib-or-thebib () |
78 ;; Tests if BibTeX or \begin{tehbibliography} should be used for the | 78 ;; Tests if BibTeX or \begin{tehbibliography} should be used for the |
79 ;; citation | 79 ;; citation |
80 ;; Find the bof of the current file | 80 ;; Find the bof of the current file |
81 (let* ((docstruct (symbol-value reftex-docstruct-symbol)) | 81 (let* ((docstruct (symbol-value reftex-docstruct-symbol)) |
82 (rest (or (member (list 'bof (buffer-file-name)) docstruct) | 82 (rest (or (member (list 'bof (buffer-file-name)) docstruct) |
83 docstruct)) | 83 docstruct)) |
84 (bib (assq 'bib rest)) | 84 (bib (assq 'bib rest)) |
85 (thebib (assq 'thebib rest)) | 85 (thebib (assq 'thebib rest)) |
86 (bibmem (memq bib rest)) | 86 (bibmem (memq bib rest)) |
87 (thebibmem (memq thebib rest))) | 87 (thebibmem (memq thebib rest))) |
88 (when (not (or thebib bib)) | 88 (when (not (or thebib bib)) |
89 (setq bib (assq 'bib docstruct) | 89 (setq bib (assq 'bib docstruct) |
90 thebib (assq 'thebib docstruct) | 90 thebib (assq 'thebib docstruct) |
91 bibmem (memq bib docstruct) | 91 bibmem (memq bib docstruct) |
92 thebibmem (memq thebib docstruct))) | 92 thebibmem (memq thebib docstruct))) |
93 (if (> (length bibmem) (length thebibmem)) | 93 (if (> (length bibmem) (length thebibmem)) |
94 (if bib 'bib nil) | 94 (if bib 'bib nil) |
95 (if thebib 'thebib nil)))) | 95 (if thebib 'thebib nil)))) |
96 | 96 |
97 (defun reftex-get-bibfile-list () | 97 (defun reftex-get-bibfile-list () |
98 ;; Return list of bibfiles for current document. | 98 ;; Return list of bibfiles for current document. |
99 ;; When using the chapterbib or bibunits package you should either | 99 ;; When using the chapterbib or bibunits package you should either |
117 (error "\\bibliography statement missing or .bib files not found"))) | 117 (error "\\bibliography statement missing or .bib files not found"))) |
118 | 118 |
119 ;; Find a certain reference in any of the BibTeX files. | 119 ;; Find a certain reference in any of the BibTeX files. |
120 | 120 |
121 (defun reftex-pop-to-bibtex-entry (key file-list &optional mark-to-kill | 121 (defun reftex-pop-to-bibtex-entry (key file-list &optional mark-to-kill |
122 highlight item return) | 122 highlight item return) |
123 ;; Find BibTeX KEY in any file in FILE-LIST in another window. | 123 ;; Find BibTeX KEY in any file in FILE-LIST in another window. |
124 ;; If MARK-TO-KILL is non-nil, mark new buffer to kill. | 124 ;; If MARK-TO-KILL is non-nil, mark new buffer to kill. |
125 ;; If HIGHLIGHT is non-nil, highlight the match. | 125 ;; If HIGHLIGHT is non-nil, highlight the match. |
126 ;; If ITEM in non-nil, search for bibitem instead of database entry. | 126 ;; If ITEM in non-nil, search for bibitem instead of database entry. |
127 ;; If RETURN is non-nil, just return the entry. | 127 ;; If RETURN is non-nil, just return the entry. |
128 | 128 |
129 (let* ((re | 129 (let* ((re |
130 (if item | 130 (if item |
131 (concat "\\\\bibitem\\(\\[[^]]*\\]\\)?{" (regexp-quote key) "}") | 131 (concat "\\\\bibitem\\(\\[[^]]*\\]\\)?{" (regexp-quote key) "}") |
132 (concat "@[a-zA-Z]+[ \t\n\r]*[{(][ \t\n\r]*" (regexp-quote key) | 132 (concat "@[a-zA-Z]+[ \t\n\r]*[{(][ \t\n\r]*" (regexp-quote key) |
133 "[, \t\r\n}]"))) | 133 "[, \t\r\n}]"))) |
134 (buffer-conf (current-buffer)) | 134 (buffer-conf (current-buffer)) |
135 file buf pos) | 135 file buf pos) |
136 | 136 |
137 (catch 'exit | 137 (catch 'exit |
138 (while file-list | 138 (while file-list |
139 (setq file (car file-list) | 139 (setq file (car file-list) |
143 (set-buffer buf) | 143 (set-buffer buf) |
144 (widen) | 144 (widen) |
145 (goto-char (point-min)) | 145 (goto-char (point-min)) |
146 (when (re-search-forward re nil t) | 146 (when (re-search-forward re nil t) |
147 (goto-char (match-beginning 0)) | 147 (goto-char (match-beginning 0)) |
148 (setq pos (point)) | 148 (setq pos (point)) |
149 (when return | 149 (when return |
150 ;; Just return the relevant entry | 150 ;; Just return the relevant entry |
151 (if item (goto-char (match-end 0))) | 151 (if item (goto-char (match-end 0))) |
152 (setq return (buffer-substring | 152 (setq return (buffer-substring |
153 (point) (reftex-end-of-bib-entry item))) | 153 (point) (reftex-end-of-bib-entry item))) |
154 (set-buffer buffer-conf) | 154 (set-buffer buffer-conf) |
155 (throw 'exit return)) | 155 (throw 'exit return)) |
156 (switch-to-buffer-other-window buf) | 156 (switch-to-buffer-other-window buf) |
157 (goto-char pos) | 157 (goto-char pos) |
158 (recenter 0) | 158 (recenter 0) |
159 (if highlight | 159 (if highlight |
160 (reftex-highlight 0 (match-beginning 0) (match-end 0))) | 160 (reftex-highlight 0 (match-beginning 0) (match-end 0))) |
161 (throw 'exit (selected-window)))) | 161 (throw 'exit (selected-window)))) |
162 (set-buffer buffer-conf) | 162 (set-buffer buffer-conf) |
163 (if item | 163 (if item |
164 (error "No \\bibitem with citation key %s" key) | 164 (error "No \\bibitem with citation key %s" key) |
165 (error "No BibTeX entry with citation key %s" key))))) | 165 (error "No BibTeX entry with citation key %s" key))))) |
166 | 166 |
167 (defun reftex-end-of-bib-entry (item) | 167 (defun reftex-end-of-bib-entry (item) |
168 (save-excursion | 168 (save-excursion |
169 (condition-case nil | 169 (condition-case nil |
170 (if item | 170 (if item |
171 (progn (end-of-line) | 171 (progn (end-of-line) |
172 (re-search-forward | 172 (re-search-forward |
173 "\\\\bibitem\\|\\end{thebibliography}") | 173 "\\\\bibitem\\|\\end{thebibliography}") |
174 (1- (match-beginning 0))) | 174 (1- (match-beginning 0))) |
175 (progn (forward-list 1) (point))) | 175 (progn (forward-list 1) (point))) |
176 (error (min (point-max) (+ 300 (point))))))) | 176 (error (min (point-max) (+ 300 (point))))))) |
177 | 177 |
178 ;; Parse bibtex buffers | 178 ;; Parse bibtex buffers |
179 | 179 |
180 (defun reftex-extract-bib-entries (buffers re-list) | 180 (defun reftex-extract-bib-entries (buffers) |
181 ;; Extract bib entries which match regexps from BUFFERS. | 181 ;; Extract bib entries which match regexps from BUFFERS. |
182 ;; BUFFERS is a list of buffers or file names. | 182 ;; BUFFERS is a list of buffers or file names. |
183 ;; Return list with entries." | 183 ;; Return list with entries." |
184 (let* ((buffer-list (if (listp buffers) buffers (list buffers))) | 184 (let* (re-list first-re rest-re |
185 (first-re (car re-list)) ; We'll use the first re to find things, | 185 (buffer-list (if (listp buffers) buffers (list buffers))) |
186 (rest-re (cdr re-list)) ; the others to narrow down. | 186 found-list entry buffer1 buffer alist |
187 found-list entry buffer1 buffer alist | 187 key-point start-point end-point default) |
188 key-point start-point end-point default) | 188 |
189 ;; Read a regexp, completing on known citation keys. | |
190 (setq default (regexp-quote (reftex-get-bibkey-default))) | |
191 (setq re-list | |
192 (split-string | |
193 (completing-read | |
194 (concat | |
195 "Regex { && Regex...}: " | |
196 "[" default "]: ") | |
197 (if reftex-mode | |
198 (if (fboundp 'LaTeX-bibitem-list) | |
199 (LaTeX-bibitem-list) | |
200 (cdr (assoc 'bibview-cache | |
201 (symbol-value reftex-docstruct-symbol)))) | |
202 nil) | |
203 nil nil nil 'reftex-cite-regexp-hist) | |
204 "[ \t]*&&[ \t]*")) | |
205 | |
206 (if (or (null re-list ) (equal re-list '(""))) | |
207 (setq re-list (list default))) | |
208 | |
209 (setq first-re (car re-list) ; We'll use the first re to find things, | |
210 rest-re (cdr re-list)) ; the others to narrow down. | |
211 (if (string-match "\\`[ \t]*\\'" (or first-re "")) | |
212 (error "Empty regular expression")) | |
189 | 213 |
190 (save-excursion | 214 (save-excursion |
191 (save-window-excursion | 215 (save-window-excursion |
192 | 216 |
193 ;; Walk through all bibtex files | 217 ;; Walk through all bibtex files |
202 (if (not buffer1) | 226 (if (not buffer1) |
203 (message "No such BibTeX file %s (ignored)" buffer) | 227 (message "No such BibTeX file %s (ignored)" buffer) |
204 (message "Scanning bibliography database %s" buffer1)) | 228 (message "Scanning bibliography database %s" buffer1)) |
205 | 229 |
206 (set-buffer buffer1) | 230 (set-buffer buffer1) |
207 (reftex-with-special-syntax-for-bib | 231 (reftex-with-special-syntax-for-bib |
208 (save-excursion | 232 (save-excursion |
209 (goto-char (point-min)) | 233 (goto-char (point-min)) |
210 (while (re-search-forward first-re nil t) | 234 (while (re-search-forward first-re nil t) |
211 (catch 'search-again | 235 (catch 'search-again |
212 (setq key-point (point)) | 236 (setq key-point (point)) |
213 (unless (re-search-backward | 237 (unless (re-search-backward |
214 "^[ \t]*@\\([a-zA-Z]+\\)[ \t\n\r]*[{(]" nil t) | 238 "\\(\\`\\|[\n\r]\\)[ \t]*@\\([a-zA-Z]+\\)[ \t\n\r]*[{(]" nil t) |
215 (throw 'search-again nil)) | 239 (throw 'search-again nil)) |
216 (setq start-point (point)) | 240 (setq start-point (point)) |
217 (goto-char (match-end 0)) | 241 (goto-char (match-end 0)) |
218 (condition-case nil | 242 (condition-case nil |
219 (up-list 1) | 243 (up-list 1) |
220 (error (goto-char key-point) | 244 (error (goto-char key-point) |
221 (throw 'search-again nil))) | 245 (throw 'search-again nil))) |
222 (setq end-point (point)) | 246 (setq end-point (point)) |
223 | 247 |
224 ;; Ignore @string, @comment and @c entries or things | 248 ;; Ignore @string, @comment and @c entries or things |
225 ;; outside entries | 249 ;; outside entries |
226 (when (or (member-ignore-case (match-string 1) | 250 (when (or (string= (downcase (match-string 2)) "string") |
227 '("string" "comment" "c")) | 251 (string= (downcase (match-string 2)) "comment") |
228 (< (point) key-point)) ; this means match not in {} | 252 (string= (downcase (match-string 2)) "c") |
229 (goto-char key-point) | 253 (< (point) key-point)) ; this means match not in {} |
230 (throw 'search-again nil)) | 254 (goto-char key-point) |
231 | 255 (throw 'search-again nil)) |
232 ;; Well, we have got a match | 256 |
233 (setq entry (buffer-substring start-point (point))) | 257 ;; Well, we have got a match |
234 | 258 ;;(setq entry (concat |
235 ;; Check if other regexp match as well | 259 ;; (buffer-substring start-point (point)) "\n")) |
236 (setq re-list rest-re) | 260 (setq entry (buffer-substring start-point (point))) |
237 (while re-list | 261 |
238 (unless (string-match (car re-list) entry) | 262 ;; Check if other regexp match as well |
239 ;; nope - move on | 263 (setq re-list rest-re) |
240 (throw 'search-again nil)) | 264 (while re-list |
241 (pop re-list)) | 265 (unless (string-match (car re-list) entry) |
242 | 266 ;; nope - move on |
243 (setq alist (reftex-parse-bibtex-entry | 267 (throw 'search-again nil)) |
244 nil start-point end-point)) | 268 (pop re-list)) |
245 (push (cons "&entry" entry) alist) | 269 |
246 | 270 (setq alist (reftex-parse-bibtex-entry |
247 ;; check for crossref entries | 271 nil start-point end-point)) |
248 (if (assoc "crossref" alist) | 272 (push (cons "&entry" entry) alist) |
249 (setq alist | 273 |
250 (append | 274 ;; check for crossref entries |
251 alist (reftex-get-crossref-alist alist)))) | 275 (if (assoc "crossref" alist) |
252 | 276 (setq alist |
253 ;; format the entry | 277 (append |
254 (push (cons "&formatted" (reftex-format-bib-entry alist)) | 278 alist (reftex-get-crossref-alist alist)))) |
255 alist) | 279 |
256 | 280 ;; format the entry |
257 ;; make key the first element | 281 (push (cons "&formatted" (reftex-format-bib-entry alist)) |
258 (push (reftex-get-bib-field "&key" alist) alist) | 282 alist) |
259 | 283 |
260 ;; add it to the list | 284 ;; make key the first element |
261 (push alist found-list))))) | 285 (push (reftex-get-bib-field "&key" alist) alist) |
262 (reftex-kill-temporary-buffers)))) | 286 |
287 ;; add it to the list | |
288 (push alist found-list))))) | |
289 (reftex-kill-temporary-buffers)))) | |
263 (setq found-list (nreverse found-list)) | 290 (setq found-list (nreverse found-list)) |
264 | 291 |
265 ;; Sorting | 292 ;; Sorting |
266 (cond | 293 (cond |
267 ((eq 'author reftex-sort-bibtex-matches) | 294 ((eq 'author reftex-sort-bibtex-matches) |
308 (error nil)) | 335 (error nil)) |
309 (reftex-parse-bibtex-entry nil start (point))) | 336 (reftex-parse-bibtex-entry nil start (point))) |
310 nil))))) | 337 nil))))) |
311 | 338 |
312 ;; Parse the bibliography environment | 339 ;; Parse the bibliography environment |
313 (defun reftex-extract-bib-entries-from-thebibliography (files re-list) | 340 (defun reftex-extract-bib-entries-from-thebibliography (files) |
314 ;; Extract bib-entries from the \begin{thebibliography} environment. | 341 ;; Extract bib-entries from the \begin{thebibliography} environment. |
315 ;; Parsing is not as good as for the BibTeX database stuff. | 342 ;; Parsing is not as good as for the BibTeX database stuff. |
316 ;; The environment should be located in file FILE. | 343 ;; The environment should be located in file FILE. |
317 | 344 |
318 (let* (start end buf entries re file default) | 345 (let* (start end buf entries re re-list file default) |
319 (unless files | 346 (unless files |
320 (error "Need file name to find thebibliography environment")) | 347 (error "Need file name to find thebibliography environment")) |
321 (while (setq file (pop files)) | 348 (while (setq file (pop files)) |
322 (setq buf (reftex-get-file-buffer-force | 349 (setq buf (reftex-get-file-buffer-force |
323 file (not reftex-keep-temporary-buffers))) | 350 file (not reftex-keep-temporary-buffers))) |
324 (unless buf | 351 (unless buf |
325 (error "No such file %s" file)) | 352 (error "No such file %s" file)) |
326 (message "Scanning thebibliography environment in %s" file) | 353 (message "Scanning thebibliography environment in %s" file) |
327 | 354 |
328 (save-excursion | 355 (save-excursion |
329 (set-buffer buf) | 356 (set-buffer buf) |
330 (save-restriction | 357 (save-restriction |
331 (widen) | 358 (widen) |
332 (goto-char (point-min)) | 359 (goto-char (point-min)) |
333 (while (re-search-forward | 360 (while (re-search-forward |
334 "\\(\\`\\|[\n\r]\\)[ \t]*\\\\begin{thebibliography}" nil t) | 361 "\\(\\`\\|[\n\r]\\)[ \t]*\\\\begin{thebibliography}" nil t) |
335 (beginning-of-line 2) | 362 (beginning-of-line 2) |
336 (setq start (point)) | 363 (setq start (point)) |
337 (if (re-search-forward | 364 (if (re-search-forward |
338 "\\(\\`\\|[\n\r]\\)[ \t]*\\\\end{thebibliography}" nil t) | 365 "\\(\\`\\|[\n\r]\\)[ \t]*\\\\end{thebibliography}" nil t) |
339 (progn | 366 (progn |
340 (beginning-of-line 1) | 367 (beginning-of-line 1) |
341 (setq end (point)))) | 368 (setq end (point)))) |
342 (when (and start end) | 369 (when (and start end) |
343 (setq entries | 370 (setq entries |
344 (append entries | 371 (append entries |
345 (mapcar 'reftex-parse-bibitem | 372 (mapcar 'reftex-parse-bibitem |
346 (delete "" | 373 (delete "" |
347 (split-string | 374 (split-string |
348 (buffer-substring-no-properties start end) | 375 (buffer-substring-no-properties start end) |
349 "[ \t\n\r]*\\\\bibitem\\(\\[[^]]*]\\)*")))))) | 376 "[ \t\n\r]*\\\\bibitem\\(\\[[^]]*]\\)*")))))) |
350 (goto-char end))))) | 377 (goto-char end))))) |
351 (unless entries | 378 (unless entries |
352 (error "No bibitems found")) | 379 (error "No bibitems found")) |
353 | 380 |
381 ;; Read a regexp, completing on known citation keys. | |
382 (setq default (regexp-quote (reftex-get-bibkey-default))) | |
383 (setq re-list | |
384 (split-string | |
385 (completing-read | |
386 (concat | |
387 "Regex { && Regex...}: " | |
388 "[" default "]: ") | |
389 (if reftex-mode | |
390 (if (fboundp 'LaTeX-bibitem-list) | |
391 (LaTeX-bibitem-list) | |
392 (cdr (assoc 'bibview-cache | |
393 (symbol-value reftex-docstruct-symbol)))) | |
394 nil) | |
395 nil nil nil 'reftex-cite-regexp-hist) | |
396 "[ \t]*&&[ \t]*")) | |
397 | |
398 (if (or (null re-list ) (equal re-list '(""))) | |
399 (setq re-list (list default))) | |
400 | |
401 (if (string-match "\\`[ \t]*\\'" (car re-list)) | |
402 (error "Empty regular expression")) | |
403 | |
354 (while (and (setq re (pop re-list)) entries) | 404 (while (and (setq re (pop re-list)) entries) |
355 (setq entries | 405 (setq entries |
356 (delq nil (mapcar | 406 (delq nil (mapcar |
357 (lambda (x) | 407 (lambda (x) |
358 (if (string-match re (cdr (assoc "&entry" x))) | 408 (if (string-match re (cdr (assoc "&entry" x))) |
359 x nil)) | 409 x nil)) |
360 entries)))) | 410 entries)))) |
361 (setq entries | 411 (setq entries |
362 (mapcar | 412 (mapcar |
363 (lambda (x) | 413 (lambda (x) |
364 (push (cons "&formatted" (reftex-format-bibitem x)) x) | 414 (push (cons "&formatted" (reftex-format-bibitem x)) x) |
365 (push (reftex-get-bib-field "&key" x) x) | 415 (push (reftex-get-bib-field "&key" x) x) |
366 x) | 416 x) |
367 entries)) | 417 entries)) |
368 | 418 |
369 entries)) | 419 entries)) |
370 | 420 |
371 (defun reftex-get-bibkey-default () | 421 (defun reftex-get-bibkey-default () |
372 ;; Return the word before the cursor. If the cursor is in a | 422 ;; Return the word before the cursor. If the cursor is in a |
373 ;; citation macro, return the word before the macro. | 423 ;; citation macro, return the word before the macro. |
374 (let* ((macro (reftex-what-macro 1))) | 424 (let* ((macro (reftex-what-macro 1))) |
375 (save-excursion | 425 (save-excursion |
376 (if (and macro (string-match "cite" (car macro))) | 426 (if (and macro (string-match "cite" (car macro))) |
377 (goto-char (cdr macro))) | 427 (goto-char (cdr macro))) |
378 (skip-chars-backward "^a-zA-Z0-9") | 428 (skip-chars-backward "^a-zA-Z0-9") |
379 (reftex-this-word)))) | 429 (reftex-this-word)))) |
380 | 430 |
381 ;; Parse and format individual entries | 431 ;; Parse and format individual entries |
382 | 432 |
401 (save-restriction | 451 (save-restriction |
402 (if entry | 452 (if entry |
403 (progn | 453 (progn |
404 (set-buffer (get-buffer-create " *RefTeX-scratch*")) | 454 (set-buffer (get-buffer-create " *RefTeX-scratch*")) |
405 (fundamental-mode) | 455 (fundamental-mode) |
406 (set-syntax-table reftex-syntax-table-for-bib) | 456 (set-syntax-table reftex-syntax-table-for-bib) |
407 (erase-buffer) | 457 (erase-buffer) |
408 (insert entry)) | 458 (insert entry)) |
409 (widen) | 459 (widen) |
410 (narrow-to-region from to)) | 460 (narrow-to-region from to)) |
411 (goto-char (point-min)) | 461 (goto-char (point-min)) |
448 | 498 |
449 (defun reftex-get-bib-field (fieldname entry &optional format) | 499 (defun reftex-get-bib-field (fieldname entry &optional format) |
450 ;; Extract the field FIELDNAME from an ENTRY | 500 ;; Extract the field FIELDNAME from an ENTRY |
451 (let ((cell (assoc fieldname entry))) | 501 (let ((cell (assoc fieldname entry))) |
452 (if cell | 502 (if cell |
453 (if format | 503 (if format |
454 (format format (cdr cell)) | 504 (format format (cdr cell)) |
455 (cdr cell)) | 505 (cdr cell)) |
456 ""))) | 506 ""))) |
457 | 507 |
458 (defun reftex-format-bib-entry (entry) | 508 (defun reftex-format-bib-entry (entry) |
459 ;; Format a BibTeX ENTRY so that it is nice to look at | 509 ;; Format a BibTeX ENTRY so that it is nice to look at |
460 (let* | 510 (let* |
485 (reftex-get-bib-field "booktitle" entry "in: %s")) | 535 (reftex-get-bib-field "booktitle" entry "in: %s")) |
486 (t "")))) | 536 (t "")))) |
487 (setq authors (reftex-truncate authors 30 t t)) | 537 (setq authors (reftex-truncate authors 30 t t)) |
488 (when (reftex-use-fonts) | 538 (when (reftex-use-fonts) |
489 (put-text-property 0 (length key) 'face | 539 (put-text-property 0 (length key) 'face |
490 (reftex-verified-face reftex-label-face | 540 (reftex-verified-face reftex-label-face |
491 'font-lock-constant-face | 541 'font-lock-constant-face |
492 'font-lock-reference-face) | 542 'font-lock-reference-face) |
493 key) | 543 key) |
494 (put-text-property 0 (length authors) 'face reftex-bib-author-face | 544 (put-text-property 0 (length authors) 'face reftex-bib-author-face |
495 authors) | 545 authors) |
496 (put-text-property 0 (length year) 'face reftex-bib-year-face | 546 (put-text-property 0 (length year) 'face reftex-bib-year-face |
497 year) | 547 year) |
504 (defun reftex-parse-bibitem (item) | 554 (defun reftex-parse-bibitem (item) |
505 ;; Parse a \bibitem entry | 555 ;; Parse a \bibitem entry |
506 (let ((key "") (text "")) | 556 (let ((key "") (text "")) |
507 (when (string-match "\\`{\\([^}]+\\)}\\([^\000]*\\)" item) | 557 (when (string-match "\\`{\\([^}]+\\)}\\([^\000]*\\)" item) |
508 (setq key (match-string 1 item) | 558 (setq key (match-string 1 item) |
509 text (match-string 2 item))) | 559 text (match-string 2 item))) |
510 ;; Clean up the text a little bit | 560 ;; Clean up the text a little bit |
511 (while (string-match "[\n\r\t]\\|[ \t][ \t]+" text) | 561 (while (string-match "[\n\r\t]\\|[ \t][ \t]+" text) |
512 (setq text (replace-match " " nil t text))) | 562 (setq text (replace-match " " nil t text))) |
513 (if (string-match "\\`[ \t]+" text) | 563 (if (string-match "\\`[ \t]+" text) |
514 (setq text (replace-match "" nil t text))) | 564 (setq text (replace-match "" nil t text))) |
515 (list | 565 (list |
516 (cons "&key" key) | 566 (cons "&key" key) |
517 (cons "&text" text) | 567 (cons "&text" text) |
518 (cons "&entry" (concat key " " text))))) | 568 (cons "&entry" (concat key " " text))))) |
519 | 569 |
520 (defun reftex-format-bibitem (item) | 570 (defun reftex-format-bibitem (item) |
521 ;; Format a \bibitem entry so that it is (relatively) nice to look at. | 571 ;; Format a \bibitem entry so that it is (relatively) nice to look at. |
522 (let ((text (reftex-get-bib-field "&text" item)) | 572 (let ((text (reftex-get-bib-field "&text" item)) |
523 (key (reftex-get-bib-field "&key" item)) | 573 (key (reftex-get-bib-field "&key" item)) |
524 (lines nil)) | 574 (lines nil)) |
525 | 575 |
526 ;; Wrap the text into several lines. | 576 ;; Wrap the text into several lines. |
527 (while (and (> (length text) 70) | 577 (while (and (> (length text) 70) |
528 (string-match " " (substring text 60))) | 578 (string-match " " (substring text 60))) |
529 (push (substring text 0 (+ 60 (match-beginning 0))) lines) | 579 (push (substring text 0 (+ 60 (match-beginning 0))) lines) |
530 (setq text (substring text (+ 61 (match-beginning 0))))) | 580 (setq text (substring text (+ 61 (match-beginning 0))))) |
531 (push text lines) | 581 (push text lines) |
532 (setq text (mapconcat 'identity (nreverse lines) "\n ")) | 582 (setq text (mapconcat 'identity (nreverse lines) "\n ")) |
533 | 583 |
534 (when (reftex-use-fonts) | 584 (when (reftex-use-fonts) |
535 (put-text-property 0 (length text) 'face reftex-bib-author-face text)) | 585 (put-text-property 0 (length text) 'face reftex-bib-author-face text)) |
577 | 627 |
578 (defun reftex-do-citation (&optional arg no-insert format-key) | 628 (defun reftex-do-citation (&optional arg no-insert format-key) |
579 ;; This really does the work of reftex-citation. | 629 ;; This really does the work of reftex-citation. |
580 | 630 |
581 (let* ((format (reftex-figure-out-cite-format arg no-insert format-key)) | 631 (let* ((format (reftex-figure-out-cite-format arg no-insert format-key)) |
582 (docstruct-symbol reftex-docstruct-symbol) | 632 (docstruct-symbol reftex-docstruct-symbol) |
583 (selected-entries (reftex-offer-bib-menu)) | 633 (selected-entries (reftex-offer-bib-menu)) |
584 (insert-entries selected-entries) | 634 (insert-entries selected-entries) |
585 entry string cite-view) | 635 entry string cite-view) |
586 | 636 |
587 (unless selected-entries (error "Quit")) | 637 (unless selected-entries (error "Quit")) |
588 | 638 |
589 (if (stringp selected-entries) | 639 (if (stringp selected-entries) |
590 ;; Nonexistent entry | 640 ;; Nonexistent entry |
591 (setq selected-entries nil | 641 (setq selected-entries nil |
592 insert-entries (list (list selected-entries | 642 insert-entries (list (list selected-entries |
593 (cons "&key" selected-entries)))) | 643 (cons "&key" selected-entries)))) |
594 ;; It makes sense to compute the cite-view strings. | 644 ;; It makes sense to compute the cite-view strings. |
595 (setq cite-view t)) | 645 (setq cite-view t)) |
596 | 646 |
597 (when (eq (car selected-entries) 'concat) | 647 (when (eq (car selected-entries) 'concat) |
598 ;; All keys go into a single command - we need to trick a little | 648 ;; All keys go into a single command - we need to trick a little |
599 (pop selected-entries) | 649 (pop selected-entries) |
600 (let ((concat-keys (mapconcat 'car selected-entries ","))) | 650 (let ((concat-keys (mapconcat 'car selected-entries ","))) |
601 (setq insert-entries | 651 (setq insert-entries |
602 (list (list concat-keys (cons "&key" concat-keys)))))) | 652 (list (list concat-keys (cons "&key" concat-keys)))))) |
603 | 653 |
604 (unless no-insert | 654 (unless no-insert |
605 | 655 |
606 ;; We shall insert this into the buffer... | 656 ;; We shall insert this into the buffer... |
607 (message "Formatting...") | 657 (message "Formatting...") |
608 | 658 |
609 (while (setq entry (pop insert-entries)) | 659 (while (setq entry (pop insert-entries)) |
610 ;; Format the citation and insert it | 660 ;; Format the citation and insert it |
611 (setq string (if reftex-format-cite-function | 661 (setq string (if reftex-format-cite-function |
612 (funcall reftex-format-cite-function | 662 (funcall reftex-format-cite-function |
613 (reftex-get-bib-field "&key" entry) | 663 (reftex-get-bib-field "&key" entry) |
614 format) | 664 format) |
615 (reftex-format-citation entry format))) | 665 (reftex-format-citation entry format))) |
616 (insert string)) | 666 (insert string)) |
617 | 667 |
618 ;; Reposition cursor? | 668 ;; Reposition cursor? |
619 (when (string-match "\\?" string) | 669 (when (string-match "\\?" string) |
620 (search-backward "?") | 670 (search-backward "?") |
621 (delete-char 1)) | 671 (delete-char 1)) |
622 | 672 |
623 ;; Tell AUCTeX | 673 ;; Tell AUCTeX |
624 (when (and reftex-mode | 674 (when (and reftex-mode |
625 (fboundp 'LaTeX-add-bibitems) | 675 (fboundp 'LaTeX-add-bibitems) |
626 reftex-plug-into-AUCTeX) | 676 reftex-plug-into-AUCTeX) |
627 (apply 'LaTeX-add-bibitems (mapcar 'car selected-entries))) | 677 (apply 'LaTeX-add-bibitems (mapcar 'car selected-entries))) |
628 | 678 |
629 ;; Produce the cite-view strings | 679 ;; Produce the cite-view strings |
630 (when (and reftex-mode reftex-cache-cite-echo cite-view) | 680 (when (and reftex-mode reftex-cache-cite-echo cite-view) |
631 (mapcar (lambda (entry) | 681 (mapcar (lambda (entry) |
632 (reftex-make-cite-echo-string entry docstruct-symbol)) | 682 (reftex-make-cite-echo-string entry docstruct-symbol)) |
633 selected-entries)) | 683 selected-entries)) |
634 | 684 |
635 (message "")) | 685 (message "")) |
636 | 686 |
637 (set-marker reftex-select-return-marker nil) | 687 (set-marker reftex-select-return-marker nil) |
638 (reftex-kill-buffer "*RefTeX Select*") | 688 (reftex-kill-buffer "*RefTeX Select*") |
639 | 689 |
640 ;; Check if the prefix arg was numeric, and call recursively | 690 ;; Check if the prefix arg was numeric, and call recursively |
641 (when (integerp arg) | 691 (when (integerp arg) |
642 (if (> arg 1) | 692 (if (> arg 1) |
643 (progn | 693 (progn |
644 (skip-chars-backward "}") | 694 (skip-chars-backward "}") |
645 (decf arg) | 695 (decf arg) |
646 (reftex-do-citation arg)) | 696 (reftex-do-citation arg)) |
647 (forward-char 1))) | 697 (forward-char 1))) |
648 | 698 |
649 ;; Return the citation key | 699 ;; Return the citation key |
650 (car (car selected-entries)))) | 700 (car (car selected-entries)))) |
651 | 701 |
652 (defun reftex-figure-out-cite-format (arg &optional no-insert format-key) | 702 (defun reftex-figure-out-cite-format (arg &optional no-insert format-key) |
653 ;; Check if there is already a cite command at point and change cite format | 703 ;; Check if there is already a cite command at point and change cite format |
654 ;; in order to only add another reference in the same cite command. | 704 ;; in order to only add another reference in the same cite command. |
655 (let ((macro (car (reftex-what-macro 1))) | 705 (let ((macro (car (reftex-what-macro 1))) |
656 (cite-format-value (reftex-get-cite-format)) | 706 (cite-format-value (reftex-get-cite-format)) |
657 key format) | 707 key format) |
658 (cond | 708 (cond |
659 (no-insert | 709 (no-insert |
660 ;; Format does not really matter because nothing will be inserted. | 710 ;; Format does not really matter because nothing will be inserted. |
661 (setq format "%l")) | 711 (setq format "%l")) |
662 | 712 |
663 ((and (stringp macro) | 713 ((and (stringp macro) |
664 (string-match "\\`\\\\cite\\|cite\\'" macro)) | 714 (string-match "\\`\\\\cite\\|cite\\'" macro)) |
665 ;; We are already inside a cite macro | 715 ;; We are already inside a cite macro |
666 (if (or (not arg) (not (listp arg))) | 716 (if (or (not arg) (not (listp arg))) |
667 (setq format | 717 (setq format |
668 (concat | 718 (concat |
669 (if (member (preceding-char) '(?\{ ?,)) "" ",") | 719 (if (member (preceding-char) '(?\{ ?,)) "" ",") |
670 "%l" | 720 "%l" |
671 (if (member (following-char) '(?\} ?,)) "" ","))) | 721 (if (member (following-char) '(?\} ?,)) "" ","))) |
672 (setq format "%l"))) | 722 (setq format "%l"))) |
673 (t | 723 (t |
674 ;; Figure out the correct format | 724 ;; Figure out the correct format |
675 (setq format | 725 (setq format |
676 (if (and (symbolp cite-format-value) | 726 (if (and (symbolp cite-format-value) |
677 (assq cite-format-value reftex-cite-format-builtin)) | 727 (assq cite-format-value reftex-cite-format-builtin)) |
678 (nth 2 (assq cite-format-value reftex-cite-format-builtin)) | 728 (nth 2 (assq cite-format-value reftex-cite-format-builtin)) |
679 cite-format-value)) | 729 cite-format-value)) |
680 (when (listp format) | 730 (when (listp format) |
681 (setq key | 731 (setq key |
682 (or format-key | 732 (or format-key |
683 (reftex-select-with-char | 733 (reftex-select-with-char |
684 "" (concat "SELECT A CITATION FORMAT\n\n" | 734 "" (concat "SELECT A CITATION FORMAT\n\n" |
685 (mapconcat | 735 (mapconcat |
686 (lambda (x) | 736 (lambda (x) |
687 (format "[%c] %s %s" (car x) | 737 (format "[%c] %s %s" (car x) |
688 (if (> (car x) 31) " " "") | 738 (if (> (car x) 31) " " "") |
689 (cdr x))) | 739 (cdr x))) |
690 format "\n"))))) | 740 format "\n"))))) |
691 (if (assq key format) | 741 (if (assq key format) |
692 (setq format (cdr (assq key format))) | 742 (setq format (cdr (assq key format))) |
693 (error "No citation format associated with key `%c'" key))))) | 743 (error "No citation format associated with key `%c'" key))))) |
694 format)) | 744 format)) |
695 | 745 |
696 (defun reftex-citep () | 746 (defun reftex-citep () |
697 "Call `reftex-citation' with a format selector `?p'." | 747 "Call `reftex-citation' with a format selector `?p'." |
698 (interactive) | 748 (interactive) |
706 (defvar reftex-select-bib-map) | 756 (defvar reftex-select-bib-map) |
707 (defun reftex-offer-bib-menu () | 757 (defun reftex-offer-bib-menu () |
708 ;; Offer bib menu and return list of selected items | 758 ;; Offer bib menu and return list of selected items |
709 | 759 |
710 (let ((bibtype (reftex-bib-or-thebib)) | 760 (let ((bibtype (reftex-bib-or-thebib)) |
711 found-list rtn key data selected-entries re-list) | 761 found-list rtn key data selected-entries) |
712 (while | 762 (while |
713 (not | 763 (not |
714 (catch 'done | 764 (catch 'done |
715 ;; Get the search regexps, completing on known citation keys. | 765 ;; Scan bibtex files |
716 (setq re-list | 766 (setq found-list |
717 (let ((default (regexp-quote (reftex-get-bibkey-default)))) | 767 (cond |
718 (split-string | 768 ((eq bibtype 'bib) |
719 (completing-read | 769 ; ((assq 'bib (symbol-value reftex-docstruct-symbol)) |
720 (concat | 770 ;; using BibTeX database files. |
721 "Regex { && Regex...}: " | 771 (reftex-extract-bib-entries (reftex-get-bibfile-list))) |
722 "[" default "]: ") | 772 ((eq bibtype 'thebib) |
723 (if reftex-mode | 773 ; ((assq 'thebib (symbol-value reftex-docstruct-symbol)) |
724 (if (fboundp 'LaTeX-bibitem-list) | 774 ;; using thebibliography environment. |
725 (LaTeX-bibitem-list) | 775 (reftex-extract-bib-entries-from-thebibliography |
726 (cdr (assoc 'bibview-cache | 776 (reftex-uniquify |
727 (symbol-value reftex-docstruct-symbol)))) | 777 (mapcar 'cdr |
728 nil) | 778 (reftex-all-assq |
729 nil nil nil 'reftex-cite-regexp-hist default) | 779 'thebib (symbol-value reftex-docstruct-symbol)))))) |
730 "[ \t]*&&[ \t]*"))) | 780 (reftex-default-bibliography |
731 | 781 (message "Using default bibliography") |
732 (if (string-match "\\`[ \t]*\\'" (car re-list)) | 782 (reftex-extract-bib-entries (reftex-default-bibliography))) |
733 (error "Empty regular expression")) | 783 (t (error "No valid bibliography in this document, and no default available")))) |
734 | 784 |
735 ;; Scan bibtex files | 785 (unless found-list |
736 (setq found-list | 786 (error "Sorry, no matches found")) |
737 (cond | 787 |
738 ((eq bibtype 'bib) | 788 ;; Remember where we came from |
739 ; ((assq 'bib (symbol-value reftex-docstruct-symbol)) | 789 (setq reftex-call-back-to-this-buffer (current-buffer)) |
740 ;; using BibTeX database files. | 790 (set-marker reftex-select-return-marker (point)) |
741 (reftex-extract-bib-entries (reftex-get-bibfile-list) re-list)) | 791 |
742 ((eq bibtype 'thebib) | 792 ;; Offer selection |
743 ; ((assq 'thebib (symbol-value reftex-docstruct-symbol)) | 793 (save-window-excursion |
744 ;; using thebibliography environment. | 794 (delete-other-windows) |
745 (reftex-extract-bib-entries-from-thebibliography | 795 (let ((default-major-mode 'reftex-select-bib-mode)) |
746 (reftex-uniquify | 796 (reftex-kill-buffer "*RefTeX Select*") |
747 (mapcar 'cdr | 797 (switch-to-buffer-other-window "*RefTeX Select*") |
748 (reftex-all-assq | 798 (unless (eq major-mode 'reftex-select-bib-mode) |
749 'thebib (symbol-value reftex-docstruct-symbol)))) | 799 (reftex-select-bib-mode)) |
750 re-list)) | 800 (let ((buffer-read-only nil)) |
751 (reftex-default-bibliography | 801 (erase-buffer) |
752 (message "Using default bibliography") | 802 (reftex-insert-bib-matches found-list))) |
753 (reftex-extract-bib-entries (reftex-default-bibliography) | 803 (setq buffer-read-only t) |
754 re-list)) | 804 (if (= 0 (buffer-size)) |
755 (t (error "No valid bibliography in this document, and no default available")))) | 805 (error "No matches found")) |
756 | 806 (setq truncate-lines t) |
757 (unless found-list | 807 (goto-char 1) |
758 (error "Sorry, no matches found")) | 808 (while t |
759 | 809 (setq rtn |
760 ;; Remember where we came from | 810 (reftex-select-item |
761 (setq reftex-call-back-to-this-buffer (current-buffer)) | 811 reftex-citation-prompt |
762 (set-marker reftex-select-return-marker (point)) | 812 reftex-citation-help |
763 | 813 reftex-select-bib-map |
764 ;; Offer selection | 814 nil |
765 (save-window-excursion | 815 'reftex-bibtex-selection-callback nil)) |
766 (delete-other-windows) | 816 (setq key (car rtn) |
767 (let ((default-major-mode 'reftex-select-bib-mode)) | 817 data (nth 1 rtn)) |
768 (reftex-kill-buffer "*RefTeX Select*") | 818 (unless key (throw 'done t)) |
769 (switch-to-buffer-other-window "*RefTeX Select*") | 819 (cond |
770 (unless (eq major-mode 'reftex-select-bib-mode) | 820 ((eq key ?g) |
771 (reftex-select-bib-mode)) | 821 ;; Start over |
772 (let ((buffer-read-only nil)) | 822 (throw 'done nil)) |
773 (erase-buffer) | 823 ((eq key ?r) |
774 (reftex-insert-bib-matches found-list))) | 824 ;; Restrict with new regular expression |
775 (setq buffer-read-only t) | 825 (setq found-list (reftex-restrict-bib-matches found-list)) |
776 (if (= 0 (buffer-size)) | 826 (let ((buffer-read-only nil)) |
777 (error "No matches found")) | 827 (erase-buffer) |
778 (setq truncate-lines t) | 828 (reftex-insert-bib-matches found-list)) |
779 (goto-char 1) | 829 (goto-char 1)) |
780 (while t | 830 ((eq key ?A) |
781 (setq rtn | 831 ;; Take all (marked) |
782 (reftex-select-item | 832 (setq selected-entries |
783 reftex-citation-prompt | 833 (if reftex-select-marked |
784 reftex-citation-help | 834 (mapcar 'car (nreverse reftex-select-marked)) |
785 reftex-select-bib-map | 835 found-list)) |
786 nil | 836 (throw 'done t)) |
787 'reftex-bibtex-selection-callback nil)) | 837 ((eq key ?a) |
788 (setq key (car rtn) | 838 ;; Take all (marked), and push the symbol 'concat |
789 data (nth 1 rtn)) | 839 (setq selected-entries |
790 (unless key (throw 'done t)) | 840 (cons 'concat |
791 (cond | 841 (if reftex-select-marked |
792 ((eq key ?g) | 842 (mapcar 'car (nreverse reftex-select-marked)) |
793 ;; Start over | 843 found-list))) |
794 (throw 'done nil)) | 844 (throw 'done t)) |
795 ((eq key ?r) | 845 ((or (eq key ?\C-m) |
796 ;; Restrict with new regular expression | 846 (eq key 'return)) |
797 (setq found-list (reftex-restrict-bib-matches found-list)) | 847 ;; Take selected |
798 (let ((buffer-read-only nil)) | 848 (setq selected-entries |
799 (erase-buffer) | 849 (if reftex-select-marked |
800 (reftex-insert-bib-matches found-list)) | 850 (cons 'concat |
801 (goto-char 1)) | 851 (mapcar 'car (nreverse reftex-select-marked))) |
802 ((eq key ?A) | 852 (if data (list data) nil))) |
803 ;; Take all (marked) | 853 (throw 'done t)) |
804 (setq selected-entries | 854 ((stringp key) |
805 (if reftex-select-marked | 855 ;; Got this one with completion |
806 (mapcar 'car (nreverse reftex-select-marked)) | 856 (setq selected-entries key) |
807 found-list)) | 857 (throw 'done t)) |
808 (throw 'done t)) | 858 (t |
809 ((eq key ?a) | 859 (ding)))))))) |
810 ;; Take all (marked), and push the symbol 'concat | |
811 (setq selected-entries | |
812 (cons 'concat | |
813 (if reftex-select-marked | |
814 (mapcar 'car (nreverse reftex-select-marked)) | |
815 found-list))) | |
816 (throw 'done t)) | |
817 ((or (eq key ?\C-m) | |
818 (eq key 'return)) | |
819 ;; Take selected | |
820 (setq selected-entries | |
821 (if reftex-select-marked | |
822 (cons 'concat | |
823 (mapcar 'car (nreverse reftex-select-marked))) | |
824 (if data (list data) nil))) | |
825 (throw 'done t)) | |
826 ((stringp key) | |
827 ;; Got this one with completion | |
828 (setq selected-entries key) | |
829 (throw 'done t)) | |
830 (t | |
831 (ding)))))))) | |
832 selected-entries)) | 860 selected-entries)) |
833 | 861 |
834 (defun reftex-restrict-bib-matches (found-list) | 862 (defun reftex-restrict-bib-matches (found-list) |
835 ;; Limit FOUND-LIST with more regular expressions | 863 ;; Limit FOUND-LIST with more regular expressions |
836 (let ((re-list (split-string (read-string | 864 (let ((re-list (split-string (read-string |
837 "RegExp [ && RegExp...]: " | 865 "RegExp [ && RegExp...]: " |
838 nil 'reftex-cite-regexp-hist) | 866 nil 'reftex-cite-regexp-hist) |
839 "[ \t]*&&[ \t]*")) | 867 "[ \t]*&&[ \t]*")) |
840 (found-list-r found-list) | 868 (found-list-r found-list) |
841 re) | 869 re) |
842 (while (setq re (pop re-list)) | 870 (while (setq re (pop re-list)) |
843 (setq found-list-r | 871 (setq found-list-r |
844 (delq nil | 872 (delq nil |
845 (mapcar | 873 (mapcar |
846 (lambda (x) | 874 (lambda (x) |
847 (if (string-match | 875 (if (string-match |
848 re (cdr (assoc "&entry" x))) | 876 re (cdr (assoc "&entry" x))) |
849 x | 877 x |
850 nil)) | 878 nil)) |
851 found-list-r)))) | 879 found-list-r)))) |
852 (if found-list-r | 880 (if found-list-r |
853 found-list-r | 881 found-list-r |
854 (ding) | 882 (ding) |
855 found-list))) | 883 found-list))) |
856 | 884 |
857 (defun reftex-insert-bib-matches (list) | 885 (defun reftex-insert-bib-matches (list) |
858 ;; Insert the bib matches and number them correctly | 886 ;; Insert the bib matches and number them correctly |
859 (let ((mouse-face | 887 (let ((mouse-face |
860 (if (memq reftex-highlight-selection '(mouse both)) | 888 (if (memq reftex-highlight-selection '(mouse both)) |
861 reftex-mouse-selected-face | 889 reftex-mouse-selected-face |
862 nil)) | 890 nil)) |
863 tmp len) | 891 tmp len) |
864 (mapcar | 892 (mapcar |
865 (lambda (x) | 893 (lambda (x) |
866 (setq tmp (cdr (assoc "&formatted" x)) | 894 (setq tmp (cdr (assoc "&formatted" x)) |
867 len (length tmp)) | 895 len (length tmp)) |
868 (put-text-property 0 len :data x tmp) | 896 (put-text-property 0 len :data x tmp) |
869 (put-text-property 0 (1- len) 'mouse-face mouse-face tmp) | 897 (put-text-property 0 (1- len) 'mouse-face mouse-face tmp) |
870 (insert tmp)) | 898 (insert tmp)) |
871 list)) | 899 list)) |
872 (run-hooks 'reftex-display-copied-context-hook)) | 900 (run-hooks 'reftex-display-copied-context-hook)) |
914 (reftex-get-bib-names "author" entry) | 942 (reftex-get-bib-names "author" entry) |
915 (or n 2))) | 943 (or n 2))) |
916 ((= l ?A) (car (reftex-get-bib-names "author" entry))) | 944 ((= l ?A) (car (reftex-get-bib-names "author" entry))) |
917 ((= l ?b) (reftex-get-bib-field "booktitle" entry "in: %s")) | 945 ((= l ?b) (reftex-get-bib-field "booktitle" entry "in: %s")) |
918 ((= l ?B) (reftex-abbreviate-title | 946 ((= l ?B) (reftex-abbreviate-title |
919 (reftex-get-bib-field "booktitle" entry "in: %s"))) | 947 (reftex-get-bib-field "booktitle" entry "in: %s"))) |
920 ((= l ?c) (reftex-get-bib-field "chapter" entry)) | 948 ((= l ?c) (reftex-get-bib-field "chapter" entry)) |
921 ((= l ?d) (reftex-get-bib-field "edition" entry)) | 949 ((= l ?d) (reftex-get-bib-field "edition" entry)) |
922 ((= l ?e) (reftex-format-names | 950 ((= l ?e) (reftex-format-names |
923 (reftex-get-bib-names "editor" entry) | 951 (reftex-get-bib-names "editor" entry) |
924 (or n 2))) | 952 (or n 2))) |
937 ((= l ?s) (reftex-get-bib-field "school" entry)) | 965 ((= l ?s) (reftex-get-bib-field "school" entry)) |
938 ((= l ?u) (reftex-get-bib-field "publisher" entry)) | 966 ((= l ?u) (reftex-get-bib-field "publisher" entry)) |
939 ((= l ?r) (reftex-get-bib-field "address" entry)) | 967 ((= l ?r) (reftex-get-bib-field "address" entry)) |
940 ((= l ?t) (reftex-get-bib-field "title" entry)) | 968 ((= l ?t) (reftex-get-bib-field "title" entry)) |
941 ((= l ?T) (reftex-abbreviate-title | 969 ((= l ?T) (reftex-abbreviate-title |
942 (reftex-get-bib-field "title" entry))) | 970 (reftex-get-bib-field "title" entry))) |
943 ((= l ?v) (reftex-get-bib-field "volume" entry)) | 971 ((= l ?v) (reftex-get-bib-field "volume" entry)) |
944 ((= l ?y) (reftex-get-bib-field "year" entry))))) | 972 ((= l ?y) (reftex-get-bib-field "year" entry))))) |
945 | 973 |
946 (if (string= rpl "") | 974 (if (string= rpl "") |
947 (setq b (match-beginning 2) e (match-end 2)) | 975 (setq b (match-beginning 2) e (match-end 2)) |
954 format) | 982 format) |
955 | 983 |
956 (defun reftex-make-cite-echo-string (entry docstruct-symbol) | 984 (defun reftex-make-cite-echo-string (entry docstruct-symbol) |
957 ;; Format a bibtex entry for the echo area and cache the result. | 985 ;; Format a bibtex entry for the echo area and cache the result. |
958 (let* ((key (reftex-get-bib-field "&key" entry)) | 986 (let* ((key (reftex-get-bib-field "&key" entry)) |
959 (string | 987 (string |
960 (let* ((reftex-cite-punctuation '(" " " & " " etal."))) | 988 (let* ((reftex-cite-punctuation '(" " " & " " etal."))) |
961 (reftex-format-citation entry reftex-cite-view-format))) | 989 (reftex-format-citation entry reftex-cite-view-format))) |
962 (cache (assq 'bibview-cache (symbol-value docstruct-symbol))) | 990 (cache (assq 'bibview-cache (symbol-value docstruct-symbol))) |
963 (cache-entry (assoc key (cdr cache)))) | 991 (cache-entry (assoc key (cdr cache)))) |
964 (unless cache | 992 (unless cache |
965 ;; This docstruct has no cache - make one. | 993 ;; This docstruct has no cache - make one. |
966 (set docstruct-symbol (cons (cons 'bibview-cache nil) | 994 (set docstruct-symbol (cons (cons 'bibview-cache nil) |
967 (symbol-value docstruct-symbol)))) | 995 (symbol-value docstruct-symbol)))) |
968 (when reftex-cache-cite-echo | 996 (when reftex-cache-cite-echo |
969 (setq key (copy-sequence key)) | 997 (setq key (copy-sequence key)) |
970 (set-text-properties 0 (length key) nil key) | 998 (set-text-properties 0 (length key) nil key) |
971 (set-text-properties 0 (length string) nil string) | 999 (set-text-properties 0 (length string) nil string) |
972 (if cache-entry | 1000 (if cache-entry |
973 (unless (string= (cdr cache-entry) string) | 1001 (unless (string= (cdr cache-entry) string) |
974 (setcdr cache-entry string) | 1002 (setcdr cache-entry string) |
975 (put reftex-docstruct-symbol 'modified t)) | 1003 (put reftex-docstruct-symbol 'modified t)) |
976 (push (cons key string) (cdr cache)) | 1004 (push (cons key string) (cdr cache)) |
977 (put reftex-docstruct-symbol 'modified t))) | 1005 (put reftex-docstruct-symbol 'modified t))) |
978 string)) | 1006 string)) |
979 | 1007 |
980 (defun reftex-bibtex-selection-callback (data ignore no-revisit) | 1008 (defun reftex-bibtex-selection-callback (data ignore no-revisit) |
981 ;; Callback function to be called from the BibTeX selection, in | 1009 ;; Callback function to be called from the BibTeX selection, in |
982 ;; order to display context. This function is relatively slow and not | 1010 ;; order to display context. This function is relatively slow and not |
985 (key (reftex-get-bib-field "&key" data)) | 1013 (key (reftex-get-bib-field "&key" data)) |
986 bibfile-list item bibtype) | 1014 bibfile-list item bibtype) |
987 | 1015 |
988 (catch 'exit | 1016 (catch 'exit |
989 (save-excursion | 1017 (save-excursion |
990 (set-buffer reftex-call-back-to-this-buffer) | 1018 (set-buffer reftex-call-back-to-this-buffer) |
991 (setq bibtype (reftex-bib-or-thebib)) | 1019 (setq bibtype (reftex-bib-or-thebib)) |
992 (cond | 1020 (cond |
993 ((eq bibtype 'bib) | 1021 ((eq bibtype 'bib) |
994 ; ((assq 'bib (symbol-value reftex-docstruct-symbol)) | 1022 ; ((assq 'bib (symbol-value reftex-docstruct-symbol)) |
995 (setq bibfile-list (reftex-get-bibfile-list))) | 1023 (setq bibfile-list (reftex-get-bibfile-list))) |
996 ((eq bibtype 'thebib) | 1024 ((eq bibtype 'thebib) |
997 ; ((assq 'thebib (symbol-value reftex-docstruct-symbol)) | 1025 ; ((assq 'thebib (symbol-value reftex-docstruct-symbol)) |
998 (setq bibfile-list | 1026 (setq bibfile-list |
999 (reftex-uniquify | 1027 (reftex-uniquify |
1000 (mapcar 'cdr | 1028 (mapcar 'cdr |
1001 (reftex-all-assq | 1029 (reftex-all-assq |
1002 'thebib (symbol-value reftex-docstruct-symbol)))) | 1030 'thebib (symbol-value reftex-docstruct-symbol)))) |
1003 item t)) | 1031 item t)) |
1004 (reftex-default-bibliography | 1032 (reftex-default-bibliography |
1005 (setq bibfile-list (reftex-default-bibliography))) | 1033 (setq bibfile-list (reftex-default-bibliography))) |
1006 (t (ding) (throw 'exit nil)))) | 1034 (t (ding) (throw 'exit nil)))) |
1007 | 1035 |
1008 (when no-revisit | 1036 (when no-revisit |
1009 (setq bibfile-list (reftex-visited-files bibfile-list))) | 1037 (setq bibfile-list (reftex-visited-files bibfile-list))) |
1010 | 1038 |
1011 (condition-case nil | 1039 (condition-case nil |
1012 (reftex-pop-to-bibtex-entry | 1040 (reftex-pop-to-bibtex-entry |
1013 key bibfile-list (not reftex-keep-temporary-buffers) t item) | 1041 key bibfile-list (not reftex-keep-temporary-buffers) t item) |
1014 (error (ding)))) | 1042 (error (ding)))) |
1015 | 1043 |
1016 (select-window win))) | 1044 (select-window win))) |
1017 | 1045 |
1018 ;;; reftex-cite.el ends here | 1046 ;;; reftex-cite.el ends here |