- (list
- (read-string "DOI: ")
- (ido-completing-read
- "Bibfile: "
- (append org-ref-default-bibliography
- (f-entries "." (lambda (f) (f-ext? f "bib")))))))
- (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)))
-#+END_SRC
-
-It may be you want to just highlight a doi, and then add it. Here is that function.
-
-#+BEGIN_SRC emacs-lisp :tangle doi-utils.el
-(defun doi-utils-add-bibtex-entry-from-region (start end)
- "add entry assuming region is a doi to end of first entry in `org-ref-default-bibliography'."
- (interactive "r")
- (let ((doi (buffer-substring start end)))
- (find-file (car org-ref-default-bibliography))
+ (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-susbstring
+ (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