blob: a70fb790c57788006f0abcd1386598f77281fab0 [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
1194 au! WinSplitMove
1195 augroup! WinSplitMove
1196 %bw!
Bram Moolenaard20dcb32019-09-10 21:22:58 +02001197endfunc
1198
Bram Moolenaare20b9ec2020-02-03 21:40:04 +01001199" Test for the :only command
1200func Test_window_only()
1201 new
1202 set modified
1203 new
1204 call assert_fails('only', 'E445:')
1205 only!
Bram Moolenaarbc2b71d2020-02-17 21:33:30 +01001206 " Test for :only with a count
1207 let wid = win_getid()
1208 new
1209 new
1210 3only
1211 call assert_equal(1, winnr('$'))
1212 call assert_equal(wid, win_getid())
1213 call assert_fails('close', 'E444:')
1214 call assert_fails('%close', 'E16:')
Bram Moolenaare20b9ec2020-02-03 21:40:04 +01001215endfunc
1216
Bram Moolenaar9f6277b2020-02-11 22:04:02 +01001217" Test for errors with :wincmd
1218func Test_wincmd_errors()
1219 call assert_fails('wincmd g', 'E474:')
1220 call assert_fails('wincmd ab', 'E474:')
1221endfunc
1222
1223" Test for errors with :winpos
1224func Test_winpos_errors()
1225 if !has("gui_running") && !has('win32')
1226 call assert_fails('winpos', 'E188:')
1227 endif
1228 call assert_fails('winpos 10', 'E466:')
1229endfunc
1230
Bram Moolenaar406cd902020-02-18 21:54:41 +01001231" Test for +cmd in a :split command
1232func Test_split_cmd()
1233 split +set\ readonly
1234 call assert_equal(1, &readonly)
1235 call assert_equal(2, winnr('$'))
1236 close
1237endfunc
1238
Bram Moolenaar5080b0a2020-03-22 21:23:47 +01001239" Create maximum number of horizontally or vertically split windows and then
1240" run commands that create a new horizontally/vertically split window
1241func Run_noroom_for_newwindow_test(dir_arg)
1242 let dir = (a:dir_arg == 'v') ? 'vert ' : ''
1243
1244 " Open as many windows as possible
Bram Moolenaarab505b12020-03-23 19:28:44 +01001245 while v:true
Bram Moolenaar5080b0a2020-03-22 21:23:47 +01001246 try
1247 exe dir . 'new'
1248 catch /E36:/
1249 break
1250 endtry
Bram Moolenaarab505b12020-03-23 19:28:44 +01001251 endwhile
Bram Moolenaar5080b0a2020-03-22 21:23:47 +01001252
Bram Moolenaardb4c9472022-10-15 22:06:06 +01001253 call writefile(['first', 'second', 'third'], 'Xnorfile1', 'D')
1254 call writefile([], 'Xnorfile2', 'D')
1255 call writefile([], 'Xnorfile3', 'D')
Bram Moolenaar5080b0a2020-03-22 21:23:47 +01001256
1257 " Argument list related commands
Bram Moolenaarb18b4962022-09-02 21:55:50 +01001258 args Xnorfile1 Xnorfile2 Xnorfile3
Bram Moolenaar5080b0a2020-03-22 21:23:47 +01001259 next
1260 for cmd in ['sargument 2', 'snext', 'sprevious', 'sNext', 'srewind',
1261 \ 'sfirst', 'slast']
1262 call assert_fails(dir .. cmd, 'E36:')
1263 endfor
1264 %argdelete
1265
1266 " Buffer related commands
1267 set modified
1268 hide enew
Bram Moolenaarb18b4962022-09-02 21:55:50 +01001269 for cmd in ['sbuffer Xnorfile1', 'sbnext', 'sbprevious', 'sbNext', 'sbrewind',
Bram Moolenaar5080b0a2020-03-22 21:23:47 +01001270 \ 'sbfirst', 'sblast', 'sball', 'sbmodified', 'sunhide']
1271 call assert_fails(dir .. cmd, 'E36:')
1272 endfor
1273
1274 " Window related commands
Bram Moolenaarb18b4962022-09-02 21:55:50 +01001275 for cmd in ['split', 'split Xnorfile2', 'new', 'new Xnorfile3', 'sview Xnorfile1',
Bram Moolenaar5080b0a2020-03-22 21:23:47 +01001276 \ 'sfind runtest.vim']
1277 call assert_fails(dir .. cmd, 'E36:')
1278 endfor
1279
1280 " Help
1281 call assert_fails(dir .. 'help', 'E36:')
1282 call assert_fails(dir .. 'helpgrep window', 'E36:')
1283
1284 " Command-line window
1285 if a:dir_arg == 'h'
1286 " Cmd-line window is always a horizontally split window
1287 call assert_beeps('call feedkeys("q:\<CR>", "xt")')
1288 endif
1289
1290 " Quickfix and location list window
1291 if has('quickfix')
1292 cexpr ''
1293 call assert_fails(dir .. 'copen', 'E36:')
1294 lexpr ''
1295 call assert_fails(dir .. 'lopen', 'E36:')
1296
1297 " Preview window
Bram Moolenaarb18b4962022-09-02 21:55:50 +01001298 call assert_fails(dir .. 'pedit Xnorfile2', 'E36:')
Bram Moolenaar5080b0a2020-03-22 21:23:47 +01001299 call setline(1, 'abc')
1300 call assert_fails(dir .. 'psearch abc', 'E36:')
1301 endif
1302
1303 " Window commands (CTRL-W ^ and CTRL-W f)
1304 if a:dir_arg == 'h'
1305 call assert_fails('call feedkeys("\<C-W>^", "xt")', 'E36:')
Bram Moolenaarb18b4962022-09-02 21:55:50 +01001306 call setline(1, 'Xnorfile1')
Bram Moolenaar5080b0a2020-03-22 21:23:47 +01001307 call assert_fails('call feedkeys("gg\<C-W>f", "xt")', 'E36:')
1308 endif
1309 enew!
1310
1311 " Tag commands (:stag, :stselect and :stjump)
1312 call writefile(["!_TAG_FILE_ENCODING\tutf-8\t//",
Bram Moolenaarb18b4962022-09-02 21:55:50 +01001313 \ "second\tXnorfile1\t2",
1314 \ "third\tXnorfile1\t3",],
Bram Moolenaar5080b0a2020-03-22 21:23:47 +01001315 \ 'Xtags')
1316 set tags=Xtags
1317 call assert_fails(dir .. 'stag second', 'E36:')
1318 call assert_fails('call feedkeys(":" .. dir .. "stselect second\n1\n", "xt")', 'E36:')
1319 call assert_fails(dir .. 'stjump second', 'E36:')
1320 call assert_fails(dir .. 'ptag second', 'E36:')
1321 set tags&
1322 call delete('Xtags')
1323
1324 " :isplit and :dsplit
1325 call setline(1, ['#define FOO 1', 'FOO'])
1326 normal 2G
1327 call assert_fails(dir .. 'isplit FOO', 'E36:')
1328 call assert_fails(dir .. 'dsplit FOO', 'E36:')
1329
1330 " terminal
1331 if has('terminal')
1332 call assert_fails(dir .. 'terminal', 'E36:')
1333 endif
1334
1335 %bwipe!
Bram Moolenaar5080b0a2020-03-22 21:23:47 +01001336 only
1337endfunc
1338
1339func Test_split_cmds_with_no_room()
1340 call Run_noroom_for_newwindow_test('h')
1341 call Run_noroom_for_newwindow_test('v')
1342endfunc
1343
Bram Moolenaarad48e6c2020-04-21 22:19:45 +02001344" Test for various wincmd failures
1345func Test_wincmd_fails()
1346 only!
1347 call assert_beeps("normal \<C-W>w")
1348 call assert_beeps("normal \<C-W>p")
1349 call assert_beeps("normal \<C-W>gk")
1350 call assert_beeps("normal \<C-W>r")
1351 call assert_beeps("normal \<C-W>K")
1352 call assert_beeps("normal \<C-W>H")
1353 call assert_beeps("normal \<C-W>2gt")
1354endfunc
1355
Bram Moolenaarfe6dce82020-09-04 14:41:21 +02001356func Test_window_resize()
1357 " Vertical :resize (absolute, relative, min and max size).
1358 vsplit
1359 vert resize 8
1360 call assert_equal(8, winwidth(0))
1361 vert resize +2
1362 call assert_equal(10, winwidth(0))
1363 vert resize -2
1364 call assert_equal(8, winwidth(0))
1365 vert resize
1366 call assert_equal(&columns - 2, winwidth(0))
1367 vert resize 0
1368 call assert_equal(1, winwidth(0))
1369 vert resize 99999
1370 call assert_equal(&columns - 2, winwidth(0))
1371
1372 %bwipe!
1373
1374 " Horizontal :resize (with absolute, relative size, min and max size).
1375 split
1376 resize 8
1377 call assert_equal(8, winheight(0))
1378 resize +2
1379 call assert_equal(10, winheight(0))
1380 resize -2
1381 call assert_equal(8, winheight(0))
1382 resize
1383 call assert_equal(&lines - 4, winheight(0))
1384 resize 0
1385 call assert_equal(1, winheight(0))
1386 resize 99999
1387 call assert_equal(&lines - 4, winheight(0))
1388
1389 " :resize with explicit window number.
1390 let other_winnr = winnr('j')
1391 exe other_winnr .. 'resize 10'
1392 call assert_equal(10, winheight(other_winnr))
1393 call assert_equal(&lines - 10 - 3, winheight(0))
Bram Moolenaar9668cc52020-10-17 17:39:55 +02001394 exe other_winnr .. 'resize +1'
1395 exe other_winnr .. 'resize +1'
1396 call assert_equal(12, winheight(other_winnr))
1397 call assert_equal(&lines - 10 - 3 -2, winheight(0))
Bram Moolenaar89015a62020-12-29 12:46:51 +01001398 close
1399
1400 vsplit
1401 wincmd l
1402 let other_winnr = winnr('h')
1403 call assert_notequal(winnr(), other_winnr)
Bram Moolenaar5efe0e52021-01-01 14:31:34 +01001404 exe 'vert ' .. other_winnr .. 'resize -' .. &columns
Bram Moolenaar89015a62020-12-29 12:46:51 +01001405 call assert_equal(0, winwidth(other_winnr))
Bram Moolenaarfe6dce82020-09-04 14:41:21 +02001406
1407 %bwipe!
1408endfunc
1409
Bram Moolenaar5d3c9f82020-06-26 20:41:39 +02001410" Test for adjusting the window width when a window is closed with some
1411" windows using 'winfixwidth'
1412func Test_window_width_adjust()
1413 only
1414 " Three vertical windows. Windows 1 and 2 have 'winfixwidth' set and close
1415 " window 2.
1416 wincmd v
1417 vert resize 10
1418 set winfixwidth
1419 wincmd v
1420 set winfixwidth
1421 wincmd c
1422 call assert_inrange(10, 12, winwidth(1))
1423 " Three vertical windows. Windows 2 and 3 have 'winfixwidth' set and close
1424 " window 3.
1425 only
1426 set winfixwidth
1427 wincmd v
1428 vert resize 10
1429 set winfixwidth
1430 wincmd v
1431 set nowinfixwidth
1432 wincmd b
1433 wincmd c
1434 call assert_inrange(10, 12, winwidth(2))
1435
1436 new | only
1437endfunc
1438
1439" Test for jumping to a vertical/horizontal neighbor window based on the
1440" current cursor position
dundargocc57b5bc2022-11-02 13:30:51 +00001441func Test_window_goto_neighbor()
Bram Moolenaar5d3c9f82020-06-26 20:41:39 +02001442 %bw!
1443
1444 " Vertical window movement
1445
1446 " create the following window layout:
1447 " +--+--+
1448 " |w1|w3|
1449 " +--+ |
1450 " |w2| |
1451 " +--+--+
1452 " |w4 |
1453 " +-----+
1454 new
1455 vsplit
1456 split
1457 " vertically jump from w4
1458 wincmd b
1459 call setline(1, repeat(' ', &columns))
1460 call cursor(1, 1)
1461 wincmd k
1462 call assert_equal(2, winnr())
1463 wincmd b
1464 call cursor(1, &columns)
1465 redraw!
1466 wincmd k
1467 call assert_equal(3, winnr())
1468 %bw!
1469
1470 " create the following window layout:
1471 " +--+--+--+
1472 " |w1|w2|w3|
1473 " +--+--+--+
1474 " |w4 |
1475 " +--------+
1476 new
1477 vsplit
1478 vsplit
1479 wincmd b
1480 call setline(1, repeat(' ', &columns))
1481 call cursor(1, 1)
1482 wincmd k
1483 call assert_equal(1, winnr())
1484 wincmd b
1485 call cursor(1, &columns / 2)
1486 redraw!
1487 wincmd k
1488 call assert_equal(2, winnr())
1489 wincmd b
1490 call cursor(1, &columns)
1491 redraw!
1492 wincmd k
1493 call assert_equal(3, winnr())
1494 %bw!
1495
1496 " Horizontal window movement
1497
1498 " create the following window layout:
1499 " +--+--+--+
1500 " |w1|w2|w4|
1501 " +--+--+ |
1502 " |w3 | |
1503 " +-----+--+
1504 vsplit
1505 split
1506 vsplit
1507 4wincmd l
1508 call setline(1, repeat([' '], &lines))
1509 call cursor(1, 1)
1510 redraw!
1511 wincmd h
1512 call assert_equal(2, winnr())
1513 4wincmd l
1514 call cursor(&lines, 1)
1515 redraw!
1516 wincmd h
1517 call assert_equal(3, winnr())
1518 %bw!
1519
1520 " create the following window layout:
1521 " +--+--+
1522 " |w1|w4|
1523 " +--+ +
1524 " |w2| |
1525 " +--+ +
1526 " |w3| |
1527 " +--+--+
1528 vsplit
1529 split
1530 split
1531 wincmd l
1532 call setline(1, repeat([' '], &lines))
1533 call cursor(1, 1)
1534 redraw!
1535 wincmd h
1536 call assert_equal(1, winnr())
1537 wincmd l
1538 call cursor(&lines / 2, 1)
1539 redraw!
1540 wincmd h
1541 call assert_equal(2, winnr())
1542 wincmd l
1543 call cursor(&lines, 1)
1544 redraw!
1545 wincmd h
1546 call assert_equal(3, winnr())
1547 %bw!
1548endfunc
1549
1550" Test for an autocmd closing the destination window when jumping from one
1551" window to another.
1552func Test_close_dest_window()
1553 split
Bram Moolenaarb18b4962022-09-02 21:55:50 +01001554 edit Xdstfile
Bram Moolenaar5d3c9f82020-06-26 20:41:39 +02001555
1556 " Test for BufLeave
1557 augroup T1
1558 au!
Bram Moolenaarb18b4962022-09-02 21:55:50 +01001559 au BufLeave Xdstfile $wincmd c
Bram Moolenaar5d3c9f82020-06-26 20:41:39 +02001560 augroup END
1561 wincmd b
1562 call assert_equal(1, winnr('$'))
Bram Moolenaarb18b4962022-09-02 21:55:50 +01001563 call assert_equal('Xdstfile', @%)
Bram Moolenaar5d3c9f82020-06-26 20:41:39 +02001564 augroup T1
1565 au!
1566 augroup END
1567
1568 " Test for WinLeave
1569 new
1570 wincmd p
1571 augroup T1
1572 au!
1573 au WinLeave * 1wincmd c
1574 augroup END
1575 wincmd t
1576 call assert_equal(1, winnr('$'))
Bram Moolenaarb18b4962022-09-02 21:55:50 +01001577 call assert_equal('Xdstfile', @%)
Bram Moolenaar5d3c9f82020-06-26 20:41:39 +02001578 augroup T1
1579 au!
1580 augroup END
1581 augroup! T1
1582 %bw!
1583endfunc
1584
Bram Moolenaard0fb9072021-12-09 11:57:22 +00001585func Test_window_minimal_size()
1586 set winminwidth=0 winminheight=0
1587
1588 " check size is fixed vertically
1589 new
1590 call win_execute(win_getid(2), 'wincmd _')
1591 call assert_equal(0, winheight(0))
1592 call feedkeys('0', 'tx')
1593 call assert_equal(1, winheight(0))
1594 bwipe!
1595
1596 " check size is fixed horizontally
1597 vert new
1598 call win_execute(win_getid(2), 'wincmd |')
1599 call assert_equal(0, winwidth(0))
1600 call feedkeys('0', 'tx')
1601 call assert_equal(1, winwidth(0))
1602 bwipe!
1603
1604 if has('timers')
1605 " check size is fixed in Insert mode
ichizokfa9a8e02021-12-12 16:42:09 +00001606 func s:CheckSize(timer) abort
1607 call win_execute(win_getid(2), 'wincmd _')
1608 call assert_equal(0, winheight(0))
1609 call feedkeys(" \<Esc>", 't!')
1610 endfunc
Bram Moolenaard0fb9072021-12-09 11:57:22 +00001611 new
ichizokfa9a8e02021-12-12 16:42:09 +00001612 call timer_start(100, function('s:CheckSize'))
Bram Moolenaard0fb9072021-12-09 11:57:22 +00001613 call feedkeys('a', 'tx!')
1614 call assert_equal(1, winheight(0))
1615 bwipe!
ichizokfa9a8e02021-12-12 16:42:09 +00001616 delfunc s:CheckSize
Bram Moolenaard0fb9072021-12-09 11:57:22 +00001617 endif
1618
1619 set winminwidth& winminheight&
1620endfunc
1621
Daniel Steinbergee630312022-01-10 13:36:34 +00001622func Test_win_move_separator()
1623 edit a
1624 leftabove vsplit b
1625 let w = winwidth(0)
1626 " check win_move_separator from left window on left window
1627 call assert_equal(1, winnr())
1628 for offset in range(5)
1629 call assert_true(win_move_separator(0, offset))
1630 call assert_equal(w + offset, winwidth(0))
1631 call assert_true(0->win_move_separator(-offset))
1632 call assert_equal(w, winwidth(0))
1633 endfor
1634 " check win_move_separator from right window on left window number
1635 wincmd l
1636 call assert_notequal(1, winnr())
1637 for offset in range(5)
1638 call assert_true(1->win_move_separator(offset))
1639 call assert_equal(w + offset, winwidth(1))
1640 call assert_true(win_move_separator(1, -offset))
1641 call assert_equal(w, winwidth(1))
1642 endfor
1643 " check win_move_separator from right window on left window ID
1644 let id = win_getid(1)
1645 for offset in range(5)
1646 call assert_true(win_move_separator(id, offset))
1647 call assert_equal(w + offset, winwidth(id))
1648 call assert_true(id->win_move_separator(-offset))
1649 call assert_equal(w, winwidth(id))
1650 endfor
zeertzjqa0c4e2f2022-01-29 10:59:53 +00001651 " check win_move_separator from right window on right window is no-op
1652 let w0 = winwidth(0)
1653 call assert_true(win_move_separator(0, 1))
1654 call assert_equal(w0, winwidth(0))
1655 call assert_true(win_move_separator(0, -1))
1656 call assert_equal(w0, winwidth(0))
zeertzjq873f41a2022-11-01 11:44:43 +00001657
Daniel Steinbergee630312022-01-10 13:36:34 +00001658 " check that win_move_separator doesn't error with offsets beyond moving
1659 " possibility
1660 call assert_true(win_move_separator(id, 5000))
1661 call assert_true(winwidth(id) > w)
1662 call assert_true(win_move_separator(id, -5000))
1663 call assert_true(winwidth(id) < w)
zeertzjq873f41a2022-11-01 11:44:43 +00001664
Daniel Steinbergee630312022-01-10 13:36:34 +00001665 " check that win_move_separator returns false for an invalid window
1666 wincmd =
1667 let w = winwidth(0)
1668 call assert_false(win_move_separator(-1, 1))
1669 call assert_equal(w, winwidth(0))
zeertzjq873f41a2022-11-01 11:44:43 +00001670
Daniel Steinbergee630312022-01-10 13:36:34 +00001671 " check that win_move_separator returns false for a popup window
1672 let id = popup_create(['hello', 'world'], {})
1673 let w = winwidth(id)
1674 call assert_false(win_move_separator(id, 1))
1675 call assert_equal(w, winwidth(id))
1676 call popup_close(id)
zeertzjq873f41a2022-11-01 11:44:43 +00001677
1678 " check that using another tabpage fails without crash
1679 let id = win_getid()
1680 tabnew
1681 call assert_fails('call win_move_separator(id, -1)', 'E1308:')
1682 tabclose
1683
Daniel Steinbergee630312022-01-10 13:36:34 +00001684 %bwipe!
1685endfunc
1686
1687func Test_win_move_statusline()
1688 edit a
1689 leftabove split b
1690 let h = winheight(0)
1691 " check win_move_statusline from top window on top window
1692 call assert_equal(1, winnr())
1693 for offset in range(5)
1694 call assert_true(win_move_statusline(0, offset))
1695 call assert_equal(h + offset, winheight(0))
1696 call assert_true(0->win_move_statusline(-offset))
1697 call assert_equal(h, winheight(0))
1698 endfor
1699 " check win_move_statusline from bottom window on top window number
1700 wincmd j
1701 call assert_notequal(1, winnr())
1702 for offset in range(5)
1703 call assert_true(1->win_move_statusline(offset))
1704 call assert_equal(h + offset, winheight(1))
1705 call assert_true(win_move_statusline(1, -offset))
1706 call assert_equal(h, winheight(1))
1707 endfor
zeertzjqa0c4e2f2022-01-29 10:59:53 +00001708 " check win_move_statusline from bottom window on bottom window
1709 let h0 = winheight(0)
1710 for offset in range(5)
1711 call assert_true(0->win_move_statusline(-offset))
1712 call assert_equal(h0 - offset, winheight(0))
1713 call assert_equal(1 + offset, &cmdheight)
1714 call assert_true(win_move_statusline(0, offset))
1715 call assert_equal(h0, winheight(0))
1716 call assert_equal(1, &cmdheight)
1717 endfor
1718 call assert_true(win_move_statusline(0, 1))
Bram Moolenaara2a89732022-08-31 14:46:18 +01001719 call assert_equal(h0, winheight(0))
1720 call assert_equal(1, &cmdheight)
Daniel Steinbergee630312022-01-10 13:36:34 +00001721 " check win_move_statusline from bottom window on top window ID
1722 let id = win_getid(1)
1723 for offset in range(5)
1724 call assert_true(win_move_statusline(id, offset))
1725 call assert_equal(h + offset, winheight(id))
1726 call assert_true(id->win_move_statusline(-offset))
1727 call assert_equal(h, winheight(id))
1728 endfor
Bram Moolenaar86e67172022-10-31 12:24:12 +00001729
Daniel Steinbergee630312022-01-10 13:36:34 +00001730 " check that win_move_statusline doesn't error with offsets beyond moving
1731 " possibility
1732 call assert_true(win_move_statusline(id, 5000))
1733 call assert_true(winheight(id) > h)
1734 call assert_true(win_move_statusline(id, -5000))
1735 call assert_true(winheight(id) < h)
Bram Moolenaar86e67172022-10-31 12:24:12 +00001736
Daniel Steinbergee630312022-01-10 13:36:34 +00001737 " check that win_move_statusline returns false for an invalid window
1738 wincmd =
1739 let h = winheight(0)
1740 call assert_false(win_move_statusline(-1, 1))
1741 call assert_equal(h, winheight(0))
Bram Moolenaar86e67172022-10-31 12:24:12 +00001742
Daniel Steinbergee630312022-01-10 13:36:34 +00001743 " check that win_move_statusline returns false for a popup window
1744 let id = popup_create(['hello', 'world'], {})
1745 let h = winheight(id)
1746 call assert_false(win_move_statusline(id, 1))
1747 call assert_equal(h, winheight(id))
1748 call popup_close(id)
Bram Moolenaar86e67172022-10-31 12:24:12 +00001749
1750 " check that using another tabpage fails without crash
1751 let id = win_getid()
1752 tabnew
1753 call assert_fails('call win_move_statusline(id, -1)', 'E1308:')
1754 tabclose
1755
Daniel Steinbergee630312022-01-10 13:36:34 +00001756 %bwipe!
1757endfunc
1758
Yegappan Lakshmanan72bb47e2022-04-03 11:22:38 +01001759" Test for window allocation failure
1760func Test_window_alloc_failure()
1761 %bw!
1762
1763 " test for creating a new window above current window
1764 call test_alloc_fail(GetAllocId('newwin_wvars'), 0, 0)
1765 call assert_fails('above new', 'E342:')
1766 call assert_equal(1, winnr('$'))
1767
1768 " test for creating a new window below current window
1769 call test_alloc_fail(GetAllocId('newwin_wvars'), 0, 0)
1770 call assert_fails('below new', 'E342:')
1771 call assert_equal(1, winnr('$'))
1772
1773 " test for popup window creation failure
1774 call test_alloc_fail(GetAllocId('newwin_wvars'), 0, 0)
1775 call assert_fails('call popup_create("Hello", {})', 'E342:')
1776 call assert_equal([], popup_list())
1777
1778 call test_alloc_fail(GetAllocId('newwin_wvars'), 0, 0)
1779 call assert_fails('split', 'E342:')
1780 call assert_equal(1, winnr('$'))
1781
Bram Moolenaarb18b4962022-09-02 21:55:50 +01001782 edit Xwaffile1
1783 edit Xwaffile2
Yegappan Lakshmanan72bb47e2022-04-03 11:22:38 +01001784 call test_alloc_fail(GetAllocId('newwin_wvars'), 0, 0)
Bram Moolenaarb18b4962022-09-02 21:55:50 +01001785 call assert_fails('sb Xwaffile1', 'E342:')
Yegappan Lakshmanan72bb47e2022-04-03 11:22:38 +01001786 call assert_equal(1, winnr('$'))
Bram Moolenaarb18b4962022-09-02 21:55:50 +01001787 call assert_equal('Xwaffile2', @%)
Yegappan Lakshmanan72bb47e2022-04-03 11:22:38 +01001788 %bw!
1789
1790 " FIXME: The following test crashes Vim
1791 " test for new tabpage creation failure
1792 " call test_alloc_fail(GetAllocId('newwin_wvars'), 0, 0)
1793 " call assert_fails('tabnew', 'E342:')
1794 " call assert_equal(1, tabpagenr('$'))
1795 " call assert_equal(1, winnr('$'))
1796
1797 " This test messes up the internal Vim window/frame information. So the
1798 " Test_window_cmd_cmdwin_with_vsp() test fails after running this test.
1799 " Open a new tab and close everything else to fix this issue.
1800 tabnew
1801 tabonly
1802endfunc
Bram Moolenaard0fb9072021-12-09 11:57:22 +00001803
Luuk van Baalfd7e60a2022-09-07 14:42:49 +01001804func Test_win_equal_last_status()
1805 let save_lines = &lines
1806 set lines=20
1807 set splitbelow
1808 set laststatus=0
1809
1810 split | split | quit
1811 call assert_equal(winheight(1), winheight(2))
1812
1813 let &lines = save_lines
1814 set splitbelow&
1815 set laststatus&
1816endfunc
1817
Luuk van Baal13ece2a2022-10-03 15:28:08 +01001818" Test "screen" and "cursor" values for 'splitkeep' with a sequence of
Luuk van Baalfaf1d412022-09-19 16:45:29 +01001819" split operations for various options: with and without a winbar,
1820" tabline, for each possible value of 'laststatus', 'scrolloff',
Luuk van Baal13ece2a2022-10-03 15:28:08 +01001821" 'equalalways', and with the cursor at the top, middle and bottom.
1822func Test_splitkeep_options()
Luuk van Baal594f9e02022-09-16 12:52:58 +01001823 " disallow window resizing
1824 let save_WS = &t_WS
1825 set t_WS=
1826
Luuk van Baal29ab5242022-09-11 16:59:53 +01001827 let gui = has("gui_running")
Luuk van Baal594f9e02022-09-16 12:52:58 +01001828 inoremap <expr> c "<cmd>copen<bar>wincmd k<CR>"
Luuk van Baal13ece2a2022-10-03 15:28:08 +01001829 for run in range(0, 20)
1830 let &splitkeep = run > 10 ? 'topline' : 'screen'
1831 let &scrolloff = (!(run % 4) ? 0 : run)
1832 let &laststatus = (run % 3)
1833 let &splitbelow = (run % 3)
1834 let &equalalways = (run % 2)
1835 let wsb = (run % 2) && &splitbelow
1836 let tl = (gui ? 0 : ((run % 5) ? 1 : 0))
1837 let pos = !(run % 3) ? 'H' : ((run % 2) ? 'M' : 'L')
1838 tabnew | tabonly! | redraw
Luuk van Baal594f9e02022-09-16 12:52:58 +01001839 execute (run % 5) ? 'tabnew' : ''
1840 execute (run % 2) ? 'nnoremenu 1.10 WinBar.Test :echo' : ''
Luuk van Baal594f9e02022-09-16 12:52:58 +01001841 call setline(1, range(1, 256))
1842 " No scroll for restore_snapshot
1843 norm G
1844 try
1845 copen | close | colder
1846 catch /E380/
1847 endtry
1848 call assert_equal(257 - winheight(0), line("w0"))
Luuk van Baal29ab5242022-09-11 16:59:53 +01001849
Luuk van Baal594f9e02022-09-16 12:52:58 +01001850 " No scroll for firstwin horizontal split
1851 execute 'norm gg' . pos
1852 split | redraw | wincmd k
1853 call assert_equal(1, line("w0"))
1854 call assert_equal(&scroll, winheight(0) / 2)
1855 wincmd j
Luuk van Baal13ece2a2022-10-03 15:28:08 +01001856 call assert_equal(&spk == 'topline' ? 1 : win_screenpos(0)[0] - tl - wsb, line("w0"))
Luuk van Baal29ab5242022-09-11 16:59:53 +01001857
Luuk van Baal594f9e02022-09-16 12:52:58 +01001858 " No scroll when resizing windows
Luuk van Baal13ece2a2022-10-03 15:28:08 +01001859 wincmd k | resize +2 | redraw
Luuk van Baal594f9e02022-09-16 12:52:58 +01001860 call assert_equal(1, line("w0"))
1861 wincmd j
Luuk van Baal13ece2a2022-10-03 15:28:08 +01001862 call assert_equal(&spk == 'topline' ? 1 : win_screenpos(0)[0] - tl - wsb, line("w0"))
Luuk van Baal29ab5242022-09-11 16:59:53 +01001863
Luuk van Baal594f9e02022-09-16 12:52:58 +01001864 " No scroll when dragging statusline
1865 call win_move_statusline(1, -3)
Luuk van Baal13ece2a2022-10-03 15:28:08 +01001866 call assert_equal(&spk == 'topline' ? 1 : win_screenpos(0)[0] - tl - wsb, line("w0"))
Luuk van Baal594f9e02022-09-16 12:52:58 +01001867 wincmd k
1868 call assert_equal(1, line("w0"))
Luuk van Baal29ab5242022-09-11 16:59:53 +01001869
Luuk van Baal594f9e02022-09-16 12:52:58 +01001870 " No scroll when changing shellsize
1871 set lines+=2
1872 call assert_equal(1, line("w0"))
1873 wincmd j
Luuk van Baal13ece2a2022-10-03 15:28:08 +01001874 call assert_equal(&spk == 'topline' ? 1 : win_screenpos(0)[0] - tl - wsb, line("w0"))
Luuk van Baal594f9e02022-09-16 12:52:58 +01001875 set lines-=2
Luuk van Baal13ece2a2022-10-03 15:28:08 +01001876 call assert_equal(&spk == 'topline' ? 1 : win_screenpos(0)[0] - tl - wsb, line("w0"))
Luuk van Baal594f9e02022-09-16 12:52:58 +01001877 wincmd k
1878 call assert_equal(1, line("w0"))
Luuk van Baal29ab5242022-09-11 16:59:53 +01001879
Luuk van Baal594f9e02022-09-16 12:52:58 +01001880 " No scroll when equalizing windows
1881 wincmd =
1882 call assert_equal(1, line("w0"))
1883 wincmd j
Luuk van Baal13ece2a2022-10-03 15:28:08 +01001884 call assert_equal(&spk == 'topline' ? 1 : win_screenpos(0)[0] - tl - wsb, line("w0"))
Luuk van Baal594f9e02022-09-16 12:52:58 +01001885 wincmd k
1886 call assert_equal(1, line("w0"))
Luuk van Baal29ab5242022-09-11 16:59:53 +01001887
Luuk van Baal594f9e02022-09-16 12:52:58 +01001888 " No scroll in windows split multiple times
1889 vsplit | split | 4wincmd w
Luuk van Baal13ece2a2022-10-03 15:28:08 +01001890 call assert_equal(&spk == 'topline' ? 1 : win_screenpos(0)[0] - tl - wsb, line("w0"))
Luuk van Baal594f9e02022-09-16 12:52:58 +01001891 1wincmd w | quit | wincmd l | split
Luuk van Baal13ece2a2022-10-03 15:28:08 +01001892 call assert_equal(&spk == 'topline' ? 1 : win_screenpos(0)[0] - tl - wsb, line("w0"))
Luuk van Baal594f9e02022-09-16 12:52:58 +01001893 wincmd j
Luuk van Baal13ece2a2022-10-03 15:28:08 +01001894 call assert_equal(&spk == 'topline' ? 1 : win_screenpos(0)[0] - tl - wsb, line("w0"))
Luuk van Baal29ab5242022-09-11 16:59:53 +01001895
Luuk van Baal594f9e02022-09-16 12:52:58 +01001896 " No scroll in small window
1897 2wincmd w | only | 5split | wincmd k
1898 call assert_equal(1, line("w0"))
1899 wincmd j
Luuk van Baal13ece2a2022-10-03 15:28:08 +01001900 call assert_equal(&spk == 'topline' ? 1 : win_screenpos(0)[0] - tl - wsb, line("w0"))
Luuk van Baal29ab5242022-09-11 16:59:53 +01001901
Luuk van Baal594f9e02022-09-16 12:52:58 +01001902 " No scroll for vertical split
1903 quit | vsplit | wincmd l
1904 call assert_equal(1, line("w0"))
1905 wincmd h
1906 call assert_equal(1, line("w0"))
Luuk van Baal29ab5242022-09-11 16:59:53 +01001907
Luuk van Baal594f9e02022-09-16 12:52:58 +01001908 " No scroll in windows split and quit multiple times
Luuk van Baal13ece2a2022-10-03 15:28:08 +01001909 quit | redraw | split | split | quit | redraw
1910 call assert_equal(&spk == 'topline' ? 1 : win_screenpos(0)[0] - tl - wsb, line("w0"))
Luuk van Baal29ab5242022-09-11 16:59:53 +01001911
Luuk van Baal594f9e02022-09-16 12:52:58 +01001912 " No scroll for new buffer
1913 1wincmd w | only | copen | wincmd k
1914 call assert_equal(1, line("w0"))
1915 only
1916 call assert_equal(1, line("w0"))
1917 above copen | wincmd j
Luuk van Baal13ece2a2022-10-03 15:28:08 +01001918 call assert_equal(&spk == 'topline' ? 1 : win_screenpos(0)[0] - tl, line("w0"))
Luuk van Baal29ab5242022-09-11 16:59:53 +01001919
Luuk van Baal594f9e02022-09-16 12:52:58 +01001920 " No scroll when opening cmdwin, and no cursor move when closing cmdwin.
1921 only | norm ggL
1922 let curpos = getcurpos()
1923 norm q:
1924 call assert_equal(1, line("w0"))
1925 call assert_equal(curpos, getcurpos())
Luuk van Baal29ab5242022-09-11 16:59:53 +01001926
Bram Moolenaarfdbd14e2023-02-14 21:56:42 +00001927 " Scroll when cursor becomes invalid in insert mode.
Luuk van Baal594f9e02022-09-16 12:52:58 +01001928 norm Lic
Luuk van Baalbc3dc292023-02-15 16:45:27 +00001929 call assert_equal(curpos, getcurpos(), 'run ' .. run)
Luuk van Baal29ab5242022-09-11 16:59:53 +01001930
Luuk van Baal594f9e02022-09-16 12:52:58 +01001931 " No scroll when topline not equal to 1
1932 only | execute "norm gg5\<C-e>" | split | wincmd k
1933 call assert_equal(6, line("w0"))
1934 wincmd j
Luuk van Baal13ece2a2022-10-03 15:28:08 +01001935 call assert_equal(&spk == 'topline' ? 6 : 5 + win_screenpos(0)[0] - tl - wsb, line("w0"))
Luuk van Baal29ab5242022-09-11 16:59:53 +01001936 endfor
1937
1938 tabnew | tabonly! | %bwipeout!
1939 iunmap c
Luuk van Baal29ab5242022-09-11 16:59:53 +01001940 set scrolloff&
1941 set splitbelow&
1942 set laststatus&
1943 set equalalways&
Luuk van Baal13ece2a2022-10-03 15:28:08 +01001944 set splitkeep&
Luuk van Baal594f9e02022-09-16 12:52:58 +01001945 let &t_WS = save_WS
Luuk van Baal29ab5242022-09-11 16:59:53 +01001946endfunc
Luuk van Baalfd7e60a2022-09-07 14:42:49 +01001947
Bram Moolenaare0f86912023-03-01 17:55:31 +00001948func Test_splitkeep_cmdwin_cursor_position()
Luuk van Baal13ece2a2022-10-03 15:28:08 +01001949 set splitkeep=screen
mityu12167d82022-09-15 17:44:07 +01001950 call setline(1, range(&lines))
1951
1952 " No scroll when cursor is at near bottom of window and cusor position
1953 " recompution (done by line('w0') in this test) happens while in cmdwin.
1954 normal! G
1955 let firstline = line('w0')
1956 autocmd CmdwinEnter * ++once autocmd WinEnter * ++once call line('w0')
1957 execute "normal! q:\<C-w>q"
1958 redraw!
1959 call assert_equal(firstline, line('w0'))
1960
1961 " User script can change cursor position successfully while in cmdwin and it
1962 " shouldn't be changed when closing cmdwin.
1963 execute "normal! Gq:\<Cmd>call win_execute(winnr('#')->win_getid(), 'call cursor(1, 1)')\<CR>\<C-w>q"
1964 call assert_equal(1, line('.'))
1965 call assert_equal(1, col('.'))
1966
1967 execute "normal! Gq:\<Cmd>autocmd WinEnter * ++once call cursor(1, 1)\<CR>\<C-w>q"
1968 call assert_equal(1, line('.'))
1969 call assert_equal(1, col('.'))
1970
1971 %bwipeout!
Luuk van Baal13ece2a2022-10-03 15:28:08 +01001972 set splitkeep&
Bram Moolenaare0f86912023-03-01 17:55:31 +00001973endfunc
Luuk van Baald5bc7622022-09-17 16:16:35 +01001974
Bram Moolenaare0f86912023-03-01 17:55:31 +00001975func Test_splitkeep_misc()
Luuk van Baal13ece2a2022-10-03 15:28:08 +01001976 set splitkeep=screen
Luuk van Baald5bc7622022-09-17 16:16:35 +01001977
1978 call setline(1, range(1, &lines))
Luuk van Baala109f392023-06-02 14:16:35 +01001979 " Cursor is adjusted to start and end of buffer
1980 norm M
1981 wincmd s
1982 resize 1
1983 call assert_equal(1, line('.'))
1984 wincmd j
1985 norm GM
1986 resize 1
1987 call assert_equal(&lines, line('.'))
1988 only!
1989
1990 set splitbelow
Luuk van Baald5bc7622022-09-17 16:16:35 +01001991 norm Gzz
1992 let top = line('w0')
Luuk van Baalfaf1d412022-09-19 16:45:29 +01001993 " No scroll when aucmd_win is opened
Luuk van Baald5bc7622022-09-17 16:16:35 +01001994 call setbufvar(bufnr("test", 1) , '&buftype', 'nofile')
1995 call assert_equal(top, line('w0'))
Luuk van Baalfaf1d412022-09-19 16:45:29 +01001996 " No scroll when tab is changed/closed
1997 tab help | close
1998 call assert_equal(top, line('w0'))
1999 " No scroll when help is closed and buffer line count < window height
2000 norm ggdG
2001 call setline(1, range(1, &lines - 10))
Luuk van Baald5bc7622022-09-17 16:16:35 +01002002 norm G
2003 let top = line('w0')
2004 help | quit
2005 call assert_equal(top, line('w0'))
Luuk van Baal346823d2022-10-05 18:26:42 +01002006 " No error when resizing window in autocmd and buffer length changed
2007 autocmd FileType qf exe "resize" line('$')
2008 cexpr getline(1, '$')
2009 copen
2010 wincmd p
2011 norm dd
2012 cexpr getline(1, '$')
Luuk van Baald5bc7622022-09-17 16:16:35 +01002013
Luuk van Baalfaf1d412022-09-19 16:45:29 +01002014 %bwipeout!
Luuk van Baald5bc7622022-09-17 16:16:35 +01002015 set splitbelow&
Luuk van Baal13ece2a2022-10-03 15:28:08 +01002016 set splitkeep&
Luuk van Baald5bc7622022-09-17 16:16:35 +01002017endfunc
2018
Luuk van Baal16af9132023-08-20 20:44:59 +02002019func Test_splitkeep_cursor()
2020 CheckScreendump
2021 let lines =<< trim END
2022 set splitkeep=screen
2023 autocmd CursorMoved * wincmd p | wincmd p
2024 call setline(1, range(1, 200))
2025 func CursorEqualize()
2026 call cursor(100, 1)
2027 wincmd =
2028 endfunc
2029 wincmd s
2030 call CursorEqualize()
2031 END
2032 call writefile(lines, 'XTestSplitkeepCallback', 'D')
2033 let buf = RunVimInTerminal('-S XTestSplitkeepCallback', #{rows: 8})
2034
2035 call VerifyScreenDump(buf, 'Test_splitkeep_cursor_1', {})
2036
2037 call term_sendkeys(buf, "j")
2038 call VerifyScreenDump(buf, 'Test_splitkeep_cursor_2', {})
2039
2040 call term_sendkeys(buf, ":set scrolloff=0\<CR>G")
2041 call VerifyScreenDump(buf, 'Test_splitkeep_cursor_3', {})
2042
2043 call StopVimInTerminal(buf)
2044endfunc
2045
Bram Moolenaare0f86912023-03-01 17:55:31 +00002046func Test_splitkeep_callback()
Luuk van Baal20e58562022-09-23 12:57:09 +01002047 CheckScreendump
2048 let lines =<< trim END
Luuk van Baal13ece2a2022-10-03 15:28:08 +01002049 set splitkeep=screen
Luuk van Baal20e58562022-09-23 12:57:09 +01002050 call setline(1, range(&lines))
Luuk van Baal13ece2a2022-10-03 15:28:08 +01002051 function C1(a, b)
Luuk van Baal20e58562022-09-23 12:57:09 +01002052 split | wincmd p
2053 endfunction
Luuk van Baal13ece2a2022-10-03 15:28:08 +01002054 function C2(a, b)
Luuk van Baal20e58562022-09-23 12:57:09 +01002055 close | split
2056 endfunction
Luuk van Baal13ece2a2022-10-03 15:28:08 +01002057 nn j <cmd>call job_start([&sh, &shcf, "true"], { 'exit_cb': 'C1' })<CR>
2058 nn t <cmd>call popup_create(term_start([&sh, &shcf, "true"],
2059 \ { 'hidden': 1, 'exit_cb': 'C2' }), {})<CR>
Luuk van Baal20e58562022-09-23 12:57:09 +01002060 END
Luuk van Baal13ece2a2022-10-03 15:28:08 +01002061 call writefile(lines, 'XTestSplitkeepCallback', 'D')
2062 let buf = RunVimInTerminal('-S XTestSplitkeepCallback', #{rows: 8})
Luuk van Baal20e58562022-09-23 12:57:09 +01002063
2064 call term_sendkeys(buf, "j")
Luuk van Baal13ece2a2022-10-03 15:28:08 +01002065 call VerifyScreenDump(buf, 'Test_splitkeep_callback_1', {})
Luuk van Baal20e58562022-09-23 12:57:09 +01002066
Luuk van Baal13ece2a2022-10-03 15:28:08 +01002067 call term_sendkeys(buf, ":quit\<CR>Ht")
2068 call VerifyScreenDump(buf, 'Test_splitkeep_callback_2', {})
Luuk van Baal20e58562022-09-23 12:57:09 +01002069
2070 call term_sendkeys(buf, ":set sb\<CR>:quit\<CR>Gj")
Luuk van Baal13ece2a2022-10-03 15:28:08 +01002071 call VerifyScreenDump(buf, 'Test_splitkeep_callback_3', {})
Luuk van Baal20e58562022-09-23 12:57:09 +01002072
Luuk van Baal13ece2a2022-10-03 15:28:08 +01002073 call term_sendkeys(buf, ":quit\<CR>Gt")
2074 call VerifyScreenDump(buf, 'Test_splitkeep_callback_4', {})
zeertzjq378e6c02023-01-14 11:46:49 +00002075
2076 call StopVimInTerminal(buf)
Luuk van Baal20e58562022-09-23 12:57:09 +01002077endfunc
2078
Bram Moolenaare0f86912023-03-01 17:55:31 +00002079func Test_splitkeep_fold()
Luuk van Baal13ece2a2022-10-03 15:28:08 +01002080 CheckScreendump
Luuk van Baal7c1cbb62022-09-27 12:31:15 +01002081
Luuk van Baal13ece2a2022-10-03 15:28:08 +01002082 let lines =<< trim END
2083 set splitkeep=screen
2084 set foldmethod=marker
2085 set number
2086 let line = 1
2087 for n in range(1, &lines)
2088 call setline(line, ['int FuncName() {/*{{{*/', 1, 2, 3, 4, 5, '}/*}}}*/',
2089 \ 'after fold'])
2090 let line += 8
2091 endfor
2092 END
2093 call writefile(lines, 'XTestSplitkeepFold', 'D')
2094 let buf = RunVimInTerminal('-S XTestSplitkeepFold', #{rows: 10})
Luuk van Baal7c1cbb62022-09-27 12:31:15 +01002095
2096 call term_sendkeys(buf, "L:wincmd s\<CR>")
Luuk van Baal13ece2a2022-10-03 15:28:08 +01002097 call VerifyScreenDump(buf, 'Test_splitkeep_fold_1', {})
Luuk van Baal7c1cbb62022-09-27 12:31:15 +01002098
2099 call term_sendkeys(buf, ":quit\<CR>")
Luuk van Baal13ece2a2022-10-03 15:28:08 +01002100 call VerifyScreenDump(buf, 'Test_splitkeep_fold_2', {})
Luuk van Baal7c1cbb62022-09-27 12:31:15 +01002101
2102 call term_sendkeys(buf, "H:below split\<CR>")
Luuk van Baal13ece2a2022-10-03 15:28:08 +01002103 call VerifyScreenDump(buf, 'Test_splitkeep_fold_3', {})
Luuk van Baal7c1cbb62022-09-27 12:31:15 +01002104
2105 call term_sendkeys(buf, ":wincmd k\<CR>:quit\<CR>")
Luuk van Baal13ece2a2022-10-03 15:28:08 +01002106 call VerifyScreenDump(buf, 'Test_splitkeep_fold_4', {})
zeertzjq378e6c02023-01-14 11:46:49 +00002107
2108 call StopVimInTerminal(buf)
Bram Moolenaare0f86912023-03-01 17:55:31 +00002109endfunc
Luuk van Baal7c1cbb62022-09-27 12:31:15 +01002110
Bram Moolenaare0f86912023-03-01 17:55:31 +00002111func Test_splitkeep_status()
Luuk van Baal74a694d2022-11-28 16:49:36 +00002112 CheckScreendump
2113
2114 let lines =<< trim END
2115 call setline(1, ['a', 'b', 'c'])
2116 set nomodified
2117 set splitkeep=screen
2118 let win = winnr()
2119 wincmd s
2120 wincmd j
2121 END
2122 call writefile(lines, 'XTestSplitkeepStatus', 'D')
2123 let buf = RunVimInTerminal('-S XTestSplitkeepStatus', #{rows: 10})
2124
2125 call term_sendkeys(buf, ":call win_move_statusline(win, 1)\<CR>")
2126 call VerifyScreenDump(buf, 'Test_splitkeep_status_1', {})
zeertzjq378e6c02023-01-14 11:46:49 +00002127
2128 call StopVimInTerminal(buf)
Bram Moolenaare0f86912023-03-01 17:55:31 +00002129endfunc
Luuk van Baal74a694d2022-11-28 16:49:36 +00002130
Luuk van Baalb926bf42023-05-06 12:53:50 +01002131" skipcol is not reset unnecessarily and is copied to new window
2132func Test_splitkeep_skipcol()
2133 CheckScreendump
2134
2135 let lines =<< trim END
2136 set splitkeep=topline smoothscroll splitbelow scrolloff=0
2137 call setline(1, 'with lots of text in one line '->repeat(6))
2138 norm 2
2139 wincmd s
2140 END
2141
2142 call writefile(lines, 'XTestSplitkeepSkipcol', 'D')
2143 let buf = RunVimInTerminal('-S XTestSplitkeepSkipcol', #{rows: 12, cols: 40})
2144
2145 call VerifyScreenDump(buf, 'Test_splitkeep_skipcol_1', {})
2146endfunc
2147
Bram Moolenaare0f86912023-03-01 17:55:31 +00002148func Test_new_help_window_on_error()
Rob Pillingcb94c912022-12-13 12:26:09 +00002149 help change.txt
2150 execute "normal! /CTRL-@\<CR>"
2151 silent! execute "normal! \<C-W>]"
2152
2153 let wincount = winnr('$')
2154 help 'mod'
2155
2156 call assert_equal(wincount, winnr('$'))
2157 call assert_equal(expand("<cword>"), "'mod'")
Bram Moolenaare0f86912023-03-01 17:55:31 +00002158endfunc
2159
Sean Dewar0fd44a52024-02-20 20:28:15 +01002160func Test_splitmove_flatten_frame()
2161 split
2162 vsplit
2163
2164 wincmd L
2165 let layout = winlayout()
2166 wincmd K
2167 wincmd L
2168 call assert_equal(winlayout(), layout)
2169
2170 only!
2171endfunc
2172
Sean Dewar02fcae02024-02-21 19:40:44 +01002173func Test_autocmd_window_force_room()
Sean Dewar0fd44a52024-02-20 20:28:15 +01002174 " Open as many windows as possible
2175 while v:true
2176 try
2177 split
2178 catch /E36:/
2179 break
2180 endtry
2181 endwhile
2182 while v:true
2183 try
2184 vsplit
2185 catch /E36:/
2186 break
2187 endtry
2188 endwhile
2189
2190 wincmd j
2191 vsplit
2192 call assert_fails('wincmd H', 'E36:')
2193 call assert_fails('wincmd J', 'E36:')
2194 call assert_fails('wincmd K', 'E36:')
2195 call assert_fails('wincmd L', 'E36:')
2196
2197 edit unload me
2198 enew
2199 bunload! unload\ me
Sean Dewar02fcae02024-02-21 19:40:44 +01002200 augroup AucmdWinForceRoom
Sean Dewar0fd44a52024-02-20 20:28:15 +01002201 au!
2202 au BufEnter * ++once let s:triggered = v:true
2203 \| call assert_equal('autocmd', win_gettype())
2204 augroup END
2205 let layout = winlayout()
2206 let restcmd = winrestcmd()
2207 " bufload opening the autocommand window shouldn't give E36.
2208 call bufload('unload me')
2209 call assert_equal(v:true, s:triggered)
2210 call assert_equal(winlayout(), layout)
2211 call assert_equal(winrestcmd(), restcmd)
2212
2213 unlet! s:triggered
Sean Dewar02fcae02024-02-21 19:40:44 +01002214 au! AucmdWinForceRoom
2215 augroup! AucmdWinForceRoom
Sean Dewar0fd44a52024-02-20 20:28:15 +01002216 %bw!
2217endfunc
Rob Pillingcb94c912022-12-13 12:26:09 +00002218
Sean Dewarf8658952024-02-20 22:05:10 +01002219func Test_win_gotoid_splitmove_textlock_cmdwin()
2220 call setline(1, 'foo')
2221 new
2222 let curwin = win_getid()
2223 call setline(1, 'bar')
2224
2225 set debug+=throw indentexpr=win_gotoid(win_getid(winnr('#')))
2226 call assert_fails('normal! ==', 'E565:')
2227 call assert_equal(curwin, win_getid())
Sean Dewar2a65e732024-02-22 19:53:33 +01002228 " No error if attempting to switch to curwin; nothing happens.
2229 set indentexpr=assert_equal(1,win_gotoid(win_getid()))
2230 normal! ==
2231 call assert_equal(curwin, win_getid())
Sean Dewarf8658952024-02-20 22:05:10 +01002232
2233 set indentexpr=win_splitmove(winnr('#'),winnr())
2234 call assert_fails('normal! ==', 'E565:')
2235 call assert_equal(curwin, win_getid())
2236
2237 %bw!
2238 set debug-=throw indentexpr&
2239
2240 call feedkeys('q:'
2241 \ .. ":call assert_fails('call win_splitmove(winnr(''#''), winnr())', 'E11:')\<CR>"
2242 \ .. ":call assert_equal('command', win_gettype())\<CR>"
2243 \ .. ":call assert_equal('', win_gettype(winnr('#')))\<CR>", 'ntx')
2244
2245 call feedkeys('q:'
2246 \ .. ":call assert_fails('call win_gotoid(win_getid(winnr(''#'')))', 'E11:')\<CR>"
Sean Dewar2a65e732024-02-22 19:53:33 +01002247 "\ No error if attempting to switch to curwin; nothing happens.
2248 \ .. ":call assert_equal(1, win_gotoid(win_getid()))\<CR>"
Sean Dewarf8658952024-02-20 22:05:10 +01002249 \ .. ":call assert_equal('command', win_gettype())\<CR>"
2250 \ .. ":call assert_equal('', win_gettype(winnr('#')))\<CR>", 'ntx')
2251endfunc
2252
Bram Moolenaar9e4d8212016-08-18 23:04:48 +02002253" vim: shiftwidth=2 sts=2 expandtab