blob: 2ee4417878cae3b941afab2a786b4b16deebf236 [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
Luuk van Baal20e58562022-09-23 12:57:09 +01004source screendump.vim
Bram Moolenaara27e1dc2019-10-07 22:27:36 +02005
Bram Moolenaar991dea32016-05-24 11:31:32 +02006func Test_window_cmd_ls0_with_split()
7 set ls=0
8 set splitbelow
9 split
10 quit
11 call assert_equal(0, &lines - &cmdheight - winheight(0))
12 new | only!
13 "
14 set splitbelow&vim
15 botright split
16 quit
17 call assert_equal(0, &lines - &cmdheight - winheight(0))
18 new | only!
19 set ls&vim
20endfunc
21
22func Test_window_cmd_cmdwin_with_vsp()
Bram Moolenaar21829c52021-01-26 22:42:21 +010023 CheckFeature cmdwin
24
Bram Moolenaar72cf47a2018-05-10 18:23:29 +020025 let efmt = 'Expected 0 but got %d (in ls=%d, %s window)'
Bram Moolenaar991dea32016-05-24 11:31:32 +020026 for v in range(0, 2)
27 exec "set ls=" . v
28 vsplit
29 call feedkeys("q:\<CR>")
30 let ac = &lines - (&cmdheight + winheight(0) + !!v)
31 let emsg = printf(efmt, ac, v, 'left')
32 call assert_equal(0, ac, emsg)
33 wincmd w
34 let ac = &lines - (&cmdheight + winheight(0) + !!v)
35 let emsg = printf(efmt, ac, v, 'right')
36 call assert_equal(0, ac, emsg)
37 new | only!
38 endfor
39 set ls&vim
40endfunc
41
Bram Moolenaar96bde992022-08-10 17:23:12 +010042func Test_cmdheight_not_changed()
43 set cmdheight=2
44 set winminheight=0
45 augroup Maximize
46 autocmd WinEnter * wincmd _
47 augroup END
48 split
49 tabnew
50 tabfirst
51 call assert_equal(2, &cmdheight)
52
53 tabonly!
54 only
55 set winminwidth& cmdheight&
56 augroup Maximize
57 au!
58 augroup END
59 augroup! Maximize
60endfunc
61
Bram Moolenaar5d3c9f82020-06-26 20:41:39 +020062" Test for jumping to windows
63func Test_window_jump()
64 new
65 " jumping to a window with a count greater than the max windows
66 exe "normal 4\<C-W>w"
67 call assert_equal(2, winnr())
68 only
69endfunc
70
71func Test_window_cmd_wincmd_gf()
Bram Moolenaar5d2ca042016-06-26 17:11:21 +020072 let fname = 'test_gf.txt'
73 let swp_fname = '.' . fname . '.swp'
74 call writefile([], fname)
75 call writefile([], swp_fname)
76 function s:swap_exists()
77 let v:swapchoice = s:swap_choice
78 endfunc
Bram Moolenaareaa49e42019-07-13 18:08:59 +020079 " Remove the catch-all that runtest.vim adds
80 au! SwapExists
Bram Moolenaar5d2ca042016-06-26 17:11:21 +020081 augroup test_window_cmd_wincmd_gf
82 autocmd!
83 exec "autocmd SwapExists " . fname . " call s:swap_exists()"
84 augroup END
85
86 call setline(1, fname)
87 " (E)dit anyway
88 let s:swap_choice = 'e'
89 wincmd gf
90 call assert_equal(2, tabpagenr())
91 call assert_equal(fname, bufname("%"))
92 quit!
93
94 " (Q)uit
95 let s:swap_choice = 'q'
96 wincmd gf
97 call assert_equal(1, tabpagenr())
98 call assert_notequal(fname, bufname("%"))
99 new | only!
100
101 call delete(fname)
102 call delete(swp_fname)
103 augroup! test_window_cmd_wincmd_gf
104endfunc
105
Bram Moolenaar4520d442017-03-19 16:09:46 +0100106func Test_window_quit()
107 e Xa
108 split Xb
Bram Moolenaarf92e58c2019-09-08 21:51:41 +0200109 call assert_equal(2, '$'->winnr())
Bram Moolenaar4520d442017-03-19 16:09:46 +0100110 call assert_equal('Xb', bufname(winbufnr(1)))
111 call assert_equal('Xa', bufname(winbufnr(2)))
112
113 wincmd q
114 call assert_equal(1, winnr('$'))
115 call assert_equal('Xa', bufname(winbufnr(1)))
116
117 bw Xa Xb
118endfunc
119
120func Test_window_horizontal_split()
121 call assert_equal(1, winnr('$'))
122 3wincmd s
123 call assert_equal(2, winnr('$'))
124 call assert_equal(3, winheight(0))
Bram Moolenaarf92e58c2019-09-08 21:51:41 +0200125 call assert_equal(winwidth(1), 2->winwidth())
Bram Moolenaar4520d442017-03-19 16:09:46 +0100126
127 call assert_fails('botright topleft wincmd s', 'E442:')
128 bw
129endfunc
130
131func Test_window_vertical_split()
132 call assert_equal(1, winnr('$'))
133 3wincmd v
134 call assert_equal(2, winnr('$'))
135 call assert_equal(3, winwidth(0))
136 call assert_equal(winheight(1), winheight(2))
137
138 call assert_fails('botright topleft wincmd v', 'E442:')
139 bw
140endfunc
141
Bram Moolenaar1bbb6192018-11-10 16:02:01 +0100142" Test the ":wincmd ^" and "<C-W>^" commands.
Bram Moolenaar4520d442017-03-19 16:09:46 +0100143func Test_window_split_edit_alternate()
Bram Moolenaar4520d442017-03-19 16:09:46 +0100144
Bram Moolenaar1bbb6192018-11-10 16:02:01 +0100145 " Test for failure when the alternate buffer/file no longer exists.
146 edit Xfoo | %bw
Bram Moolenaare2e40752020-09-04 21:18:46 +0200147 call assert_fails(':wincmd ^', 'E23:')
Bram Moolenaar1bbb6192018-11-10 16:02:01 +0100148
149 " Test for the expected behavior when we have two named buffers.
150 edit Xfoo | edit Xbar
Bram Moolenaar4520d442017-03-19 16:09:46 +0100151 wincmd ^
Bram Moolenaar1bbb6192018-11-10 16:02:01 +0100152 call assert_equal('Xfoo', bufname(winbufnr(1)))
153 call assert_equal('Xbar', bufname(winbufnr(2)))
154 only
Bram Moolenaar4520d442017-03-19 16:09:46 +0100155
Bram Moolenaar1bbb6192018-11-10 16:02:01 +0100156 " Test for the expected behavior when the alternate buffer is not named.
157 enew | let l:nr1 = bufnr('%')
158 edit Xfoo | let l:nr2 = bufnr('%')
159 wincmd ^
160 call assert_equal(l:nr1, winbufnr(1))
161 call assert_equal(l:nr2, winbufnr(2))
162 only
163
Bram Moolenaard42333d2018-11-10 20:28:19 +0100164 " FIXME: this currently fails on AppVeyor, but passes locally
165 if !has('win32')
166 " Test the Normal mode command.
167 call feedkeys("\<C-W>\<C-^>", 'tx')
168 call assert_equal(l:nr2, winbufnr(1))
169 call assert_equal(l:nr1, winbufnr(2))
170 endif
Bram Moolenaar1bbb6192018-11-10 16:02:01 +0100171
172 %bw!
173endfunc
174
175" Test the ":[count]wincmd ^" and "[count]<C-W>^" commands.
176func Test_window_split_edit_bufnr()
177
178 %bwipeout
179 let l:nr = bufnr('%') + 1
Bram Moolenaare2e40752020-09-04 21:18:46 +0200180 call assert_fails(':execute "normal! ' . l:nr . '\<C-W>\<C-^>"', 'E92:')
181 call assert_fails(':' . l:nr . 'wincmd ^', 'E16:')
182 call assert_fails(':0wincmd ^', 'E16:')
Bram Moolenaar1bbb6192018-11-10 16:02:01 +0100183
184 edit Xfoo | edit Xbar | edit Xbaz
185 let l:foo_nr = bufnr('Xfoo')
186 let l:bar_nr = bufnr('Xbar')
187 let l:baz_nr = bufnr('Xbaz')
188
Bram Moolenaar8617b402018-11-10 20:47:48 +0100189 " FIXME: this currently fails on AppVeyor, but passes locally
190 if !has('win32')
191 call feedkeys(l:foo_nr . "\<C-W>\<C-^>", 'tx')
192 call assert_equal('Xfoo', bufname(winbufnr(1)))
193 call assert_equal('Xbaz', bufname(winbufnr(2)))
194 only
Bram Moolenaar1bbb6192018-11-10 16:02:01 +0100195
Bram Moolenaar8617b402018-11-10 20:47:48 +0100196 call feedkeys(l:bar_nr . "\<C-W>\<C-^>", 'tx')
197 call assert_equal('Xbar', bufname(winbufnr(1)))
198 call assert_equal('Xfoo', bufname(winbufnr(2)))
199 only
Bram Moolenaar1bbb6192018-11-10 16:02:01 +0100200
Bram Moolenaar8617b402018-11-10 20:47:48 +0100201 execute l:baz_nr . 'wincmd ^'
202 call assert_equal('Xbaz', bufname(winbufnr(1)))
203 call assert_equal('Xbar', bufname(winbufnr(2)))
204 endif
Bram Moolenaar1bbb6192018-11-10 16:02:01 +0100205
206 %bw!
Bram Moolenaar4520d442017-03-19 16:09:46 +0100207endfunc
208
Bram Moolenaar9b9be002020-03-22 14:41:22 +0100209func Test_window_split_no_room()
210 " N horizontal windows need >= 2*N + 1 lines:
211 " - 1 line + 1 status line in each window
212 " - 1 Ex command line
213 "
214 " 2*N + 1 <= &lines
215 " N <= (lines - 1)/2
216 "
217 " Beyond that number of windows, E36: Not enough room is expected.
218 let hor_win_count = (&lines - 1)/2
219 let hor_split_count = hor_win_count - 1
220 for s in range(1, hor_split_count) | split | endfor
221 call assert_fails('split', 'E36:')
222
223 " N vertical windows need >= 2*(N - 1) + 1 columns:
224 " - 1 column + 1 separator for each window (except last window)
225 " - 1 column for the last window which does not have separator
226 "
227 " 2*(N - 1) + 1 <= &columns
228 " 2*N - 1 <= &columns
229 " N <= (&columns + 1)/2
230 let ver_win_count = (&columns + 1)/2
231 let ver_split_count = ver_win_count - 1
232 for s in range(1, ver_split_count) | vsplit | endfor
233 call assert_fails('vsplit', 'E36:')
234
235 %bw!
236endfunc
237
Bram Moolenaar4520d442017-03-19 16:09:46 +0100238func Test_window_exchange()
239 e Xa
240
241 " Nothing happens with window exchange when there is 1 window
242 wincmd x
243 call assert_equal(1, winnr('$'))
244
245 split Xb
246 split Xc
247
248 call assert_equal('Xc', bufname(winbufnr(1)))
249 call assert_equal('Xb', bufname(winbufnr(2)))
250 call assert_equal('Xa', bufname(winbufnr(3)))
251
252 " Exchange current window 1 with window 3
253 3wincmd x
254 call assert_equal('Xa', bufname(winbufnr(1)))
255 call assert_equal('Xb', bufname(winbufnr(2)))
256 call assert_equal('Xc', bufname(winbufnr(3)))
257
258 " Exchange window with next when at the top window
259 wincmd x
260 call assert_equal('Xb', bufname(winbufnr(1)))
261 call assert_equal('Xa', bufname(winbufnr(2)))
262 call assert_equal('Xc', bufname(winbufnr(3)))
263
264 " Exchange window with next when at the middle window
265 wincmd j
266 wincmd x
267 call assert_equal('Xb', bufname(winbufnr(1)))
268 call assert_equal('Xc', bufname(winbufnr(2)))
269 call assert_equal('Xa', bufname(winbufnr(3)))
270
271 " Exchange window with next when at the bottom window.
272 " When there is no next window, it exchanges with the previous window.
273 wincmd j
274 wincmd x
275 call assert_equal('Xb', bufname(winbufnr(1)))
276 call assert_equal('Xa', bufname(winbufnr(2)))
277 call assert_equal('Xc', bufname(winbufnr(3)))
278
279 bw Xa Xb Xc
280endfunc
281
282func Test_window_rotate()
283 e Xa
284 split Xb
285 split Xc
286 call assert_equal('Xc', bufname(winbufnr(1)))
287 call assert_equal('Xb', bufname(winbufnr(2)))
288 call assert_equal('Xa', bufname(winbufnr(3)))
289
290 " Rotate downwards
291 wincmd r
292 call assert_equal('Xa', bufname(winbufnr(1)))
293 call assert_equal('Xc', bufname(winbufnr(2)))
294 call assert_equal('Xb', bufname(winbufnr(3)))
295
296 2wincmd r
297 call assert_equal('Xc', bufname(winbufnr(1)))
298 call assert_equal('Xb', bufname(winbufnr(2)))
299 call assert_equal('Xa', bufname(winbufnr(3)))
300
301 " Rotate upwards
302 wincmd R
303 call assert_equal('Xb', bufname(winbufnr(1)))
304 call assert_equal('Xa', bufname(winbufnr(2)))
305 call assert_equal('Xc', bufname(winbufnr(3)))
306
307 2wincmd R
308 call assert_equal('Xc', bufname(winbufnr(1)))
309 call assert_equal('Xb', bufname(winbufnr(2)))
310 call assert_equal('Xa', bufname(winbufnr(3)))
311
312 bot vsplit
313 call assert_fails('wincmd R', 'E443:')
314
315 bw Xa Xb Xc
316endfunc
317
318func Test_window_height()
319 e Xa
320 split Xb
321
322 let [wh1, wh2] = [winheight(1), winheight(2)]
323 " Active window (1) should have the same height or 1 more
324 " than the other window.
325 call assert_inrange(wh2, wh2 + 1, wh1)
326
327 wincmd -
328 call assert_equal(wh1 - 1, winheight(1))
329 call assert_equal(wh2 + 1, winheight(2))
330
331 wincmd +
332 call assert_equal(wh1, winheight(1))
Bram Moolenaarf92e58c2019-09-08 21:51:41 +0200333 call assert_equal(wh2, 2->winheight())
Bram Moolenaar4520d442017-03-19 16:09:46 +0100334
335 2wincmd _
336 call assert_equal(2, winheight(1))
337 call assert_equal(wh1 + wh2 - 2, winheight(2))
338
339 wincmd =
340 call assert_equal(wh1, winheight(1))
341 call assert_equal(wh2, winheight(2))
342
343 2wincmd _
344 set winfixheight
345 split Xc
346 let [wh1, wh2, wh3] = [winheight(1), winheight(2), winheight(3)]
347 call assert_equal(2, winheight(2))
348 call assert_inrange(wh3, wh3 + 1, wh1)
349 3wincmd +
350 call assert_equal(2, winheight(2))
351 call assert_equal(wh1 + 3, winheight(1))
352 call assert_equal(wh3 - 3, winheight(3))
353 wincmd =
354 call assert_equal(2, winheight(2))
355 call assert_equal(wh1, winheight(1))
356 call assert_equal(wh3, winheight(3))
357
358 wincmd j
359 set winfixheight&
360
361 wincmd =
362 let [wh1, wh2, wh3] = [winheight(1), winheight(2), winheight(3)]
363 " Current window (2) should have the same height or 1 more
364 " than the other windows.
365 call assert_inrange(wh1, wh1 + 1, wh2)
366 call assert_inrange(wh3, wh3 + 1, wh2)
367
368 bw Xa Xb Xc
369endfunc
370
Bram Moolenaar21c3a802022-08-31 17:49:14 +0100371func Test_wincmd_equal()
372 edit Xone
373 below split Xtwo
374 rightbelow vsplit Xthree
375 call assert_equal('Xone', bufname(winbufnr(1)))
376 call assert_equal('Xtwo', bufname(winbufnr(2)))
377 call assert_equal('Xthree', bufname(winbufnr(3)))
378
379 " Xone and Xtwo should be about the same height
380 let [wh1, wh2] = [winheight(1), winheight(2)]
381 call assert_inrange(wh1 - 1, wh1 + 1, wh2)
382 " Xtwo and Xthree should be about the same width
383 let [ww2, ww3] = [winwidth(2), winwidth(3)]
384 call assert_inrange(ww2 - 1, ww2 + 1, ww3)
385
386 1wincmd w
387 10wincmd _
388 2wincmd w
389 20wincmd |
390 call assert_equal(10, winheight(1))
391 call assert_equal(20, winwidth(2))
392
393 " equalizing horizontally doesn't change the heights
394 hor wincmd =
395 call assert_equal(10, winheight(1))
396 let [ww2, ww3] = [winwidth(2), winwidth(3)]
397 call assert_inrange(ww2 - 1, ww2 + 1, ww3)
398
399 2wincmd w
400 20wincmd |
401 call assert_equal(20, winwidth(2))
402 " equalizing vertically doesn't change the widths
403 vert wincmd =
404 call assert_equal(20, winwidth(2))
405 let [wh1, wh2] = [winheight(1), winheight(2)]
406 call assert_inrange(wh1 - 1, wh1 + 1, wh2)
407
408 bwipe Xone Xtwo Xthree
409endfunc
410
Bram Moolenaar4520d442017-03-19 16:09:46 +0100411func Test_window_width()
412 e Xa
413 vsplit Xb
414
415 let [ww1, ww2] = [winwidth(1), winwidth(2)]
416 " Active window (1) should have the same width or 1 more
417 " than the other window.
418 call assert_inrange(ww2, ww2 + 1, ww1)
419
420 wincmd <
421 call assert_equal(ww1 - 1, winwidth(1))
422 call assert_equal(ww2 + 1, winwidth(2))
423
424 wincmd >
425 call assert_equal(ww1, winwidth(1))
426 call assert_equal(ww2, winwidth(2))
427
428 2wincmd |
429 call assert_equal(2, winwidth(1))
430 call assert_equal(ww1 + ww2 - 2, winwidth(2))
431
432 wincmd =
433 call assert_equal(ww1, winwidth(1))
434 call assert_equal(ww2, winwidth(2))
435
436 2wincmd |
437 set winfixwidth
438 vsplit Xc
439 let [ww1, ww2, ww3] = [winwidth(1), winwidth(2), winwidth(3)]
Bram Moolenaar38e34832017-03-19 20:22:36 +0100440 call assert_equal(2, winwidth(2))
Bram Moolenaar4520d442017-03-19 16:09:46 +0100441 call assert_inrange(ww3, ww3 + 1, ww1)
442 3wincmd >
Bram Moolenaar38e34832017-03-19 20:22:36 +0100443 call assert_equal(2, winwidth(2))
Bram Moolenaar4520d442017-03-19 16:09:46 +0100444 call assert_equal(ww1 + 3, winwidth(1))
445 call assert_equal(ww3 - 3, winwidth(3))
446 wincmd =
Bram Moolenaar38e34832017-03-19 20:22:36 +0100447 call assert_equal(2, winwidth(2))
Bram Moolenaar4520d442017-03-19 16:09:46 +0100448 call assert_equal(ww1, winwidth(1))
449 call assert_equal(ww3, winwidth(3))
450
451 wincmd l
452 set winfixwidth&
453
454 wincmd =
455 let [ww1, ww2, ww3] = [winwidth(1), winwidth(2), winwidth(3)]
456 " Current window (2) should have the same width or 1 more
457 " than the other windows.
458 call assert_inrange(ww1, ww1 + 1, ww2)
459 call assert_inrange(ww3, ww3 + 1, ww2)
460
Yegappan Lakshmanan59585492021-06-12 13:46:41 +0200461 " when the current window width is less than the new 'winwidth', the current
462 " window width should be increased.
463 enew | only
464 split
465 10vnew
466 set winwidth=15
467 call assert_equal(15, winwidth(0))
468
469 %bw!
Bram Moolenaar4520d442017-03-19 16:09:46 +0100470endfunc
471
Bram Moolenaar8eeeba82017-06-25 22:45:39 +0200472func Test_equalalways_on_close()
473 set equalalways
474 vsplit
475 windo split
Bram Moolenaar1bbb6192018-11-10 16:02:01 +0100476 split
Bram Moolenaar8eeeba82017-06-25 22:45:39 +0200477 wincmd J
478 " now we have a frame top-left with two windows, a frame top-right with two
479 " windows and a frame at the bottom, full-width.
480 let height_1 = winheight(1)
481 let height_2 = winheight(2)
482 let height_3 = winheight(3)
483 let height_4 = winheight(4)
484 " closing the bottom window causes all windows to be resized.
485 close
486 call assert_notequal(height_1, winheight(1))
487 call assert_notequal(height_2, winheight(2))
488 call assert_notequal(height_3, winheight(3))
489 call assert_notequal(height_4, winheight(4))
490 call assert_equal(winheight(1), winheight(3))
491 call assert_equal(winheight(2), winheight(4))
492
493 1wincmd w
494 split
495 4wincmd w
496 resize + 5
497 " left column has three windows, equalized heights.
498 " right column has two windows, top one a bit higher
499 let height_1 = winheight(1)
500 let height_2 = winheight(2)
501 let height_4 = winheight(4)
502 let height_5 = winheight(5)
503 3wincmd w
504 " closing window in left column equalizes heights in left column but not in
505 " the right column
506 close
507 call assert_notequal(height_1, winheight(1))
508 call assert_notequal(height_2, winheight(2))
509 call assert_equal(height_4, winheight(3))
510 call assert_equal(height_5, winheight(4))
511
512 only
513 set equalalways&
514endfunc
515
Bram Moolenaar22044dc2017-12-02 15:43:37 +0100516func Test_win_screenpos()
Bram Moolenaar5a4c3082019-12-01 15:23:11 +0100517 CheckFeature quickfix
518
Bram Moolenaar22044dc2017-12-02 15:43:37 +0100519 call assert_equal(1, winnr('$'))
520 split
521 vsplit
522 10wincmd _
523 30wincmd |
524 call assert_equal([1, 1], win_screenpos(1))
525 call assert_equal([1, 32], win_screenpos(2))
526 call assert_equal([12, 1], win_screenpos(3))
527 call assert_equal([0, 0], win_screenpos(4))
Bram Moolenaar99fa7212020-04-26 15:59:55 +0200528 call assert_fails('let l = win_screenpos([])', 'E745:')
Bram Moolenaar22044dc2017-12-02 15:43:37 +0100529 only
530endfunc
531
Bram Moolenaar4520d442017-03-19 16:09:46 +0100532func Test_window_jump_tag()
Bram Moolenaar5a4c3082019-12-01 15:23:11 +0100533 CheckFeature quickfix
534
Bram Moolenaar4520d442017-03-19 16:09:46 +0100535 help
536 /iccf
537 call assert_match('^|iccf|', getline('.'))
538 call assert_equal(2, winnr('$'))
539 2wincmd }
540 call assert_equal(3, winnr('$'))
541 call assert_match('^|iccf|', getline('.'))
542 wincmd k
543 call assert_match('\*iccf\*', getline('.'))
544 call assert_equal(2, winheight(0))
545
546 wincmd z
547 set previewheight=4
548 help
549 /bugs
550 wincmd }
551 wincmd k
552 call assert_match('\*bugs\*', getline('.'))
553 call assert_equal(4, winheight(0))
554 set previewheight&
555
556 %bw!
557endfunc
558
559func Test_window_newtab()
560 e Xa
561
562 call assert_equal(1, tabpagenr('$'))
563 call assert_equal("\nAlready only one window", execute('wincmd T'))
564
565 split Xb
566 split Xc
567
568 wincmd T
569 call assert_equal(2, tabpagenr('$'))
570 call assert_equal(['Xb', 'Xa'], map(tabpagebuflist(1), 'bufname(v:val)'))
Bram Moolenaarce90e362019-09-08 18:58:44 +0200571 call assert_equal(['Xc' ], map(2->tabpagebuflist(), 'bufname(v:val)'))
Bram Moolenaar0e05de42020-03-25 22:23:46 +0100572 call assert_equal(['Xc' ], map(tabpagebuflist(), 'bufname(v:val)'))
Bram Moolenaar4520d442017-03-19 16:09:46 +0100573
574 %bw!
575endfunc
576
Bram Moolenaarf79225e2017-03-18 23:11:04 +0100577func Test_next_split_all()
578 " This was causing an illegal memory access.
579 n x
580 norm axxx
581 split
582 split
583 s/x
584 s/x
585 all
586 bwipe!
587endfunc
588
Bram Moolenaar75373f32017-08-07 22:02:30 +0200589" Tests for adjusting window and contents
590func GetScreenStr(row)
591 let str = ""
592 for c in range(1,3)
593 let str .= nr2char(screenchar(a:row, c))
594 endfor
595 return str
596endfunc
597
598func Test_window_contents()
599 enew! | only | new
600 call setline(1, range(1,256))
601
602 exe "norm! \<C-W>t\<C-W>=1Gzt\<C-W>w\<C-W>+"
603 redraw
Bram Moolenaar72cf47a2018-05-10 18:23:29 +0200604 let s3 = GetScreenStr(1)
Bram Moolenaar75373f32017-08-07 22:02:30 +0200605 wincmd p
606 call assert_equal(1, line("w0"))
607 call assert_equal('1 ', s3)
608
609 exe "norm! \<C-W>t\<C-W>=50Gzt\<C-W>w\<C-W>+"
610 redraw
Bram Moolenaar72cf47a2018-05-10 18:23:29 +0200611 let s3 = GetScreenStr(1)
Bram Moolenaar75373f32017-08-07 22:02:30 +0200612 wincmd p
613 call assert_equal(50, line("w0"))
614 call assert_equal('50 ', s3)
615
616 exe "norm! \<C-W>t\<C-W>=59Gzt\<C-W>w\<C-W>+"
617 redraw
Bram Moolenaar72cf47a2018-05-10 18:23:29 +0200618 let s3 = GetScreenStr(1)
Bram Moolenaar75373f32017-08-07 22:02:30 +0200619 wincmd p
620 call assert_equal(59, line("w0"))
621 call assert_equal('59 ', s3)
622
Bram Moolenaar8b633132020-03-20 18:20:51 +0100623 %d
624 call setline(1, ['one', 'two', 'three'])
625 call assert_equal(1, line('w0'))
626 call assert_equal(3, line('w$'))
627
Bram Moolenaar75373f32017-08-07 22:02:30 +0200628 bwipeout!
629 call test_garbagecollect_now()
630endfunc
631
Bram Moolenaar2efb3232017-12-19 12:27:23 +0100632func Test_window_colon_command()
633 " This was reading invalid memory.
634 exe "norm! v\<C-W>:\<C-U>echo v:version"
635endfunc
636
Bram Moolenaar6f361c92018-01-31 19:06:50 +0100637func Test_access_freed_mem()
Bram Moolenaar1417c762019-07-27 17:31:36 +0200638 call assert_equal(&columns, winwidth(0))
Bram Moolenaar9a046fd2021-01-28 13:47:59 +0100639 " This was accessing freed memory (but with what events?)
640 au BufEnter,BufLeave,WinEnter,WinLeave 0 vs xxx
Bram Moolenaar6f361c92018-01-31 19:06:50 +0100641 arg 0
642 argadd
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +0200643 call assert_fails("all", "E242:")
Bram Moolenaar6f361c92018-01-31 19:06:50 +0100644 au!
645 bwipe xxx
Bram Moolenaar1417c762019-07-27 17:31:36 +0200646 call assert_equal(&columns, winwidth(0))
Bram Moolenaar6f361c92018-01-31 19:06:50 +0100647endfunc
648
Bram Moolenaara27e1dc2019-10-07 22:27:36 +0200649func Test_insert_cleared_on_switch_to_term()
650 CheckFeature terminal
651
652 set showmode
653 terminal
654 wincmd p
655
656 call feedkeys("i\<C-O>", 'ntx')
657 redraw
658
659 " The "-- (insert) --" indicator should be visible.
660 let chars = map(range(1, &columns), 'nr2char(screenchar(&lines, v:val))')
661 let str = trim(join(chars, ''))
662 call assert_equal('-- (insert) --', str)
663
664 call feedkeys("\<C-W>p", 'ntx')
665 redraw
666
667 " The "-- (insert) --" indicator should have been cleared.
668 let chars = map(range(1, &columns), 'nr2char(screenchar(&lines, v:val))')
669 let str = trim(join(chars, ''))
670 call assert_equal('', str)
671
672 set showmode&
673 %bw!
674endfunc
675
Bram Moolenaar5bab5552018-04-13 20:41:29 +0200676func Test_visual_cleared_after_window_split()
677 new | only!
678 let smd_save = &showmode
679 set showmode
680 let ls_save = &laststatus
681 set laststatus=1
682 call setline(1, ['a', 'b', 'c', 'd', ''])
683 norm! G
684 exe "norm! kkvk"
685 redraw
686 exe "norm! \<C-W>v"
687 redraw
688 " check if '-- VISUAL --' disappeared from command line
689 let columns = range(1, &columns)
690 let cmdlinechars = map(columns, 'nr2char(screenchar(&lines, v:val))')
691 let cmdline = join(cmdlinechars, '')
692 let cmdline_ltrim = substitute(cmdline, '^\s*', "", "")
693 let mode_shown = substitute(cmdline_ltrim, '\s*$', "", "")
694 call assert_equal('', mode_shown)
695 let &showmode = smd_save
696 let &laststatus = ls_save
697 bwipe!
698endfunc
699
Bram Moolenaar72cf47a2018-05-10 18:23:29 +0200700func Test_winrestcmd()
701 2split
702 3vsplit
Bram Moolenaara0c8aea2021-03-20 19:55:35 +0100703 let restcmd = winrestcmd()
Bram Moolenaar72cf47a2018-05-10 18:23:29 +0200704 call assert_equal(2, winheight(0))
705 call assert_equal(3, winwidth(0))
706 wincmd =
707 call assert_notequal(2, winheight(0))
708 call assert_notequal(3, winwidth(0))
Bram Moolenaara0c8aea2021-03-20 19:55:35 +0100709 exe restcmd
Bram Moolenaar72cf47a2018-05-10 18:23:29 +0200710 call assert_equal(2, winheight(0))
711 call assert_equal(3, winwidth(0))
712 only
Bram Moolenaara0c8aea2021-03-20 19:55:35 +0100713
714 wincmd v
715 wincmd s
716 wincmd v
717 redraw
718 let restcmd = winrestcmd()
719 wincmd _
720 wincmd |
721 exe restcmd
722 redraw
723 call assert_equal(restcmd, winrestcmd())
724
725 only
Bram Moolenaar72cf47a2018-05-10 18:23:29 +0200726endfunc
727
Bram Moolenaar1e115362019-01-09 23:01:02 +0100728func Fun_RenewFile()
Bram Moolenaar026587b2019-08-17 15:08:00 +0200729 " Need to wait a bit for the timestamp to be older.
Bram Moolenaarce90e362019-09-08 18:58:44 +0200730 let old_ftime = getftime("tmp.txt")
731 while getftime("tmp.txt") == old_ftime
732 sleep 100m
733 silent execute '!echo "1" > tmp.txt'
734 endwhile
Bram Moolenaara42df592018-12-24 00:22:39 +0100735 sp
736 wincmd p
737 edit! tmp.txt
Bram Moolenaar1e115362019-01-09 23:01:02 +0100738endfunc
Bram Moolenaara42df592018-12-24 00:22:39 +0100739
740func Test_window_prevwin()
741 " Can we make this work on MS-Windows?
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +0200742 CheckUnix
Bram Moolenaara42df592018-12-24 00:22:39 +0100743
744 set hidden autoread
745 call writefile(['2'], 'tmp.txt')
746 new tmp.txt
747 q
Bram Moolenaara42df592018-12-24 00:22:39 +0100748 call Fun_RenewFile()
749 call assert_equal(2, winnr())
750 wincmd p
751 call assert_equal(1, winnr())
752 wincmd p
753 q
754 call Fun_RenewFile()
755 call assert_equal(2, winnr())
756 wincmd p
757 call assert_equal(1, winnr())
758 wincmd p
759 " reset
760 q
761 call delete('tmp.txt')
762 set hidden&vim autoread&vim
763 delfunc Fun_RenewFile
764endfunc
765
Bram Moolenaar8fcb60f2019-03-04 13:18:30 +0100766func Test_relative_cursor_position_in_one_line_window()
767 new
768 only
769 call setline(1, range(1, 10000))
770 normal 50%
771 let lnum = getcurpos()[1]
772 split
773 split
774 " make third window take as many lines as possible, other windows will
775 " become one line
776 3wincmd w
777 for i in range(1, &lines - 6)
778 wincmd +
779 redraw!
780 endfor
781
782 " first and second window should show cursor line
783 let wininfo = getwininfo()
784 call assert_equal(lnum, wininfo[0].topline)
785 call assert_equal(lnum, wininfo[1].topline)
786
787 only!
788 bwipe!
Yegappan Lakshmanan04c4c572022-08-30 19:48:24 +0100789 call assert_fails('call winrestview(test_null_dict())', 'E1297:')
Bram Moolenaar8fcb60f2019-03-04 13:18:30 +0100790endfunc
791
792func Test_relative_cursor_position_after_move_and_resize()
793 let so_save = &so
794 set so=0
795 enew
796 call setline(1, range(1, 10000))
797 normal 50%
798 split
799 1wincmd w
800 " Move cursor to first line in window
801 normal H
802 redraw!
803 " Reduce window height to two lines
804 let height = winheight(0)
805 while winheight(0) > 2
806 wincmd -
807 redraw!
808 endwhile
809 " move cursor to second/last line in window
810 normal j
811 " restore previous height
812 while winheight(0) < height
813 wincmd +
814 redraw!
815 endwhile
816 " make window two lines again
817 while winheight(0) > 2
818 wincmd -
819 redraw!
820 endwhile
821
822 " cursor should be at bottom line
823 let info = getwininfo(win_getid())[0]
824 call assert_equal(info.topline + 1, getcurpos()[1])
825
826 only!
827 bwipe!
828 let &so = so_save
829endfunc
830
831func Test_relative_cursor_position_after_resize()
832 let so_save = &so
833 set so=0
834 enew
835 call setline(1, range(1, 10000))
836 normal 50%
837 split
838 1wincmd w
839 let winid1 = win_getid()
840 let info = getwininfo(winid1)[0]
841 " Move cursor to second line in window
842 exe "normal " . (info.topline + 1) . "G"
843 redraw!
844 let lnum = getcurpos()[1]
845
846 " Make the window only two lines high, cursor should end up in top line
847 2wincmd w
848 exe (info.height - 2) . "wincmd +"
849 redraw!
850 let info = getwininfo(winid1)[0]
851 call assert_equal(lnum, info.topline)
852
853 only!
854 bwipe!
855 let &so = so_save
856endfunc
857
858func Test_relative_cursor_second_line_after_resize()
859 let so_save = &so
860 set so=0
861 enew
862 call setline(1, range(1, 10000))
863 normal 50%
864 split
865 1wincmd w
866 let winid1 = win_getid()
867 let info = getwininfo(winid1)[0]
868
869 " Make the window only two lines high
870 2wincmd _
871
872 " Move cursor to second line in window
873 normal H
874 normal j
875
876 " Make window size bigger, then back to 2 lines
877 for i in range(1, 10)
878 wincmd +
879 redraw!
880 endfor
881 for i in range(1, 10)
882 wincmd -
883 redraw!
884 endfor
885
886 " cursor should end up in bottom line
887 let info = getwininfo(winid1)[0]
888 call assert_equal(info.topline + 1, getcurpos()[1])
889
890 only!
891 bwipe!
892 let &so = so_save
893endfunc
894
Bram Moolenaara9b25352019-05-12 14:25:30 +0200895func Test_split_noscroll()
896 let so_save = &so
Bram Moolenaarbd2d68c2019-05-18 15:36:11 +0200897 enew
898 call setline(1, range(1, 8))
Bram Moolenaara9b25352019-05-12 14:25:30 +0200899 normal 100%
900 split
901
902 1wincmd w
903 let winid1 = win_getid()
904 let info1 = getwininfo(winid1)[0]
905
906 2wincmd w
907 let winid2 = win_getid()
908 let info2 = getwininfo(winid2)[0]
909
910 call assert_equal(1, info1.topline)
911 call assert_equal(1, info2.topline)
912
Bram Moolenaarbd2d68c2019-05-18 15:36:11 +0200913 " window that fits all lines by itself, but not when split: closing other
914 " window should restore fraction.
Bram Moolenaara9b25352019-05-12 14:25:30 +0200915 only!
Bram Moolenaarbd2d68c2019-05-18 15:36:11 +0200916 call setline(1, range(1, &lines - 10))
917 exe &lines / 4
918 let winid1 = win_getid()
919 let info1 = getwininfo(winid1)[0]
920 call assert_equal(1, info1.topline)
921 new
922 redraw
923 close
924 let info1 = getwininfo(winid1)[0]
925 call assert_equal(1, info1.topline)
926
Bram Moolenaara9b25352019-05-12 14:25:30 +0200927 bwipe!
928 let &so = so_save
929endfunc
930
Bram Moolenaar46ad2882019-04-08 20:01:47 +0200931" Tests for the winnr() function
932func Test_winnr()
933 only | tabonly
934 call assert_equal(1, winnr('j'))
935 call assert_equal(1, winnr('k'))
936 call assert_equal(1, winnr('h'))
937 call assert_equal(1, winnr('l'))
938
939 " create a set of horizontally and vertically split windows
940 leftabove new | wincmd p
941 leftabove new | wincmd p
942 rightbelow new | wincmd p
943 rightbelow new | wincmd p
944 leftabove vnew | wincmd p
945 leftabove vnew | wincmd p
946 rightbelow vnew | wincmd p
947 rightbelow vnew | wincmd p
948
949 call assert_equal(8, winnr('j'))
950 call assert_equal(2, winnr('k'))
951 call assert_equal(4, winnr('h'))
952 call assert_equal(6, winnr('l'))
953 call assert_equal(9, winnr('2j'))
954 call assert_equal(1, winnr('2k'))
955 call assert_equal(3, winnr('2h'))
956 call assert_equal(7, winnr('2l'))
957
958 " Error cases
959 call assert_fails("echo winnr('0.2k')", 'E15:')
960 call assert_equal(2, winnr('-2k'))
961 call assert_fails("echo winnr('-2xj')", 'E15:')
962 call assert_fails("echo winnr('j2j')", 'E15:')
963 call assert_fails("echo winnr('ll')", 'E15:')
964 call assert_fails("echo winnr('5')", 'E15:')
965 call assert_equal(4, winnr('0h'))
Bram Moolenaar99fa7212020-04-26 15:59:55 +0200966 call assert_fails("let w = winnr([])", 'E730:')
967 call assert_equal('unknown', win_gettype(-1))
968 call assert_equal(-1, winheight(-1))
969 call assert_equal(-1, winwidth(-1))
Bram Moolenaar46ad2882019-04-08 20:01:47 +0200970
971 tabnew
972 call assert_equal(8, tabpagewinnr(1, 'j'))
Bram Moolenaarce90e362019-09-08 18:58:44 +0200973 call assert_equal(2, 1->tabpagewinnr('k'))
Bram Moolenaar46ad2882019-04-08 20:01:47 +0200974 call assert_equal(4, tabpagewinnr(1, 'h'))
975 call assert_equal(6, tabpagewinnr(1, 'l'))
976
977 only | tabonly
978endfunc
979
Bram Moolenaarf92e58c2019-09-08 21:51:41 +0200980func Test_winrestview()
981 split runtest.vim
982 normal 50%
983 let view = winsaveview()
984 close
985 split runtest.vim
986 eval view->winrestview()
987 call assert_equal(view, winsaveview())
988
989 bwipe!
Yegappan Lakshmanan04c4c572022-08-30 19:48:24 +0100990 call assert_fails('call winrestview(test_null_dict())', 'E1297:')
Bram Moolenaarf92e58c2019-09-08 21:51:41 +0200991endfunc
992
Bram Moolenaard20dcb32019-09-10 21:22:58 +0200993func Test_win_splitmove()
Bram Moolenaar5a4c3082019-12-01 15:23:11 +0100994 CheckFeature quickfix
995
Bram Moolenaard20dcb32019-09-10 21:22:58 +0200996 edit a
997 leftabove split b
998 leftabove vsplit c
999 leftabove split d
1000 call assert_equal(0, win_splitmove(winnr(), winnr('l')))
1001 call assert_equal(bufname(winbufnr(1)), 'c')
1002 call assert_equal(bufname(winbufnr(2)), 'd')
1003 call assert_equal(bufname(winbufnr(3)), 'b')
1004 call assert_equal(bufname(winbufnr(4)), 'a')
1005 call assert_equal(0, win_splitmove(winnr(), winnr('j'), {'vertical': 1}))
1006 call assert_equal(0, win_splitmove(winnr(), winnr('j'), {'vertical': 1}))
1007 call assert_equal(bufname(winbufnr(1)), 'c')
1008 call assert_equal(bufname(winbufnr(2)), 'b')
1009 call assert_equal(bufname(winbufnr(3)), 'd')
1010 call assert_equal(bufname(winbufnr(4)), 'a')
1011 call assert_equal(0, win_splitmove(winnr(), winnr('k'), {'vertical': 1}))
1012 call assert_equal(bufname(winbufnr(1)), 'd')
1013 call assert_equal(bufname(winbufnr(2)), 'c')
1014 call assert_equal(bufname(winbufnr(3)), 'b')
1015 call assert_equal(bufname(winbufnr(4)), 'a')
1016 call assert_equal(0, win_splitmove(winnr(), winnr('j'), {'rightbelow': v:true}))
1017 call assert_equal(bufname(winbufnr(1)), 'c')
1018 call assert_equal(bufname(winbufnr(2)), 'b')
1019 call assert_equal(bufname(winbufnr(3)), 'a')
1020 call assert_equal(bufname(winbufnr(4)), 'd')
Yegappan Lakshmanan04c4c572022-08-30 19:48:24 +01001021 call assert_fails('call win_splitmove(winnr(), winnr("k"), test_null_dict())', 'E1297:')
Bram Moolenaard20dcb32019-09-10 21:22:58 +02001022 only | bd
1023
1024 call assert_fails('call win_splitmove(winnr(), 123)', 'E957:')
1025 call assert_fails('call win_splitmove(123, winnr())', 'E957:')
1026 call assert_fails('call win_splitmove(winnr(), winnr())', 'E957:')
Bram Moolenaar7b94e772020-01-06 21:03:24 +01001027
1028 tabnew
1029 call assert_fails('call win_splitmove(1, win_getid(1, 1))', 'E957:')
1030 tabclose
Bram Moolenaard20dcb32019-09-10 21:22:58 +02001031endfunc
1032
Bram Moolenaare20b9ec2020-02-03 21:40:04 +01001033" Test for the :only command
1034func Test_window_only()
1035 new
1036 set modified
1037 new
1038 call assert_fails('only', 'E445:')
1039 only!
Bram Moolenaarbc2b71d2020-02-17 21:33:30 +01001040 " Test for :only with a count
1041 let wid = win_getid()
1042 new
1043 new
1044 3only
1045 call assert_equal(1, winnr('$'))
1046 call assert_equal(wid, win_getid())
1047 call assert_fails('close', 'E444:')
1048 call assert_fails('%close', 'E16:')
Bram Moolenaare20b9ec2020-02-03 21:40:04 +01001049endfunc
1050
Bram Moolenaar9f6277b2020-02-11 22:04:02 +01001051" Test for errors with :wincmd
1052func Test_wincmd_errors()
1053 call assert_fails('wincmd g', 'E474:')
1054 call assert_fails('wincmd ab', 'E474:')
1055endfunc
1056
1057" Test for errors with :winpos
1058func Test_winpos_errors()
1059 if !has("gui_running") && !has('win32')
1060 call assert_fails('winpos', 'E188:')
1061 endif
1062 call assert_fails('winpos 10', 'E466:')
1063endfunc
1064
Bram Moolenaar406cd902020-02-18 21:54:41 +01001065" Test for +cmd in a :split command
1066func Test_split_cmd()
1067 split +set\ readonly
1068 call assert_equal(1, &readonly)
1069 call assert_equal(2, winnr('$'))
1070 close
1071endfunc
1072
Bram Moolenaar5080b0a2020-03-22 21:23:47 +01001073" Create maximum number of horizontally or vertically split windows and then
1074" run commands that create a new horizontally/vertically split window
1075func Run_noroom_for_newwindow_test(dir_arg)
1076 let dir = (a:dir_arg == 'v') ? 'vert ' : ''
1077
1078 " Open as many windows as possible
Bram Moolenaarab505b12020-03-23 19:28:44 +01001079 while v:true
Bram Moolenaar5080b0a2020-03-22 21:23:47 +01001080 try
1081 exe dir . 'new'
1082 catch /E36:/
1083 break
1084 endtry
Bram Moolenaarab505b12020-03-23 19:28:44 +01001085 endwhile
Bram Moolenaar5080b0a2020-03-22 21:23:47 +01001086
Bram Moolenaarb18b4962022-09-02 21:55:50 +01001087 call writefile(['first', 'second', 'third'], 'Xnorfile1')
1088 call writefile([], 'Xnorfile2')
1089 call writefile([], 'Xnorfile3')
Bram Moolenaar5080b0a2020-03-22 21:23:47 +01001090
1091 " Argument list related commands
Bram Moolenaarb18b4962022-09-02 21:55:50 +01001092 args Xnorfile1 Xnorfile2 Xnorfile3
Bram Moolenaar5080b0a2020-03-22 21:23:47 +01001093 next
1094 for cmd in ['sargument 2', 'snext', 'sprevious', 'sNext', 'srewind',
1095 \ 'sfirst', 'slast']
1096 call assert_fails(dir .. cmd, 'E36:')
1097 endfor
1098 %argdelete
1099
1100 " Buffer related commands
1101 set modified
1102 hide enew
Bram Moolenaarb18b4962022-09-02 21:55:50 +01001103 for cmd in ['sbuffer Xnorfile1', 'sbnext', 'sbprevious', 'sbNext', 'sbrewind',
Bram Moolenaar5080b0a2020-03-22 21:23:47 +01001104 \ 'sbfirst', 'sblast', 'sball', 'sbmodified', 'sunhide']
1105 call assert_fails(dir .. cmd, 'E36:')
1106 endfor
1107
1108 " Window related commands
Bram Moolenaarb18b4962022-09-02 21:55:50 +01001109 for cmd in ['split', 'split Xnorfile2', 'new', 'new Xnorfile3', 'sview Xnorfile1',
Bram Moolenaar5080b0a2020-03-22 21:23:47 +01001110 \ 'sfind runtest.vim']
1111 call assert_fails(dir .. cmd, 'E36:')
1112 endfor
1113
1114 " Help
1115 call assert_fails(dir .. 'help', 'E36:')
1116 call assert_fails(dir .. 'helpgrep window', 'E36:')
1117
1118 " Command-line window
1119 if a:dir_arg == 'h'
1120 " Cmd-line window is always a horizontally split window
1121 call assert_beeps('call feedkeys("q:\<CR>", "xt")')
1122 endif
1123
1124 " Quickfix and location list window
1125 if has('quickfix')
1126 cexpr ''
1127 call assert_fails(dir .. 'copen', 'E36:')
1128 lexpr ''
1129 call assert_fails(dir .. 'lopen', 'E36:')
1130
1131 " Preview window
Bram Moolenaarb18b4962022-09-02 21:55:50 +01001132 call assert_fails(dir .. 'pedit Xnorfile2', 'E36:')
Bram Moolenaar5080b0a2020-03-22 21:23:47 +01001133 call setline(1, 'abc')
1134 call assert_fails(dir .. 'psearch abc', 'E36:')
1135 endif
1136
1137 " Window commands (CTRL-W ^ and CTRL-W f)
1138 if a:dir_arg == 'h'
1139 call assert_fails('call feedkeys("\<C-W>^", "xt")', 'E36:')
Bram Moolenaarb18b4962022-09-02 21:55:50 +01001140 call setline(1, 'Xnorfile1')
Bram Moolenaar5080b0a2020-03-22 21:23:47 +01001141 call assert_fails('call feedkeys("gg\<C-W>f", "xt")', 'E36:')
1142 endif
1143 enew!
1144
1145 " Tag commands (:stag, :stselect and :stjump)
1146 call writefile(["!_TAG_FILE_ENCODING\tutf-8\t//",
Bram Moolenaarb18b4962022-09-02 21:55:50 +01001147 \ "second\tXnorfile1\t2",
1148 \ "third\tXnorfile1\t3",],
Bram Moolenaar5080b0a2020-03-22 21:23:47 +01001149 \ 'Xtags')
1150 set tags=Xtags
1151 call assert_fails(dir .. 'stag second', 'E36:')
1152 call assert_fails('call feedkeys(":" .. dir .. "stselect second\n1\n", "xt")', 'E36:')
1153 call assert_fails(dir .. 'stjump second', 'E36:')
1154 call assert_fails(dir .. 'ptag second', 'E36:')
1155 set tags&
1156 call delete('Xtags')
1157
1158 " :isplit and :dsplit
1159 call setline(1, ['#define FOO 1', 'FOO'])
1160 normal 2G
1161 call assert_fails(dir .. 'isplit FOO', 'E36:')
1162 call assert_fails(dir .. 'dsplit FOO', 'E36:')
1163
1164 " terminal
1165 if has('terminal')
1166 call assert_fails(dir .. 'terminal', 'E36:')
1167 endif
1168
1169 %bwipe!
Bram Moolenaarb18b4962022-09-02 21:55:50 +01001170 call delete('Xnorfile1')
1171 call delete('Xnorfile2')
1172 call delete('Xnorfile3')
Bram Moolenaar5080b0a2020-03-22 21:23:47 +01001173 only
1174endfunc
1175
1176func Test_split_cmds_with_no_room()
1177 call Run_noroom_for_newwindow_test('h')
1178 call Run_noroom_for_newwindow_test('v')
1179endfunc
1180
Bram Moolenaarad48e6c2020-04-21 22:19:45 +02001181" Test for various wincmd failures
1182func Test_wincmd_fails()
1183 only!
1184 call assert_beeps("normal \<C-W>w")
1185 call assert_beeps("normal \<C-W>p")
1186 call assert_beeps("normal \<C-W>gk")
1187 call assert_beeps("normal \<C-W>r")
1188 call assert_beeps("normal \<C-W>K")
1189 call assert_beeps("normal \<C-W>H")
1190 call assert_beeps("normal \<C-W>2gt")
1191endfunc
1192
Bram Moolenaarfe6dce82020-09-04 14:41:21 +02001193func Test_window_resize()
1194 " Vertical :resize (absolute, relative, min and max size).
1195 vsplit
1196 vert resize 8
1197 call assert_equal(8, winwidth(0))
1198 vert resize +2
1199 call assert_equal(10, winwidth(0))
1200 vert resize -2
1201 call assert_equal(8, winwidth(0))
1202 vert resize
1203 call assert_equal(&columns - 2, winwidth(0))
1204 vert resize 0
1205 call assert_equal(1, winwidth(0))
1206 vert resize 99999
1207 call assert_equal(&columns - 2, winwidth(0))
1208
1209 %bwipe!
1210
1211 " Horizontal :resize (with absolute, relative size, min and max size).
1212 split
1213 resize 8
1214 call assert_equal(8, winheight(0))
1215 resize +2
1216 call assert_equal(10, winheight(0))
1217 resize -2
1218 call assert_equal(8, winheight(0))
1219 resize
1220 call assert_equal(&lines - 4, winheight(0))
1221 resize 0
1222 call assert_equal(1, winheight(0))
1223 resize 99999
1224 call assert_equal(&lines - 4, winheight(0))
1225
1226 " :resize with explicit window number.
1227 let other_winnr = winnr('j')
1228 exe other_winnr .. 'resize 10'
1229 call assert_equal(10, winheight(other_winnr))
1230 call assert_equal(&lines - 10 - 3, winheight(0))
Bram Moolenaar9668cc52020-10-17 17:39:55 +02001231 exe other_winnr .. 'resize +1'
1232 exe other_winnr .. 'resize +1'
1233 call assert_equal(12, winheight(other_winnr))
1234 call assert_equal(&lines - 10 - 3 -2, winheight(0))
Bram Moolenaar89015a62020-12-29 12:46:51 +01001235 close
1236
1237 vsplit
1238 wincmd l
1239 let other_winnr = winnr('h')
1240 call assert_notequal(winnr(), other_winnr)
Bram Moolenaar5efe0e52021-01-01 14:31:34 +01001241 exe 'vert ' .. other_winnr .. 'resize -' .. &columns
Bram Moolenaar89015a62020-12-29 12:46:51 +01001242 call assert_equal(0, winwidth(other_winnr))
Bram Moolenaarfe6dce82020-09-04 14:41:21 +02001243
1244 %bwipe!
1245endfunc
1246
Bram Moolenaar5d3c9f82020-06-26 20:41:39 +02001247" Test for adjusting the window width when a window is closed with some
1248" windows using 'winfixwidth'
1249func Test_window_width_adjust()
1250 only
1251 " Three vertical windows. Windows 1 and 2 have 'winfixwidth' set and close
1252 " window 2.
1253 wincmd v
1254 vert resize 10
1255 set winfixwidth
1256 wincmd v
1257 set winfixwidth
1258 wincmd c
1259 call assert_inrange(10, 12, winwidth(1))
1260 " Three vertical windows. Windows 2 and 3 have 'winfixwidth' set and close
1261 " window 3.
1262 only
1263 set winfixwidth
1264 wincmd v
1265 vert resize 10
1266 set winfixwidth
1267 wincmd v
1268 set nowinfixwidth
1269 wincmd b
1270 wincmd c
1271 call assert_inrange(10, 12, winwidth(2))
1272
1273 new | only
1274endfunc
1275
1276" Test for jumping to a vertical/horizontal neighbor window based on the
1277" current cursor position
1278func Test_window_goto_neightbor()
1279 %bw!
1280
1281 " Vertical window movement
1282
1283 " create the following window layout:
1284 " +--+--+
1285 " |w1|w3|
1286 " +--+ |
1287 " |w2| |
1288 " +--+--+
1289 " |w4 |
1290 " +-----+
1291 new
1292 vsplit
1293 split
1294 " vertically jump from w4
1295 wincmd b
1296 call setline(1, repeat(' ', &columns))
1297 call cursor(1, 1)
1298 wincmd k
1299 call assert_equal(2, winnr())
1300 wincmd b
1301 call cursor(1, &columns)
1302 redraw!
1303 wincmd k
1304 call assert_equal(3, winnr())
1305 %bw!
1306
1307 " create the following window layout:
1308 " +--+--+--+
1309 " |w1|w2|w3|
1310 " +--+--+--+
1311 " |w4 |
1312 " +--------+
1313 new
1314 vsplit
1315 vsplit
1316 wincmd b
1317 call setline(1, repeat(' ', &columns))
1318 call cursor(1, 1)
1319 wincmd k
1320 call assert_equal(1, winnr())
1321 wincmd b
1322 call cursor(1, &columns / 2)
1323 redraw!
1324 wincmd k
1325 call assert_equal(2, winnr())
1326 wincmd b
1327 call cursor(1, &columns)
1328 redraw!
1329 wincmd k
1330 call assert_equal(3, winnr())
1331 %bw!
1332
1333 " Horizontal window movement
1334
1335 " create the following window layout:
1336 " +--+--+--+
1337 " |w1|w2|w4|
1338 " +--+--+ |
1339 " |w3 | |
1340 " +-----+--+
1341 vsplit
1342 split
1343 vsplit
1344 4wincmd l
1345 call setline(1, repeat([' '], &lines))
1346 call cursor(1, 1)
1347 redraw!
1348 wincmd h
1349 call assert_equal(2, winnr())
1350 4wincmd l
1351 call cursor(&lines, 1)
1352 redraw!
1353 wincmd h
1354 call assert_equal(3, winnr())
1355 %bw!
1356
1357 " create the following window layout:
1358 " +--+--+
1359 " |w1|w4|
1360 " +--+ +
1361 " |w2| |
1362 " +--+ +
1363 " |w3| |
1364 " +--+--+
1365 vsplit
1366 split
1367 split
1368 wincmd l
1369 call setline(1, repeat([' '], &lines))
1370 call cursor(1, 1)
1371 redraw!
1372 wincmd h
1373 call assert_equal(1, winnr())
1374 wincmd l
1375 call cursor(&lines / 2, 1)
1376 redraw!
1377 wincmd h
1378 call assert_equal(2, winnr())
1379 wincmd l
1380 call cursor(&lines, 1)
1381 redraw!
1382 wincmd h
1383 call assert_equal(3, winnr())
1384 %bw!
1385endfunc
1386
1387" Test for an autocmd closing the destination window when jumping from one
1388" window to another.
1389func Test_close_dest_window()
1390 split
Bram Moolenaarb18b4962022-09-02 21:55:50 +01001391 edit Xdstfile
Bram Moolenaar5d3c9f82020-06-26 20:41:39 +02001392
1393 " Test for BufLeave
1394 augroup T1
1395 au!
Bram Moolenaarb18b4962022-09-02 21:55:50 +01001396 au BufLeave Xdstfile $wincmd c
Bram Moolenaar5d3c9f82020-06-26 20:41:39 +02001397 augroup END
1398 wincmd b
1399 call assert_equal(1, winnr('$'))
Bram Moolenaarb18b4962022-09-02 21:55:50 +01001400 call assert_equal('Xdstfile', @%)
Bram Moolenaar5d3c9f82020-06-26 20:41:39 +02001401 augroup T1
1402 au!
1403 augroup END
1404
1405 " Test for WinLeave
1406 new
1407 wincmd p
1408 augroup T1
1409 au!
1410 au WinLeave * 1wincmd c
1411 augroup END
1412 wincmd t
1413 call assert_equal(1, winnr('$'))
Bram Moolenaarb18b4962022-09-02 21:55:50 +01001414 call assert_equal('Xdstfile', @%)
Bram Moolenaar5d3c9f82020-06-26 20:41:39 +02001415 augroup T1
1416 au!
1417 augroup END
1418 augroup! T1
1419 %bw!
1420endfunc
1421
Bram Moolenaard0fb9072021-12-09 11:57:22 +00001422func Test_window_minimal_size()
1423 set winminwidth=0 winminheight=0
1424
1425 " check size is fixed vertically
1426 new
1427 call win_execute(win_getid(2), 'wincmd _')
1428 call assert_equal(0, winheight(0))
1429 call feedkeys('0', 'tx')
1430 call assert_equal(1, winheight(0))
1431 bwipe!
1432
1433 " check size is fixed horizontally
1434 vert new
1435 call win_execute(win_getid(2), 'wincmd |')
1436 call assert_equal(0, winwidth(0))
1437 call feedkeys('0', 'tx')
1438 call assert_equal(1, winwidth(0))
1439 bwipe!
1440
1441 if has('timers')
1442 " check size is fixed in Insert mode
ichizokfa9a8e02021-12-12 16:42:09 +00001443 func s:CheckSize(timer) abort
1444 call win_execute(win_getid(2), 'wincmd _')
1445 call assert_equal(0, winheight(0))
1446 call feedkeys(" \<Esc>", 't!')
1447 endfunc
Bram Moolenaard0fb9072021-12-09 11:57:22 +00001448 new
ichizokfa9a8e02021-12-12 16:42:09 +00001449 call timer_start(100, function('s:CheckSize'))
Bram Moolenaard0fb9072021-12-09 11:57:22 +00001450 call feedkeys('a', 'tx!')
1451 call assert_equal(1, winheight(0))
1452 bwipe!
ichizokfa9a8e02021-12-12 16:42:09 +00001453 delfunc s:CheckSize
Bram Moolenaard0fb9072021-12-09 11:57:22 +00001454 endif
1455
1456 set winminwidth& winminheight&
1457endfunc
1458
Daniel Steinbergee630312022-01-10 13:36:34 +00001459func Test_win_move_separator()
1460 edit a
1461 leftabove vsplit b
1462 let w = winwidth(0)
1463 " check win_move_separator from left window on left window
1464 call assert_equal(1, winnr())
1465 for offset in range(5)
1466 call assert_true(win_move_separator(0, offset))
1467 call assert_equal(w + offset, winwidth(0))
1468 call assert_true(0->win_move_separator(-offset))
1469 call assert_equal(w, winwidth(0))
1470 endfor
1471 " check win_move_separator from right window on left window number
1472 wincmd l
1473 call assert_notequal(1, winnr())
1474 for offset in range(5)
1475 call assert_true(1->win_move_separator(offset))
1476 call assert_equal(w + offset, winwidth(1))
1477 call assert_true(win_move_separator(1, -offset))
1478 call assert_equal(w, winwidth(1))
1479 endfor
1480 " check win_move_separator from right window on left window ID
1481 let id = win_getid(1)
1482 for offset in range(5)
1483 call assert_true(win_move_separator(id, offset))
1484 call assert_equal(w + offset, winwidth(id))
1485 call assert_true(id->win_move_separator(-offset))
1486 call assert_equal(w, winwidth(id))
1487 endfor
zeertzjqa0c4e2f2022-01-29 10:59:53 +00001488 " check win_move_separator from right window on right window is no-op
1489 let w0 = winwidth(0)
1490 call assert_true(win_move_separator(0, 1))
1491 call assert_equal(w0, winwidth(0))
1492 call assert_true(win_move_separator(0, -1))
1493 call assert_equal(w0, winwidth(0))
Daniel Steinbergee630312022-01-10 13:36:34 +00001494 " check that win_move_separator doesn't error with offsets beyond moving
1495 " possibility
1496 call assert_true(win_move_separator(id, 5000))
1497 call assert_true(winwidth(id) > w)
1498 call assert_true(win_move_separator(id, -5000))
1499 call assert_true(winwidth(id) < w)
1500 " check that win_move_separator returns false for an invalid window
1501 wincmd =
1502 let w = winwidth(0)
1503 call assert_false(win_move_separator(-1, 1))
1504 call assert_equal(w, winwidth(0))
1505 " check that win_move_separator returns false for a popup window
1506 let id = popup_create(['hello', 'world'], {})
1507 let w = winwidth(id)
1508 call assert_false(win_move_separator(id, 1))
1509 call assert_equal(w, winwidth(id))
1510 call popup_close(id)
1511 %bwipe!
1512endfunc
1513
1514func Test_win_move_statusline()
1515 edit a
1516 leftabove split b
1517 let h = winheight(0)
1518 " check win_move_statusline from top window on top window
1519 call assert_equal(1, winnr())
1520 for offset in range(5)
1521 call assert_true(win_move_statusline(0, offset))
1522 call assert_equal(h + offset, winheight(0))
1523 call assert_true(0->win_move_statusline(-offset))
1524 call assert_equal(h, winheight(0))
1525 endfor
1526 " check win_move_statusline from bottom window on top window number
1527 wincmd j
1528 call assert_notequal(1, winnr())
1529 for offset in range(5)
1530 call assert_true(1->win_move_statusline(offset))
1531 call assert_equal(h + offset, winheight(1))
1532 call assert_true(win_move_statusline(1, -offset))
1533 call assert_equal(h, winheight(1))
1534 endfor
zeertzjqa0c4e2f2022-01-29 10:59:53 +00001535 " check win_move_statusline from bottom window on bottom window
1536 let h0 = winheight(0)
1537 for offset in range(5)
1538 call assert_true(0->win_move_statusline(-offset))
1539 call assert_equal(h0 - offset, winheight(0))
1540 call assert_equal(1 + offset, &cmdheight)
1541 call assert_true(win_move_statusline(0, offset))
1542 call assert_equal(h0, winheight(0))
1543 call assert_equal(1, &cmdheight)
1544 endfor
1545 call assert_true(win_move_statusline(0, 1))
Bram Moolenaara2a89732022-08-31 14:46:18 +01001546 call assert_equal(h0, winheight(0))
1547 call assert_equal(1, &cmdheight)
Daniel Steinbergee630312022-01-10 13:36:34 +00001548 " check win_move_statusline from bottom window on top window ID
1549 let id = win_getid(1)
1550 for offset in range(5)
1551 call assert_true(win_move_statusline(id, offset))
1552 call assert_equal(h + offset, winheight(id))
1553 call assert_true(id->win_move_statusline(-offset))
1554 call assert_equal(h, winheight(id))
1555 endfor
1556 " check that win_move_statusline doesn't error with offsets beyond moving
1557 " possibility
1558 call assert_true(win_move_statusline(id, 5000))
1559 call assert_true(winheight(id) > h)
1560 call assert_true(win_move_statusline(id, -5000))
1561 call assert_true(winheight(id) < h)
1562 " check that win_move_statusline returns false for an invalid window
1563 wincmd =
1564 let h = winheight(0)
1565 call assert_false(win_move_statusline(-1, 1))
1566 call assert_equal(h, winheight(0))
1567 " check that win_move_statusline returns false for a popup window
1568 let id = popup_create(['hello', 'world'], {})
1569 let h = winheight(id)
1570 call assert_false(win_move_statusline(id, 1))
1571 call assert_equal(h, winheight(id))
1572 call popup_close(id)
1573 %bwipe!
1574endfunc
1575
Yegappan Lakshmanan72bb47e2022-04-03 11:22:38 +01001576" Test for window allocation failure
1577func Test_window_alloc_failure()
1578 %bw!
1579
1580 " test for creating a new window above current window
1581 call test_alloc_fail(GetAllocId('newwin_wvars'), 0, 0)
1582 call assert_fails('above new', 'E342:')
1583 call assert_equal(1, winnr('$'))
1584
1585 " test for creating a new window below current window
1586 call test_alloc_fail(GetAllocId('newwin_wvars'), 0, 0)
1587 call assert_fails('below new', 'E342:')
1588 call assert_equal(1, winnr('$'))
1589
1590 " test for popup window creation failure
1591 call test_alloc_fail(GetAllocId('newwin_wvars'), 0, 0)
1592 call assert_fails('call popup_create("Hello", {})', 'E342:')
1593 call assert_equal([], popup_list())
1594
1595 call test_alloc_fail(GetAllocId('newwin_wvars'), 0, 0)
1596 call assert_fails('split', 'E342:')
1597 call assert_equal(1, winnr('$'))
1598
Bram Moolenaarb18b4962022-09-02 21:55:50 +01001599 edit Xwaffile1
1600 edit Xwaffile2
Yegappan Lakshmanan72bb47e2022-04-03 11:22:38 +01001601 call test_alloc_fail(GetAllocId('newwin_wvars'), 0, 0)
Bram Moolenaarb18b4962022-09-02 21:55:50 +01001602 call assert_fails('sb Xwaffile1', 'E342:')
Yegappan Lakshmanan72bb47e2022-04-03 11:22:38 +01001603 call assert_equal(1, winnr('$'))
Bram Moolenaarb18b4962022-09-02 21:55:50 +01001604 call assert_equal('Xwaffile2', @%)
Yegappan Lakshmanan72bb47e2022-04-03 11:22:38 +01001605 %bw!
1606
1607 " FIXME: The following test crashes Vim
1608 " test for new tabpage creation failure
1609 " call test_alloc_fail(GetAllocId('newwin_wvars'), 0, 0)
1610 " call assert_fails('tabnew', 'E342:')
1611 " call assert_equal(1, tabpagenr('$'))
1612 " call assert_equal(1, winnr('$'))
1613
1614 " This test messes up the internal Vim window/frame information. So the
1615 " Test_window_cmd_cmdwin_with_vsp() test fails after running this test.
1616 " Open a new tab and close everything else to fix this issue.
1617 tabnew
1618 tabonly
1619endfunc
Bram Moolenaard0fb9072021-12-09 11:57:22 +00001620
Luuk van Baalfd7e60a2022-09-07 14:42:49 +01001621func Test_win_equal_last_status()
1622 let save_lines = &lines
1623 set lines=20
1624 set splitbelow
1625 set laststatus=0
1626
1627 split | split | quit
1628 call assert_equal(winheight(1), winheight(2))
1629
1630 let &lines = save_lines
1631 set splitbelow&
1632 set laststatus&
1633endfunc
1634
Luuk van Baalfaf1d412022-09-19 16:45:29 +01001635" Ensure no scrolling happens with 'nosplitscroll' for a sequence of
1636" split operations for various options: with and without a winbar,
1637" tabline, for each possible value of 'laststatus', 'scrolloff',
Luuk van Baal29ab5242022-09-11 16:59:53 +01001638" 'equalalways', and regardless of the cursor position.
Luuk van Baalfaf1d412022-09-19 16:45:29 +01001639func Test_nosplitscroll_options()
Luuk van Baal29ab5242022-09-11 16:59:53 +01001640 set nowrap
1641 set nosplitscroll
Luuk van Baal594f9e02022-09-16 12:52:58 +01001642
1643 " disallow window resizing
1644 let save_WS = &t_WS
1645 set t_WS=
1646
Luuk van Baal29ab5242022-09-11 16:59:53 +01001647 let gui = has("gui_running")
Luuk van Baal594f9e02022-09-16 12:52:58 +01001648 inoremap <expr> c "<cmd>copen<bar>wincmd k<CR>"
1649 for run in range(0, 10)
1650 tabnew | tabonly! | redraw
1651 let tabline = (gui ? 0 : ((run % 5) ? 1 : 0))
1652 let winbar_sb = (run % 2) && (run % 3)
Luuk van Baalfaf1d412022-09-19 16:45:29 +01001653 execute 'set scrolloff=' . (!(run % 4) ? 0 : run)
Luuk van Baal594f9e02022-09-16 12:52:58 +01001654 execute 'set laststatus=' . (run % 3)
1655 execute 'set ' . ((run % 2) ? 'equalalways' : 'noequalalways')
1656 execute 'set ' . ((run % 3) ? 'splitbelow' : 'nosplitbelow')
1657 execute (run % 5) ? 'tabnew' : ''
1658 execute (run % 2) ? 'nnoremenu 1.10 WinBar.Test :echo' : ''
1659 let pos = !(run % 3) ? 'H' : ((run % 2) ? 'M' : 'L')
1660 call setline(1, range(1, 256))
1661 " No scroll for restore_snapshot
1662 norm G
1663 try
1664 copen | close | colder
1665 catch /E380/
1666 endtry
1667 call assert_equal(257 - winheight(0), line("w0"))
Luuk van Baal29ab5242022-09-11 16:59:53 +01001668
Luuk van Baal594f9e02022-09-16 12:52:58 +01001669 " No scroll for firstwin horizontal split
1670 execute 'norm gg' . pos
1671 split | redraw | wincmd k
1672 call assert_equal(1, line("w0"))
1673 call assert_equal(&scroll, winheight(0) / 2)
1674 wincmd j
1675 call assert_equal(win_screenpos(0)[0] - tabline - winbar_sb, line("w0"))
Luuk van Baal29ab5242022-09-11 16:59:53 +01001676
Luuk van Baal594f9e02022-09-16 12:52:58 +01001677 " No scroll when resizing windows
1678 wincmd k | resize +2
1679 call assert_equal(1, line("w0"))
1680 wincmd j
1681 call assert_equal(win_screenpos(0)[0] - tabline - winbar_sb, line("w0"))
Luuk van Baal29ab5242022-09-11 16:59:53 +01001682
Luuk van Baal594f9e02022-09-16 12:52:58 +01001683 " No scroll when dragging statusline
1684 call win_move_statusline(1, -3)
1685 call assert_equal(win_screenpos(0)[0] - tabline - winbar_sb, line("w0"))
1686 wincmd k
1687 call assert_equal(1, line("w0"))
Luuk van Baal29ab5242022-09-11 16:59:53 +01001688
Luuk van Baal594f9e02022-09-16 12:52:58 +01001689 " No scroll when changing shellsize
1690 set lines+=2
1691 call assert_equal(1, line("w0"))
1692 wincmd j
1693 call assert_equal(win_screenpos(0)[0] - tabline - winbar_sb, line("w0"))
1694 set lines-=2
1695 call assert_equal(win_screenpos(0)[0] - tabline - winbar_sb, line("w0"))
1696 wincmd k
1697 call assert_equal(1, line("w0"))
Luuk van Baal29ab5242022-09-11 16:59:53 +01001698
Luuk van Baal594f9e02022-09-16 12:52:58 +01001699 " No scroll when equalizing windows
1700 wincmd =
1701 call assert_equal(1, line("w0"))
1702 wincmd j
1703 call assert_equal(win_screenpos(0)[0] - tabline - winbar_sb, line("w0"))
1704 wincmd k
1705 call assert_equal(1, line("w0"))
Luuk van Baal29ab5242022-09-11 16:59:53 +01001706
Luuk van Baal594f9e02022-09-16 12:52:58 +01001707 " No scroll in windows split multiple times
1708 vsplit | split | 4wincmd w
1709 call assert_equal(win_screenpos(0)[0] - tabline - winbar_sb, line("w0"))
1710 1wincmd w | quit | wincmd l | split
1711 call assert_equal(win_screenpos(0)[0] - tabline - winbar_sb, line("w0"))
1712 wincmd j
1713 call assert_equal(win_screenpos(0)[0] - tabline - winbar_sb, line("w0"))
Luuk van Baal29ab5242022-09-11 16:59:53 +01001714
Luuk van Baal594f9e02022-09-16 12:52:58 +01001715 " No scroll in small window
1716 2wincmd w | only | 5split | wincmd k
1717 call assert_equal(1, line("w0"))
1718 wincmd j
1719 call assert_equal(win_screenpos(0)[0] - tabline - winbar_sb, line("w0"))
Luuk van Baal29ab5242022-09-11 16:59:53 +01001720
Luuk van Baal594f9e02022-09-16 12:52:58 +01001721 " No scroll for vertical split
1722 quit | vsplit | wincmd l
1723 call assert_equal(1, line("w0"))
1724 wincmd h
1725 call assert_equal(1, line("w0"))
Luuk van Baal29ab5242022-09-11 16:59:53 +01001726
Luuk van Baal594f9e02022-09-16 12:52:58 +01001727 " No scroll in windows split and quit multiple times
1728 quit | redraw | split | redraw | split | redraw | quit | redraw
1729 call assert_equal(win_screenpos(0)[0] - tabline - winbar_sb, line("w0"))
Luuk van Baal29ab5242022-09-11 16:59:53 +01001730
Luuk van Baal594f9e02022-09-16 12:52:58 +01001731 " No scroll for new buffer
1732 1wincmd w | only | copen | wincmd k
1733 call assert_equal(1, line("w0"))
1734 only
1735 call assert_equal(1, line("w0"))
1736 above copen | wincmd j
1737 call assert_equal(win_screenpos(0)[0] - tabline, line("w0"))
Luuk van Baal29ab5242022-09-11 16:59:53 +01001738
Luuk van Baal594f9e02022-09-16 12:52:58 +01001739 " No scroll when opening cmdwin, and no cursor move when closing cmdwin.
1740 only | norm ggL
1741 let curpos = getcurpos()
1742 norm q:
1743 call assert_equal(1, line("w0"))
1744 call assert_equal(curpos, getcurpos())
Luuk van Baal29ab5242022-09-11 16:59:53 +01001745
Luuk van Baal594f9e02022-09-16 12:52:58 +01001746 " Scroll when cursor becomes invalid in insert mode
1747 norm Lic
1748 call assert_equal(curpos, getcurpos())
Luuk van Baal29ab5242022-09-11 16:59:53 +01001749
Luuk van Baal594f9e02022-09-16 12:52:58 +01001750 " No scroll when topline not equal to 1
1751 only | execute "norm gg5\<C-e>" | split | wincmd k
1752 call assert_equal(6, line("w0"))
1753 wincmd j
1754 call assert_equal(5 + win_screenpos(0)[0] - tabline - winbar_sb, line("w0"))
Luuk van Baal29ab5242022-09-11 16:59:53 +01001755 endfor
1756
1757 tabnew | tabonly! | %bwipeout!
1758 iunmap c
1759 set wrap&
1760 set scrolloff&
1761 set splitbelow&
1762 set laststatus&
1763 set equalalways&
1764 set splitscroll&
Luuk van Baal594f9e02022-09-16 12:52:58 +01001765 let &t_WS = save_WS
Luuk van Baal29ab5242022-09-11 16:59:53 +01001766endfunc
Luuk van Baalfd7e60a2022-09-07 14:42:49 +01001767
mityu12167d82022-09-15 17:44:07 +01001768function Test_nosplitscroll_cmdwin_cursor_position()
1769 set nosplitscroll
1770 call setline(1, range(&lines))
1771
1772 " No scroll when cursor is at near bottom of window and cusor position
1773 " recompution (done by line('w0') in this test) happens while in cmdwin.
1774 normal! G
1775 let firstline = line('w0')
1776 autocmd CmdwinEnter * ++once autocmd WinEnter * ++once call line('w0')
1777 execute "normal! q:\<C-w>q"
1778 redraw!
1779 call assert_equal(firstline, line('w0'))
1780
1781 " User script can change cursor position successfully while in cmdwin and it
1782 " shouldn't be changed when closing cmdwin.
1783 execute "normal! Gq:\<Cmd>call win_execute(winnr('#')->win_getid(), 'call cursor(1, 1)')\<CR>\<C-w>q"
1784 call assert_equal(1, line('.'))
1785 call assert_equal(1, col('.'))
1786
1787 execute "normal! Gq:\<Cmd>autocmd WinEnter * ++once call cursor(1, 1)\<CR>\<C-w>q"
1788 call assert_equal(1, line('.'))
1789 call assert_equal(1, col('.'))
1790
1791 %bwipeout!
1792 set splitscroll&
1793endfunction
Luuk van Baald5bc7622022-09-17 16:16:35 +01001794
Luuk van Baalfaf1d412022-09-19 16:45:29 +01001795function Test_nosplitscroll_misc()
Luuk van Baald5bc7622022-09-17 16:16:35 +01001796 set nosplitscroll
Luuk van Baalfaf1d412022-09-19 16:45:29 +01001797 set splitbelow
Luuk van Baald5bc7622022-09-17 16:16:35 +01001798
1799 call setline(1, range(1, &lines))
1800 norm Gzz
1801 let top = line('w0')
Luuk van Baalfaf1d412022-09-19 16:45:29 +01001802 " No scroll when aucmd_win is opened
Luuk van Baald5bc7622022-09-17 16:16:35 +01001803 call setbufvar(bufnr("test", 1) , '&buftype', 'nofile')
1804 call assert_equal(top, line('w0'))
Luuk van Baalfaf1d412022-09-19 16:45:29 +01001805 " No scroll when tab is changed/closed
1806 tab help | close
1807 call assert_equal(top, line('w0'))
1808 " No scroll when help is closed and buffer line count < window height
1809 norm ggdG
1810 call setline(1, range(1, &lines - 10))
Luuk van Baald5bc7622022-09-17 16:16:35 +01001811 norm G
1812 let top = line('w0')
1813 help | quit
1814 call assert_equal(top, line('w0'))
1815
Luuk van Baalfaf1d412022-09-19 16:45:29 +01001816 %bwipeout!
Luuk van Baald5bc7622022-09-17 16:16:35 +01001817 set splitbelow&
1818 set splitscroll&
1819endfunc
1820
Luuk van Baal20e58562022-09-23 12:57:09 +01001821function Test_nosplitscroll_callback()
1822 CheckScreendump
1823 let lines =<< trim END
1824 set nosplitscroll
1825 call setline(1, range(&lines))
1826 function WincmdCb(a, b)
1827 split | wincmd p
1828 endfunction
1829 function TermCb(a, b)
1830 close | split
1831 endfunction
1832 nnoremap t <cmd>call popup_create(term_start(&shell, { 'hidden': 1, 'exit_cb': 'TermCb' }), {})<CR>
1833 nnoremap j <cmd>call job_start([&shell, &shellcmdflag, "echo"], { 'exit_cb': 'WincmdCb' })<CR>
1834 END
1835 call writefile(lines, 'XTestNosplitscrollCallback', 'D')
1836 let buf = RunVimInTerminal('-S XTestNosplitscrollCallback', #{rows: 8})
1837
1838 call term_sendkeys(buf, "j")
1839 call VerifyScreenDump(buf, 'Test_nosplitscroll_callback_1', {})
1840
1841 call term_sendkeys(buf, ":quit\<CR>Htexit\<CR>")
1842 call VerifyScreenDump(buf, 'Test_nosplitscroll_callback_2', {})
1843
1844 call term_sendkeys(buf, ":set sb\<CR>:quit\<CR>Gj")
1845 call VerifyScreenDump(buf, 'Test_nosplitscroll_callback_3', {})
1846
1847 call term_sendkeys(buf, ":quit\<CR>Gtexit\<CR>")
1848 call VerifyScreenDump(buf, 'Test_nosplitscroll_callback_4', {})
1849endfunc
1850
Bram Moolenaar9e4d8212016-08-18 23:04:48 +02001851" vim: shiftwidth=2 sts=2 expandtab