Created
January 27, 2024 16:22
-
-
Save lgatto/f54888e7f16968f853346c67b232cae0 to your computer and use it in GitHub Desktop.
Create and yank bibtex entry from a DOI
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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) | |
) |
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.
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
Here would be my version:
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:
hence the doc string comment about C-c C-q to fix that. Do you get newlines?