コマンド名と割り当てているキーの文字列のリストを返す関数

anythingでmenuを作れないかと思って調べたのでメモ。

(defun get-describe-command-cands (s)
  (loop for sym being the symbols
        for sym-name = (symbol-name sym)
        when (and (string-match s sym-name)
                  (commandp sym))
        collect (substitute-command-keys
                 (concat sym-name ": " "\\[" sym-name "]")) into ret
        finally return (remove-if (lambda (s)
                                    (string-match "M-x " s))
                                  ret)))

;; indent を含むコマンドの名前と割り当てられているキーバインドの文字列のリストを返す
(get-describe-command-cands "indent")