zhmz90 / vim

Automatically exported from code.google.com/p/vim
0 stars 0 forks source link

can not wipe buffer on bufhidden #177

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
What steps will reproduce the problem?
1) set hidden.
2) 

set nocompatible
set hidden
syntax on

autocmd BufHidden * call OnBufHidden(expand("<afile>"))

function! OnBufHidden(buffer)

    let br = str2nr(expand("<abuf>"))

    if br < 0
        return
    endif

    if a:buffer == ""
        echoerr "clsing:".br
        execute "bw! ".br
    endif

endfunction

What is the expected output? What do you see instead?
buffer can be wiped out.
nothing happen.

What version of the product are you using? On what operating system?

v7.4

Please provide any additional information below.

Original issue reported on code.google.com by liaoming...@gmail.com on 6 Nov 2013 at 7:33

GoogleCodeExporter commented 9 years ago
As noted by ZyX, my reply on the mailing list wasn't posted here. Sorry for the 
double post on vim_dev, I'm posing using the issue tracker now.

On Wednesday, November 6, 2013 1:33:33 AM UTC-6, v...@googlecode.com wrote:
> 
> autocmd BufHidden * call OnBufHidden(expand("<afile>"))
> 
> function! OnBufHidden(buffer)
> 
> [snip]

> 
>      if a:buffer == ""
>          echoerr "clsing:".br
>          execute "bw! ".br
>      endif
> endfunction
> 
> 
> 
> What is the expected output? What do you see instead?
> 
> buffer can be wiped out.
> nothing happen.
>
This function only ever wipes out a buffer if the buffer name is empty.

First of all...why?

Secondly, what is the buffer you are wiping out? Does it actually have
no name?

If so, does your echoerr message appear?

How did you determine that the buffer isn't being wiped?

Original comment by fritzoph...@gmail.com on 6 Nov 2013 at 3:08

GoogleCodeExporter commented 9 years ago
I do not see you posting an exact sequence of commands you used to test this 
behavior. When testing your code with

    " # vim onbufhiddenwipe.vim
    " " paste your code
    " :write
    " :source %
    enew
    bnext

I see it behaving exactly as expected. But here is an educated guess on what 
you mean: when you edit a file and current buffer is empty, unmodified and has 
empty name :e uses this existing buffer to edit text in it. As it is reused it 
is not hidden and BufHidden does not launch. It is documented somewhere, but I 
do not remember where exactly.

Other issues:

1. passing <afile> as an argument and using <abuf> in a function is 
inconsistent. I would expect you either pass *both* <afile> and <abuf> in 
arguments or *none*, expanding them both in function instead.
2. never use echoerr. If you need vim to abort processing use :throw. If you 
need vim not to abort processing use :echom. Whenever you write :echoerr like 
this (*not* inside :try) you no longer know whether or not it will abort 
execution.

    As an example consider the following case:

    1. You have an empty buffer at the start
    2. And some plugin that switches to its buffer like this
        try
            let saved_bufnr=bufnr('%')
            execute 'buffer' plugin_bnr
            " …
        finally
            if bufexists(saved_bufnr)
                execute 'buffer' saved_bufnr
            endif
        endtry
    What do you think this will result in? This will result in plugin doing nothing (it stops on `execute 'buffer' plugin_bnr` due to echoerr) and user still seeing empty buffer (because it stops on echoerr without executing next line). I.e. neither example plugin, nor your code had done its job: and all this was unexpected and due to :echoerr!
3. There is `empty()` to check for emptiness.
4. I would highly suggest never use `==` for string comparison because its 
meaning changes when &ignorecase option changes. It does not matter when you 
compare with an empty string, but it is usually a habit to use one type of 
string comparison everywhere. I normally use `is#` for the job, `==#` should 
also work.

Original comment by zyx....@gmail.com on 6 Nov 2013 at 3:30

GoogleCodeExporter commented 9 years ago
thank you for your reply.

here is how I reproduce it.

1) launch vim.
2) edit a file 
3) tabnew opens another tab.
4) try to close the new tab by ":x".

when I close the new open tab, there is always a empty buffer hidden.
so I use the autocmd to help clean it up.

I use echoerr just for test, the result remains the same even when I remove the 
"echoerr".

In my test, the result is:

1) I can always find the hidden & empty buffer.
2) but "bw! ".br did not work.
3) I confirm it by using ":ls" to list all the buffers, and I found that the 
empty buffer is hidden.

Hi zyx, thank you so much for your suggestions. I am really new on the vim 
scripting.

Original comment by liaoming...@gmail.com on 6 Nov 2013 at 6:18

GoogleCodeExporter commented 9 years ago

Original comment by chrisbr...@googlemail.com on 30 Sep 2014 at 9:08