-(setq compilation-exit-message-function
- (lambda (status code msg)
- "Close the compilation window if successful."
- ;; if M-x compile exits with 0
- (when (and (eq status 'exit) (zerop code))
- (bury-buffer)
- (delete-window (get-buffer-window (get-buffer "*compilation*"))))
- ;; return the result of compilation-exit-message-function
- (cons msg code)))
+(defun amin--compilation-finish-function (buffer outstr)
+ (unless (string-match "finished" outstr)
+ (switch-to-buffer-other-window buffer))
+ t)
+
+(setq compilation-finish-functions #'amin--compilation-finish-function)
+
+(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)