Skip to content
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

Make the behavior of window splits consistent #1035

Merged
merged 7 commits into from
Sep 24, 2019
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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# Change Log

#### 6.0...
- **.0**: Make the behavior of window splits consistent [#1035](https://github.com/scrooloose/nerdtree/pull/1035)
#### 5.3...
- **.1**: Fix the `e` key mapping to use netrw if desired [#1031](https://github.com/scrooloose/nerdtree/pull/1031)
- **.0**: Add file extension and size to sorting capabilities [#1029](https://github.com/scrooloose/nerdtree/pull/1029)
Expand Down
7 changes: 6 additions & 1 deletion autoload/nerdtree.vim
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ endif
let g:loaded_nerdtree_autoload = 1

let s:rootNERDTreePath = resolve(expand("<sfile>:p:h:h"))

"FUNCTION: nerdtree#version(...) {{{1
" If any value is given as an argument, the entire line of text from the
" change log is shown for the current version; otherwise, only the version
" number is shown.
function! nerdtree#version(...)
let l:changelog = readfile(join([s:rootNERDTreePath, "CHANGELOG.md"], nerdtree#slash()))
let l:text = 'Unknown'
Expand All @@ -22,6 +27,7 @@ endfunction
" SECTION: General Functions {{{1
"============================================================

"FUNCTION: nerdtree#slash() {{{2
function! nerdtree#slash()

if nerdtree#runningWindows()
Expand Down Expand Up @@ -49,7 +55,6 @@ function! nerdtree#and(x,y)
if (l:x % 2) && (l:y % 2)
let l:result += float2nr(pow(2, l:n))
endif
echomsg l:x . ", " . l:y . " => " l:result
let l:x = float2nr(l:x / 2)
let l:y = float2nr(l:y / 2)
let l:n += 1
Expand Down
9 changes: 8 additions & 1 deletion lib/nerdtree/nerdtree.vim
Original file line number Diff line number Diff line change
Expand Up @@ -148,12 +148,19 @@ function! s:NERDTree.GetWinNum()
return bufwinnr(t:NERDTreeBufName)
endif

" If WindowTree, there is no t:NERDTreeBufName variable. Search all windows.
for w in range(1,winnr('$'))
if bufname(winbufnr(w)) =~# '^' . g:NERDTreeCreator.BufNamePrefix() . '\d\+$'
return w
endif
endfor

return -1
endfunction

"FUNCTION: s:NERDTree.IsOpen() {{{1
function! s:NERDTree.IsOpen()
return s:NERDTree.GetWinNum() != -1 || bufname('%') =~# '^' . g:NERDTreeCreator.BufNamePrefix() . '\d\+$'
return s:NERDTree.GetWinNum() != -1
endfunction

"FUNCTION: s:NERDTree.isTabTree() {{{1
Expand Down
55 changes: 15 additions & 40 deletions lib/nerdtree/opener.vim
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,9 @@ endfunction
function! s:Opener._gotoTargetWin()
if b:NERDTree.isWinTree()
if self._where == 'v'
vsplit
call self._newVSplit()
elseif self._where == 'h'
split
call self._newSplit()
elseif self._where == 't'
tabnew
endif
Expand Down Expand Up @@ -153,44 +153,17 @@ endfunction

" FUNCTION: Opener._newSplit() {{{1
function! s:Opener._newSplit()
" Save the user's settings for splitbelow and splitright
let savesplitbelow=&splitbelow
let savesplitright=&splitright

" 'there' will be set to a command to move from the split window
" back to the explorer window
"
" 'back' will be set to a command to move from the explorer window
" back to the newly split window
"
" 'right' and 'below' will be set to the settings needed for
" splitbelow and splitright IF the explorer is the only window.
"
let there= g:NERDTreeWinPos ==# "left" ? "wincmd h" : "wincmd l"
let back = g:NERDTreeWinPos ==# "left" ? "wincmd l" : "wincmd h"
let right= g:NERDTreeWinPos ==# "left"
let below=0

" Attempt to go to adjacent window
call nerdtree#exec(back, 1)

let onlyOneWin = (winnr("$") ==# 1)

" If no adjacent window, set splitright and splitbelow appropriately
let savesplitright = &splitright
if onlyOneWin
let &splitright=right
let &splitbelow=below
else
" found adjacent window - invert split direction
let &splitright=!right
let &splitbelow=!below
let &splitright = (g:NERDTreeWinPos ==# "left")
endif

let splitMode = onlyOneWin ? "vertical" : ""

" Open the new window
try
exec(splitMode." sp ")
call nerdtree#exec('wincmd p', 1)
" If only one window (ie. NERDTree), split vertically instead.
call nerdtree#exec(onlyOneWin ? "vertical split" : "split",1)
catch /^Vim\%((\a\+)\)\=:E37/
call g:NERDTree.CursorToTreeWin()
throw "NERDTree.FileAlreadyOpenAndModifiedError: ". self._path.str() ." is already open and modified."
Expand All @@ -200,22 +173,23 @@ function! s:Opener._newSplit()

"resize the tree window if no other window was open before
if onlyOneWin
let size = exists("b:NERDTreeOldWindowSize") ? b:NERDTreeOldWindowSize : g:NERDTreeWinSize
call nerdtree#exec(there, 1)
exec("silent ". splitMode ." resize ". size)
let size = exists('b:NERDTreeOldWindowSize') ? b:NERDTreeOldWindowSize : g:NERDTreeWinSize
call nerdtree#exec('wincmd p', 1)
call nerdtree#exec('silent '. splitMode .' resize '. size, 1)
call nerdtree#exec('wincmd p', 0)
endif

" Restore splitmode settings
let &splitbelow=savesplitbelow
let &splitright=savesplitright
endfunction

" FUNCTION: Opener._newVSplit() {{{1
function! s:Opener._newVSplit()
let l:winwidth = winwidth('.')

if winnr('$') == 1
let onlyOneWin = (winnr("$") ==# 1)
let savesplitright = &splitright
if onlyOneWin
let &splitright = (g:NERDTreeWinPos ==# "left")
let l:winwidth = g:NERDTreeWinSize
endif

Expand All @@ -229,6 +203,7 @@ function! s:Opener._newVSplit()
execute 'silent vertical resize ' . l:winwidth

call nerdtree#exec(l:currentWindowNumber . 'wincmd w', 0)
let &splitright=savesplitright
endfunction

" FUNCTION: Opener.open(target) {{{1
Expand Down