blob: b6381a33b7a1ca7bcd7eaafecf6347529a5143a4 [file] [log] [blame]
Bram Moolenaar991dea32016-05-24 11:31:32 +02001" Tests for window cmd (:wincmd, :split, :vsplit, :resize and etc...)
2
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +02003source check.vim
Luuk van Baal20e58562022-09-23 12:57:09 +01004source screendump.vim
Bram Moolenaara27e1dc2019-10-07 22:27:36 +02005
Bram Moolenaar991dea32016-05-24 11:31:32 +02006func Test_window_cmd_ls0_with_split()
7 set ls=0
8 set splitbelow
9 split
10 quit
11 call assert_equal(0, &lines - &cmdheight - winheight(0))
12 new | only!
13 "
14 set splitbelow&vim
15 botright split
16 quit
17 call assert_equal(0, &lines - &cmdheight - winheight(0))
18 new | only!
19 set ls&vim
20endfunc
21
zeertzjqfbf20712023-04-26 19:01:44 +010022func Test_window_cmd_ls0_split_scrolling()
23 CheckRunVimInTerminal
24
25 let lines =<< trim END
26 set laststatus=0
27 call setline(1, range(1, 100))
28 normal! G
29 END
30 call writefile(lines, 'XTestLs0SplitScrolling', 'D')
31 let buf = RunVimInTerminal('-S XTestLs0SplitScrolling', #{rows: 10})
32
33 call term_sendkeys(buf, ":botright split\<CR>")
34 call WaitForAssert({-> assert_match('Bot$', term_getline(buf, 5))})
35 call assert_equal('100', term_getline(buf, 4))
36
37 call StopVimInTerminal(buf)
38endfunc
39
Bram Moolenaar991dea32016-05-24 11:31:32 +020040func Test_window_cmd_cmdwin_with_vsp()
Bram Moolenaar72cf47a2018-05-10 18:23:29 +020041 let efmt = 'Expected 0 but got %d (in ls=%d, %s window)'
Bram Moolenaar991dea32016-05-24 11:31:32 +020042 for v in range(0, 2)
43 exec "set ls=" . v
44 vsplit
45 call feedkeys("q:\<CR>")
46 let ac = &lines - (&cmdheight + winheight(0) + !!v)
47 let emsg = printf(efmt, ac, v, 'left')
48 call assert_equal(0, ac, emsg)
49 wincmd w
50 let ac = &lines - (&cmdheight + winheight(0) + !!v)
51 let emsg = printf(efmt, ac, v, 'right')
52 call assert_equal(0, ac, emsg)
53 new | only!
54 endfor
55 set ls&vim
56endfunc
57
Bram Moolenaar96bde992022-08-10 17:23:12 +010058func Test_cmdheight_not_changed()
59 set cmdheight=2
60 set winminheight=0
61 augroup Maximize
62 autocmd WinEnter * wincmd _
63 augroup END
64 split
65 tabnew
66 tabfirst
67 call assert_equal(2, &cmdheight)
68
69 tabonly!
70 only
Milly2cddf0e2024-11-28 18:16:55 +010071 set winminheight& cmdheight&
Bram Moolenaar96bde992022-08-10 17:23:12 +010072 augroup Maximize
73 au!
74 augroup END
75 augroup! Maximize
76endfunc
77
Bram Moolenaar5d3c9f82020-06-26 20:41:39 +020078" Test for jumping to windows
79func Test_window_jump()
80 new
81 " jumping to a window with a count greater than the max windows
82 exe "normal 4\<C-W>w"
83 call assert_equal(2, winnr())
84 only
85endfunc
86
87func Test_window_cmd_wincmd_gf()
Bram Moolenaar5d2ca042016-06-26 17:11:21 +020088 let fname = 'test_gf.txt'
89 let swp_fname = '.' . fname . '.swp'
Bram Moolenaardb4c9472022-10-15 22:06:06 +010090 call writefile([], fname, 'D')
91 call writefile([], swp_fname, 'D')
Bram Moolenaar5d2ca042016-06-26 17:11:21 +020092 function s:swap_exists()
93 let v:swapchoice = s:swap_choice
94 endfunc
Bram Moolenaareaa49e42019-07-13 18:08:59 +020095 " Remove the catch-all that runtest.vim adds
96 au! SwapExists
Bram Moolenaar5d2ca042016-06-26 17:11:21 +020097 augroup test_window_cmd_wincmd_gf
98 autocmd!
99 exec "autocmd SwapExists " . fname . " call s:swap_exists()"
100 augroup END
101
102 call setline(1, fname)
103 " (E)dit anyway
104 let s:swap_choice = 'e'
105 wincmd gf
106 call assert_equal(2, tabpagenr())
107 call assert_equal(fname, bufname("%"))
108 quit!
109
110 " (Q)uit
111 let s:swap_choice = 'q'
112 wincmd gf
113 call assert_equal(1, tabpagenr())
114 call assert_notequal(fname, bufname("%"))
115 new | only!
116
Bram Moolenaar5d2ca042016-06-26 17:11:21 +0200117 augroup! test_window_cmd_wincmd_gf
118endfunc
119
Bram Moolenaar4520d442017-03-19 16:09:46 +0100120func Test_window_quit()
121 e Xa
122 split Xb
Bram Moolenaarf92e58c2019-09-08 21:51:41 +0200123 call assert_equal(2, '$'->winnr())
Bram Moolenaar4520d442017-03-19 16:09:46 +0100124 call assert_equal('Xb', bufname(winbufnr(1)))
125 call assert_equal('Xa', bufname(winbufnr(2)))
126
127 wincmd q
128 call assert_equal(1, winnr('$'))
129 call assert_equal('Xa', bufname(winbufnr(1)))
130
131 bw Xa Xb
132endfunc
133
134func Test_window_horizontal_split()
135 call assert_equal(1, winnr('$'))
136 3wincmd s
137 call assert_equal(2, winnr('$'))
138 call assert_equal(3, winheight(0))
Bram Moolenaarf92e58c2019-09-08 21:51:41 +0200139 call assert_equal(winwidth(1), 2->winwidth())
Bram Moolenaar4520d442017-03-19 16:09:46 +0100140
141 call assert_fails('botright topleft wincmd s', 'E442:')
142 bw
143endfunc
144
145func Test_window_vertical_split()
146 call assert_equal(1, winnr('$'))
147 3wincmd v
148 call assert_equal(2, winnr('$'))
149 call assert_equal(3, winwidth(0))
150 call assert_equal(winheight(1), winheight(2))
151
152 call assert_fails('botright topleft wincmd v', 'E442:')
153 bw
154endfunc
155
Bram Moolenaar1bbb6192018-11-10 16:02:01 +0100156" Test the ":wincmd ^" and "<C-W>^" commands.
Bram Moolenaar4520d442017-03-19 16:09:46 +0100157func Test_window_split_edit_alternate()
Bram Moolenaar1bbb6192018-11-10 16:02:01 +0100158 " Test for failure when the alternate buffer/file no longer exists.
159 edit Xfoo | %bw
Bram Moolenaare2e40752020-09-04 21:18:46 +0200160 call assert_fails(':wincmd ^', 'E23:')
Bram Moolenaar1bbb6192018-11-10 16:02:01 +0100161
162 " Test for the expected behavior when we have two named buffers.
163 edit Xfoo | edit Xbar
Bram Moolenaar4520d442017-03-19 16:09:46 +0100164 wincmd ^
Bram Moolenaar1bbb6192018-11-10 16:02:01 +0100165 call assert_equal('Xfoo', bufname(winbufnr(1)))
166 call assert_equal('Xbar', bufname(winbufnr(2)))
167 only
Bram Moolenaar4520d442017-03-19 16:09:46 +0100168
Bram Moolenaar1bbb6192018-11-10 16:02:01 +0100169 " Test for the expected behavior when the alternate buffer is not named.
170 enew | let l:nr1 = bufnr('%')
171 edit Xfoo | let l:nr2 = bufnr('%')
172 wincmd ^
173 call assert_equal(l:nr1, winbufnr(1))
174 call assert_equal(l:nr2, winbufnr(2))
175 only
176
Bram Moolenaard42333d2018-11-10 20:28:19 +0100177 " FIXME: this currently fails on AppVeyor, but passes locally
178 if !has('win32')
179 " Test the Normal mode command.
180 call feedkeys("\<C-W>\<C-^>", 'tx')
181 call assert_equal(l:nr2, winbufnr(1))
182 call assert_equal(l:nr1, winbufnr(2))
183 endif
Bram Moolenaar1bbb6192018-11-10 16:02:01 +0100184
185 %bw!
186endfunc
187
188" Test the ":[count]wincmd ^" and "[count]<C-W>^" commands.
189func Test_window_split_edit_bufnr()
Bram Moolenaar1bbb6192018-11-10 16:02:01 +0100190 %bwipeout
191 let l:nr = bufnr('%') + 1
Bram Moolenaare2e40752020-09-04 21:18:46 +0200192 call assert_fails(':execute "normal! ' . l:nr . '\<C-W>\<C-^>"', 'E92:')
193 call assert_fails(':' . l:nr . 'wincmd ^', 'E16:')
194 call assert_fails(':0wincmd ^', 'E16:')
Bram Moolenaar1bbb6192018-11-10 16:02:01 +0100195
196 edit Xfoo | edit Xbar | edit Xbaz
197 let l:foo_nr = bufnr('Xfoo')
198 let l:bar_nr = bufnr('Xbar')
199 let l:baz_nr = bufnr('Xbaz')
200
Bram Moolenaar8617b402018-11-10 20:47:48 +0100201 " FIXME: this currently fails on AppVeyor, but passes locally
202 if !has('win32')
203 call feedkeys(l:foo_nr . "\<C-W>\<C-^>", 'tx')
204 call assert_equal('Xfoo', bufname(winbufnr(1)))
205 call assert_equal('Xbaz', bufname(winbufnr(2)))
206 only
Bram Moolenaar1bbb6192018-11-10 16:02:01 +0100207
Bram Moolenaar8617b402018-11-10 20:47:48 +0100208 call feedkeys(l:bar_nr . "\<C-W>\<C-^>", 'tx')
209 call assert_equal('Xbar', bufname(winbufnr(1)))
210 call assert_equal('Xfoo', bufname(winbufnr(2)))
211 only
Bram Moolenaar1bbb6192018-11-10 16:02:01 +0100212
Bram Moolenaar8617b402018-11-10 20:47:48 +0100213 execute l:baz_nr . 'wincmd ^'
214 call assert_equal('Xbaz', bufname(winbufnr(1)))
215 call assert_equal('Xbar', bufname(winbufnr(2)))
216 endif
Bram Moolenaar1bbb6192018-11-10 16:02:01 +0100217
218 %bw!
Bram Moolenaar4520d442017-03-19 16:09:46 +0100219endfunc
220
Sean Dewar5866bc32024-03-13 20:17:24 +0100221func s:win_layout_info(tp = tabpagenr()) abort
Sean Dewar5cac1a92024-03-12 21:11:39 +0100222 return #{
Sean Dewar5866bc32024-03-13 20:17:24 +0100223 \ layout: winlayout(a:tp),
224 \ pos_sizes: range(1, tabpagewinnr(a:tp, '$'))
225 \ ->map({_, nr -> win_getid(nr, a:tp)->getwininfo()[0]})
Sean Dewar5cac1a92024-03-12 21:11:39 +0100226 \ ->map({_, wininfo -> #{id: wininfo.winid,
227 \ row: wininfo.winrow,
228 \ col: wininfo.wincol,
229 \ width: wininfo.width,
230 \ height: wininfo.height}})
231 \ ->sort({a, b -> a.id - b.id})
232 \ }
233endfunc
234
Bram Moolenaar9b9be002020-03-22 14:41:22 +0100235func Test_window_split_no_room()
236 " N horizontal windows need >= 2*N + 1 lines:
237 " - 1 line + 1 status line in each window
238 " - 1 Ex command line
239 "
240 " 2*N + 1 <= &lines
241 " N <= (lines - 1)/2
242 "
243 " Beyond that number of windows, E36: Not enough room is expected.
244 let hor_win_count = (&lines - 1)/2
245 let hor_split_count = hor_win_count - 1
246 for s in range(1, hor_split_count) | split | endfor
247 call assert_fails('split', 'E36:')
248
Sean Dewar0fd44a52024-02-20 20:28:15 +0100249 botright vsplit
250 wincmd |
Sean Dewar5cac1a92024-03-12 21:11:39 +0100251 let info = s:win_layout_info()
Sean Dewar0fd44a52024-02-20 20:28:15 +0100252 call assert_fails('wincmd J', 'E36:')
253 call assert_fails('wincmd K', 'E36:')
Sean Dewar5cac1a92024-03-12 21:11:39 +0100254 call assert_equal(info, s:win_layout_info())
Sean Dewar0fd44a52024-02-20 20:28:15 +0100255 only
256
Bram Moolenaar9b9be002020-03-22 14:41:22 +0100257 " N vertical windows need >= 2*(N - 1) + 1 columns:
258 " - 1 column + 1 separator for each window (except last window)
259 " - 1 column for the last window which does not have separator
260 "
261 " 2*(N - 1) + 1 <= &columns
262 " 2*N - 1 <= &columns
263 " N <= (&columns + 1)/2
264 let ver_win_count = (&columns + 1)/2
265 let ver_split_count = ver_win_count - 1
266 for s in range(1, ver_split_count) | vsplit | endfor
267 call assert_fails('vsplit', 'E36:')
268
Sean Dewar0fd44a52024-02-20 20:28:15 +0100269 split
270 wincmd |
Sean Dewar5cac1a92024-03-12 21:11:39 +0100271 let info = s:win_layout_info()
Sean Dewar0fd44a52024-02-20 20:28:15 +0100272 call assert_fails('wincmd H', 'E36:')
273 call assert_fails('wincmd L', 'E36:')
Sean Dewar5cac1a92024-03-12 21:11:39 +0100274 call assert_equal(info, s:win_layout_info())
Sean Dewar0fd44a52024-02-20 20:28:15 +0100275
276 " Check that the last statusline isn't lost.
Sean Dewar02fcae02024-02-21 19:40:44 +0100277 " Set its window's width to 2 for the test.
Sean Dewar0fd44a52024-02-20 20:28:15 +0100278 wincmd j
Sean Dewar02fcae02024-02-21 19:40:44 +0100279 set laststatus=0 winminwidth=0
280 vertical resize 2
Sean Dewar5cac1a92024-03-12 21:11:39 +0100281 " Update expected positions/sizes after the resize. Layout is unchanged.
282 let info.pos_sizes = s:win_layout_info().pos_sizes
Sean Dewar02fcae02024-02-21 19:40:44 +0100283 set winminwidth&
Sean Dewar0fd44a52024-02-20 20:28:15 +0100284 call setwinvar(winnr('k'), '&statusline', '@#')
285 let last_stl_row = win_screenpos(0)[0] - 1
286 redraw
287 call assert_equal('@#|', GetScreenStr(last_stl_row))
288 call assert_equal('~ |', GetScreenStr(&lines - &cmdheight))
Sean Dewar02fcae02024-02-21 19:40:44 +0100289
Sean Dewar0fd44a52024-02-20 20:28:15 +0100290 call assert_fails('wincmd H', 'E36:')
291 call assert_fails('wincmd L', 'E36:')
Sean Dewar5cac1a92024-03-12 21:11:39 +0100292 call assert_equal(info, s:win_layout_info())
Sean Dewar0fd44a52024-02-20 20:28:15 +0100293 call setwinvar(winnr('k'), '&statusline', '=-')
294 redraw
295 call assert_equal('=-|', GetScreenStr(last_stl_row))
296 call assert_equal('~ |', GetScreenStr(&lines - &cmdheight))
297
Bram Moolenaar9b9be002020-03-22 14:41:22 +0100298 %bw!
Sean Dewar0fd44a52024-02-20 20:28:15 +0100299 set laststatus&
Bram Moolenaar9b9be002020-03-22 14:41:22 +0100300endfunc
301
Bram Moolenaar4520d442017-03-19 16:09:46 +0100302func Test_window_exchange()
303 e Xa
304
305 " Nothing happens with window exchange when there is 1 window
306 wincmd x
307 call assert_equal(1, winnr('$'))
308
309 split Xb
310 split Xc
311
312 call assert_equal('Xc', bufname(winbufnr(1)))
313 call assert_equal('Xb', bufname(winbufnr(2)))
314 call assert_equal('Xa', bufname(winbufnr(3)))
315
316 " Exchange current window 1 with window 3
317 3wincmd x
318 call assert_equal('Xa', bufname(winbufnr(1)))
319 call assert_equal('Xb', bufname(winbufnr(2)))
320 call assert_equal('Xc', bufname(winbufnr(3)))
321
322 " Exchange window with next when at the top window
323 wincmd x
324 call assert_equal('Xb', bufname(winbufnr(1)))
325 call assert_equal('Xa', bufname(winbufnr(2)))
326 call assert_equal('Xc', bufname(winbufnr(3)))
327
328 " Exchange window with next when at the middle window
329 wincmd j
330 wincmd x
331 call assert_equal('Xb', bufname(winbufnr(1)))
332 call assert_equal('Xc', bufname(winbufnr(2)))
333 call assert_equal('Xa', bufname(winbufnr(3)))
334
335 " Exchange window with next when at the bottom window.
336 " When there is no next window, it exchanges with the previous window.
337 wincmd j
338 wincmd x
339 call assert_equal('Xb', bufname(winbufnr(1)))
340 call assert_equal('Xa', bufname(winbufnr(2)))
341 call assert_equal('Xc', bufname(winbufnr(3)))
342
343 bw Xa Xb Xc
344endfunc
345
346func Test_window_rotate()
347 e Xa
348 split Xb
349 split Xc
350 call assert_equal('Xc', bufname(winbufnr(1)))
351 call assert_equal('Xb', bufname(winbufnr(2)))
352 call assert_equal('Xa', bufname(winbufnr(3)))
353
354 " Rotate downwards
355 wincmd r
356 call assert_equal('Xa', bufname(winbufnr(1)))
357 call assert_equal('Xc', bufname(winbufnr(2)))
358 call assert_equal('Xb', bufname(winbufnr(3)))
359
360 2wincmd r
361 call assert_equal('Xc', bufname(winbufnr(1)))
362 call assert_equal('Xb', bufname(winbufnr(2)))
363 call assert_equal('Xa', bufname(winbufnr(3)))
364
365 " Rotate upwards
366 wincmd R
367 call assert_equal('Xb', bufname(winbufnr(1)))
368 call assert_equal('Xa', bufname(winbufnr(2)))
369 call assert_equal('Xc', bufname(winbufnr(3)))
370
371 2wincmd R
372 call assert_equal('Xc', bufname(winbufnr(1)))
373 call assert_equal('Xb', bufname(winbufnr(2)))
374 call assert_equal('Xa', bufname(winbufnr(3)))
375
376 bot vsplit
377 call assert_fails('wincmd R', 'E443:')
378
379 bw Xa Xb Xc
380endfunc
381
382func Test_window_height()
383 e Xa
384 split Xb
385
386 let [wh1, wh2] = [winheight(1), winheight(2)]
387 " Active window (1) should have the same height or 1 more
388 " than the other window.
389 call assert_inrange(wh2, wh2 + 1, wh1)
390
391 wincmd -
392 call assert_equal(wh1 - 1, winheight(1))
393 call assert_equal(wh2 + 1, winheight(2))
394
395 wincmd +
396 call assert_equal(wh1, winheight(1))
Bram Moolenaarf92e58c2019-09-08 21:51:41 +0200397 call assert_equal(wh2, 2->winheight())
Bram Moolenaar4520d442017-03-19 16:09:46 +0100398
399 2wincmd _
400 call assert_equal(2, winheight(1))
401 call assert_equal(wh1 + wh2 - 2, winheight(2))
402
403 wincmd =
404 call assert_equal(wh1, winheight(1))
405 call assert_equal(wh2, winheight(2))
406
407 2wincmd _
408 set winfixheight
409 split Xc
410 let [wh1, wh2, wh3] = [winheight(1), winheight(2), winheight(3)]
411 call assert_equal(2, winheight(2))
412 call assert_inrange(wh3, wh3 + 1, wh1)
413 3wincmd +
414 call assert_equal(2, winheight(2))
415 call assert_equal(wh1 + 3, winheight(1))
416 call assert_equal(wh3 - 3, winheight(3))
417 wincmd =
418 call assert_equal(2, winheight(2))
419 call assert_equal(wh1, winheight(1))
420 call assert_equal(wh3, winheight(3))
421
422 wincmd j
423 set winfixheight&
424
425 wincmd =
426 let [wh1, wh2, wh3] = [winheight(1), winheight(2), winheight(3)]
427 " Current window (2) should have the same height or 1 more
428 " than the other windows.
429 call assert_inrange(wh1, wh1 + 1, wh2)
430 call assert_inrange(wh3, wh3 + 1, wh2)
431
432 bw Xa Xb Xc
433endfunc
434
Bram Moolenaar21c3a802022-08-31 17:49:14 +0100435func Test_wincmd_equal()
436 edit Xone
437 below split Xtwo
438 rightbelow vsplit Xthree
439 call assert_equal('Xone', bufname(winbufnr(1)))
440 call assert_equal('Xtwo', bufname(winbufnr(2)))
441 call assert_equal('Xthree', bufname(winbufnr(3)))
442
443 " Xone and Xtwo should be about the same height
444 let [wh1, wh2] = [winheight(1), winheight(2)]
445 call assert_inrange(wh1 - 1, wh1 + 1, wh2)
446 " Xtwo and Xthree should be about the same width
447 let [ww2, ww3] = [winwidth(2), winwidth(3)]
448 call assert_inrange(ww2 - 1, ww2 + 1, ww3)
449
450 1wincmd w
451 10wincmd _
452 2wincmd w
453 20wincmd |
454 call assert_equal(10, winheight(1))
455 call assert_equal(20, winwidth(2))
456
457 " equalizing horizontally doesn't change the heights
458 hor wincmd =
459 call assert_equal(10, winheight(1))
460 let [ww2, ww3] = [winwidth(2), winwidth(3)]
461 call assert_inrange(ww2 - 1, ww2 + 1, ww3)
462
463 2wincmd w
464 20wincmd |
465 call assert_equal(20, winwidth(2))
466 " equalizing vertically doesn't change the widths
467 vert wincmd =
468 call assert_equal(20, winwidth(2))
469 let [wh1, wh2] = [winheight(1), winheight(2)]
470 call assert_inrange(wh1 - 1, wh1 + 1, wh2)
471
472 bwipe Xone Xtwo Xthree
473endfunc
474
Bram Moolenaar4520d442017-03-19 16:09:46 +0100475func Test_window_width()
476 e Xa
477 vsplit Xb
478
479 let [ww1, ww2] = [winwidth(1), winwidth(2)]
480 " Active window (1) should have the same width or 1 more
481 " than the other window.
482 call assert_inrange(ww2, ww2 + 1, ww1)
483
484 wincmd <
485 call assert_equal(ww1 - 1, winwidth(1))
486 call assert_equal(ww2 + 1, winwidth(2))
487
488 wincmd >
489 call assert_equal(ww1, winwidth(1))
490 call assert_equal(ww2, winwidth(2))
491
492 2wincmd |
493 call assert_equal(2, winwidth(1))
494 call assert_equal(ww1 + ww2 - 2, winwidth(2))
495
496 wincmd =
497 call assert_equal(ww1, winwidth(1))
498 call assert_equal(ww2, winwidth(2))
499
500 2wincmd |
501 set winfixwidth
502 vsplit Xc
503 let [ww1, ww2, ww3] = [winwidth(1), winwidth(2), winwidth(3)]
Bram Moolenaar38e34832017-03-19 20:22:36 +0100504 call assert_equal(2, winwidth(2))
Bram Moolenaar4520d442017-03-19 16:09:46 +0100505 call assert_inrange(ww3, ww3 + 1, ww1)
506 3wincmd >
Bram Moolenaar38e34832017-03-19 20:22:36 +0100507 call assert_equal(2, winwidth(2))
Bram Moolenaar4520d442017-03-19 16:09:46 +0100508 call assert_equal(ww1 + 3, winwidth(1))
509 call assert_equal(ww3 - 3, winwidth(3))
510 wincmd =
Bram Moolenaar38e34832017-03-19 20:22:36 +0100511 call assert_equal(2, winwidth(2))
Bram Moolenaar4520d442017-03-19 16:09:46 +0100512 call assert_equal(ww1, winwidth(1))
513 call assert_equal(ww3, winwidth(3))
514
515 wincmd l
516 set winfixwidth&
517
518 wincmd =
519 let [ww1, ww2, ww3] = [winwidth(1), winwidth(2), winwidth(3)]
520 " Current window (2) should have the same width or 1 more
521 " than the other windows.
522 call assert_inrange(ww1, ww1 + 1, ww2)
523 call assert_inrange(ww3, ww3 + 1, ww2)
524
Yegappan Lakshmanan59585492021-06-12 13:46:41 +0200525 " when the current window width is less than the new 'winwidth', the current
526 " window width should be increased.
527 enew | only
528 split
529 10vnew
530 set winwidth=15
531 call assert_equal(15, winwidth(0))
532
533 %bw!
Bram Moolenaar4520d442017-03-19 16:09:46 +0100534endfunc
535
Bram Moolenaar8eeeba82017-06-25 22:45:39 +0200536func Test_equalalways_on_close()
537 set equalalways
538 vsplit
539 windo split
Bram Moolenaar1bbb6192018-11-10 16:02:01 +0100540 split
Bram Moolenaar8eeeba82017-06-25 22:45:39 +0200541 wincmd J
542 " now we have a frame top-left with two windows, a frame top-right with two
543 " windows and a frame at the bottom, full-width.
544 let height_1 = winheight(1)
545 let height_2 = winheight(2)
546 let height_3 = winheight(3)
547 let height_4 = winheight(4)
548 " closing the bottom window causes all windows to be resized.
549 close
550 call assert_notequal(height_1, winheight(1))
551 call assert_notequal(height_2, winheight(2))
552 call assert_notequal(height_3, winheight(3))
553 call assert_notequal(height_4, winheight(4))
554 call assert_equal(winheight(1), winheight(3))
555 call assert_equal(winheight(2), winheight(4))
556
557 1wincmd w
558 split
559 4wincmd w
Millyc0cba182024-10-16 20:03:44 +0200560 resize +5
Bram Moolenaar8eeeba82017-06-25 22:45:39 +0200561 " left column has three windows, equalized heights.
562 " right column has two windows, top one a bit higher
563 let height_1 = winheight(1)
564 let height_2 = winheight(2)
565 let height_4 = winheight(4)
566 let height_5 = winheight(5)
567 3wincmd w
568 " closing window in left column equalizes heights in left column but not in
569 " the right column
570 close
571 call assert_notequal(height_1, winheight(1))
572 call assert_notequal(height_2, winheight(2))
573 call assert_equal(height_4, winheight(3))
574 call assert_equal(height_5, winheight(4))
575
576 only
577 set equalalways&
578endfunc
579
Bram Moolenaar22044dc2017-12-02 15:43:37 +0100580func Test_win_screenpos()
Bram Moolenaar5a4c3082019-12-01 15:23:11 +0100581 CheckFeature quickfix
582
Bram Moolenaar22044dc2017-12-02 15:43:37 +0100583 call assert_equal(1, winnr('$'))
584 split
585 vsplit
586 10wincmd _
587 30wincmd |
588 call assert_equal([1, 1], win_screenpos(1))
589 call assert_equal([1, 32], win_screenpos(2))
590 call assert_equal([12, 1], win_screenpos(3))
591 call assert_equal([0, 0], win_screenpos(4))
Bram Moolenaar99fa7212020-04-26 15:59:55 +0200592 call assert_fails('let l = win_screenpos([])', 'E745:')
Bram Moolenaar22044dc2017-12-02 15:43:37 +0100593 only
594endfunc
595
Bram Moolenaar4520d442017-03-19 16:09:46 +0100596func Test_window_jump_tag()
Bram Moolenaar5a4c3082019-12-01 15:23:11 +0100597 CheckFeature quickfix
598
Bram Moolenaar4520d442017-03-19 16:09:46 +0100599 help
600 /iccf
601 call assert_match('^|iccf|', getline('.'))
602 call assert_equal(2, winnr('$'))
603 2wincmd }
604 call assert_equal(3, winnr('$'))
605 call assert_match('^|iccf|', getline('.'))
606 wincmd k
607 call assert_match('\*iccf\*', getline('.'))
608 call assert_equal(2, winheight(0))
609
610 wincmd z
611 set previewheight=4
612 help
613 /bugs
614 wincmd }
615 wincmd k
616 call assert_match('\*bugs\*', getline('.'))
617 call assert_equal(4, winheight(0))
618 set previewheight&
619
620 %bw!
621endfunc
622
623func Test_window_newtab()
624 e Xa
625
626 call assert_equal(1, tabpagenr('$'))
627 call assert_equal("\nAlready only one window", execute('wincmd T'))
628
629 split Xb
630 split Xc
631
632 wincmd T
633 call assert_equal(2, tabpagenr('$'))
634 call assert_equal(['Xb', 'Xa'], map(tabpagebuflist(1), 'bufname(v:val)'))
Bram Moolenaarce90e362019-09-08 18:58:44 +0200635 call assert_equal(['Xc' ], map(2->tabpagebuflist(), 'bufname(v:val)'))
Bram Moolenaar0e05de42020-03-25 22:23:46 +0100636 call assert_equal(['Xc' ], map(tabpagebuflist(), 'bufname(v:val)'))
Bram Moolenaar4520d442017-03-19 16:09:46 +0100637
638 %bw!
639endfunc
640
Bram Moolenaarf79225e2017-03-18 23:11:04 +0100641func Test_next_split_all()
642 " This was causing an illegal memory access.
643 n x
644 norm axxx
645 split
646 split
647 s/x
648 s/x
649 all
650 bwipe!
651endfunc
652
Bram Moolenaar75373f32017-08-07 22:02:30 +0200653" Tests for adjusting window and contents
654func GetScreenStr(row)
655 let str = ""
656 for c in range(1,3)
657 let str .= nr2char(screenchar(a:row, c))
658 endfor
659 return str
660endfunc
661
662func Test_window_contents()
663 enew! | only | new
664 call setline(1, range(1,256))
665
666 exe "norm! \<C-W>t\<C-W>=1Gzt\<C-W>w\<C-W>+"
667 redraw
Bram Moolenaar72cf47a2018-05-10 18:23:29 +0200668 let s3 = GetScreenStr(1)
Bram Moolenaar75373f32017-08-07 22:02:30 +0200669 wincmd p
670 call assert_equal(1, line("w0"))
671 call assert_equal('1 ', s3)
672
673 exe "norm! \<C-W>t\<C-W>=50Gzt\<C-W>w\<C-W>+"
674 redraw
Bram Moolenaar72cf47a2018-05-10 18:23:29 +0200675 let s3 = GetScreenStr(1)
Bram Moolenaar75373f32017-08-07 22:02:30 +0200676 wincmd p
677 call assert_equal(50, line("w0"))
678 call assert_equal('50 ', s3)
679
680 exe "norm! \<C-W>t\<C-W>=59Gzt\<C-W>w\<C-W>+"
681 redraw
Bram Moolenaar72cf47a2018-05-10 18:23:29 +0200682 let s3 = GetScreenStr(1)
Bram Moolenaar75373f32017-08-07 22:02:30 +0200683 wincmd p
684 call assert_equal(59, line("w0"))
685 call assert_equal('59 ', s3)
686
Bram Moolenaar8b633132020-03-20 18:20:51 +0100687 %d
688 call setline(1, ['one', 'two', 'three'])
689 call assert_equal(1, line('w0'))
690 call assert_equal(3, line('w$'))
691
Bram Moolenaar75373f32017-08-07 22:02:30 +0200692 bwipeout!
693 call test_garbagecollect_now()
694endfunc
695
Bram Moolenaar2efb3232017-12-19 12:27:23 +0100696func Test_window_colon_command()
697 " This was reading invalid memory.
698 exe "norm! v\<C-W>:\<C-U>echo v:version"
699endfunc
700
Bram Moolenaar6f361c92018-01-31 19:06:50 +0100701func Test_access_freed_mem()
Bram Moolenaar1417c762019-07-27 17:31:36 +0200702 call assert_equal(&columns, winwidth(0))
Bram Moolenaar9a046fd2021-01-28 13:47:59 +0100703 " This was accessing freed memory (but with what events?)
704 au BufEnter,BufLeave,WinEnter,WinLeave 0 vs xxx
Bram Moolenaar6f361c92018-01-31 19:06:50 +0100705 arg 0
706 argadd
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +0200707 call assert_fails("all", "E242:")
Bram Moolenaar6f361c92018-01-31 19:06:50 +0100708 au!
709 bwipe xxx
Bram Moolenaar1417c762019-07-27 17:31:36 +0200710 call assert_equal(&columns, winwidth(0))
Bram Moolenaar6f361c92018-01-31 19:06:50 +0100711endfunc
712
Bram Moolenaara27e1dc2019-10-07 22:27:36 +0200713func Test_insert_cleared_on_switch_to_term()
714 CheckFeature terminal
715
716 set showmode
717 terminal
718 wincmd p
719
720 call feedkeys("i\<C-O>", 'ntx')
721 redraw
722
723 " The "-- (insert) --" indicator should be visible.
724 let chars = map(range(1, &columns), 'nr2char(screenchar(&lines, v:val))')
725 let str = trim(join(chars, ''))
726 call assert_equal('-- (insert) --', str)
727
728 call feedkeys("\<C-W>p", 'ntx')
729 redraw
730
731 " The "-- (insert) --" indicator should have been cleared.
732 let chars = map(range(1, &columns), 'nr2char(screenchar(&lines, v:val))')
733 let str = trim(join(chars, ''))
734 call assert_equal('', str)
735
736 set showmode&
737 %bw!
738endfunc
739
Bram Moolenaar5bab5552018-04-13 20:41:29 +0200740func Test_visual_cleared_after_window_split()
741 new | only!
742 let smd_save = &showmode
743 set showmode
744 let ls_save = &laststatus
745 set laststatus=1
746 call setline(1, ['a', 'b', 'c', 'd', ''])
747 norm! G
748 exe "norm! kkvk"
749 redraw
750 exe "norm! \<C-W>v"
751 redraw
752 " check if '-- VISUAL --' disappeared from command line
753 let columns = range(1, &columns)
754 let cmdlinechars = map(columns, 'nr2char(screenchar(&lines, v:val))')
755 let cmdline = join(cmdlinechars, '')
756 let cmdline_ltrim = substitute(cmdline, '^\s*', "", "")
757 let mode_shown = substitute(cmdline_ltrim, '\s*$', "", "")
758 call assert_equal('', mode_shown)
759 let &showmode = smd_save
760 let &laststatus = ls_save
761 bwipe!
762endfunc
763
Bram Moolenaar72cf47a2018-05-10 18:23:29 +0200764func Test_winrestcmd()
765 2split
766 3vsplit
Bram Moolenaara0c8aea2021-03-20 19:55:35 +0100767 let restcmd = winrestcmd()
Bram Moolenaar72cf47a2018-05-10 18:23:29 +0200768 call assert_equal(2, winheight(0))
769 call assert_equal(3, winwidth(0))
770 wincmd =
771 call assert_notequal(2, winheight(0))
772 call assert_notequal(3, winwidth(0))
Bram Moolenaara0c8aea2021-03-20 19:55:35 +0100773 exe restcmd
Bram Moolenaar72cf47a2018-05-10 18:23:29 +0200774 call assert_equal(2, winheight(0))
775 call assert_equal(3, winwidth(0))
776 only
Bram Moolenaara0c8aea2021-03-20 19:55:35 +0100777
778 wincmd v
779 wincmd s
780 wincmd v
781 redraw
782 let restcmd = winrestcmd()
783 wincmd _
784 wincmd |
785 exe restcmd
786 redraw
787 call assert_equal(restcmd, winrestcmd())
788
789 only
Bram Moolenaar72cf47a2018-05-10 18:23:29 +0200790endfunc
791
Bram Moolenaar1e115362019-01-09 23:01:02 +0100792func Fun_RenewFile()
Bram Moolenaar026587b2019-08-17 15:08:00 +0200793 " Need to wait a bit for the timestamp to be older.
Bram Moolenaarce90e362019-09-08 18:58:44 +0200794 let old_ftime = getftime("tmp.txt")
795 while getftime("tmp.txt") == old_ftime
796 sleep 100m
797 silent execute '!echo "1" > tmp.txt'
798 endwhile
Bram Moolenaara42df592018-12-24 00:22:39 +0100799 sp
800 wincmd p
801 edit! tmp.txt
Bram Moolenaar1e115362019-01-09 23:01:02 +0100802endfunc
Bram Moolenaara42df592018-12-24 00:22:39 +0100803
804func Test_window_prevwin()
805 " Can we make this work on MS-Windows?
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +0200806 CheckUnix
Bram Moolenaara42df592018-12-24 00:22:39 +0100807
808 set hidden autoread
Bram Moolenaardb4c9472022-10-15 22:06:06 +0100809 call writefile(['2'], 'tmp.txt', 'D')
Bram Moolenaara42df592018-12-24 00:22:39 +0100810 new tmp.txt
811 q
Bram Moolenaara42df592018-12-24 00:22:39 +0100812 call Fun_RenewFile()
813 call assert_equal(2, winnr())
814 wincmd p
815 call assert_equal(1, winnr())
816 wincmd p
817 q
818 call Fun_RenewFile()
819 call assert_equal(2, winnr())
820 wincmd p
821 call assert_equal(1, winnr())
822 wincmd p
823 " reset
824 q
Bram Moolenaara42df592018-12-24 00:22:39 +0100825 set hidden&vim autoread&vim
826 delfunc Fun_RenewFile
827endfunc
828
Bram Moolenaar8fcb60f2019-03-04 13:18:30 +0100829func Test_relative_cursor_position_in_one_line_window()
830 new
831 only
832 call setline(1, range(1, 10000))
833 normal 50%
834 let lnum = getcurpos()[1]
835 split
836 split
837 " make third window take as many lines as possible, other windows will
838 " become one line
839 3wincmd w
840 for i in range(1, &lines - 6)
841 wincmd +
842 redraw!
843 endfor
844
845 " first and second window should show cursor line
846 let wininfo = getwininfo()
847 call assert_equal(lnum, wininfo[0].topline)
848 call assert_equal(lnum, wininfo[1].topline)
849
850 only!
851 bwipe!
Yegappan Lakshmanan04c4c572022-08-30 19:48:24 +0100852 call assert_fails('call winrestview(test_null_dict())', 'E1297:')
Bram Moolenaar8fcb60f2019-03-04 13:18:30 +0100853endfunc
854
855func Test_relative_cursor_position_after_move_and_resize()
856 let so_save = &so
857 set so=0
858 enew
859 call setline(1, range(1, 10000))
860 normal 50%
861 split
862 1wincmd w
863 " Move cursor to first line in window
864 normal H
865 redraw!
866 " Reduce window height to two lines
867 let height = winheight(0)
868 while winheight(0) > 2
869 wincmd -
870 redraw!
871 endwhile
872 " move cursor to second/last line in window
873 normal j
874 " restore previous height
875 while winheight(0) < height
876 wincmd +
877 redraw!
878 endwhile
879 " make window two lines again
880 while winheight(0) > 2
881 wincmd -
882 redraw!
883 endwhile
884
885 " cursor should be at bottom line
886 let info = getwininfo(win_getid())[0]
887 call assert_equal(info.topline + 1, getcurpos()[1])
888
889 only!
890 bwipe!
891 let &so = so_save
892endfunc
893
894func Test_relative_cursor_position_after_resize()
895 let so_save = &so
896 set so=0
897 enew
898 call setline(1, range(1, 10000))
899 normal 50%
900 split
901 1wincmd w
902 let winid1 = win_getid()
903 let info = getwininfo(winid1)[0]
904 " Move cursor to second line in window
905 exe "normal " . (info.topline + 1) . "G"
906 redraw!
907 let lnum = getcurpos()[1]
908
909 " Make the window only two lines high, cursor should end up in top line
910 2wincmd w
911 exe (info.height - 2) . "wincmd +"
912 redraw!
913 let info = getwininfo(winid1)[0]
914 call assert_equal(lnum, info.topline)
915
916 only!
917 bwipe!
918 let &so = so_save
919endfunc
920
921func Test_relative_cursor_second_line_after_resize()
922 let so_save = &so
923 set so=0
924 enew
925 call setline(1, range(1, 10000))
926 normal 50%
927 split
928 1wincmd w
929 let winid1 = win_getid()
930 let info = getwininfo(winid1)[0]
931
932 " Make the window only two lines high
933 2wincmd _
934
935 " Move cursor to second line in window
936 normal H
937 normal j
938
939 " Make window size bigger, then back to 2 lines
940 for i in range(1, 10)
941 wincmd +
942 redraw!
943 endfor
944 for i in range(1, 10)
945 wincmd -
946 redraw!
947 endfor
948
949 " cursor should end up in bottom line
950 let info = getwininfo(winid1)[0]
951 call assert_equal(info.topline + 1, getcurpos()[1])
952
953 only!
954 bwipe!
955 let &so = so_save
956endfunc
957
Bram Moolenaara9b25352019-05-12 14:25:30 +0200958func Test_split_noscroll()
959 let so_save = &so
Bram Moolenaarbd2d68c2019-05-18 15:36:11 +0200960 enew
961 call setline(1, range(1, 8))
Bram Moolenaara9b25352019-05-12 14:25:30 +0200962 normal 100%
963 split
964
965 1wincmd w
966 let winid1 = win_getid()
967 let info1 = getwininfo(winid1)[0]
968
969 2wincmd w
970 let winid2 = win_getid()
971 let info2 = getwininfo(winid2)[0]
972
973 call assert_equal(1, info1.topline)
974 call assert_equal(1, info2.topline)
975
Bram Moolenaarbd2d68c2019-05-18 15:36:11 +0200976 " window that fits all lines by itself, but not when split: closing other
977 " window should restore fraction.
Bram Moolenaara9b25352019-05-12 14:25:30 +0200978 only!
Bram Moolenaarbd2d68c2019-05-18 15:36:11 +0200979 call setline(1, range(1, &lines - 10))
980 exe &lines / 4
981 let winid1 = win_getid()
982 let info1 = getwininfo(winid1)[0]
983 call assert_equal(1, info1.topline)
984 new
985 redraw
986 close
987 let info1 = getwininfo(winid1)[0]
988 call assert_equal(1, info1.topline)
989
Bram Moolenaara9b25352019-05-12 14:25:30 +0200990 bwipe!
991 let &so = so_save
992endfunc
993
Bram Moolenaar46ad2882019-04-08 20:01:47 +0200994" Tests for the winnr() function
995func Test_winnr()
996 only | tabonly
997 call assert_equal(1, winnr('j'))
998 call assert_equal(1, winnr('k'))
999 call assert_equal(1, winnr('h'))
1000 call assert_equal(1, winnr('l'))
1001
1002 " create a set of horizontally and vertically split windows
1003 leftabove new | wincmd p
1004 leftabove new | wincmd p
1005 rightbelow new | wincmd p
1006 rightbelow new | wincmd p
1007 leftabove vnew | wincmd p
1008 leftabove vnew | wincmd p
1009 rightbelow vnew | wincmd p
1010 rightbelow vnew | wincmd p
1011
1012 call assert_equal(8, winnr('j'))
1013 call assert_equal(2, winnr('k'))
1014 call assert_equal(4, winnr('h'))
1015 call assert_equal(6, winnr('l'))
1016 call assert_equal(9, winnr('2j'))
1017 call assert_equal(1, winnr('2k'))
1018 call assert_equal(3, winnr('2h'))
1019 call assert_equal(7, winnr('2l'))
1020
1021 " Error cases
1022 call assert_fails("echo winnr('0.2k')", 'E15:')
1023 call assert_equal(2, winnr('-2k'))
1024 call assert_fails("echo winnr('-2xj')", 'E15:')
1025 call assert_fails("echo winnr('j2j')", 'E15:')
1026 call assert_fails("echo winnr('ll')", 'E15:')
1027 call assert_fails("echo winnr('5')", 'E15:')
1028 call assert_equal(4, winnr('0h'))
Bram Moolenaar99fa7212020-04-26 15:59:55 +02001029 call assert_fails("let w = winnr([])", 'E730:')
1030 call assert_equal('unknown', win_gettype(-1))
1031 call assert_equal(-1, winheight(-1))
1032 call assert_equal(-1, winwidth(-1))
Bram Moolenaar46ad2882019-04-08 20:01:47 +02001033
1034 tabnew
1035 call assert_equal(8, tabpagewinnr(1, 'j'))
Bram Moolenaarce90e362019-09-08 18:58:44 +02001036 call assert_equal(2, 1->tabpagewinnr('k'))
Bram Moolenaar46ad2882019-04-08 20:01:47 +02001037 call assert_equal(4, tabpagewinnr(1, 'h'))
1038 call assert_equal(6, tabpagewinnr(1, 'l'))
1039
1040 only | tabonly
1041endfunc
1042
Bram Moolenaarf92e58c2019-09-08 21:51:41 +02001043func Test_winrestview()
1044 split runtest.vim
1045 normal 50%
1046 let view = winsaveview()
1047 close
1048 split runtest.vim
1049 eval view->winrestview()
1050 call assert_equal(view, winsaveview())
1051
1052 bwipe!
Yegappan Lakshmanan04c4c572022-08-30 19:48:24 +01001053 call assert_fails('call winrestview(test_null_dict())', 'E1297:')
Bram Moolenaarf92e58c2019-09-08 21:51:41 +02001054endfunc
1055
Bram Moolenaard20dcb32019-09-10 21:22:58 +02001056func Test_win_splitmove()
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01001057 CheckFeature quickfix
1058
Bram Moolenaard20dcb32019-09-10 21:22:58 +02001059 edit a
1060 leftabove split b
1061 leftabove vsplit c
1062 leftabove split d
Sean Dewar96cc4ae2024-02-20 21:52:31 +01001063
1064 " win_splitmove doesn't actually create or close any windows, so expect an
1065 " unchanged winid and no WinNew/WinClosed events, like :wincmd H/J/K/L.
1066 let s:triggered = []
1067 augroup WinSplitMove
1068 au!
1069 au WinNewPre * let s:triggered += ['WinNewPre']
1070 au WinNew * let s:triggered += ['WinNew', win_getid()]
1071 au WinClosed * let s:triggered += ['WinClosed', str2nr(expand('<afile>'))]
1072 augroup END
1073 let winid = win_getid()
1074
Bram Moolenaard20dcb32019-09-10 21:22:58 +02001075 call assert_equal(0, win_splitmove(winnr(), winnr('l')))
1076 call assert_equal(bufname(winbufnr(1)), 'c')
1077 call assert_equal(bufname(winbufnr(2)), 'd')
1078 call assert_equal(bufname(winbufnr(3)), 'b')
1079 call assert_equal(bufname(winbufnr(4)), 'a')
1080 call assert_equal(0, win_splitmove(winnr(), winnr('j'), {'vertical': 1}))
1081 call assert_equal(0, win_splitmove(winnr(), winnr('j'), {'vertical': 1}))
1082 call assert_equal(bufname(winbufnr(1)), 'c')
1083 call assert_equal(bufname(winbufnr(2)), 'b')
1084 call assert_equal(bufname(winbufnr(3)), 'd')
1085 call assert_equal(bufname(winbufnr(4)), 'a')
1086 call assert_equal(0, win_splitmove(winnr(), winnr('k'), {'vertical': 1}))
1087 call assert_equal(bufname(winbufnr(1)), 'd')
1088 call assert_equal(bufname(winbufnr(2)), 'c')
1089 call assert_equal(bufname(winbufnr(3)), 'b')
1090 call assert_equal(bufname(winbufnr(4)), 'a')
1091 call assert_equal(0, win_splitmove(winnr(), winnr('j'), {'rightbelow': v:true}))
1092 call assert_equal(bufname(winbufnr(1)), 'c')
1093 call assert_equal(bufname(winbufnr(2)), 'b')
1094 call assert_equal(bufname(winbufnr(3)), 'a')
1095 call assert_equal(bufname(winbufnr(4)), 'd')
Yegappan Lakshmanan04c4c572022-08-30 19:48:24 +01001096 call assert_fails('call win_splitmove(winnr(), winnr("k"), test_null_dict())', 'E1297:')
Sean Dewar96cc4ae2024-02-20 21:52:31 +01001097 call assert_equal([], s:triggered)
1098 call assert_equal(winid, win_getid())
1099
1100 unlet! s:triggered
1101 au! WinSplitMove
Bram Moolenaard20dcb32019-09-10 21:22:58 +02001102 only | bd
1103
1104 call assert_fails('call win_splitmove(winnr(), 123)', 'E957:')
1105 call assert_fails('call win_splitmove(123, winnr())', 'E957:')
1106 call assert_fails('call win_splitmove(winnr(), winnr())', 'E957:')
Bram Moolenaar7b94e772020-01-06 21:03:24 +01001107
1108 tabnew
1109 call assert_fails('call win_splitmove(1, win_getid(1, 1))', 'E957:')
1110 tabclose
Sean Dewar0fd44a52024-02-20 20:28:15 +01001111
1112 split
1113 augroup WinSplitMove
1114 au!
1115 au WinEnter * ++once call win_gotoid(win_getid(winnr('#')))
1116 augroup END
1117 call assert_fails('call win_splitmove(winnr(), winnr("#"))', 'E855:')
1118
1119 augroup WinSplitMove
1120 au!
1121 au WinLeave * ++once quit
1122 augroup END
1123 call assert_fails('call win_splitmove(winnr(), winnr("#"))', 'E855:')
1124
1125 split
1126 split
1127 augroup WinSplitMove
1128 au!
1129 au WinEnter * ++once let s:triggered = v:true
Sean Dewar5cac1a92024-03-12 21:11:39 +01001130 \| call assert_fails('call win_splitmove(winnr(), winnr("$"))', 'E242:')
Sean Dewar0fd44a52024-02-20 20:28:15 +01001131 \| call assert_fails('call win_splitmove(winnr("$"), winnr())', 'E242:')
1132 augroup END
1133 quit
1134 call assert_equal(v:true, s:triggered)
1135 unlet! s:triggered
1136
1137 new
1138 augroup WinSplitMove
1139 au!
1140 au BufHidden * ++once let s:triggered = v:true
Sean Dewar5cac1a92024-03-12 21:11:39 +01001141 \| call assert_fails('call win_splitmove(winnr(), winnr("#"))', 'E1159:')
Sean Dewar0fd44a52024-02-20 20:28:15 +01001142 augroup END
1143 hide
1144 call assert_equal(v:true, s:triggered)
1145 unlet! s:triggered
1146
Sean Dewarabf70302024-02-24 10:20:24 +01001147 split
1148 let close_win = winnr('#')
1149 augroup WinSplitMove
1150 au!
1151 au WinEnter * ++once quit!
1152 augroup END
1153 call win_splitmove(close_win, winnr())
1154 call assert_equal(0, win_id2win(close_win))
1155
Sean Dewar0fd44a52024-02-20 20:28:15 +01001156 au! WinSplitMove
1157 augroup! WinSplitMove
1158 %bw!
Bram Moolenaard20dcb32019-09-10 21:22:58 +02001159endfunc
1160
Bram Moolenaare20b9ec2020-02-03 21:40:04 +01001161" Test for the :only command
1162func Test_window_only()
1163 new
1164 set modified
1165 new
1166 call assert_fails('only', 'E445:')
1167 only!
Bram Moolenaarbc2b71d2020-02-17 21:33:30 +01001168 " Test for :only with a count
1169 let wid = win_getid()
1170 new
1171 new
1172 3only
1173 call assert_equal(1, winnr('$'))
1174 call assert_equal(wid, win_getid())
1175 call assert_fails('close', 'E444:')
1176 call assert_fails('%close', 'E16:')
Bram Moolenaare20b9ec2020-02-03 21:40:04 +01001177endfunc
1178
Bram Moolenaar9f6277b2020-02-11 22:04:02 +01001179" Test for errors with :wincmd
1180func Test_wincmd_errors()
1181 call assert_fails('wincmd g', 'E474:')
1182 call assert_fails('wincmd ab', 'E474:')
1183endfunc
1184
1185" Test for errors with :winpos
1186func Test_winpos_errors()
1187 if !has("gui_running") && !has('win32')
1188 call assert_fails('winpos', 'E188:')
1189 endif
1190 call assert_fails('winpos 10', 'E466:')
1191endfunc
1192
Bram Moolenaar406cd902020-02-18 21:54:41 +01001193" Test for +cmd in a :split command
1194func Test_split_cmd()
1195 split +set\ readonly
1196 call assert_equal(1, &readonly)
1197 call assert_equal(2, winnr('$'))
1198 close
1199endfunc
1200
Bram Moolenaar5080b0a2020-03-22 21:23:47 +01001201" Create maximum number of horizontally or vertically split windows and then
1202" run commands that create a new horizontally/vertically split window
1203func Run_noroom_for_newwindow_test(dir_arg)
1204 let dir = (a:dir_arg == 'v') ? 'vert ' : ''
1205
1206 " Open as many windows as possible
Bram Moolenaarab505b12020-03-23 19:28:44 +01001207 while v:true
Bram Moolenaar5080b0a2020-03-22 21:23:47 +01001208 try
1209 exe dir . 'new'
1210 catch /E36:/
1211 break
1212 endtry
Bram Moolenaarab505b12020-03-23 19:28:44 +01001213 endwhile
Bram Moolenaar5080b0a2020-03-22 21:23:47 +01001214
Bram Moolenaardb4c9472022-10-15 22:06:06 +01001215 call writefile(['first', 'second', 'third'], 'Xnorfile1', 'D')
1216 call writefile([], 'Xnorfile2', 'D')
1217 call writefile([], 'Xnorfile3', 'D')
Bram Moolenaar5080b0a2020-03-22 21:23:47 +01001218
1219 " Argument list related commands
Bram Moolenaarb18b4962022-09-02 21:55:50 +01001220 args Xnorfile1 Xnorfile2 Xnorfile3
Bram Moolenaar5080b0a2020-03-22 21:23:47 +01001221 next
1222 for cmd in ['sargument 2', 'snext', 'sprevious', 'sNext', 'srewind',
1223 \ 'sfirst', 'slast']
1224 call assert_fails(dir .. cmd, 'E36:')
1225 endfor
1226 %argdelete
1227
1228 " Buffer related commands
1229 set modified
1230 hide enew
Bram Moolenaarb18b4962022-09-02 21:55:50 +01001231 for cmd in ['sbuffer Xnorfile1', 'sbnext', 'sbprevious', 'sbNext', 'sbrewind',
Bram Moolenaar5080b0a2020-03-22 21:23:47 +01001232 \ 'sbfirst', 'sblast', 'sball', 'sbmodified', 'sunhide']
1233 call assert_fails(dir .. cmd, 'E36:')
1234 endfor
1235
1236 " Window related commands
Bram Moolenaarb18b4962022-09-02 21:55:50 +01001237 for cmd in ['split', 'split Xnorfile2', 'new', 'new Xnorfile3', 'sview Xnorfile1',
Bram Moolenaar5080b0a2020-03-22 21:23:47 +01001238 \ 'sfind runtest.vim']
1239 call assert_fails(dir .. cmd, 'E36:')
1240 endfor
1241
1242 " Help
1243 call assert_fails(dir .. 'help', 'E36:')
1244 call assert_fails(dir .. 'helpgrep window', 'E36:')
1245
1246 " Command-line window
1247 if a:dir_arg == 'h'
1248 " Cmd-line window is always a horizontally split window
1249 call assert_beeps('call feedkeys("q:\<CR>", "xt")')
1250 endif
1251
1252 " Quickfix and location list window
1253 if has('quickfix')
1254 cexpr ''
1255 call assert_fails(dir .. 'copen', 'E36:')
1256 lexpr ''
1257 call assert_fails(dir .. 'lopen', 'E36:')
1258
1259 " Preview window
Bram Moolenaarb18b4962022-09-02 21:55:50 +01001260 call assert_fails(dir .. 'pedit Xnorfile2', 'E36:')
Yinzuo Jianga2a2fe82024-12-16 21:22:09 +01001261 call assert_fails(dir .. 'pbuffer', 'E36:')
Bram Moolenaar5080b0a2020-03-22 21:23:47 +01001262 call setline(1, 'abc')
1263 call assert_fails(dir .. 'psearch abc', 'E36:')
1264 endif
1265
1266 " Window commands (CTRL-W ^ and CTRL-W f)
1267 if a:dir_arg == 'h'
1268 call assert_fails('call feedkeys("\<C-W>^", "xt")', 'E36:')
Bram Moolenaarb18b4962022-09-02 21:55:50 +01001269 call setline(1, 'Xnorfile1')
Bram Moolenaar5080b0a2020-03-22 21:23:47 +01001270 call assert_fails('call feedkeys("gg\<C-W>f", "xt")', 'E36:')
1271 endif
1272 enew!
1273
1274 " Tag commands (:stag, :stselect and :stjump)
1275 call writefile(["!_TAG_FILE_ENCODING\tutf-8\t//",
Bram Moolenaarb18b4962022-09-02 21:55:50 +01001276 \ "second\tXnorfile1\t2",
1277 \ "third\tXnorfile1\t3",],
Bram Moolenaar5080b0a2020-03-22 21:23:47 +01001278 \ 'Xtags')
1279 set tags=Xtags
1280 call assert_fails(dir .. 'stag second', 'E36:')
1281 call assert_fails('call feedkeys(":" .. dir .. "stselect second\n1\n", "xt")', 'E36:')
1282 call assert_fails(dir .. 'stjump second', 'E36:')
1283 call assert_fails(dir .. 'ptag second', 'E36:')
1284 set tags&
1285 call delete('Xtags')
1286
1287 " :isplit and :dsplit
1288 call setline(1, ['#define FOO 1', 'FOO'])
1289 normal 2G
1290 call assert_fails(dir .. 'isplit FOO', 'E36:')
1291 call assert_fails(dir .. 'dsplit FOO', 'E36:')
1292
1293 " terminal
1294 if has('terminal')
1295 call assert_fails(dir .. 'terminal', 'E36:')
1296 endif
1297
1298 %bwipe!
Bram Moolenaar5080b0a2020-03-22 21:23:47 +01001299 only
1300endfunc
1301
1302func Test_split_cmds_with_no_room()
1303 call Run_noroom_for_newwindow_test('h')
1304 call Run_noroom_for_newwindow_test('v')
1305endfunc
1306
Bram Moolenaarad48e6c2020-04-21 22:19:45 +02001307" Test for various wincmd failures
1308func Test_wincmd_fails()
1309 only!
1310 call assert_beeps("normal \<C-W>w")
1311 call assert_beeps("normal \<C-W>p")
1312 call assert_beeps("normal \<C-W>gk")
1313 call assert_beeps("normal \<C-W>r")
1314 call assert_beeps("normal \<C-W>K")
1315 call assert_beeps("normal \<C-W>H")
1316 call assert_beeps("normal \<C-W>2gt")
1317endfunc
1318
Bram Moolenaarfe6dce82020-09-04 14:41:21 +02001319func Test_window_resize()
1320 " Vertical :resize (absolute, relative, min and max size).
1321 vsplit
1322 vert resize 8
1323 call assert_equal(8, winwidth(0))
1324 vert resize +2
1325 call assert_equal(10, winwidth(0))
1326 vert resize -2
1327 call assert_equal(8, winwidth(0))
1328 vert resize
1329 call assert_equal(&columns - 2, winwidth(0))
1330 vert resize 0
1331 call assert_equal(1, winwidth(0))
1332 vert resize 99999
1333 call assert_equal(&columns - 2, winwidth(0))
1334
1335 %bwipe!
1336
1337 " Horizontal :resize (with absolute, relative size, min and max size).
1338 split
1339 resize 8
1340 call assert_equal(8, winheight(0))
1341 resize +2
1342 call assert_equal(10, winheight(0))
1343 resize -2
1344 call assert_equal(8, winheight(0))
1345 resize
1346 call assert_equal(&lines - 4, winheight(0))
1347 resize 0
1348 call assert_equal(1, winheight(0))
1349 resize 99999
1350 call assert_equal(&lines - 4, winheight(0))
1351
1352 " :resize with explicit window number.
1353 let other_winnr = winnr('j')
1354 exe other_winnr .. 'resize 10'
1355 call assert_equal(10, winheight(other_winnr))
1356 call assert_equal(&lines - 10 - 3, winheight(0))
Bram Moolenaar9668cc52020-10-17 17:39:55 +02001357 exe other_winnr .. 'resize +1'
1358 exe other_winnr .. 'resize +1'
1359 call assert_equal(12, winheight(other_winnr))
1360 call assert_equal(&lines - 10 - 3 -2, winheight(0))
Bram Moolenaar89015a62020-12-29 12:46:51 +01001361 close
1362
1363 vsplit
1364 wincmd l
1365 let other_winnr = winnr('h')
1366 call assert_notequal(winnr(), other_winnr)
Bram Moolenaar5efe0e52021-01-01 14:31:34 +01001367 exe 'vert ' .. other_winnr .. 'resize -' .. &columns
Bram Moolenaar89015a62020-12-29 12:46:51 +01001368 call assert_equal(0, winwidth(other_winnr))
Bram Moolenaarfe6dce82020-09-04 14:41:21 +02001369
1370 %bwipe!
1371endfunc
1372
Bram Moolenaar5d3c9f82020-06-26 20:41:39 +02001373" Test for adjusting the window width when a window is closed with some
1374" windows using 'winfixwidth'
1375func Test_window_width_adjust()
1376 only
1377 " Three vertical windows. Windows 1 and 2 have 'winfixwidth' set and close
1378 " window 2.
1379 wincmd v
1380 vert resize 10
1381 set winfixwidth
1382 wincmd v
1383 set winfixwidth
1384 wincmd c
1385 call assert_inrange(10, 12, winwidth(1))
1386 " Three vertical windows. Windows 2 and 3 have 'winfixwidth' set and close
1387 " window 3.
1388 only
1389 set winfixwidth
1390 wincmd v
1391 vert resize 10
1392 set winfixwidth
1393 wincmd v
1394 set nowinfixwidth
1395 wincmd b
1396 wincmd c
1397 call assert_inrange(10, 12, winwidth(2))
1398
1399 new | only
1400endfunc
1401
1402" Test for jumping to a vertical/horizontal neighbor window based on the
1403" current cursor position
dundargocc57b5bc2022-11-02 13:30:51 +00001404func Test_window_goto_neighbor()
Bram Moolenaar5d3c9f82020-06-26 20:41:39 +02001405 %bw!
1406
1407 " Vertical window movement
1408
1409 " create the following window layout:
1410 " +--+--+
1411 " |w1|w3|
1412 " +--+ |
1413 " |w2| |
1414 " +--+--+
1415 " |w4 |
1416 " +-----+
1417 new
1418 vsplit
1419 split
1420 " vertically jump from w4
1421 wincmd b
1422 call setline(1, repeat(' ', &columns))
1423 call cursor(1, 1)
1424 wincmd k
1425 call assert_equal(2, winnr())
1426 wincmd b
1427 call cursor(1, &columns)
1428 redraw!
1429 wincmd k
1430 call assert_equal(3, winnr())
1431 %bw!
1432
1433 " create the following window layout:
1434 " +--+--+--+
1435 " |w1|w2|w3|
1436 " +--+--+--+
1437 " |w4 |
1438 " +--------+
1439 new
1440 vsplit
1441 vsplit
1442 wincmd b
1443 call setline(1, repeat(' ', &columns))
1444 call cursor(1, 1)
1445 wincmd k
1446 call assert_equal(1, winnr())
1447 wincmd b
1448 call cursor(1, &columns / 2)
1449 redraw!
1450 wincmd k
1451 call assert_equal(2, winnr())
1452 wincmd b
1453 call cursor(1, &columns)
1454 redraw!
1455 wincmd k
1456 call assert_equal(3, winnr())
1457 %bw!
1458
1459 " Horizontal window movement
1460
1461 " create the following window layout:
1462 " +--+--+--+
1463 " |w1|w2|w4|
1464 " +--+--+ |
1465 " |w3 | |
1466 " +-----+--+
1467 vsplit
1468 split
1469 vsplit
1470 4wincmd l
1471 call setline(1, repeat([' '], &lines))
1472 call cursor(1, 1)
1473 redraw!
1474 wincmd h
1475 call assert_equal(2, winnr())
1476 4wincmd l
1477 call cursor(&lines, 1)
1478 redraw!
1479 wincmd h
1480 call assert_equal(3, winnr())
1481 %bw!
1482
1483 " create the following window layout:
1484 " +--+--+
1485 " |w1|w4|
1486 " +--+ +
1487 " |w2| |
1488 " +--+ +
1489 " |w3| |
1490 " +--+--+
1491 vsplit
1492 split
1493 split
1494 wincmd l
1495 call setline(1, repeat([' '], &lines))
1496 call cursor(1, 1)
1497 redraw!
1498 wincmd h
1499 call assert_equal(1, winnr())
1500 wincmd l
1501 call cursor(&lines / 2, 1)
1502 redraw!
1503 wincmd h
1504 call assert_equal(2, winnr())
1505 wincmd l
1506 call cursor(&lines, 1)
1507 redraw!
1508 wincmd h
1509 call assert_equal(3, winnr())
1510 %bw!
1511endfunc
1512
1513" Test for an autocmd closing the destination window when jumping from one
1514" window to another.
1515func Test_close_dest_window()
1516 split
Bram Moolenaarb18b4962022-09-02 21:55:50 +01001517 edit Xdstfile
Bram Moolenaar5d3c9f82020-06-26 20:41:39 +02001518
1519 " Test for BufLeave
1520 augroup T1
1521 au!
Bram Moolenaarb18b4962022-09-02 21:55:50 +01001522 au BufLeave Xdstfile $wincmd c
Bram Moolenaar5d3c9f82020-06-26 20:41:39 +02001523 augroup END
1524 wincmd b
1525 call assert_equal(1, winnr('$'))
Bram Moolenaarb18b4962022-09-02 21:55:50 +01001526 call assert_equal('Xdstfile', @%)
Bram Moolenaar5d3c9f82020-06-26 20:41:39 +02001527 augroup T1
1528 au!
1529 augroup END
1530
1531 " Test for WinLeave
1532 new
1533 wincmd p
1534 augroup T1
1535 au!
1536 au WinLeave * 1wincmd c
1537 augroup END
1538 wincmd t
1539 call assert_equal(1, winnr('$'))
Bram Moolenaarb18b4962022-09-02 21:55:50 +01001540 call assert_equal('Xdstfile', @%)
Bram Moolenaar5d3c9f82020-06-26 20:41:39 +02001541 augroup T1
1542 au!
1543 augroup END
1544 augroup! T1
1545 %bw!
1546endfunc
1547
Bram Moolenaard0fb9072021-12-09 11:57:22 +00001548func Test_window_minimal_size()
1549 set winminwidth=0 winminheight=0
1550
1551 " check size is fixed vertically
1552 new
1553 call win_execute(win_getid(2), 'wincmd _')
1554 call assert_equal(0, winheight(0))
1555 call feedkeys('0', 'tx')
1556 call assert_equal(1, winheight(0))
1557 bwipe!
1558
1559 " check size is fixed horizontally
1560 vert new
1561 call win_execute(win_getid(2), 'wincmd |')
1562 call assert_equal(0, winwidth(0))
1563 call feedkeys('0', 'tx')
1564 call assert_equal(1, winwidth(0))
1565 bwipe!
1566
1567 if has('timers')
1568 " check size is fixed in Insert mode
ichizokfa9a8e02021-12-12 16:42:09 +00001569 func s:CheckSize(timer) abort
1570 call win_execute(win_getid(2), 'wincmd _')
1571 call assert_equal(0, winheight(0))
1572 call feedkeys(" \<Esc>", 't!')
1573 endfunc
Bram Moolenaard0fb9072021-12-09 11:57:22 +00001574 new
ichizokfa9a8e02021-12-12 16:42:09 +00001575 call timer_start(100, function('s:CheckSize'))
Bram Moolenaard0fb9072021-12-09 11:57:22 +00001576 call feedkeys('a', 'tx!')
1577 call assert_equal(1, winheight(0))
1578 bwipe!
ichizokfa9a8e02021-12-12 16:42:09 +00001579 delfunc s:CheckSize
Bram Moolenaard0fb9072021-12-09 11:57:22 +00001580 endif
1581
1582 set winminwidth& winminheight&
1583endfunc
1584
Daniel Steinbergee630312022-01-10 13:36:34 +00001585func Test_win_move_separator()
1586 edit a
1587 leftabove vsplit b
1588 let w = winwidth(0)
1589 " check win_move_separator from left window on left window
1590 call assert_equal(1, winnr())
1591 for offset in range(5)
1592 call assert_true(win_move_separator(0, offset))
1593 call assert_equal(w + offset, winwidth(0))
1594 call assert_true(0->win_move_separator(-offset))
1595 call assert_equal(w, winwidth(0))
1596 endfor
1597 " check win_move_separator from right window on left window number
1598 wincmd l
1599 call assert_notequal(1, winnr())
1600 for offset in range(5)
1601 call assert_true(1->win_move_separator(offset))
1602 call assert_equal(w + offset, winwidth(1))
1603 call assert_true(win_move_separator(1, -offset))
1604 call assert_equal(w, winwidth(1))
1605 endfor
1606 " check win_move_separator from right window on left window ID
1607 let id = win_getid(1)
1608 for offset in range(5)
1609 call assert_true(win_move_separator(id, offset))
1610 call assert_equal(w + offset, winwidth(id))
1611 call assert_true(id->win_move_separator(-offset))
1612 call assert_equal(w, winwidth(id))
1613 endfor
zeertzjqa0c4e2f2022-01-29 10:59:53 +00001614 " check win_move_separator from right window on right window is no-op
1615 let w0 = winwidth(0)
1616 call assert_true(win_move_separator(0, 1))
1617 call assert_equal(w0, winwidth(0))
1618 call assert_true(win_move_separator(0, -1))
1619 call assert_equal(w0, winwidth(0))
zeertzjq873f41a2022-11-01 11:44:43 +00001620
Daniel Steinbergee630312022-01-10 13:36:34 +00001621 " check that win_move_separator doesn't error with offsets beyond moving
1622 " possibility
1623 call assert_true(win_move_separator(id, 5000))
1624 call assert_true(winwidth(id) > w)
1625 call assert_true(win_move_separator(id, -5000))
1626 call assert_true(winwidth(id) < w)
zeertzjq873f41a2022-11-01 11:44:43 +00001627
Daniel Steinbergee630312022-01-10 13:36:34 +00001628 " check that win_move_separator returns false for an invalid window
1629 wincmd =
1630 let w = winwidth(0)
1631 call assert_false(win_move_separator(-1, 1))
1632 call assert_equal(w, winwidth(0))
zeertzjq873f41a2022-11-01 11:44:43 +00001633
Daniel Steinbergee630312022-01-10 13:36:34 +00001634 " check that win_move_separator returns false for a popup window
1635 let id = popup_create(['hello', 'world'], {})
1636 let w = winwidth(id)
1637 call assert_false(win_move_separator(id, 1))
1638 call assert_equal(w, winwidth(id))
1639 call popup_close(id)
zeertzjq873f41a2022-11-01 11:44:43 +00001640
1641 " check that using another tabpage fails without crash
1642 let id = win_getid()
1643 tabnew
1644 call assert_fails('call win_move_separator(id, -1)', 'E1308:')
1645 tabclose
1646
Daniel Steinbergee630312022-01-10 13:36:34 +00001647 %bwipe!
1648endfunc
1649
1650func Test_win_move_statusline()
1651 edit a
1652 leftabove split b
1653 let h = winheight(0)
1654 " check win_move_statusline from top window on top window
1655 call assert_equal(1, winnr())
1656 for offset in range(5)
1657 call assert_true(win_move_statusline(0, offset))
1658 call assert_equal(h + offset, winheight(0))
1659 call assert_true(0->win_move_statusline(-offset))
1660 call assert_equal(h, winheight(0))
1661 endfor
1662 " check win_move_statusline from bottom window on top window number
1663 wincmd j
1664 call assert_notequal(1, winnr())
1665 for offset in range(5)
1666 call assert_true(1->win_move_statusline(offset))
1667 call assert_equal(h + offset, winheight(1))
1668 call assert_true(win_move_statusline(1, -offset))
1669 call assert_equal(h, winheight(1))
1670 endfor
zeertzjqa0c4e2f2022-01-29 10:59:53 +00001671 " check win_move_statusline from bottom window on bottom window
1672 let h0 = winheight(0)
1673 for offset in range(5)
1674 call assert_true(0->win_move_statusline(-offset))
1675 call assert_equal(h0 - offset, winheight(0))
1676 call assert_equal(1 + offset, &cmdheight)
1677 call assert_true(win_move_statusline(0, offset))
1678 call assert_equal(h0, winheight(0))
1679 call assert_equal(1, &cmdheight)
1680 endfor
1681 call assert_true(win_move_statusline(0, 1))
Bram Moolenaara2a89732022-08-31 14:46:18 +01001682 call assert_equal(h0, winheight(0))
1683 call assert_equal(1, &cmdheight)
Daniel Steinbergee630312022-01-10 13:36:34 +00001684 " check win_move_statusline from bottom window on top window ID
1685 let id = win_getid(1)
1686 for offset in range(5)
1687 call assert_true(win_move_statusline(id, offset))
1688 call assert_equal(h + offset, winheight(id))
1689 call assert_true(id->win_move_statusline(-offset))
1690 call assert_equal(h, winheight(id))
1691 endfor
Bram Moolenaar86e67172022-10-31 12:24:12 +00001692
Daniel Steinbergee630312022-01-10 13:36:34 +00001693 " check that win_move_statusline doesn't error with offsets beyond moving
1694 " possibility
1695 call assert_true(win_move_statusline(id, 5000))
1696 call assert_true(winheight(id) > h)
1697 call assert_true(win_move_statusline(id, -5000))
1698 call assert_true(winheight(id) < h)
Bram Moolenaar86e67172022-10-31 12:24:12 +00001699
Daniel Steinbergee630312022-01-10 13:36:34 +00001700 " check that win_move_statusline returns false for an invalid window
1701 wincmd =
1702 let h = winheight(0)
1703 call assert_false(win_move_statusline(-1, 1))
1704 call assert_equal(h, winheight(0))
Bram Moolenaar86e67172022-10-31 12:24:12 +00001705
Daniel Steinbergee630312022-01-10 13:36:34 +00001706 " check that win_move_statusline returns false for a popup window
1707 let id = popup_create(['hello', 'world'], {})
1708 let h = winheight(id)
1709 call assert_false(win_move_statusline(id, 1))
1710 call assert_equal(h, winheight(id))
1711 call popup_close(id)
Bram Moolenaar86e67172022-10-31 12:24:12 +00001712
1713 " check that using another tabpage fails without crash
1714 let id = win_getid()
1715 tabnew
1716 call assert_fails('call win_move_statusline(id, -1)', 'E1308:')
1717 tabclose
1718
Daniel Steinbergee630312022-01-10 13:36:34 +00001719 %bwipe!
1720endfunc
1721
Yegappan Lakshmanan72bb47e2022-04-03 11:22:38 +01001722" Test for window allocation failure
1723func Test_window_alloc_failure()
1724 %bw!
1725
1726 " test for creating a new window above current window
1727 call test_alloc_fail(GetAllocId('newwin_wvars'), 0, 0)
1728 call assert_fails('above new', 'E342:')
1729 call assert_equal(1, winnr('$'))
1730
1731 " test for creating a new window below current window
1732 call test_alloc_fail(GetAllocId('newwin_wvars'), 0, 0)
1733 call assert_fails('below new', 'E342:')
1734 call assert_equal(1, winnr('$'))
1735
1736 " test for popup window creation failure
1737 call test_alloc_fail(GetAllocId('newwin_wvars'), 0, 0)
1738 call assert_fails('call popup_create("Hello", {})', 'E342:')
1739 call assert_equal([], popup_list())
1740
1741 call test_alloc_fail(GetAllocId('newwin_wvars'), 0, 0)
1742 call assert_fails('split', 'E342:')
1743 call assert_equal(1, winnr('$'))
1744
Bram Moolenaarb18b4962022-09-02 21:55:50 +01001745 edit Xwaffile1
1746 edit Xwaffile2
Yegappan Lakshmanan72bb47e2022-04-03 11:22:38 +01001747 call test_alloc_fail(GetAllocId('newwin_wvars'), 0, 0)
Bram Moolenaarb18b4962022-09-02 21:55:50 +01001748 call assert_fails('sb Xwaffile1', 'E342:')
Yegappan Lakshmanan72bb47e2022-04-03 11:22:38 +01001749 call assert_equal(1, winnr('$'))
Bram Moolenaarb18b4962022-09-02 21:55:50 +01001750 call assert_equal('Xwaffile2', @%)
Yegappan Lakshmanan72bb47e2022-04-03 11:22:38 +01001751 %bw!
1752
1753 " FIXME: The following test crashes Vim
1754 " test for new tabpage creation failure
1755 " call test_alloc_fail(GetAllocId('newwin_wvars'), 0, 0)
1756 " call assert_fails('tabnew', 'E342:')
1757 " call assert_equal(1, tabpagenr('$'))
1758 " call assert_equal(1, winnr('$'))
1759
1760 " This test messes up the internal Vim window/frame information. So the
1761 " Test_window_cmd_cmdwin_with_vsp() test fails after running this test.
1762 " Open a new tab and close everything else to fix this issue.
1763 tabnew
1764 tabonly
1765endfunc
Bram Moolenaard0fb9072021-12-09 11:57:22 +00001766
Luuk van Baalfd7e60a2022-09-07 14:42:49 +01001767func Test_win_equal_last_status()
1768 let save_lines = &lines
1769 set lines=20
1770 set splitbelow
1771 set laststatus=0
1772
1773 split | split | quit
1774 call assert_equal(winheight(1), winheight(2))
1775
1776 let &lines = save_lines
1777 set splitbelow&
1778 set laststatus&
1779endfunc
1780
Luuk van Baal13ece2a2022-10-03 15:28:08 +01001781" Test "screen" and "cursor" values for 'splitkeep' with a sequence of
Luuk van Baalfaf1d412022-09-19 16:45:29 +01001782" split operations for various options: with and without a winbar,
1783" tabline, for each possible value of 'laststatus', 'scrolloff',
Luuk van Baal13ece2a2022-10-03 15:28:08 +01001784" 'equalalways', and with the cursor at the top, middle and bottom.
1785func Test_splitkeep_options()
Luuk van Baal594f9e02022-09-16 12:52:58 +01001786 " disallow window resizing
1787 let save_WS = &t_WS
1788 set t_WS=
1789
Luuk van Baal29ab5242022-09-11 16:59:53 +01001790 let gui = has("gui_running")
Luuk van Baal594f9e02022-09-16 12:52:58 +01001791 inoremap <expr> c "<cmd>copen<bar>wincmd k<CR>"
Luuk van Baal13ece2a2022-10-03 15:28:08 +01001792 for run in range(0, 20)
1793 let &splitkeep = run > 10 ? 'topline' : 'screen'
1794 let &scrolloff = (!(run % 4) ? 0 : run)
1795 let &laststatus = (run % 3)
1796 let &splitbelow = (run % 3)
1797 let &equalalways = (run % 2)
1798 let wsb = (run % 2) && &splitbelow
1799 let tl = (gui ? 0 : ((run % 5) ? 1 : 0))
1800 let pos = !(run % 3) ? 'H' : ((run % 2) ? 'M' : 'L')
1801 tabnew | tabonly! | redraw
Luuk van Baal594f9e02022-09-16 12:52:58 +01001802 execute (run % 5) ? 'tabnew' : ''
1803 execute (run % 2) ? 'nnoremenu 1.10 WinBar.Test :echo' : ''
Luuk van Baal594f9e02022-09-16 12:52:58 +01001804 call setline(1, range(1, 256))
1805 " No scroll for restore_snapshot
1806 norm G
1807 try
1808 copen | close | colder
1809 catch /E380/
1810 endtry
1811 call assert_equal(257 - winheight(0), line("w0"))
Luuk van Baal29ab5242022-09-11 16:59:53 +01001812
Luuk van Baal594f9e02022-09-16 12:52:58 +01001813 " No scroll for firstwin horizontal split
1814 execute 'norm gg' . pos
1815 split | redraw | wincmd k
1816 call assert_equal(1, line("w0"))
1817 call assert_equal(&scroll, winheight(0) / 2)
1818 wincmd j
Luuk van Baal13ece2a2022-10-03 15:28:08 +01001819 call assert_equal(&spk == 'topline' ? 1 : win_screenpos(0)[0] - tl - wsb, line("w0"))
Luuk van Baal29ab5242022-09-11 16:59:53 +01001820
Luuk van Baal594f9e02022-09-16 12:52:58 +01001821 " No scroll when resizing windows
Luuk van Baal13ece2a2022-10-03 15:28:08 +01001822 wincmd k | resize +2 | redraw
Luuk van Baal594f9e02022-09-16 12:52:58 +01001823 call assert_equal(1, line("w0"))
1824 wincmd j
Luuk van Baal13ece2a2022-10-03 15:28:08 +01001825 call assert_equal(&spk == 'topline' ? 1 : win_screenpos(0)[0] - tl - wsb, line("w0"))
Luuk van Baal29ab5242022-09-11 16:59:53 +01001826
Luuk van Baal594f9e02022-09-16 12:52:58 +01001827 " No scroll when dragging statusline
1828 call win_move_statusline(1, -3)
Luuk van Baal13ece2a2022-10-03 15:28:08 +01001829 call assert_equal(&spk == 'topline' ? 1 : win_screenpos(0)[0] - tl - wsb, line("w0"))
Luuk van Baal594f9e02022-09-16 12:52:58 +01001830 wincmd k
1831 call assert_equal(1, line("w0"))
Luuk van Baal29ab5242022-09-11 16:59:53 +01001832
Luuk van Baal594f9e02022-09-16 12:52:58 +01001833 " No scroll when changing shellsize
1834 set lines+=2
1835 call assert_equal(1, line("w0"))
1836 wincmd j
Luuk van Baal13ece2a2022-10-03 15:28:08 +01001837 call assert_equal(&spk == 'topline' ? 1 : win_screenpos(0)[0] - tl - wsb, line("w0"))
Luuk van Baal594f9e02022-09-16 12:52:58 +01001838 set lines-=2
Luuk van Baal13ece2a2022-10-03 15:28:08 +01001839 call assert_equal(&spk == 'topline' ? 1 : win_screenpos(0)[0] - tl - wsb, line("w0"))
Luuk van Baal594f9e02022-09-16 12:52:58 +01001840 wincmd k
1841 call assert_equal(1, line("w0"))
Luuk van Baal29ab5242022-09-11 16:59:53 +01001842
Luuk van Baal594f9e02022-09-16 12:52:58 +01001843 " No scroll when equalizing windows
1844 wincmd =
1845 call assert_equal(1, line("w0"))
1846 wincmd j
Luuk van Baal13ece2a2022-10-03 15:28:08 +01001847 call assert_equal(&spk == 'topline' ? 1 : win_screenpos(0)[0] - tl - wsb, line("w0"))
Luuk van Baal594f9e02022-09-16 12:52:58 +01001848 wincmd k
1849 call assert_equal(1, line("w0"))
Luuk van Baal29ab5242022-09-11 16:59:53 +01001850
Luuk van Baal594f9e02022-09-16 12:52:58 +01001851 " No scroll in windows split multiple times
1852 vsplit | split | 4wincmd w
Luuk van Baal13ece2a2022-10-03 15:28:08 +01001853 call assert_equal(&spk == 'topline' ? 1 : win_screenpos(0)[0] - tl - wsb, line("w0"))
Luuk van Baal594f9e02022-09-16 12:52:58 +01001854 1wincmd w | quit | wincmd l | split
Luuk van Baal13ece2a2022-10-03 15:28:08 +01001855 call assert_equal(&spk == 'topline' ? 1 : win_screenpos(0)[0] - tl - wsb, line("w0"))
Luuk van Baal594f9e02022-09-16 12:52:58 +01001856 wincmd j
Luuk van Baal13ece2a2022-10-03 15:28:08 +01001857 call assert_equal(&spk == 'topline' ? 1 : win_screenpos(0)[0] - tl - wsb, line("w0"))
Luuk van Baal29ab5242022-09-11 16:59:53 +01001858
Luuk van Baal594f9e02022-09-16 12:52:58 +01001859 " No scroll in small window
1860 2wincmd w | only | 5split | wincmd k
1861 call assert_equal(1, line("w0"))
1862 wincmd j
Luuk van Baal13ece2a2022-10-03 15:28:08 +01001863 call assert_equal(&spk == 'topline' ? 1 : win_screenpos(0)[0] - tl - wsb, line("w0"))
Luuk van Baal29ab5242022-09-11 16:59:53 +01001864
Luuk van Baal594f9e02022-09-16 12:52:58 +01001865 " No scroll for vertical split
1866 quit | vsplit | wincmd l
1867 call assert_equal(1, line("w0"))
1868 wincmd h
1869 call assert_equal(1, line("w0"))
Luuk van Baal29ab5242022-09-11 16:59:53 +01001870
Luuk van Baal594f9e02022-09-16 12:52:58 +01001871 " No scroll in windows split and quit multiple times
Luuk van Baal13ece2a2022-10-03 15:28:08 +01001872 quit | redraw | split | split | quit | redraw
1873 call assert_equal(&spk == 'topline' ? 1 : win_screenpos(0)[0] - tl - wsb, line("w0"))
Luuk van Baal29ab5242022-09-11 16:59:53 +01001874
Luuk van Baal594f9e02022-09-16 12:52:58 +01001875 " No scroll for new buffer
1876 1wincmd w | only | copen | wincmd k
1877 call assert_equal(1, line("w0"))
1878 only
1879 call assert_equal(1, line("w0"))
1880 above copen | wincmd j
Luuk van Baal13ece2a2022-10-03 15:28:08 +01001881 call assert_equal(&spk == 'topline' ? 1 : win_screenpos(0)[0] - tl, line("w0"))
Luuk van Baal29ab5242022-09-11 16:59:53 +01001882
Luuk van Baal594f9e02022-09-16 12:52:58 +01001883 " No scroll when opening cmdwin, and no cursor move when closing cmdwin.
1884 only | norm ggL
1885 let curpos = getcurpos()
1886 norm q:
1887 call assert_equal(1, line("w0"))
1888 call assert_equal(curpos, getcurpos())
Luuk van Baal29ab5242022-09-11 16:59:53 +01001889
Bram Moolenaarfdbd14e2023-02-14 21:56:42 +00001890 " Scroll when cursor becomes invalid in insert mode.
Luuk van Baal594f9e02022-09-16 12:52:58 +01001891 norm Lic
Luuk van Baalbc3dc292023-02-15 16:45:27 +00001892 call assert_equal(curpos, getcurpos(), 'run ' .. run)
Luuk van Baal29ab5242022-09-11 16:59:53 +01001893
Luuk van Baal594f9e02022-09-16 12:52:58 +01001894 " No scroll when topline not equal to 1
1895 only | execute "norm gg5\<C-e>" | split | wincmd k
1896 call assert_equal(6, line("w0"))
1897 wincmd j
Luuk van Baal13ece2a2022-10-03 15:28:08 +01001898 call assert_equal(&spk == 'topline' ? 6 : 5 + win_screenpos(0)[0] - tl - wsb, line("w0"))
Luuk van Baal29ab5242022-09-11 16:59:53 +01001899 endfor
1900
1901 tabnew | tabonly! | %bwipeout!
1902 iunmap c
Luuk van Baal29ab5242022-09-11 16:59:53 +01001903 set scrolloff&
1904 set splitbelow&
1905 set laststatus&
1906 set equalalways&
Luuk van Baal13ece2a2022-10-03 15:28:08 +01001907 set splitkeep&
Luuk van Baal594f9e02022-09-16 12:52:58 +01001908 let &t_WS = save_WS
Luuk van Baal29ab5242022-09-11 16:59:53 +01001909endfunc
Luuk van Baalfd7e60a2022-09-07 14:42:49 +01001910
Bram Moolenaare0f86912023-03-01 17:55:31 +00001911func Test_splitkeep_cmdwin_cursor_position()
Luuk van Baal13ece2a2022-10-03 15:28:08 +01001912 set splitkeep=screen
mityu12167d82022-09-15 17:44:07 +01001913 call setline(1, range(&lines))
1914
1915 " No scroll when cursor is at near bottom of window and cusor position
1916 " recompution (done by line('w0') in this test) happens while in cmdwin.
1917 normal! G
1918 let firstline = line('w0')
1919 autocmd CmdwinEnter * ++once autocmd WinEnter * ++once call line('w0')
1920 execute "normal! q:\<C-w>q"
1921 redraw!
1922 call assert_equal(firstline, line('w0'))
1923
1924 " User script can change cursor position successfully while in cmdwin and it
1925 " shouldn't be changed when closing cmdwin.
1926 execute "normal! Gq:\<Cmd>call win_execute(winnr('#')->win_getid(), 'call cursor(1, 1)')\<CR>\<C-w>q"
1927 call assert_equal(1, line('.'))
1928 call assert_equal(1, col('.'))
1929
1930 execute "normal! Gq:\<Cmd>autocmd WinEnter * ++once call cursor(1, 1)\<CR>\<C-w>q"
1931 call assert_equal(1, line('.'))
1932 call assert_equal(1, col('.'))
1933
1934 %bwipeout!
Luuk van Baal13ece2a2022-10-03 15:28:08 +01001935 set splitkeep&
Bram Moolenaare0f86912023-03-01 17:55:31 +00001936endfunc
Luuk van Baald5bc7622022-09-17 16:16:35 +01001937
Bram Moolenaare0f86912023-03-01 17:55:31 +00001938func Test_splitkeep_misc()
Luuk van Baal13ece2a2022-10-03 15:28:08 +01001939 set splitkeep=screen
Luuk van Baald5bc7622022-09-17 16:16:35 +01001940
1941 call setline(1, range(1, &lines))
Luuk van Baala109f392023-06-02 14:16:35 +01001942 " Cursor is adjusted to start and end of buffer
1943 norm M
1944 wincmd s
1945 resize 1
1946 call assert_equal(1, line('.'))
1947 wincmd j
1948 norm GM
1949 resize 1
1950 call assert_equal(&lines, line('.'))
1951 only!
1952
1953 set splitbelow
Luuk van Baald5bc7622022-09-17 16:16:35 +01001954 norm Gzz
1955 let top = line('w0')
Luuk van Baalfaf1d412022-09-19 16:45:29 +01001956 " No scroll when aucmd_win is opened
Luuk van Baald5bc7622022-09-17 16:16:35 +01001957 call setbufvar(bufnr("test", 1) , '&buftype', 'nofile')
1958 call assert_equal(top, line('w0'))
Luuk van Baalfaf1d412022-09-19 16:45:29 +01001959 " No scroll when tab is changed/closed
1960 tab help | close
1961 call assert_equal(top, line('w0'))
1962 " No scroll when help is closed and buffer line count < window height
1963 norm ggdG
1964 call setline(1, range(1, &lines - 10))
Luuk van Baald5bc7622022-09-17 16:16:35 +01001965 norm G
1966 let top = line('w0')
1967 help | quit
1968 call assert_equal(top, line('w0'))
Luuk van Baal346823d2022-10-05 18:26:42 +01001969 " No error when resizing window in autocmd and buffer length changed
1970 autocmd FileType qf exe "resize" line('$')
1971 cexpr getline(1, '$')
1972 copen
1973 wincmd p
1974 norm dd
1975 cexpr getline(1, '$')
Luuk van Baald5bc7622022-09-17 16:16:35 +01001976
Luuk van Baalfaf1d412022-09-19 16:45:29 +01001977 %bwipeout!
Luuk van Baald5bc7622022-09-17 16:16:35 +01001978 set splitbelow&
Luuk van Baal13ece2a2022-10-03 15:28:08 +01001979 set splitkeep&
Luuk van Baald5bc7622022-09-17 16:16:35 +01001980endfunc
1981
Luuk van Baal16af9132023-08-20 20:44:59 +02001982func Test_splitkeep_cursor()
1983 CheckScreendump
1984 let lines =<< trim END
1985 set splitkeep=screen
1986 autocmd CursorMoved * wincmd p | wincmd p
1987 call setline(1, range(1, 200))
1988 func CursorEqualize()
1989 call cursor(100, 1)
1990 wincmd =
1991 endfunc
1992 wincmd s
1993 call CursorEqualize()
1994 END
1995 call writefile(lines, 'XTestSplitkeepCallback', 'D')
1996 let buf = RunVimInTerminal('-S XTestSplitkeepCallback', #{rows: 8})
1997
1998 call VerifyScreenDump(buf, 'Test_splitkeep_cursor_1', {})
1999
2000 call term_sendkeys(buf, "j")
2001 call VerifyScreenDump(buf, 'Test_splitkeep_cursor_2', {})
2002
2003 call term_sendkeys(buf, ":set scrolloff=0\<CR>G")
2004 call VerifyScreenDump(buf, 'Test_splitkeep_cursor_3', {})
2005
2006 call StopVimInTerminal(buf)
2007endfunc
2008
Bram Moolenaare0f86912023-03-01 17:55:31 +00002009func Test_splitkeep_callback()
Luuk van Baal20e58562022-09-23 12:57:09 +01002010 CheckScreendump
2011 let lines =<< trim END
Luuk van Baal13ece2a2022-10-03 15:28:08 +01002012 set splitkeep=screen
Luuk van Baal20e58562022-09-23 12:57:09 +01002013 call setline(1, range(&lines))
Luuk van Baal13ece2a2022-10-03 15:28:08 +01002014 function C1(a, b)
Luuk van Baal20e58562022-09-23 12:57:09 +01002015 split | wincmd p
2016 endfunction
Luuk van Baal13ece2a2022-10-03 15:28:08 +01002017 function C2(a, b)
Luuk van Baal20e58562022-09-23 12:57:09 +01002018 close | split
2019 endfunction
Luuk van Baal13ece2a2022-10-03 15:28:08 +01002020 nn j <cmd>call job_start([&sh, &shcf, "true"], { 'exit_cb': 'C1' })<CR>
2021 nn t <cmd>call popup_create(term_start([&sh, &shcf, "true"],
2022 \ { 'hidden': 1, 'exit_cb': 'C2' }), {})<CR>
Luuk van Baal20e58562022-09-23 12:57:09 +01002023 END
Luuk van Baal13ece2a2022-10-03 15:28:08 +01002024 call writefile(lines, 'XTestSplitkeepCallback', 'D')
2025 let buf = RunVimInTerminal('-S XTestSplitkeepCallback', #{rows: 8})
Luuk van Baal20e58562022-09-23 12:57:09 +01002026
2027 call term_sendkeys(buf, "j")
Luuk van Baal13ece2a2022-10-03 15:28:08 +01002028 call VerifyScreenDump(buf, 'Test_splitkeep_callback_1', {})
Luuk van Baal20e58562022-09-23 12:57:09 +01002029
Luuk van Baal13ece2a2022-10-03 15:28:08 +01002030 call term_sendkeys(buf, ":quit\<CR>Ht")
2031 call VerifyScreenDump(buf, 'Test_splitkeep_callback_2', {})
Luuk van Baal20e58562022-09-23 12:57:09 +01002032
2033 call term_sendkeys(buf, ":set sb\<CR>:quit\<CR>Gj")
Luuk van Baal13ece2a2022-10-03 15:28:08 +01002034 call VerifyScreenDump(buf, 'Test_splitkeep_callback_3', {})
Luuk van Baal20e58562022-09-23 12:57:09 +01002035
Luuk van Baal13ece2a2022-10-03 15:28:08 +01002036 call term_sendkeys(buf, ":quit\<CR>Gt")
2037 call VerifyScreenDump(buf, 'Test_splitkeep_callback_4', {})
zeertzjq378e6c02023-01-14 11:46:49 +00002038
2039 call StopVimInTerminal(buf)
Luuk van Baal20e58562022-09-23 12:57:09 +01002040endfunc
2041
Bram Moolenaare0f86912023-03-01 17:55:31 +00002042func Test_splitkeep_fold()
Luuk van Baal13ece2a2022-10-03 15:28:08 +01002043 CheckScreendump
Luuk van Baal7c1cbb62022-09-27 12:31:15 +01002044
Luuk van Baal13ece2a2022-10-03 15:28:08 +01002045 let lines =<< trim END
2046 set splitkeep=screen
2047 set foldmethod=marker
2048 set number
2049 let line = 1
2050 for n in range(1, &lines)
2051 call setline(line, ['int FuncName() {/*{{{*/', 1, 2, 3, 4, 5, '}/*}}}*/',
2052 \ 'after fold'])
2053 let line += 8
2054 endfor
2055 END
2056 call writefile(lines, 'XTestSplitkeepFold', 'D')
2057 let buf = RunVimInTerminal('-S XTestSplitkeepFold', #{rows: 10})
Luuk van Baal7c1cbb62022-09-27 12:31:15 +01002058
2059 call term_sendkeys(buf, "L:wincmd s\<CR>")
Luuk van Baal13ece2a2022-10-03 15:28:08 +01002060 call VerifyScreenDump(buf, 'Test_splitkeep_fold_1', {})
Luuk van Baal7c1cbb62022-09-27 12:31:15 +01002061
2062 call term_sendkeys(buf, ":quit\<CR>")
Luuk van Baal13ece2a2022-10-03 15:28:08 +01002063 call VerifyScreenDump(buf, 'Test_splitkeep_fold_2', {})
Luuk van Baal7c1cbb62022-09-27 12:31:15 +01002064
2065 call term_sendkeys(buf, "H:below split\<CR>")
Luuk van Baal13ece2a2022-10-03 15:28:08 +01002066 call VerifyScreenDump(buf, 'Test_splitkeep_fold_3', {})
Luuk van Baal7c1cbb62022-09-27 12:31:15 +01002067
2068 call term_sendkeys(buf, ":wincmd k\<CR>:quit\<CR>")
Luuk van Baal13ece2a2022-10-03 15:28:08 +01002069 call VerifyScreenDump(buf, 'Test_splitkeep_fold_4', {})
zeertzjq378e6c02023-01-14 11:46:49 +00002070
2071 call StopVimInTerminal(buf)
Bram Moolenaare0f86912023-03-01 17:55:31 +00002072endfunc
Luuk van Baal7c1cbb62022-09-27 12:31:15 +01002073
Bram Moolenaare0f86912023-03-01 17:55:31 +00002074func Test_splitkeep_status()
Luuk van Baal74a694d2022-11-28 16:49:36 +00002075 CheckScreendump
2076
2077 let lines =<< trim END
2078 call setline(1, ['a', 'b', 'c'])
2079 set nomodified
2080 set splitkeep=screen
2081 let win = winnr()
2082 wincmd s
2083 wincmd j
2084 END
2085 call writefile(lines, 'XTestSplitkeepStatus', 'D')
2086 let buf = RunVimInTerminal('-S XTestSplitkeepStatus', #{rows: 10})
2087
2088 call term_sendkeys(buf, ":call win_move_statusline(win, 1)\<CR>")
2089 call VerifyScreenDump(buf, 'Test_splitkeep_status_1', {})
zeertzjq378e6c02023-01-14 11:46:49 +00002090
2091 call StopVimInTerminal(buf)
Bram Moolenaare0f86912023-03-01 17:55:31 +00002092endfunc
Luuk van Baal74a694d2022-11-28 16:49:36 +00002093
Luuk van Baalb926bf42023-05-06 12:53:50 +01002094" skipcol is not reset unnecessarily and is copied to new window
2095func Test_splitkeep_skipcol()
2096 CheckScreendump
2097
2098 let lines =<< trim END
2099 set splitkeep=topline smoothscroll splitbelow scrolloff=0
2100 call setline(1, 'with lots of text in one line '->repeat(6))
2101 norm 2
2102 wincmd s
2103 END
2104
2105 call writefile(lines, 'XTestSplitkeepSkipcol', 'D')
2106 let buf = RunVimInTerminal('-S XTestSplitkeepSkipcol', #{rows: 12, cols: 40})
2107
2108 call VerifyScreenDump(buf, 'Test_splitkeep_skipcol_1', {})
2109endfunc
2110
Bram Moolenaare0f86912023-03-01 17:55:31 +00002111func Test_new_help_window_on_error()
Rob Pillingcb94c912022-12-13 12:26:09 +00002112 help change.txt
2113 execute "normal! /CTRL-@\<CR>"
2114 silent! execute "normal! \<C-W>]"
2115
2116 let wincount = winnr('$')
2117 help 'mod'
2118
2119 call assert_equal(wincount, winnr('$'))
2120 call assert_equal(expand("<cword>"), "'mod'")
Bram Moolenaare0f86912023-03-01 17:55:31 +00002121endfunc
2122
Sean Dewar0fd44a52024-02-20 20:28:15 +01002123func Test_splitmove_flatten_frame()
2124 split
2125 vsplit
2126
2127 wincmd L
2128 let layout = winlayout()
2129 wincmd K
2130 wincmd L
2131 call assert_equal(winlayout(), layout)
2132
2133 only!
2134endfunc
2135
Sean Dewar02fcae02024-02-21 19:40:44 +01002136func Test_autocmd_window_force_room()
Sean Dewar0fd44a52024-02-20 20:28:15 +01002137 " Open as many windows as possible
2138 while v:true
2139 try
2140 split
2141 catch /E36:/
2142 break
2143 endtry
2144 endwhile
2145 while v:true
2146 try
2147 vsplit
2148 catch /E36:/
2149 break
2150 endtry
2151 endwhile
2152
2153 wincmd j
2154 vsplit
2155 call assert_fails('wincmd H', 'E36:')
2156 call assert_fails('wincmd J', 'E36:')
2157 call assert_fails('wincmd K', 'E36:')
2158 call assert_fails('wincmd L', 'E36:')
2159
2160 edit unload me
2161 enew
2162 bunload! unload\ me
Sean Dewar02fcae02024-02-21 19:40:44 +01002163 augroup AucmdWinForceRoom
Sean Dewar0fd44a52024-02-20 20:28:15 +01002164 au!
2165 au BufEnter * ++once let s:triggered = v:true
2166 \| call assert_equal('autocmd', win_gettype())
2167 augroup END
Sean Dewar5cac1a92024-03-12 21:11:39 +01002168 let info = s:win_layout_info()
Sean Dewar0fd44a52024-02-20 20:28:15 +01002169 " bufload opening the autocommand window shouldn't give E36.
2170 call bufload('unload me')
2171 call assert_equal(v:true, s:triggered)
Sean Dewar5cac1a92024-03-12 21:11:39 +01002172 call assert_equal(info, s:win_layout_info())
Sean Dewar0fd44a52024-02-20 20:28:15 +01002173
2174 unlet! s:triggered
Sean Dewar02fcae02024-02-21 19:40:44 +01002175 au! AucmdWinForceRoom
2176 augroup! AucmdWinForceRoom
Sean Dewar0fd44a52024-02-20 20:28:15 +01002177 %bw!
2178endfunc
Rob Pillingcb94c912022-12-13 12:26:09 +00002179
Sean Dewarf8658952024-02-20 22:05:10 +01002180func Test_win_gotoid_splitmove_textlock_cmdwin()
2181 call setline(1, 'foo')
2182 new
2183 let curwin = win_getid()
2184 call setline(1, 'bar')
2185
2186 set debug+=throw indentexpr=win_gotoid(win_getid(winnr('#')))
2187 call assert_fails('normal! ==', 'E565:')
2188 call assert_equal(curwin, win_getid())
Sean Dewar2a65e732024-02-22 19:53:33 +01002189 " No error if attempting to switch to curwin; nothing happens.
2190 set indentexpr=assert_equal(1,win_gotoid(win_getid()))
2191 normal! ==
2192 call assert_equal(curwin, win_getid())
Sean Dewarf8658952024-02-20 22:05:10 +01002193
2194 set indentexpr=win_splitmove(winnr('#'),winnr())
2195 call assert_fails('normal! ==', 'E565:')
2196 call assert_equal(curwin, win_getid())
2197
2198 %bw!
2199 set debug-=throw indentexpr&
2200
2201 call feedkeys('q:'
2202 \ .. ":call assert_fails('call win_splitmove(winnr(''#''), winnr())', 'E11:')\<CR>"
2203 \ .. ":call assert_equal('command', win_gettype())\<CR>"
2204 \ .. ":call assert_equal('', win_gettype(winnr('#')))\<CR>", 'ntx')
2205
2206 call feedkeys('q:'
2207 \ .. ":call assert_fails('call win_gotoid(win_getid(winnr(''#'')))', 'E11:')\<CR>"
Sean Dewar2a65e732024-02-22 19:53:33 +01002208 "\ No error if attempting to switch to curwin; nothing happens.
2209 \ .. ":call assert_equal(1, win_gotoid(win_getid()))\<CR>"
Sean Dewarf8658952024-02-20 22:05:10 +01002210 \ .. ":call assert_equal('command', win_gettype())\<CR>"
2211 \ .. ":call assert_equal('', win_gettype(winnr('#')))\<CR>", 'ntx')
2212endfunc
2213
Sean Dewar5866bc32024-03-13 20:17:24 +01002214func Test_winfixsize_positions()
2215 " Check positions are correct when closing a window in a non-current tabpage
2216 " causes non-adjacent window to fill the space due to 'winfix{width,height}'.
2217 tabnew
2218 vsplit
2219 wincmd |
2220 split
2221 set winfixheight
2222 split foo
2223 tabfirst
2224
2225 bwipe! foo
2226 " Save actual values before entering the tabpage.
2227 let info = s:win_layout_info(2)
2228 tabnext
2229 " Compare it with the expected value (after win_comp_pos) from entering.
2230 call assert_equal(s:win_layout_info(), info)
2231
2232 $tabnew
2233 split
2234 split
2235 wincmd k
2236 belowright vsplit
2237 set winfixwidth
2238 belowright vsplit foo
2239 tabprevious
2240
2241 bwipe! foo
2242 " Save actual values before entering the tabpage.
2243 let info = s:win_layout_info(3)
2244 tabnext
2245 " Compare it with the expected value (after win_comp_pos) from entering.
2246 call assert_equal(s:win_layout_info(), info)
2247
2248 " Check positions unchanged when failing to move a window, if 'winfix{width,
2249 " height}' would otherwise cause a non-adjacent window to fill the space.
2250 %bwipe
2251 call assert_fails('execute "split|"->repeat(&lines)', 'E36:')
2252 wincmd p
2253 vsplit
2254 set winfixwidth
2255 vsplit
2256 set winfixwidth
2257 vsplit
2258 vsplit
2259 set winfixwidth
2260 wincmd p
2261
2262 let info = s:win_layout_info()
2263 call assert_fails('wincmd J', 'E36:')
2264 call assert_equal(info, s:win_layout_info())
2265
2266 only
2267 call assert_fails('execute "vsplit|"->repeat(&columns)', 'E36:')
2268 belowright split
2269 set winfixheight
2270 belowright split
2271
2272 let info = s:win_layout_info()
2273 call assert_fails('wincmd H', 'E36:')
2274 call assert_equal(info, s:win_layout_info())
2275
2276 %bwipe
2277endfunc
2278
Bram Moolenaar9e4d8212016-08-18 23:04:48 +02002279" vim: shiftwidth=2 sts=2 expandtab