blob: c043a3531145470120037663db9d3cf59d2b55a0 [file] [log] [blame]
Bram Moolenaar14735512016-03-26 21:00:08 +01001" Tests for autocommands
2
Bram Moolenaar8c64a362018-03-23 22:39:31 +01003source shared.vim
Bram Moolenaar8c5a2782019-08-07 23:07:07 +02004source check.vim
Bram Moolenaarcadbe1b2019-09-22 21:50:09 +02005source term_util.vim
LemonBoy09371822022-04-08 15:18:45 +01006source screendump.vim
Bram Moolenaar6f2465d2022-03-22 18:13:01 +00007import './vim9.vim' as v9
Bram Moolenaar8c64a362018-03-23 22:39:31 +01008
Bram Moolenaar1e115362019-01-09 23:01:02 +01009func s:cleanup_buffers() abort
Bram Moolenaarb3435b02016-09-29 20:54:59 +020010 for bnr in range(1, bufnr('$'))
11 if bufloaded(bnr) && bufnr('%') != bnr
12 execute 'bd! ' . bnr
13 endif
14 endfor
Bram Moolenaar04f62f82017-07-19 18:18:39 +020015endfunc
Bram Moolenaarb3435b02016-09-29 20:54:59 +020016
Bram Moolenaar14735512016-03-26 21:00:08 +010017func Test_vim_did_enter()
18 call assert_false(v:vim_did_enter)
19
20 " This script will never reach the main loop, can't check if v:vim_did_enter
21 " becomes one.
22endfunc
Bram Moolenaar40b1b542016-04-20 20:18:23 +020023
Bram Moolenaar75911162020-07-21 19:44:47 +020024" Test for the CursorHold autocmd
25func Test_CursorHold_autocmd()
26 CheckRunVimInTerminal
27 call writefile(['one', 'two', 'three'], 'Xfile')
28 let before =<< trim END
29 set updatetime=10
30 au CursorHold * call writefile([line('.')], 'Xoutput', 'a')
31 END
32 call writefile(before, 'Xinit')
33 let buf = RunVimInTerminal('-S Xinit Xfile', {})
Bram Moolenaar17f67542020-08-20 18:29:13 +020034 call term_sendkeys(buf, "G")
Bram Moolenaar62cd26a2020-10-11 20:08:44 +020035 call term_wait(buf, 50)
Bram Moolenaar75911162020-07-21 19:44:47 +020036 call term_sendkeys(buf, "gg")
37 call term_wait(buf)
Bram Moolenaar17f67542020-08-20 18:29:13 +020038 call WaitForAssert({-> assert_equal(['1'], readfile('Xoutput')[-1:-1])})
Bram Moolenaar75911162020-07-21 19:44:47 +020039 call term_sendkeys(buf, "j")
40 call term_wait(buf)
Bram Moolenaar17f67542020-08-20 18:29:13 +020041 call WaitForAssert({-> assert_equal(['1', '2'], readfile('Xoutput')[-2:-1])})
Bram Moolenaar75911162020-07-21 19:44:47 +020042 call term_sendkeys(buf, "j")
43 call term_wait(buf)
Bram Moolenaar17f67542020-08-20 18:29:13 +020044 call WaitForAssert({-> assert_equal(['1', '2', '3'], readfile('Xoutput')[-3:-1])})
Bram Moolenaar75911162020-07-21 19:44:47 +020045 call StopVimInTerminal(buf)
46
Bram Moolenaar75911162020-07-21 19:44:47 +020047 call delete('Xinit')
48 call delete('Xoutput')
49 call delete('Xfile')
50endfunc
51
Bram Moolenaarc67e8922016-05-24 16:07:40 +020052if has('timers')
Bram Moolenaar97b00752019-05-12 13:07:14 +020053
Bram Moolenaarc67e8922016-05-24 16:07:40 +020054 func ExitInsertMode(id)
55 call feedkeys("\<Esc>")
56 endfunc
57
58 func Test_cursorhold_insert()
Bram Moolenaarf18c4db2016-09-08 22:10:06 +020059 " Need to move the cursor.
60 call feedkeys("ggG", "xt")
61
Bram Moolenaarc67e8922016-05-24 16:07:40 +020062 let g:triggered = 0
63 au CursorHoldI * let g:triggered += 1
64 set updatetime=20
Bram Moolenaar92bb83e2021-02-03 23:04:46 +010065 call timer_start(200, 'ExitInsertMode')
Bram Moolenaarc67e8922016-05-24 16:07:40 +020066 call feedkeys('a', 'x!')
67 call assert_equal(1, g:triggered)
Bram Moolenaar26d98212019-01-27 22:32:55 +010068 unlet g:triggered
69 au! CursorHoldI
70 set updatetime&
71 endfunc
72
73 func Test_cursorhold_insert_with_timer_interrupt()
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +020074 CheckFeature job
Bram Moolenaar26d98212019-01-27 22:32:55 +010075 " Need to move the cursor.
76 call feedkeys("ggG", "xt")
77
78 " Confirm the timer invoked in exit_cb of the job doesn't disturb
79 " CursorHoldI event.
80 let g:triggered = 0
81 au CursorHoldI * let g:triggered += 1
Bram Moolenaar62cd26a2020-10-11 20:08:44 +020082 set updatetime=100
Bram Moolenaar26d98212019-01-27 22:32:55 +010083 call job_start(has('win32') ? 'cmd /c echo:' : 'echo',
Bram Moolenaar62cd26a2020-10-11 20:08:44 +020084 \ {'exit_cb': {-> timer_start(200, 'ExitInsertMode')}})
Bram Moolenaar26d98212019-01-27 22:32:55 +010085 call feedkeys('a', 'x!')
86 call assert_equal(1, g:triggered)
87 unlet g:triggered
Bram Moolenaare99e8442016-07-26 20:43:40 +020088 au! CursorHoldI
Bram Moolenaaraeac9002016-09-06 22:15:08 +020089 set updatetime&
Bram Moolenaarc67e8922016-05-24 16:07:40 +020090 endfunc
91
92 func Test_cursorhold_insert_ctrl_x()
93 let g:triggered = 0
94 au CursorHoldI * let g:triggered += 1
95 set updatetime=20
96 call timer_start(100, 'ExitInsertMode')
97 " CursorHoldI does not trigger after CTRL-X
98 call feedkeys("a\<C-X>", 'x!')
99 call assert_equal(0, g:triggered)
Bram Moolenaar26d98212019-01-27 22:32:55 +0100100 unlet g:triggered
Bram Moolenaare99e8442016-07-26 20:43:40 +0200101 au! CursorHoldI
Bram Moolenaaraeac9002016-09-06 22:15:08 +0200102 set updatetime&
Bram Moolenaarc67e8922016-05-24 16:07:40 +0200103 endfunc
Bram Moolenaar97b00752019-05-12 13:07:14 +0200104
Bram Moolenaar5a9357d2021-10-03 16:22:05 +0100105 func Test_cursorhold_insert_ctrl_g_U()
106 au CursorHoldI * :
107 set updatetime=20
108 new
109 call timer_start(100, { -> feedkeys("\<Left>foo\<Esc>", 't') })
110 call feedkeys("i()\<C-g>U", 'tx!')
111 sleep 200m
112 call assert_equal('(foo)', getline(1))
113 undo
114 call assert_equal('', getline(1))
115
116 bwipe!
117 au! CursorHoldI
118 set updatetime&
119 endfunc
120
Bram Moolenaar97b00752019-05-12 13:07:14 +0200121 func Test_OptionSet_modeline()
122 call test_override('starting', 1)
123 au! OptionSet
124 augroup set_tabstop
125 au OptionSet tabstop call timer_start(1, {-> execute("echo 'Handler called'", "")})
126 augroup END
127 call writefile(['vim: set ts=7 sw=5 :', 'something'], 'XoptionsetModeline')
128 set modeline
129 let v:errmsg = ''
130 call assert_fails('split XoptionsetModeline', 'E12:')
131 call assert_equal(7, &ts)
132 call assert_equal('', v:errmsg)
133
134 augroup set_tabstop
135 au!
136 augroup END
137 bwipe!
138 set ts&
139 call delete('XoptionsetModeline')
140 call test_override('starting', 0)
141 endfunc
142
143endif "has('timers')
Bram Moolenaar40b1b542016-04-20 20:18:23 +0200144
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200145func Test_bufunload()
Bram Moolenaarc67e8922016-05-24 16:07:40 +0200146 augroup test_bufunload_group
147 autocmd!
148 autocmd BufUnload * call add(s:li, "bufunload")
149 autocmd BufDelete * call add(s:li, "bufdelete")
150 autocmd BufWipeout * call add(s:li, "bufwipeout")
151 augroup END
Bram Moolenaar40b1b542016-04-20 20:18:23 +0200152
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100153 let s:li = []
Bram Moolenaarc67e8922016-05-24 16:07:40 +0200154 new
155 setlocal bufhidden=
156 bunload
157 call assert_equal(["bufunload", "bufdelete"], s:li)
Bram Moolenaar40b1b542016-04-20 20:18:23 +0200158
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100159 let s:li = []
Bram Moolenaarc67e8922016-05-24 16:07:40 +0200160 new
161 setlocal bufhidden=delete
162 bunload
163 call assert_equal(["bufunload", "bufdelete"], s:li)
164
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100165 let s:li = []
Bram Moolenaarc67e8922016-05-24 16:07:40 +0200166 new
167 setlocal bufhidden=unload
168 bwipeout
169 call assert_equal(["bufunload", "bufdelete", "bufwipeout"], s:li)
170
Bram Moolenaare99e8442016-07-26 20:43:40 +0200171 au! test_bufunload_group
Bram Moolenaarc67e8922016-05-24 16:07:40 +0200172 augroup! test_bufunload_group
Bram Moolenaar40b1b542016-04-20 20:18:23 +0200173endfunc
Bram Moolenaar30445cb2016-07-09 15:21:02 +0200174
175" SEGV occurs in older versions. (At least 7.4.2005 or older)
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200176func Test_autocmd_bufunload_with_tabnext()
Bram Moolenaar30445cb2016-07-09 15:21:02 +0200177 tabedit
178 tabfirst
179
180 augroup test_autocmd_bufunload_with_tabnext_group
181 autocmd!
182 autocmd BufUnload <buffer> tabnext
183 augroup END
184
185 quit
186 call assert_equal(2, tabpagenr('$'))
187
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200188 autocmd! test_autocmd_bufunload_with_tabnext_group
Bram Moolenaar30445cb2016-07-09 15:21:02 +0200189 augroup! test_autocmd_bufunload_with_tabnext_group
190 tablast
191 quit
192endfunc
Bram Moolenaarc917da42016-07-19 22:31:36 +0200193
Bram Moolenaar5ed58c72021-01-28 14:24:55 +0100194func Test_argdelete_in_next()
195 au BufNew,BufEnter,BufLeave,BufWinEnter * argdel
196 call assert_fails('next a b', 'E1156:')
197 au! BufNew,BufEnter,BufLeave,BufWinEnter *
198endfunc
199
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200200func Test_autocmd_bufwinleave_with_tabfirst()
Bram Moolenaarf9e687e2016-09-04 21:33:09 +0200201 tabedit
202 augroup sample
203 autocmd!
204 autocmd BufWinLeave <buffer> tabfirst
205 augroup END
206 call setline(1, ['a', 'b', 'c'])
207 edit! a.txt
Bram Moolenaarf18c4db2016-09-08 22:10:06 +0200208 tabclose
Bram Moolenaarf9e687e2016-09-04 21:33:09 +0200209endfunc
210
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200211" SEGV occurs in older versions. (At least 7.4.2321 or older)
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200212func Test_autocmd_bufunload_avoiding_SEGV_01()
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200213 split aa.txt
214 let lastbuf = bufnr('$')
215
216 augroup test_autocmd_bufunload
217 autocmd!
218 exe 'autocmd BufUnload <buffer> ' . (lastbuf + 1) . 'bwipeout!'
219 augroup END
220
Bram Moolenaar28ee8922020-10-28 20:20:00 +0100221 call assert_fails('edit bb.txt', 'E937:')
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200222
223 autocmd! test_autocmd_bufunload
224 augroup! test_autocmd_bufunload
225 bwipe! aa.txt
226 bwipe! bb.txt
227endfunc
228
229" SEGV occurs in older versions. (At least 7.4.2321 or older)
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200230func Test_autocmd_bufunload_avoiding_SEGV_02()
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200231 setlocal buftype=nowrite
232 let lastbuf = bufnr('$')
233
234 augroup test_autocmd_bufunload
235 autocmd!
236 exe 'autocmd BufUnload <buffer> ' . (lastbuf + 1) . 'bwipeout!'
237 augroup END
238
239 normal! i1
240 call assert_fails('edit a.txt', 'E517:')
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200241
242 autocmd! test_autocmd_bufunload
243 augroup! test_autocmd_bufunload
244 bwipe! a.txt
245endfunc
246
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100247func Test_autocmd_dummy_wipeout()
248 " prepare files
249 call writefile([''], 'Xdummywipetest1.txt')
250 call writefile([''], 'Xdummywipetest2.txt')
251 augroup test_bufunload_group
252 autocmd!
253 autocmd BufUnload * call add(s:li, "bufunload")
254 autocmd BufDelete * call add(s:li, "bufdelete")
255 autocmd BufWipeout * call add(s:li, "bufwipeout")
256 augroup END
257
258 let s:li = []
259 split Xdummywipetest1.txt
260 silent! vimgrep /notmatched/ Xdummywipetest*
261 call assert_equal(["bufunload", "bufwipeout"], s:li)
262
263 bwipeout
264 call delete('Xdummywipetest1.txt')
265 call delete('Xdummywipetest2.txt')
266 au! test_bufunload_group
267 augroup! test_bufunload_group
268endfunc
269
Bram Moolenaarc917da42016-07-19 22:31:36 +0200270func Test_win_tab_autocmd()
271 let g:record = []
272
273 augroup testing
274 au WinNew * call add(g:record, 'WinNew')
naohiro ono23beefe2021-11-13 12:38:49 +0000275 au WinClosed * call add(g:record, 'WinClosed')
Bram Moolenaarc917da42016-07-19 22:31:36 +0200276 au WinEnter * call add(g:record, 'WinEnter')
277 au WinLeave * call add(g:record, 'WinLeave')
278 au TabNew * call add(g:record, 'TabNew')
Bram Moolenaar12c11d52016-07-19 23:13:03 +0200279 au TabClosed * call add(g:record, 'TabClosed')
Bram Moolenaarc917da42016-07-19 22:31:36 +0200280 au TabEnter * call add(g:record, 'TabEnter')
281 au TabLeave * call add(g:record, 'TabLeave')
282 augroup END
283
284 split
285 tabnew
286 close
287 close
288
289 call assert_equal([
290 \ 'WinLeave', 'WinNew', 'WinEnter',
291 \ 'WinLeave', 'TabLeave', 'WinNew', 'WinEnter', 'TabNew', 'TabEnter',
naohiro ono23beefe2021-11-13 12:38:49 +0000292 \ 'WinLeave', 'TabLeave', 'WinClosed', 'TabClosed', 'WinEnter', 'TabEnter',
293 \ 'WinLeave', 'WinClosed', 'WinEnter'
Bram Moolenaarc917da42016-07-19 22:31:36 +0200294 \ ], g:record)
295
Bram Moolenaar12c11d52016-07-19 23:13:03 +0200296 let g:record = []
297 tabnew somefile
298 tabnext
299 bwipe somefile
300
301 call assert_equal([
302 \ 'WinLeave', 'TabLeave', 'WinNew', 'WinEnter', 'TabNew', 'TabEnter',
303 \ 'WinLeave', 'TabLeave', 'WinEnter', 'TabEnter',
naohiro ono23beefe2021-11-13 12:38:49 +0000304 \ 'WinClosed', 'TabClosed'
Bram Moolenaar12c11d52016-07-19 23:13:03 +0200305 \ ], g:record)
306
Bram Moolenaarc917da42016-07-19 22:31:36 +0200307 augroup testing
308 au!
309 augroup END
310 unlet g:record
311endfunc
Bram Moolenaare99e8442016-07-26 20:43:40 +0200312
LemonBoy09371822022-04-08 15:18:45 +0100313func Test_WinScrolled()
314 CheckRunVimInTerminal
315
316 let lines =<< trim END
zeertzjqd58862d2022-04-12 11:32:48 +0100317 set nowrap scrolloff=0
318 for ii in range(1, 18)
319 call setline(ii, repeat(nr2char(96 + ii), ii * 2))
320 endfor
321 let win_id = win_getid()
322 let g:matched = v:false
323 execute 'au WinScrolled' win_id 'let g:matched = v:true'
324 let g:scrolled = 0
325 au WinScrolled * let g:scrolled += 1
326 au WinScrolled * let g:amatch = str2nr(expand('<amatch>'))
327 au WinScrolled * let g:afile = str2nr(expand('<afile>'))
LemonBoy09371822022-04-08 15:18:45 +0100328 END
329 call writefile(lines, 'Xtest_winscrolled')
330 let buf = RunVimInTerminal('-S Xtest_winscrolled', {'rows': 6})
331
332 call term_sendkeys(buf, ":echo g:scrolled\<CR>")
333 call WaitForAssert({-> assert_match('^0 ', term_getline(buf, 6))}, 1000)
334
335 " Scroll left/right in Normal mode.
336 call term_sendkeys(buf, "zlzh:echo g:scrolled\<CR>")
337 call WaitForAssert({-> assert_match('^2 ', term_getline(buf, 6))}, 1000)
338
339 " Scroll up/down in Normal mode.
340 call term_sendkeys(buf, "\<c-e>\<c-y>:echo g:scrolled\<CR>")
341 call WaitForAssert({-> assert_match('^4 ', term_getline(buf, 6))}, 1000)
342
343 " Scroll up/down in Insert mode.
344 call term_sendkeys(buf, "Mi\<c-x>\<c-e>\<Esc>i\<c-x>\<c-y>\<Esc>")
345 call term_sendkeys(buf, ":echo g:scrolled\<CR>")
346 call WaitForAssert({-> assert_match('^6 ', term_getline(buf, 6))}, 1000)
347
348 " Scroll the window horizontally to focus the last letter of the third line
349 " containing only six characters. Moving to the previous and shorter lines
350 " should trigger another autocommand as Vim has to make them visible.
351 call term_sendkeys(buf, "5zl2k")
352 call term_sendkeys(buf, ":echo g:scrolled\<CR>")
353 call WaitForAssert({-> assert_match('^8 ', term_getline(buf, 6))}, 1000)
354
355 " Ensure the command was triggered for the specified window ID.
356 call term_sendkeys(buf, ":echo g:matched\<CR>")
357 call WaitForAssert({-> assert_match('^v:true ', term_getline(buf, 6))}, 1000)
358
359 " Ensure the expansion of <amatch> and <afile> matches the window ID.
360 call term_sendkeys(buf, ":echo g:amatch == win_id && g:afile == win_id\<CR>")
361 call WaitForAssert({-> assert_match('^v:true ', term_getline(buf, 6))}, 1000)
362
363 call StopVimInTerminal(buf)
364 call delete('Xtest_winscrolled')
365endfunc
366
LemonBoy66e13ae2022-04-21 22:52:11 +0100367func Test_WinScrolled_mouse()
368 CheckRunVimInTerminal
369
370 let lines =<< trim END
371 set nowrap scrolloff=0
372 set mouse=a term=xterm ttymouse=sgr mousetime=200 clipboard=
373 call setline(1, ['foo']->repeat(32))
374 split
375 let g:scrolled = 0
376 au WinScrolled * let g:scrolled += 1
377 END
378 call writefile(lines, 'Xtest_winscrolled_mouse')
379 let buf = RunVimInTerminal('-S Xtest_winscrolled_mouse', {'rows': 10})
380
381 " With the upper split focused, send a scroll-down event to the unfocused one.
382 call test_setmouse(7, 1)
383 call term_sendkeys(buf, "\<ScrollWheelDown>")
384 call TermWait(buf)
385 call term_sendkeys(buf, ":echo g:scrolled\<CR>")
386 call WaitForAssert({-> assert_match('^1', term_getline(buf, 10))}, 1000)
387
388 " Again, but this time while we're in insert mode.
389 call term_sendkeys(buf, "i\<ScrollWheelDown>\<Esc>")
390 call TermWait(buf)
391 call term_sendkeys(buf, ":echo g:scrolled\<CR>")
392 call WaitForAssert({-> assert_match('^2', term_getline(buf, 10))}, 1000)
393
394 call StopVimInTerminal(buf)
395 call delete('Xtest_winscrolled_mouse')
396endfunc
397
zeertzjqd58862d2022-04-12 11:32:48 +0100398func Test_WinScrolled_close_curwin()
399 CheckRunVimInTerminal
400
401 let lines =<< trim END
402 set nowrap scrolloff=0
403 call setline(1, ['aaa', 'bbb'])
404 vsplit
405 au WinScrolled * close
406 au VimLeave * call writefile(['123456'], 'Xtestout')
407 END
408 call writefile(lines, 'Xtest_winscrolled_close_curwin')
409 let buf = RunVimInTerminal('-S Xtest_winscrolled_close_curwin', {'rows': 6})
410
411 " This was using freed memory
412 call term_sendkeys(buf, "\<C-E>")
413 call TermWait(buf)
414 call StopVimInTerminal(buf)
415
416 call assert_equal(['123456'], readfile('Xtestout'))
417
418 call delete('Xtest_winscrolled_close_curwin')
419 call delete('Xtestout')
420endfunc
421
naohiro ono23beefe2021-11-13 12:38:49 +0000422func Test_WinClosed()
423 " Test that the pattern is matched against the closed window's ID, and both
424 " <amatch> and <afile> are set to it.
425 new
426 let winid = win_getid()
427 let g:matched = v:false
428 augroup test-WinClosed
429 autocmd!
430 execute 'autocmd WinClosed' winid 'let g:matched = v:true'
431 autocmd WinClosed * let g:amatch = str2nr(expand('<amatch>'))
432 autocmd WinClosed * let g:afile = str2nr(expand('<afile>'))
433 augroup END
434 close
435 call assert_true(g:matched)
436 call assert_equal(winid, g:amatch)
437 call assert_equal(winid, g:afile)
438
439 " Test that WinClosed is non-recursive.
440 new
441 new
442 call assert_equal(3, winnr('$'))
443 let g:triggered = 0
444 augroup test-WinClosed
445 autocmd!
446 autocmd WinClosed * let g:triggered += 1
447 autocmd WinClosed * 2 wincmd c
448 augroup END
449 close
450 call assert_equal(1, winnr('$'))
451 call assert_equal(1, g:triggered)
452
453 autocmd! test-WinClosed
454 augroup! test-WinClosed
455 unlet g:matched
456 unlet g:amatch
457 unlet g:afile
458 unlet g:triggered
459endfunc
460
Bram Moolenaarc947b9a2022-04-06 17:59:21 +0100461func Test_WinClosed_throws()
462 vnew
463 let bnr = bufnr()
464 call assert_equal(1, bufloaded(bnr))
465 augroup test-WinClosed
466 autocmd WinClosed * throw 'foo'
467 augroup END
468 try
469 close
470 catch /.*/
471 endtry
472 call assert_equal(0, bufloaded(bnr))
473
474 autocmd! test-WinClosed
475 augroup! test-WinClosed
476endfunc
477
zeertzjq6a069402022-04-07 14:08:29 +0100478func Test_WinClosed_throws_with_tabs()
479 tabnew
480 let bnr = bufnr()
481 call assert_equal(1, bufloaded(bnr))
482 augroup test-WinClosed
483 autocmd WinClosed * throw 'foo'
484 augroup END
485 try
486 close
487 catch /.*/
488 endtry
489 call assert_equal(0, bufloaded(bnr))
490
491 autocmd! test-WinClosed
492 augroup! test-WinClosed
493endfunc
494
Bram Moolenaare99e8442016-07-26 20:43:40 +0200495func s:AddAnAutocmd()
496 augroup vimBarTest
497 au BufReadCmd * echo 'hello'
498 augroup END
499 call assert_equal(3, len(split(execute('au vimBarTest'), "\n")))
500endfunc
501
502func Test_early_bar()
503 " test that a bar is recognized before the {event}
504 call s:AddAnAutocmd()
Bram Moolenaarb8e642f2021-11-20 10:38:25 +0000505 augroup vimBarTest | au! | let done = 77 | augroup END
Bram Moolenaare99e8442016-07-26 20:43:40 +0200506 call assert_equal(1, len(split(execute('au vimBarTest'), "\n")))
Bram Moolenaarb8e642f2021-11-20 10:38:25 +0000507 call assert_equal(77, done)
Bram Moolenaare99e8442016-07-26 20:43:40 +0200508
509 call s:AddAnAutocmd()
Bram Moolenaarb8e642f2021-11-20 10:38:25 +0000510 augroup vimBarTest| au!| let done = 88 | augroup END
Bram Moolenaare99e8442016-07-26 20:43:40 +0200511 call assert_equal(1, len(split(execute('au vimBarTest'), "\n")))
Bram Moolenaarb8e642f2021-11-20 10:38:25 +0000512 call assert_equal(88, done)
Bram Moolenaare99e8442016-07-26 20:43:40 +0200513
514 " test that a bar is recognized after the {event}
515 call s:AddAnAutocmd()
Bram Moolenaarb8e642f2021-11-20 10:38:25 +0000516 augroup vimBarTest| au!BufReadCmd| let done = 99 | augroup END
Bram Moolenaare99e8442016-07-26 20:43:40 +0200517 call assert_equal(1, len(split(execute('au vimBarTest'), "\n")))
Bram Moolenaarb8e642f2021-11-20 10:38:25 +0000518 call assert_equal(99, done)
Bram Moolenaare99e8442016-07-26 20:43:40 +0200519
520 " test that a bar is recognized after the {group}
521 call s:AddAnAutocmd()
522 au! vimBarTest|echo 'hello'
523 call assert_equal(1, len(split(execute('au vimBarTest'), "\n")))
524endfunc
Bram Moolenaarf2c4c392016-07-29 20:50:24 +0200525
Bram Moolenaar5c809082016-09-01 16:21:48 +0200526func RemoveGroup()
527 autocmd! StartOK
528 augroup! StartOK
529endfunc
530
Bram Moolenaarf2c4c392016-07-29 20:50:24 +0200531func Test_augroup_warning()
532 augroup TheWarning
533 au VimEnter * echo 'entering'
534 augroup END
Bram Moolenaar5dc4e2f2020-11-25 14:15:12 +0100535 call assert_match("TheWarning.*VimEnter", execute('au VimEnter'))
Bram Moolenaarf2c4c392016-07-29 20:50:24 +0200536 redir => res
537 augroup! TheWarning
538 redir END
Bram Moolenaar5dc4e2f2020-11-25 14:15:12 +0100539 call assert_match("W19:", res)
540 call assert_match("-Deleted-.*VimEnter", execute('au VimEnter'))
Bram Moolenaarf2c4c392016-07-29 20:50:24 +0200541
542 " check "Another" does not take the pace of the deleted entry
543 augroup Another
544 augroup END
Bram Moolenaar5dc4e2f2020-11-25 14:15:12 +0100545 call assert_match("-Deleted-.*VimEnter", execute('au VimEnter'))
Bram Moolenaaraeac9002016-09-06 22:15:08 +0200546 augroup! Another
Bram Moolenaar5c809082016-09-01 16:21:48 +0200547
548 " no warning for postpone aucmd delete
549 augroup StartOK
550 au VimEnter * call RemoveGroup()
551 augroup END
Bram Moolenaar5dc4e2f2020-11-25 14:15:12 +0100552 call assert_match("StartOK.*VimEnter", execute('au VimEnter'))
Bram Moolenaar5c809082016-09-01 16:21:48 +0200553 redir => res
554 doautocmd VimEnter
555 redir END
Bram Moolenaar5dc4e2f2020-11-25 14:15:12 +0100556 call assert_notmatch("W19:", res)
Bram Moolenaarde653f02016-09-03 16:59:06 +0200557 au! VimEnter
Bram Moolenaarad48e6c2020-04-21 22:19:45 +0200558
559 call assert_fails('augroup!', 'E471:')
Bram Moolenaarf2c4c392016-07-29 20:50:24 +0200560endfunc
Bram Moolenaarb62cc362016-09-03 16:43:53 +0200561
Bram Moolenaar8d84ff12017-10-26 16:42:16 +0200562func Test_BufReadCmdHelp()
563 " This used to cause access to free memory
564 au BufReadCmd * e +h
565 help
566
Bram Moolenaar8d84ff12017-10-26 16:42:16 +0200567 au! BufReadCmd
568endfunc
569
570func Test_BufReadCmdHelpJump()
571 " This used to cause access to free memory
572 au BufReadCmd * e +h{
Bram Moolenaarcf1ba352017-10-27 00:55:04 +0200573 " } to fix highlighting
574 call assert_fails('help', 'E434:')
Bram Moolenaar8d84ff12017-10-26 16:42:16 +0200575
Bram Moolenaar8d84ff12017-10-26 16:42:16 +0200576 au! BufReadCmd
577endfunc
578
Bram Moolenaarb1d2c812022-08-26 11:55:01 +0100579" BufReadCmd is triggered for a "nofile" buffer
580func Test_BufReadCmdNofile()
581 new somefile
582 set buftype=nofile
583 au BufReadCmd somefile call setline(1, 'triggered')
584 edit
585 call assert_equal('triggered', getline(1))
586
587 au! BufReadCmd
588 bwipe!
589endfunc
590
Bram Moolenaarb62cc362016-09-03 16:43:53 +0200591func Test_augroup_deleted()
Bram Moolenaarde653f02016-09-03 16:59:06 +0200592 " This caused a crash before E936 was introduced
Bram Moolenaarb62cc362016-09-03 16:43:53 +0200593 augroup x
Bram Moolenaarde653f02016-09-03 16:59:06 +0200594 call assert_fails('augroup! x', 'E936:')
595 au VimEnter * echo
596 augroup end
Bram Moolenaarb62cc362016-09-03 16:43:53 +0200597 augroup! x
Bram Moolenaar5dc4e2f2020-11-25 14:15:12 +0100598 call assert_match("-Deleted-.*VimEnter", execute('au VimEnter'))
Bram Moolenaarde653f02016-09-03 16:59:06 +0200599 au! VimEnter
Bram Moolenaarb62cc362016-09-03 16:43:53 +0200600endfunc
601
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200602" Tests for autocommands on :close command.
603" This used to be in test13.
604func Test_three_windows()
Bram Moolenaarb3435b02016-09-29 20:54:59 +0200605 " Clean up buffers, because in some cases this function fails.
606 call s:cleanup_buffers()
607
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200608 " Write three files and open them, each in a window.
609 " Then go to next window, with autocommand that deletes the previous one.
610 " Do this twice, writing the file.
611 e! Xtestje1
612 call setline(1, 'testje1')
613 w
614 sp Xtestje2
615 call setline(1, 'testje2')
616 w
617 sp Xtestje3
618 call setline(1, 'testje3')
619 w
620 wincmd w
621 au WinLeave Xtestje2 bwipe
622 wincmd w
623 call assert_equal('Xtestje1', expand('%'))
624
625 au WinLeave Xtestje1 bwipe Xtestje3
626 close
627 call assert_equal('Xtestje1', expand('%'))
628
629 " Test deleting the buffer on a Unload event. If this goes wrong there
630 " will be the ATTENTION prompt.
631 e Xtestje1
632 au!
633 au! BufUnload Xtestje1 bwipe
634 call assert_fails('e Xtestje3', 'E937:')
635 call assert_equal('Xtestje3', expand('%'))
636
637 e Xtestje2
638 sp Xtestje1
639 call assert_fails('e', 'E937:')
Bram Moolenaara997b452018-04-17 23:24:06 +0200640 call assert_equal('Xtestje1', expand('%'))
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200641
642 " Test changing buffers in a BufWipeout autocommand. If this goes wrong
643 " there are ml_line errors and/or a Crash.
644 au!
645 only
646 e Xanother
647 e Xtestje1
648 bwipe Xtestje2
649 bwipe Xtestje3
650 au BufWipeout Xtestje1 buf Xtestje1
651 bwipe
652 call assert_equal('Xanother', expand('%'))
653
654 only
655 help
656 wincmd w
657 1quit
658 call assert_equal('Xanother', expand('%'))
659
660 au!
Bram Moolenaar4520d442017-03-19 16:09:46 +0100661 enew
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200662 call delete('Xtestje1')
663 call delete('Xtestje2')
664 call delete('Xtestje3')
665endfunc
Bram Moolenaare13b9af2017-01-13 22:01:02 +0100666
667func Test_BufEnter()
668 au! BufEnter
669 au Bufenter * let val = val . '+'
670 let g:val = ''
671 split NewFile
672 call assert_equal('+', g:val)
673 bwipe!
674 call assert_equal('++', g:val)
675
676 " Also get BufEnter when editing a directory
677 call mkdir('Xdir')
678 split Xdir
679 call assert_equal('+++', g:val)
Bram Moolenaare94260f2017-03-21 15:50:12 +0100680
681 " On MS-Windows we can't edit the directory, make sure we wipe the right
682 " buffer.
683 bwipe! Xdir
Bram Moolenaare13b9af2017-01-13 22:01:02 +0100684
685 call delete('Xdir', 'd')
686 au! BufEnter
687endfunc
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100688
689" Closing a window might cause an endless loop
690" E814 for older Vims
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200691func Test_autocmd_bufwipe_in_SessLoadPost()
Bram Moolenaar1d68d9b2017-10-13 22:33:32 +0200692 edit Xtest
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100693 tabnew
Bram Moolenaar1d68d9b2017-10-13 22:33:32 +0200694 file Xsomething
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100695 set noswapfile
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100696 mksession!
697
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200698 let content =<< trim [CODE]
Bram Moolenaar62cd26a2020-10-11 20:08:44 +0200699 call test_override('ui_delay', 10)
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200700 set nocp noswapfile
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100701 let v:swapchoice = "e"
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200702 augroup test_autocmd_sessionload
703 autocmd!
704 autocmd SessionLoadPost * exe bufnr("Xsomething") . "bw!"
705 augroup END
706
707 func WriteErrors()
708 call writefile([execute("messages")], "Xerrors")
709 endfunc
710 au VimLeave * call WriteErrors()
711 [CODE]
712
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100713 call writefile(content, 'Xvimrc')
Bram Moolenaar93344c22019-08-14 21:12:05 +0200714 call system(GetVimCommand('Xvimrc') .. ' --not-a-term --noplugins -S Session.vim -c cq')
Bram Moolenaare94260f2017-03-21 15:50:12 +0100715 let errors = join(readfile('Xerrors'))
Bram Moolenaare2e40752020-09-04 21:18:46 +0200716 call assert_match('E814:', errors)
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100717
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100718 set swapfile
Bram Moolenaare94260f2017-03-21 15:50:12 +0100719 for file in ['Session.vim', 'Xvimrc', 'Xerrors']
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100720 call delete(file)
721 endfor
722endfunc
723
Bram Moolenaar797e63b2021-01-15 16:22:52 +0100724" Using :blast and :ball for many events caused a crash, because b_nwindows was
725" not incremented correctly.
726func Test_autocmd_blast_badd()
727 let content =<< trim [CODE]
728 au BufNew,BufAdd,BufWinEnter,BufEnter,BufLeave,BufWinLeave,BufUnload,VimEnter foo* blast
729 edit foo1
730 au BufNew,BufAdd,BufWinEnter,BufEnter,BufLeave,BufWinLeave,BufUnload,VimEnter foo* ball
731 edit foo2
732 call writefile(['OK'], 'Xerrors')
733 qall
734 [CODE]
735
736 call writefile(content, 'XblastBall')
737 call system(GetVimCommand() .. ' --clean -S XblastBall')
738 call assert_match('OK', readfile('Xerrors')->join())
739
740 call delete('XblastBall')
741 call delete('Xerrors')
742endfunc
743
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100744" SEGV occurs in older versions.
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200745func Test_autocmd_bufwipe_in_SessLoadPost2()
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100746 tabnew
747 set noswapfile
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100748 mksession!
749
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200750 let content =<< trim [CODE]
751 set nocp noswapfile
752 function! DeleteInactiveBufs()
753 tabfirst
754 let tabblist = []
755 for i in range(1, tabpagenr(''$''))
756 call extend(tabblist, tabpagebuflist(i))
757 endfor
758 for b in range(1, bufnr(''$''))
759 if bufexists(b) && buflisted(b) && (index(tabblist, b) == -1 || bufname(b) =~# ''^$'')
760 exec ''bwipeout '' . b
761 endif
762 endfor
763 echomsg "SessionLoadPost DONE"
764 endfunction
765 au SessionLoadPost * call DeleteInactiveBufs()
766
767 func WriteErrors()
768 call writefile([execute("messages")], "Xerrors")
769 endfunc
770 au VimLeave * call WriteErrors()
771 [CODE]
772
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100773 call writefile(content, 'Xvimrc')
Bram Moolenaar93344c22019-08-14 21:12:05 +0200774 call system(GetVimCommand('Xvimrc') .. ' --not-a-term --noplugins -S Session.vim -c cq')
Bram Moolenaare94260f2017-03-21 15:50:12 +0100775 let errors = join(readfile('Xerrors'))
776 " This probably only ever matches on unix.
777 call assert_notmatch('Caught deadly signal SEGV', errors)
778 call assert_match('SessionLoadPost DONE', errors)
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100779
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100780 set swapfile
Bram Moolenaare94260f2017-03-21 15:50:12 +0100781 for file in ['Session.vim', 'Xvimrc', 'Xerrors']
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100782 call delete(file)
783 endfor
784endfunc
Bram Moolenaarfaf29d72017-07-09 11:07:16 +0200785
786func Test_empty_doau()
787 doau \|
788endfunc
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200789
790func s:AutoCommandOptionSet(match)
Bram Moolenaard7c96872019-06-15 17:12:48 +0200791 let template = "Option: <%s>, OldVal: <%s>, OldValLocal: <%s>, OldValGlobal: <%s>, NewVal: <%s>, Scope: <%s>, Command: <%s>\n"
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200792 let item = remove(g:options, 0)
Bram Moolenaard7c96872019-06-15 17:12:48 +0200793 let expected = printf(template, item[0], item[1], item[2], item[3], item[4], item[5], item[6])
794 let actual = printf(template, a:match, v:option_old, v:option_oldlocal, v:option_oldglobal, v:option_new, v:option_type, v:option_command)
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200795 let g:opt = [expected, actual]
796 "call assert_equal(expected, actual)
797endfunc
798
799func Test_OptionSet()
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +0200800 CheckOption autochdir
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200801
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +0200802 badd test_autocmd.vim
803
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200804 call test_override('starting', 1)
805 set nocp
806 au OptionSet * :call s:AutoCommandOptionSet(expand("<amatch>"))
807
808 " 1: Setting number option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100809 let g:options = [['number', 0, 0, 0, 1, 'global', 'set']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200810 set nu
811 call assert_equal([], g:options)
812 call assert_equal(g:opt[0], g:opt[1])
813
814 " 2: Setting local number option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100815 let g:options = [['number', 1, 1, '', 0, 'local', 'setlocal']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200816 setlocal nonu
817 call assert_equal([], g:options)
818 call assert_equal(g:opt[0], g:opt[1])
819
820 " 3: Setting global number option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100821 let g:options = [['number', 1, '', 1, 0, 'global', 'setglobal']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200822 setglobal nonu
823 call assert_equal([], g:options)
824 call assert_equal(g:opt[0], g:opt[1])
825
826 " 4: Setting local autoindent option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100827 let g:options = [['autoindent', 0, 0, '', 1, 'local', 'setlocal']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200828 setlocal ai
829 call assert_equal([], g:options)
830 call assert_equal(g:opt[0], g:opt[1])
831
832 " 5: Setting global autoindent option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100833 let g:options = [['autoindent', 0, '', 0, 1, 'global', 'setglobal']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200834 setglobal ai
835 call assert_equal([], g:options)
836 call assert_equal(g:opt[0], g:opt[1])
837
838 " 6: Setting global autoindent option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100839 let g:options = [['autoindent', 1, 1, 1, 0, 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +0200840 set ai!
841 call assert_equal([], g:options)
842 call assert_equal(g:opt[0], g:opt[1])
843
844 " 6a: Setting global autoindent option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100845 let g:options = [['autoindent', 1, 1, 0, 0, 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +0200846 noa setlocal ai
847 noa setglobal noai
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200848 set ai!
849 call assert_equal([], g:options)
850 call assert_equal(g:opt[0], g:opt[1])
851
852 " Should not print anything, use :noa
853 " 7: don't trigger OptionSet"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100854 let g:options = [['invalid', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200855 noa set nonu
Bram Moolenaard7c96872019-06-15 17:12:48 +0200856 call assert_equal([['invalid', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid']], g:options)
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200857 call assert_equal(g:opt[0], g:opt[1])
858
859 " 8: Setting several global list and number option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100860 let g:options = [['list', 0, 0, 0, 1, 'global', 'set'], ['number', 0, 0, 0, 1, 'global', 'set']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200861 set list nu
862 call assert_equal([], g:options)
863 call assert_equal(g:opt[0], g:opt[1])
864
865 " 9: don't trigger OptionSet"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100866 let g:options = [['invalid', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid'], ['invalid', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200867 noa set nolist nonu
Bram Moolenaard7c96872019-06-15 17:12:48 +0200868 call assert_equal([['invalid', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid'], ['invalid', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid']], g:options)
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200869 call assert_equal(g:opt[0], g:opt[1])
870
871 " 10: Setting global acd"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100872 let g:options = [['autochdir', 0, 0, '', 1, 'local', 'setlocal']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200873 setlocal acd
874 call assert_equal([], g:options)
875 call assert_equal(g:opt[0], g:opt[1])
876
877 " 11: Setting global autoread (also sets local value)"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100878 let g:options = [['autoread', 0, 0, 0, 1, 'global', 'set']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200879 set ar
880 call assert_equal([], g:options)
881 call assert_equal(g:opt[0], g:opt[1])
882
883 " 12: Setting local autoread"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100884 let g:options = [['autoread', 1, 1, '', 1, 'local', 'setlocal']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200885 setlocal ar
886 call assert_equal([], g:options)
887 call assert_equal(g:opt[0], g:opt[1])
888
889 " 13: Setting global autoread"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100890 let g:options = [['autoread', 1, '', 1, 0, 'global', 'setglobal']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200891 setglobal invar
892 call assert_equal([], g:options)
893 call assert_equal(g:opt[0], g:opt[1])
894
895 " 14: Setting option backspace through :let"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100896 let g:options = [['backspace', '', '', '', 'eol,indent,start', 'global', 'set']]
897 let &bs = "eol,indent,start"
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200898 call assert_equal([], g:options)
899 call assert_equal(g:opt[0], g:opt[1])
900
901 " 15: Setting option backspace through setbufvar()"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100902 let g:options = [['backup', 0, 0, '', 1, 'local', 'setlocal']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200903 " try twice, first time, shouldn't trigger because option name is invalid,
904 " second time, it should trigger
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +0200905 let bnum = bufnr('%')
Bram Moolenaare2e40752020-09-04 21:18:46 +0200906 call assert_fails("call setbufvar(bnum, '&l:bk', 1)", 'E355:')
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200907 " should trigger, use correct option name
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +0200908 call setbufvar(bnum, '&backup', 1)
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200909 call assert_equal([], g:options)
910 call assert_equal(g:opt[0], g:opt[1])
911
912 " 16: Setting number option using setwinvar"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100913 let g:options = [['number', 0, 0, '', 1, 'local', 'setlocal']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200914 call setwinvar(0, '&number', 1)
915 call assert_equal([], g:options)
916 call assert_equal(g:opt[0], g:opt[1])
917
918 " 17: Setting key option, shouldn't trigger"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100919 let g:options = [['key', 'invalid', 'invalid1', 'invalid2', 'invalid3', 'invalid4', 'invalid5']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200920 setlocal key=blah
921 setlocal key=
Bram Moolenaard7c96872019-06-15 17:12:48 +0200922 call assert_equal([['key', 'invalid', 'invalid1', 'invalid2', 'invalid3', 'invalid4', 'invalid5']], g:options)
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200923 call assert_equal(g:opt[0], g:opt[1])
924
Bram Moolenaard7c96872019-06-15 17:12:48 +0200925
926 " 18a: Setting string global option"
927 let oldval = &backupext
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100928 let g:options = [['backupext', oldval, oldval, oldval, 'foo', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +0200929 set backupext=foo
930 call assert_equal([], g:options)
931 call assert_equal(g:opt[0], g:opt[1])
932
933 " 18b: Resetting string global option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100934 let g:options = [['backupext', 'foo', 'foo', 'foo', oldval, 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +0200935 set backupext&
936 call assert_equal([], g:options)
937 call assert_equal(g:opt[0], g:opt[1])
938
939 " 18c: Setting global string global option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100940 let g:options = [['backupext', oldval, '', oldval, 'bar', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +0200941 setglobal backupext=bar
942 call assert_equal([], g:options)
943 call assert_equal(g:opt[0], g:opt[1])
944
945 " 18d: Setting local string global option"
946 " As this is a global option this sets the global value even though
947 " :setlocal is used!
948 noa set backupext& " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100949 let g:options = [['backupext', oldval, oldval, '', 'baz', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +0200950 setlocal backupext=baz
951 call assert_equal([], g:options)
952 call assert_equal(g:opt[0], g:opt[1])
953
954 " 18e: Setting again string global option"
955 noa setglobal backupext=ext_global " Reset global and local value (without triggering autocmd)
956 noa setlocal backupext=ext_local " Sets the global(!) value!
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100957 let g:options = [['backupext', 'ext_local', 'ext_local', 'ext_local', 'fuu', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +0200958 set backupext=fuu
959 call assert_equal([], g:options)
960 call assert_equal(g:opt[0], g:opt[1])
961
962
zeertzjqb811de52021-10-21 10:50:44 +0100963 " 19a: Setting string global-local (to buffer) option"
Bram Moolenaar8efa0262017-08-20 15:47:20 +0200964 let oldval = &tags
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100965 let g:options = [['tags', oldval, oldval, oldval, 'tagpath', 'global', 'set']]
Bram Moolenaar8efa0262017-08-20 15:47:20 +0200966 set tags=tagpath
967 call assert_equal([], g:options)
968 call assert_equal(g:opt[0], g:opt[1])
969
zeertzjqb811de52021-10-21 10:50:44 +0100970 " 19b: Resetting string global-local (to buffer) option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100971 let g:options = [['tags', 'tagpath', 'tagpath', 'tagpath', oldval, 'global', 'set']]
Bram Moolenaar8efa0262017-08-20 15:47:20 +0200972 set tags&
973 call assert_equal([], g:options)
974 call assert_equal(g:opt[0], g:opt[1])
975
zeertzjqb811de52021-10-21 10:50:44 +0100976 " 19c: Setting global string global-local (to buffer) option "
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100977 let g:options = [['tags', oldval, '', oldval, 'tagpath1', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +0200978 setglobal tags=tagpath1
979 call assert_equal([], g:options)
980 call assert_equal(g:opt[0], g:opt[1])
981
zeertzjqb811de52021-10-21 10:50:44 +0100982 " 19d: Setting local string global-local (to buffer) option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100983 let g:options = [['tags', 'tagpath1', 'tagpath1', '', 'tagpath2', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +0200984 setlocal tags=tagpath2
985 call assert_equal([], g:options)
986 call assert_equal(g:opt[0], g:opt[1])
987
zeertzjqb811de52021-10-21 10:50:44 +0100988 " 19e: Setting again string global-local (to buffer) option"
989 " Note: v:option_old is the old global value for global-local string options
Bram Moolenaard7c96872019-06-15 17:12:48 +0200990 " but the old local value for all other kinds of options.
991 noa setglobal tags=tag_global " Reset global and local value (without triggering autocmd)
992 noa setlocal tags=tag_local
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100993 let g:options = [['tags', 'tag_global', 'tag_local', 'tag_global', 'tagpath', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +0200994 set tags=tagpath
995 call assert_equal([], g:options)
996 call assert_equal(g:opt[0], g:opt[1])
997
zeertzjqb811de52021-10-21 10:50:44 +0100998 " 19f: Setting string global-local (to buffer) option to an empty string"
999 " Note: v:option_old is the old global value for global-local string options
Bram Moolenaard7c96872019-06-15 17:12:48 +02001000 " but the old local value for all other kinds of options.
1001 noa set tags=tag_global " Reset global and local value (without triggering autocmd)
1002 noa setlocal tags= " empty string
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001003 let g:options = [['tags', 'tag_global', '', 'tag_global', 'tagpath', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001004 set tags=tagpath
1005 call assert_equal([], g:options)
1006 call assert_equal(g:opt[0], g:opt[1])
1007
1008
1009 " 20a: Setting string local (to buffer) option"
1010 let oldval = &spelllang
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001011 let g:options = [['spelllang', oldval, oldval, oldval, 'elvish,klingon', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001012 set spelllang=elvish,klingon
1013 call assert_equal([], g:options)
1014 call assert_equal(g:opt[0], g:opt[1])
1015
1016 " 20b: Resetting string local (to buffer) option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001017 let g:options = [['spelllang', 'elvish,klingon', 'elvish,klingon', 'elvish,klingon', oldval, 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001018 set spelllang&
1019 call assert_equal([], g:options)
1020 call assert_equal(g:opt[0], g:opt[1])
1021
1022 " 20c: Setting global string local (to buffer) option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001023 let g:options = [['spelllang', oldval, '', oldval, 'elvish', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001024 setglobal spelllang=elvish
1025 call assert_equal([], g:options)
1026 call assert_equal(g:opt[0], g:opt[1])
1027
1028 " 20d: Setting local string local (to buffer) option"
1029 noa set spelllang& " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001030 let g:options = [['spelllang', oldval, oldval, '', 'klingon', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001031 setlocal spelllang=klingon
1032 call assert_equal([], g:options)
1033 call assert_equal(g:opt[0], g:opt[1])
1034
1035 " 20e: Setting again string local (to buffer) option"
zeertzjqb811de52021-10-21 10:50:44 +01001036 " Note: v:option_old is the old global value for global-local string options
Bram Moolenaard7c96872019-06-15 17:12:48 +02001037 " but the old local value for all other kinds of options.
1038 noa setglobal spelllang=spellglobal " Reset global and local value (without triggering autocmd)
1039 noa setlocal spelllang=spelllocal
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001040 let g:options = [['spelllang', 'spelllocal', 'spelllocal', 'spellglobal', 'foo', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001041 set spelllang=foo
1042 call assert_equal([], g:options)
1043 call assert_equal(g:opt[0], g:opt[1])
1044
1045
zeertzjqb811de52021-10-21 10:50:44 +01001046 " 21a: Setting string global-local (to window) option"
Bram Moolenaard7c96872019-06-15 17:12:48 +02001047 let oldval = &statusline
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001048 let g:options = [['statusline', oldval, oldval, oldval, 'foo', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001049 set statusline=foo
1050 call assert_equal([], g:options)
1051 call assert_equal(g:opt[0], g:opt[1])
1052
zeertzjqb811de52021-10-21 10:50:44 +01001053 " 21b: Resetting string global-local (to window) option"
1054 " Note: v:option_old is the old global value for global-local string options
Bram Moolenaard7c96872019-06-15 17:12:48 +02001055 " but the old local value for all other kinds of options.
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001056 let g:options = [['statusline', 'foo', 'foo', 'foo', oldval, 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001057 set statusline&
1058 call assert_equal([], g:options)
1059 call assert_equal(g:opt[0], g:opt[1])
1060
zeertzjqb811de52021-10-21 10:50:44 +01001061 " 21c: Setting global string global-local (to window) option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001062 let g:options = [['statusline', oldval, '', oldval, 'bar', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001063 setglobal statusline=bar
1064 call assert_equal([], g:options)
1065 call assert_equal(g:opt[0], g:opt[1])
1066
zeertzjqb811de52021-10-21 10:50:44 +01001067 " 21d: Setting local string global-local (to window) option"
Bram Moolenaard7c96872019-06-15 17:12:48 +02001068 noa set statusline& " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001069 let g:options = [['statusline', oldval, oldval, '', 'baz', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001070 setlocal statusline=baz
1071 call assert_equal([], g:options)
1072 call assert_equal(g:opt[0], g:opt[1])
1073
zeertzjqb811de52021-10-21 10:50:44 +01001074 " 21e: Setting again string global-local (to window) option"
1075 " Note: v:option_old is the old global value for global-local string options
Bram Moolenaard7c96872019-06-15 17:12:48 +02001076 " but the old local value for all other kinds of options.
1077 noa setglobal statusline=bar " Reset global and local value (without triggering autocmd)
1078 noa setlocal statusline=baz
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001079 let g:options = [['statusline', 'bar', 'baz', 'bar', 'foo', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001080 set statusline=foo
1081 call assert_equal([], g:options)
1082 call assert_equal(g:opt[0], g:opt[1])
1083
1084
1085 " 22a: Setting string local (to window) option"
1086 let oldval = &foldignore
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001087 let g:options = [['foldignore', oldval, oldval, oldval, 'fo', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001088 set foldignore=fo
1089 call assert_equal([], g:options)
1090 call assert_equal(g:opt[0], g:opt[1])
1091
1092 " 22b: Resetting string local (to window) option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001093 let g:options = [['foldignore', 'fo', 'fo', 'fo', oldval, 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001094 set foldignore&
1095 call assert_equal([], g:options)
1096 call assert_equal(g:opt[0], g:opt[1])
1097
1098 " 22c: Setting global string local (to window) option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001099 let g:options = [['foldignore', oldval, '', oldval, 'bar', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001100 setglobal foldignore=bar
1101 call assert_equal([], g:options)
1102 call assert_equal(g:opt[0], g:opt[1])
1103
1104 " 22d: Setting local string local (to window) option"
1105 noa set foldignore& " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001106 let g:options = [['foldignore', oldval, oldval, '', 'baz', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001107 setlocal foldignore=baz
1108 call assert_equal([], g:options)
1109 call assert_equal(g:opt[0], g:opt[1])
1110
1111 " 22e: Setting again string local (to window) option"
1112 noa setglobal foldignore=glob " Reset global and local value (without triggering autocmd)
1113 noa setlocal foldignore=loc
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001114 let g:options = [['foldignore', 'loc', 'loc', 'glob', 'fo', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001115 set foldignore=fo
1116 call assert_equal([], g:options)
1117 call assert_equal(g:opt[0], g:opt[1])
1118
1119
zeertzjqb811de52021-10-21 10:50:44 +01001120 " 23a: Setting global number global option"
Bram Moolenaard7c96872019-06-15 17:12:48 +02001121 noa setglobal cmdheight=8 " Reset global and local value (without triggering autocmd)
1122 noa setlocal cmdheight=1 " Sets the global(!) value!
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001123 let g:options = [['cmdheight', '1', '', '1', '2', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001124 setglobal cmdheight=2
1125 call assert_equal([], g:options)
1126 call assert_equal(g:opt[0], g:opt[1])
1127
1128 " 23b: Setting local number global option"
1129 noa setglobal cmdheight=8 " Reset global and local value (without triggering autocmd)
1130 noa setlocal cmdheight=1 " Sets the global(!) value!
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001131 let g:options = [['cmdheight', '1', '1', '', '2', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001132 setlocal cmdheight=2
1133 call assert_equal([], g:options)
1134 call assert_equal(g:opt[0], g:opt[1])
1135
1136 " 23c: Setting again number global option"
1137 noa setglobal cmdheight=8 " Reset global and local value (without triggering autocmd)
1138 noa setlocal cmdheight=1 " Sets the global(!) value!
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001139 let g:options = [['cmdheight', '1', '1', '1', '2', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001140 set cmdheight=2
1141 call assert_equal([], g:options)
1142 call assert_equal(g:opt[0], g:opt[1])
1143
1144 " 23d: Setting again number global option"
1145 noa set cmdheight=8 " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001146 let g:options = [['cmdheight', '8', '8', '8', '2', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001147 set cmdheight=2
1148 call assert_equal([], g:options)
1149 call assert_equal(g:opt[0], g:opt[1])
1150
1151
1152 " 24a: Setting global number global-local (to buffer) option"
1153 noa setglobal undolevels=8 " Reset global and local value (without triggering autocmd)
1154 noa setlocal undolevels=1
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001155 let g:options = [['undolevels', '8', '', '8', '2', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001156 setglobal undolevels=2
1157 call assert_equal([], g:options)
1158 call assert_equal(g:opt[0], g:opt[1])
1159
1160 " 24b: Setting local number global-local (to buffer) option"
1161 noa setglobal undolevels=8 " Reset global and local value (without triggering autocmd)
1162 noa setlocal undolevels=1
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001163 let g:options = [['undolevels', '1', '1', '', '2', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001164 setlocal undolevels=2
1165 call assert_equal([], g:options)
1166 call assert_equal(g:opt[0], g:opt[1])
1167
1168 " 24c: Setting again number global-local (to buffer) option"
1169 noa setglobal undolevels=8 " Reset global and local value (without triggering autocmd)
1170 noa setlocal undolevels=1
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001171 let g:options = [['undolevels', '1', '1', '8', '2', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001172 set undolevels=2
1173 call assert_equal([], g:options)
1174 call assert_equal(g:opt[0], g:opt[1])
1175
1176 " 24d: Setting again global number global-local (to buffer) option"
1177 noa set undolevels=8 " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001178 let g:options = [['undolevels', '8', '8', '8', '2', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001179 set undolevels=2
1180 call assert_equal([], g:options)
1181 call assert_equal(g:opt[0], g:opt[1])
1182
1183
1184 " 25a: Setting global number local (to buffer) option"
1185 noa setglobal wrapmargin=8 " Reset global and local value (without triggering autocmd)
1186 noa setlocal wrapmargin=1
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001187 let g:options = [['wrapmargin', '8', '', '8', '2', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001188 setglobal wrapmargin=2
1189 call assert_equal([], g:options)
1190 call assert_equal(g:opt[0], g:opt[1])
1191
1192 " 25b: Setting local number local (to buffer) option"
1193 noa setglobal wrapmargin=8 " Reset global and local value (without triggering autocmd)
1194 noa setlocal wrapmargin=1
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001195 let g:options = [['wrapmargin', '1', '1', '', '2', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001196 setlocal wrapmargin=2
1197 call assert_equal([], g:options)
1198 call assert_equal(g:opt[0], g:opt[1])
1199
1200 " 25c: Setting again number local (to buffer) option"
1201 noa setglobal wrapmargin=8 " Reset global and local value (without triggering autocmd)
1202 noa setlocal wrapmargin=1
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001203 let g:options = [['wrapmargin', '1', '1', '8', '2', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001204 set wrapmargin=2
1205 call assert_equal([], g:options)
1206 call assert_equal(g:opt[0], g:opt[1])
1207
1208 " 25d: Setting again global number local (to buffer) option"
1209 noa set wrapmargin=8 " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001210 let g:options = [['wrapmargin', '8', '8', '8', '2', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001211 set wrapmargin=2
1212 call assert_equal([], g:options)
1213 call assert_equal(g:opt[0], g:opt[1])
1214
1215
1216 " 26: Setting number global-local (to window) option.
1217 " Such option does currently not exist.
1218
1219
1220 " 27a: Setting global number local (to window) option"
1221 noa setglobal foldcolumn=8 " Reset global and local value (without triggering autocmd)
1222 noa setlocal foldcolumn=1
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001223 let g:options = [['foldcolumn', '8', '', '8', '2', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001224 setglobal foldcolumn=2
1225 call assert_equal([], g:options)
1226 call assert_equal(g:opt[0], g:opt[1])
1227
1228 " 27b: Setting local number local (to window) option"
1229 noa setglobal foldcolumn=8 " Reset global and local value (without triggering autocmd)
1230 noa setlocal foldcolumn=1
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001231 let g:options = [['foldcolumn', '1', '1', '', '2', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001232 setlocal foldcolumn=2
1233 call assert_equal([], g:options)
1234 call assert_equal(g:opt[0], g:opt[1])
1235
1236 " 27c: Setting again number local (to window) option"
1237 noa setglobal foldcolumn=8 " Reset global and local value (without triggering autocmd)
1238 noa setlocal foldcolumn=1
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001239 let g:options = [['foldcolumn', '1', '1', '8', '2', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001240 set foldcolumn=2
1241 call assert_equal([], g:options)
1242 call assert_equal(g:opt[0], g:opt[1])
1243
zeertzjqb811de52021-10-21 10:50:44 +01001244 " 27d: Setting again global number local (to window) option"
Bram Moolenaard7c96872019-06-15 17:12:48 +02001245 noa set foldcolumn=8 " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001246 let g:options = [['foldcolumn', '8', '8', '8', '2', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001247 set foldcolumn=2
1248 call assert_equal([], g:options)
1249 call assert_equal(g:opt[0], g:opt[1])
1250
1251
1252 " 28a: Setting global boolean global option"
1253 noa setglobal nowrapscan " Reset global and local value (without triggering autocmd)
1254 noa setlocal wrapscan " Sets the global(!) value!
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001255 let g:options = [['wrapscan', '1', '', '1', '0', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001256 setglobal nowrapscan
1257 call assert_equal([], g:options)
1258 call assert_equal(g:opt[0], g:opt[1])
1259
1260 " 28b: Setting local boolean global option"
1261 noa setglobal nowrapscan " Reset global and local value (without triggering autocmd)
1262 noa setlocal wrapscan " Sets the global(!) value!
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001263 let g:options = [['wrapscan', '1', '1', '', '0', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001264 setlocal nowrapscan
1265 call assert_equal([], g:options)
1266 call assert_equal(g:opt[0], g:opt[1])
1267
1268 " 28c: Setting again boolean global option"
1269 noa setglobal nowrapscan " Reset global and local value (without triggering autocmd)
1270 noa setlocal wrapscan " Sets the global(!) value!
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001271 let g:options = [['wrapscan', '1', '1', '1', '0', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001272 set nowrapscan
1273 call assert_equal([], g:options)
1274 call assert_equal(g:opt[0], g:opt[1])
1275
1276 " 28d: Setting again global boolean global option"
1277 noa set nowrapscan " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001278 let g:options = [['wrapscan', '0', '0', '0', '1', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001279 set wrapscan
1280 call assert_equal([], g:options)
1281 call assert_equal(g:opt[0], g:opt[1])
1282
1283
1284 " 29a: Setting global boolean global-local (to buffer) option"
1285 noa setglobal noautoread " Reset global and local value (without triggering autocmd)
1286 noa setlocal autoread
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001287 let g:options = [['autoread', '0', '', '0', '1', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001288 setglobal autoread
1289 call assert_equal([], g:options)
1290 call assert_equal(g:opt[0], g:opt[1])
1291
1292 " 29b: Setting local boolean global-local (to buffer) option"
1293 noa setglobal noautoread " Reset global and local value (without triggering autocmd)
1294 noa setlocal autoread
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001295 let g:options = [['autoread', '1', '1', '', '0', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001296 setlocal noautoread
1297 call assert_equal([], g:options)
1298 call assert_equal(g:opt[0], g:opt[1])
1299
1300 " 29c: Setting again boolean global-local (to buffer) option"
1301 noa setglobal noautoread " Reset global and local value (without triggering autocmd)
1302 noa setlocal autoread
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001303 let g:options = [['autoread', '1', '1', '0', '1', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001304 set autoread
1305 call assert_equal([], g:options)
1306 call assert_equal(g:opt[0], g:opt[1])
1307
1308 " 29d: Setting again global boolean global-local (to buffer) option"
1309 noa set noautoread " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001310 let g:options = [['autoread', '0', '0', '0', '1', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001311 set autoread
1312 call assert_equal([], g:options)
1313 call assert_equal(g:opt[0], g:opt[1])
1314
1315
1316 " 30a: Setting global boolean local (to buffer) option"
1317 noa setglobal nocindent " Reset global and local value (without triggering autocmd)
1318 noa setlocal cindent
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001319 let g:options = [['cindent', '0', '', '0', '1', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001320 setglobal cindent
1321 call assert_equal([], g:options)
1322 call assert_equal(g:opt[0], g:opt[1])
1323
1324 " 30b: Setting local boolean local (to buffer) option"
1325 noa setglobal nocindent " Reset global and local value (without triggering autocmd)
1326 noa setlocal cindent
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001327 let g:options = [['cindent', '1', '1', '', '0', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001328 setlocal nocindent
1329 call assert_equal([], g:options)
1330 call assert_equal(g:opt[0], g:opt[1])
1331
1332 " 30c: Setting again boolean local (to buffer) option"
1333 noa setglobal nocindent " Reset global and local value (without triggering autocmd)
1334 noa setlocal cindent
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001335 let g:options = [['cindent', '1', '1', '0', '1', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001336 set cindent
1337 call assert_equal([], g:options)
1338 call assert_equal(g:opt[0], g:opt[1])
1339
1340 " 30d: Setting again global boolean local (to buffer) option"
1341 noa set nocindent " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001342 let g:options = [['cindent', '0', '0', '0', '1', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001343 set cindent
1344 call assert_equal([], g:options)
1345 call assert_equal(g:opt[0], g:opt[1])
1346
1347
1348 " 31: Setting boolean global-local (to window) option
1349 " Currently no such option exists.
1350
1351
1352 " 32a: Setting global boolean local (to window) option"
1353 noa setglobal nocursorcolumn " Reset global and local value (without triggering autocmd)
1354 noa setlocal cursorcolumn
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001355 let g:options = [['cursorcolumn', '0', '', '0', '1', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001356 setglobal cursorcolumn
1357 call assert_equal([], g:options)
1358 call assert_equal(g:opt[0], g:opt[1])
1359
1360 " 32b: Setting local boolean local (to window) option"
1361 noa setglobal nocursorcolumn " Reset global and local value (without triggering autocmd)
1362 noa setlocal cursorcolumn
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001363 let g:options = [['cursorcolumn', '1', '1', '', '0', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001364 setlocal nocursorcolumn
1365 call assert_equal([], g:options)
1366 call assert_equal(g:opt[0], g:opt[1])
1367
1368 " 32c: Setting again boolean local (to window) option"
1369 noa setglobal nocursorcolumn " Reset global and local value (without triggering autocmd)
1370 noa setlocal cursorcolumn
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001371 let g:options = [['cursorcolumn', '1', '1', '0', '1', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001372 set cursorcolumn
1373 call assert_equal([], g:options)
1374 call assert_equal(g:opt[0], g:opt[1])
1375
1376 " 32d: Setting again global boolean local (to window) option"
1377 noa set nocursorcolumn " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001378 let g:options = [['cursorcolumn', '0', '0', '0', '1', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001379 set cursorcolumn
1380 call assert_equal([], g:options)
1381 call assert_equal(g:opt[0], g:opt[1])
1382
1383
Bram Moolenaar1bc353b2019-09-01 14:45:28 +02001384 " 33: Test autocommands when an option value is converted internally.
Bram Moolenaard7c96872019-06-15 17:12:48 +02001385 noa set backspace=1 " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001386 let g:options = [['backspace', 'indent,eol', 'indent,eol', 'indent,eol', '2', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001387 set backspace=2
1388 call assert_equal([], g:options)
1389 call assert_equal(g:opt[0], g:opt[1])
1390
1391
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001392 " Cleanup
1393 au! OptionSet
Bram Moolenaar0331faf2019-06-15 18:40:37 +02001394 " set tags&
Bram Moolenaard7c96872019-06-15 17:12:48 +02001395 for opt in ['nu', 'ai', 'acd', 'ar', 'bs', 'backup', 'cul', 'cp', 'backupext', 'tags', 'spelllang', 'statusline', 'foldignore', 'cmdheight', 'undolevels', 'wrapmargin', 'foldcolumn', 'wrapscan', 'autoread', 'cindent', 'cursorcolumn']
Bram Moolenaar91d2e782018-08-07 19:05:01 +02001396 exe printf(":set %s&vim", opt)
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001397 endfor
1398 call test_override('starting', 0)
1399 delfunc! AutoCommandOptionSet
1400endfunc
1401
1402func Test_OptionSet_diffmode()
1403 call test_override('starting', 1)
Bram Moolenaar26d98212019-01-27 22:32:55 +01001404 " 18: Changing an option when entering diff mode
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001405 new
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001406 au OptionSet diff :let &l:cul = v:option_new
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001407
1408 call setline(1, ['buffer 1', 'line2', 'line3', 'line4'])
1409 call assert_equal(0, &l:cul)
1410 diffthis
1411 call assert_equal(1, &l:cul)
1412
1413 vnew
1414 call setline(1, ['buffer 2', 'line 2', 'line 3', 'line4'])
1415 call assert_equal(0, &l:cul)
1416 diffthis
1417 call assert_equal(1, &l:cul)
1418
1419 diffoff
1420 call assert_equal(0, &l:cul)
1421 call assert_equal(1, getwinvar(2, '&l:cul'))
1422 bw!
1423
1424 call assert_equal(1, &l:cul)
1425 diffoff!
1426 call assert_equal(0, &l:cul)
1427 call assert_equal(0, getwinvar(1, '&l:cul'))
1428 bw!
1429
1430 " Cleanup
1431 au! OptionSet
1432 call test_override('starting', 0)
1433endfunc
1434
1435func Test_OptionSet_diffmode_close()
1436 call test_override('starting', 1)
1437 " 19: Try to close the current window when entering diff mode
1438 " should not segfault
1439 new
1440 au OptionSet diff close
1441
1442 call setline(1, ['buffer 1', 'line2', 'line3', 'line4'])
Bram Moolenaare2e40752020-09-04 21:18:46 +02001443 call assert_fails(':diffthis', 'E788:')
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001444 call assert_equal(1, &diff)
1445 vnew
1446 call setline(1, ['buffer 2', 'line 2', 'line 3', 'line4'])
Bram Moolenaare2e40752020-09-04 21:18:46 +02001447 call assert_fails(':diffthis', 'E788:')
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001448 call assert_equal(1, &diff)
Bram Moolenaara9aa86f2019-11-10 21:25:45 +01001449 set diffopt-=closeoff
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001450 bw!
Bram Moolenaare2e40752020-09-04 21:18:46 +02001451 call assert_fails(':diffoff!', 'E788:')
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001452 bw!
1453
1454 " Cleanup
1455 au! OptionSet
1456 call test_override('starting', 0)
1457 "delfunc! AutoCommandOptionSet
1458endfunc
Bram Moolenaar4a137b42017-08-04 22:37:11 +02001459
1460" Test for Bufleave autocommand that deletes the buffer we are about to edit.
1461func Test_BufleaveWithDelete()
1462 new | edit Xfile1
1463
1464 augroup test_bufleavewithdelete
1465 autocmd!
1466 autocmd BufLeave Xfile1 bwipe Xfile2
1467 augroup END
1468
1469 call assert_fails('edit Xfile2', 'E143:')
1470 call assert_equal('Xfile1', bufname('%'))
1471
1472 autocmd! test_bufleavewithdelete BufLeave Xfile1
1473 augroup! test_bufleavewithdelete
1474
1475 new
1476 bwipe! Xfile1
1477endfunc
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +02001478
1479" Test for autocommand that changes the buffer list, when doing ":ball".
1480func Test_Acmd_BufAll()
1481 enew!
1482 %bwipe!
1483 call writefile(['Test file Xxx1'], 'Xxx1')
1484 call writefile(['Test file Xxx2'], 'Xxx2')
1485 call writefile(['Test file Xxx3'], 'Xxx3')
1486
1487 " Add three files to the buffer list
1488 split Xxx1
1489 close
1490 split Xxx2
1491 close
1492 split Xxx3
1493 close
1494
1495 " Wipe the buffer when the buffer is opened
1496 au BufReadPost Xxx2 bwipe
1497
1498 call append(0, 'Test file Xxx4')
1499 ball
1500
1501 call assert_equal(2, winnr('$'))
1502 call assert_equal('Xxx1', bufname(winbufnr(winnr('$'))))
1503 wincmd t
1504
1505 au! BufReadPost
1506 %bwipe!
1507 call delete('Xxx1')
1508 call delete('Xxx2')
1509 call delete('Xxx3')
1510 enew! | only
1511endfunc
1512
1513" Test for autocommand that changes current buffer on BufEnter event.
1514" Check if modelines are interpreted for the correct buffer.
1515func Test_Acmd_BufEnter()
1516 %bwipe!
1517 call writefile(['start of test file Xxx1',
1518 \ "\<Tab>this is a test",
1519 \ 'end of test file Xxx1'], 'Xxx1')
1520 call writefile(['start of test file Xxx2',
1521 \ 'vim: set noai :',
1522 \ "\<Tab>this is a test",
1523 \ 'end of test file Xxx2'], 'Xxx2')
1524
1525 au BufEnter Xxx2 brew
1526 set ai modeline modelines=3
1527 edit Xxx1
1528 " edit Xxx2, autocmd will do :brew
1529 edit Xxx2
1530 exe "normal G?this is a\<CR>"
1531 " Append text with autoindent to this file
1532 normal othis should be auto-indented
1533 call assert_equal("\<Tab>this should be auto-indented", getline('.'))
1534 call assert_equal(3, line('.'))
1535 " Remove autocmd and edit Xxx2 again
1536 au! BufEnter Xxx2
1537 buf! Xxx2
1538 exe "normal G?this is a\<CR>"
1539 " append text without autoindent to Xxx
1540 normal othis should be in column 1
1541 call assert_equal("this should be in column 1", getline('.'))
1542 call assert_equal(4, line('.'))
1543
1544 %bwipe!
1545 call delete('Xxx1')
1546 call delete('Xxx2')
1547 set ai&vim modeline&vim modelines&vim
1548endfunc
1549
1550" Test for issue #57
1551" do not move cursor on <c-o> when autoindent is set
1552func Test_ai_CTRL_O()
1553 enew!
1554 set ai
1555 let save_fo = &fo
1556 set fo+=r
1557 exe "normal o# abcdef\<Esc>2hi\<CR>\<C-O>d0\<Esc>"
1558 exe "normal o# abcdef\<Esc>2hi\<C-O>d0\<Esc>"
1559 call assert_equal(['# abc', 'def', 'def'], getline(2, 4))
1560
1561 set ai&vim
1562 let &fo = save_fo
1563 enew!
1564endfunc
1565
1566" Test for autocommand that deletes the current buffer on BufLeave event.
1567" Also test deleting the last buffer, should give a new, empty buffer.
1568func Test_BufLeave_Wipe()
1569 %bwipe!
1570 let content = ['start of test file Xxx',
1571 \ 'this is a test',
1572 \ 'end of test file Xxx']
1573 call writefile(content, 'Xxx1')
1574 call writefile(content, 'Xxx2')
1575
1576 au BufLeave Xxx2 bwipe
1577 edit Xxx1
1578 split Xxx2
1579 " delete buffer Xxx2, we should be back to Xxx1
1580 bwipe
1581 call assert_equal('Xxx1', bufname('%'))
1582 call assert_equal(1, winnr('$'))
1583
1584 " Create an alternate buffer
1585 %write! test.out
1586 call assert_equal('test.out', bufname('#'))
1587 " delete alternate buffer
1588 bwipe test.out
1589 call assert_equal('Xxx1', bufname('%'))
1590 call assert_equal('', bufname('#'))
1591
1592 au BufLeave Xxx1 bwipe
1593 " delete current buffer, get an empty one
1594 bwipe!
1595 call assert_equal(1, line('$'))
1596 call assert_equal('', bufname('%'))
Bram Moolenaarb2c87502017-10-14 21:15:58 +02001597 let g:bufinfo = getbufinfo()
1598 call assert_equal(1, len(g:bufinfo))
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +02001599
1600 call delete('Xxx1')
1601 call delete('Xxx2')
Bram Moolenaar53f0c962017-10-22 14:23:59 +02001602 call delete('test.out')
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +02001603 %bwipe
1604 au! BufLeave
Bram Moolenaarb2c87502017-10-14 21:15:58 +02001605
1606 " check that bufinfo doesn't contain a pointer to freed memory
1607 call test_garbagecollect_now()
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +02001608endfunc
Bram Moolenaar87ffb5c2017-10-19 12:37:42 +02001609
1610func Test_QuitPre()
1611 edit Xfoo
1612 let winid = win_getid(winnr())
1613 split Xbar
1614 au! QuitPre * let g:afile = expand('<afile>')
1615 " Close the other window, <afile> should be correct.
1616 exe win_id2win(winid) . 'q'
1617 call assert_equal('Xfoo', g:afile)
LemonBoy66e13ae2022-04-21 22:52:11 +01001618
Bram Moolenaar87ffb5c2017-10-19 12:37:42 +02001619 unlet g:afile
1620 bwipe Xfoo
1621 bwipe Xbar
1622endfunc
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02001623
1624func Test_Cmdline()
Bram Moolenaar153b7042018-01-31 15:48:32 +01001625 au! CmdlineChanged : let g:text = getcmdline()
1626 let g:text = 0
1627 call feedkeys(":echom 'hello'\<CR>", 'xt')
1628 call assert_equal("echom 'hello'", g:text)
1629 au! CmdlineChanged
1630
1631 au! CmdlineChanged : let g:entered = expand('<afile>')
1632 let g:entered = 0
1633 call feedkeys(":echom 'hello'\<CR>", 'xt')
1634 call assert_equal(':', g:entered)
1635 au! CmdlineChanged
1636
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02001637 au! CmdlineEnter : let g:entered = expand('<afile>')
1638 au! CmdlineLeave : let g:left = expand('<afile>')
1639 let g:entered = 0
1640 let g:left = 0
1641 call feedkeys(":echo 'hello'\<CR>", 'xt')
1642 call assert_equal(':', g:entered)
1643 call assert_equal(':', g:left)
1644 au! CmdlineEnter
1645 au! CmdlineLeave
1646
Bram Moolenaara4baf5b2018-04-22 13:27:44 +02001647 let save_shellslash = &shellslash
1648 set noshellslash
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02001649 au! CmdlineEnter / let g:entered = expand('<afile>')
1650 au! CmdlineLeave / let g:left = expand('<afile>')
1651 let g:entered = 0
1652 let g:left = 0
Bram Moolenaar53f0c962017-10-22 14:23:59 +02001653 new
1654 call setline(1, 'hello')
1655 call feedkeys("/hello\<CR>", 'xt')
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02001656 call assert_equal('/', g:entered)
1657 call assert_equal('/', g:left)
Bram Moolenaar53f0c962017-10-22 14:23:59 +02001658 bwipe!
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02001659 au! CmdlineEnter
1660 au! CmdlineLeave
Bram Moolenaara4baf5b2018-04-22 13:27:44 +02001661 let &shellslash = save_shellslash
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02001662endfunc
Bram Moolenaar53f0c962017-10-22 14:23:59 +02001663
1664" Test for BufWritePre autocommand that deletes or unloads the buffer.
1665func Test_BufWritePre()
1666 %bwipe
1667 au BufWritePre Xxx1 bunload
1668 au BufWritePre Xxx2 bwipe
1669
1670 call writefile(['start of Xxx1', 'test', 'end of Xxx1'], 'Xxx1')
1671 call writefile(['start of Xxx2', 'test', 'end of Xxx2'], 'Xxx2')
1672
1673 edit Xtest
1674 e! Xxx2
1675 bdel Xtest
1676 e Xxx1
1677 " write it, will unload it and give an error msg
Bram Moolenaare2e40752020-09-04 21:18:46 +02001678 call assert_fails('w', 'E203:')
Bram Moolenaar53f0c962017-10-22 14:23:59 +02001679 call assert_equal('Xxx2', bufname('%'))
1680 edit Xtest
1681 e! Xxx2
1682 bwipe Xtest
1683 " write it, will delete the buffer and give an error msg
Bram Moolenaare2e40752020-09-04 21:18:46 +02001684 call assert_fails('w', 'E203:')
Bram Moolenaar53f0c962017-10-22 14:23:59 +02001685 call assert_equal('Xxx1', bufname('%'))
1686 au! BufWritePre
1687 call delete('Xxx1')
1688 call delete('Xxx2')
1689endfunc
1690
1691" Test for BufUnload autocommand that unloads all the other buffers
1692func Test_bufunload_all()
Bram Moolenaarf08b0eb2021-10-16 13:00:14 +01001693 let g:test_is_flaky = 1
Bram Moolenaar53f0c962017-10-22 14:23:59 +02001694 call writefile(['Test file Xxx1'], 'Xxx1')"
1695 call writefile(['Test file Xxx2'], 'Xxx2')"
1696
Bram Moolenaarc79745a2019-05-20 22:12:34 +02001697 let content =<< trim [CODE]
1698 func UnloadAllBufs()
1699 let i = 1
1700 while i <= bufnr('$')
1701 if i != bufnr('%') && bufloaded(i)
1702 exe i . 'bunload'
1703 endif
1704 let i += 1
1705 endwhile
1706 endfunc
1707 au BufUnload * call UnloadAllBufs()
1708 au VimLeave * call writefile(['Test Finished'], 'Xout')
1709 edit Xxx1
1710 split Xxx2
1711 q
1712 [CODE]
1713
Bram Moolenaar53f0c962017-10-22 14:23:59 +02001714 call writefile(content, 'Xtest')
1715
1716 call delete('Xout')
Bram Moolenaar93344c22019-08-14 21:12:05 +02001717 call system(GetVimCommandClean() .. ' -N --not-a-term -S Xtest')
Bram Moolenaar53f0c962017-10-22 14:23:59 +02001718 call assert_true(filereadable('Xout'))
1719
1720 call delete('Xxx1')
1721 call delete('Xxx2')
1722 call delete('Xtest')
1723 call delete('Xout')
1724endfunc
1725
1726" Some tests for buffer-local autocommands
1727func Test_buflocal_autocmd()
1728 let g:bname = ''
1729 edit xx
1730 au BufLeave <buffer> let g:bname = expand("%")
1731 " here, autocommand for xx should trigger.
1732 " but autocommand shall not apply to buffer named <buffer>.
1733 edit somefile
1734 call assert_equal('xx', g:bname)
1735 let g:bname = ''
1736 " here, autocommand shall be auto-deleted
1737 bwipe xx
1738 " autocmd should not trigger
1739 edit xx
1740 call assert_equal('', g:bname)
1741 " autocmd should not trigger
1742 edit somefile
1743 call assert_equal('', g:bname)
1744 enew
1745 unlet g:bname
1746endfunc
Bram Moolenaar430dc5d2017-11-02 21:04:47 +01001747
1748" Test for "*Cmd" autocommands
1749func Test_Cmd_Autocmds()
1750 call writefile(['start of Xxx', "\tabc2", 'end of Xxx'], 'Xxx')
1751
1752 enew!
1753 au BufReadCmd XtestA 0r Xxx|$del
1754 edit XtestA " will read text of Xxd instead
1755 call assert_equal('start of Xxx', getline(1))
1756
1757 au BufWriteCmd XtestA call append(line("$"), "write")
1758 write " will append a line to the file
1759 call assert_equal('write', getline('$'))
Bram Moolenaare2e40752020-09-04 21:18:46 +02001760 call assert_fails('read XtestA', 'E484:') " should not read anything
Bram Moolenaar430dc5d2017-11-02 21:04:47 +01001761 call assert_equal('write', getline(4))
1762
1763 " now we have:
1764 " 1 start of Xxx
1765 " 2 abc2
1766 " 3 end of Xxx
1767 " 4 write
1768
1769 au FileReadCmd XtestB '[r Xxx
1770 2r XtestB " will read Xxx below line 2 instead
1771 call assert_equal('start of Xxx', getline(3))
1772
1773 " now we have:
1774 " 1 start of Xxx
1775 " 2 abc2
1776 " 3 start of Xxx
1777 " 4 abc2
1778 " 5 end of Xxx
1779 " 6 end of Xxx
1780 " 7 write
1781
1782 au FileWriteCmd XtestC '[,']copy $
1783 normal 4GA1
1784 4,5w XtestC " will copy lines 4 and 5 to the end
1785 call assert_equal("\tabc21", getline(8))
Bram Moolenaare2e40752020-09-04 21:18:46 +02001786 call assert_fails('r XtestC', 'E484:') " should not read anything
Bram Moolenaar430dc5d2017-11-02 21:04:47 +01001787 call assert_equal("end of Xxx", getline(9))
1788
1789 " now we have:
1790 " 1 start of Xxx
1791 " 2 abc2
1792 " 3 start of Xxx
1793 " 4 abc21
1794 " 5 end of Xxx
1795 " 6 end of Xxx
1796 " 7 write
1797 " 8 abc21
1798 " 9 end of Xxx
1799
1800 let g:lines = []
1801 au FileAppendCmd XtestD call extend(g:lines, getline(line("'["), line("']")))
1802 w >>XtestD " will add lines to 'lines'
1803 call assert_equal(9, len(g:lines))
Bram Moolenaare2e40752020-09-04 21:18:46 +02001804 call assert_fails('$r XtestD', 'E484:') " should not read anything
Bram Moolenaar430dc5d2017-11-02 21:04:47 +01001805 call assert_equal(9, line('$'))
1806 call assert_equal('end of Xxx', getline('$'))
1807
1808 au BufReadCmd XtestE 0r Xxx|$del
1809 sp XtestE " split window with test.out
1810 call assert_equal('end of Xxx', getline(3))
1811
1812 let g:lines = []
1813 exe "normal 2Goasdf\<Esc>\<C-W>\<C-W>"
1814 au BufWriteCmd XtestE call extend(g:lines, getline(0, '$'))
1815 wall " will write other window to 'lines'
1816 call assert_equal(4, len(g:lines), g:lines)
1817 call assert_equal('asdf', g:lines[2])
1818
1819 au! BufReadCmd
1820 au! BufWriteCmd
1821 au! FileReadCmd
1822 au! FileWriteCmd
1823 au! FileAppendCmd
1824 %bwipe!
1825 call delete('Xxx')
1826 enew!
1827endfunc
Bram Moolenaaraace2152017-11-05 16:23:10 +01001828
Bram Moolenaar0fff4412020-03-29 16:06:29 +02001829func s:ReadFile()
1830 setl noswapfile nomodified
1831 let filename = resolve(expand("<afile>:p"))
1832 execute 'read' fnameescape(filename)
1833 1d_
1834 exe 'file' fnameescape(filename)
1835 setl buftype=acwrite
1836endfunc
1837
1838func s:WriteFile()
1839 let filename = resolve(expand("<afile>:p"))
1840 setl buftype=
1841 noautocmd execute 'write' fnameescape(filename)
1842 setl buftype=acwrite
1843 setl nomodified
1844endfunc
1845
1846func Test_BufReadCmd()
1847 autocmd BufReadCmd *.test call s:ReadFile()
1848 autocmd BufWriteCmd *.test call s:WriteFile()
1849
1850 call writefile(['one', 'two', 'three'], 'Xcmd.test')
1851 edit Xcmd.test
1852 call assert_match('Xcmd.test" line 1 of 3', execute('file'))
1853 normal! Gofour
1854 write
1855 call assert_equal(['one', 'two', 'three', 'four'], readfile('Xcmd.test'))
1856
1857 bwipe!
1858 call delete('Xcmd.test')
1859 au! BufReadCmd
1860 au! BufWriteCmd
1861endfunc
1862
Bram Moolenaaraace2152017-11-05 16:23:10 +01001863func SetChangeMarks(start, end)
Bram Moolenaar97c69432021-01-15 16:45:21 +01001864 exe a:start .. 'mark ['
1865 exe a:end .. 'mark ]'
Bram Moolenaaraace2152017-11-05 16:23:10 +01001866endfunc
1867
1868" Verify the effects of autocmds on '[ and ']
1869func Test_change_mark_in_autocmds()
1870 edit! Xtest
Bram Moolenaar97c69432021-01-15 16:45:21 +01001871 call feedkeys("ia\<CR>b\<CR>c\<CR>d\<C-g>u\<Esc>", 'xtn')
Bram Moolenaaraace2152017-11-05 16:23:10 +01001872
1873 call SetChangeMarks(2, 3)
1874 write
1875 call assert_equal([1, 4], [line("'["), line("']")])
1876
1877 call SetChangeMarks(2, 3)
1878 au BufWritePre * call assert_equal([1, 4], [line("'["), line("']")])
1879 write
1880 au! BufWritePre
1881
Bram Moolenaar14ddd222020-08-05 12:02:40 +02001882 if has('unix')
Bram Moolenaaraace2152017-11-05 16:23:10 +01001883 write XtestFilter
1884 write >> XtestFilter
1885
1886 call SetChangeMarks(2, 3)
1887 " Marks are set to the entire range of the write
1888 au FilterWritePre * call assert_equal([1, 4], [line("'["), line("']")])
1889 " '[ is adjusted to just before the line that will receive the filtered
1890 " data
1891 au FilterReadPre * call assert_equal([4, 4], [line("'["), line("']")])
1892 " The filtered data is read into the buffer, and the source lines are
1893 " still present, so the range is after the source lines
1894 au FilterReadPost * call assert_equal([5, 12], [line("'["), line("']")])
1895 %!cat XtestFilter
1896 " After the filtered data is read, the original lines are deleted
1897 call assert_equal([1, 8], [line("'["), line("']")])
1898 au! FilterWritePre,FilterReadPre,FilterReadPost
1899 undo
1900
1901 call SetChangeMarks(1, 4)
1902 au FilterWritePre * call assert_equal([2, 3], [line("'["), line("']")])
1903 au FilterReadPre * call assert_equal([3, 3], [line("'["), line("']")])
1904 au FilterReadPost * call assert_equal([4, 11], [line("'["), line("']")])
1905 2,3!cat XtestFilter
1906 call assert_equal([2, 9], [line("'["), line("']")])
1907 au! FilterWritePre,FilterReadPre,FilterReadPost
1908 undo
1909
1910 call delete('XtestFilter')
1911 endif
1912
1913 call SetChangeMarks(1, 4)
1914 au FileWritePre * call assert_equal([2, 3], [line("'["), line("']")])
1915 2,3write Xtest2
1916 au! FileWritePre
1917
1918 call SetChangeMarks(2, 3)
1919 au FileAppendPre * call assert_equal([1, 4], [line("'["), line("']")])
1920 write >> Xtest2
1921 au! FileAppendPre
1922
1923 call SetChangeMarks(1, 4)
1924 au FileAppendPre * call assert_equal([2, 3], [line("'["), line("']")])
1925 2,3write >> Xtest2
1926 au! FileAppendPre
1927
1928 call SetChangeMarks(1, 1)
1929 au FileReadPre * call assert_equal([3, 1], [line("'["), line("']")])
1930 au FileReadPost * call assert_equal([4, 11], [line("'["), line("']")])
1931 3read Xtest2
1932 au! FileReadPre,FileReadPost
1933 undo
1934
1935 call SetChangeMarks(4, 4)
1936 " When the line is 0, it's adjusted to 1
1937 au FileReadPre * call assert_equal([1, 4], [line("'["), line("']")])
1938 au FileReadPost * call assert_equal([1, 8], [line("'["), line("']")])
1939 0read Xtest2
1940 au! FileReadPre,FileReadPost
1941 undo
1942
1943 call SetChangeMarks(4, 4)
1944 " When the line is 0, it's adjusted to 1
1945 au FileReadPre * call assert_equal([1, 4], [line("'["), line("']")])
1946 au FileReadPost * call assert_equal([2, 9], [line("'["), line("']")])
1947 1read Xtest2
1948 au! FileReadPre,FileReadPost
1949 undo
1950
1951 bwipe!
1952 call delete('Xtest')
1953 call delete('Xtest2')
1954endfunc
1955
1956func Test_Filter_noshelltemp()
Bram Moolenaaraeb313f2020-11-27 19:13:28 +01001957 CheckExecutable cat
Bram Moolenaaraace2152017-11-05 16:23:10 +01001958
1959 enew!
1960 call setline(1, ['a', 'b', 'c', 'd'])
1961
1962 let shelltemp = &shelltemp
1963 set shelltemp
1964
1965 let g:filter_au = 0
1966 au FilterWritePre * let g:filter_au += 1
1967 au FilterReadPre * let g:filter_au += 1
1968 au FilterReadPost * let g:filter_au += 1
1969 %!cat
1970 call assert_equal(3, g:filter_au)
1971
1972 if has('filterpipe')
1973 set noshelltemp
1974
1975 let g:filter_au = 0
1976 au FilterWritePre * let g:filter_au += 1
1977 au FilterReadPre * let g:filter_au += 1
1978 au FilterReadPost * let g:filter_au += 1
1979 %!cat
1980 call assert_equal(0, g:filter_au)
1981 endif
1982
1983 au! FilterWritePre,FilterReadPre,FilterReadPost
1984 let &shelltemp = shelltemp
1985 bwipe!
1986endfunc
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01001987
1988func Test_TextYankPost()
1989 enew!
1990 call setline(1, ['foo'])
1991
1992 let g:event = []
1993 au TextYankPost * let g:event = copy(v:event)
1994
1995 call assert_equal({}, v:event)
1996 call assert_fails('let v:event = {}', 'E46:')
1997 call assert_fails('let v:event.mykey = 0', 'E742:')
1998
1999 norm "ayiw
2000 call assert_equal(
Bram Moolenaara016eeb2022-04-09 11:37:38 +01002001 \ #{regcontents: ['foo'], regname: 'a', operator: 'y',
2002 \ regtype: 'v', visual: v:false, inclusive: v:true},
2003 \ g:event)
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01002004 norm y_
2005 call assert_equal(
Bram Moolenaara016eeb2022-04-09 11:37:38 +01002006 \ #{regcontents: ['foo'], regname: '', operator: 'y', regtype: 'V',
2007 \ visual: v:false, inclusive: v:false},
2008 \ g:event)
Bram Moolenaar37d16732020-06-12 22:09:01 +02002009 norm Vy
2010 call assert_equal(
Bram Moolenaara016eeb2022-04-09 11:37:38 +01002011 \ #{regcontents: ['foo'], regname: '', operator: 'y', regtype: 'V',
2012 \ visual: v:true, inclusive: v:true},
2013 \ g:event)
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01002014 call feedkeys("\<C-V>y", 'x')
2015 call assert_equal(
Bram Moolenaara016eeb2022-04-09 11:37:38 +01002016 \ #{regcontents: ['f'], regname: '', operator: 'y', regtype: "\x161",
2017 \ visual: v:true, inclusive: v:true},
2018 \ g:event)
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01002019 norm "xciwbar
2020 call assert_equal(
Bram Moolenaara016eeb2022-04-09 11:37:38 +01002021 \ #{regcontents: ['foo'], regname: 'x', operator: 'c', regtype: 'v',
2022 \ visual: v:false, inclusive: v:true},
2023 \ g:event)
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01002024 norm "bdiw
2025 call assert_equal(
Bram Moolenaara016eeb2022-04-09 11:37:38 +01002026 \ #{regcontents: ['bar'], regname: 'b', operator: 'd', regtype: 'v',
2027 \ visual: v:false, inclusive: v:true},
2028 \ g:event)
2029
2030 call setline(1, 'foobar')
2031 " exclusive motion
2032 norm $"ay0
2033 call assert_equal(
2034 \ #{regcontents: ['fooba'], regname: 'a', operator: 'y', regtype: 'v',
2035 \ visual: v:false, inclusive: v:false},
2036 \ g:event)
2037 " inclusive motion
2038 norm 0"ay$
2039 call assert_equal(
2040 \ #{regcontents: ['foobar'], regname: 'a', operator: 'y', regtype: 'v',
2041 \ visual: v:false, inclusive: v:true},
2042 \ g:event)
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01002043
2044 call assert_equal({}, v:event)
2045
Bram Moolenaarfccbf062020-11-26 20:34:00 +01002046 if has('clipboard_working') && !has('gui_running')
2047 " Test that when the visual selection is automatically copied to clipboard
2048 " register a TextYankPost is emitted
2049 call setline(1, ['foobar'])
2050
2051 let @* = ''
2052 set clipboard=autoselect
2053 exe "norm! ggviw\<Esc>"
2054 call assert_equal(
Bram Moolenaara016eeb2022-04-09 11:37:38 +01002055 \ #{regcontents: ['foobar'], regname: '*', operator: 'y',
2056 \ regtype: 'v', visual: v:true, inclusive: v:false},
2057 \ g:event)
Bram Moolenaarfccbf062020-11-26 20:34:00 +01002058
2059 let @+ = ''
2060 set clipboard=autoselectplus
2061 exe "norm! ggviw\<Esc>"
2062 call assert_equal(
Bram Moolenaara016eeb2022-04-09 11:37:38 +01002063 \ #{regcontents: ['foobar'], regname: '+', operator: 'y',
2064 \ regtype: 'v', visual: v:true, inclusive: v:false},
2065 \ g:event)
Bram Moolenaarfccbf062020-11-26 20:34:00 +01002066
2067 set clipboard&vim
2068 endif
2069
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01002070 au! TextYankPost
2071 unlet g:event
2072 bwipe!
2073endfunc
Bram Moolenaar9bca8052017-12-18 12:37:55 +01002074
Bram Moolenaar9a046fd2021-01-28 13:47:59 +01002075func Test_autocommand_all_events()
2076 call assert_fails('au * * bwipe', 'E1155:')
2077 call assert_fails('au * x bwipe', 'E1155:')
Bram Moolenaarb6db1462021-12-24 19:24:47 +00002078 call assert_fails('au! * x bwipe', 'E1155:')
Bram Moolenaar4fb921e2017-12-18 15:33:00 +01002079endfunc
Bram Moolenaarb7407d32018-02-03 17:36:27 +01002080
Bram Moolenaarf6246f52022-02-11 16:30:12 +00002081func Test_autocmd_user()
2082 au User MyEvent let s:res = [expand("<afile>"), expand("<amatch>")]
2083 doautocmd User MyEvent
2084 call assert_equal(['MyEvent', 'MyEvent'], s:res)
2085 au! User
2086 unlet s:res
2087endfunc
2088
Bram Moolenaarb7407d32018-02-03 17:36:27 +01002089function s:Before_test_dirchanged()
2090 augroup test_dirchanged
2091 autocmd!
2092 augroup END
2093 let s:li = []
2094 let s:dir_this = getcwd()
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02002095 let s:dir_foo = s:dir_this . '/Xfoo'
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01002096 call mkdir(s:dir_foo)
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02002097 let s:dir_bar = s:dir_this . '/Xbar'
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01002098 call mkdir(s:dir_bar)
Bram Moolenaarb7407d32018-02-03 17:36:27 +01002099endfunc
2100
2101function s:After_test_dirchanged()
Bram Moolenaar3503d7c2019-11-09 20:10:17 +01002102 call chdir(s:dir_this)
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01002103 call delete(s:dir_foo, 'd')
2104 call delete(s:dir_bar, 'd')
Bram Moolenaarb7407d32018-02-03 17:36:27 +01002105 augroup test_dirchanged
2106 autocmd!
2107 augroup END
2108endfunc
2109
2110function Test_dirchanged_global()
2111 call s:Before_test_dirchanged()
Bram Moolenaarf6246f52022-02-11 16:30:12 +00002112 autocmd test_dirchanged DirChangedPre global call add(s:li, expand("<amatch>") .. " pre cd " .. v:event.directory)
Bram Moolenaarb7407d32018-02-03 17:36:27 +01002113 autocmd test_dirchanged DirChanged global call add(s:li, "cd:")
2114 autocmd test_dirchanged DirChanged global call add(s:li, expand("<afile>"))
Bram Moolenaar3503d7c2019-11-09 20:10:17 +01002115 call chdir(s:dir_foo)
Bram Moolenaarf6246f52022-02-11 16:30:12 +00002116 let expected = ["global pre cd " .. s:dir_foo, "cd:", s:dir_foo]
Bram Moolenaar28e8f732022-02-09 12:58:20 +00002117 call assert_equal(expected, s:li)
Bram Moolenaar3503d7c2019-11-09 20:10:17 +01002118 call chdir(s:dir_foo)
Bram Moolenaar28e8f732022-02-09 12:58:20 +00002119 call assert_equal(expected, s:li)
Bram Moolenaar3503d7c2019-11-09 20:10:17 +01002120 exe 'lcd ' .. fnameescape(s:dir_bar)
Bram Moolenaar28e8f732022-02-09 12:58:20 +00002121 call assert_equal(expected, s:li)
Bram Moolenaard8c9d322022-06-12 11:49:16 +01002122
2123 exe 'cd ' .. s:dir_foo
2124 exe 'cd ' .. s:dir_bar
2125 autocmd! test_dirchanged DirChanged global let g:result = expand("<afile>")
2126 cd -
Bram Moolenaardb77c492022-06-12 23:26:50 +01002127 call assert_equal(s:dir_foo, substitute(g:result, '\\', '/', 'g'))
Bram Moolenaard8c9d322022-06-12 11:49:16 +01002128
Bram Moolenaarb7407d32018-02-03 17:36:27 +01002129 call s:After_test_dirchanged()
2130endfunc
2131
2132function Test_dirchanged_local()
2133 call s:Before_test_dirchanged()
2134 autocmd test_dirchanged DirChanged window call add(s:li, "lcd:")
2135 autocmd test_dirchanged DirChanged window call add(s:li, expand("<afile>"))
Bram Moolenaar3503d7c2019-11-09 20:10:17 +01002136 call chdir(s:dir_foo)
Bram Moolenaarb7407d32018-02-03 17:36:27 +01002137 call assert_equal([], s:li)
Bram Moolenaar3503d7c2019-11-09 20:10:17 +01002138 exe 'lcd ' .. fnameescape(s:dir_bar)
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01002139 call assert_equal(["lcd:", s:dir_bar], s:li)
Bram Moolenaar3503d7c2019-11-09 20:10:17 +01002140 exe 'lcd ' .. fnameescape(s:dir_bar)
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01002141 call assert_equal(["lcd:", s:dir_bar], s:li)
Bram Moolenaarb7407d32018-02-03 17:36:27 +01002142 call s:After_test_dirchanged()
2143endfunc
2144
2145function Test_dirchanged_auto()
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +02002146 CheckOption autochdir
Bram Moolenaarb7407d32018-02-03 17:36:27 +01002147 call s:Before_test_dirchanged()
2148 call test_autochdir()
Bram Moolenaar28e8f732022-02-09 12:58:20 +00002149 autocmd test_dirchanged DirChangedPre auto call add(s:li, "pre cd " .. v:event.directory)
Bram Moolenaarb7407d32018-02-03 17:36:27 +01002150 autocmd test_dirchanged DirChanged auto call add(s:li, "auto:")
2151 autocmd test_dirchanged DirChanged auto call add(s:li, expand("<afile>"))
2152 set acd
Bram Moolenaar3503d7c2019-11-09 20:10:17 +01002153 cd ..
Bram Moolenaarb7407d32018-02-03 17:36:27 +01002154 call assert_equal([], s:li)
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01002155 exe 'edit ' . s:dir_foo . '/Xfile'
2156 call assert_equal(s:dir_foo, getcwd())
Bram Moolenaar28e8f732022-02-09 12:58:20 +00002157 let expected = ["pre cd " .. s:dir_foo, "auto:", s:dir_foo]
2158 call assert_equal(expected, s:li)
Bram Moolenaarb7407d32018-02-03 17:36:27 +01002159 set noacd
2160 bwipe!
2161 call s:After_test_dirchanged()
2162endfunc
Bram Moolenaar5a093432018-02-10 18:15:19 +01002163
2164" Test TextChangedI and TextChangedP
2165func Test_ChangedP()
2166 new
2167 call setline(1, ['foo', 'bar', 'foobar'])
2168 call test_override("char_avail", 1)
2169 set complete=. completeopt=menuone
2170
2171 func! TextChangedAutocmd(char)
2172 let g:autocmd .= a:char
2173 endfunc
2174
Christian Brabandtdb3b4462021-10-16 11:58:55 +01002175 " TextChanged will not be triggered, only check that it isn't.
Bram Moolenaar5a093432018-02-10 18:15:19 +01002176 au! TextChanged <buffer> :call TextChangedAutocmd('N')
2177 au! TextChangedI <buffer> :call TextChangedAutocmd('I')
2178 au! TextChangedP <buffer> :call TextChangedAutocmd('P')
2179
2180 call cursor(3, 1)
2181 let g:autocmd = ''
2182 call feedkeys("o\<esc>", 'tnix')
2183 call assert_equal('I', g:autocmd)
2184
2185 let g:autocmd = ''
2186 call feedkeys("Sf", 'tnix')
2187 call assert_equal('II', g:autocmd)
2188
2189 let g:autocmd = ''
2190 call feedkeys("Sf\<C-N>", 'tnix')
2191 call assert_equal('IIP', g:autocmd)
2192
2193 let g:autocmd = ''
2194 call feedkeys("Sf\<C-N>\<C-N>", 'tnix')
2195 call assert_equal('IIPP', g:autocmd)
2196
2197 let g:autocmd = ''
2198 call feedkeys("Sf\<C-N>\<C-N>\<C-N>", 'tnix')
2199 call assert_equal('IIPPP', g:autocmd)
2200
2201 let g:autocmd = ''
2202 call feedkeys("Sf\<C-N>\<C-N>\<C-N>\<C-N>", 'tnix')
2203 call assert_equal('IIPPPP', g:autocmd)
2204
2205 call assert_equal(['foo', 'bar', 'foobar', 'foo'], getline(1, '$'))
2206 " TODO: how should it handle completeopt=noinsert,noselect?
2207
2208 " CleanUp
2209 call test_override("char_avail", 0)
2210 au! TextChanged
2211 au! TextChangedI
2212 au! TextChangedP
2213 delfu TextChangedAutocmd
2214 unlet! g:autocmd
2215 set complete&vim completeopt&vim
2216
2217 bw!
2218endfunc
Bram Moolenaar8c64a362018-03-23 22:39:31 +01002219
Bram Moolenaar91d2e782018-08-07 19:05:01 +02002220let g:setline_handled = v:false
Bram Moolenaar1e115362019-01-09 23:01:02 +01002221func SetLineOne()
Bram Moolenaar91d2e782018-08-07 19:05:01 +02002222 if !g:setline_handled
2223 call setline(1, "(x)")
2224 let g:setline_handled = v:true
2225 endif
2226endfunc
2227
2228func Test_TextChangedI_with_setline()
2229 new
2230 call test_override('char_avail', 1)
2231 autocmd TextChangedI <buffer> call SetLineOne()
2232 call feedkeys("i(\<CR>\<Esc>", 'tx')
2233 call assert_equal('(', getline(1))
2234 call assert_equal('x)', getline(2))
2235 undo
Bram Moolenaar91d2e782018-08-07 19:05:01 +02002236 call assert_equal('', getline(1))
Bram Moolenaar9fa95062018-08-08 22:08:32 +02002237 call assert_equal('', getline(2))
Bram Moolenaar91d2e782018-08-07 19:05:01 +02002238
Bram Moolenaarca34db32022-01-20 11:17:18 +00002239 call test_override('char_avail', 0)
Bram Moolenaar91d2e782018-08-07 19:05:01 +02002240 bwipe!
2241endfunc
2242
Bram Moolenaar8c64a362018-03-23 22:39:31 +01002243func Test_Changed_FirstTime()
Bram Moolenaar8c5a2782019-08-07 23:07:07 +02002244 CheckFeature terminal
2245 CheckNotGui
Bram Moolenaar3cdcb092020-03-18 19:18:10 +01002246 " Starting a terminal to run Vim is always considered flaky.
Bram Moolenaar30d53e22020-03-18 21:10:44 +01002247 let g:test_is_flaky = 1
Bram Moolenaar8c5a2782019-08-07 23:07:07 +02002248
Bram Moolenaar8c64a362018-03-23 22:39:31 +01002249 " Prepare file for TextChanged event.
2250 call writefile([''], 'Xchanged.txt')
2251 let buf = term_start([GetVimProg(), '--clean', '-c', 'set noswapfile'], {'term_rows': 3})
2252 call assert_equal('running', term_getstatus(buf))
Bram Moolenaar1834d372018-03-29 17:40:46 +02002253 " Wait for the ruler (in the status line) to be shown.
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01002254 " In ConPTY, there is additional character which is drawn up to the width of
2255 " the screen.
2256 if has('conpty')
2257 call WaitForAssert({-> assert_match('\<All.*$', term_getline(buf, 3))})
2258 else
2259 call WaitForAssert({-> assert_match('\<All$', term_getline(buf, 3))})
2260 endif
Bram Moolenaar8c64a362018-03-23 22:39:31 +01002261 " It's only adding autocmd, so that no event occurs.
2262 call term_sendkeys(buf, ":au! TextChanged <buffer> call writefile(['No'], 'Xchanged.txt')\<cr>")
2263 call term_sendkeys(buf, "\<C-\\>\<C-N>:qa!\<cr>")
Bram Moolenaar50182fa2018-04-28 21:34:40 +02002264 call WaitForAssert({-> assert_equal('finished', term_getstatus(buf))})
Bram Moolenaar8c64a362018-03-23 22:39:31 +01002265 call assert_equal([''], readfile('Xchanged.txt'))
2266
2267 " clean up
2268 call delete('Xchanged.txt')
2269 bwipe!
2270endfunc
Bram Moolenaar0566e892019-01-24 19:37:40 +01002271
Bram Moolenaareb93f3f2019-04-04 15:04:56 +02002272func Test_autocmd_nested()
2273 let g:did_nested = 0
2274 augroup Testing
2275 au WinNew * edit somefile
2276 au BufNew * let g:did_nested = 1
2277 augroup END
2278 split
2279 call assert_equal(0, g:did_nested)
2280 close
2281 bwipe! somefile
2282
2283 " old nested argument still works
2284 augroup Testing
2285 au!
2286 au WinNew * nested edit somefile
2287 au BufNew * let g:did_nested = 1
2288 augroup END
2289 split
2290 call assert_equal(1, g:did_nested)
2291 close
2292 bwipe! somefile
2293
2294 " New ++nested argument works
2295 augroup Testing
2296 au!
2297 au WinNew * ++nested edit somefile
2298 au BufNew * let g:did_nested = 1
2299 augroup END
2300 split
2301 call assert_equal(1, g:did_nested)
2302 close
2303 bwipe! somefile
2304
Bram Moolenaarf0775142022-03-04 20:10:38 +00002305 " nested without ++ does not work in Vim9 script
2306 call assert_fails('vim9cmd au WinNew * nested echo fails', 'E1078:')
2307
Bram Moolenaareb93f3f2019-04-04 15:04:56 +02002308 augroup Testing
2309 au!
2310 augroup END
2311
2312 call assert_fails('au WinNew * ++nested ++nested echo bad', 'E983:')
2313 call assert_fails('au WinNew * nested nested echo bad', 'E983:')
2314endfunc
2315
Bram Moolenaar5fa9f232022-07-23 09:06:48 +01002316func Test_autocmd_nested_cursor_invalid()
2317 set laststatus=0
2318 copen
2319 cclose
2320 call setline(1, ['foo', 'bar', 'baz'])
2321 3
2322 augroup nested_inv
2323 autocmd User foo ++nested copen
2324 autocmd BufAdd * let &laststatus = 2 - &laststatus
2325 augroup END
2326 doautocmd User foo
2327
2328 augroup nested_inv
2329 au!
2330 augroup END
2331 set laststatus&
Bram Moolenaarb03950f2022-07-26 13:47:13 +01002332 cclose
Bram Moolenaar5fa9f232022-07-23 09:06:48 +01002333 bwipe!
2334endfunc
2335
Bram Moolenaar3d6ee8b2022-07-27 15:23:35 +01002336func Test_autocmd_nested_keeps_cursor_pos()
2337 enew
2338 call setline(1, 'foo')
2339 autocmd User foo ++nested normal! $a
2340 autocmd InsertLeave * :
2341 doautocmd User foo
2342 call assert_equal([0, 1, 3, 0], getpos('.'))
2343
2344 bwipe!
2345endfunc
2346
Bram Moolenaarb03950f2022-07-26 13:47:13 +01002347func Test_autocmd_nested_switch_window()
2348 " run this in a separate Vim so that SafeState works
2349 CheckRunVimInTerminal
2350
2351 let lines =<< trim END
2352 vim9script
2353 ['()']->writefile('Xautofile')
2354 autocmd VimEnter * ++nested edit Xautofile | split
2355 autocmd BufReadPost * autocmd SafeState * ++once foldclosed('.')
2356 autocmd WinEnter * matchadd('ErrorMsg', 'pat')
2357 END
2358 call writefile(lines, 'Xautoscript')
2359 let buf = RunVimInTerminal('-S Xautoscript', {'rows': 10})
2360 call VerifyScreenDump(buf, 'Test_autocmd_nested_switch', {})
2361
2362 call StopVimInTerminal(buf)
2363 call delete('Xautofile')
2364 call delete('Xautoscript')
2365endfunc
2366
Bram Moolenaareb93f3f2019-04-04 15:04:56 +02002367func Test_autocmd_once()
2368 " Without ++once WinNew triggers twice
2369 let g:did_split = 0
2370 augroup Testing
2371 au WinNew * let g:did_split += 1
2372 augroup END
2373 split
2374 split
2375 call assert_equal(2, g:did_split)
2376 call assert_true(exists('#WinNew'))
2377 close
2378 close
2379
2380 " With ++once WinNew triggers once
2381 let g:did_split = 0
2382 augroup Testing
2383 au!
2384 au WinNew * ++once let g:did_split += 1
2385 augroup END
2386 split
2387 split
2388 call assert_equal(1, g:did_split)
2389 call assert_false(exists('#WinNew'))
2390 close
2391 close
2392
2393 call assert_fails('au WinNew * ++once ++once echo bad', 'E983:')
2394endfunc
2395
Bram Moolenaara68e5952019-04-25 22:22:01 +02002396func Test_autocmd_bufreadpre()
2397 new
2398 let b:bufreadpre = 1
Bram Moolenaarab505b12020-03-23 19:28:44 +01002399 call append(0, range(1000))
Bram Moolenaara68e5952019-04-25 22:22:01 +02002400 w! XAutocmdBufReadPre.txt
2401 autocmd BufReadPre <buffer> :let b:bufreadpre += 1
Bram Moolenaarab505b12020-03-23 19:28:44 +01002402 norm! 500gg
Bram Moolenaara68e5952019-04-25 22:22:01 +02002403 sp
Bram Moolenaarab505b12020-03-23 19:28:44 +01002404 norm! 1000gg
Bram Moolenaara68e5952019-04-25 22:22:01 +02002405 wincmd p
2406 let g:wsv1 = winsaveview()
2407 wincmd p
2408 let g:wsv2 = winsaveview()
2409 " triggers BufReadPre, should not move the cursor in either window
2410 " The topline may change one line in a large window.
2411 edit
2412 call assert_inrange(g:wsv2.topline - 1, g:wsv2.topline + 1, winsaveview().topline)
2413 call assert_equal(g:wsv2.lnum, winsaveview().lnum)
2414 call assert_equal(2, b:bufreadpre)
2415 wincmd p
2416 call assert_equal(g:wsv1.topline, winsaveview().topline)
2417 call assert_equal(g:wsv1.lnum, winsaveview().lnum)
2418 call assert_equal(2, b:bufreadpre)
2419 " Now set the cursor position in an BufReadPre autocommand
2420 " (even though the position will be invalid, this should make Vim reset the
2421 " cursor position in the other window.
2422 wincmd p
2423 set cpo+=g
2424 " won't do anything, but try to set the cursor on an invalid lnum
2425 autocmd BufReadPre <buffer> :norm! 70gg
2426 " triggers BufReadPre, should not move the cursor in either window
2427 e
2428 call assert_equal(1, winsaveview().topline)
2429 call assert_equal(1, winsaveview().lnum)
2430 call assert_equal(3, b:bufreadpre)
2431 wincmd p
2432 call assert_equal(g:wsv1.topline, winsaveview().topline)
2433 call assert_equal(g:wsv1.lnum, winsaveview().lnum)
2434 call assert_equal(3, b:bufreadpre)
2435 close
2436 close
2437 call delete('XAutocmdBufReadPre.txt')
2438 set cpo-=g
2439endfunc
2440
Bram Moolenaar5e66b422019-01-24 21:58:10 +01002441" FileChangedShell tested in test_filechanged.vim
Bram Moolenaar69ea5872019-04-25 20:29:00 +02002442
2443" Tests for the following autocommands:
2444" - FileWritePre writing a compressed file
2445" - FileReadPost reading a compressed file
2446" - BufNewFile reading a file template
2447" - BufReadPre decompressing the file to be read
2448" - FilterReadPre substituting characters in the temp file
2449" - FilterReadPost substituting characters after filtering
2450" - FileReadPre set options for decompression
2451" - FileReadPost decompress the file
2452func Test_ReadWrite_Autocmds()
2453 " Run this test only on Unix-like systems and if gzip is available
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +02002454 CheckUnix
2455 CheckExecutable gzip
Bram Moolenaar69ea5872019-04-25 20:29:00 +02002456
2457 " Make $GZIP empty, "-v" would cause trouble.
2458 let $GZIP = ""
2459
2460 " Use a FileChangedShell autocommand to avoid a prompt for 'Xtestfile.gz'
2461 " being modified outside of Vim (noticed on Solaris).
2462 au FileChangedShell * echo 'caught FileChangedShell'
2463
2464 " Test for the FileReadPost, FileWritePre and FileWritePost autocmds
2465 augroup Test1
2466 au!
2467 au FileWritePre *.gz '[,']!gzip
2468 au FileWritePost *.gz undo
2469 au FileReadPost *.gz '[,']!gzip -d
2470 augroup END
2471
2472 new
2473 set bin
2474 call append(0, [
2475 \ 'line 2 Abcdefghijklmnopqrstuvwxyz',
2476 \ 'line 3 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2477 \ 'line 4 Abcdefghijklmnopqrstuvwxyz',
2478 \ 'line 5 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2479 \ 'line 6 Abcdefghijklmnopqrstuvwxyz',
2480 \ 'line 7 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2481 \ 'line 8 Abcdefghijklmnopqrstuvwxyz',
2482 \ 'line 9 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2483 \ 'line 10 Abcdefghijklmnopqrstuvwxyz'
2484 \ ])
2485 1,9write! Xtestfile.gz
2486 enew! | close
2487
2488 new
2489 " Read and decompress the testfile
2490 0read Xtestfile.gz
2491 call assert_equal([
2492 \ 'line 2 Abcdefghijklmnopqrstuvwxyz',
2493 \ 'line 3 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2494 \ 'line 4 Abcdefghijklmnopqrstuvwxyz',
2495 \ 'line 5 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2496 \ 'line 6 Abcdefghijklmnopqrstuvwxyz',
2497 \ 'line 7 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2498 \ 'line 8 Abcdefghijklmnopqrstuvwxyz',
2499 \ 'line 9 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2500 \ 'line 10 Abcdefghijklmnopqrstuvwxyz'
2501 \ ], getline(1, 9))
2502 enew! | close
2503
2504 augroup Test1
2505 au!
2506 augroup END
2507
2508 " Test for the FileAppendPre and FileAppendPost autocmds
2509 augroup Test2
2510 au!
2511 au BufNewFile *.c read Xtest.c
2512 au FileAppendPre *.out '[,']s/new/NEW/
2513 au FileAppendPost *.out !cat Xtest.c >> test.out
2514 augroup END
2515
2516 call writefile(['/*', ' * Here is a new .c file', ' */'], 'Xtest.c')
2517 new foo.c " should load Xtest.c
2518 call assert_equal(['/*', ' * Here is a new .c file', ' */'], getline(2, 4))
2519 w! >> test.out " append it to the output file
2520
2521 let contents = readfile('test.out')
2522 call assert_equal(' * Here is a NEW .c file', contents[2])
2523 call assert_equal(' * Here is a new .c file', contents[5])
2524
2525 call delete('test.out')
2526 enew! | close
2527 augroup Test2
2528 au!
2529 augroup END
2530
2531 " Test for the BufReadPre and BufReadPost autocmds
2532 augroup Test3
2533 au!
2534 " setup autocommands to decompress before reading and re-compress
2535 " afterwards
2536 au BufReadPre *.gz exe '!gzip -d ' . shellescape(expand("<afile>"))
2537 au BufReadPre *.gz call rename(expand("<afile>:r"), expand("<afile>"))
2538 au BufReadPost *.gz call rename(expand("<afile>"), expand("<afile>:r"))
2539 au BufReadPost *.gz exe '!gzip ' . shellescape(expand("<afile>:r"))
2540 augroup END
2541
2542 e! Xtestfile.gz " Edit compressed file
2543 call assert_equal([
2544 \ 'line 2 Abcdefghijklmnopqrstuvwxyz',
2545 \ 'line 3 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2546 \ 'line 4 Abcdefghijklmnopqrstuvwxyz',
2547 \ 'line 5 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2548 \ 'line 6 Abcdefghijklmnopqrstuvwxyz',
2549 \ 'line 7 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2550 \ 'line 8 Abcdefghijklmnopqrstuvwxyz',
2551 \ 'line 9 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2552 \ 'line 10 Abcdefghijklmnopqrstuvwxyz'
2553 \ ], getline(1, 9))
2554
2555 w! >> test.out " Append it to the output file
2556
2557 augroup Test3
2558 au!
2559 augroup END
2560
2561 " Test for the FilterReadPre and FilterReadPost autocmds.
2562 set shelltemp " need temp files here
2563 augroup Test4
2564 au!
2565 au FilterReadPre *.out call rename(expand("<afile>"), expand("<afile>") . ".t")
2566 au FilterReadPre *.out exe 'silent !sed s/e/E/ ' . shellescape(expand("<afile>")) . ".t >" . shellescape(expand("<afile>"))
2567 au FilterReadPre *.out exe 'silent !rm ' . shellescape(expand("<afile>")) . '.t'
2568 au FilterReadPost *.out '[,']s/x/X/g
2569 augroup END
2570
2571 e! test.out " Edit the output file
2572 1,$!cat
2573 call assert_equal([
2574 \ 'linE 2 AbcdefghijklmnopqrstuvwXyz',
2575 \ 'linE 3 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
2576 \ 'linE 4 AbcdefghijklmnopqrstuvwXyz',
2577 \ 'linE 5 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
2578 \ 'linE 6 AbcdefghijklmnopqrstuvwXyz',
2579 \ 'linE 7 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
2580 \ 'linE 8 AbcdefghijklmnopqrstuvwXyz',
2581 \ 'linE 9 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
2582 \ 'linE 10 AbcdefghijklmnopqrstuvwXyz'
2583 \ ], getline(1, 9))
2584 call assert_equal([
2585 \ 'line 2 Abcdefghijklmnopqrstuvwxyz',
2586 \ 'line 3 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2587 \ 'line 4 Abcdefghijklmnopqrstuvwxyz',
2588 \ 'line 5 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2589 \ 'line 6 Abcdefghijklmnopqrstuvwxyz',
2590 \ 'line 7 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2591 \ 'line 8 Abcdefghijklmnopqrstuvwxyz',
2592 \ 'line 9 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2593 \ 'line 10 Abcdefghijklmnopqrstuvwxyz'
2594 \ ], readfile('test.out'))
2595
2596 augroup Test4
2597 au!
2598 augroup END
2599 set shelltemp&vim
2600
2601 " Test for the FileReadPre and FileReadPost autocmds.
2602 augroup Test5
2603 au!
2604 au FileReadPre *.gz exe 'silent !gzip -d ' . shellescape(expand("<afile>"))
2605 au FileReadPre *.gz call rename(expand("<afile>:r"), expand("<afile>"))
2606 au FileReadPost *.gz '[,']s/l/L/
2607 augroup END
2608
2609 new
2610 0r Xtestfile.gz " Read compressed file
2611 call assert_equal([
2612 \ 'Line 2 Abcdefghijklmnopqrstuvwxyz',
2613 \ 'Line 3 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2614 \ 'Line 4 Abcdefghijklmnopqrstuvwxyz',
2615 \ 'Line 5 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2616 \ 'Line 6 Abcdefghijklmnopqrstuvwxyz',
2617 \ 'Line 7 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2618 \ 'Line 8 Abcdefghijklmnopqrstuvwxyz',
2619 \ 'Line 9 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2620 \ 'Line 10 Abcdefghijklmnopqrstuvwxyz'
2621 \ ], getline(1, 9))
2622 call assert_equal([
2623 \ 'line 2 Abcdefghijklmnopqrstuvwxyz',
2624 \ 'line 3 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2625 \ 'line 4 Abcdefghijklmnopqrstuvwxyz',
2626 \ 'line 5 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2627 \ 'line 6 Abcdefghijklmnopqrstuvwxyz',
2628 \ 'line 7 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2629 \ 'line 8 Abcdefghijklmnopqrstuvwxyz',
2630 \ 'line 9 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2631 \ 'line 10 Abcdefghijklmnopqrstuvwxyz'
2632 \ ], readfile('Xtestfile.gz'))
2633
2634 augroup Test5
2635 au!
2636 augroup END
2637
2638 au! FileChangedShell
2639 call delete('Xtestfile.gz')
2640 call delete('Xtest.c')
2641 call delete('test.out')
2642endfunc
Bram Moolenaar23b51392019-05-09 21:38:43 +02002643
2644func Test_throw_in_BufWritePre()
2645 new
2646 call setline(1, ['one', 'two', 'three'])
2647 call assert_false(filereadable('Xthefile'))
2648 augroup throwing
2649 au BufWritePre X* throw 'do not write'
2650 augroup END
2651 try
2652 w Xthefile
2653 catch
2654 let caught = 1
2655 endtry
2656 call assert_equal(1, caught)
2657 call assert_false(filereadable('Xthefile'))
2658
2659 bwipe!
2660 au! throwing
2661endfunc
Bram Moolenaarcadbe1b2019-09-22 21:50:09 +02002662
Bram Moolenaar40fa12a2021-09-22 14:18:13 +02002663func Test_autocmd_in_try_block()
2664 call mkdir('Xdir')
2665 au BufEnter * let g:fname = expand('%')
2666 try
2667 edit Xdir/
2668 endtry
2669 call assert_match('Xdir', g:fname)
2670
2671 unlet g:fname
2672 au! BufEnter
2673 call delete('Xdir', 'rf')
2674endfunc
2675
Bram Moolenaarcadbe1b2019-09-22 21:50:09 +02002676func Test_autocmd_SafeState()
2677 CheckRunVimInTerminal
2678
2679 let lines =<< trim END
2680 let g:safe = 0
2681 let g:again = ''
2682 au SafeState * let g:safe += 1
2683 au SafeStateAgain * let g:again ..= 'x'
2684 func CallTimer()
2685 call timer_start(10, {id -> execute('let g:again ..= "t"')})
2686 endfunc
2687 END
2688 call writefile(lines, 'XSafeState')
2689 let buf = RunVimInTerminal('-S XSafeState', #{rows: 6})
2690
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01002691 " Sometimes we loop to handle a K_IGNORE, SafeState may be triggered once or
Bram Moolenaar8fb1b472020-02-23 16:16:26 +01002692 " more often.
Bram Moolenaarcadbe1b2019-09-22 21:50:09 +02002693 call term_sendkeys(buf, ":echo g:safe\<CR>")
Bram Moolenaar8fb1b472020-02-23 16:16:26 +01002694 call WaitForAssert({-> assert_match('^\d ', term_getline(buf, 6))}, 1000)
Bram Moolenaarcadbe1b2019-09-22 21:50:09 +02002695
Bram Moolenaar8fb1b472020-02-23 16:16:26 +01002696 " SafeStateAgain should be invoked at least three times
Bram Moolenaarcadbe1b2019-09-22 21:50:09 +02002697 call term_sendkeys(buf, ":echo g:again\<CR>")
Bram Moolenaar8fb1b472020-02-23 16:16:26 +01002698 call WaitForAssert({-> assert_match('^xxx', term_getline(buf, 6))}, 1000)
Bram Moolenaarcadbe1b2019-09-22 21:50:09 +02002699
2700 call term_sendkeys(buf, ":let g:again = ''\<CR>:call CallTimer()\<CR>")
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02002701 call TermWait(buf, 50)
Bram Moolenaar0f6629a2019-09-22 23:24:13 +02002702 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02002703 call TermWait(buf, 50)
Bram Moolenaarcadbe1b2019-09-22 21:50:09 +02002704 call term_sendkeys(buf, ":echo g:again\<CR>")
2705 call WaitForAssert({-> assert_match('xtx', term_getline(buf, 6))}, 1000)
2706
2707 call StopVimInTerminal(buf)
2708 call delete('XSafeState')
2709endfunc
Bram Moolenaar23324a02019-10-01 17:39:04 +02002710
2711func Test_autocmd_CmdWinEnter()
2712 CheckRunVimInTerminal
Bram Moolenaar21829c52021-01-26 22:42:21 +01002713 CheckFeature cmdwin
2714
Bram Moolenaar23324a02019-10-01 17:39:04 +02002715 let lines =<< trim END
Egor Zvorykin125ffd22021-11-17 14:01:14 +00002716 augroup vimHints | au! | augroup END
Bram Moolenaar23324a02019-10-01 17:39:04 +02002717 let b:dummy_var = 'This is a dummy'
2718 autocmd CmdWinEnter * quit
2719 let winnr = winnr('$')
2720 END
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01002721 let filename = 'XCmdWinEnter'
Bram Moolenaar23324a02019-10-01 17:39:04 +02002722 call writefile(lines, filename)
2723 let buf = RunVimInTerminal('-S '.filename, #{rows: 6})
2724
2725 call term_sendkeys(buf, "q:")
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02002726 call TermWait(buf)
Bram Moolenaar23324a02019-10-01 17:39:04 +02002727 call term_sendkeys(buf, ":echo b:dummy_var\<cr>")
Bram Moolenaar353c3512020-03-15 14:19:26 +01002728 call WaitForAssert({-> assert_match('^This is a dummy', term_getline(buf, 6))}, 2000)
Bram Moolenaar23324a02019-10-01 17:39:04 +02002729 call term_sendkeys(buf, ":echo &buftype\<cr>")
2730 call WaitForAssert({-> assert_notmatch('^nofile', term_getline(buf, 6))}, 1000)
2731 call term_sendkeys(buf, ":echo winnr\<cr>")
2732 call WaitForAssert({-> assert_match('^1', term_getline(buf, 6))}, 1000)
2733
2734 " clean up
2735 call StopVimInTerminal(buf)
2736 call delete(filename)
2737endfunc
Bram Moolenaarec66c412019-10-11 21:19:13 +02002738
2739func Test_autocmd_was_using_freed_memory()
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01002740 CheckFeature quickfix
2741
Bram Moolenaarec66c412019-10-11 21:19:13 +02002742 pedit xx
2743 n x
Bram Moolenaar92bb83e2021-02-03 23:04:46 +01002744 augroup winenter
2745 au WinEnter * if winnr('$') > 2 | quit | endif
2746 augroup END
Bram Moolenaarec66c412019-10-11 21:19:13 +02002747 split
Bram Moolenaar92bb83e2021-02-03 23:04:46 +01002748
2749 augroup winenter
2750 au! WinEnter
2751 augroup END
2752
2753 bwipe xx
2754 bwipe x
2755 pclose
Bram Moolenaarec66c412019-10-11 21:19:13 +02002756endfunc
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +01002757
2758func Test_BufWrite_lockmarks()
Bram Moolenaarf08b0eb2021-10-16 13:00:14 +01002759 let g:test_is_flaky = 1
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +01002760 edit! Xtest
2761 call setline(1, ['a', 'b', 'c', 'd'])
2762
2763 " :lockmarks preserves the marks
2764 call SetChangeMarks(2, 3)
2765 lockmarks write
2766 call assert_equal([2, 3], [line("'["), line("']")])
2767
2768 " *WritePre autocmds get the correct line range, but lockmarks preserves the
2769 " original values for the user
2770 augroup lockmarks
2771 au!
2772 au BufWritePre,FilterWritePre * call assert_equal([1, 4], [line("'["), line("']")])
2773 au FileWritePre * call assert_equal([3, 4], [line("'["), line("']")])
2774 augroup END
2775
2776 lockmarks write
2777 call assert_equal([2, 3], [line("'["), line("']")])
2778
2779 if executable('cat')
2780 lockmarks %!cat
2781 call assert_equal([2, 3], [line("'["), line("']")])
2782 endif
2783
2784 lockmarks 3,4write Xtest2
2785 call assert_equal([2, 3], [line("'["), line("']")])
2786
2787 au! lockmarks
2788 augroup! lockmarks
2789 call delete('Xtest')
2790 call delete('Xtest2')
2791endfunc
Bram Moolenaarce6db022020-01-07 20:11:42 +01002792
2793func Test_FileType_spell()
2794 if !isdirectory('/tmp')
2795 throw "Skipped: requires /tmp directory"
2796 endif
2797
2798 " this was crashing with an invalid free()
2799 setglobal spellfile=/tmp/en.utf-8.add
2800 augroup crash
2801 autocmd!
2802 autocmd BufNewFile,BufReadPost crashfile setf somefiletype
2803 autocmd BufNewFile,BufReadPost crashfile set ft=anotherfiletype
2804 autocmd FileType anotherfiletype setlocal spell
2805 augroup END
2806 func! NoCrash() abort
2807 edit /tmp/crashfile
2808 endfunc
2809 call NoCrash()
2810
2811 au! crash
2812 setglobal spellfile=
2813endfunc
Bram Moolenaarbc2b71d2020-02-17 21:33:30 +01002814
Bram Moolenaar406cd902020-02-18 21:54:41 +01002815" Test closing a window or editing another buffer from a FileChangedRO handler
2816" in a readonly buffer
2817func Test_FileChangedRO_winclose()
Bram Moolenaar62cd26a2020-10-11 20:08:44 +02002818 call test_override('ui_delay', 10)
2819
Bram Moolenaar406cd902020-02-18 21:54:41 +01002820 augroup FileChangedROTest
2821 au!
2822 autocmd FileChangedRO * quit
2823 augroup END
2824 new
2825 set readonly
2826 call assert_fails('normal i', 'E788:')
2827 close
2828 augroup! FileChangedROTest
2829
2830 augroup FileChangedROTest
2831 au!
2832 autocmd FileChangedRO * edit Xfile
2833 augroup END
2834 new
2835 set readonly
2836 call assert_fails('normal i', 'E788:')
2837 close
2838 augroup! FileChangedROTest
Bram Moolenaar62cd26a2020-10-11 20:08:44 +02002839 call test_override('ALL', 0)
Bram Moolenaar406cd902020-02-18 21:54:41 +01002840endfunc
2841
Bram Moolenaar0c81d1b2020-02-22 22:45:55 +01002842func LogACmd()
2843 call add(g:logged, line('$'))
2844endfunc
2845
2846func Test_TermChanged()
Bram Moolenaard28e0b32020-02-22 23:08:52 +01002847 CheckNotGui
2848
Bram Moolenaar0c81d1b2020-02-22 22:45:55 +01002849 enew!
2850 tabnew
2851 call setline(1, ['a', 'b', 'c', 'd'])
2852 $
2853 au TermChanged * call LogACmd()
2854 let g:logged = []
2855 let term_save = &term
2856 set term=xterm
2857 call assert_equal([1, 4], g:logged)
2858
2859 au! TermChanged
2860 let &term = term_save
2861 bwipe!
2862endfunc
2863
Bram Moolenaare3284872020-03-19 13:55:03 +01002864" Test for FileReadCmd autocmd
2865func Test_autocmd_FileReadCmd()
2866 func ReadFileCmd()
2867 call append(line('$'), "v:cmdarg = " .. v:cmdarg)
2868 endfunc
2869 augroup FileReadCmdTest
2870 au!
2871 au FileReadCmd Xtest call ReadFileCmd()
2872 augroup END
2873
2874 new
2875 read ++bin Xtest
2876 read ++nobin Xtest
2877 read ++edit Xtest
2878 read ++bad=keep Xtest
2879 read ++bad=drop Xtest
2880 read ++bad=- Xtest
2881 read ++ff=unix Xtest
2882 read ++ff=dos Xtest
2883 read ++ff=mac Xtest
2884 read ++enc=utf-8 Xtest
2885
2886 call assert_equal(['',
2887 \ 'v:cmdarg = ++bin',
2888 \ 'v:cmdarg = ++nobin',
2889 \ 'v:cmdarg = ++edit',
2890 \ 'v:cmdarg = ++bad=keep',
2891 \ 'v:cmdarg = ++bad=drop',
2892 \ 'v:cmdarg = ++bad=-',
2893 \ 'v:cmdarg = ++ff=unix',
2894 \ 'v:cmdarg = ++ff=dos',
2895 \ 'v:cmdarg = ++ff=mac',
2896 \ 'v:cmdarg = ++enc=utf-8'], getline(1, '$'))
2897
2898 close!
2899 augroup FileReadCmdTest
2900 au!
2901 augroup END
2902 delfunc ReadFileCmd
2903endfunc
2904
Bram Moolenaaree4e0c12020-04-06 21:35:05 +02002905" Test for passing invalid arguments to autocmd
2906func Test_autocmd_invalid_args()
2907 " Additional character after * for event
2908 call assert_fails('autocmd *a Xfile set ff=unix', 'E215:')
2909 augroup Test
2910 augroup END
2911 " Invalid autocmd event
2912 call assert_fails('autocmd Bufabc Xfile set ft=vim', 'E216:')
2913 " Invalid autocmd event in a autocmd group
2914 call assert_fails('autocmd Test Bufabc Xfile set ft=vim', 'E216:')
2915 augroup! Test
2916 " Execute all autocmds
2917 call assert_fails('doautocmd * BufEnter', 'E217:')
2918 call assert_fails('augroup! x1a2b3', 'E367:')
2919 call assert_fails('autocmd BufNew <buffer=999> pwd', 'E680:')
Bram Moolenaar531be472020-09-23 22:38:05 +02002920 call assert_fails('autocmd BufNew \) set ff=unix', 'E55:')
Bram Moolenaaree4e0c12020-04-06 21:35:05 +02002921endfunc
2922
2923" Test for deep nesting of autocmds
2924func Test_autocmd_deep_nesting()
2925 autocmd BufEnter Xfile doautocmd BufEnter Xfile
2926 call assert_fails('doautocmd BufEnter Xfile', 'E218:')
2927 autocmd! BufEnter Xfile
2928endfunc
2929
Bram Moolenaarbe5ee862020-06-10 20:56:58 +02002930" Tests for SigUSR1 autocmd event, which is only available on posix systems.
2931func Test_autocmd_sigusr1()
2932 CheckUnix
Bram Moolenaar62cd26a2020-10-11 20:08:44 +02002933 CheckExecutable /bin/kill
Bram Moolenaarbe5ee862020-06-10 20:56:58 +02002934
2935 let g:sigusr1_passed = 0
2936 au SigUSR1 * let g:sigusr1_passed = 1
2937 call system('/bin/kill -s usr1 ' . getpid())
2938 call WaitForAssert({-> assert_true(g:sigusr1_passed)})
2939
2940 au! SigUSR1
2941 unlet g:sigusr1_passed
2942endfunc
2943
Bram Moolenaarb340bae2020-06-15 19:51:56 +02002944" Test for BufReadPre autocmd deleting the file
2945func Test_BufReadPre_delfile()
2946 augroup TestAuCmd
2947 au!
2948 autocmd BufReadPre Xfile call delete('Xfile')
2949 augroup END
2950 call writefile([], 'Xfile')
2951 call assert_fails('new Xfile', 'E200:')
2952 call assert_equal('Xfile', @%)
2953 call assert_equal(1, &readonly)
2954 call delete('Xfile')
2955 augroup TestAuCmd
2956 au!
2957 augroup END
2958 close!
2959endfunc
2960
2961" Test for BufReadPre autocmd changing the current buffer
2962func Test_BufReadPre_changebuf()
2963 augroup TestAuCmd
2964 au!
2965 autocmd BufReadPre Xfile edit Xsomeotherfile
2966 augroup END
2967 call writefile([], 'Xfile')
2968 call assert_fails('new Xfile', 'E201:')
2969 call assert_equal('Xsomeotherfile', @%)
2970 call assert_equal(1, &readonly)
2971 call delete('Xfile')
2972 augroup TestAuCmd
2973 au!
2974 augroup END
2975 close!
2976endfunc
2977
2978" Test for BufWipeouti autocmd changing the current buffer when reading a file
2979" in an empty buffer with 'f' flag in 'cpo'
2980func Test_BufDelete_changebuf()
2981 new
2982 augroup TestAuCmd
2983 au!
2984 autocmd BufWipeout * let bufnr = bufadd('somefile') | exe "b " .. bufnr
2985 augroup END
2986 let save_cpo = &cpo
2987 set cpo+=f
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02002988 call assert_fails('r Xfile', ['E812:', 'E484:'])
Bram Moolenaarb340bae2020-06-15 19:51:56 +02002989 call assert_equal('somefile', @%)
2990 let &cpo = save_cpo
2991 augroup TestAuCmd
2992 au!
2993 augroup END
2994 close!
2995endfunc
2996
Bram Moolenaar0fe937f2020-06-16 22:42:04 +02002997" Test for the temporary internal window used to execute autocmds
2998func Test_autocmd_window()
2999 %bw!
3000 edit one.txt
3001 tabnew two.txt
Bram Moolenaar41cd8032021-03-13 15:47:56 +01003002 vnew three.txt
3003 tabnew four.txt
3004 tabprevious
Bram Moolenaar0fe937f2020-06-16 22:42:04 +02003005 let g:blist = []
Bram Moolenaar832adf92020-06-25 19:01:36 +02003006 augroup aucmd_win_test1
Bram Moolenaar0fe937f2020-06-16 22:42:04 +02003007 au!
3008 au BufEnter * call add(g:blist, [expand('<afile>'),
3009 \ win_gettype(bufwinnr(expand('<afile>')))])
3010 augroup END
3011
3012 doautoall BufEnter
Bram Moolenaar41cd8032021-03-13 15:47:56 +01003013 call assert_equal([
3014 \ ['one.txt', 'autocmd'],
3015 \ ['two.txt', ''],
3016 \ ['four.txt', 'autocmd'],
3017 \ ['three.txt', ''],
3018 \ ], g:blist)
Bram Moolenaar0fe937f2020-06-16 22:42:04 +02003019
Bram Moolenaar832adf92020-06-25 19:01:36 +02003020 augroup aucmd_win_test1
Bram Moolenaar0fe937f2020-06-16 22:42:04 +02003021 au!
3022 augroup END
Bram Moolenaar832adf92020-06-25 19:01:36 +02003023 augroup! aucmd_win_test1
3024 %bw!
3025endfunc
3026
3027" Test for trying to close the temporary window used for executing an autocmd
3028func Test_close_autocmd_window()
3029 %bw!
3030 edit one.txt
3031 tabnew two.txt
3032 augroup aucmd_win_test2
3033 au!
3034 au BufEnter * if expand('<afile>') == 'one.txt' | 1close | endif
3035 augroup END
3036
3037 call assert_fails('doautoall BufEnter', 'E813:')
3038
3039 augroup aucmd_win_test2
3040 au!
3041 augroup END
3042 augroup! aucmd_win_test2
Bram Moolenaarcf844172020-06-26 19:44:06 +02003043 %bwipe!
3044endfunc
3045
3046" Test for trying to close the tab that has the temporary window for exeucing
3047" an autocmd.
3048func Test_close_autocmd_tab()
3049 edit one.txt
3050 tabnew two.txt
3051 augroup aucmd_win_test
3052 au!
3053 au BufEnter * if expand('<afile>') == 'one.txt' | tabfirst | tabonly | endif
3054 augroup END
3055
3056 call assert_fails('doautoall BufEnter', 'E813:')
3057
3058 tabonly
3059 augroup aucmd_win_test
3060 au!
3061 augroup END
3062 augroup! aucmd_win_test
3063 %bwipe!
Bram Moolenaar0fe937f2020-06-16 22:42:04 +02003064endfunc
3065
Bram Moolenaarcb1956d2022-01-07 15:45:18 +00003066func Test_Visual_doautoall_redraw()
3067 call setline(1, ['a', 'b'])
3068 new
3069 wincmd p
3070 call feedkeys("G\<C-V>", 'txn')
3071 autocmd User Explode ++once redraw
3072 doautoall User Explode
3073 %bwipe!
3074endfunc
3075
Bram Moolenaar6bcb8772021-02-03 21:23:29 +01003076" This was using freed memory.
3077func Test_BufNew_arglocal()
3078 arglocal
3079 au BufNew * arglocal
3080 call assert_fails('drop xx', 'E1156:')
3081
3082 au! BufNew
3083endfunc
3084
Bram Moolenaar8ab37572021-02-03 21:56:59 +01003085func Test_autocmd_closes_window()
3086 au BufNew,BufWinLeave * e %e
3087 file yyy
3088 au BufNew,BufWinLeave * ball
Bram Moolenaaraad5f9d2021-02-06 17:30:31 +01003089 n xxx
Bram Moolenaar8ab37572021-02-03 21:56:59 +01003090
Bram Moolenaaraad5f9d2021-02-06 17:30:31 +01003091 %bwipe
Bram Moolenaar8ab37572021-02-03 21:56:59 +01003092 au! BufNew
3093 au! BufWinLeave
3094endfunc
3095
Bram Moolenaar92bb83e2021-02-03 23:04:46 +01003096func Test_autocmd_quit_psearch()
3097 sn aa bb
3098 augroup aucmd_win_test
3099 au!
3100 au BufEnter,BufLeave,BufNew,WinEnter,WinLeave,WinNew * if winnr('$') > 1 | q | endif
3101 augroup END
3102 ps /
3103
3104 augroup aucmd_win_test
3105 au!
3106 augroup END
zeertzjq7851c692022-04-21 11:14:01 +01003107 new
3108 pclose
Bram Moolenaar92bb83e2021-02-03 23:04:46 +01003109endfunc
3110
Bram Moolenaaraad5f9d2021-02-06 17:30:31 +01003111" Fuzzer found some strange combination that caused a crash.
3112func Test_autocmd_normal_mess()
Bram Moolenaardd07c022021-02-07 13:32:46 +01003113 " For unknown reason this hangs on MS-Windows
3114 CheckNotMSWindows
3115
Bram Moolenaaraad5f9d2021-02-06 17:30:31 +01003116 augroup aucmd_normal_test
3117 au BufLeave,BufWinLeave,BufHidden,BufUnload,BufDelete,BufWipeout * norm 7q/qc
3118 augroup END
Bram Moolenaar983d83f2021-02-07 12:12:43 +01003119 call assert_fails('o4', 'E1159')
Bram Moolenaaraad5f9d2021-02-06 17:30:31 +01003120 silent! H
Bram Moolenaar983d83f2021-02-07 12:12:43 +01003121 call assert_fails('e xx', 'E1159')
Bram Moolenaaraad5f9d2021-02-06 17:30:31 +01003122 normal G
3123
3124 augroup aucmd_normal_test
3125 au!
3126 augroup END
3127endfunc
3128
Bram Moolenaar8c6951f2021-02-06 18:08:45 +01003129func Test_autocmd_closing_cmdwin()
Bram Moolenaardd07c022021-02-07 13:32:46 +01003130 " For unknown reason this hangs on MS-Windows
3131 CheckNotMSWindows
3132
Bram Moolenaar8c6951f2021-02-06 18:08:45 +01003133 au BufWinLeave * nested q
3134 call assert_fails("norm 7q?\n", 'E855:')
3135
3136 au! BufWinLeave
3137 new
3138 only
3139endfunc
3140
Bram Moolenaar2c7080b2021-02-06 19:19:42 +01003141func Test_autocmd_vimgrep()
3142 augroup aucmd_vimgrep
Charlie Grovesfef44852022-04-19 16:24:12 +01003143 au QuickfixCmdPre,BufNew,BufReadCmd * sb
zeertzjq7851c692022-04-21 11:14:01 +01003144 au QuickfixCmdPre,BufNew,BufReadCmd * q9
Bram Moolenaar2c7080b2021-02-06 19:19:42 +01003145 augroup END
Bram Moolenaardd07c022021-02-07 13:32:46 +01003146 call assert_fails('lv ?a? foo', 'E926:')
Bram Moolenaar2c7080b2021-02-06 19:19:42 +01003147
3148 augroup aucmd_vimgrep
3149 au!
3150 augroup END
3151endfunc
3152
Bram Moolenaar73b8b0a2021-08-01 14:52:32 +02003153func Test_autocmd_with_block()
3154 augroup block_testing
3155 au BufReadPost *.xml {
3156 setlocal matchpairs+=<:>
3157 /<start
3158 }
Bram Moolenaar63b91732021-08-05 20:40:03 +02003159 au CursorHold * {
3160 autocmd BufReadPre * ++once echo 'one' | echo 'two'
3161 g:gotSafeState = 77
3162 }
Bram Moolenaar73b8b0a2021-08-01 14:52:32 +02003163 augroup END
3164
3165 let expected = "\n--- Autocommands ---\nblock_testing BufRead\n *.xml {^@ setlocal matchpairs+=<:>^@ /<start^@ }"
3166 call assert_equal(expected, execute('au BufReadPost *.xml'))
3167
Bram Moolenaar63b91732021-08-05 20:40:03 +02003168 doautocmd CursorHold
3169 call assert_equal(77, g:gotSafeState)
3170 unlet g:gotSafeState
3171
Bram Moolenaar73b8b0a2021-08-01 14:52:32 +02003172 augroup block_testing
3173 au!
Bram Moolenaar75ebd2a2022-06-03 17:39:46 +01003174 autocmd CursorHold * {
3175 if true
3176 # comment
3177 && true
3178
3179 && true
3180 g:done = 'yes'
3181 endif
3182 }
3183 augroup END
3184 doautocmd CursorHold
3185 call assert_equal('yes', g:done)
3186
3187 unlet g:done
3188 augroup block_testing
3189 au!
Bram Moolenaar73b8b0a2021-08-01 14:52:32 +02003190 augroup END
3191endfunc
3192
Christian Brabandtdb3b4462021-10-16 11:58:55 +01003193" Test TextChangedI and TextChanged
3194func Test_Changed_ChangedI()
3195 new
3196 call test_override("char_avail", 1)
3197 let [g:autocmd_i, g:autocmd_n] = ['','']
3198
3199 func! TextChangedAutocmdI(char)
3200 let g:autocmd_{tolower(a:char)} = a:char .. b:changedtick
3201 endfunc
3202
3203 augroup Test_TextChanged
3204 au!
3205 au TextChanged <buffer> :call TextChangedAutocmdI('N')
3206 au TextChangedI <buffer> :call TextChangedAutocmdI('I')
3207 augroup END
3208
3209 call feedkeys("ifoo\<esc>", 'tnix')
3210 " TODO: Test test does not seem to trigger TextChanged autocommand, this
3211 " requires running Vim in a terminal window.
3212 " call assert_equal('N3', g:autocmd_n)
3213 call assert_equal('I3', g:autocmd_i)
3214
3215 call feedkeys("yyp", 'tnix')
3216 " TODO: Test test does not seem to trigger TextChanged autocommand.
3217 " call assert_equal('N4', g:autocmd_n)
3218 call assert_equal('I3', g:autocmd_i)
3219
3220 " CleanUp
3221 call test_override("char_avail", 0)
3222 au! TextChanged <buffer>
3223 au! TextChangedI <buffer>
3224 augroup! Test_TextChanged
3225 delfu TextChangedAutocmdI
3226 unlet! g:autocmd_i g:autocmd_n
3227
3228 bw!
3229endfunc
Bram Moolenaar2c7080b2021-02-06 19:19:42 +01003230
Bram Moolenaar6f2465d2022-03-22 18:13:01 +00003231func Test_closing_autocmd_window()
3232 let lines =<< trim END
3233 edit Xa.txt
3234 tabnew Xb.txt
3235 autocmd BufEnter Xa.txt unhide 1
3236 doautoall BufEnter
3237 END
3238 call v9.CheckScriptFailure(lines, 'E814:')
3239 au! BufEnter
3240 only!
3241 bwipe Xa.txt
3242 bwipe Xb.txt
3243endfunc
3244
Bram Moolenaar347538f2022-03-26 16:28:06 +00003245func Test_bufwipeout_changes_window()
3246 " This should not crash, but we don't have any expectations about what
3247 " happens, changing window in BufWipeout has unpredictable results.
3248 tabedit
3249 let g:window_id = win_getid()
3250 topleft new
3251 setlocal bufhidden=wipe
3252 autocmd BufWipeout <buffer> call win_gotoid(g:window_id)
3253 tabprevious
3254 +tabclose
3255
3256 unlet g:window_id
3257 au! BufWipeout
3258 %bwipe!
3259endfunc
3260
zeertzjq021996f2022-04-10 11:44:04 +01003261func Test_v_event_readonly()
3262 autocmd CompleteChanged * let v:event.width = 0
3263 call assert_fails("normal! i\<C-X>\<C-V>", 'E46:')
3264 au! CompleteChanged
3265
3266 autocmd DirChangedPre * let v:event.directory = ''
3267 call assert_fails('cd .', 'E46:')
3268 au! DirChangedPre
3269
3270 autocmd ModeChanged * let v:event.new_mode = ''
3271 call assert_fails('normal! cc', 'E46:')
3272 au! ModeChanged
3273
3274 autocmd TextYankPost * let v:event.operator = ''
3275 call assert_fails('normal! yy', 'E46:')
3276 au! TextYankPost
3277endfunc
3278
zeertzjqc9e8fd62022-07-26 18:12:38 +01003279" Test for ModeChanged pattern
3280func Test_mode_changes()
3281 let g:index = 0
3282 let g:mode_seq = ['n', 'i', 'n', 'v', 'V', 'i', 'ix', 'i', 'ic', 'i', 'n', 'no', 'n', 'V', 'v', 's', 'n']
3283 func! TestMode()
3284 call assert_equal(g:mode_seq[g:index], get(v:event, "old_mode"))
3285 call assert_equal(g:mode_seq[g:index + 1], get(v:event, "new_mode"))
3286 call assert_equal(mode(1), get(v:event, "new_mode"))
3287 let g:index += 1
3288 endfunc
3289
3290 au ModeChanged * :call TestMode()
3291 let g:n_to_any = 0
3292 au ModeChanged n:* let g:n_to_any += 1
3293 call feedkeys("i\<esc>vVca\<CR>\<C-X>\<C-L>\<esc>ggdG", 'tnix')
3294
3295 let g:V_to_v = 0
3296 au ModeChanged V:v let g:V_to_v += 1
3297 call feedkeys("Vv\<C-G>\<esc>", 'tnix')
3298 call assert_equal(len(filter(g:mode_seq[1:], {idx, val -> val == 'n'})), g:n_to_any)
3299 call assert_equal(1, g:V_to_v)
3300 call assert_equal(len(g:mode_seq) - 1, g:index)
3301
3302 let g:n_to_i = 0
3303 au ModeChanged n:i let g:n_to_i += 1
3304 let g:n_to_niI = 0
3305 au ModeChanged i:niI let g:n_to_niI += 1
3306 let g:niI_to_i = 0
3307 au ModeChanged niI:i let g:niI_to_i += 1
3308 let g:nany_to_i = 0
3309 au ModeChanged n*:i let g:nany_to_i += 1
3310 let g:i_to_n = 0
3311 au ModeChanged i:n let g:i_to_n += 1
3312 let g:nori_to_any = 0
3313 au ModeChanged [ni]:* let g:nori_to_any += 1
3314 let g:i_to_any = 0
3315 au ModeChanged i:* let g:i_to_any += 1
3316 let g:index = 0
3317 let g:mode_seq = ['n', 'i', 'niI', 'i', 'n']
3318 call feedkeys("a\<C-O>l\<esc>", 'tnix')
3319 call assert_equal(len(g:mode_seq) - 1, g:index)
3320 call assert_equal(1, g:n_to_i)
3321 call assert_equal(1, g:n_to_niI)
3322 call assert_equal(1, g:niI_to_i)
3323 call assert_equal(2, g:nany_to_i)
3324 call assert_equal(1, g:i_to_n)
3325 call assert_equal(2, g:i_to_any)
3326 call assert_equal(3, g:nori_to_any)
3327
3328 if has('terminal')
3329 let g:mode_seq += ['c', 'n', 't', 'nt', 'c', 'nt', 'n']
3330 call feedkeys(":term\<CR>\<C-W>N:bd!\<CR>", 'tnix')
3331 call assert_equal(len(g:mode_seq) - 1, g:index)
3332 call assert_equal(1, g:n_to_i)
3333 call assert_equal(1, g:n_to_niI)
3334 call assert_equal(1, g:niI_to_i)
3335 call assert_equal(2, g:nany_to_i)
3336 call assert_equal(1, g:i_to_n)
3337 call assert_equal(2, g:i_to_any)
3338 call assert_equal(5, g:nori_to_any)
3339 endif
3340
3341 if has('cmdwin')
3342 let g:n_to_c = 0
3343 au ModeChanged n:c let g:n_to_c += 1
3344 let g:c_to_n = 0
3345 au ModeChanged c:n let g:c_to_n += 1
3346 let g:mode_seq += ['c', 'n', 'c', 'n']
3347 call feedkeys("q:\<C-C>\<Esc>", 'tnix')
3348 call assert_equal(len(g:mode_seq) - 1, g:index)
3349 call assert_equal(2, g:n_to_c)
3350 call assert_equal(2, g:c_to_n)
3351 unlet g:n_to_c
3352 unlet g:c_to_n
3353 endif
3354
3355 au! ModeChanged
3356 delfunc TestMode
3357 unlet! g:mode_seq
3358 unlet! g:index
3359 unlet! g:n_to_any
3360 unlet! g:V_to_v
3361 unlet! g:n_to_i
3362 unlet! g:n_to_niI
3363 unlet! g:niI_to_i
3364 unlet! g:nany_to_i
3365 unlet! g:i_to_n
3366 unlet! g:nori_to_any
3367 unlet! g:i_to_any
3368endfunc
3369
3370func Test_recursive_ModeChanged()
3371 au! ModeChanged * norm 0u
3372 sil! norm 
3373 au! ModeChanged
3374endfunc
3375
3376func Test_ModeChanged_starts_visual()
3377 " This was triggering ModeChanged before setting VIsual, causing a crash.
3378 au! ModeChanged * norm 0u
3379 sil! norm 
3380
3381 au! ModeChanged
3382endfunc
Bram Moolenaar347538f2022-03-26 16:28:06 +00003383
Charlie Grovesfef44852022-04-19 16:24:12 +01003384func Test_noname_autocmd()
3385 augroup test_noname_autocmd_group
3386 autocmd!
3387 autocmd BufEnter * call add(s:li, ["BufEnter", expand("<afile>")])
3388 autocmd BufDelete * call add(s:li, ["BufDelete", expand("<afile>")])
3389 autocmd BufLeave * call add(s:li, ["BufLeave", expand("<afile>")])
3390 autocmd BufUnload * call add(s:li, ["BufUnload", expand("<afile>")])
3391 autocmd BufWipeout * call add(s:li, ["BufWipeout", expand("<afile>")])
3392 augroup END
3393
3394 let s:li = []
3395 edit foo
3396 call assert_equal([['BufUnload', ''], ['BufDelete', ''], ['BufWipeout', ''], ['BufEnter', 'foo']], s:li)
3397
3398 au! test_noname_autocmd_group
3399 augroup! test_noname_autocmd_group
3400endfunc
3401
Yegappan Lakshmanan1755a912022-05-19 10:31:47 +01003402" Test for the autocmd_get() function
3403func Test_autocmd_get()
3404 augroup TestAutoCmdFns
3405 au!
3406 autocmd BufAdd *.vim echo "bufadd-vim"
3407 autocmd BufAdd *.py echo "bufadd-py"
3408 autocmd BufHidden *.vim echo "bufhidden"
3409 augroup END
3410 augroup TestAutoCmdFns2
3411 autocmd BufAdd *.vim echo "bufadd-vim-2"
3412 autocmd BufRead *.a1b2c3 echo "bufadd-vim-2"
3413 augroup END
3414
3415 let l = autocmd_get()
3416 call assert_true(l->len() > 0)
3417
3418 " Test for getting all the autocmds in a group
3419 let expected = [
3420 \ #{cmd: 'echo "bufadd-vim"', group: 'TestAutoCmdFns',
3421 \ pattern: '*.vim', nested: v:false, once: v:false,
3422 \ event: 'BufAdd'},
3423 \ #{cmd: 'echo "bufadd-py"', group: 'TestAutoCmdFns',
3424 \ pattern: '*.py', nested: v:false, once: v:false,
3425 \ event: 'BufAdd'},
3426 \ #{cmd: 'echo "bufhidden"', group: 'TestAutoCmdFns',
3427 \ pattern: '*.vim', nested: v:false,
3428 \ once: v:false, event: 'BufHidden'}]
3429 call assert_equal(expected, autocmd_get(#{group: 'TestAutoCmdFns'}))
3430
3431 " Test for getting autocmds for all the patterns in a group
3432 call assert_equal(expected, autocmd_get(#{group: 'TestAutoCmdFns',
3433 \ event: '*'}))
3434
3435 " Test for getting autocmds for an event in a group
3436 let expected = [
3437 \ #{cmd: 'echo "bufadd-vim"', group: 'TestAutoCmdFns',
3438 \ pattern: '*.vim', nested: v:false, once: v:false,
3439 \ event: 'BufAdd'},
3440 \ #{cmd: 'echo "bufadd-py"', group: 'TestAutoCmdFns',
3441 \ pattern: '*.py', nested: v:false, once: v:false,
3442 \ event: 'BufAdd'}]
3443 call assert_equal(expected, autocmd_get(#{group: 'TestAutoCmdFns',
3444 \ event: 'BufAdd'}))
3445
3446 " Test for getting the autocmds for all the events in a group for particular
3447 " pattern
3448 call assert_equal([{'cmd': 'echo "bufadd-py"', 'group': 'TestAutoCmdFns',
3449 \ 'pattern': '*.py', 'nested': v:false, 'once': v:false,
3450 \ 'event': 'BufAdd'}],
3451 \ autocmd_get(#{group: 'TestAutoCmdFns', event: '*', pattern: '*.py'}))
3452
3453 " Test for getting the autocmds for an events in a group for particular
3454 " pattern
3455 let l = autocmd_get(#{group: 'TestAutoCmdFns', event: 'BufAdd',
3456 \ pattern: '*.vim'})
3457 call assert_equal([
3458 \ #{cmd: 'echo "bufadd-vim"', group: 'TestAutoCmdFns',
3459 \ pattern: '*.vim', nested: v:false, once: v:false,
3460 \ event: 'BufAdd'}], l)
3461
3462 " Test for getting the autocmds for a pattern in a group
3463 let l = autocmd_get(#{group: 'TestAutoCmdFns', pattern: '*.vim'})
3464 call assert_equal([
3465 \ #{cmd: 'echo "bufadd-vim"', group: 'TestAutoCmdFns',
3466 \ pattern: '*.vim', nested: v:false, once: v:false,
3467 \ event: 'BufAdd'},
3468 \ #{cmd: 'echo "bufhidden"', group: 'TestAutoCmdFns',
3469 \ pattern: '*.vim', nested: v:false,
3470 \ once: v:false, event: 'BufHidden'}], l)
3471
3472 " Test for getting the autocmds for a pattern in all the groups
3473 let l = autocmd_get(#{pattern: '*.a1b2c3'})
3474 call assert_equal([{'cmd': 'echo "bufadd-vim-2"', 'group': 'TestAutoCmdFns2',
3475 \ 'pattern': '*.a1b2c3', 'nested': v:false, 'once': v:false,
3476 \ 'event': 'BufRead'}], l)
3477
3478 " Test for getting autocmds for a pattern without any autocmds
3479 call assert_equal([], autocmd_get(#{group: 'TestAutoCmdFns',
3480 \ pattern: '*.abc'}))
3481 call assert_equal([], autocmd_get(#{group: 'TestAutoCmdFns',
3482 \ event: 'BufAdd', pattern: '*.abc'}))
3483 call assert_equal([], autocmd_get(#{group: 'TestAutoCmdFns',
3484 \ event: 'BufWipeout'}))
3485 call assert_fails("call autocmd_get(#{group: 'abc', event: 'BufAdd'})",
3486 \ 'E367:')
3487 let cmd = "echo autocmd_get(#{group: 'TestAutoCmdFns', event: 'abc'})"
3488 call assert_fails(cmd, 'E216:')
3489 call assert_fails("call autocmd_get(#{group: 'abc'})", 'E367:')
3490 call assert_fails("echo autocmd_get(#{event: 'abc'})", 'E216:')
3491
3492 augroup TestAutoCmdFns
3493 au!
3494 augroup END
3495 call assert_equal([], autocmd_get(#{group: 'TestAutoCmdFns'}))
3496
3497 " Test for nested and once autocmds
3498 augroup TestAutoCmdFns
3499 au!
3500 autocmd VimSuspend * ++nested echo "suspend"
3501 autocmd VimResume * ++once echo "resume"
3502 augroup END
3503
3504 let expected = [
3505 \ {'cmd': 'echo "suspend"', 'group': 'TestAutoCmdFns', 'pattern': '*',
3506 \ 'nested': v:true, 'once': v:false, 'event': 'VimSuspend'},
3507 \ {'cmd': 'echo "resume"', 'group': 'TestAutoCmdFns', 'pattern': '*',
3508 \ 'nested': v:false, 'once': v:true, 'event': 'VimResume'}]
3509 call assert_equal(expected, autocmd_get(#{group: 'TestAutoCmdFns'}))
3510
3511 " Test for buffer-local autocmd
3512 augroup TestAutoCmdFns
3513 au!
3514 autocmd TextYankPost <buffer> echo "textyankpost"
3515 augroup END
3516
3517 let expected = [
3518 \ {'cmd': 'echo "textyankpost"', 'group': 'TestAutoCmdFns',
3519 \ 'pattern': '<buffer=' .. bufnr() .. '>', 'nested': v:false,
3520 \ 'once': v:false, 'bufnr': bufnr(), 'event': 'TextYankPost'}]
3521 call assert_equal(expected, autocmd_get(#{group: 'TestAutoCmdFns'}))
3522
3523 augroup TestAutoCmdFns
3524 au!
3525 augroup END
3526 augroup! TestAutoCmdFns
3527 augroup TestAutoCmdFns2
3528 au!
3529 augroup END
3530 augroup! TestAutoCmdFns2
3531
3532 call assert_fails("echo autocmd_get(#{group: []})", 'E730:')
3533 call assert_fails("echo autocmd_get(#{event: {}})", 'E731:')
3534 call assert_fails("echo autocmd_get([])", 'E1206:')
3535endfunc
3536
3537" Test for the autocmd_add() function
3538func Test_autocmd_add()
3539 " Define a single autocmd in a group
3540 call autocmd_add([#{group: 'TestAcSet', event: 'BufAdd', pattern: '*.sh',
3541 \ cmd: 'echo "bufadd"', once: v:true, nested: v:true}])
3542 call assert_equal([#{cmd: 'echo "bufadd"', group: 'TestAcSet',
3543 \ pattern: '*.sh', nested: v:true, once: v:true,
3544 \ event: 'BufAdd'}], autocmd_get(#{group: 'TestAcSet'}))
3545
3546 " Define two autocmds in the same group
3547 call autocmd_delete([#{group: 'TestAcSet'}])
3548 call autocmd_add([#{group: 'TestAcSet', event: 'BufAdd', pattern: '*.sh',
3549 \ cmd: 'echo "bufadd"'},
3550 \ #{group: 'TestAcSet', event: 'BufEnter', pattern: '*.sh',
3551 \ cmd: 'echo "bufenter"'}])
3552 call assert_equal([
3553 \ #{cmd: 'echo "bufadd"', group: 'TestAcSet', pattern: '*.sh',
3554 \ nested: v:false, once: v:false, event: 'BufAdd'},
3555 \ #{cmd: 'echo "bufenter"', group: 'TestAcSet', pattern: '*.sh',
3556 \ nested: v:false, once: v:false, event: 'BufEnter'}],
3557 \ autocmd_get(#{group: 'TestAcSet'}))
3558
3559 " Define a buffer-local autocmd
3560 call autocmd_delete([#{group: 'TestAcSet'}])
3561 call autocmd_add([#{group: 'TestAcSet', event: 'CursorHold',
3562 \ bufnr: bufnr(), cmd: 'echo "cursorhold"'}])
3563 call assert_equal([
3564 \ #{cmd: 'echo "cursorhold"', group: 'TestAcSet',
3565 \ pattern: '<buffer=' .. bufnr() .. '>', nested: v:false,
3566 \ once: v:false, bufnr: bufnr(), event: 'CursorHold'}],
3567 \ autocmd_get(#{group: 'TestAcSet'}))
3568
3569 " Use an invalid buffer number
3570 call autocmd_delete([#{group: 'TestAcSet'}])
3571 call autocmd_add([#{group: 'TestAcSet', event: 'BufEnter',
3572 \ bufnr: -1, cmd: 'echo "bufenter"'}])
3573 let l = [#{group: 'TestAcSet', event: 'BufAdd', bufnr: 9999,
3574 \ cmd: 'echo "bufadd"'}]
3575 call assert_fails("echo autocmd_add(l)", 'E680:')
Yegappan Lakshmanan00e977c2022-06-01 12:31:53 +01003576 let l = [#{group: 'TestAcSet', event: 'BufAdd', bufnr: 9999,
3577 \ pattern: '*.py', cmd: 'echo "bufadd"'}]
3578 call assert_fails("echo autocmd_add(l)", 'E680:')
3579 let l = [#{group: 'TestAcSet', event: 'BufAdd', bufnr: 9999,
3580 \ pattern: ['*.py', '*.c'], cmd: 'echo "bufadd"'}]
3581 call assert_fails("echo autocmd_add(l)", 'E680:')
Yegappan Lakshmanan1755a912022-05-19 10:31:47 +01003582 let l = [#{group: 'TestAcSet', event: 'BufRead', bufnr: [],
3583 \ cmd: 'echo "bufread"'}]
3584 call assert_fails("echo autocmd_add(l)", 'E745:')
3585 call assert_equal([], autocmd_get(#{group: 'TestAcSet'}))
3586
3587 " Add two commands to the same group, event and pattern
3588 call autocmd_delete([#{group: 'TestAcSet'}])
3589 call autocmd_add([#{group: 'TestAcSet', event: 'BufUnload',
3590 \ pattern: 'abc', cmd: 'echo "cmd1"'}])
3591 call autocmd_add([#{group: 'TestAcSet', event: 'BufUnload',
3592 \ pattern: 'abc', cmd: 'echo "cmd2"'}])
3593 call assert_equal([
3594 \ #{cmd: 'echo "cmd1"', group: 'TestAcSet', pattern: 'abc',
3595 \ nested: v:false, once: v:false, event: 'BufUnload'},
3596 \ #{cmd: 'echo "cmd2"', group: 'TestAcSet', pattern: 'abc',
3597 \ nested: v:false, once: v:false, event: 'BufUnload'}],
3598 \ autocmd_get(#{group: 'TestAcSet'}))
3599
3600 " When adding a new autocmd, if the autocmd 'group' is not specified, then
3601 " the current autocmd group should be used.
3602 call autocmd_delete([#{group: 'TestAcSet'}])
3603 augroup TestAcSet
3604 call autocmd_add([#{event: 'BufHidden', pattern: 'abc', cmd: 'echo "abc"'}])
3605 augroup END
3606 call assert_equal([
3607 \ #{cmd: 'echo "abc"', group: 'TestAcSet', pattern: 'abc',
3608 \ nested: v:false, once: v:false, event: 'BufHidden'}],
3609 \ autocmd_get(#{group: 'TestAcSet'}))
3610
Yegappan Lakshmanan971f6822022-05-24 11:40:11 +01003611 " Test for replacing a cmd for an event in a group
3612 call autocmd_delete([#{group: 'TestAcSet'}])
3613 call autocmd_add([#{replace: v:true, group: 'TestAcSet', event: 'BufEnter',
3614 \ pattern: '*.py', cmd: 'echo "bufenter"'}])
3615 call autocmd_add([#{replace: v:true, group: 'TestAcSet', event: 'BufEnter',
3616 \ pattern: '*.py', cmd: 'echo "bufenter"'}])
3617 call assert_equal([
3618 \ #{cmd: 'echo "bufenter"', group: 'TestAcSet', pattern: '*.py',
3619 \ nested: v:false, once: v:false, event: 'BufEnter'}],
3620 \ autocmd_get(#{group: 'TestAcSet'}))
3621
3622 " Test for adding a command for an unsupported autocmd event
Yegappan Lakshmanan1755a912022-05-19 10:31:47 +01003623 let l = [#{group: 'TestAcSet', event: 'abc', pattern: '*.sh',
3624 \ cmd: 'echo "bufadd"'}]
3625 call assert_fails('call autocmd_add(l)', 'E216:')
3626
Yegappan Lakshmanane0ff3a72022-05-27 18:05:33 +01003627 " Test for using a list of events and patterns
3628 call autocmd_delete([#{group: 'TestAcSet'}])
3629 let l = [#{group: 'TestAcSet', event: ['BufEnter', 'BufLeave'],
3630 \ pattern: ['*.py', '*.sh'], cmd: 'echo "bufcmds"'}]
3631 call autocmd_add(l)
3632 call assert_equal([
3633 \ #{cmd: 'echo "bufcmds"', group: 'TestAcSet', pattern: '*.py',
3634 \ nested: v:false, once: v:false, event: 'BufEnter'},
3635 \ #{cmd: 'echo "bufcmds"', group: 'TestAcSet', pattern: '*.sh',
3636 \ nested: v:false, once: v:false, event: 'BufEnter'},
3637 \ #{cmd: 'echo "bufcmds"', group: 'TestAcSet', pattern: '*.py',
3638 \ nested: v:false, once: v:false, event: 'BufLeave'},
3639 \ #{cmd: 'echo "bufcmds"', group: 'TestAcSet', pattern: '*.sh',
3640 \ nested: v:false, once: v:false, event: 'BufLeave'}],
3641 \ autocmd_get(#{group: 'TestAcSet'}))
3642
3643 " Test for invalid values for 'event' item
3644 call autocmd_delete([#{group: 'TestAcSet'}])
3645 let l = [#{group: 'TestAcSet', event: test_null_string(),
3646 \ pattern: "*.py", cmd: 'echo "bufcmds"'}]
3647 call assert_fails('call autocmd_add(l)', 'E928:')
3648 let l = [#{group: 'TestAcSet', event: test_null_list(),
3649 \ pattern: "*.py", cmd: 'echo "bufcmds"'}]
3650 call assert_fails('call autocmd_add(l)', 'E714:')
3651 let l = [#{group: 'TestAcSet', event: {},
3652 \ pattern: "*.py", cmd: 'echo "bufcmds"'}]
3653 call assert_fails('call autocmd_add(l)', 'E777:')
3654 let l = [#{group: 'TestAcSet', event: [{}],
3655 \ pattern: "*.py", cmd: 'echo "bufcmds"'}]
3656 call assert_fails('call autocmd_add(l)', 'E928:')
3657 let l = [#{group: 'TestAcSet', event: [test_null_string()],
3658 \ pattern: "*.py", cmd: 'echo "bufcmds"'}]
3659 call assert_fails('call autocmd_add(l)', 'E928:')
3660 let l = [#{group: 'TestAcSet', event: 'BufEnter,BufLeave',
3661 \ pattern: '*.py', cmd: 'echo "bufcmds"'}]
3662 call assert_fails('call autocmd_add(l)', 'E216:')
3663 let l = [#{group: 'TestAcSet', event: [],
3664 \ pattern: "*.py", cmd: 'echo "bufcmds"'}]
3665 call autocmd_add(l)
3666 let l = [#{group: 'TestAcSet', event: [""],
3667 \ pattern: "*.py", cmd: 'echo "bufcmds"'}]
3668 call assert_fails('call autocmd_add(l)', 'E216:')
3669 let l = [#{group: 'TestAcSet', event: "",
3670 \ pattern: "*.py", cmd: 'echo "bufcmds"'}]
3671 call autocmd_add(l)
3672 call assert_equal([], autocmd_get(#{group: 'TestAcSet'}))
3673
3674 " Test for invalid values for 'pattern' item
3675 let l = [#{group: 'TestAcSet', event: "BufEnter",
3676 \ pattern: test_null_string(), cmd: 'echo "bufcmds"'}]
Yegappan Lakshmanan00e977c2022-06-01 12:31:53 +01003677 call assert_fails('call autocmd_add(l)', 'E928:')
Yegappan Lakshmanane0ff3a72022-05-27 18:05:33 +01003678 let l = [#{group: 'TestAcSet', event: "BufEnter",
3679 \ pattern: test_null_list(), cmd: 'echo "bufcmds"'}]
3680 call assert_fails('call autocmd_add(l)', 'E714:')
3681 let l = [#{group: 'TestAcSet', event: "BufEnter",
3682 \ pattern: {}, cmd: 'echo "bufcmds"'}]
3683 call assert_fails('call autocmd_add(l)', 'E777:')
3684 let l = [#{group: 'TestAcSet', event: "BufEnter",
3685 \ pattern: [{}], cmd: 'echo "bufcmds"'}]
3686 call assert_fails('call autocmd_add(l)', 'E928:')
3687 let l = [#{group: 'TestAcSet', event: "BufEnter",
3688 \ pattern: [test_null_string()], cmd: 'echo "bufcmds"'}]
3689 call assert_fails('call autocmd_add(l)', 'E928:')
3690 let l = [#{group: 'TestAcSet', event: "BufEnter",
3691 \ pattern: [], cmd: 'echo "bufcmds"'}]
3692 call autocmd_add(l)
3693 let l = [#{group: 'TestAcSet', event: "BufEnter",
3694 \ pattern: [""], cmd: 'echo "bufcmds"'}]
3695 call autocmd_add(l)
3696 let l = [#{group: 'TestAcSet', event: "BufEnter",
3697 \ pattern: "", cmd: 'echo "bufcmds"'}]
3698 call autocmd_add(l)
3699 call assert_equal([], autocmd_get(#{group: 'TestAcSet'}))
3700
3701 let l = [#{group: 'TestAcSet', event: 'BufEnter,abc,BufLeave',
3702 \ pattern: '*.py', cmd: 'echo "bufcmds"'}]
3703 call assert_fails('call autocmd_add(l)', 'E216:')
3704
Yegappan Lakshmanan1755a912022-05-19 10:31:47 +01003705 call assert_fails("call autocmd_add({})", 'E1211:')
3706 call assert_equal(v:false, autocmd_add(test_null_list()))
3707 call assert_true(autocmd_add([[]]))
3708 call assert_true(autocmd_add([test_null_dict()]))
3709
3710 augroup TestAcSet
3711 au!
3712 augroup END
3713
3714 call autocmd_add([#{group: 'TestAcSet'}])
3715 call autocmd_add([#{group: 'TestAcSet', event: 'BufAdd'}])
3716 call autocmd_add([#{group: 'TestAcSet', pat: '*.sh'}])
3717 call autocmd_add([#{group: 'TestAcSet', cmd: 'echo "a"'}])
3718 call autocmd_add([#{group: 'TestAcSet', event: 'BufAdd', pat: '*.sh'}])
3719 call autocmd_add([#{group: 'TestAcSet', event: 'BufAdd', cmd: 'echo "a"'}])
3720 call autocmd_add([#{group: 'TestAcSet', pat: '*.sh', cmd: 'echo "a"'}])
3721 call assert_equal([], autocmd_get(#{group: 'TestAcSet'}))
3722
3723 augroup! TestAcSet
3724endfunc
3725
3726" Test for deleting autocmd events and groups
3727func Test_autocmd_delete()
3728 " Delete an event in an autocmd group
3729 augroup TestAcSet
3730 au!
3731 au BufAdd *.sh echo "bufadd"
3732 au BufEnter *.sh echo "bufenter"
3733 augroup END
3734 call autocmd_delete([#{group: 'TestAcSet', event: 'BufAdd'}])
3735 call assert_equal([#{cmd: 'echo "bufenter"', group: 'TestAcSet',
3736 \ pattern: '*.sh', nested: v:false, once: v:false,
3737 \ event: 'BufEnter'}], autocmd_get(#{group: 'TestAcSet'}))
3738
3739 " Delete all the events in an autocmd group
3740 augroup TestAcSet
3741 au BufAdd *.sh echo "bufadd"
3742 augroup END
3743 call autocmd_delete([#{group: 'TestAcSet', event: '*'}])
3744 call assert_equal([], autocmd_get(#{group: 'TestAcSet'}))
3745
3746 " Delete a non-existing autocmd group
3747 call assert_fails("call autocmd_delete([#{group: 'abc'}])", 'E367:')
3748 " Delete a non-existing autocmd event
3749 let l = [#{group: 'TestAcSet', event: 'abc'}]
3750 call assert_fails("call autocmd_delete(l)", 'E216:')
3751 " Delete a non-existing autocmd pattern
3752 let l = [#{group: 'TestAcSet', event: 'BufAdd', pat: 'abc'}]
3753 call assert_true(autocmd_delete(l))
Yegappan Lakshmanan00e977c2022-06-01 12:31:53 +01003754 " Delete an autocmd for a non-existing buffer
3755 let l = [#{event: '*', bufnr: 9999, cmd: 'echo "x"'}]
3756 call assert_fails('call autocmd_delete(l)', 'E680:')
Yegappan Lakshmanan1755a912022-05-19 10:31:47 +01003757
3758 " Delete an autocmd group
3759 augroup TestAcSet
3760 au!
3761 au BufAdd *.sh echo "bufadd"
3762 au BufEnter *.sh echo "bufenter"
3763 augroup END
3764 call autocmd_delete([#{group: 'TestAcSet'}])
3765 call assert_fails("call autocmd_get(#{group: 'TestAcSet'})", 'E367:')
3766
3767 call assert_true(autocmd_delete([[]]))
3768 call assert_true(autocmd_delete([test_null_dict()]))
3769endfunc
3770
Bram Moolenaarbc2b71d2020-02-17 21:33:30 +01003771" vim: shiftwidth=2 sts=2 expandtab