[TxMt] Reformat comment command

Alan Schussman alan at schussman.com
Wed Aug 23 17:24:11 UTC 2006


As I set up Reformat Comment commands for the languages I use most  
frequently (LaTeX and R), it occurred  to me that maybe there's a way  
to make a single call to rubywrap more generic, so that we don't need  
a command per bundle. This is the result:

#!/usr/bin/env ruby

$LOAD_PATH << "#{ENV["TM_SUPPORT_PATH"]}/lib"
require "escape"

scope = ENV["TM_SCOPE"]
case scope
   when /comment\.(block|line)\.number-sign\./
      cstring = "# "
   when /comment\.(block|line)\.percentage\./
      cstring = "% "
end

flags =  ""
flags += " -p \"#{cstring}\" "
flags += " --retabify" if ENV["TM_SOFT_TABS"] == "NO"

text =`echo -n "#{e_as(STDIN.read).gsub(/[$`]/, '\\\\\0')}" | ruby "# 
{ENV["TM_SUPPORT_PATH"]}/bin/rubywrap.rb" #{flags}`
print e_sn(text)


The parameters are the same as the current command, with the  
exception of scope, which I set to "comment.line, comment.block".

I also added a gsub to the command because it was eating latex math  
and R symbols ($). There's probably a better solution to that. This  
seems to work for me, and should be extended easily by adding lines  
to the case statement for other languages. One advantage is that by  
specifying the comment character based on the scope, it ought to work  
for anything; it catches comments for both bash and perl, for  
instance, without any extra effort. I think it's kind of cool.

-Alan




More information about the textmate mailing list