blob: 6b7dccbb000717d344bc4eb992fe34e740cd7c31 [file] [log] [blame]
Bram Moolenaar991dea32016-05-24 11:31:32 +02001" Tests for window cmd (:wincmd, :split, :vsplit, :resize and etc...)
2
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +02003source check.vim
Luuk van Baal20e58562022-09-23 12:57:09 +01004source screendump.vim
Bram Moolenaara27e1dc2019-10-07 22:27:36 +02005
Bram Moolenaar991dea32016-05-24 11:31:32 +02006func Test_window_cmd_ls0_with_split()
7 set ls=0
8 set splitbelow
9 split
10 quit
11 call assert_equal(0, &lines - &cmdheight - winheight(0))
12 new | only!
13 "
14 set splitbelow&vim
15 botright split
16 quit
17 call assert_equal(0, &lines - &cmdheight - winheight(0))
18 new | only!
19 set ls&vim
20endfunc
21
zeertzjqfbf20712023-04-26 19:01:44 +010022func Test_window_cmd_ls0_split_scrolling()
23 CheckRunVimInTerminal
24
25 let lines =<< trim END
26 set laststatus=0
27 call setline(1, range(1, 100))
28 normal! G
29 END
30 call writefile(lines, 'XTestLs0SplitScrolling', 'D')
31 let buf = RunVimInTerminal('-S XTestLs0SplitScrolling', #{rows: 10})
32
33 call term_sendkeys(buf, ":botright split\<CR>")
34 call WaitForAssert({-> assert_match('Bot$', term_getline(buf, 5))})
35 call assert_equal('100', term_getline(buf, 4))
36
37 call StopVimInTerminal(buf)
38endfunc
39
Bram Moolenaar991dea32016-05-24 11:31:32 +020040func Test_window_cmd_cmdwin_with_vsp()
Bram Moolenaar72cf47a2018-05-10 18:23:29 +020041 let efmt = 'Expected 0 but got %d (in ls=%d, %s window)'
Bram Moolenaar991dea32016-05-24 11:31:32 +020042 for v in range(0, 2)
43 exec "set ls=" . v
44 vsplit
45 call feedkeys("q:\<CR>")
46 let ac = &lines - (&cmdheight + winheight(0) + !!v)
47 let emsg = printf(efmt, ac, v, 'left')
48 call assert_equal(0, ac, emsg)
49 wincmd w
50 let ac = &lines - (&cmdheight + winheight(0) + !!v)
51 let emsg = printf(efmt, ac, v, 'right')
52 call assert_equal(0, ac, emsg)
53 new | only!
54 endfor
55 set ls&vim
56endfunc
57
Bram Moolenaar96bde992022-08-10 17:23:12 +010058func Test_cmdheight_not_changed()
59 set cmdheight=2
60 set winminheight=0
61 augroup Maximize
62 autocmd WinEnter * wincmd _
63 augroup END
64 split
65 tabnew
66 tabfirst
67 call assert_equal(2, &cmdheight)
68
69 tabonly!
70 only
71 set winminwidth& cmdheight&
72 augroup Maximize
73 au!
74 augroup END
75 augroup! Maximize
76endfunc
77
Bram Moolenaar5d3c9f82020-06-26 20:41:39 +020078" Test for jumping to windows
79func Test_window_jump()
80 new
81 " jumping to a window with a count greater than the max windows
82 exe "normal 4\<C-W>w"
83 call assert_equal(2, winnr())
84 only
85endfunc
86
87func Test_window_cmd_wincmd_gf()
Bram Moolenaar5d2ca042016-06-26 17:11:21 +020088 let fname = 'test_gf.txt'
89 let swp_fname = '.' . fname . '.swp'
Bram Moolenaardb4c9472022-10-15 22:06:06 +010090 call writefile([], fname, 'D')
91 call writefile([], swp_fname, 'D')
Bram Moolenaar5d2ca042016-06-26 17:11:21 +020092 function s:swap_exists()
93 let v:swapchoice = s:swap_choice
94 endfunc
Bram Moolenaareaa49e42019-07-13 18:08:59 +020095 " Remove the catch-all that runtest.vim adds
96 au! SwapExists
Bram Moolenaar5d2ca042016-06-26 17:11:21 +020097 augroup test_window_cmd_wincmd_gf
98 autocmd!
99 exec "autocmd SwapExists " . fname . " call s:swap_exists()"
100 augroup END
101
102 call setline(1, fname)
103 " (E)dit anyway
104 let s:swap_choice = 'e'
105 wincmd gf
106 call assert_equal(2, tabpagenr())
107 call assert_equal(fname, bufname("%"))
108 quit!
109
110 " (Q)uit
111 let s:swap_choice = 'q'
112 wincmd gf
113 call assert_equal(1, tabpagenr())
114 call assert_notequal(fname, bufname("%"))
115 new | only!
116
Bram Moolenaar5d2ca042016-06-26 17:11:21 +0200117 augroup! test_window_cmd_wincmd_gf
118endfunc
119
Bram Moolenaar4520d442017-03-19 16:09:46 +0100120func Test_window_quit()
121 e Xa
122 split Xb
Bram Moolenaarf92e58c2019-09-08 21:51:41 +0200123 call assert_equal(2, '$'->winnr())
Bram Moolenaar4520d442017-03-19 16:09:46 +0100124 call assert_equal('Xb', bufname(winbufnr(1)))
125 call assert_equal('Xa', bufname(winbufnr(2)))
126
127 wincmd q
128 call assert_equal(1, winnr('$'))
129 call assert_equal('Xa', bufname(winbufnr(1)))
130
131 bw Xa Xb
132endfunc
133
Sean Dewarbf44b692024-01-03 18:52:52 +0100134func Test_window_curwin_not_prevwin()
135 botright split
136 call assert_equal(2, winnr())
137 call assert_equal(1, winnr('#'))
138 quit
139 call assert_equal(1, winnr())
140 call assert_equal(0, winnr('#'))
141
142 botright split
143 botright split
144 call assert_equal(3, winnr())
145 call assert_equal(2, winnr('#'))
146 1quit
147 call assert_equal(2, winnr())
148 call assert_equal(1, winnr('#'))
149
150 botright split
151 call assert_equal(1, tabpagenr())
152 call assert_equal(3, winnr())
153 call assert_equal(2, winnr('#'))
154 wincmd T
155 call assert_equal(2, tabpagenr())
156 call assert_equal(1, winnr())
157 call assert_equal(0, winnr('#'))
158 tabfirst
159 call assert_equal(1, tabpagenr())
160 call assert_equal(2, winnr())
161 call assert_equal(0, winnr('#'))
162
163 tabonly
164 botright split
165 wincmd t
166 wincmd p
167 call assert_equal(3, winnr())
168 call assert_equal(1, winnr('#'))
169 quit
170 call assert_equal(2, winnr())
171 call assert_equal(1, winnr('#'))
172
173 botright split
174 wincmd t
175 wincmd p
176 call assert_equal(1, tabpagenr())
177 call assert_equal(3, winnr())
178 call assert_equal(1, winnr('#'))
179 wincmd T
180 call assert_equal(2, tabpagenr())
181 call assert_equal(1, winnr())
182 call assert_equal(0, winnr('#'))
183 tabfirst
184 call assert_equal(1, tabpagenr())
185 call assert_equal(2, winnr())
186 call assert_equal(1, winnr('#'))
187
188 tabonly
189 only
190endfunc
191
Bram Moolenaar4520d442017-03-19 16:09:46 +0100192func Test_window_horizontal_split()
193 call assert_equal(1, winnr('$'))
194 3wincmd s
195 call assert_equal(2, winnr('$'))
196 call assert_equal(3, winheight(0))
Bram Moolenaarf92e58c2019-09-08 21:51:41 +0200197 call assert_equal(winwidth(1), 2->winwidth())
Bram Moolenaar4520d442017-03-19 16:09:46 +0100198
199 call assert_fails('botright topleft wincmd s', 'E442:')
200 bw
201endfunc
202
203func Test_window_vertical_split()
204 call assert_equal(1, winnr('$'))
205 3wincmd v
206 call assert_equal(2, winnr('$'))
207 call assert_equal(3, winwidth(0))
208 call assert_equal(winheight(1), winheight(2))
209
210 call assert_fails('botright topleft wincmd v', 'E442:')
211 bw
212endfunc
213
Bram Moolenaar1bbb6192018-11-10 16:02:01 +0100214" Test the ":wincmd ^" and "<C-W>^" commands.
Bram Moolenaar4520d442017-03-19 16:09:46 +0100215func Test_window_split_edit_alternate()
Bram Moolenaar1bbb6192018-11-10 16:02:01 +0100216 " Test for failure when the alternate buffer/file no longer exists.
217 edit Xfoo | %bw
Bram Moolenaare2e40752020-09-04 21:18:46 +0200218 call assert_fails(':wincmd ^', 'E23:')
Bram Moolenaar1bbb6192018-11-10 16:02:01 +0100219
220 " Test for the expected behavior when we have two named buffers.
221 edit Xfoo | edit Xbar
Bram Moolenaar4520d442017-03-19 16:09:46 +0100222 wincmd ^
Bram Moolenaar1bbb6192018-11-10 16:02:01 +0100223 call assert_equal('Xfoo', bufname(winbufnr(1)))
224 call assert_equal('Xbar', bufname(winbufnr(2)))
225 only
Bram Moolenaar4520d442017-03-19 16:09:46 +0100226
Bram Moolenaar1bbb6192018-11-10 16:02:01 +0100227 " Test for the expected behavior when the alternate buffer is not named.
228 enew | let l:nr1 = bufnr('%')
229 edit Xfoo | let l:nr2 = bufnr('%')
230 wincmd ^
231 call assert_equal(l:nr1, winbufnr(1))
232 call assert_equal(l:nr2, winbufnr(2))
233 only
234
Bram Moolenaard42333d2018-11-10 20:28:19 +0100235 " FIXME: this currently fails on AppVeyor, but passes locally
236 if !has('win32')
237 " Test the Normal mode command.
238 call feedkeys("\<C-W>\<C-^>", 'tx')
239 call assert_equal(l:nr2, winbufnr(1))
240 call assert_equal(l:nr1, winbufnr(2))
241 endif
Bram Moolenaar1bbb6192018-11-10 16:02:01 +0100242
243 %bw!
244endfunc
245
246" Test the ":[count]wincmd ^" and "[count]<C-W>^" commands.
247func Test_window_split_edit_bufnr()
Bram Moolenaar1bbb6192018-11-10 16:02:01 +0100248 %bwipeout
249 let l:nr = bufnr('%') + 1
Bram Moolenaare2e40752020-09-04 21:18:46 +0200250 call assert_fails(':execute "normal! ' . l:nr . '\<C-W>\<C-^>"', 'E92:')
251 call assert_fails(':' . l:nr . 'wincmd ^', 'E16:')
252 call assert_fails(':0wincmd ^', 'E16:')
Bram Moolenaar1bbb6192018-11-10 16:02:01 +0100253
254 edit Xfoo | edit Xbar | edit Xbaz
255 let l:foo_nr = bufnr('Xfoo')
256 let l:bar_nr = bufnr('Xbar')
257 let l:baz_nr = bufnr('Xbaz')
258
Bram Moolenaar8617b402018-11-10 20:47:48 +0100259 " FIXME: this currently fails on AppVeyor, but passes locally
260 if !has('win32')
261 call feedkeys(l:foo_nr . "\<C-W>\<C-^>", 'tx')
262 call assert_equal('Xfoo', bufname(winbufnr(1)))
263 call assert_equal('Xbaz', bufname(winbufnr(2)))
264 only
Bram Moolenaar1bbb6192018-11-10 16:02:01 +0100265
Bram Moolenaar8617b402018-11-10 20:47:48 +0100266 call feedkeys(l:bar_nr . "\<C-W>\<C-^>", 'tx')
267 call assert_equal('Xbar', bufname(winbufnr(1)))
268 call assert_equal('Xfoo', bufname(winbufnr(2)))
269 only
Bram Moolenaar1bbb6192018-11-10 16:02:01 +0100270
Bram Moolenaar8617b402018-11-10 20:47:48 +0100271 execute l:baz_nr . 'wincmd ^'
272 call assert_equal('Xbaz', bufname(winbufnr(1)))
273 call assert_equal('Xbar', bufname(winbufnr(2)))
274 endif
Bram Moolenaar1bbb6192018-11-10 16:02:01 +0100275
276 %bw!
Bram Moolenaar4520d442017-03-19 16:09:46 +0100277endfunc
278
Bram Moolenaar9b9be002020-03-22 14:41:22 +0100279func Test_window_split_no_room()
280 " N horizontal windows need >= 2*N + 1 lines:
281 " - 1 line + 1 status line in each window
282 " - 1 Ex command line
283 "
284 " 2*N + 1 <= &lines
285 " N <= (lines - 1)/2
286 "
287 " Beyond that number of windows, E36: Not enough room is expected.
288 let hor_win_count = (&lines - 1)/2
289 let hor_split_count = hor_win_count - 1
290 for s in range(1, hor_split_count) | split | endfor
291 call assert_fails('split', 'E36:')
292
293 " N vertical windows need >= 2*(N - 1) + 1 columns:
294 " - 1 column + 1 separator for each window (except last window)
295 " - 1 column for the last window which does not have separator
296 "
297 " 2*(N - 1) + 1 <= &columns
298 " 2*N - 1 <= &columns
299 " N <= (&columns + 1)/2
300 let ver_win_count = (&columns + 1)/2
301 let ver_split_count = ver_win_count - 1
302 for s in range(1, ver_split_count) | vsplit | endfor
303 call assert_fails('vsplit', 'E36:')
304
305 %bw!
306endfunc
307
Bram Moolenaar4520d442017-03-19 16:09:46 +0100308func Test_window_exchange()
309 e Xa
310
311 " Nothing happens with window exchange when there is 1 window
312 wincmd x
313 call assert_equal(1, winnr('$'))
314
315 split Xb
316 split Xc
317
318 call assert_equal('Xc', bufname(winbufnr(1)))
319 call assert_equal('Xb', bufname(winbufnr(2)))
320 call assert_equal('Xa', bufname(winbufnr(3)))
321
322 " Exchange current window 1 with window 3
323 3wincmd x
324 call assert_equal('Xa', bufname(winbufnr(1)))
325 call assert_equal('Xb', bufname(winbufnr(2)))
326 call assert_equal('Xc', bufname(winbufnr(3)))
327
328 " Exchange window with next when at the top window
329 wincmd x
330 call assert_equal('Xb', bufname(winbufnr(1)))
331 call assert_equal('Xa', bufname(winbufnr(2)))
332 call assert_equal('Xc', bufname(winbufnr(3)))
333
334 " Exchange window with next when at the middle window
335 wincmd j
336 wincmd x
337 call assert_equal('Xb', bufname(winbufnr(1)))
338 call assert_equal('Xc', bufname(winbufnr(2)))
339 call assert_equal('Xa', bufname(winbufnr(3)))
340
341 " Exchange window with next when at the bottom window.
342 " When there is no next window, it exchanges with the previous window.
343 wincmd j
344 wincmd x
345 call assert_equal('Xb', bufname(winbufnr(1)))
346 call assert_equal('Xa', bufname(winbufnr(2)))
347 call assert_equal('Xc', bufname(winbufnr(3)))
348
349 bw Xa Xb Xc
350endfunc
351
352func Test_window_rotate()
353 e Xa
354 split Xb
355 split Xc
356 call assert_equal('Xc', bufname(winbufnr(1)))
357 call assert_equal('Xb', bufname(winbufnr(2)))
358 call assert_equal('Xa', bufname(winbufnr(3)))
359
360 " Rotate downwards
361 wincmd r
362 call assert_equal('Xa', bufname(winbufnr(1)))
363 call assert_equal('Xc', bufname(winbufnr(2)))
364 call assert_equal('Xb', bufname(winbufnr(3)))
365
366 2wincmd r
367 call assert_equal('Xc', bufname(winbufnr(1)))
368 call assert_equal('Xb', bufname(winbufnr(2)))
369 call assert_equal('Xa', bufname(winbufnr(3)))
370
371 " Rotate upwards
372 wincmd R
373 call assert_equal('Xb', bufname(winbufnr(1)))
374 call assert_equal('Xa', bufname(winbufnr(2)))
375 call assert_equal('Xc', bufname(winbufnr(3)))
376
377 2wincmd R
378 call assert_equal('Xc', bufname(winbufnr(1)))
379 call assert_equal('Xb', bufname(winbufnr(2)))
380 call assert_equal('Xa', bufname(winbufnr(3)))
381
382 bot vsplit
383 call assert_fails('wincmd R', 'E443:')
384
385 bw Xa Xb Xc
386endfunc
387
388func Test_window_height()
389 e Xa
390 split Xb
391
392 let [wh1, wh2] = [winheight(1), winheight(2)]
393 " Active window (1) should have the same height or 1 more
394 " than the other window.
395 call assert_inrange(wh2, wh2 + 1, wh1)
396
397 wincmd -
398 call assert_equal(wh1 - 1, winheight(1))
399 call assert_equal(wh2 + 1, winheight(2))
400
401 wincmd +
402 call assert_equal(wh1, winheight(1))
Bram Moolenaarf92e58c2019-09-08 21:51:41 +0200403 call assert_equal(wh2, 2->winheight())
Bram Moolenaar4520d442017-03-19 16:09:46 +0100404
405 2wincmd _
406 call assert_equal(2, winheight(1))
407 call assert_equal(wh1 + wh2 - 2, winheight(2))
408
409 wincmd =
410 call assert_equal(wh1, winheight(1))
411 call assert_equal(wh2, winheight(2))
412
413 2wincmd _
414 set winfixheight
415 split Xc
416 let [wh1, wh2, wh3] = [winheight(1), winheight(2), winheight(3)]
417 call assert_equal(2, winheight(2))
418 call assert_inrange(wh3, wh3 + 1, wh1)
419 3wincmd +
420 call assert_equal(2, winheight(2))
421 call assert_equal(wh1 + 3, winheight(1))
422 call assert_equal(wh3 - 3, winheight(3))
423 wincmd =
424 call assert_equal(2, winheight(2))
425 call assert_equal(wh1, winheight(1))
426 call assert_equal(wh3, winheight(3))
427
428 wincmd j
429 set winfixheight&
430
431 wincmd =
432 let [wh1, wh2, wh3] = [winheight(1), winheight(2), winheight(3)]
433 " Current window (2) should have the same height or 1 more
434 " than the other windows.
435 call assert_inrange(wh1, wh1 + 1, wh2)
436 call assert_inrange(wh3, wh3 + 1, wh2)
437
438 bw Xa Xb Xc
439endfunc
440
Bram Moolenaar21c3a802022-08-31 17:49:14 +0100441func Test_wincmd_equal()
442 edit Xone
443 below split Xtwo
444 rightbelow vsplit Xthree
445 call assert_equal('Xone', bufname(winbufnr(1)))
446 call assert_equal('Xtwo', bufname(winbufnr(2)))
447 call assert_equal('Xthree', bufname(winbufnr(3)))
448
449 " Xone and Xtwo should be about the same height
450 let [wh1, wh2] = [winheight(1), winheight(2)]
451 call assert_inrange(wh1 - 1, wh1 + 1, wh2)
452 " Xtwo and Xthree should be about the same width
453 let [ww2, ww3] = [winwidth(2), winwidth(3)]
454 call assert_inrange(ww2 - 1, ww2 + 1, ww3)
455
456 1wincmd w
457 10wincmd _
458 2wincmd w
459 20wincmd |
460 call assert_equal(10, winheight(1))
461 call assert_equal(20, winwidth(2))
462
463 " equalizing horizontally doesn't change the heights
464 hor wincmd =
465 call assert_equal(10, winheight(1))
466 let [ww2, ww3] = [winwidth(2), winwidth(3)]
467 call assert_inrange(ww2 - 1, ww2 + 1, ww3)
468
469 2wincmd w
470 20wincmd |
471 call assert_equal(20, winwidth(2))
472 " equalizing vertically doesn't change the widths
473 vert wincmd =
474 call assert_equal(20, winwidth(2))
475 let [wh1, wh2] = [winheight(1), winheight(2)]
476 call assert_inrange(wh1 - 1, wh1 + 1, wh2)
477
478 bwipe Xone Xtwo Xthree
479endfunc
480
Bram Moolenaar4520d442017-03-19 16:09:46 +0100481func Test_window_width()
482 e Xa
483 vsplit Xb
484
485 let [ww1, ww2] = [winwidth(1), winwidth(2)]
486 " Active window (1) should have the same width or 1 more
487 " than the other window.
488 call assert_inrange(ww2, ww2 + 1, ww1)
489
490 wincmd <
491 call assert_equal(ww1 - 1, winwidth(1))
492 call assert_equal(ww2 + 1, winwidth(2))
493
494 wincmd >
495 call assert_equal(ww1, winwidth(1))
496 call assert_equal(ww2, winwidth(2))
497
498 2wincmd |
499 call assert_equal(2, winwidth(1))
500 call assert_equal(ww1 + ww2 - 2, winwidth(2))
501
502 wincmd =
503 call assert_equal(ww1, winwidth(1))
504 call assert_equal(ww2, winwidth(2))
505
506 2wincmd |
507 set winfixwidth
508 vsplit Xc
509 let [ww1, ww2, ww3] = [winwidth(1), winwidth(2), winwidth(3)]
Bram Moolenaar38e34832017-03-19 20:22:36 +0100510 call assert_equal(2, winwidth(2))
Bram Moolenaar4520d442017-03-19 16:09:46 +0100511 call assert_inrange(ww3, ww3 + 1, ww1)
512 3wincmd >
Bram Moolenaar38e34832017-03-19 20:22:36 +0100513 call assert_equal(2, winwidth(2))
Bram Moolenaar4520d442017-03-19 16:09:46 +0100514 call assert_equal(ww1 + 3, winwidth(1))
515 call assert_equal(ww3 - 3, winwidth(3))
516 wincmd =
Bram Moolenaar38e34832017-03-19 20:22:36 +0100517 call assert_equal(2, winwidth(2))
Bram Moolenaar4520d442017-03-19 16:09:46 +0100518 call assert_equal(ww1, winwidth(1))
519 call assert_equal(ww3, winwidth(3))
520
521 wincmd l
522 set winfixwidth&
523
524 wincmd =
525 let [ww1, ww2, ww3] = [winwidth(1), winwidth(2), winwidth(3)]
526 " Current window (2) should have the same width or 1 more
527 " than the other windows.
528 call assert_inrange(ww1, ww1 + 1, ww2)
529 call assert_inrange(ww3, ww3 + 1, ww2)
530
Yegappan Lakshmanan59585492021-06-12 13:46:41 +0200531 " when the current window width is less than the new 'winwidth', the current
532 " window width should be increased.
533 enew | only
534 split
535 10vnew
536 set winwidth=15
537 call assert_equal(15, winwidth(0))
538
539 %bw!
Bram Moolenaar4520d442017-03-19 16:09:46 +0100540endfunc
541
Bram Moolenaar8eeeba82017-06-25 22:45:39 +0200542func Test_equalalways_on_close()
543 set equalalways
544 vsplit
545 windo split
Bram Moolenaar1bbb6192018-11-10 16:02:01 +0100546 split
Bram Moolenaar8eeeba82017-06-25 22:45:39 +0200547 wincmd J
548 " now we have a frame top-left with two windows, a frame top-right with two
549 " windows and a frame at the bottom, full-width.
550 let height_1 = winheight(1)
551 let height_2 = winheight(2)
552 let height_3 = winheight(3)
553 let height_4 = winheight(4)
554 " closing the bottom window causes all windows to be resized.
555 close
556 call assert_notequal(height_1, winheight(1))
557 call assert_notequal(height_2, winheight(2))
558 call assert_notequal(height_3, winheight(3))
559 call assert_notequal(height_4, winheight(4))
560 call assert_equal(winheight(1), winheight(3))
561 call assert_equal(winheight(2), winheight(4))
562
563 1wincmd w
564 split
565 4wincmd w
566 resize + 5
567 " left column has three windows, equalized heights.
568 " right column has two windows, top one a bit higher
569 let height_1 = winheight(1)
570 let height_2 = winheight(2)
571 let height_4 = winheight(4)
572 let height_5 = winheight(5)
573 3wincmd w
574 " closing window in left column equalizes heights in left column but not in
575 " the right column
576 close
577 call assert_notequal(height_1, winheight(1))
578 call assert_notequal(height_2, winheight(2))
579 call assert_equal(height_4, winheight(3))
580 call assert_equal(height_5, winheight(4))
581
582 only
583 set equalalways&
584endfunc
585
Bram Moolenaar22044dc2017-12-02 15:43:37 +0100586func Test_win_screenpos()
Bram Moolenaar5a4c3082019-12-01 15:23:11 +0100587 CheckFeature quickfix
588
Bram Moolenaar22044dc2017-12-02 15:43:37 +0100589 call assert_equal(1, winnr('$'))
590 split
591 vsplit
592 10wincmd _
593 30wincmd |
594 call assert_equal([1, 1], win_screenpos(1))
595 call assert_equal([1, 32], win_screenpos(2))
596 call assert_equal([12, 1], win_screenpos(3))
597 call assert_equal([0, 0], win_screenpos(4))
Bram Moolenaar99fa7212020-04-26 15:59:55 +0200598 call assert_fails('let l = win_screenpos([])', 'E745:')
Bram Moolenaar22044dc2017-12-02 15:43:37 +0100599 only
600endfunc
601
Bram Moolenaar4520d442017-03-19 16:09:46 +0100602func Test_window_jump_tag()
Bram Moolenaar5a4c3082019-12-01 15:23:11 +0100603 CheckFeature quickfix
604
Bram Moolenaar4520d442017-03-19 16:09:46 +0100605 help
606 /iccf
607 call assert_match('^|iccf|', getline('.'))
608 call assert_equal(2, winnr('$'))
609 2wincmd }
610 call assert_equal(3, winnr('$'))
611 call assert_match('^|iccf|', getline('.'))
612 wincmd k
613 call assert_match('\*iccf\*', getline('.'))
614 call assert_equal(2, winheight(0))
615
616 wincmd z
617 set previewheight=4
618 help
619 /bugs
620 wincmd }
621 wincmd k
622 call assert_match('\*bugs\*', getline('.'))
623 call assert_equal(4, winheight(0))
624 set previewheight&
625
626 %bw!
627endfunc
628
629func Test_window_newtab()
630 e Xa
631
632 call assert_equal(1, tabpagenr('$'))
633 call assert_equal("\nAlready only one window", execute('wincmd T'))
634
635 split Xb
636 split Xc
637
638 wincmd T
639 call assert_equal(2, tabpagenr('$'))
640 call assert_equal(['Xb', 'Xa'], map(tabpagebuflist(1), 'bufname(v:val)'))
Bram Moolenaarce90e362019-09-08 18:58:44 +0200641 call assert_equal(['Xc' ], map(2->tabpagebuflist(), 'bufname(v:val)'))
Bram Moolenaar0e05de42020-03-25 22:23:46 +0100642 call assert_equal(['Xc' ], map(tabpagebuflist(), 'bufname(v:val)'))
Bram Moolenaar4520d442017-03-19 16:09:46 +0100643
644 %bw!
645endfunc
646
Bram Moolenaarf79225e2017-03-18 23:11:04 +0100647func Test_next_split_all()
648 " This was causing an illegal memory access.
649 n x
650 norm axxx
651 split
652 split
653 s/x
654 s/x
655 all
656 bwipe!
657endfunc
658
Bram Moolenaar75373f32017-08-07 22:02:30 +0200659" Tests for adjusting window and contents
660func GetScreenStr(row)
661 let str = ""
662 for c in range(1,3)
663 let str .= nr2char(screenchar(a:row, c))
664 endfor
665 return str
666endfunc
667
668func Test_window_contents()
669 enew! | only | new
670 call setline(1, range(1,256))
671
672 exe "norm! \<C-W>t\<C-W>=1Gzt\<C-W>w\<C-W>+"
673 redraw
Bram Moolenaar72cf47a2018-05-10 18:23:29 +0200674 let s3 = GetScreenStr(1)
Bram Moolenaar75373f32017-08-07 22:02:30 +0200675 wincmd p
676 call assert_equal(1, line("w0"))
677 call assert_equal('1 ', s3)
678
679 exe "norm! \<C-W>t\<C-W>=50Gzt\<C-W>w\<C-W>+"
680 redraw
Bram Moolenaar72cf47a2018-05-10 18:23:29 +0200681 let s3 = GetScreenStr(1)
Bram Moolenaar75373f32017-08-07 22:02:30 +0200682 wincmd p
683 call assert_equal(50, line("w0"))
684 call assert_equal('50 ', s3)
685
686 exe "norm! \<C-W>t\<C-W>=59Gzt\<C-W>w\<C-W>+"
687 redraw
Bram Moolenaar72cf47a2018-05-10 18:23:29 +0200688 let s3 = GetScreenStr(1)
Bram Moolenaar75373f32017-08-07 22:02:30 +0200689 wincmd p
690 call assert_equal(59, line("w0"))
691 call assert_equal('59 ', s3)
692
Bram Moolenaar8b633132020-03-20 18:20:51 +0100693 %d
694 call setline(1, ['one', 'two', 'three'])
695 call assert_equal(1, line('w0'))
696 call assert_equal(3, line('w$'))
697
Bram Moolenaar75373f32017-08-07 22:02:30 +0200698 bwipeout!
699 call test_garbagecollect_now()
700endfunc
701
Bram Moolenaar2efb3232017-12-19 12:27:23 +0100702func Test_window_colon_command()
703 " This was reading invalid memory.
704 exe "norm! v\<C-W>:\<C-U>echo v:version"
705endfunc
706
Bram Moolenaar6f361c92018-01-31 19:06:50 +0100707func Test_access_freed_mem()
Bram Moolenaar1417c762019-07-27 17:31:36 +0200708 call assert_equal(&columns, winwidth(0))
Bram Moolenaar9a046fd2021-01-28 13:47:59 +0100709 " This was accessing freed memory (but with what events?)
710 au BufEnter,BufLeave,WinEnter,WinLeave 0 vs xxx
Bram Moolenaar6f361c92018-01-31 19:06:50 +0100711 arg 0
712 argadd
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +0200713 call assert_fails("all", "E242:")
Bram Moolenaar6f361c92018-01-31 19:06:50 +0100714 au!
715 bwipe xxx
Bram Moolenaar1417c762019-07-27 17:31:36 +0200716 call assert_equal(&columns, winwidth(0))
Bram Moolenaar6f361c92018-01-31 19:06:50 +0100717endfunc
718
Bram Moolenaara27e1dc2019-10-07 22:27:36 +0200719func Test_insert_cleared_on_switch_to_term()
720 CheckFeature terminal
721
722 set showmode
723 terminal
724 wincmd p
725
726 call feedkeys("i\<C-O>", 'ntx')
727 redraw
728
729 " The "-- (insert) --" indicator should be visible.
730 let chars = map(range(1, &columns), 'nr2char(screenchar(&lines, v:val))')
731 let str = trim(join(chars, ''))
732 call assert_equal('-- (insert) --', str)
733
734 call feedkeys("\<C-W>p", 'ntx')
735 redraw
736
737 " The "-- (insert) --" indicator should have been cleared.
738 let chars = map(range(1, &columns), 'nr2char(screenchar(&lines, v:val))')
739 let str = trim(join(chars, ''))
740 call assert_equal('', str)
741
742 set showmode&
743 %bw!
744endfunc
745
Bram Moolenaar5bab5552018-04-13 20:41:29 +0200746func Test_visual_cleared_after_window_split()
747 new | only!
748 let smd_save = &showmode
749 set showmode
750 let ls_save = &laststatus
751 set laststatus=1
752 call setline(1, ['a', 'b', 'c', 'd', ''])
753 norm! G
754 exe "norm! kkvk"
755 redraw
756 exe "norm! \<C-W>v"
757 redraw
758 " check if '-- VISUAL --' disappeared from command line
759 let columns = range(1, &columns)
760 let cmdlinechars = map(columns, 'nr2char(screenchar(&lines, v:val))')
761 let cmdline = join(cmdlinechars, '')
762 let cmdline_ltrim = substitute(cmdline, '^\s*', "", "")
763 let mode_shown = substitute(cmdline_ltrim, '\s*$', "", "")
764 call assert_equal('', mode_shown)
765 let &showmode = smd_save
766 let &laststatus = ls_save
767 bwipe!
768endfunc
769
Bram Moolenaar72cf47a2018-05-10 18:23:29 +0200770func Test_winrestcmd()
771 2split
772 3vsplit
Bram Moolenaara0c8aea2021-03-20 19:55:35 +0100773 let restcmd = winrestcmd()
Bram Moolenaar72cf47a2018-05-10 18:23:29 +0200774 call assert_equal(2, winheight(0))
775 call assert_equal(3, winwidth(0))
776 wincmd =
777 call assert_notequal(2, winheight(0))
778 call assert_notequal(3, winwidth(0))
Bram Moolenaara0c8aea2021-03-20 19:55:35 +0100779 exe restcmd
Bram Moolenaar72cf47a2018-05-10 18:23:29 +0200780 call assert_equal(2, winheight(0))
781 call assert_equal(3, winwidth(0))
782 only
Bram Moolenaara0c8aea2021-03-20 19:55:35 +0100783
784 wincmd v
785 wincmd s
786 wincmd v
787 redraw
788 let restcmd = winrestcmd()
789 wincmd _
790 wincmd |
791 exe restcmd
792 redraw
793 call assert_equal(restcmd, winrestcmd())
794
795 only
Bram Moolenaar72cf47a2018-05-10 18:23:29 +0200796endfunc
797
Bram Moolenaar1e115362019-01-09 23:01:02 +0100798func Fun_RenewFile()
Bram Moolenaar026587b2019-08-17 15:08:00 +0200799 " Need to wait a bit for the timestamp to be older.
Bram Moolenaarce90e362019-09-08 18:58:44 +0200800 let old_ftime = getftime("tmp.txt")
801 while getftime("tmp.txt") == old_ftime
802 sleep 100m
803 silent execute '!echo "1" > tmp.txt'
804 endwhile
Bram Moolenaara42df592018-12-24 00:22:39 +0100805 sp
806 wincmd p
807 edit! tmp.txt
Bram Moolenaar1e115362019-01-09 23:01:02 +0100808endfunc
Bram Moolenaara42df592018-12-24 00:22:39 +0100809
810func Test_window_prevwin()
811 " Can we make this work on MS-Windows?
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +0200812 CheckUnix
Bram Moolenaara42df592018-12-24 00:22:39 +0100813
814 set hidden autoread
Bram Moolenaardb4c9472022-10-15 22:06:06 +0100815 call writefile(['2'], 'tmp.txt', 'D')
Bram Moolenaara42df592018-12-24 00:22:39 +0100816 new tmp.txt
817 q
Bram Moolenaara42df592018-12-24 00:22:39 +0100818 call Fun_RenewFile()
819 call assert_equal(2, winnr())
820 wincmd p
821 call assert_equal(1, winnr())
822 wincmd p
823 q
824 call Fun_RenewFile()
825 call assert_equal(2, winnr())
826 wincmd p
827 call assert_equal(1, winnr())
828 wincmd p
829 " reset
830 q
Bram Moolenaara42df592018-12-24 00:22:39 +0100831 set hidden&vim autoread&vim
832 delfunc Fun_RenewFile
833endfunc
834
Bram Moolenaar8fcb60f2019-03-04 13:18:30 +0100835func Test_relative_cursor_position_in_one_line_window()
836 new
837 only
838 call setline(1, range(1, 10000))
839 normal 50%
840 let lnum = getcurpos()[1]
841 split
842 split
843 " make third window take as many lines as possible, other windows will
844 " become one line
845 3wincmd w
846 for i in range(1, &lines - 6)
847 wincmd +
848 redraw!
849 endfor
850
851 " first and second window should show cursor line
852 let wininfo = getwininfo()
853 call assert_equal(lnum, wininfo[0].topline)
854 call assert_equal(lnum, wininfo[1].topline)
855
856 only!
857 bwipe!
Yegappan Lakshmanan04c4c572022-08-30 19:48:24 +0100858 call assert_fails('call winrestview(test_null_dict())', 'E1297:')
Bram Moolenaar8fcb60f2019-03-04 13:18:30 +0100859endfunc
860
861func Test_relative_cursor_position_after_move_and_resize()
862 let so_save = &so
863 set so=0
864 enew
865 call setline(1, range(1, 10000))
866 normal 50%
867 split
868 1wincmd w
869 " Move cursor to first line in window
870 normal H
871 redraw!
872 " Reduce window height to two lines
873 let height = winheight(0)
874 while winheight(0) > 2
875 wincmd -
876 redraw!
877 endwhile
878 " move cursor to second/last line in window
879 normal j
880 " restore previous height
881 while winheight(0) < height
882 wincmd +
883 redraw!
884 endwhile
885 " make window two lines again
886 while winheight(0) > 2
887 wincmd -
888 redraw!
889 endwhile
890
891 " cursor should be at bottom line
892 let info = getwininfo(win_getid())[0]
893 call assert_equal(info.topline + 1, getcurpos()[1])
894
895 only!
896 bwipe!
897 let &so = so_save
898endfunc
899
900func Test_relative_cursor_position_after_resize()
901 let so_save = &so
902 set so=0
903 enew
904 call setline(1, range(1, 10000))
905 normal 50%
906 split
907 1wincmd w
908 let winid1 = win_getid()
909 let info = getwininfo(winid1)[0]
910 " Move cursor to second line in window
911 exe "normal " . (info.topline + 1) . "G"
912 redraw!
913 let lnum = getcurpos()[1]
914
915 " Make the window only two lines high, cursor should end up in top line
916 2wincmd w
917 exe (info.height - 2) . "wincmd +"
918 redraw!
919 let info = getwininfo(winid1)[0]
920 call assert_equal(lnum, info.topline)
921
922 only!
923 bwipe!
924 let &so = so_save
925endfunc
926
927func Test_relative_cursor_second_line_after_resize()
928 let so_save = &so
929 set so=0
930 enew
931 call setline(1, range(1, 10000))
932 normal 50%
933 split
934 1wincmd w
935 let winid1 = win_getid()
936 let info = getwininfo(winid1)[0]
937
938 " Make the window only two lines high
939 2wincmd _
940
941 " Move cursor to second line in window
942 normal H
943 normal j
944
945 " Make window size bigger, then back to 2 lines
946 for i in range(1, 10)
947 wincmd +
948 redraw!
949 endfor
950 for i in range(1, 10)
951 wincmd -
952 redraw!
953 endfor
954
955 " cursor should end up in bottom line
956 let info = getwininfo(winid1)[0]
957 call assert_equal(info.topline + 1, getcurpos()[1])
958
959 only!
960 bwipe!
961 let &so = so_save
962endfunc
963
Bram Moolenaara9b25352019-05-12 14:25:30 +0200964func Test_split_noscroll()
965 let so_save = &so
Bram Moolenaarbd2d68c2019-05-18 15:36:11 +0200966 enew
967 call setline(1, range(1, 8))
Bram Moolenaara9b25352019-05-12 14:25:30 +0200968 normal 100%
969 split
970
971 1wincmd w
972 let winid1 = win_getid()
973 let info1 = getwininfo(winid1)[0]
974
975 2wincmd w
976 let winid2 = win_getid()
977 let info2 = getwininfo(winid2)[0]
978
979 call assert_equal(1, info1.topline)
980 call assert_equal(1, info2.topline)
981
Bram Moolenaarbd2d68c2019-05-18 15:36:11 +0200982 " window that fits all lines by itself, but not when split: closing other
983 " window should restore fraction.
Bram Moolenaara9b25352019-05-12 14:25:30 +0200984 only!
Bram Moolenaarbd2d68c2019-05-18 15:36:11 +0200985 call setline(1, range(1, &lines - 10))
986 exe &lines / 4
987 let winid1 = win_getid()
988 let info1 = getwininfo(winid1)[0]
989 call assert_equal(1, info1.topline)
990 new
991 redraw
992 close
993 let info1 = getwininfo(winid1)[0]
994 call assert_equal(1, info1.topline)
995
Bram Moolenaara9b25352019-05-12 14:25:30 +0200996 bwipe!
997 let &so = so_save
998endfunc
999
Bram Moolenaar46ad2882019-04-08 20:01:47 +02001000" Tests for the winnr() function
1001func Test_winnr()
1002 only | tabonly
1003 call assert_equal(1, winnr('j'))
1004 call assert_equal(1, winnr('k'))
1005 call assert_equal(1, winnr('h'))
1006 call assert_equal(1, winnr('l'))
1007
1008 " create a set of horizontally and vertically split windows
1009 leftabove new | wincmd p
1010 leftabove new | wincmd p
1011 rightbelow new | wincmd p
1012 rightbelow new | wincmd p
1013 leftabove vnew | wincmd p
1014 leftabove vnew | wincmd p
1015 rightbelow vnew | wincmd p
1016 rightbelow vnew | wincmd p
1017
1018 call assert_equal(8, winnr('j'))
1019 call assert_equal(2, winnr('k'))
1020 call assert_equal(4, winnr('h'))
1021 call assert_equal(6, winnr('l'))
1022 call assert_equal(9, winnr('2j'))
1023 call assert_equal(1, winnr('2k'))
1024 call assert_equal(3, winnr('2h'))
1025 call assert_equal(7, winnr('2l'))
1026
1027 " Error cases
1028 call assert_fails("echo winnr('0.2k')", 'E15:')
1029 call assert_equal(2, winnr('-2k'))
1030 call assert_fails("echo winnr('-2xj')", 'E15:')
1031 call assert_fails("echo winnr('j2j')", 'E15:')
1032 call assert_fails("echo winnr('ll')", 'E15:')
1033 call assert_fails("echo winnr('5')", 'E15:')
1034 call assert_equal(4, winnr('0h'))
Bram Moolenaar99fa7212020-04-26 15:59:55 +02001035 call assert_fails("let w = winnr([])", 'E730:')
1036 call assert_equal('unknown', win_gettype(-1))
1037 call assert_equal(-1, winheight(-1))
1038 call assert_equal(-1, winwidth(-1))
Bram Moolenaar46ad2882019-04-08 20:01:47 +02001039
1040 tabnew
1041 call assert_equal(8, tabpagewinnr(1, 'j'))
Bram Moolenaarce90e362019-09-08 18:58:44 +02001042 call assert_equal(2, 1->tabpagewinnr('k'))
Bram Moolenaar46ad2882019-04-08 20:01:47 +02001043 call assert_equal(4, tabpagewinnr(1, 'h'))
1044 call assert_equal(6, tabpagewinnr(1, 'l'))
1045
1046 only | tabonly
1047endfunc
1048
Bram Moolenaarf92e58c2019-09-08 21:51:41 +02001049func Test_winrestview()
1050 split runtest.vim
1051 normal 50%
1052 let view = winsaveview()
1053 close
1054 split runtest.vim
1055 eval view->winrestview()
1056 call assert_equal(view, winsaveview())
1057
1058 bwipe!
Yegappan Lakshmanan04c4c572022-08-30 19:48:24 +01001059 call assert_fails('call winrestview(test_null_dict())', 'E1297:')
Bram Moolenaarf92e58c2019-09-08 21:51:41 +02001060endfunc
1061
Bram Moolenaard20dcb32019-09-10 21:22:58 +02001062func Test_win_splitmove()
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01001063 CheckFeature quickfix
1064
Bram Moolenaard20dcb32019-09-10 21:22:58 +02001065 edit a
1066 leftabove split b
1067 leftabove vsplit c
1068 leftabove split d
1069 call assert_equal(0, win_splitmove(winnr(), winnr('l')))
1070 call assert_equal(bufname(winbufnr(1)), 'c')
1071 call assert_equal(bufname(winbufnr(2)), 'd')
1072 call assert_equal(bufname(winbufnr(3)), 'b')
1073 call assert_equal(bufname(winbufnr(4)), 'a')
1074 call assert_equal(0, win_splitmove(winnr(), winnr('j'), {'vertical': 1}))
1075 call assert_equal(0, win_splitmove(winnr(), winnr('j'), {'vertical': 1}))
1076 call assert_equal(bufname(winbufnr(1)), 'c')
1077 call assert_equal(bufname(winbufnr(2)), 'b')
1078 call assert_equal(bufname(winbufnr(3)), 'd')
1079 call assert_equal(bufname(winbufnr(4)), 'a')
1080 call assert_equal(0, win_splitmove(winnr(), winnr('k'), {'vertical': 1}))
1081 call assert_equal(bufname(winbufnr(1)), 'd')
1082 call assert_equal(bufname(winbufnr(2)), 'c')
1083 call assert_equal(bufname(winbufnr(3)), 'b')
1084 call assert_equal(bufname(winbufnr(4)), 'a')
1085 call assert_equal(0, win_splitmove(winnr(), winnr('j'), {'rightbelow': v:true}))
1086 call assert_equal(bufname(winbufnr(1)), 'c')
1087 call assert_equal(bufname(winbufnr(2)), 'b')
1088 call assert_equal(bufname(winbufnr(3)), 'a')
1089 call assert_equal(bufname(winbufnr(4)), 'd')
Yegappan Lakshmanan04c4c572022-08-30 19:48:24 +01001090 call assert_fails('call win_splitmove(winnr(), winnr("k"), test_null_dict())', 'E1297:')
Bram Moolenaard20dcb32019-09-10 21:22:58 +02001091 only | bd
1092
1093 call assert_fails('call win_splitmove(winnr(), 123)', 'E957:')
1094 call assert_fails('call win_splitmove(123, winnr())', 'E957:')
1095 call assert_fails('call win_splitmove(winnr(), winnr())', 'E957:')
Bram Moolenaar7b94e772020-01-06 21:03:24 +01001096
1097 tabnew
1098 call assert_fails('call win_splitmove(1, win_getid(1, 1))', 'E957:')
1099 tabclose
Bram Moolenaard20dcb32019-09-10 21:22:58 +02001100endfunc
1101
Bram Moolenaare20b9ec2020-02-03 21:40:04 +01001102" Test for the :only command
1103func Test_window_only()
1104 new
1105 set modified
1106 new
1107 call assert_fails('only', 'E445:')
1108 only!
Bram Moolenaarbc2b71d2020-02-17 21:33:30 +01001109 " Test for :only with a count
1110 let wid = win_getid()
1111 new
1112 new
1113 3only
1114 call assert_equal(1, winnr('$'))
1115 call assert_equal(wid, win_getid())
1116 call assert_fails('close', 'E444:')
1117 call assert_fails('%close', 'E16:')
Bram Moolenaare20b9ec2020-02-03 21:40:04 +01001118endfunc
1119
Bram Moolenaar9f6277b2020-02-11 22:04:02 +01001120" Test for errors with :wincmd
1121func Test_wincmd_errors()
1122 call assert_fails('wincmd g', 'E474:')
1123 call assert_fails('wincmd ab', 'E474:')
1124endfunc
1125
1126" Test for errors with :winpos
1127func Test_winpos_errors()
1128 if !has("gui_running") && !has('win32')
1129 call assert_fails('winpos', 'E188:')
1130 endif
1131 call assert_fails('winpos 10', 'E466:')
1132endfunc
1133
Bram Moolenaar406cd902020-02-18 21:54:41 +01001134" Test for +cmd in a :split command
1135func Test_split_cmd()
1136 split +set\ readonly
1137 call assert_equal(1, &readonly)
1138 call assert_equal(2, winnr('$'))
1139 close
1140endfunc
1141
Bram Moolenaar5080b0a2020-03-22 21:23:47 +01001142" Create maximum number of horizontally or vertically split windows and then
1143" run commands that create a new horizontally/vertically split window
1144func Run_noroom_for_newwindow_test(dir_arg)
1145 let dir = (a:dir_arg == 'v') ? 'vert ' : ''
1146
1147 " Open as many windows as possible
Bram Moolenaarab505b12020-03-23 19:28:44 +01001148 while v:true
Bram Moolenaar5080b0a2020-03-22 21:23:47 +01001149 try
1150 exe dir . 'new'
1151 catch /E36:/
1152 break
1153 endtry
Bram Moolenaarab505b12020-03-23 19:28:44 +01001154 endwhile
Bram Moolenaar5080b0a2020-03-22 21:23:47 +01001155
Bram Moolenaardb4c9472022-10-15 22:06:06 +01001156 call writefile(['first', 'second', 'third'], 'Xnorfile1', 'D')
1157 call writefile([], 'Xnorfile2', 'D')
1158 call writefile([], 'Xnorfile3', 'D')
Bram Moolenaar5080b0a2020-03-22 21:23:47 +01001159
1160 " Argument list related commands
Bram Moolenaarb18b4962022-09-02 21:55:50 +01001161 args Xnorfile1 Xnorfile2 Xnorfile3
Bram Moolenaar5080b0a2020-03-22 21:23:47 +01001162 next
1163 for cmd in ['sargument 2', 'snext', 'sprevious', 'sNext', 'srewind',
1164 \ 'sfirst', 'slast']
1165 call assert_fails(dir .. cmd, 'E36:')
1166 endfor
1167 %argdelete
1168
1169 " Buffer related commands
1170 set modified
1171 hide enew
Bram Moolenaarb18b4962022-09-02 21:55:50 +01001172 for cmd in ['sbuffer Xnorfile1', 'sbnext', 'sbprevious', 'sbNext', 'sbrewind',
Bram Moolenaar5080b0a2020-03-22 21:23:47 +01001173 \ 'sbfirst', 'sblast', 'sball', 'sbmodified', 'sunhide']
1174 call assert_fails(dir .. cmd, 'E36:')
1175 endfor
1176
1177 " Window related commands
Bram Moolenaarb18b4962022-09-02 21:55:50 +01001178 for cmd in ['split', 'split Xnorfile2', 'new', 'new Xnorfile3', 'sview Xnorfile1',
Bram Moolenaar5080b0a2020-03-22 21:23:47 +01001179 \ 'sfind runtest.vim']
1180 call assert_fails(dir .. cmd, 'E36:')
1181 endfor
1182
1183 " Help
1184 call assert_fails(dir .. 'help', 'E36:')
1185 call assert_fails(dir .. 'helpgrep window', 'E36:')
1186
1187 " Command-line window
1188 if a:dir_arg == 'h'
1189 " Cmd-line window is always a horizontally split window
1190 call assert_beeps('call feedkeys("q:\<CR>", "xt")')
1191 endif
1192
1193 " Quickfix and location list window
1194 if has('quickfix')
1195 cexpr ''
1196 call assert_fails(dir .. 'copen', 'E36:')
1197 lexpr ''
1198 call assert_fails(dir .. 'lopen', 'E36:')
1199
1200 " Preview window
Bram Moolenaarb18b4962022-09-02 21:55:50 +01001201 call assert_fails(dir .. 'pedit Xnorfile2', 'E36:')
Bram Moolenaar5080b0a2020-03-22 21:23:47 +01001202 call setline(1, 'abc')
1203 call assert_fails(dir .. 'psearch abc', 'E36:')
1204 endif
1205
1206 " Window commands (CTRL-W ^ and CTRL-W f)
1207 if a:dir_arg == 'h'
1208 call assert_fails('call feedkeys("\<C-W>^", "xt")', 'E36:')
Bram Moolenaarb18b4962022-09-02 21:55:50 +01001209 call setline(1, 'Xnorfile1')
Bram Moolenaar5080b0a2020-03-22 21:23:47 +01001210 call assert_fails('call feedkeys("gg\<C-W>f", "xt")', 'E36:')
1211 endif
1212 enew!
1213
1214 " Tag commands (:stag, :stselect and :stjump)
1215 call writefile(["!_TAG_FILE_ENCODING\tutf-8\t//",
Bram Moolenaarb18b4962022-09-02 21:55:50 +01001216 \ "second\tXnorfile1\t2",
1217 \ "third\tXnorfile1\t3",],
Bram Moolenaar5080b0a2020-03-22 21:23:47 +01001218 \ 'Xtags')
1219 set tags=Xtags
1220 call assert_fails(dir .. 'stag second', 'E36:')
1221 call assert_fails('call feedkeys(":" .. dir .. "stselect second\n1\n", "xt")', 'E36:')
1222 call assert_fails(dir .. 'stjump second', 'E36:')
1223 call assert_fails(dir .. 'ptag second', 'E36:')
1224 set tags&
1225 call delete('Xtags')
1226
1227 " :isplit and :dsplit
1228 call setline(1, ['#define FOO 1', 'FOO'])
1229 normal 2G
1230 call assert_fails(dir .. 'isplit FOO', 'E36:')
1231 call assert_fails(dir .. 'dsplit FOO', 'E36:')
1232
1233 " terminal
1234 if has('terminal')
1235 call assert_fails(dir .. 'terminal', 'E36:')
1236 endif
1237
1238 %bwipe!
Bram Moolenaar5080b0a2020-03-22 21:23:47 +01001239 only
1240endfunc
1241
1242func Test_split_cmds_with_no_room()
1243 call Run_noroom_for_newwindow_test('h')
1244 call Run_noroom_for_newwindow_test('v')
1245endfunc
1246
Bram Moolenaarad48e6c2020-04-21 22:19:45 +02001247" Test for various wincmd failures
1248func Test_wincmd_fails()
1249 only!
1250 call assert_beeps("normal \<C-W>w")
1251 call assert_beeps("normal \<C-W>p")
1252 call assert_beeps("normal \<C-W>gk")
1253 call assert_beeps("normal \<C-W>r")
1254 call assert_beeps("normal \<C-W>K")
1255 call assert_beeps("normal \<C-W>H")
1256 call assert_beeps("normal \<C-W>2gt")
1257endfunc
1258
Bram Moolenaarfe6dce82020-09-04 14:41:21 +02001259func Test_window_resize()
1260 " Vertical :resize (absolute, relative, min and max size).
1261 vsplit
1262 vert resize 8
1263 call assert_equal(8, winwidth(0))
1264 vert resize +2
1265 call assert_equal(10, winwidth(0))
1266 vert resize -2
1267 call assert_equal(8, winwidth(0))
1268 vert resize
1269 call assert_equal(&columns - 2, winwidth(0))
1270 vert resize 0
1271 call assert_equal(1, winwidth(0))
1272 vert resize 99999
1273 call assert_equal(&columns - 2, winwidth(0))
1274
1275 %bwipe!
1276
1277 " Horizontal :resize (with absolute, relative size, min and max size).
1278 split
1279 resize 8
1280 call assert_equal(8, winheight(0))
1281 resize +2
1282 call assert_equal(10, winheight(0))
1283 resize -2
1284 call assert_equal(8, winheight(0))
1285 resize
1286 call assert_equal(&lines - 4, winheight(0))
1287 resize 0
1288 call assert_equal(1, winheight(0))
1289 resize 99999
1290 call assert_equal(&lines - 4, winheight(0))
1291
1292 " :resize with explicit window number.
1293 let other_winnr = winnr('j')
1294 exe other_winnr .. 'resize 10'
1295 call assert_equal(10, winheight(other_winnr))
1296 call assert_equal(&lines - 10 - 3, winheight(0))
Bram Moolenaar9668cc52020-10-17 17:39:55 +02001297 exe other_winnr .. 'resize +1'
1298 exe other_winnr .. 'resize +1'
1299 call assert_equal(12, winheight(other_winnr))
1300 call assert_equal(&lines - 10 - 3 -2, winheight(0))
Bram Moolenaar89015a62020-12-29 12:46:51 +01001301 close
1302
1303 vsplit
1304 wincmd l
1305 let other_winnr = winnr('h')
1306 call assert_notequal(winnr(), other_winnr)
Bram Moolenaar5efe0e52021-01-01 14:31:34 +01001307 exe 'vert ' .. other_winnr .. 'resize -' .. &columns
Bram Moolenaar89015a62020-12-29 12:46:51 +01001308 call assert_equal(0, winwidth(other_winnr))
Bram Moolenaarfe6dce82020-09-04 14:41:21 +02001309
1310 %bwipe!
1311endfunc
1312
Bram Moolenaar5d3c9f82020-06-26 20:41:39 +02001313" Test for adjusting the window width when a window is closed with some
1314" windows using 'winfixwidth'
1315func Test_window_width_adjust()
1316 only
1317 " Three vertical windows. Windows 1 and 2 have 'winfixwidth' set and close
1318 " window 2.
1319 wincmd v
1320 vert resize 10
1321 set winfixwidth
1322 wincmd v
1323 set winfixwidth
1324 wincmd c
1325 call assert_inrange(10, 12, winwidth(1))
1326 " Three vertical windows. Windows 2 and 3 have 'winfixwidth' set and close
1327 " window 3.
1328 only
1329 set winfixwidth
1330 wincmd v
1331 vert resize 10
1332 set winfixwidth
1333 wincmd v
1334 set nowinfixwidth
1335 wincmd b
1336 wincmd c
1337 call assert_inrange(10, 12, winwidth(2))
1338
1339 new | only
1340endfunc
1341
1342" Test for jumping to a vertical/horizontal neighbor window based on the
1343" current cursor position
dundargocc57b5bc2022-11-02 13:30:51 +00001344func Test_window_goto_neighbor()
Bram Moolenaar5d3c9f82020-06-26 20:41:39 +02001345 %bw!
1346
1347 " Vertical window movement
1348
1349 " create the following window layout:
1350 " +--+--+
1351 " |w1|w3|
1352 " +--+ |
1353 " |w2| |
1354 " +--+--+
1355 " |w4 |
1356 " +-----+
1357 new
1358 vsplit
1359 split
1360 " vertically jump from w4
1361 wincmd b
1362 call setline(1, repeat(' ', &columns))
1363 call cursor(1, 1)
1364 wincmd k
1365 call assert_equal(2, winnr())
1366 wincmd b
1367 call cursor(1, &columns)
1368 redraw!
1369 wincmd k
1370 call assert_equal(3, winnr())
1371 %bw!
1372
1373 " create the following window layout:
1374 " +--+--+--+
1375 " |w1|w2|w3|
1376 " +--+--+--+
1377 " |w4 |
1378 " +--------+
1379 new
1380 vsplit
1381 vsplit
1382 wincmd b
1383 call setline(1, repeat(' ', &columns))
1384 call cursor(1, 1)
1385 wincmd k
1386 call assert_equal(1, winnr())
1387 wincmd b
1388 call cursor(1, &columns / 2)
1389 redraw!
1390 wincmd k
1391 call assert_equal(2, winnr())
1392 wincmd b
1393 call cursor(1, &columns)
1394 redraw!
1395 wincmd k
1396 call assert_equal(3, winnr())
1397 %bw!
1398
1399 " Horizontal window movement
1400
1401 " create the following window layout:
1402 " +--+--+--+
1403 " |w1|w2|w4|
1404 " +--+--+ |
1405 " |w3 | |
1406 " +-----+--+
1407 vsplit
1408 split
1409 vsplit
1410 4wincmd l
1411 call setline(1, repeat([' '], &lines))
1412 call cursor(1, 1)
1413 redraw!
1414 wincmd h
1415 call assert_equal(2, winnr())
1416 4wincmd l
1417 call cursor(&lines, 1)
1418 redraw!
1419 wincmd h
1420 call assert_equal(3, winnr())
1421 %bw!
1422
1423 " create the following window layout:
1424 " +--+--+
1425 " |w1|w4|
1426 " +--+ +
1427 " |w2| |
1428 " +--+ +
1429 " |w3| |
1430 " +--+--+
1431 vsplit
1432 split
1433 split
1434 wincmd l
1435 call setline(1, repeat([' '], &lines))
1436 call cursor(1, 1)
1437 redraw!
1438 wincmd h
1439 call assert_equal(1, winnr())
1440 wincmd l
1441 call cursor(&lines / 2, 1)
1442 redraw!
1443 wincmd h
1444 call assert_equal(2, winnr())
1445 wincmd l
1446 call cursor(&lines, 1)
1447 redraw!
1448 wincmd h
1449 call assert_equal(3, winnr())
1450 %bw!
1451endfunc
1452
1453" Test for an autocmd closing the destination window when jumping from one
1454" window to another.
1455func Test_close_dest_window()
1456 split
Bram Moolenaarb18b4962022-09-02 21:55:50 +01001457 edit Xdstfile
Bram Moolenaar5d3c9f82020-06-26 20:41:39 +02001458
1459 " Test for BufLeave
1460 augroup T1
1461 au!
Bram Moolenaarb18b4962022-09-02 21:55:50 +01001462 au BufLeave Xdstfile $wincmd c
Bram Moolenaar5d3c9f82020-06-26 20:41:39 +02001463 augroup END
1464 wincmd b
1465 call assert_equal(1, winnr('$'))
Bram Moolenaarb18b4962022-09-02 21:55:50 +01001466 call assert_equal('Xdstfile', @%)
Bram Moolenaar5d3c9f82020-06-26 20:41:39 +02001467 augroup T1
1468 au!
1469 augroup END
1470
1471 " Test for WinLeave
1472 new
1473 wincmd p
1474 augroup T1
1475 au!
1476 au WinLeave * 1wincmd c
1477 augroup END
1478 wincmd t
1479 call assert_equal(1, winnr('$'))
Bram Moolenaarb18b4962022-09-02 21:55:50 +01001480 call assert_equal('Xdstfile', @%)
Bram Moolenaar5d3c9f82020-06-26 20:41:39 +02001481 augroup T1
1482 au!
1483 augroup END
1484 augroup! T1
1485 %bw!
1486endfunc
1487
Bram Moolenaard0fb9072021-12-09 11:57:22 +00001488func Test_window_minimal_size()
1489 set winminwidth=0 winminheight=0
1490
1491 " check size is fixed vertically
1492 new
1493 call win_execute(win_getid(2), 'wincmd _')
1494 call assert_equal(0, winheight(0))
1495 call feedkeys('0', 'tx')
1496 call assert_equal(1, winheight(0))
1497 bwipe!
1498
1499 " check size is fixed horizontally
1500 vert new
1501 call win_execute(win_getid(2), 'wincmd |')
1502 call assert_equal(0, winwidth(0))
1503 call feedkeys('0', 'tx')
1504 call assert_equal(1, winwidth(0))
1505 bwipe!
1506
1507 if has('timers')
1508 " check size is fixed in Insert mode
ichizokfa9a8e02021-12-12 16:42:09 +00001509 func s:CheckSize(timer) abort
1510 call win_execute(win_getid(2), 'wincmd _')
1511 call assert_equal(0, winheight(0))
1512 call feedkeys(" \<Esc>", 't!')
1513 endfunc
Bram Moolenaard0fb9072021-12-09 11:57:22 +00001514 new
ichizokfa9a8e02021-12-12 16:42:09 +00001515 call timer_start(100, function('s:CheckSize'))
Bram Moolenaard0fb9072021-12-09 11:57:22 +00001516 call feedkeys('a', 'tx!')
1517 call assert_equal(1, winheight(0))
1518 bwipe!
ichizokfa9a8e02021-12-12 16:42:09 +00001519 delfunc s:CheckSize
Bram Moolenaard0fb9072021-12-09 11:57:22 +00001520 endif
1521
1522 set winminwidth& winminheight&
1523endfunc
1524
Daniel Steinbergee630312022-01-10 13:36:34 +00001525func Test_win_move_separator()
1526 edit a
1527 leftabove vsplit b
1528 let w = winwidth(0)
1529 " check win_move_separator from left window on left window
1530 call assert_equal(1, winnr())
1531 for offset in range(5)
1532 call assert_true(win_move_separator(0, offset))
1533 call assert_equal(w + offset, winwidth(0))
1534 call assert_true(0->win_move_separator(-offset))
1535 call assert_equal(w, winwidth(0))
1536 endfor
1537 " check win_move_separator from right window on left window number
1538 wincmd l
1539 call assert_notequal(1, winnr())
1540 for offset in range(5)
1541 call assert_true(1->win_move_separator(offset))
1542 call assert_equal(w + offset, winwidth(1))
1543 call assert_true(win_move_separator(1, -offset))
1544 call assert_equal(w, winwidth(1))
1545 endfor
1546 " check win_move_separator from right window on left window ID
1547 let id = win_getid(1)
1548 for offset in range(5)
1549 call assert_true(win_move_separator(id, offset))
1550 call assert_equal(w + offset, winwidth(id))
1551 call assert_true(id->win_move_separator(-offset))
1552 call assert_equal(w, winwidth(id))
1553 endfor
zeertzjqa0c4e2f2022-01-29 10:59:53 +00001554 " check win_move_separator from right window on right window is no-op
1555 let w0 = winwidth(0)
1556 call assert_true(win_move_separator(0, 1))
1557 call assert_equal(w0, winwidth(0))
1558 call assert_true(win_move_separator(0, -1))
1559 call assert_equal(w0, winwidth(0))
zeertzjq873f41a2022-11-01 11:44:43 +00001560
Daniel Steinbergee630312022-01-10 13:36:34 +00001561 " check that win_move_separator doesn't error with offsets beyond moving
1562 " possibility
1563 call assert_true(win_move_separator(id, 5000))
1564 call assert_true(winwidth(id) > w)
1565 call assert_true(win_move_separator(id, -5000))
1566 call assert_true(winwidth(id) < w)
zeertzjq873f41a2022-11-01 11:44:43 +00001567
Daniel Steinbergee630312022-01-10 13:36:34 +00001568 " check that win_move_separator returns false for an invalid window
1569 wincmd =
1570 let w = winwidth(0)
1571 call assert_false(win_move_separator(-1, 1))
1572 call assert_equal(w, winwidth(0))
zeertzjq873f41a2022-11-01 11:44:43 +00001573
Daniel Steinbergee630312022-01-10 13:36:34 +00001574 " check that win_move_separator returns false for a popup window
1575 let id = popup_create(['hello', 'world'], {})
1576 let w = winwidth(id)
1577 call assert_false(win_move_separator(id, 1))
1578 call assert_equal(w, winwidth(id))
1579 call popup_close(id)
zeertzjq873f41a2022-11-01 11:44:43 +00001580
1581 " check that using another tabpage fails without crash
1582 let id = win_getid()
1583 tabnew
1584 call assert_fails('call win_move_separator(id, -1)', 'E1308:')
1585 tabclose
1586
Daniel Steinbergee630312022-01-10 13:36:34 +00001587 %bwipe!
1588endfunc
1589
1590func Test_win_move_statusline()
1591 edit a
1592 leftabove split b
1593 let h = winheight(0)
1594 " check win_move_statusline from top window on top window
1595 call assert_equal(1, winnr())
1596 for offset in range(5)
1597 call assert_true(win_move_statusline(0, offset))
1598 call assert_equal(h + offset, winheight(0))
1599 call assert_true(0->win_move_statusline(-offset))
1600 call assert_equal(h, winheight(0))
1601 endfor
1602 " check win_move_statusline from bottom window on top window number
1603 wincmd j
1604 call assert_notequal(1, winnr())
1605 for offset in range(5)
1606 call assert_true(1->win_move_statusline(offset))
1607 call assert_equal(h + offset, winheight(1))
1608 call assert_true(win_move_statusline(1, -offset))
1609 call assert_equal(h, winheight(1))
1610 endfor
zeertzjqa0c4e2f2022-01-29 10:59:53 +00001611 " check win_move_statusline from bottom window on bottom window
1612 let h0 = winheight(0)
1613 for offset in range(5)
1614 call assert_true(0->win_move_statusline(-offset))
1615 call assert_equal(h0 - offset, winheight(0))
1616 call assert_equal(1 + offset, &cmdheight)
1617 call assert_true(win_move_statusline(0, offset))
1618 call assert_equal(h0, winheight(0))
1619 call assert_equal(1, &cmdheight)
1620 endfor
1621 call assert_true(win_move_statusline(0, 1))
Bram Moolenaara2a89732022-08-31 14:46:18 +01001622 call assert_equal(h0, winheight(0))
1623 call assert_equal(1, &cmdheight)
Daniel Steinbergee630312022-01-10 13:36:34 +00001624 " check win_move_statusline from bottom window on top window ID
1625 let id = win_getid(1)
1626 for offset in range(5)
1627 call assert_true(win_move_statusline(id, offset))
1628 call assert_equal(h + offset, winheight(id))
1629 call assert_true(id->win_move_statusline(-offset))
1630 call assert_equal(h, winheight(id))
1631 endfor
Bram Moolenaar86e67172022-10-31 12:24:12 +00001632
Daniel Steinbergee630312022-01-10 13:36:34 +00001633 " check that win_move_statusline doesn't error with offsets beyond moving
1634 " possibility
1635 call assert_true(win_move_statusline(id, 5000))
1636 call assert_true(winheight(id) > h)
1637 call assert_true(win_move_statusline(id, -5000))
1638 call assert_true(winheight(id) < h)
Bram Moolenaar86e67172022-10-31 12:24:12 +00001639
Daniel Steinbergee630312022-01-10 13:36:34 +00001640 " check that win_move_statusline returns false for an invalid window
1641 wincmd =
1642 let h = winheight(0)
1643 call assert_false(win_move_statusline(-1, 1))
1644 call assert_equal(h, winheight(0))
Bram Moolenaar86e67172022-10-31 12:24:12 +00001645
Daniel Steinbergee630312022-01-10 13:36:34 +00001646 " check that win_move_statusline returns false for a popup window
1647 let id = popup_create(['hello', 'world'], {})
1648 let h = winheight(id)
1649 call assert_false(win_move_statusline(id, 1))
1650 call assert_equal(h, winheight(id))
1651 call popup_close(id)
Bram Moolenaar86e67172022-10-31 12:24:12 +00001652
1653 " check that using another tabpage fails without crash
1654 let id = win_getid()
1655 tabnew
1656 call assert_fails('call win_move_statusline(id, -1)', 'E1308:')
1657 tabclose
1658
Daniel Steinbergee630312022-01-10 13:36:34 +00001659 %bwipe!
1660endfunc
1661
Yegappan Lakshmanan72bb47e2022-04-03 11:22:38 +01001662" Test for window allocation failure
1663func Test_window_alloc_failure()
1664 %bw!
1665
1666 " test for creating a new window above current window
1667 call test_alloc_fail(GetAllocId('newwin_wvars'), 0, 0)
1668 call assert_fails('above new', 'E342:')
1669 call assert_equal(1, winnr('$'))
1670
1671 " test for creating a new window below current window
1672 call test_alloc_fail(GetAllocId('newwin_wvars'), 0, 0)
1673 call assert_fails('below new', 'E342:')
1674 call assert_equal(1, winnr('$'))
1675
1676 " test for popup window creation failure
1677 call test_alloc_fail(GetAllocId('newwin_wvars'), 0, 0)
1678 call assert_fails('call popup_create("Hello", {})', 'E342:')
1679 call assert_equal([], popup_list())
1680
1681 call test_alloc_fail(GetAllocId('newwin_wvars'), 0, 0)
1682 call assert_fails('split', 'E342:')
1683 call assert_equal(1, winnr('$'))
1684
Bram Moolenaarb18b4962022-09-02 21:55:50 +01001685 edit Xwaffile1
1686 edit Xwaffile2
Yegappan Lakshmanan72bb47e2022-04-03 11:22:38 +01001687 call test_alloc_fail(GetAllocId('newwin_wvars'), 0, 0)
Bram Moolenaarb18b4962022-09-02 21:55:50 +01001688 call assert_fails('sb Xwaffile1', 'E342:')
Yegappan Lakshmanan72bb47e2022-04-03 11:22:38 +01001689 call assert_equal(1, winnr('$'))
Bram Moolenaarb18b4962022-09-02 21:55:50 +01001690 call assert_equal('Xwaffile2', @%)
Yegappan Lakshmanan72bb47e2022-04-03 11:22:38 +01001691 %bw!
1692
1693 " FIXME: The following test crashes Vim
1694 " test for new tabpage creation failure
1695 " call test_alloc_fail(GetAllocId('newwin_wvars'), 0, 0)
1696 " call assert_fails('tabnew', 'E342:')
1697 " call assert_equal(1, tabpagenr('$'))
1698 " call assert_equal(1, winnr('$'))
1699
1700 " This test messes up the internal Vim window/frame information. So the
1701 " Test_window_cmd_cmdwin_with_vsp() test fails after running this test.
1702 " Open a new tab and close everything else to fix this issue.
1703 tabnew
1704 tabonly
1705endfunc
Bram Moolenaard0fb9072021-12-09 11:57:22 +00001706
Luuk van Baalfd7e60a2022-09-07 14:42:49 +01001707func Test_win_equal_last_status()
1708 let save_lines = &lines
1709 set lines=20
1710 set splitbelow
1711 set laststatus=0
1712
1713 split | split | quit
1714 call assert_equal(winheight(1), winheight(2))
1715
1716 let &lines = save_lines
1717 set splitbelow&
1718 set laststatus&
1719endfunc
1720
Luuk van Baal13ece2a2022-10-03 15:28:08 +01001721" Test "screen" and "cursor" values for 'splitkeep' with a sequence of
Luuk van Baalfaf1d412022-09-19 16:45:29 +01001722" split operations for various options: with and without a winbar,
1723" tabline, for each possible value of 'laststatus', 'scrolloff',
Luuk van Baal13ece2a2022-10-03 15:28:08 +01001724" 'equalalways', and with the cursor at the top, middle and bottom.
1725func Test_splitkeep_options()
Luuk van Baal594f9e02022-09-16 12:52:58 +01001726 " disallow window resizing
1727 let save_WS = &t_WS
1728 set t_WS=
1729
Luuk van Baal29ab5242022-09-11 16:59:53 +01001730 let gui = has("gui_running")
Luuk van Baal594f9e02022-09-16 12:52:58 +01001731 inoremap <expr> c "<cmd>copen<bar>wincmd k<CR>"
Luuk van Baal13ece2a2022-10-03 15:28:08 +01001732 for run in range(0, 20)
1733 let &splitkeep = run > 10 ? 'topline' : 'screen'
1734 let &scrolloff = (!(run % 4) ? 0 : run)
1735 let &laststatus = (run % 3)
1736 let &splitbelow = (run % 3)
1737 let &equalalways = (run % 2)
1738 let wsb = (run % 2) && &splitbelow
1739 let tl = (gui ? 0 : ((run % 5) ? 1 : 0))
1740 let pos = !(run % 3) ? 'H' : ((run % 2) ? 'M' : 'L')
1741 tabnew | tabonly! | redraw
Luuk van Baal594f9e02022-09-16 12:52:58 +01001742 execute (run % 5) ? 'tabnew' : ''
1743 execute (run % 2) ? 'nnoremenu 1.10 WinBar.Test :echo' : ''
Luuk van Baal594f9e02022-09-16 12:52:58 +01001744 call setline(1, range(1, 256))
1745 " No scroll for restore_snapshot
1746 norm G
1747 try
1748 copen | close | colder
1749 catch /E380/
1750 endtry
1751 call assert_equal(257 - winheight(0), line("w0"))
Luuk van Baal29ab5242022-09-11 16:59:53 +01001752
Luuk van Baal594f9e02022-09-16 12:52:58 +01001753 " No scroll for firstwin horizontal split
1754 execute 'norm gg' . pos
1755 split | redraw | wincmd k
1756 call assert_equal(1, line("w0"))
1757 call assert_equal(&scroll, winheight(0) / 2)
1758 wincmd j
Luuk van Baal13ece2a2022-10-03 15:28:08 +01001759 call assert_equal(&spk == 'topline' ? 1 : win_screenpos(0)[0] - tl - wsb, line("w0"))
Luuk van Baal29ab5242022-09-11 16:59:53 +01001760
Luuk van Baal594f9e02022-09-16 12:52:58 +01001761 " No scroll when resizing windows
Luuk van Baal13ece2a2022-10-03 15:28:08 +01001762 wincmd k | resize +2 | redraw
Luuk van Baal594f9e02022-09-16 12:52:58 +01001763 call assert_equal(1, line("w0"))
1764 wincmd j
Luuk van Baal13ece2a2022-10-03 15:28:08 +01001765 call assert_equal(&spk == 'topline' ? 1 : win_screenpos(0)[0] - tl - wsb, line("w0"))
Luuk van Baal29ab5242022-09-11 16:59:53 +01001766
Luuk van Baal594f9e02022-09-16 12:52:58 +01001767 " No scroll when dragging statusline
1768 call win_move_statusline(1, -3)
Luuk van Baal13ece2a2022-10-03 15:28:08 +01001769 call assert_equal(&spk == 'topline' ? 1 : win_screenpos(0)[0] - tl - wsb, line("w0"))
Luuk van Baal594f9e02022-09-16 12:52:58 +01001770 wincmd k
1771 call assert_equal(1, line("w0"))
Luuk van Baal29ab5242022-09-11 16:59:53 +01001772
Luuk van Baal594f9e02022-09-16 12:52:58 +01001773 " No scroll when changing shellsize
1774 set lines+=2
1775 call assert_equal(1, line("w0"))
1776 wincmd j
Luuk van Baal13ece2a2022-10-03 15:28:08 +01001777 call assert_equal(&spk == 'topline' ? 1 : win_screenpos(0)[0] - tl - wsb, line("w0"))
Luuk van Baal594f9e02022-09-16 12:52:58 +01001778 set lines-=2
Luuk van Baal13ece2a2022-10-03 15:28:08 +01001779 call assert_equal(&spk == 'topline' ? 1 : win_screenpos(0)[0] - tl - wsb, line("w0"))
Luuk van Baal594f9e02022-09-16 12:52:58 +01001780 wincmd k
1781 call assert_equal(1, line("w0"))
Luuk van Baal29ab5242022-09-11 16:59:53 +01001782
Luuk van Baal594f9e02022-09-16 12:52:58 +01001783 " No scroll when equalizing windows
1784 wincmd =
1785 call assert_equal(1, line("w0"))
1786 wincmd j
Luuk van Baal13ece2a2022-10-03 15:28:08 +01001787 call assert_equal(&spk == 'topline' ? 1 : win_screenpos(0)[0] - tl - wsb, line("w0"))
Luuk van Baal594f9e02022-09-16 12:52:58 +01001788 wincmd k
1789 call assert_equal(1, line("w0"))
Luuk van Baal29ab5242022-09-11 16:59:53 +01001790
Luuk van Baal594f9e02022-09-16 12:52:58 +01001791 " No scroll in windows split multiple times
1792 vsplit | split | 4wincmd w
Luuk van Baal13ece2a2022-10-03 15:28:08 +01001793 call assert_equal(&spk == 'topline' ? 1 : win_screenpos(0)[0] - tl - wsb, line("w0"))
Luuk van Baal594f9e02022-09-16 12:52:58 +01001794 1wincmd w | quit | wincmd l | split
Luuk van Baal13ece2a2022-10-03 15:28:08 +01001795 call assert_equal(&spk == 'topline' ? 1 : win_screenpos(0)[0] - tl - wsb, line("w0"))
Luuk van Baal594f9e02022-09-16 12:52:58 +01001796 wincmd j
Luuk van Baal13ece2a2022-10-03 15:28:08 +01001797 call assert_equal(&spk == 'topline' ? 1 : win_screenpos(0)[0] - tl - wsb, line("w0"))
Luuk van Baal29ab5242022-09-11 16:59:53 +01001798
Luuk van Baal594f9e02022-09-16 12:52:58 +01001799 " No scroll in small window
1800 2wincmd w | only | 5split | wincmd k
1801 call assert_equal(1, line("w0"))
1802 wincmd j
Luuk van Baal13ece2a2022-10-03 15:28:08 +01001803 call assert_equal(&spk == 'topline' ? 1 : win_screenpos(0)[0] - tl - wsb, line("w0"))
Luuk van Baal29ab5242022-09-11 16:59:53 +01001804
Luuk van Baal594f9e02022-09-16 12:52:58 +01001805 " No scroll for vertical split
1806 quit | vsplit | wincmd l
1807 call assert_equal(1, line("w0"))
1808 wincmd h
1809 call assert_equal(1, line("w0"))
Luuk van Baal29ab5242022-09-11 16:59:53 +01001810
Luuk van Baal594f9e02022-09-16 12:52:58 +01001811 " No scroll in windows split and quit multiple times
Luuk van Baal13ece2a2022-10-03 15:28:08 +01001812 quit | redraw | split | split | quit | redraw
1813 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 for new buffer
1816 1wincmd w | only | copen | wincmd k
1817 call assert_equal(1, line("w0"))
1818 only
1819 call assert_equal(1, line("w0"))
1820 above copen | wincmd j
Luuk van Baal13ece2a2022-10-03 15:28:08 +01001821 call assert_equal(&spk == 'topline' ? 1 : win_screenpos(0)[0] - tl, line("w0"))
Luuk van Baal29ab5242022-09-11 16:59:53 +01001822
Luuk van Baal594f9e02022-09-16 12:52:58 +01001823 " No scroll when opening cmdwin, and no cursor move when closing cmdwin.
1824 only | norm ggL
1825 let curpos = getcurpos()
1826 norm q:
1827 call assert_equal(1, line("w0"))
1828 call assert_equal(curpos, getcurpos())
Luuk van Baal29ab5242022-09-11 16:59:53 +01001829
Bram Moolenaarfdbd14e2023-02-14 21:56:42 +00001830 " Scroll when cursor becomes invalid in insert mode.
Luuk van Baal594f9e02022-09-16 12:52:58 +01001831 norm Lic
Luuk van Baalbc3dc292023-02-15 16:45:27 +00001832 call assert_equal(curpos, getcurpos(), 'run ' .. run)
Luuk van Baal29ab5242022-09-11 16:59:53 +01001833
Luuk van Baal594f9e02022-09-16 12:52:58 +01001834 " No scroll when topline not equal to 1
1835 only | execute "norm gg5\<C-e>" | split | wincmd k
1836 call assert_equal(6, line("w0"))
1837 wincmd j
Luuk van Baal13ece2a2022-10-03 15:28:08 +01001838 call assert_equal(&spk == 'topline' ? 6 : 5 + win_screenpos(0)[0] - tl - wsb, line("w0"))
Luuk van Baal29ab5242022-09-11 16:59:53 +01001839 endfor
1840
1841 tabnew | tabonly! | %bwipeout!
1842 iunmap c
Luuk van Baal29ab5242022-09-11 16:59:53 +01001843 set scrolloff&
1844 set splitbelow&
1845 set laststatus&
1846 set equalalways&
Luuk van Baal13ece2a2022-10-03 15:28:08 +01001847 set splitkeep&
Luuk van Baal594f9e02022-09-16 12:52:58 +01001848 let &t_WS = save_WS
Luuk van Baal29ab5242022-09-11 16:59:53 +01001849endfunc
Luuk van Baalfd7e60a2022-09-07 14:42:49 +01001850
Bram Moolenaare0f86912023-03-01 17:55:31 +00001851func Test_splitkeep_cmdwin_cursor_position()
Luuk van Baal13ece2a2022-10-03 15:28:08 +01001852 set splitkeep=screen
mityu12167d82022-09-15 17:44:07 +01001853 call setline(1, range(&lines))
1854
1855 " No scroll when cursor is at near bottom of window and cusor position
1856 " recompution (done by line('w0') in this test) happens while in cmdwin.
1857 normal! G
1858 let firstline = line('w0')
1859 autocmd CmdwinEnter * ++once autocmd WinEnter * ++once call line('w0')
1860 execute "normal! q:\<C-w>q"
1861 redraw!
1862 call assert_equal(firstline, line('w0'))
1863
1864 " User script can change cursor position successfully while in cmdwin and it
1865 " shouldn't be changed when closing cmdwin.
1866 execute "normal! Gq:\<Cmd>call win_execute(winnr('#')->win_getid(), 'call cursor(1, 1)')\<CR>\<C-w>q"
1867 call assert_equal(1, line('.'))
1868 call assert_equal(1, col('.'))
1869
1870 execute "normal! Gq:\<Cmd>autocmd WinEnter * ++once call cursor(1, 1)\<CR>\<C-w>q"
1871 call assert_equal(1, line('.'))
1872 call assert_equal(1, col('.'))
1873
1874 %bwipeout!
Luuk van Baal13ece2a2022-10-03 15:28:08 +01001875 set splitkeep&
Bram Moolenaare0f86912023-03-01 17:55:31 +00001876endfunc
Luuk van Baald5bc7622022-09-17 16:16:35 +01001877
Bram Moolenaare0f86912023-03-01 17:55:31 +00001878func Test_splitkeep_misc()
Luuk van Baal13ece2a2022-10-03 15:28:08 +01001879 set splitkeep=screen
Luuk van Baald5bc7622022-09-17 16:16:35 +01001880
1881 call setline(1, range(1, &lines))
Luuk van Baala109f392023-06-02 14:16:35 +01001882 " Cursor is adjusted to start and end of buffer
1883 norm M
1884 wincmd s
1885 resize 1
1886 call assert_equal(1, line('.'))
1887 wincmd j
1888 norm GM
1889 resize 1
1890 call assert_equal(&lines, line('.'))
1891 only!
1892
1893 set splitbelow
Luuk van Baald5bc7622022-09-17 16:16:35 +01001894 norm Gzz
1895 let top = line('w0')
Luuk van Baalfaf1d412022-09-19 16:45:29 +01001896 " No scroll when aucmd_win is opened
Luuk van Baald5bc7622022-09-17 16:16:35 +01001897 call setbufvar(bufnr("test", 1) , '&buftype', 'nofile')
1898 call assert_equal(top, line('w0'))
Luuk van Baalfaf1d412022-09-19 16:45:29 +01001899 " No scroll when tab is changed/closed
1900 tab help | close
1901 call assert_equal(top, line('w0'))
1902 " No scroll when help is closed and buffer line count < window height
1903 norm ggdG
1904 call setline(1, range(1, &lines - 10))
Luuk van Baald5bc7622022-09-17 16:16:35 +01001905 norm G
1906 let top = line('w0')
1907 help | quit
1908 call assert_equal(top, line('w0'))
Luuk van Baal346823d2022-10-05 18:26:42 +01001909 " No error when resizing window in autocmd and buffer length changed
1910 autocmd FileType qf exe "resize" line('$')
1911 cexpr getline(1, '$')
1912 copen
1913 wincmd p
1914 norm dd
1915 cexpr getline(1, '$')
Luuk van Baald5bc7622022-09-17 16:16:35 +01001916
Luuk van Baalfaf1d412022-09-19 16:45:29 +01001917 %bwipeout!
Luuk van Baald5bc7622022-09-17 16:16:35 +01001918 set splitbelow&
Luuk van Baal13ece2a2022-10-03 15:28:08 +01001919 set splitkeep&
Luuk van Baald5bc7622022-09-17 16:16:35 +01001920endfunc
1921
Luuk van Baal16af9132023-08-20 20:44:59 +02001922func Test_splitkeep_cursor()
1923 CheckScreendump
1924 let lines =<< trim END
1925 set splitkeep=screen
1926 autocmd CursorMoved * wincmd p | wincmd p
1927 call setline(1, range(1, 200))
1928 func CursorEqualize()
1929 call cursor(100, 1)
1930 wincmd =
1931 endfunc
1932 wincmd s
1933 call CursorEqualize()
1934 END
1935 call writefile(lines, 'XTestSplitkeepCallback', 'D')
1936 let buf = RunVimInTerminal('-S XTestSplitkeepCallback', #{rows: 8})
1937
1938 call VerifyScreenDump(buf, 'Test_splitkeep_cursor_1', {})
1939
1940 call term_sendkeys(buf, "j")
1941 call VerifyScreenDump(buf, 'Test_splitkeep_cursor_2', {})
1942
1943 call term_sendkeys(buf, ":set scrolloff=0\<CR>G")
1944 call VerifyScreenDump(buf, 'Test_splitkeep_cursor_3', {})
1945
1946 call StopVimInTerminal(buf)
1947endfunc
1948
Bram Moolenaare0f86912023-03-01 17:55:31 +00001949func Test_splitkeep_callback()
Luuk van Baal20e58562022-09-23 12:57:09 +01001950 CheckScreendump
1951 let lines =<< trim END
Luuk van Baal13ece2a2022-10-03 15:28:08 +01001952 set splitkeep=screen
Luuk van Baal20e58562022-09-23 12:57:09 +01001953 call setline(1, range(&lines))
Luuk van Baal13ece2a2022-10-03 15:28:08 +01001954 function C1(a, b)
Luuk van Baal20e58562022-09-23 12:57:09 +01001955 split | wincmd p
1956 endfunction
Luuk van Baal13ece2a2022-10-03 15:28:08 +01001957 function C2(a, b)
Luuk van Baal20e58562022-09-23 12:57:09 +01001958 close | split
1959 endfunction
Luuk van Baal13ece2a2022-10-03 15:28:08 +01001960 nn j <cmd>call job_start([&sh, &shcf, "true"], { 'exit_cb': 'C1' })<CR>
1961 nn t <cmd>call popup_create(term_start([&sh, &shcf, "true"],
1962 \ { 'hidden': 1, 'exit_cb': 'C2' }), {})<CR>
Luuk van Baal20e58562022-09-23 12:57:09 +01001963 END
Luuk van Baal13ece2a2022-10-03 15:28:08 +01001964 call writefile(lines, 'XTestSplitkeepCallback', 'D')
1965 let buf = RunVimInTerminal('-S XTestSplitkeepCallback', #{rows: 8})
Luuk van Baal20e58562022-09-23 12:57:09 +01001966
1967 call term_sendkeys(buf, "j")
Luuk van Baal13ece2a2022-10-03 15:28:08 +01001968 call VerifyScreenDump(buf, 'Test_splitkeep_callback_1', {})
Luuk van Baal20e58562022-09-23 12:57:09 +01001969
Luuk van Baal13ece2a2022-10-03 15:28:08 +01001970 call term_sendkeys(buf, ":quit\<CR>Ht")
1971 call VerifyScreenDump(buf, 'Test_splitkeep_callback_2', {})
Luuk van Baal20e58562022-09-23 12:57:09 +01001972
1973 call term_sendkeys(buf, ":set sb\<CR>:quit\<CR>Gj")
Luuk van Baal13ece2a2022-10-03 15:28:08 +01001974 call VerifyScreenDump(buf, 'Test_splitkeep_callback_3', {})
Luuk van Baal20e58562022-09-23 12:57:09 +01001975
Luuk van Baal13ece2a2022-10-03 15:28:08 +01001976 call term_sendkeys(buf, ":quit\<CR>Gt")
1977 call VerifyScreenDump(buf, 'Test_splitkeep_callback_4', {})
zeertzjq378e6c02023-01-14 11:46:49 +00001978
1979 call StopVimInTerminal(buf)
Luuk van Baal20e58562022-09-23 12:57:09 +01001980endfunc
1981
Bram Moolenaare0f86912023-03-01 17:55:31 +00001982func Test_splitkeep_fold()
Luuk van Baal13ece2a2022-10-03 15:28:08 +01001983 CheckScreendump
Luuk van Baal7c1cbb62022-09-27 12:31:15 +01001984
Luuk van Baal13ece2a2022-10-03 15:28:08 +01001985 let lines =<< trim END
1986 set splitkeep=screen
1987 set foldmethod=marker
1988 set number
1989 let line = 1
1990 for n in range(1, &lines)
1991 call setline(line, ['int FuncName() {/*{{{*/', 1, 2, 3, 4, 5, '}/*}}}*/',
1992 \ 'after fold'])
1993 let line += 8
1994 endfor
1995 END
1996 call writefile(lines, 'XTestSplitkeepFold', 'D')
1997 let buf = RunVimInTerminal('-S XTestSplitkeepFold', #{rows: 10})
Luuk van Baal7c1cbb62022-09-27 12:31:15 +01001998
1999 call term_sendkeys(buf, "L:wincmd s\<CR>")
Luuk van Baal13ece2a2022-10-03 15:28:08 +01002000 call VerifyScreenDump(buf, 'Test_splitkeep_fold_1', {})
Luuk van Baal7c1cbb62022-09-27 12:31:15 +01002001
2002 call term_sendkeys(buf, ":quit\<CR>")
Luuk van Baal13ece2a2022-10-03 15:28:08 +01002003 call VerifyScreenDump(buf, 'Test_splitkeep_fold_2', {})
Luuk van Baal7c1cbb62022-09-27 12:31:15 +01002004
2005 call term_sendkeys(buf, "H:below split\<CR>")
Luuk van Baal13ece2a2022-10-03 15:28:08 +01002006 call VerifyScreenDump(buf, 'Test_splitkeep_fold_3', {})
Luuk van Baal7c1cbb62022-09-27 12:31:15 +01002007
2008 call term_sendkeys(buf, ":wincmd k\<CR>:quit\<CR>")
Luuk van Baal13ece2a2022-10-03 15:28:08 +01002009 call VerifyScreenDump(buf, 'Test_splitkeep_fold_4', {})
zeertzjq378e6c02023-01-14 11:46:49 +00002010
2011 call StopVimInTerminal(buf)
Bram Moolenaare0f86912023-03-01 17:55:31 +00002012endfunc
Luuk van Baal7c1cbb62022-09-27 12:31:15 +01002013
Bram Moolenaare0f86912023-03-01 17:55:31 +00002014func Test_splitkeep_status()
Luuk van Baal74a694d2022-11-28 16:49:36 +00002015 CheckScreendump
2016
2017 let lines =<< trim END
2018 call setline(1, ['a', 'b', 'c'])
2019 set nomodified
2020 set splitkeep=screen
2021 let win = winnr()
2022 wincmd s
2023 wincmd j
2024 END
2025 call writefile(lines, 'XTestSplitkeepStatus', 'D')
2026 let buf = RunVimInTerminal('-S XTestSplitkeepStatus', #{rows: 10})
2027
2028 call term_sendkeys(buf, ":call win_move_statusline(win, 1)\<CR>")
2029 call VerifyScreenDump(buf, 'Test_splitkeep_status_1', {})
zeertzjq378e6c02023-01-14 11:46:49 +00002030
2031 call StopVimInTerminal(buf)
Bram Moolenaare0f86912023-03-01 17:55:31 +00002032endfunc
Luuk van Baal74a694d2022-11-28 16:49:36 +00002033
Luuk van Baalb926bf42023-05-06 12:53:50 +01002034" skipcol is not reset unnecessarily and is copied to new window
2035func Test_splitkeep_skipcol()
2036 CheckScreendump
2037
2038 let lines =<< trim END
2039 set splitkeep=topline smoothscroll splitbelow scrolloff=0
2040 call setline(1, 'with lots of text in one line '->repeat(6))
2041 norm 2
2042 wincmd s
2043 END
2044
2045 call writefile(lines, 'XTestSplitkeepSkipcol', 'D')
2046 let buf = RunVimInTerminal('-S XTestSplitkeepSkipcol', #{rows: 12, cols: 40})
2047
2048 call VerifyScreenDump(buf, 'Test_splitkeep_skipcol_1', {})
2049endfunc
2050
Bram Moolenaare0f86912023-03-01 17:55:31 +00002051func Test_new_help_window_on_error()
Rob Pillingcb94c912022-12-13 12:26:09 +00002052 help change.txt
2053 execute "normal! /CTRL-@\<CR>"
2054 silent! execute "normal! \<C-W>]"
2055
2056 let wincount = winnr('$')
2057 help 'mod'
2058
2059 call assert_equal(wincount, winnr('$'))
2060 call assert_equal(expand("<cword>"), "'mod'")
Bram Moolenaare0f86912023-03-01 17:55:31 +00002061endfunc
2062
2063func Test_smoothscroll_in_zero_width_window()
2064 let save_lines = &lines
2065 let save_columns = &columns
2066
2067 winsize 0 24
2068 set cpo+=n
2069 exe "noremap 0 \<C-W>n\<C-W>L"
2070 norm 000000
2071 set number smoothscroll
2072 exe "norm \<C-Y>"
2073
2074 only!
2075 let &lines = save_lines
2076 let &columns = save_columns
2077 set cpo-=n
2078 unmap 0
2079 set nonumber nosmoothscroll
2080endfunc
Rob Pillingcb94c912022-12-13 12:26:09 +00002081
2082
Bram Moolenaar9e4d8212016-08-18 23:04:48 +02002083" vim: shiftwidth=2 sts=2 expandtab