blob: 4585cd321b8ff72fd6b828d02f6737fe429fdf66 [file] [log] [blame]
Bram Moolenaar991dea32016-05-24 11:31:32 +02001" Tests for window cmd (:wincmd, :split, :vsplit, :resize and etc...)
2
3func Test_window_cmd_ls0_with_split()
4 set ls=0
5 set splitbelow
6 split
7 quit
8 call assert_equal(0, &lines - &cmdheight - winheight(0))
9 new | only!
10 "
11 set splitbelow&vim
12 botright split
13 quit
14 call assert_equal(0, &lines - &cmdheight - winheight(0))
15 new | only!
16 set ls&vim
17endfunc
18
19func Test_window_cmd_cmdwin_with_vsp()
Bram Moolenaar72cf47a2018-05-10 18:23:29 +020020 let efmt = 'Expected 0 but got %d (in ls=%d, %s window)'
Bram Moolenaar991dea32016-05-24 11:31:32 +020021 for v in range(0, 2)
22 exec "set ls=" . v
23 vsplit
24 call feedkeys("q:\<CR>")
25 let ac = &lines - (&cmdheight + winheight(0) + !!v)
26 let emsg = printf(efmt, ac, v, 'left')
27 call assert_equal(0, ac, emsg)
28 wincmd w
29 let ac = &lines - (&cmdheight + winheight(0) + !!v)
30 let emsg = printf(efmt, ac, v, 'right')
31 call assert_equal(0, ac, emsg)
32 new | only!
33 endfor
34 set ls&vim
35endfunc
36
Bram Moolenaar5d2ca042016-06-26 17:11:21 +020037function Test_window_cmd_wincmd_gf()
38 let fname = 'test_gf.txt'
39 let swp_fname = '.' . fname . '.swp'
40 call writefile([], fname)
41 call writefile([], swp_fname)
42 function s:swap_exists()
43 let v:swapchoice = s:swap_choice
44 endfunc
Bram Moolenaareaa49e42019-07-13 18:08:59 +020045 " Remove the catch-all that runtest.vim adds
46 au! SwapExists
Bram Moolenaar5d2ca042016-06-26 17:11:21 +020047 augroup test_window_cmd_wincmd_gf
48 autocmd!
49 exec "autocmd SwapExists " . fname . " call s:swap_exists()"
50 augroup END
51
52 call setline(1, fname)
53 " (E)dit anyway
54 let s:swap_choice = 'e'
55 wincmd gf
56 call assert_equal(2, tabpagenr())
57 call assert_equal(fname, bufname("%"))
58 quit!
59
60 " (Q)uit
61 let s:swap_choice = 'q'
62 wincmd gf
63 call assert_equal(1, tabpagenr())
64 call assert_notequal(fname, bufname("%"))
65 new | only!
66
67 call delete(fname)
68 call delete(swp_fname)
69 augroup! test_window_cmd_wincmd_gf
70endfunc
71
Bram Moolenaar4520d442017-03-19 16:09:46 +010072func Test_window_quit()
73 e Xa
74 split Xb
75 call assert_equal(2, winnr('$'))
76 call assert_equal('Xb', bufname(winbufnr(1)))
77 call assert_equal('Xa', bufname(winbufnr(2)))
78
79 wincmd q
80 call assert_equal(1, winnr('$'))
81 call assert_equal('Xa', bufname(winbufnr(1)))
82
83 bw Xa Xb
84endfunc
85
86func Test_window_horizontal_split()
87 call assert_equal(1, winnr('$'))
88 3wincmd s
89 call assert_equal(2, winnr('$'))
90 call assert_equal(3, winheight(0))
91 call assert_equal(winwidth(1), winwidth(2))
92
93 call assert_fails('botright topleft wincmd s', 'E442:')
94 bw
95endfunc
96
97func Test_window_vertical_split()
98 call assert_equal(1, winnr('$'))
99 3wincmd v
100 call assert_equal(2, winnr('$'))
101 call assert_equal(3, winwidth(0))
102 call assert_equal(winheight(1), winheight(2))
103
104 call assert_fails('botright topleft wincmd v', 'E442:')
105 bw
106endfunc
107
Bram Moolenaar1bbb6192018-11-10 16:02:01 +0100108" Test the ":wincmd ^" and "<C-W>^" commands.
Bram Moolenaar4520d442017-03-19 16:09:46 +0100109func Test_window_split_edit_alternate()
Bram Moolenaar4520d442017-03-19 16:09:46 +0100110
Bram Moolenaar1bbb6192018-11-10 16:02:01 +0100111 " Test for failure when the alternate buffer/file no longer exists.
112 edit Xfoo | %bw
113 call assert_fails(':wincmd ^', 'E23')
114
115 " Test for the expected behavior when we have two named buffers.
116 edit Xfoo | edit Xbar
Bram Moolenaar4520d442017-03-19 16:09:46 +0100117 wincmd ^
Bram Moolenaar1bbb6192018-11-10 16:02:01 +0100118 call assert_equal('Xfoo', bufname(winbufnr(1)))
119 call assert_equal('Xbar', bufname(winbufnr(2)))
120 only
Bram Moolenaar4520d442017-03-19 16:09:46 +0100121
Bram Moolenaar1bbb6192018-11-10 16:02:01 +0100122 " Test for the expected behavior when the alternate buffer is not named.
123 enew | let l:nr1 = bufnr('%')
124 edit Xfoo | let l:nr2 = bufnr('%')
125 wincmd ^
126 call assert_equal(l:nr1, winbufnr(1))
127 call assert_equal(l:nr2, winbufnr(2))
128 only
129
Bram Moolenaard42333d2018-11-10 20:28:19 +0100130 " FIXME: this currently fails on AppVeyor, but passes locally
131 if !has('win32')
132 " Test the Normal mode command.
133 call feedkeys("\<C-W>\<C-^>", 'tx')
134 call assert_equal(l:nr2, winbufnr(1))
135 call assert_equal(l:nr1, winbufnr(2))
136 endif
Bram Moolenaar1bbb6192018-11-10 16:02:01 +0100137
138 %bw!
139endfunc
140
141" Test the ":[count]wincmd ^" and "[count]<C-W>^" commands.
142func Test_window_split_edit_bufnr()
143
144 %bwipeout
145 let l:nr = bufnr('%') + 1
146 call assert_fails(':execute "normal! ' . l:nr . '\<C-W>\<C-^>"', 'E92')
147 call assert_fails(':' . l:nr . 'wincmd ^', 'E16')
148 call assert_fails(':0wincmd ^', 'E16')
149
150 edit Xfoo | edit Xbar | edit Xbaz
151 let l:foo_nr = bufnr('Xfoo')
152 let l:bar_nr = bufnr('Xbar')
153 let l:baz_nr = bufnr('Xbaz')
154
Bram Moolenaar8617b402018-11-10 20:47:48 +0100155 " FIXME: this currently fails on AppVeyor, but passes locally
156 if !has('win32')
157 call feedkeys(l:foo_nr . "\<C-W>\<C-^>", 'tx')
158 call assert_equal('Xfoo', bufname(winbufnr(1)))
159 call assert_equal('Xbaz', bufname(winbufnr(2)))
160 only
Bram Moolenaar1bbb6192018-11-10 16:02:01 +0100161
Bram Moolenaar8617b402018-11-10 20:47:48 +0100162 call feedkeys(l:bar_nr . "\<C-W>\<C-^>", 'tx')
163 call assert_equal('Xbar', bufname(winbufnr(1)))
164 call assert_equal('Xfoo', bufname(winbufnr(2)))
165 only
Bram Moolenaar1bbb6192018-11-10 16:02:01 +0100166
Bram Moolenaar8617b402018-11-10 20:47:48 +0100167 execute l:baz_nr . 'wincmd ^'
168 call assert_equal('Xbaz', bufname(winbufnr(1)))
169 call assert_equal('Xbar', bufname(winbufnr(2)))
170 endif
Bram Moolenaar1bbb6192018-11-10 16:02:01 +0100171
172 %bw!
Bram Moolenaar4520d442017-03-19 16:09:46 +0100173endfunc
174
175func Test_window_preview()
176 " Open a preview window
177 pedit Xa
178 call assert_equal(2, winnr('$'))
179 call assert_equal(0, &previewwindow)
180
181 " Go to the preview window
182 wincmd P
183 call assert_equal(1, &previewwindow)
184
185 " Close preview window
186 wincmd z
187 call assert_equal(1, winnr('$'))
188 call assert_equal(0, &previewwindow)
189
190 call assert_fails('wincmd P', 'E441:')
191endfunc
192
193func Test_window_exchange()
194 e Xa
195
196 " Nothing happens with window exchange when there is 1 window
197 wincmd x
198 call assert_equal(1, winnr('$'))
199
200 split Xb
201 split Xc
202
203 call assert_equal('Xc', bufname(winbufnr(1)))
204 call assert_equal('Xb', bufname(winbufnr(2)))
205 call assert_equal('Xa', bufname(winbufnr(3)))
206
207 " Exchange current window 1 with window 3
208 3wincmd x
209 call assert_equal('Xa', bufname(winbufnr(1)))
210 call assert_equal('Xb', bufname(winbufnr(2)))
211 call assert_equal('Xc', bufname(winbufnr(3)))
212
213 " Exchange window with next when at the top window
214 wincmd x
215 call assert_equal('Xb', bufname(winbufnr(1)))
216 call assert_equal('Xa', bufname(winbufnr(2)))
217 call assert_equal('Xc', bufname(winbufnr(3)))
218
219 " Exchange window with next when at the middle window
220 wincmd j
221 wincmd x
222 call assert_equal('Xb', bufname(winbufnr(1)))
223 call assert_equal('Xc', bufname(winbufnr(2)))
224 call assert_equal('Xa', bufname(winbufnr(3)))
225
226 " Exchange window with next when at the bottom window.
227 " When there is no next window, it exchanges with the previous window.
228 wincmd j
229 wincmd x
230 call assert_equal('Xb', bufname(winbufnr(1)))
231 call assert_equal('Xa', bufname(winbufnr(2)))
232 call assert_equal('Xc', bufname(winbufnr(3)))
233
234 bw Xa Xb Xc
235endfunc
236
237func Test_window_rotate()
238 e Xa
239 split Xb
240 split Xc
241 call assert_equal('Xc', bufname(winbufnr(1)))
242 call assert_equal('Xb', bufname(winbufnr(2)))
243 call assert_equal('Xa', bufname(winbufnr(3)))
244
245 " Rotate downwards
246 wincmd r
247 call assert_equal('Xa', bufname(winbufnr(1)))
248 call assert_equal('Xc', bufname(winbufnr(2)))
249 call assert_equal('Xb', bufname(winbufnr(3)))
250
251 2wincmd r
252 call assert_equal('Xc', bufname(winbufnr(1)))
253 call assert_equal('Xb', bufname(winbufnr(2)))
254 call assert_equal('Xa', bufname(winbufnr(3)))
255
256 " Rotate upwards
257 wincmd R
258 call assert_equal('Xb', bufname(winbufnr(1)))
259 call assert_equal('Xa', bufname(winbufnr(2)))
260 call assert_equal('Xc', bufname(winbufnr(3)))
261
262 2wincmd R
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 bot vsplit
268 call assert_fails('wincmd R', 'E443:')
269
270 bw Xa Xb Xc
271endfunc
272
273func Test_window_height()
274 e Xa
275 split Xb
276
277 let [wh1, wh2] = [winheight(1), winheight(2)]
278 " Active window (1) should have the same height or 1 more
279 " than the other window.
280 call assert_inrange(wh2, wh2 + 1, wh1)
281
282 wincmd -
283 call assert_equal(wh1 - 1, winheight(1))
284 call assert_equal(wh2 + 1, winheight(2))
285
286 wincmd +
287 call assert_equal(wh1, winheight(1))
288 call assert_equal(wh2, winheight(2))
289
290 2wincmd _
291 call assert_equal(2, winheight(1))
292 call assert_equal(wh1 + wh2 - 2, winheight(2))
293
294 wincmd =
295 call assert_equal(wh1, winheight(1))
296 call assert_equal(wh2, winheight(2))
297
298 2wincmd _
299 set winfixheight
300 split Xc
301 let [wh1, wh2, wh3] = [winheight(1), winheight(2), winheight(3)]
302 call assert_equal(2, winheight(2))
303 call assert_inrange(wh3, wh3 + 1, wh1)
304 3wincmd +
305 call assert_equal(2, winheight(2))
306 call assert_equal(wh1 + 3, winheight(1))
307 call assert_equal(wh3 - 3, winheight(3))
308 wincmd =
309 call assert_equal(2, winheight(2))
310 call assert_equal(wh1, winheight(1))
311 call assert_equal(wh3, winheight(3))
312
313 wincmd j
314 set winfixheight&
315
316 wincmd =
317 let [wh1, wh2, wh3] = [winheight(1), winheight(2), winheight(3)]
318 " Current window (2) should have the same height or 1 more
319 " than the other windows.
320 call assert_inrange(wh1, wh1 + 1, wh2)
321 call assert_inrange(wh3, wh3 + 1, wh2)
322
323 bw Xa Xb Xc
324endfunc
325
326func Test_window_width()
327 e Xa
328 vsplit Xb
329
330 let [ww1, ww2] = [winwidth(1), winwidth(2)]
331 " Active window (1) should have the same width or 1 more
332 " than the other window.
333 call assert_inrange(ww2, ww2 + 1, ww1)
334
335 wincmd <
336 call assert_equal(ww1 - 1, winwidth(1))
337 call assert_equal(ww2 + 1, winwidth(2))
338
339 wincmd >
340 call assert_equal(ww1, winwidth(1))
341 call assert_equal(ww2, winwidth(2))
342
343 2wincmd |
344 call assert_equal(2, winwidth(1))
345 call assert_equal(ww1 + ww2 - 2, winwidth(2))
346
347 wincmd =
348 call assert_equal(ww1, winwidth(1))
349 call assert_equal(ww2, winwidth(2))
350
351 2wincmd |
352 set winfixwidth
353 vsplit Xc
354 let [ww1, ww2, ww3] = [winwidth(1), winwidth(2), winwidth(3)]
Bram Moolenaar38e34832017-03-19 20:22:36 +0100355 call assert_equal(2, winwidth(2))
Bram Moolenaar4520d442017-03-19 16:09:46 +0100356 call assert_inrange(ww3, ww3 + 1, ww1)
357 3wincmd >
Bram Moolenaar38e34832017-03-19 20:22:36 +0100358 call assert_equal(2, winwidth(2))
Bram Moolenaar4520d442017-03-19 16:09:46 +0100359 call assert_equal(ww1 + 3, winwidth(1))
360 call assert_equal(ww3 - 3, winwidth(3))
361 wincmd =
Bram Moolenaar38e34832017-03-19 20:22:36 +0100362 call assert_equal(2, winwidth(2))
Bram Moolenaar4520d442017-03-19 16:09:46 +0100363 call assert_equal(ww1, winwidth(1))
364 call assert_equal(ww3, winwidth(3))
365
366 wincmd l
367 set winfixwidth&
368
369 wincmd =
370 let [ww1, ww2, ww3] = [winwidth(1), winwidth(2), winwidth(3)]
371 " Current window (2) should have the same width or 1 more
372 " than the other windows.
373 call assert_inrange(ww1, ww1 + 1, ww2)
374 call assert_inrange(ww3, ww3 + 1, ww2)
375
376 bw Xa Xb Xc
377endfunc
378
Bram Moolenaar8eeeba82017-06-25 22:45:39 +0200379func Test_equalalways_on_close()
380 set equalalways
381 vsplit
382 windo split
Bram Moolenaar1bbb6192018-11-10 16:02:01 +0100383 split
Bram Moolenaar8eeeba82017-06-25 22:45:39 +0200384 wincmd J
385 " now we have a frame top-left with two windows, a frame top-right with two
386 " windows and a frame at the bottom, full-width.
387 let height_1 = winheight(1)
388 let height_2 = winheight(2)
389 let height_3 = winheight(3)
390 let height_4 = winheight(4)
391 " closing the bottom window causes all windows to be resized.
392 close
393 call assert_notequal(height_1, winheight(1))
394 call assert_notequal(height_2, winheight(2))
395 call assert_notequal(height_3, winheight(3))
396 call assert_notequal(height_4, winheight(4))
397 call assert_equal(winheight(1), winheight(3))
398 call assert_equal(winheight(2), winheight(4))
399
400 1wincmd w
401 split
402 4wincmd w
403 resize + 5
404 " left column has three windows, equalized heights.
405 " right column has two windows, top one a bit higher
406 let height_1 = winheight(1)
407 let height_2 = winheight(2)
408 let height_4 = winheight(4)
409 let height_5 = winheight(5)
410 3wincmd w
411 " closing window in left column equalizes heights in left column but not in
412 " the right column
413 close
414 call assert_notequal(height_1, winheight(1))
415 call assert_notequal(height_2, winheight(2))
416 call assert_equal(height_4, winheight(3))
417 call assert_equal(height_5, winheight(4))
418
419 only
420 set equalalways&
421endfunc
422
Bram Moolenaar22044dc2017-12-02 15:43:37 +0100423func Test_win_screenpos()
424 call assert_equal(1, winnr('$'))
425 split
426 vsplit
427 10wincmd _
428 30wincmd |
429 call assert_equal([1, 1], win_screenpos(1))
430 call assert_equal([1, 32], win_screenpos(2))
431 call assert_equal([12, 1], win_screenpos(3))
432 call assert_equal([0, 0], win_screenpos(4))
433 only
434endfunc
435
Bram Moolenaar4520d442017-03-19 16:09:46 +0100436func Test_window_jump_tag()
437 help
438 /iccf
439 call assert_match('^|iccf|', getline('.'))
440 call assert_equal(2, winnr('$'))
441 2wincmd }
442 call assert_equal(3, winnr('$'))
443 call assert_match('^|iccf|', getline('.'))
444 wincmd k
445 call assert_match('\*iccf\*', getline('.'))
446 call assert_equal(2, winheight(0))
447
448 wincmd z
449 set previewheight=4
450 help
451 /bugs
452 wincmd }
453 wincmd k
454 call assert_match('\*bugs\*', getline('.'))
455 call assert_equal(4, winheight(0))
456 set previewheight&
457
458 %bw!
459endfunc
460
461func Test_window_newtab()
462 e Xa
463
464 call assert_equal(1, tabpagenr('$'))
465 call assert_equal("\nAlready only one window", execute('wincmd T'))
466
467 split Xb
468 split Xc
469
470 wincmd T
471 call assert_equal(2, tabpagenr('$'))
472 call assert_equal(['Xb', 'Xa'], map(tabpagebuflist(1), 'bufname(v:val)'))
473 call assert_equal(['Xc' ], map(tabpagebuflist(2), 'bufname(v:val)'))
474
475 %bw!
476endfunc
477
Bram Moolenaarf79225e2017-03-18 23:11:04 +0100478func Test_next_split_all()
479 " This was causing an illegal memory access.
480 n x
481 norm axxx
482 split
483 split
484 s/x
485 s/x
486 all
487 bwipe!
488endfunc
489
Bram Moolenaar75373f32017-08-07 22:02:30 +0200490" Tests for adjusting window and contents
491func GetScreenStr(row)
492 let str = ""
493 for c in range(1,3)
494 let str .= nr2char(screenchar(a:row, c))
495 endfor
496 return str
497endfunc
498
499func Test_window_contents()
500 enew! | only | new
501 call setline(1, range(1,256))
502
503 exe "norm! \<C-W>t\<C-W>=1Gzt\<C-W>w\<C-W>+"
504 redraw
Bram Moolenaar72cf47a2018-05-10 18:23:29 +0200505 let s3 = GetScreenStr(1)
Bram Moolenaar75373f32017-08-07 22:02:30 +0200506 wincmd p
507 call assert_equal(1, line("w0"))
508 call assert_equal('1 ', s3)
509
510 exe "norm! \<C-W>t\<C-W>=50Gzt\<C-W>w\<C-W>+"
511 redraw
Bram Moolenaar72cf47a2018-05-10 18:23:29 +0200512 let s3 = GetScreenStr(1)
Bram Moolenaar75373f32017-08-07 22:02:30 +0200513 wincmd p
514 call assert_equal(50, line("w0"))
515 call assert_equal('50 ', s3)
516
517 exe "norm! \<C-W>t\<C-W>=59Gzt\<C-W>w\<C-W>+"
518 redraw
Bram Moolenaar72cf47a2018-05-10 18:23:29 +0200519 let s3 = GetScreenStr(1)
Bram Moolenaar75373f32017-08-07 22:02:30 +0200520 wincmd p
521 call assert_equal(59, line("w0"))
522 call assert_equal('59 ', s3)
523
524 bwipeout!
525 call test_garbagecollect_now()
526endfunc
527
Bram Moolenaar2efb3232017-12-19 12:27:23 +0100528func Test_window_colon_command()
529 " This was reading invalid memory.
530 exe "norm! v\<C-W>:\<C-U>echo v:version"
531endfunc
532
Bram Moolenaar6f361c92018-01-31 19:06:50 +0100533func Test_access_freed_mem()
534 " This was accessing freed memory
535 au * 0 vs xxx
536 arg 0
537 argadd
538 all
539 all
540 au!
541 bwipe xxx
542endfunc
543
Bram Moolenaar5bab5552018-04-13 20:41:29 +0200544func Test_visual_cleared_after_window_split()
545 new | only!
546 let smd_save = &showmode
547 set showmode
548 let ls_save = &laststatus
549 set laststatus=1
550 call setline(1, ['a', 'b', 'c', 'd', ''])
551 norm! G
552 exe "norm! kkvk"
553 redraw
554 exe "norm! \<C-W>v"
555 redraw
556 " check if '-- VISUAL --' disappeared from command line
557 let columns = range(1, &columns)
558 let cmdlinechars = map(columns, 'nr2char(screenchar(&lines, v:val))')
559 let cmdline = join(cmdlinechars, '')
560 let cmdline_ltrim = substitute(cmdline, '^\s*', "", "")
561 let mode_shown = substitute(cmdline_ltrim, '\s*$', "", "")
562 call assert_equal('', mode_shown)
563 let &showmode = smd_save
564 let &laststatus = ls_save
565 bwipe!
566endfunc
567
Bram Moolenaar72cf47a2018-05-10 18:23:29 +0200568func Test_winrestcmd()
569 2split
570 3vsplit
571 let a = winrestcmd()
572 call assert_equal(2, winheight(0))
573 call assert_equal(3, winwidth(0))
574 wincmd =
575 call assert_notequal(2, winheight(0))
576 call assert_notequal(3, winwidth(0))
577 exe a
578 call assert_equal(2, winheight(0))
579 call assert_equal(3, winwidth(0))
580 only
581endfunc
582
Bram Moolenaar1e115362019-01-09 23:01:02 +0100583func Fun_RenewFile()
Bram Moolenaara42df592018-12-24 00:22:39 +0100584 sleep 2
585 silent execute '!echo "1" > tmp.txt'
586 sp
587 wincmd p
588 edit! tmp.txt
Bram Moolenaar1e115362019-01-09 23:01:02 +0100589endfunc
Bram Moolenaara42df592018-12-24 00:22:39 +0100590
591func Test_window_prevwin()
592 " Can we make this work on MS-Windows?
593 if !has('unix')
594 return
595 endif
596
597 set hidden autoread
598 call writefile(['2'], 'tmp.txt')
599 new tmp.txt
600 q
601 " Need to wait a bit for the timestamp to be older.
602 call Fun_RenewFile()
603 call assert_equal(2, winnr())
604 wincmd p
605 call assert_equal(1, winnr())
606 wincmd p
607 q
608 call Fun_RenewFile()
609 call assert_equal(2, winnr())
610 wincmd p
611 call assert_equal(1, winnr())
612 wincmd p
613 " reset
614 q
615 call delete('tmp.txt')
616 set hidden&vim autoread&vim
617 delfunc Fun_RenewFile
618endfunc
619
Bram Moolenaar8fcb60f2019-03-04 13:18:30 +0100620func Test_relative_cursor_position_in_one_line_window()
621 new
622 only
623 call setline(1, range(1, 10000))
624 normal 50%
625 let lnum = getcurpos()[1]
626 split
627 split
628 " make third window take as many lines as possible, other windows will
629 " become one line
630 3wincmd w
631 for i in range(1, &lines - 6)
632 wincmd +
633 redraw!
634 endfor
635
636 " first and second window should show cursor line
637 let wininfo = getwininfo()
638 call assert_equal(lnum, wininfo[0].topline)
639 call assert_equal(lnum, wininfo[1].topline)
640
641 only!
642 bwipe!
643endfunc
644
645func Test_relative_cursor_position_after_move_and_resize()
646 let so_save = &so
647 set so=0
648 enew
649 call setline(1, range(1, 10000))
650 normal 50%
651 split
652 1wincmd w
653 " Move cursor to first line in window
654 normal H
655 redraw!
656 " Reduce window height to two lines
657 let height = winheight(0)
658 while winheight(0) > 2
659 wincmd -
660 redraw!
661 endwhile
662 " move cursor to second/last line in window
663 normal j
664 " restore previous height
665 while winheight(0) < height
666 wincmd +
667 redraw!
668 endwhile
669 " make window two lines again
670 while winheight(0) > 2
671 wincmd -
672 redraw!
673 endwhile
674
675 " cursor should be at bottom line
676 let info = getwininfo(win_getid())[0]
677 call assert_equal(info.topline + 1, getcurpos()[1])
678
679 only!
680 bwipe!
681 let &so = so_save
682endfunc
683
684func Test_relative_cursor_position_after_resize()
685 let so_save = &so
686 set so=0
687 enew
688 call setline(1, range(1, 10000))
689 normal 50%
690 split
691 1wincmd w
692 let winid1 = win_getid()
693 let info = getwininfo(winid1)[0]
694 " Move cursor to second line in window
695 exe "normal " . (info.topline + 1) . "G"
696 redraw!
697 let lnum = getcurpos()[1]
698
699 " Make the window only two lines high, cursor should end up in top line
700 2wincmd w
701 exe (info.height - 2) . "wincmd +"
702 redraw!
703 let info = getwininfo(winid1)[0]
704 call assert_equal(lnum, info.topline)
705
706 only!
707 bwipe!
708 let &so = so_save
709endfunc
710
711func Test_relative_cursor_second_line_after_resize()
712 let so_save = &so
713 set so=0
714 enew
715 call setline(1, range(1, 10000))
716 normal 50%
717 split
718 1wincmd w
719 let winid1 = win_getid()
720 let info = getwininfo(winid1)[0]
721
722 " Make the window only two lines high
723 2wincmd _
724
725 " Move cursor to second line in window
726 normal H
727 normal j
728
729 " Make window size bigger, then back to 2 lines
730 for i in range(1, 10)
731 wincmd +
732 redraw!
733 endfor
734 for i in range(1, 10)
735 wincmd -
736 redraw!
737 endfor
738
739 " cursor should end up in bottom line
740 let info = getwininfo(winid1)[0]
741 call assert_equal(info.topline + 1, getcurpos()[1])
742
743 only!
744 bwipe!
745 let &so = so_save
746endfunc
747
Bram Moolenaara9b25352019-05-12 14:25:30 +0200748func Test_split_noscroll()
749 let so_save = &so
Bram Moolenaarbd2d68c2019-05-18 15:36:11 +0200750 enew
751 call setline(1, range(1, 8))
Bram Moolenaara9b25352019-05-12 14:25:30 +0200752 normal 100%
753 split
754
755 1wincmd w
756 let winid1 = win_getid()
757 let info1 = getwininfo(winid1)[0]
758
759 2wincmd w
760 let winid2 = win_getid()
761 let info2 = getwininfo(winid2)[0]
762
763 call assert_equal(1, info1.topline)
764 call assert_equal(1, info2.topline)
765
Bram Moolenaarbd2d68c2019-05-18 15:36:11 +0200766 " window that fits all lines by itself, but not when split: closing other
767 " window should restore fraction.
Bram Moolenaara9b25352019-05-12 14:25:30 +0200768 only!
Bram Moolenaarbd2d68c2019-05-18 15:36:11 +0200769 call setline(1, range(1, &lines - 10))
770 exe &lines / 4
771 let winid1 = win_getid()
772 let info1 = getwininfo(winid1)[0]
773 call assert_equal(1, info1.topline)
774 new
775 redraw
776 close
777 let info1 = getwininfo(winid1)[0]
778 call assert_equal(1, info1.topline)
779
Bram Moolenaara9b25352019-05-12 14:25:30 +0200780 bwipe!
781 let &so = so_save
782endfunc
783
Bram Moolenaar46ad2882019-04-08 20:01:47 +0200784" Tests for the winnr() function
785func Test_winnr()
786 only | tabonly
787 call assert_equal(1, winnr('j'))
788 call assert_equal(1, winnr('k'))
789 call assert_equal(1, winnr('h'))
790 call assert_equal(1, winnr('l'))
791
792 " create a set of horizontally and vertically split windows
793 leftabove new | wincmd p
794 leftabove new | wincmd p
795 rightbelow new | wincmd p
796 rightbelow new | wincmd p
797 leftabove vnew | wincmd p
798 leftabove vnew | wincmd p
799 rightbelow vnew | wincmd p
800 rightbelow vnew | wincmd p
801
802 call assert_equal(8, winnr('j'))
803 call assert_equal(2, winnr('k'))
804 call assert_equal(4, winnr('h'))
805 call assert_equal(6, winnr('l'))
806 call assert_equal(9, winnr('2j'))
807 call assert_equal(1, winnr('2k'))
808 call assert_equal(3, winnr('2h'))
809 call assert_equal(7, winnr('2l'))
810
811 " Error cases
812 call assert_fails("echo winnr('0.2k')", 'E15:')
813 call assert_equal(2, winnr('-2k'))
814 call assert_fails("echo winnr('-2xj')", 'E15:')
815 call assert_fails("echo winnr('j2j')", 'E15:')
816 call assert_fails("echo winnr('ll')", 'E15:')
817 call assert_fails("echo winnr('5')", 'E15:')
818 call assert_equal(4, winnr('0h'))
819
820 tabnew
821 call assert_equal(8, tabpagewinnr(1, 'j'))
822 call assert_equal(2, tabpagewinnr(1, 'k'))
823 call assert_equal(4, tabpagewinnr(1, 'h'))
824 call assert_equal(6, tabpagewinnr(1, 'l'))
825
826 only | tabonly
827endfunc
828
Bram Moolenaar9e4d8212016-08-18 23:04:48 +0200829" vim: shiftwidth=2 sts=2 expandtab