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
# 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)
end

#add(_buf) ⇒ Object



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

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



110
111
112
113
114
115
116
117
118
119
# File 'lib/vimamsa/buffer_list.rb', line 110

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



121
122
123
# File 'lib/vimamsa/buffer_list.rb', line 121

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

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



248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
# File 'lib/vimamsa/buffer_list.rb', line 248

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)

  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



298
299
300
# File 'lib/vimamsa/buffer_list.rb', line 298

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

#close_other_buffer(idx) ⇒ Object

Close buffer in the background



271
272
273
# File 'lib/vimamsa/buffer_list.rb', line 271

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



289
290
291
292
293
294
295
296
# File 'lib/vimamsa/buffer_list.rb', line 289

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



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

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



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

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

#get_buffer_by_filename(fname) ⇒ Object



99
100
101
102
103
104
# File 'lib/vimamsa/buffer_list.rb', line 99

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



106
107
108
# File 'lib/vimamsa/buffer_list.rb', line 106

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

#get_last_dirObject



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

def get_last_dir
  return File.expand_path(@last_dir)
end

#get_last_non_active_bufferObject



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

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



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

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



190
191
192
193
194
195
196
197
198
199
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
# File 'lib/vimamsa/buffer_list.rb', line 190

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



233
234
235
# File 'lib/vimamsa/buffer_list.rb', line 233

def history_switch_backwards()
  history_switch(-1)
end

#history_switch_forwardsObject



237
238
239
# File 'lib/vimamsa/buffer_list.rb', line 237

def history_switch_forwards()
  history_switch(+1)
end

#reset_navigationObject



186
187
188
# File 'lib/vimamsa/buffer_list.rb', line 186

def reset_navigation
  @navigation_idx = 0
end

#set_current_buffer(idx, update_history = true) ⇒ Object



129
130
131
132
133
134
135
136
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
# File 'lib/vimamsa/buffer_list.rb', line 129

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
  vma.gui.set_current_buffer(idx)

  #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



125
126
127
# File 'lib/vimamsa/buffer_list.rb', line 125

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

#sizeObject



95
96
97
# File 'lib/vimamsa/buffer_list.rb', line 95

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



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

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

#switch_to_last_bufObject



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

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



167
168
169
# File 'lib/vimamsa/buffer_list.rb', line 167

def to_s
  return self.class.to_s
end

#update_last_dir(buf) ⇒ Object



171
172
173
174
175
176
# File 'lib/vimamsa/buffer_list.rb', line 171

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