blob: 04a5d1058d73d1579357608389ac9a2e9ba8651b [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
370func Test_window_width()
371 e Xa
372 vsplit Xb
373
374 let [ww1, ww2] = [winwidth(1), winwidth(2)]
375 " Active window (1) should have the same width or 1 more
376 " than the other window.
377 call assert_inrange(ww2, ww2 + 1, ww1)
378
379 wincmd <
380 call assert_equal(ww1 - 1, winwidth(1))
381 call assert_equal(ww2 + 1, winwidth(2))
382
383 wincmd >
384 call assert_equal(ww1, winwidth(1))
385 call assert_equal(ww2, winwidth(2))
386
387 2wincmd |
388 call assert_equal(2, winwidth(1))
389 call assert_equal(ww1 + ww2 - 2, winwidth(2))
390
391 wincmd =
392 call assert_equal(ww1, winwidth(1))
393 call assert_equal(ww2, winwidth(2))
394
395 2wincmd |
396 set winfixwidth
397 vsplit Xc
398 let [ww1, ww2, ww3] = [winwidth(1), winwidth(2), winwidth(3)]
Bram Moolenaar38e34832017-03-19 20:22:36 +0100399 call assert_equal(2, winwidth(2))
Bram Moolenaar4520d442017-03-19 16:09:46 +0100400 call assert_inrange(ww3, ww3 + 1, ww1)
401 3wincmd >
Bram Moolenaar38e34832017-03-19 20:22:36 +0100402 call assert_equal(2, winwidth(2))
Bram Moolenaar4520d442017-03-19 16:09:46 +0100403 call assert_equal(ww1 + 3, winwidth(1))
404 call assert_equal(ww3 - 3, winwidth(3))
405 wincmd =
Bram Moolenaar38e34832017-03-19 20:22:36 +0100406 call assert_equal(2, winwidth(2))
Bram Moolenaar4520d442017-03-19 16:09:46 +0100407 call assert_equal(ww1, winwidth(1))
408 call assert_equal(ww3, winwidth(3))
409
410 wincmd l
411 set winfixwidth&
412
413 wincmd =
414 let [ww1, ww2, ww3] = [winwidth(1), winwidth(2), winwidth(3)]
415 " Current window (2) should have the same width or 1 more
416 " than the other windows.
417 call assert_inrange(ww1, ww1 + 1, ww2)
418 call assert_inrange(ww3, ww3 + 1, ww2)
419
Yegappan Lakshmanan59585492021-06-12 13:46:41 +0200420 " when the current window width is less than the new 'winwidth', the current
421 " window width should be increased.
422 enew | only
423 split
424 10vnew
425 set winwidth=15
426 call assert_equal(15, winwidth(0))
427
428 %bw!
Bram Moolenaar4520d442017-03-19 16:09:46 +0100429endfunc
430
Bram Moolenaar8eeeba82017-06-25 22:45:39 +0200431func Test_equalalways_on_close()
432 set equalalways
433 vsplit
434 windo split
Bram Moolenaar1bbb6192018-11-10 16:02:01 +0100435 split
Bram Moolenaar8eeeba82017-06-25 22:45:39 +0200436 wincmd J
437 " now we have a frame top-left with two windows, a frame top-right with two
438 " windows and a frame at the bottom, full-width.
439 let height_1 = winheight(1)
440 let height_2 = winheight(2)
441 let height_3 = winheight(3)
442 let height_4 = winheight(4)
443 " closing the bottom window causes all windows to be resized.
444 close
445 call assert_notequal(height_1, winheight(1))
446 call assert_notequal(height_2, winheight(2))
447 call assert_notequal(height_3, winheight(3))
448 call assert_notequal(height_4, winheight(4))
449 call assert_equal(winheight(1), winheight(3))
450 call assert_equal(winheight(2), winheight(4))
451
452 1wincmd w
453 split
454 4wincmd w
455 resize + 5
456 " left column has three windows, equalized heights.
457 " right column has two windows, top one a bit higher
458 let height_1 = winheight(1)
459 let height_2 = winheight(2)
460 let height_4 = winheight(4)
461 let height_5 = winheight(5)
462 3wincmd w
463 " closing window in left column equalizes heights in left column but not in
464 " the right column
465 close
466 call assert_notequal(height_1, winheight(1))
467 call assert_notequal(height_2, winheight(2))
468 call assert_equal(height_4, winheight(3))
469 call assert_equal(height_5, winheight(4))
470
471 only
472 set equalalways&
473endfunc
474
Bram Moolenaar22044dc2017-12-02 15:43:37 +0100475func Test_win_screenpos()
Bram Moolenaar5a4c3082019-12-01 15:23:11 +0100476 CheckFeature quickfix
477
Bram Moolenaar22044dc2017-12-02 15:43:37 +0100478 call assert_equal(1, winnr('$'))
479 split
480 vsplit
481 10wincmd _
482 30wincmd |
483 call assert_equal([1, 1], win_screenpos(1))
484 call assert_equal([1, 32], win_screenpos(2))
485 call assert_equal([12, 1], win_screenpos(3))
486 call assert_equal([0, 0], win_screenpos(4))
Bram Moolenaar99fa7212020-04-26 15:59:55 +0200487 call assert_fails('let l = win_screenpos([])', 'E745:')
Bram Moolenaar22044dc2017-12-02 15:43:37 +0100488 only
489endfunc
490
Bram Moolenaar4520d442017-03-19 16:09:46 +0100491func Test_window_jump_tag()
Bram Moolenaar5a4c3082019-12-01 15:23:11 +0100492 CheckFeature quickfix
493
Bram Moolenaar4520d442017-03-19 16:09:46 +0100494 help
495 /iccf
496 call assert_match('^|iccf|', getline('.'))
497 call assert_equal(2, winnr('$'))
498 2wincmd }
499 call assert_equal(3, winnr('$'))
500 call assert_match('^|iccf|', getline('.'))
501 wincmd k
502 call assert_match('\*iccf\*', getline('.'))
503 call assert_equal(2, winheight(0))
504
505 wincmd z
506 set previewheight=4
507 help
508 /bugs
509 wincmd }
510 wincmd k
511 call assert_match('\*bugs\*', getline('.'))
512 call assert_equal(4, winheight(0))
513 set previewheight&
514
515 %bw!
516endfunc
517
518func Test_window_newtab()
519 e Xa
520
521 call assert_equal(1, tabpagenr('$'))
522 call assert_equal("\nAlready only one window", execute('wincmd T'))
523
524 split Xb
525 split Xc
526
527 wincmd T
528 call assert_equal(2, tabpagenr('$'))
529 call assert_equal(['Xb', 'Xa'], map(tabpagebuflist(1), 'bufname(v:val)'))
Bram Moolenaarce90e362019-09-08 18:58:44 +0200530 call assert_equal(['Xc' ], map(2->tabpagebuflist(), 'bufname(v:val)'))
Bram Moolenaar0e05de42020-03-25 22:23:46 +0100531 call assert_equal(['Xc' ], map(tabpagebuflist(), 'bufname(v:val)'))
Bram Moolenaar4520d442017-03-19 16:09:46 +0100532
533 %bw!
534endfunc
535
Bram Moolenaarf79225e2017-03-18 23:11:04 +0100536func Test_next_split_all()
537 " This was causing an illegal memory access.
538 n x
539 norm axxx
540 split
541 split
542 s/x
543 s/x
544 all
545 bwipe!
546endfunc
547
Bram Moolenaar75373f32017-08-07 22:02:30 +0200548" Tests for adjusting window and contents
549func GetScreenStr(row)
550 let str = ""
551 for c in range(1,3)
552 let str .= nr2char(screenchar(a:row, c))
553 endfor
554 return str
555endfunc
556
557func Test_window_contents()
558 enew! | only | new
559 call setline(1, range(1,256))
560
561 exe "norm! \<C-W>t\<C-W>=1Gzt\<C-W>w\<C-W>+"
562 redraw
Bram Moolenaar72cf47a2018-05-10 18:23:29 +0200563 let s3 = GetScreenStr(1)
Bram Moolenaar75373f32017-08-07 22:02:30 +0200564 wincmd p
565 call assert_equal(1, line("w0"))
566 call assert_equal('1 ', s3)
567
568 exe "norm! \<C-W>t\<C-W>=50Gzt\<C-W>w\<C-W>+"
569 redraw
Bram Moolenaar72cf47a2018-05-10 18:23:29 +0200570 let s3 = GetScreenStr(1)
Bram Moolenaar75373f32017-08-07 22:02:30 +0200571 wincmd p
572 call assert_equal(50, line("w0"))
573 call assert_equal('50 ', s3)
574
575 exe "norm! \<C-W>t\<C-W>=59Gzt\<C-W>w\<C-W>+"
576 redraw
Bram Moolenaar72cf47a2018-05-10 18:23:29 +0200577 let s3 = GetScreenStr(1)
Bram Moolenaar75373f32017-08-07 22:02:30 +0200578 wincmd p
579 call assert_equal(59, line("w0"))
580 call assert_equal('59 ', s3)
581
Bram Moolenaar8b633132020-03-20 18:20:51 +0100582 %d
583 call setline(1, ['one', 'two', 'three'])
584 call assert_equal(1, line('w0'))
585 call assert_equal(3, line('w$'))
586
Bram Moolenaar75373f32017-08-07 22:02:30 +0200587 bwipeout!
588 call test_garbagecollect_now()
589endfunc
590
Bram Moolenaar2efb3232017-12-19 12:27:23 +0100591func Test_window_colon_command()
592 " This was reading invalid memory.
593 exe "norm! v\<C-W>:\<C-U>echo v:version"
594endfunc
595
Bram Moolenaar6f361c92018-01-31 19:06:50 +0100596func Test_access_freed_mem()
Bram Moolenaar1417c762019-07-27 17:31:36 +0200597 call assert_equal(&columns, winwidth(0))
Bram Moolenaar9a046fd2021-01-28 13:47:59 +0100598 " This was accessing freed memory (but with what events?)
599 au BufEnter,BufLeave,WinEnter,WinLeave 0 vs xxx
Bram Moolenaar6f361c92018-01-31 19:06:50 +0100600 arg 0
601 argadd
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +0200602 call assert_fails("all", "E242:")
Bram Moolenaar6f361c92018-01-31 19:06:50 +0100603 au!
604 bwipe xxx
Bram Moolenaar1417c762019-07-27 17:31:36 +0200605 call assert_equal(&columns, winwidth(0))
Bram Moolenaar6f361c92018-01-31 19:06:50 +0100606endfunc
607
Bram Moolenaara27e1dc2019-10-07 22:27:36 +0200608func Test_insert_cleared_on_switch_to_term()
609 CheckFeature terminal
610
611 set showmode
612 terminal
613 wincmd p
614
615 call feedkeys("i\<C-O>", 'ntx')
616 redraw
617
618 " The "-- (insert) --" indicator should be visible.
619 let chars = map(range(1, &columns), 'nr2char(screenchar(&lines, v:val))')
620 let str = trim(join(chars, ''))
621 call assert_equal('-- (insert) --', str)
622
623 call feedkeys("\<C-W>p", 'ntx')
624 redraw
625
626 " The "-- (insert) --" indicator should have been cleared.
627 let chars = map(range(1, &columns), 'nr2char(screenchar(&lines, v:val))')
628 let str = trim(join(chars, ''))
629 call assert_equal('', str)
630
631 set showmode&
632 %bw!
633endfunc
634
Bram Moolenaar5bab5552018-04-13 20:41:29 +0200635func Test_visual_cleared_after_window_split()
636 new | only!
637 let smd_save = &showmode
638 set showmode
639 let ls_save = &laststatus
640 set laststatus=1
641 call setline(1, ['a', 'b', 'c', 'd', ''])
642 norm! G
643 exe "norm! kkvk"
644 redraw
645 exe "norm! \<C-W>v"
646 redraw
647 " check if '-- VISUAL --' disappeared from command line
648 let columns = range(1, &columns)
649 let cmdlinechars = map(columns, 'nr2char(screenchar(&lines, v:val))')
650 let cmdline = join(cmdlinechars, '')
651 let cmdline_ltrim = substitute(cmdline, '^\s*', "", "")
652 let mode_shown = substitute(cmdline_ltrim, '\s*$', "", "")
653 call assert_equal('', mode_shown)
654 let &showmode = smd_save
655 let &laststatus = ls_save
656 bwipe!
657endfunc
658
Bram Moolenaar72cf47a2018-05-10 18:23:29 +0200659func Test_winrestcmd()
660 2split
661 3vsplit
Bram Moolenaara0c8aea2021-03-20 19:55:35 +0100662 let restcmd = winrestcmd()
Bram Moolenaar72cf47a2018-05-10 18:23:29 +0200663 call assert_equal(2, winheight(0))
664 call assert_equal(3, winwidth(0))
665 wincmd =
666 call assert_notequal(2, winheight(0))
667 call assert_notequal(3, winwidth(0))
Bram Moolenaara0c8aea2021-03-20 19:55:35 +0100668 exe restcmd
Bram Moolenaar72cf47a2018-05-10 18:23:29 +0200669 call assert_equal(2, winheight(0))
670 call assert_equal(3, winwidth(0))
671 only
Bram Moolenaara0c8aea2021-03-20 19:55:35 +0100672
673 wincmd v
674 wincmd s
675 wincmd v
676 redraw
677 let restcmd = winrestcmd()
678 wincmd _
679 wincmd |
680 exe restcmd
681 redraw
682 call assert_equal(restcmd, winrestcmd())
683
684 only
Bram Moolenaar72cf47a2018-05-10 18:23:29 +0200685endfunc
686
Bram Moolenaar1e115362019-01-09 23:01:02 +0100687func Fun_RenewFile()
Bram Moolenaar026587b2019-08-17 15:08:00 +0200688 " Need to wait a bit for the timestamp to be older.
Bram Moolenaarce90e362019-09-08 18:58:44 +0200689 let old_ftime = getftime("tmp.txt")
690 while getftime("tmp.txt") == old_ftime
691 sleep 100m
692 silent execute '!echo "1" > tmp.txt'
693 endwhile
Bram Moolenaara42df592018-12-24 00:22:39 +0100694 sp
695 wincmd p
696 edit! tmp.txt
Bram Moolenaar1e115362019-01-09 23:01:02 +0100697endfunc
Bram Moolenaara42df592018-12-24 00:22:39 +0100698
699func Test_window_prevwin()
700 " Can we make this work on MS-Windows?
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +0200701 CheckUnix
Bram Moolenaara42df592018-12-24 00:22:39 +0100702
703 set hidden autoread
704 call writefile(['2'], 'tmp.txt')
705 new tmp.txt
706 q
Bram Moolenaara42df592018-12-24 00:22:39 +0100707 call Fun_RenewFile()
708 call assert_equal(2, winnr())
709 wincmd p
710 call assert_equal(1, winnr())
711 wincmd p
712 q
713 call Fun_RenewFile()
714 call assert_equal(2, winnr())
715 wincmd p
716 call assert_equal(1, winnr())
717 wincmd p
718 " reset
719 q
720 call delete('tmp.txt')
721 set hidden&vim autoread&vim
722 delfunc Fun_RenewFile
723endfunc
724
Bram Moolenaar8fcb60f2019-03-04 13:18:30 +0100725func Test_relative_cursor_position_in_one_line_window()
726 new
727 only
728 call setline(1, range(1, 10000))
729 normal 50%
730 let lnum = getcurpos()[1]
731 split
732 split
733 " make third window take as many lines as possible, other windows will
734 " become one line
735 3wincmd w
736 for i in range(1, &lines - 6)
737 wincmd +
738 redraw!
739 endfor
740
741 " first and second window should show cursor line
742 let wininfo = getwininfo()
743 call assert_equal(lnum, wininfo[0].topline)
744 call assert_equal(lnum, wininfo[1].topline)
745
746 only!
747 bwipe!
Yegappan Lakshmanan04c4c572022-08-30 19:48:24 +0100748 call assert_fails('call winrestview(test_null_dict())', 'E1297:')
Bram Moolenaar8fcb60f2019-03-04 13:18:30 +0100749endfunc
750
751func Test_relative_cursor_position_after_move_and_resize()
752 let so_save = &so
753 set so=0
754 enew
755 call setline(1, range(1, 10000))
756 normal 50%
757 split
758 1wincmd w
759 " Move cursor to first line in window
760 normal H
761 redraw!
762 " Reduce window height to two lines
763 let height = winheight(0)
764 while winheight(0) > 2
765 wincmd -
766 redraw!
767 endwhile
768 " move cursor to second/last line in window
769 normal j
770 " restore previous height
771 while winheight(0) < height
772 wincmd +
773 redraw!
774 endwhile
775 " make window two lines again
776 while winheight(0) > 2
777 wincmd -
778 redraw!
779 endwhile
780
781 " cursor should be at bottom line
782 let info = getwininfo(win_getid())[0]
783 call assert_equal(info.topline + 1, getcurpos()[1])
784
785 only!
786 bwipe!
787 let &so = so_save
788endfunc
789
790func Test_relative_cursor_position_after_resize()
791 let so_save = &so
792 set so=0
793 enew
794 call setline(1, range(1, 10000))
795 normal 50%
796 split
797 1wincmd w
798 let winid1 = win_getid()
799 let info = getwininfo(winid1)[0]
800 " Move cursor to second line in window
801 exe "normal " . (info.topline + 1) . "G"
802 redraw!
803 let lnum = getcurpos()[1]
804
805 " Make the window only two lines high, cursor should end up in top line
806 2wincmd w
807 exe (info.height - 2) . "wincmd +"
808 redraw!
809 let info = getwininfo(winid1)[0]
810 call assert_equal(lnum, info.topline)
811
812 only!
813 bwipe!
814 let &so = so_save
815endfunc
816
817func Test_relative_cursor_second_line_after_resize()
818 let so_save = &so
819 set so=0
820 enew
821 call setline(1, range(1, 10000))
822 normal 50%
823 split
824 1wincmd w
825 let winid1 = win_getid()
826 let info = getwininfo(winid1)[0]
827
828 " Make the window only two lines high
829 2wincmd _
830
831 " Move cursor to second line in window
832 normal H
833 normal j
834
835 " Make window size bigger, then back to 2 lines
836 for i in range(1, 10)
837 wincmd +
838 redraw!
839 endfor
840 for i in range(1, 10)
841 wincmd -
842 redraw!
843 endfor
844
845 " cursor should end up in bottom line
846 let info = getwininfo(winid1)[0]
847 call assert_equal(info.topline + 1, getcurpos()[1])
848
849 only!
850 bwipe!
851 let &so = so_save
852endfunc
853
Bram Moolenaara9b25352019-05-12 14:25:30 +0200854func Test_split_noscroll()
855 let so_save = &so
Bram Moolenaarbd2d68c2019-05-18 15:36:11 +0200856 enew
857 call setline(1, range(1, 8))
Bram Moolenaara9b25352019-05-12 14:25:30 +0200858 normal 100%
859 split
860
861 1wincmd w
862 let winid1 = win_getid()
863 let info1 = getwininfo(winid1)[0]
864
865 2wincmd w
866 let winid2 = win_getid()
867 let info2 = getwininfo(winid2)[0]
868
869 call assert_equal(1, info1.topline)
870 call assert_equal(1, info2.topline)
871
Bram Moolenaarbd2d68c2019-05-18 15:36:11 +0200872 " window that fits all lines by itself, but not when split: closing other
873 " window should restore fraction.
Bram Moolenaara9b25352019-05-12 14:25:30 +0200874 only!
Bram Moolenaarbd2d68c2019-05-18 15:36:11 +0200875 call setline(1, range(1, &lines - 10))
876 exe &lines / 4
877 let winid1 = win_getid()
878 let info1 = getwininfo(winid1)[0]
879 call assert_equal(1, info1.topline)
880 new
881 redraw
882 close
883 let info1 = getwininfo(winid1)[0]
884 call assert_equal(1, info1.topline)
885
Bram Moolenaara9b25352019-05-12 14:25:30 +0200886 bwipe!
887 let &so = so_save
888endfunc
889
Bram Moolenaar46ad2882019-04-08 20:01:47 +0200890" Tests for the winnr() function
891func Test_winnr()
892 only | tabonly
893 call assert_equal(1, winnr('j'))
894 call assert_equal(1, winnr('k'))
895 call assert_equal(1, winnr('h'))
896 call assert_equal(1, winnr('l'))
897
898 " create a set of horizontally and vertically split windows
899 leftabove new | wincmd p
900 leftabove new | wincmd p
901 rightbelow new | wincmd p
902 rightbelow new | wincmd p
903 leftabove vnew | wincmd p
904 leftabove vnew | wincmd p
905 rightbelow vnew | wincmd p
906 rightbelow vnew | wincmd p
907
908 call assert_equal(8, winnr('j'))
909 call assert_equal(2, winnr('k'))
910 call assert_equal(4, winnr('h'))
911 call assert_equal(6, winnr('l'))
912 call assert_equal(9, winnr('2j'))
913 call assert_equal(1, winnr('2k'))
914 call assert_equal(3, winnr('2h'))
915 call assert_equal(7, winnr('2l'))
916
917 " Error cases
918 call assert_fails("echo winnr('0.2k')", 'E15:')
919 call assert_equal(2, winnr('-2k'))
920 call assert_fails("echo winnr('-2xj')", 'E15:')
921 call assert_fails("echo winnr('j2j')", 'E15:')
922 call assert_fails("echo winnr('ll')", 'E15:')
923 call assert_fails("echo winnr('5')", 'E15:')
924 call assert_equal(4, winnr('0h'))
Bram Moolenaar99fa7212020-04-26 15:59:55 +0200925 call assert_fails("let w = winnr([])", 'E730:')
926 call assert_equal('unknown', win_gettype(-1))
927 call assert_equal(-1, winheight(-1))
928 call assert_equal(-1, winwidth(-1))
Bram Moolenaar46ad2882019-04-08 20:01:47 +0200929
930 tabnew
931 call assert_equal(8, tabpagewinnr(1, 'j'))
Bram Moolenaarce90e362019-09-08 18:58:44 +0200932 call assert_equal(2, 1->tabpagewinnr('k'))
Bram Moolenaar46ad2882019-04-08 20:01:47 +0200933 call assert_equal(4, tabpagewinnr(1, 'h'))
934 call assert_equal(6, tabpagewinnr(1, 'l'))
935
936 only | tabonly
937endfunc
938
Bram Moolenaarf92e58c2019-09-08 21:51:41 +0200939func Test_winrestview()
940 split runtest.vim
941 normal 50%
942 let view = winsaveview()
943 close
944 split runtest.vim
945 eval view->winrestview()
946 call assert_equal(view, winsaveview())
947
948 bwipe!
Yegappan Lakshmanan04c4c572022-08-30 19:48:24 +0100949 call assert_fails('call winrestview(test_null_dict())', 'E1297:')
Bram Moolenaarf92e58c2019-09-08 21:51:41 +0200950endfunc
951
Bram Moolenaard20dcb32019-09-10 21:22:58 +0200952func Test_win_splitmove()
Bram Moolenaar5a4c3082019-12-01 15:23:11 +0100953 CheckFeature quickfix
954
Bram Moolenaard20dcb32019-09-10 21:22:58 +0200955 edit a
956 leftabove split b
957 leftabove vsplit c
958 leftabove split d
959 call assert_equal(0, win_splitmove(winnr(), winnr('l')))
960 call assert_equal(bufname(winbufnr(1)), 'c')
961 call assert_equal(bufname(winbufnr(2)), 'd')
962 call assert_equal(bufname(winbufnr(3)), 'b')
963 call assert_equal(bufname(winbufnr(4)), 'a')
964 call assert_equal(0, win_splitmove(winnr(), winnr('j'), {'vertical': 1}))
965 call assert_equal(0, win_splitmove(winnr(), winnr('j'), {'vertical': 1}))
966 call assert_equal(bufname(winbufnr(1)), 'c')
967 call assert_equal(bufname(winbufnr(2)), 'b')
968 call assert_equal(bufname(winbufnr(3)), 'd')
969 call assert_equal(bufname(winbufnr(4)), 'a')
970 call assert_equal(0, win_splitmove(winnr(), winnr('k'), {'vertical': 1}))
971 call assert_equal(bufname(winbufnr(1)), 'd')
972 call assert_equal(bufname(winbufnr(2)), 'c')
973 call assert_equal(bufname(winbufnr(3)), 'b')
974 call assert_equal(bufname(winbufnr(4)), 'a')
975 call assert_equal(0, win_splitmove(winnr(), winnr('j'), {'rightbelow': v:true}))
976 call assert_equal(bufname(winbufnr(1)), 'c')
977 call assert_equal(bufname(winbufnr(2)), 'b')
978 call assert_equal(bufname(winbufnr(3)), 'a')
979 call assert_equal(bufname(winbufnr(4)), 'd')
Yegappan Lakshmanan04c4c572022-08-30 19:48:24 +0100980 call assert_fails('call win_splitmove(winnr(), winnr("k"), test_null_dict())', 'E1297:')
Bram Moolenaard20dcb32019-09-10 21:22:58 +0200981 only | bd
982
983 call assert_fails('call win_splitmove(winnr(), 123)', 'E957:')
984 call assert_fails('call win_splitmove(123, winnr())', 'E957:')
985 call assert_fails('call win_splitmove(winnr(), winnr())', 'E957:')
Bram Moolenaar7b94e772020-01-06 21:03:24 +0100986
987 tabnew
988 call assert_fails('call win_splitmove(1, win_getid(1, 1))', 'E957:')
989 tabclose
Bram Moolenaard20dcb32019-09-10 21:22:58 +0200990endfunc
991
Bram Moolenaare20b9ec2020-02-03 21:40:04 +0100992" Test for the :only command
993func Test_window_only()
994 new
995 set modified
996 new
997 call assert_fails('only', 'E445:')
998 only!
Bram Moolenaarbc2b71d2020-02-17 21:33:30 +0100999 " Test for :only with a count
1000 let wid = win_getid()
1001 new
1002 new
1003 3only
1004 call assert_equal(1, winnr('$'))
1005 call assert_equal(wid, win_getid())
1006 call assert_fails('close', 'E444:')
1007 call assert_fails('%close', 'E16:')
Bram Moolenaare20b9ec2020-02-03 21:40:04 +01001008endfunc
1009
Bram Moolenaar9f6277b2020-02-11 22:04:02 +01001010" Test for errors with :wincmd
1011func Test_wincmd_errors()
1012 call assert_fails('wincmd g', 'E474:')
1013 call assert_fails('wincmd ab', 'E474:')
1014endfunc
1015
1016" Test for errors with :winpos
1017func Test_winpos_errors()
1018 if !has("gui_running") && !has('win32')
1019 call assert_fails('winpos', 'E188:')
1020 endif
1021 call assert_fails('winpos 10', 'E466:')
1022endfunc
1023
Bram Moolenaar406cd902020-02-18 21:54:41 +01001024" Test for +cmd in a :split command
1025func Test_split_cmd()
1026 split +set\ readonly
1027 call assert_equal(1, &readonly)
1028 call assert_equal(2, winnr('$'))
1029 close
1030endfunc
1031
Bram Moolenaar5080b0a2020-03-22 21:23:47 +01001032" Create maximum number of horizontally or vertically split windows and then
1033" run commands that create a new horizontally/vertically split window
1034func Run_noroom_for_newwindow_test(dir_arg)
1035 let dir = (a:dir_arg == 'v') ? 'vert ' : ''
1036
1037 " Open as many windows as possible
Bram Moolenaarab505b12020-03-23 19:28:44 +01001038 while v:true
Bram Moolenaar5080b0a2020-03-22 21:23:47 +01001039 try
1040 exe dir . 'new'
1041 catch /E36:/
1042 break
1043 endtry
Bram Moolenaarab505b12020-03-23 19:28:44 +01001044 endwhile
Bram Moolenaar5080b0a2020-03-22 21:23:47 +01001045
1046 call writefile(['first', 'second', 'third'], 'Xfile1')
1047 call writefile([], 'Xfile2')
1048 call writefile([], 'Xfile3')
1049
1050 " Argument list related commands
1051 args Xfile1 Xfile2 Xfile3
1052 next
1053 for cmd in ['sargument 2', 'snext', 'sprevious', 'sNext', 'srewind',
1054 \ 'sfirst', 'slast']
1055 call assert_fails(dir .. cmd, 'E36:')
1056 endfor
1057 %argdelete
1058
1059 " Buffer related commands
1060 set modified
1061 hide enew
1062 for cmd in ['sbuffer Xfile1', 'sbnext', 'sbprevious', 'sbNext', 'sbrewind',
1063 \ 'sbfirst', 'sblast', 'sball', 'sbmodified', 'sunhide']
1064 call assert_fails(dir .. cmd, 'E36:')
1065 endfor
1066
1067 " Window related commands
1068 for cmd in ['split', 'split Xfile2', 'new', 'new Xfile3', 'sview Xfile1',
1069 \ 'sfind runtest.vim']
1070 call assert_fails(dir .. cmd, 'E36:')
1071 endfor
1072
1073 " Help
1074 call assert_fails(dir .. 'help', 'E36:')
1075 call assert_fails(dir .. 'helpgrep window', 'E36:')
1076
1077 " Command-line window
1078 if a:dir_arg == 'h'
1079 " Cmd-line window is always a horizontally split window
1080 call assert_beeps('call feedkeys("q:\<CR>", "xt")')
1081 endif
1082
1083 " Quickfix and location list window
1084 if has('quickfix')
1085 cexpr ''
1086 call assert_fails(dir .. 'copen', 'E36:')
1087 lexpr ''
1088 call assert_fails(dir .. 'lopen', 'E36:')
1089
1090 " Preview window
1091 call assert_fails(dir .. 'pedit Xfile2', 'E36:')
1092 call setline(1, 'abc')
1093 call assert_fails(dir .. 'psearch abc', 'E36:')
1094 endif
1095
1096 " Window commands (CTRL-W ^ and CTRL-W f)
1097 if a:dir_arg == 'h'
1098 call assert_fails('call feedkeys("\<C-W>^", "xt")', 'E36:')
1099 call setline(1, 'Xfile1')
1100 call assert_fails('call feedkeys("gg\<C-W>f", "xt")', 'E36:')
1101 endif
1102 enew!
1103
1104 " Tag commands (:stag, :stselect and :stjump)
1105 call writefile(["!_TAG_FILE_ENCODING\tutf-8\t//",
1106 \ "second\tXfile1\t2",
1107 \ "third\tXfile1\t3",],
1108 \ 'Xtags')
1109 set tags=Xtags
1110 call assert_fails(dir .. 'stag second', 'E36:')
1111 call assert_fails('call feedkeys(":" .. dir .. "stselect second\n1\n", "xt")', 'E36:')
1112 call assert_fails(dir .. 'stjump second', 'E36:')
1113 call assert_fails(dir .. 'ptag second', 'E36:')
1114 set tags&
1115 call delete('Xtags')
1116
1117 " :isplit and :dsplit
1118 call setline(1, ['#define FOO 1', 'FOO'])
1119 normal 2G
1120 call assert_fails(dir .. 'isplit FOO', 'E36:')
1121 call assert_fails(dir .. 'dsplit FOO', 'E36:')
1122
1123 " terminal
1124 if has('terminal')
1125 call assert_fails(dir .. 'terminal', 'E36:')
1126 endif
1127
1128 %bwipe!
1129 call delete('Xfile1')
1130 call delete('Xfile2')
1131 call delete('Xfile3')
1132 only
1133endfunc
1134
1135func Test_split_cmds_with_no_room()
1136 call Run_noroom_for_newwindow_test('h')
1137 call Run_noroom_for_newwindow_test('v')
1138endfunc
1139
Bram Moolenaarad48e6c2020-04-21 22:19:45 +02001140" Test for various wincmd failures
1141func Test_wincmd_fails()
1142 only!
1143 call assert_beeps("normal \<C-W>w")
1144 call assert_beeps("normal \<C-W>p")
1145 call assert_beeps("normal \<C-W>gk")
1146 call assert_beeps("normal \<C-W>r")
1147 call assert_beeps("normal \<C-W>K")
1148 call assert_beeps("normal \<C-W>H")
1149 call assert_beeps("normal \<C-W>2gt")
1150endfunc
1151
Bram Moolenaarfe6dce82020-09-04 14:41:21 +02001152func Test_window_resize()
1153 " Vertical :resize (absolute, relative, min and max size).
1154 vsplit
1155 vert resize 8
1156 call assert_equal(8, winwidth(0))
1157 vert resize +2
1158 call assert_equal(10, winwidth(0))
1159 vert resize -2
1160 call assert_equal(8, winwidth(0))
1161 vert resize
1162 call assert_equal(&columns - 2, winwidth(0))
1163 vert resize 0
1164 call assert_equal(1, winwidth(0))
1165 vert resize 99999
1166 call assert_equal(&columns - 2, winwidth(0))
1167
1168 %bwipe!
1169
1170 " Horizontal :resize (with absolute, relative size, min and max size).
1171 split
1172 resize 8
1173 call assert_equal(8, winheight(0))
1174 resize +2
1175 call assert_equal(10, winheight(0))
1176 resize -2
1177 call assert_equal(8, winheight(0))
1178 resize
1179 call assert_equal(&lines - 4, winheight(0))
1180 resize 0
1181 call assert_equal(1, winheight(0))
1182 resize 99999
1183 call assert_equal(&lines - 4, winheight(0))
1184
1185 " :resize with explicit window number.
1186 let other_winnr = winnr('j')
1187 exe other_winnr .. 'resize 10'
1188 call assert_equal(10, winheight(other_winnr))
1189 call assert_equal(&lines - 10 - 3, winheight(0))
Bram Moolenaar9668cc52020-10-17 17:39:55 +02001190 exe other_winnr .. 'resize +1'
1191 exe other_winnr .. 'resize +1'
1192 call assert_equal(12, winheight(other_winnr))
1193 call assert_equal(&lines - 10 - 3 -2, winheight(0))
Bram Moolenaar89015a62020-12-29 12:46:51 +01001194 close
1195
1196 vsplit
1197 wincmd l
1198 let other_winnr = winnr('h')
1199 call assert_notequal(winnr(), other_winnr)
Bram Moolenaar5efe0e52021-01-01 14:31:34 +01001200 exe 'vert ' .. other_winnr .. 'resize -' .. &columns
Bram Moolenaar89015a62020-12-29 12:46:51 +01001201 call assert_equal(0, winwidth(other_winnr))
Bram Moolenaarfe6dce82020-09-04 14:41:21 +02001202
1203 %bwipe!
1204endfunc
1205
Bram Moolenaar5d3c9f82020-06-26 20:41:39 +02001206" Test for adjusting the window width when a window is closed with some
1207" windows using 'winfixwidth'
1208func Test_window_width_adjust()
1209 only
1210 " Three vertical windows. Windows 1 and 2 have 'winfixwidth' set and close
1211 " window 2.
1212 wincmd v
1213 vert resize 10
1214 set winfixwidth
1215 wincmd v
1216 set winfixwidth
1217 wincmd c
1218 call assert_inrange(10, 12, winwidth(1))
1219 " Three vertical windows. Windows 2 and 3 have 'winfixwidth' set and close
1220 " window 3.
1221 only
1222 set winfixwidth
1223 wincmd v
1224 vert resize 10
1225 set winfixwidth
1226 wincmd v
1227 set nowinfixwidth
1228 wincmd b
1229 wincmd c
1230 call assert_inrange(10, 12, winwidth(2))
1231
1232 new | only
1233endfunc
1234
1235" Test for jumping to a vertical/horizontal neighbor window based on the
1236" current cursor position
1237func Test_window_goto_neightbor()
1238 %bw!
1239
1240 " Vertical window movement
1241
1242 " create the following window layout:
1243 " +--+--+
1244 " |w1|w3|
1245 " +--+ |
1246 " |w2| |
1247 " +--+--+
1248 " |w4 |
1249 " +-----+
1250 new
1251 vsplit
1252 split
1253 " vertically jump from w4
1254 wincmd b
1255 call setline(1, repeat(' ', &columns))
1256 call cursor(1, 1)
1257 wincmd k
1258 call assert_equal(2, winnr())
1259 wincmd b
1260 call cursor(1, &columns)
1261 redraw!
1262 wincmd k
1263 call assert_equal(3, winnr())
1264 %bw!
1265
1266 " create the following window layout:
1267 " +--+--+--+
1268 " |w1|w2|w3|
1269 " +--+--+--+
1270 " |w4 |
1271 " +--------+
1272 new
1273 vsplit
1274 vsplit
1275 wincmd b
1276 call setline(1, repeat(' ', &columns))
1277 call cursor(1, 1)
1278 wincmd k
1279 call assert_equal(1, winnr())
1280 wincmd b
1281 call cursor(1, &columns / 2)
1282 redraw!
1283 wincmd k
1284 call assert_equal(2, winnr())
1285 wincmd b
1286 call cursor(1, &columns)
1287 redraw!
1288 wincmd k
1289 call assert_equal(3, winnr())
1290 %bw!
1291
1292 " Horizontal window movement
1293
1294 " create the following window layout:
1295 " +--+--+--+
1296 " |w1|w2|w4|
1297 " +--+--+ |
1298 " |w3 | |
1299 " +-----+--+
1300 vsplit
1301 split
1302 vsplit
1303 4wincmd l
1304 call setline(1, repeat([' '], &lines))
1305 call cursor(1, 1)
1306 redraw!
1307 wincmd h
1308 call assert_equal(2, winnr())
1309 4wincmd l
1310 call cursor(&lines, 1)
1311 redraw!
1312 wincmd h
1313 call assert_equal(3, winnr())
1314 %bw!
1315
1316 " create the following window layout:
1317 " +--+--+
1318 " |w1|w4|
1319 " +--+ +
1320 " |w2| |
1321 " +--+ +
1322 " |w3| |
1323 " +--+--+
1324 vsplit
1325 split
1326 split
1327 wincmd l
1328 call setline(1, repeat([' '], &lines))
1329 call cursor(1, 1)
1330 redraw!
1331 wincmd h
1332 call assert_equal(1, winnr())
1333 wincmd l
1334 call cursor(&lines / 2, 1)
1335 redraw!
1336 wincmd h
1337 call assert_equal(2, winnr())
1338 wincmd l
1339 call cursor(&lines, 1)
1340 redraw!
1341 wincmd h
1342 call assert_equal(3, winnr())
1343 %bw!
1344endfunc
1345
1346" Test for an autocmd closing the destination window when jumping from one
1347" window to another.
1348func Test_close_dest_window()
1349 split
1350 edit Xfile
1351
1352 " Test for BufLeave
1353 augroup T1
1354 au!
1355 au BufLeave Xfile $wincmd c
1356 augroup END
1357 wincmd b
1358 call assert_equal(1, winnr('$'))
1359 call assert_equal('Xfile', @%)
1360 augroup T1
1361 au!
1362 augroup END
1363
1364 " Test for WinLeave
1365 new
1366 wincmd p
1367 augroup T1
1368 au!
1369 au WinLeave * 1wincmd c
1370 augroup END
1371 wincmd t
1372 call assert_equal(1, winnr('$'))
1373 call assert_equal('Xfile', @%)
1374 augroup T1
1375 au!
1376 augroup END
1377 augroup! T1
1378 %bw!
1379endfunc
1380
Bram Moolenaard0fb9072021-12-09 11:57:22 +00001381func Test_window_minimal_size()
1382 set winminwidth=0 winminheight=0
1383
1384 " check size is fixed vertically
1385 new
1386 call win_execute(win_getid(2), 'wincmd _')
1387 call assert_equal(0, winheight(0))
1388 call feedkeys('0', 'tx')
1389 call assert_equal(1, winheight(0))
1390 bwipe!
1391
1392 " check size is fixed horizontally
1393 vert new
1394 call win_execute(win_getid(2), 'wincmd |')
1395 call assert_equal(0, winwidth(0))
1396 call feedkeys('0', 'tx')
1397 call assert_equal(1, winwidth(0))
1398 bwipe!
1399
1400 if has('timers')
1401 " check size is fixed in Insert mode
ichizokfa9a8e02021-12-12 16:42:09 +00001402 func s:CheckSize(timer) abort
1403 call win_execute(win_getid(2), 'wincmd _')
1404 call assert_equal(0, winheight(0))
1405 call feedkeys(" \<Esc>", 't!')
1406 endfunc
Bram Moolenaard0fb9072021-12-09 11:57:22 +00001407 new
ichizokfa9a8e02021-12-12 16:42:09 +00001408 call timer_start(100, function('s:CheckSize'))
Bram Moolenaard0fb9072021-12-09 11:57:22 +00001409 call feedkeys('a', 'tx!')
1410 call assert_equal(1, winheight(0))
1411 bwipe!
ichizokfa9a8e02021-12-12 16:42:09 +00001412 delfunc s:CheckSize
Bram Moolenaard0fb9072021-12-09 11:57:22 +00001413 endif
1414
1415 set winminwidth& winminheight&
1416endfunc
1417
Daniel Steinbergee630312022-01-10 13:36:34 +00001418func Test_win_move_separator()
1419 edit a
1420 leftabove vsplit b
1421 let w = winwidth(0)
1422 " check win_move_separator from left window on left window
1423 call assert_equal(1, winnr())
1424 for offset in range(5)
1425 call assert_true(win_move_separator(0, offset))
1426 call assert_equal(w + offset, winwidth(0))
1427 call assert_true(0->win_move_separator(-offset))
1428 call assert_equal(w, winwidth(0))
1429 endfor
1430 " check win_move_separator from right window on left window number
1431 wincmd l
1432 call assert_notequal(1, winnr())
1433 for offset in range(5)
1434 call assert_true(1->win_move_separator(offset))
1435 call assert_equal(w + offset, winwidth(1))
1436 call assert_true(win_move_separator(1, -offset))
1437 call assert_equal(w, winwidth(1))
1438 endfor
1439 " check win_move_separator from right window on left window ID
1440 let id = win_getid(1)
1441 for offset in range(5)
1442 call assert_true(win_move_separator(id, offset))
1443 call assert_equal(w + offset, winwidth(id))
1444 call assert_true(id->win_move_separator(-offset))
1445 call assert_equal(w, winwidth(id))
1446 endfor
zeertzjqa0c4e2f2022-01-29 10:59:53 +00001447 " check win_move_separator from right window on right window is no-op
1448 let w0 = winwidth(0)
1449 call assert_true(win_move_separator(0, 1))
1450 call assert_equal(w0, winwidth(0))
1451 call assert_true(win_move_separator(0, -1))
1452 call assert_equal(w0, winwidth(0))
Daniel Steinbergee630312022-01-10 13:36:34 +00001453 " check that win_move_separator doesn't error with offsets beyond moving
1454 " possibility
1455 call assert_true(win_move_separator(id, 5000))
1456 call assert_true(winwidth(id) > w)
1457 call assert_true(win_move_separator(id, -5000))
1458 call assert_true(winwidth(id) < w)
1459 " check that win_move_separator returns false for an invalid window
1460 wincmd =
1461 let w = winwidth(0)
1462 call assert_false(win_move_separator(-1, 1))
1463 call assert_equal(w, winwidth(0))
1464 " check that win_move_separator returns false for a popup window
1465 let id = popup_create(['hello', 'world'], {})
1466 let w = winwidth(id)
1467 call assert_false(win_move_separator(id, 1))
1468 call assert_equal(w, winwidth(id))
1469 call popup_close(id)
1470 %bwipe!
1471endfunc
1472
1473func Test_win_move_statusline()
1474 edit a
1475 leftabove split b
1476 let h = winheight(0)
1477 " check win_move_statusline from top window on top window
1478 call assert_equal(1, winnr())
1479 for offset in range(5)
1480 call assert_true(win_move_statusline(0, offset))
1481 call assert_equal(h + offset, winheight(0))
1482 call assert_true(0->win_move_statusline(-offset))
1483 call assert_equal(h, winheight(0))
1484 endfor
1485 " check win_move_statusline from bottom window on top window number
1486 wincmd j
1487 call assert_notequal(1, winnr())
1488 for offset in range(5)
1489 call assert_true(1->win_move_statusline(offset))
1490 call assert_equal(h + offset, winheight(1))
1491 call assert_true(win_move_statusline(1, -offset))
1492 call assert_equal(h, winheight(1))
1493 endfor
zeertzjqa0c4e2f2022-01-29 10:59:53 +00001494 " check win_move_statusline from bottom window on bottom window
1495 let h0 = winheight(0)
1496 for offset in range(5)
1497 call assert_true(0->win_move_statusline(-offset))
1498 call assert_equal(h0 - offset, winheight(0))
1499 call assert_equal(1 + offset, &cmdheight)
1500 call assert_true(win_move_statusline(0, offset))
1501 call assert_equal(h0, winheight(0))
1502 call assert_equal(1, &cmdheight)
1503 endfor
1504 call assert_true(win_move_statusline(0, 1))
Bram Moolenaara2a89732022-08-31 14:46:18 +01001505 call assert_equal(h0, winheight(0))
1506 call assert_equal(1, &cmdheight)
Daniel Steinbergee630312022-01-10 13:36:34 +00001507 " check win_move_statusline from bottom window on top window ID
1508 let id = win_getid(1)
1509 for offset in range(5)
1510 call assert_true(win_move_statusline(id, offset))
1511 call assert_equal(h + offset, winheight(id))
1512 call assert_true(id->win_move_statusline(-offset))
1513 call assert_equal(h, winheight(id))
1514 endfor
1515 " check that win_move_statusline doesn't error with offsets beyond moving
1516 " possibility
1517 call assert_true(win_move_statusline(id, 5000))
1518 call assert_true(winheight(id) > h)
1519 call assert_true(win_move_statusline(id, -5000))
1520 call assert_true(winheight(id) < h)
1521 " check that win_move_statusline returns false for an invalid window
1522 wincmd =
1523 let h = winheight(0)
1524 call assert_false(win_move_statusline(-1, 1))
1525 call assert_equal(h, winheight(0))
1526 " check that win_move_statusline returns false for a popup window
1527 let id = popup_create(['hello', 'world'], {})
1528 let h = winheight(id)
1529 call assert_false(win_move_statusline(id, 1))
1530 call assert_equal(h, winheight(id))
1531 call popup_close(id)
1532 %bwipe!
1533endfunc
1534
Yegappan Lakshmanan72bb47e2022-04-03 11:22:38 +01001535" Test for window allocation failure
1536func Test_window_alloc_failure()
1537 %bw!
1538
1539 " test for creating a new window above current window
1540 call test_alloc_fail(GetAllocId('newwin_wvars'), 0, 0)
1541 call assert_fails('above new', 'E342:')
1542 call assert_equal(1, winnr('$'))
1543
1544 " test for creating a new window below current window
1545 call test_alloc_fail(GetAllocId('newwin_wvars'), 0, 0)
1546 call assert_fails('below new', 'E342:')
1547 call assert_equal(1, winnr('$'))
1548
1549 " test for popup window creation failure
1550 call test_alloc_fail(GetAllocId('newwin_wvars'), 0, 0)
1551 call assert_fails('call popup_create("Hello", {})', 'E342:')
1552 call assert_equal([], popup_list())
1553
1554 call test_alloc_fail(GetAllocId('newwin_wvars'), 0, 0)
1555 call assert_fails('split', 'E342:')
1556 call assert_equal(1, winnr('$'))
1557
1558 edit Xfile1
1559 edit Xfile2
1560 call test_alloc_fail(GetAllocId('newwin_wvars'), 0, 0)
1561 call assert_fails('sb Xfile1', 'E342:')
1562 call assert_equal(1, winnr('$'))
1563 call assert_equal('Xfile2', @%)
1564 %bw!
1565
1566 " FIXME: The following test crashes Vim
1567 " test for new tabpage creation failure
1568 " call test_alloc_fail(GetAllocId('newwin_wvars'), 0, 0)
1569 " call assert_fails('tabnew', 'E342:')
1570 " call assert_equal(1, tabpagenr('$'))
1571 " call assert_equal(1, winnr('$'))
1572
1573 " This test messes up the internal Vim window/frame information. So the
1574 " Test_window_cmd_cmdwin_with_vsp() test fails after running this test.
1575 " Open a new tab and close everything else to fix this issue.
1576 tabnew
1577 tabonly
1578endfunc
Bram Moolenaard0fb9072021-12-09 11:57:22 +00001579
Bram Moolenaar9e4d8212016-08-18 23:04:48 +02001580" vim: shiftwidth=2 sts=2 expandtab