blob: e4b76dba57c09ceb112d9f9eeaadd0a060d87f0d [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
Bram Moolenaaraace2152017-11-05 16:23:10 +01001925func SetChangeMarks(start, end)
Bram Moolenaar97c69432021-01-15 16:45:21 +01001926 exe a:start .. 'mark ['
1927 exe a:end .. 'mark ]'
Bram Moolenaaraace2152017-11-05 16:23:10 +01001928endfunc
1929
1930" Verify the effects of autocmds on '[ and ']
1931func Test_change_mark_in_autocmds()
1932 edit! Xtest
Bram Moolenaar97c69432021-01-15 16:45:21 +01001933 call feedkeys("ia\<CR>b\<CR>c\<CR>d\<C-g>u\<Esc>", 'xtn')
Bram Moolenaaraace2152017-11-05 16:23:10 +01001934
1935 call SetChangeMarks(2, 3)
1936 write
1937 call assert_equal([1, 4], [line("'["), line("']")])
1938
1939 call SetChangeMarks(2, 3)
1940 au BufWritePre * call assert_equal([1, 4], [line("'["), line("']")])
1941 write
1942 au! BufWritePre
1943
Bram Moolenaar14ddd222020-08-05 12:02:40 +02001944 if has('unix')
Bram Moolenaaraace2152017-11-05 16:23:10 +01001945 write XtestFilter
1946 write >> XtestFilter
1947
1948 call SetChangeMarks(2, 3)
1949 " Marks are set to the entire range of the write
1950 au FilterWritePre * call assert_equal([1, 4], [line("'["), line("']")])
1951 " '[ is adjusted to just before the line that will receive the filtered
1952 " data
1953 au FilterReadPre * call assert_equal([4, 4], [line("'["), line("']")])
1954 " The filtered data is read into the buffer, and the source lines are
1955 " still present, so the range is after the source lines
1956 au FilterReadPost * call assert_equal([5, 12], [line("'["), line("']")])
1957 %!cat XtestFilter
1958 " After the filtered data is read, the original lines are deleted
1959 call assert_equal([1, 8], [line("'["), line("']")])
1960 au! FilterWritePre,FilterReadPre,FilterReadPost
1961 undo
1962
1963 call SetChangeMarks(1, 4)
1964 au FilterWritePre * call assert_equal([2, 3], [line("'["), line("']")])
1965 au FilterReadPre * call assert_equal([3, 3], [line("'["), line("']")])
1966 au FilterReadPost * call assert_equal([4, 11], [line("'["), line("']")])
1967 2,3!cat XtestFilter
1968 call assert_equal([2, 9], [line("'["), line("']")])
1969 au! FilterWritePre,FilterReadPre,FilterReadPost
1970 undo
1971
1972 call delete('XtestFilter')
1973 endif
1974
1975 call SetChangeMarks(1, 4)
1976 au FileWritePre * call assert_equal([2, 3], [line("'["), line("']")])
1977 2,3write Xtest2
1978 au! FileWritePre
1979
1980 call SetChangeMarks(2, 3)
1981 au FileAppendPre * call assert_equal([1, 4], [line("'["), line("']")])
1982 write >> Xtest2
1983 au! FileAppendPre
1984
1985 call SetChangeMarks(1, 4)
1986 au FileAppendPre * call assert_equal([2, 3], [line("'["), line("']")])
1987 2,3write >> Xtest2
1988 au! FileAppendPre
1989
1990 call SetChangeMarks(1, 1)
1991 au FileReadPre * call assert_equal([3, 1], [line("'["), line("']")])
1992 au FileReadPost * call assert_equal([4, 11], [line("'["), line("']")])
1993 3read Xtest2
1994 au! FileReadPre,FileReadPost
1995 undo
1996
1997 call SetChangeMarks(4, 4)
1998 " When the line is 0, it's adjusted to 1
1999 au FileReadPre * call assert_equal([1, 4], [line("'["), line("']")])
2000 au FileReadPost * call assert_equal([1, 8], [line("'["), line("']")])
2001 0read Xtest2
2002 au! FileReadPre,FileReadPost
2003 undo
2004
2005 call SetChangeMarks(4, 4)
2006 " When the line is 0, it's adjusted to 1
2007 au FileReadPre * call assert_equal([1, 4], [line("'["), line("']")])
2008 au FileReadPost * call assert_equal([2, 9], [line("'["), line("']")])
2009 1read Xtest2
2010 au! FileReadPre,FileReadPost
2011 undo
2012
2013 bwipe!
2014 call delete('Xtest')
2015 call delete('Xtest2')
2016endfunc
2017
2018func Test_Filter_noshelltemp()
Bram Moolenaaraeb313f2020-11-27 19:13:28 +01002019 CheckExecutable cat
Bram Moolenaaraace2152017-11-05 16:23:10 +01002020
2021 enew!
2022 call setline(1, ['a', 'b', 'c', 'd'])
2023
2024 let shelltemp = &shelltemp
2025 set shelltemp
2026
2027 let g:filter_au = 0
2028 au FilterWritePre * let g:filter_au += 1
2029 au FilterReadPre * let g:filter_au += 1
2030 au FilterReadPost * let g:filter_au += 1
2031 %!cat
2032 call assert_equal(3, g:filter_au)
2033
2034 if has('filterpipe')
2035 set noshelltemp
2036
2037 let g:filter_au = 0
2038 au FilterWritePre * let g:filter_au += 1
2039 au FilterReadPre * let g:filter_au += 1
2040 au FilterReadPost * let g:filter_au += 1
2041 %!cat
2042 call assert_equal(0, g:filter_au)
2043 endif
2044
2045 au! FilterWritePre,FilterReadPre,FilterReadPost
2046 let &shelltemp = shelltemp
2047 bwipe!
2048endfunc
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01002049
2050func Test_TextYankPost()
2051 enew!
2052 call setline(1, ['foo'])
2053
2054 let g:event = []
2055 au TextYankPost * let g:event = copy(v:event)
2056
2057 call assert_equal({}, v:event)
2058 call assert_fails('let v:event = {}', 'E46:')
2059 call assert_fails('let v:event.mykey = 0', 'E742:')
2060
2061 norm "ayiw
2062 call assert_equal(
Bram Moolenaara016eeb2022-04-09 11:37:38 +01002063 \ #{regcontents: ['foo'], regname: 'a', operator: 'y',
2064 \ regtype: 'v', visual: v:false, inclusive: v:true},
2065 \ g:event)
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01002066 norm y_
2067 call assert_equal(
Bram Moolenaara016eeb2022-04-09 11:37:38 +01002068 \ #{regcontents: ['foo'], regname: '', operator: 'y', regtype: 'V',
2069 \ visual: v:false, inclusive: v:false},
2070 \ g:event)
Bram Moolenaar37d16732020-06-12 22:09:01 +02002071 norm Vy
2072 call assert_equal(
Bram Moolenaara016eeb2022-04-09 11:37:38 +01002073 \ #{regcontents: ['foo'], regname: '', operator: 'y', regtype: 'V',
2074 \ visual: v:true, inclusive: v:true},
2075 \ g:event)
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01002076 call feedkeys("\<C-V>y", 'x')
2077 call assert_equal(
Bram Moolenaara016eeb2022-04-09 11:37:38 +01002078 \ #{regcontents: ['f'], regname: '', operator: 'y', regtype: "\x161",
2079 \ visual: v:true, inclusive: v:true},
2080 \ g:event)
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01002081 norm "xciwbar
2082 call assert_equal(
Bram Moolenaara016eeb2022-04-09 11:37:38 +01002083 \ #{regcontents: ['foo'], regname: 'x', operator: 'c', regtype: 'v',
2084 \ visual: v:false, inclusive: v:true},
2085 \ g:event)
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01002086 norm "bdiw
2087 call assert_equal(
Bram Moolenaara016eeb2022-04-09 11:37:38 +01002088 \ #{regcontents: ['bar'], regname: 'b', operator: 'd', regtype: 'v',
2089 \ visual: v:false, inclusive: v:true},
2090 \ g:event)
2091
2092 call setline(1, 'foobar')
2093 " exclusive motion
2094 norm $"ay0
2095 call assert_equal(
2096 \ #{regcontents: ['fooba'], regname: 'a', operator: 'y', regtype: 'v',
2097 \ visual: v:false, inclusive: v:false},
2098 \ g:event)
2099 " inclusive motion
2100 norm 0"ay$
2101 call assert_equal(
2102 \ #{regcontents: ['foobar'], regname: 'a', operator: 'y', regtype: 'v',
2103 \ visual: v:false, inclusive: v:true},
2104 \ g:event)
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01002105
2106 call assert_equal({}, v:event)
2107
Bram Moolenaarfccbf062020-11-26 20:34:00 +01002108 if has('clipboard_working') && !has('gui_running')
2109 " Test that when the visual selection is automatically copied to clipboard
2110 " register a TextYankPost is emitted
2111 call setline(1, ['foobar'])
2112
2113 let @* = ''
2114 set clipboard=autoselect
2115 exe "norm! ggviw\<Esc>"
2116 call assert_equal(
Bram Moolenaara016eeb2022-04-09 11:37:38 +01002117 \ #{regcontents: ['foobar'], regname: '*', operator: 'y',
2118 \ regtype: 'v', visual: v:true, inclusive: v:false},
2119 \ g:event)
Bram Moolenaarfccbf062020-11-26 20:34:00 +01002120
2121 let @+ = ''
2122 set clipboard=autoselectplus
2123 exe "norm! ggviw\<Esc>"
2124 call assert_equal(
Bram Moolenaara016eeb2022-04-09 11:37:38 +01002125 \ #{regcontents: ['foobar'], regname: '+', operator: 'y',
2126 \ regtype: 'v', visual: v:true, inclusive: v:false},
2127 \ g:event)
Bram Moolenaarfccbf062020-11-26 20:34:00 +01002128
2129 set clipboard&vim
2130 endif
2131
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01002132 au! TextYankPost
2133 unlet g:event
2134 bwipe!
2135endfunc
Bram Moolenaar9bca8052017-12-18 12:37:55 +01002136
Bram Moolenaar9a046fd2021-01-28 13:47:59 +01002137func Test_autocommand_all_events()
2138 call assert_fails('au * * bwipe', 'E1155:')
2139 call assert_fails('au * x bwipe', 'E1155:')
Bram Moolenaarb6db1462021-12-24 19:24:47 +00002140 call assert_fails('au! * x bwipe', 'E1155:')
Bram Moolenaar4fb921e2017-12-18 15:33:00 +01002141endfunc
Bram Moolenaarb7407d32018-02-03 17:36:27 +01002142
Bram Moolenaarf6246f52022-02-11 16:30:12 +00002143func Test_autocmd_user()
2144 au User MyEvent let s:res = [expand("<afile>"), expand("<amatch>")]
2145 doautocmd User MyEvent
2146 call assert_equal(['MyEvent', 'MyEvent'], s:res)
2147 au! User
2148 unlet s:res
2149endfunc
2150
Bram Moolenaarb7407d32018-02-03 17:36:27 +01002151function s:Before_test_dirchanged()
2152 augroup test_dirchanged
2153 autocmd!
2154 augroup END
2155 let s:li = []
2156 let s:dir_this = getcwd()
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02002157 let s:dir_foo = s:dir_this . '/Xfoo'
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01002158 call mkdir(s:dir_foo)
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02002159 let s:dir_bar = s:dir_this . '/Xbar'
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01002160 call mkdir(s:dir_bar)
Bram Moolenaarb7407d32018-02-03 17:36:27 +01002161endfunc
2162
2163function s:After_test_dirchanged()
Bram Moolenaar3503d7c2019-11-09 20:10:17 +01002164 call chdir(s:dir_this)
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01002165 call delete(s:dir_foo, 'd')
2166 call delete(s:dir_bar, 'd')
Bram Moolenaarb7407d32018-02-03 17:36:27 +01002167 augroup test_dirchanged
2168 autocmd!
2169 augroup END
2170endfunc
2171
2172function Test_dirchanged_global()
2173 call s:Before_test_dirchanged()
Bram Moolenaarf6246f52022-02-11 16:30:12 +00002174 autocmd test_dirchanged DirChangedPre global call add(s:li, expand("<amatch>") .. " pre cd " .. v:event.directory)
Bram Moolenaarb7407d32018-02-03 17:36:27 +01002175 autocmd test_dirchanged DirChanged global call add(s:li, "cd:")
2176 autocmd test_dirchanged DirChanged global call add(s:li, expand("<afile>"))
Bram Moolenaar3503d7c2019-11-09 20:10:17 +01002177 call chdir(s:dir_foo)
Bram Moolenaarf6246f52022-02-11 16:30:12 +00002178 let expected = ["global pre cd " .. s:dir_foo, "cd:", s:dir_foo]
Bram Moolenaar28e8f732022-02-09 12:58:20 +00002179 call assert_equal(expected, s:li)
Bram Moolenaar3503d7c2019-11-09 20:10:17 +01002180 call chdir(s:dir_foo)
Bram Moolenaar28e8f732022-02-09 12:58:20 +00002181 call assert_equal(expected, s:li)
Bram Moolenaar3503d7c2019-11-09 20:10:17 +01002182 exe 'lcd ' .. fnameescape(s:dir_bar)
Bram Moolenaar28e8f732022-02-09 12:58:20 +00002183 call assert_equal(expected, s:li)
Bram Moolenaard8c9d322022-06-12 11:49:16 +01002184
2185 exe 'cd ' .. s:dir_foo
2186 exe 'cd ' .. s:dir_bar
2187 autocmd! test_dirchanged DirChanged global let g:result = expand("<afile>")
2188 cd -
Bram Moolenaardb77c492022-06-12 23:26:50 +01002189 call assert_equal(s:dir_foo, substitute(g:result, '\\', '/', 'g'))
Bram Moolenaard8c9d322022-06-12 11:49:16 +01002190
Bram Moolenaarb7407d32018-02-03 17:36:27 +01002191 call s:After_test_dirchanged()
2192endfunc
2193
2194function Test_dirchanged_local()
2195 call s:Before_test_dirchanged()
2196 autocmd test_dirchanged DirChanged window call add(s:li, "lcd:")
2197 autocmd test_dirchanged DirChanged window call add(s:li, expand("<afile>"))
Bram Moolenaar3503d7c2019-11-09 20:10:17 +01002198 call chdir(s:dir_foo)
Bram Moolenaarb7407d32018-02-03 17:36:27 +01002199 call assert_equal([], s:li)
Bram Moolenaar3503d7c2019-11-09 20:10:17 +01002200 exe 'lcd ' .. fnameescape(s:dir_bar)
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01002201 call assert_equal(["lcd:", s:dir_bar], s:li)
Bram Moolenaar3503d7c2019-11-09 20:10:17 +01002202 exe 'lcd ' .. fnameescape(s:dir_bar)
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01002203 call assert_equal(["lcd:", s:dir_bar], s:li)
Bram Moolenaarb7407d32018-02-03 17:36:27 +01002204 call s:After_test_dirchanged()
2205endfunc
2206
2207function Test_dirchanged_auto()
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +02002208 CheckOption autochdir
Bram Moolenaarb7407d32018-02-03 17:36:27 +01002209 call s:Before_test_dirchanged()
2210 call test_autochdir()
Bram Moolenaar28e8f732022-02-09 12:58:20 +00002211 autocmd test_dirchanged DirChangedPre auto call add(s:li, "pre cd " .. v:event.directory)
Bram Moolenaarb7407d32018-02-03 17:36:27 +01002212 autocmd test_dirchanged DirChanged auto call add(s:li, "auto:")
2213 autocmd test_dirchanged DirChanged auto call add(s:li, expand("<afile>"))
2214 set acd
Bram Moolenaar3503d7c2019-11-09 20:10:17 +01002215 cd ..
Bram Moolenaarb7407d32018-02-03 17:36:27 +01002216 call assert_equal([], s:li)
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01002217 exe 'edit ' . s:dir_foo . '/Xfile'
2218 call assert_equal(s:dir_foo, getcwd())
Bram Moolenaar28e8f732022-02-09 12:58:20 +00002219 let expected = ["pre cd " .. s:dir_foo, "auto:", s:dir_foo]
2220 call assert_equal(expected, s:li)
Bram Moolenaarb7407d32018-02-03 17:36:27 +01002221 set noacd
2222 bwipe!
2223 call s:After_test_dirchanged()
2224endfunc
Bram Moolenaar5a093432018-02-10 18:15:19 +01002225
2226" Test TextChangedI and TextChangedP
2227func Test_ChangedP()
2228 new
2229 call setline(1, ['foo', 'bar', 'foobar'])
2230 call test_override("char_avail", 1)
2231 set complete=. completeopt=menuone
2232
2233 func! TextChangedAutocmd(char)
2234 let g:autocmd .= a:char
2235 endfunc
2236
Christian Brabandtdb3b4462021-10-16 11:58:55 +01002237 " TextChanged will not be triggered, only check that it isn't.
Bram Moolenaar5a093432018-02-10 18:15:19 +01002238 au! TextChanged <buffer> :call TextChangedAutocmd('N')
2239 au! TextChangedI <buffer> :call TextChangedAutocmd('I')
2240 au! TextChangedP <buffer> :call TextChangedAutocmd('P')
2241
2242 call cursor(3, 1)
2243 let g:autocmd = ''
2244 call feedkeys("o\<esc>", 'tnix')
2245 call assert_equal('I', g:autocmd)
2246
2247 let g:autocmd = ''
2248 call feedkeys("Sf", 'tnix')
2249 call assert_equal('II', g:autocmd)
2250
2251 let g:autocmd = ''
2252 call feedkeys("Sf\<C-N>", 'tnix')
2253 call assert_equal('IIP', g:autocmd)
2254
2255 let g:autocmd = ''
2256 call feedkeys("Sf\<C-N>\<C-N>", 'tnix')
2257 call assert_equal('IIPP', g:autocmd)
2258
2259 let g:autocmd = ''
2260 call feedkeys("Sf\<C-N>\<C-N>\<C-N>", 'tnix')
2261 call assert_equal('IIPPP', g:autocmd)
2262
2263 let g:autocmd = ''
2264 call feedkeys("Sf\<C-N>\<C-N>\<C-N>\<C-N>", 'tnix')
2265 call assert_equal('IIPPPP', g:autocmd)
2266
2267 call assert_equal(['foo', 'bar', 'foobar', 'foo'], getline(1, '$'))
2268 " TODO: how should it handle completeopt=noinsert,noselect?
2269
2270 " CleanUp
2271 call test_override("char_avail", 0)
2272 au! TextChanged
2273 au! TextChangedI
2274 au! TextChangedP
2275 delfu TextChangedAutocmd
2276 unlet! g:autocmd
2277 set complete&vim completeopt&vim
2278
2279 bw!
2280endfunc
Bram Moolenaar8c64a362018-03-23 22:39:31 +01002281
Bram Moolenaar91d2e782018-08-07 19:05:01 +02002282let g:setline_handled = v:false
Bram Moolenaar1e115362019-01-09 23:01:02 +01002283func SetLineOne()
Bram Moolenaar91d2e782018-08-07 19:05:01 +02002284 if !g:setline_handled
2285 call setline(1, "(x)")
2286 let g:setline_handled = v:true
2287 endif
2288endfunc
2289
2290func Test_TextChangedI_with_setline()
2291 new
2292 call test_override('char_avail', 1)
2293 autocmd TextChangedI <buffer> call SetLineOne()
2294 call feedkeys("i(\<CR>\<Esc>", 'tx')
2295 call assert_equal('(', getline(1))
2296 call assert_equal('x)', getline(2))
2297 undo
Bram Moolenaar91d2e782018-08-07 19:05:01 +02002298 call assert_equal('', getline(1))
Bram Moolenaar9fa95062018-08-08 22:08:32 +02002299 call assert_equal('', getline(2))
Bram Moolenaar91d2e782018-08-07 19:05:01 +02002300
Bram Moolenaarca34db32022-01-20 11:17:18 +00002301 call test_override('char_avail', 0)
Bram Moolenaar91d2e782018-08-07 19:05:01 +02002302 bwipe!
2303endfunc
2304
Bram Moolenaar8c64a362018-03-23 22:39:31 +01002305func Test_Changed_FirstTime()
Bram Moolenaar8c5a2782019-08-07 23:07:07 +02002306 CheckFeature terminal
2307 CheckNotGui
Bram Moolenaar3cdcb092020-03-18 19:18:10 +01002308 " Starting a terminal to run Vim is always considered flaky.
Bram Moolenaar30d53e22020-03-18 21:10:44 +01002309 let g:test_is_flaky = 1
Bram Moolenaar8c5a2782019-08-07 23:07:07 +02002310
Bram Moolenaar8c64a362018-03-23 22:39:31 +01002311 " Prepare file for TextChanged event.
2312 call writefile([''], 'Xchanged.txt')
2313 let buf = term_start([GetVimProg(), '--clean', '-c', 'set noswapfile'], {'term_rows': 3})
2314 call assert_equal('running', term_getstatus(buf))
Bram Moolenaar1834d372018-03-29 17:40:46 +02002315 " Wait for the ruler (in the status line) to be shown.
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01002316 " In ConPTY, there is additional character which is drawn up to the width of
2317 " the screen.
2318 if has('conpty')
2319 call WaitForAssert({-> assert_match('\<All.*$', term_getline(buf, 3))})
2320 else
2321 call WaitForAssert({-> assert_match('\<All$', term_getline(buf, 3))})
2322 endif
Bram Moolenaar8c64a362018-03-23 22:39:31 +01002323 " It's only adding autocmd, so that no event occurs.
2324 call term_sendkeys(buf, ":au! TextChanged <buffer> call writefile(['No'], 'Xchanged.txt')\<cr>")
2325 call term_sendkeys(buf, "\<C-\\>\<C-N>:qa!\<cr>")
Bram Moolenaar50182fa2018-04-28 21:34:40 +02002326 call WaitForAssert({-> assert_equal('finished', term_getstatus(buf))})
Bram Moolenaar8c64a362018-03-23 22:39:31 +01002327 call assert_equal([''], readfile('Xchanged.txt'))
2328
2329 " clean up
2330 call delete('Xchanged.txt')
2331 bwipe!
2332endfunc
Bram Moolenaar0566e892019-01-24 19:37:40 +01002333
Bram Moolenaareb93f3f2019-04-04 15:04:56 +02002334func Test_autocmd_nested()
2335 let g:did_nested = 0
2336 augroup Testing
2337 au WinNew * edit somefile
2338 au BufNew * let g:did_nested = 1
2339 augroup END
2340 split
2341 call assert_equal(0, g:did_nested)
2342 close
2343 bwipe! somefile
2344
2345 " old nested argument still works
2346 augroup Testing
2347 au!
2348 au WinNew * nested edit somefile
2349 au BufNew * let g:did_nested = 1
2350 augroup END
2351 split
2352 call assert_equal(1, g:did_nested)
2353 close
2354 bwipe! somefile
2355
2356 " New ++nested argument works
2357 augroup Testing
2358 au!
2359 au WinNew * ++nested edit somefile
2360 au BufNew * let g:did_nested = 1
2361 augroup END
2362 split
2363 call assert_equal(1, g:did_nested)
2364 close
2365 bwipe! somefile
2366
Bram Moolenaarf0775142022-03-04 20:10:38 +00002367 " nested without ++ does not work in Vim9 script
2368 call assert_fails('vim9cmd au WinNew * nested echo fails', 'E1078:')
2369
Bram Moolenaareb93f3f2019-04-04 15:04:56 +02002370 augroup Testing
2371 au!
2372 augroup END
2373
2374 call assert_fails('au WinNew * ++nested ++nested echo bad', 'E983:')
2375 call assert_fails('au WinNew * nested nested echo bad', 'E983:')
2376endfunc
2377
Bram Moolenaar5fa9f232022-07-23 09:06:48 +01002378func Test_autocmd_nested_cursor_invalid()
2379 set laststatus=0
2380 copen
2381 cclose
2382 call setline(1, ['foo', 'bar', 'baz'])
2383 3
2384 augroup nested_inv
2385 autocmd User foo ++nested copen
2386 autocmd BufAdd * let &laststatus = 2 - &laststatus
2387 augroup END
2388 doautocmd User foo
2389
2390 augroup nested_inv
2391 au!
2392 augroup END
2393 set laststatus&
Bram Moolenaarb03950f2022-07-26 13:47:13 +01002394 cclose
Bram Moolenaar5fa9f232022-07-23 09:06:48 +01002395 bwipe!
2396endfunc
2397
Bram Moolenaar3d6ee8b2022-07-27 15:23:35 +01002398func Test_autocmd_nested_keeps_cursor_pos()
2399 enew
2400 call setline(1, 'foo')
2401 autocmd User foo ++nested normal! $a
2402 autocmd InsertLeave * :
2403 doautocmd User foo
2404 call assert_equal([0, 1, 3, 0], getpos('.'))
2405
2406 bwipe!
2407endfunc
2408
Bram Moolenaarb03950f2022-07-26 13:47:13 +01002409func Test_autocmd_nested_switch_window()
2410 " run this in a separate Vim so that SafeState works
2411 CheckRunVimInTerminal
2412
2413 let lines =<< trim END
2414 vim9script
2415 ['()']->writefile('Xautofile')
2416 autocmd VimEnter * ++nested edit Xautofile | split
2417 autocmd BufReadPost * autocmd SafeState * ++once foldclosed('.')
2418 autocmd WinEnter * matchadd('ErrorMsg', 'pat')
2419 END
2420 call writefile(lines, 'Xautoscript')
2421 let buf = RunVimInTerminal('-S Xautoscript', {'rows': 10})
2422 call VerifyScreenDump(buf, 'Test_autocmd_nested_switch', {})
2423
2424 call StopVimInTerminal(buf)
2425 call delete('Xautofile')
2426 call delete('Xautoscript')
2427endfunc
2428
Bram Moolenaareb93f3f2019-04-04 15:04:56 +02002429func Test_autocmd_once()
2430 " Without ++once WinNew triggers twice
2431 let g:did_split = 0
2432 augroup Testing
2433 au WinNew * let g:did_split += 1
2434 augroup END
2435 split
2436 split
2437 call assert_equal(2, g:did_split)
2438 call assert_true(exists('#WinNew'))
2439 close
2440 close
2441
2442 " With ++once WinNew triggers once
2443 let g:did_split = 0
2444 augroup Testing
2445 au!
2446 au WinNew * ++once let g:did_split += 1
2447 augroup END
2448 split
2449 split
2450 call assert_equal(1, g:did_split)
2451 call assert_false(exists('#WinNew'))
2452 close
2453 close
2454
2455 call assert_fails('au WinNew * ++once ++once echo bad', 'E983:')
2456endfunc
2457
Bram Moolenaara68e5952019-04-25 22:22:01 +02002458func Test_autocmd_bufreadpre()
2459 new
2460 let b:bufreadpre = 1
Bram Moolenaarab505b12020-03-23 19:28:44 +01002461 call append(0, range(1000))
Bram Moolenaara68e5952019-04-25 22:22:01 +02002462 w! XAutocmdBufReadPre.txt
2463 autocmd BufReadPre <buffer> :let b:bufreadpre += 1
Bram Moolenaarab505b12020-03-23 19:28:44 +01002464 norm! 500gg
Bram Moolenaara68e5952019-04-25 22:22:01 +02002465 sp
Bram Moolenaarab505b12020-03-23 19:28:44 +01002466 norm! 1000gg
Bram Moolenaara68e5952019-04-25 22:22:01 +02002467 wincmd p
2468 let g:wsv1 = winsaveview()
2469 wincmd p
2470 let g:wsv2 = winsaveview()
2471 " triggers BufReadPre, should not move the cursor in either window
2472 " The topline may change one line in a large window.
2473 edit
2474 call assert_inrange(g:wsv2.topline - 1, g:wsv2.topline + 1, winsaveview().topline)
2475 call assert_equal(g:wsv2.lnum, winsaveview().lnum)
2476 call assert_equal(2, b:bufreadpre)
2477 wincmd p
2478 call assert_equal(g:wsv1.topline, winsaveview().topline)
2479 call assert_equal(g:wsv1.lnum, winsaveview().lnum)
2480 call assert_equal(2, b:bufreadpre)
2481 " Now set the cursor position in an BufReadPre autocommand
2482 " (even though the position will be invalid, this should make Vim reset the
2483 " cursor position in the other window.
2484 wincmd p
2485 set cpo+=g
2486 " won't do anything, but try to set the cursor on an invalid lnum
2487 autocmd BufReadPre <buffer> :norm! 70gg
2488 " triggers BufReadPre, should not move the cursor in either window
2489 e
2490 call assert_equal(1, winsaveview().topline)
2491 call assert_equal(1, winsaveview().lnum)
2492 call assert_equal(3, b:bufreadpre)
2493 wincmd p
2494 call assert_equal(g:wsv1.topline, winsaveview().topline)
2495 call assert_equal(g:wsv1.lnum, winsaveview().lnum)
2496 call assert_equal(3, b:bufreadpre)
2497 close
2498 close
2499 call delete('XAutocmdBufReadPre.txt')
2500 set cpo-=g
2501endfunc
2502
Bram Moolenaar5e66b422019-01-24 21:58:10 +01002503" FileChangedShell tested in test_filechanged.vim
Bram Moolenaar69ea5872019-04-25 20:29:00 +02002504
2505" Tests for the following autocommands:
2506" - FileWritePre writing a compressed file
2507" - FileReadPost reading a compressed file
2508" - BufNewFile reading a file template
2509" - BufReadPre decompressing the file to be read
2510" - FilterReadPre substituting characters in the temp file
2511" - FilterReadPost substituting characters after filtering
2512" - FileReadPre set options for decompression
2513" - FileReadPost decompress the file
2514func Test_ReadWrite_Autocmds()
2515 " Run this test only on Unix-like systems and if gzip is available
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +02002516 CheckUnix
2517 CheckExecutable gzip
Bram Moolenaar69ea5872019-04-25 20:29:00 +02002518
2519 " Make $GZIP empty, "-v" would cause trouble.
2520 let $GZIP = ""
2521
2522 " Use a FileChangedShell autocommand to avoid a prompt for 'Xtestfile.gz'
2523 " being modified outside of Vim (noticed on Solaris).
2524 au FileChangedShell * echo 'caught FileChangedShell'
2525
2526 " Test for the FileReadPost, FileWritePre and FileWritePost autocmds
2527 augroup Test1
2528 au!
2529 au FileWritePre *.gz '[,']!gzip
2530 au FileWritePost *.gz undo
2531 au FileReadPost *.gz '[,']!gzip -d
2532 augroup END
2533
2534 new
2535 set bin
2536 call append(0, [
2537 \ 'line 2 Abcdefghijklmnopqrstuvwxyz',
2538 \ 'line 3 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2539 \ 'line 4 Abcdefghijklmnopqrstuvwxyz',
2540 \ 'line 5 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2541 \ 'line 6 Abcdefghijklmnopqrstuvwxyz',
2542 \ 'line 7 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2543 \ 'line 8 Abcdefghijklmnopqrstuvwxyz',
2544 \ 'line 9 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2545 \ 'line 10 Abcdefghijklmnopqrstuvwxyz'
2546 \ ])
2547 1,9write! Xtestfile.gz
2548 enew! | close
2549
2550 new
2551 " Read and decompress the testfile
2552 0read Xtestfile.gz
2553 call assert_equal([
2554 \ 'line 2 Abcdefghijklmnopqrstuvwxyz',
2555 \ 'line 3 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2556 \ 'line 4 Abcdefghijklmnopqrstuvwxyz',
2557 \ 'line 5 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2558 \ 'line 6 Abcdefghijklmnopqrstuvwxyz',
2559 \ 'line 7 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2560 \ 'line 8 Abcdefghijklmnopqrstuvwxyz',
2561 \ 'line 9 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2562 \ 'line 10 Abcdefghijklmnopqrstuvwxyz'
2563 \ ], getline(1, 9))
2564 enew! | close
2565
2566 augroup Test1
2567 au!
2568 augroup END
2569
2570 " Test for the FileAppendPre and FileAppendPost autocmds
2571 augroup Test2
2572 au!
2573 au BufNewFile *.c read Xtest.c
2574 au FileAppendPre *.out '[,']s/new/NEW/
2575 au FileAppendPost *.out !cat Xtest.c >> test.out
2576 augroup END
2577
2578 call writefile(['/*', ' * Here is a new .c file', ' */'], 'Xtest.c')
2579 new foo.c " should load Xtest.c
2580 call assert_equal(['/*', ' * Here is a new .c file', ' */'], getline(2, 4))
2581 w! >> test.out " append it to the output file
2582
2583 let contents = readfile('test.out')
2584 call assert_equal(' * Here is a NEW .c file', contents[2])
2585 call assert_equal(' * Here is a new .c file', contents[5])
2586
2587 call delete('test.out')
2588 enew! | close
2589 augroup Test2
2590 au!
2591 augroup END
2592
2593 " Test for the BufReadPre and BufReadPost autocmds
2594 augroup Test3
2595 au!
2596 " setup autocommands to decompress before reading and re-compress
2597 " afterwards
2598 au BufReadPre *.gz exe '!gzip -d ' . shellescape(expand("<afile>"))
2599 au BufReadPre *.gz call rename(expand("<afile>:r"), expand("<afile>"))
2600 au BufReadPost *.gz call rename(expand("<afile>"), expand("<afile>:r"))
2601 au BufReadPost *.gz exe '!gzip ' . shellescape(expand("<afile>:r"))
2602 augroup END
2603
2604 e! Xtestfile.gz " Edit compressed file
2605 call assert_equal([
2606 \ 'line 2 Abcdefghijklmnopqrstuvwxyz',
2607 \ 'line 3 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2608 \ 'line 4 Abcdefghijklmnopqrstuvwxyz',
2609 \ 'line 5 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2610 \ 'line 6 Abcdefghijklmnopqrstuvwxyz',
2611 \ 'line 7 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2612 \ 'line 8 Abcdefghijklmnopqrstuvwxyz',
2613 \ 'line 9 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2614 \ 'line 10 Abcdefghijklmnopqrstuvwxyz'
2615 \ ], getline(1, 9))
2616
2617 w! >> test.out " Append it to the output file
2618
2619 augroup Test3
2620 au!
2621 augroup END
2622
2623 " Test for the FilterReadPre and FilterReadPost autocmds.
2624 set shelltemp " need temp files here
2625 augroup Test4
2626 au!
2627 au FilterReadPre *.out call rename(expand("<afile>"), expand("<afile>") . ".t")
2628 au FilterReadPre *.out exe 'silent !sed s/e/E/ ' . shellescape(expand("<afile>")) . ".t >" . shellescape(expand("<afile>"))
2629 au FilterReadPre *.out exe 'silent !rm ' . shellescape(expand("<afile>")) . '.t'
2630 au FilterReadPost *.out '[,']s/x/X/g
2631 augroup END
2632
2633 e! test.out " Edit the output file
2634 1,$!cat
2635 call assert_equal([
2636 \ 'linE 2 AbcdefghijklmnopqrstuvwXyz',
2637 \ 'linE 3 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
2638 \ 'linE 4 AbcdefghijklmnopqrstuvwXyz',
2639 \ 'linE 5 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
2640 \ 'linE 6 AbcdefghijklmnopqrstuvwXyz',
2641 \ 'linE 7 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
2642 \ 'linE 8 AbcdefghijklmnopqrstuvwXyz',
2643 \ 'linE 9 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
2644 \ 'linE 10 AbcdefghijklmnopqrstuvwXyz'
2645 \ ], getline(1, 9))
2646 call assert_equal([
2647 \ 'line 2 Abcdefghijklmnopqrstuvwxyz',
2648 \ 'line 3 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2649 \ 'line 4 Abcdefghijklmnopqrstuvwxyz',
2650 \ 'line 5 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2651 \ 'line 6 Abcdefghijklmnopqrstuvwxyz',
2652 \ 'line 7 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2653 \ 'line 8 Abcdefghijklmnopqrstuvwxyz',
2654 \ 'line 9 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2655 \ 'line 10 Abcdefghijklmnopqrstuvwxyz'
2656 \ ], readfile('test.out'))
2657
2658 augroup Test4
2659 au!
2660 augroup END
2661 set shelltemp&vim
2662
2663 " Test for the FileReadPre and FileReadPost autocmds.
2664 augroup Test5
2665 au!
2666 au FileReadPre *.gz exe 'silent !gzip -d ' . shellescape(expand("<afile>"))
2667 au FileReadPre *.gz call rename(expand("<afile>:r"), expand("<afile>"))
2668 au FileReadPost *.gz '[,']s/l/L/
2669 augroup END
2670
2671 new
2672 0r Xtestfile.gz " Read compressed file
2673 call assert_equal([
2674 \ 'Line 2 Abcdefghijklmnopqrstuvwxyz',
2675 \ 'Line 3 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2676 \ 'Line 4 Abcdefghijklmnopqrstuvwxyz',
2677 \ 'Line 5 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2678 \ 'Line 6 Abcdefghijklmnopqrstuvwxyz',
2679 \ 'Line 7 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2680 \ 'Line 8 Abcdefghijklmnopqrstuvwxyz',
2681 \ 'Line 9 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2682 \ 'Line 10 Abcdefghijklmnopqrstuvwxyz'
2683 \ ], getline(1, 9))
2684 call assert_equal([
2685 \ 'line 2 Abcdefghijklmnopqrstuvwxyz',
2686 \ 'line 3 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2687 \ 'line 4 Abcdefghijklmnopqrstuvwxyz',
2688 \ 'line 5 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2689 \ 'line 6 Abcdefghijklmnopqrstuvwxyz',
2690 \ 'line 7 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2691 \ 'line 8 Abcdefghijklmnopqrstuvwxyz',
2692 \ 'line 9 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2693 \ 'line 10 Abcdefghijklmnopqrstuvwxyz'
2694 \ ], readfile('Xtestfile.gz'))
2695
2696 augroup Test5
2697 au!
2698 augroup END
2699
2700 au! FileChangedShell
2701 call delete('Xtestfile.gz')
2702 call delete('Xtest.c')
2703 call delete('test.out')
2704endfunc
Bram Moolenaar23b51392019-05-09 21:38:43 +02002705
2706func Test_throw_in_BufWritePre()
2707 new
2708 call setline(1, ['one', 'two', 'three'])
2709 call assert_false(filereadable('Xthefile'))
2710 augroup throwing
2711 au BufWritePre X* throw 'do not write'
2712 augroup END
2713 try
2714 w Xthefile
2715 catch
2716 let caught = 1
2717 endtry
2718 call assert_equal(1, caught)
2719 call assert_false(filereadable('Xthefile'))
2720
2721 bwipe!
2722 au! throwing
2723endfunc
Bram Moolenaarcadbe1b2019-09-22 21:50:09 +02002724
Bram Moolenaar40fa12a2021-09-22 14:18:13 +02002725func Test_autocmd_in_try_block()
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +01002726 call mkdir('Xintrydir')
Bram Moolenaar40fa12a2021-09-22 14:18:13 +02002727 au BufEnter * let g:fname = expand('%')
2728 try
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +01002729 edit Xintrydir/
Bram Moolenaar40fa12a2021-09-22 14:18:13 +02002730 endtry
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +01002731 call assert_match('Xintrydir', g:fname)
Bram Moolenaar40fa12a2021-09-22 14:18:13 +02002732
2733 unlet g:fname
2734 au! BufEnter
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +01002735 call delete('Xintrydir', 'rf')
Bram Moolenaar40fa12a2021-09-22 14:18:13 +02002736endfunc
2737
Bram Moolenaarcadbe1b2019-09-22 21:50:09 +02002738func Test_autocmd_SafeState()
2739 CheckRunVimInTerminal
2740
2741 let lines =<< trim END
2742 let g:safe = 0
2743 let g:again = ''
2744 au SafeState * let g:safe += 1
2745 au SafeStateAgain * let g:again ..= 'x'
2746 func CallTimer()
2747 call timer_start(10, {id -> execute('let g:again ..= "t"')})
2748 endfunc
2749 END
2750 call writefile(lines, 'XSafeState')
2751 let buf = RunVimInTerminal('-S XSafeState', #{rows: 6})
2752
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01002753 " Sometimes we loop to handle a K_IGNORE, SafeState may be triggered once or
Bram Moolenaar8fb1b472020-02-23 16:16:26 +01002754 " more often.
Bram Moolenaarcadbe1b2019-09-22 21:50:09 +02002755 call term_sendkeys(buf, ":echo g:safe\<CR>")
Bram Moolenaar8fb1b472020-02-23 16:16:26 +01002756 call WaitForAssert({-> assert_match('^\d ', term_getline(buf, 6))}, 1000)
Bram Moolenaarcadbe1b2019-09-22 21:50:09 +02002757
Bram Moolenaar8fb1b472020-02-23 16:16:26 +01002758 " SafeStateAgain should be invoked at least three times
Bram Moolenaarcadbe1b2019-09-22 21:50:09 +02002759 call term_sendkeys(buf, ":echo g:again\<CR>")
Bram Moolenaar8fb1b472020-02-23 16:16:26 +01002760 call WaitForAssert({-> assert_match('^xxx', term_getline(buf, 6))}, 1000)
Bram Moolenaarcadbe1b2019-09-22 21:50:09 +02002761
2762 call term_sendkeys(buf, ":let g:again = ''\<CR>:call CallTimer()\<CR>")
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02002763 call TermWait(buf, 50)
Bram Moolenaar0f6629a2019-09-22 23:24:13 +02002764 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02002765 call TermWait(buf, 50)
Bram Moolenaarcadbe1b2019-09-22 21:50:09 +02002766 call term_sendkeys(buf, ":echo g:again\<CR>")
2767 call WaitForAssert({-> assert_match('xtx', term_getline(buf, 6))}, 1000)
2768
2769 call StopVimInTerminal(buf)
2770 call delete('XSafeState')
2771endfunc
Bram Moolenaar23324a02019-10-01 17:39:04 +02002772
2773func Test_autocmd_CmdWinEnter()
2774 CheckRunVimInTerminal
Bram Moolenaar21829c52021-01-26 22:42:21 +01002775 CheckFeature cmdwin
2776
Bram Moolenaar23324a02019-10-01 17:39:04 +02002777 let lines =<< trim END
Egor Zvorykin125ffd22021-11-17 14:01:14 +00002778 augroup vimHints | au! | augroup END
Bram Moolenaar23324a02019-10-01 17:39:04 +02002779 let b:dummy_var = 'This is a dummy'
2780 autocmd CmdWinEnter * quit
2781 let winnr = winnr('$')
2782 END
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01002783 let filename = 'XCmdWinEnter'
Bram Moolenaar23324a02019-10-01 17:39:04 +02002784 call writefile(lines, filename)
2785 let buf = RunVimInTerminal('-S '.filename, #{rows: 6})
2786
2787 call term_sendkeys(buf, "q:")
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02002788 call TermWait(buf)
Bram Moolenaar23324a02019-10-01 17:39:04 +02002789 call term_sendkeys(buf, ":echo b:dummy_var\<cr>")
Bram Moolenaar353c3512020-03-15 14:19:26 +01002790 call WaitForAssert({-> assert_match('^This is a dummy', term_getline(buf, 6))}, 2000)
Bram Moolenaar23324a02019-10-01 17:39:04 +02002791 call term_sendkeys(buf, ":echo &buftype\<cr>")
2792 call WaitForAssert({-> assert_notmatch('^nofile', term_getline(buf, 6))}, 1000)
2793 call term_sendkeys(buf, ":echo winnr\<cr>")
2794 call WaitForAssert({-> assert_match('^1', term_getline(buf, 6))}, 1000)
2795
2796 " clean up
2797 call StopVimInTerminal(buf)
2798 call delete(filename)
2799endfunc
Bram Moolenaarec66c412019-10-11 21:19:13 +02002800
2801func Test_autocmd_was_using_freed_memory()
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01002802 CheckFeature quickfix
2803
Bram Moolenaarec66c412019-10-11 21:19:13 +02002804 pedit xx
2805 n x
Bram Moolenaar92bb83e2021-02-03 23:04:46 +01002806 augroup winenter
2807 au WinEnter * if winnr('$') > 2 | quit | endif
2808 augroup END
Bram Moolenaarec66c412019-10-11 21:19:13 +02002809 split
Bram Moolenaar92bb83e2021-02-03 23:04:46 +01002810
2811 augroup winenter
2812 au! WinEnter
2813 augroup END
2814
2815 bwipe xx
2816 bwipe x
2817 pclose
Bram Moolenaarec66c412019-10-11 21:19:13 +02002818endfunc
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +01002819
2820func Test_BufWrite_lockmarks()
Bram Moolenaarf08b0eb2021-10-16 13:00:14 +01002821 let g:test_is_flaky = 1
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +01002822 edit! Xtest
2823 call setline(1, ['a', 'b', 'c', 'd'])
2824
2825 " :lockmarks preserves the marks
2826 call SetChangeMarks(2, 3)
2827 lockmarks write
2828 call assert_equal([2, 3], [line("'["), line("']")])
2829
2830 " *WritePre autocmds get the correct line range, but lockmarks preserves the
2831 " original values for the user
2832 augroup lockmarks
2833 au!
2834 au BufWritePre,FilterWritePre * call assert_equal([1, 4], [line("'["), line("']")])
2835 au FileWritePre * call assert_equal([3, 4], [line("'["), line("']")])
2836 augroup END
2837
2838 lockmarks write
2839 call assert_equal([2, 3], [line("'["), line("']")])
2840
2841 if executable('cat')
2842 lockmarks %!cat
2843 call assert_equal([2, 3], [line("'["), line("']")])
2844 endif
2845
2846 lockmarks 3,4write Xtest2
2847 call assert_equal([2, 3], [line("'["), line("']")])
2848
2849 au! lockmarks
2850 augroup! lockmarks
2851 call delete('Xtest')
2852 call delete('Xtest2')
2853endfunc
Bram Moolenaarce6db022020-01-07 20:11:42 +01002854
2855func Test_FileType_spell()
2856 if !isdirectory('/tmp')
2857 throw "Skipped: requires /tmp directory"
2858 endif
2859
2860 " this was crashing with an invalid free()
2861 setglobal spellfile=/tmp/en.utf-8.add
2862 augroup crash
2863 autocmd!
2864 autocmd BufNewFile,BufReadPost crashfile setf somefiletype
2865 autocmd BufNewFile,BufReadPost crashfile set ft=anotherfiletype
2866 autocmd FileType anotherfiletype setlocal spell
2867 augroup END
2868 func! NoCrash() abort
2869 edit /tmp/crashfile
2870 endfunc
2871 call NoCrash()
2872
2873 au! crash
2874 setglobal spellfile=
2875endfunc
Bram Moolenaarbc2b71d2020-02-17 21:33:30 +01002876
Bram Moolenaar406cd902020-02-18 21:54:41 +01002877" Test closing a window or editing another buffer from a FileChangedRO handler
2878" in a readonly buffer
2879func Test_FileChangedRO_winclose()
Bram Moolenaar62cd26a2020-10-11 20:08:44 +02002880 call test_override('ui_delay', 10)
2881
Bram Moolenaar406cd902020-02-18 21:54:41 +01002882 augroup FileChangedROTest
2883 au!
2884 autocmd FileChangedRO * quit
2885 augroup END
2886 new
2887 set readonly
2888 call assert_fails('normal i', 'E788:')
2889 close
2890 augroup! FileChangedROTest
2891
2892 augroup FileChangedROTest
2893 au!
2894 autocmd FileChangedRO * edit Xfile
2895 augroup END
2896 new
2897 set readonly
2898 call assert_fails('normal i', 'E788:')
2899 close
2900 augroup! FileChangedROTest
Bram Moolenaar62cd26a2020-10-11 20:08:44 +02002901 call test_override('ALL', 0)
Bram Moolenaar406cd902020-02-18 21:54:41 +01002902endfunc
2903
Bram Moolenaar0c81d1b2020-02-22 22:45:55 +01002904func LogACmd()
2905 call add(g:logged, line('$'))
2906endfunc
2907
2908func Test_TermChanged()
Bram Moolenaard28e0b32020-02-22 23:08:52 +01002909 CheckNotGui
2910
Bram Moolenaar0c81d1b2020-02-22 22:45:55 +01002911 enew!
2912 tabnew
2913 call setline(1, ['a', 'b', 'c', 'd'])
2914 $
2915 au TermChanged * call LogACmd()
2916 let g:logged = []
2917 let term_save = &term
2918 set term=xterm
2919 call assert_equal([1, 4], g:logged)
2920
2921 au! TermChanged
2922 let &term = term_save
2923 bwipe!
2924endfunc
2925
Bram Moolenaare3284872020-03-19 13:55:03 +01002926" Test for FileReadCmd autocmd
2927func Test_autocmd_FileReadCmd()
2928 func ReadFileCmd()
2929 call append(line('$'), "v:cmdarg = " .. v:cmdarg)
2930 endfunc
2931 augroup FileReadCmdTest
2932 au!
2933 au FileReadCmd Xtest call ReadFileCmd()
2934 augroup END
2935
2936 new
2937 read ++bin Xtest
2938 read ++nobin Xtest
2939 read ++edit Xtest
2940 read ++bad=keep Xtest
2941 read ++bad=drop Xtest
2942 read ++bad=- Xtest
2943 read ++ff=unix Xtest
2944 read ++ff=dos Xtest
2945 read ++ff=mac Xtest
2946 read ++enc=utf-8 Xtest
2947
2948 call assert_equal(['',
2949 \ 'v:cmdarg = ++bin',
2950 \ 'v:cmdarg = ++nobin',
2951 \ 'v:cmdarg = ++edit',
2952 \ 'v:cmdarg = ++bad=keep',
2953 \ 'v:cmdarg = ++bad=drop',
2954 \ 'v:cmdarg = ++bad=-',
2955 \ 'v:cmdarg = ++ff=unix',
2956 \ 'v:cmdarg = ++ff=dos',
2957 \ 'v:cmdarg = ++ff=mac',
2958 \ 'v:cmdarg = ++enc=utf-8'], getline(1, '$'))
2959
2960 close!
2961 augroup FileReadCmdTest
2962 au!
2963 augroup END
2964 delfunc ReadFileCmd
2965endfunc
2966
Bram Moolenaaree4e0c12020-04-06 21:35:05 +02002967" Test for passing invalid arguments to autocmd
2968func Test_autocmd_invalid_args()
2969 " Additional character after * for event
2970 call assert_fails('autocmd *a Xfile set ff=unix', 'E215:')
2971 augroup Test
2972 augroup END
2973 " Invalid autocmd event
2974 call assert_fails('autocmd Bufabc Xfile set ft=vim', 'E216:')
2975 " Invalid autocmd event in a autocmd group
2976 call assert_fails('autocmd Test Bufabc Xfile set ft=vim', 'E216:')
2977 augroup! Test
2978 " Execute all autocmds
2979 call assert_fails('doautocmd * BufEnter', 'E217:')
2980 call assert_fails('augroup! x1a2b3', 'E367:')
2981 call assert_fails('autocmd BufNew <buffer=999> pwd', 'E680:')
Bram Moolenaar531be472020-09-23 22:38:05 +02002982 call assert_fails('autocmd BufNew \) set ff=unix', 'E55:')
Bram Moolenaaree4e0c12020-04-06 21:35:05 +02002983endfunc
2984
2985" Test for deep nesting of autocmds
2986func Test_autocmd_deep_nesting()
2987 autocmd BufEnter Xfile doautocmd BufEnter Xfile
2988 call assert_fails('doautocmd BufEnter Xfile', 'E218:')
2989 autocmd! BufEnter Xfile
2990endfunc
2991
Bram Moolenaarbe5ee862020-06-10 20:56:58 +02002992" Tests for SigUSR1 autocmd event, which is only available on posix systems.
2993func Test_autocmd_sigusr1()
2994 CheckUnix
Bram Moolenaar62cd26a2020-10-11 20:08:44 +02002995 CheckExecutable /bin/kill
Bram Moolenaarbe5ee862020-06-10 20:56:58 +02002996
2997 let g:sigusr1_passed = 0
2998 au SigUSR1 * let g:sigusr1_passed = 1
2999 call system('/bin/kill -s usr1 ' . getpid())
3000 call WaitForAssert({-> assert_true(g:sigusr1_passed)})
3001
3002 au! SigUSR1
3003 unlet g:sigusr1_passed
3004endfunc
3005
Bram Moolenaarb340bae2020-06-15 19:51:56 +02003006" Test for BufReadPre autocmd deleting the file
3007func Test_BufReadPre_delfile()
3008 augroup TestAuCmd
3009 au!
Bram Moolenaare7cda972022-08-29 11:02:59 +01003010 autocmd BufReadPre XbufreadPre call delete('XbufreadPre')
Bram Moolenaarb340bae2020-06-15 19:51:56 +02003011 augroup END
Bram Moolenaare7cda972022-08-29 11:02:59 +01003012 call writefile([], 'XbufreadPre')
3013 call assert_fails('new XbufreadPre', 'E200:')
3014 call assert_equal('XbufreadPre', @%)
Bram Moolenaarb340bae2020-06-15 19:51:56 +02003015 call assert_equal(1, &readonly)
Bram Moolenaare7cda972022-08-29 11:02:59 +01003016 call delete('XbufreadPre')
Bram Moolenaarb340bae2020-06-15 19:51:56 +02003017 augroup TestAuCmd
3018 au!
3019 augroup END
3020 close!
3021endfunc
3022
3023" Test for BufReadPre autocmd changing the current buffer
3024func Test_BufReadPre_changebuf()
3025 augroup TestAuCmd
3026 au!
Bram Moolenaare7cda972022-08-29 11:02:59 +01003027 autocmd BufReadPre Xchangebuf edit Xsomeotherfile
Bram Moolenaarb340bae2020-06-15 19:51:56 +02003028 augroup END
Bram Moolenaare7cda972022-08-29 11:02:59 +01003029 call writefile([], 'Xchangebuf')
3030 call assert_fails('new Xchangebuf', 'E201:')
Bram Moolenaarb340bae2020-06-15 19:51:56 +02003031 call assert_equal('Xsomeotherfile', @%)
3032 call assert_equal(1, &readonly)
Bram Moolenaare7cda972022-08-29 11:02:59 +01003033 call delete('Xchangebuf')
Bram Moolenaarb340bae2020-06-15 19:51:56 +02003034 augroup TestAuCmd
3035 au!
3036 augroup END
3037 close!
3038endfunc
3039
3040" Test for BufWipeouti autocmd changing the current buffer when reading a file
3041" in an empty buffer with 'f' flag in 'cpo'
3042func Test_BufDelete_changebuf()
3043 new
3044 augroup TestAuCmd
3045 au!
3046 autocmd BufWipeout * let bufnr = bufadd('somefile') | exe "b " .. bufnr
3047 augroup END
3048 let save_cpo = &cpo
3049 set cpo+=f
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02003050 call assert_fails('r Xfile', ['E812:', 'E484:'])
Bram Moolenaarb340bae2020-06-15 19:51:56 +02003051 call assert_equal('somefile', @%)
3052 let &cpo = save_cpo
3053 augroup TestAuCmd
3054 au!
3055 augroup END
3056 close!
3057endfunc
3058
Bram Moolenaar0fe937f2020-06-16 22:42:04 +02003059" Test for the temporary internal window used to execute autocmds
3060func Test_autocmd_window()
3061 %bw!
3062 edit one.txt
3063 tabnew two.txt
Bram Moolenaar41cd8032021-03-13 15:47:56 +01003064 vnew three.txt
3065 tabnew four.txt
3066 tabprevious
Bram Moolenaar0fe937f2020-06-16 22:42:04 +02003067 let g:blist = []
Bram Moolenaar832adf92020-06-25 19:01:36 +02003068 augroup aucmd_win_test1
Bram Moolenaar0fe937f2020-06-16 22:42:04 +02003069 au!
3070 au BufEnter * call add(g:blist, [expand('<afile>'),
3071 \ win_gettype(bufwinnr(expand('<afile>')))])
3072 augroup END
3073
3074 doautoall BufEnter
Bram Moolenaar41cd8032021-03-13 15:47:56 +01003075 call assert_equal([
3076 \ ['one.txt', 'autocmd'],
3077 \ ['two.txt', ''],
3078 \ ['four.txt', 'autocmd'],
3079 \ ['three.txt', ''],
3080 \ ], g:blist)
Bram Moolenaar0fe937f2020-06-16 22:42:04 +02003081
Bram Moolenaar832adf92020-06-25 19:01:36 +02003082 augroup aucmd_win_test1
Bram Moolenaar0fe937f2020-06-16 22:42:04 +02003083 au!
3084 augroup END
Bram Moolenaar832adf92020-06-25 19:01:36 +02003085 augroup! aucmd_win_test1
3086 %bw!
3087endfunc
3088
3089" Test for trying to close the temporary window used for executing an autocmd
3090func Test_close_autocmd_window()
3091 %bw!
3092 edit one.txt
3093 tabnew two.txt
3094 augroup aucmd_win_test2
3095 au!
3096 au BufEnter * if expand('<afile>') == 'one.txt' | 1close | endif
3097 augroup END
3098
3099 call assert_fails('doautoall BufEnter', 'E813:')
3100
3101 augroup aucmd_win_test2
3102 au!
3103 augroup END
3104 augroup! aucmd_win_test2
Bram Moolenaarcf844172020-06-26 19:44:06 +02003105 %bwipe!
3106endfunc
3107
3108" Test for trying to close the tab that has the temporary window for exeucing
3109" an autocmd.
3110func Test_close_autocmd_tab()
3111 edit one.txt
3112 tabnew two.txt
3113 augroup aucmd_win_test
3114 au!
3115 au BufEnter * if expand('<afile>') == 'one.txt' | tabfirst | tabonly | endif
3116 augroup END
3117
3118 call assert_fails('doautoall BufEnter', 'E813:')
3119
3120 tabonly
3121 augroup aucmd_win_test
3122 au!
3123 augroup END
3124 augroup! aucmd_win_test
3125 %bwipe!
Bram Moolenaar0fe937f2020-06-16 22:42:04 +02003126endfunc
3127
Bram Moolenaarcb1956d2022-01-07 15:45:18 +00003128func Test_Visual_doautoall_redraw()
3129 call setline(1, ['a', 'b'])
3130 new
3131 wincmd p
3132 call feedkeys("G\<C-V>", 'txn')
3133 autocmd User Explode ++once redraw
3134 doautoall User Explode
3135 %bwipe!
3136endfunc
3137
Bram Moolenaar6bcb8772021-02-03 21:23:29 +01003138" This was using freed memory.
3139func Test_BufNew_arglocal()
3140 arglocal
3141 au BufNew * arglocal
3142 call assert_fails('drop xx', 'E1156:')
3143
3144 au! BufNew
3145endfunc
3146
Bram Moolenaar8ab37572021-02-03 21:56:59 +01003147func Test_autocmd_closes_window()
3148 au BufNew,BufWinLeave * e %e
3149 file yyy
3150 au BufNew,BufWinLeave * ball
Bram Moolenaaraad5f9d2021-02-06 17:30:31 +01003151 n xxx
Bram Moolenaar8ab37572021-02-03 21:56:59 +01003152
Bram Moolenaaraad5f9d2021-02-06 17:30:31 +01003153 %bwipe
Bram Moolenaar8ab37572021-02-03 21:56:59 +01003154 au! BufNew
3155 au! BufWinLeave
3156endfunc
3157
Bram Moolenaar92bb83e2021-02-03 23:04:46 +01003158func Test_autocmd_quit_psearch()
3159 sn aa bb
3160 augroup aucmd_win_test
3161 au!
3162 au BufEnter,BufLeave,BufNew,WinEnter,WinLeave,WinNew * if winnr('$') > 1 | q | endif
3163 augroup END
3164 ps /
3165
3166 augroup aucmd_win_test
3167 au!
3168 augroup END
zeertzjq7851c692022-04-21 11:14:01 +01003169 new
3170 pclose
Bram Moolenaar92bb83e2021-02-03 23:04:46 +01003171endfunc
3172
Bram Moolenaaraad5f9d2021-02-06 17:30:31 +01003173" Fuzzer found some strange combination that caused a crash.
3174func Test_autocmd_normal_mess()
Bram Moolenaardd07c022021-02-07 13:32:46 +01003175 " For unknown reason this hangs on MS-Windows
3176 CheckNotMSWindows
3177
Bram Moolenaaraad5f9d2021-02-06 17:30:31 +01003178 augroup aucmd_normal_test
3179 au BufLeave,BufWinLeave,BufHidden,BufUnload,BufDelete,BufWipeout * norm 7q/qc
3180 augroup END
Bram Moolenaar983d83f2021-02-07 12:12:43 +01003181 call assert_fails('o4', 'E1159')
Bram Moolenaaraad5f9d2021-02-06 17:30:31 +01003182 silent! H
Bram Moolenaar983d83f2021-02-07 12:12:43 +01003183 call assert_fails('e xx', 'E1159')
Bram Moolenaaraad5f9d2021-02-06 17:30:31 +01003184 normal G
3185
3186 augroup aucmd_normal_test
3187 au!
3188 augroup END
3189endfunc
3190
Bram Moolenaar8c6951f2021-02-06 18:08:45 +01003191func Test_autocmd_closing_cmdwin()
Bram Moolenaardd07c022021-02-07 13:32:46 +01003192 " For unknown reason this hangs on MS-Windows
3193 CheckNotMSWindows
3194
Bram Moolenaar8c6951f2021-02-06 18:08:45 +01003195 au BufWinLeave * nested q
3196 call assert_fails("norm 7q?\n", 'E855:')
3197
3198 au! BufWinLeave
3199 new
3200 only
3201endfunc
3202
Bram Moolenaar2c7080b2021-02-06 19:19:42 +01003203func Test_autocmd_vimgrep()
3204 augroup aucmd_vimgrep
Charlie Grovesfef44852022-04-19 16:24:12 +01003205 au QuickfixCmdPre,BufNew,BufReadCmd * sb
zeertzjq7851c692022-04-21 11:14:01 +01003206 au QuickfixCmdPre,BufNew,BufReadCmd * q9
Bram Moolenaar2c7080b2021-02-06 19:19:42 +01003207 augroup END
Bram Moolenaardd07c022021-02-07 13:32:46 +01003208 call assert_fails('lv ?a? foo', 'E926:')
Bram Moolenaar2c7080b2021-02-06 19:19:42 +01003209
3210 augroup aucmd_vimgrep
3211 au!
3212 augroup END
3213endfunc
3214
Bram Moolenaar73b8b0a2021-08-01 14:52:32 +02003215func Test_autocmd_with_block()
3216 augroup block_testing
3217 au BufReadPost *.xml {
3218 setlocal matchpairs+=<:>
3219 /<start
3220 }
Bram Moolenaar63b91732021-08-05 20:40:03 +02003221 au CursorHold * {
3222 autocmd BufReadPre * ++once echo 'one' | echo 'two'
3223 g:gotSafeState = 77
3224 }
Bram Moolenaar73b8b0a2021-08-01 14:52:32 +02003225 augroup END
3226
3227 let expected = "\n--- Autocommands ---\nblock_testing BufRead\n *.xml {^@ setlocal matchpairs+=<:>^@ /<start^@ }"
3228 call assert_equal(expected, execute('au BufReadPost *.xml'))
3229
Bram Moolenaar63b91732021-08-05 20:40:03 +02003230 doautocmd CursorHold
3231 call assert_equal(77, g:gotSafeState)
3232 unlet g:gotSafeState
3233
Bram Moolenaar73b8b0a2021-08-01 14:52:32 +02003234 augroup block_testing
3235 au!
Bram Moolenaar75ebd2a2022-06-03 17:39:46 +01003236 autocmd CursorHold * {
3237 if true
3238 # comment
3239 && true
3240
3241 && true
3242 g:done = 'yes'
3243 endif
3244 }
3245 augroup END
3246 doautocmd CursorHold
3247 call assert_equal('yes', g:done)
3248
3249 unlet g:done
3250 augroup block_testing
3251 au!
Bram Moolenaar73b8b0a2021-08-01 14:52:32 +02003252 augroup END
3253endfunc
3254
Christian Brabandtdb3b4462021-10-16 11:58:55 +01003255" Test TextChangedI and TextChanged
3256func Test_Changed_ChangedI()
3257 new
3258 call test_override("char_avail", 1)
3259 let [g:autocmd_i, g:autocmd_n] = ['','']
3260
3261 func! TextChangedAutocmdI(char)
3262 let g:autocmd_{tolower(a:char)} = a:char .. b:changedtick
3263 endfunc
3264
3265 augroup Test_TextChanged
3266 au!
3267 au TextChanged <buffer> :call TextChangedAutocmdI('N')
3268 au TextChangedI <buffer> :call TextChangedAutocmdI('I')
3269 augroup END
3270
3271 call feedkeys("ifoo\<esc>", 'tnix')
3272 " TODO: Test test does not seem to trigger TextChanged autocommand, this
3273 " requires running Vim in a terminal window.
3274 " call assert_equal('N3', g:autocmd_n)
3275 call assert_equal('I3', g:autocmd_i)
3276
3277 call feedkeys("yyp", 'tnix')
3278 " TODO: Test test does not seem to trigger TextChanged autocommand.
3279 " call assert_equal('N4', g:autocmd_n)
3280 call assert_equal('I3', g:autocmd_i)
3281
3282 " CleanUp
3283 call test_override("char_avail", 0)
3284 au! TextChanged <buffer>
3285 au! TextChangedI <buffer>
3286 augroup! Test_TextChanged
3287 delfu TextChangedAutocmdI
3288 unlet! g:autocmd_i g:autocmd_n
3289
3290 bw!
3291endfunc
Bram Moolenaar2c7080b2021-02-06 19:19:42 +01003292
Bram Moolenaar6f2465d2022-03-22 18:13:01 +00003293func Test_closing_autocmd_window()
3294 let lines =<< trim END
3295 edit Xa.txt
3296 tabnew Xb.txt
3297 autocmd BufEnter Xa.txt unhide 1
3298 doautoall BufEnter
3299 END
3300 call v9.CheckScriptFailure(lines, 'E814:')
3301 au! BufEnter
3302 only!
3303 bwipe Xa.txt
3304 bwipe Xb.txt
3305endfunc
3306
Bram Moolenaar347538f2022-03-26 16:28:06 +00003307func Test_bufwipeout_changes_window()
3308 " This should not crash, but we don't have any expectations about what
3309 " happens, changing window in BufWipeout has unpredictable results.
3310 tabedit
3311 let g:window_id = win_getid()
3312 topleft new
3313 setlocal bufhidden=wipe
3314 autocmd BufWipeout <buffer> call win_gotoid(g:window_id)
3315 tabprevious
3316 +tabclose
3317
3318 unlet g:window_id
3319 au! BufWipeout
3320 %bwipe!
3321endfunc
3322
zeertzjq021996f2022-04-10 11:44:04 +01003323func Test_v_event_readonly()
3324 autocmd CompleteChanged * let v:event.width = 0
3325 call assert_fails("normal! i\<C-X>\<C-V>", 'E46:')
3326 au! CompleteChanged
3327
3328 autocmd DirChangedPre * let v:event.directory = ''
3329 call assert_fails('cd .', 'E46:')
3330 au! DirChangedPre
3331
3332 autocmd ModeChanged * let v:event.new_mode = ''
3333 call assert_fails('normal! cc', 'E46:')
3334 au! ModeChanged
3335
3336 autocmd TextYankPost * let v:event.operator = ''
3337 call assert_fails('normal! yy', 'E46:')
3338 au! TextYankPost
3339endfunc
3340
zeertzjqc9e8fd62022-07-26 18:12:38 +01003341" Test for ModeChanged pattern
3342func Test_mode_changes()
3343 let g:index = 0
3344 let g:mode_seq = ['n', 'i', 'n', 'v', 'V', 'i', 'ix', 'i', 'ic', 'i', 'n', 'no', 'n', 'V', 'v', 's', 'n']
3345 func! TestMode()
3346 call assert_equal(g:mode_seq[g:index], get(v:event, "old_mode"))
3347 call assert_equal(g:mode_seq[g:index + 1], get(v:event, "new_mode"))
3348 call assert_equal(mode(1), get(v:event, "new_mode"))
3349 let g:index += 1
3350 endfunc
3351
3352 au ModeChanged * :call TestMode()
3353 let g:n_to_any = 0
3354 au ModeChanged n:* let g:n_to_any += 1
3355 call feedkeys("i\<esc>vVca\<CR>\<C-X>\<C-L>\<esc>ggdG", 'tnix')
3356
3357 let g:V_to_v = 0
3358 au ModeChanged V:v let g:V_to_v += 1
3359 call feedkeys("Vv\<C-G>\<esc>", 'tnix')
3360 call assert_equal(len(filter(g:mode_seq[1:], {idx, val -> val == 'n'})), g:n_to_any)
3361 call assert_equal(1, g:V_to_v)
3362 call assert_equal(len(g:mode_seq) - 1, g:index)
3363
3364 let g:n_to_i = 0
3365 au ModeChanged n:i let g:n_to_i += 1
3366 let g:n_to_niI = 0
3367 au ModeChanged i:niI let g:n_to_niI += 1
3368 let g:niI_to_i = 0
3369 au ModeChanged niI:i let g:niI_to_i += 1
3370 let g:nany_to_i = 0
3371 au ModeChanged n*:i let g:nany_to_i += 1
3372 let g:i_to_n = 0
3373 au ModeChanged i:n let g:i_to_n += 1
3374 let g:nori_to_any = 0
3375 au ModeChanged [ni]:* let g:nori_to_any += 1
3376 let g:i_to_any = 0
3377 au ModeChanged i:* let g:i_to_any += 1
3378 let g:index = 0
3379 let g:mode_seq = ['n', 'i', 'niI', 'i', 'n']
3380 call feedkeys("a\<C-O>l\<esc>", 'tnix')
3381 call assert_equal(len(g:mode_seq) - 1, g:index)
3382 call assert_equal(1, g:n_to_i)
3383 call assert_equal(1, g:n_to_niI)
3384 call assert_equal(1, g:niI_to_i)
3385 call assert_equal(2, g:nany_to_i)
3386 call assert_equal(1, g:i_to_n)
3387 call assert_equal(2, g:i_to_any)
3388 call assert_equal(3, g:nori_to_any)
3389
3390 if has('terminal')
3391 let g:mode_seq += ['c', 'n', 't', 'nt', 'c', 'nt', 'n']
3392 call feedkeys(":term\<CR>\<C-W>N:bd!\<CR>", 'tnix')
3393 call assert_equal(len(g:mode_seq) - 1, g:index)
3394 call assert_equal(1, g:n_to_i)
3395 call assert_equal(1, g:n_to_niI)
3396 call assert_equal(1, g:niI_to_i)
3397 call assert_equal(2, g:nany_to_i)
3398 call assert_equal(1, g:i_to_n)
3399 call assert_equal(2, g:i_to_any)
3400 call assert_equal(5, g:nori_to_any)
3401 endif
3402
3403 if has('cmdwin')
3404 let g:n_to_c = 0
3405 au ModeChanged n:c let g:n_to_c += 1
3406 let g:c_to_n = 0
3407 au ModeChanged c:n let g:c_to_n += 1
3408 let g:mode_seq += ['c', 'n', 'c', 'n']
3409 call feedkeys("q:\<C-C>\<Esc>", 'tnix')
3410 call assert_equal(len(g:mode_seq) - 1, g:index)
3411 call assert_equal(2, g:n_to_c)
3412 call assert_equal(2, g:c_to_n)
3413 unlet g:n_to_c
3414 unlet g:c_to_n
3415 endif
3416
3417 au! ModeChanged
3418 delfunc TestMode
3419 unlet! g:mode_seq
3420 unlet! g:index
3421 unlet! g:n_to_any
3422 unlet! g:V_to_v
3423 unlet! g:n_to_i
3424 unlet! g:n_to_niI
3425 unlet! g:niI_to_i
3426 unlet! g:nany_to_i
3427 unlet! g:i_to_n
3428 unlet! g:nori_to_any
3429 unlet! g:i_to_any
3430endfunc
3431
3432func Test_recursive_ModeChanged()
3433 au! ModeChanged * norm 0u
3434 sil! norm 
3435 au! ModeChanged
3436endfunc
3437
3438func Test_ModeChanged_starts_visual()
3439 " This was triggering ModeChanged before setting VIsual, causing a crash.
3440 au! ModeChanged * norm 0u
3441 sil! norm 
3442
3443 au! ModeChanged
3444endfunc
Bram Moolenaar347538f2022-03-26 16:28:06 +00003445
Charlie Grovesfef44852022-04-19 16:24:12 +01003446func Test_noname_autocmd()
3447 augroup test_noname_autocmd_group
3448 autocmd!
3449 autocmd BufEnter * call add(s:li, ["BufEnter", expand("<afile>")])
3450 autocmd BufDelete * call add(s:li, ["BufDelete", expand("<afile>")])
3451 autocmd BufLeave * call add(s:li, ["BufLeave", expand("<afile>")])
3452 autocmd BufUnload * call add(s:li, ["BufUnload", expand("<afile>")])
3453 autocmd BufWipeout * call add(s:li, ["BufWipeout", expand("<afile>")])
3454 augroup END
3455
3456 let s:li = []
3457 edit foo
3458 call assert_equal([['BufUnload', ''], ['BufDelete', ''], ['BufWipeout', ''], ['BufEnter', 'foo']], s:li)
3459
3460 au! test_noname_autocmd_group
3461 augroup! test_noname_autocmd_group
3462endfunc
3463
Yegappan Lakshmanan1755a912022-05-19 10:31:47 +01003464" Test for the autocmd_get() function
3465func Test_autocmd_get()
3466 augroup TestAutoCmdFns
3467 au!
3468 autocmd BufAdd *.vim echo "bufadd-vim"
3469 autocmd BufAdd *.py echo "bufadd-py"
3470 autocmd BufHidden *.vim echo "bufhidden"
3471 augroup END
3472 augroup TestAutoCmdFns2
3473 autocmd BufAdd *.vim echo "bufadd-vim-2"
3474 autocmd BufRead *.a1b2c3 echo "bufadd-vim-2"
3475 augroup END
3476
3477 let l = autocmd_get()
3478 call assert_true(l->len() > 0)
3479
3480 " Test for getting all the autocmds in a group
3481 let expected = [
3482 \ #{cmd: 'echo "bufadd-vim"', group: 'TestAutoCmdFns',
3483 \ pattern: '*.vim', nested: v:false, once: v:false,
3484 \ event: 'BufAdd'},
3485 \ #{cmd: 'echo "bufadd-py"', group: 'TestAutoCmdFns',
3486 \ pattern: '*.py', nested: v:false, once: v:false,
3487 \ event: 'BufAdd'},
3488 \ #{cmd: 'echo "bufhidden"', group: 'TestAutoCmdFns',
3489 \ pattern: '*.vim', nested: v:false,
3490 \ once: v:false, event: 'BufHidden'}]
3491 call assert_equal(expected, autocmd_get(#{group: 'TestAutoCmdFns'}))
3492
3493 " Test for getting autocmds for all the patterns in a group
3494 call assert_equal(expected, autocmd_get(#{group: 'TestAutoCmdFns',
3495 \ event: '*'}))
3496
3497 " Test for getting autocmds for an event in a group
3498 let expected = [
3499 \ #{cmd: 'echo "bufadd-vim"', group: 'TestAutoCmdFns',
3500 \ pattern: '*.vim', nested: v:false, once: v:false,
3501 \ event: 'BufAdd'},
3502 \ #{cmd: 'echo "bufadd-py"', group: 'TestAutoCmdFns',
3503 \ pattern: '*.py', nested: v:false, once: v:false,
3504 \ event: 'BufAdd'}]
3505 call assert_equal(expected, autocmd_get(#{group: 'TestAutoCmdFns',
3506 \ event: 'BufAdd'}))
3507
3508 " Test for getting the autocmds for all the events in a group for particular
3509 " pattern
3510 call assert_equal([{'cmd': 'echo "bufadd-py"', 'group': 'TestAutoCmdFns',
3511 \ 'pattern': '*.py', 'nested': v:false, 'once': v:false,
3512 \ 'event': 'BufAdd'}],
3513 \ autocmd_get(#{group: 'TestAutoCmdFns', event: '*', pattern: '*.py'}))
3514
3515 " Test for getting the autocmds for an events in a group for particular
3516 " pattern
3517 let l = autocmd_get(#{group: 'TestAutoCmdFns', event: 'BufAdd',
3518 \ pattern: '*.vim'})
3519 call assert_equal([
3520 \ #{cmd: 'echo "bufadd-vim"', group: 'TestAutoCmdFns',
3521 \ pattern: '*.vim', nested: v:false, once: v:false,
3522 \ event: 'BufAdd'}], l)
3523
3524 " Test for getting the autocmds for a pattern in a group
3525 let l = autocmd_get(#{group: 'TestAutoCmdFns', pattern: '*.vim'})
3526 call assert_equal([
3527 \ #{cmd: 'echo "bufadd-vim"', group: 'TestAutoCmdFns',
3528 \ pattern: '*.vim', nested: v:false, once: v:false,
3529 \ event: 'BufAdd'},
3530 \ #{cmd: 'echo "bufhidden"', group: 'TestAutoCmdFns',
3531 \ pattern: '*.vim', nested: v:false,
3532 \ once: v:false, event: 'BufHidden'}], l)
3533
3534 " Test for getting the autocmds for a pattern in all the groups
3535 let l = autocmd_get(#{pattern: '*.a1b2c3'})
3536 call assert_equal([{'cmd': 'echo "bufadd-vim-2"', 'group': 'TestAutoCmdFns2',
3537 \ 'pattern': '*.a1b2c3', 'nested': v:false, 'once': v:false,
3538 \ 'event': 'BufRead'}], l)
3539
3540 " Test for getting autocmds for a pattern without any autocmds
3541 call assert_equal([], autocmd_get(#{group: 'TestAutoCmdFns',
3542 \ pattern: '*.abc'}))
3543 call assert_equal([], autocmd_get(#{group: 'TestAutoCmdFns',
3544 \ event: 'BufAdd', pattern: '*.abc'}))
3545 call assert_equal([], autocmd_get(#{group: 'TestAutoCmdFns',
3546 \ event: 'BufWipeout'}))
3547 call assert_fails("call autocmd_get(#{group: 'abc', event: 'BufAdd'})",
3548 \ 'E367:')
3549 let cmd = "echo autocmd_get(#{group: 'TestAutoCmdFns', event: 'abc'})"
3550 call assert_fails(cmd, 'E216:')
3551 call assert_fails("call autocmd_get(#{group: 'abc'})", 'E367:')
3552 call assert_fails("echo autocmd_get(#{event: 'abc'})", 'E216:')
3553
3554 augroup TestAutoCmdFns
3555 au!
3556 augroup END
3557 call assert_equal([], autocmd_get(#{group: 'TestAutoCmdFns'}))
3558
3559 " Test for nested and once autocmds
3560 augroup TestAutoCmdFns
3561 au!
3562 autocmd VimSuspend * ++nested echo "suspend"
3563 autocmd VimResume * ++once echo "resume"
3564 augroup END
3565
3566 let expected = [
3567 \ {'cmd': 'echo "suspend"', 'group': 'TestAutoCmdFns', 'pattern': '*',
3568 \ 'nested': v:true, 'once': v:false, 'event': 'VimSuspend'},
3569 \ {'cmd': 'echo "resume"', 'group': 'TestAutoCmdFns', 'pattern': '*',
3570 \ 'nested': v:false, 'once': v:true, 'event': 'VimResume'}]
3571 call assert_equal(expected, autocmd_get(#{group: 'TestAutoCmdFns'}))
3572
3573 " Test for buffer-local autocmd
3574 augroup TestAutoCmdFns
3575 au!
3576 autocmd TextYankPost <buffer> echo "textyankpost"
3577 augroup END
3578
3579 let expected = [
3580 \ {'cmd': 'echo "textyankpost"', 'group': 'TestAutoCmdFns',
3581 \ 'pattern': '<buffer=' .. bufnr() .. '>', 'nested': v:false,
3582 \ 'once': v:false, 'bufnr': bufnr(), 'event': 'TextYankPost'}]
3583 call assert_equal(expected, autocmd_get(#{group: 'TestAutoCmdFns'}))
3584
3585 augroup TestAutoCmdFns
3586 au!
3587 augroup END
3588 augroup! TestAutoCmdFns
3589 augroup TestAutoCmdFns2
3590 au!
3591 augroup END
3592 augroup! TestAutoCmdFns2
3593
3594 call assert_fails("echo autocmd_get(#{group: []})", 'E730:')
3595 call assert_fails("echo autocmd_get(#{event: {}})", 'E731:')
3596 call assert_fails("echo autocmd_get([])", 'E1206:')
3597endfunc
3598
3599" Test for the autocmd_add() function
3600func Test_autocmd_add()
3601 " Define a single autocmd in a group
3602 call autocmd_add([#{group: 'TestAcSet', event: 'BufAdd', pattern: '*.sh',
3603 \ cmd: 'echo "bufadd"', once: v:true, nested: v:true}])
3604 call assert_equal([#{cmd: 'echo "bufadd"', group: 'TestAcSet',
3605 \ pattern: '*.sh', nested: v:true, once: v:true,
3606 \ event: 'BufAdd'}], autocmd_get(#{group: 'TestAcSet'}))
3607
3608 " Define two autocmds in the same group
3609 call autocmd_delete([#{group: 'TestAcSet'}])
3610 call autocmd_add([#{group: 'TestAcSet', event: 'BufAdd', pattern: '*.sh',
3611 \ cmd: 'echo "bufadd"'},
3612 \ #{group: 'TestAcSet', event: 'BufEnter', pattern: '*.sh',
3613 \ cmd: 'echo "bufenter"'}])
3614 call assert_equal([
3615 \ #{cmd: 'echo "bufadd"', group: 'TestAcSet', pattern: '*.sh',
3616 \ nested: v:false, once: v:false, event: 'BufAdd'},
3617 \ #{cmd: 'echo "bufenter"', group: 'TestAcSet', pattern: '*.sh',
3618 \ nested: v:false, once: v:false, event: 'BufEnter'}],
3619 \ autocmd_get(#{group: 'TestAcSet'}))
3620
3621 " Define a buffer-local autocmd
3622 call autocmd_delete([#{group: 'TestAcSet'}])
3623 call autocmd_add([#{group: 'TestAcSet', event: 'CursorHold',
3624 \ bufnr: bufnr(), cmd: 'echo "cursorhold"'}])
3625 call assert_equal([
3626 \ #{cmd: 'echo "cursorhold"', group: 'TestAcSet',
3627 \ pattern: '<buffer=' .. bufnr() .. '>', nested: v:false,
3628 \ once: v:false, bufnr: bufnr(), event: 'CursorHold'}],
3629 \ autocmd_get(#{group: 'TestAcSet'}))
3630
3631 " Use an invalid buffer number
3632 call autocmd_delete([#{group: 'TestAcSet'}])
3633 call autocmd_add([#{group: 'TestAcSet', event: 'BufEnter',
3634 \ bufnr: -1, cmd: 'echo "bufenter"'}])
3635 let l = [#{group: 'TestAcSet', event: 'BufAdd', bufnr: 9999,
3636 \ cmd: 'echo "bufadd"'}]
3637 call assert_fails("echo autocmd_add(l)", 'E680:')
Yegappan Lakshmanan00e977c2022-06-01 12:31:53 +01003638 let l = [#{group: 'TestAcSet', event: 'BufAdd', bufnr: 9999,
3639 \ pattern: '*.py', cmd: 'echo "bufadd"'}]
3640 call assert_fails("echo autocmd_add(l)", 'E680:')
3641 let l = [#{group: 'TestAcSet', event: 'BufAdd', bufnr: 9999,
3642 \ pattern: ['*.py', '*.c'], cmd: 'echo "bufadd"'}]
3643 call assert_fails("echo autocmd_add(l)", 'E680:')
Yegappan Lakshmanan1755a912022-05-19 10:31:47 +01003644 let l = [#{group: 'TestAcSet', event: 'BufRead', bufnr: [],
3645 \ cmd: 'echo "bufread"'}]
3646 call assert_fails("echo autocmd_add(l)", 'E745:')
3647 call assert_equal([], autocmd_get(#{group: 'TestAcSet'}))
3648
3649 " Add two commands to the same group, event and pattern
3650 call autocmd_delete([#{group: 'TestAcSet'}])
3651 call autocmd_add([#{group: 'TestAcSet', event: 'BufUnload',
3652 \ pattern: 'abc', cmd: 'echo "cmd1"'}])
3653 call autocmd_add([#{group: 'TestAcSet', event: 'BufUnload',
3654 \ pattern: 'abc', cmd: 'echo "cmd2"'}])
3655 call assert_equal([
3656 \ #{cmd: 'echo "cmd1"', group: 'TestAcSet', pattern: 'abc',
3657 \ nested: v:false, once: v:false, event: 'BufUnload'},
3658 \ #{cmd: 'echo "cmd2"', group: 'TestAcSet', pattern: 'abc',
3659 \ nested: v:false, once: v:false, event: 'BufUnload'}],
3660 \ autocmd_get(#{group: 'TestAcSet'}))
3661
3662 " When adding a new autocmd, if the autocmd 'group' is not specified, then
3663 " the current autocmd group should be used.
3664 call autocmd_delete([#{group: 'TestAcSet'}])
3665 augroup TestAcSet
3666 call autocmd_add([#{event: 'BufHidden', pattern: 'abc', cmd: 'echo "abc"'}])
3667 augroup END
3668 call assert_equal([
3669 \ #{cmd: 'echo "abc"', group: 'TestAcSet', pattern: 'abc',
3670 \ nested: v:false, once: v:false, event: 'BufHidden'}],
3671 \ autocmd_get(#{group: 'TestAcSet'}))
3672
Yegappan Lakshmanan971f6822022-05-24 11:40:11 +01003673 " Test for replacing a cmd for an event in a group
3674 call autocmd_delete([#{group: 'TestAcSet'}])
3675 call autocmd_add([#{replace: v:true, group: 'TestAcSet', event: 'BufEnter',
3676 \ pattern: '*.py', cmd: 'echo "bufenter"'}])
3677 call autocmd_add([#{replace: v:true, group: 'TestAcSet', event: 'BufEnter',
3678 \ pattern: '*.py', cmd: 'echo "bufenter"'}])
3679 call assert_equal([
3680 \ #{cmd: 'echo "bufenter"', group: 'TestAcSet', pattern: '*.py',
3681 \ nested: v:false, once: v:false, event: 'BufEnter'}],
3682 \ autocmd_get(#{group: 'TestAcSet'}))
3683
3684 " Test for adding a command for an unsupported autocmd event
Yegappan Lakshmanan1755a912022-05-19 10:31:47 +01003685 let l = [#{group: 'TestAcSet', event: 'abc', pattern: '*.sh',
3686 \ cmd: 'echo "bufadd"'}]
3687 call assert_fails('call autocmd_add(l)', 'E216:')
3688
Yegappan Lakshmanane0ff3a72022-05-27 18:05:33 +01003689 " Test for using a list of events and patterns
3690 call autocmd_delete([#{group: 'TestAcSet'}])
3691 let l = [#{group: 'TestAcSet', event: ['BufEnter', 'BufLeave'],
3692 \ pattern: ['*.py', '*.sh'], cmd: 'echo "bufcmds"'}]
3693 call autocmd_add(l)
3694 call assert_equal([
3695 \ #{cmd: 'echo "bufcmds"', group: 'TestAcSet', pattern: '*.py',
3696 \ nested: v:false, once: v:false, event: 'BufEnter'},
3697 \ #{cmd: 'echo "bufcmds"', group: 'TestAcSet', pattern: '*.sh',
3698 \ nested: v:false, once: v:false, event: 'BufEnter'},
3699 \ #{cmd: 'echo "bufcmds"', group: 'TestAcSet', pattern: '*.py',
3700 \ nested: v:false, once: v:false, event: 'BufLeave'},
3701 \ #{cmd: 'echo "bufcmds"', group: 'TestAcSet', pattern: '*.sh',
3702 \ nested: v:false, once: v:false, event: 'BufLeave'}],
3703 \ autocmd_get(#{group: 'TestAcSet'}))
3704
3705 " Test for invalid values for 'event' item
3706 call autocmd_delete([#{group: 'TestAcSet'}])
3707 let l = [#{group: 'TestAcSet', event: test_null_string(),
3708 \ pattern: "*.py", cmd: 'echo "bufcmds"'}]
3709 call assert_fails('call autocmd_add(l)', 'E928:')
3710 let l = [#{group: 'TestAcSet', event: test_null_list(),
3711 \ pattern: "*.py", cmd: 'echo "bufcmds"'}]
3712 call assert_fails('call autocmd_add(l)', 'E714:')
3713 let l = [#{group: 'TestAcSet', event: {},
3714 \ pattern: "*.py", cmd: 'echo "bufcmds"'}]
3715 call assert_fails('call autocmd_add(l)', 'E777:')
3716 let l = [#{group: 'TestAcSet', event: [{}],
3717 \ pattern: "*.py", cmd: 'echo "bufcmds"'}]
3718 call assert_fails('call autocmd_add(l)', 'E928:')
3719 let l = [#{group: 'TestAcSet', event: [test_null_string()],
3720 \ pattern: "*.py", cmd: 'echo "bufcmds"'}]
3721 call assert_fails('call autocmd_add(l)', 'E928:')
3722 let l = [#{group: 'TestAcSet', event: 'BufEnter,BufLeave',
3723 \ pattern: '*.py', cmd: 'echo "bufcmds"'}]
3724 call assert_fails('call autocmd_add(l)', 'E216:')
3725 let l = [#{group: 'TestAcSet', event: [],
3726 \ pattern: "*.py", cmd: 'echo "bufcmds"'}]
3727 call autocmd_add(l)
3728 let l = [#{group: 'TestAcSet', event: [""],
3729 \ pattern: "*.py", cmd: 'echo "bufcmds"'}]
3730 call assert_fails('call autocmd_add(l)', 'E216:')
3731 let l = [#{group: 'TestAcSet', event: "",
3732 \ pattern: "*.py", cmd: 'echo "bufcmds"'}]
3733 call autocmd_add(l)
3734 call assert_equal([], autocmd_get(#{group: 'TestAcSet'}))
3735
3736 " Test for invalid values for 'pattern' item
3737 let l = [#{group: 'TestAcSet', event: "BufEnter",
3738 \ pattern: test_null_string(), cmd: 'echo "bufcmds"'}]
Yegappan Lakshmanan00e977c2022-06-01 12:31:53 +01003739 call assert_fails('call autocmd_add(l)', 'E928:')
Yegappan Lakshmanane0ff3a72022-05-27 18:05:33 +01003740 let l = [#{group: 'TestAcSet', event: "BufEnter",
3741 \ pattern: test_null_list(), cmd: 'echo "bufcmds"'}]
3742 call assert_fails('call autocmd_add(l)', 'E714:')
3743 let l = [#{group: 'TestAcSet', event: "BufEnter",
3744 \ pattern: {}, cmd: 'echo "bufcmds"'}]
3745 call assert_fails('call autocmd_add(l)', 'E777:')
3746 let l = [#{group: 'TestAcSet', event: "BufEnter",
3747 \ pattern: [{}], cmd: 'echo "bufcmds"'}]
3748 call assert_fails('call autocmd_add(l)', 'E928:')
3749 let l = [#{group: 'TestAcSet', event: "BufEnter",
3750 \ pattern: [test_null_string()], cmd: 'echo "bufcmds"'}]
3751 call assert_fails('call autocmd_add(l)', 'E928:')
3752 let l = [#{group: 'TestAcSet', event: "BufEnter",
3753 \ pattern: [], cmd: 'echo "bufcmds"'}]
3754 call autocmd_add(l)
3755 let l = [#{group: 'TestAcSet', event: "BufEnter",
3756 \ pattern: [""], cmd: 'echo "bufcmds"'}]
3757 call autocmd_add(l)
3758 let l = [#{group: 'TestAcSet', event: "BufEnter",
3759 \ pattern: "", cmd: 'echo "bufcmds"'}]
3760 call autocmd_add(l)
3761 call assert_equal([], autocmd_get(#{group: 'TestAcSet'}))
3762
3763 let l = [#{group: 'TestAcSet', event: 'BufEnter,abc,BufLeave',
3764 \ pattern: '*.py', cmd: 'echo "bufcmds"'}]
3765 call assert_fails('call autocmd_add(l)', 'E216:')
3766
Yegappan Lakshmanan1755a912022-05-19 10:31:47 +01003767 call assert_fails("call autocmd_add({})", 'E1211:')
3768 call assert_equal(v:false, autocmd_add(test_null_list()))
3769 call assert_true(autocmd_add([[]]))
3770 call assert_true(autocmd_add([test_null_dict()]))
3771
3772 augroup TestAcSet
3773 au!
3774 augroup END
3775
3776 call autocmd_add([#{group: 'TestAcSet'}])
3777 call autocmd_add([#{group: 'TestAcSet', event: 'BufAdd'}])
3778 call autocmd_add([#{group: 'TestAcSet', pat: '*.sh'}])
3779 call autocmd_add([#{group: 'TestAcSet', cmd: 'echo "a"'}])
3780 call autocmd_add([#{group: 'TestAcSet', event: 'BufAdd', pat: '*.sh'}])
3781 call autocmd_add([#{group: 'TestAcSet', event: 'BufAdd', cmd: 'echo "a"'}])
3782 call autocmd_add([#{group: 'TestAcSet', pat: '*.sh', cmd: 'echo "a"'}])
3783 call assert_equal([], autocmd_get(#{group: 'TestAcSet'}))
3784
3785 augroup! TestAcSet
3786endfunc
3787
3788" Test for deleting autocmd events and groups
3789func Test_autocmd_delete()
3790 " Delete an event in an autocmd group
3791 augroup TestAcSet
3792 au!
3793 au BufAdd *.sh echo "bufadd"
3794 au BufEnter *.sh echo "bufenter"
3795 augroup END
3796 call autocmd_delete([#{group: 'TestAcSet', event: 'BufAdd'}])
3797 call assert_equal([#{cmd: 'echo "bufenter"', group: 'TestAcSet',
3798 \ pattern: '*.sh', nested: v:false, once: v:false,
3799 \ event: 'BufEnter'}], autocmd_get(#{group: 'TestAcSet'}))
3800
3801 " Delete all the events in an autocmd group
3802 augroup TestAcSet
3803 au BufAdd *.sh echo "bufadd"
3804 augroup END
3805 call autocmd_delete([#{group: 'TestAcSet', event: '*'}])
3806 call assert_equal([], autocmd_get(#{group: 'TestAcSet'}))
3807
3808 " Delete a non-existing autocmd group
3809 call assert_fails("call autocmd_delete([#{group: 'abc'}])", 'E367:')
3810 " Delete a non-existing autocmd event
3811 let l = [#{group: 'TestAcSet', event: 'abc'}]
3812 call assert_fails("call autocmd_delete(l)", 'E216:')
3813 " Delete a non-existing autocmd pattern
3814 let l = [#{group: 'TestAcSet', event: 'BufAdd', pat: 'abc'}]
3815 call assert_true(autocmd_delete(l))
Yegappan Lakshmanan00e977c2022-06-01 12:31:53 +01003816 " Delete an autocmd for a non-existing buffer
3817 let l = [#{event: '*', bufnr: 9999, cmd: 'echo "x"'}]
3818 call assert_fails('call autocmd_delete(l)', 'E680:')
Yegappan Lakshmanan1755a912022-05-19 10:31:47 +01003819
3820 " Delete an autocmd group
3821 augroup TestAcSet
3822 au!
3823 au BufAdd *.sh echo "bufadd"
3824 au BufEnter *.sh echo "bufenter"
3825 augroup END
3826 call autocmd_delete([#{group: 'TestAcSet'}])
3827 call assert_fails("call autocmd_get(#{group: 'TestAcSet'})", 'E367:')
3828
3829 call assert_true(autocmd_delete([[]]))
3830 call assert_true(autocmd_delete([test_null_dict()]))
3831endfunc
3832
Bram Moolenaarbc2b71d2020-02-17 21:33:30 +01003833" vim: shiftwidth=2 sts=2 expandtab