Wednesday, March 4, 2009

Color testrun output (I mean it this time!)

So, the last code I posted did yield colorful test output, but it broke the next-error and goto-error functions, which are incredibly useful. goto-error is the function that lets you hit enter on a line from a stack trace and wind up looking at that line in that file. That's far more useful than just colorful stuff.

goto-error uses text properties to determine whether there's an error on that line or not, so turning off font-lock-mode turns off goto-error. Thus, font-lock-mode must stay on. However, ansi-color-apply gives you text with its 'face' property set, and font-lock-mode overwrites the 'face' property when it fontifies the buffer.

There's a way out, though: font-lock-mode leaves the 'font-lock-face' property alone, and if your text has 'font-lock-face' set, then that acts like its 'face' property.

So all we have to do is take the output of ansi-color-apply, turn its 'face' property into 'font-lock-face', and make *that* the string that compilation-filter gets.


(defadvice compilation-filter (before ansify-compilation-output activate)
(with-current-buffer (process-buffer (ad-get-arg 0))
(let ((colorstr (ansi-color-apply (ad-get-arg 1))))
(ad-set-arg 1 (font-lock-proof colorstr 0)))))

(defun font-lock-proof (string start)
(cond
((>= start (length string)) "")
(t
(let* ((end (next-property-change start string (length string)))
(s (substring string start end)))
(set-text-properties 0
(length s)
(substitute 'font-lock-face 'face (text-properties-at 0 s))
s)
(concat s (font-lock-proof string end))))))



(fancy highlighted code courtesy of htmlize.el)

No comments:

Post a Comment