Browse Source

import whole bibtex files into Org-mode

* lisp/org-bibtex.el (org-bibtex-read-buffer): Reads all bibtex entries
  in a buffer.
  (org-bibtex-read-file): Read all bibtex entries in a file.
  (org-bibtex-import-from-file): Import all bibtex entries from a file
  into the current buffer.
Eric Schulte 6 years ago
parent
commit
f47a7176b8
1 changed files with 29 additions and 0 deletions
  1. 29 0
      lisp/org-bibtex.el

+ 29 - 0
lisp/org-bibtex.el

@@ -624,6 +624,27 @@ This uses `bibtex-parse-entry'."
            (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)
@@ -665,6 +686,14 @@ This uses `bibtex-parse-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)