blob: 458ac38f47ba06b2ddc47aff238ef939a4aba58e [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 Moolenaare1f3ab72022-09-04 21:29:08 +010027 call writefile(['one', 'two', 'three'], 'XoneTwoThree', 'D')
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 Moolenaare1f3ab72022-09-04 21:29:08 +010032 call writefile(before, 'XCHinit', 'D')
Bram Moolenaare7cda972022-08-29 11:02:59 +010033 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('XCHoutput')
Bram Moolenaar75911162020-07-21 19:44:47 +020048endfunc
49
Bram Moolenaarc67e8922016-05-24 16:07:40 +020050if has('timers')
Bram Moolenaar97b00752019-05-12 13:07:14 +020051
Bram Moolenaarc67e8922016-05-24 16:07:40 +020052 func ExitInsertMode(id)
53 call feedkeys("\<Esc>")
54 endfunc
55
56 func Test_cursorhold_insert()
Bram Moolenaarf18c4db2016-09-08 22:10:06 +020057 " Need to move the cursor.
58 call feedkeys("ggG", "xt")
59
Bram Moolenaarc67e8922016-05-24 16:07:40 +020060 let g:triggered = 0
61 au CursorHoldI * let g:triggered += 1
62 set updatetime=20
Bram Moolenaar92bb83e2021-02-03 23:04:46 +010063 call timer_start(200, 'ExitInsertMode')
Bram Moolenaarc67e8922016-05-24 16:07:40 +020064 call feedkeys('a', 'x!')
65 call assert_equal(1, g:triggered)
Bram Moolenaar26d98212019-01-27 22:32:55 +010066 unlet g:triggered
67 au! CursorHoldI
68 set updatetime&
69 endfunc
70
71 func Test_cursorhold_insert_with_timer_interrupt()
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +020072 CheckFeature job
Bram Moolenaar26d98212019-01-27 22:32:55 +010073 " Need to move the cursor.
74 call feedkeys("ggG", "xt")
75
76 " Confirm the timer invoked in exit_cb of the job doesn't disturb
77 " CursorHoldI event.
78 let g:triggered = 0
79 au CursorHoldI * let g:triggered += 1
Bram Moolenaar62cd26a2020-10-11 20:08:44 +020080 set updatetime=100
Bram Moolenaar26d98212019-01-27 22:32:55 +010081 call job_start(has('win32') ? 'cmd /c echo:' : 'echo',
Bram Moolenaar62cd26a2020-10-11 20:08:44 +020082 \ {'exit_cb': {-> timer_start(200, 'ExitInsertMode')}})
Bram Moolenaar26d98212019-01-27 22:32:55 +010083 call feedkeys('a', 'x!')
84 call assert_equal(1, g:triggered)
85 unlet g:triggered
Bram Moolenaare99e8442016-07-26 20:43:40 +020086 au! CursorHoldI
Bram Moolenaaraeac9002016-09-06 22:15:08 +020087 set updatetime&
Bram Moolenaarc67e8922016-05-24 16:07:40 +020088 endfunc
89
90 func Test_cursorhold_insert_ctrl_x()
91 let g:triggered = 0
92 au CursorHoldI * let g:triggered += 1
93 set updatetime=20
94 call timer_start(100, 'ExitInsertMode')
95 " CursorHoldI does not trigger after CTRL-X
96 call feedkeys("a\<C-X>", 'x!')
97 call assert_equal(0, g:triggered)
Bram Moolenaar26d98212019-01-27 22:32:55 +010098 unlet g:triggered
Bram Moolenaare99e8442016-07-26 20:43:40 +020099 au! CursorHoldI
Bram Moolenaaraeac9002016-09-06 22:15:08 +0200100 set updatetime&
Bram Moolenaarc67e8922016-05-24 16:07:40 +0200101 endfunc
Bram Moolenaar97b00752019-05-12 13:07:14 +0200102
Bram Moolenaar5a9357d2021-10-03 16:22:05 +0100103 func Test_cursorhold_insert_ctrl_g_U()
104 au CursorHoldI * :
105 set updatetime=20
106 new
107 call timer_start(100, { -> feedkeys("\<Left>foo\<Esc>", 't') })
108 call feedkeys("i()\<C-g>U", 'tx!')
109 sleep 200m
110 call assert_equal('(foo)', getline(1))
111 undo
112 call assert_equal('', getline(1))
113
114 bwipe!
115 au! CursorHoldI
116 set updatetime&
117 endfunc
118
Bram Moolenaar97b00752019-05-12 13:07:14 +0200119 func Test_OptionSet_modeline()
120 call test_override('starting', 1)
121 au! OptionSet
122 augroup set_tabstop
123 au OptionSet tabstop call timer_start(1, {-> execute("echo 'Handler called'", "")})
124 augroup END
Bram Moolenaare1f3ab72022-09-04 21:29:08 +0100125 call writefile(['vim: set ts=7 sw=5 :', 'something'], 'XoptionsetModeline', 'D')
Bram Moolenaar97b00752019-05-12 13:07:14 +0200126 set modeline
127 let v:errmsg = ''
128 call assert_fails('split XoptionsetModeline', 'E12:')
129 call assert_equal(7, &ts)
130 call assert_equal('', v:errmsg)
131
132 augroup set_tabstop
133 au!
134 augroup END
135 bwipe!
136 set ts&
Bram Moolenaar97b00752019-05-12 13:07:14 +0200137 call test_override('starting', 0)
138 endfunc
139
140endif "has('timers')
Bram Moolenaar40b1b542016-04-20 20:18:23 +0200141
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200142func Test_bufunload()
Bram Moolenaarc67e8922016-05-24 16:07:40 +0200143 augroup test_bufunload_group
144 autocmd!
145 autocmd BufUnload * call add(s:li, "bufunload")
146 autocmd BufDelete * call add(s:li, "bufdelete")
147 autocmd BufWipeout * call add(s:li, "bufwipeout")
148 augroup END
Bram Moolenaar40b1b542016-04-20 20:18:23 +0200149
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100150 let s:li = []
Bram Moolenaarc67e8922016-05-24 16:07:40 +0200151 new
152 setlocal bufhidden=
153 bunload
154 call assert_equal(["bufunload", "bufdelete"], s:li)
Bram Moolenaar40b1b542016-04-20 20:18:23 +0200155
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100156 let s:li = []
Bram Moolenaarc67e8922016-05-24 16:07:40 +0200157 new
158 setlocal bufhidden=delete
159 bunload
160 call assert_equal(["bufunload", "bufdelete"], s:li)
161
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100162 let s:li = []
Bram Moolenaarc67e8922016-05-24 16:07:40 +0200163 new
164 setlocal bufhidden=unload
165 bwipeout
166 call assert_equal(["bufunload", "bufdelete", "bufwipeout"], s:li)
167
Bram Moolenaare99e8442016-07-26 20:43:40 +0200168 au! test_bufunload_group
Bram Moolenaarc67e8922016-05-24 16:07:40 +0200169 augroup! test_bufunload_group
Bram Moolenaar40b1b542016-04-20 20:18:23 +0200170endfunc
Bram Moolenaar30445cb2016-07-09 15:21:02 +0200171
172" SEGV occurs in older versions. (At least 7.4.2005 or older)
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200173func Test_autocmd_bufunload_with_tabnext()
Bram Moolenaar30445cb2016-07-09 15:21:02 +0200174 tabedit
175 tabfirst
176
177 augroup test_autocmd_bufunload_with_tabnext_group
178 autocmd!
179 autocmd BufUnload <buffer> tabnext
180 augroup END
181
182 quit
183 call assert_equal(2, tabpagenr('$'))
184
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200185 autocmd! test_autocmd_bufunload_with_tabnext_group
Bram Moolenaar30445cb2016-07-09 15:21:02 +0200186 augroup! test_autocmd_bufunload_with_tabnext_group
187 tablast
188 quit
189endfunc
Bram Moolenaarc917da42016-07-19 22:31:36 +0200190
Bram Moolenaar5ed58c72021-01-28 14:24:55 +0100191func Test_argdelete_in_next()
192 au BufNew,BufEnter,BufLeave,BufWinEnter * argdel
193 call assert_fails('next a b', 'E1156:')
194 au! BufNew,BufEnter,BufLeave,BufWinEnter *
195endfunc
196
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200197func Test_autocmd_bufwinleave_with_tabfirst()
Bram Moolenaarf9e687e2016-09-04 21:33:09 +0200198 tabedit
199 augroup sample
200 autocmd!
201 autocmd BufWinLeave <buffer> tabfirst
202 augroup END
203 call setline(1, ['a', 'b', 'c'])
204 edit! a.txt
Bram Moolenaarf18c4db2016-09-08 22:10:06 +0200205 tabclose
Bram Moolenaarf9e687e2016-09-04 21:33:09 +0200206endfunc
207
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200208" SEGV occurs in older versions. (At least 7.4.2321 or older)
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200209func Test_autocmd_bufunload_avoiding_SEGV_01()
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200210 split aa.txt
211 let lastbuf = bufnr('$')
212
213 augroup test_autocmd_bufunload
214 autocmd!
215 exe 'autocmd BufUnload <buffer> ' . (lastbuf + 1) . 'bwipeout!'
216 augroup END
217
Bram Moolenaar28ee8922020-10-28 20:20:00 +0100218 call assert_fails('edit bb.txt', 'E937:')
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200219
220 autocmd! test_autocmd_bufunload
221 augroup! test_autocmd_bufunload
222 bwipe! aa.txt
223 bwipe! bb.txt
224endfunc
225
226" SEGV occurs in older versions. (At least 7.4.2321 or older)
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200227func Test_autocmd_bufunload_avoiding_SEGV_02()
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200228 setlocal buftype=nowrite
229 let lastbuf = bufnr('$')
230
231 augroup test_autocmd_bufunload
232 autocmd!
233 exe 'autocmd BufUnload <buffer> ' . (lastbuf + 1) . 'bwipeout!'
234 augroup END
235
236 normal! i1
237 call assert_fails('edit a.txt', 'E517:')
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200238
239 autocmd! test_autocmd_bufunload
240 augroup! test_autocmd_bufunload
241 bwipe! a.txt
242endfunc
243
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100244func Test_autocmd_dummy_wipeout()
245 " prepare files
Bram Moolenaare1f3ab72022-09-04 21:29:08 +0100246 call writefile([''], 'Xdummywipetest1.txt', 'D')
247 call writefile([''], 'Xdummywipetest2.txt', 'D')
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100248 augroup test_bufunload_group
249 autocmd!
250 autocmd BufUnload * call add(s:li, "bufunload")
251 autocmd BufDelete * call add(s:li, "bufdelete")
252 autocmd BufWipeout * call add(s:li, "bufwipeout")
253 augroup END
254
255 let s:li = []
256 split Xdummywipetest1.txt
257 silent! vimgrep /notmatched/ Xdummywipetest*
258 call assert_equal(["bufunload", "bufwipeout"], s:li)
259
260 bwipeout
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100261 au! test_bufunload_group
262 augroup! test_bufunload_group
263endfunc
264
Bram Moolenaarc917da42016-07-19 22:31:36 +0200265func Test_win_tab_autocmd()
266 let g:record = []
267
268 augroup testing
269 au WinNew * call add(g:record, 'WinNew')
naohiro ono23beefe2021-11-13 12:38:49 +0000270 au WinClosed * call add(g:record, 'WinClosed')
Bram Moolenaarc917da42016-07-19 22:31:36 +0200271 au WinEnter * call add(g:record, 'WinEnter')
272 au WinLeave * call add(g:record, 'WinLeave')
273 au TabNew * call add(g:record, 'TabNew')
Bram Moolenaar12c11d52016-07-19 23:13:03 +0200274 au TabClosed * call add(g:record, 'TabClosed')
Bram Moolenaarc917da42016-07-19 22:31:36 +0200275 au TabEnter * call add(g:record, 'TabEnter')
276 au TabLeave * call add(g:record, 'TabLeave')
277 augroup END
278
279 split
280 tabnew
281 close
282 close
283
284 call assert_equal([
285 \ 'WinLeave', 'WinNew', 'WinEnter',
286 \ 'WinLeave', 'TabLeave', 'WinNew', 'WinEnter', 'TabNew', 'TabEnter',
naohiro ono23beefe2021-11-13 12:38:49 +0000287 \ 'WinLeave', 'TabLeave', 'WinClosed', 'TabClosed', 'WinEnter', 'TabEnter',
288 \ 'WinLeave', 'WinClosed', 'WinEnter'
Bram Moolenaarc917da42016-07-19 22:31:36 +0200289 \ ], g:record)
290
Bram Moolenaar12c11d52016-07-19 23:13:03 +0200291 let g:record = []
292 tabnew somefile
293 tabnext
294 bwipe somefile
295
296 call assert_equal([
297 \ 'WinLeave', 'TabLeave', 'WinNew', 'WinEnter', 'TabNew', 'TabEnter',
298 \ 'WinLeave', 'TabLeave', 'WinEnter', 'TabEnter',
naohiro ono23beefe2021-11-13 12:38:49 +0000299 \ 'WinClosed', 'TabClosed'
Bram Moolenaar12c11d52016-07-19 23:13:03 +0200300 \ ], g:record)
301
Bram Moolenaarc917da42016-07-19 22:31:36 +0200302 augroup testing
303 au!
304 augroup END
305 unlet g:record
306endfunc
Bram Moolenaare99e8442016-07-26 20:43:40 +0200307
LemonBoy09371822022-04-08 15:18:45 +0100308func Test_WinScrolled()
309 CheckRunVimInTerminal
310
311 let lines =<< trim END
zeertzjqd58862d2022-04-12 11:32:48 +0100312 set nowrap scrolloff=0
313 for ii in range(1, 18)
314 call setline(ii, repeat(nr2char(96 + ii), ii * 2))
315 endfor
316 let win_id = win_getid()
317 let g:matched = v:false
318 execute 'au WinScrolled' win_id 'let g:matched = v:true'
319 let g:scrolled = 0
320 au WinScrolled * let g:scrolled += 1
321 au WinScrolled * let g:amatch = str2nr(expand('<amatch>'))
322 au WinScrolled * let g:afile = str2nr(expand('<afile>'))
LemonBoy09371822022-04-08 15:18:45 +0100323 END
Bram Moolenaare1f3ab72022-09-04 21:29:08 +0100324 call writefile(lines, 'Xtest_winscrolled', 'D')
LemonBoy09371822022-04-08 15:18:45 +0100325 let buf = RunVimInTerminal('-S Xtest_winscrolled', {'rows': 6})
326
327 call term_sendkeys(buf, ":echo g:scrolled\<CR>")
328 call WaitForAssert({-> assert_match('^0 ', term_getline(buf, 6))}, 1000)
329
330 " Scroll left/right in Normal mode.
331 call term_sendkeys(buf, "zlzh:echo g:scrolled\<CR>")
332 call WaitForAssert({-> assert_match('^2 ', term_getline(buf, 6))}, 1000)
333
334 " Scroll up/down in Normal mode.
335 call term_sendkeys(buf, "\<c-e>\<c-y>:echo g:scrolled\<CR>")
336 call WaitForAssert({-> assert_match('^4 ', term_getline(buf, 6))}, 1000)
337
338 " Scroll up/down in Insert mode.
339 call term_sendkeys(buf, "Mi\<c-x>\<c-e>\<Esc>i\<c-x>\<c-y>\<Esc>")
340 call term_sendkeys(buf, ":echo g:scrolled\<CR>")
341 call WaitForAssert({-> assert_match('^6 ', term_getline(buf, 6))}, 1000)
342
343 " Scroll the window horizontally to focus the last letter of the third line
344 " containing only six characters. Moving to the previous and shorter lines
345 " should trigger another autocommand as Vim has to make them visible.
346 call term_sendkeys(buf, "5zl2k")
347 call term_sendkeys(buf, ":echo g:scrolled\<CR>")
348 call WaitForAssert({-> assert_match('^8 ', term_getline(buf, 6))}, 1000)
349
350 " Ensure the command was triggered for the specified window ID.
351 call term_sendkeys(buf, ":echo g:matched\<CR>")
352 call WaitForAssert({-> assert_match('^v:true ', term_getline(buf, 6))}, 1000)
353
354 " Ensure the expansion of <amatch> and <afile> matches the window ID.
355 call term_sendkeys(buf, ":echo g:amatch == win_id && g:afile == win_id\<CR>")
356 call WaitForAssert({-> assert_match('^v:true ', term_getline(buf, 6))}, 1000)
357
358 call StopVimInTerminal(buf)
LemonBoy09371822022-04-08 15:18:45 +0100359endfunc
360
LemonBoy66e13ae2022-04-21 22:52:11 +0100361func Test_WinScrolled_mouse()
362 CheckRunVimInTerminal
363
364 let lines =<< trim END
365 set nowrap scrolloff=0
366 set mouse=a term=xterm ttymouse=sgr mousetime=200 clipboard=
367 call setline(1, ['foo']->repeat(32))
368 split
369 let g:scrolled = 0
370 au WinScrolled * let g:scrolled += 1
371 END
Bram Moolenaare1f3ab72022-09-04 21:29:08 +0100372 call writefile(lines, 'Xtest_winscrolled_mouse', 'D')
LemonBoy66e13ae2022-04-21 22:52:11 +0100373 let buf = RunVimInTerminal('-S Xtest_winscrolled_mouse', {'rows': 10})
374
375 " With the upper split focused, send a scroll-down event to the unfocused one.
376 call test_setmouse(7, 1)
377 call term_sendkeys(buf, "\<ScrollWheelDown>")
378 call TermWait(buf)
379 call term_sendkeys(buf, ":echo g:scrolled\<CR>")
380 call WaitForAssert({-> assert_match('^1', term_getline(buf, 10))}, 1000)
381
382 " Again, but this time while we're in insert mode.
383 call term_sendkeys(buf, "i\<ScrollWheelDown>\<Esc>")
384 call TermWait(buf)
385 call term_sendkeys(buf, ":echo g:scrolled\<CR>")
386 call WaitForAssert({-> assert_match('^2', term_getline(buf, 10))}, 1000)
387
388 call StopVimInTerminal(buf)
LemonBoy66e13ae2022-04-21 22:52:11 +0100389endfunc
390
zeertzjqd58862d2022-04-12 11:32:48 +0100391func Test_WinScrolled_close_curwin()
392 CheckRunVimInTerminal
393
394 let lines =<< trim END
395 set nowrap scrolloff=0
396 call setline(1, ['aaa', 'bbb'])
397 vsplit
398 au WinScrolled * close
399 au VimLeave * call writefile(['123456'], 'Xtestout')
400 END
Bram Moolenaare1f3ab72022-09-04 21:29:08 +0100401 call writefile(lines, 'Xtest_winscrolled_close_curwin', 'D')
zeertzjqd58862d2022-04-12 11:32:48 +0100402 let buf = RunVimInTerminal('-S Xtest_winscrolled_close_curwin', {'rows': 6})
403
404 " This was using freed memory
405 call term_sendkeys(buf, "\<C-E>")
406 call TermWait(buf)
407 call StopVimInTerminal(buf)
408
409 call assert_equal(['123456'], readfile('Xtestout'))
410
zeertzjqd58862d2022-04-12 11:32:48 +0100411 call delete('Xtestout')
412endfunc
413
zeertzjq670ab032022-08-28 19:16:15 +0100414func Test_WinScrolled_long_wrapped()
415 CheckRunVimInTerminal
416
417 let lines =<< trim END
418 set scrolloff=0
419 let height = winheight(0)
420 let width = winwidth(0)
421 let g:scrolled = 0
422 au WinScrolled * let g:scrolled += 1
423 call setline(1, repeat('foo', height * width))
424 call cursor(1, height * width)
425 END
Bram Moolenaare1f3ab72022-09-04 21:29:08 +0100426 call writefile(lines, 'Xtest_winscrolled_long_wrapped', 'D')
zeertzjq670ab032022-08-28 19:16:15 +0100427 let buf = RunVimInTerminal('-S Xtest_winscrolled_long_wrapped', {'rows': 6})
428
429 call term_sendkeys(buf, ":echo g:scrolled\<CR>")
430 call WaitForAssert({-> assert_match('^0 ', term_getline(buf, 6))}, 1000)
431
432 call term_sendkeys(buf, 'gj')
433 call term_sendkeys(buf, ":echo g:scrolled\<CR>")
434 call WaitForAssert({-> assert_match('^1 ', term_getline(buf, 6))}, 1000)
435
436 call term_sendkeys(buf, '0')
437 call term_sendkeys(buf, ":echo g:scrolled\<CR>")
438 call WaitForAssert({-> assert_match('^2 ', term_getline(buf, 6))}, 1000)
439
440 call term_sendkeys(buf, '$')
441 call term_sendkeys(buf, ":echo g:scrolled\<CR>")
442 call WaitForAssert({-> assert_match('^3 ', term_getline(buf, 6))}, 1000)
zeertzjq670ab032022-08-28 19:16:15 +0100443endfunc
444
naohiro ono23beefe2021-11-13 12:38:49 +0000445func Test_WinClosed()
446 " Test that the pattern is matched against the closed window's ID, and both
447 " <amatch> and <afile> are set to it.
448 new
449 let winid = win_getid()
450 let g:matched = v:false
451 augroup test-WinClosed
452 autocmd!
453 execute 'autocmd WinClosed' winid 'let g:matched = v:true'
454 autocmd WinClosed * let g:amatch = str2nr(expand('<amatch>'))
455 autocmd WinClosed * let g:afile = str2nr(expand('<afile>'))
456 augroup END
457 close
458 call assert_true(g:matched)
459 call assert_equal(winid, g:amatch)
460 call assert_equal(winid, g:afile)
461
462 " Test that WinClosed is non-recursive.
463 new
464 new
465 call assert_equal(3, winnr('$'))
466 let g:triggered = 0
467 augroup test-WinClosed
468 autocmd!
469 autocmd WinClosed * let g:triggered += 1
470 autocmd WinClosed * 2 wincmd c
471 augroup END
472 close
473 call assert_equal(1, winnr('$'))
474 call assert_equal(1, g:triggered)
475
476 autocmd! test-WinClosed
477 augroup! test-WinClosed
478 unlet g:matched
479 unlet g:amatch
480 unlet g:afile
481 unlet g:triggered
482endfunc
483
Bram Moolenaarc947b9a2022-04-06 17:59:21 +0100484func Test_WinClosed_throws()
485 vnew
486 let bnr = bufnr()
487 call assert_equal(1, bufloaded(bnr))
488 augroup test-WinClosed
489 autocmd WinClosed * throw 'foo'
490 augroup END
491 try
492 close
493 catch /.*/
494 endtry
495 call assert_equal(0, bufloaded(bnr))
496
497 autocmd! test-WinClosed
498 augroup! test-WinClosed
499endfunc
500
zeertzjq6a069402022-04-07 14:08:29 +0100501func Test_WinClosed_throws_with_tabs()
502 tabnew
503 let bnr = bufnr()
504 call assert_equal(1, bufloaded(bnr))
505 augroup test-WinClosed
506 autocmd WinClosed * throw 'foo'
507 augroup END
508 try
509 close
510 catch /.*/
511 endtry
512 call assert_equal(0, bufloaded(bnr))
513
514 autocmd! test-WinClosed
515 augroup! test-WinClosed
516endfunc
517
Bram Moolenaare99e8442016-07-26 20:43:40 +0200518func s:AddAnAutocmd()
519 augroup vimBarTest
520 au BufReadCmd * echo 'hello'
521 augroup END
522 call assert_equal(3, len(split(execute('au vimBarTest'), "\n")))
523endfunc
524
525func Test_early_bar()
526 " test that a bar is recognized before the {event}
527 call s:AddAnAutocmd()
Bram Moolenaarb8e642f2021-11-20 10:38:25 +0000528 augroup vimBarTest | au! | let done = 77 | augroup END
Bram Moolenaare99e8442016-07-26 20:43:40 +0200529 call assert_equal(1, len(split(execute('au vimBarTest'), "\n")))
Bram Moolenaarb8e642f2021-11-20 10:38:25 +0000530 call assert_equal(77, done)
Bram Moolenaare99e8442016-07-26 20:43:40 +0200531
532 call s:AddAnAutocmd()
Bram Moolenaarb8e642f2021-11-20 10:38:25 +0000533 augroup vimBarTest| au!| let done = 88 | augroup END
Bram Moolenaare99e8442016-07-26 20:43:40 +0200534 call assert_equal(1, len(split(execute('au vimBarTest'), "\n")))
Bram Moolenaarb8e642f2021-11-20 10:38:25 +0000535 call assert_equal(88, done)
Bram Moolenaare99e8442016-07-26 20:43:40 +0200536
537 " test that a bar is recognized after the {event}
538 call s:AddAnAutocmd()
Bram Moolenaarb8e642f2021-11-20 10:38:25 +0000539 augroup vimBarTest| au!BufReadCmd| let done = 99 | augroup END
Bram Moolenaare99e8442016-07-26 20:43:40 +0200540 call assert_equal(1, len(split(execute('au vimBarTest'), "\n")))
Bram Moolenaarb8e642f2021-11-20 10:38:25 +0000541 call assert_equal(99, done)
Bram Moolenaare99e8442016-07-26 20:43:40 +0200542
543 " test that a bar is recognized after the {group}
544 call s:AddAnAutocmd()
545 au! vimBarTest|echo 'hello'
546 call assert_equal(1, len(split(execute('au vimBarTest'), "\n")))
547endfunc
Bram Moolenaarf2c4c392016-07-29 20:50:24 +0200548
Bram Moolenaar5c809082016-09-01 16:21:48 +0200549func RemoveGroup()
550 autocmd! StartOK
551 augroup! StartOK
552endfunc
553
Bram Moolenaarf2c4c392016-07-29 20:50:24 +0200554func Test_augroup_warning()
555 augroup TheWarning
556 au VimEnter * echo 'entering'
557 augroup END
Bram Moolenaar5dc4e2f2020-11-25 14:15:12 +0100558 call assert_match("TheWarning.*VimEnter", execute('au VimEnter'))
Bram Moolenaarf2c4c392016-07-29 20:50:24 +0200559 redir => res
560 augroup! TheWarning
561 redir END
Bram Moolenaar5dc4e2f2020-11-25 14:15:12 +0100562 call assert_match("W19:", res)
563 call assert_match("-Deleted-.*VimEnter", execute('au VimEnter'))
Bram Moolenaarf2c4c392016-07-29 20:50:24 +0200564
565 " check "Another" does not take the pace of the deleted entry
566 augroup Another
567 augroup END
Bram Moolenaar5dc4e2f2020-11-25 14:15:12 +0100568 call assert_match("-Deleted-.*VimEnter", execute('au VimEnter'))
Bram Moolenaaraeac9002016-09-06 22:15:08 +0200569 augroup! Another
Bram Moolenaar5c809082016-09-01 16:21:48 +0200570
571 " no warning for postpone aucmd delete
572 augroup StartOK
573 au VimEnter * call RemoveGroup()
574 augroup END
Bram Moolenaar5dc4e2f2020-11-25 14:15:12 +0100575 call assert_match("StartOK.*VimEnter", execute('au VimEnter'))
Bram Moolenaar5c809082016-09-01 16:21:48 +0200576 redir => res
577 doautocmd VimEnter
578 redir END
Bram Moolenaar5dc4e2f2020-11-25 14:15:12 +0100579 call assert_notmatch("W19:", res)
Bram Moolenaarde653f02016-09-03 16:59:06 +0200580 au! VimEnter
Bram Moolenaarad48e6c2020-04-21 22:19:45 +0200581
582 call assert_fails('augroup!', 'E471:')
Bram Moolenaarf2c4c392016-07-29 20:50:24 +0200583endfunc
Bram Moolenaarb62cc362016-09-03 16:43:53 +0200584
Bram Moolenaar8d84ff12017-10-26 16:42:16 +0200585func Test_BufReadCmdHelp()
586 " This used to cause access to free memory
587 au BufReadCmd * e +h
588 help
589
Bram Moolenaar8d84ff12017-10-26 16:42:16 +0200590 au! BufReadCmd
591endfunc
592
593func Test_BufReadCmdHelpJump()
594 " This used to cause access to free memory
595 au BufReadCmd * e +h{
Bram Moolenaarcf1ba352017-10-27 00:55:04 +0200596 " } to fix highlighting
597 call assert_fails('help', 'E434:')
Bram Moolenaar8d84ff12017-10-26 16:42:16 +0200598
Bram Moolenaar8d84ff12017-10-26 16:42:16 +0200599 au! BufReadCmd
600endfunc
601
zeertzjq93f72cc2022-08-26 15:34:52 +0100602" BufReadCmd is triggered for a "nofile" buffer. Check all values.
Bram Moolenaarb1d2c812022-08-26 11:55:01 +0100603func Test_BufReadCmdNofile()
zeertzjq93f72cc2022-08-26 15:34:52 +0100604 for val in ['nofile',
605 \ 'nowrite',
606 \ 'acwrite',
607 \ 'quickfix',
608 \ 'help',
609 \ 'terminal',
610 \ 'prompt',
611 \ 'popup',
612 \ ]
613 new somefile
614 exe 'set buftype=' .. val
615 au BufReadCmd somefile call setline(1, 'triggered')
616 edit
617 call assert_equal('triggered', getline(1))
Bram Moolenaarb1d2c812022-08-26 11:55:01 +0100618
zeertzjq93f72cc2022-08-26 15:34:52 +0100619 au! BufReadCmd
620 bwipe!
621 endfor
Bram Moolenaarb1d2c812022-08-26 11:55:01 +0100622endfunc
623
Bram Moolenaarb62cc362016-09-03 16:43:53 +0200624func Test_augroup_deleted()
Bram Moolenaarde653f02016-09-03 16:59:06 +0200625 " This caused a crash before E936 was introduced
Bram Moolenaarb62cc362016-09-03 16:43:53 +0200626 augroup x
Bram Moolenaarde653f02016-09-03 16:59:06 +0200627 call assert_fails('augroup! x', 'E936:')
628 au VimEnter * echo
629 augroup end
Bram Moolenaarb62cc362016-09-03 16:43:53 +0200630 augroup! x
Bram Moolenaar5dc4e2f2020-11-25 14:15:12 +0100631 call assert_match("-Deleted-.*VimEnter", execute('au VimEnter'))
Bram Moolenaarde653f02016-09-03 16:59:06 +0200632 au! VimEnter
Bram Moolenaarb62cc362016-09-03 16:43:53 +0200633endfunc
634
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200635" Tests for autocommands on :close command.
636" This used to be in test13.
637func Test_three_windows()
Bram Moolenaarb3435b02016-09-29 20:54:59 +0200638 " Clean up buffers, because in some cases this function fails.
639 call s:cleanup_buffers()
640
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200641 " Write three files and open them, each in a window.
642 " Then go to next window, with autocommand that deletes the previous one.
643 " Do this twice, writing the file.
644 e! Xtestje1
645 call setline(1, 'testje1')
646 w
647 sp Xtestje2
648 call setline(1, 'testje2')
649 w
650 sp Xtestje3
651 call setline(1, 'testje3')
652 w
653 wincmd w
654 au WinLeave Xtestje2 bwipe
655 wincmd w
656 call assert_equal('Xtestje1', expand('%'))
657
658 au WinLeave Xtestje1 bwipe Xtestje3
659 close
660 call assert_equal('Xtestje1', expand('%'))
661
662 " Test deleting the buffer on a Unload event. If this goes wrong there
663 " will be the ATTENTION prompt.
664 e Xtestje1
665 au!
666 au! BufUnload Xtestje1 bwipe
667 call assert_fails('e Xtestje3', 'E937:')
668 call assert_equal('Xtestje3', expand('%'))
669
670 e Xtestje2
671 sp Xtestje1
672 call assert_fails('e', 'E937:')
Bram Moolenaara997b452018-04-17 23:24:06 +0200673 call assert_equal('Xtestje1', expand('%'))
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200674
675 " Test changing buffers in a BufWipeout autocommand. If this goes wrong
676 " there are ml_line errors and/or a Crash.
677 au!
678 only
679 e Xanother
680 e Xtestje1
681 bwipe Xtestje2
682 bwipe Xtestje3
683 au BufWipeout Xtestje1 buf Xtestje1
684 bwipe
685 call assert_equal('Xanother', expand('%'))
686
687 only
688 help
689 wincmd w
690 1quit
691 call assert_equal('Xanother', expand('%'))
692
693 au!
Bram Moolenaar4520d442017-03-19 16:09:46 +0100694 enew
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200695 call delete('Xtestje1')
696 call delete('Xtestje2')
697 call delete('Xtestje3')
698endfunc
Bram Moolenaare13b9af2017-01-13 22:01:02 +0100699
700func Test_BufEnter()
701 au! BufEnter
702 au Bufenter * let val = val . '+'
703 let g:val = ''
704 split NewFile
705 call assert_equal('+', g:val)
706 bwipe!
707 call assert_equal('++', g:val)
708
709 " Also get BufEnter when editing a directory
Bram Moolenaar6f14da12022-09-07 21:30:44 +0100710 call mkdir('Xbufenterdir', 'D')
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +0100711 split Xbufenterdir
Bram Moolenaare13b9af2017-01-13 22:01:02 +0100712 call assert_equal('+++', g:val)
Bram Moolenaare94260f2017-03-21 15:50:12 +0100713
714 " On MS-Windows we can't edit the directory, make sure we wipe the right
715 " buffer.
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +0100716 bwipe! Xbufenterdir
Bram Moolenaare13b9af2017-01-13 22:01:02 +0100717 au! BufEnter
Bram Moolenaara9b5b852022-08-26 13:16:20 +0100718
719 " Editing a "nofile" buffer doesn't read the file but does trigger BufEnter
zeertzjq93f72cc2022-08-26 15:34:52 +0100720 " for historic reasons. Also test other 'buftype' values.
721 for val in ['nofile',
722 \ 'nowrite',
723 \ 'acwrite',
724 \ 'quickfix',
725 \ 'help',
726 \ 'terminal',
727 \ 'prompt',
728 \ 'popup',
729 \ ]
730 new somefile
731 exe 'set buftype=' .. val
732 au BufEnter somefile call setline(1, 'some text')
733 edit
734 call assert_equal('some text', getline(1))
735 bwipe!
736 au! BufEnter
737 endfor
Bram Moolenaare13b9af2017-01-13 22:01:02 +0100738endfunc
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100739
740" Closing a window might cause an endless loop
741" E814 for older Vims
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200742func Test_autocmd_bufwipe_in_SessLoadPost()
Bram Moolenaar1d68d9b2017-10-13 22:33:32 +0200743 edit Xtest
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100744 tabnew
Bram Moolenaar1d68d9b2017-10-13 22:33:32 +0200745 file Xsomething
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100746 set noswapfile
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100747 mksession!
748
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200749 let content =<< trim [CODE]
Bram Moolenaar62cd26a2020-10-11 20:08:44 +0200750 call test_override('ui_delay', 10)
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200751 set nocp noswapfile
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100752 let v:swapchoice = "e"
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200753 augroup test_autocmd_sessionload
754 autocmd!
755 autocmd SessionLoadPost * exe bufnr("Xsomething") . "bw!"
756 augroup END
757
758 func WriteErrors()
759 call writefile([execute("messages")], "Xerrors")
760 endfunc
761 au VimLeave * call WriteErrors()
762 [CODE]
763
Bram Moolenaare1f3ab72022-09-04 21:29:08 +0100764 call writefile(content, 'Xvimrc', 'D')
Bram Moolenaar93344c22019-08-14 21:12:05 +0200765 call system(GetVimCommand('Xvimrc') .. ' --not-a-term --noplugins -S Session.vim -c cq')
Bram Moolenaarae04a602022-09-09 15:08:10 +0100766 sleep 100m
Bram Moolenaare94260f2017-03-21 15:50:12 +0100767 let errors = join(readfile('Xerrors'))
Bram Moolenaare2e40752020-09-04 21:18:46 +0200768 call assert_match('E814:', errors)
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100769
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100770 set swapfile
Bram Moolenaare1f3ab72022-09-04 21:29:08 +0100771 for file in ['Session.vim', 'Xerrors']
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100772 call delete(file)
773 endfor
774endfunc
775
Bram Moolenaar797e63b2021-01-15 16:22:52 +0100776" Using :blast and :ball for many events caused a crash, because b_nwindows was
777" not incremented correctly.
778func Test_autocmd_blast_badd()
779 let content =<< trim [CODE]
780 au BufNew,BufAdd,BufWinEnter,BufEnter,BufLeave,BufWinLeave,BufUnload,VimEnter foo* blast
781 edit foo1
782 au BufNew,BufAdd,BufWinEnter,BufEnter,BufLeave,BufWinLeave,BufUnload,VimEnter foo* ball
783 edit foo2
784 call writefile(['OK'], 'Xerrors')
785 qall
786 [CODE]
787
Bram Moolenaare1f3ab72022-09-04 21:29:08 +0100788 call writefile(content, 'XblastBall', 'D')
Bram Moolenaar797e63b2021-01-15 16:22:52 +0100789 call system(GetVimCommand() .. ' --clean -S XblastBall')
Bram Moolenaarae04a602022-09-09 15:08:10 +0100790 sleep 100m
Bram Moolenaar797e63b2021-01-15 16:22:52 +0100791 call assert_match('OK', readfile('Xerrors')->join())
792
Bram Moolenaar797e63b2021-01-15 16:22:52 +0100793 call delete('Xerrors')
794endfunc
795
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100796" SEGV occurs in older versions.
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200797func Test_autocmd_bufwipe_in_SessLoadPost2()
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100798 tabnew
799 set noswapfile
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100800 mksession!
801
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200802 let content =<< trim [CODE]
803 set nocp noswapfile
804 function! DeleteInactiveBufs()
805 tabfirst
806 let tabblist = []
807 for i in range(1, tabpagenr(''$''))
808 call extend(tabblist, tabpagebuflist(i))
809 endfor
810 for b in range(1, bufnr(''$''))
811 if bufexists(b) && buflisted(b) && (index(tabblist, b) == -1 || bufname(b) =~# ''^$'')
812 exec ''bwipeout '' . b
813 endif
814 endfor
815 echomsg "SessionLoadPost DONE"
816 endfunction
817 au SessionLoadPost * call DeleteInactiveBufs()
818
819 func WriteErrors()
820 call writefile([execute("messages")], "Xerrors")
821 endfunc
822 au VimLeave * call WriteErrors()
823 [CODE]
824
Bram Moolenaare1f3ab72022-09-04 21:29:08 +0100825 call writefile(content, 'Xvimrc', 'D')
Bram Moolenaar93344c22019-08-14 21:12:05 +0200826 call system(GetVimCommand('Xvimrc') .. ' --not-a-term --noplugins -S Session.vim -c cq')
Bram Moolenaarae04a602022-09-09 15:08:10 +0100827 sleep 100m
Bram Moolenaare94260f2017-03-21 15:50:12 +0100828 let errors = join(readfile('Xerrors'))
829 " This probably only ever matches on unix.
830 call assert_notmatch('Caught deadly signal SEGV', errors)
831 call assert_match('SessionLoadPost DONE', errors)
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100832
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100833 set swapfile
Bram Moolenaare1f3ab72022-09-04 21:29:08 +0100834 for file in ['Session.vim', 'Xerrors']
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100835 call delete(file)
836 endfor
837endfunc
Bram Moolenaarfaf29d72017-07-09 11:07:16 +0200838
839func Test_empty_doau()
840 doau \|
841endfunc
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200842
843func s:AutoCommandOptionSet(match)
Bram Moolenaard7c96872019-06-15 17:12:48 +0200844 let template = "Option: <%s>, OldVal: <%s>, OldValLocal: <%s>, OldValGlobal: <%s>, NewVal: <%s>, Scope: <%s>, Command: <%s>\n"
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200845 let item = remove(g:options, 0)
Bram Moolenaard7c96872019-06-15 17:12:48 +0200846 let expected = printf(template, item[0], item[1], item[2], item[3], item[4], item[5], item[6])
847 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 +0200848 let g:opt = [expected, actual]
849 "call assert_equal(expected, actual)
850endfunc
851
852func Test_OptionSet()
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +0200853 CheckOption autochdir
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200854
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +0200855 badd test_autocmd.vim
856
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200857 call test_override('starting', 1)
858 set nocp
859 au OptionSet * :call s:AutoCommandOptionSet(expand("<amatch>"))
860
861 " 1: Setting number option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100862 let g:options = [['number', 0, 0, 0, 1, 'global', 'set']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200863 set nu
864 call assert_equal([], g:options)
865 call assert_equal(g:opt[0], g:opt[1])
866
867 " 2: Setting local number option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100868 let g:options = [['number', 1, 1, '', 0, 'local', 'setlocal']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200869 setlocal nonu
870 call assert_equal([], g:options)
871 call assert_equal(g:opt[0], g:opt[1])
872
873 " 3: Setting global number option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100874 let g:options = [['number', 1, '', 1, 0, 'global', 'setglobal']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200875 setglobal nonu
876 call assert_equal([], g:options)
877 call assert_equal(g:opt[0], g:opt[1])
878
879 " 4: Setting local autoindent option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100880 let g:options = [['autoindent', 0, 0, '', 1, 'local', 'setlocal']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200881 setlocal ai
882 call assert_equal([], g:options)
883 call assert_equal(g:opt[0], g:opt[1])
884
885 " 5: Setting global autoindent option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100886 let g:options = [['autoindent', 0, '', 0, 1, 'global', 'setglobal']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200887 setglobal ai
888 call assert_equal([], g:options)
889 call assert_equal(g:opt[0], g:opt[1])
890
891 " 6: Setting global autoindent option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100892 let g:options = [['autoindent', 1, 1, 1, 0, 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +0200893 set ai!
894 call assert_equal([], g:options)
895 call assert_equal(g:opt[0], g:opt[1])
896
897 " 6a: Setting global autoindent option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100898 let g:options = [['autoindent', 1, 1, 0, 0, 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +0200899 noa setlocal ai
900 noa setglobal noai
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200901 set ai!
902 call assert_equal([], g:options)
903 call assert_equal(g:opt[0], g:opt[1])
904
905 " Should not print anything, use :noa
906 " 7: don't trigger OptionSet"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100907 let g:options = [['invalid', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200908 noa set nonu
Bram Moolenaard7c96872019-06-15 17:12:48 +0200909 call assert_equal([['invalid', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid']], g:options)
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200910 call assert_equal(g:opt[0], g:opt[1])
911
912 " 8: Setting several global list and number option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100913 let g:options = [['list', 0, 0, 0, 1, 'global', 'set'], ['number', 0, 0, 0, 1, 'global', 'set']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200914 set list nu
915 call assert_equal([], g:options)
916 call assert_equal(g:opt[0], g:opt[1])
917
918 " 9: don't trigger OptionSet"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100919 let g:options = [['invalid', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid'], ['invalid', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200920 noa set nolist nonu
Bram Moolenaard7c96872019-06-15 17:12:48 +0200921 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 +0200922 call assert_equal(g:opt[0], g:opt[1])
923
924 " 10: Setting global acd"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100925 let g:options = [['autochdir', 0, 0, '', 1, 'local', 'setlocal']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200926 setlocal acd
927 call assert_equal([], g:options)
928 call assert_equal(g:opt[0], g:opt[1])
929
930 " 11: Setting global autoread (also sets local value)"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100931 let g:options = [['autoread', 0, 0, 0, 1, 'global', 'set']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200932 set ar
933 call assert_equal([], g:options)
934 call assert_equal(g:opt[0], g:opt[1])
935
936 " 12: Setting local autoread"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100937 let g:options = [['autoread', 1, 1, '', 1, 'local', 'setlocal']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200938 setlocal ar
939 call assert_equal([], g:options)
940 call assert_equal(g:opt[0], g:opt[1])
941
942 " 13: Setting global autoread"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100943 let g:options = [['autoread', 1, '', 1, 0, 'global', 'setglobal']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200944 setglobal invar
945 call assert_equal([], g:options)
946 call assert_equal(g:opt[0], g:opt[1])
947
948 " 14: Setting option backspace through :let"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100949 let g:options = [['backspace', '', '', '', 'eol,indent,start', 'global', 'set']]
950 let &bs = "eol,indent,start"
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200951 call assert_equal([], g:options)
952 call assert_equal(g:opt[0], g:opt[1])
953
954 " 15: Setting option backspace through setbufvar()"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100955 let g:options = [['backup', 0, 0, '', 1, 'local', 'setlocal']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200956 " try twice, first time, shouldn't trigger because option name is invalid,
957 " second time, it should trigger
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +0200958 let bnum = bufnr('%')
Bram Moolenaare2e40752020-09-04 21:18:46 +0200959 call assert_fails("call setbufvar(bnum, '&l:bk', 1)", 'E355:')
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200960 " should trigger, use correct option name
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +0200961 call setbufvar(bnum, '&backup', 1)
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200962 call assert_equal([], g:options)
963 call assert_equal(g:opt[0], g:opt[1])
964
965 " 16: Setting number option using setwinvar"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100966 let g:options = [['number', 0, 0, '', 1, 'local', 'setlocal']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200967 call setwinvar(0, '&number', 1)
968 call assert_equal([], g:options)
969 call assert_equal(g:opt[0], g:opt[1])
970
971 " 17: Setting key option, shouldn't trigger"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100972 let g:options = [['key', 'invalid', 'invalid1', 'invalid2', 'invalid3', 'invalid4', 'invalid5']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200973 setlocal key=blah
974 setlocal key=
Bram Moolenaard7c96872019-06-15 17:12:48 +0200975 call assert_equal([['key', 'invalid', 'invalid1', 'invalid2', 'invalid3', 'invalid4', 'invalid5']], g:options)
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200976 call assert_equal(g:opt[0], g:opt[1])
977
Bram Moolenaard7c96872019-06-15 17:12:48 +0200978
979 " 18a: Setting string global option"
980 let oldval = &backupext
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100981 let g:options = [['backupext', oldval, oldval, oldval, 'foo', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +0200982 set backupext=foo
983 call assert_equal([], g:options)
984 call assert_equal(g:opt[0], g:opt[1])
985
986 " 18b: Resetting string global option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100987 let g:options = [['backupext', 'foo', 'foo', 'foo', oldval, 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +0200988 set backupext&
989 call assert_equal([], g:options)
990 call assert_equal(g:opt[0], g:opt[1])
991
992 " 18c: Setting global string global option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100993 let g:options = [['backupext', oldval, '', oldval, 'bar', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +0200994 setglobal backupext=bar
995 call assert_equal([], g:options)
996 call assert_equal(g:opt[0], g:opt[1])
997
998 " 18d: Setting local string global option"
999 " As this is a global option this sets the global value even though
1000 " :setlocal is used!
1001 noa set backupext& " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001002 let g:options = [['backupext', oldval, oldval, '', 'baz', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001003 setlocal backupext=baz
1004 call assert_equal([], g:options)
1005 call assert_equal(g:opt[0], g:opt[1])
1006
1007 " 18e: Setting again string global option"
1008 noa setglobal backupext=ext_global " Reset global and local value (without triggering autocmd)
1009 noa setlocal backupext=ext_local " Sets the global(!) value!
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001010 let g:options = [['backupext', 'ext_local', 'ext_local', 'ext_local', 'fuu', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001011 set backupext=fuu
1012 call assert_equal([], g:options)
1013 call assert_equal(g:opt[0], g:opt[1])
1014
1015
zeertzjqb811de52021-10-21 10:50:44 +01001016 " 19a: Setting string global-local (to buffer) option"
Bram Moolenaar8efa0262017-08-20 15:47:20 +02001017 let oldval = &tags
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001018 let g:options = [['tags', oldval, oldval, oldval, 'tagpath', 'global', 'set']]
Bram Moolenaar8efa0262017-08-20 15:47:20 +02001019 set tags=tagpath
1020 call assert_equal([], g:options)
1021 call assert_equal(g:opt[0], g:opt[1])
1022
zeertzjqb811de52021-10-21 10:50:44 +01001023 " 19b: Resetting string global-local (to buffer) option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001024 let g:options = [['tags', 'tagpath', 'tagpath', 'tagpath', oldval, 'global', 'set']]
Bram Moolenaar8efa0262017-08-20 15:47:20 +02001025 set tags&
1026 call assert_equal([], g:options)
1027 call assert_equal(g:opt[0], g:opt[1])
1028
zeertzjqb811de52021-10-21 10:50:44 +01001029 " 19c: Setting global string global-local (to buffer) option "
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001030 let g:options = [['tags', oldval, '', oldval, 'tagpath1', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001031 setglobal tags=tagpath1
1032 call assert_equal([], g:options)
1033 call assert_equal(g:opt[0], g:opt[1])
1034
zeertzjqb811de52021-10-21 10:50:44 +01001035 " 19d: Setting local string global-local (to buffer) option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001036 let g:options = [['tags', 'tagpath1', 'tagpath1', '', 'tagpath2', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001037 setlocal tags=tagpath2
1038 call assert_equal([], g:options)
1039 call assert_equal(g:opt[0], g:opt[1])
1040
zeertzjqb811de52021-10-21 10:50:44 +01001041 " 19e: Setting again string global-local (to buffer) option"
1042 " Note: v:option_old is the old global value for global-local string options
Bram Moolenaard7c96872019-06-15 17:12:48 +02001043 " but the old local value for all other kinds of options.
1044 noa setglobal tags=tag_global " Reset global and local value (without triggering autocmd)
1045 noa setlocal tags=tag_local
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001046 let g:options = [['tags', 'tag_global', 'tag_local', 'tag_global', 'tagpath', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001047 set tags=tagpath
1048 call assert_equal([], g:options)
1049 call assert_equal(g:opt[0], g:opt[1])
1050
zeertzjqb811de52021-10-21 10:50:44 +01001051 " 19f: Setting string global-local (to buffer) option to an empty string"
1052 " Note: v:option_old is the old global value for global-local string options
Bram Moolenaard7c96872019-06-15 17:12:48 +02001053 " but the old local value for all other kinds of options.
1054 noa set tags=tag_global " Reset global and local value (without triggering autocmd)
1055 noa setlocal tags= " empty string
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001056 let g:options = [['tags', 'tag_global', '', 'tag_global', 'tagpath', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001057 set tags=tagpath
1058 call assert_equal([], g:options)
1059 call assert_equal(g:opt[0], g:opt[1])
1060
1061
1062 " 20a: Setting string local (to buffer) option"
1063 let oldval = &spelllang
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001064 let g:options = [['spelllang', oldval, oldval, oldval, 'elvish,klingon', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001065 set spelllang=elvish,klingon
1066 call assert_equal([], g:options)
1067 call assert_equal(g:opt[0], g:opt[1])
1068
1069 " 20b: Resetting string local (to buffer) option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001070 let g:options = [['spelllang', 'elvish,klingon', 'elvish,klingon', 'elvish,klingon', oldval, 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001071 set spelllang&
1072 call assert_equal([], g:options)
1073 call assert_equal(g:opt[0], g:opt[1])
1074
1075 " 20c: Setting global string local (to buffer) option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001076 let g:options = [['spelllang', oldval, '', oldval, 'elvish', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001077 setglobal spelllang=elvish
1078 call assert_equal([], g:options)
1079 call assert_equal(g:opt[0], g:opt[1])
1080
1081 " 20d: Setting local string local (to buffer) option"
1082 noa set spelllang& " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001083 let g:options = [['spelllang', oldval, oldval, '', 'klingon', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001084 setlocal spelllang=klingon
1085 call assert_equal([], g:options)
1086 call assert_equal(g:opt[0], g:opt[1])
1087
1088 " 20e: Setting again string local (to buffer) option"
zeertzjqb811de52021-10-21 10:50:44 +01001089 " Note: v:option_old is the old global value for global-local string options
Bram Moolenaard7c96872019-06-15 17:12:48 +02001090 " but the old local value for all other kinds of options.
1091 noa setglobal spelllang=spellglobal " Reset global and local value (without triggering autocmd)
1092 noa setlocal spelllang=spelllocal
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001093 let g:options = [['spelllang', 'spelllocal', 'spelllocal', 'spellglobal', 'foo', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001094 set spelllang=foo
1095 call assert_equal([], g:options)
1096 call assert_equal(g:opt[0], g:opt[1])
1097
1098
zeertzjqb811de52021-10-21 10:50:44 +01001099 " 21a: Setting string global-local (to window) option"
Bram Moolenaard7c96872019-06-15 17:12:48 +02001100 let oldval = &statusline
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001101 let g:options = [['statusline', oldval, oldval, oldval, 'foo', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001102 set statusline=foo
1103 call assert_equal([], g:options)
1104 call assert_equal(g:opt[0], g:opt[1])
1105
zeertzjqb811de52021-10-21 10:50:44 +01001106 " 21b: Resetting string global-local (to window) option"
1107 " Note: v:option_old is the old global value for global-local string options
Bram Moolenaard7c96872019-06-15 17:12:48 +02001108 " but the old local value for all other kinds of options.
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001109 let g:options = [['statusline', 'foo', 'foo', 'foo', oldval, 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001110 set statusline&
1111 call assert_equal([], g:options)
1112 call assert_equal(g:opt[0], g:opt[1])
1113
zeertzjqb811de52021-10-21 10:50:44 +01001114 " 21c: Setting global string global-local (to window) option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001115 let g:options = [['statusline', oldval, '', oldval, 'bar', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001116 setglobal statusline=bar
1117 call assert_equal([], g:options)
1118 call assert_equal(g:opt[0], g:opt[1])
1119
zeertzjqb811de52021-10-21 10:50:44 +01001120 " 21d: Setting local string global-local (to window) option"
Bram Moolenaard7c96872019-06-15 17:12:48 +02001121 noa set statusline& " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001122 let g:options = [['statusline', oldval, oldval, '', 'baz', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001123 setlocal statusline=baz
1124 call assert_equal([], g:options)
1125 call assert_equal(g:opt[0], g:opt[1])
1126
zeertzjqb811de52021-10-21 10:50:44 +01001127 " 21e: Setting again string global-local (to window) option"
1128 " Note: v:option_old is the old global value for global-local string options
Bram Moolenaard7c96872019-06-15 17:12:48 +02001129 " but the old local value for all other kinds of options.
1130 noa setglobal statusline=bar " Reset global and local value (without triggering autocmd)
1131 noa setlocal statusline=baz
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001132 let g:options = [['statusline', 'bar', 'baz', 'bar', 'foo', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001133 set statusline=foo
1134 call assert_equal([], g:options)
1135 call assert_equal(g:opt[0], g:opt[1])
1136
1137
1138 " 22a: Setting string local (to window) option"
1139 let oldval = &foldignore
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001140 let g:options = [['foldignore', oldval, oldval, oldval, 'fo', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001141 set foldignore=fo
1142 call assert_equal([], g:options)
1143 call assert_equal(g:opt[0], g:opt[1])
1144
1145 " 22b: Resetting string local (to window) option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001146 let g:options = [['foldignore', 'fo', 'fo', 'fo', oldval, 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001147 set foldignore&
1148 call assert_equal([], g:options)
1149 call assert_equal(g:opt[0], g:opt[1])
1150
1151 " 22c: Setting global string local (to window) option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001152 let g:options = [['foldignore', oldval, '', oldval, 'bar', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001153 setglobal foldignore=bar
1154 call assert_equal([], g:options)
1155 call assert_equal(g:opt[0], g:opt[1])
1156
1157 " 22d: Setting local string local (to window) option"
1158 noa set foldignore& " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001159 let g:options = [['foldignore', oldval, oldval, '', 'baz', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001160 setlocal foldignore=baz
1161 call assert_equal([], g:options)
1162 call assert_equal(g:opt[0], g:opt[1])
1163
1164 " 22e: Setting again string local (to window) option"
1165 noa setglobal foldignore=glob " Reset global and local value (without triggering autocmd)
1166 noa setlocal foldignore=loc
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001167 let g:options = [['foldignore', 'loc', 'loc', 'glob', 'fo', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001168 set foldignore=fo
1169 call assert_equal([], g:options)
1170 call assert_equal(g:opt[0], g:opt[1])
1171
1172
zeertzjqb811de52021-10-21 10:50:44 +01001173 " 23a: Setting global number global option"
Bram Moolenaard7c96872019-06-15 17:12:48 +02001174 noa setglobal cmdheight=8 " Reset global and local value (without triggering autocmd)
1175 noa setlocal cmdheight=1 " Sets the global(!) value!
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001176 let g:options = [['cmdheight', '1', '', '1', '2', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001177 setglobal cmdheight=2
1178 call assert_equal([], g:options)
1179 call assert_equal(g:opt[0], g:opt[1])
1180
1181 " 23b: Setting local number global option"
1182 noa setglobal cmdheight=8 " Reset global and local value (without triggering autocmd)
1183 noa setlocal cmdheight=1 " Sets the global(!) value!
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001184 let g:options = [['cmdheight', '1', '1', '', '2', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001185 setlocal cmdheight=2
1186 call assert_equal([], g:options)
1187 call assert_equal(g:opt[0], g:opt[1])
1188
1189 " 23c: Setting again number global option"
1190 noa setglobal cmdheight=8 " Reset global and local value (without triggering autocmd)
1191 noa setlocal cmdheight=1 " Sets the global(!) value!
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001192 let g:options = [['cmdheight', '1', '1', '1', '2', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001193 set cmdheight=2
1194 call assert_equal([], g:options)
1195 call assert_equal(g:opt[0], g:opt[1])
1196
1197 " 23d: Setting again number global option"
1198 noa set cmdheight=8 " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001199 let g:options = [['cmdheight', '8', '8', '8', '2', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001200 set cmdheight=2
1201 call assert_equal([], g:options)
1202 call assert_equal(g:opt[0], g:opt[1])
1203
1204
1205 " 24a: Setting global number global-local (to buffer) option"
1206 noa setglobal undolevels=8 " Reset global and local value (without triggering autocmd)
1207 noa setlocal undolevels=1
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001208 let g:options = [['undolevels', '8', '', '8', '2', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001209 setglobal undolevels=2
1210 call assert_equal([], g:options)
1211 call assert_equal(g:opt[0], g:opt[1])
1212
1213 " 24b: Setting local number global-local (to buffer) option"
1214 noa setglobal undolevels=8 " Reset global and local value (without triggering autocmd)
1215 noa setlocal undolevels=1
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001216 let g:options = [['undolevels', '1', '1', '', '2', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001217 setlocal undolevels=2
1218 call assert_equal([], g:options)
1219 call assert_equal(g:opt[0], g:opt[1])
1220
1221 " 24c: Setting again number global-local (to buffer) option"
1222 noa setglobal undolevels=8 " Reset global and local value (without triggering autocmd)
1223 noa setlocal undolevels=1
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001224 let g:options = [['undolevels', '1', '1', '8', '2', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001225 set undolevels=2
1226 call assert_equal([], g:options)
1227 call assert_equal(g:opt[0], g:opt[1])
1228
1229 " 24d: Setting again global number global-local (to buffer) option"
1230 noa set undolevels=8 " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001231 let g:options = [['undolevels', '8', '8', '8', '2', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001232 set undolevels=2
1233 call assert_equal([], g:options)
1234 call assert_equal(g:opt[0], g:opt[1])
1235
1236
1237 " 25a: Setting global number local (to buffer) option"
1238 noa setglobal wrapmargin=8 " Reset global and local value (without triggering autocmd)
1239 noa setlocal wrapmargin=1
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001240 let g:options = [['wrapmargin', '8', '', '8', '2', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001241 setglobal wrapmargin=2
1242 call assert_equal([], g:options)
1243 call assert_equal(g:opt[0], g:opt[1])
1244
1245 " 25b: Setting local number local (to buffer) option"
1246 noa setglobal wrapmargin=8 " Reset global and local value (without triggering autocmd)
1247 noa setlocal wrapmargin=1
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001248 let g:options = [['wrapmargin', '1', '1', '', '2', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001249 setlocal wrapmargin=2
1250 call assert_equal([], g:options)
1251 call assert_equal(g:opt[0], g:opt[1])
1252
1253 " 25c: Setting again number local (to buffer) option"
1254 noa setglobal wrapmargin=8 " Reset global and local value (without triggering autocmd)
1255 noa setlocal wrapmargin=1
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001256 let g:options = [['wrapmargin', '1', '1', '8', '2', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001257 set wrapmargin=2
1258 call assert_equal([], g:options)
1259 call assert_equal(g:opt[0], g:opt[1])
1260
1261 " 25d: Setting again global number local (to buffer) option"
1262 noa set wrapmargin=8 " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001263 let g:options = [['wrapmargin', '8', '8', '8', '2', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001264 set wrapmargin=2
1265 call assert_equal([], g:options)
1266 call assert_equal(g:opt[0], g:opt[1])
1267
1268
1269 " 26: Setting number global-local (to window) option.
1270 " Such option does currently not exist.
1271
1272
1273 " 27a: Setting global number local (to window) option"
1274 noa setglobal foldcolumn=8 " Reset global and local value (without triggering autocmd)
1275 noa setlocal foldcolumn=1
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001276 let g:options = [['foldcolumn', '8', '', '8', '2', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001277 setglobal foldcolumn=2
1278 call assert_equal([], g:options)
1279 call assert_equal(g:opt[0], g:opt[1])
1280
1281 " 27b: Setting local number local (to window) option"
1282 noa setglobal foldcolumn=8 " Reset global and local value (without triggering autocmd)
1283 noa setlocal foldcolumn=1
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001284 let g:options = [['foldcolumn', '1', '1', '', '2', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001285 setlocal foldcolumn=2
1286 call assert_equal([], g:options)
1287 call assert_equal(g:opt[0], g:opt[1])
1288
1289 " 27c: Setting again number local (to window) option"
1290 noa setglobal foldcolumn=8 " Reset global and local value (without triggering autocmd)
1291 noa setlocal foldcolumn=1
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001292 let g:options = [['foldcolumn', '1', '1', '8', '2', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001293 set foldcolumn=2
1294 call assert_equal([], g:options)
1295 call assert_equal(g:opt[0], g:opt[1])
1296
zeertzjqb811de52021-10-21 10:50:44 +01001297 " 27d: Setting again global number local (to window) option"
Bram Moolenaard7c96872019-06-15 17:12:48 +02001298 noa set foldcolumn=8 " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001299 let g:options = [['foldcolumn', '8', '8', '8', '2', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001300 set foldcolumn=2
1301 call assert_equal([], g:options)
1302 call assert_equal(g:opt[0], g:opt[1])
1303
1304
1305 " 28a: Setting global boolean global option"
1306 noa setglobal nowrapscan " Reset global and local value (without triggering autocmd)
1307 noa setlocal wrapscan " Sets the global(!) value!
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001308 let g:options = [['wrapscan', '1', '', '1', '0', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001309 setglobal nowrapscan
1310 call assert_equal([], g:options)
1311 call assert_equal(g:opt[0], g:opt[1])
1312
1313 " 28b: Setting local boolean global option"
1314 noa setglobal nowrapscan " Reset global and local value (without triggering autocmd)
1315 noa setlocal wrapscan " Sets the global(!) value!
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001316 let g:options = [['wrapscan', '1', '1', '', '0', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001317 setlocal nowrapscan
1318 call assert_equal([], g:options)
1319 call assert_equal(g:opt[0], g:opt[1])
1320
1321 " 28c: Setting again boolean global option"
1322 noa setglobal nowrapscan " Reset global and local value (without triggering autocmd)
1323 noa setlocal wrapscan " Sets the global(!) value!
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001324 let g:options = [['wrapscan', '1', '1', '1', '0', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001325 set nowrapscan
1326 call assert_equal([], g:options)
1327 call assert_equal(g:opt[0], g:opt[1])
1328
1329 " 28d: Setting again global boolean global option"
1330 noa set nowrapscan " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001331 let g:options = [['wrapscan', '0', '0', '0', '1', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001332 set wrapscan
1333 call assert_equal([], g:options)
1334 call assert_equal(g:opt[0], g:opt[1])
1335
1336
1337 " 29a: Setting global boolean global-local (to buffer) option"
1338 noa setglobal noautoread " Reset global and local value (without triggering autocmd)
1339 noa setlocal autoread
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001340 let g:options = [['autoread', '0', '', '0', '1', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001341 setglobal autoread
1342 call assert_equal([], g:options)
1343 call assert_equal(g:opt[0], g:opt[1])
1344
1345 " 29b: Setting local boolean global-local (to buffer) option"
1346 noa setglobal noautoread " Reset global and local value (without triggering autocmd)
1347 noa setlocal autoread
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001348 let g:options = [['autoread', '1', '1', '', '0', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001349 setlocal noautoread
1350 call assert_equal([], g:options)
1351 call assert_equal(g:opt[0], g:opt[1])
1352
1353 " 29c: Setting again boolean global-local (to buffer) option"
1354 noa setglobal noautoread " Reset global and local value (without triggering autocmd)
1355 noa setlocal autoread
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001356 let g:options = [['autoread', '1', '1', '0', '1', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001357 set autoread
1358 call assert_equal([], g:options)
1359 call assert_equal(g:opt[0], g:opt[1])
1360
1361 " 29d: Setting again global boolean global-local (to buffer) option"
1362 noa set noautoread " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001363 let g:options = [['autoread', '0', '0', '0', '1', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001364 set autoread
1365 call assert_equal([], g:options)
1366 call assert_equal(g:opt[0], g:opt[1])
1367
1368
1369 " 30a: Setting global boolean local (to buffer) option"
1370 noa setglobal nocindent " Reset global and local value (without triggering autocmd)
1371 noa setlocal cindent
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001372 let g:options = [['cindent', '0', '', '0', '1', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001373 setglobal cindent
1374 call assert_equal([], g:options)
1375 call assert_equal(g:opt[0], g:opt[1])
1376
1377 " 30b: Setting local boolean local (to buffer) option"
1378 noa setglobal nocindent " Reset global and local value (without triggering autocmd)
1379 noa setlocal cindent
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001380 let g:options = [['cindent', '1', '1', '', '0', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001381 setlocal nocindent
1382 call assert_equal([], g:options)
1383 call assert_equal(g:opt[0], g:opt[1])
1384
1385 " 30c: Setting again boolean local (to buffer) option"
1386 noa setglobal nocindent " Reset global and local value (without triggering autocmd)
1387 noa setlocal cindent
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001388 let g:options = [['cindent', '1', '1', '0', '1', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001389 set cindent
1390 call assert_equal([], g:options)
1391 call assert_equal(g:opt[0], g:opt[1])
1392
1393 " 30d: Setting again global boolean local (to buffer) option"
1394 noa set nocindent " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001395 let g:options = [['cindent', '0', '0', '0', '1', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001396 set cindent
1397 call assert_equal([], g:options)
1398 call assert_equal(g:opt[0], g:opt[1])
1399
1400
1401 " 31: Setting boolean global-local (to window) option
1402 " Currently no such option exists.
1403
1404
1405 " 32a: Setting global boolean local (to window) option"
1406 noa setglobal nocursorcolumn " Reset global and local value (without triggering autocmd)
1407 noa setlocal cursorcolumn
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001408 let g:options = [['cursorcolumn', '0', '', '0', '1', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001409 setglobal cursorcolumn
1410 call assert_equal([], g:options)
1411 call assert_equal(g:opt[0], g:opt[1])
1412
1413 " 32b: Setting local boolean local (to window) option"
1414 noa setglobal nocursorcolumn " Reset global and local value (without triggering autocmd)
1415 noa setlocal cursorcolumn
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001416 let g:options = [['cursorcolumn', '1', '1', '', '0', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001417 setlocal nocursorcolumn
1418 call assert_equal([], g:options)
1419 call assert_equal(g:opt[0], g:opt[1])
1420
1421 " 32c: Setting again boolean local (to window) option"
1422 noa setglobal nocursorcolumn " Reset global and local value (without triggering autocmd)
1423 noa setlocal cursorcolumn
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001424 let g:options = [['cursorcolumn', '1', '1', '0', '1', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001425 set cursorcolumn
1426 call assert_equal([], g:options)
1427 call assert_equal(g:opt[0], g:opt[1])
1428
1429 " 32d: Setting again global boolean local (to window) option"
1430 noa set nocursorcolumn " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001431 let g:options = [['cursorcolumn', '0', '0', '0', '1', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001432 set cursorcolumn
1433 call assert_equal([], g:options)
1434 call assert_equal(g:opt[0], g:opt[1])
1435
1436
Bram Moolenaar1bc353b2019-09-01 14:45:28 +02001437 " 33: Test autocommands when an option value is converted internally.
Bram Moolenaard7c96872019-06-15 17:12:48 +02001438 noa set backspace=1 " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001439 let g:options = [['backspace', 'indent,eol', 'indent,eol', 'indent,eol', '2', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001440 set backspace=2
1441 call assert_equal([], g:options)
1442 call assert_equal(g:opt[0], g:opt[1])
1443
1444
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001445 " Cleanup
1446 au! OptionSet
Bram Moolenaar0331faf2019-06-15 18:40:37 +02001447 " set tags&
Bram Moolenaard7c96872019-06-15 17:12:48 +02001448 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 +02001449 exe printf(":set %s&vim", opt)
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001450 endfor
1451 call test_override('starting', 0)
1452 delfunc! AutoCommandOptionSet
1453endfunc
1454
1455func Test_OptionSet_diffmode()
1456 call test_override('starting', 1)
Bram Moolenaar26d98212019-01-27 22:32:55 +01001457 " 18: Changing an option when entering diff mode
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001458 new
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001459 au OptionSet diff :let &l:cul = v:option_new
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001460
1461 call setline(1, ['buffer 1', 'line2', 'line3', 'line4'])
1462 call assert_equal(0, &l:cul)
1463 diffthis
1464 call assert_equal(1, &l:cul)
1465
1466 vnew
1467 call setline(1, ['buffer 2', 'line 2', 'line 3', 'line4'])
1468 call assert_equal(0, &l:cul)
1469 diffthis
1470 call assert_equal(1, &l:cul)
1471
1472 diffoff
1473 call assert_equal(0, &l:cul)
1474 call assert_equal(1, getwinvar(2, '&l:cul'))
1475 bw!
1476
1477 call assert_equal(1, &l:cul)
1478 diffoff!
1479 call assert_equal(0, &l:cul)
1480 call assert_equal(0, getwinvar(1, '&l:cul'))
1481 bw!
1482
1483 " Cleanup
1484 au! OptionSet
1485 call test_override('starting', 0)
1486endfunc
1487
1488func Test_OptionSet_diffmode_close()
1489 call test_override('starting', 1)
1490 " 19: Try to close the current window when entering diff mode
1491 " should not segfault
1492 new
1493 au OptionSet diff close
1494
1495 call setline(1, ['buffer 1', 'line2', 'line3', 'line4'])
Bram Moolenaare2e40752020-09-04 21:18:46 +02001496 call assert_fails(':diffthis', 'E788:')
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001497 call assert_equal(1, &diff)
1498 vnew
1499 call setline(1, ['buffer 2', 'line 2', 'line 3', 'line4'])
Bram Moolenaare2e40752020-09-04 21:18:46 +02001500 call assert_fails(':diffthis', 'E788:')
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001501 call assert_equal(1, &diff)
Bram Moolenaara9aa86f2019-11-10 21:25:45 +01001502 set diffopt-=closeoff
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001503 bw!
Bram Moolenaare2e40752020-09-04 21:18:46 +02001504 call assert_fails(':diffoff!', 'E788:')
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001505 bw!
1506
1507 " Cleanup
1508 au! OptionSet
1509 call test_override('starting', 0)
1510 "delfunc! AutoCommandOptionSet
1511endfunc
Bram Moolenaar4a137b42017-08-04 22:37:11 +02001512
1513" Test for Bufleave autocommand that deletes the buffer we are about to edit.
1514func Test_BufleaveWithDelete()
Bram Moolenaare7cda972022-08-29 11:02:59 +01001515 new | edit XbufLeave1
Bram Moolenaar4a137b42017-08-04 22:37:11 +02001516
1517 augroup test_bufleavewithdelete
1518 autocmd!
Bram Moolenaare7cda972022-08-29 11:02:59 +01001519 autocmd BufLeave XbufLeave1 bwipe XbufLeave2
Bram Moolenaar4a137b42017-08-04 22:37:11 +02001520 augroup END
1521
Bram Moolenaare7cda972022-08-29 11:02:59 +01001522 call assert_fails('edit XbufLeave2', 'E143:')
1523 call assert_equal('XbufLeave1', bufname('%'))
Bram Moolenaar4a137b42017-08-04 22:37:11 +02001524
Bram Moolenaare7cda972022-08-29 11:02:59 +01001525 autocmd! test_bufleavewithdelete BufLeave XbufLeave1
Bram Moolenaar4a137b42017-08-04 22:37:11 +02001526 augroup! test_bufleavewithdelete
1527
1528 new
Bram Moolenaare7cda972022-08-29 11:02:59 +01001529 bwipe! XbufLeave1
Bram Moolenaar4a137b42017-08-04 22:37:11 +02001530endfunc
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +02001531
1532" Test for autocommand that changes the buffer list, when doing ":ball".
1533func Test_Acmd_BufAll()
1534 enew!
1535 %bwipe!
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01001536 call writefile(['Test file Xxx1'], 'Xxx1', 'D')
1537 call writefile(['Test file Xxx2'], 'Xxx2', 'D')
1538 call writefile(['Test file Xxx3'], 'Xxx3', 'D')
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +02001539
1540 " Add three files to the buffer list
1541 split Xxx1
1542 close
1543 split Xxx2
1544 close
1545 split Xxx3
1546 close
1547
1548 " Wipe the buffer when the buffer is opened
1549 au BufReadPost Xxx2 bwipe
1550
1551 call append(0, 'Test file Xxx4')
1552 ball
1553
1554 call assert_equal(2, winnr('$'))
1555 call assert_equal('Xxx1', bufname(winbufnr(winnr('$'))))
1556 wincmd t
1557
1558 au! BufReadPost
1559 %bwipe!
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +02001560 enew! | only
1561endfunc
1562
1563" Test for autocommand that changes current buffer on BufEnter event.
1564" Check if modelines are interpreted for the correct buffer.
1565func Test_Acmd_BufEnter()
1566 %bwipe!
1567 call writefile(['start of test file Xxx1',
1568 \ "\<Tab>this is a test",
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01001569 \ 'end of test file Xxx1'], 'Xxx1', 'D')
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +02001570 call writefile(['start of test file Xxx2',
1571 \ 'vim: set noai :',
1572 \ "\<Tab>this is a test",
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01001573 \ 'end of test file Xxx2'], 'Xxx2', 'D')
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +02001574
1575 au BufEnter Xxx2 brew
1576 set ai modeline modelines=3
1577 edit Xxx1
1578 " edit Xxx2, autocmd will do :brew
1579 edit Xxx2
1580 exe "normal G?this is a\<CR>"
1581 " Append text with autoindent to this file
1582 normal othis should be auto-indented
1583 call assert_equal("\<Tab>this should be auto-indented", getline('.'))
1584 call assert_equal(3, line('.'))
1585 " Remove autocmd and edit Xxx2 again
1586 au! BufEnter Xxx2
1587 buf! Xxx2
1588 exe "normal G?this is a\<CR>"
1589 " append text without autoindent to Xxx
1590 normal othis should be in column 1
1591 call assert_equal("this should be in column 1", getline('.'))
1592 call assert_equal(4, line('.'))
1593
1594 %bwipe!
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +02001595 set ai&vim modeline&vim modelines&vim
1596endfunc
1597
1598" Test for issue #57
1599" do not move cursor on <c-o> when autoindent is set
1600func Test_ai_CTRL_O()
1601 enew!
1602 set ai
1603 let save_fo = &fo
1604 set fo+=r
1605 exe "normal o# abcdef\<Esc>2hi\<CR>\<C-O>d0\<Esc>"
1606 exe "normal o# abcdef\<Esc>2hi\<C-O>d0\<Esc>"
1607 call assert_equal(['# abc', 'def', 'def'], getline(2, 4))
1608
1609 set ai&vim
1610 let &fo = save_fo
1611 enew!
1612endfunc
1613
1614" Test for autocommand that deletes the current buffer on BufLeave event.
1615" Also test deleting the last buffer, should give a new, empty buffer.
1616func Test_BufLeave_Wipe()
1617 %bwipe!
1618 let content = ['start of test file Xxx',
1619 \ 'this is a test',
1620 \ 'end of test file Xxx']
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01001621 call writefile(content, 'Xxx1', 'D')
1622 call writefile(content, 'Xxx2', 'D')
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +02001623
1624 au BufLeave Xxx2 bwipe
1625 edit Xxx1
1626 split Xxx2
1627 " delete buffer Xxx2, we should be back to Xxx1
1628 bwipe
1629 call assert_equal('Xxx1', bufname('%'))
1630 call assert_equal(1, winnr('$'))
1631
1632 " Create an alternate buffer
1633 %write! test.out
1634 call assert_equal('test.out', bufname('#'))
1635 " delete alternate buffer
1636 bwipe test.out
1637 call assert_equal('Xxx1', bufname('%'))
1638 call assert_equal('', bufname('#'))
1639
1640 au BufLeave Xxx1 bwipe
1641 " delete current buffer, get an empty one
1642 bwipe!
1643 call assert_equal(1, line('$'))
1644 call assert_equal('', bufname('%'))
Bram Moolenaarb2c87502017-10-14 21:15:58 +02001645 let g:bufinfo = getbufinfo()
1646 call assert_equal(1, len(g:bufinfo))
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +02001647
Bram Moolenaar53f0c962017-10-22 14:23:59 +02001648 call delete('test.out')
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +02001649 %bwipe
1650 au! BufLeave
Bram Moolenaarb2c87502017-10-14 21:15:58 +02001651
1652 " check that bufinfo doesn't contain a pointer to freed memory
1653 call test_garbagecollect_now()
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +02001654endfunc
Bram Moolenaar87ffb5c2017-10-19 12:37:42 +02001655
1656func Test_QuitPre()
1657 edit Xfoo
1658 let winid = win_getid(winnr())
1659 split Xbar
1660 au! QuitPre * let g:afile = expand('<afile>')
1661 " Close the other window, <afile> should be correct.
1662 exe win_id2win(winid) . 'q'
1663 call assert_equal('Xfoo', g:afile)
LemonBoy66e13ae2022-04-21 22:52:11 +01001664
Bram Moolenaar87ffb5c2017-10-19 12:37:42 +02001665 unlet g:afile
1666 bwipe Xfoo
1667 bwipe Xbar
1668endfunc
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02001669
1670func Test_Cmdline()
Bram Moolenaar153b7042018-01-31 15:48:32 +01001671 au! CmdlineChanged : let g:text = getcmdline()
1672 let g:text = 0
1673 call feedkeys(":echom 'hello'\<CR>", 'xt')
1674 call assert_equal("echom 'hello'", g:text)
1675 au! CmdlineChanged
1676
1677 au! CmdlineChanged : let g:entered = expand('<afile>')
1678 let g:entered = 0
1679 call feedkeys(":echom 'hello'\<CR>", 'xt')
1680 call assert_equal(':', g:entered)
1681 au! CmdlineChanged
1682
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02001683 au! CmdlineEnter : let g:entered = expand('<afile>')
1684 au! CmdlineLeave : let g:left = expand('<afile>')
1685 let g:entered = 0
1686 let g:left = 0
1687 call feedkeys(":echo 'hello'\<CR>", 'xt')
1688 call assert_equal(':', g:entered)
1689 call assert_equal(':', g:left)
1690 au! CmdlineEnter
1691 au! CmdlineLeave
1692
Bram Moolenaara4baf5b2018-04-22 13:27:44 +02001693 let save_shellslash = &shellslash
1694 set noshellslash
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02001695 au! CmdlineEnter / let g:entered = expand('<afile>')
1696 au! CmdlineLeave / let g:left = expand('<afile>')
1697 let g:entered = 0
1698 let g:left = 0
Bram Moolenaar53f0c962017-10-22 14:23:59 +02001699 new
1700 call setline(1, 'hello')
1701 call feedkeys("/hello\<CR>", 'xt')
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02001702 call assert_equal('/', g:entered)
1703 call assert_equal('/', g:left)
Bram Moolenaar53f0c962017-10-22 14:23:59 +02001704 bwipe!
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02001705 au! CmdlineEnter
1706 au! CmdlineLeave
Bram Moolenaara4baf5b2018-04-22 13:27:44 +02001707 let &shellslash = save_shellslash
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02001708endfunc
Bram Moolenaar53f0c962017-10-22 14:23:59 +02001709
1710" Test for BufWritePre autocommand that deletes or unloads the buffer.
1711func Test_BufWritePre()
1712 %bwipe
1713 au BufWritePre Xxx1 bunload
1714 au BufWritePre Xxx2 bwipe
1715
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01001716 call writefile(['start of Xxx1', 'test', 'end of Xxx1'], 'Xxx1', 'D')
1717 call writefile(['start of Xxx2', 'test', 'end of Xxx2'], 'Xxx2', 'D')
Bram Moolenaar53f0c962017-10-22 14:23:59 +02001718
1719 edit Xtest
1720 e! Xxx2
1721 bdel Xtest
1722 e Xxx1
1723 " write it, will unload it and give an error msg
Bram Moolenaare2e40752020-09-04 21:18:46 +02001724 call assert_fails('w', 'E203:')
Bram Moolenaar53f0c962017-10-22 14:23:59 +02001725 call assert_equal('Xxx2', bufname('%'))
1726 edit Xtest
1727 e! Xxx2
1728 bwipe Xtest
1729 " write it, will delete the buffer and give an error msg
Bram Moolenaare2e40752020-09-04 21:18:46 +02001730 call assert_fails('w', 'E203:')
Bram Moolenaar53f0c962017-10-22 14:23:59 +02001731 call assert_equal('Xxx1', bufname('%'))
1732 au! BufWritePre
Bram Moolenaar53f0c962017-10-22 14:23:59 +02001733endfunc
1734
1735" Test for BufUnload autocommand that unloads all the other buffers
1736func Test_bufunload_all()
Bram Moolenaarf08b0eb2021-10-16 13:00:14 +01001737 let g:test_is_flaky = 1
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01001738 call writefile(['Test file Xxx1'], 'Xxx1', 'D')"
1739 call writefile(['Test file Xxx2'], 'Xxx2', 'D')"
Bram Moolenaar53f0c962017-10-22 14:23:59 +02001740
Bram Moolenaarc79745a2019-05-20 22:12:34 +02001741 let content =<< trim [CODE]
1742 func UnloadAllBufs()
1743 let i = 1
1744 while i <= bufnr('$')
1745 if i != bufnr('%') && bufloaded(i)
1746 exe i . 'bunload'
1747 endif
1748 let i += 1
1749 endwhile
1750 endfunc
1751 au BufUnload * call UnloadAllBufs()
1752 au VimLeave * call writefile(['Test Finished'], 'Xout')
1753 edit Xxx1
1754 split Xxx2
1755 q
1756 [CODE]
1757
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01001758 call writefile(content, 'Xbunloadtest', 'D')
Bram Moolenaar53f0c962017-10-22 14:23:59 +02001759
1760 call delete('Xout')
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01001761 call system(GetVimCommandClean() .. ' -N --not-a-term -S Xbunloadtest')
Bram Moolenaar53f0c962017-10-22 14:23:59 +02001762 call assert_true(filereadable('Xout'))
1763
Bram Moolenaar53f0c962017-10-22 14:23:59 +02001764 call delete('Xout')
1765endfunc
1766
1767" Some tests for buffer-local autocommands
1768func Test_buflocal_autocmd()
1769 let g:bname = ''
1770 edit xx
1771 au BufLeave <buffer> let g:bname = expand("%")
1772 " here, autocommand for xx should trigger.
1773 " but autocommand shall not apply to buffer named <buffer>.
1774 edit somefile
1775 call assert_equal('xx', g:bname)
1776 let g:bname = ''
1777 " here, autocommand shall be auto-deleted
1778 bwipe xx
1779 " autocmd should not trigger
1780 edit xx
1781 call assert_equal('', g:bname)
1782 " autocmd should not trigger
1783 edit somefile
1784 call assert_equal('', g:bname)
1785 enew
1786 unlet g:bname
1787endfunc
Bram Moolenaar430dc5d2017-11-02 21:04:47 +01001788
1789" Test for "*Cmd" autocommands
1790func Test_Cmd_Autocmds()
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01001791 call writefile(['start of Xxx', "\tabc2", 'end of Xxx'], 'Xxx', 'D')
Bram Moolenaar430dc5d2017-11-02 21:04:47 +01001792
1793 enew!
1794 au BufReadCmd XtestA 0r Xxx|$del
1795 edit XtestA " will read text of Xxd instead
1796 call assert_equal('start of Xxx', getline(1))
1797
1798 au BufWriteCmd XtestA call append(line("$"), "write")
1799 write " will append a line to the file
1800 call assert_equal('write', getline('$'))
Bram Moolenaare2e40752020-09-04 21:18:46 +02001801 call assert_fails('read XtestA', 'E484:') " should not read anything
Bram Moolenaar430dc5d2017-11-02 21:04:47 +01001802 call assert_equal('write', getline(4))
1803
1804 " now we have:
1805 " 1 start of Xxx
1806 " 2 abc2
1807 " 3 end of Xxx
1808 " 4 write
1809
1810 au FileReadCmd XtestB '[r Xxx
1811 2r XtestB " will read Xxx below line 2 instead
1812 call assert_equal('start of Xxx', getline(3))
1813
1814 " now we have:
1815 " 1 start of Xxx
1816 " 2 abc2
1817 " 3 start of Xxx
1818 " 4 abc2
1819 " 5 end of Xxx
1820 " 6 end of Xxx
1821 " 7 write
1822
1823 au FileWriteCmd XtestC '[,']copy $
1824 normal 4GA1
1825 4,5w XtestC " will copy lines 4 and 5 to the end
1826 call assert_equal("\tabc21", getline(8))
Bram Moolenaare2e40752020-09-04 21:18:46 +02001827 call assert_fails('r XtestC', 'E484:') " should not read anything
Bram Moolenaar430dc5d2017-11-02 21:04:47 +01001828 call assert_equal("end of Xxx", getline(9))
1829
1830 " now we have:
1831 " 1 start of Xxx
1832 " 2 abc2
1833 " 3 start of Xxx
1834 " 4 abc21
1835 " 5 end of Xxx
1836 " 6 end of Xxx
1837 " 7 write
1838 " 8 abc21
1839 " 9 end of Xxx
1840
1841 let g:lines = []
1842 au FileAppendCmd XtestD call extend(g:lines, getline(line("'["), line("']")))
1843 w >>XtestD " will add lines to 'lines'
1844 call assert_equal(9, len(g:lines))
Bram Moolenaare2e40752020-09-04 21:18:46 +02001845 call assert_fails('$r XtestD', 'E484:') " should not read anything
Bram Moolenaar430dc5d2017-11-02 21:04:47 +01001846 call assert_equal(9, line('$'))
1847 call assert_equal('end of Xxx', getline('$'))
1848
1849 au BufReadCmd XtestE 0r Xxx|$del
1850 sp XtestE " split window with test.out
1851 call assert_equal('end of Xxx', getline(3))
1852
1853 let g:lines = []
1854 exe "normal 2Goasdf\<Esc>\<C-W>\<C-W>"
1855 au BufWriteCmd XtestE call extend(g:lines, getline(0, '$'))
1856 wall " will write other window to 'lines'
1857 call assert_equal(4, len(g:lines), g:lines)
1858 call assert_equal('asdf', g:lines[2])
1859
1860 au! BufReadCmd
1861 au! BufWriteCmd
1862 au! FileReadCmd
1863 au! FileWriteCmd
1864 au! FileAppendCmd
1865 %bwipe!
Bram Moolenaar430dc5d2017-11-02 21:04:47 +01001866 enew!
1867endfunc
Bram Moolenaaraace2152017-11-05 16:23:10 +01001868
Bram Moolenaar0fff4412020-03-29 16:06:29 +02001869func s:ReadFile()
1870 setl noswapfile nomodified
1871 let filename = resolve(expand("<afile>:p"))
1872 execute 'read' fnameescape(filename)
1873 1d_
1874 exe 'file' fnameescape(filename)
1875 setl buftype=acwrite
1876endfunc
1877
1878func s:WriteFile()
1879 let filename = resolve(expand("<afile>:p"))
1880 setl buftype=
1881 noautocmd execute 'write' fnameescape(filename)
1882 setl buftype=acwrite
1883 setl nomodified
1884endfunc
1885
1886func Test_BufReadCmd()
1887 autocmd BufReadCmd *.test call s:ReadFile()
1888 autocmd BufWriteCmd *.test call s:WriteFile()
1889
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01001890 call writefile(['one', 'two', 'three'], 'Xcmd.test', 'D')
Bram Moolenaar0fff4412020-03-29 16:06:29 +02001891 edit Xcmd.test
1892 call assert_match('Xcmd.test" line 1 of 3', execute('file'))
1893 normal! Gofour
1894 write
1895 call assert_equal(['one', 'two', 'three', 'four'], readfile('Xcmd.test'))
1896
1897 bwipe!
Bram Moolenaar0fff4412020-03-29 16:06:29 +02001898 au! BufReadCmd
1899 au! BufWriteCmd
1900endfunc
1901
zeertzjq9c8f9462022-08-30 18:17:15 +01001902func Test_BufWriteCmd()
1903 autocmd BufWriteCmd Xbufwritecmd let g:written = 1
1904 new
1905 file Xbufwritecmd
1906 set buftype=acwrite
Bram Moolenaar6f14da12022-09-07 21:30:44 +01001907 call mkdir('Xbufwritecmd', 'D')
zeertzjq9c8f9462022-08-30 18:17:15 +01001908 write
1909 " BufWriteCmd should be triggered even if a directory has the same name
1910 call assert_equal(1, g:written)
zeertzjq9c8f9462022-08-30 18:17:15 +01001911 unlet g:written
1912 au! BufWriteCmd
1913 bwipe!
1914endfunc
1915
Bram Moolenaaraace2152017-11-05 16:23:10 +01001916func SetChangeMarks(start, end)
Bram Moolenaar97c69432021-01-15 16:45:21 +01001917 exe a:start .. 'mark ['
1918 exe a:end .. 'mark ]'
Bram Moolenaaraace2152017-11-05 16:23:10 +01001919endfunc
1920
1921" Verify the effects of autocmds on '[ and ']
1922func Test_change_mark_in_autocmds()
1923 edit! Xtest
Bram Moolenaar97c69432021-01-15 16:45:21 +01001924 call feedkeys("ia\<CR>b\<CR>c\<CR>d\<C-g>u\<Esc>", 'xtn')
Bram Moolenaaraace2152017-11-05 16:23:10 +01001925
1926 call SetChangeMarks(2, 3)
1927 write
1928 call assert_equal([1, 4], [line("'["), line("']")])
1929
1930 call SetChangeMarks(2, 3)
1931 au BufWritePre * call assert_equal([1, 4], [line("'["), line("']")])
1932 write
1933 au! BufWritePre
1934
Bram Moolenaar14ddd222020-08-05 12:02:40 +02001935 if has('unix')
Bram Moolenaaraace2152017-11-05 16:23:10 +01001936 write XtestFilter
1937 write >> XtestFilter
1938
1939 call SetChangeMarks(2, 3)
1940 " Marks are set to the entire range of the write
1941 au FilterWritePre * call assert_equal([1, 4], [line("'["), line("']")])
1942 " '[ is adjusted to just before the line that will receive the filtered
1943 " data
1944 au FilterReadPre * call assert_equal([4, 4], [line("'["), line("']")])
1945 " The filtered data is read into the buffer, and the source lines are
1946 " still present, so the range is after the source lines
1947 au FilterReadPost * call assert_equal([5, 12], [line("'["), line("']")])
1948 %!cat XtestFilter
1949 " After the filtered data is read, the original lines are deleted
1950 call assert_equal([1, 8], [line("'["), line("']")])
1951 au! FilterWritePre,FilterReadPre,FilterReadPost
1952 undo
1953
1954 call SetChangeMarks(1, 4)
1955 au FilterWritePre * call assert_equal([2, 3], [line("'["), line("']")])
1956 au FilterReadPre * call assert_equal([3, 3], [line("'["), line("']")])
1957 au FilterReadPost * call assert_equal([4, 11], [line("'["), line("']")])
1958 2,3!cat XtestFilter
1959 call assert_equal([2, 9], [line("'["), line("']")])
1960 au! FilterWritePre,FilterReadPre,FilterReadPost
1961 undo
1962
1963 call delete('XtestFilter')
1964 endif
1965
1966 call SetChangeMarks(1, 4)
1967 au FileWritePre * call assert_equal([2, 3], [line("'["), line("']")])
1968 2,3write Xtest2
1969 au! FileWritePre
1970
1971 call SetChangeMarks(2, 3)
1972 au FileAppendPre * call assert_equal([1, 4], [line("'["), line("']")])
1973 write >> Xtest2
1974 au! FileAppendPre
1975
1976 call SetChangeMarks(1, 4)
1977 au FileAppendPre * call assert_equal([2, 3], [line("'["), line("']")])
1978 2,3write >> Xtest2
1979 au! FileAppendPre
1980
1981 call SetChangeMarks(1, 1)
1982 au FileReadPre * call assert_equal([3, 1], [line("'["), line("']")])
1983 au FileReadPost * call assert_equal([4, 11], [line("'["), line("']")])
1984 3read Xtest2
1985 au! FileReadPre,FileReadPost
1986 undo
1987
1988 call SetChangeMarks(4, 4)
1989 " When the line is 0, it's adjusted to 1
1990 au FileReadPre * call assert_equal([1, 4], [line("'["), line("']")])
1991 au FileReadPost * call assert_equal([1, 8], [line("'["), line("']")])
1992 0read Xtest2
1993 au! FileReadPre,FileReadPost
1994 undo
1995
1996 call SetChangeMarks(4, 4)
1997 " When the line is 0, it's adjusted to 1
1998 au FileReadPre * call assert_equal([1, 4], [line("'["), line("']")])
1999 au FileReadPost * call assert_equal([2, 9], [line("'["), line("']")])
2000 1read Xtest2
2001 au! FileReadPre,FileReadPost
2002 undo
2003
2004 bwipe!
2005 call delete('Xtest')
2006 call delete('Xtest2')
2007endfunc
2008
2009func Test_Filter_noshelltemp()
Bram Moolenaaraeb313f2020-11-27 19:13:28 +01002010 CheckExecutable cat
Bram Moolenaaraace2152017-11-05 16:23:10 +01002011
2012 enew!
2013 call setline(1, ['a', 'b', 'c', 'd'])
2014
2015 let shelltemp = &shelltemp
2016 set shelltemp
2017
2018 let g:filter_au = 0
2019 au FilterWritePre * let g:filter_au += 1
2020 au FilterReadPre * let g:filter_au += 1
2021 au FilterReadPost * let g:filter_au += 1
2022 %!cat
2023 call assert_equal(3, g:filter_au)
2024
2025 if has('filterpipe')
2026 set noshelltemp
2027
2028 let g:filter_au = 0
2029 au FilterWritePre * let g:filter_au += 1
2030 au FilterReadPre * let g:filter_au += 1
2031 au FilterReadPost * let g:filter_au += 1
2032 %!cat
2033 call assert_equal(0, g:filter_au)
2034 endif
2035
2036 au! FilterWritePre,FilterReadPre,FilterReadPost
2037 let &shelltemp = shelltemp
2038 bwipe!
2039endfunc
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01002040
2041func Test_TextYankPost()
2042 enew!
2043 call setline(1, ['foo'])
2044
2045 let g:event = []
2046 au TextYankPost * let g:event = copy(v:event)
2047
2048 call assert_equal({}, v:event)
2049 call assert_fails('let v:event = {}', 'E46:')
2050 call assert_fails('let v:event.mykey = 0', 'E742:')
2051
2052 norm "ayiw
2053 call assert_equal(
Bram Moolenaara016eeb2022-04-09 11:37:38 +01002054 \ #{regcontents: ['foo'], regname: 'a', operator: 'y',
2055 \ regtype: 'v', visual: v:false, inclusive: v:true},
2056 \ g:event)
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01002057 norm y_
2058 call assert_equal(
Bram Moolenaara016eeb2022-04-09 11:37:38 +01002059 \ #{regcontents: ['foo'], regname: '', operator: 'y', regtype: 'V',
2060 \ visual: v:false, inclusive: v:false},
2061 \ g:event)
Bram Moolenaar37d16732020-06-12 22:09:01 +02002062 norm Vy
2063 call assert_equal(
Bram Moolenaara016eeb2022-04-09 11:37:38 +01002064 \ #{regcontents: ['foo'], regname: '', operator: 'y', regtype: 'V',
2065 \ visual: v:true, inclusive: v:true},
2066 \ g:event)
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01002067 call feedkeys("\<C-V>y", 'x')
2068 call assert_equal(
Bram Moolenaara016eeb2022-04-09 11:37:38 +01002069 \ #{regcontents: ['f'], regname: '', operator: 'y', regtype: "\x161",
2070 \ visual: v:true, inclusive: v:true},
2071 \ g:event)
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01002072 norm "xciwbar
2073 call assert_equal(
Bram Moolenaara016eeb2022-04-09 11:37:38 +01002074 \ #{regcontents: ['foo'], regname: 'x', operator: 'c', regtype: 'v',
2075 \ visual: v:false, inclusive: v:true},
2076 \ g:event)
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01002077 norm "bdiw
2078 call assert_equal(
Bram Moolenaara016eeb2022-04-09 11:37:38 +01002079 \ #{regcontents: ['bar'], regname: 'b', operator: 'd', regtype: 'v',
2080 \ visual: v:false, inclusive: v:true},
2081 \ g:event)
2082
2083 call setline(1, 'foobar')
2084 " exclusive motion
2085 norm $"ay0
2086 call assert_equal(
2087 \ #{regcontents: ['fooba'], regname: 'a', operator: 'y', regtype: 'v',
2088 \ visual: v:false, inclusive: v:false},
2089 \ g:event)
2090 " inclusive motion
2091 norm 0"ay$
2092 call assert_equal(
2093 \ #{regcontents: ['foobar'], regname: 'a', operator: 'y', regtype: 'v',
2094 \ visual: v:false, inclusive: v:true},
2095 \ g:event)
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01002096
2097 call assert_equal({}, v:event)
2098
Bram Moolenaarfccbf062020-11-26 20:34:00 +01002099 if has('clipboard_working') && !has('gui_running')
2100 " Test that when the visual selection is automatically copied to clipboard
2101 " register a TextYankPost is emitted
2102 call setline(1, ['foobar'])
2103
2104 let @* = ''
2105 set clipboard=autoselect
2106 exe "norm! ggviw\<Esc>"
2107 call assert_equal(
Bram Moolenaara016eeb2022-04-09 11:37:38 +01002108 \ #{regcontents: ['foobar'], regname: '*', operator: 'y',
2109 \ regtype: 'v', visual: v:true, inclusive: v:false},
2110 \ g:event)
Bram Moolenaarfccbf062020-11-26 20:34:00 +01002111
2112 let @+ = ''
2113 set clipboard=autoselectplus
2114 exe "norm! ggviw\<Esc>"
2115 call assert_equal(
Bram Moolenaara016eeb2022-04-09 11:37:38 +01002116 \ #{regcontents: ['foobar'], regname: '+', operator: 'y',
2117 \ regtype: 'v', visual: v:true, inclusive: v:false},
2118 \ g:event)
Bram Moolenaarfccbf062020-11-26 20:34:00 +01002119
2120 set clipboard&vim
2121 endif
2122
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01002123 au! TextYankPost
2124 unlet g:event
2125 bwipe!
2126endfunc
Bram Moolenaar9bca8052017-12-18 12:37:55 +01002127
Bram Moolenaar9a046fd2021-01-28 13:47:59 +01002128func Test_autocommand_all_events()
2129 call assert_fails('au * * bwipe', 'E1155:')
2130 call assert_fails('au * x bwipe', 'E1155:')
Bram Moolenaarb6db1462021-12-24 19:24:47 +00002131 call assert_fails('au! * x bwipe', 'E1155:')
Bram Moolenaar4fb921e2017-12-18 15:33:00 +01002132endfunc
Bram Moolenaarb7407d32018-02-03 17:36:27 +01002133
Bram Moolenaarf6246f52022-02-11 16:30:12 +00002134func Test_autocmd_user()
2135 au User MyEvent let s:res = [expand("<afile>"), expand("<amatch>")]
2136 doautocmd User MyEvent
2137 call assert_equal(['MyEvent', 'MyEvent'], s:res)
2138 au! User
2139 unlet s:res
2140endfunc
2141
Bram Moolenaarb7407d32018-02-03 17:36:27 +01002142function s:Before_test_dirchanged()
2143 augroup test_dirchanged
2144 autocmd!
2145 augroup END
2146 let s:li = []
2147 let s:dir_this = getcwd()
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02002148 let s:dir_foo = s:dir_this . '/Xfoo'
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01002149 call mkdir(s:dir_foo)
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02002150 let s:dir_bar = s:dir_this . '/Xbar'
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01002151 call mkdir(s:dir_bar)
Bram Moolenaarb7407d32018-02-03 17:36:27 +01002152endfunc
2153
2154function s:After_test_dirchanged()
Bram Moolenaar3503d7c2019-11-09 20:10:17 +01002155 call chdir(s:dir_this)
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01002156 call delete(s:dir_foo, 'd')
2157 call delete(s:dir_bar, 'd')
Bram Moolenaarb7407d32018-02-03 17:36:27 +01002158 augroup test_dirchanged
2159 autocmd!
2160 augroup END
2161endfunc
2162
2163function Test_dirchanged_global()
2164 call s:Before_test_dirchanged()
Bram Moolenaarf6246f52022-02-11 16:30:12 +00002165 autocmd test_dirchanged DirChangedPre global call add(s:li, expand("<amatch>") .. " pre cd " .. v:event.directory)
Bram Moolenaarb7407d32018-02-03 17:36:27 +01002166 autocmd test_dirchanged DirChanged global call add(s:li, "cd:")
2167 autocmd test_dirchanged DirChanged global call add(s:li, expand("<afile>"))
Bram Moolenaar3503d7c2019-11-09 20:10:17 +01002168 call chdir(s:dir_foo)
Bram Moolenaarf6246f52022-02-11 16:30:12 +00002169 let expected = ["global pre cd " .. s:dir_foo, "cd:", s:dir_foo]
Bram Moolenaar28e8f732022-02-09 12:58:20 +00002170 call assert_equal(expected, s:li)
Bram Moolenaar3503d7c2019-11-09 20:10:17 +01002171 call chdir(s:dir_foo)
Bram Moolenaar28e8f732022-02-09 12:58:20 +00002172 call assert_equal(expected, s:li)
Bram Moolenaar3503d7c2019-11-09 20:10:17 +01002173 exe 'lcd ' .. fnameescape(s:dir_bar)
Bram Moolenaar28e8f732022-02-09 12:58:20 +00002174 call assert_equal(expected, s:li)
Bram Moolenaard8c9d322022-06-12 11:49:16 +01002175
2176 exe 'cd ' .. s:dir_foo
2177 exe 'cd ' .. s:dir_bar
2178 autocmd! test_dirchanged DirChanged global let g:result = expand("<afile>")
2179 cd -
Bram Moolenaardb77c492022-06-12 23:26:50 +01002180 call assert_equal(s:dir_foo, substitute(g:result, '\\', '/', 'g'))
Bram Moolenaard8c9d322022-06-12 11:49:16 +01002181
Bram Moolenaarb7407d32018-02-03 17:36:27 +01002182 call s:After_test_dirchanged()
2183endfunc
2184
2185function Test_dirchanged_local()
2186 call s:Before_test_dirchanged()
2187 autocmd test_dirchanged DirChanged window call add(s:li, "lcd:")
2188 autocmd test_dirchanged DirChanged window call add(s:li, expand("<afile>"))
Bram Moolenaar3503d7c2019-11-09 20:10:17 +01002189 call chdir(s:dir_foo)
Bram Moolenaarb7407d32018-02-03 17:36:27 +01002190 call assert_equal([], s:li)
Bram Moolenaar3503d7c2019-11-09 20:10:17 +01002191 exe 'lcd ' .. fnameescape(s:dir_bar)
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01002192 call assert_equal(["lcd:", s:dir_bar], s:li)
Bram Moolenaar3503d7c2019-11-09 20:10:17 +01002193 exe 'lcd ' .. fnameescape(s:dir_bar)
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01002194 call assert_equal(["lcd:", s:dir_bar], s:li)
Bram Moolenaarb7407d32018-02-03 17:36:27 +01002195 call s:After_test_dirchanged()
2196endfunc
2197
2198function Test_dirchanged_auto()
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +02002199 CheckOption autochdir
Bram Moolenaarb7407d32018-02-03 17:36:27 +01002200 call s:Before_test_dirchanged()
2201 call test_autochdir()
Bram Moolenaar28e8f732022-02-09 12:58:20 +00002202 autocmd test_dirchanged DirChangedPre auto call add(s:li, "pre cd " .. v:event.directory)
Bram Moolenaarb7407d32018-02-03 17:36:27 +01002203 autocmd test_dirchanged DirChanged auto call add(s:li, "auto:")
2204 autocmd test_dirchanged DirChanged auto call add(s:li, expand("<afile>"))
2205 set acd
Bram Moolenaar3503d7c2019-11-09 20:10:17 +01002206 cd ..
Bram Moolenaarb7407d32018-02-03 17:36:27 +01002207 call assert_equal([], s:li)
Bram Moolenaar61abe7d2022-08-30 21:46:08 +01002208 exe 'edit ' . s:dir_foo . '/Xautofile'
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01002209 call assert_equal(s:dir_foo, getcwd())
Bram Moolenaar28e8f732022-02-09 12:58:20 +00002210 let expected = ["pre cd " .. s:dir_foo, "auto:", s:dir_foo]
2211 call assert_equal(expected, s:li)
Bram Moolenaarb7407d32018-02-03 17:36:27 +01002212 set noacd
2213 bwipe!
2214 call s:After_test_dirchanged()
2215endfunc
Bram Moolenaar5a093432018-02-10 18:15:19 +01002216
2217" Test TextChangedI and TextChangedP
2218func Test_ChangedP()
2219 new
2220 call setline(1, ['foo', 'bar', 'foobar'])
2221 call test_override("char_avail", 1)
2222 set complete=. completeopt=menuone
2223
2224 func! TextChangedAutocmd(char)
2225 let g:autocmd .= a:char
2226 endfunc
2227
Christian Brabandtdb3b4462021-10-16 11:58:55 +01002228 " TextChanged will not be triggered, only check that it isn't.
Bram Moolenaar5a093432018-02-10 18:15:19 +01002229 au! TextChanged <buffer> :call TextChangedAutocmd('N')
2230 au! TextChangedI <buffer> :call TextChangedAutocmd('I')
2231 au! TextChangedP <buffer> :call TextChangedAutocmd('P')
2232
2233 call cursor(3, 1)
2234 let g:autocmd = ''
2235 call feedkeys("o\<esc>", 'tnix')
2236 call assert_equal('I', g:autocmd)
2237
2238 let g:autocmd = ''
2239 call feedkeys("Sf", 'tnix')
2240 call assert_equal('II', g:autocmd)
2241
2242 let g:autocmd = ''
2243 call feedkeys("Sf\<C-N>", 'tnix')
2244 call assert_equal('IIP', g:autocmd)
2245
2246 let g:autocmd = ''
2247 call feedkeys("Sf\<C-N>\<C-N>", 'tnix')
2248 call assert_equal('IIPP', g:autocmd)
2249
2250 let g:autocmd = ''
2251 call feedkeys("Sf\<C-N>\<C-N>\<C-N>", 'tnix')
2252 call assert_equal('IIPPP', g:autocmd)
2253
2254 let g:autocmd = ''
2255 call feedkeys("Sf\<C-N>\<C-N>\<C-N>\<C-N>", 'tnix')
2256 call assert_equal('IIPPPP', g:autocmd)
2257
2258 call assert_equal(['foo', 'bar', 'foobar', 'foo'], getline(1, '$'))
2259 " TODO: how should it handle completeopt=noinsert,noselect?
2260
2261 " CleanUp
2262 call test_override("char_avail", 0)
2263 au! TextChanged
2264 au! TextChangedI
2265 au! TextChangedP
2266 delfu TextChangedAutocmd
2267 unlet! g:autocmd
2268 set complete&vim completeopt&vim
2269
2270 bw!
2271endfunc
Bram Moolenaar8c64a362018-03-23 22:39:31 +01002272
Bram Moolenaar91d2e782018-08-07 19:05:01 +02002273let g:setline_handled = v:false
Bram Moolenaar1e115362019-01-09 23:01:02 +01002274func SetLineOne()
Bram Moolenaar91d2e782018-08-07 19:05:01 +02002275 if !g:setline_handled
2276 call setline(1, "(x)")
2277 let g:setline_handled = v:true
2278 endif
2279endfunc
2280
2281func Test_TextChangedI_with_setline()
2282 new
2283 call test_override('char_avail', 1)
2284 autocmd TextChangedI <buffer> call SetLineOne()
2285 call feedkeys("i(\<CR>\<Esc>", 'tx')
2286 call assert_equal('(', getline(1))
2287 call assert_equal('x)', getline(2))
2288 undo
Bram Moolenaar91d2e782018-08-07 19:05:01 +02002289 call assert_equal('', getline(1))
Bram Moolenaar9fa95062018-08-08 22:08:32 +02002290 call assert_equal('', getline(2))
Bram Moolenaar91d2e782018-08-07 19:05:01 +02002291
Bram Moolenaarca34db32022-01-20 11:17:18 +00002292 call test_override('char_avail', 0)
Bram Moolenaar91d2e782018-08-07 19:05:01 +02002293 bwipe!
2294endfunc
2295
Bram Moolenaar8c64a362018-03-23 22:39:31 +01002296func Test_Changed_FirstTime()
Bram Moolenaar8c5a2782019-08-07 23:07:07 +02002297 CheckFeature terminal
2298 CheckNotGui
Bram Moolenaar3cdcb092020-03-18 19:18:10 +01002299 " Starting a terminal to run Vim is always considered flaky.
Bram Moolenaar30d53e22020-03-18 21:10:44 +01002300 let g:test_is_flaky = 1
Bram Moolenaar8c5a2782019-08-07 23:07:07 +02002301
Bram Moolenaar8c64a362018-03-23 22:39:31 +01002302 " Prepare file for TextChanged event.
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01002303 call writefile([''], 'Xchanged.txt', 'D')
Bram Moolenaar8c64a362018-03-23 22:39:31 +01002304 let buf = term_start([GetVimProg(), '--clean', '-c', 'set noswapfile'], {'term_rows': 3})
2305 call assert_equal('running', term_getstatus(buf))
Bram Moolenaar1834d372018-03-29 17:40:46 +02002306 " Wait for the ruler (in the status line) to be shown.
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01002307 " In ConPTY, there is additional character which is drawn up to the width of
2308 " the screen.
2309 if has('conpty')
2310 call WaitForAssert({-> assert_match('\<All.*$', term_getline(buf, 3))})
2311 else
2312 call WaitForAssert({-> assert_match('\<All$', term_getline(buf, 3))})
2313 endif
Bram Moolenaar8c64a362018-03-23 22:39:31 +01002314 " It's only adding autocmd, so that no event occurs.
2315 call term_sendkeys(buf, ":au! TextChanged <buffer> call writefile(['No'], 'Xchanged.txt')\<cr>")
2316 call term_sendkeys(buf, "\<C-\\>\<C-N>:qa!\<cr>")
Bram Moolenaar50182fa2018-04-28 21:34:40 +02002317 call WaitForAssert({-> assert_equal('finished', term_getstatus(buf))})
Bram Moolenaar8c64a362018-03-23 22:39:31 +01002318 call assert_equal([''], readfile('Xchanged.txt'))
2319
2320 " clean up
Bram Moolenaar8c64a362018-03-23 22:39:31 +01002321 bwipe!
2322endfunc
Bram Moolenaar0566e892019-01-24 19:37:40 +01002323
Bram Moolenaareb93f3f2019-04-04 15:04:56 +02002324func Test_autocmd_nested()
2325 let g:did_nested = 0
2326 augroup Testing
2327 au WinNew * edit somefile
2328 au BufNew * let g:did_nested = 1
2329 augroup END
2330 split
2331 call assert_equal(0, g:did_nested)
2332 close
2333 bwipe! somefile
2334
2335 " old nested argument still works
2336 augroup Testing
2337 au!
2338 au WinNew * nested edit somefile
2339 au BufNew * let g:did_nested = 1
2340 augroup END
2341 split
2342 call assert_equal(1, g:did_nested)
2343 close
2344 bwipe! somefile
2345
2346 " New ++nested argument works
2347 augroup Testing
2348 au!
2349 au WinNew * ++nested edit somefile
2350 au BufNew * let g:did_nested = 1
2351 augroup END
2352 split
2353 call assert_equal(1, g:did_nested)
2354 close
2355 bwipe! somefile
2356
Bram Moolenaarf0775142022-03-04 20:10:38 +00002357 " nested without ++ does not work in Vim9 script
2358 call assert_fails('vim9cmd au WinNew * nested echo fails', 'E1078:')
2359
Bram Moolenaareb93f3f2019-04-04 15:04:56 +02002360 augroup Testing
2361 au!
2362 augroup END
2363
2364 call assert_fails('au WinNew * ++nested ++nested echo bad', 'E983:')
2365 call assert_fails('au WinNew * nested nested echo bad', 'E983:')
2366endfunc
2367
Bram Moolenaar5fa9f232022-07-23 09:06:48 +01002368func Test_autocmd_nested_cursor_invalid()
2369 set laststatus=0
2370 copen
2371 cclose
2372 call setline(1, ['foo', 'bar', 'baz'])
2373 3
2374 augroup nested_inv
2375 autocmd User foo ++nested copen
2376 autocmd BufAdd * let &laststatus = 2 - &laststatus
2377 augroup END
2378 doautocmd User foo
2379
2380 augroup nested_inv
2381 au!
2382 augroup END
2383 set laststatus&
Bram Moolenaarb03950f2022-07-26 13:47:13 +01002384 cclose
Bram Moolenaar5fa9f232022-07-23 09:06:48 +01002385 bwipe!
2386endfunc
2387
Bram Moolenaar3d6ee8b2022-07-27 15:23:35 +01002388func Test_autocmd_nested_keeps_cursor_pos()
2389 enew
2390 call setline(1, 'foo')
2391 autocmd User foo ++nested normal! $a
2392 autocmd InsertLeave * :
2393 doautocmd User foo
2394 call assert_equal([0, 1, 3, 0], getpos('.'))
2395
2396 bwipe!
2397endfunc
2398
Bram Moolenaarb03950f2022-07-26 13:47:13 +01002399func Test_autocmd_nested_switch_window()
2400 " run this in a separate Vim so that SafeState works
2401 CheckRunVimInTerminal
2402
2403 let lines =<< trim END
2404 vim9script
2405 ['()']->writefile('Xautofile')
2406 autocmd VimEnter * ++nested edit Xautofile | split
2407 autocmd BufReadPost * autocmd SafeState * ++once foldclosed('.')
2408 autocmd WinEnter * matchadd('ErrorMsg', 'pat')
2409 END
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01002410 call writefile(lines, 'Xautoscript', 'D')
Bram Moolenaarb03950f2022-07-26 13:47:13 +01002411 let buf = RunVimInTerminal('-S Xautoscript', {'rows': 10})
2412 call VerifyScreenDump(buf, 'Test_autocmd_nested_switch', {})
2413
2414 call StopVimInTerminal(buf)
2415 call delete('Xautofile')
Bram Moolenaarb03950f2022-07-26 13:47:13 +01002416endfunc
2417
Bram Moolenaareb93f3f2019-04-04 15:04:56 +02002418func Test_autocmd_once()
2419 " Without ++once WinNew triggers twice
2420 let g:did_split = 0
2421 augroup Testing
2422 au WinNew * let g:did_split += 1
2423 augroup END
2424 split
2425 split
2426 call assert_equal(2, g:did_split)
2427 call assert_true(exists('#WinNew'))
2428 close
2429 close
2430
2431 " With ++once WinNew triggers once
2432 let g:did_split = 0
2433 augroup Testing
2434 au!
2435 au WinNew * ++once let g:did_split += 1
2436 augroup END
2437 split
2438 split
2439 call assert_equal(1, g:did_split)
2440 call assert_false(exists('#WinNew'))
2441 close
2442 close
2443
2444 call assert_fails('au WinNew * ++once ++once echo bad', 'E983:')
2445endfunc
2446
Bram Moolenaara68e5952019-04-25 22:22:01 +02002447func Test_autocmd_bufreadpre()
2448 new
2449 let b:bufreadpre = 1
Bram Moolenaarab505b12020-03-23 19:28:44 +01002450 call append(0, range(1000))
Bram Moolenaara68e5952019-04-25 22:22:01 +02002451 w! XAutocmdBufReadPre.txt
2452 autocmd BufReadPre <buffer> :let b:bufreadpre += 1
Bram Moolenaarab505b12020-03-23 19:28:44 +01002453 norm! 500gg
Bram Moolenaara68e5952019-04-25 22:22:01 +02002454 sp
Bram Moolenaarab505b12020-03-23 19:28:44 +01002455 norm! 1000gg
Bram Moolenaara68e5952019-04-25 22:22:01 +02002456 wincmd p
2457 let g:wsv1 = winsaveview()
2458 wincmd p
2459 let g:wsv2 = winsaveview()
2460 " triggers BufReadPre, should not move the cursor in either window
2461 " The topline may change one line in a large window.
2462 edit
2463 call assert_inrange(g:wsv2.topline - 1, g:wsv2.topline + 1, winsaveview().topline)
2464 call assert_equal(g:wsv2.lnum, winsaveview().lnum)
2465 call assert_equal(2, b:bufreadpre)
2466 wincmd p
2467 call assert_equal(g:wsv1.topline, winsaveview().topline)
2468 call assert_equal(g:wsv1.lnum, winsaveview().lnum)
2469 call assert_equal(2, b:bufreadpre)
2470 " Now set the cursor position in an BufReadPre autocommand
2471 " (even though the position will be invalid, this should make Vim reset the
2472 " cursor position in the other window.
2473 wincmd p
2474 set cpo+=g
2475 " won't do anything, but try to set the cursor on an invalid lnum
2476 autocmd BufReadPre <buffer> :norm! 70gg
2477 " triggers BufReadPre, should not move the cursor in either window
2478 e
2479 call assert_equal(1, winsaveview().topline)
2480 call assert_equal(1, winsaveview().lnum)
2481 call assert_equal(3, b:bufreadpre)
2482 wincmd p
2483 call assert_equal(g:wsv1.topline, winsaveview().topline)
2484 call assert_equal(g:wsv1.lnum, winsaveview().lnum)
2485 call assert_equal(3, b:bufreadpre)
2486 close
2487 close
2488 call delete('XAutocmdBufReadPre.txt')
2489 set cpo-=g
2490endfunc
2491
Bram Moolenaar5e66b422019-01-24 21:58:10 +01002492" FileChangedShell tested in test_filechanged.vim
Bram Moolenaar69ea5872019-04-25 20:29:00 +02002493
2494" Tests for the following autocommands:
2495" - FileWritePre writing a compressed file
2496" - FileReadPost reading a compressed file
2497" - BufNewFile reading a file template
2498" - BufReadPre decompressing the file to be read
2499" - FilterReadPre substituting characters in the temp file
2500" - FilterReadPost substituting characters after filtering
2501" - FileReadPre set options for decompression
2502" - FileReadPost decompress the file
2503func Test_ReadWrite_Autocmds()
2504 " Run this test only on Unix-like systems and if gzip is available
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +02002505 CheckUnix
2506 CheckExecutable gzip
Bram Moolenaar69ea5872019-04-25 20:29:00 +02002507
2508 " Make $GZIP empty, "-v" would cause trouble.
2509 let $GZIP = ""
2510
2511 " Use a FileChangedShell autocommand to avoid a prompt for 'Xtestfile.gz'
2512 " being modified outside of Vim (noticed on Solaris).
2513 au FileChangedShell * echo 'caught FileChangedShell'
2514
2515 " Test for the FileReadPost, FileWritePre and FileWritePost autocmds
2516 augroup Test1
2517 au!
2518 au FileWritePre *.gz '[,']!gzip
2519 au FileWritePost *.gz undo
2520 au FileReadPost *.gz '[,']!gzip -d
2521 augroup END
2522
2523 new
2524 set bin
2525 call append(0, [
2526 \ 'line 2 Abcdefghijklmnopqrstuvwxyz',
2527 \ 'line 3 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2528 \ 'line 4 Abcdefghijklmnopqrstuvwxyz',
2529 \ 'line 5 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2530 \ 'line 6 Abcdefghijklmnopqrstuvwxyz',
2531 \ 'line 7 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2532 \ 'line 8 Abcdefghijklmnopqrstuvwxyz',
2533 \ 'line 9 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2534 \ 'line 10 Abcdefghijklmnopqrstuvwxyz'
2535 \ ])
2536 1,9write! Xtestfile.gz
2537 enew! | close
2538
2539 new
2540 " Read and decompress the testfile
2541 0read Xtestfile.gz
2542 call assert_equal([
2543 \ 'line 2 Abcdefghijklmnopqrstuvwxyz',
2544 \ 'line 3 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2545 \ 'line 4 Abcdefghijklmnopqrstuvwxyz',
2546 \ 'line 5 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2547 \ 'line 6 Abcdefghijklmnopqrstuvwxyz',
2548 \ 'line 7 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2549 \ 'line 8 Abcdefghijklmnopqrstuvwxyz',
2550 \ 'line 9 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2551 \ 'line 10 Abcdefghijklmnopqrstuvwxyz'
2552 \ ], getline(1, 9))
2553 enew! | close
2554
2555 augroup Test1
2556 au!
2557 augroup END
2558
2559 " Test for the FileAppendPre and FileAppendPost autocmds
2560 augroup Test2
2561 au!
2562 au BufNewFile *.c read Xtest.c
2563 au FileAppendPre *.out '[,']s/new/NEW/
2564 au FileAppendPost *.out !cat Xtest.c >> test.out
2565 augroup END
2566
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01002567 call writefile(['/*', ' * Here is a new .c file', ' */'], 'Xtest.c', 'D')
Bram Moolenaar69ea5872019-04-25 20:29:00 +02002568 new foo.c " should load Xtest.c
2569 call assert_equal(['/*', ' * Here is a new .c file', ' */'], getline(2, 4))
2570 w! >> test.out " append it to the output file
2571
2572 let contents = readfile('test.out')
2573 call assert_equal(' * Here is a NEW .c file', contents[2])
2574 call assert_equal(' * Here is a new .c file', contents[5])
2575
2576 call delete('test.out')
2577 enew! | close
2578 augroup Test2
2579 au!
2580 augroup END
2581
2582 " Test for the BufReadPre and BufReadPost autocmds
2583 augroup Test3
2584 au!
2585 " setup autocommands to decompress before reading and re-compress
2586 " afterwards
2587 au BufReadPre *.gz exe '!gzip -d ' . shellescape(expand("<afile>"))
2588 au BufReadPre *.gz call rename(expand("<afile>:r"), expand("<afile>"))
2589 au BufReadPost *.gz call rename(expand("<afile>"), expand("<afile>:r"))
2590 au BufReadPost *.gz exe '!gzip ' . shellescape(expand("<afile>:r"))
2591 augroup END
2592
2593 e! Xtestfile.gz " Edit compressed file
2594 call assert_equal([
2595 \ 'line 2 Abcdefghijklmnopqrstuvwxyz',
2596 \ 'line 3 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2597 \ 'line 4 Abcdefghijklmnopqrstuvwxyz',
2598 \ 'line 5 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2599 \ 'line 6 Abcdefghijklmnopqrstuvwxyz',
2600 \ 'line 7 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2601 \ 'line 8 Abcdefghijklmnopqrstuvwxyz',
2602 \ 'line 9 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2603 \ 'line 10 Abcdefghijklmnopqrstuvwxyz'
2604 \ ], getline(1, 9))
2605
2606 w! >> test.out " Append it to the output file
2607
2608 augroup Test3
2609 au!
2610 augroup END
2611
2612 " Test for the FilterReadPre and FilterReadPost autocmds.
2613 set shelltemp " need temp files here
2614 augroup Test4
2615 au!
2616 au FilterReadPre *.out call rename(expand("<afile>"), expand("<afile>") . ".t")
2617 au FilterReadPre *.out exe 'silent !sed s/e/E/ ' . shellescape(expand("<afile>")) . ".t >" . shellescape(expand("<afile>"))
2618 au FilterReadPre *.out exe 'silent !rm ' . shellescape(expand("<afile>")) . '.t'
2619 au FilterReadPost *.out '[,']s/x/X/g
2620 augroup END
2621
2622 e! test.out " Edit the output file
2623 1,$!cat
2624 call assert_equal([
2625 \ 'linE 2 AbcdefghijklmnopqrstuvwXyz',
2626 \ 'linE 3 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
2627 \ 'linE 4 AbcdefghijklmnopqrstuvwXyz',
2628 \ 'linE 5 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
2629 \ 'linE 6 AbcdefghijklmnopqrstuvwXyz',
2630 \ 'linE 7 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
2631 \ 'linE 8 AbcdefghijklmnopqrstuvwXyz',
2632 \ 'linE 9 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
2633 \ 'linE 10 AbcdefghijklmnopqrstuvwXyz'
2634 \ ], getline(1, 9))
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 \ ], readfile('test.out'))
2646
2647 augroup Test4
2648 au!
2649 augroup END
2650 set shelltemp&vim
2651
2652 " Test for the FileReadPre and FileReadPost autocmds.
2653 augroup Test5
2654 au!
2655 au FileReadPre *.gz exe 'silent !gzip -d ' . shellescape(expand("<afile>"))
2656 au FileReadPre *.gz call rename(expand("<afile>:r"), expand("<afile>"))
2657 au FileReadPost *.gz '[,']s/l/L/
2658 augroup END
2659
2660 new
2661 0r Xtestfile.gz " Read compressed file
2662 call assert_equal([
2663 \ 'Line 2 Abcdefghijklmnopqrstuvwxyz',
2664 \ 'Line 3 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2665 \ 'Line 4 Abcdefghijklmnopqrstuvwxyz',
2666 \ 'Line 5 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2667 \ 'Line 6 Abcdefghijklmnopqrstuvwxyz',
2668 \ 'Line 7 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2669 \ 'Line 8 Abcdefghijklmnopqrstuvwxyz',
2670 \ 'Line 9 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2671 \ 'Line 10 Abcdefghijklmnopqrstuvwxyz'
2672 \ ], getline(1, 9))
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 \ ], readfile('Xtestfile.gz'))
2684
2685 augroup Test5
2686 au!
2687 augroup END
2688
2689 au! FileChangedShell
2690 call delete('Xtestfile.gz')
Bram Moolenaar69ea5872019-04-25 20:29:00 +02002691 call delete('test.out')
2692endfunc
Bram Moolenaar23b51392019-05-09 21:38:43 +02002693
2694func Test_throw_in_BufWritePre()
2695 new
2696 call setline(1, ['one', 'two', 'three'])
2697 call assert_false(filereadable('Xthefile'))
2698 augroup throwing
2699 au BufWritePre X* throw 'do not write'
2700 augroup END
2701 try
2702 w Xthefile
2703 catch
2704 let caught = 1
2705 endtry
2706 call assert_equal(1, caught)
2707 call assert_false(filereadable('Xthefile'))
2708
2709 bwipe!
2710 au! throwing
2711endfunc
Bram Moolenaarcadbe1b2019-09-22 21:50:09 +02002712
Bram Moolenaar40fa12a2021-09-22 14:18:13 +02002713func Test_autocmd_in_try_block()
Bram Moolenaar6f14da12022-09-07 21:30:44 +01002714 call mkdir('Xintrydir', 'R')
Bram Moolenaar40fa12a2021-09-22 14:18:13 +02002715 au BufEnter * let g:fname = expand('%')
2716 try
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +01002717 edit Xintrydir/
Bram Moolenaar40fa12a2021-09-22 14:18:13 +02002718 endtry
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +01002719 call assert_match('Xintrydir', g:fname)
Bram Moolenaar40fa12a2021-09-22 14:18:13 +02002720
2721 unlet g:fname
2722 au! BufEnter
Bram Moolenaar40fa12a2021-09-22 14:18:13 +02002723endfunc
2724
Bram Moolenaarcadbe1b2019-09-22 21:50:09 +02002725func Test_autocmd_SafeState()
2726 CheckRunVimInTerminal
2727
2728 let lines =<< trim END
2729 let g:safe = 0
2730 let g:again = ''
2731 au SafeState * let g:safe += 1
2732 au SafeStateAgain * let g:again ..= 'x'
2733 func CallTimer()
2734 call timer_start(10, {id -> execute('let g:again ..= "t"')})
2735 endfunc
2736 END
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01002737 call writefile(lines, 'XSafeState', 'D')
Bram Moolenaarcadbe1b2019-09-22 21:50:09 +02002738 let buf = RunVimInTerminal('-S XSafeState', #{rows: 6})
2739
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01002740 " Sometimes we loop to handle a K_IGNORE, SafeState may be triggered once or
Bram Moolenaar8fb1b472020-02-23 16:16:26 +01002741 " more often.
Bram Moolenaarcadbe1b2019-09-22 21:50:09 +02002742 call term_sendkeys(buf, ":echo g:safe\<CR>")
Bram Moolenaar8fb1b472020-02-23 16:16:26 +01002743 call WaitForAssert({-> assert_match('^\d ', term_getline(buf, 6))}, 1000)
Bram Moolenaarcadbe1b2019-09-22 21:50:09 +02002744
Bram Moolenaar8fb1b472020-02-23 16:16:26 +01002745 " SafeStateAgain should be invoked at least three times
Bram Moolenaarcadbe1b2019-09-22 21:50:09 +02002746 call term_sendkeys(buf, ":echo g:again\<CR>")
Bram Moolenaar8fb1b472020-02-23 16:16:26 +01002747 call WaitForAssert({-> assert_match('^xxx', term_getline(buf, 6))}, 1000)
Bram Moolenaarcadbe1b2019-09-22 21:50:09 +02002748
2749 call term_sendkeys(buf, ":let g:again = ''\<CR>:call CallTimer()\<CR>")
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02002750 call TermWait(buf, 50)
Bram Moolenaar0f6629a2019-09-22 23:24:13 +02002751 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02002752 call TermWait(buf, 50)
Bram Moolenaarcadbe1b2019-09-22 21:50:09 +02002753 call term_sendkeys(buf, ":echo g:again\<CR>")
2754 call WaitForAssert({-> assert_match('xtx', term_getline(buf, 6))}, 1000)
2755
2756 call StopVimInTerminal(buf)
Bram Moolenaarcadbe1b2019-09-22 21:50:09 +02002757endfunc
Bram Moolenaar23324a02019-10-01 17:39:04 +02002758
2759func Test_autocmd_CmdWinEnter()
2760 CheckRunVimInTerminal
Bram Moolenaar21829c52021-01-26 22:42:21 +01002761 CheckFeature cmdwin
2762
Bram Moolenaar23324a02019-10-01 17:39:04 +02002763 let lines =<< trim END
Egor Zvorykin125ffd22021-11-17 14:01:14 +00002764 augroup vimHints | au! | augroup END
Bram Moolenaar23324a02019-10-01 17:39:04 +02002765 let b:dummy_var = 'This is a dummy'
2766 autocmd CmdWinEnter * quit
2767 let winnr = winnr('$')
2768 END
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01002769 let filename = 'XCmdWinEnter'
Bram Moolenaar23324a02019-10-01 17:39:04 +02002770 call writefile(lines, filename)
2771 let buf = RunVimInTerminal('-S '.filename, #{rows: 6})
2772
2773 call term_sendkeys(buf, "q:")
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02002774 call TermWait(buf)
Bram Moolenaar23324a02019-10-01 17:39:04 +02002775 call term_sendkeys(buf, ":echo b:dummy_var\<cr>")
Bram Moolenaar353c3512020-03-15 14:19:26 +01002776 call WaitForAssert({-> assert_match('^This is a dummy', term_getline(buf, 6))}, 2000)
Bram Moolenaar23324a02019-10-01 17:39:04 +02002777 call term_sendkeys(buf, ":echo &buftype\<cr>")
2778 call WaitForAssert({-> assert_notmatch('^nofile', term_getline(buf, 6))}, 1000)
2779 call term_sendkeys(buf, ":echo winnr\<cr>")
2780 call WaitForAssert({-> assert_match('^1', term_getline(buf, 6))}, 1000)
2781
2782 " clean up
2783 call StopVimInTerminal(buf)
2784 call delete(filename)
2785endfunc
Bram Moolenaarec66c412019-10-11 21:19:13 +02002786
2787func Test_autocmd_was_using_freed_memory()
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01002788 CheckFeature quickfix
2789
Bram Moolenaarec66c412019-10-11 21:19:13 +02002790 pedit xx
2791 n x
Bram Moolenaar92bb83e2021-02-03 23:04:46 +01002792 augroup winenter
2793 au WinEnter * if winnr('$') > 2 | quit | endif
2794 augroup END
Bram Moolenaarec66c412019-10-11 21:19:13 +02002795 split
Bram Moolenaar92bb83e2021-02-03 23:04:46 +01002796
2797 augroup winenter
2798 au! WinEnter
2799 augroup END
2800
2801 bwipe xx
2802 bwipe x
2803 pclose
Bram Moolenaarec66c412019-10-11 21:19:13 +02002804endfunc
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +01002805
2806func Test_BufWrite_lockmarks()
Bram Moolenaarf08b0eb2021-10-16 13:00:14 +01002807 let g:test_is_flaky = 1
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +01002808 edit! Xtest
2809 call setline(1, ['a', 'b', 'c', 'd'])
2810
2811 " :lockmarks preserves the marks
2812 call SetChangeMarks(2, 3)
2813 lockmarks write
2814 call assert_equal([2, 3], [line("'["), line("']")])
2815
2816 " *WritePre autocmds get the correct line range, but lockmarks preserves the
2817 " original values for the user
2818 augroup lockmarks
2819 au!
2820 au BufWritePre,FilterWritePre * call assert_equal([1, 4], [line("'["), line("']")])
2821 au FileWritePre * call assert_equal([3, 4], [line("'["), line("']")])
2822 augroup END
2823
2824 lockmarks write
2825 call assert_equal([2, 3], [line("'["), line("']")])
2826
2827 if executable('cat')
2828 lockmarks %!cat
2829 call assert_equal([2, 3], [line("'["), line("']")])
2830 endif
2831
2832 lockmarks 3,4write Xtest2
2833 call assert_equal([2, 3], [line("'["), line("']")])
2834
2835 au! lockmarks
2836 augroup! lockmarks
2837 call delete('Xtest')
2838 call delete('Xtest2')
2839endfunc
Bram Moolenaarce6db022020-01-07 20:11:42 +01002840
2841func Test_FileType_spell()
2842 if !isdirectory('/tmp')
2843 throw "Skipped: requires /tmp directory"
2844 endif
2845
2846 " this was crashing with an invalid free()
2847 setglobal spellfile=/tmp/en.utf-8.add
2848 augroup crash
2849 autocmd!
2850 autocmd BufNewFile,BufReadPost crashfile setf somefiletype
2851 autocmd BufNewFile,BufReadPost crashfile set ft=anotherfiletype
2852 autocmd FileType anotherfiletype setlocal spell
2853 augroup END
2854 func! NoCrash() abort
2855 edit /tmp/crashfile
2856 endfunc
2857 call NoCrash()
2858
2859 au! crash
2860 setglobal spellfile=
2861endfunc
Bram Moolenaarbc2b71d2020-02-17 21:33:30 +01002862
Bram Moolenaar406cd902020-02-18 21:54:41 +01002863" Test closing a window or editing another buffer from a FileChangedRO handler
2864" in a readonly buffer
2865func Test_FileChangedRO_winclose()
Bram Moolenaar62cd26a2020-10-11 20:08:44 +02002866 call test_override('ui_delay', 10)
2867
Bram Moolenaar406cd902020-02-18 21:54:41 +01002868 augroup FileChangedROTest
2869 au!
2870 autocmd FileChangedRO * quit
2871 augroup END
2872 new
2873 set readonly
2874 call assert_fails('normal i', 'E788:')
2875 close
2876 augroup! FileChangedROTest
2877
2878 augroup FileChangedROTest
2879 au!
Bram Moolenaar61abe7d2022-08-30 21:46:08 +01002880 autocmd FileChangedRO * edit Xrofile
Bram Moolenaar406cd902020-02-18 21:54:41 +01002881 augroup END
2882 new
2883 set readonly
2884 call assert_fails('normal i', 'E788:')
2885 close
2886 augroup! FileChangedROTest
Bram Moolenaar62cd26a2020-10-11 20:08:44 +02002887 call test_override('ALL', 0)
Bram Moolenaar406cd902020-02-18 21:54:41 +01002888endfunc
2889
Bram Moolenaar0c81d1b2020-02-22 22:45:55 +01002890func LogACmd()
2891 call add(g:logged, line('$'))
2892endfunc
2893
2894func Test_TermChanged()
Bram Moolenaard28e0b32020-02-22 23:08:52 +01002895 CheckNotGui
2896
Bram Moolenaar0c81d1b2020-02-22 22:45:55 +01002897 enew!
2898 tabnew
2899 call setline(1, ['a', 'b', 'c', 'd'])
2900 $
2901 au TermChanged * call LogACmd()
2902 let g:logged = []
2903 let term_save = &term
2904 set term=xterm
2905 call assert_equal([1, 4], g:logged)
2906
2907 au! TermChanged
2908 let &term = term_save
2909 bwipe!
2910endfunc
2911
Bram Moolenaare3284872020-03-19 13:55:03 +01002912" Test for FileReadCmd autocmd
2913func Test_autocmd_FileReadCmd()
2914 func ReadFileCmd()
2915 call append(line('$'), "v:cmdarg = " .. v:cmdarg)
2916 endfunc
2917 augroup FileReadCmdTest
2918 au!
2919 au FileReadCmd Xtest call ReadFileCmd()
2920 augroup END
2921
2922 new
2923 read ++bin Xtest
2924 read ++nobin Xtest
2925 read ++edit Xtest
2926 read ++bad=keep Xtest
2927 read ++bad=drop Xtest
2928 read ++bad=- Xtest
2929 read ++ff=unix Xtest
2930 read ++ff=dos Xtest
2931 read ++ff=mac Xtest
2932 read ++enc=utf-8 Xtest
2933
2934 call assert_equal(['',
2935 \ 'v:cmdarg = ++bin',
2936 \ 'v:cmdarg = ++nobin',
2937 \ 'v:cmdarg = ++edit',
2938 \ 'v:cmdarg = ++bad=keep',
2939 \ 'v:cmdarg = ++bad=drop',
2940 \ 'v:cmdarg = ++bad=-',
2941 \ 'v:cmdarg = ++ff=unix',
2942 \ 'v:cmdarg = ++ff=dos',
2943 \ 'v:cmdarg = ++ff=mac',
2944 \ 'v:cmdarg = ++enc=utf-8'], getline(1, '$'))
2945
2946 close!
2947 augroup FileReadCmdTest
2948 au!
2949 augroup END
2950 delfunc ReadFileCmd
2951endfunc
2952
Bram Moolenaaree4e0c12020-04-06 21:35:05 +02002953" Test for passing invalid arguments to autocmd
2954func Test_autocmd_invalid_args()
2955 " Additional character after * for event
Bram Moolenaar61abe7d2022-08-30 21:46:08 +01002956 call assert_fails('autocmd *a Xinvfile set ff=unix', 'E215:')
Bram Moolenaaree4e0c12020-04-06 21:35:05 +02002957 augroup Test
2958 augroup END
2959 " Invalid autocmd event
Bram Moolenaar61abe7d2022-08-30 21:46:08 +01002960 call assert_fails('autocmd Bufabc Xinvfile set ft=vim', 'E216:')
Bram Moolenaaree4e0c12020-04-06 21:35:05 +02002961 " Invalid autocmd event in a autocmd group
Bram Moolenaar61abe7d2022-08-30 21:46:08 +01002962 call assert_fails('autocmd Test Bufabc Xinvfile set ft=vim', 'E216:')
Bram Moolenaaree4e0c12020-04-06 21:35:05 +02002963 augroup! Test
2964 " Execute all autocmds
2965 call assert_fails('doautocmd * BufEnter', 'E217:')
2966 call assert_fails('augroup! x1a2b3', 'E367:')
2967 call assert_fails('autocmd BufNew <buffer=999> pwd', 'E680:')
Bram Moolenaar531be472020-09-23 22:38:05 +02002968 call assert_fails('autocmd BufNew \) set ff=unix', 'E55:')
Bram Moolenaaree4e0c12020-04-06 21:35:05 +02002969endfunc
2970
2971" Test for deep nesting of autocmds
2972func Test_autocmd_deep_nesting()
Bram Moolenaar61abe7d2022-08-30 21:46:08 +01002973 autocmd BufEnter Xdeepfile doautocmd BufEnter Xdeepfile
2974 call assert_fails('doautocmd BufEnter Xdeepfile', 'E218:')
2975 autocmd! BufEnter Xdeepfile
Bram Moolenaaree4e0c12020-04-06 21:35:05 +02002976endfunc
2977
Bram Moolenaarbe5ee862020-06-10 20:56:58 +02002978" Tests for SigUSR1 autocmd event, which is only available on posix systems.
2979func Test_autocmd_sigusr1()
2980 CheckUnix
Bram Moolenaar62cd26a2020-10-11 20:08:44 +02002981 CheckExecutable /bin/kill
Bram Moolenaarbe5ee862020-06-10 20:56:58 +02002982
2983 let g:sigusr1_passed = 0
2984 au SigUSR1 * let g:sigusr1_passed = 1
2985 call system('/bin/kill -s usr1 ' . getpid())
2986 call WaitForAssert({-> assert_true(g:sigusr1_passed)})
2987
2988 au! SigUSR1
2989 unlet g:sigusr1_passed
2990endfunc
2991
Bram Moolenaarb340bae2020-06-15 19:51:56 +02002992" Test for BufReadPre autocmd deleting the file
2993func Test_BufReadPre_delfile()
2994 augroup TestAuCmd
2995 au!
Bram Moolenaare7cda972022-08-29 11:02:59 +01002996 autocmd BufReadPre XbufreadPre call delete('XbufreadPre')
Bram Moolenaarb340bae2020-06-15 19:51:56 +02002997 augroup END
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01002998 call writefile([], 'XbufreadPre', 'D')
Bram Moolenaare7cda972022-08-29 11:02:59 +01002999 call assert_fails('new XbufreadPre', 'E200:')
3000 call assert_equal('XbufreadPre', @%)
Bram Moolenaarb340bae2020-06-15 19:51:56 +02003001 call assert_equal(1, &readonly)
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01003002
Bram Moolenaarb340bae2020-06-15 19:51:56 +02003003 augroup TestAuCmd
3004 au!
3005 augroup END
3006 close!
3007endfunc
3008
3009" Test for BufReadPre autocmd changing the current buffer
3010func Test_BufReadPre_changebuf()
3011 augroup TestAuCmd
3012 au!
Bram Moolenaare7cda972022-08-29 11:02:59 +01003013 autocmd BufReadPre Xchangebuf edit Xsomeotherfile
Bram Moolenaarb340bae2020-06-15 19:51:56 +02003014 augroup END
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01003015 call writefile([], 'Xchangebuf', 'D')
Bram Moolenaare7cda972022-08-29 11:02:59 +01003016 call assert_fails('new Xchangebuf', 'E201:')
Bram Moolenaarb340bae2020-06-15 19:51:56 +02003017 call assert_equal('Xsomeotherfile', @%)
3018 call assert_equal(1, &readonly)
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01003019
Bram Moolenaarb340bae2020-06-15 19:51:56 +02003020 augroup TestAuCmd
3021 au!
3022 augroup END
3023 close!
3024endfunc
3025
3026" Test for BufWipeouti autocmd changing the current buffer when reading a file
3027" in an empty buffer with 'f' flag in 'cpo'
3028func Test_BufDelete_changebuf()
3029 new
3030 augroup TestAuCmd
3031 au!
3032 autocmd BufWipeout * let bufnr = bufadd('somefile') | exe "b " .. bufnr
3033 augroup END
3034 let save_cpo = &cpo
3035 set cpo+=f
Bram Moolenaarb18b4962022-09-02 21:55:50 +01003036 call assert_fails('r Xchangebuf', ['E812:', 'E484:'])
Bram Moolenaarb340bae2020-06-15 19:51:56 +02003037 call assert_equal('somefile', @%)
3038 let &cpo = save_cpo
3039 augroup TestAuCmd
3040 au!
3041 augroup END
3042 close!
3043endfunc
3044
Bram Moolenaar0fe937f2020-06-16 22:42:04 +02003045" Test for the temporary internal window used to execute autocmds
3046func Test_autocmd_window()
3047 %bw!
3048 edit one.txt
3049 tabnew two.txt
Bram Moolenaar41cd8032021-03-13 15:47:56 +01003050 vnew three.txt
3051 tabnew four.txt
3052 tabprevious
Bram Moolenaar0fe937f2020-06-16 22:42:04 +02003053 let g:blist = []
Bram Moolenaar832adf92020-06-25 19:01:36 +02003054 augroup aucmd_win_test1
Bram Moolenaar0fe937f2020-06-16 22:42:04 +02003055 au!
3056 au BufEnter * call add(g:blist, [expand('<afile>'),
3057 \ win_gettype(bufwinnr(expand('<afile>')))])
3058 augroup END
3059
3060 doautoall BufEnter
Bram Moolenaar41cd8032021-03-13 15:47:56 +01003061 call assert_equal([
3062 \ ['one.txt', 'autocmd'],
3063 \ ['two.txt', ''],
3064 \ ['four.txt', 'autocmd'],
3065 \ ['three.txt', ''],
3066 \ ], g:blist)
Bram Moolenaar0fe937f2020-06-16 22:42:04 +02003067
Bram Moolenaar832adf92020-06-25 19:01:36 +02003068 augroup aucmd_win_test1
Bram Moolenaar0fe937f2020-06-16 22:42:04 +02003069 au!
3070 augroup END
Bram Moolenaar832adf92020-06-25 19:01:36 +02003071 augroup! aucmd_win_test1
3072 %bw!
3073endfunc
3074
3075" Test for trying to close the temporary window used for executing an autocmd
3076func Test_close_autocmd_window()
3077 %bw!
3078 edit one.txt
3079 tabnew two.txt
3080 augroup aucmd_win_test2
3081 au!
3082 au BufEnter * if expand('<afile>') == 'one.txt' | 1close | endif
3083 augroup END
3084
3085 call assert_fails('doautoall BufEnter', 'E813:')
3086
3087 augroup aucmd_win_test2
3088 au!
3089 augroup END
3090 augroup! aucmd_win_test2
Bram Moolenaarcf844172020-06-26 19:44:06 +02003091 %bwipe!
3092endfunc
3093
3094" Test for trying to close the tab that has the temporary window for exeucing
3095" an autocmd.
3096func Test_close_autocmd_tab()
3097 edit one.txt
3098 tabnew two.txt
3099 augroup aucmd_win_test
3100 au!
3101 au BufEnter * if expand('<afile>') == 'one.txt' | tabfirst | tabonly | endif
3102 augroup END
3103
3104 call assert_fails('doautoall BufEnter', 'E813:')
3105
3106 tabonly
3107 augroup aucmd_win_test
3108 au!
3109 augroup END
3110 augroup! aucmd_win_test
3111 %bwipe!
Bram Moolenaar0fe937f2020-06-16 22:42:04 +02003112endfunc
3113
Bram Moolenaarcb1956d2022-01-07 15:45:18 +00003114func Test_Visual_doautoall_redraw()
3115 call setline(1, ['a', 'b'])
3116 new
3117 wincmd p
3118 call feedkeys("G\<C-V>", 'txn')
3119 autocmd User Explode ++once redraw
3120 doautoall User Explode
3121 %bwipe!
3122endfunc
3123
Bram Moolenaar6bcb8772021-02-03 21:23:29 +01003124" This was using freed memory.
3125func Test_BufNew_arglocal()
3126 arglocal
3127 au BufNew * arglocal
3128 call assert_fails('drop xx', 'E1156:')
3129
3130 au! BufNew
3131endfunc
3132
Bram Moolenaar8ab37572021-02-03 21:56:59 +01003133func Test_autocmd_closes_window()
3134 au BufNew,BufWinLeave * e %e
3135 file yyy
3136 au BufNew,BufWinLeave * ball
Bram Moolenaaraad5f9d2021-02-06 17:30:31 +01003137 n xxx
Bram Moolenaar8ab37572021-02-03 21:56:59 +01003138
Bram Moolenaaraad5f9d2021-02-06 17:30:31 +01003139 %bwipe
Bram Moolenaar8ab37572021-02-03 21:56:59 +01003140 au! BufNew
3141 au! BufWinLeave
3142endfunc
3143
Bram Moolenaar92bb83e2021-02-03 23:04:46 +01003144func Test_autocmd_quit_psearch()
3145 sn aa bb
3146 augroup aucmd_win_test
3147 au!
3148 au BufEnter,BufLeave,BufNew,WinEnter,WinLeave,WinNew * if winnr('$') > 1 | q | endif
3149 augroup END
3150 ps /
3151
3152 augroup aucmd_win_test
3153 au!
3154 augroup END
zeertzjq7851c692022-04-21 11:14:01 +01003155 new
3156 pclose
Bram Moolenaar92bb83e2021-02-03 23:04:46 +01003157endfunc
3158
Bram Moolenaaraad5f9d2021-02-06 17:30:31 +01003159" Fuzzer found some strange combination that caused a crash.
3160func Test_autocmd_normal_mess()
Bram Moolenaardd07c022021-02-07 13:32:46 +01003161 " For unknown reason this hangs on MS-Windows
3162 CheckNotMSWindows
3163
Bram Moolenaaraad5f9d2021-02-06 17:30:31 +01003164 augroup aucmd_normal_test
3165 au BufLeave,BufWinLeave,BufHidden,BufUnload,BufDelete,BufWipeout * norm 7q/qc
3166 augroup END
Bram Moolenaar983d83f2021-02-07 12:12:43 +01003167 call assert_fails('o4', 'E1159')
Bram Moolenaaraad5f9d2021-02-06 17:30:31 +01003168 silent! H
Bram Moolenaar983d83f2021-02-07 12:12:43 +01003169 call assert_fails('e xx', 'E1159')
Bram Moolenaaraad5f9d2021-02-06 17:30:31 +01003170 normal G
3171
3172 augroup aucmd_normal_test
3173 au!
3174 augroup END
3175endfunc
3176
Bram Moolenaar8c6951f2021-02-06 18:08:45 +01003177func Test_autocmd_closing_cmdwin()
Bram Moolenaardd07c022021-02-07 13:32:46 +01003178 " For unknown reason this hangs on MS-Windows
3179 CheckNotMSWindows
3180
Bram Moolenaar8c6951f2021-02-06 18:08:45 +01003181 au BufWinLeave * nested q
3182 call assert_fails("norm 7q?\n", 'E855:')
3183
3184 au! BufWinLeave
3185 new
3186 only
3187endfunc
3188
Bram Moolenaar2c7080b2021-02-06 19:19:42 +01003189func Test_autocmd_vimgrep()
3190 augroup aucmd_vimgrep
Charlie Grovesfef44852022-04-19 16:24:12 +01003191 au QuickfixCmdPre,BufNew,BufReadCmd * sb
zeertzjq7851c692022-04-21 11:14:01 +01003192 au QuickfixCmdPre,BufNew,BufReadCmd * q9
Bram Moolenaar2c7080b2021-02-06 19:19:42 +01003193 augroup END
Bram Moolenaardd07c022021-02-07 13:32:46 +01003194 call assert_fails('lv ?a? foo', 'E926:')
Bram Moolenaar2c7080b2021-02-06 19:19:42 +01003195
3196 augroup aucmd_vimgrep
3197 au!
3198 augroup END
3199endfunc
3200
Bram Moolenaar73b8b0a2021-08-01 14:52:32 +02003201func Test_autocmd_with_block()
3202 augroup block_testing
3203 au BufReadPost *.xml {
3204 setlocal matchpairs+=<:>
3205 /<start
3206 }
Bram Moolenaar63b91732021-08-05 20:40:03 +02003207 au CursorHold * {
3208 autocmd BufReadPre * ++once echo 'one' | echo 'two'
3209 g:gotSafeState = 77
3210 }
Bram Moolenaar73b8b0a2021-08-01 14:52:32 +02003211 augroup END
3212
3213 let expected = "\n--- Autocommands ---\nblock_testing BufRead\n *.xml {^@ setlocal matchpairs+=<:>^@ /<start^@ }"
3214 call assert_equal(expected, execute('au BufReadPost *.xml'))
3215
Bram Moolenaar63b91732021-08-05 20:40:03 +02003216 doautocmd CursorHold
3217 call assert_equal(77, g:gotSafeState)
3218 unlet g:gotSafeState
3219
Bram Moolenaar73b8b0a2021-08-01 14:52:32 +02003220 augroup block_testing
3221 au!
Bram Moolenaar75ebd2a2022-06-03 17:39:46 +01003222 autocmd CursorHold * {
3223 if true
3224 # comment
3225 && true
3226
3227 && true
3228 g:done = 'yes'
3229 endif
3230 }
3231 augroup END
3232 doautocmd CursorHold
3233 call assert_equal('yes', g:done)
3234
3235 unlet g:done
3236 augroup block_testing
3237 au!
Bram Moolenaar73b8b0a2021-08-01 14:52:32 +02003238 augroup END
3239endfunc
3240
Christian Brabandtdb3b4462021-10-16 11:58:55 +01003241" Test TextChangedI and TextChanged
3242func Test_Changed_ChangedI()
3243 new
3244 call test_override("char_avail", 1)
3245 let [g:autocmd_i, g:autocmd_n] = ['','']
3246
3247 func! TextChangedAutocmdI(char)
3248 let g:autocmd_{tolower(a:char)} = a:char .. b:changedtick
3249 endfunc
3250
3251 augroup Test_TextChanged
3252 au!
3253 au TextChanged <buffer> :call TextChangedAutocmdI('N')
3254 au TextChangedI <buffer> :call TextChangedAutocmdI('I')
3255 augroup END
3256
3257 call feedkeys("ifoo\<esc>", 'tnix')
3258 " TODO: Test test does not seem to trigger TextChanged autocommand, this
3259 " requires running Vim in a terminal window.
3260 " call assert_equal('N3', g:autocmd_n)
3261 call assert_equal('I3', g:autocmd_i)
3262
3263 call feedkeys("yyp", 'tnix')
3264 " TODO: Test test does not seem to trigger TextChanged autocommand.
3265 " call assert_equal('N4', g:autocmd_n)
3266 call assert_equal('I3', g:autocmd_i)
3267
3268 " CleanUp
3269 call test_override("char_avail", 0)
3270 au! TextChanged <buffer>
3271 au! TextChangedI <buffer>
3272 augroup! Test_TextChanged
3273 delfu TextChangedAutocmdI
3274 unlet! g:autocmd_i g:autocmd_n
3275
3276 bw!
3277endfunc
Bram Moolenaar2c7080b2021-02-06 19:19:42 +01003278
Bram Moolenaar6f2465d2022-03-22 18:13:01 +00003279func Test_closing_autocmd_window()
3280 let lines =<< trim END
3281 edit Xa.txt
3282 tabnew Xb.txt
3283 autocmd BufEnter Xa.txt unhide 1
3284 doautoall BufEnter
3285 END
3286 call v9.CheckScriptFailure(lines, 'E814:')
3287 au! BufEnter
3288 only!
3289 bwipe Xa.txt
3290 bwipe Xb.txt
3291endfunc
3292
Bram Moolenaar347538f2022-03-26 16:28:06 +00003293func Test_bufwipeout_changes_window()
3294 " This should not crash, but we don't have any expectations about what
3295 " happens, changing window in BufWipeout has unpredictable results.
3296 tabedit
3297 let g:window_id = win_getid()
3298 topleft new
3299 setlocal bufhidden=wipe
3300 autocmd BufWipeout <buffer> call win_gotoid(g:window_id)
3301 tabprevious
3302 +tabclose
3303
3304 unlet g:window_id
3305 au! BufWipeout
3306 %bwipe!
3307endfunc
3308
zeertzjq021996f2022-04-10 11:44:04 +01003309func Test_v_event_readonly()
3310 autocmd CompleteChanged * let v:event.width = 0
3311 call assert_fails("normal! i\<C-X>\<C-V>", 'E46:')
3312 au! CompleteChanged
3313
3314 autocmd DirChangedPre * let v:event.directory = ''
3315 call assert_fails('cd .', 'E46:')
3316 au! DirChangedPre
3317
3318 autocmd ModeChanged * let v:event.new_mode = ''
3319 call assert_fails('normal! cc', 'E46:')
3320 au! ModeChanged
3321
3322 autocmd TextYankPost * let v:event.operator = ''
3323 call assert_fails('normal! yy', 'E46:')
3324 au! TextYankPost
3325endfunc
3326
zeertzjqc9e8fd62022-07-26 18:12:38 +01003327" Test for ModeChanged pattern
3328func Test_mode_changes()
3329 let g:index = 0
3330 let g:mode_seq = ['n', 'i', 'n', 'v', 'V', 'i', 'ix', 'i', 'ic', 'i', 'n', 'no', 'n', 'V', 'v', 's', 'n']
3331 func! TestMode()
3332 call assert_equal(g:mode_seq[g:index], get(v:event, "old_mode"))
3333 call assert_equal(g:mode_seq[g:index + 1], get(v:event, "new_mode"))
3334 call assert_equal(mode(1), get(v:event, "new_mode"))
3335 let g:index += 1
3336 endfunc
3337
3338 au ModeChanged * :call TestMode()
3339 let g:n_to_any = 0
3340 au ModeChanged n:* let g:n_to_any += 1
3341 call feedkeys("i\<esc>vVca\<CR>\<C-X>\<C-L>\<esc>ggdG", 'tnix')
3342
3343 let g:V_to_v = 0
3344 au ModeChanged V:v let g:V_to_v += 1
3345 call feedkeys("Vv\<C-G>\<esc>", 'tnix')
3346 call assert_equal(len(filter(g:mode_seq[1:], {idx, val -> val == 'n'})), g:n_to_any)
3347 call assert_equal(1, g:V_to_v)
3348 call assert_equal(len(g:mode_seq) - 1, g:index)
3349
3350 let g:n_to_i = 0
3351 au ModeChanged n:i let g:n_to_i += 1
3352 let g:n_to_niI = 0
3353 au ModeChanged i:niI let g:n_to_niI += 1
3354 let g:niI_to_i = 0
3355 au ModeChanged niI:i let g:niI_to_i += 1
3356 let g:nany_to_i = 0
3357 au ModeChanged n*:i let g:nany_to_i += 1
3358 let g:i_to_n = 0
3359 au ModeChanged i:n let g:i_to_n += 1
3360 let g:nori_to_any = 0
3361 au ModeChanged [ni]:* let g:nori_to_any += 1
3362 let g:i_to_any = 0
3363 au ModeChanged i:* let g:i_to_any += 1
3364 let g:index = 0
3365 let g:mode_seq = ['n', 'i', 'niI', 'i', 'n']
3366 call feedkeys("a\<C-O>l\<esc>", 'tnix')
3367 call assert_equal(len(g:mode_seq) - 1, g:index)
3368 call assert_equal(1, g:n_to_i)
3369 call assert_equal(1, g:n_to_niI)
3370 call assert_equal(1, g:niI_to_i)
3371 call assert_equal(2, g:nany_to_i)
3372 call assert_equal(1, g:i_to_n)
3373 call assert_equal(2, g:i_to_any)
3374 call assert_equal(3, g:nori_to_any)
3375
3376 if has('terminal')
3377 let g:mode_seq += ['c', 'n', 't', 'nt', 'c', 'nt', 'n']
3378 call feedkeys(":term\<CR>\<C-W>N:bd!\<CR>", 'tnix')
3379 call assert_equal(len(g:mode_seq) - 1, g:index)
3380 call assert_equal(1, g:n_to_i)
3381 call assert_equal(1, g:n_to_niI)
3382 call assert_equal(1, g:niI_to_i)
3383 call assert_equal(2, g:nany_to_i)
3384 call assert_equal(1, g:i_to_n)
3385 call assert_equal(2, g:i_to_any)
3386 call assert_equal(5, g:nori_to_any)
3387 endif
3388
3389 if has('cmdwin')
3390 let g:n_to_c = 0
3391 au ModeChanged n:c let g:n_to_c += 1
3392 let g:c_to_n = 0
3393 au ModeChanged c:n let g:c_to_n += 1
3394 let g:mode_seq += ['c', 'n', 'c', 'n']
3395 call feedkeys("q:\<C-C>\<Esc>", 'tnix')
3396 call assert_equal(len(g:mode_seq) - 1, g:index)
3397 call assert_equal(2, g:n_to_c)
3398 call assert_equal(2, g:c_to_n)
3399 unlet g:n_to_c
3400 unlet g:c_to_n
3401 endif
3402
3403 au! ModeChanged
3404 delfunc TestMode
3405 unlet! g:mode_seq
3406 unlet! g:index
3407 unlet! g:n_to_any
3408 unlet! g:V_to_v
3409 unlet! g:n_to_i
3410 unlet! g:n_to_niI
3411 unlet! g:niI_to_i
3412 unlet! g:nany_to_i
3413 unlet! g:i_to_n
3414 unlet! g:nori_to_any
3415 unlet! g:i_to_any
3416endfunc
3417
3418func Test_recursive_ModeChanged()
3419 au! ModeChanged * norm 0u
3420 sil! norm 
3421 au! ModeChanged
3422endfunc
3423
3424func Test_ModeChanged_starts_visual()
3425 " This was triggering ModeChanged before setting VIsual, causing a crash.
3426 au! ModeChanged * norm 0u
3427 sil! norm 
3428
3429 au! ModeChanged
3430endfunc
Bram Moolenaar347538f2022-03-26 16:28:06 +00003431
Charlie Grovesfef44852022-04-19 16:24:12 +01003432func Test_noname_autocmd()
3433 augroup test_noname_autocmd_group
3434 autocmd!
3435 autocmd BufEnter * call add(s:li, ["BufEnter", expand("<afile>")])
3436 autocmd BufDelete * call add(s:li, ["BufDelete", expand("<afile>")])
3437 autocmd BufLeave * call add(s:li, ["BufLeave", expand("<afile>")])
3438 autocmd BufUnload * call add(s:li, ["BufUnload", expand("<afile>")])
3439 autocmd BufWipeout * call add(s:li, ["BufWipeout", expand("<afile>")])
3440 augroup END
3441
3442 let s:li = []
3443 edit foo
3444 call assert_equal([['BufUnload', ''], ['BufDelete', ''], ['BufWipeout', ''], ['BufEnter', 'foo']], s:li)
3445
3446 au! test_noname_autocmd_group
3447 augroup! test_noname_autocmd_group
3448endfunc
3449
Yegappan Lakshmanan1755a912022-05-19 10:31:47 +01003450" Test for the autocmd_get() function
3451func Test_autocmd_get()
3452 augroup TestAutoCmdFns
3453 au!
3454 autocmd BufAdd *.vim echo "bufadd-vim"
3455 autocmd BufAdd *.py echo "bufadd-py"
3456 autocmd BufHidden *.vim echo "bufhidden"
3457 augroup END
3458 augroup TestAutoCmdFns2
3459 autocmd BufAdd *.vim echo "bufadd-vim-2"
3460 autocmd BufRead *.a1b2c3 echo "bufadd-vim-2"
3461 augroup END
3462
3463 let l = autocmd_get()
3464 call assert_true(l->len() > 0)
3465
3466 " Test for getting all the autocmds in a group
3467 let expected = [
3468 \ #{cmd: 'echo "bufadd-vim"', group: 'TestAutoCmdFns',
3469 \ pattern: '*.vim', nested: v:false, once: v:false,
3470 \ event: 'BufAdd'},
3471 \ #{cmd: 'echo "bufadd-py"', group: 'TestAutoCmdFns',
3472 \ pattern: '*.py', nested: v:false, once: v:false,
3473 \ event: 'BufAdd'},
3474 \ #{cmd: 'echo "bufhidden"', group: 'TestAutoCmdFns',
3475 \ pattern: '*.vim', nested: v:false,
3476 \ once: v:false, event: 'BufHidden'}]
3477 call assert_equal(expected, autocmd_get(#{group: 'TestAutoCmdFns'}))
3478
3479 " Test for getting autocmds for all the patterns in a group
3480 call assert_equal(expected, autocmd_get(#{group: 'TestAutoCmdFns',
3481 \ event: '*'}))
3482
3483 " Test for getting autocmds for an event in a group
3484 let expected = [
3485 \ #{cmd: 'echo "bufadd-vim"', group: 'TestAutoCmdFns',
3486 \ pattern: '*.vim', nested: v:false, once: v:false,
3487 \ event: 'BufAdd'},
3488 \ #{cmd: 'echo "bufadd-py"', group: 'TestAutoCmdFns',
3489 \ pattern: '*.py', nested: v:false, once: v:false,
3490 \ event: 'BufAdd'}]
3491 call assert_equal(expected, autocmd_get(#{group: 'TestAutoCmdFns',
3492 \ event: 'BufAdd'}))
3493
3494 " Test for getting the autocmds for all the events in a group for particular
3495 " pattern
3496 call assert_equal([{'cmd': 'echo "bufadd-py"', 'group': 'TestAutoCmdFns',
3497 \ 'pattern': '*.py', 'nested': v:false, 'once': v:false,
3498 \ 'event': 'BufAdd'}],
3499 \ autocmd_get(#{group: 'TestAutoCmdFns', event: '*', pattern: '*.py'}))
3500
3501 " Test for getting the autocmds for an events in a group for particular
3502 " pattern
3503 let l = autocmd_get(#{group: 'TestAutoCmdFns', event: 'BufAdd',
3504 \ pattern: '*.vim'})
3505 call assert_equal([
3506 \ #{cmd: 'echo "bufadd-vim"', group: 'TestAutoCmdFns',
3507 \ pattern: '*.vim', nested: v:false, once: v:false,
3508 \ event: 'BufAdd'}], l)
3509
3510 " Test for getting the autocmds for a pattern in a group
3511 let l = autocmd_get(#{group: 'TestAutoCmdFns', pattern: '*.vim'})
3512 call assert_equal([
3513 \ #{cmd: 'echo "bufadd-vim"', group: 'TestAutoCmdFns',
3514 \ pattern: '*.vim', nested: v:false, once: v:false,
3515 \ event: 'BufAdd'},
3516 \ #{cmd: 'echo "bufhidden"', group: 'TestAutoCmdFns',
3517 \ pattern: '*.vim', nested: v:false,
3518 \ once: v:false, event: 'BufHidden'}], l)
3519
3520 " Test for getting the autocmds for a pattern in all the groups
3521 let l = autocmd_get(#{pattern: '*.a1b2c3'})
3522 call assert_equal([{'cmd': 'echo "bufadd-vim-2"', 'group': 'TestAutoCmdFns2',
3523 \ 'pattern': '*.a1b2c3', 'nested': v:false, 'once': v:false,
3524 \ 'event': 'BufRead'}], l)
3525
3526 " Test for getting autocmds for a pattern without any autocmds
3527 call assert_equal([], autocmd_get(#{group: 'TestAutoCmdFns',
3528 \ pattern: '*.abc'}))
3529 call assert_equal([], autocmd_get(#{group: 'TestAutoCmdFns',
3530 \ event: 'BufAdd', pattern: '*.abc'}))
3531 call assert_equal([], autocmd_get(#{group: 'TestAutoCmdFns',
3532 \ event: 'BufWipeout'}))
3533 call assert_fails("call autocmd_get(#{group: 'abc', event: 'BufAdd'})",
3534 \ 'E367:')
3535 let cmd = "echo autocmd_get(#{group: 'TestAutoCmdFns', event: 'abc'})"
3536 call assert_fails(cmd, 'E216:')
3537 call assert_fails("call autocmd_get(#{group: 'abc'})", 'E367:')
3538 call assert_fails("echo autocmd_get(#{event: 'abc'})", 'E216:')
3539
3540 augroup TestAutoCmdFns
3541 au!
3542 augroup END
3543 call assert_equal([], autocmd_get(#{group: 'TestAutoCmdFns'}))
3544
3545 " Test for nested and once autocmds
3546 augroup TestAutoCmdFns
3547 au!
3548 autocmd VimSuspend * ++nested echo "suspend"
3549 autocmd VimResume * ++once echo "resume"
3550 augroup END
3551
3552 let expected = [
3553 \ {'cmd': 'echo "suspend"', 'group': 'TestAutoCmdFns', 'pattern': '*',
3554 \ 'nested': v:true, 'once': v:false, 'event': 'VimSuspend'},
3555 \ {'cmd': 'echo "resume"', 'group': 'TestAutoCmdFns', 'pattern': '*',
3556 \ 'nested': v:false, 'once': v:true, 'event': 'VimResume'}]
3557 call assert_equal(expected, autocmd_get(#{group: 'TestAutoCmdFns'}))
3558
3559 " Test for buffer-local autocmd
3560 augroup TestAutoCmdFns
3561 au!
3562 autocmd TextYankPost <buffer> echo "textyankpost"
3563 augroup END
3564
3565 let expected = [
3566 \ {'cmd': 'echo "textyankpost"', 'group': 'TestAutoCmdFns',
3567 \ 'pattern': '<buffer=' .. bufnr() .. '>', 'nested': v:false,
3568 \ 'once': v:false, 'bufnr': bufnr(), 'event': 'TextYankPost'}]
3569 call assert_equal(expected, autocmd_get(#{group: 'TestAutoCmdFns'}))
3570
3571 augroup TestAutoCmdFns
3572 au!
3573 augroup END
3574 augroup! TestAutoCmdFns
3575 augroup TestAutoCmdFns2
3576 au!
3577 augroup END
3578 augroup! TestAutoCmdFns2
3579
3580 call assert_fails("echo autocmd_get(#{group: []})", 'E730:')
3581 call assert_fails("echo autocmd_get(#{event: {}})", 'E731:')
3582 call assert_fails("echo autocmd_get([])", 'E1206:')
3583endfunc
3584
3585" Test for the autocmd_add() function
3586func Test_autocmd_add()
3587 " Define a single autocmd in a group
3588 call autocmd_add([#{group: 'TestAcSet', event: 'BufAdd', pattern: '*.sh',
3589 \ cmd: 'echo "bufadd"', once: v:true, nested: v:true}])
3590 call assert_equal([#{cmd: 'echo "bufadd"', group: 'TestAcSet',
3591 \ pattern: '*.sh', nested: v:true, once: v:true,
3592 \ event: 'BufAdd'}], autocmd_get(#{group: 'TestAcSet'}))
3593
3594 " Define two autocmds in the same group
3595 call autocmd_delete([#{group: 'TestAcSet'}])
3596 call autocmd_add([#{group: 'TestAcSet', event: 'BufAdd', pattern: '*.sh',
3597 \ cmd: 'echo "bufadd"'},
3598 \ #{group: 'TestAcSet', event: 'BufEnter', pattern: '*.sh',
3599 \ cmd: 'echo "bufenter"'}])
3600 call assert_equal([
3601 \ #{cmd: 'echo "bufadd"', group: 'TestAcSet', pattern: '*.sh',
3602 \ nested: v:false, once: v:false, event: 'BufAdd'},
3603 \ #{cmd: 'echo "bufenter"', group: 'TestAcSet', pattern: '*.sh',
3604 \ nested: v:false, once: v:false, event: 'BufEnter'}],
3605 \ autocmd_get(#{group: 'TestAcSet'}))
3606
3607 " Define a buffer-local autocmd
3608 call autocmd_delete([#{group: 'TestAcSet'}])
3609 call autocmd_add([#{group: 'TestAcSet', event: 'CursorHold',
3610 \ bufnr: bufnr(), cmd: 'echo "cursorhold"'}])
3611 call assert_equal([
3612 \ #{cmd: 'echo "cursorhold"', group: 'TestAcSet',
3613 \ pattern: '<buffer=' .. bufnr() .. '>', nested: v:false,
3614 \ once: v:false, bufnr: bufnr(), event: 'CursorHold'}],
3615 \ autocmd_get(#{group: 'TestAcSet'}))
3616
3617 " Use an invalid buffer number
3618 call autocmd_delete([#{group: 'TestAcSet'}])
3619 call autocmd_add([#{group: 'TestAcSet', event: 'BufEnter',
3620 \ bufnr: -1, cmd: 'echo "bufenter"'}])
3621 let l = [#{group: 'TestAcSet', event: 'BufAdd', bufnr: 9999,
3622 \ cmd: 'echo "bufadd"'}]
3623 call assert_fails("echo autocmd_add(l)", 'E680:')
Yegappan Lakshmanan00e977c2022-06-01 12:31:53 +01003624 let l = [#{group: 'TestAcSet', event: 'BufAdd', bufnr: 9999,
3625 \ pattern: '*.py', cmd: 'echo "bufadd"'}]
3626 call assert_fails("echo autocmd_add(l)", 'E680:')
3627 let l = [#{group: 'TestAcSet', event: 'BufAdd', bufnr: 9999,
3628 \ pattern: ['*.py', '*.c'], cmd: 'echo "bufadd"'}]
3629 call assert_fails("echo autocmd_add(l)", 'E680:')
Yegappan Lakshmanan1755a912022-05-19 10:31:47 +01003630 let l = [#{group: 'TestAcSet', event: 'BufRead', bufnr: [],
3631 \ cmd: 'echo "bufread"'}]
3632 call assert_fails("echo autocmd_add(l)", 'E745:')
3633 call assert_equal([], autocmd_get(#{group: 'TestAcSet'}))
3634
3635 " Add two commands to the same group, event and pattern
3636 call autocmd_delete([#{group: 'TestAcSet'}])
3637 call autocmd_add([#{group: 'TestAcSet', event: 'BufUnload',
3638 \ pattern: 'abc', cmd: 'echo "cmd1"'}])
3639 call autocmd_add([#{group: 'TestAcSet', event: 'BufUnload',
3640 \ pattern: 'abc', cmd: 'echo "cmd2"'}])
3641 call assert_equal([
3642 \ #{cmd: 'echo "cmd1"', group: 'TestAcSet', pattern: 'abc',
3643 \ nested: v:false, once: v:false, event: 'BufUnload'},
3644 \ #{cmd: 'echo "cmd2"', group: 'TestAcSet', pattern: 'abc',
3645 \ nested: v:false, once: v:false, event: 'BufUnload'}],
3646 \ autocmd_get(#{group: 'TestAcSet'}))
3647
3648 " When adding a new autocmd, if the autocmd 'group' is not specified, then
3649 " the current autocmd group should be used.
3650 call autocmd_delete([#{group: 'TestAcSet'}])
3651 augroup TestAcSet
3652 call autocmd_add([#{event: 'BufHidden', pattern: 'abc', cmd: 'echo "abc"'}])
3653 augroup END
3654 call assert_equal([
3655 \ #{cmd: 'echo "abc"', group: 'TestAcSet', pattern: 'abc',
3656 \ nested: v:false, once: v:false, event: 'BufHidden'}],
3657 \ autocmd_get(#{group: 'TestAcSet'}))
3658
Yegappan Lakshmanan971f6822022-05-24 11:40:11 +01003659 " Test for replacing a cmd for an event in a group
3660 call autocmd_delete([#{group: 'TestAcSet'}])
3661 call autocmd_add([#{replace: v:true, group: 'TestAcSet', event: 'BufEnter',
3662 \ pattern: '*.py', cmd: 'echo "bufenter"'}])
3663 call autocmd_add([#{replace: v:true, group: 'TestAcSet', event: 'BufEnter',
3664 \ pattern: '*.py', cmd: 'echo "bufenter"'}])
3665 call assert_equal([
3666 \ #{cmd: 'echo "bufenter"', group: 'TestAcSet', pattern: '*.py',
3667 \ nested: v:false, once: v:false, event: 'BufEnter'}],
3668 \ autocmd_get(#{group: 'TestAcSet'}))
3669
3670 " Test for adding a command for an unsupported autocmd event
Yegappan Lakshmanan1755a912022-05-19 10:31:47 +01003671 let l = [#{group: 'TestAcSet', event: 'abc', pattern: '*.sh',
3672 \ cmd: 'echo "bufadd"'}]
3673 call assert_fails('call autocmd_add(l)', 'E216:')
3674
Yegappan Lakshmanane0ff3a72022-05-27 18:05:33 +01003675 " Test for using a list of events and patterns
3676 call autocmd_delete([#{group: 'TestAcSet'}])
3677 let l = [#{group: 'TestAcSet', event: ['BufEnter', 'BufLeave'],
3678 \ pattern: ['*.py', '*.sh'], cmd: 'echo "bufcmds"'}]
3679 call autocmd_add(l)
3680 call assert_equal([
3681 \ #{cmd: 'echo "bufcmds"', group: 'TestAcSet', pattern: '*.py',
3682 \ nested: v:false, once: v:false, event: 'BufEnter'},
3683 \ #{cmd: 'echo "bufcmds"', group: 'TestAcSet', pattern: '*.sh',
3684 \ nested: v:false, once: v:false, event: 'BufEnter'},
3685 \ #{cmd: 'echo "bufcmds"', group: 'TestAcSet', pattern: '*.py',
3686 \ nested: v:false, once: v:false, event: 'BufLeave'},
3687 \ #{cmd: 'echo "bufcmds"', group: 'TestAcSet', pattern: '*.sh',
3688 \ nested: v:false, once: v:false, event: 'BufLeave'}],
3689 \ autocmd_get(#{group: 'TestAcSet'}))
3690
3691 " Test for invalid values for 'event' item
3692 call autocmd_delete([#{group: 'TestAcSet'}])
3693 let l = [#{group: 'TestAcSet', event: test_null_string(),
3694 \ pattern: "*.py", cmd: 'echo "bufcmds"'}]
3695 call assert_fails('call autocmd_add(l)', 'E928:')
3696 let l = [#{group: 'TestAcSet', event: test_null_list(),
3697 \ pattern: "*.py", cmd: 'echo "bufcmds"'}]
3698 call assert_fails('call autocmd_add(l)', 'E714:')
3699 let l = [#{group: 'TestAcSet', event: {},
3700 \ pattern: "*.py", cmd: 'echo "bufcmds"'}]
3701 call assert_fails('call autocmd_add(l)', 'E777:')
3702 let l = [#{group: 'TestAcSet', event: [{}],
3703 \ pattern: "*.py", cmd: 'echo "bufcmds"'}]
3704 call assert_fails('call autocmd_add(l)', 'E928:')
3705 let l = [#{group: 'TestAcSet', event: [test_null_string()],
3706 \ pattern: "*.py", cmd: 'echo "bufcmds"'}]
3707 call assert_fails('call autocmd_add(l)', 'E928:')
3708 let l = [#{group: 'TestAcSet', event: 'BufEnter,BufLeave',
3709 \ pattern: '*.py', cmd: 'echo "bufcmds"'}]
3710 call assert_fails('call autocmd_add(l)', 'E216:')
3711 let l = [#{group: 'TestAcSet', event: [],
3712 \ pattern: "*.py", cmd: 'echo "bufcmds"'}]
3713 call autocmd_add(l)
3714 let l = [#{group: 'TestAcSet', event: [""],
3715 \ pattern: "*.py", cmd: 'echo "bufcmds"'}]
3716 call assert_fails('call autocmd_add(l)', 'E216:')
3717 let l = [#{group: 'TestAcSet', event: "",
3718 \ pattern: "*.py", cmd: 'echo "bufcmds"'}]
3719 call autocmd_add(l)
3720 call assert_equal([], autocmd_get(#{group: 'TestAcSet'}))
3721
3722 " Test for invalid values for 'pattern' item
3723 let l = [#{group: 'TestAcSet', event: "BufEnter",
3724 \ pattern: test_null_string(), cmd: 'echo "bufcmds"'}]
Yegappan Lakshmanan00e977c2022-06-01 12:31:53 +01003725 call assert_fails('call autocmd_add(l)', 'E928:')
Yegappan Lakshmanane0ff3a72022-05-27 18:05:33 +01003726 let l = [#{group: 'TestAcSet', event: "BufEnter",
3727 \ pattern: test_null_list(), cmd: 'echo "bufcmds"'}]
3728 call assert_fails('call autocmd_add(l)', 'E714:')
3729 let l = [#{group: 'TestAcSet', event: "BufEnter",
3730 \ pattern: {}, cmd: 'echo "bufcmds"'}]
3731 call assert_fails('call autocmd_add(l)', 'E777:')
3732 let l = [#{group: 'TestAcSet', event: "BufEnter",
3733 \ pattern: [{}], cmd: 'echo "bufcmds"'}]
3734 call assert_fails('call autocmd_add(l)', 'E928:')
3735 let l = [#{group: 'TestAcSet', event: "BufEnter",
3736 \ pattern: [test_null_string()], cmd: 'echo "bufcmds"'}]
3737 call assert_fails('call autocmd_add(l)', 'E928:')
3738 let l = [#{group: 'TestAcSet', event: "BufEnter",
3739 \ pattern: [], cmd: 'echo "bufcmds"'}]
3740 call autocmd_add(l)
3741 let l = [#{group: 'TestAcSet', event: "BufEnter",
3742 \ pattern: [""], cmd: 'echo "bufcmds"'}]
3743 call autocmd_add(l)
3744 let l = [#{group: 'TestAcSet', event: "BufEnter",
3745 \ pattern: "", cmd: 'echo "bufcmds"'}]
3746 call autocmd_add(l)
3747 call assert_equal([], autocmd_get(#{group: 'TestAcSet'}))
3748
3749 let l = [#{group: 'TestAcSet', event: 'BufEnter,abc,BufLeave',
3750 \ pattern: '*.py', cmd: 'echo "bufcmds"'}]
3751 call assert_fails('call autocmd_add(l)', 'E216:')
3752
Yegappan Lakshmanan1755a912022-05-19 10:31:47 +01003753 call assert_fails("call autocmd_add({})", 'E1211:')
3754 call assert_equal(v:false, autocmd_add(test_null_list()))
3755 call assert_true(autocmd_add([[]]))
3756 call assert_true(autocmd_add([test_null_dict()]))
3757
3758 augroup TestAcSet
3759 au!
3760 augroup END
3761
3762 call autocmd_add([#{group: 'TestAcSet'}])
3763 call autocmd_add([#{group: 'TestAcSet', event: 'BufAdd'}])
3764 call autocmd_add([#{group: 'TestAcSet', pat: '*.sh'}])
3765 call autocmd_add([#{group: 'TestAcSet', cmd: 'echo "a"'}])
3766 call autocmd_add([#{group: 'TestAcSet', event: 'BufAdd', pat: '*.sh'}])
3767 call autocmd_add([#{group: 'TestAcSet', event: 'BufAdd', cmd: 'echo "a"'}])
3768 call autocmd_add([#{group: 'TestAcSet', pat: '*.sh', cmd: 'echo "a"'}])
3769 call assert_equal([], autocmd_get(#{group: 'TestAcSet'}))
3770
3771 augroup! TestAcSet
3772endfunc
3773
3774" Test for deleting autocmd events and groups
3775func Test_autocmd_delete()
3776 " Delete an event in an autocmd group
3777 augroup TestAcSet
3778 au!
3779 au BufAdd *.sh echo "bufadd"
3780 au BufEnter *.sh echo "bufenter"
3781 augroup END
3782 call autocmd_delete([#{group: 'TestAcSet', event: 'BufAdd'}])
3783 call assert_equal([#{cmd: 'echo "bufenter"', group: 'TestAcSet',
3784 \ pattern: '*.sh', nested: v:false, once: v:false,
3785 \ event: 'BufEnter'}], autocmd_get(#{group: 'TestAcSet'}))
3786
3787 " Delete all the events in an autocmd group
3788 augroup TestAcSet
3789 au BufAdd *.sh echo "bufadd"
3790 augroup END
3791 call autocmd_delete([#{group: 'TestAcSet', event: '*'}])
3792 call assert_equal([], autocmd_get(#{group: 'TestAcSet'}))
3793
3794 " Delete a non-existing autocmd group
3795 call assert_fails("call autocmd_delete([#{group: 'abc'}])", 'E367:')
3796 " Delete a non-existing autocmd event
3797 let l = [#{group: 'TestAcSet', event: 'abc'}]
3798 call assert_fails("call autocmd_delete(l)", 'E216:')
3799 " Delete a non-existing autocmd pattern
3800 let l = [#{group: 'TestAcSet', event: 'BufAdd', pat: 'abc'}]
3801 call assert_true(autocmd_delete(l))
Yegappan Lakshmanan00e977c2022-06-01 12:31:53 +01003802 " Delete an autocmd for a non-existing buffer
3803 let l = [#{event: '*', bufnr: 9999, cmd: 'echo "x"'}]
3804 call assert_fails('call autocmd_delete(l)', 'E680:')
Yegappan Lakshmanan1755a912022-05-19 10:31:47 +01003805
3806 " Delete an autocmd group
3807 augroup TestAcSet
3808 au!
3809 au BufAdd *.sh echo "bufadd"
3810 au BufEnter *.sh echo "bufenter"
3811 augroup END
3812 call autocmd_delete([#{group: 'TestAcSet'}])
3813 call assert_fails("call autocmd_get(#{group: 'TestAcSet'})", 'E367:')
3814
3815 call assert_true(autocmd_delete([[]]))
3816 call assert_true(autocmd_delete([test_null_dict()]))
3817endfunc
3818
Bram Moolenaarbc2b71d2020-02-17 21:33:30 +01003819" vim: shiftwidth=2 sts=2 expandtab