forked from dash-docs-el/helm-dash
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelm-dash.el
214 lines (183 loc) · 7.87 KB
/
helm-dash.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
;;; helm-dash.el --- Helm extension to search dash docsets
;; Copyright (C) 2013 Raimon Grau
;; Copyright (C) 2013 Toni Reina
;; Author: Raimon Grau <[email protected]>
;; Toni Reina <[email protected]>
;; Version: 0.1
;; Package-Requires: ((sqlite "0.1") (helm "0.0.0"))
;; Keywords: docs
;; This program is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with this program. If not, see <http://www.gnu.org/licenses/>.
;;
;;; Commentary:
;;
;; Clone the functionality of dash using helm foundation. Browse
;; documentation via dash docsets.
;;
;; More info in the project site https://github.com/areina/helm-dash
;;
;;; Code:
(require 'helm)
(require 'helm-match-plugin)
(require 'sqlite)
(require 'json)
(require 'ido)
(defgroup helm-dash nil
"Experimental task management."
:prefix "helm-dash-"
:group 'applications)
(defcustom helm-dash-docsets-path
(format "%s/.docsets" (getenv "HOME"))
"Default path for docsets."
:group 'helm-dash)
(defcustom helm-dash-active-docsets
'() "List of Docsets to search.")
(defcustom helm-dash-docsets-url "https://raw.github.com/Kapeli/feeds/master"
"Foo." :group 'helm-dash)
(defcustom helm-dash-completing-read-func 'ido-completing-read
"Completion function to be used when installing docsets.
Suggested possible values are:
* `completing-read': built-in completion method.
* `ido-completing-read': dynamic completion within the minibuffer."
:type 'function
:options '(completing-read ido-completing-read)
:group 'helm-dash)
(defun helm-dash-connect-to-docset (docset)
(sqlite-init (format
"%s/%s.docset/Contents/Resources/docSet.dsidx"
helm-dash-docsets-path docset)))
(defvar helm-dash-connections nil
;;; create conses like ("Go" . connection)
)
(defun helm-dash-create-connections ()
(when (not helm-dash-connections)
(setq helm-dash-connections
(mapcar (lambda (x)
(cons x (helm-dash-connect-to-docset x)))
helm-dash-active-docsets))))
(defun helm-dash-reset-connections ()
(interactive)
(dolist (i helm-dash-connections)
(sqlite-bye (cdr i)))
(setq helm-dash-connections nil))
(defun helm-dash-search-all-docsets ()
(let ((url "https://api.github.com/repos/Kapeli/feeds/contents/"))
(with-current-buffer
(url-retrieve-synchronously url)
(goto-char url-http-end-of-headers)
(json-read))))
(defun helm-dash-available-docsets ()
""
(delq nil (mapcar (lambda (docset)
(let ((name (assoc-default 'name (cdr docset))))
(unless (member name '(".gitignore" ".DS_Store" "price.txt" "zzz"))
(substring name 0 -4))))
(helm-dash-search-all-docsets))))
(defun helm-dash-installed-docsets ()
"Return a list of installed docsets."
(let ((docsets (directory-files helm-dash-docsets-path nil ".docset$")))
(mapcar '(lambda (name)
(cond ((string-match "[^.]+" name) (match-string 0 name))
(t name)))
docsets)))
;;;###autoload
(defun helm-dash-deactivate-docset (docset)
"Deactivate DOCSET. If called interactively prompts for the docset name."
(interactive (list (funcall helm-dash-completing-read-func
"Deactivate docset: " helm-dash-active-docsets
nil t)))
(setq helm-dash-active-docsets (remove docset helm-dash-active-docsets))
(customize-save-variable 'helm-dash-active-docsets helm-dash-active-docsets)
(helm-dash-reset-connections))
;;;###autoload
(defun helm-dash-activate-docset (docset)
"Activate DOCSET. If called interactively prompts for the docset name."
(interactive (list (funcall helm-dash-completing-read-func
"Activate docset: " (helm-dash-installed-docsets)
nil t)))
(add-to-list 'helm-dash-active-docsets docset)
(customize-save-variable 'helm-dash-active-docsets helm-dash-active-docsets)
(helm-dash-reset-connections))
;;;###autoload
(defun helm-dash-install-docset ()
"Download docset with specified NAME and move its stuff to docsets-path."
(interactive)
(let* ((docset-name (funcall helm-dash-completing-read-func
"Install docset: " (helm-dash-available-docsets)))
(feed-url (format "%s/%s.xml" helm-dash-docsets-url docset-name))
(docset-tmp-path (format "%s%s-docset.tgz" temporary-file-directory docset-name))
(feed-tmp-path (format "%s%s-feed.xml" temporary-file-directory docset-name)))
(url-copy-file feed-url feed-tmp-path t)
(url-copy-file (helm-dash-get-docset-url feed-tmp-path) docset-tmp-path t)
(shell-command-to-string (format "tar xvf %s -C %s" docset-tmp-path helm-dash-docsets-path))
(helm-dash-activate-docset docset-name)
(message (format "Installed docset %s." docset-name))))
(defun helm-dash-get-docset-url (feed-path)
""
(let* ((xml (xml-parse-file feed-path))
(urls (car xml))
(url (xml-get-children urls 'url)))
(caddr (first url))))
(defun helm-dash-where-query (pattern)
""
(let ((conditions
(mapcar (lambda (word)
(format "\"name\" like \"%%%s%%\"" word))
(split-string pattern " "))))
(format " WHERE %s" (mapconcat 'identity conditions " AND "))))
(defun helm-dash-search ()
"Iterates every `helm-dash-connections' looking for the
`helm-pattern'."
(let ((db "searchIndex")
(full-res (list))
(where-query (helm-dash-where-query helm-pattern)) ;let the magic happen with spaces
)
(dolist (docset helm-dash-connections)
(let ((res
(and
;; hack to avoid sqlite hanging (timeouting) because of no results
(< 0 (string-to-number (caadr (sqlite-query (cdr docset)
(format
"SELECT COUNT(*) FROM %s %s"
db where-query)))))
(sqlite-query (cdr docset)
(format
"SELECT t.type, t.name, t.path FROM %s t %s order by lower(t.name)"
db where-query)))))
;; how to do the appending properly?
(setq full-res
(append full-res
(mapcar (lambda (x)
(cons (format "%s - %s" (car docset) (cadr x)) (format "%s%s%s%s"
"file://"
helm-dash-docsets-path
(format "/%s.docset/Contents/Resources/Documents/"
(car docset))
(caddr x))))
res)))))
full-res))
(defun helm-dash-actions (actions doc-item) `(("Go to doc" . browse-url)))
(defvar helm-source-dash-search
'((name . "Dash")
(volatile)
(delayed)
(requires-pattern . 3)
(candidates-process . helm-dash-search)
(action-transformer . helm-dash-actions)))
;;;###autoload
(defun helm-dash ()
"Bring up a Dash search interface in helm."
(interactive)
(helm-dash-create-connections)
(helm :sources '(helm-source-dash-search)
:buffer "*helm-dash*"))
(provide 'helm-dash)
;;; helm-dash.el ends here