robin/laufey

git clone https://git.robinwobin.dev/robin/laufey

robinmerge: 1dc9728daa1d8a2702

master
66.7 KiB2531 linesraw
1" Tests for window cmd (:wincmd, :split, :vsplit, :resize and etc...)
2
3source check.vim
4source screendump.vim
5
6func 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_ls0_split_scrolling()
23  CheckRunVimInTerminal
24
25  let lines =<< trim END
26    set laststatus=0
27    call setline(1, range(1, 100))
28    normal! G
29  END
30  call writefile(lines, 'XTestLs0SplitScrolling', 'D')
31  let buf = RunVimInTerminal('-S XTestLs0SplitScrolling', #{rows: 10})
32
33  call term_sendkeys(buf, ":botright split\<CR>")
34  call WaitForAssert({-> assert_match('Bot$', term_getline(buf, 5))})
35  call assert_equal('100', term_getline(buf, 4))
36
37  call StopVimInTerminal(buf)
38endfunc
39
40func Test_window_cmd_cmdwin_with_vsp()
41  let efmt = 'Expected 0 but got %d (in ls=%d, %s window)'
42  for v in range(0, 2)
43    exec "set ls=" . v
44    vsplit
45    call feedkeys("q:\<CR>")
46    let ac = &lines - (&cmdheight + winheight(0) + !!v)
47    let emsg = printf(efmt, ac, v, 'left')
48    call assert_equal(0, ac, emsg)
49    wincmd w
50    let ac = &lines - (&cmdheight + winheight(0) + !!v)
51    let emsg = printf(efmt, ac, v, 'right')
52    call assert_equal(0, ac, emsg)
53    new | only!
54  endfor
55  set ls&vim
56endfunc
57
58func Test_cmdheight_not_changed()
59  set cmdheight=2
60  set winminheight=0
61  augroup Maximize
62    autocmd WinEnter * wincmd _
63  augroup END
64  split
65  tabnew
66  tabfirst
67  call assert_equal(2, &cmdheight)
68
69  tabonly!
70  only
71  set winminheight& cmdheight&
72  augroup Maximize
73    au!
74  augroup END
75  augroup! Maximize
76endfunc
77
78" Test for jumping to windows
79func Test_window_jump()
80  new
81  " jumping to a window with a count greater than the max windows
82  exe "normal 4\<C-W>w"
83  call assert_equal(2, winnr())
84  only
85endfunc
86
87func Test_window_cmd_wincmd_gf()
88  let fname = 'test_gf.txt'
89  let swp_fname = '.' . fname . '.swp'
90  call writefile([], fname, 'D')
91  call writefile([], swp_fname, 'D')
92  function s:swap_exists()
93    let v:swapchoice = s:swap_choice
94  endfunc
95  " Remove the catch-all that runtest.vim adds
96  au! SwapExists
97  augroup test_window_cmd_wincmd_gf
98    autocmd!
99    exec "autocmd SwapExists " . fname . " call s:swap_exists()"
100  augroup END
101
102  call setline(1, fname)
103  " (E)dit anyway
104  let s:swap_choice = 'e'
105  wincmd gf
106  call assert_equal(2, tabpagenr())
107  call assert_equal(fname, bufname("%"))
108  quit!
109
110  " (Q)uit
111  let s:swap_choice = 'q'
112  wincmd gf
113  call assert_equal(1, tabpagenr())
114  call assert_notequal(fname, bufname("%"))
115  new | only!
116
117  au! test_window_cmd_wincmd_gf
118  augroup! test_window_cmd_wincmd_gf
119  delfunc s:swap_exists
120  bw!
121endfunc
122
123func Test_abort_in_wincmd_f()
124  let fname = 'test_f.txt'
125  let swp_fname = $'.{fname}.swp'
126  call writefile([], fname, 'D')
127  call writefile([], swp_fname, 'D')
128  " Remove the catch-all that runtest.vim adds
129  au! SwapExists
130  augroup test_window_cmd_wincmd_f
131    autocmd!
132    " (A)bort
133    autocmd SwapExists test_f.txt let v:swapchoice = 'a'
134  augroup END
135
136  call setline(1, fname)
137  call assert_equal(1, winnr('$'))
138  try
139    wincmd f
140  catch /^Vim:Interrupt$/
141    " expected interrupt by abort
142  endtry
143  call assert_equal(1, winnr('$'))
144  new | only!
145
146  " See :h W19 for the background of this au!. Ideally other tests
147  " should also follow this.
148  au! test_window_cmd_wincmd_f
149  augroup! test_window_cmd_wincmd_f
150  bw!
151endfunc
152
153func Test_window_quit()
154  e Xa
155  split Xb
156  call assert_equal(2, '$'->winnr())
157  call assert_equal('Xb', bufname(winbufnr(1)))
158  call assert_equal('Xa', bufname(winbufnr(2)))
159
160  wincmd q
161  call assert_equal(1, winnr('$'))
162  call assert_equal('Xa', bufname(winbufnr(1)))
163
164  bw Xa Xb
165endfunc
166
167func Test_window_horizontal_split()
168  call assert_equal(1, winnr('$'))
169  3wincmd s
170  call assert_equal(2, winnr('$'))
171  call assert_equal(3, winheight(0))
172  call assert_equal(winwidth(1), 2->winwidth())
173
174  call assert_fails('botright topleft wincmd s', 'E442:')
175  bw
176endfunc
177
178func Test_window_vertical_split()
179  call assert_equal(1, winnr('$'))
180  3wincmd v
181  call assert_equal(2, winnr('$'))
182  call assert_equal(3, winwidth(0))
183  call assert_equal(winheight(1), winheight(2))
184
185  call assert_fails('botright topleft wincmd v', 'E442:')
186  bw
187endfunc
188
189" Test the ":wincmd ^" and "<C-W>^" commands.
190func Test_window_split_edit_alternate()
191  " Test for failure when the alternate buffer/file no longer exists.
192  edit Xfoo | %bw
193  call assert_fails(':wincmd ^', 'E23:')
194
195  " Test for the expected behavior when we have two named buffers.
196  edit Xfoo | edit Xbar
197  wincmd ^
198  call assert_equal('Xfoo', bufname(winbufnr(1)))
199  call assert_equal('Xbar', bufname(winbufnr(2)))
200  only
201
202  " Test for the expected behavior when the alternate buffer is not named.
203  enew | let l:nr1 = bufnr('%')
204  edit Xfoo | let l:nr2 = bufnr('%')
205  wincmd ^
206  call assert_equal(l:nr1, winbufnr(1))
207  call assert_equal(l:nr2, winbufnr(2))
208  only
209
210  " FIXME: this currently fails on AppVeyor, but passes locally
211  if !has('win32')
212    " Test the Normal mode command.
213    call feedkeys("\<C-W>\<C-^>", 'tx')
214    call assert_equal(l:nr2, winbufnr(1))
215    call assert_equal(l:nr1, winbufnr(2))
216  endif
217
218  %bw!
219endfunc
220
221" Test the ":[count]wincmd ^" and "[count]<C-W>^" commands.
222func Test_window_split_edit_bufnr()
223  %bwipeout
224  let l:nr = bufnr('%') + 1
225  call assert_fails(':execute "normal! ' . l:nr . '\<C-W>\<C-^>"', 'E92:')
226  call assert_fails(':' . l:nr . 'wincmd ^', 'E16:')
227  call assert_fails(':0wincmd ^', 'E16:')
228
229  edit Xfoo | edit Xbar | edit Xbaz
230  let l:foo_nr = bufnr('Xfoo')
231  let l:bar_nr = bufnr('Xbar')
232  let l:baz_nr = bufnr('Xbaz')
233
234  " FIXME: this currently fails on AppVeyor, but passes locally
235  if !has('win32')
236    call feedkeys(l:foo_nr . "\<C-W>\<C-^>", 'tx')
237    call assert_equal('Xfoo', bufname(winbufnr(1)))
238    call assert_equal('Xbaz', bufname(winbufnr(2)))
239    only
240
241    call feedkeys(l:bar_nr . "\<C-W>\<C-^>", 'tx')
242    call assert_equal('Xbar', bufname(winbufnr(1)))
243    call assert_equal('Xfoo', bufname(winbufnr(2)))
244    only
245
246    execute l:baz_nr . 'wincmd ^'
247    call assert_equal('Xbaz', bufname(winbufnr(1)))
248    call assert_equal('Xbar', bufname(winbufnr(2)))
249  endif
250
251  %bw!
252endfunc
253
254func s:win_layout_info(tp = tabpagenr()) abort
255  return #{
256        \ layout: winlayout(a:tp),
257        \ pos_sizes: range(1, tabpagewinnr(a:tp, '$'))
258        \            ->map({_, nr -> win_getid(nr, a:tp)->getwininfo()[0]})
259        \            ->map({_, wininfo -> #{id: wininfo.winid,
260        \                                   row: wininfo.winrow,
261        \                                   col: wininfo.wincol,
262        \                                   width: wininfo.width,
263        \                                   height: wininfo.height}})
264        \            ->sort({a, b -> a.id - b.id})
265        \ }
266endfunc
267
268func Test_window_split_no_room()
269  " N horizontal windows need >= 2*N + 1 lines:
270  " - 1 line + 1 status line in each window
271  " - 1 Ex command line
272  "
273  " 2*N + 1 <= &lines
274  " N <= (lines - 1)/2
275  "
276  " Beyond that number of windows, E36: Not enough room is expected.
277  let hor_win_count = (&lines - 1)/2
278  let hor_split_count = hor_win_count - 1
279  for s in range(1, hor_split_count) | split | endfor
280  call assert_fails('split', 'E36:')
281
282  botright vsplit
283  wincmd |
284  let info = s:win_layout_info()
285  call assert_fails('wincmd J', 'E36:')
286  call assert_fails('wincmd K', 'E36:')
287  call assert_equal(info, s:win_layout_info())
288  only
289
290  " N vertical windows need >= 2*(N - 1) + 1 columns:
291  " - 1 column + 1 separator for each window (except last window)
292  " - 1 column for the last window which does not have separator
293  "
294  " 2*(N - 1) + 1 <= &columns
295  " 2*N - 1 <= &columns
296  " N <= (&columns + 1)/2
297  let ver_win_count = (&columns + 1)/2
298  let ver_split_count = ver_win_count - 1
299  for s in range(1, ver_split_count) | vsplit | endfor
300  call assert_fails('vsplit', 'E36:')
301
302  split
303  wincmd |
304  let info = s:win_layout_info()
305  call assert_fails('wincmd H', 'E36:')
306  call assert_fails('wincmd L', 'E36:')
307  call assert_equal(info, s:win_layout_info())
308
309  " Check that the last statusline isn't lost.
310  " Set its window's width to 2 for the test.
311  wincmd j
312  set laststatus=0 winminwidth=0
313  vertical resize 2
314  " Update expected positions/sizes after the resize.  Layout is unchanged.
315  let info.pos_sizes = s:win_layout_info().pos_sizes
316  set winminwidth&
317  call setwinvar(winnr('k'), '&statusline', '@#')
318  let last_stl_row = win_screenpos(0)[0] - 1
319  redraw
320  call assert_equal('@#|', GetScreenStr(last_stl_row))
321  call assert_equal('~ |', GetScreenStr(&lines - &cmdheight))
322
323  call assert_fails('wincmd H', 'E36:')
324  call assert_fails('wincmd L', 'E36:')
325  call assert_equal(info, s:win_layout_info())
326  call setwinvar(winnr('k'), '&statusline', '=-')
327  redraw
328  call assert_equal('=-|', GetScreenStr(last_stl_row))
329  call assert_equal('~ |', GetScreenStr(&lines - &cmdheight))
330
331  %bw!
332  set laststatus&
333endfunc
334
335func Test_window_exchange()
336  e Xa
337
338  " Nothing happens with window exchange when there is 1 window
339  wincmd x
340  call assert_equal(1, winnr('$'))
341
342  split Xb
343  split Xc
344
345  call assert_equal('Xc', bufname(winbufnr(1)))
346  call assert_equal('Xb', bufname(winbufnr(2)))
347  call assert_equal('Xa', bufname(winbufnr(3)))
348
349  " Exchange current window 1 with window 3
350  3wincmd x
351  call assert_equal('Xa', bufname(winbufnr(1)))
352  call assert_equal('Xb', bufname(winbufnr(2)))
353  call assert_equal('Xc', bufname(winbufnr(3)))
354
355  " Exchange window with next when at the top window
356  wincmd x
357  call assert_equal('Xb', bufname(winbufnr(1)))
358  call assert_equal('Xa', bufname(winbufnr(2)))
359  call assert_equal('Xc', bufname(winbufnr(3)))
360
361  " Exchange window with next when at the middle window
362  wincmd j
363  wincmd x
364  call assert_equal('Xb', bufname(winbufnr(1)))
365  call assert_equal('Xc', bufname(winbufnr(2)))
366  call assert_equal('Xa', bufname(winbufnr(3)))
367
368  " Exchange window with next when at the bottom window.
369  " When there is no next window, it exchanges with the previous window.
370  wincmd j
371  wincmd x
372  call assert_equal('Xb', bufname(winbufnr(1)))
373  call assert_equal('Xa', bufname(winbufnr(2)))
374  call assert_equal('Xc', bufname(winbufnr(3)))
375
376  bw Xa Xb Xc
377endfunc
378
379func Test_window_rotate()
380  e Xa
381  split Xb
382  split Xc
383  call assert_equal('Xc', bufname(winbufnr(1)))
384  call assert_equal('Xb', bufname(winbufnr(2)))
385  call assert_equal('Xa', bufname(winbufnr(3)))
386
387  " Rotate downwards
388  wincmd r
389  call assert_equal('Xa', bufname(winbufnr(1)))
390  call assert_equal('Xc', bufname(winbufnr(2)))
391  call assert_equal('Xb', bufname(winbufnr(3)))
392
393  2wincmd r
394  call assert_equal('Xc', bufname(winbufnr(1)))
395  call assert_equal('Xb', bufname(winbufnr(2)))
396  call assert_equal('Xa', bufname(winbufnr(3)))
397
398  " Rotate upwards
399  wincmd R
400  call assert_equal('Xb', bufname(winbufnr(1)))
401  call assert_equal('Xa', bufname(winbufnr(2)))
402  call assert_equal('Xc', bufname(winbufnr(3)))
403
404  2wincmd R
405  call assert_equal('Xc', bufname(winbufnr(1)))
406  call assert_equal('Xb', bufname(winbufnr(2)))
407  call assert_equal('Xa', bufname(winbufnr(3)))
408
409  bot vsplit
410  call assert_fails('wincmd R', 'E443:')
411
412  bw Xa Xb Xc
413endfunc
414
415func Test_window_height()
416  e Xa
417  split Xb
418
419  let [wh1, wh2] = [winheight(1), winheight(2)]
420  " Active window (1) should have the same height or 1 more
421  " than the other window.
422  call assert_inrange(wh2, wh2 + 1, wh1)
423
424  wincmd -
425  call assert_equal(wh1 - 1, winheight(1))
426  call assert_equal(wh2 + 1, winheight(2))
427
428  wincmd +
429  call assert_equal(wh1, winheight(1))
430  call assert_equal(wh2, 2->winheight())
431
432  2wincmd _
433  call assert_equal(2, winheight(1))
434  call assert_equal(wh1 + wh2 - 2, winheight(2))
435
436  wincmd =
437  call assert_equal(wh1, winheight(1))
438  call assert_equal(wh2, winheight(2))
439
440  2wincmd _
441  set winfixheight
442  split Xc
443  let [wh1, wh2, wh3] = [winheight(1), winheight(2), winheight(3)]
444  call assert_equal(2, winheight(2))
445  call assert_inrange(wh3, wh3 + 1, wh1)
446  3wincmd +
447  call assert_equal(2,       winheight(2))
448  call assert_equal(wh1 + 3, winheight(1))
449  call assert_equal(wh3 - 3, winheight(3))
450  wincmd =
451  call assert_equal(2,   winheight(2))
452  call assert_equal(wh1, winheight(1))
453  call assert_equal(wh3, winheight(3))
454
455  wincmd j
456  set winfixheight&
457
458  wincmd =
459  let [wh1, wh2, wh3] = [winheight(1), winheight(2), winheight(3)]
460  " Current window (2) should have the same height or 1 more
461  " than the other windows.
462  call assert_inrange(wh1, wh1 + 1, wh2)
463  call assert_inrange(wh3, wh3 + 1, wh2)
464
465  bw Xa Xb Xc
466endfunc
467
468func Test_wincmd_equal()
469  edit Xone
470  below split Xtwo
471  rightbelow vsplit Xthree
472  call assert_equal('Xone', bufname(winbufnr(1)))
473  call assert_equal('Xtwo', bufname(winbufnr(2)))
474  call assert_equal('Xthree', bufname(winbufnr(3)))
475
476  " Xone and Xtwo should be about the same height
477  let [wh1, wh2] = [winheight(1), winheight(2)]
478  call assert_inrange(wh1 - 1, wh1 + 1, wh2)
479  " Xtwo and Xthree should be about the same width
480  let [ww2, ww3] = [winwidth(2), winwidth(3)]
481  call assert_inrange(ww2 - 1, ww2 + 1, ww3)
482
483  1wincmd w
484  10wincmd _
485  2wincmd w
486  20wincmd |
487  call assert_equal(10, winheight(1))
488  call assert_equal(20, winwidth(2))
489
490  " equalizing horizontally doesn't change the heights
491  hor wincmd =
492  call assert_equal(10, winheight(1))
493  let [ww2, ww3] = [winwidth(2), winwidth(3)]
494  call assert_inrange(ww2 - 1, ww2 + 1, ww3)
495
496  2wincmd w
497  20wincmd |
498  call assert_equal(20, winwidth(2))
499  " equalizing vertically doesn't change the widths
500  vert wincmd =
501  call assert_equal(20, winwidth(2))
502  let [wh1, wh2] = [winheight(1), winheight(2)]
503  call assert_inrange(wh1 - 1, wh1 + 1, wh2)
504
505  bwipe Xone Xtwo Xthree
506endfunc
507
508func Test_window_width()
509  e Xa
510  vsplit Xb
511
512  let [ww1, ww2] = [winwidth(1), winwidth(2)]
513  " Active window (1) should have the same width or 1 more
514  " than the other window.
515  call assert_inrange(ww2, ww2 + 1, ww1)
516
517  wincmd <
518  call assert_equal(ww1 - 1, winwidth(1))
519  call assert_equal(ww2 + 1, winwidth(2))
520
521  wincmd >
522  call assert_equal(ww1, winwidth(1))
523  call assert_equal(ww2, winwidth(2))
524
525  2wincmd |
526  call assert_equal(2, winwidth(1))
527  call assert_equal(ww1 + ww2 - 2, winwidth(2))
528
529  wincmd =
530  call assert_equal(ww1, winwidth(1))
531  call assert_equal(ww2, winwidth(2))
532
533  2wincmd |
534  set winfixwidth
535  vsplit Xc
536  let [ww1, ww2, ww3] = [winwidth(1), winwidth(2), winwidth(3)]
537  call assert_equal(2, winwidth(2))
538  call assert_inrange(ww3, ww3 + 1, ww1)
539  3wincmd >
540  call assert_equal(2,       winwidth(2))
541  call assert_equal(ww1 + 3, winwidth(1))
542  call assert_equal(ww3 - 3, winwidth(3))
543  wincmd =
544  call assert_equal(2,   winwidth(2))
545  call assert_equal(ww1, winwidth(1))
546  call assert_equal(ww3, winwidth(3))
547
548  wincmd l
549  set winfixwidth&
550
551  wincmd =
552  let [ww1, ww2, ww3] = [winwidth(1), winwidth(2), winwidth(3)]
553  " Current window (2) should have the same width or 1 more
554  " than the other windows.
555  call assert_inrange(ww1, ww1 + 1, ww2)
556  call assert_inrange(ww3, ww3 + 1, ww2)
557
558  " when the current window width is less than the new 'winwidth', the current
559  " window width should be increased.
560  enew | only
561  split
562  10vnew
563  set winwidth=15
564  call assert_equal(15, winwidth(0))
565
566  %bw!
567endfunc
568
569func Test_equalalways_on_close()
570  set equalalways
571  vsplit
572  windo split
573  split
574  wincmd J
575  " now we have a frame top-left with two windows, a frame top-right with two
576  " windows and a frame at the bottom, full-width.
577  let height_1 = winheight(1)
578  let height_2 = winheight(2)
579  let height_3 = winheight(3)
580  let height_4 = winheight(4)
581  " closing the bottom window causes all windows to be resized.
582  close
583  call assert_notequal(height_1, winheight(1))
584  call assert_notequal(height_2, winheight(2))
585  call assert_notequal(height_3, winheight(3))
586  call assert_notequal(height_4, winheight(4))
587  call assert_equal(winheight(1), winheight(3))
588  call assert_equal(winheight(2), winheight(4))
589
590  1wincmd w
591  split
592  4wincmd w
593  resize +5
594  " left column has three windows, equalized heights.
595  " right column has two windows, top one a bit higher
596  let height_1 = winheight(1)
597  let height_2 = winheight(2)
598  let height_4 = winheight(4)
599  let height_5 = winheight(5)
600  3wincmd w
601  " closing window in left column equalizes heights in left column but not in
602  " the right column
603  close
604  call assert_notequal(height_1, winheight(1))
605  call assert_notequal(height_2, winheight(2))
606  call assert_equal(height_4, winheight(3))
607  call assert_equal(height_5, winheight(4))
608
609  only
610  set equalalways&
611endfunc
612
613func Test_win_screenpos()
614  CheckFeature quickfix
615
616  call assert_equal(1, winnr('$'))
617  split
618  vsplit
619  10wincmd _
620  30wincmd |
621  call assert_equal([1, 1], win_screenpos(1))
622  call assert_equal([1, 32], win_screenpos(2))
623  call assert_equal([12, 1], win_screenpos(3))
624  call assert_equal([0, 0], win_screenpos(4))
625  call assert_fails('let l = win_screenpos([])', 'E745:')
626  only
627endfunc
628
629func Test_window_jump_tag()
630  CheckFeature quickfix
631
632  help
633  /Kuwasha
634  call assert_match('^- |Kuwasha|',  getline('.'))
635  call assert_equal(2, winnr('$'))
636  norm! fK
637  2wincmd }
638  call assert_equal(3, winnr('$'))
639  call assert_match('^- |Kuwasha|',  getline('.'))
640  wincmd k
641  call assert_match('\*Kuwasha\*',  getline('.'))
642  call assert_equal(2, winheight(0))
643
644  wincmd z
645  set previewheight=4
646  help
647  /bugs
648  norm! fb
649  wincmd }
650  wincmd k
651  call assert_match('\*bugs\*',  getline('.'))
652  call assert_equal(4, winheight(0))
653  set previewheight&
654
655  %bw!
656endfunc
657
658func Test_window_newtab()
659  e Xa
660
661  call assert_equal(1, tabpagenr('$'))
662  call assert_equal("\nAlready only one window", execute('wincmd T'))
663
664  split Xb
665  split Xc
666
667  wincmd T
668  call assert_equal(2, tabpagenr('$'))
669  call assert_equal(['Xb', 'Xa'], map(tabpagebuflist(1), 'bufname(v:val)'))
670  call assert_equal(['Xc'      ], map(2->tabpagebuflist(), 'bufname(v:val)'))
671  call assert_equal(['Xc'      ], map(tabpagebuflist(), 'bufname(v:val)'))
672
673  %bw!
674endfunc
675
676func Test_next_split_all()
677  " This was causing an illegal memory access.
678  n x
679  norm axxx
680  split
681  split
682  s/x
683  s/x
684  all
685  bwipe!
686endfunc
687
688" Tests for adjusting window and contents
689func GetScreenStr(row)
690   let str = ""
691   for c in range(1,3)
692       let str .= nr2char(screenchar(a:row, c))
693   endfor
694   return str
695endfunc
696
697func Test_window_contents()
698  enew! | only | new
699  call setline(1, range(1,256))
700
701  exe "norm! \<C-W>t\<C-W>=1Gzt\<C-W>w\<C-W>+"
702  redraw
703  let s3 = GetScreenStr(1)
704  wincmd p
705  call assert_equal(1, line("w0"))
706  call assert_equal('1  ', s3)
707
708  exe "norm! \<C-W>t\<C-W>=50Gzt\<C-W>w\<C-W>+"
709  redraw
710  let s3 = GetScreenStr(1)
711  wincmd p
712  call assert_equal(50, line("w0"))
713  call assert_equal('50 ', s3)
714
715  exe "norm! \<C-W>t\<C-W>=59Gzt\<C-W>w\<C-W>+"
716  redraw
717  let s3 = GetScreenStr(1)
718  wincmd p
719  call assert_equal(59, line("w0"))
720  call assert_equal('59 ', s3)
721
722  %d
723  call setline(1, ['one', 'two', 'three'])
724  call assert_equal(1, line('w0'))
725  call assert_equal(3, line('w$'))
726
727  bwipeout!
728  call test_garbagecollect_now()
729endfunc
730
731func Test_window_colon_command()
732  " This was reading invalid memory.
733  exe "norm! v\<C-W>:\<C-U>echo v:version"
734endfunc
735
736func Test_access_freed_mem()
737  call assert_equal(&columns, winwidth(0))
738  " This was accessing freed memory (but with what events?)
739  au BufEnter,BufLeave,WinEnter,WinLeave 0 vs xxx
740  arg 0
741  argadd
742  call assert_fails("all", "E242:")
743  au!
744  bwipe xxx
745  call assert_equal(&columns, winwidth(0))
746endfunc
747
748func Test_visual_cleared_after_window_split()
749  new | only!
750  let smd_save = &showmode
751  set showmode
752  let ls_save = &laststatus
753  set laststatus=1
754  call setline(1, ['a', 'b', 'c', 'd', ''])
755  norm! G
756  exe "norm! kkvk"
757  redraw
758  exe "norm! \<C-W>v"
759  redraw
760  " check if '-- VISUAL --' disappeared from command line
761  let columns = range(1, &columns)
762  let cmdlinechars = map(columns, 'nr2char(screenchar(&lines, v:val))')
763  let cmdline = join(cmdlinechars, '')
764  let cmdline_ltrim = substitute(cmdline, '^\s*', "", "")
765  let mode_shown = substitute(cmdline_ltrim, '\s*$', "", "")
766  call assert_equal('', mode_shown)
767  let &showmode = smd_save
768  let &laststatus = ls_save
769  bwipe!
770endfunc
771
772func Test_winrestcmd()
773  2split
774  3vsplit
775  let restcmd = winrestcmd()
776  call assert_equal(2, winheight(0))
777  call assert_equal(3, winwidth(0))
778  wincmd =
779  call assert_notequal(2, winheight(0))
780  call assert_notequal(3, winwidth(0))
781  exe restcmd
782  call assert_equal(2, winheight(0))
783  call assert_equal(3, winwidth(0))
784  only
785
786  wincmd v
787  wincmd s
788  wincmd v
789  redraw
790  let restcmd = winrestcmd()
791  wincmd _
792  wincmd |
793  exe restcmd
794  redraw
795  call assert_equal(restcmd, winrestcmd())
796
797  only
798endfunc
799
800func Fun_RenewFile()
801  " Need to wait a bit for the timestamp to be older.
802  let old_ftime = getftime("tmp.txt")
803  while getftime("tmp.txt") == old_ftime
804    sleep 100m
805    silent execute '!echo "1" > tmp.txt'
806  endwhile
807  sp
808  wincmd p
809  edit! tmp.txt
810endfunc
811
812func Test_window_prevwin()
813  " Can we make this work on MS-Windows?
814  CheckUnix
815
816  set hidden autoread
817  call writefile(['2'], 'tmp.txt', 'D')
818  new tmp.txt
819  q
820  call Fun_RenewFile()
821  call assert_equal(2, winnr())
822  wincmd p
823  call assert_equal(1, winnr())
824  wincmd p
825  q
826  call Fun_RenewFile()
827  call assert_equal(2, winnr())
828  wincmd p
829  call assert_equal(1, winnr())
830  wincmd p
831  " reset
832  q
833  set hidden&vim autoread&vim
834  delfunc Fun_RenewFile
835  bw!
836endfunc
837
838func Test_relative_cursor_position_in_one_line_window()
839  new
840  only
841  call setline(1, range(1, 10000))
842  normal 50%
843  let lnum = getcurpos()[1]
844  split
845  split
846  " make third window take as many lines as possible, other windows will
847  " become one line
848  3wincmd w
849  for i in range(1, &lines - 6)
850    wincmd +
851    redraw!
852  endfor
853
854  " first and second window should show cursor line
855  let wininfo = getwininfo()
856  call assert_equal(lnum, wininfo[0].topline)
857  call assert_equal(lnum, wininfo[1].topline)
858
859  only!
860  bwipe!
861  call assert_fails('call winrestview(v:_null_dict)', 'E1297:')
862endfunc
863
864func Test_relative_cursor_position_after_move_and_resize()
865  let so_save = &so
866  set so=0
867  enew
868  call setline(1, range(1, 10000))
869  normal 50%
870  split
871  1wincmd w
872  " Move cursor to first line in window
873  normal H
874  redraw!
875  " Reduce window height to two lines
876  let height = winheight(0)
877  while winheight(0) > 2
878    wincmd -
879    redraw!
880  endwhile
881  " move cursor to second/last line in window
882  normal j
883  " restore previous height
884  while winheight(0) < height
885    wincmd +
886    redraw!
887  endwhile
888  " make window two lines again
889  while winheight(0) > 2
890    wincmd -
891    redraw!
892  endwhile
893
894  " cursor should be at bottom line
895  let info = getwininfo(win_getid())[0]
896  call assert_equal(info.topline + 1, getcurpos()[1])
897
898  only!
899  bwipe!
900  let &so = so_save
901endfunc
902
903func Test_relative_cursor_position_after_resize()
904  let so_save = &so
905  set so=0
906  enew
907  call setline(1, range(1, 10000))
908  normal 50%
909  split
910  1wincmd w
911  let winid1 = win_getid()
912  let info = getwininfo(winid1)[0]
913  " Move cursor to second line in window
914  exe "normal " . (info.topline + 1) . "G"
915  redraw!
916  let lnum = getcurpos()[1]
917
918  " Make the window only two lines high, cursor should end up in top line
919  2wincmd w
920  exe (info.height - 2) . "wincmd +"
921  redraw!
922  let info = getwininfo(winid1)[0]
923  call assert_equal(lnum, info.topline)
924
925  only!
926  bwipe!
927  let &so = so_save
928endfunc
929
930func Test_relative_cursor_second_line_after_resize()
931  let so_save = &so
932  set so=0
933  enew
934  call setline(1, range(1, 10000))
935  normal 50%
936  split
937  1wincmd w
938  let winid1 = win_getid()
939  let info = getwininfo(winid1)[0]
940
941  " Make the window only two lines high
942  2wincmd _
943
944  " Move cursor to second line in window
945  normal H
946  normal j
947
948  " Make window size bigger, then back to 2 lines
949  for i in range(1, 10)
950    wincmd +
951    redraw!
952  endfor
953  for i in range(1, 10)
954    wincmd -
955    redraw!
956  endfor
957
958  " cursor should end up in bottom line
959  let info = getwininfo(winid1)[0]
960  call assert_equal(info.topline + 1, getcurpos()[1])
961
962  only!
963  bwipe!
964  let &so = so_save
965endfunc
966
967func Test_split_noscroll()
968  let so_save = &so
969  enew
970  call setline(1, range(1, 8))
971  normal 100%
972  split
973
974  1wincmd w
975  let winid1 = win_getid()
976  let info1 = getwininfo(winid1)[0]
977
978  2wincmd w
979  let winid2 = win_getid()
980  let info2 = getwininfo(winid2)[0]
981
982  call assert_equal(1, info1.topline)
983  call assert_equal(1, info2.topline)
984
985  " window that fits all lines by itself, but not when split: closing other
986  " window should restore fraction.
987  only!
988  call setline(1, range(1, &lines - 10))
989  exe &lines / 4
990  let winid1 = win_getid()
991  let info1 = getwininfo(winid1)[0]
992  call assert_equal(1, info1.topline)
993  new
994  redraw
995  close
996  let info1 = getwininfo(winid1)[0]
997  call assert_equal(1, info1.topline)
998
999  bwipe!
1000  let &so = so_save
1001endfunc
1002
1003" Tests for the winnr() function
1004func Test_winnr()
1005  only | tabonly
1006  call assert_equal(1, winnr('j'))
1007  call assert_equal(1, winnr('k'))
1008  call assert_equal(1, winnr('h'))
1009  call assert_equal(1, winnr('l'))
1010
1011  " create a set of horizontally and vertically split windows
1012  leftabove new | wincmd p
1013  leftabove new | wincmd p
1014  rightbelow new | wincmd p
1015  rightbelow new | wincmd p
1016  leftabove vnew | wincmd p
1017  leftabove vnew | wincmd p
1018  rightbelow vnew | wincmd p
1019  rightbelow vnew | wincmd p
1020
1021  call assert_equal(8, winnr('j'))
1022  call assert_equal(2, winnr('k'))
1023  call assert_equal(4, winnr('h'))
1024  call assert_equal(6, winnr('l'))
1025  call assert_equal(9, winnr('2j'))
1026  call assert_equal(1, winnr('2k'))
1027  call assert_equal(3, winnr('2h'))
1028  call assert_equal(7, winnr('2l'))
1029
1030  " Error cases
1031  call assert_fails("echo winnr('0.2k')", 'E15:')
1032  call assert_equal(2, winnr('-2k'))
1033  call assert_fails("echo winnr('-2xj')", 'E15:')
1034  call assert_fails("echo winnr('j2j')", 'E15:')
1035  call assert_fails("echo winnr('ll')", 'E15:')
1036  call assert_fails("echo winnr('5')", 'E15:')
1037  call assert_equal(4, winnr('0h'))
1038  call assert_fails("let w = winnr([])", 'E730:')
1039  call assert_equal('unknown', win_gettype(-1))
1040  call assert_equal(-1, winheight(-1))
1041  call assert_equal(-1, winwidth(-1))
1042
1043  tabnew
1044  call assert_equal(8, tabpagewinnr(1, 'j'))
1045  call assert_equal(2, 1->tabpagewinnr('k'))
1046  call assert_equal(4, tabpagewinnr(1, 'h'))
1047  call assert_equal(6, tabpagewinnr(1, 'l'))
1048
1049  only | tabonly
1050endfunc
1051
1052func Test_winrestview()
1053  split runtest.vim
1054  normal 50%
1055  let view = winsaveview()
1056  close
1057  split runtest.vim
1058  eval view->winrestview()
1059  call assert_equal(view, winsaveview())
1060
1061  bwipe!
1062  call assert_fails('call winrestview(v:_null_dict)', 'E1297:')
1063endfunc
1064
1065func Test_win_splitmove()
1066  CheckFeature quickfix
1067
1068  edit a
1069  leftabove split b
1070  leftabove vsplit c
1071  leftabove split d
1072
1073  " win_splitmove doesn't actually create or close any windows, so expect an
1074  " unchanged winid and no WinNew/WinClosed events, like :wincmd H/J/K/L.
1075  let s:triggered = []
1076  augroup WinSplitMove
1077    au!
1078    au WinNewPre * let s:triggered += ['WinNewPre']
1079    au WinNew * let s:triggered += ['WinNew', win_getid()]
1080    au WinClosed * let s:triggered += ['WinClosed', str2nr(expand('<afile>'))]
1081  augroup END
1082  let winid = win_getid()
1083
1084  call assert_equal(0, win_splitmove(winnr(), winnr('l')))
1085  call assert_equal(bufname(winbufnr(1)), 'c')
1086  call assert_equal(bufname(winbufnr(2)), 'd')
1087  call assert_equal(bufname(winbufnr(3)), 'b')
1088  call assert_equal(bufname(winbufnr(4)), 'a')
1089  call assert_equal(0, win_splitmove(winnr(), winnr('j'), {'vertical': 1}))
1090  call assert_equal(0, win_splitmove(winnr(), winnr('j'), {'vertical': 1}))
1091  call assert_equal(bufname(winbufnr(1)), 'c')
1092  call assert_equal(bufname(winbufnr(2)), 'b')
1093  call assert_equal(bufname(winbufnr(3)), 'd')
1094  call assert_equal(bufname(winbufnr(4)), 'a')
1095  call assert_equal(0, win_splitmove(winnr(), winnr('k'), {'vertical': 1}))
1096  call assert_equal(bufname(winbufnr(1)), 'd')
1097  call assert_equal(bufname(winbufnr(2)), 'c')
1098  call assert_equal(bufname(winbufnr(3)), 'b')
1099  call assert_equal(bufname(winbufnr(4)), 'a')
1100  call assert_equal(0, win_splitmove(winnr(), winnr('j'), {'rightbelow': v:true}))
1101  call assert_equal(bufname(winbufnr(1)), 'c')
1102  call assert_equal(bufname(winbufnr(2)), 'b')
1103  call assert_equal(bufname(winbufnr(3)), 'a')
1104  call assert_equal(bufname(winbufnr(4)), 'd')
1105  call assert_fails('call win_splitmove(winnr(), winnr("k"), v:_null_dict)', 'E1297:')
1106  call assert_equal([], s:triggered)
1107  call assert_equal(winid, win_getid())
1108
1109  unlet! s:triggered
1110  au! WinSplitMove
1111  only | bd
1112
1113  call assert_fails('call win_splitmove(winnr(), 123)', 'E957:')
1114  call assert_fails('call win_splitmove(123, winnr())', 'E957:')
1115  call assert_fails('call win_splitmove(winnr(), winnr())', 'E957:')
1116
1117  tabnew
1118  call assert_fails('call win_splitmove(1, win_getid(1, 1))', 'E957:')
1119  tabclose
1120
1121  split
1122  augroup WinSplitMove
1123    au!
1124    au WinEnter * ++once call win_gotoid(win_getid(winnr('#')))
1125  augroup END
1126  call assert_fails('call win_splitmove(winnr(), winnr("#"))', 'E855:')
1127
1128  augroup WinSplitMove
1129    au!
1130    au WinLeave * ++once quit
1131  augroup END
1132  call assert_fails('call win_splitmove(winnr(), winnr("#"))', 'E855:')
1133
1134  split
1135  split
1136  augroup WinSplitMove
1137    au!
1138    au WinEnter * ++once let s:triggered = v:true
1139          \| call assert_fails('call win_splitmove(winnr(), winnr("$"))', 'E242:')
1140          \| call assert_fails('call win_splitmove(winnr("$"), winnr())', 'E242:')
1141  augroup END
1142  quit
1143  call assert_equal(v:true, s:triggered)
1144  unlet! s:triggered
1145
1146  new
1147  augroup WinSplitMove
1148    au!
1149    au BufHidden * ++once let s:triggered = v:true
1150          \| call assert_fails('call win_splitmove(winnr(), winnr("#"))', 'E1159:')
1151  augroup END
1152  hide
1153  call assert_equal(v:true, s:triggered)
1154  unlet! s:triggered
1155
1156  split
1157  let close_win = winnr('#')
1158  augroup WinSplitMove
1159    au!
1160    au WinEnter * ++once quit!
1161  augroup END
1162  call win_splitmove(close_win, winnr())
1163  call assert_equal(0, win_id2win(close_win))
1164
1165  au! WinSplitMove
1166  augroup! WinSplitMove
1167  %bw!
1168endfunc
1169
1170" Test for the :only command
1171func Test_window_only()
1172  new
1173  set modified
1174  new
1175  call assert_fails('only', 'E445:')
1176  only!
1177  " Test for :only with a count
1178  let wid = win_getid()
1179  new
1180  new
1181  3only
1182  call assert_equal(1, winnr('$'))
1183  call assert_equal(wid, win_getid())
1184  call assert_fails('close', 'E444:')
1185  call assert_fails('%close', 'E16:')
1186endfunc
1187
1188" Test for errors with :wincmd
1189func Test_wincmd_errors()
1190  call assert_fails('wincmd g', 'E474:')
1191  call assert_fails('wincmd ab', 'E474:')
1192endfunc
1193
1194" Test for errors with :winpos
1195func Test_winpos_errors()
1196  throw 'Skipped: Nvim does not have :winpos'
1197  if !has("gui_running") && !has('win32')
1198    call assert_fails('winpos', 'E188:')
1199  endif
1200  call assert_fails('winpos 10', 'E466:')
1201endfunc
1202
1203" Test for +cmd in a :split command
1204func Test_split_cmd()
1205  split +set\ readonly
1206  call assert_equal(1, &readonly)
1207  call assert_equal(2, winnr('$'))
1208  close
1209endfunc
1210
1211" Create maximum number of horizontally or vertically split windows and then
1212" run commands that create a new horizontally/vertically split window
1213func Run_noroom_for_newwindow_test(dir_arg)
1214  let dir = (a:dir_arg == 'v') ? 'vert ' : ''
1215
1216  " Open as many windows as possible
1217  while v:true
1218    try
1219      exe dir . 'new'
1220    catch /E36:/
1221      break
1222    endtry
1223  endwhile
1224
1225  call writefile(['first', 'second', 'third'], 'Xnorfile1')
1226  call writefile([], 'Xnorfile2')
1227  call writefile([], 'Xnorfile3')
1228
1229  " Argument list related commands
1230  args Xnorfile1 Xnorfile2 Xnorfile3
1231  next
1232  for cmd in ['sargument 2', 'snext', 'sprevious', 'sNext', 'srewind',
1233			\ 'sfirst', 'slast']
1234    call assert_fails(dir .. cmd, 'E36:')
1235  endfor
1236  %argdelete
1237
1238  " Buffer related commands
1239  set modified
1240  hide enew
1241  for cmd in ['sbuffer Xnorfile1', 'sbnext', 'sbprevious', 'sbNext', 'sbrewind',
1242		\ 'sbfirst', 'sblast', 'sball', 'sbmodified', 'sunhide']
1243    call assert_fails(dir .. cmd, 'E36:')
1244  endfor
1245
1246  " Window related commands
1247  for cmd in ['split', 'split Xnorfile2', 'new', 'new Xnorfile3', 'sview Xnorfile1',
1248		\ 'sfind runtest.vim']
1249    call assert_fails(dir .. cmd, 'E36:')
1250  endfor
1251
1252  " Help
1253  call assert_fails(dir .. 'help', 'E36:')
1254  call assert_fails(dir .. 'helpgrep window', 'E36:')
1255
1256  " Command-line window
1257  if a:dir_arg == 'h'
1258    " Cmd-line window is always a horizontally split window
1259    call assert_beeps('call feedkeys("q:\<CR>", "xt")')
1260  endif
1261
1262  " Quickfix and location list window
1263  if has('quickfix')
1264    cexpr ''
1265    call assert_fails(dir .. 'copen', 'E36:')
1266    lexpr ''
1267    call assert_fails(dir .. 'lopen', 'E36:')
1268
1269    " Preview window
1270    call assert_fails(dir .. 'pedit Xnorfile2', 'E36:')
1271    call assert_fails(dir .. 'pbuffer', 'E36:')
1272    call setline(1, 'abc')
1273    call assert_fails(dir .. 'psearch abc', 'E36:')
1274  endif
1275
1276  " Window commands (CTRL-W ^ and CTRL-W f)
1277  if a:dir_arg == 'h'
1278    call assert_fails('call feedkeys("\<C-W>^", "xt")', 'E36:')
1279    call setline(1, 'Xnorfile1')
1280    call assert_fails('call feedkeys("gg\<C-W>f", "xt")', 'E36:')
1281  endif
1282  enew!
1283
1284  " Tag commands (:stag, :stselect and :stjump)
1285  call writefile(["!_TAG_FILE_ENCODING\tutf-8\t//",
1286        \ "second\tXnorfile1\t2",
1287        \ "third\tXnorfile1\t3",],
1288        \ 'Xtags')
1289  set tags=Xtags
1290  call assert_fails(dir .. 'stag second', 'E36:')
1291  call assert_fails('call feedkeys(":" .. dir .. "stselect second\n1\n", "xt")', 'E36:')
1292  call assert_fails(dir .. 'stjump second', 'E36:')
1293  call assert_fails(dir .. 'ptag second', 'E36:')
1294  set tags&
1295  call delete('Xtags')
1296
1297  " :isplit and :dsplit
1298  call setline(1, ['#define FOO 1', 'FOO'])
1299  normal 2G
1300  call assert_fails(dir .. 'isplit FOO', 'E36:')
1301  call assert_fails(dir .. 'dsplit FOO', 'E36:')
1302
1303  " terminal
1304  if has('terminal')
1305    call assert_fails(dir .. 'terminal', 'E36:')
1306  endif
1307
1308  %bwipe!
1309  call delete('Xnorfile1')
1310  call delete('Xnorfile2')
1311  call delete('Xnorfile3')
1312  only
1313endfunc
1314
1315func Test_split_cmds_with_no_room()
1316  throw "Skipped: Nvim supports cmdwin freedom #40312"
1317  call Run_noroom_for_newwindow_test('h')
1318  call Run_noroom_for_newwindow_test('v')
1319endfunc
1320
1321" Test for various wincmd failures
1322func Test_wincmd_fails()
1323  only!
1324  call assert_beeps("normal \<C-W>w")
1325  call assert_beeps("normal \<C-W>p")
1326  call assert_beeps("normal \<C-W>gk")
1327  call assert_beeps("normal \<C-W>r")
1328  call assert_beeps("normal \<C-W>K")
1329  call assert_beeps("normal \<C-W>H")
1330  call assert_beeps("normal \<C-W>2gt")
1331endfunc
1332
1333func Test_window_resize()
1334  " Vertical :resize (absolute, relative, min and max size).
1335  vsplit
1336  vert resize 8
1337  call assert_equal(8, winwidth(0))
1338  vert resize +2
1339  call assert_equal(10, winwidth(0))
1340  vert resize -2
1341  call assert_equal(8, winwidth(0))
1342  vert resize
1343  call assert_equal(&columns - 2, winwidth(0))
1344  vert resize 0
1345  call assert_equal(1, winwidth(0))
1346  vert resize 99999
1347  call assert_equal(&columns - 2, winwidth(0))
1348
1349  %bwipe!
1350
1351  " Horizontal :resize (with absolute, relative size, min and max size).
1352  split
1353  resize 8
1354  call assert_equal(8, winheight(0))
1355  resize +2
1356  call assert_equal(10, winheight(0))
1357  resize -2
1358  call assert_equal(8, winheight(0))
1359  resize
1360  call assert_equal(&lines - 4, winheight(0))
1361  resize 0
1362  call assert_equal(1, winheight(0))
1363  resize 99999
1364  call assert_equal(&lines - 4, winheight(0))
1365
1366  " :resize with explicit window number.
1367  let other_winnr = winnr('j')
1368  exe other_winnr .. 'resize 10'
1369  call assert_equal(10, winheight(other_winnr))
1370  call assert_equal(&lines - 10 - 3, winheight(0))
1371  exe other_winnr .. 'resize +1'
1372  exe other_winnr .. 'resize +1'
1373  call assert_equal(12, winheight(other_winnr))
1374  call assert_equal(&lines - 10 - 3 -2, winheight(0))
1375  close
1376
1377  vsplit
1378  wincmd l
1379  let other_winnr = winnr('h')
1380  call assert_notequal(winnr(), other_winnr)
1381  exe 'vert ' .. other_winnr .. 'resize -' .. &columns
1382  call assert_equal(0, winwidth(other_winnr))
1383
1384  %bwipe!
1385endfunc
1386
1387" Test for adjusting the window width when a window is closed with some
1388" windows using 'winfixwidth'
1389func Test_window_width_adjust()
1390  only
1391  " Three vertical windows. Windows 1 and 2 have 'winfixwidth' set and close
1392  " window 2.
1393  wincmd v
1394  vert resize 10
1395  set winfixwidth
1396  wincmd v
1397  set winfixwidth
1398  wincmd c
1399  call assert_inrange(10, 12, winwidth(1))
1400  " Three vertical windows. Windows 2 and 3 have 'winfixwidth' set and close
1401  " window 3.
1402  only
1403  set winfixwidth
1404  wincmd v
1405  vert resize 10
1406  set winfixwidth
1407  wincmd v
1408  set nowinfixwidth
1409  wincmd b
1410  wincmd c
1411  call assert_inrange(10, 12, winwidth(2))
1412
1413  new | only
1414endfunc
1415
1416" Test for jumping to a vertical/horizontal neighbor window based on the
1417" current cursor position
1418func Test_window_goto_neighbor()
1419  %bw!
1420
1421  " Vertical window movement
1422
1423  " create the following window layout:
1424  "     +--+--+
1425  "     |w1|w3|
1426  "     +--+  |
1427  "     |w2|  |
1428  "     +--+--+
1429  "     |w4   |
1430  "     +-----+
1431  new
1432  vsplit
1433  split
1434  " vertically jump from w4
1435  wincmd b
1436  call setline(1, repeat(' ', &columns))
1437  call cursor(1, 1)
1438  wincmd k
1439  call assert_equal(2, winnr())
1440  wincmd b
1441  call cursor(1, &columns)
1442  redraw!
1443  wincmd k
1444  call assert_equal(3, winnr())
1445  %bw!
1446
1447  " create the following window layout:
1448  "     +--+--+--+
1449  "     |w1|w2|w3|
1450  "     +--+--+--+
1451  "     |w4      |
1452  "     +--------+
1453  new
1454  vsplit
1455  vsplit
1456  wincmd b
1457  call setline(1, repeat(' ', &columns))
1458  call cursor(1, 1)
1459  wincmd k
1460  call assert_equal(1, winnr())
1461  wincmd b
1462  call cursor(1, &columns / 2)
1463  redraw!
1464  wincmd k
1465  call assert_equal(2, winnr())
1466  wincmd b
1467  call cursor(1, &columns)
1468  redraw!
1469  wincmd k
1470  call assert_equal(3, winnr())
1471  %bw!
1472
1473  " Horizontal window movement
1474
1475  " create the following window layout:
1476  "     +--+--+--+
1477  "     |w1|w2|w4|
1478  "     +--+--+  |
1479  "     |w3   |  |
1480  "     +-----+--+
1481  vsplit
1482  split
1483  vsplit
1484  4wincmd l
1485  call setline(1, repeat([' '], &lines))
1486  call cursor(1, 1)
1487  redraw!
1488  wincmd h
1489  call assert_equal(2, winnr())
1490  4wincmd l
1491  call cursor(&lines, 1)
1492  redraw!
1493  wincmd h
1494  call assert_equal(3, winnr())
1495  %bw!
1496
1497  " create the following window layout:
1498  "     +--+--+
1499  "     |w1|w4|
1500  "     +--+  +
1501  "     |w2|  |
1502  "     +--+  +
1503  "     |w3|  |
1504  "     +--+--+
1505  vsplit
1506  split
1507  split
1508  wincmd l
1509  call setline(1, repeat([' '], &lines))
1510  call cursor(1, 1)
1511  redraw!
1512  wincmd h
1513  call assert_equal(1, winnr())
1514  wincmd l
1515  call cursor(&lines / 2, 1)
1516  redraw!
1517  wincmd h
1518  call assert_equal(2, winnr())
1519  wincmd l
1520  call cursor(&lines, 1)
1521  redraw!
1522  wincmd h
1523  call assert_equal(3, winnr())
1524  %bw!
1525endfunc
1526
1527" Test for an autocmd closing the destination window when jumping from one
1528" window to another.
1529func Test_close_dest_window()
1530  split
1531  edit Xfile
1532
1533  " Test for BufLeave
1534  augroup T1
1535    au!
1536    au BufLeave Xfile $wincmd c
1537  augroup END
1538  wincmd b
1539  call assert_equal(1, winnr('$'))
1540  call assert_equal('Xfile', @%)
1541  augroup T1
1542    au!
1543  augroup END
1544
1545  " Test for WinLeave
1546  new
1547  wincmd p
1548  augroup T1
1549    au!
1550    au WinLeave * 1wincmd c
1551  augroup END
1552  wincmd t
1553  call assert_equal(1, winnr('$'))
1554  call assert_equal('Xfile', @%)
1555  augroup T1
1556    au!
1557  augroup END
1558  augroup! T1
1559  %bw!
1560endfunc
1561
1562func Test_win_move_separator()
1563  edit a
1564  leftabove vsplit b
1565  let w = winwidth(0)
1566  " check win_move_separator from left window on left window
1567  call assert_equal(1, winnr())
1568  for offset in range(5)
1569    call assert_true(win_move_separator(0, offset))
1570    call assert_equal(w + offset, winwidth(0))
1571    call assert_true(0->win_move_separator(-offset))
1572    call assert_equal(w, winwidth(0))
1573  endfor
1574  " check win_move_separator from right window on left window number
1575  wincmd l
1576  call assert_notequal(1, winnr())
1577  for offset in range(5)
1578    call assert_true(1->win_move_separator(offset))
1579    call assert_equal(w + offset, winwidth(1))
1580    call assert_true(win_move_separator(1, -offset))
1581    call assert_equal(w, winwidth(1))
1582  endfor
1583  " check win_move_separator from right window on left window ID
1584  let id = win_getid(1)
1585  for offset in range(5)
1586    call assert_true(win_move_separator(id, offset))
1587    call assert_equal(w + offset, winwidth(id))
1588    call assert_true(id->win_move_separator(-offset))
1589    call assert_equal(w, winwidth(id))
1590  endfor
1591  " check win_move_separator from right window on right window is no-op
1592  let w0 = winwidth(0)
1593  call assert_true(win_move_separator(0, 1))
1594  call assert_equal(w0, winwidth(0))
1595  call assert_true(win_move_separator(0, -1))
1596  call assert_equal(w0, winwidth(0))
1597
1598  " check that win_move_separator doesn't error with offsets beyond moving
1599  " possibility
1600  call assert_true(win_move_separator(id, 5000))
1601  call assert_true(winwidth(id) > w)
1602  call assert_true(win_move_separator(id, -5000))
1603  call assert_true(winwidth(id) < w)
1604
1605  " check that win_move_separator returns false for an invalid window
1606  wincmd =
1607  let w = winwidth(0)
1608  call assert_false(win_move_separator(-1, 1))
1609  call assert_equal(w, winwidth(0))
1610
1611  " check that win_move_separator returns false for a floating window
1612  let id = nvim_open_win(
1613        \ 0, 0, #{relative: 'editor', row: 2, col: 2, width: 5, height: 3})
1614  let w = winwidth(id)
1615  call assert_false(win_move_separator(id, 1))
1616  call assert_equal(w, winwidth(id))
1617  call nvim_win_close(id, 1)
1618
1619  " check that using another tabpage fails without crash
1620  let id = win_getid()
1621  tabnew
1622  call assert_fails('call win_move_separator(id, -1)', 'E1308:')
1623  tabclose
1624
1625  %bwipe!
1626endfunc
1627
1628func Test_win_move_statusline()
1629  edit a
1630  leftabove split b
1631  let h = winheight(0)
1632  " check win_move_statusline from top window on top window
1633  call assert_equal(1, winnr())
1634  for offset in range(5)
1635    call assert_true(win_move_statusline(0, offset))
1636    call assert_equal(h + offset, winheight(0))
1637    call assert_true(0->win_move_statusline(-offset))
1638    call assert_equal(h, winheight(0))
1639  endfor
1640  " check win_move_statusline from bottom window on top window number
1641  wincmd j
1642  call assert_notequal(1, winnr())
1643  for offset in range(5)
1644    call assert_true(1->win_move_statusline(offset))
1645    call assert_equal(h + offset, winheight(1))
1646    call assert_true(win_move_statusline(1, -offset))
1647    call assert_equal(h, winheight(1))
1648  endfor
1649  " check win_move_statusline from bottom window on bottom window
1650  let h0 = winheight(0)
1651  for offset in range(5)
1652    call assert_true(0->win_move_statusline(-offset))
1653    call assert_equal(h0 - offset, winheight(0))
1654    call assert_equal(1 + offset, &cmdheight)
1655    call assert_true(win_move_statusline(0, offset))
1656    call assert_equal(h0, winheight(0))
1657    call assert_equal(1, &cmdheight)
1658  endfor
1659  " supports cmdheight=0
1660  set cmdheight=0
1661  call assert_true(win_move_statusline(0, 1))
1662  call assert_equal(h0 + 1, winheight(0))
1663  call assert_equal(0, &cmdheight)
1664  set cmdheight&
1665  " check win_move_statusline from bottom window on top window ID
1666  let id = win_getid(1)
1667  for offset in range(5)
1668    call assert_true(win_move_statusline(id, offset))
1669    call assert_equal(h + offset, winheight(id))
1670    call assert_true(id->win_move_statusline(-offset))
1671    call assert_equal(h, winheight(id))
1672  endfor
1673
1674  " check that win_move_statusline doesn't error with offsets beyond moving
1675  " possibility
1676  call assert_true(win_move_statusline(id, 5000))
1677  call assert_true(winheight(id) > h)
1678  call assert_true(win_move_statusline(id, -5000))
1679  call assert_true(winheight(id) < h)
1680
1681  " check that win_move_statusline returns false for an invalid window
1682  wincmd =
1683  let h = winheight(0)
1684  call assert_false(win_move_statusline(-1, 1))
1685  call assert_equal(h, winheight(0))
1686
1687  " check that win_move_statusline returns false for a floating window
1688  let id = nvim_open_win(
1689        \ 0, 0, #{relative: 'editor', row: 2, col: 2, width: 5, height: 3})
1690  let h = winheight(id)
1691  call assert_false(win_move_statusline(id, 1))
1692  call assert_equal(h, winheight(id))
1693  call nvim_win_close(id, 1)
1694
1695  " check that using another tabpage fails without crash
1696  let id = win_getid()
1697  tabnew
1698  call assert_fails('call win_move_statusline(id, -1)', 'E1308:')
1699  tabclose
1700
1701  %bwipe!
1702endfunc
1703
1704" Test for window allocation failure
1705func Test_window_alloc_failure()
1706  CheckFunction test_alloc_fail
1707  %bw!
1708
1709  " test for creating a new window above current window
1710  call test_alloc_fail(GetAllocId('newwin_wvars'), 0, 0)
1711  call assert_fails('above new', 'E342:')
1712  call assert_equal(1, winnr('$'))
1713
1714  " test for creating a new window below current window
1715  call test_alloc_fail(GetAllocId('newwin_wvars'), 0, 0)
1716  call assert_fails('below new', 'E342:')
1717  call assert_equal(1, winnr('$'))
1718
1719  " test for popup window creation failure
1720  call test_alloc_fail(GetAllocId('newwin_wvars'), 0, 0)
1721  call assert_fails('call popup_create("Hello", {})', 'E342:')
1722  call assert_equal([], popup_list())
1723
1724  call test_alloc_fail(GetAllocId('newwin_wvars'), 0, 0)
1725  call assert_fails('split', 'E342:')
1726  call assert_equal(1, winnr('$'))
1727
1728  edit Xfile1
1729  edit Xfile2
1730  call test_alloc_fail(GetAllocId('newwin_wvars'), 0, 0)
1731  call assert_fails('sb Xfile1', 'E342:')
1732  call assert_equal(1, winnr('$'))
1733  call assert_equal('Xfile2', @%)
1734  %bw!
1735
1736  " FIXME: The following test crashes Vim
1737  " test for new tabpage creation failure
1738  " call test_alloc_fail(GetAllocId('newwin_wvars'), 0, 0)
1739  " call assert_fails('tabnew', 'E342:')
1740  " call assert_equal(1, tabpagenr('$'))
1741  " call assert_equal(1, winnr('$'))
1742
1743  " This test messes up the internal Vim window/frame information. So the
1744  " Test_window_cmd_cmdwin_with_vsp() test fails after running this test.
1745  " Open a new tab and close everything else to fix this issue.
1746  tabnew
1747  tabonly
1748endfunc
1749
1750func Test_win_equal_last_status()
1751  let save_lines = &lines
1752  set lines=20
1753  set splitbelow
1754  set laststatus=0
1755
1756  split | split | quit
1757  call assert_equal(winheight(1), winheight(2))
1758
1759  let &lines = save_lines
1760  set splitbelow&
1761  set laststatus&
1762endfunc
1763
1764" Test "screen" and "cursor" values for 'splitkeep' with a sequence of
1765" split operations for various options: with and without a winbar,
1766" tabline, for each possible value of 'laststatus', 'scrolloff',
1767" 'equalalways', and with the cursor at the top, middle and bottom.
1768func Test_splitkeep_options()
1769  throw "Skipped: Nvim supports cmdwin freedom #40312"
1770  " disallow window resizing
1771  " let save_WS = &t_WS
1772  " set t_WS=
1773
1774  let gui = has("gui_running")
1775  inoremap <expr> c "<cmd>copen<bar>wincmd k<CR>"
1776  for run in range(0, 20)
1777    let &splitkeep = run > 10 ? 'topline' : 'screen'
1778    let &scrolloff = (!(run % 4) ? 0 : run)
1779    let &laststatus = (run % 3)
1780    let &splitbelow = (run % 3)
1781    let &equalalways = (run % 2)
1782    " Nvim: both windows have a winbar after splitting
1783    " let wsb = (run % 2) && &splitbelow
1784    let wsb = 0
1785    let tl = (gui ? 0 : ((run % 5) ? 1 : 0))
1786    let pos = !(run % 3) ? 'H' : ((run % 2) ? 'M' : 'L')
1787    tabnew | tabonly! | redraw
1788    execute (run % 5) ? 'tabnew' : ''
1789    " execute (run % 2) ? 'nnoremenu 1.10 WinBar.Test :echo' : ''
1790    let &winbar = (run % 2) ? '%f' : ''
1791    call setline(1, range(1, 256))
1792    " No scroll for restore_snapshot
1793    norm G
1794    try
1795      copen | close | colder
1796    catch /E380/
1797    endtry
1798    call assert_equal(257 - winheight(0), line("w0"))
1799
1800    " No scroll for firstwin horizontal split
1801    execute 'norm gg' . pos
1802    split | redraw | wincmd k
1803    call assert_equal(1, line("w0"))
1804    call assert_equal(&scroll, winheight(0) / 2)
1805    wincmd j
1806    call assert_equal(&spk == 'topline' ? 1 : win_screenpos(0)[0] - tl - wsb, line("w0"))
1807
1808    " No scroll when resizing windows
1809    wincmd k | resize +2 | redraw
1810    call assert_equal(1, line("w0"))
1811    wincmd j
1812    call assert_equal(&spk == 'topline' ? 1 : win_screenpos(0)[0] - tl - wsb, line("w0"))
1813
1814    " No scroll when dragging statusline
1815    call win_move_statusline(1, -3)
1816    call assert_equal(&spk == 'topline' ? 1 : win_screenpos(0)[0] - tl - wsb, line("w0"))
1817    wincmd k
1818    call assert_equal(1, line("w0"))
1819
1820    " No scroll when changing shellsize
1821    set lines+=2
1822    call assert_equal(1, line("w0"))
1823    wincmd j
1824    call assert_equal(&spk == 'topline' ? 1 : win_screenpos(0)[0] - tl - wsb, line("w0"))
1825    set lines-=2
1826    call assert_equal(&spk == 'topline' ? 1 : win_screenpos(0)[0] - tl - wsb, line("w0"))
1827    wincmd k
1828    call assert_equal(1, line("w0"))
1829
1830    " No scroll when equalizing windows
1831    wincmd =
1832    call assert_equal(1, line("w0"))
1833    wincmd j
1834    call assert_equal(&spk == 'topline' ? 1 : win_screenpos(0)[0] - tl - wsb, line("w0"))
1835    wincmd k
1836    call assert_equal(1, line("w0"))
1837
1838    " No scroll in windows split multiple times
1839    vsplit | split | 4wincmd w
1840    call assert_equal(&spk == 'topline' ? 1 : win_screenpos(0)[0] - tl - wsb, line("w0"))
1841    1wincmd w | quit | wincmd l | split
1842    call assert_equal(&spk == 'topline' ? 1 : win_screenpos(0)[0] - tl - wsb, line("w0"))
1843    wincmd j
1844    call assert_equal(&spk == 'topline' ? 1 : win_screenpos(0)[0] - tl - wsb, line("w0"))
1845
1846    " No scroll in small window
1847    2wincmd w | only | 5split | wincmd k
1848    call assert_equal(1, line("w0"))
1849    wincmd j
1850    call assert_equal(&spk == 'topline' ? 1 : win_screenpos(0)[0] - tl - wsb, line("w0"))
1851
1852    " No scroll for vertical split
1853    quit | vsplit | wincmd l
1854    call assert_equal(1, line("w0"))
1855    wincmd h
1856    call assert_equal(1, line("w0"))
1857
1858    " No scroll in windows split and quit multiple times
1859    quit | redraw | split | split | quit | redraw
1860    call assert_equal(&spk == 'topline' ? 1 : win_screenpos(0)[0] - tl - wsb, line("w0"))
1861
1862    " No scroll for new buffer
1863    1wincmd w | only | copen | wincmd k
1864    call assert_equal(1, line("w0"))
1865    only
1866    call assert_equal(1, line("w0"))
1867    above copen | wincmd j
1868    call assert_equal(&spk == 'topline' ? 1 : win_screenpos(0)[0] - tl, line("w0"))
1869
1870    " No scroll when opening cmdwin, and no cursor move when closing cmdwin.
1871    only | norm ggL
1872    let curpos = getcurpos()
1873    norm q:
1874    call assert_equal(1, line("w0"))
1875    call assert_equal(curpos, getcurpos())
1876
1877    " Scroll when cursor becomes invalid in insert mode.
1878    norm Lic
1879    call assert_equal(curpos, getcurpos(), 'run ' .. run)
1880
1881    " No scroll when topline not equal to 1
1882    only | execute "norm gg5\<C-e>" | split | wincmd k
1883    call assert_equal(6, line("w0"))
1884    wincmd j
1885    call assert_equal(&spk == 'topline' ? 6 : 5 + win_screenpos(0)[0] - tl - wsb, line("w0"))
1886  endfor
1887
1888  tabnew | tabonly! | %bwipeout!
1889  iunmap c
1890  set scrolloff&
1891  set splitbelow&
1892  set laststatus&
1893  set equalalways&
1894  set splitkeep&
1895  " let &t_WS = save_WS
1896endfunc
1897
1898func Test_splitkeep_cmdwin_cursor_position()
1899  throw 'Skipped: Nvim supports cmdwin freedom #40312'
1900  set splitkeep=screen
1901  call setline(1, range(&lines))
1902
1903  " No scroll when cursor is at near bottom of window and cursor position
1904  " recompution (done by line('w0') in this test) happens while in cmdwin.
1905  normal! G
1906  let firstline = line('w0')
1907  autocmd CmdwinEnter * ++once autocmd WinEnter * ++once call line('w0')
1908  execute "normal! q:\<C-w>q"
1909  redraw!
1910  call assert_equal(firstline, line('w0'))
1911
1912  " User script can change cursor position successfully while in cmdwin and it
1913  " shouldn't be changed when closing cmdwin.
1914  execute "normal! Gq:\<Cmd>call win_execute(winnr('#')->win_getid(), 'call cursor(1, 1)')\<CR>\<C-w>q"
1915  call assert_equal(1, line('.'))
1916  call assert_equal(1, col('.'))
1917
1918  execute "normal! Gq:\<Cmd>autocmd WinEnter * ++once call cursor(1, 1)\<CR>\<C-w>q"
1919  call assert_equal(1, line('.'))
1920  call assert_equal(1, col('.'))
1921
1922  %bwipeout!
1923  set splitkeep&
1924endfunc
1925
1926func Test_splitkeep_misc()
1927  set splitkeep=screen
1928
1929  call setline(1, range(1, &lines))
1930  " Cursor is adjusted to start and end of buffer
1931  norm M
1932  wincmd s
1933  resize 1
1934  call assert_equal(1, line('.'))
1935  wincmd j
1936  norm GM
1937  resize 1
1938  call assert_equal(&lines, line('.'))
1939  only!
1940
1941  set splitbelow
1942  norm Gzz
1943  let top = line('w0')
1944  " No scroll when aucmd_win is opened
1945  call setbufvar(bufnr("test", 1) , '&buftype', 'nofile')
1946  call assert_equal(top, line('w0'))
1947  " No scroll when tab is changed/closed
1948  tab help | close
1949  call assert_equal(top, line('w0'))
1950  " No scroll when help is closed and buffer line count < window height
1951  norm ggdG
1952  call setline(1, range(1, &lines - 10))
1953  norm G
1954  let top = line('w0')
1955  help | quit
1956  call assert_equal(top, line('w0'))
1957  " No error when resizing window in autocmd and buffer length changed
1958  autocmd FileType qf exe "resize" line('$')
1959  cexpr getline(1, '$')
1960  copen
1961  wincmd p
1962  norm dd
1963  cexpr getline(1, '$')
1964
1965  %bwipeout!
1966  set splitbelow&
1967  set splitkeep&
1968endfunc
1969
1970func Test_splitkeep_screen_cursor_pos()
1971  new
1972  set splitkeep=screen
1973  call setline(1, ["longer than the last", "shorter"])
1974  norm! $
1975  wincmd s
1976  close
1977  call assert_equal([0, 1, 20, 0], getpos('.'))
1978  %bwipeout!
1979  set splitkeep&
1980endfunc
1981
1982func Test_splitkeep_phantom_jump()
1983  set splitbelow
1984  call setline(1, range(1, 100))
1985  split
1986
1987  " Move cursor to last visible line so it is at risk of being pushed off
1988  normal! L
1989  let old = getcurpos()
1990
1991  " No jumps, just 3 header lines
1992  call assert_equal(3, execute('jumps')->split('\n')->len())
1993
1994  " Switching to "screen" was causing vim to not update relevant variables
1995  set splitkeep=screen
1996
1997  " Reduce window 2's space, and so bump up the cursor
1998  set cmdheight=2
1999
2000  " Cursor has moved up a single line in the buffer, respecting splitkeep
2001  call assert_equal(4, execute('jumps')->split('\n')->len())
2002
2003  let old[1] = old[1] - 1
2004  call assert_equal(old, getcurpos())
2005
2006  %bwipeout!
2007  set splitbelow& splitkeep& cmdheight&
2008endfunc
2009
2010func Test_splitkeep_cmdheight()
2011  set splitkeep=screen
2012  call setline(1, range(&lines))
2013  norm! G
2014  set cmdheight=2
2015  call assert_equal(&lines - 1, line('.'))
2016  %bwipeout!
2017  set splitkeep& cmdheight&
2018endfunc
2019
2020func Test_aucmd_win_scroll_multibyte()
2021  " Using the autocommand window must not scroll the current window when the
2022  " cursor is behind multi-byte characters.
2023  set splitkeep=cursor
2024  " Use a window with a fixed size, the size of the screen may change while
2025  " the test is running.
2026  call NewWindow(11, 40)
2027  call setline(1, repeat([repeat(nr2char(0x3042), 100)], 20))
2028  normal! G050l
2029  redraw
2030  let topline = line('w0')
2031
2032  for i in range(3)
2033    call bufload(bufadd(''))
2034  endfor
2035  call assert_equal(topline, line('w0'))
2036
2037  %bwipeout!
2038  only!
2039  set splitkeep&
2040endfunc
2041
2042func Test_splitkeep_cursor()
2043  CheckScreendump
2044  let lines =<< trim END
2045    set splitkeep=screen
2046    autocmd CursorMoved * wincmd p | wincmd p
2047    call setline(1, range(1, 200))
2048    func CursorEqualize()
2049      call cursor(100, 1)
2050      wincmd =
2051    endfunc
2052    wincmd s
2053    call CursorEqualize()
2054  END
2055  call writefile(lines, 'XTestSplitkeepCallback', 'D')
2056  let buf = RunVimInTerminal('-S XTestSplitkeepCallback', #{rows: 8})
2057
2058  call VerifyScreenDump(buf, 'Test_splitkeep_cursor_1', {})
2059
2060  call term_sendkeys(buf, "j")
2061  call VerifyScreenDump(buf, 'Test_splitkeep_cursor_2', {})
2062
2063  call term_sendkeys(buf, ":set scrolloff=0\<CR>G")
2064  call VerifyScreenDump(buf, 'Test_splitkeep_cursor_3', {})
2065
2066  call StopVimInTerminal(buf)
2067endfunc
2068
2069func Test_splitkeep_callback()
2070  CheckScreendump
2071  let lines =<< trim END
2072    set splitkeep=screen
2073    call setline(1, range(&lines))
2074    function C1(a, b)
2075      split | wincmd p
2076    endfunction
2077    function C2(a, b)
2078      close | split
2079    endfunction
2080    nn j <cmd>call job_start([&sh, &shcf, "true"], { 'exit_cb': 'C1' })<CR>
2081    nn t <cmd>call popup_create(term_start([&sh, &shcf, "true"],
2082          \ { 'hidden': 1, 'exit_cb': 'C2' }), {})<CR>
2083  END
2084  call writefile(lines, 'XTestSplitkeepCallback', 'D')
2085  let buf = RunVimInTerminal('-S XTestSplitkeepCallback', #{rows: 8})
2086
2087  call term_sendkeys(buf, "j")
2088  call VerifyScreenDump(buf, 'Test_splitkeep_callback_1', {})
2089
2090  call term_sendkeys(buf, ":quit\<CR>Ht")
2091  call VerifyScreenDump(buf, 'Test_splitkeep_callback_2', {})
2092
2093  call term_sendkeys(buf, ":set sb\<CR>:quit\<CR>Gj")
2094  call VerifyScreenDump(buf, 'Test_splitkeep_callback_3', {})
2095
2096  call term_sendkeys(buf, ":quit\<CR>Gt")
2097  call VerifyScreenDump(buf, 'Test_splitkeep_callback_4', {})
2098
2099  call StopVimInTerminal(buf)
2100endfunc
2101
2102func Test_splitkeep_fold()
2103  CheckScreendump
2104
2105  let lines =<< trim END
2106    set splitkeep=screen
2107    set foldmethod=marker
2108    set number
2109    let line = 1
2110    for n in range(1, &lines)
2111      call setline(line, ['int FuncName() {/*{{{*/', 1, 2, 3, 4, 5, '}/*}}}*/',
2112            \ 'after fold'])
2113      let line += 8
2114    endfor
2115  END
2116  call writefile(lines, 'XTestSplitkeepFold', 'D')
2117  let buf = RunVimInTerminal('-S XTestSplitkeepFold', #{rows: 10})
2118
2119  call term_sendkeys(buf, "L:wincmd s\<CR>")
2120  call VerifyScreenDump(buf, 'Test_splitkeep_fold_1', {})
2121
2122  call term_sendkeys(buf, ":quit\<CR>")
2123  call VerifyScreenDump(buf, 'Test_splitkeep_fold_2', {})
2124
2125  call term_sendkeys(buf, "H:below split\<CR>")
2126  call VerifyScreenDump(buf, 'Test_splitkeep_fold_3', {})
2127
2128  call term_sendkeys(buf, ":wincmd k\<CR>:quit\<CR>")
2129  call VerifyScreenDump(buf, 'Test_splitkeep_fold_4', {})
2130
2131  call StopVimInTerminal(buf)
2132endfunc
2133
2134func Test_splitkeep_status()
2135  CheckScreendump
2136
2137  let lines =<< trim END
2138    call setline(1, ['a', 'b', 'c'])
2139    set nomodified
2140    set splitkeep=screen
2141    let win = winnr()
2142    wincmd s
2143    wincmd j
2144  END
2145  call writefile(lines, 'XTestSplitkeepStatus', 'D')
2146  let buf = RunVimInTerminal('-S XTestSplitkeepStatus', #{rows: 10})
2147
2148  call term_sendkeys(buf, ":call win_move_statusline(win, 1)\<CR>")
2149  call VerifyScreenDump(buf, 'Test_splitkeep_status_1', {})
2150
2151  call StopVimInTerminal(buf)
2152endfunc
2153
2154" skipcol is not reset unnecessarily and is copied to new window
2155func Test_splitkeep_skipcol()
2156  CheckScreendump
2157
2158  let lines =<< trim END
2159    set splitkeep=topline smoothscroll splitbelow scrolloff=0
2160    call setline(1, 'with lots of text in one line '->repeat(6))
2161    norm 2
2162    wincmd s
2163  END
2164
2165  call writefile(lines, 'XTestSplitkeepSkipcol', 'D')
2166  let buf = RunVimInTerminal('-S XTestSplitkeepSkipcol', #{rows: 12, cols: 40})
2167
2168  call VerifyScreenDump(buf, 'Test_splitkeep_skipcol_1', {})
2169  call StopVimInTerminal(buf)
2170endfunc
2171
2172func Test_splitkeep_line()
2173  CheckScreendump
2174
2175  let lines =<< trim END
2176    set splitkeep=screen nosplitbelow
2177    autocmd WinResized * call line('w0', 1000)
2178    call setline(1, range(1000))
2179  END
2180
2181  call writefile(lines, 'XTestSplitkeepSkipcol', 'D')
2182  let buf = RunVimInTerminal('-S XTestSplitkeepSkipcol', #{rows: 6, cols: 40})
2183
2184  call VerifyScreenDump(buf, 'Test_splitkeep_line_1', {})
2185
2186  call term_sendkeys(buf, ":wincmd s\<CR>")
2187  call VerifyScreenDump(buf, 'Test_splitkeep_line_2', {})
2188  call StopVimInTerminal(buf)
2189endfunc
2190
2191func Test_new_help_window_on_error()
2192  help change.txt
2193  execute "normal! /CTRL-@\<CR>"
2194  silent! execute "normal! \<C-W>]"
2195
2196  let wincount = winnr('$')
2197  help 'mod'
2198
2199  call assert_equal(wincount, winnr('$'))
2200  call assert_equal(expand("<cword>"), "'mod'")
2201endfunc
2202
2203func Test_splitmove_flatten_frame()
2204  split
2205  vsplit
2206
2207  wincmd L
2208  let layout = winlayout()
2209  wincmd K
2210  wincmd L
2211  call assert_equal(winlayout(), layout)
2212
2213  only!
2214endfunc
2215
2216func Test_autocmd_window_force_room()
2217  " Open as many windows as possible
2218  while v:true
2219    try
2220      split
2221    catch /E36:/
2222      break
2223    endtry
2224  endwhile
2225  while v:true
2226    try
2227      vsplit
2228    catch /E36:/
2229      break
2230    endtry
2231  endwhile
2232
2233  wincmd j
2234  vsplit
2235  call assert_fails('wincmd H', 'E36:')
2236  call assert_fails('wincmd J', 'E36:')
2237  call assert_fails('wincmd K', 'E36:')
2238  call assert_fails('wincmd L', 'E36:')
2239
2240  edit unload me
2241  enew
2242  bunload! unload\ me
2243  augroup AucmdWinForceRoom
2244    au!
2245    au BufEnter * ++once let s:triggered = v:true
2246                      \| call assert_equal('autocmd', win_gettype())
2247  augroup END
2248  let info = s:win_layout_info()
2249  " bufload opening the autocommand window shouldn't give E36.
2250  call bufload('unload me')
2251  call assert_equal(v:true, s:triggered)
2252  call assert_equal(info, s:win_layout_info())
2253
2254  unlet! s:triggered
2255  au! AucmdWinForceRoom
2256  augroup! AucmdWinForceRoom
2257  %bw!
2258endfunc
2259
2260func Test_win_gotoid_splitmove_textlock_cmdwin()
2261  throw 'Skipped: Nvim supports cmdwin freedom #40312'
2262  call setline(1, 'foo')
2263  new
2264  let curwin = win_getid()
2265  call setline(1, 'bar')
2266
2267  set debug+=throw indentexpr=win_gotoid(win_getid(winnr('#')))
2268  call assert_fails('normal! ==', 'E565:')
2269  call assert_equal(curwin, win_getid())
2270  " No error if attempting to switch to curwin; nothing happens.
2271  set indentexpr=assert_equal(1,win_gotoid(win_getid()))
2272  normal! ==
2273  call assert_equal(curwin, win_getid())
2274
2275  set indentexpr=win_splitmove(winnr('#'),winnr())
2276  call assert_fails('normal! ==', 'E565:')
2277  call assert_equal(curwin, win_getid())
2278
2279  %bw!
2280  set debug-=throw indentexpr&
2281
2282  call feedkeys('q:'
2283           \ .. ":call assert_fails('call win_splitmove(winnr(''#''), winnr())', 'E11:')\<CR>"
2284           \ .. ":call assert_equal('command', win_gettype())\<CR>"
2285           \ .. ":call assert_equal('', win_gettype(winnr('#')))\<CR>", 'ntx')
2286
2287  call feedkeys('q:'
2288           \ .. ":call assert_fails('call win_gotoid(win_getid(winnr(''#'')))', 'E11:')\<CR>"
2289           "\ No error if attempting to switch to curwin; nothing happens.
2290           \ .. ":call assert_equal(1, win_gotoid(win_getid()))\<CR>"
2291           \ .. ":call assert_equal('command', win_gettype())\<CR>"
2292           \ .. ":call assert_equal('', win_gettype(winnr('#')))\<CR>", 'ntx')
2293endfunc
2294
2295func Test_winfixsize_positions()
2296  " Check positions are correct when closing a window in a non-current tabpage
2297  " causes non-adjacent window to fill the space due to 'winfix{width,height}'.
2298  tabnew
2299  vsplit
2300  wincmd |
2301  split
2302  set winfixheight
2303  split foo
2304  tabfirst
2305
2306  bwipe! foo
2307  " Save actual values before entering the tabpage.
2308  let info = s:win_layout_info(2)
2309  tabnext
2310  " Compare it with the expected value (after win_comp_pos) from entering.
2311  call assert_equal(s:win_layout_info(), info)
2312
2313  $tabnew
2314  split
2315  split
2316  wincmd k
2317  belowright vsplit
2318  set winfixwidth
2319  belowright vsplit foo
2320  tabprevious
2321
2322  bwipe! foo
2323  " Save actual values before entering the tabpage.
2324  let info = s:win_layout_info(3)
2325  tabnext
2326  " Compare it with the expected value (after win_comp_pos) from entering.
2327  call assert_equal(s:win_layout_info(), info)
2328
2329  " Check positions unchanged when failing to move a window, if 'winfix{width,
2330  " height}' would otherwise cause a non-adjacent window to fill the space.
2331  %bwipe
2332  call assert_fails('execute "split|"->repeat(&lines)', 'E36:')
2333  wincmd p
2334  vsplit
2335  set winfixwidth
2336  vsplit
2337  set winfixwidth
2338  vsplit
2339  vsplit
2340  set winfixwidth
2341  wincmd p
2342
2343  let info = s:win_layout_info()
2344  call assert_fails('wincmd J', 'E36:')
2345  call assert_equal(info, s:win_layout_info())
2346
2347  only
2348  call assert_fails('execute "vsplit|"->repeat(&columns)', 'E36:')
2349  belowright split
2350  set winfixheight
2351  belowright split
2352
2353  let info = s:win_layout_info()
2354  call assert_fails('wincmd H', 'E36:')
2355  call assert_equal(info, s:win_layout_info())
2356
2357  %bwipe
2358endfunc
2359
2360func Test_winfixsize_vsep_statusline()
2361  CheckScreendump
2362
2363  let lines =<< trim END
2364    set noequalalways splitbelow splitright
2365    vsplit
2366    setlocal winfixwidth
2367    vsplit
2368    func SetupWfh()
2369      set laststatus=0
2370      only
2371      split
2372      set winfixheight
2373      split
2374    endfunc
2375  END
2376  call writefile(lines, 'XTestWinfixsizeVsepStatusline', 'D')
2377  let buf = RunVimInTerminal('-S XTestWinfixsizeVsepStatusline', #{rows: 8})
2378
2379  call term_sendkeys(buf, ":echo winwidth(1) winwidth(2) winwidth(3)\n")
2380  call WaitForAssert({-> assert_match('^16 37 20\>', term_getline(buf, 8))})
2381
2382  call term_sendkeys(buf, ":quit\n")
2383  call VerifyScreenDump(buf, 'Test_winfixsize_vsep_statusline_1', {})
2384
2385  " Reported widths should be consistent with the screen dump.
2386  call term_sendkeys(buf, ":echo winwidth(1) winwidth(2)\n")
2387  " (May be better if 'wfw' window remains at 37 columns, but the resize is
2388  " consistent with how things currently work for 'winfix*' windows)
2389  call WaitForAssert({-> assert_match('^36 38\>', term_getline(buf, 8))})
2390
2391  " For good measure, also check bottom-most 'winfixheight' windows don't leave
2392  " stray statuslines with &laststatus=0.
2393  call term_sendkeys(buf,
2394        \ ":call SetupWfh() | echo winheight(1) winheight(2) winheight(3)\n")
2395  call WaitForAssert({-> assert_match('^1 3 1\>', term_getline(buf, 8))})
2396
2397  call term_sendkeys(buf, ":quit\n")
2398  call VerifyScreenDump(buf, 'Test_winfixsize_vsep_statusline_2', {})
2399
2400  call term_sendkeys(buf, ":echo winheight(1) winheight(2)\n")
2401  " (Likewise, may be better if 'wfh' window remains at 3 rows)
2402  call WaitForAssert({-> assert_match('^2 4\>', term_getline(buf, 8))})
2403
2404  call StopVimInTerminal(buf)
2405endfunc
2406
2407func Test_resize_from_another_tabpage()
2408  CheckScreendump
2409
2410  let lines =<< trim END
2411    set laststatus=2
2412    vnew
2413    let w = win_getid()
2414    tabnew
2415    call win_execute(w, 'vertical resize 20')
2416    tabprev
2417  END
2418  call writefile(lines, 'XTestResizeFromAnotherTabpage', 'D')
2419  let buf = RunVimInTerminal('-S XTestResizeFromAnotherTabpage', #{rows: 8})
2420  call VerifyScreenDump(buf, 'Test_resize_from_another_tabpage_1', {})
2421
2422  call StopVimInTerminal(buf)
2423endfunc
2424
2425" When shrinking a window and the only other window has 'winfixheight'
2426" with 'winminheight'=0, freed rows must go to the wfh window.
2427func Test_winfixheight_resize_wmh_zero()
2428  set winminheight=0 laststatus=0
2429
2430  let id1 = win_getid()
2431  copen
2432  let id2 = win_getid()
2433  wincmd w
2434  wincmd _
2435  let wi1 = getwininfo(id1)[0]
2436  let wi2 = getwininfo(id2)[0]
2437  let exp = &lines - wi1.status_height - wi2.height - wi2.status_height - &ch
2438  call assert_equal([exp, 1], [wi1.height, wi1.status_height])
2439  call assert_equal([0, 0], [wi2.height, wi2.status_height])
2440
2441  wincmd w  " enter qf (height 0 -> 1)
2442  let wi1 = getwininfo(id1)[0]
2443  let wi2 = getwininfo(id2)[0]
2444  let exp = &lines - wi1.status_height - wi2.height - wi2.status_height - &ch
2445  call assert_equal(exp, wi1.height)
2446  call assert_equal(1, wi2.height)
2447
2448  wincmd w
2449  " Freed rows must go to qf (wfh, only other window).
2450  " Before the fix, the rows were lost (fr_height sum < topframe fr_height),
2451  " resulting in wrong window heights in subsequent operations.
2452  1wincmd _
2453  let wi1 = getwininfo(id1)[0]
2454  let wi2 = getwininfo(id2)[0]
2455  call assert_equal(1, wi1.height)
2456  let exp = &lines - wi1.height - wi1.status_height - &ch
2457  call assert_equal(exp, wi2.height)
2458
2459  wincmd w
2460  99wincmd +
2461  let wi1 = getwininfo(id1)[0]
2462  let wi2 = getwininfo(id2)[0]
2463  call assert_equal(0, wi1.height)
2464  let exp = &lines - wi1.height - wi1.status_height - &ch
2465  call assert_equal(exp, wi2.height)
2466
2467  99wincmd -
2468  let wi1 = getwininfo(id1)[0]
2469  let wi2 = getwininfo(id2)[0]
2470  let exp = &lines - wi1.status_height - wi2.height - &ch
2471  call assert_equal(exp, wi1.height)
2472  call assert_equal(1, wi2.height)
2473
2474  " The original bug caused a crash here, but could not be reproduced in the
2475  " test.  Kept as-is, though it has no particular significance.
2476  wincmd w
2477  call feedkeys("999i\<CR>\<Esc>", 'tx')
2478  call feedkeys("ggMi" .. repeat("\<CR>", 99) .. "\<Esc>", 'tx')
2479  let wi1 = getwininfo(id1)[0]
2480  let wi2 = getwininfo(id2)[0]
2481  let exp = &lines - wi1.status_height - wi2.height - &ch
2482  call assert_equal(exp, wi1.height)
2483  call assert_equal(1, wi2.height)
2484
2485  cclose
2486  set winminheight& laststatus&
2487endfunc
2488
2489" Splitting the only window while it has 'winfixheight' set and 'laststatus' is
2490" one must not leave a screen line unused.
2491func Test_winfixheight_split_only_window()
2492  set laststatus=1
2493  new
2494  only!
2495  setlocal winfixheight
2496  split
2497  " Two windows, both with a status line, and the command line.
2498  call assert_equal(&lines - &cmdheight - 2, winheight(1) + winheight(2))
2499
2500  only!
2501  setlocal winfixheight&
2502  set laststatus&
2503  bwipe!
2504endfunc
2505
2506func Test_window_w_locked_bypass()
2507  split Xfoo
2508  let s:win = win_getid()
2509
2510  augroup TestBypass
2511    " :quit fired this with w_locked set.  Shouldn't be able to unset w_locked
2512    " and close s:win if we do other stuff that also sets it.
2513    au WinLeave * ++once call assert_equal(s:win, win_getid())
2514                      \| quit | call assert_notequal(0, win_id2win(s:win))
2515                      \| args Xbar
2516                      \| argadd Xbaz
2517                      \| edit Xbaz-but-cooler
2518                      \| quit | call assert_notequal(0, win_id2win(s:win))
2519  augroup END
2520  quit
2521  call assert_equal(1, bufexists('Xbaz-but-cooler')) " check WinLeave ran
2522
2523  unlet! s:win
2524  augroup TestBypass
2525    au!
2526  augroup END
2527  %argd!
2528  %bw!
2529endfunc
2530
2531" vim: shiftwidth=2 sts=2 expandtab