vimamsa 0.1.14 → 0.1.15

Sign up to get free protection for your applications and to get access to all the features.
@@ -2,7 +2,7 @@ def save_buffer_list()
2
2
  message("Save buffer list")
3
3
  buffn = get_dot_path("buffers.txt")
4
4
  f = File.open(buffn, "w")
5
- bufstr = vma.buffers.collect { |buf| buf.fname }.inspect
5
+ bufstr = vma.buffers.list.collect { |buf| buf.fname }.inspect
6
6
  f.write(bufstr)
7
7
  f.close()
8
8
  end
@@ -19,74 +19,93 @@ def load_buffer_list()
19
19
  end
20
20
  end
21
21
 
22
- class BufferList < Array
22
+ class BufferList
23
23
  attr_reader :current_buf, :last_dir, :last_file, :buffer_history
24
+ attr_accessor :list
24
25
 
25
26
  def initialize()
26
27
  @last_dir = File.expand_path(".")
27
28
  @buffer_history = []
28
29
  super
29
30
  @current_buf = 0
31
+ @list = []
32
+ @h = {}
33
+ reset_navigation
30
34
  end
31
35
 
32
36
  # lastdir = File.expand_path(".") if lastdir.nil?
33
37
  def <<(_buf)
34
- super
35
38
  vma.buf = _buf
36
- @current_buf = self.size - 1
37
- @buffer_history << @current_buf
38
- @recent_ind = 0
39
+ self.add(_buf)
40
+
39
41
  $hook.call(:change_buffer, vma.buf)
40
- vma.gui.set_current_buffer(vma.buf.id)
41
- # gui_set_cursor_pos(vma.buf.id, vma.buf.pos)
42
- vma.buf.view.set_cursor_pos(vma.buf.pos)
42
+ vma.gui.set_current_buffer(vma.buf.id) #TODO: handle elswhere?
43
+ # vma.buf.view.set_cursor_pos(vma.buf.pos) #TODO: handle elswhere?
43
44
  update_last_dir(_buf)
44
45
  end
45
46
 
46
47
  def add(_buf)
47
- self.append(_buf)
48
- @buffer_history << self.size - 1
48
+ @buffer_history << _buf.id
49
+ # @navigation_idx = _buf.id #TODO:??
50
+ @list << _buf
51
+ @h[_buf.id] = _buf
49
52
  end
50
53
 
51
- def switch()
52
- debug "SWITCH BUF. bufsize:#{self.size}, curbuf: #{@current_buf}"
53
- @current_buf += 1
54
- @current_buf = 0 if @current_buf >= self.size
55
- m = method("switch")
56
- set_last_command({ method: m, params: [] })
57
- set_current_buffer(@current_buf)
54
+ #NOTE: unused. enable?
55
+ # def switch()
56
+ # debug "SWITCH BUF. bufsize:#{self.size}, curbuf: #{@current_buf}"
57
+ # @current_buf += 1
58
+ # @current_buf = 0 if @current_buf >= self.size
59
+ # m = method("switch")
60
+ # set_last_command({ method: m, params: [] })
61
+ # set_current_buffer(@current_buf)
62
+ # end
63
+
64
+ def slist
65
+ # TODO: implement using heap/priorityque
66
+ @list.sort_by! { |x| x.access_time }
58
67
  end
59
68
 
60
69
  def get_last_visited_id
61
- last_buf = @buffer_history[-2]
62
- return self[last_buf].id
70
+ last_buf = nil
71
+ for i in 0..(slist.size - 1)
72
+ next if slist[i].is_active?
73
+ last_buf = slist[i].id
74
+ end
75
+ return last_buf
63
76
  end
64
77
 
65
78
  def switch_to_last_buf()
66
79
  debug "SWITCH TO LAST BUF:"
67
- debug @buffer_history
68
- last_buf = @buffer_history[-2]
69
- if last_buf
70
- set_current_buffer(last_buf)
80
+ # debug @buffer_history
81
+ # last_buf = @buffer_history[-2]
82
+
83
+ last_buf = slist[-2]
84
+ if !last_buf.nil?
85
+ set_current_buffer(last_buf.id)
71
86
  end
72
87
  end
73
88
 
