blob: fc4786d4acdc7e9d3dbd649c7c08f91b055b117a [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
134func Test_window_horizontal_split()
135 call assert_equal(1, winnr('$'))
136 3wincmd s
137 call assert_equal(2, winnr('$'))
138 call assert_equal(3, winheight(0))
Bram Moolenaarf92e58c2019-09-08 21:51:41 +0200139 call assert_equal(winwidth(1), 2->winwidth())
Bram Moolenaar4520d442017-03-19 16:09:46 +0100140
141 call assert_fails('botright topleft wincmd s', 'E442:')
142 bw
143endfunc
144
145func Test_window_vertical_split()
146 call assert_equal(1, winnr('$'))
147 3wincmd v
148 call assert_equal(2, winnr('$'))
149 call assert_equal(3, winwidth(0))
150 call assert_equal(winheight(1), winheight(2))
151
152 call assert_fails('botright topleft wincmd v', 'E442:')
153 bw
154endfunc
155
Bram Moolenaar1bbb6192018-11-10 16:02:01 +0100156" Test the ":wincmd ^" and "<C-W>^" commands.
Bram Moolenaar4520d442017-03-19 16:09:46 +0100157func Test_window_split_edit_alternate()
Bram Moolenaar1bbb6192018-11-10 16:02:01 +0100158 " Test for failure when the alternate buffer/file no longer exists.
159 edit Xfoo | %bw
Bram Moolenaare2e40752020-09-04 21:18:46 +0200160 call assert_fails(':wincmd ^', 'E23:')
Bram Moolenaar1bbb6192018-11-10 16:02:01 +0100161
162 " Test for the expected behavior when we have two named buffers.
163 edit Xfoo | edit Xbar
Bram Moolenaar4520d442017-03-19 16:09:46 +0100164 wincmd ^
Bram Moolenaar1bbb6192018-11-10 16:02:01 +0100165 call assert_equal('Xfoo', bufname(winbufnr(1)))
166 call assert_equal('Xbar', bufname(winbufnr(2)))
167 only
Bram Moolenaar4520d442017-03-19 16:09:46 +0100168
Bram Moolenaar1bbb6192018-11-10 16:02:01 +0100169 " Test for the expected behavior when the alternate buffer is not named.
170 enew | let l:nr1 = bufnr('%')
171 edit Xfoo | let l:nr2 = bufnr('%')
172 wincmd ^
173 call assert_equal(l:nr1, winbufnr(1))
174 call assert_equal(l:nr2, winbufnr(2))
175 only
176
Bram Moolenaard42333d2018-11-10 20:28:19 +0100177 " FIXME: this currently fails on AppVeyor, but passes locally
178 if !has('win32')
179 " Test the Normal mode command.
180 call feedkeys("\<C-W>\<C-^>", 'tx')
181 call assert_equal(l:nr2, winbufnr(1))
182 call assert_equal(l:nr1, winbufnr(2))
183 endif
Bram Moolenaar1bbb6192018-11-10 16:02:01 +0100184
185 %bw!
186endfunc
187
188" Test the ":[count]wincmd ^" and "[count]<C-W>^" commands.
189func Test_window_split_edit_bufnr()
Bram Moolenaar1bbb6192018-11-10 16:02:01 +0100190 %bwipeout
191 let l:nr = bufnr('%') + 1
Bram Moolenaare2e40752020-09-04 21:18:46 +0200192 call assert_fails(':execute "normal! ' . l:nr . '\<C-W>\<C-^>"', 'E92:')
193 call assert_fails(':' . l:nr . 'wincmd ^', 'E16:')
194 call assert_fails(':0wincmd ^', 'E16:')
Bram Moolenaar1bbb6192018-11-10 16:02:01 +0100195
196 edit Xfoo | edit Xbar | edit Xbaz
197 let l:foo_nr = bufnr('Xfoo')
198 let l:bar_nr = bufnr('Xbar')
199 let l:baz_nr = bufnr('Xbaz')
200
Bram Moolenaar8617b402018-11-10 20:47:48 +0100201 " FIXME: this currently fails on AppVeyor, but passes locally
202 if !has('win32')
203 call feedkeys(l:foo_nr . "\<C-W>\<C-^>", 'tx')
204 call assert_equal('Xfoo', bufname(winbufnr(1)))
205 call assert_equal('Xbaz', bufname(winbufnr(2)))
206 only
Bram Moolenaar1bbb6192018-11-10 16:02:01 +0100207
Bram Moolenaar8617b402018-11-10 20:47:48 +0100208 call feedkeys(l:bar_nr . "\<C-W>\<C-^>", 'tx')
209 call assert_equal('Xbar', bufname(winbufnr(1)))
210 call assert_equal('Xfoo', bufname(winbufnr(2)))
211 only
Bram Moolenaar1bbb6192018-11-10 16:02:01 +0100212
Bram Moolenaar8617b402018-11-10 20:47:48 +0100213 execute l:baz_nr . 'wincmd ^'
214 call assert_equal('Xbaz', bufname(winbufnr(1)))
215 call assert_equal('Xbar', bufname(winbufnr(2)))
216 endif
Bram Moolenaar1bbb6192018-11-10 16:02:01 +0100217
218 %bw!
Bram Moolenaar4520d442017-03-19 16:09:46 +0100219endfunc
220
Bram Moolenaar9b9be002020-03-22 14:41:22 +0100221func Test_window_split_no_room()
222 " N horizontal windows need >= 2*N + 1 lines:
223 " - 1 line + 1 status line in each window
224 " - 1 Ex command line
225 "
226 " 2*N + 1 <= &lines
227 " N <= (lines - 1)/2
228 "
229 " Beyond that number of windows, E36: Not enough room is expected.
230 let hor_win_count = (&lines - 1)/2
231 let hor_split_count = hor_win_count - 1
232 for s in range(1, hor_split_count) | split | endfor
233 call assert_fails('split', 'E36:')
234
Sean Dewar0fd44a52024-02-20 20:28:15 +0100235 botright vsplit
236 wincmd |
237 let layout = winlayout()
238 let restcmd = winrestcmd()
239 call assert_fails('wincmd J', 'E36:')
240 call assert_fails('wincmd K', 'E36:')
241 call assert_equal(layout, winlayout())
242 call assert_equal(restcmd, winrestcmd())
243 only
244
Bram Moolenaar9b9be002020-03-22 14:41:22 +0100245 " N vertical windows need >= 2*(N - 1) + 1 columns:
246 " - 1 column + 1 separator for each window (except last window)
247 " - 1 column for the last window which does not have separator
248 "
249 " 2*(N - 1) + 1 <= &columns
250 " 2*N - 1 <= &columns
251 " N <= (&columns + 1)/2
252 let ver_win_count = (&columns + 1)/2
253 let ver_split_count = ver_win_count - 1
254 for s in range(1, ver_split_count) | vsplit | endfor
255 call assert_fails('vsplit', 'E36:')
256
Sean Dewar0fd44a52024-02-20 20:28:15 +0100257 split
258 wincmd |
259 let layout = winlayout()
260 let restcmd = winrestcmd()
261 call assert_fails('wincmd H', 'E36:')
262 call assert_fails('wincmd L', 'E36:')
263 call assert_equal(layout, winlayout())
264 call assert_equal(restcmd, winrestcmd())
265
266 " Check that the last statusline isn't lost.
Sean Dewar02fcae02024-02-21 19:40:44 +0100267 " Set its window's width to 2 for the test.
Sean Dewar0fd44a52024-02-20 20:28:15 +0100268 wincmd j
Sean Dewar02fcae02024-02-21 19:40:44 +0100269 set laststatus=0 winminwidth=0
270 vertical resize 2
271 set winminwidth&
Sean Dewar0fd44a52024-02-20 20:28:15 +0100272 call setwinvar(winnr('k'), '&statusline', '@#')
273 let last_stl_row = win_screenpos(0)[0] - 1
274 redraw
275 call assert_equal('@#|', GetScreenStr(last_stl_row))
276 call assert_equal('~ |', GetScreenStr(&lines - &cmdheight))
Sean Dewar02fcae02024-02-21 19:40:44 +0100277
278 let restcmd = winrestcmd()
Sean Dewar0fd44a52024-02-20 20:28:15 +0100279 call assert_fails('wincmd H', 'E36:')
280 call assert_fails('wincmd L', 'E36:')
281 call assert_equal(layout, winlayout())
282 call assert_equal(restcmd, winrestcmd())
283 call setwinvar(winnr('k'), '&statusline', '=-')
284 redraw
285 call assert_equal('=-|', GetScreenStr(last_stl_row))
286 call assert_equal('~ |', GetScreenStr(&lines - &cmdheight))
287
Bram Moolenaar9b9be002020-03-22 14:41:22 +0100288 %bw!
Sean Dewar0fd44a52024-02-20 20:28:15 +0100289 set laststatus&
Bram Moolenaar9b9be002020-03-22 14:41:22 +0100290endfunc
291
Bram Moolenaar4520d442017-03-19 16:09:46 +0100292func Test_window_exchange()
293 e Xa
294
295 " Nothing happens with window exchange when there is 1 window
296 wincmd x
297 call assert_equal(1, winnr('$'))
298
299 split Xb
300 split Xc
301
302 call assert_equal('Xc', bufname(winbufnr(1)))
303 call assert_equal('Xb', bufname(winbufnr(2)))
304 call assert_equal('Xa', bufname(winbufnr(3)))
305
306 " Exchange current window 1 with window 3
307 3wincmd x
308 call assert_equal('Xa', bufname(winbufnr(1)))
309 call assert_equal('Xb', bufname(winbufnr(2)))
310 call assert_equal('Xc', bufname(winbufnr(3)))
311
312 " Exchange window with next when at the top window
313 wincmd x
314 call assert_equal('Xb', bufname(winbufnr(1)))
315 call assert_equal('Xa', bufname(winbufnr(2)))
316 call assert_equal('Xc', bufname(winbufnr(3)))
317
318 " Exchange window with next when at the middle window
319 wincmd j
320 wincmd x
321 call assert_equal('Xb', bufname(winbufnr(1)))
322 call assert_equal('Xc', bufname(winbufnr(2)))
323 call assert_equal('Xa', bufname(winbufnr(3)))
324
325 " Exchange window with next when at the bottom window.
326 " When there is no next window, it exchanges with the previous window.
327 wincmd j
328 wincmd x
329 call assert_equal('Xb', bufname(winbufnr(1)))
330 call assert_equal('Xa', bufname(winbufnr(2)))
331 call assert_equal('Xc', bufname(winbufnr(3)))
332
333 bw Xa Xb Xc
334endfunc
335
336func Test_window_rotate()
337 e Xa
338 split Xb
339 split Xc
340 call assert_equal('Xc', bufname(winbufnr(1)))
341 call assert_equal('Xb', bufname(winbufnr(2)))
342 call assert_equal('Xa', bufname(winbufnr(3)))
343
344 " Rotate downwards
345 wincmd r
346 call assert_equal('Xa', bufname(winbufnr(1)))
347 call assert_equal('Xc', bufname(winbufnr(2)))
348 call assert_equal('Xb', bufname(winbufnr(3)))
349
350 2wincmd r
351 call assert_equal('Xc', bufname(winbufnr(1)))
352 call assert_equal('Xb', bufname(winbufnr(2)))
353 call assert_equal('Xa', bufname(winbufnr(3)))
354
355 " Rotate upwards
356 wincmd R
357 call assert_equal('Xb', bufname(winbufnr(1)))
358 call assert_equal('Xa', bufname(winbufnr(2)))
359 call assert_equal('Xc', bufname(winbufnr(3)))
360
361 2wincmd R
362 call assert_equal('Xc', bufname(winbufnr(1)))
363 call assert_equal('Xb', bufname(winbufnr(2)))
364 call assert_equal('Xa', bufname(winbufnr(3)))
365
366 bot vsplit
367 call assert_fails('wincmd R', 'E443:')
368
369 bw Xa Xb Xc
370endfunc
371
372func Test_window_height()
373 e Xa
374 split Xb
375
376 let [wh1, wh2] = [winheight(1), winheight(2)]
377 " Active window (1) should have the same height or 1 more
378 " than the other window.
379 call assert_inrange(wh2, wh2 + 1, wh1)
380
381 wincmd -
382 call assert_equal(wh1 - 1, winheight(1))
383 call assert_equal(wh2 + 1, winheight(2))
384
385 wincmd +
386 call assert_equal(wh1, winheight(1))
Bram Moolenaarf92e58c2019-09-08 21:51:41 +0200387 call assert_equal(wh2, 2->winheight())
Bram Moolenaar4520d442017-03-19 16:09:46 +0100388
389 2wincmd _
390 call assert_equal(2, winheight(1))
391 call assert_equal(wh1 + wh2 - 2, winheight(2))
392
393 wincmd =
394 call assert_equal(wh1, winheight(1))
395 call assert_equal(wh2, winheight(2))
396
397 2wincmd _
398 set winfixheight
399 split Xc
400 let [wh1, wh2, wh3] = [winheight(1), winheight(2), winheight(3)]
401 call assert_equal(2, winheight(2))
402 call assert_inrange(wh3, wh3 + 1, wh1)
403 3wincmd +
404 call assert_equal(2, winheight(2))
405 call assert_equal(wh1 + 3, winheight(1))
406 call assert_equal(wh3 - 3, winheight(3))
407 wincmd =
408 call assert_equal(2, winheight(2))
409 call assert_equal(wh1, winheight(1))
410 call assert_equal(wh3, winheight(3))
411
412 wincmd j
413 set winfixheight&
414
415 wincmd =
416 let [wh1, wh2, wh3] = [winheight(1), winheight(2), winheight(3)]
417 " Current window (2) should have the same height or 1 more
418 " than the other windows.
419 call assert_inrange(wh1, wh1 + 1, wh2)
420 call assert_inrange(wh3, wh3 + 1, wh2)
421
422 bw Xa Xb Xc
423endfunc
424
Bram Moolenaar21c3a802022-08-31 17:49:14 +0100425func Test_wincmd_equal()
426 edit Xone
427 below split Xtwo
428 rightbelow vsplit Xthree
429 call assert_equal('Xone', bufname(winbufnr(1)))
430 call assert_equal('Xtwo', bufname(winbufnr(2)))
431 call assert_equal('Xthree', bufname(winbufnr(3)))
432
433 " Xone and Xtwo should be about the same height
434 let [wh1, wh2] = [winheight(1), winheight(2)]
435 call assert_inrange(wh1 - 1, wh1 + 1, wh2)
436 " Xtwo and Xthree should be about the same width
437 let [ww2, ww3] = [winwidth(2), winwidth(3)]
438 call assert_inrange(ww2 - 1, ww2 + 1, ww3)
439
440 1wincmd w
441 10wincmd _
442 2wincmd w
443 20wincmd |
444 call assert_equal(10, winheight(1))
445 call assert_equal(20, winwidth(2))
446
447 " equalizing horizontally doesn't change the heights
448 hor wincmd =
449 call assert_equal(10, winheight(1))
450 let [ww2, ww3] = [winwidth(2), winwidth(3)]
451 call assert_inrange(ww2 - 1, ww2 + 1, ww3)
452
453 2wincmd w
454 20wincmd |
455 call assert_equal(20, winwidth(2))
456 " equalizing vertically doesn't change the widths
457 vert wincmd =
458 call assert_equal(20, winwidth(2))
459 let [wh1, wh2] = [winheight(1), winheight(2)]
460 call assert_inrange(wh1 - 1, wh1 + 1, wh2)
461
462 bwipe Xone Xtwo Xthree
463endfunc
464
Bram Moolenaar4520d442017-03-19 16:09:46 +0100465func Test_window_width()
466 e Xa
467 vsplit Xb
468
469 let [ww1, ww2] = [winwidth(1), winwidth(2)]
470 " Active window (1) should have the same width or 1 more
471 " than the other window.
472 call assert_inrange(ww2, ww2 + 1, ww1)
473
474 wincmd <
475 call assert_equal(ww1 - 1, winwidth(1))
476 call assert_equal(ww2 + 1, winwidth(2))
477
478 wincmd >
479 call assert_equal(ww1, winwidth(1))
480 call assert_equal(ww2, winwidth(2))
481
482 2wincmd |
483 call assert_equal(2, winwidth(1))
484 call assert_equal(ww1 + ww2 - 2, winwidth(2))
485
486 wincmd =
487 call assert_equal(ww1, winwidth(1))
488 call assert_equal(ww2, winwidth(2))
489
490 2wincmd |
491 set winfixwidth
492 vsplit Xc
493 let [ww1, ww2, ww3] = [winwidth(1), winwidth(2), winwidth(3)]
Bram Moolenaar38e34832017-03-19 20:22:36 +0100494 call assert_equal(2, winwidth(2))
Bram Moolenaar4520d442017-03-19 16:09:46 +0100495 call assert_inrange(ww3, ww3 + 1, ww1)
496 3wincmd >
Bram Moolenaar38e34832017-03-19 20:22:36 +0100497 call assert_equal(2, winwidth(2))
Bram Moolenaar4520d442017-03-19 16:09:46 +0100498 call assert_equal(ww1 + 3, winwidth(1))
499 call assert_equal(ww3 - 3, winwidth(3))
500 wincmd =
Bram Moolenaar38e34832017-03-19 20:22:36 +0100501 call assert_equal(2, winwidth(2))
Bram Moolenaar4520d442017-03-19 16:09:46 +0100502 call assert_equal(ww1, winwidth(1))
503 call assert_equal(ww3, winwidth(3))
504
505 wincmd l
506 set winfixwidth&
507
508 wincmd =
509 let [ww1, ww2, ww3] = [winwidth(1), winwidth(2), winwidth(3)]
510 " Current window (2) should have the same width or 1 more
511 " than the other windows.
512 call assert_inrange(ww1, ww1 + 1, ww2)
513 call assert_inrange(ww3, ww3 + 1, ww2)
514
Yegappan Lakshmanan59585492021-06-12 13:46:41 +0200515 " when the current window width is less than the new 'winwidth', the current
516 " window width should be increased.
517 enew | only
518 split
519 10vnew
520 set winwidth=15
521 call assert_equal(15, winwidth(0))
522
523 %bw!
Bram Moolenaar4520d442017-03-19 16:09:46 +0100524endfunc
525
Bram Moolenaar8eeeba82017-06-25 22:45:39 +0200526func Test_equalalways_on_close()
527 set equalalways
528 vsplit
529 windo split
Bram Moolenaar1bbb6192018-11-10 16:02:01 +0100530 split
Bram Moolenaar8eeeba82017-06-25 22:45:39 +0200531 wincmd J
532 " now we have a frame top-left with two windows, a frame top-right with two
533 " windows and a frame at the bottom, full-width.
534 let height_1 = winheight(1)
535 let height_2 = winheight(2)
536 let height_3 = winheight(3)
537 let height_4 = winheight(4)
538 " closing the bottom window causes all windows to be resized.
539 close
540 call assert_notequal(height_1, winheight(1))
541 call assert_notequal(height_2, winheight(2))
542 call assert_notequal(height_3, winheight(3))
543 call assert_notequal(height_4, winheight(4))
544 call assert_equal(winheight(1), winheight(3))
545 call assert_equal(winheight(2), winheight(4))
546
547 1wincmd w
548 split
549 4wincmd w
550 resize + 5
551 " left column has three windows, equalized heights.
552 " right column has two windows, top one a bit higher
553 let height_1 = winheight(1)
554 let height_2 = winheight(2)
555 let height_4 = winheight(4)
556 let height_5 = winheight(5)
557 3wincmd w
558 " closing window in left column equalizes heights in left column but not in
559 " the right column
560 close
561 call assert_notequal(height_1, winheight(1))
562 call assert_notequal(height_2, winheight(2))
563 call assert_equal(height_4, winheight(3))
564 call assert_equal(height_5, winheight(4))
565
566 only
567 set equalalways&
568endfunc
569
Bram Moolenaar22044dc2017-12-02 15:43:37 +0100570func Test_win_screenpos()
Bram Moolenaar5a4c3082019-12-01 15:23:11 +0100571 CheckFeature quickfix
572
Bram Moolenaar22044dc2017-12-02 15:43:37 +0100573 call assert_equal(1, winnr('$'))
574 split
575 vsplit
576 10wincmd _
577 30wincmd |
578 call assert_equal([1, 1], win_screenpos(1))
579 call assert_equal([1, 32], win_screenpos(2))
580 call assert_equal([12, 1], win_screenpos(3))
581 call assert_equal([0, 0], win_screenpos(4))
Bram Moolenaar99fa7212020-04-26 15:59:55 +0200582 call assert_fails('let l = win_screenpos([])', 'E745:')
Bram Moolenaar22044dc2017-12-02 15:43:37 +0100583 only
584endfunc
585
Bram Moolenaar4520d442017-03-19 16:09:46 +0100586func Test_window_jump_tag()
Bram Moolenaar5a4c3082019-12-01 15:23:11 +0100587 CheckFeature quickfix
588
Bram Moolenaar4520d442017-03-19 16:09:46 +0100589 help
590 /iccf
591 call assert_match('^|iccf|', getline('.'))
592 call assert_equal(2, winnr('$'))
593 2wincmd }
594 call assert_equal(3, winnr('$'))
595 call assert_match('^|iccf|', getline('.'))
596 wincmd k
597 call assert_match('\*iccf\*', getline('.'))
598 call assert_equal(2, winheight(0))
599
600 wincmd z
601 set previewheight=4
602 help
603 /bugs
604 wincmd }
605 wincmd k
606 call assert_match('\*bugs\*', getline('.'))
607 call assert_equal(4, winheight(0))
608 set previewheight&
609
610 %bw!
611endfunc
612
613func Test_window_newtab()
614 e Xa
615
616 call assert_equal(1, tabpagenr('$'))
617 call assert_equal("\nAlready only one window", execute('wincmd T'))
618
619 split Xb
620 split Xc
621
622 wincmd T
623 call assert_equal(2, tabpagenr('$'))
624 call assert_equal(['Xb', 'Xa'], map(tabpagebuflist(1), 'bufname(v:val)'))
Bram Moolenaarce90e362019-09-08 18:58:44 +0200625 call assert_equal(['Xc' ], map(2->tabpagebuflist(), 'bufname(v:val)'))
Bram Moolenaar0e05de42020-03-25 22:23:46 +0100626 call assert_equal(['Xc' ], map(tabpagebuflist(), 'bufname(v:val)'))
Bram Moolenaar4520d442017-03-19 16:09:46 +0100627
628 %bw!
629endfunc
630
Bram Moolenaarf79225e2017-03-18 23:11:04 +0100631func Test_next_split_all()
632 " This was causing an illegal memory access.
633 n x
634 norm axxx
635 split
636 split
637 s/x
638 s/x
639 all
640 bwipe!
641endfunc
642
Bram Moolenaar75373f32017-08-07 22:02:30 +0200643" Tests for adjusting window and contents
644func GetScreenStr(row)
645 let str = ""
646 for c in range(1,3)
647 let str .= nr2char(screenchar(a:row, c))
648 endfor
649 return str
650endfunc
651
652func Test_window_contents()
653 enew! | only | new
654 call setline(1, range(1,256))
655
656 exe "norm! \<C-W>t\<C-W>=1Gzt\<C-W>w\<C-W>+"
657 redraw
Bram Moolenaar72cf47a2018-05-10 18:23:29 +0200658 let s3 = GetScreenStr(1)
Bram Moolenaar75373f32017-08-07 22:02:30 +0200659 wincmd p
660 call assert_equal(1, line("w0"))
661 call assert_equal('1 ', s3)
662
663 exe "norm! \<C-W>t\<C-W>=50Gzt\<C-W>w\<C-W>+"
664 redraw
Bram Moolenaar72cf47a2018-05-10 18:23:29 +0200665 let s3 = GetScreenStr(1)
Bram Moolenaar75373f32017-08-07 22:02:30 +0200666 wincmd p
667 call assert_equal(50, line("w0"))
668 call assert_equal('50 ', s3)
669
670 exe "norm! \<C-W>t\<C-W>=59Gzt\<C-W>w\<C-W>+"
671 redraw
Bram Moolenaar72cf47a2018-05-10 18:23:29 +0200672 let s3 = GetScreenStr(1)
Bram Moolenaar75373f32017-08-07 22:02:30 +0200673 wincmd p
674 call assert_equal(59, line("w0"))
675 call assert_equal('59 ', s3)
676
Bram Moolenaar8b633132020-03-20 18:20:51 +0100677 %d
678 call setline(1, ['one', 'two', 'three'])
679 call assert_equal(1, line('w0'))
680 call assert_equal(3, line('w$'))
681
Bram Moolenaar75373f32017-08-07 22:02:30 +0200682 bwipeout!
683 call test_garbagecollect_now()
684endfunc
685
Bram Moolenaar2efb3232017-12-19 12:27:23 +0100686func Test_window_colon_command()
687 " This was reading invalid memory.
688 exe "norm! v\<C-W>:\<C-U>echo v:version"
689endfunc
690
Bram Moolenaar6f361c92018-01-31 19:06:50 +0100691func Test_access_freed_mem()
Bram Moolenaar1417c762019-07-27 17:31:36 +0200692 call assert_equal(&columns, winwidth(0))
Bram Moolenaar9a046fd2021-01-28 13:47:59 +0100693 " This was accessing freed memory (but with what events?)
694 au BufEnter,BufLeave,WinEnter,WinLeave 0 vs xxx
Bram Moolenaar6f361c92018-01-31 19:06:50 +0100695 arg 0
696 argadd
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +0200697 call assert_fails("all", "E242:")
Bram Moolenaar6f361c92018-01-31 19:06:50 +0100698 au!
699 bwipe xxx
Bram Moolenaar1417c762019-07-27 17:31:36 +0200700 call assert_equal(&columns, winwidth(0))
Bram Moolenaar6f361c92018-01-31 19:06:50 +0100701endfunc
702
Bram Moolenaara27e1dc2019-10-07 22:27:36 +0200703func Test_insert_cleared_on_switch_to_term()
704 CheckFeature terminal
705
706 set showmode
707 terminal
708 wincmd p
709
710 call feedkeys("i\<C-O>", 'ntx')
711 redraw
712
713 " The "-- (insert) --" indicator should be visible.
714 let chars = map(range(1, &columns), 'nr2char(screenchar(&lines, v:val))')
715 let str = trim(join(chars, ''))
716 call assert_equal('-- (insert) --', str)
717
718 call feedkeys("\<C-W>p", 'ntx')
719 redraw
720
721 " The "-- (insert) --" indicator should have been cleared.
722 let chars = map(range(1, &columns), 'nr2char(screenchar(&lines, v:val))')
723 let str = trim(join(chars, ''))
724 call assert_equal('', str)
725
726 set showmode&
727 %bw!
728endfunc
729
Bram Moolenaar5bab5552018-04-13 20:41:29 +0200730func Test_visual_cleared_after_window_split()
731 new | only!
732 let smd_save = &showmode
733 set showmode
734 let ls_save = &laststatus
735 set laststatus=1
736 call setline(1, ['a', 'b', 'c', 'd', ''])
737 norm! G
738 exe "norm! kkvk"
739 redraw
740 exe "norm! \<C-W>v"
741 redraw
742 " check if '-- VISUAL --' disappeared from command line
743 let columns = range(1, &columns)
744 let cmdlinechars = map(columns, 'nr2char(screenchar(&lines, v:val))')
745 let cmdline = join(cmdlinechars, '')
746 let cmdline_ltrim = substitute(cmdline, '^\s*', "", "")
747 let mode_shown = substitute(cmdline_ltrim, '\s*$', "", "")
748 call assert_equal('', mode_shown)
749 let &showmode = smd_save
750 let &laststatus = ls_save
751 bwipe!
752endfunc
753
Bram Moolenaar72cf47a2018-05-10 18:23:29 +0200754func Test_winrestcmd()
755 2split
756 3vsplit
Bram Moolenaara0c8aea2021-03-20 19:55:35 +0100757 let restcmd = winrestcmd()
Bram Moolenaar72cf47a2018-05-10 18:23:29 +0200758 call assert_equal(2, winheight(0))
759 call assert_equal(3, winwidth(0))
760 wincmd =
761 call assert_notequal(2, winheight(0))
762 call assert_notequal(3, winwidth(0))
Bram Moolenaara0c8aea2021-03-20 19:55:35 +0100763 exe restcmd
Bram Moolenaar72cf47a2018-05-10 18:23:29 +0200764 call assert_equal(2, winheight(0))
765 call assert_equal(3, winwidth(0))
766 only
Bram Moolenaara0c8aea2021-03-20 19:55:35 +0100767
768 wincmd v
769 wincmd s
770 wincmd v
771 redraw
772 let restcmd = winrestcmd()
773 wincmd _
774 wincmd |
775 exe restcmd
776 redraw
777 call assert_equal(restcmd, winrestcmd())
778
779 only
Bram Moolenaar72cf47a2018-05-10 18:23:29 +0200780endfunc
781
Bram Moolenaar1e115362019-01-09 23:01:02 +0100782func Fun_RenewFile()
Bram Moolenaar026587b2019-08-17 15:08:00 +0200783 " Need to wait a bit for the timestamp to be older.
Bram Moolenaarce90e362019-09-08 18:58:44 +0200784 let old_ftime = getftime("tmp.txt")
785 while getftime("tmp.txt") == old_ftime
786 sleep 100m
787 silent execute '!echo "1" > tmp.txt'
788 endwhile
Bram Moolenaara42df592018-12-24 00:22:39 +0100789 sp
790 wincmd p
791 edit! tmp.txt
Bram Moolenaar1e115362019-01-09 23:01:02 +0100792endfunc
Bram Moolenaara42df592018-12-24 00:22:39 +0100793
794func Test_window_prevwin()
795 " Can we make this work on MS-Windows?
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +0200796 CheckUnix
Bram Moolenaara42df592018-12-24 00:22:39 +0100797
798 set hidden autoread
Bram Moolenaardb4c9472022-10-15 22:06:06 +0100799 call writefile(['2'], 'tmp.txt', 'D')
Bram Moolenaara42df592018-12-24 00:22:39 +0100800 new tmp.txt
801 q
Bram Moolenaara42df592018-12-24 00:22:39 +0100802 call Fun_RenewFile()
803 call assert_equal(2, winnr())
804 wincmd p
805 call assert_equal(1, winnr())
806 wincmd p
807 q
808 call Fun_RenewFile()
809 call assert_equal(2, winnr())
810 wincmd p
811 call assert_equal(1, winnr())
812 wincmd p
813 " reset
814 q
Bram Moolenaara42df592018-12-24 00:22:39 +0100815 set hidden&vim autoread&vim
816 delfunc Fun_RenewFile
817endfunc
818
Bram Moolenaar8fcb60f2019-03-04 13:18:30 +0100819func Test_relative_cursor_position_in_one_line_window()
820 new
821 only
822 call setline(1, range(1, 10000))
823 normal 50%
824 let lnum = getcurpos()[1]
825 split
826 split
827 " make third window take as many lines as possible, other windows will
828 " become one line
829 3wincmd w
830 for i in range(1, &lines - 6)
831 wincmd +
832 redraw!
833 endfor
834
835 " first and second window should show cursor line
836 let wininfo = getwininfo()
837 call assert_equal(lnum, wininfo[0].topline)
838 call assert_equal(lnum, wininfo[1].topline)
839
840 only!
841 bwipe!
Yegappan Lakshmanan04c4c572022-08-30 19:48:24 +0100842 call assert_fails('call winrestview(test_null_dict())', 'E1297:')
Bram Moolenaar8fcb60f2019-03-04 13:18:30 +0100843endfunc
844
845func Test_relative_cursor_position_after_move_and_resize()
846 let so_save = &so
847 set so=0
848 enew
849 call setline(1, range(1, 10000))
850 normal 50%
851 split
852 1wincmd w
853 " Move cursor to first line in window
854 normal H
855 redraw!
856 " Reduce window height to two lines
857 let height = winheight(0)
858 while winheight(0) > 2
859 wincmd -
860 redraw!
861 endwhile
862 " move cursor to second/last line in window
863 normal j
864 " restore previous height
865 while winheight(0) < height
866 wincmd +
867 redraw!
868 endwhile
869 " make window two lines again
870 while winheight(0) > 2
871 wincmd -
872 redraw!
873 endwhile
874
875 " cursor should be at bottom line
876 let info = getwininfo(win_getid())[0]
877 call assert_equal(info.topline + 1, getcurpos()[1])
878
879 only!
880 bwipe!
881 let &so = so_save
882endfunc
883
884func Test_relative_cursor_position_after_resize()
885 let so_save = &so
886 set so=0
887 enew
888 call setline(1, range(1, 10000))
889 normal 50%
890 split
891 1wincmd w
892 let winid1 = win_getid()
893 let info = getwininfo(winid1)[0]
894 " Move cursor to second line in window
895 exe "normal " . (info.topline + 1) . "G"
896 redraw!
897 let lnum = getcurpos()[1]
898
899 " Make the window only two lines high, cursor should end up in top line
900 2wincmd w
901 exe (info.height - 2) . "wincmd +"
902 redraw!
903 let info = getwininfo(winid1)[0]
904 call assert_equal(lnum, info.topline)
905
906 only!
907 bwipe!
908 let &so = so_save
909endfunc
910
911func Test_relative_cursor_second_line_after_resize()
912 let so_save = &so
913 set so=0
914 enew
915 call setline(1, range(1, 10000))
916 normal 50%
917 split
918 1wincmd w
919 let winid1 = win_getid()
920 let info = getwininfo(winid1)[0]
921
922 " Make the window only two lines high
923 2wincmd _
924
925 " Move cursor to second line in window
926 normal H
927 normal j
928
929 " Make window size bigger, then back to 2 lines
930 for i in range(1, 10)
931 wincmd +
932 redraw!
933 endfor
934 for i in range(1, 10)
935 wincmd -
936 redraw!
937 endfor
938
939 " cursor should end up in bottom line
940 let info = getwininfo(winid1)[0]
941 call assert_equal(info.topline + 1, getcurpos()[1])
942
943 only!
944 bwipe!
945 let &so = so_save
946endfunc
947
Bram Moolenaara9b25352019-05-12 14:25:30 +0200948func Test_split_noscroll()
949 let so_save = &so
Bram Moolenaarbd2d68c2019-05-18 15:36:11 +0200950 enew
951 call setline(1, range(1, 8))
Bram Moolenaara9b25352019-05-12 14:25:30 +0200952 normal 100%
953 split
954
955 1wincmd w
956 let winid1 = win_getid()
957 let info1 = getwininfo(winid1)[0]
958
959 2wincmd w
960 let winid2 = win_getid()
961 let info2 = getwininfo(winid2)[0]
962
963 call assert_equal(1, info1.topline)
964 call assert_equal(1, info2.topline)
965
Bram Moolenaarbd2d68c2019-05-18 15:36:11 +0200966 " window that fits all lines by itself, but not when split: closing other
967 " window should restore fraction.
Bram Moolenaara9b25352019-05-12 14:25:30 +0200968 only!
Bram Moolenaarbd2d68c2019-05-18 15:36:11 +0200969 call setline(1, range(1, &lines - 10))
970 exe &lines / 4
971 let winid1 = win_getid()
972 let info1 = getwininfo(winid1)[0]
973 call assert_equal(1, info1.topline)
974 new
975 redraw
976 close
977 let info1 = getwininfo(winid1)[0]
978 call assert_equal(1, info1.topline)
979
Bram Moolenaara9b25352019-05-12 14:25:30 +0200980 bwipe!
981 let &so = so_save
982endfunc
983
Bram Moolenaar46ad2882019-04-08 20:01:47 +0200984" Tests for the winnr() function
985func Test_winnr()
986 only | tabonly
987 call assert_equal(1, winnr('j'))
988 call assert_equal(1, winnr('k'))
989 call assert_equal(1, winnr('h'))
990 call assert_equal(1, winnr('l'))
991
992 " create a set of horizontally and vertically split windows
993 leftabove new | wincmd p
994 leftabove new | wincmd p
995 rightbelow new | wincmd p
996 rightbelow new | wincmd p
997 leftabove vnew | wincmd p
998 leftabove vnew | wincmd p
999 rightbelow vnew | wincmd p
1000 rightbelow vnew | wincmd p
1001
1002 call assert_equal(8, winnr('j'))
1003 call assert_equal(2, winnr('k'))
1004 call assert_equal(4, winnr('h'))
1005 call assert_equal(6, winnr('l'))
1006 call assert_equal(9, winnr('2j'))
1007 call assert_equal(1, winnr('2k'))
1008 call assert_equal(3, winnr('2h'))
1009 call assert_equal(7, winnr('2l'))
1010
1011 " Error cases
1012 call assert_fails("echo winnr('0.2k')", 'E15:')
1013 call assert_equal(2, winnr('-2k'))
1014 call assert_fails("echo winnr('-2xj')", 'E15:')
1015 call assert_fails("echo winnr('j2j')", 'E15:')
1016 call assert_fails("echo winnr('ll')", 'E15:')
1017 call assert_fails("echo winnr('5')", 'E15:')
1018 call assert_equal(4, winnr('0h'))
Bram Moolenaar99fa7212020-04-26 15:59:55 +02001019 call assert_fails("let w = winnr([])", 'E730:')
1020 call assert_equal('unknown', win_gettype(-1))
1021 call assert_equal(-1, winheight(-1))
1022 call assert_equal(-1, winwidth(-1))
Bram Moolenaar46ad2882019-04-08 20:01:47 +02001023
1024 tabnew
1025 call assert_equal(8, tabpagewinnr(1, 'j'))
Bram Moolenaarce90e362019-09-08 18:58:44 +02001026 call assert_equal(2, 1->tabpagewinnr('k'))
Bram Moolenaar46ad2882019-04-08 20:01:47 +02001027 call assert_equal(4, tabpagewinnr(1, 'h'))
1028 call assert_equal(6, tabpagewinnr(1, 'l'))
1029
1030 only | tabonly
1031endfunc
1032
Bram Moolenaarf92e58c2019-09-08 21:51:41 +02001033func Test_winrestview()
1034 split runtest.vim
1035 normal 50%
1036 let view = winsaveview()
1037 close
1038 split runtest.vim
1039 eval view->winrestview()
1040 call assert_equal(view, winsaveview())
1041
1042 bwipe!
Yegappan Lakshmanan04c4c572022-08-30 19:48:24 +01001043 call assert_fails('call winrestview(test_null_dict())', 'E1297:')
Bram Moolenaarf92e58c2019-09-08 21:51:41 +02001044endfunc
1045
Bram Moolenaard20dcb32019-09-10 21:22:58 +02001046func Test_win_splitmove()
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01001047 CheckFeature quickfix
1048
Bram Moolenaard20dcb32019-09-10 21:22:58 +02001049 edit a
1050 leftabove split b
1051 leftabove vsplit c
1052 leftabove split d
Sean Dewar96cc4ae2024-02-20 21:52:31 +01001053
1054 " win_splitmove doesn't actually create or close any windows, so expect an
1055 " unchanged winid and no WinNew/WinClosed events, like :wincmd H/J/K/L.
1056 let s:triggered = []
1057 augroup WinSplitMove
1058 au!
1059 au WinNewPre * let s:triggered += ['WinNewPre']
1060 au WinNew * let s:triggered += ['WinNew', win_getid()]
1061 au WinClosed * let s:triggered += ['WinClosed', str2nr(expand('<afile>'))]
1062 augroup END
1063 let winid = win_getid()
1064
Bram Moolenaard20dcb32019-09-10 21:22:58 +02001065 call assert_equal(0, win_splitmove(winnr(), winnr('l')))
1066 call assert_equal(bufname(winbufnr(1)), 'c')
1067 call assert_equal(bufname(winbufnr(2)), 'd')
1068 call assert_equal(bufname(winbufnr(3)), 'b')
1069 call assert_equal(bufname(winbufnr(4)), 'a')
1070 call assert_equal(0, win_splitmove(winnr(), winnr('j'), {'vertical': 1}))
1071 call assert_equal(0, win_splitmove(winnr(), winnr('j'), {'vertical': 1}))
1072 call assert_equal(bufname(winbufnr(1)), 'c')
1073 call assert_equal(bufname(winbufnr(2)), 'b')
1074 call assert_equal(bufname(winbufnr(3)), 'd')
1075 call assert_equal(bufname(winbufnr(4)), 'a')
1076 call assert_equal(0, win_splitmove(winnr(), winnr('k'), {'vertical': 1}))
1077 call assert_equal(bufname(winbufnr(1)), 'd')
1078 call assert_equal(bufname(winbufnr(2)), 'c')
1079 call assert_equal(bufname(winbufnr(3)), 'b')
1080 call assert_equal(bufname(winbufnr(4)), 'a')
1081 call assert_equal(0, win_splitmove(winnr(), winnr('j'), {'rightbelow': v:true}))
1082 call assert_equal(bufname(winbufnr(1)), 'c')
1083 call assert_equal(bufname(winbufnr(2)), 'b')
1084 call assert_equal(bufname(winbufnr(3)), 'a')
1085 call assert_equal(bufname(winbufnr(4)), 'd')
Yegappan Lakshmanan04c4c572022-08-30 19:48:24 +01001086 call assert_fails('call win_splitmove(winnr(), winnr("k"), test_null_dict())', 'E1297:')
Sean Dewar96cc4ae2024-02-20 21:52:31 +01001087 call assert_equal([], s:triggered)
1088 call assert_equal(winid, win_getid())
1089
1090 unlet! s:triggered
1091 au! WinSplitMove
Bram Moolenaard20dcb32019-09-10 21:22:58 +02001092 only | bd
1093
1094 call assert_fails('call win_splitmove(winnr(), 123)', 'E957:')
1095 call assert_fails('call win_splitmove(123, winnr())', 'E957:')
1096 call assert_fails('call win_splitmove(winnr(), winnr())', 'E957:')
Bram Moolenaar7b94e772020-01-06 21:03:24 +01001097
1098 tabnew
1099 call assert_fails('call win_splitmove(1, win_getid(1, 1))', 'E957:')
1100 tabclose
Sean Dewar0fd44a52024-02-20 20:28:15 +01001101
1102 split
1103 augroup WinSplitMove
1104 au!
1105 au WinEnter * ++once call win_gotoid(win_getid(winnr('#')))
1106 augroup END
1107 call assert_fails('call win_splitmove(winnr(), winnr("#"))', 'E855:')
1108
1109 augroup WinSplitMove
1110 au!
1111 au WinLeave * ++once quit
1112 augroup END
1113 call assert_fails('call win_splitmove(winnr(), winnr("#"))', 'E855:')
1114
1115 split
1116 split
1117 augroup WinSplitMove
1118 au!
1119 au WinEnter * ++once let s:triggered = v:true
1120 \| call assert_fails('call win_splitmove(winnr("$"), winnr())', 'E242:')
1121 augroup END
1122 quit
1123 call assert_equal(v:true, s:triggered)
1124 unlet! s:triggered
1125
1126 new
1127 augroup WinSplitMove
1128 au!
1129 au BufHidden * ++once let s:triggered = v:true
1130 \| call assert_fails('call win_splitmove(winnr("#"), winnr())', 'E1159:')
1131 augroup END
1132 hide
1133 call assert_equal(v:true, s:triggered)
1134 unlet! s:triggered
1135
Sean Dewarabf70302024-02-24 10:20:24 +01001136 split
1137 let close_win = winnr('#')
1138 augroup WinSplitMove
1139 au!
1140 au WinEnter * ++once quit!
1141 augroup END
1142 call win_splitmove(close_win, winnr())
1143 call assert_equal(0, win_id2win(close_win))
1144
Sean Dewar0fd44a52024-02-20 20:28:15 +01001145 au! WinSplitMove
1146 augroup! WinSplitMove
1147 %bw!
Bram Moolenaard20dcb32019-09-10 21:22:58 +02001148endfunc
1149
Bram Moolenaare20b9ec2020-02-03 21:40:04 +01001150" Test for the :only command
1151func Test_window_only()
1152 new
1153 set modified
1154 new
1155 call assert_fails('only', 'E445:')
1156 only!
Bram Moolenaarbc2b71d2020-02-17 21:33:30 +01001157 " Test for :only with a count
1158 let wid = win_getid()
1159 new
1160 new
1161 3only
1162 call assert_equal(1, winnr('$'))
1163 call assert_equal(wid, win_getid())
1164 call assert_fails('close', 'E444:')
1165 call assert_fails('%close', 'E16:')
Bram Moolenaare20b9ec2020-02-03 21:40:04 +01001166endfunc
1167
Bram Moolenaar9f6277b2020-02-11 22:04:02 +01001168" Test for errors with :wincmd
1169func Test_wincmd_errors()
1170 call assert_fails('wincmd g', 'E474:')
1171 call assert_fails('wincmd ab', 'E474:')
1172endfunc
1173
1174" Test for errors with :winpos
1175func Test_winpos_errors()
1176 if !has("gui_running") && !has('win32')
1177 call assert_fails('winpos', 'E188:')
1178 endif
1179 call assert_fails('winpos 10', 'E466:')
1180endfunc
1181
Bram Moolenaar406cd902020-02-18 21:54:41 +01001182" Test for +cmd in a :split command
1183func Test_split_cmd()
1184 split +set\ readonly
1185 call assert_equal(1, &readonly)
1186 call assert_equal(2, winnr('$'))
1187 close
1188endfunc
1189
Bram Moolenaar5080b0a2020-03-22 21:23:47 +01001190" Create maximum number of horizontally or vertically split windows and then
1191" run commands that create a new horizontally/vertically split window
1192func Run_noroom_for_newwindow_test(dir_arg)
1193 let dir = (a:dir_arg == 'v') ? 'vert ' : ''
1194
1195 " Open as many windows as possible
Bram Moolenaarab505b12020-03-23 19:28:44 +01001196 while v:true
Bram Moolenaar5080b0a2020-03-22 21:23:47 +01001197 try
1198 exe dir . 'new'
1199 catch /E36:/
1200 break
1201 endtry
Bram Moolenaarab505b12020-03-23 19:28:44 +01001202 endwhile
Bram Moolenaar5080b0a2020-03-22 21:23:47 +01001203
Bram Moolenaardb4c9472022-10-15 22:06:06 +01001204 call writefile(['first', 'second', 'third'], 'Xnorfile1', 'D')
1205 call writefile([], 'Xnorfile2', 'D')
1206 call writefile([], 'Xnorfile3', 'D')
Bram Moolenaar5080b0a2020-03-22 21:23:47 +01001207
1208 " Argument list related commands
Bram Moolenaarb18b4962022-09-02 21:55:50 +01001209 args Xnorfile1 Xnorfile2 Xnorfile3
Bram Moolenaar5080b0a2020-03-22 21:23:47 +01001210 next
1211 for cmd in ['sargument 2', 'snext', 'sprevious', 'sNext', 'srewind',
1212 \ 'sfirst', 'slast']
1213 call assert_fails(dir .. cmd, 'E36:')
1214 endfor
1215 %argdelete
1216
1217 " Buffer related commands
1218 set modified
1219 hide enew
Bram Moolenaarb18b4962022-09-02 21:55:50 +01001220 for cmd in ['sbuffer Xnorfile1', 'sbnext', 'sbprevious', 'sbNext', 'sbrewind',
Bram Moolenaar5080b0a2020-03-22 21:23:47 +01001221 \ 'sbfirst', 'sblast', 'sball', 'sbmodified', 'sunhide']
1222 call assert_fails(dir .. cmd, 'E36:')
1223 endfor
1224
1225 " Window related commands
Bram Moolenaarb18b4962022-09-02 21:55:50 +01001226 for cmd in ['split', 'split Xnorfile2', 'new', 'new Xnorfile3', 'sview Xnorfile1',
Bram Moolenaar5080b0a2020-03-22 21:23:47 +01001227 \ 'sfind runtest.vim']
1228 call assert_fails(dir .. cmd, 'E36:')
1229 endfor
1230
1231 " Help
1232 call assert_fails(dir .. 'help', 'E36:')
1233 call assert_fails(dir .. 'helpgrep window', 'E36:')
1234
1235 " Command-line window
1236 if a:dir_arg == 'h'
1237 " Cmd-line window is always a horizontally split window
1238 call assert_beeps('call feedkeys("q:\<CR>", "xt")')
1239 endif
1240
1241 " Quickfix and location list window
1242 if has('quickfix')
1243 cexpr ''
1244 call assert_fails(dir .. 'copen', 'E36:')
1245 lexpr ''
1246 call assert_fails(dir .. 'lopen', 'E36:')
1247
1248 " Preview window
Bram Moolenaarb18b4962022-09-02 21:55:50 +01001249 call assert_fails(dir .. 'pedit Xnorfile2', 'E36:')
Bram Moolenaar5080b0a2020-03-22 21:23:47 +01001250 call setline(1, 'abc')
1251 call assert_fails(dir .. 'psearch abc', 'E36:')
1252 endif
1253
1254 " Window commands (CTRL-W ^ and CTRL-W f)
1255 if a:dir_arg == 'h'
1256 call assert_fails('call feedkeys("\<C-W>^", "xt")', 'E36:')
Bram Moolenaarb18b4962022-09-02 21:55:50 +01001257 call setline(1, 'Xnorfile1')
Bram Moolenaar5080b0a2020-03-22 21:23:47 +01001258 call assert_fails('call feedkeys("gg\<C-W>f", "xt")', 'E36:')
1259 endif
1260 enew!
1261
1262 " Tag commands (:stag, :stselect and :stjump)
1263 call writefile(["!_TAG_FILE_ENCODING\tutf-8\t//",
Bram Moolenaarb18b4962022-09-02 21:55:50 +01001264 \ "second\tXnorfile1\t2",
1265 \ "third\tXnorfile1\t3",],
Bram Moolenaar5080b0a2020-03-22 21:23:47 +01001266 \ 'Xtags')
1267 set tags=Xtags
1268 call assert_fails(dir .. 'stag second', 'E36:')
1269 call assert_fails('call feedkeys(":" .. dir .. "stselect second\n1\n", "xt")', 'E36:')
1270 call assert_fails(dir .. 'stjump second', 'E36:')
1271 call assert_fails(dir .. 'ptag second', 'E36:')
1272 set tags&
1273 call delete('Xtags')
1274
1275 " :isplit and :dsplit
1276 call setline(1, ['#define FOO 1', 'FOO'])
1277 normal 2G
1278 call assert_fails(dir .. 'isplit FOO', 'E36:')
1279 call assert_fails(dir .. 'dsplit FOO', 'E36:')
1280
1281 " terminal
1282 if has('terminal')
1283 call assert_fails(dir .. 'terminal', 'E36:')
1284 endif
1285
1286 %bwipe!
Bram Moolenaar5080b0a2020-03-22 21:23:47 +01001287 only
1288endfunc
1289
1290func Test_split_cmds_with_no_room()
1291 call Run_noroom_for_newwindow_test('h')
1292 call Run_noroom_for_newwindow_test('v')
1293endfunc
1294
Bram Moolenaarad48e6c2020-04-21 22:19:45 +02001295" Test for various wincmd failures
1296func Test_wincmd_fails()
1297 only!
1298 call assert_beeps("normal \<C-W>w")
1299 call assert_beeps("normal \<C-W>p")
1300 call assert_beeps("normal \<C-W>gk")
1301 call assert_beeps("normal \<C-W>r")
1302 call assert_beeps("normal \<C-W>K")
1303 call assert_beeps("normal \<C-W>H")
1304 call assert_beeps("normal \<C-W>2gt")
1305endfunc
1306
Bram Moolenaarfe6dce82020-09-04 14:41:21 +02001307func Test_window_resize()
1308 " Vertical :resize (absolute, relative, min and max size).
1309 vsplit
1310 vert resize 8
1311 call assert_equal(8, winwidth(0))
1312 vert resize +2
1313 call assert_equal(10, winwidth(0))
1314 vert resize -2
1315 call assert_equal(8, winwidth(0))
1316 vert resize
1317 call assert_equal(&columns - 2, winwidth(0))
1318 vert resize 0
1319 call assert_equal(1, winwidth(0))
1320 vert resize 99999
1321 call assert_equal(&columns - 2, winwidth(0))
1322
1323 %bwipe!
1324
1325 " Horizontal :resize (with absolute, relative size, min and max size).
1326 split
1327 resize 8
1328 call assert_equal(8, winheight(0))
1329 resize +2
1330 call assert_equal(10, winheight(0))
1331 resize -2
1332 call assert_equal(8, winheight(0))
1333 resize
1334 call assert_equal(&lines - 4, winheight(0))
1335 resize 0
1336 call assert_equal(1, winheight(0))
1337 resize 99999
1338 call assert_equal(&lines - 4, winheight(0))
1339
1340 " :resize with explicit window number.
1341 let other_winnr = winnr('j')
1342 exe other_winnr .. 'resize 10'
1343 call assert_equal(10, winheight(other_winnr))
1344 call assert_equal(&lines - 10 - 3, winheight(0))
Bram Moolenaar9668cc52020-10-17 17:39:55 +02001345 exe other_winnr .. 'resize +1'
1346 exe other_winnr .. 'resize +1'
1347 call assert_equal(12, winheight(other_winnr))
1348 call assert_equal(&lines - 10 - 3 -2, winheight(0))
Bram Moolenaar89015a62020-12-29 12:46:51 +01001349 close
1350
1351 vsplit
1352 wincmd l
1353 let other_winnr = winnr('h')
1354 call assert_notequal(winnr(), other_winnr)
Bram Moolenaar5efe0e52021-01-01 14:31:34 +01001355 exe 'vert ' .. other_winnr .. 'resize -' .. &columns
Bram Moolenaar89015a62020-12-29 12:46:51 +01001356 call assert_equal(0, winwidth(other_winnr))
Bram Moolenaarfe6dce82020-09-04 14:41:21 +02001357
1358 %bwipe!
1359endfunc
1360
Bram Moolenaar5d3c9f82020-06-26 20:41:39 +02001361" Test for adjusting the window width when a window is closed with some
1362" windows using 'winfixwidth'
1363func Test_window_width_adjust()
1364 only
1365 " Three vertical windows. Windows 1 and 2 have 'winfixwidth' set and close
1366 " window 2.
1367 wincmd v
1368 vert resize 10
1369 set winfixwidth
1370 wincmd v
1371 set winfixwidth
1372 wincmd c
1373 call assert_inrange(10, 12, winwidth(1))
1374 " Three vertical windows. Windows 2 and 3 have 'winfixwidth' set and close
1375 " window 3.
1376 only
1377 set winfixwidth
1378 wincmd v
1379 vert resize 10
1380 set winfixwidth
1381 wincmd v
1382 set nowinfixwidth
1383 wincmd b
1384 wincmd c
1385 call assert_inrange(10, 12, winwidth(2))
1386
1387 new | only
1388endfunc
1389
1390" Test for jumping to a vertical/horizontal neighbor window based on the
1391" current cursor position
dundargocc57b5bc2022-11-02 13:30:51 +00001392func Test_window_goto_neighbor()
Bram Moolenaar5d3c9f82020-06-26 20:41:39 +02001393 %bw!
1394
1395 " Vertical window movement
1396
1397 " create the following window layout:
1398 " +--+--+
1399 " |w1|w3|
1400 " +--+ |
1401 " |w2| |
1402 " +--+--+
1403 " |w4 |
1404 " +-----+
1405 new
1406 vsplit
1407 split
1408 " vertically jump from w4
1409 wincmd b
1410 call setline(1, repeat(' ', &columns))
1411 call cursor(1, 1)
1412 wincmd k
1413 call assert_equal(2, winnr())
1414 wincmd b
1415 call cursor(1, &columns)
1416 redraw!
1417 wincmd k
1418 call assert_equal(3, winnr())
1419 %bw!
1420
1421 " create the following window layout:
1422 " +--+--+--+
1423 " |w1|w2|w3|
1424 " +--+--+--+
1425 " |w4 |
1426 " +--------+
1427 new
1428 vsplit
1429 vsplit
1430 wincmd b
1431 call setline(1, repeat(' ', &columns))
1432 call cursor(1, 1)
1433 wincmd k
1434 call assert_equal(1, winnr())
1435 wincmd b
1436 call cursor(1, &columns / 2)
1437 redraw!
1438 wincmd k
1439 call assert_equal(2, winnr())
1440 wincmd b
1441 call cursor(1, &columns)
1442 redraw!
1443 wincmd k
1444 call assert_equal(3, winnr())
1445 %bw!
1446
1447 " Horizontal window movement
1448
1449 " create the following window layout:
1450 " +--+--+--+
1451 " |w1|w2|w4|
1452 " +--+--+ |
1453 " |w3 | |
1454 " +-----+--+
1455 vsplit
1456 split
1457 vsplit
1458 4wincmd l
1459 call setline(1, repeat([' '], &lines))
1460 call cursor(1, 1)
1461 redraw!
1462 wincmd h
1463 call assert_equal(2, winnr())
1464 4wincmd l
1465 call cursor(&lines, 1)
1466 redraw!
1467 wincmd h
1468 call assert_equal(3, winnr())
1469 %bw!
1470
1471 " create the following window layout:
1472 " +--+--+
1473 " |w1|w4|
1474 " +--+ +
1475 " |w2| |
1476 " +--+ +
1477 " |w3| |
1478 " +--+--+
1479 vsplit
1480 split
1481 split
1482 wincmd l
1483 call setline(1, repeat([' '], &lines))
1484 call cursor(1, 1)
1485 redraw!
1486 wincmd h
1487 call assert_equal(1, winnr())
1488 wincmd l
1489 call cursor(&lines / 2, 1)
1490 redraw!
1491 wincmd h
1492 call assert_equal(2, winnr())
1493 wincmd l
1494 call cursor(&lines, 1)
1495 redraw!
1496 wincmd h
1497 call assert_equal(3, winnr())
1498 %bw!
1499endfunc
1500
1501" Test for an autocmd closing the destination window when jumping from one
1502" window to another.
1503func Test_close_dest_window()
1504 split
Bram Moolenaarb18b4962022-09-02 21:55:50 +01001505 edit Xdstfile
Bram Moolenaar5d3c9f82020-06-26 20:41:39 +02001506
1507 " Test for BufLeave
1508 augroup T1
1509 au!
Bram Moolenaarb18b4962022-09-02 21:55:50 +01001510 au BufLeave Xdstfile $wincmd c
Bram Moolenaar5d3c9f82020-06-26 20:41:39 +02001511 augroup END
1512 wincmd b
1513 call assert_equal(1, winnr('$'))
Bram Moolenaarb18b4962022-09-02 21:55:50 +01001514 call assert_equal('Xdstfile', @%)
Bram Moolenaar5d3c9f82020-06-26 20:41:39 +02001515 augroup T1
1516 au!
1517 augroup END
1518
1519 " Test for WinLeave
1520 new
1521 wincmd p
1522 augroup T1
1523 au!
1524 au WinLeave * 1wincmd c
1525 augroup END
1526 wincmd t
1527 call assert_equal(1, winnr('$'))
Bram Moolenaarb18b4962022-09-02 21:55:50 +01001528 call assert_equal('Xdstfile', @%)
Bram Moolenaar5d3c9f82020-06-26 20:41:39 +02001529 augroup T1
1530 au!
1531 augroup END
1532 augroup! T1
1533 %bw!
1534endfunc
1535
Bram Moolenaard0fb9072021-12-09 11:57:22 +00001536func Test_window_minimal_size()
1537 set winminwidth=0 winminheight=0
1538
1539 " check size is fixed vertically
1540 new
1541 call win_execute(win_getid(2), 'wincmd _')
1542 call assert_equal(0, winheight(0))
1543 call feedkeys('0', 'tx')
1544 call assert_equal(1, winheight(0))
1545 bwipe!
1546
1547 " check size is fixed horizontally
1548 vert new
1549 call win_execute(win_getid(2), 'wincmd |')
1550 call assert_equal(0, winwidth(0))
1551 call feedkeys('0', 'tx')
1552 call assert_equal(1, winwidth(0))
1553 bwipe!
1554
1555 if has('timers')
1556 " check size is fixed in Insert mode
ichizokfa9a8e02021-12-12 16:42:09 +00001557 func s:CheckSize(timer) abort
1558 call win_execute(win_getid(2), 'wincmd _')
1559 call assert_equal(0, winheight(0))
1560 call feedkeys(" \<Esc>", 't!')
1561 endfunc
Bram Moolenaard0fb9072021-12-09 11:57:22 +00001562 new
ichizokfa9a8e02021-12-12 16:42:09 +00001563 call timer_start(100, function('s:CheckSize'))
Bram Moolenaard0fb9072021-12-09 11:57:22 +00001564 call feedkeys('a', 'tx!')
1565 call assert_equal(1, winheight(0))
1566 bwipe!
ichizokfa9a8e02021-12-12 16:42:09 +00001567 delfunc s:CheckSize
Bram Moolenaard0fb9072021-12-09 11:57:22 +00001568 endif
1569
1570 set winminwidth& winminheight&
1571endfunc
1572
Daniel Steinbergee630312022-01-10 13:36:34 +00001573func Test_win_move_separator()
1574 edit a
1575 leftabove vsplit b
1576 let w = winwidth(0)
1577 " check win_move_separator from left window on left window
1578 call assert_equal(1, winnr())
1579 for offset in range(5)
1580 call assert_true(win_move_separator(0, offset))
1581 call assert_equal(w + offset, winwidth(0))
1582 call assert_true(0->win_move_separator(-offset))
1583 call assert_equal(w, winwidth(0))
1584 endfor
1585 " check win_move_separator from right window on left window number
1586 wincmd l
1587 call assert_notequal(1, winnr())
1588 for offset in range(5)
1589 call assert_true(1->win_move_separator(offset))
1590 call assert_equal(w + offset, winwidth(1))
1591 call assert_true(win_move_separator(1, -offset))
1592 call assert_equal(w, winwidth(1))
1593 endfor
1594 " check win_move_separator from right window on left window ID
1595 let id = win_getid(1)
1596 for offset in range(5)
1597 call assert_true(win_move_separator(id, offset))
1598 call assert_equal(w + offset, winwidth(id))
1599 call assert_true(id->win_move_separator(-offset))
1600 call assert_equal(w, winwidth(id))
1601 endfor
zeertzjqa0c4e2f2022-01-29 10:59:53 +00001602 " check win_move_separator from right window on right window is no-op
1603 let w0 = winwidth(0)
1604 call assert_true(win_move_separator(0, 1))
1605 call assert_equal(w0, winwidth(0))
1606 call assert_true(win_move_separator(0, -1))
1607 call assert_equal(w0, winwidth(0))
zeertzjq873f41a2022-11-01 11:44:43 +00001608
Daniel Steinbergee630312022-01-10 13:36:34 +00001609 " check that win_move_separator doesn't error with offsets beyond moving
1610 " possibility
1611 call assert_true(win_move_separator(id, 5000))
1612 call assert_true(winwidth(id) > w)
1613 call assert_true(win_move_separator(id, -5000))
1614 call assert_true(winwidth(id) < w)
zeertzjq873f41a2022-11-01 11:44:43 +00001615
Daniel Steinbergee630312022-01-10 13:36:34 +00001616 " check that win_move_separator returns false for an invalid window
1617 wincmd =
1618 let w = winwidth(0)
1619 call assert_false(win_move_separator(-1, 1))
1620 call assert_equal(w, winwidth(0))
zeertzjq873f41a2022-11-01 11:44:43 +00001621
Daniel Steinbergee630312022-01-10 13:36:34 +00001622 " check that win_move_separator returns false for a popup window
1623 let id = popup_create(['hello', 'world'], {})
1624 let w = winwidth(id)
1625 call assert_false(win_move_separator(id, 1))
1626 call assert_equal(w, winwidth(id))
1627 call popup_close(id)
zeertzjq873f41a2022-11-01 11:44:43 +00001628
1629 " check that using another tabpage fails without crash
1630 let id = win_getid()
1631 tabnew
1632 call assert_fails('call win_move_separator(id, -1)', 'E1308:')
1633 tabclose
1634
Daniel Steinbergee630312022-01-10 13:36:34 +00001635 %bwipe!
1636endfunc
1637
1638func Test_win_move_statusline()
1639 edit a
1640 leftabove split b
1641 let h = winheight(0)
1642 " check win_move_statusline from top window on top window
1643 call assert_equal(1, winnr())
1644 for offset in range(5)
1645 call assert_true(win_move_statusline(0, offset))
1646 call assert_equal(h + offset, winheight(0))
1647 call assert_true(0->win_move_statusline(-offset))
1648 call assert_equal(h, winheight(0))
1649 endfor
1650 " check win_move_statusline from bottom window on top window number
1651 wincmd j
1652 call assert_notequal(1, winnr())
1653 for offset in range(5)
1654 call assert_true(1->win_move_statusline(offset))
1655 call assert_equal(h + offset, winheight(1))
1656 call assert_true(win_move_statusline(1, -offset))
1657 call assert_equal(h, winheight(1))
1658 endfor
zeertzjqa0c4e2f2022-01-29 10:59:53 +00001659 " check win_move_statusline from bottom window on bottom window
1660 let h0 = winheight(0)
1661 for offset in range(5)
1662 call assert_true(0->win_move_statusline(-offset))
1663 call assert_equal(h0 - offset, winheight(0))
1664 call assert_equal(1 + offset, &cmdheight)
1665 call assert_true(win_move_statusline(0, offset))
1666 call assert_equal(h0, winheight(0))
1667 call assert_equal(1, &cmdheight)
1668 endfor
1669 call assert_true(win_move_statusline(0, 1))
Bram Moolenaara2a89732022-08-31 14:46:18 +01001670 call assert_equal(h0, winheight(0))
1671 call assert_equal(1, &cmdheight)
Daniel Steinbergee630312022-01-10 13:36:34 +00001672 " check win_move_statusline from bottom window on top window ID
1673 let id = win_getid(1)
1674 for offset in range(5)
1675 call assert_true(win_move_statusline(id, offset))
1676 call assert_equal(h + offset, winheight(id))
1677 call assert_true(id->win_move_statusline(-offset))
1678 call assert_equal(h, winheight(id))
1679 endfor
Bram Moolenaar86e67172022-10-31 12:24:12 +00001680
Daniel Steinbergee630312022-01-10 13:36:34 +00001681 " check that win_move_statusline doesn't error with offsets beyond moving
1682 " possibility
1683 call assert_true(win_move_statusline(id, 5000))
1684 call assert_true(winheight(id) > h)
1685 call assert_true(win_move_statusline(id, -5000))
1686 call assert_true(winheight(id) < h)
Bram Moolenaar86e67172022-10-31 12:24:12 +00001687
Daniel Steinbergee630312022-01-10 13:36:34 +00001688 " check that win_move_statusline returns false for an invalid window
1689 wincmd =
1690 let h = winheight(0)
1691 call assert_false(win_move_statusline(-1, 1))
1692 call assert_equal(h, winheight(0))
Bram Moolenaar86e67172022-10-31 12:24:12 +00001693
Daniel Steinbergee630312022-01-10 13:36:34 +00001694 " check that win_move_statusline returns false for a popup window
1695 let id = popup_create(['hello', 'world'], {})
1696 let h = winheight(id)
1697 call assert_false(win_move_statusline(id, 1))
1698 call assert_equal(h, winheight(id))
1699 call popup_close(id)
Bram Moolenaar86e67172022-10-31 12:24:12 +00001700
1701 " check that using another tabpage fails without crash
1702 let id = win_getid()
1703 tabnew
1704 call assert_fails('call win_move_statusline(id, -1)', 'E1308:')
1705 tabclose
1706
Daniel Steinbergee630312022-01-10 13:36:34 +00001707 %bwipe!
1708endfunc
1709
Yegappan Lakshmanan72bb47e2022-04-03 11:22:38 +01001710" Test for window allocation failure
1711func Test_window_alloc_failure()
1712 %bw!
1713
1714 " test for creating a new window above current window
1715 call test_alloc_fail(GetAllocId('newwin_wvars'), 0, 0)
1716 call assert_fails('above new', 'E342:')
1717 call assert_equal(1, winnr('$'))
1718
1719 " test for creating a new window below current window
1720 call test_alloc_fail(GetAllocId('newwin_wvars'), 0, 0)
1721 call assert_fails('below new', 'E342:')
1722 call assert_equal(1, winnr('$'))
1723
1724 " test for popup window creation failure
1725 call test_alloc_fail(GetAllocId('newwin_wvars'), 0, 0)
1726 call assert_fails('call popup_create("Hello", {})', 'E342:')
1727 call assert_equal([], popup_list())
1728
1729 call test_alloc_fail(GetAllocId('newwin_wvars'), 0, 0)
1730 call assert_fails('split', 'E342:')
1731 call assert_equal(1, winnr('$'))
1732
Bram Moolenaarb18b4962022-09-02 21:55:50 +01001733 edit Xwaffile1
1734 edit Xwaffile2
Yegappan Lakshmanan72bb47e2022-04-03 11:22:38 +01001735 call test_alloc_fail(GetAllocId('newwin_wvars'), 0, 0)
Bram Moolenaarb18b4962022-09-02 21:55:50 +01001736 call assert_fails('sb Xwaffile1', 'E342:')
Yegappan Lakshmanan72bb47e2022-04-03 11:22:38 +01001737 call assert_equal(1, winnr('$'))
Bram Moolenaarb18b4962022-09-02 21:55:50 +01001738 call assert_equal('Xwaffile2', @%)
Yegappan Lakshmanan72bb47e2022-04-03 11:22:38 +01001739 %bw!
1740
1741 " FIXME: The following test crashes Vim
1742 " test for new tabpage creation failure
1743 " call test_alloc_fail(GetAllocId('newwin_wvars'), 0, 0)
1744 " call assert_fails('tabnew', 'E342:')
1745 " call assert_equal(1, tabpagenr('$'))
1746 " call assert_equal(1, winnr('$'))
1747
1748 " This test messes up the internal Vim window/frame information. So the
1749 " Test_window_cmd_cmdwin_with_vsp() test fails after running this test.
1750 " Open a new tab and close everything else to fix this issue.
1751 tabnew
1752 tabonly
1753endfunc
Bram Moolenaard0fb9072021-12-09 11:57:22 +00001754
Luuk van Baalfd7e60a2022-09-07 14:42:49 +01001755func Test_win_equal_last_status()
1756 let save_lines = &lines
1757 set lines=20
1758 set splitbelow
1759 set laststatus=0
1760
1761 split | split | quit
1762 call assert_equal(winheight(1), winheight(2))
1763
1764 let &lines = save_lines
1765 set splitbelow&
1766 set laststatus&
1767endfunc
1768
Luuk van Baal13ece2a2022-10-03 15:28:08 +01001769" Test "screen" and "cursor" values for 'splitkeep' with a sequence of
Luuk van Baalfaf1d412022-09-19 16:45:29 +01001770" split operations for various options: with and without a winbar,
1771" tabline, for each possible value of 'laststatus', 'scrolloff',
Luuk van Baal13ece2a2022-10-03 15:28:08 +01001772" 'equalalways', and with the cursor at the top, middle and bottom.
1773func Test_splitkeep_options()
Luuk van Baal594f9e02022-09-16 12:52:58 +01001774 " disallow window resizing
1775 let save_WS = &t_WS
1776 set t_WS=
1777
Luuk van Baal29ab5242022-09-11 16:59:53 +01001778 let gui = has("gui_running")
Luuk van Baal594f9e02022-09-16 12:52:58 +01001779 inoremap <expr> c "<cmd>copen<bar>wincmd k<CR>"
Luuk van Baal13ece2a2022-10-03 15:28:08 +01001780 for run in range(0, 20)
1781 let &splitkeep = run > 10 ? 'topline' : 'screen'
1782 let &scrolloff = (!(run % 4) ? 0 : run)
1783 let &laststatus = (run % 3)
1784 let &splitbelow = (run % 3)
1785 let &equalalways = (run % 2)
1786 let wsb = (run % 2) && &splitbelow
1787 let tl = (gui ? 0 : ((run % 5) ? 1 : 0))
1788 let pos = !(run % 3) ? 'H' : ((run % 2) ? 'M' : 'L')
1789 tabnew | tabonly! | redraw
Luuk van Baal594f9e02022-09-16 12:52:58 +01001790 execute (run % 5) ? 'tabnew' : ''
1791 execute (run % 2) ? 'nnoremenu 1.10 WinBar.Test :echo' : ''
Luuk van Baal594f9e02022-09-16 12:52:58 +01001792 call setline(1, range(1, 256))
1793 " No scroll for restore_snapshot
1794 norm G
1795 try
1796 copen | close | colder
1797 catch /E380/
1798 endtry
1799 call assert_equal(257 - winheight(0), line("w0"))
Luuk van Baal29ab5242022-09-11 16:59:53 +01001800
Luuk van Baal594f9e02022-09-16 12:52:58 +01001801 " No scroll for firstwin horizontal split
1802 execute 'norm gg' . pos
1803 split | redraw | wincmd k
1804 call assert_equal(1, line("w0"))
1805 call assert_equal(&scroll, winheight(0) / 2)
1806 wincmd j
Luuk van Baal13ece2a2022-10-03 15:28:08 +01001807 call assert_equal(&spk == 'topline' ? 1 : win_screenpos(0)[0] - tl - wsb, line("w0"))
Luuk van Baal29ab5242022-09-11 16:59:53 +01001808
Luuk van Baal594f9e02022-09-16 12:52:58 +01001809 " No scroll when resizing windows
Luuk van Baal13ece2a2022-10-03 15:28:08 +01001810 wincmd k | resize +2 | redraw
Luuk van Baal594f9e02022-09-16 12:52:58 +01001811 call assert_equal(1, line("w0"))
1812 wincmd j
Luuk van Baal13ece2a2022-10-03 15:28:08 +01001813 call assert_equal(&spk == 'topline' ? 1 : win_screenpos(0)[0] - tl - wsb, line("w0"))
Luuk van Baal29ab5242022-09-11 16:59:53 +01001814
Luuk van Baal594f9e02022-09-16 12:52:58 +01001815 " No scroll when dragging statusline
1816 call win_move_statusline(1, -3)
Luuk van Baal13ece2a2022-10-03 15:28:08 +01001817 call assert_equal(&spk == 'topline' ? 1 : win_screenpos(0)[0] - tl - wsb, line("w0"))
Luuk van Baal594f9e02022-09-16 12:52:58 +01001818 wincmd k
1819 call assert_equal(1, line("w0"))
Luuk van Baal29ab5242022-09-11 16:59:53 +01001820
Luuk van Baal594f9e02022-09-16 12:52:58 +01001821 " No scroll when changing shellsize
1822 set lines+=2
1823 call assert_equal(1, line("w0"))
1824 wincmd j
Luuk van Baal13ece2a2022-10-03 15:28:08 +01001825 call assert_equal(&spk == 'topline' ? 1 : win_screenpos(0)[0] - tl - wsb, line("w0"))
Luuk van Baal594f9e02022-09-16 12:52:58 +01001826 set lines-=2
Luuk van Baal13ece2a2022-10-03 15:28:08 +01001827 call assert_equal(&spk == 'topline' ? 1 : win_screenpos(0)[0] - tl - wsb, line("w0"))
Luuk van Baal594f9e02022-09-16 12:52:58 +01001828 wincmd k
1829 call assert_equal(1, line("w0"))
Luuk van Baal29ab5242022-09-11 16:59:53 +01001830
Luuk van Baal594f9e02022-09-16 12:52:58 +01001831 " No scroll when equalizing windows
1832 wincmd =
1833 call assert_equal(1, line("w0"))
1834 wincmd j
Luuk van Baal13ece2a2022-10-03 15:28:08 +01001835 call assert_equal(&spk == 'topline' ? 1 : win_screenpos(0)[0] - tl - wsb, line("w0"))
Luuk van Baal594f9e02022-09-16 12:52:58 +01001836 wincmd k
1837 call assert_equal(1, line("w0"))
Luuk van Baal29ab5242022-09-11 16:59:53 +01001838
Luuk van Baal594f9e02022-09-16 12:52:58 +01001839 " No scroll in windows split multiple times
1840 vsplit | split | 4wincmd w
Luuk van Baal13ece2a2022-10-03 15:28:08 +01001841 call assert_equal(&spk == 'topline' ? 1 : win_screenpos(0)[0] - tl - wsb, line("w0"))
Luuk van Baal594f9e02022-09-16 12:52:58 +01001842 1wincmd w | quit | wincmd l | split
Luuk van Baal13ece2a2022-10-03 15:28:08 +01001843 call assert_equal(&spk == 'topline' ? 1 : win_screenpos(0)[0] - tl - wsb, line("w0"))
Luuk van Baal594f9e02022-09-16 12:52:58 +01001844 wincmd j
Luuk van Baal13ece2a2022-10-03 15:28:08 +01001845 call assert_equal(&spk == 'topline' ? 1 : win_screenpos(0)[0] - tl - wsb, line("w0"))
Luuk van Baal29ab5242022-09-11 16:59:53 +01001846
Luuk van Baal594f9e02022-09-16 12:52:58 +01001847 " No scroll in small window
1848 2wincmd w | only | 5split | wincmd k
1849 call assert_equal(1, line("w0"))
1850 wincmd j
Luuk van Baal13ece2a2022-10-03 15:28:08 +01001851 call assert_equal(&spk == 'topline' ? 1 : win_screenpos(0)[0] - tl - wsb, line("w0"))
Luuk van Baal29ab5242022-09-11 16:59:53 +01001852
Luuk van Baal594f9e02022-09-16 12:52:58 +01001853 " No scroll for vertical split
1854 quit | vsplit | wincmd l
1855 call assert_equal(1, line("w0"))
1856 wincmd h
1857 call assert_equal(1, line("w0"))
Luuk van Baal29ab5242022-09-11 16:59:53 +01001858
Luuk van Baal594f9e02022-09-16 12:52:58 +01001859 " No scroll in windows split and quit multiple times
Luuk van Baal13ece2a2022-10-03 15:28:08 +01001860 quit | redraw | split | split | quit | redraw
1861 call assert_equal(&spk == 'topline' ? 1 : win_screenpos(0)[0] - tl - wsb, line("w0"))
Luuk van Baal29ab5242022-09-11 16:59:53 +01001862
Luuk van Baal594f9e02022-09-16 12:52:58 +01001863 " No scroll for new buffer
1864 1wincmd w | only | copen | wincmd k
1865 call assert_equal(1, line("w0"))
1866 only
1867 call assert_equal(1, line("w0"))
1868 above copen | wincmd j
Luuk van Baal13ece2a2022-10-03 15:28:08 +01001869 call assert_equal(&spk == 'topline' ? 1 : win_screenpos(0)[0] - tl, line("w0"))
Luuk van Baal29ab5242022-09-11 16:59:53 +01001870
Luuk van Baal594f9e02022-09-16 12:52:58 +01001871 " No scroll when opening cmdwin, and no cursor move when closing cmdwin.
1872 only | norm ggL
1873 let curpos = getcurpos()
1874 norm q:
1875 call assert_equal(1, line("w0"))
1876 call assert_equal(curpos, getcurpos())
Luuk van Baal29ab5242022-09-11 16:59:53 +01001877
Bram Moolenaarfdbd14e2023-02-14 21:56:42 +00001878 " Scroll when cursor becomes invalid in insert mode.
Luuk van Baal594f9e02022-09-16 12:52:58 +01001879 norm Lic
Luuk van Baalbc3dc292023-02-15 16:45:27 +00001880 call assert_equal(curpos, getcurpos(), 'run ' .. run)
Luuk van Baal29ab5242022-09-11 16:59:53 +01001881
Luuk van Baal594f9e02022-09-16 12:52:58 +01001882 " No scroll when topline not equal to 1
1883 only | execute "norm gg5\<C-e>" | split | wincmd k
1884 call assert_equal(6, line("w0"))
1885 wincmd j
Luuk van Baal13ece2a2022-10-03 15:28:08 +01001886 call assert_equal(&spk == 'topline' ? 6 : 5 + win_screenpos(0)[0] - tl - wsb, line("w0"))
Luuk van Baal29ab5242022-09-11 16:59:53 +01001887 endfor
1888
1889 tabnew | tabonly! | %bwipeout!
1890 iunmap c
Luuk van Baal29ab5242022-09-11 16:59:53 +01001891 set scrolloff&
1892 set splitbelow&
1893 set laststatus&
1894 set equalalways&
Luuk van Baal13ece2a2022-10-03 15:28:08 +01001895 set splitkeep&
Luuk van Baal594f9e02022-09-16 12:52:58 +01001896 let &t_WS = save_WS
Luuk van Baal29ab5242022-09-11 16:59:53 +01001897endfunc
Luuk van Baalfd7e60a2022-09-07 14:42:49 +01001898
Bram Moolenaare0f86912023-03-01 17:55:31 +00001899func Test_splitkeep_cmdwin_cursor_position()
Luuk van Baal13ece2a2022-10-03 15:28:08 +01001900 set splitkeep=screen
mityu12167d82022-09-15 17:44:07 +01001901 call setline(1, range(&lines))
1902
1903 " No scroll when cursor is at near bottom of window and cusor position
1904 " recompution (done by line('w0') in this test) happens while in cmdwin.
1905 normal! G
1906 let firstline = line('w0')
1907 autocmd CmdwinEnter * ++once autocmd WinEnter * ++once call line('w0')
1908 execute "normal! q:\<C-w>q"
1909 redraw!
1910 call assert_equal(firstline, line('w0'))
1911
1912 " User script can change cursor position successfully while in cmdwin and it
1913 " shouldn't be changed when closing cmdwin.
1914 execute "normal! Gq:\<Cmd>call win_execute(winnr('#')->win_getid(), 'call cursor(1, 1)')\<CR>\<C-w>q"
1915 call assert_equal(1, line('.'))
1916 call assert_equal(1, col('.'))
1917
1918 execute "normal! Gq:\<Cmd>autocmd WinEnter * ++once call cursor(1, 1)\<CR>\<C-w>q"
1919 call assert_equal(1, line('.'))
1920 call assert_equal(1, col('.'))
1921
1922 %bwipeout!
Luuk van Baal13ece2a2022-10-03 15:28:08 +01001923 set splitkeep&
Bram Moolenaare0f86912023-03-01 17:55:31 +00001924endfunc
Luuk van Baald5bc7622022-09-17 16:16:35 +01001925
Bram Moolenaare0f86912023-03-01 17:55:31 +00001926func Test_splitkeep_misc()
Luuk van Baal13ece2a2022-10-03 15:28:08 +01001927 set splitkeep=screen
Luuk van Baald5bc7622022-09-17 16:16:35 +01001928
1929 call setline(1, range(1, &lines))
Luuk van Baala109f392023-06-02 14:16:35 +01001930 " Cursor is adjusted to start and end of buffer
1931 norm M
1932 wincmd s
1933 resize 1
1934 call assert_equal(1, line('.'))
1935 wincmd j
1936 norm GM
1937 resize 1
1938 call assert_equal(&lines, line('.'))
1939 only!
1940
1941 set splitbelow
Luuk van Baald5bc7622022-09-17 16:16:35 +01001942 norm Gzz
1943 let top = line('w0')
Luuk van Baalfaf1d412022-09-19 16:45:29 +01001944 " No scroll when aucmd_win is opened
Luuk van Baald5bc7622022-09-17 16:16:35 +01001945 call setbufvar(bufnr("test", 1) , '&buftype', 'nofile')
1946 call assert_equal(top, line('w0'))
Luuk van Baalfaf1d412022-09-19 16:45:29 +01001947 " No scroll when tab is changed/closed
1948 tab help | close
1949 call assert_equal(top, line('w0'))
1950 " No scroll when help is closed and buffer line count < window height
1951 norm ggdG
1952 call setline(1, range(1, &lines - 10))
Luuk van Baald5bc7622022-09-17 16:16:35 +01001953 norm G
1954 let top = line('w0')
1955 help | quit
1956 call assert_equal(top, line('w0'))
Luuk van Baal346823d2022-10-05 18:26:42 +01001957 " No error when resizing window in autocmd and buffer length changed
1958 autocmd FileType qf exe "resize" line('$')
1959 cexpr getline(1, '$')
1960 copen
1961 wincmd p
1962 norm dd
1963 cexpr getline(1, '$')
Luuk van Baald5bc7622022-09-17 16:16:35 +01001964
Luuk van Baalfaf1d412022-09-19 16:45:29 +01001965 %bwipeout!
Luuk van Baald5bc7622022-09-17 16:16:35 +01001966 set splitbelow&
Luuk van Baal13ece2a2022-10-03 15:28:08 +01001967 set splitkeep&
Luuk van Baald5bc7622022-09-17 16:16:35 +01001968endfunc
1969
Luuk van Baal16af9132023-08-20 20:44:59 +02001970func Test_splitkeep_cursor()
1971 CheckScreendump
1972 let lines =<< trim END
1973 set splitkeep=screen
1974 autocmd CursorMoved * wincmd p | wincmd p
1975 call setline(1, range(1, 200))
1976 func CursorEqualize()
1977 call cursor(100, 1)
1978 wincmd =
1979 endfunc
1980 wincmd s
1981 call CursorEqualize()
1982 END
1983 call writefile(lines, 'XTestSplitkeepCallback', 'D')
1984 let buf = RunVimInTerminal('-S XTestSplitkeepCallback', #{rows: 8})
1985
1986 call VerifyScreenDump(buf, 'Test_splitkeep_cursor_1', {})
1987
1988 call term_sendkeys(buf, "j")
1989 call VerifyScreenDump(buf, 'Test_splitkeep_cursor_2', {})
1990
1991 call term_sendkeys(buf, ":set scrolloff=0\<CR>G")
1992 call VerifyScreenDump(buf, 'Test_splitkeep_cursor_3', {})
1993
1994 call StopVimInTerminal(buf)
1995endfunc
1996
Bram Moolenaare0f86912023-03-01 17:55:31 +00001997func Test_splitkeep_callback()
Luuk van Baal20e58562022-09-23 12:57:09 +01001998 CheckScreendump
1999 let lines =<< trim END
Luuk van Baal13ece2a2022-10-03 15:28:08 +01002000 set splitkeep=screen
Luuk van Baal20e58562022-09-23 12:57:09 +01002001 call setline(1, range(&lines))
Luuk van Baal13ece2a2022-10-03 15:28:08 +01002002 function C1(a, b)
Luuk van Baal20e58562022-09-23 12:57:09 +01002003 split | wincmd p
2004 endfunction
Luuk van Baal13ece2a2022-10-03 15:28:08 +01002005 function C2(a, b)
Luuk van Baal20e58562022-09-23 12:57:09 +01002006 close | split
2007 endfunction
Luuk van Baal13ece2a2022-10-03 15:28:08 +01002008 nn j <cmd>call job_start([&sh, &shcf, "true"], { 'exit_cb': 'C1' })<CR>
2009 nn t <cmd>call popup_create(term_start([&sh, &shcf, "true"],
2010 \ { 'hidden': 1, 'exit_cb': 'C2' }), {})<CR>
Luuk van Baal20e58562022-09-23 12:57:09 +01002011 END
Luuk van Baal13ece2a2022-10-03 15:28:08 +01002012 call writefile(lines, 'XTestSplitkeepCallback', 'D')
2013 let buf = RunVimInTerminal('-S XTestSplitkeepCallback', #{rows: 8})
Luuk van Baal20e58562022-09-23 12:57:09 +01002014
2015 call term_sendkeys(buf, "j")
Luuk van Baal13ece2a2022-10-03 15:28:08 +01002016 call VerifyScreenDump(buf, 'Test_splitkeep_callback_1', {})
Luuk van Baal20e58562022-09-23 12:57:09 +01002017
Luuk van Baal13ece2a2022-10-03 15:28:08 +01002018 call term_sendkeys(buf, ":quit\<CR>Ht")
2019 call VerifyScreenDump(buf, 'Test_splitkeep_callback_2', {})
Luuk van Baal20e58562022-09-23 12:57:09 +01002020
2021 call term_sendkeys(buf, ":set sb\<CR>:quit\<CR>Gj")
Luuk van Baal13ece2a2022-10-03 15:28:08 +01002022 call VerifyScreenDump(buf, 'Test_splitkeep_callback_3', {})
Luuk van Baal20e58562022-09-23 12:57:09 +01002023
Luuk van Baal13ece2a2022-10-03 15:28:08 +01002024 call term_sendkeys(buf, ":quit\<CR>Gt")
2025 call VerifyScreenDump(buf, 'Test_splitkeep_callback_4', {})
zeertzjq378e6c02023-01-14 11:46:49 +00002026
2027 call StopVimInTerminal(buf)
Luuk van Baal20e58562022-09-23 12:57:09 +01002028endfunc
2029
Bram Moolenaare0f86912023-03-01 17:55:31 +00002030func Test_splitkeep_fold()
Luuk van Baal13ece2a2022-10-03 15:28:08 +01002031 CheckScreendump
Luuk van Baal7c1cbb62022-09-27 12:31:15 +01002032
Luuk van Baal13ece2a2022-10-03 15:28:08 +01002033 let lines =<< trim END
2034 set splitkeep=screen
2035 set foldmethod=marker
2036 set number
2037 let line = 1
2038 for n in range(1, &lines)
2039 call setline(line, ['int FuncName() {/*{{{*/', 1, 2, 3, 4, 5, '}/*}}}*/',
2040 \ 'after fold'])
2041 let line += 8
2042 endfor
2043 END
2044 call writefile(lines, 'XTestSplitkeepFold', 'D')
2045 let buf = RunVimInTerminal('-S XTestSplitkeepFold', #{rows: 10})
Luuk van Baal7c1cbb62022-09-27 12:31:15 +01002046
2047 call term_sendkeys(buf, "L:wincmd s\<CR>")
Luuk van Baal13ece2a2022-10-03 15:28:08 +01002048 call VerifyScreenDump(buf, 'Test_splitkeep_fold_1', {})
Luuk van Baal7c1cbb62022-09-27 12:31:15 +01002049
2050 call term_sendkeys(buf, ":quit\<CR>")
Luuk van Baal13ece2a2022-10-03 15:28:08 +01002051 call VerifyScreenDump(buf, 'Test_splitkeep_fold_2', {})
Luuk van Baal7c1cbb62022-09-27 12:31:15 +01002052
2053 call term_sendkeys(buf, "H:below split\<CR>")
Luuk van Baal13ece2a2022-10-03 15:28:08 +01002054 call VerifyScreenDump(buf, 'Test_splitkeep_fold_3', {})
Luuk van Baal7c1cbb62022-09-27 12:31:15 +01002055
2056 call term_sendkeys(buf, ":wincmd k\<CR>:quit\<CR>")
Luuk van Baal13ece2a2022-10-03 15:28:08 +01002057 call VerifyScreenDump(buf, 'Test_splitkeep_fold_4', {})
zeertzjq378e6c02023-01-14 11:46:49 +00002058
2059 call StopVimInTerminal(buf)
Bram Moolenaare0f86912023-03-01 17:55:31 +00002060endfunc
Luuk van Baal7c1cbb62022-09-27 12:31:15 +01002061
Bram Moolenaare0f86912023-03-01 17:55:31 +00002062func Test_splitkeep_status()
Luuk van Baal74a694d2022-11-28 16:49:36 +00002063 CheckScreendump
2064
2065 let lines =<< trim END
2066 call setline(1, ['a', 'b', 'c'])
2067 set nomodified
2068 set splitkeep=screen
2069 let win = winnr()
2070 wincmd s
2071 wincmd j
2072 END
2073 call writefile(lines, 'XTestSplitkeepStatus', 'D')
2074 let buf = RunVimInTerminal('-S XTestSplitkeepStatus', #{rows: 10})
2075
2076 call term_sendkeys(buf, ":call win_move_statusline(win, 1)\<CR>")
2077 call VerifyScreenDump(buf, 'Test_splitkeep_status_1', {})
zeertzjq378e6c02023-01-14 11:46:49 +00002078
2079 call StopVimInTerminal(buf)
Bram Moolenaare0f86912023-03-01 17:55:31 +00002080endfunc
Luuk van Baal74a694d2022-11-28 16:49:36 +00002081
Luuk van Baalb926bf42023-05-06 12:53:50 +01002082" skipcol is not reset unnecessarily and is copied to new window
2083func Test_splitkeep_skipcol()
2084 CheckScreendump
2085
2086 let lines =<< trim END
2087 set splitkeep=topline smoothscroll splitbelow scrolloff=0
2088 call setline(1, 'with lots of text in one line '->repeat(6))
2089 norm 2
2090 wincmd s
2091 END
2092
2093 call writefile(lines, 'XTestSplitkeepSkipcol', 'D')
2094 let buf = RunVimInTerminal('-S XTestSplitkeepSkipcol', #{rows: 12, cols: 40})
2095
2096 call VerifyScreenDump(buf, 'Test_splitkeep_skipcol_1', {})
2097endfunc
2098
Bram Moolenaare0f86912023-03-01 17:55:31 +00002099func Test_new_help_window_on_error()
Rob Pillingcb94c912022-12-13 12:26:09 +00002100 help change.txt
2101 execute "normal! /CTRL-@\<CR>"
2102 silent! execute "normal! \<C-W>]"
2103
2104 let wincount = winnr('$')
2105 help 'mod'
2106
2107 call assert_equal(wincount, winnr('$'))
2108 call assert_equal(expand("<cword>"), "'mod'")
Bram Moolenaare0f86912023-03-01 17:55:31 +00002109endfunc
2110
Sean Dewar0fd44a52024-02-20 20:28:15 +01002111func Test_splitmove_flatten_frame()
2112 split
2113 vsplit
2114
2115 wincmd L
2116 let layout = winlayout()
2117 wincmd K
2118 wincmd L
2119 call assert_equal(winlayout(), layout)
2120
2121 only!
2122endfunc
2123
Sean Dewar02fcae02024-02-21 19:40:44 +01002124func Test_autocmd_window_force_room()
Sean Dewar0fd44a52024-02-20 20:28:15 +01002125 " Open as many windows as possible
2126 while v:true
2127 try
2128 split
2129 catch /E36:/
2130 break
2131 endtry
2132 endwhile
2133 while v:true
2134 try
2135 vsplit
2136 catch /E36:/
2137 break
2138 endtry
2139 endwhile
2140
2141 wincmd j
2142 vsplit
2143 call assert_fails('wincmd H', 'E36:')
2144 call assert_fails('wincmd J', 'E36:')
2145 call assert_fails('wincmd K', 'E36:')
2146 call assert_fails('wincmd L', 'E36:')
2147
2148 edit unload me
2149 enew
2150 bunload! unload\ me
Sean Dewar02fcae02024-02-21 19:40:44 +01002151 augroup AucmdWinForceRoom
Sean Dewar0fd44a52024-02-20 20:28:15 +01002152 au!
2153 au BufEnter * ++once let s:triggered = v:true
2154 \| call assert_equal('autocmd', win_gettype())
2155 augroup END
2156 let layout = winlayout()
2157 let restcmd = winrestcmd()
2158 " bufload opening the autocommand window shouldn't give E36.
2159 call bufload('unload me')
2160 call assert_equal(v:true, s:triggered)
2161 call assert_equal(winlayout(), layout)
2162 call assert_equal(winrestcmd(), restcmd)
2163
2164 unlet! s:triggered
Sean Dewar02fcae02024-02-21 19:40:44 +01002165 au! AucmdWinForceRoom
2166 augroup! AucmdWinForceRoom
Sean Dewar0fd44a52024-02-20 20:28:15 +01002167 %bw!
2168endfunc
Rob Pillingcb94c912022-12-13 12:26:09 +00002169
Sean Dewarf8658952024-02-20 22:05:10 +01002170func Test_win_gotoid_splitmove_textlock_cmdwin()
2171 call setline(1, 'foo')
2172 new
2173 let curwin = win_getid()
2174 call setline(1, 'bar')
2175
2176 set debug+=throw indentexpr=win_gotoid(win_getid(winnr('#')))
2177 call assert_fails('normal! ==', 'E565:')
2178 call assert_equal(curwin, win_getid())
Sean Dewar2a65e732024-02-22 19:53:33 +01002179 " No error if attempting to switch to curwin; nothing happens.
2180 set indentexpr=assert_equal(1,win_gotoid(win_getid()))
2181 normal! ==
2182 call assert_equal(curwin, win_getid())
Sean Dewarf8658952024-02-20 22:05:10 +01002183
2184 set indentexpr=win_splitmove(winnr('#'),winnr())
2185 call assert_fails('normal! ==', 'E565:')
2186 call assert_equal(curwin, win_getid())
2187
2188 %bw!
2189 set debug-=throw indentexpr&
2190
2191 call feedkeys('q:'
2192 \ .. ":call assert_fails('call win_splitmove(winnr(''#''), winnr())', 'E11:')\<CR>"
2193 \ .. ":call assert_equal('command', win_gettype())\<CR>"
2194 \ .. ":call assert_equal('', win_gettype(winnr('#')))\<CR>", 'ntx')
2195
2196 call feedkeys('q:'
2197 \ .. ":call assert_fails('call win_gotoid(win_getid(winnr(''#'')))', 'E11:')\<CR>"
Sean Dewar2a65e732024-02-22 19:53:33 +01002198 "\ No error if attempting to switch to curwin; nothing happens.
2199 \ .. ":call assert_equal(1, win_gotoid(win_getid()))\<CR>"
Sean Dewarf8658952024-02-20 22:05:10 +01002200 \ .. ":call assert_equal('command', win_gettype())\<CR>"
2201 \ .. ":call assert_equal('', win_gettype(winnr('#')))\<CR>", 'ntx')
2202endfunc
2203
Bram Moolenaar9e4d8212016-08-18 23:04:48 +02002204" vim: shiftwidth=2 sts=2 expandtab