org-bibtex.el 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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.
  14. ;;
  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.
  19. ;;
  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.
  23. ;;
  24. ;;; Commentary:
  25. ;;
  26. ;; The Org mode already lets you store/insert links to BibTeX entries.
  27. ;;
  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.
  31. ;;
  32. ;; The available properties for each entry are listed here:
  33. ;;
  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. ;; =====================================================================
  70. ;;
  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
  75. ;;
  76. ;; Put this file into your load-path and the following into your ~/.emacs:
  77. ;; (require 'org-bibtex)
  78. ;;; Code:
  79. (provide 'org-bibtex)
  80. (require 'org)
  81. (defvar description nil) ; dynamically scoped in org.el
  82. (org-add-link-type "bibtex" 'org-bibtex-open)
  83. (add-hook 'org-store-link-functions 'org-bibtex-store-link)
  84. ;; (defun org-bibtex-publish (path)
  85. ;; "Build the description of the BibTeX entry for publishing."
  86. ;; (let* ((search (when (string-match "::\\(.+\\)\\'" path)
  87. ;; (match-string 1 path)))
  88. ;; (path (substring path 0 (match-beginning 0)))
  89. ;; key)
  90. ;; (with-temp-buffer
  91. ;; (org-open-file path t nil search)
  92. ;; (setq key (org-create-file-search-functions)))
  93. ;; (or description key)))
  94. (defun org-bibtex-open (path)
  95. "Visit the bibliography entry on PATH."
  96. (let* ((search (when (string-match "::\\(.+\\)\\'" path)
  97. (match-string 1 path)))
  98. (path (substring path 0 (match-beginning 0))))
  99. (org-open-file path t nil search)))
  100. (defun org-bibtex-store-link ()
  101. "Store a link to a BibTeX entry."
  102. (when (eq major-mode 'bibtex-mode)
  103. (let* ((search (run-hook-with-args-until-success
  104. 'org-create-file-search-functions))
  105. (link (concat "file:" (abbreviate-file-name buffer-file-name)
  106. "::" search))
  107. (entry (mapcar ; repair strings enclosed in "..." or {...}
  108. (lambda(c)
  109. (if (string-match
  110. "^\\(?:{\\|\"\\)\\(.*\\)\\(?:}\\|\"\\)$" (cdr c))
  111. (cons (car c) (match-string 1 (cdr c))) c))
  112. (save-excursion
  113. (bibtex-beginning-of-entry)
  114. (bibtex-parse-entry)))))
  115. (org-store-link-props
  116. :key (cdr (assoc "=key=" entry))
  117. :author (or (cdr (assoc "author" entry)) "[no author]")
  118. :editor (or (cdr (assoc "editor" entry)) "[no editor]")
  119. :title (or (cdr (assoc "title" entry)) "[no title]")
  120. :booktitle (or (cdr (assoc "booktitle" entry)) "[no booktitle]")
  121. :journal (or (cdr (assoc "journal" entry)) "[no journal]")
  122. :publisher (or (cdr (assoc "publisher" entry)) "[no publisher]")
  123. :pages (or (cdr (assoc "pages" entry)) "[no pages]")
  124. :url (or (cdr (assoc "url" entry)) "[no url]")
  125. :year (or (cdr (assoc "year" entry)) "[no year]")
  126. :month (or (cdr (assoc "month" entry)) "[no month]")
  127. :address (or (cdr (assoc "address" entry)) "[no address]")
  128. :volume (or (cdr (assoc "volume" entry)) "[no volume]")
  129. :number (or (cdr (assoc "number" entry)) "[no number]")
  130. :annote (or (cdr (assoc "annote" entry)) "[no annotation]")
  131. :series (or (cdr (assoc "series" entry)) "[no series]")
  132. :abstract (or (cdr (assoc "abstract" entry)) "[no abstract]")
  133. :btype (or (cdr (assoc "=type=" entry)) "[no type]")
  134. :type "bibtex"
  135. :link link
  136. :description description))))
  137. (provide 'org-bibtex)
  138. ;;;;##########################################################################
  139. ;;;; User Options, Variables
  140. ;;;;##########################################################################
  141. ;;; org-bibtex.el ends here