vimamsa 0.1.10 → 0.1.11

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,3 +1,4 @@
1
+
1
2
  def save_buffer_list()
2
3
  message("Save buffer list")
3
4
  buffn = get_dot_path("buffers.txt")
@@ -20,11 +21,13 @@ def load_buffer_list()
20
21
  end
21
22
 
22
23
  class BufferList < Array
23
- attr_reader :current_buf, :last_dir
24
+ attr_reader :current_buf, :last_dir, :buffer_history
24
25
 
25
26
  def initialize()
26
27
  @last_dir = File.expand_path(".")
28
+ @buffer_history = []
27
29
  super
30
+ @current_buf=0
28
31
  end
29
32
 
30
33
  # lastdir = File.expand_path(".") if lastdir.nil?
@@ -32,11 +35,17 @@ class BufferList < Array
32
35
  super
33
36
  vma.buf = _buf
34
37
  @current_buf = self.size - 1
35
- $buffer_history << @current_buf
38
+ @buffer_history << @current_buf
36
39
  @recent_ind = 0
37
40
  $hook.call(:change_buffer, vma.buf)
38
- gui_set_current_buffer(vma.buf.id)
41
+ vma.gui.set_current_buffer(vma.buf.id)
39
42
  gui_set_cursor_pos(vma.buf.id, vma.buf.pos)
43
+ update_last_dir(_buf)
44
+ end
45
+
46
+ def add(_buf)
47
+ self.append(_buf)
48
+ @buffer_history << self.size - 1
40
49
  end
41
50
 
42
51
  def switch()
@@ -48,10 +57,15 @@ class BufferList < Array
48
57
  set_current_buffer(@current_buf)
49
58
  end
50
59
 
60
+ def get_last_visited_id
61
+ last_buf = @buffer_history[-2]
62
+ return self[last_buf].id
63
+ end
64
+
51
65
  def switch_to_last_buf()
52
66
  debug "SWITCH TO LAST BUF:"
53
- debug $buffer_history
54
- last_buf = $buffer_history[-2]
67
+ debug @buffer_history
68
+ last_buf = @buffer_history[-2]
55
69
  if last_buf
56
70
  set_current_buffer(last_buf)
57
71
  end
@@ -68,12 +82,32 @@ class BufferList < Array
68
82
  return buf_idx
69
83
  end
70
84
 
85
+ def add_buf_to_history(buf_idx)
86
+ if self.include?(buf_idx)
87
+ @buffer_history << @buf_idx
88
+ @recent_ind = 0
89
+ compact_buf_history()
90
+ else
91
+ debug "buffer_list, no such id:#{buf_idx}"
92
+ return
93
+ end
94
+ end
95
+
71
96
  def add_current_buf_to_history()
72
97
  @recent_ind = 0
73
- $buffer_history << @current_buf
98
+ @buffer_history << @current_buf
74
99
  compact_buf_history()
75
100
  end
76
101
 
102
+ def set_current_buffer_by_id(buf_id, update_history = true)
103
+ idx = get_buffer_by_id(buf_id)
104
+ if idx.nil?
105
+ debug "IDX=nil"
106
+ return
107
+ end
108
+ set_current_buffer(idx, update_history)
109
+ end
110
+
77
111
  def set_current_buffer(buffer_i, update_history = true)
78
112
  buffer_i = self.size -1 if buffer_i > self.size
79
113
  buffer_i = 0 if buffer_i < 0
@@ -90,10 +124,11 @@ class BufferList < Array
90
124
  add_current_buf_to_history
91
125
  end
92
126
 
93
- $hook.call(:change_buffer, vma.buf)
127
+ vma.hook.call(:change_buffer, vma.buf)
94
128
  vma.buf.set_active
95
129
 
96
- gui_set_current_buffer(vma.buf.id)
130
+ vma.gui.set_current_buffer(vma.buf.id)
131
+
97
132
  gui_set_window_title(vma.buf.title, vma.buf.subtitle)
98
133
 
99
134
  if vma.buf.fname
@@ -103,6 +138,12 @@ class BufferList < Array
103
138
  # hpt_scan_images() if $debug # experimental
104
139
  end
105
140
 
141
+ def update_last_dir(buf)
142
+ if buf.fname
143
+ @last_dir = File.dirname(buf.fname)
144
+ end
145
+ end
146
+
106
147
  def last_dir=(d)
107
148
  @last_dir = d
108
149
  end
@@ -113,7 +154,7 @@ class BufferList < Array
113
154
 
114
155
  def get_recent_buffers()
115
156
  bufs = []; b = {}
