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

Russell E. Owen rowen at cesmail.net
Tue Dec 21 20:09:55 UTC 2004

In article <BB9A086A-5375-11D9-8F1A-000D93589AF6 at macromates.com>,
 Allan Odgaard <allan at macromates.com> wrote:

> On Dec 21, 2004, at 16:46, Eric Hsu wrote:
> > 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. [...]
> Nice! :)
> I changed the extension-grabbing code to this: ³($t)=/\.([^.]*)$/² to  
> handle paths with multiple dots and I quoted TM FILEPATH (for paths  
> with spaces).
> I also added: "cc,mm"=>["//","","/*","*/"] and m+h to the plist,c types.
> So it's now: perl -e  
> '$a={"cc,mm"=>["//","","/*","*/"],"pl,pm"=>["#",""],"plist,c,m,h"=>["/ 
> *","*/"],"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"
> > If there is enough interest, I will release commented source code and  
> > make it into a nice .tmbundle.
> Sounds like when you do, I should add that bundle to what's included by  
> default with TM (if you don't mind)! :)

It might be worth discussing the feature before making it part of 

I'm really pleased to see this script, but would like to suggest an 
alternative implementation:
- Separate commands for comment/uncomment, rather than trying to guess 
which is wanted. The philosophy of "explicit is better than implicit". 
This has significant advantages.
- No ambiguity about what should be done. Many people temporarily 
comment out blocks of code AND may include the leading comments. Having 
separate commands makes it painless and reliable. Guessing leads to the 
wrong thing happening. Lack of this is one of the reasons I dislike 
- No need for the code to be fancy and try to guess!

Typically one would use a certain command keystroke (e.g. option-;) to 
comment, and use the same keystroke plus the option key to get 
uncomment. Thus nothing really extra to memorize.

I will also argue for putting the comment character in the first column 
(or offering an option for this). I use comment/uncomment solely to 
temporarily comment out blocks of code, and having the comment char in 
the 1st column makes such code easy to spot. Indenting the comment char 
makes it look like any other comment, hiding the block.

Just my two bits. What do others think?

-- Russell

More information about the textmate mailing list