On Nov 15, 2009, at 2:22 AM, Alain Matthes wrote:
Hi
I'm not an expert with Ruby so I need to know if the next command is safe
I want to select a "number" and to get $\numprint{number}$ if number is not empty and number is not inside a math environment or \numprint{number} if number is inside a math environment.
then if number is empty, I want to get \numprint{...} or $\numprint{...}$
I made this and I would like to know if this code is correct
#!/usr/bin/ruby # Created by Alain Matthes on 2009-11-15 (www.altermundus.com).
require ENV["TM_SUPPORT_PATH"] + "/lib/exit_codes.rb"
if ENV['TM_SELECTED_TEXT'].nil? input = ENV[''] else input = ENV['TM_SELECTED_TEXT'] end
if ENV['TM_SCOPE'].match(/math.((.*)|(la))tex/) $math_del = "" else $math_del = "\$" end
TextMate.exit_insert_snippet("#{$math_del}\numprint{#{input}$0}#{$math_del}")
Is it possible to use a shortcut to go out of the first environment {...} and then to go out of the math environment because the caret is inside {number} ?
Hey Alain,
Your code looks safe. I'm not sure what you meant by safe, but I'm quite sure this won't hurt anything.
I simplified it a little bit, and added the features you wanted:
#!/usr/bin/ruby # Created by Alain Matthes on 2009-11-15 (www.altermundus.com). # Some simplification by Alex Ross (http://lasersox.net)
snippet = "\numprint{${1:#{$stdin.read || "..."}}}$2" snippet = "\$#{snippet}\$$0" unless ENV['TM_SCOPE'].match(/math/)
puts snippet
Set Input: Selected Text or Nothing Output: Insert as Snippet