compile can execute command line, show you result, indicate exit code in mode line and more. How do you get exit code of the executed command from Lisp? Documentation of the function is silent about it.

I've tried to read descriptions of various variables that start with compilation- prefix. No luck. Anyway, exit code is displayed in mode line, so it should be available. Looks like long digging into source code can help here.

Maybe you can save me and future readers time? What is the official (if this word can be used here) way to get exit code?

  • Seems like all the status handling happens in compilation-handle-exit, which is called from process' sentinel, and the return code isn't set anywhere else (though maybe you could find the reference to the process, and then use process-status.
    – wvxvw
    Jul 19, 2015 at 16:08

1 Answer 1


There is no variable set to the last error code. Such a thing can be emulated though by setting compilation-exit-message-function to something custom:

(defvar compilation-exit-message-function nil "\
If non-nil, called when a compilation process dies to return a status message.
This should be a function of three arguments: process status, exit status,
and exit message; it returns a cons (MESSAGE . MODELINE) of the strings to
write into the compilation buffer, and to put in its mode line.")

With the given information it's trivial to write some code to achieve what you're after:

(defvar my-compilation-exit-code nil)
(defun my-compilation-exit-message-function (status_ code message)
  (setq my-compilation-exit-code code)
  (cons message code))

(setq compilation-exit-message-function 'my-compilation-exit-message-function)

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.