3
votes

I have my emacs set up so that colors in shell buffers work great. I also use the compile command to run individual test files in my ruby on rails environment But when I do that, the ror test functionality puts lots of shell/terminal escape characters into my compilation buffer. Is there any way to get that stuff to display in terminal colors?

BTW: I searched around and tried some things, but they didn't work.

Thanks!

2
There is a function called ansi-color-process-output. I would try to experiment with compilation mode filters (see compilation-filter-hook, etc) to see if it can be added there. Disclaimer: I do not use ruby.Alex Vorobiev

2 Answers

4
votes

Here's what I have in my .emacs file now. It does not work until the end, but that's OK.

;; This stuff is to ansi-colorize the compilation buffer after a rails test so the terminal colors come through.
(define-derived-mode ansi-compilation-mode compilation-mode "ansi compilation"
  "Compilation mode that understands ansi colors."
  (require 'ansi-color)
  (toggle-read-only 0)
  (ansi-color-apply-on-region (point-min) (point-max)))

(defun colorize-compilation (one two)
  "ansi colorize the compilation buffer."
  (ansi-compilation-mode))

(setq compilation-finish-function 'colorize-compilation)

EDIT

I have switched from using the compile mode to using an async shell command. Here's the code:

(defun run-it ()
  "Run it on the current file."
  (interactive)
  (save-buffer)
  (shell-command
   (format "my_command %s &"
       (shell-quote-argument (buffer-name)))))
(global-set-key "\C-ct" 'run-it)

It saves the buffer first. The & makes it actually interactive so I can enter text in the buffer and the command will get that input. And it colors the command output on the fly, which my compile buffer was not doing.

1
votes

Backing the comment by Alex Vorobiev, which delivered the answer.

Seems you've put a comint-mode aside and with that the ansi-color-process-output filter.

AFAIU fontifying is done on a per-buffer-base, run from an idle-timer resp. triggered by buffer-changes. If enabled in a output-shell, Emacs might hang, as a lot of changes may occur in short time. Therefor fontification is commonly off here. An alternative approach: M-x MY-MODE at the shell-buffer. Which might need some reset to shell environment or re-start then.