Skip to content

Instantly share code, notes, and snippets.

@lgatto
Created January 27, 2024 16:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lgatto/f54888e7f16968f853346c67b232cae0 to your computer and use it in GitHub Desktop.
Save lgatto/f54888e7f16968f853346c67b232cae0 to your computer and use it in GitHub Desktop.
Create and yank bibtex entry from a DOI
(defun yank-bibtex-from-doi ()
"Create and yank bibtex entry from a DOI."
(interactive)
;; read the doi from minibuffer
(setq doi (read-from-minibuffer "doi: "))
;; define the curl shell command
(setq cmd
(concat
"curl -LH \"Accept: application/x-bibtex\" "
"https://doi.org/"
doi))
;; define the bibtex entry by calling the cmd and strip stderr
(setq bibtex
(shell-command-to-string
(concat cmd " 2>/dev/null")
)
)
(insert-for-yank bibtex)
)
@sje30
Copy link

sje30 commented Jan 29, 2024

Here would be my version:

(defun yank-bibtex-from-doi ()
  "Create and yank bibtex entry from a DOI.
This command expects a DOI, of the form e.g.
10.1371/journal.pcbi.1004947 and will then grab the bibtex entry.
No error handling is performed e.g. if the DOI is invalid.
If you run this from a bibtex buffer, then run C-c C-q to reformat the entry
after it is inserted."
  (interactive)
  (let* ((doi
	  (read-from-minibuffer "doi: "))
	 (cmd
          (concat
           "curl -LH \"Accept: application/x-bibtex\" "
           "https://doi.org/"
           doi))
	 (bibtex
          (shell-command-to-string
           (concat cmd " 2>/dev/null"))))
    (insert-for-yank bibtex)))

Provide a clearer doc string to say what it should expect (e.g. the DOI stripped of any https://doi.org part).

Use (let*) to define the variables as local within the function, rather than global vars. You need let* rather than let as each var depends on the value of a previously defined local var.

at least for me on Mac, when I run the command I get the bibtex back, but the newlines have been eaten:

 @article{Perez_Riverol_2016, title={Ten Simple Rules for Taking Advantage of Git and GitHub}, volume={12}, ISSN={1553-7358}, url={http://dx.doi.org/10.1371/journal.pcbi.1004947}, DOI={10.1371/journal.pcbi.1004947}, number={7}, journal={PLOS Computational Biology}, publisher={Public Library of Science (PLoS)}, author={Perez-Riverol, Yasset and Gatto, Laurent and Wang, Rui and Sachsenberg, Timo and Uszkoreit, Julian and Leprevost, Felipe da Veiga and Fufezan, Christian and Ternent, Tobias and Eglen, Stephen J. and Katz, Daniel S. and Pollard, Tom J. and Konovalov, Alexander and Flight, Robert M. and Blin, Kai and Vizcaíno, Juan Antonio}, editor={Markel, Scott}, year={2016}, month=jul, pages={e1004947} }

hence the doc string comment about C-c C-q to fix that. Do you get newlines?

@sje30
Copy link

sje30 commented Jan 29, 2024

the comment re: global vars is simply that after running your function, C-h v RET bibtex RET will show you the value of the bibtex variable. This might be intended, as it helps with debugging, but for finished functions, it is better to not clobber the global environment.

@lgatto
Copy link
Author

lgatto commented Jan 29, 2024

Thank you very much! The global var is a mistake, thank you for showing me (let*).

I also get the bibtex entry as a single long line (also from the command line). However, C-c C-q doesn't work for me (tested two GNU/Linux distros), but bibtex-reformat does exactly what I need.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment