emacs で anything 起動中に入力中のパターンを区切りマッチに変化させるコマンド

ik:anything-cycle-pattern を anything 起動中に起動させると入力中のパターンを「区切りにマッチする正規表現」に変化させる事ができます。連続して起動すると順番に変化していきます。

自分は Shift+d に割り当てています。

(define-key anything-map (kbd "D") 'ik:anything-cycle-pattern)

ik:anything-cycle-pattern 関数の定義は次のようになります。

(eval-when-compile
  (require 'cl))
(defvar ik:anything-cycle-pattern-count 0)
(defun ik:anything-cycle-pattern ()
  (interactive)
  (unless (string= "" anything-pattern)
    (let* ((los '(("\\<" "\\>")
                  "\\<"
                  "\\>"
                  ("\\_<" "\\_>")
                  "\\_<"
                  "\\_>"
                  nil))
           (cleanup-re (rx (or "\\<"
                               "\\>"
                               "\\_<"
                               "\\_>"))))
      (if (eq this-command real-last-command)
          (incf ik:anything-cycle-pattern-count)
        (setq ik:anything-cycle-pattern-count 0))
      (when (>= ik:anything-cycle-pattern-count (length los))
        (setq ik:anything-cycle-pattern-count 0))
      (when (eq this-command real-last-command)
        (save-excursion
          (loop while (re-search-backward cleanup-re nil t)
                do (replace-match ""))))
      (let ((sep (nth ik:anything-cycle-pattern-count los)))
        (etypecase sep
          (cons (save-excursion (backward-sexp) (insert (car sep)))
                (insert (second sep)))
          (string (cond
                   ((string-match "\\\\_?<" sep)
                    (save-excursion (backward-sexp) (insert sep)))
                   (t
                    (insert sep))))
          (null 'do-nothing))))))