vimamsa 0.1.9 → 0.1.11

Sign up to get free protection for your applications and to get access to all the features.
@@ -32,8 +32,8 @@ def mkdir_if_not_exists(_dirpath)
32
32
  end
33
33
 
34
34
  class Editor
35
- attr_reader :file_content_search_paths, :file_name_search_paths, :gui
36
- attr_accessor :converters, :fh, :paint_stack, :kbd
35
+ attr_reader :file_content_search_paths, :file_name_search_paths, :gui, :hook, :macro
36
+ attr_accessor :converters, :fh, :paint_stack, :kbd, :langsrv
37
37
  #attr_writer :call_func, :update_highlight
38
38
 
39
39
  def initialize()
@@ -73,14 +73,17 @@ class Editor
73
73
  @gui = $vmag #TODO
74
74
 
75
75
  $hook = Hook.new
76
- register_plugin(:Hook, $hook)
77
- $macro = Macro.new
76
+ @hook = $hook
77
+ register_plugin(:Hook, @hook)
78
+ @macro = Macro.new
79
+ $macro = @macro
78
80
  register_plugin(:Macro, $macro)
79
81
  $search = Search.new
80
82
  register_plugin(:Search, $search)
81
83
 
82
84
  $buffers = BufferList.new
83
85
  $minibuffer = Buffer.new(">", "")
86
+ @langsrv = {}
84
87
 
85
88
  require "vimamsa/text_transforms"
86
89
 
@@ -88,20 +91,13 @@ class Editor
88
91
  # build_key_bindings_tree
89
92
  @kbd = KeyBindingTree.new()
90
93
  $kbd = @kbd
91
- @kbd.add_mode("C", :command)
92
- @kbd.add_mode("I", :insert)
93
- @kbd.add_mode("V", :visual)
94
- @kbd.add_mode("M", :minibuffer)
95
- @kbd.add_mode("R", :readchar)
96
- @kbd.add_mode("B", :browse)
97
- @kbd.set_default_mode(:command)
98
- @kbd.set_mode(:command)
99
- @kbd.show_state_trail
100
94
  require "vimamsa/key_bindings_vimlike"
101
95
  sleep(0.03)
102
96
 
103
97
  FileManager.init
104
98
  BufferManager.init
99
+
100
+ @gui.init_menu
105
101
 
106
102
  mkdir_if_not_exists("~/.vimamsa")
107
103
  mkdir_if_not_exists("~/.vimamsa/backup")
@@ -130,15 +126,19 @@ class Editor
130
126
 
131
127
  # set_gui_style(1)
132
128
 
133
- # Limit file search to these extensions:
134
- $find_extensions = [".txt", ".h", ".c", ".cpp", ".hpp", ".rb"]
135
-
129
+ #TODO: remove
136
130
  dotfile = read_file("", "~/.vimamsarc")
137
131
  eval(dotfile) if dotfile
138
-
132
+
139
133
  custom_script = read_file("", custom_fn)
140
134
  eval(custom_script) if custom_script
141
135
 
136
+ if conf(:enable_lsp)
137
+ require "vimamsa/langservp"
138
+ @langsrv["ruby"] = LangSrv.new("ruby")
139
+ @langsrv["cpp"] = LangSrv.new("cpp")
140
+ end
141
+
142
142
  # build_options
143
143
 
144
144
  fname = nil
@@ -161,18 +161,10 @@ class Editor
161
161
  end
162
162
 
163
163
  if fname
164
- buffer = Buffer.new(read_file("", fname), fname)
164
+ open_new_file(fname)
165
165
  else
166
- buffer = Buffer.new(" \n")
166
+ create_new_buffer(file_contents = "\n")
167
167
  end
168
- $buffers << buffer
169
-
170
- # load_theme($cnf[:theme])
171
-
172
- # render_buffer($buffer, 1) #TODO
173
-
174
- # gui_select_buffer_init #TODO
175
- # gui_file_finder_init #TODO
176
168
 
177
169
  #Load plugins
178
170
  require "vimamsa/file_history.rb"
@@ -185,7 +177,7 @@ class Editor
185
177
  # To access via vma.FileFinder
186
178
  # self.define_singleton_method(:FileFinder) { @_plugins[:FileFinder] }
187
179
 
