bairui

BufferList

Sep 24th, 2011
177
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VIM 2.83 KB | None | 0 0
  1. " BufferList object
  2. " ARB
  3. " version 0.5
  4.  
  5. function! BufferList()
  6.   let bl = {}
  7.   let bl.buffers = {}
  8.   let bl.current = 0
  9.   let bl.alternate = 0
  10.  
  11.   " public interface
  12.  
  13.   func bl.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.buffers = bufferlist
  44.     let current_l = filter(copy(bufferlist), 'v:val["current"] == 1')
  45.     let alternate_l = filter(copy(bufferlist), 'v:val["alternate"] == 1')
  46.     let self.current = len(current_l) > 0 ?current_l[0].number : 0
  47.     let self.alternate = len(alternate_l) > 0 ? alternate_l[0].number : 0
  48.   endfun
  49.  
  50.   func bl.to_s() dict
  51.     let str = ''
  52.     for bfr in self.buffers
  53.       let str .= printf("%3d %s%s \"%s\" line %d\n",
  54.             \ bfr['number'], self.buffer_type(bfr),
  55.             \ self.buffer_status(bfr), bfr['name'], bfr['line'])
  56.     endfor
  57.     return str
  58.   endfunc
  59.  
  60.   " Private functions - don't need 'dict' modifier
  61.  
  62.   func bl.buffer_status(b)
  63.     return a:b['active'] == 1 ? 'a' : a:b['hidden'] == 1 ? 'h' : '!'
  64.   endfunc
  65.  
  66.   func bl.buffer_type(b)
  67.     return a:b['current'] == 1 ? '%' : a:b['alternate'] == 1 ? '#' : ' '
  68.   endfunc
  69.  
  70.   func bl.SortByCurrent(b1, b2)
  71.     return a:b1['current'] == a:b2['current'] ? 0 : a:b1['current'] < a:b2['current'] ? 1 : -1
  72.   endfunc
  73.  
  74.   func bl.SortByAlternate(b1, b2)
  75.     return a:b1['alternate'] == a:b2['alternate'] ? 0 : a:b1['alternate'] < a:b2['alternate'] ? 1 : -1
  76.   endfunc
  77.  
  78.   " constructor
  79.  
  80.   call bl.update()
  81.   return bl
  82. endfunction
  83.  
  84. let bl = BufferList()
  85. call bl.update()      " not necessary here, just showing it's callable
  86. "echo bl.buffers
  87. echo "Current buffer   : " . bl.current
  88. echo "Alternate buffer : " . bl.alternate
  89. echo bl.to_s()
  90.  
  91. " vim: et sw=2 ft=vim
Advertisement
Add Comment
Please, Sign In to add comment