anything.elで表示中の情報源を選択して絞り込むコマンド

anything-sourceを選択してanythingを起動するanything-source - IMAKADO::BLOG

似たようなものはすでに作ってある。こっちはanything-sourcesの中から絞り込むもの。候補を表示させた後に情報源を絞り込む点が違う。そのうち本家に取り込む予定。

http://www.emacswiki.org/cgi-bin/wiki/download/RubikitchAnythingConfiguration より抜粋。宝の山なのでanythingに興味がある人は読んでおくことをおすすめする。

;; [2008/09/03] <<<anything-nest>>>
(defun anything-nest (&rest same-as-anything)
  "Nested `anything'. If you use `anything' within `anything', use it."
  (with-selected-window (anything-window)
    (let (anything-current-position
          anything-current-buffer
          (orig-anything-buffer anything-buffer)
          anything-pattern
          anything-buffer
          anything-sources
          anything-compiled-sources
          anything-buffer-chars-modified-tick
          (anything-samewindow t)
          (enable-recursive-minibuffers t))
      (unwind-protect
          (apply #'anything same-as-anything)
        (anything-initialize-overlays orig-anything-buffer)
        (add-hook 'post-command-hook 'anything-check-minibuffer-input)))))

;; [2008/09/03] <<<source selector>>>
(defun anything-displaying-source-names ()
  (with-current-buffer anything-buffer
    (goto-char (point-min))
    (loop with pos
          while (setq pos (next-single-property-change (point) 'anything-header))
          do (goto-char pos)
          collect (buffer-substring-no-properties (point-at-bol)(point-at-eol))
          do (forward-line 1))))

(defun anything-select-source ()
  (interactive)
  (let ((default (assoc-default 'name (anything-get-current-source)))
        (source-names (anything-displaying-source-names))
        (all-source-names (mapcar (lambda (s) (assoc-default 'name s))
                                  (anything-get-sources))))
    (setq anything-candidate-number-limit 9999)
    (anything-aif
        (let (anything-source-filter)
          (anything-nest '(((name . "Anything Source")
                            (candidates . source-names)
                            (action . identity))
                           ((name . "Anything Source (ALL)")
                            (candidates . all-source-names)
                            (action . identity)))
                         nil "Source: " nil
                         default "*anything select source*"))
        (anything-set-source-filter (list it))
      (anything-set-source-filter nil))))

(define-key anything-map "\C-r" 'anything-select-source)

今後のanything.elの開発予定

  • anything-last-sources、anything-compiled-sources等をbuffer localにして複数のanythingバッファをresume可能にする。
  • candidate-transformer、filtered-candidate-transformer、action-transformerを複数個指定できるようにする。これでプラグイン側で自在に出し入れできる。anything-c-composeは不要になるだろう。
  • processまわりのコードを洗練する。
  • async情報源を含んでいても順序を保つようにしたい。marker使えばできるのかな…
  • multi-line属性を実用化する。
  • テスト、テスト、もっとテスト!

1行に(見た目上)複数行表示させるにはoverlayを使えばよい。たとえば*scratch*バッファで以下のコードを実行するとなかなかカオスなことに。multi-line属性を実装するには、このテクニックを使うことになると思う。candidates-in-bufferに対応させるには、改行を\0\0とかに置き換えてバッファに書くときにoverlayを使うといいような。

(setq o (save-excursion (goto-char 1) (make-overlay (point-at-bol)(point-at-eol)))) ; => #<overlay from 1 to 77 in *scratch*>
(overlay-put o 'face 'highlight)        ; => highlight
(overlay-put o 'display "abcd\nefg\nhij")