16

I want to auto close the compile buffer when there is no error and no warning,but i want to show it when there is warnings. Anyone can help me? This code from emacswiki only do the first requirement. How to change it?

  ;; Helper for compilation. Close the compilation window if
  ;; there was no error at all.
  (defun compilation-exit-autoclose (status code msg)
    ;; If M-x compile exists with a 0
    (when (and (eq status 'exit) (zerop code))
      ;; then bury the *compilation* buffer, so that C-x b doesn't go there
      (bury-buffer)
      ;; and delete the *compilation* window
      (delete-window (get-buffer-window (get-buffer "*compilation*"))))
    ;; Always return the anticipated result of compilation-exit-message-function
    (cons msg code))
  ;; Specify my function (maybe I should have done a lambda function)
  (setq compilation-exit-message-function 'compilation-exit-autoclose)
Chen Levy
  • 15,438
  • 17
  • 74
  • 92
Iceman
  • 367
  • 1
  • 3
  • 5
  • @Thomas this isn't the key problem – Iceman Jun 15 '12 at 01:48
  • 1
    It might be useful to know which compiler you're running because you might be able to use the `msg` parameter to check if there are errors or warnings. – Thomas Jun 15 '12 at 01:56
  • You could try adding another condition to the and clause to look for the string 'warning' in the **compilation** buffer. Or whatever other string that your compiler uses to indicate warnings. – vpit3833 Jun 15 '12 at 07:01
  • @vpit3833 i just tried it , but it doesn't work. – Iceman Jun 16 '12 at 21:42

3 Answers3

22

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 "
 (when (and
         (buffer-live-p buffer)
         (string-match "compilation" (buffer-name buffer))
         (string-match "finished" string)
         (not
          (with-current-buffer buffer
            (goto-char (point-min))
            (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)
jpkotta
  • 9,237
  • 3
  • 29
  • 34
  • this is cool, but why does it leave the window open after the compilation buffer closes? this window then remains open until I move the cursor, then it suddenly closes. what causes this behavior? – johnbakers May 10 '16 at 09:07
  • @johnbakers: Because all it does is switch the buffer in the window, leaving the window layout unchanged. I generally don't like Emacs to change my window layout. Try playing with `delete-windows-on` instead of `switch-to-prev-buffer`. – jpkotta May 10 '16 at 16:17
  • this is a really neat function, but I'm wondering if I can make it always popup the compilation in a specific window rather than it automatically creating a new window at the bottom of my screen? – johnbakers May 10 '16 at 17:50
  • @johnbakers: The code in my answer doesn't pop up the window or create the buffer, that's done by `compile`; my code is run once the compilation finishes (which should be pretty obvious because it's added to `compilation-finish-functions`). If you want more control over how the window is created, look into the `shackle` package. – jpkotta May 10 '16 at 20:46
2

jpkotta, it does work most of the times. Sometimes, even if there is a warning, it doesn't switch to compilation buffer. So I made a change to your form & it does work now:

(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
          **(goto-char 1)**
          (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)
duelin markers
  • 553
  • 3
  • 14
Punit Arya
  • 146
  • 1
  • 3
0

I've tweaked above answers with better logic and tested it, working perfectly:

  1. Add Hooks to functions:
  (add-hook 'compilation-start-hook 'compilation-started)
  (add-hook 'compilation-finish-functions 'hide-compile-buffer-if-successful)
  1. Auto Hide Compile Buffer Delay Customizable Variable (via 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
  )
  1. Get time taken in compilation and use 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))
  )

Demo

nitin
  • 21
  • 1
  • 2