- (list
- (read-string "DOI: ")
- (ido-completing-read "Bibfile: " org-ref-default-bibliography)))
- (find-file bibfile)
- (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)))
+ (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))))