anything.elでrakeタスクを選択する

俺はrakeが大好きだ。だから常にrakeを使っている。タスクもたくさん定義している。だけど俺は記憶力がやばいのでEmacsの中で実行しようと思っても、どんなタスクを定義したのか忘れてしまうことがたびたびある。zshだとタスクを補完してくれるけど、Emacsでできないかと考えた。

結論は今流行りのanything.elを使うことだった。このanything-c-source-rake-taskをanything-sourcesの先頭に入れておくことで、anythingを起動して「rake」と入れた時点で内部で「rake -T」が実行され、タスクを表示してくれるようになった。念願叶って涙が出るほどだ。作者Tamas Patrovicsには感謝している。すばらしいツールをありがとう。

(defvar anything-current-buffer nil)
(defadvice anything (before get-current-buffer activate)
  ""
  (setq anything-current-buffer (current-buffer)))
(setq anything-c-source-rake-task
  '((name . "Rake Task")
    (candidates
     . (lambda ()
         (when (string-match "^rake" anything-pattern)
           (cons '("rake" . "rake")
                 (mapcar (lambda (line)
                           (cons line (car (split-string line " +#"))))
                         (with-current-buffer anything-current-buffer
                           (split-string (shell-command-to-string "rake -T") "\n" t)))))))
    (action ("Compile" . compile)
            ("Compile with command-line edit"
             . (lambda (c) (let ((compile-command (concat c " ")))
                             (call-interactively 'compile)))))
    (requires-pattern . 4)))

現在俺のanything-sourcesの設定は20個。まだまだ増える勢いである。Pentium4クラスだとこれくらいの数は余裕で処理できる。