I believe there are two ways to solve this problem.
The first is to use the hook `'compilation-finish-functions', which
is:
[A list of f]unctions to call when a compilation process finishes.
Each function is called with two arguments: the compilation buffer,
and a string describing how the process finished.
Which leads to a solution like this:
(add-hook 'compilation-finish-functions 'my-compilation-finish-function)
(defun my-compilation-finish-function (buffer resstring)
"Shrink the window if the process finished successfully."
(let ((compilation-window-height (if (string-match-p "finished" resstring) 5 nil)))
(compilation-set-window-height (get-buffer-window buffer 0))))
The only problem I have with that solution is that it assumes that success can be determined by finding the string "finished" in the result string.
The other alternative is to advise 'compilation-handle-exit
- which
gets passed the exit-status explicitly. I wrote this advice which
shrinks the window when the exit status is non-zero.
(defadvice compilation-handle-exit (around my-compilation-handle-exit-shrink-height activate)
(let ((compilation-window-height (if (zerop (car (ad-get-args 1))) 5 nil)))
(compilation-set-window-height (get-buffer-window (current-buffer) 0))
ad-do-it))
Note: if the *compilation*
window is still visible when you do your second compile, it will not be resized larger upon failure. If you want it to be resized, you'll need to specify a height instead of nil
. Perhaps this would be to your liking (changing the first example):
(if (string-match-p "finished" resstring) 5 (/ (frame-height) 2))
The nil
was replaced with (/ (frame-height) 2)