Use external tool `w3m' for dumping html to plain texts.
[xwl-elisp.git] / gentoo.el
blob4a79ca57237b0bb6385c16a5a9abd8fb83da09bd
1 ;;; gentoo.el --- gentoo administration within Emacs
3 ;; Copyright (C) 2007 William Xu
5 ;; Author: William Xu <william.xwl@gmail.com>
6 ;; Version: 0.1
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)
12 ;; any later version.
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.
24 ;;; Commentary:
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
35 ;; ~/.emacs:
36 ;; (autoload 'gentoo "gentoo")
38 ;;; Code:
40 (require 'ansi-color)
42 ;;; Customizations
44 (defgroup gentoo nil
45 "An interface for gentoo in debian."
46 :group 'gentoo)
48 (defcustom gentoo-mode-hook nil
49 "Normal hook run after entering gentoo mode."
50 :type 'hook
51 :group 'gentoo)
53 (defcustom gentoo-cache-filename "~/.gentoo-cache.el"
54 "Gentoo cache file."
55 :type 'string
56 :group 'gentoo)
59 ;;; Gentoo Mode
61 ;;;###autoload
62 (defun gentoo ()
63 "Create a *gentoo* buffer."
64 (interactive)
65 (let ((gentoo-exist-p (get-buffer "*gentoo*")))
66 (switch-to-buffer "*gentoo*")
67 (unless gentoo-exist-p
68 (gentoo-mode))))
70 (defvar gentoo-mode-map
71 ;; (setq gentoo-mode-map
72 (let ((map (make-sparse-keymap)))
73 ;; help
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)
78 ;; mode related
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)
88 ;; service control
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)
95 ;; query pkg info
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)
99 map)
100 ;; )
101 "Keymap for `gentoo-mode'.")
103 (define-derived-mode gentoo-mode nil "Gentoo"
104 "Major mode for gentoo.
105 \\{gentoo-mode-map}"
106 (gentoo-mode-help)
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."
115 (interactive)
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'.")
126 ;;; emerge
128 (defun gentoo-emerge (pkg)
129 (interactive
130 (list
131 (ido-completing-read "$ sudo emerge " gentoo-installed-pkgs)))
132 (gentoo-command `("sudo" "emerge" ,pkg)))
134 (defun gentoo-emerge-unmerge (pkg)
135 (interactive
136 (list
137 (ido-completing-read "$ sudo emerge --unmerge " gentoo-installed-pkgs)))
138 (gentoo-command `("sudo" "emerge" "--unmerge" ,pkg)))
140 (defun gentoo-emerge-update (pkg)
141 (interactive
142 (list
143 (ido-completing-read "$ sudo emerge --update " gentoo-installed-pkgs)))
144 (gentoo-command `("sudo" "emerge" "--update" ,pkg)))
147 ;;; eix
149 (defun gentoo-eix-update ()
150 "$ sudo update-eix"
151 (interactive)
152 (gentoo-command '("sudo" "update-eix")))
154 (defun gentoo-eix-sync ()
155 (interactive)
156 (gentoo-command '("sudo" "eix-sync")))
158 (defun gentoo-eix-search-name (pkg)
159 "$ eix -s pkg"
160 (interactive
161 (list
162 (ido-completing-read "$ eix -s " gentoo-installed-pkgs)))
163 (gentoo-command `("eix" "-s" ,pkg)))
165 (defun gentoo-eix-search-description (pkg)
166 "$ eix -S pkg"
167 (interactive
168 (list
169 (ido-completing-read "$ eix -S " gentoo-installed-pkgs)))
170 (gentoo-command `("eix" "-S" ,pkg)))
173 ;;; equery
175 (defun gentoo-equery-files (pkg)
176 (interactive
177 (list
178 (ido-completing-read "$ equery files " gentoo-installed-pkgs)))
179 (gentoo-command `("equery" "files" ,pkg)))
181 (defun gentoo-equery-sizes (pkg)
182 (interactive
183 (list
184 (ido-completing-read "$ equery sizes " gentoo-installed-pkgs)))
185 (gentoo-command `("equery" "sizes" ,pkg)))
187 (defun gentoo-equery-belongs (pkg)
188 (interactive
189 (list (read-string "$ equery belongs ")))
190 (gentoo-command `("equery" "belongs" ,pkg)))
193 ;;; Service Control (/etc/init.d/*)
195 (defun gentoo-service-start (service)
196 (interactive
197 (list
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)
202 (interactive
203 (list
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)
208 (interactive
209 (list
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."
215 (interactive
216 (list
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."
222 (interactive
223 (list
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."
229 (interactive)
230 (gentoo-command '("sudo" "rc-update" "show" "default" "--verbose")))
233 ;;; Other Commands
235 (defun gentoo-help-emerge ()
236 (interactive)
237 (gentoo-command (split-string "emerge -h")))
239 (defun gentoo-help-eix ()
240 (interactive)
241 (gentoo-command (split-string "eix -h")))
243 (defun gentoo-help-equery ()
244 (interactive)
245 (gentoo-command (split-string "equery -h")))
247 (defun gentoo-help-genlop ()
248 (interactive)
249 (gentoo-command (split-string "genlop -h")))
251 (defun gentoo-edit-make-conf ()
252 (interactive)
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*
260 buffer."
261 (let ((inhibit-read-only t))
262 (gentoo)
263 (erase-buffer)
264 (if gentoo-running
265 (error "Gentoo process already exists")
266 (setq gentoo-running t)
267 (setq gentoo-process
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)
277 (save-excursion
278 (with-current-buffer (get-buffer "*gentoo*")
279 (let ((inhibit-read-only t))
280 (cond
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))
291 (save-excursion
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."
301 (interactive)
302 (when 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'."
317 (interactive)
318 (message "Updating gentoo cache...")
319 (gentoo-update-installed-pkgs)
320 (gentoo-update-services)
321 ;; (gentoo-update-command-path-alist)
322 (with-temp-buffer
323 (insert ";; automatically generated by gentoo.el\n")
324 (insert (format "
325 \(setq gentoo-installed-pkgs '%S
326 gentoo-services '%S)
328 gentoo-installed-pkgs
329 gentoo-services))
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
336 (split-string
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)
346 ;; initial variables
347 (if (file-readable-p gentoo-cache-filename)
348 (load-file gentoo-cache-filename)
349 (gentoo-update-cache))
351 (provide 'gentoo)
353 ;;; gentoo.el ends here