blob: 6368889df5363da48448dab5c832e78fd1d0b4ec [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 Moolenaar4520d442017-03-19 16:09:46 +0100140
Bram Moolenaar1bbb6192018-11-10 16:02:01 +0100141 " Test for failure when the alternate buffer/file no longer exists.
142 edit Xfoo | %bw
Bram Moolenaare2e40752020-09-04 21:18:46 +0200143 call assert_fails(':wincmd ^', 'E23:')
Bram Moolenaar1bbb6192018-11-10 16:02:01 +0100144
145 " Test for the expected behavior when we have two named buffers.
146 edit Xfoo | edit Xbar
Bram Moolenaar4520d442017-03-19 16:09:46 +0100147 wincmd ^
Bram Moolenaar1bbb6192018-11-10 16:02:01 +0100148 call assert_equal('Xfoo', bufname(winbufnr(1)))
149 call assert_equal('Xbar', bufname(winbufnr(2)))
150 only
Bram Moolenaar4520d442017-03-19 16:09:46 +0100151
Bram Moolenaar1bbb6192018-11-10 16:02:01 +0100152 " Test for the expected behavior when the alternate buffer is not named.
153 enew | let l:nr1 = bufnr('%')
154 edit Xfoo | let l:nr2 = bufnr('%')
155 wincmd ^
156 call assert_equal(l:nr1, winbufnr(1))
157 call assert_equal(l:nr2, winbufnr(2))
158 only
159
Bram Moolenaard42333d2018-11-10 20:28:19 +0100160 " FIXME: this currently fails on AppVeyor, but passes locally
161 if !has('win32')
162 " Test the Normal mode command.
163 call feedkeys("\<C-W>\<C-^>", 'tx')
164 call assert_equal(l:nr2, winbufnr(1))
165 call assert_equal(l:nr1, winbufnr(2))
166 endif
Bram Moolenaar1bbb6192018-11-10 16:02:01 +0100167
168 %bw!
169endfunc
170
171" Test the ":[count]wincmd ^" and "[count]<C-W>^" commands.
172func Test_window_split_edit_bufnr()
173
174 %bwipeout
175 let l:nr = bufnr('%') + 1
Bram Moolenaare2e40752020-09-04 21:18:46 +0200176 call assert_fails(':execute "normal! ' . l:nr . '\<C-W>\<C-^>"', 'E92:')
177 call assert_fails(':' . l:nr . 'wincmd ^', 'E16:')
178 call assert_fails(':0wincmd ^', 'E16:')
Bram Moolenaar1bbb6192018-11-10 16:02:01 +0100179
180 edit Xfoo | edit Xbar | edit Xbaz
181 let l:foo_nr = bufnr('Xfoo')
182 let l:bar_nr = bufnr('Xbar')
183 let l:baz_nr = bufnr('Xbaz')
184
Bram Moolenaar8617b402018-11-10 20:47:48 +0100185 " FIXME: this currently fails on AppVeyor, but passes locally
186 if !has('win32')
187 call feedkeys(l:foo_nr . "\<C-W>\<C-^>", 'tx')
188 call assert_equal('Xfoo', bufname(winbufnr(1)))
189 call assert_equal('Xbaz', bufname(winbufnr(2)))
190 only
Bram Moolenaar1bbb6192018-11-10 16:02:01 +0100191
Bram Moolenaar8617b402018-11-10 20:47:48 +0100192 call feedkeys(l:bar_nr . "\<C-W>\<C-^>", 'tx')
193 call assert_equal('Xbar', bufname(winbufnr(1)))
194 call assert_equal('Xfoo', bufname(winbufnr(2)))
195 only
Bram Moolenaar1bbb6192018-11-10 16:02:01 +0100196
Bram Moolenaar8617b402018-11-10 20:47:48 +0100197 execute l:baz_nr . 'wincmd ^'
198 call assert_equal('Xbaz', bufname(winbufnr(1)))
199 call assert_equal('Xbar', bufname(winbufnr(2)))
200 endif
Bram Moolenaar1bbb6192018-11-10 16:02:01 +0100201
202 %bw!
Bram Moolenaar4520d442017-03-19 16:09:46 +0100203endfunc
204
Bram Moolenaar9b9be002020-03-22 14:41:22 +0100205func Test_window_split_no_room()
206 " N horizontal windows need >= 2*N + 1 lines:
207 " - 1 line + 1 status line in each window
208 " - 1 Ex command line
209 "
210 " 2*N + 1 <= &lines
211 " N <= (lines - 1)/2
212 "
213 " Beyond that number of windows, E36: Not enough room is expected.
214 let hor_win_count = (&lines - 1)/2
215 let hor_split_count = hor_win_count - 1
216 for s in range(1, hor_split_count) | split | endfor
217 call assert_fails('split', 'E36:')
218
219 " N vertical windows need >= 2*(N - 1) + 1 columns:
220 " - 1 column + 1 separator for each window (except last window)
221 " - 1 column for the last window which does not have separator
222 "
223 " 2*(N - 1) + 1 <= &columns
224 " 2*N - 1 <= &columns
225 " N <= (&columns + 1)/2
226 let ver_win_count = (&columns + 1)/2
227 let ver_split_count = ver_win_count - 1
228 for s in range(1, ver_split_count) | vsplit | endfor
229 call assert_fails('vsplit', 'E36:')
230
231 %bw!
232endfunc
233
Bram Moolenaar4520d442017-03-19 16:09:46 +0100234func Test_window_exchange()
235 e Xa
236
237 " Nothing happens with window exchange when there is 1 window
238 wincmd x
239 call assert_equal(1, winnr('$'))
240
241 split Xb
242 split Xc
243
244 call assert_equal('Xc', bufname(winbufnr(1)))
245 call assert_equal('Xb', bufname(winbufnr(2)))
246 call assert_equal('Xa', bufname(winbufnr(3)))
247
248 " Exchange current window 1 with window 3
249 3wincmd x
250 call assert_equal('Xa', bufname(winbufnr(1)))
251 call assert_equal('Xb', bufname(winbufnr(2)))
252 call assert_equal('Xc', bufname(winbufnr(3)))
253
254 " Exchange window with next when at the top window
255 wincmd x
256 call assert_equal('Xb', bufname(winbufnr(1)))
257 call assert_equal('Xa', bufname(winbufnr(2)))
258 call assert_equal('Xc', bufname(winbufnr(3)))
259
260 " Exchange window with next when at the middle window
261 wincmd j
262 wincmd x
263 call assert_equal('Xb', bufname(winbufnr(1)))
264 call assert_equal('Xc', bufname(winbufnr(2)))
265 call assert_equal('Xa', bufname(winbufnr(3)))
266
267 " Exchange window with next when at the bottom window.
268 " When there is no next window, it exchanges with the previous window.
269 wincmd j
270 wincmd x
271 call assert_equal('Xb', bufname(winbufnr(1)))
272 call assert_equal('Xa', bufname(winbufnr(2)))
273 call assert_equal('Xc', bufname(winbufnr(3)))
274
275 bw Xa Xb Xc
276endfunc
277
278func Test_window_rotate()
279 e Xa
280 split Xb
281 split Xc
282 call assert_equal('Xc', bufname(winbufnr(1)))
283 call assert_equal('Xb', bufname(winbufnr(2)))
284 call assert_equal('Xa', bufname(winbufnr(3)))
285
286 " Rotate downwards
287 wincmd r
288 call assert_equal('Xa', bufname(winbufnr(1)))
289 call assert_equal('Xc', bufname(winbufnr(2)))
290 call assert_equal('Xb', bufname(winbufnr(3)))
291
292 2wincmd r
293 call assert_equal('Xc', bufname(winbufnr(1)))
294 call assert_equal('Xb', bufname(winbufnr(2)))
295 call assert_equal('Xa', bufname(winbufnr(3)))
296
297 " Rotate upwards
298 wincmd R
299 call assert_equal('Xb', bufname(winbufnr(1)))
300 call assert_equal('Xa', bufname(winbufnr(2)))
301 call assert_equal('Xc', bufname(winbufnr(3)))
302
303 2wincmd R
304 call assert_equal('Xc', bufname(winbufnr(1)))
305 call assert_equal('Xb', bufname(winbufnr(2)))
306 call assert_equal('Xa', bufname(winbufnr(3)))
307
308 bot vsplit
309 call assert_fails('wincmd R', 'E443:')
310
311 bw Xa Xb Xc
312endfunc
313
314func Test_window_height()
315 e Xa
316 split Xb
317
318 let [wh1, wh2] = [winheight(1), winheight(2)]
319 " Active window (1) should have the same height or 1 more
320 " than the other window.
321 call assert_inrange(wh2, wh2 + 1, wh1)
322
323 wincmd -
324 call assert_equal(wh1 - 1, winheight(1))
325 call assert_equal(wh2 + 1, winheight(2))
326
327 wincmd +
328 call assert_equal(wh1, winheight(1))
Bram Moolenaarf92e58c2019-09-08 21:51:41 +0200329 call assert_equal(wh2, 2->winheight())
Bram Moolenaar4520d442017-03-19 16:09:46 +0100330
331 2wincmd _
332 call assert_equal(2, winheight(1))
333 call assert_equal(wh1 + wh2 - 2, winheight(2))
334
335 wincmd =
336 call assert_equal(wh1, winheight(1))
337 call assert_equal(wh2, winheight(2))
338
339 2wincmd _
340 set winfixheight
341 split Xc
342 let [wh1, wh2, wh3] = [winheight(1), winheight(2), winheight(3)]
343 call assert_equal(2, winheight(2))
344 call assert_inrange(wh3, wh3 + 1, wh1)
345 3wincmd +
346 call assert_equal(2, winheight(2))
347 call assert_equal(wh1 + 3, winheight(1))
348 call assert_equal(wh3 - 3, winheight(3))
349 wincmd =
350 call assert_equal(2, winheight(2))
351 call assert_equal(wh1, winheight(1))
352 call assert_equal(wh3, winheight(3))
353
354 wincmd j
355 set winfixheight&
356
357 wincmd =
358 let [wh1, wh2, wh3] = [winheight(1), winheight(2), winheight(3)]
359 " Current window (2) should have the same height or 1 more
360 " than the other windows.
361 call assert_inrange(wh1, wh1 + 1, wh2)
362 call assert_inrange(wh3, wh3 + 1, wh2)
363
364 bw Xa Xb Xc
365endfunc
366
Bram Moolenaar21c3a802022-08-31 17:49:14 +0100367func Test_wincmd_equal()
368 edit Xone
369 below split Xtwo
370 rightbelow vsplit Xthree
371 call assert_equal('Xone', bufname(winbufnr(1)))
372 call assert_equal('Xtwo', bufname(winbufnr(2)))
373 call assert_equal('Xthree', bufname(winbufnr(3)))
374
375 " Xone and Xtwo should be about the same height
376 let [wh1, wh2] = [winheight(1), winheight(2)]
377 call assert_inrange(wh1 - 1, wh1 + 1, wh2)
378 " Xtwo and Xthree should be about the same width
379 let [ww2, ww3] = [winwidth(2), winwidth(3)]
380 call assert_inrange(ww2 - 1, ww2 + 1, ww3)
381
382 1wincmd w
383 10wincmd _
384 2wincmd w
385 20wincmd |
386 call assert_equal(10, winheight(1))
387 call assert_equal(20, winwidth(2))
388
389 " equalizing horizontally doesn't change the heights
390 hor wincmd =
391 call assert_equal(10, winheight(1))
392 let [ww2, ww3] = [winwidth(2), winwidth(3)]
393 call assert_inrange(ww2 - 1, ww2 + 1, ww3)
394
395 2wincmd w
396 20wincmd |
397 call assert_equal(20, winwidth(2))
398 " equalizing vertically doesn't change the widths
399 vert wincmd =
400 call assert_equal(20, winwidth(2))
401 let [wh1, wh2] = [winheight(1), winheight(2)]
402 call assert_inrange(wh1 - 1, wh1 + 1, wh2)
403
404 bwipe Xone Xtwo Xthree
405endfunc
406
Bram Moolenaar4520d442017-03-19 16:09:46 +0100407func Test_window_width()
408 e Xa
409 vsplit Xb
410
411 let [ww1, ww2] = [winwidth(1), winwidth(2)]
412 " Active window (1) should have the same width or 1 more
413 " than the other window.
414 call assert_inrange(ww2, ww2 + 1, ww1)
415
416 wincmd <
417 call assert_equal(ww1 - 1, winwidth(1))
418 call assert_equal(ww2 + 1, winwidth(2))
419
420 wincmd >
421 call assert_equal(ww1, winwidth(1))
422 call assert_equal(ww2, winwidth(2))
423
424 2wincmd |
425 call assert_equal(2, winwidth(1))
426 call assert_equal(ww1 + ww2 - 2, winwidth(2))
427
428 wincmd =
429 call assert_equal(ww1, winwidth(1))
430 call assert_equal(ww2, winwidth(2))
431
432 2wincmd |
433 set winfixwidth
434 vsplit Xc
435 let [ww1, ww2, ww3] = [winwidth(1), winwidth(2), winwidth(3)]
Bram Moolenaar38e34832017-03-19 20:22:36 +0100436 call assert_equal(2, winwidth(2))
Bram Moolenaar4520d442017-03-19 16:09:46 +0100437 call assert_inrange(ww3, ww3 + 1, ww1)
438 3wincmd >
Bram Moolenaar38e34832017-03-19 20:22:36 +0100439 call assert_equal(2, winwidth(2))
Bram Moolenaar4520d442017-03-19 16:09:46 +0100440 call assert_equal(ww1 + 3, winwidth(1))
441 call assert_equal(ww3 - 3, winwidth(3))
442 wincmd =
Bram Moolenaar38e34832017-03-19 20:22:36 +0100443 call assert_equal(2, winwidth(2))
Bram Moolenaar4520d442017-03-19 16:09:46 +0100444 call assert_equal(ww1, winwidth(1))
445 call assert_equal(ww3, winwidth(3))
446
447 wincmd l
448 set winfixwidth&
449
450 wincmd =
451 let [ww1, ww2, ww3] = [winwidth(1), winwidth(2), winwidth(3)]
452 " Current window (2) should have the same width or 1 more
453 " than the other windows.
454 call assert_inrange(ww1, ww1 + 1, ww2)
455 call assert_inrange(ww3, ww3 + 1, ww2)
456
Yegappan Lakshmanan59585492021-06-12 13:46:41 +0200457 " when the current window width is less than the new 'winwidth', the current
458 " window width should be increased.
459 enew | only
460 split
461 10vnew
462 set winwidth=15
463 call assert_equal(15, winwidth(0))
464
465 %bw!
Bram Moolenaar4520d442017-03-19 16:09:46 +0100466endfunc
467
Bram Moolenaar8eeeba82017-06-25 22:45:39 +0200468func Test_equalalways_on_close()
469 set equalalways
470 vsplit
471 windo split
Bram Moolenaar1bbb6192018-11-10 16:02:01 +0100472 split
Bram Moolenaar8eeeba82017-06-25 22:45:39 +0200473 wincmd J
474 " now we have a frame top-left with two windows, a frame top-right with two
475 " windows and a frame at the bottom, full-width.
476 let height_1 = winheight(1)
477 let height_2 = winheight(2)
478 let height_3 = winheight(3)
479 let height_4 = winheight(4)
480 " closing the bottom window causes all windows to be resized.
481 close
482 call assert_notequal(height_1, winheight(1))
483 call assert_notequal(height_2, winheight(2))
484 call assert_notequal(height_3, winheight(3))
485 call assert_notequal(height_4, winheight(4))
486 call assert_equal(winheight(1), winheight(3))
487 call assert_equal(winheight(2), winheight(4))
488
489 1wincmd w
490 split
491 4wincmd w
492 resize + 5
493 " left column has three windows, equalized heights.
494 " right column has two windows, top one a bit higher
495 let height_1 = winheight(1)
496 let height_2 = winheight(2)
497 let height_4 = winheight(4)
498 let height_5 = winheight(5)
499 3wincmd w
500 " closing window in left column equalizes heights in left column but not in
501 " the right column
502 close
503 call assert_notequal(height_1, winheight(1))
504 call assert_notequal(height_2, winheight(2))
505 call assert_equal(height_4, winheight(3))
506 call assert_equal(height_5, winheight(4))
507
508 only
509 set equalalways&
510endfunc
511
Bram Moolenaar22044dc2017-12-02 15:43:37 +0100512func Test_win_screenpos()
Bram Moolenaar5a4c3082019-12-01 15:23:11 +0100513 CheckFeature quickfix
514
Bram Moolenaar22044dc2017-12-02 15:43:37 +0100515 call assert_equal(1, winnr('$'))
516 split
517 vsplit
518 10wincmd _
519 30wincmd |
520 call assert_equal([1, 1], win_screenpos(1))
521 call assert_equal([1, 32], win_screenpos(2))
522 call assert_equal([12, 1], win_screenpos(3))
523 call assert_equal([0, 0], win_screenpos(4))
Bram Moolenaar99fa7212020-04-26 15:59:55 +0200524 call assert_fails('let l = win_screenpos([])', 'E745:')
Bram Moolenaar22044dc2017-12-02 15:43:37 +0100525 only
526endfunc
527
Bram Moolenaar4520d442017-03-19 16:09:46 +0100528func Test_window_jump_tag()
Bram Moolenaar5a4c3082019-12-01 15:23:11 +0100529 CheckFeature quickfix
530
Bram Moolenaar4520d442017-03-19 16:09:46 +0100531 help
532 /iccf
533 call assert_match('^|iccf|', getline('.'))
534 call assert_equal(2, winnr('$'))
535 2wincmd }
536 call assert_equal(3, winnr('$'))
537 call assert_match('^|iccf|', getline('.'))
538 wincmd k
539 call assert_match('\*iccf\*', getline('.'))
540 call assert_equal(2, winheight(0))
541
542 wincmd z
543 set previewheight=4
544 help
545 /bugs
546 wincmd }
547 wincmd k
548 call assert_match('\*bugs\*', getline('.'))
549 call assert_equal(4, winheight(0))
550 set previewheight&
551
552 %bw!
553endfunc
554
555func Test_window_newtab()
556 e Xa
557
558 call assert_equal(1, tabpagenr('$'))
559 call assert_equal("\nAlready only one window", execute('wincmd T'))
560
561 split Xb
562 split Xc
563
564 wincmd T
565 call assert_equal(2, tabpagenr('$'))
566 call assert_equal(['Xb', 'Xa'], map(tabpagebuflist(1), 'bufname(v:val)'))
Bram Moolenaarce90e362019-09-08 18:58:44 +0200567 call assert_equal(['Xc' ], map(2->tabpagebuflist(), 'bufname(v:val)'))
Bram Moolenaar0e05de42020-03-25 22:23:46 +0100568 call assert_equal(['Xc' ], map(tabpagebuflist(), 'bufname(v:val)'))
Bram Moolenaar4520d442017-03-19 16:09:46 +0100569
570 %bw!
571endfunc
572
Bram Moolenaarf79225e2017-03-18 23:11:04 +0100573func Test_next_split_all()
574 " This was causing an illegal memory access.
575 n x
576 norm axxx
577 split
578 split
579 s/x
580 s/x
581 all
582 bwipe!
583endfunc
584
Bram Moolenaar75373f32017-08-07 22:02:30 +0200585" Tests for adjusting window and contents
586func GetScreenStr(row)
587 let str = ""
588 for c in range(1,3)
589 let str .= nr2char(screenchar(a:row, c))
590 endfor
591 return str
592endfunc
593
594func Test_window_contents()
595 enew! | only | new
596 call setline(1, range(1,256))
597
598 exe "norm! \<C-W>t\<C-W>=1Gzt\<C-W>w\<C-W>+"
599 redraw
Bram Moolenaar72cf47a2018-05-10 18:23:29 +0200600 let s3 = GetScreenStr(1)
Bram Moolenaar75373f32017-08-07 22:02:30 +0200601 wincmd p
602 call assert_equal(1, line("w0"))
603 call assert_equal('1 ', s3)
604
605 exe "norm! \<C-W>t\<C-W>=50Gzt\<C-W>w\<C-W>+"
606 redraw
Bram Moolenaar72cf47a2018-05-10 18:23:29 +0200607 let s3 = GetScreenStr(1)
Bram Moolenaar75373f32017-08-07 22:02:30 +0200608 wincmd p
609 call assert_equal(50, line("w0"))
610 call assert_equal('50 ', s3)
611
612 exe "norm! \<C-W>t\<C-W>=59Gzt\<C-W>w\<C-W>+"
613 redraw
Bram Moolenaar72cf47a2018-05-10 18:23:29 +0200614 let s3 = GetScreenStr(1)
Bram Moolenaar75373f32017-08-07 22:02:30 +0200615 wincmd p
616 call assert_equal(59, line("w0"))
617 call assert_equal('59 ', s3)
618
Bram Moolenaar8b633132020-03-20 18:20:51 +0100619 %d
620 call setline(1, ['one', 'two', 'three'])
621 call assert_equal(1, line('w0'))
622 call assert_equal(3, line('w$'))
623
Bram Moolenaar75373f32017-08-07 22:02:30 +0200624 bwipeout!
625 call test_garbagecollect_now()
626endfunc
627
Bram Moolenaar2efb3232017-12-19 12:27:23 +0100628func Test_window_colon_command()
629 " This was reading invalid memory.
630 exe "norm! v\<C-W>:\<C-U>echo v:version"
631endfunc
632
Bram Moolenaar6f361c92018-01-31 19:06:50 +0100633func Test_access_freed_mem()
Bram Moolenaar1417c762019-07-27 17:31:36 +0200634 call assert_equal(&columns, winwidth(0))
Bram Moolenaar9a046fd2021-01-28 13:47:59 +0100635 " This was accessing freed memory (but with what events?)
636 au BufEnter,BufLeave,WinEnter,WinLeave 0 vs xxx
Bram Moolenaar6f361c92018-01-31 19:06:50 +0100637 arg 0
638 argadd
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +0200639 call assert_fails("all", "E242:")
Bram Moolenaar6f361c92018-01-31 19:06:50 +0100640 au!
641 bwipe xxx
Bram Moolenaar1417c762019-07-27 17:31:36 +0200642 call assert_equal(&columns, winwidth(0))
Bram Moolenaar6f361c92018-01-31 19:06:50 +0100643endfunc
644
Bram Moolenaara27e1dc2019-10-07 22:27:36 +0200645func Test_insert_cleared_on_switch_to_term()
646 CheckFeature terminal
647
648 set showmode
649 terminal
650 wincmd p
651
652 call feedkeys("i\<C-O>", 'ntx')
653 redraw
654
655 " The "-- (insert) --" indicator should be visible.
656 let chars = map(range(1, &columns), 'nr2char(screenchar(&lines, v:val))')
657 let str = trim(join(chars, ''))
658 call assert_equal('-- (insert) --', str)
659
660 call feedkeys("\<C-W>p", 'ntx')
661 redraw
662
663 " The "-- (insert) --" indicator should have been cleared.
664 let chars = map(range(1, &columns), 'nr2char(screenchar(&lines, v:val))')
665 let str = trim(join(chars, ''))
666 call assert_equal('', str)
667
668 set showmode&
669 %bw!
670endfunc
671
Bram Moolenaar5bab5552018-04-13 20:41:29 +0200672func Test_visual_cleared_after_window_split()
673 new | only!
674 let smd_save = &showmode
675 set showmode
676 let ls_save = &laststatus
677 set laststatus=1
678 call setline(1, ['a', 'b', 'c', 'd', ''])
679 norm! G
680 exe "norm! kkvk"
681 redraw
682 exe "norm! \<C-W>v"
683 redraw
684 " check if '-- VISUAL --' disappeared from command line
685 let columns = range(1, &columns)
686 let cmdlinechars = map(columns, 'nr2char(screenchar(&lines, v:val))')
687 let cmdline = join(cmdlinechars, '')
688 let cmdline_ltrim = substitute(cmdline, '^\s*', "", "")
689 let mode_shown = substitute(cmdline_ltrim, '\s*$', "", "")
690 call assert_equal('', mode_shown)
691 let &showmode = smd_save
692 let &laststatus = ls_save
693 bwipe!
694endfunc
695
Bram Moolenaar72cf47a2018-05-10 18:23:29 +0200696func Test_winrestcmd()
697 2split
698 3vsplit
Bram Moolenaara0c8aea2021-03-20 19:55:35 +0100699 let restcmd = winrestcmd()
Bram Moolenaar72cf47a2018-05-10 18:23:29 +0200700 call assert_equal(2, winheight(0))
701 call assert_equal(3, winwidth(0))
702 wincmd =
703 call assert_notequal(2, winheight(0))
704 call assert_notequal(3, winwidth(0))
Bram Moolenaara0c8aea2021-03-20 19:55:35 +0100705 exe restcmd
Bram Moolenaar72cf47a2018-05-10 18:23:29 +0200706 call assert_equal(2, winheight(0))
707 call assert_equal(3, winwidth(0))
708 only
Bram Moolenaara0c8aea2021-03-20 19:55:35 +0100709
710 wincmd v
711 wincmd s
712 wincmd v
713 redraw
714 let restcmd = winrestcmd()
715 wincmd _
716 wincmd |
717 exe restcmd
718 redraw
719 call assert_equal(restcmd, winrestcmd())
720
721 only
Bram Moolenaar72cf47a2018-05-10 18:23:29 +0200722endfunc
723
Bram Moolenaar1e115362019-01-09 23:01:02 +0100724func Fun_RenewFile()
Bram Moolenaar026587b2019-08-17 15:08:00 +0200725 " Need to wait a bit for the timestamp to be older.
Bram Moolenaarce90e362019-09-08 18:58:44 +0200726 let old_ftime = getftime("tmp.txt")
727 while getftime("tmp.txt") == old_ftime
728 sleep 100m
729 silent execute '!echo "1" > tmp.txt'
730 endwhile
Bram Moolenaara42df592018-12-24 00:22:39 +0100731 sp
732 wincmd p
733 edit! tmp.txt
Bram Moolenaar1e115362019-01-09 23:01:02 +0100734endfunc
Bram Moolenaara42df592018-12-24 00:22:39 +0100735
736func Test_window_prevwin()
737 " Can we make this work on MS-Windows?
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +0200738 CheckUnix
Bram Moolenaara42df592018-12-24 00:22:39 +0100739
740 set hidden autoread
Bram Moolenaardb4c9472022-10-15 22:06:06 +0100741 call writefile(['2'], 'tmp.txt', 'D')
Bram Moolenaara42df592018-12-24 00:22:39 +0100742 new tmp.txt
743 q
Bram Moolenaara42df592018-12-24 00:22:39 +0100744 call Fun_RenewFile()
745 call assert_equal(2, winnr())
746 wincmd p
747 call assert_equal(1, winnr())
748 wincmd p
749 q
750 call Fun_RenewFile()
751 call assert_equal(2, winnr())
752 wincmd p
753 call assert_equal(1, winnr())
754 wincmd p
755 " reset
756 q
Bram Moolenaara42df592018-12-24 00:22:39 +0100757 set hidden&vim autoread&vim
758 delfunc Fun_RenewFile
759endfunc
760
Bram Moolenaar8fcb60f2019-03-04 13:18:30 +0100761func Test_relative_cursor_position_in_one_line_window()
762 new
763 only
764 call setline(1, range(1, 10000))
765 normal 50%
766 let lnum = getcurpos()[1]
767 split
768 split
769 " make third window take as many lines as possible, other windows will
770 " become one line
771 3wincmd w
772 for i in range(1, &lines - 6)
773 wincmd +
774 redraw!
775 endfor
776
777 " first and second window should show cursor line
778 let wininfo = getwininfo()
779 call assert_equal(lnum, wininfo[0].topline)
780 call assert_equal(lnum, wininfo[1].topline)
781
782 only!
783 bwipe!
Yegappan Lakshmanan04c4c572022-08-30 19:48:24 +0100784 call assert_fails('call winrestview(test_null_dict())', 'E1297:')
Bram Moolenaar8fcb60f2019-03-04 13:18:30 +0100785endfunc
786
787func Test_relative_cursor_position_after_move_and_resize()
788 let so_save = &so
789 set so=0
790 enew
791 call setline(1, range(1, 10000))
792 normal 50%
793 split
794 1wincmd w
795 " Move cursor to first line in window
796 normal H
797 redraw!
798 " Reduce window height to two lines
799 let height = winheight(0)
800 while winheight(0) > 2
801 wincmd -
802 redraw!
803 endwhile
804 " move cursor to second/last line in window
805 normal j
806 " restore previous height
807 while winheight(0) < height
808 wincmd +
809 redraw!
810 endwhile
811 " make window two lines again
812 while winheight(0) > 2
813 wincmd -
814 redraw!
815 endwhile
816
817 " cursor should be at bottom line
818 let info = getwininfo(win_getid())[0]
819 call assert_equal(info.topline + 1, getcurpos()[1])
820
821 only!
822 bwipe!
823 let &so = so_save
824endfunc
825
826func Test_relative_cursor_position_after_resize()
827 let so_save = &so
828 set so=0
829 enew
830 call setline(1, range(1, 10000))
831 normal 50%
832 split
833 1wincmd w
834 let winid1 = win_getid()
835 let info = getwininfo(winid1)[0]
836 " Move cursor to second line in window
837 exe "normal " . (info.topline + 1) . "G"
838 redraw!
839 let lnum = getcurpos()[1]
840
841 " Make the window only two lines high, cursor should end up in top line
842 2wincmd w
843 exe (info.height - 2) . "wincmd +"
844 redraw!
845 let info = getwininfo(winid1)[0]
846 call assert_equal(lnum, info.topline)
847
848 only!
849 bwipe!
850 let &so = so_save
851endfunc
852
853func Test_relative_cursor_second_line_after_resize()
854 let so_save = &so
855 set so=0
856 enew
857 call setline(1, range(1, 10000))
858 normal 50%
859 split
860 1wincmd w
861 let winid1 = win_getid()
862 let info = getwininfo(winid1)[0]
863
864 " Make the window only two lines high
865 2wincmd _
866
867 " Move cursor to second line in window
868 normal H
869 normal j
870
871 " Make window size bigger, then back to 2 lines
872 for i in range(1, 10)
873 wincmd +
874 redraw!
875 endfor
876 for i in range(1, 10)
877 wincmd -
878 redraw!
879 endfor
880
881 " cursor should end up in bottom line
882 let info = getwininfo(winid1)[0]
883 call assert_equal(info.topline + 1, getcurpos()[1])
884
885 only!
886 bwipe!
887 let &so = so_save
888endfunc
889
Bram Moolenaara9b25352019-05-12 14:25:30 +0200890func Test_split_noscroll()
891 let so_save = &so
Bram Moolenaarbd2d68c2019-05-18 15:36:11 +0200892 enew
893 call setline(1, range(1, 8))
Bram Moolenaara9b25352019-05-12 14:25:30 +0200894 normal 100%
895 split
896
897 1wincmd w
898 let winid1 = win_getid()
899 let info1 = getwininfo(winid1)[0]
900
901 2wincmd w
902 let winid2 = win_getid()
903 let info2 = getwininfo(winid2)[0]
904
905 call assert_equal(1, info1.topline)
906 call assert_equal(1, info2.topline)
907
Bram Moolenaarbd2d68c2019-05-18 15:36:11 +0200908 " window that fits all lines by itself, but not when split: closing other
909 " window should restore fraction.
Bram Moolenaara9b25352019-05-12 14:25:30 +0200910 only!
Bram Moolenaarbd2d68c2019-05-18 15:36:11 +0200911 call setline(1, range(1, &lines - 10))
912 exe &lines / 4
913 let winid1 = win_getid()
914 let info1 = getwininfo(winid1)[0]
915 call assert_equal(1, info1.topline)
916 new
917 redraw
918 close
919 let info1 = getwininfo(winid1)[0]
920 call assert_equal(1, info1.topline)
921
Bram Moolenaara9b25352019-05-12 14:25:30 +0200922 bwipe!
923 let &so = so_save
924endfunc
925
Bram Moolenaar46ad2882019-04-08 20:01:47 +0200926" Tests for the winnr() function
927func Test_winnr()
928 only | tabonly
929 call assert_equal(1, winnr('j'))
930 call assert_equal(1, winnr('k'))
931 call assert_equal(1, winnr('h'))
932 call assert_equal(1, winnr('l'))
933
934 " create a set of horizontally and vertically split windows
935 leftabove new | wincmd p
936 leftabove new | wincmd p
937 rightbelow new | wincmd p
938 rightbelow new | wincmd p
939 leftabove vnew | wincmd p
940 leftabove vnew | wincmd p
941 rightbelow vnew | wincmd p
942 rightbelow vnew | wincmd p
943
944 call assert_equal(8, winnr('j'))
945 call assert_equal(2, winnr('k'))
946 call assert_equal(4, winnr('h'))
947 call assert_equal(6, winnr('l'))
948 call assert_equal(9, winnr('2j'))
949 call assert_equal(1, winnr('2k'))
950 call assert_equal(3, winnr('2h'))
951 call assert_equal(7, winnr('2l'))
952
953 " Error cases
954 call assert_fails("echo winnr('0.2k')", 'E15:')
955 call assert_equal(2, winnr('-2k'))
956 call assert_fails("echo winnr('-2xj')", 'E15:')
957 call assert_fails("echo winnr('j2j')", 'E15:')
958 call assert_fails("echo winnr('ll')", 'E15:')
959 call assert_fails("echo winnr('5')", 'E15:')
960 call assert_equal(4, winnr('0h'))
Bram Moolenaar99fa7212020-04-26 15:59:55 +0200961 call assert_fails("let w = winnr([])", 'E730:')
962 call assert_equal('unknown', win_gettype(-1))
963 call assert_equal(-1, winheight(-1))
964 call assert_equal(-1, winwidth(-1))
Bram Moolenaar46ad2882019-04-08 20:01:47 +0200965
966 tabnew
967 call assert_equal(8, tabpagewinnr(1, 'j'))
Bram Moolenaarce90e362019-09-08 18:58:44 +0200968 call assert_equal(2, 1->tabpagewinnr('k'))
Bram Moolenaar46ad2882019-04-08 20:01:47 +0200969 call assert_equal(4, tabpagewinnr(1, 'h'))
970 call assert_equal(6, tabpagewinnr(1, 'l'))
971
972 only | tabonly
973endfunc
974
Bram Moolenaarf92e58c2019-09-08 21:51:41 +0200975func Test_winrestview()
976 split runtest.vim
977 normal 50%
978 let view = winsaveview()
979 close
980 split runtest.vim
981 eval view->winrestview()
982 call assert_equal(view, winsaveview())
983
984 bwipe!
Yegappan Lakshmanan04c4c572022-08-30 19:48:24 +0100985 call assert_fails('call winrestview(test_null_dict())', 'E1297:')
Bram Moolenaarf92e58c2019-09-08 21:51:41 +0200986endfunc
987
Bram Moolenaard20dcb32019-09-10 21:22:58 +0200988func Test_win_splitmove()
Bram Moolenaar5a4c3082019-12-01 15:23:11 +0100989 CheckFeature quickfix
990
Bram Moolenaard20dcb32019-09-10 21:22:58 +0200991 edit a
992 leftabove split b
993 leftabove vsplit c
994 leftabove split d
995 call assert_equal(0, win_splitmove(winnr(), winnr('l')))
996 call assert_equal(bufname(winbufnr(1)), 'c')
997 call assert_equal(bufname(winbufnr(2)), 'd')
998 call assert_equal(bufname(winbufnr(3)), 'b')
999 call assert_equal(bufname(winbufnr(4)), 'a')
1000 call assert_equal(0, win_splitmove(winnr(), winnr('j'), {'vertical': 1}))
1001 call assert_equal(0, win_splitmove(winnr(), winnr('j'), {'vertical': 1}))
1002 call assert_equal(bufname(winbufnr(1)), 'c')
1003 call assert_equal(bufname(winbufnr(2)), 'b')
1004 call assert_equal(bufname(winbufnr(3)), 'd')
1005 call assert_equal(bufname(winbufnr(4)), 'a')
1006 call assert_equal(0, win_splitmove(winnr(), winnr('k'), {'vertical': 1}))
1007 call assert_equal(bufname(winbufnr(1)), 'd')
1008 call assert_equal(bufname(winbufnr(2)), 'c')
1009 call assert_equal(bufname(winbufnr(3)), 'b')
1010 call assert_equal(bufname(winbufnr(4)), 'a')
1011 call assert_equal(0, win_splitmove(winnr(), winnr('j'), {'rightbelow': v:true}))
1012 call assert_equal(bufname(winbufnr(1)), 'c')
1013 call assert_equal(bufname(winbufnr(2)), 'b')
1014 call assert_equal(bufname(winbufnr(3)), 'a')
1015 call assert_equal(bufname(winbufnr(4)), 'd')
Yegappan Lakshmanan04c4c572022-08-30 19:48:24 +01001016 call assert_fails('call win_splitmove(winnr(), winnr("k"), test_null_dict())', 'E1297:')
Bram Moolenaard20dcb32019-09-10 21:22:58 +02001017 only | bd
1018
1019 call assert_fails('call win_splitmove(winnr(), 123)', 'E957:')
1020 call assert_fails('call win_splitmove(123, winnr())', 'E957:')
1021 call assert_fails('call win_splitmove(winnr(), winnr())', 'E957:')
Bram Moolenaar7b94e772020-01-06 21:03:24 +01001022
1023 tabnew
1024 call assert_fails('call win_splitmove(1, win_getid(1, 1))', 'E957:')
1025 tabclose
Bram Moolenaard20dcb32019-09-10 21:22:58 +02001026endfunc
1027
Bram Moolenaare20b9ec2020-02-03 21:40:04 +01001028" Test for the :only command
1029func Test_window_only()
1030 new
1031 set modified
1032 new
1033 call assert_fails('only', 'E445:')
1034 only!
Bram Moolenaarbc2b71d2020-02-17 21:33:30 +01001035 " Test for :only with a count
1036 let wid = win_getid()
1037 new
1038 new
1039 3only
1040 call assert_equal(1, winnr('$'))
1041 call assert_equal(wid, win_getid())
1042 call assert_fails('close', 'E444:')
1043 call assert_fails('%close', 'E16:')
Bram Moolenaare20b9ec2020-02-03 21:40:04 +01001044endfunc
1045
Bram Moolenaar9f6277b2020-02-11 22:04:02 +01001046" Test for errors with :wincmd
1047func Test_wincmd_errors()
1048 call assert_fails('wincmd g', 'E474:')
1049 call assert_fails('wincmd ab', 'E474:')
1050endfunc
1051
1052" Test for errors with :winpos
1053func Test_winpos_errors()
1054 if !has("gui_running") && !has('win32')
1055 call assert_fails('winpos', 'E188:')
1056 endif
1057 call assert_fails('winpos 10', 'E466:')
1058endfunc
1059
Bram Moolenaar406cd902020-02-18 21:54:41 +01001060" Test for +cmd in a :split command
1061func Test_split_cmd()
1062 split +set\ readonly
1063 call assert_equal(1, &readonly)
1064 call assert_equal(2, winnr('$'))
1065 close
1066endfunc
1067
Bram Moolenaar5080b0a2020-03-22 21:23:47 +01001068" Create maximum number of horizontally or vertically split windows and then
1069" run commands that create a new horizontally/vertically split window
1070func Run_noroom_for_newwindow_test(dir_arg)
1071 let dir = (a:dir_arg == 'v') ? 'vert ' : ''
1072
1073 " Open as many windows as possible
Bram Moolenaarab505b12020-03-23 19:28:44 +01001074 while v:true
Bram Moolenaar5080b0a2020-03-22 21:23:47 +01001075 try
1076 exe dir . 'new'
1077 catch /E36:/
1078 break
1079 endtry
Bram Moolenaarab505b12020-03-23 19:28:44 +01001080 endwhile
Bram Moolenaar5080b0a2020-03-22 21:23:47 +01001081
Bram Moolenaardb4c9472022-10-15 22:06:06 +01001082 call writefile(['first', 'second', 'third'], 'Xnorfile1', 'D')
1083 call writefile([], 'Xnorfile2', 'D')
1084 call writefile([], 'Xnorfile3', 'D')
Bram Moolenaar5080b0a2020-03-22 21:23:47 +01001085
1086 " Argument list related commands
Bram Moolenaarb18b4962022-09-02 21:55:50 +01001087 args Xnorfile1 Xnorfile2 Xnorfile3
Bram Moolenaar5080b0a2020-03-22 21:23:47 +01001088 next
1089 for cmd in ['sargument 2', 'snext', 'sprevious', 'sNext', 'srewind',
1090 \ 'sfirst', 'slast']
1091 call assert_fails(dir .. cmd, 'E36:')
1092 endfor
1093 %argdelete
1094
1095 " Buffer related commands
1096 set modified
1097 hide enew
Bram Moolenaarb18b4962022-09-02 21:55:50 +01001098 for cmd in ['sbuffer Xnorfile1', 'sbnext', 'sbprevious', 'sbNext', 'sbrewind',
Bram Moolenaar5080b0a2020-03-22 21:23:47 +01001099 \ 'sbfirst', 'sblast', 'sball', 'sbmodified', 'sunhide']
1100 call assert_fails(dir .. cmd, 'E36:')
1101 endfor
1102
1103 " Window related commands
Bram Moolenaarb18b4962022-09-02 21:55:50 +01001104 for cmd in ['split', 'split Xnorfile2', 'new', 'new Xnorfile3', 'sview Xnorfile1',
Bram Moolenaar5080b0a2020-03-22 21:23:47 +01001105 \ 'sfind runtest.vim']
1106 call assert_fails(dir .. cmd, 'E36:')
1107 endfor
1108
1109 " Help
1110 call assert_fails(dir .. 'help', 'E36:')
1111 call assert_fails(dir .. 'helpgrep window', 'E36:')
1112
1113 " Command-line window
1114 if a:dir_arg == 'h'
1115 " Cmd-line window is always a horizontally split window
1116 call assert_beeps('call feedkeys("q:\<CR>", "xt")')
1117 endif
1118
1119 " Quickfix and location list window
1120 if has('quickfix')
1121 cexpr ''
1122 call assert_fails(dir .. 'copen', 'E36:')
1123 lexpr ''
1124 call assert_fails(dir .. 'lopen', 'E36:')
1125
1126 " Preview window
Bram Moolenaarb18b4962022-09-02 21:55:50 +01001127 call assert_fails(dir .. 'pedit Xnorfile2', 'E36:')
Bram Moolenaar5080b0a2020-03-22 21:23:47 +01001128 call setline(1, 'abc')
1129 call assert_fails(dir .. 'psearch abc', 'E36:')
1130 endif
1131
1132 " Window commands (CTRL-W ^ and CTRL-W f)
1133 if a:dir_arg == 'h'
1134 call assert_fails('call feedkeys("\<C-W>^", "xt")', 'E36:')
Bram Moolenaarb18b4962022-09-02 21:55:50 +01001135 call setline(1, 'Xnorfile1')
Bram Moolenaar5080b0a2020-03-22 21:23:47 +01001136 call assert_fails('call feedkeys("gg\<C-W>f", "xt")', 'E36:')
1137 endif
1138 enew!
1139
1140 " Tag commands (:stag, :stselect and :stjump)
1141 call writefile(["!_TAG_FILE_ENCODING\tutf-8\t//",
Bram Moolenaarb18b4962022-09-02 21:55:50 +01001142 \ "second\tXnorfile1\t2",
1143 \ "third\tXnorfile1\t3",],
Bram Moolenaar5080b0a2020-03-22 21:23:47 +01001144 \ 'Xtags')
1145 set tags=Xtags
1146 call assert_fails(dir .. 'stag second', 'E36:')
1147 call assert_fails('call feedkeys(":" .. dir .. "stselect second\n1\n", "xt")', 'E36:')
1148 call assert_fails(dir .. 'stjump second', 'E36:')
1149 call assert_fails(dir .. 'ptag second', 'E36:')
1150 set tags&
1151 call delete('Xtags')
1152
1153 " :isplit and :dsplit
1154 call setline(1, ['#define FOO 1', 'FOO'])
1155 normal 2G
1156 call assert_fails(dir .. 'isplit FOO', 'E36:')
1157 call assert_fails(dir .. 'dsplit FOO', 'E36:')
1158
1159 " terminal
1160 if has('terminal')
1161 call assert_fails(dir .. 'terminal', 'E36:')
1162 endif
1163
1164 %bwipe!
Bram Moolenaar5080b0a2020-03-22 21:23:47 +01001165 only
1166endfunc
1167
1168func Test_split_cmds_with_no_room()
1169 call Run_noroom_for_newwindow_test('h')
1170 call Run_noroom_for_newwindow_test('v')
1171endfunc
1172
Bram Moolenaarad48e6c2020-04-21 22:19:45 +02001173" Test for various wincmd failures
1174func Test_wincmd_fails()
1175 only!
1176 call assert_beeps("normal \<C-W>w")
1177 call assert_beeps("normal \<C-W>p")
1178 call assert_beeps("normal \<C-W>gk")
1179 call assert_beeps("normal \<C-W>r")
1180 call assert_beeps("normal \<C-W>K")
1181 call assert_beeps("normal \<C-W>H")
1182 call assert_beeps("normal \<C-W>2gt")
1183endfunc
1184
Bram Moolenaarfe6dce82020-09-04 14:41:21 +02001185func Test_window_resize()
1186 " Vertical :resize (absolute, relative, min and max size).
1187 vsplit
1188 vert resize 8
1189 call assert_equal(8, winwidth(0))
1190 vert resize +2
1191 call assert_equal(10, winwidth(0))
1192 vert resize -2
1193 call assert_equal(8, winwidth(0))
1194 vert resize
1195 call assert_equal(&columns - 2, winwidth(0))
1196 vert resize 0
1197 call assert_equal(1, winwidth(0))
1198 vert resize 99999
1199 call assert_equal(&columns - 2, winwidth(0))
1200
1201 %bwipe!
1202
1203 " Horizontal :resize (with absolute, relative size, min and max size).
1204 split
1205 resize 8
1206 call assert_equal(8, winheight(0))
1207 resize +2
1208 call assert_equal(10, winheight(0))
1209 resize -2
1210 call assert_equal(8, winheight(0))
1211 resize
1212 call assert_equal(&lines - 4, winheight(0))
1213 resize 0
1214 call assert_equal(1, winheight(0))
1215 resize 99999
1216 call assert_equal(&lines - 4, winheight(0))
1217
1218 " :resize with explicit window number.
1219 let other_winnr = winnr('j')
1220 exe other_winnr .. 'resize 10'
1221 call assert_equal(10, winheight(other_winnr))
1222 call assert_equal(&lines - 10 - 3, winheight(0))
Bram Moolenaar9668cc52020-10-17 17:39:55 +02001223 exe other_winnr .. 'resize +1'
1224 exe other_winnr .. 'resize +1'
1225 call assert_equal(12, winheight(other_winnr))
1226 call assert_equal(&lines - 10 - 3 -2, winheight(0))
Bram Moolenaar89015a62020-12-29 12:46:51 +01001227 close
1228
1229 vsplit
1230 wincmd l
1231 let other_winnr = winnr('h')
1232 call assert_notequal(winnr(), other_winnr)
Bram Moolenaar5efe0e52021-01-01 14:31:34 +01001233 exe 'vert ' .. other_winnr .. 'resize -' .. &columns
Bram Moolenaar89015a62020-12-29 12:46:51 +01001234 call assert_equal(0, winwidth(other_winnr))
Bram Moolenaarfe6dce82020-09-04 14:41:21 +02001235
1236 %bwipe!
1237endfunc
1238
Bram Moolenaar5d3c9f82020-06-26 20:41:39 +02001239" Test for adjusting the window width when a window is closed with some
1240" windows using 'winfixwidth'
1241func Test_window_width_adjust()
1242 only
1243 " Three vertical windows. Windows 1 and 2 have 'winfixwidth' set and close
1244 " window 2.
1245 wincmd v
1246 vert resize 10
1247 set winfixwidth
1248 wincmd v
1249 set winfixwidth
1250 wincmd c
1251 call assert_inrange(10, 12, winwidth(1))
1252 " Three vertical windows. Windows 2 and 3 have 'winfixwidth' set and close
1253 " window 3.
1254 only
1255 set winfixwidth
1256 wincmd v
1257 vert resize 10
1258 set winfixwidth
1259 wincmd v
1260 set nowinfixwidth
1261 wincmd b
1262 wincmd c
1263 call assert_inrange(10, 12, winwidth(2))
1264
1265 new | only
1266endfunc
1267
1268" Test for jumping to a vertical/horizontal neighbor window based on the
1269" current cursor position
1270func Test_window_goto_neightbor()
1271 %bw!
1272
1273 " Vertical window movement
1274
1275 " create the following window layout:
1276 " +--+--+
1277 " |w1|w3|
1278 " +--+ |
1279 " |w2| |
1280 " +--+--+
1281 " |w4 |
1282 " +-----+
1283 new
1284 vsplit
1285 split
1286 " vertically jump from w4
1287 wincmd b
1288 call setline(1, repeat(' ', &columns))
1289 call cursor(1, 1)
1290 wincmd k
1291 call assert_equal(2, winnr())
1292 wincmd b
1293 call cursor(1, &columns)
1294 redraw!
1295 wincmd k
1296 call assert_equal(3, winnr())
1297 %bw!
1298
1299 " create the following window layout:
1300 " +--+--+--+
1301 " |w1|w2|w3|
1302 " +--+--+--+
1303 " |w4 |
1304 " +--------+
1305 new
1306 vsplit
1307 vsplit
1308 wincmd b
1309 call setline(1, repeat(' ', &columns))
1310 call cursor(1, 1)
1311 wincmd k
1312 call assert_equal(1, winnr())
1313 wincmd b
1314 call cursor(1, &columns / 2)
1315 redraw!
1316 wincmd k
1317 call assert_equal(2, winnr())
1318 wincmd b
1319 call cursor(1, &columns)
1320 redraw!
1321 wincmd k
1322 call assert_equal(3, winnr())
1323 %bw!
1324
1325 " Horizontal window movement
1326
1327 " create the following window layout:
1328 " +--+--+--+
1329 " |w1|w2|w4|
1330 " +--+--+ |
1331 " |w3 | |
1332 " +-----+--+
1333 vsplit
1334 split
1335 vsplit
1336 4wincmd l
1337 call setline(1, repeat([' '], &lines))
1338 call cursor(1, 1)
1339 redraw!
1340 wincmd h
1341 call assert_equal(2, winnr())
1342 4wincmd l
1343 call cursor(&lines, 1)
1344 redraw!
1345 wincmd h
1346 call assert_equal(3, winnr())
1347 %bw!
1348
1349 " create the following window layout:
1350 " +--+--+
1351 " |w1|w4|
1352 " +--+ +
1353 " |w2| |
1354 " +--+ +
1355 " |w3| |
1356 " +--+--+
1357 vsplit
1358 split
1359 split
1360 wincmd l
1361 call setline(1, repeat([' '], &lines))
1362 call cursor(1, 1)
1363 redraw!
1364 wincmd h
1365 call assert_equal(1, winnr())
1366 wincmd l
1367 call cursor(&lines / 2, 1)
1368 redraw!
1369 wincmd h
1370 call assert_equal(2, winnr())
1371 wincmd l
1372 call cursor(&lines, 1)
1373 redraw!
1374 wincmd h
1375 call assert_equal(3, winnr())
1376 %bw!
1377endfunc
1378
1379" Test for an autocmd closing the destination window when jumping from one
1380" window to another.
1381func Test_close_dest_window()
1382 split
Bram Moolenaarb18b4962022-09-02 21:55:50 +01001383 edit Xdstfile
Bram Moolenaar5d3c9f82020-06-26 20:41:39 +02001384
1385 " Test for BufLeave
1386 augroup T1
1387 au!
Bram Moolenaarb18b4962022-09-02 21:55:50 +01001388 au BufLeave Xdstfile $wincmd c
Bram Moolenaar5d3c9f82020-06-26 20:41:39 +02001389 augroup END
1390 wincmd b
1391 call assert_equal(1, winnr('$'))
Bram Moolenaarb18b4962022-09-02 21:55:50 +01001392 call assert_equal('Xdstfile', @%)
Bram Moolenaar5d3c9f82020-06-26 20:41:39 +02001393 augroup T1
1394 au!
1395 augroup END
1396
1397 " Test for WinLeave
1398 new
1399 wincmd p
1400 augroup T1
1401 au!
1402 au WinLeave * 1wincmd c
1403 augroup END
1404 wincmd t
1405 call assert_equal(1, winnr('$'))
Bram Moolenaarb18b4962022-09-02 21:55:50 +01001406 call assert_equal('Xdstfile', @%)
Bram Moolenaar5d3c9f82020-06-26 20:41:39 +02001407 augroup T1
1408 au!
1409 augroup END
1410 augroup! T1
1411 %bw!
1412endfunc
1413
Bram Moolenaard0fb9072021-12-09 11:57:22 +00001414func Test_window_minimal_size()
1415 set winminwidth=0 winminheight=0
1416
1417 " check size is fixed vertically
1418 new
1419 call win_execute(win_getid(2), 'wincmd _')
1420 call assert_equal(0, winheight(0))
1421 call feedkeys('0', 'tx')
1422 call assert_equal(1, winheight(0))
1423 bwipe!
1424
1425 " check size is fixed horizontally
1426 vert new
1427 call win_execute(win_getid(2), 'wincmd |')
1428 call assert_equal(0, winwidth(0))
1429 call feedkeys('0', 'tx')
1430 call assert_equal(1, winwidth(0))
1431 bwipe!
1432
1433 if has('timers')
1434 " check size is fixed in Insert mode
ichizokfa9a8e02021-12-12 16:42:09 +00001435 func s:CheckSize(timer) abort
1436 call win_execute(win_getid(2), 'wincmd _')
1437 call assert_equal(0, winheight(0))
1438 call feedkeys(" \<Esc>", 't!')
1439 endfunc
Bram Moolenaard0fb9072021-12-09 11:57:22 +00001440 new
ichizokfa9a8e02021-12-12 16:42:09 +00001441 call timer_start(100, function('s:CheckSize'))
Bram Moolenaard0fb9072021-12-09 11:57:22 +00001442 call feedkeys('a', 'tx!')
1443 call assert_equal(1, winheight(0))
1444 bwipe!
ichizokfa9a8e02021-12-12 16:42:09 +00001445 delfunc s:CheckSize
Bram Moolenaard0fb9072021-12-09 11:57:22 +00001446 endif
1447
1448 set winminwidth& winminheight&
1449endfunc
1450
Daniel Steinbergee630312022-01-10 13:36:34 +00001451func Test_win_move_separator()
1452 edit a
1453 leftabove vsplit b
1454 let w = winwidth(0)
1455 " check win_move_separator from left window on left window
1456 call assert_equal(1, winnr())
1457 for offset in range(5)
1458 call assert_true(win_move_separator(0, offset))
1459 call assert_equal(w + offset, winwidth(0))
1460 call assert_true(0->win_move_separator(-offset))
1461 call assert_equal(w, winwidth(0))
1462 endfor
1463 " check win_move_separator from right window on left window number
1464 wincmd l
1465 call assert_notequal(1, winnr())
1466 for offset in range(5)
1467 call assert_true(1->win_move_separator(offset))
1468 call assert_equal(w + offset, winwidth(1))
1469 call assert_true(win_move_separator(1, -offset))
1470 call assert_equal(w, winwidth(1))
1471 endfor
1472 " check win_move_separator from right window on left window ID
1473 let id = win_getid(1)
1474 for offset in range(5)
1475 call assert_true(win_move_separator(id, offset))
1476 call assert_equal(w + offset, winwidth(id))
1477 call assert_true(id->win_move_separator(-offset))
1478 call assert_equal(w, winwidth(id))
1479 endfor
zeertzjqa0c4e2f2022-01-29 10:59:53 +00001480 " check win_move_separator from right window on right window is no-op
1481 let w0 = winwidth(0)
1482 call assert_true(win_move_separator(0, 1))
1483 call assert_equal(w0, winwidth(0))
1484 call assert_true(win_move_separator(0, -1))
1485 call assert_equal(w0, winwidth(0))
Daniel Steinbergee630312022-01-10 13:36:34 +00001486 " check that win_move_separator doesn't error with offsets beyond moving
1487 " possibility
1488 call assert_true(win_move_separator(id, 5000))
1489 call assert_true(winwidth(id) > w)
1490 call assert_true(win_move_separator(id, -5000))
1491 call assert_true(winwidth(id) < w)
1492 " 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))
1497 " check that win_move_separator returns false for a popup window
1498 let id = popup_create(['hello', 'world'], {})
1499 let w = winwidth(id)
1500 call assert_false(win_move_separator(id, 1))
1501 call assert_equal(w, winwidth(id))
1502 call popup_close(id)
1503 %bwipe!
1504endfunc
1505
1506func Test_win_move_statusline()
1507 edit a
1508 leftabove split b
1509 let h = winheight(0)
1510 " check win_move_statusline from top window on top window
1511 call assert_equal(1, winnr())
1512 for offset in range(5)
1513 call assert_true(win_move_statusline(0, offset))
1514 call assert_equal(h + offset, winheight(0))
1515 call assert_true(0->win_move_statusline(-offset))
1516 call assert_equal(h, winheight(0))
1517 endfor
1518 " check win_move_statusline from bottom window on top window number
1519 wincmd j
1520 call assert_notequal(1, winnr())
1521 for offset in range(5)
1522 call assert_true(1->win_move_statusline(offset))
1523 call assert_equal(h + offset, winheight(1))
1524 call assert_true(win_move_statusline(1, -offset))
1525 call assert_equal(h, winheight(1))
1526 endfor
zeertzjqa0c4e2f2022-01-29 10:59:53 +00001527 " check win_move_statusline from bottom window on bottom window
1528 let h0 = winheight(0)
1529 for offset in range(5)
1530 call assert_true(0->win_move_statusline(-offset))
1531 call assert_equal(h0 - offset, winheight(0))
1532 call assert_equal(1 + offset, &cmdheight)
1533 call assert_true(win_move_statusline(0, offset))
1534 call assert_equal(h0, winheight(0))
1535 call assert_equal(1, &cmdheight)
1536 endfor
1537 call assert_true(win_move_statusline(0, 1))
Bram Moolenaara2a89732022-08-31 14:46:18 +01001538 call assert_equal(h0, winheight(0))
1539 call assert_equal(1, &cmdheight)
Daniel Steinbergee630312022-01-10 13:36:34 +00001540 " check win_move_statusline from bottom window on top window ID
1541 let id = win_getid(1)
1542 for offset in range(5)
1543 call assert_true(win_move_statusline(id, offset))
1544 call assert_equal(h + offset, winheight(id))
1545 call assert_true(id->win_move_statusline(-offset))
1546 call assert_equal(h, winheight(id))
1547 endfor
1548 " check that win_move_statusline doesn't error with offsets beyond moving
1549 " possibility
1550 call assert_true(win_move_statusline(id, 5000))
1551 call assert_true(winheight(id) > h)
1552 call assert_true(win_move_statusline(id, -5000))
1553 call assert_true(winheight(id) < h)
1554 " check that win_move_statusline returns false for an invalid window
1555 wincmd =
1556 let h = winheight(0)
1557 call assert_false(win_move_statusline(-1, 1))
1558 call assert_equal(h, winheight(0))
1559 " check that win_move_statusline returns false for a popup window
1560 let id = popup_create(['hello', 'world'], {})
1561 let h = winheight(id)
1562 call assert_false(win_move_statusline(id, 1))
1563 call assert_equal(h, winheight(id))
1564 call popup_close(id)
1565 %bwipe!
1566endfunc
1567
Yegappan Lakshmanan72bb47e2022-04-03 11:22:38 +01001568" Test for window allocation failure
1569func Test_window_alloc_failure()
1570 %bw!
1571
1572 " test for creating a new window above current window
1573 call test_alloc_fail(GetAllocId('newwin_wvars'), 0, 0)
1574 call assert_fails('above new', 'E342:')
1575 call assert_equal(1, winnr('$'))
1576
1577 " test for creating a new window below current window
1578 call test_alloc_fail(GetAllocId('newwin_wvars'), 0, 0)
1579 call assert_fails('below new', 'E342:')
1580 call assert_equal(1, winnr('$'))
1581
1582 " test for popup window creation failure
1583 call test_alloc_fail(GetAllocId('newwin_wvars'), 0, 0)
1584 call assert_fails('call popup_create("Hello", {})', 'E342:')
1585 call assert_equal([], popup_list())
1586
1587 call test_alloc_fail(GetAllocId('newwin_wvars'), 0, 0)
1588 call assert_fails('split', 'E342:')
1589 call assert_equal(1, winnr('$'))
1590
Bram Moolenaarb18b4962022-09-02 21:55:50 +01001591 edit Xwaffile1
1592 edit Xwaffile2
Yegappan Lakshmanan72bb47e2022-04-03 11:22:38 +01001593 call test_alloc_fail(GetAllocId('newwin_wvars'), 0, 0)
Bram Moolenaarb18b4962022-09-02 21:55:50 +01001594 call assert_fails('sb Xwaffile1', 'E342:')
Yegappan Lakshmanan72bb47e2022-04-03 11:22:38 +01001595 call assert_equal(1, winnr('$'))
Bram Moolenaarb18b4962022-09-02 21:55:50 +01001596 call assert_equal('Xwaffile2', @%)
Yegappan Lakshmanan72bb47e2022-04-03 11:22:38 +01001597 %bw!
1598
1599 " FIXME: The following test crashes Vim
1600 " test for new tabpage creation failure
1601 " call test_alloc_fail(GetAllocId('newwin_wvars'), 0, 0)
1602 " call assert_fails('tabnew', 'E342:')
1603 " call assert_equal(1, tabpagenr('$'))
1604 " call assert_equal(1, winnr('$'))
1605
1606 " This test messes up the internal Vim window/frame information. So the
1607 " Test_window_cmd_cmdwin_with_vsp() test fails after running this test.
1608 " Open a new tab and close everything else to fix this issue.
1609 tabnew
1610 tabonly
1611endfunc
Bram Moolenaard0fb9072021-12-09 11:57:22 +00001612
Luuk van Baalfd7e60a2022-09-07 14:42:49 +01001613func Test_win_equal_last_status()
1614 let save_lines = &lines
1615 set lines=20
1616 set splitbelow
1617 set laststatus=0
1618
1619 split | split | quit
1620 call assert_equal(winheight(1), winheight(2))
1621
1622 let &lines = save_lines
1623 set splitbelow&
1624 set laststatus&
1625endfunc
1626
Luuk van Baal13ece2a2022-10-03 15:28:08 +01001627" Test "screen" and "cursor" values for 'splitkeep' with a sequence of
Luuk van Baalfaf1d412022-09-19 16:45:29 +01001628" split operations for various options: with and without a winbar,
1629" tabline, for each possible value of 'laststatus', 'scrolloff',
Luuk van Baal13ece2a2022-10-03 15:28:08 +01001630" 'equalalways', and with the cursor at the top, middle and bottom.
1631func Test_splitkeep_options()
Luuk van Baal594f9e02022-09-16 12:52:58 +01001632 " disallow window resizing
1633 let save_WS = &t_WS
1634 set t_WS=
1635
Luuk van Baal29ab5242022-09-11 16:59:53 +01001636 let gui = has("gui_running")
Luuk van Baal594f9e02022-09-16 12:52:58 +01001637 inoremap <expr> c "<cmd>copen<bar>wincmd k<CR>"
Luuk van Baal13ece2a2022-10-03 15:28:08 +01001638 for run in range(0, 20)
1639 let &splitkeep = run > 10 ? 'topline' : 'screen'
1640 let &scrolloff = (!(run % 4) ? 0 : run)
1641 let &laststatus = (run % 3)
1642 let &splitbelow = (run % 3)
1643 let &equalalways = (run % 2)
1644 let wsb = (run % 2) && &splitbelow
1645 let tl = (gui ? 0 : ((run % 5) ? 1 : 0))
1646 let pos = !(run % 3) ? 'H' : ((run % 2) ? 'M' : 'L')
1647 tabnew | tabonly! | redraw
Luuk van Baal594f9e02022-09-16 12:52:58 +01001648 execute (run % 5) ? 'tabnew' : ''
1649 execute (run % 2) ? 'nnoremenu 1.10 WinBar.Test :echo' : ''
Luuk van Baal594f9e02022-09-16 12:52:58 +01001650 call setline(1, range(1, 256))
1651 " No scroll for restore_snapshot
1652 norm G
1653 try
1654 copen | close | colder
1655 catch /E380/
1656 endtry
1657 call assert_equal(257 - winheight(0), line("w0"))
Luuk van Baal29ab5242022-09-11 16:59:53 +01001658
Luuk van Baal594f9e02022-09-16 12:52:58 +01001659 " No scroll for firstwin horizontal split
1660 execute 'norm gg' . pos
1661 split | redraw | wincmd k
1662 call assert_equal(1, line("w0"))
1663 call assert_equal(&scroll, winheight(0) / 2)
1664 wincmd j
Luuk van Baal13ece2a2022-10-03 15:28:08 +01001665 call assert_equal(&spk == 'topline' ? 1 : win_screenpos(0)[0] - tl - wsb, line("w0"))
Luuk van Baal29ab5242022-09-11 16:59:53 +01001666
Luuk van Baal594f9e02022-09-16 12:52:58 +01001667 " No scroll when resizing windows
Luuk van Baal13ece2a2022-10-03 15:28:08 +01001668 wincmd k | resize +2 | redraw
Luuk van Baal594f9e02022-09-16 12:52:58 +01001669 call assert_equal(1, line("w0"))
1670 wincmd j
Luuk van Baal13ece2a2022-10-03 15:28:08 +01001671 call assert_equal(&spk == 'topline' ? 1 : win_screenpos(0)[0] - tl - wsb, line("w0"))
Luuk van Baal29ab5242022-09-11 16:59:53 +01001672
Luuk van Baal594f9e02022-09-16 12:52:58 +01001673 " No scroll when dragging statusline
1674 call win_move_statusline(1, -3)
Luuk van Baal13ece2a2022-10-03 15:28:08 +01001675 call assert_equal(&spk == 'topline' ? 1 : win_screenpos(0)[0] - tl - wsb, line("w0"))
Luuk van Baal594f9e02022-09-16 12:52:58 +01001676 wincmd k
1677 call assert_equal(1, line("w0"))
Luuk van Baal29ab5242022-09-11 16:59:53 +01001678
Luuk van Baal594f9e02022-09-16 12:52:58 +01001679 " No scroll when changing shellsize
1680 set lines+=2
1681 call assert_equal(1, line("w0"))
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 Baal594f9e02022-09-16 12:52:58 +01001684 set lines-=2
Luuk van Baal13ece2a2022-10-03 15:28:08 +01001685 call assert_equal(&spk == 'topline' ? 1 : win_screenpos(0)[0] - tl - wsb, line("w0"))
Luuk van Baal594f9e02022-09-16 12:52:58 +01001686 wincmd k
1687 call assert_equal(1, line("w0"))
Luuk van Baal29ab5242022-09-11 16:59:53 +01001688
Luuk van Baal594f9e02022-09-16 12:52:58 +01001689 " No scroll when equalizing windows
1690 wincmd =
1691 call assert_equal(1, line("w0"))
1692 wincmd j
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 in windows split multiple times
1698 vsplit | split | 4wincmd w
Luuk van Baal13ece2a2022-10-03 15:28:08 +01001699 call assert_equal(&spk == 'topline' ? 1 : win_screenpos(0)[0] - tl - wsb, line("w0"))
Luuk van Baal594f9e02022-09-16 12:52:58 +01001700 1wincmd w | quit | wincmd l | split
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 wincmd j
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 Baal29ab5242022-09-11 16:59:53 +01001704
Luuk van Baal594f9e02022-09-16 12:52:58 +01001705 " No scroll in small window
1706 2wincmd w | only | 5split | wincmd k
1707 call assert_equal(1, line("w0"))
1708 wincmd j
Luuk van Baal13ece2a2022-10-03 15:28:08 +01001709 call assert_equal(&spk == 'topline' ? 1 : win_screenpos(0)[0] - tl - wsb, line("w0"))
Luuk van Baal29ab5242022-09-11 16:59:53 +01001710
Luuk van Baal594f9e02022-09-16 12:52:58 +01001711 " No scroll for vertical split
1712 quit | vsplit | wincmd l
1713 call assert_equal(1, line("w0"))
1714 wincmd h
1715 call assert_equal(1, line("w0"))
Luuk van Baal29ab5242022-09-11 16:59:53 +01001716
Luuk van Baal594f9e02022-09-16 12:52:58 +01001717 " No scroll in windows split and quit multiple times
Luuk van Baal13ece2a2022-10-03 15:28:08 +01001718 quit | redraw | split | split | quit | redraw
1719 call assert_equal(&spk == 'topline' ? 1 : win_screenpos(0)[0] - tl - wsb, line("w0"))
Luuk van Baal29ab5242022-09-11 16:59:53 +01001720
Luuk van Baal594f9e02022-09-16 12:52:58 +01001721 " No scroll for new buffer
1722 1wincmd w | only | copen | wincmd k
1723 call assert_equal(1, line("w0"))
1724 only
1725 call assert_equal(1, line("w0"))
1726 above copen | wincmd j
Luuk van Baal13ece2a2022-10-03 15:28:08 +01001727 call assert_equal(&spk == 'topline' ? 1 : win_screenpos(0)[0] - tl, line("w0"))
Luuk van Baal29ab5242022-09-11 16:59:53 +01001728
Luuk van Baal594f9e02022-09-16 12:52:58 +01001729 " No scroll when opening cmdwin, and no cursor move when closing cmdwin.
1730 only | norm ggL
1731 let curpos = getcurpos()
1732 norm q:
1733 call assert_equal(1, line("w0"))
1734 call assert_equal(curpos, getcurpos())
Luuk van Baal29ab5242022-09-11 16:59:53 +01001735
Luuk van Baal594f9e02022-09-16 12:52:58 +01001736 " Scroll when cursor becomes invalid in insert mode
1737 norm Lic
1738 call assert_equal(curpos, getcurpos())
Luuk van Baal29ab5242022-09-11 16:59:53 +01001739
Luuk van Baal594f9e02022-09-16 12:52:58 +01001740 " No scroll when topline not equal to 1
1741 only | execute "norm gg5\<C-e>" | split | wincmd k
1742 call assert_equal(6, line("w0"))
1743 wincmd j
Luuk van Baal13ece2a2022-10-03 15:28:08 +01001744 call assert_equal(&spk == 'topline' ? 6 : 5 + win_screenpos(0)[0] - tl - wsb, line("w0"))
Luuk van Baal29ab5242022-09-11 16:59:53 +01001745 endfor
1746
1747 tabnew | tabonly! | %bwipeout!
1748 iunmap c
Luuk van Baal29ab5242022-09-11 16:59:53 +01001749 set scrolloff&
1750 set splitbelow&
1751 set laststatus&
1752 set equalalways&
Luuk van Baal13ece2a2022-10-03 15:28:08 +01001753 set splitkeep&
Luuk van Baal594f9e02022-09-16 12:52:58 +01001754 let &t_WS = save_WS
Luuk van Baal29ab5242022-09-11 16:59:53 +01001755endfunc
Luuk van Baalfd7e60a2022-09-07 14:42:49 +01001756
Luuk van Baal13ece2a2022-10-03 15:28:08 +01001757function Test_splitkeep_cmdwin_cursor_position()
1758 set splitkeep=screen
mityu12167d82022-09-15 17:44:07 +01001759 call setline(1, range(&lines))
1760
1761 " No scroll when cursor is at near bottom of window and cusor position
1762 " recompution (done by line('w0') in this test) happens while in cmdwin.
1763 normal! G
1764 let firstline = line('w0')
1765 autocmd CmdwinEnter * ++once autocmd WinEnter * ++once call line('w0')
1766 execute "normal! q:\<C-w>q"
1767 redraw!
1768 call assert_equal(firstline, line('w0'))
1769
1770 " User script can change cursor position successfully while in cmdwin and it
1771 " shouldn't be changed when closing cmdwin.
1772 execute "normal! Gq:\<Cmd>call win_execute(winnr('#')->win_getid(), 'call cursor(1, 1)')\<CR>\<C-w>q"
1773 call assert_equal(1, line('.'))
1774 call assert_equal(1, col('.'))
1775
1776 execute "normal! Gq:\<Cmd>autocmd WinEnter * ++once call cursor(1, 1)\<CR>\<C-w>q"
1777 call assert_equal(1, line('.'))
1778 call assert_equal(1, col('.'))
1779
1780 %bwipeout!
Luuk van Baal13ece2a2022-10-03 15:28:08 +01001781 set splitkeep&
mityu12167d82022-09-15 17:44:07 +01001782endfunction
Luuk van Baald5bc7622022-09-17 16:16:35 +01001783
Luuk van Baal13ece2a2022-10-03 15:28:08 +01001784function Test_splitkeep_misc()
1785 set splitkeep=screen
Luuk van Baalfaf1d412022-09-19 16:45:29 +01001786 set splitbelow
Luuk van Baald5bc7622022-09-17 16:16:35 +01001787
1788 call setline(1, range(1, &lines))
1789 norm Gzz
1790 let top = line('w0')
Luuk van Baalfaf1d412022-09-19 16:45:29 +01001791 " No scroll when aucmd_win is opened
Luuk van Baald5bc7622022-09-17 16:16:35 +01001792 call setbufvar(bufnr("test", 1) , '&buftype', 'nofile')
1793 call assert_equal(top, line('w0'))
Luuk van Baalfaf1d412022-09-19 16:45:29 +01001794 " No scroll when tab is changed/closed
1795 tab help | close
1796 call assert_equal(top, line('w0'))
1797 " No scroll when help is closed and buffer line count < window height
1798 norm ggdG
1799 call setline(1, range(1, &lines - 10))
Luuk van Baald5bc7622022-09-17 16:16:35 +01001800 norm G
1801 let top = line('w0')
1802 help | quit
1803 call assert_equal(top, line('w0'))
Luuk van Baal346823d2022-10-05 18:26:42 +01001804 " No error when resizing window in autocmd and buffer length changed
1805 autocmd FileType qf exe "resize" line('$')
1806 cexpr getline(1, '$')
1807 copen
1808 wincmd p
1809 norm dd
1810 cexpr getline(1, '$')
Luuk van Baald5bc7622022-09-17 16:16:35 +01001811
Luuk van Baalfaf1d412022-09-19 16:45:29 +01001812 %bwipeout!
Luuk van Baald5bc7622022-09-17 16:16:35 +01001813 set splitbelow&
Luuk van Baal13ece2a2022-10-03 15:28:08 +01001814 set splitkeep&
Luuk van Baald5bc7622022-09-17 16:16:35 +01001815endfunc
1816
Luuk van Baal13ece2a2022-10-03 15:28:08 +01001817function Test_splitkeep_callback()
Luuk van Baal20e58562022-09-23 12:57:09 +01001818 CheckScreendump
1819 let lines =<< trim END
Luuk van Baal13ece2a2022-10-03 15:28:08 +01001820 set splitkeep=screen
Luuk van Baal20e58562022-09-23 12:57:09 +01001821 call setline(1, range(&lines))
Luuk van Baal13ece2a2022-10-03 15:28:08 +01001822 function C1(a, b)
Luuk van Baal20e58562022-09-23 12:57:09 +01001823 split | wincmd p
1824 endfunction
Luuk van Baal13ece2a2022-10-03 15:28:08 +01001825 function C2(a, b)
Luuk van Baal20e58562022-09-23 12:57:09 +01001826 close | split
1827 endfunction
Luuk van Baal13ece2a2022-10-03 15:28:08 +01001828 nn j <cmd>call job_start([&sh, &shcf, "true"], { 'exit_cb': 'C1' })<CR>
1829 nn t <cmd>call popup_create(term_start([&sh, &shcf, "true"],
1830 \ { 'hidden': 1, 'exit_cb': 'C2' }), {})<CR>
Luuk van Baal20e58562022-09-23 12:57:09 +01001831 END
Luuk van Baal13ece2a2022-10-03 15:28:08 +01001832 call writefile(lines, 'XTestSplitkeepCallback', 'D')
1833 let buf = RunVimInTerminal('-S XTestSplitkeepCallback', #{rows: 8})
Luuk van Baal20e58562022-09-23 12:57:09 +01001834
1835 call term_sendkeys(buf, "j")
Luuk van Baal13ece2a2022-10-03 15:28:08 +01001836 call VerifyScreenDump(buf, 'Test_splitkeep_callback_1', {})
Luuk van Baal20e58562022-09-23 12:57:09 +01001837
Luuk van Baal13ece2a2022-10-03 15:28:08 +01001838 call term_sendkeys(buf, ":quit\<CR>Ht")
1839 call VerifyScreenDump(buf, 'Test_splitkeep_callback_2', {})
Luuk van Baal20e58562022-09-23 12:57:09 +01001840
1841 call term_sendkeys(buf, ":set sb\<CR>:quit\<CR>Gj")
Luuk van Baal13ece2a2022-10-03 15:28:08 +01001842 call VerifyScreenDump(buf, 'Test_splitkeep_callback_3', {})
Luuk van Baal20e58562022-09-23 12:57:09 +01001843
Luuk van Baal13ece2a2022-10-03 15:28:08 +01001844 call term_sendkeys(buf, ":quit\<CR>Gt")
1845 call VerifyScreenDump(buf, 'Test_splitkeep_callback_4', {})
Luuk van Baal20e58562022-09-23 12:57:09 +01001846endfunc
1847
Luuk van Baal13ece2a2022-10-03 15:28:08 +01001848function Test_splitkeep_fold()
1849 CheckScreendump
Luuk van Baal7c1cbb62022-09-27 12:31:15 +01001850
Luuk van Baal13ece2a2022-10-03 15:28:08 +01001851 let lines =<< trim END
1852 set splitkeep=screen
1853 set foldmethod=marker
1854 set number
1855 let line = 1
1856 for n in range(1, &lines)
1857 call setline(line, ['int FuncName() {/*{{{*/', 1, 2, 3, 4, 5, '}/*}}}*/',
1858 \ 'after fold'])
1859 let line += 8
1860 endfor
1861 END
1862 call writefile(lines, 'XTestSplitkeepFold', 'D')
1863 let buf = RunVimInTerminal('-S XTestSplitkeepFold', #{rows: 10})
Luuk van Baal7c1cbb62022-09-27 12:31:15 +01001864
1865 call term_sendkeys(buf, "L:wincmd s\<CR>")
Luuk van Baal13ece2a2022-10-03 15:28:08 +01001866 call VerifyScreenDump(buf, 'Test_splitkeep_fold_1', {})
Luuk van Baal7c1cbb62022-09-27 12:31:15 +01001867
1868 call term_sendkeys(buf, ":quit\<CR>")
Luuk van Baal13ece2a2022-10-03 15:28:08 +01001869 call VerifyScreenDump(buf, 'Test_splitkeep_fold_2', {})
Luuk van Baal7c1cbb62022-09-27 12:31:15 +01001870
1871 call term_sendkeys(buf, "H:below split\<CR>")
Luuk van Baal13ece2a2022-10-03 15:28:08 +01001872 call VerifyScreenDump(buf, 'Test_splitkeep_fold_3', {})
Luuk van Baal7c1cbb62022-09-27 12:31:15 +01001873
1874 call term_sendkeys(buf, ":wincmd k\<CR>:quit\<CR>")
Luuk van Baal13ece2a2022-10-03 15:28:08 +01001875 call VerifyScreenDump(buf, 'Test_splitkeep_fold_4', {})
Luuk van Baal7c1cbb62022-09-27 12:31:15 +01001876endfunction
1877
Bram Moolenaar9e4d8212016-08-18 23:04:48 +02001878" vim: shiftwidth=2 sts=2 expandtab