bairui

BufferList

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