Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- " BufferList object
- " ARB
- " version 0.4
- function! BufferList()
- let list = {}
- let list.list = {}
- let list.current = 0
- let list.alternate = 0
- " public interface
- func list.update() dict
- redir => bufliststr
- silent! ls
- redir END
- let bufferlist = split(bufliststr, '\n')
- " Reorder and clean up a bit the output.
- for i in range(0, len(bufferlist) - 1)
- let bufferlist[i] = substitute(bufferlist[i], '^\s*\(\d\+\)\(\s*[-u%#ah=+x ]*\)\s\+\"\(.\{-}\)\"\s\+line\s\+\(\d\+\)\s*$', '\1,\2,\4,\3','')
- endfor
- " Split on commas.
- call map(bufferlist, 'split(v:val, ",")')
- " Restore file names with comma(s)
- call map(bufferlist, 'add(v:val[0:2], join(v:val[3:-1], ","))')
- " Empty names give an empty string instead of "[No Name]".
- call map(bufferlist,
- \ '{"number": v:val[0],'
- \.'"line": v:val[2],'
- \.'"name": (v:val[3] =~ ''\[.\{-}\]'' ? bufname(v:val[0]) : v:val[3]),'
- \.'"listed": v:val[1] !~ "u",'
- \.'"current": v:val[1] =~ "%",'
- \.'"alternate": v:val[1] =~ "#",'
- \.'"active": v:val[1] =~ "a",'
- \.'"hidden": v:val[1] =~ "h",'
- \.'"modifiable": v:val[1] !~ "-",'
- \.'"readonly": v:val[1] =~ "=",'
- \.'"modified": v:val[1] =~ "+",'
- \.'"read_error": v:val[1] =~ "x"}')
- let self.list = bufferlist
- let self.current = sort(copy(bufferlist), 'SortByCurrent')[0]['number']
- let alternate_l = filter(copy(bufferlist), 'v:val["alternate"] == 1')
- let self.alternate = len(alternate_l) > 0 ? alternate_l[0].number : 0
- endfun
- func list.to_s() dict
- let str = ''
- for bfr in self.list
- let str .= printf("%3d %s%s \"%s\" line %d\n",
- \ bfr['number'], self.buffer_type(bfr),
- \ self.buffer_status(bfr), bfr['name'], bfr['line'])
- endfor
- return str
- endfunc
- " Private functions - don't need 'dict' modifier
- func list.buffer_status(b)
- return a:b['active'] == 1 ? 'a' : a:b['hidden'] == 1 ? 'h' : '!'
- endfunc
- func list.buffer_type(b)
- return a:b['current'] == 1 ? '%' : a:b['alternate'] == 1 ? '#' : ' '
- endfunc
- func list.SortByCurrent(b1, b2)
- return a:b1['current'] == a:b2['current'] ? 0 : a:b1['current'] < a:b2['current'] ? 1 : -1
- endfunc
- func list.SortByAlternate(b1, b2)
- return a:b1['alternate'] == a:b2['alternate'] ? 0 : a:b1['alternate'] < a:b2['alternate'] ? 1 : -1
- endfunc
- " constructor
- call list.update()
- return list
- endfunction
- let bl = BufferList()
- call bl.update() " not necessary here, just showing it's callable
- echo bl.list
- echo bl.current
- echo bl.alternate
- echo bl.to_s()
- " vim: et sw=2 sws=2 ft=vim
Advertisement
Add Comment
Please, Sign In to add comment