89
+ def size
90
+ return @list.size
91
+ end
92
+
74
93
  def get_buffer_by_filename(fname)
75
94
  #TODO: check using stat/inode? http://ruby-doc.org/core-1.9.3/File/Stat.html#method-i-ino
76
- buf_idx = self.index { |b| b.fname == fname }
77
- return buf_idx
95
+ b = @list.find { |b| b.fname == fname }
96
+ return b.id unless b.nil?
97
+ return nil
78
98
  end
79
99
 
80
100
  def get_buffer_by_id(id)
81
- buf_idx = self.index { |b| b.id == id }
82
- return buf_idx
101
+ return @h[id]
83
102
  end
84
103
 
85
104
  def add_buf_to_history(buf_idx)
86
- if self.include?(buf_idx)
105
+ if @list.include?(buf_idx)
87
106
  @buffer_history << @buf_idx
88
- @recent_ind = 0
89
- compact_buf_history()
107
+ @navigation_idx = 0
108
+ # compact_buf_history()
90
109
  else
91
110
  debug "buffer_list, no such id:#{buf_idx}"
92
111
  return
@@ -94,52 +113,40 @@ class BufferList < Array
94
113
  end
95
114
 
96
115
  def add_current_buf_to_history()
97
- @recent_ind = 0
98
- @buffer_history << @current_buf
99
- compact_buf_history()
116
+ @h[@current_buf].update_access_time
100
117
  end
101
118
 
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
119
+ def set_current_buffer_by_id(idx, update_history = true)
108
120
  set_current_buffer(idx, update_history)
109
121
  end
110
122
 
111
- def set_current_buffer(buffer_i, update_history = true)
112
- buffer_i = self.size -1 if buffer_i > self.size
113
- buffer_i = 0 if buffer_i < 0
114
- if !vma.buf.nil? and vma.kbd.get_mode != :browse #TODO
123
+ def set_current_buffer(idx, update_history = true)
124
+ # Set update_history = false if we are only browsing
125
+
126
+ if !vma.buf.nil? and vma.kbd.get_scope != :editor
115
127
  # Save keyboard mode status of old buffer when switching buffer
116
128
  vma.buf.mode_stack = vma.kbd.default_mode_stack.clone
117
129
  end
118
- vma.buf = self[buffer_i]
119
- return if !vma.buf
130
+ return if !@h[idx]
131
+ vma.buf = bu = @h[idx]
120
132
  update_last_dir(vma.buf)
121
- @current_buf = buffer_i
122
- debug "SWITCH BUF2. bufsize:#{self.size}, curbuf: #{@current_buf}"
123
- fpath = vma.buf.fname
124
- if fpath and fpath.size > 50
125
- fpath = fpath[-50..-1]
126
- end
133
+ @current_buf = idx
134
+ debug "SWITCH BUF. bufsize:#{@list.size}, curbuf: #{@current_buf}"
127
135
 
128
- if update_history
129
- add_current_buf_to_history
130
- end
131
136
  vma.hook.call(:change_buffer, vma.buf)
132
- vma.buf.set_active # TODO
133
137
 
134
- vma.gui.set_current_buffer(vma.buf.id)
138
+ bu.set_active # TODO
139
+ bu.update_access_time if update_history
140
+ vma.gui.set_current_buffer(idx)
135
141
 
136
- if !vma.buf.mode_stack.nil? and vma.kbd.get_mode != :browse #TODO
137
- debug "set kbd mode stack #{vma.buf.mode_stack} #{vma.buf.id}", 2
138
- # Reload previously saved keyboard mode status
139
- # vma.kbd.set_mode_stack(vma.buf.mode_stack.clone) #TODO:needed?
140
- vma.kbd.set_mode_stack([vma.buf.default_mode])
141
- end
142
- vma.kbd.set_mode_to_default
142
+ #TODO: delete?
143
+ # if !vma.buf.mode_stack.nil? and vma.kbd.get_scope != :editor #TODO
144
+ # debug "set kbd mode stack #{vma.buf.mode_stack} #{vma.buf.id}", 2
145
+ # Reload previously saved keyboard mode status
146
+ # vma.kbd.set_mode_stack(vma.buf.mode_stack.clone) #TODO:needed?
147
+ # vma.kbd.set_mode_stack([vma.buf.default_mode])
148
+ # end
149
+ # vma.kbd.set_mode_to_default if vma.kbd.get_scope != :editor
143
150
 
