anything.elでもうひとつのpersistent actionを定義する方法

前置引数でpersistent-actionを追加する

http://www.emacswiki.org/cgi-bin/wiki/AnythingSources にておもしろい情報源が定義された。「Double persistent action for anything-c-source-buffers」だ。本来ならばひとつしか定義できないpersistent-actionにもうひとつの挙動を追加する。
anything-c-source-buffersのpersistent-actionはswitch-to-bufferだが、前置引数をつけるとkill-bufferするように。つまり、C-zでswitch-to-buffer、C-u C-zでkill-bufferとなる。連続的にバッファを削除する機会があるのでなかなかの名案だ。

オリジナルに修正を加えた。

(add-to-list 'anything-c-source-buffers
             '(persistent-action . (lambda (name)
                           (flet ((kill (item)
                                    (kill-buffer item)
                                    (with-current-buffer anything-buffer
                                      (goto-char (point-min))
                                      (re-search-forward "Buffers")
                                      (when (re-search-forward
                                             (concat "^" (regexp-quote item) "$") nil t)
                                        (delete-region (line-beginning-position)
                                                       (line-end-position))
                                        (delete-blank-lines)))
                                    (with-anything-window (anything-mark-current-line))
                                    (message "Buffer <%s> Killed" item))
                                  (goto (item)
                                    (switch-to-buffer item)))
                             (if current-prefix-arg
                                 (kill name)
                                 (goto name))))))

複数のpersistent-actionを定義するようにしてみた

anything-execute-persistent-actionを見てみるとpersistent-actionの関数の属性('persistent-action)がハードコーディングされているので、省略引数にしてみたら互換性を保ちつつ複数のpersistent-actionを持てることに気付いた。なのでさっそく更新。

M-x install-elisp http://www.emacswiki.org/cgi-bin/wiki/download/anything.el

たとえば、C-dでpersistent-action-2で定義されたpersistent-actionを実行するようにした場合のデモはこんなの。C-zで「persistent-action-1」、C-dで「persistent-action-2」とメッセージが表示される。

(define-key anything-map "\C-d" 'anything-execute-persistent-action-2)
(defun anything-execute-persistent-action-2 ()
  (interactive)
  (anything-execute-persistent-action 'persistent-action-2))
(anything '(((name . "test")
             (candidates "ok")
             (persistent-action . (lambda (x) (message "persistent-action-1")))
             (persistent-action-2 . (lambda (x) (message "persistent-action-2"))))))

複数のpersistent-actionがあると、より柔軟なanythingアプリケーションが作成できることだろう。1.120以降から使える。