zefei / vim-wintabs

Modern buffer manager for Vim
MIT License
325 stars 25 forks source link

Cannot WintabsClose the final modified buffer in a window. #24

Closed jordwalke closed 6 years ago

jordwalke commented 6 years ago

If you open Vim, and modify the default non-named buffer. It will show up as a wintab, but then if you try to WintabsClose it, you will get an error message:

screen shot 2018-02-03 at 10 50 41 pm
set hidden
let g:tabSystem = 'wintabs'
let g:wintabs_autoclose=1
let g:wintabs_autoclose_vim=1
let g:wintabs_switchbuf='useopen,usetab'
let g:wintabs_ui_modified*
let g:wintabs_ui_active_higroup = 'TabLineSel'

Note that let g:wintabs_autoclose=2 does work, but let g:wintabs_autoclose=1 does not.

zefei commented 6 years ago

Thanks for flagging this! Will fix it soonish.

jordwalke commented 6 years ago

Related, you cannot even close any non-named buffer without raising this error (try :enew). One helpful datapoint: If before deleting the non-named buffer, you set :setlocal bufhidden=hide then the error doesn't happen. I think it's because with bufhidden=delete (or even the default value of empty string) the buffer is deleted twice - once by default and once by wintabs. But bufhidden=hide it doesn't fully delete the buffer when wintabs tries to remove it after modifying the unnamed buffer.

jordwalke commented 6 years ago

And with the following config:

let g:wintabs_display='statusline'
set hidden
let g:wintabs_autoclose=2
let g:wintabs_autoclose_vim=1
let g:wintabs_autoclose_vimtab=1
let g:wintabs_switchbuf='useopen,usetab'
let g:wintabs_ui_active_higroup = 'TabLineSel'

As mentioned in the initial description closing the first/only non-named modified buffer works.

But then with that new config, closing the first/only named buffer does not work if you have modifications.

In this case, I think the problem is this function which is called before bdelete on the unnamed buffer.

function! s:switch_tab(n, confirm)
  " do nothing if n >= size
  if a:n >= len(w:wintabs_buflist)
    return
  endif

  " set nohidden to trigger confirm behavior
  let hidden = &hidden
  let &hidden = 0

  if a:n < 0
    execute a:confirm ? 'silent! confirm enew' : 'enew!'
  else
    let buffer = w:wintabs_buflist[a:n]
    execute a:confirm ? 'silent! confirm buffer '.buffer : 'buffer! '.buffer
  endif

  " restore hidden
  let &hidden = hidden
endfunction

which is called before "purge"ing the unnamed buffer via bdelete. The silent! confirm buffer switches to another tab, and since nohidden is set on the unnamed buffer it is deleted. Then when you later try to bdelete it it's already gone.

This is actually hard to get right because you rely on the tab switching function to trigger the confirmation if the buffer does happen to implicitly hidden when switching away from it and if it's not displayed in any other window. I think you might just be able to get away with not ever purging. This seems to restore proper functionality from what I can tell by testing. I think what you can do after any interaction, is to examine what the end result was - which buffers ended up being completely unloaded wintabs, yet remained listed by vim - and then only purge those, instead of trying to predict which buffer number must be purged based on the interaction at hand. You can instead inspect what Vim sees at the end of each interaction and purge according to that.

Simply commenting out the bdelete line works well - the UI is always up to date, and there's no errors. The only downside is that there's some useless buffers listed in buffers. Seems better than having errors though.

zefei commented 6 years ago

This should be fixed now. Thank you for the analysis @jordwalke, it helped me resolve another issue. Some of the edge cases we see here are indeed caused by wintab's reliance on the interaction between "confirm" and "hidden".

Due to the recent vim changes on "silent!"+"confirm", I'm not very sure if I'm fixing the right thing (I removed some "silent!" to bubble up exceptions). Please test out the new version and let me know if things still behave correctly and consistently.