[TxMt] Re: Xmas Wish

Steve King sking at arbor.net
Mon Dec 22 15:27:50 UTC 2008

Alexy Khrabrov wrote:
> And my wish is an easy way to communicate with command-line processes  
> such as toplevels and compilers, and parse their errors like Emacs  
> does, pointing you back into the text.  Once that exists, TM will have  
> officially superseded Emacs for compiling...  unless it;s possible  
> already?  :)

Good news! Not only is it possible, it's easy.  Open the bundle editor
and create a new command. Set the input to "Nothing" and the output to
"Show as HTML". Define the activation and scope as suits you.

Now, for the command itself.  It needs to execute your compiler, parse
the output, and convert it to HTML. The key is to turn the filename,
line number, and column number into a 'txmt://' URL. When the HTML is
displayed, clicking on such a URL will open the file in TextMate; or, if
the file is already open, simply jump to the appropriate line and column.

You can do this in any scripting language you like. I use Perl for my
example, simply because that's what I'm used to.  So here's a quickie
that I banged out in a few minutes to get you going. This simply
compiles the current file using gcc, highlights errors in red and
warnings in yellow, and makes links back to the source file.

----- Cut Here -----

#! /usr/bin/perl -w
use strict;
use IO::File;
use HTML::Entities;

my $dir  = $ENV{'TM_DIRECTORY'};
my $file = $ENV{'TM_FILENAME'};
my $path = $ENV{'TM_FILEPATH'};
my ($str);

my $cmd = "gcc -o foo $file 2>&1 |";
my $fh = IO::File->new($cmd);

while (<$fh>) {
    if (/^(.*?):(\d+):(\d+):\s*(error|warning):\s*(.*)$/) {
        my $file = $1;
        my $line = $2;
        my $col  = $3;
        my $type = $4;
        my $msg  = $5;
        $msg = encode_entities($msg);
        my $color = ($type eq 'error') ? "red" : "yellow";
        my $url = sprintf 'txmt://open/?url=file://%s&line=%d&column=%d', $path, $line, $col;
        $str = "<span style=\"background-color:$color\"><a href=\"$url\">$file:$line:$col:</a> $type: $msg</span>";
    else {
        $str = encode_entities($_);
    print $str . "<br>\n";

----- Cut Here -----

Enjoy your Christmas present!

Steve King
Sr. Software Engineer
Arbor Networks
+1 734 821 1461
www.arbornetworks.com <http://www.arbornetworks.com/>

More information about the textmate mailing list