robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
aa1d8a2702
master
1local t =require ( 'test.testutil' ) 2local n =require ( 'test.functional.testnvim' )() 3local Screen =require ( 'test.functional.ui.screen' ) 4local tt =require ( 'test.functional.testterm' ) 5 6local describe , it , before_each , after_each , finally = 7t . describe , t . it , t . before_each , t . after_each , t . finally 8local clear , curbuf , curbuf_contents , curwin , eq , neq , matches , ok , feed , insert , eval = 9n . clear , 10n . api . nvim_get_current_buf , 11n . curbuf_contents , 12n . api . nvim_get_current_win , 13t . eq , 14t . neq , 15t . matches , 16t . ok , 17n . feed , 18n . insert , 19n . eval 20local poke_eventloop =n . poke_eventloop 21local exec =n . exec 22local exec_lua =n . exec_lua 23local fn =n . fn 24local request =n . request 25local NIL =vim . NIL 26local api =n . api 27local command =n . command 28local pcall_err =t . pcall_err 29local assert_alive =n . assert_alive 30 31describe ( 'API/win' , function () 32before_each ( clear ) 33 34describe ( 'get_buf' , function () 35it ( 'works' , function () 36eq ( curbuf (), api . nvim_win_get_buf ( api . nvim_list_wins ()[ 1 ])) 37command ( 'new' ) 38api . nvim_set_current_win ( api . nvim_list_wins ()[ 2 ]) 39eq ( curbuf (), api . nvim_win_get_buf ( api . nvim_list_wins ()[ 2 ])) 40neq ( 41api . nvim_win_get_buf ( api . nvim_list_wins ()[ 1 ]), 42api . nvim_win_get_buf ( api . nvim_list_wins ()[ 2 ]) 43) 44end ) 45end ) 46 47describe ( 'set_buf' , function () 48it ( 'works' , function () 49command ( 'new' ) 50local windows =api . nvim_list_wins () 51neq ( api . nvim_win_get_buf ( windows [ 1 ]), api . nvim_win_get_buf ( windows [ 2 ])) 52api . nvim_win_set_buf ( windows [ 2 ], api . nvim_win_get_buf ( windows [ 1 ])) 53eq ( api . nvim_win_get_buf ( windows [ 1 ]), api . nvim_win_get_buf ( windows [ 2 ])) 54end ) 55 56it ( 'validates args' , function () 57eq ( 'Invalid buffer id: 23' , pcall_err ( api . nvim_win_set_buf , api . nvim_get_current_win (), 23 )) 58eq ( 'Invalid window id: 23' , pcall_err ( api . nvim_win_set_buf , 23 , api . nvim_get_current_buf ())) 59-- When both are invalid the window is reported, so the message allocated 60-- for it is not overwritten and leaked. 61eq ( 'Invalid window id: 23' , pcall_err ( api . nvim_win_set_buf , 23 , 24 )) 62end ) 63 64it ( 'in cmdwin #40312' , function () 65local new_buf =api . nvim_create_buf ( true , true ) 66local old_win =api . nvim_get_current_win () 67local new_win =api . nvim_open_win ( new_buf , false , { 68relative ='editor' , 69row =10 , 70col =10 , 71width =50 , 72height =10 , 73} ) 74feed ( 'q:' ) 75n . poke_eventloop () 76-- Replacing the cmdwin's own buffer is allowed. 77api . nvim_win_set_buf ( 0 , new_buf ) 78-- Also other windows can be touched freely. 79local next_buf =api . nvim_create_buf ( true , true ) 80api . nvim_win_set_buf ( new_win , next_buf ) 81eq ( next_buf , api . nvim_win_get_buf ( new_win )) 82-- Doesn't matter that old_win was the caller; no cmdwin guard. 83api . nvim_win_set_buf ( old_win , new_buf ) 84eq ( new_buf , api . nvim_win_get_buf ( old_win )) 85end ) 86 87describe ( "with 'autochdir'" , function () 88local topdir 89local otherbuf 90local oldwin 91local newwin 92 93before_each ( function () 94command ( 'set shellslash' ) 95topdir =fn . getcwd () 96t . mkdir ( topdir .. '/Xacd' ) 97t . mkdir ( topdir .. '/Xacd/foo' ) 98otherbuf =api . nvim_create_buf ( false , true ) 99api . nvim_buf_set_name ( otherbuf , topdir .. '/Xacd/baz.txt' ) 100 101command ( 'set autochdir' ) 102command ( 'edit Xacd/foo/bar.txt' ) 103eq ( topdir .. '/Xacd/foo' , fn . getcwd ()) 104 105oldwin =api . nvim_get_current_win () 106command ( 'vsplit' ) 107newwin =api . nvim_get_current_win () 108end ) 109 110after_each ( function () 111n . rmdir ( topdir .. '/Xacd' ) 112end ) 113 114it ( 'does not change cwd with non-current window' , function () 115api . nvim_win_set_buf ( oldwin , otherbuf ) 116eq ( topdir .. '/Xacd/foo' , fn . getcwd ()) 117end ) 118 119it ( 'changes cwd with current window' , function () 120api . nvim_win_set_buf ( newwin , otherbuf ) 121eq ( topdir .. '/Xacd' , fn . getcwd ()) 122end ) 123end ) 124end ) 125 126describe ( '{get,set}_cursor' , function () 127it ( 'works' , function () 128eq ( { 1 , 0 } , api . nvim_win_get_cursor ( 0 )) 129command ( 'normal ityping\027o some text' ) 130eq ( 'typing\n some text' , curbuf_contents ()) 131eq ( { 2 , 10 } , api . nvim_win_get_cursor ( 0 )) 132api . nvim_win_set_cursor ( 0 , { 2 , 6 } ) 133command ( 'normal i dumb' ) 134eq ( 'typing\n some dumb text' , curbuf_contents ()) 135end ) 136 137it ( 'no memory leak when using invalid window ID with invalid pos' , function () 138eq ( 'Invalid window id: 1' , pcall_err ( api . nvim_win_set_cursor , 1 , { 'b\na' } )) 139end ) 140 141it ( 'updates the screen, and also when the window is unfocused' , function () 142local screen =Screen . new ( 30 , 9 ) 143 144insert ( 'prologue' ) 145feed ( '100o<esc>' ) 146insert ( 'epilogue' ) 147local win =curwin () 148feed ( 'gg' ) 149 150local s1 =[[ 151^prologue | 152|*8 153]] 154screen : expect ( s1 ) 155-- cursor position is at beginning 156eq ( { 1 , 0 } , api . nvim_win_get_cursor ( win )) 157 158-- move cursor to end 159api . nvim_win_set_cursor ( win , { 101 , 0 } ) 160screen : expect ( [[ 161|*7 162^epilogue | 163| 164]] ) 165 166-- move cursor to the beginning again 167api . nvim_win_set_cursor ( win , { 1 , 0 } ) 168screen : expect ( s1 ) 169 170-- move focus to new window 171command ( 'new' ) 172neq ( win , curwin ()) 173 174-- sanity check, cursor position is kept 175eq ( { 1 , 0 } , api . nvim_win_get_cursor ( win )) 176local s2 =[[ 177^ | 178{1:~ }|*2 179{3:[No Name] }| 180prologue | 181|*2 182{2:[No Name] [+] }| 183| 184]] 185screen : expect ( s2 ) 186 187-- move cursor to end 188api . nvim_win_set_cursor ( win , { 101 , 0 } ) 189screen : expect ( [[ 190^ | 191{1:~ }|*2 192{3:[No Name] }| 193|*2 194epilogue | 195{2:[No Name] [+] }| 196| 197]] ) 198 199-- move cursor to the beginning again 200api . nvim_win_set_cursor ( win , { 1 , 0 } ) 201screen : expect ( s2 ) 202 203-- curwin didn't change back 204neq ( win , curwin ()) 205end ) 206 207it ( 'remembers what column it wants to be in' , function () 208insert ( 'first line' ) 209feed ( 'o<esc>' ) 210insert ( 'second line' ) 211 212feed ( 'gg' ) 213poke_eventloop () -- let nvim process the 'gg' command 214 215-- cursor position is at beginning 216local win =curwin () 217eq ( { 1 , 0 } , api . nvim_win_get_cursor ( win )) 218 219-- move cursor to column 5 220api . nvim_win_set_cursor ( win , { 1 , 5 } ) 221 222-- move down a line 223feed ( 'j' ) 224poke_eventloop () -- let nvim process the 'j' command 225 226-- cursor is still in column 5 227eq ( { 2 , 5 } , api . nvim_win_get_cursor ( win )) 228end ) 229 230it ( 'updates cursorline and statusline ruler in non-current window' , function () 231local screen =Screen . new ( 60 , 8 ) 232command ( 'set ruler' ) 233command ( 'set cursorline' ) 234insert ( [[ 235aaa 236bbb 237ccc 238ddd]] ) 239local oldwin =curwin () 240command ( 'vsplit' ) 241screen : expect ( [[ 242aaa │aaa | 243bbb │bbb | 244ccc │ccc | 245{21:dd^d }│{21:ddd }| 246{1:~ }│{1:~ }|*2 247{3:< Name] [+] 4,3 All }{2:<Name] [+] 4,3 All}| 248| 249]] ) 250api . nvim_win_set_cursor ( oldwin , { 1 , 0 } ) 251screen : expect ( [[ 252aaa │{21:aaa }| 253bbb │bbb | 254ccc │ccc | 255{21:dd^d }│ddd | 256{1:~ }│{1:~ }|*2 257{3:< Name] [+] 4,3 All }{2:<Name] [+] 1,1 All}| 258| 259]] ) 260end ) 261 262it ( 'updates cursorcolumn in non-current window' , function () 263local screen =Screen . new ( 60 , 8 ) 264command ( 'set cursorcolumn' ) 265insert ( [[ 266aaa 267bbb 268ccc 269ddd]] ) 270local oldwin =curwin () 271command ( 'vsplit' ) 272screen : expect ( [[ 273aa{21:a} │aa{21:a} | 274bb{21:b} │bb{21:b} | 275cc{21:c} │cc{21:c} | 276dd^d │ddd | 277{1:~ }│{1:~ }|*2 278{3:[No Name] [+] }{2:[No Name] [+] }| 279| 280]] ) 281api . nvim_win_set_cursor ( oldwin , { 2 , 0 } ) 282screen : expect ( [[ 283aa{21:a} │{21:a}aa | 284bb{21:b} │bbb | 285cc{21:c} │{21:c}cc | 286dd^d │{21:d}dd | 287{1:~ }│{1:~ }|*2 288{3:[No Name] [+] }{2:[No Name] [+] }| 289| 290]] ) 291end ) 292end ) 293 294describe ( 'resize' , function () 295local function heights ( wins ) 296return vim . tbl_map ( api . nvim_win_get_height , wins ) 297end 298local function widths ( wins ) 299return vim . tbl_map ( api . nvim_win_get_width , wins ) 300end 301 302it ( 'height: default anchor="top"' , function () 303command ( 'split | split' ) 304local wins =api . nvim_list_wins () 305local before =heights ( wins ) 306api . nvim_win_resize ( wins [ 2 ], - 1 , before [ 2 ] + 2 , {} ) 307-- default anchor top. 308local after =heights ( wins ) 309eq ( before [ 1 ], after [ 1 ]) 310eq ( before [ 2 ] + 2 , after [ 2 ]) 311eq ( before [ 3 ] - 2 , after [ 3 ]) 312end ) 313 314it ( 'height: anchor="bottom"' , function () 315command ( 'split | split' ) 316local wins =api . nvim_list_wins () 317local before =heights ( wins ) 318api . nvim_win_resize ( wins [ 2 ], - 1 , before [ 2 ] + 2 , { anchor ='bottom' } ) 319local after =heights ( wins ) 320-- bottom anchor unchanged. 321eq ( before [ 1 ] - 2 , after [ 1 ]) 322eq ( before [ 2 ] + 2 , after [ 2 ]) 323eq ( before [ 3 ], after [ 3 ]) 324end ) 325 326it ( 'height: anchor="bottom" in a nested frame' , function () 327-- layout: column[ Top, row[ML|MR], Bottom ]; current window = ML 328command ( 'split | split | 2wincmd w | vsplit' ) 329local ml =api . nvim_get_current_win () 330local function row_of ( w ) 331return fn . win_screenpos ( fn . win_id2win ( w ))[ 1 ] 332end 333local top , bottom 334for _ , w in ipairs ( api . nvim_list_wins ()) do 335if row_of ( w ) == 1 then 336top =w 337end 338if not bottom or row_of ( w ) > row_of ( bottom ) then 339bottom =w 340end 341end 342local top_h =api . nvim_win_get_height ( top ) 343local bottom_h =api . nvim_win_get_height ( bottom ) 344api . nvim_win_resize ( ml , - 1 , api . nvim_win_get_height ( ml ) + 3 , { anchor ='bottom' } ) 345-- "bottom" grow the row upward, taking from Top, not Bottom 346eq ( top_h - 3 , api . nvim_win_get_height ( top )) 347eq ( bottom_h , api . nvim_win_get_height ( bottom )) 348end ) 349 350it ( 'height: anchor="bottom" with borrowing' , function () 351-- column[ Wupper, row[ col[Ltop|Ltarget], Right ], Wlower ]; current = Ltarget. 352-- Growing Ltarget more than Ltop can supply forces us to borrow from row 353-- and row's growth must respect given anchor. 354command ( 'split | split | 2wincmd w | vsplit | split | wincmd j' ) 355local target =api . nvim_get_current_win () 356local function row_of ( w ) 357return fn . win_screenpos ( fn . win_id2win ( w ))[ 1 ] 358end 359local wupper , wlower 360for _ , w in ipairs ( api . nvim_list_wins ()) do 361if row_of ( w ) == 1 then 362wupper =w 363end 364if not wlower or row_of ( w ) > row_of ( wlower ) then 365wlower =w 366end 367end 368local wupper_h =api . nvim_win_get_height ( wupper ) 369local wlower_h =api . nvim_win_get_height ( wlower ) 370api . nvim_win_resize ( target , - 1 , api . nvim_win_get_height ( target ) + 6 , { anchor ='bottom' } ) 371-- must borrow from above. 372ok ( api . nvim_win_get_height ( wupper ) < wupper_h ) 373eq ( wlower_h , api . nvim_win_get_height ( wlower )) 374end ) 375 376it ( 'width: default anchor="left"' , function () 377command ( 'vsplit | vsplit' ) 378local wins =api . nvim_list_wins () 379local before =widths ( wins ) 380api . nvim_win_resize ( wins [ 2 ], before [ 2 ] + 4 , - 1 , {} ) 381local after =widths ( wins ) 382eq ( before [ 1 ], after [ 1 ]) 383eq ( before [ 2 ] + 4 , after [ 2 ]) 384eq ( before [ 3 ] - 4 , after [ 3 ]) 385end ) 386 387it ( 'width: anchor="right"' , function () 388command ( 'vsplit | vsplit' ) 389local wins =api . nvim_list_wins () 390local before =widths ( wins ) 391api . nvim_win_resize ( wins [ 2 ], before [ 2 ] + 4 , - 1 , { anchor ='right' } ) 392local after =widths ( wins ) 393eq ( before [ 1 ] - 4 , after [ 1 ]) 394eq ( before [ 2 ] + 4 , after [ 2 ]) 395eq ( before [ 3 ], after [ 3 ]) 396end ) 397 398it ( 'width: anchor="right" a nested frame' , function () 399-- layout: row[ Left, col[MT|MB], Right ]; current window = MT 400command ( 'vsplit | vsplit | 2wincmd w | split' ) 401local mt =api . nvim_get_current_win () 402local function col_of ( w ) 403return fn . win_screenpos ( fn . win_id2win ( w ))[ 2 ] 404end 405local left , right 406for _ , w in ipairs ( api . nvim_list_wins ()) do 407if col_of ( w ) == 1 then 408left =w 409end 410if not right or col_of ( w ) > col_of ( right ) then 411right =w 412end 413end 414local left_w =api . nvim_win_get_width ( left ) 415local right_w =api . nvim_win_get_width ( right ) 416api . nvim_win_resize ( mt , api . nvim_win_get_width ( mt ) + 3 , - 1 , { anchor ='right' } ) 417-- "right" must grow the column leftward, taking from Left, not Right 418eq ( left_w - 3 , api . nvim_win_get_width ( left )) 419eq ( right_w , api . nvim_win_get_width ( right )) 420end ) 421 422it ( 'sets both height and width in a single call' , function () 423-- layout: column[ Top, row[ML|MR], Bottom ]; current = ML. 424command ( 'split | split | 2wincmd w | vsplit' ) 425local ml =api . nvim_get_current_win () 426local function row_of ( w ) 427return fn . win_screenpos ( fn . win_id2win ( w ))[ 1 ] 428end 429local top , bottom , mr 430for _ , w in ipairs ( api . nvim_list_wins ()) do 431if row_of ( w ) == 1 then 432top =w 433end 434if not bottom or row_of ( w ) > row_of ( bottom ) then 435bottom =w 436end 437if row_of ( w ) == row_of ( ml ) and w ~= ml then 438mr =w 439end 440end 441local top_h =api . nvim_win_get_height ( top ) 442local bottom_h =api . nvim_win_get_height ( bottom ) 443local mr_w =api . nvim_win_get_width ( mr ) 444local h , w =api . nvim_win_get_height ( ml ), api . nvim_win_get_width ( ml ) 445-- "bottom" anchors the height axis; width has no matching anchor so it defaults to "left" 446api . nvim_win_resize ( ml , w - 4 , h + 2 , { anchor ='bottom' } ) 447eq ( h + 2 , api . nvim_win_get_height ( ml )) 448eq ( w - 4 , api . nvim_win_get_width ( ml )) 449-- height took from above 450eq ( top_h - 2 , api . nvim_win_get_height ( top )) 451eq ( bottom_h , api . nvim_win_get_height ( bottom )) 452-- width default to "left" 453eq ( mr_w + 4 , api . nvim_win_get_width ( mr )) 454end ) 455 456it ( 'correctly handles height=1' , function () 457command ( 'split' ) 458api . nvim_set_current_win ( api . nvim_list_wins ()[ 1 ]) 459api . nvim_win_resize ( api . nvim_list_wins ()[ 2 ], - 1 , 1 , {} ) 460eq ( 1 , api . nvim_win_get_height ( api . nvim_list_wins ()[ 2 ])) 461end ) 462 463it ( 'correctly handles height=1 with a winbar' , function () 464command ( 'set winbar=foobar' ) 465command ( 'set winminheight=0' ) 466command ( 'split' ) 467api . nvim_set_current_win ( api . nvim_list_wins ()[ 1 ]) 468api . nvim_win_resize ( api . nvim_list_wins ()[ 2 ], - 1 , 1 , {} ) 469eq ( 1 , api . nvim_win_get_height ( api . nvim_list_wins ()[ 2 ])) 470end ) 471 472it ( 'does not cause ml_get errors with foldmethod=expr #19989' , function () 473insert ( [[ 474aaaaa 475bbbbb 476ccccc]] ) 477command ( 'set foldmethod=expr' ) 478exec ( [[ 479new 480let w = nvim_get_current_win() 481wincmd w 482call nvim_win_resize(w, -1, 5, #{}) 483]] ) 484feed ( 'l' ) 485eq ( '' , api . nvim_get_vvar ( 'errmsg' )) 486end ) 487 488it ( 489'accepts size = 0 (clamp to minimum size). Compatible with deprecated set_height/width' , 490function () 491command ( 'set winminheight=1' ) 492command ( 'split' ) 493api . nvim_set_current_win ( api . nvim_list_wins ()[ 1 ]) 494api . nvim_win_resize ( api . nvim_list_wins ()[ 2 ], - 1 , 0 , {} ) 495eq ( 1 , api . nvim_win_get_height ( api . nvim_list_wins ()[ 2 ])) 496end 497) 498 499it ( 'validation' , function () 500command ( 'split' ) 501eq ( "Required: 'height' or 'width'" , pcall_err ( api . nvim_win_resize , 0 , - 1 , - 1 , {} )) 502eq ( 503"Invalid 'width': expected non-negative number or -1" , 504pcall_err ( api . nvim_win_resize , 0 , - 2 , 5 , {} ) 505) 506eq ( 507"Invalid 'height': expected non-negative number or -1" , 508pcall_err ( api . nvim_win_resize , 0 , 5 , - 2 , {} ) 509) 510-- anchor not applicable to the dimension(s) being changed 511eq ( 512"Conflict: 'left' not allowed with 'width'" , 513pcall_err ( api . nvim_win_resize , 0 , - 1 , 5 , { anchor ='left' } ) 514) 515-- unrecognized anchor value 516eq ( 517[[Invalid 'anchor': expected "top", "bottom", "left" or "right", got garbage]] , 518pcall_err ( api . nvim_win_resize , 0 , - 1 , 5 , { anchor ='garbage' } ) 519) 520eq ( 'Invalid window id: 999999' , pcall_err ( api . nvim_win_resize , 999999 , 5 , - 1 , {} )) 521end ) 522end ) 523 524describe ( '{get,set,del}_var' , function () 525it ( 'works' , function () 526api . nvim_win_set_var ( 0 , 'lua' , { 1 , 2 , { [ '3' ] =1 } } ) 527eq ( { 1 , 2 , { [ '3' ] =1 } } , api . nvim_win_get_var ( 0 , 'lua' )) 528eq ( { 1 , 2 , { [ '3' ] =1 } } , api . nvim_eval ( 'w:lua' )) 529eq ( 1 , fn . exists ( 'w:lua' )) 530api . nvim_win_del_var ( 0 , 'lua' ) 531eq ( 0 , fn . exists ( 'w:lua' )) 532eq ( 'Key not found: lua' , pcall_err ( api . nvim_win_del_var , 0 , 'lua' )) 533api . nvim_win_set_var ( 0 , 'lua' , 1 ) 534command ( 'lockvar w:lua' ) 535eq ( 'Key is locked: lua' , pcall_err ( api . nvim_win_del_var , 0 , 'lua' )) 536eq ( 'Key is locked: lua' , pcall_err ( api . nvim_win_set_var , 0 , 'lua' , 1 )) 537end ) 538 539it ( 'window_set_var returns the old value' , function () 540local val1 ={ 1 , 2 , { [ '3' ] =1 } } 541local val2 ={ 4 , 7 } 542eq ( NIL , request ( 'window_set_var' , 0 , 'lua' , val1 )) 543eq ( val1 , request ( 'window_set_var' , 0 , 'lua' , val2 )) 544end ) 545 546it ( 'window_del_var returns the old value' , function () 547local val1 ={ 1 , 2 , { [ '3' ] =1 } } 548local val2 ={ 4 , 7 } 549eq ( NIL , request ( 'window_set_var' , 0 , 'lua' , val1 )) 550eq ( val1 , request ( 'window_set_var' , 0 , 'lua' , val2 )) 551eq ( val2 , request ( 'window_del_var' , 0 , 'lua' )) 552end ) 553end ) 554 555describe ( 'nvim_get_option_value, nvim_set_option_value' , function () 556it ( 'works' , function () 557api . nvim_set_option_value ( 'colorcolumn' , '4,3' , {} ) 558eq ( '4,3' , api . nvim_get_option_value ( 'colorcolumn' , {} )) 559command ( 'set modified hidden' ) 560command ( 'enew' ) -- edit new buffer, window option is preserved 561eq ( '4,3' , api . nvim_get_option_value ( 'colorcolumn' , {} )) 562 563-- global-local option 564api . nvim_set_option_value ( 'statusline' , 'window-status' , { win =0 } ) 565eq ( 'window-status' , api . nvim_get_option_value ( 'statusline' , { win =0 } )) 566eq ( 567"%<%f %{%nvim_eval_statusline('%h%w%m%r', {'maxwidth': 30}).width > 0 ? '%h%w%m%r ' : ''%}%=%{% &showcmdloc == 'statusline' ? '%-10.S ' : '' %}%{% exists('b:keymap_name') ? '<'..b:keymap_name..'> ' : '' %}%{% &ruler ? &rulerformat : '' %}" , 568api . nvim_get_option_value ( 'statusline' , { scope ='global' } ) 569) 570command ( 'set modified' ) 571command ( 'enew' ) -- global-local: not preserved in new buffer 572-- confirm local value was not copied 573eq ( 574"%<%f %{%nvim_eval_statusline('%h%w%m%r', {'maxwidth': 30}).width > 0 ? '%h%w%m%r ' : ''%}%=%{% &showcmdloc == 'statusline' ? '%-10.S ' : '' %}%{% exists('b:keymap_name') ? '<'..b:keymap_name..'> ' : '' %}%{% &ruler ? &rulerformat : '' %}" , 575api . nvim_get_option_value ( 'statusline' , { win =0 } ) 576) 577eq ( '' , eval ( '&l:statusline' )) 578end ) 579 580it ( 'after switching windows #15390' , function () 581command ( 'tabnew' ) 582local tab1 =unpack ( api . nvim_list_tabpages ()) 583local win1 =unpack ( api . nvim_tabpage_list_wins ( tab1 )) 584api . nvim_set_option_value ( 'statusline' , 'window-status' , { win =win1 } ) 585command ( 'split' ) 586command ( 'wincmd J' ) 587command ( 'wincmd j' ) 588eq ( 'window-status' , api . nvim_get_option_value ( 'statusline' , { win =win1 } )) 589assert_alive () 590end ) 591 592describe ( 'after closing' , function () 593local buf , win0 , win1 , win2 594 595before_each ( function () 596win0 =api . nvim_get_current_win () 597command ( 'new' ) 598buf =api . nvim_get_current_buf () 599win1 =api . nvim_get_current_win () 600command ( 'set numberwidth=10' ) 601command ( 'split' ) 602win2 =api . nvim_get_current_win () 603command ( 'set numberwidth=15' ) 604command ( 'enew' ) 605api . nvim_set_current_win ( win1 ) 606command ( 'normal ix' ) 607command ( 'enew' ) 608api . nvim_set_current_win ( win0 ) 609eq ( 4 , api . nvim_get_option_value ( 'numberwidth' , {} )) 610end ) 611 612-- at this point buffer `buf` is current in no windows. Closing shouldn't affect its defaults 613it ( '0 windows' , function () 614api . nvim_set_current_buf ( buf ) 615eq ( 10 , api . nvim_get_option_value ( 'numberwidth' , {} )) 616end ) 617 618it ( '1 window' , function () 619api . nvim_win_close ( win1 , false ) 620 621api . nvim_set_current_buf ( buf ) 622eq ( 10 , api . nvim_get_option_value ( 'numberwidth' , {} )) 623end ) 624 625it ( '2 windows' , function () 626api . nvim_win_close ( win1 , false ) 627api . nvim_win_close ( win2 , false ) 628 629api . nvim_set_current_buf ( buf ) 630eq ( 10 , api . nvim_get_option_value ( 'numberwidth' , {} )) 631end ) 632end ) 633 634it ( 'returns values for unset local options' , function () 635eq ( - 1 , api . nvim_get_option_value ( 'scrolloff' , { win =0 , scope ='local' } )) 636end ) 637end ) 638 639describe ( 'get_position' , function () 640it ( 'works' , function () 641local height =api . nvim_win_get_height ( api . nvim_list_wins ()[ 1 ]) 642local width =api . nvim_win_get_width ( api . nvim_list_wins ()[ 1 ]) 643command ( 'split' ) 644command ( 'vsplit' ) 645eq ( { 0 , 0 } , api . nvim_win_get_position ( api . nvim_list_wins ()[ 1 ])) 646local vsplit_pos =math . floor ( width / 2 ) 647local split_pos =math . floor ( height / 2 ) 648local win2row , win2col =unpack ( api . nvim_win_get_position ( api . nvim_list_wins ()[ 2 ])) 649local win3row , win3col =unpack ( api . nvim_win_get_position ( api . nvim_list_wins ()[ 3 ])) 650eq ( 0 , win2row ) 651eq ( 0 , win3col ) 652ok ( vsplit_pos - 1 <= win2col and win2col <= vsplit_pos + 1 ) 653ok ( split_pos - 1 <= win3row and win3row <= split_pos + 1 ) 654end ) 655end ) 656 657describe ( 'get_tabpage' , function () 658it ( 'works' , function () 659command ( 'tabnew' ) 660command ( 'vsplit' ) 661eq ( api . nvim_list_tabpages ()[ 1 ], api . nvim_win_get_tabpage ( api . nvim_list_wins ()[ 1 ])) 662eq ( api . nvim_list_tabpages ()[ 2 ], api . nvim_win_get_tabpage ( api . nvim_list_wins ()[ 2 ])) 663eq ( api . nvim_list_tabpages ()[ 2 ], api . nvim_win_get_tabpage ( api . nvim_list_wins ()[ 3 ])) 664end ) 665end ) 666 667describe ( 'get_number' , function () 668it ( 'works' , function () 669local wins =api . nvim_list_wins () 670eq ( 1 , api . nvim_win_get_number ( wins [ 1 ])) 671 672command ( 'split' ) 673local win1 , win2 =unpack ( api . nvim_list_wins ()) 674eq ( 1 , api . nvim_win_get_number ( win1 )) 675eq ( 2 , api . nvim_win_get_number ( win2 )) 676 677command ( 'wincmd J' ) 678eq ( 2 , api . nvim_win_get_number ( win1 )) 679eq ( 1 , api . nvim_win_get_number ( win2 )) 680 681command ( 'tabnew' ) 682local win3 =api . nvim_list_wins ()[ 3 ] 683-- First tab page 684eq ( 2 , api . nvim_win_get_number ( win1 )) 685eq ( 1 , api . nvim_win_get_number ( win2 )) 686-- Second tab page 687eq ( 1 , api . nvim_win_get_number ( win3 )) 688end ) 689end ) 690 691describe ( 'is_valid' , function () 692it ( 'works' , function () 693command ( 'split' ) 694local win =api . nvim_list_wins ()[ 2 ] 695api . nvim_set_current_win ( win ) 696ok ( api . nvim_win_is_valid ( win )) 697command ( 'close' ) 698ok ( not api . nvim_win_is_valid ( win )) 699end ) 700end ) 701 702describe ( 'close' , function () 703it ( 'can close current window' , function () 704local oldwin =api . nvim_get_current_win () 705command ( 'split' ) 706local newwin =api . nvim_get_current_win () 707api . nvim_win_close ( newwin , false ) 708eq ( { oldwin } , api . nvim_list_wins ()) 709end ) 710 711it ( 'can close noncurrent window' , function () 712local oldwin =api . nvim_get_current_win () 713command ( 'split' ) 714local newwin =api . nvim_get_current_win () 715api . nvim_win_close ( oldwin , false ) 716eq ( { newwin } , api . nvim_list_wins ()) 717end ) 718 719it ( "handles changed buffer when 'hidden' is unset" , function () 720command ( 'set nohidden' ) 721local oldwin =api . nvim_get_current_win () 722insert ( 'text' ) 723command ( 'new' ) 724local newwin =api . nvim_get_current_win () 725eq ( 726'Vim:E37: No write since last change (add ! to override)' , 727pcall_err ( api . nvim_win_close , oldwin , false ) 728) 729eq ( { newwin , oldwin } , api . nvim_list_wins ()) 730end ) 731 732it ( 'handles changed buffer with force' , function () 733local oldwin =api . nvim_get_current_win () 734insert ( 'text' ) 735command ( 'new' ) 736local newwin =api . nvim_get_current_win () 737api . nvim_win_close ( oldwin , true ) 738eq ( { newwin } , api . nvim_list_wins ()) 739end ) 740 741it ( 'in cmdwin #9767' , function () 742command ( 'split' ) 743eq ( 2 , # api . nvim_list_wins ()) 744local otherwin =api . nvim_open_win ( 0 , false , { 745relative ='editor' , 746row =10 , 747col =10 , 748width =10 , 749height =10 , 750} ) 751feed ( 'q:' ) 752n . poke_eventloop () 753eq ( 4 , # api . nvim_list_wins ()) 754eq ( ':' , fn . getcmdwintype ()) 755-- Closing other windows still works. 756api . nvim_win_close ( otherwin , true ) 757eq ( false , api . nvim_win_is_valid ( otherwin )) 758-- Close cmdline-window. 759api . nvim_win_close ( 0 , true ) 760eq ( 2 , # api . nvim_list_wins ()) 761eq ( '' , fn . getcmdwintype ()) 762end ) 763 764it ( 'closing current (float) window of another tabpage #15313 #40745' , function () 765command ( 'tabedit' ) 766command ( 'botright split' ) 767local prevwin =curwin () 768eq ( 2 , eval ( 'tabpagenr()' )) 769local opts ={ relative ='editor' , row =10 , col =10 , width =50 , height =10 } 770local win =api . nvim_open_win ( 0 , true , opts ) 771local tab =eval ( 'tabpagenr()' ) 772command ( 'tabprevious' ) 773eq ( 1 , eval ( 'tabpagenr()' )) 774api . nvim_win_close ( win , false ) 775 776eq ( prevwin , api . nvim_tabpage_get_win ( tab )) 777assert_alive () 778 779command ( 'tabnext' ) 780local win1 =api . nvim_open_win ( 0 , true , opts ) 781local win2 =api . nvim_open_win ( 0 , true , opts ) 782command ( 'tabprevious' ) 783-- win1 becomes the tabpage's current window after win2 is closed 784api . nvim_win_close ( win2 , false ) 785api . nvim_win_close ( win1 , false ) 786assert_alive () 787end ) 788 789it ( 'closing a float does not enter unfocusable or hidden prevwin' , function () 790local firstwin =api . nvim_get_current_win () 791local wins ={} ---@type integer[] 792for _ =1 , 4 do 793wins [ # wins + 1 ] =api . nvim_open_win ( 0 , true , { 794relative ='editor' , 795row =10 , 796col =10 , 797width =50 , 798height =10 , 799} ) 800end 801api . nvim_win_set_config ( wins [ 3 ], { hide =true } ) 802api . nvim_win_close ( 0 , false ) 803eq ( firstwin , api . nvim_get_current_win ()) 804api . nvim_set_current_win ( wins [ 2 ]) 805api . nvim_set_current_win ( wins [ 3 ]) 806api . nvim_win_set_config ( wins [ 2 ], { focusable =false } ) 807api . nvim_win_close ( 0 , false ) 808eq ( firstwin , api . nvim_get_current_win ()) 809api . nvim_set_current_win ( wins [ 1 ]) 810api . nvim_set_current_win ( wins [ 2 ]) 811api . nvim_win_close ( 0 , false ) 812eq ( wins [ 1 ], api . nvim_get_current_win ()) 813end ) 814end ) 815 816describe ( 'hide' , function () 817it ( 'can hide current window' , function () 818local oldwin =api . nvim_get_current_win () 819command ( 'split' ) 820local newwin =api . nvim_get_current_win () 821api . nvim_win_hide ( newwin ) 822eq ( { oldwin } , api . nvim_list_wins ()) 823end ) 824it ( 'can hide noncurrent window' , function () 825local oldwin =api . nvim_get_current_win () 826command ( 'split' ) 827local newwin =api . nvim_get_current_win () 828api . nvim_win_hide ( oldwin ) 829eq ( { newwin } , api . nvim_list_wins ()) 830end ) 831it ( 'does not close the buffer' , function () 832local oldwin =api . nvim_get_current_win () 833local oldbuf =api . nvim_get_current_buf () 834local buf =api . nvim_create_buf ( true , false ) 835local newwin =api . nvim_open_win ( buf , true , { 836relative ='win' , 837row =3 , 838col =3 , 839width =12 , 840height =3 , 841} ) 842api . nvim_win_hide ( newwin ) 843eq ( { oldwin } , api . nvim_list_wins ()) 844eq ( { oldbuf , buf } , api . nvim_list_bufs ()) 845end ) 846it ( 'deletes the buffer when bufhidden=wipe' , function () 847local oldwin =api . nvim_get_current_win () 848local oldbuf =api . nvim_get_current_buf () 849local buf =api . nvim_create_buf ( true , false ) 850local newwin =api . nvim_open_win ( buf , true , { 851relative ='win' , 852row =3 , 853col =3 , 854width =12 , 855height =3 , 856} ) 857api . nvim_set_option_value ( 'bufhidden' , 'wipe' , { buf =buf } ) 858api . nvim_win_hide ( newwin ) 859eq ( { oldwin } , api . nvim_list_wins ()) 860eq ( { oldbuf } , api . nvim_list_bufs ()) 861end ) 862it ( 'in cmdwin' , function () 863feed ( 'q:' ) 864n . poke_eventloop () 865api . nvim_win_hide ( 0 ) 866eq ( '' , fn . getcmdwintype ()) 867end ) 868end ) 869 870describe ( 'text_height' , function () 871local screen , ns , X 872before_each ( function () 873screen =Screen . new ( 45 , 22 ) 874ns =api . nvim_create_namespace ( '' ) 875X =api . nvim_get_vvar ( 'maxcol' ) 876end ) 877 878it ( 'validation' , function () 879insert ( [[ 880aaa 881bbb 882ccc 883ddd 884eee]] ) 885eq ( 'Invalid window id: 23' , pcall_err ( api . nvim_win_text_height , 23 , {} )) 886eq ( 'Line index out of bounds' , pcall_err ( api . nvim_win_text_height , 0 , { start_row =5 } )) 887eq ( 'Line index out of bounds' , pcall_err ( api . nvim_win_text_height , 0 , { start_row =- 6 } )) 888eq ( 'Line index out of bounds' , pcall_err ( api . nvim_win_text_height , 0 , { end_row =5 } )) 889eq ( 'Line index out of bounds' , pcall_err ( api . nvim_win_text_height , 0 , { end_row =- 6 } )) 890eq ( 891"'start_row' is higher than 'end_row'" , 892pcall_err ( api . nvim_win_text_height , 0 , { start_row =3 , end_row =1 } ) 893) 894eq ( 895"'start_vcol' specified without 'start_row'" , 896pcall_err ( api . nvim_win_text_height , 0 , { end_row =2 , start_vcol =0 } ) 897) 898eq ( 899"'end_vcol' specified without 'end_row'" , 900pcall_err ( api . nvim_win_text_height , 0 , { start_row =2 , end_vcol =0 } ) 901) 902eq ( 903"Invalid 'start_vcol': out of range" , 904pcall_err ( api . nvim_win_text_height , 0 , { start_row =2 , start_vcol =- 1 } ) 905) 906eq ( 907"Invalid 'start_vcol': out of range" , 908pcall_err ( api . nvim_win_text_height , 0 , { start_row =2 , start_vcol =X + 1 } ) 909) 910eq ( 911"Invalid 'end_vcol': out of range" , 912pcall_err ( api . nvim_win_text_height , 0 , { end_row =2 , end_vcol =- 1 } ) 913) 914eq ( 915"Invalid 'end_vcol': out of range" , 916pcall_err ( api . nvim_win_text_height , 0 , { end_row =2 , end_vcol =X + 1 } ) 917) 918eq ( 919"Invalid 'max_height': out of range" , 920pcall_err ( api . nvim_win_text_height , 0 , { max_height =0 } ) 921) 922eq ( 923"'start_vcol' is higher than 'end_vcol'" , 924pcall_err ( 925api . nvim_win_text_height , 9260 , 927{ start_row =2 , end_row =2 , start_vcol =10 , end_vcol =5 } 928) 929) 930end ) 931 932it ( 'with two diff windows' , function () 933exec ( [[ 934set diffopt+=context:2 number 935let expr = 'printf("%08d", v:val) .. repeat("!", v:val)' 936call setline(1, map(range(1, 20) + range(25, 45), expr)) 937vnew 938call setline(1, map(range(3, 20) + range(28, 50), expr)) 939windo diffthis 940]] ) 941feed ( '24gg' ) 942screen : expect ( [[ 943{7: }{8: }{23:----------------}│{7: }{8: 1 }{22:00000001! }| 944{7: }{8: }{23:----------------}│{7: }{8: 2 }{22:00000002!! }| 945{7: }{8: 1 }00000003!!! │{7: }{8: 3 }00000003!!! | 946{7: }{8: 2 }00000004!!!! │{7: }{8: 4 }00000004!!!! | 947{7:+ }{8: 3 }{13:+-- 14 lines: 00}│{7:+ }{8: 5 }{13:+-- 14 lines: 00}| 948{7: }{8: 17 }00000019!!!!!!!!│{7: }{8: 19 }00000019!!!!!!!!| 949{7: }{8: 18 }00000020!!!!!!!!│{7: }{8: 20 }00000020!!!!!!!!| 950{7: }{8: }{23:----------------}│{7: }{8: 21 }{22:00000025!!!!!!!!}| 951{7: }{8: }{23:----------------}│{7: }{8: 22 }{22:00000026!!!!!!!!}| 952{7: }{8: }{23:----------------}│{7: }{8: 23 }{22:00000027!!!!!!!!}| 953{7: }{8: 19 }00000028!!!!!!!!│{7: }{8: 24 }^00000028!!!!!!!!| 954{7: }{8: 20 }00000029!!!!!!!!│{7: }{8: 25 }00000029!!!!!!!!| 955{7:+ }{8: 21 }{13:+-- 14 lines: 00}│{7:+ }{8: 26 }{13:+-- 14 lines: 00}| 956{7: }{8: 35 }00000044!!!!!!!!│{7: }{8: 40 }00000044!!!!!!!!| 957{7: }{8: 36 }00000045!!!!!!!!│{7: }{8: 41 }00000045!!!!!!!!| 958{7: }{8: 37 }{22:00000046!!!!!!!!}│{7: }{8: }{23:----------------}| 959{7: }{8: 38 }{22:00000047!!!!!!!!}│{7: }{8: }{23:----------------}| 960{7: }{8: 39 }{22:00000048!!!!!!!!}│{7: }{8: }{23:----------------}| 961{7: }{8: 40 }{22:00000049!!!!!!!!}│{7: }{8: }{23:----------------}| 962{7: }{8: 41 }{22:00000050!!!!!!!!}│{7: }{8: }{23:----------------}| 963{2:[No Name] [+] }{3:[No Name] [+] }| 964| 965]] ) 966screen : try_resize ( 45 , 3 ) 967screen : expect ( [[ 968{7: }{8: 19 }00000028!!!!!!!!│{7: }{8: 24 }^00000028!!!!!!!!| 969{2:[No Name] [+] }{3:[No Name] [+] }| 970| 971]] ) 972eq ( { all =20 , fill =5 , end_row =40 , end_vcol =53 } , api . nvim_win_text_height ( 1000 , {} )) 973eq ( { all =20 , fill =5 , end_row =40 , end_vcol =58 } , api . nvim_win_text_height ( 1001 , {} )) 974eq ( 975{ all =20 , fill =5 , end_row =40 , end_vcol =53 } , 976api . nvim_win_text_height ( 1000 , { start_row =0 } ) 977) 978eq ( 979{ all =20 , fill =5 , end_row =40 , end_vcol =58 } , 980api . nvim_win_text_height ( 1001 , { start_row =0 } ) 981) 982eq ( 983{ all =15 , fill =0 , end_row =40 , end_vcol =53 } , 984api . nvim_win_text_height ( 1000 , { end_row =- 1 } ) 985) 986eq ( 987{ all =15 , fill =0 , end_row =40 , end_vcol =53 } , 988api . nvim_win_text_height ( 1000 , { end_row =40 } ) 989) 990eq ( 991{ all =20 , fill =5 , end_row =40 , end_vcol =58 } , 992api . nvim_win_text_height ( 1001 , { end_row =- 1 } ) 993) 994eq ( 995{ all =20 , fill =5 , end_row =40 , end_vcol =58 } , 996api . nvim_win_text_height ( 1001 , { end_row =40 } ) 997) 998eq ( 999{ all =10 , fill =5 , end_row =40 , end_vcol =53 } , 1000api . nvim_win_text_height ( 1000 , { start_row =23 } ) 1001) 1002eq ( 1003{ all =13 , fill =3 , end_row =40 , end_vcol =58 } , 1004api . nvim_win_text_height ( 1001 , { start_row =18 } ) 1005) 1006eq ( 1007{ all =11 , fill =0 , end_row =23 , end_vcol =36 } , 1008api . nvim_win_text_height ( 1000 , { end_row =23 } ) 1009) 1010eq ( 1011{ all =11 , fill =5 , end_row =18 , end_vcol =36 } , 1012api . nvim_win_text_height ( 1001 , { end_row =18 } ) 1013) 1014eq ( 1015{ all =11 , fill =0 , end_row =39 , end_vcol =52 } , 1016api . nvim_win_text_height ( 1000 , { start_row =3 , end_row =39 } ) 1017) 1018eq ( 1019{ all =11 , fill =3 , end_row =34 , end_vcol =52 } , 1020api . nvim_win_text_height ( 1001 , { start_row =1 , end_row =34 } ) 1021) 1022eq ( 1023{ all =9 , fill =0 , end_row =25 , end_vcol =0 } , 1024api . nvim_win_text_height ( 1000 , { start_row =4 , end_row =38 } ) 1025) 1026eq ( 1027{ all =9 , fill =3 , end_row =20 , end_vcol =0 } , 1028api . nvim_win_text_height ( 1001 , { start_row =2 , end_row =33 } ) 1029) 1030eq ( 1031{ all =9 , fill =0 , end_row =25 , end_vcol =0 } , 1032api . nvim_win_text_height ( 1000 , { start_row =5 , end_row =37 } ) 1033) 1034eq ( 1035{ all =9 , fill =3 , end_row =20 , end_vcol =0 } , 1036api . nvim_win_text_height ( 1001 , { start_row =3 , end_row =32 } ) 1037) 1038eq ( 1039{ all =9 , fill =0 , end_row =25 , end_vcol =0 } , 1040api . nvim_win_text_height ( 1000 , { start_row =17 , end_row =25 } ) 1041) 1042eq ( 1043{ all =9 , fill =3 , end_row =20 , end_vcol =0 } , 1044api . nvim_win_text_height ( 1001 , { start_row =15 , end_row =20 } ) 1045) 1046eq ( 1047{ all =7 , fill =0 , end_row =24 , end_vcol =37 } , 1048api . nvim_win_text_height ( 1000 , { start_row =18 , end_row =24 } ) 1049) 1050eq ( 1051{ all =7 , fill =3 , end_row =19 , end_vcol =37 } , 1052api . nvim_win_text_height ( 1001 , { start_row =16 , end_row =19 } ) 1053) 1054eq ( 1055{ all =6 , fill =5 , end_row =40 , end_vcol =53 } , 1056api . nvim_win_text_height ( 1000 , { start_row =- 1 } ) 1057) 1058eq ( 1059{ all =5 , fill =5 , end_row =40 , end_vcol =53 } , 1060api . nvim_win_text_height ( 1000 , { start_row =- 1 , start_vcol =X } ) 1061) 1062eq ( 1063{ all =0 , fill =0 , end_row =40 , end_vcol =53 } , 1064api . nvim_win_text_height ( 1000 , { start_row =- 1 , start_vcol =X , end_row =- 1 } ) 1065) 1066eq ( 1067{ all =0 , fill =0 , end_row =40 , end_vcol =53 } , 1068api . nvim_win_text_height ( 10691000 , 1070{ start_row =- 1 , start_vcol =X , end_row =- 1 , end_vcol =X } 1071) 1072) 1073eq ( 1074{ all =1 , fill =0 , end_row =40 , end_vcol =53 } , 1075api . nvim_win_text_height ( 10761000 , 1077{ start_row =- 1 , start_vcol =0 , end_row =- 1 , end_vcol =X } 1078) 1079) 1080eq ( 1081{ all =3 , fill =2 , end_row =0 , end_vcol =11 } , 1082api . nvim_win_text_height ( 1001 , { end_row =0 } ) 1083) 1084eq ( 1085{ all =2 , fill =2 , end_row =0 , end_vcol =0 } , 1086api . nvim_win_text_height ( 1001 , { end_row =0 , end_vcol =0 } ) 1087) 1088eq ( 1089{ all =2 , fill =2 , end_row =0 , end_vcol =0 } , 1090api . nvim_win_text_height ( 1001 , { start_row =0 , end_row =0 , end_vcol =0 } ) 1091) 1092eq ( 1093{ all =0 , fill =0 , end_row =0 , end_vcol =0 } , 1094api . nvim_win_text_height ( 1001 , { start_row =0 , start_vcol =0 , end_row =0 , end_vcol =0 } ) 1095) 1096eq ( 1097{ all =1 , fill =0 , end_row =0 , end_vcol =11 } , 1098api . nvim_win_text_height ( 1001 , { start_row =0 , start_vcol =0 , end_row =0 , end_vcol =X } ) 1099) 1100eq ( 1101{ all =11 , fill =5 , end_row =18 , end_vcol =36 } , 1102api . nvim_win_text_height ( 1001 , { end_row =18 } ) 1103) 1104eq ( 1105{ all =9 , fill =3 , end_row =18 , end_vcol =36 } , 1106api . nvim_win_text_height ( 1001 , { start_row =0 , start_vcol =0 , end_row =18 } ) 1107) 1108eq ( 1109{ all =10 , fill =5 , end_row =18 , end_vcol =0 } , 1110api . nvim_win_text_height ( 1001 , { end_row =18 , end_vcol =0 } ) 1111) 1112eq ( 1113{ all =8 , fill =3 , end_row =18 , end_vcol =0 } , 1114api . nvim_win_text_height ( 11151001 , 1116{ start_row =0 , start_vcol =0 , end_row =18 , end_vcol =0 } 1117) 1118) 1119end ) 1120 1121it ( 'with wrapped lines' , function () 1122exec ( [[ 1123set number cpoptions+=n 1124call setline(1, repeat([repeat('foobar-', 36)], 3)) 1125]] ) 1126api . nvim_buf_set_extmark ( 11270 , 1128ns , 11291 , 1130100 , 1131{ virt_text ={ { ( '?' ): rep ( 15 ), 'Search' } } , virt_text_pos ='inline' } 1132) 1133api . nvim_buf_set_extmark ( 11340 , 1135ns , 11362 , 1137200 , 1138{ virt_text ={ { ( '!' ): rep ( 75 ), 'Search' } } , virt_text_pos ='inline' } 1139) 1140screen : expect ( [[ 1141{8: 1 }^foobar-foobar-foobar-foobar-foobar-foobar| 1142-foobar-foobar-foobar-foobar-foobar-foobar-fo| 1143obar-foobar-foobar-foobar-foobar-foobar-fooba| 1144r-foobar-foobar-foobar-foobar-foobar-foobar-f| 1145oobar-foobar-foobar-foobar-foobar-foobar-foob| 1146ar-foobar-foobar-foobar-foobar- | 1147{8: 2 }foobar-foobar-foobar-foobar-foobar-foobar| 1148-foobar-foobar-foobar-foobar-foobar-foobar-fo| 1149obar-foobar-fo{10:???????????????}obar-foobar-foob| 1150ar-foobar-foobar-foobar-foobar-foobar-foobar-| 1151foobar-foobar-foobar-foobar-foobar-foobar-foo| 1152bar-foobar-foobar-foobar-foobar-foobar-foobar| 1153- | 1154{8: 3 }foobar-foobar-foobar-foobar-foobar-foobar| 1155-foobar-foobar-foobar-foobar-foobar-foobar-fo| 1156obar-foobar-foobar-foobar-foobar-foobar-fooba| 1157r-foobar-foobar-foobar-foobar-foobar-foobar-f| 1158oobar-foobar-foobar-foob{10:!!!!!!!!!!!!!!!!!!!!!}| 1159{10:!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!}| 1160{10:!!!!!!!!!}ar-foobar-foobar-foobar-foobar-fooba| 1161r-foobar-foobar- | 1162| 1163]] ) 1164screen : try_resize ( 45 , 2 ) 1165screen : expect ( [[ 1166{8: 1 }^foobar-foobar-foobar-foobar-foobar-foobar| 1167| 1168]] ) 1169eq ( { all =21 , fill =0 , end_row =2 , end_vcol =327 } , api . nvim_win_text_height ( 0 , {} )) 1170eq ( 1171{ all =6 , fill =0 , end_row =0 , end_vcol =252 } , 1172api . nvim_win_text_height ( 0 , { start_row =0 , end_row =0 } ) 1173) 1174eq ( 1175{ all =7 , fill =0 , end_row =1 , end_vcol =267 } , 1176api . nvim_win_text_height ( 0 , { start_row =1 , end_row =1 } ) 1177) 1178eq ( 1179{ all =8 , fill =0 , end_row =2 , end_vcol =327 } , 1180api . nvim_win_text_height ( 0 , { start_row =2 , end_row =2 } ) 1181) 1182eq ( 1183{ all =0 , fill =0 , end_row =1 , end_vcol =0 } , 1184api . nvim_win_text_height ( 0 , { start_row =1 , start_vcol =0 , end_row =1 , end_vcol =0 } ) 1185) 1186eq ( 1187{ all =1 , fill =0 , end_row =1 , end_vcol =41 } , 1188api . nvim_win_text_height ( 0 , { start_row =1 , start_vcol =0 , end_row =1 , end_vcol =41 } ) 1189) 1190eq ( 1191{ all =2 , fill =0 , end_row =1 , end_vcol =42 } , 1192api . nvim_win_text_height ( 0 , { start_row =1 , start_vcol =0 , end_row =1 , end_vcol =42 } ) 1193) 1194eq ( 1195{ all =2 , fill =0 , end_row =1 , end_vcol =86 } , 1196api . nvim_win_text_height ( 0 , { start_row =1 , start_vcol =0 , end_row =1 , end_vcol =86 } ) 1197) 1198eq ( 1199{ all =3 , fill =0 , end_row =1 , end_vcol =87 } , 1200api . nvim_win_text_height ( 0 , { start_row =1 , start_vcol =0 , end_row =1 , end_vcol =87 } ) 1201) 1202eq ( 1203{ all =6 , fill =0 , end_row =1 , end_vcol =266 } , 1204api . nvim_win_text_height ( 0 , { start_row =1 , start_vcol =0 , end_row =1 , end_vcol =266 } ) 1205) 1206eq ( 1207{ all =7 , fill =0 , end_row =1 , end_vcol =267 } , 1208api . nvim_win_text_height ( 0 , { start_row =1 , start_vcol =0 , end_row =1 , end_vcol =267 } ) 1209) 1210eq ( 1211{ all =7 , fill =0 , end_row =1 , end_vcol =267 } , 1212api . nvim_win_text_height ( 0 , { start_row =1 , start_vcol =0 , end_row =1 , end_vcol =311 } ) 1213) 1214eq ( 1215{ all =7 , fill =0 , end_row =1 , end_vcol =267 } , 1216api . nvim_win_text_height ( 0 , { start_row =1 , start_vcol =0 , end_row =1 , end_vcol =312 } ) 1217) 1218eq ( 1219{ all =7 , fill =0 , end_row =1 , end_vcol =267 } , 1220api . nvim_win_text_height ( 0 , { start_row =1 , start_vcol =0 , end_row =1 , end_vcol =X } ) 1221) 1222eq ( 1223{ all =7 , fill =0 , end_row =1 , end_vcol =267 } , 1224api . nvim_win_text_height ( 0 , { start_row =1 , start_vcol =40 , end_row =1 , end_vcol =X } ) 1225) 1226eq ( 1227{ all =6 , fill =0 , end_row =1 , end_vcol =267 } , 1228api . nvim_win_text_height ( 0 , { start_row =1 , start_vcol =41 , end_row =1 , end_vcol =X } ) 1229) 1230eq ( 1231{ all =6 , fill =0 , end_row =1 , end_vcol =267 } , 1232api . nvim_win_text_height ( 0 , { start_row =1 , start_vcol =85 , end_row =1 , end_vcol =X } ) 1233) 1234eq ( 1235{ all =5 , fill =0 , end_row =1 , end_vcol =267 } , 1236api . nvim_win_text_height ( 0 , { start_row =1 , start_vcol =86 , end_row =1 , end_vcol =X } ) 1237) 1238eq ( 1239{ all =2 , fill =0 , end_row =1 , end_vcol =267 } , 1240api . nvim_win_text_height ( 0 , { start_row =1 , start_vcol =265 , end_row =1 , end_vcol =X } ) 1241) 1242eq ( 1243{ all =1 , fill =0 , end_row =1 , end_vcol =267 } , 1244api . nvim_win_text_height ( 0 , { start_row =1 , start_vcol =266 , end_row =1 , end_vcol =X } ) 1245) 1246eq ( 1247{ all =1 , fill =0 , end_row =1 , end_vcol =267 } , 1248api . nvim_win_text_height ( 0 , { start_row =1 , start_vcol =310 , end_row =1 , end_vcol =X } ) 1249) 1250eq ( 1251{ all =0 , fill =0 , end_row =1 , end_vcol =267 } , 1252api . nvim_win_text_height ( 0 , { start_row =1 , start_vcol =311 , end_row =1 , end_vcol =X } ) 1253) 1254eq ( 1255{ all =1 , fill =0 , end_row =1 , end_vcol =131 } , 1256api . nvim_win_text_height ( 0 , { start_row =1 , start_vcol =86 , end_row =1 , end_vcol =131 } ) 1257) 1258eq ( 1259{ all =1 , fill =0 , end_row =1 , end_vcol =266 } , 1260api . nvim_win_text_height ( 12610 , 1262{ start_row =1 , start_vcol =221 , end_row =1 , end_vcol =266 } 1263) 1264) 1265eq ( 1266{ all =18 , fill =0 , end_row =2 , end_vcol =327 } , 1267api . nvim_win_text_height ( 0 , { start_row =0 , start_vcol =131 } ) 1268) 1269eq ( 1270{ all =19 , fill =0 , end_row =2 , end_vcol =327 } , 1271api . nvim_win_text_height ( 0 , { start_row =0 , start_vcol =130 } ) 1272) 1273eq ( 1274{ all =20 , fill =0 , end_row =2 , end_vcol =311 } , 1275api . nvim_win_text_height ( 0 , { end_row =2 , end_vcol =311 } ) 1276) 1277eq ( 1278{ all =21 , fill =0 , end_row =2 , end_vcol =312 } , 1279api . nvim_win_text_height ( 0 , { end_row =2 , end_vcol =312 } ) 1280) 1281eq ( 1282{ all =17 , fill =0 , end_row =2 , end_vcol =311 } , 1283api . nvim_win_text_height ( 12840 , 1285{ start_row =0 , start_vcol =131 , end_row =2 , end_vcol =311 } 1286) 1287) 1288eq ( 1289{ all =19 , fill =0 , end_row =2 , end_vcol =312 } , 1290api . nvim_win_text_height ( 12910 , 1292{ start_row =0 , start_vcol =130 , end_row =2 , end_vcol =312 } 1293) 1294) 1295eq ( 1296{ all =16 , fill =0 , end_row =2 , end_vcol =327 } , 1297api . nvim_win_text_height ( 0 , { start_row =0 , start_vcol =221 } ) 1298) 1299eq ( 1300{ all =17 , fill =0 , end_row =2 , end_vcol =327 } , 1301api . nvim_win_text_height ( 0 , { start_row =0 , start_vcol =220 } ) 1302) 1303eq ( 1304{ all =14 , fill =0 , end_row =2 , end_vcol =41 } , 1305api . nvim_win_text_height ( 0 , { end_row =2 , end_vcol =41 } ) 1306) 1307eq ( 1308{ all =15 , fill =0 , end_row =2 , end_vcol =42 } , 1309api . nvim_win_text_height ( 0 , { end_row =2 , end_vcol =42 } ) 1310) 1311eq ( 1312{ all =9 , fill =0 , end_row =2 , end_vcol =41 } , 1313api . nvim_win_text_height ( 0 , { start_row =0 , start_vcol =221 , end_row =2 , end_vcol =41 } ) 1314) 1315eq ( 1316{ all =11 , fill =0 , end_row =2 , end_vcol =42 } , 1317api . nvim_win_text_height ( 0 , { start_row =0 , start_vcol =220 , end_row =2 , end_vcol =42 } ) 1318) 1319exec ( 'call setline(1, "foo")' ) 1320eq ( 1321{ all =1 , fill =0 , end_row =0 , end_vcol =3 } , 1322api . nvim_win_text_height ( 0 , { max_height =1 } ) 1323) 1324eq ( 1325{ all =8 , fill =0 , end_row =1 , end_vcol =41 } , 1326api . nvim_win_text_height ( 0 , { max_height =2 } ) 1327) 1328eq ( 1329{ all =2 , fill =0 , end_row =1 , end_vcol =1 } , 1330api . nvim_win_text_height ( 0 , { max_height =2 , end_row =1 , end_vcol =1 } ) 1331) 1332eq ( 1333{ all =8 , fill =0 , end_row =1 , end_vcol =41 } , 1334api . nvim_win_text_height ( 0 , { max_height =2 , end_row =2 , end_vcol =1 } ) 1335) 1336end ) 1337 1338it ( 'with virtual lines around a fold' , function () 1339screen : try_resize ( 45 , 10 ) 1340exec ( [[ 1341call setline(1, range(1, 8)) 13423,6fold 1343]] ) 1344api . nvim_buf_set_extmark ( 13450 , 1346ns , 13471 , 13480 , 1349{ virt_lines ={ { { 'VIRT LINE 1' } } , { { 'VIRT LINE 2' } } } } 1350) 1351api . nvim_buf_set_extmark ( 13520 , 1353ns , 13546 , 13550 , 1356{ virt_lines ={ { { 'VIRT LINE 3' } } } , virt_lines_above =true } 1357) 1358screen : expect ( [[ 1359^1 | 13602 | 1361VIRT LINE 1 | 1362VIRT LINE 2 | 1363{13:+-- 4 lines: 3······························}| 1364VIRT LINE 3 | 13657 | 13668 | 1367{1:~ }| 1368| 1369]] ) 1370eq ( { all =8 , fill =3 , end_row =7 , end_vcol =1 } , api . nvim_win_text_height ( 0 , {} )) 1371eq ( 1372{ all =5 , fill =2 , end_row =2 , end_vcol =0 } , 1373api . nvim_win_text_height ( 0 , { end_row =2 } ) 1374) 1375eq ( 1376{ all =5 , fill =2 , end_row =2 , end_vcol =0 } , 1377api . nvim_win_text_height ( 0 , { end_row =2 , end_vcol =X } ) 1378) 1379eq ( 1380{ all =5 , fill =2 , end_row =2 , end_vcol =0 } , 1381api . nvim_win_text_height ( 0 , { end_row =2 , end_vcol =90 } ) 1382) 1383eq ( 1384{ all =5 , fill =2 , end_row =2 , end_vcol =0 } , 1385api . nvim_win_text_height ( 0 , { end_row =2 , end_vcol =46 } ) 1386) 1387eq ( 1388{ all =5 , fill =2 , end_row =2 , end_vcol =0 } , 1389api . nvim_win_text_height ( 0 , { end_row =2 , end_vcol =45 } ) 1390) 1391eq ( 1392{ all =5 , fill =2 , end_row =2 , end_vcol =0 } , 1393api . nvim_win_text_height ( 0 , { end_row =2 , end_vcol =1 } ) 1394) 1395eq ( 1396{ all =4 , fill =2 , end_row =2 , end_vcol =0 } , 1397api . nvim_win_text_height ( 0 , { end_row =2 , end_vcol =0 } ) 1398) 1399eq ( 1400{ all =6 , fill =3 , end_row =7 , end_vcol =1 } , 1401api . nvim_win_text_height ( 0 , { start_row =2 } ) 1402) 1403eq ( 1404{ all =4 , fill =1 , end_row =7 , end_vcol =1 } , 1405api . nvim_win_text_height ( 0 , { start_row =2 , start_vcol =0 } ) 1406) 1407eq ( 1408{ all =4 , fill =1 , end_row =7 , end_vcol =1 } , 1409api . nvim_win_text_height ( 0 , { start_row =2 , start_vcol =44 } ) 1410) 1411eq ( 1412{ all =3 , fill =1 , end_row =7 , end_vcol =1 } , 1413api . nvim_win_text_height ( 0 , { start_row =2 , start_vcol =45 } ) 1414) 1415eq ( 1416{ all =3 , fill =1 , end_row =7 , end_vcol =1 } , 1417api . nvim_win_text_height ( 0 , { start_row =2 , start_vcol =89 } ) 1418) 1419eq ( 1420{ all =3 , fill =1 , end_row =7 , end_vcol =1 } , 1421api . nvim_win_text_height ( 0 , { start_row =2 , start_vcol =90 } ) 1422) 1423eq ( 1424{ all =3 , fill =1 , end_row =7 , end_vcol =1 } , 1425api . nvim_win_text_height ( 0 , { start_row =2 , start_vcol =X } ) 1426) 1427end ) 1428 1429it ( 'with virt_lines above max_height row' , function () 1430screen : try_resize ( 45 , 10 ) 1431exec ( 'call setline(1, range(1, 7) + ["foo"->repeat(20)])' ) 1432api . nvim_buf_set_extmark ( 0 , ns , 6 , 0 , { virt_lines ={ { { 'VIRT LINE 1' } } } } ) 1433screen : expect ( [[ 1434^1 | 14352 | 14363 | 14374 | 14385 | 14396 | 14407 | 1441VIRT LINE 1 | 1442foofoofoofoofoofoofoofoofoofoofoofoofoofoo{1:@@@}| 1443| 1444]] ) 1445eq ( 1446{ all =10 , fill =1 , end_row =7 , end_vcol =45 } , 1447api . nvim_win_text_height ( 0 , { max_height =api . nvim_win_get_height ( 0 ) } ) 1448) 1449end ) 1450end ) 1451 1452describe ( 'open_win' , function () 1453it ( 'works while cmdwin is open #40312' , function () 1454-- Legacy cmdwin blocked nvim_open_win with E11; now there is no such restriction. 1455local new_buf =api . nvim_create_buf ( true , true ) 1456feed ( 'q:' ) 1457n . poke_eventloop () 1458local new_win =api . nvim_open_win ( new_buf , false , { 1459relative ='editor' , 1460row =5 , 1461col =5 , 1462width =5 , 1463height =5 , 1464} ) 1465eq ( new_buf , api . nvim_win_get_buf ( new_win )) 1466end ) 1467 1468it ( 'aborts if buffer is invalid' , function () 1469local wins_before =api . nvim_list_wins () 1470eq ( 1471'Invalid buffer id: 1337' , 1472pcall_err ( api . nvim_open_win , 1337 , false , { 1473relative ='editor' , 1474row =5 , 1475col =5 , 1476width =5 , 1477height =5 , 1478} ) 1479) 1480eq ( wins_before , api . nvim_list_wins ()) 1481end ) 1482 1483describe ( 'creates a split window above' , function () 1484local function test_open_win_split_above ( key , val ) 1485local initial_win =api . nvim_get_current_win () 1486local win =api . nvim_open_win ( 0 , true , { 1487[ key ] =val , 1488height =10 , 1489} ) 1490eq ( '' , api . nvim_win_get_config ( win ). relative ) 1491eq ( 10 , api . nvim_win_get_height ( win )) 1492local layout =fn . winlayout () 1493eq ( { 1494'col' , 1495{ 1496{ 'leaf' , win } , 1497{ 'leaf' , initial_win } , 1498} , 1499} , layout ) 1500end 1501 1502it ( "with split = 'above'" , function () 1503test_open_win_split_above ( 'split' , 'above' ) 1504end ) 1505 1506it ( "with vertical = false and 'nosplitbelow'" , function () 1507api . nvim_set_option_value ( 'splitbelow' , false , {} ) 1508test_open_win_split_above ( 'vertical' , false ) 1509end ) 1510end ) 1511 1512describe ( 'creates a split window below' , function () 1513local function test_open_win_split_below ( key , val ) 1514local initial_win =api . nvim_get_current_win () 1515local win =api . nvim_open_win ( 0 , true , { 1516[ key ] =val , 1517height =15 , 1518} ) 1519eq ( '' , api . nvim_win_get_config ( win ). relative ) 1520eq ( 15 , api . nvim_win_get_height ( win )) 1521local layout =fn . winlayout () 1522eq ( { 1523'col' , 1524{ 1525{ 'leaf' , initial_win } , 1526{ 'leaf' , win } , 1527} , 1528} , layout ) 1529end 1530 1531it ( "with split = 'below'" , function () 1532test_open_win_split_below ( 'split' , 'below' ) 1533end ) 1534 1535it ( "with vertical = false and 'splitbelow'" , function () 1536api . nvim_set_option_value ( 'splitbelow' , true , {} ) 1537test_open_win_split_below ( 'vertical' , false ) 1538end ) 1539end ) 1540 1541describe ( 'creates a split window to the left' , function () 1542local function test_open_win_split_left ( key , val ) 1543local initial_win =api . nvim_get_current_win () 1544local win =api . nvim_open_win ( 0 , true , { 1545[ key ] =val , 1546width =25 , 1547} ) 1548eq ( '' , api . nvim_win_get_config ( win ). relative ) 1549eq ( 25 , api . nvim_win_get_width ( win )) 1550local layout =fn . winlayout () 1551eq ( { 1552'row' , 1553{ 1554{ 'leaf' , win } , 1555{ 'leaf' , initial_win } , 1556} , 1557} , layout ) 1558end 1559 1560it ( "with split = 'left'" , function () 1561test_open_win_split_left ( 'split' , 'left' ) 1562end ) 1563 1564it ( "with vertical = true and 'nosplitright'" , function () 1565api . nvim_set_option_value ( 'splitright' , false , {} ) 1566test_open_win_split_left ( 'vertical' , true ) 1567end ) 1568end ) 1569 1570describe ( 'creates a split window to the right' , function () 1571local function test_open_win_split_right ( key , val ) 1572local initial_win =api . nvim_get_current_win () 1573local win =api . nvim_open_win ( 0 , true , { 1574[ key ] =val , 1575width =30 , 1576} ) 1577eq ( '' , api . nvim_win_get_config ( win ). relative ) 1578eq ( 30 , api . nvim_win_get_width ( win )) 1579local layout =fn . winlayout () 1580eq ( { 1581'row' , 1582{ 1583{ 'leaf' , initial_win } , 1584{ 'leaf' , win } , 1585} , 1586} , layout ) 1587end 1588 1589it ( "with split = 'right'" , function () 1590test_open_win_split_right ( 'split' , 'right' ) 1591end ) 1592 1593it ( "with vertical = true and 'splitright'" , function () 1594api . nvim_set_option_value ( 'splitright' , true , {} ) 1595test_open_win_split_right ( 'vertical' , true ) 1596end ) 1597end ) 1598 1599it ( "doesn't change tp_curwin when splitting window in another tab with enter=false" , function () 1600local tab1 =api . nvim_get_current_tabpage () 1601local tab1_win =api . nvim_get_current_win () 1602 1603n . command ( 'tabnew' ) 1604local tab2 =api . nvim_get_current_tabpage () 1605local tab2_win =api . nvim_get_current_win () 1606 1607eq ( { tab1_win , tab2_win } , api . nvim_list_wins ()) 1608eq ( { tab1 , tab2 } , api . nvim_list_tabpages ()) 1609 1610api . nvim_set_current_tabpage ( tab1 ) 1611eq ( tab1_win , api . nvim_get_current_win ()) 1612 1613local tab2_prevwin =fn . tabpagewinnr ( tab2 , '#' ) 1614 1615-- split in tab2 whine in tab2, with enter = false 1616local tab2_win2 =api . nvim_open_win ( api . nvim_create_buf ( false , true ), false , { 1617win =tab2_win , 1618split ='right' , 1619} ) 1620eq ( tab1_win , api . nvim_get_current_win ()) -- we should still be in the first tp 1621eq ( tab1_win , api . nvim_tabpage_get_win ( tab1 )) 1622 1623eq ( tab2_win , api . nvim_tabpage_get_win ( tab2 )) -- tab2's tp_curwin should not have changed 1624eq ( tab2_prevwin , fn . tabpagewinnr ( tab2 , '#' )) -- tab2's tp_prevwin should not have changed 1625eq ( { tab1_win , tab2_win , tab2_win2 } , api . nvim_list_wins ()) 1626eq ( { tab2_win , tab2_win2 } , api . nvim_tabpage_list_wins ( tab2 )) 1627end ) 1628 1629it ( 'creates splits in the correct location' , function () 1630local first_win =api . nvim_get_current_win () 1631-- specifying window 0 should create a split next to the current window 1632local win =api . nvim_open_win ( 0 , true , { 1633vertical =false , 1634} ) 1635local layout =fn . winlayout () 1636eq ( { 1637'col' , 1638{ 1639{ 'leaf' , win } , 1640{ 'leaf' , first_win } , 1641} , 1642} , layout ) 1643-- not specifying a window should create a top-level split 1644local win2 =api . nvim_open_win ( 0 , true , { 1645split ='left' , 1646win =- 1 , 1647} ) 1648layout =fn . winlayout () 1649eq ( { 1650'row' , 1651{ 1652{ 'leaf' , win2 } , 1653{ 1654'col' , 1655{ 1656{ 'leaf' , win } , 1657{ 'leaf' , first_win } , 1658} , 1659} , 1660} , 1661} , layout ) 1662 1663-- specifying a window should create a split next to that window 1664local win3 =api . nvim_open_win ( 0 , true , { 1665win =win , 1666vertical =false , 1667} ) 1668layout =fn . winlayout () 1669eq ( { 1670'row' , 1671{ 1672{ 'leaf' , win2 } , 1673{ 1674'col' , 1675{ 1676{ 'leaf' , win3 } , 1677{ 'leaf' , win } , 1678{ 'leaf' , first_win } , 1679} , 1680} , 1681} , 1682} , layout ) 1683end ) 1684 1685it ( 'opens floating windows in other tabpages' , function () 1686local first_win =api . nvim_get_current_win () 1687local first_tab =api . nvim_get_current_tabpage () 1688 1689command ( 'tabnew' ) 1690local new_tab =api . nvim_get_current_tabpage () 1691local win =api . nvim_open_win ( 0 , false , { 1692relative ='win' , 1693win =first_win , 1694width =5 , 1695height =5 , 1696row =1 , 1697col =1 , 1698} ) 1699eq ( first_tab , api . nvim_win_get_tabpage ( win )) 1700eq ( new_tab , api . nvim_get_current_tabpage ()) 1701end ) 1702 1703it ( 'switches to new windows in non-current tabpages when enter=true' , function () 1704local first_win =api . nvim_get_current_win () 1705local first_tab =api . nvim_get_current_tabpage () 1706command ( 'tabnew' ) 1707local win =api . nvim_open_win ( 0 , true , { 1708relative ='win' , 1709win =first_win , 1710width =5 , 1711height =5 , 1712row =1 , 1713col =1 , 1714} ) 1715eq ( first_tab , api . nvim_win_get_tabpage ( win )) 1716eq ( first_tab , api . nvim_get_current_tabpage ()) 1717end ) 1718 1719local function setup_tabbed_autocmd_test () 1720local info ={} 1721info . orig_buf =api . nvim_get_current_buf () 1722info . other_buf =api . nvim_create_buf ( true , true ) 1723info . tab1_curwin =api . nvim_get_current_win () 1724info . tab1 =api . nvim_get_current_tabpage () 1725command ( 'tab split | split' ) 1726info . tab2_curwin =api . nvim_get_current_win () 1727info . tab2 =api . nvim_get_current_tabpage () 1728exec ( [=[ 1729tabfirst 1730let result = [] 1731autocmd TabEnter * let result += [["TabEnter", nvim_get_current_tabpage()]] 1732autocmd TabLeave * let result += [["TabLeave", nvim_get_current_tabpage()]] 1733autocmd WinEnter * let result += [["WinEnter", win_getid()]] 1734autocmd WinLeave * let result += [["WinLeave", win_getid()]] 1735autocmd WinNew * let result += [["WinNew", win_getid()]] 1736autocmd WinClosed * let result += [["WinClosed", str2nr(expand("<afile>"))]] 1737autocmd BufEnter * let result += [["BufEnter", win_getid(), bufnr()]] 1738autocmd BufLeave * let result += [["BufLeave", win_getid(), bufnr()]] 1739autocmd BufWinEnter * let result += [["BufWinEnter", win_getid(), bufnr()]] 1740autocmd BufWinLeave * let result += [["BufWinLeave", win_getid(), bufnr()]] 1741]=] ) 1742return info 1743end 1744 1745it ( 'noautocmd option works' , function () 1746local info =setup_tabbed_autocmd_test () 1747 1748api . nvim_open_win ( 1749info . other_buf , 1750true , 1751{ split ='left' , win =info . tab2_curwin , noautocmd =true } 1752) 1753eq ( {} , eval ( 'result' )) 1754 1755api . nvim_open_win ( 1756info . orig_buf , 1757true , 1758{ relative ='editor' , row =0 , col =0 , width =10 , height =10 , noautocmd =true } 1759) 1760eq ( {} , eval ( 'result' )) 1761end ) 1762 1763it ( 'fires expected autocmds when creating splits without entering' , function () 1764local info =setup_tabbed_autocmd_test () 1765 1766-- For these, don't want BufWinEnter if visiting the same buffer, like :{s}buffer. 1767-- Same tabpage, same buffer. 1768local new_win =api . nvim_open_win ( 0 , false , { split ='left' , win =info . tab1_curwin } ) 1769eq ( { 1770{ 'WinNew' , new_win } , 1771} , eval ( 'result' )) 1772eq ( info . tab1_curwin , api . nvim_get_current_win ()) 1773 1774-- Other tabpage, same buffer. 1775command ( 'let result = []' ) 1776new_win =api . nvim_open_win ( 0 , false , { split ='left' , win =info . tab2_curwin } ) 1777eq ( { 1778{ 'WinNew' , new_win } , 1779} , eval ( 'result' )) 1780eq ( info . tab1_curwin , api . nvim_get_current_win ()) 1781 1782-- Same tabpage, other buffer. 1783command ( 'let result = []' ) 1784new_win =api . nvim_open_win ( info . other_buf , false , { split ='left' , win =info . tab1_curwin } ) 1785eq ( { 1786{ 'WinNew' , new_win } , 1787{ 'BufWinEnter' , new_win , info . other_buf } , 1788} , eval ( 'result' )) 1789eq ( info . tab1_curwin , api . nvim_get_current_win ()) 1790 1791-- Other tabpage, other buffer. 1792command ( 'let result = []' ) 1793new_win =api . nvim_open_win ( info . other_buf , false , { split ='left' , win =info . tab2_curwin } ) 1794eq ( { 1795{ 'WinNew' , new_win } , 1796{ 'BufWinEnter' , new_win , info . other_buf } , 1797} , eval ( 'result' )) 1798eq ( info . tab1_curwin , api . nvim_get_current_win ()) 1799end ) 1800 1801it ( 'fires expected autocmds when creating and entering splits' , function () 1802local info =setup_tabbed_autocmd_test () 1803 1804-- Same tabpage, same buffer. 1805local new_win =api . nvim_open_win ( 0 , true , { split ='left' , win =info . tab1_curwin } ) 1806eq ( { 1807{ 'WinNew' , new_win } , 1808{ 'WinLeave' , info . tab1_curwin } , 1809{ 'WinEnter' , new_win } , 1810} , eval ( 'result' )) 1811 1812-- Same tabpage, other buffer. 1813api . nvim_set_current_win ( info . tab1_curwin ) 1814command ( 'let result = []' ) 1815new_win =api . nvim_open_win ( info . other_buf , true , { split ='left' , win =info . tab1_curwin } ) 1816eq ( { 1817{ 'WinNew' , new_win } , 1818{ 'WinLeave' , info . tab1_curwin } , 1819{ 'WinEnter' , new_win } , 1820{ 'BufLeave' , new_win , info . orig_buf } , 1821{ 'BufEnter' , new_win , info . other_buf } , 1822{ 'BufWinEnter' , new_win , info . other_buf } , 1823} , eval ( 'result' )) 1824 1825-- For these, the other tabpage's prevwin and curwin will change like we switched from its old 1826-- curwin to the new window, so the extra events near TabEnter reflect that. 1827-- Other tabpage, same buffer. 1828api . nvim_set_current_win ( info . tab1_curwin ) 1829command ( 'let result = []' ) 1830new_win =api . nvim_open_win ( 0 , true , { split ='left' , win =info . tab2_curwin } ) 1831eq ( { 1832{ 'WinNew' , new_win } , 1833{ 'WinLeave' , info . tab1_curwin } , 1834{ 'TabLeave' , info . tab1 } , 1835 1836{ 'WinEnter' , info . tab2_curwin } , 1837{ 'TabEnter' , info . tab2 } , 1838{ 'WinLeave' , info . tab2_curwin } , 1839{ 'WinEnter' , new_win } , 1840} , eval ( 'result' )) 1841 1842-- Other tabpage, other buffer. 1843api . nvim_set_current_win ( info . tab2_curwin ) 1844api . nvim_set_current_win ( info . tab1_curwin ) 1845command ( 'let result = []' ) 1846new_win =api . nvim_open_win ( info . other_buf , true , { split ='left' , win =info . tab2_curwin } ) 1847eq ( { 1848{ 'WinNew' , new_win } , 1849{ 'WinLeave' , info . tab1_curwin } , 1850{ 'TabLeave' , info . tab1 } , 1851 1852{ 'WinEnter' , info . tab2_curwin } , 1853{ 'TabEnter' , info . tab2 } , 1854{ 'WinLeave' , info . tab2_curwin } , 1855{ 'WinEnter' , new_win } , 1856 1857{ 'BufLeave' , new_win , info . orig_buf } , 1858{ 'BufEnter' , new_win , info . other_buf } , 1859{ 'BufWinEnter' , new_win , info . other_buf } , 1860} , eval ( 'result' )) 1861 1862-- Other tabpage, other buffer; but other tabpage's curwin has a new buffer active. 1863api . nvim_set_current_win ( info . tab2_curwin ) 1864local new_buf =api . nvim_create_buf ( true , true ) 1865api . nvim_set_current_buf ( new_buf ) 1866api . nvim_set_current_win ( info . tab1_curwin ) 1867command ( 'let result = []' ) 1868new_win =api . nvim_open_win ( info . other_buf , true , { split ='left' , win =info . tab2_curwin } ) 1869eq ( { 1870{ 'WinNew' , new_win } , 1871{ 'BufLeave' , info . tab1_curwin , info . orig_buf } , 1872{ 'WinLeave' , info . tab1_curwin } , 1873{ 'TabLeave' , info . tab1 } , 1874 1875{ 'WinEnter' , info . tab2_curwin } , 1876{ 'TabEnter' , info . tab2 } , 1877{ 'BufEnter' , info . tab2_curwin , new_buf } , 1878{ 'WinLeave' , info . tab2_curwin } , 1879{ 'WinEnter' , new_win } , 1880{ 'BufLeave' , new_win , new_buf } , 1881{ 'BufEnter' , new_win , info . other_buf } , 1882{ 'BufWinEnter' , new_win , info . other_buf } , 1883} , eval ( 'result' )) 1884end ) 1885 1886it ( 'OK when new window is moved to other tabpage by autocommands' , function () 1887-- Use nvim_win_set_config in the autocommands, as other methods of moving a window to a 1888-- different tabpage (e.g: wincmd T) actually creates a new window. 1889local tab0 =api . nvim_get_current_tabpage () 1890local tab0_win =api . nvim_get_current_win () 1891command ( 'tabnew' ) 1892local new_buf =api . nvim_create_buf ( true , true ) 1893local tab1 =api . nvim_get_current_tabpage () 1894local tab1_parent =api . nvim_get_current_win () 1895command ( 1896'tabfirst | autocmd WinNew * ++once call nvim_win_set_config(0, #{split: "left", win: ' 1897.. tab1_parent 1898.. '})' 1899) 1900local new_win =api . nvim_open_win ( new_buf , true , { split ='left' } ) 1901eq ( tab1 , api . nvim_get_current_tabpage ()) 1902eq ( new_win , api . nvim_get_current_win ()) 1903eq ( new_buf , api . nvim_get_current_buf ()) 1904 1905-- nvim_win_set_config called after entering. It doesn't follow a curwin that is moved to a 1906-- different tabpage, but instead moves to the win filling the space, which is tab0_win. 1907command ( 1908'tabfirst | autocmd WinEnter * ++once call nvim_win_set_config(0, #{split: "left", win: ' 1909.. tab1_parent 1910.. '})' 1911) 1912new_win =api . nvim_open_win ( new_buf , true , { split ='left' } ) 1913eq ( tab0 , api . nvim_get_current_tabpage ()) 1914eq ( tab0_win , api . nvim_get_current_win ()) 1915eq ( tab1 , api . nvim_win_get_tabpage ( new_win )) 1916eq ( new_buf , api . nvim_win_get_buf ( new_win )) 1917 1918command ( 1919'tabfirst | autocmd BufEnter * ++once call nvim_win_set_config(0, #{split: "left", win: ' 1920.. tab1_parent 1921.. '})' 1922) 1923new_win =api . nvim_open_win ( new_buf , true , { split ='left' } ) 1924eq ( tab0 , api . nvim_get_current_tabpage ()) 1925eq ( tab0_win , api . nvim_get_current_win ()) 1926eq ( tab1 , api . nvim_win_get_tabpage ( new_win )) 1927eq ( new_buf , api . nvim_win_get_buf ( new_win )) 1928end ) 1929 1930it ( 'does not fire BufWinEnter if win_set_buf fails' , function () 1931exec ( [[ 1932set nohidden modified 1933autocmd WinNew * ++once only! 1934let fired = v:false 1935autocmd BufWinEnter * ++once let fired = v:true 1936]] ) 1937eq ( 1938'Vim:E37: No write since last change (add ! to override)' , 1939pcall_err ( api . nvim_open_win , api . nvim_create_buf ( true , true ), false , { split ='left' } ) 1940) 1941eq ( false , eval ( 'fired' )) 1942end ) 1943 1944it ( 'fires Buf* autocommands when `!enter` if window is entered via autocommands' , function () 1945exec ( [[ 1946autocmd WinNew * ++once only! 1947let fired = v:false 1948autocmd BufEnter * ++once let fired = v:true 1949]] ) 1950api . nvim_open_win ( api . nvim_create_buf ( true , true ), false , { split ='left' } ) 1951eq ( true , eval ( 'fired' )) 1952end ) 1953 1954it ( 'no heap-use-after-free if target buffer deleted by autocommands' , function () 1955local cur_buf =api . nvim_get_current_buf () 1956local new_buf =api . nvim_create_buf ( true , true ) 1957command ( 'autocmd WinNew * ++once call nvim_buf_delete(' .. new_buf .. ', #{force: 1})' ) 1958api . nvim_open_win ( new_buf , true , { split ='left' } ) 1959eq ( cur_buf , api . nvim_get_current_buf ()) 1960end ) 1961 1962it ( 'checks if splitting disallowed' , function () 1963command ( 'split | autocmd WinEnter * ++once call nvim_open_win(0, 0, #{split: "right"})' ) 1964matches ( "E242: Can't split a window while closing another$" , pcall_err ( command , 'quit' )) 1965-- E242 is not needed for floats. 1966exec ( [[ 1967split 1968autocmd WinEnter * ++once let g:win = nvim_open_win(0, 0, #{relative: "editor", row: 0, col: 0, width: 5, height: 5}) 1969quit 1970]] ) 1971eq ( 'editor' , eval ( 'nvim_win_get_config(g:win).relative' )) 1972 1973command ( 'only | autocmd BufHidden * ++once call nvim_open_win(0, 0, #{split: "left"})' ) 1974matches ( 1975'E1159: Cannot split a window when closing the buffer$' , 1976pcall_err ( command , 'new | quit' ) 1977) 1978 1979local w =api . nvim_get_current_win () 1980command ( 1981'only | new | autocmd BufHidden * ++once call nvim_open_win(0, 0, #{split: "left", win: ' 1982.. w 1983.. '})' 1984) 1985matches ( 1986'E1159: Cannot split a window when closing the buffer$' , 1987pcall_err ( api . nvim_win_close , w , true ) 1988) 1989 1990-- OK when using a buffer that isn't closing. 1991w =api . nvim_get_current_win () 1992command ( 1993'only | autocmd BufHidden * ++once call nvim_open_win(bufnr("#"), 0, #{split: "left", win: ' 1994.. w 1995.. '})' 1996) 1997command ( 'new | quit' ) 1998 1999-- Apply to opening floats too, as that can similarly create new views into a closing buffer. 2000-- For example, the following would open a float into an unloaded buffer: 2001exec ( [[ 2002only 2003new 2004let g:buf = bufnr() 2005autocmd BufUnload * ++once call nvim_open_win(g:buf, 0, #{relative: "editor", width: 5, height: 5, row: 1, col: 1}) 2006setlocal bufhidden=unload 2007]] ) 2008matches ( 'E1159: Cannot open a float when closing the buffer$' , pcall_err ( command , 'quit' )) 2009eq ( false , eval ( 'nvim_buf_is_loaded(g:buf)' )) 2010eq ( 0 , eval ( 'win_findbuf(g:buf)->len()' )) 2011 2012-- Only checking b_locked_split for the target buffer is insufficient, as naughty autocommands 2013-- can cause win_set_buf to remain in a closing curbuf: 2014exec ( [[ 2015only 2016new 2017let g:buf = bufnr() 2018autocmd BufWipeout * ++once ++nested let g:buf2 = nvim_create_buf(1, 0) 2019\| execute 'autocmd BufLeave * ++once call nvim_buf_delete(g:buf2, #{force: 1})' 2020\| setlocal bufhidden= 2021\| call nvim_open_win(g:buf2, 1, #{relative: 'editor', width: 5, height: 5, col: 5, row: 5}) 2022setlocal bufhidden=wipe 2023]] ) 2024matches ( 'E1159: Cannot open a float when closing the buffer$' , pcall_err ( command , 'quit' )) 2025eq ( false , eval ( 'nvim_buf_is_loaded(g:buf)' )) 2026eq ( 0 , eval ( 'win_findbuf(g:buf)->len()' )) 2027-- BufLeave shouldn't run here (buf2 isn't deleted and remains hidden) 2028eq ( true , eval ( 'nvim_buf_is_loaded(g:buf2)' )) 2029eq ( 0 , eval ( 'win_findbuf(g:buf2)->len()' )) 2030end ) 2031 2032it ( 'restores last known cursor position if BufWinEnter did not move it' , function () 2033-- This test mostly exists to ensure BufWinEnter is executed before enter_buffer's epilogue. 2034local buf =api . nvim_get_current_buf () 2035insert ( [[ 2036foo 2037bar baz .etc 2038i love autocommand bugs! 2039supercalifragilisticexpialidocious 2040marvim is actually a human 2041llanfairpwllgwyngyllgogerychwyrndrobwllllantysiliogogogoch 2042]] ) 2043api . nvim_win_set_cursor ( 0 , { 5 , 2 } ) 2044command ( 'set nostartofline | enew' ) 2045local new_win =api . nvim_open_win ( buf , false , { split ='left' } ) 2046eq ( { 5 , 2 } , api . nvim_win_get_cursor ( new_win )) 2047 2048exec ( [[ 2049only! 2050autocmd BufWinEnter * ++once normal! j6l 2051]] ) 2052new_win =api . nvim_open_win ( buf , false , { split ='left' } ) 2053eq ( { 2 , 6 } , api . nvim_win_get_cursor ( new_win )) 2054end ) 2055 2056it ( 'does not block all win_set_buf autocommands if !enter and !noautocmd' , function () 2057local new_buf =fn . bufadd ( 'foobarbaz' ) 2058exec ( [[ 2059let triggered = "" 2060autocmd BufReadCmd * ++once let triggered = bufname() 2061]] ) 2062api . nvim_open_win ( new_buf , false , { split ='left' } ) 2063eq ( 'foobarbaz' , eval ( 'triggered' )) 2064end ) 2065 2066it ( 'sets error when no room' , function () 2067matches ( 'E36: Not enough room$' , pcall_err ( command , 'execute "split|"->repeat(&lines)' )) 2068matches ( 2069'E36: Not enough room$' , 2070pcall_err ( api . nvim_open_win , 0 , true , { split ='above' , win =0 } ) 2071) 2072matches ( 2073'E36: Not enough room$' , 2074pcall_err ( api . nvim_open_win , 0 , true , { split ='below' , win =0 } ) 2075) 2076end ) 2077 2078it ( "can create split window when 'winborder' is set" , function () 2079local old_win =api . nvim_get_current_win () 2080api . nvim_set_option_value ( 'winborder' , 'single' , {} ) 2081local new_win =api . nvim_open_win ( 0 , false , { split ='right' , win =0 } ) 2082eq ( { 'row' , { { 'leaf' , old_win } , { 'leaf' , new_win } } } , fn . winlayout ()) 2083eq ( '' , api . nvim_win_get_config ( new_win ). relative ) 2084end ) 2085 2086describe ( "with 'autochdir'" , function () 2087local topdir 2088local otherbuf 2089 2090before_each ( function () 2091command ( 'set shellslash' ) 2092topdir =fn . getcwd () 2093t . mkdir ( topdir .. '/Xacd' ) 2094t . mkdir ( topdir .. '/Xacd/foo' ) 2095otherbuf =api . nvim_create_buf ( false , true ) 2096api . nvim_buf_set_name ( otherbuf , topdir .. '/Xacd/baz.txt' ) 2097 2098command ( 'set autochdir' ) 2099command ( 'edit Xacd/foo/bar.txt' ) 2100eq ( topdir .. '/Xacd/foo' , fn . getcwd ()) 2101end ) 2102 2103after_each ( function () 2104n . rmdir ( topdir .. '/Xacd' ) 2105end ) 2106 2107it ( 'does not change cwd with enter=false #15280' , function () 2108api . nvim_open_win ( 2109otherbuf , 2110false , 2111{ relative ='editor' , height =5 , width =5 , row =5 , col =5 } 2112) 2113eq ( topdir .. '/Xacd/foo' , fn . getcwd ()) 2114end ) 2115 2116it ( 'changes cwd with enter=true' , function () 2117api . nvim_open_win ( 2118otherbuf , 2119true , 2120{ relative ='editor' , height =5 , width =5 , row =5 , col =5 } 2121) 2122eq ( topdir .. '/Xacd' , fn . getcwd ()) 2123end ) 2124end ) 2125 2126it ( 'no memory leak with valid title and invalid footer' , function () 2127eq ( 2128"Invalid 'title/footer': expected String or Array, got Integer" , 2129pcall_err ( api . nvim_open_win , 0 , false , { 2130relative ='editor' , 2131row =10 , 2132col =10 , 2133height =10 , 2134width =10 , 2135border ='single' , 2136title ={ { 'TITLE' } } , 2137footer =0 , 2138} ) 2139) 2140end ) 2141 2142it ( 'no memory leak with invalid title and valid footer' , function () 2143eq ( 2144"Invalid 'title/footer': expected String or Array, got Integer" , 2145pcall_err ( api . nvim_open_win , 0 , false , { 2146relative ='editor' , 2147row =10 , 2148col =10 , 2149height =10 , 2150width =10 , 2151border ='single' , 2152title =0 , 2153footer ={ { 'FOOTER' } } , 2154} ) 2155) 2156end ) 2157 2158it ( 'no crash when closing the only non-float in other tabpage #31236' , function () 2159local tp =api . nvim_get_current_tabpage () 2160local split_win =api . nvim_get_current_win () 2161local float_win =api . nvim_open_win ( 21620 , 2163false , 2164{ relative ='editor' , width =5 , height =5 , row =1 , col =1 } 2165) 2166command ( 'tabnew' ) 2167 2168api . nvim_win_close ( split_win , false ) 2169eq ( false , api . nvim_win_is_valid ( split_win )) 2170eq ( false , api . nvim_win_is_valid ( float_win )) 2171eq ( false , api . nvim_tabpage_is_valid ( tp )) 2172 2173tp =api . nvim_get_current_tabpage () 2174split_win =api . nvim_get_current_win () 2175local float_buf =api . nvim_create_buf ( true , false ) 2176float_win =api . nvim_open_win ( 2177float_buf , 2178false , 2179{ relative ='editor' , width =5 , height =5 , row =1 , col =1 } 2180) 2181-- Set these options to prevent the float from being automatically closed. 2182api . nvim_set_option_value ( 'modified' , true , { buf =float_buf } ) 2183api . nvim_set_option_value ( 'bufhidden' , 'wipe' , { buf =float_buf } ) 2184command ( 'tabnew' ) 2185 2186matches ( 2187'E5601: Cannot close window, only floating window would remain$' , 2188pcall_err ( api . nvim_win_close , split_win , false ) 2189) 2190eq ( true , api . nvim_win_is_valid ( split_win )) 2191eq ( true , api . nvim_win_is_valid ( float_win )) 2192eq ( true , api . nvim_tabpage_is_valid ( tp )) 2193 2194api . nvim_set_current_win ( float_win ) 2195api . nvim_win_close ( split_win , true ) -- Force it this time. 2196eq ( false , api . nvim_win_is_valid ( split_win )) 2197eq ( false , api . nvim_win_is_valid ( float_win )) 2198eq ( false , api . nvim_tabpage_is_valid ( tp )) 2199 2200-- Ensure opening a float after the initial check (like in WinClosed) doesn't crash... 2201exec ( [[ 2202tabnew 2203let g:tp = nvim_get_current_tabpage() 2204let g:win = win_getid() 2205tabprevious 2206autocmd! WinClosed * ++once call nvim_open_win(0, 0, #{win: g:win, relative: 'win', width: 5, height: 5, row: 5, col: 5}) 2207]] ) 2208matches ( 2209'E5601: Cannot close window, only floating window would remain$' , 2210pcall_err ( command , 'call nvim_win_close(g:win, 0)' ) 2211) 2212eq ( true , eval 'nvim_tabpage_is_valid(g:tp)' ) 2213 2214exec ( [[ 2215tabnew 2216let g:tp = nvim_get_current_tabpage() 2217let g:win = win_getid() 2218let g:buf = bufnr() 2219tabprevious 2220let s:buf2 = nvim_create_buf(0, 0) 2221call setbufvar(s:buf2, '&modified', 1) 2222call setbufvar(s:buf2, '&bufhidden', 'wipe') 2223autocmd! WinClosed * ++once call nvim_open_win(s:buf2, 0, #{win: g:win, relative: 'win', width: 5, height: 5, row: 5, col: 5}) 2224]] ) 2225matches ( 2226'E5601: Cannot close window, only floating window would remain$' , 2227pcall_err ( command , 'call nvim_buf_delete(g:buf, #{force: 1})' ) 2228) 2229eq ( true , eval 'nvim_tabpage_is_valid(g:tp)' ) 2230end ) 2231 2232it ( 'respects requested size for large splits' , function () 2233command ( 'vsplit' ) 2234local win =api . nvim_open_win ( 0 , false , { win =- 1 , split ='right' , width =38 } ) 2235eq ( 38 , api . nvim_win_get_width ( win )) 2236 2237-- No zero-sized windows (e.g: from skipping forced equalization in win_split_ins) if 2238-- requesting a chonky window; that could lead to crashes! 2239api . nvim_open_win ( 0 , false , { win =- 1 , split ='right' , width =9999 } ) 2240eq ( { 1 , 1 , 1 , 74 } , eval ( "range(1, winnr('$'))->map({_, nr -> winwidth(nr)})" )) 2241 2242command ( 'split' ) 2243win =api . nvim_open_win ( 0 , false , { win =0 , split ='below' , height =10 } ) 2244eq ( 10 , api . nvim_win_get_height ( win )) 2245 2246-- Still defaults to half-sized when no size was specified. 2247command ( 'only' ) 2248eq ( 80 , api . nvim_win_get_width ( 0 )) 2249api . nvim_open_win ( 0 , true , { split ='right' } ) 2250eq ( 40 , api . nvim_win_get_width ( 0 )) 2251 2252eq ( 22 , api . nvim_win_get_height ( 0 )) 2253api . nvim_open_win ( 0 , true , { split ='below' } ) 2254eq ( 11 , api . nvim_win_get_height ( 0 )) 2255end ) 2256 2257it ( 'no leak when win_set_buf fails and window is closed immediately' , function () 2258-- Following used to leak. 2259command ( 'autocmd BufEnter * ++once quit! | throw 1337' ) 2260eq ( 2261'Window was closed immediately' , 2262pcall_err ( 2263api . nvim_open_win , 2264api . nvim_create_buf ( true , true ), 2265true , 2266{ relative ='editor' , width =5 , height =5 , row =1 , col =1 } 2267) 2268) 2269-- If the window wasn't closed, still set errors from win_set_buf. 2270command ( 'autocmd BufEnter * ++once throw 1337' ) 2271eq ( 2272'BufEnter Autocommands for "*": 1337' , 2273pcall_err ( 2274api . nvim_open_win , 2275api . nvim_create_buf ( true , true ), 2276true , 2277{ relative ='editor' , width =5 , height =5 , row =1 , col =1 } 2278) 2279) 2280end ) 2281 2282it ( 'redraws after setting minimal style' , function () 2283local screen =Screen . new ( 10 , 10 ) 2284-- Autocommand processes pending redraws earlier than when minimal style is set, so it doesn't 2285-- implicitly rely on those. 2286exec ( [[ 2287set cursorline cursorcolumn number 2288autocmd WinNew * ++once redraw | let g:triggered = 1 2289]] ) 2290screen : expect ( [[ 2291{15: 1 }{21:^ }| 2292{1:~ }|*8 2293| 2294]] ) 2295api . nvim_open_win ( 0 , false , { style ='minimal' , split ='below' } ) 2296eq ( 1 , eval ( 'g:triggered' )) 2297screen : expect ( [[ 2298{15: 1 }{21:^ }| 2299{1:~ }|*2 2300{2:[No Name] }| 2301|*4 2302{2:[No Name] }| 2303| 2304]] ) 2305-- Also check nvim_win_set_config: only set style to avoid redraws from other config fields. 2306api . nvim_win_set_config ( 0 , { style ='minimal' } ) 2307screen : expect ( [[ 2308^ | 2309|*2 2310{3:[No Name] }| 2311|*4 2312{2:[No Name] }| 2313| 2314]] ) 2315end ) 2316 2317it ( 'keep focus when creating split window with enter=false in init script' , function () 2318local script_file ='Xstartup.lua' 2319t . write_file ( 2320script_file , 2321[[ 2322vim.o.laststatus = 0 2323local enter = vim.g.test_enter 2324local win = vim.api.nvim_open_win(vim.api.nvim_create_buf(false, true), enter, { 2325split = 'left', 2326win = 0, 2327}) 2328]] 2329) 2330finally ( function () 2331os . remove ( script_file ) 2332end ) 2333local screen =tt . setup_child_nvim ( { 2334'--clean' , 2335'--cmd' , 2336'let g:test_enter = v:false' , 2337'-u' , 2338script_file , 2339} ) 2340screen : expect ( [[ 2341│^ | 2342~ │~ |*4 23430,0-1 All| 2344{5:-- TERMINAL --} | 2345]] ) 2346screen : detach () 2347screen =tt . setup_child_nvim ( { 2348'--clean' , 2349'--cmd' , 2350'let g:test_enter = v:true' , 2351'-u' , 2352script_file , 2353} ) 2354screen : expect ( [[ 2355^ │ | 2356~ │~ |*4 23570,0-1 All| 2358{5:-- TERMINAL --} | 2359]] ) 2360end ) 2361end ) 2362 2363describe ( 'set_config' , function () 2364it ( "uses 'winborder' when converting a split to a floating window" , function () 2365api . nvim_set_option_value ( 'winborder' , 'single' , {} ) 2366command ( 'split' ) 2367local winid =api . nvim_get_current_win () 2368-- Convert split to float without specifying border 2369api . nvim_win_set_config ( winid , { 2370relative ='editor' , 2371row =2 , 2372col =2 , 2373width =10 , 2374height =5 , 2375} ) 2376local config =api . nvim_win_get_config ( winid ) 2377eq ( '┌' , config . border [ 1 ]) 2378end ) 2379 2380it ( 'erases border of a floating window when converting to split window' , function () 2381api . nvim_set_option_value ( 'winborder' , 'single' , {} ) 2382local winid =api . nvim_open_win ( api . nvim_create_buf ( false , false ), false , { 2383relative ='editor' , 2384row =2 , 2385col =2 , 2386width =10 , 2387height =5 , 2388} ) 2389local config =api . nvim_win_get_config ( winid ) 2390eq ( '┌' , config . border [ 1 ]) 2391api . nvim_win_set_config ( winid , { split ='right' , win =0 } ) 2392config =api . nvim_win_get_config ( winid ) 2393eq ( nil , config . border ) 2394end ) 2395 2396it ( 'moves a split into a float' , function () 2397local win =api . nvim_open_win ( 0 , true , { 2398vertical =false , 2399} ) 2400eq ( '' , api . nvim_win_get_config ( win ). relative ) 2401api . nvim_win_set_config ( win , { 2402relative ='editor' , 2403row =5 , 2404col =5 , 2405width =5 , 2406height =5 , 2407} ) 2408eq ( 'editor' , api . nvim_win_get_config ( win ). relative ) 2409end ) 2410 2411it ( 'throws error when attempting to move the last non-floating window' , function () 2412local err =pcall_err ( api . nvim_win_set_config , 0 , { 2413vertical =false , 2414} ) 2415eq ( 'Cannot move last non-floating window' , err ) 2416 2417local win1 =api . nvim_get_current_win () 2418command ( 'tabnew' ) 2419eq ( 2420'Cannot move last non-floating window' , 2421pcall_err ( api . nvim_win_set_config , 0 , { win =win1 , split ='left' } ) 2422) 2423api . nvim_open_win ( 0 , false , { relative ='editor' , width =5 , height =5 , row =1 , col =1 } ) 2424eq ( 2425'Cannot move last non-floating window' , 2426pcall_err ( api . nvim_win_set_config , 0 , { win =win1 , split ='left' } ) 2427) 2428 2429-- If it's no longer the last non-float, still an error if autocommands make it the last 2430-- non-float again before it's moved. 2431command ( 'vsplit' ) 2432exec_lua ( function () 2433vim . api . nvim_create_autocmd ( 'WinEnter' , { 2434once =true , 2435callback =function () 2436vim . api . nvim_win_set_config ( 24370 , 2438{ relative ='editor' , width =5 , height =5 , row =1 , col =1 } 2439) 2440end , 2441} ) 2442end ) 2443eq ( 2444'Cannot move last non-floating window' , 2445pcall_err ( api . nvim_win_set_config , 0 , { win =win1 , split ='left' } ) 2446) 2447end ) 2448 2449it ( 'passing retval of get_config results in no-op' , function () 2450-- simple split layout 2451local win =api . nvim_open_win ( 0 , true , { 2452split ='left' , 2453} ) 2454local layout =fn . winlayout () 2455local config =api . nvim_win_get_config ( win ) 2456api . nvim_win_set_config ( win , config ) 2457eq ( layout , fn . winlayout ()) 2458 2459-- nested split layout 2460local win2 =api . nvim_open_win ( 0 , true , { 2461vertical =true , 2462} ) 2463local win3 =api . nvim_open_win ( 0 , true , { 2464win =win2 , 2465vertical =false , 2466} ) 2467layout =fn . winlayout () 2468config =api . nvim_win_get_config ( win2 ) 2469api . nvim_win_set_config ( win2 , config ) 2470eq ( layout , fn . winlayout ()) 2471 2472config =api . nvim_win_get_config ( win3 ) 2473api . nvim_win_set_config ( win3 , config ) 2474eq ( layout , fn . winlayout ()) 2475end ) 2476 2477it ( 'moves a float into a split' , function () 2478local layout =fn . winlayout () 2479eq ( 'leaf' , layout [ 1 ]) 2480local win =api . nvim_open_win ( 0 , true , { 2481relative ='editor' , 2482row =5 , 2483col =5 , 2484width =5 , 2485height =5 , 2486} ) 2487api . nvim_win_set_config ( win , { 2488split ='below' , 2489win =- 1 , 2490} ) 2491eq ( '' , api . nvim_win_get_config ( win ). relative ) 2492layout =fn . winlayout () 2493eq ( 'col' , layout [ 1 ]) 2494eq ( 2 , # layout [ 2 ]) 2495eq ( win , layout [ 2 ][ 2 ][ 2 ]) 2496end ) 2497 2498it ( 'respects the "split" option' , function () 2499local layout =fn . winlayout () 2500eq ( 'leaf' , layout [ 1 ]) 2501local first_win =layout [ 2 ] 2502local win =api . nvim_open_win ( 0 , true , { 2503relative ='editor' , 2504row =5 , 2505col =5 , 2506width =5 , 2507height =5 , 2508} ) 2509api . nvim_win_set_config ( win , { 2510split ='right' , 2511win =first_win , 2512} ) 2513layout =fn . winlayout () 2514eq ( 'row' , layout [ 1 ]) 2515eq ( 2 , # layout [ 2 ]) 2516eq ( win , layout [ 2 ][ 2 ][ 2 ]) 2517local config =api . nvim_win_get_config ( win ) 2518eq ( '' , config . relative ) 2519eq ( 'right' , config . split ) 2520api . nvim_win_set_config ( win , { 2521split ='below' , 2522win =first_win , 2523} ) 2524layout =fn . winlayout () 2525eq ( 'col' , layout [ 1 ]) 2526eq ( 2 , # layout [ 2 ]) 2527eq ( win , layout [ 2 ][ 2 ][ 2 ]) 2528config =api . nvim_win_get_config ( win ) 2529eq ( '' , config . relative ) 2530eq ( 'below' , config . split ) 2531 2532eq ( 2533"Required: non-float with 'win' requires 'split' or 'vertical'" , 2534pcall_err ( api . nvim_win_set_config , 0 , { win =0 } ) 2535) 2536eq ( 2537"Required: non-float with 'win' requires 'split' or 'vertical'" , 2538pcall_err ( api . nvim_win_set_config , 0 , { win =0 , relative ='' } ) 2539) 2540 2541-- "minimal" style takes effect immediately for a split. 2542api . nvim_set_option_value ( 'cursorline' , true , { win =win , scope ='local' } ) 2543eq ( true , api . nvim_get_option_value ( 'cursorline' , { win =win } )) 2544api . nvim_win_set_config ( win , { style ='minimal' } ) 2545eq ( false , api . nvim_get_option_value ( 'cursorline' , { win =win } )) 2546end ) 2547 2548it ( 'creates top-level splits' , function () 2549local win =api . nvim_open_win ( 0 , true , { 2550vertical =false , 2551} ) 2552local win2 =api . nvim_open_win ( 0 , true , { 2553vertical =true , 2554win =- 1 , 2555} ) 2556local layout =fn . winlayout () 2557eq ( 'row' , layout [ 1 ]) 2558eq ( 2 , # layout [ 2 ]) 2559eq ( win2 , layout [ 2 ][ 1 ][ 2 ]) 2560api . nvim_win_set_config ( win , { 2561split ='below' , 2562win =- 1 , 2563} ) 2564layout =fn . winlayout () 2565eq ( 'col' , layout [ 1 ]) 2566eq ( 2 , # layout [ 2 ]) 2567eq ( 'row' , layout [ 2 ][ 1 ][ 1 ]) 2568eq ( win , layout [ 2 ][ 2 ][ 2 ]) 2569end ) 2570 2571it ( 'moves windows to other tabpages' , function () 2572local first_tab =api . nvim_get_current_tabpage () 2573local first_win =api . nvim_get_current_win () 2574local win =api . nvim_open_win ( 0 , false , { split ='left' } ) 2575command ( 'tabnew' ) 2576local new_tab =api . nvim_get_current_tabpage () 2577local tab2_win =api . nvim_get_current_win () 2578api . nvim_set_current_tabpage ( first_tab ) 2579-- move new win to new tabpage 2580api . nvim_win_set_config ( win , { split ='right' , win =api . nvim_tabpage_get_win ( new_tab ) } ) 2581eq ( new_tab , api . nvim_win_get_tabpage ( win )) 2582-- we are changing the config, the current tabpage should not change 2583eq ( first_tab , api . nvim_get_current_tabpage ()) 2584 2585api . nvim_set_current_tabpage ( new_tab ) 2586local layout =fn . winlayout () 2587eq ( { 2588'row' , 2589{ 2590{ 'leaf' , api . nvim_tabpage_get_win ( new_tab ) } , 2591{ 'leaf' , win } , 2592} , 2593} , layout ) 2594 2595-- directly convert split into a float for a different tabpage 2596local win2 =api . nvim_open_win ( 0 , true , { split ='below' } ) 2597eq ( '' , api . nvim_win_get_config ( win2 ). relative ) 2598api . nvim_win_set_config ( 2599win2 , 2600{ relative ='editor' , row =0 , col =0 , width =1 , height =1 , win =first_win } 2601) 2602eq ( first_tab , api . nvim_win_get_tabpage ( win2 )) 2603eq ( 'editor' , api . nvim_win_get_config ( win2 ). relative ) 2604eq ( { first_win , win2 } , api . nvim_tabpage_list_wins ( first_tab )) 2605eq ( { tab2_win , win } , api . nvim_tabpage_list_wins ( new_tab )) 2606 2607-- convert new win to float in new tabpage 2608api . nvim_win_set_config ( win , { relative ='editor' , row =2 , col =2 , height =2 , width =2 } ) 2609api . nvim_set_current_tabpage ( first_tab ) 2610-- move to other tabpage 2611api . nvim_win_set_config ( win , { win =first_win } ) 2612eq ( first_tab , api . nvim_win_get_tabpage ( win )) 2613eq ( { first_win , win , win2 } , api . nvim_tabpage_list_wins ( first_tab )) 2614eq ( { tab2_win } , api . nvim_tabpage_list_wins ( new_tab )) 2615-- unlike splits, negative win is invalid 2616eq ( 'Invalid window id: -1' , pcall_err ( api . nvim_win_set_config , win , { win =- 1 } )) 2617 2618-- can't convert only window in other tabpage to float 2619command ( 'tabnew' ) 2620local only_win =api . nvim_get_current_win () 2621command ( 'tabprevious' ) 2622eq ( 2623'Cannot change last window into float' , 2624pcall_err ( 2625api . nvim_win_set_config , 2626only_win , 2627{ relative ='editor' , width =5 , height =5 , row =0 , col =0 } 2628) 2629) 2630end ) 2631 2632it ( 'correctly moves curwin when moving curwin to a different tabpage' , function () 2633local tab1 =api . nvim_get_current_tabpage () 2634local tab1_win =api . nvim_get_current_win () 2635command ( 'tabnew' ) 2636local tab2 =api . nvim_get_current_tabpage () 2637local tab2_win =api . nvim_get_current_win () 2638api . nvim_set_current_tabpage ( tab1 ) -- return to the initial tab 2639-- create and enter a new split 2640local win =api . nvim_open_win ( 0 , true , { 2641vertical =false , 2642} ) 2643 2644eq ( tab1 , api . nvim_win_get_tabpage ( win )) 2645eq ( { win , tab1_win } , api . nvim_tabpage_list_wins ( tab1 )) 2646 2647-- move the current win to a different tabpage 2648api . nvim_win_set_config ( win , { 2649split ='right' , 2650win =api . nvim_tabpage_get_win ( tab2 ), 2651} ) 2652eq ( tab1 , api . nvim_get_current_tabpage ()) 2653 2654-- win should have moved to tab2 2655eq ( tab2 , api . nvim_win_get_tabpage ( win )) 2656-- tp_curwin of tab2 should not have changed 2657eq ( tab2_win , api . nvim_tabpage_get_win ( tab2 )) 2658-- win lists should be correct 2659eq ( { tab2_win , win } , api . nvim_tabpage_list_wins ( tab2 )) 2660eq ( { tab1_win } , api . nvim_tabpage_list_wins ( tab1 )) 2661-- current win should have moved to neighboring win 2662eq ( tab1_win , api . nvim_tabpage_get_win ( tab1 )) 2663 2664api . nvim_set_current_tabpage ( tab2 ) 2665-- convert new win to float 2666api . nvim_win_set_config ( win , { relative ='editor' , row =2 , col =2 , height =2 , width =2 } ) 2667api . nvim_set_current_win ( win ) 2668api . nvim_win_set_config ( win , { relative ='win' , win =tab1_win , row =3 , col =3 } ) 2669eq ( tab1 , api . nvim_win_get_tabpage ( win )) 2670eq ( tab2 , api . nvim_get_current_tabpage ()) 2671eq ( { tab1_win , win } , api . nvim_tabpage_list_wins ( tab1 )) 2672end ) 2673 2674it ( 'splits windows in non-current tabpage' , function () 2675local curtab =api . nvim_get_current_tabpage () 2676command ( 'tabnew' ) 2677local tabnr =api . nvim_get_current_tabpage () 2678command ( 'tabprev' ) -- return to the initial tab 2679 2680local win =api . nvim_open_win ( 0 , false , { 2681vertical =false , 2682win =api . nvim_tabpage_get_win ( tabnr ), 2683} ) 2684 2685eq ( tabnr , api . nvim_win_get_tabpage ( win )) 2686-- since enter = false, the current tabpage should not change 2687eq ( curtab , api . nvim_get_current_tabpage ()) 2688end ) 2689 2690it ( 'moves the current split window' , function () 2691local initial_win =api . nvim_get_current_win () 2692local win =api . nvim_open_win ( 0 , true , { 2693vertical =true , 2694} ) 2695local win2 =api . nvim_open_win ( 0 , true , { 2696vertical =true , 2697} ) 2698api . nvim_set_current_win ( win ) 2699eq ( { 2700'row' , 2701{ 2702{ 'leaf' , win2 } , 2703{ 'leaf' , win } , 2704{ 'leaf' , initial_win } , 2705} , 2706} , fn . winlayout ()) 2707 2708api . nvim_win_set_config ( 0 , { 2709vertical =false , 2710win =0 , 2711} ) 2712eq ( win , api . nvim_get_current_win ()) 2713eq ( { 2714'col' , 2715{ 2716{ 'leaf' , win } , 2717{ 2718'row' , 2719{ 2720{ 'leaf' , win2 } , 2721{ 'leaf' , initial_win } , 2722} , 2723} , 2724} , 2725} , fn . winlayout ()) 2726 2727api . nvim_set_current_win ( win2 ) 2728local win3 =api . nvim_open_win ( 0 , true , { 2729vertical =true , 2730} ) 2731eq ( win3 , api . nvim_get_current_win ()) 2732 2733eq ( { 2734'col' , 2735{ 2736{ 'leaf' , win } , 2737{ 2738'row' , 2739{ 2740{ 'leaf' , win3 } , 2741{ 'leaf' , win2 } , 2742{ 'leaf' , initial_win } , 2743} , 2744} , 2745} , 2746} , fn . winlayout ()) 2747 2748api . nvim_win_set_config ( 0 , { 2749vertical =false , 2750win =0 , 2751} ) 2752 2753eq ( win3 , api . nvim_get_current_win ()) 2754eq ( { 2755'col' , 2756{ 2757{ 'leaf' , win } , 2758{ 2759'row' , 2760{ 2761{ 2762'col' , 2763{ 2764{ 'leaf' , win3 } , 2765{ 'leaf' , win2 } , 2766} , 2767} , 2768{ 'leaf' , initial_win } , 2769} , 2770} , 2771} , 2772} , fn . winlayout ()) 2773end ) 2774 2775it ( 'closing new curwin when moving window to other tabpage works' , function () 2776command ( 'split | tabnew' ) 2777local t2_win =api . nvim_get_current_win () 2778command ( 'tabfirst | autocmd WinEnter * ++once quit' ) 2779local t1_move_win =api . nvim_get_current_win () 2780-- win_set_config fails to switch away from "t1_move_win" because the WinEnter autocmd that 2781-- closed the window we're switched to returns us to "t1_move_win", as it filled the space. 2782eq ( 2783'Failed to switch away from window ' .. t1_move_win , 2784pcall_err ( api . nvim_win_set_config , t1_move_win , { win =t2_win , split ='left' } ) 2785) 2786eq ( t1_move_win , api . nvim_get_current_win ()) 2787 2788command ( 'split | split | autocmd WinEnter * ++once quit' ) 2789t1_move_win =api . nvim_get_current_win () 2790-- In this case, we closed the window that we got switched to, but doing so didn't switch us 2791-- back to "t1_move_win", which is fine. 2792api . nvim_win_set_config ( t1_move_win , { win =t2_win , split ='left' } ) 2793neq ( t1_move_win , api . nvim_get_current_win ()) 2794end ) 2795 2796it ( 'messing with "win" or "parent" when moving "win" to other tabpage' , function () 2797command ( 'split | tabnew' ) 2798local t2_win1 =api . nvim_get_current_win () 2799command ( 'split' ) 2800local t2_win2 =api . nvim_get_current_win () 2801command ( 'split' ) 2802local t2_win3 =api . nvim_get_current_win () 2803 2804command ( 'tabfirst | autocmd WinEnter * ++once call nvim_win_close(' .. t2_win1 .. ', 1)' ) 2805local cur_win =api . nvim_get_current_win () 2806eq ( 2807'Windows to split were closed' , 2808pcall_err ( api . nvim_win_set_config , 0 , { win =t2_win1 , split ='left' } ) 2809) 2810eq ( cur_win , api . nvim_get_current_win ()) 2811 2812command ( 'split | autocmd WinLeave * ++once quit!' ) 2813cur_win =api . nvim_get_current_win () 2814eq ( 2815'Windows to split were closed' , 2816pcall_err ( api . nvim_win_set_config , 0 , { win =t2_win2 , split ='left' } ) 2817) 2818neq ( cur_win , api . nvim_get_current_win ()) 2819 2820exec ( [[ 2821split 2822autocmd WinLeave * ++once 2823\ call nvim_win_set_config(0, #{relative:'editor', row:0, col:0, width:5, height:5}) 2824]] ) 2825cur_win =api . nvim_get_current_win () 2826eq ( 2827'Floating state of windows to split changed' , 2828pcall_err ( api . nvim_win_set_config , 0 , { win =t2_win3 , split ='left' } ) 2829) 2830eq ( 'editor' , api . nvim_win_get_config ( 0 ). relative ) 2831eq ( cur_win , api . nvim_get_current_win ()) 2832 2833command ( 'autocmd WinLeave * ++once wincmd J' ) 2834cur_win =api . nvim_get_current_win () 2835eq ( 2836'Floating state of windows to split changed' , 2837pcall_err ( api . nvim_win_set_config , 0 , { win =t2_win3 , split ='left' } ) 2838) 2839eq ( '' , api . nvim_win_get_config ( 0 ). relative ) 2840eq ( cur_win , api . nvim_get_current_win ()) 2841 2842-- Try to make "parent" floating. This should give the same error as before. 2843exec (( [[ 2844autocmd WinLeave * ++once 2845\ call nvim_win_set_config(%d, #{relative:'editor', row:0, col:0, width:5, height:5}) 2846]] ): format ( t2_win3 )) 2847cur_win =api . nvim_get_current_win () 2848eq ( 2849'Floating state of windows to split changed' , 2850pcall_err ( api . nvim_win_set_config , 0 , { win =t2_win3 , split ='left' } ) 2851) 2852eq ( 'editor' , api . nvim_win_get_config ( t2_win3 ). relative ) 2853eq ( '' , api . nvim_win_get_config ( 0 ). relative ) 2854eq ( cur_win , api . nvim_get_current_win ()) 2855end ) 2856 2857it ( 'expected autocmds when moving window to other tabpage' , function () 2858local new_curwin =api . nvim_get_current_win () 2859command ( 'split' ) 2860local win =api . nvim_get_current_win () 2861command ( 'tabnew' ) 2862local parent =api . nvim_get_current_win () 2863exec ( [[ 2864tabfirst 2865let result = [] 2866autocmd WinEnter * let result += ["Enter", win_getid()] 2867autocmd WinLeave * let result += ["Leave", win_getid()] 2868autocmd WinNew * let result += ["New", win_getid()] 2869]] ) 2870api . nvim_win_set_config ( 0 , { win =parent , split ='left' } ) 2871-- Shouldn't see WinNew, as we're not creating any new windows, just moving existing ones. 2872eq ( { 'Leave' , win , 'Enter' , new_curwin } , eval ( 'result' )) 2873end ) 2874 2875it ( 'no autocmds when moving window in same or other tabpage' , function () 2876local parent =api . nvim_get_current_win () 2877exec ( [[ 2878split 2879let g:result = [] 2880autocmd WinEnter * let g:result += ["Enter", win_getid()] 2881autocmd WinLeave * let g:result += ["Leave", win_getid()] 2882autocmd WinNew * let g:result += ["New", win_getid()] 2883]] ) 2884api . nvim_win_set_config ( 0 , { win =parent , split ='left' } ) 2885-- Shouldn't see any of those events, as we remain in the same window. 2886eq ( {} , eval ( 'g:result' )) 2887 2888-- move float window from tab2 to tab1 2889command ( 'tabdo only' ) 2890local tab1 =api . nvim_get_current_tabpage () 2891local tab1_win1 =api . nvim_get_current_win () 2892command ( 'tabnew' ) 2893local fwin =api . nvim_open_win ( 0 , false , { 2894relative ='editor' , 2895row =2 , 2896col =2 , 2897height =2 , 2898width =2 , 2899} ) 2900api . nvim_set_current_tabpage ( tab1 ) 2901api . nvim_set_var ( 'result' , {} ) 2902api . nvim_win_set_config ( fwin , { win =tab1_win1 } ) 2903eq ( {} , eval ( 'g:result' )) 2904end ) 2905 2906it ( 'checks if splitting disallowed' , function () 2907command ( 'split | autocmd WinEnter * ++once call nvim_win_set_config(0, #{split: "right"})' ) 2908matches ( "E242: Can't split a window while closing another$" , pcall_err ( command , 'quit' )) 2909 2910command ( 'autocmd BufHidden * ++once call nvim_win_set_config(0, #{split: "left"})' ) 2911matches ( 2912'E1159: Cannot split a window when closing the buffer$' , 2913pcall_err ( command , 'new | quit' ) 2914) 2915 2916-- OK when using window to different buffer. 2917local w =api . nvim_get_current_win () 2918command ( 'autocmd BufHidden * ++once call nvim_win_set_config(' .. w .. ', #{split: "left"})' ) 2919command ( 'new | quit' ) 2920end ) 2921 2922--- Returns a function to get information about the window layout, sizes and positions of a 2923--- tabpage. 2924local function define_tp_info_function () 2925exec_lua ( [[ 2926function tp_info(tp) 2927return { 2928layout = vim.fn.winlayout(vim.api.nvim_tabpage_get_number(tp)), 2929pos_sizes = vim.tbl_map( 2930function(w) 2931local pos = vim.fn.win_screenpos(w) 2932return { 2933row = pos[1], 2934col = pos[2], 2935width = vim.fn.winwidth(w), 2936height = vim.fn.winheight(w) 2937} 2938end, 2939vim.api.nvim_tabpage_list_wins(tp) 2940) 2941} 2942end 2943]] ) 2944 2945return function ( tp ) 2946return exec_lua ( 'return tp_info(...)' , tp ) 2947end 2948end 2949 2950it ( 'attempt to move window with no room' , function () 2951-- Fill the 2nd tabpage full of windows until we run out of room. 2952-- Use &laststatus=0 to ensure restoring missing statuslines doesn't affect things. 2953command ( 'set laststatus=0 | tabnew' ) 2954matches ( 'E36: Not enough room$' , pcall_err ( command , 'execute "split|"->repeat(&lines)' )) 2955command ( 'vsplit | wincmd | | wincmd p' ) 2956local t2 =api . nvim_get_current_tabpage () 2957local t2_cur_win =api . nvim_get_current_win () 2958local t2_top_split =fn . win_getid ( 1 ) 2959local t2_bot_split =fn . win_getid ( fn . winnr ( '$' )) 2960local t2_float =api . nvim_open_win ( 29610 , 2962false , 2963{ relative ='editor' , row =0 , col =0 , width =10 , height =10 } 2964) 2965local t2_float_config =api . nvim_win_get_config ( t2_float ) 2966local tp_info =define_tp_info_function () 2967local t2_info =tp_info ( t2 ) 2968matches ( 2969'E36: Not enough room$' , 2970pcall_err ( api . nvim_win_set_config , 0 , { win =t2_top_split , split ='above' } ) 2971) 2972matches ( 2973'E36: Not enough room$' , 2974pcall_err ( api . nvim_win_set_config , 0 , { win =t2_top_split , split ='below' } ) 2975) 2976matches ( 2977'E36: Not enough room$' , 2978pcall_err ( api . nvim_win_set_config , 0 , { win =t2_bot_split , split ='above' } ) 2979) 2980matches ( 2981'E36: Not enough room$' , 2982pcall_err ( api . nvim_win_set_config , 0 , { win =t2_bot_split , split ='below' } ) 2983) 2984matches ( 2985'E36: Not enough room$' , 2986pcall_err ( api . nvim_win_set_config , t2_float , { win =t2_top_split , split ='above' } ) 2987) 2988matches ( 2989'E36: Not enough room$' , 2990pcall_err ( api . nvim_win_set_config , t2_float , { win =t2_top_split , split ='below' } ) 2991) 2992matches ( 2993'E36: Not enough room$' , 2994pcall_err ( api . nvim_win_set_config , t2_float , { win =t2_bot_split , split ='above' } ) 2995) 2996matches ( 2997'E36: Not enough room$' , 2998pcall_err ( api . nvim_win_set_config , t2_float , { win =t2_bot_split , split ='below' } ) 2999) 3000eq ( t2_cur_win , api . nvim_get_current_win ()) 3001eq ( t2_info , tp_info ( t2 )) 3002eq ( t2_float_config , api . nvim_win_get_config ( t2_float )) 3003 3004-- Try to move windows from the 1st tabpage to the 2nd. 3005command ( 'tabfirst | split | wincmd _' ) 3006local t1 =api . nvim_get_current_tabpage () 3007local t1_cur_win =api . nvim_get_current_win () 3008local t1_float =api . nvim_open_win ( 30090 , 3010false , 3011{ relative ='editor' , row =5 , col =3 , width =7 , height =6 } 3012) 3013local t1_float_config =api . nvim_win_get_config ( t1_float ) 3014local t1_info =tp_info ( t1 ) 3015matches ( 3016'E36: Not enough room$' , 3017pcall_err ( api . nvim_win_set_config , 0 , { win =t2_top_split , split ='above' } ) 3018) 3019matches ( 3020'E36: Not enough room$' , 3021pcall_err ( api . nvim_win_set_config , 0 , { win =t2_top_split , split ='below' } ) 3022) 3023matches ( 3024'E36: Not enough room$' , 3025pcall_err ( api . nvim_win_set_config , 0 , { win =t2_bot_split , split ='above' } ) 3026) 3027matches ( 3028'E36: Not enough room$' , 3029pcall_err ( api . nvim_win_set_config , 0 , { win =t2_bot_split , split ='below' } ) 3030) 3031matches ( 3032'E36: Not enough room$' , 3033pcall_err ( api . nvim_win_set_config , t1_float , { win =t2_top_split , split ='above' } ) 3034) 3035matches ( 3036'E36: Not enough room$' , 3037pcall_err ( api . nvim_win_set_config , t1_float , { win =t2_top_split , split ='below' } ) 3038) 3039matches ( 3040'E36: Not enough room$' , 3041pcall_err ( api . nvim_win_set_config , t1_float , { win =t2_bot_split , split ='above' } ) 3042) 3043matches ( 3044'E36: Not enough room$' , 3045pcall_err ( api . nvim_win_set_config , t1_float , { win =t2_bot_split , split ='below' } ) 3046) 3047eq ( t1_cur_win , api . nvim_get_current_win ()) 3048eq ( t1_info , tp_info ( t1 )) 3049eq ( t1_float_config , api . nvim_win_get_config ( t1_float )) 3050end ) 3051 3052it ( 'attempt to move window from other tabpage with no room' , function () 3053-- Fill up the 1st tabpage with horizontal splits, then create a 2nd with only a few. Go back 3054-- to the 1st and try to move windows from the 2nd (while it's non-current) to it. Check that 3055-- window positions and sizes in the 2nd are unchanged. 3056command ( 'set laststatus=0' ) 3057matches ( 'E36: Not enough room$' , pcall_err ( command , 'execute "split|"->repeat(&lines)' )) 3058 3059command ( 'tab split' ) 3060local t2 =api . nvim_get_current_tabpage () 3061local t2_top =api . nvim_get_current_win () 3062command ( 'belowright split' ) 3063local t2_mid_left =api . nvim_get_current_win () 3064command ( 'belowright vsplit' ) 3065local t2_mid_right =api . nvim_get_current_win () 3066command ( 'split | wincmd J' ) 3067local t2_bot =api . nvim_get_current_win () 3068local tp_info =define_tp_info_function () 3069local t2_info =tp_info ( t2 ) 3070eq ( { 3071'col' , 3072{ 3073{ 'leaf' , t2_top } , 3074{ 3075'row' , 3076{ 3077{ 'leaf' , t2_mid_left } , 3078{ 'leaf' , t2_mid_right } , 3079} , 3080} , 3081{ 'leaf' , t2_bot } , 3082} , 3083} , t2_info . layout ) 3084 3085local function try_move_t2_wins_to_t1 () 3086for _ , w in ipairs ( { t2_bot , t2_mid_left , t2_mid_right , t2_top } ) do 3087matches ( 3088'E36: Not enough room$' , 3089pcall_err ( api . nvim_win_set_config , w , { win =0 , split ='below' } ) 3090) 3091eq ( t2_info , tp_info ( t2 )) 3092end 3093end 3094command ( 'tabfirst' ) 3095try_move_t2_wins_to_t1 () 3096-- Go to the 2nd tabpage to ensure nothing changes after win_comp_pos, last_status, .etc. 3097-- from enter_tabpage. 3098command ( 'tabnext' ) 3099eq ( t2_info , tp_info ( t2 )) 3100 3101-- Check things are fine with the global statusline too, for good measure. 3102-- Set it while the 2nd tabpage is current, so last_status runs for it. 3103command ( 'set laststatus=3' ) 3104t2_info =tp_info ( t2 ) 3105command ( 'tabfirst' ) 3106try_move_t2_wins_to_t1 () 3107end ) 3108 3109it ( 'handles textlock' , function () 3110command ( 'tabnew' ) 3111local t2_win =api . nvim_get_current_win () 3112command ( 'tabfirst' ) 3113 3114exec (( [[ 3115new 3116call setline(1, 'foo') 3117setlocal debug=throw indentexpr=nvim_win_set_config(0,#{split:'left',win:%d}) 3118]] ): format ( t2_win )) 3119local cur_win =api . nvim_get_current_win () 3120matches ( 3121'E565: Not allowed to change text or change window$' , 3122pcall_err ( command , 'normal! ==' ) 3123) 3124eq ( cur_win , api . nvim_get_current_win ()) 3125exec (( [[ 3126wincmd p 3127call setline(1, 'bar') 3128setlocal indentexpr=nvim_win_set_config(win_getid(winnr('#')),#{split:'left',win:%d}) 3129]] ): format ( t2_win )) 3130neq ( cur_win , api . nvim_get_current_win ()) 3131matches ( 3132'E565: Not allowed to change text or change window$' , 3133pcall_err ( command , 'normal! ==' ) 3134) 3135-- expr_map_lock 3136exec (( [[ 3137nnoremap <expr> @ nvim_win_set_config(win_getid(winnr('#')),#{split:'left',win:%d}) 3138]] ): format ( t2_win )) 3139neq ( cur_win , api . nvim_get_current_win ()) 3140matches ( 3141'E565: Not allowed to change text or change window$' , 3142pcall_err ( fn . feedkeys , '@' , 'x' ) 3143) 3144 3145exec (( [[ 3146wincmd p 3147autocmd WinNewPre * ++once call nvim_win_set_config(0, #{relative:'editor', win:%d, row:0, col:0, width:1, height:1}) 3148]] ): format ( t2_win )) 3149matches ( 3150'E1312: Not allowed to change the window layout in this autocmd$' , 3151pcall_err ( command , 'split' ) 3152) 3153eq ( cur_win , api . nvim_get_current_win ()) -- :split didn't enter new window due to error 3154 3155exec (( [[ 3156autocmd WinLeave * ++once call nvim_win_set_config(0, #{relative:'editor', win:%d, row:0, col:0, width:1, height:1}) 3157]] ): format ( t2_win )) 3158matches ( 'Cannot move window to another tabpage whilst in use$' , pcall_err ( command , 'quit' )) 3159eq ( cur_win , api . nvim_get_current_win ()) -- :quit didn't close window due to error 3160end ) 3161 3162it ( 'updates statusline when moving bottom split' , function () 3163local screen =Screen . new ( 10 , 10 ) 3164exec ( [[ 3165set laststatus=0 3166belowright split 3167call nvim_win_set_config(0, #{split: 'above', win: win_getid(winnr('#'))}) 3168]] ) 3169screen : expect ( [[ 3170^ | 3171{1:~ }|*3 3172{3:[No Name] }| 3173| 3174{1:~ }|*3 3175| 3176]] ) 3177end ) 3178 3179it ( "updates tp_curwin of moved window's original tabpage" , function () 3180local t1 =api . nvim_get_current_tabpage () 3181command ( 'tab split | split' ) 3182local t2 =api . nvim_get_current_tabpage () 3183local t2_alt_win =api . nvim_get_current_win () 3184command ( 'vsplit' ) 3185local t2_cur_win =api . nvim_get_current_win () 3186command ( 'tabprevious' ) 3187eq ( t2_cur_win , api . nvim_tabpage_get_win ( t2 )) 3188 3189-- tp_curwin is unchanged when moved within the same tabpage. 3190api . nvim_win_set_config ( t2_cur_win , { split ='left' , win =t2_alt_win } ) 3191eq ( t2_cur_win , api . nvim_tabpage_get_win ( t2 )) 3192 3193-- Also unchanged if the move failed. 3194command ( 'let &winwidth = &columns | let &winminwidth = &columns' ) 3195matches ( 3196'E36: Not enough room$' , 3197pcall_err ( api . nvim_win_set_config , t2_cur_win , { split ='left' , win =0 } ) 3198) 3199eq ( t2_cur_win , api . nvim_tabpage_get_win ( t2 )) 3200command ( 'set winminwidth& winwidth&' ) 3201 3202-- But is changed if successfully moved to a different tabpage. 3203api . nvim_win_set_config ( t2_cur_win , { split ='left' , win =0 } ) 3204eq ( t2_alt_win , api . nvim_tabpage_get_win ( t2 )) 3205eq ( t1 , api . nvim_win_get_tabpage ( t2_cur_win )) 3206 3207-- Now do it for a float, which has different altwin logic. 3208command ( 'tabnext' ) 3209t2_cur_win = 3210api . nvim_open_win ( 0 , true , { relative ='editor' , row =5 , col =5 , width =5 , height =5 } ) 3211eq ( t2_alt_win , fn . win_getid ( fn . winnr ( '#' ))) 3212command ( 'tabprevious' ) 3213eq ( t2_cur_win , api . nvim_tabpage_get_win ( t2 )) 3214 3215api . nvim_win_set_config ( t2_cur_win , { split ='left' , win =0 } ) 3216eq ( t2_alt_win , api . nvim_tabpage_get_win ( t2 )) 3217eq ( t1 , api . nvim_win_get_tabpage ( t2_cur_win )) 3218 3219-- Very fun: move curwin between tabpages, converting from split to float, but with an autocmd 3220-- that deletes altwin after we're bumped to it, re-enters curwin, then switches to a 3rd 3221-- tabpage. tp_curwin of the window's old tabpage shouldn't be set to the freed altwin! 3222command ( 'tablast | tab split | tabprevious | split' ) 3223command ( 'autocmd WinEnter * ++once quit | let expect_alt = win_getid() | wincmd p | tabnext' ) 3224api . nvim_win_set_config ( 0 , { 3225relative ='editor' , 3226win =api . nvim_tabpage_get_win ( t1 ), 3227row =0 , 3228col =0 , 3229width =5 , 3230height =5 , 3231} ) 3232eq ( eval ( 'g:expect_alt' ), api . nvim_tabpage_get_win ( t2 )) 3233 3234-- Same, but for float -> float. 3235command ( 'tabprevious | split' ) 3236api . nvim_open_win ( 0 , true , { relative ='editor' , row =0 , col =0 , width =1 , height =1 } ) 3237command ( 'autocmd WinEnter * ++once quit | let expect_alt = win_getid() | wincmd p | tabnext' ) 3238api . nvim_win_set_config ( 0 , { 3239relative ='editor' , 3240win =api . nvim_tabpage_get_win ( t1 ), 3241row =0 , 3242col =0 , 3243width =5 , 3244height =5 , 3245} ) 3246eq ( eval ( 'g:expect_alt' ), api . nvim_tabpage_get_win ( t2 )) 3247end ) 3248 3249it ( 'set_config cannot change "noautocmd" #36409' , function () 3250local cfg ={ relative ='editor' , row =1 , col =1 , height =2 , width =2 , noautocmd =true } 3251local win =api . nvim_open_win ( 0 , false , cfg ) 3252cfg . height =10 3253eq ( true , pcall ( api . nvim_win_set_config , win , cfg )) 3254cfg . noautocmd =false 3255eq ( 3256"'noautocmd' cannot be changed on existing window" , 3257pcall_err ( api . nvim_win_set_config , win , cfg ) 3258) 3259end ) 3260 3261it ( 'removes last statusline if needed' , function () 3262local screen =Screen . new ( 30 , 9 ) 3263command ( 'set laststatus=1 | botright split' ) 3264screen : expect ( [[ 3265| 3266{1:~ }|*2 3267{2:[No Name] }| 3268^ | 3269{1:~ }|*2 3270{3:[No Name] }| 3271| 3272]] ) 3273api . nvim_win_set_config ( 0 , { relative ='editor' , row =0 , col =0 , width =4 , height =4 } ) 3274screen : expect ( [[ 3275{4:^ } | 3276{11:~ }{1: }|*3 3277{1:~ }|*4 3278| 3279]] ) 3280command ( 'quit | set laststatus=2 | botright split' ) 3281screen : expect ( [[ 3282| 3283{1:~ }|*2 3284{2:[No Name] }| 3285^ | 3286{1:~ }|*2 3287{3:[No Name] }| 3288| 3289]] ) 3290api . nvim_win_set_config ( 0 , { relative ='editor' , row =1 , col =5 , width =4 , height =4 } ) 3291screen : expect ( [[ 3292| 3293{1:~ }{4:^ }{1: }| 3294{1:~ }{11:~ }{1: }|*3 3295{1:~ }|*2 3296{2:[No Name] }| 3297| 3298]] ) 3299end ) 3300 3301it ( 'can convert external window to non-external' , function () 3302Screen . new ( 20 , 7 , { ext_multigrid =true } ) -- multigrid needed for external windows 3303api . nvim_open_win ( 0 , true , { external =true , width =5 , height =5 } ) 3304eq ( true , api . nvim_win_get_config ( 0 ). external ) 3305api . nvim_win_set_config ( 0 , { split ='below' , win =fn . win_getid ( 1 ) } ) 3306eq ( false , api . nvim_win_get_config ( 0 ). external ) 3307 3308api . nvim_win_set_config ( 0 , { external =true , width =5 , height =5 } ) 3309eq ( true , api . nvim_win_get_config ( 0 ). external ) 3310api . nvim_win_set_config ( 0 , { relative ='editor' , row =3 , col =3 } ) 3311eq ( false , api . nvim_win_get_config ( 0 ). external ) 3312 3313api . nvim_win_set_config ( 0 , { external =true , width =5 , height =5 } ) 3314eq ( true , api . nvim_win_get_config ( 0 ). external ) 3315api . nvim_win_set_config ( 0 , { external =false } ) 3316eq ( false , api . nvim_win_get_config ( 0 ). external ) 3317end ) 3318end ) 3319 3320describe ( 'get_config' , function () 3321it ( 'includes border' , function () 3322local b ={ 'a' , 'b' , 'c' , 'd' , 'e' , 'f' , 'g' , 'h' } 3323local win =api . nvim_open_win ( 0 , true , { 3324relative ='win' , 3325row =3 , 3326col =3 , 3327width =12 , 3328height =3 , 3329border =b , 3330} ) 3331 3332local cfg =api . nvim_win_get_config ( win ) 3333eq ( b , cfg . border ) 3334end ) 3335 3336it ( 'includes border with highlight group' , function () 3337local b ={ 3338{ 'a' , 'Normal' } , 3339{ 'b' , 'Special' } , 3340{ 'c' , 'String' } , 3341{ 'd' , 'Comment' } , 3342{ 'e' , 'Visual' } , 3343{ 'f' , 'Error' } , 3344{ 'g' , 'Constant' } , 3345{ 'h' , 'PreProc' } , 3346} 3347local win =api . nvim_open_win ( 0 , true , { 3348relative ='win' , 3349row =3 , 3350col =3 , 3351width =12 , 3352height =3 , 3353border =b , 3354} ) 3355 3356local cfg =api . nvim_win_get_config ( win ) 3357eq ( b , cfg . border ) 3358end ) 3359 3360it ( 'includes title and footer' , function () 3361local title ={ { 'A' , { 'StatusLine' , 'TabLine' } } , { 'B' } , { 'C' , 'WinBar' } } 3362local footer ={ { 'A' , 'WinBar' } , { 'B' } , { 'C' , { 'StatusLine' , 'TabLine' } } } 3363local win =api . nvim_open_win ( 0 , true , { 3364relative ='win' , 3365row =3 , 3366col =3 , 3367width =12 , 3368height =3 , 3369border ='single' , 3370title =title , 3371footer =footer , 3372} ) 3373 3374local cfg =api . nvim_win_get_config ( win ) 3375eq ( title , cfg . title ) 3376eq ( footer , cfg . footer ) 3377end ) 3378 3379it ( 'includes split for normal windows' , function () 3380local win =api . nvim_open_win ( 0 , true , { 3381vertical =true , 3382win =- 1 , 3383} ) 3384eq ( 'left' , api . nvim_win_get_config ( win ). split ) 3385api . nvim_win_set_config ( win , { 3386vertical =false , 3387win =- 1 , 3388} ) 3389eq ( 'above' , api . nvim_win_get_config ( win ). split ) 3390api . nvim_win_set_config ( win , { 3391split ='below' , 3392win =- 1 , 3393} ) 3394eq ( 'below' , api . nvim_win_get_config ( win ). split ) 3395end ) 3396 3397it ( 'includes split when splitting with ex commands' , function () 3398local win =api . nvim_get_current_win () 3399eq ( 'left' , api . nvim_win_get_config ( win ). split ) 3400 3401command ( 'vsplit' ) 3402local win2 =api . nvim_get_current_win () 3403 3404-- initial window now be marked as right split 3405-- since it was split with a vertical split 3406-- and 'splitright' is false by default 3407eq ( 'right' , api . nvim_win_get_config ( win ). split ) 3408eq ( 'left' , api . nvim_win_get_config ( win2 ). split ) 3409 3410api . nvim_set_option_value ( 'splitbelow' , true , { 3411scope ='global' , 3412} ) 3413api . nvim_win_close ( win , true ) 3414command ( 'split' ) 3415local win3 =api . nvim_get_current_win () 3416eq ( 'below' , api . nvim_win_get_config ( win3 ). split ) 3417end ) 3418 3419it ( "includes the correct 'split' option in complex layouts" , function () 3420local initial_win =api . nvim_get_current_win () 3421local win =api . nvim_open_win ( 0 , false , { 3422split ='right' , 3423win =- 1 , 3424} ) 3425 3426local win2 =api . nvim_open_win ( 0 , false , { 3427split ='below' , 3428win =win , 3429} ) 3430 3431api . nvim_win_set_config ( win2 , { 3432width =50 , 3433} ) 3434 3435api . nvim_win_set_config ( win , { 3436split ='left' , 3437win =- 1 , 3438} ) 3439 3440local win3 =api . nvim_open_win ( 0 , false , { 3441split ='above' , 3442win =- 1 , 3443} ) 3444local float =api . nvim_open_win ( 0 , false , { 3445relative ='editor' , 3446width =40 , 3447height =20 , 3448col =20 , 3449row =10 , 3450} ) 3451api . nvim_win_set_config ( float , { 3452split ='right' , 3453win =- 1 , 3454} ) 3455 3456local layout =fn . winlayout () 3457 3458eq ( { 3459'row' , 3460{ 3461{ 3462'col' , 3463{ 3464{ 'leaf' , win3 } , 3465{ 3466'row' , 3467{ 3468{ 'leaf' , win } , 3469{ 'leaf' , initial_win } , 3470{ 'leaf' , win2 } , 3471} , 3472} , 3473} , 3474} , 3475{ 3476'leaf' , 3477float , 3478} , 3479} , 3480} , layout ) 3481 3482eq ( 'above' , api . nvim_win_get_config ( win3 ). split ) 3483eq ( 'left' , api . nvim_win_get_config ( win ). split ) 3484eq ( 'left' , api . nvim_win_get_config ( initial_win ). split ) 3485eq ( 'right' , api . nvim_win_get_config ( win2 ). split ) 3486eq ( 'right' , api . nvim_win_get_config ( float ). split ) 3487end ) 3488 3489it ( 'includes style' , function () 3490local unused_style1 =api . nvim_open_win ( 0 , false , { 3491width =10 , 3492height =10 , 3493relative ='editor' , 3494row =10 , 3495col =10 , 3496} ) 3497local unused_style2 =api . nvim_open_win ( 0 , false , { 3498width =10 , 3499height =10 , 3500relative ='editor' , 3501row =10 , 3502col =10 , 3503style ='' , 3504} ) 3505local minimal_style =api . nvim_open_win ( 0 , false , { 3506width =10 , 3507height =10 , 3508relative ='editor' , 3509row =10 , 3510col =10 , 3511style ='minimal' , 3512} ) 3513 3514eq ( '' , api . nvim_win_get_config ( unused_style1 ). style ) 3515eq ( '' , api . nvim_win_get_config ( unused_style2 ). style ) 3516eq ( 'minimal' , api . nvim_win_get_config ( minimal_style ). style ) 3517 3518-- "style" is allowed for splits too. 3519eq ( '' , api . nvim_win_get_config ( 0 ). relative ) 3520eq ( '' , api . nvim_win_get_config ( 0 ). style ) 3521api . nvim_win_set_config ( 0 , { style ='minimal' } ) 3522eq ( 'minimal' , api . nvim_win_get_config ( 0 ). style ) 3523api . nvim_win_set_config ( 0 , { height =1 } ) -- "style" unchanged when not included. 3524eq ( 'minimal' , api . nvim_win_get_config ( 0 ). style ) 3525api . nvim_win_set_config ( 0 , { style ='' } ) 3526eq ( '' , api . nvim_win_get_config ( 0 ). style ) 3527end ) 3528end ) 3529 3530describe ( 'set_config' , function () 3531it ( 'no crash with invalid title' , function () 3532local win =api . nvim_open_win ( 0 , true , { 3533width =10 , 3534height =10 , 3535relative ='editor' , 3536row =10 , 3537col =10 , 3538title ={ { 'test' } } , 3539border ='single' , 3540} ) 3541eq ( 3542"Invalid 'title/footer': expected String or Array, got Integer" , 3543pcall_err ( api . nvim_win_set_config , win , { title =0 } ) 3544) 3545command ( 'redraw!' ) 3546assert_alive () 3547eq ( 3548"Invalid 'title/footer': expected non-empty Array" , 3549pcall_err ( api . nvim_win_set_config , win , { title ={} } ) 3550) 3551command ( 'redraw!' ) 3552assert_alive () 3553end ) 3554 3555it ( 'no crash with invalid footer' , function () 3556local win =api . nvim_open_win ( 0 , true , { 3557width =10 , 3558height =10 , 3559relative ='editor' , 3560row =10 , 3561col =10 , 3562footer ={ { 'test' } } , 3563border ='single' , 3564} ) 3565eq ( 3566"Invalid 'title/footer': expected String or Array, got Integer" , 3567pcall_err ( api . nvim_win_set_config , win , { footer =0 } ) 3568) 3569command ( 'redraw!' ) 3570assert_alive () 3571eq ( 3572"Invalid 'title/footer': expected non-empty Array" , 3573pcall_err ( api . nvim_win_set_config , win , { footer ={} } ) 3574) 3575command ( 'redraw!' ) 3576assert_alive () 3577end ) 3578 3579describe ( 'no crash or memory leak' , function () 3580local win 3581 3582before_each ( function () 3583win =api . nvim_open_win ( 0 , false , { 3584relative ='editor' , 3585row =10 , 3586col =10 , 3587height =10 , 3588width =10 , 3589border ='single' , 3590title ={ { 'OLD_TITLE' } } , 3591footer ={ { 'OLD_FOOTER' } } , 3592} ) 3593end ) 3594 3595it ( 'with valid title and invalid footer' , function () 3596eq ( 3597"Invalid 'title/footer': expected String or Array, got Integer" , 3598pcall_err ( api . nvim_win_set_config , win , { 3599title ={ { 'NEW_TITLE' } } , 3600footer =0 , 3601} ) 3602) 3603command ( 'redraw!' ) 3604assert_alive () 3605eq ( { { 'OLD_TITLE' } } , api . nvim_win_get_config ( win ). title ) 3606end ) 3607 3608it ( 'with invalid title and valid footer' , function () 3609eq ( 3610"Invalid 'title/footer': expected String or Array, got Integer" , 3611pcall_err ( api . nvim_win_set_config , win , { 3612title =0 , 3613footer ={ { 'NEW_FOOTER' } } , 3614} ) 3615) 3616command ( 'redraw!' ) 3617assert_alive () 3618eq ( { { 'OLD_FOOTER' } } , api . nvim_win_get_config ( win ). footer ) 3619end ) 3620end ) 3621 3622it ( 'cannot split from a float' , function () 3623local win =api . nvim_get_current_win () 3624local float_win =api . nvim_open_win ( 0 , true , { 3625relative ='editor' , 3626width =10 , 3627height =10 , 3628row =10 , 3629col =10 , 3630} ) 3631eq ( 3632'Cannot split a floating window' , 3633pcall_err ( api . nvim_win_set_config , win , { win =float_win , split ='right' } ) 3634) 3635eq ( 3636'Cannot split a floating window' , 3637pcall_err ( api . nvim_win_set_config , win , { win =0 , split ='right' } ) 3638) 3639 3640-- No errors when not actually splitting. 3641local cfg =api . nvim_win_get_config ( win ) 3642api . nvim_win_set_config ( win , {} ) 3643eq ( cfg , api . nvim_win_get_config ( win )) 3644 3645eq ( 1 , eval ( '&cmdheight' )) 3646api . nvim_win_set_config ( win , { height =1 } ) 3647cfg . height =1 3648eq ( cfg , api . nvim_win_get_config ( win )) 3649eq ( 23 , eval ( '&cmdheight' )) 3650 3651api . nvim_win_set_config ( win , { style ='minimal' } ) 3652cfg . style ='minimal' 3653eq ( cfg , api . nvim_win_get_config ( win )) 3654end ) 3655 3656it ( 'cannot move autocmd window between tabpages' , function () 3657local win_type , split_ok , split_err , float_ok , float_err =exec_lua ( function () 3658local other_tp_win =vim . api . nvim_get_current_win () 3659vim . cmd . tabnew () 3660 3661local win_type , split_ok , split_err , float_ok , float_err 3662vim . api . nvim_buf_call ( vim . api . nvim_create_buf ( true , true ), function () 3663win_type =vim . fn . win_gettype () 3664 3665split_ok , split_err = 3666pcall ( vim . api . nvim_win_set_config , 0 , { win =other_tp_win , split ='right' } ) 3667 3668float_ok , float_err =pcall ( vim . api . nvim_win_set_config , 0 , { win =other_tp_win } ) 3669end ) 3670return win_type , split_ok , split_err , float_ok , float_err 3671end ) 3672 3673eq ( 'autocmd' , win_type ) 3674eq ( { false , 'Cannot move autocmd window to another tabpage' } , { split_ok , split_err } ) 3675eq ( { false , 'Cannot move autocmd window to another tabpage' } , { float_ok , float_err } ) 3676end ) 3677 3678it ( 'can move cmdwin between tabpages #40312' , function () 3679local other_tp_win =api . nvim_get_current_win () 3680command ( 'tabnew' ) 3681feed ( 'q:' ) 3682n . poke_eventloop () 3683eq ( 'command' , fn . win_gettype ()) 3684api . nvim_win_set_config ( 0 , { win =other_tp_win , split ='right' } ) 3685end ) 3686 3687it ( 'minimal style persists through float-to-split and buffer change #37067' , function () 3688-- Set all options globally 3689command ( 'set number relativenumber cursorline cursorcolumn spell list' ) 3690command ( 'set signcolumn=yes colorcolumn=80 statuscolumn=%l foldcolumn=2' ) 3691local buf1 =api . nvim_create_buf ( false , true ) 3692local win =api . nvim_open_win ( buf1 , true , { 3693relative ='editor' , 3694width =10 , 3695height =10 , 3696row =5 , 3697col =5 , 3698style ='minimal' , 3699} ) 3700-- Convert to split then change buffer 3701api . nvim_win_set_config ( win , { split ='below' , win =- 1 } ) 3702local buf2 =api . nvim_create_buf ( false , true ) 3703api . nvim_win_set_buf ( win , buf2 ) 3704eq ( false , api . nvim_get_option_value ( 'number' , { win =win } )) 3705eq ( false , api . nvim_get_option_value ( 'relativenumber' , { win =win } )) 3706eq ( false , api . nvim_get_option_value ( 'cursorline' , { win =win } )) 3707eq ( false , api . nvim_get_option_value ( 'cursorcolumn' , { win =win } )) 3708eq ( false , api . nvim_get_option_value ( 'spell' , { win =win } )) 3709eq ( false , api . nvim_get_option_value ( 'list' , { win =win } )) 3710eq ( '0' , api . nvim_get_option_value ( 'foldcolumn' , { win =win } )) 3711eq ( 'auto' , api . nvim_get_option_value ( 'signcolumn' , { win =win } )) 3712eq ( '' , api . nvim_get_option_value ( 'colorcolumn' , { win =win } )) 3713eq ( '' , api . nvim_get_option_value ( 'statuscolumn' , { win =win } )) 3714end ) 3715 3716it ( 'merges configs only after successfully configuring split' , function () 3717local win =api . nvim_open_win ( 0 , true , { 3718relative ='editor' , 3719width =10 , 3720height =10 , 3721row =5 , 3722col =5 , 3723} ) 3724local cfg =api . nvim_win_get_config ( win ) 3725eq ( '' , cfg . style ) 3726command ( 'set cursorline | tabnew' ) 3727local tp2_win =api . nvim_get_current_win () 3728command ( 'tabfirst | autocmd WinEnter * ++once wincmd p' ) 3729eq ( 3730'Failed to switch away from window 1001' , 3731pcall_err ( 3732api . nvim_win_set_config , 3733win , 3734{ split ='below' , win =tp2_win , style ='minimal' } 3735) 3736) 3737eq ( cfg , api . nvim_win_get_config ( win )) 3738eq ( true , api . nvim_get_option_value ( 'cursorline' , { win =win } )) 3739 3740exec ( [[ 3741autocmd WinLeave * ++once let g:style_before = nvim_win_get_config(0).style 3742\| let g:cul_before = &cursorline 3743\| call nvim_win_set_config(0, #{style: ""}) 3744]] ) 3745api . nvim_win_set_config ( win , { split ='below' , win =tp2_win , style ='minimal' } ) 3746eq ( '' , eval ( 'g:style_before' )) 3747eq ( 1 , eval ( 'g:cul_before' )) 3748eq ( 'minimal' , api . nvim_win_get_config ( win ). style ) 3749eq ( false , api . nvim_get_option_value ( 'cursorline' , { win =win } )) 3750end ) 3751 3752it ( 'minimal style not re-applied if style is unchanged' , function () 3753api . nvim_open_win ( 0 , true , { relative ='editor' , width =10 , height =10 , row =5 , col =5 } ) 3754command ( 'setlocal number rightleft' ) 3755api . nvim_win_set_config ( 0 , { style ='minimal' } ) 3756eq ( 0 , eval ( '&number' )) -- style changed; should've reset 3757command ( 'setlocal number' ) 3758api . nvim_win_set_config ( 0 , { style ='minimal' } ) 3759eq ( 1 , eval ( '&number' )) 3760eq ( 1 , eval ( '&rightleft' )) -- unrelated option unaffected 3761end ) 3762 3763it ( 'minimal style does not leak WinInfo fold memory' , function () 3764feed ( 'zfG' ) 3765api . nvim_open_win ( 0 , true , { split ='below' , style ='minimal' } ) 3766command ( 'quit' ) 3767end ) 3768 3769it ( 'preserve current floating window when moving fails' , function () 3770local buf =api . nvim_create_buf ( false , true ) 3771local float_win =api . nvim_open_win ( buf , true , { 3772relative ='editor' , 3773row =1 , 3774col =1 , 3775width =10 , 3776height =5 , 3777} ) 3778command ( 'tabnew' ) 3779local tab2_win =api . nvim_get_current_win () 3780command ( 'tabprev' ) 3781api . nvim_set_current_win ( float_win ) 3782command ( 'autocmd WinLeave * ++once call nvim_win_close(' .. tab2_win .. ', v:true)' ) 3783eq ( 3784'Target windows were closed' , 3785pcall_err ( api . nvim_win_set_config , float_win , { win =tab2_win } ) 3786) 3787eq ( float_win , api . nvim_get_current_win ()) 3788 3789command ( 'tabnew' ) 3790local tab3_win =api . nvim_get_current_win () 3791command ( 'tabprev | autocmd WinEnter * ++once wincmd p' ) 3792eq ( 3793( 'Failed to switch away from window %d' ): format ( float_win ), 3794pcall_err ( api . nvim_win_set_config , float_win , { win =tab3_win } ) 3795) 3796eq ( float_win , api . nvim_get_current_win ()) 3797end ) 3798end ) 3799 3800it ( ':fclose and :only skip hidden and pinned windows #36123' , function () 3801local cfg ={ relative ='editor' , row =0 , col =0 , width =1 , height =1 } 3802local win1 =api . nvim_open_win ( 0 , false , cfg ) 3803command ( 'fclose' ) 3804eq ( false , api . nvim_win_is_valid ( win1 )) 3805cfg . hide =true 3806win1 =api . nvim_open_win ( 0 , false , cfg ) 3807cfg . hide , cfg . focusable =false , false 3808local win2 =api . nvim_open_win ( 0 , false , cfg ) 3809command ( 'fclose' ) 3810eq ( true , api . nvim_win_is_valid ( win1 )) 3811eq ( false , api . nvim_win_is_valid ( win2 )) 3812api . nvim_win_set_config ( win1 , { hide =false } ) 3813api . nvim_set_option_value ( 'winpinned' , true , { win =win1 , scope ='local' } ) 3814win2 =api . nvim_open_win ( 0 , false , { split ='right' } ) 3815api . nvim_set_option_value ( 'winpinned' , true , { win =win2 , scope ='local' } ) 3816command ( 'only' ) 3817eq ( true , api . nvim_win_is_valid ( win1 )) 3818eq ( true , api . nvim_win_is_valid ( win2 )) 3819local tab2 =api . nvim_open_tabpage ( 0 , false , {} ) 3820command ( 'tabclose' ) 3821eq ( tab2 , api . nvim_get_current_tabpage ()) 3822end ) 3823 3824describe ( 'nvim_win_call' , function () 3825it ( 'restores prevwin' , function () 3826local w1 =api . nvim_get_current_win () 3827command ( 'split' ) 3828local w2 =api . nvim_get_current_win () 3829command ( 'split' ) 3830local w3 =api . nvim_get_current_win () 3831-- Entry state: curwin=w3, prevwin=w2. 3832eq ( w2 , fn . win_getid ( fn . winnr ( '#' ))) 3833exec_lua ( function () 3834vim . api . nvim_win_call ( w1 , function () 3835vim . api . nvim_set_current_win ( w2 ) -- changes prevwin to w1 3836end ) 3837end ) 3838-- No evidence of the context-switch; curwin/prevwin are restored. 3839eq ( w3 , api . nvim_get_current_win ()) 3840eq ( w2 , fn . win_getid ( fn . winnr ( '#' ))) 3841end ) 3842 3843it ( 'supports multiple returns' , function () 3844local cur =api . nvim_get_current_win () 3845local other =api . nvim_open_win ( api . nvim_create_buf ( false , true ), false , { split ='left' } ) 3846exec_lua ( function () 3847function with_len ( ... ) 3848return select ( '#' , ... ), { ... } 3849end 3850function test ( fn ) 3851local len , res =with_len ( vim . api . nvim_win_call ( other , fn )) 3852-- convert to serializable vim.NIL 3853for i =1 , len do 3854if res [ i ] == nil then 3855res [ i ] =vim . NIL 3856end 3857end 3858return res 3859end 3860end ) 3861 3862eq ( 3863{ other } , 3864exec_lua ( function () 3865return test ( function () 3866return vim . api . nvim_get_current_win () 3867end ) 3868end ) 3869) 3870eq ( cur , api . nvim_get_current_win ()) 3871eq ( 3872{ other , vim . NIL } , 3873exec_lua ( function () 3874return test ( function () 3875return vim . api . nvim_get_current_win (), nil 3876end ) 3877end ) 3878) 3879 3880eq ( 3881{ 6 , 7 } , 3882exec_lua ( function () 3883return test ( function () 3884return 6 , 7 3885end ) 3886end ) 3887) 3888eq ( 3889{ 6 , vim . NIL , 7 } , 3890exec_lua ( function () 3891return test ( function () 3892return 6 , nil , 7 3893end ) 3894end ) 3895) 3896eq ( 3897{} , 3898exec_lua ( function () 3899return test ( function () end ) 3900end ) 3901) 3902eq ( 3903{ vim . NIL } , 3904exec_lua ( function () 3905return test ( function () 3906return nil 3907end ) 3908end ) 3909) 3910end ) 3911 3912it ( 'propagates return values when called from a coroutine #39834' , function () 3913local other =api . nvim_open_win ( api . nvim_create_buf ( false , true ), false , { split ='left' } ) 3914-- Single return value. 3915eq ( 3916{ other } , 3917exec_lua ( function () 3918local out 3919local co =coroutine . create ( function () 3920out ={ 3921vim . api . nvim_win_call ( other , function () 3922return vim . api . nvim_get_current_win () 3923end ), 3924} 3925end ) 3926assert ( coroutine . resume ( co )) 3927return out 3928end ) 3929) 3930-- Multiple return values (including nil in the middle). 3931eq ( 3932{ 6 , vim . NIL , 7 } , 3933exec_lua ( function () 3934local out 3935local co =coroutine . create ( function () 3936local function pack ( ... ) 3937local r ={ ... } 3938for i =1 , select ( '#' , ... ) do 3939if r [ i ] == nil then 3940r [ i ] =vim . NIL 3941end 3942end 3943return r 3944end 3945out =pack ( vim . api . nvim_win_call ( other , function () 3946return 6 , nil , 7 3947end )) 3948end ) 3949assert ( coroutine . resume ( co )) 3950return out 3951end ) 3952) 3953end ) 3954 3955it ( 'can access window options' , function () 3956command ( 'vsplit' ) 3957local win1 =api . nvim_get_current_win () 3958command ( 'wincmd w' ) 3959local win2 =exec_lua [[ 3960win2 = vim.api.nvim_get_current_win() 3961return win2 3962]] 3963command ( 'wincmd p' ) 3964 3965eq ( '' , api . nvim_get_option_value ( 'winhighlight' , { win =win1 } )) 3966eq ( '' , api . nvim_get_option_value ( 'winhighlight' , { win =win2 } )) 3967 3968local val =exec_lua [[ 3969return vim.api.nvim_win_call(win2, function() 3970vim.cmd "setlocal winhighlight=Normal:Normal" 3971return vim.api.nvim_get_current_win() 3972end) 3973]] 3974 3975eq ( '' , api . nvim_get_option_value ( 'winhighlight' , { win =win1 } )) 3976eq ( 'Normal:Normal' , api . nvim_get_option_value ( 'winhighlight' , { win =win2 } )) 3977eq ( win1 , api . nvim_get_current_win ()) 3978eq ( win2 , val ) 3979end ) 3980 3981it ( 'failure modes' , function () 3982matches ( 3983'nvim_exec2%(%), line 1: Vim:E492: Not an editor command: fooooo' , 3984pcall_err ( exec_lua , [[vim.api.nvim_win_call(0, function() vim.cmd 'fooooo' end)]] ) 3985) 3986eq ( 3987'Lua: [string "<nvim>"]:0: fooooo' , 3988pcall_err ( exec_lua , [[vim.api.nvim_win_call(0, function() error('fooooo') end)]] ) 3989) 3990end ) 3991 3992it ( 'does not cause ml_get errors with invalid visual selection' , function () 3993-- Add lines to the current buffer and make another window looking into an empty buffer. 3994exec_lua [[ 3995_G.api = vim.api 3996_G.t = function(s) return api.nvim_replace_termcodes(s, true, true, true) end 3997_G.win_lines = api.nvim_get_current_win() 3998vim.cmd "new" 3999_G.win_empty = api.nvim_get_current_win() 4000api.nvim_set_current_win(win_lines) 4001api.nvim_buf_set_lines(0, 0, -1, true, {"a", "b", "c"}) 4002]] 4003 4004-- Start Visual in current window, redraw in other window with fewer lines. 4005-- Should be fixed by vim-patch:8.2.4018. 4006exec_lua [[ 4007api.nvim_feedkeys(t "G<C-V>", "txn", false) 4008api.nvim_win_call(win_empty, function() vim.cmd "redraw" end) 4009]] 4010 4011-- Start Visual in current window, extend it in other window with more lines. 4012-- Fixed for win_execute by vim-patch:8.2.4026, but nvim_win_call should also not be affected. 4013exec_lua [[ 4014api.nvim_feedkeys(t "<Esc>gg", "txn", false) 4015api.nvim_set_current_win(win_empty) 4016api.nvim_feedkeys(t "gg<C-V>", "txn", false) 4017api.nvim_win_call(win_lines, function() api.nvim_feedkeys(t "G<C-V>", "txn", false) end) 4018vim.cmd "redraw" 4019]] 4020end ) 4021 4022it ( 'updates ruler if cursor moved' , function () 4023-- Fixed for win_execute in vim-patch:8.1.2124, but should've applied to nvim_win_call too! 4024local screen =Screen . new ( 30 , 5 ) 4025exec_lua [[ 4026_G.api = vim.api 4027vim.opt.ruler = true 4028local lines = {} 4029for i = 0, 499 do lines[#lines + 1] = tostring(i) end 4030api.nvim_buf_set_lines(0, 0, -1, true, lines) 4031api.nvim_win_set_cursor(0, {20, 0}) 4032vim.cmd "split" 4033_G.win = api.nvim_get_current_win() 4034vim.cmd "wincmd w | redraw" 4035]] 4036screen : expect [[ 403719 | 4038{2:< Name] [+] 20,1 3%}| 4039^19 | 4040{3:< Name] [+] 20,1 3%}| 4041| 4042]] 4043exec_lua [[ 4044api.nvim_win_call(win, function() api.nvim_win_set_cursor(0, {100, 0}) end) 4045vim.cmd "redraw" 4046]] 4047screen : expect [[ 404899 | 4049{2:< Name] [+] 100,1 19%}| 4050^19 | 4051{3:< Name] [+] 20,1 3%}| 4052| 4053]] 4054end ) 4055 4056it ( 'can return values by reference' , function () 4057eq ( 4058{ 7 , 10 } , 4059exec_lua [[ 4060local val = {4, 10} 4061local ref = vim.api.nvim_win_call(0, function() return val end) 4062ref[1] = 7 4063return val 4064]] 4065) 4066end ) 4067 4068it ( 'layout in current tabpage does not affect windows in others' , function () 4069command ( 'tab split' ) 4070local t2_move_win =api . nvim_get_current_win () 4071command ( 'vsplit' ) 4072local t2_other_win =api . nvim_get_current_win () 4073command ( 'tabprevious' ) 4074matches ( 'E36: Not enough room$' , pcall_err ( command , 'execute "split|"->repeat(&lines)' )) 4075command ( 'vsplit' ) 4076 4077-- Without vim-patch:8.2.3862, this gives E36, despite just the 1st tabpage being full. 4078exec_lua ( 'vim.api.nvim_win_call(..., function() vim.cmd.wincmd "J" end)' , t2_move_win ) 4079eq ( { 'col' , { { 'leaf' , t2_other_win } , { 'leaf' , t2_move_win } } } , fn . winlayout ( 2 )) 4080end ) 4081end ) 4082end )