* lisp/org-bibtex.el: Add crossref option to incollection
[org-mode.git] / lisp / org-bibtex.el
blob2eefd023b82983a1acf9f75efc9f9dd4fb8bb14b
1 ;;; org-bibtex.el --- Org links to BibTeX entries
2 ;;
3 ;; Copyright (C) 2007, 2008, 2009, 2010, 2011 Free Software Foundation, Inc.
4 ;;
5 ;; Author: Bastien Guerry <bzg at altern dot org>
6 ;; Carsten Dominik <carsten dot dominik at gmail dot com>
7 ;; Eric Schulte <schulte dot eric at gmail dot com>
8 ;; Keywords: org, wp, remember
9 ;; Version: 7.5
11 ;; This file is part of GNU Emacs.
13 ;; GNU Emacs is free software: you can redistribute it and/or modify
14 ;; it under the terms of the GNU General Public License as published by
15 ;; the Free Software Foundation, either version 3 of the License, or
16 ;; (at your option) any later version.
18 ;; GNU Emacs is distributed in the hope that it will be useful,
19 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
20 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 ;; GNU General Public License for more details.
23 ;; You should have received a copy of the GNU General Public License
24 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
26 ;;; Commentary:
28 ;; This file implements links to database entries in BibTeX files.
29 ;; Instead of defining a special link prefix, it uses the normal file
30 ;; links combined with a custom search mechanism to find entries
31 ;; by reference key. And it constructs a nice description tag for
32 ;; the link that contains the author name, the year and a short title.
34 ;; It also stores detailed information about the entry so that
35 ;; remember templates can access and enter this information easily.
37 ;; The available properties for each entry are listed here:
39 ;; :author :publisher :volume :pages
40 ;; :editor :url :number :journal
41 ;; :title :year :series :address
42 ;; :booktitle :month :annote :abstract
43 ;; :key :btype
45 ;; Here is an example of a remember template that use some of this
46 ;; information (:author :year :title :journal :pages):
48 ;; (setq org-remember-templates
49 ;; '((?b "* READ %?\n\n%a\n\n%:author (%:year): %:title\n \
50 ;; In %:journal, %:pages.")))
52 ;; Let's say you want to remember this BibTeX entry:
54 ;; @Article{dolev83,
55 ;; author = {Danny Dolev and Andrew C. Yao},
56 ;; title = {On the security of public-key protocols},
57 ;; journal = {IEEE Transaction on Information Theory},
58 ;; year = 1983,
59 ;; volume = 2,
60 ;; number = 29,
61 ;; pages = {198--208},
62 ;; month = {Mars}
63 ;; }
65 ;; M-x `org-remember' on this entry will produce this buffer:
67 ;; =====================================================================
68 ;; * READ <== [point here]
70 ;; [[file:file.bib::dolev83][Dolev & Yao 1983: security of public key protocols]]
72 ;; Danny Dolev and Andrew C. Yao (1983): On the security of public-key protocols
73 ;; In IEEE Transaction on Information Theory, 198--208.
74 ;; =====================================================================
76 ;; Additionally, the following functions are now available for storing
77 ;; bibtex entries within Org-mode documents.
79 ;; - Run `org-bibtex' to export the current file to a .bib.
81 ;; - Run `org-bibtex-check' or `org-bibtex-check-all' to check and
82 ;; fill in missing field of either the current, or all headlines
84 ;; - Run `org-bibtex-create' to add a bibtex entry
86 ;; - Use `org-bibtex-read' to read a bibtex entry after `point' or in
87 ;; the active region, then call `org-bibtex-write' in a .org file to
88 ;; insert a heading for the read bibtex entry
90 ;; - All Bibtex information is taken from the document compiled by
91 ;; Andrew Roberts from the Bibtex manual, available at
92 ;; http://www.andy-roberts.net/misc/latex/sessions/bibtex/bibentries.pdf
94 ;;; History:
96 ;; The link creation part has been part of Org-mode for a long time.
98 ;; Creating better remember template information was inspired by a request
99 ;; of Austin Frank: http://article.gmane.org/gmane.emacs.orgmode/4112
100 ;; and then implemented by Bastien Guerry.
102 ;; Eric Schulte eventually added the functions for translating between
103 ;; Org-mode headlines and Bibtex entries, and for fleshing out the Bibtex
104 ;; fields of existing Org-mode headlines.
106 ;; Org-mode loads this module by default - if this is not what you want,
107 ;; configure the variable `org-modules'.
109 ;;; Code:
111 (require 'org)
112 (require 'bibtex)
113 (eval-when-compile
114 (require 'cl))
116 (defvar description nil) ; dynamically scoped from org.el
118 (declare-function bibtex-beginning-of-entry "bibtex" ())
119 (declare-function bibtex-generate-autokey "bibtex" ())
120 (declare-function bibtex-parse-entry "bibtex" (&optional content))
121 (declare-function bibtex-url "bibtex" (&optional pos no-browse))
122 (declare-function longlines-mode "longlines" (&optional arg))
125 ;;; Bibtex data
126 (defvar org-bibtex-types
127 '((:article
128 (:description . "An article from a journal or magazine")
129 (:required :author :title :journal :year)
130 (:optional :volume :number :pages :month :note))
131 (:book
132 (:description . "A book with an explicit publisher")
133 (:required (:editor :author) :title :publisher :year)
134 (:optional (:volume :number) :series :address :edition :month :note))
135 (:booklet
136 (:description . "A work that is printed and bound, but without a named publisher or sponsoring institution.")
137 (:required :title)
138 (:optional :author :howpublished :address :month :year :note))
139 (:conference
140 (:description . "")
141 (:required :author :title :booktitle :year)
142 (:optional :editor :pages :organization :publisher :address :month :note))
143 (:inbook
144 (:description . "A part of a book, which may be a chapter (or section or whatever) and/or a range of pages.")
145 (:required (:author :editor) :title (:chapter :pages) :publisher :year)
146 (:optional (:volume :number) :series :type :address :edition :month :note))
147 (:incollection
148 (:description . "A part of a book having its own title.")
149 (:required :author :title :booktitle :publisher :year)
150 (:optional :crossref :editor (:volume :number) :series :type :chapter :pages :address :edition :month :note))
151 (:inproceedings
152 (:description . "An article in a conference proceedings")
153 (:required :author :title :booktitle :year)
154 (:optional :editor (:volume :number) :series :pages :address :month :organization :publisher :note))
155 (:manual
156 (:description . "Technical documentation.")
157 (:required :title)
158 (:optional :author :organization :address :edition :month :year :note))
159 (:mastersthesis
160 (:description . "A Master’s thesis.")
161 (:required :author :title :school :year)
162 (:optional :type :address :month :note))
163 (:misc
164 (:description . "Use this type when nothing else fits.")
165 (:required)
166 (:optional :author :title :howpublished :month :year :note))
167 (:phdthesis
168 (:description . "A PhD thesis.")
169 (:required :author :title :school :year)
170 (:optional :type :address :month :note))
171 (:proceedings
172 (:description . "The proceedings of a conference.")
173 (:required :title :year)
174 (:optional :editor (:volume :number) :series :address :month :organization :publisher :note))
175 (:techreport
176 (:description . "A report published by a school or other institution.")
177 (:required :author :title :institution :year)
178 (:optional :type :address :month :note))
179 (:unpublished
180 (:description . "A document having an author and title, but not formally published.")
181 (:required :author :title :note)
182 (:optional :month :year)))
183 "Bibtex entry types with required and optional parameters.")
185 (defvar org-bibtex-fields
186 '((:address . "Usually the address of the publisher or other type of institution. For major publishing houses, van Leunen recommends omitting the information entirely. For small publishers, on the other hand, you can help the reader by giving the complete address.")
187 (:annote . "An annotation. It is not used by the standard bibliography styles, but may be used by others that produce an annotated bibliography.")
188 (:author . "The name(s) of the author(s), in the format described in the LaTeX book. Remember, all names are separated with the and keyword, and not commas.")
189 (:booktitle . "Title of a book, part of which is being cited. See the LaTeX book for how to type titles. For book entries, use the title field instead.")
190 (:chapter . "A chapter (or section or whatever) number.")
191 (:crossref . "The database key of the entry being cross referenced.")
192 (:edition . "The edition of a book for example, 'Second'. This should be an ordinal, and should have the first letter capitalized, as shown here; the standard styles convert to lower case when necessary.")
193 (:editor . "Name(s) of editor(s), typed as indicated in the LaTeX book. If there is also an author field, then the editor field gives the editor of the book or collection in which the reference appears.")
194 (:howpublished . "How something strange has been published. The first word should be capitalized.")
195 (:institution . "The sponsoring institution of a technical report.")
196 (:journal . "A journal name.")
197 (:key . "Used for alphabetizing, cross-referencing, and creating a label when the author information is missing. This field should not be confused with the key that appears in the \cite command and at the beginning of the database entry.")
198 (:month . "The month in which the work was published or, for an unpublished work, in which it was written. You should use the standard three-letter abbreviation,")
199 (:note . "Any additional information that can help the reader. The first word should be capitalized.")
200 (:number . "Any additional information that can help the reader. The first word should be capitalized.")
201 (:organization . "The organization that sponsors a conference or that publishes a manual.")
202 (:pages . "One or more page numbers or range of numbers, such as 42-111 or 7,41,73-97 or 43+ (the ‘+’ in this last example indicates pages following that don’t form simple range). BibTEX requires double dashes for page ranges (--).")
203 (:publisher . "The publisher’s name.")
204 (:school . "The name of the school where a thesis was written.")
205 (:series . "The name of a series or set of books. When citing an entire book, the the title field gives its title and an optional series field gives the name of a series or multi-volume set in which the book is published.")
206 (:title . "The work’s title, typed as explained in the LaTeX book.")
207 (:type . "The type of a technical report for example, 'Research Note'.")
208 (:volume . "The volume of a journal or multi-volume book.")
209 (:year . "The year of publication or, for an unpublished work, the year it was written. Generally it should consist of four numerals, such as 1984, although the standard styles can handle any year whose last four nonpunctuation characters are numerals, such as '(about 1984)'"))
210 "Bibtex fields with descriptions.")
212 (defvar *org-bibtex-entries* nil
213 "List to hold parsed bibtex entries.")
215 (defcustom org-bibtex-autogen-keys nil
216 "Set to a truthy value to use `bibtex-generate-autokey' to generate keys."
217 :group 'org-bibtex
218 :type 'boolean)
220 (defcustom org-bibtex-prefix nil
221 "Optional prefix for all bibtex property names.
222 For example setting to 'BIB_' would allow interoperability with fireforg."
223 :group 'org-bibtex
224 :type 'string)
226 (defcustom org-bibtex-export-arbitrary-fields nil
227 "When converting to bibtex allow fields not defined in `org-bibtex-fields'.
228 This only has effect if org-bibtex-prefix is defined, so as to
229 ensure that other org-properties, such as CATEGORY or LOGGING are
230 not placed in the exported bibtex entry."
231 :group 'org-bibtex
232 :type 'boolean)
234 (defcustom org-bibtex-key-property "CUSTOM_ID"
235 "Property that holds the bibtex key.
236 By default, this is CUSTOM_ID, which enables easy linking to
237 bibtex headlines from within an org file. This can be set to ID
238 to enable global links, but only with great caution, as global
239 IDs must be unique."
240 :group 'org-bibtex
241 :type 'string)
243 (defcustom org-bibtex-tags nil
244 "List of tag(s) that should be added to new bib entries."
245 :group 'org-bibtex
246 :type '(repeat :tag "Tag" (string)))
248 (defcustom org-bibtex-tags-are-keywords nil
249 "Convert the value of the keywords field to tags and vice versa.
250 If set to t, comma-separated entries in a bibtex entry's keywords
251 field will be converted to org tags. Note: spaces will be escaped
252 with underscores, and characters that are not permitted in org
253 tags will be removed.
255 If t, local tags in an org entry will be exported as a
256 comma-separated string of keywords when exported to bibtex. Tags
257 defined in `org-bibtex-tags' or `org-bibtex-no-export-tags' will
258 not be exported."
259 :group 'org-bibtex
260 :type 'boolean)
262 (defcustom org-bibtex-no-export-tags nil
263 "List of tag(s) that should not be converted to keywords.
264 This variable is relevant only if `org-bibtex-export-tags-as-keywords` is t."
265 :group 'org-bibtex
266 :type '(repeat :tag "Tag" (string)))
269 ;;; Utility functions
270 (defun org-bibtex-get (property)
271 (or (org-entry-get (point) (upcase property))
272 (org-entry-get (point) (concat org-bibtex-prefix (upcase property)))))
274 (defun org-bibtex-put (property value)
275 (let ((prop (upcase (if (keywordp property)
276 (substring (symbol-name property) 1)
277 property))))
278 (org-set-property
279 (concat (unless (string= org-bibtex-key-property prop) org-bibtex-prefix)
280 prop)
281 value)))
283 (defun org-bibtex-headline ()
284 "Return a bibtex entry of the given headline as a string."
285 (flet ((val (key lst) (cdr (assoc key lst)))
286 (to (string) (intern (concat ":" string)))
287 (from (key) (substring (symbol-name key) 1))
288 (flatten (&rest lsts)
289 (apply #'append (mapcar
290 (lambda (e)
291 (if (listp e) (apply #'flatten e) (list e)))
292 lsts))))
293 (let ((notes (buffer-string))
294 (id (org-bibtex-get org-bibtex-key-property))
295 (type (org-bibtex-get "type"))
296 (tags (when org-bibtex-tags-are-keywords
297 (delq nil
298 (mapcar
299 (lambda (tag)
300 (unless (member tag
301 (append org-bibtex-tags
302 org-bibtex-no-export-tags))
303 tag))
304 (org-get-local-tags-at))))))
305 (when type
306 (let ((entry (format
307 "@%s{%s,\n%s\n}\n" type id
308 (mapconcat
309 (lambda (pair) (format " %s={%s}" (car pair) (cdr pair)))
310 (remove nil
311 (if (and org-bibtex-export-arbitrary-fields
312 org-bibtex-prefix)
313 (mapcar
314 (lambda (kv)
315 (let ((key (car kv)) (val (cdr kv)))
316 (when (and (string-match org-bibtex-prefix key)
317 (not (equalp
318 (concat org-bibtex-prefix "TYPE") key)))
319 (cons (downcase (replace-regexp-in-string
320 org-bibtex-prefix "" key))
321 val))))
322 (org-entry-properties nil 'standard))
323 (mapcar
324 (lambda (field)
325 (let ((value (or (org-bibtex-get (from field))
326 (and (equal :title field)
327 (org-get-heading)))))
328 (when value (cons (from field) value))))
329 (flatten
330 (val :required (val (to type) org-bibtex-types))
331 (val :optional (val (to type) org-bibtex-types))))))
332 ",\n"))))
333 (with-temp-buffer
334 (insert entry)
335 (when tags
336 (bibtex-beginning-of-entry)
337 (if (re-search-forward "keywords.*=.*{\\(.*\\)}" nil t)
338 (progn (goto-char (match-end 1)) (insert ", "))
339 (bibtex-make-field "keywords" t t))
340 (insert (mapconcat #'identity tags ", ")))
341 (bibtex-reformat) (buffer-string)))))))
343 (defun org-bibtex-ask (field)
344 (unless (assoc field org-bibtex-fields)
345 (error "field:%s is not known" field))
346 (save-window-excursion
347 (let* ((name (substring (symbol-name field) 1))
348 (buf-name (format "*Bibtex Help %s*" name)))
349 (with-output-to-temp-buffer buf-name
350 (princ (cdr (assoc field org-bibtex-fields))))
351 (with-current-buffer buf-name (longlines-mode t))
352 (org-fit-window-to-buffer (get-buffer-window buf-name))
353 ((lambda (result) (when (> (length result) 0) result))
354 (read-from-minibuffer (format "%s: " name))))))
356 (defun org-bibtex-autokey ()
357 "Generate an autokey for the current headline"
358 (org-bibtex-put org-bibtex-key-property
359 (if org-bibtex-autogen-keys
360 (let* ((entry (org-bibtex-headline))
361 (key
362 (with-temp-buffer
363 (insert entry)
364 (bibtex-generate-autokey))))
365 ;; test for duplicate IDs if using global ID
366 (when (and
367 (equal org-bibtex-key-property "ID")
368 (featurep 'org-id)
369 (hash-table-p org-id-locations)
370 (gethash key org-id-locations))
371 (warn "Another entry has the same ID"))
372 key)
373 (read-from-minibuffer "id: "))))
375 (defun org-bibtex-fleshout (type &optional optional)
376 "Fleshout the current heading, ensuring that all required fields are present.
377 With optional argument OPTIONAL, also prompt for optional fields."
378 (flet ((val (key lst) (cdr (assoc key lst)))
379 (keyword (name) (intern (concat ":" (downcase name))))
380 (name (keyword) (upcase (substring (symbol-name keyword) 1))))
381 (dolist (field (append
382 (remove :title (val :required (val type org-bibtex-types)))
383 (when optional (val :optional (val type org-bibtex-types)))))
384 (when (consp field) ; or'd pair of fields e.g., (:editor :author)
385 (let ((present (first (remove nil
386 (mapcar
387 (lambda (f) (when (org-bibtex-get (name f)) f))
388 field)))))
389 (setf field (or present (keyword (org-icompleting-read
390 "Field: " (mapcar #'name field)))))))
391 (let ((name (name field)))
392 (unless (org-bibtex-get name)
393 (let ((prop (org-bibtex-ask field)))
394 (when prop (org-bibtex-put name prop)))))))
395 (when (and type (assoc type org-bibtex-types)
396 (not (org-bibtex-get org-bibtex-key-property)))
397 (org-bibtex-autokey)))
400 ;;; Bibtex link functions
401 (org-add-link-type "bibtex" 'org-bibtex-open)
402 (add-hook 'org-store-link-functions 'org-bibtex-store-link)
404 (defun org-bibtex-open (path)
405 "Visit the bibliography entry on PATH."
406 (let* ((search (when (string-match "::\\(.+\\)\\'" path)
407 (match-string 1 path)))
408 (path (substring path 0 (match-beginning 0))))
409 (org-open-file path t nil search)))
411 (defun org-bibtex-store-link ()
412 "Store a link to a BibTeX entry."
413 (when (eq major-mode 'bibtex-mode)
414 (let* ((search (org-create-file-search-in-bibtex))
415 (link (concat "file:" (abbreviate-file-name buffer-file-name)
416 "::" search))
417 (entry (mapcar ; repair strings enclosed in "..." or {...}
418 (lambda(c)
419 (if (string-match
420 "^\\(?:{\\|\"\\)\\(.*\\)\\(?:}\\|\"\\)$" (cdr c))
421 (cons (car c) (match-string 1 (cdr c))) c))
422 (save-excursion
423 (bibtex-beginning-of-entry)
424 (bibtex-parse-entry)))))
425 (org-store-link-props
426 :key (cdr (assoc "=key=" entry))
427 :author (or (cdr (assoc "author" entry)) "[no author]")
428 :editor (or (cdr (assoc "editor" entry)) "[no editor]")
429 :title (or (cdr (assoc "title" entry)) "[no title]")
430 :booktitle (or (cdr (assoc "booktitle" entry)) "[no booktitle]")
431 :journal (or (cdr (assoc "journal" entry)) "[no journal]")
432 :publisher (or (cdr (assoc "publisher" entry)) "[no publisher]")
433 :pages (or (cdr (assoc "pages" entry)) "[no pages]")
434 :url (or (cdr (assoc "url" entry)) "[no url]")
435 :year (or (cdr (assoc "year" entry)) "[no year]")
436 :month (or (cdr (assoc "month" entry)) "[no month]")
437 :address (or (cdr (assoc "address" entry)) "[no address]")
438 :volume (or (cdr (assoc "volume" entry)) "[no volume]")
439 :number (or (cdr (assoc "number" entry)) "[no number]")
440 :annote (or (cdr (assoc "annote" entry)) "[no annotation]")
441 :series (or (cdr (assoc "series" entry)) "[no series]")
442 :abstract (or (cdr (assoc "abstract" entry)) "[no abstract]")
443 :btype (or (cdr (assoc "=type=" entry)) "[no type]")
444 :type "bibtex"
445 :link link
446 :description description))))
448 (defun org-create-file-search-in-bibtex ()
449 "Create the search string and description for a BibTeX database entry."
450 ;; Make a good description for this entry, using names, year and the title
451 ;; Put it into the `description' variable which is dynamically scoped.
452 (let ((bibtex-autokey-names 1)
453 (bibtex-autokey-names-stretch 1)
454 (bibtex-autokey-name-case-convert-function 'identity)
455 (bibtex-autokey-name-separator " & ")
456 (bibtex-autokey-additional-names " et al.")
457 (bibtex-autokey-year-length 4)
458 (bibtex-autokey-name-year-separator " ")
459 (bibtex-autokey-titlewords 3)
460 (bibtex-autokey-titleword-separator " ")
461 (bibtex-autokey-titleword-case-convert-function 'identity)
462 (bibtex-autokey-titleword-length 'infty)
463 (bibtex-autokey-year-title-separator ": "))
464 (setq description (bibtex-generate-autokey)))
465 ;; Now parse the entry, get the key and return it.
466 (save-excursion
467 (bibtex-beginning-of-entry)
468 (cdr (assoc "=key=" (bibtex-parse-entry)))))
470 (defun org-execute-file-search-in-bibtex (s)
471 "Find the link search string S as a key for a database entry."
472 (when (eq major-mode 'bibtex-mode)
473 ;; Yes, we want to do the search in this file.
474 ;; We construct a regexp that searches for "@entrytype{" followed by the key
475 (goto-char (point-min))
476 (and (re-search-forward (concat "@[a-zA-Z]+[ \t\n]*{[ \t\n]*"
477 (regexp-quote s) "[ \t\n]*,") nil t)
478 (goto-char (match-beginning 0)))
479 (if (and (match-beginning 0) (equal current-prefix-arg '(16)))
480 ;; Use double prefix to indicate that any web link should be browsed
481 (let ((b (current-buffer)) (p (point)))
482 ;; Restore the window configuration because we just use the web link
483 (set-window-configuration org-window-config-before-follow-link)
484 (with-current-buffer b
485 (goto-char p)
486 (bibtex-url)))
487 (recenter 0)) ; Move entry start to beginning of window
488 ;; return t to indicate that the search is done.
491 ;; Finally add the link search function to the right hook.
492 (add-hook 'org-execute-file-search-functions 'org-execute-file-search-in-bibtex)
495 ;;; Bibtex <-> Org-mode headline translation functions
496 (defun org-bibtex (&optional filename)
497 "Export each headline in the current file to a bibtex entry.
498 Headlines are exported using `org-bibtex-export-headline'."
499 (interactive
500 (list (read-file-name
501 "Bibtex file: " nil nil nil
502 (file-name-nondirectory
503 (concat (file-name-sans-extension (buffer-file-name)) ".bib")))))
504 (let ((bibtex-entries (remove nil (org-map-entries #'org-bibtex-headline))))
505 (with-temp-file filename
506 (insert (mapconcat #'identity bibtex-entries "\n")))))
508 (defun org-bibtex-check (&optional optional)
509 "Check the current headline for required fields.
510 With prefix argument OPTIONAL also prompt for optional fields."
511 (interactive "P")
512 (save-restriction
513 (org-narrow-to-subtree)
514 (let ((type ((lambda (name) (when name (intern (concat ":" name))))
515 (org-bibtex-get "TYPE"))))
516 (when type (org-bibtex-fleshout type optional)))))
518 (defun org-bibtex-check-all (&optional optional)
519 "Check all headlines in the current file.
520 With prefix argument OPTIONAL also prompt for optional fields."
521 (interactive) (org-map-entries (lambda () (org-bibtex-check optional))))
523 (defun org-bibtex-create (&optional arg)
524 "Create a new entry at the given level."
525 (interactive "P")
526 (let* ((type (org-icompleting-read
527 "Type: " (mapcar (lambda (type) (symbol-name (car type)))
528 org-bibtex-types)))
529 (type (if (keywordp type) type (intern type))))
530 (unless (assoc type org-bibtex-types)
531 (error "type:%s is not known" type))
532 (org-insert-heading)
533 (let ((title (org-bibtex-ask :title)))
534 (insert title) (org-bibtex-put "TITLE" title))
535 (org-bibtex-put "TYPE" (substring (symbol-name type) 1))
536 (org-bibtex-fleshout type arg)
537 (mapc (lambda (tag) (org-toggle-tag tag t)) org-bibtex-tags)))
539 (defun org-bibtex-read ()
540 "Read a bibtex entry and save to `*org-bibtex-entries*'.
541 This uses `bibtex-parse-entry'."
542 (interactive)
543 (flet ((keyword (str) (intern (concat ":" (downcase str))))
544 (clean-space (str) (replace-regexp-in-string
545 "[[:space:]\n\r]+" " " str))
546 (strip-delim (str) ; strip enclosing "..." and {...}
547 (dolist (pair '((34 . 34) (123 . 125) (123 . 125)))
548 (when (and (= (aref str 0) (car pair))
549 (= (aref str (1- (length str))) (cdr pair)))
550 (setf str (substring str 1 (1- (length str)))))) str))
551 (push (mapcar
552 (lambda (pair)
553 (cons (let ((field (keyword (car pair))))
554 (case field
555 (:=type= :type)
556 (:=key= :key)
557 (otherwise field)))
558 (clean-space (strip-delim (cdr pair)))))
559 (save-excursion (bibtex-beginning-of-entry) (bibtex-parse-entry)))
560 *org-bibtex-entries*)))
562 (defun org-bibtex-write ()
563 "Insert a heading built from the first element of `*org-bibtex-entries*'."
564 (interactive)
565 (when (= (length *org-bibtex-entries*) 0)
566 (error "No entries in `*org-bibtex-entries*'."))
567 (let ((entry (pop *org-bibtex-entries*))
568 (org-special-properties nil)) ; avoids errors with `org-entry-put'
569 (flet ((val (field) (cdr (assoc field entry)))
570 (togtag (tag) (org-toggle-tag tag t)))
571 (org-insert-heading)
572 (insert (val :title))
573 (org-bibtex-put "TITLE" (val :title))
574 (org-bibtex-put "TYPE" (downcase (val :type)))
575 (dolist (pair entry)
576 (case (car pair)
577 (:title nil)
578 (:type nil)
579 (:key (org-bibtex-put org-bibtex-key-property (cdr pair)))
580 (:keywords (if org-bibtex-tags-are-keywords
581 (mapc
582 (lambda (kw)
583 (togtag
584 (replace-regexp-in-string
585 "[^[:alnum:]_@#%]" ""
586 (replace-regexp-in-string "[ \t]+" "_" kw))))
587 (split-string (cdr pair) ", *"))
588 (org-bibtex-put (car pair) (cdr pair))))
589 (otherwise (org-bibtex-put (car pair) (cdr pair)))))
590 (mapc #'togtag org-bibtex-tags))))
592 (defun org-bibtex-yank ()
593 "If kill ring holds a bibtex entry yank it as an Org-mode headline."
594 (interactive)
595 (let (entry)
596 (with-temp-buffer (yank 1) (setf entry (org-bibtex-read)))
597 (if entry
598 (org-bibtex-write)
599 (error "yanked text does not appear to contain a bibtex entry"))))
601 (provide 'org-bibtex)
603 ;; arch-tag: 83987d5a-01b8-41c7-85bc-77700f1285f5
605 ;;; org-bibtex.el ends here