Blob of Support/lib/ruby_tm_helpers.rb (raw blob data)

1 # TextMate helpers
2 # Author: Tim Harper with Lead Media Partners.
3 # http://code.google.com/p/productivity-bundle/
4
5
6 def exit_discard
7 exit 200;
8 end
9
10 def exit_replace_text
11 exit 201;
12 end
13
14 def exit_replace_document
15 exit 202;
16 end
17
18 def exit_insert_text
19 exit 203;
20 end
21
22 def exit_insert_snippet
23 exit 204;
24 end
25
26 def exit_show_html
27 exit 205
28 end
29
30 def exit_show_tool_tip
31 exit 206;
32 end
33
34 def exit_create_new_document
35 exit 207;
36 end
37
38 def tm_open(file, options = {})
39 line = options[:line]
40 wait = options[:wait]
41 if line.nil? && /^(.+):(\d+)$/.match(file)
42 file = $1
43 line = $2
44 end
45
46 unless /^\//.match(file)
47 file = File.join((ENV['TM_PROJECT_DIRECTORY'] || Dir.pwd), file)
48 end
49
50 args = []
51 args << "-w" if wait
52 args << e_sh(file)
53 args << "-l #{line}" if line
54 %x{"#{ENV['TM_SUPPORT_PATH']}/bin/mate" #{args * " "}}
55 end
56
57 # this method only applies when the whole document contents are sent in
58 def tm_expanded_selection(options = {})
59 text=ENV['TM_SELECTED_TEXT'].to_s
60 return text unless text.empty?
61
62 options = {
63 :input_type => :doc,
64 :input => nil,
65 :forward => /\w*/i,
66 :backward => /\w*/i,
67 :line_number => ENV['TM_LINE_NUMBER'].to_i,
68 :col_number => ENV['TM_COLUMN_NUMBER'].to_i
69 }.merge(options)
70
71 col_number, line_number = options[:col_number], options[:line_number]
72
73 doc = options[:input] ||= $stdin.read
74
75 line =
76 case options[:input_type]
77 when :doc then doc.split("\n")[line_number - 1].to_s
78 when :line then doc
79 else
80 raise "Can't handle input_type #{options[:input_type]} for tm_expanded_selection"
81 end
82
83 last_part = line[ (col_number - 1)..-1]
84 first_part = line[ 0..col_number - 2]
85
86 last_part.gsub!(/^(#{options[:forward]}){0,1}.*$/i) { $1 }
87
88 first_part.reverse!
89 first_part.gsub!(/^(#{options[:backward]}){0,1}.*$/i) { $1 }
90 first_part.reverse!
91 first_part + last_part
92 end
93
94
95
96 module Enumerable
97 # TODO remove when 1.9 supports natively
98 def map_with_index
99 result = []
100 each_with_index do |item, idx|
101 result << yield(item, idx)
102 end
103 result
104 end
105 end