hi folks,
I am startled but gratified at the response to the Un/Comment hack. I added a number of fixes and options people have been asking for and packaged it up into a .tmbundle so you don't have to copy in the hideous Perl 1-liner (but you can... attached below). In the bundle, I also included the source code, which is released as Niceware. That's like the Perl Artistic License, except that you have to be nice to me when you criticize the code.
The bundle lives at http://math.sfsu.edu/hsu/textmate.
Changes - I added some options. To invoke them, put them on the command line, e.g.
perl -e '...' "$TM_FILEPATH" first-column comment .tex
- so if you want separate comment/uncomment, make two commands with one option 'comment' and the other 'uncomment' - I added a bunch of filetype endings, including the ones in Chris T's beautiful Ruby script. - fixed Ollivier Robert's newline bug - Drew McLellan: I added CSS, but sadly, I can only comment each line, not the whole thing. I hope that is still useable.
Options # 'first-column' puts the first comment marker at the line start # 'comment' always comments (for people who want the smartness, but not the toggleness) # 'uncomment' always uncomments. If you foolishly use both comment and this, comment wins. # 'comment-whitespace' comments whitespace which we normally skip. # '.tex' will force the use of TeX style comments. You can substitute your favorite ending, like '.java' or '.pl'. The leading dot is important. Last type listed wins. # 'toggle' changes the comment status line by line. Normally, the whole thing is a block.
best wishes, Eric ---
If you install the thing as a tmbundle, you can also install it as a command as
Before: do nothing Command: ~/Library/Application\ Support/TextMate/Bundles/UnComment.tmbundle/comment-toggle.pl "$TM_FILEPATH" STDIN: Selected STDOUT: Replace Selected
To install it as a command as a one-liner, with no path issues:
Before: do nothing Command: perl -e '$markers={"pl,pm,rb"=>["#",""],"plist,c,css,m+h,cp,cpp,h,p,pas,js,htc,c++,h++"=>["/*","*/","//",""],"java,cc,mm"=>["//","","/*","*/"],"html,htm,xml"=>["<!--","-->"],"bib,ltx,tex,ps,eps"=>["%",""],"php"=>["#","","/*","*/","<!--","-->","//",""],"mli,ml,mll,mly"=>["(*","*)"],"script,adb,ads,sql,ddl,dml"=>["--",""],"f,f77"=>["C ",""],"inf,f90"=>["!",""],};$tab=4;while(($k,$v)=each(%$markers)){foreach(split(/\s*,\s*/,$k)){$c{"$_"}=$v;}}$_=shift@ARGV;($type)=/.([^.]*)$/;foreach$option(@ARGV){if($option=~/^.(.*)$/){$type=$1;}$opt->{$option}++;}unless($c{"$type"}){$type="pl";}($start,$finish,@etc)=@{$c{$type}};$isntfirst=0;while(<STDIN>){push@in,$_;($indent)=/^([ \t]*)/;$indentl=0;foreach$j(1..(length($indent))){$ch=substr($indent,$j-1,1);if($ch eq" "){$indentl++;}else{unless($indentl%$tab){$indentl+=$tab;}else{$indentl+=$indentl%$tab;}}}unless($isntfirst){$indentmin=$indent;$indentminl=$indentl;$isntfirst++;}else{if($indentl<$indentminl||$indent eq""){$indentmin=$indent;$indentminl=$indentl;}}}$isntfirst=0;foreach(@in){$lineend="";if($opt->{"toggle"}){$iscomment=$isfirstofpair=$isntfirst=0;}if(chomp){$lineend="\n";}unless(/\S/){unless($opt->{"comment-whitespace"}){$out.=$_."\n";next;}}unless($isntfirst){$isntfirst=1;foreach$delimiter(@{$c{"$type"}}){$isfirstofpair=1-$isfirstofpair;$delimiterq=quotemeta($delimiter);if($isfirstofpair){if(/^\s*$delimiterq/){$iscomment=1;$start=$delimiter;$startq=$delimiterq;}}elsif($iscomment){$finish=$delimiter;$finishq=$delimiterq;last;}}}if($opt->{"uncomment"}){$iscomment=1;}if($opt->{"comment"}){$iscomment=0;}if($iscomment){s/^(\s*)$startq(\ )?/$1/;s/(\ )?$finishq(\s*)$/$1/;$out.=$_.$lineend;}else{if($opt->{"first-column"}){$out.=$start;unless($indentmin){$out.=" ";}$out.="$_";if($finish){$out.=" ".$finish;}$out.=$lineend;}else{s/^$indentmin//;$out.=$indentmin.$start." $_";if($finish){$out.=" ".$finish;}$out.=$lineend;}}}print$out;' "$TM_FILEPATH"
STDIN: Selected STDOUT: Replace Selected