blob: f2a9f1797a976bd1247bfdccf99f670ed68a48e7 [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)'))
Bram Moolenaarce90e362019-09-08 18:58:44 +0200488 call assert_equal(['Xc' ], map(2->tabpagebuflist(), 'bufname(v:val)'))
Bram Moolenaar4520d442017-03-19 16:09:46 +0100489
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 Moolenaarce90e362019-09-08 18:58:44 +0200601 let old_ftime = getftime("tmp.txt")
602 while getftime("tmp.txt") == old_ftime
603 sleep 100m
604 silent execute '!echo "1" > tmp.txt'
605 endwhile
Bram Moolenaara42df592018-12-24 00:22:39 +0100606 sp
607 wincmd p
608 edit! tmp.txt
Bram Moolenaar1e115362019-01-09 23:01:02 +0100609endfunc
Bram Moolenaara42df592018-12-24 00:22:39 +0100610
611func Test_window_prevwin()
612 " Can we make this work on MS-Windows?
613 if !has('unix')
614 return
615 endif
616
617 set hidden autoread
618 call writefile(['2'], 'tmp.txt')
619 new tmp.txt
620 q
Bram Moolenaara42df592018-12-24 00:22:39 +0100621 call Fun_RenewFile()
622 call assert_equal(2, winnr())
623 wincmd p
624 call assert_equal(1, winnr())
625 wincmd p
626 q
627 call Fun_RenewFile()
628 call assert_equal(2, winnr())
629 wincmd p
630 call assert_equal(1, winnr())
631 wincmd p
632 " reset
633 q
634 call delete('tmp.txt')
635 set hidden&vim autoread&vim
636 delfunc Fun_RenewFile
637endfunc
638
Bram Moolenaar8fcb60f2019-03-04 13:18:30 +0100639func Test_relative_cursor_position_in_one_line_window()
640 new
641 only
642 call setline(1, range(1, 10000))
643 normal 50%
644 let lnum = getcurpos()[1]
645 split
646 split
647 " make third window take as many lines as possible, other windows will
648 " become one line
649 3wincmd w
650 for i in range(1, &lines - 6)
651 wincmd +
652 redraw!
653 endfor
654
655 " first and second window should show cursor line
656 let wininfo = getwininfo()
657 call assert_equal(lnum, wininfo[0].topline)
658 call assert_equal(lnum, wininfo[1].topline)
659
660 only!
661 bwipe!
662endfunc
663
664func Test_relative_cursor_position_after_move_and_resize()
665 let so_save = &so
666 set so=0
667 enew
668 call setline(1, range(1, 10000))
669 normal 50%
670 split
671 1wincmd w
672 " Move cursor to first line in window
673 normal H
674 redraw!
675 " Reduce window height to two lines
676 let height = winheight(0)
677 while winheight(0) > 2
678 wincmd -
679 redraw!
680 endwhile
681 " move cursor to second/last line in window
682 normal j
683 " restore previous height
684 while winheight(0) < height
685 wincmd +
686 redraw!
687 endwhile
688 " make window two lines again
689 while winheight(0) > 2
690 wincmd -
691 redraw!
692 endwhile
693
694 " cursor should be at bottom line
695 let info = getwininfo(win_getid())[0]
696 call assert_equal(info.topline + 1, getcurpos()[1])
697
698 only!
699 bwipe!
700 let &so = so_save
701endfunc
702
703func Test_relative_cursor_position_after_resize()
704 let so_save = &so
705 set so=0
706 enew
707 call setline(1, range(1, 10000))
708 normal 50%
709 split
710 1wincmd w
711 let winid1 = win_getid()
712 let info = getwininfo(winid1)[0]
713 " Move cursor to second line in window
714 exe "normal " . (info.topline + 1) . "G"
715 redraw!
716 let lnum = getcurpos()[1]
717
718 " Make the window only two lines high, cursor should end up in top line
719 2wincmd w
720 exe (info.height - 2) . "wincmd +"
721 redraw!
722 let info = getwininfo(winid1)[0]
723 call assert_equal(lnum, info.topline)
724
725 only!
726 bwipe!
727 let &so = so_save
728endfunc
729
730func Test_relative_cursor_second_line_after_resize()
731 let so_save = &so
732 set so=0
733 enew
734 call setline(1, range(1, 10000))
735 normal 50%
736 split
737 1wincmd w
738 let winid1 = win_getid()
739 let info = getwininfo(winid1)[0]
740
741 " Make the window only two lines high
742 2wincmd _
743
744 " Move cursor to second line in window
745 normal H
746 normal j
747
748 " Make window size bigger, then back to 2 lines
749 for i in range(1, 10)
750 wincmd +
751 redraw!
752 endfor
753 for i in range(1, 10)
754 wincmd -
755 redraw!
756 endfor
757
758 " cursor should end up in bottom line
759 let info = getwininfo(winid1)[0]
760 call assert_equal(info.topline + 1, getcurpos()[1])
761
762 only!
763 bwipe!
764 let &so = so_save
765endfunc
766
Bram Moolenaara9b25352019-05-12 14:25:30 +0200767func Test_split_noscroll()
768 let so_save = &so
Bram Moolenaarbd2d68c2019-05-18 15:36:11 +0200769 enew
770 call setline(1, range(1, 8))
Bram Moolenaara9b25352019-05-12 14:25:30 +0200771 normal 100%
772 split
773
774 1wincmd w
775 let winid1 = win_getid()
776 let info1 = getwininfo(winid1)[0]
777
778 2wincmd w
779 let winid2 = win_getid()
780 let info2 = getwininfo(winid2)[0]
781
782 call assert_equal(1, info1.topline)
783 call assert_equal(1, info2.topline)
784
Bram Moolenaarbd2d68c2019-05-18 15:36:11 +0200785 " window that fits all lines by itself, but not when split: closing other
786 " window should restore fraction.
Bram Moolenaara9b25352019-05-12 14:25:30 +0200787 only!
Bram Moolenaarbd2d68c2019-05-18 15:36:11 +0200788 call setline(1, range(1, &lines - 10))
789 exe &lines / 4
790 let winid1 = win_getid()
791 let info1 = getwininfo(winid1)[0]
792 call assert_equal(1, info1.topline)
793 new
794 redraw
795 close
796 let info1 = getwininfo(winid1)[0]
797 call assert_equal(1, info1.topline)
798
Bram Moolenaara9b25352019-05-12 14:25:30 +0200799 bwipe!
800 let &so = so_save
801endfunc
802
Bram Moolenaar46ad2882019-04-08 20:01:47 +0200803" Tests for the winnr() function
804func Test_winnr()
805 only | tabonly
806 call assert_equal(1, winnr('j'))
807 call assert_equal(1, winnr('k'))
808 call assert_equal(1, winnr('h'))
809 call assert_equal(1, winnr('l'))
810
811 " create a set of horizontally and vertically split windows
812 leftabove new | wincmd p
813 leftabove new | wincmd p
814 rightbelow new | wincmd p
815 rightbelow new | wincmd p
816 leftabove vnew | wincmd p
817 leftabove vnew | wincmd p
818 rightbelow vnew | wincmd p
819 rightbelow vnew | wincmd p
820
821 call assert_equal(8, winnr('j'))
822 call assert_equal(2, winnr('k'))
823 call assert_equal(4, winnr('h'))
824 call assert_equal(6, winnr('l'))
825 call assert_equal(9, winnr('2j'))
826 call assert_equal(1, winnr('2k'))
827 call assert_equal(3, winnr('2h'))
828 call assert_equal(7, winnr('2l'))
829
830 " Error cases
831 call assert_fails("echo winnr('0.2k')", 'E15:')
832 call assert_equal(2, winnr('-2k'))
833 call assert_fails("echo winnr('-2xj')", 'E15:')
834 call assert_fails("echo winnr('j2j')", 'E15:')
835 call assert_fails("echo winnr('ll')", 'E15:')
836 call assert_fails("echo winnr('5')", 'E15:')
837 call assert_equal(4, winnr('0h'))
838
839 tabnew
840 call assert_equal(8, tabpagewinnr(1, 'j'))
Bram Moolenaarce90e362019-09-08 18:58:44 +0200841 call assert_equal(2, 1->tabpagewinnr('k'))
Bram Moolenaar46ad2882019-04-08 20:01:47 +0200842 call assert_equal(4, tabpagewinnr(1, 'h'))
843 call assert_equal(6, tabpagewinnr(1, 'l'))
844
845 only | tabonly
846endfunc
847
Bram Moolenaar9e4d8212016-08-18 23:04:48 +0200848" vim: shiftwidth=2 sts=2 expandtab