robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
2cd4229d01
master
1local t =require ( 'test.testutil' ) 2local n =require ( 'test.functional.testnvim' )() 3local Screen =require ( 'test.functional.ui.screen' ) 4 5local describe , it , before_each =t . describe , t . it , t . before_each 6local assert_visible =n . assert_visible 7local assert_alive =n . assert_alive 8local dedent =t . dedent 9local eq =t . eq 10local neq =t . neq 11local eval =n . eval 12local exec =n . exec 13local feed =n . feed 14local clear =n . clear 15local matches =t . matches 16local api =n . api 17local pcall_err =t . pcall_err 18local fn =n . fn 19local expect =n . expect 20local command =n . command 21local exec_lua =n . exec_lua 22local retry =t . retry 23local source =n . source 24local request =n . request 25 26describe ( 'autocmd' , function () 27before_each ( clear ) 28 29it ( ':tabnew, :split, :close events order, <afile>' , function () 30local expected ={ 31{ 'WinLeave' , '' } , 32{ 'TabLeave' , '' } , 33{ 'WinEnter' , '' } , 34{ 'TabNew' , 'testfile1' } , -- :tabnew 35{ 'TabEnter' , '' } , 36{ 'BufLeave' , '' } , 37{ 'BufEnter' , 'testfile1' } , -- :split 38{ 'WinLeave' , 'testfile1' } , 39{ 'WinEnter' , 'testfile1' } , 40{ 'WinLeave' , 'testfile1' } , 41{ 'WinClosed' , '1002' } , -- :close, WinClosed <afile> = window-id 42{ 'WinEnter' , 'testfile1' } , 43{ 'WinLeave' , 'testfile1' } , -- :bdelete 44{ 'WinEnter' , 'testfile1' } , 45{ 'BufLeave' , 'testfile1' } , 46{ 'BufEnter' , 'testfile2' } , 47{ 'WinClosed' , '1000' } , 48} 49command ( 'let g:evs = []' ) 50command ( 'autocmd BufEnter * :call add(g:evs, ["BufEnter", expand("<afile>")])' ) 51command ( 'autocmd BufLeave * :call add(g:evs, ["BufLeave", expand("<afile>")])' ) 52command ( 'autocmd TabEnter * :call add(g:evs, ["TabEnter", expand("<afile>")])' ) 53command ( 'autocmd TabLeave * :call add(g:evs, ["TabLeave", expand("<afile>")])' ) 54command ( 'autocmd TabNew * :call add(g:evs, ["TabNew", expand("<afile>")])' ) 55command ( 'autocmd WinEnter * :call add(g:evs, ["WinEnter", expand("<afile>")])' ) 56command ( 'autocmd WinLeave * :call add(g:evs, ["WinLeave", expand("<afile>")])' ) 57command ( 'autocmd WinClosed * :call add(g:evs, ["WinClosed", expand("<afile>")])' ) 58command ( 'tabnew testfile1' ) 59command ( 'split' ) 60command ( 'close' ) 61command ( 'new testfile2' ) 62command ( 'bdelete 1' ) 63eq ( expected , eval ( 'g:evs' )) 64end ) 65 66it ( 'first edit causes BufUnload on NoName' , function () 67local expected ={ 68{ 'BufUnload' , '' } , 69{ 'BufDelete' , '' } , 70{ 'BufWipeout' , '' } , 71{ 'BufEnter' , 'testfile1' } , 72} 73command ( 'let g:evs = []' ) 74command ( 'autocmd BufEnter * :call add(g:evs, ["BufEnter", expand("<afile>")])' ) 75command ( 'autocmd BufDelete * :call add(g:evs, ["BufDelete", expand("<afile>")])' ) 76command ( 'autocmd BufLeave * :call add(g:evs, ["BufLeave", expand("<afile>")])' ) 77command ( 'autocmd BufUnload * :call add(g:evs, ["BufUnload", expand("<afile>")])' ) 78command ( 'autocmd BufWipeout * :call add(g:evs, ["BufWipeout", expand("<afile>")])' ) 79command ( 'edit testfile1' ) 80eq ( expected , eval ( 'g:evs' )) 81end ) 82 83it ( 'WinClosed is non-recursive' , function () 84command ( 'let g:triggered = 0' ) 85command ( 'autocmd WinClosed * :let g:triggered+=1 | :bdelete 2' ) 86command ( 'new testfile2' ) 87command ( 'new testfile3' ) 88 89-- All 3 buffers are visible. 90assert_visible ( 1 , true ) 91assert_visible ( 2 , true ) 92assert_visible ( 3 , true ) 93 94-- Trigger WinClosed, which also deletes buffer/window 2. 95command ( 'bdelete 1' ) 96 97-- Buffers 1 and 2 were closed but WinClosed was triggered only once. 98eq ( 1 , eval ( 'g:triggered' )) 99assert_visible ( 1 , false ) 100assert_visible ( 2 , false ) 101assert_visible ( 3 , true ) 102end ) 103 104it ( 'WinClosed from a different tabpage' , function () 105command ( 'let g:evs = []' ) 106command ( 'edit tesfile1' ) 107command ( 'autocmd WinClosed <buffer> :call add(g:evs, ["WinClosed", expand("<abuf>")])' ) 108local buf1 =eval ( "bufnr('%')" ) 109command ( 'new' ) 110local buf2 =eval ( "bufnr('%')" ) 111command ( 112'autocmd WinClosed <buffer> :call add(g:evs, ["WinClosed", expand("<abuf>")])' 113-- Attempt recursion. 114.. ' | bdelete ' 115.. buf2 116) 117command ( 'tabedit testfile2' ) 118command ( 'tabedit testfile3' ) 119command ( 'bdelete ' .. buf2 ) 120-- Non-recursive: only triggered once. 121eq ( { 122{ 'WinClosed' , '2' } , 123} , eval ( 'g:evs' )) 124command ( 'bdelete ' .. buf1 ) 125eq ( { 126{ 'WinClosed' , '2' } , 127{ 'WinClosed' , '1' } , 128} , eval ( 'g:evs' )) 129end ) 130 131it ( 'WinClosed from root directory' , function () 132command ( 'cd /' ) 133command ( 'let g:evs = []' ) 134command ( 'autocmd WinClosed * :call add(g:evs, ["WinClosed", expand("<afile>")])' ) 135command ( 'new' ) 136command ( 'close' ) 137eq ( { 138{ 'WinClosed' , '1001' } , 139} , eval ( 'g:evs' )) 140end ) 141 142it ( 'v:vim_did_enter is 1 after VimEnter' , function () 143eq ( 1 , eval ( 'v:vim_did_enter' )) 144end ) 145 146describe ( 'BufLeave autocommand' , function () 147it ( 'can wipe out the buffer created by :edit which triggered autocmd' , function () 148api . nvim_set_option_value ( 'hidden' , true , {} ) 149api . nvim_buf_set_lines ( 0 , 0 , 1 , false , { 150'start of test file xx' , 151'end of test file xx' , 152} ) 153 154command ( 'autocmd BufLeave * bwipeout yy' ) 155eq ( 156'Vim(edit):E143: Autocommands unexpectedly deleted new buffer yy' , 157pcall_err ( command , 'edit yy' ) 158) 159 160expect ( [[ 161start of test file xx 162end of test file xx]] ) 163end ) 164end ) 165 166it ( '++once' , function () -- :help autocmd-once 167-- 168-- ":autocmd … ++once" executes its handler once, then removes the handler. 169-- 170local expected ={ 171'Many1' , 172'Once1' , 173'Once2' , 174'Many2' , 175'Once3' , 176'Many1' , 177'Many2' , 178'Many1' , 179'Many2' , 180} 181command ( 'let g:foo = []' ) 182command ( 'autocmd TabNew * :call add(g:foo, "Many1")' ) 183command ( 'autocmd TabNew * ++once :call add(g:foo, "Once1")' ) 184command ( 'autocmd TabNew * ++once :call add(g:foo, "Once2")' ) 185command ( 'autocmd TabNew * :call add(g:foo, "Many2")' ) 186command ( 'autocmd TabNew * ++once :call add(g:foo, "Once3")' ) 187eq ( 188dedent ( [[ 189 190--- Autocommands --- 191TabNew 192* :call add(g:foo, "Many1") 193:call add(g:foo, "Once1") 194:call add(g:foo, "Once2") 195:call add(g:foo, "Many2") 196:call add(g:foo, "Once3")]] ), 197fn . execute ( 'autocmd Tabnew' ) 198) 199command ( 'tabnew' ) 200command ( 'tabnew' ) 201command ( 'tabnew' ) 202eq ( expected , eval ( 'g:foo' )) 203eq ( 204dedent ( [[ 205 206--- Autocommands --- 207TabNew 208* :call add(g:foo, "Many1") 209:call add(g:foo, "Many2")]] ), 210fn . execute ( 'autocmd Tabnew' ) 211) 212 213-- 214-- ":autocmd … ++once" handlers can be deleted. 215-- 216expected ={} 217command ( 'let g:foo = []' ) 218command ( 'autocmd TabNew * ++once :call add(g:foo, "Once1")' ) 219command ( 'autocmd! TabNew' ) 220command ( 'tabnew' ) 221eq ( expected , eval ( 'g:foo' )) 222 223-- 224-- ":autocmd … <buffer> ++once ++nested" 225-- 226expected ={ 227'OptionSet-Once' , 228'CursorMoved-Once' , 229} 230command ( 'let g:foo = []' ) 231command ( 'autocmd OptionSet binary ++nested ++once :call add(g:foo, "OptionSet-Once")' ) 232command ( 233'autocmd CursorMoved <buffer> ++once ++nested setlocal binary|:call add(g:foo, "CursorMoved-Once")' 234) 235command ( "put ='foo bar baz'" ) 236feed ( '0llhlh' ) 237eq ( expected , eval ( 'g:foo' )) 238 239-- 240-- :autocmd should not show empty section after ++once handlers expire. 241-- 242expected ={ 243'Once1' , 244'Once2' , 245} 246command ( 'let g:foo = []' ) 247command ( 'autocmd! TabNew' ) -- Clear all TabNew handlers. 248command ( 'autocmd TabNew * ++once :call add(g:foo, "Once1")' ) 249command ( 'autocmd TabNew * ++once :call add(g:foo, "Once2")' ) 250command ( 'tabnew' ) 251eq ( expected , eval ( 'g:foo' )) 252eq ( 253dedent ( [[ 254 255--- Autocommands ---]] ), 256fn . execute ( 'autocmd Tabnew' ) 257) 258 259-- 260-- :autocmd does not recursively call ++once Lua handlers. 261-- 262exec_lua [[vim.g.count = 0]] 263eq ( 0 , eval ( 'g:count' )) 264exec_lua [[ 265vim.api.nvim_create_autocmd('User', { 266once = true, 267pattern = nil, 268callback = function() 269vim.g.count = vim.g.count + 1 270vim.api.nvim_exec_autocmds('User', { pattern = nil }) 271end, 272}) 273vim.api.nvim_exec_autocmds('User', { pattern = nil }) 274]] 275eq ( 1 , eval ( 'g:count' )) 276end ) 277 278it ( 'internal `aucmd_win` window' , function () 279-- Nvim uses a special internal window `aucmd_win` to execute certain 280-- actions for an invisible buffer (:help E813). 281-- Check redrawing (never shown) and API access to this window. 282 283local screen =Screen . new ( 50 , 10 ) 284 285source ( [[ 286function! Doit() 287let g:winid = nvim_get_current_win() 288redraw! 289echo getchar() 290" API functions work when aucmd_win is in scope 291let g:had_value = has_key(w:, "testvar") 292call nvim_win_set_var(g:winid, "testvar", 7) 293let g:test = w:testvar 294endfunction 295set hidden 296" add dummy text to not discard the buffer 297call setline(1,"bb") 298autocmd User <buffer> call Doit() 299]] ) 300screen : expect ( [[ 301^bb | 302{1:~ }|*8 303| 304]] ) 305 306feed ( ':enew | doautoall User<cr>' ) 307screen : expect ( [[ 308| 309{1:~ }|*8 310:enew | doautoall User | 311]] ) 312 313feed ( '<cr>' ) 314screen : expect ( [[ 315^ | 316{1:~ }|*8 31713 | 318]] ) 319eq ( 7 , eval ( 'g:test' )) 320 321-- API calls are blocked when aucmd_win is not in scope 322eq ( 323'Vim(call):E5555: API call: Invalid window id: 1001' , 324pcall_err ( command , 'call nvim_set_current_win(g:winid)' ) 325) 326 327-- second time aucmd_win is needed, a different code path is invoked 328-- to reuse the same window, so check again 329command ( 'let g:test = v:null' ) 330command ( 'let g:had_value = v:null' ) 331feed ( ':doautoall User<cr>' ) 332screen : expect ( [[ 333| 334{1:~ }|*8 335:doautoall User | 336]] ) 337 338feed ( '<cr>' ) 339screen : expect ( [[ 340^ | 341{1:~ }|*8 34213 | 343]] ) 344-- win vars in aucmd_win should have been reset 345eq ( 0 , eval ( 'g:had_value' )) 346eq ( 7 , eval ( 'g:test' )) 347 348eq ( 349'Vim(call):E5555: API call: Invalid window id: 1001' , 350pcall_err ( command , 'call nvim_set_current_win(g:winid)' ) 351) 352end ) 353 354it ( '`aucmd_win` cannot be changed into a normal window #13699' , function () 355local screen =Screen . new ( 50 , 10 ) 356 357-- Create specific layout and ensure it's left unchanged. 358-- Use vim._with on a hidden buffer so aucmd_win is used. 359exec_lua [[ 360vim.cmd "wincmd s | wincmd _" 361_G.buf = vim.api.nvim_create_buf(true, true) 362vim._with({buf = _G.buf}, function() vim.cmd "wincmd J" end) 363]] 364screen : expect [[ 365^ | 366{1:~ }|*5 367{3:[No Name] }| 368| 369{2:[No Name] }| 370| 371]] 372-- This used to crash after making aucmd_win a normal window via the above. 373exec_lua [[ 374vim.cmd "tabnew | tabclose # | wincmd s | wincmd _" 375vim._with({buf = _G.buf}, function() vim.cmd "wincmd K" end) 376]] 377assert_alive () 378screen : expect_unchanged () 379 380-- Also check with win_splitmove(). 381exec_lua [[ 382vim._with({buf = _G.buf}, function() 383vim.fn.win_splitmove(vim.fn.win_getid(), vim.fn.win_getid(1)) 384end) 385]] 386screen : expect_unchanged () 387 388-- Also check with nvim_win_set_config(). 389matches ( 390'^Failed to move window %d+ into split$' , 391pcall_err ( 392exec_lua , 393[[ 394vim._with({buf = _G.buf}, function() 395vim.api.nvim_win_set_config(0, { 396vertical = true, 397win = vim.fn.win_getid(1) 398}) 399end) 400]] 401) 402) 403screen : expect_unchanged () 404 405-- Ensure splitting still works from inside the aucmd_win. 406exec_lua [[vim._with({buf = _G.buf}, function() vim.cmd "split" end)]] 407screen : expect [[ 408^ | 409{1:~ }| 410{3:[No Name] }| 411| 412{1:~ }| 413{2:[Scratch] }| 414| 415{1:~ }| 416{2:[No Name] }| 417| 418]] 419 420-- After all of our messing around, aucmd_win should still be floating. 421-- Use :only to ensure _G.buf is hidden again (so the aucmd_win is used). 422eq ( 423'editor' , 424exec_lua [[ 425vim.cmd "only" 426vim._with({buf = _G.buf}, function() 427_G.config = vim.api.nvim_win_get_config(0) 428end) 429return _G.config.relative 430]] 431) 432end ) 433 434it ( 'cannot close `aucmd_win` in non-current tabpage' , function () 435exec ( [[ 436file Xa 437tabnew Xb 438call setline(1, 'foo') 439tabfirst 440autocmd BufWriteCmd Xb tablast | bwipe! Xa 441]] ) 442eq ( 443'BufWriteCmd Autocommands for "Xb": Vim(bwipeout):E813: Cannot close autocmd window' , 444pcall_err ( command , 'wall' ) 445) 446-- Sanity check: :bwipe failing to close all windows into Xa should keep it loaded. 447-- (So there's no risk of it being left unloaded inside a window) 448eq ( 1 , eval ( 'bufloaded("Xa")' )) 449end ) 450 451describe ( 'closing last non-floating window in tab from `aucmd_win`' , function () 452before_each ( function () 453command ( 'edit Xa.txt' ) 454command ( 'tabnew Xb.txt' ) 455command ( 'autocmd BufAdd Xa.txt 1close' ) 456end ) 457 458it ( 'gives E814 when there are no other floating windows' , function () 459eq ( 460'BufAdd Autocommands for "Xa.txt": Vim(close):E814: Cannot close window, only autocmd window would remain' , 461pcall_err ( command , 'doautoall BufAdd' ) 462) 463end ) 464 465it ( 'gives E814 when there are other floating windows' , function () 466api . nvim_open_win ( 4670 , 468true , 469{ width =10 , height =10 , relative ='editor' , row =10 , col =10 } 470) 471eq ( 472'BufAdd Autocommands for "Xa.txt": Vim(close):E814: Cannot close window, only autocmd window would remain' , 473pcall_err ( command , 'doautoall BufAdd' ) 474) 475end ) 476end ) 477 478it ( 'closing `aucmd_win` using API gives E813' , function () 479exec_lua ( [[ 480vim.cmd('tabnew') 481_G.buf = vim.api.nvim_create_buf(true, true) 482]] ) 483matches ( 484'Vim:E813: Cannot close autocmd window$' , 485pcall_err ( 486exec_lua , 487[[ 488vim._with({buf = _G.buf}, function() 489local win = vim.api.nvim_get_current_win() 490vim.api.nvim_win_close(win, true) 491end) 492]] 493) 494) 495matches ( 496'Vim:E813: Cannot close autocmd window$' , 497pcall_err ( 498exec_lua , 499[[ 500vim._with({buf = _G.buf}, function() 501local win = vim.api.nvim_get_current_win() 502vim.cmd('tabnext') 503vim.api.nvim_win_close(win, true) 504end) 505]] 506) 507) 508matches ( 509'Vim:E813: Cannot close autocmd window$' , 510pcall_err ( 511exec_lua , 512[[ 513vim._with({buf = _G.buf}, function() 514local win = vim.api.nvim_get_current_win() 515vim.api.nvim_win_hide(win) 516end) 517]] 518) 519) 520matches ( 521'Vim:E813: Cannot close autocmd window$' , 522pcall_err ( 523exec_lua , 524[[ 525vim._with({buf = _G.buf}, function() 526local win = vim.api.nvim_get_current_win() 527vim.cmd('tabnext') 528vim.api.nvim_win_hide(win) 529end) 530]] 531) 532) 533end ) 534 535it ( ':doautocmd does not warn "No matching autocommands" #10689' , function () 536local screen =Screen . new ( 32 , 3 ) 537 538feed ( ':doautocmd User Foo<cr>' ) 539screen : expect { 540grid =[[ 541^ | 542{1:~ }| 543:doautocmd User Foo | 544]] , 545} 546feed ( ':autocmd! SessionLoadPost<cr>' ) 547feed ( ':doautocmd SessionLoadPost<cr>' ) 548screen : expect { 549grid =[[ 550^ | 551{1:~ }| 552:doautocmd SessionLoadPost | 553]] , 554} 555end ) 556 557describe ( 'v:event is readonly #18063' , function () 558it ( 'during ChanOpen event' , function () 559command ( 'autocmd ChanOpen * let v:event.info.id = 0' ) 560fn . jobstart ( { 'cat' } ) 561retry ( nil , nil , function () 562eq ( 'E46: Cannot change read-only variable "v:event.info"' , api . nvim_get_vvar ( 'errmsg' )) 563end ) 564end ) 565 566it ( 'during ChanOpen event' , function () 567command ( 'autocmd ChanInfo * let v:event.info.id = 0' ) 568api . nvim_set_client_info ( 'foo' , {} , 'remote' , {} , {} ) 569retry ( nil , nil , function () 570eq ( 'E46: Cannot change read-only variable "v:event.info"' , api . nvim_get_vvar ( 'errmsg' )) 571end ) 572end ) 573 574it ( 'during RecordingLeave event' , function () 575command ( [[autocmd RecordingLeave * let v:event.regname = '']] ) 576eq ( 577'RecordingLeave Autocommands for "*": Vim(let):E46: Cannot change read-only variable "v:event.regname"' , 578pcall_err ( command , 'normal! qqq' ) 579) 580end ) 581 582it ( 'during TermClose event' , function () 583command ( 'autocmd TermClose * let v:event.status = 0' ) 584command ( 'terminal' ) 585eq ( 586'TermClose Autocommands for "*": Vim(let):E46: Cannot change read-only variable "v:event.status"' , 587pcall_err ( command , 'bdelete!' ) 588) 589end ) 590end ) 591 592describe ( 'old_tests' , function () 593it ( 'vimscript: WinNew ++once' , function () 594source [[ 595" Without ++once WinNew triggers twice 596let g:did_split = 0 597augroup Testing 598au! 599au WinNew * let g:did_split += 1 600augroup END 601split 602split 603call assert_equal(2, g:did_split) 604call assert_true(exists('#WinNew')) 605close 606close 607 608" With ++once WinNew triggers once 609let g:did_split = 0 610augroup Testing 611au! 612au WinNew * ++once let g:did_split += 1 613augroup END 614split 615split 616call assert_equal(1, g:did_split) 617call assert_false(exists('#WinNew')) 618close 619close 620 621call assert_fails('au WinNew * ++once ++once echo bad', 'E983:') 622]] 623 624api . nvim_set_var ( 'did_split' , 0 ) 625 626source [[ 627augroup Testing 628au! 629au WinNew * let g:did_split += 1 630augroup END 631 632split 633split 634]] 635 636eq ( 2 , api . nvim_get_var ( 'did_split' )) 637eq ( 1 , fn . exists ( '#WinNew' )) 638 639-- Now with once 640api . nvim_set_var ( 'did_split' , 0 ) 641 642source [[ 643augroup Testing 644au! 645au WinNew * ++once let g:did_split += 1 646augroup END 647 648split 649split 650]] 651 652eq ( 1 , api . nvim_get_var ( 'did_split' )) 653eq ( 0 , fn . exists ( '#WinNew' )) 654 655-- call assert_fails('au WinNew * ++once ++once echo bad', 'E983:') 656local ok , msg =pcall ( 657source , 658[[ 659au WinNew * ++once ++once echo bad 660]] 661) 662 663eq ( false , ok ) 664eq ( true , not not string . find ( msg , 'E983:' )) 665end ) 666 667it ( 'should have autocmds in filetypedetect group' , function () 668source [[filetype on]] 669neq ( {} , api . nvim_get_autocmds { group ='filetypedetect' } ) 670end ) 671 672it ( 'should allow comma-separated patterns' , function () 673source [[ 674augroup TestingPatterns 675au! 676autocmd BufReadCmd *.shada,*.shada.tmp.[a-z] echo 'hello' 677autocmd BufReadCmd *.shada,*.shada.tmp.[a-z] echo 'hello' 678augroup END 679]] 680 681eq ( 4 , # api . nvim_get_autocmds { event ='BufReadCmd' , group ='TestingPatterns' } ) 682end ) 683end ) 684 685it ( 'no use-after-free when adding autocommands from a callback' , function () 686exec_lua [[ 687vim.cmd "autocmd! TabNew" 688vim.g.count = 0 689vim.api.nvim_create_autocmd('TabNew', { 690callback = function() 691vim.g.count = vim.g.count + 1 692for _ = 1, 100 do 693vim.cmd "autocmd TabNew * let g:count += 1" 694end 695return true 696end, 697}) 698vim.cmd "tabnew" 699]] 700eq ( 1 , eval ( 'g:count' )) -- Added autocommands should not be executed 701end ) 702 703it ( 'no crash when clearing a group inside a callback #23355' , function () 704exec_lua [[ 705vim.cmd "autocmd! TabNew" 706local group = vim.api.nvim_create_augroup('Test', {}) 707local id 708id = vim.api.nvim_create_autocmd('TabNew', { 709group = group, 710callback = function() 711vim.api.nvim_del_autocmd(id) 712vim.api.nvim_create_augroup('Test', { clear = true }) 713end, 714}) 715vim.cmd "tabnew" 716]] 717end ) 718 719it ( 'no use-after-free when wiping buffer in Syntax autocommand' , function () 720exec ( [[ 721new 722autocmd Syntax * ++once bwipe! 723setlocal syntax=vim 724]] ) 725assert_alive () 726end ) 727 728it ( 'no use-after-free from win_enter autocommands in win_move_after' , function () 729exec [[ 730split foo 731split bar 732lcd .. 733wincmd b 734]] 735eq ( fn . winnr ( '$' ), fn . winnr ()) 736-- Using DirChanged as Enter/Leave autocmds are blocked by :ball here. 737exec [[ 738autocmd DirChanged * ++once split flarb | only! 739ball 740]] 741eq ( 'flarb' , fn . bufname ()) 742end ) 743 744it ( 'no use-after-free when closing new curwin during CTRL-W_x #41373' , function () 745exec ( [[ 746tabnew 747vsplit 748autocmd WinLeave * ++once bwipe! 749]] ) 750eq ( 2 , # api . nvim_list_tabpages ()) 751feed ( '<C-W>x' ) 752eq ( 1 , # api . nvim_list_tabpages ()) 753assert_alive () 754end ) 755 756it ( 'does not ignore comma-separated patterns after a buffer-local pattern' , function () 757exec [[ 758edit baz " reuses buffer 1 759edit bazinga 760edit bar 761edit boop 762edit foo 763edit floob 764 765let g:events1 = [] 766autocmd BufEnter <buffer>,<buffer=1>,boop,bar let g:events1 += [expand('<afile>')] 767let g:events2 = [] 768augroup flobby 769autocmd BufEnter <buffer=2>,foo let g:events2 += [expand('<afile>')] 770autocmd BufEnter foo,<buffer=3> let g:events2 += ['flobby ' .. expand('<afile>')] 771augroup END 772]] 773eq ( 774dedent ( [=[ 775 776--- Autocommands --- 777BufEnter 778<buffer=6> 779let g:events1 += [expand('<afile>')] 780<buffer=1> 781let g:events1 += [expand('<afile>')] 782boop let g:events1 += [expand('<afile>')] 783bar let g:events1 += [expand('<afile>')] 784flobby BufEnter 785<buffer=2> 786let g:events2 += [expand('<afile>')] 787foo let g:events2 += [expand('<afile>')] 788let g:events2 += ['flobby ' .. expand('<afile>')] 789<buffer=3> 790let g:events2 += ['flobby ' .. expand('<afile>')]]=] ), 791fn . execute ( 'autocmd BufEnter' ) 792) 793command ( 'bufdo "' ) 794eq ( { 'baz' , 'bar' , 'boop' , 'floob' } , eval ( 'g:events1' )) 795eq ( { 'bazinga' , 'flobby bar' , 'foo' , 'flobby foo' } , eval ( 'g:events2' )) 796 797-- Also make sure it doesn't repeat the group/event name or pattern for each printed event. 798-- Do however repeat the pattern if the user specified it multiple times to be printed. 799-- These conditions aim to make the output consistent with Vim. 800eq ( 801dedent ( [=[ 802 803--- Autocommands --- 804BufEnter 805<buffer=1> 806let g:events1 += [expand('<afile>')] 807bar let g:events1 += [expand('<afile>')] 808bar let g:events1 += [expand('<afile>')] 809flobby BufEnter 810foo let g:events2 += [expand('<afile>')] 811let g:events2 += ['flobby ' .. expand('<afile>')] 812foo let g:events2 += [expand('<afile>')] 813let g:events2 += ['flobby ' .. expand('<afile>')] 814BufEnter 815<buffer=6> 816let g:events1 += [expand('<afile>')] 817<buffer=6> 818let g:events1 += [expand('<afile>')] 819<buffer=6> 820let g:events1 += [expand('<afile>')]]=] ), 821fn . execute ( 'autocmd BufEnter <buffer=1>,bar,bar,foo,foo,<buffer>,<buffer=6>,<buffer>' ) 822) 823end ) 824 825it ( 'parses empty comma-delimited patterns correctly' , function () 826exec [[ 827autocmd User , " 828autocmd User ,, " 829autocmd User ,,according,to,,all,known,,,laws,, " 830]] 831api . nvim_create_autocmd ( 'User' , { pattern =',,of,,,aviation,,,,there,,' , command ='' } ) 832api . nvim_create_autocmd ( 'User' , { 833pattern ={ ',,,,is,,no' , ',,way,,,' , 'a,,bee{,should be, able to,},fly' } , 834command ='' , 835} ) 836eq ( 837{ 838'according' , 839'to' , 840'all' , 841'known' , 842'laws' , 843'of' , 844'aviation' , 845'there' , 846'is' , 847'no' , 848'way' , 849'a' , 850'bee{,should be, able to,}' , 851'fly' , 852} , 853exec_lua ( function () 854return vim . tbl_map ( function ( v ) 855return v . pattern 856end , vim . api . nvim_get_autocmds ( { event ='User' } )) 857end ) 858) 859eq ( 860dedent ( [[ 861 862--- Autocommands --- 863User 864there 865is 866a 867fly]] ), 868fn . execute ( 'autocmd User ,,,there,is,,a,fly,,' ) 869) 870end ) 871 872it ( 'normalizes path sep (slashes) in env var `pattern` #39382' , function () 873local path =t . is_os ( 'win' ) and [[C:\foo\bar]] or 'C:/foo/bar' 874fn . setenv ( 'FOOBAR' , path ) 875exec [[ 876autocmd User ~,$FOOBAR " 877]] 878local cmds =exec_lua ( function () 879return vim . api . nvim_get_autocmds ( { event ='User' } ) 880end ) 881eq ( vim . fs . normalize ( '~' ), cmds [ 1 ]. pattern ) 882eq ( vim . fs . normalize ( path ), cmds [ 2 ]. pattern ) 883end ) 884 885it ( 'exists() consults &fileignorecase' , function () 886command ( [[autocmd User foo/bar echo]] ) 887eq ( 0 , fn . exists ( [[#User#foo/bar/]] )) 888eq ( 1 , fn . exists ( [[#User#foo/bar]] )) 889-- Even on Windows, `/` should be used as the path sep 890eq ( 0 , fn . exists ( [[#User#foo\bar]] )) 891 892command ( [[set fileignorecase]] ) 893eq ( 1 , fn . exists ( [[#User#Foo/Bar]] )) 894command ( [[set nofileignorecase]] ) 895eq ( 0 , fn . exists ( [[#User#Foo/Bar]] )) 896end ) 897end )