blob: a97ccba2dee6485a91f65b9dd12451863ac30e25 [file] [log] [blame]
Bram Moolenaar991dea32016-05-24 11:31:32 +02001" Tests for window cmd (:wincmd, :split, :vsplit, :resize and etc...)
2
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +02003source check.vim
Luuk van Baal20e58562022-09-23 12:57:09 +01004source screendump.vim
Bram Moolenaara27e1dc2019-10-07 22:27:36 +02005
Bram Moolenaar991dea32016-05-24 11:31:32 +02006func Test_window_cmd_ls0_with_split()
7 set ls=0
8 set splitbelow
9 split
10 quit
11 call assert_equal(0, &lines - &cmdheight - winheight(0))
12 new | only!
13 "
14 set splitbelow&vim
15 botright split
16 quit
17 call assert_equal(0, &lines - &cmdheight - winheight(0))
18 new | only!
19 set ls&vim
20endfunc
21
22func Test_window_cmd_cmdwin_with_vsp()
Bram Moolenaar72cf47a2018-05-10 18:23:29 +020023 let efmt = 'Expected 0 but got %d (in ls=%d, %s window)'
Bram Moolenaar991dea32016-05-24 11:31:32 +020024 for v in range(0, 2)
25 exec "set ls=" . v
26 vsplit
27 call feedkeys("q:\<CR>")
28 let ac = &lines - (&cmdheight + winheight(0) + !!v)
29 let emsg = printf(efmt, ac, v, 'left')
30 call assert_equal(0, ac, emsg)
31 wincmd w
32 let ac = &lines - (&cmdheight + winheight(0) + !!v)
33 let emsg = printf(efmt, ac, v, 'right')
34 call assert_equal(0, ac, emsg)
35 new | only!
36 endfor
37 set ls&vim
38endfunc
39
Bram Moolenaar96bde992022-08-10 17:23:12 +010040func Test_cmdheight_not_changed()
41 set cmdheight=2
42 set winminheight=0
43 augroup Maximize
44 autocmd WinEnter * wincmd _
45 augroup END
46 split
47 tabnew
48 tabfirst
49 call assert_equal(2, &cmdheight)
50
51 tabonly!
52 only
53 set winminwidth& cmdheight&
54 augroup Maximize
55 au!
56 augroup END
57 augroup! Maximize
58endfunc
59
Bram Moolenaar5d3c9f82020-06-26 20:41:39 +020060" Test for jumping to windows
61func Test_window_jump()
62 new
63 " jumping to a window with a count greater than the max windows
64 exe "normal 4\<C-W>w"
65 call assert_equal(2, winnr())
66 only
67endfunc
68
69func Test_window_cmd_wincmd_gf()
Bram Moolenaar5d2ca042016-06-26 17:11:21 +020070 let fname = 'test_gf.txt'
71 let swp_fname = '.' . fname . '.swp'
Bram Moolenaardb4c9472022-10-15 22:06:06 +010072 call writefile([], fname, 'D')
73 call writefile([], swp_fname, 'D')
Bram Moolenaar5d2ca042016-06-26 17:11:21 +020074 function s:swap_exists()
75 let v:swapchoice = s:swap_choice
76 endfunc
Bram Moolenaareaa49e42019-07-13 18:08:59 +020077 " Remove the catch-all that runtest.vim adds
78 au! SwapExists
Bram Moolenaar5d2ca042016-06-26 17:11:21 +020079 augroup test_window_cmd_wincmd_gf
80 autocmd!
81 exec "autocmd SwapExists " . fname . " call s:swap_exists()"
82 augroup END
83
84 call setline(1, fname)
85 " (E)dit anyway
86 let s:swap_choice = 'e'
87 wincmd gf
88 call assert_equal(2, tabpagenr())
89 call assert_equal(fname, bufname("%"))
90 quit!
91
92 " (Q)uit
93 let s:swap_choice = 'q'
94 wincmd gf
95 call assert_equal(1, tabpagenr())
96 call assert_notequal(fname, bufname("%"))
97 new | only!
98
Bram Moolenaar5d2ca042016-06-26 17:11:21 +020099 augroup! test_window_cmd_wincmd_gf
100endfunc
101
Bram Moolenaar4520d442017-03-19 16:09:46 +0100102func Test_window_quit()
103 e Xa
104 split Xb
Bram Moolenaarf92e58c2019-09-08 21:51:41 +0200105 call assert_equal(2, '$'->winnr())
Bram Moolenaar4520d442017-03-19 16:09:46 +0100106 call assert_equal('Xb', bufname(winbufnr(1)))
107 call assert_equal('Xa', bufname(winbufnr(2)))
108
109 wincmd q
110 call assert_equal(1, winnr('$'))
111 call assert_equal('Xa', bufname(winbufnr(1)))
112
113 bw Xa Xb
114endfunc
115
116func Test_window_horizontal_split()
117 call assert_equal(1, winnr('$'))
118 3wincmd s
119 call assert_equal(2, winnr('$'))
120 call assert_equal(3, winheight(0))
Bram Moolenaarf92e58c2019-09-08 21:51:41 +0200121 call assert_equal(winwidth(1), 2->winwidth())
Bram Moolenaar4520d442017-03-19 16:09:46 +0100122
123 call assert_fails('botright topleft wincmd s', 'E442:')
124 bw
125endfunc
126
127func Test_window_vertical_split()
128 call assert_equal(1, winnr('$'))
129 3wincmd v
130 call assert_equal(2, winnr('$'))
131 call assert_equal(3, winwidth(0))
132 call assert_equal(winheight(1), winheight(2))
133
134 call assert_fails('botright topleft wincmd v', 'E442:')
135 bw
136endfunc
137
Bram Moolenaar1bbb6192018-11-10 16:02:01 +0100138" Test the ":wincmd ^" and "<C-W>^" commands.
Bram Moolenaar4520d442017-03-19 16:09:46 +0100139func Test_window_split_edit_alternate()
Bram Moolenaar1bbb6192018-11-10 16:02:01 +0100140 " Test for failure when the alternate buffer/file no longer exists.
141 edit Xfoo | %bw
Bram Moolenaare2e40752020-09-04 21:18:46 +0200142 call assert_fails(':wincmd ^', 'E23:')
Bram Moolenaar1bbb6192018-11-10 16:02:01 +0100143
144 " Test for the expected behavior when we have two named buffers.
145 edit Xfoo | edit Xbar
Bram Moolenaar4520d442017-03-19 16:09:46 +0100146 wincmd ^
Bram Moolenaar1bbb6192018-11-10 16:02:01 +0100147 call assert_equal('Xfoo', bufname(winbufnr(1)))
148 call assert_equal('Xbar', bufname(winbufnr(2)))
149 only
Bram Moolenaar4520d442017-03-19 16:09:46 +0100150
Bram Moolenaar1bbb6192018-11-10 16:02:01 +0100151 " Test for the expected behavior when the alternate buffer is not named.
152 enew | let l:nr1 = bufnr('%')
153 edit Xfoo | let l:nr2 = bufnr('%')
154 wincmd ^
155 call assert_equal(l:nr1, winbufnr(1))
156 call assert_equal(l:nr2, winbufnr(2))
157 only
158
Bram Moolenaard42333d2018-11-10 20:28:19 +0100159 " FIXME: this currently fails on AppVeyor, but passes locally
160 if !has('win32')
161 " Test the Normal mode command.
162 call feedkeys("\<C-W>\<C-^>", 'tx')
163 call assert_equal(l:nr2, winbufnr(1))
164 call assert_equal(l:nr1, winbufnr(2))
165 endif
Bram Moolenaar1bbb6192018-11-10 16:02:01 +0100166
167 %bw!
168endfunc
169
170" Test the ":[count]wincmd ^" and "[count]<C-W>^" commands.
171func Test_window_split_edit_bufnr()
Bram Moolenaar1bbb6192018-11-10 16:02:01 +0100172 %bwipeout
173 let l:nr = bufnr('%') + 1
Bram Moolenaare2e40752020-09-04 21:18:46 +0200174 call assert_fails(':execute "normal! ' . l:nr . '\<C-W>\<C-^>"', 'E92:')
175 call assert_fails(':' . l:nr . 'wincmd ^', 'E16:')
176 call assert_fails(':0wincmd ^', 'E16:')
Bram Moolenaar1bbb6192018-11-10 16:02:01 +0100177
178 edit Xfoo | edit Xbar | edit Xbaz
179 let l:foo_nr = bufnr('Xfoo')
180 let l:bar_nr = bufnr('Xbar')
181 let l:baz_nr = bufnr('Xbaz')
182
Bram Moolenaar8617b402018-11-10 20:47:48 +0100183 " FIXME: this currently fails on AppVeyor, but passes locally
184 if !has('win32')
185 call feedkeys(l:foo_nr . "\<C-W>\<C-^>", 'tx')
186 call assert_equal('Xfoo', bufname(winbufnr(1)))
187 call assert_equal('Xbaz', bufname(winbufnr(2)))
188 only
Bram Moolenaar1bbb6192018-11-10 16:02:01 +0100189
Bram Moolenaar8617b402018-11-10 20:47:48 +0100190 call feedkeys(l:bar_nr . "\<C-W>\<C-^>", 'tx')
191 call assert_equal('Xbar', bufname(winbufnr(1)))
192 call assert_equal('Xfoo', bufname(winbufnr(2)))
193 only
Bram Moolenaar1bbb6192018-11-10 16:02:01 +0100194
Bram Moolenaar8617b402018-11-10 20:47:48 +0100195 execute l:baz_nr . 'wincmd ^'
196 call assert_equal('Xbaz', bufname(winbufnr(1)))
197 call assert_equal('Xbar', bufname(winbufnr(2)))
198 endif
Bram Moolenaar1bbb6192018-11-10 16:02:01 +0100199
200 %bw!
Bram Moolenaar4520d442017-03-19 16:09:46 +0100201endfunc
202
Bram Moolenaar9b9be002020-03-22 14:41:22 +0100203func Test_window_split_no_room()
204 " N horizontal windows need >= 2*N + 1 lines:
205 " - 1 line + 1 status line in each window
206 " - 1 Ex command line
207 "
208 " 2*N + 1 <= &lines
209 " N <= (lines - 1)/2
210 "
211 " Beyond that number of windows, E36: Not enough room is expected.
212 let hor_win_count = (&lines - 1)/2
213 let hor_split_count = hor_win_count - 1
214 for s in range(1, hor_split_count) | split | endfor
215 call assert_fails('split', 'E36:')
216
217 " N vertical windows need >= 2*(N - 1) + 1 columns:
218 " - 1 column + 1 separator for each window (except last window)
219 " - 1 column for the last window which does not have separator
220 "
221 " 2*(N - 1) + 1 <= &columns
222 " 2*N - 1 <= &columns
223 " N <= (&columns + 1)/2
224 let ver_win_count = (&columns + 1)/2
225 let ver_split_count = ver_win_count - 1
226 for s in range(1, ver_split_count) | vsplit | endfor
227 call assert_fails('vsplit', 'E36:')
228
229 %bw!
230endfunc
231
Bram Moolenaar4520d442017-03-19 16:09:46 +0100232func Test_window_exchange()
233 e Xa
234
235 " Nothing happens with window exchange when there is 1 window
236 wincmd x
237 call assert_equal(1, winnr('$'))
238
239 split Xb
240 split Xc
241
242 call assert_equal('Xc', bufname(winbufnr(1)))
243 call assert_equal('Xb', bufname(winbufnr(2)))
244 call assert_equal('Xa', bufname(winbufnr(3)))
245
246 " Exchange current window 1 with window 3
247 3wincmd x
248 call assert_equal('Xa', bufname(winbufnr(1)))
249 call assert_equal('Xb', bufname(winbufnr(2)))
250 call assert_equal('Xc', bufname(winbufnr(3)))
251
252 " Exchange window with next when at the top window
253 wincmd x
254 call assert_equal('Xb', bufname(winbufnr(1)))
255 call assert_equal('Xa', bufname(winbufnr(2)))
256 call assert_equal('Xc', bufname(winbufnr(3)))
257
258 " Exchange window with next when at the middle window
259 wincmd j
260 wincmd x
261 call assert_equal('Xb', bufname(winbufnr(1)))
262 call assert_equal('Xc', bufname(winbufnr(2)))
263 call assert_equal('Xa', bufname(winbufnr(3)))
264
265 " Exchange window with next when at the bottom window.
266 " When there is no next window, it exchanges with the previous window.
267 wincmd j
268 wincmd x
269 call assert_equal('Xb', bufname(winbufnr(1)))
270 call assert_equal('Xa', bufname(winbufnr(2)))
271 call assert_equal('Xc', bufname(winbufnr(3)))
272
273 bw Xa Xb Xc
274endfunc
275
276func Test_window_rotate()
277 e Xa
278 split Xb
279 split Xc
280 call assert_equal('Xc', bufname(winbufnr(1)))
281 call assert_equal('Xb', bufname(winbufnr(2)))
282 call assert_equal('Xa', bufname(winbufnr(3)))
283
284 " Rotate downwards
285 wincmd r
286 call assert_equal('Xa', bufname(winbufnr(1)))
287 call assert_equal('Xc', bufname(winbufnr(2)))
288 call assert_equal('Xb', bufname(winbufnr(3)))
289
290 2wincmd r
291 call assert_equal('Xc', bufname(winbufnr(1)))
292 call assert_equal('Xb', bufname(winbufnr(2)))
293 call assert_equal('Xa', bufname(winbufnr(3)))
294
295 " Rotate upwards
296 wincmd R
297 call assert_equal('Xb', bufname(winbufnr(1)))
298 call assert_equal('Xa', bufname(winbufnr(2)))
299 call assert_equal('Xc', bufname(winbufnr(3)))
300
301 2wincmd R
302 call assert_equal('Xc', bufname(winbufnr(1)))
303 call assert_equal('Xb', bufname(winbufnr(2)))
304 call assert_equal('Xa', bufname(winbufnr(3)))
305
306 bot vsplit
307 call assert_fails('wincmd R', 'E443:')
308
309 bw Xa Xb Xc
310endfunc
311
312func Test_window_height()
313 e Xa
314 split Xb
315
316 let [wh1, wh2] = [winheight(1), winheight(2)]
317 " Active window (1) should have the same height or 1 more
318 " than the other window.
319 call assert_inrange(wh2, wh2 + 1, wh1)
320
321 wincmd -
322 call assert_equal(wh1 - 1, winheight(1))
323 call assert_equal(wh2 + 1, winheight(2))
324
325 wincmd +
326 call assert_equal(wh1, winheight(1))
Bram Moolenaarf92e58c2019-09-08 21:51:41 +0200327 call assert_equal(wh2, 2->winheight())
Bram Moolenaar4520d442017-03-19 16:09:46 +0100328
329 2wincmd _
330 call assert_equal(2, winheight(1))
331 call assert_equal(wh1 + wh2 - 2, winheight(2))
332
333 wincmd =
334 call assert_equal(wh1, winheight(1))
335 call assert_equal(wh2, winheight(2))
336
337 2wincmd _
338 set winfixheight
339 split Xc
340 let [wh1, wh2, wh3] = [winheight(1), winheight(2), winheight(3)]
341 call assert_equal(2, winheight(2))
342 call assert_inrange(wh3, wh3 + 1, wh1)
343 3wincmd +
344 call assert_equal(2, winheight(2))
345 call assert_equal(wh1 + 3, winheight(1))
346 call assert_equal(wh3 - 3, winheight(3))
347 wincmd =
348 call assert_equal(2, winheight(2))
349 call assert_equal(wh1, winheight(1))
350 call assert_equal(wh3, winheight(3))
351
352 wincmd j
353 set winfixheight&
354
355 wincmd =
356 let [wh1, wh2, wh3] = [winheight(1), winheight(2), winheight(3)]
357 " Current window (2) should have the same height or 1 more
358 " than the other windows.
359 call assert_inrange(wh1, wh1 + 1, wh2)
360 call assert_inrange(wh3, wh3 + 1, wh2)
361
362 bw Xa Xb Xc
363endfunc
364
Bram Moolenaar21c3a802022-08-31 17:49:14 +0100365func Test_wincmd_equal()
366 edit Xone
367 below split Xtwo
368 rightbelow vsplit Xthree
369 call assert_equal('Xone', bufname(winbufnr(1)))
370 call assert_equal('Xtwo', bufname(winbufnr(2)))
371 call assert_equal('Xthree', bufname(winbufnr(3)))
372
373 " Xone and Xtwo should be about the same height
374 let [wh1, wh2] = [winheight(1), winheight(2)]
375 call assert_inrange(wh1 - 1, wh1 + 1, wh2)
376 " Xtwo and Xthree should be about the same width
377 let [ww2, ww3] = [winwidth(2), winwidth(3)]
378 call assert_inrange(ww2 - 1, ww2 + 1, ww3)
379
380 1wincmd w
381 10wincmd _
382 2wincmd w
383 20wincmd |
384 call assert_equal(10, winheight(1))
385 call assert_equal(20, winwidth(2))
386
387 " equalizing horizontally doesn't change the heights
388 hor wincmd =
389 call assert_equal(10, winheight(1))
390 let [ww2, ww3] = [winwidth(2), winwidth(3)]
391 call assert_inrange(ww2 - 1, ww2 + 1, ww3)
392
393 2wincmd w
394 20wincmd |
395 call assert_equal(20, winwidth(2))
396 " equalizing vertically doesn't change the widths
397 vert wincmd =
398 call assert_equal(20, winwidth(2))
399 let [wh1, wh2] = [winheight(1), winheight(2)]
400 call assert_inrange(wh1 - 1, wh1 + 1, wh2)
401
402 bwipe Xone Xtwo Xthree
403endfunc
404
Bram Moolenaar4520d442017-03-19 16:09:46 +0100405func Test_window_width()
406 e Xa
407 vsplit Xb
408
409 let [ww1, ww2] = [winwidth(1), winwidth(2)]
410 " Active window (1) should have the same width or 1 more
411 " than the other window.
412 call assert_inrange(ww2, ww2 + 1, ww1)
413
414 wincmd <
415 call assert_equal(ww1 - 1, winwidth(1))
416 call assert_equal(ww2 + 1, winwidth(2))
417
418 wincmd >
419 call assert_equal(ww1, winwidth(1))
420 call assert_equal(ww2, winwidth(2))
421
422 2wincmd |
423 call assert_equal(2, winwidth(1))
424 call assert_equal(ww1 + ww2 - 2, winwidth(2))
425
426 wincmd =
427 call assert_equal(ww1, winwidth(1))
428 call assert_equal(ww2, winwidth(2))
429
430 2wincmd |
431 set winfixwidth
432 vsplit Xc
433 let [ww1, ww2, ww3] = [winwidth(1), winwidth(2), winwidth(3)]
Bram Moolenaar38e34832017-03-19 20:22:36 +0100434 call assert_equal(2, winwidth(2))
Bram Moolenaar4520d442017-03-19 16:09:46 +0100435 call assert_inrange(ww3, ww3 + 1, ww1)
436 3wincmd >
Bram Moolenaar38e34832017-03-19 20:22:36 +0100437 call assert_equal(2, winwidth(2))
Bram Moolenaar4520d442017-03-19 16:09:46 +0100438 call assert_equal(ww1 + 3, winwidth(1))
439 call assert_equal(ww3 - 3, winwidth(3))
440 wincmd =
Bram Moolenaar38e34832017-03-19 20:22:36 +0100441 call assert_equal(2, winwidth(2))
Bram Moolenaar4520d442017-03-19 16:09:46 +0100442 call assert_equal(ww1, winwidth(1))
443 call assert_equal(ww3, winwidth(3))
444
445 wincmd l
446 set winfixwidth&
447
448 wincmd =
449 let [ww1, ww2, ww3] = [winwidth(1), winwidth(2), winwidth(3)]
450 " Current window (2) should have the same width or 1 more
451 " than the other windows.
452 call assert_inrange(ww1, ww1 + 1, ww2)
453 call assert_inrange(ww3, ww3 + 1, ww2)
454
Yegappan Lakshmanan59585492021-06-12 13:46:41 +0200455 " when the current window width is less than the new 'winwidth', the current
456 " window width should be increased.
457 enew | only
458 split
459 10vnew
460 set winwidth=15
461 call assert_equal(15, winwidth(0))
462
463 %bw!
Bram Moolenaar4520d442017-03-19 16:09:46 +0100464endfunc
465
Bram Moolenaar8eeeba82017-06-25 22:45:39 +0200466func Test_equalalways_on_close()
467 set equalalways
468 vsplit
469 windo split
Bram Moolenaar1bbb6192018-11-10 16:02:01 +0100470 split
Bram Moolenaar8eeeba82017-06-25 22:45:39 +0200471 wincmd J
472 " now we have a frame top-left with two windows, a frame top-right with two
473 " windows and a frame at the bottom, full-width.
474 let height_1 = winheight(1)
475 let height_2 = winheight(2)
476 let height_3 = winheight(3)
477 let height_4 = winheight(4)
478 " closing the bottom window causes all windows to be resized.
479 close
480 call assert_notequal(height_1, winheight(1))
481 call assert_notequal(height_2, winheight(2))
482 call assert_notequal(height_3, winheight(3))
483 call assert_notequal(height_4, winheight(4))
484 call assert_equal(winheight(1), winheight(3))
485 call assert_equal(winheight(2), winheight(4))
486
487 1wincmd w
488 split
489 4wincmd w
490 resize + 5
491 " left column has three windows, equalized heights.
492 " right column has two windows, top one a bit higher
493 let height_1 = winheight(1)
494 let height_2 = winheight(2)
495 let height_4 = winheight(4)
496 let height_5 = winheight(5)
497 3wincmd w
498 " closing window in left column equalizes heights in left column but not in
499 " the right column
500 close
501 call assert_notequal(height_1, winheight(1))
502 call assert_notequal(height_2, winheight(2))
503 call assert_equal(height_4, winheight(3))
504 call assert_equal(height_5, winheight(4))
505
506 only
507 set equalalways&
508endfunc
509
Bram Moolenaar22044dc2017-12-02 15:43:37 +0100510func Test_win_screenpos()
Bram Moolenaar5a4c3082019-12-01 15:23:11 +0100511 CheckFeature quickfix
512
Bram Moolenaar22044dc2017-12-02 15:43:37 +0100513 call assert_equal(1, winnr('$'))
514 split
515 vsplit
516 10wincmd _
517 30wincmd |
518 call assert_equal([1, 1], win_screenpos(1))
519 call assert_equal([1, 32], win_screenpos(2))
520 call assert_equal([12, 1], win_screenpos(3))
521 call assert_equal([0, 0], win_screenpos(4))
Bram Moolenaar99fa7212020-04-26 15:59:55 +0200522 call assert_fails('let l = win_screenpos([])', 'E745:')
Bram Moolenaar22044dc2017-12-02 15:43:37 +0100523 only
524endfunc
525
Bram Moolenaar4520d442017-03-19 16:09:46 +0100526func Test_window_jump_tag()
Bram Moolenaar5a4c3082019-12-01 15:23:11 +0100527 CheckFeature quickfix
528
Bram Moolenaar4520d442017-03-19 16:09:46 +0100529 help
530 /iccf
531 call assert_match('^|iccf|', getline('.'))
532 call assert_equal(2, winnr('$'))
533 2wincmd }
534 call assert_equal(3, winnr('$'))
535 call assert_match('^|iccf|', getline('.'))
536 wincmd k
537 call assert_match('\*iccf\*', getline('.'))
538 call assert_equal(2, winheight(0))
539
540 wincmd z
541 set previewheight=4
542 help
543 /bugs
544 wincmd }
545 wincmd k
546 call assert_match('\*bugs\*', getline('.'))
547 call assert_equal(4, winheight(0))
548 set previewheight&
549
550 %bw!
551endfunc
552
553func Test_window_newtab()
554 e Xa
555
556 call assert_equal(1, tabpagenr('$'))
557 call assert_equal("\nAlready only one window", execute('wincmd T'))
558
559 split Xb
560 split Xc
561
562 wincmd T
563 call assert_equal(2, tabpagenr('$'))
564 call assert_equal(['Xb', 'Xa'], map(tabpagebuflist(1), 'bufname(v:val)'))
Bram Moolenaarce90e362019-09-08 18:58:44 +0200565 call assert_equal(['Xc' ], map(2->tabpagebuflist(), 'bufname(v:val)'))
Bram Moolenaar0e05de42020-03-25 22:23:46 +0100566 call assert_equal(['Xc' ], map(tabpagebuflist(), 'bufname(v:val)'))
Bram Moolenaar4520d442017-03-19 16:09:46 +0100567
568 %bw!
569endfunc
570
Bram Moolenaarf79225e2017-03-18 23:11:04 +0100571func Test_next_split_all()
572 " This was causing an illegal memory access.
573 n x
574 norm axxx
575 split
576 split
577 s/x
578 s/x
579 all
580 bwipe!
581endfunc
582
Bram Moolenaar75373f32017-08-07 22:02:30 +0200583" Tests for adjusting window and contents
584func GetScreenStr(row)
585 let str = ""
586 for c in range(1,3)
587 let str .= nr2char(screenchar(a:row, c))
588 endfor
589 return str
590endfunc
591
592func Test_window_contents()
593 enew! | only | new
594 call setline(1, range(1,256))
595
596 exe "norm! \<C-W>t\<C-W>=1Gzt\<C-W>w\<C-W>+"
597 redraw
Bram Moolenaar72cf47a2018-05-10 18:23:29 +0200598 let s3 = GetScreenStr(1)
Bram Moolenaar75373f32017-08-07 22:02:30 +0200599 wincmd p
600 call assert_equal(1, line("w0"))
601 call assert_equal('1 ', s3)
602
603 exe "norm! \<C-W>t\<C-W>=50Gzt\<C-W>w\<C-W>+"
604 redraw
Bram Moolenaar72cf47a2018-05-10 18:23:29 +0200605 let s3 = GetScreenStr(1)
Bram Moolenaar75373f32017-08-07 22:02:30 +0200606 wincmd p
607 call assert_equal(50, line("w0"))
608 call assert_equal('50 ', s3)
609
610 exe "norm! \<C-W>t\<C-W>=59Gzt\<C-W>w\<C-W>+"
611 redraw
Bram Moolenaar72cf47a2018-05-10 18:23:29 +0200612 let s3 = GetScreenStr(1)
Bram Moolenaar75373f32017-08-07 22:02:30 +0200613 wincmd p
614 call assert_equal(59, line("w0"))
615 call assert_equal('59 ', s3)
616
Bram Moolenaar8b633132020-03-20 18:20:51 +0100617 %d
618 call setline(1, ['one', 'two', 'three'])
619 call assert_equal(1, line('w0'))
620 call assert_equal(3, line('w$'))
621
Bram Moolenaar75373f32017-08-07 22:02:30 +0200622 bwipeout!
623 call test_garbagecollect_now()
624endfunc
625
Bram Moolenaar2efb3232017-12-19 12:27:23 +0100626func Test_window_colon_command()
627 " This was reading invalid memory.
628 exe "norm! v\<C-W>:\<C-U>echo v:version"
629endfunc
630
Bram Moolenaar6f361c92018-01-31 19:06:50 +0100631func Test_access_freed_mem()
Bram Moolenaar1417c762019-07-27 17:31:36 +0200632 call assert_equal(&columns, winwidth(0))
Bram Moolenaar9a046fd2021-01-28 13:47:59 +0100633 " This was accessing freed memory (but with what events?)
634 au BufEnter,BufLeave,WinEnter,WinLeave 0 vs xxx
Bram Moolenaar6f361c92018-01-31 19:06:50 +0100635 arg 0
636 argadd
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +0200637 call assert_fails("all", "E242:")
Bram Moolenaar6f361c92018-01-31 19:06:50 +0100638 au!
639 bwipe xxx
Bram Moolenaar1417c762019-07-27 17:31:36 +0200640 call assert_equal(&columns, winwidth(0))
Bram Moolenaar6f361c92018-01-31 19:06:50 +0100641endfunc
642
Bram Moolenaara27e1dc2019-10-07 22:27:36 +0200643func Test_insert_cleared_on_switch_to_term()
644 CheckFeature terminal
645
646 set showmode
647 terminal
648 wincmd p
649
650 call feedkeys("i\<C-O>", 'ntx')
651 redraw
652
653 " The "-- (insert) --" indicator should be visible.
654 let chars = map(range(1, &columns), 'nr2char(screenchar(&lines, v:val))')
655 let str = trim(join(chars, ''))
656 call assert_equal('-- (insert) --', str)
657
658 call feedkeys("\<C-W>p", 'ntx')
659 redraw
660
661 " The "-- (insert) --" indicator should have been cleared.
662 let chars = map(range(1, &columns), 'nr2char(screenchar(&lines, v:val))')
663 let str = trim(join(chars, ''))
664 call assert_equal('', str)
665
666 set showmode&
667 %bw!
668endfunc
669
Bram Moolenaar5bab5552018-04-13 20:41:29 +0200670func Test_visual_cleared_after_window_split()
671 new | only!
672 let smd_save = &showmode
673 set showmode
674 let ls_save = &laststatus
675 set laststatus=1
676 call setline(1, ['a', 'b', 'c', 'd', ''])
677 norm! G
678 exe "norm! kkvk"
679 redraw
680 exe "norm! \<C-W>v"
681 redraw
682 " check if '-- VISUAL --' disappeared from command line
683 let columns = range(1, &columns)
684 let cmdlinechars = map(columns, 'nr2char(screenchar(&lines, v:val))')
685 let cmdline = join(cmdlinechars, '')
686 let cmdline_ltrim = substitute(cmdline, '^\s*', "", "")
687 let mode_shown = substitute(cmdline_ltrim, '\s*$', "", "")
688 call assert_equal('', mode_shown)
689 let &showmode = smd_save
690 let &laststatus = ls_save
691 bwipe!
692endfunc
693
Bram Moolenaar72cf47a2018-05-10 18:23:29 +0200694func Test_winrestcmd()
695 2split
696 3vsplit
Bram Moolenaara0c8aea2021-03-20 19:55:35 +0100697 let restcmd = winrestcmd()
Bram Moolenaar72cf47a2018-05-10 18:23:29 +0200698 call assert_equal(2, winheight(0))
699 call assert_equal(3, winwidth(0))
700 wincmd =
701 call assert_notequal(2, winheight(0))
702 call assert_notequal(3, winwidth(0))
Bram Moolenaara0c8aea2021-03-20 19:55:35 +0100703 exe restcmd
Bram Moolenaar72cf47a2018-05-10 18:23:29 +0200704 call assert_equal(2, winheight(0))
705 call assert_equal(3, winwidth(0))
706 only
Bram Moolenaara0c8aea2021-03-20 19:55:35 +0100707
708 wincmd v
709 wincmd s
710 wincmd v
711 redraw
712 let restcmd = winrestcmd()
713 wincmd _
714 wincmd |
715 exe restcmd
716 redraw
717 call assert_equal(restcmd, winrestcmd())
718
719 only
Bram Moolenaar72cf47a2018-05-10 18:23:29 +0200720endfunc
721
Bram Moolenaar1e115362019-01-09 23:01:02 +0100722func Fun_RenewFile()
Bram Moolenaar026587b2019-08-17 15:08:00 +0200723 " Need to wait a bit for the timestamp to be older.
Bram Moolenaarce90e362019-09-08 18:58:44 +0200724 let old_ftime = getftime("tmp.txt")
725 while getftime("tmp.txt") == old_ftime
726 sleep 100m
727 silent execute '!echo "1" > tmp.txt'
728 endwhile
Bram Moolenaara42df592018-12-24 00:22:39 +0100729 sp
730 wincmd p
731 edit! tmp.txt
Bram Moolenaar1e115362019-01-09 23:01:02 +0100732endfunc
Bram Moolenaara42df592018-12-24 00:22:39 +0100733
734func Test_window_prevwin()
735 " Can we make this work on MS-Windows?
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +0200736 CheckUnix
Bram Moolenaara42df592018-12-24 00:22:39 +0100737
738 set hidden autoread
Bram Moolenaardb4c9472022-10-15 22:06:06 +0100739 call writefile(['2'], 'tmp.txt', 'D')
Bram Moolenaara42df592018-12-24 00:22:39 +0100740 new tmp.txt
741 q
Bram Moolenaara42df592018-12-24 00:22:39 +0100742 call Fun_RenewFile()
743 call assert_equal(2, winnr())
744 wincmd p
745 call assert_equal(1, winnr())
746 wincmd p
747 q
748 call Fun_RenewFile()
749 call assert_equal(2, winnr())
750 wincmd p
751 call assert_equal(1, winnr())
752 wincmd p
753 " reset
754 q
Bram Moolenaara42df592018-12-24 00:22:39 +0100755 set hidden&vim autoread&vim
756 delfunc Fun_RenewFile
757endfunc
758
Bram Moolenaar8fcb60f2019-03-04 13:18:30 +0100759func Test_relative_cursor_position_in_one_line_window()
760 new
761 only
762 call setline(1, range(1, 10000))
763 normal 50%
764 let lnum = getcurpos()[1]
765 split
766 split
767 " make third window take as many lines as possible, other windows will
768 " become one line
769 3wincmd w
770 for i in range(1, &lines - 6)
771 wincmd +
772 redraw!
773 endfor
774
775 " first and second window should show cursor line
776 let wininfo = getwininfo()
777 call assert_equal(lnum, wininfo[0].topline)
778 call assert_equal(lnum, wininfo[1].topline)
779
780 only!
781 bwipe!
Yegappan Lakshmanan04c4c572022-08-30 19:48:24 +0100782 call assert_fails('call winrestview(test_null_dict())', 'E1297:')
Bram Moolenaar8fcb60f2019-03-04 13:18:30 +0100783endfunc
784
785func Test_relative_cursor_position_after_move_and_resize()
786 let so_save = &so
787 set so=0
788 enew
789 call setline(1, range(1, 10000))
790 normal 50%
791 split
792 1wincmd w
793 " Move cursor to first line in window
794 normal H
795 redraw!
796 " Reduce window height to two lines
797 let height = winheight(0)
798 while winheight(0) > 2
799 wincmd -
800 redraw!
801 endwhile
802 " move cursor to second/last line in window
803 normal j
804 " restore previous height
805 while winheight(0) < height
806 wincmd +
807 redraw!
808 endwhile
809 " make window two lines again
810 while winheight(0) > 2
811 wincmd -
812 redraw!
813 endwhile
814
815 " cursor should be at bottom line
816 let info = getwininfo(win_getid())[0]
817 call assert_equal(info.topline + 1, getcurpos()[1])
818
819 only!
820 bwipe!
821 let &so = so_save
822endfunc
823
824func Test_relative_cursor_position_after_resize()
825 let so_save = &so
826 set so=0
827 enew
828 call setline(1, range(1, 10000))
829 normal 50%
830 split
831 1wincmd w
832 let winid1 = win_getid()
833 let info = getwininfo(winid1)[0]
834 " Move cursor to second line in window
835 exe "normal " . (info.topline + 1) . "G"
836 redraw!
837 let lnum = getcurpos()[1]
838
839 " Make the window only two lines high, cursor should end up in top line
840 2wincmd w
841 exe (info.height - 2) . "wincmd +"
842 redraw!
843 let info = getwininfo(winid1)[0]
844 call assert_equal(lnum, info.topline)
845
846 only!
847 bwipe!
848 let &so = so_save
849endfunc
850
851func Test_relative_cursor_second_line_after_resize()
852 let so_save = &so
853 set so=0
854 enew
855 call setline(1, range(1, 10000))
856 normal 50%
857 split
858 1wincmd w
859 let winid1 = win_getid()
860 let info = getwininfo(winid1)[0]
861
862 " Make the window only two lines high
863 2wincmd _
864
865 " Move cursor to second line in window
866 normal H
867 normal j
868
869 " Make window size bigger, then back to 2 lines
870 for i in range(1, 10)
871 wincmd +
872 redraw!
873 endfor
874 for i in range(1, 10)
875 wincmd -
876 redraw!
877 endfor
878
879 " cursor should end up in bottom line
880 let info = getwininfo(winid1)[0]
881 call assert_equal(info.topline + 1, getcurpos()[1])
882
883 only!
884 bwipe!
885 let &so = so_save
886endfunc
887
Bram Moolenaara9b25352019-05-12 14:25:30 +0200888func Test_split_noscroll()
889 let so_save = &so
Bram Moolenaarbd2d68c2019-05-18 15:36:11 +0200890 enew
891 call setline(1, range(1, 8))
Bram Moolenaara9b25352019-05-12 14:25:30 +0200892 normal 100%
893 split
894
895 1wincmd w
896 let winid1 = win_getid()
897 let info1 = getwininfo(winid1)[0]
898
899 2wincmd w
900 let winid2 = win_getid()
901 let info2 = getwininfo(winid2)[0]
902
903 call assert_equal(1, info1.topline)
904 call assert_equal(1, info2.topline)
905
Bram Moolenaarbd2d68c2019-05-18 15:36:11 +0200906 " window that fits all lines by itself, but not when split: closing other
907 " window should restore fraction.
Bram Moolenaara9b25352019-05-12 14:25:30 +0200908 only!
Bram Moolenaarbd2d68c2019-05-18 15:36:11 +0200909 call setline(1, range(1, &lines - 10))
910 exe &lines / 4
911 let winid1 = win_getid()
912 let info1 = getwininfo(winid1)[0]
913 call assert_equal(1, info1.topline)
914 new
915 redraw
916 close
917 let info1 = getwininfo(winid1)[0]
918 call assert_equal(1, info1.topline)
919
Bram Moolenaara9b25352019-05-12 14:25:30 +0200920 bwipe!
921 let &so = so_save
922endfunc
923
Bram Moolenaar46ad2882019-04-08 20:01:47 +0200924" Tests for the winnr() function
925func Test_winnr()
926 only | tabonly
927 call assert_equal(1, winnr('j'))
928 call assert_equal(1, winnr('k'))
929 call assert_equal(1, winnr('h'))
930 call assert_equal(1, winnr('l'))
931
932 " create a set of horizontally and vertically split windows
933 leftabove new | wincmd p
934 leftabove new | wincmd p
935 rightbelow new | wincmd p
936 rightbelow new | wincmd p
937 leftabove vnew | wincmd p
938 leftabove vnew | wincmd p
939 rightbelow vnew | wincmd p
940 rightbelow vnew | wincmd p
941
942 call assert_equal(8, winnr('j'))
943 call assert_equal(2, winnr('k'))
944 call assert_equal(4, winnr('h'))
945 call assert_equal(6, winnr('l'))
946 call assert_equal(9, winnr('2j'))
947 call assert_equal(1, winnr('2k'))
948 call assert_equal(3, winnr('2h'))
949 call assert_equal(7, winnr('2l'))
950
951 " Error cases
952 call assert_fails("echo winnr('0.2k')", 'E15:')
953 call assert_equal(2, winnr('-2k'))
954 call assert_fails("echo winnr('-2xj')", 'E15:')
955 call assert_fails("echo winnr('j2j')", 'E15:')
956 call assert_fails("echo winnr('ll')", 'E15:')
957 call assert_fails("echo winnr('5')", 'E15:')
958 call assert_equal(4, winnr('0h'))
Bram Moolenaar99fa7212020-04-26 15:59:55 +0200959 call assert_fails("let w = winnr([])", 'E730:')
960 call assert_equal('unknown', win_gettype(-1))
961 call assert_equal(-1, winheight(-1))
962 call assert_equal(-1, winwidth(-1))
Bram Moolenaar46ad2882019-04-08 20:01:47 +0200963
964 tabnew
965 call assert_equal(8, tabpagewinnr(1, 'j'))
Bram Moolenaarce90e362019-09-08 18:58:44 +0200966 call assert_equal(2, 1->tabpagewinnr('k'))
Bram Moolenaar46ad2882019-04-08 20:01:47 +0200967 call assert_equal(4, tabpagewinnr(1, 'h'))
968 call assert_equal(6, tabpagewinnr(1, 'l'))
969
970 only | tabonly
971endfunc
972
Bram Moolenaarf92e58c2019-09-08 21:51:41 +0200973func Test_winrestview()
974 split runtest.vim
975 normal 50%
976 let view = winsaveview()
977 close
978 split runtest.vim
979 eval view->winrestview()
980 call assert_equal(view, winsaveview())
981
982 bwipe!
Yegappan Lakshmanan04c4c572022-08-30 19:48:24 +0100983 call assert_fails('call winrestview(test_null_dict())', 'E1297:')
Bram Moolenaarf92e58c2019-09-08 21:51:41 +0200984endfunc
985
Bram Moolenaard20dcb32019-09-10 21:22:58 +0200986func Test_win_splitmove()
Bram Moolenaar5a4c3082019-12-01 15:23:11 +0100987 CheckFeature quickfix
988
Bram Moolenaard20dcb32019-09-10 21:22:58 +0200989 edit a
990 leftabove split b
991 leftabove vsplit c
992 leftabove split d
993 call assert_equal(0, win_splitmove(winnr(), winnr('l')))
994 call assert_equal(bufname(winbufnr(1)), 'c')
995 call assert_equal(bufname(winbufnr(2)), 'd')
996 call assert_equal(bufname(winbufnr(3)), 'b')
997 call assert_equal(bufname(winbufnr(4)), 'a')
998 call assert_equal(0, win_splitmove(winnr(), winnr('j'), {'vertical': 1}))
999 call assert_equal(0, win_splitmove(winnr(), winnr('j'), {'vertical': 1}))
1000 call assert_equal(bufname(winbufnr(1)), 'c')
1001 call assert_equal(bufname(winbufnr(2)), 'b')
1002 call assert_equal(bufname(winbufnr(3)), 'd')
1003 call assert_equal(bufname(winbufnr(4)), 'a')
1004 call assert_equal(0, win_splitmove(winnr(), winnr('k'), {'vertical': 1}))
1005 call assert_equal(bufname(winbufnr(1)), 'd')
1006 call assert_equal(bufname(winbufnr(2)), 'c')
1007 call assert_equal(bufname(winbufnr(3)), 'b')
1008 call assert_equal(bufname(winbufnr(4)), 'a')
1009 call assert_equal(0, win_splitmove(winnr(), winnr('j'), {'rightbelow': v:true}))
1010 call assert_equal(bufname(winbufnr(1)), 'c')
1011 call assert_equal(bufname(winbufnr(2)), 'b')
1012 call assert_equal(bufname(winbufnr(3)), 'a')
1013 call assert_equal(bufname(winbufnr(4)), 'd')
Yegappan Lakshmanan04c4c572022-08-30 19:48:24 +01001014 call assert_fails('call win_splitmove(winnr(), winnr("k"), test_null_dict())', 'E1297:')
Bram Moolenaard20dcb32019-09-10 21:22:58 +02001015 only | bd
1016
1017 call assert_fails('call win_splitmove(winnr(), 123)', 'E957:')
1018 call assert_fails('call win_splitmove(123, winnr())', 'E957:')
1019 call assert_fails('call win_splitmove(winnr(), winnr())', 'E957:')
Bram Moolenaar7b94e772020-01-06 21:03:24 +01001020
1021 tabnew
1022 call assert_fails('call win_splitmove(1, win_getid(1, 1))', 'E957:')
1023 tabclose
Bram Moolenaard20dcb32019-09-10 21:22:58 +02001024endfunc
1025
Bram Moolenaare20b9ec2020-02-03 21:40:04 +01001026" Test for the :only command
1027func Test_window_only()
1028 new
1029 set modified
1030 new
1031 call assert_fails('only', 'E445:')
1032 only!
Bram Moolenaarbc2b71d2020-02-17 21:33:30 +01001033 " Test for :only with a count
1034 let wid = win_getid()
1035 new
1036 new
1037 3only
1038 call assert_equal(1, winnr('$'))
1039 call assert_equal(wid, win_getid())
1040 call assert_fails('close', 'E444:')
1041 call assert_fails('%close', 'E16:')
Bram Moolenaare20b9ec2020-02-03 21:40:04 +01001042endfunc
1043
Bram Moolenaar9f6277b2020-02-11 22:04:02 +01001044" Test for errors with :wincmd
1045func Test_wincmd_errors()
1046 call assert_fails('wincmd g', 'E474:')
1047 call assert_fails('wincmd ab', 'E474:')
1048endfunc
1049
1050" Test for errors with :winpos
1051func Test_winpos_errors()
1052 if !has("gui_running") && !has('win32')
1053 call assert_fails('winpos', 'E188:')
1054 endif
1055 call assert_fails('winpos 10', 'E466:')
1056endfunc
1057
Bram Moolenaar406cd902020-02-18 21:54:41 +01001058" Test for +cmd in a :split command
1059func Test_split_cmd()
1060 split +set\ readonly
1061 call assert_equal(1, &readonly)
1062 call assert_equal(2, winnr('$'))
1063 close
1064endfunc
1065
Bram Moolenaar5080b0a2020-03-22 21:23:47 +01001066" Create maximum number of horizontally or vertically split windows and then
1067" run commands that create a new horizontally/vertically split window
1068func Run_noroom_for_newwindow_test(dir_arg)
1069 let dir = (a:dir_arg == 'v') ? 'vert ' : ''
1070
1071 " Open as many windows as possible
Bram Moolenaarab505b12020-03-23 19:28:44 +01001072 while v:true
Bram Moolenaar5080b0a2020-03-22 21:23:47 +01001073 try
1074 exe dir . 'new'
1075 catch /E36:/
1076 break
1077 endtry
Bram Moolenaarab505b12020-03-23 19:28:44 +01001078 endwhile
Bram Moolenaar5080b0a2020-03-22 21:23:47 +01001079
Bram Moolenaardb4c9472022-10-15 22:06:06 +01001080 call writefile(['first', 'second', 'third'], 'Xnorfile1', 'D')
1081 call writefile([], 'Xnorfile2', 'D')
1082 call writefile([], 'Xnorfile3', 'D')
Bram Moolenaar5080b0a2020-03-22 21:23:47 +01001083
1084 " Argument list related commands
Bram Moolenaarb18b4962022-09-02 21:55:50 +01001085 args Xnorfile1 Xnorfile2 Xnorfile3
Bram Moolenaar5080b0a2020-03-22 21:23:47 +01001086 next
1087 for cmd in ['sargument 2', 'snext', 'sprevious', 'sNext', 'srewind',
1088 \ 'sfirst', 'slast']
1089 call assert_fails(dir .. cmd, 'E36:')
1090 endfor
1091 %argdelete
1092
1093 " Buffer related commands
1094 set modified
1095 hide enew
Bram Moolenaarb18b4962022-09-02 21:55:50 +01001096 for cmd in ['sbuffer Xnorfile1', 'sbnext', 'sbprevious', 'sbNext', 'sbrewind',
Bram Moolenaar5080b0a2020-03-22 21:23:47 +01001097 \ 'sbfirst', 'sblast', 'sball', 'sbmodified', 'sunhide']
1098 call assert_fails(dir .. cmd, 'E36:')
1099 endfor
1100
1101 " Window related commands
Bram Moolenaarb18b4962022-09-02 21:55:50 +01001102 for cmd in ['split', 'split Xnorfile2', 'new', 'new Xnorfile3', 'sview Xnorfile1',
Bram Moolenaar5080b0a2020-03-22 21:23:47 +01001103 \ 'sfind runtest.vim']
1104 call assert_fails(dir .. cmd, 'E36:')
1105 endfor
1106
1107 " Help
1108 call assert_fails(dir .. 'help', 'E36:')
1109 call assert_fails(dir .. 'helpgrep window', 'E36:')
1110
1111 " Command-line window
1112 if a:dir_arg == 'h'
1113 " Cmd-line window is always a horizontally split window
1114 call assert_beeps('call feedkeys("q:\<CR>", "xt")')
1115 endif
1116
1117 " Quickfix and location list window
1118 if has('quickfix')
1119 cexpr ''
1120 call assert_fails(dir .. 'copen', 'E36:')
1121 lexpr ''
1122 call assert_fails(dir .. 'lopen', 'E36:')
1123
1124 " Preview window
Bram Moolenaarb18b4962022-09-02 21:55:50 +01001125 call assert_fails(dir .. 'pedit Xnorfile2', 'E36:')
Bram Moolenaar5080b0a2020-03-22 21:23:47 +01001126 call setline(1, 'abc')
1127 call assert_fails(dir .. 'psearch abc', 'E36:')
1128 endif
1129
1130 " Window commands (CTRL-W ^ and CTRL-W f)
1131 if a:dir_arg == 'h'
1132 call assert_fails('call feedkeys("\<C-W>^", "xt")', 'E36:')
Bram Moolenaarb18b4962022-09-02 21:55:50 +01001133 call setline(1, 'Xnorfile1')
Bram Moolenaar5080b0a2020-03-22 21:23:47 +01001134 call assert_fails('call feedkeys("gg\<C-W>f", "xt")', 'E36:')
1135 endif
1136 enew!
1137
1138 " Tag commands (:stag, :stselect and :stjump)
1139 call writefile(["!_TAG_FILE_ENCODING\tutf-8\t//",
Bram Moolenaarb18b4962022-09-02 21:55:50 +01001140 \ "second\tXnorfile1\t2",
1141 \ "third\tXnorfile1\t3",],
Bram Moolenaar5080b0a2020-03-22 21:23:47 +01001142 \ 'Xtags')
1143 set tags=Xtags
1144 call assert_fails(dir .. 'stag second', 'E36:')
1145 call assert_fails('call feedkeys(":" .. dir .. "stselect second\n1\n", "xt")', 'E36:')
1146 call assert_fails(dir .. 'stjump second', 'E36:')
1147 call assert_fails(dir .. 'ptag second', 'E36:')
1148 set tags&
1149 call delete('Xtags')
1150
1151 " :isplit and :dsplit
1152 call setline(1, ['#define FOO 1', 'FOO'])
1153 normal 2G
1154 call assert_fails(dir .. 'isplit FOO', 'E36:')
1155 call assert_fails(dir .. 'dsplit FOO', 'E36:')
1156
1157 " terminal
1158 if has('terminal')
1159 call assert_fails(dir .. 'terminal', 'E36:')
1160 endif
1161
1162 %bwipe!
Bram Moolenaar5080b0a2020-03-22 21:23:47 +01001163 only
1164endfunc
1165
1166func Test_split_cmds_with_no_room()
1167 call Run_noroom_for_newwindow_test('h')
1168 call Run_noroom_for_newwindow_test('v')
1169endfunc
1170
Bram Moolenaarad48e6c2020-04-21 22:19:45 +02001171" Test for various wincmd failures
1172func Test_wincmd_fails()
1173 only!
1174 call assert_beeps("normal \<C-W>w")
1175 call assert_beeps("normal \<C-W>p")
1176 call assert_beeps("normal \<C-W>gk")
1177 call assert_beeps("normal \<C-W>r")
1178 call assert_beeps("normal \<C-W>K")
1179 call assert_beeps("normal \<C-W>H")
1180 call assert_beeps("normal \<C-W>2gt")
1181endfunc
1182
Bram Moolenaarfe6dce82020-09-04 14:41:21 +02001183func Test_window_resize()
1184 " Vertical :resize (absolute, relative, min and max size).
1185 vsplit
1186 vert resize 8
1187 call assert_equal(8, winwidth(0))
1188 vert resize +2
1189 call assert_equal(10, winwidth(0))
1190 vert resize -2
1191 call assert_equal(8, winwidth(0))
1192 vert resize
1193 call assert_equal(&columns - 2, winwidth(0))
1194 vert resize 0
1195 call assert_equal(1, winwidth(0))
1196 vert resize 99999
1197 call assert_equal(&columns - 2, winwidth(0))
1198
1199 %bwipe!
1200
1201 " Horizontal :resize (with absolute, relative size, min and max size).
1202 split
1203 resize 8
1204 call assert_equal(8, winheight(0))
1205 resize +2
1206 call assert_equal(10, winheight(0))
1207 resize -2
1208 call assert_equal(8, winheight(0))
1209 resize
1210 call assert_equal(&lines - 4, winheight(0))
1211 resize 0
1212 call assert_equal(1, winheight(0))
1213 resize 99999
1214 call assert_equal(&lines - 4, winheight(0))
1215
1216 " :resize with explicit window number.
1217 let other_winnr = winnr('j')
1218 exe other_winnr .. 'resize 10'
1219 call assert_equal(10, winheight(other_winnr))
1220 call assert_equal(&lines - 10 - 3, winheight(0))
Bram Moolenaar9668cc52020-10-17 17:39:55 +02001221 exe other_winnr .. 'resize +1'
1222 exe other_winnr .. 'resize +1'
1223 call assert_equal(12, winheight(other_winnr))
1224 call assert_equal(&lines - 10 - 3 -2, winheight(0))
Bram Moolenaar89015a62020-12-29 12:46:51 +01001225 close
1226
1227 vsplit
1228 wincmd l
1229 let other_winnr = winnr('h')
1230 call assert_notequal(winnr(), other_winnr)
Bram Moolenaar5efe0e52021-01-01 14:31:34 +01001231 exe 'vert ' .. other_winnr .. 'resize -' .. &columns
Bram Moolenaar89015a62020-12-29 12:46:51 +01001232 call assert_equal(0, winwidth(other_winnr))
Bram Moolenaarfe6dce82020-09-04 14:41:21 +02001233
1234 %bwipe!
1235endfunc
1236
Bram Moolenaar5d3c9f82020-06-26 20:41:39 +02001237" Test for adjusting the window width when a window is closed with some
1238" windows using 'winfixwidth'
1239func Test_window_width_adjust()
1240 only
1241 " Three vertical windows. Windows 1 and 2 have 'winfixwidth' set and close
1242 " window 2.
1243 wincmd v
1244 vert resize 10
1245 set winfixwidth
1246 wincmd v
1247 set winfixwidth
1248 wincmd c
1249 call assert_inrange(10, 12, winwidth(1))
1250 " Three vertical windows. Windows 2 and 3 have 'winfixwidth' set and close
1251 " window 3.
1252 only
1253 set winfixwidth
1254 wincmd v
1255 vert resize 10
1256 set winfixwidth
1257 wincmd v
1258 set nowinfixwidth
1259 wincmd b
1260 wincmd c
1261 call assert_inrange(10, 12, winwidth(2))
1262
1263 new | only
1264endfunc
1265
1266" Test for jumping to a vertical/horizontal neighbor window based on the
1267" current cursor position
dundargocc57b5bc2022-11-02 13:30:51 +00001268func Test_window_goto_neighbor()
Bram Moolenaar5d3c9f82020-06-26 20:41:39 +02001269 %bw!
1270
1271 " Vertical window movement
1272
1273 " create the following window layout:
1274 " +--+--+
1275 " |w1|w3|
1276 " +--+ |
1277 " |w2| |
1278 " +--+--+
1279 " |w4 |
1280 " +-----+
1281 new
1282 vsplit
1283 split
1284 " vertically jump from w4
1285 wincmd b
1286 call setline(1, repeat(' ', &columns))
1287 call cursor(1, 1)
1288 wincmd k
1289 call assert_equal(2, winnr())
1290 wincmd b
1291 call cursor(1, &columns)
1292 redraw!
1293 wincmd k
1294 call assert_equal(3, winnr())
1295 %bw!
1296
1297 " create the following window layout:
1298 " +--+--+--+
1299 " |w1|w2|w3|
1300 " +--+--+--+
1301 " |w4 |
1302 " +--------+
1303 new
1304 vsplit
1305 vsplit
1306 wincmd b
1307 call setline(1, repeat(' ', &columns))
1308 call cursor(1, 1)
1309 wincmd k
1310 call assert_equal(1, winnr())
1311 wincmd b
1312 call cursor(1, &columns / 2)
1313 redraw!
1314 wincmd k
1315 call assert_equal(2, winnr())
1316 wincmd b
1317 call cursor(1, &columns)
1318 redraw!
1319 wincmd k
1320 call assert_equal(3, winnr())
1321 %bw!
1322
1323 " Horizontal window movement
1324
1325 " create the following window layout:
1326 " +--+--+--+
1327 " |w1|w2|w4|
1328 " +--+--+ |
1329 " |w3 | |
1330 " +-----+--+
1331 vsplit
1332 split
1333 vsplit
1334 4wincmd l
1335 call setline(1, repeat([' '], &lines))
1336 call cursor(1, 1)
1337 redraw!
1338 wincmd h
1339 call assert_equal(2, winnr())
1340 4wincmd l
1341 call cursor(&lines, 1)
1342 redraw!
1343 wincmd h
1344 call assert_equal(3, winnr())
1345 %bw!
1346
1347 " create the following window layout:
1348 " +--+--+
1349 " |w1|w4|
1350 " +--+ +
1351 " |w2| |
1352 " +--+ +
1353 " |w3| |
1354 " +--+--+
1355 vsplit
1356 split
1357 split
1358 wincmd l
1359 call setline(1, repeat([' '], &lines))
1360 call cursor(1, 1)
1361 redraw!
1362 wincmd h
1363 call assert_equal(1, winnr())
1364 wincmd l
1365 call cursor(&lines / 2, 1)
1366 redraw!
1367 wincmd h
1368 call assert_equal(2, winnr())
1369 wincmd l
1370 call cursor(&lines, 1)
1371 redraw!
1372 wincmd h
1373 call assert_equal(3, winnr())
1374 %bw!
1375endfunc
1376
1377" Test for an autocmd closing the destination window when jumping from one
1378" window to another.
1379func Test_close_dest_window()
1380 split
Bram Moolenaarb18b4962022-09-02 21:55:50 +01001381 edit Xdstfile
Bram Moolenaar5d3c9f82020-06-26 20:41:39 +02001382
1383 " Test for BufLeave
1384 augroup T1
1385 au!
Bram Moolenaarb18b4962022-09-02 21:55:50 +01001386 au BufLeave Xdstfile $wincmd c
Bram Moolenaar5d3c9f82020-06-26 20:41:39 +02001387 augroup END
1388 wincmd b
1389 call assert_equal(1, winnr('$'))
Bram Moolenaarb18b4962022-09-02 21:55:50 +01001390 call assert_equal('Xdstfile', @%)
Bram Moolenaar5d3c9f82020-06-26 20:41:39 +02001391 augroup T1
1392 au!
1393 augroup END
1394
1395 " Test for WinLeave
1396 new
1397 wincmd p
1398 augroup T1
1399 au!
1400 au WinLeave * 1wincmd c
1401 augroup END
1402 wincmd t
1403 call assert_equal(1, winnr('$'))
Bram Moolenaarb18b4962022-09-02 21:55:50 +01001404 call assert_equal('Xdstfile', @%)
Bram Moolenaar5d3c9f82020-06-26 20:41:39 +02001405 augroup T1
1406 au!
1407 augroup END
1408 augroup! T1
1409 %bw!
1410endfunc
1411
Bram Moolenaard0fb9072021-12-09 11:57:22 +00001412func Test_window_minimal_size()
1413 set winminwidth=0 winminheight=0
1414
1415 " check size is fixed vertically
1416 new
1417 call win_execute(win_getid(2), 'wincmd _')
1418 call assert_equal(0, winheight(0))
1419 call feedkeys('0', 'tx')
1420 call assert_equal(1, winheight(0))
1421 bwipe!
1422
1423 " check size is fixed horizontally
1424 vert new
1425 call win_execute(win_getid(2), 'wincmd |')
1426 call assert_equal(0, winwidth(0))
1427 call feedkeys('0', 'tx')
1428 call assert_equal(1, winwidth(0))
1429 bwipe!
1430
1431 if has('timers')
1432 " check size is fixed in Insert mode
ichizokfa9a8e02021-12-12 16:42:09 +00001433 func s:CheckSize(timer) abort
1434 call win_execute(win_getid(2), 'wincmd _')
1435 call assert_equal(0, winheight(0))
1436 call feedkeys(" \<Esc>", 't!')
1437 endfunc
Bram Moolenaard0fb9072021-12-09 11:57:22 +00001438 new
ichizokfa9a8e02021-12-12 16:42:09 +00001439 call timer_start(100, function('s:CheckSize'))
Bram Moolenaard0fb9072021-12-09 11:57:22 +00001440 call feedkeys('a', 'tx!')
1441 call assert_equal(1, winheight(0))
1442 bwipe!
ichizokfa9a8e02021-12-12 16:42:09 +00001443 delfunc s:CheckSize
Bram Moolenaard0fb9072021-12-09 11:57:22 +00001444 endif
1445
1446 set winminwidth& winminheight&
1447endfunc
1448
Daniel Steinbergee630312022-01-10 13:36:34 +00001449func Test_win_move_separator()
1450 edit a
1451 leftabove vsplit b
1452 let w = winwidth(0)
1453 " check win_move_separator from left window on left window
1454 call assert_equal(1, winnr())
1455 for offset in range(5)
1456 call assert_true(win_move_separator(0, offset))
1457 call assert_equal(w + offset, winwidth(0))
1458 call assert_true(0->win_move_separator(-offset))
1459 call assert_equal(w, winwidth(0))
1460 endfor
1461 " check win_move_separator from right window on left window number
1462 wincmd l
1463 call assert_notequal(1, winnr())
1464 for offset in range(5)
1465 call assert_true(1->win_move_separator(offset))
1466 call assert_equal(w + offset, winwidth(1))
1467 call assert_true(win_move_separator(1, -offset))
1468 call assert_equal(w, winwidth(1))
1469 endfor
1470 " check win_move_separator from right window on left window ID
1471 let id = win_getid(1)
1472 for offset in range(5)
1473 call assert_true(win_move_separator(id, offset))
1474 call assert_equal(w + offset, winwidth(id))
1475 call assert_true(id->win_move_separator(-offset))
1476 call assert_equal(w, winwidth(id))
1477 endfor
zeertzjqa0c4e2f2022-01-29 10:59:53 +00001478 " check win_move_separator from right window on right window is no-op
1479 let w0 = winwidth(0)
1480 call assert_true(win_move_separator(0, 1))
1481 call assert_equal(w0, winwidth(0))
1482 call assert_true(win_move_separator(0, -1))
1483 call assert_equal(w0, winwidth(0))
zeertzjq873f41a2022-11-01 11:44:43 +00001484
Daniel Steinbergee630312022-01-10 13:36:34 +00001485 " check that win_move_separator doesn't error with offsets beyond moving
1486 " possibility
1487 call assert_true(win_move_separator(id, 5000))
1488 call assert_true(winwidth(id) > w)
1489 call assert_true(win_move_separator(id, -5000))
1490 call assert_true(winwidth(id) < w)
zeertzjq873f41a2022-11-01 11:44:43 +00001491
Daniel Steinbergee630312022-01-10 13:36:34 +00001492 " check that win_move_separator returns false for an invalid window
1493 wincmd =
1494 let w = winwidth(0)
1495 call assert_false(win_move_separator(-1, 1))
1496 call assert_equal(w, winwidth(0))
zeertzjq873f41a2022-11-01 11:44:43 +00001497
Daniel Steinbergee630312022-01-10 13:36:34 +00001498 " check that win_move_separator returns false for a popup window
1499 let id = popup_create(['hello', 'world'], {})
1500 let w = winwidth(id)
1501 call assert_false(win_move_separator(id, 1))
1502 call assert_equal(w, winwidth(id))
1503 call popup_close(id)
zeertzjq873f41a2022-11-01 11:44:43 +00001504
1505 " check that using another tabpage fails without crash
1506 let id = win_getid()
1507 tabnew
1508 call assert_fails('call win_move_separator(id, -1)', 'E1308:')
1509 tabclose
1510
Daniel Steinbergee630312022-01-10 13:36:34 +00001511 %bwipe!
1512endfunc
1513
1514func Test_win_move_statusline()
1515 edit a
1516 leftabove split b
1517 let h = winheight(0)
1518 " check win_move_statusline from top window on top window
1519 call assert_equal(1, winnr())
1520 for offset in range(5)
1521 call assert_true(win_move_statusline(0, offset))
1522 call assert_equal(h + offset, winheight(0))
1523 call assert_true(0->win_move_statusline(-offset))
1524 call assert_equal(h, winheight(0))
1525 endfor
1526 " check win_move_statusline from bottom window on top window number
1527 wincmd j
1528 call assert_notequal(1, winnr())
1529 for offset in range(5)
1530 call assert_true(1->win_move_statusline(offset))
1531 call assert_equal(h + offset, winheight(1))
1532 call assert_true(win_move_statusline(1, -offset))
1533 call assert_equal(h, winheight(1))
1534 endfor
zeertzjqa0c4e2f2022-01-29 10:59:53 +00001535 " check win_move_statusline from bottom window on bottom window
1536 let h0 = winheight(0)
1537 for offset in range(5)
1538 call assert_true(0->win_move_statusline(-offset))
1539 call assert_equal(h0 - offset, winheight(0))
1540 call assert_equal(1 + offset, &cmdheight)
1541 call assert_true(win_move_statusline(0, offset))
1542 call assert_equal(h0, winheight(0))
1543 call assert_equal(1, &cmdheight)
1544 endfor
1545 call assert_true(win_move_statusline(0, 1))
Bram Moolenaara2a89732022-08-31 14:46:18 +01001546 call assert_equal(h0, winheight(0))
1547 call assert_equal(1, &cmdheight)
Daniel Steinbergee630312022-01-10 13:36:34 +00001548 " check win_move_statusline from bottom window on top window ID
1549 let id = win_getid(1)
1550 for offset in range(5)
1551 call assert_true(win_move_statusline(id, offset))
1552 call assert_equal(h + offset, winheight(id))
1553 call assert_true(id->win_move_statusline(-offset))
1554 call assert_equal(h, winheight(id))
1555 endfor
Bram Moolenaar86e67172022-10-31 12:24:12 +00001556
Daniel Steinbergee630312022-01-10 13:36:34 +00001557 " check that win_move_statusline doesn't error with offsets beyond moving
1558 " possibility
1559 call assert_true(win_move_statusline(id, 5000))
1560 call assert_true(winheight(id) > h)
1561 call assert_true(win_move_statusline(id, -5000))
1562 call assert_true(winheight(id) < h)
Bram Moolenaar86e67172022-10-31 12:24:12 +00001563
Daniel Steinbergee630312022-01-10 13:36:34 +00001564 " check that win_move_statusline returns false for an invalid window
1565 wincmd =
1566 let h = winheight(0)
1567 call assert_false(win_move_statusline(-1, 1))
1568 call assert_equal(h, winheight(0))
Bram Moolenaar86e67172022-10-31 12:24:12 +00001569
Daniel Steinbergee630312022-01-10 13:36:34 +00001570 " check that win_move_statusline returns false for a popup window
1571 let id = popup_create(['hello', 'world'], {})
1572 let h = winheight(id)
1573 call assert_false(win_move_statusline(id, 1))
1574 call assert_equal(h, winheight(id))
1575 call popup_close(id)
Bram Moolenaar86e67172022-10-31 12:24:12 +00001576
1577 " check that using another tabpage fails without crash
1578 let id = win_getid()
1579 tabnew
1580 call assert_fails('call win_move_statusline(id, -1)', 'E1308:')
1581 tabclose
1582
Daniel Steinbergee630312022-01-10 13:36:34 +00001583 %bwipe!
1584endfunc
1585
Yegappan Lakshmanan72bb47e2022-04-03 11:22:38 +01001586" Test for window allocation failure
1587func Test_window_alloc_failure()
1588 %bw!
1589
1590 " test for creating a new window above current window
1591 call test_alloc_fail(GetAllocId('newwin_wvars'), 0, 0)
1592 call assert_fails('above new', 'E342:')
1593 call assert_equal(1, winnr('$'))
1594
1595 " test for creating a new window below current window
1596 call test_alloc_fail(GetAllocId('newwin_wvars'), 0, 0)
1597 call assert_fails('below new', 'E342:')
1598 call assert_equal(1, winnr('$'))
1599
1600 " test for popup window creation failure
1601 call test_alloc_fail(GetAllocId('newwin_wvars'), 0, 0)
1602 call assert_fails('call popup_create("Hello", {})', 'E342:')
1603 call assert_equal([], popup_list())
1604
1605 call test_alloc_fail(GetAllocId('newwin_wvars'), 0, 0)
1606 call assert_fails('split', 'E342:')
1607 call assert_equal(1, winnr('$'))
1608
Bram Moolenaarb18b4962022-09-02 21:55:50 +01001609 edit Xwaffile1
1610 edit Xwaffile2
Yegappan Lakshmanan72bb47e2022-04-03 11:22:38 +01001611 call test_alloc_fail(GetAllocId('newwin_wvars'), 0, 0)
Bram Moolenaarb18b4962022-09-02 21:55:50 +01001612 call assert_fails('sb Xwaffile1', 'E342:')
Yegappan Lakshmanan72bb47e2022-04-03 11:22:38 +01001613 call assert_equal(1, winnr('$'))
Bram Moolenaarb18b4962022-09-02 21:55:50 +01001614 call assert_equal('Xwaffile2', @%)
Yegappan Lakshmanan72bb47e2022-04-03 11:22:38 +01001615 %bw!
1616
1617 " FIXME: The following test crashes Vim
1618 " test for new tabpage creation failure
1619 " call test_alloc_fail(GetAllocId('newwin_wvars'), 0, 0)
1620 " call assert_fails('tabnew', 'E342:')
1621 " call assert_equal(1, tabpagenr('$'))
1622 " call assert_equal(1, winnr('$'))
1623
1624 " This test messes up the internal Vim window/frame information. So the
1625 " Test_window_cmd_cmdwin_with_vsp() test fails after running this test.
1626 " Open a new tab and close everything else to fix this issue.
1627 tabnew
1628 tabonly
1629endfunc
Bram Moolenaard0fb9072021-12-09 11:57:22 +00001630
Luuk van Baalfd7e60a2022-09-07 14:42:49 +01001631func Test_win_equal_last_status()
1632 let save_lines = &lines
1633 set lines=20
1634 set splitbelow
1635 set laststatus=0
1636
1637 split | split | quit
1638 call assert_equal(winheight(1), winheight(2))
1639
1640 let &lines = save_lines
1641 set splitbelow&
1642 set laststatus&
1643endfunc
1644
Luuk van Baal13ece2a2022-10-03 15:28:08 +01001645" Test "screen" and "cursor" values for 'splitkeep' with a sequence of
Luuk van Baalfaf1d412022-09-19 16:45:29 +01001646" split operations for various options: with and without a winbar,
1647" tabline, for each possible value of 'laststatus', 'scrolloff',
Luuk van Baal13ece2a2022-10-03 15:28:08 +01001648" 'equalalways', and with the cursor at the top, middle and bottom.
1649func Test_splitkeep_options()
Luuk van Baal594f9e02022-09-16 12:52:58 +01001650 " disallow window resizing
1651 let save_WS = &t_WS
1652 set t_WS=
1653
Luuk van Baal29ab5242022-09-11 16:59:53 +01001654 let gui = has("gui_running")
Luuk van Baal594f9e02022-09-16 12:52:58 +01001655 inoremap <expr> c "<cmd>copen<bar>wincmd k<CR>"
Luuk van Baal13ece2a2022-10-03 15:28:08 +01001656 for run in range(0, 20)
1657 let &splitkeep = run > 10 ? 'topline' : 'screen'
1658 let &scrolloff = (!(run % 4) ? 0 : run)
1659 let &laststatus = (run % 3)
1660 let &splitbelow = (run % 3)
1661 let &equalalways = (run % 2)
1662 let wsb = (run % 2) && &splitbelow
1663 let tl = (gui ? 0 : ((run % 5) ? 1 : 0))
1664 let pos = !(run % 3) ? 'H' : ((run % 2) ? 'M' : 'L')
1665 tabnew | tabonly! | redraw
Luuk van Baal594f9e02022-09-16 12:52:58 +01001666 execute (run % 5) ? 'tabnew' : ''
1667 execute (run % 2) ? 'nnoremenu 1.10 WinBar.Test :echo' : ''
Luuk van Baal594f9e02022-09-16 12:52:58 +01001668 call setline(1, range(1, 256))
1669 " No scroll for restore_snapshot
1670 norm G
1671 try
1672 copen | close | colder
1673 catch /E380/
1674 endtry
1675 call assert_equal(257 - winheight(0), line("w0"))
Luuk van Baal29ab5242022-09-11 16:59:53 +01001676
Luuk van Baal594f9e02022-09-16 12:52:58 +01001677 " No scroll for firstwin horizontal split
1678 execute 'norm gg' . pos
1679 split | redraw | wincmd k
1680 call assert_equal(1, line("w0"))
1681 call assert_equal(&scroll, winheight(0) / 2)
1682 wincmd j
Luuk van Baal13ece2a2022-10-03 15:28:08 +01001683 call assert_equal(&spk == 'topline' ? 1 : win_screenpos(0)[0] - tl - wsb, line("w0"))
Luuk van Baal29ab5242022-09-11 16:59:53 +01001684
Luuk van Baal594f9e02022-09-16 12:52:58 +01001685 " No scroll when resizing windows
Luuk van Baal13ece2a2022-10-03 15:28:08 +01001686 wincmd k | resize +2 | redraw
Luuk van Baal594f9e02022-09-16 12:52:58 +01001687 call assert_equal(1, line("w0"))
1688 wincmd j
Luuk van Baal13ece2a2022-10-03 15:28:08 +01001689 call assert_equal(&spk == 'topline' ? 1 : win_screenpos(0)[0] - tl - wsb, line("w0"))
Luuk van Baal29ab5242022-09-11 16:59:53 +01001690
Luuk van Baal594f9e02022-09-16 12:52:58 +01001691 " No scroll when dragging statusline
1692 call win_move_statusline(1, -3)
Luuk van Baal13ece2a2022-10-03 15:28:08 +01001693 call assert_equal(&spk == 'topline' ? 1 : win_screenpos(0)[0] - tl - wsb, line("w0"))
Luuk van Baal594f9e02022-09-16 12:52:58 +01001694 wincmd k
1695 call assert_equal(1, line("w0"))
Luuk van Baal29ab5242022-09-11 16:59:53 +01001696
Luuk van Baal594f9e02022-09-16 12:52:58 +01001697 " No scroll when changing shellsize
1698 set lines+=2
1699 call assert_equal(1, line("w0"))
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 Baal594f9e02022-09-16 12:52:58 +01001702 set lines-=2
Luuk van Baal13ece2a2022-10-03 15:28:08 +01001703 call assert_equal(&spk == 'topline' ? 1 : win_screenpos(0)[0] - tl - wsb, line("w0"))
Luuk van Baal594f9e02022-09-16 12:52:58 +01001704 wincmd k
1705 call assert_equal(1, line("w0"))
Luuk van Baal29ab5242022-09-11 16:59:53 +01001706
Luuk van Baal594f9e02022-09-16 12:52:58 +01001707 " No scroll when equalizing windows
1708 wincmd =
1709 call assert_equal(1, line("w0"))
1710 wincmd j
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 in windows split multiple times
1716 vsplit | split | 4wincmd w
Luuk van Baal13ece2a2022-10-03 15:28:08 +01001717 call assert_equal(&spk == 'topline' ? 1 : win_screenpos(0)[0] - tl - wsb, line("w0"))
Luuk van Baal594f9e02022-09-16 12:52:58 +01001718 1wincmd w | quit | wincmd l | split
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 wincmd j
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 Baal29ab5242022-09-11 16:59:53 +01001722
Luuk van Baal594f9e02022-09-16 12:52:58 +01001723 " No scroll in small window
1724 2wincmd w | only | 5split | wincmd k
1725 call assert_equal(1, line("w0"))
1726 wincmd j
Luuk van Baal13ece2a2022-10-03 15:28:08 +01001727 call assert_equal(&spk == 'topline' ? 1 : win_screenpos(0)[0] - tl - wsb, line("w0"))
Luuk van Baal29ab5242022-09-11 16:59:53 +01001728
Luuk van Baal594f9e02022-09-16 12:52:58 +01001729 " No scroll for vertical split
1730 quit | vsplit | wincmd l
1731 call assert_equal(1, line("w0"))
1732 wincmd h
1733 call assert_equal(1, line("w0"))
Luuk van Baal29ab5242022-09-11 16:59:53 +01001734
Luuk van Baal594f9e02022-09-16 12:52:58 +01001735 " No scroll in windows split and quit multiple times
Luuk van Baal13ece2a2022-10-03 15:28:08 +01001736 quit | redraw | split | split | quit | redraw
1737 call assert_equal(&spk == 'topline' ? 1 : win_screenpos(0)[0] - tl - wsb, line("w0"))
Luuk van Baal29ab5242022-09-11 16:59:53 +01001738
Luuk van Baal594f9e02022-09-16 12:52:58 +01001739 " No scroll for new buffer
1740 1wincmd w | only | copen | wincmd k
1741 call assert_equal(1, line("w0"))
1742 only
1743 call assert_equal(1, line("w0"))
1744 above copen | wincmd j
Luuk van Baal13ece2a2022-10-03 15:28:08 +01001745 call assert_equal(&spk == 'topline' ? 1 : win_screenpos(0)[0] - tl, line("w0"))
Luuk van Baal29ab5242022-09-11 16:59:53 +01001746
Luuk van Baal594f9e02022-09-16 12:52:58 +01001747 " No scroll when opening cmdwin, and no cursor move when closing cmdwin.
1748 only | norm ggL
1749 let curpos = getcurpos()
1750 norm q:
1751 call assert_equal(1, line("w0"))
1752 call assert_equal(curpos, getcurpos())
Luuk van Baal29ab5242022-09-11 16:59:53 +01001753
Luuk van Baal594f9e02022-09-16 12:52:58 +01001754 " Scroll when cursor becomes invalid in insert mode
1755 norm Lic
1756 call assert_equal(curpos, getcurpos())
Luuk van Baal29ab5242022-09-11 16:59:53 +01001757
Luuk van Baal594f9e02022-09-16 12:52:58 +01001758 " No scroll when topline not equal to 1
1759 only | execute "norm gg5\<C-e>" | split | wincmd k
1760 call assert_equal(6, line("w0"))
1761 wincmd j
Luuk van Baal13ece2a2022-10-03 15:28:08 +01001762 call assert_equal(&spk == 'topline' ? 6 : 5 + win_screenpos(0)[0] - tl - wsb, line("w0"))
Luuk van Baal29ab5242022-09-11 16:59:53 +01001763 endfor
1764
1765 tabnew | tabonly! | %bwipeout!
1766 iunmap c
Luuk van Baal29ab5242022-09-11 16:59:53 +01001767 set scrolloff&
1768 set splitbelow&
1769 set laststatus&
1770 set equalalways&
Luuk van Baal13ece2a2022-10-03 15:28:08 +01001771 set splitkeep&
Luuk van Baal594f9e02022-09-16 12:52:58 +01001772 let &t_WS = save_WS
Luuk van Baal29ab5242022-09-11 16:59:53 +01001773endfunc
Luuk van Baalfd7e60a2022-09-07 14:42:49 +01001774
Luuk van Baal13ece2a2022-10-03 15:28:08 +01001775function Test_splitkeep_cmdwin_cursor_position()
1776 set splitkeep=screen
mityu12167d82022-09-15 17:44:07 +01001777 call setline(1, range(&lines))
1778
1779 " No scroll when cursor is at near bottom of window and cusor position
1780 " recompution (done by line('w0') in this test) happens while in cmdwin.
1781 normal! G
1782 let firstline = line('w0')
1783 autocmd CmdwinEnter * ++once autocmd WinEnter * ++once call line('w0')
1784 execute "normal! q:\<C-w>q"
1785 redraw!
1786 call assert_equal(firstline, line('w0'))
1787
1788 " User script can change cursor position successfully while in cmdwin and it
1789 " shouldn't be changed when closing cmdwin.
1790 execute "normal! Gq:\<Cmd>call win_execute(winnr('#')->win_getid(), 'call cursor(1, 1)')\<CR>\<C-w>q"
1791 call assert_equal(1, line('.'))
1792 call assert_equal(1, col('.'))
1793
1794 execute "normal! Gq:\<Cmd>autocmd WinEnter * ++once call cursor(1, 1)\<CR>\<C-w>q"
1795 call assert_equal(1, line('.'))
1796 call assert_equal(1, col('.'))
1797
1798 %bwipeout!
Luuk van Baal13ece2a2022-10-03 15:28:08 +01001799 set splitkeep&
mityu12167d82022-09-15 17:44:07 +01001800endfunction
Luuk van Baald5bc7622022-09-17 16:16:35 +01001801
Luuk van Baal13ece2a2022-10-03 15:28:08 +01001802function Test_splitkeep_misc()
1803 set splitkeep=screen
Luuk van Baalfaf1d412022-09-19 16:45:29 +01001804 set splitbelow
Luuk van Baald5bc7622022-09-17 16:16:35 +01001805
1806 call setline(1, range(1, &lines))
1807 norm Gzz
1808 let top = line('w0')
Luuk van Baalfaf1d412022-09-19 16:45:29 +01001809 " No scroll when aucmd_win is opened
Luuk van Baald5bc7622022-09-17 16:16:35 +01001810 call setbufvar(bufnr("test", 1) , '&buftype', 'nofile')
1811 call assert_equal(top, line('w0'))
Luuk van Baalfaf1d412022-09-19 16:45:29 +01001812 " No scroll when tab is changed/closed
1813 tab help | close
1814 call assert_equal(top, line('w0'))
1815 " No scroll when help is closed and buffer line count < window height
1816 norm ggdG
1817 call setline(1, range(1, &lines - 10))
Luuk van Baald5bc7622022-09-17 16:16:35 +01001818 norm G
1819 let top = line('w0')
1820 help | quit
1821 call assert_equal(top, line('w0'))
Luuk van Baal346823d2022-10-05 18:26:42 +01001822 " No error when resizing window in autocmd and buffer length changed
1823 autocmd FileType qf exe "resize" line('$')
1824 cexpr getline(1, '$')
1825 copen
1826 wincmd p
1827 norm dd
1828 cexpr getline(1, '$')
Luuk van Baald5bc7622022-09-17 16:16:35 +01001829
Luuk van Baalfaf1d412022-09-19 16:45:29 +01001830 %bwipeout!
Luuk van Baald5bc7622022-09-17 16:16:35 +01001831 set splitbelow&
Luuk van Baal13ece2a2022-10-03 15:28:08 +01001832 set splitkeep&
Luuk van Baald5bc7622022-09-17 16:16:35 +01001833endfunc
1834
Luuk van Baal13ece2a2022-10-03 15:28:08 +01001835function Test_splitkeep_callback()
Luuk van Baal20e58562022-09-23 12:57:09 +01001836 CheckScreendump
1837 let lines =<< trim END
Luuk van Baal13ece2a2022-10-03 15:28:08 +01001838 set splitkeep=screen
Luuk van Baal20e58562022-09-23 12:57:09 +01001839 call setline(1, range(&lines))
Luuk van Baal13ece2a2022-10-03 15:28:08 +01001840 function C1(a, b)
Luuk van Baal20e58562022-09-23 12:57:09 +01001841 split | wincmd p
1842 endfunction
Luuk van Baal13ece2a2022-10-03 15:28:08 +01001843 function C2(a, b)
Luuk van Baal20e58562022-09-23 12:57:09 +01001844 close | split
1845 endfunction
Luuk van Baal13ece2a2022-10-03 15:28:08 +01001846 nn j <cmd>call job_start([&sh, &shcf, "true"], { 'exit_cb': 'C1' })<CR>
1847 nn t <cmd>call popup_create(term_start([&sh, &shcf, "true"],
1848 \ { 'hidden': 1, 'exit_cb': 'C2' }), {})<CR>
Luuk van Baal20e58562022-09-23 12:57:09 +01001849 END
Luuk van Baal13ece2a2022-10-03 15:28:08 +01001850 call writefile(lines, 'XTestSplitkeepCallback', 'D')
1851 let buf = RunVimInTerminal('-S XTestSplitkeepCallback', #{rows: 8})
Luuk van Baal20e58562022-09-23 12:57:09 +01001852
1853 call term_sendkeys(buf, "j")
Luuk van Baal13ece2a2022-10-03 15:28:08 +01001854 call VerifyScreenDump(buf, 'Test_splitkeep_callback_1', {})
Luuk van Baal20e58562022-09-23 12:57:09 +01001855
Luuk van Baal13ece2a2022-10-03 15:28:08 +01001856 call term_sendkeys(buf, ":quit\<CR>Ht")
1857 call VerifyScreenDump(buf, 'Test_splitkeep_callback_2', {})
Luuk van Baal20e58562022-09-23 12:57:09 +01001858
1859 call term_sendkeys(buf, ":set sb\<CR>:quit\<CR>Gj")
Luuk van Baal13ece2a2022-10-03 15:28:08 +01001860 call VerifyScreenDump(buf, 'Test_splitkeep_callback_3', {})
Luuk van Baal20e58562022-09-23 12:57:09 +01001861
Luuk van Baal13ece2a2022-10-03 15:28:08 +01001862 call term_sendkeys(buf, ":quit\<CR>Gt")
1863 call VerifyScreenDump(buf, 'Test_splitkeep_callback_4', {})
zeertzjq378e6c02023-01-14 11:46:49 +00001864
1865 call StopVimInTerminal(buf)
Luuk van Baal20e58562022-09-23 12:57:09 +01001866endfunc
1867
Luuk van Baal13ece2a2022-10-03 15:28:08 +01001868function Test_splitkeep_fold()
1869 CheckScreendump
Luuk van Baal7c1cbb62022-09-27 12:31:15 +01001870
Luuk van Baal13ece2a2022-10-03 15:28:08 +01001871 let lines =<< trim END
1872 set splitkeep=screen
1873 set foldmethod=marker
1874 set number
1875 let line = 1
1876 for n in range(1, &lines)
1877 call setline(line, ['int FuncName() {/*{{{*/', 1, 2, 3, 4, 5, '}/*}}}*/',
1878 \ 'after fold'])
1879 let line += 8
1880 endfor
1881 END
1882 call writefile(lines, 'XTestSplitkeepFold', 'D')
1883 let buf = RunVimInTerminal('-S XTestSplitkeepFold', #{rows: 10})
Luuk van Baal7c1cbb62022-09-27 12:31:15 +01001884
1885 call term_sendkeys(buf, "L:wincmd s\<CR>")
Luuk van Baal13ece2a2022-10-03 15:28:08 +01001886 call VerifyScreenDump(buf, 'Test_splitkeep_fold_1', {})
Luuk van Baal7c1cbb62022-09-27 12:31:15 +01001887
1888 call term_sendkeys(buf, ":quit\<CR>")
Luuk van Baal13ece2a2022-10-03 15:28:08 +01001889 call VerifyScreenDump(buf, 'Test_splitkeep_fold_2', {})
Luuk van Baal7c1cbb62022-09-27 12:31:15 +01001890
1891 call term_sendkeys(buf, "H:below split\<CR>")
Luuk van Baal13ece2a2022-10-03 15:28:08 +01001892 call VerifyScreenDump(buf, 'Test_splitkeep_fold_3', {})
Luuk van Baal7c1cbb62022-09-27 12:31:15 +01001893
1894 call term_sendkeys(buf, ":wincmd k\<CR>:quit\<CR>")
Luuk van Baal13ece2a2022-10-03 15:28:08 +01001895 call VerifyScreenDump(buf, 'Test_splitkeep_fold_4', {})
zeertzjq378e6c02023-01-14 11:46:49 +00001896
1897 call StopVimInTerminal(buf)
Luuk van Baal7c1cbb62022-09-27 12:31:15 +01001898endfunction
1899
Luuk van Baal74a694d2022-11-28 16:49:36 +00001900function Test_splitkeep_status()
1901 CheckScreendump
1902
1903 let lines =<< trim END
1904 call setline(1, ['a', 'b', 'c'])
1905 set nomodified
1906 set splitkeep=screen
1907 let win = winnr()
1908 wincmd s
1909 wincmd j
1910 END
1911 call writefile(lines, 'XTestSplitkeepStatus', 'D')
1912 let buf = RunVimInTerminal('-S XTestSplitkeepStatus', #{rows: 10})
1913
1914 call term_sendkeys(buf, ":call win_move_statusline(win, 1)\<CR>")
1915 call VerifyScreenDump(buf, 'Test_splitkeep_status_1', {})
zeertzjq378e6c02023-01-14 11:46:49 +00001916
1917 call StopVimInTerminal(buf)
Luuk van Baal74a694d2022-11-28 16:49:36 +00001918endfunction
1919
Rob Pillingcb94c912022-12-13 12:26:09 +00001920function Test_new_help_window_on_error()
1921 help change.txt
1922 execute "normal! /CTRL-@\<CR>"
1923 silent! execute "normal! \<C-W>]"
1924
1925 let wincount = winnr('$')
1926 help 'mod'
1927
1928 call assert_equal(wincount, winnr('$'))
1929 call assert_equal(expand("<cword>"), "'mod'")
1930endfunction
1931
1932
Bram Moolenaar9e4d8212016-08-18 23:04:48 +02001933" vim: shiftwidth=2 sts=2 expandtab