Created
December 23, 2017 02:33
-
-
Save guygurari/4ef78c5a5dc2c544eb1cabdf03817a65 to your computer and use it in GitHub Desktop.
An Emacs Helm extension that retrieves citations from the ADS abstract service and inserts a \cite{...} command in the buffer.
This file contains hidden or 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
;; | |
;; An Emacs Helm extension that retrieves citations from the ADS abstract service | |
;; and inserts a \cite{...} command in the buffer. The citation key is the bibcode | |
;; returned by ADS. | |
;; | |
;; To use, call (helm-ads) | |
;; | |
(require 'url) | |
(require 'json) | |
(setq ads-token "YOUR ADS ABSTRACT SERVICE TOKEN") | |
(defun join-strings (list-of-strings separator) | |
(mapconcat #'identity list-of-strings separator)) | |
(defun background-color () | |
(face-attribute 'default :background)) | |
(defun ads-entry-string (doc) | |
`( | |
,(format "%s\t%s\t%s\n'%s'" | |
(gethash "year" doc) | |
(join-strings (gethash "author" doc) ", ") | |
;; Include the pattern in the result so Helm displays | |
;; all our results. Use background color so it's | |
;; invisible, at least when it's not selected. | |
(propertize (format "(pattern: %s)" helm-pattern) 'font-lock-face | |
`(:foreground ,(background-color))) | |
(join-strings (gethash "title" doc) " ")) | |
. ,(gethash "bibcode" doc) | |
) | |
) | |
(defun ads-search-query (query-str) | |
(let ((a-url (format "https://api.adsabs.harvard.edu/v1/search/query?q=%s&fl=bibcode,year,author,title" query-str)) | |
(url-request-method "GET") | |
(url-request-extra-headers | |
`(("Authorization" . ,(format "Bearer %s" ads-token)))) | |
(json-object-type 'hash-table) | |
) | |
(let ((json-response | |
(with-current-buffer | |
(url-retrieve-synchronously a-url) | |
(goto-char url-http-end-of-headers) | |
(json-read))) | |
) | |
(let ((docs (gethash "docs" (gethash "response" json-response)))) | |
(mapcar 'ads-entry-string docs) | |
)))) | |
(defun helm-ads-search () | |
(let ((result (ads-search-query helm-pattern))) | |
(if (eq (length result) 0) `((,(format "No results found for pattern: %s" helm-pattern) . "")) result) | |
)) | |
(setq helm-source-ads-citation-search | |
'((name . "ADS Abstract Service") | |
(volatile) | |
(delayed) | |
(multiline) | |
(requires-pattern . 2) | |
(candidates . helm-ads-search) | |
(action . (lambda (bibcode) (insert (format "\\cite{%s}" bibcode)))) | |
)) | |
(defun helm-ads () | |
"Bring up an ADS search interface in helm. Use this to add citations." | |
(interactive) | |
(let ((helm-input-idle-delay 0.5)) | |
(helm :sources '(helm-source-ads-citation-search) | |
:buffer "*helm-ads*")) | |
) | |
;; Example keybinding | |
(add-hook 'LaTeX-mode-hook | |
(lambda () (local-set-key (kbd "C-c C-c") 'helm-ads))) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment