Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- " BufferList object
- " ARB
- " version 0.3
- 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 = map(
- \ map(split(bufliststr, '\n'), 'split(v:val, "\\s\\+")'),
- \ '{"number": v:val[0],'
- \ . '"line": v:val[4],'
- \ . '"name": v:val[2],'
- \ . '"current": v:val[1] =~ "%",'
- \ . '"alternate": v:val[1] =~ "#",'
- \ . '"active": v:val[1] =~ "a",'
- \ . '"hidden": v:val[1] =~ "h"}')
- let self.list = bufferlist
- let self.current = sort(copy(bufferlist), 'SortByCurrent')[0]['number']
- let self.alternate = sort(copy(bufferlist), 'SortByAlternate')[0]['number']
- endfun
- func list.to_s() dict
- let str = ''
- for bfr in self.list
- let str .= printf("%3d %c%c \"%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()
Advertisement
Add Comment
Please, Sign In to add comment