116
- $buffer_history.reverse.each { |x| bufs << x if !b[x] && x < self.size; b[x] = true }
157
+ @buffer_history.reverse.each { |x| bufs << x if !b[x] && x < self.size; b[x] = true }
117
158
  return bufs
118
159
  end
119
160
 
@@ -138,8 +179,8 @@ class BufferList < Array
138
179
  def compact_buf_history()
139
180
  h = {}
140
181
  # Keep only first occurence in history
141
- bh = $buffer_history.reverse.select { |x| r = h[x] == nil; h[x] = true; r }
142
- $buffer_history = bh.reverse
182
+ bh = @buffer_history.reverse.select { |x| r = h[x] == nil; h[x] = true; r }
183
+ @buffer_history = bh.reverse
143
184
  end
144
185
 
145
186
  # Close buffer in the background
@@ -152,7 +193,7 @@ class BufferList < Array
152
193
  message("Closed buffer #{bufname}")
153
194
 
154
195
  self.slice!(buffer_i)
155
- $buffer_history = $buffer_history.collect { |x| r = x; r = x - 1 if x > buffer_i; r = nil if x == buffer_i; r }.compact
196
+ @buffer_history = @buffer_history.collect { |x| r = x; r = x - 1 if x > buffer_i; r = nil if x == buffer_i; r }.compact
156
197
  end
157
198
 
158
199
  def close_buffer(buffer_i, from_recent = false)
@@ -166,17 +207,17 @@ class BufferList < Array
166
207
  jump_to_buf = 0 if jump_to_buf == nil
167
208
 
168
209
  self.slice!(buffer_i)
169
- $buffer_history = $buffer_history.collect { |x| r = x; r = x - 1 if x > buffer_i; r = nil if x == buffer_i; r }.compact
210
+ @buffer_history = @buffer_history.collect { |x| r = x; r = x - 1 if x > buffer_i; r = nil if x == buffer_i; r }.compact
170
211
 
171
212
  if @current_buf == buffer_i
172
213
  if from_recent
173
214
  @current_buf = jump_to_buf
174
215
  else
175
- @current_buf = $buffer_history.last
216
+ # Find last edited buffer that is not already open
217
+ @current_buf = @buffer_history.filter{|x| !vma.gui.is_buffer_open(self[x].id)}.last
176
218
  end
177
219
  end
178
- # Ripl.start :binding => binding
179
- if self.size == 0
220
+ if self.size == 0 or @current_buf.nil?
180
221
  self << Buffer.new("\n")
181
222
  @current_buf = 0
182
223
  else
@@ -43,8 +43,11 @@ class BufferManager
43
43
  buf_i = buf_of_current_line()
44
44
  return if buf_i.nil?
45
45
 
46
- vma.buffers.close_current_buffer()
46
+ # vma.buffers.close_current_buffer() #TODO:??
47
47
  vma.buffers.set_current_buffer(buf_i)
48
+
49
+ bid = vma.buffers.get_buffer_by_id(@buf.id)
50
+ vma.buffers.close_other_buffer(bid)
48
51
  @@cur = nil
49
52
  end
50
53
 
@@ -64,20 +67,33 @@ class BufferManager
64
67
  s << @header.join("\n")
65
68
  s << "\n"
66
69
  i = 0
70
+ jump_to_line = 0
67
71
  for b in vma.buffers.sort_by { |x| x.list_str }
72
+ if b.id == vma.buf.id # current file
73
+ # s << " "
74
+ jump_to_line = i
75
+ end
68
76
  x = b.list_str
69
77
  s << "#{x}\n"
70
78
  @line_to_id[i] = b.id
71
79
  i += 1
72
80
  end
73
81
 
82
+
83
+
74
84
  if @buf.nil?
75
- @buf = create_new_file(nil, s)
85
+ @buf = create_new_buffer(s,"bufmgr")
76
86
  @buf.module = self
77
87
  @buf.active_kbd_mode = :buf_mgr
78
88
  else
79
89
  @buf.set_content(s)
80
90
  end
81
- @buf.set_line_and_column_pos(@header.size, 0)
91
+ # Position on the line of the active buffer
92
+ # @buf.set_content(s)
93
+ newlpos = @header.size + jump_to_line
94
+
95
+ @buf.set_line_and_column_pos(newlpos, 0)
96
+
97
+ # Thread.new{sleep 0.1; center_on_current_line()} # TODO
82
98
  end
83
99
  end
data/lib/vimamsa/conf.rb CHANGED
@@ -12,10 +12,19 @@ def setcnf(id, val)
12
12
  set_conf(id, val)
13
13
  end
14
14
 
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
+
15
20
  setcnf :indent_based_on_last_line, true
