Emacs補完候補の選択を便利に - あどけない話

Emacs が提供する標準の Completion List を便利にする方法。以下の方法
を想定しているそうな。今までは 2 の後に補完候補をミニバッファにコピ
ペしていたので、これは結構良いかも。(・∀・)

  1. TAB や M-TAB で補完の候補を表示
  2. C-xo で補完の候補のバッファへ移動
  3. C-f/C-b/C-n/C-p あるいは C-s/C-r で候補を選択
  4. RET で候補を選ぶと、元々の状態に戻り、選んだ候補が挿入される

以下が該当のコード。~/.emacs に張り付けるだけ。n/p にも割り当ててみた。

(define-key completion-list-mode-map (kbd "n")   'next-completion)
(define-key completion-list-mode-map (kbd "C-n") 'next-completion)
(define-key completion-list-mode-map (kbd "C-f") 'next-completion)
(define-key completion-list-mode-map (kbd "p")   'previous-completion)
(define-key completion-list-mode-map (kbd "C-p") 'previous-completion)
(define-key completion-list-mode-map (kbd "C-b") 'previous-completion)
(define-key completion-list-mode-map (kbd "C-m") 'my-choose-completion)

(defun my-choose-completion ()
  "Choose the completion that point is in or next to."
  (interactive)
  (let (beg end completion (buffer completion-reference-buffer)
            (base-size completion-base-size))
    (if (and (not (eobp)) (get-text-property (point) 'mouse-face))
        (setq end (point) beg (1+ (point))))
    (if (and (not (bobp)) (get-text-property (1- (point)) 'mouse-face))
        (setq end (1- (point)) beg (point)))
    (if (null beg)
        (error "No completion here"))
    (setq beg (previous-single-property-change beg 'mouse-face))
    (setq end (or (next-single-property-change end 'mouse-face) (point-max)))
    (setq completion (buffer-substring-no-properties beg end))
    (delete-completion-window)
    (choose-completion-string completion buffer base-size)))

P.S.
Mew でのフォルダ名入力時の、C-s/C-r/TAB ももちろん使っています。:-)