Get the angular brackets right.
[org-mode.git] / CONTRIB / org-bibtex.el
blob2584d159dfb885d3d09337d173cab6c5672df51f
1 ;;; org-bibtex.el --- Org links to BibTeX entries
2 ;;
3 ;; Copyright 2007 Bastien Guerry
4 ;;
5 ;; Author: bzg AT altern DOT org
6 ;; Version: 0.2
7 ;; Keywords: org, wp, remember
8 ;; URL: http://www.cognition.ens.fr/~guerry/u/org-bibtex.el
9 ;;
10 ;; This program is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 3, or (at your option)
13 ;; any later version.
15 ;; This program is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with this program; if not, write to the Free Software
22 ;; Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 ;;; Commentary:
26 ;; The Org mode already lets you store/insert links to BibTeX entries.
28 ;; But what if you want to insert the author or the title of a BibTeX
29 ;; item in a *remember* buffer? This library lets you deal with this
30 ;; by adding more properties to the BibTeX link.
32 ;; The available properties for each entry are listed here:
34 ;; :author :publisher :volume :pages
35 ;; :editor :url :number :journal
36 ;; :title :year :series :address
37 ;; :booktitle :month :annote :abstract
38 ;; :key :btype
39 ;;
40 ;; Here is an example of a remember template that use some of this
41 ;; information (:author :year :title :journal :pages):
42 ;;
43 ;; (setq org-remember-templates
44 ;; '((?b "* READ %?\n\n%a\n\n%:author (%:year): %:title\n \
45 ;; In %:journal, %:pages.")))
46 ;;
47 ;; Let's say you want to remember this BibTeX entry:
48 ;;
49 ;; @Article{dolev83,
50 ;; author = {Danny Dolev and Andrew C. Yao},
51 ;; title = {On the security of public-key protocols},
52 ;; journal = {IEEE Transaction on Information Theory},
53 ;; year = 1983,
54 ;; volume = 2,
55 ;; number = 29,
56 ;; pages = {198--208},
57 ;; month = {Mars}
58 ;; }
59 ;;
60 ;; M-x `org-remember' on this entry will produce this buffer:
61 ;;
62 ;; =====================================================================
63 ;; * READ <== [point here]
64 ;;
65 ;; [[file:/file.bib::dolev83][Dolev & Yao 1983: security of public key protocols]]
66 ;;
67 ;; Danny Dolev and Andrew C. Yao (1983): On the security of public-key protocols
68 ;; In IEEE Transaction on Information Theory, 198--208.
69 ;; =====================================================================
71 ;;; History:
72 ;;
73 ;; This piece of code was inspired by a request of Austin Frank:
74 ;; http://article.gmane.org/gmane.emacs.orgmode/4112
76 ;; Put this file into your load-path and the following into your ~/.emacs:
77 ;; (require 'org-bibtex)
79 ;;; Code:
81 (provide 'org-bibtex)
83 (require 'org)
85 (defvar description nil) ; dynamically scoped in org.el
87 (org-add-link-type "bibtex" 'org-bibtex-open)
88 (add-hook 'org-store-link-functions 'org-bibtex-store-link)
90 ;; (defun org-bibtex-publish (path)
91 ;; "Build the description of the BibTeX entry for publishing."
92 ;; (let* ((search (when (string-match "::\\(.+\\)\\'" path)
93 ;; (match-string 1 path)))
94 ;; (path (substring path 0 (match-beginning 0)))
95 ;; key)
96 ;; (with-temp-buffer
97 ;; (org-open-file path t nil search)
98 ;; (setq key (org-create-file-search-functions)))
99 ;; (or description key)))
101 (defun org-bibtex-open (path)
102 "Visit the bibliography entry on PATH."
103 (let* ((search (when (string-match "::\\(.+\\)\\'" path)
104 (match-string 1 path)))
105 (path (substring path 0 (match-beginning 0))))
106 (org-open-file path t nil search)))
108 (defun org-bibtex-store-link ()
109 "Store a link to a BibTeX entry."
110 (when (eq major-mode 'bibtex-mode)
111 (let* ((search (run-hook-with-args-until-success
112 'org-create-file-search-functions))
113 (link (concat "file:" (abbreviate-file-name buffer-file-name)
114 "::" search))
115 (entry (mapcar ; repair strings enclosed in "..." or {...}
116 (lambda(c)
117 (if (string-match
118 "^\\(?:{\\|\"\\)\\(.*\\)\\(?:}\\|\"\\)$" (cdr c))
119 (cons (car c) (match-string 1 (cdr c))) c))
120 (save-excursion
121 (bibtex-beginning-of-entry)
122 (bibtex-parse-entry)))))
123 (org-store-link-props
124 :key (cdr (assoc "=key=" entry))
125 :author (or (cdr (assoc "author" entry)) "[no author]")
126 :editor (or (cdr (assoc "editor" entry)) "[no editor]")
127 :title (or (cdr (assoc "title" entry)) "[no title]")
128 :booktitle (or (cdr (assoc "booktitle" entry)) "[no booktitle]")
129 :journal (or (cdr (assoc "journal" entry)) "[no journal]")
130 :publisher (or (cdr (assoc "publisher" entry)) "[no publisher]")
131 :pages (or (cdr (assoc "pages" entry)) "[no pages]")
132 :url (or (cdr (assoc "url" entry)) "[no url]")
133 :year (or (cdr (assoc "year" entry)) "[no year]")
134 :month (or (cdr (assoc "month" entry)) "[no month]")
135 :address (or (cdr (assoc "address" entry)) "[no address]")
136 :volume (or (cdr (assoc "volume" entry)) "[no volume]")
137 :number (or (cdr (assoc "number" entry)) "[no number]")
138 :annote (or (cdr (assoc "annote" entry)) "[no annotation]")
139 :series (or (cdr (assoc "series" entry)) "[no series]")
140 :abstract (or (cdr (assoc "abstract" entry)) "[no abstract]")
141 :btype (or (cdr (assoc "=type=" entry)) "[no type]")
142 :type "bibtex"
143 :link link
144 :description description))))
146 (provide 'org-bibtex)
149 ;;;;##########################################################################
150 ;;;; User Options, Variables
151 ;;;;##########################################################################
154 ;;; org-bibtex.el ends here