vimamsa 0.1.5 → 0.1.8

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,16 +1,23 @@
1
1
  require "pty"
2
2
 
3
- def exec_in_terminal(cmd)
4
- # puts "CMD:#{cmd}"
3
+ def exec_in_terminal(cmd, autoclose = false)
4
+ # debug "CMD:#{cmd}"
5
5
 
6
6
  # global to prevent garbage collect unlink
7
7
  $initf = Tempfile.new("bashinit")
8
- # puts $initf.path
8
+ # debug $initf.path
9
9
  $initf.write(cmd)
10
- $initf.write("rm #{$initf.path}\n")
11
- $initf.write("\nexec bash\n")
10
+ if autoclose
11
+ $initf.write("\nsleep 10; exit;\n")
12
+ $initf.write("rm #{$initf.path}\n")
13
+ else
14
+ $initf.write("rm #{$initf.path}\n")
15
+ $initf.write("\nexec bash\n")
16
+ end
12
17
  $initf.close
13
18
  # PTY.spawn("gnome-terminal", "--tab", "--", "bash", "-i", $initf.path, "-c", "exec bash")
19
+ # fork { exec "gnome-terminal", "--tab", "--", "bash", "-i", $initf.path, "-c", "exec bash" }
20
+ # Just another execution
14
21
  fork { exec "gnome-terminal", "--tab", "--", "bash", "-i", $initf.path, "-c", "exec bash" }
15
22
  end
16
23
 
@@ -19,13 +26,18 @@ def handle_drag_and_drop(fname)
19
26
  buf.handle_drag_and_drop(fname)
20
27
  end
21
28
 
29
+ def mkdir_if_not_exists(_dirpath)
30
+ dirpath = File.expand_path(_dirpath)
31
+ Dir.mkdir(dirpath) unless File.exist?(dirpath)
32
+ end
33
+
22
34
  class Editor
23
- attr_reader :file_content_search_paths, :file_name_search_paths
24
- attr_accessor :converters, :fh, :paint_stack
35
+ attr_reader :file_content_search_paths, :file_name_search_paths, :gui
36
+ attr_accessor :converters, :fh, :paint_stack, :kbd
25
37
  #attr_writer :call_func, :update_highlight
26
38
 
27
39
  def initialize()
28
- # Thread.new{10000.times{|x|sleep(3);10000.times{|y|y+2};puts "FOOTHREAD #{x}"}}
40
+ # Thread.new{10000.times{|x|sleep(3);10000.times{|y|y+2};debug "FOOTHREAD #{x}"}}
29
41
 
30
42
  # Search for content inside files (e.g. using ack/grep) in:
31
43
  @file_content_search_paths = []
@@ -42,7 +54,7 @@ class Editor
42
54
  def open_file_listener(added)
43
55
  if !added.empty?
44
56
  for fp in added
45
- sleep 0.1
57
+ sleep 0.1
46
58
  x = IO.read(fp)
47
59
  File.delete(fp)
48
60
  for f in x.lines
@@ -62,9 +74,11 @@ class Editor
62
74
 
63
75
  # GLib::Idle.add
64
76
  # Ripl.start :binding => binding
65
- # GLib::Idle.add(proc{ puts "IDLEFUNC"})
77
+ # GLib::Idle.add(proc{ debug "IDLEFUNC"})
66
78
  # GLib::Idle.add(proc { idle_func })
67
79
 
80
+ @gui = $vmag #TODO
81
+
68
82
  $hook = Hook.new
69
83
  register_plugin(:Hook, $hook)
70
84
  $macro = Macro.new
@@ -79,7 +93,8 @@ class Editor
79
93
 
80
94
  debug "ARGV: " + ARGV.inspect
81
95
  # build_key_bindings_tree
82
- $kbd = KeyBindingTree.new()
96
+ @kbd = KeyBindingTree.new()
97
+ $kbd = @kbd
83
98
  $kbd.add_mode("C", :command)
84
99
  $kbd.add_mode("I", :insert)
85
100
  $kbd.add_mode("V", :visual)
@@ -87,17 +102,18 @@ class Editor
87
102
  $kbd.add_mode("R", :readchar)
88
103
  $kbd.add_mode("B", :browse)
89
104
  $kbd.set_default_mode(:command)
90
- require "vimamsa/default_key_bindings"
105
+ require "vimamsa/key_bindings_vimlike"
91
106
  sleep(0.03)
92
107
 