16
- setcnf :extensions_to_open, [".txt", ".h", ".c", ".cpp", ".hpp", ".rb", ".inc", ".php", ".sh", ".m", ".gd", ".js"]
21
+ setcnf :extensions_to_open, [".txt", ".h", ".c", ".cpp", ".hpp", ".rb", ".inc", ".php", ".sh", ".m", ".gd", ".js", ".py"]
22
+ setcnf :default_search_extensions, ["txt", "rb"]
17
23
 
18
24
 
19
25
  setcnf "log.verbose", 1
20
26
  setcnf :tab_width, 4
27
+ setcnf :enable_lsp, false
28
+
29
+ setcnf :workspace_folders, []
21
30
 
data/lib/vimamsa/debug.rb CHANGED
@@ -1,7 +1,12 @@
1
1
  require "fileutils"
2
2
 
3
- def debug(message)
3
+ def debug(message, severity = 1)
4
4
  if $debug
5
+ if severity > 1
6
+ # Add red colour and bold for attention
7
+ # https://en.wikipedia.org/wiki/ANSI_escape_code
8
+ message = "\033[1;31m!!! \033[0m#{message}"
9
+ end
5
10
  puts "[#{DateTime.now().strftime("%H:%M:%S")}] #{message}"
6
11
  $stdout.flush
7
12
  end
@@ -22,7 +27,7 @@ end
22
27
 
23
28
  $log_messages = []
24
29
 
25
- def log_message(message,vlevel=1)
30
+ def log_message(message, vlevel = 1)
26
31
  puts message if conf("log.verbose") >= vlevel
27
32
  $log_messages << message
28
33
  end
@@ -137,7 +142,6 @@ def run_random_jump_test__tmpl(test_time = 60 * 60 * 10)
137
142
  end
138
143
  end
139
144
 
140
-
141
145
  def start_ripl
142
146
  Ripl.start :binding => binding
143
147
  end
@@ -22,18 +22,20 @@ class EasyJump
22
22
  r = false if lsh[x] or lsh[x - 1] or lsh[x - 2]
23
23
  r
24
24
  }
25
+
26
+ wsmarks = [0] if wsmarks.empty?
25
27
 
26
28
  # Exclude those word start positions that are too close to each other
27
29
  wsmarks.sort!
28
30
  wsm2 = [wsmarks[0]]
29
31
  for i in 1..(wsmarks.size - 1)
30
32
  if (wsmarks[i] - wsm2[-1]) >= 4 or visible_text[wsm2[-1]..wsmarks[i]].include?("\n")
31
-
32
33
  wsm2 << wsmarks[i]
33
34
  end
34
35
  end
35
36
  wsmarks = wsm2
36
37
 
38
+
37
39
  linestart_buf = (line_starts).collect { |x| x + visible_range[0] }
38
40
  wsmarks_buf = (wsmarks).collect { |x| x + visible_range[0] }
39
41
 
@@ -57,9 +59,11 @@ class EasyJump
57
59
  if @jump_sequence.include?(@easy_jump_input)
58
60
  jshash = Hash[@jump_sequence.map.with_index.to_a]
59
61
  nthword = jshash[@easy_jump_input]
60
- debug "nthword:#{nthword} #{[@easy_jump_wsmarks[nthword], @jump_sequence[nthword]]}"
61
- buf.set_pos(@easy_jump_wsmarks[nthword])
62
- # @kbd.set_mode(:command)
62
+ newpos = @easy_jump_wsmarks[nthword]
63
+ if !newpos.nil?
64
+ debug "nthword:#{nthword} #{[@easy_jump_wsmarks[nthword], @jump_sequence[nthword]]}"
65
+ buf.set_pos(@easy_jump_wsmarks[nthword])
66
+ end
63
67
  vma.kbd.remove_keyhandling_override
64
68
  @jump_sequence = []
65
69
  vma.gui.clear_overlay()
@@ -76,7 +80,7 @@ class EasyJump
76
80
  def easy_jump_draw()
77
81
  vma.gui.start_overlay_draw
78
82
  for i in 0..(@easy_jump_wsmarks.size - 1)
79
- vma.gui.overlay_draw_text(@jump_sequence[i], @easy_jump_wsmarks[i])
83
+ vma.gui.overlay_draw_text(@jump_sequence[i], @easy_jump_wsmarks[i])
80
84
  end
81
85
  vma.gui.end_overlay_draw
82
86
  end
@@ -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
- vma.buffers << buffer
169
-
170
- # load_theme($cnf[:theme])
171
-
172
- # render_buffer(vma.buf, 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)
@@ -197,11 +189,11 @@ class Editor
197
189
  def buf()
