blob: 917c43203c67fb5386d94039d952d96bf5bd24fb [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
71 set winminwidth& cmdheight&
72 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
Sean Dewarbf44b692024-01-03 18:52:52 +0100134func Test_window_curwin_not_prevwin()
135 botright split
136 call assert_equal(2, winnr())
137 call assert_equal(1, winnr('#'))
138 quit
139 call assert_equal(1, winnr())
140 call assert_equal(0, winnr('#'))
141
142 botright split
143 botright split
144 call assert_equal(3, winnr())
145 call assert_equal(2, winnr('#'))
146 1quit
147 call assert_equal(2, winnr())
148 call assert_equal(1, winnr('#'))
149
150 botright split
151 call assert_equal(1, tabpagenr())
152 call assert_equal(3, winnr())
153 call assert_equal(2, winnr('#'))
154 wincmd T
155 call assert_equal(2, tabpagenr())
156 call assert_equal(1, winnr())
157 call assert_equal(0, winnr('#'))
158 tabfirst
159 call assert_equal(1, tabpagenr())
160 call assert_equal(2, winnr())
161 call assert_equal(0, winnr('#'))
162
163 tabonly
164 botright split
165 wincmd t
166 wincmd p
167 call assert_equal(3, winnr())
168 call assert_equal(1, winnr('#'))
169 quit
170 call assert_equal(2, winnr())
171 call assert_equal(1, winnr('#'))
172
173 botright split
174 wincmd t
175 wincmd p
176 call assert_equal(1, tabpagenr())
177 call assert_equal(3, winnr())
178 call assert_equal(1, winnr('#'))
179 wincmd T
180 call assert_equal(2, tabpagenr())
181 call assert_equal(1, winnr())
182 call assert_equal(0, winnr('#'))
183 tabfirst
184 call assert_equal(1, tabpagenr())
185 call assert_equal(2, winnr())
186 call assert_equal(1, winnr('#'))
187
188 tabonly
189 only
190endfunc
191
Bram Moolenaar4520d442017-03-19 16:09:46 +0100192func Test_window_horizontal_split()
193 call assert_equal(1, winnr('$'))
194 3wincmd s
195 call assert_equal(2, winnr('$'))
196 call assert_equal(3, winheight(0))
Bram Moolenaarf92e58c2019-09-08 21:51:41 +0200197 call assert_equal(winwidth(1), 2->winwidth())
Bram Moolenaar4520d442017-03-19 16:09:46 +0100198
199 call assert_fails('botright topleft wincmd s', 'E442:')
200 bw
201endfunc
202
203func Test_window_vertical_split()
204 call assert_equal(1, winnr('$'))
205 3wincmd v
206 call assert_equal(2, winnr('$'))
207 call assert_equal(3, winwidth(0))
208 call assert_equal(winheight(1), winheight(2))
209
210 call assert_fails('botright topleft wincmd v', 'E442:')
211 bw
212endfunc
213
Bram Moolenaar1bbb6192018-11-10 16:02:01 +0100214" Test the ":wincmd ^" and "<C-W>^" commands.
Bram Moolenaar4520d442017-03-19 16:09:46 +0100215func Test_window_split_edit_alternate()
Bram Moolenaar1bbb6192018-11-10 16:02:01 +0100216 " Test for failure when the alternate buffer/file no longer exists.
217 edit Xfoo | %bw
Bram Moolenaare2e40752020-09-04 21:18:46 +0200218 call assert_fails(':wincmd ^', 'E23:')
Bram Moolenaar1bbb6192018-11-10 16:02:01 +0100219
220 " Test for the expected behavior when we have two named buffers.
221 edit Xfoo | edit Xbar
Bram Moolenaar4520d442017-03-19 16:09:46 +0100222 wincmd ^
Bram Moolenaar1bbb6192018-11-10 16:02:01 +0100223 call assert_equal('Xfoo', bufname(winbufnr(1)))
224 call assert_equal('Xbar', bufname(winbufnr(2)))
225 only
Bram Moolenaar4520d442017-03-19 16:09:46 +0100226
Bram Moolenaar1bbb6192018-11-10 16:02:01 +0100227 " Test for the expected behavior when the alternate buffer is not named.
228 enew | let l:nr1 = bufnr('%')
229 edit Xfoo | let l:nr2 = bufnr('%')
230 wincmd ^
231 call assert_equal(l:nr1, winbufnr(1))
232 call assert_equal(l:nr2, winbufnr(2))
233 only
234
Bram Moolenaard42333d2018-11-10 20:28:19 +0100235 " FIXME: this currently fails on AppVeyor, but passes locally
236 if !has('win32')
237 " Test the Normal mode command.
238 call feedkeys("\<C-W>\<C-^>", 'tx')
239 call assert_equal(l:nr2, winbufnr(1))
240 call assert_equal(l:nr1, winbufnr(2))
241 endif
Bram Moolenaar1bbb6192018-11-10 16:02:01 +0100242
243 %bw!
244endfunc
245
246" Test the ":[count]wincmd ^" and "[count]<C-W>^" commands.
247func Test_window_split_edit_bufnr()
Bram Moolenaar1bbb6192018-11-10 16:02:01 +0100248 %bwipeout
249 let l:nr = bufnr('%') + 1
Bram Moolenaare2e40752020-09-04 21:18:46 +0200250 call assert_fails(':execute "normal! ' . l:nr . '\<C-W>\<C-^>"', 'E92:')
251 call assert_fails(':' . l:nr . 'wincmd ^', 'E16:')
252 call assert_fails(':0wincmd ^', 'E16:')
Bram Moolenaar1bbb6192018-11-10 16:02:01 +0100253
254 edit Xfoo | edit Xbar | edit Xbaz
255 let l:foo_nr = bufnr('Xfoo')
256 let l:bar_nr = bufnr('Xbar')
257 let l:baz_nr = bufnr('Xbaz')
258
Bram Moolenaar8617b402018-11-10 20:47:48 +0100259 " FIXME: this currently fails on AppVeyor, but passes locally
260 if !has('win32')
261 call feedkeys(l:foo_nr . "\<C-W>\<C-^>", 'tx')
262 call assert_equal('Xfoo', bufname(winbufnr(1)))
263 call assert_equal('Xbaz', bufname(winbufnr(2)))
264 only
Bram Moolenaar1bbb6192018-11-10 16:02:01 +0100265
Bram Moolenaar8617b402018-11-10 20:47:48 +0100266 call feedkeys(l:bar_nr . "\<C-W>\<C-^>", 'tx')
267 call assert_equal('Xbar', bufname(winbufnr(1)))
268 call assert_equal('Xfoo', bufname(winbufnr(2)))
269 only
Bram Moolenaar1bbb6192018-11-10 16:02:01 +0100270
Bram Moolenaar8617b402018-11-10 20:47:48 +0100271 execute l:baz_nr . 'wincmd ^'
272 call assert_equal('Xbaz', bufname(winbufnr(1)))
273 call assert_equal('Xbar', bufname(winbufnr(2)))
274 endif
Bram Moolenaar1bbb6192018-11-10 16:02:01 +0100275
276 %bw!
Bram Moolenaar4520d442017-03-19 16:09:46 +0100277endfunc
278
Bram Moolenaar9b9be002020-03-22 14:41:22 +0100279func Test_window_split_no_room()
280 " N horizontal windows need >= 2*N + 1 lines:
281 " - 1 line + 1 status line in each window
282 " - 1 Ex command line
283 "
284 " 2*N + 1 <= &lines
285 " N <= (lines - 1)/2
286 "
287 " Beyond that number of windows, E36: Not enough room is expected.
288 let hor_win_count = (&lines - 1)/2
289 let hor_split_count = hor_win_count - 1
290 for s in range(1, hor_split_count) | split | endfor
291 call assert_fails('split', 'E36:')
292
Sean Dewar0fd44a52024-02-20 20:28:15 +0100293 botright vsplit
294 wincmd |
295 let layout = winlayout()
296 let restcmd = winrestcmd()
297 call assert_fails('wincmd J', 'E36:')
298 call assert_fails('wincmd K', 'E36:')
299 call assert_equal(layout, winlayout())
300 call assert_equal(restcmd, winrestcmd())
301 only
302
Bram Moolenaar9b9be002020-03-22 14:41:22 +0100303 " N vertical windows need >= 2*(N - 1) + 1 columns:
304 " - 1 column + 1 separator for each window (except last window)
305 " - 1 column for the last window which does not have separator
306 "
307 " 2*(N - 1) + 1 <= &columns
308 " 2*N - 1 <= &columns
309 " N <= (&columns + 1)/2
310 let ver_win_count = (&columns + 1)/2
311 let ver_split_count = ver_win_count - 1
312 for s in range(1, ver_split_count) | vsplit | endfor
313 call assert_fails('vsplit', 'E36:')
314
Sean Dewar0fd44a52024-02-20 20:28:15 +0100315 split
316 wincmd |
317 let layout = winlayout()
318 let restcmd = winrestcmd()
319 call assert_fails('wincmd H', 'E36:')
320 call assert_fails('wincmd L', 'E36:')
321 call assert_equal(layout, winlayout())
322 call assert_equal(restcmd, winrestcmd())
323
324 " Check that the last statusline isn't lost.
Sean Dewar02fcae02024-02-21 19:40:44 +0100325 " Set its window's width to 2 for the test.
Sean Dewar0fd44a52024-02-20 20:28:15 +0100326 wincmd j
Sean Dewar02fcae02024-02-21 19:40:44 +0100327 set laststatus=0 winminwidth=0
328 vertical resize 2
329 set winminwidth&
Sean Dewar0fd44a52024-02-20 20:28:15 +0100330 call setwinvar(winnr('k'), '&statusline', '@#')
331 let last_stl_row = win_screenpos(0)[0] - 1
332 redraw
333 call assert_equal('@#|', GetScreenStr(last_stl_row))
334 call assert_equal('~ |', GetScreenStr(&lines - &cmdheight))
Sean Dewar02fcae02024-02-21 19:40:44 +0100335
336 let restcmd = winrestcmd()
Sean Dewar0fd44a52024-02-20 20:28:15 +0100337 call assert_fails('wincmd H', 'E36:')
338 call assert_fails('wincmd L', 'E36:')
339 call assert_equal(layout, winlayout())
340 call assert_equal(restcmd, winrestcmd())
341 call setwinvar(winnr('k'), '&statusline', '=-')
342 redraw
343 call assert_equal('=-|', GetScreenStr(last_stl_row))
344 call assert_equal('~ |', GetScreenStr(&lines - &cmdheight))
345
Bram Moolenaar9b9be002020-03-22 14:41:22 +0100346 %bw!
Sean Dewar0fd44a52024-02-20 20:28:15 +0100347 set laststatus&
Bram Moolenaar9b9be002020-03-22 14:41:22 +0100348endfunc
349
Bram Moolenaar4520d442017-03-19 16:09:46 +0100350func Test_window_exchange()
351 e Xa
352
353 " Nothing happens with window exchange when there is 1 window
354 wincmd x
355 call assert_equal(1, winnr('$'))
356
357 split Xb
358 split Xc
359
360 call assert_equal('Xc', bufname(winbufnr(1)))
361 call assert_equal('Xb', bufname(winbufnr(2)))
362 call assert_equal('Xa', bufname(winbufnr(3)))
363
364 " Exchange current window 1 with window 3
365 3wincmd x
366 call assert_equal('Xa', bufname(winbufnr(1)))
367 call assert_equal('Xb', bufname(winbufnr(2)))
368 call assert_equal('Xc', bufname(winbufnr(3)))
369
370 " Exchange window with next when at the top window
371 wincmd x
372 call assert_equal('Xb', bufname(winbufnr(1)))
373 call assert_equal('Xa', bufname(winbufnr(2)))
374 call assert_equal('Xc', bufname(winbufnr(3)))
375
376 " Exchange window with next when at the middle window
377 wincmd j
378 wincmd x
379 call assert_equal('Xb', bufname(winbufnr(1)))
380 call assert_equal('Xc', bufname(winbufnr(2)))
381 call assert_equal('Xa', bufname(winbufnr(3)))
382
383 " Exchange window with next when at the bottom window.
384 " When there is no next window, it exchanges with the previous window.
385 wincmd j
386 wincmd x
387 call assert_equal('Xb', bufname(winbufnr(1)))
388 call assert_equal('Xa', bufname(winbufnr(2)))
389 call assert_equal('Xc', bufname(winbufnr(3)))
390
391 bw Xa Xb Xc
392endfunc
393
394func Test_window_rotate()
395 e Xa
396 split Xb
397 split Xc
398 call assert_equal('Xc', bufname(winbufnr(1)))
399 call assert_equal('Xb', bufname(winbufnr(2)))
400 call assert_equal('Xa', bufname(winbufnr(3)))
401
402 " Rotate downwards
403 wincmd r
404 call assert_equal('Xa', bufname(winbufnr(1)))
405 call assert_equal('Xc', bufname(winbufnr(2)))
406 call assert_equal('Xb', bufname(winbufnr(3)))
407
408 2wincmd r
409 call assert_equal('Xc', bufname(winbufnr(1)))
410 call assert_equal('Xb', bufname(winbufnr(2)))
411 call assert_equal('Xa', bufname(winbufnr(3)))
412
413 " Rotate upwards
414 wincmd R
415 call assert_equal('Xb', bufname(winbufnr(1)))
416 call assert_equal('Xa', bufname(winbufnr(2)))
417 call assert_equal('Xc', bufname(winbufnr(3)))
418
419 2wincmd R
420 call assert_equal('Xc', bufname(winbufnr(1)))
421 call assert_equal('Xb', bufname(winbufnr(2)))
422 call assert_equal('Xa', bufname(winbufnr(3)))
423
424 bot vsplit
425 call assert_fails('wincmd R', 'E443:')
426
427 bw Xa Xb Xc
428endfunc
429
430func Test_window_height()
431 e Xa
432 split Xb
433
434 let [wh1, wh2] = [winheight(1), winheight(2)]
435 " Active window (1) should have the same height or 1 more
436 " than the other window.
437 call assert_inrange(wh2, wh2 + 1, wh1)
438
439 wincmd -
440 call assert_equal(wh1 - 1, winheight(1))
441 call assert_equal(wh2 + 1, winheight(2))
442
443 wincmd +
444 call assert_equal(wh1, winheight(1))
Bram Moolenaarf92e58c2019-09-08 21:51:41 +0200445 call assert_equal(wh2, 2->winheight())
Bram Moolenaar4520d442017-03-19 16:09:46 +0100446
447 2wincmd _
448 call assert_equal(2, winheight(1))
449 call assert_equal(wh1 + wh2 - 2, winheight(2))
450
451 wincmd =
452 call assert_equal(wh1, winheight(1))
453 call assert_equal(wh2, winheight(2))
454
455 2wincmd _
456 set winfixheight
457 split Xc
458 let [wh1, wh2, wh3] = [winheight(1), winheight(2), winheight(3)]
459 call assert_equal(2, winheight(2))
460 call assert_inrange(wh3, wh3 + 1, wh1)
461 3wincmd +
462 call assert_equal(2, winheight(2))
463 call assert_equal(wh1 + 3, winheight(1))
464 call assert_equal(wh3 - 3, winheight(3))
465 wincmd =
466 call assert_equal(2, winheight(2))
467 call assert_equal(wh1, winheight(1))
468 call assert_equal(wh3, winheight(3))
469
470 wincmd j
471 set winfixheight&
472
473 wincmd =
474 let [wh1, wh2, wh3] = [winheight(1), winheight(2), winheight(3)]
475 " Current window (2) should have the same height or 1 more
476 " than the other windows.
477 call assert_inrange(wh1, wh1 + 1, wh2)
478 call assert_inrange(wh3, wh3 + 1, wh2)
479
480 bw Xa Xb Xc
481endfunc
482
Bram Moolenaar21c3a802022-08-31 17:49:14 +0100483func Test_wincmd_equal()
484 edit Xone
485 below split Xtwo
486 rightbelow vsplit Xthree
487 call assert_equal('Xone', bufname(winbufnr(1)))
488 call assert_equal('Xtwo', bufname(winbufnr(2)))
489 call assert_equal('Xthree', bufname(winbufnr(3)))
490
491 " Xone and Xtwo should be about the same height
492 let [wh1, wh2] = [winheight(1), winheight(2)]
493 call assert_inrange(wh1 - 1, wh1 + 1, wh2)
494 " Xtwo and Xthree should be about the same width
495 let [ww2, ww3] = [winwidth(2), winwidth(3)]
496 call assert_inrange(ww2 - 1, ww2 + 1, ww3)
497
498 1wincmd w
499 10wincmd _
500 2wincmd w
501 20wincmd |
502 call assert_equal(10, winheight(1))
503 call assert_equal(20, winwidth(2))
504
505 " equalizing horizontally doesn't change the heights
506 hor wincmd =
507 call assert_equal(10, winheight(1))
508 let [ww2, ww3] = [winwidth(2), winwidth(3)]
509 call assert_inrange(ww2 - 1, ww2 + 1, ww3)
510
511 2wincmd w
512 20wincmd |
513 call assert_equal(20, winwidth(2))
514 " equalizing vertically doesn't change the widths
515 vert wincmd =
516 call assert_equal(20, winwidth(2))
517 let [wh1, wh2] = [winheight(1), winheight(2)]
518 call assert_inrange(wh1 - 1, wh1 + 1, wh2)
519
520 bwipe Xone Xtwo Xthree
521endfunc
522
Bram Moolenaar4520d442017-03-19 16:09:46 +0100523func Test_window_width()
524 e Xa
525 vsplit Xb
526
527 let [ww1, ww2] = [winwidth(1), winwidth(2)]
528 " Active window (1) should have the same width or 1 more
529 " than the other window.
530 call assert_inrange(ww2, ww2 + 1, ww1)
531
532 wincmd <
533 call assert_equal(ww1 - 1, winwidth(1))
534 call assert_equal(ww2 + 1, winwidth(2))
535
536 wincmd >
537 call assert_equal(ww1, winwidth(1))
538 call assert_equal(ww2, winwidth(2))
539
540 2wincmd |
541 call assert_equal(2, winwidth(1))
542 call assert_equal(ww1 + ww2 - 2, winwidth(2))
543
544 wincmd =
545 call assert_equal(ww1, winwidth(1))
546 call assert_equal(ww2, winwidth(2))
547
548 2wincmd |
549 set winfixwidth
550 vsplit Xc
551 let [ww1, ww2, ww3] = [winwidth(1), winwidth(2), winwidth(3)]
Bram Moolenaar38e34832017-03-19 20:22:36 +0100552 call assert_equal(2, winwidth(2))
Bram Moolenaar4520d442017-03-19 16:09:46 +0100553 call assert_inrange(ww3, ww3 + 1, ww1)
554 3wincmd >
Bram Moolenaar38e34832017-03-19 20:22:36 +0100555 call assert_equal(2, winwidth(2))
Bram Moolenaar4520d442017-03-19 16:09:46 +0100556 call assert_equal(ww1 + 3, winwidth(1))
557 call assert_equal(ww3 - 3, winwidth(3))
558 wincmd =
Bram Moolenaar38e34832017-03-19 20:22:36 +0100559 call assert_equal(2, winwidth(2))
Bram Moolenaar4520d442017-03-19 16:09:46 +0100560 call assert_equal(ww1, winwidth(1))
561 call assert_equal(ww3, winwidth(3))
562
563 wincmd l
564 set winfixwidth&
565
566 wincmd =
567 let [ww1, ww2, ww3] = [winwidth(1), winwidth(2), winwidth(3)]
568 " Current window (2) should have the same width or 1 more
569 " than the other windows.
570 call assert_inrange(ww1, ww1 + 1, ww2)
571 call assert_inrange(ww3, ww3 + 1, ww2)
572
Yegappan Lakshmanan59585492021-06-12 13:46:41 +0200573 " when the current window width is less than the new 'winwidth', the current
574 " window width should be increased.
575 enew | only
576 split
577 10vnew
578 set winwidth=15
579 call assert_equal(15, winwidth(0))
580
581 %bw!
Bram Moolenaar4520d442017-03-19 16:09:46 +0100582endfunc
583
Bram Moolenaar8eeeba82017-06-25 22:45:39 +0200584func Test_equalalways_on_close()
585 set equalalways
586 vsplit
587 windo split
Bram Moolenaar1bbb6192018-11-10 16:02:01 +0100588 split
Bram Moolenaar8eeeba82017-06-25 22:45:39 +0200589 wincmd J
590 " now we have a frame top-left with two windows, a frame top-right with two
591 " windows and a frame at the bottom, full-width.
592 let height_1 = winheight(1)
593 let height_2 = winheight(2)
594 let height_3 = winheight(3)
595 let height_4 = winheight(4)
596 " closing the bottom window causes all windows to be resized.
597 close
598 call assert_notequal(height_1, winheight(1))
599 call assert_notequal(height_2, winheight(2))
600 call assert_notequal(height_3, winheight(3))
601 call assert_notequal(height_4, winheight(4))
602 call assert_equal(winheight(1), winheight(3))
603 call assert_equal(winheight(2), winheight(4))
604
605 1wincmd w
606 split
607 4wincmd w
608 resize + 5
609 " left column has three windows, equalized heights.
610 " right column has two windows, top one a bit higher
611 let height_1 = winheight(1)
612 let height_2 = winheight(2)
613 let height_4 = winheight(4)
614 let height_5 = winheight(5)
615 3wincmd w
616 " closing window in left column equalizes heights in left column but not in
617 " the right column
618 close
619 call assert_notequal(height_1, winheight(1))
620 call assert_notequal(height_2, winheight(2))
621 call assert_equal(height_4, winheight(3))
622 call assert_equal(height_5, winheight(4))
623
624 only
625 set equalalways&
626endfunc
627
Bram Moolenaar22044dc2017-12-02 15:43:37 +0100628func Test_win_screenpos()
Bram Moolenaar5a4c3082019-12-01 15:23:11 +0100629 CheckFeature quickfix
630
Bram Moolenaar22044dc2017-12-02 15:43:37 +0100631 call assert_equal(1, winnr('$'))
632 split
633 vsplit
634 10wincmd _
635 30wincmd |
636 call assert_equal([1, 1], win_screenpos(1))
637 call assert_equal([1, 32], win_screenpos(2))
638 call assert_equal([12, 1], win_screenpos(3))
639 call assert_equal([0, 0], win_screenpos(4))
Bram Moolenaar99fa7212020-04-26 15:59:55 +0200640 call assert_fails('let l = win_screenpos([])', 'E745:')
Bram Moolenaar22044dc2017-12-02 15:43:37 +0100641 only
642endfunc
643
Bram Moolenaar4520d442017-03-19 16:09:46 +0100644func Test_window_jump_tag()
Bram Moolenaar5a4c3082019-12-01 15:23:11 +0100645 CheckFeature quickfix
646
Bram Moolenaar4520d442017-03-19 16:09:46 +0100647 help
648 /iccf
649 call assert_match('^|iccf|', getline('.'))
650 call assert_equal(2, winnr('$'))
651 2wincmd }
652 call assert_equal(3, winnr('$'))
653 call assert_match('^|iccf|', getline('.'))
654 wincmd k
655 call assert_match('\*iccf\*', getline('.'))
656 call assert_equal(2, winheight(0))
657
658 wincmd z
659 set previewheight=4
660 help
661 /bugs
662 wincmd }
663 wincmd k
664 call assert_match('\*bugs\*', getline('.'))
665 call assert_equal(4, winheight(0))
666 set previewheight&
667
668 %bw!
669endfunc
670
671func Test_window_newtab()
672 e Xa
673
674 call assert_equal(1, tabpagenr('$'))
675 call assert_equal("\nAlready only one window", execute('wincmd T'))
676
677 split Xb
678 split Xc
679
680 wincmd T
681 call assert_equal(2, tabpagenr('$'))
682 call assert_equal(['Xb', 'Xa'], map(tabpagebuflist(1), 'bufname(v:val)'))
Bram Moolenaarce90e362019-09-08 18:58:44 +0200683 call assert_equal(['Xc' ], map(2->tabpagebuflist(), 'bufname(v:val)'))
Bram Moolenaar0e05de42020-03-25 22:23:46 +0100684 call assert_equal(['Xc' ], map(tabpagebuflist(), 'bufname(v:val)'))
Bram Moolenaar4520d442017-03-19 16:09:46 +0100685
686 %bw!
687endfunc
688
Bram Moolenaarf79225e2017-03-18 23:11:04 +0100689func Test_next_split_all()
690 " This was causing an illegal memory access.
691 n x
692 norm axxx
693 split
694 split
695 s/x
696 s/x
697 all
698 bwipe!
699endfunc
700
Bram Moolenaar75373f32017-08-07 22:02:30 +0200701" Tests for adjusting window and contents
702func GetScreenStr(row)
703 let str = ""
704 for c in range(1,3)
705 let str .= nr2char(screenchar(a:row, c))
706 endfor
707 return str
708endfunc
709
710func Test_window_contents()
711 enew! | only | new
712 call setline(1, range(1,256))
713
714 exe "norm! \<C-W>t\<C-W>=1Gzt\<C-W>w\<C-W>+"
715 redraw
Bram Moolenaar72cf47a2018-05-10 18:23:29 +0200716 let s3 = GetScreenStr(1)
Bram Moolenaar75373f32017-08-07 22:02:30 +0200717 wincmd p
718 call assert_equal(1, line("w0"))
719 call assert_equal('1 ', s3)
720
721 exe "norm! \<C-W>t\<C-W>=50Gzt\<C-W>w\<C-W>+"
722 redraw
Bram Moolenaar72cf47a2018-05-10 18:23:29 +0200723 let s3 = GetScreenStr(1)
Bram Moolenaar75373f32017-08-07 22:02:30 +0200724 wincmd p
725 call assert_equal(50, line("w0"))
726 call assert_equal('50 ', s3)
727
728 exe "norm! \<C-W>t\<C-W>=59Gzt\<C-W>w\<C-W>+"
729 redraw
Bram Moolenaar72cf47a2018-05-10 18:23:29 +0200730 let s3 = GetScreenStr(1)
Bram Moolenaar75373f32017-08-07 22:02:30 +0200731 wincmd p
732 call assert_equal(59, line("w0"))
733 call assert_equal('59 ', s3)
734
Bram Moolenaar8b633132020-03-20 18:20:51 +0100735 %d
736 call setline(1, ['one', 'two', 'three'])
737 call assert_equal(1, line('w0'))
738 call assert_equal(3, line('w$'))
739
Bram Moolenaar75373f32017-08-07 22:02:30 +0200740 bwipeout!
741 call test_garbagecollect_now()
742endfunc
743
Bram Moolenaar2efb3232017-12-19 12:27:23 +0100744func Test_window_colon_command()
745 " This was reading invalid memory.
746 exe "norm! v\<C-W>:\<C-U>echo v:version"
747endfunc
748
Bram Moolenaar6f361c92018-01-31 19:06:50 +0100749func Test_access_freed_mem()
Bram Moolenaar1417c762019-07-27 17:31:36 +0200750 call assert_equal(&columns, winwidth(0))
Bram Moolenaar9a046fd2021-01-28 13:47:59 +0100751 " This was accessing freed memory (but with what events?)
752 au BufEnter,BufLeave,WinEnter,WinLeave 0 vs xxx
Bram Moolenaar6f361c92018-01-31 19:06:50 +0100753 arg 0
754 argadd
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +0200755 call assert_fails("all", "E242:")
Bram Moolenaar6f361c92018-01-31 19:06:50 +0100756 au!
757 bwipe xxx
Bram Moolenaar1417c762019-07-27 17:31:36 +0200758 call assert_equal(&columns, winwidth(0))
Bram Moolenaar6f361c92018-01-31 19:06:50 +0100759endfunc
760
Bram Moolenaara27e1dc2019-10-07 22:27:36 +0200761func Test_insert_cleared_on_switch_to_term()
762 CheckFeature terminal
763
764 set showmode
765 terminal
766 wincmd p
767
768 call feedkeys("i\<C-O>", 'ntx')
769 redraw
770
771 " The "-- (insert) --" indicator should be visible.
772 let chars = map(range(1, &columns), 'nr2char(screenchar(&lines, v:val))')
773 let str = trim(join(chars, ''))
774 call assert_equal('-- (insert) --', str)
775
776 call feedkeys("\<C-W>p", 'ntx')
777 redraw
778
779 " The "-- (insert) --" indicator should have been cleared.
780 let chars = map(range(1, &columns), 'nr2char(screenchar(&lines, v:val))')
781 let str = trim(join(chars, ''))
782 call assert_equal('', str)
783
784 set showmode&
785 %bw!
786endfunc
787
Bram Moolenaar5bab5552018-04-13 20:41:29 +0200788func Test_visual_cleared_after_window_split()
789 new | only!
790 let smd_save = &showmode
791 set showmode
792 let ls_save = &laststatus
793 set laststatus=1
794 call setline(1, ['a', 'b', 'c', 'd', ''])
795 norm! G
796 exe "norm! kkvk"
797 redraw
798 exe "norm! \<C-W>v"
799 redraw
800 " check if '-- VISUAL --' disappeared from command line
801 let columns = range(1, &columns)
802 let cmdlinechars = map(columns, 'nr2char(screenchar(&lines, v:val))')
803 let cmdline = join(cmdlinechars, '')
804 let cmdline_ltrim = substitute(cmdline, '^\s*', "", "")
805 let mode_shown = substitute(cmdline_ltrim, '\s*$', "", "")
806 call assert_equal('', mode_shown)
807 let &showmode = smd_save
808 let &laststatus = ls_save
809 bwipe!
810endfunc
811
Bram Moolenaar72cf47a2018-05-10 18:23:29 +0200812func Test_winrestcmd()
813 2split
814 3vsplit
Bram Moolenaara0c8aea2021-03-20 19:55:35 +0100815 let restcmd = winrestcmd()
Bram Moolenaar72cf47a2018-05-10 18:23:29 +0200816 call assert_equal(2, winheight(0))
817 call assert_equal(3, winwidth(0))
818 wincmd =
819 call assert_notequal(2, winheight(0))
820 call assert_notequal(3, winwidth(0))
Bram Moolenaara0c8aea2021-03-20 19:55:35 +0100821 exe restcmd
Bram Moolenaar72cf47a2018-05-10 18:23:29 +0200822 call assert_equal(2, winheight(0))
823 call assert_equal(3, winwidth(0))
824 only
Bram Moolenaara0c8aea2021-03-20 19:55:35 +0100825
826 wincmd v
827 wincmd s
828 wincmd v
829 redraw
830 let restcmd = winrestcmd()
831 wincmd _
832 wincmd |
833 exe restcmd
834 redraw
835 call assert_equal(restcmd, winrestcmd())
836
837 only
Bram Moolenaar72cf47a2018-05-10 18:23:29 +0200838endfunc
839
Bram Moolenaar1e115362019-01-09 23:01:02 +0100840func Fun_RenewFile()
Bram Moolenaar026587b2019-08-17 15:08:00 +0200841 " Need to wait a bit for the timestamp to be older.
Bram Moolenaarce90e362019-09-08 18:58:44 +0200842 let old_ftime = getftime("tmp.txt")
843 while getftime("tmp.txt") == old_ftime
844 sleep 100m
845 silent execute '!echo "1" > tmp.txt'
846 endwhile
Bram Moolenaara42df592018-12-24 00:22:39 +0100847 sp
848 wincmd p
849 edit! tmp.txt
Bram Moolenaar1e115362019-01-09 23:01:02 +0100850endfunc
Bram Moolenaara42df592018-12-24 00:22:39 +0100851
852func Test_window_prevwin()
853 " Can we make this work on MS-Windows?
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +0200854 CheckUnix
Bram Moolenaara42df592018-12-24 00:22:39 +0100855
856 set hidden autoread
Bram Moolenaardb4c9472022-10-15 22:06:06 +0100857 call writefile(['2'], 'tmp.txt', 'D')
Bram Moolenaara42df592018-12-24 00:22:39 +0100858 new tmp.txt
859 q
Bram Moolenaara42df592018-12-24 00:22:39 +0100860 call Fun_RenewFile()
861 call assert_equal(2, winnr())
862 wincmd p
863 call assert_equal(1, winnr())
864 wincmd p
865 q
866 call Fun_RenewFile()
867 call assert_equal(2, winnr())
868 wincmd p
869 call assert_equal(1, winnr())
870 wincmd p
871 " reset
872 q
Bram Moolenaara42df592018-12-24 00:22:39 +0100873 set hidden&vim autoread&vim
874 delfunc Fun_RenewFile
875endfunc
876
Bram Moolenaar8fcb60f2019-03-04 13:18:30 +0100877func Test_relative_cursor_position_in_one_line_window()
878 new
879 only
880 call setline(1, range(1, 10000))
881 normal 50%
882 let lnum = getcurpos()[1]
883 split
884 split
885 " make third window take as many lines as possible, other windows will
886 " become one line
887 3wincmd w
888 for i in range(1, &lines - 6)
889 wincmd +
890 redraw!
891 endfor
892
893 " first and second window should show cursor line
894 let wininfo = getwininfo()
895 call assert_equal(lnum, wininfo[0].topline)
896 call assert_equal(lnum, wininfo[1].topline)
897
898 only!
899 bwipe!
Yegappan Lakshmanan04c4c572022-08-30 19:48:24 +0100900 call assert_fails('call winrestview(test_null_dict())', 'E1297:')
Bram Moolenaar8fcb60f2019-03-04 13:18:30 +0100901endfunc
902
903func Test_relative_cursor_position_after_move_and_resize()
904 let so_save = &so
905 set so=0
906 enew
907 call setline(1, range(1, 10000))
908 normal 50%
909 split
910 1wincmd w
911 " Move cursor to first line in window
912 normal H
913 redraw!
914 " Reduce window height to two lines
915 let height = winheight(0)
916 while winheight(0) > 2
917 wincmd -
918 redraw!
919 endwhile
920 " move cursor to second/last line in window
921 normal j
922 " restore previous height
923 while winheight(0) < height
924 wincmd +
925 redraw!
926 endwhile
927 " make window two lines again
928 while winheight(0) > 2
929 wincmd -
930 redraw!
931 endwhile
932
933 " cursor should be at bottom line
934 let info = getwininfo(win_getid())[0]
935 call assert_equal(info.topline + 1, getcurpos()[1])
936
937 only!
938 bwipe!
939 let &so = so_save
940endfunc
941
942func Test_relative_cursor_position_after_resize()
943 let so_save = &so
944 set so=0
945 enew
946 call setline(1, range(1, 10000))
947 normal 50%
948 split
949 1wincmd w
950 let winid1 = win_getid()
951 let info = getwininfo(winid1)[0]
952 " Move cursor to second line in window
953 exe "normal " . (info.topline + 1) . "G"
954 redraw!
955 let lnum = getcurpos()[1]
956
957 " Make the window only two lines high, cursor should end up in top line
958 2wincmd w
959 exe (info.height - 2) . "wincmd +"
960 redraw!
961 let info = getwininfo(winid1)[0]
962 call assert_equal(lnum, info.topline)
963
964 only!
965 bwipe!
966 let &so = so_save
967endfunc
968
969func Test_relative_cursor_second_line_after_resize()
970 let so_save = &so
971 set so=0
972 enew
973 call setline(1, range(1, 10000))
974 normal 50%
975 split
976 1wincmd w
977 let winid1 = win_getid()
978 let info = getwininfo(winid1)[0]
979
980 " Make the window only two lines high
981 2wincmd _
982
983 " Move cursor to second line in window
984 normal H
985 normal j
986
987 " Make window size bigger, then back to 2 lines
988 for i in range(1, 10)
989 wincmd +
990 redraw!
991 endfor
992 for i in range(1, 10)
993 wincmd -
994 redraw!
995 endfor
996
997 " cursor should end up in bottom line
998 let info = getwininfo(winid1)[0]
999 call assert_equal(info.topline + 1, getcurpos()[1])
1000
1001 only!
1002 bwipe!
1003 let &so = so_save
1004endfunc
1005
Bram Moolenaara9b25352019-05-12 14:25:30 +02001006func Test_split_noscroll()
1007 let so_save = &so
Bram Moolenaarbd2d68c2019-05-18 15:36:11 +02001008 enew
1009 call setline(1, range(1, 8))
Bram Moolenaara9b25352019-05-12 14:25:30 +02001010 normal 100%
1011 split
1012
1013 1wincmd w
1014 let winid1 = win_getid()
1015 let info1 = getwininfo(winid1)[0]
1016
1017 2wincmd w
1018 let winid2 = win_getid()
1019 let info2 = getwininfo(winid2)[0]
1020
1021 call assert_equal(1, info1.topline)
1022 call assert_equal(1, info2.topline)
1023
Bram Moolenaarbd2d68c2019-05-18 15:36:11 +02001024 " window that fits all lines by itself, but not when split: closing other
1025 " window should restore fraction.
Bram Moolenaara9b25352019-05-12 14:25:30 +02001026 only!
Bram Moolenaarbd2d68c2019-05-18 15:36:11 +02001027 call setline(1, range(1, &lines - 10))
1028 exe &lines / 4
1029 let winid1 = win_getid()
1030 let info1 = getwininfo(winid1)[0]
1031 call assert_equal(1, info1.topline)
1032 new
1033 redraw
1034 close
1035 let info1 = getwininfo(winid1)[0]
1036 call assert_equal(1, info1.topline)
1037
Bram Moolenaara9b25352019-05-12 14:25:30 +02001038 bwipe!
1039 let &so = so_save
1040endfunc
1041
Bram Moolenaar46ad2882019-04-08 20:01:47 +02001042" Tests for the winnr() function
1043func Test_winnr()
1044 only | tabonly
1045 call assert_equal(1, winnr('j'))
1046 call assert_equal(1, winnr('k'))
1047 call assert_equal(1, winnr('h'))
1048 call assert_equal(1, winnr('l'))
1049
1050 " create a set of horizontally and vertically split windows
1051 leftabove new | wincmd p
1052 leftabove new | wincmd p
1053 rightbelow new | wincmd p
1054 rightbelow new | wincmd p
1055 leftabove vnew | wincmd p
1056 leftabove vnew | wincmd p
1057 rightbelow vnew | wincmd p
1058 rightbelow vnew | wincmd p
1059
1060 call assert_equal(8, winnr('j'))
1061 call assert_equal(2, winnr('k'))
1062 call assert_equal(4, winnr('h'))
1063 call assert_equal(6, winnr('l'))
1064 call assert_equal(9, winnr('2j'))
1065 call assert_equal(1, winnr('2k'))
1066 call assert_equal(3, winnr('2h'))
1067 call assert_equal(7, winnr('2l'))
1068
1069 " Error cases
1070 call assert_fails("echo winnr('0.2k')", 'E15:')
1071 call assert_equal(2, winnr('-2k'))
1072 call assert_fails("echo winnr('-2xj')", 'E15:')
1073 call assert_fails("echo winnr('j2j')", 'E15:')
1074 call assert_fails("echo winnr('ll')", 'E15:')
1075 call assert_fails("echo winnr('5')", 'E15:')
1076 call assert_equal(4, winnr('0h'))
Bram Moolenaar99fa7212020-04-26 15:59:55 +02001077 call assert_fails("let w = winnr([])", 'E730:')
1078 call assert_equal('unknown', win_gettype(-1))
1079 call assert_equal(-1, winheight(-1))
1080 call assert_equal(-1, winwidth(-1))
Bram Moolenaar46ad2882019-04-08 20:01:47 +02001081
1082 tabnew
1083 call assert_equal(8, tabpagewinnr(1, 'j'))
Bram Moolenaarce90e362019-09-08 18:58:44 +02001084 call assert_equal(2, 1->tabpagewinnr('k'))
Bram Moolenaar46ad2882019-04-08 20:01:47 +02001085 call assert_equal(4, tabpagewinnr(1, 'h'))
1086 call assert_equal(6, tabpagewinnr(1, 'l'))
1087
1088 only | tabonly
1089endfunc
1090
Bram Moolenaarf92e58c2019-09-08 21:51:41 +02001091func Test_winrestview()
1092 split runtest.vim
1093 normal 50%
1094 let view = winsaveview()
1095 close
1096 split runtest.vim
1097 eval view->winrestview()
1098 call assert_equal(view, winsaveview())
1099
1100 bwipe!
Yegappan Lakshmanan04c4c572022-08-30 19:48:24 +01001101 call assert_fails('call winrestview(test_null_dict())', 'E1297:')
Bram Moolenaarf92e58c2019-09-08 21:51:41 +02001102endfunc
1103
Bram Moolenaard20dcb32019-09-10 21:22:58 +02001104func Test_win_splitmove()
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01001105 CheckFeature quickfix
1106
Bram Moolenaard20dcb32019-09-10 21:22:58 +02001107 edit a
1108 leftabove split b
1109 leftabove vsplit c
1110 leftabove split d
Sean Dewar96cc4ae2024-02-20 21:52:31 +01001111
1112 " win_splitmove doesn't actually create or close any windows, so expect an
1113 " unchanged winid and no WinNew/WinClosed events, like :wincmd H/J/K/L.
1114 let s:triggered = []
1115 augroup WinSplitMove
1116 au!
1117 au WinNewPre * let s:triggered += ['WinNewPre']
1118 au WinNew * let s:triggered += ['WinNew', win_getid()]
1119 au WinClosed * let s:triggered += ['WinClosed', str2nr(expand('<afile>'))]
1120 augroup END
1121 let winid = win_getid()
1122
Bram Moolenaard20dcb32019-09-10 21:22:58 +02001123 call assert_equal(0, win_splitmove(winnr(), winnr('l')))
1124 call assert_equal(bufname(winbufnr(1)), 'c')
1125 call assert_equal(bufname(winbufnr(2)), 'd')
1126 call assert_equal(bufname(winbufnr(3)), 'b')
1127 call assert_equal(bufname(winbufnr(4)), 'a')
1128 call assert_equal(0, win_splitmove(winnr(), winnr('j'), {'vertical': 1}))
1129 call assert_equal(0, win_splitmove(winnr(), winnr('j'), {'vertical': 1}))
1130 call assert_equal(bufname(winbufnr(1)), 'c')
1131 call assert_equal(bufname(winbufnr(2)), 'b')
1132 call assert_equal(bufname(winbufnr(3)), 'd')
1133 call assert_equal(bufname(winbufnr(4)), 'a')
1134 call assert_equal(0, win_splitmove(winnr(), winnr('k'), {'vertical': 1}))
1135 call assert_equal(bufname(winbufnr(1)), 'd')
1136 call assert_equal(bufname(winbufnr(2)), 'c')
1137 call assert_equal(bufname(winbufnr(3)), 'b')
1138 call assert_equal(bufname(winbufnr(4)), 'a')
1139 call assert_equal(0, win_splitmove(winnr(), winnr('j'), {'rightbelow': v:true}))
1140 call assert_equal(bufname(winbufnr(1)), 'c')
1141 call assert_equal(bufname(winbufnr(2)), 'b')
1142 call assert_equal(bufname(winbufnr(3)), 'a')
1143 call assert_equal(bufname(winbufnr(4)), 'd')
Yegappan Lakshmanan04c4c572022-08-30 19:48:24 +01001144 call assert_fails('call win_splitmove(winnr(), winnr("k"), test_null_dict())', 'E1297:')
Sean Dewar96cc4ae2024-02-20 21:52:31 +01001145 call assert_equal([], s:triggered)
1146 call assert_equal(winid, win_getid())
1147
1148 unlet! s:triggered
1149 au! WinSplitMove
Bram Moolenaard20dcb32019-09-10 21:22:58 +02001150 only | bd
1151
1152 call assert_fails('call win_splitmove(winnr(), 123)', 'E957:')
1153 call assert_fails('call win_splitmove(123, winnr())', 'E957:')
1154 call assert_fails('call win_splitmove(winnr(), winnr())', 'E957:')
Bram Moolenaar7b94e772020-01-06 21:03:24 +01001155
1156 tabnew
1157 call assert_fails('call win_splitmove(1, win_getid(1, 1))', 'E957:')
1158 tabclose
Sean Dewar0fd44a52024-02-20 20:28:15 +01001159
1160 split
1161 augroup WinSplitMove
1162 au!
1163 au WinEnter * ++once call win_gotoid(win_getid(winnr('#')))
1164 augroup END
1165 call assert_fails('call win_splitmove(winnr(), winnr("#"))', 'E855:')
1166
1167 augroup WinSplitMove
1168 au!
1169 au WinLeave * ++once quit
1170 augroup END
1171 call assert_fails('call win_splitmove(winnr(), winnr("#"))', 'E855:')
1172
1173 split
1174 split
1175 augroup WinSplitMove
1176 au!
1177 au WinEnter * ++once let s:triggered = v:true
1178 \| call assert_fails('call win_splitmove(winnr("$"), winnr())', 'E242:')
1179 augroup END
1180 quit
1181 call assert_equal(v:true, s:triggered)
1182 unlet! s:triggered
1183
1184 new
1185 augroup WinSplitMove
1186 au!
1187 au BufHidden * ++once let s:triggered = v:true
1188 \| call assert_fails('call win_splitmove(winnr("#"), winnr())', 'E1159:')
1189 augroup END
1190 hide
1191 call assert_equal(v:true, s:triggered)
1192 unlet! s:triggered
1193
Sean Dewarabf70302024-02-24 10:20:24 +01001194 split
1195 let close_win = winnr('#')
1196 augroup WinSplitMove
1197 au!
1198 au WinEnter * ++once quit!
1199 augroup END
1200 call win_splitmove(close_win, winnr())
1201 call assert_equal(0, win_id2win(close_win))
1202
Sean Dewar0fd44a52024-02-20 20:28:15 +01001203 au! WinSplitMove
1204 augroup! WinSplitMove
1205 %bw!
Bram Moolenaard20dcb32019-09-10 21:22:58 +02001206endfunc
1207
Bram Moolenaare20b9ec2020-02-03 21:40:04 +01001208" Test for the :only command
1209func Test_window_only()
1210 new
1211 set modified
1212 new
1213 call assert_fails('only', 'E445:')
1214 only!
Bram Moolenaarbc2b71d2020-02-17 21:33:30 +01001215 " Test for :only with a count
1216 let wid = win_getid()
1217 new
1218 new
1219 3only
1220 call assert_equal(1, winnr('$'))
1221 call assert_equal(wid, win_getid())
1222 call assert_fails('close', 'E444:')
1223 call assert_fails('%close', 'E16:')
Bram Moolenaare20b9ec2020-02-03 21:40:04 +01001224endfunc
1225
Bram Moolenaar9f6277b2020-02-11 22:04:02 +01001226" Test for errors with :wincmd
1227func Test_wincmd_errors()
1228 call assert_fails('wincmd g', 'E474:')
1229 call assert_fails('wincmd ab', 'E474:')
1230endfunc
1231
1232" Test for errors with :winpos
1233func Test_winpos_errors()
1234 if !has("gui_running") && !has('win32')
1235 call assert_fails('winpos', 'E188:')
1236 endif
1237 call assert_fails('winpos 10', 'E466:')
1238endfunc
1239
Bram Moolenaar406cd902020-02-18 21:54:41 +01001240" Test for +cmd in a :split command
1241func Test_split_cmd()
1242 split +set\ readonly
1243 call assert_equal(1, &readonly)
1244 call assert_equal(2, winnr('$'))
1245 close
1246endfunc
1247
Bram Moolenaar5080b0a2020-03-22 21:23:47 +01001248" Create maximum number of horizontally or vertically split windows and then
1249" run commands that create a new horizontally/vertically split window
1250func Run_noroom_for_newwindow_test(dir_arg)
1251 let dir = (a:dir_arg == 'v') ? 'vert ' : ''
1252
1253 " Open as many windows as possible
Bram Moolenaarab505b12020-03-23 19:28:44 +01001254 while v:true
Bram Moolenaar5080b0a2020-03-22 21:23:47 +01001255 try
1256 exe dir . 'new'
1257 catch /E36:/
1258 break
1259 endtry
Bram Moolenaarab505b12020-03-23 19:28:44 +01001260 endwhile
Bram Moolenaar5080b0a2020-03-22 21:23:47 +01001261
Bram Moolenaardb4c9472022-10-15 22:06:06 +01001262 call writefile(['first', 'second', 'third'], 'Xnorfile1', 'D')
1263 call writefile([], 'Xnorfile2', 'D')
1264 call writefile([], 'Xnorfile3', 'D')
Bram Moolenaar5080b0a2020-03-22 21:23:47 +01001265
1266 " Argument list related commands
Bram Moolenaarb18b4962022-09-02 21:55:50 +01001267 args Xnorfile1 Xnorfile2 Xnorfile3
Bram Moolenaar5080b0a2020-03-22 21:23:47 +01001268 next
1269 for cmd in ['sargument 2', 'snext', 'sprevious', 'sNext', 'srewind',
1270 \ 'sfirst', 'slast']
1271 call assert_fails(dir .. cmd, 'E36:')
1272 endfor
1273 %argdelete
1274
1275 " Buffer related commands
1276 set modified
1277 hide enew
Bram Moolenaarb18b4962022-09-02 21:55:50 +01001278 for cmd in ['sbuffer Xnorfile1', 'sbnext', 'sbprevious', 'sbNext', 'sbrewind',
Bram Moolenaar5080b0a2020-03-22 21:23:47 +01001279 \ 'sbfirst', 'sblast', 'sball', 'sbmodified', 'sunhide']
1280 call assert_fails(dir .. cmd, 'E36:')
1281 endfor
1282
1283 " Window related commands
Bram Moolenaarb18b4962022-09-02 21:55:50 +01001284 for cmd in ['split', 'split Xnorfile2', 'new', 'new Xnorfile3', 'sview Xnorfile1',
Bram Moolenaar5080b0a2020-03-22 21:23:47 +01001285 \ 'sfind runtest.vim']
1286 call assert_fails(dir .. cmd, 'E36:')
1287 endfor
1288
1289 " Help
1290 call assert_fails(dir .. 'help', 'E36:')
1291 call assert_fails(dir .. 'helpgrep window', 'E36:')
1292
1293 " Command-line window
1294 if a:dir_arg == 'h'
1295 " Cmd-line window is always a horizontally split window
1296 call assert_beeps('call feedkeys("q:\<CR>", "xt")')
1297 endif
1298
1299 " Quickfix and location list window
1300 if has('quickfix')
1301 cexpr ''
1302 call assert_fails(dir .. 'copen', 'E36:')
1303 lexpr ''
1304 call assert_fails(dir .. 'lopen', 'E36:')
1305
1306 " Preview window
Bram Moolenaarb18b4962022-09-02 21:55:50 +01001307 call assert_fails(dir .. 'pedit Xnorfile2', 'E36:')
Bram Moolenaar5080b0a2020-03-22 21:23:47 +01001308 call setline(1, 'abc')
1309 call assert_fails(dir .. 'psearch abc', 'E36:')
1310 endif
1311
1312 " Window commands (CTRL-W ^ and CTRL-W f)
1313 if a:dir_arg == 'h'
1314 call assert_fails('call feedkeys("\<C-W>^", "xt")', 'E36:')
Bram Moolenaarb18b4962022-09-02 21:55:50 +01001315 call setline(1, 'Xnorfile1')
Bram Moolenaar5080b0a2020-03-22 21:23:47 +01001316 call assert_fails('call feedkeys("gg\<C-W>f", "xt")', 'E36:')
1317 endif
1318 enew!
1319
1320 " Tag commands (:stag, :stselect and :stjump)
1321 call writefile(["!_TAG_FILE_ENCODING\tutf-8\t//",
Bram Moolenaarb18b4962022-09-02 21:55:50 +01001322 \ "second\tXnorfile1\t2",
1323 \ "third\tXnorfile1\t3",],
Bram Moolenaar5080b0a2020-03-22 21:23:47 +01001324 \ 'Xtags')
1325 set tags=Xtags
1326 call assert_fails(dir .. 'stag second', 'E36:')
1327 call assert_fails('call feedkeys(":" .. dir .. "stselect second\n1\n", "xt")', 'E36:')
1328 call assert_fails(dir .. 'stjump second', 'E36:')
1329 call assert_fails(dir .. 'ptag second', 'E36:')
1330 set tags&
1331 call delete('Xtags')
1332
1333 " :isplit and :dsplit
1334 call setline(1, ['#define FOO 1', 'FOO'])
1335 normal 2G
1336 call assert_fails(dir .. 'isplit FOO', 'E36:')
1337 call assert_fails(dir .. 'dsplit FOO', 'E36:')
1338
1339 " terminal
1340 if has('terminal')
1341 call assert_fails(dir .. 'terminal', 'E36:')
1342 endif
1343
1344 %bwipe!
Bram Moolenaar5080b0a2020-03-22 21:23:47 +01001345 only
1346endfunc
1347
1348func Test_split_cmds_with_no_room()
1349 call Run_noroom_for_newwindow_test('h')
1350 call Run_noroom_for_newwindow_test('v')
1351endfunc
1352
Bram Moolenaarad48e6c2020-04-21 22:19:45 +02001353" Test for various wincmd failures
1354func Test_wincmd_fails()
1355 only!
1356 call assert_beeps("normal \<C-W>w")
1357 call assert_beeps("normal \<C-W>p")
1358 call assert_beeps("normal \<C-W>gk")
1359 call assert_beeps("normal \<C-W>r")
1360 call assert_beeps("normal \<C-W>K")
1361 call assert_beeps("normal \<C-W>H")
1362 call assert_beeps("normal \<C-W>2gt")
1363endfunc
1364
Bram Moolenaarfe6dce82020-09-04 14:41:21 +02001365func Test_window_resize()
1366 " Vertical :resize (absolute, relative, min and max size).
1367 vsplit
1368 vert resize 8
1369 call assert_equal(8, winwidth(0))
1370 vert resize +2
1371 call assert_equal(10, winwidth(0))
1372 vert resize -2
1373 call assert_equal(8, winwidth(0))
1374 vert resize
1375 call assert_equal(&columns - 2, winwidth(0))
1376 vert resize 0
1377 call assert_equal(1, winwidth(0))
1378 vert resize 99999
1379 call assert_equal(&columns - 2, winwidth(0))
1380
1381 %bwipe!
1382
1383 " Horizontal :resize (with absolute, relative size, min and max size).
1384 split
1385 resize 8
1386 call assert_equal(8, winheight(0))
1387 resize +2
1388 call assert_equal(10, winheight(0))
1389 resize -2
1390 call assert_equal(8, winheight(0))
1391 resize
1392 call assert_equal(&lines - 4, winheight(0))
1393 resize 0
1394 call assert_equal(1, winheight(0))
1395 resize 99999
1396 call assert_equal(&lines - 4, winheight(0))
1397
1398 " :resize with explicit window number.
1399 let other_winnr = winnr('j')
1400 exe other_winnr .. 'resize 10'
1401 call assert_equal(10, winheight(other_winnr))
1402 call assert_equal(&lines - 10 - 3, winheight(0))
Bram Moolenaar9668cc52020-10-17 17:39:55 +02001403 exe other_winnr .. 'resize +1'
1404 exe other_winnr .. 'resize +1'
1405 call assert_equal(12, winheight(other_winnr))
1406 call assert_equal(&lines - 10 - 3 -2, winheight(0))
Bram Moolenaar89015a62020-12-29 12:46:51 +01001407 close
1408
1409 vsplit
1410 wincmd l
1411 let other_winnr = winnr('h')
1412 call assert_notequal(winnr(), other_winnr)
Bram Moolenaar5efe0e52021-01-01 14:31:34 +01001413 exe 'vert ' .. other_winnr .. 'resize -' .. &columns
Bram Moolenaar89015a62020-12-29 12:46:51 +01001414 call assert_equal(0, winwidth(other_winnr))
Bram Moolenaarfe6dce82020-09-04 14:41:21 +02001415
1416 %bwipe!
1417endfunc
1418
Bram Moolenaar5d3c9f82020-06-26 20:41:39 +02001419" Test for adjusting the window width when a window is closed with some
1420" windows using 'winfixwidth'
1421func Test_window_width_adjust()
1422 only
1423 " Three vertical windows. Windows 1 and 2 have 'winfixwidth' set and close
1424 " window 2.
1425 wincmd v
1426 vert resize 10
1427 set winfixwidth
1428 wincmd v
1429 set winfixwidth
1430 wincmd c
1431 call assert_inrange(10, 12, winwidth(1))
1432 " Three vertical windows. Windows 2 and 3 have 'winfixwidth' set and close
1433 " window 3.
1434 only
1435 set winfixwidth
1436 wincmd v
1437 vert resize 10
1438 set winfixwidth
1439 wincmd v
1440 set nowinfixwidth
1441 wincmd b
1442 wincmd c
1443 call assert_inrange(10, 12, winwidth(2))
1444
1445 new | only
1446endfunc
1447
1448" Test for jumping to a vertical/horizontal neighbor window based on the
1449" current cursor position
dundargocc57b5bc2022-11-02 13:30:51 +00001450func Test_window_goto_neighbor()
Bram Moolenaar5d3c9f82020-06-26 20:41:39 +02001451 %bw!
1452
1453 " Vertical window movement
1454
1455 " create the following window layout:
1456 " +--+--+
1457 " |w1|w3|
1458 " +--+ |
1459 " |w2| |
1460 " +--+--+
1461 " |w4 |
1462 " +-----+
1463 new
1464 vsplit
1465 split
1466 " vertically jump from w4
1467 wincmd b
1468 call setline(1, repeat(' ', &columns))
1469 call cursor(1, 1)
1470 wincmd k
1471 call assert_equal(2, winnr())
1472 wincmd b
1473 call cursor(1, &columns)
1474 redraw!
1475 wincmd k
1476 call assert_equal(3, winnr())
1477 %bw!
1478
1479 " create the following window layout:
1480 " +--+--+--+
1481 " |w1|w2|w3|
1482 " +--+--+--+
1483 " |w4 |
1484 " +--------+
1485 new
1486 vsplit
1487 vsplit
1488 wincmd b
1489 call setline(1, repeat(' ', &columns))
1490 call cursor(1, 1)
1491 wincmd k
1492 call assert_equal(1, winnr())
1493 wincmd b
1494 call cursor(1, &columns / 2)
1495 redraw!
1496 wincmd k
1497 call assert_equal(2, winnr())
1498 wincmd b
1499 call cursor(1, &columns)
1500 redraw!
1501 wincmd k
1502 call assert_equal(3, winnr())
1503 %bw!
1504
1505 " Horizontal window movement
1506
1507 " create the following window layout:
1508 " +--+--+--+
1509 " |w1|w2|w4|
1510 " +--+--+ |
1511 " |w3 | |
1512 " +-----+--+
1513 vsplit
1514 split
1515 vsplit
1516 4wincmd l
1517 call setline(1, repeat([' '], &lines))
1518 call cursor(1, 1)
1519 redraw!
1520 wincmd h
1521 call assert_equal(2, winnr())
1522 4wincmd l
1523 call cursor(&lines, 1)
1524 redraw!
1525 wincmd h
1526 call assert_equal(3, winnr())
1527 %bw!
1528
1529 " create the following window layout:
1530 " +--+--+
1531 " |w1|w4|
1532 " +--+ +
1533 " |w2| |
1534 " +--+ +
1535 " |w3| |
1536 " +--+--+
1537 vsplit
1538 split
1539 split
1540 wincmd l
1541 call setline(1, repeat([' '], &lines))
1542 call cursor(1, 1)
1543 redraw!
1544 wincmd h
1545 call assert_equal(1, winnr())
1546 wincmd l
1547 call cursor(&lines / 2, 1)
1548 redraw!
1549 wincmd h
1550 call assert_equal(2, winnr())
1551 wincmd l
1552 call cursor(&lines, 1)
1553 redraw!
1554 wincmd h
1555 call assert_equal(3, winnr())
1556 %bw!
1557endfunc
1558
1559" Test for an autocmd closing the destination window when jumping from one
1560" window to another.
1561func Test_close_dest_window()
1562 split
Bram Moolenaarb18b4962022-09-02 21:55:50 +01001563 edit Xdstfile
Bram Moolenaar5d3c9f82020-06-26 20:41:39 +02001564
1565 " Test for BufLeave
1566 augroup T1
1567 au!
Bram Moolenaarb18b4962022-09-02 21:55:50 +01001568 au BufLeave Xdstfile $wincmd c
Bram Moolenaar5d3c9f82020-06-26 20:41:39 +02001569 augroup END
1570 wincmd b
1571 call assert_equal(1, winnr('$'))
Bram Moolenaarb18b4962022-09-02 21:55:50 +01001572 call assert_equal('Xdstfile', @%)
Bram Moolenaar5d3c9f82020-06-26 20:41:39 +02001573 augroup T1
1574 au!
1575 augroup END
1576
1577 " Test for WinLeave
1578 new
1579 wincmd p
1580 augroup T1
1581 au!
1582 au WinLeave * 1wincmd c
1583 augroup END
1584 wincmd t
1585 call assert_equal(1, winnr('$'))
Bram Moolenaarb18b4962022-09-02 21:55:50 +01001586 call assert_equal('Xdstfile', @%)
Bram Moolenaar5d3c9f82020-06-26 20:41:39 +02001587 augroup T1
1588 au!
1589 augroup END
1590 augroup! T1
1591 %bw!
1592endfunc
1593
Bram Moolenaard0fb9072021-12-09 11:57:22 +00001594func Test_window_minimal_size()
1595 set winminwidth=0 winminheight=0
1596
1597 " check size is fixed vertically
1598 new
1599 call win_execute(win_getid(2), 'wincmd _')
1600 call assert_equal(0, winheight(0))
1601 call feedkeys('0', 'tx')
1602 call assert_equal(1, winheight(0))
1603 bwipe!
1604
1605 " check size is fixed horizontally
1606 vert new
1607 call win_execute(win_getid(2), 'wincmd |')
1608 call assert_equal(0, winwidth(0))
1609 call feedkeys('0', 'tx')
1610 call assert_equal(1, winwidth(0))
1611 bwipe!
1612
1613 if has('timers')
1614 " check size is fixed in Insert mode
ichizokfa9a8e02021-12-12 16:42:09 +00001615 func s:CheckSize(timer) abort
1616 call win_execute(win_getid(2), 'wincmd _')
1617 call assert_equal(0, winheight(0))
1618 call feedkeys(" \<Esc>", 't!')
1619 endfunc
Bram Moolenaard0fb9072021-12-09 11:57:22 +00001620 new
ichizokfa9a8e02021-12-12 16:42:09 +00001621 call timer_start(100, function('s:CheckSize'))
Bram Moolenaard0fb9072021-12-09 11:57:22 +00001622 call feedkeys('a', 'tx!')
1623 call assert_equal(1, winheight(0))
1624 bwipe!
ichizokfa9a8e02021-12-12 16:42:09 +00001625 delfunc s:CheckSize
Bram Moolenaard0fb9072021-12-09 11:57:22 +00001626 endif
1627
1628 set winminwidth& winminheight&
1629endfunc
1630
Daniel Steinbergee630312022-01-10 13:36:34 +00001631func Test_win_move_separator()
1632 edit a
1633 leftabove vsplit b
1634 let w = winwidth(0)
1635 " check win_move_separator from left window on left window
1636 call assert_equal(1, winnr())
1637 for offset in range(5)
1638 call assert_true(win_move_separator(0, offset))
1639 call assert_equal(w + offset, winwidth(0))
1640 call assert_true(0->win_move_separator(-offset))
1641 call assert_equal(w, winwidth(0))
1642 endfor
1643 " check win_move_separator from right window on left window number
1644 wincmd l
1645 call assert_notequal(1, winnr())
1646 for offset in range(5)
1647 call assert_true(1->win_move_separator(offset))
1648 call assert_equal(w + offset, winwidth(1))
1649 call assert_true(win_move_separator(1, -offset))
1650 call assert_equal(w, winwidth(1))
1651 endfor
1652 " check win_move_separator from right window on left window ID
1653 let id = win_getid(1)
1654 for offset in range(5)
1655 call assert_true(win_move_separator(id, offset))
1656 call assert_equal(w + offset, winwidth(id))
1657 call assert_true(id->win_move_separator(-offset))
1658 call assert_equal(w, winwidth(id))
1659 endfor
zeertzjqa0c4e2f2022-01-29 10:59:53 +00001660 " check win_move_separator from right window on right window is no-op
1661 let w0 = winwidth(0)
1662 call assert_true(win_move_separator(0, 1))
1663 call assert_equal(w0, winwidth(0))
1664 call assert_true(win_move_separator(0, -1))
1665 call assert_equal(w0, winwidth(0))
zeertzjq873f41a2022-11-01 11:44:43 +00001666
Daniel Steinbergee630312022-01-10 13:36:34 +00001667 " check that win_move_separator doesn't error with offsets beyond moving
1668 " possibility
1669 call assert_true(win_move_separator(id, 5000))
1670 call assert_true(winwidth(id) > w)
1671 call assert_true(win_move_separator(id, -5000))
1672 call assert_true(winwidth(id) < w)
zeertzjq873f41a2022-11-01 11:44:43 +00001673
Daniel Steinbergee630312022-01-10 13:36:34 +00001674 " check that win_move_separator returns false for an invalid window
1675 wincmd =
1676 let w = winwidth(0)
1677 call assert_false(win_move_separator(-1, 1))
1678 call assert_equal(w, winwidth(0))
zeertzjq873f41a2022-11-01 11:44:43 +00001679
Daniel Steinbergee630312022-01-10 13:36:34 +00001680 " check that win_move_separator returns false for a popup window
1681 let id = popup_create(['hello', 'world'], {})
1682 let w = winwidth(id)
1683 call assert_false(win_move_separator(id, 1))
1684 call assert_equal(w, winwidth(id))
1685 call popup_close(id)
zeertzjq873f41a2022-11-01 11:44:43 +00001686
1687 " check that using another tabpage fails without crash
1688 let id = win_getid()
1689 tabnew
1690 call assert_fails('call win_move_separator(id, -1)', 'E1308:')
1691 tabclose
1692
Daniel Steinbergee630312022-01-10 13:36:34 +00001693 %bwipe!
1694endfunc
1695
1696func Test_win_move_statusline()
1697 edit a
1698 leftabove split b
1699 let h = winheight(0)
1700 " check win_move_statusline from top window on top window
1701 call assert_equal(1, winnr())
1702 for offset in range(5)
1703 call assert_true(win_move_statusline(0, offset))
1704 call assert_equal(h + offset, winheight(0))
1705 call assert_true(0->win_move_statusline(-offset))
1706 call assert_equal(h, winheight(0))
1707 endfor
1708 " check win_move_statusline from bottom window on top window number
1709 wincmd j
1710 call assert_notequal(1, winnr())
1711 for offset in range(5)
1712 call assert_true(1->win_move_statusline(offset))
1713 call assert_equal(h + offset, winheight(1))
1714 call assert_true(win_move_statusline(1, -offset))
1715 call assert_equal(h, winheight(1))
1716 endfor
zeertzjqa0c4e2f2022-01-29 10:59:53 +00001717 " check win_move_statusline from bottom window on bottom window
1718 let h0 = winheight(0)
1719 for offset in range(5)
1720 call assert_true(0->win_move_statusline(-offset))
1721 call assert_equal(h0 - offset, winheight(0))
1722 call assert_equal(1 + offset, &cmdheight)
1723 call assert_true(win_move_statusline(0, offset))
1724 call assert_equal(h0, winheight(0))
1725 call assert_equal(1, &cmdheight)
1726 endfor
1727 call assert_true(win_move_statusline(0, 1))
Bram Moolenaara2a89732022-08-31 14:46:18 +01001728 call assert_equal(h0, winheight(0))
1729 call assert_equal(1, &cmdheight)
Daniel Steinbergee630312022-01-10 13:36:34 +00001730 " check win_move_statusline from bottom window on top window ID
1731 let id = win_getid(1)
1732 for offset in range(5)
1733 call assert_true(win_move_statusline(id, offset))
1734 call assert_equal(h + offset, winheight(id))
1735 call assert_true(id->win_move_statusline(-offset))
1736 call assert_equal(h, winheight(id))
1737 endfor
Bram Moolenaar86e67172022-10-31 12:24:12 +00001738
Daniel Steinbergee630312022-01-10 13:36:34 +00001739 " check that win_move_statusline doesn't error with offsets beyond moving
1740 " possibility
1741 call assert_true(win_move_statusline(id, 5000))
1742 call assert_true(winheight(id) > h)
1743 call assert_true(win_move_statusline(id, -5000))
1744 call assert_true(winheight(id) < h)
Bram Moolenaar86e67172022-10-31 12:24:12 +00001745
Daniel Steinbergee630312022-01-10 13:36:34 +00001746 " check that win_move_statusline returns false for an invalid window
1747 wincmd =
1748 let h = winheight(0)
1749 call assert_false(win_move_statusline(-1, 1))
1750 call assert_equal(h, winheight(0))
Bram Moolenaar86e67172022-10-31 12:24:12 +00001751
Daniel Steinbergee630312022-01-10 13:36:34 +00001752 " check that win_move_statusline returns false for a popup window
1753 let id = popup_create(['hello', 'world'], {})
1754 let h = winheight(id)
1755 call assert_false(win_move_statusline(id, 1))
1756 call assert_equal(h, winheight(id))
1757 call popup_close(id)
Bram Moolenaar86e67172022-10-31 12:24:12 +00001758
1759 " check that using another tabpage fails without crash
1760 let id = win_getid()
1761 tabnew
1762 call assert_fails('call win_move_statusline(id, -1)', 'E1308:')
1763 tabclose
1764
Daniel Steinbergee630312022-01-10 13:36:34 +00001765 %bwipe!
1766endfunc
1767
Yegappan Lakshmanan72bb47e2022-04-03 11:22:38 +01001768" Test for window allocation failure
1769func Test_window_alloc_failure()
1770 %bw!
1771
1772 " test for creating a new window above current window
1773 call test_alloc_fail(GetAllocId('newwin_wvars'), 0, 0)
1774 call assert_fails('above new', 'E342:')
1775 call assert_equal(1, winnr('$'))
1776
1777 " test for creating a new window below current window
1778 call test_alloc_fail(GetAllocId('newwin_wvars'), 0, 0)
1779 call assert_fails('below new', 'E342:')
1780 call assert_equal(1, winnr('$'))
1781
1782 " test for popup window creation failure
1783 call test_alloc_fail(GetAllocId('newwin_wvars'), 0, 0)
1784 call assert_fails('call popup_create("Hello", {})', 'E342:')
1785 call assert_equal([], popup_list())
1786
1787 call test_alloc_fail(GetAllocId('newwin_wvars'), 0, 0)
1788 call assert_fails('split', 'E342:')
1789 call assert_equal(1, winnr('$'))
1790
Bram Moolenaarb18b4962022-09-02 21:55:50 +01001791 edit Xwaffile1
1792 edit Xwaffile2
Yegappan Lakshmanan72bb47e2022-04-03 11:22:38 +01001793 call test_alloc_fail(GetAllocId('newwin_wvars'), 0, 0)
Bram Moolenaarb18b4962022-09-02 21:55:50 +01001794 call assert_fails('sb Xwaffile1', 'E342:')
Yegappan Lakshmanan72bb47e2022-04-03 11:22:38 +01001795 call assert_equal(1, winnr('$'))
Bram Moolenaarb18b4962022-09-02 21:55:50 +01001796 call assert_equal('Xwaffile2', @%)
Yegappan Lakshmanan72bb47e2022-04-03 11:22:38 +01001797 %bw!
1798
1799 " FIXME: The following test crashes Vim
1800 " test for new tabpage creation failure
1801 " call test_alloc_fail(GetAllocId('newwin_wvars'), 0, 0)
1802 " call assert_fails('tabnew', 'E342:')
1803 " call assert_equal(1, tabpagenr('$'))
1804 " call assert_equal(1, winnr('$'))
1805
1806 " This test messes up the internal Vim window/frame information. So the
1807 " Test_window_cmd_cmdwin_with_vsp() test fails after running this test.
1808 " Open a new tab and close everything else to fix this issue.
1809 tabnew
1810 tabonly
1811endfunc
Bram Moolenaard0fb9072021-12-09 11:57:22 +00001812
Luuk van Baalfd7e60a2022-09-07 14:42:49 +01001813func Test_win_equal_last_status()
1814 let save_lines = &lines
1815 set lines=20
1816 set splitbelow
1817 set laststatus=0
1818
1819 split | split | quit
1820 call assert_equal(winheight(1), winheight(2))
1821
1822 let &lines = save_lines
1823 set splitbelow&
1824 set laststatus&
1825endfunc
1826
Luuk van Baal13ece2a2022-10-03 15:28:08 +01001827" Test "screen" and "cursor" values for 'splitkeep' with a sequence of
Luuk van Baalfaf1d412022-09-19 16:45:29 +01001828" split operations for various options: with and without a winbar,
1829" tabline, for each possible value of 'laststatus', 'scrolloff',
Luuk van Baal13ece2a2022-10-03 15:28:08 +01001830" 'equalalways', and with the cursor at the top, middle and bottom.
1831func Test_splitkeep_options()
Luuk van Baal594f9e02022-09-16 12:52:58 +01001832 " disallow window resizing
1833 let save_WS = &t_WS
1834 set t_WS=
1835
Luuk van Baal29ab5242022-09-11 16:59:53 +01001836 let gui = has("gui_running")
Luuk van Baal594f9e02022-09-16 12:52:58 +01001837 inoremap <expr> c "<cmd>copen<bar>wincmd k<CR>"
Luuk van Baal13ece2a2022-10-03 15:28:08 +01001838 for run in range(0, 20)
1839 let &splitkeep = run > 10 ? 'topline' : 'screen'
1840 let &scrolloff = (!(run % 4) ? 0 : run)
1841 let &laststatus = (run % 3)
1842 let &splitbelow = (run % 3)
1843 let &equalalways = (run % 2)
1844 let wsb = (run % 2) && &splitbelow
1845 let tl = (gui ? 0 : ((run % 5) ? 1 : 0))
1846 let pos = !(run % 3) ? 'H' : ((run % 2) ? 'M' : 'L')
1847 tabnew | tabonly! | redraw
Luuk van Baal594f9e02022-09-16 12:52:58 +01001848 execute (run % 5) ? 'tabnew' : ''
1849 execute (run % 2) ? 'nnoremenu 1.10 WinBar.Test :echo' : ''
Luuk van Baal594f9e02022-09-16 12:52:58 +01001850 call setline(1, range(1, 256))
1851 " No scroll for restore_snapshot
1852 norm G
1853 try
1854 copen | close | colder
1855 catch /E380/
1856 endtry
1857 call assert_equal(257 - winheight(0), line("w0"))
Luuk van Baal29ab5242022-09-11 16:59:53 +01001858
Luuk van Baal594f9e02022-09-16 12:52:58 +01001859 " No scroll for firstwin horizontal split
1860 execute 'norm gg' . pos
1861 split | redraw | wincmd k
1862 call assert_equal(1, line("w0"))
1863 call assert_equal(&scroll, winheight(0) / 2)
1864 wincmd j
Luuk van Baal13ece2a2022-10-03 15:28:08 +01001865 call assert_equal(&spk == 'topline' ? 1 : win_screenpos(0)[0] - tl - wsb, line("w0"))
Luuk van Baal29ab5242022-09-11 16:59:53 +01001866
Luuk van Baal594f9e02022-09-16 12:52:58 +01001867 " No scroll when resizing windows
Luuk van Baal13ece2a2022-10-03 15:28:08 +01001868 wincmd k | resize +2 | redraw
Luuk van Baal594f9e02022-09-16 12:52:58 +01001869 call assert_equal(1, line("w0"))
1870 wincmd j
Luuk van Baal13ece2a2022-10-03 15:28:08 +01001871 call assert_equal(&spk == 'topline' ? 1 : win_screenpos(0)[0] - tl - wsb, line("w0"))
Luuk van Baal29ab5242022-09-11 16:59:53 +01001872
Luuk van Baal594f9e02022-09-16 12:52:58 +01001873 " No scroll when dragging statusline
1874 call win_move_statusline(1, -3)
Luuk van Baal13ece2a2022-10-03 15:28:08 +01001875 call assert_equal(&spk == 'topline' ? 1 : win_screenpos(0)[0] - tl - wsb, line("w0"))
Luuk van Baal594f9e02022-09-16 12:52:58 +01001876 wincmd k
1877 call assert_equal(1, line("w0"))
Luuk van Baal29ab5242022-09-11 16:59:53 +01001878
Luuk van Baal594f9e02022-09-16 12:52:58 +01001879 " No scroll when changing shellsize
1880 set lines+=2
1881 call assert_equal(1, line("w0"))
1882 wincmd j
Luuk van Baal13ece2a2022-10-03 15:28:08 +01001883 call assert_equal(&spk == 'topline' ? 1 : win_screenpos(0)[0] - tl - wsb, line("w0"))
Luuk van Baal594f9e02022-09-16 12:52:58 +01001884 set lines-=2
Luuk van Baal13ece2a2022-10-03 15:28:08 +01001885 call assert_equal(&spk == 'topline' ? 1 : win_screenpos(0)[0] - tl - wsb, line("w0"))
Luuk van Baal594f9e02022-09-16 12:52:58 +01001886 wincmd k
1887 call assert_equal(1, line("w0"))
Luuk van Baal29ab5242022-09-11 16:59:53 +01001888
Luuk van Baal594f9e02022-09-16 12:52:58 +01001889 " No scroll when equalizing windows
1890 wincmd =
1891 call assert_equal(1, line("w0"))
1892 wincmd j
Luuk van Baal13ece2a2022-10-03 15:28:08 +01001893 call assert_equal(&spk == 'topline' ? 1 : win_screenpos(0)[0] - tl - wsb, line("w0"))
Luuk van Baal594f9e02022-09-16 12:52:58 +01001894 wincmd k
1895 call assert_equal(1, line("w0"))
Luuk van Baal29ab5242022-09-11 16:59:53 +01001896
Luuk van Baal594f9e02022-09-16 12:52:58 +01001897 " No scroll in windows split multiple times
1898 vsplit | split | 4wincmd w
Luuk van Baal13ece2a2022-10-03 15:28:08 +01001899 call assert_equal(&spk == 'topline' ? 1 : win_screenpos(0)[0] - tl - wsb, line("w0"))
Luuk van Baal594f9e02022-09-16 12:52:58 +01001900 1wincmd w | quit | wincmd l | split
Luuk van Baal13ece2a2022-10-03 15:28:08 +01001901 call assert_equal(&spk == 'topline' ? 1 : win_screenpos(0)[0] - tl - wsb, line("w0"))
Luuk van Baal594f9e02022-09-16 12:52:58 +01001902 wincmd j
Luuk van Baal13ece2a2022-10-03 15:28:08 +01001903 call assert_equal(&spk == 'topline' ? 1 : win_screenpos(0)[0] - tl - wsb, line("w0"))
Luuk van Baal29ab5242022-09-11 16:59:53 +01001904
Luuk van Baal594f9e02022-09-16 12:52:58 +01001905 " No scroll in small window
1906 2wincmd w | only | 5split | wincmd k
1907 call assert_equal(1, line("w0"))
1908 wincmd j
Luuk van Baal13ece2a2022-10-03 15:28:08 +01001909 call assert_equal(&spk == 'topline' ? 1 : win_screenpos(0)[0] - tl - wsb, line("w0"))
Luuk van Baal29ab5242022-09-11 16:59:53 +01001910
Luuk van Baal594f9e02022-09-16 12:52:58 +01001911 " No scroll for vertical split
1912 quit | vsplit | wincmd l
1913 call assert_equal(1, line("w0"))
1914 wincmd h
1915 call assert_equal(1, line("w0"))
Luuk van Baal29ab5242022-09-11 16:59:53 +01001916
Luuk van Baal594f9e02022-09-16 12:52:58 +01001917 " No scroll in windows split and quit multiple times
Luuk van Baal13ece2a2022-10-03 15:28:08 +01001918 quit | redraw | split | split | quit | redraw
1919 call assert_equal(&spk == 'topline' ? 1 : win_screenpos(0)[0] - tl - wsb, line("w0"))
Luuk van Baal29ab5242022-09-11 16:59:53 +01001920
Luuk van Baal594f9e02022-09-16 12:52:58 +01001921 " No scroll for new buffer
1922 1wincmd w | only | copen | wincmd k
1923 call assert_equal(1, line("w0"))
1924 only
1925 call assert_equal(1, line("w0"))
1926 above copen | wincmd j
Luuk van Baal13ece2a2022-10-03 15:28:08 +01001927 call assert_equal(&spk == 'topline' ? 1 : win_screenpos(0)[0] - tl, line("w0"))
Luuk van Baal29ab5242022-09-11 16:59:53 +01001928
Luuk van Baal594f9e02022-09-16 12:52:58 +01001929 " No scroll when opening cmdwin, and no cursor move when closing cmdwin.
1930 only | norm ggL
1931 let curpos = getcurpos()
1932 norm q:
1933 call assert_equal(1, line("w0"))
1934 call assert_equal(curpos, getcurpos())
Luuk van Baal29ab5242022-09-11 16:59:53 +01001935
Bram Moolenaarfdbd14e2023-02-14 21:56:42 +00001936 " Scroll when cursor becomes invalid in insert mode.
Luuk van Baal594f9e02022-09-16 12:52:58 +01001937 norm Lic
Luuk van Baalbc3dc292023-02-15 16:45:27 +00001938 call assert_equal(curpos, getcurpos(), 'run ' .. run)
Luuk van Baal29ab5242022-09-11 16:59:53 +01001939
Luuk van Baal594f9e02022-09-16 12:52:58 +01001940 " No scroll when topline not equal to 1
1941 only | execute "norm gg5\<C-e>" | split | wincmd k
1942 call assert_equal(6, line("w0"))
1943 wincmd j
Luuk van Baal13ece2a2022-10-03 15:28:08 +01001944 call assert_equal(&spk == 'topline' ? 6 : 5 + win_screenpos(0)[0] - tl - wsb, line("w0"))
Luuk van Baal29ab5242022-09-11 16:59:53 +01001945 endfor
1946
1947 tabnew | tabonly! | %bwipeout!
1948 iunmap c
Luuk van Baal29ab5242022-09-11 16:59:53 +01001949 set scrolloff&
1950 set splitbelow&
1951 set laststatus&
1952 set equalalways&
Luuk van Baal13ece2a2022-10-03 15:28:08 +01001953 set splitkeep&
Luuk van Baal594f9e02022-09-16 12:52:58 +01001954 let &t_WS = save_WS
Luuk van Baal29ab5242022-09-11 16:59:53 +01001955endfunc
Luuk van Baalfd7e60a2022-09-07 14:42:49 +01001956
Bram Moolenaare0f86912023-03-01 17:55:31 +00001957func Test_splitkeep_cmdwin_cursor_position()
Luuk van Baal13ece2a2022-10-03 15:28:08 +01001958 set splitkeep=screen
mityu12167d82022-09-15 17:44:07 +01001959 call setline(1, range(&lines))
1960
1961 " No scroll when cursor is at near bottom of window and cusor position
1962 " recompution (done by line('w0') in this test) happens while in cmdwin.
1963 normal! G
1964 let firstline = line('w0')
1965 autocmd CmdwinEnter * ++once autocmd WinEnter * ++once call line('w0')
1966 execute "normal! q:\<C-w>q"
1967 redraw!
1968 call assert_equal(firstline, line('w0'))
1969
1970 " User script can change cursor position successfully while in cmdwin and it
1971 " shouldn't be changed when closing cmdwin.
1972 execute "normal! Gq:\<Cmd>call win_execute(winnr('#')->win_getid(), 'call cursor(1, 1)')\<CR>\<C-w>q"
1973 call assert_equal(1, line('.'))
1974 call assert_equal(1, col('.'))
1975
1976 execute "normal! Gq:\<Cmd>autocmd WinEnter * ++once call cursor(1, 1)\<CR>\<C-w>q"
1977 call assert_equal(1, line('.'))
1978 call assert_equal(1, col('.'))
1979
1980 %bwipeout!
Luuk van Baal13ece2a2022-10-03 15:28:08 +01001981 set splitkeep&
Bram Moolenaare0f86912023-03-01 17:55:31 +00001982endfunc
Luuk van Baald5bc7622022-09-17 16:16:35 +01001983
Bram Moolenaare0f86912023-03-01 17:55:31 +00001984func Test_splitkeep_misc()
Luuk van Baal13ece2a2022-10-03 15:28:08 +01001985 set splitkeep=screen
Luuk van Baald5bc7622022-09-17 16:16:35 +01001986
1987 call setline(1, range(1, &lines))
Luuk van Baala109f392023-06-02 14:16:35 +01001988 " Cursor is adjusted to start and end of buffer
1989 norm M
1990 wincmd s
1991 resize 1
1992 call assert_equal(1, line('.'))
1993 wincmd j
1994 norm GM
1995 resize 1
1996 call assert_equal(&lines, line('.'))
1997 only!
1998
1999 set splitbelow
Luuk van Baald5bc7622022-09-17 16:16:35 +01002000 norm Gzz
2001 let top = line('w0')
Luuk van Baalfaf1d412022-09-19 16:45:29 +01002002 " No scroll when aucmd_win is opened
Luuk van Baald5bc7622022-09-17 16:16:35 +01002003 call setbufvar(bufnr("test", 1) , '&buftype', 'nofile')
2004 call assert_equal(top, line('w0'))
Luuk van Baalfaf1d412022-09-19 16:45:29 +01002005 " No scroll when tab is changed/closed
2006 tab help | close
2007 call assert_equal(top, line('w0'))
2008 " No scroll when help is closed and buffer line count < window height
2009 norm ggdG
2010 call setline(1, range(1, &lines - 10))
Luuk van Baald5bc7622022-09-17 16:16:35 +01002011 norm G
2012 let top = line('w0')
2013 help | quit
2014 call assert_equal(top, line('w0'))
Luuk van Baal346823d2022-10-05 18:26:42 +01002015 " No error when resizing window in autocmd and buffer length changed
2016 autocmd FileType qf exe "resize" line('$')
2017 cexpr getline(1, '$')
2018 copen
2019 wincmd p
2020 norm dd
2021 cexpr getline(1, '$')
Luuk van Baald5bc7622022-09-17 16:16:35 +01002022
Luuk van Baalfaf1d412022-09-19 16:45:29 +01002023 %bwipeout!
Luuk van Baald5bc7622022-09-17 16:16:35 +01002024 set splitbelow&
Luuk van Baal13ece2a2022-10-03 15:28:08 +01002025 set splitkeep&
Luuk van Baald5bc7622022-09-17 16:16:35 +01002026endfunc
2027
Luuk van Baal16af9132023-08-20 20:44:59 +02002028func Test_splitkeep_cursor()
2029 CheckScreendump
2030 let lines =<< trim END
2031 set splitkeep=screen
2032 autocmd CursorMoved * wincmd p | wincmd p
2033 call setline(1, range(1, 200))
2034 func CursorEqualize()
2035 call cursor(100, 1)
2036 wincmd =
2037 endfunc
2038 wincmd s
2039 call CursorEqualize()
2040 END
2041 call writefile(lines, 'XTestSplitkeepCallback', 'D')
2042 let buf = RunVimInTerminal('-S XTestSplitkeepCallback', #{rows: 8})
2043
2044 call VerifyScreenDump(buf, 'Test_splitkeep_cursor_1', {})
2045
2046 call term_sendkeys(buf, "j")
2047 call VerifyScreenDump(buf, 'Test_splitkeep_cursor_2', {})
2048
2049 call term_sendkeys(buf, ":set scrolloff=0\<CR>G")
2050 call VerifyScreenDump(buf, 'Test_splitkeep_cursor_3', {})
2051
2052 call StopVimInTerminal(buf)
2053endfunc
2054
Bram Moolenaare0f86912023-03-01 17:55:31 +00002055func Test_splitkeep_callback()
Luuk van Baal20e58562022-09-23 12:57:09 +01002056 CheckScreendump
2057 let lines =<< trim END
Luuk van Baal13ece2a2022-10-03 15:28:08 +01002058 set splitkeep=screen
Luuk van Baal20e58562022-09-23 12:57:09 +01002059 call setline(1, range(&lines))
Luuk van Baal13ece2a2022-10-03 15:28:08 +01002060 function C1(a, b)
Luuk van Baal20e58562022-09-23 12:57:09 +01002061 split | wincmd p
2062 endfunction
Luuk van Baal13ece2a2022-10-03 15:28:08 +01002063 function C2(a, b)
Luuk van Baal20e58562022-09-23 12:57:09 +01002064 close | split
2065 endfunction
Luuk van Baal13ece2a2022-10-03 15:28:08 +01002066 nn j <cmd>call job_start([&sh, &shcf, "true"], { 'exit_cb': 'C1' })<CR>
2067 nn t <cmd>call popup_create(term_start([&sh, &shcf, "true"],
2068 \ { 'hidden': 1, 'exit_cb': 'C2' }), {})<CR>
Luuk van Baal20e58562022-09-23 12:57:09 +01002069 END
Luuk van Baal13ece2a2022-10-03 15:28:08 +01002070 call writefile(lines, 'XTestSplitkeepCallback', 'D')
2071 let buf = RunVimInTerminal('-S XTestSplitkeepCallback', #{rows: 8})
Luuk van Baal20e58562022-09-23 12:57:09 +01002072
2073 call term_sendkeys(buf, "j")
Luuk van Baal13ece2a2022-10-03 15:28:08 +01002074 call VerifyScreenDump(buf, 'Test_splitkeep_callback_1', {})
Luuk van Baal20e58562022-09-23 12:57:09 +01002075
Luuk van Baal13ece2a2022-10-03 15:28:08 +01002076 call term_sendkeys(buf, ":quit\<CR>Ht")
2077 call VerifyScreenDump(buf, 'Test_splitkeep_callback_2', {})
Luuk van Baal20e58562022-09-23 12:57:09 +01002078
2079 call term_sendkeys(buf, ":set sb\<CR>:quit\<CR>Gj")
Luuk van Baal13ece2a2022-10-03 15:28:08 +01002080 call VerifyScreenDump(buf, 'Test_splitkeep_callback_3', {})
Luuk van Baal20e58562022-09-23 12:57:09 +01002081
Luuk van Baal13ece2a2022-10-03 15:28:08 +01002082 call term_sendkeys(buf, ":quit\<CR>Gt")
2083 call VerifyScreenDump(buf, 'Test_splitkeep_callback_4', {})
zeertzjq378e6c02023-01-14 11:46:49 +00002084
2085 call StopVimInTerminal(buf)
Luuk van Baal20e58562022-09-23 12:57:09 +01002086endfunc
2087
Bram Moolenaare0f86912023-03-01 17:55:31 +00002088func Test_splitkeep_fold()
Luuk van Baal13ece2a2022-10-03 15:28:08 +01002089 CheckScreendump
Luuk van Baal7c1cbb62022-09-27 12:31:15 +01002090
Luuk van Baal13ece2a2022-10-03 15:28:08 +01002091 let lines =<< trim END
2092 set splitkeep=screen
2093 set foldmethod=marker
2094 set number
2095 let line = 1
2096 for n in range(1, &lines)
2097 call setline(line, ['int FuncName() {/*{{{*/', 1, 2, 3, 4, 5, '}/*}}}*/',
2098 \ 'after fold'])
2099 let line += 8
2100 endfor
2101 END
2102 call writefile(lines, 'XTestSplitkeepFold', 'D')
2103 let buf = RunVimInTerminal('-S XTestSplitkeepFold', #{rows: 10})
Luuk van Baal7c1cbb62022-09-27 12:31:15 +01002104
2105 call term_sendkeys(buf, "L:wincmd s\<CR>")
Luuk van Baal13ece2a2022-10-03 15:28:08 +01002106 call VerifyScreenDump(buf, 'Test_splitkeep_fold_1', {})
Luuk van Baal7c1cbb62022-09-27 12:31:15 +01002107
2108 call term_sendkeys(buf, ":quit\<CR>")
Luuk van Baal13ece2a2022-10-03 15:28:08 +01002109 call VerifyScreenDump(buf, 'Test_splitkeep_fold_2', {})
Luuk van Baal7c1cbb62022-09-27 12:31:15 +01002110
2111 call term_sendkeys(buf, "H:below split\<CR>")
Luuk van Baal13ece2a2022-10-03 15:28:08 +01002112 call VerifyScreenDump(buf, 'Test_splitkeep_fold_3', {})
Luuk van Baal7c1cbb62022-09-27 12:31:15 +01002113
2114 call term_sendkeys(buf, ":wincmd k\<CR>:quit\<CR>")
Luuk van Baal13ece2a2022-10-03 15:28:08 +01002115 call VerifyScreenDump(buf, 'Test_splitkeep_fold_4', {})
zeertzjq378e6c02023-01-14 11:46:49 +00002116
2117 call StopVimInTerminal(buf)
Bram Moolenaare0f86912023-03-01 17:55:31 +00002118endfunc
Luuk van Baal7c1cbb62022-09-27 12:31:15 +01002119
Bram Moolenaare0f86912023-03-01 17:55:31 +00002120func Test_splitkeep_status()
Luuk van Baal74a694d2022-11-28 16:49:36 +00002121 CheckScreendump
2122
2123 let lines =<< trim END
2124 call setline(1, ['a', 'b', 'c'])
2125 set nomodified
2126 set splitkeep=screen
2127 let win = winnr()
2128 wincmd s
2129 wincmd j
2130 END
2131 call writefile(lines, 'XTestSplitkeepStatus', 'D')
2132 let buf = RunVimInTerminal('-S XTestSplitkeepStatus', #{rows: 10})
2133
2134 call term_sendkeys(buf, ":call win_move_statusline(win, 1)\<CR>")
2135 call VerifyScreenDump(buf, 'Test_splitkeep_status_1', {})
zeertzjq378e6c02023-01-14 11:46:49 +00002136
2137 call StopVimInTerminal(buf)
Bram Moolenaare0f86912023-03-01 17:55:31 +00002138endfunc
Luuk van Baal74a694d2022-11-28 16:49:36 +00002139
Luuk van Baalb926bf42023-05-06 12:53:50 +01002140" skipcol is not reset unnecessarily and is copied to new window
2141func Test_splitkeep_skipcol()
2142 CheckScreendump
2143
2144 let lines =<< trim END
2145 set splitkeep=topline smoothscroll splitbelow scrolloff=0
2146 call setline(1, 'with lots of text in one line '->repeat(6))
2147 norm 2
2148 wincmd s
2149 END
2150
2151 call writefile(lines, 'XTestSplitkeepSkipcol', 'D')
2152 let buf = RunVimInTerminal('-S XTestSplitkeepSkipcol', #{rows: 12, cols: 40})
2153
2154 call VerifyScreenDump(buf, 'Test_splitkeep_skipcol_1', {})
2155endfunc
2156
Bram Moolenaare0f86912023-03-01 17:55:31 +00002157func Test_new_help_window_on_error()
Rob Pillingcb94c912022-12-13 12:26:09 +00002158 help change.txt
2159 execute "normal! /CTRL-@\<CR>"
2160 silent! execute "normal! \<C-W>]"
2161
2162 let wincount = winnr('$')
2163 help 'mod'
2164
2165 call assert_equal(wincount, winnr('$'))
2166 call assert_equal(expand("<cword>"), "'mod'")
Bram Moolenaare0f86912023-03-01 17:55:31 +00002167endfunc
2168
Sean Dewar0fd44a52024-02-20 20:28:15 +01002169func Test_splitmove_flatten_frame()
2170 split
2171 vsplit
2172
2173 wincmd L
2174 let layout = winlayout()
2175 wincmd K
2176 wincmd L
2177 call assert_equal(winlayout(), layout)
2178
2179 only!
2180endfunc
2181
Sean Dewar02fcae02024-02-21 19:40:44 +01002182func Test_autocmd_window_force_room()
Sean Dewar0fd44a52024-02-20 20:28:15 +01002183 " Open as many windows as possible
2184 while v:true
2185 try
2186 split
2187 catch /E36:/
2188 break
2189 endtry
2190 endwhile
2191 while v:true
2192 try
2193 vsplit
2194 catch /E36:/
2195 break
2196 endtry
2197 endwhile
2198
2199 wincmd j
2200 vsplit
2201 call assert_fails('wincmd H', 'E36:')
2202 call assert_fails('wincmd J', 'E36:')
2203 call assert_fails('wincmd K', 'E36:')
2204 call assert_fails('wincmd L', 'E36:')
2205
2206 edit unload me
2207 enew
2208 bunload! unload\ me
Sean Dewar02fcae02024-02-21 19:40:44 +01002209 augroup AucmdWinForceRoom
Sean Dewar0fd44a52024-02-20 20:28:15 +01002210 au!
2211 au BufEnter * ++once let s:triggered = v:true
2212 \| call assert_equal('autocmd', win_gettype())
2213 augroup END
2214 let layout = winlayout()
2215 let restcmd = winrestcmd()
2216 " bufload opening the autocommand window shouldn't give E36.
2217 call bufload('unload me')
2218 call assert_equal(v:true, s:triggered)
2219 call assert_equal(winlayout(), layout)
2220 call assert_equal(winrestcmd(), restcmd)
2221
2222 unlet! s:triggered
Sean Dewar02fcae02024-02-21 19:40:44 +01002223 au! AucmdWinForceRoom
2224 augroup! AucmdWinForceRoom
Sean Dewar0fd44a52024-02-20 20:28:15 +01002225 %bw!
2226endfunc
Rob Pillingcb94c912022-12-13 12:26:09 +00002227
Sean Dewarf8658952024-02-20 22:05:10 +01002228func Test_win_gotoid_splitmove_textlock_cmdwin()
2229 call setline(1, 'foo')
2230 new
2231 let curwin = win_getid()
2232 call setline(1, 'bar')
2233
2234 set debug+=throw indentexpr=win_gotoid(win_getid(winnr('#')))
2235 call assert_fails('normal! ==', 'E565:')
2236 call assert_equal(curwin, win_getid())
Sean Dewar2a65e732024-02-22 19:53:33 +01002237 " No error if attempting to switch to curwin; nothing happens.
2238 set indentexpr=assert_equal(1,win_gotoid(win_getid()))
2239 normal! ==
2240 call assert_equal(curwin, win_getid())
Sean Dewarf8658952024-02-20 22:05:10 +01002241
2242 set indentexpr=win_splitmove(winnr('#'),winnr())
2243 call assert_fails('normal! ==', 'E565:')
2244 call assert_equal(curwin, win_getid())
2245
2246 %bw!
2247 set debug-=throw indentexpr&
2248
2249 call feedkeys('q:'
2250 \ .. ":call assert_fails('call win_splitmove(winnr(''#''), winnr())', 'E11:')\<CR>"
2251 \ .. ":call assert_equal('command', win_gettype())\<CR>"
2252 \ .. ":call assert_equal('', win_gettype(winnr('#')))\<CR>", 'ntx')
2253
2254 call feedkeys('q:'
2255 \ .. ":call assert_fails('call win_gotoid(win_getid(winnr(''#'')))', 'E11:')\<CR>"
Sean Dewar2a65e732024-02-22 19:53:33 +01002256 "\ No error if attempting to switch to curwin; nothing happens.
2257 \ .. ":call assert_equal(1, win_gotoid(win_getid()))\<CR>"
Sean Dewarf8658952024-02-20 22:05:10 +01002258 \ .. ":call assert_equal('command', win_gettype())\<CR>"
2259 \ .. ":call assert_equal('', win_gettype(winnr('#')))\<CR>", 'ntx')
2260endfunc
2261
Bram Moolenaar9e4d8212016-08-18 23:04:48 +02002262" vim: shiftwidth=2 sts=2 expandtab