/
anything-rurema-suggest.el
77 lines (66 loc) · 2.73 KB
/
anything-rurema-suggest.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
(require 'json)
(require 'anything-config)
(require 'anything-match-plugin)
(defcustom anything-c-rurema-suggest-url
"http://rurema.clear-code.com/api:internal/auto-complete/?term="
"URL used for looking up Rurema suggestions."
:type 'string
:group 'anything-config)
(defcustom anything-c-rurema-suggest-search-url
"http://rurema.clear-code.com/query:"
"URL used for Rurema searching."
:type 'string
:group 'anything-config)
(defcustom anything-rurema-suggest-use-curl-p nil
"*When non--nil use CURL to get info from `anything-c-rurema-suggest-url'.
Otherwise `url-retrieve-synchronously' is used."
:type 'boolean
:group 'anything-config)
(defun anything-rurema-suggest ()
"Preconfigured `anything' for rurema search with rurema suggest."
(interactive)
(anything-other-buffer 'anything-c-source-rurema-suggest "*anything rurema*"))
;;; Rurema Suggestions
(defvar anything-rm-sug-lgh-flag 0)
(defun anything-c-rurema-suggest-fetch (input)
"Fetch suggestions for INPUT from JSON buffer.
Return an alist with elements like (data)."
(let ((request (concat anything-c-rurema-suggest-url
(url-hexify-string input))))
(flet ((fetch ()
(json-read-from-string
(car (cdr
(split-string
(funcall 'buffer-string)
"\n\n"
))))))
(if anything-rurema-suggest-use-curl-p
(with-temp-buffer
(call-process "curl" nil t nil request)
(fetch))
(with-current-buffer
(url-retrieve-synchronously request)
(fetch))))))
(defun anything-c-rurema-suggest-set-candidates ()
"Set candidates with result and number of rurema results found."
(let ((suggestions (anything-c-rurema-suggest-fetch anything-input)))
(append suggestions nil)))
(defvar anything-c-rurema-suggest-default-browser-function nil
"*The browse url function you prefer to use with rurema suggest.
When nil, use the first browser function available
See `anything-browse-url-default-browser-alist'.")
(defun anything-c-rurema-suggest-action (candidate)
"Default action to jump to a rurema suggested candidate."
(let ((arg (concat anything-c-rurema-suggest-search-url
(url-hexify-string candidate))))
(anything-aif anything-c-rurema-suggest-default-browser-function
(funcall it arg)
(anything-c-browse-url arg))))
(defvar anything-c-source-rurema-suggest
'((name . "Rurema Suggest")
(candidates . anything-c-rurema-suggest-set-candidates)
(action . (("Rurema Search" . anything-c-rurema-suggest-action)))
(volatile)
(requires-pattern . 1)
(delayed)))
(provide 'anything-rurima-suggest)