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



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

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



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

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

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



208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
# File 'lib/vimamsa/buffer_list.rb', line 208

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



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

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

#close_other_buffer(idx) ⇒ Object

Close buffer in the background



231
232
233
# File 'lib/vimamsa/buffer_list.rb', line 231

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



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

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



262
263
264
265
266
267
268
269
270
# File 'lib/vimamsa/buffer_list.rb', line 262

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

#get_buffer_by_filename(fname) ⇒ Object



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

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



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

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

#get_last_dirObject



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

def get_last_dir
  return @last_dir
end

#get_last_non_active_bufferObject



201
202
203
204
205
206
# File 'lib/vimamsa/buffer_list.rb', line 201

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



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

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_backwardsObject



184
185
186
187
188
189
190
# File 'lib/vimamsa/buffer_list.rb', line 184

def history_switch_backwards
  @navigation_idx += 1
  @navigation_idx = 0 if @navigation_idx >= list.size
  b = slist[-1 - @navigation_idx]
  debug "IND:#{@navigation_idx} RECENT:#{slist.collect { |x| x.fname }.join(" ")}"
  set_current_buffer(b.id, false)
end

#history_switch_forwardsObject



192
193
194
195
196
197
198
199
# File 'lib/vimamsa/buffer_list.rb', line 192

def history_switch_forwards()
  @navigation_idx -= 1
  @navigation_idx = list.size - 1 if @navigation_idx < 0

  b = slist[-1 - @navigation_idx]
  debug "IND:#{@navigation_idx} RECENT:#{slist.collect { |x| x.fname }.join(" ")}"
  set_current_buffer(b.id, false)
end

#reset_navigationObject



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

def reset_navigation
  @navigation_idx = 0
end

#set_current_buffer(idx, update_history = true) ⇒ Object



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
150
151
152
153
154
155
156
157
158
159
# File 'lib/vimamsa/buffer_list.rb', line 123

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

  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 cnf.debug? # experimental
  return bu
end

#set_current_buffer_by_id(idx, update_history = true) ⇒ Object



119
120
121
# File 'lib/vimamsa/buffer_list.rb', line 119

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

#sizeObject



89
90
91
# File 'lib/vimamsa/buffer_list.rb', line 89

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



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

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



161
162
163
# File 'lib/vimamsa/buffer_list.rb', line 161

def to_s
  return self.class.to_s
end

#update_last_dir(buf) ⇒ Object



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

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