dotemacs

My Emacs configuration
git clone git://git.entf.net/dotemacs
Log | Files | Refs | LICENSE

ol-bibtex.el (33923B)


      1 ;;; ol-bibtex.el --- Links to BibTeX entries        -*- lexical-binding: t; -*-
      2 ;;
      3 ;; Copyright (C) 2007-2023 Free Software Foundation, Inc.
      4 ;;
      5 ;; Authors: Bastien Guerry <bzg@gnu.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, capture
      9 ;;
     10 ;; This file is part of GNU Emacs.
     11 ;;
     12 ;; GNU Emacs is free software: you can redistribute it and/or modify
     13 ;; it under the terms of the GNU General Public License as published by
     14 ;; the Free Software Foundation, either version 3 of the License, or
     15 ;; (at your option) any later version.
     16 
     17 ;; GNU Emacs is distributed in the hope that it will be useful,
     18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
     19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     20 ;; GNU General Public License for more details.
     21 
     22 ;; You should have received a copy of the GNU General Public License
     23 ;; along with GNU Emacs.  If not, see <https://www.gnu.org/licenses/>.
     24 ;;
     25 ;;; Commentary:
     26 ;;
     27 ;; This file implements links to database entries in BibTeX files.
     28 ;; Instead of defining a special link prefix, it uses the normal file
     29 ;; links combined with a custom search mechanism to find entries
     30 ;; by reference key.  And it constructs a nice description tag for
     31 ;; the link that contains the author name, the year and a short title.
     32 ;;
     33 ;; It also stores detailed information about the entry so that
     34 ;; capture templates can access and enter this information easily.
     35 ;;
     36 ;; The available properties for each entry are listed here:
     37 ;;
     38 ;; :author        :publisher      :volume      :pages
     39 ;; :editor        :url            :number      :journal
     40 ;; :title         :year           :series      :address
     41 ;; :booktitle     :month          :annote      :abstract
     42 ;; :key           :btype
     43 ;;
     44 ;; Here is an example of a capture template that use some of this
     45 ;; information (:author :year :title :journal :pages):
     46 ;;
     47 ;; (setq org-capture-templates
     48 ;;   '((?b "* READ %?\n\n%a\n\n%:author (%:year): %:title\n   \
     49 ;;          In %:journal, %:pages.")))
     50 ;;
     51 ;; Let's say you want to capture this BibTeX entry:
     52 ;;
     53 ;; @Article{dolev83,
     54 ;;   author = 	 {Danny Dolev and Andrew C. Yao},
     55 ;;   title = 	 {On the security of public-key protocols},
     56 ;;   journal = 	 {IEEE Transaction on Information Theory},
     57 ;;   year = 	 1983,
     58 ;;   volume =	 2,
     59 ;;   number =	 29,
     60 ;;   pages =	 {198--208},
     61 ;;   month =	 {Mars}
     62 ;; }
     63 ;;
     64 ;; M-x `org-capture' on this entry will produce this buffer:
     65 ;;
     66 ;; =====================================================================
     67 ;; * READ <== [point here]
     68 ;;
     69 ;; [[file:file.bib::dolev83][Dolev & Yao 1983: security of public key protocols]]
     70 ;;
     71 ;; Danny Dolev and Andrew C. Yao (1983): On the security of public-key protocols
     72 ;; In IEEE Transaction on Information Theory, 198--208.
     73 ;; =====================================================================
     74 ;;
     75 ;; Additionally, the following functions are now available for storing
     76 ;; bibtex entries within Org documents.
     77 ;;
     78 ;; - Run `org-bibtex' to export the current file to a .bib.
     79 ;;
     80 ;; - Run `org-bibtex-check' or `org-bibtex-check-all' to check and
     81 ;;   fill in missing field of either the current, or all headlines
     82 ;;
     83 ;; - Run `org-bibtex-create' to add a bibtex entry
     84 ;;
     85 ;; - Use `org-bibtex-read' to read a bibtex entry after `point' or in
     86 ;;   the active region, then call `org-bibtex-write' in a .org file to
     87 ;;   insert a heading for the read bibtex entry
     88 ;;
     89 ;; - All Bibtex information is taken from the document compiled by
     90 ;;   Andrew Roberts from the Bibtex manual, available at
     91 ;;   https://www.andy-roberts.net/res/writing/latex/bibentries.pdf
     92 ;;
     93 ;;; History:
     94 ;;
     95 ;; The link creation part has been part of Org for a long time.
     96 ;;
     97 ;; Creating better capture template information was inspired by a request
     98 ;; of Austin Frank: https://orgmode.org/list/m0myu03vbx.fsf@gmail.com
     99 ;; and then implemented by Bastien Guerry.
    100 ;;
    101 ;; Eric Schulte eventually added the functions for translating between
    102 ;; Org headlines and Bibtex entries, and for fleshing out the Bibtex
    103 ;; fields of existing Org headlines.
    104 ;;
    105 ;; Org mode loads this module by default - if this is not what you want,
    106 ;; configure the variable `org-modules'.
    107 
    108 ;;; Code:
    109 
    110 (require 'org-macs)
    111 (org-assert-version)
    112 
    113 (require 'bibtex)
    114 (require 'cl-lib)
    115 (require 'org-compat)
    116 (require 'org-macs)
    117 (require 'ol)
    118 
    119 (defvar org-agenda-overriding-header)
    120 (defvar org-agenda-search-view-always-boolean)
    121 (defvar org-bibtex-description nil)
    122 (defvar org-id-locations)
    123 (defvar org-property-end-re)
    124 (defvar org-special-properties)
    125 (defvar org-window-config-before-follow-link)
    126 
    127 (declare-function bibtex-beginning-of-entry "bibtex" ())
    128 (declare-function bibtex-generate-autokey "bibtex" ())
    129 (declare-function bibtex-parse-entry "bibtex" (&optional content))
    130 (declare-function bibtex-url "bibtex" (&optional pos no-browse))
    131 
    132 (declare-function org-back-to-heading "org" (&optional invisible-ok))
    133 (declare-function org-entry-get "org" (pom property &optional inherit literal-nil))
    134 (declare-function org-entry-properties "org" (&optional pom which))
    135 (declare-function org-get-tags "org" (&optional pos local))
    136 (declare-function org-heading-components "org" ())
    137 (declare-function org-insert-heading "org" (&optional arg invisible-ok top))
    138 (declare-function org-map-entries "org" (func &optional match scope &rest skip))
    139 (declare-function org-narrow-to-subtree "org" (&optional element))
    140 (declare-function org-set-property "org" (property value))
    141 (declare-function org-toggle-tag "org" (tag &optional onoff))
    142 (declare-function org-indent-region "org" (start end))
    143 
    144 (declare-function org-search-view "org-agenda" (&optional todo-only string edit-at))
    145 
    146 
    147 ;;; Bibtex data
    148 (defvar org-bibtex-types
    149   '((:article
    150      (:description . "An article from a journal or magazine")
    151      (:required :author :title :journal :year)
    152      (:optional :volume :number :pages :month :note :doi))
    153     (:book
    154      (:description . "A book with an explicit publisher")
    155      (:required (:editor :author) :title :publisher :year)
    156      (:optional (:volume :number) :series :address :edition :month :note :doi))
    157     (:booklet
    158      (:description . "A work that is printed and bound, but without a named publisher or sponsoring institution.")
    159      (:required :title)
    160      (:optional :author :howpublished :address :month :year :note :doi :url))
    161     (:conference
    162      (:description . "")
    163      (:required :author :title :booktitle :year)
    164      (:optional :editor :pages :organization :publisher :address :month :note :doi :url))
    165     (:inbook
    166      (:description . "A part of a book, which may be a chapter (or section or whatever) and/or a range of pages.")
    167      (:required (:author :editor) :title (:chapter :pages) :publisher :year)
    168      (:optional :crossref (:volume :number) :series :type :address :edition :month :note :doi))
    169     (:incollection
    170      (:description . "A part of a book having its own title.")
    171      (:required :author :title :booktitle :publisher :year)
    172      (:optional :crossref :editor (:volume :number) :series :type :chapter :pages :address :edition :month :note :doi))
    173     (:inproceedings
    174      (:description . "An article in a conference proceedings")
    175      (:required :author :title :booktitle :year)
    176      (:optional :crossref :editor (:volume :number) :series :pages :address :month :organization :publisher :note :doi))
    177     (:manual
    178      (:description . "Technical documentation.")
    179      (:required :title)
    180      (:optional :author :organization :address :edition :month :year :note :doi :url))
    181     (:mastersthesis
    182      (:description . "A Master’s thesis.")
    183      (:required :author :title :school :year)
    184      (:optional :type :address :month :note :doi :url))
    185     (:misc
    186      (:description . "Use this type when nothing else fits.")
    187      (:required)
    188      (:optional :author :title :howpublished :month :year :note :doi :url))
    189     (:phdthesis
    190      (:description . "A PhD thesis.")
    191      (:required :author :title :school :year)
    192      (:optional :type :address :month :note :doi :url))
    193     (:proceedings
    194      (:description . "The proceedings of a conference.")
    195      (:required :title :year)
    196      (:optional :editor (:volume :number) :series :address :month :organization :publisher :note :doi))
    197     (:techreport
    198      (:description . "A report published by a school or other institution.")
    199      (:required :author :title :institution :year)
    200      (:optional :type :address :month :note :doi :url))
    201     (:unpublished
    202      (:description . "A document having an author and title, but not formally published.")
    203      (:required :author :title :note)
    204      (:optional :month :year :doi :url)))
    205   "Bibtex entry types with required and optional parameters.")
    206 
    207 (defvar org-bibtex-fields
    208   '((: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.")
    209     (:annote       . "An annotation.  It is not used by the standard bibliography styles, but may be used by others that produce an annotated bibliography.")
    210     (: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.")
    211     (: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.")
    212     (:chapter      . "A chapter (or section or whatever) number.")
    213     (:crossref     . "The database key of the entry being cross referenced.")
    214     (:doi          . "The digital object identifier.")
    215     (: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.")
    216     (: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.")
    217     (:howpublished . "How something strange has been published.  The first word should be capitalized.")
    218     (:institution  . "The sponsoring institution of a technical report.")
    219     (:journal      . "A journal name.")
    220     (: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.")
    221     (: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,")
    222     (:note         . "Any additional information that can help the reader.  The first word should be capitalized.")
    223     (:number       . "Any additional information that can help the reader.  The first word should be capitalized.")
    224     (:organization . "The organization that sponsors a conference or that publishes a manual.")
    225     (: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 (--).")
    226     (:publisher    . "The publisher’s name.")
    227     (:school       . "The name of the school where a thesis was written.")
    228     (:series       . "The name of a series or set of books.  When citing an entire book, 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.")
    229     (:title        . "The work’s title, typed as explained in the LaTeX book.")
    230     (:type         . "The type of a technical report for example, 'Research Note'.")
    231     (:url          . "Uniform resource locator.")
    232     (:volume       . "The volume of a journal or multi-volume book.")
    233     (: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)'"))
    234   "Bibtex fields with descriptions.")
    235 
    236 (defvar org-bibtex-entries nil
    237   "List to hold parsed bibtex entries.")
    238 
    239 (defgroup org-bibtex nil
    240   "Options for translating between Org headlines and BibTeX entries."
    241   :tag "Org BibTeX"
    242   :group 'org)
    243 
    244 (defcustom org-bibtex-autogen-keys nil
    245   "Set to a truth value to use `bibtex-generate-autokey' to generate keys."
    246   :group 'org-bibtex
    247   :version "24.1"
    248   :type  'boolean)
    249 
    250 (defcustom org-bibtex-prefix nil
    251   "Optional prefix for all bibtex property names.
    252 For example setting to `BIB_' would allow interoperability with fireforg."
    253   :group 'org-bibtex
    254   :version "24.1"
    255   :type  '(choice
    256 	   (const nil)
    257 	   (string)))
    258 
    259 (defcustom org-bibtex-treat-headline-as-title t
    260   "Treat headline text as title if title property is absent.
    261 If an entry is missing a title property, use the headline text as
    262 the property.  If this value is t, `org-bibtex-check' will ignore
    263 a missing title field."
    264   :group 'org-bibtex
    265   :version "24.1"
    266   :type 'boolean)
    267 
    268 (defcustom org-bibtex-headline-format-function
    269   (lambda (entry) (cdr (assq :title entry)))
    270   "Function returning the headline text for `org-bibtex-write'.
    271 It should take a single argument, the bibtex entry (an alist as
    272 returned by `org-bibtex-read').  The default value simply returns
    273 the entry title."
    274   :group 'org-bibtex
    275   :version "26.1"
    276   :package-version '(Org . "9.1")
    277   :type 'function)
    278 
    279 (defcustom org-bibtex-export-arbitrary-fields nil
    280   "When converting to bibtex allow fields not defined in `org-bibtex-fields'.
    281 This only has effect if `org-bibtex-prefix' is defined, so as to
    282 ensure that other org-properties, such as CATEGORY or LOGGING are
    283 not placed in the exported bibtex entry."
    284   :group 'org-bibtex
    285   :version "24.1"
    286   :type 'boolean)
    287 
    288 (defcustom org-bibtex-key-property "CUSTOM_ID"
    289   "Property that holds the bibtex key.
    290 By default, this is CUSTOM_ID, which enables easy linking to
    291 bibtex headlines from within an org file.  This can be set to ID
    292 to enable global links, but only with great caution, as global
    293 IDs must be unique."
    294   :group 'org-bibtex
    295   :version "24.1"
    296   :type 'string)
    297 
    298 (defcustom org-bibtex-tags nil
    299   "List of tag(s) that should be added to new bib entries."
    300   :group 'org-bibtex
    301   :version "24.1"
    302   :type '(repeat  :tag "Tag" (string)))
    303 
    304 (defcustom org-bibtex-tags-are-keywords nil
    305   "Convert the value of the keywords field to tags and vice versa.
    306 
    307 When non-nil, comma-separated entries in a bibtex entry's keywords
    308 field will be converted to Org tags.  Note: spaces will be escaped
    309 with underscores, and characters that are not permitted in Org
    310 tags will be removed.
    311 
    312 When non-nil, local tags in an Org entry will be exported as
    313 a comma-separated string of keywords when exported to bibtex.
    314 If `org-bibtex-inherit-tags' is non-nil, inherited tags will also
    315 be exported as keywords.  Tags defined in `org-bibtex-tags' or
    316 `org-bibtex-no-export-tags' will not be exported."
    317   :group 'org-bibtex
    318   :version "24.1"
    319   :type 'boolean)
    320 
    321 (defcustom org-bibtex-no-export-tags nil
    322   "List of tag(s) that should not be converted to keywords.
    323 This variable is relevant only if `org-bibtex-tags-are-keywords'
    324 is non-nil."
    325   :group 'org-bibtex
    326   :version "24.1"
    327   :type '(repeat :tag "Tag" (string)))
    328 
    329 (defcustom org-bibtex-inherit-tags nil
    330   "Controls whether inherited tags are converted to bibtex keywords.
    331 It is relevant only if `org-bibtex-tags-are-keywords' is non-nil.
    332 Tag inheritance itself is controlled by `org-use-tag-inheritance'
    333 and `org-tags-exclude-from-inheritance'."
    334   :group 'org-bibtex
    335   :version "26.1"
    336   :package-version '(Org . "8.3")
    337   :type 'boolean)
    338 
    339 (defcustom org-bibtex-type-property-name "btype"
    340   "Property in which to store bibtex entry type (e.g., article)."
    341   :group 'org-bibtex
    342   :version "24.1"
    343   :type 'string)
    344 
    345 
    346 ;;; Utility functions
    347 (defun org-bibtex-get (property)
    348   (let ((it (let ((org-special-properties
    349                    (delete "FILE" (copy-sequence org-special-properties))))
    350               (or
    351                (org-entry-get (point) (upcase property))
    352                (org-entry-get (point) (concat org-bibtex-prefix
    353                                               (upcase property)))))))
    354     (when it (org-trim it))))
    355 
    356 (defun org-bibtex-put (property value &optional insert-raw)
    357   "Set PROPERTY of headline at point to VALUE.
    358 The PROPERTY will be prefixed with `org-bibtex-prefix' when necessary.
    359 With non-nil optional argument INSERT-RAW, insert node property string
    360 at point."
    361   (let* ((prop (upcase (if (keywordp property)
    362                            (substring (symbol-name property) 1)
    363                          property)))
    364          (prop (concat (unless (string= org-bibtex-key-property prop)
    365                          org-bibtex-prefix)
    366 	               prop)))
    367     (if insert-raw
    368         (insert (format ":%s: %s\n" prop value))
    369       (org-set-property prop value))))
    370 
    371 (defun org-bibtex-headline ()
    372   "Return a bibtex entry of the given headline as a string."
    373   (letrec ((val (lambda (key lst) (cdr (assoc key lst))))
    374 	   (to (lambda (string) (intern (concat ":" string))))
    375 	   (from (lambda (key) (substring (symbol-name key) 1)))
    376 	   (flatten (lambda (&rest lsts)
    377 		      (apply #'append (mapcar
    378 				       (lambda (e)
    379 					 (if (listp e) (apply flatten e) (list e)))
    380 				       lsts))))
    381 	   (id (org-bibtex-get org-bibtex-key-property))
    382 	   (type (org-bibtex-get org-bibtex-type-property-name))
    383 	   (tags (when org-bibtex-tags-are-keywords
    384 		   (delq nil
    385 			 (mapcar
    386 			  (lambda (tag)
    387 			    (unless (member tag
    388 					    (append org-bibtex-tags
    389 						    org-bibtex-no-export-tags))
    390 			      tag))
    391 			  (if org-bibtex-inherit-tags (org-get-tags)
    392 			    (org-get-tags nil t)))))))
    393     (when type
    394       (let ((entry (format
    395 		    "@%s{%s,\n%s\n}\n" type id
    396 		    (mapconcat
    397 		     (lambda (pair)
    398 		       (format "  %s={%s}" (car pair) (cdr pair)))
    399 		     (remove nil
    400 			     (if (and org-bibtex-export-arbitrary-fields
    401 				      org-bibtex-prefix)
    402 				 (mapcar
    403 				  (lambda (kv)
    404 				    (let ((key (car kv)) (val0 (cdr kv)))
    405 				      (when (and
    406 					     (string-match org-bibtex-prefix key)
    407 					     (not (string=
    408 						   (downcase (concat org-bibtex-prefix
    409 								     org-bibtex-type-property-name))
    410 						   (downcase key))))
    411 					(cons (downcase (replace-regexp-in-string
    412 							 org-bibtex-prefix "" key))
    413 					      val0))))
    414 				  (org-entry-properties nil 'standard))
    415 			       (mapcar
    416 				(lambda (field)
    417 				  (let ((value (or (org-bibtex-get (funcall from field))
    418 						   (and (eq :title field)
    419 							(nth 4 (org-heading-components))))))
    420 				    (when value (cons (funcall from field) value))))
    421 				(funcall flatten
    422 					 (funcall val :required (funcall val (funcall to type) org-bibtex-types))
    423 					 (funcall val :optional (funcall val (funcall to type) org-bibtex-types))))))
    424 		     ",\n"))))
    425 	(with-temp-buffer
    426 	  (insert entry)
    427 	  (when tags
    428 	    (bibtex-beginning-of-entry)
    429 	    (if (re-search-forward "keywords.*=.*{\\(.*\\)}" nil t)
    430 		(progn (goto-char (match-end 1)) (insert ", "))
    431 	      (search-forward ",\n" nil t)
    432 	      (insert "  keywords={},\n")
    433 	      (search-backward "}," nil t))
    434 	    (insert (mapconcat #'identity tags ", ")))
    435 	  (buffer-string))))))
    436 
    437 (defun org-bibtex-ask (field)
    438   (unless (assoc field org-bibtex-fields)
    439     (error "Field:%s is not known" field))
    440   (save-window-excursion
    441     (let* ((name (substring (symbol-name field) 1))
    442 	   (buf-name (format "*Bibtex Help %s*" name)))
    443       (with-output-to-temp-buffer buf-name
    444 	(princ (cdr (assoc field org-bibtex-fields))))
    445       (with-current-buffer buf-name (visual-line-mode 1))
    446       (org-fit-window-to-buffer (get-buffer-window buf-name))
    447       (let ((result (read-from-minibuffer (format "%s: " name))))
    448         (when (> (length result) 0) result)))))
    449 
    450 (defun org-bibtex-autokey ()
    451   "Generate an autokey for the current headline."
    452   (org-bibtex-put org-bibtex-key-property
    453                   (if org-bibtex-autogen-keys
    454                       (let* ((entry (org-bibtex-headline))
    455 			     (key
    456 			      (with-temp-buffer
    457 				(insert entry)
    458 				(bibtex-generate-autokey))))
    459 			;; test for duplicate IDs if using global ID
    460 			(when (and
    461 			       (equal org-bibtex-key-property "ID")
    462 			       (featurep 'org-id)
    463 			       (hash-table-p org-id-locations)
    464 			       (gethash key org-id-locations))
    465 			  (warn "Another entry has the same ID"))
    466 			key)
    467                     (read-from-minibuffer "id: "))))
    468 
    469 (defun org-bibtex-fleshout (type &optional optional)
    470   "Fleshout current heading, ensuring all required fields are present.
    471 With optional argument OPTIONAL, also prompt for optional fields."
    472   (let ((val (lambda (key lst) (cdr (assoc key lst))))
    473 	(keyword (lambda (name) (intern (concat ":" (downcase name)))))
    474 	(name (lambda (keyword) (substring (symbol-name keyword) 1))))
    475     (dolist (field (append
    476 		    (if org-bibtex-treat-headline-as-title
    477 			(remove :title (funcall val :required (funcall val type org-bibtex-types)))
    478 		      (funcall val :required (funcall val type org-bibtex-types)))
    479 		    (when optional (funcall val :optional (funcall val type org-bibtex-types)))))
    480       (when (consp field) ; or'd pair of fields e.g., (:editor :author)
    481         (let ((present (nth 0 (remove
    482 			       nil
    483 			       (mapcar
    484 				(lambda (f)
    485 				  (when (org-bibtex-get (funcall name f)) f))
    486 				field)))))
    487           (setf field (or present (funcall keyword
    488 					   (completing-read
    489 					    "Field: " (mapcar name field)))))))
    490       (let ((name (funcall name field)))
    491         (unless (org-bibtex-get name)
    492           (let ((prop (org-bibtex-ask field)))
    493             (when prop (org-bibtex-put name prop)))))))
    494   (when (and type (assoc type org-bibtex-types)
    495              (not (org-bibtex-get org-bibtex-key-property)))
    496     (org-bibtex-autokey)))
    497 
    498 
    499 ;;; Bibtex link functions
    500 (org-link-set-parameters "bibtex"
    501 			 :follow #'org-bibtex-open
    502 			 :store #'org-bibtex-store-link)
    503 
    504 (defun org-bibtex-open (path arg)
    505   "Visit the bibliography entry on PATH.
    506 ARG, when non-nil, is a universal prefix argument.  See
    507 `org-open-file' for details."
    508   (org-link-open-as-file path arg))
    509 
    510 (defun org-bibtex-store-link ()
    511   "Store a link to a BibTeX entry."
    512   (when (eq major-mode 'bibtex-mode)
    513     (let* ((search (org-create-file-search-in-bibtex))
    514 	   (link (concat "file:" (abbreviate-file-name buffer-file-name)
    515 			 "::" search))
    516 	   (entry (mapcar ; repair strings enclosed in "..." or {...}
    517 		   (lambda(c)
    518 		     (if (string-match
    519 			  "^\\(?:{\\|\"\\)\\(.*\\)\\(?:}\\|\"\\)$" (cdr c))
    520 			 (cons (car c) (match-string 1 (cdr c))) c))
    521 		   (save-excursion
    522 		     (bibtex-beginning-of-entry)
    523 		     (bibtex-parse-entry)))))
    524       (org-link-store-props
    525        :key (cdr (assoc "=key=" entry))
    526        :author (or (cdr (assoc "author" entry)) "[no author]")
    527        :doi (or (cdr (assoc "doi" entry)) "[no doi]")
    528        :editor (or (cdr (assoc "editor" entry)) "[no editor]")
    529        :title (or (cdr (assoc "title" entry)) "[no title]")
    530        :booktitle (or (cdr (assoc "booktitle" entry)) "[no booktitle]")
    531        :journal (or (cdr (assoc "journal" entry)) "[no journal]")
    532        :publisher (or (cdr (assoc "publisher" entry)) "[no publisher]")
    533        :pages (or (cdr (assoc "pages" entry)) "[no pages]")
    534        :url (or (cdr (assoc "url" entry)) "[no url]")
    535        :year (or (cdr (assoc "year" entry)) "[no year]")
    536        :month (or (cdr (assoc "month" entry)) "[no month]")
    537        :address (or (cdr (assoc "address" entry)) "[no address]")
    538        :volume (or (cdr (assoc "volume" entry)) "[no volume]")
    539        :number (or (cdr (assoc "number" entry)) "[no number]")
    540        :annote (or (cdr (assoc "annote" entry)) "[no annotation]")
    541        :series (or (cdr (assoc "series" entry)) "[no series]")
    542        :abstract (or (cdr (assoc "abstract" entry)) "[no abstract]")
    543        :btype (or (cdr (assoc "=type=" entry)) "[no type]")
    544        :type "bibtex"
    545        :link link
    546        :description org-bibtex-description))))
    547 
    548 (defun org-create-file-search-in-bibtex ()
    549   "Create the search string and description for a BibTeX database entry."
    550   ;; Make a good description for this entry, using names, year and the title
    551   ;; Put it into the `description' variable which is dynamically scoped.
    552   (let ((bibtex-autokey-names 1)
    553 	(bibtex-autokey-names-stretch 1)
    554 	(bibtex-autokey-name-case-convert-function 'identity)
    555 	(bibtex-autokey-name-separator " & ")
    556 	(bibtex-autokey-additional-names " et al.")
    557 	(bibtex-autokey-year-length 4)
    558 	(bibtex-autokey-name-year-separator " ")
    559 	(bibtex-autokey-titlewords 3)
    560 	(bibtex-autokey-titleword-separator " ")
    561 	(bibtex-autokey-titleword-case-convert-function 'identity)
    562 	(bibtex-autokey-titleword-length 'infty)
    563 	(bibtex-autokey-year-title-separator ": "))
    564     (setq org-bibtex-description (bibtex-generate-autokey)))
    565   ;; Now parse the entry, get the key and return it.
    566   (save-excursion
    567     (bibtex-beginning-of-entry)
    568     (cdr (assoc "=key=" (bibtex-parse-entry)))))
    569 
    570 (defun org-execute-file-search-in-bibtex (s)
    571   "Find the link search string S as a key for a database entry."
    572   (when (eq major-mode 'bibtex-mode)
    573     ;; Yes, we want to do the search in this file.
    574     ;; We construct a regexp that searches for "@entrytype{" followed by the key
    575     (goto-char (point-min))
    576     (and (re-search-forward (concat "@[a-zA-Z]+[ \t\n]*{[ \t\n]*"
    577 				    (regexp-quote s) "[ \t\n]*,")
    578 			    nil t)
    579 	 (goto-char (match-beginning 0)))
    580     (if (and (match-beginning 0) (equal current-prefix-arg '(16)))
    581 	;; Use double prefix to indicate that any web link should be browsed
    582 	(let ((b (current-buffer)) (p (point)))
    583 	  ;; Restore the window configuration because we just use the web link
    584 	  (set-window-configuration org-window-config-before-follow-link)
    585 	  (with-current-buffer b
    586 	    (goto-char p)
    587 	    (bibtex-url)))
    588       (recenter 0))  ; Move entry start to beginning of window
    589     ;; return t to indicate that the search is done.
    590     t))
    591 
    592 ;; Finally add the link search function to the right hook.
    593 (add-hook 'org-execute-file-search-functions 'org-execute-file-search-in-bibtex)
    594 
    595 
    596 ;;; Bibtex <-> Org headline translation functions
    597 (defun org-bibtex (filename)
    598   "Export each headline in the current file to a bibtex entry.
    599 Headlines are exported using `org-bibtex-headline'."
    600   (interactive
    601    (list (read-file-name
    602 	  "Bibtex file: " nil nil nil
    603 	  (let ((file (buffer-file-name (buffer-base-buffer))))
    604 	    (and file
    605 		 (file-name-nondirectory
    606 		  (concat (file-name-sans-extension file) ".bib")))))))
    607   (let ((error-point
    608          (catch 'bib
    609            (let ((bibtex-entries
    610                   (remove nil (org-map-entries
    611                                (lambda ()
    612                                  (condition-case nil
    613                                      (org-bibtex-headline)
    614                                    (error (throw 'bib (point)))))))))
    615              (with-temp-file filename
    616                (insert (mapconcat #'identity bibtex-entries "\n")))
    617              (message "Successfully exported %d BibTeX entries to %s"
    618                       (length bibtex-entries) filename)
    619 	     nil))))
    620     (when error-point
    621       (goto-char error-point)
    622       (message "Bibtex error at %S" (nth 4 (org-heading-components))))))
    623 
    624 (defun org-bibtex-check (&optional optional)
    625   "Check the current headline for required fields.
    626 With prefix argument OPTIONAL also prompt for optional fields."
    627   (interactive "P")
    628   (save-restriction
    629     (org-narrow-to-subtree)
    630     (let ((type (let ((name (org-bibtex-get org-bibtex-type-property-name)))
    631                   (when name (intern (concat ":" name))))))
    632       (when type (org-bibtex-fleshout type optional)))))
    633 
    634 (defun org-bibtex-check-all (&optional optional)
    635   "Check all headlines in the current file.
    636 With prefix argument OPTIONAL also prompt for optional fields."
    637   (interactive) (org-map-entries (lambda () (org-bibtex-check optional))))
    638 
    639 (defun org-bibtex-create (&optional arg nonew)
    640   "Create a new entry at the given level.
    641 With a prefix arg, query for optional fields as well.
    642 If nonew is t, add data to the headline of the entry at point."
    643   (interactive "P")
    644   (let* ((type (completing-read
    645 		"Type: " (mapcar (lambda (type)
    646 				   (substring (symbol-name (car type)) 1))
    647 				 org-bibtex-types)
    648 		nil nil (when nonew
    649 			  (org-bibtex-get org-bibtex-type-property-name))))
    650 	 (type (if (keywordp type) type (intern (concat ":" type))))
    651 	 (org-bibtex-treat-headline-as-title (if nonew nil t)))
    652     (unless (assoc type org-bibtex-types)
    653       (error "Type:%s is not known" type))
    654     (if nonew
    655 	(org-back-to-heading)
    656       (org-insert-heading)
    657       (let ((title (org-bibtex-ask :title)))
    658 	(insert title)
    659 	(org-bibtex-put "TITLE" title)))
    660     (org-bibtex-put org-bibtex-type-property-name
    661 		    (substring (symbol-name type) 1))
    662     (org-bibtex-fleshout type arg)
    663     (dolist (tag org-bibtex-tags) (org-toggle-tag tag 'on))))
    664 
    665 (defun org-bibtex-create-in-current-entry (&optional arg)
    666   "Add bibliographical data to the current entry.
    667 With a prefix arg, query for optional fields."
    668   (interactive "P")
    669   (org-bibtex-create arg t))
    670 
    671 (defun org-bibtex-read ()
    672   "Read a bibtex entry and save to `org-bibtex-entries'.
    673 This uses `bibtex-parse-entry'.
    674 Return the new value of `org-bibtex-entries'."
    675   (interactive)
    676   (let ((keyword (lambda (str) (intern (concat ":" (downcase str)))))
    677 	(clean-space (lambda (str) (replace-regexp-in-string
    678 			            "[[:space:]\n\r]+" " " str)))
    679 	(strip-delim
    680 	 (lambda (str)		     ; strip enclosing "..." and {...}
    681 	   (dolist (pair '((34 . 34) (123 . 125)))
    682 	     (when (and (> (length str) 1)
    683 			(= (aref str 0) (car pair))
    684 			(= (aref str (1- (length str))) (cdr pair)))
    685 	       (setf str (substring str 1 (1- (length str))))))
    686 	   str)))
    687     (push (mapcar
    688            (lambda (pair)
    689              (cons (let ((field (funcall keyword (car pair))))
    690                      (pcase field
    691                        (:=type= :type)
    692                        (:=key= :key)
    693                        (_ field)))
    694                    (funcall clean-space (funcall strip-delim (cdr pair)))))
    695            (save-excursion (bibtex-beginning-of-entry) (bibtex-parse-entry)))
    696           org-bibtex-entries)
    697     (unless (car org-bibtex-entries) (pop org-bibtex-entries))
    698     org-bibtex-entries))
    699 
    700 (defun org-bibtex-read-buffer (buffer)
    701   "Read all bibtex entries in BUFFER and save to `org-bibtex-entries'.
    702 Return the number of saved entries."
    703   (interactive "bBuffer: ")
    704   (let ((start-length (length org-bibtex-entries)))
    705     (with-current-buffer buffer
    706       (save-excursion
    707 	(goto-char (point-max))
    708 	(while (not (= (point) (point-min)))
    709 	  (backward-char 1)
    710 	  (org-bibtex-read)
    711 	  (bibtex-beginning-of-entry))))
    712     (let ((added (- (length org-bibtex-entries) start-length)))
    713       (message "Parsed %d entries" added)
    714       added)))
    715 
    716 (defun org-bibtex-read-file (file)
    717   "Read FILE with `org-bibtex-read-buffer'."
    718   (interactive "fFile: ")
    719   (org-bibtex-read-buffer (find-file-noselect file 'nowarn 'rawfile)))
    720 
    721 (defun org-bibtex-write (&optional noindent)
    722   "Insert a heading built from the first element of `org-bibtex-entries'.
    723 When optional argument NOINDENT is non-nil, do not indent the properties
    724 drawer."
    725   (interactive)
    726   (unless org-bibtex-entries
    727     (error "No entries in `org-bibtex-entries'"))
    728   (let* ((entry (pop org-bibtex-entries))
    729 	 (org-special-properties nil) ; avoids errors with `org-entry-put'
    730 	 (val (lambda (field) (cdr (assoc field entry))))
    731 	 (togtag (lambda (tag) (org-toggle-tag tag 'on))))
    732     (org-insert-heading)
    733     (insert (funcall org-bibtex-headline-format-function entry))
    734     (insert "\n:PROPERTIES:\n")
    735     (org-bibtex-put "TITLE" (funcall val :title) 'insert)
    736     (org-bibtex-put org-bibtex-type-property-name
    737 		    (downcase (funcall val :type))
    738                     'insert)
    739     (dolist (pair entry)
    740       (pcase (car pair)
    741 	(:title    nil)
    742 	(:type     nil)
    743 	(:key      (org-bibtex-put org-bibtex-key-property (cdr pair) 'insert))
    744 	(:keywords (if org-bibtex-tags-are-keywords
    745 		       (dolist (kw (split-string (cdr pair) ", *"))
    746 			 (funcall
    747 			  togtag
    748 			  (replace-regexp-in-string
    749 			   "[^[:alnum:]_@#%]" ""
    750 			   (replace-regexp-in-string "[ \t]+" "_" kw))))
    751 		     (org-bibtex-put (car pair) (cdr pair) 'insert)))
    752 	(_ (org-bibtex-put (car pair) (cdr pair) 'insert))))
    753     (insert ":END:\n")
    754     (mapc togtag org-bibtex-tags)
    755     (unless noindent
    756       (org-indent-region
    757        (save-excursion (org-back-to-heading t) (point))
    758        (point)))))
    759 
    760 (defun org-bibtex-yank ()
    761   "If kill ring holds a bibtex entry yank it as an Org headline."
    762   (interactive)
    763   (let (entry)
    764     (with-temp-buffer (yank 1) (setf entry (org-bibtex-read)))
    765     (if entry
    766 	(org-bibtex-write)
    767       (error "Yanked text does not appear to contain a BibTeX entry"))))
    768 
    769 (defun org-bibtex-import-from-file (file)
    770   "Read bibtex entries from FILE and insert as Org headlines after point."
    771   (interactive "fFile: ")
    772   (let ((pos (point)))
    773     (dotimes (_ (org-bibtex-read-file file))
    774       (save-excursion (org-bibtex-write 'noindent))
    775       (re-search-forward org-property-end-re)
    776       (insert "\n"))
    777     (org-indent-region pos (point))))
    778 
    779 (defun org-bibtex-export-to-kill-ring ()
    780   "Export current headline to kill ring as bibtex entry."
    781   (interactive)
    782   (let ((result (org-bibtex-headline)))
    783     (kill-new result) result))
    784 
    785 (defun org-bibtex-search (string)
    786   "Search for bibliographical entries in agenda files.
    787 This function relies `org-search-view' to locate results."
    788   (interactive "sSearch string: ")
    789   (let ((org-agenda-overriding-header "Bib search results:")
    790         (org-agenda-search-view-always-boolean t))
    791     (org-search-view nil
    792 		     (format "%s +{:%s%s:}"
    793 			     string (or org-bibtex-prefix "")
    794 			     org-bibtex-type-property-name))))
    795 
    796 (provide 'ol-bibtex)
    797 
    798 ;;; ol-bibtex.el ends here