188
- $hook.call(:after_init)
180
+ @hook.call(:after_init)
189
181
  end
190
182
 
191
183
  def register_plugin(name, obj)
@@ -198,6 +190,10 @@ class Editor
198
190
  return $buffer
199
191
  end
200
192
 
193
+ def buf=(aa)
194
+ $buffer = aa
195
+ end
196
+
201
197
  def buffers()
202
198
  return $buffers
203
199
  end
@@ -239,8 +235,9 @@ class Editor
239
235
  end
240
236
 
241
237
  def shutdown()
242
- $hook.call(:shutdown)
238
+ @hook.call(:shutdown)
243
239
  save_state
240
+ @gui.quit
244
241
  end
245
242
 
246
243
  def save_state
@@ -256,7 +253,7 @@ class Editor
256
253
  # Register converter
257
254
  def reg_conv(converter, converter_id)
258
255
  @converters[converter_id] = converter
259
- reg_act(converter_id, proc { $buffer.convert_selected_text(converter_id) }, "Converter #{converter_id}", { :scope => [:selection] })
256
+ reg_act(converter_id, proc { vma.buf.convert_selected_text(converter_id) }, "Converter #{converter_id}", { :scope => [:selection] })
260
257
  end
261
258
 
262
259
  def apply_conv(converter_id, txt)
@@ -268,7 +265,7 @@ class Editor
268
265
  r = @file_content_search_paths.clone
269
266
  p = find_project_dir_of_cur_buffer()
270
267
  if p.nil?
271
- p = buf.dirname # Search dir of current file by default
268
+ p = vma.buffers.last_dir
272
269
  end
273
270
 
274
271
  if p and !@file_content_search_paths.include?(p)
@@ -289,7 +286,7 @@ end
289
286
 
290
287
  def _quit()
291
288
  vma.shutdown
292
- Gtk.main_quit
289
+ # Gtk.main_quit
293
290
  end
294
291
 
295
292
  def fatal_error(msg)
@@ -303,10 +300,11 @@ end
303
300
 
304
301
  def open_file_dialog()
305
302
  path = ""
306
- path = $buffer.fname if $buffer.fname
303
+ path = vma.buf.fname if vma.buf.fname
307
304
  gui_open_file_dialog(File.dirname(path))
308
305
  end
309
306
 
307
+ #TODO:delete?
310
308
  def system_clipboard_changed(clipboard_contents)
311
309
  max_clipboard_items = 100
312
310
  if clipboard_contents != $clipboard[-1]
@@ -339,8 +337,8 @@ end
339
337
 
340
338
  def set_cursor_pos(new_pos)
341
339
  buf.set_pos(new_pos)
342
- #render_buffer($buffer)
343
- debug "New pos: #{new_pos}lpos:#{$buffer.lpos} cpos:#{$buffer.cpos}"
340
+ #render_buffer(vma.buf)
341
+ debug "New pos: #{new_pos}lpos:#{vma.buf.lpos} cpos:#{vma.buf.cpos}"
344
342
  end
345
343
 
346
344
  def set_last_command(cmd)
@@ -361,7 +359,7 @@ end
361
359
 
362
360
  def repeat_last_find()
363
361
  return if !defined? $last_find_command
