Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #! /usr/bin/ruby
- require ENV['TM_SUPPORT_PATH'] + "/lib/exit_codes.rb"
- require ENV['TM_SUPPORT_PATH'] + "/lib/escape.rb"
- require ENV['TM_SUPPORT_PATH'] + "/lib/dialog.rb"
- text = STDIN.read
- # text="boxplot"
- file = `find /Library/Frameworks/R.framework/Versions/Current/Resources/library -name #{text}.tex -print`
- if file.empty?
- matches = `grep -r --include CONTENTS ' #{text.gsub(".", "\\.")} ' /Library/Frameworks/R.framework/Versions/Current/Resources/library/*`
- matches.split("\n").each do |line|
- if line.include? "Alias"
- alias_text = line.split(/\s+/).slice(1)
- file = `find /Library/Frameworks/R.framework/Versions/Current/Resources/library -name #{alias_text}.tex -print`
- end
- end
- end
- file=file.chomp
- TextMate.exit_discard if file.empty?
- dir = File.dirname(file)
- tex = File.read(file)
- results = tex.scan(/\\begin\{verbatim\}\n(.*)\\end\{verbatim\}/m)[0][0].split("\n\n")
- require 'pp'
- results=results.grep(/^#{text.gsub(".","\.")}\((.*)\)/m).map do |line|
- line.gsub(/\n|( )/,"")
- end
- if results.length>1 then
- reply = Dialog.request_item(:title => "Insert Snippet for Command",:prompt => "Multiple syntaxes found, please choose the one you want",:items => results)
- line=reply
- else
- line=results[0]
- end
- m=line.scan(/(#{text.gsub(".","\.")}\()|(\s*[\w.]+\s*(?=,))|((\s*[\w.]+\s*=\s*)((?:\"((?:[^"]|(?:\\\\\\\"))*)\"(?=,))|[^,"]*(?=,)))|(\)\s*)$/)
- index = 0
- result = ""
- m.each_with_index do |item,idx|
- case
- when !item[0].nil? # Case of the starting part
- result << e_sn(item[0])
- when !item[1].nil? # Case of a parameter of the form "name" ( or ...)
- result << "#{idx==1?"":",${#{index+=1}:}"}${#{index+=1}:#{e_sn(item[1])}}#{idx==1?"":"}"}"
- when item[2].nil? # Case of the last parenthesis
- result << item[6]
- when item[5].nil? # Case of "name = item"
- result << "${#{index+=1}:#{idx==1?"":","}#{e_sn(item[3])}${#{index+=1}:#{e_sn(item[4])}}}"
- else # Case of "name = \"item\""
- result << "${#{index+=1}:#{idx==1?"":","}#{e_sn(item[3])}\"${#{index+=1}:#{e_sn(item[5])}}\"}"
- end
- end
- print result
Add Comment
Please, Sign In to add comment