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

3 Answers 3

2

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
    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.

Your Answer

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

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