Class: BufferList

Inherits:
Object
  • Object
show all
Defined in:
lib/vimamsa/buffer_list.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeBufferList

Returns a new instance of BufferList.



26
27
28
29
30
31
32
33
34
# File 'lib/vimamsa/buffer_list.rb', line 26

def initialize()
  @last_dir = File.expand_path(".")
  @buffer_history = []
  super
  @current_buf = 0
  @list = []
  @h = {}
  reset_navigation
end

Instance Attribute Details

#buffer_historyObject (readonly)

Returns the value of attribute buffer_history.



23
24
25
# File 'lib/vimamsa/buffer_list.rb', line 23

def buffer_history
  @buffer_history
end

#current_bufObject (readonly)

Returns the value of attribute current_buf.



23
24
25
# File 'lib/vimamsa/buffer_list.rb', line 23

def current_buf
  @current_buf
end

#last_dirObject

Returns the value of attribute last_dir.



23
24
25
# File 'lib/vimamsa/buffer_list.rb', line 23

def last_dir
  @last_dir
end

#last_fileObject (readonly)

Returns the value of attribute last_file.



23
24
25
# File 'lib/vimamsa/buffer_list.rb', line 23

def last_file
  @last_file
end

#listObject

Returns the value of attribute list.



24
25
26
# File 'lib/vimamsa/buffer_list.rb', line 24

def list
  @list
end

Instance Method Details

#<<(_buf) ⇒ Object

lastdir = File.expand_path(“.”) if lastdir.nil?



37
38
39
40
41
42
43
44
45
46
# File 'lib/vimamsa/buffer_list.rb', line 37

def <<(_buf)
  vma.buf = _buf
  self.add(_buf)

  $hook.call(:change_buffer, vma.buf)
  vma.gui.set_current_buffer(vma.buf.id) #TODO: handle elswhere?
  # vma.buf.view.set_cursor_pos(vma.buf.pos)  #TODO: handle elswhere?
  update_last_dir(_buf)
  vma.gui.file_panel_refresh
end

#add(_buf) ⇒ Object



48
49
50
51
52
53
# File 'lib/vimamsa/buffer_list.rb', line 48

def add(_buf)
  @buffer_history << _buf.id
  # @navigation_idx = _buf.id #TODO:??
  @list << _buf
  @h[_buf.id] = _buf
end

#add_buf_to_history(buf_idx) ⇒ Object



118
119
120
121
122
123
124
125
126
127
# File 'lib/vimamsa/buffer_list.rb', line 118

def add_buf_to_history(buf_idx)
  if @list.include?(buf_idx)
    @buffer_history << @buf_idx
    @navigation_idx = 0
    # compact_buf_history()
  else
    debug "buffer_list, no such id:#{buf_idx}"
    return
  end
end

#add_current_buf_to_historyObject



129
130
131
# File 'lib/vimamsa/buffer_list.rb', line 129

def add_current_buf_to_history()
  @h[@current_buf].update_access_time
end

#close_buffer(idx, from_recent = false, auto_open: true) ⇒ Object



258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
# File 'lib/vimamsa/buffer_list.rb', line 258

def close_buffer(idx, from_recent = false, auto_open: true)
  return if idx.nil?
  bu = @h[idx]
  return if bu.nil?

  bufname = bu.basename
  message("Closed buffer #{bufname}")

  @list.delete(@h[idx])
  @h.delete(idx)
  gui_close_buffer(idx)
  vma.gui.file_panel_refresh

  if auto_open
    @current_buf = get_last_non_active_buffer
    if @list.size == 0 or @current_buf.nil?
      bu = Buffer.new("\n")
      add(bu)
      @current_buf = bu.id
    end
    set_current_buffer(@current_buf, false)
  end
end

#close_current_buffer(from_recent = false) ⇒ Object



310
311
312
# File 'lib/vimamsa/buffer_list.rb', line 310

def close_current_buffer(from_recent = false)
  close_buffer(@current_buf, from_recent)
end

#close_other_buffer(idx) ⇒ Object

Close buffer in the background



283
284
285
# File 'lib/vimamsa/buffer_list.rb', line 283

