blob: a60986810637b48141dedcfe32cc69a6aa38f4a0 [file] [log] [blame]
Bram Moolenaar991dea32016-05-24 11:31:32 +02001" Tests for window cmd (:wincmd, :split, :vsplit, :resize and etc...)
2
3func Test_window_cmd_ls0_with_split()
4 set ls=0
5 set splitbelow
6 split
7 quit
8 call assert_equal(0, &lines - &cmdheight - winheight(0))
9 new | only!
10 "
11 set splitbelow&vim
12 botright split
13 quit
14 call assert_equal(0, &lines - &cmdheight - winheight(0))
15 new | only!
16 set ls&vim
17endfunc
18
19func Test_window_cmd_cmdwin_with_vsp()
Bram Moolenaar72cf47a2018-05-10 18:23:29 +020020 let efmt = 'Expected 0 but got %d (in ls=%d, %s window)'
Bram Moolenaar991dea32016-05-24 11:31:32 +020021 for v in range(0, 2)
22 exec "set ls=" . v
23 vsplit
24 call feedkeys("q:\<CR>")
25 let ac = &lines - (&cmdheight + winheight(0) + !!v)
26 let emsg = printf(efmt, ac, v, 'left')
27 call assert_equal(0, ac, emsg)
28 wincmd w
29 let ac = &lines - (&cmdheight + winheight(0) + !!v)
30 let emsg = printf(efmt, ac, v, 'right')
31 call assert_equal(0, ac, emsg)
32 new | only!
33 endfor
34 set ls&vim
35endfunc
36
Bram Moolenaar5d2ca042016-06-26 17:11:21 +020037function Test_window_cmd_wincmd_gf()
38 let fname = 'test_gf.txt'
39 let swp_fname = '.' . fname . '.swp'
40 call writefile([], fname)
41 call writefile([], swp_fname)
42 function s:swap_exists()
43 let v:swapchoice = s:swap_choice
44 endfunc
45 augroup test_window_cmd_wincmd_gf
46 autocmd!
47 exec "autocmd SwapExists " . fname . " call s:swap_exists()"
48 augroup END
49
50 call setline(1, fname)
51 " (E)dit anyway
52 let s:swap_choice = 'e'
53 wincmd gf
54 call assert_equal(2, tabpagenr())
55 call assert_equal(fname, bufname("%"))
56 quit!
57
58 " (Q)uit
59 let s:swap_choice = 'q'
60 wincmd gf
61 call assert_equal(1, tabpagenr())
62 call assert_notequal(fname, bufname("%"))
63 new | only!
64
65 call delete(fname)
66 call delete(swp_fname)
67 augroup! test_window_cmd_wincmd_gf
68endfunc
69
Bram Moolenaar4520d442017-03-19 16:09:46 +010070func Test_window_quit()
71 e Xa
72 split Xb
73 call assert_equal(2, winnr('$'))
74 call assert_equal('Xb', bufname(winbufnr(1)))
75 call assert_equal('Xa', bufname(winbufnr(2)))
76
77 wincmd q
78 call assert_equal(1, winnr('$'))
79 call assert_equal('Xa', bufname(winbufnr(1)))
80
81 bw Xa Xb
82endfunc
83
84func Test_window_horizontal_split()
85 call assert_equal(1, winnr('$'))
86 3wincmd s
87 call assert_equal(2, winnr('$'))
88 call assert_equal(3, winheight(0))
89 call assert_equal(winwidth(1), winwidth(2))
90
91 call assert_fails('botright topleft wincmd s', 'E442:')
92 bw
93endfunc
94
95func Test_window_vertical_split()
96 call assert_equal(1, winnr('$'))
97 3wincmd v
98 call assert_equal(2, winnr('$'))
99 call assert_equal(3, winwidth(0))
100 call assert_equal(winheight(1), winheight(2))
101
102 call assert_fails('botright topleft wincmd v', 'E442:')
103 bw
104endfunc
105
Bram Moolenaar1bbb6192018-11-10 16:02:01 +0100106" Test the ":wincmd ^" and "<C-W>^" commands.
Bram Moolenaar4520d442017-03-19 16:09:46 +0100107func Test_window_split_edit_alternate()
Bram Moolenaar4520d442017-03-19 16:09:46 +0100108
Bram Moolenaar1bbb6192018-11-10 16:02:01 +0100109 " Test for failure when the alternate buffer/file no longer exists.
110 edit Xfoo | %bw
111 call assert_fails(':wincmd ^', 'E23')
112
113 " Test for the expected behavior when we have two named buffers.
114 edit Xfoo | edit Xbar
Bram Moolenaar4520d442017-03-19 16:09:46 +0100115 wincmd ^
Bram Moolenaar1bbb6192018-11-10 16:02:01 +0100116 call assert_equal('Xfoo', bufname(winbufnr(1)))
117 call assert_equal('Xbar', bufname(winbufnr(2)))
118 only
Bram Moolenaar4520d442017-03-19 16:09:46 +0100119
Bram Moolenaar1bbb6192018-11-10 16:02:01 +0100120 " Test for the expected behavior when the alternate buffer is not named.
121 enew | let l:nr1 = bufnr('%')
122 edit Xfoo | let l:nr2 = bufnr('%')
123 wincmd ^
124 call assert_equal(l:nr1, winbufnr(1))
125 call assert_equal(l:nr2, winbufnr(2))
126 only
127
128 " Test the Normal mode command.
129 call feedkeys("\<C-W>\<C-^>", 'tx')
130 call assert_equal(l:nr2, winbufnr(1))
131 call assert_equal(l:nr1, winbufnr(2))
132
133 %bw!
134endfunc
135
136" Test the ":[count]wincmd ^" and "[count]<C-W>^" commands.
137func Test_window_split_edit_bufnr()
138
139 %bwipeout
140 let l:nr = bufnr('%') + 1
141 call assert_fails(':execute "normal! ' . l:nr . '\<C-W>\<C-^>"', 'E92')
142 call assert_fails(':' . l:nr . 'wincmd ^', 'E16')
143 call assert_fails(':0wincmd ^', 'E16')
144
145 edit Xfoo | edit Xbar | edit Xbaz
146 let l:foo_nr = bufnr('Xfoo')
147 let l:bar_nr = bufnr('Xbar')
148 let l:baz_nr = bufnr('Xbaz')
149
150 call feedkeys(l:foo_nr . "\<C-W>\<C-^>", 'tx')
151 call assert_equal('Xfoo', bufname(winbufnr(1)))
152 call assert_equal('Xbaz', bufname(winbufnr(2)))
153 only
154
155 call feedkeys(l:bar_nr . "\<C-W>\<C-^>", 'tx')
156 call assert_equal('Xbar', bufname(winbufnr(1)))
157 call assert_equal('Xfoo', bufname(winbufnr(2)))
158 only
159
160 execute l:baz_nr . 'wincmd ^'
161 call assert_equal('Xbaz', bufname(winbufnr(1)))
162 call assert_equal('Xbar', bufname(winbufnr(2)))
163
164 %bw!
Bram Moolenaar4520d442017-03-19 16:09:46 +0100165endfunc
166
167func Test_window_preview()
168 " Open a preview window
169 pedit Xa
170 call assert_equal(2, winnr('$'))
171 call assert_equal(0, &previewwindow)
172
173 " Go to the preview window
174 wincmd P
175 call assert_equal(1, &previewwindow)
176
177 " Close preview window
178 wincmd z
179 call assert_equal(1, winnr('$'))
180 call assert_equal(0, &previewwindow)
181
182 call assert_fails('wincmd P', 'E441:')
183endfunc
184
185func Test_window_exchange()
186 e Xa
187
188 " Nothing happens with window exchange when there is 1 window
189 wincmd x
190 call assert_equal(1, winnr('$'))
191
192 split Xb
193 split Xc
194
195 call assert_equal('Xc', bufname(winbufnr(1)))
196 call assert_equal('Xb', bufname(winbufnr(2)))
197 call assert_equal('Xa', bufname(winbufnr(3)))
198
199 " Exchange current window 1 with window 3
200 3wincmd x
201 call assert_equal('Xa', bufname(winbufnr(1)))
202 call assert_equal('Xb', bufname(winbufnr(2)))
203 call assert_equal('Xc', bufname(winbufnr(3)))
204
205 " Exchange window with next when at the top window
206 wincmd x
207 call assert_equal('Xb', bufname(winbufnr(1)))
208 call assert_equal('Xa', bufname(winbufnr(2)))
209 call assert_equal('Xc', bufname(winbufnr(3)))
210
211 " Exchange window with next when at the middle window
212 wincmd j
213 wincmd x
214 call assert_equal('Xb', bufname(winbufnr(1)))
215 call assert_equal('Xc', bufname(winbufnr(2)))
216 call assert_equal('Xa', bufname(winbufnr(3)))
217
218 " Exchange window with next when at the bottom window.
219 " When there is no next window, it exchanges with the previous window.
220 wincmd j
221 wincmd x
222 call assert_equal('Xb', bufname(winbufnr(1)))
223 call assert_equal('Xa', bufname(winbufnr(2)))
224 call assert_equal('Xc', bufname(winbufnr(3)))
225
226 bw Xa Xb Xc
227endfunc
228
229func Test_window_rotate()
230 e Xa
231 split Xb
232 split Xc
233 call assert_equal('Xc', bufname(winbufnr(1)))
234 call assert_equal('Xb', bufname(winbufnr(2)))
235 call assert_equal('Xa', bufname(winbufnr(3)))
236
237 " Rotate downwards
238 wincmd r
239 call assert_equal('Xa', bufname(winbufnr(1)))
240 call assert_equal('Xc', bufname(winbufnr(2)))
241 call assert_equal('Xb', bufname(winbufnr(3)))
242
243 2wincmd r
244 call assert_equal('Xc', bufname(winbufnr(1)))
245 call assert_equal('Xb', bufname(winbufnr(2)))
246 call assert_equal('Xa', bufname(winbufnr(3)))
247
248 " Rotate upwards
249 wincmd R
250 call assert_equal('Xb', bufname(winbufnr(1)))
251 call assert_equal('Xa', bufname(winbufnr(2)))
252 call assert_equal('Xc', bufname(winbufnr(3)))
253
254 2wincmd R
255 call assert_equal('Xc', bufname(winbufnr(1)))
256 call assert_equal('Xb', bufname(winbufnr(2)))
257 call assert_equal('Xa', bufname(winbufnr(3)))
258
259 bot vsplit
260 call assert_fails('wincmd R', 'E443:')
261
262 bw Xa Xb Xc
263endfunc
264
265func Test_window_height()
266 e Xa
267 split Xb
268
269 let [wh1, wh2] = [winheight(1), winheight(2)]
270 " Active window (1) should have the same height or 1 more
271 " than the other window.
272 call assert_inrange(wh2, wh2 + 1, wh1)
273
274 wincmd -
275 call assert_equal(wh1 - 1, winheight(1))
276 call assert_equal(wh2 + 1, winheight(2))
277
278 wincmd +
279 call assert_equal(wh1, winheight(1))
280 call assert_equal(wh2, winheight(2))
281
282 2wincmd _
283 call assert_equal(2, winheight(1))
284 call assert_equal(wh1 + wh2 - 2, winheight(2))
285
286 wincmd =
287 call assert_equal(wh1, winheight(1))
288 call assert_equal(wh2, winheight(2))
289
290 2wincmd _
291 set winfixheight
292 split Xc
293 let [wh1, wh2, wh3] = [winheight(1), winheight(2), winheight(3)]
294 call assert_equal(2, winheight(2))
295 call assert_inrange(wh3, wh3 + 1, wh1)
296 3wincmd +
297 call assert_equal(2, winheight(2))
298 call assert_equal(wh1 + 3, winheight(1))
299 call assert_equal(wh3 - 3, winheight(3))
300 wincmd =
301 call assert_equal(2, winheight(2))
302 call assert_equal(wh1, winheight(1))
303 call assert_equal(wh3, winheight(3))
304
305 wincmd j
306 set winfixheight&
307
308 wincmd =
309 let [wh1, wh2, wh3] = [winheight(1), winheight(2), winheight(3)]
310 " Current window (2) should have the same height or 1 more
311 " than the other windows.
312 call assert_inrange(wh1, wh1 + 1, wh2)
313 call assert_inrange(wh3, wh3 + 1, wh2)
314
315 bw Xa Xb Xc
316endfunc
317
318func Test_window_width()
319 e Xa
320 vsplit Xb
321
322 let [ww1, ww2] = [winwidth(1), winwidth(2)]
323 " Active window (1) should have the same width or 1 more
324 " than the other window.
325 call assert_inrange(ww2, ww2 + 1, ww1)
326
327 wincmd <
328 call assert_equal(ww1 - 1, winwidth(1))
329 call assert_equal(ww2 + 1, winwidth(2))
330
331 wincmd >
332 call assert_equal(ww1, winwidth(1))
333 call assert_equal(ww2, winwidth(2))
334
335 2wincmd |
336 call assert_equal(2, winwidth(1))
337 call assert_equal(ww1 + ww2 - 2, winwidth(2))
338
339 wincmd =
340 call assert_equal(ww1, winwidth(1))
341 call assert_equal(ww2, winwidth(2))
342
343 2wincmd |
344 set winfixwidth
345 vsplit Xc
346 let [ww1, ww2, ww3] = [winwidth(1), winwidth(2), winwidth(3)]
Bram Moolenaar38e34832017-03-19 20:22:36 +0100347 call assert_equal(2, winwidth(2))
Bram Moolenaar4520d442017-03-19 16:09:46 +0100348 call assert_inrange(ww3, ww3 + 1, ww1)
349 3wincmd >
Bram Moolenaar38e34832017-03-19 20:22:36 +0100350 call assert_equal(2, winwidth(2))
Bram Moolenaar4520d442017-03-19 16:09:46 +0100351 call assert_equal(ww1 + 3, winwidth(1))
352 call assert_equal(ww3 - 3, winwidth(3))
353 wincmd =
Bram Moolenaar38e34832017-03-19 20:22:36 +0100354 call assert_equal(2, winwidth(2))
Bram Moolenaar4520d442017-03-19 16:09:46 +0100355 call assert_equal(ww1, winwidth(1))
356 call assert_equal(ww3, winwidth(3))
357
358 wincmd l
359 set winfixwidth&
360
361 wincmd =
362 let [ww1, ww2, ww3] = [winwidth(1), winwidth(2), winwidth(3)]
363 " Current window (2) should have the same width or 1 more
364 " than the other windows.
365 call assert_inrange(ww1, ww1 + 1, ww2)
366 call assert_inrange(ww3, ww3 + 1, ww2)
367
368 bw Xa Xb Xc
369endfunc
370
Bram Moolenaar8eeeba82017-06-25 22:45:39 +0200371func Test_equalalways_on_close()
372 set equalalways
373 vsplit
374 windo split
Bram Moolenaar1bbb6192018-11-10 16:02:01 +0100375 split
Bram Moolenaar8eeeba82017-06-25 22:45:39 +0200376 wincmd J
377 " now we have a frame top-left with two windows, a frame top-right with two
378 " windows and a frame at the bottom, full-width.
379 let height_1 = winheight(1)
380 let height_2 = winheight(2)
381 let height_3 = winheight(3)
382 let height_4 = winheight(4)
383 " closing the bottom window causes all windows to be resized.
384 close
385 call assert_notequal(height_1, winheight(1))
386 call assert_notequal(height_2, winheight(2))
387 call assert_notequal(height_3, winheight(3))
388 call assert_notequal(height_4, winheight(4))
389 call assert_equal(winheight(1), winheight(3))
390 call assert_equal(winheight(2), winheight(4))
391
392 1wincmd w
393 split
394 4wincmd w
395 resize + 5
396 " left column has three windows, equalized heights.
397 " right column has two windows, top one a bit higher
398 let height_1 = winheight(1)
399 let height_2 = winheight(2)
400 let height_4 = winheight(4)
401 let height_5 = winheight(5)
402 3wincmd w
403 " closing window in left column equalizes heights in left column but not in
404 " the right column
405 close
406 call assert_notequal(height_1, winheight(1))
407 call assert_notequal(height_2, winheight(2))
408 call assert_equal(height_4, winheight(3))
409 call assert_equal(height_5, winheight(4))
410
411 only
412 set equalalways&
413endfunc
414
Bram Moolenaar22044dc2017-12-02 15:43:37 +0100415func Test_win_screenpos()
416 call assert_equal(1, winnr('$'))
417 split
418 vsplit
419 10wincmd _
420 30wincmd |
421 call assert_equal([1, 1], win_screenpos(1))
422 call assert_equal([1, 32], win_screenpos(2))
423 call assert_equal([12, 1], win_screenpos(3))
424 call assert_equal([0, 0], win_screenpos(4))
425 only
426endfunc
427
Bram Moolenaar4520d442017-03-19 16:09:46 +0100428func Test_window_jump_tag()
429 help
430 /iccf
431 call assert_match('^|iccf|', getline('.'))
432 call assert_equal(2, winnr('$'))
433 2wincmd }
434 call assert_equal(3, winnr('$'))
435 call assert_match('^|iccf|', getline('.'))
436 wincmd k
437 call assert_match('\*iccf\*', getline('.'))
438 call assert_equal(2, winheight(0))
439
440 wincmd z
441 set previewheight=4
442 help
443 /bugs
444 wincmd }
445 wincmd k
446 call assert_match('\*bugs\*', getline('.'))
447 call assert_equal(4, winheight(0))
448 set previewheight&
449
450 %bw!
451endfunc
452
453func Test_window_newtab()
454 e Xa
455
456 call assert_equal(1, tabpagenr('$'))
457 call assert_equal("\nAlready only one window", execute('wincmd T'))
458
459 split Xb
460 split Xc
461
462 wincmd T
463 call assert_equal(2, tabpagenr('$'))
464 call assert_equal(['Xb', 'Xa'], map(tabpagebuflist(1), 'bufname(v:val)'))
465 call assert_equal(['Xc' ], map(tabpagebuflist(2), 'bufname(v:val)'))
466
467 %bw!
468endfunc
469
Bram Moolenaarf79225e2017-03-18 23:11:04 +0100470func Test_next_split_all()
471 " This was causing an illegal memory access.
472 n x
473 norm axxx
474 split
475 split
476 s/x
477 s/x
478 all
479 bwipe!
480endfunc
481
Bram Moolenaar75373f32017-08-07 22:02:30 +0200482" Tests for adjusting window and contents
483func GetScreenStr(row)
484 let str = ""
485 for c in range(1,3)
486 let str .= nr2char(screenchar(a:row, c))
487 endfor
488 return str
489endfunc
490
491func Test_window_contents()
492 enew! | only | new
493 call setline(1, range(1,256))
494
495 exe "norm! \<C-W>t\<C-W>=1Gzt\<C-W>w\<C-W>+"
496 redraw
Bram Moolenaar72cf47a2018-05-10 18:23:29 +0200497 let s3 = GetScreenStr(1)
Bram Moolenaar75373f32017-08-07 22:02:30 +0200498 wincmd p
499 call assert_equal(1, line("w0"))
500 call assert_equal('1 ', s3)
501
502 exe "norm! \<C-W>t\<C-W>=50Gzt\<C-W>w\<C-W>+"
503 redraw
Bram Moolenaar72cf47a2018-05-10 18:23:29 +0200504 let s3 = GetScreenStr(1)
Bram Moolenaar75373f32017-08-07 22:02:30 +0200505 wincmd p
506 call assert_equal(50, line("w0"))
507 call assert_equal('50 ', s3)
508
509 exe "norm! \<C-W>t\<C-W>=59Gzt\<C-W>w\<C-W>+"
510 redraw
Bram Moolenaar72cf47a2018-05-10 18:23:29 +0200511 let s3 = GetScreenStr(1)
Bram Moolenaar75373f32017-08-07 22:02:30 +0200512 wincmd p
513 call assert_equal(59, line("w0"))
514 call assert_equal('59 ', s3)
515
516 bwipeout!
517 call test_garbagecollect_now()
518endfunc
519
Bram Moolenaar2efb3232017-12-19 12:27:23 +0100520func Test_window_colon_command()
521 " This was reading invalid memory.
522 exe "norm! v\<C-W>:\<C-U>echo v:version"
523endfunc
524
Bram Moolenaar6f361c92018-01-31 19:06:50 +0100525func Test_access_freed_mem()
526 " This was accessing freed memory
527 au * 0 vs xxx
528 arg 0
529 argadd
530 all
531 all
532 au!
533 bwipe xxx
534endfunc
535
Bram Moolenaar5bab5552018-04-13 20:41:29 +0200536func Test_visual_cleared_after_window_split()
537 new | only!
538 let smd_save = &showmode
539 set showmode
540 let ls_save = &laststatus
541 set laststatus=1
542 call setline(1, ['a', 'b', 'c', 'd', ''])
543 norm! G
544 exe "norm! kkvk"
545 redraw
546 exe "norm! \<C-W>v"
547 redraw
548 " check if '-- VISUAL --' disappeared from command line
549 let columns = range(1, &columns)
550 let cmdlinechars = map(columns, 'nr2char(screenchar(&lines, v:val))')
551 let cmdline = join(cmdlinechars, '')
552 let cmdline_ltrim = substitute(cmdline, '^\s*', "", "")
553 let mode_shown = substitute(cmdline_ltrim, '\s*$', "", "")
554 call assert_equal('', mode_shown)
555 let &showmode = smd_save
556 let &laststatus = ls_save
557 bwipe!
558endfunc
559
Bram Moolenaar72cf47a2018-05-10 18:23:29 +0200560func Test_winrestcmd()
561 2split
562 3vsplit
563 let a = winrestcmd()
564 call assert_equal(2, winheight(0))
565 call assert_equal(3, winwidth(0))
566 wincmd =
567 call assert_notequal(2, winheight(0))
568 call assert_notequal(3, winwidth(0))
569 exe a
570 call assert_equal(2, winheight(0))
571 call assert_equal(3, winwidth(0))
572 only
573endfunc
574
Bram Moolenaar9e4d8212016-08-18 23:04:48 +0200575" vim: shiftwidth=2 sts=2 expandtab