93
108
  FileManager.init
109
+ BufferManager.init
94
110
 
95
- dot_dir = File.expand_path("~/.vimamsa")
96
- Dir.mkdir(dot_dir) unless File.exist?(dot_dir)
97
- listen_dir = File.expand_path("~/.vimamsa/listen")
98
- Dir.mkdir(listen_dir) unless File.exist?(dot_dir)
111
+ mkdir_if_not_exists("~/.vimamsa")
112
+ mkdir_if_not_exists("~/.vimamsa/backup")
113
+ mkdir_if_not_exists("~/.vimamsa/listen")
114
+ listen_dir = File.expand_path "~/.vimamsa/listen"
99
115
  listener = Listen.to(listen_dir) do |modified, added, removed|
100
- puts(modified: modified, added: added, removed: removed)
116
+ debug([modified: modified, added: added, removed: removed])
101
117
  open_file_listener(added)
102
118
  end
103
119
  listener.start
@@ -109,7 +125,7 @@ class Editor
109
125
  $cnf = eval(IO.read(settings_path))
110
126
  end
111
127
 
112
- # set_qt_style(1)
128
+ # set_gui_style(1)
113
129
 
114
130
  # Limit file search to these extensions:
115
131
  $find_extensions = [".txt", ".h", ".c", ".cpp", ".hpp", ".rb"]
@@ -139,7 +155,7 @@ class Editor
139
155
  if fname
140
156
  buffer = Buffer.new(read_file("", fname), fname)
141
157
  else
142
- buffer = Buffer.new("INIT\n")
158
+ buffer = Buffer.new(" \n")
143
159
  end
144
160
  $buffers << buffer
145
161
 
@@ -173,6 +189,11 @@ class Editor
173
189
  def buf()
174
190
  return $buffer
175
191
  end
192
+
193
+ def buffers()
194
+ return $buffers
195
+ end
196
+
176
197
 
177
198
  def marshal_save(varname, vardata)
178
199
  save_var_to_file(varname, Marshal.dump(vardata))
@@ -228,8 +249,7 @@ class Editor
228
249
  # Register converter
229
250
  def reg_conv(converter, converter_id)
230
251
  @converters[converter_id] = converter
231
- reg_act(converter_id, proc { $buffer.convert_selected_text(converter_id) }, "Converter #{converter_id}", [:selection])
232
- # reg_act(converter_id, "$buffer.convert_selected_text(:#{converter_id})", "Converter #{converter_id}", [:selection])
252
+ reg_act(converter_id, proc { $buffer.convert_selected_text(converter_id) }, "Converter #{converter_id}", { :scope => [:selection] })
233
253
  end
234
254
 
235
255
  def apply_conv(converter_id, txt)
@@ -250,30 +270,29 @@ class Editor
250
270
  exts = $cnf[:extensions_to_open]
251
271
  extname = Pathname.new(filepath).extname.downcase
252
272
  can_open = exts.include?(extname)
253
- puts "CAN OPEN?: #{can_open}"
273
+ debug "CAN OPEN?: #{can_open}"
254
274
  return can_open
255
275
  end
256
276
  end
257
277
 
258
278
  def _quit()
259
279
  vma.shutdown
260
- exit
280
+ Gtk.main_quit
261
281
  end
262
282
 
263
283
  def fatal_error(msg)
264
- puts msg
284
+ debug msg
265
285
  exit!
266
286
  end
267
287
 
268
288
  def file_saveas(filename)
269
- $buffer.set_filename(filename)
270
- $buffer.save()
289
+ buf.save_as_callback(filename)
271
290
  end
272
291
 
273
292
  def open_file_dialog()
274
293
  path = ""
275
294
  path = $buffer.fname if $buffer.fname
276
- qt_open_file_dialog(File.dirname(path))
295
+ gui_open_file_dialog(File.dirname(path))
277
296
  end
278
297
 
279
298
  def system_clipboard_changed(clipboard_contents)
@@ -283,14 +302,18 @@ def system_clipboard_changed(clipboard_contents)
283
302
  $paste_lines = false
284
303
  end
285
304
  $clipboard << clipboard_contents
286
- # puts $clipboard[-1]
305
+ # debug $clipboard[-1]
287
306
  $clipboard = $clipboard[-([$clipboard.size, max_clipboard_items].min)..-1]
288
307
  end
289
308
 
309
+ def get_clipboard()
310
+ return $clipboard[-1]
311
+ end
312
+
290
313
  def set_clipboard(s)
