初めての advice を shell-pop.el に捧ぐ。

ども。
いきなりですが,shell-pop.el って便利ですね。
でも,C-uM-x shell とかして,複数の shell-mode のバッファがあるときには,
現在の Window に *shell<2>* があっても,*shell* を使います。
だもんで,上記のような場合には,*shell<2>* を使ってくれるとうれしいわけです。


ソースに手を入れようとしたけど,もう大きいおじさんなんだからアドバイスなるものに,
挑戦しようと思い,Elisp Manual を見ながら,やってみました。

(defun shell-mode-exists ()
  "Return a buffer opened with shell-mode if it exists in the current window.
Otherwise, return nil."
  (interactive)
  (let ((ret nil))
    (walk-windows '(lambda (w)
                     (let ((buffer (window-buffer w)))
                       (save-excursion
                         (set-buffer buffer)
                         (when (eq major-mode 'shell-mode)
                           (setq ret buffer))))))
    ret))

(defadvice shell-pop (before change-to-the-current-shell ())
  "Makes `shell-pop' use the currently-visible shell-mode buffer if it exists.
If multiple shell-mode buffers, for instance, *shell* and
*shell<2>*, are opened, `shell-pop' uses *shell* as its internal
shell-mode buffer even if the current window contains
*shell<2>*. This advice makes `shell-pop' use the
currently-visible shell-mode buffer (in this case *shell<2>*) as
its internal shell-mode buffer."
  (let ((buffer (shell-mode-exists)))
    (when (and buffer (not (eq (buffer-name buffer) shell-pop-internal-mode-buffer)))
      (setq shell-pop-internal-mode-buffer (buffer-name buffer)))))

(ad-activate 'shell-pop)

一応これで期待通りの動作にはなっています。
しかし,advice に外部にある関数を使うのはどうかと思ったりするわけで。
さらに,shell-pop.el が使っている変数を変更してしまうのも,
どうなんだ? という罪の意識もあったりなかったり。
あと,現在の Window に複数の shell-mode バッファがあるばあいとかも,
考えてません。
そのときが来たら直します。


というわけで,どなたか,アドバイス・突っ込みなど,あればお願いします。
(おっ,うまいねぇ。)