commit 3e44d4ea5c3d8cfa192e66c77afd05629ddc92e6
parent d62180d0ff26d308b815205ca2f2cd651a112b49
Author: Martin Grenfell <martin.grenfell@gmail.com>
Date: Thu, 18 Oct 2012 01:11:35 -0700
Merge pull request #191 from ZeusTheTrueGod/master
Improve handling obsolete buffers when renaming or deleting a file via the NERDTree 'm' menu
Diffstat:
2 files changed, 60 insertions(+), 4 deletions(-)
diff --git a/doc/NERD_tree.txt b/doc/NERD_tree.txt
@@ -658,6 +658,10 @@ NERD tree. These options should be set in your vimrc.
Casade open while selected directory has only
one child that also is a directory.
+|'NERDTreeAutoDeleteBuffer'| Tells the NERD tree to automatically remove
+ a buffer when a file is being deleted or renamed
+ via a context menu command.
+
------------------------------------------------------------------------------
3.2. Customisation details *NERDTreeOptionDetails*
@@ -982,6 +986,20 @@ for Java projects. Use one of the follow lines to set this option: >
let NERDTreeCasadeOpenSingleChildDir=1
<
+------------------------------------------------------------------------------
+ *'NERDTreeAutoDeleteBuffer'*
+Values: 0 or 1
+Default: 0.
+
+When using a context menu to delete or rename a file you may also want to delete
+the buffer which is no more valid. If the option is not set you will see a
+confirmation if you really want to delete an old buffer. If you always press 'y'
+then it worths to set this option to 1. Use one of the follow lines to set this
+option: >
+ let NERDTreeAutoDeleteBuffer=0
+ let NERDTreeAutoDeleteBuffer=1
+<
+
==============================================================================
4. The NERD tree API *NERDTreeAPI*
diff --git a/nerdtree_plugin/fs_menu.vim b/nerdtree_plugin/fs_menu.vim
@@ -15,6 +15,11 @@ if exists("g:loaded_nerdtree_fs_menu")
endif
let g:loaded_nerdtree_fs_menu = 1
+"Automatically delete the buffer after deleting or renaming a file
+if !exists("g:NERDTreeAutoDeleteBuffer")
+ let g:NERDTreeAutoDeleteBuffer = 0
+endif
+
call NERDTreeAddMenuItem({'text': '(a)dd a childnode', 'shortcut': 'a', 'callback': 'NERDTreeAddNode'})
call NERDTreeAddMenuItem({'text': '(m)ove the current node', 'shortcut': 'm', 'callback': 'NERDTreeMoveNode'})
call NERDTreeAddMenuItem({'text': '(d)elete the current node', 'shortcut': 'd', 'callback': 'NERDTreeDeleteNode'})
@@ -52,11 +57,44 @@ endfunction
" del the buffer
function! s:promptToDelBuffer(bufnum, msg)
echo a:msg
- if nr2char(getchar()) ==# 'y'
- exec "silent bdelete! " . a:bufnum
+ if g:NERDTreeAutoDeleteBuffer || nr2char(getchar()) ==# 'y'
+ " 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
function! NERDTreeAddNode()
let curDirNode = g:NERDTreeDirNode.GetSelected()
@@ -108,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)