Class: BufferList

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

def initialize()
  @last_dir = File.expand_path(".")
  @buffer_history = []
  super
  @current_buf=0
end

Instance Attribute Details

#buffer_historyObject (readonly)

Returns the value of attribute buffer_history.



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

def buffer_history
  @buffer_history
end

#current_bufObject (readonly)

Returns the value of attribute current_buf.



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

def current_buf
  @current_buf
end

#last_dirObject

Returns the value of attribute last_dir.



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

def last_dir
  @last_dir
end

Instance Method Details

#<<(_buf) ⇒ Object

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



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

def <<(_buf)
  super
  vma.buf = _buf
  @current_buf = self.size - 1
  @buffer_history << @current_buf
  @recent_ind = 0
  $hook.call(:change_buffer, vma.buf)
  vma.gui.set_current_buffer(vma.buf.id)
  gui_set_cursor_pos(vma.buf.id, vma.buf.pos)
  update_last_dir(_buf)
end

#add(_buf) ⇒ Object



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

def add(_buf)
  self.append(_buf)
  @buffer_history << self.size - 1
end

#add_buf_to_history(buf_idx) ⇒ Object



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

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

#add_current_buf_to_historyObject



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

def add_current_buf_to_history()
  @recent_ind = 0
  @buffer_history << @current_buf
  compact_buf_history()
end

#close_all_buffersObject



229
230
231
232
233
234
235
236
237
238
239
240
# File 'lib/vimamsa/buffer_list.rb', line 229

def close_all_buffers()
  message("Closing all buffers")
  while true
    if self.size == 1
      close_buffer(0)
      break
    else
      close_buffer(0)
    end
  end
  # self << Buffer.new("\n")
end

#close_buffer(buffer_i, from_recent = false) ⇒ Object



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

def close_buffer(buffer_i, from_recent = false)
  return if buffer_i.nil?
  return if self.size <= buffer_i

  bufname = self[buffer_i].basename
  message("Closed buffer #{bufname}")
  recent = get_recent_buffers
  jump_to_buf = recent[@recent_ind + 1]
  jump_to_buf = 0 if jump_to_buf == nil

  self.slice!(buffer_i)
  @buffer_history = @buffer_history.collect { |x| r = x; r = x - 1 if x > buffer_i; r = nil if x == buffer_i; r }.compact

  if @current_buf == buffer_i
    if from_recent
      @current_buf = jump_to_buf
    else
      # Find last edited buffer that is not already open
      @current_buf = @buffer_history.filter{|x| !vma.gui.is_buffer_open(self[x].id)}.last
    end
  end
  if self.size == 0 or @current_buf.nil?
    self << Buffer.new("\n")
    @current_buf = 0
  else
    @current_buf = 0 if @current_buf >= self.size
  end
  set_current_buffer(@current_buf, false)
end

#close_current_buffer(from_recent = false) ⇒ Object



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

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

#close_other_buffer(buffer_i) ⇒ Object

Close buffer in the background TODO: if open in another widget



188
189
190
191
192
193
194
195
196
197
# File 'lib/vimamsa/buffer_list.rb', line 188

def close_other_buffer(buffer_i)
  return if self.size <= buffer_i
  return if @current_buf == buffer_i

  bufname = self[buffer_i].basename
  message("Closed buffer #{bufname}")

  self.slice!(buffer_i)
  @buffer_history = @buffer_history.collect { |x| r = x; r = x - 1 if x > buffer_i; r = nil if x == buffer_i; r }.compact
end

#close_scrap_buffersObject



242
243
244
245
246
247
248
249
250
251
# File 'lib/vimamsa/buffer_list.rb', line 242

def close_scrap_buffers()
  i = 0
  while i < self.size
    if !self[i].pathname
      close_buffer(i)
    else
      i += 1
    end
  end
end

#compact_buf_historyObject



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

def compact_buf_history()
  h = {}
  # Keep only first occurence in history
  bh = @buffer_history.reverse.select { |x| r = h[x] == nil; h[x] = true; r }
  @buffer_history = bh.reverse
