*compilation* バッファの挙動が気に入らんかったもんでね。

どもです。

動機

+-------------------+-------------------+
|                   |                   |
|                   |                   |
|                   |                   |
|     foo.cc        |    bar.cc         |
|                   |                   |
|                   |                   |
|                   |                   |
|                   |                   |
+-------------------+-------------------+

M-x compile!!

+-------------------+-------------------+
|                   |                   |
|                   |                   |
|                   |                   |
|     foo.cc        |  *compilation*    |
|                   |                   |
|                   |                   |
|                   |                   |
|                   |                   |
+-------------------+-------------------+

bar.cc を表示しているバッファが,bar.cc を表示しているバッファが〜!!
*compilation* バッファになってしまう・・・。
えぇ〜。これが気に入らん。

なので,アドバイス

;; compile する前に,*compilation* バッファが無ければそれを作る。
;; ウィンドウを縦に2分割していたら,
;; もう片方が *compilation* バッファになってしまうので,
;; それを防ぐため
(defadvice compile (before my-before-compile-split-window-ad activate)
  (unless (get-buffer-window "*compilation*" nil)
    ;; 現在のフレーム内に,*compilation* バッファがあるかチェック
    (let (comp-buf-height)
      (cond
       ((and compilation-window-height (> (window-height) compilation-window-height))
        (setq comp-buf-height compilation-window-height))
       (t
        ;; compilation-window-height が設定されていない場合は,
        ;; 現在のウィンドウの1/4の高さとする
        (setq comp-buf-height (* 0.25 (window-height)))
        ;; 念のため,高さが0になった場合は,1にしておく
        (if (zerop comp-buf-height) (setq comp-buf-height 1))))
      (split-window-vertically (- comp-buf-height))
      (select-window (next-window))
      ;; *compilation* がリネームされてたらアウチ!!
      (switch-to-buffer "*compilation*")
      (select-window (previous-window)))))

そうすると・・・。

+-------------------+-------------------+
|                   |                   |
|                   |                   |
|                   |                   |
|     foo.cc        |    bar.cc         |
|                   |                   |
|                   |                   |
|                   |                   |
|                   |                   |
+-------------------+-------------------+

M-x compile!!

+-------------------+-------------------+
|                   |                   |
|                   |                   |
|                   |                   |
|     foo.cc        |     bar.cc        |
|                   |                   |
|                   |                   |
+-------------------+                   |
|   *compilation*   |                   |
+-------------------+-------------------+

やったぜ!!

まとめ

(select-window (next-window))
...
(select-window (previous-window))

の部分がかっこわるい気がする。
save-excursion ではだめだった。