summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--bog-tests.el43
-rw-r--r--bog.el33
2 files changed, 76 insertions, 0 deletions
diff --git a/bog-tests.el b/bog-tests.el
index e7ce2cc..24b5d0d 100644
--- a/bog-tests.el
+++ b/bog-tests.el
@@ -114,3 +114,46 @@
(org-mode)
(show-all)
(should-error (bog-citekey-action nil nil nil))))
+
+
+;;; BibTeX functions
+
+;; `bog-prepare-bib-file'
+
+(ert-deftest bog-prepare-bib-file ()
+ (let ((temp-file (make-temp-file "bog-testing-" nil ".bib"))
+ (citekey "name2010word"))
+ (with-current-buffer (find-file-noselect temp-file)
+ (insert (format "\n@article{%s,\n" citekey)
+ "title = {A title},\n"
+ "author = {Last, First},\n"
+ "journal = {Some journal},\n"
+ "year = 2009,\n"
+ "\n}")
+ (save-buffer))
+ (kill-buffer (get-file-buffer temp-file))
+ (bog-prepare-bib-file temp-file)
+ (should-not (file-exists-p temp-file))
+ (let* ((new-file (expand-file-name (concat citekey ".bib") "/tmp"))
+ (new-buffer (get-file-buffer new-file)))
+ (should-not new-buffer)
+ (delete-file new-file))))
+
+(ert-deftest bog-prepare-bib-file-was-open ()
+ (let ((temp-file (make-temp-file "bog-testing-" nil ".bib"))
+ (citekey "name2010word"))
+ (with-current-buffer (find-file-noselect temp-file)
+ (insert (format "\n@article{%s,\n" citekey)
+ "title = {A title},\n"
+ "author = {Last, First},\n"
+ "journal = {Some journal},\n"
+ "year = 2009,\n"
+ "\n}")
+ (save-buffer))
+ (bog-prepare-bib-file temp-file)
+ (should-not (file-exists-p temp-file))
+ (let* ((new-file (expand-file-name (concat citekey ".bib") "/tmp"))
+ (new-buffer (get-file-buffer new-file)))
+ (should new-buffer)
+ (kill-buffer new-buffer)
+ (delete-file new-file))))
diff --git a/bog.el b/bog.el
index 9e4cea4..7e81885 100644
--- a/bog.el
+++ b/bog.el
@@ -229,6 +229,39 @@ first parent heading that matches `bog-citekey-format'."
(error "%s does not exist" bib-file))
(find-file-other-window bib-file)))
+;;;###autoload
+(defun bog-rename-and-clean-new-bib-files ()
+ "Prepare new BibTeX files.
+New files are determined as files in `bog-bib-directory' that do
+not have a basename matching `bog-citekey-format'. This is only
+useful if you use the non-standard setup of one entry per BibTeX
+file."
+ (interactive)
+ (let* ((new (--filter (not (string-match bog-citekey-format it))
+ (bog-bib-citekeys)))
+ (new (--map (concat (expand-file-name it bog-bib-directory) ".bib")
+ new)))
+ (--each new (bog-prepare-bib-file it t))))
+
+(defun bog-prepare-bib-file (file &optional new-key)
+ (save-excursion
+ (let ((was-open (get-file-buffer file))
+ (buffer (find-file-noselect file)))
+ (with-current-buffer buffer
+ (goto-char (point-min))
+ (bibtex-skip-to-valid-entry)
+ (bibtex-clean-entry new-key)
+ (let* ((citekey (bibtex-key-in-head))
+ (bib-file (concat citekey ".bib")))
+ (when (get-buffer bib-file)
+ (error "Buffer for %s already exists" bib-file))
+ (rename-file file bib-file)
+ (rename-buffer bib-file)
+ (set-visited-file-name bib-file)
+ (save-buffer)))
+ (unless was-open
+ (kill-buffer buffer)))))
+
(defun bog-citekey-as-bib (citekey)
(expand-file-name (concat citekey ".bib") bog-bib-directory))