Skip to content

Improve handling obsolete buffers when renaming or deleting a file via the NERDTree 'm' menu #191

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Oct 18, 2012
Merged
Show file tree
Hide file tree
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
18 changes: 18 additions & 0 deletions doc/NERD_tree.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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*

Expand Down Expand Up @@ -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*

Expand Down
46 changes: 42 additions & 4 deletions nerdtree_plugin/fs_menu.vim
Original file line number Diff line number Diff line change
Expand Up @@ -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'})
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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)
Expand Down