Friday, July 13, 2012

Re: Cleaning up the buffer list

On Jul 12, 10:54 pm, Chris Jones <cjns1...@gmail.com> wrote:
> On Thu, Jul 12, 2012 at 01:58:24AM EDT, Christian Brabandt wrote:
> > On Thu, July 12, 2012 01:38, Chris Jones wrote:
>
> [..]
>
> > > Has anyone looked into buffer list manipulation before and could
> > > advise on a different approach & suggest how I might be able to come
> > > up with something a bit less clunky..?
> > Somethins like this, probably:
> > fu! s:MyBufferClean()
> >     let _t = tabpagenr()
> >     let blist=range(1,bufnr('$'))
> >     tabdo call filter(blist, 'bufwinnr(v:val)==-1')
> >     for bufnr in blist
> >    exe 'bw' bufnr
> >     endfor
> >     exe _t "tabnext"
> > endfu
> > command! MyBufferClean :call <sid>MyBufferClean()
>
> Yes, bufwinnr()'s  '-1' return value was just about the only thing that
> came close. I believe the above works but it's limited to hidden vs.
> active.. does not check if a buffer was modified..
>
> I guess I was looking for something that would basically return
> something a bit like a C struct, with maybe the buffer number, the file
> name and then the indicators.


I think used filter() the wrong way in my first reply. Better:

let bnrs = range(1,bufnr('$'))
call filter(bnrs, 'bufexists(v:val)')
for bnr in bnrs
if !bufloaded(bnr) && !buflisted(bnr)
exe 'bw '.bnr
endif
endfor

After rereading your first message I realized you probably want to get
rid of 'hidden' buffers. The function that Christian Brabandt
suggested would do it. I would do it like this (untested):

" Wipe out buffers that are not displayed in any window
" (including hidden buffers) and are not modified.
let bnrs = range(1,bufnr('$'))
call filter(bnrs, 'bufexists(v:val)')
let tablist = []
for i in range(1,tabpagenr('$'))
call extend(tablist, tabpagebuflist(i))
endfor
for bnr in bnrs
if index(tablist, bnr) < 0 && !getbufvar(bnr, '&mod')
exe 'bw '.bnr
endif
endfor

You can add check for buflisted() if for some reason you want to keep
unlisted buffers.

HTH,
Vlad

--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

No comments: