blob: 9927e27576e37868d569843744453107c384cbf6 [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
Bram Moolenaar026587b2019-08-17 15:08:00 +0200193func Test_window_preview_from_help()
194 filetype on
195 call writefile(['/* some C code */'], 'Xpreview.c')
196 help
197 pedit Xpreview.c
198 wincmd P
199 call assert_equal(1, &previewwindow)
200 call assert_equal('c', &filetype)
201 wincmd z
202
203 filetype off
204 close
205 call delete('Xpreview.c')
206endfunc
207
Bram Moolenaar4520d442017-03-19 16:09:46 +0100208func Test_window_exchange()
209 e Xa
210
211 " Nothing happens with window exchange when there is 1 window
212 wincmd x
213 call assert_equal(1, winnr('$'))
214
215 split Xb
216 split Xc
217
218 call assert_equal('Xc', bufname(winbufnr(1)))
219 call assert_equal('Xb', bufname(winbufnr(2)))
220 call assert_equal('Xa', bufname(winbufnr(3)))
221
222 " Exchange current window 1 with window 3
223 3wincmd x
224 call assert_equal('Xa', bufname(winbufnr(1)))
225 call assert_equal('Xb', bufname(winbufnr(2)))
226 call assert_equal('Xc', bufname(winbufnr(3)))
227
228 " Exchange window with next when at the top window
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 " Exchange window with next when at the middle window
235 wincmd j
236 wincmd x
237 call assert_equal('Xb', bufname(winbufnr(1)))
238 call assert_equal('Xc', bufname(winbufnr(2)))
239 call assert_equal('Xa', bufname(winbufnr(3)))
240
241 " Exchange window with next when at the bottom window.
242 " When there is no next window, it exchanges with the previous window.
243 wincmd j
244 wincmd x
245 call assert_equal('Xb', bufname(winbufnr(1)))
246 call assert_equal('Xa', bufname(winbufnr(2)))
247 call assert_equal('Xc', bufname(winbufnr(3)))
248
249 bw Xa Xb Xc
250endfunc
251
252func Test_window_rotate()
253 e Xa
254 split Xb
255 split Xc
256 call assert_equal('Xc', bufname(winbufnr(1)))
257 call assert_equal('Xb', bufname(winbufnr(2)))
258 call assert_equal('Xa', bufname(winbufnr(3)))
259
260 " Rotate downwards
261 wincmd r
262 call assert_equal('Xa', bufname(winbufnr(1)))
263 call assert_equal('Xc', bufname(winbufnr(2)))
264 call assert_equal('Xb', bufname(winbufnr(3)))
265
266 2wincmd r
267 call assert_equal('Xc', bufname(winbufnr(1)))
268 call assert_equal('Xb', bufname(winbufnr(2)))
269 call assert_equal('Xa', bufname(winbufnr(3)))
270
271 " Rotate upwards
272 wincmd R
273 call assert_equal('Xb', bufname(winbufnr(1)))
274 call assert_equal('Xa', bufname(winbufnr(2)))
275 call assert_equal('Xc', bufname(winbufnr(3)))
276
277 2wincmd R
278 call assert_equal('Xc', bufname(winbufnr(1)))
279 call assert_equal('Xb', bufname(winbufnr(2)))
280 call assert_equal('Xa', bufname(winbufnr(3)))
281
282 bot vsplit
283 call assert_fails('wincmd R', 'E443:')
284
285 bw Xa Xb Xc
286endfunc
287
288func Test_window_height()
289 e Xa
290 split Xb
291
292 let [wh1, wh2] = [winheight(1), winheight(2)]
293 " Active window (1) should have the same height or 1 more
294 " than the other window.
295 call assert_inrange(wh2, wh2 + 1, wh1)
296
297 wincmd -
298 call assert_equal(wh1 - 1, winheight(1))
299 call assert_equal(wh2 + 1, winheight(2))
300
301 wincmd +
302 call assert_equal(wh1, winheight(1))
303 call assert_equal(wh2, winheight(2))
304
305 2wincmd _
306 call assert_equal(2, winheight(1))
307 call assert_equal(wh1 + wh2 - 2, winheight(2))
308
309 wincmd =
310 call assert_equal(wh1, winheight(1))
311 call assert_equal(wh2, winheight(2))
312
313 2wincmd _
314 set winfixheight
315 split Xc
316 let [wh1, wh2, wh3] = [winheight(1), winheight(2), winheight(3)]
317 call assert_equal(2, winheight(2))
318 call assert_inrange(wh3, wh3 + 1, wh1)
319 3wincmd +
320 call assert_equal(2, winheight(2))
321 call assert_equal(wh1 + 3, winheight(1))
322 call assert_equal(wh3 - 3, winheight(3))
323 wincmd =
324 call assert_equal(2, winheight(2))
325 call assert_equal(wh1, winheight(1))
326 call assert_equal(wh3, winheight(3))
327
328 wincmd j
329 set winfixheight&
330
331 wincmd =
332 let [wh1, wh2, wh3] = [winheight(1), winheight(2), winheight(3)]
333 " Current window (2) should have the same height or 1 more
334 " than the other windows.
335 call assert_inrange(wh1, wh1 + 1, wh2)
336 call assert_inrange(wh3, wh3 + 1, wh2)
337
338 bw Xa Xb Xc
339endfunc
340
341func Test_window_width()
342 e Xa
343 vsplit Xb
344
345 let [ww1, ww2] = [winwidth(1), winwidth(2)]
346 " Active window (1) should have the same width or 1 more
347 " than the other window.
348 call assert_inrange(ww2, ww2 + 1, ww1)
349
350 wincmd <
351 call assert_equal(ww1 - 1, winwidth(1))
352 call assert_equal(ww2 + 1, winwidth(2))
353
354 wincmd >
355 call assert_equal(ww1, winwidth(1))
356 call assert_equal(ww2, winwidth(2))
357
358 2wincmd |
359 call assert_equal(2, winwidth(1))
360 call assert_equal(ww1 + ww2 - 2, winwidth(2))
361
362 wincmd =
363 call assert_equal(ww1, winwidth(1))
364 call assert_equal(ww2, winwidth(2))
365
366 2wincmd |
367 set winfixwidth
368 vsplit Xc
369 let [ww1, ww2, ww3] = [winwidth(1), winwidth(2), winwidth(3)]
Bram Moolenaar38e34832017-03-19 20:22:36 +0100370 call assert_equal(2, winwidth(2))
Bram Moolenaar4520d442017-03-19 16:09:46 +0100371 call assert_inrange(ww3, ww3 + 1, ww1)
372 3wincmd >
Bram Moolenaar38e34832017-03-19 20:22:36 +0100373 call assert_equal(2, winwidth(2))
Bram Moolenaar4520d442017-03-19 16:09:46 +0100374 call assert_equal(ww1 + 3, winwidth(1))
375 call assert_equal(ww3 - 3, winwidth(3))
376 wincmd =
Bram Moolenaar38e34832017-03-19 20:22:36 +0100377 call assert_equal(2, winwidth(2))
Bram Moolenaar4520d442017-03-19 16:09:46 +0100378 call assert_equal(ww1, winwidth(1))
379 call assert_equal(ww3, winwidth(3))
380
381 wincmd l
382 set winfixwidth&
383
384 wincmd =
385 let [ww1, ww2, ww3] = [winwidth(1), winwidth(2), winwidth(3)]
386 " Current window (2) should have the same width or 1 more
387 " than the other windows.
388 call assert_inrange(ww1, ww1 + 1, ww2)
389 call assert_inrange(ww3, ww3 + 1, ww2)
390
391 bw Xa Xb Xc
392endfunc
393
Bram Moolenaar8eeeba82017-06-25 22:45:39 +0200394func Test_equalalways_on_close()
395 set equalalways
396 vsplit
397 windo split
Bram Moolenaar1bbb6192018-11-10 16:02:01 +0100398 split
Bram Moolenaar8eeeba82017-06-25 22:45:39 +0200399 wincmd J
400 " now we have a frame top-left with two windows, a frame top-right with two
401 " windows and a frame at the bottom, full-width.
402 let height_1 = winheight(1)
403 let height_2 = winheight(2)
404 let height_3 = winheight(3)
405 let height_4 = winheight(4)
406 " closing the bottom window causes all windows to be resized.
407 close
408 call assert_notequal(height_1, winheight(1))
409 call assert_notequal(height_2, winheight(2))
410 call assert_notequal(height_3, winheight(3))
411 call assert_notequal(height_4, winheight(4))
412 call assert_equal(winheight(1), winheight(3))
413 call assert_equal(winheight(2), winheight(4))
414
415 1wincmd w
416 split
417 4wincmd w
418 resize + 5
419 " left column has three windows, equalized heights.
420 " right column has two windows, top one a bit higher
421 let height_1 = winheight(1)
422 let height_2 = winheight(2)
423 let height_4 = winheight(4)
424 let height_5 = winheight(5)
425 3wincmd w
426 " closing window in left column equalizes heights in left column but not in
427 " the right column
428 close
429 call assert_notequal(height_1, winheight(1))
430 call assert_notequal(height_2, winheight(2))
431 call assert_equal(height_4, winheight(3))
432 call assert_equal(height_5, winheight(4))
433
434 only
435 set equalalways&
436endfunc
437
Bram Moolenaar22044dc2017-12-02 15:43:37 +0100438func Test_win_screenpos()
439 call assert_equal(1, winnr('$'))
440 split
441 vsplit
442 10wincmd _
443 30wincmd |
444 call assert_equal([1, 1], win_screenpos(1))
445 call assert_equal([1, 32], win_screenpos(2))
446 call assert_equal([12, 1], win_screenpos(3))
447 call assert_equal([0, 0], win_screenpos(4))
448 only
449endfunc
450
Bram Moolenaar4520d442017-03-19 16:09:46 +0100451func Test_window_jump_tag()
452 help
453 /iccf
454 call assert_match('^|iccf|', getline('.'))
455 call assert_equal(2, winnr('$'))
456 2wincmd }
457 call assert_equal(3, winnr('$'))
458 call assert_match('^|iccf|', getline('.'))
459 wincmd k
460 call assert_match('\*iccf\*', getline('.'))
461 call assert_equal(2, winheight(0))
462
463 wincmd z
464 set previewheight=4
465 help
466 /bugs
467 wincmd }
468 wincmd k
469 call assert_match('\*bugs\*', getline('.'))
470 call assert_equal(4, winheight(0))
471 set previewheight&
472
473 %bw!
474endfunc
475
476func Test_window_newtab()
477 e Xa
478
479 call assert_equal(1, tabpagenr('$'))
480 call assert_equal("\nAlready only one window", execute('wincmd T'))
481
482 split Xb
483 split Xc
484
485 wincmd T
486 call assert_equal(2, tabpagenr('$'))
487 call assert_equal(['Xb', 'Xa'], map(tabpagebuflist(1), 'bufname(v:val)'))
488 call assert_equal(['Xc' ], map(tabpagebuflist(2), 'bufname(v:val)'))
489
490 %bw!
491endfunc
492
Bram Moolenaarf79225e2017-03-18 23:11:04 +0100493func Test_next_split_all()
494 " This was causing an illegal memory access.
495 n x
496 norm axxx
497 split
498 split
499 s/x
500 s/x
501 all
502 bwipe!
503endfunc
504
Bram Moolenaar75373f32017-08-07 22:02:30 +0200505" Tests for adjusting window and contents
506func GetScreenStr(row)
507 let str = ""
508 for c in range(1,3)
509 let str .= nr2char(screenchar(a:row, c))
510 endfor
511 return str
512endfunc
513
514func Test_window_contents()
515 enew! | only | new
516 call setline(1, range(1,256))
517
518 exe "norm! \<C-W>t\<C-W>=1Gzt\<C-W>w\<C-W>+"
519 redraw
Bram Moolenaar72cf47a2018-05-10 18:23:29 +0200520 let s3 = GetScreenStr(1)
Bram Moolenaar75373f32017-08-07 22:02:30 +0200521 wincmd p
522 call assert_equal(1, line("w0"))
523 call assert_equal('1 ', s3)
524
525 exe "norm! \<C-W>t\<C-W>=50Gzt\<C-W>w\<C-W>+"
526 redraw
Bram Moolenaar72cf47a2018-05-10 18:23:29 +0200527 let s3 = GetScreenStr(1)
Bram Moolenaar75373f32017-08-07 22:02:30 +0200528 wincmd p
529 call assert_equal(50, line("w0"))
530 call assert_equal('50 ', s3)
531
532 exe "norm! \<C-W>t\<C-W>=59Gzt\<C-W>w\<C-W>+"
533 redraw
Bram Moolenaar72cf47a2018-05-10 18:23:29 +0200534 let s3 = GetScreenStr(1)
Bram Moolenaar75373f32017-08-07 22:02:30 +0200535 wincmd p
536 call assert_equal(59, line("w0"))
537 call assert_equal('59 ', s3)
538
539 bwipeout!
540 call test_garbagecollect_now()
541endfunc
542
Bram Moolenaar2efb3232017-12-19 12:27:23 +0100543func Test_window_colon_command()
544 " This was reading invalid memory.
545 exe "norm! v\<C-W>:\<C-U>echo v:version"
546endfunc
547
Bram Moolenaar6f361c92018-01-31 19:06:50 +0100548func Test_access_freed_mem()
Bram Moolenaar1417c762019-07-27 17:31:36 +0200549 call assert_equal(&columns, winwidth(0))
Bram Moolenaar6f361c92018-01-31 19:06:50 +0100550 " This was accessing freed memory
551 au * 0 vs xxx
552 arg 0
553 argadd
Bram Moolenaar1417c762019-07-27 17:31:36 +0200554 call assert_fails("all", "E249:")
Bram Moolenaar6f361c92018-01-31 19:06:50 +0100555 au!
556 bwipe xxx
Bram Moolenaar1417c762019-07-27 17:31:36 +0200557 call assert_equal(&columns, winwidth(0))
Bram Moolenaar6f361c92018-01-31 19:06:50 +0100558endfunc
559
Bram Moolenaar5bab5552018-04-13 20:41:29 +0200560func Test_visual_cleared_after_window_split()
561 new | only!
562 let smd_save = &showmode
563 set showmode
564 let ls_save = &laststatus
565 set laststatus=1
566 call setline(1, ['a', 'b', 'c', 'd', ''])
567 norm! G
568 exe "norm! kkvk"
569 redraw
570 exe "norm! \<C-W>v"
571 redraw
572 " check if '-- VISUAL --' disappeared from command line
573 let columns = range(1, &columns)
574 let cmdlinechars = map(columns, 'nr2char(screenchar(&lines, v:val))')
575 let cmdline = join(cmdlinechars, '')
576 let cmdline_ltrim = substitute(cmdline, '^\s*', "", "")
577 let mode_shown = substitute(cmdline_ltrim, '\s*$', "", "")
578 call assert_equal('', mode_shown)
579 let &showmode = smd_save
580 let &laststatus = ls_save
581 bwipe!
582endfunc
583
Bram Moolenaar72cf47a2018-05-10 18:23:29 +0200584func Test_winrestcmd()
585 2split
586 3vsplit
587 let a = winrestcmd()
588 call assert_equal(2, winheight(0))
589 call assert_equal(3, winwidth(0))
590 wincmd =
591 call assert_notequal(2, winheight(0))
592 call assert_notequal(3, winwidth(0))
593 exe a
594 call assert_equal(2, winheight(0))
595 call assert_equal(3, winwidth(0))
596 only
597endfunc
598
Bram Moolenaar1e115362019-01-09 23:01:02 +0100599func Fun_RenewFile()
Bram Moolenaar026587b2019-08-17 15:08:00 +0200600 " Need to wait a bit for the timestamp to be older.
Bram Moolenaara42df592018-12-24 00:22:39 +0100601 sleep 2
602 silent execute '!echo "1" > tmp.txt'
603 sp
604 wincmd p
605 edit! tmp.txt
Bram Moolenaar1e115362019-01-09 23:01:02 +0100606endfunc
Bram Moolenaara42df592018-12-24 00:22:39 +0100607
608func Test_window_prevwin()
609 " Can we make this work on MS-Windows?
610 if !has('unix')
611 return
612 endif
613
614 set hidden autoread
615 call writefile(['2'], 'tmp.txt')
616 new tmp.txt
617 q
Bram Moolenaara42df592018-12-24 00:22:39 +0100618 call Fun_RenewFile()
619 call assert_equal(2, winnr())
620 wincmd p
621 call assert_equal(1, winnr())
622 wincmd p
623 q
624 call Fun_RenewFile()
625 call assert_equal(2, winnr())
626 wincmd p
627 call assert_equal(1, winnr())
628 wincmd p
629 " reset
630 q
631 call delete('tmp.txt')
632 set hidden&vim autoread&vim
633 delfunc Fun_RenewFile
634endfunc
635
Bram Moolenaar8fcb60f2019-03-04 13:18:30 +0100636func Test_relative_cursor_position_in_one_line_window()
637 new
638 only
639 call setline(1, range(1, 10000))
640 normal 50%
641 let lnum = getcurpos()[1]
642 split
643 split
644 " make third window take as many lines as possible, other windows will
645 " become one line
646 3wincmd w
647 for i in range(1, &lines - 6)
648 wincmd +
649 redraw!
650 endfor
651
652 " first and second window should show cursor line
653 let wininfo = getwininfo()
654 call assert_equal(lnum, wininfo[0].topline)
655 call assert_equal(lnum, wininfo[1].topline)
656
657 only!
658 bwipe!
659endfunc
660
661func Test_relative_cursor_position_after_move_and_resize()
662 let so_save = &so
663 set so=0
664 enew
665 call setline(1, range(1, 10000))
666 normal 50%
667 split
668 1wincmd w
669 " Move cursor to first line in window
670 normal H
671 redraw!
672 " Reduce window height to two lines
673 let height = winheight(0)
674 while winheight(0) > 2
675 wincmd -
676 redraw!
677 endwhile
678 " move cursor to second/last line in window
679 normal j
680 " restore previous height
681 while winheight(0) < height
682 wincmd +
683 redraw!
684 endwhile
685 " make window two lines again
686 while winheight(0) > 2
687 wincmd -
688 redraw!
689 endwhile
690
691 " cursor should be at bottom line
692 let info = getwininfo(win_getid())[0]
693 call assert_equal(info.topline + 1, getcurpos()[1])
694
695 only!
696 bwipe!
697 let &so = so_save
698endfunc
699
700func Test_relative_cursor_position_after_resize()
701 let so_save = &so
702 set so=0
703 enew
704 call setline(1, range(1, 10000))
705 normal 50%
706 split
707 1wincmd w
708 let winid1 = win_getid()
709 let info = getwininfo(winid1)[0]
710 " Move cursor to second line in window
711 exe "normal " . (info.topline + 1) . "G"
712 redraw!
713 let lnum = getcurpos()[1]
714
715 " Make the window only two lines high, cursor should end up in top line
716 2wincmd w
717 exe (info.height - 2) . "wincmd +"
718 redraw!
719 let info = getwininfo(winid1)[0]
720 call assert_equal(lnum, info.topline)
721
722 only!
723 bwipe!
724 let &so = so_save
725endfunc
726
727func Test_relative_cursor_second_line_after_resize()
728 let so_save = &so
729 set so=0
730 enew
731 call setline(1, range(1, 10000))
732 normal 50%
733 split
734 1wincmd w
735 let winid1 = win_getid()
736 let info = getwininfo(winid1)[0]
737
738 " Make the window only two lines high
739 2wincmd _
740
741 " Move cursor to second line in window
742 normal H
743 normal j
744
745 " Make window size bigger, then back to 2 lines
746 for i in range(1, 10)
747 wincmd +
748 redraw!
749 endfor
750 for i in range(1, 10)
751 wincmd -
752 redraw!
753 endfor
754
755 " cursor should end up in bottom line
756 let info = getwininfo(winid1)[0]
757 call assert_equal(info.topline + 1, getcurpos()[1])
758
759 only!
760 bwipe!
761 let &so = so_save
762endfunc
763
Bram Moolenaara9b25352019-05-12 14:25:30 +0200764func Test_split_noscroll()
765 let so_save = &so
Bram Moolenaarbd2d68c2019-05-18 15:36:11 +0200766 enew
767 call setline(1, range(1, 8))
Bram Moolenaara9b25352019-05-12 14:25:30 +0200768 normal 100%
769 split
770
771 1wincmd w
772 let winid1 = win_getid()
773 let info1 = getwininfo(winid1)[0]
774
775 2wincmd w
776 let winid2 = win_getid()
777 let info2 = getwininfo(winid2)[0]
778
779 call assert_equal(1, info1.topline)
780 call assert_equal(1, info2.topline)
781
Bram Moolenaarbd2d68c2019-05-18 15:36:11 +0200782 " window that fits all lines by itself, but not when split: closing other
783 " window should restore fraction.
Bram Moolenaara9b25352019-05-12 14:25:30 +0200784 only!
Bram Moolenaarbd2d68c2019-05-18 15:36:11 +0200785 call setline(1, range(1, &lines - 10))
786 exe &lines / 4
787 let winid1 = win_getid()
788 let info1 = getwininfo(winid1)[0]
789 call assert_equal(1, info1.topline)
790 new
791 redraw
792 close
793 let info1 = getwininfo(winid1)[0]
794 call assert_equal(1, info1.topline)
795
Bram Moolenaara9b25352019-05-12 14:25:30 +0200796 bwipe!
797 let &so = so_save
798endfunc
799
Bram Moolenaar46ad2882019-04-08 20:01:47 +0200800" Tests for the winnr() function
801func Test_winnr()
802 only | tabonly
803 call assert_equal(1, winnr('j'))
804 call assert_equal(1, winnr('k'))
805 call assert_equal(1, winnr('h'))
806 call assert_equal(1, winnr('l'))
807
808 " create a set of horizontally and vertically split windows
809 leftabove new | wincmd p
810 leftabove new | wincmd p
811 rightbelow new | wincmd p
812 rightbelow new | wincmd p
813 leftabove vnew | wincmd p
814 leftabove vnew | wincmd p
815 rightbelow vnew | wincmd p
816 rightbelow vnew | wincmd p
817
818 call assert_equal(8, winnr('j'))
819 call assert_equal(2, winnr('k'))
820 call assert_equal(4, winnr('h'))
821 call assert_equal(6, winnr('l'))
822 call assert_equal(9, winnr('2j'))
823 call assert_equal(1, winnr('2k'))
824 call assert_equal(3, winnr('2h'))
825 call assert_equal(7, winnr('2l'))
826
827 " Error cases
828 call assert_fails("echo winnr('0.2k')", 'E15:')
829 call assert_equal(2, winnr('-2k'))
830 call assert_fails("echo winnr('-2xj')", 'E15:')
831 call assert_fails("echo winnr('j2j')", 'E15:')
832 call assert_fails("echo winnr('ll')", 'E15:')
833 call assert_fails("echo winnr('5')", 'E15:')
834 call assert_equal(4, winnr('0h'))
835
836 tabnew
837 call assert_equal(8, tabpagewinnr(1, 'j'))
838 call assert_equal(2, tabpagewinnr(1, 'k'))
839 call assert_equal(4, tabpagewinnr(1, 'h'))
840 call assert_equal(6, tabpagewinnr(1, 'l'))
841
842 only | tabonly
843endfunc
844
Bram Moolenaar9e4d8212016-08-18 23:04:48 +0200845" vim: shiftwidth=2 sts=2 expandtab