[TxMt] new command: Universal Un/Comment (BBEdit-ish)

Ralph Pöllath lists at poellath.org
Tue Dec 21 17:26:07 UTC 2004

Thanks Eric,

Works as advertised! I'd like to see support for Java  
("java"=>["//","","/*","*/"]) and XML (same as html). This command  
would be even better if instead of looking at the filename extension,  
it would use the language chosen from menu "View -> Syntax Highlight  
as", because then it could work with unsaved files. I'm not sure wether  
that's currently possible, though.


On 21.12.2004, at 16:46, Eric Hsu wrote:

> Hi folks,
> I missed a command from BBEdit that let you comment and uncomment text  
> magically for different languages with the same keystroke  
> (Text->Un/Comment), so I wrote a version for TextMate. If there is  
> enough interest, I will release commented source code and make it into  
> a nice .tmbundle.  It currently only supports perl, php, html, plist  
> and tex. It is trivial to add support for other languages, which I'll  
> do on request (or you can easily figure out yourself, I think... if  
> you do, send me an e-mail so I can update mine!).
> If the selection's first line is a comment; if so, it uncomments the  
> rest of the selection.
> If not, it comments the whole selection, line by line. It tries to  
> keep the selection's indentation.
> It parses the filetype from the filename ending to figure out what are  
> comments. If a language has more than one kind of delimiter pair, it  
> will use the first pair in the list for commenting, and will search  
> for all listed pairs for uncommenting. Notice this last feature  
> improves on BBEdit's Text->Un/Comment functionality. The delimiters  
> are stored in $a in the format "a,b,c"=>[$first, $last], where each of  
> the file endings .a, .b and .c will use that delimiter pair. Roll your  
> own!
> This version is actually better than BBEdit's, in my opinion, because
> 1. it can uncomment many different comment formats for silly languages  
> like PHP that support several; and
> 2. it places comment delimiters at the original level of indent (and  
> not the first column like BB does); and
> 3. we can all customize it.
> Textmate is powerful!
> best wishes, Eric
> --
> To install it as a command:
> Before: do nothing
> Command:
> perl -e  
> '$a={"pl,pm"=>["#",""],"plist,c"=>["/*","*/"],"html,htm"=>["<!--","-- 
> >"],"tex,ltx"=>["%",""],"php"=>["#","","/*","*/","<!--","-->","/ 
> /",""]};while(($k,$v)=each(%$a)){foreach(split(/\s*,\s*/ 
> ,$k)){$c{"$_"}=$v;}}$_=shift at ARGV;($t)=/\.(.*?)$/; 
> ($s,$f, at etc)=@{$c{$t}};$b=0;while(<STDIN>){push at in,$_;($in)=/^([  
> \t]*)/;$inl=0;foreach$j(1..(length($in))){$ch=substr($in,$j-1,1); 
> if($ch eq"  
> "){$inl++;}else{unless($inl%4){$inl+=4;}else{$inl+=$inl%4;}}}unless($i) 
> {$ind=$in;$indl=$inl;$i++;}else{if($inl<$indl||$in  
> eq""){$ind=$in;$indl=$inl;}}}$i=0; 
> foreach(@in){if(chomp){$n="\n";}unless(/\S/){$o.=$_."\n"; 
> next;}unless($b){$b=1;foreach$d(@{$c{$t}}){$i=1-$i;$d=quotemeta($d); 
> if($i){if(/^\s*$d/){$y=1;$s=$d;}}elsif($y){$f=$d;last;}}}if($y){s/ 
> ^(\s*)$s(\ )?/$1/;s/(\  
> )?$f(\s*)$/$1/;$o.=$_.$n;}else{s/^$ind//;$o.=$ind.$s." $_  
> ".$f.$n;}}print$o;' $TM_FILEPATH
> STDIN: Selected
> STDOUT: Replace Selected
> ists.macromates.com/mailman/listinfo/textmate

More information about the textmate mailing list