2016-11-14 08:19:06 -07:00
|
|
|
" Tests for autocommands
|
|
|
|
|
2017-03-21 02:15:22 -07:00
|
|
|
set belloff=all
|
|
|
|
|
|
|
|
function! s:cleanup_buffers() abort
|
|
|
|
for bnr in range(1, bufnr('$'))
|
|
|
|
if bufloaded(bnr) && bufnr('%') != bnr
|
|
|
|
execute 'bd! ' . bnr
|
|
|
|
endif
|
|
|
|
endfor
|
|
|
|
endfunction
|
|
|
|
|
2016-11-14 08:19:06 -07:00
|
|
|
func Test_vim_did_enter()
|
|
|
|
call assert_false(v:vim_did_enter)
|
|
|
|
|
|
|
|
" This script will never reach the main loop, can't check if v:vim_did_enter
|
|
|
|
" becomes one.
|
|
|
|
endfunc
|
2016-11-21 14:52:51 -07:00
|
|
|
|
2016-12-27 15:39:21 -07:00
|
|
|
if has('timers')
|
|
|
|
func ExitInsertMode(id)
|
|
|
|
call feedkeys("\<Esc>")
|
|
|
|
endfunc
|
|
|
|
|
|
|
|
func Test_cursorhold_insert()
|
2017-03-21 02:19:01 -07:00
|
|
|
" Need to move the cursor.
|
|
|
|
call feedkeys("ggG", "xt")
|
|
|
|
|
2016-12-27 15:39:21 -07:00
|
|
|
let g:triggered = 0
|
|
|
|
au CursorHoldI * let g:triggered += 1
|
|
|
|
set updatetime=20
|
|
|
|
call timer_start(100, 'ExitInsertMode')
|
|
|
|
call feedkeys('a', 'x!')
|
|
|
|
call assert_equal(1, g:triggered)
|
2017-01-04 21:06:09 -07:00
|
|
|
au! CursorHoldI
|
2017-03-21 02:24:40 -07:00
|
|
|
set updatetime&
|
2016-12-27 15:39:21 -07:00
|
|
|
endfunc
|
|
|
|
|
|
|
|
func Test_cursorhold_insert_ctrl_x()
|
|
|
|
let g:triggered = 0
|
|
|
|
au CursorHoldI * let g:triggered += 1
|
|
|
|
set updatetime=20
|
|
|
|
call timer_start(100, 'ExitInsertMode')
|
|
|
|
" CursorHoldI does not trigger after CTRL-X
|
|
|
|
call feedkeys("a\<C-X>", 'x!')
|
|
|
|
call assert_equal(0, g:triggered)
|
2017-01-04 21:06:09 -07:00
|
|
|
au! CursorHoldI
|
2017-03-21 02:24:40 -07:00
|
|
|
set updatetime&
|
2016-12-27 15:39:21 -07:00
|
|
|
endfunc
|
2016-11-21 14:52:51 -07:00
|
|
|
endif
|
|
|
|
|
2016-12-27 15:39:21 -07:00
|
|
|
function Test_bufunload()
|
|
|
|
augroup test_bufunload_group
|
|
|
|
autocmd!
|
|
|
|
autocmd BufUnload * call add(s:li, "bufunload")
|
|
|
|
autocmd BufDelete * call add(s:li, "bufdelete")
|
|
|
|
autocmd BufWipeout * call add(s:li, "bufwipeout")
|
|
|
|
augroup END
|
2016-11-21 14:52:51 -07:00
|
|
|
|
2016-12-27 15:39:21 -07:00
|
|
|
let s:li=[]
|
|
|
|
new
|
|
|
|
setlocal bufhidden=
|
|
|
|
bunload
|
|
|
|
call assert_equal(["bufunload", "bufdelete"], s:li)
|
|
|
|
|
|
|
|
let s:li=[]
|
|
|
|
new
|
|
|
|
setlocal bufhidden=delete
|
|
|
|
bunload
|
|
|
|
call assert_equal(["bufunload", "bufdelete"], s:li)
|
|
|
|
|
|
|
|
let s:li=[]
|
|
|
|
new
|
|
|
|
setlocal bufhidden=unload
|
|
|
|
bwipeout
|
|
|
|
call assert_equal(["bufunload", "bufdelete", "bufwipeout"], s:li)
|
2016-11-21 14:52:51 -07:00
|
|
|
|
2017-01-04 21:06:09 -07:00
|
|
|
au! test_bufunload_group
|
2016-12-27 15:39:21 -07:00
|
|
|
augroup! test_bufunload_group
|
2016-11-21 14:52:51 -07:00
|
|
|
endfunc
|
2017-01-04 21:06:09 -07:00
|
|
|
|
2017-01-07 07:56:51 -07:00
|
|
|
" SEGV occurs in older versions. (At least 7.4.2005 or older)
|
|
|
|
function Test_autocmd_bufunload_with_tabnext()
|
|
|
|
tabedit
|
|
|
|
tabfirst
|
|
|
|
|
|
|
|
augroup test_autocmd_bufunload_with_tabnext_group
|
|
|
|
autocmd!
|
|
|
|
autocmd BufUnload <buffer> tabnext
|
|
|
|
augroup END
|
|
|
|
|
|
|
|
quit
|
|
|
|
call assert_equal(2, tabpagenr('$'))
|
|
|
|
|
2017-03-20 06:01:22 -07:00
|
|
|
autocmd! test_autocmd_bufunload_with_tabnext_group
|
2017-01-07 07:56:51 -07:00
|
|
|
augroup! test_autocmd_bufunload_with_tabnext_group
|
|
|
|
tablast
|
|
|
|
quit
|
|
|
|
endfunc
|
|
|
|
|
2017-03-21 02:39:24 -07:00
|
|
|
function Test_autocmd_bufwinleave_with_tabfirst()
|
|
|
|
tabedit
|
|
|
|
augroup sample
|
|
|
|
autocmd!
|
|
|
|
autocmd BufWinLeave <buffer> tabfirst
|
|
|
|
augroup END
|
|
|
|
call setline(1, ['a', 'b', 'c'])
|
|
|
|
edit! a.txt
|
|
|
|
tabclose
|
|
|
|
endfunc
|
|
|
|
|
2017-03-20 06:01:22 -07:00
|
|
|
" SEGV occurs in older versions. (At least 7.4.2321 or older)
|
|
|
|
function Test_autocmd_bufunload_avoiding_SEGV_01()
|
|
|
|
split aa.txt
|
|
|
|
let lastbuf = bufnr('$')
|
|
|
|
|
|
|
|
augroup test_autocmd_bufunload
|
|
|
|
autocmd!
|
|
|
|
exe 'autocmd BufUnload <buffer> ' . (lastbuf + 1) . 'bwipeout!'
|
|
|
|
augroup END
|
|
|
|
|
|
|
|
call assert_fails('edit bb.txt', 'E937:')
|
|
|
|
|
|
|
|
autocmd! test_autocmd_bufunload
|
|
|
|
augroup! test_autocmd_bufunload
|
|
|
|
bwipe! aa.txt
|
|
|
|
bwipe! bb.txt
|
|
|
|
endfunc
|
|
|
|
|
|
|
|
" SEGV occurs in older versions. (At least 7.4.2321 or older)
|
|
|
|
function Test_autocmd_bufunload_avoiding_SEGV_02()
|
|
|
|
setlocal buftype=nowrite
|
|
|
|
let lastbuf = bufnr('$')
|
|
|
|
|
|
|
|
augroup test_autocmd_bufunload
|
|
|
|
autocmd!
|
|
|
|
exe 'autocmd BufUnload <buffer> ' . (lastbuf + 1) . 'bwipeout!'
|
|
|
|
augroup END
|
|
|
|
|
|
|
|
normal! i1
|
|
|
|
call assert_fails('edit a.txt', 'E517:')
|
|
|
|
call feedkeys("\<CR>")
|
|
|
|
|
|
|
|
autocmd! test_autocmd_bufunload
|
|
|
|
augroup! test_autocmd_bufunload
|
|
|
|
bwipe! a.txt
|
|
|
|
endfunc
|
|
|
|
|
2017-01-08 06:05:41 -07:00
|
|
|
func Test_win_tab_autocmd()
|
|
|
|
let g:record = []
|
|
|
|
|
|
|
|
augroup testing
|
|
|
|
au WinNew * call add(g:record, 'WinNew')
|
|
|
|
au WinEnter * call add(g:record, 'WinEnter')
|
|
|
|
au WinLeave * call add(g:record, 'WinLeave')
|
|
|
|
au TabNew * call add(g:record, 'TabNew')
|
2017-01-08 08:12:52 -07:00
|
|
|
au TabClosed * call add(g:record, 'TabClosed')
|
2017-01-08 06:05:41 -07:00
|
|
|
au TabEnter * call add(g:record, 'TabEnter')
|
|
|
|
au TabLeave * call add(g:record, 'TabLeave')
|
|
|
|
augroup END
|
|
|
|
|
|
|
|
split
|
|
|
|
tabnew
|
|
|
|
close
|
|
|
|
close
|
|
|
|
|
|
|
|
call assert_equal([
|
|
|
|
\ 'WinLeave', 'WinNew', 'WinEnter',
|
|
|
|
\ 'WinLeave', 'TabLeave', 'WinNew', 'WinEnter', 'TabNew', 'TabEnter',
|
2017-01-08 08:12:52 -07:00
|
|
|
\ 'WinLeave', 'TabLeave', 'TabClosed', 'WinEnter', 'TabEnter',
|
2017-01-08 06:05:41 -07:00
|
|
|
\ 'WinLeave', 'WinEnter'
|
|
|
|
\ ], g:record)
|
|
|
|
|
2017-01-08 08:12:52 -07:00
|
|
|
let g:record = []
|
|
|
|
tabnew somefile
|
|
|
|
tabnext
|
|
|
|
bwipe somefile
|
|
|
|
|
|
|
|
call assert_equal([
|
|
|
|
\ 'WinLeave', 'TabLeave', 'WinNew', 'WinEnter', 'TabNew', 'TabEnter',
|
|
|
|
\ 'WinLeave', 'TabLeave', 'WinEnter', 'TabEnter',
|
|
|
|
\ 'TabClosed'
|
|
|
|
\ ], g:record)
|
|
|
|
|
2017-01-08 06:05:41 -07:00
|
|
|
augroup testing
|
|
|
|
au!
|
|
|
|
augroup END
|
|
|
|
unlet g:record
|
|
|
|
endfunc
|
|
|
|
|
2017-01-04 21:06:09 -07:00
|
|
|
func s:AddAnAutocmd()
|
|
|
|
augroup vimBarTest
|
|
|
|
au BufReadCmd * echo 'hello'
|
|
|
|
augroup END
|
|
|
|
call assert_equal(3, len(split(execute('au vimBarTest'), "\n")))
|
|
|
|
endfunc
|
|
|
|
|
|
|
|
func Test_early_bar()
|
|
|
|
" test that a bar is recognized before the {event}
|
|
|
|
call s:AddAnAutocmd()
|
|
|
|
augroup vimBarTest | au! | augroup END
|
|
|
|
call assert_equal(1, len(split(execute('au vimBarTest'), "\n")))
|
|
|
|
|
|
|
|
call s:AddAnAutocmd()
|
|
|
|
augroup vimBarTest| au!| augroup END
|
|
|
|
call assert_equal(1, len(split(execute('au vimBarTest'), "\n")))
|
|
|
|
|
|
|
|
" test that a bar is recognized after the {event}
|
|
|
|
call s:AddAnAutocmd()
|
|
|
|
augroup vimBarTest| au!BufReadCmd| augroup END
|
|
|
|
call assert_equal(1, len(split(execute('au vimBarTest'), "\n")))
|
|
|
|
|
|
|
|
" test that a bar is recognized after the {group}
|
|
|
|
call s:AddAnAutocmd()
|
|
|
|
au! vimBarTest|echo 'hello'
|
|
|
|
call assert_equal(1, len(split(execute('au vimBarTest'), "\n")))
|
|
|
|
endfunc
|
2017-01-08 17:41:45 -07:00
|
|
|
|
2017-01-08 20:00:56 -07:00
|
|
|
func RemoveGroup()
|
|
|
|
autocmd! StartOK
|
|
|
|
augroup! StartOK
|
|
|
|
endfunc
|
|
|
|
|
2017-01-08 17:41:45 -07:00
|
|
|
func Test_augroup_warning()
|
|
|
|
augroup TheWarning
|
|
|
|
au VimEnter * echo 'entering'
|
|
|
|
augroup END
|
|
|
|
call assert_true(match(execute('au VimEnter'), "TheWarning.*VimEnter") >= 0)
|
|
|
|
redir => res
|
|
|
|
augroup! TheWarning
|
|
|
|
redir END
|
|
|
|
call assert_true(match(res, "W19:") >= 0)
|
|
|
|
call assert_true(match(execute('au VimEnter'), "-Deleted-.*VimEnter") >= 0)
|
|
|
|
|
|
|
|
" check "Another" does not take the pace of the deleted entry
|
|
|
|
augroup Another
|
|
|
|
augroup END
|
|
|
|
call assert_true(match(execute('au VimEnter'), "-Deleted-.*VimEnter") >= 0)
|
2017-03-21 02:24:40 -07:00
|
|
|
augroup! Another
|
2017-01-08 20:00:56 -07:00
|
|
|
|
|
|
|
" no warning for postpone aucmd delete
|
|
|
|
augroup StartOK
|
|
|
|
au VimEnter * call RemoveGroup()
|
|
|
|
augroup END
|
|
|
|
call assert_true(match(execute('au VimEnter'), "StartOK.*VimEnter") >= 0)
|
|
|
|
redir => res
|
|
|
|
doautocmd VimEnter
|
|
|
|
redir END
|
|
|
|
call assert_true(match(res, "W19:") < 0)
|
2017-01-08 21:15:10 -07:00
|
|
|
au! VimEnter
|
2017-01-08 17:41:45 -07:00
|
|
|
endfunc
|
2017-01-08 21:01:50 -07:00
|
|
|
|
|
|
|
func Test_augroup_deleted()
|
2017-01-08 21:15:10 -07:00
|
|
|
" This caused a crash before E936 was introduced
|
2017-01-08 21:01:50 -07:00
|
|
|
augroup x
|
2017-01-08 21:15:10 -07:00
|
|
|
call assert_fails('augroup! x', 'E936:')
|
|
|
|
au VimEnter * echo
|
|
|
|
augroup end
|
2017-01-08 21:01:50 -07:00
|
|
|
augroup! x
|
2017-01-08 21:15:10 -07:00
|
|
|
call assert_true(match(execute('au VimEnter'), "-Deleted-.*VimEnter") >= 0)
|
|
|
|
au! VimEnter
|
2017-01-08 21:01:50 -07:00
|
|
|
endfunc
|
|
|
|
|
2017-03-20 06:01:22 -07:00
|
|
|
" Tests for autocommands on :close command.
|
|
|
|
" This used to be in test13.
|
|
|
|
func Test_three_windows()
|
2017-03-21 02:15:22 -07:00
|
|
|
" Clean up buffers, because in some cases this function fails.
|
|
|
|
call s:cleanup_buffers()
|
|
|
|
|
2017-03-20 06:01:22 -07:00
|
|
|
" Write three files and open them, each in a window.
|
|
|
|
" Then go to next window, with autocommand that deletes the previous one.
|
|
|
|
" Do this twice, writing the file.
|
|
|
|
e! Xtestje1
|
|
|
|
call setline(1, 'testje1')
|
|
|
|
w
|
|
|
|
sp Xtestje2
|
|
|
|
call setline(1, 'testje2')
|
|
|
|
w
|
|
|
|
sp Xtestje3
|
|
|
|
call setline(1, 'testje3')
|
|
|
|
w
|
|
|
|
wincmd w
|
|
|
|
au WinLeave Xtestje2 bwipe
|
|
|
|
wincmd w
|
|
|
|
call assert_equal('Xtestje1', expand('%'))
|
|
|
|
|
|
|
|
au WinLeave Xtestje1 bwipe Xtestje3
|
|
|
|
close
|
|
|
|
call assert_equal('Xtestje1', expand('%'))
|
|
|
|
|
|
|
|
" Test deleting the buffer on a Unload event. If this goes wrong there
|
|
|
|
" will be the ATTENTION prompt.
|
|
|
|
e Xtestje1
|
|
|
|
au!
|
|
|
|
au! BufUnload Xtestje1 bwipe
|
|
|
|
call assert_fails('e Xtestje3', 'E937:')
|
|
|
|
call assert_equal('Xtestje3', expand('%'))
|
|
|
|
|
|
|
|
e Xtestje2
|
|
|
|
sp Xtestje1
|
|
|
|
call assert_fails('e', 'E937:')
|
|
|
|
call assert_equal('Xtestje2', expand('%'))
|
|
|
|
|
|
|
|
" Test changing buffers in a BufWipeout autocommand. If this goes wrong
|
|
|
|
" there are ml_line errors and/or a Crash.
|
|
|
|
au!
|
|
|
|
only
|
|
|
|
e Xanother
|
|
|
|
e Xtestje1
|
|
|
|
bwipe Xtestje2
|
|
|
|
bwipe Xtestje3
|
|
|
|
au BufWipeout Xtestje1 buf Xtestje1
|
|
|
|
bwipe
|
|
|
|
call assert_equal('Xanother', expand('%'))
|
|
|
|
|
|
|
|
only
|
2017-03-21 17:19:13 -07:00
|
|
|
|
|
|
|
helptags ALL
|
2017-03-20 06:01:22 -07:00
|
|
|
help
|
|
|
|
wincmd w
|
|
|
|
1quit
|
|
|
|
call assert_equal('Xanother', expand('%'))
|
|
|
|
|
|
|
|
au!
|
2017-03-21 15:36:27 -07:00
|
|
|
enew
|
|
|
|
bwipe! Xtestje1
|
2017-03-20 06:01:22 -07:00
|
|
|
call delete('Xtestje1')
|
|
|
|
call delete('Xtestje2')
|
|
|
|
call delete('Xtestje3')
|
|
|
|
endfunc
|
|
|
|
|
2017-03-20 05:59:11 -07:00
|
|
|
func Test_BufEnter()
|
|
|
|
au! BufEnter
|
|
|
|
au Bufenter * let val = val . '+'
|
|
|
|
let g:val = ''
|
|
|
|
split NewFile
|
|
|
|
call assert_equal('+', g:val)
|
|
|
|
bwipe!
|
|
|
|
call assert_equal('++', g:val)
|
|
|
|
|
|
|
|
" Also get BufEnter when editing a directory
|
|
|
|
call mkdir('Xdir')
|
|
|
|
split Xdir
|
|
|
|
call assert_equal('+++', g:val)
|
|
|
|
bwipe!
|
|
|
|
|
|
|
|
call delete('Xdir', 'd')
|
|
|
|
au! BufEnter
|
|
|
|
endfunc
|
|
|
|
|
2017-03-19 18:48:28 -07:00
|
|
|
" Closing a window might cause an endless loop
|
|
|
|
" E814 for older Vims
|
|
|
|
function Test_autocmd_bufwipe_in_SessLoadPost()
|
|
|
|
if has('win32')
|
|
|
|
throw 'Skipped: test hangs on MS-Windows'
|
|
|
|
endif
|
|
|
|
tabnew
|
|
|
|
set noswapfile
|
|
|
|
let g:bufnr=bufnr('%')
|
|
|
|
mksession!
|
|
|
|
|
|
|
|
let content=['set nocp noswapfile',
|
|
|
|
\ 'let v:swapchoice="e"',
|
|
|
|
\ 'augroup test_autocmd_sessionload',
|
|
|
|
\ 'autocmd!',
|
2017-03-21 02:47:46 -07:00
|
|
|
\ 'autocmd SessionLoadPost * 4bw!|qall!',
|
|
|
|
\ 'augroup END',
|
2017-03-19 18:48:28 -07:00
|
|
|
\ ]
|
|
|
|
call writefile(content, 'Xvimrc')
|
2017-03-21 02:47:46 -07:00
|
|
|
let a=system(v:progpath. ' --headless -i NONE -u Xvimrc --noplugins -S Session.vim')
|
2017-03-19 18:48:28 -07:00
|
|
|
call assert_match('E814', a)
|
|
|
|
|
|
|
|
unlet! g:bufnr
|
|
|
|
set swapfile
|
|
|
|
for file in ['Session.vim', 'Xvimrc']
|
|
|
|
call delete(file)
|
|
|
|
endfor
|
|
|
|
endfunc
|
|
|
|
|
|
|
|
" SEGV occurs in older versions.
|
|
|
|
function Test_autocmd_bufwipe_in_SessLoadPost2()
|
|
|
|
if has('win32')
|
|
|
|
throw 'Skipped: test hangs on MS-Windows'
|
|
|
|
endif
|
|
|
|
tabnew
|
|
|
|
set noswapfile
|
|
|
|
let g:bufnr=bufnr('%')
|
|
|
|
mksession!
|
|
|
|
|
|
|
|
let content = ['set nocp noswapfile',
|
|
|
|
\ 'function! DeleteInactiveBufs()',
|
|
|
|
\ ' tabfirst',
|
|
|
|
\ ' let tabblist = []',
|
|
|
|
\ ' for i in range(1, tabpagenr(''$''))',
|
|
|
|
\ ' call extend(tabblist, tabpagebuflist(i))',
|
|
|
|
\ ' endfor',
|
|
|
|
\ ' for b in range(1, bufnr(''$''))',
|
|
|
|
\ ' if bufexists(b) && buflisted(b) && (index(tabblist, b) == -1 || bufname(b) =~# ''^$'')',
|
|
|
|
\ ' exec ''bwipeout '' . b',
|
|
|
|
\ ' endif',
|
|
|
|
\ ' endfor',
|
2017-03-21 02:47:46 -07:00
|
|
|
\ 'redraw!',
|
|
|
|
\ 'echon "SessionLoadPost DONE"',
|
|
|
|
\ 'qall!',
|
2017-03-19 18:48:28 -07:00
|
|
|
\ 'endfunction',
|
|
|
|
\ 'au SessionLoadPost * call DeleteInactiveBufs()']
|
|
|
|
call writefile(content, 'Xvimrc')
|
2017-03-21 02:47:46 -07:00
|
|
|
let a=system(v:progpath. ' --headless -i NONE -u Xvimrc --noplugins -S Session.vim')
|
2017-03-19 18:48:28 -07:00
|
|
|
" this probably only matches on unix
|
|
|
|
if has("unix")
|
|
|
|
call assert_notmatch('Caught deadly signal SEGV', a)
|
|
|
|
endif
|
|
|
|
call assert_match('SessionLoadPost DONE', a)
|
|
|
|
|
|
|
|
unlet! g:bufnr
|
|
|
|
set swapfile
|
|
|
|
for file in ['Session.vim', 'Xvimrc']
|
|
|
|
call delete(file)
|
|
|
|
endfor
|
|
|
|
endfunc
|