+(defun doi-utils-add-bibtex-entry-from-doi (doi bibfile)
+ "Add entry to end of a file in in the current directory ending
+with .bib or in `org-ref-default-bibliography'. If you have an
+active region that starts like a DOI, that will be the initial
+prompt. If no region is selected and the first entry of the
+kill-ring starts like a DOI, then that is the intial
+prompt. Otherwise, you have to type or pste in a DOI."
+ (interactive
+ (list (read-input "DOI: "
+ ;; now set initial input
+ (cond
+ ;; If region is active and it starts like a doi we want it.
+ ((and (region-active-p)
+ (s-match "^10" (buffer-substring
+ (region-beginning)
+ (region-end))))
+ (buffer-susbstring (region-beginning) (region-end)))
+ ;; if the first entry in the kill-ring looks
+ ;; like a DOI, let's use it.
+ ((if (s-match "^10" (car kill-ring))
+ (car kill-ring)))
+ ;; otherwise, we have no initial input. You
+ ;; will have to type it in.
+ (t
+ nil)))
+ ;; now get the bibfile to add it to
+ (ido-completing-read
+ "Bibfile: "
+ (append (f-entries "." (lambda (f) (f-ext? f "bib")))
+ org-ref-default-bibliography))))
+ ;; Wrap in save-window-excursion to restore your window arrangement after this
+ ;; is done.
+ (save-window-excursion
+ (find-file bibfile)
+ ;; Check if the doi already exists
+ (goto-char (point-min))
+ (if (search-forward doi nil t)
+ (message "%s is already in this file" doi)
+ (end-of-buffer)
+ (insert "\n\n")
+ (doi-utils-insert-bibtex-entry-from-doi doi)
+ (save-buffer))))