Add helper functions to org-bibtex

* lisp/org-bibtex.el: (org-bibtex-search): New function.
  (org-bibtex-export-to-kill-ring): New function. Export to kill ring.
  (org-bibtex-create-in-current-entry): New function
  (org-bibtex-create): Make it easier to add bib fields to an
  existing headline
  (org-bibtex-export-arbitrary-fields)
  (org-bibtex-treat-headline-as-title): Fix typos
  (org-bibtex-fleshout): Don't upcase optional field; remove ":" from
  type completion

This patch implements several helper functions: exporting to kill
ring, searching only for entries with bib fields, and creating bib
fields in an existing headline. It makes the UI of org-bibtex-fleshout
more consistent.
This commit is contained in:
Matt Lundin 2011-05-01 17:01:59 -04:00 committed by Eric Schulte
parent 3c148beeb7
commit 1918731fdf

View file

@ -226,14 +226,14 @@ For example setting to 'BIB_' would allow interoperability with fireforg."
(defcustom org-bibtex-treat-headline-as-title t (defcustom org-bibtex-treat-headline-as-title t
"Treat headline text as title if title property is absent. "Treat headline text as title if title property is absent.
If an entry is missing a title property, use the headline text as 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 the property. If this value is t, `org-bibtex-check' will ignore
a missing title field." a missing title field."
:group 'org-bibtex :group 'org-bibtex
:type 'boolean) :type 'boolean)
(defcustom org-bibtex-export-arbitrary-fields nil (defcustom org-bibtex-export-arbitrary-fields nil
"When converting to bibtex allow fields not defined in `org-bibtex-fields'. "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 This only has effect if `org-bibtex-prefix' is defined, so as to
ensure that other org-properties, such as CATEGORY or LOGGING are ensure that other org-properties, such as CATEGORY or LOGGING are
not placed in the exported bibtex entry." not placed in the exported bibtex entry."
:group 'org-bibtex :group 'org-bibtex
@ -385,7 +385,7 @@ This variable is relevant only if `org-bibtex-export-tags-as-keywords` is t."
With optional argument OPTIONAL, also prompt for optional fields." With optional argument OPTIONAL, also prompt for optional fields."
(flet ((val (key lst) (cdr (assoc key lst))) (flet ((val (key lst) (cdr (assoc key lst)))
(keyword (name) (intern (concat ":" (downcase name)))) (keyword (name) (intern (concat ":" (downcase name))))
(name (keyword) (upcase (substring (symbol-name keyword) 1)))) (name (keyword) (substring (symbol-name keyword) 1)))
(dolist (field (append (dolist (field (append
(if org-bibtex-treat-headline-as-title (if org-bibtex-treat-headline-as-title
(remove :title (val :required (val type org-bibtex-types))) (remove :title (val :required (val type org-bibtex-types)))
@ -530,22 +530,36 @@ With prefix argument OPTIONAL also prompt for optional fields."
With prefix argument OPTIONAL also prompt for optional fields." With prefix argument OPTIONAL also prompt for optional fields."
(interactive) (org-map-entries (lambda () (org-bibtex-check optional)))) (interactive) (org-map-entries (lambda () (org-bibtex-check optional))))
(defun org-bibtex-create (&optional arg) (defun org-bibtex-create (&optional arg nonew)
"Create a new entry at the given level." "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") (interactive "P")
(let* ((type (org-icompleting-read (let* ((type (org-icompleting-read
"Type: " (mapcar (lambda (type) (symbol-name (car type))) "Type: " (mapcar (lambda (type)
org-bibtex-types))) (substring (symbol-name (car type)) 1))
(type (if (keywordp type) type (intern type)))) org-bibtex-types)
nil nil (when nonew (org-bibtex-get "TYPE"))))
(type (if (keywordp type) type (intern (concat ":" type))))
(org-bibtex-treat-headline-as-title (if nonew nil t)))
(unless (assoc type org-bibtex-types) (unless (assoc type org-bibtex-types)
(error "type:%s is not known" type)) (error "type:%s is not known" type))
(org-insert-heading) (if nonew
(let ((title (org-bibtex-ask :title))) (org-back-to-heading)
(insert title) (org-bibtex-put "TITLE" title)) (org-insert-heading)
(let ((title (org-bibtex-ask :title)))
(insert title)
(org-bibtex-put "TITLE" title)))
(org-bibtex-put "TYPE" (substring (symbol-name type) 1)) (org-bibtex-put "TYPE" (substring (symbol-name type) 1))
(org-bibtex-fleshout type arg) (org-bibtex-fleshout type arg)
(mapc (lambda (tag) (org-toggle-tag tag t)) org-bibtex-tags))) (mapc (lambda (tag) (org-toggle-tag tag t)) org-bibtex-tags)))
(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 () (defun org-bibtex-read ()
"Read a bibtex entry and save to `*org-bibtex-entries*'. "Read a bibtex entry and save to `*org-bibtex-entries*'.
This uses `bibtex-parse-entry'." This uses `bibtex-parse-entry'."
@ -608,6 +622,21 @@ This uses `bibtex-parse-entry'."
(org-bibtex-write) (org-bibtex-write)
(error "yanked text does not appear to contain a bibtex entry")))) (error "yanked text does not appear to contain a bibtex entry"))))
(defun org-bibtex-export-to-kill-ring ()
"Export current headline to kill ring as bibtex entry."
(interactive)
(kill-new (org-bibtex-headline)))
(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 +{:%sTYPE:}"
string org-bibtex-prefix))))
(provide 'org-bibtex) (provide 'org-bibtex)
;; arch-tag: 83987d5a-01b8-41c7-85bc-77700f1285f5 ;; arch-tag: 83987d5a-01b8-41c7-85bc-77700f1285f5