forked from emacs-helm/helm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
helm-elisp-package.el
380 lines (341 loc) · 15.9 KB
/
helm-elisp-package.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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
;;; helm-elisp-package.el --- helm interface for package.el -*- lexical-binding: t -*-
;; Copyright (C) 2012 ~ 2015 Thierry Volpiatto <thierry.volpiatto@gmail.com>
;; 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/>.
;;; Code:
(require 'cl-lib)
(require 'helm)
(require 'helm-help)
(require 'package)
(defgroup helm-el-package nil
"helm elisp packages."
:group 'helm)
(defcustom helm-el-package-initial-filter 'all
"Show only installed, upgraded or all packages at startup."
:group 'helm-el-package
:type '(radio :tag "Initial filter for elisp packages"
(const :tag "Show all packages" all)
(const :tag "Show installed packages" installed)
(const :tag "Show not installed packages" uninstalled)
(const :tag "Show upgradable packages" upgrade)))
;; internals vars
(defvar helm-el-package--show-only 'all)
(defvar helm-el-package--initialized-p nil)
(defvar helm-el-package--tabulated-list nil)
(defvar helm-el-package--upgrades nil)
(defvar helm-el-package--removable-packages nil)
(defun helm-el-package--init ()
(when (null package-alist)
(setq helm-el-package--show-only 'all))
(when (fboundp 'package--removable-packages)
(setq helm-el-package--removable-packages
(package--removable-packages)))
(save-selected-window
(list-packages helm-el-package--initialized-p)
(setq helm-el-package--initialized-p t)
(message nil))
(helm-init-candidates-in-buffer
'global
(with-current-buffer (get-buffer "*Packages*")
(setq helm-el-package--tabulated-list tabulated-list-entries)
(buffer-string)))
(setq helm-el-package--upgrades (helm-el-package-menu--find-upgrades))
(if helm-force-updating-p
(if helm-el-package--upgrades
(message "%d package(s) can be upgraded, Refreshing packages list done"
(length helm-el-package--upgrades))
(message "Refreshing packages list done, no upgrades available"))
(setq helm-el-package--show-only (if helm-el-package--upgrades
'upgrade
helm-el-package-initial-filter)))
(kill-buffer "*Packages*"))
(defun helm-el-package-describe (candidate)
(let ((id (get-text-property 0 'tabulated-list-id candidate)))
(describe-package (if (fboundp 'package-desc-name)
(package-desc-name id)
(car id)))))
(defun helm-el-package-visit-homepage (candidate)
(let* ((id (get-text-property 0 'tabulated-list-id candidate))
(pkg (if (fboundp 'package-desc-name) (package-desc-name id)
(car id)))
(desc (cadr (assoc pkg package-archive-contents)))
(extras (package-desc-extras desc))
(url (and (listp extras) (cdr-safe (assoc :url extras)))))
(if (stringp url)
(browse-url url)
(message "Package %s has no homepage"
(propertize (symbol-name pkg)
'face 'font-lock-keyword-face)))))
(defun helm-el-run-visit-homepage ()
(interactive)
(with-helm-alive-p
(helm-exit-and-execute-action 'helm-el-package-visit-homepage)))
(defun helm-el-package-install-1 (pkg-list)
(cl-loop with mkd = pkg-list
for p in mkd
for id = (get-text-property 0 'tabulated-list-id p)
do (package-install
(if (fboundp 'package-desc-name)
(package-desc-name id)
(car id)))
collect (if (fboundp 'package-desc-full-name)
id
(car id))
into installed-list
finally do (progn
(when (boundp 'package-selected-packages)
(customize-save-variable
'package-selected-packages
(append (mapcar 'package-desc-name installed-list)
package-selected-packages)))
(if (fboundp 'package-desc-full-name)
(message (format "%d packages installed:\n(%s)"
(length installed-list)
(mapconcat #'package-desc-full-name
installed-list ", ")))
(message (format "%d packages installed:\n(%s)"
(length installed-list)
(mapconcat 'symbol-name installed-list ", ")))))))
(defun helm-el-package-install (_candidate)
(helm-el-package-install-1 (helm-marked-candidates)))
(defun helm-el-run-package-install ()
(interactive)
(with-helm-alive-p
(helm-exit-and-execute-action 'helm-el-package-install)))
(defun helm-el-package-uninstall-1 (pkg-list)
(cl-loop with mkd = pkg-list
for p in mkd
for id = (get-text-property 0 'tabulated-list-id p)
do
(condition-case-unless-debug err
(with-no-warnings
(if (fboundp 'package-desc-full-name)
;; emacs 24.4
(package-delete id)
;; emacs 24.3
(package-delete (symbol-name (car id))
(package-version-join (cdr id)))))
(error (message (cadr err))))
unless (assoc (elt id 1) package-alist)
collect (if (fboundp 'package-desc-full-name)
id
(cons (symbol-name (car id))
(package-version-join (cdr id))))
into delete-list
finally do (if delete-list
(if (fboundp 'package-desc-full-name)
;; emacs 24.4
(message (format "%d packages deleted:\n(%s)"
(length delete-list)
(mapconcat #'package-desc-full-name
delete-list ", ")))
;; emacs 24.3
(message (format "%d packages deleted:\n(%s)"
(length delete-list)
(mapconcat (lambda (x)
(concat (car x) "-" (cdr x)))
delete-list ", ")))
;; emacs 24.3 doesn't update
;; its `package-alist' after deleting.
(cl-loop for p in package-alist
when (assq (symbol-name (car p)) delete-list)
do (setq package-alist (delete p package-alist))))
"No package deleted")))
(defun helm-el-package-uninstall (_candidate)
(helm-el-package-uninstall-1 (helm-marked-candidates)))
(defun helm-el-run-package-uninstall ()
(interactive)
(with-helm-alive-p
(helm-exit-and-execute-action 'helm-el-package-uninstall)))
(defun helm-el-package-menu--find-upgrades ()
(cl-loop for entry in helm-el-package--tabulated-list
for pkg-desc = (car entry)
for status = (package-desc-status pkg-desc)
when (member status '("installed" "unsigned" "dependency"))
collect pkg-desc
into installed
when (member status '("available" "new"))
collect (cons (package-desc-name pkg-desc) pkg-desc)
into available
finally return
(cl-loop for pkg in installed
for avail-pkg = (assq (package-desc-name pkg) available)
when (and avail-pkg
(version-list-< (package-desc-version pkg)
(package-desc-version
(cdr avail-pkg))))
collect avail-pkg)))
(defun helm-el-package-upgrade-1 (pkg-list)
(cl-loop for p in pkg-list
for pkg-desc = (car p)
for upgrade = (cdr (assq (package-desc-name pkg-desc)
helm-el-package--upgrades))
do
(cond ((null upgrade)
(ignore))
((equal pkg-desc upgrade)
;;Install.
(package-install pkg-desc))
(t
;; Delete.
(if (boundp 'package-selected-packages)
(with-no-warnings
(package-delete pkg-desc t t))
(package-delete pkg-desc))))))
(defun helm-el-package-upgrade (_candidate)
(helm-el-package-upgrade-1
(cl-loop with pkgs = (helm-marked-candidates)
for p in helm-el-package--tabulated-list
for pkg = (car p)
if (member (symbol-name (package-desc-name pkg)) pkgs)
collect p)))
(defun helm-el-run-package-upgrade ()
(interactive)
(with-helm-alive-p
(helm-exit-and-execute-action 'helm-el-package-upgrade)))
(defun helm-el-package-upgrade-all ()
(if helm-el-package--upgrades
(with-helm-display-marked-candidates
helm-marked-buffer-name (mapcar (lambda (x) (symbol-name (car x)))
helm-el-package--upgrades)
(when (y-or-n-p "Upgrade all packages? ")
(helm-el-package-upgrade-1 helm-el-package--tabulated-list)))
(message "No packages to upgrade actually!")))
(defun helm-el-package-upgrade-all-action (_candidate)
(helm-el-package-upgrade-all))
(defun helm-el-run-package-upgrade-all ()
(interactive)
(with-helm-alive-p
(helm-exit-and-execute-action 'helm-el-package-upgrade-all-action)))
(defun helm-el-package--transformer (candidates _source)
(cl-loop for c in candidates
for id = (get-text-property 0 'tabulated-list-id c)
for name = (if (fboundp 'package-desc-name)
(package-desc-name id)
(car id))
for installed-p = (assq name package-alist)
for upgrade-p = (assq name helm-el-package--upgrades)
for user-installed-p = (and (boundp 'package-selected-packages)
(memq name package-selected-packages))
do (when user-installed-p (put-text-property 0 2 'display "S " c))
do (when (memq name helm-el-package--removable-packages)
(put-text-property 0 2 'display "U " c)
(put-text-property
2 (+ (length (symbol-name name)) 2)
'face 'font-lock-variable-name-face c))
for cand = (cons c (car (split-string c)))
when (or (and upgrade-p
(eq helm-el-package--show-only 'upgrade))
(and installed-p
(eq helm-el-package--show-only 'installed))
(and (not installed-p)
(eq helm-el-package--show-only 'uninstalled))
(eq helm-el-package--show-only 'all))
collect cand))
(defun helm-el-package-show-upgrade ()
(interactive)
(with-helm-alive-p
(setq helm-el-package--show-only 'upgrade)
(helm-update)))
(defun helm-el-package-show-installed ()
(interactive)
(with-helm-alive-p
(setq helm-el-package--show-only 'installed)
(helm-update)))
(defun helm-el-package-show-all ()
(interactive)
(with-helm-alive-p
(setq helm-el-package--show-only 'all)
(helm-update)))
(defun helm-el-package-show-uninstalled ()
(interactive)
(with-helm-alive-p
(setq helm-el-package--show-only 'uninstalled)
(helm-update)))
(defvar helm-el-package-map
(let ((map (make-sparse-keymap)))
(set-keymap-parent map helm-map)
(define-key map (kbd "M-I") 'helm-el-package-show-installed)
(define-key map (kbd "M-O") 'helm-el-package-show-uninstalled)
(define-key map (kbd "M-U") 'helm-el-package-show-upgrade)
(define-key map (kbd "M-A") 'helm-el-package-show-all)
(define-key map (kbd "C-c i") 'helm-el-run-package-install)
(define-key map (kbd "C-c r") 'helm-el-run-package-reinstall)
(define-key map (kbd "C-c d") 'helm-el-run-package-uninstall)
(define-key map (kbd "C-c u") 'helm-el-run-package-upgrade)
(define-key map (kbd "C-c U") 'helm-el-run-package-upgrade-all)
(define-key map (kbd "C-c @") 'helm-el-run-visit-homepage)
map))
(defvar helm-source-list-el-package nil)
(defclass helm-list-el-package-source (helm-source-in-buffer)
((init :initform 'helm-el-package--init)
(get-line :initform 'buffer-substring)
(filtered-candidate-transformer :initform 'helm-el-package--transformer)
(action-transformer
:initform
(lambda (actions candidate)
(let ((pkg-desc (get-text-property
0 'tabulated-list-id candidate))
(acts (if helm-el-package--upgrades
(append actions '(("Upgrade all packages"
. helm-el-package-upgrade-all-action)))
actions)))
(cond ((and (package-installed-p (package-desc-name pkg-desc))
(cdr (assq (package-desc-name pkg-desc)
helm-el-package--upgrades)))
(append '(("Upgrade package(s)" . helm-el-package-upgrade)
("Uninstall package(s)" . helm-el-package-uninstall)) acts))
((cdr (assq (package-desc-name pkg-desc)
helm-el-package--upgrades))
(append '(("Upgrade package(s)" . helm-el-package-upgrade)) acts))
((package-installed-p (package-desc-name pkg-desc))
(append acts '(("Reinstall package(s)" . helm-el-package-reinstall)
("Uninstall package(s)" . helm-el-package-uninstall))))
(t (append acts '(("Install packages(s)" . helm-el-package-install))))))))
(help-message :initform 'helm-el-package-help-message)
(keymap :initform helm-el-package-map)
(update :initform 'helm-el-package--update)
(candidate-number-limit :initform 9999)
(action :initform '(("Describe package" . helm-el-package-describe)
("Visit homepage" . helm-el-package-visit-homepage)))))
(defun helm-el-package--update ()
(setq helm-el-package--initialized-p nil))
(defun helm-el-package-reinstall (_pkg)
(cl-loop for p in (helm-marked-candidates)
for pkg-desc = (get-text-property 0 'tabulated-list-id p)
for name = (package-desc-name pkg-desc)
do (if (fboundp 'package-reinstall)
(package-reinstall name)
(package-delete pkg-desc)
(package-install name))))
(defun helm-el-run-package-reinstall ()
(interactive)
(with-helm-alive-p
(helm-exit-and-execute-action 'helm-el-package-reinstall)))
;;;###autoload
(defun helm-list-elisp-packages (arg)
"Preconfigured helm for listing and handling emacs packages."
(interactive "P")
(when arg (setq helm-el-package--initialized-p nil))
(unless helm-source-list-el-package
(setq helm-source-list-el-package
(helm-make-source "list packages" 'helm-list-el-package-source)))
(helm :sources 'helm-source-list-el-package
:buffer "*helm list packages*"))
;;;###autoload
(defun helm-list-elisp-packages-no-fetch ()
"Preconfigured helm for emacs packages.
Same as `helm-list-elisp-packages' but don't fetch packages on remote."
(interactive)
(let ((helm-el-package--initialized-p t))
(helm-list-elisp-packages nil)))
(provide 'helm-elisp-package)
;;; helm-elisp-package.el ends here