| 1 |
|
|---|
| 2 |
|
|---|
| 3 |
|
|---|
| 4 |
|
|---|
| 5 |
|
|---|
| 6 |
|
|---|
| 7 |
|
|---|
| 8 |
|
|---|
| 9 |
|
|---|
| 10 |
|
|---|
| 11 |
|
|---|
| 12 |
|
|---|
| 13 |
|
|---|
| 14 |
|
|---|
| 15 |
|
|---|
| 16 |
|
|---|
| 17 |
|
|---|
| 18 |
|
|---|
| 19 |
|
|---|
| 20 |
|
|---|
| 21 |
|
|---|
| 22 |
|
|---|
| 23 |
|
|---|
| 24 |
|
|---|
| 25 |
|
|---|
| 26 |
|
|---|
| 27 |
|
|---|
| 28 |
|
|---|
| 29 |
|
|---|
| 30 |
|
|---|
| 31 |
(eval-when-compile (require 'cl)) |
|---|
| 32 |
(provide 'reftex-cite) |
|---|
| 33 |
(require 'reftex) |
|---|
| 34 |
|
|---|
| 35 |
|
|---|
| 36 |
|
|---|
| 37 |
|
|---|
| 38 |
|
|---|
| 39 |
(defvar reftex-cite-regexp-hist nil) |
|---|
| 40 |
|
|---|
| 41 |
|
|---|
| 42 |
(defconst reftex-citation-prompt |
|---|
| 43 |
"Select: [n]ext [p]revious [r]estrict [ ]full_entry [q]uit RET [?]Help+more") |
|---|
| 44 |
|
|---|
| 45 |
(defconst reftex-citation-help |
|---|
| 46 |
" n / p Go to next/previous entry (Cursor motion works as well). |
|---|
| 47 |
g / r Start over with new regexp / Refine with additional regexp. |
|---|
| 48 |
SPC Show full database entry in other window. |
|---|
| 49 |
f Toggle follow mode: Other window will follow with full db entry. |
|---|
| 50 |
. Show insertion point. |
|---|
| 51 |
q Quit without inserting \\cite macro into buffer. |
|---|
| 52 |
TAB Enter citation key with completion. |
|---|
| 53 |
RET Accept current entry (also on mouse-2) and create \\cite macro. |
|---|
| 54 |
m / u Mark/Unmark the entry. |
|---|
| 55 |
e / E Create BibTeX file with all (marked/unmarked) entries |
|---|
| 56 |
a / A Put all (marked) entries into one/many \\cite commands.") |
|---|
| 57 |
|
|---|
| 58 |
|
|---|
| 59 |
|
|---|
| 60 |
(defmacro reftex-with-special-syntax-for-bib (&rest body) |
|---|
| 61 |
`(let ((saved-syntax (syntax-table))) |
|---|
| 62 |
(unwind-protect |
|---|
| 63 |
(progn |
|---|
| 64 |
(set-syntax-table reftex-syntax-table-for-bib) |
|---|
| 65 |
,@body) |
|---|
| 66 |
(set-syntax-table saved-syntax)))) |
|---|
| 67 |
|
|---|
| 68 |
(defun reftex-default-bibliography () |
|---|
| 69 |
|
|---|
| 70 |
|
|---|
| 71 |
(unless (eq (get 'reftex-default-bibliography :reftex-raw) |
|---|
| 72 |
reftex-default-bibliography) |
|---|
| 73 |
(put 'reftex-default-bibliography :reftex-expanded |
|---|
| 74 |
(reftex-locate-bibliography-files |
|---|
| 75 |
default-directory reftex-default-bibliography)) |
|---|
| 76 |
(put 'reftex-default-bibliography :reftex-raw |
|---|
| 77 |
reftex-default-bibliography)) |
|---|
| 78 |
(get 'reftex-default-bibliography :reftex-expanded)) |
|---|
| 79 |
|
|---|
| 80 |
(defun reftex-bib-or-thebib () |
|---|
| 81 |
|
|---|
| 82 |
|
|---|
| 83 |
|
|---|
| 84 |
(let* ((docstruct (symbol-value reftex-docstruct-symbol)) |
|---|
| 85 |
(rest (or (member (list 'bof (buffer-file-name)) docstruct) |
|---|
| 86 |
docstruct)) |
|---|
| 87 |
(bib (assq 'bib rest)) |
|---|
| 88 |
(thebib (assq 'thebib rest)) |
|---|
| 89 |
(bibmem (memq bib rest)) |
|---|
| 90 |
(thebibmem (memq thebib rest))) |
|---|
| 91 |
(when (not (or thebib bib)) |
|---|
| 92 |
(setq bib (assq 'bib docstruct) |
|---|
| 93 |
thebib (assq 'thebib docstruct) |
|---|
| 94 |
bibmem (memq bib docstruct) |
|---|
| 95 |
thebibmem (memq thebib docstruct))) |
|---|
| 96 |
(if (> (length bibmem) (length thebibmem)) |
|---|
| 97 |
(if bib 'bib nil) |
|---|
| 98 |
(if thebib 'thebib nil)))) |
|---|
| 99 |
|
|---|
| 100 |
(defun reftex-get-bibfile-list () |
|---|
| 101 |
|
|---|
| 102 |
|
|---|
| 103 |
|
|---|
| 104 |
|
|---|
| 105 |
|
|---|
| 106 |
|
|---|
| 107 |
|
|---|
| 108 |
(reftex-access-scan-info) |
|---|
| 109 |
(or |
|---|
| 110 |
|
|---|
| 111 |
(cdr (reftex-last-assoc-before-elt |
|---|
| 112 |
'bib (list 'eof (buffer-file-name)) |
|---|
| 113 |
(member (list 'bof (buffer-file-name)) |
|---|
| 114 |
(symbol-value reftex-docstruct-symbol)))) |
|---|
| 115 |
|
|---|
| 116 |
(cdr (assq 'bib (member (list 'bof (buffer-file-name)) |
|---|
| 117 |
(symbol-value reftex-docstruct-symbol)))) |
|---|
| 118 |
|
|---|
| 119 |
(cdr (assq 'bib (symbol-value reftex-docstruct-symbol))) |
|---|
| 120 |
(error "\\bibliography statement missing or .bib files not found"))) |
|---|
| 121 |
|
|---|
| 122 |
|
|---|
| 123 |
|
|---|
| 124 |
(defun reftex-pop-to-bibtex-entry (key file-list &optional mark-to-kill |
|---|
| 125 |
highlight item return) |
|---|
| 126 |
|
|---|
| 127 |
|
|---|
| 128 |
|
|---|
| 129 |
|
|---|
| 130 |
|
|---|
| 131 |
|
|---|
| 132 |
(let* ((re |
|---|
| 133 |
(if item |
|---|
| 134 |
(concat "\\\\bibitem\\(\\[[^]]*\\]\\)?{" (regexp-quote key) "}") |
|---|
| 135 |
(concat "@[a-zA-Z]+[ \t\n\r]*[{(][ \t\n\r]*" (regexp-quote key) |
|---|
| 136 |
"[, \t\r\n}]"))) |
|---|
| 137 |
(buffer-conf (current-buffer)) |
|---|
| 138 |
file buf pos oldpos) |
|---|
| 139 |
|
|---|
| 140 |
(catch 'exit |
|---|
| 141 |
(while file-list |
|---|
| 142 |
(setq file (car file-list) |
|---|
| 143 |
file-list (cdr file-list)) |
|---|
| 144 |
(unless (setq buf (reftex-get-file-buffer-force file mark-to-kill)) |
|---|
| 145 |
(error "No such file %s" file)) |
|---|
| 146 |
(set-buffer buf) |
|---|
| 147 |
(setq oldpos (point)) |
|---|
| 148 |
(widen) |
|---|
| 149 |
(goto-char (point-min)) |
|---|
| 150 |
(if (not (re-search-forward re nil t)) |
|---|
| 151 |
(goto-char oldpos) |
|---|
| 152 |
(goto-char (match-beginning 0)) |
|---|
| 153 |
(setq pos (point)) |
|---|
| 154 |
(when return |
|---|
| 155 |
|
|---|
| 156 |
(if item (goto-char (match-end 0))) |
|---|
| 157 |
(setq return (buffer-substring |
|---|
| 158 |
(point) (reftex-end-of-bib-entry item))) |
|---|
| 159 |
(goto-char oldpos) |
|---|
| 160 |
(set-buffer buffer-conf) |
|---|
| 161 |
(throw 'exit return)) |
|---|
| 162 |
(switch-to-buffer-other-window buf) |
|---|
| 163 |
(goto-char pos) |
|---|
| 164 |
(recenter 0) |
|---|
| 165 |
(if highlight |
|---|
| 166 |
(reftex-highlight 0 (match-beginning 0) (match-end 0))) |
|---|
| 167 |
(throw 'exit (selected-window)))) |
|---|
| 168 |
(set-buffer buffer-conf) |
|---|
| 169 |
(if item |
|---|
| 170 |
(error "No \\bibitem with citation key %s" key) |
|---|
| 171 |
(error "No BibTeX entry with citation key %s" key))))) |
|---|
| 172 |
|
|---|
| 173 |
(defun reftex-end-of-bib-entry (item) |
|---|
| 174 |
(save-excursion |
|---|
| 175 |
(condition-case nil |
|---|
| 176 |
(if item |
|---|
| 177 |
(progn (end-of-line) |
|---|
| 178 |
(re-search-forward |
|---|
| 179 |
"\\\\bibitem\\|\\end{thebibliography}") |
|---|
| 180 |
(1- (match-beginning 0))) |
|---|
| 181 |
(progn (forward-list 1) (point))) |
|---|
| 182 |
(error (min (point-max) (+ 300 (point))))))) |
|---|
| 183 |
|
|---|
| 184 |
|
|---|
| 185 |
|
|---|
| 186 |
(defun reftex-extract-bib-entries (buffers) |
|---|
| 187 |
|
|---|
| 188 |
|
|---|
| 189 |
|
|---|
| 190 |
(let* (re-list first-re rest-re |
|---|
| 191 |
(buffer-list (if (listp buffers) buffers (list buffers))) |
|---|
| 192 |
found-list entry buffer1 buffer alist |
|---|
| 193 |
key-point start-point end-point default) |
|---|
| 194 |
|
|---|
| 195 |
|
|---|
| 196 |
(setq default (regexp-quote (reftex-get-bibkey-default))) |
|---|
| 197 |
(setq re-list |
|---|
| 198 |
(split-string |
|---|
| 199 |
(completing-read |
|---|
| 200 |
(concat |
|---|
| 201 |
"Regex { && Regex...}: " |
|---|
| 202 |
"[" default "]: ") |
|---|
| 203 |
(if reftex-mode |
|---|
| 204 |
(if (fboundp 'LaTeX-bibitem-list) |
|---|
| 205 |
(LaTeX-bibitem-list) |
|---|
| 206 |
(cdr (assoc 'bibview-cache |
|---|
| 207 |
(symbol-value reftex-docstruct-symbol)))) |
|---|
| 208 |
nil) |
|---|
| 209 |
nil nil nil 'reftex-cite-regexp-hist) |
|---|
| 210 |
"[ \t]*&&[ \t]*")) |
|---|
| 211 |
|
|---|
| 212 |
(if (or (null re-list ) (equal re-list '(""))) |
|---|
| 213 |
(setq re-list (list default))) |
|---|
| 214 |
|
|---|
| 215 |
(setq first-re (car re-list) |
|---|
| 216 |
rest-re (cdr re-list)) |
|---|
| 217 |
(if (string-match "\\`[ \t]*\\'" (or first-re "")) |
|---|
| 218 |
(error "Empty regular expression")) |
|---|
| 219 |
|
|---|
| 220 |
(save-excursion |
|---|
| 221 |
(save-window-excursion |
|---|
| 222 |
|
|---|
| 223 |
|
|---|
| 224 |
(while buffer-list |
|---|
| 225 |
(setq buffer (car buffer-list) |
|---|
| 226 |
buffer-list (cdr buffer-list)) |
|---|
| 227 |
(if (and (bufferp buffer) |
|---|
| 228 |
(buffer-live-p buffer)) |
|---|
| 229 |
(setq buffer1 buffer) |
|---|
| 230 |
(setq buffer1 (reftex-get-file-buffer-force |
|---|
| 231 |
buffer (not reftex-keep-temporary-buffers)))) |
|---|
| 232 |
(if (not buffer1) |
|---|
| 233 |
(message "No such BibTeX file %s (ignored)" buffer) |
|---|
| 234 |
(message "Scanning bibliography database %s" buffer1)) |
|---|
| 235 |
|
|---|
| 236 |
(set-buffer buffer1) |
|---|
| 237 |
(reftex-with-special-syntax-for-bib |
|---|
| 238 |
(save-excursion |
|---|
| 239 |
(goto-char (point-min)) |
|---|
| 240 |
(while (re-search-forward first-re nil t) |
|---|
| 241 |
(catch 'search-again |
|---|
| 242 |
(setq key-point (point)) |
|---|
| 243 |
(unless (re-search-backward |
|---|
| 244 |
"\\(\\`\\|[\n\r]\\)[ \t]*@\\([a-zA-Z]+\\)[ \t\n\r]*[{(]" nil t) |
|---|
| 245 |
(throw 'search-again nil)) |
|---|
| 246 |
(setq start-point (point)) |
|---|
| 247 |
(goto-char (match-end 0)) |
|---|
| 248 |
(condition-case nil |
|---|
| 249 |
(up-list 1) |
|---|
| 250 |
(error (goto-char key-point) |
|---|
| 251 |
(throw 'search-again nil))) |
|---|
| 252 |
(setq end-point (point)) |
|---|
| 253 |
|
|---|
| 254 |
|
|---|
| 255 |
|
|---|
| 256 |
(when (or (string= (downcase (match-string 2)) "string") |
|---|
| 257 |
(string= (downcase (match-string 2)) "comment") |
|---|
| 258 |
(string= (downcase (match-string 2)) "c") |
|---|
| 259 |
(< (point) key-point)) |
|---|
| 260 |
(goto-char key-point) |
|---|
| 261 |
(throw 'search-again nil)) |
|---|
| 262 |
|
|---|
| 263 |
|
|---|
| 264 |
|
|---|
| 265 |
|
|---|
| 266 |
(setq entry (buffer-substring start-point (point))) |
|---|
| 267 |
|
|---|
| 268 |
|
|---|
| 269 |
(setq re-list rest-re) |
|---|
| 270 |
(while re-list |
|---|
| 271 |
(unless (string-match (car re-list) entry) |
|---|
| 272 |
|
|---|
| 273 |
(throw 'search-again nil)) |
|---|
| 274 |
(pop re-list)) |
|---|
| 275 |
|
|---|
| 276 |
(setq alist (reftex-parse-bibtex-entry |
|---|
| 277 |
nil start-point end-point)) |
|---|
| 278 |
(push (cons "&entry" entry) alist) |
|---|
| 279 |
|
|---|
| 280 |
|
|---|
| 281 |
(if (assoc "crossref" alist) |
|---|
| 282 |
(setq alist |
|---|
| 283 |
(append |
|---|
| 284 |
alist (reftex-get-crossref-alist alist)))) |
|---|
| 285 |
|
|---|
| 286 |
|
|---|
| 287 |
(push (cons "&formatted" (reftex-format-bib-entry alist)) |
|---|
| 288 |
alist) |
|---|
| 289 |
|
|---|
| 290 |
|
|---|
| 291 |
(push (reftex-get-bib-field "&key" alist) alist) |
|---|
| 292 |
|
|---|
| 293 |
|
|---|
| 294 |
(push alist found-list))))) |
|---|
| 295 |
(reftex-kill-temporary-buffers)))) |
|---|
| 296 |
(setq found-list (nreverse found-list)) |
|---|
| 297 |
|
|---|
| 298 |
|
|---|
| 299 |
(cond |
|---|
| 300 |
((eq 'author reftex-sort-bibtex-matches) |
|---|
| 301 |
(sort found-list 'reftex-bib-sort-author)) |
|---|
| 302 |
((eq 'year reftex-sort-bibtex-matches) |
|---|
| 303 |
(sort found-list 'reftex-bib-sort-year)) |
|---|
| 304 |
((eq 'reverse-year reftex-sort-bibtex-matches) |
|---|
| 305 |
(sort found-list 'reftex-bib-sort-year-reverse)) |
|---|
| 306 |
(t found-list)))) |
|---|
| 307 |
|
|---|
| 308 |
(defun reftex-bib-sort-author (e1 e2) |
|---|
| 309 |
(let ((al1 (reftex-get-bib-names "author" e1)) |
|---|
| 310 |
(al2 (reftex-get-bib-names "author" e2))) |
|---|
| 311 |
(while (and al1 al2 (string= (car al1) (car al2))) |
|---|
| 312 |
(pop al1) |
|---|
| 313 |
(pop al2)) |
|---|
| 314 |
(if (and (stringp (car al1)) |
|---|
| 315 |
(stringp (car al2))) |
|---|
| 316 |
(string< (car al1) (car al2)) |
|---|
| 317 |
(not (stringp (car al1)))))) |
|---|
| 318 |
|
|---|
| 319 |
(defun reftex-bib-sort-year (e1 e2) |
|---|
| 320 |
(< (string-to-number (or (cdr (assoc "year" e1)) "0")) |
|---|
| 321 |
(string-to-number (or (cdr (assoc "year" e2)) "0")))) |
|---|
| 322 |
|
|---|
| 323 |
(defun reftex-bib-sort-year-reverse (e1 e2) |
|---|
| 324 |
(> (string-to-number (or (cdr (assoc "year" e1)) "0")) |
|---|
| 325 |
(string-to-number (or (cdr (assoc "year" e2)) "0")))) |
|---|
| 326 |
|
|---|
| 327 |
(defun reftex-get-crossref-alist (entry) |
|---|
| 328 |
|
|---|
| 329 |
(let ((crkey (cdr (assoc "crossref" entry))) |
|---|
| 330 |
start) |
|---|
| 331 |
(save-excursion |
|---|
| 332 |
(save-restriction |
|---|
| 333 |
(widen) |
|---|
| 334 |
(if (re-search-forward |
|---|
| 335 |
(concat "@\\w+[{(][ \t\n\r]*" (regexp-quote crkey) |
|---|
| 336 |
"[ \t\n\r]*,") nil t) |
|---|
| 337 |
(progn |
|---|
| 338 |
(setq start (match-beginning 0)) |
|---|
| 339 |
(condition-case nil |
|---|
| 340 |
(up-list 1) |
|---|
| 341 |
(error nil)) |
|---|
| 342 |
(reftex-parse-bibtex-entry nil start (point))) |
|---|
| 343 |
nil))))) |
|---|
| 344 |
|
|---|
| 345 |
|
|---|
| 346 |
(defun reftex-extract-bib-entries-from-thebibliography (files) |
|---|
| 347 |
|
|---|
| 348 |
|
|---|
| 349 |
|
|---|
| 350 |
|
|---|
| 351 |
(let* (start end buf entries re re-list file default) |
|---|
| 352 |
(unless files |
|---|
| 353 |
(error "Need file name to find thebibliography environment")) |
|---|
| 354 |
(while (setq file (pop files)) |
|---|
| 355 |
(setq buf (reftex-get-file-buffer-force |
|---|
| 356 |
file (not reftex-keep-temporary-buffers))) |
|---|
| 357 |
(unless buf |
|---|
| 358 |
(error "No such file %s" file)) |
|---|
| 359 |
(message "Scanning thebibliography environment in %s" file) |
|---|
| 360 |
|
|---|
| 361 |
(save-excursion |
|---|
| 362 |
(set-buffer buf) |
|---|
| 363 |
(save-restriction |
|---|
| 364 |
(widen) |
|---|
| 365 |
(goto-char (point-min)) |
|---|
| 366 |
(while (re-search-forward |
|---|
| 367 |
"\\(\\`\\|[\n\r]\\)[ \t]*\\\\begin{thebibliography}" nil t) |
|---|
| 368 |
(beginning-of-line 2) |
|---|
| 369 |
(setq start (point)) |
|---|
| 370 |
(if (re-search-forward |
|---|
| 371 |
"\\(\\`\\|[\n\r]\\)[ \t]*\\\\end{thebibliography}" nil t) |
|---|
| 372 |
(progn |
|---|
| 373 |
(beginning-of-line 1) |
|---|
| 374 |
(setq end (point)))) |
|---|
| 375 |
(when (and start end) |
|---|
| 376 |
(setq entries |
|---|
| 377 |
(append entries |
|---|
| 378 |
(mapcar 'reftex-parse-bibitem |
|---|
| 379 |
(delete "" |
|---|
| 380 |
(split-string |
|---|
| 381 |
(buffer-substring-no-properties start end) |
|---|
| 382 |
"[ \t\n\r]*\\\\bibitem\\(\\[[^]]*]\\)*")))))) |
|---|
| 383 |
(goto-char end))))) |
|---|
| 384 |
(unless entries |
|---|
| 385 |
(error "No bibitems found")) |
|---|
| 386 |
|
|---|
| 387 |
|
|---|
| 388 |
(setq default (regexp-quote (reftex-get-bibkey-default))) |
|---|
| 389 |
(setq re-list |
|---|
| 390 |
(split-string |
|---|
| 391 |
(completing-read |
|---|
| 392 |
(concat |
|---|
| 393 |
"Regex { && Regex...}: " |
|---|
| 394 |
"[" default "]: ") |
|---|
| 395 |
(if reftex-mode |
|---|
| 396 |
(if (fboundp 'LaTeX-bibitem-list) |
|---|
| 397 |
(LaTeX-bibitem-list) |
|---|
| 398 |
(cdr (assoc 'bibview-cache |
|---|
| 399 |
(symbol-value reftex-docstruct-symbol)))) |
|---|
| 400 |
nil) |
|---|
| 401 |
nil nil nil 'reftex-cite-regexp-hist) |
|---|
| 402 |
"[ \t]*&&[ \t]*")) |
|---|
| 403 |
|
|---|
| 404 |
(if (or (null re-list ) (equal re-list '(""))) |
|---|
| 405 |
(setq re-list (list default))) |
|---|
| 406 |
|
|---|
| 407 |
(if (string-match "\\`[ \t]*\\'" (car re-list)) |
|---|
| 408 |
(error "Empty regular expression")) |
|---|
| 409 |
|
|---|
| 410 |
(while (and (setq re (pop re-list)) entries) |
|---|
| 411 |
(setq entries |
|---|
| 412 |
(delq nil (mapcar |
|---|
| 413 |
(lambda (x) |
|---|
| 414 |
(if (string-match re (cdr (assoc "&entry" x))) |
|---|
| 415 |
x nil)) |
|---|
| 416 |
entries)))) |
|---|
| 417 |
(setq entries |
|---|
| 418 |
(mapcar |
|---|
| 419 |
(lambda (x) |
|---|
| 420 |
(push (cons "&formatted" (reftex-format-bibitem x)) x) |
|---|
| 421 |
(push (reftex-get-bib-field "&key" x) x) |
|---|
| 422 |
x) |
|---|
| 423 |
entries)) |
|---|
| 424 |
|
|---|
| 425 |
entries)) |
|---|
| 426 |
|
|---|
| 427 |
(defun reftex-get-bibkey-default () |
|---|
| 428 |
|
|---|
| 429 |
|
|---|
| 430 |
(let* ((macro (reftex-what-macro 1))) |
|---|
| 431 |
(save-excursion |
|---|
| 432 |
(if (and macro (string-match "cite" (car macro))) |
|---|
| 433 |
(goto-char (cdr macro))) |
|---|
| 434 |
(skip-chars-backward "^a-zA-Z0-9") |
|---|
| 435 |
(reftex-this-word)))) |
|---|
| 436 |
|
|---|
| 437 |
|
|---|
| 438 |
|
|---|
| 439 |
(defun reftex-get-bib-names (field entry) |
|---|
| 440 |
|
|---|
| 441 |
(let ((names (reftex-get-bib-field field entry))) |
|---|
| 442 |
(if (equal "" names) |
|---|
| 443 |
(setq names (reftex-get-bib-field "editor" entry))) |
|---|
| 444 |
(while (string-match "\\band\\b[ \t]*" names) |
|---|
| 445 |
(setq names (replace-match "\n" nil t names))) |
|---|
| 446 |
(while (string-match "[\\.a-zA-Z\\-]+\\.[ \t]*\\|,.*\\|[{}]+" names) |
|---|
| 447 |
(setq names (replace-match "" nil t names))) |
|---|
| 448 |
(while (string-match "^[ \t]+\\|[ \t]+$" names) |
|---|
| 449 |
(setq names (replace-match "" nil t names))) |
|---|
| 450 |
(while (string-match "[ \t][ \t]+" names) |
|---|
| 451 |
(setq names (replace-match " " nil t names))) |
|---|
| 452 |
(split-string names "\n"))) |
|---|
| 453 |
|
|---|
| 454 |
(defun reftex-parse-bibtex-entry (entry &optional from to) |
|---|
| 455 |
(let (alist key start field) |
|---|
| 456 |
(save-excursion |
|---|
| 457 |
(save-restriction |
|---|
| 458 |
(if entry |
|---|
| 459 |
(progn |
|---|
| 460 |
(set-buffer (get-buffer-create " *RefTeX-scratch*")) |
|---|
| 461 |
(fundamental-mode) |
|---|
| 462 |
(set-syntax-table reftex-syntax-table-for-bib) |
|---|
| 463 |
(erase-buffer) |
|---|
| 464 |
(insert entry)) |
|---|
| 465 |
(widen) |
|---|
| 466 |
(narrow-to-region from to)) |
|---|
| 467 |
(goto-char (point-min)) |
|---|
| 468 |
|
|---|
| 469 |
(if (re-search-forward |
|---|
| 470 |
"@\\(\\w+\\)[ \t\n\r]*[{(][ \t\n\r]*\\([^ \t\n\r,]+\\)" nil t) |
|---|
| 471 |
(setq alist |
|---|
| 472 |
(list |
|---|
| 473 |
(cons "&type" (downcase (reftex-match-string 1))) |
|---|
| 474 |
(cons "&key" (reftex-match-string 2))))) |
|---|
| 475 |
(while (re-search-forward "\\(\\w+\\)[ \t\n\r]*=[ \t\n\r]*" nil t) |
|---|
| 476 |
(setq key (downcase (reftex-match-string 1))) |
|---|
| 477 |
(cond |
|---|
| 478 |
((= (following-char) ?{) |
|---|
| 479 |
(forward-char 1) |
|---|
| 480 |
(setq start (point)) |
|---|
| 481 |
(condition-case nil |
|---|
| 482 |
(up-list 1) |
|---|
| 483 |
(error nil))) |
|---|
| 484 |
((= (following-char) ?\") |
|---|
| 485 |
(forward-char 1) |
|---|
| 486 |
(setq start (point)) |
|---|
| 487 |
(while (and (search-forward "\"" nil t) |
|---|
| 488 |
(= ?\\ (char-after (- (point) 2)))))) |
|---|
| 489 |
(t |
|---|
| 490 |
(setq start (point)) |
|---|
| 491 |
(re-search-forward "[ \t]*[\n\r,}]" nil 1))) |
|---|
| 492 |
(setq field (buffer-substring-no-properties start (1- (point)))) |
|---|
| 493 |
|
|---|
| 494 |
(while (string-match "[\n\t\r]\\|[ \t][ \t]+" field) |
|---|
| 495 |
(setq field (replace-match " " nil t field))) |
|---|
| 496 |
|
|---|
| 497 |
(if (string-match "^[ \t{]+" field) |
|---|
| 498 |
(setq field (replace-match "" nil t field))) |
|---|
| 499 |
|
|---|
| 500 |
(if (string-match "[ \t}]+$" field) |
|---|
| 501 |
(setq field (replace-match "" nil t field))) |
|---|
| 502 |
(push (cons key field) alist)))) |
|---|
| 503 |
alist)) |
|---|
| 504 |
|
|---|
| 505 |
(defun reftex-get-bib-field (fieldname entry &optional format) |
|---|
| 506 |
|
|---|
| 507 |
(let ((cell (assoc fieldname entry))) |
|---|
| 508 |
(if cell |
|---|
| 509 |
(if format |
|---|
| 510 |
(format format (cdr cell)) |
|---|
| 511 |
(cdr cell)) |
|---|
| 512 |
""))) |
|---|
| 513 |
|
|---|
| 514 |
(defun reftex-format-bib-entry (entry) |
|---|
| 515 |
|
|---|
| 516 |
(let* |
|---|
| 517 |
((auth-list (reftex-get-bib-names "author" entry)) |
|---|
| 518 |
(authors (mapconcat 'identity auth-list ", ")) |
|---|
| 519 |
(year (reftex-get-bib-field "year" entry)) |
|---|
| 520 |
(title (reftex-get-bib-field "title" entry)) |
|---|
| 521 |
(type (reftex-get-bib-field "&type" entry)) |
|---|
| 522 |
(key (reftex-get-bib-field "&key" entry)) |
|---|
| 523 |
(extra |
|---|
| 524 |
(cond |
|---|
| 525 |
((equal type "article") |
|---|
| 526 |
(concat (reftex-get-bib-field "journal" entry) " " |
|---|
| 527 |
(reftex-get-bib-field "volume" entry) ", " |
|---|
| 528 |
(reftex-get-bib-field "pages" entry))) |
|---|
| 529 |
((equal type "book") |
|---|
| 530 |
(concat "book (" (reftex-get-bib-field "publisher" entry) ")")) |
|---|
| 531 |
((equal type "phdthesis") |
|---|
| 532 |
(concat "PhD: " (reftex-get-bib-field "school" entry))) |
|---|
| 533 |
((equal type "mastersthesis") |
|---|
| 534 |
(concat "Master: " (reftex-get-bib-field "school" entry))) |
|---|
| 535 |
((equal type "inbook") |
|---|
| 536 |
(concat "Chap: " (reftex-get-bib-field "chapter" entry) |
|---|
| 537 |
", pp. " (reftex-get-bib-field "pages" entry))) |
|---|
| 538 |
((or (equal type "conference") |
|---|
| 539 |
(equal type "incollection") |
|---|
| 540 |
(equal type "inproceedings")) |
|---|
| 541 |
(reftex-get-bib-field "booktitle" entry "in: %s")) |
|---|
| 542 |
(t "")))) |
|---|
| 543 |
(setq authors (reftex-truncate authors 30 t t)) |
|---|
| 544 |
(when (reftex-use-fonts) |
|---|
| 545 |
(put-text-property 0 (length key) 'face |
|---|
| 546 |
(reftex-verified-face reftex-label-face |
|---|
| 547 |
'font-lock-constant-face |
|---|
| 548 |
'font-lock-reference-face) |
|---|
| 549 |
key) |
|---|
| 550 |
(put-text-property 0 (length authors) 'face reftex-bib-author-face |
|---|
| 551 |
authors) |
|---|
| 552 |
(put-text-property 0 (length year) 'face reftex-bib-year-face |
|---|
| 553 |
year) |
|---|
| 554 |
(put-text-property 0 (length title) 'face reftex-bib-title-face |
|---|
| 555 |
title) |
|---|
| 556 |
(put-text-property 0 (length extra) 'face reftex-bib-extra-face |
|---|
| 557 |
extra)) |
|---|
| 558 |
(concat key "\n " authors " " year " " extra "\n " title "\n\n"))) |
|---|
| 559 |
|
|---|
| 560 |
(defun reftex-parse-bibitem (item) |
|---|
| 561 |
|
|---|
| 562 |
(let ((key "") (text "")) |
|---|
| 563 |
(when (string-match "\\`{\\([^}]+\\)}\\([^\000]*\\)" item) |
|---|
| 564 |
(setq key (match-string 1 item) |
|---|
| 565 |
text (match-string 2 item))) |
|---|
| 566 |
|
|---|
| 567 |
(while (string-match "[\n\r\t]\\|[ \t][ \t]+" text) |
|---|
| 568 |
(setq text (replace-match " " nil t text))) |
|---|
| 569 |
(if (string-match "\\`[ \t]+" text) |
|---|
| 570 |
(setq text (replace-match "" nil t text))) |
|---|
| 571 |
(list |
|---|
| 572 |
(cons "&key" key) |
|---|
| 573 |
(cons "&text" text) |
|---|
| 574 |
(cons "&entry" (concat key " " text))))) |
|---|
| 575 |
|
|---|
| 576 |
(defun reftex-format-bibitem (item) |
|---|
| 577 |
|
|---|
| 578 |
(let ((text (reftex-get-bib-field "&text" item)) |
|---|
| 579 |
(key (reftex-get-bib-field "&key" item)) |
|---|
| 580 |
(lines nil)) |
|---|
| 581 |
|
|---|
| 582 |
|
|---|
| 583 |
(while (and (> (length text) 70) |
|---|
| 584 |
(string-match " " (substring text 60))) |
|---|
| 585 |
(push (substring text 0 (+ 60 (match-beginning 0))) lines) |
|---|
| 586 |
(setq text (substring text (+ 61 (match-beginning 0))))) |
|---|
| 587 |
(push text lines) |
|---|
| 588 |
(setq text (mapconcat 'identity (nreverse lines) "\n ")) |
|---|
| 589 |
|
|---|
| 590 |
(when (reftex-use-fonts) |
|---|
| 591 |
(put-text-property 0 (length text) 'face reftex-bib-author-face text)) |
|---|
| 592 |
(concat key "\n " text "\n\n"))) |
|---|
| 593 |
|
|---|
| 594 |
|
|---|
| 595 |
|
|---|
| 596 |
|
|---|
| 597 |
(defun reftex-citation (&optional no-insert format-key) |
|---|
| 598 |
"Make a citation using BibTeX database files. |
|---|
| 599 |
After prompting for a regular expression, scans the buffers with |
|---|
| 600 |
bibtex entries (taken from the \\bibliography command) and offers the |
|---|
| 601 |
matching entries for selection. The selected entry is formatted according |
|---|
| 602 |
to `reftex-cite-format' and inserted into the buffer. |
|---|
| 603 |
|
|---|
| 604 |
If NO-INSERT is non-nil, nothing is inserted, only the selected key returned. |
|---|
| 605 |
|
|---|
| 606 |
FORMAT-KEY can be used to pre-select a citation format. |
|---|
| 607 |
|
|---|
| 608 |
When called with a `C-u' prefix, prompt for optional arguments in |
|---|
| 609 |
cite macros. When called with a numeric prefix, make that many |
|---|
| 610 |
citations. When called with point inside the braces of a `\\cite' |
|---|
| 611 |
command, it will add another key, ignoring the value of |
|---|
| 612 |
`reftex-cite-format'. |
|---|
| 613 |
|
|---|
| 614 |
The regular expression uses an expanded syntax: && is interpreted as `and'. |
|---|
| 615 |
Thus, `aaaa&&bbb' matches entries which contain both `aaaa' and `bbb'. |
|---|
| 616 |
While entering the regexp, completion on knows citation keys is possible. |
|---|
| 617 |
`=' is a good regular expression to match all entries in all files." |
|---|
| 618 |
|
|---|
| 619 |
(interactive) |
|---|
| 620 |
|
|---|
| 621 |
|
|---|
| 622 |
(reftex-check-recursive-edit) |
|---|
| 623 |
|
|---|
| 624 |
|
|---|
| 625 |
|
|---|
| 626 |
|
|---|
| 627 |
(when reftex-mode |
|---|
| 628 |
(reftex-access-scan-info nil)) |
|---|
| 629 |
|
|---|
| 630 |
|
|---|
| 631 |
(unwind-protect |
|---|
| 632 |
(reftex-do-citation current-prefix-arg no-insert format-key) |
|---|
| 633 |
(reftex-kill-temporary-buffers))) |
|---|
| 634 |
|
|---|
| 635 |
(defun reftex-do-citation (&optional arg no-insert format-key) |
|---|
| 636 |
|
|---|
| 637 |
|
|---|
| 638 |
(let* ((format (reftex-figure-out-cite-format arg no-insert format-key)) |
|---|
| 639 |
(docstruct-symbol reftex-docstruct-symbol) |
|---|
| 640 |
(selected-entries (reftex-offer-bib-menu)) |
|---|
| 641 |
(insert-entries selected-entries) |
|---|
| 642 |
entry string cite-view) |
|---|
| 643 |
|
|---|
| 644 |
(when (stringp selected-entries) |
|---|
| 645 |
(error selected-entries)) |
|---|
| 646 |
(unless selected-entries (error "Quit")) |
|---|
| 647 |
|
|---|
| 648 |
(if (stringp selected-entries) |
|---|
| 649 |
|
|---|
| 650 |
(setq selected-entries nil |
|---|
| 651 |
insert-entries (list (list selected-entries |
|---|
| 652 |
(cons "&key" selected-entries)))) |
|---|
| 653 |
|
|---|
| 654 |
(setq cite-view t)) |
|---|
| 655 |
|
|---|
| 656 |
(when (eq (car selected-entries) 'concat) |
|---|
| 657 |
|
|---|
| 658 |
|
|---|
| 659 |
(pop selected-entries) |
|---|
| 660 |
(let ((concat-keys (mapconcat 'car selected-entries ","))) |
|---|
| 661 |
(setq insert-entries |
|---|
| 662 |
(list (list concat-keys (cons "&key" concat-keys)))))) |
|---|
| 663 |
|
|---|
| 664 |
(unless no-insert |
|---|
| 665 |
|
|---|
| 666 |
|
|---|
| 667 |
(message "Formatting...") |
|---|
| 668 |
|
|---|
| 669 |
(while (setq entry (pop insert-entries)) |
|---|
| 670 |
|
|---|
| 671 |
(setq string (if reftex-format-cite-function |
|---|
| 672 |
(funcall reftex-format-cite-function |
|---|
| 673 |
(reftex-get-bib-field "&key" entry) |
|---|
| 674 |
format) |
|---|
| 675 |
(reftex-format-citation entry format))) |
|---|
| 676 |
(when (or (eq reftex-cite-prompt-optional-args t) |
|---|
| 677 |
(and reftex-cite-prompt-optional-args |
|---|
| 678 |
(equal arg '(4)))) |
|---|
| 679 |
(let ((start 0) (nth 0) value) |
|---|
| 680 |
(while (setq start (string-match "\\[\\]" string start)) |
|---|
| 681 |
(setq value (read-string (format "Optional argument %d: " |
|---|
| 682 |
(setq nth (1+ nth))))) |
|---|
| 683 |
(setq string (replace-match (concat "[" value "]") t t string)) |
|---|
| 684 |
(setq start (1+ start))))) |
|---|
| 685 |
|
|---|
| 686 |
|
|---|
| 687 |
|
|---|
| 688 |
|
|---|
| 689 |
(when reftex-cite-cleanup-optional-args |
|---|
| 690 |
(cond |
|---|
| 691 |
((string-match "\\([a-zA-Z0-9]\\)\\[\\]{" string) |
|---|
| 692 |
(setq string (replace-match "\\1{" nil nil string))) |
|---|
| 693 |
((string-match "\\[\\]\\(\\[[a-zA-Z0-9., ]+\\]\\)" string) |
|---|
| 694 |
(setq string (replace-match "\\1" nil nil string))) |
|---|
| 695 |
((string-match "\\[\\]\\[\\]" string) |
|---|
| 696 |
(setq string (replace-match "" t t string))))) |
|---|
| 697 |
(insert string)) |
|---|
| 698 |
|
|---|
| 699 |
|
|---|
| 700 |
(when (string-match "\\?" string) |
|---|
| 701 |
(search-backward "?") |
|---|
| 702 |
(delete-char 1)) |
|---|
| 703 |
|
|---|
| 704 |
|
|---|
| 705 |
(when (and reftex-mode |
|---|
| 706 |
(fboundp 'LaTeX-add-bibitems) |
|---|
| 707 |
reftex-plug-into-AUCTeX) |
|---|
| 708 |
(apply 'LaTeX-add-bibitems (mapcar 'car selected-entries))) |
|---|
| 709 |
|
|---|
| 710 |
|
|---|
| 711 |
(when (and reftex-mode reftex-cache-cite-echo cite-view) |
|---|
| 712 |
(mapcar (lambda (entry) |
|---|
| 713 |
(reftex-make-cite-echo-string entry docstruct-symbol)) |
|---|
| 714 |
selected-entries)) |
|---|
| 715 |
|
|---|
| 716 |
(message "")) |
|---|
| 717 |
|
|---|
| 718 |
(set-marker reftex-select-return-marker nil) |
|---|
| 719 |
(reftex-kill-buffer "*RefTeX Select*") |
|---|
| 720 |
|
|---|
| 721 |
|
|---|
| 722 |
(when (integerp arg) |
|---|
| 723 |
(if (> arg 1) |
|---|
| 724 |
(progn |
|---|
| 725 |
(skip-chars-backward "}") |
|---|
| 726 |
(decf arg) |
|---|
| 727 |
(reftex-do-citation arg)) |
|---|
| 728 |
(forward-char 1))) |
|---|
| 729 |
|
|---|
| 730 |
|
|---|
| 731 |
(car (car selected-entries)))) |
|---|
| 732 |
|
|---|
| 733 |
(defun reftex-figure-out-cite-format (arg &optional no-insert format-key) |
|---|
| 734 |
|
|---|
| 735 |
|
|---|
| 736 |
(let ((macro (car (reftex-what-macro 1))) |
|---|
| 737 |
(cite-format-value (reftex-get-cite-format)) |
|---|
| 738 |
key format) |
|---|
| 739 |
(cond |
|---|
| 740 |
(no-insert |
|---|
| 741 |
|
|---|
| 742 |
(setq format "%l")) |
|---|
| 743 |
|
|---|
| 744 |
((and (stringp macro) |
|---|
| 745 |
(string-match "\\`\\\\cite\\|cite\\'" macro)) |
|---|
| 746 |
|
|---|
| 747 |
(if (or (not arg) (not (listp arg))) |
|---|
| 748 |
(setq format |
|---|
| 749 |
(concat |
|---|
| 750 |
(if (member (preceding-char) '(?\{ ?,)) "" ",") |
|---|
| 751 |
"%l" |
|---|
| 752 |
(if (member (following-char) '(?\} ?,)) "" ","))) |
|---|
| 753 |
(setq format "%l"))) |
|---|
| 754 |
(t |
|---|
| 755 |
|
|---|
| 756 |
(setq format |
|---|
| 757 |
(if (and (symbolp cite-format-value) |
|---|
| 758 |
(assq cite-format-value reftex-cite-format-builtin)) |
|---|
| 759 |
(nth 2 (assq cite-format-value reftex-cite-format-builtin)) |
|---|
| 760 |
cite-format-value)) |
|---|
| 761 |
(when (listp format) |
|---|
| 762 |
(setq key |
|---|
| 763 |
(or format-key |
|---|
| 764 |
(reftex-select-with-char |
|---|
| 765 |
"" (concat "SELECT A CITATION FORMAT\n\n" |
|---|
| 766 |
(mapconcat |
|---|
| 767 |
(lambda (x) |
|---|
| 768 |
(format "[%c] %s %s" (car x) |
|---|
| 769 |
(if (> (car x) 31) " " "") |
|---|
| 770 |
(cdr x))) |
|---|
| 771 |
format "\n"))))) |
|---|
| 772 |
(if (assq key format) |
|---|
| 773 |
(setq format (cdr (assq key format))) |
|---|
| 774 |
(error "No citation format associated with key `%c'" key))))) |
|---|
| 775 |
format)) |
|---|
| 776 |
|
|---|
| 777 |
(defun reftex-citep () |
|---|
| 778 |
"Call `reftex-citation' with a format selector `?p'." |
|---|
|
|---|