144
151
  gui_set_window_title(vma.buf.title, vma.buf.subtitle)
145
152
 
@@ -169,100 +176,80 @@ class BufferList < Array
169
176
  return @last_dir
170
177
  end
171
178
 
172
- def get_recent_buffers()
173
- bufs = []; b = {}
174
- @buffer_history.reverse.each { |x| bufs << x if !b[x] && x < self.size; b[x] = true }
175
- return bufs
179
+ def reset_navigation
180
+ @navigation_idx = 0
176
181
  end
177
182
 
178
- def history_switch_backwards()
179
- recent = get_recent_buffers()
180
- @recent_ind += 1
181
- @recent_ind = 0 if @recent_ind >= recent.size
182
- bufid = recent[@recent_ind]
183
- debug "IND:#{@recent_ind} RECENT:#{recent.join(" ")}"
184
- set_current_buffer(bufid, false)
183
+ def history_switch_backwards
184
+ @navigation_idx += 1
185
+ @navigation_idx = 0 if @navigation_idx >= list.size
186
+ b = slist[-1 - @navigation_idx]
187
+ debug "IND:#{@navigation_idx} RECENT:#{slist.collect { |x| x.fname }.join(" ")}"
188
+ set_current_buffer(b.id, false)
185
189
  end
186
190
 
187
191
  def history_switch_forwards()
188
- recent = get_recent_buffers()
189
- @recent_ind -= 1
190
- @recent_ind = self.size - 1 if @recent_ind < 0
191
- bufid = recent[@recent_ind]
192
- debug "IND:#{@recent_ind} RECENT:#{recent.join(" ")}"
193
- set_current_buffer(bufid, false)
192
+ @navigation_idx -= 1
193
+ @navigation_idx = list.size - 1 if @navigation_idx < 0
194
+
195
+ b = slist[-1 - @navigation_idx]
196
+ debug "IND:#{@navigation_idx} RECENT:#{slist.collect { |x| x.fname }.join(" ")}"
197
+ set_current_buffer(b.id, false)
194
198
  end
195
199
 
196
- def compact_buf_history()
197
- h = {}
198
- # Keep only first occurence in history
199
- bh = @buffer_history.reverse.select { |x| r = h[x] == nil; h[x] = true; r }
200
- @buffer_history = bh.reverse
200
+ def get_last_non_active_buffer
201
+ for bu in slist.reverse
202
+ return bu.id if !bu.is_active
203
+ end
204
+ return nil
201
205
  end
202
206
 
203
- # Close buffer in the background
204
- # TODO: if open in another widget
205
- def close_other_buffer(buffer_i)
206
- return if self.size <= buffer_i
207
- return if @current_buf == buffer_i
207
+ def close_buffer(idx, from_recent = false, auto_open: true)
208
+ return if idx.nil?
209
+ bu = @h[idx]
210
+ return if bu.nil?
208
211
 
209
- bufname = self[buffer_i].basename
212
+ bufname = bu.basename
210
213
  message("Closed buffer #{bufname}")
211
214
 
212
- self.slice!(buffer_i)
213
- @buffer_history = @buffer_history.collect { |x| r = x; r = x - 1 if x > buffer_i; r = nil if x == buffer_i; r }.compact
214
- end
215
-
216
- def close_buffer(buffer_i, from_recent = false)
217
- return if buffer_i.nil?
218
- return if self.size <= buffer_i
215
+ @list.delete(@h[idx])
216
+ @h.delete(idx)
219
217
 
