blob: 337019096084daa0f4f66e6baa6ceacef571b409 [file] [log] [blame]
Bram Moolenaar14735512016-03-26 21:00:08 +01001" Tests for autocommands
2
Bram Moolenaar8c64a362018-03-23 22:39:31 +01003source shared.vim
Bram Moolenaar8c5a2782019-08-07 23:07:07 +02004source check.vim
Bram Moolenaarcadbe1b2019-09-22 21:50:09 +02005source term_util.vim
LemonBoy09371822022-04-08 15:18:45 +01006source screendump.vim
Bram Moolenaar6f2465d2022-03-22 18:13:01 +00007import './vim9.vim' as v9
Bram Moolenaar8c64a362018-03-23 22:39:31 +01008
Bram Moolenaar1e115362019-01-09 23:01:02 +01009func s:cleanup_buffers() abort
Bram Moolenaarb3435b02016-09-29 20:54:59 +020010 for bnr in range(1, bufnr('$'))
11 if bufloaded(bnr) && bufnr('%') != bnr
12 execute 'bd! ' . bnr
13 endif
14 endfor
Bram Moolenaar04f62f82017-07-19 18:18:39 +020015endfunc
Bram Moolenaarb3435b02016-09-29 20:54:59 +020016
Bram Moolenaar14735512016-03-26 21:00:08 +010017func Test_vim_did_enter()
18 call assert_false(v:vim_did_enter)
19
20 " This script will never reach the main loop, can't check if v:vim_did_enter
21 " becomes one.
22endfunc
Bram Moolenaar40b1b542016-04-20 20:18:23 +020023
Bram Moolenaar75911162020-07-21 19:44:47 +020024" Test for the CursorHold autocmd
25func Test_CursorHold_autocmd()
26 CheckRunVimInTerminal
Bram Moolenaare1f3ab72022-09-04 21:29:08 +010027 call writefile(['one', 'two', 'three'], 'XoneTwoThree', 'D')
Bram Moolenaar75911162020-07-21 19:44:47 +020028 let before =<< trim END
29 set updatetime=10
Bram Moolenaare7cda972022-08-29 11:02:59 +010030 au CursorHold * call writefile([line('.')], 'XCHoutput', 'a')
Bram Moolenaar75911162020-07-21 19:44:47 +020031 END
Bram Moolenaare1f3ab72022-09-04 21:29:08 +010032 call writefile(before, 'XCHinit', 'D')
Bram Moolenaare7cda972022-08-29 11:02:59 +010033 let buf = RunVimInTerminal('-S XCHinit XoneTwoThree', {})
Bram Moolenaar17f67542020-08-20 18:29:13 +020034 call term_sendkeys(buf, "G")
Bram Moolenaar62cd26a2020-10-11 20:08:44 +020035 call term_wait(buf, 50)
Bram Moolenaar75911162020-07-21 19:44:47 +020036 call term_sendkeys(buf, "gg")
37 call term_wait(buf)
Bram Moolenaare7cda972022-08-29 11:02:59 +010038 call WaitForAssert({-> assert_equal(['1'], readfile('XCHoutput')[-1:-1])})
Bram Moolenaar75911162020-07-21 19:44:47 +020039 call term_sendkeys(buf, "j")
40 call term_wait(buf)
Bram Moolenaare7cda972022-08-29 11:02:59 +010041 call WaitForAssert({-> assert_equal(['1', '2'], readfile('XCHoutput')[-2:-1])})
Bram Moolenaar75911162020-07-21 19:44:47 +020042 call term_sendkeys(buf, "j")
43 call term_wait(buf)
Bram Moolenaare7cda972022-08-29 11:02:59 +010044 call WaitForAssert({-> assert_equal(['1', '2', '3'], readfile('XCHoutput')[-3:-1])})
Bram Moolenaar75911162020-07-21 19:44:47 +020045 call StopVimInTerminal(buf)
46
Bram Moolenaare7cda972022-08-29 11:02:59 +010047 call delete('XCHoutput')
Bram Moolenaar75911162020-07-21 19:44:47 +020048endfunc
49
Bram Moolenaarc67e8922016-05-24 16:07:40 +020050if has('timers')
Bram Moolenaar97b00752019-05-12 13:07:14 +020051
Bram Moolenaarc67e8922016-05-24 16:07:40 +020052 func ExitInsertMode(id)
53 call feedkeys("\<Esc>")
54 endfunc
55
56 func Test_cursorhold_insert()
Bram Moolenaarf18c4db2016-09-08 22:10:06 +020057 " Need to move the cursor.
58 call feedkeys("ggG", "xt")
59
Bram Moolenaarc67e8922016-05-24 16:07:40 +020060 let g:triggered = 0
61 au CursorHoldI * let g:triggered += 1
62 set updatetime=20
Bram Moolenaar92bb83e2021-02-03 23:04:46 +010063 call timer_start(200, 'ExitInsertMode')
Bram Moolenaarc67e8922016-05-24 16:07:40 +020064 call feedkeys('a', 'x!')
65 call assert_equal(1, g:triggered)
Bram Moolenaar26d98212019-01-27 22:32:55 +010066 unlet g:triggered
67 au! CursorHoldI
68 set updatetime&
69 endfunc
70
71 func Test_cursorhold_insert_with_timer_interrupt()
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +020072 CheckFeature job
Bram Moolenaar26d98212019-01-27 22:32:55 +010073 " Need to move the cursor.
74 call feedkeys("ggG", "xt")
75
76 " Confirm the timer invoked in exit_cb of the job doesn't disturb
77 " CursorHoldI event.
78 let g:triggered = 0
79 au CursorHoldI * let g:triggered += 1
Bram Moolenaar62cd26a2020-10-11 20:08:44 +020080 set updatetime=100
Bram Moolenaar26d98212019-01-27 22:32:55 +010081 call job_start(has('win32') ? 'cmd /c echo:' : 'echo',
Bram Moolenaar62cd26a2020-10-11 20:08:44 +020082 \ {'exit_cb': {-> timer_start(200, 'ExitInsertMode')}})
Bram Moolenaar26d98212019-01-27 22:32:55 +010083 call feedkeys('a', 'x!')
84 call assert_equal(1, g:triggered)
85 unlet g:triggered
Bram Moolenaare99e8442016-07-26 20:43:40 +020086 au! CursorHoldI
Bram Moolenaaraeac9002016-09-06 22:15:08 +020087 set updatetime&
Bram Moolenaarc67e8922016-05-24 16:07:40 +020088 endfunc
89
90 func Test_cursorhold_insert_ctrl_x()
91 let g:triggered = 0
92 au CursorHoldI * let g:triggered += 1
93 set updatetime=20
94 call timer_start(100, 'ExitInsertMode')
95 " CursorHoldI does not trigger after CTRL-X
96 call feedkeys("a\<C-X>", 'x!')
97 call assert_equal(0, g:triggered)
Bram Moolenaar26d98212019-01-27 22:32:55 +010098 unlet g:triggered
Bram Moolenaare99e8442016-07-26 20:43:40 +020099 au! CursorHoldI
Bram Moolenaaraeac9002016-09-06 22:15:08 +0200100 set updatetime&
Bram Moolenaarc67e8922016-05-24 16:07:40 +0200101 endfunc
Bram Moolenaar97b00752019-05-12 13:07:14 +0200102
Bram Moolenaar5a9357d2021-10-03 16:22:05 +0100103 func Test_cursorhold_insert_ctrl_g_U()
104 au CursorHoldI * :
105 set updatetime=20
106 new
107 call timer_start(100, { -> feedkeys("\<Left>foo\<Esc>", 't') })
108 call feedkeys("i()\<C-g>U", 'tx!')
109 sleep 200m
110 call assert_equal('(foo)', getline(1))
111 undo
112 call assert_equal('', getline(1))
113
114 bwipe!
115 au! CursorHoldI
116 set updatetime&
117 endfunc
118
Bram Moolenaar97b00752019-05-12 13:07:14 +0200119 func Test_OptionSet_modeline()
120 call test_override('starting', 1)
121 au! OptionSet
122 augroup set_tabstop
123 au OptionSet tabstop call timer_start(1, {-> execute("echo 'Handler called'", "")})
124 augroup END
Bram Moolenaare1f3ab72022-09-04 21:29:08 +0100125 call writefile(['vim: set ts=7 sw=5 :', 'something'], 'XoptionsetModeline', 'D')
Bram Moolenaar97b00752019-05-12 13:07:14 +0200126 set modeline
127 let v:errmsg = ''
128 call assert_fails('split XoptionsetModeline', 'E12:')
129 call assert_equal(7, &ts)
130 call assert_equal('', v:errmsg)
131
132 augroup set_tabstop
133 au!
134 augroup END
135 bwipe!
136 set ts&
Bram Moolenaar97b00752019-05-12 13:07:14 +0200137 call test_override('starting', 0)
138 endfunc
139
140endif "has('timers')
Bram Moolenaar40b1b542016-04-20 20:18:23 +0200141
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200142func Test_bufunload()
Bram Moolenaarc67e8922016-05-24 16:07:40 +0200143 augroup test_bufunload_group
144 autocmd!
145 autocmd BufUnload * call add(s:li, "bufunload")
146 autocmd BufDelete * call add(s:li, "bufdelete")
147 autocmd BufWipeout * call add(s:li, "bufwipeout")
148 augroup END
Bram Moolenaar40b1b542016-04-20 20:18:23 +0200149
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100150 let s:li = []
Bram Moolenaarc67e8922016-05-24 16:07:40 +0200151 new
152 setlocal bufhidden=
153 bunload
154 call assert_equal(["bufunload", "bufdelete"], s:li)
Bram Moolenaar40b1b542016-04-20 20:18:23 +0200155
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100156 let s:li = []
Bram Moolenaarc67e8922016-05-24 16:07:40 +0200157 new
158 setlocal bufhidden=delete
159 bunload
160 call assert_equal(["bufunload", "bufdelete"], s:li)
161
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100162 let s:li = []
Bram Moolenaarc67e8922016-05-24 16:07:40 +0200163 new
164 setlocal bufhidden=unload
165 bwipeout
166 call assert_equal(["bufunload", "bufdelete", "bufwipeout"], s:li)
167
Bram Moolenaare99e8442016-07-26 20:43:40 +0200168 au! test_bufunload_group
Bram Moolenaarc67e8922016-05-24 16:07:40 +0200169 augroup! test_bufunload_group
Bram Moolenaar40b1b542016-04-20 20:18:23 +0200170endfunc
Bram Moolenaar30445cb2016-07-09 15:21:02 +0200171
172" SEGV occurs in older versions. (At least 7.4.2005 or older)
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200173func Test_autocmd_bufunload_with_tabnext()
Bram Moolenaar30445cb2016-07-09 15:21:02 +0200174 tabedit
175 tabfirst
176
177 augroup test_autocmd_bufunload_with_tabnext_group
178 autocmd!
179 autocmd BufUnload <buffer> tabnext
180 augroup END
181
182 quit
183 call assert_equal(2, tabpagenr('$'))
184
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200185 autocmd! test_autocmd_bufunload_with_tabnext_group
Bram Moolenaar30445cb2016-07-09 15:21:02 +0200186 augroup! test_autocmd_bufunload_with_tabnext_group
187 tablast
188 quit
189endfunc
Bram Moolenaarc917da42016-07-19 22:31:36 +0200190
Bram Moolenaar5ed58c72021-01-28 14:24:55 +0100191func Test_argdelete_in_next()
192 au BufNew,BufEnter,BufLeave,BufWinEnter * argdel
193 call assert_fails('next a b', 'E1156:')
194 au! BufNew,BufEnter,BufLeave,BufWinEnter *
195endfunc
196
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200197func Test_autocmd_bufwinleave_with_tabfirst()
Bram Moolenaarf9e687e2016-09-04 21:33:09 +0200198 tabedit
199 augroup sample
200 autocmd!
201 autocmd BufWinLeave <buffer> tabfirst
202 augroup END
203 call setline(1, ['a', 'b', 'c'])
204 edit! a.txt
Bram Moolenaarf18c4db2016-09-08 22:10:06 +0200205 tabclose
Bram Moolenaarf9e687e2016-09-04 21:33:09 +0200206endfunc
207
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200208" SEGV occurs in older versions. (At least 7.4.2321 or older)
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200209func Test_autocmd_bufunload_avoiding_SEGV_01()
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200210 split aa.txt
211 let lastbuf = bufnr('$')
212
213 augroup test_autocmd_bufunload
214 autocmd!
215 exe 'autocmd BufUnload <buffer> ' . (lastbuf + 1) . 'bwipeout!'
216 augroup END
217
Bram Moolenaar28ee8922020-10-28 20:20:00 +0100218 call assert_fails('edit bb.txt', 'E937:')
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200219
220 autocmd! test_autocmd_bufunload
221 augroup! test_autocmd_bufunload
222 bwipe! aa.txt
223 bwipe! bb.txt
224endfunc
225
226" SEGV occurs in older versions. (At least 7.4.2321 or older)
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200227func Test_autocmd_bufunload_avoiding_SEGV_02()
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200228 setlocal buftype=nowrite
229 let lastbuf = bufnr('$')
230
231 augroup test_autocmd_bufunload
232 autocmd!
233 exe 'autocmd BufUnload <buffer> ' . (lastbuf + 1) . 'bwipeout!'
234 augroup END
235
236 normal! i1
237 call assert_fails('edit a.txt', 'E517:')
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200238
239 autocmd! test_autocmd_bufunload
240 augroup! test_autocmd_bufunload
241 bwipe! a.txt
242endfunc
243
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100244func Test_autocmd_dummy_wipeout()
245 " prepare files
Bram Moolenaare1f3ab72022-09-04 21:29:08 +0100246 call writefile([''], 'Xdummywipetest1.txt', 'D')
247 call writefile([''], 'Xdummywipetest2.txt', 'D')
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100248 augroup test_bufunload_group
249 autocmd!
250 autocmd BufUnload * call add(s:li, "bufunload")
251 autocmd BufDelete * call add(s:li, "bufdelete")
252 autocmd BufWipeout * call add(s:li, "bufwipeout")
253 augroup END
254
255 let s:li = []
256 split Xdummywipetest1.txt
257 silent! vimgrep /notmatched/ Xdummywipetest*
258 call assert_equal(["bufunload", "bufwipeout"], s:li)
259
260 bwipeout
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100261 au! test_bufunload_group
262 augroup! test_bufunload_group
263endfunc
264
Bram Moolenaarc917da42016-07-19 22:31:36 +0200265func Test_win_tab_autocmd()
266 let g:record = []
267
268 augroup testing
269 au WinNew * call add(g:record, 'WinNew')
naohiro ono23beefe2021-11-13 12:38:49 +0000270 au WinClosed * call add(g:record, 'WinClosed')
Bram Moolenaarc917da42016-07-19 22:31:36 +0200271 au WinEnter * call add(g:record, 'WinEnter')
272 au WinLeave * call add(g:record, 'WinLeave')
273 au TabNew * call add(g:record, 'TabNew')
Bram Moolenaar12c11d52016-07-19 23:13:03 +0200274 au TabClosed * call add(g:record, 'TabClosed')
Bram Moolenaarc917da42016-07-19 22:31:36 +0200275 au TabEnter * call add(g:record, 'TabEnter')
276 au TabLeave * call add(g:record, 'TabLeave')
277 augroup END
278
279 split
280 tabnew
281 close
282 close
283
284 call assert_equal([
285 \ 'WinLeave', 'WinNew', 'WinEnter',
286 \ 'WinLeave', 'TabLeave', 'WinNew', 'WinEnter', 'TabNew', 'TabEnter',
naohiro ono23beefe2021-11-13 12:38:49 +0000287 \ 'WinLeave', 'TabLeave', 'WinClosed', 'TabClosed', 'WinEnter', 'TabEnter',
288 \ 'WinLeave', 'WinClosed', 'WinEnter'
Bram Moolenaarc917da42016-07-19 22:31:36 +0200289 \ ], g:record)
290
Bram Moolenaar12c11d52016-07-19 23:13:03 +0200291 let g:record = []
292 tabnew somefile
293 tabnext
294 bwipe somefile
295
296 call assert_equal([
297 \ 'WinLeave', 'TabLeave', 'WinNew', 'WinEnter', 'TabNew', 'TabEnter',
298 \ 'WinLeave', 'TabLeave', 'WinEnter', 'TabEnter',
naohiro ono23beefe2021-11-13 12:38:49 +0000299 \ 'WinClosed', 'TabClosed'
Bram Moolenaar12c11d52016-07-19 23:13:03 +0200300 \ ], g:record)
301
Bram Moolenaarc917da42016-07-19 22:31:36 +0200302 augroup testing
303 au!
304 augroup END
305 unlet g:record
306endfunc
Bram Moolenaare99e8442016-07-26 20:43:40 +0200307
LemonBoy09371822022-04-08 15:18:45 +0100308func Test_WinScrolled()
309 CheckRunVimInTerminal
310
311 let lines =<< trim END
zeertzjqd58862d2022-04-12 11:32:48 +0100312 set nowrap scrolloff=0
313 for ii in range(1, 18)
314 call setline(ii, repeat(nr2char(96 + ii), ii * 2))
315 endfor
316 let win_id = win_getid()
317 let g:matched = v:false
318 execute 'au WinScrolled' win_id 'let g:matched = v:true'
319 let g:scrolled = 0
320 au WinScrolled * let g:scrolled += 1
321 au WinScrolled * let g:amatch = str2nr(expand('<amatch>'))
322 au WinScrolled * let g:afile = str2nr(expand('<afile>'))
LemonBoy09371822022-04-08 15:18:45 +0100323 END
Bram Moolenaare1f3ab72022-09-04 21:29:08 +0100324 call writefile(lines, 'Xtest_winscrolled', 'D')
LemonBoy09371822022-04-08 15:18:45 +0100325 let buf = RunVimInTerminal('-S Xtest_winscrolled', {'rows': 6})
326
327 call term_sendkeys(buf, ":echo g:scrolled\<CR>")
328 call WaitForAssert({-> assert_match('^0 ', term_getline(buf, 6))}, 1000)
329
330 " Scroll left/right in Normal mode.
331 call term_sendkeys(buf, "zlzh:echo g:scrolled\<CR>")
332 call WaitForAssert({-> assert_match('^2 ', term_getline(buf, 6))}, 1000)
333
334 " Scroll up/down in Normal mode.
335 call term_sendkeys(buf, "\<c-e>\<c-y>:echo g:scrolled\<CR>")
336 call WaitForAssert({-> assert_match('^4 ', term_getline(buf, 6))}, 1000)
337
338 " Scroll up/down in Insert mode.
339 call term_sendkeys(buf, "Mi\<c-x>\<c-e>\<Esc>i\<c-x>\<c-y>\<Esc>")
340 call term_sendkeys(buf, ":echo g:scrolled\<CR>")
341 call WaitForAssert({-> assert_match('^6 ', term_getline(buf, 6))}, 1000)
342
343 " Scroll the window horizontally to focus the last letter of the third line
344 " containing only six characters. Moving to the previous and shorter lines
345 " should trigger another autocommand as Vim has to make them visible.
346 call term_sendkeys(buf, "5zl2k")
347 call term_sendkeys(buf, ":echo g:scrolled\<CR>")
348 call WaitForAssert({-> assert_match('^8 ', term_getline(buf, 6))}, 1000)
349
350 " Ensure the command was triggered for the specified window ID.
351 call term_sendkeys(buf, ":echo g:matched\<CR>")
352 call WaitForAssert({-> assert_match('^v:true ', term_getline(buf, 6))}, 1000)
353
354 " Ensure the expansion of <amatch> and <afile> matches the window ID.
355 call term_sendkeys(buf, ":echo g:amatch == win_id && g:afile == win_id\<CR>")
356 call WaitForAssert({-> assert_match('^v:true ', term_getline(buf, 6))}, 1000)
357
358 call StopVimInTerminal(buf)
LemonBoy09371822022-04-08 15:18:45 +0100359endfunc
360
LemonBoy66e13ae2022-04-21 22:52:11 +0100361func Test_WinScrolled_mouse()
362 CheckRunVimInTerminal
363
364 let lines =<< trim END
365 set nowrap scrolloff=0
366 set mouse=a term=xterm ttymouse=sgr mousetime=200 clipboard=
367 call setline(1, ['foo']->repeat(32))
368 split
369 let g:scrolled = 0
370 au WinScrolled * let g:scrolled += 1
371 END
Bram Moolenaare1f3ab72022-09-04 21:29:08 +0100372 call writefile(lines, 'Xtest_winscrolled_mouse', 'D')
LemonBoy66e13ae2022-04-21 22:52:11 +0100373 let buf = RunVimInTerminal('-S Xtest_winscrolled_mouse', {'rows': 10})
374
375 " With the upper split focused, send a scroll-down event to the unfocused one.
376 call test_setmouse(7, 1)
377 call term_sendkeys(buf, "\<ScrollWheelDown>")
378 call TermWait(buf)
379 call term_sendkeys(buf, ":echo g:scrolled\<CR>")
380 call WaitForAssert({-> assert_match('^1', term_getline(buf, 10))}, 1000)
381
382 " Again, but this time while we're in insert mode.
383 call term_sendkeys(buf, "i\<ScrollWheelDown>\<Esc>")
384 call TermWait(buf)
385 call term_sendkeys(buf, ":echo g:scrolled\<CR>")
386 call WaitForAssert({-> assert_match('^2', term_getline(buf, 10))}, 1000)
387
388 call StopVimInTerminal(buf)
LemonBoy66e13ae2022-04-21 22:52:11 +0100389endfunc
390
zeertzjqd58862d2022-04-12 11:32:48 +0100391func Test_WinScrolled_close_curwin()
392 CheckRunVimInTerminal
393
394 let lines =<< trim END
395 set nowrap scrolloff=0
396 call setline(1, ['aaa', 'bbb'])
397 vsplit
398 au WinScrolled * close
399 au VimLeave * call writefile(['123456'], 'Xtestout')
400 END
Bram Moolenaare1f3ab72022-09-04 21:29:08 +0100401 call writefile(lines, 'Xtest_winscrolled_close_curwin', 'D')
zeertzjqd58862d2022-04-12 11:32:48 +0100402 let buf = RunVimInTerminal('-S Xtest_winscrolled_close_curwin', {'rows': 6})
403
404 " This was using freed memory
405 call term_sendkeys(buf, "\<C-E>")
406 call TermWait(buf)
407 call StopVimInTerminal(buf)
408
409 call assert_equal(['123456'], readfile('Xtestout'))
410
zeertzjqd58862d2022-04-12 11:32:48 +0100411 call delete('Xtestout')
412endfunc
413
zeertzjq670ab032022-08-28 19:16:15 +0100414func Test_WinScrolled_long_wrapped()
415 CheckRunVimInTerminal
416
417 let lines =<< trim END
418 set scrolloff=0
419 let height = winheight(0)
420 let width = winwidth(0)
421 let g:scrolled = 0
422 au WinScrolled * let g:scrolled += 1
423 call setline(1, repeat('foo', height * width))
424 call cursor(1, height * width)
425 END
Bram Moolenaare1f3ab72022-09-04 21:29:08 +0100426 call writefile(lines, 'Xtest_winscrolled_long_wrapped', 'D')
zeertzjq670ab032022-08-28 19:16:15 +0100427 let buf = RunVimInTerminal('-S Xtest_winscrolled_long_wrapped', {'rows': 6})
428
429 call term_sendkeys(buf, ":echo g:scrolled\<CR>")
430 call WaitForAssert({-> assert_match('^0 ', term_getline(buf, 6))}, 1000)
431
432 call term_sendkeys(buf, 'gj')
433 call term_sendkeys(buf, ":echo g:scrolled\<CR>")
434 call WaitForAssert({-> assert_match('^1 ', term_getline(buf, 6))}, 1000)
435
436 call term_sendkeys(buf, '0')
437 call term_sendkeys(buf, ":echo g:scrolled\<CR>")
438 call WaitForAssert({-> assert_match('^2 ', term_getline(buf, 6))}, 1000)
439
440 call term_sendkeys(buf, '$')
441 call term_sendkeys(buf, ":echo g:scrolled\<CR>")
442 call WaitForAssert({-> assert_match('^3 ', term_getline(buf, 6))}, 1000)
zeertzjq670ab032022-08-28 19:16:15 +0100443endfunc
444
naohiro ono23beefe2021-11-13 12:38:49 +0000445func Test_WinClosed()
446 " Test that the pattern is matched against the closed window's ID, and both
447 " <amatch> and <afile> are set to it.
448 new
449 let winid = win_getid()
450 let g:matched = v:false
451 augroup test-WinClosed
452 autocmd!
453 execute 'autocmd WinClosed' winid 'let g:matched = v:true'
454 autocmd WinClosed * let g:amatch = str2nr(expand('<amatch>'))
455 autocmd WinClosed * let g:afile = str2nr(expand('<afile>'))
456 augroup END
457 close
458 call assert_true(g:matched)
459 call assert_equal(winid, g:amatch)
460 call assert_equal(winid, g:afile)
461
462 " Test that WinClosed is non-recursive.
463 new
464 new
465 call assert_equal(3, winnr('$'))
466 let g:triggered = 0
467 augroup test-WinClosed
468 autocmd!
469 autocmd WinClosed * let g:triggered += 1
470 autocmd WinClosed * 2 wincmd c
471 augroup END
472 close
473 call assert_equal(1, winnr('$'))
474 call assert_equal(1, g:triggered)
475
476 autocmd! test-WinClosed
477 augroup! test-WinClosed
478 unlet g:matched
479 unlet g:amatch
480 unlet g:afile
481 unlet g:triggered
482endfunc
483
Bram Moolenaarc947b9a2022-04-06 17:59:21 +0100484func Test_WinClosed_throws()
485 vnew
486 let bnr = bufnr()
487 call assert_equal(1, bufloaded(bnr))
488 augroup test-WinClosed
489 autocmd WinClosed * throw 'foo'
490 augroup END
491 try
492 close
493 catch /.*/
494 endtry
495 call assert_equal(0, bufloaded(bnr))
496
497 autocmd! test-WinClosed
498 augroup! test-WinClosed
499endfunc
500
zeertzjq6a069402022-04-07 14:08:29 +0100501func Test_WinClosed_throws_with_tabs()
502 tabnew
503 let bnr = bufnr()
504 call assert_equal(1, bufloaded(bnr))
505 augroup test-WinClosed
506 autocmd WinClosed * throw 'foo'
507 augroup END
508 try
509 close
510 catch /.*/
511 endtry
512 call assert_equal(0, bufloaded(bnr))
513
514 autocmd! test-WinClosed
515 augroup! test-WinClosed
516endfunc
517
Bram Moolenaare99e8442016-07-26 20:43:40 +0200518func s:AddAnAutocmd()
519 augroup vimBarTest
520 au BufReadCmd * echo 'hello'
521 augroup END
522 call assert_equal(3, len(split(execute('au vimBarTest'), "\n")))
523endfunc
524
525func Test_early_bar()
526 " test that a bar is recognized before the {event}
527 call s:AddAnAutocmd()
Bram Moolenaarb8e642f2021-11-20 10:38:25 +0000528 augroup vimBarTest | au! | let done = 77 | augroup END
Bram Moolenaare99e8442016-07-26 20:43:40 +0200529 call assert_equal(1, len(split(execute('au vimBarTest'), "\n")))
Bram Moolenaarb8e642f2021-11-20 10:38:25 +0000530 call assert_equal(77, done)
Bram Moolenaare99e8442016-07-26 20:43:40 +0200531
532 call s:AddAnAutocmd()
Bram Moolenaarb8e642f2021-11-20 10:38:25 +0000533 augroup vimBarTest| au!| let done = 88 | augroup END
Bram Moolenaare99e8442016-07-26 20:43:40 +0200534 call assert_equal(1, len(split(execute('au vimBarTest'), "\n")))
Bram Moolenaarb8e642f2021-11-20 10:38:25 +0000535 call assert_equal(88, done)
Bram Moolenaare99e8442016-07-26 20:43:40 +0200536
537 " test that a bar is recognized after the {event}
538 call s:AddAnAutocmd()
Bram Moolenaarb8e642f2021-11-20 10:38:25 +0000539 augroup vimBarTest| au!BufReadCmd| let done = 99 | augroup END
Bram Moolenaare99e8442016-07-26 20:43:40 +0200540 call assert_equal(1, len(split(execute('au vimBarTest'), "\n")))
Bram Moolenaarb8e642f2021-11-20 10:38:25 +0000541 call assert_equal(99, done)
Bram Moolenaare99e8442016-07-26 20:43:40 +0200542
543 " test that a bar is recognized after the {group}
544 call s:AddAnAutocmd()
545 au! vimBarTest|echo 'hello'
546 call assert_equal(1, len(split(execute('au vimBarTest'), "\n")))
547endfunc
Bram Moolenaarf2c4c392016-07-29 20:50:24 +0200548
Bram Moolenaar5c809082016-09-01 16:21:48 +0200549func RemoveGroup()
550 autocmd! StartOK
551 augroup! StartOK
552endfunc
553
Bram Moolenaarf2c4c392016-07-29 20:50:24 +0200554func Test_augroup_warning()
555 augroup TheWarning
556 au VimEnter * echo 'entering'
557 augroup END
Bram Moolenaar5dc4e2f2020-11-25 14:15:12 +0100558 call assert_match("TheWarning.*VimEnter", execute('au VimEnter'))
Bram Moolenaarf2c4c392016-07-29 20:50:24 +0200559 redir => res
560 augroup! TheWarning
561 redir END
Bram Moolenaar5dc4e2f2020-11-25 14:15:12 +0100562 call assert_match("W19:", res)
563 call assert_match("-Deleted-.*VimEnter", execute('au VimEnter'))
Bram Moolenaarf2c4c392016-07-29 20:50:24 +0200564
565 " check "Another" does not take the pace of the deleted entry
566 augroup Another
567 augroup END
Bram Moolenaar5dc4e2f2020-11-25 14:15:12 +0100568 call assert_match("-Deleted-.*VimEnter", execute('au VimEnter'))
Bram Moolenaaraeac9002016-09-06 22:15:08 +0200569 augroup! Another
Bram Moolenaar5c809082016-09-01 16:21:48 +0200570
571 " no warning for postpone aucmd delete
572 augroup StartOK
573 au VimEnter * call RemoveGroup()
574 augroup END
Bram Moolenaar5dc4e2f2020-11-25 14:15:12 +0100575 call assert_match("StartOK.*VimEnter", execute('au VimEnter'))
Bram Moolenaar5c809082016-09-01 16:21:48 +0200576 redir => res
577 doautocmd VimEnter
578 redir END
Bram Moolenaar5dc4e2f2020-11-25 14:15:12 +0100579 call assert_notmatch("W19:", res)
Bram Moolenaarde653f02016-09-03 16:59:06 +0200580 au! VimEnter
Bram Moolenaarad48e6c2020-04-21 22:19:45 +0200581
582 call assert_fails('augroup!', 'E471:')
Bram Moolenaarf2c4c392016-07-29 20:50:24 +0200583endfunc
Bram Moolenaarb62cc362016-09-03 16:43:53 +0200584
Bram Moolenaar8d84ff12017-10-26 16:42:16 +0200585func Test_BufReadCmdHelp()
586 " This used to cause access to free memory
587 au BufReadCmd * e +h
588 help
589
Bram Moolenaar8d84ff12017-10-26 16:42:16 +0200590 au! BufReadCmd
591endfunc
592
593func Test_BufReadCmdHelpJump()
594 " This used to cause access to free memory
595 au BufReadCmd * e +h{
Bram Moolenaarcf1ba352017-10-27 00:55:04 +0200596 " } to fix highlighting
597 call assert_fails('help', 'E434:')
Bram Moolenaar8d84ff12017-10-26 16:42:16 +0200598
Bram Moolenaar8d84ff12017-10-26 16:42:16 +0200599 au! BufReadCmd
600endfunc
601
zeertzjq93f72cc2022-08-26 15:34:52 +0100602" BufReadCmd is triggered for a "nofile" buffer. Check all values.
Bram Moolenaarb1d2c812022-08-26 11:55:01 +0100603func Test_BufReadCmdNofile()
zeertzjq93f72cc2022-08-26 15:34:52 +0100604 for val in ['nofile',
605 \ 'nowrite',
606 \ 'acwrite',
607 \ 'quickfix',
608 \ 'help',
609 \ 'terminal',
610 \ 'prompt',
611 \ 'popup',
612 \ ]
613 new somefile
614 exe 'set buftype=' .. val
615 au BufReadCmd somefile call setline(1, 'triggered')
616 edit
617 call assert_equal('triggered', getline(1))
Bram Moolenaarb1d2c812022-08-26 11:55:01 +0100618
zeertzjq93f72cc2022-08-26 15:34:52 +0100619 au! BufReadCmd
620 bwipe!
621 endfor
Bram Moolenaarb1d2c812022-08-26 11:55:01 +0100622endfunc
623
Bram Moolenaarb62cc362016-09-03 16:43:53 +0200624func Test_augroup_deleted()
Bram Moolenaarde653f02016-09-03 16:59:06 +0200625 " This caused a crash before E936 was introduced
Bram Moolenaarb62cc362016-09-03 16:43:53 +0200626 augroup x
Bram Moolenaarde653f02016-09-03 16:59:06 +0200627 call assert_fails('augroup! x', 'E936:')
628 au VimEnter * echo
629 augroup end
Bram Moolenaarb62cc362016-09-03 16:43:53 +0200630 augroup! x
Bram Moolenaar5dc4e2f2020-11-25 14:15:12 +0100631 call assert_match("-Deleted-.*VimEnter", execute('au VimEnter'))
Bram Moolenaarde653f02016-09-03 16:59:06 +0200632 au! VimEnter
Bram Moolenaarb62cc362016-09-03 16:43:53 +0200633endfunc
634
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200635" Tests for autocommands on :close command.
636" This used to be in test13.
637func Test_three_windows()
Bram Moolenaarb3435b02016-09-29 20:54:59 +0200638 " Clean up buffers, because in some cases this function fails.
639 call s:cleanup_buffers()
640
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200641 " Write three files and open them, each in a window.
642 " Then go to next window, with autocommand that deletes the previous one.
643 " Do this twice, writing the file.
644 e! Xtestje1
645 call setline(1, 'testje1')
646 w
647 sp Xtestje2
648 call setline(1, 'testje2')
649 w
650 sp Xtestje3
651 call setline(1, 'testje3')
652 w
653 wincmd w
654 au WinLeave Xtestje2 bwipe
655 wincmd w
656 call assert_equal('Xtestje1', expand('%'))
657
658 au WinLeave Xtestje1 bwipe Xtestje3
659 close
660 call assert_equal('Xtestje1', expand('%'))
661
662 " Test deleting the buffer on a Unload event. If this goes wrong there
663 " will be the ATTENTION prompt.
664 e Xtestje1
665 au!
666 au! BufUnload Xtestje1 bwipe
667 call assert_fails('e Xtestje3', 'E937:')
668 call assert_equal('Xtestje3', expand('%'))
669
670 e Xtestje2
671 sp Xtestje1
672 call assert_fails('e', 'E937:')
Bram Moolenaara997b452018-04-17 23:24:06 +0200673 call assert_equal('Xtestje1', expand('%'))
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200674
675 " Test changing buffers in a BufWipeout autocommand. If this goes wrong
676 " there are ml_line errors and/or a Crash.
677 au!
678 only
679 e Xanother
680 e Xtestje1
681 bwipe Xtestje2
682 bwipe Xtestje3
683 au BufWipeout Xtestje1 buf Xtestje1
684 bwipe
685 call assert_equal('Xanother', expand('%'))
686
687 only
688 help
689 wincmd w
690 1quit
691 call assert_equal('Xanother', expand('%'))
692
693 au!
Bram Moolenaar4520d442017-03-19 16:09:46 +0100694 enew
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200695 call delete('Xtestje1')
696 call delete('Xtestje2')
697 call delete('Xtestje3')
698endfunc
Bram Moolenaare13b9af2017-01-13 22:01:02 +0100699
700func Test_BufEnter()
701 au! BufEnter
702 au Bufenter * let val = val . '+'
703 let g:val = ''
704 split NewFile
705 call assert_equal('+', g:val)
706 bwipe!
707 call assert_equal('++', g:val)
708
709 " Also get BufEnter when editing a directory
Bram Moolenaar6f14da12022-09-07 21:30:44 +0100710 call mkdir('Xbufenterdir', 'D')
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +0100711 split Xbufenterdir
Bram Moolenaare13b9af2017-01-13 22:01:02 +0100712 call assert_equal('+++', g:val)
Bram Moolenaare94260f2017-03-21 15:50:12 +0100713
714 " On MS-Windows we can't edit the directory, make sure we wipe the right
715 " buffer.
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +0100716 bwipe! Xbufenterdir
Bram Moolenaare13b9af2017-01-13 22:01:02 +0100717 au! BufEnter
Bram Moolenaara9b5b852022-08-26 13:16:20 +0100718
719 " Editing a "nofile" buffer doesn't read the file but does trigger BufEnter
zeertzjq93f72cc2022-08-26 15:34:52 +0100720 " for historic reasons. Also test other 'buftype' values.
721 for val in ['nofile',
722 \ 'nowrite',
723 \ 'acwrite',
724 \ 'quickfix',
725 \ 'help',
726 \ 'terminal',
727 \ 'prompt',
728 \ 'popup',
729 \ ]
730 new somefile
731 exe 'set buftype=' .. val
732 au BufEnter somefile call setline(1, 'some text')
733 edit
734 call assert_equal('some text', getline(1))
735 bwipe!
736 au! BufEnter
737 endfor
Bram Moolenaare13b9af2017-01-13 22:01:02 +0100738endfunc
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100739
740" Closing a window might cause an endless loop
741" E814 for older Vims
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200742func Test_autocmd_bufwipe_in_SessLoadPost()
Bram Moolenaar1d68d9b2017-10-13 22:33:32 +0200743 edit Xtest
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100744 tabnew
Bram Moolenaar1d68d9b2017-10-13 22:33:32 +0200745 file Xsomething
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100746 set noswapfile
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100747 mksession!
748
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200749 let content =<< trim [CODE]
Bram Moolenaar62cd26a2020-10-11 20:08:44 +0200750 call test_override('ui_delay', 10)
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200751 set nocp noswapfile
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100752 let v:swapchoice = "e"
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200753 augroup test_autocmd_sessionload
754 autocmd!
755 autocmd SessionLoadPost * exe bufnr("Xsomething") . "bw!"
756 augroup END
757
758 func WriteErrors()
759 call writefile([execute("messages")], "Xerrors")
760 endfunc
761 au VimLeave * call WriteErrors()
762 [CODE]
763
Bram Moolenaare1f3ab72022-09-04 21:29:08 +0100764 call writefile(content, 'Xvimrc', 'D')
Bram Moolenaar93344c22019-08-14 21:12:05 +0200765 call system(GetVimCommand('Xvimrc') .. ' --not-a-term --noplugins -S Session.vim -c cq')
K.Takata0500e872022-09-08 12:28:02 +0100766 sleep 50m
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
Bram Moolenaar6f14da12022-09-07 21:30:44 +01001905 call mkdir('Xbufwritecmd', 'D')
zeertzjq9c8f9462022-08-30 18:17:15 +01001906 write
1907 " BufWriteCmd should be triggered even if a directory has the same name
1908 call assert_equal(1, g:written)
zeertzjq9c8f9462022-08-30 18:17:15 +01001909 unlet g:written
1910 au! BufWriteCmd
1911 bwipe!
1912endfunc
1913
Bram Moolenaaraace2152017-11-05 16:23:10 +01001914func SetChangeMarks(start, end)
Bram Moolenaar97c69432021-01-15 16:45:21 +01001915 exe a:start .. 'mark ['
1916 exe a:end .. 'mark ]'
Bram Moolenaaraace2152017-11-05 16:23:10 +01001917endfunc
1918
1919" Verify the effects of autocmds on '[ and ']
1920func Test_change_mark_in_autocmds()
1921 edit! Xtest
Bram Moolenaar97c69432021-01-15 16:45:21 +01001922 call feedkeys("ia\<CR>b\<CR>c\<CR>d\<C-g>u\<Esc>", 'xtn')
Bram Moolenaaraace2152017-11-05 16:23:10 +01001923
1924 call SetChangeMarks(2, 3)
1925 write
1926 call assert_equal([1, 4], [line("'["), line("']")])
1927
1928 call SetChangeMarks(2, 3)
1929 au BufWritePre * call assert_equal([1, 4], [line("'["), line("']")])
1930 write
1931 au! BufWritePre
1932
Bram Moolenaar14ddd222020-08-05 12:02:40 +02001933 if has('unix')
Bram Moolenaaraace2152017-11-05 16:23:10 +01001934 write XtestFilter
1935 write >> XtestFilter
1936
1937 call SetChangeMarks(2, 3)
1938 " Marks are set to the entire range of the write
1939 au FilterWritePre * call assert_equal([1, 4], [line("'["), line("']")])
1940 " '[ is adjusted to just before the line that will receive the filtered
1941 " data
1942 au FilterReadPre * call assert_equal([4, 4], [line("'["), line("']")])
1943 " The filtered data is read into the buffer, and the source lines are
1944 " still present, so the range is after the source lines
1945 au FilterReadPost * call assert_equal([5, 12], [line("'["), line("']")])
1946 %!cat XtestFilter
1947 " After the filtered data is read, the original lines are deleted
1948 call assert_equal([1, 8], [line("'["), line("']")])
1949 au! FilterWritePre,FilterReadPre,FilterReadPost
1950 undo
1951
1952 call SetChangeMarks(1, 4)
1953 au FilterWritePre * call assert_equal([2, 3], [line("'["), line("']")])
1954 au FilterReadPre * call assert_equal([3, 3], [line("'["), line("']")])
1955 au FilterReadPost * call assert_equal([4, 11], [line("'["), line("']")])
1956 2,3!cat XtestFilter
1957 call assert_equal([2, 9], [line("'["), line("']")])
1958 au! FilterWritePre,FilterReadPre,FilterReadPost
1959 undo
1960
1961 call delete('XtestFilter')
1962 endif
1963
1964 call SetChangeMarks(1, 4)
1965 au FileWritePre * call assert_equal([2, 3], [line("'["), line("']")])
1966 2,3write Xtest2
1967 au! FileWritePre
1968
1969 call SetChangeMarks(2, 3)
1970 au FileAppendPre * call assert_equal([1, 4], [line("'["), line("']")])
1971 write >> Xtest2
1972 au! FileAppendPre
1973
1974 call SetChangeMarks(1, 4)
1975 au FileAppendPre * call assert_equal([2, 3], [line("'["), line("']")])
1976 2,3write >> Xtest2
1977 au! FileAppendPre
1978
1979 call SetChangeMarks(1, 1)
1980 au FileReadPre * call assert_equal([3, 1], [line("'["), line("']")])
1981 au FileReadPost * call assert_equal([4, 11], [line("'["), line("']")])
1982 3read Xtest2
1983 au! FileReadPre,FileReadPost
1984 undo
1985
1986 call SetChangeMarks(4, 4)
1987 " When the line is 0, it's adjusted to 1
1988 au FileReadPre * call assert_equal([1, 4], [line("'["), line("']")])
1989 au FileReadPost * call assert_equal([1, 8], [line("'["), line("']")])
1990 0read Xtest2
1991 au! FileReadPre,FileReadPost
1992 undo
1993
1994 call SetChangeMarks(4, 4)
1995 " When the line is 0, it's adjusted to 1
1996 au FileReadPre * call assert_equal([1, 4], [line("'["), line("']")])
1997 au FileReadPost * call assert_equal([2, 9], [line("'["), line("']")])
1998 1read Xtest2
1999 au! FileReadPre,FileReadPost
2000 undo
2001
2002 bwipe!
2003 call delete('Xtest')
2004 call delete('Xtest2')
2005endfunc
2006
2007func Test_Filter_noshelltemp()
Bram Moolenaaraeb313f2020-11-27 19:13:28 +01002008 CheckExecutable cat
Bram Moolenaaraace2152017-11-05 16:23:10 +01002009
2010 enew!
2011 call setline(1, ['a', 'b', 'c', 'd'])
2012
2013 let shelltemp = &shelltemp
2014 set shelltemp
2015
2016 let g:filter_au = 0
2017 au FilterWritePre * let g:filter_au += 1
2018 au FilterReadPre * let g:filter_au += 1
2019 au FilterReadPost * let g:filter_au += 1
2020 %!cat
2021 call assert_equal(3, g:filter_au)
2022
2023 if has('filterpipe')
2024 set noshelltemp
2025
2026 let g:filter_au = 0
2027 au FilterWritePre * let g:filter_au += 1
2028 au FilterReadPre * let g:filter_au += 1
2029 au FilterReadPost * let g:filter_au += 1
2030 %!cat
2031 call assert_equal(0, g:filter_au)
2032 endif
2033
2034 au! FilterWritePre,FilterReadPre,FilterReadPost
2035 let &shelltemp = shelltemp
2036 bwipe!
2037endfunc
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01002038
2039func Test_TextYankPost()
2040 enew!
2041 call setline(1, ['foo'])
2042
2043 let g:event = []
2044 au TextYankPost * let g:event = copy(v:event)
2045
2046 call assert_equal({}, v:event)
2047 call assert_fails('let v:event = {}', 'E46:')
2048 call assert_fails('let v:event.mykey = 0', 'E742:')
2049
2050 norm "ayiw
2051 call assert_equal(
Bram Moolenaara016eeb2022-04-09 11:37:38 +01002052 \ #{regcontents: ['foo'], regname: 'a', operator: 'y',
2053 \ regtype: 'v', visual: v:false, inclusive: v:true},
2054 \ g:event)
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01002055 norm y_
2056 call assert_equal(
Bram Moolenaara016eeb2022-04-09 11:37:38 +01002057 \ #{regcontents: ['foo'], regname: '', operator: 'y', regtype: 'V',
2058 \ visual: v:false, inclusive: v:false},
2059 \ g:event)
Bram Moolenaar37d16732020-06-12 22:09:01 +02002060 norm Vy
2061 call assert_equal(
Bram Moolenaara016eeb2022-04-09 11:37:38 +01002062 \ #{regcontents: ['foo'], regname: '', operator: 'y', regtype: 'V',
2063 \ visual: v:true, inclusive: v:true},
2064 \ g:event)
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01002065 call feedkeys("\<C-V>y", 'x')
2066 call assert_equal(
Bram Moolenaara016eeb2022-04-09 11:37:38 +01002067 \ #{regcontents: ['f'], regname: '', operator: 'y', regtype: "\x161",
2068 \ visual: v:true, inclusive: v:true},
2069 \ g:event)
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01002070 norm "xciwbar
2071 call assert_equal(
Bram Moolenaara016eeb2022-04-09 11:37:38 +01002072 \ #{regcontents: ['foo'], regname: 'x', operator: 'c', regtype: 'v',
2073 \ visual: v:false, inclusive: v:true},
2074 \ g:event)
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01002075 norm "bdiw
2076 call assert_equal(
Bram Moolenaara016eeb2022-04-09 11:37:38 +01002077 \ #{regcontents: ['bar'], regname: 'b', operator: 'd', regtype: 'v',
2078 \ visual: v:false, inclusive: v:true},
2079 \ g:event)
2080
2081 call setline(1, 'foobar')
2082 " exclusive motion
2083 norm $"ay0
2084 call assert_equal(
2085 \ #{regcontents: ['fooba'], regname: 'a', operator: 'y', regtype: 'v',
2086 \ visual: v:false, inclusive: v:false},
2087 \ g:event)
2088 " inclusive motion
2089 norm 0"ay$
2090 call assert_equal(
2091 \ #{regcontents: ['foobar'], regname: 'a', operator: 'y', regtype: 'v',
2092 \ visual: v:false, inclusive: v:true},
2093 \ g:event)
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01002094
2095 call assert_equal({}, v:event)
2096
Bram Moolenaarfccbf062020-11-26 20:34:00 +01002097 if has('clipboard_working') && !has('gui_running')
2098 " Test that when the visual selection is automatically copied to clipboard
2099 " register a TextYankPost is emitted
2100 call setline(1, ['foobar'])
2101
2102 let @* = ''
2103 set clipboard=autoselect
2104 exe "norm! ggviw\<Esc>"
2105 call assert_equal(
Bram Moolenaara016eeb2022-04-09 11:37:38 +01002106 \ #{regcontents: ['foobar'], regname: '*', operator: 'y',
2107 \ regtype: 'v', visual: v:true, inclusive: v:false},
2108 \ g:event)
Bram Moolenaarfccbf062020-11-26 20:34:00 +01002109
2110 let @+ = ''
2111 set clipboard=autoselectplus
2112 exe "norm! ggviw\<Esc>"
2113 call assert_equal(
Bram Moolenaara016eeb2022-04-09 11:37:38 +01002114 \ #{regcontents: ['foobar'], regname: '+', operator: 'y',
2115 \ regtype: 'v', visual: v:true, inclusive: v:false},
2116 \ g:event)
Bram Moolenaarfccbf062020-11-26 20:34:00 +01002117
2118 set clipboard&vim
2119 endif
2120
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01002121 au! TextYankPost
2122 unlet g:event
2123 bwipe!
2124endfunc
Bram Moolenaar9bca8052017-12-18 12:37:55 +01002125
Bram Moolenaar9a046fd2021-01-28 13:47:59 +01002126func Test_autocommand_all_events()
2127 call assert_fails('au * * bwipe', 'E1155:')
2128 call assert_fails('au * x bwipe', 'E1155:')
Bram Moolenaarb6db1462021-12-24 19:24:47 +00002129 call assert_fails('au! * x bwipe', 'E1155:')
Bram Moolenaar4fb921e2017-12-18 15:33:00 +01002130endfunc
Bram Moolenaarb7407d32018-02-03 17:36:27 +01002131
Bram Moolenaarf6246f52022-02-11 16:30:12 +00002132func Test_autocmd_user()
2133 au User MyEvent let s:res = [expand("<afile>"), expand("<amatch>")]
2134 doautocmd User MyEvent
2135 call assert_equal(['MyEvent', 'MyEvent'], s:res)
2136 au! User
2137 unlet s:res
2138endfunc
2139
Bram Moolenaarb7407d32018-02-03 17:36:27 +01002140function s:Before_test_dirchanged()
2141 augroup test_dirchanged
2142 autocmd!
2143 augroup END
2144 let s:li = []
2145 let s:dir_this = getcwd()
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02002146 let s:dir_foo = s:dir_this . '/Xfoo'
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01002147 call mkdir(s:dir_foo)
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02002148 let s:dir_bar = s:dir_this . '/Xbar'
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01002149 call mkdir(s:dir_bar)
Bram Moolenaarb7407d32018-02-03 17:36:27 +01002150endfunc
2151
2152function s:After_test_dirchanged()
Bram Moolenaar3503d7c2019-11-09 20:10:17 +01002153 call chdir(s:dir_this)
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01002154 call delete(s:dir_foo, 'd')
2155 call delete(s:dir_bar, 'd')
Bram Moolenaarb7407d32018-02-03 17:36:27 +01002156 augroup test_dirchanged
2157 autocmd!
2158 augroup END
2159endfunc
2160
2161function Test_dirchanged_global()
2162 call s:Before_test_dirchanged()
Bram Moolenaarf6246f52022-02-11 16:30:12 +00002163 autocmd test_dirchanged DirChangedPre global call add(s:li, expand("<amatch>") .. " pre cd " .. v:event.directory)
Bram Moolenaarb7407d32018-02-03 17:36:27 +01002164 autocmd test_dirchanged DirChanged global call add(s:li, "cd:")
2165 autocmd test_dirchanged DirChanged global call add(s:li, expand("<afile>"))
Bram Moolenaar3503d7c2019-11-09 20:10:17 +01002166 call chdir(s:dir_foo)
Bram Moolenaarf6246f52022-02-11 16:30:12 +00002167 let expected = ["global pre cd " .. s:dir_foo, "cd:", s:dir_foo]
Bram Moolenaar28e8f732022-02-09 12:58:20 +00002168 call assert_equal(expected, s:li)
Bram Moolenaar3503d7c2019-11-09 20:10:17 +01002169 call chdir(s:dir_foo)
Bram Moolenaar28e8f732022-02-09 12:58:20 +00002170 call assert_equal(expected, s:li)
Bram Moolenaar3503d7c2019-11-09 20:10:17 +01002171 exe 'lcd ' .. fnameescape(s:dir_bar)
Bram Moolenaar28e8f732022-02-09 12:58:20 +00002172 call assert_equal(expected, s:li)
Bram Moolenaard8c9d322022-06-12 11:49:16 +01002173
2174 exe 'cd ' .. s:dir_foo
2175 exe 'cd ' .. s:dir_bar
2176 autocmd! test_dirchanged DirChanged global let g:result = expand("<afile>")
2177 cd -
Bram Moolenaardb77c492022-06-12 23:26:50 +01002178 call assert_equal(s:dir_foo, substitute(g:result, '\\', '/', 'g'))
Bram Moolenaard8c9d322022-06-12 11:49:16 +01002179
Bram Moolenaarb7407d32018-02-03 17:36:27 +01002180 call s:After_test_dirchanged()
2181endfunc
2182
2183function Test_dirchanged_local()
2184 call s:Before_test_dirchanged()
2185 autocmd test_dirchanged DirChanged window call add(s:li, "lcd:")
2186 autocmd test_dirchanged DirChanged window call add(s:li, expand("<afile>"))
Bram Moolenaar3503d7c2019-11-09 20:10:17 +01002187 call chdir(s:dir_foo)
Bram Moolenaarb7407d32018-02-03 17:36:27 +01002188 call assert_equal([], s:li)
Bram Moolenaar3503d7c2019-11-09 20:10:17 +01002189 exe 'lcd ' .. fnameescape(s:dir_bar)
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01002190 call assert_equal(["lcd:", s:dir_bar], s:li)
Bram Moolenaar3503d7c2019-11-09 20:10:17 +01002191 exe 'lcd ' .. fnameescape(s:dir_bar)
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01002192 call assert_equal(["lcd:", s:dir_bar], s:li)
Bram Moolenaarb7407d32018-02-03 17:36:27 +01002193 call s:After_test_dirchanged()
2194endfunc
2195
2196function Test_dirchanged_auto()
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +02002197 CheckOption autochdir
Bram Moolenaarb7407d32018-02-03 17:36:27 +01002198 call s:Before_test_dirchanged()
2199 call test_autochdir()
Bram Moolenaar28e8f732022-02-09 12:58:20 +00002200 autocmd test_dirchanged DirChangedPre auto call add(s:li, "pre cd " .. v:event.directory)
Bram Moolenaarb7407d32018-02-03 17:36:27 +01002201 autocmd test_dirchanged DirChanged auto call add(s:li, "auto:")
2202 autocmd test_dirchanged DirChanged auto call add(s:li, expand("<afile>"))
2203 set acd
Bram Moolenaar3503d7c2019-11-09 20:10:17 +01002204 cd ..
Bram Moolenaarb7407d32018-02-03 17:36:27 +01002205 call assert_equal([], s:li)
Bram Moolenaar61abe7d2022-08-30 21:46:08 +01002206 exe 'edit ' . s:dir_foo . '/Xautofile'
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01002207 call assert_equal(s:dir_foo, getcwd())
Bram Moolenaar28e8f732022-02-09 12:58:20 +00002208 let expected = ["pre cd " .. s:dir_foo, "auto:", s:dir_foo]
2209 call assert_equal(expected, s:li)
Bram Moolenaarb7407d32018-02-03 17:36:27 +01002210 set noacd
2211 bwipe!
2212 call s:After_test_dirchanged()
2213endfunc
Bram Moolenaar5a093432018-02-10 18:15:19 +01002214
2215" Test TextChangedI and TextChangedP
2216func Test_ChangedP()
2217 new
2218 call setline(1, ['foo', 'bar', 'foobar'])
2219 call test_override("char_avail", 1)
2220 set complete=. completeopt=menuone
2221
2222 func! TextChangedAutocmd(char)
2223 let g:autocmd .= a:char
2224 endfunc
2225
Christian Brabandtdb3b4462021-10-16 11:58:55 +01002226 " TextChanged will not be triggered, only check that it isn't.
Bram Moolenaar5a093432018-02-10 18:15:19 +01002227 au! TextChanged <buffer> :call TextChangedAutocmd('N')
2228 au! TextChangedI <buffer> :call TextChangedAutocmd('I')
2229 au! TextChangedP <buffer> :call TextChangedAutocmd('P')
2230
2231 call cursor(3, 1)
2232 let g:autocmd = ''
2233 call feedkeys("o\<esc>", 'tnix')
2234 call assert_equal('I', g:autocmd)
2235
2236 let g:autocmd = ''
2237 call feedkeys("Sf", 'tnix')
2238 call assert_equal('II', g:autocmd)
2239
2240 let g:autocmd = ''
2241 call feedkeys("Sf\<C-N>", 'tnix')
2242 call assert_equal('IIP', g:autocmd)
2243
2244 let g:autocmd = ''
2245 call feedkeys("Sf\<C-N>\<C-N>", 'tnix')
2246 call assert_equal('IIPP', g:autocmd)
2247
2248 let g:autocmd = ''
2249 call feedkeys("Sf\<C-N>\<C-N>\<C-N>", 'tnix')
2250 call assert_equal('IIPPP', g:autocmd)
2251
2252 let g:autocmd = ''
2253 call feedkeys("Sf\<C-N>\<C-N>\<C-N>\<C-N>", 'tnix')
2254 call assert_equal('IIPPPP', g:autocmd)
2255
2256 call assert_equal(['foo', 'bar', 'foobar', 'foo'], getline(1, '$'))
2257 " TODO: how should it handle completeopt=noinsert,noselect?
2258
2259 " CleanUp
2260 call test_override("char_avail", 0)
2261 au! TextChanged
2262 au! TextChangedI
2263 au! TextChangedP
2264 delfu TextChangedAutocmd
2265 unlet! g:autocmd
2266 set complete&vim completeopt&vim
2267
2268 bw!
2269endfunc
Bram Moolenaar8c64a362018-03-23 22:39:31 +01002270
Bram Moolenaar91d2e782018-08-07 19:05:01 +02002271let g:setline_handled = v:false
Bram Moolenaar1e115362019-01-09 23:01:02 +01002272func SetLineOne()
Bram Moolenaar91d2e782018-08-07 19:05:01 +02002273 if !g:setline_handled
2274 call setline(1, "(x)")
2275 let g:setline_handled = v:true
2276 endif
2277endfunc
2278
2279func Test_TextChangedI_with_setline()
2280 new
2281 call test_override('char_avail', 1)
2282 autocmd TextChangedI <buffer> call SetLineOne()
2283 call feedkeys("i(\<CR>\<Esc>", 'tx')
2284 call assert_equal('(', getline(1))
2285 call assert_equal('x)', getline(2))
2286 undo
Bram Moolenaar91d2e782018-08-07 19:05:01 +02002287 call assert_equal('', getline(1))
Bram Moolenaar9fa95062018-08-08 22:08:32 +02002288 call assert_equal('', getline(2))
Bram Moolenaar91d2e782018-08-07 19:05:01 +02002289
Bram Moolenaarca34db32022-01-20 11:17:18 +00002290 call test_override('char_avail', 0)
Bram Moolenaar91d2e782018-08-07 19:05:01 +02002291 bwipe!
2292endfunc
2293
Bram Moolenaar8c64a362018-03-23 22:39:31 +01002294func Test_Changed_FirstTime()
Bram Moolenaar8c5a2782019-08-07 23:07:07 +02002295 CheckFeature terminal
2296 CheckNotGui
Bram Moolenaar3cdcb092020-03-18 19:18:10 +01002297 " Starting a terminal to run Vim is always considered flaky.
Bram Moolenaar30d53e22020-03-18 21:10:44 +01002298 let g:test_is_flaky = 1
Bram Moolenaar8c5a2782019-08-07 23:07:07 +02002299
Bram Moolenaar8c64a362018-03-23 22:39:31 +01002300 " Prepare file for TextChanged event.
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01002301 call writefile([''], 'Xchanged.txt', 'D')
Bram Moolenaar8c64a362018-03-23 22:39:31 +01002302 let buf = term_start([GetVimProg(), '--clean', '-c', 'set noswapfile'], {'term_rows': 3})
2303 call assert_equal('running', term_getstatus(buf))
Bram Moolenaar1834d372018-03-29 17:40:46 +02002304 " Wait for the ruler (in the status line) to be shown.
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01002305 " In ConPTY, there is additional character which is drawn up to the width of
2306 " the screen.
2307 if has('conpty')
2308 call WaitForAssert({-> assert_match('\<All.*$', term_getline(buf, 3))})
2309 else
2310 call WaitForAssert({-> assert_match('\<All$', term_getline(buf, 3))})
2311 endif
Bram Moolenaar8c64a362018-03-23 22:39:31 +01002312 " It's only adding autocmd, so that no event occurs.
2313 call term_sendkeys(buf, ":au! TextChanged <buffer> call writefile(['No'], 'Xchanged.txt')\<cr>")
2314 call term_sendkeys(buf, "\<C-\\>\<C-N>:qa!\<cr>")
Bram Moolenaar50182fa2018-04-28 21:34:40 +02002315 call WaitForAssert({-> assert_equal('finished', term_getstatus(buf))})
Bram Moolenaar8c64a362018-03-23 22:39:31 +01002316 call assert_equal([''], readfile('Xchanged.txt'))
2317
2318 " clean up
Bram Moolenaar8c64a362018-03-23 22:39:31 +01002319 bwipe!
2320endfunc
Bram Moolenaar0566e892019-01-24 19:37:40 +01002321
Bram Moolenaareb93f3f2019-04-04 15:04:56 +02002322func Test_autocmd_nested()
2323 let g:did_nested = 0
2324 augroup Testing
2325 au WinNew * edit somefile
2326 au BufNew * let g:did_nested = 1
2327 augroup END
2328 split
2329 call assert_equal(0, g:did_nested)
2330 close
2331 bwipe! somefile
2332
2333 " old nested argument still works
2334 augroup Testing
2335 au!
2336 au WinNew * nested edit somefile
2337 au BufNew * let g:did_nested = 1
2338 augroup END
2339 split
2340 call assert_equal(1, g:did_nested)
2341 close
2342 bwipe! somefile
2343
2344 " New ++nested argument works
2345 augroup Testing
2346 au!
2347 au WinNew * ++nested edit somefile
2348 au BufNew * let g:did_nested = 1
2349 augroup END
2350 split
2351 call assert_equal(1, g:did_nested)
2352 close
2353 bwipe! somefile
2354
Bram Moolenaarf0775142022-03-04 20:10:38 +00002355 " nested without ++ does not work in Vim9 script
2356 call assert_fails('vim9cmd au WinNew * nested echo fails', 'E1078:')
2357
Bram Moolenaareb93f3f2019-04-04 15:04:56 +02002358 augroup Testing
2359 au!
2360 augroup END
2361
2362 call assert_fails('au WinNew * ++nested ++nested echo bad', 'E983:')
2363 call assert_fails('au WinNew * nested nested echo bad', 'E983:')
2364endfunc
2365
Bram Moolenaar5fa9f232022-07-23 09:06:48 +01002366func Test_autocmd_nested_cursor_invalid()
2367 set laststatus=0
2368 copen
2369 cclose
2370 call setline(1, ['foo', 'bar', 'baz'])
2371 3
2372 augroup nested_inv
2373 autocmd User foo ++nested copen
2374 autocmd BufAdd * let &laststatus = 2 - &laststatus
2375 augroup END
2376 doautocmd User foo
2377
2378 augroup nested_inv
2379 au!
2380 augroup END
2381 set laststatus&
Bram Moolenaarb03950f2022-07-26 13:47:13 +01002382 cclose
Bram Moolenaar5fa9f232022-07-23 09:06:48 +01002383 bwipe!
2384endfunc
2385
Bram Moolenaar3d6ee8b2022-07-27 15:23:35 +01002386func Test_autocmd_nested_keeps_cursor_pos()
2387 enew
2388 call setline(1, 'foo')
2389 autocmd User foo ++nested normal! $a
2390 autocmd InsertLeave * :
2391 doautocmd User foo
2392 call assert_equal([0, 1, 3, 0], getpos('.'))
2393
2394 bwipe!
2395endfunc
2396
Bram Moolenaarb03950f2022-07-26 13:47:13 +01002397func Test_autocmd_nested_switch_window()
2398 " run this in a separate Vim so that SafeState works
2399 CheckRunVimInTerminal
2400
2401 let lines =<< trim END
2402 vim9script
2403 ['()']->writefile('Xautofile')
2404 autocmd VimEnter * ++nested edit Xautofile | split
2405 autocmd BufReadPost * autocmd SafeState * ++once foldclosed('.')
2406 autocmd WinEnter * matchadd('ErrorMsg', 'pat')
2407 END
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01002408 call writefile(lines, 'Xautoscript', 'D')
Bram Moolenaarb03950f2022-07-26 13:47:13 +01002409 let buf = RunVimInTerminal('-S Xautoscript', {'rows': 10})
2410 call VerifyScreenDump(buf, 'Test_autocmd_nested_switch', {})
2411
2412 call StopVimInTerminal(buf)
2413 call delete('Xautofile')
Bram Moolenaarb03950f2022-07-26 13:47:13 +01002414endfunc
2415
Bram Moolenaareb93f3f2019-04-04 15:04:56 +02002416func Test_autocmd_once()
2417 " Without ++once WinNew triggers twice
2418 let g:did_split = 0
2419 augroup Testing
2420 au WinNew * let g:did_split += 1
2421 augroup END
2422 split
2423 split
2424 call assert_equal(2, g:did_split)
2425 call assert_true(exists('#WinNew'))
2426 close
2427 close
2428
2429 " With ++once WinNew triggers once
2430 let g:did_split = 0
2431 augroup Testing
2432 au!
2433 au WinNew * ++once let g:did_split += 1
2434 augroup END
2435 split
2436 split
2437 call assert_equal(1, g:did_split)
2438 call assert_false(exists('#WinNew'))
2439 close
2440 close
2441
2442 call assert_fails('au WinNew * ++once ++once echo bad', 'E983:')
2443endfunc
2444
Bram Moolenaara68e5952019-04-25 22:22:01 +02002445func Test_autocmd_bufreadpre()
2446 new
2447 let b:bufreadpre = 1
Bram Moolenaarab505b12020-03-23 19:28:44 +01002448 call append(0, range(1000))
Bram Moolenaara68e5952019-04-25 22:22:01 +02002449 w! XAutocmdBufReadPre.txt
2450 autocmd BufReadPre <buffer> :let b:bufreadpre += 1
Bram Moolenaarab505b12020-03-23 19:28:44 +01002451 norm! 500gg
Bram Moolenaara68e5952019-04-25 22:22:01 +02002452 sp
Bram Moolenaarab505b12020-03-23 19:28:44 +01002453 norm! 1000gg
Bram Moolenaara68e5952019-04-25 22:22:01 +02002454 wincmd p
2455 let g:wsv1 = winsaveview()
2456 wincmd p
2457 let g:wsv2 = winsaveview()
2458 " triggers BufReadPre, should not move the cursor in either window
2459 " The topline may change one line in a large window.
2460 edit
2461 call assert_inrange(g:wsv2.topline - 1, g:wsv2.topline + 1, winsaveview().topline)
2462 call assert_equal(g:wsv2.lnum, winsaveview().lnum)
2463 call assert_equal(2, b:bufreadpre)
2464 wincmd p
2465 call assert_equal(g:wsv1.topline, winsaveview().topline)
2466 call assert_equal(g:wsv1.lnum, winsaveview().lnum)
2467 call assert_equal(2, b:bufreadpre)
2468 " Now set the cursor position in an BufReadPre autocommand
2469 " (even though the position will be invalid, this should make Vim reset the
2470 " cursor position in the other window.
2471 wincmd p
2472 set cpo+=g
2473 " won't do anything, but try to set the cursor on an invalid lnum
2474 autocmd BufReadPre <buffer> :norm! 70gg
2475 " triggers BufReadPre, should not move the cursor in either window
2476 e
2477 call assert_equal(1, winsaveview().topline)
2478 call assert_equal(1, winsaveview().lnum)
2479 call assert_equal(3, b:bufreadpre)
2480 wincmd p
2481 call assert_equal(g:wsv1.topline, winsaveview().topline)
2482 call assert_equal(g:wsv1.lnum, winsaveview().lnum)
2483 call assert_equal(3, b:bufreadpre)
2484 close
2485 close
2486 call delete('XAutocmdBufReadPre.txt')
2487 set cpo-=g
2488endfunc
2489
Bram Moolenaar5e66b422019-01-24 21:58:10 +01002490" FileChangedShell tested in test_filechanged.vim
Bram Moolenaar69ea5872019-04-25 20:29:00 +02002491
2492" Tests for the following autocommands:
2493" - FileWritePre writing a compressed file
2494" - FileReadPost reading a compressed file
2495" - BufNewFile reading a file template
2496" - BufReadPre decompressing the file to be read
2497" - FilterReadPre substituting characters in the temp file
2498" - FilterReadPost substituting characters after filtering
2499" - FileReadPre set options for decompression
2500" - FileReadPost decompress the file
2501func Test_ReadWrite_Autocmds()
2502 " Run this test only on Unix-like systems and if gzip is available
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +02002503 CheckUnix
2504 CheckExecutable gzip
Bram Moolenaar69ea5872019-04-25 20:29:00 +02002505
2506 " Make $GZIP empty, "-v" would cause trouble.
2507 let $GZIP = ""
2508
2509 " Use a FileChangedShell autocommand to avoid a prompt for 'Xtestfile.gz'
2510 " being modified outside of Vim (noticed on Solaris).
2511 au FileChangedShell * echo 'caught FileChangedShell'
2512
2513 " Test for the FileReadPost, FileWritePre and FileWritePost autocmds
2514 augroup Test1
2515 au!
2516 au FileWritePre *.gz '[,']!gzip
2517 au FileWritePost *.gz undo
2518 au FileReadPost *.gz '[,']!gzip -d
2519 augroup END
2520
2521 new
2522 set bin
2523 call append(0, [
2524 \ 'line 2 Abcdefghijklmnopqrstuvwxyz',
2525 \ 'line 3 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2526 \ 'line 4 Abcdefghijklmnopqrstuvwxyz',
2527 \ 'line 5 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2528 \ 'line 6 Abcdefghijklmnopqrstuvwxyz',
2529 \ 'line 7 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2530 \ 'line 8 Abcdefghijklmnopqrstuvwxyz',
2531 \ 'line 9 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2532 \ 'line 10 Abcdefghijklmnopqrstuvwxyz'
2533 \ ])
2534 1,9write! Xtestfile.gz
2535 enew! | close
2536
2537 new
2538 " Read and decompress the testfile
2539 0read Xtestfile.gz
2540 call assert_equal([
2541 \ 'line 2 Abcdefghijklmnopqrstuvwxyz',
2542 \ 'line 3 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2543 \ 'line 4 Abcdefghijklmnopqrstuvwxyz',
2544 \ 'line 5 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2545 \ 'line 6 Abcdefghijklmnopqrstuvwxyz',
2546 \ 'line 7 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2547 \ 'line 8 Abcdefghijklmnopqrstuvwxyz',
2548 \ 'line 9 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2549 \ 'line 10 Abcdefghijklmnopqrstuvwxyz'
2550 \ ], getline(1, 9))
2551 enew! | close
2552
2553 augroup Test1
2554 au!
2555 augroup END
2556
2557 " Test for the FileAppendPre and FileAppendPost autocmds
2558 augroup Test2
2559 au!
2560 au BufNewFile *.c read Xtest.c
2561 au FileAppendPre *.out '[,']s/new/NEW/
2562 au FileAppendPost *.out !cat Xtest.c >> test.out
2563 augroup END
2564
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01002565 call writefile(['/*', ' * Here is a new .c file', ' */'], 'Xtest.c', 'D')
Bram Moolenaar69ea5872019-04-25 20:29:00 +02002566 new foo.c " should load Xtest.c
2567 call assert_equal(['/*', ' * Here is a new .c file', ' */'], getline(2, 4))
2568 w! >> test.out " append it to the output file
2569
2570 let contents = readfile('test.out')
2571 call assert_equal(' * Here is a NEW .c file', contents[2])
2572 call assert_equal(' * Here is a new .c file', contents[5])
2573
2574 call delete('test.out')
2575 enew! | close
2576 augroup Test2
2577 au!
2578 augroup END
2579
2580 " Test for the BufReadPre and BufReadPost autocmds
2581 augroup Test3
2582 au!
2583 " setup autocommands to decompress before reading and re-compress
2584 " afterwards
2585 au BufReadPre *.gz exe '!gzip -d ' . shellescape(expand("<afile>"))
2586 au BufReadPre *.gz call rename(expand("<afile>:r"), expand("<afile>"))
2587 au BufReadPost *.gz call rename(expand("<afile>"), expand("<afile>:r"))
2588 au BufReadPost *.gz exe '!gzip ' . shellescape(expand("<afile>:r"))
2589 augroup END
2590
2591 e! Xtestfile.gz " Edit compressed file
2592 call assert_equal([
2593 \ 'line 2 Abcdefghijklmnopqrstuvwxyz',
2594 \ 'line 3 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2595 \ 'line 4 Abcdefghijklmnopqrstuvwxyz',
2596 \ 'line 5 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2597 \ 'line 6 Abcdefghijklmnopqrstuvwxyz',
2598 \ 'line 7 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2599 \ 'line 8 Abcdefghijklmnopqrstuvwxyz',
2600 \ 'line 9 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2601 \ 'line 10 Abcdefghijklmnopqrstuvwxyz'
2602 \ ], getline(1, 9))
2603
2604 w! >> test.out " Append it to the output file
2605
2606 augroup Test3
2607 au!
2608 augroup END
2609
2610 " Test for the FilterReadPre and FilterReadPost autocmds.
2611 set shelltemp " need temp files here
2612 augroup Test4
2613 au!
2614 au FilterReadPre *.out call rename(expand("<afile>"), expand("<afile>") . ".t")
2615 au FilterReadPre *.out exe 'silent !sed s/e/E/ ' . shellescape(expand("<afile>")) . ".t >" . shellescape(expand("<afile>"))
2616 au FilterReadPre *.out exe 'silent !rm ' . shellescape(expand("<afile>")) . '.t'
2617 au FilterReadPost *.out '[,']s/x/X/g
2618 augroup END
2619
2620 e! test.out " Edit the output file
2621 1,$!cat
2622 call assert_equal([
2623 \ 'linE 2 AbcdefghijklmnopqrstuvwXyz',
2624 \ 'linE 3 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
2625 \ 'linE 4 AbcdefghijklmnopqrstuvwXyz',
2626 \ 'linE 5 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
2627 \ 'linE 6 AbcdefghijklmnopqrstuvwXyz',
2628 \ 'linE 7 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
2629 \ 'linE 8 AbcdefghijklmnopqrstuvwXyz',
2630 \ 'linE 9 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
2631 \ 'linE 10 AbcdefghijklmnopqrstuvwXyz'
2632 \ ], getline(1, 9))
2633 call assert_equal([
2634 \ 'line 2 Abcdefghijklmnopqrstuvwxyz',
2635 \ 'line 3 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2636 \ 'line 4 Abcdefghijklmnopqrstuvwxyz',
2637 \ 'line 5 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2638 \ 'line 6 Abcdefghijklmnopqrstuvwxyz',
2639 \ 'line 7 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2640 \ 'line 8 Abcdefghijklmnopqrstuvwxyz',
2641 \ 'line 9 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2642 \ 'line 10 Abcdefghijklmnopqrstuvwxyz'
2643 \ ], readfile('test.out'))
2644
2645 augroup Test4
2646 au!
2647 augroup END
2648 set shelltemp&vim
2649
2650 " Test for the FileReadPre and FileReadPost autocmds.
2651 augroup Test5
2652 au!
2653 au FileReadPre *.gz exe 'silent !gzip -d ' . shellescape(expand("<afile>"))
2654 au FileReadPre *.gz call rename(expand("<afile>:r"), expand("<afile>"))
2655 au FileReadPost *.gz '[,']s/l/L/
2656 augroup END
2657
2658 new
2659 0r Xtestfile.gz " Read compressed file
2660 call assert_equal([
2661 \ 'Line 2 Abcdefghijklmnopqrstuvwxyz',
2662 \ 'Line 3 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2663 \ 'Line 4 Abcdefghijklmnopqrstuvwxyz',
2664 \ 'Line 5 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2665 \ 'Line 6 Abcdefghijklmnopqrstuvwxyz',
2666 \ 'Line 7 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2667 \ 'Line 8 Abcdefghijklmnopqrstuvwxyz',
2668 \ 'Line 9 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2669 \ 'Line 10 Abcdefghijklmnopqrstuvwxyz'
2670 \ ], getline(1, 9))
2671 call assert_equal([
2672 \ 'line 2 Abcdefghijklmnopqrstuvwxyz',
2673 \ 'line 3 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2674 \ 'line 4 Abcdefghijklmnopqrstuvwxyz',
2675 \ 'line 5 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2676 \ 'line 6 Abcdefghijklmnopqrstuvwxyz',
2677 \ 'line 7 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2678 \ 'line 8 Abcdefghijklmnopqrstuvwxyz',
2679 \ 'line 9 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2680 \ 'line 10 Abcdefghijklmnopqrstuvwxyz'
2681 \ ], readfile('Xtestfile.gz'))
2682
2683 augroup Test5
2684 au!
2685 augroup END
2686
2687 au! FileChangedShell
2688 call delete('Xtestfile.gz')
Bram Moolenaar69ea5872019-04-25 20:29:00 +02002689 call delete('test.out')
2690endfunc
Bram Moolenaar23b51392019-05-09 21:38:43 +02002691
2692func Test_throw_in_BufWritePre()
2693 new
2694 call setline(1, ['one', 'two', 'three'])
2695 call assert_false(filereadable('Xthefile'))
2696 augroup throwing
2697 au BufWritePre X* throw 'do not write'
2698 augroup END
2699 try
2700 w Xthefile
2701 catch
2702 let caught = 1
2703 endtry
2704 call assert_equal(1, caught)
2705 call assert_false(filereadable('Xthefile'))
2706
2707 bwipe!
2708 au! throwing
2709endfunc
Bram Moolenaarcadbe1b2019-09-22 21:50:09 +02002710
Bram Moolenaar40fa12a2021-09-22 14:18:13 +02002711func Test_autocmd_in_try_block()
Bram Moolenaar6f14da12022-09-07 21:30:44 +01002712 call mkdir('Xintrydir', 'R')
Bram Moolenaar40fa12a2021-09-22 14:18:13 +02002713 au BufEnter * let g:fname = expand('%')
2714 try
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +01002715 edit Xintrydir/
Bram Moolenaar40fa12a2021-09-22 14:18:13 +02002716 endtry
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +01002717 call assert_match('Xintrydir', g:fname)
Bram Moolenaar40fa12a2021-09-22 14:18:13 +02002718
2719 unlet g:fname
2720 au! BufEnter
Bram Moolenaar40fa12a2021-09-22 14:18:13 +02002721endfunc
2722
Bram Moolenaarcadbe1b2019-09-22 21:50:09 +02002723func Test_autocmd_SafeState()
2724 CheckRunVimInTerminal
2725
2726 let lines =<< trim END
2727 let g:safe = 0
2728 let g:again = ''
2729 au SafeState * let g:safe += 1
2730 au SafeStateAgain * let g:again ..= 'x'
2731 func CallTimer()
2732 call timer_start(10, {id -> execute('let g:again ..= "t"')})
2733 endfunc
2734 END
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01002735 call writefile(lines, 'XSafeState', 'D')
Bram Moolenaarcadbe1b2019-09-22 21:50:09 +02002736 let buf = RunVimInTerminal('-S XSafeState', #{rows: 6})
2737
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01002738 " Sometimes we loop to handle a K_IGNORE, SafeState may be triggered once or
Bram Moolenaar8fb1b472020-02-23 16:16:26 +01002739 " more often.
Bram Moolenaarcadbe1b2019-09-22 21:50:09 +02002740 call term_sendkeys(buf, ":echo g:safe\<CR>")
Bram Moolenaar8fb1b472020-02-23 16:16:26 +01002741 call WaitForAssert({-> assert_match('^\d ', term_getline(buf, 6))}, 1000)
Bram Moolenaarcadbe1b2019-09-22 21:50:09 +02002742
Bram Moolenaar8fb1b472020-02-23 16:16:26 +01002743 " SafeStateAgain should be invoked at least three times
Bram Moolenaarcadbe1b2019-09-22 21:50:09 +02002744 call term_sendkeys(buf, ":echo g:again\<CR>")
Bram Moolenaar8fb1b472020-02-23 16:16:26 +01002745 call WaitForAssert({-> assert_match('^xxx', term_getline(buf, 6))}, 1000)
Bram Moolenaarcadbe1b2019-09-22 21:50:09 +02002746
2747 call term_sendkeys(buf, ":let g:again = ''\<CR>:call CallTimer()\<CR>")
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02002748 call TermWait(buf, 50)
Bram Moolenaar0f6629a2019-09-22 23:24:13 +02002749 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02002750 call TermWait(buf, 50)
Bram Moolenaarcadbe1b2019-09-22 21:50:09 +02002751 call term_sendkeys(buf, ":echo g:again\<CR>")
2752 call WaitForAssert({-> assert_match('xtx', term_getline(buf, 6))}, 1000)
2753
2754 call StopVimInTerminal(buf)
Bram Moolenaarcadbe1b2019-09-22 21:50:09 +02002755endfunc
Bram Moolenaar23324a02019-10-01 17:39:04 +02002756
2757func Test_autocmd_CmdWinEnter()
2758 CheckRunVimInTerminal
Bram Moolenaar21829c52021-01-26 22:42:21 +01002759 CheckFeature cmdwin
2760
Bram Moolenaar23324a02019-10-01 17:39:04 +02002761 let lines =<< trim END
Egor Zvorykin125ffd22021-11-17 14:01:14 +00002762 augroup vimHints | au! | augroup END
Bram Moolenaar23324a02019-10-01 17:39:04 +02002763 let b:dummy_var = 'This is a dummy'
2764 autocmd CmdWinEnter * quit
2765 let winnr = winnr('$')
2766 END
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01002767 let filename = 'XCmdWinEnter'
Bram Moolenaar23324a02019-10-01 17:39:04 +02002768 call writefile(lines, filename)
2769 let buf = RunVimInTerminal('-S '.filename, #{rows: 6})
2770
2771 call term_sendkeys(buf, "q:")
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02002772 call TermWait(buf)
Bram Moolenaar23324a02019-10-01 17:39:04 +02002773 call term_sendkeys(buf, ":echo b:dummy_var\<cr>")
Bram Moolenaar353c3512020-03-15 14:19:26 +01002774 call WaitForAssert({-> assert_match('^This is a dummy', term_getline(buf, 6))}, 2000)
Bram Moolenaar23324a02019-10-01 17:39:04 +02002775 call term_sendkeys(buf, ":echo &buftype\<cr>")
2776 call WaitForAssert({-> assert_notmatch('^nofile', term_getline(buf, 6))}, 1000)
2777 call term_sendkeys(buf, ":echo winnr\<cr>")
2778 call WaitForAssert({-> assert_match('^1', term_getline(buf, 6))}, 1000)
2779
2780 " clean up
2781 call StopVimInTerminal(buf)
2782 call delete(filename)
2783endfunc
Bram Moolenaarec66c412019-10-11 21:19:13 +02002784
2785func Test_autocmd_was_using_freed_memory()
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01002786 CheckFeature quickfix
2787
Bram Moolenaarec66c412019-10-11 21:19:13 +02002788 pedit xx
2789 n x
Bram Moolenaar92bb83e2021-02-03 23:04:46 +01002790 augroup winenter
2791 au WinEnter * if winnr('$') > 2 | quit | endif
2792 augroup END
Bram Moolenaarec66c412019-10-11 21:19:13 +02002793 split
Bram Moolenaar92bb83e2021-02-03 23:04:46 +01002794
2795 augroup winenter
2796 au! WinEnter
2797 augroup END
2798
2799 bwipe xx
2800 bwipe x
2801 pclose
Bram Moolenaarec66c412019-10-11 21:19:13 +02002802endfunc
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +01002803
2804func Test_BufWrite_lockmarks()
Bram Moolenaarf08b0eb2021-10-16 13:00:14 +01002805 let g:test_is_flaky = 1
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +01002806 edit! Xtest
2807 call setline(1, ['a', 'b', 'c', 'd'])
2808
2809 " :lockmarks preserves the marks
2810 call SetChangeMarks(2, 3)
2811 lockmarks write
2812 call assert_equal([2, 3], [line("'["), line("']")])
2813
2814 " *WritePre autocmds get the correct line range, but lockmarks preserves the
2815 " original values for the user
2816 augroup lockmarks
2817 au!
2818 au BufWritePre,FilterWritePre * call assert_equal([1, 4], [line("'["), line("']")])
2819 au FileWritePre * call assert_equal([3, 4], [line("'["), line("']")])
2820 augroup END
2821
2822 lockmarks write
2823 call assert_equal([2, 3], [line("'["), line("']")])
2824
2825 if executable('cat')
2826 lockmarks %!cat
2827 call assert_equal([2, 3], [line("'["), line("']")])
2828 endif
2829
2830 lockmarks 3,4write Xtest2
2831 call assert_equal([2, 3], [line("'["), line("']")])
2832
2833 au! lockmarks
2834 augroup! lockmarks
2835 call delete('Xtest')
2836 call delete('Xtest2')
2837endfunc
Bram Moolenaarce6db022020-01-07 20:11:42 +01002838
2839func Test_FileType_spell()
2840 if !isdirectory('/tmp')
2841 throw "Skipped: requires /tmp directory"
2842 endif
2843
2844 " this was crashing with an invalid free()
2845 setglobal spellfile=/tmp/en.utf-8.add
2846 augroup crash
2847 autocmd!
2848 autocmd BufNewFile,BufReadPost crashfile setf somefiletype
2849 autocmd BufNewFile,BufReadPost crashfile set ft=anotherfiletype
2850 autocmd FileType anotherfiletype setlocal spell
2851 augroup END
2852 func! NoCrash() abort
2853 edit /tmp/crashfile
2854 endfunc
2855 call NoCrash()
2856
2857 au! crash
2858 setglobal spellfile=
2859endfunc
Bram Moolenaarbc2b71d2020-02-17 21:33:30 +01002860
Bram Moolenaar406cd902020-02-18 21:54:41 +01002861" Test closing a window or editing another buffer from a FileChangedRO handler
2862" in a readonly buffer
2863func Test_FileChangedRO_winclose()
Bram Moolenaar62cd26a2020-10-11 20:08:44 +02002864 call test_override('ui_delay', 10)
2865
Bram Moolenaar406cd902020-02-18 21:54:41 +01002866 augroup FileChangedROTest
2867 au!
2868 autocmd FileChangedRO * quit
2869 augroup END
2870 new
2871 set readonly
2872 call assert_fails('normal i', 'E788:')
2873 close
2874 augroup! FileChangedROTest
2875
2876 augroup FileChangedROTest
2877 au!
Bram Moolenaar61abe7d2022-08-30 21:46:08 +01002878 autocmd FileChangedRO * edit Xrofile
Bram Moolenaar406cd902020-02-18 21:54:41 +01002879 augroup END
2880 new
2881 set readonly
2882 call assert_fails('normal i', 'E788:')
2883 close
2884 augroup! FileChangedROTest
Bram Moolenaar62cd26a2020-10-11 20:08:44 +02002885 call test_override('ALL', 0)
Bram Moolenaar406cd902020-02-18 21:54:41 +01002886endfunc
2887
Bram Moolenaar0c81d1b2020-02-22 22:45:55 +01002888func LogACmd()
2889 call add(g:logged, line('$'))
2890endfunc
2891
2892func Test_TermChanged()
Bram Moolenaard28e0b32020-02-22 23:08:52 +01002893 CheckNotGui
2894
Bram Moolenaar0c81d1b2020-02-22 22:45:55 +01002895 enew!
2896 tabnew
2897 call setline(1, ['a', 'b', 'c', 'd'])
2898 $
2899 au TermChanged * call LogACmd()
2900 let g:logged = []
2901 let term_save = &term
2902 set term=xterm
2903 call assert_equal([1, 4], g:logged)
2904
2905 au! TermChanged
2906 let &term = term_save
2907 bwipe!
2908endfunc
2909
Bram Moolenaare3284872020-03-19 13:55:03 +01002910" Test for FileReadCmd autocmd
2911func Test_autocmd_FileReadCmd()
2912 func ReadFileCmd()
2913 call append(line('$'), "v:cmdarg = " .. v:cmdarg)
2914 endfunc
2915 augroup FileReadCmdTest
2916 au!
2917 au FileReadCmd Xtest call ReadFileCmd()
2918 augroup END
2919
2920 new
2921 read ++bin Xtest
2922 read ++nobin Xtest
2923 read ++edit Xtest
2924 read ++bad=keep Xtest
2925 read ++bad=drop Xtest
2926 read ++bad=- Xtest
2927 read ++ff=unix Xtest
2928 read ++ff=dos Xtest
2929 read ++ff=mac Xtest
2930 read ++enc=utf-8 Xtest
2931
2932 call assert_equal(['',
2933 \ 'v:cmdarg = ++bin',
2934 \ 'v:cmdarg = ++nobin',
2935 \ 'v:cmdarg = ++edit',
2936 \ 'v:cmdarg = ++bad=keep',
2937 \ 'v:cmdarg = ++bad=drop',
2938 \ 'v:cmdarg = ++bad=-',
2939 \ 'v:cmdarg = ++ff=unix',
2940 \ 'v:cmdarg = ++ff=dos',
2941 \ 'v:cmdarg = ++ff=mac',
2942 \ 'v:cmdarg = ++enc=utf-8'], getline(1, '$'))
2943
2944 close!
2945 augroup FileReadCmdTest
2946 au!
2947 augroup END
2948 delfunc ReadFileCmd
2949endfunc
2950
Bram Moolenaaree4e0c12020-04-06 21:35:05 +02002951" Test for passing invalid arguments to autocmd
2952func Test_autocmd_invalid_args()
2953 " Additional character after * for event
Bram Moolenaar61abe7d2022-08-30 21:46:08 +01002954 call assert_fails('autocmd *a Xinvfile set ff=unix', 'E215:')
Bram Moolenaaree4e0c12020-04-06 21:35:05 +02002955 augroup Test
2956 augroup END
2957 " Invalid autocmd event
Bram Moolenaar61abe7d2022-08-30 21:46:08 +01002958 call assert_fails('autocmd Bufabc Xinvfile set ft=vim', 'E216:')
Bram Moolenaaree4e0c12020-04-06 21:35:05 +02002959 " Invalid autocmd event in a autocmd group
Bram Moolenaar61abe7d2022-08-30 21:46:08 +01002960 call assert_fails('autocmd Test Bufabc Xinvfile set ft=vim', 'E216:')
Bram Moolenaaree4e0c12020-04-06 21:35:05 +02002961 augroup! Test
2962 " Execute all autocmds
2963 call assert_fails('doautocmd * BufEnter', 'E217:')
2964 call assert_fails('augroup! x1a2b3', 'E367:')
2965 call assert_fails('autocmd BufNew <buffer=999> pwd', 'E680:')
Bram Moolenaar531be472020-09-23 22:38:05 +02002966 call assert_fails('autocmd BufNew \) set ff=unix', 'E55:')
Bram Moolenaaree4e0c12020-04-06 21:35:05 +02002967endfunc
2968
2969" Test for deep nesting of autocmds
2970func Test_autocmd_deep_nesting()
Bram Moolenaar61abe7d2022-08-30 21:46:08 +01002971 autocmd BufEnter Xdeepfile doautocmd BufEnter Xdeepfile
2972 call assert_fails('doautocmd BufEnter Xdeepfile', 'E218:')
2973 autocmd! BufEnter Xdeepfile
Bram Moolenaaree4e0c12020-04-06 21:35:05 +02002974endfunc
2975
Bram Moolenaarbe5ee862020-06-10 20:56:58 +02002976" Tests for SigUSR1 autocmd event, which is only available on posix systems.
2977func Test_autocmd_sigusr1()
2978 CheckUnix
Bram Moolenaar62cd26a2020-10-11 20:08:44 +02002979 CheckExecutable /bin/kill
Bram Moolenaarbe5ee862020-06-10 20:56:58 +02002980
2981 let g:sigusr1_passed = 0
2982 au SigUSR1 * let g:sigusr1_passed = 1
2983 call system('/bin/kill -s usr1 ' . getpid())
2984 call WaitForAssert({-> assert_true(g:sigusr1_passed)})
2985
2986 au! SigUSR1
2987 unlet g:sigusr1_passed
2988endfunc
2989
Bram Moolenaarb340bae2020-06-15 19:51:56 +02002990" Test for BufReadPre autocmd deleting the file
2991func Test_BufReadPre_delfile()
2992 augroup TestAuCmd
2993 au!
Bram Moolenaare7cda972022-08-29 11:02:59 +01002994 autocmd BufReadPre XbufreadPre call delete('XbufreadPre')
Bram Moolenaarb340bae2020-06-15 19:51:56 +02002995 augroup END
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01002996 call writefile([], 'XbufreadPre', 'D')
Bram Moolenaare7cda972022-08-29 11:02:59 +01002997 call assert_fails('new XbufreadPre', 'E200:')
2998 call assert_equal('XbufreadPre', @%)
Bram Moolenaarb340bae2020-06-15 19:51:56 +02002999 call assert_equal(1, &readonly)
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01003000
Bram Moolenaarb340bae2020-06-15 19:51:56 +02003001 augroup TestAuCmd
3002 au!
3003 augroup END
3004 close!
3005endfunc
3006
3007" Test for BufReadPre autocmd changing the current buffer
3008func Test_BufReadPre_changebuf()
3009 augroup TestAuCmd
3010 au!
Bram Moolenaare7cda972022-08-29 11:02:59 +01003011 autocmd BufReadPre Xchangebuf edit Xsomeotherfile
Bram Moolenaarb340bae2020-06-15 19:51:56 +02003012 augroup END
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01003013 call writefile([], 'Xchangebuf', 'D')
Bram Moolenaare7cda972022-08-29 11:02:59 +01003014 call assert_fails('new Xchangebuf', 'E201:')
Bram Moolenaarb340bae2020-06-15 19:51:56 +02003015 call assert_equal('Xsomeotherfile', @%)
3016 call assert_equal(1, &readonly)
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01003017
Bram Moolenaarb340bae2020-06-15 19:51:56 +02003018 augroup TestAuCmd
3019 au!
3020 augroup END
3021 close!
3022endfunc
3023
3024" Test for BufWipeouti autocmd changing the current buffer when reading a file
3025" in an empty buffer with 'f' flag in 'cpo'
3026func Test_BufDelete_changebuf()
3027 new
3028 augroup TestAuCmd
3029 au!
3030 autocmd BufWipeout * let bufnr = bufadd('somefile') | exe "b " .. bufnr
3031 augroup END
3032 let save_cpo = &cpo
3033 set cpo+=f
Bram Moolenaarb18b4962022-09-02 21:55:50 +01003034 call assert_fails('r Xchangebuf', ['E812:', 'E484:'])
Bram Moolenaarb340bae2020-06-15 19:51:56 +02003035 call assert_equal('somefile', @%)
3036 let &cpo = save_cpo
3037 augroup TestAuCmd
3038 au!
3039 augroup END
3040 close!
3041endfunc
3042
Bram Moolenaar0fe937f2020-06-16 22:42:04 +02003043" Test for the temporary internal window used to execute autocmds
3044func Test_autocmd_window()
3045 %bw!
3046 edit one.txt
3047 tabnew two.txt
Bram Moolenaar41cd8032021-03-13 15:47:56 +01003048 vnew three.txt
3049 tabnew four.txt
3050 tabprevious
Bram Moolenaar0fe937f2020-06-16 22:42:04 +02003051 let g:blist = []
Bram Moolenaar832adf92020-06-25 19:01:36 +02003052 augroup aucmd_win_test1
Bram Moolenaar0fe937f2020-06-16 22:42:04 +02003053 au!
3054 au BufEnter * call add(g:blist, [expand('<afile>'),
3055 \ win_gettype(bufwinnr(expand('<afile>')))])
3056 augroup END
3057
3058 doautoall BufEnter
Bram Moolenaar41cd8032021-03-13 15:47:56 +01003059 call assert_equal([
3060 \ ['one.txt', 'autocmd'],
3061 \ ['two.txt', ''],
3062 \ ['four.txt', 'autocmd'],
3063 \ ['three.txt', ''],
3064 \ ], g:blist)
Bram Moolenaar0fe937f2020-06-16 22:42:04 +02003065
Bram Moolenaar832adf92020-06-25 19:01:36 +02003066 augroup aucmd_win_test1
Bram Moolenaar0fe937f2020-06-16 22:42:04 +02003067 au!
3068 augroup END
Bram Moolenaar832adf92020-06-25 19:01:36 +02003069 augroup! aucmd_win_test1
3070 %bw!
3071endfunc
3072
3073" Test for trying to close the temporary window used for executing an autocmd
3074func Test_close_autocmd_window()
3075 %bw!
3076 edit one.txt
3077 tabnew two.txt
3078 augroup aucmd_win_test2
3079 au!
3080 au BufEnter * if expand('<afile>') == 'one.txt' | 1close | endif
3081 augroup END
3082
3083 call assert_fails('doautoall BufEnter', 'E813:')
3084
3085 augroup aucmd_win_test2
3086 au!
3087 augroup END
3088 augroup! aucmd_win_test2
Bram Moolenaarcf844172020-06-26 19:44:06 +02003089 %bwipe!
3090endfunc
3091
3092" Test for trying to close the tab that has the temporary window for exeucing
3093" an autocmd.
3094func Test_close_autocmd_tab()
3095 edit one.txt
3096 tabnew two.txt
3097 augroup aucmd_win_test
3098 au!
3099 au BufEnter * if expand('<afile>') == 'one.txt' | tabfirst | tabonly | endif
3100 augroup END
3101
3102 call assert_fails('doautoall BufEnter', 'E813:')
3103
3104 tabonly
3105 augroup aucmd_win_test
3106 au!
3107 augroup END
3108 augroup! aucmd_win_test
3109 %bwipe!
Bram Moolenaar0fe937f2020-06-16 22:42:04 +02003110endfunc
3111
Bram Moolenaarcb1956d2022-01-07 15:45:18 +00003112func Test_Visual_doautoall_redraw()
3113 call setline(1, ['a', 'b'])
3114 new
3115 wincmd p
3116 call feedkeys("G\<C-V>", 'txn')
3117 autocmd User Explode ++once redraw
3118 doautoall User Explode
3119 %bwipe!
3120endfunc
3121
Bram Moolenaar6bcb8772021-02-03 21:23:29 +01003122" This was using freed memory.
3123func Test_BufNew_arglocal()
3124 arglocal
3125 au BufNew * arglocal
3126 call assert_fails('drop xx', 'E1156:')
3127
3128 au! BufNew
3129endfunc
3130
Bram Moolenaar8ab37572021-02-03 21:56:59 +01003131func Test_autocmd_closes_window()
3132 au BufNew,BufWinLeave * e %e
3133 file yyy
3134 au BufNew,BufWinLeave * ball
Bram Moolenaaraad5f9d2021-02-06 17:30:31 +01003135 n xxx
Bram Moolenaar8ab37572021-02-03 21:56:59 +01003136
Bram Moolenaaraad5f9d2021-02-06 17:30:31 +01003137 %bwipe
Bram Moolenaar8ab37572021-02-03 21:56:59 +01003138 au! BufNew
3139 au! BufWinLeave
3140endfunc
3141
Bram Moolenaar92bb83e2021-02-03 23:04:46 +01003142func Test_autocmd_quit_psearch()
3143 sn aa bb
3144 augroup aucmd_win_test
3145 au!
3146 au BufEnter,BufLeave,BufNew,WinEnter,WinLeave,WinNew * if winnr('$') > 1 | q | endif
3147 augroup END
3148 ps /
3149
3150 augroup aucmd_win_test
3151 au!
3152 augroup END
zeertzjq7851c692022-04-21 11:14:01 +01003153 new
3154 pclose
Bram Moolenaar92bb83e2021-02-03 23:04:46 +01003155endfunc
3156
Bram Moolenaaraad5f9d2021-02-06 17:30:31 +01003157" Fuzzer found some strange combination that caused a crash.
3158func Test_autocmd_normal_mess()
Bram Moolenaardd07c022021-02-07 13:32:46 +01003159 " For unknown reason this hangs on MS-Windows
3160 CheckNotMSWindows
3161
Bram Moolenaaraad5f9d2021-02-06 17:30:31 +01003162 augroup aucmd_normal_test
3163 au BufLeave,BufWinLeave,BufHidden,BufUnload,BufDelete,BufWipeout * norm 7q/qc
3164 augroup END
Bram Moolenaar983d83f2021-02-07 12:12:43 +01003165 call assert_fails('o4', 'E1159')
Bram Moolenaaraad5f9d2021-02-06 17:30:31 +01003166 silent! H
Bram Moolenaar983d83f2021-02-07 12:12:43 +01003167 call assert_fails('e xx', 'E1159')
Bram Moolenaaraad5f9d2021-02-06 17:30:31 +01003168 normal G
3169
3170 augroup aucmd_normal_test
3171 au!
3172 augroup END
3173endfunc
3174
Bram Moolenaar8c6951f2021-02-06 18:08:45 +01003175func Test_autocmd_closing_cmdwin()
Bram Moolenaardd07c022021-02-07 13:32:46 +01003176 " For unknown reason this hangs on MS-Windows
3177 CheckNotMSWindows
3178
Bram Moolenaar8c6951f2021-02-06 18:08:45 +01003179 au BufWinLeave * nested q
3180 call assert_fails("norm 7q?\n", 'E855:')
3181
3182 au! BufWinLeave
3183 new
3184 only
3185endfunc
3186
Bram Moolenaar2c7080b2021-02-06 19:19:42 +01003187func Test_autocmd_vimgrep()
3188 augroup aucmd_vimgrep
Charlie Grovesfef44852022-04-19 16:24:12 +01003189 au QuickfixCmdPre,BufNew,BufReadCmd * sb
zeertzjq7851c692022-04-21 11:14:01 +01003190 au QuickfixCmdPre,BufNew,BufReadCmd * q9
Bram Moolenaar2c7080b2021-02-06 19:19:42 +01003191 augroup END
Bram Moolenaardd07c022021-02-07 13:32:46 +01003192 call assert_fails('lv ?a? foo', 'E926:')
Bram Moolenaar2c7080b2021-02-06 19:19:42 +01003193
3194 augroup aucmd_vimgrep
3195 au!
3196 augroup END
3197endfunc
3198
Bram Moolenaar73b8b0a2021-08-01 14:52:32 +02003199func Test_autocmd_with_block()
3200 augroup block_testing
3201 au BufReadPost *.xml {
3202 setlocal matchpairs+=<:>
3203 /<start
3204 }
Bram Moolenaar63b91732021-08-05 20:40:03 +02003205 au CursorHold * {
3206 autocmd BufReadPre * ++once echo 'one' | echo 'two'
3207 g:gotSafeState = 77
3208 }
Bram Moolenaar73b8b0a2021-08-01 14:52:32 +02003209 augroup END
3210
3211 let expected = "\n--- Autocommands ---\nblock_testing BufRead\n *.xml {^@ setlocal matchpairs+=<:>^@ /<start^@ }"
3212 call assert_equal(expected, execute('au BufReadPost *.xml'))
3213
Bram Moolenaar63b91732021-08-05 20:40:03 +02003214 doautocmd CursorHold
3215 call assert_equal(77, g:gotSafeState)
3216 unlet g:gotSafeState
3217
Bram Moolenaar73b8b0a2021-08-01 14:52:32 +02003218 augroup block_testing
3219 au!
Bram Moolenaar75ebd2a2022-06-03 17:39:46 +01003220 autocmd CursorHold * {
3221 if true
3222 # comment
3223 && true
3224
3225 && true
3226 g:done = 'yes'
3227 endif
3228 }
3229 augroup END
3230 doautocmd CursorHold
3231 call assert_equal('yes', g:done)
3232
3233 unlet g:done
3234 augroup block_testing
3235 au!
Bram Moolenaar73b8b0a2021-08-01 14:52:32 +02003236 augroup END
3237endfunc
3238
Christian Brabandtdb3b4462021-10-16 11:58:55 +01003239" Test TextChangedI and TextChanged
3240func Test_Changed_ChangedI()
3241 new
3242 call test_override("char_avail", 1)
3243 let [g:autocmd_i, g:autocmd_n] = ['','']
3244
3245 func! TextChangedAutocmdI(char)
3246 let g:autocmd_{tolower(a:char)} = a:char .. b:changedtick
3247 endfunc
3248
3249 augroup Test_TextChanged
3250 au!
3251 au TextChanged <buffer> :call TextChangedAutocmdI('N')
3252 au TextChangedI <buffer> :call TextChangedAutocmdI('I')
3253 augroup END
3254
3255 call feedkeys("ifoo\<esc>", 'tnix')
3256 " TODO: Test test does not seem to trigger TextChanged autocommand, this
3257 " requires running Vim in a terminal window.
3258 " call assert_equal('N3', g:autocmd_n)
3259 call assert_equal('I3', g:autocmd_i)
3260
3261 call feedkeys("yyp", 'tnix')
3262 " TODO: Test test does not seem to trigger TextChanged autocommand.
3263 " call assert_equal('N4', g:autocmd_n)
3264 call assert_equal('I3', g:autocmd_i)
3265
3266 " CleanUp
3267 call test_override("char_avail", 0)
3268 au! TextChanged <buffer>
3269 au! TextChangedI <buffer>
3270 augroup! Test_TextChanged
3271 delfu TextChangedAutocmdI
3272 unlet! g:autocmd_i g:autocmd_n
3273
3274 bw!
3275endfunc
Bram Moolenaar2c7080b2021-02-06 19:19:42 +01003276
Bram Moolenaar6f2465d2022-03-22 18:13:01 +00003277func Test_closing_autocmd_window()
3278 let lines =<< trim END
3279 edit Xa.txt
3280 tabnew Xb.txt
3281 autocmd BufEnter Xa.txt unhide 1
3282 doautoall BufEnter
3283 END
3284 call v9.CheckScriptFailure(lines, 'E814:')
3285 au! BufEnter
3286 only!
3287 bwipe Xa.txt
3288 bwipe Xb.txt
3289endfunc
3290
Bram Moolenaar347538f2022-03-26 16:28:06 +00003291func Test_bufwipeout_changes_window()
3292 " This should not crash, but we don't have any expectations about what
3293 " happens, changing window in BufWipeout has unpredictable results.
3294 tabedit
3295 let g:window_id = win_getid()
3296 topleft new
3297 setlocal bufhidden=wipe
3298 autocmd BufWipeout <buffer> call win_gotoid(g:window_id)
3299 tabprevious
3300 +tabclose
3301
3302 unlet g:window_id
3303 au! BufWipeout
3304 %bwipe!
3305endfunc
3306
zeertzjq021996f2022-04-10 11:44:04 +01003307func Test_v_event_readonly()
3308 autocmd CompleteChanged * let v:event.width = 0
3309 call assert_fails("normal! i\<C-X>\<C-V>", 'E46:')
3310 au! CompleteChanged
3311
3312 autocmd DirChangedPre * let v:event.directory = ''
3313 call assert_fails('cd .', 'E46:')
3314 au! DirChangedPre
3315
3316 autocmd ModeChanged * let v:event.new_mode = ''
3317 call assert_fails('normal! cc', 'E46:')
3318 au! ModeChanged
3319
3320 autocmd TextYankPost * let v:event.operator = ''
3321 call assert_fails('normal! yy', 'E46:')
3322 au! TextYankPost
3323endfunc
3324
zeertzjqc9e8fd62022-07-26 18:12:38 +01003325" Test for ModeChanged pattern
3326func Test_mode_changes()
3327 let g:index = 0
3328 let g:mode_seq = ['n', 'i', 'n', 'v', 'V', 'i', 'ix', 'i', 'ic', 'i', 'n', 'no', 'n', 'V', 'v', 's', 'n']
3329 func! TestMode()
3330 call assert_equal(g:mode_seq[g:index], get(v:event, "old_mode"))
3331 call assert_equal(g:mode_seq[g:index + 1], get(v:event, "new_mode"))
3332 call assert_equal(mode(1), get(v:event, "new_mode"))
3333 let g:index += 1
3334 endfunc
3335
3336 au ModeChanged * :call TestMode()
3337 let g:n_to_any = 0
3338 au ModeChanged n:* let g:n_to_any += 1
3339 call feedkeys("i\<esc>vVca\<CR>\<C-X>\<C-L>\<esc>ggdG", 'tnix')
3340
3341 let g:V_to_v = 0
3342 au ModeChanged V:v let g:V_to_v += 1
3343 call feedkeys("Vv\<C-G>\<esc>", 'tnix')
3344 call assert_equal(len(filter(g:mode_seq[1:], {idx, val -> val == 'n'})), g:n_to_any)
3345 call assert_equal(1, g:V_to_v)
3346 call assert_equal(len(g:mode_seq) - 1, g:index)
3347
3348 let g:n_to_i = 0
3349 au ModeChanged n:i let g:n_to_i += 1
3350 let g:n_to_niI = 0
3351 au ModeChanged i:niI let g:n_to_niI += 1
3352 let g:niI_to_i = 0
3353 au ModeChanged niI:i let g:niI_to_i += 1
3354 let g:nany_to_i = 0
3355 au ModeChanged n*:i let g:nany_to_i += 1
3356 let g:i_to_n = 0
3357 au ModeChanged i:n let g:i_to_n += 1
3358 let g:nori_to_any = 0
3359 au ModeChanged [ni]:* let g:nori_to_any += 1
3360 let g:i_to_any = 0
3361 au ModeChanged i:* let g:i_to_any += 1
3362 let g:index = 0
3363 let g:mode_seq = ['n', 'i', 'niI', 'i', 'n']
3364 call feedkeys("a\<C-O>l\<esc>", 'tnix')
3365 call assert_equal(len(g:mode_seq) - 1, g:index)
3366 call assert_equal(1, g:n_to_i)
3367 call assert_equal(1, g:n_to_niI)
3368 call assert_equal(1, g:niI_to_i)
3369 call assert_equal(2, g:nany_to_i)
3370 call assert_equal(1, g:i_to_n)
3371 call assert_equal(2, g:i_to_any)
3372 call assert_equal(3, g:nori_to_any)
3373
3374 if has('terminal')
3375 let g:mode_seq += ['c', 'n', 't', 'nt', 'c', 'nt', 'n']
3376 call feedkeys(":term\<CR>\<C-W>N:bd!\<CR>", 'tnix')
3377 call assert_equal(len(g:mode_seq) - 1, g:index)
3378 call assert_equal(1, g:n_to_i)
3379 call assert_equal(1, g:n_to_niI)
3380 call assert_equal(1, g:niI_to_i)
3381 call assert_equal(2, g:nany_to_i)
3382 call assert_equal(1, g:i_to_n)
3383 call assert_equal(2, g:i_to_any)
3384 call assert_equal(5, g:nori_to_any)
3385 endif
3386
3387 if has('cmdwin')
3388 let g:n_to_c = 0
3389 au ModeChanged n:c let g:n_to_c += 1
3390 let g:c_to_n = 0
3391 au ModeChanged c:n let g:c_to_n += 1
3392 let g:mode_seq += ['c', 'n', 'c', 'n']
3393 call feedkeys("q:\<C-C>\<Esc>", 'tnix')
3394 call assert_equal(len(g:mode_seq) - 1, g:index)
3395 call assert_equal(2, g:n_to_c)
3396 call assert_equal(2, g:c_to_n)
3397 unlet g:n_to_c
3398 unlet g:c_to_n
3399 endif
3400
3401 au! ModeChanged
3402 delfunc TestMode
3403 unlet! g:mode_seq
3404 unlet! g:index
3405 unlet! g:n_to_any
3406 unlet! g:V_to_v
3407 unlet! g:n_to_i
3408 unlet! g:n_to_niI
3409 unlet! g:niI_to_i
3410 unlet! g:nany_to_i
3411 unlet! g:i_to_n
3412 unlet! g:nori_to_any
3413 unlet! g:i_to_any
3414endfunc
3415
3416func Test_recursive_ModeChanged()
3417 au! ModeChanged * norm 0u
3418 sil! norm 
3419 au! ModeChanged
3420endfunc
3421
3422func Test_ModeChanged_starts_visual()
3423 " This was triggering ModeChanged before setting VIsual, causing a crash.
3424 au! ModeChanged * norm 0u
3425 sil! norm 
3426
3427 au! ModeChanged
3428endfunc
Bram Moolenaar347538f2022-03-26 16:28:06 +00003429
Charlie Grovesfef44852022-04-19 16:24:12 +01003430func Test_noname_autocmd()
3431 augroup test_noname_autocmd_group
3432 autocmd!
3433 autocmd BufEnter * call add(s:li, ["BufEnter", expand("<afile>")])
3434 autocmd BufDelete * call add(s:li, ["BufDelete", expand("<afile>")])
3435 autocmd BufLeave * call add(s:li, ["BufLeave", expand("<afile>")])
3436 autocmd BufUnload * call add(s:li, ["BufUnload", expand("<afile>")])
3437 autocmd BufWipeout * call add(s:li, ["BufWipeout", expand("<afile>")])
3438 augroup END
3439
3440 let s:li = []
3441 edit foo
3442 call assert_equal([['BufUnload', ''], ['BufDelete', ''], ['BufWipeout', ''], ['BufEnter', 'foo']], s:li)
3443
3444 au! test_noname_autocmd_group
3445 augroup! test_noname_autocmd_group
3446endfunc
3447
Yegappan Lakshmanan1755a912022-05-19 10:31:47 +01003448" Test for the autocmd_get() function
3449func Test_autocmd_get()
3450 augroup TestAutoCmdFns
3451 au!
3452 autocmd BufAdd *.vim echo "bufadd-vim"
3453 autocmd BufAdd *.py echo "bufadd-py"
3454 autocmd BufHidden *.vim echo "bufhidden"
3455 augroup END
3456 augroup TestAutoCmdFns2
3457 autocmd BufAdd *.vim echo "bufadd-vim-2"
3458 autocmd BufRead *.a1b2c3 echo "bufadd-vim-2"
3459 augroup END
3460
3461 let l = autocmd_get()
3462 call assert_true(l->len() > 0)
3463
3464 " Test for getting all the autocmds in a group
3465 let expected = [
3466 \ #{cmd: 'echo "bufadd-vim"', group: 'TestAutoCmdFns',
3467 \ pattern: '*.vim', nested: v:false, once: v:false,
3468 \ event: 'BufAdd'},
3469 \ #{cmd: 'echo "bufadd-py"', group: 'TestAutoCmdFns',
3470 \ pattern: '*.py', nested: v:false, once: v:false,
3471 \ event: 'BufAdd'},
3472 \ #{cmd: 'echo "bufhidden"', group: 'TestAutoCmdFns',
3473 \ pattern: '*.vim', nested: v:false,
3474 \ once: v:false, event: 'BufHidden'}]
3475 call assert_equal(expected, autocmd_get(#{group: 'TestAutoCmdFns'}))
3476
3477 " Test for getting autocmds for all the patterns in a group
3478 call assert_equal(expected, autocmd_get(#{group: 'TestAutoCmdFns',
3479 \ event: '*'}))
3480
3481 " Test for getting autocmds for an event in a group
3482 let expected = [
3483 \ #{cmd: 'echo "bufadd-vim"', group: 'TestAutoCmdFns',
3484 \ pattern: '*.vim', nested: v:false, once: v:false,
3485 \ event: 'BufAdd'},
3486 \ #{cmd: 'echo "bufadd-py"', group: 'TestAutoCmdFns',
3487 \ pattern: '*.py', nested: v:false, once: v:false,
3488 \ event: 'BufAdd'}]
3489 call assert_equal(expected, autocmd_get(#{group: 'TestAutoCmdFns',
3490 \ event: 'BufAdd'}))
3491
3492 " Test for getting the autocmds for all the events in a group for particular
3493 " pattern
3494 call assert_equal([{'cmd': 'echo "bufadd-py"', 'group': 'TestAutoCmdFns',
3495 \ 'pattern': '*.py', 'nested': v:false, 'once': v:false,
3496 \ 'event': 'BufAdd'}],
3497 \ autocmd_get(#{group: 'TestAutoCmdFns', event: '*', pattern: '*.py'}))
3498
3499 " Test for getting the autocmds for an events in a group for particular
3500 " pattern
3501 let l = autocmd_get(#{group: 'TestAutoCmdFns', event: 'BufAdd',
3502 \ pattern: '*.vim'})
3503 call assert_equal([
3504 \ #{cmd: 'echo "bufadd-vim"', group: 'TestAutoCmdFns',
3505 \ pattern: '*.vim', nested: v:false, once: v:false,
3506 \ event: 'BufAdd'}], l)
3507
3508 " Test for getting the autocmds for a pattern in a group
3509 let l = autocmd_get(#{group: 'TestAutoCmdFns', pattern: '*.vim'})
3510 call assert_equal([
3511 \ #{cmd: 'echo "bufadd-vim"', group: 'TestAutoCmdFns',
3512 \ pattern: '*.vim', nested: v:false, once: v:false,
3513 \ event: 'BufAdd'},
3514 \ #{cmd: 'echo "bufhidden"', group: 'TestAutoCmdFns',
3515 \ pattern: '*.vim', nested: v:false,
3516 \ once: v:false, event: 'BufHidden'}], l)
3517
3518 " Test for getting the autocmds for a pattern in all the groups
3519 let l = autocmd_get(#{pattern: '*.a1b2c3'})
3520 call assert_equal([{'cmd': 'echo "bufadd-vim-2"', 'group': 'TestAutoCmdFns2',
3521 \ 'pattern': '*.a1b2c3', 'nested': v:false, 'once': v:false,
3522 \ 'event': 'BufRead'}], l)
3523
3524 " Test for getting autocmds for a pattern without any autocmds
3525 call assert_equal([], autocmd_get(#{group: 'TestAutoCmdFns',
3526 \ pattern: '*.abc'}))
3527 call assert_equal([], autocmd_get(#{group: 'TestAutoCmdFns',
3528 \ event: 'BufAdd', pattern: '*.abc'}))
3529 call assert_equal([], autocmd_get(#{group: 'TestAutoCmdFns',
3530 \ event: 'BufWipeout'}))
3531 call assert_fails("call autocmd_get(#{group: 'abc', event: 'BufAdd'})",
3532 \ 'E367:')
3533 let cmd = "echo autocmd_get(#{group: 'TestAutoCmdFns', event: 'abc'})"
3534 call assert_fails(cmd, 'E216:')
3535 call assert_fails("call autocmd_get(#{group: 'abc'})", 'E367:')
3536 call assert_fails("echo autocmd_get(#{event: 'abc'})", 'E216:')
3537
3538 augroup TestAutoCmdFns
3539 au!
3540 augroup END
3541 call assert_equal([], autocmd_get(#{group: 'TestAutoCmdFns'}))
3542
3543 " Test for nested and once autocmds
3544 augroup TestAutoCmdFns
3545 au!
3546 autocmd VimSuspend * ++nested echo "suspend"
3547 autocmd VimResume * ++once echo "resume"
3548 augroup END
3549
3550 let expected = [
3551 \ {'cmd': 'echo "suspend"', 'group': 'TestAutoCmdFns', 'pattern': '*',
3552 \ 'nested': v:true, 'once': v:false, 'event': 'VimSuspend'},
3553 \ {'cmd': 'echo "resume"', 'group': 'TestAutoCmdFns', 'pattern': '*',
3554 \ 'nested': v:false, 'once': v:true, 'event': 'VimResume'}]
3555 call assert_equal(expected, autocmd_get(#{group: 'TestAutoCmdFns'}))
3556
3557 " Test for buffer-local autocmd
3558 augroup TestAutoCmdFns
3559 au!
3560 autocmd TextYankPost <buffer> echo "textyankpost"
3561 augroup END
3562
3563 let expected = [
3564 \ {'cmd': 'echo "textyankpost"', 'group': 'TestAutoCmdFns',
3565 \ 'pattern': '<buffer=' .. bufnr() .. '>', 'nested': v:false,
3566 \ 'once': v:false, 'bufnr': bufnr(), 'event': 'TextYankPost'}]
3567 call assert_equal(expected, autocmd_get(#{group: 'TestAutoCmdFns'}))
3568
3569 augroup TestAutoCmdFns
3570 au!
3571 augroup END
3572 augroup! TestAutoCmdFns
3573 augroup TestAutoCmdFns2
3574 au!
3575 augroup END
3576 augroup! TestAutoCmdFns2
3577
3578 call assert_fails("echo autocmd_get(#{group: []})", 'E730:')
3579 call assert_fails("echo autocmd_get(#{event: {}})", 'E731:')
3580 call assert_fails("echo autocmd_get([])", 'E1206:')
3581endfunc
3582
3583" Test for the autocmd_add() function
3584func Test_autocmd_add()
3585 " Define a single autocmd in a group
3586 call autocmd_add([#{group: 'TestAcSet', event: 'BufAdd', pattern: '*.sh',
3587 \ cmd: 'echo "bufadd"', once: v:true, nested: v:true}])
3588 call assert_equal([#{cmd: 'echo "bufadd"', group: 'TestAcSet',
3589 \ pattern: '*.sh', nested: v:true, once: v:true,
3590 \ event: 'BufAdd'}], autocmd_get(#{group: 'TestAcSet'}))
3591
3592 " Define two autocmds in the same group
3593 call autocmd_delete([#{group: 'TestAcSet'}])
3594 call autocmd_add([#{group: 'TestAcSet', event: 'BufAdd', pattern: '*.sh',
3595 \ cmd: 'echo "bufadd"'},
3596 \ #{group: 'TestAcSet', event: 'BufEnter', pattern: '*.sh',
3597 \ cmd: 'echo "bufenter"'}])
3598 call assert_equal([
3599 \ #{cmd: 'echo "bufadd"', group: 'TestAcSet', pattern: '*.sh',
3600 \ nested: v:false, once: v:false, event: 'BufAdd'},
3601 \ #{cmd: 'echo "bufenter"', group: 'TestAcSet', pattern: '*.sh',
3602 \ nested: v:false, once: v:false, event: 'BufEnter'}],
3603 \ autocmd_get(#{group: 'TestAcSet'}))
3604
3605 " Define a buffer-local autocmd
3606 call autocmd_delete([#{group: 'TestAcSet'}])
3607 call autocmd_add([#{group: 'TestAcSet', event: 'CursorHold',
3608 \ bufnr: bufnr(), cmd: 'echo "cursorhold"'}])
3609 call assert_equal([
3610 \ #{cmd: 'echo "cursorhold"', group: 'TestAcSet',
3611 \ pattern: '<buffer=' .. bufnr() .. '>', nested: v:false,
3612 \ once: v:false, bufnr: bufnr(), event: 'CursorHold'}],
3613 \ autocmd_get(#{group: 'TestAcSet'}))
3614
3615 " Use an invalid buffer number
3616 call autocmd_delete([#{group: 'TestAcSet'}])
3617 call autocmd_add([#{group: 'TestAcSet', event: 'BufEnter',
3618 \ bufnr: -1, cmd: 'echo "bufenter"'}])
3619 let l = [#{group: 'TestAcSet', event: 'BufAdd', bufnr: 9999,
3620 \ cmd: 'echo "bufadd"'}]
3621 call assert_fails("echo autocmd_add(l)", 'E680:')
Yegappan Lakshmanan00e977c2022-06-01 12:31:53 +01003622 let l = [#{group: 'TestAcSet', event: 'BufAdd', bufnr: 9999,
3623 \ pattern: '*.py', cmd: 'echo "bufadd"'}]
3624 call assert_fails("echo autocmd_add(l)", 'E680:')
3625 let l = [#{group: 'TestAcSet', event: 'BufAdd', bufnr: 9999,
3626 \ pattern: ['*.py', '*.c'], cmd: 'echo "bufadd"'}]
3627 call assert_fails("echo autocmd_add(l)", 'E680:')
Yegappan Lakshmanan1755a912022-05-19 10:31:47 +01003628 let l = [#{group: 'TestAcSet', event: 'BufRead', bufnr: [],
3629 \ cmd: 'echo "bufread"'}]
3630 call assert_fails("echo autocmd_add(l)", 'E745:')
3631 call assert_equal([], autocmd_get(#{group: 'TestAcSet'}))
3632
3633 " Add two commands to the same group, event and pattern
3634 call autocmd_delete([#{group: 'TestAcSet'}])
3635 call autocmd_add([#{group: 'TestAcSet', event: 'BufUnload',
3636 \ pattern: 'abc', cmd: 'echo "cmd1"'}])
3637 call autocmd_add([#{group: 'TestAcSet', event: 'BufUnload',
3638 \ pattern: 'abc', cmd: 'echo "cmd2"'}])
3639 call assert_equal([
3640 \ #{cmd: 'echo "cmd1"', group: 'TestAcSet', pattern: 'abc',
3641 \ nested: v:false, once: v:false, event: 'BufUnload'},
3642 \ #{cmd: 'echo "cmd2"', group: 'TestAcSet', pattern: 'abc',
3643 \ nested: v:false, once: v:false, event: 'BufUnload'}],
3644 \ autocmd_get(#{group: 'TestAcSet'}))
3645
3646 " When adding a new autocmd, if the autocmd 'group' is not specified, then
3647 " the current autocmd group should be used.
3648 call autocmd_delete([#{group: 'TestAcSet'}])
3649 augroup TestAcSet
3650 call autocmd_add([#{event: 'BufHidden', pattern: 'abc', cmd: 'echo "abc"'}])
3651 augroup END
3652 call assert_equal([
3653 \ #{cmd: 'echo "abc"', group: 'TestAcSet', pattern: 'abc',
3654 \ nested: v:false, once: v:false, event: 'BufHidden'}],
3655 \ autocmd_get(#{group: 'TestAcSet'}))
3656
Yegappan Lakshmanan971f6822022-05-24 11:40:11 +01003657 " Test for replacing a cmd for an event in a group
3658 call autocmd_delete([#{group: 'TestAcSet'}])
3659 call autocmd_add([#{replace: v:true, group: 'TestAcSet', event: 'BufEnter',
3660 \ pattern: '*.py', cmd: 'echo "bufenter"'}])
3661 call autocmd_add([#{replace: v:true, group: 'TestAcSet', event: 'BufEnter',
3662 \ pattern: '*.py', cmd: 'echo "bufenter"'}])
3663 call assert_equal([
3664 \ #{cmd: 'echo "bufenter"', group: 'TestAcSet', pattern: '*.py',
3665 \ nested: v:false, once: v:false, event: 'BufEnter'}],
3666 \ autocmd_get(#{group: 'TestAcSet'}))
3667
3668 " Test for adding a command for an unsupported autocmd event
Yegappan Lakshmanan1755a912022-05-19 10:31:47 +01003669 let l = [#{group: 'TestAcSet', event: 'abc', pattern: '*.sh',
3670 \ cmd: 'echo "bufadd"'}]
3671 call assert_fails('call autocmd_add(l)', 'E216:')
3672
Yegappan Lakshmanane0ff3a72022-05-27 18:05:33 +01003673 " Test for using a list of events and patterns
3674 call autocmd_delete([#{group: 'TestAcSet'}])
3675 let l = [#{group: 'TestAcSet', event: ['BufEnter', 'BufLeave'],
3676 \ pattern: ['*.py', '*.sh'], cmd: 'echo "bufcmds"'}]
3677 call autocmd_add(l)
3678 call assert_equal([
3679 \ #{cmd: 'echo "bufcmds"', group: 'TestAcSet', pattern: '*.py',
3680 \ nested: v:false, once: v:false, event: 'BufEnter'},
3681 \ #{cmd: 'echo "bufcmds"', group: 'TestAcSet', pattern: '*.sh',
3682 \ nested: v:false, once: v:false, event: 'BufEnter'},
3683 \ #{cmd: 'echo "bufcmds"', group: 'TestAcSet', pattern: '*.py',
3684 \ nested: v:false, once: v:false, event: 'BufLeave'},
3685 \ #{cmd: 'echo "bufcmds"', group: 'TestAcSet', pattern: '*.sh',
3686 \ nested: v:false, once: v:false, event: 'BufLeave'}],
3687 \ autocmd_get(#{group: 'TestAcSet'}))
3688
3689 " Test for invalid values for 'event' item
3690 call autocmd_delete([#{group: 'TestAcSet'}])
3691 let l = [#{group: 'TestAcSet', event: test_null_string(),
3692 \ pattern: "*.py", cmd: 'echo "bufcmds"'}]
3693 call assert_fails('call autocmd_add(l)', 'E928:')
3694 let l = [#{group: 'TestAcSet', event: test_null_list(),
3695 \ pattern: "*.py", cmd: 'echo "bufcmds"'}]
3696 call assert_fails('call autocmd_add(l)', 'E714:')
3697 let l = [#{group: 'TestAcSet', event: {},
3698 \ pattern: "*.py", cmd: 'echo "bufcmds"'}]
3699 call assert_fails('call autocmd_add(l)', 'E777:')
3700 let l = [#{group: 'TestAcSet', event: [{}],
3701 \ pattern: "*.py", cmd: 'echo "bufcmds"'}]
3702 call assert_fails('call autocmd_add(l)', 'E928:')
3703 let l = [#{group: 'TestAcSet', event: [test_null_string()],
3704 \ pattern: "*.py", cmd: 'echo "bufcmds"'}]
3705 call assert_fails('call autocmd_add(l)', 'E928:')
3706 let l = [#{group: 'TestAcSet', event: 'BufEnter,BufLeave',
3707 \ pattern: '*.py', cmd: 'echo "bufcmds"'}]
3708 call assert_fails('call autocmd_add(l)', 'E216:')
3709 let l = [#{group: 'TestAcSet', event: [],
3710 \ pattern: "*.py", cmd: 'echo "bufcmds"'}]
3711 call autocmd_add(l)
3712 let l = [#{group: 'TestAcSet', event: [""],
3713 \ pattern: "*.py", cmd: 'echo "bufcmds"'}]
3714 call assert_fails('call autocmd_add(l)', 'E216:')
3715 let l = [#{group: 'TestAcSet', event: "",
3716 \ pattern: "*.py", cmd: 'echo "bufcmds"'}]
3717 call autocmd_add(l)
3718 call assert_equal([], autocmd_get(#{group: 'TestAcSet'}))
3719
3720 " Test for invalid values for 'pattern' item
3721 let l = [#{group: 'TestAcSet', event: "BufEnter",
3722 \ pattern: test_null_string(), cmd: 'echo "bufcmds"'}]
Yegappan Lakshmanan00e977c2022-06-01 12:31:53 +01003723 call assert_fails('call autocmd_add(l)', 'E928:')
Yegappan Lakshmanane0ff3a72022-05-27 18:05:33 +01003724 let l = [#{group: 'TestAcSet', event: "BufEnter",
3725 \ pattern: test_null_list(), cmd: 'echo "bufcmds"'}]
3726 call assert_fails('call autocmd_add(l)', 'E714:')
3727 let l = [#{group: 'TestAcSet', event: "BufEnter",
3728 \ pattern: {}, cmd: 'echo "bufcmds"'}]
3729 call assert_fails('call autocmd_add(l)', 'E777:')
3730 let l = [#{group: 'TestAcSet', event: "BufEnter",
3731 \ pattern: [{}], cmd: 'echo "bufcmds"'}]
3732 call assert_fails('call autocmd_add(l)', 'E928:')
3733 let l = [#{group: 'TestAcSet', event: "BufEnter",
3734 \ pattern: [test_null_string()], cmd: 'echo "bufcmds"'}]
3735 call assert_fails('call autocmd_add(l)', 'E928:')
3736 let l = [#{group: 'TestAcSet', event: "BufEnter",
3737 \ pattern: [], cmd: 'echo "bufcmds"'}]
3738 call autocmd_add(l)
3739 let l = [#{group: 'TestAcSet', event: "BufEnter",
3740 \ pattern: [""], cmd: 'echo "bufcmds"'}]
3741 call autocmd_add(l)
3742 let l = [#{group: 'TestAcSet', event: "BufEnter",
3743 \ pattern: "", cmd: 'echo "bufcmds"'}]
3744 call autocmd_add(l)
3745 call assert_equal([], autocmd_get(#{group: 'TestAcSet'}))
3746
3747 let l = [#{group: 'TestAcSet', event: 'BufEnter,abc,BufLeave',
3748 \ pattern: '*.py', cmd: 'echo "bufcmds"'}]
3749 call assert_fails('call autocmd_add(l)', 'E216:')
3750
Yegappan Lakshmanan1755a912022-05-19 10:31:47 +01003751 call assert_fails("call autocmd_add({})", 'E1211:')
3752 call assert_equal(v:false, autocmd_add(test_null_list()))
3753 call assert_true(autocmd_add([[]]))
3754 call assert_true(autocmd_add([test_null_dict()]))
3755
3756 augroup TestAcSet
3757 au!
3758 augroup END
3759
3760 call autocmd_add([#{group: 'TestAcSet'}])
3761 call autocmd_add([#{group: 'TestAcSet', event: 'BufAdd'}])
3762 call autocmd_add([#{group: 'TestAcSet', pat: '*.sh'}])
3763 call autocmd_add([#{group: 'TestAcSet', cmd: 'echo "a"'}])
3764 call autocmd_add([#{group: 'TestAcSet', event: 'BufAdd', pat: '*.sh'}])
3765 call autocmd_add([#{group: 'TestAcSet', event: 'BufAdd', cmd: 'echo "a"'}])
3766 call autocmd_add([#{group: 'TestAcSet', pat: '*.sh', cmd: 'echo "a"'}])
3767 call assert_equal([], autocmd_get(#{group: 'TestAcSet'}))
3768
3769 augroup! TestAcSet
3770endfunc
3771
3772" Test for deleting autocmd events and groups
3773func Test_autocmd_delete()
3774 " Delete an event in an autocmd group
3775 augroup TestAcSet
3776 au!
3777 au BufAdd *.sh echo "bufadd"
3778 au BufEnter *.sh echo "bufenter"
3779 augroup END
3780 call autocmd_delete([#{group: 'TestAcSet', event: 'BufAdd'}])
3781 call assert_equal([#{cmd: 'echo "bufenter"', group: 'TestAcSet',
3782 \ pattern: '*.sh', nested: v:false, once: v:false,
3783 \ event: 'BufEnter'}], autocmd_get(#{group: 'TestAcSet'}))
3784
3785 " Delete all the events in an autocmd group
3786 augroup TestAcSet
3787 au BufAdd *.sh echo "bufadd"
3788 augroup END
3789 call autocmd_delete([#{group: 'TestAcSet', event: '*'}])
3790 call assert_equal([], autocmd_get(#{group: 'TestAcSet'}))
3791
3792 " Delete a non-existing autocmd group
3793 call assert_fails("call autocmd_delete([#{group: 'abc'}])", 'E367:')
3794 " Delete a non-existing autocmd event
3795 let l = [#{group: 'TestAcSet', event: 'abc'}]
3796 call assert_fails("call autocmd_delete(l)", 'E216:')
3797 " Delete a non-existing autocmd pattern
3798 let l = [#{group: 'TestAcSet', event: 'BufAdd', pat: 'abc'}]
3799 call assert_true(autocmd_delete(l))
Yegappan Lakshmanan00e977c2022-06-01 12:31:53 +01003800 " Delete an autocmd for a non-existing buffer
3801 let l = [#{event: '*', bufnr: 9999, cmd: 'echo "x"'}]
3802 call assert_fails('call autocmd_delete(l)', 'E680:')
Yegappan Lakshmanan1755a912022-05-19 10:31:47 +01003803
3804 " Delete an autocmd group
3805 augroup TestAcSet
3806 au!
3807 au BufAdd *.sh echo "bufadd"
3808 au BufEnter *.sh echo "bufenter"
3809 augroup END
3810 call autocmd_delete([#{group: 'TestAcSet'}])
3811 call assert_fails("call autocmd_get(#{group: 'TestAcSet'})", 'E367:')
3812
3813 call assert_true(autocmd_delete([[]]))
3814 call assert_true(autocmd_delete([test_null_dict()]))
3815endfunc
3816
Bram Moolenaarbc2b71d2020-02-17 21:33:30 +01003817" vim: shiftwidth=2 sts=2 expandtab