]> git.donarmstrong.com Git - org-ref.git/blob - arxiv.el
create arxiv functions for getting a bibtex entry from arxiv.
[org-ref.git] / arxiv.el
1 ;;; arxiv.el --- arxiv utilities for org-mode        -*- lexical-binding: t; -*-
2
3 ;; Copyright (C) 2015  John Kitchin
4
5 ;; Author: John Kitchin <jkitchin@andrew.cmu.edu>
6 ;; Keywords:
7
8 ;; This program is free software; you can redistribute it and/or modify
9 ;; it under the terms of the GNU General Public License as published by
10 ;; the Free Software Foundation, either version 3 of the License, or
11 ;; (at your option) any later version.
12
13 ;; This program is distributed in the hope that it will be useful,
14 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
15 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 ;; GNU General Public License for more details.
17
18 ;; You should have received a copy of the GNU General Public License
19 ;; along with this program.  If not, see <http://www.gnu.org/licenses/>.
20
21 ;;; Commentary:
22
23 ;;
24
25 ;;; Code:
26
27 (org-add-link-type
28  "arxiv"
29  ;; clicking
30  (lambda (link-string) (browse-url (format "http://arxiv.org/abs/%s" link-string)))
31  ;; formatting
32 (lambda (keyword desc format)
33    (cond
34     ((eq format 'html)
35      (format  "<a href=\"http://arxiv.org/abs/%s\">arxiv:%s</a>" keyword  keyword))
36     ((eq format 'latex)
37      ;; write out the latex command
38      (format "\\url{http://arxiv.org/abs/%s}" keyword)))))
39
40 ;; arxiv:cond-mat/0410285
41
42 ;; * Getting a bibtex entry for an arxiv article
43 ;; For an arxiv article, there is a link to a NASA ADS page like this:
44 ;; http://adsabs.harvard.edu/cgi-bin/bib_query?arXiv:1503.01742
45 ;; On that page, there is a link to a bibtex entry:
46 ;; http://adsabs.harvard.edu/cgi-bin/nph-bib_query?bibcode=2015arXiv150301742H&data_type=BIBTEX&db_key=PRE&nocookieset=1
47 ;;
48 ;; It looks like you need to get a Bibliographic code from the arxiv number to
49 ;; then get the bibtex entry.
50
51 (defun arxiv-get-bibliographic-code (arxiv-number)
52   "Get Bibliographic code for ARXIV-NUMBER."
53   (with-current-buffer
54       (url-retrieve-synchronously
55        (concat
56         "http://adsabs.harvard.edu/cgi-bin/bib_query?arXiv:"
57         arxiv-number))
58     (search-forward-regexp "name=\\\"bibcode\\\" value=\\\"\\(.*\\)\\\"")
59     (match-string 1)))
60
61 (defun arxiv-get-bibtex-entry (arxiv-bibliographic-code)
62   "Get bibtex entry for ARXIV-BIBLIOGRAPHIC-CODE"
63   (with-current-buffer
64       (url-retrieve-synchronously
65        (format
66         "http://adsabs.harvard.edu/cgi-bin/nph-bib_query?bibcode=%s&data_type=BIBTEX&db_key=PRE&nocookieset=1"
67         arxiv-bibliographic-code))
68     (goto-char  url-http-end-of-headers)
69     (if (search-forward  "Retrieved 1 abstracts" (point-max) t)
70         (progn
71           (forward-line)
72           (buffer-substring (point) (point-max)))
73       (error "Did not get one entry: %s" (buffer-substring (point) (point-max))))))
74
75
76 (defun arxiv-add-bibtex-entry (arxiv-number bibfile)
77   "Add bibtex entry for ARXIV-NUMBER to BIBFILE."
78  (interactive
79    (list (read-input "arxiv: ")
80          ;;  now get the bibfile to add it to
81          (ido-completing-read
82           "Bibfile: "
83           (append (f-entries "." (lambda (f) (f-ext? f "bib")))
84                   org-ref-default-bibliography))))
85  (save-window-excursion
86    (find-file bibfile)
87    (goto-char (point-max))
88    (when (not (looking-at "^")) (insert "\n"))
89    (insert (arxiv-get-bibtex-entry (arxiv-get-bibliographic-code arxiv-number)))
90    (save-buffer)))
91
92
93 (provide 'arxiv)
94 ;;; arxiv.el ends here