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.
Emacs Stack Exchange is a question and answer site for those using, extending or developing Emacs. It only takes a minute to sign up.Sign up to join this community
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 (setq compilation-finish-function (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!")))))
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
This thread is also relevant, in case some one is interested:
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
(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))))))