vimamsa 0.1.12 → 0.1.14

Sign up to get free protection for your applications and to get access to all the features.
data/lib/vimamsa/conf.rb CHANGED
@@ -13,18 +13,143 @@ def setcnf(id, val)
13
13
  end
14
14
 
15
15
  setcnf :custom_lsp, {}
16
- conf(:custom_lsp)[:ruby] = {name: "solargraph", command:"solargraph stdio", type: "stdio"}
17
- conf(:custom_lsp)[:cpp] = {name: "clangd", command:"clangd-12 --offset-encoding=utf-8", type: "stdio"}
18
- conf(:custom_lsp)[:python] = {name: "pyright", command:"pyright-langserver --stdio --verbose", type: "stdio"}
19
16
 
20
17
  setcnf :indent_based_on_last_line, true
21
18
  setcnf :extensions_to_open, [".txt", ".h", ".c", ".cpp", ".hpp", ".rb", ".inc", ".php", ".sh", ".m", ".gd", ".js", ".py"]
22
19
  setcnf :default_search_extensions, ["txt", "rb"]
23
20
 
24
-
25
21
  setcnf "log.verbose", 1
26
- setcnf :tab_width, 4
27
22
  setcnf :enable_lsp, false
28
23
 
24
+ setcnf :tab_width, 2
25
+ setcnf :tab_to_spaces_default, false
26
+ setcnf :tab_to_spaces_languages, ["c", "java", "ruby", "hyperplaintext", "php"]
27
+ setcnf :tab_to_spaces_not_languages, ["makefile"]
28
+
29
29
  setcnf :workspace_folders, []
30
30
 
31
+
32
+ # New way to configure:
33
+ # To set conf value:
34
+ # cnf.foo.bar.baz = 3
35
+
36
+ #To get conf value:
37
+ # cnf.foo.bar.baz?
38
+ # cnf.foo.bar.baz!
39
+ # get(cnf.foo.bar.baz)
40
+ # (All get the same result)
41
+
42
+ class ConfId
43
+ def initialize(first)
44
+ @id = [first]
45
+ end
46
+
47
+ def method_missing(method_name, *args)
48
+ # pp "asize:#{args.size}"
49
+ if m = method_name.match(/(.*)=$/)
50
+ @id << m[1].to_sym
51
+ # pp [@id, args[0]]
52
+ set(self, args[0])
53
+ return args[0]
54
+ elsif m = method_name.match(/(.*)[\!\?]$/)
55
+ @id << m[1].to_sym
56
+ r = get(self)
57
+
58
+ if r.class == Hash and r.empty?
59
+ # The accessed key was not defined
60
+ return nil
61
+ else
62
+ return r
63
+ end
64
+ else
65
+ @id << method_name
66
+ end
67
+
68
+ return self
69
+ end
70
+
71
+ def to_s
72
+ @id.join(".")
73
+ end
74
+
75
+ def to_a
76
+ return @id
77
+ end
78
+ end
79
+
80
+ class Conf
81
+ attr_reader :confh
82
+
83
+ def initialize()
84
+ @id = []
85
+ @confh = Hash.new { |h, k| h[k] = Hash.new(&h.default_proc) }
86
+ end
87
+
88
+ def method_missing(method_name, *args)
89
+ c = ConfId.new(method_name)
90
+
91
+ #TODO: improve
92
+ if m = method_name.match(/(.*)[\!\?]$/)
93
+ c = ConfId.new(m[1])
94
+ return get(c)
95
+ end
96
+
97
+ if m = method_name.match(/(.*)=$/)
98
+ c = ConfId.new(m[1])
99
+ set(c, args[0])
100
+ return args[0]
101
+ end
102
+
103
+ return c
104
+ end
105
+ end
106
+
107
+
108
+
109
+ $confh = Hash.new { |h, k| h[k] = Hash.new(&h.default_proc) }
110
+ # set cnf.foo.bar.baz, 3
111
+ # => $confh = {:foo=>{:bar=>{:baz=>3}}}
112
+ def set(_id, val)
113
+ a = $confh
114
+ id = _id.to_a
115
+ last = id.pop
116
+ for x in id
117
+ a = a[x]
118
+ end
119
+ a[last] = val
120
+ end
121
+
122
+ def get(id)
123
+ id = id.to_a
124
+ a = $confh
125
+ for x in id
126
+ return nil if a[x].nil?
127
+ return nil if a.empty?
128
+ a = a[x]
129
+ end
130
+ return a
131
+ end
132
+
133
+ $vimamsa_conf = Conf.new
134
+
135
+ def cnf()
136
+ return $vimamsa_conf
137
+ end
138
+
139
+ cnf.indent_based_on_last_line = true
140
+ cnf.extensions_to_open = [".txt", ".h", ".c", ".cpp", ".hpp", ".rb", ".inc", ".php", ".sh", ".m", ".gd", ".js", ".py"]
141
+ cnf.default_search_extensions = ["txt", "rb"]
142
+
143
+ cnf.log.verbose = 1
144
+ cnf.lsp.enabled = false
145
+ cnf.fexp.experimental = false
146
+ cnf.experimental = false
147
+
148
+ cnf.tab.width = 2
149
+ cnf.tab.to_spaces_default = false
150
+ cnf.tab.to_spaces_languages = ["c", "java", "ruby", "hyperplaintext", "php"]
151
+ cnf.tab.to_spaces_not_languages = ["makefile"]
152
+ cnf.workspace_folders = []
153
+
154
+ cnf.match.highlight.color = "#10bd8e"
155
+
data/lib/vimamsa/debug.rb CHANGED
@@ -1,7 +1,7 @@
1
1
  require "fileutils"
