vimamsa 0.1.0 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,100 @@
1
+
2
+ def execute_search(input_str)
3
+ $search = Search.new
4
+ eval_str="execute_search(#{input_str.dump})"
5
+ $macro.overwrite_current_action(eval_str)
6
+ return $search.set(input_str, "simple", $buffer)
7
+ end
8
+
9
+ def invoke_search()
10
+ start_minibuffer_cmd("", "", :execute_search)
11
+ end
12
+
13
+ def invoke_search()
14
+ nfo = ""
15
+
16
+ callback = proc{|x| execute_search(x)}
17
+ gui_one_input_action(nfo, "Search:", "search", callback)
18
+ end
19
+
20
+ class Search
21
+
22
+ def initialize()
23
+ @cur_search_i = -1
24
+ @search_indexes = []
25
+ end
26
+
27
+ def set(search_str, search_type, buffer)
28
+ @search_str = search_str
29
+ @search_type = search_type
30
+ @buffer = buffer
31
+ regex = Regexp.escape(search_str)
32
+ if /.*\p{Upper}.*/ =~ regex
33
+ @reg = Regexp.new(regex)
34
+ else
35
+ # if does not contain uppercase characters, ignore case
36
+ @reg = Regexp.new(regex, Regexp::IGNORECASE)
37
+ end
38
+ @search_indexes = scan_indexes(buffer, @reg)
39
+ puts @search_indexes.inspect
40
+ @cur_search_i = -1
41
+ if @search_indexes.any?
42
+ @cur_search_i = 0
43
+ startpos = @search_indexes.select { |x| x > @buffer.pos }.min
44
+ if startpos != nil
45
+ @cur_search_i = @search_indexes.find_index(startpos)
46
+ end
47
+ @buffer.set_pos(@search_indexes[@cur_search_i])
48
+ else
49
+ return false
50
+ end
51
+ end
52
+
53
+ def update_search()
54
+ @search_indexes = scan_indexes(@buffer, @reg)
55
+
56
+ @cur_search_i = 0
57
+ startpos = @search_indexes.select { |x| x > @buffer.pos }.min
58
+ if startpos != nil
59
+ @cur_search_i = @search_indexes.find_index(startpos)
60
+ end
61
+ # Ripl.start :binding => binding
62
+ end
63
+
64
+ def jump_to_next()
65
+
66
+ return if @cur_search_i < 0
67
+ # TODO: optimize, update only after buffer changed
68
+ # or search only for the next match
69
+ update_search
70
+
71
+ return if !@search_indexes.any?
72
+
73
+ # if @search_indexes.size > @cur_search_i + 1
74
+ # @cur_search_i = @cur_search_i + 1
75
+ # else
76
+ # @cur_search_i = 0
77
+ # end
78
+ @buffer.set_pos(@search_indexes[@cur_search_i])
79
+ end
80
+
81
+ def jump_to_previous()
82
+ return if @cur_search_i < 0
83
+
84
+ update_search
85
+ return if !@search_indexes.any?
86
+
87
+ # TODO: hack
88
+ 2.times {
89
+ if @cur_search_i - 1 < 0
90
+ @cur_search_i = @search_indexes.size - 1
91
+ else
92
+ @cur_search_i = @cur_search_i - 1
93
+ end
94
+ break if @buffer.pos != @search_indexes[@cur_search_i]
95
+ }
96
+ @buffer.set_pos(@search_indexes[@cur_search_i])
97
+ end
98
+ end
99
+
100
+
@@ -0,0 +1,333 @@
1
+ class Grep
2
+ attr_accessor :history
3
+
4
+ def initialize()
5
+ end
6
+ end
7
+
8
+ class FileSelector
9
+ def initialize()
10
+ @buf = nil
11
+ end
12
+
13
+ def run
14
+ ld = buflist.get_last_dir
15
+ dir_to_buf(ld)
16
+ # puts "ld=#{ld}"
17
+ # dlist = Dir["#{ld}/*"]
18
+ end
19
+
20
+ def dir_to_buf(dirpath, b = nil)
21
+ @ld = dirpath
22
+ @dlist = Dir.children(@ld)
23
+ @cdirs = []
24
+ @cfiles = []
25
+ for x in @dlist
26
+ if File.directory?(fullp(x))
27
+ @cdirs << x
28
+ else
29
+ @cfiles << x
30
+ end
31
+ end
32
+ s = "..\n"
33
+ s << @cdirs.join("\n")
34
+ s << @cfiles.join("\n")
35
+
36
+ if @buf.nil?
37
+ @buf = create_new_file(nil, s)
38
+ @buf.module = self
39
+ @buf.active_kbd_mode = :file_exp
40
+ else
41
+ @buf.set_content(s)
42
+ end
43
+ end
44
+
45
+ def fullp(fn)
46
+ "#{@ld}/#{fn}"
47
+ end
48
+
49
+ def select_line
50
+ # puts "def select_line"
51
+ fn = fullp(@buf.get_current_line[0..-2])
52
+ if File.directory?(fn)
53
+ puts "CHDIR: #{fn}"
54
+ dir_to_buf(fn)
55
+ # elsif vma.can_open_extension?(fn) #TODO: remove this check?
56
+ # jump_to_file(fn)
57
+ elsif file_is_text_file(fn)
58
+ jump_to_file(fn)
59
+ else
60
+ open_with_default_program(fn)
61
+ end
62
+ # puts l.inspect
63
+ end
64
+ end
65
+
66
+ def gui_grep()
67
+ callback = proc { |x| grep_cur_buffer(x) }
68
+ # gui_one_input_action("Grep", "Search:", "grep", "grep_cur_buffer")
69
+ gui_one_input_action("Grep", "Search:", "grep", callback)
70
+ end
71
+
72
+ def grep_cur_buffer(search_str, b = nil)
73
+ debug "grep_cur_buffer(search_str)"
74
+ lines = $buffer.split("\n")
75
+ r = Regexp.new(Regexp.escape(search_str), Regexp::IGNORECASE)
76
+ fpath = ""
77
+ fpath = $buffer.pathname.expand_path.to_s + ":" if $buffer.pathname
78
+ res_str = ""
79
+
80
+ $grep_matches = []
81
+ lines.each_with_index { |l, i|
82
+ if r.match(l)
83
+ # res_str << "#{fpath}#{i + 1}:#{l}\n"
84
+ res_str << "#{i + 1}:#{l}\n"
85
+ $grep_matches << i + 1 # Lines start from index 1
86
+ end
87
+ }
88
+ $grep_bufid = $buffers.current_buf
89
+ b = create_new_file(nil, res_str)
90
+ # set_current_buffer(buffer_i, update_history = true)
91
+ # @current_buf = buffer_i
92
+
93
+ b.line_action_handler = proc { |lineno|
94
+ puts "GREP HANDLER:#{lineno}"
95
+ jumpto = $grep_matches[lineno]
96
+ if jumpto.class == Integer
97
+ $buffers.set_current_buffer($grep_bufid, update_history = true)
98
+ buf.jump_to_line(jumpto)
99
+ end
100
+ }
101
+ end
102
+
103
+ def invoke_grep_search()
104
+ start_minibuffer_cmd("", "", :grep_cur_buffer)
105
+ end
106
+
107
+ def gui_one_input_action(title, field_label, button_title, callback)
108
+ a = OneInputAction.new(nil, title, field_label, button_title, callback)
109
+ a.run
110
+ return
111
+ end
112
+
113
+ # def gui_replace_callback(search_str, replace_str)
114
+ def gui_replace_callback(vals)
115
+ search_str = vals["search"]
116
+ replace_str = vals["replace"]
117
+ puts "gui_replace_callback: #{search_str} => #{replace_str}"
118
+ qt_select_window_close(0)
119
+ buf_replace(search_str, replace_str)
120
+ end
121
+
122
+ # Search and replace text via GUI interface
123
+ def gui_search_replace()
124
+ params = {}
125
+ params["inputs"] = {}
126
+ params["inputs"]["search"] = { :label => "Search", :type => :entry }
127
+ params["inputs"]["replace"] = { :label => "Replace", :type => :entry }
128
+ params["inputs"]["btn1"] = { :label => "Replace all", :type => :button }
129
+ callback = proc { |x| gui_replace_callback(x) }
130
+
131
+ params[:callback] = callback
132
+ PopupFormGenerator.new(params).run
133
+ end
134
+
135
+ def invoke_replace()
136
+ start_minibuffer_cmd("", "", :buf_replace_string)
137
+ end
138
+
139
+ def buf_replace(search_str, replace_str)
140
+ if $buffer.visual_mode?
141
+ r = $buffer.get_visual_mode_range
142
+ txt = $buffer[r]
143
+ txt.gsub!(search_str, replace_str)
144
+ $buffer.replace_range(r, txt)
145
+ $buffer.end_visual_mode
146
+ else
147
+ repbuf = $buffer.to_s.clone
148
+ repbuf.gsub!(search_str, replace_str)
149
+ tmppos = $buffer.pos
150
+ if repbuf == $buffer.to_s.clone
151
+ message("NO CHANGE. Replacing #{search_str} with #{replace_str}.")
152
+ else
153
+ $buffer.set_content(repbuf)
154
+ $buffer.set_pos(tmppos)
155
+ # $do_center = 1 #TODO
156
+ message("Replacing #{search_str} with #{replace_str}.")
157
+ end
158
+ end
159
+ end
160
+
161
+ # Requires instr in form "FROM/TO"
162
+ # Replaces all occurences of FROM with TO
163
+ def buf_replace_string(instr)
164
+ # puts "buf_replace_string(instr=#{instr})"
165
+
166
+ a = instr.split("/")
167
+ if a.size != 2
168
+ return
169
+ end
170
+ buf_replace(a[0], a[1])
171
+ end
172
+
173
+ # PopupFormGenerator.new().run
174
+ class PopupFormGenerator
175
+ def submit()
176
+ ret = {}
177
+ for id, entry in @vals
178
+ ret[id] = entry.text
179
+ end
180
+ @callback.call(ret)
181
+ @window.destroy
182
+ end
183
+
184
+ def initialize(params = nil)
185
+ @window = Gtk::Window.new(:toplevel)
186
+ # @window.screen = main_window.screen
187
+ # @window.title = title
188
+ # params = {}
189
+ # params["inputs"] = {}
190
+ # params["inputs"]["search"] = { :label => "Search", :type => :entry }
191
+ # params["inputs"]["replace"] = { :label => "Replace", :type => :entry }
192
+ # params["inputs"]["btn1"] = { :label => "Replace all", :type => :button }
193
+ # params[:callback] = proc { |x| puts "====="; puts x.inspect; puts "=====" }
194
+
195
+ @callback = params[:callback]
196
+ @window.title = ""
197
+
198
+ frame = Gtk::Frame.new()
199
+ frame.margin = 8
200
+ @window.add(frame)
201
+
202
+ # @window.title = params["title"]
203
+
204
+ # @callback = params["callback"]
205
+
206
+ infolabel = Gtk::Label.new
207
+ # infolabel.markup = params["title"]
208
+
209
+ vbox = Gtk::Box.new(:vertical, 8)
210
+ vbox.margin = 8
211
+ frame.add(vbox)
212
+
213
+ hbox = Gtk::Box.new(:horizontal, 8)
214
+ # @window.add(hbox)
215
+ @vals = {}
216
+
217
+ for id, elem in params["inputs"]
218
+ if elem[:type] == :button
219
+ button = Gtk::Button.new(:label => elem[:label])
220
+ hbox.pack_start(button, :expand => false, :fill => false, :padding => 0)
221
+ button.signal_connect "clicked" do
222
+ submit
223
+ end
224
+ elsif elem[:type] == :entry
225
+ label = Gtk::Label.new(elem[:label])
226
+ entry = Gtk::Entry.new
227
+ hbox.pack_start(label, :expand => false, :fill => false, :padding => 0)
228
+ hbox.pack_start(entry, :expand => false, :fill => false, :padding => 0)
229
+ @vals[id] = entry
230
+
231
+ entry.signal_connect("key_press_event") do |widget, event|
232
+ if event.keyval == Gdk::Keyval::KEY_Return
233
+ submit
234
+ true
235
+ elsif event.keyval == Gdk::Keyval::KEY_Escape
236
+ @window.destroy
237
+ true
238
+ else
239
+ false
240
+ end
241
+ end
242
+ end
243
+ end
244
+
245
+ vbox.pack_start(hbox, :expand => false, :fill => false, :padding => 0)
246
+
247
+ cancel_button = Gtk::Button.new(:label => "Cancel")
248
+ cancel_button.signal_connect "clicked" do
249
+ @window.destroy
250
+ end
251
+ hbox.pack_start(cancel_button, :expand => false, :fill => false, :padding => 0)
252
+
253
+ return
254
+ end
255
+
256
+ def run
257
+ if !@window.visible?
258
+ @window.show_all
259
+ else
260
+ @window.destroy
261
+ end
262
+ @window
263
+ end
264
+ end
265
+
266
+ class OneInputAction
267
+ def initialize(main_window, title, field_label, button_title, callback)
268
+ @window = Gtk::Window.new(:toplevel)
269
+ # @window.screen = main_window.screen
270
+ # @window.title = title
271
+ @window.title = ""
272
+
273
+ frame = Gtk::Frame.new()
274
+ frame.margin = 8
275
+ @window.add(frame)
276
+
277
+ infolabel = Gtk::Label.new
278
+ infolabel.markup = title
279
+
280
+ vbox = Gtk::Box.new(:vertical, 8)
281
+ vbox.margin = 8
282
+ frame.add(vbox)
283
+
284
+ hbox = Gtk::Box.new(:horizontal, 8)
285
+ # @window.add(hbox)
286
+ vbox.pack_start(infolabel, :expand => false, :fill => false, :padding => 0)
287
+ vbox.pack_start(hbox, :expand => false, :fill => false, :padding => 0)
288
+
289
+ button = Gtk::Button.new(:label => button_title)
290
+ cancel_button = Gtk::Button.new(:label => "Cancel")
291
+
292
+ label = Gtk::Label.new(field_label)
293
+
294
+ @entry1 = Gtk::Entry.new
295
+
296
+ button.signal_connect "clicked" do
297
+ callback.call(@entry1.text)
298
+ @window.destroy
299
+ end
300
+
301
+ cancel_button.signal_connect "clicked" do
302
+ @window.destroy
303
+ end
304
+
305
+ @entry1.signal_connect("key_press_event") do |widget, event|
306
+ if event.keyval == Gdk::Keyval::KEY_Return
307
+ callback.call(@entry1.text)
308
+ @window.destroy
309
+ true
310
+ elsif event.keyval == Gdk::Keyval::KEY_Escape
311
+ @window.destroy
312
+ true
313
+ else
314
+ false
315
+ end
316
+ end
317
+
318
+ hbox.pack_start(label, :expand => false, :fill => false, :padding => 0)
319
+ hbox.pack_start(@entry1, :expand => false, :fill => false, :padding => 0)
320
+ hbox.pack_start(button, :expand => false, :fill => false, :padding => 0)
321
+ hbox.pack_start(cancel_button, :expand => false, :fill => false, :padding => 0)
322
+ return
323
+ end
324
+
325
+ def run
326
+ if !@window.visible?
327
+ @window.show_all
328
+ else
329
+ @window.destroy
330
+ end
331
+ @window
332
+ end
333
+ end
@@ -0,0 +1,32 @@
1
+ class Converter
2
+ def initialize(obj, type, id = nil)
3
+ @obj = obj
4
+ @type = type
5
+ if id != nil
6
+ $vma.reg_conv(self, id)
7
+ end
8
+ end
9
+
10
+ def apply(txt)
11
+ if @type == :gsub
12
+ return txt.gsub(@obj[0], @obj[1])
13
+ elsif @type == :lambda
14
+ return @obj.call(txt)
15
+ end
16
+ end
17
+ end
18
+
19
+ Converter.new(lambda { |x| x.split("\n").collect { |x| r = x.strip }.select { |y| !y.empty? }.join(" ") + "\n" }, :lambda, :joinlines)
20
+
21
+ Converter.new(lambda { |x| x.split("\n").sort.join("\n") }, :lambda, :sortlines)
22
+ Converter.new(lambda { |x| x.split(/\s+/).sort.join(" ") }, :lambda, :sortwords)
23
+ Converter.new(lambda { |x| x.split("\n").collect { |b| b.scan(/(\d+(\.\d+)?)/).collect { |c| c[0] }.join(" ") }.join("\n") }, :lambda, :getnums_on_lines)
24
+
25
+ Converter.new(lambda { |x| x + "\n" + x.split("\n").collect { |b| b.scan(/(\d+(\.\d+)?)/).collect { |c| c[0] }.join(" ") }.join("\n") }, :lambda, :getnums_on_lines)
26
+
27
+ Converter.new(lambda { |x|
28
+ nums = x.scan(/(\d+(\.\d+)?)/).collect { |c| c[0] }
29
+ sum = nums.inject(0) { |sum, x| sum + x.to_f }
30
+ x + "\n" + nums.join("+") + "=#{sum}"
31
+ }, :lambda, :sum_of_numbers)
32
+