-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbibfetch.el
94 lines (78 loc) · 2.81 KB
/
bibfetch.el
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
;;; bibfetch.el -- Fetch BibTeX entries from Google Scholar
;; Author: Daniel Schoepe
;; Version: 0.1
;;; Commentary:
;; This programs uses the accompanying bibfetch.pl script to fetch
;; BibTeX entries from Google Scholar, and provides some convenience
;; functionality for inserting entries into a bibtex file. See
;; README.org for usage information.
;;; Code:
(require 'bibtex)
(defcustom bibfetch-script "bibfetch.pl"
"Path to the bibfetch.pl script.")
(defcustom bibfetch-arguments nil
"Arguments to pass to bibfetch.pl.")
(defvar bibfetch-original-buffer nil
"Buffer from which bibtex was called.")
(make-variable-buffer-local 'bibfetch-original-buffer)
(defun bibfetch-next-entry ()
"Skip to next entry."
(interactive)
(bibtex-end-of-entry)
(forward-char)
(bibtex-beginning-of-entry))
(defun bibfetch-previous-entry ()
"Skip to previous entry."
(interactive)
(bibtex-beginning-of-entry)
(backward-char)
(bibtex-beginning-of-entry))
(defun bibfetch-copy-entry-as-kill ()
"Copy current entry to killring."
(interactive)
(save-excursion
(bibtex-mark-entry)
(copy-region-as-kill (region-beginning) (region-end))))
(defun bibfetch-append-entry-to-caller ()
"Append the current entry in the buffer in which bibfetch was called."
(interactive)
(if (not (buffer-live-p bibfetch-original-buffer))
(message "Original buffer no longer exists")
(bibfetch-copy-entry-as-kill)
(with-current-buffer bibfetch-original-buffer
(save-excursion
(goto-char (point-max))
(unless (bolp)
(newline))
(insert (current-kill 0) "\n")))))
(defun bibfetch-quit ()
"Exit bibfetch results"
(kill-buffer-and-window))
(defvar bibfetch-mode-map
(let ((map (make-sparse-keymap)))
(set-keymap-parent map bibtex-mode-map)
(define-key map "j" #'bibfetch-next-entry)
(define-key map "k" #'bibfetch-previous-entry)
(define-key map "y" #'bibfetch-copy-entry-as-kill)
(define-key map "q" #'bibfetch-quit)
(define-key map (kbd "<down>") #'bibfetch-next-entry)
(define-key map (kbd "<up>") #'bibfetch-previous-entry)
(define-key map (kbd "<return>") #'bibfetch-append-entry-to-caller)
map)
"Key map for interacting with bibfetch results")
(define-derived-mode bibfetch-mode bibtex-mode "bibfetch results"
(use-local-map bibfetch-mode-map)
(setq buffer-read-only t))
(defun bibfetch (query)
"Query Google Scholar with QUERY and return results"
(interactive "MQuery: ")
(let ((buf (generate-new-buffer (concat "*bibfetch: " query "*")))
(orig-buf (current-buffer)))
(with-current-buffer buf
;; XXX make this asynchronous
(apply #'call-process bibfetch-script nil t nil query bibfetch-arguments)
(beginning-of-buffer)
(bibfetch-mode)
(setq bibfetch-original-buffer orig-buf)
(switch-to-buffer-other-window buf))))
(provide 'bibfetch)