blob: 03c51e845ab179511019a916880bc979a0ad3d9a [file] [log] [blame]
Bram Moolenaar991dea32016-05-24 11:31:32 +02001" Tests for window cmd (:wincmd, :split, :vsplit, :resize and etc...)
2
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +02003source check.vim
Luuk van Baal20e58562022-09-23 12:57:09 +01004source screendump.vim
Bram Moolenaara27e1dc2019-10-07 22:27:36 +02005
Bram Moolenaar991dea32016-05-24 11:31:32 +02006func Test_window_cmd_ls0_with_split()
7 set ls=0
8 set splitbelow
9 split
10 quit
11 call assert_equal(0, &lines - &cmdheight - winheight(0))
12 new | only!
13 "
14 set splitbelow&vim
15 botright split
16 quit
17 call assert_equal(0, &lines - &cmdheight - winheight(0))
18 new | only!
19 set ls&vim
20endfunc
21
zeertzjqfbf20712023-04-26 19:01:44 +010022func Test_window_cmd_ls0_split_scrolling()
23 CheckRunVimInTerminal
24
25 let lines =<< trim END
26 set laststatus=0
27 call setline(1, range(1, 100))
28 normal! G
29 END
30 call writefile(lines, 'XTestLs0SplitScrolling', 'D')
31 let buf = RunVimInTerminal('-S XTestLs0SplitScrolling', #{rows: 10})
32
33 call term_sendkeys(buf, ":botright split\<CR>")
34 call WaitForAssert({-> assert_match('Bot$', term_getline(buf, 5))})
35 call assert_equal('100', term_getline(buf, 4))
36
37 call StopVimInTerminal(buf)
38endfunc
39
Bram Moolenaar991dea32016-05-24 11:31:32 +020040func Test_window_cmd_cmdwin_with_vsp()
Bram Moolenaar72cf47a2018-05-10 18:23:29 +020041 let efmt = 'Expected 0 but got %d (in ls=%d, %s window)'
Bram Moolenaar991dea32016-05-24 11:31:32 +020042 for v in range(0, 2)
43 exec "set ls=" . v
44 vsplit
45 call feedkeys("q:\<CR>")
46 let ac = &lines - (&cmdheight + winheight(0) + !!v)
47 let emsg = printf(efmt, ac, v, 'left')
48 call assert_equal(0, ac, emsg)
49 wincmd w
50 let ac = &lines - (&cmdheight + winheight(0) + !!v)
51 let emsg = printf(efmt, ac, v, 'right')
52 call assert_equal(0, ac, emsg)
53 new | only!
54 endfor
55 set ls&vim
56endfunc
57
Bram Moolenaar96bde992022-08-10 17:23:12 +010058func Test_cmdheight_not_changed()
59 set cmdheight=2
60 set winminheight=0
61 augroup Maximize
62 autocmd WinEnter * wincmd _
63 augroup END
64 split
65 tabnew
66 tabfirst
67 call assert_equal(2, &cmdheight)
68
69 tabonly!
70 only
71 set winminwidth& cmdheight&
72 augroup Maximize
73 au!
74 augroup END
75 augroup! Maximize
76endfunc
77
Bram Moolenaar5d3c9f82020-06-26 20:41:39 +020078" Test for jumping to windows
79func Test_window_jump()
80 new
81 " jumping to a window with a count greater than the max windows
82 exe "normal 4\<C-W>w"
83 call assert_equal(2, winnr())
84 only
85endfunc
86
87func Test_window_cmd_wincmd_gf()
Bram Moolenaar5d2ca042016-06-26 17:11:21 +020088 let fname = 'test_gf.txt'
89 let swp_fname = '.' . fname . '.swp'
Bram Moolenaardb4c9472022-10-15 22:06:06 +010090 call writefile([], fname, 'D')
91 call writefile([], swp_fname, 'D')
Bram Moolenaar5d2ca042016-06-26 17:11:21 +020092 function s:swap_exists()
93 let v:swapchoice = s:swap_choice
94 endfunc
Bram Moolenaareaa49e42019-07-13 18:08:59 +020095 " Remove the catch-all that runtest.vim adds
96 au! SwapExists
Bram Moolenaar5d2ca042016-06-26 17:11:21 +020097 augroup test_window_cmd_wincmd_gf
98 autocmd!
99 exec "autocmd SwapExists " . fname . " call s:swap_exists()"
100 augroup END
101
102 call setline(1, fname)
103 " (E)dit anyway
104 let s:swap_choice = 'e'
105 wincmd gf
106 call assert_equal(2, tabpagenr())
107 call assert_equal(fname, bufname("%"))
108 quit!
109
110 " (Q)uit
111 let s:swap_choice = 'q'
112 wincmd gf
113 call assert_equal(1, tabpagenr())
114 call assert_notequal(fname, bufname("%"))
115 new | only!
116
Bram Moolenaar5d2ca042016-06-26 17:11:21 +0200117 augroup! test_window_cmd_wincmd_gf
118endfunc
119
Bram Moolenaar4520d442017-03-19 16:09:46 +0100120func Test_window_quit()
121 e Xa
122 split Xb
Bram Moolenaarf92e58c2019-09-08 21:51:41 +0200123 call assert_equal(2, '$'->winnr())
Bram Moolenaar4520d442017-03-19 16:09:46 +0100124 call assert_equal('Xb', bufname(winbufnr(1)))
125 call assert_equal('Xa', bufname(winbufnr(2)))
126
127 wincmd q
128 call assert_equal(1, winnr('$'))
129 call assert_equal('Xa', bufname(winbufnr(1)))
130
131 bw Xa Xb
132endfunc
133
134func Test_window_horizontal_split()
135 call assert_equal(1, winnr('$'))
136 3wincmd s
137 call assert_equal(2, winnr('$'))
138 call assert_equal(3, winheight(0))
Bram Moolenaarf92e58c2019-09-08 21:51:41 +0200139 call assert_equal(winwidth(1), 2->winwidth())
Bram Moolenaar4520d442017-03-19 16:09:46 +0100140
141 call assert_fails('botright topleft wincmd s', 'E442:')
142 bw
143endfunc
144
145func Test_window_vertical_split()
146 call assert_equal(1, winnr('$'))
147 3wincmd v
148 call assert_equal(2, winnr('$'))
149 call assert_equal(3, winwidth(0))
150 call assert_equal(winheight(1), winheight(2))
151
152 call assert_fails('botright topleft wincmd v', 'E442:')
153 bw
154endfunc
155
Bram Moolenaar1bbb6192018-11-10 16:02:01 +0100156" Test the ":wincmd ^" and "<C-W>^" commands.
Bram Moolenaar4520d442017-03-19 16:09:46 +0100157func Test_window_split_edit_alternate()
Bram Moolenaar1bbb6192018-11-10 16:02:01 +0100158 " Test for failure when the alternate buffer/file no longer exists.
159 edit Xfoo | %bw
Bram Moolenaare2e40752020-09-04 21:18:46 +0200160 call assert_fails(':wincmd ^', 'E23:')
Bram Moolenaar1bbb6192018-11-10 16:02:01 +0100161
162 " Test for the expected behavior when we have two named buffers.
163 edit Xfoo | edit Xbar
Bram Moolenaar4520d442017-03-19 16:09:46 +0100164 wincmd ^
Bram Moolenaar1bbb6192018-11-10 16:02:01 +0100165 call assert_equal('Xfoo', bufname(winbufnr(1)))
166 call assert_equal('Xbar', bufname(winbufnr(2)))
167 only
Bram Moolenaar4520d442017-03-19 16:09:46 +0100168
Bram Moolenaar1bbb6192018-11-10 16:02:01 +0100169 " Test for the expected behavior when the alternate buffer is not named.
170 enew | let l:nr1 = bufnr('%')
171 edit Xfoo | let l:nr2 = bufnr('%')
172 wincmd ^
173 call assert_equal(l:nr1, winbufnr(1))
174 call assert_equal(l:nr2, winbufnr(2))
175 only
176
Bram Moolenaard42333d2018-11-10 20:28:19 +0100177 " FIXME: this currently fails on AppVeyor, but passes locally
178 if !has('win32')
179 " Test the Normal mode command.
180 call feedkeys("\<C-W>\<C-^>", 'tx')
181 call assert_equal(l:nr2, winbufnr(1))
182 call assert_equal(l:nr1, winbufnr(2))
183 endif
Bram Moolenaar1bbb6192018-11-10 16:02:01 +0100184
185 %bw!
186endfunc
187
188" Test the ":[count]wincmd ^" and "[count]<C-W>^" commands.
189func Test_window_split_edit_bufnr()
Bram Moolenaar1bbb6192018-11-10 16:02:01 +0100190 %bwipeout
191 let l:nr = bufnr('%') + 1
Bram Moolenaare2e40752020-09-04 21:18:46 +0200192 call assert_fails(':execute "normal! ' . l:nr . '\<C-W>\<C-^>"', 'E92:')
193 call assert_fails(':' . l:nr . 'wincmd ^', 'E16:')
194 call assert_fails(':0wincmd ^', 'E16:')
Bram Moolenaar1bbb6192018-11-10 16:02:01 +0100195
196 edit Xfoo | edit Xbar | edit Xbaz
197 let l:foo_nr = bufnr('Xfoo')
198 let l:bar_nr = bufnr('Xbar')
199 let l:baz_nr = bufnr('Xbaz')
200
Bram Moolenaar8617b402018-11-10 20:47:48 +0100201 " FIXME: this currently fails on AppVeyor, but passes locally
202 if !has('win32')
203 call feedkeys(l:foo_nr . "\<C-W>\<C-^>", 'tx')
204 call assert_equal('Xfoo', bufname(winbufnr(1)))
205 call assert_equal('Xbaz', bufname(winbufnr(2)))
206 only
Bram Moolenaar1bbb6192018-11-10 16:02:01 +0100207
Bram Moolenaar8617b402018-11-10 20:47:48 +0100208 call feedkeys(l:bar_nr . "\<C-W>\<C-^>", 'tx')
209 call assert_equal('Xbar', bufname(winbufnr(1)))
210 call assert_equal('Xfoo', bufname(winbufnr(2)))
211 only
Bram Moolenaar1bbb6192018-11-10 16:02:01 +0100212
Bram Moolenaar8617b402018-11-10 20:47:48 +0100213 execute l:baz_nr . 'wincmd ^'
214 call assert_equal('Xbaz', bufname(winbufnr(1)))
215 call assert_equal('Xbar', bufname(winbufnr(2)))
216 endif
Bram Moolenaar1bbb6192018-11-10 16:02:01 +0100217
218 %bw!
Bram Moolenaar4520d442017-03-19 16:09:46 +0100219endfunc
220
Bram Moolenaar9b9be002020-03-22 14:41:22 +0100221func Test_window_split_no_room()
222 " N horizontal windows need >= 2*N + 1 lines:
223 " - 1 line + 1 status line in each window
224 " - 1 Ex command line
225 "
226 " 2*N + 1 <= &lines
227 " N <= (lines - 1)/2
228 "
229 " Beyond that number of windows, E36: Not enough room is expected.
230 let hor_win_count = (&lines - 1)/2
231 let hor_split_count = hor_win_count - 1
232 for s in range(1, hor_split_count) | split | endfor
233 call assert_fails('split', 'E36:')
234
235 " N vertical windows need >= 2*(N - 1) + 1 columns:
236 " - 1 column + 1 separator for each window (except last window)
237 " - 1 column for the last window which does not have separator
238 "
239 " 2*(N - 1) + 1 <= &columns
240 " 2*N - 1 <= &columns
241 " N <= (&columns + 1)/2
242 let ver_win_count = (&columns + 1)/2
243 let ver_split_count = ver_win_count - 1
244 for s in range(1, ver_split_count) | vsplit | endfor
245 call assert_fails('vsplit', 'E36:')
246
247 %bw!
248endfunc
249
Bram Moolenaar4520d442017-03-19 16:09:46 +0100250func Test_window_exchange()
251 e Xa
252
253 " Nothing happens with window exchange when there is 1 window
254 wincmd x
255 call assert_equal(1, winnr('$'))
256
257 split Xb
258 split Xc
259
260 call assert_equal('Xc', bufname(winbufnr(1)))
261 call assert_equal('Xb', bufname(winbufnr(2)))
262 call assert_equal('Xa', bufname(winbufnr(3)))
263
264 " Exchange current window 1 with window 3
265 3wincmd x
266 call assert_equal('Xa', bufname(winbufnr(1)))
267 call assert_equal('Xb', bufname(winbufnr(2)))
268 call assert_equal('Xc', bufname(winbufnr(3)))
269
270 " Exchange window with next when at the top window
271 wincmd x
272 call assert_equal('Xb', bufname(winbufnr(1)))
273 call assert_equal('Xa', bufname(winbufnr(2)))
274 call assert_equal('Xc', bufname(winbufnr(3)))
275
276 " Exchange window with next when at the middle window
277 wincmd j
278 wincmd x
279 call assert_equal('Xb', bufname(winbufnr(1)))
280 call assert_equal('Xc', bufname(winbufnr(2)))
281 call assert_equal('Xa', bufname(winbufnr(3)))
282
283 " Exchange window with next when at the bottom window.
284 " When there is no next window, it exchanges with the previous window.
285 wincmd j
286 wincmd x
287 call assert_equal('Xb', bufname(winbufnr(1)))
288 call assert_equal('Xa', bufname(winbufnr(2)))
289 call assert_equal('Xc', bufname(winbufnr(3)))
290
291 bw Xa Xb Xc
292endfunc
293
294func Test_window_rotate()
295 e Xa
296 split Xb
297 split Xc
298 call assert_equal('Xc', bufname(winbufnr(1)))
299 call assert_equal('Xb', bufname(winbufnr(2)))
300 call assert_equal('Xa', bufname(winbufnr(3)))
301
302 " Rotate downwards
303 wincmd r
304 call assert_equal('Xa', bufname(winbufnr(1)))
305 call assert_equal('Xc', bufname(winbufnr(2)))
306 call assert_equal('Xb', bufname(winbufnr(3)))
307
308 2wincmd r
309 call assert_equal('Xc', bufname(winbufnr(1)))
310 call assert_equal('Xb', bufname(winbufnr(2)))
311 call assert_equal('Xa', bufname(winbufnr(3)))
312
313 " Rotate upwards
314 wincmd R
315 call assert_equal('Xb', bufname(winbufnr(1)))
316 call assert_equal('Xa', bufname(winbufnr(2)))
317 call assert_equal('Xc', bufname(winbufnr(3)))
318
319 2wincmd R
320 call assert_equal('Xc', bufname(winbufnr(1)))
321 call assert_equal('Xb', bufname(winbufnr(2)))
322 call assert_equal('Xa', bufname(winbufnr(3)))
323
324 bot vsplit
325 call assert_fails('wincmd R', 'E443:')
326
327 bw Xa Xb Xc
328endfunc
329
330func Test_window_height()
331 e Xa
332 split Xb
333
334 let [wh1, wh2] = [winheight(1), winheight(2)]
335 " Active window (1) should have the same height or 1 more
336 " than the other window.
337 call assert_inrange(wh2, wh2 + 1, wh1)
338
339 wincmd -
340 call assert_equal(wh1 - 1, winheight(1))
341 call assert_equal(wh2 + 1, winheight(2))
342
343 wincmd +
344 call assert_equal(wh1, winheight(1))
Bram Moolenaarf92e58c2019-09-08 21:51:41 +0200345 call assert_equal(wh2, 2->winheight())
Bram Moolenaar4520d442017-03-19 16:09:46 +0100346
347 2wincmd _
348 call assert_equal(2, winheight(1))
349 call assert_equal(wh1 + wh2 - 2, winheight(2))
350
351 wincmd =
352 call assert_equal(wh1, winheight(1))
353 call assert_equal(wh2, winheight(2))
354
355 2wincmd _
356 set winfixheight
357 split Xc
358 let [wh1, wh2, wh3] = [winheight(1), winheight(2), winheight(3)]
359 call assert_equal(2, winheight(2))
360 call assert_inrange(wh3, wh3 + 1, wh1)
361 3wincmd +
362 call assert_equal(2, winheight(2))
363 call assert_equal(wh1 + 3, winheight(1))
364 call assert_equal(wh3 - 3, winheight(3))
365 wincmd =
366 call assert_equal(2, winheight(2))
367 call assert_equal(wh1, winheight(1))
368 call assert_equal(wh3, winheight(3))
369
370 wincmd j
371 set winfixheight&
372
373 wincmd =
374 let [wh1, wh2, wh3] = [winheight(1), winheight(2), winheight(3)]
375 " Current window (2) should have the same height or 1 more
376 " than the other windows.
377 call assert_inrange(wh1, wh1 + 1, wh2)
378 call assert_inrange(wh3, wh3 + 1, wh2)
379
380 bw Xa Xb Xc
381endfunc
382
Bram Moolenaar21c3a802022-08-31 17:49:14 +0100383func Test_wincmd_equal()
384 edit Xone
385 below split Xtwo
386 rightbelow vsplit Xthree
387 call assert_equal('Xone', bufname(winbufnr(1)))
388 call assert_equal('Xtwo', bufname(winbufnr(2)))
389 call assert_equal('Xthree', bufname(winbufnr(3)))
390
391 " Xone and Xtwo should be about the same height
392 let [wh1, wh2] = [winheight(1), winheight(2)]
393 call assert_inrange(wh1 - 1, wh1 + 1, wh2)
394 " Xtwo and Xthree should be about the same width
395 let [ww2, ww3] = [winwidth(2), winwidth(3)]
396 call assert_inrange(ww2 - 1, ww2 + 1, ww3)
397
398 1wincmd w
399 10wincmd _
400 2wincmd w
401 20wincmd |
402 call assert_equal(10, winheight(1))
403 call assert_equal(20, winwidth(2))
404
405 " equalizing horizontally doesn't change the heights
406 hor wincmd =
407 call assert_equal(10, winheight(1))
408 let [ww2, ww3] = [winwidth(2), winwidth(3)]
409 call assert_inrange(ww2 - 1, ww2 + 1, ww3)
410
411 2wincmd w
412 20wincmd |
413 call assert_equal(20, winwidth(2))
414 " equalizing vertically doesn't change the widths
415 vert wincmd =
416 call assert_equal(20, winwidth(2))
417 let [wh1, wh2] = [winheight(1), winheight(2)]
418 call assert_inrange(wh1 - 1, wh1 + 1, wh2)
419
420 bwipe Xone Xtwo Xthree
421endfunc
422
Bram Moolenaar4520d442017-03-19 16:09:46 +0100423func Test_window_width()
424 e Xa
425 vsplit Xb
426
427 let [ww1, ww2] = [winwidth(1), winwidth(2)]
428 " Active window (1) should have the same width or 1 more
429 " than the other window.
430 call assert_inrange(ww2, ww2 + 1, ww1)
431
432 wincmd <
433 call assert_equal(ww1 - 1, winwidth(1))
434 call assert_equal(ww2 + 1, winwidth(2))
435
436 wincmd >
437 call assert_equal(ww1, winwidth(1))
438 call assert_equal(ww2, winwidth(2))
439
440 2wincmd |
441 call assert_equal(2, winwidth(1))
442 call assert_equal(ww1 + ww2 - 2, winwidth(2))
443
444 wincmd =
445 call assert_equal(ww1, winwidth(1))
446 call assert_equal(ww2, winwidth(2))
447
448 2wincmd |
449 set winfixwidth
450 vsplit Xc
451 let [ww1, ww2, ww3] = [winwidth(1), winwidth(2), winwidth(3)]
Bram Moolenaar38e34832017-03-19 20:22:36 +0100452 call assert_equal(2, winwidth(2))
Bram Moolenaar4520d442017-03-19 16:09:46 +0100453 call assert_inrange(ww3, ww3 + 1, ww1)
454 3wincmd >
Bram Moolenaar38e34832017-03-19 20:22:36 +0100455 call assert_equal(2, winwidth(2))
Bram Moolenaar4520d442017-03-19 16:09:46 +0100456 call assert_equal(ww1 + 3, winwidth(1))
457 call assert_equal(ww3 - 3, winwidth(3))
458 wincmd =
Bram Moolenaar38e34832017-03-19 20:22:36 +0100459 call assert_equal(2, winwidth(2))
Bram Moolenaar4520d442017-03-19 16:09:46 +0100460 call assert_equal(ww1, winwidth(1))
461 call assert_equal(ww3, winwidth(3))
462
463 wincmd l
464 set winfixwidth&
465
466 wincmd =
467 let [ww1, ww2, ww3] = [winwidth(1), winwidth(2), winwidth(3)]
468 " Current window (2) should have the same width or 1 more
469 " than the other windows.
470 call assert_inrange(ww1, ww1 + 1, ww2)
471 call assert_inrange(ww3, ww3 + 1, ww2)
472
Yegappan Lakshmanan59585492021-06-12 13:46:41 +0200473 " when the current window width is less than the new 'winwidth', the current
474 " window width should be increased.
475 enew | only
476 split
477 10vnew
478 set winwidth=15
479 call assert_equal(15, winwidth(0))
480
481 %bw!
Bram Moolenaar4520d442017-03-19 16:09:46 +0100482endfunc
483
Bram Moolenaar8eeeba82017-06-25 22:45:39 +0200484func Test_equalalways_on_close()
485 set equalalways
486 vsplit
487 windo split
Bram Moolenaar1bbb6192018-11-10 16:02:01 +0100488 split
Bram Moolenaar8eeeba82017-06-25 22:45:39 +0200489 wincmd J
490 " now we have a frame top-left with two windows, a frame top-right with two
491 " windows and a frame at the bottom, full-width.
492 let height_1 = winheight(1)
493 let height_2 = winheight(2)
494 let height_3 = winheight(3)
495 let height_4 = winheight(4)
496 " closing the bottom window causes all windows to be resized.
497 close
498 call assert_notequal(height_1, winheight(1))
499 call assert_notequal(height_2, winheight(2))
500 call assert_notequal(height_3, winheight(3))
501 call assert_notequal(height_4, winheight(4))
502 call assert_equal(winheight(1), winheight(3))
503 call assert_equal(winheight(2), winheight(4))
504
505 1wincmd w
506 split
507 4wincmd w
508 resize + 5
509 " left column has three windows, equalized heights.
510 " right column has two windows, top one a bit higher
511 let height_1 = winheight(1)
512 let height_2 = winheight(2)
513 let height_4 = winheight(4)
514 let height_5 = winheight(5)
515 3wincmd w
516 " closing window in left column equalizes heights in left column but not in
517 " the right column
518 close
519 call assert_notequal(height_1, winheight(1))
520 call assert_notequal(height_2, winheight(2))
521 call assert_equal(height_4, winheight(3))
522 call assert_equal(height_5, winheight(4))
523
524 only
525 set equalalways&
526endfunc
527
Bram Moolenaar22044dc2017-12-02 15:43:37 +0100528func Test_win_screenpos()
Bram Moolenaar5a4c3082019-12-01 15:23:11 +0100529 CheckFeature quickfix
530
Bram Moolenaar22044dc2017-12-02 15:43:37 +0100531 call assert_equal(1, winnr('$'))
532 split
533 vsplit
534 10wincmd _
535 30wincmd |
536 call assert_equal([1, 1], win_screenpos(1))
537 call assert_equal([1, 32], win_screenpos(2))
538 call assert_equal([12, 1], win_screenpos(3))
539 call assert_equal([0, 0], win_screenpos(4))
Bram Moolenaar99fa7212020-04-26 15:59:55 +0200540 call assert_fails('let l = win_screenpos([])', 'E745:')
Bram Moolenaar22044dc2017-12-02 15:43:37 +0100541 only
542endfunc
543
Bram Moolenaar4520d442017-03-19 16:09:46 +0100544func Test_window_jump_tag()
Bram Moolenaar5a4c3082019-12-01 15:23:11 +0100545 CheckFeature quickfix
546
Bram Moolenaar4520d442017-03-19 16:09:46 +0100547 help
548 /iccf
549 call assert_match('^|iccf|', getline('.'))
550 call assert_equal(2, winnr('$'))
551 2wincmd }
552 call assert_equal(3, winnr('$'))
553 call assert_match('^|iccf|', getline('.'))
554 wincmd k
555 call assert_match('\*iccf\*', getline('.'))
556 call assert_equal(2, winheight(0))
557
558 wincmd z
559 set previewheight=4
560 help
561 /bugs
562 wincmd }
563 wincmd k
564 call assert_match('\*bugs\*', getline('.'))
565 call assert_equal(4, winheight(0))
566 set previewheight&
567
568 %bw!
569endfunc
570
571func Test_window_newtab()
572 e Xa
573
574 call assert_equal(1, tabpagenr('$'))
575 call assert_equal("\nAlready only one window", execute('wincmd T'))
576
577 split Xb
578 split Xc
579
580 wincmd T
581 call assert_equal(2, tabpagenr('$'))
582 call assert_equal(['Xb', 'Xa'], map(tabpagebuflist(1), 'bufname(v:val)'))
Bram Moolenaarce90e362019-09-08 18:58:44 +0200583 call assert_equal(['Xc' ], map(2->tabpagebuflist(), 'bufname(v:val)'))
Bram Moolenaar0e05de42020-03-25 22:23:46 +0100584 call assert_equal(['Xc' ], map(tabpagebuflist(), 'bufname(v:val)'))
Bram Moolenaar4520d442017-03-19 16:09:46 +0100585
586 %bw!
587endfunc
588
Bram Moolenaarf79225e2017-03-18 23:11:04 +0100589func Test_next_split_all()
590 " This was causing an illegal memory access.
591 n x
592 norm axxx
593 split
594 split
595 s/x
596 s/x
597 all
598 bwipe!
599endfunc
600
Bram Moolenaar75373f32017-08-07 22:02:30 +0200601" Tests for adjusting window and contents
602func GetScreenStr(row)
603 let str = ""
604 for c in range(1,3)
605 let str .= nr2char(screenchar(a:row, c))
606 endfor
607 return str
608endfunc
609
610func Test_window_contents()
611 enew! | only | new
612 call setline(1, range(1,256))
613
614 exe "norm! \<C-W>t\<C-W>=1Gzt\<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(1, line("w0"))
619 call assert_equal('1 ', s3)
620
621 exe "norm! \<C-W>t\<C-W>=50Gzt\<C-W>w\<C-W>+"
622 redraw
Bram Moolenaar72cf47a2018-05-10 18:23:29 +0200623 let s3 = GetScreenStr(1)
Bram Moolenaar75373f32017-08-07 22:02:30 +0200624 wincmd p
625 call assert_equal(50, line("w0"))
626 call assert_equal('50 ', s3)
627
628 exe "norm! \<C-W>t\<C-W>=59Gzt\<C-W>w\<C-W>+"
629 redraw
Bram Moolenaar72cf47a2018-05-10 18:23:29 +0200630 let s3 = GetScreenStr(1)
Bram Moolenaar75373f32017-08-07 22:02:30 +0200631 wincmd p
632 call assert_equal(59, line("w0"))
633 call assert_equal('59 ', s3)
634
Bram Moolenaar8b633132020-03-20 18:20:51 +0100635 %d
636 call setline(1, ['one', 'two', 'three'])
637 call assert_equal(1, line('w0'))
638 call assert_equal(3, line('w$'))
639
Bram Moolenaar75373f32017-08-07 22:02:30 +0200640 bwipeout!
641 call test_garbagecollect_now()
642endfunc
643
Bram Moolenaar2efb3232017-12-19 12:27:23 +0100644func Test_window_colon_command()
645 " This was reading invalid memory.
646 exe "norm! v\<C-W>:\<C-U>echo v:version"
647endfunc
648
Bram Moolenaar6f361c92018-01-31 19:06:50 +0100649func Test_access_freed_mem()
Bram Moolenaar1417c762019-07-27 17:31:36 +0200650 call assert_equal(&columns, winwidth(0))
Bram Moolenaar9a046fd2021-01-28 13:47:59 +0100651 " This was accessing freed memory (but with what events?)
652 au BufEnter,BufLeave,WinEnter,WinLeave 0 vs xxx
Bram Moolenaar6f361c92018-01-31 19:06:50 +0100653 arg 0
654 argadd
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +0200655 call assert_fails("all", "E242:")
Bram Moolenaar6f361c92018-01-31 19:06:50 +0100656 au!
657 bwipe xxx
Bram Moolenaar1417c762019-07-27 17:31:36 +0200658 call assert_equal(&columns, winwidth(0))
Bram Moolenaar6f361c92018-01-31 19:06:50 +0100659endfunc
660
Bram Moolenaara27e1dc2019-10-07 22:27:36 +0200661func Test_insert_cleared_on_switch_to_term()
662 CheckFeature terminal
663
664 set showmode
665 terminal
666 wincmd p
667
668 call feedkeys("i\<C-O>", 'ntx')
669 redraw
670
671 " The "-- (insert) --" indicator should be visible.
672 let chars = map(range(1, &columns), 'nr2char(screenchar(&lines, v:val))')
673 let str = trim(join(chars, ''))
674 call assert_equal('-- (insert) --', str)
675
676 call feedkeys("\<C-W>p", 'ntx')
677 redraw
678
679 " The "-- (insert) --" indicator should have been cleared.
680 let chars = map(range(1, &columns), 'nr2char(screenchar(&lines, v:val))')
681 let str = trim(join(chars, ''))
682 call assert_equal('', str)
683
684 set showmode&
685 %bw!
686endfunc
687
Bram Moolenaar5bab5552018-04-13 20:41:29 +0200688func Test_visual_cleared_after_window_split()
689 new | only!
690 let smd_save = &showmode
691 set showmode
692 let ls_save = &laststatus
693 set laststatus=1
694 call setline(1, ['a', 'b', 'c', 'd', ''])
695 norm! G
696 exe "norm! kkvk"
697 redraw
698 exe "norm! \<C-W>v"
699 redraw
700 " check if '-- VISUAL --' disappeared from command line
701 let columns = range(1, &columns)
702 let cmdlinechars = map(columns, 'nr2char(screenchar(&lines, v:val))')
703 let cmdline = join(cmdlinechars, '')
704 let cmdline_ltrim = substitute(cmdline, '^\s*', "", "")
705 let mode_shown = substitute(cmdline_ltrim, '\s*$', "", "")
706 call assert_equal('', mode_shown)
707 let &showmode = smd_save
708 let &laststatus = ls_save
709 bwipe!
710endfunc
711
Bram Moolenaar72cf47a2018-05-10 18:23:29 +0200712func Test_winrestcmd()
713 2split
714 3vsplit
Bram Moolenaara0c8aea2021-03-20 19:55:35 +0100715 let restcmd = winrestcmd()
Bram Moolenaar72cf47a2018-05-10 18:23:29 +0200716 call assert_equal(2, winheight(0))
717 call assert_equal(3, winwidth(0))
718 wincmd =
719 call assert_notequal(2, winheight(0))
720 call assert_notequal(3, winwidth(0))
Bram Moolenaara0c8aea2021-03-20 19:55:35 +0100721 exe restcmd
Bram Moolenaar72cf47a2018-05-10 18:23:29 +0200722 call assert_equal(2, winheight(0))
723 call assert_equal(3, winwidth(0))
724 only
Bram Moolenaara0c8aea2021-03-20 19:55:35 +0100725
726 wincmd v
727 wincmd s
728 wincmd v
729 redraw
730 let restcmd = winrestcmd()
731 wincmd _
732 wincmd |
733 exe restcmd
734 redraw
735 call assert_equal(restcmd, winrestcmd())
736
737 only
Bram Moolenaar72cf47a2018-05-10 18:23:29 +0200738endfunc
739
Bram Moolenaar1e115362019-01-09 23:01:02 +0100740func Fun_RenewFile()
Bram Moolenaar026587b2019-08-17 15:08:00 +0200741 " Need to wait a bit for the timestamp to be older.
Bram Moolenaarce90e362019-09-08 18:58:44 +0200742 let old_ftime = getftime("tmp.txt")
743 while getftime("tmp.txt") == old_ftime
744 sleep 100m
745 silent execute '!echo "1" > tmp.txt'
746 endwhile
Bram Moolenaara42df592018-12-24 00:22:39 +0100747 sp
748 wincmd p
749 edit! tmp.txt
Bram Moolenaar1e115362019-01-09 23:01:02 +0100750endfunc
Bram Moolenaara42df592018-12-24 00:22:39 +0100751
752func Test_window_prevwin()
753 " Can we make this work on MS-Windows?
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +0200754 CheckUnix
Bram Moolenaara42df592018-12-24 00:22:39 +0100755
756 set hidden autoread
Bram Moolenaardb4c9472022-10-15 22:06:06 +0100757 call writefile(['2'], 'tmp.txt', 'D')
Bram Moolenaara42df592018-12-24 00:22:39 +0100758 new tmp.txt
759 q
Bram Moolenaara42df592018-12-24 00:22:39 +0100760 call Fun_RenewFile()
761 call assert_equal(2, winnr())
762 wincmd p
763 call assert_equal(1, winnr())
764 wincmd p
765 q
766 call Fun_RenewFile()
767 call assert_equal(2, winnr())
768 wincmd p
769 call assert_equal(1, winnr())
770 wincmd p
771 " reset
772 q
Bram Moolenaara42df592018-12-24 00:22:39 +0100773 set hidden&vim autoread&vim
774 delfunc Fun_RenewFile
775endfunc
776
Bram Moolenaar8fcb60f2019-03-04 13:18:30 +0100777func Test_relative_cursor_position_in_one_line_window()
778 new
779 only
780 call setline(1, range(1, 10000))
781 normal 50%
782 let lnum = getcurpos()[1]
783 split
784 split
785 " make third window take as many lines as possible, other windows will
786 " become one line
787 3wincmd w
788 for i in range(1, &lines - 6)
789 wincmd +
790 redraw!
791 endfor
792
793 " first and second window should show cursor line
794 let wininfo = getwininfo()
795 call assert_equal(lnum, wininfo[0].topline)
796 call assert_equal(lnum, wininfo[1].topline)
797
798 only!
799 bwipe!
Yegappan Lakshmanan04c4c572022-08-30 19:48:24 +0100800 call assert_fails('call winrestview(test_null_dict())', 'E1297:')
Bram Moolenaar8fcb60f2019-03-04 13:18:30 +0100801endfunc
802
803func Test_relative_cursor_position_after_move_and_resize()
804 let so_save = &so
805 set so=0
806 enew
807 call setline(1, range(1, 10000))
808 normal 50%
809 split
810 1wincmd w
811 " Move cursor to first line in window
812 normal H
813 redraw!
814 " Reduce window height to two lines
815 let height = winheight(0)
816 while winheight(0) > 2
817 wincmd -
818 redraw!
819 endwhile
820 " move cursor to second/last line in window
821 normal j
822 " restore previous height
823 while winheight(0) < height
824 wincmd +
825 redraw!
826 endwhile
827 " make window two lines again
828 while winheight(0) > 2
829 wincmd -
830 redraw!
831 endwhile
832
833 " cursor should be at bottom line
834 let info = getwininfo(win_getid())[0]
835 call assert_equal(info.topline + 1, getcurpos()[1])
836
837 only!
838 bwipe!
839 let &so = so_save
840endfunc
841
842func Test_relative_cursor_position_after_resize()
843 let so_save = &so
844 set so=0
845 enew
846 call setline(1, range(1, 10000))
847 normal 50%
848 split
849 1wincmd w
850 let winid1 = win_getid()
851 let info = getwininfo(winid1)[0]
852 " Move cursor to second line in window
853 exe "normal " . (info.topline + 1) . "G"
854 redraw!
855 let lnum = getcurpos()[1]
856
857 " Make the window only two lines high, cursor should end up in top line
858 2wincmd w
859 exe (info.height - 2) . "wincmd +"
860 redraw!
861 let info = getwininfo(winid1)[0]
862 call assert_equal(lnum, info.topline)
863
864 only!
865 bwipe!
866 let &so = so_save
867endfunc
868
869func Test_relative_cursor_second_line_after_resize()
870 let so_save = &so
871 set so=0
872 enew
873 call setline(1, range(1, 10000))
874 normal 50%
875 split
876 1wincmd w
877 let winid1 = win_getid()
878 let info = getwininfo(winid1)[0]
879
880 " Make the window only two lines high
881 2wincmd _
882
883 " Move cursor to second line in window
884 normal H
885 normal j
886
887 " Make window size bigger, then back to 2 lines
888 for i in range(1, 10)
889 wincmd +
890 redraw!
891 endfor
892 for i in range(1, 10)
893 wincmd -
894 redraw!
895 endfor
896
897 " cursor should end up in bottom line
898 let info = getwininfo(winid1)[0]
899 call assert_equal(info.topline + 1, getcurpos()[1])
900
901 only!
902 bwipe!
903 let &so = so_save
904endfunc
905
Bram Moolenaara9b25352019-05-12 14:25:30 +0200906func Test_split_noscroll()
907 let so_save = &so
Bram Moolenaarbd2d68c2019-05-18 15:36:11 +0200908 enew
909 call setline(1, range(1, 8))
Bram Moolenaara9b25352019-05-12 14:25:30 +0200910 normal 100%
911 split
912
913 1wincmd w
914 let winid1 = win_getid()
915 let info1 = getwininfo(winid1)[0]
916
917 2wincmd w
918 let winid2 = win_getid()
919 let info2 = getwininfo(winid2)[0]
920
921 call assert_equal(1, info1.topline)
922 call assert_equal(1, info2.topline)
923
Bram Moolenaarbd2d68c2019-05-18 15:36:11 +0200924 " window that fits all lines by itself, but not when split: closing other
925 " window should restore fraction.
Bram Moolenaara9b25352019-05-12 14:25:30 +0200926 only!
Bram Moolenaarbd2d68c2019-05-18 15:36:11 +0200927 call setline(1, range(1, &lines - 10))
928 exe &lines / 4
929 let winid1 = win_getid()
930 let info1 = getwininfo(winid1)[0]
931 call assert_equal(1, info1.topline)
932 new
933 redraw
934 close
935 let info1 = getwininfo(winid1)[0]
936 call assert_equal(1, info1.topline)
937
Bram Moolenaara9b25352019-05-12 14:25:30 +0200938 bwipe!
939 let &so = so_save
940endfunc
941
Bram Moolenaar46ad2882019-04-08 20:01:47 +0200942" Tests for the winnr() function
943func Test_winnr()
944 only | tabonly
945 call assert_equal(1, winnr('j'))
946 call assert_equal(1, winnr('k'))
947 call assert_equal(1, winnr('h'))
948 call assert_equal(1, winnr('l'))
949
950 " create a set of horizontally and vertically split windows
951 leftabove new | wincmd p
952 leftabove new | wincmd p
953 rightbelow new | wincmd p
954 rightbelow new | wincmd p
955 leftabove vnew | wincmd p
956 leftabove vnew | wincmd p
957 rightbelow vnew | wincmd p
958 rightbelow vnew | wincmd p
959
960 call assert_equal(8, winnr('j'))
961 call assert_equal(2, winnr('k'))
962 call assert_equal(4, winnr('h'))
963 call assert_equal(6, winnr('l'))
964 call assert_equal(9, winnr('2j'))
965 call assert_equal(1, winnr('2k'))
966 call assert_equal(3, winnr('2h'))
967 call assert_equal(7, winnr('2l'))
968
969 " Error cases
970 call assert_fails("echo winnr('0.2k')", 'E15:')
971 call assert_equal(2, winnr('-2k'))
972 call assert_fails("echo winnr('-2xj')", 'E15:')
973 call assert_fails("echo winnr('j2j')", 'E15:')
974 call assert_fails("echo winnr('ll')", 'E15:')
975 call assert_fails("echo winnr('5')", 'E15:')
976 call assert_equal(4, winnr('0h'))
Bram Moolenaar99fa7212020-04-26 15:59:55 +0200977 call assert_fails("let w = winnr([])", 'E730:')
978 call assert_equal('unknown', win_gettype(-1))
979 call assert_equal(-1, winheight(-1))
980 call assert_equal(-1, winwidth(-1))
Bram Moolenaar46ad2882019-04-08 20:01:47 +0200981
982 tabnew
983 call assert_equal(8, tabpagewinnr(1, 'j'))
Bram Moolenaarce90e362019-09-08 18:58:44 +0200984 call assert_equal(2, 1->tabpagewinnr('k'))
Bram Moolenaar46ad2882019-04-08 20:01:47 +0200985 call assert_equal(4, tabpagewinnr(1, 'h'))
986 call assert_equal(6, tabpagewinnr(1, 'l'))
987
988 only | tabonly
989endfunc
990
Bram Moolenaarf92e58c2019-09-08 21:51:41 +0200991func Test_winrestview()
992 split runtest.vim
993 normal 50%
994 let view = winsaveview()
995 close
996 split runtest.vim
997 eval view->winrestview()
998 call assert_equal(view, winsaveview())
999
1000 bwipe!
Yegappan Lakshmanan04c4c572022-08-30 19:48:24 +01001001 call assert_fails('call winrestview(test_null_dict())', 'E1297:')
Bram Moolenaarf92e58c2019-09-08 21:51:41 +02001002endfunc
1003
Bram Moolenaard20dcb32019-09-10 21:22:58 +02001004func Test_win_splitmove()
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01001005 CheckFeature quickfix
1006
Bram Moolenaard20dcb32019-09-10 21:22:58 +02001007 edit a
1008 leftabove split b
1009 leftabove vsplit c
1010 leftabove split d
1011 call assert_equal(0, win_splitmove(winnr(), winnr('l')))
1012 call assert_equal(bufname(winbufnr(1)), 'c')
1013 call assert_equal(bufname(winbufnr(2)), 'd')
1014 call assert_equal(bufname(winbufnr(3)), 'b')
1015 call assert_equal(bufname(winbufnr(4)), 'a')
1016 call assert_equal(0, win_splitmove(winnr(), winnr('j'), {'vertical': 1}))
1017 call assert_equal(0, win_splitmove(winnr(), winnr('j'), {'vertical': 1}))
1018 call assert_equal(bufname(winbufnr(1)), 'c')
1019 call assert_equal(bufname(winbufnr(2)), 'b')
1020 call assert_equal(bufname(winbufnr(3)), 'd')
1021 call assert_equal(bufname(winbufnr(4)), 'a')
1022 call assert_equal(0, win_splitmove(winnr(), winnr('k'), {'vertical': 1}))
1023 call assert_equal(bufname(winbufnr(1)), 'd')
1024 call assert_equal(bufname(winbufnr(2)), 'c')
1025 call assert_equal(bufname(winbufnr(3)), 'b')
1026 call assert_equal(bufname(winbufnr(4)), 'a')
1027 call assert_equal(0, win_splitmove(winnr(), winnr('j'), {'rightbelow': v:true}))
1028 call assert_equal(bufname(winbufnr(1)), 'c')
1029 call assert_equal(bufname(winbufnr(2)), 'b')
1030 call assert_equal(bufname(winbufnr(3)), 'a')
1031 call assert_equal(bufname(winbufnr(4)), 'd')
Yegappan Lakshmanan04c4c572022-08-30 19:48:24 +01001032 call assert_fails('call win_splitmove(winnr(), winnr("k"), test_null_dict())', 'E1297:')
Bram Moolenaard20dcb32019-09-10 21:22:58 +02001033 only | bd
1034
1035 call assert_fails('call win_splitmove(winnr(), 123)', 'E957:')
1036 call assert_fails('call win_splitmove(123, winnr())', 'E957:')
1037 call assert_fails('call win_splitmove(winnr(), winnr())', 'E957:')
Bram Moolenaar7b94e772020-01-06 21:03:24 +01001038
1039 tabnew
1040 call assert_fails('call win_splitmove(1, win_getid(1, 1))', 'E957:')
1041 tabclose
Bram Moolenaard20dcb32019-09-10 21:22:58 +02001042endfunc
1043
Bram Moolenaare20b9ec2020-02-03 21:40:04 +01001044" Test for the :only command
1045func Test_window_only()
1046 new
1047 set modified
1048 new
1049 call assert_fails('only', 'E445:')
1050 only!
Bram Moolenaarbc2b71d2020-02-17 21:33:30 +01001051 " Test for :only with a count
1052 let wid = win_getid()
1053 new
1054 new
1055 3only
1056 call assert_equal(1, winnr('$'))
1057 call assert_equal(wid, win_getid())
1058 call assert_fails('close', 'E444:')
1059 call assert_fails('%close', 'E16:')
Bram Moolenaare20b9ec2020-02-03 21:40:04 +01001060endfunc
1061
Bram Moolenaar9f6277b2020-02-11 22:04:02 +01001062" Test for errors with :wincmd
1063func Test_wincmd_errors()
1064 call assert_fails('wincmd g', 'E474:')
1065 call assert_fails('wincmd ab', 'E474:')
1066endfunc
1067
1068" Test for errors with :winpos
1069func Test_winpos_errors()
1070 if !has("gui_running") && !has('win32')
1071 call assert_fails('winpos', 'E188:')
1072 endif
1073 call assert_fails('winpos 10', 'E466:')
1074endfunc
1075
Bram Moolenaar406cd902020-02-18 21:54:41 +01001076" Test for +cmd in a :split command
1077func Test_split_cmd()
1078 split +set\ readonly
1079 call assert_equal(1, &readonly)
1080 call assert_equal(2, winnr('$'))
1081 close
1082endfunc
1083
Bram Moolenaar5080b0a2020-03-22 21:23:47 +01001084" Create maximum number of horizontally or vertically split windows and then
1085" run commands that create a new horizontally/vertically split window
1086func Run_noroom_for_newwindow_test(dir_arg)
1087 let dir = (a:dir_arg == 'v') ? 'vert ' : ''
1088
1089 " Open as many windows as possible
Bram Moolenaarab505b12020-03-23 19:28:44 +01001090 while v:true
Bram Moolenaar5080b0a2020-03-22 21:23:47 +01001091 try
1092 exe dir . 'new'
1093 catch /E36:/
1094 break
1095 endtry
Bram Moolenaarab505b12020-03-23 19:28:44 +01001096 endwhile
Bram Moolenaar5080b0a2020-03-22 21:23:47 +01001097
Bram Moolenaardb4c9472022-10-15 22:06:06 +01001098 call writefile(['first', 'second', 'third'], 'Xnorfile1', 'D')
1099 call writefile([], 'Xnorfile2', 'D')
1100 call writefile([], 'Xnorfile3', 'D')
Bram Moolenaar5080b0a2020-03-22 21:23:47 +01001101
1102 " Argument list related commands
Bram Moolenaarb18b4962022-09-02 21:55:50 +01001103 args Xnorfile1 Xnorfile2 Xnorfile3
Bram Moolenaar5080b0a2020-03-22 21:23:47 +01001104 next
1105 for cmd in ['sargument 2', 'snext', 'sprevious', 'sNext', 'srewind',
1106 \ 'sfirst', 'slast']
1107 call assert_fails(dir .. cmd, 'E36:')
1108 endfor
1109 %argdelete
1110
1111 " Buffer related commands
1112 set modified
1113 hide enew
Bram Moolenaarb18b4962022-09-02 21:55:50 +01001114 for cmd in ['sbuffer Xnorfile1', 'sbnext', 'sbprevious', 'sbNext', 'sbrewind',
Bram Moolenaar5080b0a2020-03-22 21:23:47 +01001115 \ 'sbfirst', 'sblast', 'sball', 'sbmodified', 'sunhide']
1116 call assert_fails(dir .. cmd, 'E36:')
1117 endfor
1118
1119 " Window related commands
Bram Moolenaarb18b4962022-09-02 21:55:50 +01001120 for cmd in ['split', 'split Xnorfile2', 'new', 'new Xnorfile3', 'sview Xnorfile1',
Bram Moolenaar5080b0a2020-03-22 21:23:47 +01001121 \ 'sfind runtest.vim']
1122 call assert_fails(dir .. cmd, 'E36:')
1123 endfor
1124
1125 " Help
1126 call assert_fails(dir .. 'help', 'E36:')
1127 call assert_fails(dir .. 'helpgrep window', 'E36:')
1128
1129 " Command-line window
1130 if a:dir_arg == 'h'
1131 " Cmd-line window is always a horizontally split window
1132 call assert_beeps('call feedkeys("q:\<CR>", "xt")')
1133 endif
1134
1135 " Quickfix and location list window
1136 if has('quickfix')
1137 cexpr ''
1138 call assert_fails(dir .. 'copen', 'E36:')
1139 lexpr ''
1140 call assert_fails(dir .. 'lopen', 'E36:')
1141
1142 " Preview window
Bram Moolenaarb18b4962022-09-02 21:55:50 +01001143 call assert_fails(dir .. 'pedit Xnorfile2', 'E36:')
Bram Moolenaar5080b0a2020-03-22 21:23:47 +01001144 call setline(1, 'abc')
1145 call assert_fails(dir .. 'psearch abc', 'E36:')
1146 endif
1147
1148 " Window commands (CTRL-W ^ and CTRL-W f)
1149 if a:dir_arg == 'h'
1150 call assert_fails('call feedkeys("\<C-W>^", "xt")', 'E36:')
Bram Moolenaarb18b4962022-09-02 21:55:50 +01001151 call setline(1, 'Xnorfile1')
Bram Moolenaar5080b0a2020-03-22 21:23:47 +01001152 call assert_fails('call feedkeys("gg\<C-W>f", "xt")', 'E36:')
1153 endif
1154 enew!
1155
1156 " Tag commands (:stag, :stselect and :stjump)
1157 call writefile(["!_TAG_FILE_ENCODING\tutf-8\t//",
Bram Moolenaarb18b4962022-09-02 21:55:50 +01001158 \ "second\tXnorfile1\t2",
1159 \ "third\tXnorfile1\t3",],
Bram Moolenaar5080b0a2020-03-22 21:23:47 +01001160 \ 'Xtags')
1161 set tags=Xtags
1162 call assert_fails(dir .. 'stag second', 'E36:')
1163 call assert_fails('call feedkeys(":" .. dir .. "stselect second\n1\n", "xt")', 'E36:')
1164 call assert_fails(dir .. 'stjump second', 'E36:')
1165 call assert_fails(dir .. 'ptag second', 'E36:')
1166 set tags&
1167 call delete('Xtags')
1168
1169 " :isplit and :dsplit
1170 call setline(1, ['#define FOO 1', 'FOO'])
1171 normal 2G
1172 call assert_fails(dir .. 'isplit FOO', 'E36:')
1173 call assert_fails(dir .. 'dsplit FOO', 'E36:')
1174
1175 " terminal
1176 if has('terminal')
1177 call assert_fails(dir .. 'terminal', 'E36:')
1178 endif
1179
1180 %bwipe!
Bram Moolenaar5080b0a2020-03-22 21:23:47 +01001181 only
1182endfunc
1183
1184func Test_split_cmds_with_no_room()
1185 call Run_noroom_for_newwindow_test('h')
1186 call Run_noroom_for_newwindow_test('v')
1187endfunc
1188
Bram Moolenaarad48e6c2020-04-21 22:19:45 +02001189" Test for various wincmd failures
1190func Test_wincmd_fails()
1191 only!
1192 call assert_beeps("normal \<C-W>w")
1193 call assert_beeps("normal \<C-W>p")
1194 call assert_beeps("normal \<C-W>gk")
1195 call assert_beeps("normal \<C-W>r")
1196 call assert_beeps("normal \<C-W>K")
1197 call assert_beeps("normal \<C-W>H")
1198 call assert_beeps("normal \<C-W>2gt")
1199endfunc
1200
Bram Moolenaarfe6dce82020-09-04 14:41:21 +02001201func Test_window_resize()
1202 " Vertical :resize (absolute, relative, min and max size).
1203 vsplit
1204 vert resize 8
1205 call assert_equal(8, winwidth(0))
1206 vert resize +2
1207 call assert_equal(10, winwidth(0))
1208 vert resize -2
1209 call assert_equal(8, winwidth(0))
1210 vert resize
1211 call assert_equal(&columns - 2, winwidth(0))
1212 vert resize 0
1213 call assert_equal(1, winwidth(0))
1214 vert resize 99999
1215 call assert_equal(&columns - 2, winwidth(0))
1216
1217 %bwipe!
1218
1219 " Horizontal :resize (with absolute, relative size, min and max size).
1220 split
1221 resize 8
1222 call assert_equal(8, winheight(0))
1223 resize +2
1224 call assert_equal(10, winheight(0))
1225 resize -2
1226 call assert_equal(8, winheight(0))
1227 resize
1228 call assert_equal(&lines - 4, winheight(0))
1229 resize 0
1230 call assert_equal(1, winheight(0))
1231 resize 99999
1232 call assert_equal(&lines - 4, winheight(0))
1233
1234 " :resize with explicit window number.
1235 let other_winnr = winnr('j')
1236 exe other_winnr .. 'resize 10'
1237 call assert_equal(10, winheight(other_winnr))
1238 call assert_equal(&lines - 10 - 3, winheight(0))
Bram Moolenaar9668cc52020-10-17 17:39:55 +02001239 exe other_winnr .. 'resize +1'
1240 exe other_winnr .. 'resize +1'
1241 call assert_equal(12, winheight(other_winnr))
1242 call assert_equal(&lines - 10 - 3 -2, winheight(0))
Bram Moolenaar89015a62020-12-29 12:46:51 +01001243 close
1244
1245 vsplit
1246 wincmd l
1247 let other_winnr = winnr('h')
1248 call assert_notequal(winnr(), other_winnr)
Bram Moolenaar5efe0e52021-01-01 14:31:34 +01001249 exe 'vert ' .. other_winnr .. 'resize -' .. &columns
Bram Moolenaar89015a62020-12-29 12:46:51 +01001250 call assert_equal(0, winwidth(other_winnr))
Bram Moolenaarfe6dce82020-09-04 14:41:21 +02001251
1252 %bwipe!
1253endfunc
1254
Bram Moolenaar5d3c9f82020-06-26 20:41:39 +02001255" Test for adjusting the window width when a window is closed with some
1256" windows using 'winfixwidth'
1257func Test_window_width_adjust()
1258 only
1259 " Three vertical windows. Windows 1 and 2 have 'winfixwidth' set and close
1260 " window 2.
1261 wincmd v
1262 vert resize 10
1263 set winfixwidth
1264 wincmd v
1265 set winfixwidth
1266 wincmd c
1267 call assert_inrange(10, 12, winwidth(1))
1268 " Three vertical windows. Windows 2 and 3 have 'winfixwidth' set and close
1269 " window 3.
1270 only
1271 set winfixwidth
1272 wincmd v
1273 vert resize 10
1274 set winfixwidth
1275 wincmd v
1276 set nowinfixwidth
1277 wincmd b
1278 wincmd c
1279 call assert_inrange(10, 12, winwidth(2))
1280
1281 new | only
1282endfunc
1283
1284" Test for jumping to a vertical/horizontal neighbor window based on the
1285" current cursor position
dundargocc57b5bc2022-11-02 13:30:51 +00001286func Test_window_goto_neighbor()
Bram Moolenaar5d3c9f82020-06-26 20:41:39 +02001287 %bw!
1288
1289 " Vertical window movement
1290
1291 " create the following window layout:
1292 " +--+--+
1293 " |w1|w3|
1294 " +--+ |
1295 " |w2| |
1296 " +--+--+
1297 " |w4 |
1298 " +-----+
1299 new
1300 vsplit
1301 split
1302 " vertically jump from w4
1303 wincmd b
1304 call setline(1, repeat(' ', &columns))
1305 call cursor(1, 1)
1306 wincmd k
1307 call assert_equal(2, winnr())
1308 wincmd b
1309 call cursor(1, &columns)
1310 redraw!
1311 wincmd k
1312 call assert_equal(3, winnr())
1313 %bw!
1314
1315 " create the following window layout:
1316 " +--+--+--+
1317 " |w1|w2|w3|
1318 " +--+--+--+
1319 " |w4 |
1320 " +--------+
1321 new
1322 vsplit
1323 vsplit
1324 wincmd b
1325 call setline(1, repeat(' ', &columns))
1326 call cursor(1, 1)
1327 wincmd k
1328 call assert_equal(1, winnr())
1329 wincmd b
1330 call cursor(1, &columns / 2)
1331 redraw!
1332 wincmd k
1333 call assert_equal(2, winnr())
1334 wincmd b
1335 call cursor(1, &columns)
1336 redraw!
1337 wincmd k
1338 call assert_equal(3, winnr())
1339 %bw!
1340
1341 " Horizontal window movement
1342
1343 " create the following window layout:
1344 " +--+--+--+
1345 " |w1|w2|w4|
1346 " +--+--+ |
1347 " |w3 | |
1348 " +-----+--+
1349 vsplit
1350 split
1351 vsplit
1352 4wincmd l
1353 call setline(1, repeat([' '], &lines))
1354 call cursor(1, 1)
1355 redraw!
1356 wincmd h
1357 call assert_equal(2, winnr())
1358 4wincmd l
1359 call cursor(&lines, 1)
1360 redraw!
1361 wincmd h
1362 call assert_equal(3, winnr())
1363 %bw!
1364
1365 " create the following window layout:
1366 " +--+--+
1367 " |w1|w4|
1368 " +--+ +
1369 " |w2| |
1370 " +--+ +
1371 " |w3| |
1372 " +--+--+
1373 vsplit
1374 split
1375 split
1376 wincmd l
1377 call setline(1, repeat([' '], &lines))
1378 call cursor(1, 1)
1379 redraw!
1380 wincmd h
1381 call assert_equal(1, winnr())
1382 wincmd l
1383 call cursor(&lines / 2, 1)
1384 redraw!
1385 wincmd h
1386 call assert_equal(2, winnr())
1387 wincmd l
1388 call cursor(&lines, 1)
1389 redraw!
1390 wincmd h
1391 call assert_equal(3, winnr())
1392 %bw!
1393endfunc
1394
1395" Test for an autocmd closing the destination window when jumping from one
1396" window to another.
1397func Test_close_dest_window()
1398 split
Bram Moolenaarb18b4962022-09-02 21:55:50 +01001399 edit Xdstfile
Bram Moolenaar5d3c9f82020-06-26 20:41:39 +02001400
1401 " Test for BufLeave
1402 augroup T1
1403 au!
Bram Moolenaarb18b4962022-09-02 21:55:50 +01001404 au BufLeave Xdstfile $wincmd c
Bram Moolenaar5d3c9f82020-06-26 20:41:39 +02001405 augroup END
1406 wincmd b
1407 call assert_equal(1, winnr('$'))
Bram Moolenaarb18b4962022-09-02 21:55:50 +01001408 call assert_equal('Xdstfile', @%)
Bram Moolenaar5d3c9f82020-06-26 20:41:39 +02001409 augroup T1
1410 au!
1411 augroup END
1412
1413 " Test for WinLeave
1414 new
1415 wincmd p
1416 augroup T1
1417 au!
1418 au WinLeave * 1wincmd c
1419 augroup END
1420 wincmd t
1421 call assert_equal(1, winnr('$'))
Bram Moolenaarb18b4962022-09-02 21:55:50 +01001422 call assert_equal('Xdstfile', @%)
Bram Moolenaar5d3c9f82020-06-26 20:41:39 +02001423 augroup T1
1424 au!
1425 augroup END
1426 augroup! T1
1427 %bw!
1428endfunc
1429
Bram Moolenaard0fb9072021-12-09 11:57:22 +00001430func Test_window_minimal_size()
1431 set winminwidth=0 winminheight=0
1432
1433 " check size is fixed vertically
1434 new
1435 call win_execute(win_getid(2), 'wincmd _')
1436 call assert_equal(0, winheight(0))
1437 call feedkeys('0', 'tx')
1438 call assert_equal(1, winheight(0))
1439 bwipe!
1440
1441 " check size is fixed horizontally
1442 vert new
1443 call win_execute(win_getid(2), 'wincmd |')
1444 call assert_equal(0, winwidth(0))
1445 call feedkeys('0', 'tx')
1446 call assert_equal(1, winwidth(0))
1447 bwipe!
1448
1449 if has('timers')
1450 " check size is fixed in Insert mode
ichizokfa9a8e02021-12-12 16:42:09 +00001451 func s:CheckSize(timer) abort
1452 call win_execute(win_getid(2), 'wincmd _')
1453 call assert_equal(0, winheight(0))
1454 call feedkeys(" \<Esc>", 't!')
1455 endfunc
Bram Moolenaard0fb9072021-12-09 11:57:22 +00001456 new
ichizokfa9a8e02021-12-12 16:42:09 +00001457 call timer_start(100, function('s:CheckSize'))
Bram Moolenaard0fb9072021-12-09 11:57:22 +00001458 call feedkeys('a', 'tx!')
1459 call assert_equal(1, winheight(0))
1460 bwipe!
ichizokfa9a8e02021-12-12 16:42:09 +00001461 delfunc s:CheckSize
Bram Moolenaard0fb9072021-12-09 11:57:22 +00001462 endif
1463
1464 set winminwidth& winminheight&
1465endfunc
1466
Daniel Steinbergee630312022-01-10 13:36:34 +00001467func Test_win_move_separator()
1468 edit a
1469 leftabove vsplit b
1470 let w = winwidth(0)
1471 " check win_move_separator from left window on left window
1472 call assert_equal(1, winnr())
1473 for offset in range(5)
1474 call assert_true(win_move_separator(0, offset))
1475 call assert_equal(w + offset, winwidth(0))
1476 call assert_true(0->win_move_separator(-offset))
1477 call assert_equal(w, winwidth(0))
1478 endfor
1479 " check win_move_separator from right window on left window number
1480 wincmd l
1481 call assert_notequal(1, winnr())
1482 for offset in range(5)
1483 call assert_true(1->win_move_separator(offset))
1484 call assert_equal(w + offset, winwidth(1))
1485 call assert_true(win_move_separator(1, -offset))
1486 call assert_equal(w, winwidth(1))
1487 endfor
1488 " check win_move_separator from right window on left window ID
1489 let id = win_getid(1)
1490 for offset in range(5)
1491 call assert_true(win_move_separator(id, offset))
1492 call assert_equal(w + offset, winwidth(id))
1493 call assert_true(id->win_move_separator(-offset))
1494 call assert_equal(w, winwidth(id))
1495 endfor
zeertzjqa0c4e2f2022-01-29 10:59:53 +00001496 " check win_move_separator from right window on right window is no-op
1497 let w0 = winwidth(0)
1498 call assert_true(win_move_separator(0, 1))
1499 call assert_equal(w0, winwidth(0))
1500 call assert_true(win_move_separator(0, -1))
1501 call assert_equal(w0, winwidth(0))
zeertzjq873f41a2022-11-01 11:44:43 +00001502
Daniel Steinbergee630312022-01-10 13:36:34 +00001503 " check that win_move_separator doesn't error with offsets beyond moving
1504 " possibility
1505 call assert_true(win_move_separator(id, 5000))
1506 call assert_true(winwidth(id) > w)
1507 call assert_true(win_move_separator(id, -5000))
1508 call assert_true(winwidth(id) < w)
zeertzjq873f41a2022-11-01 11:44:43 +00001509
Daniel Steinbergee630312022-01-10 13:36:34 +00001510 " check that win_move_separator returns false for an invalid window
1511 wincmd =
1512 let w = winwidth(0)
1513 call assert_false(win_move_separator(-1, 1))
1514 call assert_equal(w, winwidth(0))
zeertzjq873f41a2022-11-01 11:44:43 +00001515
Daniel Steinbergee630312022-01-10 13:36:34 +00001516 " check that win_move_separator returns false for a popup window
1517 let id = popup_create(['hello', 'world'], {})
1518 let w = winwidth(id)
1519 call assert_false(win_move_separator(id, 1))
1520 call assert_equal(w, winwidth(id))
1521 call popup_close(id)
zeertzjq873f41a2022-11-01 11:44:43 +00001522
1523 " check that using another tabpage fails without crash
1524 let id = win_getid()
1525 tabnew
1526 call assert_fails('call win_move_separator(id, -1)', 'E1308:')
1527 tabclose
1528
Daniel Steinbergee630312022-01-10 13:36:34 +00001529 %bwipe!
1530endfunc
1531
1532func Test_win_move_statusline()
1533 edit a
1534 leftabove split b
1535 let h = winheight(0)
1536 " check win_move_statusline from top window on top window
1537 call assert_equal(1, winnr())
1538 for offset in range(5)
1539 call assert_true(win_move_statusline(0, offset))
1540 call assert_equal(h + offset, winheight(0))
1541 call assert_true(0->win_move_statusline(-offset))
1542 call assert_equal(h, winheight(0))
1543 endfor
1544 " check win_move_statusline from bottom window on top window number
1545 wincmd j
1546 call assert_notequal(1, winnr())
1547 for offset in range(5)
1548 call assert_true(1->win_move_statusline(offset))
1549 call assert_equal(h + offset, winheight(1))
1550 call assert_true(win_move_statusline(1, -offset))
1551 call assert_equal(h, winheight(1))
1552 endfor
zeertzjqa0c4e2f2022-01-29 10:59:53 +00001553 " check win_move_statusline from bottom window on bottom window
1554 let h0 = winheight(0)
1555 for offset in range(5)
1556 call assert_true(0->win_move_statusline(-offset))
1557 call assert_equal(h0 - offset, winheight(0))
1558 call assert_equal(1 + offset, &cmdheight)
1559 call assert_true(win_move_statusline(0, offset))
1560 call assert_equal(h0, winheight(0))
1561 call assert_equal(1, &cmdheight)
1562 endfor
1563 call assert_true(win_move_statusline(0, 1))
Bram Moolenaara2a89732022-08-31 14:46:18 +01001564 call assert_equal(h0, winheight(0))
1565 call assert_equal(1, &cmdheight)
Daniel Steinbergee630312022-01-10 13:36:34 +00001566 " check win_move_statusline from bottom window on top window ID
1567 let id = win_getid(1)
1568 for offset in range(5)
1569 call assert_true(win_move_statusline(id, offset))
1570 call assert_equal(h + offset, winheight(id))
1571 call assert_true(id->win_move_statusline(-offset))
1572 call assert_equal(h, winheight(id))
1573 endfor
Bram Moolenaar86e67172022-10-31 12:24:12 +00001574
Daniel Steinbergee630312022-01-10 13:36:34 +00001575 " check that win_move_statusline doesn't error with offsets beyond moving
1576 " possibility
1577 call assert_true(win_move_statusline(id, 5000))
1578 call assert_true(winheight(id) > h)
1579 call assert_true(win_move_statusline(id, -5000))
1580 call assert_true(winheight(id) < h)
Bram Moolenaar86e67172022-10-31 12:24:12 +00001581
Daniel Steinbergee630312022-01-10 13:36:34 +00001582 " check that win_move_statusline returns false for an invalid window
1583 wincmd =
1584 let h = winheight(0)
1585 call assert_false(win_move_statusline(-1, 1))
1586 call assert_equal(h, winheight(0))
Bram Moolenaar86e67172022-10-31 12:24:12 +00001587
Daniel Steinbergee630312022-01-10 13:36:34 +00001588 " check that win_move_statusline returns false for a popup window
1589 let id = popup_create(['hello', 'world'], {})
1590 let h = winheight(id)
1591 call assert_false(win_move_statusline(id, 1))
1592 call assert_equal(h, winheight(id))
1593 call popup_close(id)
Bram Moolenaar86e67172022-10-31 12:24:12 +00001594
1595 " check that using another tabpage fails without crash
1596 let id = win_getid()
1597 tabnew
1598 call assert_fails('call win_move_statusline(id, -1)', 'E1308:')
1599 tabclose
1600
Daniel Steinbergee630312022-01-10 13:36:34 +00001601 %bwipe!
1602endfunc
1603
Yegappan Lakshmanan72bb47e2022-04-03 11:22:38 +01001604" Test for window allocation failure
1605func Test_window_alloc_failure()
1606 %bw!
1607
1608 " test for creating a new window above current window
1609 call test_alloc_fail(GetAllocId('newwin_wvars'), 0, 0)
1610 call assert_fails('above new', 'E342:')
1611 call assert_equal(1, winnr('$'))
1612
1613 " test for creating a new window below current window
1614 call test_alloc_fail(GetAllocId('newwin_wvars'), 0, 0)
1615 call assert_fails('below new', 'E342:')
1616 call assert_equal(1, winnr('$'))
1617
1618 " test for popup window creation failure
1619 call test_alloc_fail(GetAllocId('newwin_wvars'), 0, 0)
1620 call assert_fails('call popup_create("Hello", {})', 'E342:')
1621 call assert_equal([], popup_list())
1622
1623 call test_alloc_fail(GetAllocId('newwin_wvars'), 0, 0)
1624 call assert_fails('split', 'E342:')
1625 call assert_equal(1, winnr('$'))
1626
Bram Moolenaarb18b4962022-09-02 21:55:50 +01001627 edit Xwaffile1
1628 edit Xwaffile2
Yegappan Lakshmanan72bb47e2022-04-03 11:22:38 +01001629 call test_alloc_fail(GetAllocId('newwin_wvars'), 0, 0)
Bram Moolenaarb18b4962022-09-02 21:55:50 +01001630 call assert_fails('sb Xwaffile1', 'E342:')
Yegappan Lakshmanan72bb47e2022-04-03 11:22:38 +01001631 call assert_equal(1, winnr('$'))
Bram Moolenaarb18b4962022-09-02 21:55:50 +01001632 call assert_equal('Xwaffile2', @%)
Yegappan Lakshmanan72bb47e2022-04-03 11:22:38 +01001633 %bw!
1634
1635 " FIXME: The following test crashes Vim
1636 " test for new tabpage creation failure
1637 " call test_alloc_fail(GetAllocId('newwin_wvars'), 0, 0)
1638 " call assert_fails('tabnew', 'E342:')
1639 " call assert_equal(1, tabpagenr('$'))
1640 " call assert_equal(1, winnr('$'))
1641
1642 " This test messes up the internal Vim window/frame information. So the
1643 " Test_window_cmd_cmdwin_with_vsp() test fails after running this test.
1644 " Open a new tab and close everything else to fix this issue.
1645 tabnew
1646 tabonly
1647endfunc
Bram Moolenaard0fb9072021-12-09 11:57:22 +00001648
Luuk van Baalfd7e60a2022-09-07 14:42:49 +01001649func Test_win_equal_last_status()
1650 let save_lines = &lines
1651 set lines=20
1652 set splitbelow
1653 set laststatus=0
1654
1655 split | split | quit
1656 call assert_equal(winheight(1), winheight(2))
1657
1658 let &lines = save_lines
1659 set splitbelow&
1660 set laststatus&
1661endfunc
1662
Luuk van Baal13ece2a2022-10-03 15:28:08 +01001663" Test "screen" and "cursor" values for 'splitkeep' with a sequence of
Luuk van Baalfaf1d412022-09-19 16:45:29 +01001664" split operations for various options: with and without a winbar,
1665" tabline, for each possible value of 'laststatus', 'scrolloff',
Luuk van Baal13ece2a2022-10-03 15:28:08 +01001666" 'equalalways', and with the cursor at the top, middle and bottom.
1667func Test_splitkeep_options()
Luuk van Baal594f9e02022-09-16 12:52:58 +01001668 " disallow window resizing
1669 let save_WS = &t_WS
1670 set t_WS=
1671
Luuk van Baal29ab5242022-09-11 16:59:53 +01001672 let gui = has("gui_running")
Luuk van Baal594f9e02022-09-16 12:52:58 +01001673 inoremap <expr> c "<cmd>copen<bar>wincmd k<CR>"
Luuk van Baal13ece2a2022-10-03 15:28:08 +01001674 for run in range(0, 20)
1675 let &splitkeep = run > 10 ? 'topline' : 'screen'
1676 let &scrolloff = (!(run % 4) ? 0 : run)
1677 let &laststatus = (run % 3)
1678 let &splitbelow = (run % 3)
1679 let &equalalways = (run % 2)
1680 let wsb = (run % 2) && &splitbelow
1681 let tl = (gui ? 0 : ((run % 5) ? 1 : 0))
1682 let pos = !(run % 3) ? 'H' : ((run % 2) ? 'M' : 'L')
1683 tabnew | tabonly! | redraw
Luuk van Baal594f9e02022-09-16 12:52:58 +01001684 execute (run % 5) ? 'tabnew' : ''
1685 execute (run % 2) ? 'nnoremenu 1.10 WinBar.Test :echo' : ''
Luuk van Baal594f9e02022-09-16 12:52:58 +01001686 call setline(1, range(1, 256))
1687 " No scroll for restore_snapshot
1688 norm G
1689 try
1690 copen | close | colder
1691 catch /E380/
1692 endtry
1693 call assert_equal(257 - winheight(0), line("w0"))
Luuk van Baal29ab5242022-09-11 16:59:53 +01001694
Luuk van Baal594f9e02022-09-16 12:52:58 +01001695 " No scroll for firstwin horizontal split
1696 execute 'norm gg' . pos
1697 split | redraw | wincmd k
1698 call assert_equal(1, line("w0"))
1699 call assert_equal(&scroll, winheight(0) / 2)
1700 wincmd j
Luuk van Baal13ece2a2022-10-03 15:28:08 +01001701 call assert_equal(&spk == 'topline' ? 1 : win_screenpos(0)[0] - tl - wsb, line("w0"))
Luuk van Baal29ab5242022-09-11 16:59:53 +01001702
Luuk van Baal594f9e02022-09-16 12:52:58 +01001703 " No scroll when resizing windows
Luuk van Baal13ece2a2022-10-03 15:28:08 +01001704 wincmd k | resize +2 | redraw
Luuk van Baal594f9e02022-09-16 12:52:58 +01001705 call assert_equal(1, line("w0"))
1706 wincmd j
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 Baal29ab5242022-09-11 16:59:53 +01001708
Luuk van Baal594f9e02022-09-16 12:52:58 +01001709 " No scroll when dragging statusline
1710 call win_move_statusline(1, -3)
Luuk van Baal13ece2a2022-10-03 15:28:08 +01001711 call assert_equal(&spk == 'topline' ? 1 : win_screenpos(0)[0] - tl - wsb, line("w0"))
Luuk van Baal594f9e02022-09-16 12:52:58 +01001712 wincmd k
1713 call assert_equal(1, line("w0"))
Luuk van Baal29ab5242022-09-11 16:59:53 +01001714
Luuk van Baal594f9e02022-09-16 12:52:58 +01001715 " No scroll when changing shellsize
1716 set lines+=2
1717 call assert_equal(1, line("w0"))
1718 wincmd j
Luuk van Baal13ece2a2022-10-03 15:28:08 +01001719 call assert_equal(&spk == 'topline' ? 1 : win_screenpos(0)[0] - tl - wsb, line("w0"))
Luuk van Baal594f9e02022-09-16 12:52:58 +01001720 set lines-=2
Luuk van Baal13ece2a2022-10-03 15:28:08 +01001721 call assert_equal(&spk == 'topline' ? 1 : win_screenpos(0)[0] - tl - wsb, line("w0"))
Luuk van Baal594f9e02022-09-16 12:52:58 +01001722 wincmd k
1723 call assert_equal(1, line("w0"))
Luuk van Baal29ab5242022-09-11 16:59:53 +01001724
Luuk van Baal594f9e02022-09-16 12:52:58 +01001725 " No scroll when equalizing windows
1726 wincmd =
1727 call assert_equal(1, line("w0"))
1728 wincmd j
Luuk van Baal13ece2a2022-10-03 15:28:08 +01001729 call assert_equal(&spk == 'topline' ? 1 : win_screenpos(0)[0] - tl - wsb, line("w0"))
Luuk van Baal594f9e02022-09-16 12:52:58 +01001730 wincmd k
1731 call assert_equal(1, line("w0"))
Luuk van Baal29ab5242022-09-11 16:59:53 +01001732
Luuk van Baal594f9e02022-09-16 12:52:58 +01001733 " No scroll in windows split multiple times
1734 vsplit | split | 4wincmd w
Luuk van Baal13ece2a2022-10-03 15:28:08 +01001735 call assert_equal(&spk == 'topline' ? 1 : win_screenpos(0)[0] - tl - wsb, line("w0"))
Luuk van Baal594f9e02022-09-16 12:52:58 +01001736 1wincmd w | quit | wincmd l | split
Luuk van Baal13ece2a2022-10-03 15:28:08 +01001737 call assert_equal(&spk == 'topline' ? 1 : win_screenpos(0)[0] - tl - wsb, line("w0"))
Luuk van Baal594f9e02022-09-16 12:52:58 +01001738 wincmd j
Luuk van Baal13ece2a2022-10-03 15:28:08 +01001739 call assert_equal(&spk == 'topline' ? 1 : win_screenpos(0)[0] - tl - wsb, line("w0"))
Luuk van Baal29ab5242022-09-11 16:59:53 +01001740
Luuk van Baal594f9e02022-09-16 12:52:58 +01001741 " No scroll in small window
1742 2wincmd w | only | 5split | wincmd k
1743 call assert_equal(1, line("w0"))
1744 wincmd j
Luuk van Baal13ece2a2022-10-03 15:28:08 +01001745 call assert_equal(&spk == 'topline' ? 1 : win_screenpos(0)[0] - tl - wsb, line("w0"))
Luuk van Baal29ab5242022-09-11 16:59:53 +01001746
Luuk van Baal594f9e02022-09-16 12:52:58 +01001747 " No scroll for vertical split
1748 quit | vsplit | wincmd l
1749 call assert_equal(1, line("w0"))
1750 wincmd h
1751 call assert_equal(1, line("w0"))
Luuk van Baal29ab5242022-09-11 16:59:53 +01001752
Luuk van Baal594f9e02022-09-16 12:52:58 +01001753 " No scroll in windows split and quit multiple times
Luuk van Baal13ece2a2022-10-03 15:28:08 +01001754 quit | redraw | split | split | quit | redraw
1755 call assert_equal(&spk == 'topline' ? 1 : win_screenpos(0)[0] - tl - wsb, line("w0"))
Luuk van Baal29ab5242022-09-11 16:59:53 +01001756
Luuk van Baal594f9e02022-09-16 12:52:58 +01001757 " No scroll for new buffer
1758 1wincmd w | only | copen | wincmd k
1759 call assert_equal(1, line("w0"))
1760 only
1761 call assert_equal(1, line("w0"))
1762 above copen | wincmd j
Luuk van Baal13ece2a2022-10-03 15:28:08 +01001763 call assert_equal(&spk == 'topline' ? 1 : win_screenpos(0)[0] - tl, line("w0"))
Luuk van Baal29ab5242022-09-11 16:59:53 +01001764
Luuk van Baal594f9e02022-09-16 12:52:58 +01001765 " No scroll when opening cmdwin, and no cursor move when closing cmdwin.
1766 only | norm ggL
1767 let curpos = getcurpos()
1768 norm q:
1769 call assert_equal(1, line("w0"))
1770 call assert_equal(curpos, getcurpos())
Luuk van Baal29ab5242022-09-11 16:59:53 +01001771
Bram Moolenaarfdbd14e2023-02-14 21:56:42 +00001772 " Scroll when cursor becomes invalid in insert mode.
Luuk van Baal594f9e02022-09-16 12:52:58 +01001773 norm Lic
Luuk van Baalbc3dc292023-02-15 16:45:27 +00001774 call assert_equal(curpos, getcurpos(), 'run ' .. run)
Luuk van Baal29ab5242022-09-11 16:59:53 +01001775
Luuk van Baal594f9e02022-09-16 12:52:58 +01001776 " No scroll when topline not equal to 1
1777 only | execute "norm gg5\<C-e>" | split | wincmd k
1778 call assert_equal(6, line("w0"))
1779 wincmd j
Luuk van Baal13ece2a2022-10-03 15:28:08 +01001780 call assert_equal(&spk == 'topline' ? 6 : 5 + win_screenpos(0)[0] - tl - wsb, line("w0"))
Luuk van Baal29ab5242022-09-11 16:59:53 +01001781 endfor
1782
1783 tabnew | tabonly! | %bwipeout!
1784 iunmap c
Luuk van Baal29ab5242022-09-11 16:59:53 +01001785 set scrolloff&
1786 set splitbelow&
1787 set laststatus&
1788 set equalalways&
Luuk van Baal13ece2a2022-10-03 15:28:08 +01001789 set splitkeep&
Luuk van Baal594f9e02022-09-16 12:52:58 +01001790 let &t_WS = save_WS
Luuk van Baal29ab5242022-09-11 16:59:53 +01001791endfunc
Luuk van Baalfd7e60a2022-09-07 14:42:49 +01001792
Bram Moolenaare0f86912023-03-01 17:55:31 +00001793func Test_splitkeep_cmdwin_cursor_position()
Luuk van Baal13ece2a2022-10-03 15:28:08 +01001794 set splitkeep=screen
mityu12167d82022-09-15 17:44:07 +01001795 call setline(1, range(&lines))
1796
1797 " No scroll when cursor is at near bottom of window and cusor position
1798 " recompution (done by line('w0') in this test) happens while in cmdwin.
1799 normal! G
1800 let firstline = line('w0')
1801 autocmd CmdwinEnter * ++once autocmd WinEnter * ++once call line('w0')
1802 execute "normal! q:\<C-w>q"
1803 redraw!
1804 call assert_equal(firstline, line('w0'))
1805
1806 " User script can change cursor position successfully while in cmdwin and it
1807 " shouldn't be changed when closing cmdwin.
1808 execute "normal! Gq:\<Cmd>call win_execute(winnr('#')->win_getid(), 'call cursor(1, 1)')\<CR>\<C-w>q"
1809 call assert_equal(1, line('.'))
1810 call assert_equal(1, col('.'))
1811
1812 execute "normal! Gq:\<Cmd>autocmd WinEnter * ++once call cursor(1, 1)\<CR>\<C-w>q"
1813 call assert_equal(1, line('.'))
1814 call assert_equal(1, col('.'))
1815
1816 %bwipeout!
Luuk van Baal13ece2a2022-10-03 15:28:08 +01001817 set splitkeep&
Bram Moolenaare0f86912023-03-01 17:55:31 +00001818endfunc
Luuk van Baald5bc7622022-09-17 16:16:35 +01001819
Bram Moolenaare0f86912023-03-01 17:55:31 +00001820func Test_splitkeep_misc()
Luuk van Baal13ece2a2022-10-03 15:28:08 +01001821 set splitkeep=screen
Luuk van Baald5bc7622022-09-17 16:16:35 +01001822
1823 call setline(1, range(1, &lines))
Luuk van Baala109f392023-06-02 14:16:35 +01001824 " Cursor is adjusted to start and end of buffer
1825 norm M
1826 wincmd s
1827 resize 1
1828 call assert_equal(1, line('.'))
1829 wincmd j
1830 norm GM
1831 resize 1
1832 call assert_equal(&lines, line('.'))
1833 only!
1834
1835 set splitbelow
Luuk van Baald5bc7622022-09-17 16:16:35 +01001836 norm Gzz
1837 let top = line('w0')
Luuk van Baalfaf1d412022-09-19 16:45:29 +01001838 " No scroll when aucmd_win is opened
Luuk van Baald5bc7622022-09-17 16:16:35 +01001839 call setbufvar(bufnr("test", 1) , '&buftype', 'nofile')
1840 call assert_equal(top, line('w0'))
Luuk van Baalfaf1d412022-09-19 16:45:29 +01001841 " No scroll when tab is changed/closed
1842 tab help | close
1843 call assert_equal(top, line('w0'))
1844 " No scroll when help is closed and buffer line count < window height
1845 norm ggdG
1846 call setline(1, range(1, &lines - 10))
Luuk van Baald5bc7622022-09-17 16:16:35 +01001847 norm G
1848 let top = line('w0')
1849 help | quit
1850 call assert_equal(top, line('w0'))
Luuk van Baal346823d2022-10-05 18:26:42 +01001851 " No error when resizing window in autocmd and buffer length changed
1852 autocmd FileType qf exe "resize" line('$')
1853 cexpr getline(1, '$')
1854 copen
1855 wincmd p
1856 norm dd
1857 cexpr getline(1, '$')
Luuk van Baald5bc7622022-09-17 16:16:35 +01001858
Luuk van Baalfaf1d412022-09-19 16:45:29 +01001859 %bwipeout!
Luuk van Baald5bc7622022-09-17 16:16:35 +01001860 set splitbelow&
Luuk van Baal13ece2a2022-10-03 15:28:08 +01001861 set splitkeep&
Luuk van Baald5bc7622022-09-17 16:16:35 +01001862endfunc
1863
Bram Moolenaare0f86912023-03-01 17:55:31 +00001864func Test_splitkeep_callback()
Luuk van Baal20e58562022-09-23 12:57:09 +01001865 CheckScreendump
1866 let lines =<< trim END
Luuk van Baal13ece2a2022-10-03 15:28:08 +01001867 set splitkeep=screen
Luuk van Baal20e58562022-09-23 12:57:09 +01001868 call setline(1, range(&lines))
Luuk van Baal13ece2a2022-10-03 15:28:08 +01001869 function C1(a, b)
Luuk van Baal20e58562022-09-23 12:57:09 +01001870 split | wincmd p
1871 endfunction
Luuk van Baal13ece2a2022-10-03 15:28:08 +01001872 function C2(a, b)
Luuk van Baal20e58562022-09-23 12:57:09 +01001873 close | split
1874 endfunction
Luuk van Baal13ece2a2022-10-03 15:28:08 +01001875 nn j <cmd>call job_start([&sh, &shcf, "true"], { 'exit_cb': 'C1' })<CR>
1876 nn t <cmd>call popup_create(term_start([&sh, &shcf, "true"],
1877 \ { 'hidden': 1, 'exit_cb': 'C2' }), {})<CR>
Luuk van Baal20e58562022-09-23 12:57:09 +01001878 END
Luuk van Baal13ece2a2022-10-03 15:28:08 +01001879 call writefile(lines, 'XTestSplitkeepCallback', 'D')
1880 let buf = RunVimInTerminal('-S XTestSplitkeepCallback', #{rows: 8})
Luuk van Baal20e58562022-09-23 12:57:09 +01001881
1882 call term_sendkeys(buf, "j")
Luuk van Baal13ece2a2022-10-03 15:28:08 +01001883 call VerifyScreenDump(buf, 'Test_splitkeep_callback_1', {})
Luuk van Baal20e58562022-09-23 12:57:09 +01001884
Luuk van Baal13ece2a2022-10-03 15:28:08 +01001885 call term_sendkeys(buf, ":quit\<CR>Ht")
1886 call VerifyScreenDump(buf, 'Test_splitkeep_callback_2', {})
Luuk van Baal20e58562022-09-23 12:57:09 +01001887
1888 call term_sendkeys(buf, ":set sb\<CR>:quit\<CR>Gj")
Luuk van Baal13ece2a2022-10-03 15:28:08 +01001889 call VerifyScreenDump(buf, 'Test_splitkeep_callback_3', {})
Luuk van Baal20e58562022-09-23 12:57:09 +01001890
Luuk van Baal13ece2a2022-10-03 15:28:08 +01001891 call term_sendkeys(buf, ":quit\<CR>Gt")
1892 call VerifyScreenDump(buf, 'Test_splitkeep_callback_4', {})
zeertzjq378e6c02023-01-14 11:46:49 +00001893
1894 call StopVimInTerminal(buf)
Luuk van Baal20e58562022-09-23 12:57:09 +01001895endfunc
1896
Bram Moolenaare0f86912023-03-01 17:55:31 +00001897func Test_splitkeep_fold()
Luuk van Baal13ece2a2022-10-03 15:28:08 +01001898 CheckScreendump
Luuk van Baal7c1cbb62022-09-27 12:31:15 +01001899
Luuk van Baal13ece2a2022-10-03 15:28:08 +01001900 let lines =<< trim END
1901 set splitkeep=screen
1902 set foldmethod=marker
1903 set number
1904 let line = 1
1905 for n in range(1, &lines)
1906 call setline(line, ['int FuncName() {/*{{{*/', 1, 2, 3, 4, 5, '}/*}}}*/',
1907 \ 'after fold'])
1908 let line += 8
1909 endfor
1910 END
1911 call writefile(lines, 'XTestSplitkeepFold', 'D')
1912 let buf = RunVimInTerminal('-S XTestSplitkeepFold', #{rows: 10})
Luuk van Baal7c1cbb62022-09-27 12:31:15 +01001913
1914 call term_sendkeys(buf, "L:wincmd s\<CR>")
Luuk van Baal13ece2a2022-10-03 15:28:08 +01001915 call VerifyScreenDump(buf, 'Test_splitkeep_fold_1', {})
Luuk van Baal7c1cbb62022-09-27 12:31:15 +01001916
1917 call term_sendkeys(buf, ":quit\<CR>")
Luuk van Baal13ece2a2022-10-03 15:28:08 +01001918 call VerifyScreenDump(buf, 'Test_splitkeep_fold_2', {})
Luuk van Baal7c1cbb62022-09-27 12:31:15 +01001919
1920 call term_sendkeys(buf, "H:below split\<CR>")
Luuk van Baal13ece2a2022-10-03 15:28:08 +01001921 call VerifyScreenDump(buf, 'Test_splitkeep_fold_3', {})
Luuk van Baal7c1cbb62022-09-27 12:31:15 +01001922
1923 call term_sendkeys(buf, ":wincmd k\<CR>:quit\<CR>")
Luuk van Baal13ece2a2022-10-03 15:28:08 +01001924 call VerifyScreenDump(buf, 'Test_splitkeep_fold_4', {})
zeertzjq378e6c02023-01-14 11:46:49 +00001925
1926 call StopVimInTerminal(buf)
Bram Moolenaare0f86912023-03-01 17:55:31 +00001927endfunc
Luuk van Baal7c1cbb62022-09-27 12:31:15 +01001928
Bram Moolenaare0f86912023-03-01 17:55:31 +00001929func Test_splitkeep_status()
Luuk van Baal74a694d2022-11-28 16:49:36 +00001930 CheckScreendump
1931
1932 let lines =<< trim END
1933 call setline(1, ['a', 'b', 'c'])
1934 set nomodified
1935 set splitkeep=screen
1936 let win = winnr()
1937 wincmd s
1938 wincmd j
1939 END
1940 call writefile(lines, 'XTestSplitkeepStatus', 'D')
1941 let buf = RunVimInTerminal('-S XTestSplitkeepStatus', #{rows: 10})
1942
1943 call term_sendkeys(buf, ":call win_move_statusline(win, 1)\<CR>")
1944 call VerifyScreenDump(buf, 'Test_splitkeep_status_1', {})
zeertzjq378e6c02023-01-14 11:46:49 +00001945
1946 call StopVimInTerminal(buf)
Bram Moolenaare0f86912023-03-01 17:55:31 +00001947endfunc
Luuk van Baal74a694d2022-11-28 16:49:36 +00001948
Luuk van Baalb926bf42023-05-06 12:53:50 +01001949" skipcol is not reset unnecessarily and is copied to new window
1950func Test_splitkeep_skipcol()
1951 CheckScreendump
1952
1953 let lines =<< trim END
1954 set splitkeep=topline smoothscroll splitbelow scrolloff=0
1955 call setline(1, 'with lots of text in one line '->repeat(6))
1956 norm 2
1957 wincmd s
1958 END
1959
1960 call writefile(lines, 'XTestSplitkeepSkipcol', 'D')
1961 let buf = RunVimInTerminal('-S XTestSplitkeepSkipcol', #{rows: 12, cols: 40})
1962
1963 call VerifyScreenDump(buf, 'Test_splitkeep_skipcol_1', {})
1964endfunc
1965
Bram Moolenaare0f86912023-03-01 17:55:31 +00001966func Test_new_help_window_on_error()
Rob Pillingcb94c912022-12-13 12:26:09 +00001967 help change.txt
1968 execute "normal! /CTRL-@\<CR>"
1969 silent! execute "normal! \<C-W>]"
1970
1971 let wincount = winnr('$')
1972 help 'mod'
1973
1974 call assert_equal(wincount, winnr('$'))
1975 call assert_equal(expand("<cword>"), "'mod'")
Bram Moolenaare0f86912023-03-01 17:55:31 +00001976endfunc
1977
1978func Test_smoothscroll_in_zero_width_window()
1979 let save_lines = &lines
1980 let save_columns = &columns
1981
1982 winsize 0 24
1983 set cpo+=n
1984 exe "noremap 0 \<C-W>n\<C-W>L"
1985 norm 000000
1986 set number smoothscroll
1987 exe "norm \<C-Y>"
1988
1989 only!
1990 let &lines = save_lines
1991 let &columns = save_columns
1992 set cpo-=n
1993 unmap 0
1994 set nonumber nosmoothscroll
1995endfunc
Rob Pillingcb94c912022-12-13 12:26:09 +00001996
1997
Bram Moolenaar9e4d8212016-08-18 23:04:48 +02001998" vim: shiftwidth=2 sts=2 expandtab