robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
aa1d8a2702
master
1local t =require ( 'test.testutil' ) 2local n =require ( 'test.functional.testnvim' )() 3 4local describe , it , before_each , pending =t . describe , t . it , t . before_each , t . pending 5local clear =n . clear 6local command =n . command 7local eq =t . eq 8local neq =t . neq 9local exec_lua =n . exec_lua 10local matches =t . matches 11local api =n . api 12local source =n . source 13local pcall_err =t . pcall_err 14 15before_each ( clear ) 16 17describe ( 'autocmd api' , function () 18describe ( 'nvim_create_autocmd' , function () 19it ( 'validation' , function () 20eq ( 21"Conflict: 'callback' not allowed with 'command'" , 22pcall_err ( api . nvim_create_autocmd , 'BufReadPost' , { 23pattern ='*.py,*.pyi' , 24command ="echo 'Should Have Errored" , 25callback ='NotAllowed' , 26} ) 27) 28eq ( 29"Conflict: 'pattern' not allowed with 'buf'" , 30pcall_err ( api . nvim_create_autocmd , 'FileType' , { 31command ='let g:called = g:called + 1' , 32buf =0 , 33pattern ='*.py' , 34} ) 35) 36-- Deprecated `buffer` key still triggers the same conflict. 37eq ( 38"Conflict: 'pattern' not allowed with 'buf'" , 39pcall_err ( api . nvim_create_autocmd , 'FileType' , { 40command ='let g:called = g:called + 1' , 41buffer =0 , 42pattern ='*.py' , 43} ) 44) 45-- Passing both `buf` and `buffer` is rejected. 46eq ( 47"Conflict: 'buf' not allowed with 'buffer'" , 48pcall_err ( api . nvim_create_autocmd , 'FileType' , { 49command ='ls' , 50buf =0 , 51buffer =1 , 52} ) 53) 54eq ( 55"Required: 'event'" , 56pcall_err ( api . nvim_create_autocmd , {} , { 57command ='ls' , 58} ) 59) 60matches ( 61"Invalid 'event': expected Array or String, got nil$" , 62pcall_err ( exec_lua , function () 63vim . api . nvim_create_autocmd ( nil , {} ) 64end ) 65) 66eq ( "Required: 'command' or 'callback'" , pcall_err ( api . nvim_create_autocmd , 'FileType' , {} )) 67eq ( 68"Invalid 'desc': expected String, got Integer" , 69pcall_err ( api . nvim_create_autocmd , 'FileType' , { 70command ='ls' , 71desc =42 , 72} ) 73) 74eq ( 75"Invalid 'callback': expected Lua function or Vim function name, got Integer" , 76pcall_err ( api . nvim_create_autocmd , 'FileType' , { 77callback =0 , 78} ) 79) 80eq ( 81"Invalid 'event' item: expected String, got Array" , 82pcall_err ( api . nvim_create_autocmd , { 'FileType' , {} } , {} ) 83) 84eq ( 85"Invalid 'group': 0" , 86pcall_err ( api . nvim_create_autocmd , 'FileType' , { 87group =0 , 88command ='ls' , 89} ) 90) 91 92eq ( "Invalid 'event': 'foo'" , pcall_err ( api . nvim_create_autocmd , 'foo' , { command ='' } )) 93eq ( 94"Invalid 'event': 'VimEnter '" , 95pcall_err ( api . nvim_create_autocmd , 'VimEnter ' , { command ='' } ) 96) 97eq ( 98"Invalid 'event': 'VimEnter foo'" , 99pcall_err ( api . nvim_create_autocmd , 'VimEnter foo' , { command ='' } ) 100) 101eq ( 102"Invalid 'event': 'BufAdd,BufDelete'" , 103pcall_err ( api . nvim_create_autocmd , 'BufAdd,BufDelete' , { command ='' } ) 104) 105 106eq ( 107'No non-empty patterns specified' , 108pcall_err ( api . nvim_create_autocmd , 'VimEnter' , { pattern ='' , command ='' } ) 109) 110eq ( 111'No non-empty patterns specified' , 112pcall_err ( api . nvim_create_autocmd , 'VimEnter' , { pattern =',' , command ='' } ) 113) 114eq ( 115'No non-empty patterns specified' , 116pcall_err ( api . nvim_create_autocmd , 'VimEnter' , { pattern ={} , command ='' } ) 117) 118eq ( 119'No non-empty patterns specified' , 120pcall_err ( api . nvim_create_autocmd , 'VimEnter' , { pattern ={ '' } , command ='' } ) 121) 122eq ( 123'No non-empty patterns specified' , 124pcall_err ( api . nvim_create_autocmd , 'VimEnter' , { pattern ={ ',,' } , command ='' } ) 125) 126-- OK when at least one non-empty pattern is given. 127api . nvim_create_autocmd ( 'VimEnter' , { pattern ='hi,,' , command ='' } ) 128api . nvim_create_autocmd ( 'VimEnter' , { pattern ={ ',' , 'bye' } , command ='' } ) 129eq ( 130{ 'hi' , 'bye' } , 131exec_lua ( function () 132return vim 133. iter ( vim . api . nvim_get_autocmds ( { event ='VimEnter' } )) 134: map ( function ( ev ) 135return ev . pattern 136end ) 137: totable () 138end ) 139) 140end ) 141 142it ( 'doesnt leak when you use ++once' , function () 143eq ( 1441 , 145exec_lua ( 146[[ 147local count = 0 148 149vim.api.nvim_create_autocmd("FileType", { 150pattern = "*", 151callback = function() count = count + 1 end, 152once = true 153}) 154 155vim.cmd "set filetype=txt" 156vim.cmd "set filetype=python" 157 158return count 159]] , 160{} 161) 162) 163end ) 164 165it ( 'allows passing buf by key' , function () 166for _ , buf_key in ipairs ( { 167'buf' , 168'buffer' , -- deprecated name 169} ) do 170api . nvim_set_var ( 'called' , 0 ) 171 172api . nvim_create_autocmd ( 'FileType' , { 173command ='let g:called = g:called + 1' , 174[ buf_key ] =0 , 175} ) 176 177command 'set filetype=txt' 178eq ( 1 , api . nvim_get_var ( 'called' )) 179 180-- switch to a new buffer 181command 'new' 182command 'set filetype=python' 183 184eq ( 1 , api . nvim_get_var ( 'called' )) 185end 186end ) 187 188it ( 'does not allow passing invalid buffers' , function () 189local ok , msg =pcall ( api . nvim_create_autocmd , 'FileType' , { 190command ='let g:called = g:called + 1' , 191buf =- 1 , 192} ) 193 194eq ( false , ok ) 195matches ( 'Invalid buffer id' , msg ) 196end ) 197 198it ( 'errors on non-functions for cb' , function () 199eq ( 200false , 201pcall ( 202exec_lua , 203[[ 204vim.api.nvim_create_autocmd("BufReadPost", { 205pattern = "*.py,*.pyi", 206callback = 5, 207}) 208]] 209) 210) 211end ) 212 213it ( 'allow passing pattern and <buffer> in same pattern' , function () 214local ok =pcall ( api . nvim_create_autocmd , 'BufReadPost' , { 215pattern ='*.py,<buffer>' , 216command ="echo 'Should Not Error'" , 217} ) 218 219eq ( true , ok ) 220end ) 221 222it ( 'should handle multiple values as comma separated list' , function () 223api . nvim_create_autocmd ( 'BufReadPost' , { 224pattern ='*.py,*.pyi' , 225command ="echo 'Should Not Have Errored'" , 226} ) 227 228-- We should have one autocmd for *.py and one for *.pyi 229eq ( 2 , # api . nvim_get_autocmds { event ='BufReadPost' } ) 230end ) 231 232it ( 'should handle multiple values as array' , function () 233api . nvim_create_autocmd ( 'BufReadPost' , { 234pattern ={ '*.py' , '*.pyi' } , 235command ="echo 'Should Not Have Errored'" , 236} ) 237 238-- We should have one autocmd for *.py and one for *.pyi 239eq ( 2 , # api . nvim_get_autocmds { event ='BufReadPost' } ) 240end ) 241 242describe ( 'desc' , function () 243it ( 'can add description to one autocmd' , function () 244local cmd ="echo 'Should Not Have Errored'" 245local desc ='Can show description' 246api . nvim_create_autocmd ( 'BufReadPost' , { 247pattern ='*.py' , 248command =cmd , 249desc =desc , 250} ) 251 252eq ( desc , api . nvim_get_autocmds { event ='BufReadPost' } [ 1 ]. desc ) 253eq ( cmd , api . nvim_get_autocmds { event ='BufReadPost' } [ 1 ]. command ) 254end ) 255 256it ( 'can add description to one autocmd that uses a callback' , function () 257local desc ='Can show description' 258api . nvim_set_var ( 'desc' , desc ) 259 260local result =exec_lua ( [[ 261local callback = function() print 'Should Not Have Errored' end 262vim.api.nvim_create_autocmd("BufReadPost", { 263pattern = "*.py", 264callback = callback, 265desc = vim.g.desc, 266}) 267local aus = vim.api.nvim_get_autocmds({ event = 'BufReadPost' }) 268local first = aus[1] 269return { 270desc = first.desc, 271cbtype = type(first.callback) 272} 273]] ) 274 275eq ( { desc =desc , cbtype ='function' } , result ) 276end ) 277 278it ( 'will not add a description unless it was provided' , function () 279exec_lua ( [[ 280local callback = function() print 'Should Not Have Errored' end 281vim.api.nvim_create_autocmd("BufReadPost", { 282pattern = "*.py", 283callback = callback, 284}) 285]] ) 286 287eq ( nil , api . nvim_get_autocmds ( { event ='BufReadPost' } )[ 1 ]. desc ) 288end ) 289 290it ( 'can add description to multiple autocmd' , function () 291api . nvim_create_autocmd ( 'BufReadPost' , { 292pattern ={ '*.py' , '*.pyi' } , 293command ="echo 'Should Not Have Errored'" , 294desc ='Can show description' , 295} ) 296 297local aus =api . nvim_get_autocmds { event ='BufReadPost' } 298eq ( 2 , # aus ) 299eq ( 'Can show description' , aus [ 1 ]. desc ) 300eq ( 'Can show description' , aus [ 2 ]. desc ) 301end ) 302end ) 303 304pending ( 'script and verbose settings' , function () 305it ( 'marks API client' , function () 306api . nvim_create_autocmd ( 'BufReadPost' , { 307pattern ='*.py' , 308command ="echo 'Should Not Have Errored'" , 309desc ='Can show description' , 310} ) 311 312local aus =api . nvim_get_autocmds { event ='BufReadPost' } 313eq ( 1 , # aus , aus ) 314end ) 315end ) 316 317it ( 'removes an autocommand if the callback returns true' , function () 318api . nvim_set_var ( 'some_condition' , false ) 319 320exec_lua [[ 321vim.api.nvim_create_autocmd("User", { 322pattern = "Test", 323desc = "A test autocommand", 324callback = function() 325return vim.g.some_condition 326end, 327}) 328]] 329 330api . nvim_exec_autocmds ( 'User' , { pattern ='Test' } ) 331 332local aus =api . nvim_get_autocmds ( { event ='User' , pattern ='Test' } ) 333local first =aus [ 1 ] 334eq ( true , first . id > 0 ) 335 336api . nvim_set_var ( 'some_condition' , true ) 337api . nvim_exec_autocmds ( 'User' , { pattern ='Test' } ) 338eq ( {} , api . nvim_get_autocmds ( { event ='User' , pattern ='Test' } )) 339end ) 340 341local function test_autocmd_args ( event ) 342local function get_amatch ( pat ) 343return event == 'User' and pat or vim . fs . normalize ( n . fn . fnamemodify ( pat , ':p' )) 344end 345 346local group_id =api . nvim_create_augroup ( 'TestGroup' , {} ) 347-- Having an existing autocmd calling expand("<afile>") shouldn't change args #18964 348api . nvim_create_autocmd ( event , { 349group ='TestGroup' , 350pattern ='Te*' , 351command ='call expand("<afile>")' , 352} ) 353 354local autocmd_id =exec_lua (( [[ 355return vim.api.nvim_create_autocmd(%q, { 356group = "TestGroup", 357pattern = "Te*", 358callback = function(args) 359vim.g.autocmd_args = args 360end, 361}) 362]] ): format ( event )) 363 364local exec_pat ='Test pattern' 365local amatch =get_amatch ( exec_pat ) 366api . nvim_exec_autocmds ( event , { pattern =exec_pat } ) 367eq ( { 368id =autocmd_id , 369group =group_id , 370event =event , 371match =amatch , 372file =exec_pat , 373win =api . nvim_get_current_win (), 374buf =1 , 375} , api . nvim_get_var ( 'autocmd_args' )) 376 377-- Test without a group 378autocmd_id =exec_lua (( [[ 379return vim.api.nvim_create_autocmd(%q, { 380pattern = "*", 381callback = function(args) 382vim.g.autocmd_args = args 383end, 384}) 385]] ): format ( event )) 386 387exec_pat ='some_pat' 388amatch =get_amatch ( exec_pat ) 389api . nvim_exec_autocmds ( event , { pattern =exec_pat } ) 390eq ( { 391id =autocmd_id , 392group =nil , 393event =event , 394match =amatch , 395file =exec_pat , 396win =api . nvim_get_current_win (), 397buf =1 , 398} , api . nvim_get_var ( 'autocmd_args' )) 399end 400 401describe ( 'receives correct args table' , function () 402it ( 'for event that takes non-file pattern' , function () 403test_autocmd_args ( 'User' ) 404end ) 405 406it ( 'for event that takes file pattern' , function () 407test_autocmd_args ( 'BufEnter' ) 408end ) 409end ) 410 411describe ( 'ev.win #25844' , function () 412it ( 'is the current window for events that are not about a window' , function () 413local win =api . nvim_get_current_win () 414exec_lua ( function () 415vim . api . nvim_create_autocmd ( 'BufAdd' , { 416callback =function ( ev ) 417vim . g . ev_win =ev . win 418end , 419} ) 420end ) 421command ( 'badd some_file' ) 422eq ( win , api . nvim_get_var ( 'ev_win' )) 423end ) 424 425it ( 'WinClosed reports the closed window' , function () 426command ( 'split' ) 427local closed_win =api . nvim_get_current_win () 428exec_lua ( function () 429vim . api . nvim_create_autocmd ( 'WinClosed' , { 430callback =function ( ev ) 431vim . g . ev_win =ev . win 432vim . g . ev_match =ev . match 433end , 434} ) 435end ) 436command ( 'quit' ) 437eq ( closed_win , api . nvim_get_var ( 'ev_win' )) 438eq ( tostring ( closed_win ), api . nvim_get_var ( 'ev_match' )) 439end ) 440 441it ( 'WinNew reports the new window, not the focused one' , function () 442local origin_win =api . nvim_get_current_win () 443local buf =api . nvim_create_buf ( false , true ) 444exec_lua ( function () 445vim . api . nvim_create_autocmd ( 'WinNew' , { 446callback =function ( ev ) 447vim . g . ev_win =ev . win 448end , 449} ) 450end ) 451local new_win =api . nvim_open_win ( buf , false , { 452relative ='editor' , 453row =0 , 454col =0 , 455width =10 , 456height =5 , 457} ) 458eq ( origin_win , api . nvim_get_current_win ()) 459eq ( new_win , api . nvim_get_var ( 'ev_win' )) 460end ) 461 462it ( 'WinResized reports the resized window, not curwin' , function () 463local top_win =api . nvim_get_current_win () 464command ( 'botright split' ) 465local bot_win =api . nvim_get_current_win () 466neq ( top_win , bot_win ) 467command ( 'redraw' ) -- flush the WinResized caused by the split itself 468exec_lua ( function () 469vim . api . nvim_create_autocmd ( 'WinResized' , { 470callback =function ( ev ) 471vim . g . ev_win =ev . win 472vim . g . ev_match =ev . match 473vim . g . ev_curwin =vim . api . nvim_get_current_win () 474end , 475} ) 476end ) 477command ( 'resize 3' ) 478command ( 'redraw' ) 479-- ev.win is the first window whose size changed, matching <amatch>. 480eq ( top_win , api . nvim_get_var ( 'ev_win' )) 481eq ( tostring ( top_win ), api . nvim_get_var ( 'ev_match' )) 482eq ( bot_win , api . nvim_get_var ( 'ev_curwin' )) 483end ) 484 485it ( 'WinScrolled reports the scrolled window, not curwin' , function () 486command ( 'split' ) 487local scrolled =api . nvim_get_current_win () 488exec_lua ( function () 489local lines ={} 490for i =1 , 30 do 491lines [ i ] ='line ' .. i 492end 493vim . api . nvim_buf_set_lines ( 0 , 0 , - 1 , false , lines ) 494end ) 495command ( 'wincmd w' ) 496neq ( scrolled , api . nvim_get_current_win ()) 497command ( 'redraw' ) 498exec_lua ( function ( scroll_win ) 499vim . api . nvim_create_autocmd ( 'WinScrolled' , { 500callback =function ( ev ) 501vim . g . ev_win =ev . win 502vim . g . ev_match =ev . match 503vim . g . ev_curwin =vim . api . nvim_get_current_win () 504end , 505} ) 506vim . api . nvim_win_call ( scroll_win , function () 507vim . cmd ( 'normal! G' ) 508end ) 509end , scrolled ) 510command ( 'redraw' ) 511eq ( scrolled , api . nvim_get_var ( 'ev_win' )) 512eq ( tostring ( scrolled ), api . nvim_get_var ( 'ev_match' )) 513neq ( scrolled , api . nvim_get_var ( 'ev_curwin' )) 514end ) 515 516it ( 'is restored after a nested autocmd' , function () 517command ( 'edit a' ) 518command ( 'rightbelow vsplit b' ) 519command ( 'wincmd w' ) 520local trigger_win =api . nvim_get_current_win () 521exec_lua ( function () 522-- Fires BufLeave/WinLeave/WinEnter/BufEnter, each with its own ev.win. 523vim . api . nvim_create_autocmd ( 'ColorScheme' , { 524callback =function () 525vim . cmd ( 'wincmd w' ) 526end , 527} ) 528vim . api . nvim_create_autocmd ( 'ColorScheme' , { 529callback =function ( ev ) 530vim . g . ev_win =ev . win 531end , 532} ) 533end ) 534command ( 'colorscheme blue' ) 535eq ( trigger_win , api . nvim_get_var ( 'ev_win' )) 536end ) 537end ) 538 539it ( 'can receive arbitrary data' , function () 540local function test ( data ) 541eq ( 542data , 543exec_lua ( 544[[ 545local input = ... 546local output 547vim.api.nvim_create_autocmd("User", { 548pattern = "Test", 549callback = function(ev) 550output = ev.data 551end, 552}) 553 554vim.api.nvim_exec_autocmds("User", { 555pattern = "Test", 556data = input, 557}) 558 559return output 560]] , 561data 562) 563) 564end 565 566test ( 'Hello' ) 567test ( 42 ) 568test ( true ) 569test ( { 'list' } ) 570test ( { foo ='bar' } ) 571end ) 572 573it ( 'function in arbitrary data is passed to all autocmds #28353' , function () 574eq ( 5751303 , 576exec_lua ( [[ 577local res = 1 578 579local fun = function(m, x) 580res = res * m + x 581end 582 583local group = vim.api.nvim_create_augroup('MyTest', { clear = false }) 584 585vim.api.nvim_create_autocmd('User', { 586group = group, 587callback = function(payload) 588payload.data.fun(10, payload.data.x) 589end, 590pattern = 'MyEvent', 591}) 592vim.api.nvim_create_autocmd('User', { 593group = group, 594callback = function(payload) 595payload.data.fun(100, payload.data.x) 596end, 597pattern = 'MyEvent', 598}) 599 600vim.api.nvim_exec_autocmds('User', { 601group = group, 602pattern = 'MyEvent', 603data = { x = 3, fun = fun }, 604}) 605 606return res 607]] ) 608) 609end ) 610end ) 611 612describe ( 'nvim_get_autocmds' , function () 613it ( 'validation' , function () 614eq ( 615"Invalid 'group': 9997999" , 616pcall_err ( api . nvim_get_autocmds , { 617group =9997999 , 618} ) 619) 620eq ( 621"Invalid 'group': 'bogus'" , 622pcall_err ( api . nvim_get_autocmds , { 623group ='bogus' , 624} ) 625) 626eq ( 627"Invalid 'group': 0" , 628pcall_err ( api . nvim_get_autocmds , { 629group =0 , 630} ) 631) 632eq ( 633"Invalid 'group': expected String or Integer, got Array" , 634pcall_err ( api . nvim_get_autocmds , { 635group ={} , 636} ) 637) 638eq ( 639"Invalid 'buffer': expected Integer or Array, got Boolean" , 640pcall_err ( api . nvim_get_autocmds , { 641buf =true , 642} ) 643) 644-- Deprecated `buffer` key still validated. 645eq ( 646"Invalid 'buffer': expected Integer or Array, got Boolean" , 647pcall_err ( api . nvim_get_autocmds , { 648buffer =true , 649} ) 650) 651-- Passing both `buf` and `buffer` is rejected. 652eq ( 653"Conflict: 'buf' not allowed with 'buffer'" , 654pcall_err ( api . nvim_get_autocmds , { 655buf =0 , 656buffer =1 , 657} ) 658) 659eq ( 660"Invalid 'event': expected String or Array" , 661pcall_err ( api . nvim_get_autocmds , { 662event =true , 663} ) 664) 665eq ( 666"Invalid 'pattern': expected String or Array, got Boolean" , 667pcall_err ( api . nvim_get_autocmds , { 668pattern =true , 669} ) 670) 671end ) 672 673describe ( 'events' , function () 674it ( 'returns one autocmd when there is only one for an event' , function () 675command [[au! InsertEnter]] 676command [[au InsertEnter * :echo "1"]] 677 678local aus =api . nvim_get_autocmds { event ='InsertEnter' } 679eq ( 1 , # aus ) 680end ) 681 682it ( 'returns two autocmds when there are two for an event' , function () 683command [[au! InsertEnter]] 684command [[au InsertEnter * :echo "1"]] 685command [[au InsertEnter * :echo "2"]] 686 687local aus =api . nvim_get_autocmds { event ='InsertEnter' } 688eq ( 2 , # aus ) 689end ) 690 691it ( 'returns the same thing if you use string or list' , function () 692command [[au! InsertEnter]] 693command [[au InsertEnter * :echo "1"]] 694command [[au InsertEnter * :echo "2"]] 695 696local string_aus =api . nvim_get_autocmds { event ='InsertEnter' } 697local array_aus =api . nvim_get_autocmds { event ={ 'InsertEnter' } } 698eq ( string_aus , array_aus ) 699end ) 700 701it ( 'returns two autocmds when there are two for an event' , function () 702command [[au! InsertEnter]] 703command [[au! InsertLeave]] 704command [[au InsertEnter * :echo "1"]] 705command [[au InsertEnter * :echo "2"]] 706 707local aus =api . nvim_get_autocmds { event ={ 'InsertEnter' , 'InsertLeave' } } 708eq ( 2 , # aus ) 709end ) 710 711it ( 'returns different IDs for different autocmds' , function () 712command [[au! InsertEnter]] 713command [[au! InsertLeave]] 714command [[au InsertEnter * :echo "1"]] 715source [[ 716call nvim_create_autocmd("InsertLeave", #{ 717\ command: ":echo 2", 718\ }) 719]] 720 721local aus =api . nvim_get_autocmds { event ={ 'InsertEnter' , 'InsertLeave' } } 722local first =aus [ 1 ] 723eq ( nil , first . id ) 724 725-- TODO: Maybe don't have this number, just assert it's not nil 726local second =aus [ 2 ] 727neq ( nil , second . id ) 728 729api . nvim_del_autocmd ( second . id ) 730local new_aus =api . nvim_get_autocmds { event ={ 'InsertEnter' , 'InsertLeave' } } 731eq ( 1 , # new_aus ) 732eq ( first , new_aus [ 1 ]) 733end ) 734 735it ( 'returns event name' , function () 736command [[au! InsertEnter]] 737command [[au InsertEnter * :echo "1"]] 738 739local aus =api . nvim_get_autocmds { event ='InsertEnter' } 740eq ( { 741{ 742buflocal =false , 743command =':echo "1"' , 744event ='InsertEnter' , 745once =false , 746pattern ='*' , 747} , 748} , aus ) 749end ) 750 751it ( 'works with buffer numbers' , function () 752command [[new]] 753command [[au! InsertEnter]] 754command [[au InsertEnter <buffer=1> :echo "1"]] 755command [[au InsertEnter <buffer=2> :echo "2"]] 756 757local aus =api . nvim_get_autocmds { event ='InsertEnter' , buf =0 } 758eq ( { 759{ 760buf =2 , 761buffer =2 , -- deprecated 762buflocal =true , 763command =':echo "2"' , 764event ='InsertEnter' , 765once =false , 766pattern ='<buffer=2>' , 767} , 768} , aus ) 769 770aus =api . nvim_get_autocmds { event ='InsertEnter' , buf =1 } 771eq ( { 772{ 773buf =1 , 774buffer =1 , -- deprecated 775buflocal =true , 776command =':echo "1"' , 777event ='InsertEnter' , 778once =false , 779pattern ='<buffer=1>' , 780} , 781} , aus ) 782 783aus =api . nvim_get_autocmds { event ='InsertEnter' , buf ={ 1 , 2 } } 784eq ( { 785{ 786buf =1 , 787buffer =1 , -- deprecated 788buflocal =true , 789command =':echo "1"' , 790event ='InsertEnter' , 791once =false , 792pattern ='<buffer=1>' , 793} , 794{ 795buf =2 , 796buffer =2 , -- deprecated 797buflocal =true , 798command =':echo "2"' , 799event ='InsertEnter' , 800once =false , 801pattern ='<buffer=2>' , 802} , 803} , aus ) 804 805-- Deprecated `buffer` key still works. 806eq ( 807api . nvim_get_autocmds { event ='InsertEnter' , buf ={ 1 , 2 } } , 808api . nvim_get_autocmds { event ='InsertEnter' , buffer ={ 1 , 2 } } 809) 810 811eq ( 812"Invalid 'buffer': expected Integer or Array, got String" , 813pcall_err ( api . nvim_get_autocmds , { event ='InsertEnter' , buf ='foo' } ) 814) 815eq ( 816"Invalid 'buffer': expected Integer, got String" , 817pcall_err ( api . nvim_get_autocmds , { event ='InsertEnter' , buf ={ 'foo' , 42 } } ) 818) 819eq ( 820'Invalid buffer id: 42' , 821pcall_err ( api . nvim_get_autocmds , { event ='InsertEnter' , buf ={ 42 } } ) 822) 823 824local bufs ={} 825for _ =1 , 257 do 826table . insert ( bufs , api . nvim_create_buf ( true , false )) 827end 828 829eq ( 830'Too many buffers (maximum of 256)' , 831pcall_err ( api . nvim_get_autocmds , { event ='InsertEnter' , buf =bufs } ) 832) 833end ) 834 835it ( 'returns autocmds when group is specified by id' , function () 836local auid =api . nvim_create_augroup ( 'nvim_test_augroup' , { clear =true } ) 837api . nvim_create_autocmd ( 'FileType' , { group =auid , command ='echo "1"' } ) 838api . nvim_create_autocmd ( 'FileType' , { group =auid , command ='echo "2"' } ) 839 840local aus =api . nvim_get_autocmds { group =auid } 841eq ( 2 , # aus ) 842 843local aus2 =api . nvim_get_autocmds { group =auid , event ='InsertEnter' } 844eq ( 0 , # aus2 ) 845end ) 846 847it ( 'returns autocmds when group is specified by name' , function () 848local auname ='nvim_test_augroup' 849api . nvim_create_augroup ( auname , { clear =true } ) 850api . nvim_create_autocmd ( 'FileType' , { group =auname , command ='echo "1"' } ) 851api . nvim_create_autocmd ( 'FileType' , { group =auname , command ='echo "2"' } ) 852 853local aus =api . nvim_get_autocmds { group =auname } 854eq ( 2 , # aus ) 855 856local aus2 =api . nvim_get_autocmds { group =auname , event ='InsertEnter' } 857eq ( 0 , # aus2 ) 858end ) 859 860it ( 'should respect nested' , function () 861local bufs =exec_lua [[ 862local count = 0 863vim.api.nvim_create_autocmd("BufNew", { 864once = false, 865nested = true, 866callback = function() 867count = count + 1 868if count > 5 then 869return true 870end 871 872vim.cmd(string.format("new README_%s.md", count)) 873end 874}) 875 876vim.cmd "new First.md" 877 878return vim.api.nvim_list_bufs() 879]] 880 881-- 1 for the first buffer 882-- 2 for First.md 883-- 3-7 for the 5 we make in the autocmd 884eq ( { 1 , 2 , 3 , 4 , 5 , 6 , 7 } , bufs ) 885end ) 886 887it ( 'can retrieve a callback from an autocmd' , function () 888local content ='I Am A Callback' 889api . nvim_set_var ( 'content' , content ) 890exec_lua ( [[ 891local cb = function() return vim.g.content end 892vim.api.nvim_create_autocmd("User", { 893pattern = "TestTrigger", 894desc = "A test autocommand with a callback", 895callback = cb, 896}) 897]] ) 898 899local result =exec_lua ( [[ 900local aus = vim.api.nvim_get_autocmds({ event = 'User', pattern = 'TestTrigger' }) 901local first = aus[1] 902return { 903cb = { 904type = type(first.callback), 905can_retrieve = first.callback() == vim.g.content 906} 907} 908]] ) 909eq ( { cb ={ type ='function' , can_retrieve =true } } , result ) 910 911-- Also test with Vimscript 912source ( [[ 913let s:aus = nvim_get_autocmds({'event': 'User', 'pattern': 'TestTrigger'}) 914let g:result = s:aus[0].callback() 915]] ) 916eq ( content , api . nvim_get_var ( 'result' )) 917end ) 918 919it ( 920'will return an empty string as the command for an autocmd that uses a callback' , 921function () 922local result =exec_lua ( [[ 923local callback = function() print 'I Am A Callback' end 924vim.api.nvim_create_autocmd("BufWritePost", { 925pattern = "*.py", 926callback = callback, 927}) 928local aus = vim.api.nvim_get_autocmds({ event = 'BufWritePost' }) 929local first = aus[1] 930return { 931command = first.command, 932cbtype = type(first.callback) 933} 934]] ) 935 936eq ( { command ='' , cbtype ='function' } , result ) 937end 938) 939end ) 940 941describe ( 'groups' , function () 942before_each ( function () 943command [[au! InsertEnter]] 944 945command [[au InsertEnter * :echo "No Group"]] 946 947command [[augroup GroupOne]] 948command [[ au InsertEnter * :echo "GroupOne:1"]] 949command [[augroup END]] 950 951command [[augroup GroupTwo]] 952command [[ au InsertEnter * :echo "GroupTwo:2"]] 953command [[ au InsertEnter * :echo "GroupTwo:3"]] 954command [[augroup END]] 955end ) 956 957it ( 'returns all groups if no group is specified' , function () 958local aus =api . nvim_get_autocmds { event ='InsertEnter' } 959if # aus ~= 4 then 960eq ( {} , aus ) 961end 962 963eq ( 4 , # aus ) 964end ) 965 966it ( 'returns only the group specified' , function () 967local aus =api . nvim_get_autocmds { 968event ='InsertEnter' , 969group ='GroupOne' , 970} 971 972eq ( 1 , # aus ) 973eq ( [[:echo "GroupOne:1"]] , aus [ 1 ]. command ) 974eq ( 'GroupOne' , aus [ 1 ]. group_name ) 975end ) 976 977it ( 'returns only the group specified, multiple values' , function () 978local aus =api . nvim_get_autocmds { 979event ='InsertEnter' , 980group ='GroupTwo' , 981} 982 983eq ( 2 , # aus ) 984eq ( [[:echo "GroupTwo:2"]] , aus [ 1 ]. command ) 985eq ( 'GroupTwo' , aus [ 1 ]. group_name ) 986eq ( [[:echo "GroupTwo:3"]] , aus [ 2 ]. command ) 987eq ( 'GroupTwo' , aus [ 2 ]. group_name ) 988end ) 989end ) 990 991describe ( 'groups: 2' , function () 992it ( 'raises error for undefined augroup name' , function () 993local success , code =unpack ( api . nvim_exec_lua ( 994[[ 995return {pcall(function() 996vim.api.nvim_create_autocmd("FileType", { 997pattern = "*", 998group = "NotDefined", 999command = "echo 'hello'", 1000}) 1001end)} 1002]] , 1003{} 1004)) 1005 1006eq ( false , success ) 1007matches ( "Invalid 'group': 'NotDefined'" , code ) 1008end ) 1009 1010it ( 'raises error for undefined augroup id' , function () 1011local success , code =unpack ( api . nvim_exec_lua ( 1012[[ 1013return {pcall(function() 1014-- Make sure the augroup is deleted 1015vim.api.nvim_del_augroup_by_id(1) 1016 1017vim.api.nvim_create_autocmd("FileType", { 1018pattern = "*", 1019group = 1, 1020command = "echo 'hello'", 1021}) 1022end)} 1023]] , 1024{} 1025)) 1026 1027eq ( false , success ) 1028matches ( "Invalid 'group': 1" , code ) 1029end ) 1030 1031it ( 'raises error for invalid group type' , function () 1032local success , code =unpack ( api . nvim_exec_lua ( 1033[[ 1034return {pcall(function() 1035vim.api.nvim_create_autocmd("FileType", { 1036pattern = "*", 1037group = true, 1038command = "echo 'hello'", 1039}) 1040end)} 1041]] , 1042{} 1043)) 1044 1045eq ( false , success ) 1046matches ( "Invalid 'group': expected String or Integer, got Boolean" , code ) 1047end ) 1048 1049it ( 'raises error for invalid pattern array' , function () 1050local success , code =unpack ( api . nvim_exec_lua ( 1051[[ 1052return {pcall(function() 1053vim.api.nvim_create_autocmd("FileType", { 1054pattern = {{}}, 1055command = "echo 'hello'", 1056}) 1057end)} 1058]] , 1059{} 1060)) 1061 1062eq ( false , success ) 1063matches ( "Invalid 'pattern' item: expected String, got Array" , code ) 1064end ) 1065end ) 1066 1067describe ( 'patterns' , function () 1068before_each ( function () 1069command [[au! InsertEnter]] 1070 1071command [[au InsertEnter * :echo "No Group"]] 1072command [[au InsertEnter *.one :echo "GroupOne:1"]] 1073command [[au InsertEnter *.two :echo "GroupTwo:2"]] 1074command [[au InsertEnter *.two :echo "GroupTwo:3"]] 1075command [[au InsertEnter <buffer> :echo "Buffer"]] 1076end ) 1077 1078it ( 'returns for literal match' , function () 1079local aus =api . nvim_get_autocmds { 1080event ='InsertEnter' , 1081pattern ='*' , 1082} 1083 1084eq ( 1 , # aus ) 1085eq ( [[:echo "No Group"]] , aus [ 1 ]. command ) 1086end ) 1087 1088it ( 'returns for multiple matches' , function () 1089-- vim.api.nvim_get_autocmds 1090local aus =api . nvim_get_autocmds { 1091event ='InsertEnter' , 1092pattern ={ '*.one' , '*.two' } , 1093} 1094 1095eq ( 3 , # aus ) 1096eq ( [[:echo "GroupOne:1"]] , aus [ 1 ]. command ) 1097eq ( [[:echo "GroupTwo:2"]] , aus [ 2 ]. command ) 1098eq ( [[:echo "GroupTwo:3"]] , aus [ 3 ]. command ) 1099end ) 1100 1101it ( 'should work for buffer autocmds' , function () 1102local normalized_aus =api . nvim_get_autocmds { 1103event ='InsertEnter' , 1104pattern ='<buffer=1>' , 1105} 1106 1107local raw_aus =api . nvim_get_autocmds { 1108event ='InsertEnter' , 1109pattern ='<buffer>' , 1110} 1111 1112local zero_aus =api . nvim_get_autocmds { 1113event ='InsertEnter' , 1114pattern ='<buffer=0>' , 1115} 1116 1117eq ( normalized_aus , raw_aus ) 1118eq ( normalized_aus , zero_aus ) 1119eq ( [[:echo "Buffer"]] , normalized_aus [ 1 ]. command ) 1120end ) 1121end ) 1122 1123describe ( 'id' , function () 1124it ( 'gets events by ID' , function () 1125local id =api . nvim_create_autocmd ( 'BufEnter' , { 1126command ='echo "hello"' , 1127} ) 1128eq ( { 1129{ 1130buflocal =false , 1131command ='echo "hello"' , 1132event ='BufEnter' , 1133id =id , 1134once =false , 1135pattern ='*' , 1136} , 1137} , api . nvim_get_autocmds ( { id =id } )) 1138end ) 1139 1140it ( 'gets events by ID by other filters' , function () 1141local group_name ='NVIM_GET_AUTOCMDS_ID' 1142local group =api . nvim_create_augroup ( group_name , { clear =true } ) 1143local id =api . nvim_create_autocmd ( 'BufEnter' , { 1144command ='set number' , 1145group =group , 1146} ) 1147api . nvim_create_autocmd ( 'WinEnter' , { 1148group =group , 1149command ='set cot&' , 1150} ) 1151eq ( { 1152{ 1153buflocal =false , 1154command ='set number' , 1155event ='BufEnter' , 1156group =group , 1157group_name =group_name , 1158id =id , 1159once =false , 1160pattern ='*' , 1161} , 1162} , api . nvim_get_autocmds ( { id =id , group =group } )) 1163end ) 1164 1165it ( 'gets events by ID and a specific event' , function () 1166local id =api . nvim_create_autocmd ( 'InsertEnter' , { command ='set number' } ) 1167api . nvim_create_autocmd ( 'InsertEnter' , { command ='set wrap' } ) 1168eq ( { 1169{ 1170buflocal =false , 1171command ='set number' , 1172event ='InsertEnter' , 1173id =id , 1174once =false , 1175pattern ='*' , 1176} , 1177} , api . nvim_get_autocmds ( { id =id , event ='InsertEnter' } )) 1178end ) 1179 1180it ( 'gets events by ID and a specific pattern' , function () 1181local id =api . nvim_create_autocmd ( 'InsertEnter' , { 1182pattern ='*.c' , 1183command ='set number' , 1184} ) 1185api . nvim_create_autocmd ( 'InsertEnter' , { 1186pattern ='*.c' , 1187command ='set wrap' , 1188} ) 1189eq ( { 1190{ 1191buflocal =false , 1192command ='set number' , 1193event ='InsertEnter' , 1194id =id , 1195once =false , 1196pattern ='*.c' , 1197} , 1198} , api . nvim_get_autocmds ( { id =id , pattern ='*.c' } )) 1199end ) 1200 1201it ( 'empty result when id does not found' , function () 1202eq ( {} , api . nvim_get_autocmds ( { id =255 } )) 1203end ) 1204end ) 1205end ) 1206 1207describe ( 'nvim_exec_autocmds' , function () 1208it ( 'validation' , function () 1209eq ( 1210"Invalid 'group': 9997999" , 1211pcall_err ( api . nvim_exec_autocmds , 'FileType' , { 1212group =9997999 , 1213} ) 1214) 1215eq ( 1216"Invalid 'group': 'bogus'" , 1217pcall_err ( api . nvim_exec_autocmds , 'FileType' , { 1218group ='bogus' , 1219} ) 1220) 1221eq ( 1222"Invalid 'group': expected String or Integer, got Array" , 1223pcall_err ( api . nvim_exec_autocmds , 'FileType' , { 1224group ={} , 1225} ) 1226) 1227eq ( 1228"Invalid 'group': 0" , 1229pcall_err ( api . nvim_exec_autocmds , 'FileType' , { 1230group =0 , 1231} ) 1232) 1233eq ( 1234"Invalid 'buf': expected Buffer, got Array" , 1235pcall_err ( api . nvim_exec_autocmds , 'FileType' , { 1236buf ={} , 1237} ) 1238) 1239-- Deprecated `buffer` key still validated. 1240eq ( 1241"Invalid 'buffer': expected Buffer, got Array" , 1242pcall_err ( api . nvim_exec_autocmds , 'FileType' , { 1243buffer ={} , 1244} ) 1245) 1246-- Passing both `buf` and `buffer` is rejected. 1247eq ( 1248"Conflict: 'buf' not allowed with 'buffer'" , 1249pcall_err ( api . nvim_exec_autocmds , 'FileType' , { 1250buf =0 , 1251buffer =1 , 1252} ) 1253) 1254eq ( 1255"Invalid 'event' item: expected String, got Array" , 1256pcall_err ( api . nvim_exec_autocmds , { 'FileType' , {} } , {} ) 1257) 1258matches ( 1259"Invalid 'event': expected Array or String, got nil$" , 1260pcall_err ( exec_lua , function () 1261vim . api . nvim_exec_autocmds ( nil , {} ) 1262end ) 1263) 1264end ) 1265 1266it ( 'can trigger builtin autocmds' , function () 1267api . nvim_set_var ( 'autocmd_executed' , false ) 1268 1269api . nvim_create_autocmd ( 'BufReadPost' , { 1270pattern ='*' , 1271command ='let g:autocmd_executed = v:true' , 1272} ) 1273 1274eq ( false , api . nvim_get_var ( 'autocmd_executed' )) 1275api . nvim_exec_autocmds ( 'BufReadPost' , {} ) 1276eq ( true , api . nvim_get_var ( 'autocmd_executed' )) 1277end ) 1278 1279it ( 'can trigger multiple patterns' , function () 1280api . nvim_set_var ( 'autocmd_executed' , 0 ) 1281 1282api . nvim_create_autocmd ( 'BufReadPost' , { 1283pattern ='*' , 1284command ='let g:autocmd_executed += 1' , 1285} ) 1286 1287api . nvim_exec_autocmds ( 'BufReadPost' , { pattern ={ '*.lua' , '*.vim' } } ) 1288eq ( 2 , api . nvim_get_var ( 'autocmd_executed' )) 1289 1290api . nvim_create_autocmd ( 'BufReadPre' , { 1291pattern ={ 'bar' , 'foo' } , 1292command ='let g:autocmd_executed += 10' , 1293} ) 1294 1295api . nvim_exec_autocmds ( 'BufReadPre' , { pattern ={ 'foo' , 'bar' , 'baz' , 'frederick' } } ) 1296eq ( 22 , api . nvim_get_var ( 'autocmd_executed' )) 1297 1298eq ( '' , api . nvim_buf_get_name ( 0 )) 1299api . nvim_exec_autocmds ( { 'BufReadPre' , 'BufReadPost' } , {} ) 1300eq ( 23 , api . nvim_get_var ( 'autocmd_executed' )) 1301 1302api . nvim_buf_set_name ( 0 , 'bar' ) 1303api . nvim_exec_autocmds ( { 'BufReadPre' , 'BufReadPost' } , {} ) 1304eq ( 34 , api . nvim_get_var ( 'autocmd_executed' )) 1305 1306-- Empty event array matches (and executes) nothing. 1307api . nvim_exec_autocmds ( {} , {} ) 1308eq ( 34 , api . nvim_get_var ( 'autocmd_executed' )) 1309 1310-- Non-nil, fully-empty patterns execute nothing. 1311api . nvim_exec_autocmds ( 'BufReadPre' , { pattern ='' } ) 1312api . nvim_exec_autocmds ( 'BufReadPre' , { pattern =',,,,' } ) 1313api . nvim_exec_autocmds ( 'BufReadPre' , { pattern ={} } ) 1314api . nvim_exec_autocmds ( 'BufReadPre' , { pattern ={ ',' , '' } } ) 1315eq ( 34 , api . nvim_get_var ( 'autocmd_executed' )) 1316end ) 1317 1318it ( 'can pass the buffer' , function () 1319api . nvim_set_var ( 'buffer_executed' , - 1 ) 1320eq ( - 1 , api . nvim_get_var ( 'buffer_executed' )) 1321 1322api . nvim_create_autocmd ( 'BufLeave' , { 1323pattern ='*' , 1324command ='let g:buffer_executed = +expand("<abuf>")' , 1325} ) 1326 1327-- Doesn't execute for other non-matching events 1328api . nvim_exec_autocmds ( 'CursorHold' , { buf =1 } ) 1329eq ( - 1 , api . nvim_get_var ( 'buffer_executed' )) 1330 1331-- Deprecated `buffer` key still works. 1332api . nvim_exec_autocmds ( 'BufLeave' , { buf =1 } ) 1333eq ( 1 , api . nvim_get_var ( 'buffer_executed' )) 1334api . nvim_set_var ( 'buffer_executed' , - 1 ) 1335api . nvim_exec_autocmds ( 'BufLeave' , { buffer =1 } ) 1336eq ( 1 , api . nvim_get_var ( 'buffer_executed' )) 1337end ) 1338 1339it ( 'executes in target buf context for one visible window' , function () 1340local caller =api . nvim_get_current_buf () 1341local caller_win =api . nvim_get_current_win () 1342command ( 'split' ) 1343local target =api . nvim_create_buf ( true , false ) 1344api . nvim_set_current_buf ( target ) 1345local target_win =api . nvim_get_current_win () 1346api . nvim_set_current_win ( caller_win ) 1347 1348api . nvim_create_autocmd ( 'CursorHold' , { 1349buffer =target , 1350command ='let [g:abuf, g:cur, g:win] = [str2nr(expand("<abuf>")), bufnr("%"), win_getid()]' , 1351} ) 1352api . nvim_exec_autocmds ( 'CursorHold' , { buf =target } ) 1353 1354eq ( target , api . nvim_get_var ( 'abuf' )) 1355eq ( target , api . nvim_get_var ( 'cur' )) 1356eq ( target_win , api . nvim_get_var ( 'win' )) 1357eq ( caller , api . nvim_get_current_buf ()) 1358eq ( caller_win , api . nvim_get_current_win ()) 1359end ) 1360 1361it ( 'executes in target buf context for a hidden buffer' , function () 1362local caller =api . nvim_get_current_buf () 1363local caller_win =api . nvim_get_current_win () 1364local target =api . nvim_create_buf ( true , false ) 1365 1366api . nvim_create_autocmd ( 'User' , { 1367buffer =target , 1368command ='let [g:abuf, g:cur] = [str2nr(expand("<abuf>")), bufnr("%")]' , 1369} ) 1370api . nvim_exec_autocmds ( 'User' , { buf =target } ) 1371 1372eq ( target , api . nvim_get_var ( 'abuf' )) 1373eq ( target , api . nvim_get_var ( 'cur' )) 1374eq ( caller , api . nvim_get_current_buf ()) 1375eq ( caller_win , api . nvim_get_current_win ()) 1376end ) 1377 1378it ( 'ignores modelines when executing for a passed buffer' , function () 1379local caller =api . nvim_get_current_buf () 1380local target =api . nvim_create_buf ( true , false ) 1381api . nvim_buf_set_lines ( caller , 0 , - 1 , false , { 'x' , '/* vim: set textwidth=23: */' } ) 1382api . nvim_buf_set_lines ( target , 0 , - 1 , false , { 'x' , '/* vim: set textwidth=17: */' } ) 1383api . nvim_set_option_value ( 'textwidth' , 0 , { buf =caller } ) 1384api . nvim_set_option_value ( 'textwidth' , 0 , { buf =target } ) 1385 1386api . nvim_create_autocmd ( 'User' , { buffer =target , command ='' } ) 1387api . nvim_exec_autocmds ( 'User' , { buf =target , modeline =true } ) 1388 1389eq ( 0 , api . nvim_get_option_value ( 'textwidth' , { buf =caller } )) 1390eq ( 0 , api . nvim_get_option_value ( 'textwidth' , { buf =target } )) 1391end ) 1392 1393it ( 'restores caller if target buffer is wiped during execution' , function () 1394local caller =api . nvim_get_current_buf () 1395local caller_win =api . nvim_get_current_win () 1396local target =api . nvim_create_buf ( true , false ) 1397 1398api . nvim_create_autocmd ( 'User' , { 1399buffer =target , 1400command ='let g:seen = bufnr("%") | execute "bwipeout!" expand("<abuf>")' , 1401} ) 1402api . nvim_exec_autocmds ( 'User' , { buf =target } ) 1403 1404eq ( target , api . nvim_get_var ( 'seen' )) 1405eq ( false , api . nvim_buf_is_valid ( target )) 1406eq ( caller , api . nvim_get_current_buf ()) 1407eq ( caller_win , api . nvim_get_current_win ()) 1408end ) 1409 1410it ( 'restores caller if callback changes current buffer' , function () 1411local caller =api . nvim_get_current_buf () 1412local caller_win =api . nvim_get_current_win () 1413local target =api . nvim_create_buf ( true , false ) 1414local other =api . nvim_create_buf ( true , false ) 1415command ( 'split' ) 1416api . nvim_set_current_buf ( target ) 1417local target_win =api . nvim_get_current_win () 1418api . nvim_set_current_win ( caller_win ) 1419 1420api . nvim_create_autocmd ( 'User' , { 1421buffer =target , 1422command =string . format ( 1423'let g:seen = bufnr("%%") | buffer %d | let g:changed = bufnr("%%")' , 1424other 1425), 1426} ) 1427api . nvim_exec_autocmds ( 'User' , { buf =target } ) 1428 1429eq ( target , api . nvim_get_var ( 'seen' )) 1430eq ( other , api . nvim_get_var ( 'changed' )) 1431eq ( caller , api . nvim_get_current_buf ()) 1432eq ( caller_win , api . nvim_get_current_win ()) 1433eq ( target , api . nvim_win_get_buf ( target_win )) 1434end ) 1435 1436it ( 'restores nested target buffer contexts' , function () 1437local caller =api . nvim_get_current_buf () 1438local caller_win =api . nvim_get_current_win () 1439local target =api . nvim_create_buf ( true , false ) 1440local other =api . nvim_create_buf ( true , false ) 1441 1442api . nvim_create_autocmd ( 'User' , { 1443buffer =other , 1444command ='let g:inner = bufnr("%")' , 1445} ) 1446api . nvim_create_autocmd ( 'User' , { 1447buffer =target , 1448nested =true , 1449command =string . format ( 1450'let g:outer = bufnr("%%")' 1451.. ' | call nvim_exec_autocmds("User", #{buf: %d})' 1452.. ' | let g:outer_after = bufnr("%%")' , 1453other 1454), 1455} ) 1456api . nvim_exec_autocmds ( 'User' , { buf =target } ) 1457 1458eq ( target , api . nvim_get_var ( 'outer' )) 1459eq ( other , api . nvim_get_var ( 'inner' )) 1460eq ( target , api . nvim_get_var ( 'outer_after' )) 1461eq ( caller , api . nvim_get_current_buf ()) 1462eq ( caller_win , api . nvim_get_current_win ()) 1463end ) 1464 1465it ( 'respects eventignorewin across multiple windows' , function () 1466local caller_win =api . nvim_get_current_win () 1467local target =api . nvim_create_buf ( false , false ) 1468command ( 'split' ) 1469local w_allows =api . nvim_get_current_win () 1470api . nvim_win_set_buf ( w_allows , target ) 1471command ( 'vsplit' ) 1472local w_ignores =api . nvim_get_current_win () 1473api . nvim_win_set_buf ( w_ignores , target ) 1474api . nvim_set_current_win ( caller_win ) 1475 1476api . nvim_create_autocmd ( 'CursorHold' , { 1477buffer =target , 1478command ='let g:fired += 1' , 1479} ) 1480api . nvim_set_option_value ( 'eventignorewin' , '' , { win =w_allows } ) 1481api . nvim_set_option_value ( 'eventignorewin' , 'CursorHold' , { win =w_ignores } ) 1482 1483api . nvim_set_var ( 'fired' , 0 ) 1484api . nvim_exec_autocmds ( 'CursorHold' , { buf =target } ) 1485eq ( 1 , api . nvim_get_var ( 'fired' )) 1486 1487api . nvim_set_option_value ( 'eventignorewin' , 'CursorHold' , { win =w_allows } ) 1488api . nvim_set_var ( 'fired' , 0 ) 1489api . nvim_exec_autocmds ( 'CursorHold' , { buf =target } ) 1490eq ( 0 , api . nvim_get_var ( 'fired' )) 1491eq ( caller_win , api . nvim_get_current_win ()) 1492end ) 1493 1494it ( 'can pass the filename, pattern match' , function () 1495api . nvim_set_var ( 'filename_executed' , 'none' ) 1496eq ( 'none' , api . nvim_get_var ( 'filename_executed' )) 1497 1498api . nvim_create_autocmd ( 'BufEnter' , { 1499pattern ='*.py' , 1500command ='let g:filename_executed = expand("<afile>")' , 1501} ) 1502 1503-- Doesn't execute for other non-matching events 1504api . nvim_exec_autocmds ( 'CursorHold' , { buf =1 } ) 1505eq ( 'none' , api . nvim_get_var ( 'filename_executed' )) 1506 1507command ( 'edit __init__.py' ) 1508eq ( '__init__.py' , api . nvim_get_var ( 'filename_executed' )) 1509end ) 1510 1511it ( 'cannot pass buf and fname' , function () 1512local ok =pcall ( 1513api . nvim_exec_autocmds , 1514'BufReadPre' , 1515{ pattern ='literally_cannot_error.rs' , buf =1 } 1516) 1517eq ( false , ok ) 1518 1519-- Same conflict via deprecated `buffer` key. 1520ok =pcall ( 1521api . nvim_exec_autocmds , 1522'BufReadPre' , 1523{ pattern ='literally_cannot_error.rs' , buffer =1 } 1524) 1525eq ( false , ok ) 1526end ) 1527 1528it ( 'can pass the filename, exact match' , function () 1529api . nvim_set_var ( 'filename_executed' , 'none' ) 1530eq ( 'none' , api . nvim_get_var ( 'filename_executed' )) 1531 1532command ( 'edit other_file.txt' ) 1533command ( 'edit __init__.py' ) 1534eq ( 'none' , api . nvim_get_var ( 'filename_executed' )) 1535 1536api . nvim_create_autocmd ( 'CursorHoldI' , { 1537pattern ='__init__.py' , 1538command ='let g:filename_executed = expand("<afile>")' , 1539} ) 1540 1541-- Doesn't execute for other non-matching events 1542api . nvim_exec_autocmds ( 'CursorHoldI' , { buf =1 } ) 1543eq ( 'none' , api . nvim_get_var ( 'filename_executed' )) 1544 1545api . nvim_exec_autocmds ( 'CursorHoldI' , { buf =api . nvim_get_current_buf () } ) 1546eq ( '__init__.py' , api . nvim_get_var ( 'filename_executed' )) 1547 1548-- Reset filename 1549api . nvim_set_var ( 'filename_executed' , 'none' ) 1550 1551api . nvim_exec_autocmds ( 'CursorHoldI' , { pattern ='__init__.py' } ) 1552eq ( '__init__.py' , api . nvim_get_var ( 'filename_executed' )) 1553end ) 1554 1555it ( 'works with user autocmds' , function () 1556api . nvim_set_var ( 'matched' , 'none' ) 1557 1558api . nvim_create_autocmd ( 'User' , { 1559pattern ='TestCommand' , 1560command ='let g:matched = "matched"' , 1561} ) 1562 1563api . nvim_exec_autocmds ( 'User' , { pattern ='OtherCommand' } ) 1564eq ( 'none' , api . nvim_get_var ( 'matched' )) 1565api . nvim_exec_autocmds ( 'User' , { pattern ='TestCommand' } ) 1566eq ( 'matched' , api . nvim_get_var ( 'matched' )) 1567end ) 1568 1569it ( 'can pass group by id' , function () 1570api . nvim_set_var ( 'group_executed' , false ) 1571 1572local auid =api . nvim_create_augroup ( 'nvim_test_augroup' , { clear =true } ) 1573api . nvim_create_autocmd ( 'FileType' , { 1574group =auid , 1575command ='let g:group_executed = v:true' , 1576} ) 1577 1578eq ( false , api . nvim_get_var ( 'group_executed' )) 1579api . nvim_exec_autocmds ( 'FileType' , { group =auid } ) 1580eq ( true , api . nvim_get_var ( 'group_executed' )) 1581end ) 1582 1583it ( 'can pass group by name' , function () 1584api . nvim_set_var ( 'group_executed' , false ) 1585 1586local auname ='nvim_test_augroup' 1587api . nvim_create_augroup ( auname , { clear =true } ) 1588api . nvim_create_autocmd ( 'FileType' , { 1589group =auname , 1590command ='let g:group_executed = v:true' , 1591} ) 1592 1593eq ( false , api . nvim_get_var ( 'group_executed' )) 1594api . nvim_exec_autocmds ( 'FileType' , { group =auname } ) 1595eq ( true , api . nvim_get_var ( 'group_executed' )) 1596end ) 1597 1598it ( 'redraw restores curwin temporarily without clearing aucmd_win' , function () 1599local win =api . nvim_get_current_win () 1600local buf =api . nvim_create_buf ( true , true ) 1601exec_lua ( [[ 1602vim.api.nvim_set_decoration_provider(vim.api.nvim_create_namespace(''), { 1603on_start = function() 1604_G.win = vim.api.nvim_get_current_win() 1605end 1606}) 1607]] ) 1608api . nvim_create_autocmd ( 'User' , { command ='let w:x=1|redraw!|let g:x=w:x' } ) 1609api . nvim_exec_autocmds ( 'User' , { buf =buf } ) 1610eq ( 1000 , exec_lua ( 'return _G.win' )) 1611eq ( 1 , n . eval ( 'g:x' )) 1612end ) 1613end ) 1614 1615describe ( 'nvim_create_augroup' , function () 1616before_each ( function () 1617api . nvim_set_var ( 'executed' , 0 ) 1618end ) 1619 1620local make_counting_autocmd =function ( opts ) 1621opts =opts or {} 1622 1623local resulting ={ 1624pattern ='*' , 1625command ='let g:executed = g:executed + 1' , 1626} 1627 1628resulting . group =opts . group 1629resulting . once =opts . once 1630 1631api . nvim_create_autocmd ( 'FileType' , resulting ) 1632end 1633 1634local set_ft =function ( ft ) 1635ft =ft or 'txt' 1636source ( string . format ( 'set filetype=%s' , ft )) 1637end 1638 1639local get_executed_count =function () 1640return api . nvim_get_var ( 'executed' ) 1641end 1642 1643it ( 'can be added in a group' , function () 1644local augroup ='TestGroup' 1645api . nvim_create_augroup ( augroup , { clear =true } ) 1646make_counting_autocmd { group =augroup } 1647 1648set_ft ( 'txt' ) 1649set_ft ( 'python' ) 1650 1651eq ( 2 , get_executed_count ()) 1652end ) 1653 1654it ( 'works getting called multiple times' , function () 1655make_counting_autocmd () 1656set_ft () 1657set_ft () 1658set_ft () 1659 1660eq ( 3 , get_executed_count ()) 1661end ) 1662 1663it ( 'handles ++once' , function () 1664make_counting_autocmd { once =true } 1665set_ft ( 'txt' ) 1666set_ft ( 'help' ) 1667set_ft ( 'txt' ) 1668set_ft ( 'help' ) 1669 1670eq ( 1 , get_executed_count ()) 1671end ) 1672 1673it ( 'errors on unexpected keys' , function () 1674local success , code =pcall ( api . nvim_create_autocmd , 'FileType' , { 1675pattern ='*' , 1676not_a_valid_key ='NotDefined' , 1677} ) 1678 1679eq ( false , success ) 1680matches ( 'not_a_valid_key' , code ) 1681end ) 1682 1683it ( 'can execute simple callback' , function () 1684exec_lua ( 1685[[ 1686vim.g.executed = false 1687 1688vim.api.nvim_create_autocmd("FileType", { 1689pattern = "*", 1690callback = function() vim.g.executed = true end, 1691}) 1692]] , 1693{} 1694) 1695 1696eq ( 1697true , 1698exec_lua ( 1699[[ 1700vim.cmd "set filetype=txt" 1701return vim.g.executed 1702]] , 1703{} 1704) 1705) 1706end ) 1707 1708it ( 'calls multiple lua callbacks for the same autocmd execution' , function () 1709eq ( 17104 , 1711exec_lua ( 1712[[ 1713local count = 0 1714local counter = function() 1715count = count + 1 1716end 1717 1718vim.api.nvim_create_autocmd("FileType", { 1719pattern = "*", 1720callback = counter, 1721}) 1722 1723vim.api.nvim_create_autocmd("FileType", { 1724pattern = "*", 1725callback = counter, 1726}) 1727 1728vim.cmd "set filetype=txt" 1729vim.cmd "set filetype=txt" 1730 1731return count 1732]] , 1733{} 1734) 1735) 1736end ) 1737 1738it ( 'properly releases functions with ++once' , function () 1739exec_lua ( [[ 1740WeakTable = setmetatable({}, { __mode = "k" }) 1741 1742OnceCount = 0 1743 1744MyVal = {} 1745WeakTable[MyVal] = true 1746 1747vim.api.nvim_create_autocmd("FileType", { 1748pattern = "*", 1749callback = function() 1750OnceCount = OnceCount + 1 1751MyVal = {} 1752end, 1753once = true 1754}) 1755]] ) 1756 1757command [[set filetype=txt]] 1758eq ( 1 , exec_lua ( [[return OnceCount]] , {} )) 1759 1760exec_lua ( [[collectgarbage()]] , {} ) 1761 1762command [[set filetype=txt]] 1763eq ( 1 , exec_lua ( [[return OnceCount]] , {} )) 1764 1765eq ( 17660 , 1767exec_lua ( [[ 1768local count = 0 1769for _ in pairs(WeakTable) do 1770count = count + 1 1771end 1772 1773return count 1774]] ), 1775'Should have no keys remaining' 1776) 1777end ) 1778 1779it ( 'groups can be cleared' , function () 1780local augroup ='TestGroup' 1781api . nvim_create_augroup ( augroup , { clear =true } ) 1782api . nvim_create_autocmd ( 'FileType' , { 1783group =augroup , 1784command ='let g:executed = g:executed + 1' , 1785} ) 1786 1787set_ft ( 'txt' ) 1788set_ft ( 'txt' ) 1789eq ( 2 , get_executed_count (), 'should only count twice' ) 1790 1791api . nvim_create_augroup ( augroup , { clear =true } ) 1792eq ( {} , api . nvim_get_autocmds { group =augroup } ) 1793 1794set_ft ( 'txt' ) 1795set_ft ( 'txt' ) 1796eq ( 2 , get_executed_count (), 'No additional counts' ) 1797end ) 1798 1799it ( 'can delete non-existent groups with pcall' , function () 1800eq ( false , exec_lua [[return pcall(vim.api.nvim_del_augroup_by_name, 'noexist')]] ) 1801eq ( 'Vim:E367: No such group: "noexist"' , pcall_err ( api . nvim_del_augroup_by_name , 'noexist' )) 1802 1803eq ( false , exec_lua [[return pcall(vim.api.nvim_del_augroup_by_id, -12342)]] ) 1804eq ( 'Vim:E367: No such group id: -12312' , pcall_err ( api . nvim_del_augroup_by_id , - 12312 )) 1805 1806eq ( false , exec_lua [[return pcall(vim.api.nvim_del_augroup_by_id, 0)]] ) 1807eq ( 'Vim:E367: No such group id: 0' , pcall_err ( api . nvim_del_augroup_by_id , 0 )) 1808 1809eq ( false , exec_lua [[return pcall(vim.api.nvim_del_augroup_by_id, 12342)]] ) 1810eq ( 'Vim:E367: No such group id: 12312' , pcall_err ( api . nvim_del_augroup_by_id , 12312 )) 1811end ) 1812 1813it ( 'groups work with once' , function () 1814local augroup ='TestGroup' 1815 1816api . nvim_create_augroup ( augroup , { clear =true } ) 1817make_counting_autocmd { group =augroup , once =true } 1818 1819set_ft ( 'txt' ) 1820set_ft ( 'python' ) 1821 1822eq ( 1 , get_executed_count ()) 1823end ) 1824 1825it ( 'autocmds can be registered multiple times.' , function () 1826local augroup ='TestGroup' 1827 1828api . nvim_create_augroup ( augroup , { clear =true } ) 1829make_counting_autocmd { group =augroup , once =false } 1830make_counting_autocmd { group =augroup , once =false } 1831make_counting_autocmd { group =augroup , once =false } 1832 1833set_ft ( 'txt' ) 1834set_ft ( 'python' ) 1835 1836eq ( 3 * 2 , get_executed_count ()) 1837end ) 1838 1839it ( 'can be deleted' , function () 1840local augroup ='WillBeDeleted' 1841 1842api . nvim_create_augroup ( augroup , { clear =true } ) 1843api . nvim_create_autocmd ( { 'FileType' } , { 1844pattern ='*' , 1845command ="echo 'does not matter'" , 1846} ) 1847 1848-- Clears the augroup from before, which erases the autocmd 1849api . nvim_create_augroup ( augroup , { clear =true } ) 1850 1851local result =# api . nvim_get_autocmds { group =augroup } 1852 1853eq ( 0 , result ) 1854end ) 1855 1856it ( 'can be used for buffer local autocmds' , function () 1857local augroup ='WillBeDeleted' 1858 1859api . nvim_set_var ( 'value_set' , false ) 1860 1861api . nvim_create_augroup ( augroup , { clear =true } ) 1862api . nvim_create_autocmd ( 'FileType' , { 1863pattern ='<buffer>' , 1864command ='let g:value_set = v:true' , 1865} ) 1866 1867command 'new' 1868command 'set filetype=python' 1869 1870eq ( false , api . nvim_get_var ( 'value_set' )) 1871end ) 1872 1873it ( 'can accept vimscript functions' , function () 1874source [[ 1875let g:vimscript_executed = 0 1876 1877function! MyVimscriptFunction() abort 1878let g:vimscript_executed = g:vimscript_executed + 1 1879endfunction 1880 1881call nvim_create_autocmd("FileType", #{ 1882\ pattern: ["python", "javascript"], 1883\ callback: "MyVimscriptFunction", 1884\ }) 1885 1886set filetype=txt 1887set filetype=python 1888set filetype=txt 1889set filetype=javascript 1890set filetype=txt 1891]] 1892 1893eq ( 2 , api . nvim_get_var ( 'vimscript_executed' )) 1894end ) 1895end ) 1896 1897describe ( 'augroup!' , function () 1898it ( 'legacy: should clear and not return any autocmds for delete groups' , function () 1899command ( 'augroup TEMP_A' ) 1900command ( ' autocmd! BufReadPost *.py :echo "Hello"' ) 1901command ( 'augroup END' ) 1902 1903command ( 'augroup! TEMP_A' ) 1904 1905eq ( false , pcall ( api . nvim_get_autocmds , { group ='TEMP_A' } )) 1906 1907-- For some reason, augroup! doesn't clear the autocmds themselves, which is just wild 1908-- but we managed to keep this behavior. 1909eq ( 1 , # api . nvim_get_autocmds { event ='BufReadPost' } ) 1910end ) 1911 1912it ( 'legacy: remove augroups that have no autocmds' , function () 1913command ( 'augroup TEMP_AB' ) 1914command ( 'augroup END' ) 1915 1916command ( 'augroup! TEMP_AB' ) 1917 1918eq ( false , pcall ( api . nvim_get_autocmds , { group ='TEMP_AB' } )) 1919eq ( 0 , # api . nvim_get_autocmds { event ='BufReadPost' } ) 1920end ) 1921 1922it ( 'legacy: multiple remove and add augroup' , function () 1923command ( 'augroup TEMP_ABC' ) 1924command ( ' au!' ) 1925command ( ' autocmd BufReadPost *.py echo "Hello"' ) 1926command ( 'augroup END' ) 1927 1928command ( 'augroup! TEMP_ABC' ) 1929 1930-- Should still have one autocmd :'( 1931local aus =api . nvim_get_autocmds { event ='BufReadPost' } 1932eq ( 1 , # aus , aus ) 1933 1934command ( 'augroup TEMP_ABC' ) 1935command ( ' au!' ) 1936command ( ' autocmd BufReadPost *.py echo "Hello"' ) 1937command ( 'augroup END' ) 1938 1939-- Should now have two autocmds :'( 1940aus =api . nvim_get_autocmds { event ='BufReadPost' } 1941eq ( 2 , # aus , aus ) 1942 1943command ( 'augroup! TEMP_ABC' ) 1944 1945eq ( false , pcall ( api . nvim_get_autocmds , { group ='TEMP_ABC' } )) 1946eq ( 2 , # api . nvim_get_autocmds { event ='BufReadPost' } ) 1947end ) 1948 1949it ( 'api: should clear and not return any autocmds for delete groups by id' , function () 1950command ( 'augroup TEMP_ABCD' ) 1951command ( 'autocmd! BufReadPost *.py :echo "Hello"' ) 1952command ( 'augroup END' ) 1953 1954local augroup_id =api . nvim_create_augroup ( 'TEMP_ABCD' , { clear =false } ) 1955api . nvim_del_augroup_by_id ( augroup_id ) 1956 1957-- For good reason, we kill all the autocmds from del_augroup, 1958-- so now this works as expected 1959eq ( false , pcall ( api . nvim_get_autocmds , { group ='TEMP_ABCD' } )) 1960eq ( 0 , # api . nvim_get_autocmds { event ='BufReadPost' } ) 1961 1962-- deleting an already deleted group gives NICE error message: 1963-- exact value of augroup_id here depends on defaults.lua and bullshit, splice it in! 1964eq ( 1965'Vim:E367: No such group id: ' .. augroup_id .. ' "--Deleted--"' , 1966pcall_err ( api . nvim_del_augroup_by_id , augroup_id ) 1967) 1968end ) 1969 1970it ( 'api: should clear and not return any autocmds for delete groups by name' , function () 1971command ( 'augroup TEMP_ABCDE' ) 1972command ( 'autocmd! BufReadPost *.py :echo "Hello"' ) 1973command ( 'augroup END' ) 1974 1975api . nvim_del_augroup_by_name ( 'TEMP_ABCDE' ) 1976 1977-- For good reason, we kill all the autocmds from del_augroup, 1978-- so now this works as expected 1979eq ( false , pcall ( api . nvim_get_autocmds , { group ='TEMP_ABCDE' } )) 1980eq ( 0 , # api . nvim_get_autocmds { event ='BufReadPost' } ) 1981end ) 1982end ) 1983 1984describe ( 'nvim_clear_autocmds' , function () 1985it ( 'validation' , function () 1986eq ( 1987"Conflict: 'pattern' not allowed with 'buf'" , 1988pcall_err ( api . nvim_clear_autocmds , { 1989pattern ='*' , 1990buf =42 , 1991} ) 1992) 1993-- Deprecated `buffer` key still triggers the same conflict. 1994eq ( 1995"Conflict: 'pattern' not allowed with 'buf'" , 1996pcall_err ( api . nvim_clear_autocmds , { 1997pattern ='*' , 1998buffer =42 , 1999} ) 2000) 2001-- Passing both `buf` and `buffer` is rejected. 2002eq ( 2003"Conflict: 'buf' not allowed with 'buffer'" , 2004pcall_err ( api . nvim_clear_autocmds , { 2005buf =0 , 2006buffer =1 , 2007} ) 2008) 2009eq ( 2010"Invalid 'event' item: expected String, got Array" , 2011pcall_err ( api . nvim_clear_autocmds , { 2012event ={ 'FileType' , {} } , 2013} ) 2014) 2015eq ( 2016"Invalid 'event': expected Array or String, got Integer" , 2017pcall_err ( api . nvim_clear_autocmds , { event =1337 } ) 2018) 2019matches ( 2020"Invalid 'event': expected Array or String, got Function$" , 2021pcall_err ( exec_lua , function () 2022vim . api . nvim_clear_autocmds ( { event =function () end } ) 2023end ) 2024) 2025eq ( "Invalid 'group': 0" , pcall_err ( api . nvim_clear_autocmds , { group =0 } )) 2026end ) 2027 2028it ( 'should clear based on event + pattern' , function () 2029command ( 'autocmd InsertEnter *.py :echo "Python can be cool sometimes"' ) 2030command ( 'autocmd InsertEnter *.txt :echo "Text Files Are Cool"' ) 2031 2032local search ={ event ='InsertEnter' , pattern ='*.txt' } 2033local before_delete =api . nvim_get_autocmds ( search ) 2034eq ( 1 , # before_delete ) 2035 2036local before_delete_all =api . nvim_get_autocmds { event =search . event } 2037eq ( 2 , # before_delete_all ) 2038 2039api . nvim_clear_autocmds ( search ) 2040local after_delete =api . nvim_get_autocmds ( search ) 2041eq ( 0 , # after_delete ) 2042 2043local after_delete_all =api . nvim_get_autocmds { event =search . event } 2044eq ( 1 , # after_delete_all ) 2045end ) 2046 2047it ( 'should clear based on event' , function () 2048command ( 'autocmd InsertEnter *.py :echo "Python can be cool sometimes"' ) 2049command ( 'autocmd InsertEnter *.txt :echo "Text Files Are Cool"' ) 2050 2051local search ={ event ='InsertEnter' } 2052local before_delete =api . nvim_get_autocmds ( search ) 2053eq ( 2 , # before_delete ) 2054 2055-- Like nvim_exec_autocmds(), empty event array matches (and clears) nothing. 2056api . nvim_clear_autocmds ( { event ={} } ) 2057eq ( 2 , # api . nvim_get_autocmds ( search )) 2058 2059api . nvim_clear_autocmds ( search ) 2060local after_delete =api . nvim_get_autocmds ( search ) 2061eq ( 0 , # after_delete ) 2062end ) 2063 2064it ( 'should clear based on pattern' , function () 2065command ( 'autocmd InsertEnter *.TestPat1 :echo "Enter 1"' ) 2066command ( 'autocmd InsertLeave *.TestPat1 :echo "Leave 1"' ) 2067command ( 'autocmd InsertEnter *.TestPat2 :echo "Enter 2"' ) 2068command ( 'autocmd InsertLeave *.TestPat2 :echo "Leave 2"' ) 2069 2070local search ={ pattern ='*.TestPat1' } 2071local before_delete =api . nvim_get_autocmds ( search ) 2072eq ( 2 , # before_delete ) 2073local before_delete_events = 2074api . nvim_get_autocmds { event ={ 'InsertEnter' , 'InsertLeave' } } 2075eq ( 4 , # before_delete_events ) 2076 2077api . nvim_clear_autocmds ( search ) 2078local after_delete =api . nvim_get_autocmds ( search ) 2079eq ( 0 , # after_delete ) 2080 2081local after_delete_events =api . nvim_get_autocmds { event ={ 'InsertEnter' , 'InsertLeave' } } 2082eq ( 2 , # after_delete_events ) 2083 2084-- Non-nil, fully-empty patterns clear nothing. 2085local event_count =# api . nvim_get_autocmds {} 2086api . nvim_clear_autocmds ( { pattern ='' } ) 2087api . nvim_clear_autocmds ( { pattern =',,,' } ) 2088api . nvim_clear_autocmds ( { pattern ={} } ) 2089api . nvim_clear_autocmds ( { pattern ={ '' , '' } } ) 2090api . nvim_clear_autocmds ( { pattern ={ ',' , ',,' } } ) 2091eq ( event_count , # api . nvim_get_autocmds {} ) 2092end ) 2093 2094it ( 'should allow clearing by buf' , function () 2095for _ , buf_key in ipairs ( { 2096'buf' , 2097'buffer' , -- deprecated name 2098} ) do 2099command ( 'autocmd! InsertEnter' ) 2100command ( 'autocmd InsertEnter <buffer> :echo "Enter Buffer"' ) 2101command ( 'autocmd InsertEnter *.TestPat1 :echo "Enter Pattern"' ) 2102 2103local search ={ event ='InsertEnter' } 2104local before_delete =api . nvim_get_autocmds ( search ) 2105eq ( 2 , # before_delete ) 2106 2107api . nvim_clear_autocmds { [ buf_key ] =0 } 2108local after_delete =api . nvim_get_autocmds ( search ) 2109eq ( 1 , # after_delete ) 2110eq ( '*.TestPat1' , after_delete [ 1 ]. pattern ) 2111end 2112end ) 2113 2114it ( 'should allow clearing by buf and group' , function () 2115command ( 'augroup TestNvimClearAutocmds' ) 2116command ( ' au!' ) 2117command ( ' autocmd InsertEnter <buffer> :echo "Enter Buffer"' ) 2118command ( ' autocmd InsertEnter *.TestPat1 :echo "Enter Pattern"' ) 2119command ( 'augroup END' ) 2120 2121local search ={ event ='InsertEnter' , group ='TestNvimClearAutocmds' } 2122local before_delete =api . nvim_get_autocmds ( search ) 2123eq ( 2 , # before_delete ) 2124 2125-- Doesn't clear without passing group. 2126api . nvim_clear_autocmds { buf =0 } 2127local without_group =api . nvim_get_autocmds ( search ) 2128eq ( 2 , # without_group ) 2129 2130-- Doesn't clear with passing group. 2131api . nvim_clear_autocmds { buf =0 , group =search . group } 2132local with_group =api . nvim_get_autocmds ( search ) 2133eq ( 1 , # with_group ) 2134end ) 2135end ) 2136end )