bairui

BufferList

Sep 24th, 2011
176
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VIM 2.74 KB | None | 0 0
  1. " BufferList object
  2. " ARB
  3. " version 0.4
  4.  
  5. function! BufferList()
  6.   let list = {}
  7.   let list.list = {}
  8.   let list.current = 0
  9.   let list.alternate = 0
  10.  
  11.   " public interface
  12.  
  13.   func list.update() dict
  14.     redir => bufliststr
  15.     silent! ls
  16.     redir END
  17.  
  18.     let bufferlist = split(bufliststr, '\n')
  19.     " Reorder and clean up a bit the output.
  20.     for i in range(0, len(bufferlist) - 1)
  21.       let bufferlist[i] = substitute(bufferlist[i], '^\s*\(\d\+\)\(\s*[-u%#ah=+x ]*\)\s\+\"\(.\{-}\)\"\s\+line\s\+\(\d\+\)\s*$', '\1,\2,\4,\3','')
  22.     endfor
  23.  
  24.     " Split on commas.
  25.     call map(bufferlist, 'split(v:val, ",")')
  26.     " Restore file names with comma(s)
  27.     call map(bufferlist, 'add(v:val[0:2], join(v:val[3:-1], ","))')
  28.     " Empty names give an empty string instead of "[No Name]".
  29.     call map(bufferlist,
  30.           \ '{"number": v:val[0],'
  31.           \.'"line": v:val[2],'
  32.           \.'"name": (v:val[3]      =~ ''\[.\{-}\]'' ? bufname(v:val[0]) : v:val[3]),'
  33.           \.'"listed": v:val[1]     !~ "u",'
  34.           \.'"current": v:val[1]    =~ "%",'
  35.           \.'"alternate": v:val[1]  =~ "#",'
  36.           \.'"active": v:val[1]     =~ "a",'
  37.           \.'"hidden": v:val[1]     =~ "h",'
  38.           \.'"modifiable": v:val[1] !~ "-",'
  39.           \.'"readonly": v:val[1]   =~ "=",'
  40.           \.'"modified": v:val[1]   =~ "+",'
  41.           \.'"read_error": v:val[1] =~ "x"}')
  42.  
  43.     let self.list = bufferlist
  44.     let self.current = sort(copy(bufferlist), 'SortByCurrent')[0]['number']
  45.     let alternate_l = filter(copy(bufferlist), 'v:val["alternate"] == 1')
  46.     let self.alternate = len(alternate_l) > 0 ? alternate_l[0].number : 0
  47.   endfun
  48.  
  49.   func list.to_s() dict
  50.     let str = ''
  51.     for bfr in self.list
  52.       let str .= printf("%3d %s%s \"%s\" line %d\n",
  53.             \ bfr['number'], self.buffer_type(bfr),
  54.             \ self.buffer_status(bfr), bfr['name'], bfr['line'])
  55.     endfor
  56.     return str
  57.   endfunc
  58.  
  59.   " Private functions - don't need 'dict' modifier
  60.  
  61.   func list.buffer_status(b)
  62.     return a:b['active'] == 1 ? 'a' : a:b['hidden'] == 1 ? 'h' : '!'
  63.   endfunc
  64.   func list.buffer_type(b)
  65.     return a:b['current'] == 1 ? '%' : a:b['alternate'] == 1 ? '#' : ' '
  66.   endfunc
  67.  
  68.   func list.SortByCurrent(b1, b2)
  69.     return a:b1['current'] == a:b2['current'] ? 0 : a:b1['current'] < a:b2['current'] ? 1 : -1
  70.   endfunc
  71.  
  72.   func list.SortByAlternate(b1, b2)
  73.     return a:b1['alternate'] == a:b2['alternate'] ? 0 : a:b1['alternate'] < a:b2['alternate'] ? 1 : -1
  74.   endfunc
  75.  
  76.   " constructor
  77.  
  78.   call list.update()
  79.   return list
  80. endfunction
  81.  
  82. let bl = BufferList()
  83. call bl.update()      " not necessary here, just showing it's callable
  84. echo bl.list
  85. echo bl.current
  86. echo bl.alternate
  87. echo bl.to_s()
  88.  
  89. " vim: et sw=2 sws=2 ft=vim
Advertisement
Add Comment
Please, Sign In to add comment