Properly handle closing the last file window, closes #345

When a window that is displaying a normal file gets closed with a :quit,
:bdelete or other command, Tagbar may end up as the only visible window
left, which isn't very useful. Previously Tagbar would just quit in such
a situation (unless there was more than one tab), but that is rather
non-intuitive.

The change in this commit introduces a mechanism that should hopefully
handle such a case properly based on which command was executed, and do
exactly what a user would expect:

- In the ':quit' case, it will close the current tabpage including
  Tagbar unless it was the only tabpage, in which case it will quit Vim.
- In the ':bdelete/:bwipeout' case, it will delete the Tagbar buffer and
  then reopen Tagbar once the buffer that Vim switched to is being
  displayed.
This commit is contained in:
Jan Larres 2016-10-09 21:01:24 +13:00
parent 497e963bc4
commit e4cfe8add5

View File

@ -66,6 +66,7 @@ let s:nearby_disabled = 0
let s:paused = 0
let s:pwin_by_tagbar = 0
let s:buffer_seqno = 0
let s:vim_quitting = 0
let s:window_expanded = 0
let s:expand_bufnr = -1
@ -1039,10 +1040,15 @@ function! s:CreateAutocommands() abort
autocmd CursorMoved __Tagbar__.* nested call s:ShowInPreviewWin()
endif
autocmd QuitPre * let s:vim_quitting = 1
autocmd WinEnter * nested call s:QuitIfOnlyWindow()
autocmd WinEnter * if bufwinnr(s:TagbarBufName()) == -1 |
\ call s:ShrinkIfExpanded() |
\ endif
autocmd BufWinEnter * if exists('s:reopen_window') |
\ unlet s:reopen_window |
\ call s:OpenWindow('') |
\ endif
autocmd BufWritePost * call
\ s:AutoUpdate(fnamemodify(expand('<afile>'), ':p'), 1)
@ -4028,10 +4034,13 @@ endfunction
" s:QuitIfOnlyWindow() {{{2
function! s:QuitIfOnlyWindow() abort
let tagbarwinnr = bufwinnr(s:TagbarBufName())
if tagbarwinnr == -1
if tagbarwinnr == -1 || exists('s:reopen_window')
return
endif
let vim_quitting = s:vim_quitting
let s:vim_quitting = 0
let curwinnr = winnr()
let prevwinnr = winnr('#') == 0 ? curwinnr : winnr('#')
call s:goto_win(tagbarwinnr, 1)
@ -4039,7 +4048,7 @@ function! s:QuitIfOnlyWindow() abort
" Check if there is more than one window
if s:NextNormalWindow() == -1
" Check if there is more than one tab page
if tabpagenr('$') == 1
if tabpagenr('$') == 1 && vim_quitting
" Before quitting Vim, delete the tagbar buffer so that
" the '0 mark is correctly set to the previous buffer.
" Also disable autocmd on this command to avoid unnecessary
@ -4048,6 +4057,15 @@ function! s:QuitIfOnlyWindow() abort
noautocmd bdelete
endif
quit
elseif tabpagenr('$') == 1
" The last normal window closed due to a :bdelete/:bwipeout.
" In order to get a normal file window back delete the Tagbar
" buffer that we're currently displaying and reset the Tagbar-set
" window options, and then re-open the Tagbar window once Vim has
" switched to the buffer it has chosen to display next.
noautocmd bdelete
set winfixwidth<
let s:reopen_window = 1
elseif exists('t:tagbar_buf_name')
close
endif