melo / search-in-project-with-ack-tmcommand

TextMate command to search the current project using ack

This URL has Read+Write access

search-in-project-with-ack-tmcommand / SearchInProjectWithAck.tmCommand
f84de58e » melo 2008-06-02 Imported current version to... 1 <?xml version="1.0" encoding="UTF-8"?>
2 <!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
3 <plist version="1.0">
4 <dict>
5 <key>beforeRunningCommand</key>
6 <string>nop</string>
7 <key>bundleUUID</key>
8 <string>5A9D4FC6-6CBE-11D9-A21B-000D93589AF6</string>
9 <key>command</key>
10 <string>#!/usr/bin/env ruby -rcgi
11
12 # By Henrik Nyh &lt;http://henrik.nyh.se&gt; 2007-06-26
13 # Free to modify and redistribute with credit.
14 #
15 # Updated to use with ack by Pedro Melo &lt;melo@simplicidade.org&gt; 2008-05-26
a035a3a2 » Corey Jewett 2008-06-02 Support TM_SELECTED_FILES 16 # Support for TM_SELECTED_FILES by Corey Jewett &lt;ml@syntheticplayground.com&gt; 2008-05-30
f84de58e » melo 2008-06-02 Imported current version to... 17 #
a598b52a » melo 2008-08-11 Make search in selected fil... 18 # 2008-08-11 (melo): Added flag &apos;:&apos; you must start your query with a : to
19 # search only in the &quot;selected files&quot;. I added this because using the
20 # selected files ends up not being something that I commonly do.
21 #
22 #
99044e23 » melo 2008-06-02 Support TM_ACK_COMMAND_PATH 23 # **NOTE WELL**: TextMate does not inherit your PATH, so if you this
24 # command does not find it define the environment variable
25 # TM_ACK_COMMAND_PATH in your Preferences &gt; Advanced &gt; Shell Variables with
26 # the full path of your copy of the ack command.
27 #
f84de58e » melo 2008-06-02 Imported current version to... 28
ebebc2a2 » melo 2008-06-04 Generated latest version of... 29 %w{ui web_preview escape textmate tm/process}.each { |lib| require &quot;%s/lib/%s&quot; % [ENV[&apos;TM_SUPPORT_PATH&apos;], lib] }
dbb9155b » Luke Daley 2008-06-04 Merged changes by Luke Daley: 30
ebebc2a2 » melo 2008-06-04 Generated latest version of... 31 ack_cmd=ENV[&apos;TM_ACK_COMMAND_PATH&apos;] || ENV[&apos;TM_ACK&apos;] || &apos;ack&apos;
dbb9155b » Luke Daley 2008-06-04 Merged changes by Luke Daley: 32 TextMate.require_cmd(ack_cmd)
f84de58e » melo 2008-06-02 Imported current version to... 33
ebebc2a2 » melo 2008-06-04 Generated latest version of... 34 NAME = &quot;Search in Project with ack&quot;
f84de58e » melo 2008-06-02 Imported current version to... 35 HEAD = &lt;&lt;-HTML
ebebc2a2 » melo 2008-06-04 Generated latest version of... 36 &lt;style type=&quot;text/css&quot;&gt;
f84de58e » melo 2008-06-02 Imported current version to... 37 table { font-size:0.9em; border-collapse:collapse; border-bottom:1px solid #555; }
38 h2 { font-size:1.3em; }
39 td { vertical-align:top; white-space:nowrap; padding:0.4em 1em; }
40 tr td:first-child { text-align:right; padding-right:1.5em; }
41 tr.binary { background:#E8AFA8; }
42 tr.binary.odd { background:#E0A7A2; }
43 tr#empty { border-bottom:1px solid #FFF; }
44 tr#empty td { text-align:center; }
45 tr.newFile, tr.binary { border-top:1px solid #555; }
46 .keyword { font-weight:bold; margin:0 0.1em; }
47 .ellipsis { color:#777; margin:0 0.5em; }
48 &lt;/style&gt;
ebebc2a2 » melo 2008-06-04 Generated latest version of... 49 &lt;script type=&quot;text/javascript&quot;&gt;
f84de58e » melo 2008-06-02 Imported current version to... 50 function reveal_file(path) {
ebebc2a2 » melo 2008-06-04 Generated latest version of... 51 const quote = &apos;&quot;&apos;;
52 const command = &quot;osascript -e &apos; tell app &quot;+quote+&quot;Finder&quot;+quote+&quot;&apos; &quot; +
53 &quot; -e &apos;reveal (POSIX file &quot; +quote+path+quote + &quot;)&apos; &quot; +
54 &quot; -e &apos;activate&apos; &quot; +
55 &quot; -e &apos;end&apos; &quot;;
f84de58e » melo 2008-06-02 Imported current version to... 56 TextMate.system(command, null);
57 }
58
59 function findPos(obj) {
60 var curleft = curtop = 0;
61 if (obj.offsetParent) {
62 curleft = obj.offsetLeft
63 curtop = obj.offsetTop
64 while (obj = obj.offsetParent) {
65 curleft += obj.offsetLeft
66 curtop += obj.offsetTop
67 }
68 }
69 return {left: curleft, top: curtop};
70 }
71
72 function resizeTableToFit() {
ebebc2a2 » melo 2008-06-04 Generated latest version of... 73 var table = document.getElementsByTagName(&quot;table&quot;)[0];
f84de58e » melo 2008-06-02 Imported current version to... 74 const minWidth = 450, minHeight = 250;
75
76 var pos = findPos(table);
77 var tableFitWidth = table.offsetWidth + pos.left * 2;
78 var tableFitHeight = table.offsetHeight + pos.top + 50;
79 var screenFitWidth = screen.width - 150;
80 var screenFitHeight = screen.height - 150;
81
82 var setWidth = tableFitWidth &gt; screenFitWidth ? screenFitWidth : tableFitWidth;
83 var setHeight = tableFitHeight &gt; screenFitHeight ? screenFitHeight : tableFitHeight;
84 setWidth = setWidth &lt; minWidth ? minWidth : setWidth;
85 setHeight = setHeight &lt; minHeight ? minHeight : setHeight;
86
87 window.resizeTo(setWidth, setHeight);
88 }
89
90 &lt;/script&gt;
91 HTML
92
93 RESIZE_TABLE = &lt;&lt;-HTML
ebebc2a2 » melo 2008-06-04 Generated latest version of... 94 &lt;script type=&quot;text/javascript&quot;&gt;
f84de58e » melo 2008-06-02 Imported current version to... 95 resizeTableToFit();
96 &lt;/script&gt;
97 HTML
98
99 def ellipsize_path(path)
ebebc2a2 » melo 2008-06-04 Generated latest version of... 100 path.sub(/^(.{30})(.{10,})(.{30})$/) { &quot;#$1?#$3&quot; }
f84de58e » melo 2008-06-02 Imported current version to... 101 end
102
103 def escape(string)
104 CGI.escapeHTML(string)
105 end
106
107 def bail(message)
108 puts &lt;&lt;-HTML
109 &lt;h2&gt;#{ message }&lt;/h2&gt;
110 HTML
111 html_footer
112 exit
113 end
114
ebebc2a2 » melo 2008-06-04 Generated latest version of... 115 directory = ENV[&apos;TM_PROJECT_DIRECTORY&apos;] ||
116 ( ENV[&apos;TM_FILEPATH&apos;] &amp;&amp; File.dirname(ENV[&apos;TM_FILEPATH&apos;]) )
f84de58e » melo 2008-06-02 Imported current version to... 117
118 puts html_head(
119 :window_title =&gt; NAME,
120 :page_title =&gt; NAME,
ebebc2a2 » melo 2008-06-04 Generated latest version of... 121 :sub_title =&gt; directory || &quot;Error&quot;,
f84de58e » melo 2008-06-02 Imported current version to... 122 :html_head =&gt; HEAD
123 )
124
ebebc2a2 » melo 2008-06-04 Generated latest version of... 125 bail(&quot;Not in a saved file&quot;) unless directory
f84de58e » melo 2008-06-02 Imported current version to... 126
ebebc2a2 » melo 2008-06-04 Generated latest version of... 127 query = TextMate::UI.request_string(:title =&gt; &quot;Search in Project with ack&quot;, :prompt =&gt; &quot;Find this:&quot;, :default =&gt; %x{pbpaste -pboard find})
128 bail(&quot;Search aborted&quot;) unless query
a598b52a » melo 2008-08-11 Make search in selected fil... 129
130 if query[0,1] == &apos;:&apos;
131 query[0,1] = &apos;&apos;
132 use_selected_files = true
133 else
134 use_selected_files = false
135 end
136
ebebc2a2 » melo 2008-06-04 Generated latest version of... 137 IO.popen(&apos;pbcopy -pboard find&apos;, &apos;w&apos;) { |copy| copy.print query }
f84de58e » melo 2008-06-02 Imported current version to... 138
139 puts &lt;&lt;-HTML
ebebc2a2 » melo 2008-06-04 Generated latest version of... 140 &lt;h2&gt;Searching for #{ escape(query) }&lt;/h2&gt;
f84de58e » melo 2008-06-02 Imported current version to... 141 HTML
142
a598b52a » melo 2008-08-11 Make search in selected fil... 143 if use_selected_files
144 selected_files=TextMate.selected_files
145 end
cb09ae8b » melo 2008-06-02 Show the search targets 146
147 if selected_files
bf09f165 » melo 2008-08-11 Mark the end of options jus... 148 command = [ack_cmd, &quot;-H&quot;, &apos;--&apos;, query, selected_files]
cb09ae8b » melo 2008-06-02 Show the search targets 149 puts &lt;&lt;-HTML
a598b52a » melo 2008-08-11 Make search in selected fil... 150 &lt;p&gt;&lt;small&gt;Search limited to #{ escape(selected_files.join(&apos; &apos;)) }&lt;/small&gt;&lt;/p&gt;
cb09ae8b » melo 2008-06-02 Show the search targets 151 HTML
a035a3a2 » Corey Jewett 2008-06-02 Support TM_SELECTED_FILES 152 else
dbb9155b » Luke Daley 2008-06-04 Merged changes by Luke Daley: 153 Dir.chdir(directory)
bf09f165 » melo 2008-08-11 Mark the end of options jus... 154 command = [ack_cmd, &apos;--&apos;, query]
a035a3a2 » Corey Jewett 2008-06-02 Support TM_SELECTED_FILES 155 end
f84de58e » melo 2008-06-02 Imported current version to... 156
cb09ae8b » melo 2008-06-02 Show the search targets 157 puts &lt;&lt;-HTML
158 &lt;table&gt;
159 HTML
160
dbb9155b » Luke Daley 2008-06-04 Merged changes by Luke Daley: 161 # Used to highlight matches
162 query_re = Regexp.new( Regexp.escape(CGI.escapeHTML(query)), Regexp::IGNORECASE)
f84de58e » melo 2008-06-02 Imported current version to... 163
dbb9155b » Luke Daley 2008-06-04 Merged changes by Luke Daley: 164 last_path = path = i = nil
ebebc2a2 » melo 2008-06-04 Generated latest version of... 165 err = &quot;&quot;
dbb9155b » Luke Daley 2008-06-04 Merged changes by Luke Daley: 166 alternate = true
167 lines = 0
f84de58e » melo 2008-06-02 Imported current version to... 168
dbb9155b » Luke Daley 2008-06-04 Merged changes by Luke Daley: 169 TextMate::Process.run(command, :interactive_input =&gt; false) do |line, type|
170 case type
171 when :err
172 err &lt;&lt; line
173 when :out
174 lines = lines + 1
f84de58e » melo 2008-06-02 Imported current version to... 175 line.gsub!(/^([^:]+):(\d+):(.*)$/) do
176
177 relative_path, line_number, content = $1, $2, $3.strip
a035a3a2 » Corey Jewett 2008-06-02 Support TM_SELECTED_FILES 178
ebebc2a2 » melo 2008-06-04 Generated latest version of... 179 relative_path.sub!(directory + &quot;/&quot;, &apos;&apos;) if selected_files
a035a3a2 » Corey Jewett 2008-06-02 Support TM_SELECTED_FILES 180
ebebc2a2 » melo 2008-06-04 Generated latest version of... 181 path = directory + &apos;/&apos; + relative_path
182 url = &quot;txmt://open/?url=file://#{path}&amp;line=#{line_number}&quot;
183 fname = &quot;%s:%s&quot; % [ellipsize_path(relative_path), line_number];
184 fname = &quot;:%s&quot; % [ line_number ] if (path == last_path);
f84de58e » melo 2008-06-02 Imported current version to... 185
186 content = escape(content).
187 # Highlight keywords
ebebc2a2 » melo 2008-06-04 Generated latest version of... 188 gsub(query_re) { %{&lt;a href=&quot;#{url}&quot;&gt;&lt;strong class=&quot;keyword&quot;&gt;#$&amp;&lt;/strong&gt;&lt;/a&gt;} }.
f84de58e » melo 2008-06-02 Imported current version to... 189 # Ellipsize before, between and after keywords
190 gsub(%r{(^[^&lt;]{25}|&lt;/strong&gt;[^&lt;]{15})([^&lt;]{20,})([^&lt;]{15}&lt;strong|[^&lt;]{25}$)}) do
ebebc2a2 » melo 2008-06-04 Generated latest version of... 191 %{#$1&lt;span class=&quot;ellipsis&quot; title=&quot;#{escape($2)}&quot;&gt;?&lt;/span&gt;#$3}
f84de58e » melo 2008-06-02 Imported current version to... 192 end
193 &lt;&lt;-HTML
194
ebebc2a2 » melo 2008-06-04 Generated latest version of... 195 &lt;tr class=&quot;#{ &apos;odd&apos; unless (alternate = (not alternate)) } #{ &apos;newFile&apos; if (path != last_path) }&quot;&gt;
f84de58e » melo 2008-06-02 Imported current version to... 196 &lt;td&gt;
ebebc2a2 » melo 2008-06-04 Generated latest version of... 197 &lt;a href=&quot;#{ url }&quot; title=&quot;#{ &quot;%s:%s&quot; % [path, line_number] }&quot;&gt;
f84de58e » melo 2008-06-02 Imported current version to... 198 #{ fname }
199 &lt;/a&gt;
200 &lt;/td&gt;
201 &lt;td&gt;#{ content }&lt;/td&gt;
202 &lt;/tr&gt;
203
204 HTML
205 end
206 puts line
207 last_path = path
208 end
dbb9155b » Luke Daley 2008-06-04 Merged changes by Luke Daley: 209 end
f84de58e » melo 2008-06-02 Imported current version to... 210
dbb9155b » Luke Daley 2008-06-04 Merged changes by Luke Daley: 211 if lines
ebebc2a2 » melo 2008-06-04 Generated latest version of... 212 # A paragraph inside the table ends up at the top even though it&apos;s output
dbb9155b » Luke Daley 2008-06-04 Merged changes by Luke Daley: 213 # at the end. Something of a hack :)
214 puts &lt;&lt;-HTML
ebebc2a2 » melo 2008-06-04 Generated latest version of... 215 &lt;p&gt;#{lines} matching line#{lines==1 ? &apos;&apos; : &apos;s&apos;}:&lt;/p&gt;
dbb9155b » Luke Daley 2008-06-04 Merged changes by Luke Daley: 216 #{RESIZE_TABLE}
217 HTML
218 else
219 puts &lt;&lt;-HTML
ebebc2a2 » melo 2008-06-04 Generated latest version of... 220 &lt;tr id=&quot;empty&quot;&gt;&lt;td colspan=&quot;2&quot;&gt;No results.&lt;/td&gt;&lt;/tr&gt;
dbb9155b » Luke Daley 2008-06-04 Merged changes by Luke Daley: 221 HTML
222 end
223
224 if $? != 0
ebebc2a2 » melo 2008-06-04 Generated latest version of... 225 TextMate::UI.alert(:critical, &quot;Search In Project With Ack Error&quot;, err)
dbb9155b » Luke Daley 2008-06-04 Merged changes by Luke Daley: 226 TextMate.exit_discard
f84de58e » melo 2008-06-02 Imported current version to... 227 end
228
99044e23 » melo 2008-06-02 Support TM_ACK_COMMAND_PATH 229 # TODO: see how to detect command not found in ruby and suggest using
230 # TM_ACK_COMMAND_PATH to solve it
231
f84de58e » melo 2008-06-02 Imported current version to... 232 puts &lt;&lt;-HTML
233 &lt;/table&gt;
234 HTML
235
236 html_footer
dbb9155b » Luke Daley 2008-06-04 Merged changes by Luke Daley: 237
238 TextMate.exit_show_html
f84de58e » melo 2008-06-02 Imported current version to... 239 </string>
240 <key>input</key>
241 <string>none</string>
242 <key>keyEquivalent</key>
243 <string>@F</string>
244 <key>name</key>
245 <string>Search in Project with ack</string>
246 <key>output</key>
247 <string>showAsHTML</string>
248 <key>uuid</key>
249 <string>0B537D91-B21D-4A52-BFD8-D1E73669E2BD</string>
250 </dict>
251 </plist>