1" Tests for autocommands 2 3source shared.vim 4source check.vim 5source term_util.vim 6 7func s:cleanup_buffers() abort 8 for bnr in range(1, bufnr('$')) 9 if bufloaded(bnr) && bufnr('%') != bnr 10 execute 'bd! ' . bnr 11 endif 12 endfor 13endfunc 14 15func Test_vim_did_enter() 16 call assert_false(v:vim_did_enter) 17 18 " This script will never reach the main loop, can't check if v:vim_did_enter 19 " becomes one. 20endfunc 21 22" Test for the CursorHold autocmd 23func Test_CursorHold_autocmd() 24 CheckRunVimInTerminal 25 call writefile(['one', 'two', 'three'], 'Xfile') 26 let before =<< trim END 27 set updatetime=10 28 au CursorHold * call writefile([line('.')], 'Xoutput', 'a') 29 END 30 call writefile(before, 'Xinit') 31 let buf = RunVimInTerminal('-S Xinit Xfile', {}) 32 call term_sendkeys(buf, "G") 33 call term_wait(buf, 50) 34 call term_sendkeys(buf, "gg") 35 call term_wait(buf) 36 call WaitForAssert({-> assert_equal(['1'], readfile('Xoutput')[-1:-1])}) 37 call term_sendkeys(buf, "j") 38 call term_wait(buf) 39 call WaitForAssert({-> assert_equal(['1', '2'], readfile('Xoutput')[-2:-1])}) 40 call term_sendkeys(buf, "j") 41 call term_wait(buf) 42 call WaitForAssert({-> assert_equal(['1', '2', '3'], readfile('Xoutput')[-3:-1])}) 43 call StopVimInTerminal(buf) 44 45 call delete('Xinit') 46 call delete('Xoutput') 47 call delete('Xfile') 48endfunc 49 50if has('timers') 51 52 func ExitInsertMode(id) 53 call feedkeys("\<Esc>") 54 endfunc 55 56 func Test_cursorhold_insert() 57 " Need to move the cursor. 58 call feedkeys("ggG", "xt") 59 60 let g:triggered = 0 61 au CursorHoldI * let g:triggered += 1 62 set updatetime=20 63 call timer_start(200, 'ExitInsertMode') 64 call feedkeys('a', 'x!') 65 call assert_equal(1, g:triggered) 66 unlet g:triggered 67 au! CursorHoldI 68 set updatetime& 69 endfunc 70 71 func Test_cursorhold_insert_with_timer_interrupt() 72 CheckFeature job 73 " Need to move the cursor. 74 call feedkeys("ggG", "xt") 75 76 " Confirm the timer invoked in exit_cb of the job doesn't disturb 77 " CursorHoldI event. 78 let g:triggered = 0 79 au CursorHoldI * let g:triggered += 1 80 set updatetime=100 81 call job_start(has('win32') ? 'cmd /c echo:' : 'echo', 82 \ {'exit_cb': {-> timer_start(200, 'ExitInsertMode')}}) 83 call feedkeys('a', 'x!') 84 call assert_equal(1, g:triggered) 85 unlet g:triggered 86 au! CursorHoldI 87 set updatetime& 88 endfunc 89 90 func Test_cursorhold_insert_ctrl_x() 91 let g:triggered = 0 92 au CursorHoldI * let g:triggered += 1 93 set updatetime=20 94 call timer_start(100, 'ExitInsertMode') 95 " CursorHoldI does not trigger after CTRL-X 96 call feedkeys("a\<C-X>", 'x!') 97 call assert_equal(0, g:triggered) 98 unlet g:triggered 99 au! CursorHoldI 100 set updatetime& 101 endfunc 102 103 func Test_cursorhold_insert_ctrl_g_U() 104 au CursorHoldI * : 105 set updatetime=20 106 new 107 call timer_start(100, { -> feedkeys("\<Left>foo\<Esc>", 't') }) 108 call feedkeys("i()\<C-g>U", 'tx!') 109 sleep 200m 110 call assert_equal('(foo)', getline(1)) 111 undo 112 call assert_equal('', getline(1)) 113 114 bwipe! 115 au! CursorHoldI 116 set updatetime& 117 endfunc 118 119 func Test_OptionSet_modeline() 120 call test_override('starting', 1) 121 au! OptionSet 122 augroup set_tabstop 123 au OptionSet tabstop call timer_start(1, {-> execute("echo 'Handler called'", "")}) 124 augroup END 125 call writefile(['vim: set ts=7 sw=5 :', 'something'], 'XoptionsetModeline') 126 set modeline 127 let v:errmsg = '' 128 call assert_fails('split XoptionsetModeline', 'E12:') 129 call assert_equal(7, &ts) 130 call assert_equal('', v:errmsg) 131 132 augroup set_tabstop 133 au! 134 augroup END 135 bwipe! 136 set ts& 137 call delete('XoptionsetModeline') 138 call test_override('starting', 0) 139 endfunc 140 141endif "has('timers') 142 143func Test_bufunload() 144 augroup test_bufunload_group 145 autocmd! 146 autocmd BufUnload * call add(s:li, "bufunload") 147 autocmd BufDelete * call add(s:li, "bufdelete") 148 autocmd BufWipeout * call add(s:li, "bufwipeout") 149 augroup END 150 151 let s:li = [] 152 new 153 setlocal bufhidden= 154 bunload 155 call assert_equal(["bufunload", "bufdelete"], s:li) 156 157 let s:li = [] 158 new 159 setlocal bufhidden=delete 160 bunload 161 call assert_equal(["bufunload", "bufdelete"], s:li) 162 163 let s:li = [] 164 new 165 setlocal bufhidden=unload 166 bwipeout 167 call assert_equal(["bufunload", "bufdelete", "bufwipeout"], s:li) 168 169 au! test_bufunload_group 170 augroup! test_bufunload_group 171endfunc 172 173" SEGV occurs in older versions. (At least 7.4.2005 or older) 174func Test_autocmd_bufunload_with_tabnext() 175 tabedit 176 tabfirst 177 178 augroup test_autocmd_bufunload_with_tabnext_group 179 autocmd! 180 autocmd BufUnload <buffer> tabnext 181 augroup END 182 183 quit 184 call assert_equal(2, tabpagenr('$')) 185 186 autocmd! test_autocmd_bufunload_with_tabnext_group 187 augroup! test_autocmd_bufunload_with_tabnext_group 188 tablast 189 quit 190endfunc 191 192func Test_argdelete_in_next() 193 au BufNew,BufEnter,BufLeave,BufWinEnter * argdel 194 call assert_fails('next a b', 'E1156:') 195 au! BufNew,BufEnter,BufLeave,BufWinEnter * 196endfunc 197 198func Test_autocmd_bufwinleave_with_tabfirst() 199 tabedit 200 augroup sample 201 autocmd! 202 autocmd BufWinLeave <buffer> tabfirst 203 augroup END 204 call setline(1, ['a', 'b', 'c']) 205 edit! a.txt 206 tabclose 207endfunc 208 209" SEGV occurs in older versions. (At least 7.4.2321 or older) 210func Test_autocmd_bufunload_avoiding_SEGV_01() 211 split aa.txt 212 let lastbuf = bufnr('$') 213 214 augroup test_autocmd_bufunload 215 autocmd! 216 exe 'autocmd BufUnload <buffer> ' . (lastbuf + 1) . 'bwipeout!' 217 augroup END 218 219 call assert_fails('edit bb.txt', 'E937:') 220 221 autocmd! test_autocmd_bufunload 222 augroup! test_autocmd_bufunload 223 bwipe! aa.txt 224 bwipe! bb.txt 225endfunc 226 227" SEGV occurs in older versions. (At least 7.4.2321 or older) 228func Test_autocmd_bufunload_avoiding_SEGV_02() 229 setlocal buftype=nowrite 230 let lastbuf = bufnr('$') 231 232 augroup test_autocmd_bufunload 233 autocmd! 234 exe 'autocmd BufUnload <buffer> ' . (lastbuf + 1) . 'bwipeout!' 235 augroup END 236 237 normal! i1 238 call assert_fails('edit a.txt', 'E517:') 239 240 autocmd! test_autocmd_bufunload 241 augroup! test_autocmd_bufunload 242 bwipe! a.txt 243endfunc 244 245func Test_autocmd_dummy_wipeout() 246 " prepare files 247 call writefile([''], 'Xdummywipetest1.txt') 248 call writefile([''], 'Xdummywipetest2.txt') 249 augroup test_bufunload_group 250 autocmd! 251 autocmd BufUnload * call add(s:li, "bufunload") 252 autocmd BufDelete * call add(s:li, "bufdelete") 253 autocmd BufWipeout * call add(s:li, "bufwipeout") 254 augroup END 255 256 let s:li = [] 257 split Xdummywipetest1.txt 258 silent! vimgrep /notmatched/ Xdummywipetest* 259 call assert_equal(["bufunload", "bufwipeout"], s:li) 260 261 bwipeout 262 call delete('Xdummywipetest1.txt') 263 call delete('Xdummywipetest2.txt') 264 au! test_bufunload_group 265 augroup! test_bufunload_group 266endfunc 267 268func Test_win_tab_autocmd() 269 let g:record = [] 270 271 augroup testing 272 au WinNew * call add(g:record, 'WinNew') 273 au WinEnter * call add(g:record, 'WinEnter') 274 au WinLeave * call add(g:record, 'WinLeave') 275 au TabNew * call add(g:record, 'TabNew') 276 au TabClosed * call add(g:record, 'TabClosed') 277 au TabEnter * call add(g:record, 'TabEnter') 278 au TabLeave * call add(g:record, 'TabLeave') 279 augroup END 280 281 split 282 tabnew 283 close 284 close 285 286 call assert_equal([ 287 \ 'WinLeave', 'WinNew', 'WinEnter', 288 \ 'WinLeave', 'TabLeave', 'WinNew', 'WinEnter', 'TabNew', 'TabEnter', 289 \ 'WinLeave', 'TabLeave', 'TabClosed', 'WinEnter', 'TabEnter', 290 \ 'WinLeave', 'WinEnter' 291 \ ], g:record) 292 293 let g:record = [] 294 tabnew somefile 295 tabnext 296 bwipe somefile 297 298 call assert_equal([ 299 \ 'WinLeave', 'TabLeave', 'WinNew', 'WinEnter', 'TabNew', 'TabEnter', 300 \ 'WinLeave', 'TabLeave', 'WinEnter', 'TabEnter', 301 \ 'TabClosed' 302 \ ], g:record) 303 304 augroup testing 305 au! 306 augroup END 307 unlet g:record 308endfunc 309 310func s:AddAnAutocmd() 311 augroup vimBarTest 312 au BufReadCmd * echo 'hello' 313 augroup END 314 call assert_equal(3, len(split(execute('au vimBarTest'), "\n"))) 315endfunc 316 317func Test_early_bar() 318 " test that a bar is recognized before the {event} 319 call s:AddAnAutocmd() 320 augroup vimBarTest | au! | augroup END 321 call assert_equal(1, len(split(execute('au vimBarTest'), "\n"))) 322 323 call s:AddAnAutocmd() 324 augroup vimBarTest| au!| augroup END 325 call assert_equal(1, len(split(execute('au vimBarTest'), "\n"))) 326 327 " test that a bar is recognized after the {event} 328 call s:AddAnAutocmd() 329 augroup vimBarTest| au!BufReadCmd| augroup END 330 call assert_equal(1, len(split(execute('au vimBarTest'), "\n"))) 331 332 " test that a bar is recognized after the {group} 333 call s:AddAnAutocmd() 334 au! vimBarTest|echo 'hello' 335 call assert_equal(1, len(split(execute('au vimBarTest'), "\n"))) 336endfunc 337 338func RemoveGroup() 339 autocmd! StartOK 340 augroup! StartOK 341endfunc 342 343func Test_augroup_warning() 344 augroup TheWarning 345 au VimEnter * echo 'entering' 346 augroup END 347 call assert_match("TheWarning.*VimEnter", execute('au VimEnter')) 348 redir => res 349 augroup! TheWarning 350 redir END 351 call assert_match("W19:", res) 352 call assert_match("-Deleted-.*VimEnter", execute('au VimEnter')) 353 354 " check "Another" does not take the pace of the deleted entry 355 augroup Another 356 augroup END 357 call assert_match("-Deleted-.*VimEnter", execute('au VimEnter')) 358 augroup! Another 359 360 " no warning for postpone aucmd delete 361 augroup StartOK 362 au VimEnter * call RemoveGroup() 363 augroup END 364 call assert_match("StartOK.*VimEnter", execute('au VimEnter')) 365 redir => res 366 doautocmd VimEnter 367 redir END 368 call assert_notmatch("W19:", res) 369 au! VimEnter 370 371 call assert_fails('augroup!', 'E471:') 372endfunc 373 374func Test_BufReadCmdHelp() 375 " This used to cause access to free memory 376 au BufReadCmd * e +h 377 help 378 379 au! BufReadCmd 380endfunc 381 382func Test_BufReadCmdHelpJump() 383 " This used to cause access to free memory 384 au BufReadCmd * e +h{ 385 " } to fix highlighting 386 call assert_fails('help', 'E434:') 387 388 au! BufReadCmd 389endfunc 390 391func Test_augroup_deleted() 392 " This caused a crash before E936 was introduced 393 augroup x 394 call assert_fails('augroup! x', 'E936:') 395 au VimEnter * echo 396 augroup end 397 augroup! x 398 call assert_match("-Deleted-.*VimEnter", execute('au VimEnter')) 399 au! VimEnter 400endfunc 401 402" Tests for autocommands on :close command. 403" This used to be in test13. 404func Test_three_windows() 405 " Clean up buffers, because in some cases this function fails. 406 call s:cleanup_buffers() 407 408 " Write three files and open them, each in a window. 409 " Then go to next window, with autocommand that deletes the previous one. 410 " Do this twice, writing the file. 411 e! Xtestje1 412 call setline(1, 'testje1') 413 w 414 sp Xtestje2 415 call setline(1, 'testje2') 416 w 417 sp Xtestje3 418 call setline(1, 'testje3') 419 w 420 wincmd w 421 au WinLeave Xtestje2 bwipe 422 wincmd w 423 call assert_equal('Xtestje1', expand('%')) 424 425 au WinLeave Xtestje1 bwipe Xtestje3 426 close 427 call assert_equal('Xtestje1', expand('%')) 428 429 " Test deleting the buffer on a Unload event. If this goes wrong there 430 " will be the ATTENTION prompt. 431 e Xtestje1 432 au! 433 au! BufUnload Xtestje1 bwipe 434 call assert_fails('e Xtestje3', 'E937:') 435 call assert_equal('Xtestje3', expand('%')) 436 437 e Xtestje2 438 sp Xtestje1 439 call assert_fails('e', 'E937:') 440 call assert_equal('Xtestje1', expand('%')) 441 442 " Test changing buffers in a BufWipeout autocommand. If this goes wrong 443 " there are ml_line errors and/or a Crash. 444 au! 445 only 446 e Xanother 447 e Xtestje1 448 bwipe Xtestje2 449 bwipe Xtestje3 450 au BufWipeout Xtestje1 buf Xtestje1 451 bwipe 452 call assert_equal('Xanother', expand('%')) 453 454 only 455 help 456 wincmd w 457 1quit 458 call assert_equal('Xanother', expand('%')) 459 460 au! 461 enew 462 call delete('Xtestje1') 463 call delete('Xtestje2') 464 call delete('Xtestje3') 465endfunc 466 467func Test_BufEnter() 468 au! BufEnter 469 au Bufenter * let val = val . '+' 470 let g:val = '' 471 split NewFile 472 call assert_equal('+', g:val) 473 bwipe! 474 call assert_equal('++', g:val) 475 476 " Also get BufEnter when editing a directory 477 call mkdir('Xdir') 478 split Xdir 479 call assert_equal('+++', g:val) 480 481 " On MS-Windows we can't edit the directory, make sure we wipe the right 482 " buffer. 483 bwipe! Xdir 484 485 call delete('Xdir', 'd') 486 au! BufEnter 487endfunc 488 489" Closing a window might cause an endless loop 490" E814 for older Vims 491func Test_autocmd_bufwipe_in_SessLoadPost() 492 edit Xtest 493 tabnew 494 file Xsomething 495 set noswapfile 496 mksession! 497 498 let content =<< trim [CODE] 499 call test_override('ui_delay', 10) 500 set nocp noswapfile 501 let v:swapchoice = "e" 502 augroup test_autocmd_sessionload 503 autocmd! 504 autocmd SessionLoadPost * exe bufnr("Xsomething") . "bw!" 505 augroup END 506 507 func WriteErrors() 508 call writefile([execute("messages")], "Xerrors") 509 endfunc 510 au VimLeave * call WriteErrors() 511 [CODE] 512 513 call writefile(content, 'Xvimrc') 514 call system(GetVimCommand('Xvimrc') .. ' --not-a-term --noplugins -S Session.vim -c cq') 515 let errors = join(readfile('Xerrors')) 516 call assert_match('E814:', errors) 517 518 set swapfile 519 for file in ['Session.vim', 'Xvimrc', 'Xerrors'] 520 call delete(file) 521 endfor 522endfunc 523 524" Using :blast and :ball for many events caused a crash, because b_nwindows was 525" not incremented correctly. 526func Test_autocmd_blast_badd() 527 let content =<< trim [CODE] 528 au BufNew,BufAdd,BufWinEnter,BufEnter,BufLeave,BufWinLeave,BufUnload,VimEnter foo* blast 529 edit foo1 530 au BufNew,BufAdd,BufWinEnter,BufEnter,BufLeave,BufWinLeave,BufUnload,VimEnter foo* ball 531 edit foo2 532 call writefile(['OK'], 'Xerrors') 533 qall 534 [CODE] 535 536 call writefile(content, 'XblastBall') 537 call system(GetVimCommand() .. ' --clean -S XblastBall') 538 call assert_match('OK', readfile('Xerrors')->join()) 539 540 call delete('XblastBall') 541 call delete('Xerrors') 542endfunc 543 544" SEGV occurs in older versions. 545func Test_autocmd_bufwipe_in_SessLoadPost2() 546 tabnew 547 set noswapfile 548 mksession! 549 550 let content =<< trim [CODE] 551 set nocp noswapfile 552 function! DeleteInactiveBufs() 553 tabfirst 554 let tabblist = [] 555 for i in range(1, tabpagenr(''$'')) 556 call extend(tabblist, tabpagebuflist(i)) 557 endfor 558 for b in range(1, bufnr(''$'')) 559 if bufexists(b) && buflisted(b) && (index(tabblist, b) == -1 || bufname(b) =~# ''^$'') 560 exec ''bwipeout '' . b 561 endif 562 endfor 563 echomsg "SessionLoadPost DONE" 564 endfunction 565 au SessionLoadPost * call DeleteInactiveBufs() 566 567 func WriteErrors() 568 call writefile([execute("messages")], "Xerrors") 569 endfunc 570 au VimLeave * call WriteErrors() 571 [CODE] 572 573 call writefile(content, 'Xvimrc') 574 call system(GetVimCommand('Xvimrc') .. ' --not-a-term --noplugins -S Session.vim -c cq') 575 let errors = join(readfile('Xerrors')) 576 " This probably only ever matches on unix. 577 call assert_notmatch('Caught deadly signal SEGV', errors) 578 call assert_match('SessionLoadPost DONE', errors) 579 580 set swapfile 581 for file in ['Session.vim', 'Xvimrc', 'Xerrors'] 582 call delete(file) 583 endfor 584endfunc 585 586func Test_empty_doau() 587 doau \| 588endfunc 589 590func s:AutoCommandOptionSet(match) 591 let template = "Option: <%s>, OldVal: <%s>, OldValLocal: <%s>, OldValGlobal: <%s>, NewVal: <%s>, Scope: <%s>, Command: <%s>\n" 592 let item = remove(g:options, 0) 593 let expected = printf(template, item[0], item[1], item[2], item[3], item[4], item[5], item[6]) 594 let actual = printf(template, a:match, v:option_old, v:option_oldlocal, v:option_oldglobal, v:option_new, v:option_type, v:option_command) 595 let g:opt = [expected, actual] 596 "call assert_equal(expected, actual) 597endfunc 598 599func Test_OptionSet() 600 CheckOption autochdir 601 602 badd test_autocmd.vim 603 604 call test_override('starting', 1) 605 set nocp 606 au OptionSet * :call s:AutoCommandOptionSet(expand("<amatch>")) 607 608 " 1: Setting number option" 609 let g:options = [['number', 0, 0, 0, 1, 'global', 'set']] 610 set nu 611 call assert_equal([], g:options) 612 call assert_equal(g:opt[0], g:opt[1]) 613 614 " 2: Setting local number option" 615 let g:options = [['number', 1, 1, '', 0, 'local', 'setlocal']] 616 setlocal nonu 617 call assert_equal([], g:options) 618 call assert_equal(g:opt[0], g:opt[1]) 619 620 " 3: Setting global number option" 621 let g:options = [['number', 1, '', 1, 0, 'global', 'setglobal']] 622 setglobal nonu 623 call assert_equal([], g:options) 624 call assert_equal(g:opt[0], g:opt[1]) 625 626 " 4: Setting local autoindent option" 627 let g:options = [['autoindent', 0, 0, '', 1, 'local', 'setlocal']] 628 setlocal ai 629 call assert_equal([], g:options) 630 call assert_equal(g:opt[0], g:opt[1]) 631 632 " 5: Setting global autoindent option" 633 let g:options = [['autoindent', 0, '', 0, 1, 'global', 'setglobal']] 634 setglobal ai 635 call assert_equal([], g:options) 636 call assert_equal(g:opt[0], g:opt[1]) 637 638 " 6: Setting global autoindent option" 639 let g:options = [['autoindent', 1, 1, 1, 0, 'global', 'set']] 640 set ai! 641 call assert_equal([], g:options) 642 call assert_equal(g:opt[0], g:opt[1]) 643 644 " 6a: Setting global autoindent option" 645 let g:options = [['autoindent', 1, 1, 0, 0, 'global', 'set']] 646 noa setlocal ai 647 noa setglobal noai 648 set ai! 649 call assert_equal([], g:options) 650 call assert_equal(g:opt[0], g:opt[1]) 651 652 " Should not print anything, use :noa 653 " 7: don't trigger OptionSet" 654 let g:options = [['invalid', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid']] 655 noa set nonu 656 call assert_equal([['invalid', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid']], g:options) 657 call assert_equal(g:opt[0], g:opt[1]) 658 659 " 8: Setting several global list and number option" 660 let g:options = [['list', 0, 0, 0, 1, 'global', 'set'], ['number', 0, 0, 0, 1, 'global', 'set']] 661 set list nu 662 call assert_equal([], g:options) 663 call assert_equal(g:opt[0], g:opt[1]) 664 665 " 9: don't trigger OptionSet" 666 let g:options = [['invalid', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid'], ['invalid', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid']] 667 noa set nolist nonu 668 call assert_equal([['invalid', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid'], ['invalid', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid']], g:options) 669 call assert_equal(g:opt[0], g:opt[1]) 670 671 " 10: Setting global acd" 672 let g:options = [['autochdir', 0, 0, '', 1, 'local', 'setlocal']] 673 setlocal acd 674 call assert_equal([], g:options) 675 call assert_equal(g:opt[0], g:opt[1]) 676 677 " 11: Setting global autoread (also sets local value)" 678 let g:options = [['autoread', 0, 0, 0, 1, 'global', 'set']] 679 set ar 680 call assert_equal([], g:options) 681 call assert_equal(g:opt[0], g:opt[1]) 682 683 " 12: Setting local autoread" 684 let g:options = [['autoread', 1, 1, '', 1, 'local', 'setlocal']] 685 setlocal ar 686 call assert_equal([], g:options) 687 call assert_equal(g:opt[0], g:opt[1]) 688 689 " 13: Setting global autoread" 690 let g:options = [['autoread', 1, '', 1, 0, 'global', 'setglobal']] 691 setglobal invar 692 call assert_equal([], g:options) 693 call assert_equal(g:opt[0], g:opt[1]) 694 695 " 14: Setting option backspace through :let" 696 let g:options = [['backspace', '', '', '', 'eol,indent,start', 'global', 'set']] 697 let &bs = "eol,indent,start" 698 call assert_equal([], g:options) 699 call assert_equal(g:opt[0], g:opt[1]) 700 701 " 15: Setting option backspace through setbufvar()" 702 let g:options = [['backup', 0, 0, '', 1, 'local', 'setlocal']] 703 " try twice, first time, shouldn't trigger because option name is invalid, 704 " second time, it should trigger 705 let bnum = bufnr('%') 706 call assert_fails("call setbufvar(bnum, '&l:bk', 1)", 'E355:') 707 " should trigger, use correct option name 708 call setbufvar(bnum, '&backup', 1) 709 call assert_equal([], g:options) 710 call assert_equal(g:opt[0], g:opt[1]) 711 712 " 16: Setting number option using setwinvar" 713 let g:options = [['number', 0, 0, '', 1, 'local', 'setlocal']] 714 call setwinvar(0, '&number', 1) 715 call assert_equal([], g:options) 716 call assert_equal(g:opt[0], g:opt[1]) 717 718 " 17: Setting key option, shouldn't trigger" 719 let g:options = [['key', 'invalid', 'invalid1', 'invalid2', 'invalid3', 'invalid4', 'invalid5']] 720 setlocal key=blah 721 setlocal key= 722 call assert_equal([['key', 'invalid', 'invalid1', 'invalid2', 'invalid3', 'invalid4', 'invalid5']], g:options) 723 call assert_equal(g:opt[0], g:opt[1]) 724 725 726 " 18a: Setting string global option" 727 let oldval = &backupext 728 let g:options = [['backupext', oldval, oldval, oldval, 'foo', 'global', 'set']] 729 set backupext=foo 730 call assert_equal([], g:options) 731 call assert_equal(g:opt[0], g:opt[1]) 732 733 " 18b: Resetting string global option" 734 let g:options = [['backupext', 'foo', 'foo', 'foo', oldval, 'global', 'set']] 735 set backupext& 736 call assert_equal([], g:options) 737 call assert_equal(g:opt[0], g:opt[1]) 738 739 " 18c: Setting global string global option" 740 let g:options = [['backupext', oldval, '', oldval, 'bar', 'global', 'setglobal']] 741 setglobal backupext=bar 742 call assert_equal([], g:options) 743 call assert_equal(g:opt[0], g:opt[1]) 744 745 " 18d: Setting local string global option" 746 " As this is a global option this sets the global value even though 747 " :setlocal is used! 748 noa set backupext& " Reset global and local value (without triggering autocmd) 749 let g:options = [['backupext', oldval, oldval, '', 'baz', 'local', 'setlocal']] 750 setlocal backupext=baz 751 call assert_equal([], g:options) 752 call assert_equal(g:opt[0], g:opt[1]) 753 754 " 18e: Setting again string global option" 755 noa setglobal backupext=ext_global " Reset global and local value (without triggering autocmd) 756 noa setlocal backupext=ext_local " Sets the global(!) value! 757 let g:options = [['backupext', 'ext_local', 'ext_local', 'ext_local', 'fuu', 'global', 'set']] 758 set backupext=fuu 759 call assert_equal([], g:options) 760 call assert_equal(g:opt[0], g:opt[1]) 761 762 763 " 19a: Setting string local-global (to buffer) option" 764 let oldval = &tags 765 let g:options = [['tags', oldval, oldval, oldval, 'tagpath', 'global', 'set']] 766 set tags=tagpath 767 call assert_equal([], g:options) 768 call assert_equal(g:opt[0], g:opt[1]) 769 770 " 19b: Resetting string local-global (to buffer) option" 771 let g:options = [['tags', 'tagpath', 'tagpath', 'tagpath', oldval, 'global', 'set']] 772 set tags& 773 call assert_equal([], g:options) 774 call assert_equal(g:opt[0], g:opt[1]) 775 776 " 19c: Setting global string local-global (to buffer) option " 777 let g:options = [['tags', oldval, '', oldval, 'tagpath1', 'global', 'setglobal']] 778 setglobal tags=tagpath1 779 call assert_equal([], g:options) 780 call assert_equal(g:opt[0], g:opt[1]) 781 782 " 19d: Setting local string local-global (to buffer) option" 783 let g:options = [['tags', 'tagpath1', 'tagpath1', '', 'tagpath2', 'local', 'setlocal']] 784 setlocal tags=tagpath2 785 call assert_equal([], g:options) 786 call assert_equal(g:opt[0], g:opt[1]) 787 788 " 19e: Setting again string local-global (to buffer) option" 789 " Note: v:option_old is the old global value for local-global string options 790 " but the old local value for all other kinds of options. 791 noa setglobal tags=tag_global " Reset global and local value (without triggering autocmd) 792 noa setlocal tags=tag_local 793 let g:options = [['tags', 'tag_global', 'tag_local', 'tag_global', 'tagpath', 'global', 'set']] 794 set tags=tagpath 795 call assert_equal([], g:options) 796 call assert_equal(g:opt[0], g:opt[1]) 797 798 " 19f: Setting string local-global (to buffer) option to an empty string" 799 " Note: v:option_old is the old global value for local-global string options 800 " but the old local value for all other kinds of options. 801 noa set tags=tag_global " Reset global and local value (without triggering autocmd) 802 noa setlocal tags= " empty string 803 let g:options = [['tags', 'tag_global', '', 'tag_global', 'tagpath', 'global', 'set']] 804 set tags=tagpath 805 call assert_equal([], g:options) 806 call assert_equal(g:opt[0], g:opt[1]) 807 808 809 " 20a: Setting string local (to buffer) option" 810 let oldval = &spelllang 811 let g:options = [['spelllang', oldval, oldval, oldval, 'elvish,klingon', 'global', 'set']] 812 set spelllang=elvish,klingon 813 call assert_equal([], g:options) 814 call assert_equal(g:opt[0], g:opt[1]) 815 816 " 20b: Resetting string local (to buffer) option" 817 let g:options = [['spelllang', 'elvish,klingon', 'elvish,klingon', 'elvish,klingon', oldval, 'global', 'set']] 818 set spelllang& 819 call assert_equal([], g:options) 820 call assert_equal(g:opt[0], g:opt[1]) 821 822 " 20c: Setting global string local (to buffer) option" 823 let g:options = [['spelllang', oldval, '', oldval, 'elvish', 'global', 'setglobal']] 824 setglobal spelllang=elvish 825 call assert_equal([], g:options) 826 call assert_equal(g:opt[0], g:opt[1]) 827 828 " 20d: Setting local string local (to buffer) option" 829 noa set spelllang& " Reset global and local value (without triggering autocmd) 830 let g:options = [['spelllang', oldval, oldval, '', 'klingon', 'local', 'setlocal']] 831 setlocal spelllang=klingon 832 call assert_equal([], g:options) 833 call assert_equal(g:opt[0], g:opt[1]) 834 835 " 20e: Setting again string local (to buffer) option" 836 " Note: v:option_old is the old global value for local-global string options 837 " but the old local value for all other kinds of options. 838 noa setglobal spelllang=spellglobal " Reset global and local value (without triggering autocmd) 839 noa setlocal spelllang=spelllocal 840 let g:options = [['spelllang', 'spelllocal', 'spelllocal', 'spellglobal', 'foo', 'global', 'set']] 841 set spelllang=foo 842 call assert_equal([], g:options) 843 call assert_equal(g:opt[0], g:opt[1]) 844 845 846 " 21a: Setting string local-global (to window) option" 847 let oldval = &statusline 848 let g:options = [['statusline', oldval, oldval, oldval, 'foo', 'global', 'set']] 849 set statusline=foo 850 call assert_equal([], g:options) 851 call assert_equal(g:opt[0], g:opt[1]) 852 853 " 21b: Resetting string local-global (to window) option" 854 " Note: v:option_old is the old global value for local-global string options 855 " but the old local value for all other kinds of options. 856 let g:options = [['statusline', 'foo', 'foo', 'foo', oldval, 'global', 'set']] 857 set statusline& 858 call assert_equal([], g:options) 859 call assert_equal(g:opt[0], g:opt[1]) 860 861 " 21c: Setting global string local-global (to window) option" 862 let g:options = [['statusline', oldval, '', oldval, 'bar', 'global', 'setglobal']] 863 setglobal statusline=bar 864 call assert_equal([], g:options) 865 call assert_equal(g:opt[0], g:opt[1]) 866 867 " 21d: Setting local string local-global (to window) option" 868 noa set statusline& " Reset global and local value (without triggering autocmd) 869 let g:options = [['statusline', oldval, oldval, '', 'baz', 'local', 'setlocal']] 870 setlocal statusline=baz 871 call assert_equal([], g:options) 872 call assert_equal(g:opt[0], g:opt[1]) 873 874 " 21e: Setting again string local-global (to window) option" 875 " Note: v:option_old is the old global value for local-global string options 876 " but the old local value for all other kinds of options. 877 noa setglobal statusline=bar " Reset global and local value (without triggering autocmd) 878 noa setlocal statusline=baz 879 let g:options = [['statusline', 'bar', 'baz', 'bar', 'foo', 'global', 'set']] 880 set statusline=foo 881 call assert_equal([], g:options) 882 call assert_equal(g:opt[0], g:opt[1]) 883 884 885 " 22a: Setting string local (to window) option" 886 let oldval = &foldignore 887 let g:options = [['foldignore', oldval, oldval, oldval, 'fo', 'global', 'set']] 888 set foldignore=fo 889 call assert_equal([], g:options) 890 call assert_equal(g:opt[0], g:opt[1]) 891 892 " 22b: Resetting string local (to window) option" 893 let g:options = [['foldignore', 'fo', 'fo', 'fo', oldval, 'global', 'set']] 894 set foldignore& 895 call assert_equal([], g:options) 896 call assert_equal(g:opt[0], g:opt[1]) 897 898 " 22c: Setting global string local (to window) option" 899 let g:options = [['foldignore', oldval, '', oldval, 'bar', 'global', 'setglobal']] 900 setglobal foldignore=bar 901 call assert_equal([], g:options) 902 call assert_equal(g:opt[0], g:opt[1]) 903 904 " 22d: Setting local string local (to window) option" 905 noa set foldignore& " Reset global and local value (without triggering autocmd) 906 let g:options = [['foldignore', oldval, oldval, '', 'baz', 'local', 'setlocal']] 907 setlocal foldignore=baz 908 call assert_equal([], g:options) 909 call assert_equal(g:opt[0], g:opt[1]) 910 911 " 22e: Setting again string local (to window) option" 912 noa setglobal foldignore=glob " Reset global and local value (without triggering autocmd) 913 noa setlocal foldignore=loc 914 let g:options = [['foldignore', 'loc', 'loc', 'glob', 'fo', 'global', 'set']] 915 set foldignore=fo 916 call assert_equal([], g:options) 917 call assert_equal(g:opt[0], g:opt[1]) 918 919 920 " 23a: Setting global number local option" 921 noa setglobal cmdheight=8 " Reset global and local value (without triggering autocmd) 922 noa setlocal cmdheight=1 " Sets the global(!) value! 923 let g:options = [['cmdheight', '1', '', '1', '2', 'global', 'setglobal']] 924 setglobal cmdheight=2 925 call assert_equal([], g:options) 926 call assert_equal(g:opt[0], g:opt[1]) 927 928 " 23b: Setting local number global option" 929 noa setglobal cmdheight=8 " Reset global and local value (without triggering autocmd) 930 noa setlocal cmdheight=1 " Sets the global(!) value! 931 let g:options = [['cmdheight', '1', '1', '', '2', 'local', 'setlocal']] 932 setlocal cmdheight=2 933 call assert_equal([], g:options) 934 call assert_equal(g:opt[0], g:opt[1]) 935 936 " 23c: Setting again number global option" 937 noa setglobal cmdheight=8 " Reset global and local value (without triggering autocmd) 938 noa setlocal cmdheight=1 " Sets the global(!) value! 939 let g:options = [['cmdheight', '1', '1', '1', '2', 'global', 'set']] 940 set cmdheight=2 941 call assert_equal([], g:options) 942 call assert_equal(g:opt[0], g:opt[1]) 943 944 " 23d: Setting again number global option" 945 noa set cmdheight=8 " Reset global and local value (without triggering autocmd) 946 let g:options = [['cmdheight', '8', '8', '8', '2', 'global', 'set']] 947 set cmdheight=2 948 call assert_equal([], g:options) 949 call assert_equal(g:opt[0], g:opt[1]) 950 951 952 " 24a: Setting global number global-local (to buffer) option" 953 noa setglobal undolevels=8 " Reset global and local value (without triggering autocmd) 954 noa setlocal undolevels=1 955 let g:options = [['undolevels', '8', '', '8', '2', 'global', 'setglobal']] 956 setglobal undolevels=2 957 call assert_equal([], g:options) 958 call assert_equal(g:opt[0], g:opt[1]) 959 960 " 24b: Setting local number global-local (to buffer) option" 961 noa setglobal undolevels=8 " Reset global and local value (without triggering autocmd) 962 noa setlocal undolevels=1 963 let g:options = [['undolevels', '1', '1', '', '2', 'local', 'setlocal']] 964 setlocal undolevels=2 965 call assert_equal([], g:options) 966 call assert_equal(g:opt[0], g:opt[1]) 967 968 " 24c: Setting again number global-local (to buffer) option" 969 noa setglobal undolevels=8 " Reset global and local value (without triggering autocmd) 970 noa setlocal undolevels=1 971 let g:options = [['undolevels', '1', '1', '8', '2', 'global', 'set']] 972 set undolevels=2 973 call assert_equal([], g:options) 974 call assert_equal(g:opt[0], g:opt[1]) 975 976 " 24d: Setting again global number global-local (to buffer) option" 977 noa set undolevels=8 " Reset global and local value (without triggering autocmd) 978 let g:options = [['undolevels', '8', '8', '8', '2', 'global', 'set']] 979 set undolevels=2 980 call assert_equal([], g:options) 981 call assert_equal(g:opt[0], g:opt[1]) 982 983 984 " 25a: Setting global number local (to buffer) option" 985 noa setglobal wrapmargin=8 " Reset global and local value (without triggering autocmd) 986 noa setlocal wrapmargin=1 987 let g:options = [['wrapmargin', '8', '', '8', '2', 'global', 'setglobal']] 988 setglobal wrapmargin=2 989 call assert_equal([], g:options) 990 call assert_equal(g:opt[0], g:opt[1]) 991 992 " 25b: Setting local number local (to buffer) option" 993 noa setglobal wrapmargin=8 " Reset global and local value (without triggering autocmd) 994 noa setlocal wrapmargin=1 995 let g:options = [['wrapmargin', '1', '1', '', '2', 'local', 'setlocal']] 996 setlocal wrapmargin=2 997 call assert_equal([], g:options) 998 call assert_equal(g:opt[0], g:opt[1]) 999 1000 " 25c: Setting again number local (to buffer) option" 1001 noa setglobal wrapmargin=8 " Reset global and local value (without triggering autocmd) 1002 noa setlocal wrapmargin=1 1003 let g:options = [['wrapmargin', '1', '1', '8', '2', 'global', 'set']] 1004 set wrapmargin=2 1005 call assert_equal([], g:options) 1006 call assert_equal(g:opt[0], g:opt[1]) 1007 1008 " 25d: Setting again global number local (to buffer) option" 1009 noa set wrapmargin=8 " Reset global and local value (without triggering autocmd) 1010 let g:options = [['wrapmargin', '8', '8', '8', '2', 'global', 'set']] 1011 set wrapmargin=2 1012 call assert_equal([], g:options) 1013 call assert_equal(g:opt[0], g:opt[1]) 1014 1015 1016 " 26: Setting number global-local (to window) option. 1017 " Such option does currently not exist. 1018 1019 1020 " 27a: Setting global number local (to window) option" 1021 noa setglobal foldcolumn=8 " Reset global and local value (without triggering autocmd) 1022 noa setlocal foldcolumn=1 1023 let g:options = [['foldcolumn', '8', '', '8', '2', 'global', 'setglobal']] 1024 setglobal foldcolumn=2 1025 call assert_equal([], g:options) 1026 call assert_equal(g:opt[0], g:opt[1]) 1027 1028 " 27b: Setting local number local (to window) option" 1029 noa setglobal foldcolumn=8 " Reset global and local value (without triggering autocmd) 1030 noa setlocal foldcolumn=1 1031 let g:options = [['foldcolumn', '1', '1', '', '2', 'local', 'setlocal']] 1032 setlocal foldcolumn=2 1033 call assert_equal([], g:options) 1034 call assert_equal(g:opt[0], g:opt[1]) 1035 1036 " 27c: Setting again number local (to window) option" 1037 noa setglobal foldcolumn=8 " Reset global and local value (without triggering autocmd) 1038 noa setlocal foldcolumn=1 1039 let g:options = [['foldcolumn', '1', '1', '8', '2', 'global', 'set']] 1040 set foldcolumn=2 1041 call assert_equal([], g:options) 1042 call assert_equal(g:opt[0], g:opt[1]) 1043 1044 " 27d: Ssettin again global number local (to window) option" 1045 noa set foldcolumn=8 " Reset global and local value (without triggering autocmd) 1046 let g:options = [['foldcolumn', '8', '8', '8', '2', 'global', 'set']] 1047 set foldcolumn=2 1048 call assert_equal([], g:options) 1049 call assert_equal(g:opt[0], g:opt[1]) 1050 1051 1052 " 28a: Setting global boolean global option" 1053 noa setglobal nowrapscan " Reset global and local value (without triggering autocmd) 1054 noa setlocal wrapscan " Sets the global(!) value! 1055 let g:options = [['wrapscan', '1', '', '1', '0', 'global', 'setglobal']] 1056 setglobal nowrapscan 1057 call assert_equal([], g:options) 1058 call assert_equal(g:opt[0], g:opt[1]) 1059 1060 " 28b: Setting local boolean global option" 1061 noa setglobal nowrapscan " Reset global and local value (without triggering autocmd) 1062 noa setlocal wrapscan " Sets the global(!) value! 1063 let g:options = [['wrapscan', '1', '1', '', '0', 'local', 'setlocal']] 1064 setlocal nowrapscan 1065 call assert_equal([], g:options) 1066 call assert_equal(g:opt[0], g:opt[1]) 1067 1068 " 28c: Setting again boolean global option" 1069 noa setglobal nowrapscan " Reset global and local value (without triggering autocmd) 1070 noa setlocal wrapscan " Sets the global(!) value! 1071 let g:options = [['wrapscan', '1', '1', '1', '0', 'global', 'set']] 1072 set nowrapscan 1073 call assert_equal([], g:options) 1074 call assert_equal(g:opt[0], g:opt[1]) 1075 1076 " 28d: Setting again global boolean global option" 1077 noa set nowrapscan " Reset global and local value (without triggering autocmd) 1078 let g:options = [['wrapscan', '0', '0', '0', '1', 'global', 'set']] 1079 set wrapscan 1080 call assert_equal([], g:options) 1081 call assert_equal(g:opt[0], g:opt[1]) 1082 1083 1084 " 29a: Setting global boolean global-local (to buffer) option" 1085 noa setglobal noautoread " Reset global and local value (without triggering autocmd) 1086 noa setlocal autoread 1087 let g:options = [['autoread', '0', '', '0', '1', 'global', 'setglobal']] 1088 setglobal autoread 1089 call assert_equal([], g:options) 1090 call assert_equal(g:opt[0], g:opt[1]) 1091 1092 " 29b: Setting local boolean global-local (to buffer) option" 1093 noa setglobal noautoread " Reset global and local value (without triggering autocmd) 1094 noa setlocal autoread 1095 let g:options = [['autoread', '1', '1', '', '0', 'local', 'setlocal']] 1096 setlocal noautoread 1097 call assert_equal([], g:options) 1098 call assert_equal(g:opt[0], g:opt[1]) 1099 1100 " 29c: Setting again boolean global-local (to buffer) option" 1101 noa setglobal noautoread " Reset global and local value (without triggering autocmd) 1102 noa setlocal autoread 1103 let g:options = [['autoread', '1', '1', '0', '1', 'global', 'set']] 1104 set autoread 1105 call assert_equal([], g:options) 1106 call assert_equal(g:opt[0], g:opt[1]) 1107 1108 " 29d: Setting again global boolean global-local (to buffer) option" 1109 noa set noautoread " Reset global and local value (without triggering autocmd) 1110 let g:options = [['autoread', '0', '0', '0', '1', 'global', 'set']] 1111 set autoread 1112 call assert_equal([], g:options) 1113 call assert_equal(g:opt[0], g:opt[1]) 1114 1115 1116 " 30a: Setting global boolean local (to buffer) option" 1117 noa setglobal nocindent " Reset global and local value (without triggering autocmd) 1118 noa setlocal cindent 1119 let g:options = [['cindent', '0', '', '0', '1', 'global', 'setglobal']] 1120 setglobal cindent 1121 call assert_equal([], g:options) 1122 call assert_equal(g:opt[0], g:opt[1]) 1123 1124 " 30b: Setting local boolean local (to buffer) option" 1125 noa setglobal nocindent " Reset global and local value (without triggering autocmd) 1126 noa setlocal cindent 1127 let g:options = [['cindent', '1', '1', '', '0', 'local', 'setlocal']] 1128 setlocal nocindent 1129 call assert_equal([], g:options) 1130 call assert_equal(g:opt[0], g:opt[1]) 1131 1132 " 30c: Setting again boolean local (to buffer) option" 1133 noa setglobal nocindent " Reset global and local value (without triggering autocmd) 1134 noa setlocal cindent 1135 let g:options = [['cindent', '1', '1', '0', '1', 'global', 'set']] 1136 set cindent 1137 call assert_equal([], g:options) 1138 call assert_equal(g:opt[0], g:opt[1]) 1139 1140 " 30d: Setting again global boolean local (to buffer) option" 1141 noa set nocindent " Reset global and local value (without triggering autocmd) 1142 let g:options = [['cindent', '0', '0', '0', '1', 'global', 'set']] 1143 set cindent 1144 call assert_equal([], g:options) 1145 call assert_equal(g:opt[0], g:opt[1]) 1146 1147 1148 " 31: Setting boolean global-local (to window) option 1149 " Currently no such option exists. 1150 1151 1152 " 32a: Setting global boolean local (to window) option" 1153 noa setglobal nocursorcolumn " Reset global and local value (without triggering autocmd) 1154 noa setlocal cursorcolumn 1155 let g:options = [['cursorcolumn', '0', '', '0', '1', 'global', 'setglobal']] 1156 setglobal cursorcolumn 1157 call assert_equal([], g:options) 1158 call assert_equal(g:opt[0], g:opt[1]) 1159 1160 " 32b: Setting local boolean local (to window) option" 1161 noa setglobal nocursorcolumn " Reset global and local value (without triggering autocmd) 1162 noa setlocal cursorcolumn 1163 let g:options = [['cursorcolumn', '1', '1', '', '0', 'local', 'setlocal']] 1164 setlocal nocursorcolumn 1165 call assert_equal([], g:options) 1166 call assert_equal(g:opt[0], g:opt[1]) 1167 1168 " 32c: Setting again boolean local (to window) option" 1169 noa setglobal nocursorcolumn " Reset global and local value (without triggering autocmd) 1170 noa setlocal cursorcolumn 1171 let g:options = [['cursorcolumn', '1', '1', '0', '1', 'global', 'set']] 1172 set cursorcolumn 1173 call assert_equal([], g:options) 1174 call assert_equal(g:opt[0], g:opt[1]) 1175 1176 " 32d: Setting again global boolean local (to window) option" 1177 noa set nocursorcolumn " Reset global and local value (without triggering autocmd) 1178 let g:options = [['cursorcolumn', '0', '0', '0', '1', 'global', 'set']] 1179 set cursorcolumn 1180 call assert_equal([], g:options) 1181 call assert_equal(g:opt[0], g:opt[1]) 1182 1183 1184 " 33: Test autocommands when an option value is converted internally. 1185 noa set backspace=1 " Reset global and local value (without triggering autocmd) 1186 let g:options = [['backspace', 'indent,eol', 'indent,eol', 'indent,eol', '2', 'global', 'set']] 1187 set backspace=2 1188 call assert_equal([], g:options) 1189 call assert_equal(g:opt[0], g:opt[1]) 1190 1191 1192 " Cleanup 1193 au! OptionSet 1194 " set tags& 1195 for opt in ['nu', 'ai', 'acd', 'ar', 'bs', 'backup', 'cul', 'cp', 'backupext', 'tags', 'spelllang', 'statusline', 'foldignore', 'cmdheight', 'undolevels', 'wrapmargin', 'foldcolumn', 'wrapscan', 'autoread', 'cindent', 'cursorcolumn'] 1196 exe printf(":set %s&vim", opt) 1197 endfor 1198 call test_override('starting', 0) 1199 delfunc! AutoCommandOptionSet 1200endfunc 1201 1202func Test_OptionSet_diffmode() 1203 call test_override('starting', 1) 1204 " 18: Changing an option when entering diff mode 1205 new 1206 au OptionSet diff :let &l:cul = v:option_new 1207 1208 call setline(1, ['buffer 1', 'line2', 'line3', 'line4']) 1209 call assert_equal(0, &l:cul) 1210 diffthis 1211 call assert_equal(1, &l:cul) 1212 1213 vnew 1214 call setline(1, ['buffer 2', 'line 2', 'line 3', 'line4']) 1215 call assert_equal(0, &l:cul) 1216 diffthis 1217 call assert_equal(1, &l:cul) 1218 1219 diffoff 1220 call assert_equal(0, &l:cul) 1221 call assert_equal(1, getwinvar(2, '&l:cul')) 1222 bw! 1223 1224 call assert_equal(1, &l:cul) 1225 diffoff! 1226 call assert_equal(0, &l:cul) 1227 call assert_equal(0, getwinvar(1, '&l:cul')) 1228 bw! 1229 1230 " Cleanup 1231 au! OptionSet 1232 call test_override('starting', 0) 1233endfunc 1234 1235func Test_OptionSet_diffmode_close() 1236 call test_override('starting', 1) 1237 " 19: Try to close the current window when entering diff mode 1238 " should not segfault 1239 new 1240 au OptionSet diff close 1241 1242 call setline(1, ['buffer 1', 'line2', 'line3', 'line4']) 1243 call assert_fails(':diffthis', 'E788:') 1244 call assert_equal(1, &diff) 1245 vnew 1246 call setline(1, ['buffer 2', 'line 2', 'line 3', 'line4']) 1247 call assert_fails(':diffthis', 'E788:') 1248 call assert_equal(1, &diff) 1249 set diffopt-=closeoff 1250 bw! 1251 call assert_fails(':diffoff!', 'E788:') 1252 bw! 1253 1254 " Cleanup 1255 au! OptionSet 1256 call test_override('starting', 0) 1257 "delfunc! AutoCommandOptionSet 1258endfunc 1259 1260" Test for Bufleave autocommand that deletes the buffer we are about to edit. 1261func Test_BufleaveWithDelete() 1262 new | edit Xfile1 1263 1264 augroup test_bufleavewithdelete 1265 autocmd! 1266 autocmd BufLeave Xfile1 bwipe Xfile2 1267 augroup END 1268 1269 call assert_fails('edit Xfile2', 'E143:') 1270 call assert_equal('Xfile1', bufname('%')) 1271 1272 autocmd! test_bufleavewithdelete BufLeave Xfile1 1273 augroup! test_bufleavewithdelete 1274 1275 new 1276 bwipe! Xfile1 1277endfunc 1278 1279" Test for autocommand that changes the buffer list, when doing ":ball". 1280func Test_Acmd_BufAll() 1281 enew! 1282 %bwipe! 1283 call writefile(['Test file Xxx1'], 'Xxx1') 1284 call writefile(['Test file Xxx2'], 'Xxx2') 1285 call writefile(['Test file Xxx3'], 'Xxx3') 1286 1287 " Add three files to the buffer list 1288 split Xxx1 1289 close 1290 split Xxx2 1291 close 1292 split Xxx3 1293 close 1294 1295 " Wipe the buffer when the buffer is opened 1296 au BufReadPost Xxx2 bwipe 1297 1298 call append(0, 'Test file Xxx4') 1299 ball 1300 1301 call assert_equal(2, winnr('$')) 1302 call assert_equal('Xxx1', bufname(winbufnr(winnr('$')))) 1303 wincmd t 1304 1305 au! BufReadPost 1306 %bwipe! 1307 call delete('Xxx1') 1308 call delete('Xxx2') 1309 call delete('Xxx3') 1310 enew! | only 1311endfunc 1312 1313" Test for autocommand that changes current buffer on BufEnter event. 1314" Check if modelines are interpreted for the correct buffer. 1315func Test_Acmd_BufEnter() 1316 %bwipe! 1317 call writefile(['start of test file Xxx1', 1318 \ "\<Tab>this is a test", 1319 \ 'end of test file Xxx1'], 'Xxx1') 1320 call writefile(['start of test file Xxx2', 1321 \ 'vim: set noai :', 1322 \ "\<Tab>this is a test", 1323 \ 'end of test file Xxx2'], 'Xxx2') 1324 1325 au BufEnter Xxx2 brew 1326 set ai modeline modelines=3 1327 edit Xxx1 1328 " edit Xxx2, autocmd will do :brew 1329 edit Xxx2 1330 exe "normal G?this is a\<CR>" 1331 " Append text with autoindent to this file 1332 normal othis should be auto-indented 1333 call assert_equal("\<Tab>this should be auto-indented", getline('.')) 1334 call assert_equal(3, line('.')) 1335 " Remove autocmd and edit Xxx2 again 1336 au! BufEnter Xxx2 1337 buf! Xxx2 1338 exe "normal G?this is a\<CR>" 1339 " append text without autoindent to Xxx 1340 normal othis should be in column 1 1341 call assert_equal("this should be in column 1", getline('.')) 1342 call assert_equal(4, line('.')) 1343 1344 %bwipe! 1345 call delete('Xxx1') 1346 call delete('Xxx2') 1347 set ai&vim modeline&vim modelines&vim 1348endfunc 1349 1350" Test for issue #57 1351" do not move cursor on <c-o> when autoindent is set 1352func Test_ai_CTRL_O() 1353 enew! 1354 set ai 1355 let save_fo = &fo 1356 set fo+=r 1357 exe "normal o# abcdef\<Esc>2hi\<CR>\<C-O>d0\<Esc>" 1358 exe "normal o# abcdef\<Esc>2hi\<C-O>d0\<Esc>" 1359 call assert_equal(['# abc', 'def', 'def'], getline(2, 4)) 1360 1361 set ai&vim 1362 let &fo = save_fo 1363 enew! 1364endfunc 1365 1366" Test for autocommand that deletes the current buffer on BufLeave event. 1367" Also test deleting the last buffer, should give a new, empty buffer. 1368func Test_BufLeave_Wipe() 1369 %bwipe! 1370 let content = ['start of test file Xxx', 1371 \ 'this is a test', 1372 \ 'end of test file Xxx'] 1373 call writefile(content, 'Xxx1') 1374 call writefile(content, 'Xxx2') 1375 1376 au BufLeave Xxx2 bwipe 1377 edit Xxx1 1378 split Xxx2 1379 " delete buffer Xxx2, we should be back to Xxx1 1380 bwipe 1381 call assert_equal('Xxx1', bufname('%')) 1382 call assert_equal(1, winnr('$')) 1383 1384 " Create an alternate buffer 1385 %write! test.out 1386 call assert_equal('test.out', bufname('#')) 1387 " delete alternate buffer 1388 bwipe test.out 1389 call assert_equal('Xxx1', bufname('%')) 1390 call assert_equal('', bufname('#')) 1391 1392 au BufLeave Xxx1 bwipe 1393 " delete current buffer, get an empty one 1394 bwipe! 1395 call assert_equal(1, line('$')) 1396 call assert_equal('', bufname('%')) 1397 let g:bufinfo = getbufinfo() 1398 call assert_equal(1, len(g:bufinfo)) 1399 1400 call delete('Xxx1') 1401 call delete('Xxx2') 1402 call delete('test.out') 1403 %bwipe 1404 au! BufLeave 1405 1406 " check that bufinfo doesn't contain a pointer to freed memory 1407 call test_garbagecollect_now() 1408endfunc 1409 1410func Test_QuitPre() 1411 edit Xfoo 1412 let winid = win_getid(winnr()) 1413 split Xbar 1414 au! QuitPre * let g:afile = expand('<afile>') 1415 " Close the other window, <afile> should be correct. 1416 exe win_id2win(winid) . 'q' 1417 call assert_equal('Xfoo', g:afile) 1418 1419 unlet g:afile 1420 bwipe Xfoo 1421 bwipe Xbar 1422endfunc 1423 1424func Test_Cmdline() 1425 au! CmdlineChanged : let g:text = getcmdline() 1426 let g:text = 0 1427 call feedkeys(":echom 'hello'\<CR>", 'xt') 1428 call assert_equal("echom 'hello'", g:text) 1429 au! CmdlineChanged 1430 1431 au! CmdlineChanged : let g:entered = expand('<afile>') 1432 let g:entered = 0 1433 call feedkeys(":echom 'hello'\<CR>", 'xt') 1434 call assert_equal(':', g:entered) 1435 au! CmdlineChanged 1436 1437 au! CmdlineEnter : let g:entered = expand('<afile>') 1438 au! CmdlineLeave : let g:left = expand('<afile>') 1439 let g:entered = 0 1440 let g:left = 0 1441 call feedkeys(":echo 'hello'\<CR>", 'xt') 1442 call assert_equal(':', g:entered) 1443 call assert_equal(':', g:left) 1444 au! CmdlineEnter 1445 au! CmdlineLeave 1446 1447 let save_shellslash = &shellslash 1448 set noshellslash 1449 au! CmdlineEnter / let g:entered = expand('<afile>') 1450 au! CmdlineLeave / let g:left = expand('<afile>') 1451 let g:entered = 0 1452 let g:left = 0 1453 new 1454 call setline(1, 'hello') 1455 call feedkeys("/hello\<CR>", 'xt') 1456 call assert_equal('/', g:entered) 1457 call assert_equal('/', g:left) 1458 bwipe! 1459 au! CmdlineEnter 1460 au! CmdlineLeave 1461 let &shellslash = save_shellslash 1462endfunc 1463 1464" Test for BufWritePre autocommand that deletes or unloads the buffer. 1465func Test_BufWritePre() 1466 %bwipe 1467 au BufWritePre Xxx1 bunload 1468 au BufWritePre Xxx2 bwipe 1469 1470 call writefile(['start of Xxx1', 'test', 'end of Xxx1'], 'Xxx1') 1471 call writefile(['start of Xxx2', 'test', 'end of Xxx2'], 'Xxx2') 1472 1473 edit Xtest 1474 e! Xxx2 1475 bdel Xtest 1476 e Xxx1 1477 " write it, will unload it and give an error msg 1478 call assert_fails('w', 'E203:') 1479 call assert_equal('Xxx2', bufname('%')) 1480 edit Xtest 1481 e! Xxx2 1482 bwipe Xtest 1483 " write it, will delete the buffer and give an error msg 1484 call assert_fails('w', 'E203:') 1485 call assert_equal('Xxx1', bufname('%')) 1486 au! BufWritePre 1487 call delete('Xxx1') 1488 call delete('Xxx2') 1489endfunc 1490 1491" Test for BufUnload autocommand that unloads all the other buffers 1492func Test_bufunload_all() 1493 call writefile(['Test file Xxx1'], 'Xxx1')" 1494 call writefile(['Test file Xxx2'], 'Xxx2')" 1495 1496 let content =<< trim [CODE] 1497 func UnloadAllBufs() 1498 let i = 1 1499 while i <= bufnr('$') 1500 if i != bufnr('%') && bufloaded(i) 1501 exe i . 'bunload' 1502 endif 1503 let i += 1 1504 endwhile 1505 endfunc 1506 au BufUnload * call UnloadAllBufs() 1507 au VimLeave * call writefile(['Test Finished'], 'Xout') 1508 edit Xxx1 1509 split Xxx2 1510 q 1511 [CODE] 1512 1513 call writefile(content, 'Xtest') 1514 1515 call delete('Xout') 1516 call system(GetVimCommandClean() .. ' -N --not-a-term -S Xtest') 1517 call assert_true(filereadable('Xout')) 1518 1519 call delete('Xxx1') 1520 call delete('Xxx2') 1521 call delete('Xtest') 1522 call delete('Xout') 1523endfunc 1524 1525" Some tests for buffer-local autocommands 1526func Test_buflocal_autocmd() 1527 let g:bname = '' 1528 edit xx 1529 au BufLeave <buffer> let g:bname = expand("%") 1530 " here, autocommand for xx should trigger. 1531 " but autocommand shall not apply to buffer named <buffer>. 1532 edit somefile 1533 call assert_equal('xx', g:bname) 1534 let g:bname = '' 1535 " here, autocommand shall be auto-deleted 1536 bwipe xx 1537 " autocmd should not trigger 1538 edit xx 1539 call assert_equal('', g:bname) 1540 " autocmd should not trigger 1541 edit somefile 1542 call assert_equal('', g:bname) 1543 enew 1544 unlet g:bname 1545endfunc 1546 1547" Test for "*Cmd" autocommands 1548func Test_Cmd_Autocmds() 1549 call writefile(['start of Xxx', "\tabc2", 'end of Xxx'], 'Xxx') 1550 1551 enew! 1552 au BufReadCmd XtestA 0r Xxx|$del 1553 edit XtestA " will read text of Xxd instead 1554 call assert_equal('start of Xxx', getline(1)) 1555 1556 au BufWriteCmd XtestA call append(line("$"), "write") 1557 write " will append a line to the file 1558 call assert_equal('write', getline('$')) 1559 call assert_fails('read XtestA', 'E484:') " should not read anything 1560 call assert_equal('write', getline(4)) 1561 1562 " now we have: 1563 " 1 start of Xxx 1564 " 2 abc2 1565 " 3 end of Xxx 1566 " 4 write 1567 1568 au FileReadCmd XtestB '[r Xxx 1569 2r XtestB " will read Xxx below line 2 instead 1570 call assert_equal('start of Xxx', getline(3)) 1571 1572 " now we have: 1573 " 1 start of Xxx 1574 " 2 abc2 1575 " 3 start of Xxx 1576 " 4 abc2 1577 " 5 end of Xxx 1578 " 6 end of Xxx 1579 " 7 write 1580 1581 au FileWriteCmd XtestC '[,']copy $ 1582 normal 4GA1 1583 4,5w XtestC " will copy lines 4 and 5 to the end 1584 call assert_equal("\tabc21", getline(8)) 1585 call assert_fails('r XtestC', 'E484:') " should not read anything 1586 call assert_equal("end of Xxx", getline(9)) 1587 1588 " now we have: 1589 " 1 start of Xxx 1590 " 2 abc2 1591 " 3 start of Xxx 1592 " 4 abc21 1593 " 5 end of Xxx 1594 " 6 end of Xxx 1595 " 7 write 1596 " 8 abc21 1597 " 9 end of Xxx 1598 1599 let g:lines = [] 1600 au FileAppendCmd XtestD call extend(g:lines, getline(line("'["), line("']"))) 1601 w >>XtestD " will add lines to 'lines' 1602 call assert_equal(9, len(g:lines)) 1603 call assert_fails('$r XtestD', 'E484:') " should not read anything 1604 call assert_equal(9, line('$')) 1605 call assert_equal('end of Xxx', getline('$')) 1606 1607 au BufReadCmd XtestE 0r Xxx|$del 1608 sp XtestE " split window with test.out 1609 call assert_equal('end of Xxx', getline(3)) 1610 1611 let g:lines = [] 1612 exe "normal 2Goasdf\<Esc>\<C-W>\<C-W>" 1613 au BufWriteCmd XtestE call extend(g:lines, getline(0, '$')) 1614 wall " will write other window to 'lines' 1615 call assert_equal(4, len(g:lines), g:lines) 1616 call assert_equal('asdf', g:lines[2]) 1617 1618 au! BufReadCmd 1619 au! BufWriteCmd 1620 au! FileReadCmd 1621 au! FileWriteCmd 1622 au! FileAppendCmd 1623 %bwipe! 1624 call delete('Xxx') 1625 enew! 1626endfunc 1627 1628func s:ReadFile() 1629 setl noswapfile nomodified 1630 let filename = resolve(expand("<afile>:p")) 1631 execute 'read' fnameescape(filename) 1632 1d_ 1633 exe 'file' fnameescape(filename) 1634 setl buftype=acwrite 1635endfunc 1636 1637func s:WriteFile() 1638 let filename = resolve(expand("<afile>:p")) 1639 setl buftype= 1640 noautocmd execute 'write' fnameescape(filename) 1641 setl buftype=acwrite 1642 setl nomodified 1643endfunc 1644 1645func Test_BufReadCmd() 1646 autocmd BufReadCmd *.test call s:ReadFile() 1647 autocmd BufWriteCmd *.test call s:WriteFile() 1648 1649 call writefile(['one', 'two', 'three'], 'Xcmd.test') 1650 edit Xcmd.test 1651 call assert_match('Xcmd.test" line 1 of 3', execute('file')) 1652 normal! Gofour 1653 write 1654 call assert_equal(['one', 'two', 'three', 'four'], readfile('Xcmd.test')) 1655 1656 bwipe! 1657 call delete('Xcmd.test') 1658 au! BufReadCmd 1659 au! BufWriteCmd 1660endfunc 1661 1662func SetChangeMarks(start, end) 1663 exe a:start .. 'mark [' 1664 exe a:end .. 'mark ]' 1665endfunc 1666 1667" Verify the effects of autocmds on '[ and '] 1668func Test_change_mark_in_autocmds() 1669 edit! Xtest 1670 call feedkeys("ia\<CR>b\<CR>c\<CR>d\<C-g>u\<Esc>", 'xtn') 1671 1672 call SetChangeMarks(2, 3) 1673 write 1674 call assert_equal([1, 4], [line("'["), line("']")]) 1675 1676 call SetChangeMarks(2, 3) 1677 au BufWritePre * call assert_equal([1, 4], [line("'["), line("']")]) 1678 write 1679 au! BufWritePre 1680 1681 if has('unix') 1682 write XtestFilter 1683 write >> XtestFilter 1684 1685 call SetChangeMarks(2, 3) 1686 " Marks are set to the entire range of the write 1687 au FilterWritePre * call assert_equal([1, 4], [line("'["), line("']")]) 1688 " '[ is adjusted to just before the line that will receive the filtered 1689 " data 1690 au FilterReadPre * call assert_equal([4, 4], [line("'["), line("']")]) 1691 " The filtered data is read into the buffer, and the source lines are 1692 " still present, so the range is after the source lines 1693 au FilterReadPost * call assert_equal([5, 12], [line("'["), line("']")]) 1694 %!cat XtestFilter 1695 " After the filtered data is read, the original lines are deleted 1696 call assert_equal([1, 8], [line("'["), line("']")]) 1697 au! FilterWritePre,FilterReadPre,FilterReadPost 1698 undo 1699 1700 call SetChangeMarks(1, 4) 1701 au FilterWritePre * call assert_equal([2, 3], [line("'["), line("']")]) 1702 au FilterReadPre * call assert_equal([3, 3], [line("'["), line("']")]) 1703 au FilterReadPost * call assert_equal([4, 11], [line("'["), line("']")]) 1704 2,3!cat XtestFilter 1705 call assert_equal([2, 9], [line("'["), line("']")]) 1706 au! FilterWritePre,FilterReadPre,FilterReadPost 1707 undo 1708 1709 call delete('XtestFilter') 1710 endif 1711 1712 call SetChangeMarks(1, 4) 1713 au FileWritePre * call assert_equal([2, 3], [line("'["), line("']")]) 1714 2,3write Xtest2 1715 au! FileWritePre 1716 1717 call SetChangeMarks(2, 3) 1718 au FileAppendPre * call assert_equal([1, 4], [line("'["), line("']")]) 1719 write >> Xtest2 1720 au! FileAppendPre 1721 1722 call SetChangeMarks(1, 4) 1723 au FileAppendPre * call assert_equal([2, 3], [line("'["), line("']")]) 1724 2,3write >> Xtest2 1725 au! FileAppendPre 1726 1727 call SetChangeMarks(1, 1) 1728 au FileReadPre * call assert_equal([3, 1], [line("'["), line("']")]) 1729 au FileReadPost * call assert_equal([4, 11], [line("'["), line("']")]) 1730 3read Xtest2 1731 au! FileReadPre,FileReadPost 1732 undo 1733 1734 call SetChangeMarks(4, 4) 1735 " When the line is 0, it's adjusted to 1 1736 au FileReadPre * call assert_equal([1, 4], [line("'["), line("']")]) 1737 au FileReadPost * call assert_equal([1, 8], [line("'["), line("']")]) 1738 0read Xtest2 1739 au! FileReadPre,FileReadPost 1740 undo 1741 1742 call SetChangeMarks(4, 4) 1743 " When the line is 0, it's adjusted to 1 1744 au FileReadPre * call assert_equal([1, 4], [line("'["), line("']")]) 1745 au FileReadPost * call assert_equal([2, 9], [line("'["), line("']")]) 1746 1read Xtest2 1747 au! FileReadPre,FileReadPost 1748 undo 1749 1750 bwipe! 1751 call delete('Xtest') 1752 call delete('Xtest2') 1753endfunc 1754 1755func Test_Filter_noshelltemp() 1756 CheckExecutable cat 1757 1758 enew! 1759 call setline(1, ['a', 'b', 'c', 'd']) 1760 1761 let shelltemp = &shelltemp 1762 set shelltemp 1763 1764 let g:filter_au = 0 1765 au FilterWritePre * let g:filter_au += 1 1766 au FilterReadPre * let g:filter_au += 1 1767 au FilterReadPost * let g:filter_au += 1 1768 %!cat 1769 call assert_equal(3, g:filter_au) 1770 1771 if has('filterpipe') 1772 set noshelltemp 1773 1774 let g:filter_au = 0 1775 au FilterWritePre * let g:filter_au += 1 1776 au FilterReadPre * let g:filter_au += 1 1777 au FilterReadPost * let g:filter_au += 1 1778 %!cat 1779 call assert_equal(0, g:filter_au) 1780 endif 1781 1782 au! FilterWritePre,FilterReadPre,FilterReadPost 1783 let &shelltemp = shelltemp 1784 bwipe! 1785endfunc 1786 1787func Test_TextYankPost() 1788 enew! 1789 call setline(1, ['foo']) 1790 1791 let g:event = [] 1792 au TextYankPost * let g:event = copy(v:event) 1793 1794 call assert_equal({}, v:event) 1795 call assert_fails('let v:event = {}', 'E46:') 1796 call assert_fails('let v:event.mykey = 0', 'E742:') 1797 1798 norm "ayiw 1799 call assert_equal( 1800 \{'regcontents': ['foo'], 'regname': 'a', 'operator': 'y', 'regtype': 'v', 'visual': v:false}, 1801 \g:event) 1802 norm y_ 1803 call assert_equal( 1804 \{'regcontents': ['foo'], 'regname': '', 'operator': 'y', 'regtype': 'V', 'visual': v:false}, 1805 \g:event) 1806 norm Vy 1807 call assert_equal( 1808 \{'regcontents': ['foo'], 'regname': '', 'operator': 'y', 'regtype': 'V', 'visual': v:true}, 1809 \g:event) 1810 call feedkeys("\<C-V>y", 'x') 1811 call assert_equal( 1812 \{'regcontents': ['f'], 'regname': '', 'operator': 'y', 'regtype': "\x161", 'visual': v:true}, 1813 \g:event) 1814 norm "xciwbar 1815 call assert_equal( 1816 \{'regcontents': ['foo'], 'regname': 'x', 'operator': 'c', 'regtype': 'v', 'visual': v:false}, 1817 \g:event) 1818 norm "bdiw 1819 call assert_equal( 1820 \{'regcontents': ['bar'], 'regname': 'b', 'operator': 'd', 'regtype': 'v', 'visual': v:false}, 1821 \g:event) 1822 1823 call assert_equal({}, v:event) 1824 1825 if has('clipboard_working') && !has('gui_running') 1826 " Test that when the visual selection is automatically copied to clipboard 1827 " register a TextYankPost is emitted 1828 call setline(1, ['foobar']) 1829 1830 let @* = '' 1831 set clipboard=autoselect 1832 exe "norm! ggviw\<Esc>" 1833 call assert_equal( 1834 \{'regcontents': ['foobar'], 'regname': '*', 'operator': 'y', 'regtype': 'v', 'visual': v:true}, 1835 \g:event) 1836 1837 let @+ = '' 1838 set clipboard=autoselectplus 1839 exe "norm! ggviw\<Esc>" 1840 call assert_equal( 1841 \{'regcontents': ['foobar'], 'regname': '+', 'operator': 'y', 'regtype': 'v', 'visual': v:true}, 1842 \g:event) 1843 1844 set clipboard&vim 1845 endif 1846 1847 au! TextYankPost 1848 unlet g:event 1849 bwipe! 1850endfunc 1851 1852func Test_autocommand_all_events() 1853 call assert_fails('au * * bwipe', 'E1155:') 1854 call assert_fails('au * x bwipe', 'E1155:') 1855endfunc 1856 1857function s:Before_test_dirchanged() 1858 augroup test_dirchanged 1859 autocmd! 1860 augroup END 1861 let s:li = [] 1862 let s:dir_this = getcwd() 1863 let s:dir_foo = s:dir_this . '/Xfoo' 1864 call mkdir(s:dir_foo) 1865 let s:dir_bar = s:dir_this . '/Xbar' 1866 call mkdir(s:dir_bar) 1867endfunc 1868 1869function s:After_test_dirchanged() 1870 call chdir(s:dir_this) 1871 call delete(s:dir_foo, 'd') 1872 call delete(s:dir_bar, 'd') 1873 augroup test_dirchanged 1874 autocmd! 1875 augroup END 1876endfunc 1877 1878function Test_dirchanged_global() 1879 call s:Before_test_dirchanged() 1880 autocmd test_dirchanged DirChanged global call add(s:li, "cd:") 1881 autocmd test_dirchanged DirChanged global call add(s:li, expand("<afile>")) 1882 call chdir(s:dir_foo) 1883 call assert_equal(["cd:", s:dir_foo], s:li) 1884 call chdir(s:dir_foo) 1885 call assert_equal(["cd:", s:dir_foo], s:li) 1886 exe 'lcd ' .. fnameescape(s:dir_bar) 1887 call assert_equal(["cd:", s:dir_foo], s:li) 1888 call s:After_test_dirchanged() 1889endfunc 1890 1891function Test_dirchanged_local() 1892 call s:Before_test_dirchanged() 1893 autocmd test_dirchanged DirChanged window call add(s:li, "lcd:") 1894 autocmd test_dirchanged DirChanged window call add(s:li, expand("<afile>")) 1895 call chdir(s:dir_foo) 1896 call assert_equal([], s:li) 1897 exe 'lcd ' .. fnameescape(s:dir_bar) 1898 call assert_equal(["lcd:", s:dir_bar], s:li) 1899 exe 'lcd ' .. fnameescape(s:dir_bar) 1900 call assert_equal(["lcd:", s:dir_bar], s:li) 1901 call s:After_test_dirchanged() 1902endfunc 1903 1904function Test_dirchanged_auto() 1905 CheckOption autochdir 1906 call s:Before_test_dirchanged() 1907 call test_autochdir() 1908 autocmd test_dirchanged DirChanged auto call add(s:li, "auto:") 1909 autocmd test_dirchanged DirChanged auto call add(s:li, expand("<afile>")) 1910 set acd 1911 cd .. 1912 call assert_equal([], s:li) 1913 exe 'edit ' . s:dir_foo . '/Xfile' 1914 call assert_equal(s:dir_foo, getcwd()) 1915 call assert_equal(["auto:", s:dir_foo], s:li) 1916 set noacd 1917 bwipe! 1918 call s:After_test_dirchanged() 1919endfunc 1920 1921" Test TextChangedI and TextChangedP 1922func Test_ChangedP() 1923 new 1924 call setline(1, ['foo', 'bar', 'foobar']) 1925 call test_override("char_avail", 1) 1926 set complete=. completeopt=menuone 1927 1928 func! TextChangedAutocmd(char) 1929 let g:autocmd .= a:char 1930 endfunc 1931 1932 au! TextChanged <buffer> :call TextChangedAutocmd('N') 1933 au! TextChangedI <buffer> :call TextChangedAutocmd('I') 1934 au! TextChangedP <buffer> :call TextChangedAutocmd('P') 1935 1936 call cursor(3, 1) 1937 let g:autocmd = '' 1938 call feedkeys("o\<esc>", 'tnix') 1939 call assert_equal('I', g:autocmd) 1940 1941 let g:autocmd = '' 1942 call feedkeys("Sf", 'tnix') 1943 call assert_equal('II', g:autocmd) 1944 1945 let g:autocmd = '' 1946 call feedkeys("Sf\<C-N>", 'tnix') 1947 call assert_equal('IIP', g:autocmd) 1948 1949 let g:autocmd = '' 1950 call feedkeys("Sf\<C-N>\<C-N>", 'tnix') 1951 call assert_equal('IIPP', g:autocmd) 1952 1953 let g:autocmd = '' 1954 call feedkeys("Sf\<C-N>\<C-N>\<C-N>", 'tnix') 1955 call assert_equal('IIPPP', g:autocmd) 1956 1957 let g:autocmd = '' 1958 call feedkeys("Sf\<C-N>\<C-N>\<C-N>\<C-N>", 'tnix') 1959 call assert_equal('IIPPPP', g:autocmd) 1960 1961 call assert_equal(['foo', 'bar', 'foobar', 'foo'], getline(1, '$')) 1962 " TODO: how should it handle completeopt=noinsert,noselect? 1963 1964 " CleanUp 1965 call test_override("char_avail", 0) 1966 au! TextChanged 1967 au! TextChangedI 1968 au! TextChangedP 1969 delfu TextChangedAutocmd 1970 unlet! g:autocmd 1971 set complete&vim completeopt&vim 1972 1973 bw! 1974endfunc 1975 1976let g:setline_handled = v:false 1977func SetLineOne() 1978 if !g:setline_handled 1979 call setline(1, "(x)") 1980 let g:setline_handled = v:true 1981 endif 1982endfunc 1983 1984func Test_TextChangedI_with_setline() 1985 new 1986 call test_override('char_avail', 1) 1987 autocmd TextChangedI <buffer> call SetLineOne() 1988 call feedkeys("i(\<CR>\<Esc>", 'tx') 1989 call assert_equal('(', getline(1)) 1990 call assert_equal('x)', getline(2)) 1991 undo 1992 call assert_equal('', getline(1)) 1993 call assert_equal('', getline(2)) 1994 1995 call test_override('starting', 0) 1996 bwipe! 1997endfunc 1998 1999func Test_Changed_FirstTime() 2000 CheckFeature terminal 2001 CheckNotGui 2002 " Starting a terminal to run Vim is always considered flaky. 2003 let g:test_is_flaky = 1 2004 2005 " Prepare file for TextChanged event. 2006 call writefile([''], 'Xchanged.txt') 2007 let buf = term_start([GetVimProg(), '--clean', '-c', 'set noswapfile'], {'term_rows': 3}) 2008 call assert_equal('running', term_getstatus(buf)) 2009 " Wait for the ruler (in the status line) to be shown. 2010 " In ConPTY, there is additional character which is drawn up to the width of 2011 " the screen. 2012 if has('conpty') 2013 call WaitForAssert({-> assert_match('\<All.*$', term_getline(buf, 3))}) 2014 else 2015 call WaitForAssert({-> assert_match('\<All$', term_getline(buf, 3))}) 2016 endif 2017 " It's only adding autocmd, so that no event occurs. 2018 call term_sendkeys(buf, ":au! TextChanged <buffer> call writefile(['No'], 'Xchanged.txt')\<cr>") 2019 call term_sendkeys(buf, "\<C-\\>\<C-N>:qa!\<cr>") 2020 call WaitForAssert({-> assert_equal('finished', term_getstatus(buf))}) 2021 call assert_equal([''], readfile('Xchanged.txt')) 2022 2023 " clean up 2024 call delete('Xchanged.txt') 2025 bwipe! 2026endfunc 2027 2028func Test_autocmd_nested() 2029 let g:did_nested = 0 2030 augroup Testing 2031 au WinNew * edit somefile 2032 au BufNew * let g:did_nested = 1 2033 augroup END 2034 split 2035 call assert_equal(0, g:did_nested) 2036 close 2037 bwipe! somefile 2038 2039 " old nested argument still works 2040 augroup Testing 2041 au! 2042 au WinNew * nested edit somefile 2043 au BufNew * let g:did_nested = 1 2044 augroup END 2045 split 2046 call assert_equal(1, g:did_nested) 2047 close 2048 bwipe! somefile 2049 2050 " New ++nested argument works 2051 augroup Testing 2052 au! 2053 au WinNew * ++nested edit somefile 2054 au BufNew * let g:did_nested = 1 2055 augroup END 2056 split 2057 call assert_equal(1, g:did_nested) 2058 close 2059 bwipe! somefile 2060 2061 augroup Testing 2062 au! 2063 augroup END 2064 2065 call assert_fails('au WinNew * ++nested ++nested echo bad', 'E983:') 2066 call assert_fails('au WinNew * nested nested echo bad', 'E983:') 2067endfunc 2068 2069func Test_autocmd_once() 2070 " Without ++once WinNew triggers twice 2071 let g:did_split = 0 2072 augroup Testing 2073 au WinNew * let g:did_split += 1 2074 augroup END 2075 split 2076 split 2077 call assert_equal(2, g:did_split) 2078 call assert_true(exists('#WinNew')) 2079 close 2080 close 2081 2082 " With ++once WinNew triggers once 2083 let g:did_split = 0 2084 augroup Testing 2085 au! 2086 au WinNew * ++once let g:did_split += 1 2087 augroup END 2088 split 2089 split 2090 call assert_equal(1, g:did_split) 2091 call assert_false(exists('#WinNew')) 2092 close 2093 close 2094 2095 call assert_fails('au WinNew * ++once ++once echo bad', 'E983:') 2096endfunc 2097 2098func Test_autocmd_bufreadpre() 2099 new 2100 let b:bufreadpre = 1 2101 call append(0, range(1000)) 2102 w! XAutocmdBufReadPre.txt 2103 autocmd BufReadPre <buffer> :let b:bufreadpre += 1 2104 norm! 500gg 2105 sp 2106 norm! 1000gg 2107 wincmd p 2108 let g:wsv1 = winsaveview() 2109 wincmd p 2110 let g:wsv2 = winsaveview() 2111 " triggers BufReadPre, should not move the cursor in either window 2112 " The topline may change one line in a large window. 2113 edit 2114 call assert_inrange(g:wsv2.topline - 1, g:wsv2.topline + 1, winsaveview().topline) 2115 call assert_equal(g:wsv2.lnum, winsaveview().lnum) 2116 call assert_equal(2, b:bufreadpre) 2117 wincmd p 2118 call assert_equal(g:wsv1.topline, winsaveview().topline) 2119 call assert_equal(g:wsv1.lnum, winsaveview().lnum) 2120 call assert_equal(2, b:bufreadpre) 2121 " Now set the cursor position in an BufReadPre autocommand 2122 " (even though the position will be invalid, this should make Vim reset the 2123 " cursor position in the other window. 2124 wincmd p 2125 set cpo+=g 2126 " won't do anything, but try to set the cursor on an invalid lnum 2127 autocmd BufReadPre <buffer> :norm! 70gg 2128 " triggers BufReadPre, should not move the cursor in either window 2129 e 2130 call assert_equal(1, winsaveview().topline) 2131 call assert_equal(1, winsaveview().lnum) 2132 call assert_equal(3, b:bufreadpre) 2133 wincmd p 2134 call assert_equal(g:wsv1.topline, winsaveview().topline) 2135 call assert_equal(g:wsv1.lnum, winsaveview().lnum) 2136 call assert_equal(3, b:bufreadpre) 2137 close 2138 close 2139 call delete('XAutocmdBufReadPre.txt') 2140 set cpo-=g 2141endfunc 2142 2143" FileChangedShell tested in test_filechanged.vim 2144 2145" Tests for the following autocommands: 2146" - FileWritePre writing a compressed file 2147" - FileReadPost reading a compressed file 2148" - BufNewFile reading a file template 2149" - BufReadPre decompressing the file to be read 2150" - FilterReadPre substituting characters in the temp file 2151" - FilterReadPost substituting characters after filtering 2152" - FileReadPre set options for decompression 2153" - FileReadPost decompress the file 2154func Test_ReadWrite_Autocmds() 2155 " Run this test only on Unix-like systems and if gzip is available 2156 CheckUnix 2157 CheckExecutable gzip 2158 2159 " Make $GZIP empty, "-v" would cause trouble. 2160 let $GZIP = "" 2161 2162 " Use a FileChangedShell autocommand to avoid a prompt for 'Xtestfile.gz' 2163 " being modified outside of Vim (noticed on Solaris). 2164 au FileChangedShell * echo 'caught FileChangedShell' 2165 2166 " Test for the FileReadPost, FileWritePre and FileWritePost autocmds 2167 augroup Test1 2168 au! 2169 au FileWritePre *.gz '[,']!gzip 2170 au FileWritePost *.gz undo 2171 au FileReadPost *.gz '[,']!gzip -d 2172 augroup END 2173 2174 new 2175 set bin 2176 call append(0, [ 2177 \ 'line 2 Abcdefghijklmnopqrstuvwxyz', 2178 \ 'line 3 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx', 2179 \ 'line 4 Abcdefghijklmnopqrstuvwxyz', 2180 \ 'line 5 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx', 2181 \ 'line 6 Abcdefghijklmnopqrstuvwxyz', 2182 \ 'line 7 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx', 2183 \ 'line 8 Abcdefghijklmnopqrstuvwxyz', 2184 \ 'line 9 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx', 2185 \ 'line 10 Abcdefghijklmnopqrstuvwxyz' 2186 \ ]) 2187 1,9write! Xtestfile.gz 2188 enew! | close 2189 2190 new 2191 " Read and decompress the testfile 2192 0read Xtestfile.gz 2193 call assert_equal([ 2194 \ 'line 2 Abcdefghijklmnopqrstuvwxyz', 2195 \ 'line 3 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx', 2196 \ 'line 4 Abcdefghijklmnopqrstuvwxyz', 2197 \ 'line 5 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx', 2198 \ 'line 6 Abcdefghijklmnopqrstuvwxyz', 2199 \ 'line 7 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx', 2200 \ 'line 8 Abcdefghijklmnopqrstuvwxyz', 2201 \ 'line 9 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx', 2202 \ 'line 10 Abcdefghijklmnopqrstuvwxyz' 2203 \ ], getline(1, 9)) 2204 enew! | close 2205 2206 augroup Test1 2207 au! 2208 augroup END 2209 2210 " Test for the FileAppendPre and FileAppendPost autocmds 2211 augroup Test2 2212 au! 2213 au BufNewFile *.c read Xtest.c 2214 au FileAppendPre *.out '[,']s/new/NEW/ 2215 au FileAppendPost *.out !cat Xtest.c >> test.out 2216 augroup END 2217 2218 call writefile(['/*', ' * Here is a new .c file', ' */'], 'Xtest.c') 2219 new foo.c " should load Xtest.c 2220 call assert_equal(['/*', ' * Here is a new .c file', ' */'], getline(2, 4)) 2221 w! >> test.out " append it to the output file 2222 2223 let contents = readfile('test.out') 2224 call assert_equal(' * Here is a NEW .c file', contents[2]) 2225 call assert_equal(' * Here is a new .c file', contents[5]) 2226 2227 call delete('test.out') 2228 enew! | close 2229 augroup Test2 2230 au! 2231 augroup END 2232 2233 " Test for the BufReadPre and BufReadPost autocmds 2234 augroup Test3 2235 au! 2236 " setup autocommands to decompress before reading and re-compress 2237 " afterwards 2238 au BufReadPre *.gz exe '!gzip -d ' . shellescape(expand("<afile>")) 2239 au BufReadPre *.gz call rename(expand("<afile>:r"), expand("<afile>")) 2240 au BufReadPost *.gz call rename(expand("<afile>"), expand("<afile>:r")) 2241 au BufReadPost *.gz exe '!gzip ' . shellescape(expand("<afile>:r")) 2242 augroup END 2243 2244 e! Xtestfile.gz " Edit compressed file 2245 call assert_equal([ 2246 \ 'line 2 Abcdefghijklmnopqrstuvwxyz', 2247 \ 'line 3 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx', 2248 \ 'line 4 Abcdefghijklmnopqrstuvwxyz', 2249 \ 'line 5 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx', 2250 \ 'line 6 Abcdefghijklmnopqrstuvwxyz', 2251 \ 'line 7 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx', 2252 \ 'line 8 Abcdefghijklmnopqrstuvwxyz', 2253 \ 'line 9 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx', 2254 \ 'line 10 Abcdefghijklmnopqrstuvwxyz' 2255 \ ], getline(1, 9)) 2256 2257 w! >> test.out " Append it to the output file 2258 2259 augroup Test3 2260 au! 2261 augroup END 2262 2263 " Test for the FilterReadPre and FilterReadPost autocmds. 2264 set shelltemp " need temp files here 2265 augroup Test4 2266 au! 2267 au FilterReadPre *.out call rename(expand("<afile>"), expand("<afile>") . ".t") 2268 au FilterReadPre *.out exe 'silent !sed s/e/E/ ' . shellescape(expand("<afile>")) . ".t >" . shellescape(expand("<afile>")) 2269 au FilterReadPre *.out exe 'silent !rm ' . shellescape(expand("<afile>")) . '.t' 2270 au FilterReadPost *.out '[,']s/x/X/g 2271 augroup END 2272 2273 e! test.out " Edit the output file 2274 1,$!cat 2275 call assert_equal([ 2276 \ 'linE 2 AbcdefghijklmnopqrstuvwXyz', 2277 \ 'linE 3 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX', 2278 \ 'linE 4 AbcdefghijklmnopqrstuvwXyz', 2279 \ 'linE 5 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX', 2280 \ 'linE 6 AbcdefghijklmnopqrstuvwXyz', 2281 \ 'linE 7 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX', 2282 \ 'linE 8 AbcdefghijklmnopqrstuvwXyz', 2283 \ 'linE 9 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX', 2284 \ 'linE 10 AbcdefghijklmnopqrstuvwXyz' 2285 \ ], getline(1, 9)) 2286 call assert_equal([ 2287 \ 'line 2 Abcdefghijklmnopqrstuvwxyz', 2288 \ 'line 3 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx', 2289 \ 'line 4 Abcdefghijklmnopqrstuvwxyz', 2290 \ 'line 5 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx', 2291 \ 'line 6 Abcdefghijklmnopqrstuvwxyz', 2292 \ 'line 7 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx', 2293 \ 'line 8 Abcdefghijklmnopqrstuvwxyz', 2294 \ 'line 9 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx', 2295 \ 'line 10 Abcdefghijklmnopqrstuvwxyz' 2296 \ ], readfile('test.out')) 2297 2298 augroup Test4 2299 au! 2300 augroup END 2301 set shelltemp&vim 2302 2303 " Test for the FileReadPre and FileReadPost autocmds. 2304 augroup Test5 2305 au! 2306 au FileReadPre *.gz exe 'silent !gzip -d ' . shellescape(expand("<afile>")) 2307 au FileReadPre *.gz call rename(expand("<afile>:r"), expand("<afile>")) 2308 au FileReadPost *.gz '[,']s/l/L/ 2309 augroup END 2310 2311 new 2312 0r Xtestfile.gz " Read compressed file 2313 call assert_equal([ 2314 \ 'Line 2 Abcdefghijklmnopqrstuvwxyz', 2315 \ 'Line 3 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx', 2316 \ 'Line 4 Abcdefghijklmnopqrstuvwxyz', 2317 \ 'Line 5 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx', 2318 \ 'Line 6 Abcdefghijklmnopqrstuvwxyz', 2319 \ 'Line 7 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx', 2320 \ 'Line 8 Abcdefghijklmnopqrstuvwxyz', 2321 \ 'Line 9 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx', 2322 \ 'Line 10 Abcdefghijklmnopqrstuvwxyz' 2323 \ ], getline(1, 9)) 2324 call assert_equal([ 2325 \ 'line 2 Abcdefghijklmnopqrstuvwxyz', 2326 \ 'line 3 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx', 2327 \ 'line 4 Abcdefghijklmnopqrstuvwxyz', 2328 \ 'line 5 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx', 2329 \ 'line 6 Abcdefghijklmnopqrstuvwxyz', 2330 \ 'line 7 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx', 2331 \ 'line 8 Abcdefghijklmnopqrstuvwxyz', 2332 \ 'line 9 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx', 2333 \ 'line 10 Abcdefghijklmnopqrstuvwxyz' 2334 \ ], readfile('Xtestfile.gz')) 2335 2336 augroup Test5 2337 au! 2338 augroup END 2339 2340 au! FileChangedShell 2341 call delete('Xtestfile.gz') 2342 call delete('Xtest.c') 2343 call delete('test.out') 2344endfunc 2345 2346func Test_throw_in_BufWritePre() 2347 new 2348 call setline(1, ['one', 'two', 'three']) 2349 call assert_false(filereadable('Xthefile')) 2350 augroup throwing 2351 au BufWritePre X* throw 'do not write' 2352 augroup END 2353 try 2354 w Xthefile 2355 catch 2356 let caught = 1 2357 endtry 2358 call assert_equal(1, caught) 2359 call assert_false(filereadable('Xthefile')) 2360 2361 bwipe! 2362 au! throwing 2363endfunc 2364 2365func Test_autocmd_in_try_block() 2366 call mkdir('Xdir') 2367 au BufEnter * let g:fname = expand('%') 2368 try 2369 edit Xdir/ 2370 endtry 2371 call assert_match('Xdir', g:fname) 2372 2373 unlet g:fname 2374 au! BufEnter 2375 call delete('Xdir', 'rf') 2376endfunc 2377 2378func Test_autocmd_SafeState() 2379 CheckRunVimInTerminal 2380 2381 let lines =<< trim END 2382 let g:safe = 0 2383 let g:again = '' 2384 au SafeState * let g:safe += 1 2385 au SafeStateAgain * let g:again ..= 'x' 2386 func CallTimer() 2387 call timer_start(10, {id -> execute('let g:again ..= "t"')}) 2388 endfunc 2389 END 2390 call writefile(lines, 'XSafeState') 2391 let buf = RunVimInTerminal('-S XSafeState', #{rows: 6}) 2392 2393 " Sometimes we loop to handle a K_IGNORE, SafeState may be triggered once or 2394 " more often. 2395 call term_sendkeys(buf, ":echo g:safe\<CR>") 2396 call WaitForAssert({-> assert_match('^\d ', term_getline(buf, 6))}, 1000) 2397 2398 " SafeStateAgain should be invoked at least three times 2399 call term_sendkeys(buf, ":echo g:again\<CR>") 2400 call WaitForAssert({-> assert_match('^xxx', term_getline(buf, 6))}, 1000) 2401 2402 call term_sendkeys(buf, ":let g:again = ''\<CR>:call CallTimer()\<CR>") 2403 call TermWait(buf, 50) 2404 call term_sendkeys(buf, ":\<CR>") 2405 call TermWait(buf, 50) 2406 call term_sendkeys(buf, ":echo g:again\<CR>") 2407 call WaitForAssert({-> assert_match('xtx', term_getline(buf, 6))}, 1000) 2408 2409 call StopVimInTerminal(buf) 2410 call delete('XSafeState') 2411endfunc 2412 2413func Test_autocmd_CmdWinEnter() 2414 CheckRunVimInTerminal 2415 CheckFeature cmdwin 2416 2417 let lines =<< trim END 2418 let b:dummy_var = 'This is a dummy' 2419 autocmd CmdWinEnter * quit 2420 let winnr = winnr('$') 2421 END 2422 let filename = 'XCmdWinEnter' 2423 call writefile(lines, filename) 2424 let buf = RunVimInTerminal('-S '.filename, #{rows: 6}) 2425 2426 call term_sendkeys(buf, "q:") 2427 call TermWait(buf) 2428 call term_sendkeys(buf, ":echo b:dummy_var\<cr>") 2429 call WaitForAssert({-> assert_match('^This is a dummy', term_getline(buf, 6))}, 2000) 2430 call term_sendkeys(buf, ":echo &buftype\<cr>") 2431 call WaitForAssert({-> assert_notmatch('^nofile', term_getline(buf, 6))}, 1000) 2432 call term_sendkeys(buf, ":echo winnr\<cr>") 2433 call WaitForAssert({-> assert_match('^1', term_getline(buf, 6))}, 1000) 2434 2435 " clean up 2436 call StopVimInTerminal(buf) 2437 call delete(filename) 2438endfunc 2439 2440func Test_autocmd_was_using_freed_memory() 2441 CheckFeature quickfix 2442 2443 pedit xx 2444 n x 2445 augroup winenter 2446 au WinEnter * if winnr('$') > 2 | quit | endif 2447 augroup END 2448 split 2449 2450 augroup winenter 2451 au! WinEnter 2452 augroup END 2453 2454 bwipe xx 2455 bwipe x 2456 pclose 2457endfunc 2458 2459func Test_BufWrite_lockmarks() 2460 edit! Xtest 2461 call setline(1, ['a', 'b', 'c', 'd']) 2462 2463 " :lockmarks preserves the marks 2464 call SetChangeMarks(2, 3) 2465 lockmarks write 2466 call assert_equal([2, 3], [line("'["), line("']")]) 2467 2468 " *WritePre autocmds get the correct line range, but lockmarks preserves the 2469 " original values for the user 2470 augroup lockmarks 2471 au! 2472 au BufWritePre,FilterWritePre * call assert_equal([1, 4], [line("'["), line("']")]) 2473 au FileWritePre * call assert_equal([3, 4], [line("'["), line("']")]) 2474 augroup END 2475 2476 lockmarks write 2477 call assert_equal([2, 3], [line("'["), line("']")]) 2478 2479 if executable('cat') 2480 lockmarks %!cat 2481 call assert_equal([2, 3], [line("'["), line("']")]) 2482 endif 2483 2484 lockmarks 3,4write Xtest2 2485 call assert_equal([2, 3], [line("'["), line("']")]) 2486 2487 au! lockmarks 2488 augroup! lockmarks 2489 call delete('Xtest') 2490 call delete('Xtest2') 2491endfunc 2492 2493func Test_FileType_spell() 2494 if !isdirectory('/tmp') 2495 throw "Skipped: requires /tmp directory" 2496 endif 2497 2498 " this was crashing with an invalid free() 2499 setglobal spellfile=/tmp/en.utf-8.add 2500 augroup crash 2501 autocmd! 2502 autocmd BufNewFile,BufReadPost crashfile setf somefiletype 2503 autocmd BufNewFile,BufReadPost crashfile set ft=anotherfiletype 2504 autocmd FileType anotherfiletype setlocal spell 2505 augroup END 2506 func! NoCrash() abort 2507 edit /tmp/crashfile 2508 endfunc 2509 call NoCrash() 2510 2511 au! crash 2512 setglobal spellfile= 2513endfunc 2514 2515" Test closing a window or editing another buffer from a FileChangedRO handler 2516" in a readonly buffer 2517func Test_FileChangedRO_winclose() 2518 call test_override('ui_delay', 10) 2519 2520 augroup FileChangedROTest 2521 au! 2522 autocmd FileChangedRO * quit 2523 augroup END 2524 new 2525 set readonly 2526 call assert_fails('normal i', 'E788:') 2527 close 2528 augroup! FileChangedROTest 2529 2530 augroup FileChangedROTest 2531 au! 2532 autocmd FileChangedRO * edit Xfile 2533 augroup END 2534 new 2535 set readonly 2536 call assert_fails('normal i', 'E788:') 2537 close 2538 augroup! FileChangedROTest 2539 call test_override('ALL', 0) 2540endfunc 2541 2542func LogACmd() 2543 call add(g:logged, line('$')) 2544endfunc 2545 2546func Test_TermChanged() 2547 CheckNotGui 2548 2549 enew! 2550 tabnew 2551 call setline(1, ['a', 'b', 'c', 'd']) 2552 $ 2553 au TermChanged * call LogACmd() 2554 let g:logged = [] 2555 let term_save = &term 2556 set term=xterm 2557 call assert_equal([1, 4], g:logged) 2558 2559 au! TermChanged 2560 let &term = term_save 2561 bwipe! 2562endfunc 2563 2564" Test for FileReadCmd autocmd 2565func Test_autocmd_FileReadCmd() 2566 func ReadFileCmd() 2567 call append(line('$'), "v:cmdarg = " .. v:cmdarg) 2568 endfunc 2569 augroup FileReadCmdTest 2570 au! 2571 au FileReadCmd Xtest call ReadFileCmd() 2572 augroup END 2573 2574 new 2575 read ++bin Xtest 2576 read ++nobin Xtest 2577 read ++edit Xtest 2578 read ++bad=keep Xtest 2579 read ++bad=drop Xtest 2580 read ++bad=- Xtest 2581 read ++ff=unix Xtest 2582 read ++ff=dos Xtest 2583 read ++ff=mac Xtest 2584 read ++enc=utf-8 Xtest 2585 2586 call assert_equal(['', 2587 \ 'v:cmdarg = ++bin', 2588 \ 'v:cmdarg = ++nobin', 2589 \ 'v:cmdarg = ++edit', 2590 \ 'v:cmdarg = ++bad=keep', 2591 \ 'v:cmdarg = ++bad=drop', 2592 \ 'v:cmdarg = ++bad=-', 2593 \ 'v:cmdarg = ++ff=unix', 2594 \ 'v:cmdarg = ++ff=dos', 2595 \ 'v:cmdarg = ++ff=mac', 2596 \ 'v:cmdarg = ++enc=utf-8'], getline(1, '$')) 2597 2598 close! 2599 augroup FileReadCmdTest 2600 au! 2601 augroup END 2602 delfunc ReadFileCmd 2603endfunc 2604 2605" Test for passing invalid arguments to autocmd 2606func Test_autocmd_invalid_args() 2607 " Additional character after * for event 2608 call assert_fails('autocmd *a Xfile set ff=unix', 'E215:') 2609 augroup Test 2610 augroup END 2611 " Invalid autocmd event 2612 call assert_fails('autocmd Bufabc Xfile set ft=vim', 'E216:') 2613 " Invalid autocmd event in a autocmd group 2614 call assert_fails('autocmd Test Bufabc Xfile set ft=vim', 'E216:') 2615 augroup! Test 2616 " Execute all autocmds 2617 call assert_fails('doautocmd * BufEnter', 'E217:') 2618 call assert_fails('augroup! x1a2b3', 'E367:') 2619 call assert_fails('autocmd BufNew <buffer=999> pwd', 'E680:') 2620 call assert_fails('autocmd BufNew \) set ff=unix', 'E55:') 2621endfunc 2622 2623" Test for deep nesting of autocmds 2624func Test_autocmd_deep_nesting() 2625 autocmd BufEnter Xfile doautocmd BufEnter Xfile 2626 call assert_fails('doautocmd BufEnter Xfile', 'E218:') 2627 autocmd! BufEnter Xfile 2628endfunc 2629 2630" Tests for SigUSR1 autocmd event, which is only available on posix systems. 2631func Test_autocmd_sigusr1() 2632 CheckUnix 2633 CheckExecutable /bin/kill 2634 2635 let g:sigusr1_passed = 0 2636 au SigUSR1 * let g:sigusr1_passed = 1 2637 call system('/bin/kill -s usr1 ' . getpid()) 2638 call WaitForAssert({-> assert_true(g:sigusr1_passed)}) 2639 2640 au! SigUSR1 2641 unlet g:sigusr1_passed 2642endfunc 2643 2644" Test for BufReadPre autocmd deleting the file 2645func Test_BufReadPre_delfile() 2646 augroup TestAuCmd 2647 au! 2648 autocmd BufReadPre Xfile call delete('Xfile') 2649 augroup END 2650 call writefile([], 'Xfile') 2651 call assert_fails('new Xfile', 'E200:') 2652 call assert_equal('Xfile', @%) 2653 call assert_equal(1, &readonly) 2654 call delete('Xfile') 2655 augroup TestAuCmd 2656 au! 2657 augroup END 2658 close! 2659endfunc 2660 2661" Test for BufReadPre autocmd changing the current buffer 2662func Test_BufReadPre_changebuf() 2663 augroup TestAuCmd 2664 au! 2665 autocmd BufReadPre Xfile edit Xsomeotherfile 2666 augroup END 2667 call writefile([], 'Xfile') 2668 call assert_fails('new Xfile', 'E201:') 2669 call assert_equal('Xsomeotherfile', @%) 2670 call assert_equal(1, &readonly) 2671 call delete('Xfile') 2672 augroup TestAuCmd 2673 au! 2674 augroup END 2675 close! 2676endfunc 2677 2678" Test for BufWipeouti autocmd changing the current buffer when reading a file 2679" in an empty buffer with 'f' flag in 'cpo' 2680func Test_BufDelete_changebuf() 2681 new 2682 augroup TestAuCmd 2683 au! 2684 autocmd BufWipeout * let bufnr = bufadd('somefile') | exe "b " .. bufnr 2685 augroup END 2686 let save_cpo = &cpo 2687 set cpo+=f 2688 call assert_fails('r Xfile', ['E812:', 'E484:']) 2689 call assert_equal('somefile', @%) 2690 let &cpo = save_cpo 2691 augroup TestAuCmd 2692 au! 2693 augroup END 2694 close! 2695endfunc 2696 2697" Test for the temporary internal window used to execute autocmds 2698func Test_autocmd_window() 2699 %bw! 2700 edit one.txt 2701 tabnew two.txt 2702 vnew three.txt 2703 tabnew four.txt 2704 tabprevious 2705 let g:blist = [] 2706 augroup aucmd_win_test1 2707 au! 2708 au BufEnter * call add(g:blist, [expand('<afile>'), 2709 \ win_gettype(bufwinnr(expand('<afile>')))]) 2710 augroup END 2711 2712 doautoall BufEnter 2713 call assert_equal([ 2714 \ ['one.txt', 'autocmd'], 2715 \ ['two.txt', ''], 2716 \ ['four.txt', 'autocmd'], 2717 \ ['three.txt', ''], 2718 \ ], g:blist) 2719 2720 augroup aucmd_win_test1 2721 au! 2722 augroup END 2723 augroup! aucmd_win_test1 2724 %bw! 2725endfunc 2726 2727" Test for trying to close the temporary window used for executing an autocmd 2728func Test_close_autocmd_window() 2729 %bw! 2730 edit one.txt 2731 tabnew two.txt 2732 augroup aucmd_win_test2 2733 au! 2734 au BufEnter * if expand('<afile>') == 'one.txt' | 1close | endif 2735 augroup END 2736 2737 call assert_fails('doautoall BufEnter', 'E813:') 2738 2739 augroup aucmd_win_test2 2740 au! 2741 augroup END 2742 augroup! aucmd_win_test2 2743 %bwipe! 2744endfunc 2745 2746" Test for trying to close the tab that has the temporary window for exeucing 2747" an autocmd. 2748func Test_close_autocmd_tab() 2749 edit one.txt 2750 tabnew two.txt 2751 augroup aucmd_win_test 2752 au! 2753 au BufEnter * if expand('<afile>') == 'one.txt' | tabfirst | tabonly | endif 2754 augroup END 2755 2756 call assert_fails('doautoall BufEnter', 'E813:') 2757 2758 tabonly 2759 augroup aucmd_win_test 2760 au! 2761 augroup END 2762 augroup! aucmd_win_test 2763 %bwipe! 2764endfunc 2765 2766" This was using freed memory. 2767func Test_BufNew_arglocal() 2768 arglocal 2769 au BufNew * arglocal 2770 call assert_fails('drop xx', 'E1156:') 2771 2772 au! BufNew 2773endfunc 2774 2775func Test_autocmd_closes_window() 2776 au BufNew,BufWinLeave * e %e 2777 file yyy 2778 au BufNew,BufWinLeave * ball 2779 n xxx 2780 2781 %bwipe 2782 au! BufNew 2783 au! BufWinLeave 2784endfunc 2785 2786func Test_autocmd_quit_psearch() 2787 sn aa bb 2788 augroup aucmd_win_test 2789 au! 2790 au BufEnter,BufLeave,BufNew,WinEnter,WinLeave,WinNew * if winnr('$') > 1 | q | endif 2791 augroup END 2792 ps / 2793 2794 augroup aucmd_win_test 2795 au! 2796 augroup END 2797endfunc 2798 2799" Fuzzer found some strange combination that caused a crash. 2800func Test_autocmd_normal_mess() 2801 " For unknown reason this hangs on MS-Windows 2802 CheckNotMSWindows 2803 2804 augroup aucmd_normal_test 2805 au BufLeave,BufWinLeave,BufHidden,BufUnload,BufDelete,BufWipeout * norm 7q/qc 2806 augroup END 2807 call assert_fails('o4', 'E1159') 2808 silent! H 2809 call assert_fails('e xx', 'E1159') 2810 normal G 2811 2812 augroup aucmd_normal_test 2813 au! 2814 augroup END 2815endfunc 2816 2817func Test_autocmd_closing_cmdwin() 2818 " For unknown reason this hangs on MS-Windows 2819 CheckNotMSWindows 2820 2821 au BufWinLeave * nested q 2822 call assert_fails("norm 7q?\n", 'E855:') 2823 2824 au! BufWinLeave 2825 new 2826 only 2827endfunc 2828 2829func Test_autocmd_vimgrep() 2830 augroup aucmd_vimgrep 2831 au QuickfixCmdPre,BufNew,BufDelete,BufReadCmd * sb 2832 au QuickfixCmdPre,BufNew,BufDelete,BufReadCmd * q9 2833 augroup END 2834 %bwipe! 2835 call assert_fails('lv ?a? foo', 'E926:') 2836 2837 augroup aucmd_vimgrep 2838 au! 2839 augroup END 2840endfunc 2841 2842func Test_autocmd_with_block() 2843 augroup block_testing 2844 au BufReadPost *.xml { 2845 setlocal matchpairs+=<:> 2846 /<start 2847 } 2848 au CursorHold * { 2849 autocmd BufReadPre * ++once echo 'one' | echo 'two' 2850 g:gotSafeState = 77 2851 } 2852 augroup END 2853 2854 let expected = "\n--- Autocommands ---\nblock_testing BufRead\n *.xml {^@ setlocal matchpairs+=<:>^@ /<start^@ }" 2855 call assert_equal(expected, execute('au BufReadPost *.xml')) 2856 2857 doautocmd CursorHold 2858 call assert_equal(77, g:gotSafeState) 2859 unlet g:gotSafeState 2860 2861 augroup block_testing 2862 au! 2863 augroup END 2864endfunc 2865 2866 2867" vim: shiftwidth=2 sts=2 expandtab 2868