Skip to content

Commit 213b00a

Browse files
committed
Move space from closed win into most recent win
If the focused win is in the same color then still use that one, otherwise use the most recently opened win. We now use the WinClosed autocmd for this which means that it also happens when using builtin vim functionality to close the window.
1 parent 248ba1c commit 213b00a

1 file changed

Lines changed: 36 additions & 20 deletions

File tree

plugin/acme.vim

Lines changed: 36 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -685,23 +685,6 @@ function s:WinCol(w)
685685
return col
686686
endfunc
687687

688-
function s:CloseWin(w)
689-
let h = winheight(a:w) + 1
690-
let col = s:WinCol(a:w)
691-
let [i, j] = [index(col, a:w), index(col, win_getid())]
692-
let sb = &splitbelow
693-
let &splitbelow = 0
694-
exe win_id2win(a:w).'close!'
695-
let &splitbelow = sb
696-
if j == -1
697-
return
698-
endif
699-
let d = i - j
700-
for i in d < 0 ? range(d + 1, -1) : reverse(range(d))
701-
call win_move_statusline(winnr() + i, d < 0 ? -h : h)
702-
endfor
703-
endfunc
704-
705688
function s:RestWinVars(w, vars)
706689
let vars = getwinvar(a:w, '&')
707690
for v in keys(a:vars)
@@ -738,7 +721,7 @@ function s:MoveWin(w, other, below)
738721
let nw = win_getid()
739722
noa exe win_id2win(p != a:w ? p : nw).'wincmd w'
740723
noa exe win_id2win(w != a:w ? w : nw).'wincmd w'
741-
noa call s:CloseWin(a:w)
724+
noa exe win_id2win(a:w).'close!'
742725
call s:RestWinVars(nw, vars)
743726
endif
744727
endfunc
@@ -753,7 +736,7 @@ function s:NewCol(w)
753736
endif
754737
noa exe win_id2win(p).'wincmd w'
755738
noa exe win_id2win(w).'wincmd w'
756-
call s:CloseWin(a:w)
739+
noa exe win_id2win(a:w).'close!'
757740
endfunc
758741

759742
function s:Scroll(topline)
@@ -856,7 +839,7 @@ function s:MiddleRelease(click)
856839
\ p.winrow <= winheight(p.winid)
857840
" off the statusline
858841
elseif p.wincol < 3
859-
call s:CloseWin(p.winid)
842+
exe win_id2win(p.winid).'close!'
860843
endif
861844
return
862845
endif
@@ -1171,6 +1154,36 @@ function s:BufWinLeave()
11711154
endif
11721155
endfunc
11731156

1157+
function s:WinClosedPre()
1158+
" Only works with 'nosplitbelow'
1159+
let w = str2nr(expand("<amatch>"))
1160+
let i = index(s:wins, w)
1161+
if i != -1
1162+
call remove(s:wins, i)
1163+
endif
1164+
let h = winheight(w) + 1
1165+
let col = s:WinCol(w)
1166+
let [i, j] = [index(col, w), index(col, win_getid())]
1167+
if j == -1 || j == i
1168+
let stack = filter(copy(s:wins), {_, w -> index(col, w) != -1})
1169+
if stack == []
1170+
return
1171+
endif
1172+
if j == i
1173+
exe win_id2win(stack[-1]).'wincmd w'
1174+
endif
1175+
let j = index(col, stack[-1])
1176+
endif
1177+
call timer_start(0, {_ -> s:WinClosedPost(col[j], i - j, h)})
1178+
endfunc
1179+
1180+
function s:WinClosedPost(w, n, h)
1181+
let w = win_id2win(a:w)
1182+
for i in a:n < 0 ? range(a:n + 1, -1) : reverse(range(a:n))
1183+
call win_move_statusline(w + i, a:n < 0 ? -a:h : a:h)
1184+
endfor
1185+
endfunc
1186+
11741187
augroup acme_vim
11751188
au!
11761189
au BufEnter * call s:ListDir()
@@ -1180,6 +1193,8 @@ au TextChanged,TextChangedI guide setl nomodified
11801193
au VimEnter * call s:ReloadDirs(winnr())
11811194
au VimResized * call s:ReloadDirs(0)
11821195
au WinResized * call s:ReloadDirs(0)
1196+
au WinClosed * call s:WinClosedPre()
1197+
au WinNew * call add(s:wins, win_getid())
11831198
augroup END
11841199

11851200
if exists("s:ctrlexe")
@@ -1200,6 +1215,7 @@ let s:editcids = {}
12001215
let s:editcmds = {}
12011216
let s:jobs = []
12021217
let s:scratch = {}
1218+
let s:wins = []
12031219

12041220
if s:ctrlexe != ''
12051221
let s:ctrl = job_start([s:ctrlexe], {

0 commit comments

Comments
 (0)