diff --git a/nerdtree_plugin/vim-nerdtree-tabs.vim b/nerdtree_plugin/vim-nerdtree-tabs.vim index 6c8c916..1970dbb 100644 --- a/nerdtree_plugin/vim-nerdtree-tabs.vim +++ b/nerdtree_plugin/vim-nerdtree-tabs.vim @@ -318,6 +318,12 @@ fun! s:NextNormalWindow() continue endif + " skip current normal window + if l:i == winnr() + let l:i = l:i + 1 + continue + endif + return l:i endwhile return -1 @@ -329,8 +335,27 @@ endfun " Close all open buffers on entering a window if the only " buffer that's left is the NERDTree buffer fun! s:CloseIfOnlyNerdTreeLeft() - if exists("t:NERDTreeBufName") && bufwinnr(t:NERDTreeBufName) != -1 && winnr("$") == 1 - q + " Only take action when we are in NERDTree window, and no more window + " with normal buffer open. + if exists("t:NERDTreeBufName") && bufwinnr(t:NERDTreeBufName) == winnr() && s:NextNormalWindow() == -1 + if tabpagenr('$') == 1 + " Before quitting Vim, delete the buffer so that the '0 mark + " is correctly set to the previous buffer. This avoids NERDTree + " buffer being the last displayed buffer if Vim fails to quit + " due to the last file in the argument list has not been edited + " yet. Also disable autocmd on this command to avoid unnecessary + " autocmd nesting. + if winnr('$') == 1 + noautocmd bdelete + endif + " Quit as usual, we have autocmd nesting open, so this command + " will trigger other plugin window's quiting behavior. If we are + " quiting Vim, our current buffer is successfully reset to the + " last file buffer, no need to warry about failure. + quit + else + close + endif endif endfun @@ -441,7 +466,7 @@ fun! s:LoadPlugin() autocmd VimEnter * call VimEnterHandler() autocmd TabEnter * call TabEnterHandler() autocmd TabLeave * call TabLeaveHandler() - autocmd WinEnter * call WinEnterHandler() + autocmd WinEnter * nested call WinEnterHandler() autocmd WinLeave * call WinLeaveHandler() augroup END