<?xml version='1.0' encoding='UTF-8'?><?xml-stylesheet href="http://www.blogger.com/styles/atom.css" type="text/css"?><feed xmlns='http://www.w3.org/2005/Atom' xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/' xmlns:georss='http://www.georss.org/georss' xmlns:gd='http://schemas.google.com/g/2005' xmlns:thr='http://purl.org/syndication/thread/1.0'><id>tag:blogger.com,1999:blog-1653471358572596263</id><updated>2011-04-21T14:00:14.252-07:00</updated><category term='emacs'/><title type='text'>(apply 'rant-about '(code stuff))</title><subtitle type='html'></subtitle><link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='http://torgomatic.blogspot.com/feeds/posts/default'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1653471358572596263/posts/default?max-results=100'/><link rel='alternate' type='text/html' href='http://torgomatic.blogspot.com/'/><link rel='hub' href='http://pubsubhubbub.appspot.com/'/><author><name>Sam Merritt</name><uri>http://www.blogger.com/profile/09862505567013690135</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><generator version='7.00' uri='http://www.blogger.com'>Blogger</generator><openSearch:totalResults>3</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>100</openSearch:itemsPerPage><entry><id>tag:blogger.com,1999:blog-1653471358572596263.post-2843519964040348293</id><published>2009-03-04T14:05:00.000-08:00</published><updated>2009-03-04T14:44:28.188-08:00</updated><title type='text'>Color testrun output (I mean it this time!)</title><content type='html'>So, the last code I posted did yield colorful test output, but it broke the &lt;code&gt;next-error&lt;/code&gt; and &lt;code&gt;goto-error&lt;/code&gt; functions, which are incredibly useful. &lt;code&gt;goto-error&lt;/code&gt; 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.&lt;br /&gt;&lt;br /&gt;&lt;code&gt;goto-error&lt;/code&gt; uses text properties to determine whether there's an error on that line or not, so turning off font-lock-mode turns off &lt;code&gt;goto-error&lt;/code&gt;. Thus, font-lock-mode must stay on. However, &lt;code&gt;ansi-color-apply&lt;/code&gt; gives you text with its 'face' property set, and font-lock-mode overwrites the 'face' property when it fontifies the buffer.&lt;br /&gt;&lt;br /&gt;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.&lt;br /&gt;&lt;br /&gt;So all we have to do is take the output of &lt;code&gt;ansi-color-apply&lt;/code&gt;, turn its 'face' property into 'font-lock-face', and make *that* the string that &lt;code&gt;compilation-filter&lt;/code&gt; gets.&lt;br /&gt;&lt;br /&gt;&lt;pre style="background-color: #000000;"&gt;&lt;br /&gt;(&lt;span style="color: #00ffff;"&gt;defadvice&lt;/span&gt; &lt;span style="color: #87cefa;"&gt;compilation-filter&lt;/span&gt; (before ansify-compilation-output activate)&lt;br /&gt;  (&lt;span style="color: #00ffff;"&gt;with-current-buffer&lt;/span&gt; (process-buffer (ad-get-arg 0))&lt;br /&gt;    (&lt;span style="color: #00ffff;"&gt;let&lt;/span&gt; ((colorstr (ansi-color-apply (ad-get-arg 1))))&lt;br /&gt;      (ad-set-arg 1 (font-lock-proof colorstr 0)))))&lt;br /&gt;&lt;br /&gt;(&lt;span style="color: #00ffff;"&gt;defun&lt;/span&gt; &lt;span style="color: #87cefa;"&gt;font-lock-proof&lt;/span&gt; (string start)&lt;br /&gt;  (&lt;span style="color: #00ffff;"&gt;cond&lt;/span&gt;&lt;br /&gt;   ((&amp;gt;= start (length string)) &lt;span style="color: #ffa07a;"&gt;""&lt;/span&gt;)&lt;br /&gt;   (t&lt;br /&gt;    (&lt;span style="color: #00ffff;"&gt;let*&lt;/span&gt; ((end (next-property-change start string (length string)))&lt;br /&gt;           (s (substring string start end)))&lt;br /&gt;      (set-text-properties 0&lt;br /&gt;                           (length s)&lt;br /&gt;                           (substitute 'font-lock-face 'face (text-properties-at 0 s))&lt;br /&gt;                           s)&lt;br /&gt;      (concat s (font-lock-proof string end))))))&lt;br /&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;(fancy highlighted code courtesy of &lt;a href="http://www.emacswiki.org/emacs/Htmlize"&gt;htmlize.el&lt;/a&gt;)&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1653471358572596263-2843519964040348293?l=torgomatic.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://torgomatic.blogspot.com/feeds/2843519964040348293/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://torgomatic.blogspot.com/2009/03/color-testrun-output-i-mean-it-this.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1653471358572596263/posts/default/2843519964040348293'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1653471358572596263/posts/default/2843519964040348293'/><link rel='alternate' type='text/html' href='http://torgomatic.blogspot.com/2009/03/color-testrun-output-i-mean-it-this.html' title='Color testrun output (I mean it this time!)'/><author><name>Sam Merritt</name><uri>http://www.blogger.com/profile/09862505567013690135</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1653471358572596263.post-4422442153551853908</id><published>2009-02-21T17:41:00.000-08:00</published><updated>2009-02-21T17:50:32.577-08:00</updated><title type='text'>Addendum and bugfix</title><content type='html'>In my last post, I had this code that would give colorful output in compilation mode. Well, it turns out that only works if your test output is very short and comes very quickly. For the toy Ruby spec I was using to develop it, that's true. For real specs, it's very much not.&lt;br /&gt;&lt;br /&gt;Here's the original code:&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;(defadvice compilation-filter (before ansify-compilation-output activate)&lt;br /&gt;  (with-current-buffer (process-buffer (ad-get-arg 0))&lt;br /&gt;    (font-lock-mode -1)&lt;br /&gt;    (ad-set-arg 1 (ansi-color-apply (ad-get-arg 1))))&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;The problem is that turning off font-lock-mode strips all the colors from the current buffer's text. If your process produces multiple pieces of output, you get this effect sort of like a line of sparks. The rightmost few are bright and colorful, but they quickly fade out and become dull gray.&lt;br /&gt;&lt;br /&gt;The fix is to turn off font-lock-mode only when it's on. That is, like so:&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;(defadvice compilation-filter (before ansify-compilation-output activate)&lt;br /&gt;  (with-current-buffer (process-buffer (ad-get-arg 0))&lt;br /&gt;    (if font-lock-mode (font-lock-mode -1))&lt;br /&gt;    (ad-set-arg 1 (ansi-color-apply (ad-get-arg 1)))))&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;Now the compilation process's output starts out and stays colorful.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1653471358572596263-4422442153551853908?l=torgomatic.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://torgomatic.blogspot.com/feeds/4422442153551853908/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://torgomatic.blogspot.com/2009/02/addendum-and-bugfix.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1653471358572596263/posts/default/4422442153551853908'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1653471358572596263/posts/default/4422442153551853908'/><link rel='alternate' type='text/html' href='http://torgomatic.blogspot.com/2009/02/addendum-and-bugfix.html' title='Addendum and bugfix'/><author><name>Sam Merritt</name><uri>http://www.blogger.com/profile/09862505567013690135</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1653471358572596263.post-8175041531423555614</id><published>2009-02-15T15:18:00.000-08:00</published><updated>2009-02-18T09:36:22.757-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='emacs'/><title type='text'>Getting colorful testrun output in Emacs</title><content type='html'>When I run my RSpec tests from the command line, the output is colorful, but from within Emacs, it's all just plain white text. Something must be done!&lt;br /&gt;&lt;br /&gt;The first and most-obvious problem: spec (the testrunner) needs the -c flag before it produces colorful output. Easy fix! Update my test-running function to throw a -c in there, and presto! Beautiful output:&lt;br /&gt;&lt;code&gt;&lt;br /&gt;Compilation started at Sun Feb 15 16:35:33&lt;br /&gt;&lt;br /&gt;spec spec/thingy.rb -c&lt;br /&gt;[32m.[0m[31mF[0m[32m.[0m[32m.[0m[32m.[0m&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;Great. spec emitted a bunch of ANSI color escape sequences (yay!), and Emacs failed to interpret them (boo!).&lt;br /&gt;&lt;br /&gt;Googling revealed that compilation-mode is derived from comint-mode, so I should just set up comint-mode to have color output. It's well-documented and easy to do:&lt;br /&gt;&lt;code&gt;&lt;br /&gt;(setq ansi-color-for-comint-mode t)&lt;br /&gt;(add-hook 'comint-output-filter-functions 'ansi-color-process-output)&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;This let me run a shell from within Emacs and have &lt;code&gt;ls --color&lt;/code&gt; show color in it. Unfortunately, this did absolutely nothing for my test output, which remained full of raw escape sequences.&lt;br /&gt;&lt;br /&gt;The problem is in compile.el. It calls &lt;code&gt;comint-exec&lt;/code&gt;, which sets the process's output filter to &lt;code&gt;comint-output-filter&lt;/code&gt;, which in turn exposes a variety of useful hooks, including the aforementioned &lt;code&gt;comint-output-filter-functions&lt;/code&gt;. Unfortunately, after calling &lt;code&gt;comint-exec&lt;/code&gt;, &lt;code&gt;compile&lt;/code&gt; sets the output filter to &lt;code&gt;compilation-filter&lt;/code&gt;, which only exposes one hook, and that one gets called *after* the string's been inserted, with no arguments. It's not very useful.&lt;br /&gt;&lt;br /&gt;Fortunately, Emacs's advice system makes it easy to modify the arguments that &lt;code&gt;compilation-filter&lt;/code&gt; gets, so it's easy to turn those escape sequences into colors.&lt;br /&gt;&lt;code&gt;&lt;br /&gt;(defadvice compilation-filter (before ansify-compilation-output activate)&lt;br /&gt;  (ad-set-arg 1 (ansi-color-apply (ad-get-arg 1))))&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;Now I run my tests, and I get... no escape sequences or colors? Huh?&lt;br /&gt;&lt;br /&gt;It turns out that you can't insert text with color properties (or something) into a buffer with font-lock-mode on. Since I have that on everywhere, I just need to turn off font-lock-mode in the compilation buffer. Fortunately, a process filter's first argument is the process.&lt;br /&gt;&lt;code&gt;&lt;br /&gt;(defadvice compilation-filter (before ansify-compilation-output activate)&lt;br /&gt;  (with-current-buffer (process-buffer (ad-get-arg 0))&lt;br /&gt;    (font-lock-mode -1)&lt;br /&gt;    (ad-set-arg 1 (ansi-color-apply (ad-get-arg 1)))))&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;Now I've got colorful test output in Emacs.&lt;br /&gt;&lt;br /&gt;(Why not add something to &lt;code&gt;compilation-mode-hook&lt;/code&gt;? It's because &lt;code&gt;compilation-mode&lt;/code&gt; unconditionally turns on font-lock-mode after running &lt;code&gt;compilation-mode-hook&lt;/code&gt;.)&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1653471358572596263-8175041531423555614?l=torgomatic.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://torgomatic.blogspot.com/feeds/8175041531423555614/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://torgomatic.blogspot.com/2009/02/getting-color-compilation-output-in.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1653471358572596263/posts/default/8175041531423555614'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1653471358572596263/posts/default/8175041531423555614'/><link rel='alternate' type='text/html' href='http://torgomatic.blogspot.com/2009/02/getting-color-compilation-output-in.html' title='Getting colorful testrun output in Emacs'/><author><name>Sam Merritt</name><uri>http://www.blogger.com/profile/09862505567013690135</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry></feed>
