Notify When Compilation Buffers Finish
by Peter Stuart on June 4, 2023
I often compile projects in Emacs using project-compile, and then don’t notice that they finished because the compilation window is no longer visible, so I added the following code to my config. It adds a hook to compilation-mode which alerts me when compilation finishes, only if the compilation buffer is not visible in a focused frame. It requires the alert package.
(defun ps/buffer-visible-in-focused-frame-p (buffer)
"Return t if BUFFER is visible in the focused frame, or nil otherwise."
(let ((windows (get-buffer-window-list buffer nil t)))
(cl-some (lambda (window)
(and (window-live-p window)
(frame-focus-state (window-frame window))))
windows)))
(defun ps/compilation-finish-alert (buffer result)
"Alert the user when a compilation buffer finishes.
If the compilation buffer is visible in the focused frame, the
user will not be alerted."
(unless (ps/buffer-visible-in-focused-frame-p buffer)
(alert (format "Compilation %s" result)
:id (format "ps/compilation-finish-alert-" (buffer-name buffer))
:never-persist t
:buffer buffer)))
(add-hook 'compilation-finish-functions 'ps/compilation-finish-alert)