nerdtree

A tree explorer plugin for vim.
Index Commits Files Refs
commit a0f3bf3ed6405453be39906ad026cd5aee6ca0e3
parent 68572ef46143c5c7d420a6ec86f1ec4595765ce5
Author: Martin Rubli <github@rubli.info>
Date:   Fri,  2 Feb 2018 20:10:34 +0800

Focus previous window when closing NERDTree

Previously closing NERDTree while two windows were showing the same
buffer would focus the first window, which was not necessarily the
previously active one.

Instead of obtaining the buffer ID of the previous buffer and
mapping that to the window ID (which is a 1:n mapping) we obtain the
unique window ID and focus the right window after closing NERDTree.

win_getid() and win_gotoid() are available from VIM 7.4.1557 but the
old behavior is used as a fallback if the two functions are not
available.

Diffstat:
Mlib/nerdtree/nerdtree.vim | 15++++++++++++---
1 file changed, 12 insertions(+), 3 deletions(-)
diff --git a/lib/nerdtree/nerdtree.vim b/lib/nerdtree/nerdtree.vim
@@ -38,17 +38,26 @@ function! s:NERDTree.Close()
     endif
 
     if winnr("$") != 1
+        " Use the window ID to identify the currently active window or fall
+        " back on the buffer ID if win_getid/win_gotoid are not available, in
+        " which case we'll focus an arbitrary window showing the buffer.
+        let l:useWinId = exists('*win_getid') && exists('*win_gotoid')
+
         if winnr() == s:NERDTree.GetWinNum()
             call nerdtree#exec("wincmd p")
-            let bufnr = bufnr("")
+            let l:activeBufOrWin = l:useWinId ? win_getid() : bufnr("")
             call nerdtree#exec("wincmd p")
         else
-            let bufnr = bufnr("")
+            let l:activeBufOrWin = l:useWinId ? win_getid() : bufnr("")
         endif
 
         call nerdtree#exec(s:NERDTree.GetWinNum() . " wincmd w")
         close
-        call nerdtree#exec(bufwinnr(bufnr) . " wincmd w")
+        if l:useWinId
+            call nerdtree#exec("call win_gotoid(" . l:activeBufOrWin . ")")
+        else
+            call nerdtree#exec(bufwinnr(l:activeBufOrWin) . " wincmd w")
+        endif
     else
         close
     endif