emacsprocessbufferelispcompile-mode

Don't display *compilation* buffer in Emacs until the process exits with error or warning


I am looking for a way to have the Emacs compilation buffer triggered by M-x compile, M-x recompile or some compile on save script only appear when the compilation exits either with an error or a warning.

Note that I am not looking for a way to close the compile buffer if there are no errors or warnings as described in [1]. No I want the buffer to never appear until the compilation is fully finished and only appear if there is an error or warning to display.

The reasons are simple: The flickering compile buffer is disturbing and rearranges the position of the code on the screen. This becomes more annoying if you have compile on save turned on.

The compile buffer contains many different types of compile processes from make to pdflatex so it would be great if the function which determines whether the buffer should be displayed works across the board.

[1] emacs compile buffer auto close?


Solution

  • Looks like you can achieve what you want through temporarily disabling display-buffer across compilation-start.

    This is a combination of what sds said and something posted on the comments @ here

    The comment there had a nasty problem with point jumping in the original source buffer that I appear to have worked out by also blocking set-window-point and goto-char. It feels like a dirty hack, but is working so far. YMMV!

    (defun brian-compile-finish (buffer outstr)
      (unless (string-match "finished" outstr)
        (switch-to-buffer-other-window buffer))
      t)
    
    (setq compilation-finish-functions 'brian-compile-finish)
    
    (require 'cl)
    
    (defadvice compilation-start
      (around inhibit-display
          (command &optional mode name-function highlight-regexp)) 
      (if (not (string-match "^\\(find\\|grep\\)" command))
          (flet ((display-buffer)
             (set-window-point)
             (goto-char)) 
        (fset 'display-buffer 'ignore)
        (fset 'goto-char 'ignore)
        (fset 'set-window-point 'ignore)
        (save-window-excursion 
          ad-do-it))
        ad-do-it))
    
    (ad-activate 'compilation-start)
    

    Now you should see that all compile buffers will only be shown if outstr doesn't return a successful finish status OR the invoked command started with "find" or "grep."