I use the compilation-finish-functions hook to perform some actions once compilation finishes. However, unexpectedly to me the hook is also triggered when rgrep completes.

I can't seem to find a more appropriate hook for what I'm after, namely a hook that is triggered only when compile's compilation completes.

Is there such a hook or must I take this into consideration in my hooked functions?

This is how I hook my function (Emacs 25.1.1):

(defun display-piped-output (buffer msg)
  (unless (string-match "^finished" msg)
    (switch-to-buffer "test.log")))

(define-minor-mode moscow-compilation-mode
  "Expect compilation output piped to file"
  :global t
  (if moscow-compilation-mode
      (add-hook 'compilation-finish-functions 'display-piped-output)
    (remove-hook 'compilation-finish-functions 'display-piped-output)))
  • Are you sure rgrep is doing this? compilation-finish-functions is not mentioned anywhere in grep.el.
    – Dan
    Aug 18, 2017 at 17:27
  • @Dan I believe so, but I can certainly dig some more into it! Aug 18, 2017 at 17:28
  • Try it without your init file (emacs -Q). If rgrep does not trigger compilation-finish-functions, bisect your init file recursively until you isolate what's happening.
    – Dan
    Aug 18, 2017 at 17:31
  • @Dan Yes, I can replicate the behavior without init-files; my Emacs version is 25.1.1. Aug 18, 2017 at 17:45
  • Following up on sshaw's answer, note that any command which generates a list of results where each one is associated with a given line from a given file (such that you can navigate to the location in question from that buffer) is almost certainly using the compile machinery behind the scenes. (And of course, you can do the same thing any time you have a similar use-case.)
    – phils
    Oct 1, 2017 at 2:23

1 Answer 1


rgrep and similar use compile, this is why hooks in compilation-finish-functions are triggered. When rgrep successfully completes its message is "finished\n".

You must take this into consideration in your hook by checking buffer:

(defun display-piped-output (buffer msg)
  (unless (and
           (string-match "^finished" msg))
           (not (string= (buffer-name buffer) "*grep*"))
    (switch-to-buffer "test.log")))
  • Depending on what you wish to test for specifically, it may also/alternatively be helpful to test the values of major-mode (e.g. grep-mode) or compilation-arguments within the compilation buffer.
    – phils
    Oct 1, 2017 at 2:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.