emacs-orgmode
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[O] [PATCH 1/4] Implement bibtex keywords field <-> org-mode tags


From: Matt Lundin
Subject: [O] [PATCH 1/4] Implement bibtex keywords field <-> org-mode tags
Date: Sun, 1 May 2011 16:54:01 -0400

* 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
headlines.
---
 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."
                                    lsts))))
     (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."
                        ",\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 ", "))
+               (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)))
       (org-insert-heading)
       (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:address@hidden" "" 
+                              (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."
-- 
1.7.5




reply via email to

[Prev in Thread] Current Thread [Next in Thread]