Well I rewrote my little script in Ruby and have had mostly success, but I need some help escaping things properly. Here's what I'm having trouble with:
#!/usr/bin/env ruby
arg = STDIN.read arg.chomp! arg.gsub!('\', '\') arg.gsub!("'", '\'') arg.gsub!(""","\"") arg.gsub!('`', '\`') system("pointfree ""+arg+""")
This is intended to enclose text selected in TextMate in quotation marks, and then escape all of the following characters within the the quotes, and then use that as the argument to pointfree. What's wrong with how I'm doing it?
Sorry for all of the questions, Ian
On 8 Apr 2009, at 19:20, Ian Duncan wrote:
Well I rewrote my little script in Ruby and have had mostly success, but I need some help escaping things properly. Here's what I'm having trouble with: [...]
This is intended to enclose text selected in TextMate in quotation marks, and then escape all of the following characters within the the quotes, and then use that as the argument to pointfree. What's wrong with how I'm doing it?
If this is a TextMate command you can do:
#!/usr/bin/env ruby require "#{ENV['TM_SUPPORT_PATH']}/lib/escape"
arg = STDIN.read arg.chomp! system("pointfree #{e_sh arg}")
On Apr 8, 2009, at 4/8/093:05 PM, Allan Odgaard wrote:
If this is a TextMate command you can do:
#!/usr/bin/env ruby require "#{ENV['TM_SUPPORT_PATH']}/lib/escape" arg = STDIN.read arg.chomp! system("pointfree #{e_sh arg}")
Ok, that helped some, but I ended up using e_as instead as the appropriate method. However, I still can't get it to escape backticks properly. Here's the script now:
#!/usr/bin/env ruby require "#{ENV['TM_SUPPORT_PATH']}/lib/escape" arg = STDIN.read arg.chomp! arg = e_as arg arg.gsub!("`", "\`") puts("pointfree "" + arg + """)
The puts is just for debugging purposes. When I run this command on the selected text:
\x y -> x `div` y
I am hoping to get:
pointfree "\x y -> x `div` y"
But I get this instead:
pointfree "\x y -> x \x y -> x div\x y -> x `div y"
* Ian Duncan [8-Apr-09 17:37]:
#!/usr/bin/env ruby require "#{ENV['TM_SUPPORT_PATH']}/lib/escape" arg = STDIN.read arg.chomp! arg = e_as arg arg.gsub!("`", "\`") puts("pointfree "" + arg + """)
The puts is just for debugging purposes. When I run this command on the selected text:
\x y -> x `div` y
I am hoping to get:
pointfree "\x y -> x `div` y"
But I get this instead:
pointfree "\x y -> x \x y -> x div\x y -> x `div y"
Short answer: use e_sn
Replacing backslashes is a pain. There are a few things going on:
1. the string interpretation 2. the regular expression engine also uses backslashes as the escape character 3. ` means "the text before the match" -- that's why you're seeing double
To fix it, you can:
1. add more toothpicks arg.gsub('`', '\\`')
2. use the block form of gsub, which only evaluates the string once arg.gsub('`') { '`' }
3. match the space before the backtick, which is what e_sn does arg.gsub(/(?=`)/, '\')