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

Lang Riley lang at langriley.com
Tue Dec 21 19:38:54 UTC 2004


Chris,
For those of us new to Ruby, do we need to wrap your script in :

ruby -e '<your script>'?

Thanks
On Dec 21, 2004, at 11:02 AM, Chris Thomas wrote:

> On Dec 21, 2004, at 7:46 AM, Eric Hsu wrote:
>
>> 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.
> Here's what I use (requires Ruby 1.8). (I typically want to preserve 
> the existing inline comments, so I don't personally find those first 
> two properties desirable.)
>
> file_extension = File.extname(ARGV[0])
> file_extension = file_extension[1..file_extension.size]
>
> # default to shell-style comments
> comment_strings = ['#','']
>
> StringMap = {
>  ['c','cp','cpp','h','m','mm','p','pas',
>  'java','js','htc','c++','h++'] => ['//',''], # C family and some 
> variants of Pascal
>  ['f','f77'] => ['C ',''], # Fortran
>  ['inf','f90'] => ['!',''], # Inform, modern Fortran
>  ['script','adb','ads','sql','ddl',
>  'dml'] => ['--',''], # AppleScript, Ada, SQL
>  ['html','xml','plist','php'] => ['<!--','-->'], # XML, HTML
>  ['mli','ml', 'mll','mly'] => ['(*','*)'], # OCaml uses Pascal comments
>  ['bib','ltx','tex','ps','eps'] => ['%',''] # Latex etc
> }
>
> key = StringMap.keys.detect {|k| k.include?(file_extension)}
>
> (comment_strings = StringMap[key]) if not key.nil?
>
> comment_expr = /^\s*#{comment_strings[0]}(.*)#{comment_strings[1]}/
>
> choose_mode = true
> uncomment = false
>
> def process_line(instring, uncomment, comment_strings, comment_expr)
>
>  if uncomment then
>  # uncomment
>  match = comment_expr.match(instring)
>  if match != nil then
>  puts match[1]
>  else
>  puts instring
>  end
>  else
>  # comment
>  puts comment_strings[0] + instring.chomp + comment_strings[1]
>  end
>  end
>
> # if the first line is commented, assume the user wants to uncomment
> # and vice versa
> first_line = $stdin.gets
> uncomment = comment_expr.match(first_line)
> uncomment = false if uncomment.nil?
> process_line(first_line, uncomment, comment_strings, comment_expr)
>
> $stdin.each_line { |instring| process_line(instring, uncomment, 
> comment_strings, comment_expr) }
>
>
>
>
> _______________________________________________
> textmate mailing list
> textmate at lists.macromates.com
> http://lists.macromates.com/mailman/listinfo/textmate
>




More information about the textmate mailing list