1 ;;; gentoo.el --- gentoo administration within Emacs
3 ;; Copyright (C) 2007 William Xu
5 ;; Author: William Xu <william.xwl@gmail.com>
7 ;; Url: http://xwl.appspot.com/ref/gentoo.el
9 ;; This program is free software; you can redistribute it and/or modify
10 ;; it under the terms of the GNU General Public License as published by
11 ;; the Free Software Foundation; either version 3, or (at your option)
14 ;; This program is distributed in the hope that it will be useful, but
15 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 ;; General Public License for more details.
19 ;; You should have received a copy of the GNU General Public License
20 ;; along with EMMS; see the file COPYING. If not, write to the
21 ;; Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
22 ;; Boston, MA 02110-1301, USA.
26 ;; This package tries to simply gentoo daily administration work(not all
27 ;; but most frequent commands) by using Emacs' ido features for
28 ;; completing package names. It's modelled after `wajig.el' which I
29 ;; wrote for Debian GNU/Linux.
31 ;; To make full use of this package, you should install emerge, eix,
32 ;; gentoolkit, genlop, sudo, etc.
34 ;; Put this file into your load-path and the following into your
36 ;; (autoload 'gentoo "gentoo")
45 "An interface for gentoo in debian."
48 (defcustom gentoo-mode-hook nil
49 "Normal hook run after entering gentoo mode."
53 (defcustom gentoo-cache-filename
"~/.gentoo-cache.el"
63 "Create a *gentoo* buffer."
65 (let ((gentoo-exist-p (get-buffer "*gentoo*")))
66 (switch-to-buffer "*gentoo*")
67 (unless gentoo-exist-p
70 (defvar gentoo-mode-map
71 ;; (setq gentoo-mode-map
72 (let ((map (make-sparse-keymap)))
74 (define-key map
(kbd "h e") 'gentoo-help-emerge
)
75 (define-key map
(kbd "h i") 'gentoo-help-eix
)
76 (define-key map
(kbd "h q") 'gentoo-help-equery
)
77 (define-key map
(kbd "h g") 'gentoo-help-genlop
)
79 (define-key map
(kbd "K") 'gentoo-kill
)
80 ;; essensial pkg operations
81 (define-key map
(kbd "U") 'gentoo-eix-sync
)
82 (define-key map
(kbd "s") 'gentoo-eix-search-name
)
83 (define-key map
(kbd "S") 'gentoo-eix-search-description
)
84 (define-key map
(kbd "i") 'gentoo-emerge
)
85 (define-key map
(kbd "u") 'gentoo-emerge-update
)
86 (define-key map
(kbd "r") 'gentoo-emerge-unmerge
)
87 (define-key map
(kbd "E") 'gentoo-edit-make-conf
)
89 (define-key map
(kbd "v x") 'gentoo-service-start
)
90 (define-key map
(kbd "v v") 'gentoo-service-stop
)
91 (define-key map
(kbd "v r") 'gentoo-service-restart
)
92 (define-key map
(kbd "v a") 'gentoo-service-rc-add
)
93 (define-key map
(kbd "v d") 'gentoo-service-rc-delete
)
94 (define-key map
(kbd "v s") 'gentoo-service-rc-show
)
96 (define-key map
(kbd "q f") 'gentoo-equery-files
)
97 (define-key map
(kbd "q s") 'gentoo-equery-size
)
98 (define-key map
(kbd "q b") 'gentoo-equery-belongs
)
101 "Keymap for `gentoo-mode'.")
103 (define-derived-mode gentoo-mode nil
"Gentoo"
104 "Major mode for gentoo.
107 (set-syntax-table gentoo-mode-syntax-table
)
108 ;; (setq font-lock-defaults '(gentoo-font-lock-keywords))
109 (unless gentoo-installed-pkgs
110 (gentoo-update-cache))
111 (run-hooks 'gentoo-mode-hook
))
113 (defun gentoo-mode-help ()
114 "Help page for gentoo-mode."
116 (message "For a list of available key bindings, press `F1 m' or `C-h m'."))
118 (defvar gentoo-mode-syntax-table
119 (let ((st (make-syntax-table)))
120 (modify-syntax-entry ?-
"w" st
)
121 (modify-syntax-entry ?
/ "w" st
)
123 "Syntax table used while in `gentoo-mode'.")
128 (defun gentoo-emerge (pkg)
131 (ido-completing-read "$ sudo emerge " gentoo-installed-pkgs
)))
132 (gentoo-command `("sudo" "emerge" ,pkg
)))
134 (defun gentoo-emerge-unmerge (pkg)
137 (ido-completing-read "$ sudo emerge --unmerge " gentoo-installed-pkgs
)))
138 (gentoo-command `("sudo" "emerge" "--unmerge" ,pkg
)))
140 (defun gentoo-emerge-update (pkg)
143 (ido-completing-read "$ sudo emerge --update " gentoo-installed-pkgs
)))
144 (gentoo-command `("sudo" "emerge" "--update" ,pkg
)))
149 (defun gentoo-eix-update ()
152 (gentoo-command '("sudo" "update-eix")))
154 (defun gentoo-eix-sync ()
156 (gentoo-command '("sudo" "eix-sync")))
158 (defun gentoo-eix-search-name (pkg)
162 (ido-completing-read "$ eix -s " gentoo-installed-pkgs
)))
163 (gentoo-command `("eix" "-s" ,pkg
)))
165 (defun gentoo-eix-search-description (pkg)
169 (ido-completing-read "$ eix -S " gentoo-installed-pkgs
)))
170 (gentoo-command `("eix" "-S" ,pkg
)))
175 (defun gentoo-equery-files (pkg)
178 (ido-completing-read "$ equery files " gentoo-installed-pkgs
)))
179 (gentoo-command `("equery" "files" ,pkg
)))
181 (defun gentoo-equery-sizes (pkg)
184 (ido-completing-read "$ equery sizes " gentoo-installed-pkgs
)))
185 (gentoo-command `("equery" "sizes" ,pkg
)))
187 (defun gentoo-equery-belongs (pkg)
189 (list (read-string "$ equery belongs ")))
190 (gentoo-command `("equery" "belongs" ,pkg
)))
193 ;;; Service Control (/etc/init.d/*)
195 (defun gentoo-service-start (service)
198 (ido-completing-read "$ sudo /etc/init.d/? start: " gentoo-services
)))
199 (gentoo-command `("sudo" ,(concat "/etc/init.d/" service
) "start")))
201 (defun gentoo-service-stop (service)
204 (ido-completing-read "$ sudo /etc/init.d/? stop: " gentoo-services
)))
205 (gentoo-command `("sudo" ,(concat "/etc/init.d/" service
) "stop")))
207 (defun gentoo-service-restart (service)
210 (ido-completing-read "$ sudo /etc/init.d/? restart: " gentoo-services
)))
211 (gentoo-command `("sudo" ,(concat "/etc/init.d/" service
) "restart")))
213 (defun gentoo-service-rc-add (service)
214 "Add SERVICE to default runlevel."
217 (ido-completing-read "$ sudo rc-update add ? default: " gentoo-services
)))
218 (gentoo-command `("sudo" "rc-update" "add" ,service
"default")))
220 (defun gentoo-service-rc-delete (service)
221 "Delete SERVICE from default runlevel."
224 (ido-completing-read "$ sudo rc-update del ? default: " gentoo-services
)))
225 (gentoo-command `("sudo" "rc-update" "del" ,service
"default")))
227 (defun gentoo-service-rc-show ()
228 "Show SERVICE enabled in default runlevel."
230 (gentoo-command '("sudo" "rc-update" "show" "default" "--verbose")))
235 (defun gentoo-help-emerge ()
237 (gentoo-command (split-string "emerge -h")))
239 (defun gentoo-help-eix ()
241 (gentoo-command (split-string "eix -h")))
243 (defun gentoo-help-equery ()
245 (gentoo-command (split-string "equery -h")))
247 (defun gentoo-help-genlop ()
249 (gentoo-command (split-string "genlop -h")))
251 (defun gentoo-edit-make-conf ()
253 (find-file "/sudo::/etc/make.conf"))
256 ;;; Low Level Functions
258 (defun gentoo-command (command-string)
259 "Run COMMAND-STRING, e.g., '(\"cmd\" \"arg1\" ...) in *gentoo*
261 (let ((inhibit-read-only t
))
265 (error "Gentoo process already exists")
266 (setq gentoo-running t
)
268 (apply 'start-process
"gentoo" "*gentoo*" command-string
))
269 (set-process-filter gentoo-process
'gentoo-process-filter
)
270 (set-process-sentinel gentoo-process
'gentoo-process-sentinel
))))
272 (defvar gentoo-process nil
)
273 (defvar gentoo-running nil
)
275 (defun gentoo-process-sentinel (process event
)
276 (setq gentoo-running nil
)
278 (with-current-buffer (get-buffer "*gentoo*")
279 (let ((inhibit-read-only t
))
281 ((eq (process-status process
) 'exit
)
282 (insert "------------- done --------------\n")
283 (goto-char (point-max)))
284 ((eq (process-status process
) 'signal
)
285 (message "gentoo process killed")))))))
287 (defun gentoo-process-filter (process output
)
288 (with-current-buffer (process-buffer process
)
289 (let ((moving (= (point) (process-mark process
)))
290 (inhibit-read-only t
))
292 (goto-char (process-mark process
))
293 (insert (replace-regexp-in-string "
" "" output
))
294 (set-marker (process-mark process
) (point)))
295 (and moving
(goto-char (process-mark process
)))
296 (let ((ansi-color-for-comint-mode t
))
297 (ansi-color-process-output "")))))
299 (defun gentoo-kill ()
300 "Kill running gentoo process."
303 (unless (eq (process-status gentoo-process
) 'exit
)
304 (delete-process gentoo-process
))
305 (setq gentoo-running nil
)))
307 (defvar gentoo-installed-pkgs nil
308 "Installed packages on the system.
309 You can run `gentoo-update-cache' to keep update.")
311 (defvar gentoo-services nil
312 "Existing services' list.
313 You can run `gentoo-update-cache' to keep update.")
315 (defun gentoo-update-cache ()
316 "Update gentoo cache saved in `gentoo-cache-filename'."
318 (message "Updating gentoo cache...")
319 (gentoo-update-installed-pkgs)
320 (gentoo-update-services)
321 ;; (gentoo-update-command-path-alist)
323 (insert ";; automatically generated by gentoo.el\n")
325 \(setq gentoo-installed-pkgs '%S
328 gentoo-installed-pkgs
330 (write-region (point-min) (point-max) gentoo-cache-filename
))
331 (message "Updating gentoo cache...done"))
333 (defun gentoo-update-installed-pkgs ()
334 "Update `gentoo-installed-pkgs'."
335 (setq gentoo-installed-pkgs
337 (shell-command-to-string
338 "eix -I | egrep '^\\[I\\]' | sed 's/^.*\\///'"))))
340 (defun gentoo-update-services ()
341 (setq gentoo-services
342 (split-string (shell-command-to-string "ls /etc/init.d/"))))
344 ;; (add-hook 'gentoo-mode-hook 'less-minor-mode-on)
347 (if (file-readable-p gentoo-cache-filename
)
348 (load-file gentoo-cache-filename
)
349 (gentoo-update-cache))
353 ;;; gentoo.el ends here