2
2
 
3
3
  def debug(message, severity = 1)
4
- if $debug
4
+ if cnf.debug?
5
5
  if severity > 1
6
6
  # Add red colour and bold for attention
7
7
  # https://en.wikipedia.org/wiki/ANSI_escape_code
@@ -18,7 +18,7 @@ def debug_print_buffer(c)
18
18
  end
19
19
 
20
20
  def debug_dump_clipboard()
21
- puts $clipboard.inspect
21
+ puts vma.clipboard.inspect
22
22
  end
23
23
 
24
24
  def debug_dump_deltas()
@@ -59,8 +59,8 @@ def savedebug(message, e)
59
59
  dbginfo["trace_str"] = dbginfo["trace"].join("\n")
60
60
  dbginfo["edit_history"] = buf.edit_history
61
61
  dbginfo["cnf"] = $cnf
62
- dbginfo["register"] = $register
63
- dbginfo["clipboard"] = $clipboard
62
+ dbginfo["register"] = vma.register
63
+ dbginfo["clipboard"] = vma.clipboard
64
64
  # dbginfo["last_event"] = $last_event
65
65
  dbginfo["buffer"] = {}
66
66
  dbginfo["buffer"]["str"] = buf.to_s
@@ -79,10 +79,6 @@ def savedebug(message, e)
79
79
  puts save_fn_json
80
80
  end
81
81
 
82
- def run_tests()
83
- run_test("01")
84
- run_test("02")
85
- end
86
82
 
87
83
  def run_test(test_id)
88
84
  target_results = read_file("", "tests/test_#{test_id}_output.txt")
@@ -1,39 +1,13 @@
1
1
  require "pty"
2
2
 
3
- def exec_in_terminal(cmd, autoclose = false)
4
- # debug "CMD:#{cmd}"
5
-
6
- # global to prevent garbage collect unlink
7
- $initf = Tempfile.new("bashinit")
8
- # debug $initf.path
9
- $initf.write(cmd)
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
17
- $initf.close
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
21
- fork { exec "gnome-terminal", "--tab", "--", "bash", "-i", $initf.path, "-c", "exec bash" }
22
- end
23
-
24
3
  def handle_drag_and_drop(fname)
25
4
  debug "EDITOR:handle_drag_and_drop"
26
5
  buf.handle_drag_and_drop(fname)
27
6
  end
28
7
 
29
- def mkdir_if_not_exists(_dirpath)
30
- dirpath = File.expand_path(_dirpath)
31
- Dir.mkdir(dirpath) unless File.exist?(dirpath)
32
- end
33
-
34
8
  class Editor
35
9
  attr_reader :file_content_search_paths, :file_name_search_paths, :gui, :hook, :macro