end

#delete_current_buffer(from_recent = false) ⇒ Object



257
258
259
260
261
262
263
264
265
# File 'lib/vimamsa/buffer_list.rb', line 257

def delete_current_buffer(from_recent = false)
  fn = buf.fname
  close_buffer(@current_buf, from_recent)
  #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

#get_buffer_by_filename(fname) ⇒ Object



74
75
76
77
78
# File 'lib/vimamsa/buffer_list.rb', line 74

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
  buf_idx = self.index { |b| b.fname == fname }
  return buf_idx
end

#get_buffer_by_id(id) ⇒ Object



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

def get_buffer_by_id(id)
  buf_idx = self.index { |b| b.id == id }
  return buf_idx
end

#get_last_dirObject



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

def get_last_dir
  return @last_dir
end

#get_last_visited_idObject



60
61
62
63
# File 'lib/vimamsa/buffer_list.rb', line 60

def get_last_visited_id
  last_buf = @buffer_history[-2]
  return self[last_buf].id
end

#get_recent_buffersObject



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

def get_recent_buffers()
  bufs = []; b = {}
  @buffer_history.reverse.each { |x| bufs << x if !b[x] && x < self.size; b[x] = true }
  return bufs
end

#history_switch_backwardsObject



161
162
163
164
165
166
167
168
# File 'lib/vimamsa/buffer_list.rb', line 161

def history_switch_backwards()
  recent = get_recent_buffers()
  @recent_ind += 1
  @recent_ind = 0 if @recent_ind >= recent.size
  bufid = recent[@recent_ind]
  debug "IND:#{@recent_ind} RECENT:#{recent.join(" ")}"
  set_current_buffer(bufid, false)
end

#history_switch_forwardsObject



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

def history_switch_forwards()
  recent = get_recent_buffers()
  @recent_ind -= 1
  @recent_ind = self.size - 1 if @recent_ind < 0
  bufid = recent[@recent_ind]
  debug "IND:#{@recent_ind} RECENT:#{recent.join(" ")}"
  set_current_buffer(bufid, false)
end

#set_current_buffer(buffer_i, update_history = true) ⇒ Object



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

def set_current_buffer(buffer_i, update_history = true)
  buffer_i = self.size -1 if buffer_i > self.size
  buffer_i = 0 if buffer_i < 0
  vma.buf = self[buffer_i]
  return if !vma.buf
  @current_buf = buffer_i
  debug "SWITCH BUF2. bufsize:#{self.size}, curbuf: #{@current_buf}"
  fpath = vma.buf.fname
  if fpath and fpath.size > 50
    fpath = fpath[-50..-1]
  end

  if update_history
    add_current_buf_to_history
  end

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

  vma.gui.set_current_buffer(vma.buf.id)

  gui_set_window_title(vma.buf.title, vma.buf.subtitle)

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

  # hpt_scan_images() if $debug # experimental
end

#set_current_buffer_by_id(buf_id, update_history = true) ⇒ Object



102
103
104
105
106
107
108
109
# File 'lib/vimamsa/buffer_list.rb', line 102

def set_current_buffer_by_id(buf_id, update_history = true)
  idx = get_buffer_by_id(buf_id)
  if idx.nil?
    debug "IDX=nil"
    return
  end
  set_current_buffer(idx, update_history)
end

#switchObject



51
52
53
54
55
56
57
58
# File 'lib/vimamsa/buffer_list.rb', line 51

def switch()
  debug "SWITCH BUF. bufsize:#{self.size}, curbuf: #{@current_buf}"
  @current_buf += 1
  @current_buf = 0 if @current_buf >= self.size
  m = method("switch")
  set_last_command({ method: m, params: [] })
  set_current_buffer(@current_buf)
end

#switch_to_last_bufObject



65
66
67
68
69
70
71
72
# File 'lib/vimamsa/buffer_list.rb', line 65

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

#update_last_dir(buf) ⇒ Object



141
142
143
144
145
# File 'lib/vimamsa/buffer_list.rb', line 141

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