blob: b3b4ec5a6fcf1ff0c6f283f253974c46dd3f8d0a [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
Bram Moolenaare7cda972022-08-29 11:02:59 +010027 call writefile(['one', 'two', 'three'], 'XoneTwoThree')
Bram Moolenaar75911162020-07-21 19:44:47 +020028 let before =<< trim END
29 set updatetime=10
Bram Moolenaare7cda972022-08-29 11:02:59 +010030 au CursorHold * call writefile([line('.')], 'XCHoutput', 'a')
Bram Moolenaar75911162020-07-21 19:44:47 +020031 END
Bram Moolenaare7cda972022-08-29 11:02:59 +010032 call writefile(before, 'XCHinit')
33 let buf = RunVimInTerminal('-S XCHinit XoneTwoThree', {})
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 Moolenaare7cda972022-08-29 11:02:59 +010038 call WaitForAssert({-> assert_equal(['1'], readfile('XCHoutput')[-1:-1])})
Bram Moolenaar75911162020-07-21 19:44:47 +020039 call term_sendkeys(buf, "j")
40 call term_wait(buf)
Bram Moolenaare7cda972022-08-29 11:02:59 +010041 call WaitForAssert({-> assert_equal(['1', '2'], readfile('XCHoutput')[-2:-1])})
Bram Moolenaar75911162020-07-21 19:44:47 +020042 call term_sendkeys(buf, "j")
43 call term_wait(buf)
Bram Moolenaare7cda972022-08-29 11:02:59 +010044 call WaitForAssert({-> assert_equal(['1', '2', '3'], readfile('XCHoutput')[-3:-1])})
Bram Moolenaar75911162020-07-21 19:44:47 +020045 call StopVimInTerminal(buf)
46
Bram Moolenaare7cda972022-08-29 11:02:59 +010047 call delete('XCHinit')
48 call delete('XCHoutput')
49 call delete('XoneTwoThree')
Bram Moolenaar75911162020-07-21 19:44:47 +020050endfunc
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
zeertzjq670ab032022-08-28 19:16:15 +0100422func Test_WinScrolled_long_wrapped()
423 CheckRunVimInTerminal
424
425 let lines =<< trim END
426 set scrolloff=0
427 let height = winheight(0)
428 let width = winwidth(0)
429 let g:scrolled = 0
430 au WinScrolled * let g:scrolled += 1
431 call setline(1, repeat('foo', height * width))
432 call cursor(1, height * width)
433 END
434 call writefile(lines, 'Xtest_winscrolled_long_wrapped')
435 let buf = RunVimInTerminal('-S Xtest_winscrolled_long_wrapped', {'rows': 6})
436
437 call term_sendkeys(buf, ":echo g:scrolled\<CR>")
438 call WaitForAssert({-> assert_match('^0 ', term_getline(buf, 6))}, 1000)
439
440 call term_sendkeys(buf, 'gj')
441 call term_sendkeys(buf, ":echo g:scrolled\<CR>")
442 call WaitForAssert({-> assert_match('^1 ', term_getline(buf, 6))}, 1000)
443
444 call term_sendkeys(buf, '0')
445 call term_sendkeys(buf, ":echo g:scrolled\<CR>")
446 call WaitForAssert({-> assert_match('^2 ', term_getline(buf, 6))}, 1000)
447
448 call term_sendkeys(buf, '$')
449 call term_sendkeys(buf, ":echo g:scrolled\<CR>")
450 call WaitForAssert({-> assert_match('^3 ', term_getline(buf, 6))}, 1000)
451
452 call delete('Xtest_winscrolled_long_wrapped')
453endfunc
454
naohiro ono23beefe2021-11-13 12:38:49 +0000455func Test_WinClosed()
456 " Test that the pattern is matched against the closed window's ID, and both
457 " <amatch> and <afile> are set to it.
458 new
459 let winid = win_getid()
460 let g:matched = v:false
461 augroup test-WinClosed
462 autocmd!
463 execute 'autocmd WinClosed' winid 'let g:matched = v:true'
464 autocmd WinClosed * let g:amatch = str2nr(expand('<amatch>'))
465 autocmd WinClosed * let g:afile = str2nr(expand('<afile>'))
466 augroup END
467 close
468 call assert_true(g:matched)
469 call assert_equal(winid, g:amatch)
470 call assert_equal(winid, g:afile)
471
472 " Test that WinClosed is non-recursive.
473 new
474 new
475 call assert_equal(3, winnr('$'))
476 let g:triggered = 0
477 augroup test-WinClosed
478 autocmd!
479 autocmd WinClosed * let g:triggered += 1
480 autocmd WinClosed * 2 wincmd c
481 augroup END
482 close
483 call assert_equal(1, winnr('$'))
484 call assert_equal(1, g:triggered)
485
486 autocmd! test-WinClosed
487 augroup! test-WinClosed
488 unlet g:matched
489 unlet g:amatch
490 unlet g:afile
491 unlet g:triggered
492endfunc
493
Bram Moolenaarc947b9a2022-04-06 17:59:21 +0100494func Test_WinClosed_throws()
495 vnew
496 let bnr = bufnr()
497 call assert_equal(1, bufloaded(bnr))
498 augroup test-WinClosed
499 autocmd WinClosed * throw 'foo'
500 augroup END
501 try
502 close
503 catch /.*/
504 endtry
505 call assert_equal(0, bufloaded(bnr))
506
507 autocmd! test-WinClosed
508 augroup! test-WinClosed
509endfunc
510
zeertzjq6a069402022-04-07 14:08:29 +0100511func Test_WinClosed_throws_with_tabs()
512 tabnew
513 let bnr = bufnr()
514 call assert_equal(1, bufloaded(bnr))
515 augroup test-WinClosed
516 autocmd WinClosed * throw 'foo'
517 augroup END
518 try
519 close
520 catch /.*/
521 endtry
522 call assert_equal(0, bufloaded(bnr))
523
524 autocmd! test-WinClosed
525 augroup! test-WinClosed
526endfunc
527
Bram Moolenaare99e8442016-07-26 20:43:40 +0200528func s:AddAnAutocmd()
529 augroup vimBarTest
530 au BufReadCmd * echo 'hello'
531 augroup END
532 call assert_equal(3, len(split(execute('au vimBarTest'), "\n")))
533endfunc
534
535func Test_early_bar()
536 " test that a bar is recognized before the {event}
537 call s:AddAnAutocmd()
Bram Moolenaarb8e642f2021-11-20 10:38:25 +0000538 augroup vimBarTest | au! | let done = 77 | augroup END
Bram Moolenaare99e8442016-07-26 20:43:40 +0200539 call assert_equal(1, len(split(execute('au vimBarTest'), "\n")))
Bram Moolenaarb8e642f2021-11-20 10:38:25 +0000540 call assert_equal(77, done)
Bram Moolenaare99e8442016-07-26 20:43:40 +0200541
542 call s:AddAnAutocmd()
Bram Moolenaarb8e642f2021-11-20 10:38:25 +0000543 augroup vimBarTest| au!| let done = 88 | augroup END
Bram Moolenaare99e8442016-07-26 20:43:40 +0200544 call assert_equal(1, len(split(execute('au vimBarTest'), "\n")))
Bram Moolenaarb8e642f2021-11-20 10:38:25 +0000545 call assert_equal(88, done)
Bram Moolenaare99e8442016-07-26 20:43:40 +0200546
547 " test that a bar is recognized after the {event}
548 call s:AddAnAutocmd()
Bram Moolenaarb8e642f2021-11-20 10:38:25 +0000549 augroup vimBarTest| au!BufReadCmd| let done = 99 | augroup END
Bram Moolenaare99e8442016-07-26 20:43:40 +0200550 call assert_equal(1, len(split(execute('au vimBarTest'), "\n")))
Bram Moolenaarb8e642f2021-11-20 10:38:25 +0000551 call assert_equal(99, done)
Bram Moolenaare99e8442016-07-26 20:43:40 +0200552
553 " test that a bar is recognized after the {group}
554 call s:AddAnAutocmd()
555 au! vimBarTest|echo 'hello'
556 call assert_equal(1, len(split(execute('au vimBarTest'), "\n")))
557endfunc
Bram Moolenaarf2c4c392016-07-29 20:50:24 +0200558
Bram Moolenaar5c809082016-09-01 16:21:48 +0200559func RemoveGroup()
560 autocmd! StartOK
561 augroup! StartOK
562endfunc
563
Bram Moolenaarf2c4c392016-07-29 20:50:24 +0200564func Test_augroup_warning()
565 augroup TheWarning
566 au VimEnter * echo 'entering'
567 augroup END
Bram Moolenaar5dc4e2f2020-11-25 14:15:12 +0100568 call assert_match("TheWarning.*VimEnter", execute('au VimEnter'))
Bram Moolenaarf2c4c392016-07-29 20:50:24 +0200569 redir => res
570 augroup! TheWarning
571 redir END
Bram Moolenaar5dc4e2f2020-11-25 14:15:12 +0100572 call assert_match("W19:", res)
573 call assert_match("-Deleted-.*VimEnter", execute('au VimEnter'))
Bram Moolenaarf2c4c392016-07-29 20:50:24 +0200574
575 " check "Another" does not take the pace of the deleted entry
576 augroup Another
577 augroup END
Bram Moolenaar5dc4e2f2020-11-25 14:15:12 +0100578 call assert_match("-Deleted-.*VimEnter", execute('au VimEnter'))
Bram Moolenaaraeac9002016-09-06 22:15:08 +0200579 augroup! Another
Bram Moolenaar5c809082016-09-01 16:21:48 +0200580
581 " no warning for postpone aucmd delete
582 augroup StartOK
583 au VimEnter * call RemoveGroup()
584 augroup END
Bram Moolenaar5dc4e2f2020-11-25 14:15:12 +0100585 call assert_match("StartOK.*VimEnter", execute('au VimEnter'))
Bram Moolenaar5c809082016-09-01 16:21:48 +0200586 redir => res
587 doautocmd VimEnter
588 redir END
Bram Moolenaar5dc4e2f2020-11-25 14:15:12 +0100589 call assert_notmatch("W19:", res)
Bram Moolenaarde653f02016-09-03 16:59:06 +0200590 au! VimEnter
Bram Moolenaarad48e6c2020-04-21 22:19:45 +0200591
592 call assert_fails('augroup!', 'E471:')
Bram Moolenaarf2c4c392016-07-29 20:50:24 +0200593endfunc
Bram Moolenaarb62cc362016-09-03 16:43:53 +0200594
Bram Moolenaar8d84ff12017-10-26 16:42:16 +0200595func Test_BufReadCmdHelp()
596 " This used to cause access to free memory
597 au BufReadCmd * e +h
598 help
599
Bram Moolenaar8d84ff12017-10-26 16:42:16 +0200600 au! BufReadCmd
601endfunc
602
603func Test_BufReadCmdHelpJump()
604 " This used to cause access to free memory
605 au BufReadCmd * e +h{
Bram Moolenaarcf1ba352017-10-27 00:55:04 +0200606 " } to fix highlighting
607 call assert_fails('help', 'E434:')
Bram Moolenaar8d84ff12017-10-26 16:42:16 +0200608
Bram Moolenaar8d84ff12017-10-26 16:42:16 +0200609 au! BufReadCmd
610endfunc
611
zeertzjq93f72cc2022-08-26 15:34:52 +0100612" BufReadCmd is triggered for a "nofile" buffer. Check all values.
Bram Moolenaarb1d2c812022-08-26 11:55:01 +0100613func Test_BufReadCmdNofile()
zeertzjq93f72cc2022-08-26 15:34:52 +0100614 for val in ['nofile',
615 \ 'nowrite',
616 \ 'acwrite',
617 \ 'quickfix',
618 \ 'help',
619 \ 'terminal',
620 \ 'prompt',
621 \ 'popup',
622 \ ]
623 new somefile
624 exe 'set buftype=' .. val
625 au BufReadCmd somefile call setline(1, 'triggered')
626 edit
627 call assert_equal('triggered', getline(1))
Bram Moolenaarb1d2c812022-08-26 11:55:01 +0100628
zeertzjq93f72cc2022-08-26 15:34:52 +0100629 au! BufReadCmd
630 bwipe!
631 endfor
Bram Moolenaarb1d2c812022-08-26 11:55:01 +0100632endfunc
633
Bram Moolenaarb62cc362016-09-03 16:43:53 +0200634func Test_augroup_deleted()
Bram Moolenaarde653f02016-09-03 16:59:06 +0200635 " This caused a crash before E936 was introduced
Bram Moolenaarb62cc362016-09-03 16:43:53 +0200636 augroup x
Bram Moolenaarde653f02016-09-03 16:59:06 +0200637 call assert_fails('augroup! x', 'E936:')
638 au VimEnter * echo
639 augroup end
Bram Moolenaarb62cc362016-09-03 16:43:53 +0200640 augroup! x
Bram Moolenaar5dc4e2f2020-11-25 14:15:12 +0100641 call assert_match("-Deleted-.*VimEnter", execute('au VimEnter'))
Bram Moolenaarde653f02016-09-03 16:59:06 +0200642 au! VimEnter
Bram Moolenaarb62cc362016-09-03 16:43:53 +0200643endfunc
644
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200645" Tests for autocommands on :close command.
646" This used to be in test13.
647func Test_three_windows()
Bram Moolenaarb3435b02016-09-29 20:54:59 +0200648 " Clean up buffers, because in some cases this function fails.
649 call s:cleanup_buffers()
650
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200651 " Write three files and open them, each in a window.
652 " Then go to next window, with autocommand that deletes the previous one.
653 " Do this twice, writing the file.
654 e! Xtestje1
655 call setline(1, 'testje1')
656 w
657 sp Xtestje2
658 call setline(1, 'testje2')
659 w
660 sp Xtestje3
661 call setline(1, 'testje3')
662 w
663 wincmd w
664 au WinLeave Xtestje2 bwipe
665 wincmd w
666 call assert_equal('Xtestje1', expand('%'))
667
668 au WinLeave Xtestje1 bwipe Xtestje3
669 close
670 call assert_equal('Xtestje1', expand('%'))
671
672 " Test deleting the buffer on a Unload event. If this goes wrong there
673 " will be the ATTENTION prompt.
674 e Xtestje1
675 au!
676 au! BufUnload Xtestje1 bwipe
677 call assert_fails('e Xtestje3', 'E937:')
678 call assert_equal('Xtestje3', expand('%'))
679
680 e Xtestje2
681 sp Xtestje1
682 call assert_fails('e', 'E937:')
Bram Moolenaara997b452018-04-17 23:24:06 +0200683 call assert_equal('Xtestje1', expand('%'))
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200684
685 " Test changing buffers in a BufWipeout autocommand. If this goes wrong
686 " there are ml_line errors and/or a Crash.
687 au!
688 only
689 e Xanother
690 e Xtestje1
691 bwipe Xtestje2
692 bwipe Xtestje3
693 au BufWipeout Xtestje1 buf Xtestje1
694 bwipe
695 call assert_equal('Xanother', expand('%'))
696
697 only
698 help
699 wincmd w
700 1quit
701 call assert_equal('Xanother', expand('%'))
702
703 au!
Bram Moolenaar4520d442017-03-19 16:09:46 +0100704 enew
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200705 call delete('Xtestje1')
706 call delete('Xtestje2')
707 call delete('Xtestje3')
708endfunc
Bram Moolenaare13b9af2017-01-13 22:01:02 +0100709
710func Test_BufEnter()
711 au! BufEnter
712 au Bufenter * let val = val . '+'
713 let g:val = ''
714 split NewFile
715 call assert_equal('+', g:val)
716 bwipe!
717 call assert_equal('++', g:val)
718
719 " Also get BufEnter when editing a directory
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +0100720 call mkdir('Xbufenterdir')
721 split Xbufenterdir
Bram Moolenaare13b9af2017-01-13 22:01:02 +0100722 call assert_equal('+++', g:val)
Bram Moolenaare94260f2017-03-21 15:50:12 +0100723
724 " On MS-Windows we can't edit the directory, make sure we wipe the right
725 " buffer.
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +0100726 bwipe! Xbufenterdir
727 call delete('Xbufenterdir', 'd')
Bram Moolenaare13b9af2017-01-13 22:01:02 +0100728 au! BufEnter
Bram Moolenaara9b5b852022-08-26 13:16:20 +0100729
730 " Editing a "nofile" buffer doesn't read the file but does trigger BufEnter
zeertzjq93f72cc2022-08-26 15:34:52 +0100731 " for historic reasons. Also test other 'buftype' values.
732 for val in ['nofile',
733 \ 'nowrite',
734 \ 'acwrite',
735 \ 'quickfix',
736 \ 'help',
737 \ 'terminal',
738 \ 'prompt',
739 \ 'popup',
740 \ ]
741 new somefile
742 exe 'set buftype=' .. val
743 au BufEnter somefile call setline(1, 'some text')
744 edit
745 call assert_equal('some text', getline(1))
746 bwipe!
747 au! BufEnter
748 endfor
Bram Moolenaare13b9af2017-01-13 22:01:02 +0100749endfunc
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100750
751" Closing a window might cause an endless loop
752" E814 for older Vims
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200753func Test_autocmd_bufwipe_in_SessLoadPost()
Bram Moolenaar1d68d9b2017-10-13 22:33:32 +0200754 edit Xtest
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100755 tabnew
Bram Moolenaar1d68d9b2017-10-13 22:33:32 +0200756 file Xsomething
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100757 set noswapfile
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100758 mksession!
759
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200760 let content =<< trim [CODE]
Bram Moolenaar62cd26a2020-10-11 20:08:44 +0200761 call test_override('ui_delay', 10)
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200762 set nocp noswapfile
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100763 let v:swapchoice = "e"
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200764 augroup test_autocmd_sessionload
765 autocmd!
766 autocmd SessionLoadPost * exe bufnr("Xsomething") . "bw!"
767 augroup END
768
769 func WriteErrors()
770 call writefile([execute("messages")], "Xerrors")
771 endfunc
772 au VimLeave * call WriteErrors()
773 [CODE]
774
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100775 call writefile(content, 'Xvimrc')
Bram Moolenaar93344c22019-08-14 21:12:05 +0200776 call system(GetVimCommand('Xvimrc') .. ' --not-a-term --noplugins -S Session.vim -c cq')
Bram Moolenaare94260f2017-03-21 15:50:12 +0100777 let errors = join(readfile('Xerrors'))
Bram Moolenaare2e40752020-09-04 21:18:46 +0200778 call assert_match('E814:', errors)
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100779
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100780 set swapfile
Bram Moolenaare94260f2017-03-21 15:50:12 +0100781 for file in ['Session.vim', 'Xvimrc', 'Xerrors']
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100782 call delete(file)
783 endfor
784endfunc
785
Bram Moolenaar797e63b2021-01-15 16:22:52 +0100786" Using :blast and :ball for many events caused a crash, because b_nwindows was
787" not incremented correctly.
788func Test_autocmd_blast_badd()
789 let content =<< trim [CODE]
790 au BufNew,BufAdd,BufWinEnter,BufEnter,BufLeave,BufWinLeave,BufUnload,VimEnter foo* blast
791 edit foo1
792 au BufNew,BufAdd,BufWinEnter,BufEnter,BufLeave,BufWinLeave,BufUnload,VimEnter foo* ball
793 edit foo2
794 call writefile(['OK'], 'Xerrors')
795 qall
796 [CODE]
797
798 call writefile(content, 'XblastBall')
799 call system(GetVimCommand() .. ' --clean -S XblastBall')
800 call assert_match('OK', readfile('Xerrors')->join())
801
802 call delete('XblastBall')
803 call delete('Xerrors')
804endfunc
805
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100806" SEGV occurs in older versions.
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200807func Test_autocmd_bufwipe_in_SessLoadPost2()
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100808 tabnew
809 set noswapfile
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100810 mksession!
811
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200812 let content =<< trim [CODE]
813 set nocp noswapfile
814 function! DeleteInactiveBufs()
815 tabfirst
816 let tabblist = []
817 for i in range(1, tabpagenr(''$''))
818 call extend(tabblist, tabpagebuflist(i))
819 endfor
820 for b in range(1, bufnr(''$''))
821 if bufexists(b) && buflisted(b) && (index(tabblist, b) == -1 || bufname(b) =~# ''^$'')
822 exec ''bwipeout '' . b
823 endif
824 endfor
825 echomsg "SessionLoadPost DONE"
826 endfunction
827 au SessionLoadPost * call DeleteInactiveBufs()
828
829 func WriteErrors()
830 call writefile([execute("messages")], "Xerrors")
831 endfunc
832 au VimLeave * call WriteErrors()
833 [CODE]
834
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100835 call writefile(content, 'Xvimrc')
Bram Moolenaar93344c22019-08-14 21:12:05 +0200836 call system(GetVimCommand('Xvimrc') .. ' --not-a-term --noplugins -S Session.vim -c cq')
Bram Moolenaare94260f2017-03-21 15:50:12 +0100837 let errors = join(readfile('Xerrors'))
838 " This probably only ever matches on unix.
839 call assert_notmatch('Caught deadly signal SEGV', errors)
840 call assert_match('SessionLoadPost DONE', errors)
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100841
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100842 set swapfile
Bram Moolenaare94260f2017-03-21 15:50:12 +0100843 for file in ['Session.vim', 'Xvimrc', 'Xerrors']
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100844 call delete(file)
845 endfor
846endfunc
Bram Moolenaarfaf29d72017-07-09 11:07:16 +0200847
848func Test_empty_doau()
849 doau \|
850endfunc
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200851
852func s:AutoCommandOptionSet(match)
Bram Moolenaard7c96872019-06-15 17:12:48 +0200853 let template = "Option: <%s>, OldVal: <%s>, OldValLocal: <%s>, OldValGlobal: <%s>, NewVal: <%s>, Scope: <%s>, Command: <%s>\n"
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200854 let item = remove(g:options, 0)
Bram Moolenaard7c96872019-06-15 17:12:48 +0200855 let expected = printf(template, item[0], item[1], item[2], item[3], item[4], item[5], item[6])
856 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 +0200857 let g:opt = [expected, actual]
858 "call assert_equal(expected, actual)
859endfunc
860
861func Test_OptionSet()
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +0200862 CheckOption autochdir
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200863
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +0200864 badd test_autocmd.vim
865
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200866 call test_override('starting', 1)
867 set nocp
868 au OptionSet * :call s:AutoCommandOptionSet(expand("<amatch>"))
869
870 " 1: Setting number option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100871 let g:options = [['number', 0, 0, 0, 1, 'global', 'set']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200872 set nu
873 call assert_equal([], g:options)
874 call assert_equal(g:opt[0], g:opt[1])
875
876 " 2: Setting local number option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100877 let g:options = [['number', 1, 1, '', 0, 'local', 'setlocal']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200878 setlocal nonu
879 call assert_equal([], g:options)
880 call assert_equal(g:opt[0], g:opt[1])
881
882 " 3: Setting global number option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100883 let g:options = [['number', 1, '', 1, 0, 'global', 'setglobal']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200884 setglobal nonu
885 call assert_equal([], g:options)
886 call assert_equal(g:opt[0], g:opt[1])
887
888 " 4: Setting local autoindent option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100889 let g:options = [['autoindent', 0, 0, '', 1, 'local', 'setlocal']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200890 setlocal ai
891 call assert_equal([], g:options)
892 call assert_equal(g:opt[0], g:opt[1])
893
894 " 5: Setting global autoindent option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100895 let g:options = [['autoindent', 0, '', 0, 1, 'global', 'setglobal']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200896 setglobal ai
897 call assert_equal([], g:options)
898 call assert_equal(g:opt[0], g:opt[1])
899
900 " 6: Setting global autoindent option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100901 let g:options = [['autoindent', 1, 1, 1, 0, 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +0200902 set ai!
903 call assert_equal([], g:options)
904 call assert_equal(g:opt[0], g:opt[1])
905
906 " 6a: Setting global autoindent option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100907 let g:options = [['autoindent', 1, 1, 0, 0, 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +0200908 noa setlocal ai
909 noa setglobal noai
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200910 set ai!
911 call assert_equal([], g:options)
912 call assert_equal(g:opt[0], g:opt[1])
913
914 " Should not print anything, use :noa
915 " 7: don't trigger OptionSet"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100916 let g:options = [['invalid', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200917 noa set nonu
Bram Moolenaard7c96872019-06-15 17:12:48 +0200918 call assert_equal([['invalid', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid']], g:options)
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200919 call assert_equal(g:opt[0], g:opt[1])
920
921 " 8: Setting several global list and number option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100922 let g:options = [['list', 0, 0, 0, 1, 'global', 'set'], ['number', 0, 0, 0, 1, 'global', 'set']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200923 set list nu
924 call assert_equal([], g:options)
925 call assert_equal(g:opt[0], g:opt[1])
926
927 " 9: don't trigger OptionSet"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100928 let g:options = [['invalid', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid'], ['invalid', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200929 noa set nolist nonu
Bram Moolenaard7c96872019-06-15 17:12:48 +0200930 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 +0200931 call assert_equal(g:opt[0], g:opt[1])
932
933 " 10: Setting global acd"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100934 let g:options = [['autochdir', 0, 0, '', 1, 'local', 'setlocal']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200935 setlocal acd
936 call assert_equal([], g:options)
937 call assert_equal(g:opt[0], g:opt[1])
938
939 " 11: Setting global autoread (also sets local value)"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100940 let g:options = [['autoread', 0, 0, 0, 1, 'global', 'set']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200941 set ar
942 call assert_equal([], g:options)
943 call assert_equal(g:opt[0], g:opt[1])
944
945 " 12: Setting local autoread"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100946 let g:options = [['autoread', 1, 1, '', 1, 'local', 'setlocal']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200947 setlocal ar
948 call assert_equal([], g:options)
949 call assert_equal(g:opt[0], g:opt[1])
950
951 " 13: Setting global autoread"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100952 let g:options = [['autoread', 1, '', 1, 0, 'global', 'setglobal']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200953 setglobal invar
954 call assert_equal([], g:options)
955 call assert_equal(g:opt[0], g:opt[1])
956
957 " 14: Setting option backspace through :let"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100958 let g:options = [['backspace', '', '', '', 'eol,indent,start', 'global', 'set']]
959 let &bs = "eol,indent,start"
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200960 call assert_equal([], g:options)
961 call assert_equal(g:opt[0], g:opt[1])
962
963 " 15: Setting option backspace through setbufvar()"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100964 let g:options = [['backup', 0, 0, '', 1, 'local', 'setlocal']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200965 " try twice, first time, shouldn't trigger because option name is invalid,
966 " second time, it should trigger
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +0200967 let bnum = bufnr('%')
Bram Moolenaare2e40752020-09-04 21:18:46 +0200968 call assert_fails("call setbufvar(bnum, '&l:bk', 1)", 'E355:')
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200969 " should trigger, use correct option name
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +0200970 call setbufvar(bnum, '&backup', 1)
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200971 call assert_equal([], g:options)
972 call assert_equal(g:opt[0], g:opt[1])
973
974 " 16: Setting number option using setwinvar"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100975 let g:options = [['number', 0, 0, '', 1, 'local', 'setlocal']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200976 call setwinvar(0, '&number', 1)
977 call assert_equal([], g:options)
978 call assert_equal(g:opt[0], g:opt[1])
979
980 " 17: Setting key option, shouldn't trigger"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100981 let g:options = [['key', 'invalid', 'invalid1', 'invalid2', 'invalid3', 'invalid4', 'invalid5']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200982 setlocal key=blah
983 setlocal key=
Bram Moolenaard7c96872019-06-15 17:12:48 +0200984 call assert_equal([['key', 'invalid', 'invalid1', 'invalid2', 'invalid3', 'invalid4', 'invalid5']], g:options)
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200985 call assert_equal(g:opt[0], g:opt[1])
986
Bram Moolenaard7c96872019-06-15 17:12:48 +0200987
988 " 18a: Setting string global option"
989 let oldval = &backupext
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100990 let g:options = [['backupext', oldval, oldval, oldval, 'foo', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +0200991 set backupext=foo
992 call assert_equal([], g:options)
993 call assert_equal(g:opt[0], g:opt[1])
994
995 " 18b: Resetting string global option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100996 let g:options = [['backupext', 'foo', 'foo', 'foo', oldval, 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +0200997 set backupext&
998 call assert_equal([], g:options)
999 call assert_equal(g:opt[0], g:opt[1])
1000
1001 " 18c: Setting global string global option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001002 let g:options = [['backupext', oldval, '', oldval, 'bar', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001003 setglobal backupext=bar
1004 call assert_equal([], g:options)
1005 call assert_equal(g:opt[0], g:opt[1])
1006
1007 " 18d: Setting local string global option"
1008 " As this is a global option this sets the global value even though
1009 " :setlocal is used!
1010 noa set backupext& " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001011 let g:options = [['backupext', oldval, oldval, '', 'baz', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001012 setlocal backupext=baz
1013 call assert_equal([], g:options)
1014 call assert_equal(g:opt[0], g:opt[1])
1015
1016 " 18e: Setting again string global option"
1017 noa setglobal backupext=ext_global " Reset global and local value (without triggering autocmd)
1018 noa setlocal backupext=ext_local " Sets the global(!) value!
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001019 let g:options = [['backupext', 'ext_local', 'ext_local', 'ext_local', 'fuu', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001020 set backupext=fuu
1021 call assert_equal([], g:options)
1022 call assert_equal(g:opt[0], g:opt[1])
1023
1024
zeertzjqb811de52021-10-21 10:50:44 +01001025 " 19a: Setting string global-local (to buffer) option"
Bram Moolenaar8efa0262017-08-20 15:47:20 +02001026 let oldval = &tags
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001027 let g:options = [['tags', oldval, oldval, oldval, 'tagpath', 'global', 'set']]
Bram Moolenaar8efa0262017-08-20 15:47:20 +02001028 set tags=tagpath
1029 call assert_equal([], g:options)
1030 call assert_equal(g:opt[0], g:opt[1])
1031
zeertzjqb811de52021-10-21 10:50:44 +01001032 " 19b: Resetting string global-local (to buffer) option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001033 let g:options = [['tags', 'tagpath', 'tagpath', 'tagpath', oldval, 'global', 'set']]
Bram Moolenaar8efa0262017-08-20 15:47:20 +02001034 set tags&
1035 call assert_equal([], g:options)
1036 call assert_equal(g:opt[0], g:opt[1])
1037
zeertzjqb811de52021-10-21 10:50:44 +01001038 " 19c: Setting global string global-local (to buffer) option "
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001039 let g:options = [['tags', oldval, '', oldval, 'tagpath1', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001040 setglobal tags=tagpath1
1041 call assert_equal([], g:options)
1042 call assert_equal(g:opt[0], g:opt[1])
1043
zeertzjqb811de52021-10-21 10:50:44 +01001044 " 19d: Setting local string global-local (to buffer) option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001045 let g:options = [['tags', 'tagpath1', 'tagpath1', '', 'tagpath2', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001046 setlocal tags=tagpath2
1047 call assert_equal([], g:options)
1048 call assert_equal(g:opt[0], g:opt[1])
1049
zeertzjqb811de52021-10-21 10:50:44 +01001050 " 19e: Setting again string global-local (to buffer) option"
1051 " Note: v:option_old is the old global value for global-local string options
Bram Moolenaard7c96872019-06-15 17:12:48 +02001052 " but the old local value for all other kinds of options.
1053 noa setglobal tags=tag_global " Reset global and local value (without triggering autocmd)
1054 noa setlocal tags=tag_local
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001055 let g:options = [['tags', 'tag_global', 'tag_local', 'tag_global', 'tagpath', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001056 set tags=tagpath
1057 call assert_equal([], g:options)
1058 call assert_equal(g:opt[0], g:opt[1])
1059
zeertzjqb811de52021-10-21 10:50:44 +01001060 " 19f: Setting string global-local (to buffer) option to an empty string"
1061 " Note: v:option_old is the old global value for global-local string options
Bram Moolenaard7c96872019-06-15 17:12:48 +02001062 " but the old local value for all other kinds of options.
1063 noa set tags=tag_global " Reset global and local value (without triggering autocmd)
1064 noa setlocal tags= " empty string
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001065 let g:options = [['tags', 'tag_global', '', 'tag_global', 'tagpath', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001066 set tags=tagpath
1067 call assert_equal([], g:options)
1068 call assert_equal(g:opt[0], g:opt[1])
1069
1070
1071 " 20a: Setting string local (to buffer) option"
1072 let oldval = &spelllang
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001073 let g:options = [['spelllang', oldval, oldval, oldval, 'elvish,klingon', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001074 set spelllang=elvish,klingon
1075 call assert_equal([], g:options)
1076 call assert_equal(g:opt[0], g:opt[1])
1077
1078 " 20b: Resetting string local (to buffer) option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001079 let g:options = [['spelllang', 'elvish,klingon', 'elvish,klingon', 'elvish,klingon', oldval, 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001080 set spelllang&
1081 call assert_equal([], g:options)
1082 call assert_equal(g:opt[0], g:opt[1])
1083
1084 " 20c: Setting global string local (to buffer) option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001085 let g:options = [['spelllang', oldval, '', oldval, 'elvish', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001086 setglobal spelllang=elvish
1087 call assert_equal([], g:options)
1088 call assert_equal(g:opt[0], g:opt[1])
1089
1090 " 20d: Setting local string local (to buffer) option"
1091 noa set spelllang& " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001092 let g:options = [['spelllang', oldval, oldval, '', 'klingon', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001093 setlocal spelllang=klingon
1094 call assert_equal([], g:options)
1095 call assert_equal(g:opt[0], g:opt[1])
1096
1097 " 20e: Setting again string local (to buffer) option"
zeertzjqb811de52021-10-21 10:50:44 +01001098 " Note: v:option_old is the old global value for global-local string options
Bram Moolenaard7c96872019-06-15 17:12:48 +02001099 " but the old local value for all other kinds of options.
1100 noa setglobal spelllang=spellglobal " Reset global and local value (without triggering autocmd)
1101 noa setlocal spelllang=spelllocal
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001102 let g:options = [['spelllang', 'spelllocal', 'spelllocal', 'spellglobal', 'foo', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001103 set spelllang=foo
1104 call assert_equal([], g:options)
1105 call assert_equal(g:opt[0], g:opt[1])
1106
1107
zeertzjqb811de52021-10-21 10:50:44 +01001108 " 21a: Setting string global-local (to window) option"
Bram Moolenaard7c96872019-06-15 17:12:48 +02001109 let oldval = &statusline
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001110 let g:options = [['statusline', oldval, oldval, oldval, 'foo', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001111 set statusline=foo
1112 call assert_equal([], g:options)
1113 call assert_equal(g:opt[0], g:opt[1])
1114
zeertzjqb811de52021-10-21 10:50:44 +01001115 " 21b: Resetting string global-local (to window) option"
1116 " Note: v:option_old is the old global value for global-local string options
Bram Moolenaard7c96872019-06-15 17:12:48 +02001117 " but the old local value for all other kinds of options.
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001118 let g:options = [['statusline', 'foo', 'foo', 'foo', oldval, 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001119 set statusline&
1120 call assert_equal([], g:options)
1121 call assert_equal(g:opt[0], g:opt[1])
1122
zeertzjqb811de52021-10-21 10:50:44 +01001123 " 21c: Setting global string global-local (to window) option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001124 let g:options = [['statusline', oldval, '', oldval, 'bar', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001125 setglobal statusline=bar
1126 call assert_equal([], g:options)
1127 call assert_equal(g:opt[0], g:opt[1])
1128
zeertzjqb811de52021-10-21 10:50:44 +01001129 " 21d: Setting local string global-local (to window) option"
Bram Moolenaard7c96872019-06-15 17:12:48 +02001130 noa set statusline& " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001131 let g:options = [['statusline', oldval, oldval, '', 'baz', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001132 setlocal statusline=baz
1133 call assert_equal([], g:options)
1134 call assert_equal(g:opt[0], g:opt[1])
1135
zeertzjqb811de52021-10-21 10:50:44 +01001136 " 21e: Setting again string global-local (to window) option"
1137 " Note: v:option_old is the old global value for global-local string options
Bram Moolenaard7c96872019-06-15 17:12:48 +02001138 " but the old local value for all other kinds of options.
1139 noa setglobal statusline=bar " Reset global and local value (without triggering autocmd)
1140 noa setlocal statusline=baz
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001141 let g:options = [['statusline', 'bar', 'baz', 'bar', 'foo', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001142 set statusline=foo
1143 call assert_equal([], g:options)
1144 call assert_equal(g:opt[0], g:opt[1])
1145
1146
1147 " 22a: Setting string local (to window) option"
1148 let oldval = &foldignore
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001149 let g:options = [['foldignore', oldval, oldval, oldval, 'fo', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001150 set foldignore=fo
1151 call assert_equal([], g:options)
1152 call assert_equal(g:opt[0], g:opt[1])
1153
1154 " 22b: Resetting string local (to window) option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001155 let g:options = [['foldignore', 'fo', 'fo', 'fo', oldval, 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001156 set foldignore&
1157 call assert_equal([], g:options)
1158 call assert_equal(g:opt[0], g:opt[1])
1159
1160 " 22c: Setting global string local (to window) option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001161 let g:options = [['foldignore', oldval, '', oldval, 'bar', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001162 setglobal foldignore=bar
1163 call assert_equal([], g:options)
1164 call assert_equal(g:opt[0], g:opt[1])
1165
1166 " 22d: Setting local string local (to window) option"
1167 noa set foldignore& " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001168 let g:options = [['foldignore', oldval, oldval, '', 'baz', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001169 setlocal foldignore=baz
1170 call assert_equal([], g:options)
1171 call assert_equal(g:opt[0], g:opt[1])
1172
1173 " 22e: Setting again string local (to window) option"
1174 noa setglobal foldignore=glob " Reset global and local value (without triggering autocmd)
1175 noa setlocal foldignore=loc
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001176 let g:options = [['foldignore', 'loc', 'loc', 'glob', 'fo', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001177 set foldignore=fo
1178 call assert_equal([], g:options)
1179 call assert_equal(g:opt[0], g:opt[1])
1180
1181
zeertzjqb811de52021-10-21 10:50:44 +01001182 " 23a: Setting global number global option"
Bram Moolenaard7c96872019-06-15 17:12:48 +02001183 noa setglobal cmdheight=8 " Reset global and local value (without triggering autocmd)
1184 noa setlocal cmdheight=1 " Sets the global(!) value!
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001185 let g:options = [['cmdheight', '1', '', '1', '2', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001186 setglobal cmdheight=2
1187 call assert_equal([], g:options)
1188 call assert_equal(g:opt[0], g:opt[1])
1189
1190 " 23b: Setting local number global option"
1191 noa setglobal cmdheight=8 " Reset global and local value (without triggering autocmd)
1192 noa setlocal cmdheight=1 " Sets the global(!) value!
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001193 let g:options = [['cmdheight', '1', '1', '', '2', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001194 setlocal cmdheight=2
1195 call assert_equal([], g:options)
1196 call assert_equal(g:opt[0], g:opt[1])
1197
1198 " 23c: Setting again number global option"
1199 noa setglobal cmdheight=8 " Reset global and local value (without triggering autocmd)
1200 noa setlocal cmdheight=1 " Sets the global(!) value!
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001201 let g:options = [['cmdheight', '1', '1', '1', '2', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001202 set cmdheight=2
1203 call assert_equal([], g:options)
1204 call assert_equal(g:opt[0], g:opt[1])
1205
1206 " 23d: Setting again number global option"
1207 noa set cmdheight=8 " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001208 let g:options = [['cmdheight', '8', '8', '8', '2', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001209 set cmdheight=2
1210 call assert_equal([], g:options)
1211 call assert_equal(g:opt[0], g:opt[1])
1212
1213
1214 " 24a: Setting global number global-local (to buffer) option"
1215 noa setglobal undolevels=8 " Reset global and local value (without triggering autocmd)
1216 noa setlocal undolevels=1
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001217 let g:options = [['undolevels', '8', '', '8', '2', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001218 setglobal undolevels=2
1219 call assert_equal([], g:options)
1220 call assert_equal(g:opt[0], g:opt[1])
1221
1222 " 24b: Setting local number global-local (to buffer) option"
1223 noa setglobal undolevels=8 " Reset global and local value (without triggering autocmd)
1224 noa setlocal undolevels=1
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001225 let g:options = [['undolevels', '1', '1', '', '2', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001226 setlocal undolevels=2
1227 call assert_equal([], g:options)
1228 call assert_equal(g:opt[0], g:opt[1])
1229
1230 " 24c: Setting again number global-local (to buffer) option"
1231 noa setglobal undolevels=8 " Reset global and local value (without triggering autocmd)
1232 noa setlocal undolevels=1
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001233 let g:options = [['undolevels', '1', '1', '8', '2', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001234 set undolevels=2
1235 call assert_equal([], g:options)
1236 call assert_equal(g:opt[0], g:opt[1])
1237
1238 " 24d: Setting again global number global-local (to buffer) option"
1239 noa set undolevels=8 " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001240 let g:options = [['undolevels', '8', '8', '8', '2', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001241 set undolevels=2
1242 call assert_equal([], g:options)
1243 call assert_equal(g:opt[0], g:opt[1])
1244
1245
1246 " 25a: Setting global number local (to buffer) option"
1247 noa setglobal wrapmargin=8 " Reset global and local value (without triggering autocmd)
1248 noa setlocal wrapmargin=1
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001249 let g:options = [['wrapmargin', '8', '', '8', '2', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001250 setglobal wrapmargin=2
1251 call assert_equal([], g:options)
1252 call assert_equal(g:opt[0], g:opt[1])
1253
1254 " 25b: Setting local number local (to buffer) option"
1255 noa setglobal wrapmargin=8 " Reset global and local value (without triggering autocmd)
1256 noa setlocal wrapmargin=1
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001257 let g:options = [['wrapmargin', '1', '1', '', '2', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001258 setlocal wrapmargin=2
1259 call assert_equal([], g:options)
1260 call assert_equal(g:opt[0], g:opt[1])
1261
1262 " 25c: Setting again number local (to buffer) option"
1263 noa setglobal wrapmargin=8 " Reset global and local value (without triggering autocmd)
1264 noa setlocal wrapmargin=1
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001265 let g:options = [['wrapmargin', '1', '1', '8', '2', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001266 set wrapmargin=2
1267 call assert_equal([], g:options)
1268 call assert_equal(g:opt[0], g:opt[1])
1269
1270 " 25d: Setting again global number local (to buffer) option"
1271 noa set wrapmargin=8 " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001272 let g:options = [['wrapmargin', '8', '8', '8', '2', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001273 set wrapmargin=2
1274 call assert_equal([], g:options)
1275 call assert_equal(g:opt[0], g:opt[1])
1276
1277
1278 " 26: Setting number global-local (to window) option.
1279 " Such option does currently not exist.
1280
1281
1282 " 27a: Setting global number local (to window) option"
1283 noa setglobal foldcolumn=8 " Reset global and local value (without triggering autocmd)
1284 noa setlocal foldcolumn=1
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001285 let g:options = [['foldcolumn', '8', '', '8', '2', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001286 setglobal foldcolumn=2
1287 call assert_equal([], g:options)
1288 call assert_equal(g:opt[0], g:opt[1])
1289
1290 " 27b: Setting local number local (to window) option"
1291 noa setglobal foldcolumn=8 " Reset global and local value (without triggering autocmd)
1292 noa setlocal foldcolumn=1
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001293 let g:options = [['foldcolumn', '1', '1', '', '2', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001294 setlocal foldcolumn=2
1295 call assert_equal([], g:options)
1296 call assert_equal(g:opt[0], g:opt[1])
1297
1298 " 27c: Setting again number local (to window) option"
1299 noa setglobal foldcolumn=8 " Reset global and local value (without triggering autocmd)
1300 noa setlocal foldcolumn=1
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001301 let g:options = [['foldcolumn', '1', '1', '8', '2', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001302 set foldcolumn=2
1303 call assert_equal([], g:options)
1304 call assert_equal(g:opt[0], g:opt[1])
1305
zeertzjqb811de52021-10-21 10:50:44 +01001306 " 27d: Setting again global number local (to window) option"
Bram Moolenaard7c96872019-06-15 17:12:48 +02001307 noa set foldcolumn=8 " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001308 let g:options = [['foldcolumn', '8', '8', '8', '2', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001309 set foldcolumn=2
1310 call assert_equal([], g:options)
1311 call assert_equal(g:opt[0], g:opt[1])
1312
1313
1314 " 28a: Setting global boolean global option"
1315 noa setglobal nowrapscan " Reset global and local value (without triggering autocmd)
1316 noa setlocal wrapscan " Sets the global(!) value!
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001317 let g:options = [['wrapscan', '1', '', '1', '0', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001318 setglobal nowrapscan
1319 call assert_equal([], g:options)
1320 call assert_equal(g:opt[0], g:opt[1])
1321
1322 " 28b: Setting local boolean global option"
1323 noa setglobal nowrapscan " Reset global and local value (without triggering autocmd)
1324 noa setlocal wrapscan " Sets the global(!) value!
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001325 let g:options = [['wrapscan', '1', '1', '', '0', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001326 setlocal nowrapscan
1327 call assert_equal([], g:options)
1328 call assert_equal(g:opt[0], g:opt[1])
1329
1330 " 28c: Setting again boolean global option"
1331 noa setglobal nowrapscan " Reset global and local value (without triggering autocmd)
1332 noa setlocal wrapscan " Sets the global(!) value!
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001333 let g:options = [['wrapscan', '1', '1', '1', '0', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001334 set nowrapscan
1335 call assert_equal([], g:options)
1336 call assert_equal(g:opt[0], g:opt[1])
1337
1338 " 28d: Setting again global boolean global option"
1339 noa set nowrapscan " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001340 let g:options = [['wrapscan', '0', '0', '0', '1', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001341 set wrapscan
1342 call assert_equal([], g:options)
1343 call assert_equal(g:opt[0], g:opt[1])
1344
1345
1346 " 29a: Setting global boolean global-local (to buffer) option"
1347 noa setglobal noautoread " Reset global and local value (without triggering autocmd)
1348 noa setlocal autoread
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001349 let g:options = [['autoread', '0', '', '0', '1', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001350 setglobal autoread
1351 call assert_equal([], g:options)
1352 call assert_equal(g:opt[0], g:opt[1])
1353
1354 " 29b: Setting local boolean global-local (to buffer) option"
1355 noa setglobal noautoread " Reset global and local value (without triggering autocmd)
1356 noa setlocal autoread
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001357 let g:options = [['autoread', '1', '1', '', '0', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001358 setlocal noautoread
1359 call assert_equal([], g:options)
1360 call assert_equal(g:opt[0], g:opt[1])
1361
1362 " 29c: Setting again boolean global-local (to buffer) option"
1363 noa setglobal noautoread " Reset global and local value (without triggering autocmd)
1364 noa setlocal autoread
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001365 let g:options = [['autoread', '1', '1', '0', '1', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001366 set autoread
1367 call assert_equal([], g:options)
1368 call assert_equal(g:opt[0], g:opt[1])
1369
1370 " 29d: Setting again global boolean global-local (to buffer) option"
1371 noa set noautoread " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001372 let g:options = [['autoread', '0', '0', '0', '1', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001373 set autoread
1374 call assert_equal([], g:options)
1375 call assert_equal(g:opt[0], g:opt[1])
1376
1377
1378 " 30a: Setting global boolean local (to buffer) option"
1379 noa setglobal nocindent " Reset global and local value (without triggering autocmd)
1380 noa setlocal cindent
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001381 let g:options = [['cindent', '0', '', '0', '1', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001382 setglobal cindent
1383 call assert_equal([], g:options)
1384 call assert_equal(g:opt[0], g:opt[1])
1385
1386 " 30b: Setting local boolean local (to buffer) option"
1387 noa setglobal nocindent " Reset global and local value (without triggering autocmd)
1388 noa setlocal cindent
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001389 let g:options = [['cindent', '1', '1', '', '0', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001390 setlocal nocindent
1391 call assert_equal([], g:options)
1392 call assert_equal(g:opt[0], g:opt[1])
1393
1394 " 30c: Setting again boolean local (to buffer) option"
1395 noa setglobal nocindent " Reset global and local value (without triggering autocmd)
1396 noa setlocal cindent
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001397 let g:options = [['cindent', '1', '1', '0', '1', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001398 set cindent
1399 call assert_equal([], g:options)
1400 call assert_equal(g:opt[0], g:opt[1])
1401
1402 " 30d: Setting again global boolean local (to buffer) option"
1403 noa set nocindent " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001404 let g:options = [['cindent', '0', '0', '0', '1', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001405 set cindent
1406 call assert_equal([], g:options)
1407 call assert_equal(g:opt[0], g:opt[1])
1408
1409
1410 " 31: Setting boolean global-local (to window) option
1411 " Currently no such option exists.
1412
1413
1414 " 32a: Setting global boolean local (to window) option"
1415 noa setglobal nocursorcolumn " Reset global and local value (without triggering autocmd)
1416 noa setlocal cursorcolumn
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001417 let g:options = [['cursorcolumn', '0', '', '0', '1', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001418 setglobal cursorcolumn
1419 call assert_equal([], g:options)
1420 call assert_equal(g:opt[0], g:opt[1])
1421
1422 " 32b: Setting local boolean local (to window) option"
1423 noa setglobal nocursorcolumn " Reset global and local value (without triggering autocmd)
1424 noa setlocal cursorcolumn
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001425 let g:options = [['cursorcolumn', '1', '1', '', '0', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001426 setlocal nocursorcolumn
1427 call assert_equal([], g:options)
1428 call assert_equal(g:opt[0], g:opt[1])
1429
1430 " 32c: Setting again boolean local (to window) option"
1431 noa setglobal nocursorcolumn " Reset global and local value (without triggering autocmd)
1432 noa setlocal cursorcolumn
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001433 let g:options = [['cursorcolumn', '1', '1', '0', '1', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001434 set cursorcolumn
1435 call assert_equal([], g:options)
1436 call assert_equal(g:opt[0], g:opt[1])
1437
1438 " 32d: Setting again global boolean local (to window) option"
1439 noa set nocursorcolumn " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001440 let g:options = [['cursorcolumn', '0', '0', '0', '1', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001441 set cursorcolumn
1442 call assert_equal([], g:options)
1443 call assert_equal(g:opt[0], g:opt[1])
1444
1445
Bram Moolenaar1bc353b2019-09-01 14:45:28 +02001446 " 33: Test autocommands when an option value is converted internally.
Bram Moolenaard7c96872019-06-15 17:12:48 +02001447 noa set backspace=1 " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001448 let g:options = [['backspace', 'indent,eol', 'indent,eol', 'indent,eol', '2', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001449 set backspace=2
1450 call assert_equal([], g:options)
1451 call assert_equal(g:opt[0], g:opt[1])
1452
1453
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001454 " Cleanup
1455 au! OptionSet
Bram Moolenaar0331faf2019-06-15 18:40:37 +02001456 " set tags&
Bram Moolenaard7c96872019-06-15 17:12:48 +02001457 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 +02001458 exe printf(":set %s&vim", opt)
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001459 endfor
1460 call test_override('starting', 0)
1461 delfunc! AutoCommandOptionSet
1462endfunc
1463
1464func Test_OptionSet_diffmode()
1465 call test_override('starting', 1)
Bram Moolenaar26d98212019-01-27 22:32:55 +01001466 " 18: Changing an option when entering diff mode
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001467 new
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001468 au OptionSet diff :let &l:cul = v:option_new
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001469
1470 call setline(1, ['buffer 1', 'line2', 'line3', 'line4'])
1471 call assert_equal(0, &l:cul)
1472 diffthis
1473 call assert_equal(1, &l:cul)
1474
1475 vnew
1476 call setline(1, ['buffer 2', 'line 2', 'line 3', 'line4'])
1477 call assert_equal(0, &l:cul)
1478 diffthis
1479 call assert_equal(1, &l:cul)
1480
1481 diffoff
1482 call assert_equal(0, &l:cul)
1483 call assert_equal(1, getwinvar(2, '&l:cul'))
1484 bw!
1485
1486 call assert_equal(1, &l:cul)
1487 diffoff!
1488 call assert_equal(0, &l:cul)
1489 call assert_equal(0, getwinvar(1, '&l:cul'))
1490 bw!
1491
1492 " Cleanup
1493 au! OptionSet
1494 call test_override('starting', 0)
1495endfunc
1496
1497func Test_OptionSet_diffmode_close()
1498 call test_override('starting', 1)
1499 " 19: Try to close the current window when entering diff mode
1500 " should not segfault
1501 new
1502 au OptionSet diff close
1503
1504 call setline(1, ['buffer 1', 'line2', 'line3', 'line4'])
Bram Moolenaare2e40752020-09-04 21:18:46 +02001505 call assert_fails(':diffthis', 'E788:')
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001506 call assert_equal(1, &diff)
1507 vnew
1508 call setline(1, ['buffer 2', 'line 2', 'line 3', 'line4'])
Bram Moolenaare2e40752020-09-04 21:18:46 +02001509 call assert_fails(':diffthis', 'E788:')
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001510 call assert_equal(1, &diff)
Bram Moolenaara9aa86f2019-11-10 21:25:45 +01001511 set diffopt-=closeoff
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001512 bw!
Bram Moolenaare2e40752020-09-04 21:18:46 +02001513 call assert_fails(':diffoff!', 'E788:')
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001514 bw!
1515
1516 " Cleanup
1517 au! OptionSet
1518 call test_override('starting', 0)
1519 "delfunc! AutoCommandOptionSet
1520endfunc
Bram Moolenaar4a137b42017-08-04 22:37:11 +02001521
1522" Test for Bufleave autocommand that deletes the buffer we are about to edit.
1523func Test_BufleaveWithDelete()
Bram Moolenaare7cda972022-08-29 11:02:59 +01001524 new | edit XbufLeave1
Bram Moolenaar4a137b42017-08-04 22:37:11 +02001525
1526 augroup test_bufleavewithdelete
1527 autocmd!
Bram Moolenaare7cda972022-08-29 11:02:59 +01001528 autocmd BufLeave XbufLeave1 bwipe XbufLeave2
Bram Moolenaar4a137b42017-08-04 22:37:11 +02001529 augroup END
1530
Bram Moolenaare7cda972022-08-29 11:02:59 +01001531 call assert_fails('edit XbufLeave2', 'E143:')
1532 call assert_equal('XbufLeave1', bufname('%'))
Bram Moolenaar4a137b42017-08-04 22:37:11 +02001533
Bram Moolenaare7cda972022-08-29 11:02:59 +01001534 autocmd! test_bufleavewithdelete BufLeave XbufLeave1
Bram Moolenaar4a137b42017-08-04 22:37:11 +02001535 augroup! test_bufleavewithdelete
1536
1537 new
Bram Moolenaare7cda972022-08-29 11:02:59 +01001538 bwipe! XbufLeave1
Bram Moolenaar4a137b42017-08-04 22:37:11 +02001539endfunc
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +02001540
1541" Test for autocommand that changes the buffer list, when doing ":ball".
1542func Test_Acmd_BufAll()
1543 enew!
1544 %bwipe!
1545 call writefile(['Test file Xxx1'], 'Xxx1')
1546 call writefile(['Test file Xxx2'], 'Xxx2')
1547 call writefile(['Test file Xxx3'], 'Xxx3')
1548
1549 " Add three files to the buffer list
1550 split Xxx1
1551 close
1552 split Xxx2
1553 close
1554 split Xxx3
1555 close
1556
1557 " Wipe the buffer when the buffer is opened
1558 au BufReadPost Xxx2 bwipe
1559
1560 call append(0, 'Test file Xxx4')
1561 ball
1562
1563 call assert_equal(2, winnr('$'))
1564 call assert_equal('Xxx1', bufname(winbufnr(winnr('$'))))
1565 wincmd t
1566
1567 au! BufReadPost
1568 %bwipe!
1569 call delete('Xxx1')
1570 call delete('Xxx2')
1571 call delete('Xxx3')
1572 enew! | only
1573endfunc
1574
1575" Test for autocommand that changes current buffer on BufEnter event.
1576" Check if modelines are interpreted for the correct buffer.
1577func Test_Acmd_BufEnter()
1578 %bwipe!
1579 call writefile(['start of test file Xxx1',
1580 \ "\<Tab>this is a test",
1581 \ 'end of test file Xxx1'], 'Xxx1')
1582 call writefile(['start of test file Xxx2',
1583 \ 'vim: set noai :',
1584 \ "\<Tab>this is a test",
1585 \ 'end of test file Xxx2'], 'Xxx2')
1586
1587 au BufEnter Xxx2 brew
1588 set ai modeline modelines=3
1589 edit Xxx1
1590 " edit Xxx2, autocmd will do :brew
1591 edit Xxx2
1592 exe "normal G?this is a\<CR>"
1593 " Append text with autoindent to this file
1594 normal othis should be auto-indented
1595 call assert_equal("\<Tab>this should be auto-indented", getline('.'))
1596 call assert_equal(3, line('.'))
1597 " Remove autocmd and edit Xxx2 again
1598 au! BufEnter Xxx2
1599 buf! Xxx2
1600 exe "normal G?this is a\<CR>"
1601 " append text without autoindent to Xxx
1602 normal othis should be in column 1
1603 call assert_equal("this should be in column 1", getline('.'))
1604 call assert_equal(4, line('.'))
1605
1606 %bwipe!
1607 call delete('Xxx1')
1608 call delete('Xxx2')
1609 set ai&vim modeline&vim modelines&vim
1610endfunc
1611
1612" Test for issue #57
1613" do not move cursor on <c-o> when autoindent is set
1614func Test_ai_CTRL_O()
1615 enew!
1616 set ai
1617 let save_fo = &fo
1618 set fo+=r
1619 exe "normal o# abcdef\<Esc>2hi\<CR>\<C-O>d0\<Esc>"
1620 exe "normal o# abcdef\<Esc>2hi\<C-O>d0\<Esc>"
1621 call assert_equal(['# abc', 'def', 'def'], getline(2, 4))
1622
1623 set ai&vim
1624 let &fo = save_fo
1625 enew!
1626endfunc
1627
1628" Test for autocommand that deletes the current buffer on BufLeave event.
1629" Also test deleting the last buffer, should give a new, empty buffer.
1630func Test_BufLeave_Wipe()
1631 %bwipe!
1632 let content = ['start of test file Xxx',
1633 \ 'this is a test',
1634 \ 'end of test file Xxx']
1635 call writefile(content, 'Xxx1')
1636 call writefile(content, 'Xxx2')
1637
1638 au BufLeave Xxx2 bwipe
1639 edit Xxx1
1640 split Xxx2
1641 " delete buffer Xxx2, we should be back to Xxx1
1642 bwipe
1643 call assert_equal('Xxx1', bufname('%'))
1644 call assert_equal(1, winnr('$'))
1645
1646 " Create an alternate buffer
1647 %write! test.out
1648 call assert_equal('test.out', bufname('#'))
1649 " delete alternate buffer
1650 bwipe test.out
1651 call assert_equal('Xxx1', bufname('%'))
1652 call assert_equal('', bufname('#'))
1653
1654 au BufLeave Xxx1 bwipe
1655 " delete current buffer, get an empty one
1656 bwipe!
1657 call assert_equal(1, line('$'))
1658 call assert_equal('', bufname('%'))
Bram Moolenaarb2c87502017-10-14 21:15:58 +02001659 let g:bufinfo = getbufinfo()
1660 call assert_equal(1, len(g:bufinfo))
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +02001661
1662 call delete('Xxx1')
1663 call delete('Xxx2')
Bram Moolenaar53f0c962017-10-22 14:23:59 +02001664 call delete('test.out')
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +02001665 %bwipe
1666 au! BufLeave
Bram Moolenaarb2c87502017-10-14 21:15:58 +02001667
1668 " check that bufinfo doesn't contain a pointer to freed memory
1669 call test_garbagecollect_now()
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +02001670endfunc
Bram Moolenaar87ffb5c2017-10-19 12:37:42 +02001671
1672func Test_QuitPre()
1673 edit Xfoo
1674 let winid = win_getid(winnr())
1675 split Xbar
1676 au! QuitPre * let g:afile = expand('<afile>')
1677 " Close the other window, <afile> should be correct.
1678 exe win_id2win(winid) . 'q'
1679 call assert_equal('Xfoo', g:afile)
LemonBoy66e13ae2022-04-21 22:52:11 +01001680
Bram Moolenaar87ffb5c2017-10-19 12:37:42 +02001681 unlet g:afile
1682 bwipe Xfoo
1683 bwipe Xbar
1684endfunc
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02001685
1686func Test_Cmdline()
Bram Moolenaar153b7042018-01-31 15:48:32 +01001687 au! CmdlineChanged : let g:text = getcmdline()
1688 let g:text = 0
1689 call feedkeys(":echom 'hello'\<CR>", 'xt')
1690 call assert_equal("echom 'hello'", g:text)
1691 au! CmdlineChanged
1692
1693 au! CmdlineChanged : let g:entered = expand('<afile>')
1694 let g:entered = 0
1695 call feedkeys(":echom 'hello'\<CR>", 'xt')
1696 call assert_equal(':', g:entered)
1697 au! CmdlineChanged
1698
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02001699 au! CmdlineEnter : let g:entered = expand('<afile>')
1700 au! CmdlineLeave : let g:left = expand('<afile>')
1701 let g:entered = 0
1702 let g:left = 0
1703 call feedkeys(":echo 'hello'\<CR>", 'xt')
1704 call assert_equal(':', g:entered)
1705 call assert_equal(':', g:left)
1706 au! CmdlineEnter
1707 au! CmdlineLeave
1708
Bram Moolenaara4baf5b2018-04-22 13:27:44 +02001709 let save_shellslash = &shellslash
1710 set noshellslash
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02001711 au! CmdlineEnter / let g:entered = expand('<afile>')
1712 au! CmdlineLeave / let g:left = expand('<afile>')
1713 let g:entered = 0
1714 let g:left = 0
Bram Moolenaar53f0c962017-10-22 14:23:59 +02001715 new
1716 call setline(1, 'hello')
1717 call feedkeys("/hello\<CR>", 'xt')
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02001718 call assert_equal('/', g:entered)
1719 call assert_equal('/', g:left)
Bram Moolenaar53f0c962017-10-22 14:23:59 +02001720 bwipe!
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02001721 au! CmdlineEnter
1722 au! CmdlineLeave
Bram Moolenaara4baf5b2018-04-22 13:27:44 +02001723 let &shellslash = save_shellslash
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02001724endfunc
Bram Moolenaar53f0c962017-10-22 14:23:59 +02001725
1726" Test for BufWritePre autocommand that deletes or unloads the buffer.
1727func Test_BufWritePre()
1728 %bwipe
1729 au BufWritePre Xxx1 bunload
1730 au BufWritePre Xxx2 bwipe
1731
1732 call writefile(['start of Xxx1', 'test', 'end of Xxx1'], 'Xxx1')
1733 call writefile(['start of Xxx2', 'test', 'end of Xxx2'], 'Xxx2')
1734
1735 edit Xtest
1736 e! Xxx2
1737 bdel Xtest
1738 e Xxx1
1739 " write it, will unload it and give an error msg
Bram Moolenaare2e40752020-09-04 21:18:46 +02001740 call assert_fails('w', 'E203:')
Bram Moolenaar53f0c962017-10-22 14:23:59 +02001741 call assert_equal('Xxx2', bufname('%'))
1742 edit Xtest
1743 e! Xxx2
1744 bwipe Xtest
1745 " write it, will delete the buffer and give an error msg
Bram Moolenaare2e40752020-09-04 21:18:46 +02001746 call assert_fails('w', 'E203:')
Bram Moolenaar53f0c962017-10-22 14:23:59 +02001747 call assert_equal('Xxx1', bufname('%'))
1748 au! BufWritePre
1749 call delete('Xxx1')
1750 call delete('Xxx2')
1751endfunc
1752
1753" Test for BufUnload autocommand that unloads all the other buffers
1754func Test_bufunload_all()
Bram Moolenaarf08b0eb2021-10-16 13:00:14 +01001755 let g:test_is_flaky = 1
Bram Moolenaar53f0c962017-10-22 14:23:59 +02001756 call writefile(['Test file Xxx1'], 'Xxx1')"
1757 call writefile(['Test file Xxx2'], 'Xxx2')"
1758
Bram Moolenaarc79745a2019-05-20 22:12:34 +02001759 let content =<< trim [CODE]
1760 func UnloadAllBufs()
1761 let i = 1
1762 while i <= bufnr('$')
1763 if i != bufnr('%') && bufloaded(i)
1764 exe i . 'bunload'
1765 endif
1766 let i += 1
1767 endwhile
1768 endfunc
1769 au BufUnload * call UnloadAllBufs()
1770 au VimLeave * call writefile(['Test Finished'], 'Xout')
1771 edit Xxx1
1772 split Xxx2
1773 q
1774 [CODE]
1775
Bram Moolenaar53f0c962017-10-22 14:23:59 +02001776 call writefile(content, 'Xtest')
1777
1778 call delete('Xout')
Bram Moolenaar93344c22019-08-14 21:12:05 +02001779 call system(GetVimCommandClean() .. ' -N --not-a-term -S Xtest')
Bram Moolenaar53f0c962017-10-22 14:23:59 +02001780 call assert_true(filereadable('Xout'))
1781
1782 call delete('Xxx1')
1783 call delete('Xxx2')
1784 call delete('Xtest')
1785 call delete('Xout')
1786endfunc
1787
1788" Some tests for buffer-local autocommands
1789func Test_buflocal_autocmd()
1790 let g:bname = ''
1791 edit xx
1792 au BufLeave <buffer> let g:bname = expand("%")
1793 " here, autocommand for xx should trigger.
1794 " but autocommand shall not apply to buffer named <buffer>.
1795 edit somefile
1796 call assert_equal('xx', g:bname)
1797 let g:bname = ''
1798 " here, autocommand shall be auto-deleted
1799 bwipe xx
1800 " autocmd should not trigger
1801 edit xx
1802 call assert_equal('', g:bname)
1803 " autocmd should not trigger
1804 edit somefile
1805 call assert_equal('', g:bname)
1806 enew
1807 unlet g:bname
1808endfunc
Bram Moolenaar430dc5d2017-11-02 21:04:47 +01001809
1810" Test for "*Cmd" autocommands
1811func Test_Cmd_Autocmds()
1812 call writefile(['start of Xxx', "\tabc2", 'end of Xxx'], 'Xxx')
1813
1814 enew!
1815 au BufReadCmd XtestA 0r Xxx|$del
1816 edit XtestA " will read text of Xxd instead
1817 call assert_equal('start of Xxx', getline(1))
1818
1819 au BufWriteCmd XtestA call append(line("$"), "write")
1820 write " will append a line to the file
1821 call assert_equal('write', getline('$'))
Bram Moolenaare2e40752020-09-04 21:18:46 +02001822 call assert_fails('read XtestA', 'E484:') " should not read anything
Bram Moolenaar430dc5d2017-11-02 21:04:47 +01001823 call assert_equal('write', getline(4))
1824
1825 " now we have:
1826 " 1 start of Xxx
1827 " 2 abc2
1828 " 3 end of Xxx
1829 " 4 write
1830
1831 au FileReadCmd XtestB '[r Xxx
1832 2r XtestB " will read Xxx below line 2 instead
1833 call assert_equal('start of Xxx', getline(3))
1834
1835 " now we have:
1836 " 1 start of Xxx
1837 " 2 abc2
1838 " 3 start of Xxx
1839 " 4 abc2
1840 " 5 end of Xxx
1841 " 6 end of Xxx
1842 " 7 write
1843
1844 au FileWriteCmd XtestC '[,']copy $
1845 normal 4GA1
1846 4,5w XtestC " will copy lines 4 and 5 to the end
1847 call assert_equal("\tabc21", getline(8))
Bram Moolenaare2e40752020-09-04 21:18:46 +02001848 call assert_fails('r XtestC', 'E484:') " should not read anything
Bram Moolenaar430dc5d2017-11-02 21:04:47 +01001849 call assert_equal("end of Xxx", getline(9))
1850
1851 " now we have:
1852 " 1 start of Xxx
1853 " 2 abc2
1854 " 3 start of Xxx
1855 " 4 abc21
1856 " 5 end of Xxx
1857 " 6 end of Xxx
1858 " 7 write
1859 " 8 abc21
1860 " 9 end of Xxx
1861
1862 let g:lines = []
1863 au FileAppendCmd XtestD call extend(g:lines, getline(line("'["), line("']")))
1864 w >>XtestD " will add lines to 'lines'
1865 call assert_equal(9, len(g:lines))
Bram Moolenaare2e40752020-09-04 21:18:46 +02001866 call assert_fails('$r XtestD', 'E484:') " should not read anything
Bram Moolenaar430dc5d2017-11-02 21:04:47 +01001867 call assert_equal(9, line('$'))
1868 call assert_equal('end of Xxx', getline('$'))
1869
1870 au BufReadCmd XtestE 0r Xxx|$del
1871 sp XtestE " split window with test.out
1872 call assert_equal('end of Xxx', getline(3))
1873
1874 let g:lines = []
1875 exe "normal 2Goasdf\<Esc>\<C-W>\<C-W>"
1876 au BufWriteCmd XtestE call extend(g:lines, getline(0, '$'))
1877 wall " will write other window to 'lines'
1878 call assert_equal(4, len(g:lines), g:lines)
1879 call assert_equal('asdf', g:lines[2])
1880
1881 au! BufReadCmd
1882 au! BufWriteCmd
1883 au! FileReadCmd
1884 au! FileWriteCmd
1885 au! FileAppendCmd
1886 %bwipe!
1887 call delete('Xxx')
1888 enew!
1889endfunc
Bram Moolenaaraace2152017-11-05 16:23:10 +01001890
Bram Moolenaar0fff4412020-03-29 16:06:29 +02001891func s:ReadFile()
1892 setl noswapfile nomodified
1893 let filename = resolve(expand("<afile>:p"))
1894 execute 'read' fnameescape(filename)
1895 1d_
1896 exe 'file' fnameescape(filename)
1897 setl buftype=acwrite
1898endfunc
1899
1900func s:WriteFile()
1901 let filename = resolve(expand("<afile>:p"))
1902 setl buftype=
1903 noautocmd execute 'write' fnameescape(filename)
1904 setl buftype=acwrite
1905 setl nomodified
1906endfunc
1907
1908func Test_BufReadCmd()
1909 autocmd BufReadCmd *.test call s:ReadFile()
1910 autocmd BufWriteCmd *.test call s:WriteFile()
1911
1912 call writefile(['one', 'two', 'three'], 'Xcmd.test')
1913 edit Xcmd.test
1914 call assert_match('Xcmd.test" line 1 of 3', execute('file'))
1915 normal! Gofour
1916 write
1917 call assert_equal(['one', 'two', 'three', 'four'], readfile('Xcmd.test'))
1918
1919 bwipe!
1920 call delete('Xcmd.test')
1921 au! BufReadCmd
1922 au! BufWriteCmd
1923endfunc
1924
zeertzjq9c8f9462022-08-30 18:17:15 +01001925func Test_BufWriteCmd()
1926 autocmd BufWriteCmd Xbufwritecmd let g:written = 1
1927 new
1928 file Xbufwritecmd
1929 set buftype=acwrite
1930 call mkdir('Xbufwritecmd')
1931 write
1932 " BufWriteCmd should be triggered even if a directory has the same name
1933 call assert_equal(1, g:written)
1934 call delete('Xbufwritecmd', 'd')
1935 unlet g:written
1936 au! BufWriteCmd
1937 bwipe!
1938endfunc
1939
Bram Moolenaaraace2152017-11-05 16:23:10 +01001940func SetChangeMarks(start, end)
Bram Moolenaar97c69432021-01-15 16:45:21 +01001941 exe a:start .. 'mark ['
1942 exe a:end .. 'mark ]'
Bram Moolenaaraace2152017-11-05 16:23:10 +01001943endfunc
1944
1945" Verify the effects of autocmds on '[ and ']
1946func Test_change_mark_in_autocmds()
1947 edit! Xtest
Bram Moolenaar97c69432021-01-15 16:45:21 +01001948 call feedkeys("ia\<CR>b\<CR>c\<CR>d\<C-g>u\<Esc>", 'xtn')
Bram Moolenaaraace2152017-11-05 16:23:10 +01001949
1950 call SetChangeMarks(2, 3)
1951 write
1952 call assert_equal([1, 4], [line("'["), line("']")])
1953
1954 call SetChangeMarks(2, 3)
1955 au BufWritePre * call assert_equal([1, 4], [line("'["), line("']")])
1956 write
1957 au! BufWritePre
1958
Bram Moolenaar14ddd222020-08-05 12:02:40 +02001959 if has('unix')
Bram Moolenaaraace2152017-11-05 16:23:10 +01001960 write XtestFilter
1961 write >> XtestFilter
1962
1963 call SetChangeMarks(2, 3)
1964 " Marks are set to the entire range of the write
1965 au FilterWritePre * call assert_equal([1, 4], [line("'["), line("']")])
1966 " '[ is adjusted to just before the line that will receive the filtered
1967 " data
1968 au FilterReadPre * call assert_equal([4, 4], [line("'["), line("']")])
1969 " The filtered data is read into the buffer, and the source lines are
1970 " still present, so the range is after the source lines
1971 au FilterReadPost * call assert_equal([5, 12], [line("'["), line("']")])
1972 %!cat XtestFilter
1973 " After the filtered data is read, the original lines are deleted
1974 call assert_equal([1, 8], [line("'["), line("']")])
1975 au! FilterWritePre,FilterReadPre,FilterReadPost
1976 undo
1977
1978 call SetChangeMarks(1, 4)
1979 au FilterWritePre * call assert_equal([2, 3], [line("'["), line("']")])
1980 au FilterReadPre * call assert_equal([3, 3], [line("'["), line("']")])
1981 au FilterReadPost * call assert_equal([4, 11], [line("'["), line("']")])
1982 2,3!cat XtestFilter
1983 call assert_equal([2, 9], [line("'["), line("']")])
1984 au! FilterWritePre,FilterReadPre,FilterReadPost
1985 undo
1986
1987 call delete('XtestFilter')
1988 endif
1989
1990 call SetChangeMarks(1, 4)
1991 au FileWritePre * call assert_equal([2, 3], [line("'["), line("']")])
1992 2,3write Xtest2
1993 au! FileWritePre
1994
1995 call SetChangeMarks(2, 3)
1996 au FileAppendPre * call assert_equal([1, 4], [line("'["), line("']")])
1997 write >> Xtest2
1998 au! FileAppendPre
1999
2000 call SetChangeMarks(1, 4)
2001 au FileAppendPre * call assert_equal([2, 3], [line("'["), line("']")])
2002 2,3write >> Xtest2
2003 au! FileAppendPre
2004
2005 call SetChangeMarks(1, 1)
2006 au FileReadPre * call assert_equal([3, 1], [line("'["), line("']")])
2007 au FileReadPost * call assert_equal([4, 11], [line("'["), line("']")])
2008 3read Xtest2
2009 au! FileReadPre,FileReadPost
2010 undo
2011
2012 call SetChangeMarks(4, 4)
2013 " When the line is 0, it's adjusted to 1
2014 au FileReadPre * call assert_equal([1, 4], [line("'["), line("']")])
2015 au FileReadPost * call assert_equal([1, 8], [line("'["), line("']")])
2016 0read Xtest2
2017 au! FileReadPre,FileReadPost
2018 undo
2019
2020 call SetChangeMarks(4, 4)
2021 " When the line is 0, it's adjusted to 1
2022 au FileReadPre * call assert_equal([1, 4], [line("'["), line("']")])
2023 au FileReadPost * call assert_equal([2, 9], [line("'["), line("']")])
2024 1read Xtest2
2025 au! FileReadPre,FileReadPost
2026 undo
2027
2028 bwipe!
2029 call delete('Xtest')
2030 call delete('Xtest2')
2031endfunc
2032
2033func Test_Filter_noshelltemp()
Bram Moolenaaraeb313f2020-11-27 19:13:28 +01002034 CheckExecutable cat
Bram Moolenaaraace2152017-11-05 16:23:10 +01002035
2036 enew!
2037 call setline(1, ['a', 'b', 'c', 'd'])
2038
2039 let shelltemp = &shelltemp
2040 set shelltemp
2041
2042 let g:filter_au = 0
2043 au FilterWritePre * let g:filter_au += 1
2044 au FilterReadPre * let g:filter_au += 1
2045 au FilterReadPost * let g:filter_au += 1
2046 %!cat
2047 call assert_equal(3, g:filter_au)
2048
2049 if has('filterpipe')
2050 set noshelltemp
2051
2052 let g:filter_au = 0
2053 au FilterWritePre * let g:filter_au += 1
2054 au FilterReadPre * let g:filter_au += 1
2055 au FilterReadPost * let g:filter_au += 1
2056 %!cat
2057 call assert_equal(0, g:filter_au)
2058 endif
2059
2060 au! FilterWritePre,FilterReadPre,FilterReadPost
2061 let &shelltemp = shelltemp
2062 bwipe!
2063endfunc
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01002064
2065func Test_TextYankPost()
2066 enew!
2067 call setline(1, ['foo'])
2068
2069 let g:event = []
2070 au TextYankPost * let g:event = copy(v:event)
2071
2072 call assert_equal({}, v:event)
2073 call assert_fails('let v:event = {}', 'E46:')
2074 call assert_fails('let v:event.mykey = 0', 'E742:')
2075
2076 norm "ayiw
2077 call assert_equal(
Bram Moolenaara016eeb2022-04-09 11:37:38 +01002078 \ #{regcontents: ['foo'], regname: 'a', operator: 'y',
2079 \ regtype: 'v', visual: v:false, inclusive: v:true},
2080 \ g:event)
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01002081 norm y_
2082 call assert_equal(
Bram Moolenaara016eeb2022-04-09 11:37:38 +01002083 \ #{regcontents: ['foo'], regname: '', operator: 'y', regtype: 'V',
2084 \ visual: v:false, inclusive: v:false},
2085 \ g:event)
Bram Moolenaar37d16732020-06-12 22:09:01 +02002086 norm Vy
2087 call assert_equal(
Bram Moolenaara016eeb2022-04-09 11:37:38 +01002088 \ #{regcontents: ['foo'], regname: '', operator: 'y', regtype: 'V',
2089 \ visual: v:true, inclusive: v:true},
2090 \ g:event)
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01002091 call feedkeys("\<C-V>y", 'x')
2092 call assert_equal(
Bram Moolenaara016eeb2022-04-09 11:37:38 +01002093 \ #{regcontents: ['f'], regname: '', operator: 'y', regtype: "\x161",
2094 \ visual: v:true, inclusive: v:true},
2095 \ g:event)
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01002096 norm "xciwbar
2097 call assert_equal(
Bram Moolenaara016eeb2022-04-09 11:37:38 +01002098 \ #{regcontents: ['foo'], regname: 'x', operator: 'c', regtype: 'v',
2099 \ visual: v:false, inclusive: v:true},
2100 \ g:event)
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01002101 norm "bdiw
2102 call assert_equal(
Bram Moolenaara016eeb2022-04-09 11:37:38 +01002103 \ #{regcontents: ['bar'], regname: 'b', operator: 'd', regtype: 'v',
2104 \ visual: v:false, inclusive: v:true},
2105 \ g:event)
2106
2107 call setline(1, 'foobar')
2108 " exclusive motion
2109 norm $"ay0
2110 call assert_equal(
2111 \ #{regcontents: ['fooba'], regname: 'a', operator: 'y', regtype: 'v',
2112 \ visual: v:false, inclusive: v:false},
2113 \ g:event)
2114 " inclusive motion
2115 norm 0"ay$
2116 call assert_equal(
2117 \ #{regcontents: ['foobar'], regname: 'a', operator: 'y', regtype: 'v',
2118 \ visual: v:false, inclusive: v:true},
2119 \ g:event)
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01002120
2121 call assert_equal({}, v:event)
2122
Bram Moolenaarfccbf062020-11-26 20:34:00 +01002123 if has('clipboard_working') && !has('gui_running')
2124 " Test that when the visual selection is automatically copied to clipboard
2125 " register a TextYankPost is emitted
2126 call setline(1, ['foobar'])
2127
2128 let @* = ''
2129 set clipboard=autoselect
2130 exe "norm! ggviw\<Esc>"
2131 call assert_equal(
Bram Moolenaara016eeb2022-04-09 11:37:38 +01002132 \ #{regcontents: ['foobar'], regname: '*', operator: 'y',
2133 \ regtype: 'v', visual: v:true, inclusive: v:false},
2134 \ g:event)
Bram Moolenaarfccbf062020-11-26 20:34:00 +01002135
2136 let @+ = ''
2137 set clipboard=autoselectplus
2138 exe "norm! ggviw\<Esc>"
2139 call assert_equal(
Bram Moolenaara016eeb2022-04-09 11:37:38 +01002140 \ #{regcontents: ['foobar'], regname: '+', operator: 'y',
2141 \ regtype: 'v', visual: v:true, inclusive: v:false},
2142 \ g:event)
Bram Moolenaarfccbf062020-11-26 20:34:00 +01002143
2144 set clipboard&vim
2145 endif
2146
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01002147 au! TextYankPost
2148 unlet g:event
2149 bwipe!
2150endfunc
Bram Moolenaar9bca8052017-12-18 12:37:55 +01002151
Bram Moolenaar9a046fd2021-01-28 13:47:59 +01002152func Test_autocommand_all_events()
2153 call assert_fails('au * * bwipe', 'E1155:')
2154 call assert_fails('au * x bwipe', 'E1155:')
Bram Moolenaarb6db1462021-12-24 19:24:47 +00002155 call assert_fails('au! * x bwipe', 'E1155:')
Bram Moolenaar4fb921e2017-12-18 15:33:00 +01002156endfunc
Bram Moolenaarb7407d32018-02-03 17:36:27 +01002157
Bram Moolenaarf6246f52022-02-11 16:30:12 +00002158func Test_autocmd_user()
2159 au User MyEvent let s:res = [expand("<afile>"), expand("<amatch>")]
2160 doautocmd User MyEvent
2161 call assert_equal(['MyEvent', 'MyEvent'], s:res)
2162 au! User
2163 unlet s:res
2164endfunc
2165
Bram Moolenaarb7407d32018-02-03 17:36:27 +01002166function s:Before_test_dirchanged()
2167 augroup test_dirchanged
2168 autocmd!
2169 augroup END
2170 let s:li = []
2171 let s:dir_this = getcwd()
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02002172 let s:dir_foo = s:dir_this . '/Xfoo'
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01002173 call mkdir(s:dir_foo)
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02002174 let s:dir_bar = s:dir_this . '/Xbar'
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01002175 call mkdir(s:dir_bar)
Bram Moolenaarb7407d32018-02-03 17:36:27 +01002176endfunc
2177
2178function s:After_test_dirchanged()
Bram Moolenaar3503d7c2019-11-09 20:10:17 +01002179 call chdir(s:dir_this)
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01002180 call delete(s:dir_foo, 'd')
2181 call delete(s:dir_bar, 'd')
Bram Moolenaarb7407d32018-02-03 17:36:27 +01002182 augroup test_dirchanged
2183 autocmd!
2184 augroup END
2185endfunc
2186
2187function Test_dirchanged_global()
2188 call s:Before_test_dirchanged()
Bram Moolenaarf6246f52022-02-11 16:30:12 +00002189 autocmd test_dirchanged DirChangedPre global call add(s:li, expand("<amatch>") .. " pre cd " .. v:event.directory)
Bram Moolenaarb7407d32018-02-03 17:36:27 +01002190 autocmd test_dirchanged DirChanged global call add(s:li, "cd:")
2191 autocmd test_dirchanged DirChanged global call add(s:li, expand("<afile>"))
Bram Moolenaar3503d7c2019-11-09 20:10:17 +01002192 call chdir(s:dir_foo)
Bram Moolenaarf6246f52022-02-11 16:30:12 +00002193 let expected = ["global pre cd " .. s:dir_foo, "cd:", s:dir_foo]
Bram Moolenaar28e8f732022-02-09 12:58:20 +00002194 call assert_equal(expected, s:li)
Bram Moolenaar3503d7c2019-11-09 20:10:17 +01002195 call chdir(s:dir_foo)
Bram Moolenaar28e8f732022-02-09 12:58:20 +00002196 call assert_equal(expected, s:li)
Bram Moolenaar3503d7c2019-11-09 20:10:17 +01002197 exe 'lcd ' .. fnameescape(s:dir_bar)
Bram Moolenaar28e8f732022-02-09 12:58:20 +00002198 call assert_equal(expected, s:li)
Bram Moolenaard8c9d322022-06-12 11:49:16 +01002199
2200 exe 'cd ' .. s:dir_foo
2201 exe 'cd ' .. s:dir_bar
2202 autocmd! test_dirchanged DirChanged global let g:result = expand("<afile>")
2203 cd -
Bram Moolenaardb77c492022-06-12 23:26:50 +01002204 call assert_equal(s:dir_foo, substitute(g:result, '\\', '/', 'g'))
Bram Moolenaard8c9d322022-06-12 11:49:16 +01002205
Bram Moolenaarb7407d32018-02-03 17:36:27 +01002206 call s:After_test_dirchanged()
2207endfunc
2208
2209function Test_dirchanged_local()
2210 call s:Before_test_dirchanged()
2211 autocmd test_dirchanged DirChanged window call add(s:li, "lcd:")
2212 autocmd test_dirchanged DirChanged window call add(s:li, expand("<afile>"))
Bram Moolenaar3503d7c2019-11-09 20:10:17 +01002213 call chdir(s:dir_foo)
Bram Moolenaarb7407d32018-02-03 17:36:27 +01002214 call assert_equal([], s:li)
Bram Moolenaar3503d7c2019-11-09 20:10:17 +01002215 exe 'lcd ' .. fnameescape(s:dir_bar)
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01002216 call assert_equal(["lcd:", s:dir_bar], s:li)
Bram Moolenaar3503d7c2019-11-09 20:10:17 +01002217 exe 'lcd ' .. fnameescape(s:dir_bar)
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01002218 call assert_equal(["lcd:", s:dir_bar], s:li)
Bram Moolenaarb7407d32018-02-03 17:36:27 +01002219 call s:After_test_dirchanged()
2220endfunc
2221
2222function Test_dirchanged_auto()
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +02002223 CheckOption autochdir
Bram Moolenaarb7407d32018-02-03 17:36:27 +01002224 call s:Before_test_dirchanged()
2225 call test_autochdir()
Bram Moolenaar28e8f732022-02-09 12:58:20 +00002226 autocmd test_dirchanged DirChangedPre auto call add(s:li, "pre cd " .. v:event.directory)
Bram Moolenaarb7407d32018-02-03 17:36:27 +01002227 autocmd test_dirchanged DirChanged auto call add(s:li, "auto:")
2228 autocmd test_dirchanged DirChanged auto call add(s:li, expand("<afile>"))
2229 set acd
Bram Moolenaar3503d7c2019-11-09 20:10:17 +01002230 cd ..
Bram Moolenaarb7407d32018-02-03 17:36:27 +01002231 call assert_equal([], s:li)
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01002232 exe 'edit ' . s:dir_foo . '/Xfile'
2233 call assert_equal(s:dir_foo, getcwd())
Bram Moolenaar28e8f732022-02-09 12:58:20 +00002234 let expected = ["pre cd " .. s:dir_foo, "auto:", s:dir_foo]
2235 call assert_equal(expected, s:li)
Bram Moolenaarb7407d32018-02-03 17:36:27 +01002236 set noacd
2237 bwipe!
2238 call s:After_test_dirchanged()
2239endfunc
Bram Moolenaar5a093432018-02-10 18:15:19 +01002240
2241" Test TextChangedI and TextChangedP
2242func Test_ChangedP()
2243 new
2244 call setline(1, ['foo', 'bar', 'foobar'])
2245 call test_override("char_avail", 1)
2246 set complete=. completeopt=menuone
2247
2248 func! TextChangedAutocmd(char)
2249 let g:autocmd .= a:char
2250 endfunc
2251
Christian Brabandtdb3b4462021-10-16 11:58:55 +01002252 " TextChanged will not be triggered, only check that it isn't.
Bram Moolenaar5a093432018-02-10 18:15:19 +01002253 au! TextChanged <buffer> :call TextChangedAutocmd('N')
2254 au! TextChangedI <buffer> :call TextChangedAutocmd('I')
2255 au! TextChangedP <buffer> :call TextChangedAutocmd('P')
2256
2257 call cursor(3, 1)
2258 let g:autocmd = ''
2259 call feedkeys("o\<esc>", 'tnix')
2260 call assert_equal('I', g:autocmd)
2261
2262 let g:autocmd = ''
2263 call feedkeys("Sf", 'tnix')
2264 call assert_equal('II', g:autocmd)
2265
2266 let g:autocmd = ''
2267 call feedkeys("Sf\<C-N>", 'tnix')
2268 call assert_equal('IIP', g:autocmd)
2269
2270 let g:autocmd = ''
2271 call feedkeys("Sf\<C-N>\<C-N>", 'tnix')
2272 call assert_equal('IIPP', g:autocmd)
2273
2274 let g:autocmd = ''
2275 call feedkeys("Sf\<C-N>\<C-N>\<C-N>", 'tnix')
2276 call assert_equal('IIPPP', g:autocmd)
2277
2278 let g:autocmd = ''
2279 call feedkeys("Sf\<C-N>\<C-N>\<C-N>\<C-N>", 'tnix')
2280 call assert_equal('IIPPPP', g:autocmd)
2281
2282 call assert_equal(['foo', 'bar', 'foobar', 'foo'], getline(1, '$'))
2283 " TODO: how should it handle completeopt=noinsert,noselect?
2284
2285 " CleanUp
2286 call test_override("char_avail", 0)
2287 au! TextChanged
2288 au! TextChangedI
2289 au! TextChangedP
2290 delfu TextChangedAutocmd
2291 unlet! g:autocmd
2292 set complete&vim completeopt&vim
2293
2294 bw!
2295endfunc
Bram Moolenaar8c64a362018-03-23 22:39:31 +01002296
Bram Moolenaar91d2e782018-08-07 19:05:01 +02002297let g:setline_handled = v:false
Bram Moolenaar1e115362019-01-09 23:01:02 +01002298func SetLineOne()
Bram Moolenaar91d2e782018-08-07 19:05:01 +02002299 if !g:setline_handled
2300 call setline(1, "(x)")
2301 let g:setline_handled = v:true
2302 endif
2303endfunc
2304
2305func Test_TextChangedI_with_setline()
2306 new
2307 call test_override('char_avail', 1)
2308 autocmd TextChangedI <buffer> call SetLineOne()
2309 call feedkeys("i(\<CR>\<Esc>", 'tx')
2310 call assert_equal('(', getline(1))
2311 call assert_equal('x)', getline(2))
2312 undo
Bram Moolenaar91d2e782018-08-07 19:05:01 +02002313 call assert_equal('', getline(1))
Bram Moolenaar9fa95062018-08-08 22:08:32 +02002314 call assert_equal('', getline(2))
Bram Moolenaar91d2e782018-08-07 19:05:01 +02002315
Bram Moolenaarca34db32022-01-20 11:17:18 +00002316 call test_override('char_avail', 0)
Bram Moolenaar91d2e782018-08-07 19:05:01 +02002317 bwipe!
2318endfunc
2319
Bram Moolenaar8c64a362018-03-23 22:39:31 +01002320func Test_Changed_FirstTime()
Bram Moolenaar8c5a2782019-08-07 23:07:07 +02002321 CheckFeature terminal
2322 CheckNotGui
Bram Moolenaar3cdcb092020-03-18 19:18:10 +01002323 " Starting a terminal to run Vim is always considered flaky.
Bram Moolenaar30d53e22020-03-18 21:10:44 +01002324 let g:test_is_flaky = 1
Bram Moolenaar8c5a2782019-08-07 23:07:07 +02002325
Bram Moolenaar8c64a362018-03-23 22:39:31 +01002326 " Prepare file for TextChanged event.
2327 call writefile([''], 'Xchanged.txt')
2328 let buf = term_start([GetVimProg(), '--clean', '-c', 'set noswapfile'], {'term_rows': 3})
2329 call assert_equal('running', term_getstatus(buf))
Bram Moolenaar1834d372018-03-29 17:40:46 +02002330 " Wait for the ruler (in the status line) to be shown.
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01002331 " In ConPTY, there is additional character which is drawn up to the width of
2332 " the screen.
2333 if has('conpty')
2334 call WaitForAssert({-> assert_match('\<All.*$', term_getline(buf, 3))})
2335 else
2336 call WaitForAssert({-> assert_match('\<All$', term_getline(buf, 3))})
2337 endif
Bram Moolenaar8c64a362018-03-23 22:39:31 +01002338 " It's only adding autocmd, so that no event occurs.
2339 call term_sendkeys(buf, ":au! TextChanged <buffer> call writefile(['No'], 'Xchanged.txt')\<cr>")
2340 call term_sendkeys(buf, "\<C-\\>\<C-N>:qa!\<cr>")
Bram Moolenaar50182fa2018-04-28 21:34:40 +02002341 call WaitForAssert({-> assert_equal('finished', term_getstatus(buf))})
Bram Moolenaar8c64a362018-03-23 22:39:31 +01002342 call assert_equal([''], readfile('Xchanged.txt'))
2343
2344 " clean up
2345 call delete('Xchanged.txt')
2346 bwipe!
2347endfunc
Bram Moolenaar0566e892019-01-24 19:37:40 +01002348
Bram Moolenaareb93f3f2019-04-04 15:04:56 +02002349func Test_autocmd_nested()
2350 let g:did_nested = 0
2351 augroup Testing
2352 au WinNew * edit somefile
2353 au BufNew * let g:did_nested = 1
2354 augroup END
2355 split
2356 call assert_equal(0, g:did_nested)
2357 close
2358 bwipe! somefile
2359
2360 " old nested argument still works
2361 augroup Testing
2362 au!
2363 au WinNew * nested edit somefile
2364 au BufNew * let g:did_nested = 1
2365 augroup END
2366 split
2367 call assert_equal(1, g:did_nested)
2368 close
2369 bwipe! somefile
2370
2371 " New ++nested argument works
2372 augroup Testing
2373 au!
2374 au WinNew * ++nested edit somefile
2375 au BufNew * let g:did_nested = 1
2376 augroup END
2377 split
2378 call assert_equal(1, g:did_nested)
2379 close
2380 bwipe! somefile
2381
Bram Moolenaarf0775142022-03-04 20:10:38 +00002382 " nested without ++ does not work in Vim9 script
2383 call assert_fails('vim9cmd au WinNew * nested echo fails', 'E1078:')
2384
Bram Moolenaareb93f3f2019-04-04 15:04:56 +02002385 augroup Testing
2386 au!
2387 augroup END
2388
2389 call assert_fails('au WinNew * ++nested ++nested echo bad', 'E983:')
2390 call assert_fails('au WinNew * nested nested echo bad', 'E983:')
2391endfunc
2392
Bram Moolenaar5fa9f232022-07-23 09:06:48 +01002393func Test_autocmd_nested_cursor_invalid()
2394 set laststatus=0
2395 copen
2396 cclose
2397 call setline(1, ['foo', 'bar', 'baz'])
2398 3
2399 augroup nested_inv
2400 autocmd User foo ++nested copen
2401 autocmd BufAdd * let &laststatus = 2 - &laststatus
2402 augroup END
2403 doautocmd User foo
2404
2405 augroup nested_inv
2406 au!
2407 augroup END
2408 set laststatus&
Bram Moolenaarb03950f2022-07-26 13:47:13 +01002409 cclose
Bram Moolenaar5fa9f232022-07-23 09:06:48 +01002410 bwipe!
2411endfunc
2412
Bram Moolenaar3d6ee8b2022-07-27 15:23:35 +01002413func Test_autocmd_nested_keeps_cursor_pos()
2414 enew
2415 call setline(1, 'foo')
2416 autocmd User foo ++nested normal! $a
2417 autocmd InsertLeave * :
2418 doautocmd User foo
2419 call assert_equal([0, 1, 3, 0], getpos('.'))
2420
2421 bwipe!
2422endfunc
2423
Bram Moolenaarb03950f2022-07-26 13:47:13 +01002424func Test_autocmd_nested_switch_window()
2425 " run this in a separate Vim so that SafeState works
2426 CheckRunVimInTerminal
2427
2428 let lines =<< trim END
2429 vim9script
2430 ['()']->writefile('Xautofile')
2431 autocmd VimEnter * ++nested edit Xautofile | split
2432 autocmd BufReadPost * autocmd SafeState * ++once foldclosed('.')
2433 autocmd WinEnter * matchadd('ErrorMsg', 'pat')
2434 END
2435 call writefile(lines, 'Xautoscript')
2436 let buf = RunVimInTerminal('-S Xautoscript', {'rows': 10})
2437 call VerifyScreenDump(buf, 'Test_autocmd_nested_switch', {})
2438
2439 call StopVimInTerminal(buf)
2440 call delete('Xautofile')
2441 call delete('Xautoscript')
2442endfunc
2443
Bram Moolenaareb93f3f2019-04-04 15:04:56 +02002444func Test_autocmd_once()
2445 " Without ++once WinNew triggers twice
2446 let g:did_split = 0
2447 augroup Testing
2448 au WinNew * let g:did_split += 1
2449 augroup END
2450 split
2451 split
2452 call assert_equal(2, g:did_split)
2453 call assert_true(exists('#WinNew'))
2454 close
2455 close
2456
2457 " With ++once WinNew triggers once
2458 let g:did_split = 0
2459 augroup Testing
2460 au!
2461 au WinNew * ++once let g:did_split += 1
2462 augroup END
2463 split
2464 split
2465 call assert_equal(1, g:did_split)
2466 call assert_false(exists('#WinNew'))
2467 close
2468 close
2469
2470 call assert_fails('au WinNew * ++once ++once echo bad', 'E983:')
2471endfunc
2472
Bram Moolenaara68e5952019-04-25 22:22:01 +02002473func Test_autocmd_bufreadpre()
2474 new
2475 let b:bufreadpre = 1
Bram Moolenaarab505b12020-03-23 19:28:44 +01002476 call append(0, range(1000))
Bram Moolenaara68e5952019-04-25 22:22:01 +02002477 w! XAutocmdBufReadPre.txt
2478 autocmd BufReadPre <buffer> :let b:bufreadpre += 1
Bram Moolenaarab505b12020-03-23 19:28:44 +01002479 norm! 500gg
Bram Moolenaara68e5952019-04-25 22:22:01 +02002480 sp
Bram Moolenaarab505b12020-03-23 19:28:44 +01002481 norm! 1000gg
Bram Moolenaara68e5952019-04-25 22:22:01 +02002482 wincmd p
2483 let g:wsv1 = winsaveview()
2484 wincmd p
2485 let g:wsv2 = winsaveview()
2486 " triggers BufReadPre, should not move the cursor in either window
2487 " The topline may change one line in a large window.
2488 edit
2489 call assert_inrange(g:wsv2.topline - 1, g:wsv2.topline + 1, winsaveview().topline)
2490 call assert_equal(g:wsv2.lnum, winsaveview().lnum)
2491 call assert_equal(2, b:bufreadpre)
2492 wincmd p
2493 call assert_equal(g:wsv1.topline, winsaveview().topline)
2494 call assert_equal(g:wsv1.lnum, winsaveview().lnum)
2495 call assert_equal(2, b:bufreadpre)
2496 " Now set the cursor position in an BufReadPre autocommand
2497 " (even though the position will be invalid, this should make Vim reset the
2498 " cursor position in the other window.
2499 wincmd p
2500 set cpo+=g
2501 " won't do anything, but try to set the cursor on an invalid lnum
2502 autocmd BufReadPre <buffer> :norm! 70gg
2503 " triggers BufReadPre, should not move the cursor in either window
2504 e
2505 call assert_equal(1, winsaveview().topline)
2506 call assert_equal(1, winsaveview().lnum)
2507 call assert_equal(3, b:bufreadpre)
2508 wincmd p
2509 call assert_equal(g:wsv1.topline, winsaveview().topline)
2510 call assert_equal(g:wsv1.lnum, winsaveview().lnum)
2511 call assert_equal(3, b:bufreadpre)
2512 close
2513 close
2514 call delete('XAutocmdBufReadPre.txt')
2515 set cpo-=g
2516endfunc
2517
Bram Moolenaar5e66b422019-01-24 21:58:10 +01002518" FileChangedShell tested in test_filechanged.vim
Bram Moolenaar69ea5872019-04-25 20:29:00 +02002519
2520" Tests for the following autocommands:
2521" - FileWritePre writing a compressed file
2522" - FileReadPost reading a compressed file
2523" - BufNewFile reading a file template
2524" - BufReadPre decompressing the file to be read
2525" - FilterReadPre substituting characters in the temp file
2526" - FilterReadPost substituting characters after filtering
2527" - FileReadPre set options for decompression
2528" - FileReadPost decompress the file
2529func Test_ReadWrite_Autocmds()
2530 " Run this test only on Unix-like systems and if gzip is available
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +02002531 CheckUnix
2532 CheckExecutable gzip
Bram Moolenaar69ea5872019-04-25 20:29:00 +02002533
2534 " Make $GZIP empty, "-v" would cause trouble.
2535 let $GZIP = ""
2536
2537 " Use a FileChangedShell autocommand to avoid a prompt for 'Xtestfile.gz'
2538 " being modified outside of Vim (noticed on Solaris).
2539 au FileChangedShell * echo 'caught FileChangedShell'
2540
2541 " Test for the FileReadPost, FileWritePre and FileWritePost autocmds
2542 augroup Test1
2543 au!
2544 au FileWritePre *.gz '[,']!gzip
2545 au FileWritePost *.gz undo
2546 au FileReadPost *.gz '[,']!gzip -d
2547 augroup END
2548
2549 new
2550 set bin
2551 call append(0, [
2552 \ 'line 2 Abcdefghijklmnopqrstuvwxyz',
2553 \ 'line 3 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2554 \ 'line 4 Abcdefghijklmnopqrstuvwxyz',
2555 \ 'line 5 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2556 \ 'line 6 Abcdefghijklmnopqrstuvwxyz',
2557 \ 'line 7 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2558 \ 'line 8 Abcdefghijklmnopqrstuvwxyz',
2559 \ 'line 9 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2560 \ 'line 10 Abcdefghijklmnopqrstuvwxyz'
2561 \ ])
2562 1,9write! Xtestfile.gz
2563 enew! | close
2564
2565 new
2566 " Read and decompress the testfile
2567 0read Xtestfile.gz
2568 call assert_equal([
2569 \ 'line 2 Abcdefghijklmnopqrstuvwxyz',
2570 \ 'line 3 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2571 \ 'line 4 Abcdefghijklmnopqrstuvwxyz',
2572 \ 'line 5 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2573 \ 'line 6 Abcdefghijklmnopqrstuvwxyz',
2574 \ 'line 7 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2575 \ 'line 8 Abcdefghijklmnopqrstuvwxyz',
2576 \ 'line 9 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2577 \ 'line 10 Abcdefghijklmnopqrstuvwxyz'
2578 \ ], getline(1, 9))
2579 enew! | close
2580
2581 augroup Test1
2582 au!
2583 augroup END
2584
2585 " Test for the FileAppendPre and FileAppendPost autocmds
2586 augroup Test2
2587 au!
2588 au BufNewFile *.c read Xtest.c
2589 au FileAppendPre *.out '[,']s/new/NEW/
2590 au FileAppendPost *.out !cat Xtest.c >> test.out
2591 augroup END
2592
2593 call writefile(['/*', ' * Here is a new .c file', ' */'], 'Xtest.c')
2594 new foo.c " should load Xtest.c
2595 call assert_equal(['/*', ' * Here is a new .c file', ' */'], getline(2, 4))
2596 w! >> test.out " append it to the output file
2597
2598 let contents = readfile('test.out')
2599 call assert_equal(' * Here is a NEW .c file', contents[2])
2600 call assert_equal(' * Here is a new .c file', contents[5])
2601
2602 call delete('test.out')
2603 enew! | close
2604 augroup Test2
2605 au!
2606 augroup END
2607
2608 " Test for the BufReadPre and BufReadPost autocmds
2609 augroup Test3
2610 au!
2611 " setup autocommands to decompress before reading and re-compress
2612 " afterwards
2613 au BufReadPre *.gz exe '!gzip -d ' . shellescape(expand("<afile>"))
2614 au BufReadPre *.gz call rename(expand("<afile>:r"), expand("<afile>"))
2615 au BufReadPost *.gz call rename(expand("<afile>"), expand("<afile>:r"))
2616 au BufReadPost *.gz exe '!gzip ' . shellescape(expand("<afile>:r"))
2617 augroup END
2618
2619 e! Xtestfile.gz " Edit compressed file
2620 call assert_equal([
2621 \ 'line 2 Abcdefghijklmnopqrstuvwxyz',
2622 \ 'line 3 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2623 \ 'line 4 Abcdefghijklmnopqrstuvwxyz',
2624 \ 'line 5 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2625 \ 'line 6 Abcdefghijklmnopqrstuvwxyz',
2626 \ 'line 7 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2627 \ 'line 8 Abcdefghijklmnopqrstuvwxyz',
2628 \ 'line 9 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2629 \ 'line 10 Abcdefghijklmnopqrstuvwxyz'
2630 \ ], getline(1, 9))
2631
2632 w! >> test.out " Append it to the output file
2633
2634 augroup Test3
2635 au!
2636 augroup END
2637
2638 " Test for the FilterReadPre and FilterReadPost autocmds.
2639 set shelltemp " need temp files here
2640 augroup Test4
2641 au!
2642 au FilterReadPre *.out call rename(expand("<afile>"), expand("<afile>") . ".t")
2643 au FilterReadPre *.out exe 'silent !sed s/e/E/ ' . shellescape(expand("<afile>")) . ".t >" . shellescape(expand("<afile>"))
2644 au FilterReadPre *.out exe 'silent !rm ' . shellescape(expand("<afile>")) . '.t'
2645 au FilterReadPost *.out '[,']s/x/X/g
2646 augroup END
2647
2648 e! test.out " Edit the output file
2649 1,$!cat
2650 call assert_equal([
2651 \ 'linE 2 AbcdefghijklmnopqrstuvwXyz',
2652 \ 'linE 3 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
2653 \ 'linE 4 AbcdefghijklmnopqrstuvwXyz',
2654 \ 'linE 5 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
2655 \ 'linE 6 AbcdefghijklmnopqrstuvwXyz',
2656 \ 'linE 7 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
2657 \ 'linE 8 AbcdefghijklmnopqrstuvwXyz',
2658 \ 'linE 9 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
2659 \ 'linE 10 AbcdefghijklmnopqrstuvwXyz'
2660 \ ], getline(1, 9))
2661 call assert_equal([
2662 \ 'line 2 Abcdefghijklmnopqrstuvwxyz',
2663 \ 'line 3 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2664 \ 'line 4 Abcdefghijklmnopqrstuvwxyz',
2665 \ 'line 5 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2666 \ 'line 6 Abcdefghijklmnopqrstuvwxyz',
2667 \ 'line 7 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2668 \ 'line 8 Abcdefghijklmnopqrstuvwxyz',
2669 \ 'line 9 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2670 \ 'line 10 Abcdefghijklmnopqrstuvwxyz'
2671 \ ], readfile('test.out'))
2672
2673 augroup Test4
2674 au!
2675 augroup END
2676 set shelltemp&vim
2677
2678 " Test for the FileReadPre and FileReadPost autocmds.
2679 augroup Test5
2680 au!
2681 au FileReadPre *.gz exe 'silent !gzip -d ' . shellescape(expand("<afile>"))
2682 au FileReadPre *.gz call rename(expand("<afile>:r"), expand("<afile>"))
2683 au FileReadPost *.gz '[,']s/l/L/
2684 augroup END
2685
2686 new
2687 0r Xtestfile.gz " Read compressed file
2688 call assert_equal([
2689 \ 'Line 2 Abcdefghijklmnopqrstuvwxyz',
2690 \ 'Line 3 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2691 \ 'Line 4 Abcdefghijklmnopqrstuvwxyz',
2692 \ 'Line 5 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2693 \ 'Line 6 Abcdefghijklmnopqrstuvwxyz',
2694 \ 'Line 7 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2695 \ 'Line 8 Abcdefghijklmnopqrstuvwxyz',
2696 \ 'Line 9 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2697 \ 'Line 10 Abcdefghijklmnopqrstuvwxyz'
2698 \ ], getline(1, 9))
2699 call assert_equal([
2700 \ 'line 2 Abcdefghijklmnopqrstuvwxyz',
2701 \ 'line 3 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2702 \ 'line 4 Abcdefghijklmnopqrstuvwxyz',
2703 \ 'line 5 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2704 \ 'line 6 Abcdefghijklmnopqrstuvwxyz',
2705 \ 'line 7 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2706 \ 'line 8 Abcdefghijklmnopqrstuvwxyz',
2707 \ 'line 9 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2708 \ 'line 10 Abcdefghijklmnopqrstuvwxyz'
2709 \ ], readfile('Xtestfile.gz'))
2710
2711 augroup Test5
2712 au!
2713 augroup END
2714
2715 au! FileChangedShell
2716 call delete('Xtestfile.gz')
2717 call delete('Xtest.c')
2718 call delete('test.out')
2719endfunc
Bram Moolenaar23b51392019-05-09 21:38:43 +02002720
2721func Test_throw_in_BufWritePre()
2722 new
2723 call setline(1, ['one', 'two', 'three'])
2724 call assert_false(filereadable('Xthefile'))
2725 augroup throwing
2726 au BufWritePre X* throw 'do not write'
2727 augroup END
2728 try
2729 w Xthefile
2730 catch
2731 let caught = 1
2732 endtry
2733 call assert_equal(1, caught)
2734 call assert_false(filereadable('Xthefile'))
2735
2736 bwipe!
2737 au! throwing
2738endfunc
Bram Moolenaarcadbe1b2019-09-22 21:50:09 +02002739
Bram Moolenaar40fa12a2021-09-22 14:18:13 +02002740func Test_autocmd_in_try_block()
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +01002741 call mkdir('Xintrydir')
Bram Moolenaar40fa12a2021-09-22 14:18:13 +02002742 au BufEnter * let g:fname = expand('%')
2743 try
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +01002744 edit Xintrydir/
Bram Moolenaar40fa12a2021-09-22 14:18:13 +02002745 endtry
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +01002746 call assert_match('Xintrydir', g:fname)
Bram Moolenaar40fa12a2021-09-22 14:18:13 +02002747
2748 unlet g:fname
2749 au! BufEnter
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +01002750 call delete('Xintrydir', 'rf')
Bram Moolenaar40fa12a2021-09-22 14:18:13 +02002751endfunc
2752
Bram Moolenaarcadbe1b2019-09-22 21:50:09 +02002753func Test_autocmd_SafeState()
2754 CheckRunVimInTerminal
2755
2756 let lines =<< trim END
2757 let g:safe = 0
2758 let g:again = ''
2759 au SafeState * let g:safe += 1
2760 au SafeStateAgain * let g:again ..= 'x'
2761 func CallTimer()
2762 call timer_start(10, {id -> execute('let g:again ..= "t"')})
2763 endfunc
2764 END
2765 call writefile(lines, 'XSafeState')
2766 let buf = RunVimInTerminal('-S XSafeState', #{rows: 6})
2767
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01002768 " Sometimes we loop to handle a K_IGNORE, SafeState may be triggered once or
Bram Moolenaar8fb1b472020-02-23 16:16:26 +01002769 " more often.
Bram Moolenaarcadbe1b2019-09-22 21:50:09 +02002770 call term_sendkeys(buf, ":echo g:safe\<CR>")
Bram Moolenaar8fb1b472020-02-23 16:16:26 +01002771 call WaitForAssert({-> assert_match('^\d ', term_getline(buf, 6))}, 1000)
Bram Moolenaarcadbe1b2019-09-22 21:50:09 +02002772
Bram Moolenaar8fb1b472020-02-23 16:16:26 +01002773 " SafeStateAgain should be invoked at least three times
Bram Moolenaarcadbe1b2019-09-22 21:50:09 +02002774 call term_sendkeys(buf, ":echo g:again\<CR>")
Bram Moolenaar8fb1b472020-02-23 16:16:26 +01002775 call WaitForAssert({-> assert_match('^xxx', term_getline(buf, 6))}, 1000)
Bram Moolenaarcadbe1b2019-09-22 21:50:09 +02002776
2777 call term_sendkeys(buf, ":let g:again = ''\<CR>:call CallTimer()\<CR>")
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02002778 call TermWait(buf, 50)
Bram Moolenaar0f6629a2019-09-22 23:24:13 +02002779 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02002780 call TermWait(buf, 50)
Bram Moolenaarcadbe1b2019-09-22 21:50:09 +02002781 call term_sendkeys(buf, ":echo g:again\<CR>")
2782 call WaitForAssert({-> assert_match('xtx', term_getline(buf, 6))}, 1000)
2783
2784 call StopVimInTerminal(buf)
2785 call delete('XSafeState')
2786endfunc
Bram Moolenaar23324a02019-10-01 17:39:04 +02002787
2788func Test_autocmd_CmdWinEnter()
2789 CheckRunVimInTerminal
Bram Moolenaar21829c52021-01-26 22:42:21 +01002790 CheckFeature cmdwin
2791
Bram Moolenaar23324a02019-10-01 17:39:04 +02002792 let lines =<< trim END
Egor Zvorykin125ffd22021-11-17 14:01:14 +00002793 augroup vimHints | au! | augroup END
Bram Moolenaar23324a02019-10-01 17:39:04 +02002794 let b:dummy_var = 'This is a dummy'
2795 autocmd CmdWinEnter * quit
2796 let winnr = winnr('$')
2797 END
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01002798 let filename = 'XCmdWinEnter'
Bram Moolenaar23324a02019-10-01 17:39:04 +02002799 call writefile(lines, filename)
2800 let buf = RunVimInTerminal('-S '.filename, #{rows: 6})
2801
2802 call term_sendkeys(buf, "q:")
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02002803 call TermWait(buf)
Bram Moolenaar23324a02019-10-01 17:39:04 +02002804 call term_sendkeys(buf, ":echo b:dummy_var\<cr>")
Bram Moolenaar353c3512020-03-15 14:19:26 +01002805 call WaitForAssert({-> assert_match('^This is a dummy', term_getline(buf, 6))}, 2000)
Bram Moolenaar23324a02019-10-01 17:39:04 +02002806 call term_sendkeys(buf, ":echo &buftype\<cr>")
2807 call WaitForAssert({-> assert_notmatch('^nofile', term_getline(buf, 6))}, 1000)
2808 call term_sendkeys(buf, ":echo winnr\<cr>")
2809 call WaitForAssert({-> assert_match('^1', term_getline(buf, 6))}, 1000)
2810
2811 " clean up
2812 call StopVimInTerminal(buf)
2813 call delete(filename)
2814endfunc
Bram Moolenaarec66c412019-10-11 21:19:13 +02002815
2816func Test_autocmd_was_using_freed_memory()
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01002817 CheckFeature quickfix
2818
Bram Moolenaarec66c412019-10-11 21:19:13 +02002819 pedit xx
2820 n x
Bram Moolenaar92bb83e2021-02-03 23:04:46 +01002821 augroup winenter
2822 au WinEnter * if winnr('$') > 2 | quit | endif
2823 augroup END
Bram Moolenaarec66c412019-10-11 21:19:13 +02002824 split
Bram Moolenaar92bb83e2021-02-03 23:04:46 +01002825
2826 augroup winenter
2827 au! WinEnter
2828 augroup END
2829
2830 bwipe xx
2831 bwipe x
2832 pclose
Bram Moolenaarec66c412019-10-11 21:19:13 +02002833endfunc
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +01002834
2835func Test_BufWrite_lockmarks()
Bram Moolenaarf08b0eb2021-10-16 13:00:14 +01002836 let g:test_is_flaky = 1
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +01002837 edit! Xtest
2838 call setline(1, ['a', 'b', 'c', 'd'])
2839
2840 " :lockmarks preserves the marks
2841 call SetChangeMarks(2, 3)
2842 lockmarks write
2843 call assert_equal([2, 3], [line("'["), line("']")])
2844
2845 " *WritePre autocmds get the correct line range, but lockmarks preserves the
2846 " original values for the user
2847 augroup lockmarks
2848 au!
2849 au BufWritePre,FilterWritePre * call assert_equal([1, 4], [line("'["), line("']")])
2850 au FileWritePre * call assert_equal([3, 4], [line("'["), line("']")])
2851 augroup END
2852
2853 lockmarks write
2854 call assert_equal([2, 3], [line("'["), line("']")])
2855
2856 if executable('cat')
2857 lockmarks %!cat
2858 call assert_equal([2, 3], [line("'["), line("']")])
2859 endif
2860
2861 lockmarks 3,4write Xtest2
2862 call assert_equal([2, 3], [line("'["), line("']")])
2863
2864 au! lockmarks
2865 augroup! lockmarks
2866 call delete('Xtest')
2867 call delete('Xtest2')
2868endfunc
Bram Moolenaarce6db022020-01-07 20:11:42 +01002869
2870func Test_FileType_spell()
2871 if !isdirectory('/tmp')
2872 throw "Skipped: requires /tmp directory"
2873 endif
2874
2875 " this was crashing with an invalid free()
2876 setglobal spellfile=/tmp/en.utf-8.add
2877 augroup crash
2878 autocmd!
2879 autocmd BufNewFile,BufReadPost crashfile setf somefiletype
2880 autocmd BufNewFile,BufReadPost crashfile set ft=anotherfiletype
2881 autocmd FileType anotherfiletype setlocal spell
2882 augroup END
2883 func! NoCrash() abort
2884 edit /tmp/crashfile
2885 endfunc
2886 call NoCrash()
2887
2888 au! crash
2889 setglobal spellfile=
2890endfunc
Bram Moolenaarbc2b71d2020-02-17 21:33:30 +01002891
Bram Moolenaar406cd902020-02-18 21:54:41 +01002892" Test closing a window or editing another buffer from a FileChangedRO handler
2893" in a readonly buffer
2894func Test_FileChangedRO_winclose()
Bram Moolenaar62cd26a2020-10-11 20:08:44 +02002895 call test_override('ui_delay', 10)
2896
Bram Moolenaar406cd902020-02-18 21:54:41 +01002897 augroup FileChangedROTest
2898 au!
2899 autocmd FileChangedRO * quit
2900 augroup END
2901 new
2902 set readonly
2903 call assert_fails('normal i', 'E788:')
2904 close
2905 augroup! FileChangedROTest
2906
2907 augroup FileChangedROTest
2908 au!
2909 autocmd FileChangedRO * edit Xfile
2910 augroup END
2911 new
2912 set readonly
2913 call assert_fails('normal i', 'E788:')
2914 close
2915 augroup! FileChangedROTest
Bram Moolenaar62cd26a2020-10-11 20:08:44 +02002916 call test_override('ALL', 0)
Bram Moolenaar406cd902020-02-18 21:54:41 +01002917endfunc
2918
Bram Moolenaar0c81d1b2020-02-22 22:45:55 +01002919func LogACmd()
2920 call add(g:logged, line('$'))
2921endfunc
2922
2923func Test_TermChanged()
Bram Moolenaard28e0b32020-02-22 23:08:52 +01002924 CheckNotGui
2925
Bram Moolenaar0c81d1b2020-02-22 22:45:55 +01002926 enew!
2927 tabnew
2928 call setline(1, ['a', 'b', 'c', 'd'])
2929 $
2930 au TermChanged * call LogACmd()
2931 let g:logged = []
2932 let term_save = &term
2933 set term=xterm
2934 call assert_equal([1, 4], g:logged)
2935
2936 au! TermChanged
2937 let &term = term_save
2938 bwipe!
2939endfunc
2940
Bram Moolenaare3284872020-03-19 13:55:03 +01002941" Test for FileReadCmd autocmd
2942func Test_autocmd_FileReadCmd()
2943 func ReadFileCmd()
2944 call append(line('$'), "v:cmdarg = " .. v:cmdarg)
2945 endfunc
2946 augroup FileReadCmdTest
2947 au!
2948 au FileReadCmd Xtest call ReadFileCmd()
2949 augroup END
2950
2951 new
2952 read ++bin Xtest
2953 read ++nobin Xtest
2954 read ++edit Xtest
2955 read ++bad=keep Xtest
2956 read ++bad=drop Xtest
2957 read ++bad=- Xtest
2958 read ++ff=unix Xtest
2959 read ++ff=dos Xtest
2960 read ++ff=mac Xtest
2961 read ++enc=utf-8 Xtest
2962
2963 call assert_equal(['',
2964 \ 'v:cmdarg = ++bin',
2965 \ 'v:cmdarg = ++nobin',
2966 \ 'v:cmdarg = ++edit',
2967 \ 'v:cmdarg = ++bad=keep',
2968 \ 'v:cmdarg = ++bad=drop',
2969 \ 'v:cmdarg = ++bad=-',
2970 \ 'v:cmdarg = ++ff=unix',
2971 \ 'v:cmdarg = ++ff=dos',
2972 \ 'v:cmdarg = ++ff=mac',
2973 \ 'v:cmdarg = ++enc=utf-8'], getline(1, '$'))
2974
2975 close!
2976 augroup FileReadCmdTest
2977 au!
2978 augroup END
2979 delfunc ReadFileCmd
2980endfunc
2981
Bram Moolenaaree4e0c12020-04-06 21:35:05 +02002982" Test for passing invalid arguments to autocmd
2983func Test_autocmd_invalid_args()
2984 " Additional character after * for event
2985 call assert_fails('autocmd *a Xfile set ff=unix', 'E215:')
2986 augroup Test
2987 augroup END
2988 " Invalid autocmd event
2989 call assert_fails('autocmd Bufabc Xfile set ft=vim', 'E216:')
2990 " Invalid autocmd event in a autocmd group
2991 call assert_fails('autocmd Test Bufabc Xfile set ft=vim', 'E216:')
2992 augroup! Test
2993 " Execute all autocmds
2994 call assert_fails('doautocmd * BufEnter', 'E217:')
2995 call assert_fails('augroup! x1a2b3', 'E367:')
2996 call assert_fails('autocmd BufNew <buffer=999> pwd', 'E680:')
Bram Moolenaar531be472020-09-23 22:38:05 +02002997 call assert_fails('autocmd BufNew \) set ff=unix', 'E55:')
Bram Moolenaaree4e0c12020-04-06 21:35:05 +02002998endfunc
2999
3000" Test for deep nesting of autocmds
3001func Test_autocmd_deep_nesting()
3002 autocmd BufEnter Xfile doautocmd BufEnter Xfile
3003 call assert_fails('doautocmd BufEnter Xfile', 'E218:')
3004 autocmd! BufEnter Xfile
3005endfunc
3006
Bram Moolenaarbe5ee862020-06-10 20:56:58 +02003007" Tests for SigUSR1 autocmd event, which is only available on posix systems.
3008func Test_autocmd_sigusr1()
3009 CheckUnix
Bram Moolenaar62cd26a2020-10-11 20:08:44 +02003010 CheckExecutable /bin/kill
Bram Moolenaarbe5ee862020-06-10 20:56:58 +02003011
3012 let g:sigusr1_passed = 0
3013 au SigUSR1 * let g:sigusr1_passed = 1
3014 call system('/bin/kill -s usr1 ' . getpid())
3015 call WaitForAssert({-> assert_true(g:sigusr1_passed)})
3016
3017 au! SigUSR1
3018 unlet g:sigusr1_passed
3019endfunc
3020
Bram Moolenaarb340bae2020-06-15 19:51:56 +02003021" Test for BufReadPre autocmd deleting the file
3022func Test_BufReadPre_delfile()
3023 augroup TestAuCmd
3024 au!
Bram Moolenaare7cda972022-08-29 11:02:59 +01003025 autocmd BufReadPre XbufreadPre call delete('XbufreadPre')
Bram Moolenaarb340bae2020-06-15 19:51:56 +02003026 augroup END
Bram Moolenaare7cda972022-08-29 11:02:59 +01003027 call writefile([], 'XbufreadPre')
3028 call assert_fails('new XbufreadPre', 'E200:')
3029 call assert_equal('XbufreadPre', @%)
Bram Moolenaarb340bae2020-06-15 19:51:56 +02003030 call assert_equal(1, &readonly)
Bram Moolenaare7cda972022-08-29 11:02:59 +01003031 call delete('XbufreadPre')
Bram Moolenaarb340bae2020-06-15 19:51:56 +02003032 augroup TestAuCmd
3033 au!
3034 augroup END
3035 close!
3036endfunc
3037
3038" Test for BufReadPre autocmd changing the current buffer
3039func Test_BufReadPre_changebuf()
3040 augroup TestAuCmd
3041 au!
Bram Moolenaare7cda972022-08-29 11:02:59 +01003042 autocmd BufReadPre Xchangebuf edit Xsomeotherfile
Bram Moolenaarb340bae2020-06-15 19:51:56 +02003043 augroup END
Bram Moolenaare7cda972022-08-29 11:02:59 +01003044 call writefile([], 'Xchangebuf')
3045 call assert_fails('new Xchangebuf', 'E201:')
Bram Moolenaarb340bae2020-06-15 19:51:56 +02003046 call assert_equal('Xsomeotherfile', @%)
3047 call assert_equal(1, &readonly)
Bram Moolenaare7cda972022-08-29 11:02:59 +01003048 call delete('Xchangebuf')
Bram Moolenaarb340bae2020-06-15 19:51:56 +02003049 augroup TestAuCmd
3050 au!
3051 augroup END
3052 close!
3053endfunc
3054
3055" Test for BufWipeouti autocmd changing the current buffer when reading a file
3056" in an empty buffer with 'f' flag in 'cpo'
3057func Test_BufDelete_changebuf()
3058 new
3059 augroup TestAuCmd
3060 au!
3061 autocmd BufWipeout * let bufnr = bufadd('somefile') | exe "b " .. bufnr
3062 augroup END
3063 let save_cpo = &cpo
3064 set cpo+=f
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02003065 call assert_fails('r Xfile', ['E812:', 'E484:'])
Bram Moolenaarb340bae2020-06-15 19:51:56 +02003066 call assert_equal('somefile', @%)
3067 let &cpo = save_cpo
3068 augroup TestAuCmd
3069 au!
3070 augroup END
3071 close!
3072endfunc
3073
Bram Moolenaar0fe937f2020-06-16 22:42:04 +02003074" Test for the temporary internal window used to execute autocmds
3075func Test_autocmd_window()
3076 %bw!
3077 edit one.txt
3078 tabnew two.txt
Bram Moolenaar41cd8032021-03-13 15:47:56 +01003079 vnew three.txt
3080 tabnew four.txt
3081 tabprevious
Bram Moolenaar0fe937f2020-06-16 22:42:04 +02003082 let g:blist = []
Bram Moolenaar832adf92020-06-25 19:01:36 +02003083 augroup aucmd_win_test1
Bram Moolenaar0fe937f2020-06-16 22:42:04 +02003084 au!
3085 au BufEnter * call add(g:blist, [expand('<afile>'),
3086 \ win_gettype(bufwinnr(expand('<afile>')))])
3087 augroup END
3088
3089 doautoall BufEnter
Bram Moolenaar41cd8032021-03-13 15:47:56 +01003090 call assert_equal([
3091 \ ['one.txt', 'autocmd'],
3092 \ ['two.txt', ''],
3093 \ ['four.txt', 'autocmd'],
3094 \ ['three.txt', ''],
3095 \ ], g:blist)
Bram Moolenaar0fe937f2020-06-16 22:42:04 +02003096
Bram Moolenaar832adf92020-06-25 19:01:36 +02003097 augroup aucmd_win_test1
Bram Moolenaar0fe937f2020-06-16 22:42:04 +02003098 au!
3099 augroup END
Bram Moolenaar832adf92020-06-25 19:01:36 +02003100 augroup! aucmd_win_test1
3101 %bw!
3102endfunc
3103
3104" Test for trying to close the temporary window used for executing an autocmd
3105func Test_close_autocmd_window()
3106 %bw!
3107 edit one.txt
3108 tabnew two.txt
3109 augroup aucmd_win_test2
3110 au!
3111 au BufEnter * if expand('<afile>') == 'one.txt' | 1close | endif
3112 augroup END
3113
3114 call assert_fails('doautoall BufEnter', 'E813:')
3115
3116 augroup aucmd_win_test2
3117 au!
3118 augroup END
3119 augroup! aucmd_win_test2
Bram Moolenaarcf844172020-06-26 19:44:06 +02003120 %bwipe!
3121endfunc
3122
3123" Test for trying to close the tab that has the temporary window for exeucing
3124" an autocmd.
3125func Test_close_autocmd_tab()
3126 edit one.txt
3127 tabnew two.txt
3128 augroup aucmd_win_test
3129 au!
3130 au BufEnter * if expand('<afile>') == 'one.txt' | tabfirst | tabonly | endif
3131 augroup END
3132
3133 call assert_fails('doautoall BufEnter', 'E813:')
3134
3135 tabonly
3136 augroup aucmd_win_test
3137 au!
3138 augroup END
3139 augroup! aucmd_win_test
3140 %bwipe!
Bram Moolenaar0fe937f2020-06-16 22:42:04 +02003141endfunc
3142
Bram Moolenaarcb1956d2022-01-07 15:45:18 +00003143func Test_Visual_doautoall_redraw()
3144 call setline(1, ['a', 'b'])
3145 new
3146 wincmd p
3147 call feedkeys("G\<C-V>", 'txn')
3148 autocmd User Explode ++once redraw
3149 doautoall User Explode
3150 %bwipe!
3151endfunc
3152
Bram Moolenaar6bcb8772021-02-03 21:23:29 +01003153" This was using freed memory.
3154func Test_BufNew_arglocal()
3155 arglocal
3156 au BufNew * arglocal
3157 call assert_fails('drop xx', 'E1156:')
3158
3159 au! BufNew
3160endfunc
3161
Bram Moolenaar8ab37572021-02-03 21:56:59 +01003162func Test_autocmd_closes_window()
3163 au BufNew,BufWinLeave * e %e
3164 file yyy
3165 au BufNew,BufWinLeave * ball
Bram Moolenaaraad5f9d2021-02-06 17:30:31 +01003166 n xxx
Bram Moolenaar8ab37572021-02-03 21:56:59 +01003167
Bram Moolenaaraad5f9d2021-02-06 17:30:31 +01003168 %bwipe
Bram Moolenaar8ab37572021-02-03 21:56:59 +01003169 au! BufNew
3170 au! BufWinLeave
3171endfunc
3172
Bram Moolenaar92bb83e2021-02-03 23:04:46 +01003173func Test_autocmd_quit_psearch()
3174 sn aa bb
3175 augroup aucmd_win_test
3176 au!
3177 au BufEnter,BufLeave,BufNew,WinEnter,WinLeave,WinNew * if winnr('$') > 1 | q | endif
3178 augroup END
3179 ps /
3180
3181 augroup aucmd_win_test
3182 au!
3183 augroup END
zeertzjq7851c692022-04-21 11:14:01 +01003184 new
3185 pclose
Bram Moolenaar92bb83e2021-02-03 23:04:46 +01003186endfunc
3187
Bram Moolenaaraad5f9d2021-02-06 17:30:31 +01003188" Fuzzer found some strange combination that caused a crash.
3189func Test_autocmd_normal_mess()
Bram Moolenaardd07c022021-02-07 13:32:46 +01003190 " For unknown reason this hangs on MS-Windows
3191 CheckNotMSWindows
3192
Bram Moolenaaraad5f9d2021-02-06 17:30:31 +01003193 augroup aucmd_normal_test
3194 au BufLeave,BufWinLeave,BufHidden,BufUnload,BufDelete,BufWipeout * norm 7q/qc
3195 augroup END
Bram Moolenaar983d83f2021-02-07 12:12:43 +01003196 call assert_fails('o4', 'E1159')
Bram Moolenaaraad5f9d2021-02-06 17:30:31 +01003197 silent! H
Bram Moolenaar983d83f2021-02-07 12:12:43 +01003198 call assert_fails('e xx', 'E1159')
Bram Moolenaaraad5f9d2021-02-06 17:30:31 +01003199 normal G
3200
3201 augroup aucmd_normal_test
3202 au!
3203 augroup END
3204endfunc
3205
Bram Moolenaar8c6951f2021-02-06 18:08:45 +01003206func Test_autocmd_closing_cmdwin()
Bram Moolenaardd07c022021-02-07 13:32:46 +01003207 " For unknown reason this hangs on MS-Windows
3208 CheckNotMSWindows
3209
Bram Moolenaar8c6951f2021-02-06 18:08:45 +01003210 au BufWinLeave * nested q
3211 call assert_fails("norm 7q?\n", 'E855:')
3212
3213 au! BufWinLeave
3214 new
3215 only
3216endfunc
3217
Bram Moolenaar2c7080b2021-02-06 19:19:42 +01003218func Test_autocmd_vimgrep()
3219 augroup aucmd_vimgrep
Charlie Grovesfef44852022-04-19 16:24:12 +01003220 au QuickfixCmdPre,BufNew,BufReadCmd * sb
zeertzjq7851c692022-04-21 11:14:01 +01003221 au QuickfixCmdPre,BufNew,BufReadCmd * q9
Bram Moolenaar2c7080b2021-02-06 19:19:42 +01003222 augroup END
Bram Moolenaardd07c022021-02-07 13:32:46 +01003223 call assert_fails('lv ?a? foo', 'E926:')
Bram Moolenaar2c7080b2021-02-06 19:19:42 +01003224
3225 augroup aucmd_vimgrep
3226 au!
3227 augroup END
3228endfunc
3229
Bram Moolenaar73b8b0a2021-08-01 14:52:32 +02003230func Test_autocmd_with_block()
3231 augroup block_testing
3232 au BufReadPost *.xml {
3233 setlocal matchpairs+=<:>
3234 /<start
3235 }
Bram Moolenaar63b91732021-08-05 20:40:03 +02003236 au CursorHold * {
3237 autocmd BufReadPre * ++once echo 'one' | echo 'two'
3238 g:gotSafeState = 77
3239 }
Bram Moolenaar73b8b0a2021-08-01 14:52:32 +02003240 augroup END
3241
3242 let expected = "\n--- Autocommands ---\nblock_testing BufRead\n *.xml {^@ setlocal matchpairs+=<:>^@ /<start^@ }"
3243 call assert_equal(expected, execute('au BufReadPost *.xml'))
3244
Bram Moolenaar63b91732021-08-05 20:40:03 +02003245 doautocmd CursorHold
3246 call assert_equal(77, g:gotSafeState)
3247 unlet g:gotSafeState
3248
Bram Moolenaar73b8b0a2021-08-01 14:52:32 +02003249 augroup block_testing
3250 au!
Bram Moolenaar75ebd2a2022-06-03 17:39:46 +01003251 autocmd CursorHold * {
3252 if true
3253 # comment
3254 && true
3255
3256 && true
3257 g:done = 'yes'
3258 endif
3259 }
3260 augroup END
3261 doautocmd CursorHold
3262 call assert_equal('yes', g:done)
3263
3264 unlet g:done
3265 augroup block_testing
3266 au!
Bram Moolenaar73b8b0a2021-08-01 14:52:32 +02003267 augroup END
3268endfunc
3269
Christian Brabandtdb3b4462021-10-16 11:58:55 +01003270" Test TextChangedI and TextChanged
3271func Test_Changed_ChangedI()
3272 new
3273 call test_override("char_avail", 1)
3274 let [g:autocmd_i, g:autocmd_n] = ['','']
3275
3276 func! TextChangedAutocmdI(char)
3277 let g:autocmd_{tolower(a:char)} = a:char .. b:changedtick
3278 endfunc
3279
3280 augroup Test_TextChanged
3281 au!
3282 au TextChanged <buffer> :call TextChangedAutocmdI('N')
3283 au TextChangedI <buffer> :call TextChangedAutocmdI('I')
3284 augroup END
3285
3286 call feedkeys("ifoo\<esc>", 'tnix')
3287 " TODO: Test test does not seem to trigger TextChanged autocommand, this
3288 " requires running Vim in a terminal window.
3289 " call assert_equal('N3', g:autocmd_n)
3290 call assert_equal('I3', g:autocmd_i)
3291
3292 call feedkeys("yyp", 'tnix')
3293 " TODO: Test test does not seem to trigger TextChanged autocommand.
3294 " call assert_equal('N4', g:autocmd_n)
3295 call assert_equal('I3', g:autocmd_i)
3296
3297 " CleanUp
3298 call test_override("char_avail", 0)
3299 au! TextChanged <buffer>
3300 au! TextChangedI <buffer>
3301 augroup! Test_TextChanged
3302 delfu TextChangedAutocmdI
3303 unlet! g:autocmd_i g:autocmd_n
3304
3305 bw!
3306endfunc
Bram Moolenaar2c7080b2021-02-06 19:19:42 +01003307
Bram Moolenaar6f2465d2022-03-22 18:13:01 +00003308func Test_closing_autocmd_window()
3309 let lines =<< trim END
3310 edit Xa.txt
3311 tabnew Xb.txt
3312 autocmd BufEnter Xa.txt unhide 1
3313 doautoall BufEnter
3314 END
3315 call v9.CheckScriptFailure(lines, 'E814:')
3316 au! BufEnter
3317 only!
3318 bwipe Xa.txt
3319 bwipe Xb.txt
3320endfunc
3321
Bram Moolenaar347538f2022-03-26 16:28:06 +00003322func Test_bufwipeout_changes_window()
3323 " This should not crash, but we don't have any expectations about what
3324 " happens, changing window in BufWipeout has unpredictable results.
3325 tabedit
3326 let g:window_id = win_getid()
3327 topleft new
3328 setlocal bufhidden=wipe
3329 autocmd BufWipeout <buffer> call win_gotoid(g:window_id)
3330 tabprevious
3331 +tabclose
3332
3333 unlet g:window_id
3334 au! BufWipeout
3335 %bwipe!
3336endfunc
3337
zeertzjq021996f2022-04-10 11:44:04 +01003338func Test_v_event_readonly()
3339 autocmd CompleteChanged * let v:event.width = 0
3340 call assert_fails("normal! i\<C-X>\<C-V>", 'E46:')
3341 au! CompleteChanged
3342
3343 autocmd DirChangedPre * let v:event.directory = ''
3344 call assert_fails('cd .', 'E46:')
3345 au! DirChangedPre
3346
3347 autocmd ModeChanged * let v:event.new_mode = ''
3348 call assert_fails('normal! cc', 'E46:')
3349 au! ModeChanged
3350
3351 autocmd TextYankPost * let v:event.operator = ''
3352 call assert_fails('normal! yy', 'E46:')
3353 au! TextYankPost
3354endfunc
3355
zeertzjqc9e8fd62022-07-26 18:12:38 +01003356" Test for ModeChanged pattern
3357func Test_mode_changes()
3358 let g:index = 0
3359 let g:mode_seq = ['n', 'i', 'n', 'v', 'V', 'i', 'ix', 'i', 'ic', 'i', 'n', 'no', 'n', 'V', 'v', 's', 'n']
3360 func! TestMode()
3361 call assert_equal(g:mode_seq[g:index], get(v:event, "old_mode"))
3362 call assert_equal(g:mode_seq[g:index + 1], get(v:event, "new_mode"))
3363 call assert_equal(mode(1), get(v:event, "new_mode"))
3364 let g:index += 1
3365 endfunc
3366
3367 au ModeChanged * :call TestMode()
3368 let g:n_to_any = 0
3369 au ModeChanged n:* let g:n_to_any += 1
3370 call feedkeys("i\<esc>vVca\<CR>\<C-X>\<C-L>\<esc>ggdG", 'tnix')
3371
3372 let g:V_to_v = 0
3373 au ModeChanged V:v let g:V_to_v += 1
3374 call feedkeys("Vv\<C-G>\<esc>", 'tnix')
3375 call assert_equal(len(filter(g:mode_seq[1:], {idx, val -> val == 'n'})), g:n_to_any)
3376 call assert_equal(1, g:V_to_v)
3377 call assert_equal(len(g:mode_seq) - 1, g:index)
3378
3379 let g:n_to_i = 0
3380 au ModeChanged n:i let g:n_to_i += 1
3381 let g:n_to_niI = 0
3382 au ModeChanged i:niI let g:n_to_niI += 1
3383 let g:niI_to_i = 0
3384 au ModeChanged niI:i let g:niI_to_i += 1
3385 let g:nany_to_i = 0
3386 au ModeChanged n*:i let g:nany_to_i += 1
3387 let g:i_to_n = 0
3388 au ModeChanged i:n let g:i_to_n += 1
3389 let g:nori_to_any = 0
3390 au ModeChanged [ni]:* let g:nori_to_any += 1
3391 let g:i_to_any = 0
3392 au ModeChanged i:* let g:i_to_any += 1
3393 let g:index = 0
3394 let g:mode_seq = ['n', 'i', 'niI', 'i', 'n']
3395 call feedkeys("a\<C-O>l\<esc>", 'tnix')
3396 call assert_equal(len(g:mode_seq) - 1, g:index)
3397 call assert_equal(1, g:n_to_i)
3398 call assert_equal(1, g:n_to_niI)
3399 call assert_equal(1, g:niI_to_i)
3400 call assert_equal(2, g:nany_to_i)
3401 call assert_equal(1, g:i_to_n)
3402 call assert_equal(2, g:i_to_any)
3403 call assert_equal(3, g:nori_to_any)
3404
3405 if has('terminal')
3406 let g:mode_seq += ['c', 'n', 't', 'nt', 'c', 'nt', 'n']
3407 call feedkeys(":term\<CR>\<C-W>N:bd!\<CR>", 'tnix')
3408 call assert_equal(len(g:mode_seq) - 1, g:index)
3409 call assert_equal(1, g:n_to_i)
3410 call assert_equal(1, g:n_to_niI)
3411 call assert_equal(1, g:niI_to_i)
3412 call assert_equal(2, g:nany_to_i)
3413 call assert_equal(1, g:i_to_n)
3414 call assert_equal(2, g:i_to_any)
3415 call assert_equal(5, g:nori_to_any)
3416 endif
3417
3418 if has('cmdwin')
3419 let g:n_to_c = 0
3420 au ModeChanged n:c let g:n_to_c += 1
3421 let g:c_to_n = 0
3422 au ModeChanged c:n let g:c_to_n += 1
3423 let g:mode_seq += ['c', 'n', 'c', 'n']
3424 call feedkeys("q:\<C-C>\<Esc>", 'tnix')
3425 call assert_equal(len(g:mode_seq) - 1, g:index)
3426 call assert_equal(2, g:n_to_c)
3427 call assert_equal(2, g:c_to_n)
3428 unlet g:n_to_c
3429 unlet g:c_to_n
3430 endif
3431
3432 au! ModeChanged
3433 delfunc TestMode
3434 unlet! g:mode_seq
3435 unlet! g:index
3436 unlet! g:n_to_any
3437 unlet! g:V_to_v
3438 unlet! g:n_to_i
3439 unlet! g:n_to_niI
3440 unlet! g:niI_to_i
3441 unlet! g:nany_to_i
3442 unlet! g:i_to_n
3443 unlet! g:nori_to_any
3444 unlet! g:i_to_any
3445endfunc
3446
3447func Test_recursive_ModeChanged()
3448 au! ModeChanged * norm 0u
3449 sil! norm 
3450 au! ModeChanged
3451endfunc
3452
3453func Test_ModeChanged_starts_visual()
3454 " This was triggering ModeChanged before setting VIsual, causing a crash.
3455 au! ModeChanged * norm 0u
3456 sil! norm 
3457
3458 au! ModeChanged
3459endfunc
Bram Moolenaar347538f2022-03-26 16:28:06 +00003460
Charlie Grovesfef44852022-04-19 16:24:12 +01003461func Test_noname_autocmd()
3462 augroup test_noname_autocmd_group
3463 autocmd!
3464 autocmd BufEnter * call add(s:li, ["BufEnter", expand("<afile>")])
3465 autocmd BufDelete * call add(s:li, ["BufDelete", expand("<afile>")])
3466 autocmd BufLeave * call add(s:li, ["BufLeave", expand("<afile>")])
3467 autocmd BufUnload * call add(s:li, ["BufUnload", expand("<afile>")])
3468 autocmd BufWipeout * call add(s:li, ["BufWipeout", expand("<afile>")])
3469 augroup END
3470
3471 let s:li = []
3472 edit foo
3473 call assert_equal([['BufUnload', ''], ['BufDelete', ''], ['BufWipeout', ''], ['BufEnter', 'foo']], s:li)
3474
3475 au! test_noname_autocmd_group
3476 augroup! test_noname_autocmd_group
3477endfunc
3478
Yegappan Lakshmanan1755a912022-05-19 10:31:47 +01003479" Test for the autocmd_get() function
3480func Test_autocmd_get()
3481 augroup TestAutoCmdFns
3482 au!
3483 autocmd BufAdd *.vim echo "bufadd-vim"
3484 autocmd BufAdd *.py echo "bufadd-py"
3485 autocmd BufHidden *.vim echo "bufhidden"
3486 augroup END
3487 augroup TestAutoCmdFns2
3488 autocmd BufAdd *.vim echo "bufadd-vim-2"
3489 autocmd BufRead *.a1b2c3 echo "bufadd-vim-2"
3490 augroup END
3491
3492 let l = autocmd_get()
3493 call assert_true(l->len() > 0)
3494
3495 " Test for getting all the autocmds in a group
3496 let expected = [
3497 \ #{cmd: 'echo "bufadd-vim"', group: 'TestAutoCmdFns',
3498 \ pattern: '*.vim', nested: v:false, once: v:false,
3499 \ event: 'BufAdd'},
3500 \ #{cmd: 'echo "bufadd-py"', group: 'TestAutoCmdFns',
3501 \ pattern: '*.py', nested: v:false, once: v:false,
3502 \ event: 'BufAdd'},
3503 \ #{cmd: 'echo "bufhidden"', group: 'TestAutoCmdFns',
3504 \ pattern: '*.vim', nested: v:false,
3505 \ once: v:false, event: 'BufHidden'}]
3506 call assert_equal(expected, autocmd_get(#{group: 'TestAutoCmdFns'}))
3507
3508 " Test for getting autocmds for all the patterns in a group
3509 call assert_equal(expected, autocmd_get(#{group: 'TestAutoCmdFns',
3510 \ event: '*'}))
3511
3512 " Test for getting autocmds for an event in a group
3513 let expected = [
3514 \ #{cmd: 'echo "bufadd-vim"', group: 'TestAutoCmdFns',
3515 \ pattern: '*.vim', nested: v:false, once: v:false,
3516 \ event: 'BufAdd'},
3517 \ #{cmd: 'echo "bufadd-py"', group: 'TestAutoCmdFns',
3518 \ pattern: '*.py', nested: v:false, once: v:false,
3519 \ event: 'BufAdd'}]
3520 call assert_equal(expected, autocmd_get(#{group: 'TestAutoCmdFns',
3521 \ event: 'BufAdd'}))
3522
3523 " Test for getting the autocmds for all the events in a group for particular
3524 " pattern
3525 call assert_equal([{'cmd': 'echo "bufadd-py"', 'group': 'TestAutoCmdFns',
3526 \ 'pattern': '*.py', 'nested': v:false, 'once': v:false,
3527 \ 'event': 'BufAdd'}],
3528 \ autocmd_get(#{group: 'TestAutoCmdFns', event: '*', pattern: '*.py'}))
3529
3530 " Test for getting the autocmds for an events in a group for particular
3531 " pattern
3532 let l = autocmd_get(#{group: 'TestAutoCmdFns', event: 'BufAdd',
3533 \ pattern: '*.vim'})
3534 call assert_equal([
3535 \ #{cmd: 'echo "bufadd-vim"', group: 'TestAutoCmdFns',
3536 \ pattern: '*.vim', nested: v:false, once: v:false,
3537 \ event: 'BufAdd'}], l)
3538
3539 " Test for getting the autocmds for a pattern in a group
3540 let l = autocmd_get(#{group: 'TestAutoCmdFns', pattern: '*.vim'})
3541 call assert_equal([
3542 \ #{cmd: 'echo "bufadd-vim"', group: 'TestAutoCmdFns',
3543 \ pattern: '*.vim', nested: v:false, once: v:false,
3544 \ event: 'BufAdd'},
3545 \ #{cmd: 'echo "bufhidden"', group: 'TestAutoCmdFns',
3546 \ pattern: '*.vim', nested: v:false,
3547 \ once: v:false, event: 'BufHidden'}], l)
3548
3549 " Test for getting the autocmds for a pattern in all the groups
3550 let l = autocmd_get(#{pattern: '*.a1b2c3'})
3551 call assert_equal([{'cmd': 'echo "bufadd-vim-2"', 'group': 'TestAutoCmdFns2',
3552 \ 'pattern': '*.a1b2c3', 'nested': v:false, 'once': v:false,
3553 \ 'event': 'BufRead'}], l)
3554
3555 " Test for getting autocmds for a pattern without any autocmds
3556 call assert_equal([], autocmd_get(#{group: 'TestAutoCmdFns',
3557 \ pattern: '*.abc'}))
3558 call assert_equal([], autocmd_get(#{group: 'TestAutoCmdFns',
3559 \ event: 'BufAdd', pattern: '*.abc'}))
3560 call assert_equal([], autocmd_get(#{group: 'TestAutoCmdFns',
3561 \ event: 'BufWipeout'}))
3562 call assert_fails("call autocmd_get(#{group: 'abc', event: 'BufAdd'})",
3563 \ 'E367:')
3564 let cmd = "echo autocmd_get(#{group: 'TestAutoCmdFns', event: 'abc'})"
3565 call assert_fails(cmd, 'E216:')
3566 call assert_fails("call autocmd_get(#{group: 'abc'})", 'E367:')
3567 call assert_fails("echo autocmd_get(#{event: 'abc'})", 'E216:')
3568
3569 augroup TestAutoCmdFns
3570 au!
3571 augroup END
3572 call assert_equal([], autocmd_get(#{group: 'TestAutoCmdFns'}))
3573
3574 " Test for nested and once autocmds
3575 augroup TestAutoCmdFns
3576 au!
3577 autocmd VimSuspend * ++nested echo "suspend"
3578 autocmd VimResume * ++once echo "resume"
3579 augroup END
3580
3581 let expected = [
3582 \ {'cmd': 'echo "suspend"', 'group': 'TestAutoCmdFns', 'pattern': '*',
3583 \ 'nested': v:true, 'once': v:false, 'event': 'VimSuspend'},
3584 \ {'cmd': 'echo "resume"', 'group': 'TestAutoCmdFns', 'pattern': '*',
3585 \ 'nested': v:false, 'once': v:true, 'event': 'VimResume'}]
3586 call assert_equal(expected, autocmd_get(#{group: 'TestAutoCmdFns'}))
3587
3588 " Test for buffer-local autocmd
3589 augroup TestAutoCmdFns
3590 au!
3591 autocmd TextYankPost <buffer> echo "textyankpost"
3592 augroup END
3593
3594 let expected = [
3595 \ {'cmd': 'echo "textyankpost"', 'group': 'TestAutoCmdFns',
3596 \ 'pattern': '<buffer=' .. bufnr() .. '>', 'nested': v:false,
3597 \ 'once': v:false, 'bufnr': bufnr(), 'event': 'TextYankPost'}]
3598 call assert_equal(expected, autocmd_get(#{group: 'TestAutoCmdFns'}))
3599
3600 augroup TestAutoCmdFns
3601 au!
3602 augroup END
3603 augroup! TestAutoCmdFns
3604 augroup TestAutoCmdFns2
3605 au!
3606 augroup END
3607 augroup! TestAutoCmdFns2
3608
3609 call assert_fails("echo autocmd_get(#{group: []})", 'E730:')
3610 call assert_fails("echo autocmd_get(#{event: {}})", 'E731:')
3611 call assert_fails("echo autocmd_get([])", 'E1206:')
3612endfunc
3613
3614" Test for the autocmd_add() function
3615func Test_autocmd_add()
3616 " Define a single autocmd in a group
3617 call autocmd_add([#{group: 'TestAcSet', event: 'BufAdd', pattern: '*.sh',
3618 \ cmd: 'echo "bufadd"', once: v:true, nested: v:true}])
3619 call assert_equal([#{cmd: 'echo "bufadd"', group: 'TestAcSet',
3620 \ pattern: '*.sh', nested: v:true, once: v:true,
3621 \ event: 'BufAdd'}], autocmd_get(#{group: 'TestAcSet'}))
3622
3623 " Define two autocmds in the same group
3624 call autocmd_delete([#{group: 'TestAcSet'}])
3625 call autocmd_add([#{group: 'TestAcSet', event: 'BufAdd', pattern: '*.sh',
3626 \ cmd: 'echo "bufadd"'},
3627 \ #{group: 'TestAcSet', event: 'BufEnter', pattern: '*.sh',
3628 \ cmd: 'echo "bufenter"'}])
3629 call assert_equal([
3630 \ #{cmd: 'echo "bufadd"', group: 'TestAcSet', pattern: '*.sh',
3631 \ nested: v:false, once: v:false, event: 'BufAdd'},
3632 \ #{cmd: 'echo "bufenter"', group: 'TestAcSet', pattern: '*.sh',
3633 \ nested: v:false, once: v:false, event: 'BufEnter'}],
3634 \ autocmd_get(#{group: 'TestAcSet'}))
3635
3636 " Define a buffer-local autocmd
3637 call autocmd_delete([#{group: 'TestAcSet'}])
3638 call autocmd_add([#{group: 'TestAcSet', event: 'CursorHold',
3639 \ bufnr: bufnr(), cmd: 'echo "cursorhold"'}])
3640 call assert_equal([
3641 \ #{cmd: 'echo "cursorhold"', group: 'TestAcSet',
3642 \ pattern: '<buffer=' .. bufnr() .. '>', nested: v:false,
3643 \ once: v:false, bufnr: bufnr(), event: 'CursorHold'}],
3644 \ autocmd_get(#{group: 'TestAcSet'}))
3645
3646 " Use an invalid buffer number
3647 call autocmd_delete([#{group: 'TestAcSet'}])
3648 call autocmd_add([#{group: 'TestAcSet', event: 'BufEnter',
3649 \ bufnr: -1, cmd: 'echo "bufenter"'}])
3650 let l = [#{group: 'TestAcSet', event: 'BufAdd', bufnr: 9999,
3651 \ cmd: 'echo "bufadd"'}]
3652 call assert_fails("echo autocmd_add(l)", 'E680:')
Yegappan Lakshmanan00e977c2022-06-01 12:31:53 +01003653 let l = [#{group: 'TestAcSet', event: 'BufAdd', bufnr: 9999,
3654 \ pattern: '*.py', cmd: 'echo "bufadd"'}]
3655 call assert_fails("echo autocmd_add(l)", 'E680:')
3656 let l = [#{group: 'TestAcSet', event: 'BufAdd', bufnr: 9999,
3657 \ pattern: ['*.py', '*.c'], cmd: 'echo "bufadd"'}]
3658 call assert_fails("echo autocmd_add(l)", 'E680:')
Yegappan Lakshmanan1755a912022-05-19 10:31:47 +01003659 let l = [#{group: 'TestAcSet', event: 'BufRead', bufnr: [],
3660 \ cmd: 'echo "bufread"'}]
3661 call assert_fails("echo autocmd_add(l)", 'E745:')
3662 call assert_equal([], autocmd_get(#{group: 'TestAcSet'}))
3663
3664 " Add two commands to the same group, event and pattern
3665 call autocmd_delete([#{group: 'TestAcSet'}])
3666 call autocmd_add([#{group: 'TestAcSet', event: 'BufUnload',
3667 \ pattern: 'abc', cmd: 'echo "cmd1"'}])
3668 call autocmd_add([#{group: 'TestAcSet', event: 'BufUnload',
3669 \ pattern: 'abc', cmd: 'echo "cmd2"'}])
3670 call assert_equal([
3671 \ #{cmd: 'echo "cmd1"', group: 'TestAcSet', pattern: 'abc',
3672 \ nested: v:false, once: v:false, event: 'BufUnload'},
3673 \ #{cmd: 'echo "cmd2"', group: 'TestAcSet', pattern: 'abc',
3674 \ nested: v:false, once: v:false, event: 'BufUnload'}],
3675 \ autocmd_get(#{group: 'TestAcSet'}))
3676
3677 " When adding a new autocmd, if the autocmd 'group' is not specified, then
3678 " the current autocmd group should be used.
3679 call autocmd_delete([#{group: 'TestAcSet'}])
3680 augroup TestAcSet
3681 call autocmd_add([#{event: 'BufHidden', pattern: 'abc', cmd: 'echo "abc"'}])
3682 augroup END
3683 call assert_equal([
3684 \ #{cmd: 'echo "abc"', group: 'TestAcSet', pattern: 'abc',
3685 \ nested: v:false, once: v:false, event: 'BufHidden'}],
3686 \ autocmd_get(#{group: 'TestAcSet'}))
3687
Yegappan Lakshmanan971f6822022-05-24 11:40:11 +01003688 " Test for replacing a cmd for an event in a group
3689 call autocmd_delete([#{group: 'TestAcSet'}])
3690 call autocmd_add([#{replace: v:true, group: 'TestAcSet', event: 'BufEnter',
3691 \ pattern: '*.py', cmd: 'echo "bufenter"'}])
3692 call autocmd_add([#{replace: v:true, group: 'TestAcSet', event: 'BufEnter',
3693 \ pattern: '*.py', cmd: 'echo "bufenter"'}])
3694 call assert_equal([
3695 \ #{cmd: 'echo "bufenter"', group: 'TestAcSet', pattern: '*.py',
3696 \ nested: v:false, once: v:false, event: 'BufEnter'}],
3697 \ autocmd_get(#{group: 'TestAcSet'}))
3698
3699 " Test for adding a command for an unsupported autocmd event
Yegappan Lakshmanan1755a912022-05-19 10:31:47 +01003700 let l = [#{group: 'TestAcSet', event: 'abc', pattern: '*.sh',
3701 \ cmd: 'echo "bufadd"'}]
3702 call assert_fails('call autocmd_add(l)', 'E216:')
3703
Yegappan Lakshmanane0ff3a72022-05-27 18:05:33 +01003704 " Test for using a list of events and patterns
3705 call autocmd_delete([#{group: 'TestAcSet'}])
3706 let l = [#{group: 'TestAcSet', event: ['BufEnter', 'BufLeave'],
3707 \ pattern: ['*.py', '*.sh'], cmd: 'echo "bufcmds"'}]
3708 call autocmd_add(l)
3709 call assert_equal([
3710 \ #{cmd: 'echo "bufcmds"', group: 'TestAcSet', pattern: '*.py',
3711 \ nested: v:false, once: v:false, event: 'BufEnter'},
3712 \ #{cmd: 'echo "bufcmds"', group: 'TestAcSet', pattern: '*.sh',
3713 \ nested: v:false, once: v:false, event: 'BufEnter'},
3714 \ #{cmd: 'echo "bufcmds"', group: 'TestAcSet', pattern: '*.py',
3715 \ nested: v:false, once: v:false, event: 'BufLeave'},
3716 \ #{cmd: 'echo "bufcmds"', group: 'TestAcSet', pattern: '*.sh',
3717 \ nested: v:false, once: v:false, event: 'BufLeave'}],
3718 \ autocmd_get(#{group: 'TestAcSet'}))
3719
3720 " Test for invalid values for 'event' item
3721 call autocmd_delete([#{group: 'TestAcSet'}])
3722 let l = [#{group: 'TestAcSet', event: test_null_string(),
3723 \ pattern: "*.py", cmd: 'echo "bufcmds"'}]
3724 call assert_fails('call autocmd_add(l)', 'E928:')
3725 let l = [#{group: 'TestAcSet', event: test_null_list(),
3726 \ pattern: "*.py", cmd: 'echo "bufcmds"'}]
3727 call assert_fails('call autocmd_add(l)', 'E714:')
3728 let l = [#{group: 'TestAcSet', event: {},
3729 \ pattern: "*.py", cmd: 'echo "bufcmds"'}]
3730 call assert_fails('call autocmd_add(l)', 'E777:')
3731 let l = [#{group: 'TestAcSet', event: [{}],
3732 \ pattern: "*.py", cmd: 'echo "bufcmds"'}]
3733 call assert_fails('call autocmd_add(l)', 'E928:')
3734 let l = [#{group: 'TestAcSet', event: [test_null_string()],
3735 \ pattern: "*.py", cmd: 'echo "bufcmds"'}]
3736 call assert_fails('call autocmd_add(l)', 'E928:')
3737 let l = [#{group: 'TestAcSet', event: 'BufEnter,BufLeave',
3738 \ pattern: '*.py', cmd: 'echo "bufcmds"'}]
3739 call assert_fails('call autocmd_add(l)', 'E216:')
3740 let l = [#{group: 'TestAcSet', event: [],
3741 \ pattern: "*.py", cmd: 'echo "bufcmds"'}]
3742 call autocmd_add(l)
3743 let l = [#{group: 'TestAcSet', event: [""],
3744 \ pattern: "*.py", cmd: 'echo "bufcmds"'}]
3745 call assert_fails('call autocmd_add(l)', 'E216:')
3746 let l = [#{group: 'TestAcSet', event: "",
3747 \ pattern: "*.py", cmd: 'echo "bufcmds"'}]
3748 call autocmd_add(l)
3749 call assert_equal([], autocmd_get(#{group: 'TestAcSet'}))
3750
3751 " Test for invalid values for 'pattern' item
3752 let l = [#{group: 'TestAcSet', event: "BufEnter",
3753 \ pattern: test_null_string(), cmd: 'echo "bufcmds"'}]
Yegappan Lakshmanan00e977c2022-06-01 12:31:53 +01003754 call assert_fails('call autocmd_add(l)', 'E928:')
Yegappan Lakshmanane0ff3a72022-05-27 18:05:33 +01003755 let l = [#{group: 'TestAcSet', event: "BufEnter",
3756 \ pattern: test_null_list(), cmd: 'echo "bufcmds"'}]
3757 call assert_fails('call autocmd_add(l)', 'E714:')
3758 let l = [#{group: 'TestAcSet', event: "BufEnter",
3759 \ pattern: {}, cmd: 'echo "bufcmds"'}]
3760 call assert_fails('call autocmd_add(l)', 'E777:')
3761 let l = [#{group: 'TestAcSet', event: "BufEnter",
3762 \ pattern: [{}], cmd: 'echo "bufcmds"'}]
3763 call assert_fails('call autocmd_add(l)', 'E928:')
3764 let l = [#{group: 'TestAcSet', event: "BufEnter",
3765 \ pattern: [test_null_string()], cmd: 'echo "bufcmds"'}]
3766 call assert_fails('call autocmd_add(l)', 'E928:')
3767 let l = [#{group: 'TestAcSet', event: "BufEnter",
3768 \ pattern: [], cmd: 'echo "bufcmds"'}]
3769 call autocmd_add(l)
3770 let l = [#{group: 'TestAcSet', event: "BufEnter",
3771 \ pattern: [""], cmd: 'echo "bufcmds"'}]
3772 call autocmd_add(l)
3773 let l = [#{group: 'TestAcSet', event: "BufEnter",
3774 \ pattern: "", cmd: 'echo "bufcmds"'}]
3775 call autocmd_add(l)
3776 call assert_equal([], autocmd_get(#{group: 'TestAcSet'}))
3777
3778 let l = [#{group: 'TestAcSet', event: 'BufEnter,abc,BufLeave',
3779 \ pattern: '*.py', cmd: 'echo "bufcmds"'}]
3780 call assert_fails('call autocmd_add(l)', 'E216:')
3781
Yegappan Lakshmanan1755a912022-05-19 10:31:47 +01003782 call assert_fails("call autocmd_add({})", 'E1211:')
3783 call assert_equal(v:false, autocmd_add(test_null_list()))
3784 call assert_true(autocmd_add([[]]))
3785 call assert_true(autocmd_add([test_null_dict()]))
3786
3787 augroup TestAcSet
3788 au!
3789 augroup END
3790
3791 call autocmd_add([#{group: 'TestAcSet'}])
3792 call autocmd_add([#{group: 'TestAcSet', event: 'BufAdd'}])
3793 call autocmd_add([#{group: 'TestAcSet', pat: '*.sh'}])
3794 call autocmd_add([#{group: 'TestAcSet', cmd: 'echo "a"'}])
3795 call autocmd_add([#{group: 'TestAcSet', event: 'BufAdd', pat: '*.sh'}])
3796 call autocmd_add([#{group: 'TestAcSet', event: 'BufAdd', cmd: 'echo "a"'}])
3797 call autocmd_add([#{group: 'TestAcSet', pat: '*.sh', cmd: 'echo "a"'}])
3798 call assert_equal([], autocmd_get(#{group: 'TestAcSet'}))
3799
3800 augroup! TestAcSet
3801endfunc
3802
3803" Test for deleting autocmd events and groups
3804func Test_autocmd_delete()
3805 " Delete an event in an autocmd group
3806 augroup TestAcSet
3807 au!
3808 au BufAdd *.sh echo "bufadd"
3809 au BufEnter *.sh echo "bufenter"
3810 augroup END
3811 call autocmd_delete([#{group: 'TestAcSet', event: 'BufAdd'}])
3812 call assert_equal([#{cmd: 'echo "bufenter"', group: 'TestAcSet',
3813 \ pattern: '*.sh', nested: v:false, once: v:false,
3814 \ event: 'BufEnter'}], autocmd_get(#{group: 'TestAcSet'}))
3815
3816 " Delete all the events in an autocmd group
3817 augroup TestAcSet
3818 au BufAdd *.sh echo "bufadd"
3819 augroup END
3820 call autocmd_delete([#{group: 'TestAcSet', event: '*'}])
3821 call assert_equal([], autocmd_get(#{group: 'TestAcSet'}))
3822
3823 " Delete a non-existing autocmd group
3824 call assert_fails("call autocmd_delete([#{group: 'abc'}])", 'E367:')
3825 " Delete a non-existing autocmd event
3826 let l = [#{group: 'TestAcSet', event: 'abc'}]
3827 call assert_fails("call autocmd_delete(l)", 'E216:')
3828 " Delete a non-existing autocmd pattern
3829 let l = [#{group: 'TestAcSet', event: 'BufAdd', pat: 'abc'}]
3830 call assert_true(autocmd_delete(l))
Yegappan Lakshmanan00e977c2022-06-01 12:31:53 +01003831 " Delete an autocmd for a non-existing buffer
3832 let l = [#{event: '*', bufnr: 9999, cmd: 'echo "x"'}]
3833 call assert_fails('call autocmd_delete(l)', 'E680:')
Yegappan Lakshmanan1755a912022-05-19 10:31:47 +01003834
3835 " Delete an autocmd group
3836 augroup TestAcSet
3837 au!
3838 au BufAdd *.sh echo "bufadd"
3839 au BufEnter *.sh echo "bufenter"
3840 augroup END
3841 call autocmd_delete([#{group: 'TestAcSet'}])
3842 call assert_fails("call autocmd_get(#{group: 'TestAcSet'})", 'E367:')
3843
3844 call assert_true(autocmd_delete([[]]))
3845 call assert_true(autocmd_delete([test_null_dict()]))
3846endfunc
3847
Bram Moolenaarbc2b71d2020-02-17 21:33:30 +01003848" vim: shiftwidth=2 sts=2 expandtab