diff options
author | Kyle Meyer <kyle@kyleam.com> | 2014-02-02 23:56:15 -0500 |
---|---|---|
committer | Kyle Meyer <kyle@kyleam.com> | 2014-02-03 00:50:18 -0500 |
commit | f8725fe18333d96b23e7f1473de991114f8f181a (patch) | |
tree | 84d61f3b20c4551c2ae3d0736d9dfb72d5fdcb9d | |
parent | 96e5ef57785a05d9a7b618ac4985fbcea53344d7 (diff) | |
download | bog-f8725fe18333d96b23e7f1473de991114f8f181a.tar.gz |
Add function to rename and clean new BibTeX file
-rw-r--r-- | bog-tests.el | 43 | ||||
-rw-r--r-- | bog.el | 33 |
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)))) @@ -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)) |