Skip to content

Commit

Permalink
Fix {'keepopen':0} in NERDTreeCustomOpenArgs (#1217)
Browse files Browse the repository at this point in the history
* Replace #and() with two more-specific functions.

* Push NERDTreeQuitOnOpen checking earlier in the call stack.

* Fix bug in `go` key for file bookmarks. It was behaving like `gs`.

* Fix the `o` mapping for bookmark nodes and the g:NERDTree reference.

* Use get() function to shorten if statement logic.

* Update version number in change log.

* Remove obsolete reference to MERDTreeQuitOnOpen in comment.
  • Loading branch information
PhilRunninger authored Feb 10, 2021
1 parent d3becd1 commit 593c16a
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 45 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
- **.PATCH**: Pull Request Title (PR Author) [PR Number](Link to PR)
-->
#### 6.10
- **.5**: Fix `{'keepopen':0}` in NERDTreeCustomOpenArgs (PhilRunninger) [#1217](https://github.com/preservim/nerdtree/pull/1217)
- **.4**: Removed directory separator from sort key (Daniel E) [#1219](https://github.com/preservim/nerdtree/pull/1219)
- **.3**: Add new FAQ and answer: How to prevent buffers replacing NERDTree. (PhilRunninger) [#1215](https://github.com/preservim/nerdtree/pull/1215)
- **.2**: New menu command: Run a system command in this directory. (PhilRunninger) [#1214](https://github.com/preservim/nerdtree/pull/1214)
Expand Down
32 changes: 10 additions & 22 deletions autoload/nerdtree.vim
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,16 @@ endfunction
" SECTION: General Functions {{{1
"============================================================

" FUNCTION: nerdtree#closeTreeOnOpen()
function! nerdtree#closeTreeOnOpen() abort
return g:NERDTreeQuitOnOpen == 1 || g:NERDTreeQuitOnOpen == 3
endfunction

" FUNCTION: nerdtree#closeBookmarksOnOpen()
function! nerdtree#closeBookmarksOnOpen() abort
return g:NERDTreeQuitOnOpen == 2 || g:NERDTreeQuitOnOpen == 3
endfunction

" FUNCTION: nerdtree#slash() {{{2
" Return the path separator used by the underlying file system. Special
" consideration is taken for the use of the 'shellslash' option on Windows
Expand All @@ -46,28 +56,6 @@ function! nerdtree#slash() abort
return '/'
endfunction

"FUNCTION: nerdtree#and(x,y) {{{2
" Implements and() function for Vim <= 7.4
function! nerdtree#and(x,y) abort
if exists('*and')
return and(a:x, a:y)
else
let l:x = a:x
let l:y = a:y
let l:n = 0
let l:result = 0
while l:x > 0 && l:y > 0
if (l:x % 2) && (l:y % 2)
let l:result += float2nr(pow(2, l:n))
endif
let l:x = float2nr(l:x / 2)
let l:y = float2nr(l:y / 2)
let l:n += 1
endwhile
return l:result
endif
endfunction

"FUNCTION: nerdtree#checkForBrowse(dir) {{{2
"inits a window tree in the current buffer if appropriate
function! nerdtree#checkForBrowse(dir) abort
Expand Down
20 changes: 10 additions & 10 deletions autoload/nerdtree/ui_glue.vim
Original file line number Diff line number Diff line change
Expand Up @@ -168,13 +168,13 @@ endfunction
"FUNCTION: s:activateFileNode() {{{1
"handle the user activating a tree node
function! s:activateFileNode(node) abort
call a:node.activate({'reuse': 'all', 'where': 'p'})
call a:node.activate({'reuse': 'all', 'where': 'p', 'keepopen': !nerdtree#closeTreeOnOpen()})
endfunction

"FUNCTION: s:activateBookmark(bookmark) {{{1
"handle the user activating a bookmark
function! s:activateBookmark(bm) abort
call a:bm.activate(b:NERDTree, !a:bm.path.isDirectory ? {'where': 'p'} : {})
call a:bm.activate(b:NERDTree, !a:bm.path.isDirectory ? {'where': 'p', 'keepopen': !nerdtree#closeTreeOnOpen()} : {})
endfunction

" FUNCTION: nerdtree#ui_glue#bookmarkNode(name) {{{1
Expand Down Expand Up @@ -539,24 +539,24 @@ endfunction

" FUNCTION: s:openHSplit(target) {{{1
function! s:openHSplit(target) abort
call a:target.activate({'where': 'h'})
call a:target.activate({'where': 'h', 'keepopen': !nerdtree#closeTreeOnOpen()})
endfunction

" FUNCTION: s:openVSplit(target) {{{1
function! s:openVSplit(target) abort
call a:target.activate({'where': 'v'})
call a:target.activate({'where': 'v', 'keepopen': !nerdtree#closeTreeOnOpen()})
endfunction

"FUNCTION: s:openHSplitBookmark(bookmark) {{{1
"handle the user activating a bookmark
function! s:openHSplitBookmark(bm) abort
call a:bm.activate(b:NERDTree, !a:bm.path.isDirectory ? {'where': 'h'} : {})
call a:bm.activate(b:NERDTree, !a:bm.path.isDirectory ? {'where': 'h', 'keepopen': !nerdtree#closeTreeOnOpen()} : {})
endfunction

"FUNCTION: s:openVSplitBookmark(bookmark) {{{1
"handle the user activating a bookmark
function! s:openVSplitBookmark(bm) abort
call a:bm.activate(b:NERDTree, !a:bm.path.isDirectory ? {'where': 'v'} : {})
call a:bm.activate(b:NERDTree, !a:bm.path.isDirectory ? {'where': 'v', 'keepopen': !nerdtree#closeTreeOnOpen()} : {})
endfunction

" FUNCTION: s:previewHSplitBookmark(bookmark) {{{1
Expand All @@ -576,13 +576,13 @@ endfunction

" FUNCTION: s:openInNewTab(target) {{{1
function! s:openInNewTab(target) abort
let l:opener = g:NERDTreeOpener.New(a:target.path, {'where': 't'})
let l:opener = g:NERDTreeOpener.New(a:target.path, {'where': 't', 'keepopen': !nerdtree#closeTreeOnOpen()})
call l:opener.open(a:target)
endfunction

" FUNCTION: s:openInNewTabSilent(target) {{{1
function! s:openInNewTabSilent(target) abort
let l:opener = g:NERDTreeOpener.New(a:target.path, {'where': 't', 'stay': 1})
let l:opener = g:NERDTreeOpener.New(a:target.path, {'where': 't', 'keepopen': !nerdtree#closeTreeOnOpen(), 'stay': 1})
call l:opener.open(a:target)
endfunction

Expand All @@ -596,7 +596,7 @@ endfunction

" FUNCTION: s:previewBookmark(bookmark) {{{1
function! s:previewBookmark(bookmark) abort
call a:bookmark.activate(b:NERDTree, !a:bookmark.path.isDirectory ? {'stay': 1, 'where': 'h', 'keepopen': 1} : {})
call a:bookmark.activate(b:NERDTree, !a:bookmark.path.isDirectory ? {'stay': 1, 'where': 'p', 'keepopen': 1} : {})
endfunction

"FUNCTION: s:previewNodeCurrent(node) {{{1
Expand All @@ -621,7 +621,7 @@ function! nerdtree#ui_glue#revealBookmark(name) abort
let targetNode = g:NERDTreeBookmark.GetNodeForName(a:name, 0, b:NERDTree)
call targetNode.putCursorHere(0, 1)
catch /^NERDTree.BookmarkNotFoundError/
call nerdtree#echo('Bookmark isnt cached under the current root')
call nerdtree#echo('Bookmark isn''t cached under the current root')
endtry
endfunction

Expand Down
2 changes: 1 addition & 1 deletion lib/nerdtree/bookmark.vim
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ endfunction
function! s:Bookmark.open(nerdtree, ...)
let opts = a:0 ? a:1 : {}

if nerdtree#and(g:NERDTreeQuitOnOpen,2)
if nerdtree#closeBookmarksOnOpen()
call a:nerdtree.ui.toggleShowBookmarks()
endif

Expand Down
8 changes: 0 additions & 8 deletions lib/nerdtree/nerdtree.vim
Original file line number Diff line number Diff line change
Expand Up @@ -65,14 +65,6 @@ function! s:NERDTree.Close()
endif
endfunction

"FUNCTION: s:NERDTree.CloseIfQuitOnOpen() {{{1
"Closes the NERD tree window if the close on open option is set
function! s:NERDTree.CloseIfQuitOnOpen()
if nerdtree#and(g:NERDTreeQuitOnOpen,1) && s:NERDTree.IsOpen()
call s:NERDTree.Close()
endif
endfunction

"FUNCTION: s:NERDTree.CursorToBookmarkTable(){{{1
"Places the cursor at the top of the bookmarks table
function! s:NERDTree.CursorToBookmarkTable()
Expand Down
7 changes: 3 additions & 4 deletions lib/nerdtree/opener.vim
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,7 @@ function! s:Opener._bufInWindows(bnum)
endfunction

" FUNCTION: Opener._checkToCloseTree(newtab) {{{1
" Check the class options and global options (i.e. NERDTreeQuitOnOpen) to see
" if the tree should be closed now.
" Check the class options to see if the tree should be closed now.
"
" Args:
" a:newtab - boolean. If set, only close the tree now if we are opening the
Expand All @@ -46,7 +45,7 @@ function! s:Opener._checkToCloseTree(newtab)
endif

if (a:newtab && self._where ==# 't') || !a:newtab
call g:NERDTree.CloseIfQuitOnOpen()
call g:NERDTree.Close()
endif
endfunction

Expand Down Expand Up @@ -218,7 +217,7 @@ endfunction

" FUNCTION: Opener._openFile() {{{1
function! s:Opener._openFile()
if !self._stay && !nerdtree#and(g:NERDTreeQuitOnOpen,1) && exists('b:NERDTreeZoomed') && b:NERDTreeZoomed
if !self._stay && self._keepopen && get(b:, 'NERDTreeZoomed', 0)
call b:NERDTree.ui.toggleZoom()
endif

Expand Down

0 comments on commit 593c16a

Please sign in to comment.