summaryrefslogtreecommitdiff
path: root/bog.el
diff options
context:
space:
mode:
authorKyle Meyer <kyle@kyleam.com>2014-02-02 23:56:15 -0500
committerKyle Meyer <kyle@kyleam.com>2014-02-03 00:50:18 -0500
commitf8725fe18333d96b23e7f1473de991114f8f181a (patch)
tree84d61f3b20c4551c2ae3d0736d9dfb72d5fdcb9d /bog.el
parent96e5ef57785a05d9a7b618ac4985fbcea53344d7 (diff)
downloadbog-f8725fe18333d96b23e7f1473de991114f8f181a.tar.gz
Add function to rename and clean new BibTeX file
Diffstat (limited to 'bog.el')
-rw-r--r--bog.el33
1 files changed, 33 insertions, 0 deletions
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))