1" Tests for window cmd (:wincmd, :split, :vsplit, :resize and etc...)
2
3so check.vim
4
5func Test_window_cmd_ls0_with_split()
6  set ls=0
7  set splitbelow
8  split
9  quit
10  call assert_equal(0, &lines - &cmdheight - winheight(0))
11  new | only!
12  "
13  set splitbelow&vim
14  botright split
15  quit
16  call assert_equal(0, &lines - &cmdheight - winheight(0))
17  new | only!
18  set ls&vim
19endfunc
20
21func Test_window_cmd_cmdwin_with_vsp()
22  let efmt = 'Expected 0 but got %d (in ls=%d, %s window)'
23  for v in range(0, 2)
24    exec "set ls=" . v
25    vsplit
26    call feedkeys("q:\<CR>")
27    let ac = &lines - (&cmdheight + winheight(0) + !!v)
28    let emsg = printf(efmt, ac, v, 'left')
29    call assert_equal(0, ac, emsg)
30    wincmd w
31    let ac = &lines - (&cmdheight + winheight(0) + !!v)
32    let emsg = printf(efmt, ac, v, 'right')
33    call assert_equal(0, ac, emsg)
34    new | only!
35  endfor
36  set ls&vim
37endfunc
38
39" Test for jumping to windows
40func Test_window_jump()
41  new
42  " jumping to a window with a count greater than the max windows
43  exe "normal 4\<C-W>w"
44  call assert_equal(2, winnr())
45  only
46endfunc
47
48func Test_window_cmd_wincmd_gf()
49  let fname = 'test_gf.txt'
50  let swp_fname = '.' . fname . '.swp'
51  call writefile([], fname)
52  call writefile([], swp_fname)
53  function s:swap_exists()
54    let v:swapchoice = s:swap_choice
55  endfunc
56  " Remove the catch-all that runtest.vim adds
57  au! SwapExists
58  augroup test_window_cmd_wincmd_gf
59    autocmd!
60    exec "autocmd SwapExists " . fname . " call s:swap_exists()"
61  augroup END
62
63  call setline(1, fname)
64  " (E)dit anyway
65  let s:swap_choice = 'e'
66  wincmd gf
67  call assert_equal(2, tabpagenr())
68  call assert_equal(fname, bufname("%"))
69  quit!
70
71  " (Q)uit
72  let s:swap_choice = 'q'
73  wincmd gf
74  call assert_equal(1, tabpagenr())
75  call assert_notequal(fname, bufname("%"))
76  new | only!
77
78  call delete(fname)
79  call delete(swp_fname)
80  augroup! test_window_cmd_wincmd_gf
81endfunc
82
83func Test_window_quit()
84  e Xa
85  split Xb
86  call assert_equal(2, '$'->winnr())
87  call assert_equal('Xb', bufname(winbufnr(1)))
88  call assert_equal('Xa', bufname(winbufnr(2)))
89
90  wincmd q
91  call assert_equal(1, winnr('$'))
92  call assert_equal('Xa', bufname(winbufnr(1)))
93
94  bw Xa Xb
95endfunc
96
97func Test_window_horizontal_split()
98  call assert_equal(1, winnr('$'))
99  3wincmd s
100  call assert_equal(2, winnr('$'))
101  call assert_equal(3, winheight(0))
102  call assert_equal(winwidth(1), 2->winwidth())
103
104  call assert_fails('botright topleft wincmd s', 'E442:')
105  bw
106endfunc
107
108func Test_window_vertical_split()
109  call assert_equal(1, winnr('$'))
110  3wincmd v
111  call assert_equal(2, winnr('$'))
112  call assert_equal(3, winwidth(0))
113  call assert_equal(winheight(1), winheight(2))
114
115  call assert_fails('botright topleft wincmd v', 'E442:')
116  bw
117endfunc
118
119" Test the ":wincmd ^" and "<C-W>^" commands.
120func Test_window_split_edit_alternate()
121
122  " Test for failure when the alternate buffer/file no longer exists.
123  edit Xfoo | %bw
124  call assert_fails(':wincmd ^', 'E23')
125
126  " Test for the expected behavior when we have two named buffers.
127  edit Xfoo | edit Xbar
128  wincmd ^
129  call assert_equal('Xfoo', bufname(winbufnr(1)))
130  call assert_equal('Xbar', bufname(winbufnr(2)))
131  only
132
133  " Test for the expected behavior when the alternate buffer is not named.
134  enew | let l:nr1 = bufnr('%')
135  edit Xfoo | let l:nr2 = bufnr('%')
136  wincmd ^
137  call assert_equal(l:nr1, winbufnr(1))
138  call assert_equal(l:nr2, winbufnr(2))
139  only
140
141  " FIXME: this currently fails on AppVeyor, but passes locally
142  if !has('win32')
143    " Test the Normal mode command.
144    call feedkeys("\<C-W>\<C-^>", 'tx')
145    call assert_equal(l:nr2, winbufnr(1))
146    call assert_equal(l:nr1, winbufnr(2))
147  endif
148
149  %bw!
150endfunc
151
152" Test the ":[count]wincmd ^" and "[count]<C-W>^" commands.
153func Test_window_split_edit_bufnr()
154
155  %bwipeout
156  let l:nr = bufnr('%') + 1
157  call assert_fails(':execute "normal! ' . l:nr . '\<C-W>\<C-^>"', 'E92')
158  call assert_fails(':' . l:nr . 'wincmd ^', 'E16')
159  call assert_fails(':0wincmd ^', 'E16')
160
161  edit Xfoo | edit Xbar | edit Xbaz
162  let l:foo_nr = bufnr('Xfoo')
163  let l:bar_nr = bufnr('Xbar')
164  let l:baz_nr = bufnr('Xbaz')
165
166  " FIXME: this currently fails on AppVeyor, but passes locally
167  if !has('win32')
168    call feedkeys(l:foo_nr . "\<C-W>\<C-^>", 'tx')
169    call assert_equal('Xfoo', bufname(winbufnr(1)))
170    call assert_equal('Xbaz', bufname(winbufnr(2)))
171    only
172
173    call feedkeys(l:bar_nr . "\<C-W>\<C-^>", 'tx')
174    call assert_equal('Xbar', bufname(winbufnr(1)))
175    call assert_equal('Xfoo', bufname(winbufnr(2)))
176    only
177
178    execute l:baz_nr . 'wincmd ^'
179    call assert_equal('Xbaz', bufname(winbufnr(1)))
180    call assert_equal('Xbar', bufname(winbufnr(2)))
181  endif
182
183  %bw!
184endfunc
185
186func Test_window_split_no_room()
187  " N horizontal windows need >= 2*N + 1 lines:
188  " - 1 line + 1 status line in each window
189  " - 1 Ex command line
190  "
191  " 2*N + 1 <= &lines
192  " N <= (lines - 1)/2
193  "
194  " Beyond that number of windows, E36: Not enough room is expected.
195  let hor_win_count = (&lines - 1)/2
196  let hor_split_count = hor_win_count - 1
197  for s in range(1, hor_split_count) | split | endfor
198  call assert_fails('split', 'E36:')
199
200  " N vertical windows need >= 2*(N - 1) + 1 columns:
201  " - 1 column + 1 separator for each window (except last window)
202  " - 1 column for the last window which does not have separator
203  "
204  " 2*(N - 1) + 1 <= &columns
205  " 2*N - 1 <= &columns
206  " N <= (&columns + 1)/2
207  let ver_win_count = (&columns + 1)/2
208  let ver_split_count = ver_win_count - 1
209  for s in range(1, ver_split_count) | vsplit | endfor
210  call assert_fails('vsplit', 'E36:')
211
212  %bw!
213endfunc
214
215func Test_window_exchange()
216  e Xa
217
218  " Nothing happens with window exchange when there is 1 window
219  wincmd x
220  call assert_equal(1, winnr('$'))
221
222  split Xb
223  split Xc
224
225  call assert_equal('Xc', bufname(winbufnr(1)))
226  call assert_equal('Xb', bufname(winbufnr(2)))
227  call assert_equal('Xa', bufname(winbufnr(3)))
228
229  " Exchange current window 1 with window 3
230  3wincmd x
231  call assert_equal('Xa', bufname(winbufnr(1)))
232  call assert_equal('Xb', bufname(winbufnr(2)))
233  call assert_equal('Xc', bufname(winbufnr(3)))
234
235  " Exchange window with next when at the top window
236  wincmd x
237  call assert_equal('Xb', bufname(winbufnr(1)))
238  call assert_equal('Xa', bufname(winbufnr(2)))
239  call assert_equal('Xc', bufname(winbufnr(3)))
240
241  " Exchange window with next when at the middle window
242  wincmd j
243  wincmd x
244  call assert_equal('Xb', bufname(winbufnr(1)))
245  call assert_equal('Xc', bufname(winbufnr(2)))
246  call assert_equal('Xa', bufname(winbufnr(3)))
247
248  " Exchange window with next when at the bottom window.
249  " When there is no next window, it exchanges with the previous window.
250  wincmd j
251  wincmd x
252  call assert_equal('Xb', bufname(winbufnr(1)))
253  call assert_equal('Xa', bufname(winbufnr(2)))
254  call assert_equal('Xc', bufname(winbufnr(3)))
255
256  bw Xa Xb Xc
257endfunc
258
259func Test_window_rotate()
260  e Xa
261  split Xb
262  split Xc
263  call assert_equal('Xc', bufname(winbufnr(1)))
264  call assert_equal('Xb', bufname(winbufnr(2)))
265  call assert_equal('Xa', bufname(winbufnr(3)))
266
267  " Rotate downwards
268  wincmd r
269  call assert_equal('Xa', bufname(winbufnr(1)))
270  call assert_equal('Xc', bufname(winbufnr(2)))
271  call assert_equal('Xb', bufname(winbufnr(3)))
272
273  2wincmd r
274  call assert_equal('Xc', bufname(winbufnr(1)))
275  call assert_equal('Xb', bufname(winbufnr(2)))
276  call assert_equal('Xa', bufname(winbufnr(3)))
277
278  " Rotate upwards
279  wincmd R
280  call assert_equal('Xb', bufname(winbufnr(1)))
281  call assert_equal('Xa', bufname(winbufnr(2)))
282  call assert_equal('Xc', bufname(winbufnr(3)))
283
284  2wincmd R
285  call assert_equal('Xc', bufname(winbufnr(1)))
286  call assert_equal('Xb', bufname(winbufnr(2)))
287  call assert_equal('Xa', bufname(winbufnr(3)))
288
289  bot vsplit
290  call assert_fails('wincmd R', 'E443:')
291
292  bw Xa Xb Xc
293endfunc
294
295func Test_window_height()
296  e Xa
297  split Xb
298
299  let [wh1, wh2] = [winheight(1), winheight(2)]
300  " Active window (1) should have the same height or 1 more
301  " than the other window.
302  call assert_inrange(wh2, wh2 + 1, wh1)
303
304  wincmd -
305  call assert_equal(wh1 - 1, winheight(1))
306  call assert_equal(wh2 + 1, winheight(2))
307
308  wincmd +
309  call assert_equal(wh1, winheight(1))
310  call assert_equal(wh2, 2->winheight())
311
312  2wincmd _
313  call assert_equal(2, winheight(1))
314  call assert_equal(wh1 + wh2 - 2, winheight(2))
315
316  wincmd =
317  call assert_equal(wh1, winheight(1))
318  call assert_equal(wh2, winheight(2))
319
320  2wincmd _
321  set winfixheight
322  split Xc
323  let [wh1, wh2, wh3] = [winheight(1), winheight(2), winheight(3)]
324  call assert_equal(2, winheight(2))
325  call assert_inrange(wh3, wh3 + 1, wh1)
326  3wincmd +
327  call assert_equal(2,       winheight(2))
328  call assert_equal(wh1 + 3, winheight(1))
329  call assert_equal(wh3 - 3, winheight(3))
330  wincmd =
331  call assert_equal(2,   winheight(2))
332  call assert_equal(wh1, winheight(1))
333  call assert_equal(wh3, winheight(3))
334
335  wincmd j
336  set winfixheight&
337
338  wincmd =
339  let [wh1, wh2, wh3] = [winheight(1), winheight(2), winheight(3)]
340  " Current window (2) should have the same height or 1 more
341  " than the other windows.
342  call assert_inrange(wh1, wh1 + 1, wh2)
343  call assert_inrange(wh3, wh3 + 1, wh2)
344
345  bw Xa Xb Xc
346endfunc
347
348func Test_window_width()
349  e Xa
350  vsplit Xb
351
352  let [ww1, ww2] = [winwidth(1), winwidth(2)]
353  " Active window (1) should have the same width or 1 more
354  " than the other window.
355  call assert_inrange(ww2, ww2 + 1, ww1)
356
357  wincmd <
358  call assert_equal(ww1 - 1, winwidth(1))
359  call assert_equal(ww2 + 1, winwidth(2))
360
361  wincmd >
362  call assert_equal(ww1, winwidth(1))
363  call assert_equal(ww2, winwidth(2))
364
365  2wincmd |
366  call assert_equal(2, winwidth(1))
367  call assert_equal(ww1 + ww2 - 2, winwidth(2))
368
369  wincmd =
370  call assert_equal(ww1, winwidth(1))
371  call assert_equal(ww2, winwidth(2))
372
373  2wincmd |
374  set winfixwidth
375  vsplit Xc
376  let [ww1, ww2, ww3] = [winwidth(1), winwidth(2), winwidth(3)]
377  call assert_equal(2, winwidth(2))
378  call assert_inrange(ww3, ww3 + 1, ww1)
379  3wincmd >
380  call assert_equal(2,       winwidth(2))
381  call assert_equal(ww1 + 3, winwidth(1))
382  call assert_equal(ww3 - 3, winwidth(3))
383  wincmd =
384  call assert_equal(2,   winwidth(2))
385  call assert_equal(ww1, winwidth(1))
386  call assert_equal(ww3, winwidth(3))
387
388  wincmd l
389  set winfixwidth&
390
391  wincmd =
392  let [ww1, ww2, ww3] = [winwidth(1), winwidth(2), winwidth(3)]
393  " Current window (2) should have the same width or 1 more
394  " than the other windows.
395  call assert_inrange(ww1, ww1 + 1, ww2)
396  call assert_inrange(ww3, ww3 + 1, ww2)
397
398  bw Xa Xb Xc
399endfunc
400
401func Test_equalalways_on_close()
402  set equalalways
403  vsplit
404  windo split
405  split
406  wincmd J
407  " now we have a frame top-left with two windows, a frame top-right with two
408  " windows and a frame at the bottom, full-width.
409  let height_1 = winheight(1)
410  let height_2 = winheight(2)
411  let height_3 = winheight(3)
412  let height_4 = winheight(4)
413  " closing the bottom window causes all windows to be resized.
414  close
415  call assert_notequal(height_1, winheight(1))
416  call assert_notequal(height_2, winheight(2))
417  call assert_notequal(height_3, winheight(3))
418  call assert_notequal(height_4, winheight(4))
419  call assert_equal(winheight(1), winheight(3))
420  call assert_equal(winheight(2), winheight(4))
421
422  1wincmd w
423  split
424  4wincmd w
425  resize + 5
426  " left column has three windows, equalized heights.
427  " right column has two windows, top one a bit higher
428  let height_1 = winheight(1)
429  let height_2 = winheight(2)
430  let height_4 = winheight(4)
431  let height_5 = winheight(5)
432  3wincmd w
433  " closing window in left column equalizes heights in left column but not in
434  " the right column
435  close
436  call assert_notequal(height_1, winheight(1))
437  call assert_notequal(height_2, winheight(2))
438  call assert_equal(height_4, winheight(3))
439  call assert_equal(height_5, winheight(4))
440
441  only
442  set equalalways&
443endfunc
444
445func Test_win_screenpos()
446  CheckFeature quickfix
447
448  call assert_equal(1, winnr('$'))
449  split
450  vsplit
451  10wincmd _
452  30wincmd |
453  call assert_equal([1, 1], win_screenpos(1))
454  call assert_equal([1, 32], win_screenpos(2))
455  call assert_equal([12, 1], win_screenpos(3))
456  call assert_equal([0, 0], win_screenpos(4))
457  call assert_fails('let l = win_screenpos([])', 'E745:')
458  only
459endfunc
460
461func Test_window_jump_tag()
462  CheckFeature quickfix
463
464  help
465  /iccf
466  call assert_match('^|iccf|',  getline('.'))
467  call assert_equal(2, winnr('$'))
468  2wincmd }
469  call assert_equal(3, winnr('$'))
470  call assert_match('^|iccf|',  getline('.'))
471  wincmd k
472  call assert_match('\*iccf\*',  getline('.'))
473  call assert_equal(2, winheight(0))
474
475  wincmd z
476  set previewheight=4
477  help
478  /bugs
479  wincmd }
480  wincmd k
481  call assert_match('\*bugs\*',  getline('.'))
482  call assert_equal(4, winheight(0))
483  set previewheight&
484
485  %bw!
486endfunc
487
488func Test_window_newtab()
489  e Xa
490
491  call assert_equal(1, tabpagenr('$'))
492  call assert_equal("\nAlready only one window", execute('wincmd T'))
493
494  split Xb
495  split Xc
496
497  wincmd T
498  call assert_equal(2, tabpagenr('$'))
499  call assert_equal(['Xb', 'Xa'], map(tabpagebuflist(1), 'bufname(v:val)'))
500  call assert_equal(['Xc'      ], map(2->tabpagebuflist(), 'bufname(v:val)'))
501  call assert_equal(['Xc'      ], map(tabpagebuflist(), 'bufname(v:val)'))
502
503  %bw!
504endfunc
505
506func Test_next_split_all()
507  " This was causing an illegal memory access.
508  n x
509  norm axxx
510  split
511  split
512  s/x
513  s/x
514  all
515  bwipe!
516endfunc
517
518" Tests for adjusting window and contents
519func GetScreenStr(row)
520   let str = ""
521   for c in range(1,3)
522       let str .= nr2char(screenchar(a:row, c))
523   endfor
524   return str
525endfunc
526
527func Test_window_contents()
528  enew! | only | new
529  call setline(1, range(1,256))
530
531  exe "norm! \<C-W>t\<C-W>=1Gzt\<C-W>w\<C-W>+"
532  redraw
533  let s3 = GetScreenStr(1)
534  wincmd p
535  call assert_equal(1, line("w0"))
536  call assert_equal('1  ', s3)
537
538  exe "norm! \<C-W>t\<C-W>=50Gzt\<C-W>w\<C-W>+"
539  redraw
540  let s3 = GetScreenStr(1)
541  wincmd p
542  call assert_equal(50, line("w0"))
543  call assert_equal('50 ', s3)
544
545  exe "norm! \<C-W>t\<C-W>=59Gzt\<C-W>w\<C-W>+"
546  redraw
547  let s3 = GetScreenStr(1)
548  wincmd p
549  call assert_equal(59, line("w0"))
550  call assert_equal('59 ', s3)
551
552  %d
553  call setline(1, ['one', 'two', 'three'])
554  call assert_equal(1, line('w0'))
555  call assert_equal(3, line('w$'))
556
557  bwipeout!
558  call test_garbagecollect_now()
559endfunc
560
561func Test_window_colon_command()
562  " This was reading invalid memory.
563  exe "norm! v\<C-W>:\<C-U>echo v:version"
564endfunc
565
566func Test_access_freed_mem()
567  call assert_equal(&columns, winwidth(0))
568  " This was accessing freed memory
569  au * 0 vs xxx
570  arg 0
571  argadd
572  call assert_fails("all", "E249:")
573  au!
574  bwipe xxx
575  call assert_equal(&columns, winwidth(0))
576endfunc
577
578func Test_insert_cleared_on_switch_to_term()
579  CheckFeature terminal
580
581  set showmode
582  terminal
583  wincmd p
584
585  call feedkeys("i\<C-O>", 'ntx')
586  redraw
587
588  " The "-- (insert) --" indicator should be visible.
589  let chars = map(range(1, &columns), 'nr2char(screenchar(&lines, v:val))')
590  let str = trim(join(chars, ''))
591  call assert_equal('-- (insert) --', str)
592
593  call feedkeys("\<C-W>p", 'ntx')
594  redraw
595
596  " The "-- (insert) --" indicator should have been cleared.
597  let chars = map(range(1, &columns), 'nr2char(screenchar(&lines, v:val))')
598  let str = trim(join(chars, ''))
599  call assert_equal('', str)
600
601  set showmode&
602  %bw!
603endfunc
604
605func Test_visual_cleared_after_window_split()
606  new | only!
607  let smd_save = &showmode
608  set showmode
609  let ls_save = &laststatus
610  set laststatus=1
611  call setline(1, ['a', 'b', 'c', 'd', ''])
612  norm! G
613  exe "norm! kkvk"
614  redraw
615  exe "norm! \<C-W>v"
616  redraw
617  " check if '-- VISUAL --' disappeared from command line
618  let columns = range(1, &columns)
619  let cmdlinechars = map(columns, 'nr2char(screenchar(&lines, v:val))')
620  let cmdline = join(cmdlinechars, '')
621  let cmdline_ltrim = substitute(cmdline, '^\s*', "", "")
622  let mode_shown = substitute(cmdline_ltrim, '\s*$', "", "")
623  call assert_equal('', mode_shown)
624  let &showmode = smd_save
625  let &laststatus = ls_save
626  bwipe!
627endfunc
628
629func Test_winrestcmd()
630  2split
631  3vsplit
632  let a = winrestcmd()
633  call assert_equal(2, winheight(0))
634  call assert_equal(3, winwidth(0))
635  wincmd =
636  call assert_notequal(2, winheight(0))
637  call assert_notequal(3, winwidth(0))
638  exe a
639  call assert_equal(2, winheight(0))
640  call assert_equal(3, winwidth(0))
641  only
642endfunc
643
644func Fun_RenewFile()
645  " Need to wait a bit for the timestamp to be older.
646  let old_ftime = getftime("tmp.txt")
647  while getftime("tmp.txt") == old_ftime
648    sleep 100m
649    silent execute '!echo "1" > tmp.txt'
650  endwhile
651  sp
652  wincmd p
653  edit! tmp.txt
654endfunc
655
656func Test_window_prevwin()
657  " Can we make this work on MS-Windows?
658  if !has('unix')
659    return
660  endif
661
662  set hidden autoread
663  call writefile(['2'], 'tmp.txt')
664  new tmp.txt
665  q
666  call Fun_RenewFile()
667  call assert_equal(2, winnr())
668  wincmd p
669  call assert_equal(1, winnr())
670  wincmd p
671  q
672  call Fun_RenewFile()
673  call assert_equal(2, winnr())
674  wincmd p
675  call assert_equal(1, winnr())
676  wincmd p
677  " reset
678  q
679  call delete('tmp.txt')
680  set hidden&vim autoread&vim
681  delfunc Fun_RenewFile
682endfunc
683
684func Test_relative_cursor_position_in_one_line_window()
685  new
686  only
687  call setline(1, range(1, 10000))
688  normal 50%
689  let lnum = getcurpos()[1]
690  split
691  split
692  " make third window take as many lines as possible, other windows will
693  " become one line
694  3wincmd w
695  for i in range(1, &lines - 6)
696    wincmd +
697    redraw!
698  endfor
699
700  " first and second window should show cursor line
701  let wininfo = getwininfo()
702  call assert_equal(lnum, wininfo[0].topline)
703  call assert_equal(lnum, wininfo[1].topline)
704
705  only!
706  bwipe!
707  call assert_fails('call winrestview(test_null_dict())', 'E474:')
708endfunc
709
710func Test_relative_cursor_position_after_move_and_resize()
711  let so_save = &so
712  set so=0
713  enew
714  call setline(1, range(1, 10000))
715  normal 50%
716  split
717  1wincmd w
718  " Move cursor to first line in window
719  normal H
720  redraw!
721  " Reduce window height to two lines
722  let height = winheight(0)
723  while winheight(0) > 2
724    wincmd -
725    redraw!
726  endwhile
727  " move cursor to second/last line in window
728  normal j
729  " restore previous height
730  while winheight(0) < height
731    wincmd +
732    redraw!
733  endwhile
734  " make window two lines again
735  while winheight(0) > 2
736    wincmd -
737    redraw!
738  endwhile
739
740  " cursor should be at bottom line
741  let info = getwininfo(win_getid())[0]
742  call assert_equal(info.topline + 1, getcurpos()[1])
743
744  only!
745  bwipe!
746  let &so = so_save
747endfunc
748
749func Test_relative_cursor_position_after_resize()
750  let so_save = &so
751  set so=0
752  enew
753  call setline(1, range(1, 10000))
754  normal 50%
755  split
756  1wincmd w
757  let winid1 = win_getid()
758  let info = getwininfo(winid1)[0]
759  " Move cursor to second line in window
760  exe "normal " . (info.topline + 1) . "G"
761  redraw!
762  let lnum = getcurpos()[1]
763
764  " Make the window only two lines high, cursor should end up in top line
765  2wincmd w
766  exe (info.height - 2) . "wincmd +"
767  redraw!
768  let info = getwininfo(winid1)[0]
769  call assert_equal(lnum, info.topline)
770
771  only!
772  bwipe!
773  let &so = so_save
774endfunc
775
776func Test_relative_cursor_second_line_after_resize()
777  let so_save = &so
778  set so=0
779  enew
780  call setline(1, range(1, 10000))
781  normal 50%
782  split
783  1wincmd w
784  let winid1 = win_getid()
785  let info = getwininfo(winid1)[0]
786
787  " Make the window only two lines high
788  2wincmd _
789
790  " Move cursor to second line in window
791  normal H
792  normal j
793
794  " Make window size bigger, then back to 2 lines
795  for i in range(1, 10)
796    wincmd +
797    redraw!
798  endfor
799  for i in range(1, 10)
800    wincmd -
801    redraw!
802  endfor
803
804  " cursor should end up in bottom line
805  let info = getwininfo(winid1)[0]
806  call assert_equal(info.topline + 1, getcurpos()[1])
807
808  only!
809  bwipe!
810  let &so = so_save
811endfunc
812
813func Test_split_noscroll()
814  let so_save = &so
815  enew
816  call setline(1, range(1, 8))
817  normal 100%
818  split
819
820  1wincmd w
821  let winid1 = win_getid()
822  let info1 = getwininfo(winid1)[0]
823
824  2wincmd w
825  let winid2 = win_getid()
826  let info2 = getwininfo(winid2)[0]
827
828  call assert_equal(1, info1.topline)
829  call assert_equal(1, info2.topline)
830
831  " window that fits all lines by itself, but not when split: closing other
832  " window should restore fraction.
833  only!
834  call setline(1, range(1, &lines - 10))
835  exe &lines / 4
836  let winid1 = win_getid()
837  let info1 = getwininfo(winid1)[0]
838  call assert_equal(1, info1.topline)
839  new
840  redraw
841  close
842  let info1 = getwininfo(winid1)[0]
843  call assert_equal(1, info1.topline)
844
845  bwipe!
846  let &so = so_save
847endfunc
848
849" Tests for the winnr() function
850func Test_winnr()
851  only | tabonly
852  call assert_equal(1, winnr('j'))
853  call assert_equal(1, winnr('k'))
854  call assert_equal(1, winnr('h'))
855  call assert_equal(1, winnr('l'))
856
857  " create a set of horizontally and vertically split windows
858  leftabove new | wincmd p
859  leftabove new | wincmd p
860  rightbelow new | wincmd p
861  rightbelow new | wincmd p
862  leftabove vnew | wincmd p
863  leftabove vnew | wincmd p
864  rightbelow vnew | wincmd p
865  rightbelow vnew | wincmd p
866
867  call assert_equal(8, winnr('j'))
868  call assert_equal(2, winnr('k'))
869  call assert_equal(4, winnr('h'))
870  call assert_equal(6, winnr('l'))
871  call assert_equal(9, winnr('2j'))
872  call assert_equal(1, winnr('2k'))
873  call assert_equal(3, winnr('2h'))
874  call assert_equal(7, winnr('2l'))
875
876  " Error cases
877  call assert_fails("echo winnr('0.2k')", 'E15:')
878  call assert_equal(2, winnr('-2k'))
879  call assert_fails("echo winnr('-2xj')", 'E15:')
880  call assert_fails("echo winnr('j2j')", 'E15:')
881  call assert_fails("echo winnr('ll')", 'E15:')
882  call assert_fails("echo winnr('5')", 'E15:')
883  call assert_equal(4, winnr('0h'))
884  call assert_fails("let w = winnr([])", 'E730:')
885  call assert_equal('unknown', win_gettype(-1))
886  call assert_equal(-1, winheight(-1))
887  call assert_equal(-1, winwidth(-1))
888
889  tabnew
890  call assert_equal(8, tabpagewinnr(1, 'j'))
891  call assert_equal(2, 1->tabpagewinnr('k'))
892  call assert_equal(4, tabpagewinnr(1, 'h'))
893  call assert_equal(6, tabpagewinnr(1, 'l'))
894
895  only | tabonly
896endfunc
897
898func Test_winrestview()
899  split runtest.vim
900  normal 50%
901  let view = winsaveview()
902  close
903  split runtest.vim
904  eval view->winrestview()
905  call assert_equal(view, winsaveview())
906
907  bwipe!
908  call assert_fails('call winrestview(test_null_dict())', 'E474:')
909endfunc
910
911func Test_win_splitmove()
912  CheckFeature quickfix
913
914  edit a
915  leftabove split b
916  leftabove vsplit c
917  leftabove split d
918  call assert_equal(0, win_splitmove(winnr(), winnr('l')))
919  call assert_equal(bufname(winbufnr(1)), 'c')
920  call assert_equal(bufname(winbufnr(2)), 'd')
921  call assert_equal(bufname(winbufnr(3)), 'b')
922  call assert_equal(bufname(winbufnr(4)), 'a')
923  call assert_equal(0, win_splitmove(winnr(), winnr('j'), {'vertical': 1}))
924  call assert_equal(0, win_splitmove(winnr(), winnr('j'), {'vertical': 1}))
925  call assert_equal(bufname(winbufnr(1)), 'c')
926  call assert_equal(bufname(winbufnr(2)), 'b')
927  call assert_equal(bufname(winbufnr(3)), 'd')
928  call assert_equal(bufname(winbufnr(4)), 'a')
929  call assert_equal(0, win_splitmove(winnr(), winnr('k'), {'vertical': 1}))
930  call assert_equal(bufname(winbufnr(1)), 'd')
931  call assert_equal(bufname(winbufnr(2)), 'c')
932  call assert_equal(bufname(winbufnr(3)), 'b')
933  call assert_equal(bufname(winbufnr(4)), 'a')
934  call assert_equal(0, win_splitmove(winnr(), winnr('j'), {'rightbelow': v:true}))
935  call assert_equal(bufname(winbufnr(1)), 'c')
936  call assert_equal(bufname(winbufnr(2)), 'b')
937  call assert_equal(bufname(winbufnr(3)), 'a')
938  call assert_equal(bufname(winbufnr(4)), 'd')
939  call assert_fails('call win_splitmove(winnr(), winnr("k"), test_null_dict())', 'E474:')
940  only | bd
941
942  call assert_fails('call win_splitmove(winnr(), 123)', 'E957:')
943  call assert_fails('call win_splitmove(123, winnr())', 'E957:')
944  call assert_fails('call win_splitmove(winnr(), winnr())', 'E957:')
945
946  tabnew
947  call assert_fails('call win_splitmove(1, win_getid(1, 1))', 'E957:')
948  tabclose
949endfunc
950
951" Test for the :only command
952func Test_window_only()
953  new
954  set modified
955  new
956  call assert_fails('only', 'E445:')
957  only!
958  " Test for :only with a count
959  let wid = win_getid()
960  new
961  new
962  3only
963  call assert_equal(1, winnr('$'))
964  call assert_equal(wid, win_getid())
965  call assert_fails('close', 'E444:')
966  call assert_fails('%close', 'E16:')
967endfunc
968
969" Test for errors with :wincmd
970func Test_wincmd_errors()
971  call assert_fails('wincmd g', 'E474:')
972  call assert_fails('wincmd ab', 'E474:')
973endfunc
974
975" Test for errors with :winpos
976func Test_winpos_errors()
977  if !has("gui_running") && !has('win32')
978    call assert_fails('winpos', 'E188:')
979  endif
980  call assert_fails('winpos 10', 'E466:')
981endfunc
982
983" Test for +cmd in a :split command
984func Test_split_cmd()
985  split +set\ readonly
986  call assert_equal(1, &readonly)
987  call assert_equal(2, winnr('$'))
988  close
989endfunc
990
991" Create maximum number of horizontally or vertically split windows and then
992" run commands that create a new horizontally/vertically split window
993func Run_noroom_for_newwindow_test(dir_arg)
994  let dir = (a:dir_arg == 'v') ? 'vert ' : ''
995
996  " Open as many windows as possible
997  while v:true
998    try
999      exe dir . 'new'
1000    catch /E36:/
1001      break
1002    endtry
1003  endwhile
1004
1005  call writefile(['first', 'second', 'third'], 'Xfile1')
1006  call writefile([], 'Xfile2')
1007  call writefile([], 'Xfile3')
1008
1009  " Argument list related commands
1010  args Xfile1 Xfile2 Xfile3
1011  next
1012  for cmd in ['sargument 2', 'snext', 'sprevious', 'sNext', 'srewind',
1013			\ 'sfirst', 'slast']
1014    call assert_fails(dir .. cmd, 'E36:')
1015  endfor
1016  %argdelete
1017
1018  " Buffer related commands
1019  set modified
1020  hide enew
1021  for cmd in ['sbuffer Xfile1', 'sbnext', 'sbprevious', 'sbNext', 'sbrewind',
1022		\ 'sbfirst', 'sblast', 'sball', 'sbmodified', 'sunhide']
1023    call assert_fails(dir .. cmd, 'E36:')
1024  endfor
1025
1026  " Window related commands
1027  for cmd in ['split', 'split Xfile2', 'new', 'new Xfile3', 'sview Xfile1',
1028		\ 'sfind runtest.vim']
1029    call assert_fails(dir .. cmd, 'E36:')
1030  endfor
1031
1032  " Help
1033  call assert_fails(dir .. 'help', 'E36:')
1034  call assert_fails(dir .. 'helpgrep window', 'E36:')
1035
1036  " Command-line window
1037  if a:dir_arg == 'h'
1038    " Cmd-line window is always a horizontally split window
1039    call assert_beeps('call feedkeys("q:\<CR>", "xt")')
1040  endif
1041
1042  " Quickfix and location list window
1043  if has('quickfix')
1044    cexpr ''
1045    call assert_fails(dir .. 'copen', 'E36:')
1046    lexpr ''
1047    call assert_fails(dir .. 'lopen', 'E36:')
1048
1049    " Preview window
1050    call assert_fails(dir .. 'pedit Xfile2', 'E36:')
1051    call setline(1, 'abc')
1052    call assert_fails(dir .. 'psearch abc', 'E36:')
1053  endif
1054
1055  " Window commands (CTRL-W ^ and CTRL-W f)
1056  if a:dir_arg == 'h'
1057    call assert_fails('call feedkeys("\<C-W>^", "xt")', 'E36:')
1058    call setline(1, 'Xfile1')
1059    call assert_fails('call feedkeys("gg\<C-W>f", "xt")', 'E36:')
1060  endif
1061  enew!
1062
1063  " Tag commands (:stag, :stselect and :stjump)
1064  call writefile(["!_TAG_FILE_ENCODING\tutf-8\t//",
1065        \ "second\tXfile1\t2",
1066        \ "third\tXfile1\t3",],
1067        \ 'Xtags')
1068  set tags=Xtags
1069  call assert_fails(dir .. 'stag second', 'E36:')
1070  call assert_fails('call feedkeys(":" .. dir .. "stselect second\n1\n", "xt")', 'E36:')
1071  call assert_fails(dir .. 'stjump second', 'E36:')
1072  call assert_fails(dir .. 'ptag second', 'E36:')
1073  set tags&
1074  call delete('Xtags')
1075
1076  " :isplit and :dsplit
1077  call setline(1, ['#define FOO 1', 'FOO'])
1078  normal 2G
1079  call assert_fails(dir .. 'isplit FOO', 'E36:')
1080  call assert_fails(dir .. 'dsplit FOO', 'E36:')
1081
1082  " terminal
1083  if has('terminal')
1084    call assert_fails(dir .. 'terminal', 'E36:')
1085  endif
1086
1087  %bwipe!
1088  call delete('Xfile1')
1089  call delete('Xfile2')
1090  call delete('Xfile3')
1091  only
1092endfunc
1093
1094func Test_split_cmds_with_no_room()
1095  call Run_noroom_for_newwindow_test('h')
1096  call Run_noroom_for_newwindow_test('v')
1097endfunc
1098
1099" Test for various wincmd failures
1100func Test_wincmd_fails()
1101  only!
1102  call assert_beeps("normal \<C-W>w")
1103  call assert_beeps("normal \<C-W>p")
1104  call assert_beeps("normal \<C-W>gk")
1105  call assert_beeps("normal \<C-W>r")
1106  call assert_beeps("normal \<C-W>K")
1107  call assert_beeps("normal \<C-W>H")
1108  call assert_beeps("normal \<C-W>2gt")
1109endfunc
1110
1111" Test for adjusting the window width when a window is closed with some
1112" windows using 'winfixwidth'
1113func Test_window_width_adjust()
1114  only
1115  " Three vertical windows. Windows 1 and 2 have 'winfixwidth' set and close
1116  " window 2.
1117  wincmd v
1118  vert resize 10
1119  set winfixwidth
1120  wincmd v
1121  set winfixwidth
1122  wincmd c
1123  call assert_inrange(10, 12, winwidth(1))
1124  " Three vertical windows. Windows 2 and 3 have 'winfixwidth' set and close
1125  " window 3.
1126  only
1127  set winfixwidth
1128  wincmd v
1129  vert resize 10
1130  set winfixwidth
1131  wincmd v
1132  set nowinfixwidth
1133  wincmd b
1134  wincmd c
1135  call assert_inrange(10, 12, winwidth(2))
1136
1137  new | only
1138endfunc
1139
1140" Test for jumping to a vertical/horizontal neighbor window based on the
1141" current cursor position
1142func Test_window_goto_neightbor()
1143  %bw!
1144
1145  " Vertical window movement
1146
1147  " create the following window layout:
1148  "     +--+--+
1149  "     |w1|w3|
1150  "     +--+  |
1151  "     |w2|  |
1152  "     +--+--+
1153  "     |w4   |
1154  "     +-----+
1155  new
1156  vsplit
1157  split
1158  " vertically jump from w4
1159  wincmd b
1160  call setline(1, repeat(' ', &columns))
1161  call cursor(1, 1)
1162  wincmd k
1163  call assert_equal(2, winnr())
1164  wincmd b
1165  call cursor(1, &columns)
1166  redraw!
1167  wincmd k
1168  call assert_equal(3, winnr())
1169  %bw!
1170
1171  " create the following window layout:
1172  "     +--+--+--+
1173  "     |w1|w2|w3|
1174  "     +--+--+--+
1175  "     |w4      |
1176  "     +--------+
1177  new
1178  vsplit
1179  vsplit
1180  wincmd b
1181  call setline(1, repeat(' ', &columns))
1182  call cursor(1, 1)
1183  wincmd k
1184  call assert_equal(1, winnr())
1185  wincmd b
1186  call cursor(1, &columns / 2)
1187  redraw!
1188  wincmd k
1189  call assert_equal(2, winnr())
1190  wincmd b
1191  call cursor(1, &columns)
1192  redraw!
1193  wincmd k
1194  call assert_equal(3, winnr())
1195  %bw!
1196
1197  " Horizontal window movement
1198
1199  " create the following window layout:
1200  "     +--+--+--+
1201  "     |w1|w2|w4|
1202  "     +--+--+  |
1203  "     |w3   |  |
1204  "     +-----+--+
1205  vsplit
1206  split
1207  vsplit
1208  4wincmd l
1209  call setline(1, repeat([' '], &lines))
1210  call cursor(1, 1)
1211  redraw!
1212  wincmd h
1213  call assert_equal(2, winnr())
1214  4wincmd l
1215  call cursor(&lines, 1)
1216  redraw!
1217  wincmd h
1218  call assert_equal(3, winnr())
1219  %bw!
1220
1221  " create the following window layout:
1222  "     +--+--+
1223  "     |w1|w4|
1224  "     +--+  +
1225  "     |w2|  |
1226  "     +--+  +
1227  "     |w3|  |
1228  "     +--+--+
1229  vsplit
1230  split
1231  split
1232  wincmd l
1233  call setline(1, repeat([' '], &lines))
1234  call cursor(1, 1)
1235  redraw!
1236  wincmd h
1237  call assert_equal(1, winnr())
1238  wincmd l
1239  call cursor(&lines / 2, 1)
1240  redraw!
1241  wincmd h
1242  call assert_equal(2, winnr())
1243  wincmd l
1244  call cursor(&lines, 1)
1245  redraw!
1246  wincmd h
1247  call assert_equal(3, winnr())
1248  %bw!
1249endfunc
1250
1251" Test for an autocmd closing the destination window when jumping from one
1252" window to another.
1253func Test_close_dest_window()
1254  split
1255  edit Xfile
1256
1257  " Test for BufLeave
1258  augroup T1
1259    au!
1260    au BufLeave Xfile $wincmd c
1261  augroup END
1262  wincmd b
1263  call assert_equal(1, winnr('$'))
1264  call assert_equal('Xfile', @%)
1265  augroup T1
1266    au!
1267  augroup END
1268
1269  " Test for WinLeave
1270  new
1271  wincmd p
1272  augroup T1
1273    au!
1274    au WinLeave * 1wincmd c
1275  augroup END
1276  wincmd t
1277  call assert_equal(1, winnr('$'))
1278  call assert_equal('Xfile', @%)
1279  augroup T1
1280    au!
1281  augroup END
1282  augroup! T1
1283  %bw!
1284endfunc
1285
1286" vim: shiftwidth=2 sts=2 expandtab
1287