220
- bufname = self[buffer_i].basename
221
- message("Closed buffer #{bufname}")
222
- recent = get_recent_buffers
223
- jump_to_buf = recent[@recent_ind + 1]
224
- jump_to_buf = 0 if jump_to_buf == nil
225
-
226
- self.slice!(buffer_i)
227
- @buffer_history = @buffer_history.collect { |x| r = x; r = x - 1 if x > buffer_i; r = nil if x == buffer_i; r }.compact
228
-
229
- if @current_buf == buffer_i
230
- if from_recent
231
- @current_buf = jump_to_buf
232
- else
233
- # Find last edited buffer that is not already open
234
- @current_buf = @buffer_history.filter { |x| !vma.gui.is_buffer_open(self[x].id) }.last
218
+ if auto_open
219
+ @current_buf = get_last_non_active_buffer
220
+ if @list.size == 0 or @current_buf.nil?
221
+ bu = Buffer.new("\n")
222
+ add(bu)
223
+ @current_buf = bu.id
235
224
  end
225
+ set_current_buffer(@current_buf, false)
236
226
  end
237
- if self.size == 0 or @current_buf.nil?
238
- self << Buffer.new("\n")
239
- @current_buf = 0
240
- else
241
- @current_buf = 0 if @current_buf >= self.size
242
- end
243
- set_current_buffer(@current_buf, false)
244
227
  end
245
228
 
246
- def close_all_buffers()
247
- message("Closing all buffers")
248
- while true
249
- if self.size == 1
250
- close_buffer(0)
251
- break
252
- else
253
- close_buffer(0)
254
- end
255
- end
256
- # self << Buffer.new("\n")
229
+ # Close buffer in the background
230
+ def close_other_buffer(idx)
231
+ close_buffer(idx, auto_open: false)
257
232
  end
258
233
 
234
+ #TODO
235
+ # def close_all_buffers()
236
+ # message("Closing all buffers")
237
+ # while @list.size > 0
238
+ # if self.size == 1
239
+ # close_buffer(0)
240
+ # break
241
+ # else
242
+ # close_buffer(0)
243
+ # end
244
+ # end
245
+ # # self << Buffer.new("\n")
246
+ # end
247
+
259
248
  def close_scrap_buffers()
260
- i = 0
261
- while i < self.size
262
- if !self[i].pathname
263
- close_buffer(i)
264
- else
265
- i += 1
249
+ l = @list.clone
250
+ for bu in l
251
+ if !bu.pathname
252
+ close_buffer(bu.id)
266
253
  end
267
254
  end
268
255
  end
@@ -273,7 +260,7 @@ class BufferList < Array
273
260
 
274
261
  def delete_current_buffer(from_recent = false)
275
262
  fn = buf.fname
276
- close_buffer(@current_buf, from_recent)
263
+ close_buffer(@current_buf)
277
264
  #TODO: confirm with user, "Do you want to delete file X"
278
265
  if is_existing_file(fn)
279
266
  message("Deleting file: #{fn}")
@@ -28,28 +28,26 @@ class BufferManager
28
28
  l = @buf.lpos - @header.size
29
29
  return nil if l < 0
30
30
  bufid = @line_to_id[l]
31
- buf_i = vma.buffers.get_buffer_by_id(bufid)
32
- return buf_i
31
+ return bufid
33
32
  end
34
33
 
35
34
  def close_selected
36
- buf_i = buf_of_current_line()
37
- if buf_i.nil?
35
+ idx = buf_of_current_line()
36
+ r = @buf.current_line_range
37
+ Gui.hilight_range(@buf,r, color:"#666666ff")
38
+ if idx.nil?
38
39
  message("buf already closed")
39
40
  return
40
41
  end
41
- vma.buffers.close_other_buffer(buf_i)
42
+ vma.buffers.close_other_buffer(idx)
42
43
  end
43
44
 
44
45
  def select_line
45
- buf_i = buf_of_current_line()
46
- return if buf_i.nil?
46
+ idx = buf_of_current_line()
47
+ return if idx.nil?
47
48
 
48
- # vma.buffers.close_current_buffer() #TODO:??
49
- vma.buffers.set_current_buffer(buf_i)
50
-
51
- bid = vma.buffers.get_buffer_by_id(@buf.id)
52
- vma.buffers.close_other_buffer(bid)
49
+ vma.buffers.set_current_buffer(idx)
50
+ vma.buffers.close_other_buffer(@buf.id)
53
51
  @@cur = nil
54
52
  end
55
53
 
@@ -70,7 +68,7 @@ class BufferManager
70
68
  s << "\n"
71
69
  i = 0
72
70
  jump_to_line = 0
