blob: 61daa8d5c24377cf16ce9ab1af19da3554c5bfbb [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
22func Test_window_cmd_cmdwin_with_vsp()
Bram Moolenaar72cf47a2018-05-10 18:23:29 +020023 let efmt = 'Expected 0 but got %d (in ls=%d, %s window)'
Bram Moolenaar991dea32016-05-24 11:31:32 +020024 for v in range(0, 2)
25 exec "set ls=" . v
26 vsplit
27 call feedkeys("q:\<CR>")
28 let ac = &lines - (&cmdheight + winheight(0) + !!v)
29 let emsg = printf(efmt, ac, v, 'left')
30 call assert_equal(0, ac, emsg)
31 wincmd w
32 let ac = &lines - (&cmdheight + winheight(0) + !!v)
33 let emsg = printf(efmt, ac, v, 'right')
34 call assert_equal(0, ac, emsg)
35 new | only!
36 endfor
37 set ls&vim
38endfunc
39
Bram Moolenaar96bde992022-08-10 17:23:12 +010040func Test_cmdheight_not_changed()
41 set cmdheight=2
42 set winminheight=0
43 augroup Maximize
44 autocmd WinEnter * wincmd _
45 augroup END
46 split
47 tabnew
48 tabfirst
49 call assert_equal(2, &cmdheight)
50
51 tabonly!
52 only
53 set winminwidth& cmdheight&
54 augroup Maximize
55 au!
56 augroup END
57 augroup! Maximize
58endfunc
59
Bram Moolenaar5d3c9f82020-06-26 20:41:39 +020060" Test for jumping to windows
61func Test_window_jump()
62 new
63 " jumping to a window with a count greater than the max windows
64 exe "normal 4\<C-W>w"
65 call assert_equal(2, winnr())
66 only
67endfunc
68
69func Test_window_cmd_wincmd_gf()
Bram Moolenaar5d2ca042016-06-26 17:11:21 +020070 let fname = 'test_gf.txt'
71 let swp_fname = '.' . fname . '.swp'
72 call writefile([], fname)
73 call writefile([], swp_fname)
74 function s:swap_exists()
75 let v:swapchoice = s:swap_choice
76 endfunc
Bram Moolenaareaa49e42019-07-13 18:08:59 +020077 " Remove the catch-all that runtest.vim adds
78 au! SwapExists
Bram Moolenaar5d2ca042016-06-26 17:11:21 +020079 augroup test_window_cmd_wincmd_gf
80 autocmd!
81 exec "autocmd SwapExists " . fname . " call s:swap_exists()"
82 augroup END
83
84 call setline(1, fname)
85 " (E)dit anyway
86 let s:swap_choice = 'e'
87 wincmd gf
88 call assert_equal(2, tabpagenr())
89 call assert_equal(fname, bufname("%"))
90 quit!
91
92 " (Q)uit
93 let s:swap_choice = 'q'
94 wincmd gf
95 call assert_equal(1, tabpagenr())
96 call assert_notequal(fname, bufname("%"))
97 new | only!
98
99 call delete(fname)
100 call delete(swp_fname)
101 augroup! test_window_cmd_wincmd_gf
102endfunc
103
Bram Moolenaar4520d442017-03-19 16:09:46 +0100104func Test_window_quit()
105 e Xa
106 split Xb
Bram Moolenaarf92e58c2019-09-08 21:51:41 +0200107 call assert_equal(2, '$'->winnr())
Bram Moolenaar4520d442017-03-19 16:09:46 +0100108 call assert_equal('Xb', bufname(winbufnr(1)))
109 call assert_equal('Xa', bufname(winbufnr(2)))
110
111 wincmd q
112 call assert_equal(1, winnr('$'))
113 call assert_equal('Xa', bufname(winbufnr(1)))
114
115 bw Xa Xb
116endfunc
117
118func Test_window_horizontal_split()
119 call assert_equal(1, winnr('$'))
120 3wincmd s
121 call assert_equal(2, winnr('$'))
122 call assert_equal(3, winheight(0))
Bram Moolenaarf92e58c2019-09-08 21:51:41 +0200123 call assert_equal(winwidth(1), 2->winwidth())
Bram Moolenaar4520d442017-03-19 16:09:46 +0100124
125 call assert_fails('botright topleft wincmd s', 'E442:')
126 bw
127endfunc
128
129func Test_window_vertical_split()
130 call assert_equal(1, winnr('$'))
131 3wincmd v
132 call assert_equal(2, winnr('$'))
133 call assert_equal(3, winwidth(0))
134 call assert_equal(winheight(1), winheight(2))
135
136 call assert_fails('botright topleft wincmd v', 'E442:')
137 bw
138endfunc
139
Bram Moolenaar1bbb6192018-11-10 16:02:01 +0100140" Test the ":wincmd ^" and "<C-W>^" commands.
Bram Moolenaar4520d442017-03-19 16:09:46 +0100141func Test_window_split_edit_alternate()
Bram Moolenaar4520d442017-03-19 16:09:46 +0100142
Bram Moolenaar1bbb6192018-11-10 16:02:01 +0100143 " Test for failure when the alternate buffer/file no longer exists.
144 edit Xfoo | %bw
Bram Moolenaare2e40752020-09-04 21:18:46 +0200145 call assert_fails(':wincmd ^', 'E23:')
Bram Moolenaar1bbb6192018-11-10 16:02:01 +0100146
147 " Test for the expected behavior when we have two named buffers.
148 edit Xfoo | edit Xbar
Bram Moolenaar4520d442017-03-19 16:09:46 +0100149 wincmd ^
Bram Moolenaar1bbb6192018-11-10 16:02:01 +0100150 call assert_equal('Xfoo', bufname(winbufnr(1)))
151 call assert_equal('Xbar', bufname(winbufnr(2)))
152 only
Bram Moolenaar4520d442017-03-19 16:09:46 +0100153
Bram Moolenaar1bbb6192018-11-10 16:02:01 +0100154 " Test for the expected behavior when the alternate buffer is not named.
155 enew | let l:nr1 = bufnr('%')
156 edit Xfoo | let l:nr2 = bufnr('%')
157 wincmd ^
158 call assert_equal(l:nr1, winbufnr(1))
159 call assert_equal(l:nr2, winbufnr(2))
160 only
161
Bram Moolenaard42333d2018-11-10 20:28:19 +0100162 " FIXME: this currently fails on AppVeyor, but passes locally
163 if !has('win32')
164 " Test the Normal mode command.
165 call feedkeys("\<C-W>\<C-^>", 'tx')
166 call assert_equal(l:nr2, winbufnr(1))
167 call assert_equal(l:nr1, winbufnr(2))
168 endif
Bram Moolenaar1bbb6192018-11-10 16:02:01 +0100169
170 %bw!
171endfunc
172
173" Test the ":[count]wincmd ^" and "[count]<C-W>^" commands.
174func Test_window_split_edit_bufnr()
175
176 %bwipeout
177 let l:nr = bufnr('%') + 1
Bram Moolenaare2e40752020-09-04 21:18:46 +0200178 call assert_fails(':execute "normal! ' . l:nr . '\<C-W>\<C-^>"', 'E92:')
179 call assert_fails(':' . l:nr . 'wincmd ^', 'E16:')
180 call assert_fails(':0wincmd ^', 'E16:')
Bram Moolenaar1bbb6192018-11-10 16:02:01 +0100181
182 edit Xfoo | edit Xbar | edit Xbaz
183 let l:foo_nr = bufnr('Xfoo')
184 let l:bar_nr = bufnr('Xbar')
185 let l:baz_nr = bufnr('Xbaz')
186
Bram Moolenaar8617b402018-11-10 20:47:48 +0100187 " FIXME: this currently fails on AppVeyor, but passes locally
188 if !has('win32')
189 call feedkeys(l:foo_nr . "\<C-W>\<C-^>", 'tx')
190 call assert_equal('Xfoo', bufname(winbufnr(1)))
191 call assert_equal('Xbaz', bufname(winbufnr(2)))
192 only
Bram Moolenaar1bbb6192018-11-10 16:02:01 +0100193
Bram Moolenaar8617b402018-11-10 20:47:48 +0100194 call feedkeys(l:bar_nr . "\<C-W>\<C-^>", 'tx')
195 call assert_equal('Xbar', bufname(winbufnr(1)))
196 call assert_equal('Xfoo', bufname(winbufnr(2)))
197 only
Bram Moolenaar1bbb6192018-11-10 16:02:01 +0100198
Bram Moolenaar8617b402018-11-10 20:47:48 +0100199 execute l:baz_nr . 'wincmd ^'
200 call assert_equal('Xbaz', bufname(winbufnr(1)))
201 call assert_equal('Xbar', bufname(winbufnr(2)))
202 endif
Bram Moolenaar1bbb6192018-11-10 16:02:01 +0100203
204 %bw!
Bram Moolenaar4520d442017-03-19 16:09:46 +0100205endfunc
206
Bram Moolenaar9b9be002020-03-22 14:41:22 +0100207func Test_window_split_no_room()
208 " N horizontal windows need >= 2*N + 1 lines:
209 " - 1 line + 1 status line in each window
210 " - 1 Ex command line
211 "
212 " 2*N + 1 <= &lines
213 " N <= (lines - 1)/2
214 "
215 " Beyond that number of windows, E36: Not enough room is expected.
216 let hor_win_count = (&lines - 1)/2
217 let hor_split_count = hor_win_count - 1
218 for s in range(1, hor_split_count) | split | endfor
219 call assert_fails('split', 'E36:')
220
221 " N vertical windows need >= 2*(N - 1) + 1 columns:
222 " - 1 column + 1 separator for each window (except last window)
223 " - 1 column for the last window which does not have separator
224 "
225 " 2*(N - 1) + 1 <= &columns
226 " 2*N - 1 <= &columns
227 " N <= (&columns + 1)/2
228 let ver_win_count = (&columns + 1)/2
229 let ver_split_count = ver_win_count - 1
230 for s in range(1, ver_split_count) | vsplit | endfor
231 call assert_fails('vsplit', 'E36:')
232
233 %bw!
234endfunc
235
Bram Moolenaar4520d442017-03-19 16:09:46 +0100236func Test_window_exchange()
237 e Xa
238
239 " Nothing happens with window exchange when there is 1 window
240 wincmd x
241 call assert_equal(1, winnr('$'))
242
243 split Xb
244 split Xc
245
246 call assert_equal('Xc', bufname(winbufnr(1)))
247 call assert_equal('Xb', bufname(winbufnr(2)))
248 call assert_equal('Xa', bufname(winbufnr(3)))
249
250 " Exchange current window 1 with window 3
251 3wincmd x
252 call assert_equal('Xa', bufname(winbufnr(1)))
253 call assert_equal('Xb', bufname(winbufnr(2)))
254 call assert_equal('Xc', bufname(winbufnr(3)))
255
256 " Exchange window with next when at the top window
257 wincmd x
258 call assert_equal('Xb', bufname(winbufnr(1)))
259 call assert_equal('Xa', bufname(winbufnr(2)))
260 call assert_equal('Xc', bufname(winbufnr(3)))
261
262 " Exchange window with next when at the middle window
263 wincmd j
264 wincmd x
265 call assert_equal('Xb', bufname(winbufnr(1)))
266 call assert_equal('Xc', bufname(winbufnr(2)))
267 call assert_equal('Xa', bufname(winbufnr(3)))
268
269 " Exchange window with next when at the bottom window.
270 " When there is no next window, it exchanges with the previous window.
271 wincmd j
272 wincmd x
273 call assert_equal('Xb', bufname(winbufnr(1)))
274 call assert_equal('Xa', bufname(winbufnr(2)))
275 call assert_equal('Xc', bufname(winbufnr(3)))
276
277 bw Xa Xb Xc
278endfunc
279
280func Test_window_rotate()
281 e Xa
282 split Xb
283 split Xc
284 call assert_equal('Xc', bufname(winbufnr(1)))
285 call assert_equal('Xb', bufname(winbufnr(2)))
286 call assert_equal('Xa', bufname(winbufnr(3)))
287
288 " Rotate downwards
289 wincmd r
290 call assert_equal('Xa', bufname(winbufnr(1)))
291 call assert_equal('Xc', bufname(winbufnr(2)))
292 call assert_equal('Xb', bufname(winbufnr(3)))
293
294 2wincmd r
295 call assert_equal('Xc', bufname(winbufnr(1)))
296 call assert_equal('Xb', bufname(winbufnr(2)))
297 call assert_equal('Xa', bufname(winbufnr(3)))
298
299 " Rotate upwards
300 wincmd R
301 call assert_equal('Xb', bufname(winbufnr(1)))
302 call assert_equal('Xa', bufname(winbufnr(2)))
303 call assert_equal('Xc', bufname(winbufnr(3)))
304
305 2wincmd R
306 call assert_equal('Xc', bufname(winbufnr(1)))
307 call assert_equal('Xb', bufname(winbufnr(2)))
308 call assert_equal('Xa', bufname(winbufnr(3)))
309
310 bot vsplit
311 call assert_fails('wincmd R', 'E443:')
312
313 bw Xa Xb Xc
314endfunc
315
316func Test_window_height()
317 e Xa
318 split Xb
319
320 let [wh1, wh2] = [winheight(1), winheight(2)]
321 " Active window (1) should have the same height or 1 more
322 " than the other window.
323 call assert_inrange(wh2, wh2 + 1, wh1)
324
325 wincmd -
326 call assert_equal(wh1 - 1, winheight(1))
327 call assert_equal(wh2 + 1, winheight(2))
328
329 wincmd +
330 call assert_equal(wh1, winheight(1))
Bram Moolenaarf92e58c2019-09-08 21:51:41 +0200331 call assert_equal(wh2, 2->winheight())
Bram Moolenaar4520d442017-03-19 16:09:46 +0100332
333 2wincmd _
334 call assert_equal(2, winheight(1))
335 call assert_equal(wh1 + wh2 - 2, winheight(2))
336
337 wincmd =
338 call assert_equal(wh1, winheight(1))
339 call assert_equal(wh2, winheight(2))
340
341 2wincmd _
342 set winfixheight
343 split Xc
344 let [wh1, wh2, wh3] = [winheight(1), winheight(2), winheight(3)]
345 call assert_equal(2, winheight(2))
346 call assert_inrange(wh3, wh3 + 1, wh1)
347 3wincmd +
348 call assert_equal(2, winheight(2))
349 call assert_equal(wh1 + 3, winheight(1))
350 call assert_equal(wh3 - 3, winheight(3))
351 wincmd =
352 call assert_equal(2, winheight(2))
353 call assert_equal(wh1, winheight(1))
354 call assert_equal(wh3, winheight(3))
355
356 wincmd j
357 set winfixheight&
358
359 wincmd =
360 let [wh1, wh2, wh3] = [winheight(1), winheight(2), winheight(3)]
361 " Current window (2) should have the same height or 1 more
362 " than the other windows.
363 call assert_inrange(wh1, wh1 + 1, wh2)
364 call assert_inrange(wh3, wh3 + 1, wh2)
365
366 bw Xa Xb Xc
367endfunc
368
Bram Moolenaar21c3a802022-08-31 17:49:14 +0100369func Test_wincmd_equal()
370 edit Xone
371 below split Xtwo
372 rightbelow vsplit Xthree
373 call assert_equal('Xone', bufname(winbufnr(1)))
374 call assert_equal('Xtwo', bufname(winbufnr(2)))
375 call assert_equal('Xthree', bufname(winbufnr(3)))
376
377 " Xone and Xtwo should be about the same height
378 let [wh1, wh2] = [winheight(1), winheight(2)]
379 call assert_inrange(wh1 - 1, wh1 + 1, wh2)
380 " Xtwo and Xthree should be about the same width
381 let [ww2, ww3] = [winwidth(2), winwidth(3)]
382 call assert_inrange(ww2 - 1, ww2 + 1, ww3)
383
384 1wincmd w
385 10wincmd _
386 2wincmd w
387 20wincmd |
388 call assert_equal(10, winheight(1))
389 call assert_equal(20, winwidth(2))
390
391 " equalizing horizontally doesn't change the heights
392 hor wincmd =
393 call assert_equal(10, winheight(1))
394 let [ww2, ww3] = [winwidth(2), winwidth(3)]
395 call assert_inrange(ww2 - 1, ww2 + 1, ww3)
396
397 2wincmd w
398 20wincmd |
399 call assert_equal(20, winwidth(2))
400 " equalizing vertically doesn't change the widths
401 vert wincmd =
402 call assert_equal(20, winwidth(2))
403 let [wh1, wh2] = [winheight(1), winheight(2)]
404 call assert_inrange(wh1 - 1, wh1 + 1, wh2)
405
406 bwipe Xone Xtwo Xthree
407endfunc
408
Bram Moolenaar4520d442017-03-19 16:09:46 +0100409func Test_window_width()
410 e Xa
411 vsplit Xb
412
413 let [ww1, ww2] = [winwidth(1), winwidth(2)]
414 " Active window (1) should have the same width or 1 more
415 " than the other window.
416 call assert_inrange(ww2, ww2 + 1, ww1)
417
418 wincmd <
419 call assert_equal(ww1 - 1, winwidth(1))
420 call assert_equal(ww2 + 1, winwidth(2))
421
422 wincmd >
423 call assert_equal(ww1, winwidth(1))
424 call assert_equal(ww2, winwidth(2))
425
426 2wincmd |
427 call assert_equal(2, winwidth(1))
428 call assert_equal(ww1 + ww2 - 2, winwidth(2))
429
430 wincmd =
431 call assert_equal(ww1, winwidth(1))
432 call assert_equal(ww2, winwidth(2))
433
434 2wincmd |
435 set winfixwidth
436 vsplit Xc
437 let [ww1, ww2, ww3] = [winwidth(1), winwidth(2), winwidth(3)]
Bram Moolenaar38e34832017-03-19 20:22:36 +0100438 call assert_equal(2, winwidth(2))
Bram Moolenaar4520d442017-03-19 16:09:46 +0100439 call assert_inrange(ww3, ww3 + 1, ww1)
440 3wincmd >
Bram Moolenaar38e34832017-03-19 20:22:36 +0100441 call assert_equal(2, winwidth(2))
Bram Moolenaar4520d442017-03-19 16:09:46 +0100442 call assert_equal(ww1 + 3, winwidth(1))
443 call assert_equal(ww3 - 3, winwidth(3))
444 wincmd =
Bram Moolenaar38e34832017-03-19 20:22:36 +0100445 call assert_equal(2, winwidth(2))
Bram Moolenaar4520d442017-03-19 16:09:46 +0100446 call assert_equal(ww1, winwidth(1))
447 call assert_equal(ww3, winwidth(3))
448
449 wincmd l
450 set winfixwidth&
451
452 wincmd =
453 let [ww1, ww2, ww3] = [winwidth(1), winwidth(2), winwidth(3)]
454 " Current window (2) should have the same width or 1 more
455 " than the other windows.
456 call assert_inrange(ww1, ww1 + 1, ww2)
457 call assert_inrange(ww3, ww3 + 1, ww2)
458
Yegappan Lakshmanan59585492021-06-12 13:46:41 +0200459 " when the current window width is less than the new 'winwidth', the current
460 " window width should be increased.
461 enew | only
462 split
463 10vnew
464 set winwidth=15
465 call assert_equal(15, winwidth(0))
466
467 %bw!
Bram Moolenaar4520d442017-03-19 16:09:46 +0100468endfunc
469
Bram Moolenaar8eeeba82017-06-25 22:45:39 +0200470func Test_equalalways_on_close()
471 set equalalways
472 vsplit
473 windo split
Bram Moolenaar1bbb6192018-11-10 16:02:01 +0100474 split
Bram Moolenaar8eeeba82017-06-25 22:45:39 +0200475 wincmd J
476 " now we have a frame top-left with two windows, a frame top-right with two
477 " windows and a frame at the bottom, full-width.
478 let height_1 = winheight(1)
479 let height_2 = winheight(2)
480 let height_3 = winheight(3)
481 let height_4 = winheight(4)
482 " closing the bottom window causes all windows to be resized.
483 close
484 call assert_notequal(height_1, winheight(1))
485 call assert_notequal(height_2, winheight(2))
486 call assert_notequal(height_3, winheight(3))
487 call assert_notequal(height_4, winheight(4))
488 call assert_equal(winheight(1), winheight(3))
489 call assert_equal(winheight(2), winheight(4))
490
491 1wincmd w
492 split
493 4wincmd w
494 resize + 5
495 " left column has three windows, equalized heights.
496 " right column has two windows, top one a bit higher
497 let height_1 = winheight(1)
498 let height_2 = winheight(2)
499 let height_4 = winheight(4)
500 let height_5 = winheight(5)
501 3wincmd w
502 " closing window in left column equalizes heights in left column but not in
503 " the right column
504 close
505 call assert_notequal(height_1, winheight(1))
506 call assert_notequal(height_2, winheight(2))
507 call assert_equal(height_4, winheight(3))
508 call assert_equal(height_5, winheight(4))
509
510 only
511 set equalalways&
512endfunc
513
Bram Moolenaar22044dc2017-12-02 15:43:37 +0100514func Test_win_screenpos()
Bram Moolenaar5a4c3082019-12-01 15:23:11 +0100515 CheckFeature quickfix
516
Bram Moolenaar22044dc2017-12-02 15:43:37 +0100517 call assert_equal(1, winnr('$'))
518 split
519 vsplit
520 10wincmd _
521 30wincmd |
522 call assert_equal([1, 1], win_screenpos(1))
523 call assert_equal([1, 32], win_screenpos(2))
524 call assert_equal([12, 1], win_screenpos(3))
525 call assert_equal([0, 0], win_screenpos(4))
Bram Moolenaar99fa7212020-04-26 15:59:55 +0200526 call assert_fails('let l = win_screenpos([])', 'E745:')
Bram Moolenaar22044dc2017-12-02 15:43:37 +0100527 only
528endfunc
529
Bram Moolenaar4520d442017-03-19 16:09:46 +0100530func Test_window_jump_tag()
Bram Moolenaar5a4c3082019-12-01 15:23:11 +0100531 CheckFeature quickfix
532
Bram Moolenaar4520d442017-03-19 16:09:46 +0100533 help
534 /iccf
535 call assert_match('^|iccf|', getline('.'))
536 call assert_equal(2, winnr('$'))
537 2wincmd }
538 call assert_equal(3, winnr('$'))
539 call assert_match('^|iccf|', getline('.'))
540 wincmd k
541 call assert_match('\*iccf\*', getline('.'))
542 call assert_equal(2, winheight(0))
543
544 wincmd z
545 set previewheight=4
546 help
547 /bugs
548 wincmd }
549 wincmd k
550 call assert_match('\*bugs\*', getline('.'))
551 call assert_equal(4, winheight(0))
552 set previewheight&
553
554 %bw!
555endfunc
556
557func Test_window_newtab()
558 e Xa
559
560 call assert_equal(1, tabpagenr('$'))
561 call assert_equal("\nAlready only one window", execute('wincmd T'))
562
563 split Xb
564 split Xc
565
566 wincmd T
567 call assert_equal(2, tabpagenr('$'))
568 call assert_equal(['Xb', 'Xa'], map(tabpagebuflist(1), 'bufname(v:val)'))
Bram Moolenaarce90e362019-09-08 18:58:44 +0200569 call assert_equal(['Xc' ], map(2->tabpagebuflist(), 'bufname(v:val)'))
Bram Moolenaar0e05de42020-03-25 22:23:46 +0100570 call assert_equal(['Xc' ], map(tabpagebuflist(), 'bufname(v:val)'))
Bram Moolenaar4520d442017-03-19 16:09:46 +0100571
572 %bw!
573endfunc
574
Bram Moolenaarf79225e2017-03-18 23:11:04 +0100575func Test_next_split_all()
576 " This was causing an illegal memory access.
577 n x
578 norm axxx
579 split
580 split
581 s/x
582 s/x
583 all
584 bwipe!
585endfunc
586
Bram Moolenaar75373f32017-08-07 22:02:30 +0200587" Tests for adjusting window and contents
588func GetScreenStr(row)
589 let str = ""
590 for c in range(1,3)
591 let str .= nr2char(screenchar(a:row, c))
592 endfor
593 return str
594endfunc
595
596func Test_window_contents()
597 enew! | only | new
598 call setline(1, range(1,256))
599
600 exe "norm! \<C-W>t\<C-W>=1Gzt\<C-W>w\<C-W>+"
601 redraw
Bram Moolenaar72cf47a2018-05-10 18:23:29 +0200602 let s3 = GetScreenStr(1)
Bram Moolenaar75373f32017-08-07 22:02:30 +0200603 wincmd p
604 call assert_equal(1, line("w0"))
605 call assert_equal('1 ', s3)
606
607 exe "norm! \<C-W>t\<C-W>=50Gzt\<C-W>w\<C-W>+"
608 redraw
Bram Moolenaar72cf47a2018-05-10 18:23:29 +0200609 let s3 = GetScreenStr(1)
Bram Moolenaar75373f32017-08-07 22:02:30 +0200610 wincmd p
611 call assert_equal(50, line("w0"))
612 call assert_equal('50 ', s3)
613
614 exe "norm! \<C-W>t\<C-W>=59Gzt\<C-W>w\<C-W>+"
615 redraw
Bram Moolenaar72cf47a2018-05-10 18:23:29 +0200616 let s3 = GetScreenStr(1)
Bram Moolenaar75373f32017-08-07 22:02:30 +0200617 wincmd p
618 call assert_equal(59, line("w0"))
619 call assert_equal('59 ', s3)
620
Bram Moolenaar8b633132020-03-20 18:20:51 +0100621 %d
622 call setline(1, ['one', 'two', 'three'])
623 call assert_equal(1, line('w0'))
624 call assert_equal(3, line('w$'))
625
Bram Moolenaar75373f32017-08-07 22:02:30 +0200626 bwipeout!
627 call test_garbagecollect_now()
628endfunc
629
Bram Moolenaar2efb3232017-12-19 12:27:23 +0100630func Test_window_colon_command()
631 " This was reading invalid memory.
632 exe "norm! v\<C-W>:\<C-U>echo v:version"
633endfunc
634
Bram Moolenaar6f361c92018-01-31 19:06:50 +0100635func Test_access_freed_mem()
Bram Moolenaar1417c762019-07-27 17:31:36 +0200636 call assert_equal(&columns, winwidth(0))
Bram Moolenaar9a046fd2021-01-28 13:47:59 +0100637 " This was accessing freed memory (but with what events?)
638 au BufEnter,BufLeave,WinEnter,WinLeave 0 vs xxx
Bram Moolenaar6f361c92018-01-31 19:06:50 +0100639 arg 0
640 argadd
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +0200641 call assert_fails("all", "E242:")
Bram Moolenaar6f361c92018-01-31 19:06:50 +0100642 au!
643 bwipe xxx
Bram Moolenaar1417c762019-07-27 17:31:36 +0200644 call assert_equal(&columns, winwidth(0))
Bram Moolenaar6f361c92018-01-31 19:06:50 +0100645endfunc
646
Bram Moolenaara27e1dc2019-10-07 22:27:36 +0200647func Test_insert_cleared_on_switch_to_term()
648 CheckFeature terminal
649
650 set showmode
651 terminal
652 wincmd p
653
654 call feedkeys("i\<C-O>", 'ntx')
655 redraw
656
657 " The "-- (insert) --" indicator should be visible.
658 let chars = map(range(1, &columns), 'nr2char(screenchar(&lines, v:val))')
659 let str = trim(join(chars, ''))
660 call assert_equal('-- (insert) --', str)
661
662 call feedkeys("\<C-W>p", 'ntx')
663 redraw
664
665 " The "-- (insert) --" indicator should have been cleared.
666 let chars = map(range(1, &columns), 'nr2char(screenchar(&lines, v:val))')
667 let str = trim(join(chars, ''))
668 call assert_equal('', str)
669
670 set showmode&
671 %bw!
672endfunc
673
Bram Moolenaar5bab5552018-04-13 20:41:29 +0200674func Test_visual_cleared_after_window_split()
675 new | only!
676 let smd_save = &showmode
677 set showmode
678 let ls_save = &laststatus
679 set laststatus=1
680 call setline(1, ['a', 'b', 'c', 'd', ''])
681 norm! G
682 exe "norm! kkvk"
683 redraw
684 exe "norm! \<C-W>v"
685 redraw
686 " check if '-- VISUAL --' disappeared from command line
687 let columns = range(1, &columns)
688 let cmdlinechars = map(columns, 'nr2char(screenchar(&lines, v:val))')
689 let cmdline = join(cmdlinechars, '')
690 let cmdline_ltrim = substitute(cmdline, '^\s*', "", "")
691 let mode_shown = substitute(cmdline_ltrim, '\s*$', "", "")
692 call assert_equal('', mode_shown)
693 let &showmode = smd_save
694 let &laststatus = ls_save
695 bwipe!
696endfunc
697
Bram Moolenaar72cf47a2018-05-10 18:23:29 +0200698func Test_winrestcmd()
699 2split
700 3vsplit
Bram Moolenaara0c8aea2021-03-20 19:55:35 +0100701 let restcmd = winrestcmd()
Bram Moolenaar72cf47a2018-05-10 18:23:29 +0200702 call assert_equal(2, winheight(0))
703 call assert_equal(3, winwidth(0))
704 wincmd =
705 call assert_notequal(2, winheight(0))
706 call assert_notequal(3, winwidth(0))
Bram Moolenaara0c8aea2021-03-20 19:55:35 +0100707 exe restcmd
Bram Moolenaar72cf47a2018-05-10 18:23:29 +0200708 call assert_equal(2, winheight(0))
709 call assert_equal(3, winwidth(0))
710 only
Bram Moolenaara0c8aea2021-03-20 19:55:35 +0100711
712 wincmd v
713 wincmd s
714 wincmd v
715 redraw
716 let restcmd = winrestcmd()
717 wincmd _
718 wincmd |
719 exe restcmd
720 redraw
721 call assert_equal(restcmd, winrestcmd())
722
723 only
Bram Moolenaar72cf47a2018-05-10 18:23:29 +0200724endfunc
725
Bram Moolenaar1e115362019-01-09 23:01:02 +0100726func Fun_RenewFile()
Bram Moolenaar026587b2019-08-17 15:08:00 +0200727 " Need to wait a bit for the timestamp to be older.
Bram Moolenaarce90e362019-09-08 18:58:44 +0200728 let old_ftime = getftime("tmp.txt")
729 while getftime("tmp.txt") == old_ftime
730 sleep 100m
731 silent execute '!echo "1" > tmp.txt'
732 endwhile
Bram Moolenaara42df592018-12-24 00:22:39 +0100733 sp
734 wincmd p
735 edit! tmp.txt
Bram Moolenaar1e115362019-01-09 23:01:02 +0100736endfunc
Bram Moolenaara42df592018-12-24 00:22:39 +0100737
738func Test_window_prevwin()
739 " Can we make this work on MS-Windows?
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +0200740 CheckUnix
Bram Moolenaara42df592018-12-24 00:22:39 +0100741
742 set hidden autoread
743 call writefile(['2'], 'tmp.txt')
744 new tmp.txt
745 q
Bram Moolenaara42df592018-12-24 00:22:39 +0100746 call Fun_RenewFile()
747 call assert_equal(2, winnr())
748 wincmd p
749 call assert_equal(1, winnr())
750 wincmd p
751 q
752 call Fun_RenewFile()
753 call assert_equal(2, winnr())
754 wincmd p
755 call assert_equal(1, winnr())
756 wincmd p
757 " reset
758 q
759 call delete('tmp.txt')
760 set hidden&vim autoread&vim
761 delfunc Fun_RenewFile
762endfunc
763
Bram Moolenaar8fcb60f2019-03-04 13:18:30 +0100764func Test_relative_cursor_position_in_one_line_window()
765 new
766 only
767 call setline(1, range(1, 10000))
768 normal 50%
769 let lnum = getcurpos()[1]
770 split
771 split
772 " make third window take as many lines as possible, other windows will
773 " become one line
774 3wincmd w
775 for i in range(1, &lines - 6)
776 wincmd +
777 redraw!
778 endfor
779
780 " first and second window should show cursor line
781 let wininfo = getwininfo()
782 call assert_equal(lnum, wininfo[0].topline)
783 call assert_equal(lnum, wininfo[1].topline)
784
785 only!
786 bwipe!
Yegappan Lakshmanan04c4c572022-08-30 19:48:24 +0100787 call assert_fails('call winrestview(test_null_dict())', 'E1297:')
Bram Moolenaar8fcb60f2019-03-04 13:18:30 +0100788endfunc
789
790func Test_relative_cursor_position_after_move_and_resize()
791 let so_save = &so
792 set so=0
793 enew
794 call setline(1, range(1, 10000))
795 normal 50%
796 split
797 1wincmd w
798 " Move cursor to first line in window
799 normal H
800 redraw!
801 " Reduce window height to two lines
802 let height = winheight(0)
803 while winheight(0) > 2
804 wincmd -
805 redraw!
806 endwhile
807 " move cursor to second/last line in window
808 normal j
809 " restore previous height
810 while winheight(0) < height
811 wincmd +
812 redraw!
813 endwhile
814 " make window two lines again
815 while winheight(0) > 2
816 wincmd -
817 redraw!
818 endwhile
819
820 " cursor should be at bottom line
821 let info = getwininfo(win_getid())[0]
822 call assert_equal(info.topline + 1, getcurpos()[1])
823
824 only!
825 bwipe!
826 let &so = so_save
827endfunc
828
829func Test_relative_cursor_position_after_resize()
830 let so_save = &so
831 set so=0
832 enew
833 call setline(1, range(1, 10000))
834 normal 50%
835 split
836 1wincmd w
837 let winid1 = win_getid()
838 let info = getwininfo(winid1)[0]
839 " Move cursor to second line in window
840 exe "normal " . (info.topline + 1) . "G"
841 redraw!
842 let lnum = getcurpos()[1]
843
844 " Make the window only two lines high, cursor should end up in top line
845 2wincmd w
846 exe (info.height - 2) . "wincmd +"
847 redraw!
848 let info = getwininfo(winid1)[0]
849 call assert_equal(lnum, info.topline)
850
851 only!
852 bwipe!
853 let &so = so_save
854endfunc
855
856func Test_relative_cursor_second_line_after_resize()
857 let so_save = &so
858 set so=0
859 enew
860 call setline(1, range(1, 10000))
861 normal 50%
862 split
863 1wincmd w
864 let winid1 = win_getid()
865 let info = getwininfo(winid1)[0]
866
867 " Make the window only two lines high
868 2wincmd _
869
870 " Move cursor to second line in window
871 normal H
872 normal j
873
874 " Make window size bigger, then back to 2 lines
875 for i in range(1, 10)
876 wincmd +
877 redraw!
878 endfor
879 for i in range(1, 10)
880 wincmd -
881 redraw!
882 endfor
883
884 " cursor should end up in bottom line
885 let info = getwininfo(winid1)[0]
886 call assert_equal(info.topline + 1, getcurpos()[1])
887
888 only!
889 bwipe!
890 let &so = so_save
891endfunc
892
Bram Moolenaara9b25352019-05-12 14:25:30 +0200893func Test_split_noscroll()
894 let so_save = &so
Bram Moolenaarbd2d68c2019-05-18 15:36:11 +0200895 enew
896 call setline(1, range(1, 8))
Bram Moolenaara9b25352019-05-12 14:25:30 +0200897 normal 100%
898 split
899
900 1wincmd w
901 let winid1 = win_getid()
902 let info1 = getwininfo(winid1)[0]
903
904 2wincmd w
905 let winid2 = win_getid()
906 let info2 = getwininfo(winid2)[0]
907
908 call assert_equal(1, info1.topline)
909 call assert_equal(1, info2.topline)
910
Bram Moolenaarbd2d68c2019-05-18 15:36:11 +0200911 " window that fits all lines by itself, but not when split: closing other
912 " window should restore fraction.
Bram Moolenaara9b25352019-05-12 14:25:30 +0200913 only!
Bram Moolenaarbd2d68c2019-05-18 15:36:11 +0200914 call setline(1, range(1, &lines - 10))
915 exe &lines / 4
916 let winid1 = win_getid()
917 let info1 = getwininfo(winid1)[0]
918 call assert_equal(1, info1.topline)
919 new
920 redraw
921 close
922 let info1 = getwininfo(winid1)[0]
923 call assert_equal(1, info1.topline)
924
Bram Moolenaara9b25352019-05-12 14:25:30 +0200925 bwipe!
926 let &so = so_save
927endfunc
928
Bram Moolenaar46ad2882019-04-08 20:01:47 +0200929" Tests for the winnr() function
930func Test_winnr()
931 only | tabonly
932 call assert_equal(1, winnr('j'))
933 call assert_equal(1, winnr('k'))
934 call assert_equal(1, winnr('h'))
935 call assert_equal(1, winnr('l'))
936
937 " create a set of horizontally and vertically split windows
938 leftabove new | wincmd p
939 leftabove new | wincmd p
940 rightbelow new | wincmd p
941 rightbelow new | wincmd p
942 leftabove vnew | wincmd p
943 leftabove vnew | wincmd p
944 rightbelow vnew | wincmd p
945 rightbelow vnew | wincmd p
946
947 call assert_equal(8, winnr('j'))
948 call assert_equal(2, winnr('k'))
949 call assert_equal(4, winnr('h'))
950 call assert_equal(6, winnr('l'))
951 call assert_equal(9, winnr('2j'))
952 call assert_equal(1, winnr('2k'))
953 call assert_equal(3, winnr('2h'))
954 call assert_equal(7, winnr('2l'))
955
956 " Error cases
957 call assert_fails("echo winnr('0.2k')", 'E15:')
958 call assert_equal(2, winnr('-2k'))
959 call assert_fails("echo winnr('-2xj')", 'E15:')
960 call assert_fails("echo winnr('j2j')", 'E15:')
961 call assert_fails("echo winnr('ll')", 'E15:')
962 call assert_fails("echo winnr('5')", 'E15:')
963 call assert_equal(4, winnr('0h'))
Bram Moolenaar99fa7212020-04-26 15:59:55 +0200964 call assert_fails("let w = winnr([])", 'E730:')
965 call assert_equal('unknown', win_gettype(-1))
966 call assert_equal(-1, winheight(-1))
967 call assert_equal(-1, winwidth(-1))
Bram Moolenaar46ad2882019-04-08 20:01:47 +0200968
969 tabnew
970 call assert_equal(8, tabpagewinnr(1, 'j'))
Bram Moolenaarce90e362019-09-08 18:58:44 +0200971 call assert_equal(2, 1->tabpagewinnr('k'))
Bram Moolenaar46ad2882019-04-08 20:01:47 +0200972 call assert_equal(4, tabpagewinnr(1, 'h'))
973 call assert_equal(6, tabpagewinnr(1, 'l'))
974
975 only | tabonly
976endfunc
977
Bram Moolenaarf92e58c2019-09-08 21:51:41 +0200978func Test_winrestview()
979 split runtest.vim
980 normal 50%
981 let view = winsaveview()
982 close
983 split runtest.vim
984 eval view->winrestview()
985 call assert_equal(view, winsaveview())
986
987 bwipe!
Yegappan Lakshmanan04c4c572022-08-30 19:48:24 +0100988 call assert_fails('call winrestview(test_null_dict())', 'E1297:')
Bram Moolenaarf92e58c2019-09-08 21:51:41 +0200989endfunc
990
Bram Moolenaard20dcb32019-09-10 21:22:58 +0200991func Test_win_splitmove()
Bram Moolenaar5a4c3082019-12-01 15:23:11 +0100992 CheckFeature quickfix
993
Bram Moolenaard20dcb32019-09-10 21:22:58 +0200994 edit a
995 leftabove split b
996 leftabove vsplit c
997 leftabove split d
998 call assert_equal(0, win_splitmove(winnr(), winnr('l')))
999 call assert_equal(bufname(winbufnr(1)), 'c')
1000 call assert_equal(bufname(winbufnr(2)), 'd')
1001 call assert_equal(bufname(winbufnr(3)), 'b')
1002 call assert_equal(bufname(winbufnr(4)), 'a')
1003 call assert_equal(0, win_splitmove(winnr(), winnr('j'), {'vertical': 1}))
1004 call assert_equal(0, win_splitmove(winnr(), winnr('j'), {'vertical': 1}))
1005 call assert_equal(bufname(winbufnr(1)), 'c')
1006 call assert_equal(bufname(winbufnr(2)), 'b')
1007 call assert_equal(bufname(winbufnr(3)), 'd')
1008 call assert_equal(bufname(winbufnr(4)), 'a')
1009 call assert_equal(0, win_splitmove(winnr(), winnr('k'), {'vertical': 1}))
1010 call assert_equal(bufname(winbufnr(1)), 'd')
1011 call assert_equal(bufname(winbufnr(2)), 'c')
1012 call assert_equal(bufname(winbufnr(3)), 'b')
1013 call assert_equal(bufname(winbufnr(4)), 'a')
1014 call assert_equal(0, win_splitmove(winnr(), winnr('j'), {'rightbelow': v:true}))
1015 call assert_equal(bufname(winbufnr(1)), 'c')
1016 call assert_equal(bufname(winbufnr(2)), 'b')
1017 call assert_equal(bufname(winbufnr(3)), 'a')
1018 call assert_equal(bufname(winbufnr(4)), 'd')
Yegappan Lakshmanan04c4c572022-08-30 19:48:24 +01001019 call assert_fails('call win_splitmove(winnr(), winnr("k"), test_null_dict())', 'E1297:')
Bram Moolenaard20dcb32019-09-10 21:22:58 +02001020 only | bd
1021
1022 call assert_fails('call win_splitmove(winnr(), 123)', 'E957:')
1023 call assert_fails('call win_splitmove(123, winnr())', 'E957:')
1024 call assert_fails('call win_splitmove(winnr(), winnr())', 'E957:')
Bram Moolenaar7b94e772020-01-06 21:03:24 +01001025
1026 tabnew
1027 call assert_fails('call win_splitmove(1, win_getid(1, 1))', 'E957:')
1028 tabclose
Bram Moolenaard20dcb32019-09-10 21:22:58 +02001029endfunc
1030
Bram Moolenaare20b9ec2020-02-03 21:40:04 +01001031" Test for the :only command
1032func Test_window_only()
1033 new
1034 set modified
1035 new
1036 call assert_fails('only', 'E445:')
1037 only!
Bram Moolenaarbc2b71d2020-02-17 21:33:30 +01001038 " Test for :only with a count
1039 let wid = win_getid()
1040 new
1041 new
1042 3only
1043 call assert_equal(1, winnr('$'))
1044 call assert_equal(wid, win_getid())
1045 call assert_fails('close', 'E444:')
1046 call assert_fails('%close', 'E16:')
Bram Moolenaare20b9ec2020-02-03 21:40:04 +01001047endfunc
1048
Bram Moolenaar9f6277b2020-02-11 22:04:02 +01001049" Test for errors with :wincmd
1050func Test_wincmd_errors()
1051 call assert_fails('wincmd g', 'E474:')
1052 call assert_fails('wincmd ab', 'E474:')
1053endfunc
1054
1055" Test for errors with :winpos
1056func Test_winpos_errors()
1057 if !has("gui_running") && !has('win32')
1058 call assert_fails('winpos', 'E188:')
1059 endif
1060 call assert_fails('winpos 10', 'E466:')
1061endfunc
1062
Bram Moolenaar406cd902020-02-18 21:54:41 +01001063" Test for +cmd in a :split command
1064func Test_split_cmd()
1065 split +set\ readonly
1066 call assert_equal(1, &readonly)
1067 call assert_equal(2, winnr('$'))
1068 close
1069endfunc
1070
Bram Moolenaar5080b0a2020-03-22 21:23:47 +01001071" Create maximum number of horizontally or vertically split windows and then
1072" run commands that create a new horizontally/vertically split window
1073func Run_noroom_for_newwindow_test(dir_arg)
1074 let dir = (a:dir_arg == 'v') ? 'vert ' : ''
1075
1076 " Open as many windows as possible
Bram Moolenaarab505b12020-03-23 19:28:44 +01001077 while v:true
Bram Moolenaar5080b0a2020-03-22 21:23:47 +01001078 try
1079 exe dir . 'new'
1080 catch /E36:/
1081 break
1082 endtry
Bram Moolenaarab505b12020-03-23 19:28:44 +01001083 endwhile
Bram Moolenaar5080b0a2020-03-22 21:23:47 +01001084
Bram Moolenaarb18b4962022-09-02 21:55:50 +01001085 call writefile(['first', 'second', 'third'], 'Xnorfile1')
1086 call writefile([], 'Xnorfile2')
1087 call writefile([], 'Xnorfile3')
Bram Moolenaar5080b0a2020-03-22 21:23:47 +01001088
1089 " Argument list related commands
Bram Moolenaarb18b4962022-09-02 21:55:50 +01001090 args Xnorfile1 Xnorfile2 Xnorfile3
Bram Moolenaar5080b0a2020-03-22 21:23:47 +01001091 next
1092 for cmd in ['sargument 2', 'snext', 'sprevious', 'sNext', 'srewind',
1093 \ 'sfirst', 'slast']
1094 call assert_fails(dir .. cmd, 'E36:')
1095 endfor
1096 %argdelete
1097
1098 " Buffer related commands
1099 set modified
1100 hide enew
Bram Moolenaarb18b4962022-09-02 21:55:50 +01001101 for cmd in ['sbuffer Xnorfile1', 'sbnext', 'sbprevious', 'sbNext', 'sbrewind',
Bram Moolenaar5080b0a2020-03-22 21:23:47 +01001102 \ 'sbfirst', 'sblast', 'sball', 'sbmodified', 'sunhide']
1103 call assert_fails(dir .. cmd, 'E36:')
1104 endfor
1105
1106 " Window related commands
Bram Moolenaarb18b4962022-09-02 21:55:50 +01001107 for cmd in ['split', 'split Xnorfile2', 'new', 'new Xnorfile3', 'sview Xnorfile1',
Bram Moolenaar5080b0a2020-03-22 21:23:47 +01001108 \ 'sfind runtest.vim']
1109 call assert_fails(dir .. cmd, 'E36:')
1110 endfor
1111
1112 " Help
1113 call assert_fails(dir .. 'help', 'E36:')
1114 call assert_fails(dir .. 'helpgrep window', 'E36:')
1115
1116 " Command-line window
1117 if a:dir_arg == 'h'
1118 " Cmd-line window is always a horizontally split window
1119 call assert_beeps('call feedkeys("q:\<CR>", "xt")')
1120 endif
1121
1122 " Quickfix and location list window
1123 if has('quickfix')
1124 cexpr ''
1125 call assert_fails(dir .. 'copen', 'E36:')
1126 lexpr ''
1127 call assert_fails(dir .. 'lopen', 'E36:')
1128
1129 " Preview window
Bram Moolenaarb18b4962022-09-02 21:55:50 +01001130 call assert_fails(dir .. 'pedit Xnorfile2', 'E36:')
Bram Moolenaar5080b0a2020-03-22 21:23:47 +01001131 call setline(1, 'abc')
1132 call assert_fails(dir .. 'psearch abc', 'E36:')
1133 endif
1134
1135 " Window commands (CTRL-W ^ and CTRL-W f)
1136 if a:dir_arg == 'h'
1137 call assert_fails('call feedkeys("\<C-W>^", "xt")', 'E36:')
Bram Moolenaarb18b4962022-09-02 21:55:50 +01001138 call setline(1, 'Xnorfile1')
Bram Moolenaar5080b0a2020-03-22 21:23:47 +01001139 call assert_fails('call feedkeys("gg\<C-W>f", "xt")', 'E36:')
1140 endif
1141 enew!
1142
1143 " Tag commands (:stag, :stselect and :stjump)
1144 call writefile(["!_TAG_FILE_ENCODING\tutf-8\t//",
Bram Moolenaarb18b4962022-09-02 21:55:50 +01001145 \ "second\tXnorfile1\t2",
1146 \ "third\tXnorfile1\t3",],
Bram Moolenaar5080b0a2020-03-22 21:23:47 +01001147 \ 'Xtags')
1148 set tags=Xtags
1149 call assert_fails(dir .. 'stag second', 'E36:')
1150 call assert_fails('call feedkeys(":" .. dir .. "stselect second\n1\n", "xt")', 'E36:')
1151 call assert_fails(dir .. 'stjump second', 'E36:')
1152 call assert_fails(dir .. 'ptag second', 'E36:')
1153 set tags&
1154 call delete('Xtags')
1155
1156 " :isplit and :dsplit
1157 call setline(1, ['#define FOO 1', 'FOO'])
1158 normal 2G
1159 call assert_fails(dir .. 'isplit FOO', 'E36:')
1160 call assert_fails(dir .. 'dsplit FOO', 'E36:')
1161
1162 " terminal
1163 if has('terminal')
1164 call assert_fails(dir .. 'terminal', 'E36:')
1165 endif
1166
1167 %bwipe!
Bram Moolenaarb18b4962022-09-02 21:55:50 +01001168 call delete('Xnorfile1')
1169 call delete('Xnorfile2')
1170 call delete('Xnorfile3')
Bram Moolenaar5080b0a2020-03-22 21:23:47 +01001171 only
1172endfunc
1173
1174func Test_split_cmds_with_no_room()
1175 call Run_noroom_for_newwindow_test('h')
1176 call Run_noroom_for_newwindow_test('v')
1177endfunc
1178
Bram Moolenaarad48e6c2020-04-21 22:19:45 +02001179" Test for various wincmd failures
1180func Test_wincmd_fails()
1181 only!
1182 call assert_beeps("normal \<C-W>w")
1183 call assert_beeps("normal \<C-W>p")
1184 call assert_beeps("normal \<C-W>gk")
1185 call assert_beeps("normal \<C-W>r")
1186 call assert_beeps("normal \<C-W>K")
1187 call assert_beeps("normal \<C-W>H")
1188 call assert_beeps("normal \<C-W>2gt")
1189endfunc
1190
Bram Moolenaarfe6dce82020-09-04 14:41:21 +02001191func Test_window_resize()
1192 " Vertical :resize (absolute, relative, min and max size).
1193 vsplit
1194 vert resize 8
1195 call assert_equal(8, winwidth(0))
1196 vert resize +2
1197 call assert_equal(10, winwidth(0))
1198 vert resize -2
1199 call assert_equal(8, winwidth(0))
1200 vert resize
1201 call assert_equal(&columns - 2, winwidth(0))
1202 vert resize 0
1203 call assert_equal(1, winwidth(0))
1204 vert resize 99999
1205 call assert_equal(&columns - 2, winwidth(0))
1206
1207 %bwipe!
1208
1209 " Horizontal :resize (with absolute, relative size, min and max size).
1210 split
1211 resize 8
1212 call assert_equal(8, winheight(0))
1213 resize +2
1214 call assert_equal(10, winheight(0))
1215 resize -2
1216 call assert_equal(8, winheight(0))
1217 resize
1218 call assert_equal(&lines - 4, winheight(0))
1219 resize 0
1220 call assert_equal(1, winheight(0))
1221 resize 99999
1222 call assert_equal(&lines - 4, winheight(0))
1223
1224 " :resize with explicit window number.
1225 let other_winnr = winnr('j')
1226 exe other_winnr .. 'resize 10'
1227 call assert_equal(10, winheight(other_winnr))
1228 call assert_equal(&lines - 10 - 3, winheight(0))
Bram Moolenaar9668cc52020-10-17 17:39:55 +02001229 exe other_winnr .. 'resize +1'
1230 exe other_winnr .. 'resize +1'
1231 call assert_equal(12, winheight(other_winnr))
1232 call assert_equal(&lines - 10 - 3 -2, winheight(0))
Bram Moolenaar89015a62020-12-29 12:46:51 +01001233 close
1234
1235 vsplit
1236 wincmd l
1237 let other_winnr = winnr('h')
1238 call assert_notequal(winnr(), other_winnr)
Bram Moolenaar5efe0e52021-01-01 14:31:34 +01001239 exe 'vert ' .. other_winnr .. 'resize -' .. &columns
Bram Moolenaar89015a62020-12-29 12:46:51 +01001240 call assert_equal(0, winwidth(other_winnr))
Bram Moolenaarfe6dce82020-09-04 14:41:21 +02001241
1242 %bwipe!
1243endfunc
1244
Bram Moolenaar5d3c9f82020-06-26 20:41:39 +02001245" Test for adjusting the window width when a window is closed with some
1246" windows using 'winfixwidth'
1247func Test_window_width_adjust()
1248 only
1249 " Three vertical windows. Windows 1 and 2 have 'winfixwidth' set and close
1250 " window 2.
1251 wincmd v
1252 vert resize 10
1253 set winfixwidth
1254 wincmd v
1255 set winfixwidth
1256 wincmd c
1257 call assert_inrange(10, 12, winwidth(1))
1258 " Three vertical windows. Windows 2 and 3 have 'winfixwidth' set and close
1259 " window 3.
1260 only
1261 set winfixwidth
1262 wincmd v
1263 vert resize 10
1264 set winfixwidth
1265 wincmd v
1266 set nowinfixwidth
1267 wincmd b
1268 wincmd c
1269 call assert_inrange(10, 12, winwidth(2))
1270
1271 new | only
1272endfunc
1273
1274" Test for jumping to a vertical/horizontal neighbor window based on the
1275" current cursor position
1276func Test_window_goto_neightbor()
1277 %bw!
1278
1279 " Vertical window movement
1280
1281 " create the following window layout:
1282 " +--+--+
1283 " |w1|w3|
1284 " +--+ |
1285 " |w2| |
1286 " +--+--+
1287 " |w4 |
1288 " +-----+
1289 new
1290 vsplit
1291 split
1292 " vertically jump from w4
1293 wincmd b
1294 call setline(1, repeat(' ', &columns))
1295 call cursor(1, 1)
1296 wincmd k
1297 call assert_equal(2, winnr())
1298 wincmd b
1299 call cursor(1, &columns)
1300 redraw!
1301 wincmd k
1302 call assert_equal(3, winnr())
1303 %bw!
1304
1305 " create the following window layout:
1306 " +--+--+--+
1307 " |w1|w2|w3|
1308 " +--+--+--+
1309 " |w4 |
1310 " +--------+
1311 new
1312 vsplit
1313 vsplit
1314 wincmd b
1315 call setline(1, repeat(' ', &columns))
1316 call cursor(1, 1)
1317 wincmd k
1318 call assert_equal(1, winnr())
1319 wincmd b
1320 call cursor(1, &columns / 2)
1321 redraw!
1322 wincmd k
1323 call assert_equal(2, winnr())
1324 wincmd b
1325 call cursor(1, &columns)
1326 redraw!
1327 wincmd k
1328 call assert_equal(3, winnr())
1329 %bw!
1330
1331 " Horizontal window movement
1332
1333 " create the following window layout:
1334 " +--+--+--+
1335 " |w1|w2|w4|
1336 " +--+--+ |
1337 " |w3 | |
1338 " +-----+--+
1339 vsplit
1340 split
1341 vsplit
1342 4wincmd l
1343 call setline(1, repeat([' '], &lines))
1344 call cursor(1, 1)
1345 redraw!
1346 wincmd h
1347 call assert_equal(2, winnr())
1348 4wincmd l
1349 call cursor(&lines, 1)
1350 redraw!
1351 wincmd h
1352 call assert_equal(3, winnr())
1353 %bw!
1354
1355 " create the following window layout:
1356 " +--+--+
1357 " |w1|w4|
1358 " +--+ +
1359 " |w2| |
1360 " +--+ +
1361 " |w3| |
1362 " +--+--+
1363 vsplit
1364 split
1365 split
1366 wincmd l
1367 call setline(1, repeat([' '], &lines))
1368 call cursor(1, 1)
1369 redraw!
1370 wincmd h
1371 call assert_equal(1, winnr())
1372 wincmd l
1373 call cursor(&lines / 2, 1)
1374 redraw!
1375 wincmd h
1376 call assert_equal(2, winnr())
1377 wincmd l
1378 call cursor(&lines, 1)
1379 redraw!
1380 wincmd h
1381 call assert_equal(3, winnr())
1382 %bw!
1383endfunc
1384
1385" Test for an autocmd closing the destination window when jumping from one
1386" window to another.
1387func Test_close_dest_window()
1388 split
Bram Moolenaarb18b4962022-09-02 21:55:50 +01001389 edit Xdstfile
Bram Moolenaar5d3c9f82020-06-26 20:41:39 +02001390
1391 " Test for BufLeave
1392 augroup T1
1393 au!
Bram Moolenaarb18b4962022-09-02 21:55:50 +01001394 au BufLeave Xdstfile $wincmd c
Bram Moolenaar5d3c9f82020-06-26 20:41:39 +02001395 augroup END
1396 wincmd b
1397 call assert_equal(1, winnr('$'))
Bram Moolenaarb18b4962022-09-02 21:55:50 +01001398 call assert_equal('Xdstfile', @%)
Bram Moolenaar5d3c9f82020-06-26 20:41:39 +02001399 augroup T1
1400 au!
1401 augroup END
1402
1403 " Test for WinLeave
1404 new
1405 wincmd p
1406 augroup T1
1407 au!
1408 au WinLeave * 1wincmd c
1409 augroup END
1410 wincmd t
1411 call assert_equal(1, winnr('$'))
Bram Moolenaarb18b4962022-09-02 21:55:50 +01001412 call assert_equal('Xdstfile', @%)
Bram Moolenaar5d3c9f82020-06-26 20:41:39 +02001413 augroup T1
1414 au!
1415 augroup END
1416 augroup! T1
1417 %bw!
1418endfunc
1419
Bram Moolenaard0fb9072021-12-09 11:57:22 +00001420func Test_window_minimal_size()
1421 set winminwidth=0 winminheight=0
1422
1423 " check size is fixed vertically
1424 new
1425 call win_execute(win_getid(2), 'wincmd _')
1426 call assert_equal(0, winheight(0))
1427 call feedkeys('0', 'tx')
1428 call assert_equal(1, winheight(0))
1429 bwipe!
1430
1431 " check size is fixed horizontally
1432 vert new
1433 call win_execute(win_getid(2), 'wincmd |')
1434 call assert_equal(0, winwidth(0))
1435 call feedkeys('0', 'tx')
1436 call assert_equal(1, winwidth(0))
1437 bwipe!
1438
1439 if has('timers')
1440 " check size is fixed in Insert mode
ichizokfa9a8e02021-12-12 16:42:09 +00001441 func s:CheckSize(timer) abort
1442 call win_execute(win_getid(2), 'wincmd _')
1443 call assert_equal(0, winheight(0))
1444 call feedkeys(" \<Esc>", 't!')
1445 endfunc
Bram Moolenaard0fb9072021-12-09 11:57:22 +00001446 new
ichizokfa9a8e02021-12-12 16:42:09 +00001447 call timer_start(100, function('s:CheckSize'))
Bram Moolenaard0fb9072021-12-09 11:57:22 +00001448 call feedkeys('a', 'tx!')
1449 call assert_equal(1, winheight(0))
1450 bwipe!
ichizokfa9a8e02021-12-12 16:42:09 +00001451 delfunc s:CheckSize
Bram Moolenaard0fb9072021-12-09 11:57:22 +00001452 endif
1453
1454 set winminwidth& winminheight&
1455endfunc
1456
Daniel Steinbergee630312022-01-10 13:36:34 +00001457func Test_win_move_separator()
1458 edit a
1459 leftabove vsplit b
1460 let w = winwidth(0)
1461 " check win_move_separator from left window on left window
1462 call assert_equal(1, winnr())
1463 for offset in range(5)
1464 call assert_true(win_move_separator(0, offset))
1465 call assert_equal(w + offset, winwidth(0))
1466 call assert_true(0->win_move_separator(-offset))
1467 call assert_equal(w, winwidth(0))
1468 endfor
1469 " check win_move_separator from right window on left window number
1470 wincmd l
1471 call assert_notequal(1, winnr())
1472 for offset in range(5)
1473 call assert_true(1->win_move_separator(offset))
1474 call assert_equal(w + offset, winwidth(1))
1475 call assert_true(win_move_separator(1, -offset))
1476 call assert_equal(w, winwidth(1))
1477 endfor
1478 " check win_move_separator from right window on left window ID
1479 let id = win_getid(1)
1480 for offset in range(5)
1481 call assert_true(win_move_separator(id, offset))
1482 call assert_equal(w + offset, winwidth(id))
1483 call assert_true(id->win_move_separator(-offset))
1484 call assert_equal(w, winwidth(id))
1485 endfor
zeertzjqa0c4e2f2022-01-29 10:59:53 +00001486 " check win_move_separator from right window on right window is no-op
1487 let w0 = winwidth(0)
1488 call assert_true(win_move_separator(0, 1))
1489 call assert_equal(w0, winwidth(0))
1490 call assert_true(win_move_separator(0, -1))
1491 call assert_equal(w0, winwidth(0))
Daniel Steinbergee630312022-01-10 13:36:34 +00001492 " check that win_move_separator doesn't error with offsets beyond moving
1493 " possibility
1494 call assert_true(win_move_separator(id, 5000))
1495 call assert_true(winwidth(id) > w)
1496 call assert_true(win_move_separator(id, -5000))
1497 call assert_true(winwidth(id) < w)
1498 " check that win_move_separator returns false for an invalid window
1499 wincmd =
1500 let w = winwidth(0)
1501 call assert_false(win_move_separator(-1, 1))
1502 call assert_equal(w, winwidth(0))
1503 " check that win_move_separator returns false for a popup window
1504 let id = popup_create(['hello', 'world'], {})
1505 let w = winwidth(id)
1506 call assert_false(win_move_separator(id, 1))
1507 call assert_equal(w, winwidth(id))
1508 call popup_close(id)
1509 %bwipe!
1510endfunc
1511
1512func Test_win_move_statusline()
1513 edit a
1514 leftabove split b
1515 let h = winheight(0)
1516 " check win_move_statusline from top window on top window
1517 call assert_equal(1, winnr())
1518 for offset in range(5)
1519 call assert_true(win_move_statusline(0, offset))
1520 call assert_equal(h + offset, winheight(0))
1521 call assert_true(0->win_move_statusline(-offset))
1522 call assert_equal(h, winheight(0))
1523 endfor
1524 " check win_move_statusline from bottom window on top window number
1525 wincmd j
1526 call assert_notequal(1, winnr())
1527 for offset in range(5)
1528 call assert_true(1->win_move_statusline(offset))
1529 call assert_equal(h + offset, winheight(1))
1530 call assert_true(win_move_statusline(1, -offset))
1531 call assert_equal(h, winheight(1))
1532 endfor
zeertzjqa0c4e2f2022-01-29 10:59:53 +00001533 " check win_move_statusline from bottom window on bottom window
1534 let h0 = winheight(0)
1535 for offset in range(5)
1536 call assert_true(0->win_move_statusline(-offset))
1537 call assert_equal(h0 - offset, winheight(0))
1538 call assert_equal(1 + offset, &cmdheight)
1539 call assert_true(win_move_statusline(0, offset))
1540 call assert_equal(h0, winheight(0))
1541 call assert_equal(1, &cmdheight)
1542 endfor
1543 call assert_true(win_move_statusline(0, 1))
Bram Moolenaara2a89732022-08-31 14:46:18 +01001544 call assert_equal(h0, winheight(0))
1545 call assert_equal(1, &cmdheight)
Daniel Steinbergee630312022-01-10 13:36:34 +00001546 " check win_move_statusline from bottom window on top window ID
1547 let id = win_getid(1)
1548 for offset in range(5)
1549 call assert_true(win_move_statusline(id, offset))
1550 call assert_equal(h + offset, winheight(id))
1551 call assert_true(id->win_move_statusline(-offset))
1552 call assert_equal(h, winheight(id))
1553 endfor
1554 " check that win_move_statusline doesn't error with offsets beyond moving
1555 " possibility
1556 call assert_true(win_move_statusline(id, 5000))
1557 call assert_true(winheight(id) > h)
1558 call assert_true(win_move_statusline(id, -5000))
1559 call assert_true(winheight(id) < h)
1560 " check that win_move_statusline returns false for an invalid window
1561 wincmd =
1562 let h = winheight(0)
1563 call assert_false(win_move_statusline(-1, 1))
1564 call assert_equal(h, winheight(0))
1565 " check that win_move_statusline returns false for a popup window
1566 let id = popup_create(['hello', 'world'], {})
1567 let h = winheight(id)
1568 call assert_false(win_move_statusline(id, 1))
1569 call assert_equal(h, winheight(id))
1570 call popup_close(id)
1571 %bwipe!
1572endfunc
1573
Yegappan Lakshmanan72bb47e2022-04-03 11:22:38 +01001574" Test for window allocation failure
1575func Test_window_alloc_failure()
1576 %bw!
1577
1578 " test for creating a new window above current window
1579 call test_alloc_fail(GetAllocId('newwin_wvars'), 0, 0)
1580 call assert_fails('above new', 'E342:')
1581 call assert_equal(1, winnr('$'))
1582
1583 " test for creating a new window below current window
1584 call test_alloc_fail(GetAllocId('newwin_wvars'), 0, 0)
1585 call assert_fails('below new', 'E342:')
1586 call assert_equal(1, winnr('$'))
1587
1588 " test for popup window creation failure
1589 call test_alloc_fail(GetAllocId('newwin_wvars'), 0, 0)
1590 call assert_fails('call popup_create("Hello", {})', 'E342:')
1591 call assert_equal([], popup_list())
1592
1593 call test_alloc_fail(GetAllocId('newwin_wvars'), 0, 0)
1594 call assert_fails('split', 'E342:')
1595 call assert_equal(1, winnr('$'))
1596
Bram Moolenaarb18b4962022-09-02 21:55:50 +01001597 edit Xwaffile1
1598 edit Xwaffile2
Yegappan Lakshmanan72bb47e2022-04-03 11:22:38 +01001599 call test_alloc_fail(GetAllocId('newwin_wvars'), 0, 0)
Bram Moolenaarb18b4962022-09-02 21:55:50 +01001600 call assert_fails('sb Xwaffile1', 'E342:')
Yegappan Lakshmanan72bb47e2022-04-03 11:22:38 +01001601 call assert_equal(1, winnr('$'))
Bram Moolenaarb18b4962022-09-02 21:55:50 +01001602 call assert_equal('Xwaffile2', @%)
Yegappan Lakshmanan72bb47e2022-04-03 11:22:38 +01001603 %bw!
1604
1605 " FIXME: The following test crashes Vim
1606 " test for new tabpage creation failure
1607 " call test_alloc_fail(GetAllocId('newwin_wvars'), 0, 0)
1608 " call assert_fails('tabnew', 'E342:')
1609 " call assert_equal(1, tabpagenr('$'))
1610 " call assert_equal(1, winnr('$'))
1611
1612 " This test messes up the internal Vim window/frame information. So the
1613 " Test_window_cmd_cmdwin_with_vsp() test fails after running this test.
1614 " Open a new tab and close everything else to fix this issue.
1615 tabnew
1616 tabonly
1617endfunc
Bram Moolenaard0fb9072021-12-09 11:57:22 +00001618
Luuk van Baalfd7e60a2022-09-07 14:42:49 +01001619func Test_win_equal_last_status()
1620 let save_lines = &lines
1621 set lines=20
1622 set splitbelow
1623 set laststatus=0
1624
1625 split | split | quit
1626 call assert_equal(winheight(1), winheight(2))
1627
1628 let &lines = save_lines
1629 set splitbelow&
1630 set laststatus&
1631endfunc
1632
Luuk van Baal13ece2a2022-10-03 15:28:08 +01001633" Test "screen" and "cursor" values for 'splitkeep' with a sequence of
Luuk van Baalfaf1d412022-09-19 16:45:29 +01001634" split operations for various options: with and without a winbar,
1635" tabline, for each possible value of 'laststatus', 'scrolloff',
Luuk van Baal13ece2a2022-10-03 15:28:08 +01001636" 'equalalways', and with the cursor at the top, middle and bottom.
1637func Test_splitkeep_options()
Luuk van Baal594f9e02022-09-16 12:52:58 +01001638 " disallow window resizing
1639 let save_WS = &t_WS
1640 set t_WS=
1641
Luuk van Baal29ab5242022-09-11 16:59:53 +01001642 let gui = has("gui_running")
Luuk van Baal594f9e02022-09-16 12:52:58 +01001643 inoremap <expr> c "<cmd>copen<bar>wincmd k<CR>"
Luuk van Baal13ece2a2022-10-03 15:28:08 +01001644 for run in range(0, 20)
1645 let &splitkeep = run > 10 ? 'topline' : 'screen'
1646 let &scrolloff = (!(run % 4) ? 0 : run)
1647 let &laststatus = (run % 3)
1648 let &splitbelow = (run % 3)
1649 let &equalalways = (run % 2)
1650 let wsb = (run % 2) && &splitbelow
1651 let tl = (gui ? 0 : ((run % 5) ? 1 : 0))
1652 let pos = !(run % 3) ? 'H' : ((run % 2) ? 'M' : 'L')
1653 tabnew | tabonly! | redraw
Luuk van Baal594f9e02022-09-16 12:52:58 +01001654 execute (run % 5) ? 'tabnew' : ''
1655 execute (run % 2) ? 'nnoremenu 1.10 WinBar.Test :echo' : ''
Luuk van Baal594f9e02022-09-16 12:52:58 +01001656 call setline(1, range(1, 256))
1657 " No scroll for restore_snapshot
1658 norm G
1659 try
1660 copen | close | colder
1661 catch /E380/
1662 endtry
1663 call assert_equal(257 - winheight(0), line("w0"))
Luuk van Baal29ab5242022-09-11 16:59:53 +01001664
Luuk van Baal594f9e02022-09-16 12:52:58 +01001665 " No scroll for firstwin horizontal split
1666 execute 'norm gg' . pos
1667 split | redraw | wincmd k
1668 call assert_equal(1, line("w0"))
1669 call assert_equal(&scroll, winheight(0) / 2)
1670 wincmd j
Luuk van Baal13ece2a2022-10-03 15:28:08 +01001671 call assert_equal(&spk == 'topline' ? 1 : win_screenpos(0)[0] - tl - wsb, line("w0"))
Luuk van Baal29ab5242022-09-11 16:59:53 +01001672
Luuk van Baal594f9e02022-09-16 12:52:58 +01001673 " No scroll when resizing windows
Luuk van Baal13ece2a2022-10-03 15:28:08 +01001674 wincmd k | resize +2 | redraw
Luuk van Baal594f9e02022-09-16 12:52:58 +01001675 call assert_equal(1, line("w0"))
1676 wincmd j
Luuk van Baal13ece2a2022-10-03 15:28:08 +01001677 call assert_equal(&spk == 'topline' ? 1 : win_screenpos(0)[0] - tl - wsb, line("w0"))
Luuk van Baal29ab5242022-09-11 16:59:53 +01001678
Luuk van Baal594f9e02022-09-16 12:52:58 +01001679 " No scroll when dragging statusline
1680 call win_move_statusline(1, -3)
Luuk van Baal13ece2a2022-10-03 15:28:08 +01001681 call assert_equal(&spk == 'topline' ? 1 : win_screenpos(0)[0] - tl - wsb, line("w0"))
Luuk van Baal594f9e02022-09-16 12:52:58 +01001682 wincmd k
1683 call assert_equal(1, line("w0"))
Luuk van Baal29ab5242022-09-11 16:59:53 +01001684
Luuk van Baal594f9e02022-09-16 12:52:58 +01001685 " No scroll when changing shellsize
1686 set lines+=2
1687 call assert_equal(1, line("w0"))
1688 wincmd j
Luuk van Baal13ece2a2022-10-03 15:28:08 +01001689 call assert_equal(&spk == 'topline' ? 1 : win_screenpos(0)[0] - tl - wsb, line("w0"))
Luuk van Baal594f9e02022-09-16 12:52:58 +01001690 set lines-=2
Luuk van Baal13ece2a2022-10-03 15:28:08 +01001691 call assert_equal(&spk == 'topline' ? 1 : win_screenpos(0)[0] - tl - wsb, line("w0"))
Luuk van Baal594f9e02022-09-16 12:52:58 +01001692 wincmd k
1693 call assert_equal(1, line("w0"))
Luuk van Baal29ab5242022-09-11 16:59:53 +01001694
Luuk van Baal594f9e02022-09-16 12:52:58 +01001695 " No scroll when equalizing windows
1696 wincmd =
1697 call assert_equal(1, line("w0"))
1698 wincmd j
Luuk van Baal13ece2a2022-10-03 15:28:08 +01001699 call assert_equal(&spk == 'topline' ? 1 : win_screenpos(0)[0] - tl - wsb, line("w0"))
Luuk van Baal594f9e02022-09-16 12:52:58 +01001700 wincmd k
1701 call assert_equal(1, line("w0"))
Luuk van Baal29ab5242022-09-11 16:59:53 +01001702
Luuk van Baal594f9e02022-09-16 12:52:58 +01001703 " No scroll in windows split multiple times
1704 vsplit | split | 4wincmd w
Luuk van Baal13ece2a2022-10-03 15:28:08 +01001705 call assert_equal(&spk == 'topline' ? 1 : win_screenpos(0)[0] - tl - wsb, line("w0"))
Luuk van Baal594f9e02022-09-16 12:52:58 +01001706 1wincmd w | quit | wincmd l | split
Luuk van Baal13ece2a2022-10-03 15:28:08 +01001707 call assert_equal(&spk == 'topline' ? 1 : win_screenpos(0)[0] - tl - wsb, line("w0"))
Luuk van Baal594f9e02022-09-16 12:52:58 +01001708 wincmd j
Luuk van Baal13ece2a2022-10-03 15:28:08 +01001709 call assert_equal(&spk == 'topline' ? 1 : win_screenpos(0)[0] - tl - wsb, line("w0"))
Luuk van Baal29ab5242022-09-11 16:59:53 +01001710
Luuk van Baal594f9e02022-09-16 12:52:58 +01001711 " No scroll in small window
1712 2wincmd w | only | 5split | wincmd k
1713 call assert_equal(1, line("w0"))
1714 wincmd j
Luuk van Baal13ece2a2022-10-03 15:28:08 +01001715 call assert_equal(&spk == 'topline' ? 1 : win_screenpos(0)[0] - tl - wsb, line("w0"))
Luuk van Baal29ab5242022-09-11 16:59:53 +01001716
Luuk van Baal594f9e02022-09-16 12:52:58 +01001717 " No scroll for vertical split
1718 quit | vsplit | wincmd l
1719 call assert_equal(1, line("w0"))
1720 wincmd h
1721 call assert_equal(1, line("w0"))
Luuk van Baal29ab5242022-09-11 16:59:53 +01001722
Luuk van Baal594f9e02022-09-16 12:52:58 +01001723 " No scroll in windows split and quit multiple times
Luuk van Baal13ece2a2022-10-03 15:28:08 +01001724 quit | redraw | split | split | quit | redraw
1725 call assert_equal(&spk == 'topline' ? 1 : win_screenpos(0)[0] - tl - wsb, line("w0"))
Luuk van Baal29ab5242022-09-11 16:59:53 +01001726
Luuk van Baal594f9e02022-09-16 12:52:58 +01001727 " No scroll for new buffer
1728 1wincmd w | only | copen | wincmd k
1729 call assert_equal(1, line("w0"))
1730 only
1731 call assert_equal(1, line("w0"))
1732 above copen | wincmd j
Luuk van Baal13ece2a2022-10-03 15:28:08 +01001733 call assert_equal(&spk == 'topline' ? 1 : win_screenpos(0)[0] - tl, line("w0"))
Luuk van Baal29ab5242022-09-11 16:59:53 +01001734
Luuk van Baal594f9e02022-09-16 12:52:58 +01001735 " No scroll when opening cmdwin, and no cursor move when closing cmdwin.
1736 only | norm ggL
1737 let curpos = getcurpos()
1738 norm q:
1739 call assert_equal(1, line("w0"))
1740 call assert_equal(curpos, getcurpos())
Luuk van Baal29ab5242022-09-11 16:59:53 +01001741
Luuk van Baal594f9e02022-09-16 12:52:58 +01001742 " Scroll when cursor becomes invalid in insert mode
1743 norm Lic
1744 call assert_equal(curpos, getcurpos())
Luuk van Baal29ab5242022-09-11 16:59:53 +01001745
Luuk van Baal594f9e02022-09-16 12:52:58 +01001746 " No scroll when topline not equal to 1
1747 only | execute "norm gg5\<C-e>" | split | wincmd k
1748 call assert_equal(6, line("w0"))
1749 wincmd j
Luuk van Baal13ece2a2022-10-03 15:28:08 +01001750 call assert_equal(&spk == 'topline' ? 6 : 5 + win_screenpos(0)[0] - tl - wsb, line("w0"))
Luuk van Baal29ab5242022-09-11 16:59:53 +01001751 endfor
1752
1753 tabnew | tabonly! | %bwipeout!
1754 iunmap c
Luuk van Baal29ab5242022-09-11 16:59:53 +01001755 set scrolloff&
1756 set splitbelow&
1757 set laststatus&
1758 set equalalways&
Luuk van Baal13ece2a2022-10-03 15:28:08 +01001759 set splitkeep&
Luuk van Baal594f9e02022-09-16 12:52:58 +01001760 let &t_WS = save_WS
Luuk van Baal29ab5242022-09-11 16:59:53 +01001761endfunc
Luuk van Baalfd7e60a2022-09-07 14:42:49 +01001762
Luuk van Baal13ece2a2022-10-03 15:28:08 +01001763function Test_splitkeep_cmdwin_cursor_position()
1764 set splitkeep=screen
mityu12167d82022-09-15 17:44:07 +01001765 call setline(1, range(&lines))
1766
1767 " No scroll when cursor is at near bottom of window and cusor position
1768 " recompution (done by line('w0') in this test) happens while in cmdwin.
1769 normal! G
1770 let firstline = line('w0')
1771 autocmd CmdwinEnter * ++once autocmd WinEnter * ++once call line('w0')
1772 execute "normal! q:\<C-w>q"
1773 redraw!
1774 call assert_equal(firstline, line('w0'))
1775
1776 " User script can change cursor position successfully while in cmdwin and it
1777 " shouldn't be changed when closing cmdwin.
1778 execute "normal! Gq:\<Cmd>call win_execute(winnr('#')->win_getid(), 'call cursor(1, 1)')\<CR>\<C-w>q"
1779 call assert_equal(1, line('.'))
1780 call assert_equal(1, col('.'))
1781
1782 execute "normal! Gq:\<Cmd>autocmd WinEnter * ++once call cursor(1, 1)\<CR>\<C-w>q"
1783 call assert_equal(1, line('.'))
1784 call assert_equal(1, col('.'))
1785
1786 %bwipeout!
Luuk van Baal13ece2a2022-10-03 15:28:08 +01001787 set splitkeep&
mityu12167d82022-09-15 17:44:07 +01001788endfunction
Luuk van Baald5bc7622022-09-17 16:16:35 +01001789
Luuk van Baal13ece2a2022-10-03 15:28:08 +01001790function Test_splitkeep_misc()
1791 set splitkeep=screen
Luuk van Baalfaf1d412022-09-19 16:45:29 +01001792 set splitbelow
Luuk van Baald5bc7622022-09-17 16:16:35 +01001793
1794 call setline(1, range(1, &lines))
1795 norm Gzz
1796 let top = line('w0')
Luuk van Baalfaf1d412022-09-19 16:45:29 +01001797 " No scroll when aucmd_win is opened
Luuk van Baald5bc7622022-09-17 16:16:35 +01001798 call setbufvar(bufnr("test", 1) , '&buftype', 'nofile')
1799 call assert_equal(top, line('w0'))
Luuk van Baalfaf1d412022-09-19 16:45:29 +01001800 " No scroll when tab is changed/closed
1801 tab help | close
1802 call assert_equal(top, line('w0'))
1803 " No scroll when help is closed and buffer line count < window height
1804 norm ggdG
1805 call setline(1, range(1, &lines - 10))
Luuk van Baald5bc7622022-09-17 16:16:35 +01001806 norm G
1807 let top = line('w0')
1808 help | quit
1809 call assert_equal(top, line('w0'))
Luuk van Baal346823d2022-10-05 18:26:42 +01001810 " No error when resizing window in autocmd and buffer length changed
1811 autocmd FileType qf exe "resize" line('$')
1812 cexpr getline(1, '$')
1813 copen
1814 wincmd p
1815 norm dd
1816 cexpr getline(1, '$')
Luuk van Baald5bc7622022-09-17 16:16:35 +01001817
Luuk van Baalfaf1d412022-09-19 16:45:29 +01001818 %bwipeout!
Luuk van Baald5bc7622022-09-17 16:16:35 +01001819 set splitbelow&
Luuk van Baal13ece2a2022-10-03 15:28:08 +01001820 set splitkeep&
Luuk van Baald5bc7622022-09-17 16:16:35 +01001821endfunc
1822
Luuk van Baal13ece2a2022-10-03 15:28:08 +01001823function Test_splitkeep_callback()
Luuk van Baal20e58562022-09-23 12:57:09 +01001824 CheckScreendump
1825 let lines =<< trim END
Luuk van Baal13ece2a2022-10-03 15:28:08 +01001826 set splitkeep=screen
Luuk van Baal20e58562022-09-23 12:57:09 +01001827 call setline(1, range(&lines))
Luuk van Baal13ece2a2022-10-03 15:28:08 +01001828 function C1(a, b)
Luuk van Baal20e58562022-09-23 12:57:09 +01001829 split | wincmd p
1830 endfunction
Luuk van Baal13ece2a2022-10-03 15:28:08 +01001831 function C2(a, b)
Luuk van Baal20e58562022-09-23 12:57:09 +01001832 close | split
1833 endfunction
Luuk van Baal13ece2a2022-10-03 15:28:08 +01001834 nn j <cmd>call job_start([&sh, &shcf, "true"], { 'exit_cb': 'C1' })<CR>
1835 nn t <cmd>call popup_create(term_start([&sh, &shcf, "true"],
1836 \ { 'hidden': 1, 'exit_cb': 'C2' }), {})<CR>
Luuk van Baal20e58562022-09-23 12:57:09 +01001837 END
Luuk van Baal13ece2a2022-10-03 15:28:08 +01001838 call writefile(lines, 'XTestSplitkeepCallback', 'D')
1839 let buf = RunVimInTerminal('-S XTestSplitkeepCallback', #{rows: 8})
Luuk van Baal20e58562022-09-23 12:57:09 +01001840
1841 call term_sendkeys(buf, "j")
Luuk van Baal13ece2a2022-10-03 15:28:08 +01001842 call VerifyScreenDump(buf, 'Test_splitkeep_callback_1', {})
Luuk van Baal20e58562022-09-23 12:57:09 +01001843
Luuk van Baal13ece2a2022-10-03 15:28:08 +01001844 call term_sendkeys(buf, ":quit\<CR>Ht")
1845 call VerifyScreenDump(buf, 'Test_splitkeep_callback_2', {})
Luuk van Baal20e58562022-09-23 12:57:09 +01001846
1847 call term_sendkeys(buf, ":set sb\<CR>:quit\<CR>Gj")
Luuk van Baal13ece2a2022-10-03 15:28:08 +01001848 call VerifyScreenDump(buf, 'Test_splitkeep_callback_3', {})
Luuk van Baal20e58562022-09-23 12:57:09 +01001849
Luuk van Baal13ece2a2022-10-03 15:28:08 +01001850 call term_sendkeys(buf, ":quit\<CR>Gt")
1851 call VerifyScreenDump(buf, 'Test_splitkeep_callback_4', {})
Luuk van Baal20e58562022-09-23 12:57:09 +01001852endfunc
1853
Luuk van Baal13ece2a2022-10-03 15:28:08 +01001854function Test_splitkeep_fold()
1855 CheckScreendump
Luuk van Baal7c1cbb62022-09-27 12:31:15 +01001856
Luuk van Baal13ece2a2022-10-03 15:28:08 +01001857 let lines =<< trim END
1858 set splitkeep=screen
1859 set foldmethod=marker
1860 set number
1861 let line = 1
1862 for n in range(1, &lines)
1863 call setline(line, ['int FuncName() {/*{{{*/', 1, 2, 3, 4, 5, '}/*}}}*/',
1864 \ 'after fold'])
1865 let line += 8
1866 endfor
1867 END
1868 call writefile(lines, 'XTestSplitkeepFold', 'D')
1869 let buf = RunVimInTerminal('-S XTestSplitkeepFold', #{rows: 10})
Luuk van Baal7c1cbb62022-09-27 12:31:15 +01001870
1871 call term_sendkeys(buf, "L:wincmd s\<CR>")
Luuk van Baal13ece2a2022-10-03 15:28:08 +01001872 call VerifyScreenDump(buf, 'Test_splitkeep_fold_1', {})
Luuk van Baal7c1cbb62022-09-27 12:31:15 +01001873
1874 call term_sendkeys(buf, ":quit\<CR>")
Luuk van Baal13ece2a2022-10-03 15:28:08 +01001875 call VerifyScreenDump(buf, 'Test_splitkeep_fold_2', {})
Luuk van Baal7c1cbb62022-09-27 12:31:15 +01001876
1877 call term_sendkeys(buf, "H:below split\<CR>")
Luuk van Baal13ece2a2022-10-03 15:28:08 +01001878 call VerifyScreenDump(buf, 'Test_splitkeep_fold_3', {})
Luuk van Baal7c1cbb62022-09-27 12:31:15 +01001879
1880 call term_sendkeys(buf, ":wincmd k\<CR>:quit\<CR>")
Luuk van Baal13ece2a2022-10-03 15:28:08 +01001881 call VerifyScreenDump(buf, 'Test_splitkeep_fold_4', {})
Luuk van Baal7c1cbb62022-09-27 12:31:15 +01001882endfunction
1883
Bram Moolenaar9e4d8212016-08-18 23:04:48 +02001884" vim: shiftwidth=2 sts=2 expandtab