blob: e38c8a8107897f20cf65a74918cea658d40ccdca [file] [log] [blame]
Bram Moolenaar991dea32016-05-24 11:31:32 +02001" Tests for window cmd (:wincmd, :split, :vsplit, :resize and etc...)
2
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +02003source check.vim
Bram Moolenaara27e1dc2019-10-07 22:27:36 +02004
Bram Moolenaar991dea32016-05-24 11:31:32 +02005func 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()
Bram Moolenaar21829c52021-01-26 22:42:21 +010022 CheckFeature cmdwin
23
Bram Moolenaar72cf47a2018-05-10 18:23:29 +020024 let efmt = 'Expected 0 but got %d (in ls=%d, %s window)'
Bram Moolenaar991dea32016-05-24 11:31:32 +020025 for v in range(0, 2)
26 exec "set ls=" . v
27 vsplit
28 call feedkeys("q:\<CR>")
29 let ac = &lines - (&cmdheight + winheight(0) + !!v)
30 let emsg = printf(efmt, ac, v, 'left')
31 call assert_equal(0, ac, emsg)
32 wincmd w
33 let ac = &lines - (&cmdheight + winheight(0) + !!v)
34 let emsg = printf(efmt, ac, v, 'right')
35 call assert_equal(0, ac, emsg)
36 new | only!
37 endfor
38 set ls&vim
39endfunc
40
Bram Moolenaar96bde992022-08-10 17:23:12 +010041func Test_cmdheight_not_changed()
42 set cmdheight=2
43 set winminheight=0
44 augroup Maximize
45 autocmd WinEnter * wincmd _
46 augroup END
47 split
48 tabnew
49 tabfirst
50 call assert_equal(2, &cmdheight)
51
52 tabonly!
53 only
54 set winminwidth& cmdheight&
55 augroup Maximize
56 au!
57 augroup END
58 augroup! Maximize
59endfunc
60
Bram Moolenaar5d3c9f82020-06-26 20:41:39 +020061" Test for jumping to windows
62func Test_window_jump()
63 new
64 " jumping to a window with a count greater than the max windows
65 exe "normal 4\<C-W>w"
66 call assert_equal(2, winnr())
67 only
68endfunc
69
70func Test_window_cmd_wincmd_gf()
Bram Moolenaar5d2ca042016-06-26 17:11:21 +020071 let fname = 'test_gf.txt'
72 let swp_fname = '.' . fname . '.swp'
73 call writefile([], fname)
74 call writefile([], swp_fname)
75 function s:swap_exists()
76 let v:swapchoice = s:swap_choice
77 endfunc
Bram Moolenaareaa49e42019-07-13 18:08:59 +020078 " Remove the catch-all that runtest.vim adds
79 au! SwapExists
Bram Moolenaar5d2ca042016-06-26 17:11:21 +020080 augroup test_window_cmd_wincmd_gf
81 autocmd!
82 exec "autocmd SwapExists " . fname . " call s:swap_exists()"
83 augroup END
84
85 call setline(1, fname)
86 " (E)dit anyway
87 let s:swap_choice = 'e'
88 wincmd gf
89 call assert_equal(2, tabpagenr())
90 call assert_equal(fname, bufname("%"))
91 quit!
92
93 " (Q)uit
94 let s:swap_choice = 'q'
95 wincmd gf
96 call assert_equal(1, tabpagenr())
97 call assert_notequal(fname, bufname("%"))
98 new | only!
99
100 call delete(fname)
101 call delete(swp_fname)
102 augroup! test_window_cmd_wincmd_gf
103endfunc
104
Bram Moolenaar4520d442017-03-19 16:09:46 +0100105func Test_window_quit()
106 e Xa
107 split Xb
Bram Moolenaarf92e58c2019-09-08 21:51:41 +0200108 call assert_equal(2, '$'->winnr())
Bram Moolenaar4520d442017-03-19 16:09:46 +0100109 call assert_equal('Xb', bufname(winbufnr(1)))
110 call assert_equal('Xa', bufname(winbufnr(2)))
111
112 wincmd q
113 call assert_equal(1, winnr('$'))
114 call assert_equal('Xa', bufname(winbufnr(1)))
115
116 bw Xa Xb
117endfunc
118
119func Test_window_horizontal_split()
120 call assert_equal(1, winnr('$'))
121 3wincmd s
122 call assert_equal(2, winnr('$'))
123 call assert_equal(3, winheight(0))
Bram Moolenaarf92e58c2019-09-08 21:51:41 +0200124 call assert_equal(winwidth(1), 2->winwidth())
Bram Moolenaar4520d442017-03-19 16:09:46 +0100125
126 call assert_fails('botright topleft wincmd s', 'E442:')
127 bw
128endfunc
129
130func Test_window_vertical_split()
131 call assert_equal(1, winnr('$'))
132 3wincmd v
133 call assert_equal(2, winnr('$'))
134 call assert_equal(3, winwidth(0))
135 call assert_equal(winheight(1), winheight(2))
136
137 call assert_fails('botright topleft wincmd v', 'E442:')
138 bw
139endfunc
140
Bram Moolenaar1bbb6192018-11-10 16:02:01 +0100141" Test the ":wincmd ^" and "<C-W>^" commands.
Bram Moolenaar4520d442017-03-19 16:09:46 +0100142func Test_window_split_edit_alternate()
Bram Moolenaar4520d442017-03-19 16:09:46 +0100143
Bram Moolenaar1bbb6192018-11-10 16:02:01 +0100144 " Test for failure when the alternate buffer/file no longer exists.
145 edit Xfoo | %bw
Bram Moolenaare2e40752020-09-04 21:18:46 +0200146 call assert_fails(':wincmd ^', 'E23:')
Bram Moolenaar1bbb6192018-11-10 16:02:01 +0100147
148 " Test for the expected behavior when we have two named buffers.
149 edit Xfoo | edit Xbar
Bram Moolenaar4520d442017-03-19 16:09:46 +0100150 wincmd ^
Bram Moolenaar1bbb6192018-11-10 16:02:01 +0100151 call assert_equal('Xfoo', bufname(winbufnr(1)))
152 call assert_equal('Xbar', bufname(winbufnr(2)))
153 only
Bram Moolenaar4520d442017-03-19 16:09:46 +0100154
Bram Moolenaar1bbb6192018-11-10 16:02:01 +0100155 " Test for the expected behavior when the alternate buffer is not named.
156 enew | let l:nr1 = bufnr('%')
157 edit Xfoo | let l:nr2 = bufnr('%')
158 wincmd ^
159 call assert_equal(l:nr1, winbufnr(1))
160 call assert_equal(l:nr2, winbufnr(2))
161 only
162
Bram Moolenaard42333d2018-11-10 20:28:19 +0100163 " FIXME: this currently fails on AppVeyor, but passes locally
164 if !has('win32')
165 " Test the Normal mode command.
166 call feedkeys("\<C-W>\<C-^>", 'tx')
167 call assert_equal(l:nr2, winbufnr(1))
168 call assert_equal(l:nr1, winbufnr(2))
169 endif
Bram Moolenaar1bbb6192018-11-10 16:02:01 +0100170
171 %bw!
172endfunc
173
174" Test the ":[count]wincmd ^" and "[count]<C-W>^" commands.
175func Test_window_split_edit_bufnr()
176
177 %bwipeout
178 let l:nr = bufnr('%') + 1
Bram Moolenaare2e40752020-09-04 21:18:46 +0200179 call assert_fails(':execute "normal! ' . l:nr . '\<C-W>\<C-^>"', 'E92:')
180 call assert_fails(':' . l:nr . 'wincmd ^', 'E16:')
181 call assert_fails(':0wincmd ^', 'E16:')
Bram Moolenaar1bbb6192018-11-10 16:02:01 +0100182
183 edit Xfoo | edit Xbar | edit Xbaz
184 let l:foo_nr = bufnr('Xfoo')
185 let l:bar_nr = bufnr('Xbar')
186 let l:baz_nr = bufnr('Xbaz')
187
Bram Moolenaar8617b402018-11-10 20:47:48 +0100188 " FIXME: this currently fails on AppVeyor, but passes locally
189 if !has('win32')
190 call feedkeys(l:foo_nr . "\<C-W>\<C-^>", 'tx')
191 call assert_equal('Xfoo', bufname(winbufnr(1)))
192 call assert_equal('Xbaz', bufname(winbufnr(2)))
193 only
Bram Moolenaar1bbb6192018-11-10 16:02:01 +0100194
Bram Moolenaar8617b402018-11-10 20:47:48 +0100195 call feedkeys(l:bar_nr . "\<C-W>\<C-^>", 'tx')
196 call assert_equal('Xbar', bufname(winbufnr(1)))
197 call assert_equal('Xfoo', bufname(winbufnr(2)))
198 only
Bram Moolenaar1bbb6192018-11-10 16:02:01 +0100199
Bram Moolenaar8617b402018-11-10 20:47:48 +0100200 execute l:baz_nr . 'wincmd ^'
201 call assert_equal('Xbaz', bufname(winbufnr(1)))
202 call assert_equal('Xbar', bufname(winbufnr(2)))
203 endif
Bram Moolenaar1bbb6192018-11-10 16:02:01 +0100204
205 %bw!
Bram Moolenaar4520d442017-03-19 16:09:46 +0100206endfunc
207
Bram Moolenaar9b9be002020-03-22 14:41:22 +0100208func Test_window_split_no_room()
209 " N horizontal windows need >= 2*N + 1 lines:
210 " - 1 line + 1 status line in each window
211 " - 1 Ex command line
212 "
213 " 2*N + 1 <= &lines
214 " N <= (lines - 1)/2
215 "
216 " Beyond that number of windows, E36: Not enough room is expected.
217 let hor_win_count = (&lines - 1)/2
218 let hor_split_count = hor_win_count - 1
219 for s in range(1, hor_split_count) | split | endfor
220 call assert_fails('split', 'E36:')
221
222 " N vertical windows need >= 2*(N - 1) + 1 columns:
223 " - 1 column + 1 separator for each window (except last window)
224 " - 1 column for the last window which does not have separator
225 "
226 " 2*(N - 1) + 1 <= &columns
227 " 2*N - 1 <= &columns
228 " N <= (&columns + 1)/2
229 let ver_win_count = (&columns + 1)/2
230 let ver_split_count = ver_win_count - 1
231 for s in range(1, ver_split_count) | vsplit | endfor
232 call assert_fails('vsplit', 'E36:')
233
234 %bw!
235endfunc
236
Bram Moolenaar4520d442017-03-19 16:09:46 +0100237func Test_window_exchange()
238 e Xa
239
240 " Nothing happens with window exchange when there is 1 window
241 wincmd x
242 call assert_equal(1, winnr('$'))
243
244 split Xb
245 split Xc
246
247 call assert_equal('Xc', bufname(winbufnr(1)))
248 call assert_equal('Xb', bufname(winbufnr(2)))
249 call assert_equal('Xa', bufname(winbufnr(3)))
250
251 " Exchange current window 1 with window 3
252 3wincmd x
253 call assert_equal('Xa', bufname(winbufnr(1)))
254 call assert_equal('Xb', bufname(winbufnr(2)))
255 call assert_equal('Xc', bufname(winbufnr(3)))
256
257 " Exchange window with next when at the top window
258 wincmd x
259 call assert_equal('Xb', bufname(winbufnr(1)))
260 call assert_equal('Xa', bufname(winbufnr(2)))
261 call assert_equal('Xc', bufname(winbufnr(3)))
262
263 " Exchange window with next when at the middle window
264 wincmd j
265 wincmd x
266 call assert_equal('Xb', bufname(winbufnr(1)))
267 call assert_equal('Xc', bufname(winbufnr(2)))
268 call assert_equal('Xa', bufname(winbufnr(3)))
269
270 " Exchange window with next when at the bottom window.
271 " When there is no next window, it exchanges with the previous window.
272 wincmd j
273 wincmd x
274 call assert_equal('Xb', bufname(winbufnr(1)))
275 call assert_equal('Xa', bufname(winbufnr(2)))
276 call assert_equal('Xc', bufname(winbufnr(3)))
277
278 bw Xa Xb Xc
279endfunc
280
281func Test_window_rotate()
282 e Xa
283 split Xb
284 split Xc
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 " Rotate downwards
290 wincmd r
291 call assert_equal('Xa', bufname(winbufnr(1)))
292 call assert_equal('Xc', bufname(winbufnr(2)))
293 call assert_equal('Xb', bufname(winbufnr(3)))
294
295 2wincmd r
296 call assert_equal('Xc', bufname(winbufnr(1)))
297 call assert_equal('Xb', bufname(winbufnr(2)))
298 call assert_equal('Xa', bufname(winbufnr(3)))
299
300 " Rotate upwards
301 wincmd R
302 call assert_equal('Xb', bufname(winbufnr(1)))
303 call assert_equal('Xa', bufname(winbufnr(2)))
304 call assert_equal('Xc', bufname(winbufnr(3)))
305
306 2wincmd R
307 call assert_equal('Xc', bufname(winbufnr(1)))
308 call assert_equal('Xb', bufname(winbufnr(2)))
309 call assert_equal('Xa', bufname(winbufnr(3)))
310
311 bot vsplit
312 call assert_fails('wincmd R', 'E443:')
313
314 bw Xa Xb Xc
315endfunc
316
317func Test_window_height()
318 e Xa
319 split Xb
320
321 let [wh1, wh2] = [winheight(1), winheight(2)]
322 " Active window (1) should have the same height or 1 more
323 " than the other window.
324 call assert_inrange(wh2, wh2 + 1, wh1)
325
326 wincmd -
327 call assert_equal(wh1 - 1, winheight(1))
328 call assert_equal(wh2 + 1, winheight(2))
329
330 wincmd +
331 call assert_equal(wh1, winheight(1))
Bram Moolenaarf92e58c2019-09-08 21:51:41 +0200332 call assert_equal(wh2, 2->winheight())
Bram Moolenaar4520d442017-03-19 16:09:46 +0100333
334 2wincmd _
335 call assert_equal(2, winheight(1))
336 call assert_equal(wh1 + wh2 - 2, winheight(2))
337
338 wincmd =
339 call assert_equal(wh1, winheight(1))
340 call assert_equal(wh2, winheight(2))
341
342 2wincmd _
343 set winfixheight
344 split Xc
345 let [wh1, wh2, wh3] = [winheight(1), winheight(2), winheight(3)]
346 call assert_equal(2, winheight(2))
347 call assert_inrange(wh3, wh3 + 1, wh1)
348 3wincmd +
349 call assert_equal(2, winheight(2))
350 call assert_equal(wh1 + 3, winheight(1))
351 call assert_equal(wh3 - 3, winheight(3))
352 wincmd =
353 call assert_equal(2, winheight(2))
354 call assert_equal(wh1, winheight(1))
355 call assert_equal(wh3, winheight(3))
356
357 wincmd j
358 set winfixheight&
359
360 wincmd =
361 let [wh1, wh2, wh3] = [winheight(1), winheight(2), winheight(3)]
362 " Current window (2) should have the same height or 1 more
363 " than the other windows.
364 call assert_inrange(wh1, wh1 + 1, wh2)
365 call assert_inrange(wh3, wh3 + 1, wh2)
366
367 bw Xa Xb Xc
368endfunc
369
Bram Moolenaar21c3a802022-08-31 17:49:14 +0100370func Test_wincmd_equal()
371 edit Xone
372 below split Xtwo
373 rightbelow vsplit Xthree
374 call assert_equal('Xone', bufname(winbufnr(1)))
375 call assert_equal('Xtwo', bufname(winbufnr(2)))
376 call assert_equal('Xthree', bufname(winbufnr(3)))
377
378 " Xone and Xtwo should be about the same height
379 let [wh1, wh2] = [winheight(1), winheight(2)]
380 call assert_inrange(wh1 - 1, wh1 + 1, wh2)
381 " Xtwo and Xthree should be about the same width
382 let [ww2, ww3] = [winwidth(2), winwidth(3)]
383 call assert_inrange(ww2 - 1, ww2 + 1, ww3)
384
385 1wincmd w
386 10wincmd _
387 2wincmd w
388 20wincmd |
389 call assert_equal(10, winheight(1))
390 call assert_equal(20, winwidth(2))
391
392 " equalizing horizontally doesn't change the heights
393 hor wincmd =
394 call assert_equal(10, winheight(1))
395 let [ww2, ww3] = [winwidth(2), winwidth(3)]
396 call assert_inrange(ww2 - 1, ww2 + 1, ww3)
397
398 2wincmd w
399 20wincmd |
400 call assert_equal(20, winwidth(2))
401 " equalizing vertically doesn't change the widths
402 vert wincmd =
403 call assert_equal(20, winwidth(2))
404 let [wh1, wh2] = [winheight(1), winheight(2)]
405 call assert_inrange(wh1 - 1, wh1 + 1, wh2)
406
407 bwipe Xone Xtwo Xthree
408endfunc
409
Bram Moolenaar4520d442017-03-19 16:09:46 +0100410func Test_window_width()
411 e Xa
412 vsplit Xb
413
414 let [ww1, ww2] = [winwidth(1), winwidth(2)]
415 " Active window (1) should have the same width or 1 more
416 " than the other window.
417 call assert_inrange(ww2, ww2 + 1, ww1)
418
419 wincmd <
420 call assert_equal(ww1 - 1, winwidth(1))
421 call assert_equal(ww2 + 1, winwidth(2))
422
423 wincmd >
424 call assert_equal(ww1, winwidth(1))
425 call assert_equal(ww2, winwidth(2))
426
427 2wincmd |
428 call assert_equal(2, winwidth(1))
429 call assert_equal(ww1 + ww2 - 2, winwidth(2))
430
431 wincmd =
432 call assert_equal(ww1, winwidth(1))
433 call assert_equal(ww2, winwidth(2))
434
435 2wincmd |
436 set winfixwidth
437 vsplit Xc
438 let [ww1, ww2, ww3] = [winwidth(1), winwidth(2), winwidth(3)]
Bram Moolenaar38e34832017-03-19 20:22:36 +0100439 call assert_equal(2, winwidth(2))
Bram Moolenaar4520d442017-03-19 16:09:46 +0100440 call assert_inrange(ww3, ww3 + 1, ww1)
441 3wincmd >
Bram Moolenaar38e34832017-03-19 20:22:36 +0100442 call assert_equal(2, winwidth(2))
Bram Moolenaar4520d442017-03-19 16:09:46 +0100443 call assert_equal(ww1 + 3, winwidth(1))
444 call assert_equal(ww3 - 3, winwidth(3))
445 wincmd =
Bram Moolenaar38e34832017-03-19 20:22:36 +0100446 call assert_equal(2, winwidth(2))
Bram Moolenaar4520d442017-03-19 16:09:46 +0100447 call assert_equal(ww1, winwidth(1))
448 call assert_equal(ww3, winwidth(3))
449
450 wincmd l
451 set winfixwidth&
452
453 wincmd =
454 let [ww1, ww2, ww3] = [winwidth(1), winwidth(2), winwidth(3)]
455 " Current window (2) should have the same width or 1 more
456 " than the other windows.
457 call assert_inrange(ww1, ww1 + 1, ww2)
458 call assert_inrange(ww3, ww3 + 1, ww2)
459
Yegappan Lakshmanan59585492021-06-12 13:46:41 +0200460 " when the current window width is less than the new 'winwidth', the current
461 " window width should be increased.
462 enew | only
463 split
464 10vnew
465 set winwidth=15
466 call assert_equal(15, winwidth(0))
467
468 %bw!
Bram Moolenaar4520d442017-03-19 16:09:46 +0100469endfunc
470
Bram Moolenaar8eeeba82017-06-25 22:45:39 +0200471func Test_equalalways_on_close()
472 set equalalways
473 vsplit
474 windo split
Bram Moolenaar1bbb6192018-11-10 16:02:01 +0100475 split
Bram Moolenaar8eeeba82017-06-25 22:45:39 +0200476 wincmd J
477 " now we have a frame top-left with two windows, a frame top-right with two
478 " windows and a frame at the bottom, full-width.
479 let height_1 = winheight(1)
480 let height_2 = winheight(2)
481 let height_3 = winheight(3)
482 let height_4 = winheight(4)
483 " closing the bottom window causes all windows to be resized.
484 close
485 call assert_notequal(height_1, winheight(1))
486 call assert_notequal(height_2, winheight(2))
487 call assert_notequal(height_3, winheight(3))
488 call assert_notequal(height_4, winheight(4))
489 call assert_equal(winheight(1), winheight(3))
490 call assert_equal(winheight(2), winheight(4))
491
492 1wincmd w
493 split
494 4wincmd w
495 resize + 5
496 " left column has three windows, equalized heights.
497 " right column has two windows, top one a bit higher
498 let height_1 = winheight(1)
499 let height_2 = winheight(2)
500 let height_4 = winheight(4)
501 let height_5 = winheight(5)
502 3wincmd w
503 " closing window in left column equalizes heights in left column but not in
504 " the right column
505 close
506 call assert_notequal(height_1, winheight(1))
507 call assert_notequal(height_2, winheight(2))
508 call assert_equal(height_4, winheight(3))
509 call assert_equal(height_5, winheight(4))
510
511 only
512 set equalalways&
513endfunc
514
Bram Moolenaar22044dc2017-12-02 15:43:37 +0100515func Test_win_screenpos()
Bram Moolenaar5a4c3082019-12-01 15:23:11 +0100516 CheckFeature quickfix
517
Bram Moolenaar22044dc2017-12-02 15:43:37 +0100518 call assert_equal(1, winnr('$'))
519 split
520 vsplit
521 10wincmd _
522 30wincmd |
523 call assert_equal([1, 1], win_screenpos(1))
524 call assert_equal([1, 32], win_screenpos(2))
525 call assert_equal([12, 1], win_screenpos(3))
526 call assert_equal([0, 0], win_screenpos(4))
Bram Moolenaar99fa7212020-04-26 15:59:55 +0200527 call assert_fails('let l = win_screenpos([])', 'E745:')
Bram Moolenaar22044dc2017-12-02 15:43:37 +0100528 only
529endfunc
530
Bram Moolenaar4520d442017-03-19 16:09:46 +0100531func Test_window_jump_tag()
Bram Moolenaar5a4c3082019-12-01 15:23:11 +0100532 CheckFeature quickfix
533
Bram Moolenaar4520d442017-03-19 16:09:46 +0100534 help
535 /iccf
536 call assert_match('^|iccf|', getline('.'))
537 call assert_equal(2, winnr('$'))
538 2wincmd }
539 call assert_equal(3, winnr('$'))
540 call assert_match('^|iccf|', getline('.'))
541 wincmd k
542 call assert_match('\*iccf\*', getline('.'))
543 call assert_equal(2, winheight(0))
544
545 wincmd z
546 set previewheight=4
547 help
548 /bugs
549 wincmd }
550 wincmd k
551 call assert_match('\*bugs\*', getline('.'))
552 call assert_equal(4, winheight(0))
553 set previewheight&
554
555 %bw!
556endfunc
557
558func Test_window_newtab()
559 e Xa
560
561 call assert_equal(1, tabpagenr('$'))
562 call assert_equal("\nAlready only one window", execute('wincmd T'))
563
564 split Xb
565 split Xc
566
567 wincmd T
568 call assert_equal(2, tabpagenr('$'))
569 call assert_equal(['Xb', 'Xa'], map(tabpagebuflist(1), 'bufname(v:val)'))
Bram Moolenaarce90e362019-09-08 18:58:44 +0200570 call assert_equal(['Xc' ], map(2->tabpagebuflist(), 'bufname(v:val)'))
Bram Moolenaar0e05de42020-03-25 22:23:46 +0100571 call assert_equal(['Xc' ], map(tabpagebuflist(), 'bufname(v:val)'))
Bram Moolenaar4520d442017-03-19 16:09:46 +0100572
573 %bw!
574endfunc
575
Bram Moolenaarf79225e2017-03-18 23:11:04 +0100576func Test_next_split_all()
577 " This was causing an illegal memory access.
578 n x
579 norm axxx
580 split
581 split
582 s/x
583 s/x
584 all
585 bwipe!
586endfunc
587
Bram Moolenaar75373f32017-08-07 22:02:30 +0200588" Tests for adjusting window and contents
589func GetScreenStr(row)
590 let str = ""
591 for c in range(1,3)
592 let str .= nr2char(screenchar(a:row, c))
593 endfor
594 return str
595endfunc
596
597func Test_window_contents()
598 enew! | only | new
599 call setline(1, range(1,256))
600
601 exe "norm! \<C-W>t\<C-W>=1Gzt\<C-W>w\<C-W>+"
602 redraw
Bram Moolenaar72cf47a2018-05-10 18:23:29 +0200603 let s3 = GetScreenStr(1)
Bram Moolenaar75373f32017-08-07 22:02:30 +0200604 wincmd p
605 call assert_equal(1, line("w0"))
606 call assert_equal('1 ', s3)
607
608 exe "norm! \<C-W>t\<C-W>=50Gzt\<C-W>w\<C-W>+"
609 redraw
Bram Moolenaar72cf47a2018-05-10 18:23:29 +0200610 let s3 = GetScreenStr(1)
Bram Moolenaar75373f32017-08-07 22:02:30 +0200611 wincmd p
612 call assert_equal(50, line("w0"))
613 call assert_equal('50 ', s3)
614
615 exe "norm! \<C-W>t\<C-W>=59Gzt\<C-W>w\<C-W>+"
616 redraw
Bram Moolenaar72cf47a2018-05-10 18:23:29 +0200617 let s3 = GetScreenStr(1)
Bram Moolenaar75373f32017-08-07 22:02:30 +0200618 wincmd p
619 call assert_equal(59, line("w0"))
620 call assert_equal('59 ', s3)
621
Bram Moolenaar8b633132020-03-20 18:20:51 +0100622 %d
623 call setline(1, ['one', 'two', 'three'])
624 call assert_equal(1, line('w0'))
625 call assert_equal(3, line('w$'))
626
Bram Moolenaar75373f32017-08-07 22:02:30 +0200627 bwipeout!
628 call test_garbagecollect_now()
629endfunc
630
Bram Moolenaar2efb3232017-12-19 12:27:23 +0100631func Test_window_colon_command()
632 " This was reading invalid memory.
633 exe "norm! v\<C-W>:\<C-U>echo v:version"
634endfunc
635
Bram Moolenaar6f361c92018-01-31 19:06:50 +0100636func Test_access_freed_mem()
Bram Moolenaar1417c762019-07-27 17:31:36 +0200637 call assert_equal(&columns, winwidth(0))
Bram Moolenaar9a046fd2021-01-28 13:47:59 +0100638 " This was accessing freed memory (but with what events?)
639 au BufEnter,BufLeave,WinEnter,WinLeave 0 vs xxx
Bram Moolenaar6f361c92018-01-31 19:06:50 +0100640 arg 0
641 argadd
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +0200642 call assert_fails("all", "E242:")
Bram Moolenaar6f361c92018-01-31 19:06:50 +0100643 au!
644 bwipe xxx
Bram Moolenaar1417c762019-07-27 17:31:36 +0200645 call assert_equal(&columns, winwidth(0))
Bram Moolenaar6f361c92018-01-31 19:06:50 +0100646endfunc
647
Bram Moolenaara27e1dc2019-10-07 22:27:36 +0200648func Test_insert_cleared_on_switch_to_term()
649 CheckFeature terminal
650
651 set showmode
652 terminal
653 wincmd p
654
655 call feedkeys("i\<C-O>", 'ntx')
656 redraw
657
658 " The "-- (insert) --" indicator should be visible.
659 let chars = map(range(1, &columns), 'nr2char(screenchar(&lines, v:val))')
660 let str = trim(join(chars, ''))
661 call assert_equal('-- (insert) --', str)
662
663 call feedkeys("\<C-W>p", 'ntx')
664 redraw
665
666 " The "-- (insert) --" indicator should have been cleared.
667 let chars = map(range(1, &columns), 'nr2char(screenchar(&lines, v:val))')
668 let str = trim(join(chars, ''))
669 call assert_equal('', str)
670
671 set showmode&
672 %bw!
673endfunc
674
Bram Moolenaar5bab5552018-04-13 20:41:29 +0200675func Test_visual_cleared_after_window_split()
676 new | only!
677 let smd_save = &showmode
678 set showmode
679 let ls_save = &laststatus
680 set laststatus=1
681 call setline(1, ['a', 'b', 'c', 'd', ''])
682 norm! G
683 exe "norm! kkvk"
684 redraw
685 exe "norm! \<C-W>v"
686 redraw
687 " check if '-- VISUAL --' disappeared from command line
688 let columns = range(1, &columns)
689 let cmdlinechars = map(columns, 'nr2char(screenchar(&lines, v:val))')
690 let cmdline = join(cmdlinechars, '')
691 let cmdline_ltrim = substitute(cmdline, '^\s*', "", "")
692 let mode_shown = substitute(cmdline_ltrim, '\s*$', "", "")
693 call assert_equal('', mode_shown)
694 let &showmode = smd_save
695 let &laststatus = ls_save
696 bwipe!
697endfunc
698
Bram Moolenaar72cf47a2018-05-10 18:23:29 +0200699func Test_winrestcmd()
700 2split
701 3vsplit
Bram Moolenaara0c8aea2021-03-20 19:55:35 +0100702 let restcmd = winrestcmd()
Bram Moolenaar72cf47a2018-05-10 18:23:29 +0200703 call assert_equal(2, winheight(0))
704 call assert_equal(3, winwidth(0))
705 wincmd =
706 call assert_notequal(2, winheight(0))
707 call assert_notequal(3, winwidth(0))
Bram Moolenaara0c8aea2021-03-20 19:55:35 +0100708 exe restcmd
Bram Moolenaar72cf47a2018-05-10 18:23:29 +0200709 call assert_equal(2, winheight(0))
710 call assert_equal(3, winwidth(0))
711 only
Bram Moolenaara0c8aea2021-03-20 19:55:35 +0100712
713 wincmd v
714 wincmd s
715 wincmd v
716 redraw
717 let restcmd = winrestcmd()
718 wincmd _
719 wincmd |
720 exe restcmd
721 redraw
722 call assert_equal(restcmd, winrestcmd())
723
724 only
Bram Moolenaar72cf47a2018-05-10 18:23:29 +0200725endfunc
726
Bram Moolenaar1e115362019-01-09 23:01:02 +0100727func Fun_RenewFile()
Bram Moolenaar026587b2019-08-17 15:08:00 +0200728 " Need to wait a bit for the timestamp to be older.
Bram Moolenaarce90e362019-09-08 18:58:44 +0200729 let old_ftime = getftime("tmp.txt")
730 while getftime("tmp.txt") == old_ftime
731 sleep 100m
732 silent execute '!echo "1" > tmp.txt'
733 endwhile
Bram Moolenaara42df592018-12-24 00:22:39 +0100734 sp
735 wincmd p
736 edit! tmp.txt
Bram Moolenaar1e115362019-01-09 23:01:02 +0100737endfunc
Bram Moolenaara42df592018-12-24 00:22:39 +0100738
739func Test_window_prevwin()
740 " Can we make this work on MS-Windows?
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +0200741 CheckUnix
Bram Moolenaara42df592018-12-24 00:22:39 +0100742
743 set hidden autoread
744 call writefile(['2'], 'tmp.txt')
745 new tmp.txt
746 q
Bram Moolenaara42df592018-12-24 00:22:39 +0100747 call Fun_RenewFile()
748 call assert_equal(2, winnr())
749 wincmd p
750 call assert_equal(1, winnr())
751 wincmd p
752 q
753 call Fun_RenewFile()
754 call assert_equal(2, winnr())
755 wincmd p
756 call assert_equal(1, winnr())
757 wincmd p
758 " reset
759 q
760 call delete('tmp.txt')
761 set hidden&vim autoread&vim
762 delfunc Fun_RenewFile
763endfunc
764
Bram Moolenaar8fcb60f2019-03-04 13:18:30 +0100765func Test_relative_cursor_position_in_one_line_window()
766 new
767 only
768 call setline(1, range(1, 10000))
769 normal 50%
770 let lnum = getcurpos()[1]
771 split
772 split
773 " make third window take as many lines as possible, other windows will
774 " become one line
775 3wincmd w
776 for i in range(1, &lines - 6)
777 wincmd +
778 redraw!
779 endfor
780
781 " first and second window should show cursor line
782 let wininfo = getwininfo()
783 call assert_equal(lnum, wininfo[0].topline)
784 call assert_equal(lnum, wininfo[1].topline)
785
786 only!
787 bwipe!
Yegappan Lakshmanan04c4c572022-08-30 19:48:24 +0100788 call assert_fails('call winrestview(test_null_dict())', 'E1297:')
Bram Moolenaar8fcb60f2019-03-04 13:18:30 +0100789endfunc
790
791func Test_relative_cursor_position_after_move_and_resize()
792 let so_save = &so
793 set so=0
794 enew
795 call setline(1, range(1, 10000))
796 normal 50%
797 split
798 1wincmd w
799 " Move cursor to first line in window
800 normal H
801 redraw!
802 " Reduce window height to two lines
803 let height = winheight(0)
804 while winheight(0) > 2
805 wincmd -
806 redraw!
807 endwhile
808 " move cursor to second/last line in window
809 normal j
810 " restore previous height
811 while winheight(0) < height
812 wincmd +
813 redraw!
814 endwhile
815 " make window two lines again
816 while winheight(0) > 2
817 wincmd -
818 redraw!
819 endwhile
820
821 " cursor should be at bottom line
822 let info = getwininfo(win_getid())[0]
823 call assert_equal(info.topline + 1, getcurpos()[1])
824
825 only!
826 bwipe!
827 let &so = so_save
828endfunc
829
830func Test_relative_cursor_position_after_resize()
831 let so_save = &so
832 set so=0
833 enew
834 call setline(1, range(1, 10000))
835 normal 50%
836 split
837 1wincmd w
838 let winid1 = win_getid()
839 let info = getwininfo(winid1)[0]
840 " Move cursor to second line in window
841 exe "normal " . (info.topline + 1) . "G"
842 redraw!
843 let lnum = getcurpos()[1]
844
845 " Make the window only two lines high, cursor should end up in top line
846 2wincmd w
847 exe (info.height - 2) . "wincmd +"
848 redraw!
849 let info = getwininfo(winid1)[0]
850 call assert_equal(lnum, info.topline)
851
852 only!
853 bwipe!
854 let &so = so_save
855endfunc
856
857func Test_relative_cursor_second_line_after_resize()
858 let so_save = &so
859 set so=0
860 enew
861 call setline(1, range(1, 10000))
862 normal 50%
863 split
864 1wincmd w
865 let winid1 = win_getid()
866 let info = getwininfo(winid1)[0]
867
868 " Make the window only two lines high
869 2wincmd _
870
871 " Move cursor to second line in window
872 normal H
873 normal j
874
875 " Make window size bigger, then back to 2 lines
876 for i in range(1, 10)
877 wincmd +
878 redraw!
879 endfor
880 for i in range(1, 10)
881 wincmd -
882 redraw!
883 endfor
884
885 " cursor should end up in bottom line
886 let info = getwininfo(winid1)[0]
887 call assert_equal(info.topline + 1, getcurpos()[1])
888
889 only!
890 bwipe!
891 let &so = so_save
892endfunc
893
Bram Moolenaara9b25352019-05-12 14:25:30 +0200894func Test_split_noscroll()
895 let so_save = &so
Bram Moolenaarbd2d68c2019-05-18 15:36:11 +0200896 enew
897 call setline(1, range(1, 8))
Bram Moolenaara9b25352019-05-12 14:25:30 +0200898 normal 100%
899 split
900
901 1wincmd w
902 let winid1 = win_getid()
903 let info1 = getwininfo(winid1)[0]
904
905 2wincmd w
906 let winid2 = win_getid()
907 let info2 = getwininfo(winid2)[0]
908
909 call assert_equal(1, info1.topline)
910 call assert_equal(1, info2.topline)
911
Bram Moolenaarbd2d68c2019-05-18 15:36:11 +0200912 " window that fits all lines by itself, but not when split: closing other
913 " window should restore fraction.
Bram Moolenaara9b25352019-05-12 14:25:30 +0200914 only!
Bram Moolenaarbd2d68c2019-05-18 15:36:11 +0200915 call setline(1, range(1, &lines - 10))
916 exe &lines / 4
917 let winid1 = win_getid()
918 let info1 = getwininfo(winid1)[0]
919 call assert_equal(1, info1.topline)
920 new
921 redraw
922 close
923 let info1 = getwininfo(winid1)[0]
924 call assert_equal(1, info1.topline)
925
Bram Moolenaara9b25352019-05-12 14:25:30 +0200926 bwipe!
927 let &so = so_save
928endfunc
929
Bram Moolenaar46ad2882019-04-08 20:01:47 +0200930" Tests for the winnr() function
931func Test_winnr()
932 only | tabonly
933 call assert_equal(1, winnr('j'))
934 call assert_equal(1, winnr('k'))
935 call assert_equal(1, winnr('h'))
936 call assert_equal(1, winnr('l'))
937
938 " create a set of horizontally and vertically split windows
939 leftabove new | wincmd p
940 leftabove new | wincmd p
941 rightbelow new | wincmd p
942 rightbelow new | wincmd p
943 leftabove vnew | wincmd p
944 leftabove vnew | wincmd p
945 rightbelow vnew | wincmd p
946 rightbelow vnew | wincmd p
947
948 call assert_equal(8, winnr('j'))
949 call assert_equal(2, winnr('k'))
950 call assert_equal(4, winnr('h'))
951 call assert_equal(6, winnr('l'))
952 call assert_equal(9, winnr('2j'))
953 call assert_equal(1, winnr('2k'))
954 call assert_equal(3, winnr('2h'))
955 call assert_equal(7, winnr('2l'))
956
957 " Error cases
958 call assert_fails("echo winnr('0.2k')", 'E15:')
959 call assert_equal(2, winnr('-2k'))
960 call assert_fails("echo winnr('-2xj')", 'E15:')
961 call assert_fails("echo winnr('j2j')", 'E15:')
962 call assert_fails("echo winnr('ll')", 'E15:')
963 call assert_fails("echo winnr('5')", 'E15:')
964 call assert_equal(4, winnr('0h'))
Bram Moolenaar99fa7212020-04-26 15:59:55 +0200965 call assert_fails("let w = winnr([])", 'E730:')
966 call assert_equal('unknown', win_gettype(-1))
967 call assert_equal(-1, winheight(-1))
968 call assert_equal(-1, winwidth(-1))
Bram Moolenaar46ad2882019-04-08 20:01:47 +0200969
970 tabnew
971 call assert_equal(8, tabpagewinnr(1, 'j'))
Bram Moolenaarce90e362019-09-08 18:58:44 +0200972 call assert_equal(2, 1->tabpagewinnr('k'))
Bram Moolenaar46ad2882019-04-08 20:01:47 +0200973 call assert_equal(4, tabpagewinnr(1, 'h'))
974 call assert_equal(6, tabpagewinnr(1, 'l'))
975
976 only | tabonly
977endfunc
978
Bram Moolenaarf92e58c2019-09-08 21:51:41 +0200979func Test_winrestview()
980 split runtest.vim
981 normal 50%
982 let view = winsaveview()
983 close
984 split runtest.vim
985 eval view->winrestview()
986 call assert_equal(view, winsaveview())
987
988 bwipe!
Yegappan Lakshmanan04c4c572022-08-30 19:48:24 +0100989 call assert_fails('call winrestview(test_null_dict())', 'E1297:')
Bram Moolenaarf92e58c2019-09-08 21:51:41 +0200990endfunc
991
Bram Moolenaard20dcb32019-09-10 21:22:58 +0200992func Test_win_splitmove()
Bram Moolenaar5a4c3082019-12-01 15:23:11 +0100993 CheckFeature quickfix
994
Bram Moolenaard20dcb32019-09-10 21:22:58 +0200995 edit a
996 leftabove split b
997 leftabove vsplit c
998 leftabove split d
999 call assert_equal(0, win_splitmove(winnr(), winnr('l')))
1000 call assert_equal(bufname(winbufnr(1)), 'c')
1001 call assert_equal(bufname(winbufnr(2)), 'd')
1002 call assert_equal(bufname(winbufnr(3)), 'b')
1003 call assert_equal(bufname(winbufnr(4)), 'a')
1004 call assert_equal(0, win_splitmove(winnr(), winnr('j'), {'vertical': 1}))
1005 call assert_equal(0, win_splitmove(winnr(), winnr('j'), {'vertical': 1}))
1006 call assert_equal(bufname(winbufnr(1)), 'c')
1007 call assert_equal(bufname(winbufnr(2)), 'b')
1008 call assert_equal(bufname(winbufnr(3)), 'd')
1009 call assert_equal(bufname(winbufnr(4)), 'a')
1010 call assert_equal(0, win_splitmove(winnr(), winnr('k'), {'vertical': 1}))
1011 call assert_equal(bufname(winbufnr(1)), 'd')
1012 call assert_equal(bufname(winbufnr(2)), 'c')
1013 call assert_equal(bufname(winbufnr(3)), 'b')
1014 call assert_equal(bufname(winbufnr(4)), 'a')
1015 call assert_equal(0, win_splitmove(winnr(), winnr('j'), {'rightbelow': v:true}))
1016 call assert_equal(bufname(winbufnr(1)), 'c')
1017 call assert_equal(bufname(winbufnr(2)), 'b')
1018 call assert_equal(bufname(winbufnr(3)), 'a')
1019 call assert_equal(bufname(winbufnr(4)), 'd')
Yegappan Lakshmanan04c4c572022-08-30 19:48:24 +01001020 call assert_fails('call win_splitmove(winnr(), winnr("k"), test_null_dict())', 'E1297:')
Bram Moolenaard20dcb32019-09-10 21:22:58 +02001021 only | bd
1022
1023 call assert_fails('call win_splitmove(winnr(), 123)', 'E957:')
1024 call assert_fails('call win_splitmove(123, winnr())', 'E957:')
1025 call assert_fails('call win_splitmove(winnr(), winnr())', 'E957:')
Bram Moolenaar7b94e772020-01-06 21:03:24 +01001026
1027 tabnew
1028 call assert_fails('call win_splitmove(1, win_getid(1, 1))', 'E957:')
1029 tabclose
Bram Moolenaard20dcb32019-09-10 21:22:58 +02001030endfunc
1031
Bram Moolenaare20b9ec2020-02-03 21:40:04 +01001032" Test for the :only command
1033func Test_window_only()
1034 new
1035 set modified
1036 new
1037 call assert_fails('only', 'E445:')
1038 only!
Bram Moolenaarbc2b71d2020-02-17 21:33:30 +01001039 " Test for :only with a count
1040 let wid = win_getid()
1041 new
1042 new
1043 3only
1044 call assert_equal(1, winnr('$'))
1045 call assert_equal(wid, win_getid())
1046 call assert_fails('close', 'E444:')
1047 call assert_fails('%close', 'E16:')
Bram Moolenaare20b9ec2020-02-03 21:40:04 +01001048endfunc
1049
Bram Moolenaar9f6277b2020-02-11 22:04:02 +01001050" Test for errors with :wincmd
1051func Test_wincmd_errors()
1052 call assert_fails('wincmd g', 'E474:')
1053 call assert_fails('wincmd ab', 'E474:')
1054endfunc
1055
1056" Test for errors with :winpos
1057func Test_winpos_errors()
1058 if !has("gui_running") && !has('win32')
1059 call assert_fails('winpos', 'E188:')
1060 endif
1061 call assert_fails('winpos 10', 'E466:')
1062endfunc
1063
Bram Moolenaar406cd902020-02-18 21:54:41 +01001064" Test for +cmd in a :split command
1065func Test_split_cmd()
1066 split +set\ readonly
1067 call assert_equal(1, &readonly)
1068 call assert_equal(2, winnr('$'))
1069 close
1070endfunc
1071
Bram Moolenaar5080b0a2020-03-22 21:23:47 +01001072" Create maximum number of horizontally or vertically split windows and then
1073" run commands that create a new horizontally/vertically split window
1074func Run_noroom_for_newwindow_test(dir_arg)
1075 let dir = (a:dir_arg == 'v') ? 'vert ' : ''
1076
1077 " Open as many windows as possible
Bram Moolenaarab505b12020-03-23 19:28:44 +01001078 while v:true
Bram Moolenaar5080b0a2020-03-22 21:23:47 +01001079 try
1080 exe dir . 'new'
1081 catch /E36:/
1082 break
1083 endtry
Bram Moolenaarab505b12020-03-23 19:28:44 +01001084 endwhile
Bram Moolenaar5080b0a2020-03-22 21:23:47 +01001085
Bram Moolenaarb18b4962022-09-02 21:55:50 +01001086 call writefile(['first', 'second', 'third'], 'Xnorfile1')
1087 call writefile([], 'Xnorfile2')
1088 call writefile([], 'Xnorfile3')
Bram Moolenaar5080b0a2020-03-22 21:23:47 +01001089
1090 " Argument list related commands
Bram Moolenaarb18b4962022-09-02 21:55:50 +01001091 args Xnorfile1 Xnorfile2 Xnorfile3
Bram Moolenaar5080b0a2020-03-22 21:23:47 +01001092 next
1093 for cmd in ['sargument 2', 'snext', 'sprevious', 'sNext', 'srewind',
1094 \ 'sfirst', 'slast']
1095 call assert_fails(dir .. cmd, 'E36:')
1096 endfor
1097 %argdelete
1098
1099 " Buffer related commands
1100 set modified
1101 hide enew
Bram Moolenaarb18b4962022-09-02 21:55:50 +01001102 for cmd in ['sbuffer Xnorfile1', 'sbnext', 'sbprevious', 'sbNext', 'sbrewind',
Bram Moolenaar5080b0a2020-03-22 21:23:47 +01001103 \ 'sbfirst', 'sblast', 'sball', 'sbmodified', 'sunhide']
1104 call assert_fails(dir .. cmd, 'E36:')
1105 endfor
1106
1107 " Window related commands
Bram Moolenaarb18b4962022-09-02 21:55:50 +01001108 for cmd in ['split', 'split Xnorfile2', 'new', 'new Xnorfile3', 'sview Xnorfile1',
Bram Moolenaar5080b0a2020-03-22 21:23:47 +01001109 \ 'sfind runtest.vim']
1110 call assert_fails(dir .. cmd, 'E36:')
1111 endfor
1112
1113 " Help
1114 call assert_fails(dir .. 'help', 'E36:')
1115 call assert_fails(dir .. 'helpgrep window', 'E36:')
1116
1117 " Command-line window
1118 if a:dir_arg == 'h'
1119 " Cmd-line window is always a horizontally split window
1120 call assert_beeps('call feedkeys("q:\<CR>", "xt")')
1121 endif
1122
1123 " Quickfix and location list window
1124 if has('quickfix')
1125 cexpr ''
1126 call assert_fails(dir .. 'copen', 'E36:')
1127 lexpr ''
1128 call assert_fails(dir .. 'lopen', 'E36:')
1129
1130 " Preview window
Bram Moolenaarb18b4962022-09-02 21:55:50 +01001131 call assert_fails(dir .. 'pedit Xnorfile2', 'E36:')
Bram Moolenaar5080b0a2020-03-22 21:23:47 +01001132 call setline(1, 'abc')
1133 call assert_fails(dir .. 'psearch abc', 'E36:')
1134 endif
1135
1136 " Window commands (CTRL-W ^ and CTRL-W f)
1137 if a:dir_arg == 'h'
1138 call assert_fails('call feedkeys("\<C-W>^", "xt")', 'E36:')
Bram Moolenaarb18b4962022-09-02 21:55:50 +01001139 call setline(1, 'Xnorfile1')
Bram Moolenaar5080b0a2020-03-22 21:23:47 +01001140 call assert_fails('call feedkeys("gg\<C-W>f", "xt")', 'E36:')
1141 endif
1142 enew!
1143
1144 " Tag commands (:stag, :stselect and :stjump)
1145 call writefile(["!_TAG_FILE_ENCODING\tutf-8\t//",
Bram Moolenaarb18b4962022-09-02 21:55:50 +01001146 \ "second\tXnorfile1\t2",
1147 \ "third\tXnorfile1\t3",],
Bram Moolenaar5080b0a2020-03-22 21:23:47 +01001148 \ 'Xtags')
1149 set tags=Xtags
1150 call assert_fails(dir .. 'stag second', 'E36:')
1151 call assert_fails('call feedkeys(":" .. dir .. "stselect second\n1\n", "xt")', 'E36:')
1152 call assert_fails(dir .. 'stjump second', 'E36:')
1153 call assert_fails(dir .. 'ptag second', 'E36:')
1154 set tags&
1155 call delete('Xtags')
1156
1157 " :isplit and :dsplit
1158 call setline(1, ['#define FOO 1', 'FOO'])
1159 normal 2G
1160 call assert_fails(dir .. 'isplit FOO', 'E36:')
1161 call assert_fails(dir .. 'dsplit FOO', 'E36:')
1162
1163 " terminal
1164 if has('terminal')
1165 call assert_fails(dir .. 'terminal', 'E36:')
1166 endif
1167
1168 %bwipe!
Bram Moolenaarb18b4962022-09-02 21:55:50 +01001169 call delete('Xnorfile1')
1170 call delete('Xnorfile2')
1171 call delete('Xnorfile3')
Bram Moolenaar5080b0a2020-03-22 21:23:47 +01001172 only
1173endfunc
1174
1175func Test_split_cmds_with_no_room()
1176 call Run_noroom_for_newwindow_test('h')
1177 call Run_noroom_for_newwindow_test('v')
1178endfunc
1179
Bram Moolenaarad48e6c2020-04-21 22:19:45 +02001180" Test for various wincmd failures
1181func Test_wincmd_fails()
1182 only!
1183 call assert_beeps("normal \<C-W>w")
1184 call assert_beeps("normal \<C-W>p")
1185 call assert_beeps("normal \<C-W>gk")
1186 call assert_beeps("normal \<C-W>r")
1187 call assert_beeps("normal \<C-W>K")
1188 call assert_beeps("normal \<C-W>H")
1189 call assert_beeps("normal \<C-W>2gt")
1190endfunc
1191
Bram Moolenaarfe6dce82020-09-04 14:41:21 +02001192func Test_window_resize()
1193 " Vertical :resize (absolute, relative, min and max size).
1194 vsplit
1195 vert resize 8
1196 call assert_equal(8, winwidth(0))
1197 vert resize +2
1198 call assert_equal(10, winwidth(0))
1199 vert resize -2
1200 call assert_equal(8, winwidth(0))
1201 vert resize
1202 call assert_equal(&columns - 2, winwidth(0))
1203 vert resize 0
1204 call assert_equal(1, winwidth(0))
1205 vert resize 99999
1206 call assert_equal(&columns - 2, winwidth(0))
1207
1208 %bwipe!
1209
1210 " Horizontal :resize (with absolute, relative size, min and max size).
1211 split
1212 resize 8
1213 call assert_equal(8, winheight(0))
1214 resize +2
1215 call assert_equal(10, winheight(0))
1216 resize -2
1217 call assert_equal(8, winheight(0))
1218 resize
1219 call assert_equal(&lines - 4, winheight(0))
1220 resize 0
1221 call assert_equal(1, winheight(0))
1222 resize 99999
1223 call assert_equal(&lines - 4, winheight(0))
1224
1225 " :resize with explicit window number.
1226 let other_winnr = winnr('j')
1227 exe other_winnr .. 'resize 10'
1228 call assert_equal(10, winheight(other_winnr))
1229 call assert_equal(&lines - 10 - 3, winheight(0))
Bram Moolenaar9668cc52020-10-17 17:39:55 +02001230 exe other_winnr .. 'resize +1'
1231 exe other_winnr .. 'resize +1'
1232 call assert_equal(12, winheight(other_winnr))
1233 call assert_equal(&lines - 10 - 3 -2, winheight(0))
Bram Moolenaar89015a62020-12-29 12:46:51 +01001234 close
1235
1236 vsplit
1237 wincmd l
1238 let other_winnr = winnr('h')
1239 call assert_notequal(winnr(), other_winnr)
Bram Moolenaar5efe0e52021-01-01 14:31:34 +01001240 exe 'vert ' .. other_winnr .. 'resize -' .. &columns
Bram Moolenaar89015a62020-12-29 12:46:51 +01001241 call assert_equal(0, winwidth(other_winnr))
Bram Moolenaarfe6dce82020-09-04 14:41:21 +02001242
1243 %bwipe!
1244endfunc
1245
Bram Moolenaar5d3c9f82020-06-26 20:41:39 +02001246" Test for adjusting the window width when a window is closed with some
1247" windows using 'winfixwidth'
1248func Test_window_width_adjust()
1249 only
1250 " Three vertical windows. Windows 1 and 2 have 'winfixwidth' set and close
1251 " window 2.
1252 wincmd v
1253 vert resize 10
1254 set winfixwidth
1255 wincmd v
1256 set winfixwidth
1257 wincmd c
1258 call assert_inrange(10, 12, winwidth(1))
1259 " Three vertical windows. Windows 2 and 3 have 'winfixwidth' set and close
1260 " window 3.
1261 only
1262 set winfixwidth
1263 wincmd v
1264 vert resize 10
1265 set winfixwidth
1266 wincmd v
1267 set nowinfixwidth
1268 wincmd b
1269 wincmd c
1270 call assert_inrange(10, 12, winwidth(2))
1271
1272 new | only
1273endfunc
1274
1275" Test for jumping to a vertical/horizontal neighbor window based on the
1276" current cursor position
1277func Test_window_goto_neightbor()
1278 %bw!
1279
1280 " Vertical window movement
1281
1282 " create the following window layout:
1283 " +--+--+
1284 " |w1|w3|
1285 " +--+ |
1286 " |w2| |
1287 " +--+--+
1288 " |w4 |
1289 " +-----+
1290 new
1291 vsplit
1292 split
1293 " vertically jump from w4
1294 wincmd b
1295 call setline(1, repeat(' ', &columns))
1296 call cursor(1, 1)
1297 wincmd k
1298 call assert_equal(2, winnr())
1299 wincmd b
1300 call cursor(1, &columns)
1301 redraw!
1302 wincmd k
1303 call assert_equal(3, winnr())
1304 %bw!
1305
1306 " create the following window layout:
1307 " +--+--+--+
1308 " |w1|w2|w3|
1309 " +--+--+--+
1310 " |w4 |
1311 " +--------+
1312 new
1313 vsplit
1314 vsplit
1315 wincmd b
1316 call setline(1, repeat(' ', &columns))
1317 call cursor(1, 1)
1318 wincmd k
1319 call assert_equal(1, winnr())
1320 wincmd b
1321 call cursor(1, &columns / 2)
1322 redraw!
1323 wincmd k
1324 call assert_equal(2, winnr())
1325 wincmd b
1326 call cursor(1, &columns)
1327 redraw!
1328 wincmd k
1329 call assert_equal(3, winnr())
1330 %bw!
1331
1332 " Horizontal window movement
1333
1334 " create the following window layout:
1335 " +--+--+--+
1336 " |w1|w2|w4|
1337 " +--+--+ |
1338 " |w3 | |
1339 " +-----+--+
1340 vsplit
1341 split
1342 vsplit
1343 4wincmd l
1344 call setline(1, repeat([' '], &lines))
1345 call cursor(1, 1)
1346 redraw!
1347 wincmd h
1348 call assert_equal(2, winnr())
1349 4wincmd l
1350 call cursor(&lines, 1)
1351 redraw!
1352 wincmd h
1353 call assert_equal(3, winnr())
1354 %bw!
1355
1356 " create the following window layout:
1357 " +--+--+
1358 " |w1|w4|
1359 " +--+ +
1360 " |w2| |
1361 " +--+ +
1362 " |w3| |
1363 " +--+--+
1364 vsplit
1365 split
1366 split
1367 wincmd l
1368 call setline(1, repeat([' '], &lines))
1369 call cursor(1, 1)
1370 redraw!
1371 wincmd h
1372 call assert_equal(1, winnr())
1373 wincmd l
1374 call cursor(&lines / 2, 1)
1375 redraw!
1376 wincmd h
1377 call assert_equal(2, winnr())
1378 wincmd l
1379 call cursor(&lines, 1)
1380 redraw!
1381 wincmd h
1382 call assert_equal(3, winnr())
1383 %bw!
1384endfunc
1385
1386" Test for an autocmd closing the destination window when jumping from one
1387" window to another.
1388func Test_close_dest_window()
1389 split
Bram Moolenaarb18b4962022-09-02 21:55:50 +01001390 edit Xdstfile
Bram Moolenaar5d3c9f82020-06-26 20:41:39 +02001391
1392 " Test for BufLeave
1393 augroup T1
1394 au!
Bram Moolenaarb18b4962022-09-02 21:55:50 +01001395 au BufLeave Xdstfile $wincmd c
Bram Moolenaar5d3c9f82020-06-26 20:41:39 +02001396 augroup END
1397 wincmd b
1398 call assert_equal(1, winnr('$'))
Bram Moolenaarb18b4962022-09-02 21:55:50 +01001399 call assert_equal('Xdstfile', @%)
Bram Moolenaar5d3c9f82020-06-26 20:41:39 +02001400 augroup T1
1401 au!
1402 augroup END
1403
1404 " Test for WinLeave
1405 new
1406 wincmd p
1407 augroup T1
1408 au!
1409 au WinLeave * 1wincmd c
1410 augroup END
1411 wincmd t
1412 call assert_equal(1, winnr('$'))
Bram Moolenaarb18b4962022-09-02 21:55:50 +01001413 call assert_equal('Xdstfile', @%)
Bram Moolenaar5d3c9f82020-06-26 20:41:39 +02001414 augroup T1
1415 au!
1416 augroup END
1417 augroup! T1
1418 %bw!
1419endfunc
1420
Bram Moolenaard0fb9072021-12-09 11:57:22 +00001421func Test_window_minimal_size()
1422 set winminwidth=0 winminheight=0
1423
1424 " check size is fixed vertically
1425 new
1426 call win_execute(win_getid(2), 'wincmd _')
1427 call assert_equal(0, winheight(0))
1428 call feedkeys('0', 'tx')
1429 call assert_equal(1, winheight(0))
1430 bwipe!
1431
1432 " check size is fixed horizontally
1433 vert new
1434 call win_execute(win_getid(2), 'wincmd |')
1435 call assert_equal(0, winwidth(0))
1436 call feedkeys('0', 'tx')
1437 call assert_equal(1, winwidth(0))
1438 bwipe!
1439
1440 if has('timers')
1441 " check size is fixed in Insert mode
ichizokfa9a8e02021-12-12 16:42:09 +00001442 func s:CheckSize(timer) abort
1443 call win_execute(win_getid(2), 'wincmd _')
1444 call assert_equal(0, winheight(0))
1445 call feedkeys(" \<Esc>", 't!')
1446 endfunc
Bram Moolenaard0fb9072021-12-09 11:57:22 +00001447 new
ichizokfa9a8e02021-12-12 16:42:09 +00001448 call timer_start(100, function('s:CheckSize'))
Bram Moolenaard0fb9072021-12-09 11:57:22 +00001449 call feedkeys('a', 'tx!')
1450 call assert_equal(1, winheight(0))
1451 bwipe!
ichizokfa9a8e02021-12-12 16:42:09 +00001452 delfunc s:CheckSize
Bram Moolenaard0fb9072021-12-09 11:57:22 +00001453 endif
1454
1455 set winminwidth& winminheight&
1456endfunc
1457
Daniel Steinbergee630312022-01-10 13:36:34 +00001458func Test_win_move_separator()
1459 edit a
1460 leftabove vsplit b
1461 let w = winwidth(0)
1462 " check win_move_separator from left window on left window
1463 call assert_equal(1, winnr())
1464 for offset in range(5)
1465 call assert_true(win_move_separator(0, offset))
1466 call assert_equal(w + offset, winwidth(0))
1467 call assert_true(0->win_move_separator(-offset))
1468 call assert_equal(w, winwidth(0))
1469 endfor
1470 " check win_move_separator from right window on left window number
1471 wincmd l
1472 call assert_notequal(1, winnr())
1473 for offset in range(5)
1474 call assert_true(1->win_move_separator(offset))
1475 call assert_equal(w + offset, winwidth(1))
1476 call assert_true(win_move_separator(1, -offset))
1477 call assert_equal(w, winwidth(1))
1478 endfor
1479 " check win_move_separator from right window on left window ID
1480 let id = win_getid(1)
1481 for offset in range(5)
1482 call assert_true(win_move_separator(id, offset))
1483 call assert_equal(w + offset, winwidth(id))
1484 call assert_true(id->win_move_separator(-offset))
1485 call assert_equal(w, winwidth(id))
1486 endfor
zeertzjqa0c4e2f2022-01-29 10:59:53 +00001487 " check win_move_separator from right window on right window is no-op
1488 let w0 = winwidth(0)
1489 call assert_true(win_move_separator(0, 1))
1490 call assert_equal(w0, winwidth(0))
1491 call assert_true(win_move_separator(0, -1))
1492 call assert_equal(w0, winwidth(0))
Daniel Steinbergee630312022-01-10 13:36:34 +00001493 " check that win_move_separator doesn't error with offsets beyond moving
1494 " possibility
1495 call assert_true(win_move_separator(id, 5000))
1496 call assert_true(winwidth(id) > w)
1497 call assert_true(win_move_separator(id, -5000))
1498 call assert_true(winwidth(id) < w)
1499 " check that win_move_separator returns false for an invalid window
1500 wincmd =
1501 let w = winwidth(0)
1502 call assert_false(win_move_separator(-1, 1))
1503 call assert_equal(w, winwidth(0))
1504 " check that win_move_separator returns false for a popup window
1505 let id = popup_create(['hello', 'world'], {})
1506 let w = winwidth(id)
1507 call assert_false(win_move_separator(id, 1))
1508 call assert_equal(w, winwidth(id))
1509 call popup_close(id)
1510 %bwipe!
1511endfunc
1512
1513func Test_win_move_statusline()
1514 edit a
1515 leftabove split b
1516 let h = winheight(0)
1517 " check win_move_statusline from top window on top window
1518 call assert_equal(1, winnr())
1519 for offset in range(5)
1520 call assert_true(win_move_statusline(0, offset))
1521 call assert_equal(h + offset, winheight(0))
1522 call assert_true(0->win_move_statusline(-offset))
1523 call assert_equal(h, winheight(0))
1524 endfor
1525 " check win_move_statusline from bottom window on top window number
1526 wincmd j
1527 call assert_notequal(1, winnr())
1528 for offset in range(5)
1529 call assert_true(1->win_move_statusline(offset))
1530 call assert_equal(h + offset, winheight(1))
1531 call assert_true(win_move_statusline(1, -offset))
1532 call assert_equal(h, winheight(1))
1533 endfor
zeertzjqa0c4e2f2022-01-29 10:59:53 +00001534 " check win_move_statusline from bottom window on bottom window
1535 let h0 = winheight(0)
1536 for offset in range(5)
1537 call assert_true(0->win_move_statusline(-offset))
1538 call assert_equal(h0 - offset, winheight(0))
1539 call assert_equal(1 + offset, &cmdheight)
1540 call assert_true(win_move_statusline(0, offset))
1541 call assert_equal(h0, winheight(0))
1542 call assert_equal(1, &cmdheight)
1543 endfor
1544 call assert_true(win_move_statusline(0, 1))
Bram Moolenaara2a89732022-08-31 14:46:18 +01001545 call assert_equal(h0, winheight(0))
1546 call assert_equal(1, &cmdheight)
Daniel Steinbergee630312022-01-10 13:36:34 +00001547 " check win_move_statusline from bottom window on top window ID
1548 let id = win_getid(1)
1549 for offset in range(5)
1550 call assert_true(win_move_statusline(id, offset))
1551 call assert_equal(h + offset, winheight(id))
1552 call assert_true(id->win_move_statusline(-offset))
1553 call assert_equal(h, winheight(id))
1554 endfor
1555 " check that win_move_statusline doesn't error with offsets beyond moving
1556 " possibility
1557 call assert_true(win_move_statusline(id, 5000))
1558 call assert_true(winheight(id) > h)
1559 call assert_true(win_move_statusline(id, -5000))
1560 call assert_true(winheight(id) < h)
1561 " check that win_move_statusline returns false for an invalid window
1562 wincmd =
1563 let h = winheight(0)
1564 call assert_false(win_move_statusline(-1, 1))
1565 call assert_equal(h, winheight(0))
1566 " check that win_move_statusline returns false for a popup window
1567 let id = popup_create(['hello', 'world'], {})
1568 let h = winheight(id)
1569 call assert_false(win_move_statusline(id, 1))
1570 call assert_equal(h, winheight(id))
1571 call popup_close(id)
1572 %bwipe!
1573endfunc
1574
Yegappan Lakshmanan72bb47e2022-04-03 11:22:38 +01001575" Test for window allocation failure
1576func Test_window_alloc_failure()
1577 %bw!
1578
1579 " test for creating a new window above current window
1580 call test_alloc_fail(GetAllocId('newwin_wvars'), 0, 0)
1581 call assert_fails('above new', 'E342:')
1582 call assert_equal(1, winnr('$'))
1583
1584 " test for creating a new window below current window
1585 call test_alloc_fail(GetAllocId('newwin_wvars'), 0, 0)
1586 call assert_fails('below new', 'E342:')
1587 call assert_equal(1, winnr('$'))
1588
1589 " test for popup window creation failure
1590 call test_alloc_fail(GetAllocId('newwin_wvars'), 0, 0)
1591 call assert_fails('call popup_create("Hello", {})', 'E342:')
1592 call assert_equal([], popup_list())
1593
1594 call test_alloc_fail(GetAllocId('newwin_wvars'), 0, 0)
1595 call assert_fails('split', 'E342:')
1596 call assert_equal(1, winnr('$'))
1597
Bram Moolenaarb18b4962022-09-02 21:55:50 +01001598 edit Xwaffile1
1599 edit Xwaffile2
Yegappan Lakshmanan72bb47e2022-04-03 11:22:38 +01001600 call test_alloc_fail(GetAllocId('newwin_wvars'), 0, 0)
Bram Moolenaarb18b4962022-09-02 21:55:50 +01001601 call assert_fails('sb Xwaffile1', 'E342:')
Yegappan Lakshmanan72bb47e2022-04-03 11:22:38 +01001602 call assert_equal(1, winnr('$'))
Bram Moolenaarb18b4962022-09-02 21:55:50 +01001603 call assert_equal('Xwaffile2', @%)
Yegappan Lakshmanan72bb47e2022-04-03 11:22:38 +01001604 %bw!
1605
1606 " FIXME: The following test crashes Vim
1607 " test for new tabpage creation failure
1608 " call test_alloc_fail(GetAllocId('newwin_wvars'), 0, 0)
1609 " call assert_fails('tabnew', 'E342:')
1610 " call assert_equal(1, tabpagenr('$'))
1611 " call assert_equal(1, winnr('$'))
1612
1613 " This test messes up the internal Vim window/frame information. So the
1614 " Test_window_cmd_cmdwin_with_vsp() test fails after running this test.
1615 " Open a new tab and close everything else to fix this issue.
1616 tabnew
1617 tabonly
1618endfunc
Bram Moolenaard0fb9072021-12-09 11:57:22 +00001619
Luuk van Baalfd7e60a2022-09-07 14:42:49 +01001620func Test_win_equal_last_status()
1621 let save_lines = &lines
1622 set lines=20
1623 set splitbelow
1624 set laststatus=0
1625
1626 split | split | quit
1627 call assert_equal(winheight(1), winheight(2))
1628
1629 let &lines = save_lines
1630 set splitbelow&
1631 set laststatus&
1632endfunc
1633
1634
Bram Moolenaar9e4d8212016-08-18 23:04:48 +02001635" vim: shiftwidth=2 sts=2 expandtab