nerdtree

A tree explorer plugin for vim.
Index Commits Files Refs
commit 6b687977d955b3f0cfb26731652f98823b8907d8
parent 0a16b24268cb0b597fd8ac815d0baf05d54ff560
Author: ZeusTheTrueGod <l-2732@yandex.ru>
Date:   Sun, 26 Aug 2012 13:19:52 +0000

Better handling file renaming and deleting

If you are renaming a file via the mm hotkey and it is already opened then all
tabs and windows containing the old file will be replaced with a
new file. Current tab and windows structure is not changed anymore

If you are deleting a file via the md hotkey and it is already open, i.e.
presents in buffer lists then a buffer will be removed but a window and tab
will be kept with a ':enew' file

Diffstat:
Mnerdtree_plugin/fs_menu.vim | 40+++++++++++++++++++++++++++++++++++++---
1 file changed, 37 insertions(+), 3 deletions(-)
diff --git a/nerdtree_plugin/fs_menu.vim b/nerdtree_plugin/fs_menu.vim
@@ -58,7 +58,41 @@ endfunction
 function! s:promptToDelBuffer(bufnum, msg)
     echo a:msg
     if g:NERDTreeAutoDeleteBuffer || nr2char(getchar()) ==# 'y'
-        exec "silent bdelete! " . a:bufnum
+        " 1. ensure that all windows which display the just deleted filename
+        " now display an empty buffer (so a layout is preserved).
+        " Is not it better to close single tabs with this file only ?
+        let s:originalTabNumber = tabpagenr()
+        let s:originalWindowNumber = winnr()
+        exec "tabdo windo if winbufnr(0) == " . a:bufnum . " | exec ':enew! ' | endif"
+        exec "tabnext " . s:originalTabNumber
+        exec s:originalWindowNumber . "wincmd w"
+        " 3. We don't need a previous buffer anymore
+        exec "bwipeout! " . a:bufnum
+    endif
+endfunction
+
+"FUNCTION: s:promptToRenameBuffer(bufnum, msg){{{1
+"prints out the given msg and, if the user responds by pushing 'y' then the
+"buffer with the given bufnum is replaced with a new one
+"
+"Args:
+"bufnum: the buffer that may be deleted
+"msg: a message that will be echoed to the user asking them if they wish to
+"     del the buffer
+function! s:promptToRenameBuffer(bufnum, msg, newFileName)
+    echo a:msg
+    if g:NERDTreeAutoDeleteBuffer || nr2char(getchar()) ==# 'y'
+        " 1. ensure that a new buffer is loaded
+        exec "badd " . a:newFileName
+        " 2. ensure that all windows which display the just deleted filename
+        " display a buffer for a new filename. 
+        let s:originalTabNumber = tabpagenr()
+        let s:originalWindowNumber = winnr()
+        exec "tabdo windo if winbufnr(0) == " . a:bufnum . " | exec ':e! " . a:newFileName . "' | endif"
+        exec "tabnext " . s:originalTabNumber
+        exec s:originalWindowNumber . "wincmd w"
+        " 3. We don't need a previous buffer anymore
+        exec "bwipeout! " . a:bufnum
     endif
 endfunction
 "FUNCTION: NERDTreeAddNode(){{{1
@@ -112,8 +146,8 @@ function! NERDTreeMoveNode()
         "if the node is open in a buffer, ask the user if they want to
         "close that buffer
         if bufnum != -1
-            let prompt = "\nNode renamed.\n\nThe old file is open in buffer ". bufnum . (bufwinnr(bufnum) ==# -1 ? " (hidden)" : "") .". Delete this buffer? (yN)"
-            call s:promptToDelBuffer(bufnum, prompt)
+            let prompt = "\nNode renamed.\n\nThe old file is open in buffer ". bufnum . (bufwinnr(bufnum) ==# -1 ? " (hidden)" : "") .". Replace this buffer with a new file? (yN)"
+            call s:promptToRenameBuffer(bufnum,  prompt, newNodePath)
         endif
 
         call curNode.putCursorHere(1, 0)