topコマンドのanything.elインターフェース

topコマンドはCPUやメモリを喰ってるプロセスをリストアップしてくれる。なるべくパソコンを軽くしたいので常に起動しているのだが、プロセスを殺すときにいちいちプロセスIDを打ち込まないといけないので面倒だ。そこでanythingインターフェース。

M-x anything-top でtopコマンドを起動し、その中でC-c C-uを押すと更新。アクションはとりあえずkillのみ。

anything-config.elに加えてるので更新するだけで使える。

M-x install-elisp-from-emacswiki anything.el
M-x install-elisp-from-emacswiki anything-config.el

;;; Top (process)
(defvar anything-c-top-command "COLUMNS=%s top -b -n 1"
  "Top command (batch mode). %s is replaced with `frame-width'.")
(defvar anything-c-source-top
  '((name . "Top (Press C-c C-u to refresh)")
    (init . anything-c-top-init)
    (candidates-in-buffer)
    (display-to-real . anything-c-top-display-to-real)
    (action
     ("kill (TERM)" . (lambda (pid) (anything-c-top-sh (format "kill -TERM %s" pid))))
     ("kill (KILL)" . (lambda (pid) (anything-c-top-sh (format "kill -KILL %s" pid)))))))
;; (anything 'anything-c-source-top)

(defun anything-c-top-sh (cmd)
  (message "Executed %s\n%s" cmd (shell-command-to-string cmd)))

(defun anything-c-top-init ()
  (with-current-buffer (anything-candidate-buffer 'global)
    (call-process-shell-command
     (format anything-c-top-command (frame-width))
     nil (current-buffer))))

(defun anything-c-top-display-to-real (line)
  (car (split-string line)))

(defun anything-c-top-refresh ()
  (interactive)
  (let ((anything-source-name (assoc-default 'name anything-c-source-top))) ;UGLY HACK
    (anything-c-top-init))
  (anything-update))

(defun anything-top ()
  "Preconfigured `anything' for top command."
  (interactive)
  (let ((anything-samewindow t)
        (anything-display-function 'anything-default-display-buffer)
        (anything-map (copy-keymap anything-map))
        (anything-candidate-number-limit 9999))
    (define-key anything-map "\C-c\C-u" 'anything-c-top-refresh)
    (save-window-excursion
      (delete-other-windows)
      (anything-other-buffer 'anything-c-source-top "*anything top*"))))