summaryrefslogtreecommitdiff
path: root/bog.el
diff options
context:
space:
mode:
authorKyle Meyer <kyle@kyleam.com>2014-10-31 02:08:06 -0400
committerKyle Meyer <kyle@kyleam.com>2014-11-01 01:21:31 -0400
commit954a8b47936374dea7e198a38b2a03ba4843d9c6 (patch)
tree6c3d04fdbfb2d86fc24194b2cbb38516c7e65196 /bog.el
parente314276c96bcc6d14ac41f70f3a747cc2e25e683 (diff)
downloadbog-954a8b47936374dea7e198a38b2a03ba4843d9c6.tar.gz
Rewrite some functions to avoid find-file-noselect
Several functions relied on find-file-noselect. I hadn't realized that it's a bad idea to use this non-interactively [1]. [1] http://emacs.stackexchange.com/questions/145/what-are-some-built-in-packages-with-nice-source-code/151#151
Diffstat (limited to 'bog.el')
-rw-r--r--bog.el77
1 files changed, 37 insertions, 40 deletions
diff --git a/bog.el b/bog.el
index 47d7d07..d3f6f92 100644
--- a/bog.el
+++ b/bog.el
@@ -225,7 +225,7 @@ be called with no arguments. If
(bog-selection-method "point-or-buffer-headings"
bog-citekey-at-point
- bog-heading-citekeys-in-buffer)
+ bog-heading-citekeys-in-wide-buffer)
(bog-selection-method "point-or-all-headings"
bog-citekey-at-point
@@ -312,31 +312,26 @@ be preceded by a characters in `bog-allowed-before-citekey'."
bog--all-heading-citekeys nil))
(defun bog-citekeys-in-file (file)
- (let ((was-open (org-find-base-buffer-visiting file))
- (buffer (find-file-noselect file))
- refs)
- (with-current-buffer buffer
- (org-with-wide-buffer
- (goto-char (point-min))
- (while (re-search-forward bog-citekey-format nil t)
- (add-to-list 'refs (match-string-no-properties 0)))))
- (unless was-open
- (kill-buffer buffer))
+ (let (refs)
+ (with-temp-buffer
+ (org-mode)
+ (insert-file-contents file)
+ (while (re-search-forward bog-citekey-format nil t)
+ (add-to-list 'refs (match-string-no-properties 0))))
refs))
(defun bog-heading-citekeys-in-file (file)
- (let ((was-open (org-find-base-buffer-visiting file))
- (buffer (find-file-noselect file))
- citekeys)
- (with-current-buffer buffer
- (save-excursion
- (setq citekeys (bog-heading-citekeys-in-buffer))))
- (unless was-open
- (kill-buffer buffer))
- citekeys))
+ (with-temp-buffer
+ (org-mode)
+ (insert-file-contents file)
+ (setq citekeys (bog-heading-citekeys-in-buffer))))
(defun bog-heading-citekeys-in-buffer ()
(--keep it
+ (org-map-entries 'bog-citekey-from-heading)))
+
+(defun bog-heading-citekeys-in-wide-buffer ()
+ (--keep it
(org-map-entries 'bog-citekey-from-heading nil 'file)))
@@ -510,24 +505,24 @@ one entry per BibTeX file."
(bog-prepare-bib-file it t bog-bib-directory))))
(defun bog-prepare-bib-file (file &optional new-key new-directory)
- (let ((was-open (find-buffer-visiting file))
- (buffer (find-file-noselect file)))
- (with-current-buffer buffer
- (save-excursion
- (goto-char (point-min))
- (bibtex-skip-to-valid-entry)
- (bibtex-clean-entry new-key)
- (let* ((citekey (bibtex-key-in-head))
- (bib-file
- (expand-file-name (concat citekey ".bib") new-directory)))
- (when (get-buffer bib-file)
- (user-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))))
+ (let (bib-file)
+ (with-temp-buffer
+ (bibtex-mode)
+ (insert-file-contents file)
+ (bibtex-skip-to-valid-entry)
+ (bibtex-clean-entry new-key)
+ (setq bib-file (expand-file-name (concat (bibtex-key-in-head) ".bib")
+ new-directory))
+ (write-file bib-file))
+ ;; If a buffer was visiting the original bib file, point it to the
+ ;; new file.
+ (--when-let (find-buffer-visiting file)
+ (with-current-buffer it
+ (when (get-buffer bib-file)
+ (user-error "Buffer for %s already exists" bib-file))
+ (rename-buffer bib-file)
+ (set-visited-file-name bib-file nil t)))
+ (delete-file file)))
;;;###autoload
(defun bog-create-combined-bib ()
@@ -565,7 +560,9 @@ occur in buffer instead of alphabetical order."
If `bog-bib-file' is non-nil, it returns citekeys from this file
instead of citekeys from file names in `bog-bib-directory'."
(if bog-bib-file
- (with-current-buffer (find-file-noselect bog-bib-file)
+ (with-temp-buffer
+ (bibtex-mode)
+ (insert-file-contents bog-bib-file)
(-map 'car (bibtex-parse-keys)))
(-map 'file-name-base
(file-expand-wildcards (concat
@@ -760,7 +757,7 @@ argument CURRENT-BUFFER, limit to heading citekeys from the
current buffer."
(interactive "P")
(let ((citekey-func (if current-buffer
- 'bog-heading-citekeys-in-buffer
+ 'bog-heading-citekeys-in-wide-buffer
'bog-all-heading-citekeys)))
(insert (bog-select-citekey (funcall citekey-func)))))