Guest User

Untitled

a guest
Feb 20th, 2018
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.05 KB | None | 0 0
  1. #! /usr/bin/ruby
  2. require ENV['TM_SUPPORT_PATH'] + "/lib/exit_codes.rb"
  3. require ENV['TM_SUPPORT_PATH'] + "/lib/escape.rb"
  4. require ENV['TM_SUPPORT_PATH'] + "/lib/dialog.rb"
  5. text = STDIN.read
  6. # text="boxplot"
  7. file = `find /Library/Frameworks/R.framework/Versions/Current/Resources/library -name #{text}.tex -print`
  8. if file.empty?
  9. matches = `grep -r --include CONTENTS ' #{text.gsub(".", "\\.")} ' /Library/Frameworks/R.framework/Versions/Current/Resources/library/*`
  10. matches.split("\n").each do |line|
  11. if line.include? "Alias"
  12. alias_text = line.split(/\s+/).slice(1)
  13. file = `find /Library/Frameworks/R.framework/Versions/Current/Resources/library -name #{alias_text}.tex -print`
  14. end
  15. end
  16. end
  17. file=file.chomp
  18. TextMate.exit_discard if file.empty?
  19. dir = File.dirname(file)
  20. tex = File.read(file)
  21. results = tex.scan(/\\begin\{verbatim\}\n(.*)\\end\{verbatim\}/m)[0][0].split("\n\n")
  22. require 'pp'
  23. results=results.grep(/^#{text.gsub(".","\.")}\((.*)\)/m).map do |line|
  24. line.gsub(/\n|( )/,"")
  25. end
  26. if results.length>1 then
  27. reply = Dialog.request_item(:title => "Insert Snippet for Command",:prompt => "Multiple syntaxes found, please choose the one you want",:items => results)
  28. line=reply
  29. else
  30. line=results[0]
  31. end
  32. m=line.scan(/(#{text.gsub(".","\.")}\()|(\s*[\w.]+\s*(?=,))|((\s*[\w.]+\s*=\s*)((?:\"((?:[^"]|(?:\\\\\\\"))*)\"(?=,))|[^,"]*(?=,)))|(\)\s*)$/)
  33. index = 0
  34. result = ""
  35. m.each_with_index do |item,idx|
  36. case
  37. when !item[0].nil? # Case of the starting part
  38. result << e_sn(item[0])
  39. when !item[1].nil? # Case of a parameter of the form "name" ( or ...)
  40. result << "#{idx==1?"":",${#{index+=1}:}"}${#{index+=1}:#{e_sn(item[1])}}#{idx==1?"":"}"}"
  41. when item[2].nil? # Case of the last parenthesis
  42. result << item[6]
  43. when item[5].nil? # Case of "name = item"
  44. result << "${#{index+=1}:#{idx==1?"":","}#{e_sn(item[3])}${#{index+=1}:#{e_sn(item[4])}}}"
  45. else # Case of "name = \"item\""
  46. result << "${#{index+=1}:#{idx==1?"":","}#{e_sn(item[3])}\"${#{index+=1}:#{e_sn(item[5])}}\"}"
  47. end
  48. end
  49. print result
Add Comment
Please, Sign In to add comment