Raimondi

Untitled

Sep 24th, 2011
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VIM 2.25 KB | None | 0 0
  1.  
  2. function! SortByCurrent(b1, b2)
  3.   return a:b1['current'] == a:b2['current'] ? 0 : a:b1['current'] < a:b2['current'] ? 1 : -1
  4. endfunction
  5.  
  6. function! SortByAlternate(b1, b2)
  7.   return a:b1['alternate'] == a:b2['alternate'] ? 0 : a:b1['alternate'] < a:b2['alternate'] ? 1 : -1
  8. endfunction
  9.  
  10. function! GetBufferList()
  11.   redir => bufliststr
  12.   silent! ls
  13.   redir END
  14.  
  15.   let bufferlist = split(bufliststr, '\n')
  16.   " For loop as requested.
  17.   "for i in range(0, len(bufferlist) - 1)
  18.   "  let bufferlist[i] = substitute(bufferlist[i], '^\s*\(\d\+\)\(\s*[-u%#ah=+x ]*\)\s\+\(\".\{-}\"\)\s\+line\s\+\(\d\+\)\s*$', '\1,\2,\4,\3','g')
  19.  "endfor
  20.  
  21.   "I'm stubborn, so I used double single quotes to avoid the backslashitis,
  22.   "but feel free to use the for loop :p
  23.   call map(bufferlist,
  24.         \'substitute(v:val,'.
  25.         \'''\m\C^\s*\(\d\+\)\(\s*[-u%#ah=+x ]*\)\s\+"\(.\{-}\)"\s\+line\s\+\(\d\+\)\s*$'','.
  26.         \'''\1,\2,\4,\3'',''g'')')
  27.   " Split on commas (I know)
  28.   call map(bufferlist, 'split(v:val, ",")')
  29.   " Restore file names with comma(s)
  30.   call map(bufferlist, 'add(v:val[0:2], join(v:val[3:-1], ","))')
  31.   " Added entries for the other flags.
  32.   " Empty names give an empty string instead of "[No Name]".
  33.   call map(bufferlist,
  34.         \ '{"number": v:val[0],'
  35.         \.'"line": v:val[2],'
  36.         \.'"name": (v:val[3] =~ ''\[.\{-}\]'' ? bufname(v:val[0]) : v:val[3]),'
  37.         \.'"listed": v:val[1] !~ "u",'
  38.         \.'"current": v:val[1] =~ "%",'
  39.         \.'"alternate": v:val[1] =~ "#",'
  40.         \.'"active": v:val[1] =~ "a",'
  41.         \.'"hidden": v:val[1] =~ "h",'
  42.         \.'"modifiable": v:val[1] !~ "-",'
  43.         \.'"readonly": v:val[1] =~ "=",'
  44.         \.'"modified": v:val[1] =~ "+",'
  45.         \.'"read_error": v:val[1] =~ "x"}')
  46.  
  47.   " Is filtering faster than sorting?
  48.   "let current = sort(copy(bufferlist), 'SortByCurrent')[0]['number']
  49.   "let alternate = sort(copy(bufferlist), 'SortByAlternate')[0]['number']
  50.   let current = filter(copy(bufferlist), 'v:val["current"] == 1')[0].number
  51.   let alternate_l = filter(copy(bufferlist), 'v:val["alternate"] == 1')
  52.   let alternate = len(alternate_l) > 0 ? alternate_l[0].number : ''
  53.   return {'list': bufferlist, 'current': current, 'alternate': alternate}
  54. endfunction
  55.  
  56. echo GetBufferList()
Advertisement
Add Comment
Please, Sign In to add comment