Skip to content
This repository has been archived by the owner on Apr 5, 2024. It is now read-only.

Feature/refactor quit if only left #40

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 28 additions & 3 deletions nerdtree_plugin/vim-nerdtree-tabs.vim
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

Expand Down Expand Up @@ -441,7 +466,7 @@ fun! s:LoadPlugin()
autocmd VimEnter * call <SID>VimEnterHandler()
autocmd TabEnter * call <SID>TabEnterHandler()
autocmd TabLeave * call <SID>TabLeaveHandler()
autocmd WinEnter * call <SID>WinEnterHandler()
autocmd WinEnter * nested call <SID>WinEnterHandler()
autocmd WinLeave * call <SID>WinLeaveHandler()
augroup END

Expand Down