36
- attr_accessor :converters, :fh, :paint_stack, :kbd, :langsrv
10
+ attr_accessor :converters, :fh, :paint_stack, :kbd, :langsrv, :register, :cur_register, :clipboard
37
11
  #attr_writer :call_func, :update_highlight
38
12
 
39
13
  def initialize()
@@ -49,6 +23,20 @@ class Editor
49
23
  @converters = {}
50
24
  @paint_stack = []
51
25
  @_plugins = {}
26
+ @errors
27
+ @register = Hash.new("")
28
+ @cur_register = "a"
29
+ @clipboard = Clipboard.new
30
+ end
31
+
32
+ def set_register(char)
33
+ @cur_register = char
34
+ message("Set register #{char}")
35
+ end
36
+
37
+ def paste_register(char)
38
+ $c = @cur_register #TODO:??
39
+ message("Paste: #{$c}")
52
40
  end
53
41
 
54
42
  def open_file_listener(added)
@@ -76,8 +64,7 @@ class Editor
76
64
  @hook = $hook
77
65
  register_plugin(:Hook, @hook)
78
66
  @macro = Macro.new
79
- $macro = @macro
80
- register_plugin(:Macro, $macro)
67
+ register_plugin(:Macro, @macro)
81
68
  $search = Search.new
82
69
  register_plugin(:Search, $search)
83
70
 
@@ -93,11 +80,9 @@ class Editor
93
80
  $kbd = @kbd
94
81
  require "vimamsa/key_bindings_vimlike"
95
82
  sleep(0.03)
96
-
97
83
 
98
- FileManager.init
99
84
  BufferManager.init
100
-
85
+
101
86
  @gui.init_menu
102
87
 
103
88
  mkdir_if_not_exists("~/.vimamsa")
@@ -134,8 +119,12 @@ class Editor
134
119
  custom_script = read_file("", custom_fn)
135
120
  eval(custom_script) if custom_script
136
121
 
122
+ Grep.init
123
+ FileManager.init
124
+
137
125
  if conf(:enable_lsp)
138
126
  require "vimamsa/langservp"
127
+ require "vimamsa/audio" # TODO:config
139
128
  @langsrv["ruby"] = LangSrv.new("ruby")
140
129
  @langsrv["cpp"] = LangSrv.new("cpp")
141
130
  end
@@ -282,6 +271,11 @@ class Editor
282
271
  debug "CAN OPEN?: #{can_open}"
283
272
  return can_open
284
273
  end
274
+
275
+ def error(message)
276
+ debug "ERORR #{caller[0]} #{str}", 2
277
+ @errors << [message, caller]
278
+ end
285
279
  end
286
280
 
287
281
  def _quit()
@@ -304,35 +298,28 @@ def open_file_dialog()
304
298
  gui_open_file_dialog(File.dirname(path))
305
299
  end
306
300
 
307
- #TODO:delete?
308
- def system_clipboard_changed(clipboard_contents)
309
- max_clipboard_items = 100
310
- if clipboard_contents != $clipboard[-1]
311
- #TODO: HACK
312
- $paste_lines = false
301
+ class Clipboard
302
+ def initialize
303
+ @clipboard = []
313
304
  end
314
- $clipboard << clipboard_contents
315
- # debug $clipboard[-1]
316
- $clipboard = $clipboard[-([$clipboard.size, max_clipboard_items].min)..-1]
317
- end
318
305
 
319
- def get_clipboard()
320
- return $clipboard[-1]
321
- end
306
+ def set(s)
307
+ if !(s.class <= String) or s.size == 0
308
+ debug s.inspect
309
+ debug [s, s.class, s.size]
310
+ log_error("s.class != String or s.size == 0")
311
+ return
312
+ end
313
+ @clipboard << s
314
+ set_system_clipboard(s)
315
+ vma.register[vma.cur_register] = s
316
+ debug "SET CLIPBOARD: [#{s}]"
317
+ debug "REGISTER: #{vma.cur_register}:#{vma.register[vma.cur_register]}"
318
+ end
322
319
 
