robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
aa1d8a2702
master
1" Tests for window cmd (:wincmd, :split, :vsplit, :resize and etc...) 2 3source check.vim 4source screendump.vim 5 6func Test_window_cmd_ls0_with_split () 7set ls = 0 8set splitbelow 9split 10quit 11call assert_equal ( 0 , & lines - & cmdheight - winheight ( 0 )) 12new |only ! 13" 14set splitbelow & vim 15botright split 16quit 17call assert_equal ( 0 , & lines - & cmdheight - winheight ( 0 )) 18new |only ! 19set ls & vim 20endfunc 21 22func Test_window_cmd_ls0_split_scrolling () 23CheckRunVimInTerminal 24 25let lines =<< trim END 26set laststatus=0 27call setline(1, range(1, 100)) 28normal! G 29END 30call writefile ( lines , 'XTestLs0SplitScrolling' , 'D' ) 31let buf = RunVimInTerminal ( '-S XTestLs0SplitScrolling' , #{ rows : 10 }) 32 33call term_sendkeys ( buf , ":botright split\<CR>" ) 34call WaitForAssert ({ ->assert_match ( 'Bot$' , term_getline ( buf , 5 ))}) 35call assert_equal ( '100' , term_getline ( buf , 4 )) 36 37call StopVimInTerminal ( buf ) 38endfunc 39 40func Test_window_cmd_cmdwin_with_vsp () 41let efmt = 'Expected 0 but got %d (in ls=%d, %s window)' 42for v in range ( 0 , 2 ) 43exec "set ls=" . v 44vsplit 45call feedkeys ( "q:\<CR>" ) 46let ac = & lines - ( & cmdheight + winheight ( 0 ) + !! v ) 47let emsg = printf ( efmt , ac , v , 'left' ) 48call assert_equal ( 0 , ac , emsg ) 49wincmd w 50let ac = & lines - ( & cmdheight + winheight ( 0 ) + !! v ) 51let emsg = printf ( efmt , ac , v , 'right' ) 52call assert_equal ( 0 , ac , emsg ) 53new |only ! 54endfor 55set ls & vim 56endfunc 57 58func Test_cmdheight_not_changed () 59set cmdheight = 2 60set winminheight = 0 61augroup Maximize 62autocmd WinEnter * wincmd _ 63augroup END 64split 65tabnew 66tabfirst 67call assert_equal ( 2 , & cmdheight ) 68 69tabonly ! 70only 71set winminheight & cmdheight & 72augroup Maximize 73au ! 74augroup END 75augroup ! Maximize 76endfunc 77 78" Test for jumping to windows 79func Test_window_jump () 80new 81" jumping to a window with a count greater than the max windows 82exe "normal 4\<C-W>w" 83call assert_equal ( 2 , winnr ()) 84only 85endfunc 86 87func Test_window_cmd_wincmd_gf () 88let fname = 'test_gf.txt' 89let swp_fname = '.' . fname . '.swp' 90call writefile ([], fname , 'D' ) 91call writefile ([], swp_fname , 'D' ) 92function s: swap_exists () 93let v: swapchoice = s: swap_choice 94endfunc 95" Remove the catch-all that runtest.vim adds 96au ! SwapExists 97augroup test_window_cmd_wincmd_gf 98autocmd ! 99exec "autocmd SwapExists " . fname . " call s:swap_exists()" 100augroup END 101 102call setline ( 1 , fname ) 103" (E)dit anyway 104let s: swap_choice = 'e' 105wincmd gf 106call assert_equal ( 2 , tabpagenr ()) 107call assert_equal ( fname , bufname ( "%" )) 108quit ! 109 110" (Q)uit 111let s: swap_choice = 'q' 112wincmd gf 113call assert_equal ( 1 , tabpagenr ()) 114call assert_notequal ( fname , bufname ( "%" )) 115new |only ! 116 117au ! test_window_cmd_wincmd_gf 118augroup ! test_window_cmd_wincmd_gf 119delfunc s:swap_exists 120bw ! 121endfunc 122 123func Test_abort_in_wincmd_f () 124let fname = 'test_f.txt' 125let swp_fname = $ '.{fname}.swp' 126call writefile ([], fname , 'D' ) 127call writefile ([], swp_fname , 'D' ) 128" Remove the catch-all that runtest.vim adds 129au ! SwapExists 130augroup test_window_cmd_wincmd_f 131autocmd ! 132" (A)bort 133autocmd SwapExists test_f.txt let v: swapchoice = 'a' 134augroup END 135 136call setline ( 1 , fname ) 137call assert_equal ( 1 , winnr ( '$' )) 138try 139wincmd f 140catch /^Vim:Interrupt$/ 141" expected interrupt by abort 142endtry 143call assert_equal ( 1 , winnr ( '$' )) 144new |only ! 145 146" See :h W19 for the background of this au!. Ideally other tests 147" should also follow this. 148au ! test_window_cmd_wincmd_f 149augroup ! test_window_cmd_wincmd_f 150bw ! 151endfunc 152 153func Test_window_quit () 154e Xa 155split Xb 156call assert_equal ( 2 , '$' ->winnr ()) 157call assert_equal ( 'Xb' , bufname ( winbufnr ( 1 ))) 158call assert_equal ( 'Xa' , bufname ( winbufnr ( 2 ))) 159 160wincmd q 161call assert_equal ( 1 , winnr ( '$' )) 162call assert_equal ( 'Xa' , bufname ( winbufnr ( 1 ))) 163 164bw Xa Xb 165endfunc 166 167func Test_window_horizontal_split () 168call assert_equal ( 1 , winnr ( '$' )) 1693 wincmd s 170call assert_equal ( 2 , winnr ( '$' )) 171call assert_equal ( 3 , winheight ( 0 )) 172call assert_equal ( winwidth ( 1 ), 2 ->winwidth ()) 173 174call assert_fails ( 'botright topleft wincmd s' , 'E442:' ) 175bw 176endfunc 177 178func Test_window_vertical_split () 179call assert_equal ( 1 , winnr ( '$' )) 1803 wincmd v 181call assert_equal ( 2 , winnr ( '$' )) 182call assert_equal ( 3 , winwidth ( 0 )) 183call assert_equal ( winheight ( 1 ), winheight ( 2 )) 184 185call assert_fails ( 'botright topleft wincmd v' , 'E442:' ) 186bw 187endfunc 188 189" Test the ":wincmd ^" and "<C-W>^" commands. 190func Test_window_split_edit_alternate () 191" Test for failure when the alternate buffer/file no longer exists. 192edit Xfoo | %bw 193call assert_fails ( ':wincmd ^' , 'E23:' ) 194 195" Test for the expected behavior when we have two named buffers. 196edit Xfoo |edit Xbar 197wincmd ^ 198call assert_equal ( 'Xfoo' , bufname ( winbufnr ( 1 ))) 199call assert_equal ( 'Xbar' , bufname ( winbufnr ( 2 ))) 200only 201 202" Test for the expected behavior when the alternate buffer is not named. 203enew |let l: nr1 = bufnr ( '%' ) 204edit Xfoo |let l: nr2 = bufnr ( '%' ) 205wincmd ^ 206call assert_equal ( l: nr1 , winbufnr ( 1 )) 207call assert_equal ( l: nr2 , winbufnr ( 2 )) 208only 209 210" FIXME: this currently fails on AppVeyor, but passes locally 211if ! has ( 'win32' ) 212" Test the Normal mode command. 213call feedkeys ( "\<C-W>\<C-^>" , 'tx' ) 214call assert_equal ( l: nr2 , winbufnr ( 1 )) 215call assert_equal ( l: nr1 , winbufnr ( 2 )) 216endif 217 218 %bw ! 219endfunc 220 221" Test the ":[count]wincmd ^" and "[count]<C-W>^" commands. 222func Test_window_split_edit_bufnr () 223 %bwipeout 224let l: nr = bufnr ( '%' ) + 1 225call assert_fails ( ':execute "normal! ' . l: nr . '\<C-W>\<C-^>"' , 'E92:' ) 226call assert_fails ( ':' . l: nr . 'wincmd ^' , 'E16:' ) 227call assert_fails ( ':0wincmd ^' , 'E16:' ) 228 229edit Xfoo |edit Xbar |edit Xbaz 230let l: foo_nr = bufnr ( 'Xfoo' ) 231let l: bar_nr = bufnr ( 'Xbar' ) 232let l: baz_nr = bufnr ( 'Xbaz' ) 233 234" FIXME: this currently fails on AppVeyor, but passes locally 235if ! has ( 'win32' ) 236call feedkeys ( l: foo_nr . "\<C-W>\<C-^>" , 'tx' ) 237call assert_equal ( 'Xfoo' , bufname ( winbufnr ( 1 ))) 238call assert_equal ( 'Xbaz' , bufname ( winbufnr ( 2 ))) 239only 240 241call feedkeys ( l: bar_nr . "\<C-W>\<C-^>" , 'tx' ) 242call assert_equal ( 'Xbar' , bufname ( winbufnr ( 1 ))) 243call assert_equal ( 'Xfoo' , bufname ( winbufnr ( 2 ))) 244only 245 246execute l: baz_nr . 'wincmd ^' 247call assert_equal ( 'Xbaz' , bufname ( winbufnr ( 1 ))) 248call assert_equal ( 'Xbar' , bufname ( winbufnr ( 2 ))) 249endif 250 251 %bw ! 252endfunc 253 254func s: win_layout_info ( tp = tabpagenr ()) abort 255return #{ 256 \layout : winlayout ( a: tp ), 257 \pos_sizes : range ( 1 , tabpagewinnr ( a: tp , '$' )) 258 \ ->map ({ _ , nr ->win_getid ( nr , a: tp ) ->getwininfo ()[ 0 ]}) 259 \ ->map ({ _ , wininfo ->#{ id : wininfo . winid , 260 \row : wininfo . winrow , 261 \col : wininfo . wincol , 262 \width : wininfo . width , 263 \height : wininfo . height }}) 264 \ ->sort ({ a , b ->a . id - b . id }) 265 \} 266endfunc 267 268func Test_window_split_no_room () 269" N horizontal windows need >= 2*N + 1 lines: 270" - 1 line + 1 status line in each window 271" - 1 Ex command line 272" 273" 2*N + 1 <= &lines 274" N <= (lines - 1)/2 275" 276" Beyond that number of windows, E36: Not enough room is expected. 277let hor_win_count = ( & lines - 1 ) / 2 278let hor_split_count = hor_win_count - 1 279for s in range ( 1 , hor_split_count ) |split |endfor 280call assert_fails ( 'split' , 'E36:' ) 281 282botright vsplit 283wincmd | 284let info = s: win_layout_info () 285call assert_fails ( 'wincmd J' , 'E36:' ) 286call assert_fails ( 'wincmd K' , 'E36:' ) 287call assert_equal ( info , s: win_layout_info ()) 288only 289 290" N vertical windows need >= 2*(N - 1) + 1 columns: 291" - 1 column + 1 separator for each window (except last window) 292" - 1 column for the last window which does not have separator 293" 294" 2*(N - 1) + 1 <= &columns 295" 2*N - 1 <= &columns 296" N <= (&columns + 1)/2 297let ver_win_count = ( & columns + 1 ) / 2 298let ver_split_count = ver_win_count - 1 299for s in range ( 1 , ver_split_count ) |vsplit |endfor 300call assert_fails ( 'vsplit' , 'E36:' ) 301 302split 303wincmd | 304let info = s: win_layout_info () 305call assert_fails ( 'wincmd H' , 'E36:' ) 306call assert_fails ( 'wincmd L' , 'E36:' ) 307call assert_equal ( info , s: win_layout_info ()) 308 309" Check that the last statusline isn't lost. 310" Set its window's width to 2 for the test. 311wincmd j 312set laststatus = 0 winminwidth = 0 313vertical resize 2 314" Update expected positions/sizes after the resize. Layout is unchanged. 315let info . pos_sizes = s: win_layout_info (). pos_sizes 316set winminwidth & 317call setwinvar ( winnr ( 'k' ), '&statusline' , '@#' ) 318let last_stl_row = win_screenpos ( 0 )[ 0 ] - 1 319redraw 320call assert_equal ( '@#|' , GetScreenStr ( last_stl_row )) 321call assert_equal ( '~ |' , GetScreenStr ( & lines - & cmdheight )) 322 323call assert_fails ( 'wincmd H' , 'E36:' ) 324call assert_fails ( 'wincmd L' , 'E36:' ) 325call assert_equal ( info , s: win_layout_info ()) 326call setwinvar ( winnr ( 'k' ), '&statusline' , '=-' ) 327redraw 328call assert_equal ( '=-|' , GetScreenStr ( last_stl_row )) 329call assert_equal ( '~ |' , GetScreenStr ( & lines - & cmdheight )) 330 331 %bw ! 332set laststatus & 333endfunc 334 335func Test_window_exchange () 336e Xa 337 338" Nothing happens with window exchange when there is 1 window 339wincmd x 340call assert_equal ( 1 , winnr ( '$' )) 341 342split Xb 343split Xc 344 345call assert_equal ( 'Xc' , bufname ( winbufnr ( 1 ))) 346call assert_equal ( 'Xb' , bufname ( winbufnr ( 2 ))) 347call assert_equal ( 'Xa' , bufname ( winbufnr ( 3 ))) 348 349" Exchange current window 1 with window 3 3503 wincmd x 351call assert_equal ( 'Xa' , bufname ( winbufnr ( 1 ))) 352call assert_equal ( 'Xb' , bufname ( winbufnr ( 2 ))) 353call assert_equal ( 'Xc' , bufname ( winbufnr ( 3 ))) 354 355" Exchange window with next when at the top window 356wincmd x 357call assert_equal ( 'Xb' , bufname ( winbufnr ( 1 ))) 358call assert_equal ( 'Xa' , bufname ( winbufnr ( 2 ))) 359call assert_equal ( 'Xc' , bufname ( winbufnr ( 3 ))) 360 361" Exchange window with next when at the middle window 362wincmd j 363wincmd x 364call assert_equal ( 'Xb' , bufname ( winbufnr ( 1 ))) 365call assert_equal ( 'Xc' , bufname ( winbufnr ( 2 ))) 366call assert_equal ( 'Xa' , bufname ( winbufnr ( 3 ))) 367 368" Exchange window with next when at the bottom window. 369" When there is no next window, it exchanges with the previous window. 370wincmd j 371wincmd x 372call assert_equal ( 'Xb' , bufname ( winbufnr ( 1 ))) 373call assert_equal ( 'Xa' , bufname ( winbufnr ( 2 ))) 374call assert_equal ( 'Xc' , bufname ( winbufnr ( 3 ))) 375 376bw Xa Xb Xc 377endfunc 378 379func Test_window_rotate () 380e Xa 381split Xb 382split Xc 383call assert_equal ( 'Xc' , bufname ( winbufnr ( 1 ))) 384call assert_equal ( 'Xb' , bufname ( winbufnr ( 2 ))) 385call assert_equal ( 'Xa' , bufname ( winbufnr ( 3 ))) 386 387" Rotate downwards 388wincmd r 389call assert_equal ( 'Xa' , bufname ( winbufnr ( 1 ))) 390call assert_equal ( 'Xc' , bufname ( winbufnr ( 2 ))) 391call assert_equal ( 'Xb' , bufname ( winbufnr ( 3 ))) 392 3932 wincmd r 394call assert_equal ( 'Xc' , bufname ( winbufnr ( 1 ))) 395call assert_equal ( 'Xb' , bufname ( winbufnr ( 2 ))) 396call assert_equal ( 'Xa' , bufname ( winbufnr ( 3 ))) 397 398" Rotate upwards 399wincmd R 400call assert_equal ( 'Xb' , bufname ( winbufnr ( 1 ))) 401call assert_equal ( 'Xa' , bufname ( winbufnr ( 2 ))) 402call assert_equal ( 'Xc' , bufname ( winbufnr ( 3 ))) 403 4042 wincmd R 405call assert_equal ( 'Xc' , bufname ( winbufnr ( 1 ))) 406call assert_equal ( 'Xb' , bufname ( winbufnr ( 2 ))) 407call assert_equal ( 'Xa' , bufname ( winbufnr ( 3 ))) 408 409bot vsplit 410call assert_fails ( 'wincmd R' , 'E443:' ) 411 412bw Xa Xb Xc 413endfunc 414 415func Test_window_height () 416e Xa 417split Xb 418 419let [ wh1 , wh2 ] = [ winheight ( 1 ), winheight ( 2 )] 420" Active window (1) should have the same height or 1 more 421" than the other window. 422call assert_inrange ( wh2 , wh2 + 1 , wh1 ) 423 424wincmd - 425call assert_equal ( wh1 - 1 , winheight ( 1 )) 426call assert_equal ( wh2 + 1 , winheight ( 2 )) 427 428wincmd + 429call assert_equal ( wh1 , winheight ( 1 )) 430call assert_equal ( wh2 , 2 ->winheight ()) 431 4322 wincmd _ 433call assert_equal ( 2 , winheight ( 1 )) 434call assert_equal ( wh1 + wh2 - 2 , winheight ( 2 )) 435 436wincmd = 437call assert_equal ( wh1 , winheight ( 1 )) 438call assert_equal ( wh2 , winheight ( 2 )) 439 4402 wincmd _ 441set winfixheight 442split Xc 443let [ wh1 , wh2 , wh3 ] = [ winheight ( 1 ), winheight ( 2 ), winheight ( 3 )] 444call assert_equal ( 2 , winheight ( 2 )) 445call assert_inrange ( wh3 , wh3 + 1 , wh1 ) 4463 wincmd + 447call assert_equal ( 2 , winheight ( 2 )) 448call assert_equal ( wh1 + 3 , winheight ( 1 )) 449call assert_equal ( wh3 - 3 , winheight ( 3 )) 450wincmd = 451call assert_equal ( 2 , winheight ( 2 )) 452call assert_equal ( wh1 , winheight ( 1 )) 453call assert_equal ( wh3 , winheight ( 3 )) 454 455wincmd j 456set winfixheight & 457 458wincmd = 459let [ wh1 , wh2 , wh3 ] = [ winheight ( 1 ), winheight ( 2 ), winheight ( 3 )] 460" Current window (2) should have the same height or 1 more 461" than the other windows. 462call assert_inrange ( wh1 , wh1 + 1 , wh2 ) 463call assert_inrange ( wh3 , wh3 + 1 , wh2 ) 464 465bw Xa Xb Xc 466endfunc 467 468func Test_wincmd_equal () 469edit Xone 470below split Xtwo 471rightbelow vsplit Xthree 472call assert_equal ( 'Xone' , bufname ( winbufnr ( 1 ))) 473call assert_equal ( 'Xtwo' , bufname ( winbufnr ( 2 ))) 474call assert_equal ( 'Xthree' , bufname ( winbufnr ( 3 ))) 475 476" Xone and Xtwo should be about the same height 477let [ wh1 , wh2 ] = [ winheight ( 1 ), winheight ( 2 )] 478call assert_inrange ( wh1 - 1 , wh1 + 1 , wh2 ) 479" Xtwo and Xthree should be about the same width 480let [ ww2 , ww3 ] = [ winwidth ( 2 ), winwidth ( 3 )] 481call assert_inrange ( ww2 - 1 , ww2 + 1 , ww3 ) 482 4831 wincmd w 48410 wincmd _ 4852 wincmd w 48620 wincmd | 487call assert_equal ( 10 , winheight ( 1 )) 488call assert_equal ( 20 , winwidth ( 2 )) 489 490" equalizing horizontally doesn't change the heights 491hor wincmd = 492call assert_equal ( 10 , winheight ( 1 )) 493let [ ww2 , ww3 ] = [ winwidth ( 2 ), winwidth ( 3 )] 494call assert_inrange ( ww2 - 1 , ww2 + 1 , ww3 ) 495 4962 wincmd w 49720 wincmd | 498call assert_equal ( 20 , winwidth ( 2 )) 499" equalizing vertically doesn't change the widths 500vert wincmd = 501call assert_equal ( 20 , winwidth ( 2 )) 502let [ wh1 , wh2 ] = [ winheight ( 1 ), winheight ( 2 )] 503call assert_inrange ( wh1 - 1 , wh1 + 1 , wh2 ) 504 505bwipe Xone Xtwo Xthree 506endfunc 507 508func Test_window_width () 509e Xa 510vsplit Xb 511 512let [ ww1 , ww2 ] = [ winwidth ( 1 ), winwidth ( 2 )] 513" Active window (1) should have the same width or 1 more 514" than the other window. 515call assert_inrange ( ww2 , ww2 + 1 , ww1 ) 516 517wincmd < 518call assert_equal ( ww1 - 1 , winwidth ( 1 )) 519call assert_equal ( ww2 + 1 , winwidth ( 2 )) 520 521wincmd > 522call assert_equal ( ww1 , winwidth ( 1 )) 523call assert_equal ( ww2 , winwidth ( 2 )) 524 5252 wincmd | 526call assert_equal ( 2 , winwidth ( 1 )) 527call assert_equal ( ww1 + ww2 - 2 , winwidth ( 2 )) 528 529wincmd = 530call assert_equal ( ww1 , winwidth ( 1 )) 531call assert_equal ( ww2 , winwidth ( 2 )) 532 5332 wincmd | 534set winfixwidth 535vsplit Xc 536let [ ww1 , ww2 , ww3 ] = [ winwidth ( 1 ), winwidth ( 2 ), winwidth ( 3 )] 537call assert_equal ( 2 , winwidth ( 2 )) 538call assert_inrange ( ww3 , ww3 + 1 , ww1 ) 5393 wincmd > 540call assert_equal ( 2 , winwidth ( 2 )) 541call assert_equal ( ww1 + 3 , winwidth ( 1 )) 542call assert_equal ( ww3 - 3 , winwidth ( 3 )) 543wincmd = 544call assert_equal ( 2 , winwidth ( 2 )) 545call assert_equal ( ww1 , winwidth ( 1 )) 546call assert_equal ( ww3 , winwidth ( 3 )) 547 548wincmd l 549set winfixwidth & 550 551wincmd = 552let [ ww1 , ww2 , ww3 ] = [ winwidth ( 1 ), winwidth ( 2 ), winwidth ( 3 )] 553" Current window (2) should have the same width or 1 more 554" than the other windows. 555call assert_inrange ( ww1 , ww1 + 1 , ww2 ) 556call assert_inrange ( ww3 , ww3 + 1 , ww2 ) 557 558" when the current window width is less than the new 'winwidth', the current 559" window width should be increased. 560enew |only 561split 56210 vnew 563set winwidth = 15 564call assert_equal ( 15 , winwidth ( 0 )) 565 566 %bw ! 567endfunc 568 569func Test_equalalways_on_close () 570set equalalways 571vsplit 572windo split 573split 574wincmd J 575" now we have a frame top-left with two windows, a frame top-right with two 576" windows and a frame at the bottom, full-width. 577let height_1 = winheight ( 1 ) 578let height_2 = winheight ( 2 ) 579let height_3 = winheight ( 3 ) 580let height_4 = winheight ( 4 ) 581" closing the bottom window causes all windows to be resized. 582close 583call assert_notequal ( height_1 , winheight ( 1 )) 584call assert_notequal ( height_2 , winheight ( 2 )) 585call assert_notequal ( height_3 , winheight ( 3 )) 586call assert_notequal ( height_4 , winheight ( 4 )) 587call assert_equal ( winheight ( 1 ), winheight ( 3 )) 588call assert_equal ( winheight ( 2 ), winheight ( 4 )) 589 5901 wincmd w 591split 5924 wincmd w 593resize +5 594" left column has three windows, equalized heights. 595" right column has two windows, top one a bit higher 596let height_1 = winheight ( 1 ) 597let height_2 = winheight ( 2 ) 598let height_4 = winheight ( 4 ) 599let height_5 = winheight ( 5 ) 6003 wincmd w 601" closing window in left column equalizes heights in left column but not in 602" the right column 603close 604call assert_notequal ( height_1 , winheight ( 1 )) 605call assert_notequal ( height_2 , winheight ( 2 )) 606call assert_equal ( height_4 , winheight ( 3 )) 607call assert_equal ( height_5 , winheight ( 4 )) 608 609only 610set equalalways & 611endfunc 612 613func Test_win_screenpos () 614CheckFeature quickfix 615 616call assert_equal ( 1 , winnr ( '$' )) 617split 618vsplit 61910 wincmd _ 62030 wincmd | 621call assert_equal ([ 1 , 1 ], win_screenpos ( 1 )) 622call assert_equal ([ 1 , 32 ], win_screenpos ( 2 )) 623call assert_equal ([ 12 , 1 ], win_screenpos ( 3 )) 624call assert_equal ([ 0 , 0 ], win_screenpos ( 4 )) 625call assert_fails ( 'let l = win_screenpos([])' , 'E745:' ) 626only 627endfunc 628 629func Test_window_jump_tag () 630CheckFeature quickfix 631 632help 633/ Kuwasha 634call assert_match ( '^- |Kuwasha|' , getline ( '.' )) 635call assert_equal ( 2 , winnr ( '$' )) 636norm ! fK 6372 wincmd } 638call assert_equal ( 3 , winnr ( '$' )) 639call assert_match ( '^- |Kuwasha|' , getline ( '.' )) 640wincmd k 641call assert_match ( '\*Kuwasha\*' , getline ( '.' )) 642call assert_equal ( 2 , winheight ( 0 )) 643 644wincmd z 645set previewheight = 4 646help 647/ bugs 648norm ! fb 649wincmd } 650wincmd k 651call assert_match ( '\*bugs\*' , getline ( '.' )) 652call assert_equal ( 4 , winheight ( 0 )) 653set previewheight & 654 655 %bw ! 656endfunc 657 658func Test_window_newtab () 659e Xa 660 661call assert_equal ( 1 , tabpagenr ( '$' )) 662call assert_equal ( "\nAlready only one window" , execute ( 'wincmd T' )) 663 664split Xb 665split Xc 666 667wincmd T 668call assert_equal ( 2 , tabpagenr ( '$' )) 669call assert_equal ([ 'Xb' , 'Xa' ], map ( tabpagebuflist ( 1 ), 'bufname(v:val)' )) 670call assert_equal ([ 'Xc' ], map ( 2 ->tabpagebuflist (), 'bufname(v:val)' )) 671call assert_equal ([ 'Xc' ], map ( tabpagebuflist (), 'bufname(v:val)' )) 672 673 %bw ! 674endfunc 675 676func Test_next_split_all () 677" This was causing an illegal memory access. 678n x 679norm axxx 680split 681split 682s /x 683s /x 684all 685bwipe ! 686endfunc 687 688" Tests for adjusting window and contents 689func GetScreenStr ( row ) 690let str = "" 691for c in range ( 1 , 3 ) 692let str .= nr2char ( screenchar ( a: row , c )) 693endfor 694return str 695endfunc 696 697func Test_window_contents () 698enew ! |only |new 699call setline ( 1 , range ( 1 , 256 )) 700 701exe "norm! \<C-W>t\<C-W>=1Gzt\<C-W>w\<C-W>+" 702redraw 703let s3 = GetScreenStr ( 1 ) 704wincmd p 705call assert_equal ( 1 , line ( "w0" )) 706call assert_equal ( '1 ' , s3 ) 707 708exe "norm! \<C-W>t\<C-W>=50Gzt\<C-W>w\<C-W>+" 709redraw 710let s3 = GetScreenStr ( 1 ) 711wincmd p 712call assert_equal ( 50 , line ( "w0" )) 713call assert_equal ( '50 ' , s3 ) 714 715exe "norm! \<C-W>t\<C-W>=59Gzt\<C-W>w\<C-W>+" 716redraw 717let s3 = GetScreenStr ( 1 ) 718wincmd p 719call assert_equal ( 59 , line ( "w0" )) 720call assert_equal ( '59 ' , s3 ) 721 722 %d 723call setline ( 1 , [ 'one' , 'two' , 'three' ]) 724call assert_equal ( 1 , line ( 'w0' )) 725call assert_equal ( 3 , line ( 'w$' )) 726 727bwipeout ! 728call test_garbagecollect_now () 729endfunc 730 731func Test_window_colon_command () 732" This was reading invalid memory. 733exe "norm! v\<C-W>:\<C-U>echo v:version" 734endfunc 735 736func Test_access_freed_mem () 737call assert_equal ( & columns , winwidth ( 0 )) 738" This was accessing freed memory (but with what events?) 739au BufEnter , BufLeave , WinEnter , WinLeave 0 vs xxx 740arg 0 741argadd 742call assert_fails ( "all" , "E242:" ) 743au ! 744bwipe xxx 745call assert_equal ( & columns , winwidth ( 0 )) 746endfunc 747 748func Test_visual_cleared_after_window_split () 749new |only ! 750let smd_save = & showmode 751set showmode 752let ls_save = & laststatus 753set laststatus = 1 754call setline ( 1 , [ 'a' , 'b' , 'c' , 'd' , '' ]) 755norm ! G 756exe "norm! kkvk" 757redraw 758exe "norm! \<C-W>v" 759redraw 760" check if '-- VISUAL --' disappeared from command line 761let columns = range ( 1 , & columns ) 762let cmdlinechars = map ( columns , 'nr2char(screenchar(&lines, v:val))' ) 763let cmdline = join ( cmdlinechars , '' ) 764let cmdline_ltrim = substitute ( cmdline , '^\s*' , "" , "" ) 765let mode_shown = substitute ( cmdline_ltrim , '\s*$' , "" , "" ) 766call assert_equal ( '' , mode_shown ) 767let & showmode = smd_save 768let & laststatus = ls_save 769bwipe ! 770endfunc 771 772func Test_winrestcmd () 7732 split 7743 vsplit 775let restcmd = winrestcmd () 776call assert_equal ( 2 , winheight ( 0 )) 777call assert_equal ( 3 , winwidth ( 0 )) 778wincmd = 779call assert_notequal ( 2 , winheight ( 0 )) 780call assert_notequal ( 3 , winwidth ( 0 )) 781exe restcmd 782call assert_equal ( 2 , winheight ( 0 )) 783call assert_equal ( 3 , winwidth ( 0 )) 784only 785 786wincmd v 787wincmd s 788wincmd v 789redraw 790let restcmd = winrestcmd () 791wincmd _ 792wincmd | 793exe restcmd 794redraw 795call assert_equal ( restcmd , winrestcmd ()) 796 797only 798endfunc 799 800func Fun_RenewFile () 801" Need to wait a bit for the timestamp to be older. 802let old_ftime = getftime ( "tmp.txt" ) 803while getftime ( "tmp.txt" ) == old_ftime 804sleep 100m 805silent execute '!echo "1" > tmp.txt' 806endwhile 807sp 808wincmd p 809edit ! tmp.txt 810endfunc 811 812func Test_window_prevwin () 813" Can we make this work on MS-Windows? 814CheckUnix 815 816set hidden autoread 817call writefile ([ '2' ], 'tmp.txt' , 'D' ) 818new tmp.txt 819q 820call Fun_RenewFile () 821call assert_equal ( 2 , winnr ()) 822wincmd p 823call assert_equal ( 1 , winnr ()) 824wincmd p 825q 826call Fun_RenewFile () 827call assert_equal ( 2 , winnr ()) 828wincmd p 829call assert_equal ( 1 , winnr ()) 830wincmd p 831" reset 832q 833set hidden & vim autoread & vim 834delfunc Fun_RenewFile 835bw ! 836endfunc 837 838func Test_relative_cursor_position_in_one_line_window () 839new 840only 841call setline ( 1 , range ( 1 , 10000 )) 842normal 50% 843let lnum = getcurpos ()[ 1 ] 844split 845split 846" make third window take as many lines as possible, other windows will 847" become one line 8483 wincmd w 849for i in range ( 1 , & lines - 6 ) 850wincmd + 851redraw ! 852endfor 853 854" first and second window should show cursor line 855let wininfo = getwininfo () 856call assert_equal ( lnum , wininfo [ 0 ]. topline ) 857call assert_equal ( lnum , wininfo [ 1 ]. topline ) 858 859only ! 860bwipe ! 861call assert_fails ( 'call winrestview(v:_null_dict)' , 'E1297:' ) 862endfunc 863 864func Test_relative_cursor_position_after_move_and_resize () 865let so_save = & so 866set so = 0 867enew 868call setline ( 1 , range ( 1 , 10000 )) 869normal 50% 870split 8711 wincmd w 872" Move cursor to first line in window 873normal H 874redraw ! 875" Reduce window height to two lines 876let height = winheight ( 0 ) 877while winheight ( 0 ) > 2 878wincmd - 879redraw ! 880endwhile 881" move cursor to second/last line in window 882normal j 883" restore previous height 884while winheight ( 0 ) < height 885wincmd + 886redraw ! 887endwhile 888" make window two lines again 889while winheight ( 0 ) > 2 890wincmd - 891redraw ! 892endwhile 893 894" cursor should be at bottom line 895let info = getwininfo ( win_getid ())[ 0 ] 896call assert_equal ( info . topline + 1 , getcurpos ()[ 1 ]) 897 898only ! 899bwipe ! 900let & so = so_save 901endfunc 902 903func Test_relative_cursor_position_after_resize () 904let so_save = & so 905set so = 0 906enew 907call setline ( 1 , range ( 1 , 10000 )) 908normal 50% 909split 9101 wincmd w 911let winid1 = win_getid () 912let info = getwininfo ( winid1 )[ 0 ] 913" Move cursor to second line in window 914exe "normal " . ( info . topline + 1 ) . "G" 915redraw ! 916let lnum = getcurpos ()[ 1 ] 917 918" Make the window only two lines high, cursor should end up in top line 9192 wincmd w 920exe ( info . height - 2 ) . "wincmd +" 921redraw ! 922let info = getwininfo ( winid1 )[ 0 ] 923call assert_equal ( lnum , info . topline ) 924 925only ! 926bwipe ! 927let & so = so_save 928endfunc 929 930func Test_relative_cursor_second_line_after_resize () 931let so_save = & so 932set so = 0 933enew 934call setline ( 1 , range ( 1 , 10000 )) 935normal 50% 936split 9371 wincmd w 938let winid1 = win_getid () 939let info = getwininfo ( winid1 )[ 0 ] 940 941" Make the window only two lines high 9422 wincmd _ 943 944" Move cursor to second line in window 945normal H 946normal j 947 948" Make window size bigger, then back to 2 lines 949for i in range ( 1 , 10 ) 950wincmd + 951redraw ! 952endfor 953for i in range ( 1 , 10 ) 954wincmd - 955redraw ! 956endfor 957 958" cursor should end up in bottom line 959let info = getwininfo ( winid1 )[ 0 ] 960call assert_equal ( info . topline + 1 , getcurpos ()[ 1 ]) 961 962only ! 963bwipe ! 964let & so = so_save 965endfunc 966 967func Test_split_noscroll () 968let so_save = & so 969enew 970call setline ( 1 , range ( 1 , 8 )) 971normal 100% 972split 973 9741 wincmd w 975let winid1 = win_getid () 976let info1 = getwininfo ( winid1 )[ 0 ] 977 9782 wincmd w 979let winid2 = win_getid () 980let info2 = getwininfo ( winid2 )[ 0 ] 981 982call assert_equal ( 1 , info1 . topline ) 983call assert_equal ( 1 , info2 . topline ) 984 985" window that fits all lines by itself, but not when split: closing other 986" window should restore fraction. 987only ! 988call setline ( 1 , range ( 1 , & lines - 10 )) 989exe & lines / 4 990let winid1 = win_getid () 991let info1 = getwininfo ( winid1 )[ 0 ] 992call assert_equal ( 1 , info1 . topline ) 993new 994redraw 995close 996let info1 = getwininfo ( winid1 )[ 0 ] 997call assert_equal ( 1 , info1 . topline ) 998 999bwipe ! 1000let & so = so_save 1001endfunc 1002 1003" Tests for the winnr() function 1004func Test_winnr () 1005only |tabonly 1006call assert_equal ( 1 , winnr ( 'j' )) 1007call assert_equal ( 1 , winnr ( 'k' )) 1008call assert_equal ( 1 , winnr ( 'h' )) 1009call assert_equal ( 1 , winnr ( 'l' )) 1010 1011" create a set of horizontally and vertically split windows 1012leftabove new |wincmd p 1013leftabove new |wincmd p 1014rightbelow new |wincmd p 1015rightbelow new |wincmd p 1016leftabove vnew |wincmd p 1017leftabove vnew |wincmd p 1018rightbelow vnew |wincmd p 1019rightbelow vnew |wincmd p 1020 1021call assert_equal ( 8 , winnr ( 'j' )) 1022call assert_equal ( 2 , winnr ( 'k' )) 1023call assert_equal ( 4 , winnr ( 'h' )) 1024call assert_equal ( 6 , winnr ( 'l' )) 1025call assert_equal ( 9 , winnr ( '2j' )) 1026call assert_equal ( 1 , winnr ( '2k' )) 1027call assert_equal ( 3 , winnr ( '2h' )) 1028call assert_equal ( 7 , winnr ( '2l' )) 1029 1030" Error cases 1031call assert_fails ( "echo winnr('0.2k')" , 'E15:' ) 1032call assert_equal ( 2 , winnr ( '-2k' )) 1033call assert_fails ( "echo winnr('-2xj')" , 'E15:' ) 1034call assert_fails ( "echo winnr('j2j')" , 'E15:' ) 1035call assert_fails ( "echo winnr('ll')" , 'E15:' ) 1036call assert_fails ( "echo winnr('5')" , 'E15:' ) 1037call assert_equal ( 4 , winnr ( '0h' )) 1038call assert_fails ( "let w = winnr([])" , 'E730:' ) 1039call assert_equal ( 'unknown' , win_gettype ( -1 )) 1040call assert_equal ( -1 , winheight ( -1 )) 1041call assert_equal ( -1 , winwidth ( -1 )) 1042 1043tabnew 1044call assert_equal ( 8 , tabpagewinnr ( 1 , 'j' )) 1045call assert_equal ( 2 , 1 ->tabpagewinnr ( 'k' )) 1046call assert_equal ( 4 , tabpagewinnr ( 1 , 'h' )) 1047call assert_equal ( 6 , tabpagewinnr ( 1 , 'l' )) 1048 1049only |tabonly 1050endfunc 1051 1052func Test_winrestview () 1053split runtest.vim 1054normal 50% 1055let view = winsaveview () 1056close 1057split runtest.vim 1058eval view ->winrestview () 1059call assert_equal ( view , winsaveview ()) 1060 1061bwipe ! 1062call assert_fails ( 'call winrestview(v:_null_dict)' , 'E1297:' ) 1063endfunc 1064 1065func Test_win_splitmove () 1066CheckFeature quickfix 1067 1068edit a 1069leftabove split b 1070leftabove vsplit c 1071leftabove split d 1072 1073" win_splitmove doesn't actually create or close any windows, so expect an 1074" unchanged winid and no WinNew/WinClosed events, like :wincmd H/J/K/L. 1075let s: triggered = [] 1076augroup WinSplitMove 1077au ! 1078au WinNew Pre * lets: triggered += ['WinNewPre'] 1079au WinNew * let s: triggered += [ 'WinNew' , win_getid ()] 1080au WinClosed * let s: triggered += [ 'WinClosed' , str2nr ( expand ( '<afile>' ))] 1081augroup END 1082let winid = win_getid () 1083 1084call assert_equal ( 0 , win_splitmove ( winnr (), winnr ( 'l' ))) 1085call assert_equal ( bufname ( winbufnr ( 1 )), 'c' ) 1086call assert_equal ( bufname ( winbufnr ( 2 )), 'd' ) 1087call assert_equal ( bufname ( winbufnr ( 3 )), 'b' ) 1088call assert_equal ( bufname ( winbufnr ( 4 )), 'a' ) 1089call assert_equal ( 0 , win_splitmove ( winnr (), winnr ( 'j' ), { 'vertical' : 1 })) 1090call assert_equal ( 0 , win_splitmove ( winnr (), winnr ( 'j' ), { 'vertical' : 1 })) 1091call assert_equal ( bufname ( winbufnr ( 1 )), 'c' ) 1092call assert_equal ( bufname ( winbufnr ( 2 )), 'b' ) 1093call assert_equal ( bufname ( winbufnr ( 3 )), 'd' ) 1094call assert_equal ( bufname ( winbufnr ( 4 )), 'a' ) 1095call assert_equal ( 0 , win_splitmove ( winnr (), winnr ( 'k' ), { 'vertical' : 1 })) 1096call assert_equal ( bufname ( winbufnr ( 1 )), 'd' ) 1097call assert_equal ( bufname ( winbufnr ( 2 )), 'c' ) 1098call assert_equal ( bufname ( winbufnr ( 3 )), 'b' ) 1099call assert_equal ( bufname ( winbufnr ( 4 )), 'a' ) 1100call assert_equal ( 0 , win_splitmove ( winnr (), winnr ( 'j' ), { 'rightbelow' : v: true })) 1101call assert_equal ( bufname ( winbufnr ( 1 )), 'c' ) 1102call assert_equal ( bufname ( winbufnr ( 2 )), 'b' ) 1103call assert_equal ( bufname ( winbufnr ( 3 )), 'a' ) 1104call assert_equal ( bufname ( winbufnr ( 4 )), 'd' ) 1105call assert_fails ( 'call win_splitmove(winnr(), winnr("k"), v:_null_dict)' , 'E1297:' ) 1106call assert_equal ([], s: triggered ) 1107call assert_equal ( winid , win_getid ()) 1108 1109unlet ! s: triggered 1110au ! WinSplitMove 1111only |bd 1112 1113call assert_fails ( 'call win_splitmove(winnr(), 123)' , 'E957:' ) 1114call assert_fails ( 'call win_splitmove(123, winnr())' , 'E957:' ) 1115call assert_fails ( 'call win_splitmove(winnr(), winnr())' , 'E957:' ) 1116 1117tabnew 1118call assert_fails ( 'call win_splitmove(1, win_getid(1, 1))' , 'E957:' ) 1119tabclose 1120 1121split 1122augroup WinSplitMove 1123au ! 1124au WinEnter * ++oncecall win_gotoid ( win_getid ( winnr ( '#' ))) 1125augroup END 1126call assert_fails ( 'call win_splitmove(winnr(), winnr("#"))' , 'E855:' ) 1127 1128augroup WinSplitMove 1129au ! 1130au WinLeave * ++oncequit 1131augroup END 1132call assert_fails ( 'call win_splitmove(winnr(), winnr("#"))' , 'E855:' ) 1133 1134split 1135split 1136augroup WinSplitMove 1137au ! 1138au WinEnter * ++oncelet s: triggered = v: true 1139 \|call assert_fails ( 'call win_splitmove(winnr(), winnr("$"))' , 'E242:' ) 1140 \|call assert_fails ( 'call win_splitmove(winnr("$"), winnr())' , 'E242:' ) 1141augroup END 1142quit 1143call assert_equal ( v: true , s: triggered ) 1144unlet ! s: triggered 1145 1146new 1147augroup WinSplitMove 1148au ! 1149au BufHidden * ++oncelet s: triggered = v: true 1150 \|call assert_fails ( 'call win_splitmove(winnr(), winnr("#"))' , 'E1159:' ) 1151augroup END 1152hide 1153call assert_equal ( v: true , s: triggered ) 1154unlet ! s: triggered 1155 1156split 1157let close_win = winnr ( '#' ) 1158augroup WinSplitMove 1159au ! 1160au WinEnter * ++oncequit ! 1161augroup END 1162call win_splitmove ( close_win , winnr ()) 1163call assert_equal ( 0 , win_id2win ( close_win )) 1164 1165au ! WinSplitMove 1166augroup ! WinSplitMove 1167 %bw ! 1168endfunc 1169 1170" Test for the :only command 1171func Test_window_only () 1172new 1173set modified 1174new 1175call assert_fails ( 'only' , 'E445:' ) 1176only ! 1177" Test for :only with a count 1178let wid = win_getid () 1179new 1180new 11813 only 1182call assert_equal ( 1 , winnr ( '$' )) 1183call assert_equal ( wid , win_getid ()) 1184call assert_fails ( 'close' , 'E444:' ) 1185call assert_fails ( '%close' , 'E16:' ) 1186endfunc 1187 1188" Test for errors with :wincmd 1189func Test_wincmd_errors () 1190call assert_fails ( 'wincmd g' , 'E474:' ) 1191call assert_fails ( 'wincmd ab' , 'E474:' ) 1192endfunc 1193 1194" Test for errors with :winpos 1195func Test_winpos_errors () 1196throw 'Skipped: Nvim does not have :winpos' 1197if ! has ( "gui_running" ) && ! has ( 'win32' ) 1198call assert_fails ( 'winpos' , 'E188:' ) 1199endif 1200call assert_fails ( 'winpos 10' , 'E466:' ) 1201endfunc 1202 1203" Test for +cmd in a :split command 1204func Test_split_cmd () 1205split +set\ readonly 1206call assert_equal ( 1 , & readonly ) 1207call assert_equal ( 2 , winnr ( '$' )) 1208close 1209endfunc 1210 1211" Create maximum number of horizontally or vertically split windows and then 1212" run commands that create a new horizontally/vertically split window 1213func Run_noroom_for_newwindow_test ( dir_arg ) 1214let dir = ( a: dir_arg == 'v' ) ? 'vert ' : '' 1215 1216" Open as many windows as possible 1217while v: true 1218try 1219exe dir . 'new' 1220catch /E36:/ 1221break 1222endtry 1223endwhile 1224 1225call writefile ([ 'first' , 'second' , 'third' ], 'Xnorfile1' ) 1226call writefile ([], 'Xnorfile2' ) 1227call writefile ([], 'Xnorfile3' ) 1228 1229" Argument list related commands 1230args Xnorfile1 Xnorfile2 Xnorfile3 1231next 1232for cmd in [ 'sargument 2' , 'snext' , 'sprevious' , 'sNext' , 'srewind' , 1233 \'sfirst' , 'slast' ] 1234call assert_fails ( dir .. cmd , 'E36:' ) 1235endfor 1236 %argdelete 1237 1238" Buffer related commands 1239set modified 1240hide enew 1241for cmd in [ 'sbuffer Xnorfile1' , 'sbnext' , 'sbprevious' , 'sbNext' , 'sbrewind' , 1242 \'sbfirst' , 'sblast' , 'sball' , 'sbmodified' , 'sunhide' ] 1243call assert_fails ( dir .. cmd , 'E36:' ) 1244endfor 1245 1246" Window related commands 1247for cmd in [ 'split' , 'split Xnorfile2' , 'new' , 'new Xnorfile3' , 'sview Xnorfile1' , 1248 \'sfind runtest.vim' ] 1249call assert_fails ( dir .. cmd , 'E36:' ) 1250endfor 1251 1252" Help 1253call assert_fails ( dir .. 'help' , 'E36:' ) 1254call assert_fails ( dir .. 'helpgrep window' , 'E36:' ) 1255 1256" Command-line window 1257if a: dir_arg == 'h' 1258" Cmd-line window is always a horizontally split window 1259call assert_beeps ( 'call feedkeys("q:\<CR>", "xt")' ) 1260endif 1261 1262" Quickfix and location list window 1263if has ( 'quickfix' ) 1264cexpr '' 1265call assert_fails ( dir .. 'copen' , 'E36:' ) 1266lexpr '' 1267call assert_fails ( dir .. 'lopen' , 'E36:' ) 1268 1269" Preview window 1270call assert_fails ( dir .. 'pedit Xnorfile2' , 'E36:' ) 1271call assert_fails ( dir .. 'pbuffer' , 'E36:' ) 1272call setline ( 1 , 'abc' ) 1273call assert_fails ( dir .. 'psearch abc' , 'E36:' ) 1274endif 1275 1276" Window commands (CTRL-W ^ and CTRL-W f) 1277if a: dir_arg == 'h' 1278call assert_fails ( 'call feedkeys("\<C-W>^", "xt")' , 'E36:' ) 1279call setline ( 1 , 'Xnorfile1' ) 1280call assert_fails ( 'call feedkeys("gg\<C-W>f", "xt")' , 'E36:' ) 1281endif 1282enew ! 1283 1284" Tag commands (:stag, :stselect and :stjump) 1285call writefile ([ "!_TAG_FILE_ENCODING\tutf-8\t//" , 1286 \"second\tXnorfile1\t2" , 1287 \"third\tXnorfile1\t3" ,], 1288 \'Xtags' ) 1289set tags = Xtags 1290call assert_fails ( dir .. 'stag second' , 'E36:' ) 1291call assert_fails ( 'call feedkeys(":" .. dir .. "stselect second\n1\n", "xt")' , 'E36:' ) 1292call assert_fails ( dir .. 'stjump second' , 'E36:' ) 1293call assert_fails ( dir .. 'ptag second' , 'E36:' ) 1294set tags & 1295call delete ( 'Xtags' ) 1296 1297" :isplit and :dsplit 1298call setline ( 1 , [ '#define FOO 1' , 'FOO' ]) 1299normal 2G 1300call assert_fails ( dir .. 'isplit FOO' , 'E36:' ) 1301call assert_fails ( dir .. 'dsplit FOO' , 'E36:' ) 1302 1303" terminal 1304if has ( 'terminal' ) 1305call assert_fails ( dir .. 'terminal' , 'E36:' ) 1306endif 1307 1308 %bwipe ! 1309call delete ( 'Xnorfile1' ) 1310call delete ( 'Xnorfile2' ) 1311call delete ( 'Xnorfile3' ) 1312only 1313endfunc 1314 1315func Test_split_cmds_with_no_room () 1316throw "Skipped: Nvim supports cmdwin freedom #40312" 1317call Run_noroom_for_newwindow_test ( 'h' ) 1318call Run_noroom_for_newwindow_test ( 'v' ) 1319endfunc 1320 1321" Test for various wincmd failures 1322func Test_wincmd_fails () 1323only ! 1324call assert_beeps ( "normal \<C-W>w" ) 1325call assert_beeps ( "normal \<C-W>p" ) 1326call assert_beeps ( "normal \<C-W>gk" ) 1327call assert_beeps ( "normal \<C-W>r" ) 1328call assert_beeps ( "normal \<C-W>K" ) 1329call assert_beeps ( "normal \<C-W>H" ) 1330call assert_beeps ( "normal \<C-W>2gt" ) 1331endfunc 1332 1333func Test_window_resize () 1334" Vertical :resize (absolute, relative, min and max size). 1335vsplit 1336vert resize 8 1337call assert_equal ( 8 , winwidth ( 0 )) 1338vert resize +2 1339call assert_equal ( 10 , winwidth ( 0 )) 1340vert resize -2 1341call assert_equal ( 8 , winwidth ( 0 )) 1342vert resize 1343call assert_equal ( & columns - 2 , winwidth ( 0 )) 1344vert resize 0 1345call assert_equal ( 1 , winwidth ( 0 )) 1346vert resize 99999 1347call assert_equal ( & columns - 2 , winwidth ( 0 )) 1348 1349 %bwipe ! 1350 1351" Horizontal :resize (with absolute, relative size, min and max size). 1352split 1353resize 8 1354call assert_equal ( 8 , winheight ( 0 )) 1355resize +2 1356call assert_equal ( 10 , winheight ( 0 )) 1357resize -2 1358call assert_equal ( 8 , winheight ( 0 )) 1359resize 1360call assert_equal ( & lines - 4 , winheight ( 0 )) 1361resize 0 1362call assert_equal ( 1 , winheight ( 0 )) 1363resize 99999 1364call assert_equal ( & lines - 4 , winheight ( 0 )) 1365 1366" :resize with explicit window number. 1367let other_winnr = winnr ( 'j' ) 1368exe other_winnr .. 'resize 10' 1369call assert_equal ( 10 , winheight ( other_winnr )) 1370call assert_equal ( & lines - 10 - 3 , winheight ( 0 )) 1371exe other_winnr .. 'resize +1' 1372exe other_winnr .. 'resize +1' 1373call assert_equal ( 12 , winheight ( other_winnr )) 1374call assert_equal ( & lines - 10 - 3 - 2 , winheight ( 0 )) 1375close 1376 1377vsplit 1378wincmd l 1379let other_winnr = winnr ( 'h' ) 1380call assert_notequal ( winnr (), other_winnr ) 1381exe 'vert ' .. other_winnr .. 'resize -' .. & columns 1382call assert_equal ( 0 , winwidth ( other_winnr )) 1383 1384 %bwipe ! 1385endfunc 1386 1387" Test for adjusting the window width when a window is closed with some 1388" windows using 'winfixwidth' 1389func Test_window_width_adjust () 1390only 1391" Three vertical windows. Windows 1 and 2 have 'winfixwidth' set and close 1392" window 2. 1393wincmd v 1394vert resize 10 1395set winfixwidth 1396wincmd v 1397set winfixwidth 1398wincmd c 1399call assert_inrange ( 10 , 12 , winwidth ( 1 )) 1400" Three vertical windows. Windows 2 and 3 have 'winfixwidth' set and close 1401" window 3. 1402only 1403set winfixwidth 1404wincmd v 1405vert resize 10 1406set winfixwidth 1407wincmd v 1408set nowinfixwidth 1409wincmd b 1410wincmd c 1411call assert_inrange ( 10 , 12 , winwidth ( 2 )) 1412 1413new |only 1414endfunc 1415 1416" Test for jumping to a vertical/horizontal neighbor window based on the 1417" current cursor position 1418func Test_window_goto_neighbor () 1419 %bw ! 1420 1421" Vertical window movement 1422 1423" create the following window layout: 1424" +--+--+ 1425" |w1|w3| 1426" +--+ | 1427" |w2| | 1428" +--+--+ 1429" |w4 | 1430" +-----+ 1431new 1432vsplit 1433split 1434" vertically jump from w4 1435wincmd b 1436call setline ( 1 , repeat ( ' ' , & columns )) 1437call cursor ( 1 , 1 ) 1438wincmd k 1439call assert_equal ( 2 , winnr ()) 1440wincmd b 1441call cursor ( 1 , & columns ) 1442redraw ! 1443wincmd k 1444call assert_equal ( 3 , winnr ()) 1445 %bw ! 1446 1447" create the following window layout: 1448" +--+--+--+ 1449" |w1|w2|w3| 1450" +--+--+--+ 1451" |w4 | 1452" +--------+ 1453new 1454vsplit 1455vsplit 1456wincmd b 1457call setline ( 1 , repeat ( ' ' , & columns )) 1458call cursor ( 1 , 1 ) 1459wincmd k 1460call assert_equal ( 1 , winnr ()) 1461wincmd b 1462call cursor ( 1 , & columns / 2 ) 1463redraw ! 1464wincmd k 1465call assert_equal ( 2 , winnr ()) 1466wincmd b 1467call cursor ( 1 , & columns ) 1468redraw ! 1469wincmd k 1470call assert_equal ( 3 , winnr ()) 1471 %bw ! 1472 1473" Horizontal window movement 1474 1475" create the following window layout: 1476" +--+--+--+ 1477" |w1|w2|w4| 1478" +--+--+ | 1479" |w3 | | 1480" +-----+--+ 1481vsplit 1482split 1483vsplit 14844 wincmd l 1485call setline ( 1 , repeat ([ ' ' ], & lines )) 1486call cursor ( 1 , 1 ) 1487redraw ! 1488wincmd h 1489call assert_equal ( 2 , winnr ()) 14904 wincmd l 1491call cursor ( & lines , 1 ) 1492redraw ! 1493wincmd h 1494call assert_equal ( 3 , winnr ()) 1495 %bw ! 1496 1497" create the following window layout: 1498" +--+--+ 1499" |w1|w4| 1500" +--+ + 1501" |w2| | 1502" +--+ + 1503" |w3| | 1504" +--+--+ 1505vsplit 1506split 1507split 1508wincmd l 1509call setline ( 1 , repeat ([ ' ' ], & lines )) 1510call cursor ( 1 , 1 ) 1511redraw ! 1512wincmd h 1513call assert_equal ( 1 , winnr ()) 1514wincmd l 1515call cursor ( & lines / 2 , 1 ) 1516redraw ! 1517wincmd h 1518call assert_equal ( 2 , winnr ()) 1519wincmd l 1520call cursor ( & lines , 1 ) 1521redraw ! 1522wincmd h 1523call assert_equal ( 3 , winnr ()) 1524 %bw ! 1525endfunc 1526 1527" Test for an autocmd closing the destination window when jumping from one 1528" window to another. 1529func Test_close_dest_window () 1530split 1531edit Xfile 1532 1533" Test for BufLeave 1534augroup T1 1535au ! 1536au BufLeave Xfile $ wincmd c 1537augroup END 1538wincmd b 1539call assert_equal ( 1 , winnr ( '$' )) 1540call assert_equal ( 'Xfile' , @%) 1541augroup T1 1542au ! 1543augroup END 1544 1545" Test for WinLeave 1546new 1547wincmd p 1548augroup T1 1549au ! 1550au WinLeave * 1 wincmd c 1551augroup END 1552wincmd t 1553call assert_equal ( 1 , winnr ( '$' )) 1554call assert_equal ( 'Xfile' , @%) 1555augroup T1 1556au ! 1557augroup END 1558augroup ! T1 1559 %bw ! 1560endfunc 1561 1562func Test_win_move_separator () 1563edit a 1564leftabove vsplit b 1565let w = winwidth ( 0 ) 1566" check win_move_separator from left window on left window 1567call assert_equal ( 1 , winnr ()) 1568for offset in range ( 5 ) 1569call assert_true ( win_move_separator ( 0 , offset )) 1570call assert_equal ( w + offset , winwidth ( 0 )) 1571call assert_true ( 0 ->win_move_separator ( - offset )) 1572call assert_equal ( w , winwidth ( 0 )) 1573endfor 1574" check win_move_separator from right window on left window number 1575wincmd l 1576call assert_notequal ( 1 , winnr ()) 1577for offset in range ( 5 ) 1578call assert_true ( 1 ->win_move_separator ( offset )) 1579call assert_equal ( w + offset , winwidth ( 1 )) 1580call assert_true ( win_move_separator ( 1 , - offset )) 1581call assert_equal ( w , winwidth ( 1 )) 1582endfor 1583" check win_move_separator from right window on left window ID 1584let id = win_getid ( 1 ) 1585for offset in range ( 5 ) 1586call assert_true ( win_move_separator ( id , offset )) 1587call assert_equal ( w + offset , winwidth ( id )) 1588call assert_true ( id ->win_move_separator ( - offset )) 1589call assert_equal ( w , winwidth ( id )) 1590endfor 1591" check win_move_separator from right window on right window is no-op 1592let w0 = winwidth ( 0 ) 1593call assert_true ( win_move_separator ( 0 , 1 )) 1594call assert_equal ( w0 , winwidth ( 0 )) 1595call assert_true ( win_move_separator ( 0 , -1 )) 1596call assert_equal ( w0 , winwidth ( 0 )) 1597 1598" check that win_move_separator doesn't error with offsets beyond moving 1599" possibility 1600call assert_true ( win_move_separator ( id , 5000 )) 1601call assert_true ( winwidth ( id ) > w ) 1602call assert_true ( win_move_separator ( id , -5000 )) 1603call assert_true ( winwidth ( id ) < w ) 1604 1605" check that win_move_separator returns false for an invalid window 1606wincmd = 1607let w = winwidth ( 0 ) 1608call assert_false ( win_move_separator ( -1 , 1 )) 1609call assert_equal ( w , winwidth ( 0 )) 1610 1611" check that win_move_separator returns false for a floating window 1612let id = nvim_open_win ( 1613 \0 , 0 , #{ relative : 'editor' , row : 2 , col : 2 , width : 5 , height : 3 }) 1614let w = winwidth ( id ) 1615call assert_false ( win_move_separator ( id , 1 )) 1616call assert_equal ( w , winwidth ( id )) 1617call nvim_win_close ( id , 1 ) 1618 1619" check that using another tabpage fails without crash 1620let id = win_getid () 1621tabnew 1622call assert_fails ( 'call win_move_separator(id, -1)' , 'E1308:' ) 1623tabclose 1624 1625 %bwipe ! 1626endfunc 1627 1628func Test_win_move_statusline () 1629edit a 1630leftabove split b 1631let h = winheight ( 0 ) 1632" check win_move_statusline from top window on top window 1633call assert_equal ( 1 , winnr ()) 1634for offset in range ( 5 ) 1635call assert_true ( win_move_statusline ( 0 , offset )) 1636call assert_equal ( h + offset , winheight ( 0 )) 1637call assert_true ( 0 ->win_move_statusline ( - offset )) 1638call assert_equal ( h , winheight ( 0 )) 1639endfor 1640" check win_move_statusline from bottom window on top window number 1641wincmd j 1642call assert_notequal ( 1 , winnr ()) 1643for offset in range ( 5 ) 1644call assert_true ( 1 ->win_move_statusline ( offset )) 1645call assert_equal ( h + offset , winheight ( 1 )) 1646call assert_true ( win_move_statusline ( 1 , - offset )) 1647call assert_equal ( h , winheight ( 1 )) 1648endfor 1649" check win_move_statusline from bottom window on bottom window 1650let h0 = winheight ( 0 ) 1651for offset in range ( 5 ) 1652call assert_true ( 0 ->win_move_statusline ( - offset )) 1653call assert_equal ( h0 - offset , winheight ( 0 )) 1654call assert_equal ( 1 + offset , & cmdheight ) 1655call assert_true ( win_move_statusline ( 0 , offset )) 1656call assert_equal ( h0 , winheight ( 0 )) 1657call assert_equal ( 1 , & cmdheight ) 1658endfor 1659" supports cmdheight=0 1660set cmdheight = 0 1661call assert_true ( win_move_statusline ( 0 , 1 )) 1662call assert_equal ( h0 + 1 , winheight ( 0 )) 1663call assert_equal ( 0 , & cmdheight ) 1664set cmdheight & 1665" check win_move_statusline from bottom window on top window ID 1666let id = win_getid ( 1 ) 1667for offset in range ( 5 ) 1668call assert_true ( win_move_statusline ( id , offset )) 1669call assert_equal ( h + offset , winheight ( id )) 1670call assert_true ( id ->win_move_statusline ( - offset )) 1671call assert_equal ( h , winheight ( id )) 1672endfor 1673 1674" check that win_move_statusline doesn't error with offsets beyond moving 1675" possibility 1676call assert_true ( win_move_statusline ( id , 5000 )) 1677call assert_true ( winheight ( id ) > h ) 1678call assert_true ( win_move_statusline ( id , -5000 )) 1679call assert_true ( winheight ( id ) < h ) 1680 1681" check that win_move_statusline returns false for an invalid window 1682wincmd = 1683let h = winheight ( 0 ) 1684call assert_false ( win_move_statusline ( -1 , 1 )) 1685call assert_equal ( h , winheight ( 0 )) 1686 1687" check that win_move_statusline returns false for a floating window 1688let id = nvim_open_win ( 1689 \0 , 0 , #{ relative : 'editor' , row : 2 , col : 2 , width : 5 , height : 3 }) 1690let h = winheight ( id ) 1691call assert_false ( win_move_statusline ( id , 1 )) 1692call assert_equal ( h , winheight ( id )) 1693call nvim_win_close ( id , 1 ) 1694 1695" check that using another tabpage fails without crash 1696let id = win_getid () 1697tabnew 1698call assert_fails ( 'call win_move_statusline(id, -1)' , 'E1308:' ) 1699tabclose 1700 1701 %bwipe ! 1702endfunc 1703 1704" Test for window allocation failure 1705func Test_window_alloc_failure () 1706CheckFunction test_alloc_fail 1707 %bw ! 1708 1709" test for creating a new window above current window 1710call test_alloc_fail ( GetAllocId ( 'newwin_wvars' ), 0 , 0 ) 1711call assert_fails ( 'above new' , 'E342:' ) 1712call assert_equal ( 1 , winnr ( '$' )) 1713 1714" test for creating a new window below current window 1715call test_alloc_fail ( GetAllocId ( 'newwin_wvars' ), 0 , 0 ) 1716call assert_fails ( 'below new' , 'E342:' ) 1717call assert_equal ( 1 , winnr ( '$' )) 1718 1719" test for popup window creation failure 1720call test_alloc_fail ( GetAllocId ( 'newwin_wvars' ), 0 , 0 ) 1721call assert_fails ( 'call popup_create("Hello", {})' , 'E342:' ) 1722call assert_equal ([], popup_list ()) 1723 1724call test_alloc_fail ( GetAllocId ( 'newwin_wvars' ), 0 , 0 ) 1725call assert_fails ( 'split' , 'E342:' ) 1726call assert_equal ( 1 , winnr ( '$' )) 1727 1728edit Xfile1 1729edit Xfile2 1730call test_alloc_fail ( GetAllocId ( 'newwin_wvars' ), 0 , 0 ) 1731call assert_fails ( 'sb Xfile1' , 'E342:' ) 1732call assert_equal ( 1 , winnr ( '$' )) 1733call assert_equal ( 'Xfile2' , @%) 1734 %bw ! 1735 1736" FIXME: The following test crashes Vim 1737" test for new tabpage creation failure 1738" call test_alloc_fail(GetAllocId('newwin_wvars'), 0, 0) 1739" call assert_fails('tabnew', 'E342:') 1740" call assert_equal(1, tabpagenr('$')) 1741" call assert_equal(1, winnr('$')) 1742 1743" This test messes up the internal Vim window/frame information. So the 1744" Test_window_cmd_cmdwin_with_vsp() test fails after running this test. 1745" Open a new tab and close everything else to fix this issue. 1746tabnew 1747tabonly 1748endfunc 1749 1750func Test_win_equal_last_status () 1751let save_lines = & lines 1752set lines = 20 1753set splitbelow 1754set laststatus = 0 1755 1756split |split |quit 1757call assert_equal ( winheight ( 1 ), winheight ( 2 )) 1758 1759let & lines = save_lines 1760set splitbelow & 1761set laststatus & 1762endfunc 1763 1764" Test "screen" and "cursor" values for 'splitkeep' with a sequence of 1765" split operations for various options: with and without a winbar, 1766" tabline, for each possible value of 'laststatus', 'scrolloff', 1767" 'equalalways', and with the cursor at the top, middle and bottom. 1768func Test_splitkeep_options () 1769throw "Skipped: Nvim supports cmdwin freedom #40312" 1770" disallow window resizing 1771" let save_WS = &t_WS 1772" set t_WS= 1773 1774let gui = has ( "gui_running" ) 1775inoremap <expr> c"<cmd>copen<bar>wincmd k<CR>" 1776for run in range ( 0 , 20 ) 1777let & splitkeep = run > 10 ? 'topline' : 'screen' 1778let & scrolloff = ( ! ( run % 4 ) ? 0 : run ) 1779let & laststatus = ( run % 3 ) 1780let & splitbelow = ( run % 3 ) 1781let & equalalways = ( run % 2 ) 1782" Nvim: both windows have a winbar after splitting 1783" let wsb = (run % 2) && &splitbelow 1784let wsb = 0 1785let tl = ( gui ? 0 : (( run % 5 ) ? 1 : 0 )) 1786let pos = ! ( run % 3 ) ? 'H' : (( run % 2 ) ? 'M' : 'L' ) 1787tabnew |tabonly ! |redraw 1788execute ( run % 5 ) ? 'tabnew' : '' 1789" execute (run % 2) ? 'nnoremenu 1.10 WinBar.Test :echo' : '' 1790let & winbar = ( run % 2 ) ? '%f' : '' 1791call setline ( 1 , range ( 1 , 256 )) 1792" No scroll for restore_snapshot 1793norm G 1794try 1795copen |close |colder 1796catch /E380/ 1797endtry 1798call assert_equal ( 257 - winheight ( 0 ), line ( "w0" )) 1799 1800" No scroll for firstwin horizontal split 1801execute 'norm gg' . pos 1802split |redraw |wincmd k 1803call assert_equal ( 1 , line ( "w0" )) 1804call assert_equal ( & scroll , winheight ( 0 ) / 2 ) 1805wincmd j 1806call assert_equal ( & spk == 'topline' ? 1 : win_screenpos ( 0 )[ 0 ] - tl - wsb , line ( "w0" )) 1807 1808" No scroll when resizing windows 1809wincmd k |resize +2 |redraw 1810call assert_equal ( 1 , line ( "w0" )) 1811wincmd j 1812call assert_equal ( & spk == 'topline' ? 1 : win_screenpos ( 0 )[ 0 ] - tl - wsb , line ( "w0" )) 1813 1814" No scroll when dragging statusline 1815call win_move_statusline ( 1 , -3 ) 1816call assert_equal ( & spk == 'topline' ? 1 : win_screenpos ( 0 )[ 0 ] - tl - wsb , line ( "w0" )) 1817wincmd k 1818call assert_equal ( 1 , line ( "w0" )) 1819 1820" No scroll when changing shellsize 1821set lines += 2 1822call assert_equal ( 1 , line ( "w0" )) 1823wincmd j 1824call assert_equal ( & spk == 'topline' ? 1 : win_screenpos ( 0 )[ 0 ] - tl - wsb , line ( "w0" )) 1825set lines -= 2 1826call assert_equal ( & spk == 'topline' ? 1 : win_screenpos ( 0 )[ 0 ] - tl - wsb , line ( "w0" )) 1827wincmd k 1828call assert_equal ( 1 , line ( "w0" )) 1829 1830" No scroll when equalizing windows 1831wincmd = 1832call assert_equal ( 1 , line ( "w0" )) 1833wincmd j 1834call assert_equal ( & spk == 'topline' ? 1 : win_screenpos ( 0 )[ 0 ] - tl - wsb , line ( "w0" )) 1835wincmd k 1836call assert_equal ( 1 , line ( "w0" )) 1837 1838" No scroll in windows split multiple times 1839vsplit |split |4 wincmd w 1840call assert_equal ( & spk == 'topline' ? 1 : win_screenpos ( 0 )[ 0 ] - tl - wsb , line ( "w0" )) 18411 wincmd w |quit |wincmd l |split 1842call assert_equal ( & spk == 'topline' ? 1 : win_screenpos ( 0 )[ 0 ] - tl - wsb , line ( "w0" )) 1843wincmd j 1844call assert_equal ( & spk == 'topline' ? 1 : win_screenpos ( 0 )[ 0 ] - tl - wsb , line ( "w0" )) 1845 1846" No scroll in small window 18472 wincmd w |only |5 split |wincmd k 1848call assert_equal ( 1 , line ( "w0" )) 1849wincmd j 1850call assert_equal ( & spk == 'topline' ? 1 : win_screenpos ( 0 )[ 0 ] - tl - wsb , line ( "w0" )) 1851 1852" No scroll for vertical split 1853quit |vsplit |wincmd l 1854call assert_equal ( 1 , line ( "w0" )) 1855wincmd h 1856call assert_equal ( 1 , line ( "w0" )) 1857 1858" No scroll in windows split and quit multiple times 1859quit |redraw |split |split |quit |redraw 1860call assert_equal ( & spk == 'topline' ? 1 : win_screenpos ( 0 )[ 0 ] - tl - wsb , line ( "w0" )) 1861 1862" No scroll for new buffer 18631 wincmd w |only |copen |wincmd k 1864call assert_equal ( 1 , line ( "w0" )) 1865only 1866call assert_equal ( 1 , line ( "w0" )) 1867above copen |wincmd j 1868call assert_equal ( & spk == 'topline' ? 1 : win_screenpos ( 0 )[ 0 ] - tl , line ( "w0" )) 1869 1870" No scroll when opening cmdwin, and no cursor move when closing cmdwin. 1871only |norm ggL 1872let curpos = getcurpos () 1873norm q: 1874call assert_equal ( 1 , line ( "w0" )) 1875call assert_equal ( curpos , getcurpos ()) 1876 1877" Scroll when cursor becomes invalid in insert mode. 1878norm Lic 1879call assert_equal ( curpos , getcurpos (), 'run ' .. run ) 1880 1881" No scroll when topline not equal to 1 1882only |execute "norm gg5\<C-e>" |split |wincmd k 1883call assert_equal ( 6 , line ( "w0" )) 1884wincmd j 1885call assert_equal ( & spk == 'topline' ? 6 : 5 + win_screenpos ( 0 )[ 0 ] - tl - wsb , line ( "w0" )) 1886endfor 1887 1888tabnew |tabonly ! | %bwipeout ! 1889iunmap c 1890set scrolloff & 1891set splitbelow & 1892set laststatus & 1893set equalalways & 1894set splitkeep & 1895" let &t_WS = save_WS 1896endfunc 1897 1898func Test_splitkeep_cmdwin_cursor_position () 1899throw 'Skipped: Nvim supports cmdwin freedom #40312' 1900set splitkeep = screen 1901call setline ( 1 , range ( & lines )) 1902 1903" No scroll when cursor is at near bottom of window and cursor position 1904" recompution (done by line('w0') in this test) happens while in cmdwin. 1905normal ! G 1906let firstline = line ( 'w0' ) 1907autocmd CmdwinEnter * ++onceautocmd WinEnter * ++oncecall line ( 'w0' ) 1908execute "normal! q:\<C-w>q" 1909redraw ! 1910call assert_equal ( firstline , line ( 'w0' )) 1911 1912" User script can change cursor position successfully while in cmdwin and it 1913" shouldn't be changed when closing cmdwin. 1914execute "normal! Gq:\<Cmd>call win_execute(winnr('#')->win_getid(), 'call cursor(1, 1)')\<CR>\<C-w>q" 1915call assert_equal ( 1 , line ( '.' )) 1916call assert_equal ( 1 , col ( '.' )) 1917 1918execute "normal! Gq:\<Cmd>autocmd WinEnter * ++once call cursor(1, 1)\<CR>\<C-w>q" 1919call assert_equal ( 1 , line ( '.' )) 1920call assert_equal ( 1 , col ( '.' )) 1921 1922 %bwipeout ! 1923set splitkeep & 1924endfunc 1925 1926func Test_splitkeep_misc () 1927set splitkeep = screen 1928 1929call setline ( 1 , range ( 1 , & lines )) 1930" Cursor is adjusted to start and end of buffer 1931norm M 1932wincmd s 1933resize 1 1934call assert_equal ( 1 , line ( '.' )) 1935wincmd j 1936norm GM 1937resize 1 1938call assert_equal ( & lines , line ( '.' )) 1939only ! 1940 1941set splitbelow 1942norm Gzz 1943let top = line ( 'w0' ) 1944" No scroll when aucmd_win is opened 1945call setbufvar ( bufnr ( "test" , 1 ) , '&buftype' , 'nofile' ) 1946call assert_equal ( top , line ( 'w0' )) 1947" No scroll when tab is changed/closed 1948tab help |close 1949call assert_equal ( top , line ( 'w0' )) 1950" No scroll when help is closed and buffer line count < window height 1951norm ggdG 1952call setline ( 1 , range ( 1 , & lines - 10 )) 1953norm G 1954let top = line ( 'w0' ) 1955help |quit 1956call assert_equal ( top , line ( 'w0' )) 1957" No error when resizing window in autocmd and buffer length changed 1958autocmd FileType qf exe "resize" line ( '$' ) 1959cexpr getline(1,'$' ) 1960copen 1961wincmd p 1962norm dd 1963cexpr getline(1,'$' ) 1964 1965 %bwipeout ! 1966set splitbelow & 1967set splitkeep & 1968endfunc 1969 1970func Test_splitkeep_screen_cursor_pos () 1971new 1972set splitkeep = screen 1973call setline ( 1 , [ "longer than the last" , "shorter" ]) 1974norm ! $ 1975wincmd s 1976close 1977call assert_equal ([ 0 , 1 , 20 , 0 ], getpos ( '.' )) 1978 %bwipeout ! 1979set splitkeep & 1980endfunc 1981 1982func Test_splitkeep_phantom_jump () 1983set splitbelow 1984call setline ( 1 , range ( 1 , 100 )) 1985split 1986 1987" Move cursor to last visible line so it is at risk of being pushed off 1988normal ! L 1989let old = getcurpos () 1990 1991" No jumps, just 3 header lines 1992call assert_equal ( 3 , execute ( 'jumps' ) ->split ( '\n' ) ->len ()) 1993 1994" Switching to "screen" was causing vim to not update relevant variables 1995set splitkeep = screen 1996 1997" Reduce window 2's space, and so bump up the cursor 1998set cmdheight = 2 1999 2000" Cursor has moved up a single line in the buffer, respecting splitkeep 2001call assert_equal ( 4 , execute ( 'jumps' ) ->split ( '\n' ) ->len ()) 2002 2003let old [ 1 ] = old [ 1 ] - 1 2004call assert_equal ( old , getcurpos ()) 2005 2006 %bwipeout ! 2007set splitbelow & splitkeep & cmdheight & 2008endfunc 2009 2010func Test_splitkeep_cmdheight () 2011set splitkeep = screen 2012call setline ( 1 , range ( & lines )) 2013norm ! G 2014set cmdheight = 2 2015call assert_equal ( & lines - 1 , line ( '.' )) 2016 %bwipeout ! 2017set splitkeep & cmdheight & 2018endfunc 2019 2020func Test_aucmd_win_scroll_multibyte () 2021" Using the autocommand window must not scroll the current window when the 2022" cursor is behind multi-byte characters. 2023set splitkeep = cursor 2024" Use a window with a fixed size, the size of the screen may change while 2025" the test is running. 2026call NewWindow ( 11 , 40 ) 2027call setline ( 1 , repeat ([ repeat ( nr2char ( 0x3042 ), 100 )], 20 )) 2028normal ! G050l 2029redraw 2030let topline = line ( 'w0' ) 2031 2032for i in range ( 3 ) 2033call bufload ( bufadd ( '' )) 2034endfor 2035call assert_equal ( topline , line ( 'w0' )) 2036 2037 %bwipeout ! 2038only ! 2039set splitkeep & 2040endfunc 2041 2042func Test_splitkeep_cursor () 2043CheckScreendump 2044let lines =<< trim END 2045set splitkeep=screen 2046autocmd CursorMoved * wincmd p | wincmd p 2047call setline(1, range(1, 200)) 2048func CursorEqualize() 2049call cursor(100, 1) 2050wincmd = 2051endfunc 2052wincmd s 2053call CursorEqualize() 2054END 2055call writefile ( lines , 'XTestSplitkeepCallback' , 'D' ) 2056let buf = RunVimInTerminal ( '-S XTestSplitkeepCallback' , #{ rows : 8 }) 2057 2058call VerifyScreenDump ( buf , 'Test_splitkeep_cursor_1' , {}) 2059 2060call term_sendkeys ( buf , "j" ) 2061call VerifyScreenDump ( buf , 'Test_splitkeep_cursor_2' , {}) 2062 2063call term_sendkeys ( buf , ":set scrolloff=0\<CR>G" ) 2064call VerifyScreenDump ( buf , 'Test_splitkeep_cursor_3' , {}) 2065 2066call StopVimInTerminal ( buf ) 2067endfunc 2068 2069func Test_splitkeep_callback () 2070CheckScreendump 2071let lines =<< trim END 2072set splitkeep=screen 2073call setline(1, range(&lines)) 2074function C1(a, b) 2075split | wincmd p 2076endfunction 2077function C2(a, b) 2078close | split 2079endfunction 2080nn j <cmd>call job_start([&sh, &shcf, "true"], { 'exit_cb': 'C1' })<CR> 2081nn t <cmd>call popup_create(term_start([&sh, &shcf, "true"], 2082\ { 'hidden': 1, 'exit_cb': 'C2' }), {})<CR> 2083END 2084call writefile ( lines , 'XTestSplitkeepCallback' , 'D' ) 2085let buf = RunVimInTerminal ( '-S XTestSplitkeepCallback' , #{ rows : 8 }) 2086 2087call term_sendkeys ( buf , "j" ) 2088call VerifyScreenDump ( buf , 'Test_splitkeep_callback_1' , {}) 2089 2090call term_sendkeys ( buf , ":quit\<CR>Ht" ) 2091call VerifyScreenDump ( buf , 'Test_splitkeep_callback_2' , {}) 2092 2093call term_sendkeys ( buf , ":set sb\<CR>:quit\<CR>Gj" ) 2094call VerifyScreenDump ( buf , 'Test_splitkeep_callback_3' , {}) 2095 2096call term_sendkeys ( buf , ":quit\<CR>Gt" ) 2097call VerifyScreenDump ( buf , 'Test_splitkeep_callback_4' , {}) 2098 2099call StopVimInTerminal ( buf ) 2100endfunc 2101 2102func Test_splitkeep_fold () 2103CheckScreendump 2104 2105let lines =<< trim END 2106set splitkeep=screen 2107set foldmethod=marker 2108set number 2109let line = 1 2110for n in range(1, &lines) 2111call setline(line, ['int FuncName() {/*{{{*/', 1, 2, 3, 4, 5, '}/*}}}*/', 2112\ 'after fold']) 2113let line += 8 2114endfor 2115END 2116call writefile ( lines , 'XTestSplitkeepFold' , 'D' ) 2117let buf = RunVimInTerminal ( '-S XTestSplitkeepFold' , #{ rows : 10 }) 2118 2119call term_sendkeys ( buf , "L:wincmd s\<CR>" ) 2120call VerifyScreenDump ( buf , 'Test_splitkeep_fold_1' , {}) 2121 2122call term_sendkeys ( buf , ":quit\<CR>" ) 2123call VerifyScreenDump ( buf , 'Test_splitkeep_fold_2' , {}) 2124 2125call term_sendkeys ( buf , "H:below split\<CR>" ) 2126call VerifyScreenDump ( buf , 'Test_splitkeep_fold_3' , {}) 2127 2128call term_sendkeys ( buf , ":wincmd k\<CR>:quit\<CR>" ) 2129call VerifyScreenDump ( buf , 'Test_splitkeep_fold_4' , {}) 2130 2131call StopVimInTerminal ( buf ) 2132endfunc 2133 2134func Test_splitkeep_status () 2135CheckScreendump 2136 2137let lines =<< trim END 2138call setline(1, ['a', 'b', 'c']) 2139set nomodified 2140set splitkeep=screen 2141let win = winnr() 2142wincmd s 2143wincmd j 2144END 2145call writefile ( lines , 'XTestSplitkeepStatus' , 'D' ) 2146let buf = RunVimInTerminal ( '-S XTestSplitkeepStatus' , #{ rows : 10 }) 2147 2148call term_sendkeys ( buf , ":call win_move_statusline(win, 1)\<CR>" ) 2149call VerifyScreenDump ( buf , 'Test_splitkeep_status_1' , {}) 2150 2151call StopVimInTerminal ( buf ) 2152endfunc 2153 2154" skipcol is not reset unnecessarily and is copied to new window 2155func Test_splitkeep_skipcol () 2156CheckScreendump 2157 2158let lines =<< trim END 2159set splitkeep=topline smoothscroll splitbelow scrolloff=0 2160call setline(1, 'with lots of text in one line '->repeat(6)) 2161norm 2 2162wincmd s 2163END 2164 2165call writefile ( lines , 'XTestSplitkeepSkipcol' , 'D' ) 2166let buf = RunVimInTerminal ( '-S XTestSplitkeepSkipcol' , #{ rows : 12 , cols : 40 }) 2167 2168call VerifyScreenDump ( buf , 'Test_splitkeep_skipcol_1' , {}) 2169call StopVimInTerminal ( buf ) 2170endfunc 2171 2172func Test_splitkeep_line () 2173CheckScreendump 2174 2175let lines =<< trim END 2176set splitkeep=screen nosplitbelow 2177autocmd WinResized * call line('w0', 1000) 2178call setline(1, range(1000)) 2179END 2180 2181call writefile ( lines , 'XTestSplitkeepSkipcol' , 'D' ) 2182let buf = RunVimInTerminal ( '-S XTestSplitkeepSkipcol' , #{ rows : 6 , cols : 40 }) 2183 2184call VerifyScreenDump ( buf , 'Test_splitkeep_line_1' , {}) 2185 2186call term_sendkeys ( buf , ":wincmd s\<CR>" ) 2187call VerifyScreenDump ( buf , 'Test_splitkeep_line_2' , {}) 2188call StopVimInTerminal ( buf ) 2189endfunc 2190 2191func Test_new_help_window_on_error () 2192help change.txt 2193execute "normal! /CTRL-@\<CR>" 2194silent ! execute "normal! \<C-W>]" 2195 2196let wincount = winnr ( '$' ) 2197help 'mod' 2198 2199call assert_equal ( wincount , winnr ( '$' )) 2200call assert_equal ( expand ( "<cword>" ), "'mod'" ) 2201endfunc 2202 2203func Test_splitmove_flatten_frame () 2204split 2205vsplit 2206 2207wincmd L 2208let layout = winlayout () 2209wincmd K 2210wincmd L 2211call assert_equal ( winlayout (), layout ) 2212 2213only ! 2214endfunc 2215 2216func Test_autocmd_window_force_room () 2217" Open as many windows as possible 2218while v: true 2219try 2220split 2221catch /E36:/ 2222break 2223endtry 2224endwhile 2225while v: true 2226try 2227vsplit 2228catch /E36:/ 2229break 2230endtry 2231endwhile 2232 2233wincmd j 2234vsplit 2235call assert_fails ( 'wincmd H' , 'E36:' ) 2236call assert_fails ( 'wincmd J' , 'E36:' ) 2237call assert_fails ( 'wincmd K' , 'E36:' ) 2238call assert_fails ( 'wincmd L' , 'E36:' ) 2239 2240edit unload me 2241enew 2242bunload ! unload\ me 2243augroup AucmdWinForceRoom 2244au ! 2245au BufEnter * ++oncelet s: triggered = v: true 2246 \|call assert_equal ( 'autocmd' , win_gettype ()) 2247augroup END 2248let info = s: win_layout_info () 2249" bufload opening the autocommand window shouldn't give E36. 2250call bufload ( 'unload me' ) 2251call assert_equal ( v: true , s: triggered ) 2252call assert_equal ( info , s: win_layout_info ()) 2253 2254unlet ! s: triggered 2255au ! AucmdWinForceRoom 2256augroup ! AucmdWinForceRoom 2257 %bw ! 2258endfunc 2259 2260func Test_win_gotoid_splitmove_textlock_cmdwin () 2261throw 'Skipped: Nvim supports cmdwin freedom #40312' 2262call setline ( 1 , 'foo' ) 2263new 2264let curwin = win_getid () 2265call setline ( 1 , 'bar' ) 2266 2267set debug += throwindentexpr = win _gotoid(win_getid(winnr('#'))) 2268call assert_fails ( 'normal! ==' , 'E565:' ) 2269call assert_equal ( curwin , win_getid ()) 2270" No error if attempting to switch to curwin; nothing happens. 2271set indentexpr = assert _equal(1,win_gotoid(win_getid())) 2272normal ! == 2273call assert_equal ( curwin , win_getid ()) 2274 2275set indentexpr = win _splitmove(winnr('#'),winnr()) 2276call assert_fails ( 'normal! ==' , 'E565:' ) 2277call assert_equal ( curwin , win_getid ()) 2278 2279 %bw ! 2280set debug -= throwindentexpr & 2281 2282call feedkeys ( 'q:' 2283 \.. ":call assert_fails('call win_splitmove(winnr(''#''), winnr())', 'E11:')\<CR>" 2284 \.. ":call assert_equal('command', win_gettype())\<CR>" 2285 \.. ":call assert_equal('', win_gettype(winnr('#')))\<CR>" , 'ntx' ) 2286 2287call feedkeys ( 'q:' 2288 \.. ":call assert_fails('call win_gotoid(win_getid(winnr(''#'')))', 'E11:')\<CR>" 2289"\ No error if attempting to switch to curwin; nothing happens. 2290 \.. ":call assert_equal(1, win_gotoid(win_getid()))\<CR>" 2291 \.. ":call assert_equal('command', win_gettype())\<CR>" 2292 \.. ":call assert_equal('', win_gettype(winnr('#')))\<CR>" , 'ntx' ) 2293endfunc 2294 2295func Test_winfixsize_positions () 2296" Check positions are correct when closing a window in a non-current tabpage 2297" causes non-adjacent window to fill the space due to 'winfix{width,height}'. 2298tabnew 2299vsplit 2300wincmd | 2301split 2302set winfixheight 2303split foo 2304tabfirst 2305 2306bwipe ! foo 2307" Save actual values before entering the tabpage. 2308let info = s: win_layout_info ( 2 ) 2309tabnext 2310" Compare it with the expected value (after win_comp_pos) from entering. 2311call assert_equal ( s: win_layout_info (), info ) 2312 2313$ tabnew 2314split 2315split 2316wincmd k 2317belowright vsplit 2318set winfixwidth 2319belowright vsplit foo 2320tabprevious 2321 2322bwipe ! foo 2323" Save actual values before entering the tabpage. 2324let info = s: win_layout_info ( 3 ) 2325tabnext 2326" Compare it with the expected value (after win_comp_pos) from entering. 2327call assert_equal ( s: win_layout_info (), info ) 2328 2329" Check positions unchanged when failing to move a window, if 'winfix{width, 2330" height}' would otherwise cause a non-adjacent window to fill the space. 2331 %bwipe 2332call assert_fails ( 'execute "split|"->repeat(&lines)' , 'E36:' ) 2333wincmd p 2334vsplit 2335set winfixwidth 2336vsplit 2337set winfixwidth 2338vsplit 2339vsplit 2340set winfixwidth 2341wincmd p 2342 2343let info = s: win_layout_info () 2344call assert_fails ( 'wincmd J' , 'E36:' ) 2345call assert_equal ( info , s: win_layout_info ()) 2346 2347only 2348call assert_fails ( 'execute "vsplit|"->repeat(&columns)' , 'E36:' ) 2349belowright split 2350set winfixheight 2351belowright split 2352 2353let info = s: win_layout_info () 2354call assert_fails ( 'wincmd H' , 'E36:' ) 2355call assert_equal ( info , s: win_layout_info ()) 2356 2357 %bwipe 2358endfunc 2359 2360func Test_winfixsize_vsep_statusline () 2361CheckScreendump 2362 2363let lines =<< trim END 2364set noequalalways splitbelow splitright 2365vsplit 2366setlocal winfixwidth 2367vsplit 2368func SetupWfh() 2369set laststatus=0 2370only 2371split 2372set winfixheight 2373split 2374endfunc 2375END 2376call writefile ( lines , 'XTestWinfixsizeVsepStatusline' , 'D' ) 2377let buf = RunVimInTerminal ( '-S XTestWinfixsizeVsepStatusline' , #{ rows : 8 }) 2378 2379call term_sendkeys ( buf , ":echo winwidth(1) winwidth(2) winwidth(3)\n" ) 2380call WaitForAssert ({ ->assert_match ( '^16 37 20\>' , term_getline ( buf , 8 ))}) 2381 2382call term_sendkeys ( buf , ":quit\n" ) 2383call VerifyScreenDump ( buf , 'Test_winfixsize_vsep_statusline_1' , {}) 2384 2385" Reported widths should be consistent with the screen dump. 2386call term_sendkeys ( buf , ":echo winwidth(1) winwidth(2)\n" ) 2387" (May be better if 'wfw' window remains at 37 columns, but the resize is 2388" consistent with how things currently work for 'winfix*' windows) 2389call WaitForAssert ({ ->assert_match ( '^36 38\>' , term_getline ( buf , 8 ))}) 2390 2391" For good measure, also check bottom-most 'winfixheight' windows don't leave 2392" stray statuslines with &laststatus=0. 2393call term_sendkeys ( buf , 2394 \":call SetupWfh() | echo winheight(1) winheight(2) winheight(3)\n" ) 2395call WaitForAssert ({ ->assert_match ( '^1 3 1\>' , term_getline ( buf , 8 ))}) 2396 2397call term_sendkeys ( buf , ":quit\n" ) 2398call VerifyScreenDump ( buf , 'Test_winfixsize_vsep_statusline_2' , {}) 2399 2400call term_sendkeys ( buf , ":echo winheight(1) winheight(2)\n" ) 2401" (Likewise, may be better if 'wfh' window remains at 3 rows) 2402call WaitForAssert ({ ->assert_match ( '^2 4\>' , term_getline ( buf , 8 ))}) 2403 2404call StopVimInTerminal ( buf ) 2405endfunc 2406 2407func Test_resize_from_another_tabpage () 2408CheckScreendump 2409 2410let lines =<< trim END 2411set laststatus=2 2412vnew 2413let w = win_getid() 2414tabnew 2415call win_execute(w, 'vertical resize 20') 2416tabprev 2417END 2418call writefile ( lines , 'XTestResizeFromAnotherTabpage' , 'D' ) 2419let buf = RunVimInTerminal ( '-S XTestResizeFromAnotherTabpage' , #{ rows : 8 }) 2420call VerifyScreenDump ( buf , 'Test_resize_from_another_tabpage_1' , {}) 2421 2422call StopVimInTerminal ( buf ) 2423endfunc 2424 2425" When shrinking a window and the only other window has 'winfixheight' 2426" with 'winminheight'=0, freed rows must go to the wfh window. 2427func Test_winfixheight_resize_wmh_zero () 2428set winminheight = 0 laststatus = 0 2429 2430let id1 = win_getid () 2431copen 2432let id2 = win_getid () 2433wincmd w 2434wincmd _ 2435let wi1 = getwininfo ( id1 )[ 0 ] 2436let wi2 = getwininfo ( id2 )[ 0 ] 2437let exp = & lines - wi1 . status_height - wi2 . height - wi2 . status_height - & ch 2438call assert_equal ([ exp , 1 ], [ wi1 . height , wi1 . status_height ]) 2439call assert_equal ([ 0 , 0 ], [ wi2 . height , wi2 . status_height ]) 2440 2441wincmd w" enter qf (height 0 -> 1) 2442let wi1 = getwininfo ( id1 )[ 0 ] 2443let wi2 = getwininfo ( id2 )[ 0 ] 2444let exp = & lines - wi1 . status_height - wi2 . height - wi2 . status_height - & ch 2445call assert_equal ( exp , wi1 . height ) 2446call assert_equal ( 1 , wi2 . height ) 2447 2448wincmd w 2449" Freed rows must go to qf (wfh, only other window). 2450" Before the fix, the rows were lost (fr_height sum < topframe fr_height), 2451" resulting in wrong window heights in subsequent operations. 24521 wincmd _ 2453let wi1 = getwininfo ( id1 )[ 0 ] 2454let wi2 = getwininfo ( id2 )[ 0 ] 2455call assert_equal ( 1 , wi1 . height ) 2456let exp = & lines - wi1 . height - wi1 . status_height - & ch 2457call assert_equal ( exp , wi2 . height ) 2458 2459wincmd w 246099 wincmd + 2461let wi1 = getwininfo ( id1 )[ 0 ] 2462let wi2 = getwininfo ( id2 )[ 0 ] 2463call assert_equal ( 0 , wi1 . height ) 2464let exp = & lines - wi1 . height - wi1 . status_height - & ch 2465call assert_equal ( exp , wi2 . height ) 2466 246799 wincmd - 2468let wi1 = getwininfo ( id1 )[ 0 ] 2469let wi2 = getwininfo ( id2 )[ 0 ] 2470let exp = & lines - wi1 . status_height - wi2 . height - & ch 2471call assert_equal ( exp , wi1 . height ) 2472call assert_equal ( 1 , wi2 . height ) 2473 2474" The original bug caused a crash here, but could not be reproduced in the 2475" test. Kept as-is, though it has no particular significance. 2476wincmd w 2477call feedkeys ( "999i\<CR>\<Esc>" , 'tx' ) 2478call feedkeys ( "ggMi" .. repeat ( "\<CR>" , 99 ) .. "\<Esc>" , 'tx' ) 2479let wi1 = getwininfo ( id1 )[ 0 ] 2480let wi2 = getwininfo ( id2 )[ 0 ] 2481let exp = & lines - wi1 . status_height - wi2 . height - & ch 2482call assert_equal ( exp , wi1 . height ) 2483call assert_equal ( 1 , wi2 . height ) 2484 2485cclose 2486set winminheight & laststatus & 2487endfunc 2488 2489" Splitting the only window while it has 'winfixheight' set and 'laststatus' is 2490" one must not leave a screen line unused. 2491func Test_winfixheight_split_only_window () 2492set laststatus = 1 2493new 2494only ! 2495setlocal winfixheight 2496split 2497" Two windows, both with a status line, and the command line. 2498call assert_equal ( & lines - & cmdheight - 2 , winheight ( 1 ) + winheight ( 2 )) 2499 2500only ! 2501setlocal winfixheight & 2502set laststatus & 2503bwipe ! 2504endfunc 2505 2506func Test_window_w_locked_bypass () 2507split Xfoo 2508let s: win = win_getid () 2509 2510augroup TestBypass 2511" :quit fired this with w_locked set. Shouldn't be able to unset w_locked 2512" and close s:win if we do other stuff that also sets it. 2513au WinLeave * ++oncecall assert_equal ( s: win , win_getid ()) 2514 \|quit |call assert_notequal ( 0 , win_id2win ( s: win )) 2515 \|args Xbar 2516 \|argadd Xbaz 2517 \|edit Xbaz-but-cooler 2518 \|quit |call assert_notequal ( 0 , win_id2win ( s: win )) 2519augroup END 2520quit 2521call assert_equal ( 1 , bufexists ( 'Xbaz-but-cooler' )) " check WinLeave ran 2522 2523unlet ! s: win 2524augroup TestBypass 2525au ! 2526augroup END 2527 %argd ! 2528 %bw ! 2529endfunc 2530 2531" vim: shiftwidth=2 sts=2 expandtab