Skip to content

Commit 3e44d4e

Browse files
committed
Merge pull request preservim#191 from ZeusTheTrueGod/master
Improve handling obsolete buffers when renaming or deleting a file via the NERDTree 'm' menu
2 parents d62180d + 6b68797 commit 3e44d4e

File tree

2 files changed

+60
-4
lines changed

2 files changed

+60
-4
lines changed

doc/NERD_tree.txt

+18
Original file line numberDiff line numberDiff line change
@@ -658,6 +658,10 @@ NERD tree. These options should be set in your vimrc.
658658
Casade open while selected directory has only
659659
one child that also is a directory.
660660

661+
|'NERDTreeAutoDeleteBuffer'| Tells the NERD tree to automatically remove
662+
a buffer when a file is being deleted or renamed
663+
via a context menu command.
664+
661665
------------------------------------------------------------------------------
662666
3.2. Customisation details *NERDTreeOptionDetails*
663667

@@ -982,6 +986,20 @@ for Java projects. Use one of the follow lines to set this option: >
982986
let NERDTreeCasadeOpenSingleChildDir=1
983987
<
984988

989+
------------------------------------------------------------------------------
990+
*'NERDTreeAutoDeleteBuffer'*
991+
Values: 0 or 1
992+
Default: 0.
993+
994+
When using a context menu to delete or rename a file you may also want to delete
995+
the buffer which is no more valid. If the option is not set you will see a
996+
confirmation if you really want to delete an old buffer. If you always press 'y'
997+
then it worths to set this option to 1. Use one of the follow lines to set this
998+
option: >
999+
let NERDTreeAutoDeleteBuffer=0
1000+
let NERDTreeAutoDeleteBuffer=1
1001+
<
1002+
9851003
==============================================================================
9861004
4. The NERD tree API *NERDTreeAPI*
9871005

nerdtree_plugin/fs_menu.vim

+42-4
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,11 @@ if exists("g:loaded_nerdtree_fs_menu")
1515
endif
1616
let g:loaded_nerdtree_fs_menu = 1
1717

18+
"Automatically delete the buffer after deleting or renaming a file
19+
if !exists("g:NERDTreeAutoDeleteBuffer")
20+
let g:NERDTreeAutoDeleteBuffer = 0
21+
endif
22+
1823
call NERDTreeAddMenuItem({'text': '(a)dd a childnode', 'shortcut': 'a', 'callback': 'NERDTreeAddNode'})
1924
call NERDTreeAddMenuItem({'text': '(m)ove the current node', 'shortcut': 'm', 'callback': 'NERDTreeMoveNode'})
2025
call NERDTreeAddMenuItem({'text': '(d)elete the current node', 'shortcut': 'd', 'callback': 'NERDTreeDeleteNode'})
@@ -52,11 +57,44 @@ endfunction
5257
" del the buffer
5358
function! s:promptToDelBuffer(bufnum, msg)
5459
echo a:msg
55-
if nr2char(getchar()) ==# 'y'
56-
exec "silent bdelete! " . a:bufnum
60+
if g:NERDTreeAutoDeleteBuffer || nr2char(getchar()) ==# 'y'
61+
" 1. ensure that all windows which display the just deleted filename
62+
" now display an empty buffer (so a layout is preserved).
63+
" Is not it better to close single tabs with this file only ?
64+
let s:originalTabNumber = tabpagenr()
65+
let s:originalWindowNumber = winnr()
66+
exec "tabdo windo if winbufnr(0) == " . a:bufnum . " | exec ':enew! ' | endif"
67+
exec "tabnext " . s:originalTabNumber
68+
exec s:originalWindowNumber . "wincmd w"
69+
" 3. We don't need a previous buffer anymore
70+
exec "bwipeout! " . a:bufnum
5771
endif
5872
endfunction
5973

74+
"FUNCTION: s:promptToRenameBuffer(bufnum, msg){{{1
75+
"prints out the given msg and, if the user responds by pushing 'y' then the
76+
"buffer with the given bufnum is replaced with a new one
77+
"
78+
"Args:
79+
"bufnum: the buffer that may be deleted
80+
"msg: a message that will be echoed to the user asking them if they wish to
81+
" del the buffer
82+
function! s:promptToRenameBuffer(bufnum, msg, newFileName)
83+
echo a:msg
84+
if g:NERDTreeAutoDeleteBuffer || nr2char(getchar()) ==# 'y'
85+
" 1. ensure that a new buffer is loaded
86+
exec "badd " . a:newFileName
87+
" 2. ensure that all windows which display the just deleted filename
88+
" display a buffer for a new filename.
89+
let s:originalTabNumber = tabpagenr()
90+
let s:originalWindowNumber = winnr()
91+
exec "tabdo windo if winbufnr(0) == " . a:bufnum . " | exec ':e! " . a:newFileName . "' | endif"
92+
exec "tabnext " . s:originalTabNumber
93+
exec s:originalWindowNumber . "wincmd w"
94+
" 3. We don't need a previous buffer anymore
95+
exec "bwipeout! " . a:bufnum
96+
endif
97+
endfunction
6098
"FUNCTION: NERDTreeAddNode(){{{1
6199
function! NERDTreeAddNode()
62100
let curDirNode = g:NERDTreeDirNode.GetSelected()
@@ -108,8 +146,8 @@ function! NERDTreeMoveNode()
108146
"if the node is open in a buffer, ask the user if they want to
109147
"close that buffer
110148
if bufnum != -1
111-
let prompt = "\nNode renamed.\n\nThe old file is open in buffer ". bufnum . (bufwinnr(bufnum) ==# -1 ? " (hidden)" : "") .". Delete this buffer? (yN)"
112-
call s:promptToDelBuffer(bufnum, prompt)
149+
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)"
150+
call s:promptToRenameBuffer(bufnum, prompt, newNodePath)
113151
endif
114152

115153
call curNode.putCursorHere(1, 0)

0 commit comments

Comments
 (0)