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 would just wipe out all unloaded buffers that are also unlisted:

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

By definition, if a buffer is unloaded, it is not displayed in any
window and is not modified.
An unloaded buffer can still be listed, which could mean the user
might need it in the future, although this is unlikely and I do not
know any actual examples when this happens.

Also, if the goal to get buffers that are displayed in windows, it's
better to use tabpagebuflist() as shown in the help.

Regards,
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: