blob: 4b0928517a734b93527eb4defc9937b0bf95c5d3 [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 call delete('Xdir', 'd')
685 au! BufEnter
Bram Moolenaara9b5b852022-08-26 13:16:20 +0100686
687 " Editing a "nofile" buffer doesn't read the file but does trigger BufEnter
688 " for historic reasons.
689 new somefile
690 set buftype=nofile
691 au BufEnter somefile call setline(1, 'some text')
692 edit
693 call assert_equal('some text', getline(1))
694
695 bwipe!
696 au! BufEnter
Bram Moolenaare13b9af2017-01-13 22:01:02 +0100697endfunc
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100698
699" Closing a window might cause an endless loop
700" E814 for older Vims
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200701func Test_autocmd_bufwipe_in_SessLoadPost()
Bram Moolenaar1d68d9b2017-10-13 22:33:32 +0200702 edit Xtest
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100703 tabnew
Bram Moolenaar1d68d9b2017-10-13 22:33:32 +0200704 file Xsomething
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100705 set noswapfile
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100706 mksession!
707
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200708 let content =<< trim [CODE]
Bram Moolenaar62cd26a2020-10-11 20:08:44 +0200709 call test_override('ui_delay', 10)
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200710 set nocp noswapfile
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100711 let v:swapchoice = "e"
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200712 augroup test_autocmd_sessionload
713 autocmd!
714 autocmd SessionLoadPost * exe bufnr("Xsomething") . "bw!"
715 augroup END
716
717 func WriteErrors()
718 call writefile([execute("messages")], "Xerrors")
719 endfunc
720 au VimLeave * call WriteErrors()
721 [CODE]
722
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100723 call writefile(content, 'Xvimrc')
Bram Moolenaar93344c22019-08-14 21:12:05 +0200724 call system(GetVimCommand('Xvimrc') .. ' --not-a-term --noplugins -S Session.vim -c cq')
Bram Moolenaare94260f2017-03-21 15:50:12 +0100725 let errors = join(readfile('Xerrors'))
Bram Moolenaare2e40752020-09-04 21:18:46 +0200726 call assert_match('E814:', errors)
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100727
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100728 set swapfile
Bram Moolenaare94260f2017-03-21 15:50:12 +0100729 for file in ['Session.vim', 'Xvimrc', 'Xerrors']
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100730 call delete(file)
731 endfor
732endfunc
733
Bram Moolenaar797e63b2021-01-15 16:22:52 +0100734" Using :blast and :ball for many events caused a crash, because b_nwindows was
735" not incremented correctly.
736func Test_autocmd_blast_badd()
737 let content =<< trim [CODE]
738 au BufNew,BufAdd,BufWinEnter,BufEnter,BufLeave,BufWinLeave,BufUnload,VimEnter foo* blast
739 edit foo1
740 au BufNew,BufAdd,BufWinEnter,BufEnter,BufLeave,BufWinLeave,BufUnload,VimEnter foo* ball
741 edit foo2
742 call writefile(['OK'], 'Xerrors')
743 qall
744 [CODE]
745
746 call writefile(content, 'XblastBall')
747 call system(GetVimCommand() .. ' --clean -S XblastBall')
748 call assert_match('OK', readfile('Xerrors')->join())
749
750 call delete('XblastBall')
751 call delete('Xerrors')
752endfunc
753
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100754" SEGV occurs in older versions.
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200755func Test_autocmd_bufwipe_in_SessLoadPost2()
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100756 tabnew
757 set noswapfile
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100758 mksession!
759
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200760 let content =<< trim [CODE]
761 set nocp noswapfile
762 function! DeleteInactiveBufs()
763 tabfirst
764 let tabblist = []
765 for i in range(1, tabpagenr(''$''))
766 call extend(tabblist, tabpagebuflist(i))
767 endfor
768 for b in range(1, bufnr(''$''))
769 if bufexists(b) && buflisted(b) && (index(tabblist, b) == -1 || bufname(b) =~# ''^$'')
770 exec ''bwipeout '' . b
771 endif
772 endfor
773 echomsg "SessionLoadPost DONE"
774 endfunction
775 au SessionLoadPost * call DeleteInactiveBufs()
776
777 func WriteErrors()
778 call writefile([execute("messages")], "Xerrors")
779 endfunc
780 au VimLeave * call WriteErrors()
781 [CODE]
782
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100783 call writefile(content, 'Xvimrc')
Bram Moolenaar93344c22019-08-14 21:12:05 +0200784 call system(GetVimCommand('Xvimrc') .. ' --not-a-term --noplugins -S Session.vim -c cq')
Bram Moolenaare94260f2017-03-21 15:50:12 +0100785 let errors = join(readfile('Xerrors'))
786 " This probably only ever matches on unix.
787 call assert_notmatch('Caught deadly signal SEGV', errors)
788 call assert_match('SessionLoadPost DONE', errors)
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100789
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100790 set swapfile
Bram Moolenaare94260f2017-03-21 15:50:12 +0100791 for file in ['Session.vim', 'Xvimrc', 'Xerrors']
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100792 call delete(file)
793 endfor
794endfunc
Bram Moolenaarfaf29d72017-07-09 11:07:16 +0200795
796func Test_empty_doau()
797 doau \|
798endfunc
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200799
800func s:AutoCommandOptionSet(match)
Bram Moolenaard7c96872019-06-15 17:12:48 +0200801 let template = "Option: <%s>, OldVal: <%s>, OldValLocal: <%s>, OldValGlobal: <%s>, NewVal: <%s>, Scope: <%s>, Command: <%s>\n"
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200802 let item = remove(g:options, 0)
Bram Moolenaard7c96872019-06-15 17:12:48 +0200803 let expected = printf(template, item[0], item[1], item[2], item[3], item[4], item[5], item[6])
804 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 +0200805 let g:opt = [expected, actual]
806 "call assert_equal(expected, actual)
807endfunc
808
809func Test_OptionSet()
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +0200810 CheckOption autochdir
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200811
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +0200812 badd test_autocmd.vim
813
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200814 call test_override('starting', 1)
815 set nocp
816 au OptionSet * :call s:AutoCommandOptionSet(expand("<amatch>"))
817
818 " 1: Setting number option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100819 let g:options = [['number', 0, 0, 0, 1, 'global', 'set']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200820 set nu
821 call assert_equal([], g:options)
822 call assert_equal(g:opt[0], g:opt[1])
823
824 " 2: Setting local number option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100825 let g:options = [['number', 1, 1, '', 0, 'local', 'setlocal']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200826 setlocal nonu
827 call assert_equal([], g:options)
828 call assert_equal(g:opt[0], g:opt[1])
829
830 " 3: Setting global number option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100831 let g:options = [['number', 1, '', 1, 0, 'global', 'setglobal']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200832 setglobal nonu
833 call assert_equal([], g:options)
834 call assert_equal(g:opt[0], g:opt[1])
835
836 " 4: Setting local autoindent option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100837 let g:options = [['autoindent', 0, 0, '', 1, 'local', 'setlocal']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200838 setlocal ai
839 call assert_equal([], g:options)
840 call assert_equal(g:opt[0], g:opt[1])
841
842 " 5: Setting global autoindent option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100843 let g:options = [['autoindent', 0, '', 0, 1, 'global', 'setglobal']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200844 setglobal ai
845 call assert_equal([], g:options)
846 call assert_equal(g:opt[0], g:opt[1])
847
848 " 6: Setting global autoindent option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100849 let g:options = [['autoindent', 1, 1, 1, 0, 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +0200850 set ai!
851 call assert_equal([], g:options)
852 call assert_equal(g:opt[0], g:opt[1])
853
854 " 6a: Setting global autoindent option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100855 let g:options = [['autoindent', 1, 1, 0, 0, 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +0200856 noa setlocal ai
857 noa setglobal noai
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200858 set ai!
859 call assert_equal([], g:options)
860 call assert_equal(g:opt[0], g:opt[1])
861
862 " Should not print anything, use :noa
863 " 7: don't trigger OptionSet"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100864 let g:options = [['invalid', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200865 noa set nonu
Bram Moolenaard7c96872019-06-15 17:12:48 +0200866 call assert_equal([['invalid', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid']], g:options)
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200867 call assert_equal(g:opt[0], g:opt[1])
868
869 " 8: Setting several global list and number option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100870 let g:options = [['list', 0, 0, 0, 1, 'global', 'set'], ['number', 0, 0, 0, 1, 'global', 'set']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200871 set list nu
872 call assert_equal([], g:options)
873 call assert_equal(g:opt[0], g:opt[1])
874
875 " 9: don't trigger OptionSet"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100876 let g:options = [['invalid', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid'], ['invalid', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200877 noa set nolist nonu
Bram Moolenaard7c96872019-06-15 17:12:48 +0200878 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 +0200879 call assert_equal(g:opt[0], g:opt[1])
880
881 " 10: Setting global acd"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100882 let g:options = [['autochdir', 0, 0, '', 1, 'local', 'setlocal']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200883 setlocal acd
884 call assert_equal([], g:options)
885 call assert_equal(g:opt[0], g:opt[1])
886
887 " 11: Setting global autoread (also sets local value)"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100888 let g:options = [['autoread', 0, 0, 0, 1, 'global', 'set']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200889 set ar
890 call assert_equal([], g:options)
891 call assert_equal(g:opt[0], g:opt[1])
892
893 " 12: Setting local autoread"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100894 let g:options = [['autoread', 1, 1, '', 1, 'local', 'setlocal']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200895 setlocal ar
896 call assert_equal([], g:options)
897 call assert_equal(g:opt[0], g:opt[1])
898
899 " 13: Setting global autoread"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100900 let g:options = [['autoread', 1, '', 1, 0, 'global', 'setglobal']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200901 setglobal invar
902 call assert_equal([], g:options)
903 call assert_equal(g:opt[0], g:opt[1])
904
905 " 14: Setting option backspace through :let"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100906 let g:options = [['backspace', '', '', '', 'eol,indent,start', 'global', 'set']]
907 let &bs = "eol,indent,start"
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200908 call assert_equal([], g:options)
909 call assert_equal(g:opt[0], g:opt[1])
910
911 " 15: Setting option backspace through setbufvar()"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100912 let g:options = [['backup', 0, 0, '', 1, 'local', 'setlocal']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200913 " try twice, first time, shouldn't trigger because option name is invalid,
914 " second time, it should trigger
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +0200915 let bnum = bufnr('%')
Bram Moolenaare2e40752020-09-04 21:18:46 +0200916 call assert_fails("call setbufvar(bnum, '&l:bk', 1)", 'E355:')
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200917 " should trigger, use correct option name
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +0200918 call setbufvar(bnum, '&backup', 1)
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200919 call assert_equal([], g:options)
920 call assert_equal(g:opt[0], g:opt[1])
921
922 " 16: Setting number option using setwinvar"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100923 let g:options = [['number', 0, 0, '', 1, 'local', 'setlocal']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200924 call setwinvar(0, '&number', 1)
925 call assert_equal([], g:options)
926 call assert_equal(g:opt[0], g:opt[1])
927
928 " 17: Setting key option, shouldn't trigger"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100929 let g:options = [['key', 'invalid', 'invalid1', 'invalid2', 'invalid3', 'invalid4', 'invalid5']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200930 setlocal key=blah
931 setlocal key=
Bram Moolenaard7c96872019-06-15 17:12:48 +0200932 call assert_equal([['key', 'invalid', 'invalid1', 'invalid2', 'invalid3', 'invalid4', 'invalid5']], g:options)
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200933 call assert_equal(g:opt[0], g:opt[1])
934
Bram Moolenaard7c96872019-06-15 17:12:48 +0200935
936 " 18a: Setting string global option"
937 let oldval = &backupext
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100938 let g:options = [['backupext', oldval, oldval, oldval, 'foo', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +0200939 set backupext=foo
940 call assert_equal([], g:options)
941 call assert_equal(g:opt[0], g:opt[1])
942
943 " 18b: Resetting string global option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100944 let g:options = [['backupext', 'foo', 'foo', 'foo', oldval, 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +0200945 set backupext&
946 call assert_equal([], g:options)
947 call assert_equal(g:opt[0], g:opt[1])
948
949 " 18c: Setting global string global option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100950 let g:options = [['backupext', oldval, '', oldval, 'bar', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +0200951 setglobal backupext=bar
952 call assert_equal([], g:options)
953 call assert_equal(g:opt[0], g:opt[1])
954
955 " 18d: Setting local string global option"
956 " As this is a global option this sets the global value even though
957 " :setlocal is used!
958 noa set backupext& " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100959 let g:options = [['backupext', oldval, oldval, '', 'baz', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +0200960 setlocal backupext=baz
961 call assert_equal([], g:options)
962 call assert_equal(g:opt[0], g:opt[1])
963
964 " 18e: Setting again string global option"
965 noa setglobal backupext=ext_global " Reset global and local value (without triggering autocmd)
966 noa setlocal backupext=ext_local " Sets the global(!) value!
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100967 let g:options = [['backupext', 'ext_local', 'ext_local', 'ext_local', 'fuu', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +0200968 set backupext=fuu
969 call assert_equal([], g:options)
970 call assert_equal(g:opt[0], g:opt[1])
971
972
zeertzjqb811de52021-10-21 10:50:44 +0100973 " 19a: Setting string global-local (to buffer) option"
Bram Moolenaar8efa0262017-08-20 15:47:20 +0200974 let oldval = &tags
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100975 let g:options = [['tags', oldval, oldval, oldval, 'tagpath', 'global', 'set']]
Bram Moolenaar8efa0262017-08-20 15:47:20 +0200976 set tags=tagpath
977 call assert_equal([], g:options)
978 call assert_equal(g:opt[0], g:opt[1])
979
zeertzjqb811de52021-10-21 10:50:44 +0100980 " 19b: Resetting string global-local (to buffer) option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100981 let g:options = [['tags', 'tagpath', 'tagpath', 'tagpath', oldval, 'global', 'set']]
Bram Moolenaar8efa0262017-08-20 15:47:20 +0200982 set tags&
983 call assert_equal([], g:options)
984 call assert_equal(g:opt[0], g:opt[1])
985
zeertzjqb811de52021-10-21 10:50:44 +0100986 " 19c: Setting global string global-local (to buffer) option "
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100987 let g:options = [['tags', oldval, '', oldval, 'tagpath1', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +0200988 setglobal tags=tagpath1
989 call assert_equal([], g:options)
990 call assert_equal(g:opt[0], g:opt[1])
991
zeertzjqb811de52021-10-21 10:50:44 +0100992 " 19d: Setting local string global-local (to buffer) option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100993 let g:options = [['tags', 'tagpath1', 'tagpath1', '', 'tagpath2', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +0200994 setlocal tags=tagpath2
995 call assert_equal([], g:options)
996 call assert_equal(g:opt[0], g:opt[1])
997
zeertzjqb811de52021-10-21 10:50:44 +0100998 " 19e: Setting again string global-local (to buffer) option"
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 setglobal tags=tag_global " Reset global and local value (without triggering autocmd)
1002 noa setlocal tags=tag_local
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001003 let g:options = [['tags', 'tag_global', 'tag_local', '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
zeertzjqb811de52021-10-21 10:50:44 +01001008 " 19f: Setting string global-local (to buffer) option to an empty string"
1009 " Note: v:option_old is the old global value for global-local string options
Bram Moolenaard7c96872019-06-15 17:12:48 +02001010 " but the old local value for all other kinds of options.
1011 noa set tags=tag_global " Reset global and local value (without triggering autocmd)
1012 noa setlocal tags= " empty string
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001013 let g:options = [['tags', 'tag_global', '', 'tag_global', 'tagpath', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001014 set tags=tagpath
1015 call assert_equal([], g:options)
1016 call assert_equal(g:opt[0], g:opt[1])
1017
1018
1019 " 20a: Setting string local (to buffer) option"
1020 let oldval = &spelllang
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001021 let g:options = [['spelllang', oldval, oldval, oldval, 'elvish,klingon', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001022 set spelllang=elvish,klingon
1023 call assert_equal([], g:options)
1024 call assert_equal(g:opt[0], g:opt[1])
1025
1026 " 20b: Resetting string local (to buffer) option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001027 let g:options = [['spelllang', 'elvish,klingon', 'elvish,klingon', 'elvish,klingon', oldval, 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001028 set spelllang&
1029 call assert_equal([], g:options)
1030 call assert_equal(g:opt[0], g:opt[1])
1031
1032 " 20c: Setting global string local (to buffer) option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001033 let g:options = [['spelllang', oldval, '', oldval, 'elvish', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001034 setglobal spelllang=elvish
1035 call assert_equal([], g:options)
1036 call assert_equal(g:opt[0], g:opt[1])
1037
1038 " 20d: Setting local string local (to buffer) option"
1039 noa set spelllang& " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001040 let g:options = [['spelllang', oldval, oldval, '', 'klingon', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001041 setlocal spelllang=klingon
1042 call assert_equal([], g:options)
1043 call assert_equal(g:opt[0], g:opt[1])
1044
1045 " 20e: Setting again string local (to buffer) option"
zeertzjqb811de52021-10-21 10:50:44 +01001046 " Note: v:option_old is the old global value for global-local string options
Bram Moolenaard7c96872019-06-15 17:12:48 +02001047 " but the old local value for all other kinds of options.
1048 noa setglobal spelllang=spellglobal " Reset global and local value (without triggering autocmd)
1049 noa setlocal spelllang=spelllocal
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001050 let g:options = [['spelllang', 'spelllocal', 'spelllocal', 'spellglobal', 'foo', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001051 set spelllang=foo
1052 call assert_equal([], g:options)
1053 call assert_equal(g:opt[0], g:opt[1])
1054
1055
zeertzjqb811de52021-10-21 10:50:44 +01001056 " 21a: Setting string global-local (to window) option"
Bram Moolenaard7c96872019-06-15 17:12:48 +02001057 let oldval = &statusline
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001058 let g:options = [['statusline', oldval, oldval, oldval, 'foo', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001059 set statusline=foo
1060 call assert_equal([], g:options)
1061 call assert_equal(g:opt[0], g:opt[1])
1062
zeertzjqb811de52021-10-21 10:50:44 +01001063 " 21b: Resetting string global-local (to window) option"
1064 " Note: v:option_old is the old global value for global-local string options
Bram Moolenaard7c96872019-06-15 17:12:48 +02001065 " but the old local value for all other kinds of options.
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001066 let g:options = [['statusline', 'foo', 'foo', 'foo', oldval, 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001067 set statusline&
1068 call assert_equal([], g:options)
1069 call assert_equal(g:opt[0], g:opt[1])
1070
zeertzjqb811de52021-10-21 10:50:44 +01001071 " 21c: Setting global string global-local (to window) option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001072 let g:options = [['statusline', oldval, '', oldval, 'bar', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001073 setglobal statusline=bar
1074 call assert_equal([], g:options)
1075 call assert_equal(g:opt[0], g:opt[1])
1076
zeertzjqb811de52021-10-21 10:50:44 +01001077 " 21d: Setting local string global-local (to window) option"
Bram Moolenaard7c96872019-06-15 17:12:48 +02001078 noa set statusline& " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001079 let g:options = [['statusline', oldval, oldval, '', 'baz', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001080 setlocal statusline=baz
1081 call assert_equal([], g:options)
1082 call assert_equal(g:opt[0], g:opt[1])
1083
zeertzjqb811de52021-10-21 10:50:44 +01001084 " 21e: Setting again string global-local (to window) option"
1085 " Note: v:option_old is the old global value for global-local string options
Bram Moolenaard7c96872019-06-15 17:12:48 +02001086 " but the old local value for all other kinds of options.
1087 noa setglobal statusline=bar " Reset global and local value (without triggering autocmd)
1088 noa setlocal statusline=baz
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001089 let g:options = [['statusline', 'bar', 'baz', 'bar', 'foo', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001090 set statusline=foo
1091 call assert_equal([], g:options)
1092 call assert_equal(g:opt[0], g:opt[1])
1093
1094
1095 " 22a: Setting string local (to window) option"
1096 let oldval = &foldignore
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001097 let g:options = [['foldignore', oldval, oldval, oldval, 'fo', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001098 set foldignore=fo
1099 call assert_equal([], g:options)
1100 call assert_equal(g:opt[0], g:opt[1])
1101
1102 " 22b: Resetting string local (to window) option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001103 let g:options = [['foldignore', 'fo', 'fo', 'fo', oldval, 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001104 set foldignore&
1105 call assert_equal([], g:options)
1106 call assert_equal(g:opt[0], g:opt[1])
1107
1108 " 22c: Setting global string local (to window) option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001109 let g:options = [['foldignore', oldval, '', oldval, 'bar', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001110 setglobal foldignore=bar
1111 call assert_equal([], g:options)
1112 call assert_equal(g:opt[0], g:opt[1])
1113
1114 " 22d: Setting local string local (to window) option"
1115 noa set foldignore& " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001116 let g:options = [['foldignore', oldval, oldval, '', 'baz', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001117 setlocal foldignore=baz
1118 call assert_equal([], g:options)
1119 call assert_equal(g:opt[0], g:opt[1])
1120
1121 " 22e: Setting again string local (to window) option"
1122 noa setglobal foldignore=glob " Reset global and local value (without triggering autocmd)
1123 noa setlocal foldignore=loc
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001124 let g:options = [['foldignore', 'loc', 'loc', 'glob', 'fo', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001125 set foldignore=fo
1126 call assert_equal([], g:options)
1127 call assert_equal(g:opt[0], g:opt[1])
1128
1129
zeertzjqb811de52021-10-21 10:50:44 +01001130 " 23a: Setting global number global option"
Bram Moolenaard7c96872019-06-15 17:12:48 +02001131 noa setglobal cmdheight=8 " Reset global and local value (without triggering autocmd)
1132 noa setlocal cmdheight=1 " Sets the global(!) value!
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001133 let g:options = [['cmdheight', '1', '', '1', '2', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001134 setglobal cmdheight=2
1135 call assert_equal([], g:options)
1136 call assert_equal(g:opt[0], g:opt[1])
1137
1138 " 23b: Setting local number global option"
1139 noa setglobal cmdheight=8 " Reset global and local value (without triggering autocmd)
1140 noa setlocal cmdheight=1 " Sets the global(!) value!
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001141 let g:options = [['cmdheight', '1', '1', '', '2', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001142 setlocal cmdheight=2
1143 call assert_equal([], g:options)
1144 call assert_equal(g:opt[0], g:opt[1])
1145
1146 " 23c: Setting again number global option"
1147 noa setglobal cmdheight=8 " Reset global and local value (without triggering autocmd)
1148 noa setlocal cmdheight=1 " Sets the global(!) value!
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001149 let g:options = [['cmdheight', '1', '1', '1', '2', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001150 set cmdheight=2
1151 call assert_equal([], g:options)
1152 call assert_equal(g:opt[0], g:opt[1])
1153
1154 " 23d: Setting again number global option"
1155 noa set cmdheight=8 " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001156 let g:options = [['cmdheight', '8', '8', '8', '2', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001157 set cmdheight=2
1158 call assert_equal([], g:options)
1159 call assert_equal(g:opt[0], g:opt[1])
1160
1161
1162 " 24a: Setting global number global-local (to buffer) option"
1163 noa setglobal undolevels=8 " Reset global and local value (without triggering autocmd)
1164 noa setlocal undolevels=1
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001165 let g:options = [['undolevels', '8', '', '8', '2', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001166 setglobal undolevels=2
1167 call assert_equal([], g:options)
1168 call assert_equal(g:opt[0], g:opt[1])
1169
1170 " 24b: Setting local number global-local (to buffer) option"
1171 noa setglobal undolevels=8 " Reset global and local value (without triggering autocmd)
1172 noa setlocal undolevels=1
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001173 let g:options = [['undolevels', '1', '1', '', '2', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001174 setlocal undolevels=2
1175 call assert_equal([], g:options)
1176 call assert_equal(g:opt[0], g:opt[1])
1177
1178 " 24c: Setting again number global-local (to buffer) option"
1179 noa setglobal undolevels=8 " Reset global and local value (without triggering autocmd)
1180 noa setlocal undolevels=1
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001181 let g:options = [['undolevels', '1', '1', '8', '2', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001182 set undolevels=2
1183 call assert_equal([], g:options)
1184 call assert_equal(g:opt[0], g:opt[1])
1185
1186 " 24d: Setting again global number global-local (to buffer) option"
1187 noa set undolevels=8 " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001188 let g:options = [['undolevels', '8', '8', '8', '2', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001189 set undolevels=2
1190 call assert_equal([], g:options)
1191 call assert_equal(g:opt[0], g:opt[1])
1192
1193
1194 " 25a: Setting global number local (to buffer) option"
1195 noa setglobal wrapmargin=8 " Reset global and local value (without triggering autocmd)
1196 noa setlocal wrapmargin=1
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001197 let g:options = [['wrapmargin', '8', '', '8', '2', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001198 setglobal wrapmargin=2
1199 call assert_equal([], g:options)
1200 call assert_equal(g:opt[0], g:opt[1])
1201
1202 " 25b: Setting local number local (to buffer) option"
1203 noa setglobal wrapmargin=8 " Reset global and local value (without triggering autocmd)
1204 noa setlocal wrapmargin=1
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001205 let g:options = [['wrapmargin', '1', '1', '', '2', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001206 setlocal wrapmargin=2
1207 call assert_equal([], g:options)
1208 call assert_equal(g:opt[0], g:opt[1])
1209
1210 " 25c: Setting again number local (to buffer) option"
1211 noa setglobal wrapmargin=8 " Reset global and local value (without triggering autocmd)
1212 noa setlocal wrapmargin=1
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001213 let g:options = [['wrapmargin', '1', '1', '8', '2', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001214 set wrapmargin=2
1215 call assert_equal([], g:options)
1216 call assert_equal(g:opt[0], g:opt[1])
1217
1218 " 25d: Setting again global number local (to buffer) option"
1219 noa set wrapmargin=8 " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001220 let g:options = [['wrapmargin', '8', '8', '8', '2', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001221 set wrapmargin=2
1222 call assert_equal([], g:options)
1223 call assert_equal(g:opt[0], g:opt[1])
1224
1225
1226 " 26: Setting number global-local (to window) option.
1227 " Such option does currently not exist.
1228
1229
1230 " 27a: Setting global number local (to window) option"
1231 noa setglobal foldcolumn=8 " Reset global and local value (without triggering autocmd)
1232 noa setlocal foldcolumn=1
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001233 let g:options = [['foldcolumn', '8', '', '8', '2', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001234 setglobal foldcolumn=2
1235 call assert_equal([], g:options)
1236 call assert_equal(g:opt[0], g:opt[1])
1237
1238 " 27b: Setting local number local (to window) option"
1239 noa setglobal foldcolumn=8 " Reset global and local value (without triggering autocmd)
1240 noa setlocal foldcolumn=1
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001241 let g:options = [['foldcolumn', '1', '1', '', '2', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001242 setlocal foldcolumn=2
1243 call assert_equal([], g:options)
1244 call assert_equal(g:opt[0], g:opt[1])
1245
1246 " 27c: Setting again number local (to window) option"
1247 noa setglobal foldcolumn=8 " Reset global and local value (without triggering autocmd)
1248 noa setlocal foldcolumn=1
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001249 let g:options = [['foldcolumn', '1', '1', '8', '2', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001250 set foldcolumn=2
1251 call assert_equal([], g:options)
1252 call assert_equal(g:opt[0], g:opt[1])
1253
zeertzjqb811de52021-10-21 10:50:44 +01001254 " 27d: Setting again global number local (to window) option"
Bram Moolenaard7c96872019-06-15 17:12:48 +02001255 noa set foldcolumn=8 " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001256 let g:options = [['foldcolumn', '8', '8', '8', '2', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001257 set foldcolumn=2
1258 call assert_equal([], g:options)
1259 call assert_equal(g:opt[0], g:opt[1])
1260
1261
1262 " 28a: Setting global boolean global option"
1263 noa setglobal nowrapscan " Reset global and local value (without triggering autocmd)
1264 noa setlocal wrapscan " Sets the global(!) value!
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001265 let g:options = [['wrapscan', '1', '', '1', '0', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001266 setglobal nowrapscan
1267 call assert_equal([], g:options)
1268 call assert_equal(g:opt[0], g:opt[1])
1269
1270 " 28b: Setting local boolean global option"
1271 noa setglobal nowrapscan " Reset global and local value (without triggering autocmd)
1272 noa setlocal wrapscan " Sets the global(!) value!
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001273 let g:options = [['wrapscan', '1', '1', '', '0', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001274 setlocal nowrapscan
1275 call assert_equal([], g:options)
1276 call assert_equal(g:opt[0], g:opt[1])
1277
1278 " 28c: Setting again boolean global option"
1279 noa setglobal nowrapscan " Reset global and local value (without triggering autocmd)
1280 noa setlocal wrapscan " Sets the global(!) value!
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001281 let g:options = [['wrapscan', '1', '1', '1', '0', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001282 set nowrapscan
1283 call assert_equal([], g:options)
1284 call assert_equal(g:opt[0], g:opt[1])
1285
1286 " 28d: Setting again global boolean global option"
1287 noa set nowrapscan " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001288 let g:options = [['wrapscan', '0', '0', '0', '1', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001289 set wrapscan
1290 call assert_equal([], g:options)
1291 call assert_equal(g:opt[0], g:opt[1])
1292
1293
1294 " 29a: Setting global boolean global-local (to buffer) option"
1295 noa setglobal noautoread " Reset global and local value (without triggering autocmd)
1296 noa setlocal autoread
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001297 let g:options = [['autoread', '0', '', '0', '1', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001298 setglobal autoread
1299 call assert_equal([], g:options)
1300 call assert_equal(g:opt[0], g:opt[1])
1301
1302 " 29b: Setting local boolean global-local (to buffer) option"
1303 noa setglobal noautoread " Reset global and local value (without triggering autocmd)
1304 noa setlocal autoread
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001305 let g:options = [['autoread', '1', '1', '', '0', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001306 setlocal noautoread
1307 call assert_equal([], g:options)
1308 call assert_equal(g:opt[0], g:opt[1])
1309
1310 " 29c: Setting again boolean global-local (to buffer) option"
1311 noa setglobal noautoread " Reset global and local value (without triggering autocmd)
1312 noa setlocal autoread
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001313 let g:options = [['autoread', '1', '1', '0', '1', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001314 set autoread
1315 call assert_equal([], g:options)
1316 call assert_equal(g:opt[0], g:opt[1])
1317
1318 " 29d: Setting again global boolean global-local (to buffer) option"
1319 noa set noautoread " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001320 let g:options = [['autoread', '0', '0', '0', '1', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001321 set autoread
1322 call assert_equal([], g:options)
1323 call assert_equal(g:opt[0], g:opt[1])
1324
1325
1326 " 30a: Setting global boolean local (to buffer) option"
1327 noa setglobal nocindent " Reset global and local value (without triggering autocmd)
1328 noa setlocal cindent
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001329 let g:options = [['cindent', '0', '', '0', '1', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001330 setglobal cindent
1331 call assert_equal([], g:options)
1332 call assert_equal(g:opt[0], g:opt[1])
1333
1334 " 30b: Setting local boolean local (to buffer) option"
1335 noa setglobal nocindent " Reset global and local value (without triggering autocmd)
1336 noa setlocal cindent
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001337 let g:options = [['cindent', '1', '1', '', '0', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001338 setlocal nocindent
1339 call assert_equal([], g:options)
1340 call assert_equal(g:opt[0], g:opt[1])
1341
1342 " 30c: Setting again boolean local (to buffer) option"
1343 noa setglobal nocindent " Reset global and local value (without triggering autocmd)
1344 noa setlocal cindent
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001345 let g:options = [['cindent', '1', '1', '0', '1', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001346 set cindent
1347 call assert_equal([], g:options)
1348 call assert_equal(g:opt[0], g:opt[1])
1349
1350 " 30d: Setting again global boolean local (to buffer) option"
1351 noa set nocindent " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001352 let g:options = [['cindent', '0', '0', '0', '1', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001353 set cindent
1354 call assert_equal([], g:options)
1355 call assert_equal(g:opt[0], g:opt[1])
1356
1357
1358 " 31: Setting boolean global-local (to window) option
1359 " Currently no such option exists.
1360
1361
1362 " 32a: Setting global boolean local (to window) option"
1363 noa setglobal nocursorcolumn " Reset global and local value (without triggering autocmd)
1364 noa setlocal cursorcolumn
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001365 let g:options = [['cursorcolumn', '0', '', '0', '1', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001366 setglobal cursorcolumn
1367 call assert_equal([], g:options)
1368 call assert_equal(g:opt[0], g:opt[1])
1369
1370 " 32b: Setting local boolean local (to window) option"
1371 noa setglobal nocursorcolumn " Reset global and local value (without triggering autocmd)
1372 noa setlocal cursorcolumn
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001373 let g:options = [['cursorcolumn', '1', '1', '', '0', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001374 setlocal nocursorcolumn
1375 call assert_equal([], g:options)
1376 call assert_equal(g:opt[0], g:opt[1])
1377
1378 " 32c: Setting again boolean local (to window) option"
1379 noa setglobal nocursorcolumn " Reset global and local value (without triggering autocmd)
1380 noa setlocal cursorcolumn
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001381 let g:options = [['cursorcolumn', '1', '1', '0', '1', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001382 set cursorcolumn
1383 call assert_equal([], g:options)
1384 call assert_equal(g:opt[0], g:opt[1])
1385
1386 " 32d: Setting again global boolean local (to window) option"
1387 noa set nocursorcolumn " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001388 let g:options = [['cursorcolumn', '0', '0', '0', '1', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001389 set cursorcolumn
1390 call assert_equal([], g:options)
1391 call assert_equal(g:opt[0], g:opt[1])
1392
1393
Bram Moolenaar1bc353b2019-09-01 14:45:28 +02001394 " 33: Test autocommands when an option value is converted internally.
Bram Moolenaard7c96872019-06-15 17:12:48 +02001395 noa set backspace=1 " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001396 let g:options = [['backspace', 'indent,eol', 'indent,eol', 'indent,eol', '2', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001397 set backspace=2
1398 call assert_equal([], g:options)
1399 call assert_equal(g:opt[0], g:opt[1])
1400
1401
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001402 " Cleanup
1403 au! OptionSet
Bram Moolenaar0331faf2019-06-15 18:40:37 +02001404 " set tags&
Bram Moolenaard7c96872019-06-15 17:12:48 +02001405 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 +02001406 exe printf(":set %s&vim", opt)
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001407 endfor
1408 call test_override('starting', 0)
1409 delfunc! AutoCommandOptionSet
1410endfunc
1411
1412func Test_OptionSet_diffmode()
1413 call test_override('starting', 1)
Bram Moolenaar26d98212019-01-27 22:32:55 +01001414 " 18: Changing an option when entering diff mode
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001415 new
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001416 au OptionSet diff :let &l:cul = v:option_new
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001417
1418 call setline(1, ['buffer 1', 'line2', 'line3', 'line4'])
1419 call assert_equal(0, &l:cul)
1420 diffthis
1421 call assert_equal(1, &l:cul)
1422
1423 vnew
1424 call setline(1, ['buffer 2', 'line 2', 'line 3', 'line4'])
1425 call assert_equal(0, &l:cul)
1426 diffthis
1427 call assert_equal(1, &l:cul)
1428
1429 diffoff
1430 call assert_equal(0, &l:cul)
1431 call assert_equal(1, getwinvar(2, '&l:cul'))
1432 bw!
1433
1434 call assert_equal(1, &l:cul)
1435 diffoff!
1436 call assert_equal(0, &l:cul)
1437 call assert_equal(0, getwinvar(1, '&l:cul'))
1438 bw!
1439
1440 " Cleanup
1441 au! OptionSet
1442 call test_override('starting', 0)
1443endfunc
1444
1445func Test_OptionSet_diffmode_close()
1446 call test_override('starting', 1)
1447 " 19: Try to close the current window when entering diff mode
1448 " should not segfault
1449 new
1450 au OptionSet diff close
1451
1452 call setline(1, ['buffer 1', 'line2', 'line3', 'line4'])
Bram Moolenaare2e40752020-09-04 21:18:46 +02001453 call assert_fails(':diffthis', 'E788:')
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001454 call assert_equal(1, &diff)
1455 vnew
1456 call setline(1, ['buffer 2', 'line 2', 'line 3', 'line4'])
Bram Moolenaare2e40752020-09-04 21:18:46 +02001457 call assert_fails(':diffthis', 'E788:')
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001458 call assert_equal(1, &diff)
Bram Moolenaara9aa86f2019-11-10 21:25:45 +01001459 set diffopt-=closeoff
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001460 bw!
Bram Moolenaare2e40752020-09-04 21:18:46 +02001461 call assert_fails(':diffoff!', 'E788:')
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001462 bw!
1463
1464 " Cleanup
1465 au! OptionSet
1466 call test_override('starting', 0)
1467 "delfunc! AutoCommandOptionSet
1468endfunc
Bram Moolenaar4a137b42017-08-04 22:37:11 +02001469
1470" Test for Bufleave autocommand that deletes the buffer we are about to edit.
1471func Test_BufleaveWithDelete()
1472 new | edit Xfile1
1473
1474 augroup test_bufleavewithdelete
1475 autocmd!
1476 autocmd BufLeave Xfile1 bwipe Xfile2
1477 augroup END
1478
1479 call assert_fails('edit Xfile2', 'E143:')
1480 call assert_equal('Xfile1', bufname('%'))
1481
1482 autocmd! test_bufleavewithdelete BufLeave Xfile1
1483 augroup! test_bufleavewithdelete
1484
1485 new
1486 bwipe! Xfile1
1487endfunc
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +02001488
1489" Test for autocommand that changes the buffer list, when doing ":ball".
1490func Test_Acmd_BufAll()
1491 enew!
1492 %bwipe!
1493 call writefile(['Test file Xxx1'], 'Xxx1')
1494 call writefile(['Test file Xxx2'], 'Xxx2')
1495 call writefile(['Test file Xxx3'], 'Xxx3')
1496
1497 " Add three files to the buffer list
1498 split Xxx1
1499 close
1500 split Xxx2
1501 close
1502 split Xxx3
1503 close
1504
1505 " Wipe the buffer when the buffer is opened
1506 au BufReadPost Xxx2 bwipe
1507
1508 call append(0, 'Test file Xxx4')
1509 ball
1510
1511 call assert_equal(2, winnr('$'))
1512 call assert_equal('Xxx1', bufname(winbufnr(winnr('$'))))
1513 wincmd t
1514
1515 au! BufReadPost
1516 %bwipe!
1517 call delete('Xxx1')
1518 call delete('Xxx2')
1519 call delete('Xxx3')
1520 enew! | only
1521endfunc
1522
1523" Test for autocommand that changes current buffer on BufEnter event.
1524" Check if modelines are interpreted for the correct buffer.
1525func Test_Acmd_BufEnter()
1526 %bwipe!
1527 call writefile(['start of test file Xxx1',
1528 \ "\<Tab>this is a test",
1529 \ 'end of test file Xxx1'], 'Xxx1')
1530 call writefile(['start of test file Xxx2',
1531 \ 'vim: set noai :',
1532 \ "\<Tab>this is a test",
1533 \ 'end of test file Xxx2'], 'Xxx2')
1534
1535 au BufEnter Xxx2 brew
1536 set ai modeline modelines=3
1537 edit Xxx1
1538 " edit Xxx2, autocmd will do :brew
1539 edit Xxx2
1540 exe "normal G?this is a\<CR>"
1541 " Append text with autoindent to this file
1542 normal othis should be auto-indented
1543 call assert_equal("\<Tab>this should be auto-indented", getline('.'))
1544 call assert_equal(3, line('.'))
1545 " Remove autocmd and edit Xxx2 again
1546 au! BufEnter Xxx2
1547 buf! Xxx2
1548 exe "normal G?this is a\<CR>"
1549 " append text without autoindent to Xxx
1550 normal othis should be in column 1
1551 call assert_equal("this should be in column 1", getline('.'))
1552 call assert_equal(4, line('.'))
1553
1554 %bwipe!
1555 call delete('Xxx1')
1556 call delete('Xxx2')
1557 set ai&vim modeline&vim modelines&vim
1558endfunc
1559
1560" Test for issue #57
1561" do not move cursor on <c-o> when autoindent is set
1562func Test_ai_CTRL_O()
1563 enew!
1564 set ai
1565 let save_fo = &fo
1566 set fo+=r
1567 exe "normal o# abcdef\<Esc>2hi\<CR>\<C-O>d0\<Esc>"
1568 exe "normal o# abcdef\<Esc>2hi\<C-O>d0\<Esc>"
1569 call assert_equal(['# abc', 'def', 'def'], getline(2, 4))
1570
1571 set ai&vim
1572 let &fo = save_fo
1573 enew!
1574endfunc
1575
1576" Test for autocommand that deletes the current buffer on BufLeave event.
1577" Also test deleting the last buffer, should give a new, empty buffer.
1578func Test_BufLeave_Wipe()
1579 %bwipe!
1580 let content = ['start of test file Xxx',
1581 \ 'this is a test',
1582 \ 'end of test file Xxx']
1583 call writefile(content, 'Xxx1')
1584 call writefile(content, 'Xxx2')
1585
1586 au BufLeave Xxx2 bwipe
1587 edit Xxx1
1588 split Xxx2
1589 " delete buffer Xxx2, we should be back to Xxx1
1590 bwipe
1591 call assert_equal('Xxx1', bufname('%'))
1592 call assert_equal(1, winnr('$'))
1593
1594 " Create an alternate buffer
1595 %write! test.out
1596 call assert_equal('test.out', bufname('#'))
1597 " delete alternate buffer
1598 bwipe test.out
1599 call assert_equal('Xxx1', bufname('%'))
1600 call assert_equal('', bufname('#'))
1601
1602 au BufLeave Xxx1 bwipe
1603 " delete current buffer, get an empty one
1604 bwipe!
1605 call assert_equal(1, line('$'))
1606 call assert_equal('', bufname('%'))
Bram Moolenaarb2c87502017-10-14 21:15:58 +02001607 let g:bufinfo = getbufinfo()
1608 call assert_equal(1, len(g:bufinfo))
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +02001609
1610 call delete('Xxx1')
1611 call delete('Xxx2')
Bram Moolenaar53f0c962017-10-22 14:23:59 +02001612 call delete('test.out')
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +02001613 %bwipe
1614 au! BufLeave
Bram Moolenaarb2c87502017-10-14 21:15:58 +02001615
1616 " check that bufinfo doesn't contain a pointer to freed memory
1617 call test_garbagecollect_now()
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +02001618endfunc
Bram Moolenaar87ffb5c2017-10-19 12:37:42 +02001619
1620func Test_QuitPre()
1621 edit Xfoo
1622 let winid = win_getid(winnr())
1623 split Xbar
1624 au! QuitPre * let g:afile = expand('<afile>')
1625 " Close the other window, <afile> should be correct.
1626 exe win_id2win(winid) . 'q'
1627 call assert_equal('Xfoo', g:afile)
LemonBoy66e13ae2022-04-21 22:52:11 +01001628
Bram Moolenaar87ffb5c2017-10-19 12:37:42 +02001629 unlet g:afile
1630 bwipe Xfoo
1631 bwipe Xbar
1632endfunc
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02001633
1634func Test_Cmdline()
Bram Moolenaar153b7042018-01-31 15:48:32 +01001635 au! CmdlineChanged : let g:text = getcmdline()
1636 let g:text = 0
1637 call feedkeys(":echom 'hello'\<CR>", 'xt')
1638 call assert_equal("echom 'hello'", g:text)
1639 au! CmdlineChanged
1640
1641 au! CmdlineChanged : let g:entered = expand('<afile>')
1642 let g:entered = 0
1643 call feedkeys(":echom 'hello'\<CR>", 'xt')
1644 call assert_equal(':', g:entered)
1645 au! CmdlineChanged
1646
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02001647 au! CmdlineEnter : let g:entered = expand('<afile>')
1648 au! CmdlineLeave : let g:left = expand('<afile>')
1649 let g:entered = 0
1650 let g:left = 0
1651 call feedkeys(":echo 'hello'\<CR>", 'xt')
1652 call assert_equal(':', g:entered)
1653 call assert_equal(':', g:left)
1654 au! CmdlineEnter
1655 au! CmdlineLeave
1656
Bram Moolenaara4baf5b2018-04-22 13:27:44 +02001657 let save_shellslash = &shellslash
1658 set noshellslash
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02001659 au! CmdlineEnter / let g:entered = expand('<afile>')
1660 au! CmdlineLeave / let g:left = expand('<afile>')
1661 let g:entered = 0
1662 let g:left = 0
Bram Moolenaar53f0c962017-10-22 14:23:59 +02001663 new
1664 call setline(1, 'hello')
1665 call feedkeys("/hello\<CR>", 'xt')
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02001666 call assert_equal('/', g:entered)
1667 call assert_equal('/', g:left)
Bram Moolenaar53f0c962017-10-22 14:23:59 +02001668 bwipe!
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02001669 au! CmdlineEnter
1670 au! CmdlineLeave
Bram Moolenaara4baf5b2018-04-22 13:27:44 +02001671 let &shellslash = save_shellslash
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02001672endfunc
Bram Moolenaar53f0c962017-10-22 14:23:59 +02001673
1674" Test for BufWritePre autocommand that deletes or unloads the buffer.
1675func Test_BufWritePre()
1676 %bwipe
1677 au BufWritePre Xxx1 bunload
1678 au BufWritePre Xxx2 bwipe
1679
1680 call writefile(['start of Xxx1', 'test', 'end of Xxx1'], 'Xxx1')
1681 call writefile(['start of Xxx2', 'test', 'end of Xxx2'], 'Xxx2')
1682
1683 edit Xtest
1684 e! Xxx2
1685 bdel Xtest
1686 e Xxx1
1687 " write it, will unload it and give an error msg
Bram Moolenaare2e40752020-09-04 21:18:46 +02001688 call assert_fails('w', 'E203:')
Bram Moolenaar53f0c962017-10-22 14:23:59 +02001689 call assert_equal('Xxx2', bufname('%'))
1690 edit Xtest
1691 e! Xxx2
1692 bwipe Xtest
1693 " write it, will delete the buffer and give an error msg
Bram Moolenaare2e40752020-09-04 21:18:46 +02001694 call assert_fails('w', 'E203:')
Bram Moolenaar53f0c962017-10-22 14:23:59 +02001695 call assert_equal('Xxx1', bufname('%'))
1696 au! BufWritePre
1697 call delete('Xxx1')
1698 call delete('Xxx2')
1699endfunc
1700
1701" Test for BufUnload autocommand that unloads all the other buffers
1702func Test_bufunload_all()
Bram Moolenaarf08b0eb2021-10-16 13:00:14 +01001703 let g:test_is_flaky = 1
Bram Moolenaar53f0c962017-10-22 14:23:59 +02001704 call writefile(['Test file Xxx1'], 'Xxx1')"
1705 call writefile(['Test file Xxx2'], 'Xxx2')"
1706
Bram Moolenaarc79745a2019-05-20 22:12:34 +02001707 let content =<< trim [CODE]
1708 func UnloadAllBufs()
1709 let i = 1
1710 while i <= bufnr('$')
1711 if i != bufnr('%') && bufloaded(i)
1712 exe i . 'bunload'
1713 endif
1714 let i += 1
1715 endwhile
1716 endfunc
1717 au BufUnload * call UnloadAllBufs()
1718 au VimLeave * call writefile(['Test Finished'], 'Xout')
1719 edit Xxx1
1720 split Xxx2
1721 q
1722 [CODE]
1723
Bram Moolenaar53f0c962017-10-22 14:23:59 +02001724 call writefile(content, 'Xtest')
1725
1726 call delete('Xout')
Bram Moolenaar93344c22019-08-14 21:12:05 +02001727 call system(GetVimCommandClean() .. ' -N --not-a-term -S Xtest')
Bram Moolenaar53f0c962017-10-22 14:23:59 +02001728 call assert_true(filereadable('Xout'))
1729
1730 call delete('Xxx1')
1731 call delete('Xxx2')
1732 call delete('Xtest')
1733 call delete('Xout')
1734endfunc
1735
1736" Some tests for buffer-local autocommands
1737func Test_buflocal_autocmd()
1738 let g:bname = ''
1739 edit xx
1740 au BufLeave <buffer> let g:bname = expand("%")
1741 " here, autocommand for xx should trigger.
1742 " but autocommand shall not apply to buffer named <buffer>.
1743 edit somefile
1744 call assert_equal('xx', g:bname)
1745 let g:bname = ''
1746 " here, autocommand shall be auto-deleted
1747 bwipe xx
1748 " autocmd should not trigger
1749 edit xx
1750 call assert_equal('', g:bname)
1751 " autocmd should not trigger
1752 edit somefile
1753 call assert_equal('', g:bname)
1754 enew
1755 unlet g:bname
1756endfunc
Bram Moolenaar430dc5d2017-11-02 21:04:47 +01001757
1758" Test for "*Cmd" autocommands
1759func Test_Cmd_Autocmds()
1760 call writefile(['start of Xxx', "\tabc2", 'end of Xxx'], 'Xxx')
1761
1762 enew!
1763 au BufReadCmd XtestA 0r Xxx|$del
1764 edit XtestA " will read text of Xxd instead
1765 call assert_equal('start of Xxx', getline(1))
1766
1767 au BufWriteCmd XtestA call append(line("$"), "write")
1768 write " will append a line to the file
1769 call assert_equal('write', getline('$'))
Bram Moolenaare2e40752020-09-04 21:18:46 +02001770 call assert_fails('read XtestA', 'E484:') " should not read anything
Bram Moolenaar430dc5d2017-11-02 21:04:47 +01001771 call assert_equal('write', getline(4))
1772
1773 " now we have:
1774 " 1 start of Xxx
1775 " 2 abc2
1776 " 3 end of Xxx
1777 " 4 write
1778
1779 au FileReadCmd XtestB '[r Xxx
1780 2r XtestB " will read Xxx below line 2 instead
1781 call assert_equal('start of Xxx', getline(3))
1782
1783 " now we have:
1784 " 1 start of Xxx
1785 " 2 abc2
1786 " 3 start of Xxx
1787 " 4 abc2
1788 " 5 end of Xxx
1789 " 6 end of Xxx
1790 " 7 write
1791
1792 au FileWriteCmd XtestC '[,']copy $
1793 normal 4GA1
1794 4,5w XtestC " will copy lines 4 and 5 to the end
1795 call assert_equal("\tabc21", getline(8))
Bram Moolenaare2e40752020-09-04 21:18:46 +02001796 call assert_fails('r XtestC', 'E484:') " should not read anything
Bram Moolenaar430dc5d2017-11-02 21:04:47 +01001797 call assert_equal("end of Xxx", getline(9))
1798
1799 " now we have:
1800 " 1 start of Xxx
1801 " 2 abc2
1802 " 3 start of Xxx
1803 " 4 abc21
1804 " 5 end of Xxx
1805 " 6 end of Xxx
1806 " 7 write
1807 " 8 abc21
1808 " 9 end of Xxx
1809
1810 let g:lines = []
1811 au FileAppendCmd XtestD call extend(g:lines, getline(line("'["), line("']")))
1812 w >>XtestD " will add lines to 'lines'
1813 call assert_equal(9, len(g:lines))
Bram Moolenaare2e40752020-09-04 21:18:46 +02001814 call assert_fails('$r XtestD', 'E484:') " should not read anything
Bram Moolenaar430dc5d2017-11-02 21:04:47 +01001815 call assert_equal(9, line('$'))
1816 call assert_equal('end of Xxx', getline('$'))
1817
1818 au BufReadCmd XtestE 0r Xxx|$del
1819 sp XtestE " split window with test.out
1820 call assert_equal('end of Xxx', getline(3))
1821
1822 let g:lines = []
1823 exe "normal 2Goasdf\<Esc>\<C-W>\<C-W>"
1824 au BufWriteCmd XtestE call extend(g:lines, getline(0, '$'))
1825 wall " will write other window to 'lines'
1826 call assert_equal(4, len(g:lines), g:lines)
1827 call assert_equal('asdf', g:lines[2])
1828
1829 au! BufReadCmd
1830 au! BufWriteCmd
1831 au! FileReadCmd
1832 au! FileWriteCmd
1833 au! FileAppendCmd
1834 %bwipe!
1835 call delete('Xxx')
1836 enew!
1837endfunc
Bram Moolenaaraace2152017-11-05 16:23:10 +01001838
Bram Moolenaar0fff4412020-03-29 16:06:29 +02001839func s:ReadFile()
1840 setl noswapfile nomodified
1841 let filename = resolve(expand("<afile>:p"))
1842 execute 'read' fnameescape(filename)
1843 1d_
1844 exe 'file' fnameescape(filename)
1845 setl buftype=acwrite
1846endfunc
1847
1848func s:WriteFile()
1849 let filename = resolve(expand("<afile>:p"))
1850 setl buftype=
1851 noautocmd execute 'write' fnameescape(filename)
1852 setl buftype=acwrite
1853 setl nomodified
1854endfunc
1855
1856func Test_BufReadCmd()
1857 autocmd BufReadCmd *.test call s:ReadFile()
1858 autocmd BufWriteCmd *.test call s:WriteFile()
1859
1860 call writefile(['one', 'two', 'three'], 'Xcmd.test')
1861 edit Xcmd.test
1862 call assert_match('Xcmd.test" line 1 of 3', execute('file'))
1863 normal! Gofour
1864 write
1865 call assert_equal(['one', 'two', 'three', 'four'], readfile('Xcmd.test'))
1866
1867 bwipe!
1868 call delete('Xcmd.test')
1869 au! BufReadCmd
1870 au! BufWriteCmd
1871endfunc
1872
Bram Moolenaaraace2152017-11-05 16:23:10 +01001873func SetChangeMarks(start, end)
Bram Moolenaar97c69432021-01-15 16:45:21 +01001874 exe a:start .. 'mark ['
1875 exe a:end .. 'mark ]'
Bram Moolenaaraace2152017-11-05 16:23:10 +01001876endfunc
1877
1878" Verify the effects of autocmds on '[ and ']
1879func Test_change_mark_in_autocmds()
1880 edit! Xtest
Bram Moolenaar97c69432021-01-15 16:45:21 +01001881 call feedkeys("ia\<CR>b\<CR>c\<CR>d\<C-g>u\<Esc>", 'xtn')
Bram Moolenaaraace2152017-11-05 16:23:10 +01001882
1883 call SetChangeMarks(2, 3)
1884 write
1885 call assert_equal([1, 4], [line("'["), line("']")])
1886
1887 call SetChangeMarks(2, 3)
1888 au BufWritePre * call assert_equal([1, 4], [line("'["), line("']")])
1889 write
1890 au! BufWritePre
1891
Bram Moolenaar14ddd222020-08-05 12:02:40 +02001892 if has('unix')
Bram Moolenaaraace2152017-11-05 16:23:10 +01001893 write XtestFilter
1894 write >> XtestFilter
1895
1896 call SetChangeMarks(2, 3)
1897 " Marks are set to the entire range of the write
1898 au FilterWritePre * call assert_equal([1, 4], [line("'["), line("']")])
1899 " '[ is adjusted to just before the line that will receive the filtered
1900 " data
1901 au FilterReadPre * call assert_equal([4, 4], [line("'["), line("']")])
1902 " The filtered data is read into the buffer, and the source lines are
1903 " still present, so the range is after the source lines
1904 au FilterReadPost * call assert_equal([5, 12], [line("'["), line("']")])
1905 %!cat XtestFilter
1906 " After the filtered data is read, the original lines are deleted
1907 call assert_equal([1, 8], [line("'["), line("']")])
1908 au! FilterWritePre,FilterReadPre,FilterReadPost
1909 undo
1910
1911 call SetChangeMarks(1, 4)
1912 au FilterWritePre * call assert_equal([2, 3], [line("'["), line("']")])
1913 au FilterReadPre * call assert_equal([3, 3], [line("'["), line("']")])
1914 au FilterReadPost * call assert_equal([4, 11], [line("'["), line("']")])
1915 2,3!cat XtestFilter
1916 call assert_equal([2, 9], [line("'["), line("']")])
1917 au! FilterWritePre,FilterReadPre,FilterReadPost
1918 undo
1919
1920 call delete('XtestFilter')
1921 endif
1922
1923 call SetChangeMarks(1, 4)
1924 au FileWritePre * call assert_equal([2, 3], [line("'["), line("']")])
1925 2,3write Xtest2
1926 au! FileWritePre
1927
1928 call SetChangeMarks(2, 3)
1929 au FileAppendPre * call assert_equal([1, 4], [line("'["), line("']")])
1930 write >> Xtest2
1931 au! FileAppendPre
1932
1933 call SetChangeMarks(1, 4)
1934 au FileAppendPre * call assert_equal([2, 3], [line("'["), line("']")])
1935 2,3write >> Xtest2
1936 au! FileAppendPre
1937
1938 call SetChangeMarks(1, 1)
1939 au FileReadPre * call assert_equal([3, 1], [line("'["), line("']")])
1940 au FileReadPost * call assert_equal([4, 11], [line("'["), line("']")])
1941 3read Xtest2
1942 au! FileReadPre,FileReadPost
1943 undo
1944
1945 call SetChangeMarks(4, 4)
1946 " When the line is 0, it's adjusted to 1
1947 au FileReadPre * call assert_equal([1, 4], [line("'["), line("']")])
1948 au FileReadPost * call assert_equal([1, 8], [line("'["), line("']")])
1949 0read Xtest2
1950 au! FileReadPre,FileReadPost
1951 undo
1952
1953 call SetChangeMarks(4, 4)
1954 " When the line is 0, it's adjusted to 1
1955 au FileReadPre * call assert_equal([1, 4], [line("'["), line("']")])
1956 au FileReadPost * call assert_equal([2, 9], [line("'["), line("']")])
1957 1read Xtest2
1958 au! FileReadPre,FileReadPost
1959 undo
1960
1961 bwipe!
1962 call delete('Xtest')
1963 call delete('Xtest2')
1964endfunc
1965
1966func Test_Filter_noshelltemp()
Bram Moolenaaraeb313f2020-11-27 19:13:28 +01001967 CheckExecutable cat
Bram Moolenaaraace2152017-11-05 16:23:10 +01001968
1969 enew!
1970 call setline(1, ['a', 'b', 'c', 'd'])
1971
1972 let shelltemp = &shelltemp
1973 set shelltemp
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(3, g:filter_au)
1981
1982 if has('filterpipe')
1983 set noshelltemp
1984
1985 let g:filter_au = 0
1986 au FilterWritePre * let g:filter_au += 1
1987 au FilterReadPre * let g:filter_au += 1
1988 au FilterReadPost * let g:filter_au += 1
1989 %!cat
1990 call assert_equal(0, g:filter_au)
1991 endif
1992
1993 au! FilterWritePre,FilterReadPre,FilterReadPost
1994 let &shelltemp = shelltemp
1995 bwipe!
1996endfunc
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01001997
1998func Test_TextYankPost()
1999 enew!
2000 call setline(1, ['foo'])
2001
2002 let g:event = []
2003 au TextYankPost * let g:event = copy(v:event)
2004
2005 call assert_equal({}, v:event)
2006 call assert_fails('let v:event = {}', 'E46:')
2007 call assert_fails('let v:event.mykey = 0', 'E742:')
2008
2009 norm "ayiw
2010 call assert_equal(
Bram Moolenaara016eeb2022-04-09 11:37:38 +01002011 \ #{regcontents: ['foo'], regname: 'a', operator: 'y',
2012 \ regtype: 'v', visual: v:false, inclusive: v:true},
2013 \ g:event)
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01002014 norm y_
2015 call assert_equal(
Bram Moolenaara016eeb2022-04-09 11:37:38 +01002016 \ #{regcontents: ['foo'], regname: '', operator: 'y', regtype: 'V',
2017 \ visual: v:false, inclusive: v:false},
2018 \ g:event)
Bram Moolenaar37d16732020-06-12 22:09:01 +02002019 norm Vy
2020 call assert_equal(
Bram Moolenaara016eeb2022-04-09 11:37:38 +01002021 \ #{regcontents: ['foo'], regname: '', operator: 'y', regtype: 'V',
2022 \ visual: v:true, inclusive: v:true},
2023 \ g:event)
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01002024 call feedkeys("\<C-V>y", 'x')
2025 call assert_equal(
Bram Moolenaara016eeb2022-04-09 11:37:38 +01002026 \ #{regcontents: ['f'], regname: '', operator: 'y', regtype: "\x161",
2027 \ visual: v:true, inclusive: v:true},
2028 \ g:event)
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01002029 norm "xciwbar
2030 call assert_equal(
Bram Moolenaara016eeb2022-04-09 11:37:38 +01002031 \ #{regcontents: ['foo'], regname: 'x', operator: 'c', regtype: 'v',
2032 \ visual: v:false, inclusive: v:true},
2033 \ g:event)
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01002034 norm "bdiw
2035 call assert_equal(
Bram Moolenaara016eeb2022-04-09 11:37:38 +01002036 \ #{regcontents: ['bar'], regname: 'b', operator: 'd', regtype: 'v',
2037 \ visual: v:false, inclusive: v:true},
2038 \ g:event)
2039
2040 call setline(1, 'foobar')
2041 " exclusive motion
2042 norm $"ay0
2043 call assert_equal(
2044 \ #{regcontents: ['fooba'], regname: 'a', operator: 'y', regtype: 'v',
2045 \ visual: v:false, inclusive: v:false},
2046 \ g:event)
2047 " inclusive motion
2048 norm 0"ay$
2049 call assert_equal(
2050 \ #{regcontents: ['foobar'], regname: 'a', operator: 'y', regtype: 'v',
2051 \ visual: v:false, inclusive: v:true},
2052 \ g:event)
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01002053
2054 call assert_equal({}, v:event)
2055
Bram Moolenaarfccbf062020-11-26 20:34:00 +01002056 if has('clipboard_working') && !has('gui_running')
2057 " Test that when the visual selection is automatically copied to clipboard
2058 " register a TextYankPost is emitted
2059 call setline(1, ['foobar'])
2060
2061 let @* = ''
2062 set clipboard=autoselect
2063 exe "norm! ggviw\<Esc>"
2064 call assert_equal(
Bram Moolenaara016eeb2022-04-09 11:37:38 +01002065 \ #{regcontents: ['foobar'], regname: '*', operator: 'y',
2066 \ regtype: 'v', visual: v:true, inclusive: v:false},
2067 \ g:event)
Bram Moolenaarfccbf062020-11-26 20:34:00 +01002068
2069 let @+ = ''
2070 set clipboard=autoselectplus
2071 exe "norm! ggviw\<Esc>"
2072 call assert_equal(
Bram Moolenaara016eeb2022-04-09 11:37:38 +01002073 \ #{regcontents: ['foobar'], regname: '+', operator: 'y',
2074 \ regtype: 'v', visual: v:true, inclusive: v:false},
2075 \ g:event)
Bram Moolenaarfccbf062020-11-26 20:34:00 +01002076
2077 set clipboard&vim
2078 endif
2079
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01002080 au! TextYankPost
2081 unlet g:event
2082 bwipe!
2083endfunc
Bram Moolenaar9bca8052017-12-18 12:37:55 +01002084
Bram Moolenaar9a046fd2021-01-28 13:47:59 +01002085func Test_autocommand_all_events()
2086 call assert_fails('au * * bwipe', 'E1155:')
2087 call assert_fails('au * x bwipe', 'E1155:')
Bram Moolenaarb6db1462021-12-24 19:24:47 +00002088 call assert_fails('au! * x bwipe', 'E1155:')
Bram Moolenaar4fb921e2017-12-18 15:33:00 +01002089endfunc
Bram Moolenaarb7407d32018-02-03 17:36:27 +01002090
Bram Moolenaarf6246f52022-02-11 16:30:12 +00002091func Test_autocmd_user()
2092 au User MyEvent let s:res = [expand("<afile>"), expand("<amatch>")]
2093 doautocmd User MyEvent
2094 call assert_equal(['MyEvent', 'MyEvent'], s:res)
2095 au! User
2096 unlet s:res
2097endfunc
2098
Bram Moolenaarb7407d32018-02-03 17:36:27 +01002099function s:Before_test_dirchanged()
2100 augroup test_dirchanged
2101 autocmd!
2102 augroup END
2103 let s:li = []
2104 let s:dir_this = getcwd()
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02002105 let s:dir_foo = s:dir_this . '/Xfoo'
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01002106 call mkdir(s:dir_foo)
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02002107 let s:dir_bar = s:dir_this . '/Xbar'
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01002108 call mkdir(s:dir_bar)
Bram Moolenaarb7407d32018-02-03 17:36:27 +01002109endfunc
2110
2111function s:After_test_dirchanged()
Bram Moolenaar3503d7c2019-11-09 20:10:17 +01002112 call chdir(s:dir_this)
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01002113 call delete(s:dir_foo, 'd')
2114 call delete(s:dir_bar, 'd')
Bram Moolenaarb7407d32018-02-03 17:36:27 +01002115 augroup test_dirchanged
2116 autocmd!
2117 augroup END
2118endfunc
2119
2120function Test_dirchanged_global()
2121 call s:Before_test_dirchanged()
Bram Moolenaarf6246f52022-02-11 16:30:12 +00002122 autocmd test_dirchanged DirChangedPre global call add(s:li, expand("<amatch>") .. " pre cd " .. v:event.directory)
Bram Moolenaarb7407d32018-02-03 17:36:27 +01002123 autocmd test_dirchanged DirChanged global call add(s:li, "cd:")
2124 autocmd test_dirchanged DirChanged global call add(s:li, expand("<afile>"))
Bram Moolenaar3503d7c2019-11-09 20:10:17 +01002125 call chdir(s:dir_foo)
Bram Moolenaarf6246f52022-02-11 16:30:12 +00002126 let expected = ["global pre cd " .. s:dir_foo, "cd:", s:dir_foo]
Bram Moolenaar28e8f732022-02-09 12:58:20 +00002127 call assert_equal(expected, s:li)
Bram Moolenaar3503d7c2019-11-09 20:10:17 +01002128 call chdir(s:dir_foo)
Bram Moolenaar28e8f732022-02-09 12:58:20 +00002129 call assert_equal(expected, s:li)
Bram Moolenaar3503d7c2019-11-09 20:10:17 +01002130 exe 'lcd ' .. fnameescape(s:dir_bar)
Bram Moolenaar28e8f732022-02-09 12:58:20 +00002131 call assert_equal(expected, s:li)
Bram Moolenaard8c9d322022-06-12 11:49:16 +01002132
2133 exe 'cd ' .. s:dir_foo
2134 exe 'cd ' .. s:dir_bar
2135 autocmd! test_dirchanged DirChanged global let g:result = expand("<afile>")
2136 cd -
Bram Moolenaardb77c492022-06-12 23:26:50 +01002137 call assert_equal(s:dir_foo, substitute(g:result, '\\', '/', 'g'))
Bram Moolenaard8c9d322022-06-12 11:49:16 +01002138
Bram Moolenaarb7407d32018-02-03 17:36:27 +01002139 call s:After_test_dirchanged()
2140endfunc
2141
2142function Test_dirchanged_local()
2143 call s:Before_test_dirchanged()
2144 autocmd test_dirchanged DirChanged window call add(s:li, "lcd:")
2145 autocmd test_dirchanged DirChanged window call add(s:li, expand("<afile>"))
Bram Moolenaar3503d7c2019-11-09 20:10:17 +01002146 call chdir(s:dir_foo)
Bram Moolenaarb7407d32018-02-03 17:36:27 +01002147 call assert_equal([], s:li)
Bram Moolenaar3503d7c2019-11-09 20:10:17 +01002148 exe 'lcd ' .. fnameescape(s:dir_bar)
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01002149 call assert_equal(["lcd:", s:dir_bar], s:li)
Bram Moolenaar3503d7c2019-11-09 20:10:17 +01002150 exe 'lcd ' .. fnameescape(s:dir_bar)
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01002151 call assert_equal(["lcd:", s:dir_bar], s:li)
Bram Moolenaarb7407d32018-02-03 17:36:27 +01002152 call s:After_test_dirchanged()
2153endfunc
2154
2155function Test_dirchanged_auto()
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +02002156 CheckOption autochdir
Bram Moolenaarb7407d32018-02-03 17:36:27 +01002157 call s:Before_test_dirchanged()
2158 call test_autochdir()
Bram Moolenaar28e8f732022-02-09 12:58:20 +00002159 autocmd test_dirchanged DirChangedPre auto call add(s:li, "pre cd " .. v:event.directory)
Bram Moolenaarb7407d32018-02-03 17:36:27 +01002160 autocmd test_dirchanged DirChanged auto call add(s:li, "auto:")
2161 autocmd test_dirchanged DirChanged auto call add(s:li, expand("<afile>"))
2162 set acd
Bram Moolenaar3503d7c2019-11-09 20:10:17 +01002163 cd ..
Bram Moolenaarb7407d32018-02-03 17:36:27 +01002164 call assert_equal([], s:li)
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01002165 exe 'edit ' . s:dir_foo . '/Xfile'
2166 call assert_equal(s:dir_foo, getcwd())
Bram Moolenaar28e8f732022-02-09 12:58:20 +00002167 let expected = ["pre cd " .. s:dir_foo, "auto:", s:dir_foo]
2168 call assert_equal(expected, s:li)
Bram Moolenaarb7407d32018-02-03 17:36:27 +01002169 set noacd
2170 bwipe!
2171 call s:After_test_dirchanged()
2172endfunc
Bram Moolenaar5a093432018-02-10 18:15:19 +01002173
2174" Test TextChangedI and TextChangedP
2175func Test_ChangedP()
2176 new
2177 call setline(1, ['foo', 'bar', 'foobar'])
2178 call test_override("char_avail", 1)
2179 set complete=. completeopt=menuone
2180
2181 func! TextChangedAutocmd(char)
2182 let g:autocmd .= a:char
2183 endfunc
2184
Christian Brabandtdb3b4462021-10-16 11:58:55 +01002185 " TextChanged will not be triggered, only check that it isn't.
Bram Moolenaar5a093432018-02-10 18:15:19 +01002186 au! TextChanged <buffer> :call TextChangedAutocmd('N')
2187 au! TextChangedI <buffer> :call TextChangedAutocmd('I')
2188 au! TextChangedP <buffer> :call TextChangedAutocmd('P')
2189
2190 call cursor(3, 1)
2191 let g:autocmd = ''
2192 call feedkeys("o\<esc>", 'tnix')
2193 call assert_equal('I', g:autocmd)
2194
2195 let g:autocmd = ''
2196 call feedkeys("Sf", 'tnix')
2197 call assert_equal('II', g:autocmd)
2198
2199 let g:autocmd = ''
2200 call feedkeys("Sf\<C-N>", 'tnix')
2201 call assert_equal('IIP', g:autocmd)
2202
2203 let g:autocmd = ''
2204 call feedkeys("Sf\<C-N>\<C-N>", 'tnix')
2205 call assert_equal('IIPP', g:autocmd)
2206
2207 let g:autocmd = ''
2208 call feedkeys("Sf\<C-N>\<C-N>\<C-N>", 'tnix')
2209 call assert_equal('IIPPP', g:autocmd)
2210
2211 let g:autocmd = ''
2212 call feedkeys("Sf\<C-N>\<C-N>\<C-N>\<C-N>", 'tnix')
2213 call assert_equal('IIPPPP', g:autocmd)
2214
2215 call assert_equal(['foo', 'bar', 'foobar', 'foo'], getline(1, '$'))
2216 " TODO: how should it handle completeopt=noinsert,noselect?
2217
2218 " CleanUp
2219 call test_override("char_avail", 0)
2220 au! TextChanged
2221 au! TextChangedI
2222 au! TextChangedP
2223 delfu TextChangedAutocmd
2224 unlet! g:autocmd
2225 set complete&vim completeopt&vim
2226
2227 bw!
2228endfunc
Bram Moolenaar8c64a362018-03-23 22:39:31 +01002229
Bram Moolenaar91d2e782018-08-07 19:05:01 +02002230let g:setline_handled = v:false
Bram Moolenaar1e115362019-01-09 23:01:02 +01002231func SetLineOne()
Bram Moolenaar91d2e782018-08-07 19:05:01 +02002232 if !g:setline_handled
2233 call setline(1, "(x)")
2234 let g:setline_handled = v:true
2235 endif
2236endfunc
2237
2238func Test_TextChangedI_with_setline()
2239 new
2240 call test_override('char_avail', 1)
2241 autocmd TextChangedI <buffer> call SetLineOne()
2242 call feedkeys("i(\<CR>\<Esc>", 'tx')
2243 call assert_equal('(', getline(1))
2244 call assert_equal('x)', getline(2))
2245 undo
Bram Moolenaar91d2e782018-08-07 19:05:01 +02002246 call assert_equal('', getline(1))
Bram Moolenaar9fa95062018-08-08 22:08:32 +02002247 call assert_equal('', getline(2))
Bram Moolenaar91d2e782018-08-07 19:05:01 +02002248
Bram Moolenaarca34db32022-01-20 11:17:18 +00002249 call test_override('char_avail', 0)
Bram Moolenaar91d2e782018-08-07 19:05:01 +02002250 bwipe!
2251endfunc
2252
Bram Moolenaar8c64a362018-03-23 22:39:31 +01002253func Test_Changed_FirstTime()
Bram Moolenaar8c5a2782019-08-07 23:07:07 +02002254 CheckFeature terminal
2255 CheckNotGui
Bram Moolenaar3cdcb092020-03-18 19:18:10 +01002256 " Starting a terminal to run Vim is always considered flaky.
Bram Moolenaar30d53e22020-03-18 21:10:44 +01002257 let g:test_is_flaky = 1
Bram Moolenaar8c5a2782019-08-07 23:07:07 +02002258
Bram Moolenaar8c64a362018-03-23 22:39:31 +01002259 " Prepare file for TextChanged event.
2260 call writefile([''], 'Xchanged.txt')
2261 let buf = term_start([GetVimProg(), '--clean', '-c', 'set noswapfile'], {'term_rows': 3})
2262 call assert_equal('running', term_getstatus(buf))
Bram Moolenaar1834d372018-03-29 17:40:46 +02002263 " Wait for the ruler (in the status line) to be shown.
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01002264 " In ConPTY, there is additional character which is drawn up to the width of
2265 " the screen.
2266 if has('conpty')
2267 call WaitForAssert({-> assert_match('\<All.*$', term_getline(buf, 3))})
2268 else
2269 call WaitForAssert({-> assert_match('\<All$', term_getline(buf, 3))})
2270 endif
Bram Moolenaar8c64a362018-03-23 22:39:31 +01002271 " It's only adding autocmd, so that no event occurs.
2272 call term_sendkeys(buf, ":au! TextChanged <buffer> call writefile(['No'], 'Xchanged.txt')\<cr>")
2273 call term_sendkeys(buf, "\<C-\\>\<C-N>:qa!\<cr>")
Bram Moolenaar50182fa2018-04-28 21:34:40 +02002274 call WaitForAssert({-> assert_equal('finished', term_getstatus(buf))})
Bram Moolenaar8c64a362018-03-23 22:39:31 +01002275 call assert_equal([''], readfile('Xchanged.txt'))
2276
2277 " clean up
2278 call delete('Xchanged.txt')
2279 bwipe!
2280endfunc
Bram Moolenaar0566e892019-01-24 19:37:40 +01002281
Bram Moolenaareb93f3f2019-04-04 15:04:56 +02002282func Test_autocmd_nested()
2283 let g:did_nested = 0
2284 augroup Testing
2285 au WinNew * edit somefile
2286 au BufNew * let g:did_nested = 1
2287 augroup END
2288 split
2289 call assert_equal(0, g:did_nested)
2290 close
2291 bwipe! somefile
2292
2293 " old nested argument still works
2294 augroup Testing
2295 au!
2296 au WinNew * nested edit somefile
2297 au BufNew * let g:did_nested = 1
2298 augroup END
2299 split
2300 call assert_equal(1, g:did_nested)
2301 close
2302 bwipe! somefile
2303
2304 " New ++nested argument works
2305 augroup Testing
2306 au!
2307 au WinNew * ++nested edit somefile
2308 au BufNew * let g:did_nested = 1
2309 augroup END
2310 split
2311 call assert_equal(1, g:did_nested)
2312 close
2313 bwipe! somefile
2314
Bram Moolenaarf0775142022-03-04 20:10:38 +00002315 " nested without ++ does not work in Vim9 script
2316 call assert_fails('vim9cmd au WinNew * nested echo fails', 'E1078:')
2317
Bram Moolenaareb93f3f2019-04-04 15:04:56 +02002318 augroup Testing
2319 au!
2320 augroup END
2321
2322 call assert_fails('au WinNew * ++nested ++nested echo bad', 'E983:')
2323 call assert_fails('au WinNew * nested nested echo bad', 'E983:')
2324endfunc
2325
Bram Moolenaar5fa9f232022-07-23 09:06:48 +01002326func Test_autocmd_nested_cursor_invalid()
2327 set laststatus=0
2328 copen
2329 cclose
2330 call setline(1, ['foo', 'bar', 'baz'])
2331 3
2332 augroup nested_inv
2333 autocmd User foo ++nested copen
2334 autocmd BufAdd * let &laststatus = 2 - &laststatus
2335 augroup END
2336 doautocmd User foo
2337
2338 augroup nested_inv
2339 au!
2340 augroup END
2341 set laststatus&
Bram Moolenaarb03950f2022-07-26 13:47:13 +01002342 cclose
Bram Moolenaar5fa9f232022-07-23 09:06:48 +01002343 bwipe!
2344endfunc
2345
Bram Moolenaar3d6ee8b2022-07-27 15:23:35 +01002346func Test_autocmd_nested_keeps_cursor_pos()
2347 enew
2348 call setline(1, 'foo')
2349 autocmd User foo ++nested normal! $a
2350 autocmd InsertLeave * :
2351 doautocmd User foo
2352 call assert_equal([0, 1, 3, 0], getpos('.'))
2353
2354 bwipe!
2355endfunc
2356
Bram Moolenaarb03950f2022-07-26 13:47:13 +01002357func Test_autocmd_nested_switch_window()
2358 " run this in a separate Vim so that SafeState works
2359 CheckRunVimInTerminal
2360
2361 let lines =<< trim END
2362 vim9script
2363 ['()']->writefile('Xautofile')
2364 autocmd VimEnter * ++nested edit Xautofile | split
2365 autocmd BufReadPost * autocmd SafeState * ++once foldclosed('.')
2366 autocmd WinEnter * matchadd('ErrorMsg', 'pat')
2367 END
2368 call writefile(lines, 'Xautoscript')
2369 let buf = RunVimInTerminal('-S Xautoscript', {'rows': 10})
2370 call VerifyScreenDump(buf, 'Test_autocmd_nested_switch', {})
2371
2372 call StopVimInTerminal(buf)
2373 call delete('Xautofile')
2374 call delete('Xautoscript')
2375endfunc
2376
Bram Moolenaareb93f3f2019-04-04 15:04:56 +02002377func Test_autocmd_once()
2378 " Without ++once WinNew triggers twice
2379 let g:did_split = 0
2380 augroup Testing
2381 au WinNew * let g:did_split += 1
2382 augroup END
2383 split
2384 split
2385 call assert_equal(2, g:did_split)
2386 call assert_true(exists('#WinNew'))
2387 close
2388 close
2389
2390 " With ++once WinNew triggers once
2391 let g:did_split = 0
2392 augroup Testing
2393 au!
2394 au WinNew * ++once let g:did_split += 1
2395 augroup END
2396 split
2397 split
2398 call assert_equal(1, g:did_split)
2399 call assert_false(exists('#WinNew'))
2400 close
2401 close
2402
2403 call assert_fails('au WinNew * ++once ++once echo bad', 'E983:')
2404endfunc
2405
Bram Moolenaara68e5952019-04-25 22:22:01 +02002406func Test_autocmd_bufreadpre()
2407 new
2408 let b:bufreadpre = 1
Bram Moolenaarab505b12020-03-23 19:28:44 +01002409 call append(0, range(1000))
Bram Moolenaara68e5952019-04-25 22:22:01 +02002410 w! XAutocmdBufReadPre.txt
2411 autocmd BufReadPre <buffer> :let b:bufreadpre += 1
Bram Moolenaarab505b12020-03-23 19:28:44 +01002412 norm! 500gg
Bram Moolenaara68e5952019-04-25 22:22:01 +02002413 sp
Bram Moolenaarab505b12020-03-23 19:28:44 +01002414 norm! 1000gg
Bram Moolenaara68e5952019-04-25 22:22:01 +02002415 wincmd p
2416 let g:wsv1 = winsaveview()
2417 wincmd p
2418 let g:wsv2 = winsaveview()
2419 " triggers BufReadPre, should not move the cursor in either window
2420 " The topline may change one line in a large window.
2421 edit
2422 call assert_inrange(g:wsv2.topline - 1, g:wsv2.topline + 1, winsaveview().topline)
2423 call assert_equal(g:wsv2.lnum, winsaveview().lnum)
2424 call assert_equal(2, b:bufreadpre)
2425 wincmd p
2426 call assert_equal(g:wsv1.topline, winsaveview().topline)
2427 call assert_equal(g:wsv1.lnum, winsaveview().lnum)
2428 call assert_equal(2, b:bufreadpre)
2429 " Now set the cursor position in an BufReadPre autocommand
2430 " (even though the position will be invalid, this should make Vim reset the
2431 " cursor position in the other window.
2432 wincmd p
2433 set cpo+=g
2434 " won't do anything, but try to set the cursor on an invalid lnum
2435 autocmd BufReadPre <buffer> :norm! 70gg
2436 " triggers BufReadPre, should not move the cursor in either window
2437 e
2438 call assert_equal(1, winsaveview().topline)
2439 call assert_equal(1, winsaveview().lnum)
2440 call assert_equal(3, b:bufreadpre)
2441 wincmd p
2442 call assert_equal(g:wsv1.topline, winsaveview().topline)
2443 call assert_equal(g:wsv1.lnum, winsaveview().lnum)
2444 call assert_equal(3, b:bufreadpre)
2445 close
2446 close
2447 call delete('XAutocmdBufReadPre.txt')
2448 set cpo-=g
2449endfunc
2450
Bram Moolenaar5e66b422019-01-24 21:58:10 +01002451" FileChangedShell tested in test_filechanged.vim
Bram Moolenaar69ea5872019-04-25 20:29:00 +02002452
2453" Tests for the following autocommands:
2454" - FileWritePre writing a compressed file
2455" - FileReadPost reading a compressed file
2456" - BufNewFile reading a file template
2457" - BufReadPre decompressing the file to be read
2458" - FilterReadPre substituting characters in the temp file
2459" - FilterReadPost substituting characters after filtering
2460" - FileReadPre set options for decompression
2461" - FileReadPost decompress the file
2462func Test_ReadWrite_Autocmds()
2463 " Run this test only on Unix-like systems and if gzip is available
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +02002464 CheckUnix
2465 CheckExecutable gzip
Bram Moolenaar69ea5872019-04-25 20:29:00 +02002466
2467 " Make $GZIP empty, "-v" would cause trouble.
2468 let $GZIP = ""
2469
2470 " Use a FileChangedShell autocommand to avoid a prompt for 'Xtestfile.gz'
2471 " being modified outside of Vim (noticed on Solaris).
2472 au FileChangedShell * echo 'caught FileChangedShell'
2473
2474 " Test for the FileReadPost, FileWritePre and FileWritePost autocmds
2475 augroup Test1
2476 au!
2477 au FileWritePre *.gz '[,']!gzip
2478 au FileWritePost *.gz undo
2479 au FileReadPost *.gz '[,']!gzip -d
2480 augroup END
2481
2482 new
2483 set bin
2484 call append(0, [
2485 \ 'line 2 Abcdefghijklmnopqrstuvwxyz',
2486 \ 'line 3 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2487 \ 'line 4 Abcdefghijklmnopqrstuvwxyz',
2488 \ 'line 5 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2489 \ 'line 6 Abcdefghijklmnopqrstuvwxyz',
2490 \ 'line 7 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2491 \ 'line 8 Abcdefghijklmnopqrstuvwxyz',
2492 \ 'line 9 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2493 \ 'line 10 Abcdefghijklmnopqrstuvwxyz'
2494 \ ])
2495 1,9write! Xtestfile.gz
2496 enew! | close
2497
2498 new
2499 " Read and decompress the testfile
2500 0read Xtestfile.gz
2501 call assert_equal([
2502 \ 'line 2 Abcdefghijklmnopqrstuvwxyz',
2503 \ 'line 3 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2504 \ 'line 4 Abcdefghijklmnopqrstuvwxyz',
2505 \ 'line 5 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2506 \ 'line 6 Abcdefghijklmnopqrstuvwxyz',
2507 \ 'line 7 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2508 \ 'line 8 Abcdefghijklmnopqrstuvwxyz',
2509 \ 'line 9 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2510 \ 'line 10 Abcdefghijklmnopqrstuvwxyz'
2511 \ ], getline(1, 9))
2512 enew! | close
2513
2514 augroup Test1
2515 au!
2516 augroup END
2517
2518 " Test for the FileAppendPre and FileAppendPost autocmds
2519 augroup Test2
2520 au!
2521 au BufNewFile *.c read Xtest.c
2522 au FileAppendPre *.out '[,']s/new/NEW/
2523 au FileAppendPost *.out !cat Xtest.c >> test.out
2524 augroup END
2525
2526 call writefile(['/*', ' * Here is a new .c file', ' */'], 'Xtest.c')
2527 new foo.c " should load Xtest.c
2528 call assert_equal(['/*', ' * Here is a new .c file', ' */'], getline(2, 4))
2529 w! >> test.out " append it to the output file
2530
2531 let contents = readfile('test.out')
2532 call assert_equal(' * Here is a NEW .c file', contents[2])
2533 call assert_equal(' * Here is a new .c file', contents[5])
2534
2535 call delete('test.out')
2536 enew! | close
2537 augroup Test2
2538 au!
2539 augroup END
2540
2541 " Test for the BufReadPre and BufReadPost autocmds
2542 augroup Test3
2543 au!
2544 " setup autocommands to decompress before reading and re-compress
2545 " afterwards
2546 au BufReadPre *.gz exe '!gzip -d ' . shellescape(expand("<afile>"))
2547 au BufReadPre *.gz call rename(expand("<afile>:r"), expand("<afile>"))
2548 au BufReadPost *.gz call rename(expand("<afile>"), expand("<afile>:r"))
2549 au BufReadPost *.gz exe '!gzip ' . shellescape(expand("<afile>:r"))
2550 augroup END
2551
2552 e! Xtestfile.gz " Edit compressed file
2553 call assert_equal([
2554 \ 'line 2 Abcdefghijklmnopqrstuvwxyz',
2555 \ 'line 3 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2556 \ 'line 4 Abcdefghijklmnopqrstuvwxyz',
2557 \ 'line 5 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2558 \ 'line 6 Abcdefghijklmnopqrstuvwxyz',
2559 \ 'line 7 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2560 \ 'line 8 Abcdefghijklmnopqrstuvwxyz',
2561 \ 'line 9 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2562 \ 'line 10 Abcdefghijklmnopqrstuvwxyz'
2563 \ ], getline(1, 9))
2564
2565 w! >> test.out " Append it to the output file
2566
2567 augroup Test3
2568 au!
2569 augroup END
2570
2571 " Test for the FilterReadPre and FilterReadPost autocmds.
2572 set shelltemp " need temp files here
2573 augroup Test4
2574 au!
2575 au FilterReadPre *.out call rename(expand("<afile>"), expand("<afile>") . ".t")
2576 au FilterReadPre *.out exe 'silent !sed s/e/E/ ' . shellescape(expand("<afile>")) . ".t >" . shellescape(expand("<afile>"))
2577 au FilterReadPre *.out exe 'silent !rm ' . shellescape(expand("<afile>")) . '.t'
2578 au FilterReadPost *.out '[,']s/x/X/g
2579 augroup END
2580
2581 e! test.out " Edit the output file
2582 1,$!cat
2583 call assert_equal([
2584 \ 'linE 2 AbcdefghijklmnopqrstuvwXyz',
2585 \ 'linE 3 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
2586 \ 'linE 4 AbcdefghijklmnopqrstuvwXyz',
2587 \ 'linE 5 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
2588 \ 'linE 6 AbcdefghijklmnopqrstuvwXyz',
2589 \ 'linE 7 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
2590 \ 'linE 8 AbcdefghijklmnopqrstuvwXyz',
2591 \ 'linE 9 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
2592 \ 'linE 10 AbcdefghijklmnopqrstuvwXyz'
2593 \ ], getline(1, 9))
2594 call assert_equal([
2595 \ 'line 2 Abcdefghijklmnopqrstuvwxyz',
2596 \ 'line 3 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2597 \ 'line 4 Abcdefghijklmnopqrstuvwxyz',
2598 \ 'line 5 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2599 \ 'line 6 Abcdefghijklmnopqrstuvwxyz',
2600 \ 'line 7 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2601 \ 'line 8 Abcdefghijklmnopqrstuvwxyz',
2602 \ 'line 9 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2603 \ 'line 10 Abcdefghijklmnopqrstuvwxyz'
2604 \ ], readfile('test.out'))
2605
2606 augroup Test4
2607 au!
2608 augroup END
2609 set shelltemp&vim
2610
2611 " Test for the FileReadPre and FileReadPost autocmds.
2612 augroup Test5
2613 au!
2614 au FileReadPre *.gz exe 'silent !gzip -d ' . shellescape(expand("<afile>"))
2615 au FileReadPre *.gz call rename(expand("<afile>:r"), expand("<afile>"))
2616 au FileReadPost *.gz '[,']s/l/L/
2617 augroup END
2618
2619 new
2620 0r Xtestfile.gz " Read compressed file
2621 call assert_equal([
2622 \ 'Line 2 Abcdefghijklmnopqrstuvwxyz',
2623 \ 'Line 3 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2624 \ 'Line 4 Abcdefghijklmnopqrstuvwxyz',
2625 \ 'Line 5 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2626 \ 'Line 6 Abcdefghijklmnopqrstuvwxyz',
2627 \ 'Line 7 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2628 \ 'Line 8 Abcdefghijklmnopqrstuvwxyz',
2629 \ 'Line 9 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2630 \ 'Line 10 Abcdefghijklmnopqrstuvwxyz'
2631 \ ], getline(1, 9))
2632 call assert_equal([
2633 \ 'line 2 Abcdefghijklmnopqrstuvwxyz',
2634 \ 'line 3 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2635 \ 'line 4 Abcdefghijklmnopqrstuvwxyz',
2636 \ 'line 5 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2637 \ 'line 6 Abcdefghijklmnopqrstuvwxyz',
2638 \ 'line 7 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2639 \ 'line 8 Abcdefghijklmnopqrstuvwxyz',
2640 \ 'line 9 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2641 \ 'line 10 Abcdefghijklmnopqrstuvwxyz'
2642 \ ], readfile('Xtestfile.gz'))
2643
2644 augroup Test5
2645 au!
2646 augroup END
2647
2648 au! FileChangedShell
2649 call delete('Xtestfile.gz')
2650 call delete('Xtest.c')
2651 call delete('test.out')
2652endfunc
Bram Moolenaar23b51392019-05-09 21:38:43 +02002653
2654func Test_throw_in_BufWritePre()
2655 new
2656 call setline(1, ['one', 'two', 'three'])
2657 call assert_false(filereadable('Xthefile'))
2658 augroup throwing
2659 au BufWritePre X* throw 'do not write'
2660 augroup END
2661 try
2662 w Xthefile
2663 catch
2664 let caught = 1
2665 endtry
2666 call assert_equal(1, caught)
2667 call assert_false(filereadable('Xthefile'))
2668
2669 bwipe!
2670 au! throwing
2671endfunc
Bram Moolenaarcadbe1b2019-09-22 21:50:09 +02002672
Bram Moolenaar40fa12a2021-09-22 14:18:13 +02002673func Test_autocmd_in_try_block()
2674 call mkdir('Xdir')
2675 au BufEnter * let g:fname = expand('%')
2676 try
2677 edit Xdir/
2678 endtry
2679 call assert_match('Xdir', g:fname)
2680
2681 unlet g:fname
2682 au! BufEnter
2683 call delete('Xdir', 'rf')
2684endfunc
2685
Bram Moolenaarcadbe1b2019-09-22 21:50:09 +02002686func Test_autocmd_SafeState()
2687 CheckRunVimInTerminal
2688
2689 let lines =<< trim END
2690 let g:safe = 0
2691 let g:again = ''
2692 au SafeState * let g:safe += 1
2693 au SafeStateAgain * let g:again ..= 'x'
2694 func CallTimer()
2695 call timer_start(10, {id -> execute('let g:again ..= "t"')})
2696 endfunc
2697 END
2698 call writefile(lines, 'XSafeState')
2699 let buf = RunVimInTerminal('-S XSafeState', #{rows: 6})
2700
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01002701 " Sometimes we loop to handle a K_IGNORE, SafeState may be triggered once or
Bram Moolenaar8fb1b472020-02-23 16:16:26 +01002702 " more often.
Bram Moolenaarcadbe1b2019-09-22 21:50:09 +02002703 call term_sendkeys(buf, ":echo g:safe\<CR>")
Bram Moolenaar8fb1b472020-02-23 16:16:26 +01002704 call WaitForAssert({-> assert_match('^\d ', term_getline(buf, 6))}, 1000)
Bram Moolenaarcadbe1b2019-09-22 21:50:09 +02002705
Bram Moolenaar8fb1b472020-02-23 16:16:26 +01002706 " SafeStateAgain should be invoked at least three times
Bram Moolenaarcadbe1b2019-09-22 21:50:09 +02002707 call term_sendkeys(buf, ":echo g:again\<CR>")
Bram Moolenaar8fb1b472020-02-23 16:16:26 +01002708 call WaitForAssert({-> assert_match('^xxx', term_getline(buf, 6))}, 1000)
Bram Moolenaarcadbe1b2019-09-22 21:50:09 +02002709
2710 call term_sendkeys(buf, ":let g:again = ''\<CR>:call CallTimer()\<CR>")
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02002711 call TermWait(buf, 50)
Bram Moolenaar0f6629a2019-09-22 23:24:13 +02002712 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02002713 call TermWait(buf, 50)
Bram Moolenaarcadbe1b2019-09-22 21:50:09 +02002714 call term_sendkeys(buf, ":echo g:again\<CR>")
2715 call WaitForAssert({-> assert_match('xtx', term_getline(buf, 6))}, 1000)
2716
2717 call StopVimInTerminal(buf)
2718 call delete('XSafeState')
2719endfunc
Bram Moolenaar23324a02019-10-01 17:39:04 +02002720
2721func Test_autocmd_CmdWinEnter()
2722 CheckRunVimInTerminal
Bram Moolenaar21829c52021-01-26 22:42:21 +01002723 CheckFeature cmdwin
2724
Bram Moolenaar23324a02019-10-01 17:39:04 +02002725 let lines =<< trim END
Egor Zvorykin125ffd22021-11-17 14:01:14 +00002726 augroup vimHints | au! | augroup END
Bram Moolenaar23324a02019-10-01 17:39:04 +02002727 let b:dummy_var = 'This is a dummy'
2728 autocmd CmdWinEnter * quit
2729 let winnr = winnr('$')
2730 END
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01002731 let filename = 'XCmdWinEnter'
Bram Moolenaar23324a02019-10-01 17:39:04 +02002732 call writefile(lines, filename)
2733 let buf = RunVimInTerminal('-S '.filename, #{rows: 6})
2734
2735 call term_sendkeys(buf, "q:")
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02002736 call TermWait(buf)
Bram Moolenaar23324a02019-10-01 17:39:04 +02002737 call term_sendkeys(buf, ":echo b:dummy_var\<cr>")
Bram Moolenaar353c3512020-03-15 14:19:26 +01002738 call WaitForAssert({-> assert_match('^This is a dummy', term_getline(buf, 6))}, 2000)
Bram Moolenaar23324a02019-10-01 17:39:04 +02002739 call term_sendkeys(buf, ":echo &buftype\<cr>")
2740 call WaitForAssert({-> assert_notmatch('^nofile', term_getline(buf, 6))}, 1000)
2741 call term_sendkeys(buf, ":echo winnr\<cr>")
2742 call WaitForAssert({-> assert_match('^1', term_getline(buf, 6))}, 1000)
2743
2744 " clean up
2745 call StopVimInTerminal(buf)
2746 call delete(filename)
2747endfunc
Bram Moolenaarec66c412019-10-11 21:19:13 +02002748
2749func Test_autocmd_was_using_freed_memory()
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01002750 CheckFeature quickfix
2751
Bram Moolenaarec66c412019-10-11 21:19:13 +02002752 pedit xx
2753 n x
Bram Moolenaar92bb83e2021-02-03 23:04:46 +01002754 augroup winenter
2755 au WinEnter * if winnr('$') > 2 | quit | endif
2756 augroup END
Bram Moolenaarec66c412019-10-11 21:19:13 +02002757 split
Bram Moolenaar92bb83e2021-02-03 23:04:46 +01002758
2759 augroup winenter
2760 au! WinEnter
2761 augroup END
2762
2763 bwipe xx
2764 bwipe x
2765 pclose
Bram Moolenaarec66c412019-10-11 21:19:13 +02002766endfunc
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +01002767
2768func Test_BufWrite_lockmarks()
Bram Moolenaarf08b0eb2021-10-16 13:00:14 +01002769 let g:test_is_flaky = 1
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +01002770 edit! Xtest
2771 call setline(1, ['a', 'b', 'c', 'd'])
2772
2773 " :lockmarks preserves the marks
2774 call SetChangeMarks(2, 3)
2775 lockmarks write
2776 call assert_equal([2, 3], [line("'["), line("']")])
2777
2778 " *WritePre autocmds get the correct line range, but lockmarks preserves the
2779 " original values for the user
2780 augroup lockmarks
2781 au!
2782 au BufWritePre,FilterWritePre * call assert_equal([1, 4], [line("'["), line("']")])
2783 au FileWritePre * call assert_equal([3, 4], [line("'["), line("']")])
2784 augroup END
2785
2786 lockmarks write
2787 call assert_equal([2, 3], [line("'["), line("']")])
2788
2789 if executable('cat')
2790 lockmarks %!cat
2791 call assert_equal([2, 3], [line("'["), line("']")])
2792 endif
2793
2794 lockmarks 3,4write Xtest2
2795 call assert_equal([2, 3], [line("'["), line("']")])
2796
2797 au! lockmarks
2798 augroup! lockmarks
2799 call delete('Xtest')
2800 call delete('Xtest2')
2801endfunc
Bram Moolenaarce6db022020-01-07 20:11:42 +01002802
2803func Test_FileType_spell()
2804 if !isdirectory('/tmp')
2805 throw "Skipped: requires /tmp directory"
2806 endif
2807
2808 " this was crashing with an invalid free()
2809 setglobal spellfile=/tmp/en.utf-8.add
2810 augroup crash
2811 autocmd!
2812 autocmd BufNewFile,BufReadPost crashfile setf somefiletype
2813 autocmd BufNewFile,BufReadPost crashfile set ft=anotherfiletype
2814 autocmd FileType anotherfiletype setlocal spell
2815 augroup END
2816 func! NoCrash() abort
2817 edit /tmp/crashfile
2818 endfunc
2819 call NoCrash()
2820
2821 au! crash
2822 setglobal spellfile=
2823endfunc
Bram Moolenaarbc2b71d2020-02-17 21:33:30 +01002824
Bram Moolenaar406cd902020-02-18 21:54:41 +01002825" Test closing a window or editing another buffer from a FileChangedRO handler
2826" in a readonly buffer
2827func Test_FileChangedRO_winclose()
Bram Moolenaar62cd26a2020-10-11 20:08:44 +02002828 call test_override('ui_delay', 10)
2829
Bram Moolenaar406cd902020-02-18 21:54:41 +01002830 augroup FileChangedROTest
2831 au!
2832 autocmd FileChangedRO * quit
2833 augroup END
2834 new
2835 set readonly
2836 call assert_fails('normal i', 'E788:')
2837 close
2838 augroup! FileChangedROTest
2839
2840 augroup FileChangedROTest
2841 au!
2842 autocmd FileChangedRO * edit Xfile
2843 augroup END
2844 new
2845 set readonly
2846 call assert_fails('normal i', 'E788:')
2847 close
2848 augroup! FileChangedROTest
Bram Moolenaar62cd26a2020-10-11 20:08:44 +02002849 call test_override('ALL', 0)
Bram Moolenaar406cd902020-02-18 21:54:41 +01002850endfunc
2851
Bram Moolenaar0c81d1b2020-02-22 22:45:55 +01002852func LogACmd()
2853 call add(g:logged, line('$'))
2854endfunc
2855
2856func Test_TermChanged()
Bram Moolenaard28e0b32020-02-22 23:08:52 +01002857 CheckNotGui
2858
Bram Moolenaar0c81d1b2020-02-22 22:45:55 +01002859 enew!
2860 tabnew
2861 call setline(1, ['a', 'b', 'c', 'd'])
2862 $
2863 au TermChanged * call LogACmd()
2864 let g:logged = []
2865 let term_save = &term
2866 set term=xterm
2867 call assert_equal([1, 4], g:logged)
2868
2869 au! TermChanged
2870 let &term = term_save
2871 bwipe!
2872endfunc
2873
Bram Moolenaare3284872020-03-19 13:55:03 +01002874" Test for FileReadCmd autocmd
2875func Test_autocmd_FileReadCmd()
2876 func ReadFileCmd()
2877 call append(line('$'), "v:cmdarg = " .. v:cmdarg)
2878 endfunc
2879 augroup FileReadCmdTest
2880 au!
2881 au FileReadCmd Xtest call ReadFileCmd()
2882 augroup END
2883
2884 new
2885 read ++bin Xtest
2886 read ++nobin Xtest
2887 read ++edit Xtest
2888 read ++bad=keep Xtest
2889 read ++bad=drop Xtest
2890 read ++bad=- Xtest
2891 read ++ff=unix Xtest
2892 read ++ff=dos Xtest
2893 read ++ff=mac Xtest
2894 read ++enc=utf-8 Xtest
2895
2896 call assert_equal(['',
2897 \ 'v:cmdarg = ++bin',
2898 \ 'v:cmdarg = ++nobin',
2899 \ 'v:cmdarg = ++edit',
2900 \ 'v:cmdarg = ++bad=keep',
2901 \ 'v:cmdarg = ++bad=drop',
2902 \ 'v:cmdarg = ++bad=-',
2903 \ 'v:cmdarg = ++ff=unix',
2904 \ 'v:cmdarg = ++ff=dos',
2905 \ 'v:cmdarg = ++ff=mac',
2906 \ 'v:cmdarg = ++enc=utf-8'], getline(1, '$'))
2907
2908 close!
2909 augroup FileReadCmdTest
2910 au!
2911 augroup END
2912 delfunc ReadFileCmd
2913endfunc
2914
Bram Moolenaaree4e0c12020-04-06 21:35:05 +02002915" Test for passing invalid arguments to autocmd
2916func Test_autocmd_invalid_args()
2917 " Additional character after * for event
2918 call assert_fails('autocmd *a Xfile set ff=unix', 'E215:')
2919 augroup Test
2920 augroup END
2921 " Invalid autocmd event
2922 call assert_fails('autocmd Bufabc Xfile set ft=vim', 'E216:')
2923 " Invalid autocmd event in a autocmd group
2924 call assert_fails('autocmd Test Bufabc Xfile set ft=vim', 'E216:')
2925 augroup! Test
2926 " Execute all autocmds
2927 call assert_fails('doautocmd * BufEnter', 'E217:')
2928 call assert_fails('augroup! x1a2b3', 'E367:')
2929 call assert_fails('autocmd BufNew <buffer=999> pwd', 'E680:')
Bram Moolenaar531be472020-09-23 22:38:05 +02002930 call assert_fails('autocmd BufNew \) set ff=unix', 'E55:')
Bram Moolenaaree4e0c12020-04-06 21:35:05 +02002931endfunc
2932
2933" Test for deep nesting of autocmds
2934func Test_autocmd_deep_nesting()
2935 autocmd BufEnter Xfile doautocmd BufEnter Xfile
2936 call assert_fails('doautocmd BufEnter Xfile', 'E218:')
2937 autocmd! BufEnter Xfile
2938endfunc
2939
Bram Moolenaarbe5ee862020-06-10 20:56:58 +02002940" Tests for SigUSR1 autocmd event, which is only available on posix systems.
2941func Test_autocmd_sigusr1()
2942 CheckUnix
Bram Moolenaar62cd26a2020-10-11 20:08:44 +02002943 CheckExecutable /bin/kill
Bram Moolenaarbe5ee862020-06-10 20:56:58 +02002944
2945 let g:sigusr1_passed = 0
2946 au SigUSR1 * let g:sigusr1_passed = 1
2947 call system('/bin/kill -s usr1 ' . getpid())
2948 call WaitForAssert({-> assert_true(g:sigusr1_passed)})
2949
2950 au! SigUSR1
2951 unlet g:sigusr1_passed
2952endfunc
2953
Bram Moolenaarb340bae2020-06-15 19:51:56 +02002954" Test for BufReadPre autocmd deleting the file
2955func Test_BufReadPre_delfile()
2956 augroup TestAuCmd
2957 au!
2958 autocmd BufReadPre Xfile call delete('Xfile')
2959 augroup END
2960 call writefile([], 'Xfile')
2961 call assert_fails('new Xfile', 'E200:')
2962 call assert_equal('Xfile', @%)
2963 call assert_equal(1, &readonly)
2964 call delete('Xfile')
2965 augroup TestAuCmd
2966 au!
2967 augroup END
2968 close!
2969endfunc
2970
2971" Test for BufReadPre autocmd changing the current buffer
2972func Test_BufReadPre_changebuf()
2973 augroup TestAuCmd
2974 au!
2975 autocmd BufReadPre Xfile edit Xsomeotherfile
2976 augroup END
2977 call writefile([], 'Xfile')
2978 call assert_fails('new Xfile', 'E201:')
2979 call assert_equal('Xsomeotherfile', @%)
2980 call assert_equal(1, &readonly)
2981 call delete('Xfile')
2982 augroup TestAuCmd
2983 au!
2984 augroup END
2985 close!
2986endfunc
2987
2988" Test for BufWipeouti autocmd changing the current buffer when reading a file
2989" in an empty buffer with 'f' flag in 'cpo'
2990func Test_BufDelete_changebuf()
2991 new
2992 augroup TestAuCmd
2993 au!
2994 autocmd BufWipeout * let bufnr = bufadd('somefile') | exe "b " .. bufnr
2995 augroup END
2996 let save_cpo = &cpo
2997 set cpo+=f
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02002998 call assert_fails('r Xfile', ['E812:', 'E484:'])
Bram Moolenaarb340bae2020-06-15 19:51:56 +02002999 call assert_equal('somefile', @%)
3000 let &cpo = save_cpo
3001 augroup TestAuCmd
3002 au!
3003 augroup END
3004 close!
3005endfunc
3006
Bram Moolenaar0fe937f2020-06-16 22:42:04 +02003007" Test for the temporary internal window used to execute autocmds
3008func Test_autocmd_window()
3009 %bw!
3010 edit one.txt
3011 tabnew two.txt
Bram Moolenaar41cd8032021-03-13 15:47:56 +01003012 vnew three.txt
3013 tabnew four.txt
3014 tabprevious
Bram Moolenaar0fe937f2020-06-16 22:42:04 +02003015 let g:blist = []
Bram Moolenaar832adf92020-06-25 19:01:36 +02003016 augroup aucmd_win_test1
Bram Moolenaar0fe937f2020-06-16 22:42:04 +02003017 au!
3018 au BufEnter * call add(g:blist, [expand('<afile>'),
3019 \ win_gettype(bufwinnr(expand('<afile>')))])
3020 augroup END
3021
3022 doautoall BufEnter
Bram Moolenaar41cd8032021-03-13 15:47:56 +01003023 call assert_equal([
3024 \ ['one.txt', 'autocmd'],
3025 \ ['two.txt', ''],
3026 \ ['four.txt', 'autocmd'],
3027 \ ['three.txt', ''],
3028 \ ], g:blist)
Bram Moolenaar0fe937f2020-06-16 22:42:04 +02003029
Bram Moolenaar832adf92020-06-25 19:01:36 +02003030 augroup aucmd_win_test1
Bram Moolenaar0fe937f2020-06-16 22:42:04 +02003031 au!
3032 augroup END
Bram Moolenaar832adf92020-06-25 19:01:36 +02003033 augroup! aucmd_win_test1
3034 %bw!
3035endfunc
3036
3037" Test for trying to close the temporary window used for executing an autocmd
3038func Test_close_autocmd_window()
3039 %bw!
3040 edit one.txt
3041 tabnew two.txt
3042 augroup aucmd_win_test2
3043 au!
3044 au BufEnter * if expand('<afile>') == 'one.txt' | 1close | endif
3045 augroup END
3046
3047 call assert_fails('doautoall BufEnter', 'E813:')
3048
3049 augroup aucmd_win_test2
3050 au!
3051 augroup END
3052 augroup! aucmd_win_test2
Bram Moolenaarcf844172020-06-26 19:44:06 +02003053 %bwipe!
3054endfunc
3055
3056" Test for trying to close the tab that has the temporary window for exeucing
3057" an autocmd.
3058func Test_close_autocmd_tab()
3059 edit one.txt
3060 tabnew two.txt
3061 augroup aucmd_win_test
3062 au!
3063 au BufEnter * if expand('<afile>') == 'one.txt' | tabfirst | tabonly | endif
3064 augroup END
3065
3066 call assert_fails('doautoall BufEnter', 'E813:')
3067
3068 tabonly
3069 augroup aucmd_win_test
3070 au!
3071 augroup END
3072 augroup! aucmd_win_test
3073 %bwipe!
Bram Moolenaar0fe937f2020-06-16 22:42:04 +02003074endfunc
3075
Bram Moolenaarcb1956d2022-01-07 15:45:18 +00003076func Test_Visual_doautoall_redraw()
3077 call setline(1, ['a', 'b'])
3078 new
3079 wincmd p
3080 call feedkeys("G\<C-V>", 'txn')
3081 autocmd User Explode ++once redraw
3082 doautoall User Explode
3083 %bwipe!
3084endfunc
3085
Bram Moolenaar6bcb8772021-02-03 21:23:29 +01003086" This was using freed memory.
3087func Test_BufNew_arglocal()
3088 arglocal
3089 au BufNew * arglocal
3090 call assert_fails('drop xx', 'E1156:')
3091
3092 au! BufNew
3093endfunc
3094
Bram Moolenaar8ab37572021-02-03 21:56:59 +01003095func Test_autocmd_closes_window()
3096 au BufNew,BufWinLeave * e %e
3097 file yyy
3098 au BufNew,BufWinLeave * ball
Bram Moolenaaraad5f9d2021-02-06 17:30:31 +01003099 n xxx
Bram Moolenaar8ab37572021-02-03 21:56:59 +01003100
Bram Moolenaaraad5f9d2021-02-06 17:30:31 +01003101 %bwipe
Bram Moolenaar8ab37572021-02-03 21:56:59 +01003102 au! BufNew
3103 au! BufWinLeave
3104endfunc
3105
Bram Moolenaar92bb83e2021-02-03 23:04:46 +01003106func Test_autocmd_quit_psearch()
3107 sn aa bb
3108 augroup aucmd_win_test
3109 au!
3110 au BufEnter,BufLeave,BufNew,WinEnter,WinLeave,WinNew * if winnr('$') > 1 | q | endif
3111 augroup END
3112 ps /
3113
3114 augroup aucmd_win_test
3115 au!
3116 augroup END
zeertzjq7851c692022-04-21 11:14:01 +01003117 new
3118 pclose
Bram Moolenaar92bb83e2021-02-03 23:04:46 +01003119endfunc
3120
Bram Moolenaaraad5f9d2021-02-06 17:30:31 +01003121" Fuzzer found some strange combination that caused a crash.
3122func Test_autocmd_normal_mess()
Bram Moolenaardd07c022021-02-07 13:32:46 +01003123 " For unknown reason this hangs on MS-Windows
3124 CheckNotMSWindows
3125
Bram Moolenaaraad5f9d2021-02-06 17:30:31 +01003126 augroup aucmd_normal_test
3127 au BufLeave,BufWinLeave,BufHidden,BufUnload,BufDelete,BufWipeout * norm 7q/qc
3128 augroup END
Bram Moolenaar983d83f2021-02-07 12:12:43 +01003129 call assert_fails('o4', 'E1159')
Bram Moolenaaraad5f9d2021-02-06 17:30:31 +01003130 silent! H
Bram Moolenaar983d83f2021-02-07 12:12:43 +01003131 call assert_fails('e xx', 'E1159')
Bram Moolenaaraad5f9d2021-02-06 17:30:31 +01003132 normal G
3133
3134 augroup aucmd_normal_test
3135 au!
3136 augroup END
3137endfunc
3138
Bram Moolenaar8c6951f2021-02-06 18:08:45 +01003139func Test_autocmd_closing_cmdwin()
Bram Moolenaardd07c022021-02-07 13:32:46 +01003140 " For unknown reason this hangs on MS-Windows
3141 CheckNotMSWindows
3142
Bram Moolenaar8c6951f2021-02-06 18:08:45 +01003143 au BufWinLeave * nested q
3144 call assert_fails("norm 7q?\n", 'E855:')
3145
3146 au! BufWinLeave
3147 new
3148 only
3149endfunc
3150
Bram Moolenaar2c7080b2021-02-06 19:19:42 +01003151func Test_autocmd_vimgrep()
3152 augroup aucmd_vimgrep
Charlie Grovesfef44852022-04-19 16:24:12 +01003153 au QuickfixCmdPre,BufNew,BufReadCmd * sb
zeertzjq7851c692022-04-21 11:14:01 +01003154 au QuickfixCmdPre,BufNew,BufReadCmd * q9
Bram Moolenaar2c7080b2021-02-06 19:19:42 +01003155 augroup END
Bram Moolenaardd07c022021-02-07 13:32:46 +01003156 call assert_fails('lv ?a? foo', 'E926:')
Bram Moolenaar2c7080b2021-02-06 19:19:42 +01003157
3158 augroup aucmd_vimgrep
3159 au!
3160 augroup END
3161endfunc
3162
Bram Moolenaar73b8b0a2021-08-01 14:52:32 +02003163func Test_autocmd_with_block()
3164 augroup block_testing
3165 au BufReadPost *.xml {
3166 setlocal matchpairs+=<:>
3167 /<start
3168 }
Bram Moolenaar63b91732021-08-05 20:40:03 +02003169 au CursorHold * {
3170 autocmd BufReadPre * ++once echo 'one' | echo 'two'
3171 g:gotSafeState = 77
3172 }
Bram Moolenaar73b8b0a2021-08-01 14:52:32 +02003173 augroup END
3174
3175 let expected = "\n--- Autocommands ---\nblock_testing BufRead\n *.xml {^@ setlocal matchpairs+=<:>^@ /<start^@ }"
3176 call assert_equal(expected, execute('au BufReadPost *.xml'))
3177
Bram Moolenaar63b91732021-08-05 20:40:03 +02003178 doautocmd CursorHold
3179 call assert_equal(77, g:gotSafeState)
3180 unlet g:gotSafeState
3181
Bram Moolenaar73b8b0a2021-08-01 14:52:32 +02003182 augroup block_testing
3183 au!
Bram Moolenaar75ebd2a2022-06-03 17:39:46 +01003184 autocmd CursorHold * {
3185 if true
3186 # comment
3187 && true
3188
3189 && true
3190 g:done = 'yes'
3191 endif
3192 }
3193 augroup END
3194 doautocmd CursorHold
3195 call assert_equal('yes', g:done)
3196
3197 unlet g:done
3198 augroup block_testing
3199 au!
Bram Moolenaar73b8b0a2021-08-01 14:52:32 +02003200 augroup END
3201endfunc
3202
Christian Brabandtdb3b4462021-10-16 11:58:55 +01003203" Test TextChangedI and TextChanged
3204func Test_Changed_ChangedI()
3205 new
3206 call test_override("char_avail", 1)
3207 let [g:autocmd_i, g:autocmd_n] = ['','']
3208
3209 func! TextChangedAutocmdI(char)
3210 let g:autocmd_{tolower(a:char)} = a:char .. b:changedtick
3211 endfunc
3212
3213 augroup Test_TextChanged
3214 au!
3215 au TextChanged <buffer> :call TextChangedAutocmdI('N')
3216 au TextChangedI <buffer> :call TextChangedAutocmdI('I')
3217 augroup END
3218
3219 call feedkeys("ifoo\<esc>", 'tnix')
3220 " TODO: Test test does not seem to trigger TextChanged autocommand, this
3221 " requires running Vim in a terminal window.
3222 " call assert_equal('N3', g:autocmd_n)
3223 call assert_equal('I3', g:autocmd_i)
3224
3225 call feedkeys("yyp", 'tnix')
3226 " TODO: Test test does not seem to trigger TextChanged autocommand.
3227 " call assert_equal('N4', g:autocmd_n)
3228 call assert_equal('I3', g:autocmd_i)
3229
3230 " CleanUp
3231 call test_override("char_avail", 0)
3232 au! TextChanged <buffer>
3233 au! TextChangedI <buffer>
3234 augroup! Test_TextChanged
3235 delfu TextChangedAutocmdI
3236 unlet! g:autocmd_i g:autocmd_n
3237
3238 bw!
3239endfunc
Bram Moolenaar2c7080b2021-02-06 19:19:42 +01003240
Bram Moolenaar6f2465d2022-03-22 18:13:01 +00003241func Test_closing_autocmd_window()
3242 let lines =<< trim END
3243 edit Xa.txt
3244 tabnew Xb.txt
3245 autocmd BufEnter Xa.txt unhide 1
3246 doautoall BufEnter
3247 END
3248 call v9.CheckScriptFailure(lines, 'E814:')
3249 au! BufEnter
3250 only!
3251 bwipe Xa.txt
3252 bwipe Xb.txt
3253endfunc
3254
Bram Moolenaar347538f2022-03-26 16:28:06 +00003255func Test_bufwipeout_changes_window()
3256 " This should not crash, but we don't have any expectations about what
3257 " happens, changing window in BufWipeout has unpredictable results.
3258 tabedit
3259 let g:window_id = win_getid()
3260 topleft new
3261 setlocal bufhidden=wipe
3262 autocmd BufWipeout <buffer> call win_gotoid(g:window_id)
3263 tabprevious
3264 +tabclose
3265
3266 unlet g:window_id
3267 au! BufWipeout
3268 %bwipe!
3269endfunc
3270
zeertzjq021996f2022-04-10 11:44:04 +01003271func Test_v_event_readonly()
3272 autocmd CompleteChanged * let v:event.width = 0
3273 call assert_fails("normal! i\<C-X>\<C-V>", 'E46:')
3274 au! CompleteChanged
3275
3276 autocmd DirChangedPre * let v:event.directory = ''
3277 call assert_fails('cd .', 'E46:')
3278 au! DirChangedPre
3279
3280 autocmd ModeChanged * let v:event.new_mode = ''
3281 call assert_fails('normal! cc', 'E46:')
3282 au! ModeChanged
3283
3284 autocmd TextYankPost * let v:event.operator = ''
3285 call assert_fails('normal! yy', 'E46:')
3286 au! TextYankPost
3287endfunc
3288
zeertzjqc9e8fd62022-07-26 18:12:38 +01003289" Test for ModeChanged pattern
3290func Test_mode_changes()
3291 let g:index = 0
3292 let g:mode_seq = ['n', 'i', 'n', 'v', 'V', 'i', 'ix', 'i', 'ic', 'i', 'n', 'no', 'n', 'V', 'v', 's', 'n']
3293 func! TestMode()
3294 call assert_equal(g:mode_seq[g:index], get(v:event, "old_mode"))
3295 call assert_equal(g:mode_seq[g:index + 1], get(v:event, "new_mode"))
3296 call assert_equal(mode(1), get(v:event, "new_mode"))
3297 let g:index += 1
3298 endfunc
3299
3300 au ModeChanged * :call TestMode()
3301 let g:n_to_any = 0
3302 au ModeChanged n:* let g:n_to_any += 1
3303 call feedkeys("i\<esc>vVca\<CR>\<C-X>\<C-L>\<esc>ggdG", 'tnix')
3304
3305 let g:V_to_v = 0
3306 au ModeChanged V:v let g:V_to_v += 1
3307 call feedkeys("Vv\<C-G>\<esc>", 'tnix')
3308 call assert_equal(len(filter(g:mode_seq[1:], {idx, val -> val == 'n'})), g:n_to_any)
3309 call assert_equal(1, g:V_to_v)
3310 call assert_equal(len(g:mode_seq) - 1, g:index)
3311
3312 let g:n_to_i = 0
3313 au ModeChanged n:i let g:n_to_i += 1
3314 let g:n_to_niI = 0
3315 au ModeChanged i:niI let g:n_to_niI += 1
3316 let g:niI_to_i = 0
3317 au ModeChanged niI:i let g:niI_to_i += 1
3318 let g:nany_to_i = 0
3319 au ModeChanged n*:i let g:nany_to_i += 1
3320 let g:i_to_n = 0
3321 au ModeChanged i:n let g:i_to_n += 1
3322 let g:nori_to_any = 0
3323 au ModeChanged [ni]:* let g:nori_to_any += 1
3324 let g:i_to_any = 0
3325 au ModeChanged i:* let g:i_to_any += 1
3326 let g:index = 0
3327 let g:mode_seq = ['n', 'i', 'niI', 'i', 'n']
3328 call feedkeys("a\<C-O>l\<esc>", 'tnix')
3329 call assert_equal(len(g:mode_seq) - 1, g:index)
3330 call assert_equal(1, g:n_to_i)
3331 call assert_equal(1, g:n_to_niI)
3332 call assert_equal(1, g:niI_to_i)
3333 call assert_equal(2, g:nany_to_i)
3334 call assert_equal(1, g:i_to_n)
3335 call assert_equal(2, g:i_to_any)
3336 call assert_equal(3, g:nori_to_any)
3337
3338 if has('terminal')
3339 let g:mode_seq += ['c', 'n', 't', 'nt', 'c', 'nt', 'n']
3340 call feedkeys(":term\<CR>\<C-W>N:bd!\<CR>", 'tnix')
3341 call assert_equal(len(g:mode_seq) - 1, g:index)
3342 call assert_equal(1, g:n_to_i)
3343 call assert_equal(1, g:n_to_niI)
3344 call assert_equal(1, g:niI_to_i)
3345 call assert_equal(2, g:nany_to_i)
3346 call assert_equal(1, g:i_to_n)
3347 call assert_equal(2, g:i_to_any)
3348 call assert_equal(5, g:nori_to_any)
3349 endif
3350
3351 if has('cmdwin')
3352 let g:n_to_c = 0
3353 au ModeChanged n:c let g:n_to_c += 1
3354 let g:c_to_n = 0
3355 au ModeChanged c:n let g:c_to_n += 1
3356 let g:mode_seq += ['c', 'n', 'c', 'n']
3357 call feedkeys("q:\<C-C>\<Esc>", 'tnix')
3358 call assert_equal(len(g:mode_seq) - 1, g:index)
3359 call assert_equal(2, g:n_to_c)
3360 call assert_equal(2, g:c_to_n)
3361 unlet g:n_to_c
3362 unlet g:c_to_n
3363 endif
3364
3365 au! ModeChanged
3366 delfunc TestMode
3367 unlet! g:mode_seq
3368 unlet! g:index
3369 unlet! g:n_to_any
3370 unlet! g:V_to_v
3371 unlet! g:n_to_i
3372 unlet! g:n_to_niI
3373 unlet! g:niI_to_i
3374 unlet! g:nany_to_i
3375 unlet! g:i_to_n
3376 unlet! g:nori_to_any
3377 unlet! g:i_to_any
3378endfunc
3379
3380func Test_recursive_ModeChanged()
3381 au! ModeChanged * norm 0u
3382 sil! norm 
3383 au! ModeChanged
3384endfunc
3385
3386func Test_ModeChanged_starts_visual()
3387 " This was triggering ModeChanged before setting VIsual, causing a crash.
3388 au! ModeChanged * norm 0u
3389 sil! norm 
3390
3391 au! ModeChanged
3392endfunc
Bram Moolenaar347538f2022-03-26 16:28:06 +00003393
Charlie Grovesfef44852022-04-19 16:24:12 +01003394func Test_noname_autocmd()
3395 augroup test_noname_autocmd_group
3396 autocmd!
3397 autocmd BufEnter * call add(s:li, ["BufEnter", expand("<afile>")])
3398 autocmd BufDelete * call add(s:li, ["BufDelete", expand("<afile>")])
3399 autocmd BufLeave * call add(s:li, ["BufLeave", expand("<afile>")])
3400 autocmd BufUnload * call add(s:li, ["BufUnload", expand("<afile>")])
3401 autocmd BufWipeout * call add(s:li, ["BufWipeout", expand("<afile>")])
3402 augroup END
3403
3404 let s:li = []
3405 edit foo
3406 call assert_equal([['BufUnload', ''], ['BufDelete', ''], ['BufWipeout', ''], ['BufEnter', 'foo']], s:li)
3407
3408 au! test_noname_autocmd_group
3409 augroup! test_noname_autocmd_group
3410endfunc
3411
Yegappan Lakshmanan1755a912022-05-19 10:31:47 +01003412" Test for the autocmd_get() function
3413func Test_autocmd_get()
3414 augroup TestAutoCmdFns
3415 au!
3416 autocmd BufAdd *.vim echo "bufadd-vim"
3417 autocmd BufAdd *.py echo "bufadd-py"
3418 autocmd BufHidden *.vim echo "bufhidden"
3419 augroup END
3420 augroup TestAutoCmdFns2
3421 autocmd BufAdd *.vim echo "bufadd-vim-2"
3422 autocmd BufRead *.a1b2c3 echo "bufadd-vim-2"
3423 augroup END
3424
3425 let l = autocmd_get()
3426 call assert_true(l->len() > 0)
3427
3428 " Test for getting all the autocmds in a group
3429 let expected = [
3430 \ #{cmd: 'echo "bufadd-vim"', group: 'TestAutoCmdFns',
3431 \ pattern: '*.vim', nested: v:false, once: v:false,
3432 \ event: 'BufAdd'},
3433 \ #{cmd: 'echo "bufadd-py"', group: 'TestAutoCmdFns',
3434 \ pattern: '*.py', nested: v:false, once: v:false,
3435 \ event: 'BufAdd'},
3436 \ #{cmd: 'echo "bufhidden"', group: 'TestAutoCmdFns',
3437 \ pattern: '*.vim', nested: v:false,
3438 \ once: v:false, event: 'BufHidden'}]
3439 call assert_equal(expected, autocmd_get(#{group: 'TestAutoCmdFns'}))
3440
3441 " Test for getting autocmds for all the patterns in a group
3442 call assert_equal(expected, autocmd_get(#{group: 'TestAutoCmdFns',
3443 \ event: '*'}))
3444
3445 " Test for getting autocmds for an event in a group
3446 let expected = [
3447 \ #{cmd: 'echo "bufadd-vim"', group: 'TestAutoCmdFns',
3448 \ pattern: '*.vim', nested: v:false, once: v:false,
3449 \ event: 'BufAdd'},
3450 \ #{cmd: 'echo "bufadd-py"', group: 'TestAutoCmdFns',
3451 \ pattern: '*.py', nested: v:false, once: v:false,
3452 \ event: 'BufAdd'}]
3453 call assert_equal(expected, autocmd_get(#{group: 'TestAutoCmdFns',
3454 \ event: 'BufAdd'}))
3455
3456 " Test for getting the autocmds for all the events in a group for particular
3457 " pattern
3458 call assert_equal([{'cmd': 'echo "bufadd-py"', 'group': 'TestAutoCmdFns',
3459 \ 'pattern': '*.py', 'nested': v:false, 'once': v:false,
3460 \ 'event': 'BufAdd'}],
3461 \ autocmd_get(#{group: 'TestAutoCmdFns', event: '*', pattern: '*.py'}))
3462
3463 " Test for getting the autocmds for an events in a group for particular
3464 " pattern
3465 let l = autocmd_get(#{group: 'TestAutoCmdFns', event: 'BufAdd',
3466 \ pattern: '*.vim'})
3467 call assert_equal([
3468 \ #{cmd: 'echo "bufadd-vim"', group: 'TestAutoCmdFns',
3469 \ pattern: '*.vim', nested: v:false, once: v:false,
3470 \ event: 'BufAdd'}], l)
3471
3472 " Test for getting the autocmds for a pattern in a group
3473 let l = autocmd_get(#{group: 'TestAutoCmdFns', pattern: '*.vim'})
3474 call assert_equal([
3475 \ #{cmd: 'echo "bufadd-vim"', group: 'TestAutoCmdFns',
3476 \ pattern: '*.vim', nested: v:false, once: v:false,
3477 \ event: 'BufAdd'},
3478 \ #{cmd: 'echo "bufhidden"', group: 'TestAutoCmdFns',
3479 \ pattern: '*.vim', nested: v:false,
3480 \ once: v:false, event: 'BufHidden'}], l)
3481
3482 " Test for getting the autocmds for a pattern in all the groups
3483 let l = autocmd_get(#{pattern: '*.a1b2c3'})
3484 call assert_equal([{'cmd': 'echo "bufadd-vim-2"', 'group': 'TestAutoCmdFns2',
3485 \ 'pattern': '*.a1b2c3', 'nested': v:false, 'once': v:false,
3486 \ 'event': 'BufRead'}], l)
3487
3488 " Test for getting autocmds for a pattern without any autocmds
3489 call assert_equal([], autocmd_get(#{group: 'TestAutoCmdFns',
3490 \ pattern: '*.abc'}))
3491 call assert_equal([], autocmd_get(#{group: 'TestAutoCmdFns',
3492 \ event: 'BufAdd', pattern: '*.abc'}))
3493 call assert_equal([], autocmd_get(#{group: 'TestAutoCmdFns',
3494 \ event: 'BufWipeout'}))
3495 call assert_fails("call autocmd_get(#{group: 'abc', event: 'BufAdd'})",
3496 \ 'E367:')
3497 let cmd = "echo autocmd_get(#{group: 'TestAutoCmdFns', event: 'abc'})"
3498 call assert_fails(cmd, 'E216:')
3499 call assert_fails("call autocmd_get(#{group: 'abc'})", 'E367:')
3500 call assert_fails("echo autocmd_get(#{event: 'abc'})", 'E216:')
3501
3502 augroup TestAutoCmdFns
3503 au!
3504 augroup END
3505 call assert_equal([], autocmd_get(#{group: 'TestAutoCmdFns'}))
3506
3507 " Test for nested and once autocmds
3508 augroup TestAutoCmdFns
3509 au!
3510 autocmd VimSuspend * ++nested echo "suspend"
3511 autocmd VimResume * ++once echo "resume"
3512 augroup END
3513
3514 let expected = [
3515 \ {'cmd': 'echo "suspend"', 'group': 'TestAutoCmdFns', 'pattern': '*',
3516 \ 'nested': v:true, 'once': v:false, 'event': 'VimSuspend'},
3517 \ {'cmd': 'echo "resume"', 'group': 'TestAutoCmdFns', 'pattern': '*',
3518 \ 'nested': v:false, 'once': v:true, 'event': 'VimResume'}]
3519 call assert_equal(expected, autocmd_get(#{group: 'TestAutoCmdFns'}))
3520
3521 " Test for buffer-local autocmd
3522 augroup TestAutoCmdFns
3523 au!
3524 autocmd TextYankPost <buffer> echo "textyankpost"
3525 augroup END
3526
3527 let expected = [
3528 \ {'cmd': 'echo "textyankpost"', 'group': 'TestAutoCmdFns',
3529 \ 'pattern': '<buffer=' .. bufnr() .. '>', 'nested': v:false,
3530 \ 'once': v:false, 'bufnr': bufnr(), 'event': 'TextYankPost'}]
3531 call assert_equal(expected, autocmd_get(#{group: 'TestAutoCmdFns'}))
3532
3533 augroup TestAutoCmdFns
3534 au!
3535 augroup END
3536 augroup! TestAutoCmdFns
3537 augroup TestAutoCmdFns2
3538 au!
3539 augroup END
3540 augroup! TestAutoCmdFns2
3541
3542 call assert_fails("echo autocmd_get(#{group: []})", 'E730:')
3543 call assert_fails("echo autocmd_get(#{event: {}})", 'E731:')
3544 call assert_fails("echo autocmd_get([])", 'E1206:')
3545endfunc
3546
3547" Test for the autocmd_add() function
3548func Test_autocmd_add()
3549 " Define a single autocmd in a group
3550 call autocmd_add([#{group: 'TestAcSet', event: 'BufAdd', pattern: '*.sh',
3551 \ cmd: 'echo "bufadd"', once: v:true, nested: v:true}])
3552 call assert_equal([#{cmd: 'echo "bufadd"', group: 'TestAcSet',
3553 \ pattern: '*.sh', nested: v:true, once: v:true,
3554 \ event: 'BufAdd'}], autocmd_get(#{group: 'TestAcSet'}))
3555
3556 " Define two autocmds in the same group
3557 call autocmd_delete([#{group: 'TestAcSet'}])
3558 call autocmd_add([#{group: 'TestAcSet', event: 'BufAdd', pattern: '*.sh',
3559 \ cmd: 'echo "bufadd"'},
3560 \ #{group: 'TestAcSet', event: 'BufEnter', pattern: '*.sh',
3561 \ cmd: 'echo "bufenter"'}])
3562 call assert_equal([
3563 \ #{cmd: 'echo "bufadd"', group: 'TestAcSet', pattern: '*.sh',
3564 \ nested: v:false, once: v:false, event: 'BufAdd'},
3565 \ #{cmd: 'echo "bufenter"', group: 'TestAcSet', pattern: '*.sh',
3566 \ nested: v:false, once: v:false, event: 'BufEnter'}],
3567 \ autocmd_get(#{group: 'TestAcSet'}))
3568
3569 " Define a buffer-local autocmd
3570 call autocmd_delete([#{group: 'TestAcSet'}])
3571 call autocmd_add([#{group: 'TestAcSet', event: 'CursorHold',
3572 \ bufnr: bufnr(), cmd: 'echo "cursorhold"'}])
3573 call assert_equal([
3574 \ #{cmd: 'echo "cursorhold"', group: 'TestAcSet',
3575 \ pattern: '<buffer=' .. bufnr() .. '>', nested: v:false,
3576 \ once: v:false, bufnr: bufnr(), event: 'CursorHold'}],
3577 \ autocmd_get(#{group: 'TestAcSet'}))
3578
3579 " Use an invalid buffer number
3580 call autocmd_delete([#{group: 'TestAcSet'}])
3581 call autocmd_add([#{group: 'TestAcSet', event: 'BufEnter',
3582 \ bufnr: -1, cmd: 'echo "bufenter"'}])
3583 let l = [#{group: 'TestAcSet', event: 'BufAdd', bufnr: 9999,
3584 \ cmd: 'echo "bufadd"'}]
3585 call assert_fails("echo autocmd_add(l)", 'E680:')
Yegappan Lakshmanan00e977c2022-06-01 12:31:53 +01003586 let l = [#{group: 'TestAcSet', event: 'BufAdd', bufnr: 9999,
3587 \ pattern: '*.py', cmd: 'echo "bufadd"'}]
3588 call assert_fails("echo autocmd_add(l)", 'E680:')
3589 let l = [#{group: 'TestAcSet', event: 'BufAdd', bufnr: 9999,
3590 \ pattern: ['*.py', '*.c'], cmd: 'echo "bufadd"'}]
3591 call assert_fails("echo autocmd_add(l)", 'E680:')
Yegappan Lakshmanan1755a912022-05-19 10:31:47 +01003592 let l = [#{group: 'TestAcSet', event: 'BufRead', bufnr: [],
3593 \ cmd: 'echo "bufread"'}]
3594 call assert_fails("echo autocmd_add(l)", 'E745:')
3595 call assert_equal([], autocmd_get(#{group: 'TestAcSet'}))
3596
3597 " Add two commands to the same group, event and pattern
3598 call autocmd_delete([#{group: 'TestAcSet'}])
3599 call autocmd_add([#{group: 'TestAcSet', event: 'BufUnload',
3600 \ pattern: 'abc', cmd: 'echo "cmd1"'}])
3601 call autocmd_add([#{group: 'TestAcSet', event: 'BufUnload',
3602 \ pattern: 'abc', cmd: 'echo "cmd2"'}])
3603 call assert_equal([
3604 \ #{cmd: 'echo "cmd1"', group: 'TestAcSet', pattern: 'abc',
3605 \ nested: v:false, once: v:false, event: 'BufUnload'},
3606 \ #{cmd: 'echo "cmd2"', group: 'TestAcSet', pattern: 'abc',
3607 \ nested: v:false, once: v:false, event: 'BufUnload'}],
3608 \ autocmd_get(#{group: 'TestAcSet'}))
3609
3610 " When adding a new autocmd, if the autocmd 'group' is not specified, then
3611 " the current autocmd group should be used.
3612 call autocmd_delete([#{group: 'TestAcSet'}])
3613 augroup TestAcSet
3614 call autocmd_add([#{event: 'BufHidden', pattern: 'abc', cmd: 'echo "abc"'}])
3615 augroup END
3616 call assert_equal([
3617 \ #{cmd: 'echo "abc"', group: 'TestAcSet', pattern: 'abc',
3618 \ nested: v:false, once: v:false, event: 'BufHidden'}],
3619 \ autocmd_get(#{group: 'TestAcSet'}))
3620
Yegappan Lakshmanan971f6822022-05-24 11:40:11 +01003621 " Test for replacing a cmd for an event in a group
3622 call autocmd_delete([#{group: 'TestAcSet'}])
3623 call autocmd_add([#{replace: v:true, group: 'TestAcSet', event: 'BufEnter',
3624 \ pattern: '*.py', cmd: 'echo "bufenter"'}])
3625 call autocmd_add([#{replace: v:true, group: 'TestAcSet', event: 'BufEnter',
3626 \ pattern: '*.py', cmd: 'echo "bufenter"'}])
3627 call assert_equal([
3628 \ #{cmd: 'echo "bufenter"', group: 'TestAcSet', pattern: '*.py',
3629 \ nested: v:false, once: v:false, event: 'BufEnter'}],
3630 \ autocmd_get(#{group: 'TestAcSet'}))
3631
3632 " Test for adding a command for an unsupported autocmd event
Yegappan Lakshmanan1755a912022-05-19 10:31:47 +01003633 let l = [#{group: 'TestAcSet', event: 'abc', pattern: '*.sh',
3634 \ cmd: 'echo "bufadd"'}]
3635 call assert_fails('call autocmd_add(l)', 'E216:')
3636
Yegappan Lakshmanane0ff3a72022-05-27 18:05:33 +01003637 " Test for using a list of events and patterns
3638 call autocmd_delete([#{group: 'TestAcSet'}])
3639 let l = [#{group: 'TestAcSet', event: ['BufEnter', 'BufLeave'],
3640 \ pattern: ['*.py', '*.sh'], cmd: 'echo "bufcmds"'}]
3641 call autocmd_add(l)
3642 call assert_equal([
3643 \ #{cmd: 'echo "bufcmds"', group: 'TestAcSet', pattern: '*.py',
3644 \ nested: v:false, once: v:false, event: 'BufEnter'},
3645 \ #{cmd: 'echo "bufcmds"', group: 'TestAcSet', pattern: '*.sh',
3646 \ nested: v:false, once: v:false, event: 'BufEnter'},
3647 \ #{cmd: 'echo "bufcmds"', group: 'TestAcSet', pattern: '*.py',
3648 \ nested: v:false, once: v:false, event: 'BufLeave'},
3649 \ #{cmd: 'echo "bufcmds"', group: 'TestAcSet', pattern: '*.sh',
3650 \ nested: v:false, once: v:false, event: 'BufLeave'}],
3651 \ autocmd_get(#{group: 'TestAcSet'}))
3652
3653 " Test for invalid values for 'event' item
3654 call autocmd_delete([#{group: 'TestAcSet'}])
3655 let l = [#{group: 'TestAcSet', event: test_null_string(),
3656 \ pattern: "*.py", cmd: 'echo "bufcmds"'}]
3657 call assert_fails('call autocmd_add(l)', 'E928:')
3658 let l = [#{group: 'TestAcSet', event: test_null_list(),
3659 \ pattern: "*.py", cmd: 'echo "bufcmds"'}]
3660 call assert_fails('call autocmd_add(l)', 'E714:')
3661 let l = [#{group: 'TestAcSet', event: {},
3662 \ pattern: "*.py", cmd: 'echo "bufcmds"'}]
3663 call assert_fails('call autocmd_add(l)', 'E777:')
3664 let l = [#{group: 'TestAcSet', event: [{}],
3665 \ pattern: "*.py", cmd: 'echo "bufcmds"'}]
3666 call assert_fails('call autocmd_add(l)', 'E928:')
3667 let l = [#{group: 'TestAcSet', event: [test_null_string()],
3668 \ pattern: "*.py", cmd: 'echo "bufcmds"'}]
3669 call assert_fails('call autocmd_add(l)', 'E928:')
3670 let l = [#{group: 'TestAcSet', event: 'BufEnter,BufLeave',
3671 \ pattern: '*.py', cmd: 'echo "bufcmds"'}]
3672 call assert_fails('call autocmd_add(l)', 'E216:')
3673 let l = [#{group: 'TestAcSet', event: [],
3674 \ pattern: "*.py", cmd: 'echo "bufcmds"'}]
3675 call autocmd_add(l)
3676 let l = [#{group: 'TestAcSet', event: [""],
3677 \ pattern: "*.py", cmd: 'echo "bufcmds"'}]
3678 call assert_fails('call autocmd_add(l)', 'E216:')
3679 let l = [#{group: 'TestAcSet', event: "",
3680 \ pattern: "*.py", cmd: 'echo "bufcmds"'}]
3681 call autocmd_add(l)
3682 call assert_equal([], autocmd_get(#{group: 'TestAcSet'}))
3683
3684 " Test for invalid values for 'pattern' item
3685 let l = [#{group: 'TestAcSet', event: "BufEnter",
3686 \ pattern: test_null_string(), cmd: 'echo "bufcmds"'}]
Yegappan Lakshmanan00e977c2022-06-01 12:31:53 +01003687 call assert_fails('call autocmd_add(l)', 'E928:')
Yegappan Lakshmanane0ff3a72022-05-27 18:05:33 +01003688 let l = [#{group: 'TestAcSet', event: "BufEnter",
3689 \ pattern: test_null_list(), cmd: 'echo "bufcmds"'}]
3690 call assert_fails('call autocmd_add(l)', 'E714:')
3691 let l = [#{group: 'TestAcSet', event: "BufEnter",
3692 \ pattern: {}, cmd: 'echo "bufcmds"'}]
3693 call assert_fails('call autocmd_add(l)', 'E777:')
3694 let l = [#{group: 'TestAcSet', event: "BufEnter",
3695 \ pattern: [{}], cmd: 'echo "bufcmds"'}]
3696 call assert_fails('call autocmd_add(l)', 'E928:')
3697 let l = [#{group: 'TestAcSet', event: "BufEnter",
3698 \ pattern: [test_null_string()], cmd: 'echo "bufcmds"'}]
3699 call assert_fails('call autocmd_add(l)', 'E928:')
3700 let l = [#{group: 'TestAcSet', event: "BufEnter",
3701 \ pattern: [], cmd: 'echo "bufcmds"'}]
3702 call autocmd_add(l)
3703 let l = [#{group: 'TestAcSet', event: "BufEnter",
3704 \ pattern: [""], cmd: 'echo "bufcmds"'}]
3705 call autocmd_add(l)
3706 let l = [#{group: 'TestAcSet', event: "BufEnter",
3707 \ pattern: "", cmd: 'echo "bufcmds"'}]
3708 call autocmd_add(l)
3709 call assert_equal([], autocmd_get(#{group: 'TestAcSet'}))
3710
3711 let l = [#{group: 'TestAcSet', event: 'BufEnter,abc,BufLeave',
3712 \ pattern: '*.py', cmd: 'echo "bufcmds"'}]
3713 call assert_fails('call autocmd_add(l)', 'E216:')
3714
Yegappan Lakshmanan1755a912022-05-19 10:31:47 +01003715 call assert_fails("call autocmd_add({})", 'E1211:')
3716 call assert_equal(v:false, autocmd_add(test_null_list()))
3717 call assert_true(autocmd_add([[]]))
3718 call assert_true(autocmd_add([test_null_dict()]))
3719
3720 augroup TestAcSet
3721 au!
3722 augroup END
3723
3724 call autocmd_add([#{group: 'TestAcSet'}])
3725 call autocmd_add([#{group: 'TestAcSet', event: 'BufAdd'}])
3726 call autocmd_add([#{group: 'TestAcSet', pat: '*.sh'}])
3727 call autocmd_add([#{group: 'TestAcSet', cmd: 'echo "a"'}])
3728 call autocmd_add([#{group: 'TestAcSet', event: 'BufAdd', pat: '*.sh'}])
3729 call autocmd_add([#{group: 'TestAcSet', event: 'BufAdd', cmd: 'echo "a"'}])
3730 call autocmd_add([#{group: 'TestAcSet', pat: '*.sh', cmd: 'echo "a"'}])
3731 call assert_equal([], autocmd_get(#{group: 'TestAcSet'}))
3732
3733 augroup! TestAcSet
3734endfunc
3735
3736" Test for deleting autocmd events and groups
3737func Test_autocmd_delete()
3738 " Delete an event in an autocmd group
3739 augroup TestAcSet
3740 au!
3741 au BufAdd *.sh echo "bufadd"
3742 au BufEnter *.sh echo "bufenter"
3743 augroup END
3744 call autocmd_delete([#{group: 'TestAcSet', event: 'BufAdd'}])
3745 call assert_equal([#{cmd: 'echo "bufenter"', group: 'TestAcSet',
3746 \ pattern: '*.sh', nested: v:false, once: v:false,
3747 \ event: 'BufEnter'}], autocmd_get(#{group: 'TestAcSet'}))
3748
3749 " Delete all the events in an autocmd group
3750 augroup TestAcSet
3751 au BufAdd *.sh echo "bufadd"
3752 augroup END
3753 call autocmd_delete([#{group: 'TestAcSet', event: '*'}])
3754 call assert_equal([], autocmd_get(#{group: 'TestAcSet'}))
3755
3756 " Delete a non-existing autocmd group
3757 call assert_fails("call autocmd_delete([#{group: 'abc'}])", 'E367:')
3758 " Delete a non-existing autocmd event
3759 let l = [#{group: 'TestAcSet', event: 'abc'}]
3760 call assert_fails("call autocmd_delete(l)", 'E216:')
3761 " Delete a non-existing autocmd pattern
3762 let l = [#{group: 'TestAcSet', event: 'BufAdd', pat: 'abc'}]
3763 call assert_true(autocmd_delete(l))
Yegappan Lakshmanan00e977c2022-06-01 12:31:53 +01003764 " Delete an autocmd for a non-existing buffer
3765 let l = [#{event: '*', bufnr: 9999, cmd: 'echo "x"'}]
3766 call assert_fails('call autocmd_delete(l)', 'E680:')
Yegappan Lakshmanan1755a912022-05-19 10:31:47 +01003767
3768 " Delete an autocmd group
3769 augroup TestAcSet
3770 au!
3771 au BufAdd *.sh echo "bufadd"
3772 au BufEnter *.sh echo "bufenter"
3773 augroup END
3774 call autocmd_delete([#{group: 'TestAcSet'}])
3775 call assert_fails("call autocmd_get(#{group: 'TestAcSet'})", 'E367:')
3776
3777 call assert_true(autocmd_delete([[]]))
3778 call assert_true(autocmd_delete([test_null_dict()]))
3779endfunc
3780
Bram Moolenaarbc2b71d2020-02-17 21:33:30 +01003781" vim: shiftwidth=2 sts=2 expandtab