def close_other_buffer(idx)
  close_buffer(idx, auto_open: false)
end

#close_scrap_buffersObject

TODO def close_all_buffers() message(“Closing all buffers”) while @list.size > 0 if self.size == 1 close_buffer(0) break else close_buffer(0) end end # self << Buffer.new(“n”) end



301
302
303
304
305
306
307
308
# File 'lib/vimamsa/buffer_list.rb', line 301

def close_scrap_buffers()
  l = @list.clone
  for bu in l
    if !bu.pathname
      close_buffer(bu.id)
    end
  end
end

#delete_current_buffer(from_recent = false) ⇒ Object



314
315
316
317
318
319
320
321
322
# File 'lib/vimamsa/buffer_list.rb', line 314

def delete_current_buffer(from_recent = false)
  fn = buf.fname
  close_buffer(@current_buf)
  #TODO: confirm with user, "Do you want to delete file X"
  if is_existing_file(fn)
    message("Deleting file: #{fn}")
    File.delete(fn)
  end
end

#each(&block) ⇒ Object



70
71
72
73
74
# File 'lib/vimamsa/buffer_list.rb', line 70

def each(&block)
  for x in slist
    block.call(x)
  end
end

#get_buffer_by_filename(fname) ⇒ Object



107
108
109
110
111
112
# File 'lib/vimamsa/buffer_list.rb', line 107

def get_buffer_by_filename(fname)
  #TODO: check using stat/inode?  http://ruby-doc.org/core-1.9.3/File/Stat.html#method-i-ino
  b = @list.find { |b| b.fname == fname }
  return b.id unless b.nil?
  return nil
end

#get_buffer_by_id(id) ⇒ Object



114
115
116
# File 'lib/vimamsa/buffer_list.rb', line 114

def get_buffer_by_id(id)
  return @h[id]
end

#get_last_dirObject



192
193
194
# File 'lib/vimamsa/buffer_list.rb', line 192

def get_last_dir
  return File.expand_path(@last_dir)
end

#get_last_non_active_bufferObject



251
252
253
254
255
256
# File 'lib/vimamsa/buffer_list.rb', line 251

def get_last_non_active_buffer
  for bu in slist.reverse
    return bu.id if !bu.is_active?
  end
  return nil
end

#get_last_visited_idObject



83
84
85
86
87
88
89
90
# File 'lib/vimamsa/buffer_list.rb', line 83

def get_last_visited_id
  last_buf = nil
  for i in 0..(slist.size - 1)
    next if slist[i].is_active?
    last_buf = slist[i].id
  end
  return last_buf
end

#history_switch(dir = -1)) ⇒ Object



200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
# File 'lib/vimamsa/buffer_list.rb', line 200

def history_switch(dir = -1)
  # -1: from newest towards oldest
  # +1: from oldest towards newest

  @navigation_idx += dir * -1
  @navigation_idx = 0 if @navigation_idx >= list.size
  @navigation_idx = list.size - 1 if @navigation_idx < 0

  # most recent is at end of slist
  b = slist[-1 - @navigation_idx]
  puts "@navigation_idx=#{@navigation_idx}"
  non_active =  slist.select{|x|!x.is_active?}
  return if non_active.size == 0

  # Take another one from the history if buffer is already open in a window (active)
  navtmp = @navigation_idx
  i = 1
  while b.is_active?
    pp "b.is_active b=#{b.id}"
    navtmp += dir * -1
    b = slist[-1 - navtmp]
    if b.nil? # went past the beginning or end of array slist
      # Start from the end
      if navtmp != 0 and dir == -1 # did not already start from the end
        navtmp = 0
        i = 0
        b = slist[-1 - navtmp]
      elsif navtmp == list.size and dir == 1
        navtmp = list.size
        i = 0
        b = slist[-1 - navtmp]
      else
        return # No buffer exists which is not active already
      end
    end
    i += 1
  end
  @navigation_idx = navtmp

  pp "IND:#{@navigation_idx} RECENT:#{slist.collect { |x| x.fname }.join("\n")}"
  set_current_buffer(b.id, false)
end

#history_switch_backwardsObject



