45

When working in Emacs, I use the compile command (F12 by default) to run programs. When I run Cucumber in Emacs, Cucumber spits out ANSI colors that the Emacs compilation mode doesn't interpret. The result is ugly and hard to read. Here's a snippet of the *compilation* buffer showing the ugly:

^[[31m(::) failed steps (::)^[[0m

The command I'm using:

( cd ~/lab/rails/todolist && rake cucumber:all )

Versions:

  • Emacs 23.1
  • Cucumber 0.8.3
  • Cucumber-rails 0.3.2

The world would be sunshine and birds singing if I could:

  • Get Emacs to interpret ANSI color codes in its compilation buffer, or
  • Get Cucumber to stop spitting out ANSI color codes

Any ideas?

Wayne Conrad
  • 103,207
  • 26
  • 155
  • 191

3 Answers3

77

I use this to turn on ansi color interpretation in my compilation buffer:

(require 'ansi-color)
(defun colorize-compilation-buffer ()
  (let ((inhibit-read-only t))
    (ansi-color-apply-on-region (point-min) (point-max))))
(add-hook 'compilation-filter-hook 'colorize-compilation-buffer)
ataylor
  • 64,891
  • 24
  • 161
  • 189
  • Exactly what I was looking for! Unfortunately, in my environment (cygwin-xemacs) it kroaked on (buffer-read-only #) with "Error in process filter", which seems very strange. Any ideas? – thoni56 Jul 25 '10 at 12:01
  • This worked great for me, too. I also added `(linum-mode 0)` (and, at first attempt `(line-number-mode 0)`, which didn't work, but I left in anyway, and might work for someone else), after `(toggle-read-only)`, which makes me even that much happier (a bunch of lines that were just barely wrapping now don't). – lindes Mar 05 '11 at 10:04
  • I decided to add some colour to my makefiles output and hit this issue in emacs. Great solution! – Maxim Egorushkin Feb 28 '14 at 11:30
  • 11
    For modern Emacsen, one should let-bind `inhibit-read-only` to `t`, instead of calling `toggle-read-only`. –  Mar 07 '14 at 10:26
  • Any way to make this work even as new text is being added? I tested that on the result of `ack`, but the hook seems to be run before the content fills the buffer, so the content is no interpreted. – Gauthier May 19 '16 at 12:14
22

I improve code so it doesn't pollute M-x grep like commands and more efficient:

(ignore-errors
  (require 'ansi-color)
  (defun my-colorize-compilation-buffer ()
    (when (eq major-mode 'compilation-mode)
      (ansi-color-apply-on-region compilation-filter-start (point-max))))
  (add-hook 'compilation-filter-hook 'my-colorize-compilation-buffer))
gavenkoa
  • 45,285
  • 19
  • 251
  • 303
7

As of 2023, the most modern way appears to be the xterm-color Emacs package.

  1. Execute M-x package-install with xterm-color.

  2. Add the following lines to your ~/.emacs or ~/.emacs.d/init.el:

(require 'xterm-color)
(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)

(See xterm-color documentation.)

Note that this will provide an error message if xterm-color was not installed properly. This is strongly advised, because on an incomplete Emacs installation it will clearly explain to you what's wrong, instead of leaving you wondering why the colors don't work.

However, if you really prefer to not being informed if xterm-color is missing, use instead:

(when (require 'ansi-color nil t)
  (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))
vog
  • 23,517
  • 11
  • 59
  • 75
  • This worked well for coloring my pytest compilation commands, but broke my ag-mode - it lost interactivity and slowed down. – simno Dec 30 '20 at 15:02
  • @simno I'd propose to file a bug report: https://github.com/atomontage/xterm-color/issues – vog Dec 30 '20 at 20:06
  • There's one already for `rg.el` and `ag.el` packages: https://github.com/dajva/rg.el/issues/65. Both packages rely on matching terminal color escapes and do not provide option to ignore highlighting. I'd switch off `my/advice-compilation-filter` for specifically `ag-mode`, but I'll have to figure it out. – simno Jan 02 '21 at 15:35
  • This didn't work for sbt color output in the *compilation* buffer for me. The escape codes still printed. – Jack Viers Apr 26 '22 at 16:14