73
- for b in vma.buffers.sort_by { |x| x.list_str }
71
+ for b in vma.buffers.list.sort_by { |x| x.list_str }
74
72
  if b.id == vma.buf.id # current file
75
73
  # s << " "
76
74
  jump_to_line = i
@@ -31,5 +31,6 @@ class Clipboard
31
31
  end
32
32
 
33
33
  def set_system_clipboard(arg)
34
+ debug arg,2
34
35
  vma.gui.window.display.clipboard.set(arg)
35
36
  end
data/lib/vimamsa/conf.rb CHANGED
@@ -153,3 +153,9 @@ cnf.workspace_folders = []
153
153
 
154
154
  cnf.match.highlight.color = "#10bd8e"
155
155
 
156
+ cnf.lsp.enabled = false
157
+
158
+ cnf.font.size = 11
159
+ cnf.font.family = "Monospace"
160
+
161
+
@@ -15,8 +15,6 @@ CURRENT_CHAR_FORWARD = 2005
15
15
  CURRENT_CHAR_BACKWARD = 2006
16
16
  START_OF_BUFFER = 2007
17
17
  END_OF_BUFFER = 2008
18
- BACKWARD = 2009
19
- FORWARD = 2010
20
18
  END_OF_LINE = 2011
21
19
  BEGINNING_OF_LINE = 2012
22
20
  WORD_START = 2013
@@ -30,14 +28,6 @@ REPLACE = 3002
30
28
  KEY_PRESS = 6
31
29
  KEY_RELEASE = 7 # QEvent::KeyRelease
32
30
 
33
- # http://qt-project.org/doc/qt-5.0/qtcore/qt.html#KeyboardModifier-enum
34
- ALTMODIFIER = 0x08000000
35
- NOMODIFIER = 0x00000000 # No modifier key is pressed.
36
- SHIFTMODIFIER = 0x02000000 # A Shift key on the keyboard is pressed.
37
- CONTROLMODIFIER = 0x04000000 # A Ctrl key on the keyboard is pressed.
38
- ALTMODIFIER = 0x08000000 # An Alt key on the keyboard is pressed.
39
- METAMODIFIER = 0x10000000 # A Meta key on the keyboard is pressed.
40
- KEYPADMODIFIER = 0x20000000 # A keypad button is pressed.
41
31
 
42
32
 
43
33
 
data/lib/vimamsa/debug.rb CHANGED
@@ -41,6 +41,11 @@ def log_error(message)
41
41
  #TODO
42
42
  end
43
43
 
44
+ def show_caller
45
+ puts caller().join("\n")
46
+ end
47
+
48
+
44
49
  def crash(message, e = nil)
45
50
  puts "FATAL ERROR:#{message}"
46
51
  puts caller().join("\n")
@@ -5,6 +5,7 @@ def handle_drag_and_drop(fname)
5
5
  buf.handle_drag_and_drop(fname)
6
6
  end
7
7
 
8
+
8
9
  class Editor
9
10
  attr_reader :file_content_search_paths, :file_name_search_paths, :gui, :hook, :macro
10
11
  attr_accessor :converters, :fh, :paint_stack, :kbd, :langsrv, :register, :cur_register, :clipboard
@@ -68,6 +69,11 @@ class Editor
68
69
  $search = Search.new
69
70
  register_plugin(:Search, $search)
70
71
 
72
+ # build_key_bindings_tree
73
+ @kbd = KeyBindingTree.new()
74
+ $kbd = @kbd
75
+ require "vimamsa/key_bindings_vimlike"
76
+
71
77
  $buffers = BufferList.new
72
78
  $minibuffer = Buffer.new(">", "")
73
79
  @langsrv = {}
@@ -75,10 +81,7 @@ class Editor
75
81
  require "vimamsa/text_transforms"
76
82
 
77
83
  debug "ARGV: " + ARGV.inspect
78
- # build_key_bindings_tree
79
- @kbd = KeyBindingTree.new()
80
- $kbd = @kbd
81
- require "vimamsa/key_bindings_vimlike"
84
+
82
85
  sleep(0.03)
83
86
 
84
87
  BufferManager.init
@@ -121,8 +124,9 @@ class Editor
121
124
 
122
125
  Grep.init
