Class: BufferList

Inherits:
Array
  • Object
show all
Defined in:
lib/vimamsa/buffer_list.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#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

Instance Method Details

#<<(_buf) ⇒ Object



27
28
29
30
31
32
33
34
35
36
# File 'lib/vimamsa/buffer_list.rb', line 27

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

#add_current_buf_to_historyObject



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

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

#close_all_buffersObject



194
195
196
197
198
199
200
201
202
203
204
205
# File 'lib/vimamsa/buffer_list.rb', line 194

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



164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
# File 'lib/vimamsa/buffer_list.rb', line 164

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



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

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



151
152
153
154
155
156
157
158
159
160
161
# File 'lib/vimamsa/buffer_list.rb', line 151

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



207
208
209
210
211
212
213
214
215
216
# File 'lib/vimamsa/buffer_list.rb', line 207

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



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

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



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

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



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

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



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

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

#get_last_dirObject



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/vimamsa/buffer_list.rb', line 99

def get_last_dir
  lastdir = nil
  if $buffer.fname
    lastdir = File.dirname($buffer.fname)
  else
    for bufid in $buffer_history.reverse[1..-1]
      bf = $buffers[bufid]
      debug "FNAME:#{bf.fname}"
      if bf.fname
        lastdir = File.dirname(bf.fname)
        break
      end
    end
  end
  lastdir = File.expand_path(".") if lastdir.nil?
  return lastdir
end

#get_recent_buffersObject



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

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



123
124
125
126
127
128
129
130
# File 'lib/vimamsa/buffer_list.rb', line 123

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



132
133
134
135
136
137
138
139
# File 'lib/vimamsa/buffer_list.rb', line 132

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



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/vimamsa/buffer_list.rb', line 74

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
  $buffer = self[buffer_i]
  return if !$buffer
  @current_buf = buffer_i
  debug "SWITCH BUF2. bufsize:#{self.size}, curbuf: #{@current_buf}"
  fpath = $buffer.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, $buffer)
  $buffer.set_active

  gui_set_current_buffer($buffer.id)
  gui_set_window_title($buffer.title,$buffer.subtitle)   

  # hpt_scan_images() if $debug # experimental
end

#switchObject



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

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



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

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