vimamsa 0.1.10 → 0.1.11

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,12 +1,26 @@
1
1
  require "parallel"
2
+
3
+ # Limit file search to these extensions:
4
+ $find_extensions = [".txt", ".h", ".c", ".cpp", ".hpp", ".rb"]
5
+ $search_dirs = []
6
+
2
7
  class FileFinder
8
+ def self.update_index()
9
+ message("Start updating file index")
10
+ Thread.new {
11
+ recursively_find_files()
12
+ message("Finnish updating file index")
13
+ }
14
+ end
15
+
3
16
  def initialize()
4
- $hook.register(:shutdown, self.method("save"))
5
- $dir_list = vma.marshal_load("file_index")
17
+ vma.hook.register(:shutdown, self.method("save"))
18
+ @dir_list = vma.marshal_load("file_index")
6
19
  end
7
20
 
8
21
  def save()
9
- vma.marshal_save("file_index", $dir_list)
22
+ debug "SAVE FILE INDEX", 2
23
+ vma.marshal_save("file_index", @dir_list)
10
24
  end
11
25
 
12
26
  def start_gui()
@@ -16,85 +30,69 @@ class FileFinder
16
30
  end
17
31
  l = []
18
32
  $select_keys = ["h", "l", "f", "d", "s", "a", "g", "z"]
19
- if $dir_list == nil
20
- Thread.new { recursively_find_files() }
33
+ if @dir_list == nil
34
+ Thread.new { FileFinder.recursively_find_files() }
21
35
  end
22
36
 
37
+ # select_callback = proc { |search_str, idx| gui_file_finder_select_callback(search_str, idx) }
38
+ select_callback = self.method("gui_file_finder_select_callback")
39
+ update_callback = self.method("gui_file_finder_update_callback")
40
+
23
41
  gui_select_update_window(l, $select_keys.collect { |x| x.upcase },
24
- "gui_file_finder_select_callback",
25
- "gui_file_finder_update_callback")
42
+ # "gui_file_finder_select_callback",
43
+ select_callback,
44
+ update_callback)
26
45
  end
27
- end
28
46
 
29
- def update_file_index()
30
- message("Start updating file index")
31
- Thread.new {
32
- recursively_find_files()
33
- message("Finnish updating file index")
34
- }
35
- end
36
-
37
- def recursively_find_files()
38
- debug("START find files")
39
- dlist = []
40
-
41
- for d in $search_dirs
42
- debug("FIND FILEs IN #{d}")
43
- dlist = dlist + Dir.glob("#{d}/**/*").select { |e| File.file?(e) and $find_extensions.include?(File.extname(e)) }
44
- debug("FIND FILEs IN #{d} END") end #$dir_list = Dir.glob('./**/*').select { |e| File.file? e }
45
- debug("END find files2")
46
- $dir_list = dlist
47
- debug("END find files")
48
- return $dir_list
49
- end
50
-
51
- def filter_files(search_str)
52
- dir_hash = {}
53
- scores = Parallel.map($dir_list, in_threads: 8) do |file|
54
- [file, srn_dst(search_str, file)]
55
- end
56
- for s in scores
57
- dir_hash[s[0]] = s[1] if s[1] > 0
58
- end
59
- # debug scores
60
- dir_hash = dir_hash.sort_by { |k, v| -v }
61
- dir_hash = dir_hash[0..20]
62
- dir_hash.map do |file, d|
63
- debug "D:#{d} #{file}"
47
+ def gui_file_finder_update_callback(search_str = "")
48
+ debug "FILE FINDER UPDATE CALLBACK: #{search_str}"
49
+ if (search_str.size > 1)
50
+ files = filter_files(search_str)
51
+ @file_search_list = files
52
+ return files
53
+ #debug files.inspect
54
+ #return files.values
55
+ end
56
+ return []
64
57
  end
65
- return dir_hash
66
- end
67
58
 
68
- def gui_file_finder_update_callback(search_str = "")
69
- debug "FILE FINDER UPDATE CALLBACK: #{search_str}"
70
- if (search_str.size > 1)
71
- files = filter_files(search_str)
72
- $file_search_list = files
73
- return files
74
- #debug files.inspect
75
- #return files.values
59
+ def gui_file_finder_select_callback(search_str, idx)
60
+ selected_file = @file_search_list[idx][0]
61
+ debug "FILE FINDER SELECT CALLBACK: s=#{search_str},i=#{idx}: #{selected_file}"
62
+ gui_select_window_close(0)
63
+ open_new_file(selected_file)
76
64
  end