123
126
  FileManager.init
127
+ Autocomplete.init
124
128
 
125
- if conf(:enable_lsp)
129
+ if cnf.lsp.enabled?
126
130
  require "vimamsa/langservp"
127
131
  require "vimamsa/audio" # TODO:config
128
132
  @langsrv["ruby"] = LangSrv.new("ruby")
@@ -444,7 +448,7 @@ def minibuffer_delete()
444
448
  end
445
449
 
446
450
  def error(str)
447
- puts caller[0]
451
+ show_caller
448
452
  debug "#{caller[0]} ERROR: #{str}", 2
449
453
  end
450
454
 
@@ -487,7 +491,7 @@ GUESS_ENCODING_ORDER = [
487
491
  ]
488
492
 
489
493
  def create_new_file(filename = nil, file_contents = "\n")
490
- buffer = Buffer.new(file_contents)
494
+ buffer = Buffer.new(file_contents, filename)
491
495
 
492
496
  debug "NEW FILE CREATED: #{buffer.id}"
493
497
  vma.buffers.add(buffer)
@@ -580,6 +584,12 @@ def open_new_file(filename, file_contents = "")
580
584
  if !is_path_writable(filename)
581
585
  message("Path #{filename} cannot be written to")
582
586
  return false
587
+ elsif !File.exist?(filename)
588
+ message("File #{filename} does not exist")
589
+ return false
590
+ elsif !file_is_text_file(filename)
591
+ message("File #{filename} does not contain text")
592
+ return false
583
593
  end
584
594
  message "New file opened: #{filename}"
585
595
  fname = filename
@@ -4,9 +4,6 @@ class FileHistory
4
4
  attr_accessor :history
5
5
 
6
6
  def initialize()
7
- # puts self.method("update")
8
- # x = self.method("update")
9
- # x.call("ASFASF")
10
7
 
11
8
  vma.hook.register(:change_buffer, self.method("update"))
12
9
  vma.hook.register(:shutdown, self.method("save"))
@@ -17,9 +14,6 @@ class FileHistory
17
14
  $search_list = []
18
15
  end
19
16
 
20
- # def self.init()
21
- # end
22
-
23
17
  def update(buf)
24
18
  debug "FileHistory.update(buf=#{buf.fname})"
25
19
  return if !buf.fname
@@ -30,8 +24,6 @@ class FileHistory
30
24
  @history[buf.fname] += 1
31
25
  end
32
26
  debug @history
33
-
34
- # puts "FileHistory.update(buf=#{buf})"
35
27
  end
36
28
 
37
29
  def save()
@@ -18,7 +18,7 @@ class FileManager
18
18
  end
19
19
 
20
20
  def self.init()
21
- reg_act(:start_file_selector, proc { FileManager.new.run; vma.kbd.set_mode(:file_exp); vma.kbd.set_default_mode(:file_exp) }, "File selector")
21
+ reg_act(:start_file_selector, proc { FileManager.new.run; vma.kbd.set_mode(:file_exp); }, "File selector")
22
22
 
23
23
  reg_act(:fexp_chdir_parent, proc { FileManager.chdir_parent }, "File selector")
24
24
  reg_act(:fexp_select, proc { buf.module.select_line }, "")
@@ -86,7 +86,7 @@ class FileManager
86
86
  # Thread.new {
87
87
  for fn in @cut_files
88
88
  FileUtils.move(fn, @ld)
89
- puts "FileUtils.move(#{fn}, #{@ld})"
89
+ debug "FileUtils.move(#{fn}, #{@ld})"
90
90
  end
91
91
  elsif !@copied_files.empty?
92
92
  for fn in @copied_files
@@ -263,8 +263,11 @@ class FileManager
263
263
  # elsif vma.can_open_extension?(fn)
264
264
  # jump_to_file(fn)
265
265
  elsif file_is_text_file(fn)
266
- bufs.close_current_buffer
266
+ # bufs.close_current_buffer
267
267
  jump_to_file(fn)
268
+ # vma.buffers.set_current_buffer(idx)
269
+ vma.buffers.close_other_buffer(@buf.id)
270
+
268
271
  else
269
272
  open_with_default_program(fn)
270
273
  end