291
314
  if !(s.class <= String) or s.size == 0
292
- puts s.inspect
293
- puts [s, s.class, s.size]
315
+ debug s.inspect
316
+ debug [s, s.class, s.size]
294
317
  log_error("s.class != String or s.size == 0")
295
318
  Ripl.start :binding => binding
296
319
  return
@@ -361,9 +384,9 @@ def diff_buffer()
361
384
  infile.write($buffer.to_s)
362
385
  infile.flush
363
386
  cmd = "diff -w '#{orig_path}' #{infile.path}"
364
- # puts cmd
387
+ # debug cmd
365
388
  bufstr << run_cmd(cmd)
366
- # puts bufstr
389
+ # debug bufstr
367
390
  infile.close; infile.unlink
368
391
  create_new_file(nil, bufstr)
369
392
  end
@@ -407,9 +430,9 @@ def minibuffer_new_char(c)
407
430
  #$buffer = $minibuffer
408
431
  end
409
432
 
410
- def readchar_new_char(c)
411
- $input_char_call_func.call(c)
412
- end
433
+ # def readchar_new_char(c)
434
+ # $input_char_call_func.call(c)
435
+ # end
413
436
 
414
437
  def minibuffer_delete()
415
438
  $minibuffer.delete(BACKWARD_CHAR)
@@ -417,7 +440,7 @@ end
417
440
 
418
441
  def message(s)
419
442
  s = "[#{DateTime.now().strftime("%H:%M")}] #{s}"
420
- puts s
443
+ debug s
421
444
 
422
445
  $vmag.add_to_minibuf(s)
423
446
  # $minibuffer = Buffer.new(s, "")