77
- return []
78
- end
79
65
 
80
- def gui_file_finder_select_callback(search_str, idx)
81
- selected_file = $file_search_list[idx][0]
82
- debug "FILE FINDER SELECT CALLBACK: s=#{search_str},i=#{idx}: #{selected_file}"
83
- gui_select_window_close(0)
84
- open_new_file(selected_file)
85
- end
66
+ def self.recursively_find_files()
67
+ debug("START find files")
68
+ dlist = []
86
69
 
87
- def gui_file_finder_handle_char(c)
88
- debug "BUFFER SELECTOR INPUT CHAR: #{c}"
89
- buffer_i = $select_keys.index(c)
90
- if buffer_i != nil
91
- gui_file_finder_callback(buffer_i)
70
+ for d in $search_dirs
71
+ debug("FIND FILEs IN #{d}")
72
+ dlist = dlist + Dir.glob("#{d}/**/*").select { |e| File.file?(e) and $find_extensions.include?(File.extname(e)) }
73
+ debug("FIND FILEs IN #{d} END")
74
+ end #@dir_list = Dir.glob('./**/*').select { |e| File.file? e }
75
+ debug("END find files2")
76
+ @dir_list = dlist
77
+ debug("END find files")
78
+ return @dir_list
92
79
  end
93
- end
94
80
 
95
- # TODO: delete?
96
- def gui_file_finder_init()
97
- $kbd.add_mode("Z", :filefinder)
98
- bindkey "Z enter", "$kbd.set_mode(:command)"
99
- bindkey "Z return", "$kbd.set_mode(:command)"
81
+ def filter_files(search_str)
82
+ dir_hash = {}
83
+ scores = Parallel.map(@dir_list, in_threads: 8) do |file|
84
+ [file, srn_dst(search_str, file)]
85
+ end
86
+ for s in scores
87
+ dir_hash[s[0]] = s[1] if s[1] > 0
88
+ end
89
+ # debug scores
90
+ dir_hash = dir_hash.sort_by { |k, v| -v }
91
+ dir_hash = dir_hash[0..20]
92
+ dir_hash.map do |file, d|
93
+ debug "D:#{d} #{file}"
94
+ end
95
+ return dir_hash
96
+ end
100
97
  end
98
+
@@ -8,8 +8,8 @@ class FileHistory
8
8
  # x = self.method("update")
9
9
  # x.call("ASFASF")
10
10
 
11
- $hook.register(:change_buffer, self.method("update"))
12
- $hook.register(:shutdown, self.method("save"))
11
+ vma.hook.register(:change_buffer, self.method("update"))
12
+ vma.hook.register(:shutdown, self.method("save"))
13
13
 
14
14
  reg_act(:fhist_remove_nonexisting, proc { remove_nonexisting }, "Cleanup history, remove non-existing files")
15
15
 
@@ -98,7 +98,6 @@ def gui_file_history_update_callback(search_str = "")
98
98
  end
99
99
 
100
100
  def gui_file_history_select_callback(search_str, idx)
101
- # selected_file = $file_search_list[idx][0]
102
101
  selected_file = $search_list[idx][0]
103
102
 
104
103
  debug "FILE HISTORY SELECT CALLBACK: s=#{search_str},i=#{idx}: #{selected_file}"
@@ -111,7 +111,7 @@ class FileManager
111
111
  end
112
112
 
113
113
  if @buf.nil?
114
- @buf = create_new_file(nil, s)
114
+ @buf = create_new_buffer(s,"filemgr")
115
115
  @buf.module = self
116
116
  @buf.active_kbd_mode = :file_exp
117
117
  else
