robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
aa1d8a2702
master
1" Tests for autocommands 2 3source shared.vim 4source check.vim 5source term_util.vim 6source screendump.vim 7source vim9.vim 8source load.vim 9 10func s: cleanup_buffers () abort 11for bnr in range ( 1 , bufnr ( '$' )) 12if bufloaded ( bnr ) && bufnr ( '%' ) != bnr 13execute 'bd! ' . bnr 14endif 15endfor 16endfunc 17 18func CleanUpTestAuGroup () 19augroup testing 20au ! 21augroup END 22augroup ! testing 23endfunc 24 25func Test_vim_did_enter () 26call assert_false ( v: vim_did_enter ) 27 28" This script will never reach the main loop, can't check if v:vim_did_enter 29" becomes one. 30endfunc 31 32" Test for the CursorHold autocmd 33func Test_CursorHold_autocmd () 34CheckRunVimInTerminal 35call writefile ([ 'one' , 'two' , 'three' ], 'XoneTwoThree' , 'D' ) 36let before =<< trim END 37set updatetime=10 38au CursorHold * call writefile([line('.')], 'XCHoutput', 'a') 39END 40call writefile ( before , 'XCHinit' , 'D' ) 41let buf = RunVimInTerminal ( '-S XCHinit XoneTwoThree' , {}) 42call term_sendkeys ( buf , "G" ) 43call term_wait ( buf , 50 ) 44call term_sendkeys ( buf , "gg" ) 45call term_wait ( buf ) 46call WaitForAssert ({ ->assert_equal ([ '1' ], readfile ( 'XCHoutput' )[ -1 : -1 ])}) 47call term_sendkeys ( buf , "j" ) 48call term_wait ( buf ) 49call WaitForAssert ({ ->assert_equal ([ '1' , '2' ], readfile ( 'XCHoutput' )[ -2 : -1 ])}) 50call term_sendkeys ( buf , "j" ) 51call term_wait ( buf ) 52call WaitForAssert ({ ->assert_equal ([ '1' , '2' , '3' ], readfile ( 'XCHoutput' )[ -3 : -1 ])}) 53call StopVimInTerminal ( buf ) 54 55call delete ( 'XCHoutput' ) 56endfunc 57 58if has ( 'timers' ) 59 60func ExitInsertMode ( id ) 61call feedkeys ( "\<Esc>" ) 62endfunc 63 64func Test_cursorhold_insert () 65" depends on timing 66let g: test_is_flaky = 1 67 68" Need to move the cursor. 69call feedkeys ( "ggG" , "xt" ) 70 71let g: triggered = 0 72au CursorHoldI * let g: triggered += 1 73set updatetime = 20 74call timer_start ( LoadAdjust ( 200 ), 'ExitInsertMode' ) 75call feedkeys ( 'a' , 'x!' ) 76call assert_equal ( 1 , g: triggered ) 77unlet g: triggered 78au ! CursorHoldI 79set updatetime & 80endfunc 81 82func Test_cursorhold_insert_with_timer_interrupt () 83CheckFeature job 84" Need to move the cursor. 85call feedkeys ( "ggG" , "xt" ) 86 87" Confirm the timer invoked in exit_cb of the job doesn't disturb 88" CursorHoldI event. 89let g: triggered = 0 90au CursorHoldI * let g: triggered += 1 91set updatetime = 100 92call job_start ( has ( 'win32' ) ? 'cmd /D /c echo:' : 'echo' , 93 \{ 'exit_cb' : { ->timer_start ( 200 , 'ExitInsertMode' )}}) 94call feedkeys ( 'a' , 'x!' ) 95call assert_equal ( 1 , g: triggered ) 96unlet g: triggered 97au ! CursorHoldI 98set updatetime & 99endfunc 100 101func Test_cursorhold_insert_ctrl_x () 102let g: triggered = 0 103au CursorHoldI * let g: triggered += 1 104set updatetime = 20 105call timer_start ( LoadAdjust ( 100 ), 'ExitInsertMode' ) 106" CursorHoldI does not trigger after CTRL-X 107call feedkeys ( "a\<C-X>" , 'x!' ) 108call assert_equal ( 0 , g: triggered ) 109unlet g: triggered 110au ! CursorHoldI 111set updatetime & 112endfunc 113 114func Test_cursorhold_insert_ctrl_g_U () 115au CursorHoldI * : 116set updatetime = 20 117new 118call timer_start ( 100 , { ->feedkeys ( "\<Left>foo\<Esc>" , 't' ) }) 119call feedkeys ( "i()\<C-g>U" , 'tx!' ) 120sleep 200m 121call assert_equal ( '(foo)' , getline ( 1 )) 122undo 123call assert_equal ( '' , getline ( 1 )) 124 125bwipe ! 126au ! CursorHoldI 127set updatetime & 128endfunc 129 130func Test_OptionSet_modeline () 131call Ntest_override ( 'starting' , 1 ) 132au ! OptionSet 133augroup set_tabstop 134au OptionSet tabstop call timer_start ( 1 , { ->execute ( "echo 'Handler called'" , "" )}) 135augroup END 136call writefile ([ 'vim: set ts=7 sw=5 :' , 'something' ], 'XoptionsetModeline' , 'D' ) 137set modeline 138let v: errmsg = '' 139call assert_fails ( 'split XoptionsetModeline' , 'E12:' ) 140call assert_equal ( 7 , & ts ) 141call assert_equal ( '' , v: errmsg ) 142 143augroup set_tabstop 144au ! 145augroup END 146bwipe ! 147set ts & 148call Ntest_override ( 'starting' , 0 ) 149endfunc 150 151endif "has('timers') 152 153func Test_bufunload () 154augroup test_bufunload_group 155autocmd ! 156autocmd BufUnload * call add ( s: li , "bufunload" ) 157autocmd BufDelete * call add ( s: li , "bufdelete" ) 158autocmd BufWipeout * call add ( s: li , "bufwipeout" ) 159augroup END 160 161let s: li = [] 162new 163setlocal bufhidden = 164bunload 165call assert_equal ([ "bufunload" , "bufdelete" ], s: li ) 166 167let s: li = [] 168new 169setlocal bufhidden = delete 170bunload 171call assert_equal ([ "bufunload" , "bufdelete" ], s: li ) 172 173let s: li = [] 174new 175setlocal bufhidden = unload 176bwipeout 177call assert_equal ([ "bufunload" , "bufdelete" , "bufwipeout" ], s: li ) 178 179au ! test_bufunload_group 180augroup ! test_bufunload_group 181endfunc 182 183" SEGV occurs in older versions. (At least 7.4.2005 or older) 184func Test_autocmd_bufunload_with_tabnext () 185tabedit 186tabfirst 187 188augroup test_autocmd_bufunload_with_tabnext_group 189autocmd ! 190autocmd BufUnload <buffer> tabnext 191augroup END 192 193quit 194call assert_equal ( 2 , tabpagenr ( '$' )) 195 196autocmd ! test_autocmd_bufunload_with_tabnext_group 197augroup ! test_autocmd_bufunload_with_tabnext_group 198tablast 199quit 200endfunc 201 202func Test_argdelete_in_next () 203au BufNew , BufEnter , BufLeave , BufWinEnter * argdel 204call assert_fails ( 'next a b' , 'E1156:' ) 205au ! BufNew , BufEnter , BufLeave , BufWinEnter * 206endfunc 207 208func Test_autocmd_bufwinleave_with_tabfirst () 209tabedit 210augroup sample 211autocmd ! 212autocmd BufWinLeave <buffer> tabfirst 213augroup END 214call setline ( 1 , [ 'a' , 'b' , 'c' ]) 215edit ! a.txt 216tabclose 217endfunc 218 219" SEGV occurs in older versions. (At least 7.4.2321 or older) 220func Test_autocmd_bufunload_avoiding_SEGV_01 () 221split aa.txt 222let lastbuf = bufnr ( '$' ) 223 224augroup test_autocmd_bufunload 225autocmd ! 226exe 'autocmd BufUnload <buffer> ' . ( lastbuf + 1 ) . 'bwipeout!' 227augroup END 228 229call assert_fails ( 'edit bb.txt' , 'E937:' ) 230 231autocmd ! test_autocmd_bufunload 232augroup ! test_autocmd_bufunload 233bwipe ! aa.txt 234bwipe ! bb.txt 235endfunc 236 237" SEGV occurs in older versions. (At least 7.4.2321 or older) 238func Test_autocmd_bufunload_avoiding_SEGV_02 () 239setlocal buftype = nowrite 240let lastbuf = bufnr ( '$' ) 241 242augroup test_autocmd_bufunload 243autocmd ! 244exe 'autocmd BufUnload <buffer> ' . ( lastbuf + 1 ) . 'bwipeout!' 245augroup END 246 247normal ! i1 248call assert_fails ( 'edit a.txt' , 'E517:' ) 249 250autocmd ! test_autocmd_bufunload 251augroup ! test_autocmd_bufunload 252bwipe ! a.txt 253endfunc 254 255func Test_autocmd_dummy_wipeout () 256" prepare files 257call writefile ([ '' ], 'Xdummywipetest1.txt' , 'D' ) 258call writefile ([ '' ], 'Xdummywipetest2.txt' , 'D' ) 259augroup test_bufunload_group 260autocmd ! 261autocmd BufUnload * call add ( s: li , "bufunload" ) 262autocmd BufDelete * call add ( s: li , "bufdelete" ) 263autocmd BufWipeout * call add ( s: li , "bufwipeout" ) 264augroup END 265 266let s: li = [] 267split Xdummywipetest1.txt 268silent ! vimgrep /notmatched/ Xdummywipetest* 269call assert_equal ([ "bufunload" , "bufwipeout" ], s: li ) 270 271bwipeout 272au ! test_bufunload_group 273augroup ! test_bufunload_group 274endfunc 275 276func Test_win_tab_autocmd () 277let g: record = [] 278 279defer CleanUpTestAuGroup() 280augroup testing 281au WinNew Pre * call add(g: record, 'WinNewPre') 282au WinNew * call add ( g: record , 'WinNew' ) 283au WinClosed * call add ( g: record , 'WinClosed' ) 284au WinEnter * call add ( g: record , 'WinEnter' ) 285au WinLeave * call add ( g: record , 'WinLeave' ) 286au TabNew * call add ( g: record , 'TabNew' ) 287au TabClosed * call add ( g: record , 'TabClosed' ) 288au TabEnter * call add ( g: record , 'TabEnter' ) 289au TabLeave * call add ( g: record , 'TabLeave' ) 290augroup END 291 292split 293tabnew 294close 295close 296 297call assert_equal ([ 298 \'WinNewPre' , 'WinLeave' , 'WinNew' , 'WinEnter' , 299 \'WinLeave' , 'TabLeave' , 'WinNew' , 'WinEnter' , 'TabNew' , 'TabEnter' , 300 \'WinLeave' , 'TabLeave' , 'WinClosed' , 'TabClosed' , 'WinEnter' , 'TabEnter' , 301 \'WinLeave' , 'WinClosed' , 'WinEnter' 302 \], g: record ) 303 304let g: record = [] 305tabnew somefile 306tabnext 307bwipe somefile 308 309call assert_equal ([ 310 \'WinLeave' , 'TabLeave' , 'WinNew' , 'WinEnter' , 'TabNew' , 'TabEnter' , 311 \'WinLeave' , 'TabLeave' , 'WinEnter' , 'TabEnter' , 312 \'WinClosed' , 'TabClosed' 313 \], g: record ) 314 315let g: record = [] 316copen 317help 318tabnext 319vnew 320 321call assert_equal ([ 322 \'WinNewPre' , 'WinLeave' , 'WinNew' , 'WinEnter' , 323 \'WinNewPre' , 'WinLeave' , 'WinNew' , 'WinEnter' , 324 \'WinNewPre' , 'WinLeave' , 'WinNew' , 'WinEnter' 325 \], g: record ) 326 327unlet g: record 328endfunc 329 330func Test_WinNewPre () 331" Test that the old window layout can be accessed before a new window is created. 332let g: layouts_pre = [] 333let g: layouts_post = [] 334augroup testing 335au WinNew Pre * call add(g: layouts_pre, winlayout()) 336au WinNew * call add ( g: layouts_post , winlayout ()) 337augroup END 338defer CleanUpTestAuGroup() 339split 340call assert_notequal ( g: layouts_pre [ 0 ], g: layouts_post [ 0 ]) 341split 342call assert_equal ( g: layouts_pre [ 1 ], g: layouts_post [ 0 ]) 343call assert_notequal ( g: layouts_pre [ 1 ], g: layouts_post [ 1 ]) 344" not triggered for tabnew 345tabnew 346call assert_equal ( 2 , len ( g: layouts_pre )) 347unlet g: layouts_pre 348unlet g: layouts_post 349 350" Test modifying window layout during WinNewPre throws. 351let g: caught = 0 352augroup testing 353au ! 354au WinNew Pre * split 355augroup END 356try 357vnew 358catch 359let g: caught += 1 360endtry 361augroup testing 362au ! 363au WinNew Pre * tabnew 364augroup END 365try 366vnew 367catch 368let g: caught += 1 369endtry 370augroup testing 371au ! 372au WinNew Pre * close 373augroup END 374try 375vnew 376catch 377let g: caught += 1 378endtry 379augroup testing 380au ! 381au WinNew Pre * tabclose 382augroup END 383try 384vnew 385catch 386let g: caught += 1 387endtry 388call assert_equal ( 4 , g: caught ) 389unlet g: caught 390endfunc 391 392func Test_WinResized () 393CheckRunVimInTerminal 394 395let lines =<< trim END 396set scrolloff=0 397call setline(1, ['111', '222']) 398vnew 399call setline(1, ['aaa', 'bbb']) 400new 401call setline(1, ['foo', 'bar']) 402 403let g:resized = 0 404au WinResized * let g:resized += 1 405 406func WriteResizedEvent() 407call writefile([json_encode(v:event)], 'XresizeEvent') 408endfunc 409au WinResized * call WriteResizedEvent() 410END 411call writefile ( lines , 'Xtest_winresized' , 'D' ) 412let buf = RunVimInTerminal ( '-S Xtest_winresized' , { 'rows' : 10 }) 413 414" redraw now to avoid a redraw after the :echo command 415call term_sendkeys ( buf , ":redraw!\<CR>" ) 416call TermWait ( buf ) 417 418call term_sendkeys ( buf , ":echo g:resized\<CR>" ) 419call WaitForAssert ({ ->assert_match ( '^0$' , term_getline ( buf , 10 ))}, 1000 ) 420 421" increase window height, two windows will be reported 422call term_sendkeys ( buf , "\<C-W>+" ) 423call TermWait ( buf ) 424call term_sendkeys ( buf , ":echo g:resized\<CR>" ) 425call WaitForAssert ({ ->assert_match ( '^1$' , term_getline ( buf , 10 ))}, 1000 ) 426 427let event = readfile ( 'XresizeEvent' )[ 0 ] ->json_decode () 428call assert_equal ({ 429 \'windows' : [ 1002 , 1001 ], 430 \}, event ) 431 432" increase window width, three windows will be reported 433call term_sendkeys ( buf , "\<C-W>>" ) 434call TermWait ( buf ) 435call term_sendkeys ( buf , ":echo g:resized\<CR>" ) 436call WaitForAssert ({ ->assert_match ( '^2$' , term_getline ( buf , 10 ))}, 1000 ) 437 438let event = readfile ( 'XresizeEvent' )[ 0 ] ->json_decode () 439call assert_equal ({ 440 \'windows' : [ 1002 , 1001 , 1000 ], 441 \}, event ) 442 443call delete ( 'XresizeEvent' ) 444call StopVimInTerminal ( buf ) 445endfunc 446 447func Test_WinScrolled () 448CheckRunVimInTerminal 449 450let lines =<< trim END 451set nowrap scrolloff=0 452for ii in range(1, 18) 453call setline(ii, repeat(nr2char(96 + ii), ii * 2)) 454endfor 455let win_id = win_getid() 456let g:matched = v:false 457func WriteScrollEvent() 458call writefile([json_encode(v:event)], 'XscrollEvent') 459endfunc 460execute 'au WinScrolled' win_id 'let g:matched = v:true' 461let g:scrolled = 0 462au WinScrolled * let g:scrolled += 1 463au WinScrolled * let g:amatch = str2nr(expand('<amatch>')) 464au WinScrolled * let g:afile = str2nr(expand('<afile>')) 465au WinScrolled * call WriteScrollEvent() 466END 467call writefile ( lines , 'Xtest_winscrolled' , 'D' ) 468let buf = RunVimInTerminal ( '-S Xtest_winscrolled' , { 'rows' : 6 }) 469 470call term_sendkeys ( buf , ":echo g:scrolled\<CR>" ) 471call WaitForAssert ({ ->assert_match ( '^0 ' , term_getline ( buf , 6 ))}, 1000 ) 472 473" Scroll left/right in Normal mode. 474call term_sendkeys ( buf , "zlzh:echo g:scrolled\<CR>" ) 475call WaitForAssert ({ ->assert_match ( '^2 ' , term_getline ( buf , 6 ))}, 1000 ) 476 477let event = readfile ( 'XscrollEvent' )[ 0 ] ->json_decode () 478call assert_equal ({ 479 \'all' : { 'leftcol' : 1 , 'topline' : 0 , 'topfill' : 0 , 'width' : 0 , 'height' : 0 , 'skipcol' : 0 }, 480 \'1000' : { 'leftcol' : -1 , 'topline' : 0 , 'topfill' : 0 , 'width' : 0 , 'height' : 0 , 'skipcol' : 0 } 481 \}, event ) 482 483" Scroll up/down in Normal mode. 484call term_sendkeys ( buf , "\<c-e>\<c-y>:echo g:scrolled\<CR>" ) 485call WaitForAssert ({ ->assert_match ( '^4 ' , term_getline ( buf , 6 ))}, 1000 ) 486 487let event = readfile ( 'XscrollEvent' )[ 0 ] ->json_decode () 488call assert_equal ({ 489 \'all' : { 'leftcol' : 0 , 'topline' : 1 , 'topfill' : 0 , 'width' : 0 , 'height' : 0 , 'skipcol' : 0 }, 490 \'1000' : { 'leftcol' : 0 , 'topline' : -1 , 'topfill' : 0 , 'width' : 0 , 'height' : 0 , 'skipcol' : 0 } 491 \}, event ) 492 493" Scroll up/down in Insert mode. 494call term_sendkeys ( buf , "Mi\<c-x>\<c-e>\<Esc>i\<c-x>\<c-y>\<Esc>" ) 495call term_sendkeys ( buf , ":echo g:scrolled\<CR>" ) 496call WaitForAssert ({ ->assert_match ( '^6 ' , term_getline ( buf , 6 ))}, 1000 ) 497 498let event = readfile ( 'XscrollEvent' )[ 0 ] ->json_decode () 499call assert_equal ({ 500 \'all' : { 'leftcol' : 0 , 'topline' : 1 , 'topfill' : 0 , 'width' : 0 , 'height' : 0 , 'skipcol' : 0 }, 501 \'1000' : { 'leftcol' : 0 , 'topline' : -1 , 'topfill' : 0 , 'width' : 0 , 'height' : 0 , 'skipcol' : 0 } 502 \}, event ) 503 504" Scroll the window horizontally to focus the last letter of the third line 505" containing only six characters. Moving to the previous and shorter lines 506" should trigger another autocommand as Vim has to make them visible. 507call term_sendkeys ( buf , "5zl2k" ) 508call term_sendkeys ( buf , ":echo g:scrolled\<CR>" ) 509call WaitForAssert ({ ->assert_match ( '^8 ' , term_getline ( buf , 6 ))}, 1000 ) 510 511let event = readfile ( 'XscrollEvent' )[ 0 ] ->json_decode () 512call assert_equal ({ 513 \'all' : { 'leftcol' : 5 , 'topline' : 0 , 'topfill' : 0 , 'width' : 0 , 'height' : 0 , 'skipcol' : 0 }, 514 \'1000' : { 'leftcol' : -5 , 'topline' : 0 , 'topfill' : 0 , 'width' : 0 , 'height' : 0 , 'skipcol' : 0 } 515 \}, event ) 516 517" Ensure the command was triggered for the specified window ID. 518call term_sendkeys ( buf , ":echo g:matched\<CR>" ) 519call WaitForAssert ({ ->assert_match ( '^v:true ' , term_getline ( buf , 6 ))}, 1000 ) 520 521" Ensure the expansion of <amatch> and <afile> matches the window ID. 522call term_sendkeys ( buf , ":echo g:amatch == win_id && g:afile == win_id\<CR>" ) 523call WaitForAssert ({ ->assert_match ( '^v:true ' , term_getline ( buf , 6 ))}, 1000 ) 524 525call delete ( 'XscrollEvent' ) 526call StopVimInTerminal ( buf ) 527endfunc 528 529func Test_WinScrolled_mouse () 530CheckRunVimInTerminal 531 532let lines =<< trim END 533set nowrap scrolloff=0 534set mouse=a term=xterm ttymouse=sgr mousetime=200 clipboard= 535call setline(1, ['foo']->repeat(32)) 536split 537let g:scrolled = 0 538au WinScrolled * let g:scrolled += 1 539END 540call writefile ( lines , 'Xtest_winscrolled_mouse' , 'D' ) 541let buf = RunVimInTerminal ( '-S Xtest_winscrolled_mouse' , { 'rows' : 10 }) 542 543" With the upper split focused, send a scroll-down event to the unfocused one. 544call test_setmouse ( 7 , 1 ) 545call term_sendkeys ( buf , "\<ScrollWheelDown>" ) 546call TermWait ( buf ) 547call term_sendkeys ( buf , ":echo g:scrolled\<CR>" ) 548call WaitForAssert ({ ->assert_match ( '^1' , term_getline ( buf , 10 ))}, 1000 ) 549 550" Again, but this time while we're in insert mode. 551call term_sendkeys ( buf , "i\<ScrollWheelDown>\<Esc>" ) 552call TermWait ( buf ) 553call term_sendkeys ( buf , ":echo g:scrolled\<CR>" ) 554call WaitForAssert ({ ->assert_match ( '^2' , term_getline ( buf , 10 ))}, 1000 ) 555 556call StopVimInTerminal ( buf ) 557endfunc 558 559func Test_WinScrolled_close_curwin () 560CheckRunVimInTerminal 561 562let lines =<< trim END 563set nowrap scrolloff=0 564call setline(1, ['aaa', 'bbb']) 565vsplit 566au WinScrolled * close 567au VimLeave * call writefile(['123456'], 'Xtestout') 568END 569call writefile ( lines , 'Xtest_winscrolled_close_curwin' , 'D' ) 570let buf = RunVimInTerminal ( '-S Xtest_winscrolled_close_curwin' , { 'rows' : 6 }) 571 572" This was using freed memory 573call term_sendkeys ( buf , "\<C-E>" ) 574call TermWait ( buf ) 575call StopVimInTerminal ( buf ) 576 577" check the startup script finished to the end 578call assert_equal ([ '123456' ], readfile ( 'Xtestout' )) 579call delete ( 'Xtestout' ) 580endfunc 581 582func Test_WinScrolled_once_only () 583CheckScreendump 584CheckRunVimInTerminal 585 586let lines =<< trim END 587set cmdheight=2 588call setline(1, ['aaa', 'bbb']) 589let trigger_count = 0 590func ShowInfo(id) 591echo g:trigger_count g:winid winlayout() 592endfunc 593 594vsplit 595split 596" use a timer to show the info after a redraw 597au WinScrolled * let trigger_count += 1 | let winid = expand('<amatch>') | call timer_start(100, 'ShowInfo') 598wincmd j 599wincmd l 600END 601call writefile ( lines , 'Xtest_winscrolled_once' , 'D' ) 602let buf = RunVimInTerminal ( '-S Xtest_winscrolled_once' , #{ rows : 10 , cols : 60 , statusoff : 2 }) 603 604call term_sendkeys ( buf , "\<C-E>" ) 605call VerifyScreenDump ( buf , 'Test_winscrolled_once_only_1' , {}) 606 607call StopVimInTerminal ( buf ) 608endfunc 609 610" Check that WinScrolled is not triggered immediately when defined and there 611" are split windows. 612func Test_WinScrolled_not_when_defined () 613CheckScreendump 614CheckRunVimInTerminal 615 616let lines =<< trim END 617call setline(1, ['aaa', 'bbb']) 618echo 'nothing happened' 619func ShowTriggered(id) 620echo 'triggered' 621endfunc 622END 623call writefile ( lines , 'Xtest_winscrolled_not' , 'D' ) 624let buf = RunVimInTerminal ( '-S Xtest_winscrolled_not' , #{ rows : 10 , cols : 60 , statusoff : 2 }) 625call term_sendkeys ( buf , ":split\<CR>" ) 626call TermWait ( buf ) 627" use a timer to show the message after redrawing 628call term_sendkeys ( buf , ":au WinScrolled * call timer_start(100, 'ShowTriggered')\<CR>" ) 629call VerifyScreenDump ( buf , 'Test_winscrolled_not_when_defined_1' , {}) 630 631call term_sendkeys ( buf , "\<C-E>" ) 632call VerifyScreenDump ( buf , 'Test_winscrolled_not_when_defined_2' , {}) 633 634call StopVimInTerminal ( buf ) 635endfunc 636 637func Test_WinScrolled_long_wrapped () 638CheckRunVimInTerminal 639 640let lines =<< trim END 641set scrolloff=0 642let height = winheight(0) 643let width = winwidth(0) 644let g:scrolled = 0 645au WinScrolled * let g:scrolled += 1 646call setline(1, repeat('foo', height * width)) 647call cursor(1, height * width) 648END 649call writefile ( lines , 'Xtest_winscrolled_long_wrapped' , 'D' ) 650let buf = RunVimInTerminal ( '-S Xtest_winscrolled_long_wrapped' , { 'rows' : 6 }) 651 652call term_sendkeys ( buf , ":echo g:scrolled\<CR>" ) 653call WaitForAssert ({ ->assert_match ( '^0 ' , term_getline ( buf , 6 ))}, 1000 ) 654 655call term_sendkeys ( buf , 'gj' ) 656call term_sendkeys ( buf , ":echo g:scrolled\<CR>" ) 657call WaitForAssert ({ ->assert_match ( '^1 ' , term_getline ( buf , 6 ))}, 1000 ) 658 659call term_sendkeys ( buf , '0' ) 660call term_sendkeys ( buf , ":echo g:scrolled\<CR>" ) 661call WaitForAssert ({ ->assert_match ( '^2 ' , term_getline ( buf , 6 ))}, 1000 ) 662 663call term_sendkeys ( buf , '$' ) 664call term_sendkeys ( buf , ":echo g:scrolled\<CR>" ) 665call WaitForAssert ({ ->assert_match ( '^3 ' , term_getline ( buf , 6 ))}, 1000 ) 666 667call StopVimInTerminal ( buf ) 668endfunc 669 670func Test_WinScrolled_diff () 671CheckRunVimInTerminal 672 673let lines =<< trim END 674set diffopt+=foldcolumn:0 675call setline(1, ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i']) 676vnew 677call setline(1, ['d', 'e', 'f', 'g', 'h', 'i']) 678windo diffthis 679func WriteScrollEvent() 680call writefile([json_encode(v:event)], 'XscrollEvent') 681endfunc 682au WinScrolled * call WriteScrollEvent() 683END 684call writefile ( lines , 'Xtest_winscrolled_diff' , 'D' ) 685let buf = RunVimInTerminal ( '-S Xtest_winscrolled_diff' , { 'rows' : 8 }) 686 687call term_sendkeys ( buf , "\<C-E>" ) 688call WaitForAssert ({ ->assert_match ( '^d' , term_getline ( buf , 3 ))}, 1000 ) 689 690let event = readfile ( 'XscrollEvent' )[ 0 ] ->json_decode () 691call assert_equal ({ 692 \'all' : { 'leftcol' : 0 , 'topline' : 1 , 'topfill' : 1 , 'width' : 0 , 'height' : 0 , 'skipcol' : 0 }, 693 \'1000' : { 'leftcol' : 0 , 'topline' : 1 , 'topfill' : 0 , 'width' : 0 , 'height' : 0 , 'skipcol' : 0 }, 694 \'1001' : { 'leftcol' : 0 , 'topline' : 0 , 'topfill' : -1 , 'width' : 0 , 'height' : 0 , 'skipcol' : 0 } 695 \}, event ) 696 697call term_sendkeys ( buf , "2\<C-E>" ) 698call WaitForAssert ({ ->assert_match ( '^f' , term_getline ( buf , 3 ))}, 1000 ) 699 700let event = readfile ( 'XscrollEvent' )[ 0 ] ->json_decode () 701call assert_equal ({ 702 \'all' : { 'leftcol' : 0 , 'topline' : 2 , 'topfill' : 2 , 'width' : 0 , 'height' : 0 , 'skipcol' : 0 }, 703 \'1000' : { 'leftcol' : 0 , 'topline' : 2 , 'topfill' : 0 , 'width' : 0 , 'height' : 0 , 'skipcol' : 0 }, 704 \'1001' : { 'leftcol' : 0 , 'topline' : 0 , 'topfill' : -2 , 'width' : 0 , 'height' : 0 , 'skipcol' : 0 } 705 \}, event ) 706 707call term_sendkeys ( buf , "\<C-E>" ) 708call WaitForAssert ({ ->assert_match ( '^g' , term_getline ( buf , 3 ))}, 1000 ) 709 710let event = readfile ( 'XscrollEvent' )[ 0 ] ->json_decode () 711call assert_equal ({ 712 \'all' : { 'leftcol' : 0 , 'topline' : 2 , 'topfill' : 0 , 'width' : 0 , 'height' : 0 , 'skipcol' : 0 }, 713 \'1000' : { 'leftcol' : 0 , 'topline' : 1 , 'topfill' : 0 , 'width' : 0 , 'height' : 0 , 'skipcol' : 0 }, 714 \'1001' : { 'leftcol' : 0 , 'topline' : 1 , 'topfill' : 0 , 'width' : 0 , 'height' : 0 , 'skipcol' : 0 } 715 \}, event ) 716 717call term_sendkeys ( buf , "2\<C-Y>" ) 718call WaitForAssert ({ ->assert_match ( '^e' , term_getline ( buf , 3 ))}, 1000 ) 719 720let event = readfile ( 'XscrollEvent' )[ 0 ] ->json_decode () 721call assert_equal ({ 722 \'all' : { 'leftcol' : 0 , 'topline' : 3 , 'topfill' : 1 , 'width' : 0 , 'height' : 0 , 'skipcol' : 0 }, 723 \'1000' : { 'leftcol' : 0 , 'topline' : -2 , 'topfill' : 0 , 'width' : 0 , 'height' : 0 , 'skipcol' : 0 }, 724 \'1001' : { 'leftcol' : 0 , 'topline' : -1 , 'topfill' : 1 , 'width' : 0 , 'height' : 0 , 'skipcol' : 0 } 725 \}, event ) 726 727call StopVimInTerminal ( buf ) 728call delete ( 'XscrollEvent' ) 729endfunc 730 731func Test_WinClosed () 732" Test that the pattern is matched against the closed window's ID, and both 733" <amatch> and <afile> are set to it. 734new 735let winid = win_getid () 736let g: matched = v: false 737augroup test -WinClosed 738autocmd ! 739execute 'autocmd WinClosed' winid 'let g:matched = v:true' 740autocmd WinClosed * let g: amatch = str2nr ( expand ( '<amatch>' )) 741autocmd WinClosed * let g: afile = str2nr ( expand ( '<afile>' )) 742augroup END 743close 744call assert_true ( g: matched ) 745call assert_equal ( winid , g: amatch ) 746call assert_equal ( winid , g: afile ) 747 748" Test that WinClosed is non-recursive. 749new 750new 751call assert_equal ( 3 , winnr ( '$' )) 752let g: triggered = 0 753augroup test -WinClosed 754autocmd ! 755autocmd WinClosed * let g: triggered += 1 756autocmd WinClosed * 2 wincmd c 757augroup END 758close 759call assert_equal ( 1 , winnr ( '$' )) 760call assert_equal ( 1 , g: triggered ) 761 762autocmd ! test-WinClosed 763augroup ! test -WinClosed 764unlet g: matched 765unlet g: amatch 766unlet g: afile 767unlet g: triggered 768endfunc 769 770func Test_WinClosed_throws () 771vnew 772let bnr = bufnr () 773call assert_equal ( 1 , bufloaded ( bnr )) 774augroup test -WinClosed 775autocmd WinClosed * throw 'foo' 776augroup END 777try 778close 779catch /.*/ 780endtry 781call assert_equal ( 0 , bufloaded ( bnr )) 782 783autocmd ! test-WinClosed 784augroup ! test -WinClosed 785endfunc 786 787func Test_WinClosed_throws_with_tabs () 788tabnew 789let bnr = bufnr () 790call assert_equal ( 1 , bufloaded ( bnr )) 791augroup test -WinClosed 792autocmd WinClosed * throw 'foo' 793augroup END 794try 795close 796catch /.*/ 797endtry 798call assert_equal ( 0 , bufloaded ( bnr )) 799 800autocmd ! test-WinClosed 801augroup ! test -WinClosed 802endfunc 803 804" This used to trigger WinClosed twice for the same window, and the window's 805" buffer was NULL in the second autocommand. 806func Test_WinClosed_switch_tab () 807edit Xa 808split Xb 809split Xc 810tab split 811new 812augroup test -WinClosed 813autocmd WinClosed * tabprev |bwipe ! 814augroup END 815close 816" Check that the tabline has been fully removed 817call assert_equal ([ 1 , 1 ], win_screenpos ( 0 )) 818 819autocmd ! test-WinClosed 820augroup ! test -WinClosed 821 %bwipe ! 822endfunc 823 824" This used to trigger WinClosed/WinLeave/BufLeave twice for the same window, 825" and the window's buffer was NULL in the second autocommand. 826func Run_test_BufUnload_close_other ( extra_cmd ) 827let oldtab = tabpagenr () 828tabnew Xb1 829let g: tab = tabpagenr () 830let g: w1 = win_getid () 831new Xb2 832let g: w2 = win_getid () 833let g: log = [] 834exe a: extra_cmd 835 836augroup test -BufUnload-close-other 837autocmd BufUnload * ++nested++ once bwipe! Xb1 838for event in [ 'WinClosed' , 'BufLeave' , 'WinLeave' , 'TabLeave' ] 839exe $ 'autocmd {event} * call tabpagebuflist(g: tab )' 840exe $ 'autocmd {event} * letg: log + = [ "{event}:" .. expand ( "<afile>" )] ' 841endfor 842augroup END 843 844close 845" WinClosed is triggered once for each of the 2 closed windows. 846" Others are only triggered once. 847call assert_equal ([ 'BufLeave:Xb2' , 'WinLeave:Xb2' , $ ' WinClosed:{g:w2}', 848\ $' WinClosed:{g:w1}', ' TabLeave:Xb2'], g: log ) 849call assert_equal ( oldtab , tabpagenr ()) 850call assert_equal ([ 0 , 0 ], win_id2tabwin ( g: w1 )) 851call assert_equal ([ 0 , 0 ], win_id2tabwin ( g: w2 )) 852 853unlet g: tab 854unlet g: w1 855unlet g: w2 856unlet g: log 857autocmd ! test-BufUnload-close-other 858augroup ! test - BufUnload-close-other 859% bwipe ! 860endfunc 861 862func Test_BufUnload_close_other () 863call Run_test_BufUnload_close_other ( '' ) 864call Run_test_BufUnload_close_other ( 'setlocal bufhidden=wipe' ) 865endfunc 866 867func Run_test_BufUnload_tabonly ( first_cmd ) 868exe a: first_cmd 869tabnew Xa 870setlocal bufhidden = wipe 871tabprevious 872autocmd BufWinLeave Xa ++once tabnext 873autocmd BufUnload Xa ++once tabonly 874tabonly 875 876% bwipe ! 877endfunc 878 879func Test_BufUnload_tabonly () 880" This used to dereference a NULL curbuf. 881call Run_test_BufUnload_tabonly ( 'setlocal bufhidden=hide' ) 882" This used to dereference a NULL firstbuf. 883call Run_test_BufUnload_tabonly ( 'setlocal bufhidden=wipe' ) 884endfunc 885 886func Run_test_BufUnload_tabonly_nested ( second_autocmd ) 887file Xa 888tabnew Xb 889setlocal bufhidden = wipe 890tabnew Xc 891setlocal bufhidden = wipe 892autocmd BufUnload Xb ++once ++nested bwipe ! Xa 893exe $ 'autocmd BufUnload Xa ++once ++nested {a:second_autocmd}' 894autocmd BufWinLeave Xc ++once tabnext 895tabfirst 8962 tabclose 897 898% bwipe ! 899endfunc 900 901func Test_BufUnload_tabonly_nested () 902" These used to cause heap-use-after-free. 903call Run_test_BufUnload_tabonly_nested ( 'tabonly' ) 904call Run_test_BufUnload_tabonly_nested ( 'tabonly | tabprevious' ) 905endfunc 906 907func s: AddAnAutocmd () 908augroup vimBarTest 909au BufReadCmd * echo 'hello' 910augroup END 911call assert_equal ( 3 , len ( split ( execute ( 'au vimBarTest' ), "\n" ))) 912endfunc 913 914func Test_early_bar () 915" test that a bar is recognized before the {event} 916call s: AddAnAutocmd () 917augroup vimBarTest | au ! | let done = 77 | augroup END 918call assert_equal ( 1 , len ( split ( execute ( 'au vimBarTest' ), "\n" ))) 919call assert_equal ( 77 , done ) 920 921call s: AddAnAutocmd () 922augroup vimBarTest | au ! | let done = 88 | augroup END 923call assert_equal ( 1 , len ( split ( execute ( 'au vimBarTest' ), "\n" ))) 924call assert_equal ( 88 , done ) 925 926" test that a bar is recognized after the {event} 927call s: AddAnAutocmd () 928augroup vimBarTest | au !BufReadCmd | let done = 99 | augroup END 929call assert_equal ( 1 , len ( split ( execute ( 'au vimBarTest' ), "\n" ))) 930call assert_equal ( 99 , done ) 931 932" test that a bar is recognized after the {group} 933call s: AddAnAutocmd () 934au ! vimBarTest | echo 'h ello ' 935call assert_equal ( 1 , len ( split ( execute ( 'au vimBarTest' ), "\n" ))) 936endfunc 937 938func RemoveGroup () 939autocmd ! StartOK 940augroup ! StartOK 941endfunc 942 943func Test_augroup_warning () 944augroup TheWarning 945au VimEnter * echo 'entering' 946augroup END 947call assert_match ( "TheWarning.*VimEnter" , execute ( 'au VimEnter' )) 948redir => res 949augroup ! TheWarning 950redir END 951call assert_match ( "W19:" , res ) 952call assert_match ( "-Deleted-.*VimEnter" , execute ( 'au VimEnter' )) 953 954" check "Another" does not take the pace of the deleted entry 955augroup Another 956augroup END 957call assert_match ( "-Deleted-.*VimEnter" , execute ( 'au VimEnter' )) 958augroup ! Another 959 960" no warning for postpone aucmd delete 961augroup StartOK 962au VimEnter * call RemoveGroup () 963augroup END 964call assert_match ( "StartOK.*VimEnter" , execute ( 'au VimEnter' )) 965redir => res 966doautocmd VimEnter 967redir END 968call assert_notmatch ( "W19:" , res ) 969au ! VimEnter 970 971call assert_fails ( 'augroup!' , 'E471:' ) 972endfunc 973 974func Test_BufReadCmdHelp () 975" This used to cause access to free memory 976au BufReadCmd * e + h 977help 978 979au ! BufReadCmd 980endfunc 981 982func Test_BufReadCmdHelpJump () 983" This used to cause access to free memory 984au BufReadCmd * e + h{ 985" } to fix highlighting 986call assert_fails ( 'help' , 'E434:' ) 987 988au ! BufReadCmd 989endfunc 990 991" BufReadCmd is triggered for a "nofile" buffer. Check all values. 992func Test_BufReadCmdNofile () 993for val in [ 'nofile' , 994\ 'nowrite' , 995\ 'acwrite' , 996\ 'quickfix' , 997\ 'help' , 998"\ 'terminal', 999"\ 'prompt', 1000"\ 'popup', 1001\ ] 1002new somefile 1003exe 'set buftype=' .. val 1004au BufReadCmd somefile call setline ( 1 , 'triggered' ) 1005edit 1006call assert_equal ( 'triggered' , getline ( 1 )) 1007 1008au ! BufReadCmd 1009bwipe ! 1010endfor 1011endfunc 1012 1013func Test_augroup_deleted () 1014" This caused a crash before E936 was introduced 1015augroup x 1016call assert_fails ( 'augroup! x' , 'E936:' ) 1017au VimEnter * echo 1018augroup end 1019augroup ! x 1020call assert_match ( "-Deleted-.*VimEnter" , execute ( 'au VimEnter' )) 1021au ! VimEnter 1022endfunc 1023 1024" Tests for autocommands on :close command. 1025" This used to be in test13. 1026func Test_three_windows () 1027" Clean up buffers, because in some cases this function fails. 1028call s: cleanup_buffers () 1029 1030" Write three files and open them, each in a window. 1031" Then go to next window, with autocommand that deletes the previous one. 1032" Do this twice, writing the file. 1033e ! Xtestje1 1034call setline ( 1 , 'testje1' ) 1035w 1036sp Xtestje2 1037call setline ( 1 , 'testje2' ) 1038w 1039sp Xtestje3 1040call setline ( 1 , 'testje3' ) 1041w 1042wincmd w 1043au WinLeave Xtestje2 bwipe 1044wincmd w 1045call assert_equal ( 'Xtestje1' , expand ( '%' )) 1046 1047au WinLeave Xtestje1 bwipe Xtestje3 1048close 1049call assert_equal ( 'Xtestje1' , expand ( '%' )) 1050 1051" Test deleting the buffer on a Unload event. If this goes wrong there 1052" will be the ATTENTION prompt. 1053e Xtestje1 1054au ! 1055au ! BufUnload Xtestje1 bwipe 1056call assert_fails ( 'e Xtestje3' , 'E937:' ) 1057call assert_equal ( 'Xtestje3' , expand ( '%' )) 1058 1059e Xtestje2 1060sp Xtestje1 1061call assert_fails ( 'e' , 'E937:' ) 1062call assert_equal ( 'Xtestje1' , expand ( '%' )) 1063 1064" Test changing buffers in a BufWipeout autocommand. If this goes wrong 1065" there are ml_line errors and/or a Crash. 1066au ! 1067only 1068e Xanother 1069e Xtestje1 1070bwipe Xtestje2 1071bwipe Xtestje3 1072au BufWipeout Xtestje1 buf Xtestje1 1073bwipe 1074call assert_equal ( 'Xanother' , expand ( '%' )) 1075 1076only 1077 1078help 1079wincmd w 10801 quit 1081call assert_equal ( 'Xanother' , expand ( '%' )) 1082 1083au ! 1084enew 1085call delete ( 'Xtestje1' ) 1086call delete ( 'Xtestje2' ) 1087call delete ( 'Xtestje3' ) 1088endfunc 1089 1090func Test_BufEnter () 1091au ! BufEnter 1092au Bufenter * let val = val . '+' 1093let g: val = '' 1094split NewFile 1095call assert_equal ( '+' , g: val ) 1096bwipe ! 1097call assert_equal ( '++' , g: val ) 1098 1099" Also get BufEnter when editing a directory 1100call mkdir ( 'Xbufenterdir' , 'D' ) 1101split Xbufenterdir 1102call assert_equal ( '+++' , g: val ) 1103 1104" On MS-Windows we can't edit the directory, make sure we wipe the right 1105" buffer. 1106bwipe ! Xbufenterdir 1107au ! BufEnter 1108 1109" Editing a "nofile" buffer doesn't read the file but does trigger BufEnter 1110" for historic reasons. Also test other 'buftype' values. 1111for val in [ 'nofile' , 1112\ 'nowrite' , 1113\ 'acwrite' , 1114\ 'quickfix' , 1115\ 'help' , 1116"\ 'terminal', 1117"\ 'prompt', 1118"\ 'popup', 1119\ ] 1120new somefile 1121exe 'set buftype=' .. val 1122au BufEnter somefile call setline ( 1 , 'some text' ) 1123edit 1124call assert_equal ( 'some text' , getline ( 1 )) 1125bwipe ! 1126au ! BufEnter 1127endfor 1128 1129new 1130new 1131autocmd BufEnter * ++once close 1132call assert_fails ( 'close' , 'E1312:' ) 1133 1134au ! BufEnter 1135only 1136endfunc 1137 1138func Test_autocmd_SessLoadPre () 1139tabnew 1140set noswapfile 1141mkview ! Xview.vim 1142mksession ! Session.vim 1143 1144call assert_false ( exists ( 'g:session_loaded_var' )) 1145 1146let content =<< trim [CODE] 1147set nocp noswapfile 1148 1149func! Assert(cond, msg) 1150if !a:cond 1151echomsg "ASSERT_FAIL: " .. a:msg 1152else 1153echomsg "ASSERT_OK: " .. a:msg 1154endif 1155endfunc 1156 1157func! OnSessionLoadPre() 1158call Assert(exists('g:SessionLoad'), 'SessionLoadPre: session IS loading') 1159call Assert(!exists('g:session_loaded_var'), 1160\ 'SessionLoadPre: var NOT set') 1161endfunc 1162au SessionLoadPre * call OnSessionLoadPre() 1163 1164func! OnSessionLoadPost() 1165call Assert(exists('g:session_loaded_var'), 1166\ 'SessionLoadPost: var IS set') 1167echomsg "SessionLoadPost DONE" 1168endfunc 1169au SessionLoadPost * call OnSessionLoadPost() 1170 1171func! WriteErrors() 1172call writefile([execute("messages")], "XerrorsPost") 1173endfunc 1174au VimLeave * call WriteErrors() 1175[CODE] 1176 1177call writefile ( content , 'Xvimrc' , 'D' ) 1178 1179call writefile ( 1180\ [ 'let g:session_loaded_var = 1' ], 1181\ 'Sessionx.vim' , 1182\ 'b' 1183\ ) 1184 1185" --- Run child Vim --- 1186call system ( 1187\ GetVimCommand ( 'Xvimrc' ) 1188\ .. ' --headless --noplugins -S Xview.vim -S Session.vim -c cq' 1189\ ) 1190 1191call WaitForAssert ({ -> assert_true ( filereadable ( 'XerrorsPost' ))}) 1192 1193let errors = join ( readfile ( 'XerrorsPost' ), "\n" ) 1194call assert_notmatch ( 'ASSERT_FAIL' , errors ) 1195call assert_match ( 'ASSERT_OK: SessionLoadPre: var NOT set' , errors ) 1196call assert_match ( 'ASSERT_OK: SessionLoadPost: var IS set' , errors ) 1197call assert_match ( 'SessionLoadPost DONE' , errors ) 1198 1199set swapfile 1200for file in [ 'Session.vim' , 'Sessionx.vim' , 'Xview.vim' , 'XerrorsPost' ] 1201call delete ( file ) 1202endfor 1203endfunc 1204 1205" Closing a window might cause an endless loop 1206" E814 for older Vims 1207func Test_autocmd_bufwipe_in_SessLoadPost () 1208set noswapfile 1209edit Xtest 1210tabnew 1211file Xsomething 1212mksession ! 1213 1214let content =<< trim [CODE] 1215set nocp noswapfile 1216let v:swapchoice = "e" 1217augroup test_autocmd_sessionload 1218autocmd! 1219autocmd SessionLoadPost * exe bufnr("Xsomething") . "bw!" 1220augroup END 1221 1222func WriteErrors() 1223call writefile([execute("messages")], "XerrorsBwipe") 1224endfunc 1225au VimLeave * call WriteErrors() 1226[CODE] 1227 1228call writefile ( content , 'Xvimrc' , 'D' ) 1229call system ( GetVimCommand ( 'Xvimrc' ) .. ' --headless --noplugins -S Session.vim -c cq' ) 1230sleep 100m 1231let errors = join ( readfile ( 'XerrorsBwipe' )) 1232call assert_match ( 'E814:' , errors ) 1233 1234set swapfile 1235for file in [ 'Session.vim' , 'XerrorsBwipe' ] 1236call delete ( file ) 1237endfor 1238endfunc 1239 1240" Using :blast and :ball for many events caused a crash, because b_nwindows was 1241" not incremented correctly. 1242func Test_autocmd_blast_badd () 1243let content =<< trim [CODE] 1244au BufNew,BufAdd,BufWinEnter,BufEnter,BufLeave,BufWinLeave,BufUnload,VimEnter foo* blast 1245edit foo1 1246au BufNew,BufAdd,BufWinEnter,BufEnter,BufLeave,BufWinLeave,BufUnload,VimEnter foo* ball 1247edit foo2 1248call writefile(['OK'], 'XerrorsBlast') 1249qall 1250[CODE] 1251 1252call writefile ( content , 'XblastBall' , 'D' ) 1253call system ( GetVimCommand () .. ' --clean -S XblastBall' ) 1254sleep 100m 1255call assert_match ( 'OK' , readfile ( 'XerrorsBlast' ) -> join ()) 1256 1257call delete ( 'XerrorsBlast' ) 1258endfunc 1259 1260" SEGV occurs in older versions. 1261func Test_autocmd_bufwipe_in_SessLoadPost2 () 1262tabnew 1263set noswapfile 1264mksession ! 1265 1266let content =<< trim [CODE] 1267set nocp noswapfile 1268function! DeleteInactiveBufs() 1269tabfirst 1270let tabblist = [] 1271for i in range(1, tabpagenr(''$'')) 1272call extend(tabblist, tabpagebuflist(i)) 1273endfor 1274for b in range(1, bufnr(''$'')) 1275if bufexists(b) && buflisted(b) && (index(tabblist, b) == -1 || bufname(b) =~# ''^$'') 1276exec ''bwipeout '' . b 1277endif 1278endfor 1279echomsg "SessionLoadPost DONE" 1280endfunction 1281au SessionLoadPost * call DeleteInactiveBufs() 1282 1283func WriteErrors() 1284call writefile([execute("messages")], "XerrorsPost") 1285endfunc 1286au VimLeave * call WriteErrors() 1287[CODE] 1288 1289call writefile ( content , 'Xvimrc' , 'D' ) 1290call system ( GetVimCommand ( 'Xvimrc' ) .. ' --headless --noplugins -S Session.vim -c cq' ) 1291sleep 100m 1292let errors = join ( readfile ( 'XerrorsPost' )) 1293" This probably only ever matches on unix. 1294call assert_notmatch ( 'Caught deadly signal SEGV' , errors ) 1295call assert_match ( 'SessionLoadPost DONE' , errors ) 1296 1297set swapfile 1298for file in [ 'Session.vim' , 'XerrorsPost' ] 1299call delete ( file ) 1300endfor 1301endfunc 1302 1303func Test_empty_doau () 1304doau \| 1305endfunc 1306 1307func s: AutoCommandOptionSet ( match ) 1308let template = "Option: <%s>, OldVal: <%s>, OldValLocal: <%s>, OldValGlobal: <%s>, NewVal: <%s>, Scope: <%s>, Command: <%s>\n" 1309let item = remove ( g: options , 0 ) 1310let expected = printf ( template , item [ 0 ], item [ 1 ], item [ 2 ], item [ 3 ], item [ 4 ], item [ 5 ], item [ 6 ]) 1311let actual = printf ( template , a: match , v: option_old , v: option_oldlocal , v: option_oldglobal , v: option_new , v: option_type , v: option_command ) 1312let g: opt = [ expected , actual ] 1313"call assert_equal(expected, actual) 1314endfunc 1315 1316func Test_OptionSet () 1317" Use test/functional/legacy/autocmd_option_spec.lua 1318throw 'Skipped: Nvim changed types of OptionSet v: variables' 1319CheckOption autochdir 1320 1321call test_override ( 'starting' , 1 ) 1322set nocp 1323au OptionSet * : call s: AutoCommandOptionSet ( expand ( "<amatch>" )) 1324 1325" 1: Setting number option" 1326let g: options = [[ 'number' , 0 , 0 , 0 , 1 , 'global' , 'set' ]] 1327set nu 1328call assert_equal ([], g: options ) 1329call assert_equal ( g: opt [ 0 ], g: opt [ 1 ]) 1330 1331" 2: Setting local number option" 1332let g: options = [[ 'number' , 1 , 1 , '' , 0 , 'local' , 'setlocal' ]] 1333setlocal nonu 1334call assert_equal ([], g: options ) 1335call assert_equal ( g: opt [ 0 ], g: opt [ 1 ]) 1336 1337" 3: Setting global number option" 1338let g: options = [[ 'number' , 1 , '' , 1 , 0 , 'global' , 'setglobal' ]] 1339setglobal nonu 1340call assert_equal ([], g: options ) 1341call assert_equal ( g: opt [ 0 ], g: opt [ 1 ]) 1342 1343" 4: Setting local autoindent option" 1344let g: options = [[ 'autoindent' , 0 , 0 , '' , 1 , 'local' , 'setlocal' ]] 1345setlocal ai 1346call assert_equal ([], g: options ) 1347call assert_equal ( g: opt [ 0 ], g: opt [ 1 ]) 1348 1349" 5: Setting global autoindent option" 1350let g: options = [[ 'autoindent' , 0 , '' , 0 , 1 , 'global' , 'setglobal' ]] 1351setglobal ai 1352call assert_equal ([], g: options ) 1353call assert_equal ( g: opt [ 0 ], g: opt [ 1 ]) 1354 1355" 6: Setting global autoindent option" 1356let g: options = [[ 'autoindent' , 1 , 1 , 1 , 0 , 'global' , 'set' ]] 1357set ai ! 1358call assert_equal ([], g: options ) 1359call assert_equal ( g: opt [ 0 ], g: opt [ 1 ]) 1360 1361" 6a: Setting global autoindent option" 1362let g: options = [[ 'autoindent' , 1 , 1 , 0 , 0 , 'global' , 'set' ]] 1363noa setlocal ai 1364noa setglobal noai 1365set ai ! 1366call assert_equal ([], g: options ) 1367call assert_equal ( g: opt [ 0 ], g: opt [ 1 ]) 1368 1369" Should not print anything, use :noa 1370" 7: don't trigger OptionSet" 1371let g: options = [[ 'invalid' , 'invalid' , 'invalid' , 'invalid' , 'invalid' , 'invalid' , 'invalid' ]] 1372noa set nonu 1373call assert_equal ([[ 'invalid' , 'invalid' , 'invalid' , 'invalid' , 'invalid' , 'invalid' , 'invalid' ]], g: options ) 1374call assert_equal ( g: opt [ 0 ], g: opt [ 1 ]) 1375 1376" 8: Setting several global list and number option" 1377let g: options = [[ 'list' , 0 , 0 , 0 , 1 , 'global' , 'set' ], [ 'number' , 0 , 0 , 0 , 1 , 'global' , 'set' ]] 1378set list nu 1379call assert_equal ([], g: options ) 1380call assert_equal ( g: opt [ 0 ], g: opt [ 1 ]) 1381 1382" 9: don't trigger OptionSet" 1383let g: options = [[ 'invalid' , 'invalid' , 'invalid' , 'invalid' , 'invalid' , 'invalid' , 'invalid' ], [ 'invalid' , 'invalid' , 'invalid' , 'invalid' , 'invalid' , 'invalid' , 'invalid' ]] 1384noa set nolist nonu 1385call assert_equal ([[ 'invalid' , 'invalid' , 'invalid' , 'invalid' , 'invalid' , 'invalid' , 'invalid' ], [ 'invalid' , 'invalid' , 'invalid' , 'invalid' , 'invalid' , 'invalid' , 'invalid' ]], g: options ) 1386call assert_equal ( g: opt [ 0 ], g: opt [ 1 ]) 1387 1388" 10: Setting global acd" 1389let g: options = [[ 'autochdir' , 0 , 0 , '' , 1 , 'local' , 'setlocal' ]] 1390setlocal acd 1391call assert_equal ([], g: options ) 1392call assert_equal ( g: opt [ 0 ], g: opt [ 1 ]) 1393 1394" 11: Setting global autoread (also sets local value)" 1395let g: options = [[ 'autoread' , 0 , 0 , 0 , 1 , 'global' , 'set' ]] 1396set ar 1397call assert_equal ([], g: options ) 1398call assert_equal ( g: opt [ 0 ], g: opt [ 1 ]) 1399 1400" 12: Setting local autoread" 1401let g: options = [[ 'autoread' , 1 , 1 , '' , 1 , 'local' , 'setlocal' ]] 1402setlocal ar 1403call assert_equal ([], g: options ) 1404call assert_equal ( g: opt [ 0 ], g: opt [ 1 ]) 1405 1406" 13: Setting global autoread" 1407let g: options = [[ 'autoread' , 1 , '' , 1 , 0 , 'global' , 'setglobal' ]] 1408setglobal invar 1409call assert_equal ([], g: options ) 1410call assert_equal ( g: opt [ 0 ], g: opt [ 1 ]) 1411 1412" 14: Setting option backspace through :let" 1413let g: options = [[ 'backspace' , 'indent,eol,start' , 'indent,eol,start' , 'indent,eol,start' , '' , 'global' , 'set' ]] 1414let & bs = '' 1415call assert_equal ([], g: options ) 1416call assert_equal ( g: opt [ 0 ], g: opt [ 1 ]) 1417 1418" 15: Setting option backspace through setbufvar()" 1419let g: options = [[ 'backup' , 0 , 0 , '' , 1 , 'local' , 'setlocal' ]] 1420" try twice, first time, shouldn't trigger because option name is invalid, 1421" second time, it should trigger 1422let bnum = bufnr ( '%' ) 1423call assert_fails ( "call setbufvar(bnum, '&l:bk', 1)" , 'E355:' ) 1424" should trigger, use correct option name 1425call setbufvar ( bnum , '&backup' , 1 ) 1426call assert_equal ([], g: options ) 1427call assert_equal ( g: opt [ 0 ], g: opt [ 1 ]) 1428 1429" 16: Setting number option using setwinvar" 1430let g: options = [[ 'number' , 0 , 0 , '' , 1 , 'local' , 'setlocal' ]] 1431call setwinvar ( 0 , '&number' , 1 ) 1432call assert_equal ([], g: options ) 1433call assert_equal ( g: opt [ 0 ], g: opt [ 1 ]) 1434 1435" 17: Setting key option, shouldn't trigger" 1436let g: options = [[ 'key' , 'invalid' , 'invalid1' , 'invalid2' , 'invalid3' , 'invalid4' , 'invalid5' ]] 1437setlocal key = blah 1438setlocal key = 1439call assert_equal ([[ 'key' , 'invalid' , 'invalid1' , 'invalid2' , 'invalid3' , 'invalid4' , 'invalid5' ]], g: options ) 1440call assert_equal ( g: opt [ 0 ], g: opt [ 1 ]) 1441 1442 1443" 18a: Setting string global option" 1444let oldval = & backupext 1445let g: options = [[ 'backupext' , oldval , oldval , oldval , 'foo' , 'global' , 'set' ]] 1446set backupext = foo 1447call assert_equal ([], g: options ) 1448call assert_equal ( g: opt [ 0 ], g: opt [ 1 ]) 1449 1450" 18b: Resetting string global option" 1451let g: options = [[ 'backupext' , 'foo' , 'foo' , 'foo' , oldval , 'global' , 'set' ]] 1452set backupext & 1453call assert_equal ([], g: options ) 1454call assert_equal ( g: opt [ 0 ], g: opt [ 1 ]) 1455 1456" 18c: Setting global string global option" 1457let g: options = [[ 'backupext' , oldval , '' , oldval , 'bar' , 'global' , 'setglobal' ]] 1458setglobal backupext=bar 1459call assert_equal ([], g: options ) 1460call assert_equal ( g: opt [ 0 ], g: opt [ 1 ]) 1461 1462" 18d: Setting local string global option" 1463" As this is a global option this sets the global value even though 1464" :setlocal is used! 1465noa set backupext& " Reset global and local value (without triggering autocmd) 1466let g: options = [[ 'backupext' , oldval , oldval , '' , 'baz' , 'local' , 'setlocal' ]] 1467setlocal backupext = baz 1468call assert_equal ([], g: options ) 1469call assert_equal ( g: opt [ 0 ], g: opt [ 1 ]) 1470 1471" 18e: Setting again string global option" 1472noa setglobal backupext=ext_global " Reset global and local value (without triggering autocmd) 1473noa setlocal backupext=ext_local " Sets the global(!) value! 1474let g: options = [[ 'backupext' , 'ext_local' , 'ext_local' , 'ext_local' , 'fuu' , 'global' , 'set' ]] 1475set backupext = fuu 1476call assert_equal ([], g: options ) 1477call assert_equal ( g: opt [ 0 ], g: opt [ 1 ]) 1478 1479 1480" 19a: Setting string global-local (to buffer) option" 1481let oldval = & tags 1482let g: options = [[ 'tags' , oldval , oldval , oldval , 'tagpath' , 'global' , 'set' ]] 1483set tags = tagpath 1484call assert_equal ([], g: options ) 1485call assert_equal ( g: opt [ 0 ], g: opt [ 1 ]) 1486 1487" 19b: Resetting string global-local (to buffer) option" 1488let g: options = [[ 'tags' , 'tagpath' , 'tagpath' , 'tagpath' , oldval , 'global' , 'set' ]] 1489set tags & 1490call assert_equal ([], g: options ) 1491call assert_equal ( g: opt [ 0 ], g: opt [ 1 ]) 1492 1493" 19c: Setting global string global-local (to buffer) option " 1494let g: options = [[ 'tags' , oldval , '' , oldval , 'tagpath1' , 'global' , 'setglobal' ]] 1495setglobal tags=tagpath1 1496call assert_equal ([], g: options ) 1497call assert_equal ( g: opt [ 0 ], g: opt [ 1 ]) 1498 1499" 19d: Setting local string global-local (to buffer) option" 1500let g: options = [[ 'tags' , 'tagpath1' , 'tagpath1' , '' , 'tagpath2' , 'local' , 'setlocal' ]] 1501setlocal tags = tagpath2 1502call assert_equal ([], g: options ) 1503call assert_equal ( g: opt [ 0 ], g: opt [ 1 ]) 1504 1505" 19e: Setting again string global-local (to buffer) option" 1506" Note: v:option_old is the old global value for global-local string options 1507" but the old local value for all other kinds of options. 1508noa setglobal tags=tag_global " Reset global and local value (without triggering autocmd) 1509noa setlocal tags=tag_local 1510let g: options = [[ 'tags' , 'tag_global' , 'tag_local' , 'tag_global' , 'tagpath' , 'global' , 'set' ]] 1511set tags = tagpath 1512call assert_equal ([], g: options ) 1513call assert_equal ( g: opt [ 0 ], g: opt [ 1 ]) 1514 1515" 19f: Setting string global-local (to buffer) option to an empty string" 1516" Note: v:option_old is the old global value for global-local string options 1517" but the old local value for all other kinds of options. 1518noa set tags=tag_global " Reset global and local value (without triggering autocmd) 1519noa setlocal tags= " empty string 1520let g: options = [[ 'tags' , 'tag_global' , '' , 'tag_global' , 'tagpath' , 'global' , 'set' ]] 1521set tags = tagpath 1522call assert_equal ([], g: options ) 1523call assert_equal ( g: opt [ 0 ], g: opt [ 1 ]) 1524 1525 1526" 20a: Setting string local (to buffer) option" 1527let oldval = & spelllang 1528let g: options = [[ 'spelllang' , oldval , oldval , oldval , 'elvish,klingon' , 'global' , 'set' ]] 1529set spelllang = elvish,klingon 1530call assert_equal ([], g: options ) 1531call assert_equal ( g: opt [ 0 ], g: opt [ 1 ]) 1532 1533" 20b: Resetting string local (to buffer) option" 1534let g: options = [[ 'spelllang' , 'elvish,klingon' , 'elvish,klingon' , 'elvish,klingon' , oldval , 'global' , 'set' ]] 1535set spelllang & 1536call assert_equal ([], g: options ) 1537call assert_equal ( g: opt [ 0 ], g: opt [ 1 ]) 1538 1539" 20c: Setting global string local (to buffer) option" 1540let g: options = [[ 'spelllang' , oldval , '' , oldval , 'elvish' , 'global' , 'setglobal' ]] 1541setglobal spelllang=elvish 1542call assert_equal ([], g: options ) 1543call assert_equal ( g: opt [ 0 ], g: opt [ 1 ]) 1544 1545" 20d: Setting local string local (to buffer) option" 1546noa set spelllang& " Reset global and local value (without triggering autocmd) 1547let g: options = [[ 'spelllang' , oldval , oldval , '' , 'klingon' , 'local' , 'setlocal' ]] 1548setlocal spelllang = klingon 1549call assert_equal ([], g: options ) 1550call assert_equal ( g: opt [ 0 ], g: opt [ 1 ]) 1551 1552" 20e: Setting again string local (to buffer) option" 1553" Note: v:option_old is the old global value for global-local string options 1554" but the old local value for all other kinds of options. 1555noa setglobal spelllang=spellglobal " Reset global and local value (without triggering autocmd) 1556noa setlocal spelllang=spelllocal 1557let g: options = [[ 'spelllang' , 'spelllocal' , 'spelllocal' , 'spellglobal' , 'foo' , 'global' , 'set' ]] 1558set spelllang = foo 1559call assert_equal ([], g: options ) 1560call assert_equal ( g: opt [ 0 ], g: opt [ 1 ]) 1561 1562 1563" 21a: Setting string global-local (to window) option" 1564let oldval = & statusline 1565let g: options = [[ 'statusline' , oldval , oldval , oldval , 'foo' , 'global' , 'set' ]] 1566set statusline = foo 1567call assert_equal ([], g: options ) 1568call assert_equal ( g: opt [ 0 ], g: opt [ 1 ]) 1569 1570" 21b: Resetting string global-local (to window) option" 1571" Note: v:option_old is the old global value for global-local string options 1572" but the old local value for all other kinds of options. 1573let g: options = [[ 'statusline' , 'foo' , 'foo' , 'foo' , oldval , 'global' , 'set' ]] 1574set statusline & 1575call assert_equal ([], g: options ) 1576call assert_equal ( g: opt [ 0 ], g: opt [ 1 ]) 1577 1578" 21c: Setting global string global-local (to window) option" 1579let g: options = [[ 'statusline' , oldval , '' , oldval , 'bar' , 'global' , 'setglobal' ]] 1580setglobal statusline=bar 1581call assert_equal ([], g: options ) 1582call assert_equal ( g: opt [ 0 ], g: opt [ 1 ]) 1583 1584" 21d: Setting local string global-local (to window) option" 1585noa set statusline& " Reset global and local value (without triggering autocmd) 1586let g: options = [[ 'statusline' , oldval , oldval , '' , 'baz' , 'local' , 'setlocal' ]] 1587setlocal statusline = baz 1588call assert_equal ([], g: options ) 1589call assert_equal ( g: opt [ 0 ], g: opt [ 1 ]) 1590 1591" 21e: Setting again string global-local (to window) option" 1592" Note: v:option_old is the old global value for global-local string options 1593" but the old local value for all other kinds of options. 1594noa setglobal statusline=bar " Reset global and local value (without triggering autocmd) 1595noa setlocal statusline=baz 1596let g: options = [[ 'statusline' , 'bar' , 'baz' , 'bar' , 'foo' , 'global' , 'set' ]] 1597set statusline = foo 1598call assert_equal ([], g: options ) 1599call assert_equal ( g: opt [ 0 ], g: opt [ 1 ]) 1600 1601 1602" 22a: Setting string local (to window) option" 1603let oldval = & foldignore 1604let g: options = [[ 'foldignore' , oldval , oldval , oldval , 'fo' , 'global' , 'set' ]] 1605set foldignore = fo 1606call assert_equal ([], g: options ) 1607call assert_equal ( g: opt [ 0 ], g: opt [ 1 ]) 1608 1609" 22b: Resetting string local (to window) option" 1610let g: options = [[ 'foldignore' , 'fo' , 'fo' , 'fo' , oldval , 'global' , 'set' ]] 1611set foldignore & 1612call assert_equal ([], g: options ) 1613call assert_equal ( g: opt [ 0 ], g: opt [ 1 ]) 1614 1615" 22c: Setting global string local (to window) option" 1616let g: options = [[ 'foldignore' , oldval , '' , oldval , 'bar' , 'global' , 'setglobal' ]] 1617setglobal foldignore=bar 1618call assert_equal ([], g: options ) 1619call assert_equal ( g: opt [ 0 ], g: opt [ 1 ]) 1620 1621" 22d: Setting local string local (to window) option" 1622noa set foldignore& " Reset global and local value (without triggering autocmd) 1623let g: options = [[ 'foldignore' , oldval , oldval , '' , 'baz' , 'local' , 'setlocal' ]] 1624setlocal foldignore = baz 1625call assert_equal ([], g: options ) 1626call assert_equal ( g: opt [ 0 ], g: opt [ 1 ]) 1627 1628" 22e: Setting again string local (to window) option" 1629noa setglobal foldignore=glob " Reset global and local value (without triggering autocmd) 1630noa setlocal foldignore=loc 1631let g: options = [[ 'foldignore' , 'loc' , 'loc' , 'glob' , 'fo' , 'global' , 'set' ]] 1632set foldignore = fo 1633call assert_equal ([], g: options ) 1634call assert_equal ( g: opt [ 0 ], g: opt [ 1 ]) 1635 1636 1637" 23a: Setting global number global option" 1638noa setglobal cmdheight=8 " Reset global and local value (without triggering autocmd) 1639noa setlocal cmdheight=1 " Sets the global(!) value! 1640let g: options = [[ 'cmdheight' , '1' , '' , '1' , '2' , 'global' , 'setglobal' ]] 1641setglobal cmdheight=2 1642call assert_equal ([], g: options ) 1643call assert_equal ( g: opt [ 0 ], g: opt [ 1 ]) 1644 1645" 23b: Setting local number global option" 1646noa setglobal cmdheight=8 " Reset global and local value (without triggering autocmd) 1647noa setlocal cmdheight=1 " Sets the global(!) value! 1648let g: options = [[ 'cmdheight' , '1' , '1' , '' , '2' , 'local' , 'setlocal' ]] 1649setlocal cmdheight = 2 1650call assert_equal ([], g: options ) 1651call assert_equal ( g: opt [ 0 ], g: opt [ 1 ]) 1652 1653" 23c: Setting again number global option" 1654noa setglobal cmdheight=8 " Reset global and local value (without triggering autocmd) 1655noa setlocal cmdheight=1 " Sets the global(!) value! 1656let g: options = [[ 'cmdheight' , '1' , '1' , '1' , '2' , 'global' , 'set' ]] 1657set cmdheight = 2 1658call assert_equal ([], g: options ) 1659call assert_equal ( g: opt [ 0 ], g: opt [ 1 ]) 1660 1661" 23d: Setting again number global option" 1662noa set cmdheight=8 " Reset global and local value (without triggering autocmd) 1663let g: options = [[ 'cmdheight' , '8' , '8' , '8' , '2' , 'global' , 'set' ]] 1664set cmdheight = 2 1665call assert_equal ([], g: options ) 1666call assert_equal ( g: opt [ 0 ], g: opt [ 1 ]) 1667 1668 1669" 24a: Setting global number global-local (to buffer) option" 1670noa setglobal undolevels=8 " Reset global and local value (without triggering autocmd) 1671noa setlocal undolevels=1 1672let g: options = [[ 'undolevels' , '8' , '' , '8' , '2' , 'global' , 'setglobal' ]] 1673setglobal undolevels=2 1674call assert_equal ([], g: options ) 1675call assert_equal ( g: opt [ 0 ], g: opt [ 1 ]) 1676 1677" 24b: Setting local number global-local (to buffer) option" 1678noa setglobal undolevels=8 " Reset global and local value (without triggering autocmd) 1679noa setlocal undolevels=1 1680let g: options = [[ 'undolevels' , '1' , '1' , '' , '2' , 'local' , 'setlocal' ]] 1681setlocal undolevels = 2 1682call assert_equal ([], g: options ) 1683call assert_equal ( g: opt [ 0 ], g: opt [ 1 ]) 1684 1685" 24c: Setting again number global-local (to buffer) option" 1686noa setglobal undolevels=8 " Reset global and local value (without triggering autocmd) 1687noa setlocal undolevels=1 1688let g: options = [[ 'undolevels' , '1' , '1' , '8' , '2' , 'global' , 'set' ]] 1689set undolevels = 2 1690call assert_equal ([], g: options ) 1691call assert_equal ( g: opt [ 0 ], g: opt [ 1 ]) 1692 1693" 24d: Setting again global number global-local (to buffer) option" 1694noa set undolevels=8 " Reset global and local value (without triggering autocmd) 1695let g: options = [[ 'undolevels' , '8' , '8' , '8' , '2' , 'global' , 'set' ]] 1696set undolevels = 2 1697call assert_equal ([], g: options ) 1698call assert_equal ( g: opt [ 0 ], g: opt [ 1 ]) 1699 1700 1701" 25a: Setting global number local (to buffer) option" 1702noa setglobal wrapmargin=8 " Reset global and local value (without triggering autocmd) 1703noa setlocal wrapmargin=1 1704let g: options = [[ 'wrapmargin' , '8' , '' , '8' , '2' , 'global' , 'setglobal' ]] 1705setglobal wrapmargin=2 1706call assert_equal ([], g: options ) 1707call assert_equal ( g: opt [ 0 ], g: opt [ 1 ]) 1708 1709" 25b: Setting local number local (to buffer) option" 1710noa setglobal wrapmargin=8 " Reset global and local value (without triggering autocmd) 1711noa setlocal wrapmargin=1 1712let g: options = [[ 'wrapmargin' , '1' , '1' , '' , '2' , 'local' , 'setlocal' ]] 1713setlocal wrapmargin = 2 1714call assert_equal ([], g: options ) 1715call assert_equal ( g: opt [ 0 ], g: opt [ 1 ]) 1716 1717" 25c: Setting again number local (to buffer) option" 1718noa setglobal wrapmargin=8 " Reset global and local value (without triggering autocmd) 1719noa setlocal wrapmargin=1 1720let g: options = [[ 'wrapmargin' , '1' , '1' , '8' , '2' , 'global' , 'set' ]] 1721set wrapmargin = 2 1722call assert_equal ([], g: options ) 1723call assert_equal ( g: opt [ 0 ], g: opt [ 1 ]) 1724 1725" 25d: Setting again global number local (to buffer) option" 1726noa set wrapmargin=8 " Reset global and local value (without triggering autocmd) 1727let g: options = [[ 'wrapmargin' , '8' , '8' , '8' , '2' , 'global' , 'set' ]] 1728set wrapmargin = 2 1729call assert_equal ([], g: options ) 1730call assert_equal ( g: opt [ 0 ], g: opt [ 1 ]) 1731 1732 1733" 26: Setting number global-local (to window) option. 1734" Such option does currently not exist. 1735 1736 1737" 27a: Setting global number local (to window) option" 1738noa setglobal foldcolumn=8 " Reset global and local value (without triggering autocmd) 1739noa setlocal foldcolumn=1 1740let g: options = [[ 'foldcolumn' , '8' , '' , '8' , '2' , 'global' , 'setglobal' ]] 1741setglobal foldcolumn=2 1742call assert_equal ([], g: options ) 1743call assert_equal ( g: opt [ 0 ], g: opt [ 1 ]) 1744 1745" 27b: Setting local number local (to window) option" 1746noa setglobal foldcolumn=8 " Reset global and local value (without triggering autocmd) 1747noa setlocal foldcolumn=1 1748let g: options = [[ 'foldcolumn' , '1' , '1' , '' , '2' , 'local' , 'setlocal' ]] 1749setlocal foldcolumn = 2 1750call assert_equal ([], g: options ) 1751call assert_equal ( g: opt [ 0 ], g: opt [ 1 ]) 1752 1753" 27c: Setting again number local (to window) option" 1754noa setglobal foldcolumn=8 " Reset global and local value (without triggering autocmd) 1755noa setlocal foldcolumn=1 1756let g: options = [[ 'foldcolumn' , '1' , '1' , '8' , '2' , 'global' , 'set' ]] 1757set foldcolumn = 2 1758call assert_equal ([], g: options ) 1759call assert_equal ( g: opt [ 0 ], g: opt [ 1 ]) 1760 1761" 27d: Setting again global number local (to window) option" 1762noa set foldcolumn=8 " Reset global and local value (without triggering autocmd) 1763let g: options = [[ 'foldcolumn' , '8' , '8' , '8' , '2' , 'global' , 'set' ]] 1764set foldcolumn = 2 1765call assert_equal ([], g: options ) 1766call assert_equal ( g: opt [ 0 ], g: opt [ 1 ]) 1767 1768 1769" 28a: Setting global boolean global option" 1770noa setglobal nowrapscan " Reset global and local value (without triggering autocmd) 1771noa setlocal wrapscan " Sets the global(!) value! 1772let g: options = [[ 'wrapscan' , '1' , '' , '1' , '0' , 'global' , 'setglobal' ]] 1773setglobal nowrapscan 1774call assert_equal ([], g: options ) 1775call assert_equal ( g: opt [ 0 ], g: opt [ 1 ]) 1776 1777" 28b: Setting local boolean global option" 1778noa setglobal nowrapscan " Reset global and local value (without triggering autocmd) 1779noa setlocal wrapscan " Sets the global(!) value! 1780let g: options = [[ 'wrapscan' , '1' , '1' , '' , '0' , 'local' , 'setlocal' ]] 1781setlocal nowrapscan 1782call assert_equal ([], g: options ) 1783call assert_equal ( g: opt [ 0 ], g: opt [ 1 ]) 1784 1785" 28c: Setting again boolean global option" 1786noa setglobal nowrapscan " Reset global and local value (without triggering autocmd) 1787noa setlocal wrapscan " Sets the global(!) value! 1788let g: options = [[ 'wrapscan' , '1' , '1' , '1' , '0' , 'global' , 'set' ]] 1789set nowrapscan 1790call assert_equal ([], g: options ) 1791call assert_equal ( g: opt [ 0 ], g: opt [ 1 ]) 1792 1793" 28d: Setting again global boolean global option" 1794noa set nowrapscan " Reset global and local value (without triggering autocmd) 1795let g: options = [[ 'wrapscan' , '0' , '0' , '0' , '1' , 'global' , 'set' ]] 1796set wrapscan 1797call assert_equal ([], g: options ) 1798call assert_equal ( g: opt [ 0 ], g: opt [ 1 ]) 1799 1800 1801" 29a: Setting global boolean global-local (to buffer) option" 1802noa setglobal noautoread " Reset global and local value (without triggering autocmd) 1803noa setlocal autoread 1804let g: options = [[ 'autoread' , '0' , '' , '0' , '1' , 'global' , 'setglobal' ]] 1805setglobal autoread 1806call assert_equal ([], g: options ) 1807call assert_equal ( g: opt [ 0 ], g: opt [ 1 ]) 1808 1809" 29b: Setting local boolean global-local (to buffer) option" 1810noa setglobal noautoread " Reset global and local value (without triggering autocmd) 1811noa setlocal autoread 1812let g: options = [[ 'autoread' , '1' , '1' , '' , '0' , 'local' , 'setlocal' ]] 1813setlocal noautoread 1814call assert_equal ([], g: options ) 1815call assert_equal ( g: opt [ 0 ], g: opt [ 1 ]) 1816 1817" 29c: Setting again boolean global-local (to buffer) option" 1818noa setglobal noautoread " Reset global and local value (without triggering autocmd) 1819noa setlocal autoread 1820let g: options = [[ 'autoread' , '1' , '1' , '0' , '1' , 'global' , 'set' ]] 1821set autoread 1822call assert_equal ([], g: options ) 1823call assert_equal ( g: opt [ 0 ], g: opt [ 1 ]) 1824 1825" 29d: Setting again global boolean global-local (to buffer) option" 1826noa set noautoread " Reset global and local value (without triggering autocmd) 1827let g: options = [[ 'autoread' , '0' , '0' , '0' , '1' , 'global' , 'set' ]] 1828set autoread 1829call assert_equal ([], g: options ) 1830call assert_equal ( g: opt [ 0 ], g: opt [ 1 ]) 1831 1832 1833" 30a: Setting global boolean local (to buffer) option" 1834noa setglobal nocindent " Reset global and local value (without triggering autocmd) 1835noa setlocal cindent 1836let g: options = [[ 'cindent' , '0' , '' , '0' , '1' , 'global' , 'setglobal' ]] 1837setglobal cindent 1838call assert_equal ([], g: options ) 1839call assert_equal ( g: opt [ 0 ], g: opt [ 1 ]) 1840 1841" 30b: Setting local boolean local (to buffer) option" 1842noa setglobal nocindent " Reset global and local value (without triggering autocmd) 1843noa setlocal cindent 1844let g: options = [[ 'cindent' , '1' , '1' , '' , '0' , 'local' , 'setlocal' ]] 1845setlocal nocindent 1846call assert_equal ([], g: options ) 1847call assert_equal ( g: opt [ 0 ], g: opt [ 1 ]) 1848 1849" 30c: Setting again boolean local (to buffer) option" 1850noa setglobal nocindent " Reset global and local value (without triggering autocmd) 1851noa setlocal cindent 1852let g: options = [[ 'cindent' , '1' , '1' , '0' , '1' , 'global' , 'set' ]] 1853set cindent 1854call assert_equal ([], g: options ) 1855call assert_equal ( g: opt [ 0 ], g: opt [ 1 ]) 1856 1857" 30d: Setting again global boolean local (to buffer) option" 1858noa set nocindent " Reset global and local value (without triggering autocmd) 1859let g: options = [[ 'cindent' , '0' , '0' , '0' , '1' , 'global' , 'set' ]] 1860set cindent 1861call assert_equal ([], g: options ) 1862call assert_equal ( g: opt [ 0 ], g: opt [ 1 ]) 1863 1864 1865" 31: Setting boolean global-local (to window) option 1866" Currently no such option exists. 1867 1868 1869" 32a: Setting global boolean local (to window) option" 1870noa setglobal nocursorcolumn " Reset global and local value (without triggering autocmd) 1871noa setlocal cursorcolumn 1872let g: options = [[ 'cursorcolumn' , '0' , '' , '0' , '1' , 'global' , 'setglobal' ]] 1873setglobal cursorcolumn 1874call assert_equal ([], g: options ) 1875call assert_equal ( g: opt [ 0 ], g: opt [ 1 ]) 1876 1877" 32b: Setting local boolean local (to window) option" 1878noa setglobal nocursorcolumn " Reset global and local value (without triggering autocmd) 1879noa setlocal cursorcolumn 1880let g: options = [[ 'cursorcolumn' , '1' , '1' , '' , '0' , 'local' , 'setlocal' ]] 1881setlocal nocursorcolumn 1882call assert_equal ([], g: options ) 1883call assert_equal ( g: opt [ 0 ], g: opt [ 1 ]) 1884 1885" 32c: Setting again boolean local (to window) option" 1886noa setglobal nocursorcolumn " Reset global and local value (without triggering autocmd) 1887noa setlocal cursorcolumn 1888let g: options = [[ 'cursorcolumn' , '1' , '1' , '0' , '1' , 'global' , 'set' ]] 1889set cursorcolumn 1890call assert_equal ([], g: options ) 1891call assert_equal ( g: opt [ 0 ], g: opt [ 1 ]) 1892 1893" 32d: Setting again global boolean local (to window) option" 1894noa set nocursorcolumn " Reset global and local value (without triggering autocmd) 1895let g: options = [[ 'cursorcolumn' , '0' , '0' , '0' , '1' , 'global' , 'set' ]] 1896set cursorcolumn 1897call assert_equal ([], g: options ) 1898call assert_equal ( g: opt [ 0 ], g: opt [ 1 ]) 1899 1900 1901" 33: Test autocommands when an option value is converted internally. 1902noa set backspace=1 " Reset global and local value (without triggering autocmd) 1903let g: options = [[ 'backspace' , 'indent,eol' , 'indent,eol' , 'indent,eol' , '2' , 'global' , 'set' ]] 1904set backspace = 2 1905call assert_equal ([], g: options ) 1906call assert_equal ( g: opt [ 0 ], g: opt [ 1 ]) 1907 1908 1909" Cleanup 1910au ! OptionSet 1911" set tags& 1912for opt in [ 'nu' , 'ai' , 'acd' , 'ar' , 'bs' , 'backup' , 'cul' , 'cp' , 'backupext' , 'tags' , 'spelllang' , 'statusline' , 'foldignore' , 'cmdheight' , 'undolevels' , 'wrapmargin' , 'foldcolumn' , 'wrapscan' , 'autoread' , 'cindent' , 'cursorcolumn' ] 1913exe printf ( ":set %s&vim" , opt ) 1914endfor 1915call test_override ( 'starting' , 0 ) 1916delfunc ! AutoCommandOptionSet 1917endfunc 1918 1919func Test_OptionSet_diffmode () 1920call Ntest_override ( 'starting' , 1 ) 1921" 18: Changing an option when entering diff mode 1922new 1923au OptionSet diff : let & l: cul = v: option_new 1924 1925call setline ( 1 , [ 'buffer 1' , 'line2' , 'line3' , 'line4' ]) 1926call assert_equal ( 0 , & l: cul ) 1927diffthis 1928call assert_equal ( 1 , & l: cul ) 1929 1930vnew 1931call setline ( 1 , [ 'buffer 2' , 'line 2' , 'line 3' , 'line4' ]) 1932call assert_equal ( 0 , & l: cul ) 1933diffthis 1934call assert_equal ( 1 , & l: cul ) 1935 1936diffoff 1937call assert_equal ( 0 , & l: cul ) 1938call assert_equal ( 1 , getwinvar ( 2 , '&l:cul' )) 1939bw ! 1940 1941call assert_equal ( 1 , & l: cul ) 1942diffoff ! 1943call assert_equal ( 0 , & l: cul ) 1944call assert_equal ( 0 , getwinvar ( 1 , '&l:cul' )) 1945bw ! 1946 1947" Cleanup 1948au ! OptionSet 1949call Ntest_override ( 'starting' , 0 ) 1950endfunc 1951 1952func Test_OptionSet_diffmode_close () 1953call Ntest_override ( 'starting' , 1 ) 1954" 19: Try to close the current window when entering diff mode 1955" should not segfault 1956new 1957au OptionSet diff close 1958 1959call setline ( 1 , [ 'buffer 1' , 'line2' , 'line3' , 'line4' ]) 1960call assert_fails ( ':diffthis' , 'E788' ) 1961call assert_equal ( 1 , & diff ) 1962vnew 1963call setline ( 1 , [ 'buffer 2' , 'line 2' , 'line 3' , 'line4' ]) 1964call assert_fails ( ':diffthis' , 'E788' ) 1965call assert_equal ( 1 , & diff ) 1966set diffopt -= closeoff 1967bw ! 1968call assert_fails ( ':diffoff!' , 'E788' ) 1969bw ! 1970 1971" Cleanup 1972au ! OptionSet 1973call Ntest_override ( 'starting' , 0 ) 1974"delfunc! AutoCommandOptionSet 1975endfunc 1976 1977" Test for Bufleave autocommand that deletes the buffer we are about to edit. 1978func Test_BufleaveWithDelete () 1979new | edit XbufLeave1 1980 1981augroup test_bufleavewithdelete 1982autocmd ! 1983autocmd BufLeave XbufLeave1 bwipe XbufLeave2 1984augroup END 1985 1986call assert_fails ( 'edit XbufLeave2' , 'E143:' ) 1987call assert_equal ( 'XbufLeave1' , bufname ( '%' )) 1988 1989autocmd ! test_bufleavewithdelete BufLeave XbufLeave1 1990augroup ! test_bufleavewithdelete 1991 1992new 1993bwipe ! XbufLeave1 1994endfunc 1995 1996" Test for autocommand that changes the buffer list, when doing ":ball". 1997func Test_Acmd_BufAll () 1998enew ! 1999% bwipe ! 2000call writefile ([ 'Test file Xxx1' ], 'Xxx1' , 'D' ) 2001call writefile ([ 'Test file Xxx2' ], 'Xxx2' , 'D' ) 2002call writefile ([ 'Test file Xxx3' ], 'Xxx3' , 'D' ) 2003 2004" Add three files to the buffer list 2005split Xxx1 2006close 2007split Xxx2 2008close 2009split Xxx3 2010close 2011 2012" Wipe the buffer when the buffer is opened 2013au BufReadPost Xxx2 bwipe 2014 2015call append ( 0 , 'Test file Xxx4' ) 2016ball 2017 2018call assert_equal ( 2 , winnr ( '$' )) 2019call assert_equal ( 'Xxx1' , bufname ( winbufnr ( winnr ( '$' )))) 2020wincmd t 2021 2022au ! BufReadPost 2023% bwipe ! 2024enew ! | only 2025endfunc 2026 2027" Test for autocommand that changes current buffer on BufEnter event. 2028" Check if modelines are interpreted for the correct buffer. 2029func Test_Acmd_BufEnter () 2030% bwipe ! 2031call writefile ([ 'start of test file Xxx1' , 2032\ "\<Tab>this is a test" , 2033\ 'end of test file Xxx1' ], 'Xxx1' , 'D' ) 2034call writefile ([ 'start of test file Xxx2' , 2035\ 'vim: set noai :' , 2036\ "\<Tab>this is a test" , 2037\ 'end of test file Xxx2' ], 'Xxx2' , 'D' ) 2038 2039au BufEnter Xxx2 brew 2040set ai modeline modelines = 3 2041edit Xxx1 2042" edit Xxx2, autocmd will do :brew 2043edit Xxx2 2044exe "normal G?this is a\<CR>" 2045" Append text with autoindent to this file 2046normal othis should be auto-indented 2047call assert_equal ( "\<Tab>this should be auto-indented" , getline ( '.' )) 2048call assert_equal ( 3 , line ( '.' )) 2049" Remove autocmd and edit Xxx2 again 2050au ! BufEnter Xxx2 2051buf ! Xxx2 2052exe "normal G?this is a\<CR>" 2053" append text without autoindent to Xxx 2054normal othis should be in column 1 2055call assert_equal ( "this should be in column 1" , getline ( '.' )) 2056call assert_equal ( 4 , line ( '.' )) 2057 2058% bwipe ! 2059set ai & vim modeline & vim modelines & vim 2060endfunc 2061 2062" Test for issue #57 2063" do not move cursor on <c-o> when autoindent is set 2064func Test_ai_CTRL_O () 2065enew ! 2066set ai 2067let save_fo = & fo 2068set fo += r 2069exe "normal o# abcdef\<Esc>2hi\<CR>\<C-O>d0\<Esc>" 2070exe "normal o# abcdef\<Esc>2hi\<C-O>d0\<Esc>" 2071call assert_equal ([ '# abc' , 'def' , 'def' ], getline ( 2 , 4 )) 2072 2073set ai & vim 2074let & fo = save_fo 2075enew ! 2076endfunc 2077 2078" Test for autocommand that deletes the current buffer on BufLeave event. 2079" Also test deleting the last buffer, should give a new, empty buffer. 2080func Test_BufLeave_Wipe () 2081% bwipe ! 2082let content = [ 'start of test file Xxx' , 2083\ 'this is a test' , 2084\ 'end of test file Xxx' ] 2085call writefile ( content , 'Xxx1' , 'D' ) 2086call writefile ( content , 'Xxx2' , 'D' ) 2087 2088au BufLeave Xxx2 bwipe 2089edit Xxx1 2090split Xxx2 2091" delete buffer Xxx2, we should be back to Xxx1 2092bwipe 2093call assert_equal ( 'Xxx1' , bufname ( '%' )) 2094call assert_equal ( 1 , winnr ( '$' )) 2095 2096" Create an alternate buffer 2097% write ! test.out 2098call assert_equal ( 'test.out' , bufname ( '#' )) 2099" delete alternate buffer 2100bwipe test.out 2101call assert_equal ( 'Xxx1' , bufname ( '%' )) 2102call assert_equal ( '' , bufname ( '#' )) 2103 2104au BufLeave Xxx1 bwipe 2105" delete current buffer, get an empty one 2106bwipe ! 2107call assert_equal ( 1 , line ( '$' )) 2108call assert_equal ( '' , bufname ( '%' )) 2109let g: bufinfo = getbufinfo () 2110call assert_equal ( 1 , len ( g: bufinfo )) 2111 2112call delete ( 'test.out' ) 2113% bwipe 2114au ! BufLeave 2115 2116" check that bufinfo doesn't contain a pointer to freed memory 2117call test_garbagecollect_now () 2118endfunc 2119 2120func Test_QuitPre () 2121edit Xfoo 2122let winid = win_getid ( winnr ()) 2123split Xbar 2124au ! QuitPre * let g: afile = expand ( '<afile>' ) 2125" Close the other window, <afile> should be correct. 2126exe win_id2win ( winid ) . 'q' 2127call assert_equal ( 'Xfoo' , g: afile ) 21282129 unlet g: afile 2130bwipe Xfoo 2131bwipe Xbar 2132endfunc 2133 2134func Test_Cmdline_Trigger () 2135autocmd CmdlineLeave Pre : let g: log = "CmdlineLeavePre" 2136autocmd CmdlineLeave : let g: log2 = "CmdlineLeave" 2137new 2138let g: log = '' 2139let g: log2 = '' 2140nnoremap < F1 > <Cmd> echo "hello"<CR> 2141call feedkeys ( "\<F1>" , 'x' ) 2142call assert_equal ( '' , g: log ) 2143call assert_equal ( '' , g: log2 ) 2144nunmap <F1> 2145 2146let g: log = '' 2147let g: log2 = '' 2148nnoremap < F1 > :echo "hello" < CR > 2149call feedkeys ( "\<F1>" , 'x' ) 2150call assert_equal ( 'CmdlineLeavePre' , g: log ) 2151call assert_equal ( 'CmdlineLeave' , g: log2 ) 2152nunmap <F1> 2153 2154let g: log = '' 2155let g: log2 = '' 2156call feedkeys ( ":\<bs>" , "tx" ) 2157call assert_equal ( 'CmdlineLeavePre' , g: log ) 2158call assert_equal ( 'CmdlineLeave' , g: log2 ) 2159 2160let g: log = '' 2161let g: log2 = '' 2162split 2163call assert_equal ( '' , g: log ) 2164call feedkeys ( ":echo hello" , "tx" ) 2165call assert_equal ( 'CmdlineLeavePre' , g: log ) 2166call assert_equal ( 'CmdlineLeave' , g: log2 ) 2167 2168let g: log = '' 2169let g: log2 = '' 2170close 2171call assert_equal ( '' , g: log ) 2172call feedkeys ( ":echo hello" , "tx" ) 2173call assert_equal ( 'CmdlineLeavePre' , g: log ) 2174call assert_equal ( 'CmdlineLeave' , g: log2 ) 2175 2176let g: log = '' 2177let g: log2 = '' 2178tabnew 2179call assert_equal ( '' , g: log ) 2180call feedkeys ( ":echo hello" , "tx" ) 2181call assert_equal ( 'CmdlineLeavePre' , g: log ) 2182call assert_equal ( 'CmdlineLeave' , g: log2 ) 2183 2184let g: log = '' 2185let g: log2 = '' 2186split 2187call assert_equal ( '' , g: log ) 2188call feedkeys ( ":echo hello" , "tx" ) 2189call assert_equal ( 'CmdlineLeavePre' , g: log ) 2190call assert_equal ( 'CmdlineLeave' , g: log2 ) 2191 2192let g: log = '' 2193let g: log2 = '' 2194tabclose 2195call assert_equal ( '' , g: log ) 2196call feedkeys ( ":echo hello" , "tx" ) 2197call assert_equal ( 'CmdlineLeavePre' , g: log ) 2198call assert_equal ( 'CmdlineLeave' , g: log2 ) 2199 2200autocmd CmdlineLeave Pre * let g: cmdline += [getcmdline()] 2201 2202for end_keys in [ "\<CR>" , "\<NL>" , "\<kEnter>" , "\<C-C>" , "\<Esc>" , 2203\ "\<C-\>\<C-N>" , "\<C-\>\<C-G>" ] 2204let g: cmdline = [] 2205let g: log = '' 2206let g: log2 = '' 2207call assert_equal ( '' , g: log ) 2208let keys = $ ':echo "hello"{end_keys}' 2209let msg = keytrans ( keys ) 2210call feedkeys ( keys , "tx" ) 2211call assert_equal ([ 'echo "hello"' ], g: cmdline , msg ) 2212call assert_equal ( 'CmdlineLeavePre' , g: log , msg ) 2213call assert_equal ( 'CmdlineLeave' , g: log2 , msg ) 2214endfor 2215 2216let g: cmdline = [] 2217call feedkeys ( ":let c = input('? ')\<cr>ABCDE\<cr>" , "tx" ) 2218call assert_equal ([ "let c = input('? ')" , 'ABCDE' ], g: cmdline ) 2219 2220au ! CmdlineLeave Pre 2221unlet ! g: cmdline 2222unlet ! g: log 2223unlet ! g: log2 2224bw ! 2225endfunc 2226 2227" Ensure :cabbr does not cause a spurious CmdlineLeavePre. 2228func Test_CmdlineLeavePre_cabbr () 2229" For unknown reason this fails intermittently on MS-Windows 2230CheckNotMSWindows 2231CheckFeature terminal 2232let buf = term_start ([ GetVimProg (), '--clean' , '-c' , 'set noswapfile' ], { 'term_rows' : 3 }) 2233call assert_equal ( 'running' , term_getstatus ( buf )) 2234call term_sendkeys ( buf , ":let g:a=0\<cr>" ) 2235call term_wait ( buf , 50 ) 2236call term_sendkeys ( buf , ":cabbr v v\<cr>" ) 2237call term_wait ( buf , 50 ) 2238call term_sendkeys ( buf , ":command! -nargs=* Foo echo\<cr>" ) 2239call term_wait ( buf , 50 ) 2240call term_sendkeys ( buf , ":au! CmdlineLeavePre * :let g:a+=1\<cr>" ) 2241call term_wait ( buf , 50 ) 2242call term_sendkeys ( buf , ":Foo v\<cr>" ) 2243call term_wait ( buf , 50 ) 2244call term_sendkeys ( buf , ":echo g:a\<cr>" ) 2245call term_wait ( buf , 50 ) 2246call WaitForAssert ({ -> assert_match ( '^2.*$' , term_getline ( buf , 3 ))}) 2247bwipe ! 2248endfunc 2249 2250func Test_Cmdline () 2251au ! CmdlineChanged : let g: text = getcmdline () 2252let g: text = 0 2253call feedkeys ( ":echom 'hello'\<CR>" , 'xt' ) 2254call assert_equal ( "echom 'hello'" , g: text ) 2255au ! CmdlineChanged 2256 2257au ! CmdlineChanged : let g: entered = expand ( '<afile>' ) 2258let g: entered = 0 2259call feedkeys ( ":echom 'hello'\<CR>" , 'xt' ) 2260call assert_equal ( ':' , g: entered ) 2261au ! CmdlineChanged 2262 2263autocmd CmdlineChanged : let g: log += [ getcmdline ()] 2264 2265let g: log = [] 2266cnoremap < F1 > <Cmd> call setcmdline ( 'ls' ) <CR> 2267call feedkeys ( ":\<F1>" , 'xt' ) 2268call assert_equal ([ 'ls' ], g: log ) 2269cunmap <F1> 2270 2271let g: log = [] 2272call feedkeys ( ":sign \<Tab>\<Tab>\<C-N>\<C-P>\<S-Tab>\<S-Tab>\<Esc>" , 'xt' ) 2273call assert_equal ([ 2274\ 's' , 2275\ 'si' , 2276\ 'sig' , 2277\ 'sign' , 2278\ 'sign ' , 2279\ 'sign define' , 2280\ 'sign jump' , 2281\ 'sign list' , 2282\ 'sign jump' , 2283\ 'sign define' , 2284\ 'sign ' , 2285\ ], g: log ) 2286let g: log = [] 2287set wildmenu wildoptions += pum 2288call feedkeys ( ":sign \<S-Tab>\<PageUp>\<kPageUp>\<kPageDown>\<PageDown>\<Esc>" , 'xt' ) 2289call assert_equal ([ 2290\ 's' , 2291\ 'si' , 2292\ 'sig' , 2293\ 'sign' , 2294\ 'sign ' , 2295\ 'sign unplace' , 2296\ 'sign jump' , 2297\ 'sign define' , 2298\ 'sign undefine' , 2299\ 'sign unplace' , 2300\ ], g: log ) 2301set wildmenu & wildoptions & 2302 2303let g: log = [] 2304let @r = 'abc' 2305call feedkeys ( ":0\<C-R>=@r\<CR>1\<C-R>\<C-O>r2\<C-R>\<C-R>r3\<Esc>" , 'xt' ) 2306call assert_equal ([ 2307\ '0' , 2308\ '0a' , 2309\ '0ab' , 2310\ '0abc' , 2311\ '0abc1' , 2312\ '0abc1abc' , 2313\ '0abc1abc2' , 2314\ '0abc1abc2abc' , 2315\ '0abc1abc2abc3' , 2316\ ], g: log ) 2317 2318" <Del> should trigger CmdlineChanged 2319let g: log = [] 2320call feedkeys ( ":foo\<Left>\<Left>\<Del>\<Del>\<Esc>" , 'xt' ) 2321call assert_equal ([ 2322\ 'f' , 2323\ 'fo' , 2324\ 'foo' , 2325\ 'fo' , 2326\ 'f' , 2327\ ], g: log ) 2328 2329unlet g: log 2330au ! CmdlineChanged 2331 2332au ! CmdlineEnter : let g: entered = expand ( '<afile>' ) 2333au ! CmdlineLeave : let g: left = expand ( '<afile>' ) 2334au ! CmdlineLeave Pre : let g: leftpre = expand ( '<afile>' ) 2335let g: entered = 0 2336let g: left = 0 2337let g: leftpre = 0 2338call feedkeys ( ":echo 'hello'\<CR>" , 'xt' ) 2339call assert_equal ( ':' , g: entered ) 2340call assert_equal ( ':' , g: left ) 2341call assert_equal ( ':' , g: leftpre ) 2342au ! CmdlineEnter 2343au ! CmdlineLeave 2344au ! CmdlineLeave Pre 2345 2346let save_shellslash = & shellslash 2347" Nvim doesn't allow setting value of a hidden option to non-default value 2348if exists ( '+shellslash' ) 2349set noshellslash 2350endif 2351au ! CmdlineEnter / let g: entered = expand ( '<afile>' ) 2352au ! CmdlineLeave / let g: left = expand ( '<afile>' ) 2353au ! CmdlineLeave Pre / let g:leftpre = expand('<afile>') 2354let g: entered = 0 2355let g: left = 0 2356let g: leftpre = 0 2357new 2358call setline ( 1 , 'hello' ) 2359call feedkeys ( "/hello\<CR>" , 'xt' ) 2360call assert_equal ( '/' , g: entered ) 2361call assert_equal ( '/' , g: left ) 2362call assert_equal ( '/' , g: leftpre ) 2363bwipe ! 2364au ! CmdlineEnter 2365au ! CmdlineLeave 2366au ! CmdlineLeave Pre 2367let & shellslash = save_shellslash 2368 2369let g: left = "cancelled" 2370let g: leftpre = "cancelled" 2371au ! CmdlineLeave : let g: left = "triggered" 2372au ! CmdlineLeave Pre : let g: leftpre = "triggered" 2373call feedkeys ( ":echo 'hello'\<esc>" , 'xt' ) 2374call assert_equal ( 'triggered' , g: left ) 2375call assert_equal ( 'triggered' , g: leftpre ) 2376let g: left = "cancelled" 2377let g: leftpre = "cancelled" 2378au ! CmdlineLeave : let g: left = "triggered" 2379call feedkeys ( ":echo 'hello'\<c-c>" , 'xt' ) 2380call assert_equal ( 'triggered' , g: left ) 2381call assert_equal ( 'triggered' , g: leftpre ) 2382au ! CmdlineLeave 2383au ! CmdlineLeave Pre 2384 2385au ! CursorMoved C : let g: pos += [ getcmdpos ()] 2386let g: pos = [] 2387call feedkeys ( ":foo bar baz\<C-W>\<C-W>\<C-W>\<Esc>" , 'xt' ) 2388call assert_equal ([ 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 , 11 , 12 , 9 , 5 , 1 ], g: pos ) 2389let g: pos = [] 2390call feedkeys ( ":hello\<C-B>\<Esc>" , 'xt' ) 2391call assert_equal ([ 2 , 3 , 4 , 5 , 6 , 1 ], g: pos ) 2392let g: pos = [] 2393call feedkeys ( ":hello\<C-U>\<Esc>" , 'xt' ) 2394call assert_equal ([ 2 , 3 , 4 , 5 , 6 , 1 ], g: pos ) 2395let g: pos = [] 2396call feedkeys ( ":hello\<Left>\<C-R>=''\<CR>\<Left>\<Right>\<Esc>" , 'xt' ) 2397call assert_equal ([ 2 , 3 , 4 , 5 , 6 , 5 , 4 , 5 ], g: pos ) 2398let g: pos = [] 2399call feedkeys ( ":12345678\<C-R>=setcmdpos(3)??''\<CR>\<Esc>" , 'xt' ) 2400call assert_equal ([ 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 3 ], g: pos ) 2401let g: pos = [] 2402call feedkeys ( ":12345678\<C-R>=setcmdpos(3)??''\<CR>\<Left>\<Esc>" , 'xt' ) 2403call assert_equal ([ 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 3 , 2 ], g: pos ) 2404au ! CursorMoved C 2405 2406" setcmdpos() is no-op inside an autocommand 2407au ! CursorMoved C : let g: pos += [ getcmdpos ()] | call setcmdpos ( 1 ) 2408let g: pos = [] 2409call feedkeys ( ":hello\<Left>\<Left>\<Esc>" , 'xt' ) 2410call assert_equal ([ 2 , 3 , 4 , 5 , 6 , 5 , 4 ], g: pos ) 2411au ! CursorMoved C 2412 2413unlet g: entered 2414unlet g: left 2415unlet g: pos 2416endfunc 2417 2418" Test for BufWritePre autocommand that deletes or unloads the buffer. 2419func Test_BufWritePre () 2420% bwipe 2421au BufWritePre Xxx1 bunload 2422au BufWritePre Xxx2 bwipe 2423 2424call writefile ([ 'start of Xxx1' , 'test' , 'end of Xxx1' ], 'Xxx1' , 'D' ) 2425call writefile ([ 'start of Xxx2' , 'test' , 'end of Xxx2' ], 'Xxx2' , 'D' ) 2426 2427edit Xtest 2428e ! Xxx2 2429bdel Xtest 2430e Xxx1 2431" write it, will unload it and give an error msg 2432call assert_fails ( 'w' , 'E203:' ) 2433call assert_equal ( 'Xxx2' , bufname ( '%' )) 2434edit Xtest 2435e ! Xxx2 2436bwipe Xtest 2437" write it, will delete the buffer and give an error msg 2438call assert_fails ( 'w' , 'E203:' ) 2439call assert_equal ( 'Xxx1' , bufname ( '%' )) 2440au ! BufWritePre 2441endfunc 2442 2443" Test for BufUnload autocommand that unloads all the other buffers 2444func Test_bufunload_all () 2445let g: test_is_flaky = 1 2446call writefile ([ 'Test file Xxx1' ], 'Xxx1' , 'D' ) 2447call writefile ([ 'Test file Xxx2' ], 'Xxx2' , 'D' ) 2448 2449let content =<< trim [CODE] 2450func UnloadAllBufs() 2451let i = 1 2452while i <= bufnr('$') 2453if i != bufnr('%') && bufloaded(i) 2454exe i . 'bunload' 2455endif 2456let i += 1 2457endwhile 2458endfunc 2459au BufUnload * call UnloadAllBufs() 2460au VimLeave * call writefile(['Test Finished'], 'Xout') 2461set nohidden " Accommodate Nvim default 2462edit Xxx1 2463split Xxx2 2464q 2465[CODE] 2466 2467call writefile ( content , 'Xbunloadtest' , 'D' ) 2468 2469call delete ( 'Xout' ) 2470call system ( GetVimCommandClean () .. ' -N --headless -S Xbunloadtest' ) 2471call assert_true ( filereadable ( 'Xout' )) 2472 2473call delete ( 'Xout' ) 2474endfunc 2475 2476" Some tests for buffer-local autocommands 2477func Test_buflocal_autocmd () 2478let g: bname = '' 2479edit xx 2480au BufLeave <buffer> let g: bname = expand ( "%" ) 2481" here, autocommand for xx should trigger. 2482" but autocommand shall not apply to buffer named <buffer>. 2483edit somefile 2484call assert_equal ( 'xx' , g: bname ) 2485let g: bname = '' 2486" here, autocommand shall be auto-deleted 2487bwipe xx 2488" autocmd should not trigger 2489edit xx 2490call assert_equal ( '' , g: bname ) 2491" autocmd should not trigger 2492edit somefile 2493call assert_equal ( '' , g: bname ) 2494enew 2495unlet g: bname 2496endfunc 2497 2498" Test for "*Cmd" autocommands 2499func Test_Cmd_Autocmds () 2500call writefile ([ 'start of Xxx' , "\tabc2" , 'end of Xxx' ], 'Xxx' , 'D' ) 2501 2502enew ! 2503au BufReadCmd XtestA 0 r Xxx|$del 2504edit XtestA " will read text of Xxd instead 2505call assert_equal ( 'start of Xxx' , getline ( 1 )) 2506 2507au BufWriteCmd XtestA call append ( line ( "$" ), "write" ) 2508write " will append a line to the file 2509call assert_equal ( 'write' , getline ( '$' )) 2510call assert_fails ( 'read XtestA' , 'E484' ) " should not read anything 2511call assert_equal ( 'write' , getline ( 4 )) 2512 2513" now we have: 2514" 1 start of Xxx 2515" 2 abc2 2516" 3 end of Xxx 2517" 4 write 2518 2519au FileReadCmd XtestB '[ r Xxx 25202 r XtestB " will read Xxx below line 2 instead 2521call assert_equal ( 'start of Xxx' , getline ( 3 )) 2522 2523" now we have: 2524" 1 start of Xxx 2525" 2 abc2 2526" 3 start of Xxx 2527" 4 abc2 2528" 5 end of Xxx 2529" 6 end of Xxx 2530" 7 write 2531 2532au FileWriteCmd XtestC '[ , '] copy $ 2533normal 4GA1 25344 , 5 w XtestC " will copy lines 4 and 5 to the end 2535call assert_equal ( "\tabc21" , getline ( 8 )) 2536call assert_fails ( 'r XtestC' , 'E484' ) " should not read anything 2537call assert_equal ( "end of Xxx" , getline ( 9 )) 2538 2539" now we have: 2540" 1 start of Xxx 2541" 2 abc2 2542" 3 start of Xxx 2543" 4 abc21 2544" 5 end of Xxx 2545" 6 end of Xxx 2546" 7 write 2547" 8 abc21 2548" 9 end of Xxx 2549 2550let g: lines = [] 2551au FileAppendCmd XtestD call extend ( g: lines , getline ( line ( "'[" ), line ( "']" ))) 2552w >>XtestD " will add lines to 'lines' 2553call assert_equal ( 9 , len ( g: lines )) 2554call assert_fails ( '$r XtestD' , 'E484' ) " should not read anything 2555call assert_equal ( 9 , line ( '$' )) 2556call assert_equal ( 'end of Xxx' , getline ( '$' )) 2557 2558au BufReadCmd XtestE 0 r Xxx|$del 2559sp XtestE " split window with test.out 2560call assert_equal ( 'end of Xxx' , getline ( 3 )) 2561 2562let g: lines = [] 2563exe "normal 2Goasdf\<Esc>\<C-W>\<C-W>" 2564au BufWriteCmd XtestE call extend ( g: lines , getline ( 0 , '$' )) 2565wall " will write other window to 'lines' 2566call assert_equal ( 4 , len ( g: lines ), g: lines ) 2567call assert_equal ( "asdf" , g: lines [ 2 ]) 2568 2569au ! BufReadCmd 2570au ! BufWriteCmd 2571au ! FileReadCmd 2572au ! FileWriteCmd 2573au ! FileAppendCmd 2574% bwipe ! 2575enew ! 2576endfunc 2577 2578func s: ReadFile () 2579setl noswapfile nomodified 2580let filename = resolve ( expand ( "<afile>:p" )) 2581execute 'read' fnameescape ( filename ) 25821 d _ 2583exe 'file' fnameescape ( filename ) 2584setl buftype = acwrite 2585endfunc 2586 2587func s: WriteFile () 2588let filename = resolve ( expand ( "<afile>:p" )) 2589setl buftype = 2590noautocmd execute 'write' fnameescape(filename) 2591setl buftype = acwrite 2592setl nomodified 2593endfunc 2594 2595func Test_BufReadCmd () 2596autocmd BufReadCmd * .test call s: ReadFile () 2597autocmd BufWriteCmd * .test call s: WriteFile () 2598 2599call writefile ([ 'one' , 'two' , 'three' ], 'Xcmd.test' , 'D' ) 2600edit Xcmd.test 2601set noruler 2602call assert_match ( 'Xcmd.test" line 1 of 3' , execute ( 'file' )) 2603set ruler 2604call assert_match ( 'Xcmd.test" 3 lines --33%--' , execute ( 'file' )) 2605normal ! Gofour 2606write 2607call assert_equal ([ 'one' , 'two' , 'three' , 'four' ], readfile ( 'Xcmd.test' )) 2608 2609bwipe ! 2610au ! BufReadCmd 2611au ! BufWriteCmd 2612endfunc 2613 2614func Test_BufWriteCmd () 2615autocmd BufWriteCmd Xbufwritecmd let g: written = 1 2616new 2617file Xbufwritecmd 2618set buftype = acwrite 2619call mkdir ( 'Xbufwritecmd' , 'D' ) 2620write 2621" BufWriteCmd should be triggered even if a directory has the same name 2622call assert_equal ( 1 , g: written ) 2623unlet g: written 2624au ! BufWriteCmd 2625bwipe ! 2626endfunc 2627 2628func SetChangeMarks ( start , end ) 2629exe a: start .. 'mark [' 2630exe a: end .. 'mark ]' 2631endfunc 2632 2633" Verify the effects of autocmds on '[ and '] 2634func Test_change_mark_in_autocmds () 2635edit ! Xtest 2636call feedkeys ( "ia\<CR>b\<CR>c\<CR>d\<C-g>u\<Esc>" , 'xtn' ) 2637 2638call SetChangeMarks ( 2 , 3 ) 2639write 2640call assert_equal ([ 1 , 4 ], [ line ( "'[" ), line ( "']" )]) 2641 2642call SetChangeMarks ( 2 , 3 ) 2643au BufWritePre * call assert_equal ([ 1 , 4 ], [ line ( "'[" ), line ( "']" )]) 2644write 2645au ! BufWritePre 2646 2647if has ( 'unix' ) 2648write XtestFilter 2649write >> XtestFilter 2650 2651call SetChangeMarks ( 2 , 3 ) 2652" Marks are set to the entire range of the write 2653au FilterWritePre * call assert_equal ([ 1 , 4 ], [ line ( "'[" ), line ( "']" )]) 2654" '[ is adjusted to just before the line that will receive the filtered 2655" data 2656au FilterReadPre * call assert_equal ([ 4 , 4 ], [ line ( "'[" ), line ( "']" )]) 2657" The filtered data is read into the buffer, and the source lines are 2658" still present, so the range is after the source lines 2659au FilterReadPost * call assert_equal ([ 5 , 12 ], [ line ( "'[" ), line ( "']" )]) 2660% ! cat XtestFilter 2661" After the filtered data is read, the original lines are deleted 2662call assert_equal ([ 1 , 8 ], [ line ( "'[" ), line ( "']" )]) 2663au ! FilterWritePre , FilterReadPre , FilterReadPost 2664undo 2665 2666call SetChangeMarks ( 1 , 4 ) 2667au FilterWritePre * call assert_equal ([ 2 , 3 ], [ line ( "'[" ), line ( "']" )]) 2668au FilterReadPre * call assert_equal ([ 3 , 3 ], [ line ( "'[" ), line ( "']" )]) 2669au FilterReadPost * call assert_equal ([ 4 , 11 ], [ line ( "'[" ), line ( "']" )]) 26702 , 3 ! cat XtestFilter 2671call assert_equal ([ 2 , 9 ], [ line ( "'[" ), line ( "']" )]) 2672au ! FilterWritePre , FilterReadPre , FilterReadPost 2673undo 2674 2675call delete ( 'XtestFilter' ) 2676endif 2677 2678call SetChangeMarks ( 1 , 4 ) 2679au FileWritePre * call assert_equal ([ 2 , 3 ], [ line ( "'[" ), line ( "']" )]) 26802 , 3 write Xtest2 2681au ! FileWritePre 2682 2683call SetChangeMarks ( 2 , 3 ) 2684au FileAppendPre * call assert_equal ([ 1 , 4 ], [ line ( "'[" ), line ( "']" )]) 2685write >> Xtest2 2686au ! FileAppendPre 2687 2688call SetChangeMarks ( 1 , 4 ) 2689au FileAppendPre * call assert_equal ([ 2 , 3 ], [ line ( "'[" ), line ( "']" )]) 26902 , 3 write >> Xtest2 2691au ! FileAppendPre 2692 2693call SetChangeMarks ( 1 , 1 ) 2694au FileReadPre * call assert_equal ([ 3 , 1 ], [ line ( "'[" ), line ( "']" )]) 2695au FileReadPost * call assert_equal ([ 4 , 11 ], [ line ( "'[" ), line ( "']" )]) 26963 read Xtest2 2697au ! FileReadPre , FileReadPost 2698undo 2699 2700call SetChangeMarks ( 4 , 4 ) 2701" When the line is 0, it's adjusted to 1 2702au FileReadPre * call assert_equal ([ 1 , 4 ], [ line ( "'[" ), line ( "']" )]) 2703au FileReadPost * call assert_equal ([ 1 , 8 ], [ line ( "'[" ), line ( "']" )]) 27040 read Xtest2 2705au ! FileReadPre , FileReadPost 2706undo 2707 2708call SetChangeMarks ( 4 , 4 ) 2709" When the line is 0, it's adjusted to 1 2710au FileReadPre * call assert_equal ([ 1 , 4 ], [ line ( "'[" ), line ( "']" )]) 2711au FileReadPost * call assert_equal ([ 2 , 9 ], [ line ( "'[" ), line ( "']" )]) 27121 read Xtest2 2713au ! FileReadPre , FileReadPost 2714undo 2715 2716bwipe ! 2717call delete ( 'Xtest' ) 2718call delete ( 'Xtest2' ) 2719endfunc 2720 2721func Test_Filter_noshelltemp () 2722CheckExecutable cat 2723 2724enew ! 2725call setline ( 1 , [ 'a' , 'b' , 'c' , 'd' ]) 2726 2727let shelltemp = & shelltemp 2728set shelltemp 2729 2730let g: filter_au = 0 2731au FilterWritePre * let g: filter_au += 1 2732au FilterReadPre * let g: filter_au += 1 2733au FilterReadPost * let g: filter_au += 1 2734% ! cat 2735call assert_equal ( 3 , g: filter_au ) 2736 2737if has ( 'filterpipe' ) 2738set noshelltemp 2739 2740let g: filter_au = 0 2741au FilterWritePre * let g: filter_au += 1 2742au FilterReadPre * let g: filter_au += 1 2743au FilterReadPost * let g: filter_au += 1 2744% ! cat 2745call assert_equal ( 0 , g: filter_au ) 2746endif 2747 2748au ! FilterWritePre , FilterReadPre , FilterReadPost 2749let & shelltemp = shelltemp 2750bwipe ! 2751endfunc 2752 2753func Test_TextYankPost () 2754enew ! 2755call setline ( 1 , [ 'foo' ]) 2756 2757let g: event = [] 2758au TextYankPost * let g: event = copy ( v: event ) 2759 2760call assert_equal ({}, v: event ) 2761call assert_fails ( 'let v:event = {}' , 'E46:' ) 2762call assert_fails ( 'let v:event.mykey = 0' , 'E742:' ) 2763 2764norm "ayiw 2765call assert_equal ( 2766\ { 'regcontents' : [ 'foo' ], 'inclusive' : v: true , 'regname' : 'a' , 'operator' : 'y' , 'visual' : v: false , 'regtype' : 'v' }, 2767\ g: event ) 2768norm y_ 2769call assert_equal ( 2770\ { 'regcontents' : [ 'foo' ], 'inclusive' : v: false , 'regname' : '' , 'operator' : 'y' , 'visual' : v: false , 'regtype' : 'V' }, 2771\ g: event ) 2772norm Vy 2773call assert_equal ( 2774\ { 'regcontents' : [ 'foo' ], 'inclusive' : v: true , 'regname' : '' , 'operator' : 'y' , 'visual' : v: true , 'regtype' : 'V' }, 2775\ g: event ) 2776call feedkeys ( "\<C-V>y" , 'x' ) 2777call assert_equal ( 2778\ { 'regcontents' : [ 'f' ], 'inclusive' : v: true , 'regname' : '' , 'operator' : 'y' , 'visual' : v: true , 'regtype' : "\x161" }, 2779\ g: event ) 2780norm "xciwbar 2781call assert_equal ( 2782\ { 'regcontents' : [ 'foo' ], 'inclusive' : v: true , 'regname' : 'x' , 'operator' : 'c' , 'visual' : v: false , 'regtype' : 'v' }, 2783\ g: event ) 2784norm "bdiw 2785call assert_equal ( 2786\ { 'regcontents' : [ 'bar' ], 'inclusive' : v: true , 'regname' : 'b' , 'operator' : 'd' , 'visual' : v: false , 'regtype' : 'v' }, 2787\ g: event ) 2788 2789call assert_equal ({}, v: event ) 2790 2791au ! TextYankPost 2792unlet g: event 2793bwipe ! 2794endfunc 2795 2796func Test_autocommand_all_events () 2797call assert_fails ( 'au * * bwipe' , 'E1155:' ) 2798call assert_fails ( 'au * x bwipe' , 'E1155:' ) 2799call assert_fails ( 'au! * x bwipe' , 'E1155:' ) 2800endfunc 2801 2802func Test_autocmd_user () 2803au User MyEvent let s: res = [ expand ( "<afile>" ), expand ( "<amatch>" )] 2804doautocmd User MyEvent 2805call assert_equal ([ 'MyEvent' , 'MyEvent' ], s: res ) 2806au ! User 2807unlet s: res 2808endfunc 2809 2810function s: Before_test_dirchanged () 2811augroup test_dirchanged 2812autocmd ! 2813augroup END 2814let s: li = [] 2815let s: dir_this = getcwd () 2816let s: dir_foo = s: dir_this . '/Xfoo' 2817call mkdir ( s: dir_foo ) 2818let s: dir_bar = s: dir_this . '/Xbar' 2819call mkdir ( s: dir_bar ) 2820endfunc 2821 2822function s: After_test_dirchanged () 2823call chdir ( s: dir_this ) 2824call delete ( s: dir_foo , 'd' ) 2825call delete ( s: dir_bar , 'd' ) 2826augroup test_dirchanged 2827autocmd ! 2828augroup END 2829endfunc 2830 2831function Test_dirchanged_global () 2832call s: Before_test_dirchanged () 2833autocmd test_dirchanged DirChangedPre global call add ( s: li , expand ( "<amatch>" ) .. " pre cd " .. v: event . directory ) 2834autocmd test_dirchanged DirChanged global call add ( s: li , "cd:" ) 2835autocmd test_dirchanged DirChanged global call add ( s: li , expand ( "<afile>" )) 2836call chdir ( s: dir_foo ) 2837let expected = [ "global pre cd " .. s: dir_foo , "cd:" , s: dir_foo ] 2838call assert_equal ( expected , s: li ) 2839call chdir ( s: dir_foo ) 2840call assert_equal ( expected , s: li ) 2841exe 'lcd ' .. fnameescape ( s: dir_bar ) 2842call assert_equal ( expected , s: li ) 2843 2844exe 'cd ' .. s: dir_foo 2845exe 'cd ' .. s: dir_bar 2846autocmd ! test_dirchanged DirChanged global let g: result = expand ( "<afile>" ) 2847cd - 2848call assert_equal ( s: dir_foo , substitute ( g: result , '\\' , '/' , 'g' )) 2849 2850call s: After_test_dirchanged () 2851endfunc 2852 2853function Test_dirchanged_local () 2854call s: Before_test_dirchanged () 2855autocmd test_dirchanged DirChanged window call add ( s: li , "lcd:" ) 2856autocmd test_dirchanged DirChanged window call add ( s: li , expand ( "<afile>" )) 2857call chdir ( s: dir_foo ) 2858call assert_equal ([], s: li ) 2859exe 'lcd ' .. fnameescape ( s: dir_bar ) 2860call assert_equal ([ "lcd:" , s: dir_bar ], s: li ) 2861exe 'lcd ' .. fnameescape ( s: dir_bar ) 2862call assert_equal ([ "lcd:" , s: dir_bar ], s: li ) 2863call s: After_test_dirchanged () 2864endfunc 2865 2866function Test_dirchanged_auto () 2867CheckFunction test_autochdir 2868CheckOption autochdir 2869call s: Before_test_dirchanged () 2870call test_autochdir () 2871autocmd test_dirchanged DirChangedPre auto call add ( s: li , "pre cd " .. v: event . directory ) 2872autocmd test_dirchanged DirChanged auto call add ( s: li , "auto:" ) 2873autocmd test_dirchanged DirChanged auto call add ( s: li , expand ( "<afile>" )) 2874set acd 2875cd .. 2876call assert_equal ([], s: li ) 2877exe 'edit ' . s: dir_foo . '/Xautofile' 2878call assert_equal ( s: dir_foo , getcwd ()) 2879let expected = [ "pre cd " .. s: dir_foo , "auto:" , s: dir_foo ] 2880call assert_equal ( expected , s: li ) 2881set noacd 2882bwipe ! 2883call s: After_test_dirchanged () 2884endfunc 2885 2886" Test TextChangedI and TextChangedP 2887func Test_ChangedP () 2888new 2889call setline ( 1 , [ 'foo' , 'bar' , 'foobar' ]) 2890call Ntest_override ( "char_avail" , 1 ) 2891set complete = . completeopt = menuone 2892 2893func ! TextChangedAutocmd ( char ) 2894let g: autocmd .= a: char 2895endfunc 2896 2897" TextChanged will not be triggered, only check that it isn't. 2898au ! TextChanged <buffer> : call TextChangedAutocmd ( 'N' ) 2899au ! TextChangedI <buffer> : call TextChangedAutocmd ( 'I' ) 2900au ! TextChangedP <buffer> : call TextChangedAutocmd ( 'P' ) 2901 2902call cursor ( 3 , 1 ) 2903let g: autocmd = '' 2904call feedkeys ( "o\<esc>" , 'tnix' ) 2905call assert_equal ( 'I' , g: autocmd ) 2906 2907let g: autocmd = '' 2908call feedkeys ( "Sf" , 'tnix' ) 2909call assert_equal ( 'II' , g: autocmd ) 2910 2911let g: autocmd = '' 2912call feedkeys ( "Sf\<C-N>" , 'tnix' ) 2913call assert_equal ( 'IIP' , g: autocmd ) 2914 2915let g: autocmd = '' 2916call feedkeys ( "Sf\<C-N>\<C-N>" , 'tnix' ) 2917call assert_equal ( 'IIPP' , g: autocmd ) 2918 2919let g: autocmd = '' 2920call feedkeys ( "Sf\<C-N>\<C-N>\<C-N>" , 'tnix' ) 2921call assert_equal ( 'IIPPP' , g: autocmd ) 2922 2923let g: autocmd = '' 2924call feedkeys ( "Sf\<C-N>\<C-N>\<C-N>\<C-N>" , 'tnix' ) 2925call assert_equal ( 'IIPPPP' , g: autocmd ) 2926 2927call assert_equal ([ 'foo' , 'bar' , 'foobar' , 'foo' ], getline ( 1 , '$' )) 2928" TODO: how should it handle completeopt=noinsert,noselect? 2929 2930" CleanUp 2931call Ntest_override ( "char_avail" , 0 ) 2932au ! TextChanged 2933au ! TextChangedI 2934au ! TextChangedP 2935delfu TextChangedAutocmd 2936unlet ! g: autocmd 2937set complete & vim completeopt & vim 2938 2939bw ! 2940endfunc 2941 2942let g: setline_handled = v: false 2943func SetLineOne () 2944if ! g: setline_handled 2945call setline ( 1 , "(x)" ) 2946let g: setline_handled = v: true 2947endif 2948endfunc 2949 2950func Test_TextChangedI_with_setline () 2951new 2952call Ntest_override ( 'char_avail' , 1 ) 2953autocmd TextChangedI <buffer> call SetLineOne () 2954call feedkeys ( "i(\<CR>\<Esc>" , 'tx' ) 2955call assert_equal ( '(' , getline ( 1 )) 2956call assert_equal ( 'x)' , getline ( 2 )) 2957undo 2958call assert_equal ( '' , getline ( 1 )) 2959call assert_equal ( '' , getline ( 2 )) 2960 2961call Ntest_override ( 'char_avail' , 0 ) 2962bwipe ! 2963endfunc 2964 2965func Test_TextChanged_with_norm () 2966" For unknown reason this fails on MS-Windows 2967CheckNotMSWindows 2968CheckFeature terminal 2969let buf = term_start ([ GetVimProg (), '--clean' , '-c' , 'set noswapfile' ], { 'term_rows' : 3 }) 2970call assert_equal ( 'running' , term_getstatus ( buf )) 2971call term_sendkeys ( buf , ":let g:a=0\<cr>" ) 2972call term_wait ( buf , 50 ) 2973call term_sendkeys ( buf , ":au! TextChanged * :let g:a+=1\<cr>" ) 2974call term_wait ( buf , 50 ) 2975call term_sendkeys ( buf , ":norm! ia\<cr>" ) 2976call term_wait ( buf , 50 ) 2977call term_sendkeys ( buf , ":echo g:a\<cr>" ) 2978call term_wait ( buf , 50 ) 2979call WaitForAssert ({ -> assert_match ( '^1.*$' , term_getline ( buf , 3 ))}) 2980bwipe ! 2981endfunc 2982 2983func Test_Changed_FirstTime () 2984CheckFeature terminal 2985CheckNotGui 2986" Starting a terminal to run Vim is always considered flaky. 2987let g: test_is_flaky = 1 2988 2989" Prepare file for TextChanged event. 2990call writefile ([ '' ], 'Xchanged.txt' , 'D' ) 2991let buf = term_start ([ GetVimProg (), '--clean' , '-c' , 'set noswapfile' ], { 'term_rows' : 3 }) 2992call assert_equal ( 'running' , term_getstatus ( buf )) 2993" Wait for the ruler (in the status line) to be shown. 2994" In ConPTY, there is additional character which is drawn up to the width of 2995" the screen. 2996if has ( 'conpty' ) 2997call WaitForAssert ({ -> assert_match ( '\<All.*$' , term_getline ( buf , 3 ))}) 2998else 2999call WaitForAssert ({ -> assert_match ( '\<All$' , term_getline ( buf , 3 ))}) 3000endif 3001" It's only adding autocmd, so that no event occurs. 3002call term_sendkeys ( buf , ":au! TextChanged <buffer> call writefile(['No'], 'Xchanged.txt')\<cr>" ) 3003call term_sendkeys ( buf , "\<C-\\>\<C-N>:qa!\<cr>" ) 3004call WaitForAssert ({ -> assert_equal ( 'finished' , term_getstatus ( buf ))}) 3005call assert_equal ([ '' ], readfile ( 'Xchanged.txt' )) 3006 3007" clean up 3008bwipe ! 3009endfunc 3010 3011func Test_autocmd_nested () 3012let g: did_nested = 0 3013defer CleanUpTestAuGroup() 3014augroup testing 3015au WinNew * edit somefile 3016au BufNew * let g: did_nested = 1 3017augroup END 3018split 3019call assert_equal ( 0 , g: did_nested ) 3020close 3021bwipe ! somefile 3022 3023" old nested argument still works 3024augroup testing 3025au ! 3026au WinNew * nested edit somefile 3027au BufNew * let g: did_nested = 1 3028augroup END 3029split 3030call assert_equal ( 1 , g: did_nested ) 3031close 3032bwipe ! somefile 3033 3034" New ++nested argument works 3035augroup Testing 3036au ! 3037au WinNew * ++nested edit somefile 3038au BufNew * let g: did_nested = 1 3039augroup END 3040split 3041call assert_equal ( 1 , g: did_nested ) 3042close 3043bwipe ! somefile 3044 3045augroup Testing 3046au ! 3047augroup END 3048 3049call assert_fails ( 'au WinNew * ++nested ++nested echo bad' , 'E983:' ) 3050call assert_fails ( 'au WinNew * nested nested echo bad' , 'E983:' ) 3051endfunc 3052 3053func Test_autocmd_nested_cursor_invalid () 3054set laststatus = 0 3055copen 3056cclose 3057call setline ( 1 , [ 'foo' , 'bar' , 'baz' ]) 30583 3059augroup nested_inv 3060autocmd User foo ++nested copen 3061autocmd BufAdd * let & laststatus = 2 - & laststatus 3062augroup END 3063doautocmd User foo 3064 3065augroup nested_inv 3066au ! 3067augroup END 3068set laststatus & 3069cclose 3070bwipe ! 3071endfunc 3072 3073func Test_autocmd_nested_keeps_cursor_pos () 3074enew 3075call setline ( 1 , 'foo' ) 3076autocmd User foo ++nested normal ! $a 3077autocmd InsertLeave * : 3078doautocmd User foo 3079call assert_equal ([ 0 , 1 , 3 , 0 ], getpos ( '.' )) 3080 3081bwipe ! 3082endfunc 3083 3084func Test_autocmd_nested_switch_window () 3085" run this in a separate Vim so that SafeState works 3086CheckRunVimInTerminal 3087CheckScreendump 3088 3089let lines =<< trim END 3090vim9script 3091['()']->writefile('Xautofile') 3092autocmd VimEnter * ++nested edit Xautofile | split 3093autocmd BufReadPost * autocmd SafeState * ++once foldclosed('.') 3094autocmd WinEnter * matchadd('ErrorMsg', 'pat') 3095END 3096call writefile ( lines , 'Xautoscript' , 'D' ) 3097let buf = RunVimInTerminal ( '-S Xautoscript' , { 'rows' : 10 }) 3098call VerifyScreenDump ( buf , 'Test_autocmd_nested_switch' , {}) 3099 3100call StopVimInTerminal ( buf ) 3101call delete ( 'Xautofile' ) 3102endfunc 3103 3104func Test_autocmd_once () 3105" Without ++once WinNew triggers twice 3106let g: did_split = 0 3107augroup Testing 3108au WinNew * let g: did_split += 1 3109augroup END 3110split 3111split 3112call assert_equal ( 2 , g: did_split ) 3113call assert_true ( exists ( '#WinNew' )) 3114close 3115close 3116 3117" With ++once WinNew triggers once 3118let g: did_split = 0 3119augroup Testing 3120au ! 3121au WinNew * ++once let g: did_split += 1 3122augroup END 3123split 3124split 3125call assert_equal ( 1 , g: did_split ) 3126call assert_false ( exists ( '#WinNew' )) 3127close 3128close 3129 3130call assert_fails ( 'au WinNew * ++once ++once echo bad' , 'E983:' ) 3131call assert_false ( exists ( '#WinNew' )) 3132endfunc 3133 3134func Test_autocmd_dup_arg () 3135" Duplicate ++once / ++nested, or the legacy "nested" used twice, must 3136" error out *and* not create the autocommand. Using an environment 3137" variable in the pattern also exercises the error-exit path that frees 3138" the expanded pattern (checked by the address/leak sanitizers). 3139augroup XdupTest 3140au ! 3141augroup END 3142let $ XAUTODIR = 'Xfoo' 3143 3144" New behavior: duplicate ++once now aborts, the autocmd is not added 3145call assert_fails ( 'au XdupTest WinNew $XAUTODIR/* ++once ++once echo bad' , 'E983:' ) 3146call assert_false ( exists ( '#XdupTest#WinNew' )) 3147 3148call assert_fails ( 'au XdupTest WinNew $XAUTODIR/* ++nested ++nested echo bad' , 'E983:' ) 3149call assert_false ( exists ( '#XdupTest#WinNew' )) 3150 3151call assert_fails ( 'au XdupTest WinNew $XAUTODIR/* nested nested echo bad' , 'E983:' ) 3152call assert_false ( exists ( '#XdupTest#WinNew' )) 3153 3154" "nested" without "++" is rejected in Vim9 script (also frees the pattern) 3155"call assert_fails('vim9cmd au XdupTest WinNew $XAUTODIR/* nested echo bad', 'E1078:') 3156call assert_false ( exists ( '#XdupTest#WinNew' )) 3157 3158augroup XdupTest 3159au ! 3160augroup END 3161augroup ! XdupTest 3162let $ XAUTODIR = '' 3163endfunc 3164 3165 3166func Test_autocmd_bufreadpre () 3167new 3168let b: bufreadpre = 1 3169call append ( 0 , range ( 100 )) 3170w ! XAutocmdBufReadPre.txt 3171autocmd BufReadPre <buffer> : let b: bufreadpre += 1 3172norm ! 50gg 3173sp 3174norm ! 100gg 3175wincmd p 3176let g: wsv1 = winsaveview () 3177wincmd p 3178let g: wsv2 = winsaveview () 3179" triggers BufReadPre, should not move the cursor in either window 3180" The topline may change one line in a large window. 3181edit 3182call assert_inrange ( g: wsv2 . topline - 1 , g: wsv2 . topline + 1 , winsaveview (). topline ) 3183call assert_equal ( g: wsv2 . lnum , winsaveview (). lnum ) 3184call assert_equal ( 2 , b: bufreadpre ) 3185wincmd p 3186call assert_equal ( g: wsv1 . topline , winsaveview (). topline ) 3187call assert_equal ( g: wsv1 . lnum , winsaveview (). lnum ) 3188call assert_equal ( 2 , b: bufreadpre ) 3189" Now set the cursor position in an BufReadPre autocommand 3190" (even though the position will be invalid, this should make Vim reset the 3191" cursor position in the other window. 3192wincmd p 31931 " set cpo+=g 3194" won't do anything, but try to set the cursor on an invalid lnum 3195autocmd BufReadPre <buffer> : norm ! 70gg 3196" triggers BufReadPre, should not move the cursor in either window 3197e 3198call assert_equal ( 1 , winsaveview (). topline ) 3199call assert_equal ( 1 , winsaveview (). lnum ) 3200call assert_equal ( 3 , b: bufreadpre ) 3201wincmd p 3202call assert_equal ( g: wsv1 . topline , winsaveview (). topline ) 3203call assert_equal ( g: wsv1 . lnum , winsaveview (). lnum ) 3204call assert_equal ( 3 , b: bufreadpre ) 3205close 3206close 3207call delete ( 'XAutocmdBufReadPre.txt' ) 3208set cpo -= g 3209endfunc 3210 3211" FileChangedShell tested in test_filechanged.vim 3212 3213" Tests for the following autocommands: 3214" - FileWritePre writing a compressed file 3215" - FileReadPost reading a compressed file 3216" - BufNewFile reading a file template 3217" - BufReadPre decompressing the file to be read 3218" - FilterReadPre substituting characters in the temp file 3219" - FilterReadPost substituting characters after filtering 3220" - FileReadPre set options for decompression 3221" - FileReadPost decompress the file 3222func Test_ReadWrite_Autocmds () 3223" Run this test only on Unix-like systems and if gzip is available 3224if ! has ( 'unix' ) || ! executable ( "gzip" ) 3225return 3226endif 3227 3228" Make $GZIP empty, "-v" would cause trouble. 3229let $ GZIP = "" 3230 3231" Use a FileChangedShell autocommand to avoid a prompt for 'Xtestfile.gz' 3232" being modified outside of Vim (noticed on Solaris). 3233au FileChangedShell * echo 'caught FileChangedShell' 3234 3235" Test for the FileReadPost, FileWritePre and FileWritePost autocmds 3236augroup Test1 3237au ! 3238au FileWritePre * .gz '[ , '] ! gzip 3239au FileWritePost * .gz undo 3240au FileReadPost * .gz '[ , '] ! gzip -d 3241augroup END 3242 3243new 3244set bin 3245call append ( 0 , [ 3246\ 'line 2 Abcdefghijklmnopqrstuvwxyz' , 3247\ 'line 3 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx' , 3248\ 'line 4 Abcdefghijklmnopqrstuvwxyz' , 3249\ 'line 5 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx' , 3250\ 'line 6 Abcdefghijklmnopqrstuvwxyz' , 3251\ 'line 7 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx' , 3252\ 'line 8 Abcdefghijklmnopqrstuvwxyz' , 3253\ 'line 9 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx' , 3254\ 'line 10 Abcdefghijklmnopqrstuvwxyz' 3255\ ]) 32561 , 9 write ! Xtestfile.gz 3257enew ! | close 3258 3259new 3260" Read and decompress the testfile 32610 read Xtestfile.gz 3262call assert_equal ([ 3263\ 'line 2 Abcdefghijklmnopqrstuvwxyz' , 3264\ 'line 3 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx' , 3265\ 'line 4 Abcdefghijklmnopqrstuvwxyz' , 3266\ 'line 5 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx' , 3267\ 'line 6 Abcdefghijklmnopqrstuvwxyz' , 3268\ 'line 7 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx' , 3269\ 'line 8 Abcdefghijklmnopqrstuvwxyz' , 3270\ 'line 9 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx' , 3271\ 'line 10 Abcdefghijklmnopqrstuvwxyz' 3272\ ], getline ( 1 , 9 )) 3273enew ! | close 3274 3275augroup Test1 3276au ! 3277augroup END 3278 3279" Test for the FileAppendPre and FileAppendPost autocmds 3280augroup Test2 3281au ! 3282au BufNewFile * .c read Xtest.c 3283au FileAppendPre * .out '[ , '] s /new/NEW/ 3284au FileAppendPost * .out !cat Xtest.c >> test.out 3285augroup END 3286 3287call writefile ([ '/*' , ' * Here is a new .c file' , ' */' ], 'Xtest.c' , 'D' ) 3288new foo.c " should load Xtest.c 3289call assert_equal ([ '/*' , ' * Here is a new .c file' , ' */' ], getline ( 2 , 4 )) 3290w ! >> test.out " append it to the output file 3291 3292let contents = readfile ( 'test.out' ) 3293call assert_equal ( ' * Here is a NEW .c file' , contents [ 2 ]) 3294call assert_equal ( ' * Here is a new .c file' , contents [ 5 ]) 3295 3296call delete ( 'test.out' ) 3297enew ! | close 3298augroup Test2 3299au ! 3300augroup END 3301 3302" Test for the BufReadPre and BufReadPost autocmds 3303augroup Test3 3304au ! 3305" setup autocommands to decompress before reading and re-compress 3306" afterwards 3307au BufReadPre * .gz exe '!gzip -d ' . shellescape ( expand ( "<afile>" )) 3308au BufReadPre * .gz call rename ( expand ( "<afile>:r" ), expand ( "<afile>" )) 3309au BufReadPost * .gz call rename ( expand ( "<afile>" ), expand ( "<afile>:r" )) 3310au BufReadPost * .gz exe '!gzip ' . shellescape ( expand ( "<afile>:r" )) 3311augroup END 3312 3313e ! Xtestfile.gz " Edit compressed file 3314call assert_equal ([ 3315\ 'line 2 Abcdefghijklmnopqrstuvwxyz' , 3316\ 'line 3 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx' , 3317\ 'line 4 Abcdefghijklmnopqrstuvwxyz' , 3318\ 'line 5 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx' , 3319\ 'line 6 Abcdefghijklmnopqrstuvwxyz' , 3320\ 'line 7 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx' , 3321\ 'line 8 Abcdefghijklmnopqrstuvwxyz' , 3322\ 'line 9 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx' , 3323\ 'line 10 Abcdefghijklmnopqrstuvwxyz' 3324\ ], getline ( 1 , 9 )) 3325 3326w ! >> test.out " Append it to the output file 3327 3328augroup Test3 3329au ! 3330augroup END 3331 3332" Test for the FilterReadPre and FilterReadPost autocmds. 3333set shelltemp " need temp files here 3334augroup Test4 3335au ! 3336au FilterReadPre * .out call rename ( expand ( "<afile>" ), expand ( "<afile>" ) . ".t" ) 3337au FilterReadPre * .out exe 'silent !sed s/e/E/ ' . shellescape ( expand ( "<afile>" )) . ".t >" . shellescape ( expand ( "<afile>" )) 3338au FilterReadPre * .out exe 'silent !rm ' . shellescape ( expand ( "<afile>" )) . '.t' 3339au FilterReadPost * .out '[ , '] s /x/X/g 3340augroup END 3341 3342e ! test.out " Edit the output file 33431 , $ ! cat 3344call assert_equal ([ 3345\ 'linE 2 AbcdefghijklmnopqrstuvwXyz' , 3346\ 'linE 3 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX' , 3347\ 'linE 4 AbcdefghijklmnopqrstuvwXyz' , 3348\ 'linE 5 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX' , 3349\ 'linE 6 AbcdefghijklmnopqrstuvwXyz' , 3350\ 'linE 7 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX' , 3351\ 'linE 8 AbcdefghijklmnopqrstuvwXyz' , 3352\ 'linE 9 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX' , 3353\ 'linE 10 AbcdefghijklmnopqrstuvwXyz' 3354\ ], getline ( 1 , 9 )) 3355call assert_equal ([ 3356\ 'line 2 Abcdefghijklmnopqrstuvwxyz' , 3357\ 'line 3 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx' , 3358\ 'line 4 Abcdefghijklmnopqrstuvwxyz' , 3359\ 'line 5 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx' , 3360\ 'line 6 Abcdefghijklmnopqrstuvwxyz' , 3361\ 'line 7 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx' , 3362\ 'line 8 Abcdefghijklmnopqrstuvwxyz' , 3363\ 'line 9 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx' , 3364\ 'line 10 Abcdefghijklmnopqrstuvwxyz' 3365\ ], readfile ( 'test.out' )) 3366 3367augroup Test4 3368au ! 3369augroup END 3370set shelltemp & vim 3371 3372" Test for the FileReadPre and FileReadPost autocmds. 3373augroup Test5 3374au ! 3375au FileReadPre * .gz exe 'silent !gzip -d ' . shellescape ( expand ( "<afile>" )) 3376au FileReadPre * .gz call rename ( expand ( "<afile>:r" ), expand ( "<afile>" )) 3377au FileReadPost * .gz '[ , '] s /l/L/ 3378augroup END 3379 3380new 33810 r Xtestfile.gz " Read compressed file 3382call assert_equal ([ 3383\ 'Line 2 Abcdefghijklmnopqrstuvwxyz' , 3384\ 'Line 3 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx' , 3385\ 'Line 4 Abcdefghijklmnopqrstuvwxyz' , 3386\ 'Line 5 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx' , 3387\ 'Line 6 Abcdefghijklmnopqrstuvwxyz' , 3388\ 'Line 7 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx' , 3389\ 'Line 8 Abcdefghijklmnopqrstuvwxyz' , 3390\ 'Line 9 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx' , 3391\ 'Line 10 Abcdefghijklmnopqrstuvwxyz' 3392\ ], getline ( 1 , 9 )) 3393call assert_equal ([ 3394\ 'line 2 Abcdefghijklmnopqrstuvwxyz' , 3395\ 'line 3 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx' , 3396\ 'line 4 Abcdefghijklmnopqrstuvwxyz' , 3397\ 'line 5 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx' , 3398\ 'line 6 Abcdefghijklmnopqrstuvwxyz' , 3399\ 'line 7 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx' , 3400\ 'line 8 Abcdefghijklmnopqrstuvwxyz' , 3401\ 'line 9 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx' , 3402\ 'line 10 Abcdefghijklmnopqrstuvwxyz' 3403\ ], readfile ( 'Xtestfile.gz' )) 3404 3405augroup Test5 3406au ! 3407augroup END 3408 3409au ! FileChangedShell 3410call delete ( 'Xtestfile.gz' ) 3411call delete ( 'test.out' ) 3412endfunc 3413 3414func Test_throw_in_BufWritePre () 3415new 3416call setline ( 1 , [ 'one' , 'two' , 'three' ]) 3417call assert_false ( filereadable ( 'Xthefile' )) 3418augroup throwing 3419au BufWritePre X * throw 'do not write' 3420augroup END 3421try 3422w Xthefile 3423catch 3424let caught = 1 3425endtry 3426call assert_equal ( 1 , caught ) 3427call assert_false ( filereadable ( 'Xthefile' )) 3428 3429bwipe ! 3430au ! throwing 3431endfunc 3432 3433func Test_autocmd_in_try_block () 3434call mkdir ( 'Xintrydir' , 'R' ) 3435au BufEnter * let g: fname = expand ( '%' ) 3436try 3437edit Xintrydir/ 3438endtry 3439call assert_match ( 'Xintrydir' , g: fname ) 3440 3441unlet g: fname 3442au ! BufEnter 3443endfunc 3444 3445func Test_autocmd_SafeState () 3446CheckRunVimInTerminal 3447 3448let lines =<< trim END 3449let g:safe = 0 3450let g:again = '' 3451au SafeState * let g:safe += 1 3452au SafeStateAgain * let g:again ..= 'x' 3453func CallTimer() 3454call timer_start(10, {id -> execute('let g:again ..= "t"')}) 3455endfunc 3456END 3457call writefile ( lines , 'XSafeState' , 'D' ) 3458let buf = RunVimInTerminal ( '-S XSafeState' , #{ rows : 6 }) 3459 3460" Sometimes we loop to handle a K_IGNORE, SafeState may be triggered once or 3461" more often. 3462call term_sendkeys ( buf , ":echo g:safe\<CR>" ) 3463call WaitForAssert ({ -> assert_match ( '^\d ' , term_getline ( buf , 6 ))}, 1000 ) 3464 3465" SafeStateAgain should be invoked at least three times 3466call term_sendkeys ( buf , ":echo g:again\<CR>" ) 3467call WaitForAssert ({ -> assert_match ( '^xxx' , term_getline ( buf , 6 ))}, 1000 ) 3468 3469call term_sendkeys ( buf , ":let g:again = ''\<CR>:call CallTimer()\<CR>" ) 3470call TermWait ( buf , 50 ) 3471call term_sendkeys ( buf , ":\<CR>" ) 3472call TermWait ( buf , 50 ) 3473call term_sendkeys ( buf , ":echo g:again\<CR>" ) 3474call WaitForAssert ({ -> assert_match ( 'xtx' , term_getline ( buf , 6 ))}, 1000 ) 3475 3476call StopVimInTerminal ( buf ) 3477endfunc 3478 3479func Test_autocmd_CmdWinEnter () 3480CheckRunVimInTerminal 3481" There is not cmdwin switch, so 3482" test for cmdline_hist 3483" (both are available with small builds) 3484CheckFeature cmdline_hist 3485let lines =<< trim END 3486let b:dummy_var = 'This is a dummy' 3487autocmd CmdWinEnter * quit 3488let winnr = winnr('$') 3489END 3490let filename = 'XCmdWinEnter' 3491call writefile ( lines , filename ) 3492let buf = RunVimInTerminal ( '-S ' . filename , #{ rows : 6 }) 3493 3494call term_sendkeys ( buf , "q:" ) 3495call TermWait ( buf ) 3496call term_sendkeys ( buf , ":echo b:dummy_var\<cr>" ) 3497call WaitForAssert ({ -> assert_match ( '^This is a dummy' , term_getline ( buf , 6 ))}, 2000 ) 3498call term_sendkeys ( buf , ":echo &buftype\<cr>" ) 3499call WaitForAssert ({ -> assert_notmatch ( '^nofile' , term_getline ( buf , 6 ))}, 1000 ) 3500call term_sendkeys ( buf , ":echo winnr\<cr>" ) 3501call WaitForAssert ({ -> assert_match ( '^1' , term_getline ( buf , 6 ))}, 1000 ) 3502 3503" clean up 3504call StopVimInTerminal ( buf ) 3505call delete ( filename ) 3506endfunc 3507 3508func Test_autocmd_was_using_freed_memory () 3509CheckFeature quickfix 3510 3511pedit xx 3512n x 3513augroup winenter 3514au WinEnter * if winnr ( '$' ) > 2 | quit | endif 3515augroup END 3516" Nvim needs large 'winwidth' and 'nowinfixwidth' to crash 3517set winwidth = 99999 nowinfixwidth 3518split 3519 3520augroup winenter 3521au ! WinEnter 3522augroup END 3523 3524set winwidth & winfixwidth & 3525bwipe xx 3526bwipe x 3527pclose 3528endfunc 3529 3530func Test_BufWrite_lockmarks () 3531let g: test_is_flaky = 1 3532edit ! Xtest 3533call setline ( 1 , [ 'a' , 'b' , 'c' , 'd' ]) 3534 3535" :lockmarks preserves the marks 3536call SetChangeMarks ( 2 , 3 ) 3537lockmarks write 3538call assert_equal ([ 2 , 3 ], [ line ( "'[" ), line ( "']" )]) 3539 3540" *WritePre autocmds get the correct line range, but lockmarks preserves the 3541" original values for the user 3542augroup lockmarks 3543au ! 3544au BufWritePre , FilterWritePre * call assert_equal ([ 1 , 4 ], [ line ( "'[" ), line ( "']" )]) 3545au FileWritePre * call assert_equal ([ 3 , 4 ], [ line ( "'[" ), line ( "']" )]) 3546augroup END 3547 3548lockmarks write 3549call assert_equal ([ 2 , 3 ], [ line ( "'[" ), line ( "']" )]) 3550 3551if executable ( 'cat' ) 3552lockmarks %!cat 3553call assert_equal ([ 2 , 3 ], [ line ( "'[" ), line ( "']" )]) 3554endif 3555 3556lockmarks 3,4write Xtest2 3557call assert_equal ([ 2 , 3 ], [ line ( "'[" ), line ( "']" )]) 3558 3559au ! lockmarks 3560augroup ! lockmarks 3561call delete ( 'Xtest' ) 3562call delete ( 'Xtest2' ) 3563endfunc 3564 3565func Test_FileType_spell () 3566if ! isdirectory ( '/tmp' ) 3567throw "Skipped: requires /tmp directory" 3568endif 3569 3570" this was crashing with an invalid free() 3571setglobal spellfile=/tmp/en.utf-8.add 3572augroup crash 3573autocmd ! 3574autocmd BufNewFile , BufReadPost crashfile setf somefiletype 3575autocmd BufNewFile , BufReadPost crashfile set ft = anotherfiletype 3576autocmd FileType anotherfiletype setlocal spell 3577augroup END 3578func ! NoCrash () abort 3579edit /tmp/crashfile 3580endfunc 3581call NoCrash () 3582 3583au ! crash 3584setglobal spellfile= 3585endfunc 3586 3587" this was wiping out the current buffer and using freed memory 3588func Test_SpellFileMissing_bwipe () 3589next 0 3590au SpellFileMissing 0 bwipe 3591call assert_fails ( 'set spell spelllang=0' , 'E937:' ) 3592 3593au ! SpellFileMissing 3594set nospell spelllang = en 3595bwipe 3596endfunc 3597 3598" Test closing a window or editing another buffer from a FileChangedRO handler 3599" in a readonly buffer 3600func Test_FileChangedRO_winclose () 3601augroup FileChangedROTest 3602au ! 3603autocmd FileChangedRO * quit 3604augroup END 3605new 3606set readonly 3607call assert_fails ( 'normal i' , 'E788:' ) 3608close 3609augroup ! FileChangedROTest 3610 3611augroup FileChangedROTest 3612au ! 3613autocmd FileChangedRO * edit Xrofile 3614augroup END 3615new 3616set readonly 3617call assert_fails ( 'normal i' , 'E788:' ) 3618close 3619augroup ! FileChangedROTest 3620endfunc 3621 3622func LogACmd () 3623call add ( g: logged , line ( '$' )) 3624endfunc 3625 3626func Test_TermChanged () 3627throw 'skipped: Nvim does not support TermChanged event' 3628CheckNotGui 3629 3630enew ! 3631tabnew 3632call setline ( 1 , [ 'a' , 'b' , 'c' , 'd' ]) 3633$ 3634au TermChanged * call LogACmd () 3635let g: logged = [] 3636let term_save = & term 3637set term = xterm 3638call assert_equal ([ 1 , 4 ], g: logged ) 3639 3640au ! TermChanged 3641let & term = term_save 3642bwipe ! 3643endfunc 3644 3645" Test for FileReadCmd autocmd 3646func Test_autocmd_FileReadCmd () 3647func ReadFileCmd () 3648call append ( line ( '$' ), "v:cmdarg = " .. v: cmdarg ) 3649endfunc 3650augroup FileReadCmdTest 3651au ! 3652au FileReadCmd Xtest call ReadFileCmd () 3653augroup END 3654 3655new 3656read ++bin Xtest 3657read ++nobin Xtest 3658read ++edit Xtest 3659read ++bad=keep Xtest 3660read ++bad=drop Xtest 3661read ++bad=- Xtest 3662read ++ff=unix Xtest 3663read ++ff=dos Xtest 3664read ++ff=mac Xtest 3665read ++enc=utf-8 Xtest 3666 3667call assert_equal ([ '' , 3668\ 'v:cmdarg = ++bin' , 3669\ 'v:cmdarg = ++nobin' , 3670\ 'v:cmdarg = ++edit' , 3671\ 'v:cmdarg = ++bad=keep' , 3672\ 'v:cmdarg = ++bad=drop' , 3673\ 'v:cmdarg = ++bad=-' , 3674\ 'v:cmdarg = ++ff=unix' , 3675\ 'v:cmdarg = ++ff=dos' , 3676\ 'v:cmdarg = ++ff=mac' , 3677\ 'v:cmdarg = ++enc=utf-8' ], getline ( 1 , '$' )) 3678 3679bwipe ! 3680augroup FileReadCmdTest 3681au ! 3682augroup END 3683delfunc ReadFileCmd 3684endfunc 3685 3686" Test for passing invalid arguments to autocmd 3687func Test_autocmd_invalid_args () 3688" Additional character after * for event 3689call assert_fails ( 'autocmd *a Xinvfile set ff=unix' , 'E215:' ) 3690augroup Test 3691augroup END 3692" Invalid autocmd event 3693call assert_fails ( 'autocmd Bufabc Xinvfile set ft=vim' , 'E216:' ) 3694" Invalid autocmd event in a autocmd group 3695call assert_fails ( 'autocmd Test Bufabc Xinvfile set ft=vim' , 'E216:' ) 3696augroup ! Test 3697" Execute all autocmds 3698call assert_fails ( 'doautocmd * BufEnter' , 'E217:' ) 3699call assert_fails ( 'augroup! x1a2b3' , 'E367:' ) 3700call assert_fails ( 'autocmd BufNew <buffer=999> pwd' , 'E680:' ) 3701call assert_fails ( 'autocmd BufNew \) set ff=unix' , 'E55:' ) 3702endfunc 3703 3704" Test for deep nesting of autocmds 3705func Test_autocmd_deep_nesting () 3706autocmd BufEnter Xdeepfile doautocmd BufEnter Xdeepfile 3707call assert_fails ( 'doautocmd BufEnter Xdeepfile' , 'E218:' ) 3708autocmd ! BufEnter Xdeepfile 3709endfunc 3710 3711" Tests for SigUSR1 autocmd event, which is only available on posix systems. 3712func Test_autocmd_sigusr1 () 3713CheckUnix 3714 3715let g: sigusr1_passed = 0 3716au Signal SIGUSR1 let g: sigusr1_passed = 1 3717call system ( 'kill -s usr1 ' . getpid ()) 3718call WaitForAssert ({ -> assert_true ( g: sigusr1_passed )}) 3719 3720au ! Signal 3721unlet g: sigusr1_passed 3722endfunc 3723 3724" Test for BufReadPre autocmd deleting the file 3725func Test_BufReadPre_delfile () 3726augroup TestAuCmd 3727au ! 3728autocmd BufReadPre XbufreadPre call delete ( 'XbufreadPre' ) 3729augroup END 3730call writefile ([], 'XbufreadPre' , 'D' ) 3731call assert_fails ( 'new XbufreadPre' , 'E200:' ) 3732call assert_equal ( 'XbufreadPre' , @% ) 3733call assert_equal ( 1 , & readonly ) 3734 3735augroup TestAuCmd 3736au ! 3737augroup END 3738close ! 3739endfunc 3740 3741" Test for BufReadPre autocmd changing the current buffer 3742func Test_BufReadPre_changebuf () 3743augroup TestAuCmd 3744au ! 3745autocmd BufReadPre Xchangebuf edit Xsomeotherfile 3746augroup END 3747call writefile ([], 'Xchangebuf' , 'D' ) 3748call assert_fails ( 'new Xchangebuf' , 'E201:' ) 3749call assert_equal ( 'Xsomeotherfile' , @% ) 3750call assert_equal ( 1 , & readonly ) 3751 3752augroup TestAuCmd 3753au ! 3754augroup END 3755close ! 3756endfunc 3757 3758" Test for BufWipeout autocmd changing the current buffer when reading a file 3759" in an empty buffer with 'f' flag in 'cpo' 3760func Test_BufDelete_changebuf () 3761new 3762augroup TestAuCmd 3763au ! 3764autocmd BufWipeout * let bufnr = bufadd ( 'somefile' ) | exe "b " .. bufnr 3765augroup END 3766let save_cpo = & cpo 3767set cpo += f 3768call assert_fails ( 'r Xfile' , [ 'E812:' , 'E484:' ]) 3769call assert_equal ( 'somefile' , @% ) 3770let & cpo = save_cpo 3771augroup TestAuCmd 3772au ! 3773augroup END 3774close ! 3775endfunc 3776 3777" Test for the temporary internal window used to execute autocmds 3778func Test_autocmd_window () 3779% bw ! 3780edit one.txt 3781tabnew two.txt 3782vnew three.txt 3783tabnew four.txt 3784tabprevious 3785let g: blist = [] 3786augroup aucmd_win_test1 3787au ! 3788au BufEnter * call add ( g: blist , [ expand ( '<afile>' ), 3789\ win_gettype ( bufwinnr ( expand ( '<afile>' )))]) 3790augroup END 3791 3792doautoall BufEnter 3793call assert_equal ([ 3794\ [ 'one.txt' , 'autocmd' ], 3795\ [ 'two.txt' , '' ], 3796\ [ 'four.txt' , 'autocmd' ], 3797\ [ 'three.txt' , '' ], 3798\ ], g: blist ) 3799 3800augroup aucmd_win_test1 3801au ! 3802augroup END 3803augroup ! aucmd_win_test1 3804% bw ! 3805endfunc 3806 3807" Test for trying to close the temporary window used for executing an autocmd 3808func Test_close_autocmd_window () 3809% bw ! 3810edit one.txt 3811tabnew two.txt 3812augroup aucmd_win_test2 3813au ! 3814" Nvim makes aucmd_win the last window 3815" au BufEnter * if expand('<afile>') == 'one.txt' | 1close | endif 3816au BufEnter * if expand ( '<afile>' ) == 'one.txt' | close | endif 3817augroup END 3818 3819call assert_fails ( 'doautoall BufEnter' , 'E813:' ) 3820 3821augroup aucmd_win_test2 3822au ! 3823augroup END 3824augroup ! aucmd_win_test2 3825% bw ! 3826endfunc 3827 3828" Test for trying to close the tab that has the temporary window for exeucing 3829" an autocmd. 3830func Test_close_autocmd_tab () 3831edit one.txt 3832tabnew two.txt 3833augroup aucmd_win_test 3834au ! 3835au BufEnter * if expand ( '<afile>' ) == 'one.txt' | tabfirst | tabonly | endif 3836augroup END 3837 3838call assert_fails ( 'doautoall BufEnter' , 'E813:' ) 3839 3840tabonly 3841augroup aucmd_win_test 3842au ! 3843augroup END 3844augroup ! aucmd_win_test 3845% bwipe ! 3846endfunc 3847 3848func Test_Visual_doautoall_redraw () 3849call setline ( 1 , [ 'a' , 'b' ]) 3850new 3851wincmd p 3852call feedkeys ( "G\<C-V>" , 'txn' ) 3853autocmd User Explode ++once redraw 3854doautoall User Explode 3855% bwipe ! 3856endfunc 3857 3858func Test_get_Visual_selection_in_curbuf_autocmd () 3859call Ntest_override ( 'starting' , 1 ) 3860new 3861autocmd OptionSet list let b: text = getregion ( getpos ( '.' ), getpos ( 'v' )) 3862call setline ( 1 , 'foo bar baz' ) 3863 3864normal ! gg0fbvtb 3865setlocal list 3866call assert_equal ([ 'bar ' ], b: text ) 3867exe "normal! \<Esc>" 3868 3869normal ! v0 3870call setbufvar ( '%' , '&list' , v: false ) 3871call assert_equal ([ 'foo bar ' ], b: text ) 3872exe "normal! \<Esc>" 3873 3874autocmd ! OptionSet list 3875bwipe ! 3876call Ntest_override ( 'starting' , 0 ) 3877endfunc 3878 3879" This was using freed memory. 3880func Test_BufNew_arglocal () 3881arglocal 3882au BufNew * arglocal 3883call assert_fails ( 'drop xx' , 'E1156:' ) 3884 3885au ! BufNew 3886endfunc 3887 3888func Test_autocmd_closes_window () 3889au BufNew , BufWinLeave * e %e 3890file yyy 3891au BufNew , BufWinLeave * ball 3892n xxx 3893 3894% bwipe 3895au ! BufNew 3896au ! BufWinLeave 3897endfunc 3898 3899func Test_autocmd_quit_psearch () 3900sn aa bb 3901augroup aucmd_win_test 3902au ! 3903au BufEnter , BufLeave , BufNew , WinEnter , WinLeave , WinNew * if winnr ( '$' ) > 1 | q | endif 3904augroup END 3905ps / 3906 3907augroup aucmd_win_test 3908au ! 3909augroup END 3910new 3911pclose 3912endfunc 3913 3914" Fuzzer found some strange combination that caused a crash. 3915func Test_autocmd_normal_mess () 3916" Uses `q/` (cmdwin) in autocmd; with non-blocking cmdwin (#40312) the 3917" expected E1159 path now hits E1513 (winfixbuf) instead. 3918throw 'Skipped: Nvim supports cmdwin freedom #40312' 3919" For unknown reason this hangs on MS-Windows 3920CheckNotMSWindows 3921 3922augroup aucmd_normal_test 3923au BufLeave , BufWinLeave , BufHidden , BufUnload , BufDelete , BufWipeout * norm 7q/qc 3924augroup END 3925" Nvim has removed :open 3926" call assert_fails('o4', 'E1159:') 3927call assert_fails ( 'e4' , 'E1159:' ) 3928silent ! H 3929call assert_fails ( 'e xx' , 'E1159:' ) 3930normal G 3931 3932augroup aucmd_normal_test 3933au ! 3934augroup END 3935endfunc 3936 3937func Test_autocmd_closing_cmdwin () 3938throw 'Skipped: Nvim supports cmdwin freedom #40312' 3939" For unknown reason this hangs on MS-Windows 3940CheckNotMSWindows 3941 3942au BufWinLeave * nested q 3943call assert_fails ( "norm 7q?\n" , 'E855:' ) 3944 3945au ! BufWinLeave 3946new 3947only 3948endfunc 3949 3950func Test_autocmd_vimgrep () 3951augroup aucmd_vimgrep 3952au QuickfixCmdPre , BufNew , BufReadCmd * sb 3953" Nvim makes aucmd_win the last window 3954" au QuickfixCmdPre,BufNew,BufReadCmd * q9 3955au QuickfixCmdPre , BufNew , BufReadCmd * exe 'q' .. ( winnr ( '$' ) - ( win_gettype ( winnr ( '$' )) == 'autocmd' )) 3956augroup END 3957call assert_fails ( 'lv ?a? foo' , 'E926:' ) 3958 3959augroup aucmd_vimgrep 3960au ! 3961augroup END 3962endfunc 3963 3964func Test_closing_autocmd_window () 3965let lines =<< trim END 3966edit Xa.txt 3967tabnew Xb.txt 3968autocmd BufEnter Xa.txt unhide 1 3969doautoall BufEnter 3970END 3971call CheckScriptFailure ( lines , 'E814:' ) 3972au ! BufEnter 3973bwipe Xa.txt 3974bwipe Xb.txt 3975endfunc 3976 3977func Test_switch_window_in_autocmd_window () 3978edit Xa.txt 3979tabnew Xb.txt 3980autocmd BufEnter Xa.txt wincmd w 3981doautoall BufEnter 3982au ! BufEnter 3983bwipe Xa.txt 3984call assert_false ( bufexists ( 'Xa.txt' )) 3985bwipe Xb.txt 3986call assert_false ( bufexists ( 'Xb.txt' )) 3987endfunc 3988 3989" Test that using the autocommand window doesn't change current directory. 3990func Test_autocmd_window_cwd () 3991let saveddir = getcwd () 3992call mkdir ( 'Xcwd/a/b/c/d' , 'pR' ) 3993 3994new Xa.txt 3995tabnew 3996new Xb.txt 3997 3998tabprev 3999cd Xcwd 4000call assert_match ( '/Xcwd$' , getcwd ()) 4001call assert_match ( '\[global\] .*/Xcwd$' , trim ( execute ( 'verbose pwd' ))) 4002 4003autocmd BufEnter Xb.txt lcd ./a/b/c/d 4004doautoall BufEnter 4005au ! BufEnter 4006call assert_match ( '/Xcwd$' , getcwd ()) 4007call assert_match ( '\[global\] .*/Xcwd$' , trim ( execute ( 'verbose pwd' ))) 4008 4009tabnext 4010cd ./a 4011tcd ./b 4012lcd ./c 4013call assert_match ( '/Xcwd/a/b/c$' , getcwd ()) 4014call assert_match ( '\[window\] .*/Xcwd/a/b/c$' , trim ( execute ( 'verbose pwd' ))) 4015 4016autocmd BufEnter Xa.txt call assert_match ( 'Xcwd/a/b/c$' , getcwd ()) 4017doautoall BufEnter 4018au ! BufEnter 4019call assert_match ( '/Xcwd/a/b/c$' , getcwd ()) 4020call assert_match ( '\[window\] .*/Xcwd/a/b/c$' , trim ( execute ( 'verbose pwd' ))) 4021bwipe ! 4022call assert_match ( '/Xcwd/a/b$' , getcwd ()) 4023call assert_match ( '\[tabpage\] .*/Xcwd/a/b$' , trim ( execute ( 'verbose pwd' ))) 4024bwipe ! 4025call assert_match ( '/Xcwd/a$' , getcwd ()) 4026call assert_match ( '\[global\] .*/Xcwd/a$' , trim ( execute ( 'verbose pwd' ))) 4027bwipe ! 4028 4029call chdir ( saveddir ) 4030endfunc 4031 4032func Test_bufwipeout_changes_window () 4033" This should not crash, but we don't have any expectations about what 4034" happens, changing window in BufWipeout has unpredictable results. 4035tabedit 4036let g: window_id = win_getid () 4037topleft new 4038setlocal bufhidden = wipe 4039autocmd BufWipeout <buffer> call win_gotoid ( g: window_id ) 4040tabprevious 4041+ tabclose 4042 4043unlet g: window_id 4044au ! BufWipeout 4045% bwipe ! 4046endfunc 4047 4048func Test_autocmd_prevent_buf_wipe () 4049" Xa must be the first buffer so that win_close_othertab() puts it in 4050" another window, which causes wiping the buffer to fail. 4051% bwipe ! 4052 4053file Xa 4054call setline ( 1 , 'foo' ) 4055setlocal bufhidden = wipe 4056tabnew Xb 4057setlocal bufhidden = wipe 4058autocmd BufUnload Xa ++once ++nested tabonly 4059autocmd BufWinLeave Xb ++once tabnext 4060tabfirst 4061 4062edit ! Xc 4063call assert_equal ( 'Xc' , bufname ( '%' )) 4064tabnext 4065call assert_equal ( 'Xa' , bufname ( '%' )) 4066call assert_equal ( "\n\"Xa\" --No lines in buffer--" , execute ( 'file' )) 4067 4068% bwipe ! 4069endfunc 4070 4071func Test_v_event_readonly () 4072autocmd CompleteChanged * let v: event . width = 0 4073call assert_fails ( "normal! i\<C-X>\<C-V>" , 'E46:' ) 4074au ! CompleteChanged 4075 4076autocmd DirChangedPre * let v: event . directory = '' 4077call assert_fails ( 'cd .' , 'E46:' ) 4078au ! DirChangedPre 4079 4080autocmd ModeChanged * let v: event . new_mode = '' 4081call assert_fails ( 'normal! cc' , 'E46:' ) 4082au ! ModeChanged 4083 4084autocmd TextYankPost * let v: event . operator = '' 4085call assert_fails ( 'normal! yy' , 'E46:' ) 4086au ! TextYankPost 4087endfunc 4088 4089" Test for ModeChanged pattern 4090func Test_mode_changes () 4091let g: index = 0 4092let g: mode_seq = [ 'n' , 'i' , 'n' , 'v' , 'V' , 'i' , 'ix' , 'i' , 'ic' , 'i' , 'n' , 'no' , 'noV' , 'n' , 'V' , 'v' , 's' , 'n' ] 4093func ! TestMode () 4094call assert_equal ( g: mode_seq [ g: index ], get ( v: event , "old_mode" )) 4095call assert_equal ( g: mode_seq [ g: index + 1 ], get ( v: event , "new_mode" )) 4096call assert_equal ( mode ( 1 ), get ( v: event , "new_mode" )) 4097let g: index += 1 4098endfunc 4099 4100au ModeChanged * : call TestMode () 4101let g: n_to_any = 0 4102au ModeChanged n: * let g: n_to_any += 1 4103call feedkeys ( "i\<esc>vVca\<CR>\<C-X>\<C-L>\<esc>ggdV\<MouseMove>G" , 'tnix' ) 4104 4105let g: V_to_v = 0 4106au ModeChanged V:v let g: V_to_v += 1 4107call feedkeys ( "Vv\<C-G>\<esc>" , 'tnix' ) 4108call assert_equal ( len ( filter ( g: mode_seq [ 1 :], { idx , val -> val == 'n' })), g: n_to_any ) 4109call assert_equal ( 1 , g: V_to_v ) 4110call assert_equal ( len ( g: mode_seq ) - 1 , g: index ) 4111 4112let g: n_to_i = 0 4113au ModeChanged n:i let g: n_to_i += 1 4114let g: n_to_niI = 0 4115au ModeChanged i:niI let g: n_to_niI += 1 4116let g: niI_to_i = 0 4117au ModeChanged niI:i let g: niI_to_i += 1 4118let g: nany_to_i = 0 4119au ModeChanged n * :i let g: nany_to_i += 1 4120let g: i_to_n = 0 4121au ModeChanged i:n let g: i_to_n += 1 4122let g: nori_to_any = 0 4123au ModeChanged [ ni ] : * let g: nori_to_any += 1 4124let g: i_to_any = 0 4125au ModeChanged i: * let g: i_to_any += 1 4126let g: index = 0 4127let g: mode_seq = [ 'n' , 'i' , 'niI' , 'i' , 'n' ] 4128call feedkeys ( "a\<C-O>l\<esc>" , 'tnix' ) 4129call assert_equal ( len ( g: mode_seq ) - 1 , g: index ) 4130call assert_equal ( 1 , g: n_to_i ) 4131call assert_equal ( 1 , g: n_to_niI ) 4132call assert_equal ( 1 , g: niI_to_i ) 4133call assert_equal ( 2 , g: nany_to_i ) 4134call assert_equal ( 1 , g: i_to_n ) 4135call assert_equal ( 2 , g: i_to_any ) 4136call assert_equal ( 3 , g: nori_to_any ) 4137 4138if has ( 'terminal' ) 4139let g: mode_seq += [ 'c' , 'n' , 't' , 'nt' , 'c' , 'nt' , 'n' ] 4140call feedkeys ( ":term\<CR>\<C-W>N:bd!\<CR>" , 'tnix' ) 4141call assert_equal ( len ( g: mode_seq ) - 1 , g: index ) 4142call assert_equal ( 1 , g: n_to_i ) 4143call assert_equal ( 1 , g: n_to_niI ) 4144call assert_equal ( 1 , g: niI_to_i ) 4145call assert_equal ( 2 , g: nany_to_i ) 4146call assert_equal ( 1 , g: i_to_n ) 4147call assert_equal ( 2 , g: i_to_any ) 4148call assert_equal ( 5 , g: nori_to_any ) 4149endif 4150 4151let g: n_to_c = 0 4152au ModeChanged n:c let g: n_to_c += 1 4153let g: c_to_n = 0 4154au ModeChanged c:n let g: c_to_n += 1 4155" Nvim: Cannot test non-blocking cmdwin (#40312) using feedkeys() 'x' flag. 4156"let g:mode_seq += ['c', 'n', 'c', 'n'] 4157"call feedkeys("q:\<C-C>\<Esc>", 'tnix') 4158"call assert_equal(len(g:mode_seq) - 1, g:index) 4159"call assert_equal(2, g:n_to_c) 4160"call assert_equal(2, g:c_to_n) 4161 4162let g: n_to_v = 0 4163au ModeChanged n:v let g: n_to_v += 1 4164let g: v_to_n = 0 4165au ModeChanged v:n let g: v_to_n += 1 4166let g: mode_seq += [ 'v' , 'n' ] 4167call feedkeys ( "v\<C-C>" , 'tnix' ) 4168call assert_equal ( len ( g: mode_seq ) - 1 , g: index ) 4169call assert_equal ( 1 , g: n_to_v ) 4170call assert_equal ( 1 , g: v_to_n ) 4171 4172let g: mode_seq += [ 'c' , 'cr' , 'c' , 'cr' , 'n' ] 4173call feedkeys ( ":\<Insert>\<Insert>\<Insert>\<CR>" , 'tnix' ) 4174call assert_equal ( len ( g: mode_seq ) - 1 , g: index ) 4175 4176au ! ModeChanged 4177delfunc TestMode 4178unlet ! g: mode_seq 4179unlet ! g: index 4180unlet ! g: n_to_any 4181unlet ! g: V_to_v 4182unlet ! g: n_to_i 4183unlet ! g: n_to_niI 4184unlet ! g: niI_to_i 4185unlet ! g: nany_to_i 4186unlet ! g: i_to_n 4187unlet ! g: nori_to_any 4188unlet ! g: i_to_any 4189unlet ! g: n_to_c 4190unlet ! g: c_to_n 4191unlet ! g: n_to_v 4192unlet ! g: v_to_n 4193endfunc 4194 4195func Test_recursive_ModeChanged () 4196au ! ModeChanged * norm 0u 4197sil ! norm 4198 au ! ModeChanged 4199endfunc 4200 4201func Test_ModeChanged_starts_visual () 4202" This was triggering ModeChanged before setting VIsual, causing a crash. 4203au ! ModeChanged * norm 0u 4204sil ! norm 4205 4206 au ! ModeChanged 4207endfunc 4208 4209func Test_noname_autocmd () 4210augroup test_noname_autocmd_group 4211autocmd ! 4212autocmd BufEnter * call add ( s: li , [ "BufEnter" , expand ( "<afile>" )]) 4213autocmd BufDelete * call add ( s: li , [ "BufDelete" , expand ( "<afile>" )]) 4214autocmd BufLeave * call add ( s: li , [ "BufLeave" , expand ( "<afile>" )]) 4215autocmd BufUnload * call add ( s: li , [ "BufUnload" , expand ( "<afile>" )]) 4216autocmd BufWipeout * call add ( s: li , [ "BufWipeout" , expand ( "<afile>" )]) 4217augroup END 4218 4219let s: li = [] 4220edit foo 4221call assert_equal ([[ 'BufUnload' , '' ], [ 'BufDelete' , '' ], [ 'BufWipeout' , '' ], [ 'BufEnter' , 'foo' ]], s: li ) 4222 4223au ! test_noname_autocmd_group 4224augroup ! test_noname_autocmd_group 4225endfunc 4226 4227func Test_autocmd_split_dummy () 4228" Autocommand trying to split a window containing a dummy buffer. 4229auto BufReadPre * exe "sbuf " .. expand ( "<abuf>" ) 4230" Avoid the "W11" prompt 4231au FileChangedShell * let v: fcs_choice = 'reload' 4232func Xautocmd_changelist () 4233cal writefile ([ 'Xtestfile2:4:4' ], 'Xerr' ) 4234edit Xerr 4235lex 'Xtestfile2:4:4' 4236endfunc 4237call Xautocmd_changelist () 4238" Should get E86, but it doesn't always happen (timing?) 4239silent ! call Xautocmd_changelist () 4240 4241au ! BufReadPre 4242au ! FileChangedShell 4243delfunc Xautocmd_changelist 4244bwipe ! Xerr 4245call delete ( 'Xerr' ) 4246endfunc 4247 4248" This was crashing because there was only one window to execute autocommands 4249" in. 4250func Test_autocmd_nested_setbufvar () 4251CheckFeature python3 4252 4253set hidden 4254edit Xaaa 4255edit Xbbb 4256call setline ( 1 , 'bar' ) 4257enew 4258au BufWriteCmd Xbbb ++nested call setbufvar ( 'Xaaa' , '&ft' , 'foo' ) | bw ! Xaaa 4259au FileType foo call py3eval ( 'vim.current.buffer.options["cindent"]' ) 4260wall 4261 4262au ! BufWriteCmd 4263au ! FileType foo 4264set nohidden 4265call delete ( 'Xaaa' ) 4266call delete ( 'Xbbb' ) 4267% bwipe ! 4268endfunc 4269 4270func SetupVimTest_shm () 4271let g: bwe = [] 4272let g: brp = [] 4273set shortmess -= l 4274messages clear 4275 4276let dirname = 'XVimTestSHM' 4277call mkdir ( dirname , 'R' ) 4278call writefile ([ 'test' ], dirname .. '/1' ) 4279call writefile ([ 'test' ], dirname .. '/2' ) 4280call writefile ([ 'test' ], dirname .. '/3' ) 4281 4282augroup test 4283autocmd ! 4284autocmd BufWinEnter * call add ( g: bwe , $ ' BufWinEnter: {expand('<amatch>')}') 4285autocmd BufReadPost * call add ( g: brp , $ ' BufReadPost: {expand('<amatch>')}') 4286augroup END 4287 4288call setqflist ([ 4289\ { 'filename' : dirname .. '/1' , 'lnum' : 1 , 'col' : 1 , 'text' : 'test' , 'vcol' : 0 }, 4290\ { 'filename' : dirname .. '/2' , 'lnum' : 1 , 'col' : 1 , 'text' : 'test' , 'vcol' : 0 }, 4291\ { 'filename' : dirname .. '/3' , 'lnum' : 1 , 'col' : 1 , 'text' : 'test' , 'vcol' : 0 } 4292\ ]) 4293cdo ! substitute/test/TEST 4294 4295" clean up 4296noa enew! 4297set shortmess & vim 4298augroup test 4299autocmd ! 4300augroup END 4301augroup ! test 4302endfunc 4303 4304func Test_autocmd_shortmess () 4305CheckNotMSWindows 4306 4307call SetupVimTest_shm () 4308let output = execute ( ':mess' ) -> split ( '\n' ) 4309 4310let info = copy ( output ) -> filter ({ idx , val -> val =~# '\d of 3' } ) 4311let bytes = copy ( output ) -> filter ({ idx , val -> val =~# 'bytes' } ) 4312 4313" We test the following here: 4314" BufReadPost should have been triggered 3 times, once per file 4315" BufWinEnter should have been triggered 3 times, once per file 4316" FileInfoMessage should have been shown 3 times, regardless of shm option 4317" "(x of 3)" message from :cnext has been shown 3 times 4318 4319call assert_equal ( 3 , g: brp -> len ()) 4320call assert_equal ( 3 , g: bwe -> len ()) 4321call assert_equal ( 3 , info -> len ()) 4322call assert_equal ( 3 , bytes -> len ()) 4323 4324delfunc SetupVimTest_shm 4325endfunc 4326 4327func Test_autocmd_invalidates_undo_on_textchanged () 4328CheckRunVimInTerminal 4329let script =<< trim END 4330set hidden 4331" create quickfix list (at least 2 lines to move line) 4332vimgrep /u/j % 4333 4334" enter quickfix window 4335cwindow 4336 4337" set modifiable 4338setlocal modifiable 4339 4340" set autocmd to clear quickfix list 4341 4342autocmd! TextChanged <buffer> call setqflist([]) 4343" move line 4344move+1 4345END 4346call writefile ( script , 'XTest_autocmd_invalidates_undo_on_textchanged' , 'D' ) 4347let buf = RunVimInTerminal ( 'XTest_autocmd_invalidates_undo_on_textchanged' , { 'rows' : 20 }) 4348call term_sendkeys ( buf , ":so %\<cr>" ) 4349call term_sendkeys ( buf , "G" ) 4350call WaitForAssert ({ -> assert_match ( '^XTest_autocmd_invalidates_undo_on_textchanged\s*$' , term_getline ( buf , 20 ))}, 1000 ) 4351 4352call StopVimInTerminal ( buf ) 4353endfunc 4354 4355func Test_autocmd_creates_new_window_on_bufleave () 4356e a.txt 4357e b.txt 4358setlocal bufhidden = wipe 4359autocmd BufLeave <buffer> diffsplit c.txt 4360bn 4361" curbuf set for the new split opened for c.txt, due to BufLeave 4362call assert_equal ( 2 , winnr ( '$' )) 4363call assert_equal ( 'a.txt' , bufname ( '%' )) 4364call assert_equal ( 'b.txt' , bufname ( '#' )) 4365% bw ! 4366endfunc 4367 4368" Ensure `expected` was just recently written as a Vim session 4369func s: assert_session_path ( expected ) 4370call assert_equal ( a: expected , v: this_session ) 4371endfunc 4372 4373" Check for `expected` after a session is written to-disk. 4374func s: watch_for_session_path ( expected ) 4375execute 'autocmd SessionWritePost * ++once execute "call s:assert_session_path(\"' 4376\ . a: expected 4377\ . '\")"' 4378endfunc 4379 4380" Ensure v:this_session gets the full session path, if explicitly stated 4381func Test_explicit_session_absolute_path () 4382% bwipeout ! 4383 4384let directory = getcwd () 4385 4386let v: this_session = "" 4387let name = "some_file.vim" 4388let expected = fnamemodify ( name , ":p" ) 4389call s: watch_for_session_path ( expected ) 4390execute "mksession! " .. expected 4391 4392call delete ( expected ) 4393endfunc 4394 4395" Ensure v:this_session gets the full session path, if explicitly stated 4396func Test_explicit_session_relative_path () 4397% bwipeout ! 4398 4399let directory = getcwd () 4400 4401let v: this_session = "" 4402let name = "some_file.vim" 4403let expected = fnamemodify ( name , ":p" ) 4404call s: watch_for_session_path ( expected ) 4405execute "mksession! " .. name 4406 4407call delete ( expected ) 4408endfunc 4409 4410" Ensure v:this_session gets the full session path, if not specified 4411func Test_implicit_session () 4412% bwipeout ! 4413 4414let directory = getcwd () 4415 4416let v: this_session = "" 4417let expected = fnamemodify ( "Session.vim" , ":p" ) 4418call s: watch_for_session_path ( expected ) 4419mksession ! 4420 4421call delete ( expected ) 4422endfunc 4423 4424" Test TextChangedI and TextChanged 4425func Test_Changed_ChangedI () 4426" Run this test in a terminal because it requires running the main loop. 4427" Don't use CheckRunVimInTerminal as that will skip the test on Windows. 4428CheckFeature terminal 4429CheckNotGui 4430" Starting a terminal to run Vim is always considered flaky. 4431let g: test_is_flaky = 1 4432 4433call writefile ([ 'one' , 'two' , 'three' ], 'XTextChangedI2' , 'D' ) 4434let before =<< trim END 4435set ttimeout ttimeoutlen=10 4436let [g:autocmd_n, g:autocmd_i] = ['',''] 4437 4438func TextChangedAutocmd(char) 4439let g:autocmd_{tolower(a:char)} = a:char .. b:changedtick 4440call writefile([$'{g:autocmd_n},{g:autocmd_i}'], 'XTextChangedI3') 4441endfunc 4442 4443au TextChanged <buffer> :call TextChangedAutocmd('N') 4444au TextChangedI <buffer> :call TextChangedAutocmd('I') 4445 4446nnoremap <CR> o<Esc> 4447autocmd SafeState * ++once call writefile([''], 'XTextChangedI3') 4448END 4449 4450call writefile ( before , 'Xinit' , 'D' ) 4451let buf = term_start ( 4452\ GetVimCommandCleanTerm () .. '-n -S Xinit XTextChangedI2' , 4453\ { 'term_rows' : 10 }) 4454call assert_equal ( 'running' , term_getstatus ( buf )) 4455call WaitForAssert ({ -> assert_true ( filereadable ( 'XTextChangedI3' ))}) 4456defer delete('XTextChangedI3') 4457call WaitForAssert ({ -> assert_equal ([ '' ], readfile ( 'XTextChangedI3' ))}) 4458 4459" TextChanged should trigger if a mapping enters and leaves Insert mode. 4460call term_sendkeys ( buf , "\<CR>" ) 4461call WaitForAssert ({ -> assert_equal ( 'N4,' , readfile ( 'XTextChangedI3' ) -> join ( "\n" ))}) 4462 4463call term_sendkeys ( buf , "i" ) 4464call WaitForAssert ({ -> assert_match ( '^-- INSERT --' , term_getline ( buf , 10 ))}) 4465call WaitForAssert ({ -> assert_equal ( 'N4,' , readfile ( 'XTextChangedI3' ) -> join ( "\n" ))}) 4466" TextChangedI should trigger if change is done in Insert mode. 4467call term_sendkeys ( buf , "f" ) 4468call WaitForAssert ({ -> assert_equal ( 'N4,I5' , readfile ( 'XTextChangedI3' ) -> join ( "\n" ))}) 4469call term_sendkeys ( buf , "o" ) 4470call WaitForAssert ({ -> assert_equal ( 'N4,I6' , readfile ( 'XTextChangedI3' ) -> join ( "\n" ))}) 4471call term_sendkeys ( buf , "o" ) 4472call WaitForAssert ({ -> assert_equal ( 'N4,I7' , readfile ( 'XTextChangedI3' ) -> join ( "\n" ))}) 4473" TextChanged shouldn't trigger when leaving Insert mode and TextChangedI 4474" has been triggered. 4475call term_sendkeys ( buf , "\<Esc>" ) 4476call WaitForAssert ({ -> assert_notmatch ( '^-- INSERT --' , term_getline ( buf , 10 ))}) 4477call WaitForAssert ({ -> assert_equal ( 'N4,I7' , readfile ( 'XTextChangedI3' ) -> join ( "\n" ))}) 4478 4479" TextChanged should trigger if change is done in Normal mode. 4480call term_sendkeys ( buf , "yyp" ) 4481call WaitForAssert ({ -> assert_equal ( 'N8,I7' , readfile ( 'XTextChangedI3' ) -> join ( "\n" ))}) 4482 4483" TextChangedI shouldn't trigger if change isn't done in Insert mode. 4484call term_sendkeys ( buf , "i" ) 4485call WaitForAssert ({ -> assert_match ( '^-- INSERT --' , term_getline ( buf , 10 ))}) 4486call WaitForAssert ({ -> assert_equal ( 'N8,I7' , readfile ( 'XTextChangedI3' ) -> join ( "\n" ))}) 4487call term_sendkeys ( buf , "\<Esc>" ) 4488call WaitForAssert ({ -> assert_notmatch ( '^-- INSERT --' , term_getline ( buf , 10 ))}) 4489call WaitForAssert ({ -> assert_equal ( 'N8,I7' , readfile ( 'XTextChangedI3' ) -> join ( "\n" ))}) 4490 4491" TextChangedI should trigger if change is a mix of Normal and Insert modes. 4492func ! s: validate_mixed_textchangedi ( buf , keys ) 4493let buf = a: buf 4494call term_sendkeys ( buf , "ifoo" ) 4495call WaitForAssert ({ -> assert_match ( '^-- INSERT --' , term_getline ( buf , 10 ))}) 4496call term_sendkeys ( buf , "\<Esc>" ) 4497call WaitForAssert ({ -> assert_notmatch ( '^-- INSERT --' , term_getline ( buf , 10 ))}) 4498call term_sendkeys ( buf , ":let [g:autocmd_n, g:autocmd_i] = ['', '']\<CR>" ) 4499call writefile ([], 'XTextChangedI3' ) 4500call term_sendkeys ( buf , a: keys ) 4501call WaitForAssert ({ -> assert_match ( '^-- INSERT --' , term_getline ( buf , 10 ))}) 4502call WaitForAssert ({ -> assert_match ( '^,I\d\+' , readfile ( 'XTextChangedI3' ) -> join ( "\n" ))}) 4503call term_sendkeys ( buf , "\<Esc>" ) 4504call WaitForAssert ({ -> assert_notmatch ( '^-- INSERT --' , term_getline ( buf , 10 ))}) 4505call WaitForAssert ({ -> assert_match ( '^,I\d\+' , readfile ( 'XTextChangedI3' ) -> join ( "\n" ))}) 4506endfunc 4507 4508call s: validate_mixed_textchangedi ( buf , "o" ) 4509call s: validate_mixed_textchangedi ( buf , "O" ) 4510call s: validate_mixed_textchangedi ( buf , "ciw" ) 4511call s: validate_mixed_textchangedi ( buf , "cc" ) 4512call s: validate_mixed_textchangedi ( buf , "C" ) 4513call s: validate_mixed_textchangedi ( buf , "s" ) 4514call s: validate_mixed_textchangedi ( buf , "S" ) 4515 4516" clean up 4517bwipe ! 4518endfunc 4519 4520" Test that filetype detection still works when SwapExists autocommand sets 4521" filetype in another buffer. 4522func Test_SwapExists_set_other_buf_filetype () 4523let lines =<< trim END 4524set nocompatible directory=. 4525filetype on 4526 4527let g:buf = bufnr() 4528new 4529 4530func SwapExists() 4531let v:swapchoice = 'o' 4532call setbufvar(g:buf, '&filetype', 'text') 4533endfunc 4534 4535func SafeState() 4536edit <script> 4537redir! > XftSwapExists.out 4538set readonly? filetype? 4539redir END 4540qall! 4541endfunc 4542 4543autocmd SwapExists * ++nested call SwapExists() 4544autocmd SafeState * ++nested ++once call SafeState() 4545END 4546call writefile ( lines , 'XftSwapExists.vim' , 'D' ) 4547 4548new XftSwapExists.vim 4549if RunVim ( '' , '' , ' -S XftSwapExists.vim' ) 4550call assert_equal ( 4551\ [ '' , ' readonly' , ' filetype=vim' ], 4552\ readfile ( 'XftSwapExists.out' )) 4553call delete ( 'XftSwapExists.out' ) 4554endif 4555 4556bwipe ! 4557endfunc 4558 4559" Test that file is not marked as modified when SwapExists autocommand sets 4560" 'modified' in another buffer. 4561func Test_SwapExists_set_other_buf_modified () 4562let lines =<< trim END 4563set nocompatible directory=. 4564 4565let g:buf = bufnr() 4566new 4567 4568func SwapExists() 4569let v:swapchoice = 'o' 4570call setbufvar(g:buf, '&modified', 1) 4571endfunc 4572 4573func SafeState() 4574edit <script> 4575redir! > XmodSwapExists.out 4576set readonly? modified? 4577redir END 4578qall! 4579endfunc 4580 4581autocmd SwapExists * ++nested call SwapExists() 4582autocmd SafeState * ++nested ++once call SafeState() 4583END 4584call writefile ( lines , 'XmodSwapExists.vim' , 'D' ) 4585 4586new XmodSwapExists.vim 4587if RunVim ( '' , '' , ' -S XmodSwapExists.vim' ) 4588call assert_equal ( 4589\ [ '' , ' readonly' , 'nomodified' ], 4590\ readfile ( 'XmodSwapExists.out' )) 4591call delete ( 'XmodSwapExists.out' ) 4592endif 4593 4594bwipe ! 4595endfunc 4596 4597func Test_BufEnter_botline () 4598set hidden 4599call writefile ( range ( 10 ), 'Xxx1' , 'D' ) 4600call writefile ( range ( 20 ), 'Xxx2' , 'D' ) 4601edit Xxx1 4602edit Xxx2 4603au BufEnter Xxx1 call assert_true ( line ( 'w$' ) > 1 ) 4604edit Xxx1 4605 4606bwipe ! Xxx1 4607bwipe ! Xxx2 4608au ! BufEnter Xxx1 4609set hidden & vim 4610endfunc 4611 4612" those commands caused null pointer access, see #15464 4613func Test_WinNewPre_crash () 4614defer CleanUpTestAuGroup() 4615let _cmdheight =& cmdheight 4616augroup testing 4617au ! 4618autocmd WinNew Pre * redraw 4619augroup END 4620tabnew 4621tabclose 4622augroup testing 4623au ! 4624autocmd WinNew Pre * wincmd t 4625augroup END 4626tabnew 4627tabclose 4628augroup testing 4629au ! 4630autocmd WinNew Pre * wincmd b 4631augroup END 4632tabnew 4633tabclose 4634augroup testing 4635au ! 4636autocmd WinNew Pre * set cmdheight+= 1 4637augroup END 4638tabnew 4639tabclose 4640let & cmdheight = _cmdheight 4641endfunc 4642 4643 4644" This was using freed memory 4645func Test_autocmd_BufWinLeave_with_vsp () 4646new 4647let fname = 'XXXBufWinLeaveUAF.txt' 4648let dummy = 'XXXDummy.txt' 4649call writefile ([], fname ) 4650call writefile ([], dummy ) 4651defer delete(fname) 4652defer delete(dummy) 4653exe "e " fname 4654vsp 4655augroup testing 4656exe 'au BufWinLeave' fname 'e' dummy 4657\ '| call assert_fails(''vsp' fname ''', ''E1546:'')' 4658augroup END 4659bw 4660call CleanUpTestAuGroup () 4661exe "bw! " .. dummy 4662endfunc 4663 4664func Test_autocmd_BufWinLeave_with_vsp2 () 4665edit Xfoo 4666split Xbar 4667split 4668let s: fired = 0 4669augroup testing 4670autocmd ! 4671autocmd BufWinLeave Xfoo ++once ++nested 4672\ execute 'autocmd WinEnter * ++once let s:fired = 1' 4673\ .. '| call assert_equal(3, win_findbuf(bufnr(''Xbar''))->len())' 4674\ .. '| quit' 4675\| call assert_fails ( 'vsplit Xfoo' , 'E1546:' ) 4676augroup END 4677bw Xfoo 4678call assert_equal ( 1 , s: fired ) 4679" After 9.1.0764, Xbar's b_nwindows would be 0 if autocmds closed the new 4680" split before E1546, causing it to be unloaded despite being in a window. 4681call assert_equal ( 0 , bufexists ( 'Xfoo' )) 4682call assert_equal ( 1 , win_findbuf ( bufnr ( 'Xbar' )) -> len ()) 4683call assert_equal ( 1 , bufloaded ( 'Xbar' )) 4684 4685call CleanUpTestAuGroup () 4686unlet ! s: fired 4687% bw ! 4688endfunc 4689 4690func Test_OptionSet_cmdheight () 4691set mouse = a laststatus = 2 4692au OptionSet cmdheight : let & l: ch = v: option_new 4693 4694resize -1 4695call assert_equal ( 2 , & l: ch ) 4696resize +1 4697call assert_equal ( 1 , & l: ch ) 4698 4699call Ntest_setmouse ( & lines - 1 , 1 ) 4700call feedkeys ( "\<LeftMouse>" , 'xt' ) 4701call Ntest_setmouse ( & lines - 2 , 1 ) 4702call feedkeys ( "\<LeftDrag>" , 'xt' ) 4703call assert_equal ( 2 , & l: ch ) 4704call feedkeys ( "\<LeftRelease>" , 'xt' ) 4705 4706tabnew | resize +1 4707call assert_equal ( 1 , & l: ch ) 4708tabfirst 4709call assert_equal ( 2 , & l: ch ) 4710 4711tabonly 4712set cmdheight & mouse & laststatus & 4713endfunc 4714 4715func Test_eventignorewin () 4716defer CleanUpTestAuGroup() 4717augroup testing 4718au WinEnter * : call add ( g: evs , [ "WinEnter" , expand ( "<afile>" )]) 4719au WinLeave * : call add ( g: evs , [ "WinLeave" , expand ( "<afile>" )]) 4720au BufWinEnter * : call add ( g: evs , [ "BufWinEnter" , expand ( "<afile>" )]) 4721augroup END 4722 4723let g: evs = [] 4724set eventignorewin = WinLeave,WinEnter 4725split foo 4726call assert_equal ([[ 'BufWinEnter' , 'foo' ]], g: evs ) 4727set eventignorewin = all 4728edit bar 4729call assert_equal ([[ 'BufWinEnter' , 'foo' ]], g: evs ) 4730set eventignorewin = 4731wincmd w 4732call assert_equal ([[ 'BufWinEnter' , 'foo' ], [ 'WinLeave' , 'bar' ]], g: evs ) 4733 4734only ! 4735% bwipe ! 4736set eventignorewin & 4737unlet g: evs 4738endfunc 4739 4740func Test_WinScrolled_Resized_eiw () 4741CheckRunVimInTerminal 4742 4743let lines =<< trim END 4744call setline(1, ['foo']->repeat(32)) 4745set eventignorewin=WinScrolled,WinResized 4746split 4747let [g:afile,g:resized,g:scrolled] = ['none',0,0] 4748au WinScrolled * let [g:afile,g:scrolled] = [expand('<afile>'),g:scrolled+1] 4749au WinResized * let [g:afile,g:resized] = [expand('<afile>'),g:resized+1] 4750END 4751call writefile ( lines , 'Xtest_winscrolled_eiw' , 'D' ) 4752let buf = RunVimInTerminal ( '-S Xtest_winscrolled_eiw' , { 'rows' : 10 }) 4753 4754" Both windows are ignoring resize events 4755call term_sendkeys ( buf , "\<C-W>-" ) 4756call TermWait ( buf ) 4757call term_sendkeys ( buf , ":echo g:afile g:resized g:scrolled\<CR>" ) 4758call WaitForAssert ({ -> assert_equal ( 'none 0 0' , term_getline ( buf , 10 ))}, 1000 ) 4759 4760" And scroll events 4761call term_sendkeys ( buf , "Ggg" ) 4762call TermWait ( buf ) 4763call term_sendkeys ( buf , ":echo g:afile g:resized g:scrolled\<CR>" ) 4764call WaitForAssert ({ -> assert_equal ( 'none 0 0' , term_getline ( buf , 10 ))}, 1000 ) 4765 4766" Un-ignore events in second window, make first window current and resize 4767call term_sendkeys ( buf , ":set eventignorewin=\<CR>\<C-W>w\<C-W>+" ) 4768call TermWait ( buf ) 4769call term_sendkeys ( buf , ":echo win_getid() g:afile g:resized g:scrolled\<CR>" ) 4770call WaitForAssert ({ -> assert_equal ( '1000 1001 1 1' , term_getline ( buf , 10 ))}, 1000 ) 4771 4772call StopVimInTerminal ( buf ) 4773endfunc 4774 4775" Test that TabClosedPre and TabClosed are triggered when closing a tab. 4776func Test_autocmd_TabClosedPre () 4777augroup testing 4778au TabClosed Pre * call add( g: tabpagenr_pre, t: testvar) 4779au TabClosed * call add ( g: tabpagenr_post , t: testvar ) 4780augroup END 4781 4782" Test 'tabclose' triggering 4783let g: tabpagenr_pre = [] 4784let g: tabpagenr_post = [] 4785let t: testvar = 1 4786tabnew 4787let t: testvar = 2 4788tabnew 4789let t: testvar = 3 4790tabnew 4791let t: testvar = 4 4792tabnext 4793tabclose 4794tabclose 4795tabclose 4796call assert_equal ([ 1 , 2 , 3 ], g: tabpagenr_pre ) 4797call assert_equal ([ 2 , 3 , 4 ], g: tabpagenr_post ) 4798 4799" Test 'tabclose {count}' triggering 4800let g: tabpagenr_pre = [] 4801let g: tabpagenr_post = [] 4802let t: testvar = 1 4803tabnew 4804let t: testvar = 2 4805tabnew 4806let t: testvar = 3 4807tabclose 2 4808tabclose 2 4809call assert_equal ([ 2 , 3 ], g: tabpagenr_pre ) 4810call assert_equal ([ 3 , 1 ], g: tabpagenr_post ) 4811 4812" Test 'tabonly' triggering 4813let g: tabpagenr_pre = [] 4814let g: tabpagenr_post = [] 4815let t: testvar = 1 4816tabnew 4817let t: testvar = 2 4818tabonly 4819call assert_equal ([ 1 ], g: tabpagenr_pre ) 4820call assert_equal ([ 2 ], g: tabpagenr_post ) 4821 4822" Test 'q' and 'close' triggering (closing the last window in a tab) 4823let g: tabpagenr_pre = [] 4824let g: tabpagenr_post = [] 4825split 4826let t: testvar = 1 4827tabnew 4828let t: testvar = 2 4829split 4830vsplit 4831tabnew 4832let t: testvar = 3 4833tabnext 4834only 4835quit 4836quit 4837close 4838close 4839call assert_equal ([ 1 , 2 ], g: tabpagenr_pre ) 4840call assert_equal ([ 2 , 3 ], g: tabpagenr_post ) 4841 4842" Test failing to close tab page 4843let g: tabpagenr_pre = [] 4844let g: tabpagenr_post = [] 4845let t: testvar = 1 4846call setline ( 1 , 'foo' ) 4847setlocal bufhidden = wipe 4848tabnew 4849let t: testvar = 2 4850tabnew 4851let t: testvar = 3 4852call setline ( 1 , 'bar' ) 4853setlocal bufhidden = wipe 4854tabnew 4855let t: testvar = 4 4856call setline ( 1 , 'baz' ) 4857setlocal bufhidden = wipe 4858new 4859call assert_fails ( 'tabclose' , 'E445:' ) 4860call assert_equal ([ 4 ], g: tabpagenr_pre ) 4861call assert_equal ([], g: tabpagenr_post ) 4862" :tabclose! after failed :tabclose should trigger TabClosedPre again. 4863tabclose ! 4864call assert_equal ([ 4 , 4 ], g: tabpagenr_pre ) 4865call assert_equal ([ 3 ], g: tabpagenr_post ) 4866call assert_fails ( 'tabclose' , 'E37:' ) 4867call assert_equal ([ 4 , 4 , 3 ], g: tabpagenr_pre ) 4868call assert_equal ([ 3 ], g: tabpagenr_post ) 4869" The same for :close! if the tab page only has one window. 4870close ! 4871call assert_equal ([ 4 , 4 , 3 , 3 ], g: tabpagenr_pre ) 4872call assert_equal ([ 3 , 2 ], g: tabpagenr_post ) 4873" Also test with :close! after failed :tabonly. 4874call assert_fails ( 'tabonly' , 'E37:' ) 4875call assert_equal ([ 4 , 4 , 3 , 3 , 1 ], g: tabpagenr_pre ) 4876call assert_equal ([ 3 , 2 ], g: tabpagenr_post ) 4877tabprevious | close ! 4878call assert_equal ([ 4 , 4 , 3 , 3 , 1 , 1 ], g: tabpagenr_pre ) 4879call assert_equal ([ 3 , 2 , 2 ], g: tabpagenr_post ) 4880% bwipe ! 4881 4882" Test closing another tab page in BufWinLeave 4883let g: tabpagenr_pre = [] 4884let g: tabpagenr_post = [] 4885split 4886let t: testvar = 1 4887tabnew 4888let t: testvar = 2 4889tabnew Xsomebuf 4890let t: testvar = 3 4891new 4892autocmd BufWinLeave Xsomebuf ++once ++nested tabclose 1 4893tabclose 4894" TabClosedPre should not be triggered for tab page 3 twice. 4895call assert_equal ([ 3 , 1 ], g: tabpagenr_pre ) 4896" When tab page 1 was closed, tab page 3 was still the current tab page. 4897call assert_equal ([ 3 , 2 ], g: tabpagenr_post ) 4898% bwipe ! 4899 4900func ClearAutocmdAndCreateTabs () 4901au ! TabClosed Pre 4902bw ! 4903e Z 4904tabonly 4905tabnew A 4906tabnew B 4907tabnew C 4908endfunc 4909 4910func GetTabs () 4911redir => tabsout 4912tabs 4913redir END 4914let tabsout = substitute ( tabsout , '\n' , '' , 'g' ) 4915let tabsout = substitute ( tabsout , 'Tab page ' , '' , 'g' ) 4916let tabsout = substitute ( tabsout , '#' , '' , 'g' ) " Nvim: remove '#' 4917let tabsout = substitute ( tabsout , ' ' , '' , 'g' ) 4918return tabsout 4919endfunc 4920 4921call CleanUpTestAuGroup () 4922 4923" Close tab in TabClosedPre autocmd 4924call ClearAutocmdAndCreateTabs () 4925au TabClosed Pre * tabclose 4926call assert_fails ( 'tabclose' , 'E1312:' ) 4927call ClearAutocmdAndCreateTabs () 4928au TabClosed Pre * tabclose 4929call assert_fails ( 'tabclose 2' , 'E1312:' ) 4930call ClearAutocmdAndCreateTabs () 4931au TabClosed Pre * tabclose 1 4932call assert_fails ( 'tabclose' , 'E1312:' ) 4933 4934" Close other (all) tabs in TabClosedPre autocmd 4935call ClearAutocmdAndCreateTabs () 4936au TabClosed Pre * tabonly 4937call assert_fails ( 'tabclose' , 'E1312:' ) 4938call ClearAutocmdAndCreateTabs () 4939au TabClosed Pre * tabonly 4940call assert_fails ( 'tabclose 2' , 'E1312:' ) 4941call ClearAutocmdAndCreateTabs () 4942au TabClosed Pre * tabclose 4 4943call assert_fails ( 'tabclose 2' , 'E1312:' ) 4944 4945" Open new tabs in TabClosedPre autocmd 4946call ClearAutocmdAndCreateTabs () 4947au TabClosed Pre * tabnew D 4948call assert_fails ( 'tabclose' , 'E1312:' ) 4949call ClearAutocmdAndCreateTabs () 4950au TabClosed Pre * tabnew D 4951call assert_fails ( 'tabclose 1' , 'E1312:' ) 4952 4953" Moving the tab page in TabClosedPre autocmd 4954call ClearAutocmdAndCreateTabs () 4955au TabClosed Pre * tabmove 0 4956tabclose 4957call assert_equal ( '1>Z2A3B' , GetTabs ()) 4958call ClearAutocmdAndCreateTabs () 4959au TabClosed Pre * tabmove 0 4960tabclose 1 4961call assert_equal ( '1A2B3>C' , GetTabs ()) 4962tabonly 4963call assert_equal ( '1>C' , GetTabs ()) 4964 4965" Switching tab page in TabClosedPre autocmd 4966call ClearAutocmdAndCreateTabs () 4967au TabClosed Pre * tabnext | e Y 4968tabclose 4969call assert_equal ( '1Y2A3>B' , GetTabs ()) 4970call ClearAutocmdAndCreateTabs () 4971au TabClosed Pre * tabnext | e Y 4972tabclose 1 4973call assert_equal ( '1Y2B3>C' , GetTabs ()) 4974tabonly 4975call assert_equal ( '1>Y' , GetTabs ()) 4976 4977" Create new windows in TabClosedPre autocmd 4978call ClearAutocmdAndCreateTabs () 4979au TabClosed Pre * split | e X| vsplit | e Y | split | e Z 4980call assert_fails ( 'tabclose' , 'E242:' ) 4981call ClearAutocmdAndCreateTabs () 4982au TabClosed Pre * new X | new Y | new Z 4983call assert_fails ( 'tabclose 1' , 'E242:' ) 4984 4985" Test directly closing the tab page with ':tabclose' 4986au ! 4987tabonly 4988bw ! 4989e Z 4990au TabClosed Pre * mksession! 4991tabnew A 4992sp 4993tabclose 4994source Session.vim 4995call assert_equal ( '1Z2>AA' , GetTabs ()) 4996 4997" Test directly closing the tab page with ':tabonly' 4998" Z is closed before A. Hence A overwrites the session. 4999au ! 5000tabonly 5001bw ! 5002e Z 5003au TabClosed Pre * mksession! 5004tabnew A 5005tabnew B 5006tabonly 5007source Session.vim 5008call assert_equal ( '1>A2B' , GetTabs ()) 5009 5010" Clean up 5011call delete ( 'Session.vim' ) 5012au ! 5013only 5014tabonly 5015bw ! 5016delfunc ClearAutocmdAndCreateTabs 5017delfunc GetTabs 5018endfunc 5019 5020" This used to cause heap-use-after-free. 5021func Run_test_TabClosedPre_wipe_buffer ( split_cmds ) 5022file Xa 5023exe a: split_cmds 5024autocmd TabClosed Pre * ++once tabnext | bwipe! Xa 5025" Closing window inside TabClosedPre is not allowed. 5026call assert_fails ( 'tabonly' , 'E1312:' ) 5027 5028% bwipe ! 5029endfunc 5030 5031func Test_TabClosedPre_wipe_buffer () 5032" Test with Xa only in other tab pages. 5033call Run_test_TabClosedPre_wipe_buffer ( 'split | tab split | tabnew Xb' ) 5034" Test with Xa in both current and other tab pages. 5035call Run_test_TabClosedPre_wipe_buffer ( 'split | tab split | new Xb' ) 5036endfunc 5037 5038func Test_TabClosedPre_mouse () 5039func MyTabline () 5040let cnt = tabpagenr ( '$' ) 5041return range ( 1 , cnt ) -> mapnew ({ _ , n -> $ '%{n} X|Close{n}|%X'})->join('') 5042endfunc 5043 5044let save_mouse = & mouse 5045if has ( 'gui' ) 5046set guioptions -= e 5047endif 5048set mouse = a tabline = %!MyTabline() 5049 5050func OpenTwoTabPages () 5051% bwipe ! 5052file Xa | split | split 5053let g: Xa_bufnr = bufnr () 5054tabnew Xb | split 5055let g: Xb_bufnr = bufnr () 5056redraw ! 5057call assert_match ( '^|Close1||Close2| *$' , Screenline ( 1 )) 5058call assert_equal ( 2 , tabpagenr ( '$' )) 5059endfunc 5060 5061autocmd ! TabClosed Pre 5062call OpenTwoTabPages () 5063let g: autocmd_bufnrs = [] 5064autocmd TabClosed Pre * let g: autocmd_bufnrs += [tabpagebuflist()] 5065call Ntest_setmouse ( 1 , 2 ) 5066call feedkeys ( "\<LeftMouse>\<LeftRelease>" , 'tx' ) 5067call assert_equal ( 1 , tabpagenr ( '$' )) 5068call assert_equal ([[ g: Xa_bufnr ] -> repeat ( 3 )], g: autocmd_bufnrs ) 5069call assert_equal ([ g: Xb_bufnr ] -> repeat ( 2 ), tabpagebuflist ()) 5070 5071call OpenTwoTabPages () 5072let g: autocmd_bufnrs = [] 5073autocmd TabClosed Pre * call feedkeys("\< LeftRelease>\<LeftMouse>", 'tx') 5074call Ntest_setmouse ( 1 , 2 ) 5075" Closing tab page inside TabClosedPre is not allowed. 5076call assert_fails ( 'call feedkeys("\<LeftMouse>", "tx")' , 'E1312:' ) 5077call feedkeys ( "\<LeftRelease>" , 'tx' ) 5078 5079autocmd ! TabClosed Pre 5080call OpenTwoTabPages () 5081let g: autocmd_bufnrs = [] 5082autocmd TabClosed Pre * let g: autocmd_bufnrs += [tabpagebuflist()] 5083call Ntest_setmouse ( 1 , 10 ) 5084call feedkeys ( "\<LeftMouse>\<LeftRelease>" , 'tx' ) 5085call assert_equal ( 1 , tabpagenr ( '$' )) 5086call assert_equal ([[ g: Xb_bufnr ] -> repeat ( 2 )], g: autocmd_bufnrs ) 5087call assert_equal ([ g: Xa_bufnr ] -> repeat ( 3 ), tabpagebuflist ()) 5088 5089call OpenTwoTabPages () 5090let g: autocmd_bufnrs = [] 5091autocmd TabClosed Pre * call feedkeys("\< LeftRelease>\<LeftMouse>", 'tx') 5092call Ntest_setmouse ( 1 , 10 ) 5093" Closing tab page inside TabClosedPre is not allowed. 5094call assert_fails ( 'call feedkeys("\<LeftMouse>", "tx")' , 'E1312:' ) 5095call feedkeys ( "\<LeftRelease>" , 'tx' ) 5096 5097autocmd ! TabClosed Pre 5098% bwipe ! 5099unlet g: Xa_bufnr g: Xb_bufnr g: autocmd_bufnrs 5100let & mouse = save_mouse 5101set tabline & guioptions & 5102delfunc MyTabline 5103delfunc OpenTwoTabPages 5104endfunc 5105 5106func Test_eventignorewin_non_current () 5107defer CleanUpTestAuGroup() 5108let s: triggered = '' 5109augroup testing 5110" Will set <abuf> to the buffer of the closing window. 5111autocmd WinClosed * let s: triggered = 'WinClosed' 5112augroup END 5113let initial_win = win_getid () 5114 5115new 5116let new_buf = bufnr () 5117" Only set for one of the windows into the new buffer. 5118setlocal eventignorewin = all 5119split 5120setlocal eventignorewin = 5121let close_winnr = winnr () 5122 5123" Return to the window where the buffer is non-current. WinClosed should 5124" trigger as not all windows into new_buf have 'eventignorewin' set for it. 5125call win_gotoid ( initial_win ) 5126call assert_notequal ( new_buf , bufnr ()) 5127execute close_winnr 'close' 5128call assert_equal ( 'WinClosed' , s: triggered ) 5129 5130wincmd w 5131call assert_equal ( new_buf , bufnr ()) 5132tab split 5133setlocal eventignorewin = 5134let close_winnr = win_getid () 5135 5136" Ensure that new_buf's window in the other tabpage with 'eventignorewin' 5137" unset allows WinClosed to run when new_buf is non-current. 5138call win_gotoid ( initial_win ) 5139call assert_notequal ( new_buf , bufnr ()) 5140let s: triggered = '' 5141only ! 5142call assert_equal ( 'WinClosed' , s: triggered ) 5143call assert_equal ( 1 , win_findbuf ( new_buf ) -> len ()) 5144 5145" Create an only window to new_buf with 'eventignorewin' set. 5146tabonly ! 5147execute new_buf 'sbuffer' 5148setlocal eventignorewin = all 5149wincmd p 5150call assert_equal ( 1 , win_findbuf ( new_buf ) -> len ()) 5151call assert_notequal ( new_buf , bufnr ()) 5152 5153" Closing a window unrelated to new_buf should not block WinClosed. 5154split 5155let s: triggered = '' 5156close 5157call assert_equal ( 'WinClosed' , s: triggered ) 5158call assert_equal ( 1 , win_findbuf ( new_buf ) -> len ()) 5159 5160" Check WinClosed is blocked when we close the only window to new_buf (that 5161" has 'eventignorewin' set) while new_buf is non-current. 5162call assert_notequal ( new_buf , bufnr ()) 5163let s: triggered = '' 5164only ! 5165call assert_equal ( '' , s: triggered ) 5166call assert_equal ( 0 , win_findbuf ( new_buf ) -> len ()) 5167 5168augroup testing 5169autocmd ! 5170autocmd BufNew * ++once let s: triggered = 'BufNew' 5171augroup END 5172 5173" Buffer not shown in a window, 'eventignorewin' should not block (and 5174" can't even be set for it anyway in this case). 5175badd foo 5176call assert_equal ( 'BufNew' , s: triggered ) 5177 5178unlet ! s: triggered 5179% bw ! 5180endfunc 5181 5182func Test_reuse_curbuf_leak () 5183new bar 5184let s: bar_buf = bufnr () 5185augroup testing 5186autocmd ! 5187autocmd BufDelete * ++once let s: triggered = 1 | execute s: bar_buf 'buffer' 5188augroup END 5189enew 5190let empty_buf = bufnr () 5191 5192" Old curbuf should be reused, firing BufDelete. As BufDelete changes curbuf, 5193" reusing the buffer would fail and leak the ffname. 5194edit foo 5195call assert_equal ( 1 , s: triggered ) 5196" Wasn't reused because the buffer changed, but buffer "foo" is still created. 5197call assert_equal ( 1 , bufexists ( empty_buf )) 5198call assert_notequal ( empty_buf , bufnr ()) 5199call assert_equal ( 'foo' , bufname ()) 5200call assert_equal ( 'bar' , bufname ( s: bar_buf )) 5201 5202unlet ! s: bar_buf s: triggered 5203call CleanUpTestAuGroup () 5204% bw ! 5205endfunc 5206 5207func Test_reuse_curbuf_switch () 5208edit asdf 5209let s: asdf_win = win_getid () 5210new 5211let other_buf = bufnr () 5212let other_win = win_getid () 5213augroup testing 5214autocmd ! 5215autocmd BufUnload * ++once let s: triggered = 1 5216\| call assert_fails ( 'split' , 'E1159:' ) 5217\| call win_gotoid ( s: asdf_win ) 5218augroup END 5219 5220" Check BufUnload changing curbuf does not cause buflist_new to create a new 5221" buffer while leaving "other_buf" unloaded in a window. 5222enew 5223call assert_equal ( 1 , s: triggered ) 5224call assert_equal ( other_buf , bufnr ()) 5225call assert_equal ( other_win , win_getid ()) 5226call assert_equal ( 1 , win_findbuf ( other_buf ) -> len ()) 5227call assert_equal ( 1 , bufloaded ( other_buf )) 5228 5229unlet ! s: asdf_win s: triggered 5230call CleanUpTestAuGroup () 5231% bw ! 5232endfunc 5233 5234func Test_eventignore_subtract () 5235set eventignore = all,-WinEnter 5236augroup testing 5237autocmd ! 5238autocmd WinEnter * ++once let s: triggered = 1 5239augroup END 5240 5241new 5242call assert_equal ( 1 , s: triggered ) 5243 5244set eventignore & 5245unlet ! s: triggered 5246call CleanUpTestAuGroup () 5247% bw ! 5248endfunc 5249 5250func Test_win_tabclose_autocmd () 5251 5252defer CleanUpTestAuGroup() 5253new 5254augroup testing 5255au WinClosed * wincmd p 5256augroup END 5257 5258tabnew 5259new 5260new 5261 5262call assert_equal ( 2 , tabpagenr ( '$' )) 5263try 5264tabclose 5265catch 5266" should not happen 5267call assert_report ( "closing tabpage failed" ) 5268endtry 5269call assert_equal ( 1 , tabpagenr ( '$' )) 5270bw ! 5271endfunc 5272 5273func Test_buffer_b_nwindows () 5274" In these cases, b_nwindows of the Xbars was 1 despite being in no windows. 5275" Would cause weird failures in other tests, as they would be un-deletable. 5276edit Xfoo1 5277augroup testing 5278autocmd ! 5279autocmd BufUnload * ++once edit Xbar1 5280augroup END 5281bdelete 5282call assert_equal ([], win_findbuf ( bufnr ( 'Xfoo1' ))) 5283call assert_equal ([], win_findbuf ( bufnr ( 'Xbar1' ))) 5284call assert_equal ( 1 , bufexists ( 'Xfoo1' )) 5285call assert_equal ( 1 , bufexists ( 'Xbar1' )) 5286% bw ! 5287call assert_equal ( 0 , bufexists ( 'Xfoo1' )) 5288call assert_equal ( 0 , bufexists ( 'Xbar1' )) 5289 5290split Xbar2 5291enew 5292augroup testing 5293autocmd ! 5294autocmd BufWinLeave * ++once buffer Xbar2 5295augroup END 5296quit 5297call assert_equal ([], win_findbuf ( bufnr ( 'Xbar2' ))) 5298call assert_equal ( 1 , bufexists ( 'Xbar2' )) 5299% bw ! 5300call assert_equal ( 0 , bufexists ( 'Xbar2' )) 5301 5302edit Xbar3 5303enew 5304setlocal bufhidden = hide 5305let s: win = win_getid () 5306tabnew 5307augroup testing 5308autocmd ! 5309autocmd BufHidden * ++once call win_execute ( s: win , 'buffer Xbar3' ) 5310augroup END 5311tabonly 5312call assert_equal ([], win_findbuf ( bufnr ( 'Xbar3' ))) 5313call assert_equal ( 1 , bufexists ( 'Xbar3' )) 5314% bw ! 5315call assert_equal ( 0 , bufexists ( 'Xbar3' )) 5316unlet ! s: win 5317 5318edit Xbar4 5319split Xfoo4 5320augroup testing 5321autocmd ! 5322autocmd BufWinLeave * ++once call assert_equal ( 'Xfoo4' , bufname ()) 5323\| edit Xbar4 5324augroup END 5325edit Xbar4 5326call assert_equal ( 0 , bufloaded ( 'Xfoo4' )) 5327call assert_equal ( 1 , bufexists ( 'Xfoo4' )) 5328" After 8.2.2354, Xfoo4 wrongly had b_nwindows of 1, so couldn't be wiped. 5329call assert_equal ([], win_findbuf ( 'Xfoo4' )) 5330% bw ! 5331call assert_equal ( 0 , bufexists ( 'Xfoo4' )) 5332 5333call CleanUpTestAuGroup () 5334% bw ! 5335endfunc 5336 5337" Test that an autocmd triggered by v:swapchoice == 'q' that switches buffers 5338" doesn't cause b_nwindows to be wrong. 5339func Test_SwapExists_b_nwindows () 5340let lines =<< trim END 5341set nocompatible directory=. 5342 5343let g:buf = bufnr() 5344new 5345 5346func SwapExists() 5347let v:swapchoice = 'q' 5348autocmd BufWinLeave * ++nested ++once buffer Xfoo 5349endfunc 5350 5351func SafeState() 5352edit Xfoo 5353edit <script> 5354%bw! 5355call writefile([bufexists('Xfoo')], 'XnwindowsSwapExists.out') 5356qall! 5357endfunc 5358 5359autocmd SwapExists * ++nested ++once call SwapExists() 5360autocmd SafeState * ++nested ++once call SafeState() 5361END 5362call writefile ( lines , 'XnwindowsSwapExists.vim' , 'D' ) 5363 5364new XnwindowsSwapExists.vim 5365if RunVim ( '' , '' , ' -S XnwindowsSwapExists.vim' ) 5366call assert_equal ([ '0' ], readfile ( 'XnwindowsSwapExists.out' )) 5367call delete ( 'XnwindowsSwapExists.out' ) 5368endif 5369 5370% bw ! 5371endfunc 5372 5373func Test_TextPutX () 5374enew ! 5375 5376let g: pre_event = [] 5377let g: post_event = [] 5378au TextPutPre * let g: pre_event = copy( v: event) 5379au TextPutPost * let g: post_event = copy( v: event) 5380 5381call setreg ( 'a' , [ 'foo' ], 'v' ) 5382norm "ap 5383call assert_equal ( 5384\ #{ regcontents : [ 'foo' ], regname : 'a' , operator : 'p' , 5385\ visual : v: false , regtype : 'v' }, 5386\ g: pre_event ) 5387call assert_equal ( g: pre_event , g: post_event ) 5388 5389call setreg ( '' , [ 'hello' ], 'V' ) 5390norm P 5391call assert_equal ( 5392\ #{ regcontents : [ 'hello' ], regname : '' , operator : 'P' , 5393\ visual : v: false , regtype : 'V' }, 5394\ g: pre_event ) 5395call assert_equal ( g: pre_event , g: post_event ) 5396 5397call setreg ( '' , [ 'maybe' , 'true' ], 'V' ) 5398norm Vp 5399call assert_equal ( 5400\ #{ regcontents : [ 'maybe' , 'true' ], regname : '' , operator : 'P' , 5401\ regtype : 'V' , visual : v: true }, 5402\ g: pre_event ) 5403call assert_equal ( g: pre_event , g: post_event ) 5404call assert_equal ({}, v: event ) 5405 5406call feedkeys ( "iinserted text\<CR>below\<Esc>" , 'x' ) 5407norm ".p 5408call assert_equal ( 5409\ #{ regcontents : [ "inserted text\nbelow" ], regname : '.' , 5410\ operator : 'p' , regtype : 'v' , visual : v: false }, 5411\ g: pre_event ) 5412call assert_equal ( g: pre_event , g: post_event ) 5413 5414call feedkeys ( "\"=201\<CR>p" , 'x' ) 5415call assert_equal ( 5416\ #{ regcontents : [ "201" ], regname : '=' , 5417\ operator : 'p' , regtype : 'v' , visual : v: false }, 5418\ g: pre_event ) 5419call assert_equal ( g: pre_event , g: post_event ) 5420 5421vsplit some.txt 5422wincmd l 5423norm "#p 5424call assert_equal ( 5425\ #{ regcontents : [ "some.txt" ], regname : '#' , 5426\ operator : 'p' , regtype : 'v' , visual : v: false }, 5427\ g: pre_event ) 5428call assert_equal ( g: pre_event , g: post_event ) 5429wincmd h 5430bw ! 5431 5432if has ( 'clipboard_working' ) 5433let @+ = 'clipboard' 5434 5435norm "+p 5436call assert_equal ( 5437\ #{ regcontents : [ "clipboard" ], regname : '+' , 5438\ operator : 'p' , regtype : 'v' , visual : v: false }, 5439\ g: pre_event ) 5440call assert_equal ( g: pre_event , g: post_event ) 5441endif 5442 5443% delete " Clear buffer 5444 5445au ! TextPutPre 5446au ! TextPutPost 5447let g: pre_event = [] 5448let g: post_event = [] 5449 5450au TextPutPre * call setreg( v: event['regname'], 5451\ getreg('', 0, v: true) + ['!']) " Unnamed register should be same as regname 5452 5453call setreg ( '' , [ 'hello' , 'world' ]) 5454norm p 5455call assert_equal ([ '' , 'hello' , 'world' , '!' ], getline ( 1 , '$' )) 5456 5457au ! TextPutPre 5458 5459" Test that special registers cannot be modified 5460% delete 5461au TextPutPre * call setreg('=', '"modified"') | let g: pre_event = copy( v: event) 5462 5463" Set up the expression register to evaluate to a known value. 5464call feedkeys ( "\"=\"original\"\<CR>p" , 'x' ) 5465 5466" The original value is what got put, not the modified one. 5467call assert_equal ([ 'original' ], getline ( 1 , '$' )) 5468 5469" v:event still reports the original value. 5470call assert_equal ([ 'original' ], g: pre_event [ 'regcontents' ]) 5471 5472au ! TextPutPre 5473let g: pre_event = [] 5474 5475for round in range ( 2 ) 5476" Recursive ". register calls have the same contents for post and pre. 5477au TextPutPre * put . | let g: pre_event = copy( v: event) 5478au TextPutPost * let g: post_event = copy( v: event) 5479 5480call feedkeys ( "iinserted\<Esc>" , 'x' ) 5481norm ! ".p 5482 5483call assert_equal ( 5484\ #{ regcontents : [ "inserted" ], regname : '.' , 5485\ operator : 'p' , regtype : 'v' , visual : v: false }, 5486\ g: pre_event ) 5487call assert_equal ( g: pre_event , g: post_event ) 5488 5489au ! TextPutPre 5490au ! TextPutPost 5491 5492" Pasting ". register without TextPutPre/TextPutPost autocommands should 5493" not interfere with these autocommands in the next round. 5494norm ! ".p 5495endfor 5496 5497unlet g: post_event 5498unlet g: pre_event 5499bwipe ! 5500endfunc 5501 5502" Test that attempting to put text in normal mode terminal buffer does not 5503" result in a crash. This only happens when terminal is only window in tabpage 5504" it seems. 5505func Test_TextPutPost_term_norm () 5506CheckFeature terminal 5507 5508tabnew 5509term ++curwin 5510 5511let bnr = bufnr ( '$' ) 5512call WaitForAssert ({ -> assert_equal ( 'running' , term_getstatus ( bnr ))}) 5513 5514call feedkeys ( "\<C-\>\<C-N>" , 'xt' ) 5515call WaitForAssert ({ -> assert_equal ( 'running,normal' , term_getstatus ( bnr ))}) 5516 5517let err = 0 5518 5519try 5520call feedkeys ( "p" , 'xt' ) 5521catch /^Vim\%((\S\+)\)\=:E21:/ 5522let err = 1 5523endtry 5524 5525call assert_true ( err ) 5526 5527unlet err 5528bwipe ! 5529endfunc 5530 5531" vim: shiftwidth=2 sts=2 expandtab