blob: 028e3d22e70c9169bc026af5b4373f30b5eb85f3 [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 Moolenaar3b0d70f2022-08-29 22:31:20 +0100710 call mkdir('Xbufenterdir')
711 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
717 call delete('Xbufenterdir', 'd')
Bram Moolenaare13b9af2017-01-13 22:01:02 +0100718 au! BufEnter
Bram Moolenaara9b5b852022-08-26 13:16:20 +0100719
720 " Editing a "nofile" buffer doesn't read the file but does trigger BufEnter
zeertzjq93f72cc2022-08-26 15:34:52 +0100721 " for historic reasons. Also test other 'buftype' values.
722 for val in ['nofile',
723 \ 'nowrite',
724 \ 'acwrite',
725 \ 'quickfix',
726 \ 'help',
727 \ 'terminal',
728 \ 'prompt',
729 \ 'popup',
730 \ ]
731 new somefile
732 exe 'set buftype=' .. val
733 au BufEnter somefile call setline(1, 'some text')
734 edit
735 call assert_equal('some text', getline(1))
736 bwipe!
737 au! BufEnter
738 endfor
Bram Moolenaare13b9af2017-01-13 22:01:02 +0100739endfunc
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100740
741" Closing a window might cause an endless loop
742" E814 for older Vims
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200743func Test_autocmd_bufwipe_in_SessLoadPost()
Bram Moolenaar1d68d9b2017-10-13 22:33:32 +0200744 edit Xtest
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100745 tabnew
Bram Moolenaar1d68d9b2017-10-13 22:33:32 +0200746 file Xsomething
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100747 set noswapfile
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100748 mksession!
749
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200750 let content =<< trim [CODE]
Bram Moolenaar62cd26a2020-10-11 20:08:44 +0200751 call test_override('ui_delay', 10)
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200752 set nocp noswapfile
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100753 let v:swapchoice = "e"
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200754 augroup test_autocmd_sessionload
755 autocmd!
756 autocmd SessionLoadPost * exe bufnr("Xsomething") . "bw!"
757 augroup END
758
759 func WriteErrors()
760 call writefile([execute("messages")], "Xerrors")
761 endfunc
762 au VimLeave * call WriteErrors()
763 [CODE]
764
Bram Moolenaare1f3ab72022-09-04 21:29:08 +0100765 call writefile(content, 'Xvimrc', 'D')
Bram Moolenaar93344c22019-08-14 21:12:05 +0200766 call system(GetVimCommand('Xvimrc') .. ' --not-a-term --noplugins -S Session.vim -c cq')
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')
790 call assert_match('OK', readfile('Xerrors')->join())
791
Bram Moolenaar797e63b2021-01-15 16:22:52 +0100792 call delete('Xerrors')
793endfunc
794
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100795" SEGV occurs in older versions.
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200796func Test_autocmd_bufwipe_in_SessLoadPost2()
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100797 tabnew
798 set noswapfile
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100799 mksession!
800
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200801 let content =<< trim [CODE]
802 set nocp noswapfile
803 function! DeleteInactiveBufs()
804 tabfirst
805 let tabblist = []
806 for i in range(1, tabpagenr(''$''))
807 call extend(tabblist, tabpagebuflist(i))
808 endfor
809 for b in range(1, bufnr(''$''))
810 if bufexists(b) && buflisted(b) && (index(tabblist, b) == -1 || bufname(b) =~# ''^$'')
811 exec ''bwipeout '' . b
812 endif
813 endfor
814 echomsg "SessionLoadPost DONE"
815 endfunction
816 au SessionLoadPost * call DeleteInactiveBufs()
817
818 func WriteErrors()
819 call writefile([execute("messages")], "Xerrors")
820 endfunc
821 au VimLeave * call WriteErrors()
822 [CODE]
823
Bram Moolenaare1f3ab72022-09-04 21:29:08 +0100824 call writefile(content, 'Xvimrc', 'D')
Bram Moolenaar93344c22019-08-14 21:12:05 +0200825 call system(GetVimCommand('Xvimrc') .. ' --not-a-term --noplugins -S Session.vim -c cq')
Bram Moolenaare94260f2017-03-21 15:50:12 +0100826 let errors = join(readfile('Xerrors'))
827 " This probably only ever matches on unix.
828 call assert_notmatch('Caught deadly signal SEGV', errors)
829 call assert_match('SessionLoadPost DONE', errors)
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100830
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100831 set swapfile
Bram Moolenaare1f3ab72022-09-04 21:29:08 +0100832 for file in ['Session.vim', 'Xerrors']
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100833 call delete(file)
834 endfor
835endfunc
Bram Moolenaarfaf29d72017-07-09 11:07:16 +0200836
837func Test_empty_doau()
838 doau \|
839endfunc
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200840
841func s:AutoCommandOptionSet(match)
Bram Moolenaard7c96872019-06-15 17:12:48 +0200842 let template = "Option: <%s>, OldVal: <%s>, OldValLocal: <%s>, OldValGlobal: <%s>, NewVal: <%s>, Scope: <%s>, Command: <%s>\n"
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200843 let item = remove(g:options, 0)
Bram Moolenaard7c96872019-06-15 17:12:48 +0200844 let expected = printf(template, item[0], item[1], item[2], item[3], item[4], item[5], item[6])
845 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 +0200846 let g:opt = [expected, actual]
847 "call assert_equal(expected, actual)
848endfunc
849
850func Test_OptionSet()
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +0200851 CheckOption autochdir
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200852
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +0200853 badd test_autocmd.vim
854
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200855 call test_override('starting', 1)
856 set nocp
857 au OptionSet * :call s:AutoCommandOptionSet(expand("<amatch>"))
858
859 " 1: Setting number option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100860 let g:options = [['number', 0, 0, 0, 1, 'global', 'set']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200861 set nu
862 call assert_equal([], g:options)
863 call assert_equal(g:opt[0], g:opt[1])
864
865 " 2: Setting local number option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100866 let g:options = [['number', 1, 1, '', 0, 'local', 'setlocal']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200867 setlocal nonu
868 call assert_equal([], g:options)
869 call assert_equal(g:opt[0], g:opt[1])
870
871 " 3: Setting global number option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100872 let g:options = [['number', 1, '', 1, 0, 'global', 'setglobal']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200873 setglobal nonu
874 call assert_equal([], g:options)
875 call assert_equal(g:opt[0], g:opt[1])
876
877 " 4: Setting local autoindent option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100878 let g:options = [['autoindent', 0, 0, '', 1, 'local', 'setlocal']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200879 setlocal ai
880 call assert_equal([], g:options)
881 call assert_equal(g:opt[0], g:opt[1])
882
883 " 5: Setting global autoindent option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100884 let g:options = [['autoindent', 0, '', 0, 1, 'global', 'setglobal']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200885 setglobal ai
886 call assert_equal([], g:options)
887 call assert_equal(g:opt[0], g:opt[1])
888
889 " 6: Setting global autoindent option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100890 let g:options = [['autoindent', 1, 1, 1, 0, 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +0200891 set ai!
892 call assert_equal([], g:options)
893 call assert_equal(g:opt[0], g:opt[1])
894
895 " 6a: Setting global autoindent option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100896 let g:options = [['autoindent', 1, 1, 0, 0, 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +0200897 noa setlocal ai
898 noa setglobal noai
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200899 set ai!
900 call assert_equal([], g:options)
901 call assert_equal(g:opt[0], g:opt[1])
902
903 " Should not print anything, use :noa
904 " 7: don't trigger OptionSet"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100905 let g:options = [['invalid', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200906 noa set nonu
Bram Moolenaard7c96872019-06-15 17:12:48 +0200907 call assert_equal([['invalid', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid']], g:options)
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200908 call assert_equal(g:opt[0], g:opt[1])
909
910 " 8: Setting several global list and number option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100911 let g:options = [['list', 0, 0, 0, 1, 'global', 'set'], ['number', 0, 0, 0, 1, 'global', 'set']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200912 set list nu
913 call assert_equal([], g:options)
914 call assert_equal(g:opt[0], g:opt[1])
915
916 " 9: don't trigger OptionSet"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100917 let g:options = [['invalid', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid'], ['invalid', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200918 noa set nolist nonu
Bram Moolenaard7c96872019-06-15 17:12:48 +0200919 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 +0200920 call assert_equal(g:opt[0], g:opt[1])
921
922 " 10: Setting global acd"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100923 let g:options = [['autochdir', 0, 0, '', 1, 'local', 'setlocal']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200924 setlocal acd
925 call assert_equal([], g:options)
926 call assert_equal(g:opt[0], g:opt[1])
927
928 " 11: Setting global autoread (also sets local value)"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100929 let g:options = [['autoread', 0, 0, 0, 1, 'global', 'set']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200930 set ar
931 call assert_equal([], g:options)
932 call assert_equal(g:opt[0], g:opt[1])
933
934 " 12: Setting local autoread"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100935 let g:options = [['autoread', 1, 1, '', 1, 'local', 'setlocal']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200936 setlocal ar
937 call assert_equal([], g:options)
938 call assert_equal(g:opt[0], g:opt[1])
939
940 " 13: Setting global autoread"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100941 let g:options = [['autoread', 1, '', 1, 0, 'global', 'setglobal']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200942 setglobal invar
943 call assert_equal([], g:options)
944 call assert_equal(g:opt[0], g:opt[1])
945
946 " 14: Setting option backspace through :let"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100947 let g:options = [['backspace', '', '', '', 'eol,indent,start', 'global', 'set']]
948 let &bs = "eol,indent,start"
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200949 call assert_equal([], g:options)
950 call assert_equal(g:opt[0], g:opt[1])
951
952 " 15: Setting option backspace through setbufvar()"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100953 let g:options = [['backup', 0, 0, '', 1, 'local', 'setlocal']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200954 " try twice, first time, shouldn't trigger because option name is invalid,
955 " second time, it should trigger
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +0200956 let bnum = bufnr('%')
Bram Moolenaare2e40752020-09-04 21:18:46 +0200957 call assert_fails("call setbufvar(bnum, '&l:bk', 1)", 'E355:')
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200958 " should trigger, use correct option name
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +0200959 call setbufvar(bnum, '&backup', 1)
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200960 call assert_equal([], g:options)
961 call assert_equal(g:opt[0], g:opt[1])
962
963 " 16: Setting number option using setwinvar"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100964 let g:options = [['number', 0, 0, '', 1, 'local', 'setlocal']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200965 call setwinvar(0, '&number', 1)
966 call assert_equal([], g:options)
967 call assert_equal(g:opt[0], g:opt[1])
968
969 " 17: Setting key option, shouldn't trigger"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100970 let g:options = [['key', 'invalid', 'invalid1', 'invalid2', 'invalid3', 'invalid4', 'invalid5']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200971 setlocal key=blah
972 setlocal key=
Bram Moolenaard7c96872019-06-15 17:12:48 +0200973 call assert_equal([['key', 'invalid', 'invalid1', 'invalid2', 'invalid3', 'invalid4', 'invalid5']], g:options)
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200974 call assert_equal(g:opt[0], g:opt[1])
975
Bram Moolenaard7c96872019-06-15 17:12:48 +0200976
977 " 18a: Setting string global option"
978 let oldval = &backupext
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100979 let g:options = [['backupext', oldval, oldval, oldval, 'foo', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +0200980 set backupext=foo
981 call assert_equal([], g:options)
982 call assert_equal(g:opt[0], g:opt[1])
983
984 " 18b: Resetting string global option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100985 let g:options = [['backupext', 'foo', 'foo', 'foo', oldval, 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +0200986 set backupext&
987 call assert_equal([], g:options)
988 call assert_equal(g:opt[0], g:opt[1])
989
990 " 18c: Setting global string global option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100991 let g:options = [['backupext', oldval, '', oldval, 'bar', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +0200992 setglobal backupext=bar
993 call assert_equal([], g:options)
994 call assert_equal(g:opt[0], g:opt[1])
995
996 " 18d: Setting local string global option"
997 " As this is a global option this sets the global value even though
998 " :setlocal is used!
999 noa set backupext& " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001000 let g:options = [['backupext', oldval, oldval, '', 'baz', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001001 setlocal backupext=baz
1002 call assert_equal([], g:options)
1003 call assert_equal(g:opt[0], g:opt[1])
1004
1005 " 18e: Setting again string global option"
1006 noa setglobal backupext=ext_global " Reset global and local value (without triggering autocmd)
1007 noa setlocal backupext=ext_local " Sets the global(!) value!
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001008 let g:options = [['backupext', 'ext_local', 'ext_local', 'ext_local', 'fuu', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001009 set backupext=fuu
1010 call assert_equal([], g:options)
1011 call assert_equal(g:opt[0], g:opt[1])
1012
1013
zeertzjqb811de52021-10-21 10:50:44 +01001014 " 19a: Setting string global-local (to buffer) option"
Bram Moolenaar8efa0262017-08-20 15:47:20 +02001015 let oldval = &tags
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001016 let g:options = [['tags', oldval, oldval, oldval, 'tagpath', 'global', 'set']]
Bram Moolenaar8efa0262017-08-20 15:47:20 +02001017 set tags=tagpath
1018 call assert_equal([], g:options)
1019 call assert_equal(g:opt[0], g:opt[1])
1020
zeertzjqb811de52021-10-21 10:50:44 +01001021 " 19b: Resetting string global-local (to buffer) option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001022 let g:options = [['tags', 'tagpath', 'tagpath', 'tagpath', oldval, 'global', 'set']]
Bram Moolenaar8efa0262017-08-20 15:47:20 +02001023 set tags&
1024 call assert_equal([], g:options)
1025 call assert_equal(g:opt[0], g:opt[1])
1026
zeertzjqb811de52021-10-21 10:50:44 +01001027 " 19c: Setting global string global-local (to buffer) option "
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001028 let g:options = [['tags', oldval, '', oldval, 'tagpath1', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001029 setglobal tags=tagpath1
1030 call assert_equal([], g:options)
1031 call assert_equal(g:opt[0], g:opt[1])
1032
zeertzjqb811de52021-10-21 10:50:44 +01001033 " 19d: Setting local string global-local (to buffer) option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001034 let g:options = [['tags', 'tagpath1', 'tagpath1', '', 'tagpath2', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001035 setlocal tags=tagpath2
1036 call assert_equal([], g:options)
1037 call assert_equal(g:opt[0], g:opt[1])
1038
zeertzjqb811de52021-10-21 10:50:44 +01001039 " 19e: Setting again string global-local (to buffer) option"
1040 " Note: v:option_old is the old global value for global-local string options
Bram Moolenaard7c96872019-06-15 17:12:48 +02001041 " but the old local value for all other kinds of options.
1042 noa setglobal tags=tag_global " Reset global and local value (without triggering autocmd)
1043 noa setlocal tags=tag_local
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001044 let g:options = [['tags', 'tag_global', 'tag_local', 'tag_global', 'tagpath', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001045 set tags=tagpath
1046 call assert_equal([], g:options)
1047 call assert_equal(g:opt[0], g:opt[1])
1048
zeertzjqb811de52021-10-21 10:50:44 +01001049 " 19f: Setting string global-local (to buffer) option to an empty string"
1050 " Note: v:option_old is the old global value for global-local string options
Bram Moolenaard7c96872019-06-15 17:12:48 +02001051 " but the old local value for all other kinds of options.
1052 noa set tags=tag_global " Reset global and local value (without triggering autocmd)
1053 noa setlocal tags= " empty string
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001054 let g:options = [['tags', 'tag_global', '', 'tag_global', 'tagpath', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001055 set tags=tagpath
1056 call assert_equal([], g:options)
1057 call assert_equal(g:opt[0], g:opt[1])
1058
1059
1060 " 20a: Setting string local (to buffer) option"
1061 let oldval = &spelllang
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001062 let g:options = [['spelllang', oldval, oldval, oldval, 'elvish,klingon', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001063 set spelllang=elvish,klingon
1064 call assert_equal([], g:options)
1065 call assert_equal(g:opt[0], g:opt[1])
1066
1067 " 20b: Resetting string local (to buffer) option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001068 let g:options = [['spelllang', 'elvish,klingon', 'elvish,klingon', 'elvish,klingon', oldval, 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001069 set spelllang&
1070 call assert_equal([], g:options)
1071 call assert_equal(g:opt[0], g:opt[1])
1072
1073 " 20c: Setting global string local (to buffer) option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001074 let g:options = [['spelllang', oldval, '', oldval, 'elvish', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001075 setglobal spelllang=elvish
1076 call assert_equal([], g:options)
1077 call assert_equal(g:opt[0], g:opt[1])
1078
1079 " 20d: Setting local string local (to buffer) option"
1080 noa set spelllang& " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001081 let g:options = [['spelllang', oldval, oldval, '', 'klingon', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001082 setlocal spelllang=klingon
1083 call assert_equal([], g:options)
1084 call assert_equal(g:opt[0], g:opt[1])
1085
1086 " 20e: Setting again string local (to buffer) option"
zeertzjqb811de52021-10-21 10:50:44 +01001087 " Note: v:option_old is the old global value for global-local string options
Bram Moolenaard7c96872019-06-15 17:12:48 +02001088 " but the old local value for all other kinds of options.
1089 noa setglobal spelllang=spellglobal " Reset global and local value (without triggering autocmd)
1090 noa setlocal spelllang=spelllocal
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001091 let g:options = [['spelllang', 'spelllocal', 'spelllocal', 'spellglobal', 'foo', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001092 set spelllang=foo
1093 call assert_equal([], g:options)
1094 call assert_equal(g:opt[0], g:opt[1])
1095
1096
zeertzjqb811de52021-10-21 10:50:44 +01001097 " 21a: Setting string global-local (to window) option"
Bram Moolenaard7c96872019-06-15 17:12:48 +02001098 let oldval = &statusline
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001099 let g:options = [['statusline', oldval, oldval, oldval, 'foo', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001100 set statusline=foo
1101 call assert_equal([], g:options)
1102 call assert_equal(g:opt[0], g:opt[1])
1103
zeertzjqb811de52021-10-21 10:50:44 +01001104 " 21b: Resetting string global-local (to window) option"
1105 " Note: v:option_old is the old global value for global-local string options
Bram Moolenaard7c96872019-06-15 17:12:48 +02001106 " but the old local value for all other kinds of options.
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001107 let g:options = [['statusline', 'foo', 'foo', 'foo', oldval, 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001108 set statusline&
1109 call assert_equal([], g:options)
1110 call assert_equal(g:opt[0], g:opt[1])
1111
zeertzjqb811de52021-10-21 10:50:44 +01001112 " 21c: Setting global string global-local (to window) option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001113 let g:options = [['statusline', oldval, '', oldval, 'bar', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001114 setglobal statusline=bar
1115 call assert_equal([], g:options)
1116 call assert_equal(g:opt[0], g:opt[1])
1117
zeertzjqb811de52021-10-21 10:50:44 +01001118 " 21d: Setting local string global-local (to window) option"
Bram Moolenaard7c96872019-06-15 17:12:48 +02001119 noa set statusline& " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001120 let g:options = [['statusline', oldval, oldval, '', 'baz', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001121 setlocal statusline=baz
1122 call assert_equal([], g:options)
1123 call assert_equal(g:opt[0], g:opt[1])
1124
zeertzjqb811de52021-10-21 10:50:44 +01001125 " 21e: Setting again string global-local (to window) option"
1126 " Note: v:option_old is the old global value for global-local string options
Bram Moolenaard7c96872019-06-15 17:12:48 +02001127 " but the old local value for all other kinds of options.
1128 noa setglobal statusline=bar " Reset global and local value (without triggering autocmd)
1129 noa setlocal statusline=baz
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001130 let g:options = [['statusline', 'bar', 'baz', 'bar', 'foo', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001131 set statusline=foo
1132 call assert_equal([], g:options)
1133 call assert_equal(g:opt[0], g:opt[1])
1134
1135
1136 " 22a: Setting string local (to window) option"
1137 let oldval = &foldignore
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001138 let g:options = [['foldignore', oldval, oldval, oldval, 'fo', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001139 set foldignore=fo
1140 call assert_equal([], g:options)
1141 call assert_equal(g:opt[0], g:opt[1])
1142
1143 " 22b: Resetting string local (to window) option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001144 let g:options = [['foldignore', 'fo', 'fo', 'fo', oldval, 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001145 set foldignore&
1146 call assert_equal([], g:options)
1147 call assert_equal(g:opt[0], g:opt[1])
1148
1149 " 22c: Setting global string local (to window) option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001150 let g:options = [['foldignore', oldval, '', oldval, 'bar', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001151 setglobal foldignore=bar
1152 call assert_equal([], g:options)
1153 call assert_equal(g:opt[0], g:opt[1])
1154
1155 " 22d: Setting local string local (to window) option"
1156 noa set foldignore& " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001157 let g:options = [['foldignore', oldval, oldval, '', 'baz', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001158 setlocal foldignore=baz
1159 call assert_equal([], g:options)
1160 call assert_equal(g:opt[0], g:opt[1])
1161
1162 " 22e: Setting again string local (to window) option"
1163 noa setglobal foldignore=glob " Reset global and local value (without triggering autocmd)
1164 noa setlocal foldignore=loc
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001165 let g:options = [['foldignore', 'loc', 'loc', 'glob', 'fo', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001166 set foldignore=fo
1167 call assert_equal([], g:options)
1168 call assert_equal(g:opt[0], g:opt[1])
1169
1170
zeertzjqb811de52021-10-21 10:50:44 +01001171 " 23a: Setting global number global option"
Bram Moolenaard7c96872019-06-15 17:12:48 +02001172 noa setglobal cmdheight=8 " Reset global and local value (without triggering autocmd)
1173 noa setlocal cmdheight=1 " Sets the global(!) value!
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001174 let g:options = [['cmdheight', '1', '', '1', '2', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001175 setglobal cmdheight=2
1176 call assert_equal([], g:options)
1177 call assert_equal(g:opt[0], g:opt[1])
1178
1179 " 23b: Setting local number global option"
1180 noa setglobal cmdheight=8 " Reset global and local value (without triggering autocmd)
1181 noa setlocal cmdheight=1 " Sets the global(!) value!
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001182 let g:options = [['cmdheight', '1', '1', '', '2', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001183 setlocal cmdheight=2
1184 call assert_equal([], g:options)
1185 call assert_equal(g:opt[0], g:opt[1])
1186
1187 " 23c: Setting again number global option"
1188 noa setglobal cmdheight=8 " Reset global and local value (without triggering autocmd)
1189 noa setlocal cmdheight=1 " Sets the global(!) value!
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001190 let g:options = [['cmdheight', '1', '1', '1', '2', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001191 set cmdheight=2
1192 call assert_equal([], g:options)
1193 call assert_equal(g:opt[0], g:opt[1])
1194
1195 " 23d: Setting again number global option"
1196 noa set cmdheight=8 " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001197 let g:options = [['cmdheight', '8', '8', '8', '2', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001198 set cmdheight=2
1199 call assert_equal([], g:options)
1200 call assert_equal(g:opt[0], g:opt[1])
1201
1202
1203 " 24a: Setting global number global-local (to buffer) option"
1204 noa setglobal undolevels=8 " Reset global and local value (without triggering autocmd)
1205 noa setlocal undolevels=1
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001206 let g:options = [['undolevels', '8', '', '8', '2', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001207 setglobal undolevels=2
1208 call assert_equal([], g:options)
1209 call assert_equal(g:opt[0], g:opt[1])
1210
1211 " 24b: Setting local number global-local (to buffer) option"
1212 noa setglobal undolevels=8 " Reset global and local value (without triggering autocmd)
1213 noa setlocal undolevels=1
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001214 let g:options = [['undolevels', '1', '1', '', '2', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001215 setlocal undolevels=2
1216 call assert_equal([], g:options)
1217 call assert_equal(g:opt[0], g:opt[1])
1218
1219 " 24c: Setting again number global-local (to buffer) option"
1220 noa setglobal undolevels=8 " Reset global and local value (without triggering autocmd)
1221 noa setlocal undolevels=1
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001222 let g:options = [['undolevels', '1', '1', '8', '2', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001223 set undolevels=2
1224 call assert_equal([], g:options)
1225 call assert_equal(g:opt[0], g:opt[1])
1226
1227 " 24d: Setting again global number global-local (to buffer) option"
1228 noa set undolevels=8 " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001229 let g:options = [['undolevels', '8', '8', '8', '2', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001230 set undolevels=2
1231 call assert_equal([], g:options)
1232 call assert_equal(g:opt[0], g:opt[1])
1233
1234
1235 " 25a: Setting global number local (to buffer) option"
1236 noa setglobal wrapmargin=8 " Reset global and local value (without triggering autocmd)
1237 noa setlocal wrapmargin=1
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001238 let g:options = [['wrapmargin', '8', '', '8', '2', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001239 setglobal wrapmargin=2
1240 call assert_equal([], g:options)
1241 call assert_equal(g:opt[0], g:opt[1])
1242
1243 " 25b: Setting local number local (to buffer) option"
1244 noa setglobal wrapmargin=8 " Reset global and local value (without triggering autocmd)
1245 noa setlocal wrapmargin=1
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001246 let g:options = [['wrapmargin', '1', '1', '', '2', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001247 setlocal wrapmargin=2
1248 call assert_equal([], g:options)
1249 call assert_equal(g:opt[0], g:opt[1])
1250
1251 " 25c: Setting again number local (to buffer) option"
1252 noa setglobal wrapmargin=8 " Reset global and local value (without triggering autocmd)
1253 noa setlocal wrapmargin=1
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001254 let g:options = [['wrapmargin', '1', '1', '8', '2', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001255 set wrapmargin=2
1256 call assert_equal([], g:options)
1257 call assert_equal(g:opt[0], g:opt[1])
1258
1259 " 25d: Setting again global number local (to buffer) option"
1260 noa set wrapmargin=8 " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001261 let g:options = [['wrapmargin', '8', '8', '8', '2', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001262 set wrapmargin=2
1263 call assert_equal([], g:options)
1264 call assert_equal(g:opt[0], g:opt[1])
1265
1266
1267 " 26: Setting number global-local (to window) option.
1268 " Such option does currently not exist.
1269
1270
1271 " 27a: Setting global number local (to window) option"
1272 noa setglobal foldcolumn=8 " Reset global and local value (without triggering autocmd)
1273 noa setlocal foldcolumn=1
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001274 let g:options = [['foldcolumn', '8', '', '8', '2', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001275 setglobal foldcolumn=2
1276 call assert_equal([], g:options)
1277 call assert_equal(g:opt[0], g:opt[1])
1278
1279 " 27b: Setting local number local (to window) option"
1280 noa setglobal foldcolumn=8 " Reset global and local value (without triggering autocmd)
1281 noa setlocal foldcolumn=1
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001282 let g:options = [['foldcolumn', '1', '1', '', '2', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001283 setlocal foldcolumn=2
1284 call assert_equal([], g:options)
1285 call assert_equal(g:opt[0], g:opt[1])
1286
1287 " 27c: Setting again number local (to window) option"
1288 noa setglobal foldcolumn=8 " Reset global and local value (without triggering autocmd)
1289 noa setlocal foldcolumn=1
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001290 let g:options = [['foldcolumn', '1', '1', '8', '2', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001291 set foldcolumn=2
1292 call assert_equal([], g:options)
1293 call assert_equal(g:opt[0], g:opt[1])
1294
zeertzjqb811de52021-10-21 10:50:44 +01001295 " 27d: Setting again global number local (to window) option"
Bram Moolenaard7c96872019-06-15 17:12:48 +02001296 noa set foldcolumn=8 " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001297 let g:options = [['foldcolumn', '8', '8', '8', '2', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001298 set foldcolumn=2
1299 call assert_equal([], g:options)
1300 call assert_equal(g:opt[0], g:opt[1])
1301
1302
1303 " 28a: Setting global boolean global option"
1304 noa setglobal nowrapscan " Reset global and local value (without triggering autocmd)
1305 noa setlocal wrapscan " Sets the global(!) value!
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001306 let g:options = [['wrapscan', '1', '', '1', '0', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001307 setglobal nowrapscan
1308 call assert_equal([], g:options)
1309 call assert_equal(g:opt[0], g:opt[1])
1310
1311 " 28b: Setting local boolean global option"
1312 noa setglobal nowrapscan " Reset global and local value (without triggering autocmd)
1313 noa setlocal wrapscan " Sets the global(!) value!
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001314 let g:options = [['wrapscan', '1', '1', '', '0', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001315 setlocal nowrapscan
1316 call assert_equal([], g:options)
1317 call assert_equal(g:opt[0], g:opt[1])
1318
1319 " 28c: Setting again boolean global option"
1320 noa setglobal nowrapscan " Reset global and local value (without triggering autocmd)
1321 noa setlocal wrapscan " Sets the global(!) value!
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001322 let g:options = [['wrapscan', '1', '1', '1', '0', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001323 set nowrapscan
1324 call assert_equal([], g:options)
1325 call assert_equal(g:opt[0], g:opt[1])
1326
1327 " 28d: Setting again global boolean global option"
1328 noa set nowrapscan " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001329 let g:options = [['wrapscan', '0', '0', '0', '1', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001330 set wrapscan
1331 call assert_equal([], g:options)
1332 call assert_equal(g:opt[0], g:opt[1])
1333
1334
1335 " 29a: Setting global boolean global-local (to buffer) option"
1336 noa setglobal noautoread " Reset global and local value (without triggering autocmd)
1337 noa setlocal autoread
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001338 let g:options = [['autoread', '0', '', '0', '1', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001339 setglobal autoread
1340 call assert_equal([], g:options)
1341 call assert_equal(g:opt[0], g:opt[1])
1342
1343 " 29b: Setting local boolean global-local (to buffer) option"
1344 noa setglobal noautoread " Reset global and local value (without triggering autocmd)
1345 noa setlocal autoread
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001346 let g:options = [['autoread', '1', '1', '', '0', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001347 setlocal noautoread
1348 call assert_equal([], g:options)
1349 call assert_equal(g:opt[0], g:opt[1])
1350
1351 " 29c: Setting again boolean global-local (to buffer) option"
1352 noa setglobal noautoread " Reset global and local value (without triggering autocmd)
1353 noa setlocal autoread
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001354 let g:options = [['autoread', '1', '1', '0', '1', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001355 set autoread
1356 call assert_equal([], g:options)
1357 call assert_equal(g:opt[0], g:opt[1])
1358
1359 " 29d: Setting again global boolean global-local (to buffer) option"
1360 noa set noautoread " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001361 let g:options = [['autoread', '0', '0', '0', '1', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001362 set autoread
1363 call assert_equal([], g:options)
1364 call assert_equal(g:opt[0], g:opt[1])
1365
1366
1367 " 30a: Setting global boolean local (to buffer) option"
1368 noa setglobal nocindent " Reset global and local value (without triggering autocmd)
1369 noa setlocal cindent
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001370 let g:options = [['cindent', '0', '', '0', '1', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001371 setglobal cindent
1372 call assert_equal([], g:options)
1373 call assert_equal(g:opt[0], g:opt[1])
1374
1375 " 30b: Setting local boolean local (to buffer) option"
1376 noa setglobal nocindent " Reset global and local value (without triggering autocmd)
1377 noa setlocal cindent
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001378 let g:options = [['cindent', '1', '1', '', '0', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001379 setlocal nocindent
1380 call assert_equal([], g:options)
1381 call assert_equal(g:opt[0], g:opt[1])
1382
1383 " 30c: Setting again boolean local (to buffer) option"
1384 noa setglobal nocindent " Reset global and local value (without triggering autocmd)
1385 noa setlocal cindent
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001386 let g:options = [['cindent', '1', '1', '0', '1', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001387 set cindent
1388 call assert_equal([], g:options)
1389 call assert_equal(g:opt[0], g:opt[1])
1390
1391 " 30d: Setting again global boolean local (to buffer) option"
1392 noa set nocindent " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001393 let g:options = [['cindent', '0', '0', '0', '1', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001394 set cindent
1395 call assert_equal([], g:options)
1396 call assert_equal(g:opt[0], g:opt[1])
1397
1398
1399 " 31: Setting boolean global-local (to window) option
1400 " Currently no such option exists.
1401
1402
1403 " 32a: Setting global boolean local (to window) option"
1404 noa setglobal nocursorcolumn " Reset global and local value (without triggering autocmd)
1405 noa setlocal cursorcolumn
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001406 let g:options = [['cursorcolumn', '0', '', '0', '1', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001407 setglobal cursorcolumn
1408 call assert_equal([], g:options)
1409 call assert_equal(g:opt[0], g:opt[1])
1410
1411 " 32b: Setting local boolean local (to window) option"
1412 noa setglobal nocursorcolumn " Reset global and local value (without triggering autocmd)
1413 noa setlocal cursorcolumn
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001414 let g:options = [['cursorcolumn', '1', '1', '', '0', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001415 setlocal nocursorcolumn
1416 call assert_equal([], g:options)
1417 call assert_equal(g:opt[0], g:opt[1])
1418
1419 " 32c: Setting again boolean local (to window) option"
1420 noa setglobal nocursorcolumn " Reset global and local value (without triggering autocmd)
1421 noa setlocal cursorcolumn
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001422 let g:options = [['cursorcolumn', '1', '1', '0', '1', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001423 set cursorcolumn
1424 call assert_equal([], g:options)
1425 call assert_equal(g:opt[0], g:opt[1])
1426
1427 " 32d: Setting again global boolean local (to window) option"
1428 noa set nocursorcolumn " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001429 let g:options = [['cursorcolumn', '0', '0', '0', '1', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001430 set cursorcolumn
1431 call assert_equal([], g:options)
1432 call assert_equal(g:opt[0], g:opt[1])
1433
1434
Bram Moolenaar1bc353b2019-09-01 14:45:28 +02001435 " 33: Test autocommands when an option value is converted internally.
Bram Moolenaard7c96872019-06-15 17:12:48 +02001436 noa set backspace=1 " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001437 let g:options = [['backspace', 'indent,eol', 'indent,eol', 'indent,eol', '2', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001438 set backspace=2
1439 call assert_equal([], g:options)
1440 call assert_equal(g:opt[0], g:opt[1])
1441
1442
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001443 " Cleanup
1444 au! OptionSet
Bram Moolenaar0331faf2019-06-15 18:40:37 +02001445 " set tags&
Bram Moolenaard7c96872019-06-15 17:12:48 +02001446 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 +02001447 exe printf(":set %s&vim", opt)
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001448 endfor
1449 call test_override('starting', 0)
1450 delfunc! AutoCommandOptionSet
1451endfunc
1452
1453func Test_OptionSet_diffmode()
1454 call test_override('starting', 1)
Bram Moolenaar26d98212019-01-27 22:32:55 +01001455 " 18: Changing an option when entering diff mode
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001456 new
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001457 au OptionSet diff :let &l:cul = v:option_new
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001458
1459 call setline(1, ['buffer 1', 'line2', 'line3', 'line4'])
1460 call assert_equal(0, &l:cul)
1461 diffthis
1462 call assert_equal(1, &l:cul)
1463
1464 vnew
1465 call setline(1, ['buffer 2', 'line 2', 'line 3', 'line4'])
1466 call assert_equal(0, &l:cul)
1467 diffthis
1468 call assert_equal(1, &l:cul)
1469
1470 diffoff
1471 call assert_equal(0, &l:cul)
1472 call assert_equal(1, getwinvar(2, '&l:cul'))
1473 bw!
1474
1475 call assert_equal(1, &l:cul)
1476 diffoff!
1477 call assert_equal(0, &l:cul)
1478 call assert_equal(0, getwinvar(1, '&l:cul'))
1479 bw!
1480
1481 " Cleanup
1482 au! OptionSet
1483 call test_override('starting', 0)
1484endfunc
1485
1486func Test_OptionSet_diffmode_close()
1487 call test_override('starting', 1)
1488 " 19: Try to close the current window when entering diff mode
1489 " should not segfault
1490 new
1491 au OptionSet diff close
1492
1493 call setline(1, ['buffer 1', 'line2', 'line3', 'line4'])
Bram Moolenaare2e40752020-09-04 21:18:46 +02001494 call assert_fails(':diffthis', 'E788:')
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001495 call assert_equal(1, &diff)
1496 vnew
1497 call setline(1, ['buffer 2', 'line 2', 'line 3', 'line4'])
Bram Moolenaare2e40752020-09-04 21:18:46 +02001498 call assert_fails(':diffthis', 'E788:')
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001499 call assert_equal(1, &diff)
Bram Moolenaara9aa86f2019-11-10 21:25:45 +01001500 set diffopt-=closeoff
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001501 bw!
Bram Moolenaare2e40752020-09-04 21:18:46 +02001502 call assert_fails(':diffoff!', 'E788:')
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001503 bw!
1504
1505 " Cleanup
1506 au! OptionSet
1507 call test_override('starting', 0)
1508 "delfunc! AutoCommandOptionSet
1509endfunc
Bram Moolenaar4a137b42017-08-04 22:37:11 +02001510
1511" Test for Bufleave autocommand that deletes the buffer we are about to edit.
1512func Test_BufleaveWithDelete()
Bram Moolenaare7cda972022-08-29 11:02:59 +01001513 new | edit XbufLeave1
Bram Moolenaar4a137b42017-08-04 22:37:11 +02001514
1515 augroup test_bufleavewithdelete
1516 autocmd!
Bram Moolenaare7cda972022-08-29 11:02:59 +01001517 autocmd BufLeave XbufLeave1 bwipe XbufLeave2
Bram Moolenaar4a137b42017-08-04 22:37:11 +02001518 augroup END
1519
Bram Moolenaare7cda972022-08-29 11:02:59 +01001520 call assert_fails('edit XbufLeave2', 'E143:')
1521 call assert_equal('XbufLeave1', bufname('%'))
Bram Moolenaar4a137b42017-08-04 22:37:11 +02001522
Bram Moolenaare7cda972022-08-29 11:02:59 +01001523 autocmd! test_bufleavewithdelete BufLeave XbufLeave1
Bram Moolenaar4a137b42017-08-04 22:37:11 +02001524 augroup! test_bufleavewithdelete
1525
1526 new
Bram Moolenaare7cda972022-08-29 11:02:59 +01001527 bwipe! XbufLeave1
Bram Moolenaar4a137b42017-08-04 22:37:11 +02001528endfunc
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +02001529
1530" Test for autocommand that changes the buffer list, when doing ":ball".
1531func Test_Acmd_BufAll()
1532 enew!
1533 %bwipe!
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01001534 call writefile(['Test file Xxx1'], 'Xxx1', 'D')
1535 call writefile(['Test file Xxx2'], 'Xxx2', 'D')
1536 call writefile(['Test file Xxx3'], 'Xxx3', 'D')
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +02001537
1538 " Add three files to the buffer list
1539 split Xxx1
1540 close
1541 split Xxx2
1542 close
1543 split Xxx3
1544 close
1545
1546 " Wipe the buffer when the buffer is opened
1547 au BufReadPost Xxx2 bwipe
1548
1549 call append(0, 'Test file Xxx4')
1550 ball
1551
1552 call assert_equal(2, winnr('$'))
1553 call assert_equal('Xxx1', bufname(winbufnr(winnr('$'))))
1554 wincmd t
1555
1556 au! BufReadPost
1557 %bwipe!
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +02001558 enew! | only
1559endfunc
1560
1561" Test for autocommand that changes current buffer on BufEnter event.
1562" Check if modelines are interpreted for the correct buffer.
1563func Test_Acmd_BufEnter()
1564 %bwipe!
1565 call writefile(['start of test file Xxx1',
1566 \ "\<Tab>this is a test",
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01001567 \ 'end of test file Xxx1'], 'Xxx1', 'D')
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +02001568 call writefile(['start of test file Xxx2',
1569 \ 'vim: set noai :',
1570 \ "\<Tab>this is a test",
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01001571 \ 'end of test file Xxx2'], 'Xxx2', 'D')
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +02001572
1573 au BufEnter Xxx2 brew
1574 set ai modeline modelines=3
1575 edit Xxx1
1576 " edit Xxx2, autocmd will do :brew
1577 edit Xxx2
1578 exe "normal G?this is a\<CR>"
1579 " Append text with autoindent to this file
1580 normal othis should be auto-indented
1581 call assert_equal("\<Tab>this should be auto-indented", getline('.'))
1582 call assert_equal(3, line('.'))
1583 " Remove autocmd and edit Xxx2 again
1584 au! BufEnter Xxx2
1585 buf! Xxx2
1586 exe "normal G?this is a\<CR>"
1587 " append text without autoindent to Xxx
1588 normal othis should be in column 1
1589 call assert_equal("this should be in column 1", getline('.'))
1590 call assert_equal(4, line('.'))
1591
1592 %bwipe!
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +02001593 set ai&vim modeline&vim modelines&vim
1594endfunc
1595
1596" Test for issue #57
1597" do not move cursor on <c-o> when autoindent is set
1598func Test_ai_CTRL_O()
1599 enew!
1600 set ai
1601 let save_fo = &fo
1602 set fo+=r
1603 exe "normal o# abcdef\<Esc>2hi\<CR>\<C-O>d0\<Esc>"
1604 exe "normal o# abcdef\<Esc>2hi\<C-O>d0\<Esc>"
1605 call assert_equal(['# abc', 'def', 'def'], getline(2, 4))
1606
1607 set ai&vim
1608 let &fo = save_fo
1609 enew!
1610endfunc
1611
1612" Test for autocommand that deletes the current buffer on BufLeave event.
1613" Also test deleting the last buffer, should give a new, empty buffer.
1614func Test_BufLeave_Wipe()
1615 %bwipe!
1616 let content = ['start of test file Xxx',
1617 \ 'this is a test',
1618 \ 'end of test file Xxx']
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01001619 call writefile(content, 'Xxx1', 'D')
1620 call writefile(content, 'Xxx2', 'D')
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +02001621
1622 au BufLeave Xxx2 bwipe
1623 edit Xxx1
1624 split Xxx2
1625 " delete buffer Xxx2, we should be back to Xxx1
1626 bwipe
1627 call assert_equal('Xxx1', bufname('%'))
1628 call assert_equal(1, winnr('$'))
1629
1630 " Create an alternate buffer
1631 %write! test.out
1632 call assert_equal('test.out', bufname('#'))
1633 " delete alternate buffer
1634 bwipe test.out
1635 call assert_equal('Xxx1', bufname('%'))
1636 call assert_equal('', bufname('#'))
1637
1638 au BufLeave Xxx1 bwipe
1639 " delete current buffer, get an empty one
1640 bwipe!
1641 call assert_equal(1, line('$'))
1642 call assert_equal('', bufname('%'))
Bram Moolenaarb2c87502017-10-14 21:15:58 +02001643 let g:bufinfo = getbufinfo()
1644 call assert_equal(1, len(g:bufinfo))
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +02001645
Bram Moolenaar53f0c962017-10-22 14:23:59 +02001646 call delete('test.out')
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +02001647 %bwipe
1648 au! BufLeave
Bram Moolenaarb2c87502017-10-14 21:15:58 +02001649
1650 " check that bufinfo doesn't contain a pointer to freed memory
1651 call test_garbagecollect_now()
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +02001652endfunc
Bram Moolenaar87ffb5c2017-10-19 12:37:42 +02001653
1654func Test_QuitPre()
1655 edit Xfoo
1656 let winid = win_getid(winnr())
1657 split Xbar
1658 au! QuitPre * let g:afile = expand('<afile>')
1659 " Close the other window, <afile> should be correct.
1660 exe win_id2win(winid) . 'q'
1661 call assert_equal('Xfoo', g:afile)
LemonBoy66e13ae2022-04-21 22:52:11 +01001662
Bram Moolenaar87ffb5c2017-10-19 12:37:42 +02001663 unlet g:afile
1664 bwipe Xfoo
1665 bwipe Xbar
1666endfunc
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02001667
1668func Test_Cmdline()
Bram Moolenaar153b7042018-01-31 15:48:32 +01001669 au! CmdlineChanged : let g:text = getcmdline()
1670 let g:text = 0
1671 call feedkeys(":echom 'hello'\<CR>", 'xt')
1672 call assert_equal("echom 'hello'", g:text)
1673 au! CmdlineChanged
1674
1675 au! CmdlineChanged : let g:entered = expand('<afile>')
1676 let g:entered = 0
1677 call feedkeys(":echom 'hello'\<CR>", 'xt')
1678 call assert_equal(':', g:entered)
1679 au! CmdlineChanged
1680
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02001681 au! CmdlineEnter : let g:entered = expand('<afile>')
1682 au! CmdlineLeave : let g:left = expand('<afile>')
1683 let g:entered = 0
1684 let g:left = 0
1685 call feedkeys(":echo 'hello'\<CR>", 'xt')
1686 call assert_equal(':', g:entered)
1687 call assert_equal(':', g:left)
1688 au! CmdlineEnter
1689 au! CmdlineLeave
1690
Bram Moolenaara4baf5b2018-04-22 13:27:44 +02001691 let save_shellslash = &shellslash
1692 set noshellslash
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02001693 au! CmdlineEnter / let g:entered = expand('<afile>')
1694 au! CmdlineLeave / let g:left = expand('<afile>')
1695 let g:entered = 0
1696 let g:left = 0
Bram Moolenaar53f0c962017-10-22 14:23:59 +02001697 new
1698 call setline(1, 'hello')
1699 call feedkeys("/hello\<CR>", 'xt')
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02001700 call assert_equal('/', g:entered)
1701 call assert_equal('/', g:left)
Bram Moolenaar53f0c962017-10-22 14:23:59 +02001702 bwipe!
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02001703 au! CmdlineEnter
1704 au! CmdlineLeave
Bram Moolenaara4baf5b2018-04-22 13:27:44 +02001705 let &shellslash = save_shellslash
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02001706endfunc
Bram Moolenaar53f0c962017-10-22 14:23:59 +02001707
1708" Test for BufWritePre autocommand that deletes or unloads the buffer.
1709func Test_BufWritePre()
1710 %bwipe
1711 au BufWritePre Xxx1 bunload
1712 au BufWritePre Xxx2 bwipe
1713
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01001714 call writefile(['start of Xxx1', 'test', 'end of Xxx1'], 'Xxx1', 'D')
1715 call writefile(['start of Xxx2', 'test', 'end of Xxx2'], 'Xxx2', 'D')
Bram Moolenaar53f0c962017-10-22 14:23:59 +02001716
1717 edit Xtest
1718 e! Xxx2
1719 bdel Xtest
1720 e Xxx1
1721 " write it, will unload it and give an error msg
Bram Moolenaare2e40752020-09-04 21:18:46 +02001722 call assert_fails('w', 'E203:')
Bram Moolenaar53f0c962017-10-22 14:23:59 +02001723 call assert_equal('Xxx2', bufname('%'))
1724 edit Xtest
1725 e! Xxx2
1726 bwipe Xtest
1727 " write it, will delete the buffer and give an error msg
Bram Moolenaare2e40752020-09-04 21:18:46 +02001728 call assert_fails('w', 'E203:')
Bram Moolenaar53f0c962017-10-22 14:23:59 +02001729 call assert_equal('Xxx1', bufname('%'))
1730 au! BufWritePre
Bram Moolenaar53f0c962017-10-22 14:23:59 +02001731endfunc
1732
1733" Test for BufUnload autocommand that unloads all the other buffers
1734func Test_bufunload_all()
Bram Moolenaarf08b0eb2021-10-16 13:00:14 +01001735 let g:test_is_flaky = 1
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01001736 call writefile(['Test file Xxx1'], 'Xxx1', 'D')"
1737 call writefile(['Test file Xxx2'], 'Xxx2', 'D')"
Bram Moolenaar53f0c962017-10-22 14:23:59 +02001738
Bram Moolenaarc79745a2019-05-20 22:12:34 +02001739 let content =<< trim [CODE]
1740 func UnloadAllBufs()
1741 let i = 1
1742 while i <= bufnr('$')
1743 if i != bufnr('%') && bufloaded(i)
1744 exe i . 'bunload'
1745 endif
1746 let i += 1
1747 endwhile
1748 endfunc
1749 au BufUnload * call UnloadAllBufs()
1750 au VimLeave * call writefile(['Test Finished'], 'Xout')
1751 edit Xxx1
1752 split Xxx2
1753 q
1754 [CODE]
1755
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01001756 call writefile(content, 'Xbunloadtest', 'D')
Bram Moolenaar53f0c962017-10-22 14:23:59 +02001757
1758 call delete('Xout')
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01001759 call system(GetVimCommandClean() .. ' -N --not-a-term -S Xbunloadtest')
Bram Moolenaar53f0c962017-10-22 14:23:59 +02001760 call assert_true(filereadable('Xout'))
1761
Bram Moolenaar53f0c962017-10-22 14:23:59 +02001762 call delete('Xout')
1763endfunc
1764
1765" Some tests for buffer-local autocommands
1766func Test_buflocal_autocmd()
1767 let g:bname = ''
1768 edit xx
1769 au BufLeave <buffer> let g:bname = expand("%")
1770 " here, autocommand for xx should trigger.
1771 " but autocommand shall not apply to buffer named <buffer>.
1772 edit somefile
1773 call assert_equal('xx', g:bname)
1774 let g:bname = ''
1775 " here, autocommand shall be auto-deleted
1776 bwipe xx
1777 " autocmd should not trigger
1778 edit xx
1779 call assert_equal('', g:bname)
1780 " autocmd should not trigger
1781 edit somefile
1782 call assert_equal('', g:bname)
1783 enew
1784 unlet g:bname
1785endfunc
Bram Moolenaar430dc5d2017-11-02 21:04:47 +01001786
1787" Test for "*Cmd" autocommands
1788func Test_Cmd_Autocmds()
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01001789 call writefile(['start of Xxx', "\tabc2", 'end of Xxx'], 'Xxx', 'D')
Bram Moolenaar430dc5d2017-11-02 21:04:47 +01001790
1791 enew!
1792 au BufReadCmd XtestA 0r Xxx|$del
1793 edit XtestA " will read text of Xxd instead
1794 call assert_equal('start of Xxx', getline(1))
1795
1796 au BufWriteCmd XtestA call append(line("$"), "write")
1797 write " will append a line to the file
1798 call assert_equal('write', getline('$'))
Bram Moolenaare2e40752020-09-04 21:18:46 +02001799 call assert_fails('read XtestA', 'E484:') " should not read anything
Bram Moolenaar430dc5d2017-11-02 21:04:47 +01001800 call assert_equal('write', getline(4))
1801
1802 " now we have:
1803 " 1 start of Xxx
1804 " 2 abc2
1805 " 3 end of Xxx
1806 " 4 write
1807
1808 au FileReadCmd XtestB '[r Xxx
1809 2r XtestB " will read Xxx below line 2 instead
1810 call assert_equal('start of Xxx', getline(3))
1811
1812 " now we have:
1813 " 1 start of Xxx
1814 " 2 abc2
1815 " 3 start of Xxx
1816 " 4 abc2
1817 " 5 end of Xxx
1818 " 6 end of Xxx
1819 " 7 write
1820
1821 au FileWriteCmd XtestC '[,']copy $
1822 normal 4GA1
1823 4,5w XtestC " will copy lines 4 and 5 to the end
1824 call assert_equal("\tabc21", getline(8))
Bram Moolenaare2e40752020-09-04 21:18:46 +02001825 call assert_fails('r XtestC', 'E484:') " should not read anything
Bram Moolenaar430dc5d2017-11-02 21:04:47 +01001826 call assert_equal("end of Xxx", getline(9))
1827
1828 " now we have:
1829 " 1 start of Xxx
1830 " 2 abc2
1831 " 3 start of Xxx
1832 " 4 abc21
1833 " 5 end of Xxx
1834 " 6 end of Xxx
1835 " 7 write
1836 " 8 abc21
1837 " 9 end of Xxx
1838
1839 let g:lines = []
1840 au FileAppendCmd XtestD call extend(g:lines, getline(line("'["), line("']")))
1841 w >>XtestD " will add lines to 'lines'
1842 call assert_equal(9, len(g:lines))
Bram Moolenaare2e40752020-09-04 21:18:46 +02001843 call assert_fails('$r XtestD', 'E484:') " should not read anything
Bram Moolenaar430dc5d2017-11-02 21:04:47 +01001844 call assert_equal(9, line('$'))
1845 call assert_equal('end of Xxx', getline('$'))
1846
1847 au BufReadCmd XtestE 0r Xxx|$del
1848 sp XtestE " split window with test.out
1849 call assert_equal('end of Xxx', getline(3))
1850
1851 let g:lines = []
1852 exe "normal 2Goasdf\<Esc>\<C-W>\<C-W>"
1853 au BufWriteCmd XtestE call extend(g:lines, getline(0, '$'))
1854 wall " will write other window to 'lines'
1855 call assert_equal(4, len(g:lines), g:lines)
1856 call assert_equal('asdf', g:lines[2])
1857
1858 au! BufReadCmd
1859 au! BufWriteCmd
1860 au! FileReadCmd
1861 au! FileWriteCmd
1862 au! FileAppendCmd
1863 %bwipe!
Bram Moolenaar430dc5d2017-11-02 21:04:47 +01001864 enew!
1865endfunc
Bram Moolenaaraace2152017-11-05 16:23:10 +01001866
Bram Moolenaar0fff4412020-03-29 16:06:29 +02001867func s:ReadFile()
1868 setl noswapfile nomodified
1869 let filename = resolve(expand("<afile>:p"))
1870 execute 'read' fnameescape(filename)
1871 1d_
1872 exe 'file' fnameescape(filename)
1873 setl buftype=acwrite
1874endfunc
1875
1876func s:WriteFile()
1877 let filename = resolve(expand("<afile>:p"))
1878 setl buftype=
1879 noautocmd execute 'write' fnameescape(filename)
1880 setl buftype=acwrite
1881 setl nomodified
1882endfunc
1883
1884func Test_BufReadCmd()
1885 autocmd BufReadCmd *.test call s:ReadFile()
1886 autocmd BufWriteCmd *.test call s:WriteFile()
1887
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01001888 call writefile(['one', 'two', 'three'], 'Xcmd.test', 'D')
Bram Moolenaar0fff4412020-03-29 16:06:29 +02001889 edit Xcmd.test
1890 call assert_match('Xcmd.test" line 1 of 3', execute('file'))
1891 normal! Gofour
1892 write
1893 call assert_equal(['one', 'two', 'three', 'four'], readfile('Xcmd.test'))
1894
1895 bwipe!
Bram Moolenaar0fff4412020-03-29 16:06:29 +02001896 au! BufReadCmd
1897 au! BufWriteCmd
1898endfunc
1899
zeertzjq9c8f9462022-08-30 18:17:15 +01001900func Test_BufWriteCmd()
1901 autocmd BufWriteCmd Xbufwritecmd let g:written = 1
1902 new
1903 file Xbufwritecmd
1904 set buftype=acwrite
1905 call mkdir('Xbufwritecmd')
1906 write
1907 " BufWriteCmd should be triggered even if a directory has the same name
1908 call assert_equal(1, g:written)
1909 call delete('Xbufwritecmd', 'd')
1910 unlet g:written
1911 au! BufWriteCmd
1912 bwipe!
1913endfunc
1914
Bram Moolenaaraace2152017-11-05 16:23:10 +01001915func SetChangeMarks(start, end)
Bram Moolenaar97c69432021-01-15 16:45:21 +01001916 exe a:start .. 'mark ['
1917 exe a:end .. 'mark ]'
Bram Moolenaaraace2152017-11-05 16:23:10 +01001918endfunc
1919
1920" Verify the effects of autocmds on '[ and ']
1921func Test_change_mark_in_autocmds()
1922 edit! Xtest
Bram Moolenaar97c69432021-01-15 16:45:21 +01001923 call feedkeys("ia\<CR>b\<CR>c\<CR>d\<C-g>u\<Esc>", 'xtn')
Bram Moolenaaraace2152017-11-05 16:23:10 +01001924
1925 call SetChangeMarks(2, 3)
1926 write
1927 call assert_equal([1, 4], [line("'["), line("']")])
1928
1929 call SetChangeMarks(2, 3)
1930 au BufWritePre * call assert_equal([1, 4], [line("'["), line("']")])
1931 write
1932 au! BufWritePre
1933
Bram Moolenaar14ddd222020-08-05 12:02:40 +02001934 if has('unix')
Bram Moolenaaraace2152017-11-05 16:23:10 +01001935 write XtestFilter
1936 write >> XtestFilter
1937
1938 call SetChangeMarks(2, 3)
1939 " Marks are set to the entire range of the write
1940 au FilterWritePre * call assert_equal([1, 4], [line("'["), line("']")])
1941 " '[ is adjusted to just before the line that will receive the filtered
1942 " data
1943 au FilterReadPre * call assert_equal([4, 4], [line("'["), line("']")])
1944 " The filtered data is read into the buffer, and the source lines are
1945 " still present, so the range is after the source lines
1946 au FilterReadPost * call assert_equal([5, 12], [line("'["), line("']")])
1947 %!cat XtestFilter
1948 " After the filtered data is read, the original lines are deleted
1949 call assert_equal([1, 8], [line("'["), line("']")])
1950 au! FilterWritePre,FilterReadPre,FilterReadPost
1951 undo
1952
1953 call SetChangeMarks(1, 4)
1954 au FilterWritePre * call assert_equal([2, 3], [line("'["), line("']")])
1955 au FilterReadPre * call assert_equal([3, 3], [line("'["), line("']")])
1956 au FilterReadPost * call assert_equal([4, 11], [line("'["), line("']")])
1957 2,3!cat XtestFilter
1958 call assert_equal([2, 9], [line("'["), line("']")])
1959 au! FilterWritePre,FilterReadPre,FilterReadPost
1960 undo
1961
1962 call delete('XtestFilter')
1963 endif
1964
1965 call SetChangeMarks(1, 4)
1966 au FileWritePre * call assert_equal([2, 3], [line("'["), line("']")])
1967 2,3write Xtest2
1968 au! FileWritePre
1969
1970 call SetChangeMarks(2, 3)
1971 au FileAppendPre * call assert_equal([1, 4], [line("'["), line("']")])
1972 write >> Xtest2
1973 au! FileAppendPre
1974
1975 call SetChangeMarks(1, 4)
1976 au FileAppendPre * call assert_equal([2, 3], [line("'["), line("']")])
1977 2,3write >> Xtest2
1978 au! FileAppendPre
1979
1980 call SetChangeMarks(1, 1)
1981 au FileReadPre * call assert_equal([3, 1], [line("'["), line("']")])
1982 au FileReadPost * call assert_equal([4, 11], [line("'["), line("']")])
1983 3read Xtest2
1984 au! FileReadPre,FileReadPost
1985 undo
1986
1987 call SetChangeMarks(4, 4)
1988 " When the line is 0, it's adjusted to 1
1989 au FileReadPre * call assert_equal([1, 4], [line("'["), line("']")])
1990 au FileReadPost * call assert_equal([1, 8], [line("'["), line("']")])
1991 0read Xtest2
1992 au! FileReadPre,FileReadPost
1993 undo
1994
1995 call SetChangeMarks(4, 4)
1996 " When the line is 0, it's adjusted to 1
1997 au FileReadPre * call assert_equal([1, 4], [line("'["), line("']")])
1998 au FileReadPost * call assert_equal([2, 9], [line("'["), line("']")])
1999 1read Xtest2
2000 au! FileReadPre,FileReadPost
2001 undo
2002
2003 bwipe!
2004 call delete('Xtest')
2005 call delete('Xtest2')
2006endfunc
2007
2008func Test_Filter_noshelltemp()
Bram Moolenaaraeb313f2020-11-27 19:13:28 +01002009 CheckExecutable cat
Bram Moolenaaraace2152017-11-05 16:23:10 +01002010
2011 enew!
2012 call setline(1, ['a', 'b', 'c', 'd'])
2013
2014 let shelltemp = &shelltemp
2015 set shelltemp
2016
2017 let g:filter_au = 0
2018 au FilterWritePre * let g:filter_au += 1
2019 au FilterReadPre * let g:filter_au += 1
2020 au FilterReadPost * let g:filter_au += 1
2021 %!cat
2022 call assert_equal(3, g:filter_au)
2023
2024 if has('filterpipe')
2025 set noshelltemp
2026
2027 let g:filter_au = 0
2028 au FilterWritePre * let g:filter_au += 1
2029 au FilterReadPre * let g:filter_au += 1
2030 au FilterReadPost * let g:filter_au += 1
2031 %!cat
2032 call assert_equal(0, g:filter_au)
2033 endif
2034
2035 au! FilterWritePre,FilterReadPre,FilterReadPost
2036 let &shelltemp = shelltemp
2037 bwipe!
2038endfunc
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01002039
2040func Test_TextYankPost()
2041 enew!
2042 call setline(1, ['foo'])
2043
2044 let g:event = []
2045 au TextYankPost * let g:event = copy(v:event)
2046
2047 call assert_equal({}, v:event)
2048 call assert_fails('let v:event = {}', 'E46:')
2049 call assert_fails('let v:event.mykey = 0', 'E742:')
2050
2051 norm "ayiw
2052 call assert_equal(
Bram Moolenaara016eeb2022-04-09 11:37:38 +01002053 \ #{regcontents: ['foo'], regname: 'a', operator: 'y',
2054 \ regtype: 'v', visual: v:false, inclusive: v:true},
2055 \ g:event)
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01002056 norm y_
2057 call assert_equal(
Bram Moolenaara016eeb2022-04-09 11:37:38 +01002058 \ #{regcontents: ['foo'], regname: '', operator: 'y', regtype: 'V',
2059 \ visual: v:false, inclusive: v:false},
2060 \ g:event)
Bram Moolenaar37d16732020-06-12 22:09:01 +02002061 norm Vy
2062 call assert_equal(
Bram Moolenaara016eeb2022-04-09 11:37:38 +01002063 \ #{regcontents: ['foo'], regname: '', operator: 'y', regtype: 'V',
2064 \ visual: v:true, inclusive: v:true},
2065 \ g:event)
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01002066 call feedkeys("\<C-V>y", 'x')
2067 call assert_equal(
Bram Moolenaara016eeb2022-04-09 11:37:38 +01002068 \ #{regcontents: ['f'], regname: '', operator: 'y', regtype: "\x161",
2069 \ visual: v:true, inclusive: v:true},
2070 \ g:event)
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01002071 norm "xciwbar
2072 call assert_equal(
Bram Moolenaara016eeb2022-04-09 11:37:38 +01002073 \ #{regcontents: ['foo'], regname: 'x', operator: 'c', regtype: 'v',
2074 \ visual: v:false, inclusive: v:true},
2075 \ g:event)
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01002076 norm "bdiw
2077 call assert_equal(
Bram Moolenaara016eeb2022-04-09 11:37:38 +01002078 \ #{regcontents: ['bar'], regname: 'b', operator: 'd', regtype: 'v',
2079 \ visual: v:false, inclusive: v:true},
2080 \ g:event)
2081
2082 call setline(1, 'foobar')
2083 " exclusive motion
2084 norm $"ay0
2085 call assert_equal(
2086 \ #{regcontents: ['fooba'], regname: 'a', operator: 'y', regtype: 'v',
2087 \ visual: v:false, inclusive: v:false},
2088 \ g:event)
2089 " inclusive motion
2090 norm 0"ay$
2091 call assert_equal(
2092 \ #{regcontents: ['foobar'], regname: 'a', operator: 'y', regtype: 'v',
2093 \ visual: v:false, inclusive: v:true},
2094 \ g:event)
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01002095
2096 call assert_equal({}, v:event)
2097
Bram Moolenaarfccbf062020-11-26 20:34:00 +01002098 if has('clipboard_working') && !has('gui_running')
2099 " Test that when the visual selection is automatically copied to clipboard
2100 " register a TextYankPost is emitted
2101 call setline(1, ['foobar'])
2102
2103 let @* = ''
2104 set clipboard=autoselect
2105 exe "norm! ggviw\<Esc>"
2106 call assert_equal(
Bram Moolenaara016eeb2022-04-09 11:37:38 +01002107 \ #{regcontents: ['foobar'], regname: '*', operator: 'y',
2108 \ regtype: 'v', visual: v:true, inclusive: v:false},
2109 \ g:event)
Bram Moolenaarfccbf062020-11-26 20:34:00 +01002110
2111 let @+ = ''
2112 set clipboard=autoselectplus
2113 exe "norm! ggviw\<Esc>"
2114 call assert_equal(
Bram Moolenaara016eeb2022-04-09 11:37:38 +01002115 \ #{regcontents: ['foobar'], regname: '+', operator: 'y',
2116 \ regtype: 'v', visual: v:true, inclusive: v:false},
2117 \ g:event)
Bram Moolenaarfccbf062020-11-26 20:34:00 +01002118
2119 set clipboard&vim
2120 endif
2121
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01002122 au! TextYankPost
2123 unlet g:event
2124 bwipe!
2125endfunc
Bram Moolenaar9bca8052017-12-18 12:37:55 +01002126
Bram Moolenaar9a046fd2021-01-28 13:47:59 +01002127func Test_autocommand_all_events()
2128 call assert_fails('au * * bwipe', 'E1155:')
2129 call assert_fails('au * x bwipe', 'E1155:')
Bram Moolenaarb6db1462021-12-24 19:24:47 +00002130 call assert_fails('au! * x bwipe', 'E1155:')
Bram Moolenaar4fb921e2017-12-18 15:33:00 +01002131endfunc
Bram Moolenaarb7407d32018-02-03 17:36:27 +01002132
Bram Moolenaarf6246f52022-02-11 16:30:12 +00002133func Test_autocmd_user()
2134 au User MyEvent let s:res = [expand("<afile>"), expand("<amatch>")]
2135 doautocmd User MyEvent
2136 call assert_equal(['MyEvent', 'MyEvent'], s:res)
2137 au! User
2138 unlet s:res
2139endfunc
2140
Bram Moolenaarb7407d32018-02-03 17:36:27 +01002141function s:Before_test_dirchanged()
2142 augroup test_dirchanged
2143 autocmd!
2144 augroup END
2145 let s:li = []
2146 let s:dir_this = getcwd()
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02002147 let s:dir_foo = s:dir_this . '/Xfoo'
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01002148 call mkdir(s:dir_foo)
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02002149 let s:dir_bar = s:dir_this . '/Xbar'
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01002150 call mkdir(s:dir_bar)
Bram Moolenaarb7407d32018-02-03 17:36:27 +01002151endfunc
2152
2153function s:After_test_dirchanged()
Bram Moolenaar3503d7c2019-11-09 20:10:17 +01002154 call chdir(s:dir_this)
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01002155 call delete(s:dir_foo, 'd')
2156 call delete(s:dir_bar, 'd')
Bram Moolenaarb7407d32018-02-03 17:36:27 +01002157 augroup test_dirchanged
2158 autocmd!
2159 augroup END
2160endfunc
2161
2162function Test_dirchanged_global()
2163 call s:Before_test_dirchanged()
Bram Moolenaarf6246f52022-02-11 16:30:12 +00002164 autocmd test_dirchanged DirChangedPre global call add(s:li, expand("<amatch>") .. " pre cd " .. v:event.directory)
Bram Moolenaarb7407d32018-02-03 17:36:27 +01002165 autocmd test_dirchanged DirChanged global call add(s:li, "cd:")
2166 autocmd test_dirchanged DirChanged global call add(s:li, expand("<afile>"))
Bram Moolenaar3503d7c2019-11-09 20:10:17 +01002167 call chdir(s:dir_foo)
Bram Moolenaarf6246f52022-02-11 16:30:12 +00002168 let expected = ["global pre cd " .. s:dir_foo, "cd:", s:dir_foo]
Bram Moolenaar28e8f732022-02-09 12:58:20 +00002169 call assert_equal(expected, s:li)
Bram Moolenaar3503d7c2019-11-09 20:10:17 +01002170 call chdir(s:dir_foo)
Bram Moolenaar28e8f732022-02-09 12:58:20 +00002171 call assert_equal(expected, s:li)
Bram Moolenaar3503d7c2019-11-09 20:10:17 +01002172 exe 'lcd ' .. fnameescape(s:dir_bar)
Bram Moolenaar28e8f732022-02-09 12:58:20 +00002173 call assert_equal(expected, s:li)
Bram Moolenaard8c9d322022-06-12 11:49:16 +01002174
2175 exe 'cd ' .. s:dir_foo
2176 exe 'cd ' .. s:dir_bar
2177 autocmd! test_dirchanged DirChanged global let g:result = expand("<afile>")
2178 cd -
Bram Moolenaardb77c492022-06-12 23:26:50 +01002179 call assert_equal(s:dir_foo, substitute(g:result, '\\', '/', 'g'))
Bram Moolenaard8c9d322022-06-12 11:49:16 +01002180
Bram Moolenaarb7407d32018-02-03 17:36:27 +01002181 call s:After_test_dirchanged()
2182endfunc
2183
2184function Test_dirchanged_local()
2185 call s:Before_test_dirchanged()
2186 autocmd test_dirchanged DirChanged window call add(s:li, "lcd:")
2187 autocmd test_dirchanged DirChanged window call add(s:li, expand("<afile>"))
Bram Moolenaar3503d7c2019-11-09 20:10:17 +01002188 call chdir(s:dir_foo)
Bram Moolenaarb7407d32018-02-03 17:36:27 +01002189 call assert_equal([], s:li)
Bram Moolenaar3503d7c2019-11-09 20:10:17 +01002190 exe 'lcd ' .. fnameescape(s:dir_bar)
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01002191 call assert_equal(["lcd:", s:dir_bar], s:li)
Bram Moolenaar3503d7c2019-11-09 20:10:17 +01002192 exe 'lcd ' .. fnameescape(s:dir_bar)
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01002193 call assert_equal(["lcd:", s:dir_bar], s:li)
Bram Moolenaarb7407d32018-02-03 17:36:27 +01002194 call s:After_test_dirchanged()
2195endfunc
2196
2197function Test_dirchanged_auto()
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +02002198 CheckOption autochdir
Bram Moolenaarb7407d32018-02-03 17:36:27 +01002199 call s:Before_test_dirchanged()
2200 call test_autochdir()
Bram Moolenaar28e8f732022-02-09 12:58:20 +00002201 autocmd test_dirchanged DirChangedPre auto call add(s:li, "pre cd " .. v:event.directory)
Bram Moolenaarb7407d32018-02-03 17:36:27 +01002202 autocmd test_dirchanged DirChanged auto call add(s:li, "auto:")
2203 autocmd test_dirchanged DirChanged auto call add(s:li, expand("<afile>"))
2204 set acd
Bram Moolenaar3503d7c2019-11-09 20:10:17 +01002205 cd ..
Bram Moolenaarb7407d32018-02-03 17:36:27 +01002206 call assert_equal([], s:li)
Bram Moolenaar61abe7d2022-08-30 21:46:08 +01002207 exe 'edit ' . s:dir_foo . '/Xautofile'
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01002208 call assert_equal(s:dir_foo, getcwd())
Bram Moolenaar28e8f732022-02-09 12:58:20 +00002209 let expected = ["pre cd " .. s:dir_foo, "auto:", s:dir_foo]
2210 call assert_equal(expected, s:li)
Bram Moolenaarb7407d32018-02-03 17:36:27 +01002211 set noacd
2212 bwipe!
2213 call s:After_test_dirchanged()
2214endfunc
Bram Moolenaar5a093432018-02-10 18:15:19 +01002215
2216" Test TextChangedI and TextChangedP
2217func Test_ChangedP()
2218 new
2219 call setline(1, ['foo', 'bar', 'foobar'])
2220 call test_override("char_avail", 1)
2221 set complete=. completeopt=menuone
2222
2223 func! TextChangedAutocmd(char)
2224 let g:autocmd .= a:char
2225 endfunc
2226
Christian Brabandtdb3b4462021-10-16 11:58:55 +01002227 " TextChanged will not be triggered, only check that it isn't.
Bram Moolenaar5a093432018-02-10 18:15:19 +01002228 au! TextChanged <buffer> :call TextChangedAutocmd('N')
2229 au! TextChangedI <buffer> :call TextChangedAutocmd('I')
2230 au! TextChangedP <buffer> :call TextChangedAutocmd('P')
2231
2232 call cursor(3, 1)
2233 let g:autocmd = ''
2234 call feedkeys("o\<esc>", 'tnix')
2235 call assert_equal('I', g:autocmd)
2236
2237 let g:autocmd = ''
2238 call feedkeys("Sf", 'tnix')
2239 call assert_equal('II', g:autocmd)
2240
2241 let g:autocmd = ''
2242 call feedkeys("Sf\<C-N>", 'tnix')
2243 call assert_equal('IIP', g:autocmd)
2244
2245 let g:autocmd = ''
2246 call feedkeys("Sf\<C-N>\<C-N>", 'tnix')
2247 call assert_equal('IIPP', g:autocmd)
2248
2249 let g:autocmd = ''
2250 call feedkeys("Sf\<C-N>\<C-N>\<C-N>", 'tnix')
2251 call assert_equal('IIPPP', g:autocmd)
2252
2253 let g:autocmd = ''
2254 call feedkeys("Sf\<C-N>\<C-N>\<C-N>\<C-N>", 'tnix')
2255 call assert_equal('IIPPPP', g:autocmd)
2256
2257 call assert_equal(['foo', 'bar', 'foobar', 'foo'], getline(1, '$'))
2258 " TODO: how should it handle completeopt=noinsert,noselect?
2259
2260 " CleanUp
2261 call test_override("char_avail", 0)
2262 au! TextChanged
2263 au! TextChangedI
2264 au! TextChangedP
2265 delfu TextChangedAutocmd
2266 unlet! g:autocmd
2267 set complete&vim completeopt&vim
2268
2269 bw!
2270endfunc
Bram Moolenaar8c64a362018-03-23 22:39:31 +01002271
Bram Moolenaar91d2e782018-08-07 19:05:01 +02002272let g:setline_handled = v:false
Bram Moolenaar1e115362019-01-09 23:01:02 +01002273func SetLineOne()
Bram Moolenaar91d2e782018-08-07 19:05:01 +02002274 if !g:setline_handled
2275 call setline(1, "(x)")
2276 let g:setline_handled = v:true
2277 endif
2278endfunc
2279
2280func Test_TextChangedI_with_setline()
2281 new
2282 call test_override('char_avail', 1)
2283 autocmd TextChangedI <buffer> call SetLineOne()
2284 call feedkeys("i(\<CR>\<Esc>", 'tx')
2285 call assert_equal('(', getline(1))
2286 call assert_equal('x)', getline(2))
2287 undo
Bram Moolenaar91d2e782018-08-07 19:05:01 +02002288 call assert_equal('', getline(1))
Bram Moolenaar9fa95062018-08-08 22:08:32 +02002289 call assert_equal('', getline(2))
Bram Moolenaar91d2e782018-08-07 19:05:01 +02002290
Bram Moolenaarca34db32022-01-20 11:17:18 +00002291 call test_override('char_avail', 0)
Bram Moolenaar91d2e782018-08-07 19:05:01 +02002292 bwipe!
2293endfunc
2294
Bram Moolenaar8c64a362018-03-23 22:39:31 +01002295func Test_Changed_FirstTime()
Bram Moolenaar8c5a2782019-08-07 23:07:07 +02002296 CheckFeature terminal
2297 CheckNotGui
Bram Moolenaar3cdcb092020-03-18 19:18:10 +01002298 " Starting a terminal to run Vim is always considered flaky.
Bram Moolenaar30d53e22020-03-18 21:10:44 +01002299 let g:test_is_flaky = 1
Bram Moolenaar8c5a2782019-08-07 23:07:07 +02002300
Bram Moolenaar8c64a362018-03-23 22:39:31 +01002301 " Prepare file for TextChanged event.
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01002302 call writefile([''], 'Xchanged.txt', 'D')
Bram Moolenaar8c64a362018-03-23 22:39:31 +01002303 let buf = term_start([GetVimProg(), '--clean', '-c', 'set noswapfile'], {'term_rows': 3})
2304 call assert_equal('running', term_getstatus(buf))
Bram Moolenaar1834d372018-03-29 17:40:46 +02002305 " Wait for the ruler (in the status line) to be shown.
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01002306 " In ConPTY, there is additional character which is drawn up to the width of
2307 " the screen.
2308 if has('conpty')
2309 call WaitForAssert({-> assert_match('\<All.*$', term_getline(buf, 3))})
2310 else
2311 call WaitForAssert({-> assert_match('\<All$', term_getline(buf, 3))})
2312 endif
Bram Moolenaar8c64a362018-03-23 22:39:31 +01002313 " It's only adding autocmd, so that no event occurs.
2314 call term_sendkeys(buf, ":au! TextChanged <buffer> call writefile(['No'], 'Xchanged.txt')\<cr>")
2315 call term_sendkeys(buf, "\<C-\\>\<C-N>:qa!\<cr>")
Bram Moolenaar50182fa2018-04-28 21:34:40 +02002316 call WaitForAssert({-> assert_equal('finished', term_getstatus(buf))})
Bram Moolenaar8c64a362018-03-23 22:39:31 +01002317 call assert_equal([''], readfile('Xchanged.txt'))
2318
2319 " clean up
Bram Moolenaar8c64a362018-03-23 22:39:31 +01002320 bwipe!
2321endfunc
Bram Moolenaar0566e892019-01-24 19:37:40 +01002322
Bram Moolenaareb93f3f2019-04-04 15:04:56 +02002323func Test_autocmd_nested()
2324 let g:did_nested = 0
2325 augroup Testing
2326 au WinNew * edit somefile
2327 au BufNew * let g:did_nested = 1
2328 augroup END
2329 split
2330 call assert_equal(0, g:did_nested)
2331 close
2332 bwipe! somefile
2333
2334 " old nested argument still works
2335 augroup Testing
2336 au!
2337 au WinNew * nested edit somefile
2338 au BufNew * let g:did_nested = 1
2339 augroup END
2340 split
2341 call assert_equal(1, g:did_nested)
2342 close
2343 bwipe! somefile
2344
2345 " New ++nested argument works
2346 augroup Testing
2347 au!
2348 au WinNew * ++nested edit somefile
2349 au BufNew * let g:did_nested = 1
2350 augroup END
2351 split
2352 call assert_equal(1, g:did_nested)
2353 close
2354 bwipe! somefile
2355
Bram Moolenaarf0775142022-03-04 20:10:38 +00002356 " nested without ++ does not work in Vim9 script
2357 call assert_fails('vim9cmd au WinNew * nested echo fails', 'E1078:')
2358
Bram Moolenaareb93f3f2019-04-04 15:04:56 +02002359 augroup Testing
2360 au!
2361 augroup END
2362
2363 call assert_fails('au WinNew * ++nested ++nested echo bad', 'E983:')
2364 call assert_fails('au WinNew * nested nested echo bad', 'E983:')
2365endfunc
2366
Bram Moolenaar5fa9f232022-07-23 09:06:48 +01002367func Test_autocmd_nested_cursor_invalid()
2368 set laststatus=0
2369 copen
2370 cclose
2371 call setline(1, ['foo', 'bar', 'baz'])
2372 3
2373 augroup nested_inv
2374 autocmd User foo ++nested copen
2375 autocmd BufAdd * let &laststatus = 2 - &laststatus
2376 augroup END
2377 doautocmd User foo
2378
2379 augroup nested_inv
2380 au!
2381 augroup END
2382 set laststatus&
Bram Moolenaarb03950f2022-07-26 13:47:13 +01002383 cclose
Bram Moolenaar5fa9f232022-07-23 09:06:48 +01002384 bwipe!
2385endfunc
2386
Bram Moolenaar3d6ee8b2022-07-27 15:23:35 +01002387func Test_autocmd_nested_keeps_cursor_pos()
2388 enew
2389 call setline(1, 'foo')
2390 autocmd User foo ++nested normal! $a
2391 autocmd InsertLeave * :
2392 doautocmd User foo
2393 call assert_equal([0, 1, 3, 0], getpos('.'))
2394
2395 bwipe!
2396endfunc
2397
Bram Moolenaarb03950f2022-07-26 13:47:13 +01002398func Test_autocmd_nested_switch_window()
2399 " run this in a separate Vim so that SafeState works
2400 CheckRunVimInTerminal
2401
2402 let lines =<< trim END
2403 vim9script
2404 ['()']->writefile('Xautofile')
2405 autocmd VimEnter * ++nested edit Xautofile | split
2406 autocmd BufReadPost * autocmd SafeState * ++once foldclosed('.')
2407 autocmd WinEnter * matchadd('ErrorMsg', 'pat')
2408 END
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01002409 call writefile(lines, 'Xautoscript', 'D')
Bram Moolenaarb03950f2022-07-26 13:47:13 +01002410 let buf = RunVimInTerminal('-S Xautoscript', {'rows': 10})
2411 call VerifyScreenDump(buf, 'Test_autocmd_nested_switch', {})
2412
2413 call StopVimInTerminal(buf)
2414 call delete('Xautofile')
Bram Moolenaarb03950f2022-07-26 13:47:13 +01002415endfunc
2416
Bram Moolenaareb93f3f2019-04-04 15:04:56 +02002417func Test_autocmd_once()
2418 " Without ++once WinNew triggers twice
2419 let g:did_split = 0
2420 augroup Testing
2421 au WinNew * let g:did_split += 1
2422 augroup END
2423 split
2424 split
2425 call assert_equal(2, g:did_split)
2426 call assert_true(exists('#WinNew'))
2427 close
2428 close
2429
2430 " With ++once WinNew triggers once
2431 let g:did_split = 0
2432 augroup Testing
2433 au!
2434 au WinNew * ++once let g:did_split += 1
2435 augroup END
2436 split
2437 split
2438 call assert_equal(1, g:did_split)
2439 call assert_false(exists('#WinNew'))
2440 close
2441 close
2442
2443 call assert_fails('au WinNew * ++once ++once echo bad', 'E983:')
2444endfunc
2445
Bram Moolenaara68e5952019-04-25 22:22:01 +02002446func Test_autocmd_bufreadpre()
2447 new
2448 let b:bufreadpre = 1
Bram Moolenaarab505b12020-03-23 19:28:44 +01002449 call append(0, range(1000))
Bram Moolenaara68e5952019-04-25 22:22:01 +02002450 w! XAutocmdBufReadPre.txt
2451 autocmd BufReadPre <buffer> :let b:bufreadpre += 1
Bram Moolenaarab505b12020-03-23 19:28:44 +01002452 norm! 500gg
Bram Moolenaara68e5952019-04-25 22:22:01 +02002453 sp
Bram Moolenaarab505b12020-03-23 19:28:44 +01002454 norm! 1000gg
Bram Moolenaara68e5952019-04-25 22:22:01 +02002455 wincmd p
2456 let g:wsv1 = winsaveview()
2457 wincmd p
2458 let g:wsv2 = winsaveview()
2459 " triggers BufReadPre, should not move the cursor in either window
2460 " The topline may change one line in a large window.
2461 edit
2462 call assert_inrange(g:wsv2.topline - 1, g:wsv2.topline + 1, winsaveview().topline)
2463 call assert_equal(g:wsv2.lnum, winsaveview().lnum)
2464 call assert_equal(2, b:bufreadpre)
2465 wincmd p
2466 call assert_equal(g:wsv1.topline, winsaveview().topline)
2467 call assert_equal(g:wsv1.lnum, winsaveview().lnum)
2468 call assert_equal(2, b:bufreadpre)
2469 " Now set the cursor position in an BufReadPre autocommand
2470 " (even though the position will be invalid, this should make Vim reset the
2471 " cursor position in the other window.
2472 wincmd p
2473 set cpo+=g
2474 " won't do anything, but try to set the cursor on an invalid lnum
2475 autocmd BufReadPre <buffer> :norm! 70gg
2476 " triggers BufReadPre, should not move the cursor in either window
2477 e
2478 call assert_equal(1, winsaveview().topline)
2479 call assert_equal(1, winsaveview().lnum)
2480 call assert_equal(3, b:bufreadpre)
2481 wincmd p
2482 call assert_equal(g:wsv1.topline, winsaveview().topline)
2483 call assert_equal(g:wsv1.lnum, winsaveview().lnum)
2484 call assert_equal(3, b:bufreadpre)
2485 close
2486 close
2487 call delete('XAutocmdBufReadPre.txt')
2488 set cpo-=g
2489endfunc
2490
Bram Moolenaar5e66b422019-01-24 21:58:10 +01002491" FileChangedShell tested in test_filechanged.vim
Bram Moolenaar69ea5872019-04-25 20:29:00 +02002492
2493" Tests for the following autocommands:
2494" - FileWritePre writing a compressed file
2495" - FileReadPost reading a compressed file
2496" - BufNewFile reading a file template
2497" - BufReadPre decompressing the file to be read
2498" - FilterReadPre substituting characters in the temp file
2499" - FilterReadPost substituting characters after filtering
2500" - FileReadPre set options for decompression
2501" - FileReadPost decompress the file
2502func Test_ReadWrite_Autocmds()
2503 " Run this test only on Unix-like systems and if gzip is available
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +02002504 CheckUnix
2505 CheckExecutable gzip
Bram Moolenaar69ea5872019-04-25 20:29:00 +02002506
2507 " Make $GZIP empty, "-v" would cause trouble.
2508 let $GZIP = ""
2509
2510 " Use a FileChangedShell autocommand to avoid a prompt for 'Xtestfile.gz'
2511 " being modified outside of Vim (noticed on Solaris).
2512 au FileChangedShell * echo 'caught FileChangedShell'
2513
2514 " Test for the FileReadPost, FileWritePre and FileWritePost autocmds
2515 augroup Test1
2516 au!
2517 au FileWritePre *.gz '[,']!gzip
2518 au FileWritePost *.gz undo
2519 au FileReadPost *.gz '[,']!gzip -d
2520 augroup END
2521
2522 new
2523 set bin
2524 call append(0, [
2525 \ 'line 2 Abcdefghijklmnopqrstuvwxyz',
2526 \ 'line 3 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2527 \ 'line 4 Abcdefghijklmnopqrstuvwxyz',
2528 \ 'line 5 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2529 \ 'line 6 Abcdefghijklmnopqrstuvwxyz',
2530 \ 'line 7 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2531 \ 'line 8 Abcdefghijklmnopqrstuvwxyz',
2532 \ 'line 9 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2533 \ 'line 10 Abcdefghijklmnopqrstuvwxyz'
2534 \ ])
2535 1,9write! Xtestfile.gz
2536 enew! | close
2537
2538 new
2539 " Read and decompress the testfile
2540 0read Xtestfile.gz
2541 call assert_equal([
2542 \ 'line 2 Abcdefghijklmnopqrstuvwxyz',
2543 \ 'line 3 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2544 \ 'line 4 Abcdefghijklmnopqrstuvwxyz',
2545 \ 'line 5 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2546 \ 'line 6 Abcdefghijklmnopqrstuvwxyz',
2547 \ 'line 7 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2548 \ 'line 8 Abcdefghijklmnopqrstuvwxyz',
2549 \ 'line 9 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2550 \ 'line 10 Abcdefghijklmnopqrstuvwxyz'
2551 \ ], getline(1, 9))
2552 enew! | close
2553
2554 augroup Test1
2555 au!
2556 augroup END
2557
2558 " Test for the FileAppendPre and FileAppendPost autocmds
2559 augroup Test2
2560 au!
2561 au BufNewFile *.c read Xtest.c
2562 au FileAppendPre *.out '[,']s/new/NEW/
2563 au FileAppendPost *.out !cat Xtest.c >> test.out
2564 augroup END
2565
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01002566 call writefile(['/*', ' * Here is a new .c file', ' */'], 'Xtest.c', 'D')
Bram Moolenaar69ea5872019-04-25 20:29:00 +02002567 new foo.c " should load Xtest.c
2568 call assert_equal(['/*', ' * Here is a new .c file', ' */'], getline(2, 4))
2569 w! >> test.out " append it to the output file
2570
2571 let contents = readfile('test.out')
2572 call assert_equal(' * Here is a NEW .c file', contents[2])
2573 call assert_equal(' * Here is a new .c file', contents[5])
2574
2575 call delete('test.out')
2576 enew! | close
2577 augroup Test2
2578 au!
2579 augroup END
2580
2581 " Test for the BufReadPre and BufReadPost autocmds
2582 augroup Test3
2583 au!
2584 " setup autocommands to decompress before reading and re-compress
2585 " afterwards
2586 au BufReadPre *.gz exe '!gzip -d ' . shellescape(expand("<afile>"))
2587 au BufReadPre *.gz call rename(expand("<afile>:r"), expand("<afile>"))
2588 au BufReadPost *.gz call rename(expand("<afile>"), expand("<afile>:r"))
2589 au BufReadPost *.gz exe '!gzip ' . shellescape(expand("<afile>:r"))
2590 augroup END
2591
2592 e! Xtestfile.gz " Edit compressed file
2593 call assert_equal([
2594 \ 'line 2 Abcdefghijklmnopqrstuvwxyz',
2595 \ 'line 3 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2596 \ 'line 4 Abcdefghijklmnopqrstuvwxyz',
2597 \ 'line 5 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2598 \ 'line 6 Abcdefghijklmnopqrstuvwxyz',
2599 \ 'line 7 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2600 \ 'line 8 Abcdefghijklmnopqrstuvwxyz',
2601 \ 'line 9 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2602 \ 'line 10 Abcdefghijklmnopqrstuvwxyz'
2603 \ ], getline(1, 9))
2604
2605 w! >> test.out " Append it to the output file
2606
2607 augroup Test3
2608 au!
2609 augroup END
2610
2611 " Test for the FilterReadPre and FilterReadPost autocmds.
2612 set shelltemp " need temp files here
2613 augroup Test4
2614 au!
2615 au FilterReadPre *.out call rename(expand("<afile>"), expand("<afile>") . ".t")
2616 au FilterReadPre *.out exe 'silent !sed s/e/E/ ' . shellescape(expand("<afile>")) . ".t >" . shellescape(expand("<afile>"))
2617 au FilterReadPre *.out exe 'silent !rm ' . shellescape(expand("<afile>")) . '.t'
2618 au FilterReadPost *.out '[,']s/x/X/g
2619 augroup END
2620
2621 e! test.out " Edit the output file
2622 1,$!cat
2623 call assert_equal([
2624 \ 'linE 2 AbcdefghijklmnopqrstuvwXyz',
2625 \ 'linE 3 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
2626 \ 'linE 4 AbcdefghijklmnopqrstuvwXyz',
2627 \ 'linE 5 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
2628 \ 'linE 6 AbcdefghijklmnopqrstuvwXyz',
2629 \ 'linE 7 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
2630 \ 'linE 8 AbcdefghijklmnopqrstuvwXyz',
2631 \ 'linE 9 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
2632 \ 'linE 10 AbcdefghijklmnopqrstuvwXyz'
2633 \ ], getline(1, 9))
2634 call assert_equal([
2635 \ 'line 2 Abcdefghijklmnopqrstuvwxyz',
2636 \ 'line 3 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2637 \ 'line 4 Abcdefghijklmnopqrstuvwxyz',
2638 \ 'line 5 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2639 \ 'line 6 Abcdefghijklmnopqrstuvwxyz',
2640 \ 'line 7 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2641 \ 'line 8 Abcdefghijklmnopqrstuvwxyz',
2642 \ 'line 9 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2643 \ 'line 10 Abcdefghijklmnopqrstuvwxyz'
2644 \ ], readfile('test.out'))
2645
2646 augroup Test4
2647 au!
2648 augroup END
2649 set shelltemp&vim
2650
2651 " Test for the FileReadPre and FileReadPost autocmds.
2652 augroup Test5
2653 au!
2654 au FileReadPre *.gz exe 'silent !gzip -d ' . shellescape(expand("<afile>"))
2655 au FileReadPre *.gz call rename(expand("<afile>:r"), expand("<afile>"))
2656 au FileReadPost *.gz '[,']s/l/L/
2657 augroup END
2658
2659 new
2660 0r Xtestfile.gz " Read compressed file
2661 call assert_equal([
2662 \ 'Line 2 Abcdefghijklmnopqrstuvwxyz',
2663 \ 'Line 3 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2664 \ 'Line 4 Abcdefghijklmnopqrstuvwxyz',
2665 \ 'Line 5 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2666 \ 'Line 6 Abcdefghijklmnopqrstuvwxyz',
2667 \ 'Line 7 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2668 \ 'Line 8 Abcdefghijklmnopqrstuvwxyz',
2669 \ 'Line 9 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2670 \ 'Line 10 Abcdefghijklmnopqrstuvwxyz'
2671 \ ], getline(1, 9))
2672 call assert_equal([
2673 \ 'line 2 Abcdefghijklmnopqrstuvwxyz',
2674 \ 'line 3 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2675 \ 'line 4 Abcdefghijklmnopqrstuvwxyz',
2676 \ 'line 5 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2677 \ 'line 6 Abcdefghijklmnopqrstuvwxyz',
2678 \ 'line 7 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2679 \ 'line 8 Abcdefghijklmnopqrstuvwxyz',
2680 \ 'line 9 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2681 \ 'line 10 Abcdefghijklmnopqrstuvwxyz'
2682 \ ], readfile('Xtestfile.gz'))
2683
2684 augroup Test5
2685 au!
2686 augroup END
2687
2688 au! FileChangedShell
2689 call delete('Xtestfile.gz')
Bram Moolenaar69ea5872019-04-25 20:29:00 +02002690 call delete('test.out')
2691endfunc
Bram Moolenaar23b51392019-05-09 21:38:43 +02002692
2693func Test_throw_in_BufWritePre()
2694 new
2695 call setline(1, ['one', 'two', 'three'])
2696 call assert_false(filereadable('Xthefile'))
2697 augroup throwing
2698 au BufWritePre X* throw 'do not write'
2699 augroup END
2700 try
2701 w Xthefile
2702 catch
2703 let caught = 1
2704 endtry
2705 call assert_equal(1, caught)
2706 call assert_false(filereadable('Xthefile'))
2707
2708 bwipe!
2709 au! throwing
2710endfunc
Bram Moolenaarcadbe1b2019-09-22 21:50:09 +02002711
Bram Moolenaar40fa12a2021-09-22 14:18:13 +02002712func Test_autocmd_in_try_block()
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +01002713 call mkdir('Xintrydir')
Bram Moolenaar40fa12a2021-09-22 14:18:13 +02002714 au BufEnter * let g:fname = expand('%')
2715 try
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +01002716 edit Xintrydir/
Bram Moolenaar40fa12a2021-09-22 14:18:13 +02002717 endtry
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +01002718 call assert_match('Xintrydir', g:fname)
Bram Moolenaar40fa12a2021-09-22 14:18:13 +02002719
2720 unlet g:fname
2721 au! BufEnter
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +01002722 call delete('Xintrydir', 'rf')
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