@@ -0,0 +1,122 @@
1
+ # PopupFormGenerator.new().run
2
+ class PopupFormGenerator
3
+ def submit()
4
+ for id, entry in @vals
5
+ @ret[id] = entry.text
6
+ end
7
+ if !@callback.nil?
8
+ @callback.call(@ret)
9
+ end
10
+ @window.destroy
11
+ end
12
+
13
+ def initialize(params = nil)
14
+ @ret = {}
15
+ @window = Gtk::Window.new()
16
+ # @window.screen = main_window.screen
17
+ # @window.title = title
18
+ # params = {}
19
+ # params["inputs"] = {}
20
+ # params["inputs"]["search"] = { :label => "Search", :type => :entry }
21
+ # params["inputs"]["replace"] = { :label => "Replace", :type => :entry }
22
+ # params["inputs"]["btn1"] = { :label => "Replace all", :type => :button }
23
+ # params[:callback] = proc { |x| puts "====="; puts x.inspect; puts "=====" }
24
+
25
+
26
+ @callback = params[:callback]
27
+ @window.title = ""
28
+
29
+ frame = Gtk::Frame.new()
30
+ frame.margin_bottom = 8
31
+ frame.margin_top = 8
32
+ frame.margin_end = 8
33
+ frame.margin_start = 8
34
+
35
+ @window.set_child(frame)
36
+
37
+ # @window.title = params["title"]
38
+
39
+ # @callback = params["callback"]
40
+
41
+ vbox = Gtk::Box.new(:vertical, 8)
42
+ vbox.margin_bottom = 8
43
+ vbox.margin_top = 8
44
+ vbox.margin_end = 8
45
+ vbox.margin_start = 8
46
+
47
+ frame.set_child(vbox)
48
+
49
+ if params.has_key?("title")
50
+ infolabel = Gtk::Label.new
51
+ infolabel.markup = params["title"]
52
+ #TODO:gtk4
53
+ # vbox.pack_start(infolabel, :expand => false, :fill => false, :padding => 0)
54
+ vbox.pack_end(infolabel, :expand => false, :fill => false, :padding => 0)
55
+ end
56
+
57
+ hbox = Gtk::Box.new(:horizontal, 8)
58
+ @vals = {}
59
+ @default_button = nil
60
+
61
+ for id, elem in params["inputs"]
62
+ if elem[:type] == :button
63
+ button = Gtk::Button.new(:label => elem[:label])
64
+ hbox.pack_end(button, :expand => false, :fill => false, :padding => 0)
65
+ if elem[:default_focus] == true
66
+ @default_button = button
67
+ end
68
+ button.signal_connect "clicked" do
69
+ @ret[id] = "submit"
70
+ submit
71
+ end
72
+ elsif elem[:type] == :entry
73
+ label = Gtk::Label.new(elem[:label])
74
+ entry = Gtk::Entry.new
75
+ if elem.has_key?(:initial_text)
76
+ entry.text = elem[:initial_text]
77
+ end
78
+ hbox.pack_end(label, :expand => false, :fill => false, :padding => 0)
79
+ hbox.pack_end(entry, :expand => false, :fill => false, :padding => 0)
80
+ @vals[id] = entry
81
+
82
+ press = Gtk::EventControllerKey.new
83
+ press.set_propagation_phase(Gtk::PropagationPhase::CAPTURE)
84
+ entry.add_controller(press)
85
+ press.signal_connect "key-pressed" do |gesture, keyval, keycode, y|
86
+ if keyval == Gdk::Keyval::KEY_Return
87
+ submit
88
+ true
89
+ elsif keyval == Gdk::Keyval::KEY_Escape
90
+ @window.destroy
91
+ true
92
+ else
93
+ false
94
+ end
95
+ end
96
+ end
97
+ end
98
+
99
+ vbox.pack_end(hbox, :expand => false, :fill => false, :padding => 0)
100
+
101
+ cancel_button = Gtk::Button.new(:label => "Cancel")
102
+ cancel_button.signal_connect "clicked" do
103
+ @window.destroy
104
+ end
105
+ hbox.pack_end(cancel_button, :expand => false, :fill => false, :padding => 0)
106
+ @cancel_button = cancel_button
107
+ return
108
+ end
109
+
110
+ def run
111
+ if !@window.visible?
112
+ @window.show
113
+ else
114
+ @window.destroy
115
+ end
116
+ if !@default_button.nil?
117
+ @default_button.grab_focus
118
+ end
119
+ @window.set_focus_visible(true)
120
+ @window
121
+ end
122
+ end