2

Some compilers support color output, which I find helpful when viewing compiler output.

GCC for example supports -fdiagnostics-color=always.

I would like to build using emacs's compile mode which has the advantage of being able to jump to the next/previous error, while seeing any warnings/errors in color.

Is this possible?

0

4 Answers 4

4

Not sure when it was added but nowdays you can simply do:

  (add-hook 'compilation-filter-hook 'ansi-color-compilation-filter)
1

Look at the package xterm-color.

It can be configured to make the compilation buffer understand the ansi codes that change color. These codes are the ones used by gcc.

Its setup for compilation from that documentation is

(setq compilation-environment '("TERM=xterm-256color"))

(defun my/advice-compilation-filter (f proc string)
  (funcall f proc (xterm-color-filter string)))

(advice-add 'compilation-filter :around #'my/advice-compilation-filter)
1
  • 1
    Thanks for the answer, I found some possible improvements though. Emacs' built-in ansi-color-apply can be used instead of depending on the xterm-color package, and I needed to set (setq compilation-max-output-line-length nil) otherwise some terminal output would get replaced with [...], especially when output was written onto the same line (some build systems do this).
    – ideasman42
    Commented Jul 14, 2022 at 14:27
1

The package fancy-compilation sets up support for ANSI colors.


This is not built into Emacs however Emacs has functionality for converting ANSI-escape sequences.

Once ANSI colors are supported, you may wish to override the background/foreground color too (to ensure good contrast with colored text).

While you can setup your own compilation filters and theme overrides, you may prefer to use a package that enables this with a single step.


Thanks to @mmmmmm, I wrote fancy-compilation with his answer as a starting point.

0

All the solutions for the ansi-color package did not work for me with CMake-generated makefiles. Only some escape sequences were translated. Apparently the filter function was overwhelmed. Finally here is the more modern solution that works properly:

(unless
    (ignore-errors
      (if (require 'xterm-color nil t)
          (progn (setq compilation-environment '("TERM=xterm-256color"))
                 (defun my/advice-compilation-filter (f proc string)
                   (funcall f proc (xterm-color-filter string)))
                 (advice-add 'compilation-filter :around #'my/advice-compilation-filter))
        (message "WARNING: xterm-color package not installed")
        (require 'ansi-color)
        (add-hook 'compilation-filter-hook 'ansi-color-compilation-filter)
        ))
  (message "WARNING: error installing compilation buffer filters"))

We do a fallback here to the classic solution in case the xterm-color package is not installed.

Tested with Emacs 29.1 and CMake 3.28.1

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.