323
- def set_clipboard(s)
324
- if !(s.class <= String) or s.size == 0
325
- debug s.inspect
326
- debug [s, s.class, s.size]
327
- log_error("s.class != String or s.size == 0")
328
- # Ripl.start :binding => binding
329
- return
320
+ def get()
321
+ return @clipboard[-1]
330
322
  end
331
- $clipboard << s
332
- set_system_clipboard(s)
333
- $register[$cur_register] = s
334
- debug "SET CLIPBOARD: [#{s}]"
335
- debug "REGISTER: #{$cur_register}:#{$register[$cur_register]}"
336
323
  end
337
324
 
338
325
  def set_cursor_pos(new_pos)
@@ -389,7 +376,7 @@ def show_key_bindings()
389
376
  kbd_s << vma.kbd.to_s
390
377
  kbd_s << "\n"
391
378
  kbd_s << "===============================================\n"
392
- b = create_new_buffer(kbd_s,"key-bindings")
379
+ b = create_new_buffer(kbd_s, "key-bindings")
393
380
  gui_set_file_lang(b.id, "hyperplaintext")
394
381
  #
395
382
  end
@@ -456,6 +443,11 @@ def minibuffer_delete()
456
443
  $minibuffer.delete(BACKWARD_CHAR)
457
444
  end
458
445
 
446
+ def error(str)
447
+ puts caller[0]
448
+ debug "#{caller[0]} ERROR: #{str}", 2
449
+ end
450
+
459
451
  def message(s)
460
452
  s = "[#{DateTime.now().strftime("%H:%M")}] #{s}"
461
453
  debug s
@@ -502,16 +494,16 @@ def create_new_file(filename = nil, file_contents = "\n")
502
494
  vma.kbd.set_mode_to_default
503
495
  vma.buffers.set_current_buffer_by_id(buffer.id)
504
496
 
505
- # Do set_content twice (once in Buffer.new) to force redraw and work around a bug
497
+ # Do set_content twice (once in Buffer.new) to force redraw and work around a bug
506
498
  # 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
499
  buffer.set_content(file_contents)
508
500
 
509
501
  return buffer
510
502
  end
511
503
 
512
- def create_new_buffer(file_contents = "\n",prefix="buf", setcurrent=true)
504
+ def create_new_buffer(file_contents = "\n", prefix = "buf", setcurrent = true)
513
505
  debug "NEW BUFFER CREATED"
514
- buffer = Buffer.new(file_contents,nil,prefix)
506
+ buffer = Buffer.new(file_contents, nil, prefix)
515
507
  vma.buffers.add(buffer)
516
508
  vma.buffers.set_current_buffer_by_id(buffer.id) if setcurrent
517
509
  buffer.set_content(file_contents)
@@ -551,21 +543,21 @@ def load_buffer(fname)
551
543
  return buffer
552
544
  end
553
545
 
554
- def jump_to_file(filename, linenum = nil, charn = nil)
546
+ def jump_to_file(filename, tnum = nil, charn = nil)
555
547
  open_new_file(filename)
556
548
 
557
549
  # Link to character position
558
550
  if !charn.nil?
559
551
  if charn == "c"
560
- buf.jump_to_pos(linenum)
552
+ buf.jump_to_pos(tnum) # tnum=character position
561
553
  center_on_current_line
562
554
  return
563
555
  end
564
556
  end
565
557
 
566
558
  # Link to line
567
- if !linenum.nil?
568
- buf.jump_to_line(linenum)
559
+ if !tnum.nil?
560
+ buf.jump_to_line(tnum) # tnum=line position
569
561
  center_on_current_line
570
562
  return
571
563
  end
@@ -585,6 +577,10 @@ def open_new_file(filename, file_contents = "")
585
577
  message "Switching to: #{filename}"
586
578
  vma.buffers.set_current_buffer(b)
587
579
  else
580
+ if !is_path_writable(filename)
581
+ message("Path #{filename} cannot be written to")
582
+ return false
583
+ end
588
584
  message "New file opened: #{filename}"
589
585
  fname = filename
590
586
  bf = load_buffer(fname)
@@ -622,66 +618,6 @@ def get_file_line_pointer(s)
622
618
  return nil
623
619
  end
624
620
 
