Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function! SortByCurrent(b1, b2)
- return a:b1['current'] == a:b2['current'] ? 0 : a:b1['current'] < a:b2['current'] ? 1 : -1
- endfunction
- function! SortByAlternate(b1, b2)
- return a:b1['alternate'] == a:b2['alternate'] ? 0 : a:b1['alternate'] < a:b2['alternate'] ? 1 : -1
- endfunction
- function! GetBufferList()
- redir => bufliststr
- silent! ls
- redir END
- let bufferlist = split(bufliststr, '\n')
- " For loop as requested.
- "for i in range(0, len(bufferlist) - 1)
- " let bufferlist[i] = substitute(bufferlist[i], '^\s*\(\d\+\)\(\s*[-u%#ah=+x ]*\)\s\+\(\".\{-}\"\)\s\+line\s\+\(\d\+\)\s*$', '\1,\2,\4,\3','g')
- "endfor
- "I'm stubborn, so I used double single quotes to avoid the backslashitis,
- "but feel free to use the for loop :p
- call map(bufferlist,
- \'substitute(v:val,'.
- \'''\m\C^\s*\(\d\+\)\(\s*[-u%#ah=+x ]*\)\s\+"\(.\{-}\)"\s\+line\s\+\(\d\+\)\s*$'','.
- \'''\1,\2,\4,\3'',''g'')')
- " Split on commas (I know)
- call map(bufferlist, 'split(v:val, ",")')
- " Restore file names with comma(s)
- call map(bufferlist, 'add(v:val[0:2], join(v:val[3:-1], ","))')
- " Added entries for the other flags.
- " Empty names give an empty string instead of "[No Name]".
- call map(bufferlist,
- \ '{"number": v:val[0],'
- \.'"line": v:val[2],'
- \.'"name": (v:val[3] =~ ''\[.\{-}\]'' ? bufname(v:val[0]) : v:val[3]),'
- \.'"listed": v:val[1] !~ "u",'
- \.'"current": v:val[1] =~ "%",'
- \.'"alternate": v:val[1] =~ "#",'
- \.'"active": v:val[1] =~ "a",'
- \.'"hidden": v:val[1] =~ "h",'
- \.'"modifiable": v:val[1] !~ "-",'
- \.'"readonly": v:val[1] =~ "=",'
- \.'"modified": v:val[1] =~ "+",'
- \.'"read_error": v:val[1] =~ "x"}')
- " Is filtering faster than sorting?
- "let current = sort(copy(bufferlist), 'SortByCurrent')[0]['number']
- "let alternate = sort(copy(bufferlist), 'SortByAlternate')[0]['number']
- let current = filter(copy(bufferlist), 'v:val["current"] == 1')[0].number
- let alternate_l = filter(copy(bufferlist), 'v:val["alternate"] == 1')
- let alternate = len(alternate_l) > 0 ? alternate_l[0].number : ''
- return {'list': bufferlist, 'current': current, 'alternate': alternate}
- endfunction
- echo GetBufferList()
Advertisement
Add Comment
Please, Sign In to add comment