364
- $buffer.jump_to_next_instance_of_char($last_find_command[:char],
362
+ vma.buf.jump_to_next_instance_of_char($last_find_command[:char],
365
363
  $last_find_command[:direction])
366
364
  end
367
365
 
@@ -375,7 +373,7 @@ def set_next_command_count(num)
375
373
  end
376
374
 
377
375
  def start_minibuffer_cmd(bufname, bufstr, cmd)
378
- $kbd.set_mode(:minibuffer)
376
+ vma.kbd.set_mode(:minibuffer)
379
377
  $minibuffer = Buffer.new(bufstr, "")
380
378
  $minibuffer.call_func = method(cmd)
381
379
  end
@@ -388,7 +386,8 @@ def show_key_bindings()
388
386
  kbd_s << "key!: Press key once, release before pressing any other keys\n"
389
387
 
390
388
  kbd_s << "===============================================\n"
391
- kbd_s << $kbd.to_s
389
+ kbd_s << vma.kbd.to_s
390
+ kbd_s << "\n"
392
391
  kbd_s << "===============================================\n"
393
392
  b = create_new_file(nil, kbd_s)
394
393
  gui_set_file_lang(b.id, "hyperplaintext")
@@ -397,10 +396,10 @@ end
397
396
 
398
397
  def diff_buffer()
399
398
  bufstr = ""
400
- orig_path = $buffer.fname
399
+ orig_path = vma.buf.fname
401
400
  infile = Tempfile.new("out")
402
401
  infile = Tempfile.new("in")
403
- infile.write($buffer.to_s)
402
+ infile.write(vma.buf.to_s)
404
403
  infile.flush
405
404
  cmd = "diff -w '#{orig_path}' #{infile.path}"
406
405
  # debug cmd
@@ -446,7 +445,7 @@ def minibuffer_new_char(c)
446
445
  $minibuffer.insert_txt(c)
447
446
  debug "MINIBUFFER: #{c}"
448
447
  end
449
- #$buffer = $minibuffer
448
+ #vma.buf = $minibuffer
450
449
  end
451
450
 
452
451
  # def readchar_new_char(c)
@@ -496,10 +495,27 @@ GUESS_ENCODING_ORDER = [
496
495
  ]
497
496
 
498
497
  def create_new_file(filename = nil, file_contents = "\n")
499
- debug "NEW FILE CREATED"
500
498
  buffer = Buffer.new(file_contents)
501
- # gui_set_current_buffer(buffer.id) #TODO: remove?
502
- $buffers << buffer
499
+
500
+ debug "NEW FILE CREATED: #{buffer.id}"
501
+ vma.buffers.add(buffer)
502
+ vma.kbd.set_mode_to_default
503
+ vma.buffers.set_current_buffer_by_id(buffer.id)
504
+
505
+ # Do set_content twice (once in Buffer.new) to force redraw and work around a bug
506
+ # The bug: if switching a child of scrolledWindow to a textview with a file smaller than the window, it won't get drawn properly if in previous (larger) file the ScrolledWindow was scrolled down.
507
+ buffer.set_content(file_contents)
508
+
509
+ return buffer
510
+ end
511
+
512
+ def create_new_buffer(file_contents = "\n",prefix="buf")
513
+ debug "NEW BUFFER CREATED"
514
+ buffer = Buffer.new(file_contents,nil,prefix)
515
+ vma.buffers.add(buffer)
516
+ vma.buffers.set_current_buffer_by_id(buffer.id)
517
+ buffer.set_content(file_contents)
518
+
503
519
  return buffer
504
520
  end
505
521
 
@@ -516,12 +532,13 @@ def filter_buffer(buf)
516
532
  end
517
533
 
518
534
  def load_buffer(fname)
519
- return if !File.exist?(fname)
520
- existing_buffer = $buffers.get_buffer_by_filename(fname)
535
+ # If file already open in existing buffer
536
+ existing_buffer = vma.buffers.get_buffer_by_filename(fname)
521
537
  if existing_buffer != nil
522
- $buffer_history << existing_buffer
538
+ vma.buffers.add_buf_to_history(existing_buffer)
523
539
  return
524
540
  end
541
+ return if !File.exist?(fname)
525
542
  debug("LOAD BUFFER: #{fname}")
526
543
  buffer = Buffer.new(read_file("", fname), fname)
527
544
  # gui_set_current_buffer(buffer.id)
@@ -529,8 +546,9 @@ def load_buffer(fname)
529
546
  debug("DONE LOAD: #{fname}")
530
547
  #buf = filter_buffer(buffer)
531
548
  # debug("END FILTER: #{fname}")
532
- $buffers << buffer
533
- #$buffer_history << $buffers.size - 1
549
+ vma.buffers << buffer
550
+ #$buffer_history << vma.buffers.size - 1
551
+ return buffer
534
552
  end
535
553
 
536
554
  def jump_to_file(filename, linenum = nil, charn = nil)
@@ -561,15 +579,16 @@ end
561
579
  def open_new_file(filename, file_contents = "")
562
580
  #TODO: expand path
563
581
  filename = File.expand_path(filename)
564
- b = $buffers.get_buffer_by_filename(filename)
582
+ b = vma.buffers.get_buffer_by_filename(filename)
565
583
  # File is already opened to existing buffer
566
584
  if b != nil
567
585
  message "Switching to: #{filename}"
568
- $buffers.set_current_buffer(b)
586
+ vma.buffers.set_current_buffer(b)
569
587
  else
570
588
  message "New file opened: #{filename}"
571
589
  fname = filename
572
- load_buffer(fname)
590
+ bf = load_buffer(fname)
591
+ vma.buffers.set_current_buffer_by_id(bf.id)
573
592
  end
574
593
  end
575
594
 
@@ -680,8 +699,8 @@ end
680
699
  def find_project_dir_of_cur_buffer()
681
700
  # Find "project dir" of current file. If currently editing file in path "/foo/bar/baz/fn.txt" and file named "/foo/bar/.vma_project" exists, then dir /foo/bar is treated as project dir and subject to e.g. ack search.
682
701
  pdir = nil
683
- if $buffer.fname
684
- pdir = find_project_dir_of_fn($buffer.fname)
702
+ if vma.buf.fname
703
+ pdir = find_project_dir_of_fn(vma.buf.fname)
685
704
  end
686
705
  # debug "Proj dir of current file: #{pdir}"
687
706
  return pdir
@@ -33,7 +33,7 @@ class Encrypt
33
33
  end
34
34
 
35
35
  def decrypt_cur_buffer(password, b = nil)
36
- $buffer.decrypt(password)
36
+ vma.buf.decrypt(password)
37
37
  end
38
38
 
39
39
  def encrypt_cur_buffer()
@@ -42,6 +42,6 @@ def encrypt_cur_buffer()
42
42
  end
43
43
 
44
44
  def encrypt_cur_buffer_callback(password,b=nil)
45
- $buffer.set_encrypted(password)
45
+ vma.buf.set_encrypted(password)
46
46
  end
47
47
 
@@ -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}"
@@ -1,4 +1,3 @@
1
-
2
1
  class FileManager
