summaryrefslogtreecommitdiff
path: root/helm-gentoo.el
blob: e7434ae65c9757dfc0b50cc6395fb2f2d8416c35 (plain)
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
;;; helm-gentoo.el --- Helm UI for gentoo portage.

;; Copyright (C) 2012 ~ 2013 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:
(eval-when-compile (require 'cl))
(require 'helm)

(declare-function term-line-mode "term")
(declare-function term-char-mode "term")
(declare-function term-send-input "term")
(declare-function term-send-eof "term")


(defgroup helm-gentoo nil
  "Predefined configurations for `helm.el'."
  :group 'helm)

(defface helm-gentoo-match '((t (:foreground "red")))
  "Face for helm-gentoo installed packages."
  :group 'traverse-faces)


;;; Internals
(defvar helm-gentoo-use-flags nil)
(defvar helm-gentoo-buffer "*helm-gentoo-output*")
(defvar helm-cache-gentoo nil)
(defvar helm-cache-world nil)
(defvar helm-source-gentoo
  '((name . "Portage sources")
    (init . (lambda ()
              (get-buffer-create helm-gentoo-buffer)
              (unless helm-cache-gentoo
                (helm-gentoo-setup-cache))
              (unless helm-cache-world
                (setq helm-cache-world (helm-gentoo-get-world)))
              (helm-gentoo-init-list)))
    (candidates-in-buffer)
    (match . identity)
    (candidate-transformer helm-highlight-world)
    (action . (("Show package" . (lambda (elm)
                                   (helm-gentoo-eshell-action elm "eix")))
               ("Show history" . (lambda (elm)
                                   (if (member elm helm-cache-world)
                                       (helm-gentoo-eshell-action elm "genlop -qe")
                                       (message "No infos on packages not yet installed"))))
               ("Copy in kill-ring" . kill-new)
               ("insert at point" . insert)
               ("Browse HomePage" . (lambda (elm)
                                      (let ((urls (helm-gentoo-get-url elm)))
                                        (browse-url (helm-comp-read "Url: " urls :must-match t)))))
               ("Show extra infos" . (lambda (elm)
                                       (if (member elm helm-cache-world)
                                           (helm-gentoo-eshell-action elm "genlop -qi")
                                           (message "No infos on packages not yet installed"))))
               ("Show use flags" . (lambda (elm)
                                     (helm-gentoo-default-action elm "equery" "-C" "u")
                                     (font-lock-add-keywords nil '(("^\+.*" . font-lock-variable-name-face)))
                                     (font-lock-mode 1)))
               ("Run emerge pretend" . (lambda (elm)
                                         (helm-gentoo-eshell-action elm "emerge -p")))
               ("Emerge" . (lambda (elm)
                             (helm-gentoo-install elm :action 'install)))
               ("Unmerge" . (lambda (elm)
                              (helm-gentoo-install elm :action 'uninstall)))
               ("Show dependencies" . (lambda (elm)
                                        (helm-gentoo-default-action elm "equery" "-C" "d")))
               ("Show related files" . (lambda (elm)
                                         (helm-gentoo-default-action elm "equery" "files")))
               ("Refresh" . (lambda (elm)
                              (helm-gentoo-setup-cache)
                              (setq helm-cache-world (helm-gentoo-get-world))))))))


(defun* helm-gentoo-install (candidate &key action)
  (setq helm-external-commands-list nil)
  (ansi-term (getenv "SHELL") "Gentoo emerge")
  (term-line-mode)
  (let ((command (case action
                   ('install "sudo emerge -av ")
                   ('uninstall "sudo emerge -avC ")
                   (t (error "Unknow action"))))
        (elms (mapconcat 'identity (helm-marked-candidates) " "))
        (beg (point)) end)
    (goto-char (point-max))
    (insert (concat command elms))
    (setq end (point))
    (term-char-mode) (term-send-input)))

(defun helm-gentoo-default-action (elm command &rest args)
  "Gentoo default action that use `helm-gentoo-buffer'."
  (if (member elm helm-cache-world)
      (progn
        (helm-switch-to-buffer helm-gentoo-buffer)
        (erase-buffer)
        (let ((com-list (append args (list elm))))
          (apply #'call-process command nil t nil
                 com-list)))
      (message "No infos on packages not yet installed")))

(defvar helm-source-use-flags
  '((name . "Use Flags")
    (init . (lambda ()
              (unless helm-gentoo-use-flags
                (helm-gentoo-setup-use-flags-cache))
              (helm-gentoo-get-use)))
    (candidates-in-buffer)
    (match . identity)
    (candidate-transformer helm-highlight-local-use)
    (action . (("Description"
                . (lambda (elm)
                    (helm-switch-to-buffer helm-gentoo-buffer)
                    (erase-buffer)
                    (apply #'call-process "euse" nil t nil
                           `("-i"
                             ,elm))
                    (font-lock-add-keywords nil `((,elm . font-lock-variable-name-face)))
                    (font-lock-mode 1)))
               ("Enable"
                . (lambda (elm)
                    (helm-gentoo-eshell-action elm "*sudo -p Password: euse -E")))
               ("Disable"
                . (lambda (elm)
                    (helm-gentoo-eshell-action elm "*sudo -p Password: euse -D")))
               ("Remove"
                . (lambda (elm)
                    (helm-gentoo-eshell-action elm "*sudo -p Password: euse -P")))
               ("Show which dep use this flag"
                . (lambda (elm)
                    (helm-switch-to-buffer helm-gentoo-buffer)
                    (erase-buffer)
                    (apply #'call-process "equery" nil t nil
                           `("-C"
                             "h"
                             ,elm))))))))



(defun helm-gentoo-init-list ()
  "Initialize buffer with all packages in Portage."
  (let* ((portage-buf (get-buffer-create "*helm-gentoo*"))
         (buf (helm-candidate-buffer 'portage-buf)))
    (with-current-buffer buf
      (dolist (i helm-cache-gentoo)
        (insert (concat i "\n"))))))

(defun helm-gentoo-setup-cache ()
  "Set up `helm-cache-gentoo'"
  (setq helm-cache-gentoo
        (split-string (with-temp-buffer
                        (call-process "eix" nil t nil
                                      "--only-names")
                        (buffer-string)))))

(defun helm-gentoo-eshell-action (elm command)
  (when (get-buffer "*EShell Command Output*")
    (kill-buffer "*EShell Command Output*"))
  (message "Wait searching...")
  (let ((buf-fname (buffer-file-name helm-current-buffer)))
    (if (and buf-fname (string-match tramp-file-name-regexp buf-fname))
        (progn
          (save-window-excursion
            (pop-to-buffer "*scratch*")
            (eshell-command (format "%s %s" command elm)))
          (pop-to-buffer "*EShell Command Output*"))
        (eshell-command (format "%s %s" command elm)))))

(defun helm-gentoo-get-use ()
  "Initialize buffer with all use flags."
  (let* ((use-buf (get-buffer-create "*helm-gentoo-use*"))
         (buf (helm-candidate-buffer 'use-buf)))
    (with-current-buffer buf
      (dolist (i helm-gentoo-use-flags)
        (insert (concat i "\n"))))))


(defun helm-gentoo-setup-use-flags-cache ()
  "Setup `helm-gentoo-use-flags'"
  (setq helm-gentoo-use-flags
        (split-string (with-temp-buffer
                        (call-process "eix" nil t nil
                                      "--print-all-useflags")
                        (buffer-string)))))

(defun helm-gentoo-get-url (elm)
  "Return a list of urls from eix output."
  (loop with url-list = (split-string
                         (with-temp-buffer
                           (call-process "eix" nil t nil
                                         elm "--format" "<homepage>\n")
                           (buffer-string)))
        with all
        for i in url-list
        when (and (string-match "^http://.*" i)
                  (not (member i all)))
        collect i into all
        finally return all))

(defun helm-gentoo-get-world ()
  "Return list of all installed package on your system."
  (split-string (with-temp-buffer
                  (call-process "qlist" nil t nil
                                "-I")
                  (buffer-string))))

(defun helm-gentoo-get-local-use ()
  (split-string (with-temp-buffer
                  (call-process "portageq" nil t nil
                                "envvar"
                                "USE")
                  (buffer-string))))


(defun helm-highlight-world (eix)
  "Highlight all installed package."
  (loop for i in eix
        if (member i helm-cache-world)
        collect (propertize i 'face 'helm-gentoo-match)
        else
        collect i))

(defun helm-highlight-local-use (use-flags)
  (let ((local-uses (helm-gentoo-get-local-use)))
    (loop for i in use-flags
          if (member i local-uses)
          collect (propertize i 'face 'helm-gentoo-match)
          else
          collect i)))

;;;###autoload
(defun helm-gentoo ()
  "Preconfigured `helm' for gentoo linux."
  (interactive)
  (helm-other-buffer '(helm-source-gentoo
                       helm-source-use-flags)
                     "*helm gentoo*"))


(provide 'helm-gentoo)

;; Local Variables:
;; byte-compile-warnings: (not cl-functions obsolete)
;; coding: utf-8
;; indent-tabs-mode: nil
;; End:

;;; helm-gentoo.el ends here