It is annoying to see the compilation window appear when it is successful. How to remove that automatically?
I want to see it though if it is not successful.
It is annoying to see the compilation window appear when it is successful. How to remove that automatically?
I want to see it though if it is not successful.
Looking quickly at the code (in library compile.el
), you should be able to just kill or hide the displayed buffer by using a function on hook compilation-finish-functions
. To do that, use something like this:
(add-hook 'compilation-finish-functions (lambda (buf strg) (kill-buffer buf))
If you don't want to kill the buffer, then use something like this:
(add-hook 'compilation-finish-functions
(lambda (buf strg)
(let ((win (get-buffer-window buf 'visible)))
(when win (delete-window win)))))
In general, you can imagine that for something like this there is probably a hook already provided, so you can easily attach code at such a significant place in the processing. A little browsing of the code or using M-x apropos
will typically quickly let you know. The names of hooks typically end in -hook
or -functions
.
years ago on the #emacs IRC channel I got this bit of code I've been using forever. The value "2 sec" sets the amount of time the successful compilation window stays visible.
; from enberg on #emacs
(add-hook 'compilation-finish-functions
(lambda (buf str)
(if (null (string-match ".*exited abnormally.*" str))
;;no errors, make the compilation window go away in a few seconds
(progn
(run-at-time
"2 sec" nil 'delete-windows-on
(get-buffer-create "*compilation*"))
(message "No Compilation Errors!")))))
This thread talks about how to autohide the compilation buffer when compilation succeeds and keep showing the buffer when there are errors or warnings. The use case may be relevant to many users.
The credit goes to the original author, jpkotta. Here is his answer:
I use the following for compilation. It keeps the compilation buffer if there are warnings or errors, and buries it otherwise (after 1 second).
(defun bury-compile-buffer-if-successful (buffer string) "Bury a compilation buffer if succeeded without warnings " (if (and (string-match "compilation" (buffer-name buffer)) (string-match "finished" string) (not (with-current-buffer buffer (search-forward "warning" nil t)))) (run-with-timer 1 nil (lambda (buf) (bury-buffer buf) (switch-to-prev-buffer (get-buffer-window buf) 'kill)) buffer))) (add-hook 'compilation-finish-functions 'bury-compile-buffer-if-successful)
I have this snippet in my .emacs.d
:
(defcustom compilation-auto-quit-window-delay 1
"Time in seconds before auto closing the window."
:group 'compilation
:type 'number)
(defun compilation-auto-quit-window-finish-function (buffer status)
"Quit the *compilation* window if it went well."
(let ((window (get-buffer-window buffer)))
(when (and (equal status "finished\n")
(compilation-went-super-p))
(run-with-timer
(or compilation-auto-quit-window-delay 0) nil
(lambda nil
(when (and (window-live-p window)
(eq (window-buffer window)
buffer)
(not (eq (selected-window)
window)))
(save-selected-window
(quit-window nil window))))))))
(define-minor-mode compilation-auto-quit-window
"Automatically close the *compilation* window if it went well."
:global t
(cond (compilation-auto-quit-window
(add-hook 'compilation-finish-functions
'compilation-auto-quit-window-finish-function))
(t
(remove-hook 'compilation-finish-functions
'compilation-auto-quit-window-finish-function))))
(defun compilation-went-super-p (&optional buffer)
"Return t, if no gotoable output appeared."
(with-current-buffer (or buffer (current-buffer))
(save-excursion
(goto-char (point-min))
(let (;; (compilation-skip-threshold 1)
)
(not (ignore-errors
(compilation-next-error 1)
t))))))
I've tweaked above answers with better logic and tested it, working perfectly:
(add-hook 'compilation-start-hook 'compilation-started)
(add-hook 'compilation-finish-functions 'hide-compile-buffer-if-successful)
M-x customize-variable RET auto-hide-compile-buffer-delay
): (defcustom auto-hide-compile-buffer-delay 0
"Time in seconds before auto hiding compile buffer."
:group 'compilation
:type 'number
)
compilation-num-*
for warnings and errors count in compilation buffer: (defun hide-compile-buffer-if-successful (buffer string)
(setq compilation-total-time (time-subtract nil compilation-start-time))
(setq time-str (concat " (Time: " (format-time-string "%s.%3N" compilation-total-time) "s)"))
(if
(with-current-buffer buffer
(setq warnings (eval compilation-num-warnings-found))
(setq warnings-str (concat " (Warnings: " (number-to-string warnings) ")"))
(setq errors (eval compilation-num-errors-found))
(if (eq errors 0) nil t)
)
;;If Errors then
(message (concat "Compiled with Errors" warnings-str time-str))
;;If Compiled Successfully or with Warnings then
(progn
(bury-buffer buffer)
(run-with-timer auto-hide-compile-buffer-delay nil 'delete-window (get-buffer-window buffer 'visible))
(message (concat "Compiled Successfully" warnings-str time-str))
)
)
)
(make-variable-buffer-local 'compilation-start-time)
(defun compilation-started (proc)
(setq compilation-start-time (current-time))
)