emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
* [PATCH 1/4] Implement bibtex keywords field <-> org-mode tags
@ 2011-05-01 20:54 Matt Lundin
  0 siblings, 0 replies; only message in thread
From: Matt Lundin @ 2011-05-01 20:54 UTC (permalink / raw)
  To: Org Mode

* lisp/org-bibtex.el (org-bibtex-tags): New variable
  (org-bibtex-tags-are-keywords): New variable
  (org-bibtex-no-export-tags): New variable
  (org-bibtex-headline): Export tags as comma-separated bibtex keywords
  (org-bibtex-read): Import bibtex keywords field as tags

Bibtex users often rely on the keywords field to tag their entries.
With biblatex, the keywords field can be used to organize and filter
bibliographic entries. This patch adds an option to import keywords as
org-mode tags, and to export tags as bibtex keywords.

It also adds an option to add user-defined tags to newly created bib
 lisp/org-bibtex.el |   60 ++++++++++++++++++++++++++++++++++++++++++++++++---
 1 files changed, 56 insertions(+), 4 deletions(-)

diff --git a/lisp/org-bibtex.el b/lisp/org-bibtex.el
index 8070755..f54a6bf 100644
--- a/lisp/org-bibtex.el
+++ b/lisp/org-bibtex.el
@@ -249,6 +249,31 @@ IDs must be unique."
   :group 'org-bibtex
   :type 'string)
+(defcustom org-bibtex-tags nil
+  "List of tag(s) that should be added to new bib entries."
+  :group 'org-bibtex
+  :type '(repeat  :tag "Tag" (string)))
+(defcustom org-bibtex-tags-are-keywords nil
+  "Convert the value of the keywords field to tags and vice versa.
+If set to t, 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.
+If t, local tags in an org entry will be exported as a
+comma-separated string of keywords when exported to bibtex. Tags
+defined in `org-bibtex-tags' or `org-bibtex-no-export-tags' will
+not be exported."
+  :group 'org-bibtex
+  :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-export-tags-as-keywords` is t."
+  :group 'org-bibtex
+  :type '(repeat :tag "Tag" (string)))
 ;;; Utility functions
 (defun org-bibtex-get (property)
@@ -276,7 +301,16 @@ IDs must be unique."
     (let ((notes (buffer-string))
           (id (org-bibtex-get org-bibtex-key-property))
-          (type (org-bibtex-get "type")))
+          (type (org-bibtex-get "type"))
+	  (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))
+			 (org-get-local-tags-at))))))
       (when type
         (let ((entry (format
                       "@%s{%s,\n%s\n}\n" type id
@@ -305,6 +339,12 @@ IDs must be unique."
             (insert entry)
+	    (when tags
+	      (bibtex-beginning-of-entry)
+	      (if (re-search-forward "keywords.*=.*{\\(.*\\)}" nil t)
+	    	  (progn (goto-char (match-end 1)) (insert ", "))
+	    	(bibtex-make-field "keywords" t t))
+	      (insert (mapconcat #'identity tags ", ")))
             (bibtex-reformat) (buffer-string)))))))
 (defun org-bibtex-ask (field)
@@ -493,7 +533,8 @@ With prefix argument OPTIONAL also prompt for optional fields."
     (let ((title (org-bibtex-ask :title)))
       (insert title) (org-bibtex-put "TITLE" title))
     (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)))
 (defun org-bibtex-read ()
   "Read a bibtex entry and save to `*org-bibtex-entries*'.
@@ -525,7 +566,8 @@ This uses `bibtex-parse-entry'."
     (error "No entries in `*org-bibtex-entries*'."))
   (let ((entry (pop *org-bibtex-entries*))
 	(org-special-properties nil)) ; avoids errors with `org-entry-put'
-    (flet ((val (field) (cdr (assoc field entry))))
+    (flet ((val (field) (cdr (assoc field entry)))
+	   (togtag (tag) (org-toggle-tag tag t)))
       (insert (val :title))
       (org-bibtex-put "TITLE" (val :title))
@@ -535,7 +577,17 @@ This uses `bibtex-parse-entry'."
           (:title    nil)
           (:type     nil)
           (:key      (org-bibtex-put org-bibtex-key-property (cdr pair)))
-          (otherwise (org-bibtex-put (car pair)  (cdr pair))))))))
+	  (:keywords (if org-bibtex-tags-are-keywords
+			  (mapc 
+			   (lambda (kw)
+			     (togtag
+			      (replace-regexp-in-string
+			       "[^[:alnum:]_@#%]" "" 
+			       (replace-regexp-in-string "[ \t]+" "_" kw))))
+			   (split-string (cdr pair) ", *"))
+		       (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."

^ permalink raw reply related	[flat|nested] only message in thread

only message in thread, other threads:[~2011-05-01 21:05 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2011-05-01 20:54 [PATCH 1/4] Implement bibtex keywords field <-> org-mode tags Matt Lundin

Code repositories for project(s) associated with this public inbox


This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).