198
190
  return $buffer
199
191
  end
192
+
200
193
  def buf=(aa)
201
- $buffer=aa
194
+ $buffer = aa
202
195
  end
203
196
 
204
-
205
197
  def buffers()
206
198
  return $buffers
207
199
  end
@@ -243,8 +235,9 @@ class Editor
243
235
  end
244
236
 
245
237
  def shutdown()
246
- $hook.call(:shutdown)
238
+ @hook.call(:shutdown)
247
239
  save_state
240
+ @gui.quit
248
241
  end
249
242
 
250
243
  def save_state
@@ -272,7 +265,7 @@ class Editor
272
265
  r = @file_content_search_paths.clone
273
266
  p = find_project_dir_of_cur_buffer()
274
267
  if p.nil?
275
- p = vma.buffers.last_dir
268
+ p = vma.buffers.last_dir
276
269
  end
277
270
 
278
271
  if p and !@file_content_search_paths.include?(p)
@@ -293,7 +286,7 @@ end
293
286
 
294
287
  def _quit()
295
288
  vma.shutdown
296
- Gtk.main_quit
289
+ # Gtk.main_quit
297
290
  end
298
291
 
299
292
  def fatal_error(msg)
@@ -311,6 +304,7 @@ def open_file_dialog()
311
304
  gui_open_file_dialog(File.dirname(path))
312
305
  end
313
306
 
307
+ #TODO:delete?
314
308
  def system_clipboard_changed(clipboard_contents)
315
309
  max_clipboard_items = 100
316
310
  if clipboard_contents != $clipboard[-1]
@@ -379,7 +373,7 @@ def set_next_command_count(num)
379
373
  end
380
374
 
381
375
  def start_minibuffer_cmd(bufname, bufstr, cmd)
382
- $kbd.set_mode(:minibuffer)
376
+ vma.kbd.set_mode(:minibuffer)
383
377
  $minibuffer = Buffer.new(bufstr, "")
384
378
  $minibuffer.call_func = method(cmd)
385
379
  end
@@ -392,7 +386,8 @@ def show_key_bindings()
392
386
  kbd_s << "key!: Press key once, release before pressing any other keys\n"
393
387
 
394
388
  kbd_s << "===============================================\n"
395
- kbd_s << $kbd.to_s
389
+ kbd_s << vma.kbd.to_s
390
+ kbd_s << "\n"
396
391
  kbd_s << "===============================================\n"
397
392
  b = create_new_file(nil, kbd_s)
398
393
  gui_set_file_lang(b.id, "hyperplaintext")
@@ -500,11 +495,27 @@ GUESS_ENCODING_ORDER = [
500
495
  ]
501
496
 
502
497
  def create_new_file(filename = nil, file_contents = "\n")
503
- debug "NEW FILE CREATED"
504
498
  buffer = Buffer.new(file_contents)
505
- # gui_set_current_buffer(buffer.id) #TODO: remove?
506
- vma.buffers << buffer
499
+
500
+ debug "NEW FILE CREATED: #{buffer.id}"
501
+ vma.buffers.add(buffer)
507
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
+
508
519
  return buffer
509
520
  end
510
521
 
@@ -521,12 +532,13 @@ def filter_buffer(buf)
521
532
  end
522
533
 
523
534
  def load_buffer(fname)
524
- return if !File.exist?(fname)
535
+ # If file already open in existing buffer
525
536
  existing_buffer = vma.buffers.get_buffer_by_filename(fname)
526
537
  if existing_buffer != nil
527
- $buffer_history << existing_buffer
538
+ vma.buffers.add_buf_to_history(existing_buffer)
528
539
  return
529
540
  end
541
+ return if !File.exist?(fname)
530
542
  debug("LOAD BUFFER: #{fname}")
531
543
  buffer = Buffer.new(read_file("", fname), fname)
532
544
  # gui_set_current_buffer(buffer.id)
@@ -536,6 +548,7 @@ def load_buffer(fname)
536
548
  # debug("END FILTER: #{fname}")
537
549
  vma.buffers << buffer
538
550
  #$buffer_history << vma.buffers.size - 1
551
+ return buffer
539
552
  end
540
553
 
541
554
  def jump_to_file(filename, linenum = nil, charn = nil)
@@ -574,7 +587,8 @@ def open_new_file(filename, file_contents = "")
574
587
  else
575
588
  message "New file opened: #{filename}"
576
589
  fname = filename
577
- load_buffer(fname)
590
+ bf = load_buffer(fname)
591
+ vma.buffers.set_current_buffer_by_id(bf.id)
578
592
  end
579
593
  end
580
594