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.



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

def initialize()
  @last_dir = File.expand_path(".")
  super
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

Instance Method Details

#<<(_buf) ⇒ Object

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



31
32
33
34
35
36
37
38
39
40
# File 'lib/vimamsa/buffer_list.rb', line 31

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

#add_current_buf_to_historyObject



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

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

#close_all_buffersObject



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

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



158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
# File 'lib/vimamsa/buffer_list.rb', line 158

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
      @current_buf = $buffer_history.last
    end
  end
  # Ripl.start :binding => binding
  if self.size == 0
    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



212
213
214
# File 'lib/vimamsa/buffer_list.rb', line 212

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



147
148
149
150
151
152
153
154
155
156
# File 'lib/vimamsa/buffer_list.rb', line 147

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



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

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



138
139
140
141
142
143
# File 'lib/vimamsa/buffer_list.rb', line 138

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



216
217
218
219
220
221
222
223
224
# File 'lib/vimamsa/buffer_list.rb', line 216

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



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

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



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

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

#get_last_dirObject



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

def get_last_dir
  return @last_dir
end

#get_recent_buffersObject



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

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



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

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



129
130
131
132
133
134
135
136
# File 'lib/vimamsa/buffer_list.rb', line 129

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



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/vimamsa/buffer_list.rb', line 77

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

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

  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

#switchObject



42
43
44
45
46
47
48
49
# File 'lib/vimamsa/buffer_list.rb', line 42

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



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

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