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
# File 'lib/vimamsa/buffer_list.rb', line 26

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

Instance Attribute Details

#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?



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

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



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

def add(_buf)
  @list << _buf
  @h[_buf.id] = _buf
end

#add_current_buf_to_historyObject



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

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

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



232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
# File 'lib/vimamsa/buffer_list.rb', line 232

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



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

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

#close_other_buffer(idx) ⇒ Object

Close buffer in the background



257
258
259
# File 'lib/vimamsa/buffer_list.rb', line 257

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



275
276
277
278
279
280
281
282
# File 'lib/vimamsa/buffer_list.rb', line 275

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



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

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



57
58
59
60
61
# File 'lib/vimamsa/buffer_list.rb', line 57

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

#get_buffer_by_filename(fname) ⇒ Object



91
92
93
94
95
96
# File 'lib/vimamsa/buffer_list.rb', line 91

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



98
99
100
# File 'lib/vimamsa/buffer_list.rb', line 98

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

#get_last_dirObject



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

def get_last_dir
  return File.expand_path(@last_dir)
end

#get_last_non_active_bufferObject



225
226
227
228
229
230
# File 'lib/vimamsa/buffer_list.rb', line 225

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



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

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



174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
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
# File 'lib/vimamsa/buffer_list.rb', line 174

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



217
218
219
# File 'lib/vimamsa/buffer_list.rb', line 217

def history_switch_backwards()
  history_switch(-1)
end

#history_switch_forwardsObject



221
222
223
# File 'lib/vimamsa/buffer_list.rb', line 221

def history_switch_forwards()
  history_switch(+1)
end


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

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



170
171
172
# File 'lib/vimamsa/buffer_list.rb', line 170

def reset_navigation
  @navigation_idx = 0
end

#set_current_buffer(idx, update_history = true) ⇒ Object



110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/vimamsa/buffer_list.rb', line 110

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
  vma.gui.func_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



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

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

#sizeObject



87
88
89
# File 'lib/vimamsa/buffer_list.rb', line 87

def size
  return @list.size
end

#slistObject



52
53
54
55
# File 'lib/vimamsa/buffer_list.rb', line 52

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

#switch_to_last_bufObject



79
80
81
82
83
84
85
# File 'lib/vimamsa/buffer_list.rb', line 79

def switch_to_last_buf()
  debug "SWITCH TO LAST BUF:"
  last_buf = slist[-2]
  if !last_buf.nil?
    set_current_buffer(last_buf.id)
  end
end

#to_sObject



151
152
153
# File 'lib/vimamsa/buffer_list.rb', line 151

def to_s
  return self.class.to_s
end

#update_last_dir(buf) ⇒ Object



155
156
157
158
159
160
# File 'lib/vimamsa/buffer_list.rb', line 155

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