@@ -455,7 +478,7 @@ GUESS_ENCODING_ORDER = [
455
478
  def create_new_file(filename = nil, file_contents = "\n")
456
479
  debug "NEW FILE CREATED"
457
480
  buffer = Buffer.new(file_contents)
458
- # qt_set_current_buffer(buffer.id) #TODO: remove?
481
+ # gui_set_current_buffer(buffer.id) #TODO: remove?
459
482
  $buffers << buffer
460
483
  return buffer
461
484
  end
@@ -481,7 +504,7 @@ def load_buffer(fname)
481
504
  end
482
505
  debug("LOAD BUFFER: #{fname}")
483
506
  buffer = Buffer.new(read_file("", fname), fname)
484
- # qt_set_current_buffer(buffer.id)
507
+ # gui_set_current_buffer(buffer.id)
485
508
  buffer.set_active
486
509
  debug("DONE LOAD: #{fname}")
487
510
  #buf = filter_buffer(buffer)
@@ -528,15 +551,12 @@ def draw_text(str, x, y)
528
551
  vma.paint_stack << [4, x, y, str]
529
552
  end
530
553
 
531
- def center_on_current_line__2del()
532
- center_where_cursor
533
- end
534
-
535
554
  def hook_draw()
536
555
  # TODO: as hook.register
537
556
  # easy_jump_draw()
538
557
  end
539
558
 
559
+ #TODO: delete this
540
560
  def render_buffer(buffer = 0, reset = 0)
541
561
  tmpbuf = $buffer.to_s
542
562
  debug "pos:#{$buffer.pos} L:#{$buffer.lpos} C:#{$buffer.cpos}"
@@ -549,8 +569,6 @@ def render_buffer(buffer = 0, reset = 0)
549
569
  t1 = Time.now
550
570
  hook_draw()
551
571
 
552
- render_text(tmpbuf, pos, selection_start, reset) #TODO: remove?
553
-
554
572
  if $buffer.need_redraw?
555
573
  hpt_scan_images() if $debug #experimental
556
574
  end
@@ -593,7 +611,7 @@ end
593
611
  def run_cmd(cmd)
594
612
  tmpf = Tempfile.new("vmarun", "/tmp").path
595
613
  cmd = "#{cmd} > #{tmpf}"
596
- puts "CMD:\n#{cmd}"
614
+ debug "CMD:\n#{cmd}"
597
615
  system("bash", "-c", cmd)
598
616
  res_str = File.read(tmpf)
599
617
  return res_str
@@ -622,11 +640,11 @@ def exec_cmd(bin_name, arg1 = nil, arg2 = nil, arg3 = nil, arg4 = nil, arg5 = ni
622
640
  end
623
641
 
624
642
  def file_is_text_file(fpath)
625
- puts "file_is_text_file(#{fpath})"
643
+ debug "file_is_text_file(#{fpath})"
626
644
  fpath = File.expand_path(fpath)
627
645
  return false if !File.exist?(fpath)
628
646
  r = exec_cmd("file", fpath)
629
- puts "DEBUG:#{r}"
647
+ debug "DEBUG:#{r}"
630
648
  return true if r.match(/UTF-8.*text/)
631
649
  return true if r.match(/ASCII.*text/)
632
650
  return false
@@ -662,6 +680,6 @@ def find_project_dir_of_cur_buffer()
662
680
  if $buffer.fname
663
681
  pdir = find_project_dir_of_fn($buffer.fname)
664
682
  end
665
- # puts "Proj dir of current file: #{pdir}"
683
+ # debug "Proj dir of current file: #{pdir}"
666
684
  return pdir
667
685
  end
@@ -1,5 +1,4 @@
1
1
  require "parallel"
2
-
3
2
  class FileFinder
4
3
  def initialize()
5
4
  $hook.register(:shutdown, self.method("save"))
@@ -21,7 +20,7 @@ class FileFinder
21
20
  Thread.new { recursively_find_files() }
22
21
  end
23
22
 
24
- qt_select_update_window(l, $select_keys.collect { |x| x.upcase },
23
+ gui_select_update_window(l, $select_keys.collect { |x| x.upcase },
25
24
  "gui_file_finder_select_callback",
26
25
  "gui_file_finder_update_callback")
27
26
  end
@@ -59,22 +58,22 @@ def filter_files(search_str)
59
58
  for s in scores
60
59
  dir_hash[s[0]] = s[1] if s[1] > 0
61
60
  end
62
- # puts scores
61
+ # debug scores
63
62
  dir_hash = dir_hash.sort_by { |k, v| -v }
64
63
  dir_hash = dir_hash[0..20]
65
64
  dir_hash.map do |file, d|
66
- puts "D:#{d} #{file}"
65
+ debug "D:#{d} #{file}"
67
66
  end
68
67
  return dir_hash
69
68
  end
70
69
 
71
70
  def gui_file_finder_update_callback(search_str = "")
72
- puts "FILE FINDER UPDATE CALLBACK: #{search_str}"
71
+ debug "FILE FINDER UPDATE CALLBACK: #{search_str}"
73
72
  if (search_str.size > 1)
74
73
  files = filter_files(search_str)
75
74
  $file_search_list = files
76
75
  return files
77
- #puts files.inspect
76
+ #debug files.inspect
78
77
  #return files.values
79
78
  end
80
79
  return []
@@ -83,12 +82,12 @@ end
83
82
  def gui_file_finder_select_callback(search_str, idx)
84
83
  selected_file = $file_search_list[idx][0]
85
84
  debug "FILE FINDER SELECT CALLBACK: s=#{search_str},i=#{idx}: #{selected_file}"
86
- qt_select_window_close(0)
85
+ gui_select_window_close(0)
87
86
  open_new_file(selected_file)
88
87
  end
89
88
 
90
89
  def gui_file_finder_handle_char(c)
91
- puts "BUFFER SELECTOR INPUT CHAR: #{c}"
90
+ debug "BUFFER SELECTOR INPUT CHAR: #{c}"
92
91
  buffer_i = $select_keys.index(c)
93
92
  if buffer_i != nil
94
93
  gui_file_finder_callback(buffer_i)
@@ -21,7 +21,7 @@ class FileHistory
21
21
  # end
22
22
 
23
23
  def update(buf)
24
- puts "FileHistory.update(buf=#{buf.fname})"
24
+ debug "FileHistory.update(buf=#{buf.fname})"
25
25
  return if !buf.fname
26
26
  @history[buf.fname] if !@history[buf.fname]
27
27
  if !@history[buf.fname]
@@ -29,7 +29,7 @@ class FileHistory
29
29
  else
30
30
  @history[buf.fname] += 1
31
31
  end
32
- puts @history
32
+ debug @history
33
33
 
34
34
  # puts "FileHistory.update(buf=#{buf})"
35
35
  end
@@ -55,9 +55,14 @@ class FileHistory
55
55
  l = []
56
56
  $select_keys = ["h", "l", "f", "d", "s", "a", "g", "z"]
57
57
 
58
- qt_select_update_window(l, $select_keys.collect { |x| x.upcase },
59
- "gui_file_history_select_callback",
60
- "gui_file_history_update_callback")
58
+ opt = { :title => "File history search",
59
+ :desc => "Search for previously opened files. Fuzzy search." ,
60
+ :columns => [{:title=>'Filename',:id=>0}]
61
+ }
62
+ gui_select_update_window(l, $select_keys.collect { |x| x.upcase },
63
+ "gui_file_history_select_callback",
64
+ "gui_file_history_update_callback",
65
+ opt)
61
66
  end
62
67
  end
63
68
 
@@ -78,7 +83,7 @@ def fuzzy_filter(search_str, list, maxfinds)
78
83
  end
79
84
 
80
85
  def gui_file_history_update_callback(search_str = "")
81
- puts "gui_file_history_update_callback: #{search_str}"
86
+ debug "gui_file_history_update_callback: #{search_str}"
82
87
  return [] if $vma.fh.history.empty?
83
88
  $search_list = []
84
89
  files = $vma.fh.history.keys.sort.collect { |x| [x, 0] }
@@ -86,8 +91,10 @@ def gui_file_history_update_callback(search_str = "")
86
91
  if (search_str.size > 1)
87
92
  files = fuzzy_filter(search_str, $vma.fh.history.keys, 40)
88
93
  end
94
+
89
95
  $search_list = files
90
- return files
96
+ ret = files.collect{|x|[x[0]]}
97
+ return ret
91
98
  end
92
99
 
93
100
  def gui_file_history_select_callback(search_str, idx)
@@ -95,6 +102,6 @@ def gui_file_history_select_callback(search_str, idx)
95
102
  selected_file = $search_list[idx][0]
96
103
 
97
104
  debug "FILE HISTORY SELECT CALLBACK: s=#{search_str},i=#{idx}: #{selected_file}"
98
- qt_select_window_close(0)
105
+ gui_select_window_close(0)
99
106
  open_new_file(selected_file)
100
107
  end
@@ -1,3 +1,4 @@
1
+
1
2
  class FileManager
2
3
  @@cur
3
4
 
@@ -28,11 +29,11 @@ class FileManager
28
29
  # bindkey "C o", :delete_state
29
30
 
30
31
  $kbd.add_minor_mode("fexp", :file_exp, :command)
31
-
32
+
32
33
  bindkey "fexp o m", :fexp_sort_mtime
33
- bindkey "fexp o f", :fexp_sort_fname
34
+ bindkey "fexp o f", :fexp_sort_fname
34
35
 
35
- # bindkey "fexp l", [:fexp_right, proc { puts "==fexp_right==" }, ""]
36
+ # bindkey "fexp l", [:fexp_right, proc { debug "==fexp_right==" }, ""]
36
37
  bindkey "fexp h", :fexp_chdir_parent
37
38
  bindkey "fexp esc", [:fexp_quit, proc { $kbd.set_mode(:command) }, ""]
38
39
  bindkey "fexp enter", :fexp_select
@@ -49,7 +50,7 @@ class FileManager
49
50
  @@cur = self
50
51
  ld = buflist.get_last_dir
51
52
  dir_to_buf(ld)
52
- # puts "ld=#{ld}"
53
+ # debug "ld=#{ld}"
53
54
  # dlist = Dir["#{ld}/*"]
54
55
  end
55
56
 
@@ -83,7 +84,7 @@ class FileManager
83
84
  next
84
85
  end
85
86
  next if x[0] == "."
86
- if File.directory?(fpath)
87
+ if File.directory?(fpath)
87
88
  # if f.directory?(fpath)
88
89
  @cdirs << x
89
90
  else
@@ -126,10 +127,10 @@ class FileManager
126
127
 
127
128
  def select_line
128
129
  return if @buf.lpos < @header.size
129
- # puts "def select_line"
130
+ # debug "def select_line"
130
131
  fn = fullp(@buf.get_current_line[0..-2])
131
132
  if File.directory?(fn)
132
- puts "CHDIR: #{fn}"
133
+ debug "CHDIR: #{fn}"
133
134
  dir_to_buf(fn)
134
135
  # elsif vma.can_open_extension?(fn)
135
136
  # jump_to_file(fn)
@@ -139,6 +140,6 @@ class FileManager
139
140
  else
140
141
  open_with_default_program(fn)
141
142
  end
142
- # puts l.inspect
143
+ # debug l.inspect
143
144
  end
144
145
  end