625
- def open_url(url)
626
- system("xdg-open", url)
627
- end
628
-
629
- def open_with_default_program(url)
630
- system("xdg-open", url)
631
- end
632
-
633
- def run_cmd(cmd)
634
- tmpf = Tempfile.new("vmarun", "/tmp").path
635
- cmd = "#{cmd} > #{tmpf}"
636
- debug "CMD:\n#{cmd}"
637
- system("bash", "-c", cmd)
638
- res_str = File.read(tmpf)
639
- return res_str
640
- end
641
-
642
- require "open3"
643
-
644
- def exec_cmd(bin_name, arg1 = nil, arg2 = nil, arg3 = nil, arg4 = nil, arg5 = nil)
645
- assert_binary_exists(bin_name)
646
- if !arg5.nil?
647
- p = Open3.popen2(bin_name, arg1, arg2, arg3, arg4, arg5)
648
- elsif !arg4.nil?
649
- p = Open3.popen2(bin_name, arg1, arg2, arg3, arg4)
650
- elsif !arg3.nil?
651
- p = Open3.popen2(bin_name, arg1, arg2, arg3)
652
- elsif !arg2.nil?
653
- p = Open3.popen2(bin_name, arg1, arg2)
654
- elsif !arg1.nil?
655
- p = Open3.popen2(bin_name, arg1)
656
- else
657
- p = Open3.popen2(bin_name)
658
- end
659
-
660
- ret_str = p[1].read
661
- return ret_str
662
- end
663
-
664
- def file_is_text_file(fpath)
665
- debug "file_is_text_file(#{fpath})"
666
- fpath = File.expand_path(fpath)
667
- return false if !File.exist?(fpath)
668
- r = exec_cmd("file", fpath)
669
- debug "DEBUG:#{r}"
670
- return true if r.match(/UTF-8.*text/)
671
- return true if r.match(/ASCII.*text/)
672
- return false
673
- end
674
-
675
- def set_register(char)
676
- $cur_register = char
677
- message("Set register #{char}")
678
- end
679
-
680
- def paste_register(char)
681
- $c = $register[char]
682
- message("Paste: #{$c}")
683
- end
684
-
685
621
  def find_project_dir_of_fn(fn)
686
622
  pcomp = Pathname.new(fn).each_filename.to_a
687
623
  parent_dirs = (0..(pcomp.size - 2)).collect { |x| "/" + pcomp[0..x].join("/") }.reverse
@@ -1,4 +1,3 @@
1
-
2
1
  require "openssl"
3
2
 
4
3
  class Encrypt
@@ -13,16 +12,16 @@ class Encrypt
13
12
  end
14
13
 
15
14
  def encrypt(text)
16
- cipher=@enc
15
+ cipher = @enc
17
16
  encrypted = cipher.update text
18
17
  encrypted << cipher.final
19
- encrypted = encrypted.unpack('H*')[0].upcase
18
+ encrypted = encrypted.unpack("H*")[0].upcase
20
19
  @enc.reset
21
20
  return encrypted
22
21
  end
23
22
 
24
23
  def decrypt(encrypted)
25
- cipher=@dec
24
+ cipher = @dec
26
25
  encrypted = [encrypted].pack("H*").unpack("C*").pack("c*")
27
26
  plain = cipher.update encrypted
28
27
  plain << cipher.final
@@ -32,16 +31,12 @@ class Encrypt
32
31
  end
33
32
  end
34
33
 
35
- def decrypt_cur_buffer(password, b = nil)
36
- vma.buf.decrypt(password)
37
- end
38
34
 
39
35
  def encrypt_cur_buffer()
40
- callback = proc{|x|encrypt_cur_buffer_callback(x)}
41
- gui_one_input_action("Encrypt", "Password:", "Encrypt", callback,{:hide=>true})
36
+ callback = proc { |x| encrypt_cur_buffer_callback(x) }
37
+ gui_one_input_action("Encrypt", "Password:", "Encrypt", callback, { :hide => true })
42
38
  end
43
39
 
44
- def encrypt_cur_buffer_callback(password,b=nil)
40
+ def encrypt_cur_buffer_callback(password, b = nil)
45
41
  vma.buf.set_encrypted(password)
46
42
  end
47
-