13 Oct 2026

Emacs salmagundi, 2026-10-11

Combining lists of compilation commands

To my delight I found that emacs-multi-compile will combine the of all matches compilation commands!

I added this to my setup

(add-to-list 'multi-compile-alist
               '((mes/is-in-git-project) . (("git clean -n" "git clean -ndx" (project-root (project-current)))
                                            ("git clean -f" "git clean -fdx" (project-root (project-current))))))

and those commands are now shown together with the ones I defined for Haskell project (see my previous post) when both match.

Recognising a git project

To add the git commands to multi-compile-list I needed a function to determine if the current buffer is in a git working tree. I failed to find something fitting so I ended up adding this little function.

(defun mes/is-in-git-project ()
  "Check if we're currently in a git project."
  (when-let* ((proj-dir (project-root (project-current))))
    (vc-git-root proj-dir)))

Taming compilation mode buffers

I'm using display-buffer-alist to control how Emacs places buffers. For compilation mode I had the following actions.

(display-buffer-reuse-mode-window display-buffer-in-side-window)
(mode compilation-mode)
(window-height . 0.25)
(side . bottom)
(post-command-select-window . t)

That last command made switching buffers with consult-buffer a bit irritating as previewing would interrupt the buffer switching by selecting the compilation window. However, I really want the compilation window selected after a compilation. Or rather, as I realised after reflecting a bit, I want the compilation window selected if there are errors.

Compilation mode has a way of running functions when a compilation finishes so I defined two functions and added them to compilation-finish-functions.

(defun mes/close-successful-compilation (buf msg)
  "Close compilation buffer after success."
  (when (and (buffer-live-p buf)
             (with-current-buffer buf (eq major-mode 'compilation-mode))
             (string-match "finished" msg))
    (run-with-timer 3 nil
                    (lambda ()
                      (kill-buffer buf)))))

(defun mes/select-failed-compilation (buf msg)
  "Select compilation buffer after failed compilation."
  (when (and (buffer-live-p buf)
             (with-current-buffer buf (eq major-mode 'compilation-mode))
             (string-match "abnormally" msg))
    (select-window (get-buffer-window buf))))

  (add-to-list 'compilation-finish-functions #'mes/close-successful-compilation)
  (add-to-list 'compilation-finish-functions #'mes/select-failed-compilation)

With that a successful compilation is closed automatically after 3s and on a failed compilation the compilation buffer is selection.

Tags: emacs
Comment here.