123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734 |
- (require 'org)
- (require 'bibtex)
- (require 'cl-lib)
- (require 'org-compat)
- (defvar org-agenda-overriding-header)
- (defvar org-agenda-search-view-always-boolean)
- (defvar org-bibtex-description nil)
- (defvar org-id-locations)
- (declare-function bibtex-beginning-of-entry "bibtex" ())
- (declare-function bibtex-generate-autokey "bibtex" ())
- (declare-function bibtex-parse-entry "bibtex" (&optional content))
- (declare-function bibtex-url "bibtex" (&optional pos no-browse))
- (declare-function org-babel-trim "ob" (string &optional regexp))
- (defvar org-bibtex-types
- '((:article
- (:description . "An article from a journal or magazine")
- (:required :author :title :journal :year)
- (:optional :volume :number :pages :month :note))
- (:book
- (:description . "A book with an explicit publisher")
- (:required (:editor :author) :title :publisher :year)
- (:optional (:volume :number) :series :address :edition :month :note))
- (:booklet
- (:description . "A work that is printed and bound, but without a named publisher or sponsoring institution.")
- (:required :title)
- (:optional :author :howpublished :address :month :year :note))
- (:conference
- (:description . "")
- (:required :author :title :booktitle :year)
- (:optional :editor :pages :organization :publisher :address :month :note))
- (:inbook
- (:description . "A part of a book, which may be a chapter (or section or whatever) and/or a range of pages.")
- (:required (:author :editor) :title (:chapter :pages) :publisher :year)
- (:optional :crossref (:volume :number) :series :type :address :edition :month :note))
- (:incollection
- (:description . "A part of a book having its own title.")
- (:required :author :title :booktitle :publisher :year)
- (:optional :crossref :editor (:volume :number) :series :type :chapter :pages :address :edition :month :note))
- (:inproceedings
- (:description . "An article in a conference proceedings")
- (:required :author :title :booktitle :year)
- (:optional :crossref :editor (:volume :number) :series :pages :address :month :organization :publisher :note))
- (:manual
- (:description . "Technical documentation.")
- (:required :title)
- (:optional :author :organization :address :edition :month :year :note))
- (:mastersthesis
- (:description . "A Master’s thesis.")
- (:required :author :title :school :year)
- (:optional :type :address :month :note))
- (:misc
- (:description . "Use this type when nothing else fits.")
- (:required)
- (:optional :author :title :howpublished :month :year :note))
- (:phdthesis
- (:description . "A PhD thesis.")
- (:required :author :title :school :year)
- (:optional :type :address :month :note))
- (:proceedings
- (:description . "The proceedings of a conference.")
- (:required :title :year)
- (:optional :editor (:volume :number) :series :address :month :organization :publisher :note))
- (:techreport
- (:description . "A report published by a school or other institution.")
- (:required :author :title :institution :year)
- (:optional :type :address :month :note))
- (:unpublished
- (:description . "A document having an author and title, but not formally published.")
- (:required :author :title :note)
- (:optional :month :year)))
- "Bibtex entry types with required and optional parameters.")
- (defvar org-bibtex-fields
- '((: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.")
- (:annote . "An annotation. It is not used by the standard bibliography styles, but may be used by others that produce an annotated bibliography.")
- (: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.")
- (: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.")
- (:chapter . "A chapter (or section or whatever) number.")
- (:crossref . "The database key of the entry being cross referenced.")
- (: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.")
- (: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.")
- (:howpublished . "How something strange has been published. The first word should be capitalized.")
- (:institution . "The sponsoring institution of a technical report.")
- (:journal . "A journal name.")
- (: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.")
- (: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,")
- (:note . "Any additional information that can help the reader. The first word should be capitalized.")
- (:number . "Any additional information that can help the reader. The first word should be capitalized.")
- (:organization . "The organization that sponsors a conference or that publishes a manual.")
- (: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 (--).")
- (:publisher . "The publisher’s name.")
- (:school . "The name of the school where a thesis was written.")
- (: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.")
- (:title . "The work’s title, typed as explained in the LaTeX book.")
- (:type . "The type of a technical report for example, 'Research Note'.")
- (:volume . "The volume of a journal or multi-volume book.")
- (: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)'"))
- "Bibtex fields with descriptions.")
- (defvar org-bibtex-entries nil
- "List to hold parsed bibtex entries.")
- (defcustom org-bibtex-autogen-keys nil
- "Set to a truth value to use `bibtex-generate-autokey' to generate keys."
- :group 'org-bibtex
- :version "24.1"
- :type 'boolean)
- (defcustom org-bibtex-prefix nil
- "Optional prefix for all bibtex property names.
- For example setting to `BIB_' would allow interoperability with fireforg."
- :group 'org-bibtex
- :version "24.1"
- :type '(choice
- (const nil)
- (string)))
- (defcustom org-bibtex-treat-headline-as-title t
- "Treat headline text as title if title property is absent.
- If an entry is missing a title property, use the headline text as
- the property. If this value is t, `org-bibtex-check' will ignore
- a missing title field."
- :group 'org-bibtex
- :version "24.1"
- :type 'boolean)
- (defcustom org-bibtex-export-arbitrary-fields nil
- "When converting to bibtex allow fields not defined in `org-bibtex-fields'.
- This only has effect if `org-bibtex-prefix' is defined, so as to
- ensure that other org-properties, such as CATEGORY or LOGGING are
- not placed in the exported bibtex entry."
- :group 'org-bibtex
- :version "24.1"
- :type 'boolean)
- (defcustom org-bibtex-key-property "CUSTOM_ID"
- "Property that holds the bibtex key.
- By default, this is CUSTOM_ID, which enables easy linking to
- bibtex headlines from within an org file. This can be set to ID
- to enable global links, but only with great caution, as global
- IDs must be unique."
- :group 'org-bibtex
- :version "24.1"
- :type 'string)
- (defcustom org-bibtex-tags nil
- "List of tag(s) that should be added to new bib entries."
- :group 'org-bibtex
- :version "24.1"
- :type '(repeat :tag "Tag" (string)))
- (defcustom org-bibtex-tags-are-keywords nil
- "Convert the value of the keywords field to tags and vice versa.
- When non-nil, comma-separated entries in a bibtex entry's keywords
- field will be converted to Org tags. Note: spaces will be escaped
- with underscores, and characters that are not permitted in Org
- tags will be removed.
- When non-nil, local tags in an Org entry will be exported as
- a comma-separated string of keywords when exported to bibtex.
- If `org-bibtex-inherit-tags' is non-nil, inherited tags will also
- be exported as keywords. Tags defined in `org-bibtex-tags' or
- `org-bibtex-no-export-tags' will not be exported."
- :group 'org-bibtex
- :version "24.1"
- :type 'boolean)
- (defcustom org-bibtex-no-export-tags nil
- "List of tag(s) that should not be converted to keywords.
- This variable is relevant only if `org-bibtex-tags-are-keywords'
- is non-nil."
- :group 'org-bibtex
- :version "24.1"
- :type '(repeat :tag "Tag" (string)))
- (defcustom org-bibtex-inherit-tags nil
- "Controls whether inherited tags are converted to bibtex keywords.
- It is relevant only if `org-bibtex-tags-are-keywords' is non-nil.
- Tag inheritence itself is controlled by `org-use-tag-inheritence'
- and `org-exclude-tags-from-inheritence'."
- :group 'org-bibtex
- :version "25.1"
- :package-version '(Org . "8.3")
- :type 'boolean)
- (defcustom org-bibtex-type-property-name "btype"
- "Property in which to store bibtex entry type (e.g., article)."
- :group 'org-bibtex
- :version "24.1"
- :type 'string)
- (defun org-bibtex-get (property)
- (let ((it (let ((org-special-properties
- (delete "FILE" (copy-sequence org-special-properties))))
- (or
- (org-entry-get (point) (upcase property))
- (org-entry-get (point) (concat org-bibtex-prefix
- (upcase property)))))))
- (when it (org-babel-trim it))))
- (defun org-bibtex-put (property value)
- (let ((prop (upcase (if (keywordp property)
- (substring (symbol-name property) 1)
- property))))
- (org-set-property
- (concat (unless (string= org-bibtex-key-property prop) org-bibtex-prefix)
- prop)
- value)))
- (defun org-bibtex-headline ()
- "Return a bibtex entry of the given headline as a string."
- (letrec ((val (lambda (key lst) (cdr (assoc key lst))))
- (to (lambda (string) (intern (concat ":" string))))
- (from (lambda (key) (substring (symbol-name key) 1)))
- (flatten (lambda (&rest lsts)
- (apply #'append (mapcar
- (lambda (e)
- (if (listp e) (apply flatten e) (list e)))
- lsts))))
- (id (org-bibtex-get org-bibtex-key-property))
- (type (org-bibtex-get org-bibtex-type-property-name))
- (tags (when org-bibtex-tags-are-keywords
- (delq nil
- (mapcar
- (lambda (tag)
- (unless (member tag
- (append org-bibtex-tags
- org-bibtex-no-export-tags))
- tag))
- (if org-bibtex-inherit-tags
- (org-get-tags-at)
- (org-get-local-tags-at)))))))
- (when type
- (let ((entry (format
- "@%s{%s,\n%s\n}\n" type id
- (mapconcat
- (lambda (pair)
- (format " %s={%s}" (car pair) (cdr pair)))
- (remove nil
- (if (and org-bibtex-export-arbitrary-fields
- org-bibtex-prefix)
- (mapcar
- (lambda (kv)
- (let ((key (car kv)) (val0 (cdr kv)))
- (when (and
- (string-match org-bibtex-prefix key)
- (not (string=
- (downcase (concat org-bibtex-prefix
- org-bibtex-type-property-name))
- (downcase key))))
- (cons (downcase (replace-regexp-in-string
- org-bibtex-prefix "" key))
- val0))))
- (org-entry-properties nil 'standard))
- (mapcar
- (lambda (field)
- (let ((value (or (org-bibtex-get (funcall from field))
- (and (equal :title field)
- (nth 4 (org-heading-components))))))
- (when value (cons (funcall from field) value))))
- (funcall flatten
- (funcall val :required (funcall val (funcall to type) org-bibtex-types))
- (funcall val :optional (funcall val (funcall to type) org-bibtex-types))))))
- ",\n"))))
- (with-temp-buffer
- (insert entry)
- (when tags
- (bibtex-beginning-of-entry)
- (if (re-search-forward "keywords.*=.*{\\(.*\\)}" nil t)
- (progn (goto-char (match-end 1)) (insert ", "))
- (search-forward ",\n" nil t)
- (insert " keywords={},\n")
- (search-backward "}," nil t))
- (insert (mapconcat #'identity tags ", ")))
- (buffer-string))))))
- (defun org-bibtex-ask (field)
- (unless (assoc field org-bibtex-fields)
- (error "Field:%s is not known" field))
- (save-window-excursion
- (let* ((name (substring (symbol-name field) 1))
- (buf-name (format "*Bibtex Help %s*" name)))
- (with-output-to-temp-buffer buf-name
- (princ (cdr (assoc field org-bibtex-fields))))
- (with-current-buffer buf-name (visual-line-mode 1))
- (org-fit-window-to-buffer (get-buffer-window buf-name))
- (let ((result (read-from-minibuffer (format "%s: " name))))
- (when (> (length result) 0) result)))))
- (defun org-bibtex-autokey ()
- "Generate an autokey for the current headline."
- (org-bibtex-put org-bibtex-key-property
- (if org-bibtex-autogen-keys
- (let* ((entry (org-bibtex-headline))
- (key
- (with-temp-buffer
- (insert entry)
- (bibtex-generate-autokey))))
-
- (when (and
- (equal org-bibtex-key-property "ID")
- (featurep 'org-id)
- (hash-table-p org-id-locations)
- (gethash key org-id-locations))
- (warn "Another entry has the same ID"))
- key)
- (read-from-minibuffer "id: "))))
- (defun org-bibtex-fleshout (type &optional optional)
- "Fleshout current heading, ensuring all required fields are present.
- With optional argument OPTIONAL, also prompt for optional fields."
- (let ((val (lambda (key lst) (cdr (assoc key lst))))
- (keyword (lambda (name) (intern (concat ":" (downcase name)))))
- (name (lambda (keyword) (substring (symbol-name keyword) 1))))
- (dolist (field (append
- (if org-bibtex-treat-headline-as-title
- (remove :title (funcall val :required (funcall val type org-bibtex-types)))
- (funcall val :required (funcall val type org-bibtex-types)))
- (when optional (funcall val :optional (funcall val type org-bibtex-types)))))
- (when (consp field)
- (let ((present (first (remove
- nil
- (mapcar
- (lambda (f) (when (org-bibtex-get (funcall name f)) f))
- field)))))
- (setf field (or present (funcall keyword
- (org-icompleting-read
- "Field: " (mapcar name field)))))))
- (let ((name (funcall name field)))
- (unless (org-bibtex-get name)
- (let ((prop (org-bibtex-ask field)))
- (when prop (org-bibtex-put name prop)))))))
- (when (and type (assoc type org-bibtex-types)
- (not (org-bibtex-get org-bibtex-key-property)))
- (org-bibtex-autokey)))
- (org-add-link-type "bibtex" 'org-bibtex-open)
- (add-hook 'org-store-link-functions 'org-bibtex-store-link)
- (defun org-bibtex-open (path)
- "Visit the bibliography entry on PATH."
- (let* ((search (when (string-match "::\\(.+\\)\\'" path)
- (match-string 1 path)))
- (path (substring path 0 (match-beginning 0))))
- (org-open-file path t nil search)))
- (defun org-bibtex-store-link ()
- "Store a link to a BibTeX entry."
- (when (eq major-mode 'bibtex-mode)
- (let* ((search (org-create-file-search-in-bibtex))
- (link (concat "file:" (abbreviate-file-name buffer-file-name)
- "::" search))
- (entry (mapcar
- (lambda(c)
- (if (string-match
- "^\\(?:{\\|\"\\)\\(.*\\)\\(?:}\\|\"\\)$" (cdr c))
- (cons (car c) (match-string 1 (cdr c))) c))
- (save-excursion
- (bibtex-beginning-of-entry)
- (bibtex-parse-entry)))))
- (org-store-link-props
- :key (cdr (assoc "=key=" entry))
- :author (or (cdr (assoc "author" entry)) "[no author]")
- :editor (or (cdr (assoc "editor" entry)) "[no editor]")
- :title (or (cdr (assoc "title" entry)) "[no title]")
- :booktitle (or (cdr (assoc "booktitle" entry)) "[no booktitle]")
- :journal (or (cdr (assoc "journal" entry)) "[no journal]")
- :publisher (or (cdr (assoc "publisher" entry)) "[no publisher]")
- :pages (or (cdr (assoc "pages" entry)) "[no pages]")
- :url (or (cdr (assoc "url" entry)) "[no url]")
- :year (or (cdr (assoc "year" entry)) "[no year]")
- :month (or (cdr (assoc "month" entry)) "[no month]")
- :address (or (cdr (assoc "address" entry)) "[no address]")
- :volume (or (cdr (assoc "volume" entry)) "[no volume]")
- :number (or (cdr (assoc "number" entry)) "[no number]")
- :annote (or (cdr (assoc "annote" entry)) "[no annotation]")
- :series (or (cdr (assoc "series" entry)) "[no series]")
- :abstract (or (cdr (assoc "abstract" entry)) "[no abstract]")
- :btype (or (cdr (assoc "=type=" entry)) "[no type]")
- :type "bibtex"
- :link link
- :description org-bibtex-description))))
- (defun org-create-file-search-in-bibtex ()
- "Create the search string and description for a BibTeX database entry."
-
-
- (let ((bibtex-autokey-names 1)
- (bibtex-autokey-names-stretch 1)
- (bibtex-autokey-name-case-convert-function 'identity)
- (bibtex-autokey-name-separator " & ")
- (bibtex-autokey-additional-names " et al.")
- (bibtex-autokey-year-length 4)
- (bibtex-autokey-name-year-separator " ")
- (bibtex-autokey-titlewords 3)
- (bibtex-autokey-titleword-separator " ")
- (bibtex-autokey-titleword-case-convert-function 'identity)
- (bibtex-autokey-titleword-length 'infty)
- (bibtex-autokey-year-title-separator ": "))
- (setq org-bibtex-description (bibtex-generate-autokey)))
-
- (save-excursion
- (bibtex-beginning-of-entry)
- (cdr (assoc "=key=" (bibtex-parse-entry)))))
- (defun org-execute-file-search-in-bibtex (s)
- "Find the link search string S as a key for a database entry."
- (when (eq major-mode 'bibtex-mode)
-
-
- (goto-char (point-min))
- (and (re-search-forward (concat "@[a-zA-Z]+[ \t\n]*{[ \t\n]*"
- (regexp-quote s) "[ \t\n]*,") nil t)
- (goto-char (match-beginning 0)))
- (if (and (match-beginning 0) (equal current-prefix-arg '(16)))
-
- (let ((b (current-buffer)) (p (point)))
-
- (set-window-configuration org-window-config-before-follow-link)
- (with-current-buffer b
- (goto-char p)
- (bibtex-url)))
- (recenter 0))
-
- t))
- (add-hook 'org-execute-file-search-functions 'org-execute-file-search-in-bibtex)
- (defun org-bibtex (&optional filename)
- "Export each headline in the current file to a bibtex entry.
- Headlines are exported using `org-bibtex-headline'."
- (interactive
- (list (read-file-name
- "Bibtex file: " nil nil nil
- (file-name-nondirectory
- (concat (file-name-sans-extension (buffer-file-name)) ".bib")))))
- (let ((error-point
- (catch 'bib
- (let ((bibtex-entries
- (remove nil (org-map-entries
- (lambda ()
- (condition-case nil
- (org-bibtex-headline)
- (error (throw 'bib (point)))))))))
- (with-temp-file filename
- (insert (mapconcat #'identity bibtex-entries "\n")))
- (message "Successfully exported %d BibTeX entries to %s"
- (length bibtex-entries) filename) nil))))
- (when error-point
- (goto-char error-point)
- (message "Bibtex error at %S" (nth 4 (org-heading-components))))))
- (defun org-bibtex-check (&optional optional)
- "Check the current headline for required fields.
- With prefix argument OPTIONAL also prompt for optional fields."
- (interactive "P")
- (save-restriction
- (org-narrow-to-subtree)
- (let ((type (let ((name (org-bibtex-get org-bibtex-type-property-name)))
- (when name (intern (concat ":" name))))))
- (when type (org-bibtex-fleshout type optional)))))
- (defun org-bibtex-check-all (&optional optional)
- "Check all headlines in the current file.
- With prefix argument OPTIONAL also prompt for optional fields."
- (interactive) (org-map-entries (lambda () (org-bibtex-check optional))))
- (defun org-bibtex-create (&optional arg nonew)
- "Create a new entry at the given level.
- With a prefix arg, query for optional fields as well.
- If nonew is t, add data to the headline of the entry at point."
- (interactive "P")
- (let* ((type (org-icompleting-read
- "Type: " (mapcar (lambda (type)
- (substring (symbol-name (car type)) 1))
- org-bibtex-types)
- nil nil (when nonew
- (org-bibtex-get org-bibtex-type-property-name))))
- (type (if (keywordp type) type (intern (concat ":" type))))
- (org-bibtex-treat-headline-as-title (if nonew nil t)))
- (unless (assoc type org-bibtex-types)
- (error "Type:%s is not known" type))
- (if nonew
- (org-back-to-heading)
- (org-insert-heading)
- (let ((title (org-bibtex-ask :title)))
- (insert title)
- (org-bibtex-put "TITLE" title)))
- (org-bibtex-put org-bibtex-type-property-name
- (substring (symbol-name type) 1))
- (org-bibtex-fleshout type arg)
- (dolist (tag org-bibtex-tags) (org-toggle-tag tag 'on))))
- (defun org-bibtex-create-in-current-entry (&optional arg)
- "Add bibliographical data to the current entry.
- With a prefix arg, query for optional fields."
- (interactive "P")
- (org-bibtex-create arg t))
- (defun org-bibtex-read ()
- "Read a bibtex entry and save to `org-bibtex-entries'.
- This uses `bibtex-parse-entry'."
- (interactive)
- (let ((keyword (lambda (str) (intern (concat ":" (downcase str)))))
- (clean-space (lambda (str) (replace-regexp-in-string
- "[[:space:]\n\r]+" " " str)))
- (strip-delim
- (lambda (str)
- (dolist (pair '((34 . 34) (123 . 125) (123 . 125)))
- (when (and (> (length str) 1)
- (= (aref str 0) (car pair))
- (= (aref str (1- (length str))) (cdr pair)))
- (setf str (substring str 1 (1- (length str)))))) str)))
- (push (mapcar
- (lambda (pair)
- (cons (let ((field (funcall keyword (car pair))))
- (case field
- (:=type= :type)
- (:=key= :key)
- (otherwise field)))
- (funcall clean-space (funcall strip-delim (cdr pair)))))
- (save-excursion (bibtex-beginning-of-entry) (bibtex-parse-entry)))
- org-bibtex-entries)))
- (defun org-bibtex-read-buffer (buffer)
- "Read all bibtex entries in BUFFER and save to `org-bibtex-entries'.
- Return the number of saved entries."
- (interactive "bBuffer: ")
- (let ((start-length (length org-bibtex-entries)))
- (with-current-buffer buffer
- (save-excursion
- (goto-char (point-max))
- (while (not (= (point) (point-min)))
- (backward-char 1)
- (org-bibtex-read)
- (bibtex-beginning-of-entry))))
- (let ((added (- (length org-bibtex-entries) start-length)))
- (message "Parsed %d entries" added)
- added)))
- (defun org-bibtex-read-file (file)
- "Read FILE with `org-bibtex-read-buffer'."
- (interactive "fFile: ")
- (org-bibtex-read-buffer (find-file-noselect file 'nowarn 'rawfile)))
- (defun org-bibtex-write ()
- "Insert a heading built from the first element of `org-bibtex-entries'."
- (interactive)
- (when (= (length org-bibtex-entries) 0)
- (error "No entries in `org-bibtex-entries'"))
- (let* ((entry (pop org-bibtex-entries))
- (org-special-properties nil)
- (val (lambda (field) (cdr (assoc field entry))))
- (togtag (lambda (tag) (org-toggle-tag tag 'on))))
- (org-insert-heading)
- (insert (funcall val :title))
- (org-bibtex-put "TITLE" (funcall val :title))
- (org-bibtex-put org-bibtex-type-property-name
- (downcase (funcall val :type)))
- (dolist (pair entry)
- (case (car pair)
- (:title nil)
- (:type nil)
- (:key (org-bibtex-put org-bibtex-key-property (cdr pair)))
- (:keywords (if org-bibtex-tags-are-keywords
- (dolist (kw (split-string (cdr pair) ", *"))
- (funcall
- togtag
- (replace-regexp-in-string
- "[^[:alnum:]_@#%]" ""
- (replace-regexp-in-string "[ \t]+" "_" kw))))
- (org-bibtex-put (car pair) (cdr pair))))
- (otherwise (org-bibtex-put (car pair) (cdr pair)))))
- (mapc togtag org-bibtex-tags)))
- (defun org-bibtex-yank ()
- "If kill ring holds a bibtex entry yank it as an Org-mode headline."
- (interactive)
- (let (entry)
- (with-temp-buffer (yank 1) (setf entry (org-bibtex-read)))
- (if entry
- (org-bibtex-write)
- (error "Yanked text does not appear to contain a BibTeX entry"))))
- (defun org-bibtex-import-from-file (file)
- "Read bibtex entries from FILE and insert as Org-mode headlines after point."
- (interactive "fFile: ")
- (dotimes (_ (org-bibtex-read-file file))
- (save-excursion (org-bibtex-write))
- (re-search-forward org-property-end-re)
- (open-line 1) (forward-char 1)))
- (defun org-bibtex-export-to-kill-ring ()
- "Export current headline to kill ring as bibtex entry."
- (interactive)
- (let ((result (org-bibtex-headline)))
- (kill-new result) result))
- (defun org-bibtex-search (string)
- "Search for bibliographical entries in agenda files.
- This function relies `org-search-view' to locate results."
- (interactive "sSearch string: ")
- (let ((org-agenda-overriding-header "Bib search results:")
- (org-agenda-search-view-always-boolean t))
- (org-search-view nil
- (format "%s +{:%s%s:}"
- string (or org-bibtex-prefix "")
- org-bibtex-type-property-name))))
- (provide 'org-bibtex)
|