243
244
245
# File 'lib/vimamsa/buffer_list.rb', line 243

def history_switch_backwards()
  history_switch(-1)
end

#history_switch_forwardsObject



247
248
249
# File 'lib/vimamsa/buffer_list.rb', line 247

def history_switch_forwards()
  history_switch(+1)
end


76
77
78
79
80
81
# File 'lib/vimamsa/buffer_list.rb', line 76

def print_by_access_time
  slist.reverse.each_with_index do |b, i|
    name = b.fname || "(untitled)"
    puts "#{i + 1}. #{b.access_time.strftime('%H:%M:%S')} #{name}"
  end
end

#reset_navigationObject



196
197
198
# File 'lib/vimamsa/buffer_list.rb', line 196

def reset_navigation
  @navigation_idx = 0
end

#set_current_buffer(idx, update_history = true) ⇒ Object



137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
# File 'lib/vimamsa/buffer_list.rb', line 137

def set_current_buffer(idx, update_history = true)
  # Set update_history = false if we are only browsing

  if !vma.buf.nil? and vma.kbd.get_scope != :editor
    # Save keyboard mode status of old buffer when switching buffer
    vma.buf.mode_stack = vma.kbd.default_mode_stack.clone
  end
  return if !@h[idx]
  vma.buf = bu = @h[idx]
  update_last_dir(vma.buf)
  @current_buf = idx
  debug "SWITCH BUF. bufsize:#{@list.size}, curbuf: #{@current_buf}"

  vma.hook.call(:change_buffer, vma.buf)

  bu.set_active # TODO
  bu.update_access_time if update_history
  reset_navigation if update_history
  vma.gui.set_current_buffer(idx)
  vma.gui.file_panel_refresh

  #TODO: delete?
  # if !vma.buf.mode_stack.nil? and vma.kbd.get_scope != :editor #TODO
  # debug "set kbd mode stack #{vma.buf.mode_stack}  #{vma.buf.id}", 2
  # Reload previously saved keyboard mode status
  # vma.kbd.set_mode_stack(vma.buf.mode_stack.clone) #TODO:needed?
  # vma.kbd.set_mode_stack([vma.buf.default_mode])
  # end
  # vma.kbd.set_mode_to_default if vma.kbd.get_scope != :editor

  vma.buf.refresh_title

  if vma.buf.fname
    @last_dir = File.dirname(vma.buf.fname)
  end

  # hpt_scan_images() if cnf.debug? # experimental
  return bu
end

#set_current_buffer_by_id(idx, update_history = true) ⇒ Object



133
134
135
# File 'lib/vimamsa/buffer_list.rb', line 133

def set_current_buffer_by_id(idx, update_history = true)
  set_current_buffer(idx, update_history)
end

#sizeObject



103
104
105
# File 'lib/vimamsa/buffer_list.rb', line 103

def size
  return @list.size
end

#slistObject

NOTE: unused. enable? def switch() debug “SWITCH BUF. bufsize:#BufferList.selfself.size, curbuf: #@current_buf” m = method(“switch”) set_last_command({ method: m, params: [] }) set_current_buffer(@current_buf) end



65
66
67
68
# File 'lib/vimamsa/buffer_list.rb', line 65

def slist
  # TODO: implement using heap/priorityque
  @list.sort_by! { |x| x.access_time }
end

#switch_to_last_bufObject



92
93
94
95
96
97
98
99
100
101
# File 'lib/vimamsa/buffer_list.rb', line 92

def switch_to_last_buf()
  debug "SWITCH TO LAST BUF:"
  # debug @buffer_history
  # last_buf = @buffer_history[-2]

  last_buf = slist[-2]
  if !last_buf.nil?
    set_current_buffer(last_buf.id)
  end
end

#to_sObject



177
178
179
# File 'lib/vimamsa/buffer_list.rb', line 177

def to_s
  return self.class.to_s
end

#update_last_dir(buf) ⇒ Object



181
182
183
184
185
186
# File 'lib/vimamsa/buffer_list.rb', line 181

def update_last_dir(buf)
  if buf.fname
    @last_dir = File.dirname(buf.fname)
    @last_file = buf.fname
  end
end