Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 199 lines (167 sloc) 7.176 kB
94b7639 @thierryvolpiatto * helm-config.el: Move apt to *apt.el.
thierryvolpiatto authored
1 ;;; helm-apt.el --- Helm interface for Debian/Ubuntu packages (apt-*)
2
3 ;; Copyright (C) 2012 Thierry Volpiatto <thierry.volpiatto@gmail.com>
4
5 ;; This program is free software; you can redistribute it and/or modify
6 ;; it under the terms of the GNU General Public License as published by
7 ;; the Free Software Foundation, either version 3 of the License, or
8 ;; (at your option) any later version.
9
10 ;; This program is distributed in the hope that it will be useful,
11 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
12 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 ;; GNU General Public License for more details.
14
15 ;; You should have received a copy of the GNU General Public License
16 ;; along with this program. If not, see <http://www.gnu.org/licenses/>.
17
18 ;;; Code:
19
20 (eval-when-compile (require 'cl))
21 (require 'helm)
22 (require 'helm-utils)
7d8197a @thierryvolpiatto Fix warnings at compilation.
thierryvolpiatto authored
23 (require 'helm-external)
24
25 (declare-function term-line-mode "term")
26 (declare-function term-char-mode "term")
27 (declare-function term-send-input "term")
28 (declare-function term-send-eof "term")
94b7639 @thierryvolpiatto * helm-config.el: Move apt to *apt.el.
thierryvolpiatto authored
29
ba17f09 @thierryvolpiatto Move all defcustoms and faces in their respectives groups and files.
thierryvolpiatto authored
30
31 (defgroup helm-apt nil
32 "Apt related Applications and libraries for Helm."
33 :group 'helm)
34
35 (defface helm-apt-installed
36 '((t (:foreground "green")))
37 "*Face used for apt installed candidates."
38 :group 'helm-apt)
39
40 (defface helm-apt-deinstalled
41 '((t (:foreground "DimGray")))
42 "*Face used for apt deinstalled candidates."
43 :group 'helm-apt)
44
45
94b7639 @thierryvolpiatto * helm-config.el: Move apt to *apt.el.
thierryvolpiatto authored
46 (defvar helm-c-source-apt
47 '((name . "APT")
48 (init . helm-c-apt-init)
49 (candidates-in-buffer)
50 (candidate-transformer helm-c-apt-candidate-transformer)
51 (display-to-real . helm-c-apt-display-to-real)
52 (requires-pattern . 2)
53 (update . helm-c-apt-refresh)
54 (action
55 ("Show package description" . helm-c-apt-cache-show)
56 ("Install package" . helm-c-apt-install)
57 ("Reinstall package" . helm-c-apt-reinstall)
58 ("Remove package" . helm-c-apt-uninstall)
59 ("Purge package" . helm-c-apt-purge))
60 (persistent-action . helm-c-apt-persistent-action)
61 (persistent-help . "Show package description")))
62
63 (defvar helm-c-apt-query "emacs")
64 (defvar helm-c-apt-search-command "apt-cache search '%s'")
65 (defvar helm-c-apt-show-command "apt-cache show '%s'")
66 (defvar helm-c-apt-installed-packages nil)
67 (defvar helm-c-apt-all-packages nil)
68 (defvar helm-c-apt-input-history nil)
69
70 (defun helm-c-apt-refresh ()
71 "Refresh installed candidates list."
72 (setq helm-c-apt-installed-packages nil)
73 (setq helm-c-apt-all-packages nil))
74
75 (defun helm-c-apt-persistent-action (candidate)
76 "Persistent action for APT source."
77 (helm-c-apt-cache-show candidate))
78
79 (defun helm-c-apt-candidate-transformer (candidates)
80 "Show installed CANDIDATES and the ones to deinstall in a different color."
81 (loop for cand in candidates
82 for name = (helm-c-apt-display-to-real cand)
83 collect (cond ((string= (assoc-default
84 name helm-c-apt-installed-packages)
85 "deinstall")
86 (propertize cand 'face 'helm-apt-deinstalled))
87 ((string= (assoc-default
88 name helm-c-apt-installed-packages)
89 "install")
90 (propertize cand 'face 'helm-apt-installed))
91 (t cand))))
92
93 (defun helm-c-apt-init ()
94 "Initialize list of debian packages."
95 (let ((query ""))
96 (unless (and helm-c-apt-installed-packages
97 helm-c-apt-all-packages)
98 (message "Loading package list...")
99 (setq helm-c-apt-installed-packages
100 (with-temp-buffer
101 (call-process-shell-command "dpkg --get-selections"
102 nil (current-buffer))
103 (loop for i in (split-string (buffer-string) "\n" t)
104 for p = (split-string i)
105 collect (cons (car p) (cadr p)))))
106 (setq helm-c-apt-all-packages
107 (with-current-buffer
108 (helm-candidate-buffer
109 (get-buffer-create (format "*helm-apt*")))
110 (erase-buffer)
111 (call-process-shell-command
112 (format helm-c-apt-search-command query)
113 nil (current-buffer))))
114 (message "Loading package list done")
115 (sit-for 0.5))))
116
117 (defun helm-c-apt-display-to-real (line)
118 "Return only name of a debian package.
119 LINE is displayed like:
120 package name - description."
121 (car (split-string line " - ")))
122
123 (defun helm-c-shell-command-if-needed (command)
124 "Run shell command COMMAND to describe package.
125 If a buffer named COMMAND already exists, just switch to it."
126 (let ((buf (get-buffer command)))
127 (helm-c-switch-to-buffer (get-buffer-create command))
128 (unless buf (insert (shell-command-to-string command)))))
129
130 (defun helm-c-apt-cache-show (package)
131 "Show information on apt package PACKAGE."
132 (helm-c-shell-command-if-needed
133 (format helm-c-apt-show-command package)))
134
135 (defun helm-c-apt-install (package)
136 "Run 'apt-get install' shell command on PACKAGE."
137 (helm-c-apt-generic-action :action 'install))
138
139 (defun helm-c-apt-reinstall (package)
140 "Run 'apt-get install --reinstall' shell command on PACKAGE."
141 (helm-c-apt-generic-action :action 'reinstall))
142
143 (defun helm-c-apt-uninstall (package)
144 "Run 'apt-get remove' shell command on PACKAGE."
145 (helm-c-apt-generic-action :action 'uninstall))
146
147 (defun helm-c-apt-purge (package)
148 "Run 'apt-get purge' shell command on PACKAGE."
149 (helm-c-apt-generic-action :action 'purge))
150
151 (defun* helm-c-apt-generic-action (&key action)
152 "Run 'apt-get ACTION'.
153 Support install, remove and purge actions."
154 (ansi-term (getenv "SHELL") "helm apt")
155 (term-line-mode)
156 (let ((command (case action
157 ('install "sudo apt-get install ")
158 ('reinstall "sudo apt-get install --reinstall ")
159 ('uninstall "sudo apt-get remove ")
160 ('purge "sudo apt-get purge ")
161 (t (error "Unknow action"))))
162 (beg (point))
163 end
164 (cand-list (mapconcat #'(lambda (x) (format "'%s'" x))
165 (helm-marked-candidates) " ")))
166 (goto-char (point-max))
167 (insert (concat command cand-list))
168 (setq end (point))
169 (if (y-or-n-p (format "%s package" (symbol-name action)))
170 (progn
171 (setq helm-c-external-commands-list nil)
172 (setq helm-c-apt-installed-packages nil)
173 (term-char-mode) (term-send-input))
174 (delete-region beg end) (term-send-eof) (kill-buffer))))
175
176 ;;;###autoload
177 (defun helm-apt (arg)
178 "Preconfigured `helm' : frontend of APT package manager.
179 With a prefix arg reload cache."
180 (interactive "P")
181 (let ((query (read-string "Search Package: " nil 'helm-c-apt-input-history)))
182 (when arg (helm-c-apt-refresh))
183 (helm :sources 'helm-c-source-apt
184 :prompt "Search Package: "
185 :input query
186 :history 'helm-c-apt-input-history)))
187
188
189 (provide 'helm-apt)
190
37b4201 @thierryvolpiatto Fix dependencies and autoloads. Reorder.
thierryvolpiatto authored
191 ;; Local Variables:
a7ee65b @thierryvolpiatto Disable compile warnings and add new keyword to helm.
thierryvolpiatto authored
192 ;; byte-compile-warnings: (not cl-functions obsolete)
37b4201 @thierryvolpiatto Fix dependencies and autoloads. Reorder.
thierryvolpiatto authored
193 ;; coding: utf-8
194 ;; indent-tabs-mode: nil
195 ;; byte-compile-dynamic: t
196 ;; End:
197
94b7639 @thierryvolpiatto * helm-config.el: Move apt to *apt.el.
thierryvolpiatto authored
198 ;;; helm-apt.el ends here
Something went wrong with that request. Please try again.