3
2
  @@cur
4
3
 
@@ -28,14 +27,14 @@ class FileManager
28
27
 
29
28
  # bindkey "C o", :delete_state
30
29
 
31
- $kbd.add_minor_mode("fexp", :file_exp, :command)
30
+ vma.kbd.add_minor_mode("fexp", :file_exp, :command)
32
31
 
33
32
  bindkey "fexp o m", :fexp_sort_mtime
34
33
  bindkey "fexp o f", :fexp_sort_fname
35
34
 
36
35
  # bindkey "fexp l", [:fexp_right, proc { debug "==fexp_right==" }, ""]
37
36
  bindkey "fexp h", :fexp_chdir_parent
38
- bindkey "fexp esc", [:fexp_quit, proc { $kbd.set_mode(:command) }, ""]
37
+ bindkey "fexp esc", [:fexp_quit, proc { FileManager.cur.quit }, ""]
39
38
  bindkey "fexp enter", :fexp_select
40
39
  bindkey "fexp l", :fexp_select
41
40
 
@@ -66,6 +65,8 @@ class FileManager
66
65
 
67
66
  def dir_to_buf(dirpath, b = nil)
68
67
  # File.stat("testfile").mtime
68
+
69
+ vma.buffers.last_dir = dirpath
69
70
  dirpath = File.expand_path(dirpath)
70
71
  @header = []
71
72
  @header << "#{dirpath}"
@@ -92,9 +93,7 @@ class FileManager
92
93
  end
93
94
  end
94
95
 
95
- # sort_by = :mtime
96
- # sort_by = :name
97
- # Ripl.start :binding => binding
96
+
98
97
  @cfiles.sort_by! { |x| x[1].mtime }.reverse! if @sort_by == :mtime
99
98
  @cfiles.sort_by! { |x| x[1].size }.reverse! if @sort_by == :size
100
99
  @cfiles.sort_by! { |x| x[0] } if @sort_by == :name
@@ -112,7 +111,7 @@ class FileManager
112
111
  end
113
112
 
114
113
  if @buf.nil?
115
- @buf = create_new_file(nil, s)
114
+ @buf = create_new_buffer(s,"filemgr")
116
115
  @buf.module = self
117
116
  @buf.active_kbd_mode = :file_exp
118
117
  else
@@ -140,6 +139,9 @@ class FileManager
140
139
  else
141
140
  open_with_default_program(fn)
142
141
  end
143
- # debug l.inspect
142
+ end
143
+
144
+ def quit
145
+ @buf.close
144
146
  end
145
147
  end