blob: 669089df2d5937afa8c84ed8294b5f5e8497d20b [file] [log] [blame]
Bram Moolenaar14735512016-03-26 21:00:08 +01001" Tests for autocommands
2
Bram Moolenaar8c64a362018-03-23 22:39:31 +01003source shared.vim
Bram Moolenaar8c5a2782019-08-07 23:07:07 +02004source check.vim
Bram Moolenaarcadbe1b2019-09-22 21:50:09 +02005source term_util.vim
LemonBoy09371822022-04-08 15:18:45 +01006source screendump.vim
Bram Moolenaar6f2465d2022-03-22 18:13:01 +00007import './vim9.vim' as v9
Bram Moolenaar8c64a362018-03-23 22:39:31 +01008
Bram Moolenaar1e115362019-01-09 23:01:02 +01009func s:cleanup_buffers() abort
Bram Moolenaarb3435b02016-09-29 20:54:59 +020010 for bnr in range(1, bufnr('$'))
11 if bufloaded(bnr) && bufnr('%') != bnr
12 execute 'bd! ' . bnr
13 endif
14 endfor
Bram Moolenaar04f62f82017-07-19 18:18:39 +020015endfunc
Bram Moolenaarb3435b02016-09-29 20:54:59 +020016
Bram Moolenaar14735512016-03-26 21:00:08 +010017func Test_vim_did_enter()
18 call assert_false(v:vim_did_enter)
19
20 " This script will never reach the main loop, can't check if v:vim_did_enter
21 " becomes one.
22endfunc
Bram Moolenaar40b1b542016-04-20 20:18:23 +020023
Bram Moolenaar75911162020-07-21 19:44:47 +020024" Test for the CursorHold autocmd
25func Test_CursorHold_autocmd()
26 CheckRunVimInTerminal
Bram Moolenaare1f3ab72022-09-04 21:29:08 +010027 call writefile(['one', 'two', 'three'], 'XoneTwoThree', 'D')
Bram Moolenaar75911162020-07-21 19:44:47 +020028 let before =<< trim END
29 set updatetime=10
Bram Moolenaare7cda972022-08-29 11:02:59 +010030 au CursorHold * call writefile([line('.')], 'XCHoutput', 'a')
Bram Moolenaar75911162020-07-21 19:44:47 +020031 END
Bram Moolenaare1f3ab72022-09-04 21:29:08 +010032 call writefile(before, 'XCHinit', 'D')
Bram Moolenaare7cda972022-08-29 11:02:59 +010033 let buf = RunVimInTerminal('-S XCHinit XoneTwoThree', {})
Bram Moolenaar17f67542020-08-20 18:29:13 +020034 call term_sendkeys(buf, "G")
Bram Moolenaar62cd26a2020-10-11 20:08:44 +020035 call term_wait(buf, 50)
Bram Moolenaar75911162020-07-21 19:44:47 +020036 call term_sendkeys(buf, "gg")
37 call term_wait(buf)
Bram Moolenaare7cda972022-08-29 11:02:59 +010038 call WaitForAssert({-> assert_equal(['1'], readfile('XCHoutput')[-1:-1])})
Bram Moolenaar75911162020-07-21 19:44:47 +020039 call term_sendkeys(buf, "j")
40 call term_wait(buf)
Bram Moolenaare7cda972022-08-29 11:02:59 +010041 call WaitForAssert({-> assert_equal(['1', '2'], readfile('XCHoutput')[-2:-1])})
Bram Moolenaar75911162020-07-21 19:44:47 +020042 call term_sendkeys(buf, "j")
43 call term_wait(buf)
Bram Moolenaare7cda972022-08-29 11:02:59 +010044 call WaitForAssert({-> assert_equal(['1', '2', '3'], readfile('XCHoutput')[-3:-1])})
Bram Moolenaar75911162020-07-21 19:44:47 +020045 call StopVimInTerminal(buf)
46
Bram Moolenaare7cda972022-08-29 11:02:59 +010047 call delete('XCHoutput')
Bram Moolenaar75911162020-07-21 19:44:47 +020048endfunc
49
Bram Moolenaarc67e8922016-05-24 16:07:40 +020050if has('timers')
Bram Moolenaar97b00752019-05-12 13:07:14 +020051
Bram Moolenaarc67e8922016-05-24 16:07:40 +020052 func ExitInsertMode(id)
53 call feedkeys("\<Esc>")
54 endfunc
55
56 func Test_cursorhold_insert()
Bram Moolenaarf18c4db2016-09-08 22:10:06 +020057 " Need to move the cursor.
58 call feedkeys("ggG", "xt")
59
Bram Moolenaarc67e8922016-05-24 16:07:40 +020060 let g:triggered = 0
61 au CursorHoldI * let g:triggered += 1
62 set updatetime=20
Bram Moolenaar92bb83e2021-02-03 23:04:46 +010063 call timer_start(200, 'ExitInsertMode')
Bram Moolenaarc67e8922016-05-24 16:07:40 +020064 call feedkeys('a', 'x!')
65 call assert_equal(1, g:triggered)
Bram Moolenaar26d98212019-01-27 22:32:55 +010066 unlet g:triggered
67 au! CursorHoldI
68 set updatetime&
69 endfunc
70
71 func Test_cursorhold_insert_with_timer_interrupt()
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +020072 CheckFeature job
Bram Moolenaar26d98212019-01-27 22:32:55 +010073 " Need to move the cursor.
74 call feedkeys("ggG", "xt")
75
76 " Confirm the timer invoked in exit_cb of the job doesn't disturb
77 " CursorHoldI event.
78 let g:triggered = 0
79 au CursorHoldI * let g:triggered += 1
Bram Moolenaar62cd26a2020-10-11 20:08:44 +020080 set updatetime=100
Bram Moolenaar26d98212019-01-27 22:32:55 +010081 call job_start(has('win32') ? 'cmd /c echo:' : 'echo',
Bram Moolenaar62cd26a2020-10-11 20:08:44 +020082 \ {'exit_cb': {-> timer_start(200, 'ExitInsertMode')}})
Bram Moolenaar26d98212019-01-27 22:32:55 +010083 call feedkeys('a', 'x!')
84 call assert_equal(1, g:triggered)
85 unlet g:triggered
Bram Moolenaare99e8442016-07-26 20:43:40 +020086 au! CursorHoldI
Bram Moolenaaraeac9002016-09-06 22:15:08 +020087 set updatetime&
Bram Moolenaarc67e8922016-05-24 16:07:40 +020088 endfunc
89
90 func Test_cursorhold_insert_ctrl_x()
91 let g:triggered = 0
92 au CursorHoldI * let g:triggered += 1
93 set updatetime=20
94 call timer_start(100, 'ExitInsertMode')
95 " CursorHoldI does not trigger after CTRL-X
96 call feedkeys("a\<C-X>", 'x!')
97 call assert_equal(0, g:triggered)
Bram Moolenaar26d98212019-01-27 22:32:55 +010098 unlet g:triggered
Bram Moolenaare99e8442016-07-26 20:43:40 +020099 au! CursorHoldI
Bram Moolenaaraeac9002016-09-06 22:15:08 +0200100 set updatetime&
Bram Moolenaarc67e8922016-05-24 16:07:40 +0200101 endfunc
Bram Moolenaar97b00752019-05-12 13:07:14 +0200102
Bram Moolenaar5a9357d2021-10-03 16:22:05 +0100103 func Test_cursorhold_insert_ctrl_g_U()
104 au CursorHoldI * :
105 set updatetime=20
106 new
107 call timer_start(100, { -> feedkeys("\<Left>foo\<Esc>", 't') })
108 call feedkeys("i()\<C-g>U", 'tx!')
109 sleep 200m
110 call assert_equal('(foo)', getline(1))
111 undo
112 call assert_equal('', getline(1))
113
114 bwipe!
115 au! CursorHoldI
116 set updatetime&
117 endfunc
118
Bram Moolenaar97b00752019-05-12 13:07:14 +0200119 func Test_OptionSet_modeline()
120 call test_override('starting', 1)
121 au! OptionSet
122 augroup set_tabstop
123 au OptionSet tabstop call timer_start(1, {-> execute("echo 'Handler called'", "")})
124 augroup END
Bram Moolenaare1f3ab72022-09-04 21:29:08 +0100125 call writefile(['vim: set ts=7 sw=5 :', 'something'], 'XoptionsetModeline', 'D')
Bram Moolenaar97b00752019-05-12 13:07:14 +0200126 set modeline
127 let v:errmsg = ''
128 call assert_fails('split XoptionsetModeline', 'E12:')
129 call assert_equal(7, &ts)
130 call assert_equal('', v:errmsg)
131
132 augroup set_tabstop
133 au!
134 augroup END
135 bwipe!
136 set ts&
Bram Moolenaar97b00752019-05-12 13:07:14 +0200137 call test_override('starting', 0)
138 endfunc
139
140endif "has('timers')
Bram Moolenaar40b1b542016-04-20 20:18:23 +0200141
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200142func Test_bufunload()
Bram Moolenaarc67e8922016-05-24 16:07:40 +0200143 augroup test_bufunload_group
144 autocmd!
145 autocmd BufUnload * call add(s:li, "bufunload")
146 autocmd BufDelete * call add(s:li, "bufdelete")
147 autocmd BufWipeout * call add(s:li, "bufwipeout")
148 augroup END
Bram Moolenaar40b1b542016-04-20 20:18:23 +0200149
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100150 let s:li = []
Bram Moolenaarc67e8922016-05-24 16:07:40 +0200151 new
152 setlocal bufhidden=
153 bunload
154 call assert_equal(["bufunload", "bufdelete"], s:li)
Bram Moolenaar40b1b542016-04-20 20:18:23 +0200155
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100156 let s:li = []
Bram Moolenaarc67e8922016-05-24 16:07:40 +0200157 new
158 setlocal bufhidden=delete
159 bunload
160 call assert_equal(["bufunload", "bufdelete"], s:li)
161
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100162 let s:li = []
Bram Moolenaarc67e8922016-05-24 16:07:40 +0200163 new
164 setlocal bufhidden=unload
165 bwipeout
166 call assert_equal(["bufunload", "bufdelete", "bufwipeout"], s:li)
167
Bram Moolenaare99e8442016-07-26 20:43:40 +0200168 au! test_bufunload_group
Bram Moolenaarc67e8922016-05-24 16:07:40 +0200169 augroup! test_bufunload_group
Bram Moolenaar40b1b542016-04-20 20:18:23 +0200170endfunc
Bram Moolenaar30445cb2016-07-09 15:21:02 +0200171
172" SEGV occurs in older versions. (At least 7.4.2005 or older)
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200173func Test_autocmd_bufunload_with_tabnext()
Bram Moolenaar30445cb2016-07-09 15:21:02 +0200174 tabedit
175 tabfirst
176
177 augroup test_autocmd_bufunload_with_tabnext_group
178 autocmd!
179 autocmd BufUnload <buffer> tabnext
180 augroup END
181
182 quit
183 call assert_equal(2, tabpagenr('$'))
184
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200185 autocmd! test_autocmd_bufunload_with_tabnext_group
Bram Moolenaar30445cb2016-07-09 15:21:02 +0200186 augroup! test_autocmd_bufunload_with_tabnext_group
187 tablast
188 quit
189endfunc
Bram Moolenaarc917da42016-07-19 22:31:36 +0200190
Bram Moolenaar5ed58c72021-01-28 14:24:55 +0100191func Test_argdelete_in_next()
192 au BufNew,BufEnter,BufLeave,BufWinEnter * argdel
193 call assert_fails('next a b', 'E1156:')
194 au! BufNew,BufEnter,BufLeave,BufWinEnter *
195endfunc
196
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200197func Test_autocmd_bufwinleave_with_tabfirst()
Bram Moolenaarf9e687e2016-09-04 21:33:09 +0200198 tabedit
199 augroup sample
200 autocmd!
201 autocmd BufWinLeave <buffer> tabfirst
202 augroup END
203 call setline(1, ['a', 'b', 'c'])
204 edit! a.txt
Bram Moolenaarf18c4db2016-09-08 22:10:06 +0200205 tabclose
Bram Moolenaarf9e687e2016-09-04 21:33:09 +0200206endfunc
207
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200208" SEGV occurs in older versions. (At least 7.4.2321 or older)
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200209func Test_autocmd_bufunload_avoiding_SEGV_01()
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200210 split aa.txt
211 let lastbuf = bufnr('$')
212
213 augroup test_autocmd_bufunload
214 autocmd!
215 exe 'autocmd BufUnload <buffer> ' . (lastbuf + 1) . 'bwipeout!'
216 augroup END
217
Bram Moolenaar28ee8922020-10-28 20:20:00 +0100218 call assert_fails('edit bb.txt', 'E937:')
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200219
220 autocmd! test_autocmd_bufunload
221 augroup! test_autocmd_bufunload
222 bwipe! aa.txt
223 bwipe! bb.txt
224endfunc
225
226" SEGV occurs in older versions. (At least 7.4.2321 or older)
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200227func Test_autocmd_bufunload_avoiding_SEGV_02()
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200228 setlocal buftype=nowrite
229 let lastbuf = bufnr('$')
230
231 augroup test_autocmd_bufunload
232 autocmd!
233 exe 'autocmd BufUnload <buffer> ' . (lastbuf + 1) . 'bwipeout!'
234 augroup END
235
236 normal! i1
237 call assert_fails('edit a.txt', 'E517:')
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200238
239 autocmd! test_autocmd_bufunload
240 augroup! test_autocmd_bufunload
241 bwipe! a.txt
242endfunc
243
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100244func Test_autocmd_dummy_wipeout()
245 " prepare files
Bram Moolenaare1f3ab72022-09-04 21:29:08 +0100246 call writefile([''], 'Xdummywipetest1.txt', 'D')
247 call writefile([''], 'Xdummywipetest2.txt', 'D')
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100248 augroup test_bufunload_group
249 autocmd!
250 autocmd BufUnload * call add(s:li, "bufunload")
251 autocmd BufDelete * call add(s:li, "bufdelete")
252 autocmd BufWipeout * call add(s:li, "bufwipeout")
253 augroup END
254
255 let s:li = []
256 split Xdummywipetest1.txt
257 silent! vimgrep /notmatched/ Xdummywipetest*
258 call assert_equal(["bufunload", "bufwipeout"], s:li)
259
260 bwipeout
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100261 au! test_bufunload_group
262 augroup! test_bufunload_group
263endfunc
264
Bram Moolenaarc917da42016-07-19 22:31:36 +0200265func Test_win_tab_autocmd()
266 let g:record = []
267
268 augroup testing
269 au WinNew * call add(g:record, 'WinNew')
naohiro ono23beefe2021-11-13 12:38:49 +0000270 au WinClosed * call add(g:record, 'WinClosed')
Bram Moolenaarc917da42016-07-19 22:31:36 +0200271 au WinEnter * call add(g:record, 'WinEnter')
272 au WinLeave * call add(g:record, 'WinLeave')
273 au TabNew * call add(g:record, 'TabNew')
Bram Moolenaar12c11d52016-07-19 23:13:03 +0200274 au TabClosed * call add(g:record, 'TabClosed')
Bram Moolenaarc917da42016-07-19 22:31:36 +0200275 au TabEnter * call add(g:record, 'TabEnter')
276 au TabLeave * call add(g:record, 'TabLeave')
277 augroup END
278
279 split
280 tabnew
281 close
282 close
283
284 call assert_equal([
285 \ 'WinLeave', 'WinNew', 'WinEnter',
286 \ 'WinLeave', 'TabLeave', 'WinNew', 'WinEnter', 'TabNew', 'TabEnter',
naohiro ono23beefe2021-11-13 12:38:49 +0000287 \ 'WinLeave', 'TabLeave', 'WinClosed', 'TabClosed', 'WinEnter', 'TabEnter',
288 \ 'WinLeave', 'WinClosed', 'WinEnter'
Bram Moolenaarc917da42016-07-19 22:31:36 +0200289 \ ], g:record)
290
Bram Moolenaar12c11d52016-07-19 23:13:03 +0200291 let g:record = []
292 tabnew somefile
293 tabnext
294 bwipe somefile
295
296 call assert_equal([
297 \ 'WinLeave', 'TabLeave', 'WinNew', 'WinEnter', 'TabNew', 'TabEnter',
298 \ 'WinLeave', 'TabLeave', 'WinEnter', 'TabEnter',
naohiro ono23beefe2021-11-13 12:38:49 +0000299 \ 'WinClosed', 'TabClosed'
Bram Moolenaar12c11d52016-07-19 23:13:03 +0200300 \ ], g:record)
301
Bram Moolenaarc917da42016-07-19 22:31:36 +0200302 augroup testing
303 au!
304 augroup END
305 unlet g:record
306endfunc
Bram Moolenaare99e8442016-07-26 20:43:40 +0200307
LemonBoy09371822022-04-08 15:18:45 +0100308func Test_WinScrolled()
309 CheckRunVimInTerminal
310
311 let lines =<< trim END
zeertzjqd58862d2022-04-12 11:32:48 +0100312 set nowrap scrolloff=0
313 for ii in range(1, 18)
314 call setline(ii, repeat(nr2char(96 + ii), ii * 2))
315 endfor
316 let win_id = win_getid()
317 let g:matched = v:false
318 execute 'au WinScrolled' win_id 'let g:matched = v:true'
319 let g:scrolled = 0
320 au WinScrolled * let g:scrolled += 1
321 au WinScrolled * let g:amatch = str2nr(expand('<amatch>'))
322 au WinScrolled * let g:afile = str2nr(expand('<afile>'))
LemonBoy09371822022-04-08 15:18:45 +0100323 END
Bram Moolenaare1f3ab72022-09-04 21:29:08 +0100324 call writefile(lines, 'Xtest_winscrolled', 'D')
LemonBoy09371822022-04-08 15:18:45 +0100325 let buf = RunVimInTerminal('-S Xtest_winscrolled', {'rows': 6})
326
327 call term_sendkeys(buf, ":echo g:scrolled\<CR>")
328 call WaitForAssert({-> assert_match('^0 ', term_getline(buf, 6))}, 1000)
329
330 " Scroll left/right in Normal mode.
331 call term_sendkeys(buf, "zlzh:echo g:scrolled\<CR>")
332 call WaitForAssert({-> assert_match('^2 ', term_getline(buf, 6))}, 1000)
333
334 " Scroll up/down in Normal mode.
335 call term_sendkeys(buf, "\<c-e>\<c-y>:echo g:scrolled\<CR>")
336 call WaitForAssert({-> assert_match('^4 ', term_getline(buf, 6))}, 1000)
337
338 " Scroll up/down in Insert mode.
339 call term_sendkeys(buf, "Mi\<c-x>\<c-e>\<Esc>i\<c-x>\<c-y>\<Esc>")
340 call term_sendkeys(buf, ":echo g:scrolled\<CR>")
341 call WaitForAssert({-> assert_match('^6 ', term_getline(buf, 6))}, 1000)
342
343 " Scroll the window horizontally to focus the last letter of the third line
344 " containing only six characters. Moving to the previous and shorter lines
345 " should trigger another autocommand as Vim has to make them visible.
346 call term_sendkeys(buf, "5zl2k")
347 call term_sendkeys(buf, ":echo g:scrolled\<CR>")
348 call WaitForAssert({-> assert_match('^8 ', term_getline(buf, 6))}, 1000)
349
350 " Ensure the command was triggered for the specified window ID.
351 call term_sendkeys(buf, ":echo g:matched\<CR>")
352 call WaitForAssert({-> assert_match('^v:true ', term_getline(buf, 6))}, 1000)
353
354 " Ensure the expansion of <amatch> and <afile> matches the window ID.
355 call term_sendkeys(buf, ":echo g:amatch == win_id && g:afile == win_id\<CR>")
356 call WaitForAssert({-> assert_match('^v:true ', term_getline(buf, 6))}, 1000)
357
358 call StopVimInTerminal(buf)
LemonBoy09371822022-04-08 15:18:45 +0100359endfunc
360
LemonBoy66e13ae2022-04-21 22:52:11 +0100361func Test_WinScrolled_mouse()
362 CheckRunVimInTerminal
363
364 let lines =<< trim END
365 set nowrap scrolloff=0
366 set mouse=a term=xterm ttymouse=sgr mousetime=200 clipboard=
367 call setline(1, ['foo']->repeat(32))
368 split
369 let g:scrolled = 0
370 au WinScrolled * let g:scrolled += 1
371 END
Bram Moolenaare1f3ab72022-09-04 21:29:08 +0100372 call writefile(lines, 'Xtest_winscrolled_mouse', 'D')
LemonBoy66e13ae2022-04-21 22:52:11 +0100373 let buf = RunVimInTerminal('-S Xtest_winscrolled_mouse', {'rows': 10})
374
375 " With the upper split focused, send a scroll-down event to the unfocused one.
376 call test_setmouse(7, 1)
377 call term_sendkeys(buf, "\<ScrollWheelDown>")
378 call TermWait(buf)
379 call term_sendkeys(buf, ":echo g:scrolled\<CR>")
380 call WaitForAssert({-> assert_match('^1', term_getline(buf, 10))}, 1000)
381
382 " Again, but this time while we're in insert mode.
383 call term_sendkeys(buf, "i\<ScrollWheelDown>\<Esc>")
384 call TermWait(buf)
385 call term_sendkeys(buf, ":echo g:scrolled\<CR>")
386 call WaitForAssert({-> assert_match('^2', term_getline(buf, 10))}, 1000)
387
388 call StopVimInTerminal(buf)
LemonBoy66e13ae2022-04-21 22:52:11 +0100389endfunc
390
zeertzjqd58862d2022-04-12 11:32:48 +0100391func Test_WinScrolled_close_curwin()
392 CheckRunVimInTerminal
393
394 let lines =<< trim END
395 set nowrap scrolloff=0
396 call setline(1, ['aaa', 'bbb'])
397 vsplit
398 au WinScrolled * close
399 au VimLeave * call writefile(['123456'], 'Xtestout')
400 END
Bram Moolenaare1f3ab72022-09-04 21:29:08 +0100401 call writefile(lines, 'Xtest_winscrolled_close_curwin', 'D')
zeertzjqd58862d2022-04-12 11:32:48 +0100402 let buf = RunVimInTerminal('-S Xtest_winscrolled_close_curwin', {'rows': 6})
403
404 " This was using freed memory
405 call term_sendkeys(buf, "\<C-E>")
406 call TermWait(buf)
407 call StopVimInTerminal(buf)
408
409 call assert_equal(['123456'], readfile('Xtestout'))
410
zeertzjqd58862d2022-04-12 11:32:48 +0100411 call delete('Xtestout')
412endfunc
413
zeertzjq670ab032022-08-28 19:16:15 +0100414func Test_WinScrolled_long_wrapped()
415 CheckRunVimInTerminal
416
417 let lines =<< trim END
418 set scrolloff=0
419 let height = winheight(0)
420 let width = winwidth(0)
421 let g:scrolled = 0
422 au WinScrolled * let g:scrolled += 1
423 call setline(1, repeat('foo', height * width))
424 call cursor(1, height * width)
425 END
Bram Moolenaare1f3ab72022-09-04 21:29:08 +0100426 call writefile(lines, 'Xtest_winscrolled_long_wrapped', 'D')
zeertzjq670ab032022-08-28 19:16:15 +0100427 let buf = RunVimInTerminal('-S Xtest_winscrolled_long_wrapped', {'rows': 6})
428
429 call term_sendkeys(buf, ":echo g:scrolled\<CR>")
430 call WaitForAssert({-> assert_match('^0 ', term_getline(buf, 6))}, 1000)
431
432 call term_sendkeys(buf, 'gj')
433 call term_sendkeys(buf, ":echo g:scrolled\<CR>")
434 call WaitForAssert({-> assert_match('^1 ', term_getline(buf, 6))}, 1000)
435
436 call term_sendkeys(buf, '0')
437 call term_sendkeys(buf, ":echo g:scrolled\<CR>")
438 call WaitForAssert({-> assert_match('^2 ', term_getline(buf, 6))}, 1000)
439
440 call term_sendkeys(buf, '$')
441 call term_sendkeys(buf, ":echo g:scrolled\<CR>")
442 call WaitForAssert({-> assert_match('^3 ', term_getline(buf, 6))}, 1000)
zeertzjq670ab032022-08-28 19:16:15 +0100443endfunc
444
naohiro ono23beefe2021-11-13 12:38:49 +0000445func Test_WinClosed()
446 " Test that the pattern is matched against the closed window's ID, and both
447 " <amatch> and <afile> are set to it.
448 new
449 let winid = win_getid()
450 let g:matched = v:false
451 augroup test-WinClosed
452 autocmd!
453 execute 'autocmd WinClosed' winid 'let g:matched = v:true'
454 autocmd WinClosed * let g:amatch = str2nr(expand('<amatch>'))
455 autocmd WinClosed * let g:afile = str2nr(expand('<afile>'))
456 augroup END
457 close
458 call assert_true(g:matched)
459 call assert_equal(winid, g:amatch)
460 call assert_equal(winid, g:afile)
461
462 " Test that WinClosed is non-recursive.
463 new
464 new
465 call assert_equal(3, winnr('$'))
466 let g:triggered = 0
467 augroup test-WinClosed
468 autocmd!
469 autocmd WinClosed * let g:triggered += 1
470 autocmd WinClosed * 2 wincmd c
471 augroup END
472 close
473 call assert_equal(1, winnr('$'))
474 call assert_equal(1, g:triggered)
475
476 autocmd! test-WinClosed
477 augroup! test-WinClosed
478 unlet g:matched
479 unlet g:amatch
480 unlet g:afile
481 unlet g:triggered
482endfunc
483
Bram Moolenaarc947b9a2022-04-06 17:59:21 +0100484func Test_WinClosed_throws()
485 vnew
486 let bnr = bufnr()
487 call assert_equal(1, bufloaded(bnr))
488 augroup test-WinClosed
489 autocmd WinClosed * throw 'foo'
490 augroup END
491 try
492 close
493 catch /.*/
494 endtry
495 call assert_equal(0, bufloaded(bnr))
496
497 autocmd! test-WinClosed
498 augroup! test-WinClosed
499endfunc
500
zeertzjq6a069402022-04-07 14:08:29 +0100501func Test_WinClosed_throws_with_tabs()
502 tabnew
503 let bnr = bufnr()
504 call assert_equal(1, bufloaded(bnr))
505 augroup test-WinClosed
506 autocmd WinClosed * throw 'foo'
507 augroup END
508 try
509 close
510 catch /.*/
511 endtry
512 call assert_equal(0, bufloaded(bnr))
513
514 autocmd! test-WinClosed
515 augroup! test-WinClosed
516endfunc
517
Bram Moolenaare99e8442016-07-26 20:43:40 +0200518func s:AddAnAutocmd()
519 augroup vimBarTest
520 au BufReadCmd * echo 'hello'
521 augroup END
522 call assert_equal(3, len(split(execute('au vimBarTest'), "\n")))
523endfunc
524
525func Test_early_bar()
526 " test that a bar is recognized before the {event}
527 call s:AddAnAutocmd()
Bram Moolenaarb8e642f2021-11-20 10:38:25 +0000528 augroup vimBarTest | au! | let done = 77 | augroup END
Bram Moolenaare99e8442016-07-26 20:43:40 +0200529 call assert_equal(1, len(split(execute('au vimBarTest'), "\n")))
Bram Moolenaarb8e642f2021-11-20 10:38:25 +0000530 call assert_equal(77, done)
Bram Moolenaare99e8442016-07-26 20:43:40 +0200531
532 call s:AddAnAutocmd()
Bram Moolenaarb8e642f2021-11-20 10:38:25 +0000533 augroup vimBarTest| au!| let done = 88 | augroup END
Bram Moolenaare99e8442016-07-26 20:43:40 +0200534 call assert_equal(1, len(split(execute('au vimBarTest'), "\n")))
Bram Moolenaarb8e642f2021-11-20 10:38:25 +0000535 call assert_equal(88, done)
Bram Moolenaare99e8442016-07-26 20:43:40 +0200536
537 " test that a bar is recognized after the {event}
538 call s:AddAnAutocmd()
Bram Moolenaarb8e642f2021-11-20 10:38:25 +0000539 augroup vimBarTest| au!BufReadCmd| let done = 99 | augroup END
Bram Moolenaare99e8442016-07-26 20:43:40 +0200540 call assert_equal(1, len(split(execute('au vimBarTest'), "\n")))
Bram Moolenaarb8e642f2021-11-20 10:38:25 +0000541 call assert_equal(99, done)
Bram Moolenaare99e8442016-07-26 20:43:40 +0200542
543 " test that a bar is recognized after the {group}
544 call s:AddAnAutocmd()
545 au! vimBarTest|echo 'hello'
546 call assert_equal(1, len(split(execute('au vimBarTest'), "\n")))
547endfunc
Bram Moolenaarf2c4c392016-07-29 20:50:24 +0200548
Bram Moolenaar5c809082016-09-01 16:21:48 +0200549func RemoveGroup()
550 autocmd! StartOK
551 augroup! StartOK
552endfunc
553
Bram Moolenaarf2c4c392016-07-29 20:50:24 +0200554func Test_augroup_warning()
555 augroup TheWarning
556 au VimEnter * echo 'entering'
557 augroup END
Bram Moolenaar5dc4e2f2020-11-25 14:15:12 +0100558 call assert_match("TheWarning.*VimEnter", execute('au VimEnter'))
Bram Moolenaarf2c4c392016-07-29 20:50:24 +0200559 redir => res
560 augroup! TheWarning
561 redir END
Bram Moolenaar5dc4e2f2020-11-25 14:15:12 +0100562 call assert_match("W19:", res)
563 call assert_match("-Deleted-.*VimEnter", execute('au VimEnter'))
Bram Moolenaarf2c4c392016-07-29 20:50:24 +0200564
565 " check "Another" does not take the pace of the deleted entry
566 augroup Another
567 augroup END
Bram Moolenaar5dc4e2f2020-11-25 14:15:12 +0100568 call assert_match("-Deleted-.*VimEnter", execute('au VimEnter'))
Bram Moolenaaraeac9002016-09-06 22:15:08 +0200569 augroup! Another
Bram Moolenaar5c809082016-09-01 16:21:48 +0200570
571 " no warning for postpone aucmd delete
572 augroup StartOK
573 au VimEnter * call RemoveGroup()
574 augroup END
Bram Moolenaar5dc4e2f2020-11-25 14:15:12 +0100575 call assert_match("StartOK.*VimEnter", execute('au VimEnter'))
Bram Moolenaar5c809082016-09-01 16:21:48 +0200576 redir => res
577 doautocmd VimEnter
578 redir END
Bram Moolenaar5dc4e2f2020-11-25 14:15:12 +0100579 call assert_notmatch("W19:", res)
Bram Moolenaarde653f02016-09-03 16:59:06 +0200580 au! VimEnter
Bram Moolenaarad48e6c2020-04-21 22:19:45 +0200581
582 call assert_fails('augroup!', 'E471:')
Bram Moolenaarf2c4c392016-07-29 20:50:24 +0200583endfunc
Bram Moolenaarb62cc362016-09-03 16:43:53 +0200584
Bram Moolenaar8d84ff12017-10-26 16:42:16 +0200585func Test_BufReadCmdHelp()
586 " This used to cause access to free memory
587 au BufReadCmd * e +h
588 help
589
Bram Moolenaar8d84ff12017-10-26 16:42:16 +0200590 au! BufReadCmd
591endfunc
592
593func Test_BufReadCmdHelpJump()
594 " This used to cause access to free memory
595 au BufReadCmd * e +h{
Bram Moolenaarcf1ba352017-10-27 00:55:04 +0200596 " } to fix highlighting
597 call assert_fails('help', 'E434:')
Bram Moolenaar8d84ff12017-10-26 16:42:16 +0200598
Bram Moolenaar8d84ff12017-10-26 16:42:16 +0200599 au! BufReadCmd
600endfunc
601
zeertzjq93f72cc2022-08-26 15:34:52 +0100602" BufReadCmd is triggered for a "nofile" buffer. Check all values.
Bram Moolenaarb1d2c812022-08-26 11:55:01 +0100603func Test_BufReadCmdNofile()
zeertzjq93f72cc2022-08-26 15:34:52 +0100604 for val in ['nofile',
605 \ 'nowrite',
606 \ 'acwrite',
607 \ 'quickfix',
608 \ 'help',
609 \ 'terminal',
610 \ 'prompt',
611 \ 'popup',
612 \ ]
613 new somefile
614 exe 'set buftype=' .. val
615 au BufReadCmd somefile call setline(1, 'triggered')
616 edit
617 call assert_equal('triggered', getline(1))
Bram Moolenaarb1d2c812022-08-26 11:55:01 +0100618
zeertzjq93f72cc2022-08-26 15:34:52 +0100619 au! BufReadCmd
620 bwipe!
621 endfor
Bram Moolenaarb1d2c812022-08-26 11:55:01 +0100622endfunc
623
Bram Moolenaarb62cc362016-09-03 16:43:53 +0200624func Test_augroup_deleted()
Bram Moolenaarde653f02016-09-03 16:59:06 +0200625 " This caused a crash before E936 was introduced
Bram Moolenaarb62cc362016-09-03 16:43:53 +0200626 augroup x
Bram Moolenaarde653f02016-09-03 16:59:06 +0200627 call assert_fails('augroup! x', 'E936:')
628 au VimEnter * echo
629 augroup end
Bram Moolenaarb62cc362016-09-03 16:43:53 +0200630 augroup! x
Bram Moolenaar5dc4e2f2020-11-25 14:15:12 +0100631 call assert_match("-Deleted-.*VimEnter", execute('au VimEnter'))
Bram Moolenaarde653f02016-09-03 16:59:06 +0200632 au! VimEnter
Bram Moolenaarb62cc362016-09-03 16:43:53 +0200633endfunc
634
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200635" Tests for autocommands on :close command.
636" This used to be in test13.
637func Test_three_windows()
Bram Moolenaarb3435b02016-09-29 20:54:59 +0200638 " Clean up buffers, because in some cases this function fails.
639 call s:cleanup_buffers()
640
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200641 " Write three files and open them, each in a window.
642 " Then go to next window, with autocommand that deletes the previous one.
643 " Do this twice, writing the file.
644 e! Xtestje1
645 call setline(1, 'testje1')
646 w
647 sp Xtestje2
648 call setline(1, 'testje2')
649 w
650 sp Xtestje3
651 call setline(1, 'testje3')
652 w
653 wincmd w
654 au WinLeave Xtestje2 bwipe
655 wincmd w
656 call assert_equal('Xtestje1', expand('%'))
657
658 au WinLeave Xtestje1 bwipe Xtestje3
659 close
660 call assert_equal('Xtestje1', expand('%'))
661
662 " Test deleting the buffer on a Unload event. If this goes wrong there
663 " will be the ATTENTION prompt.
664 e Xtestje1
665 au!
666 au! BufUnload Xtestje1 bwipe
667 call assert_fails('e Xtestje3', 'E937:')
668 call assert_equal('Xtestje3', expand('%'))
669
670 e Xtestje2
671 sp Xtestje1
672 call assert_fails('e', 'E937:')
Bram Moolenaara997b452018-04-17 23:24:06 +0200673 call assert_equal('Xtestje1', expand('%'))
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200674
675 " Test changing buffers in a BufWipeout autocommand. If this goes wrong
676 " there are ml_line errors and/or a Crash.
677 au!
678 only
679 e Xanother
680 e Xtestje1
681 bwipe Xtestje2
682 bwipe Xtestje3
683 au BufWipeout Xtestje1 buf Xtestje1
684 bwipe
685 call assert_equal('Xanother', expand('%'))
686
687 only
688 help
689 wincmd w
690 1quit
691 call assert_equal('Xanother', expand('%'))
692
693 au!
Bram Moolenaar4520d442017-03-19 16:09:46 +0100694 enew
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200695 call delete('Xtestje1')
696 call delete('Xtestje2')
697 call delete('Xtestje3')
698endfunc
Bram Moolenaare13b9af2017-01-13 22:01:02 +0100699
700func Test_BufEnter()
701 au! BufEnter
702 au Bufenter * let val = val . '+'
703 let g:val = ''
704 split NewFile
705 call assert_equal('+', g:val)
706 bwipe!
707 call assert_equal('++', g:val)
708
709 " Also get BufEnter when editing a directory
Bram Moolenaar6f14da12022-09-07 21:30:44 +0100710 call mkdir('Xbufenterdir', 'D')
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +0100711 split Xbufenterdir
Bram Moolenaare13b9af2017-01-13 22:01:02 +0100712 call assert_equal('+++', g:val)
Bram Moolenaare94260f2017-03-21 15:50:12 +0100713
714 " On MS-Windows we can't edit the directory, make sure we wipe the right
715 " buffer.
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +0100716 bwipe! Xbufenterdir
Bram Moolenaare13b9af2017-01-13 22:01:02 +0100717 au! BufEnter
Bram Moolenaara9b5b852022-08-26 13:16:20 +0100718
719 " Editing a "nofile" buffer doesn't read the file but does trigger BufEnter
zeertzjq93f72cc2022-08-26 15:34:52 +0100720 " for historic reasons. Also test other 'buftype' values.
721 for val in ['nofile',
722 \ 'nowrite',
723 \ 'acwrite',
724 \ 'quickfix',
725 \ 'help',
726 \ 'terminal',
727 \ 'prompt',
728 \ 'popup',
729 \ ]
730 new somefile
731 exe 'set buftype=' .. val
732 au BufEnter somefile call setline(1, 'some text')
733 edit
734 call assert_equal('some text', getline(1))
735 bwipe!
736 au! BufEnter
737 endfor
Bram Moolenaare13b9af2017-01-13 22:01:02 +0100738endfunc
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100739
740" Closing a window might cause an endless loop
741" E814 for older Vims
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200742func Test_autocmd_bufwipe_in_SessLoadPost()
Bram Moolenaar1d68d9b2017-10-13 22:33:32 +0200743 edit Xtest
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100744 tabnew
Bram Moolenaar1d68d9b2017-10-13 22:33:32 +0200745 file Xsomething
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100746 set noswapfile
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100747 mksession!
748
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200749 let content =<< trim [CODE]
Bram Moolenaar62cd26a2020-10-11 20:08:44 +0200750 call test_override('ui_delay', 10)
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200751 set nocp noswapfile
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100752 let v:swapchoice = "e"
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200753 augroup test_autocmd_sessionload
754 autocmd!
755 autocmd SessionLoadPost * exe bufnr("Xsomething") . "bw!"
756 augroup END
757
758 func WriteErrors()
759 call writefile([execute("messages")], "Xerrors")
760 endfunc
761 au VimLeave * call WriteErrors()
762 [CODE]
763
Bram Moolenaare1f3ab72022-09-04 21:29:08 +0100764 call writefile(content, 'Xvimrc', 'D')
Bram Moolenaar93344c22019-08-14 21:12:05 +0200765 call system(GetVimCommand('Xvimrc') .. ' --not-a-term --noplugins -S Session.vim -c cq')
Bram Moolenaare94260f2017-03-21 15:50:12 +0100766 let errors = join(readfile('Xerrors'))
Bram Moolenaare2e40752020-09-04 21:18:46 +0200767 call assert_match('E814:', errors)
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100768
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100769 set swapfile
Bram Moolenaare1f3ab72022-09-04 21:29:08 +0100770 for file in ['Session.vim', 'Xerrors']
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100771 call delete(file)
772 endfor
773endfunc
774
Bram Moolenaar797e63b2021-01-15 16:22:52 +0100775" Using :blast and :ball for many events caused a crash, because b_nwindows was
776" not incremented correctly.
777func Test_autocmd_blast_badd()
778 let content =<< trim [CODE]
779 au BufNew,BufAdd,BufWinEnter,BufEnter,BufLeave,BufWinLeave,BufUnload,VimEnter foo* blast
780 edit foo1
781 au BufNew,BufAdd,BufWinEnter,BufEnter,BufLeave,BufWinLeave,BufUnload,VimEnter foo* ball
782 edit foo2
783 call writefile(['OK'], 'Xerrors')
784 qall
785 [CODE]
786
Bram Moolenaare1f3ab72022-09-04 21:29:08 +0100787 call writefile(content, 'XblastBall', 'D')
Bram Moolenaar797e63b2021-01-15 16:22:52 +0100788 call system(GetVimCommand() .. ' --clean -S XblastBall')
789 call assert_match('OK', readfile('Xerrors')->join())
790
Bram Moolenaar797e63b2021-01-15 16:22:52 +0100791 call delete('Xerrors')
792endfunc
793
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100794" SEGV occurs in older versions.
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200795func Test_autocmd_bufwipe_in_SessLoadPost2()
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100796 tabnew
797 set noswapfile
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100798 mksession!
799
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200800 let content =<< trim [CODE]
801 set nocp noswapfile
802 function! DeleteInactiveBufs()
803 tabfirst
804 let tabblist = []
805 for i in range(1, tabpagenr(''$''))
806 call extend(tabblist, tabpagebuflist(i))
807 endfor
808 for b in range(1, bufnr(''$''))
809 if bufexists(b) && buflisted(b) && (index(tabblist, b) == -1 || bufname(b) =~# ''^$'')
810 exec ''bwipeout '' . b
811 endif
812 endfor
813 echomsg "SessionLoadPost DONE"
814 endfunction
815 au SessionLoadPost * call DeleteInactiveBufs()
816
817 func WriteErrors()
818 call writefile([execute("messages")], "Xerrors")
819 endfunc
820 au VimLeave * call WriteErrors()
821 [CODE]
822
Bram Moolenaare1f3ab72022-09-04 21:29:08 +0100823 call writefile(content, 'Xvimrc', 'D')
Bram Moolenaar93344c22019-08-14 21:12:05 +0200824 call system(GetVimCommand('Xvimrc') .. ' --not-a-term --noplugins -S Session.vim -c cq')
Bram Moolenaare94260f2017-03-21 15:50:12 +0100825 let errors = join(readfile('Xerrors'))
826 " This probably only ever matches on unix.
827 call assert_notmatch('Caught deadly signal SEGV', errors)
828 call assert_match('SessionLoadPost DONE', errors)
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100829
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100830 set swapfile
Bram Moolenaare1f3ab72022-09-04 21:29:08 +0100831 for file in ['Session.vim', 'Xerrors']
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100832 call delete(file)
833 endfor
834endfunc
Bram Moolenaarfaf29d72017-07-09 11:07:16 +0200835
836func Test_empty_doau()
837 doau \|
838endfunc
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200839
840func s:AutoCommandOptionSet(match)
Bram Moolenaard7c96872019-06-15 17:12:48 +0200841 let template = "Option: <%s>, OldVal: <%s>, OldValLocal: <%s>, OldValGlobal: <%s>, NewVal: <%s>, Scope: <%s>, Command: <%s>\n"
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200842 let item = remove(g:options, 0)
Bram Moolenaard7c96872019-06-15 17:12:48 +0200843 let expected = printf(template, item[0], item[1], item[2], item[3], item[4], item[5], item[6])
844 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 +0200845 let g:opt = [expected, actual]
846 "call assert_equal(expected, actual)
847endfunc
848
849func Test_OptionSet()
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +0200850 CheckOption autochdir
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200851
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +0200852 badd test_autocmd.vim
853
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200854 call test_override('starting', 1)
855 set nocp
856 au OptionSet * :call s:AutoCommandOptionSet(expand("<amatch>"))
857
858 " 1: Setting number option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100859 let g:options = [['number', 0, 0, 0, 1, 'global', 'set']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200860 set nu
861 call assert_equal([], g:options)
862 call assert_equal(g:opt[0], g:opt[1])
863
864 " 2: Setting local number option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100865 let g:options = [['number', 1, 1, '', 0, 'local', 'setlocal']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200866 setlocal nonu
867 call assert_equal([], g:options)
868 call assert_equal(g:opt[0], g:opt[1])
869
870 " 3: Setting global number option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100871 let g:options = [['number', 1, '', 1, 0, 'global', 'setglobal']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200872 setglobal nonu
873 call assert_equal([], g:options)
874 call assert_equal(g:opt[0], g:opt[1])
875
876 " 4: Setting local autoindent option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100877 let g:options = [['autoindent', 0, 0, '', 1, 'local', 'setlocal']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200878 setlocal ai
879 call assert_equal([], g:options)
880 call assert_equal(g:opt[0], g:opt[1])
881
882 " 5: Setting global autoindent option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100883 let g:options = [['autoindent', 0, '', 0, 1, 'global', 'setglobal']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200884 setglobal ai
885 call assert_equal([], g:options)
886 call assert_equal(g:opt[0], g:opt[1])
887
888 " 6: Setting global autoindent option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100889 let g:options = [['autoindent', 1, 1, 1, 0, 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +0200890 set ai!
891 call assert_equal([], g:options)
892 call assert_equal(g:opt[0], g:opt[1])
893
894 " 6a: Setting global autoindent option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100895 let g:options = [['autoindent', 1, 1, 0, 0, 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +0200896 noa setlocal ai
897 noa setglobal noai
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200898 set ai!
899 call assert_equal([], g:options)
900 call assert_equal(g:opt[0], g:opt[1])
901
902 " Should not print anything, use :noa
903 " 7: don't trigger OptionSet"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100904 let g:options = [['invalid', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200905 noa set nonu
Bram Moolenaard7c96872019-06-15 17:12:48 +0200906 call assert_equal([['invalid', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid']], g:options)
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200907 call assert_equal(g:opt[0], g:opt[1])
908
909 " 8: Setting several global list and number option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100910 let g:options = [['list', 0, 0, 0, 1, 'global', 'set'], ['number', 0, 0, 0, 1, 'global', 'set']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200911 set list nu
912 call assert_equal([], g:options)
913 call assert_equal(g:opt[0], g:opt[1])
914
915 " 9: don't trigger OptionSet"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100916 let g:options = [['invalid', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid'], ['invalid', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200917 noa set nolist nonu
Bram Moolenaard7c96872019-06-15 17:12:48 +0200918 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 +0200919 call assert_equal(g:opt[0], g:opt[1])
920
921 " 10: Setting global acd"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100922 let g:options = [['autochdir', 0, 0, '', 1, 'local', 'setlocal']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200923 setlocal acd
924 call assert_equal([], g:options)
925 call assert_equal(g:opt[0], g:opt[1])
926
927 " 11: Setting global autoread (also sets local value)"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100928 let g:options = [['autoread', 0, 0, 0, 1, 'global', 'set']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200929 set ar
930 call assert_equal([], g:options)
931 call assert_equal(g:opt[0], g:opt[1])
932
933 " 12: Setting local autoread"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100934 let g:options = [['autoread', 1, 1, '', 1, 'local', 'setlocal']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200935 setlocal ar
936 call assert_equal([], g:options)
937 call assert_equal(g:opt[0], g:opt[1])
938
939 " 13: Setting global autoread"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100940 let g:options = [['autoread', 1, '', 1, 0, 'global', 'setglobal']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200941 setglobal invar
942 call assert_equal([], g:options)
943 call assert_equal(g:opt[0], g:opt[1])
944
945 " 14: Setting option backspace through :let"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100946 let g:options = [['backspace', '', '', '', 'eol,indent,start', 'global', 'set']]
947 let &bs = "eol,indent,start"
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200948 call assert_equal([], g:options)
949 call assert_equal(g:opt[0], g:opt[1])
950
951 " 15: Setting option backspace through setbufvar()"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100952 let g:options = [['backup', 0, 0, '', 1, 'local', 'setlocal']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200953 " try twice, first time, shouldn't trigger because option name is invalid,
954 " second time, it should trigger
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +0200955 let bnum = bufnr('%')
Bram Moolenaare2e40752020-09-04 21:18:46 +0200956 call assert_fails("call setbufvar(bnum, '&l:bk', 1)", 'E355:')
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200957 " should trigger, use correct option name
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +0200958 call setbufvar(bnum, '&backup', 1)
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200959 call assert_equal([], g:options)
960 call assert_equal(g:opt[0], g:opt[1])
961
962 " 16: Setting number option using setwinvar"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100963 let g:options = [['number', 0, 0, '', 1, 'local', 'setlocal']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200964 call setwinvar(0, '&number', 1)
965 call assert_equal([], g:options)
966 call assert_equal(g:opt[0], g:opt[1])
967
968 " 17: Setting key option, shouldn't trigger"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100969 let g:options = [['key', 'invalid', 'invalid1', 'invalid2', 'invalid3', 'invalid4', 'invalid5']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200970 setlocal key=blah
971 setlocal key=
Bram Moolenaard7c96872019-06-15 17:12:48 +0200972 call assert_equal([['key', 'invalid', 'invalid1', 'invalid2', 'invalid3', 'invalid4', 'invalid5']], g:options)
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200973 call assert_equal(g:opt[0], g:opt[1])
974
Bram Moolenaard7c96872019-06-15 17:12:48 +0200975
976 " 18a: Setting string global option"
977 let oldval = &backupext
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100978 let g:options = [['backupext', oldval, oldval, oldval, 'foo', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +0200979 set backupext=foo
980 call assert_equal([], g:options)
981 call assert_equal(g:opt[0], g:opt[1])
982
983 " 18b: Resetting string global option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100984 let g:options = [['backupext', 'foo', 'foo', 'foo', oldval, 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +0200985 set backupext&
986 call assert_equal([], g:options)
987 call assert_equal(g:opt[0], g:opt[1])
988
989 " 18c: Setting global string global option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100990 let g:options = [['backupext', oldval, '', oldval, 'bar', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +0200991 setglobal backupext=bar
992 call assert_equal([], g:options)
993 call assert_equal(g:opt[0], g:opt[1])
994
995 " 18d: Setting local string global option"
996 " As this is a global option this sets the global value even though
997 " :setlocal is used!
998 noa set backupext& " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100999 let g:options = [['backupext', oldval, oldval, '', 'baz', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001000 setlocal backupext=baz
1001 call assert_equal([], g:options)
1002 call assert_equal(g:opt[0], g:opt[1])
1003
1004 " 18e: Setting again string global option"
1005 noa setglobal backupext=ext_global " Reset global and local value (without triggering autocmd)
1006 noa setlocal backupext=ext_local " Sets the global(!) value!
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001007 let g:options = [['backupext', 'ext_local', 'ext_local', 'ext_local', 'fuu', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001008 set backupext=fuu
1009 call assert_equal([], g:options)
1010 call assert_equal(g:opt[0], g:opt[1])
1011
1012
zeertzjqb811de52021-10-21 10:50:44 +01001013 " 19a: Setting string global-local (to buffer) option"
Bram Moolenaar8efa0262017-08-20 15:47:20 +02001014 let oldval = &tags
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001015 let g:options = [['tags', oldval, oldval, oldval, 'tagpath', 'global', 'set']]
Bram Moolenaar8efa0262017-08-20 15:47:20 +02001016 set tags=tagpath
1017 call assert_equal([], g:options)
1018 call assert_equal(g:opt[0], g:opt[1])
1019
zeertzjqb811de52021-10-21 10:50:44 +01001020 " 19b: Resetting string global-local (to buffer) option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001021 let g:options = [['tags', 'tagpath', 'tagpath', 'tagpath', oldval, 'global', 'set']]
Bram Moolenaar8efa0262017-08-20 15:47:20 +02001022 set tags&
1023 call assert_equal([], g:options)
1024 call assert_equal(g:opt[0], g:opt[1])
1025
zeertzjqb811de52021-10-21 10:50:44 +01001026 " 19c: Setting global string global-local (to buffer) option "
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001027 let g:options = [['tags', oldval, '', oldval, 'tagpath1', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001028 setglobal tags=tagpath1
1029 call assert_equal([], g:options)
1030 call assert_equal(g:opt[0], g:opt[1])
1031
zeertzjqb811de52021-10-21 10:50:44 +01001032 " 19d: Setting local string global-local (to buffer) option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001033 let g:options = [['tags', 'tagpath1', 'tagpath1', '', 'tagpath2', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001034 setlocal tags=tagpath2
1035 call assert_equal([], g:options)
1036 call assert_equal(g:opt[0], g:opt[1])
1037
zeertzjqb811de52021-10-21 10:50:44 +01001038 " 19e: Setting again string global-local (to buffer) option"
1039 " Note: v:option_old is the old global value for global-local string options
Bram Moolenaard7c96872019-06-15 17:12:48 +02001040 " but the old local value for all other kinds of options.
1041 noa setglobal tags=tag_global " Reset global and local value (without triggering autocmd)
1042 noa setlocal tags=tag_local
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001043 let g:options = [['tags', 'tag_global', 'tag_local', 'tag_global', 'tagpath', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001044 set tags=tagpath
1045 call assert_equal([], g:options)
1046 call assert_equal(g:opt[0], g:opt[1])
1047
zeertzjqb811de52021-10-21 10:50:44 +01001048 " 19f: Setting string global-local (to buffer) option to an empty string"
1049 " Note: v:option_old is the old global value for global-local string options
Bram Moolenaard7c96872019-06-15 17:12:48 +02001050 " but the old local value for all other kinds of options.
1051 noa set tags=tag_global " Reset global and local value (without triggering autocmd)
1052 noa setlocal tags= " empty string
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001053 let g:options = [['tags', 'tag_global', '', 'tag_global', 'tagpath', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001054 set tags=tagpath
1055 call assert_equal([], g:options)
1056 call assert_equal(g:opt[0], g:opt[1])
1057
1058
1059 " 20a: Setting string local (to buffer) option"
1060 let oldval = &spelllang
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001061 let g:options = [['spelllang', oldval, oldval, oldval, 'elvish,klingon', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001062 set spelllang=elvish,klingon
1063 call assert_equal([], g:options)
1064 call assert_equal(g:opt[0], g:opt[1])
1065
1066 " 20b: Resetting string local (to buffer) option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001067 let g:options = [['spelllang', 'elvish,klingon', 'elvish,klingon', 'elvish,klingon', oldval, 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001068 set spelllang&
1069 call assert_equal([], g:options)
1070 call assert_equal(g:opt[0], g:opt[1])
1071
1072 " 20c: Setting global string local (to buffer) option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001073 let g:options = [['spelllang', oldval, '', oldval, 'elvish', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001074 setglobal spelllang=elvish
1075 call assert_equal([], g:options)
1076 call assert_equal(g:opt[0], g:opt[1])
1077
1078 " 20d: Setting local string local (to buffer) option"
1079 noa set spelllang& " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001080 let g:options = [['spelllang', oldval, oldval, '', 'klingon', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001081 setlocal spelllang=klingon
1082 call assert_equal([], g:options)
1083 call assert_equal(g:opt[0], g:opt[1])
1084
1085 " 20e: Setting again string local (to buffer) option"
zeertzjqb811de52021-10-21 10:50:44 +01001086 " Note: v:option_old is the old global value for global-local string options
Bram Moolenaard7c96872019-06-15 17:12:48 +02001087 " but the old local value for all other kinds of options.
1088 noa setglobal spelllang=spellglobal " Reset global and local value (without triggering autocmd)
1089 noa setlocal spelllang=spelllocal
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001090 let g:options = [['spelllang', 'spelllocal', 'spelllocal', 'spellglobal', 'foo', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001091 set spelllang=foo
1092 call assert_equal([], g:options)
1093 call assert_equal(g:opt[0], g:opt[1])
1094
1095
zeertzjqb811de52021-10-21 10:50:44 +01001096 " 21a: Setting string global-local (to window) option"
Bram Moolenaard7c96872019-06-15 17:12:48 +02001097 let oldval = &statusline
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001098 let g:options = [['statusline', oldval, oldval, oldval, 'foo', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001099 set statusline=foo
1100 call assert_equal([], g:options)
1101 call assert_equal(g:opt[0], g:opt[1])
1102
zeertzjqb811de52021-10-21 10:50:44 +01001103 " 21b: Resetting string global-local (to window) option"
1104 " Note: v:option_old is the old global value for global-local string options
Bram Moolenaard7c96872019-06-15 17:12:48 +02001105 " but the old local value for all other kinds of options.
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001106 let g:options = [['statusline', 'foo', 'foo', 'foo', oldval, 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001107 set statusline&
1108 call assert_equal([], g:options)
1109 call assert_equal(g:opt[0], g:opt[1])
1110
zeertzjqb811de52021-10-21 10:50:44 +01001111 " 21c: Setting global string global-local (to window) option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001112 let g:options = [['statusline', oldval, '', oldval, 'bar', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001113 setglobal statusline=bar
1114 call assert_equal([], g:options)
1115 call assert_equal(g:opt[0], g:opt[1])
1116
zeertzjqb811de52021-10-21 10:50:44 +01001117 " 21d: Setting local string global-local (to window) option"
Bram Moolenaard7c96872019-06-15 17:12:48 +02001118 noa set statusline& " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001119 let g:options = [['statusline', oldval, oldval, '', 'baz', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001120 setlocal statusline=baz
1121 call assert_equal([], g:options)
1122 call assert_equal(g:opt[0], g:opt[1])
1123
zeertzjqb811de52021-10-21 10:50:44 +01001124 " 21e: Setting again string global-local (to window) option"
1125 " Note: v:option_old is the old global value for global-local string options
Bram Moolenaard7c96872019-06-15 17:12:48 +02001126 " but the old local value for all other kinds of options.
1127 noa setglobal statusline=bar " Reset global and local value (without triggering autocmd)
1128 noa setlocal statusline=baz
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001129 let g:options = [['statusline', 'bar', 'baz', 'bar', 'foo', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001130 set statusline=foo
1131 call assert_equal([], g:options)
1132 call assert_equal(g:opt[0], g:opt[1])
1133
1134
1135 " 22a: Setting string local (to window) option"
1136 let oldval = &foldignore
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001137 let g:options = [['foldignore', oldval, oldval, oldval, 'fo', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001138 set foldignore=fo
1139 call assert_equal([], g:options)
1140 call assert_equal(g:opt[0], g:opt[1])
1141
1142 " 22b: Resetting string local (to window) option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001143 let g:options = [['foldignore', 'fo', 'fo', 'fo', oldval, 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001144 set foldignore&
1145 call assert_equal([], g:options)
1146 call assert_equal(g:opt[0], g:opt[1])
1147
1148 " 22c: Setting global string local (to window) option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001149 let g:options = [['foldignore', oldval, '', oldval, 'bar', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001150 setglobal foldignore=bar
1151 call assert_equal([], g:options)
1152 call assert_equal(g:opt[0], g:opt[1])
1153
1154 " 22d: Setting local string local (to window) option"
1155 noa set foldignore& " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001156 let g:options = [['foldignore', oldval, oldval, '', 'baz', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001157 setlocal foldignore=baz
1158 call assert_equal([], g:options)
1159 call assert_equal(g:opt[0], g:opt[1])
1160
1161 " 22e: Setting again string local (to window) option"
1162 noa setglobal foldignore=glob " Reset global and local value (without triggering autocmd)
1163 noa setlocal foldignore=loc
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001164 let g:options = [['foldignore', 'loc', 'loc', 'glob', 'fo', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001165 set foldignore=fo
1166 call assert_equal([], g:options)
1167 call assert_equal(g:opt[0], g:opt[1])
1168
1169
zeertzjqb811de52021-10-21 10:50:44 +01001170 " 23a: Setting global number global option"
Bram Moolenaard7c96872019-06-15 17:12:48 +02001171 noa setglobal cmdheight=8 " Reset global and local value (without triggering autocmd)
1172 noa setlocal cmdheight=1 " Sets the global(!) value!
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001173 let g:options = [['cmdheight', '1', '', '1', '2', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001174 setglobal cmdheight=2
1175 call assert_equal([], g:options)
1176 call assert_equal(g:opt[0], g:opt[1])
1177
1178 " 23b: Setting local number global option"
1179 noa setglobal cmdheight=8 " Reset global and local value (without triggering autocmd)
1180 noa setlocal cmdheight=1 " Sets the global(!) value!
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001181 let g:options = [['cmdheight', '1', '1', '', '2', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001182 setlocal cmdheight=2
1183 call assert_equal([], g:options)
1184 call assert_equal(g:opt[0], g:opt[1])
1185
1186 " 23c: Setting again number global option"
1187 noa setglobal cmdheight=8 " Reset global and local value (without triggering autocmd)
1188 noa setlocal cmdheight=1 " Sets the global(!) value!
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001189 let g:options = [['cmdheight', '1', '1', '1', '2', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001190 set cmdheight=2
1191 call assert_equal([], g:options)
1192 call assert_equal(g:opt[0], g:opt[1])
1193
1194 " 23d: Setting again number global option"
1195 noa set cmdheight=8 " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001196 let g:options = [['cmdheight', '8', '8', '8', '2', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001197 set cmdheight=2
1198 call assert_equal([], g:options)
1199 call assert_equal(g:opt[0], g:opt[1])
1200
1201
1202 " 24a: Setting global number global-local (to buffer) option"
1203 noa setglobal undolevels=8 " Reset global and local value (without triggering autocmd)
1204 noa setlocal undolevels=1
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001205 let g:options = [['undolevels', '8', '', '8', '2', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001206 setglobal undolevels=2
1207 call assert_equal([], g:options)
1208 call assert_equal(g:opt[0], g:opt[1])
1209
1210 " 24b: Setting local number global-local (to buffer) option"
1211 noa setglobal undolevels=8 " Reset global and local value (without triggering autocmd)
1212 noa setlocal undolevels=1
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001213 let g:options = [['undolevels', '1', '1', '', '2', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001214 setlocal undolevels=2
1215 call assert_equal([], g:options)
1216 call assert_equal(g:opt[0], g:opt[1])
1217
1218 " 24c: Setting again number global-local (to buffer) option"
1219 noa setglobal undolevels=8 " Reset global and local value (without triggering autocmd)
1220 noa setlocal undolevels=1
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001221 let g:options = [['undolevels', '1', '1', '8', '2', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001222 set undolevels=2
1223 call assert_equal([], g:options)
1224 call assert_equal(g:opt[0], g:opt[1])
1225
1226 " 24d: Setting again global number global-local (to buffer) option"
1227 noa set undolevels=8 " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001228 let g:options = [['undolevels', '8', '8', '8', '2', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001229 set undolevels=2
1230 call assert_equal([], g:options)
1231 call assert_equal(g:opt[0], g:opt[1])
1232
1233
1234 " 25a: Setting global number local (to buffer) option"
1235 noa setglobal wrapmargin=8 " Reset global and local value (without triggering autocmd)
1236 noa setlocal wrapmargin=1
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001237 let g:options = [['wrapmargin', '8', '', '8', '2', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001238 setglobal wrapmargin=2
1239 call assert_equal([], g:options)
1240 call assert_equal(g:opt[0], g:opt[1])
1241
1242 " 25b: Setting local number local (to buffer) option"
1243 noa setglobal wrapmargin=8 " Reset global and local value (without triggering autocmd)
1244 noa setlocal wrapmargin=1
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001245 let g:options = [['wrapmargin', '1', '1', '', '2', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001246 setlocal wrapmargin=2
1247 call assert_equal([], g:options)
1248 call assert_equal(g:opt[0], g:opt[1])
1249
1250 " 25c: Setting again number local (to buffer) option"
1251 noa setglobal wrapmargin=8 " Reset global and local value (without triggering autocmd)
1252 noa setlocal wrapmargin=1
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001253 let g:options = [['wrapmargin', '1', '1', '8', '2', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001254 set wrapmargin=2
1255 call assert_equal([], g:options)
1256 call assert_equal(g:opt[0], g:opt[1])
1257
1258 " 25d: Setting again global number local (to buffer) option"
1259 noa set wrapmargin=8 " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001260 let g:options = [['wrapmargin', '8', '8', '8', '2', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001261 set wrapmargin=2
1262 call assert_equal([], g:options)
1263 call assert_equal(g:opt[0], g:opt[1])
1264
1265
1266 " 26: Setting number global-local (to window) option.
1267 " Such option does currently not exist.
1268
1269
1270 " 27a: Setting global number local (to window) option"
1271 noa setglobal foldcolumn=8 " Reset global and local value (without triggering autocmd)
1272 noa setlocal foldcolumn=1
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001273 let g:options = [['foldcolumn', '8', '', '8', '2', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001274 setglobal foldcolumn=2
1275 call assert_equal([], g:options)
1276 call assert_equal(g:opt[0], g:opt[1])
1277
1278 " 27b: Setting local number local (to window) option"
1279 noa setglobal foldcolumn=8 " Reset global and local value (without triggering autocmd)
1280 noa setlocal foldcolumn=1
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001281 let g:options = [['foldcolumn', '1', '1', '', '2', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001282 setlocal foldcolumn=2
1283 call assert_equal([], g:options)
1284 call assert_equal(g:opt[0], g:opt[1])
1285
1286 " 27c: Setting again number local (to window) option"
1287 noa setglobal foldcolumn=8 " Reset global and local value (without triggering autocmd)
1288 noa setlocal foldcolumn=1
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001289 let g:options = [['foldcolumn', '1', '1', '8', '2', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001290 set foldcolumn=2
1291 call assert_equal([], g:options)
1292 call assert_equal(g:opt[0], g:opt[1])
1293
zeertzjqb811de52021-10-21 10:50:44 +01001294 " 27d: Setting again global number local (to window) option"
Bram Moolenaard7c96872019-06-15 17:12:48 +02001295 noa set foldcolumn=8 " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001296 let g:options = [['foldcolumn', '8', '8', '8', '2', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001297 set foldcolumn=2
1298 call assert_equal([], g:options)
1299 call assert_equal(g:opt[0], g:opt[1])
1300
1301
1302 " 28a: Setting global boolean global option"
1303 noa setglobal nowrapscan " Reset global and local value (without triggering autocmd)
1304 noa setlocal wrapscan " Sets the global(!) value!
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001305 let g:options = [['wrapscan', '1', '', '1', '0', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001306 setglobal nowrapscan
1307 call assert_equal([], g:options)
1308 call assert_equal(g:opt[0], g:opt[1])
1309
1310 " 28b: Setting local boolean global option"
1311 noa setglobal nowrapscan " Reset global and local value (without triggering autocmd)
1312 noa setlocal wrapscan " Sets the global(!) value!
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001313 let g:options = [['wrapscan', '1', '1', '', '0', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001314 setlocal nowrapscan
1315 call assert_equal([], g:options)
1316 call assert_equal(g:opt[0], g:opt[1])
1317
1318 " 28c: Setting again boolean global option"
1319 noa setglobal nowrapscan " Reset global and local value (without triggering autocmd)
1320 noa setlocal wrapscan " Sets the global(!) value!
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001321 let g:options = [['wrapscan', '1', '1', '1', '0', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001322 set nowrapscan
1323 call assert_equal([], g:options)
1324 call assert_equal(g:opt[0], g:opt[1])
1325
1326 " 28d: Setting again global boolean global option"
1327 noa set nowrapscan " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001328 let g:options = [['wrapscan', '0', '0', '0', '1', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001329 set wrapscan
1330 call assert_equal([], g:options)
1331 call assert_equal(g:opt[0], g:opt[1])
1332
1333
1334 " 29a: Setting global boolean global-local (to buffer) option"
1335 noa setglobal noautoread " Reset global and local value (without triggering autocmd)
1336 noa setlocal autoread
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001337 let g:options = [['autoread', '0', '', '0', '1', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001338 setglobal autoread
1339 call assert_equal([], g:options)
1340 call assert_equal(g:opt[0], g:opt[1])
1341
1342 " 29b: Setting local boolean global-local (to buffer) option"
1343 noa setglobal noautoread " Reset global and local value (without triggering autocmd)
1344 noa setlocal autoread
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001345 let g:options = [['autoread', '1', '1', '', '0', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001346 setlocal noautoread
1347 call assert_equal([], g:options)
1348 call assert_equal(g:opt[0], g:opt[1])
1349
1350 " 29c: Setting again boolean global-local (to buffer) option"
1351 noa setglobal noautoread " Reset global and local value (without triggering autocmd)
1352 noa setlocal autoread
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001353 let g:options = [['autoread', '1', '1', '0', '1', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001354 set autoread
1355 call assert_equal([], g:options)
1356 call assert_equal(g:opt[0], g:opt[1])
1357
1358 " 29d: Setting again global boolean global-local (to buffer) option"
1359 noa set noautoread " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001360 let g:options = [['autoread', '0', '0', '0', '1', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001361 set autoread
1362 call assert_equal([], g:options)
1363 call assert_equal(g:opt[0], g:opt[1])
1364
1365
1366 " 30a: Setting global boolean local (to buffer) option"
1367 noa setglobal nocindent " Reset global and local value (without triggering autocmd)
1368 noa setlocal cindent
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001369 let g:options = [['cindent', '0', '', '0', '1', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001370 setglobal cindent
1371 call assert_equal([], g:options)
1372 call assert_equal(g:opt[0], g:opt[1])
1373
1374 " 30b: Setting local boolean local (to buffer) option"
1375 noa setglobal nocindent " Reset global and local value (without triggering autocmd)
1376 noa setlocal cindent
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001377 let g:options = [['cindent', '1', '1', '', '0', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001378 setlocal nocindent
1379 call assert_equal([], g:options)
1380 call assert_equal(g:opt[0], g:opt[1])
1381
1382 " 30c: Setting again boolean local (to buffer) option"
1383 noa setglobal nocindent " Reset global and local value (without triggering autocmd)
1384 noa setlocal cindent
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001385 let g:options = [['cindent', '1', '1', '0', '1', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001386 set cindent
1387 call assert_equal([], g:options)
1388 call assert_equal(g:opt[0], g:opt[1])
1389
1390 " 30d: Setting again global boolean local (to buffer) option"
1391 noa set nocindent " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001392 let g:options = [['cindent', '0', '0', '0', '1', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001393 set cindent
1394 call assert_equal([], g:options)
1395 call assert_equal(g:opt[0], g:opt[1])
1396
1397
1398 " 31: Setting boolean global-local (to window) option
1399 " Currently no such option exists.
1400
1401
1402 " 32a: Setting global boolean local (to window) option"
1403 noa setglobal nocursorcolumn " Reset global and local value (without triggering autocmd)
1404 noa setlocal cursorcolumn
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001405 let g:options = [['cursorcolumn', '0', '', '0', '1', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001406 setglobal cursorcolumn
1407 call assert_equal([], g:options)
1408 call assert_equal(g:opt[0], g:opt[1])
1409
1410 " 32b: Setting local boolean local (to window) option"
1411 noa setglobal nocursorcolumn " Reset global and local value (without triggering autocmd)
1412 noa setlocal cursorcolumn
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001413 let g:options = [['cursorcolumn', '1', '1', '', '0', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001414 setlocal nocursorcolumn
1415 call assert_equal([], g:options)
1416 call assert_equal(g:opt[0], g:opt[1])
1417
1418 " 32c: Setting again boolean local (to window) option"
1419 noa setglobal nocursorcolumn " Reset global and local value (without triggering autocmd)
1420 noa setlocal cursorcolumn
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001421 let g:options = [['cursorcolumn', '1', '1', '0', '1', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001422 set cursorcolumn
1423 call assert_equal([], g:options)
1424 call assert_equal(g:opt[0], g:opt[1])
1425
1426 " 32d: Setting again global boolean local (to window) option"
1427 noa set nocursorcolumn " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001428 let g:options = [['cursorcolumn', '0', '0', '0', '1', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001429 set cursorcolumn
1430 call assert_equal([], g:options)
1431 call assert_equal(g:opt[0], g:opt[1])
1432
1433
Bram Moolenaar1bc353b2019-09-01 14:45:28 +02001434 " 33: Test autocommands when an option value is converted internally.
Bram Moolenaard7c96872019-06-15 17:12:48 +02001435 noa set backspace=1 " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001436 let g:options = [['backspace', 'indent,eol', 'indent,eol', 'indent,eol', '2', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001437 set backspace=2
1438 call assert_equal([], g:options)
1439 call assert_equal(g:opt[0], g:opt[1])
1440
1441
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001442 " Cleanup
1443 au! OptionSet
Bram Moolenaar0331faf2019-06-15 18:40:37 +02001444 " set tags&
Bram Moolenaard7c96872019-06-15 17:12:48 +02001445 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 +02001446 exe printf(":set %s&vim", opt)
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001447 endfor
1448 call test_override('starting', 0)
1449 delfunc! AutoCommandOptionSet
1450endfunc
1451
1452func Test_OptionSet_diffmode()
1453 call test_override('starting', 1)
Bram Moolenaar26d98212019-01-27 22:32:55 +01001454 " 18: Changing an option when entering diff mode
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001455 new
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001456 au OptionSet diff :let &l:cul = v:option_new
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001457
1458 call setline(1, ['buffer 1', 'line2', 'line3', 'line4'])
1459 call assert_equal(0, &l:cul)
1460 diffthis
1461 call assert_equal(1, &l:cul)
1462
1463 vnew
1464 call setline(1, ['buffer 2', 'line 2', 'line 3', 'line4'])
1465 call assert_equal(0, &l:cul)
1466 diffthis
1467 call assert_equal(1, &l:cul)
1468
1469 diffoff
1470 call assert_equal(0, &l:cul)
1471 call assert_equal(1, getwinvar(2, '&l:cul'))
1472 bw!
1473
1474 call assert_equal(1, &l:cul)
1475 diffoff!
1476 call assert_equal(0, &l:cul)
1477 call assert_equal(0, getwinvar(1, '&l:cul'))
1478 bw!
1479
1480 " Cleanup
1481 au! OptionSet
1482 call test_override('starting', 0)
1483endfunc
1484
1485func Test_OptionSet_diffmode_close()
1486 call test_override('starting', 1)
1487 " 19: Try to close the current window when entering diff mode
1488 " should not segfault
1489 new
1490 au OptionSet diff close
1491
1492 call setline(1, ['buffer 1', 'line2', 'line3', 'line4'])
Bram Moolenaare2e40752020-09-04 21:18:46 +02001493 call assert_fails(':diffthis', 'E788:')
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001494 call assert_equal(1, &diff)
1495 vnew
1496 call setline(1, ['buffer 2', 'line 2', 'line 3', 'line4'])
Bram Moolenaare2e40752020-09-04 21:18:46 +02001497 call assert_fails(':diffthis', 'E788:')
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001498 call assert_equal(1, &diff)
Bram Moolenaara9aa86f2019-11-10 21:25:45 +01001499 set diffopt-=closeoff
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001500 bw!
Bram Moolenaare2e40752020-09-04 21:18:46 +02001501 call assert_fails(':diffoff!', 'E788:')
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001502 bw!
1503
1504 " Cleanup
1505 au! OptionSet
1506 call test_override('starting', 0)
1507 "delfunc! AutoCommandOptionSet
1508endfunc
Bram Moolenaar4a137b42017-08-04 22:37:11 +02001509
1510" Test for Bufleave autocommand that deletes the buffer we are about to edit.
1511func Test_BufleaveWithDelete()
Bram Moolenaare7cda972022-08-29 11:02:59 +01001512 new | edit XbufLeave1
Bram Moolenaar4a137b42017-08-04 22:37:11 +02001513
1514 augroup test_bufleavewithdelete
1515 autocmd!
Bram Moolenaare7cda972022-08-29 11:02:59 +01001516 autocmd BufLeave XbufLeave1 bwipe XbufLeave2
Bram Moolenaar4a137b42017-08-04 22:37:11 +02001517 augroup END
1518
Bram Moolenaare7cda972022-08-29 11:02:59 +01001519 call assert_fails('edit XbufLeave2', 'E143:')
1520 call assert_equal('XbufLeave1', bufname('%'))
Bram Moolenaar4a137b42017-08-04 22:37:11 +02001521
Bram Moolenaare7cda972022-08-29 11:02:59 +01001522 autocmd! test_bufleavewithdelete BufLeave XbufLeave1
Bram Moolenaar4a137b42017-08-04 22:37:11 +02001523 augroup! test_bufleavewithdelete
1524
1525 new
Bram Moolenaare7cda972022-08-29 11:02:59 +01001526 bwipe! XbufLeave1
Bram Moolenaar4a137b42017-08-04 22:37:11 +02001527endfunc
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +02001528
1529" Test for autocommand that changes the buffer list, when doing ":ball".
1530func Test_Acmd_BufAll()
1531 enew!
1532 %bwipe!
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01001533 call writefile(['Test file Xxx1'], 'Xxx1', 'D')
1534 call writefile(['Test file Xxx2'], 'Xxx2', 'D')
1535 call writefile(['Test file Xxx3'], 'Xxx3', 'D')
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +02001536
1537 " Add three files to the buffer list
1538 split Xxx1
1539 close
1540 split Xxx2
1541 close
1542 split Xxx3
1543 close
1544
1545 " Wipe the buffer when the buffer is opened
1546 au BufReadPost Xxx2 bwipe
1547
1548 call append(0, 'Test file Xxx4')
1549 ball
1550
1551 call assert_equal(2, winnr('$'))
1552 call assert_equal('Xxx1', bufname(winbufnr(winnr('$'))))
1553 wincmd t
1554
1555 au! BufReadPost
1556 %bwipe!
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +02001557 enew! | only
1558endfunc
1559
1560" Test for autocommand that changes current buffer on BufEnter event.
1561" Check if modelines are interpreted for the correct buffer.
1562func Test_Acmd_BufEnter()
1563 %bwipe!
1564 call writefile(['start of test file Xxx1',
1565 \ "\<Tab>this is a test",
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01001566 \ 'end of test file Xxx1'], 'Xxx1', 'D')
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +02001567 call writefile(['start of test file Xxx2',
1568 \ 'vim: set noai :',
1569 \ "\<Tab>this is a test",
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01001570 \ 'end of test file Xxx2'], 'Xxx2', 'D')
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +02001571
1572 au BufEnter Xxx2 brew
1573 set ai modeline modelines=3
1574 edit Xxx1
1575 " edit Xxx2, autocmd will do :brew
1576 edit Xxx2
1577 exe "normal G?this is a\<CR>"
1578 " Append text with autoindent to this file
1579 normal othis should be auto-indented
1580 call assert_equal("\<Tab>this should be auto-indented", getline('.'))
1581 call assert_equal(3, line('.'))
1582 " Remove autocmd and edit Xxx2 again
1583 au! BufEnter Xxx2
1584 buf! Xxx2
1585 exe "normal G?this is a\<CR>"
1586 " append text without autoindent to Xxx
1587 normal othis should be in column 1
1588 call assert_equal("this should be in column 1", getline('.'))
1589 call assert_equal(4, line('.'))
1590
1591 %bwipe!
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +02001592 set ai&vim modeline&vim modelines&vim
1593endfunc
1594
1595" Test for issue #57
1596" do not move cursor on <c-o> when autoindent is set
1597func Test_ai_CTRL_O()
1598 enew!
1599 set ai
1600 let save_fo = &fo
1601 set fo+=r
1602 exe "normal o# abcdef\<Esc>2hi\<CR>\<C-O>d0\<Esc>"
1603 exe "normal o# abcdef\<Esc>2hi\<C-O>d0\<Esc>"
1604 call assert_equal(['# abc', 'def', 'def'], getline(2, 4))
1605
1606 set ai&vim
1607 let &fo = save_fo
1608 enew!
1609endfunc
1610
1611" Test for autocommand that deletes the current buffer on BufLeave event.
1612" Also test deleting the last buffer, should give a new, empty buffer.
1613func Test_BufLeave_Wipe()
1614 %bwipe!
1615 let content = ['start of test file Xxx',
1616 \ 'this is a test',
1617 \ 'end of test file Xxx']
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01001618 call writefile(content, 'Xxx1', 'D')
1619 call writefile(content, 'Xxx2', 'D')
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +02001620
1621 au BufLeave Xxx2 bwipe
1622 edit Xxx1
1623 split Xxx2
1624 " delete buffer Xxx2, we should be back to Xxx1
1625 bwipe
1626 call assert_equal('Xxx1', bufname('%'))
1627 call assert_equal(1, winnr('$'))
1628
1629 " Create an alternate buffer
1630 %write! test.out
1631 call assert_equal('test.out', bufname('#'))
1632 " delete alternate buffer
1633 bwipe test.out
1634 call assert_equal('Xxx1', bufname('%'))
1635 call assert_equal('', bufname('#'))
1636
1637 au BufLeave Xxx1 bwipe
1638 " delete current buffer, get an empty one
1639 bwipe!
1640 call assert_equal(1, line('$'))
1641 call assert_equal('', bufname('%'))
Bram Moolenaarb2c87502017-10-14 21:15:58 +02001642 let g:bufinfo = getbufinfo()
1643 call assert_equal(1, len(g:bufinfo))
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +02001644
Bram Moolenaar53f0c962017-10-22 14:23:59 +02001645 call delete('test.out')
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +02001646 %bwipe
1647 au! BufLeave
Bram Moolenaarb2c87502017-10-14 21:15:58 +02001648
1649 " check that bufinfo doesn't contain a pointer to freed memory
1650 call test_garbagecollect_now()
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +02001651endfunc
Bram Moolenaar87ffb5c2017-10-19 12:37:42 +02001652
1653func Test_QuitPre()
1654 edit Xfoo
1655 let winid = win_getid(winnr())
1656 split Xbar
1657 au! QuitPre * let g:afile = expand('<afile>')
1658 " Close the other window, <afile> should be correct.
1659 exe win_id2win(winid) . 'q'
1660 call assert_equal('Xfoo', g:afile)
LemonBoy66e13ae2022-04-21 22:52:11 +01001661
Bram Moolenaar87ffb5c2017-10-19 12:37:42 +02001662 unlet g:afile
1663 bwipe Xfoo
1664 bwipe Xbar
1665endfunc
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02001666
1667func Test_Cmdline()
Bram Moolenaar153b7042018-01-31 15:48:32 +01001668 au! CmdlineChanged : let g:text = getcmdline()
1669 let g:text = 0
1670 call feedkeys(":echom 'hello'\<CR>", 'xt')
1671 call assert_equal("echom 'hello'", g:text)
1672 au! CmdlineChanged
1673
1674 au! CmdlineChanged : let g:entered = expand('<afile>')
1675 let g:entered = 0
1676 call feedkeys(":echom 'hello'\<CR>", 'xt')
1677 call assert_equal(':', g:entered)
1678 au! CmdlineChanged
1679
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02001680 au! CmdlineEnter : let g:entered = expand('<afile>')
1681 au! CmdlineLeave : let g:left = expand('<afile>')
1682 let g:entered = 0
1683 let g:left = 0
1684 call feedkeys(":echo 'hello'\<CR>", 'xt')
1685 call assert_equal(':', g:entered)
1686 call assert_equal(':', g:left)
1687 au! CmdlineEnter
1688 au! CmdlineLeave
1689
Bram Moolenaara4baf5b2018-04-22 13:27:44 +02001690 let save_shellslash = &shellslash
1691 set noshellslash
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02001692 au! CmdlineEnter / let g:entered = expand('<afile>')
1693 au! CmdlineLeave / let g:left = expand('<afile>')
1694 let g:entered = 0
1695 let g:left = 0
Bram Moolenaar53f0c962017-10-22 14:23:59 +02001696 new
1697 call setline(1, 'hello')
1698 call feedkeys("/hello\<CR>", 'xt')
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02001699 call assert_equal('/', g:entered)
1700 call assert_equal('/', g:left)
Bram Moolenaar53f0c962017-10-22 14:23:59 +02001701 bwipe!
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02001702 au! CmdlineEnter
1703 au! CmdlineLeave
Bram Moolenaara4baf5b2018-04-22 13:27:44 +02001704 let &shellslash = save_shellslash
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02001705endfunc
Bram Moolenaar53f0c962017-10-22 14:23:59 +02001706
1707" Test for BufWritePre autocommand that deletes or unloads the buffer.
1708func Test_BufWritePre()
1709 %bwipe
1710 au BufWritePre Xxx1 bunload
1711 au BufWritePre Xxx2 bwipe
1712
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01001713 call writefile(['start of Xxx1', 'test', 'end of Xxx1'], 'Xxx1', 'D')
1714 call writefile(['start of Xxx2', 'test', 'end of Xxx2'], 'Xxx2', 'D')
Bram Moolenaar53f0c962017-10-22 14:23:59 +02001715
1716 edit Xtest
1717 e! Xxx2
1718 bdel Xtest
1719 e Xxx1
1720 " write it, will unload it and give an error msg
Bram Moolenaare2e40752020-09-04 21:18:46 +02001721 call assert_fails('w', 'E203:')
Bram Moolenaar53f0c962017-10-22 14:23:59 +02001722 call assert_equal('Xxx2', bufname('%'))
1723 edit Xtest
1724 e! Xxx2
1725 bwipe Xtest
1726 " write it, will delete the buffer and give an error msg
Bram Moolenaare2e40752020-09-04 21:18:46 +02001727 call assert_fails('w', 'E203:')
Bram Moolenaar53f0c962017-10-22 14:23:59 +02001728 call assert_equal('Xxx1', bufname('%'))
1729 au! BufWritePre
Bram Moolenaar53f0c962017-10-22 14:23:59 +02001730endfunc
1731
1732" Test for BufUnload autocommand that unloads all the other buffers
1733func Test_bufunload_all()
Bram Moolenaarf08b0eb2021-10-16 13:00:14 +01001734 let g:test_is_flaky = 1
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01001735 call writefile(['Test file Xxx1'], 'Xxx1', 'D')"
1736 call writefile(['Test file Xxx2'], 'Xxx2', 'D')"
Bram Moolenaar53f0c962017-10-22 14:23:59 +02001737
Bram Moolenaarc79745a2019-05-20 22:12:34 +02001738 let content =<< trim [CODE]
1739 func UnloadAllBufs()
1740 let i = 1
1741 while i <= bufnr('$')
1742 if i != bufnr('%') && bufloaded(i)
1743 exe i . 'bunload'
1744 endif
1745 let i += 1
1746 endwhile
1747 endfunc
1748 au BufUnload * call UnloadAllBufs()
1749 au VimLeave * call writefile(['Test Finished'], 'Xout')
1750 edit Xxx1
1751 split Xxx2
1752 q
1753 [CODE]
1754
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01001755 call writefile(content, 'Xbunloadtest', 'D')
Bram Moolenaar53f0c962017-10-22 14:23:59 +02001756
1757 call delete('Xout')
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01001758 call system(GetVimCommandClean() .. ' -N --not-a-term -S Xbunloadtest')
Bram Moolenaar53f0c962017-10-22 14:23:59 +02001759 call assert_true(filereadable('Xout'))
1760
Bram Moolenaar53f0c962017-10-22 14:23:59 +02001761 call delete('Xout')
1762endfunc
1763
1764" Some tests for buffer-local autocommands
1765func Test_buflocal_autocmd()
1766 let g:bname = ''
1767 edit xx
1768 au BufLeave <buffer> let g:bname = expand("%")
1769 " here, autocommand for xx should trigger.
1770 " but autocommand shall not apply to buffer named <buffer>.
1771 edit somefile
1772 call assert_equal('xx', g:bname)
1773 let g:bname = ''
1774 " here, autocommand shall be auto-deleted
1775 bwipe xx
1776 " autocmd should not trigger
1777 edit xx
1778 call assert_equal('', g:bname)
1779 " autocmd should not trigger
1780 edit somefile
1781 call assert_equal('', g:bname)
1782 enew
1783 unlet g:bname
1784endfunc
Bram Moolenaar430dc5d2017-11-02 21:04:47 +01001785
1786" Test for "*Cmd" autocommands
1787func Test_Cmd_Autocmds()
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01001788 call writefile(['start of Xxx', "\tabc2", 'end of Xxx'], 'Xxx', 'D')
Bram Moolenaar430dc5d2017-11-02 21:04:47 +01001789
1790 enew!
1791 au BufReadCmd XtestA 0r Xxx|$del
1792 edit XtestA " will read text of Xxd instead
1793 call assert_equal('start of Xxx', getline(1))
1794
1795 au BufWriteCmd XtestA call append(line("$"), "write")
1796 write " will append a line to the file
1797 call assert_equal('write', getline('$'))
Bram Moolenaare2e40752020-09-04 21:18:46 +02001798 call assert_fails('read XtestA', 'E484:') " should not read anything
Bram Moolenaar430dc5d2017-11-02 21:04:47 +01001799 call assert_equal('write', getline(4))
1800
1801 " now we have:
1802 " 1 start of Xxx
1803 " 2 abc2
1804 " 3 end of Xxx
1805 " 4 write
1806
1807 au FileReadCmd XtestB '[r Xxx
1808 2r XtestB " will read Xxx below line 2 instead
1809 call assert_equal('start of Xxx', getline(3))
1810
1811 " now we have:
1812 " 1 start of Xxx
1813 " 2 abc2
1814 " 3 start of Xxx
1815 " 4 abc2
1816 " 5 end of Xxx
1817 " 6 end of Xxx
1818 " 7 write
1819
1820 au FileWriteCmd XtestC '[,']copy $
1821 normal 4GA1
1822 4,5w XtestC " will copy lines 4 and 5 to the end
1823 call assert_equal("\tabc21", getline(8))
Bram Moolenaare2e40752020-09-04 21:18:46 +02001824 call assert_fails('r XtestC', 'E484:') " should not read anything
Bram Moolenaar430dc5d2017-11-02 21:04:47 +01001825 call assert_equal("end of Xxx", getline(9))
1826
1827 " now we have:
1828 " 1 start of Xxx
1829 " 2 abc2
1830 " 3 start of Xxx
1831 " 4 abc21
1832 " 5 end of Xxx
1833 " 6 end of Xxx
1834 " 7 write
1835 " 8 abc21
1836 " 9 end of Xxx
1837
1838 let g:lines = []
1839 au FileAppendCmd XtestD call extend(g:lines, getline(line("'["), line("']")))
1840 w >>XtestD " will add lines to 'lines'
1841 call assert_equal(9, len(g:lines))
Bram Moolenaare2e40752020-09-04 21:18:46 +02001842 call assert_fails('$r XtestD', 'E484:') " should not read anything
Bram Moolenaar430dc5d2017-11-02 21:04:47 +01001843 call assert_equal(9, line('$'))
1844 call assert_equal('end of Xxx', getline('$'))
1845
1846 au BufReadCmd XtestE 0r Xxx|$del
1847 sp XtestE " split window with test.out
1848 call assert_equal('end of Xxx', getline(3))
1849
1850 let g:lines = []
1851 exe "normal 2Goasdf\<Esc>\<C-W>\<C-W>"
1852 au BufWriteCmd XtestE call extend(g:lines, getline(0, '$'))
1853 wall " will write other window to 'lines'
1854 call assert_equal(4, len(g:lines), g:lines)
1855 call assert_equal('asdf', g:lines[2])
1856
1857 au! BufReadCmd
1858 au! BufWriteCmd
1859 au! FileReadCmd
1860 au! FileWriteCmd
1861 au! FileAppendCmd
1862 %bwipe!
Bram Moolenaar430dc5d2017-11-02 21:04:47 +01001863 enew!
1864endfunc
Bram Moolenaaraace2152017-11-05 16:23:10 +01001865
Bram Moolenaar0fff4412020-03-29 16:06:29 +02001866func s:ReadFile()
1867 setl noswapfile nomodified
1868 let filename = resolve(expand("<afile>:p"))
1869 execute 'read' fnameescape(filename)
1870 1d_
1871 exe 'file' fnameescape(filename)
1872 setl buftype=acwrite
1873endfunc
1874
1875func s:WriteFile()
1876 let filename = resolve(expand("<afile>:p"))
1877 setl buftype=
1878 noautocmd execute 'write' fnameescape(filename)
1879 setl buftype=acwrite
1880 setl nomodified
1881endfunc
1882
1883func Test_BufReadCmd()
1884 autocmd BufReadCmd *.test call s:ReadFile()
1885 autocmd BufWriteCmd *.test call s:WriteFile()
1886
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01001887 call writefile(['one', 'two', 'three'], 'Xcmd.test', 'D')
Bram Moolenaar0fff4412020-03-29 16:06:29 +02001888 edit Xcmd.test
1889 call assert_match('Xcmd.test" line 1 of 3', execute('file'))
1890 normal! Gofour
1891 write
1892 call assert_equal(['one', 'two', 'three', 'four'], readfile('Xcmd.test'))
1893
1894 bwipe!
Bram Moolenaar0fff4412020-03-29 16:06:29 +02001895 au! BufReadCmd
1896 au! BufWriteCmd
1897endfunc
1898
zeertzjq9c8f9462022-08-30 18:17:15 +01001899func Test_BufWriteCmd()
1900 autocmd BufWriteCmd Xbufwritecmd let g:written = 1
1901 new
1902 file Xbufwritecmd
1903 set buftype=acwrite
Bram Moolenaar6f14da12022-09-07 21:30:44 +01001904 call mkdir('Xbufwritecmd', 'D')
zeertzjq9c8f9462022-08-30 18:17:15 +01001905 write
1906 " BufWriteCmd should be triggered even if a directory has the same name
1907 call assert_equal(1, g:written)
zeertzjq9c8f9462022-08-30 18:17:15 +01001908 unlet g:written
1909 au! BufWriteCmd
1910 bwipe!
1911endfunc
1912
Bram Moolenaaraace2152017-11-05 16:23:10 +01001913func SetChangeMarks(start, end)
Bram Moolenaar97c69432021-01-15 16:45:21 +01001914 exe a:start .. 'mark ['
1915 exe a:end .. 'mark ]'
Bram Moolenaaraace2152017-11-05 16:23:10 +01001916endfunc
1917
1918" Verify the effects of autocmds on '[ and ']
1919func Test_change_mark_in_autocmds()
1920 edit! Xtest
Bram Moolenaar97c69432021-01-15 16:45:21 +01001921 call feedkeys("ia\<CR>b\<CR>c\<CR>d\<C-g>u\<Esc>", 'xtn')
Bram Moolenaaraace2152017-11-05 16:23:10 +01001922
1923 call SetChangeMarks(2, 3)
1924 write
1925 call assert_equal([1, 4], [line("'["), line("']")])
1926
1927 call SetChangeMarks(2, 3)
1928 au BufWritePre * call assert_equal([1, 4], [line("'["), line("']")])
1929 write
1930 au! BufWritePre
1931
Bram Moolenaar14ddd222020-08-05 12:02:40 +02001932 if has('unix')
Bram Moolenaaraace2152017-11-05 16:23:10 +01001933 write XtestFilter
1934 write >> XtestFilter
1935
1936 call SetChangeMarks(2, 3)
1937 " Marks are set to the entire range of the write
1938 au FilterWritePre * call assert_equal([1, 4], [line("'["), line("']")])
1939 " '[ is adjusted to just before the line that will receive the filtered
1940 " data
1941 au FilterReadPre * call assert_equal([4, 4], [line("'["), line("']")])
1942 " The filtered data is read into the buffer, and the source lines are
1943 " still present, so the range is after the source lines
1944 au FilterReadPost * call assert_equal([5, 12], [line("'["), line("']")])
1945 %!cat XtestFilter
1946 " After the filtered data is read, the original lines are deleted
1947 call assert_equal([1, 8], [line("'["), line("']")])
1948 au! FilterWritePre,FilterReadPre,FilterReadPost
1949 undo
1950
1951 call SetChangeMarks(1, 4)
1952 au FilterWritePre * call assert_equal([2, 3], [line("'["), line("']")])
1953 au FilterReadPre * call assert_equal([3, 3], [line("'["), line("']")])
1954 au FilterReadPost * call assert_equal([4, 11], [line("'["), line("']")])
1955 2,3!cat XtestFilter
1956 call assert_equal([2, 9], [line("'["), line("']")])
1957 au! FilterWritePre,FilterReadPre,FilterReadPost
1958 undo
1959
1960 call delete('XtestFilter')
1961 endif
1962
1963 call SetChangeMarks(1, 4)
1964 au FileWritePre * call assert_equal([2, 3], [line("'["), line("']")])
1965 2,3write Xtest2
1966 au! FileWritePre
1967
1968 call SetChangeMarks(2, 3)
1969 au FileAppendPre * call assert_equal([1, 4], [line("'["), line("']")])
1970 write >> Xtest2
1971 au! FileAppendPre
1972
1973 call SetChangeMarks(1, 4)
1974 au FileAppendPre * call assert_equal([2, 3], [line("'["), line("']")])
1975 2,3write >> Xtest2
1976 au! FileAppendPre
1977
1978 call SetChangeMarks(1, 1)
1979 au FileReadPre * call assert_equal([3, 1], [line("'["), line("']")])
1980 au FileReadPost * call assert_equal([4, 11], [line("'["), line("']")])
1981 3read Xtest2
1982 au! FileReadPre,FileReadPost
1983 undo
1984
1985 call SetChangeMarks(4, 4)
1986 " When the line is 0, it's adjusted to 1
1987 au FileReadPre * call assert_equal([1, 4], [line("'["), line("']")])
1988 au FileReadPost * call assert_equal([1, 8], [line("'["), line("']")])
1989 0read Xtest2
1990 au! FileReadPre,FileReadPost
1991 undo
1992
1993 call SetChangeMarks(4, 4)
1994 " When the line is 0, it's adjusted to 1
1995 au FileReadPre * call assert_equal([1, 4], [line("'["), line("']")])
1996 au FileReadPost * call assert_equal([2, 9], [line("'["), line("']")])
1997 1read Xtest2
1998 au! FileReadPre,FileReadPost
1999 undo
2000
2001 bwipe!
2002 call delete('Xtest')
2003 call delete('Xtest2')
2004endfunc
2005
2006func Test_Filter_noshelltemp()
Bram Moolenaaraeb313f2020-11-27 19:13:28 +01002007 CheckExecutable cat
Bram Moolenaaraace2152017-11-05 16:23:10 +01002008
2009 enew!
2010 call setline(1, ['a', 'b', 'c', 'd'])
2011
2012 let shelltemp = &shelltemp
2013 set shelltemp
2014
2015 let g:filter_au = 0
2016 au FilterWritePre * let g:filter_au += 1
2017 au FilterReadPre * let g:filter_au += 1
2018 au FilterReadPost * let g:filter_au += 1
2019 %!cat
2020 call assert_equal(3, g:filter_au)
2021
2022 if has('filterpipe')
2023 set noshelltemp
2024
2025 let g:filter_au = 0
2026 au FilterWritePre * let g:filter_au += 1
2027 au FilterReadPre * let g:filter_au += 1
2028 au FilterReadPost * let g:filter_au += 1
2029 %!cat
2030 call assert_equal(0, g:filter_au)
2031 endif
2032
2033 au! FilterWritePre,FilterReadPre,FilterReadPost
2034 let &shelltemp = shelltemp
2035 bwipe!
2036endfunc
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01002037
2038func Test_TextYankPost()
2039 enew!
2040 call setline(1, ['foo'])
2041
2042 let g:event = []
2043 au TextYankPost * let g:event = copy(v:event)
2044
2045 call assert_equal({}, v:event)
2046 call assert_fails('let v:event = {}', 'E46:')
2047 call assert_fails('let v:event.mykey = 0', 'E742:')
2048
2049 norm "ayiw
2050 call assert_equal(
Bram Moolenaara016eeb2022-04-09 11:37:38 +01002051 \ #{regcontents: ['foo'], regname: 'a', operator: 'y',
2052 \ regtype: 'v', visual: v:false, inclusive: v:true},
2053 \ g:event)
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01002054 norm y_
2055 call assert_equal(
Bram Moolenaara016eeb2022-04-09 11:37:38 +01002056 \ #{regcontents: ['foo'], regname: '', operator: 'y', regtype: 'V',
2057 \ visual: v:false, inclusive: v:false},
2058 \ g:event)
Bram Moolenaar37d16732020-06-12 22:09:01 +02002059 norm Vy
2060 call assert_equal(
Bram Moolenaara016eeb2022-04-09 11:37:38 +01002061 \ #{regcontents: ['foo'], regname: '', operator: 'y', regtype: 'V',
2062 \ visual: v:true, inclusive: v:true},
2063 \ g:event)
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01002064 call feedkeys("\<C-V>y", 'x')
2065 call assert_equal(
Bram Moolenaara016eeb2022-04-09 11:37:38 +01002066 \ #{regcontents: ['f'], regname: '', operator: 'y', regtype: "\x161",
2067 \ visual: v:true, inclusive: v:true},
2068 \ g:event)
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01002069 norm "xciwbar
2070 call assert_equal(
Bram Moolenaara016eeb2022-04-09 11:37:38 +01002071 \ #{regcontents: ['foo'], regname: 'x', operator: 'c', regtype: 'v',
2072 \ visual: v:false, inclusive: v:true},
2073 \ g:event)
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01002074 norm "bdiw
2075 call assert_equal(
Bram Moolenaara016eeb2022-04-09 11:37:38 +01002076 \ #{regcontents: ['bar'], regname: 'b', operator: 'd', regtype: 'v',
2077 \ visual: v:false, inclusive: v:true},
2078 \ g:event)
2079
2080 call setline(1, 'foobar')
2081 " exclusive motion
2082 norm $"ay0
2083 call assert_equal(
2084 \ #{regcontents: ['fooba'], regname: 'a', operator: 'y', regtype: 'v',
2085 \ visual: v:false, inclusive: v:false},
2086 \ g:event)
2087 " inclusive motion
2088 norm 0"ay$
2089 call assert_equal(
2090 \ #{regcontents: ['foobar'], regname: 'a', operator: 'y', regtype: 'v',
2091 \ visual: v:false, inclusive: v:true},
2092 \ g:event)
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01002093
2094 call assert_equal({}, v:event)
2095
Bram Moolenaarfccbf062020-11-26 20:34:00 +01002096 if has('clipboard_working') && !has('gui_running')
2097 " Test that when the visual selection is automatically copied to clipboard
2098 " register a TextYankPost is emitted
2099 call setline(1, ['foobar'])
2100
2101 let @* = ''
2102 set clipboard=autoselect
2103 exe "norm! ggviw\<Esc>"
2104 call assert_equal(
Bram Moolenaara016eeb2022-04-09 11:37:38 +01002105 \ #{regcontents: ['foobar'], regname: '*', operator: 'y',
2106 \ regtype: 'v', visual: v:true, inclusive: v:false},
2107 \ g:event)
Bram Moolenaarfccbf062020-11-26 20:34:00 +01002108
2109 let @+ = ''
2110 set clipboard=autoselectplus
2111 exe "norm! ggviw\<Esc>"
2112 call assert_equal(
Bram Moolenaara016eeb2022-04-09 11:37:38 +01002113 \ #{regcontents: ['foobar'], regname: '+', operator: 'y',
2114 \ regtype: 'v', visual: v:true, inclusive: v:false},
2115 \ g:event)
Bram Moolenaarfccbf062020-11-26 20:34:00 +01002116
2117 set clipboard&vim
2118 endif
2119
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01002120 au! TextYankPost
2121 unlet g:event
2122 bwipe!
2123endfunc
Bram Moolenaar9bca8052017-12-18 12:37:55 +01002124
Bram Moolenaar9a046fd2021-01-28 13:47:59 +01002125func Test_autocommand_all_events()
2126 call assert_fails('au * * bwipe', 'E1155:')
2127 call assert_fails('au * x bwipe', 'E1155:')
Bram Moolenaarb6db1462021-12-24 19:24:47 +00002128 call assert_fails('au! * x bwipe', 'E1155:')
Bram Moolenaar4fb921e2017-12-18 15:33:00 +01002129endfunc
Bram Moolenaarb7407d32018-02-03 17:36:27 +01002130
Bram Moolenaarf6246f52022-02-11 16:30:12 +00002131func Test_autocmd_user()
2132 au User MyEvent let s:res = [expand("<afile>"), expand("<amatch>")]
2133 doautocmd User MyEvent
2134 call assert_equal(['MyEvent', 'MyEvent'], s:res)
2135 au! User
2136 unlet s:res
2137endfunc
2138
Bram Moolenaarb7407d32018-02-03 17:36:27 +01002139function s:Before_test_dirchanged()
2140 augroup test_dirchanged
2141 autocmd!
2142 augroup END
2143 let s:li = []
2144 let s:dir_this = getcwd()
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02002145 let s:dir_foo = s:dir_this . '/Xfoo'
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01002146 call mkdir(s:dir_foo)
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02002147 let s:dir_bar = s:dir_this . '/Xbar'
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01002148 call mkdir(s:dir_bar)
Bram Moolenaarb7407d32018-02-03 17:36:27 +01002149endfunc
2150
2151function s:After_test_dirchanged()
Bram Moolenaar3503d7c2019-11-09 20:10:17 +01002152 call chdir(s:dir_this)
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01002153 call delete(s:dir_foo, 'd')
2154 call delete(s:dir_bar, 'd')
Bram Moolenaarb7407d32018-02-03 17:36:27 +01002155 augroup test_dirchanged
2156 autocmd!
2157 augroup END
2158endfunc
2159
2160function Test_dirchanged_global()
2161 call s:Before_test_dirchanged()
Bram Moolenaarf6246f52022-02-11 16:30:12 +00002162 autocmd test_dirchanged DirChangedPre global call add(s:li, expand("<amatch>") .. " pre cd " .. v:event.directory)
Bram Moolenaarb7407d32018-02-03 17:36:27 +01002163 autocmd test_dirchanged DirChanged global call add(s:li, "cd:")
2164 autocmd test_dirchanged DirChanged global call add(s:li, expand("<afile>"))
Bram Moolenaar3503d7c2019-11-09 20:10:17 +01002165 call chdir(s:dir_foo)
Bram Moolenaarf6246f52022-02-11 16:30:12 +00002166 let expected = ["global pre cd " .. s:dir_foo, "cd:", s:dir_foo]
Bram Moolenaar28e8f732022-02-09 12:58:20 +00002167 call assert_equal(expected, s:li)
Bram Moolenaar3503d7c2019-11-09 20:10:17 +01002168 call chdir(s:dir_foo)
Bram Moolenaar28e8f732022-02-09 12:58:20 +00002169 call assert_equal(expected, s:li)
Bram Moolenaar3503d7c2019-11-09 20:10:17 +01002170 exe 'lcd ' .. fnameescape(s:dir_bar)
Bram Moolenaar28e8f732022-02-09 12:58:20 +00002171 call assert_equal(expected, s:li)
Bram Moolenaard8c9d322022-06-12 11:49:16 +01002172
2173 exe 'cd ' .. s:dir_foo
2174 exe 'cd ' .. s:dir_bar
2175 autocmd! test_dirchanged DirChanged global let g:result = expand("<afile>")
2176 cd -
Bram Moolenaardb77c492022-06-12 23:26:50 +01002177 call assert_equal(s:dir_foo, substitute(g:result, '\\', '/', 'g'))
Bram Moolenaard8c9d322022-06-12 11:49:16 +01002178
Bram Moolenaarb7407d32018-02-03 17:36:27 +01002179 call s:After_test_dirchanged()
2180endfunc
2181
2182function Test_dirchanged_local()
2183 call s:Before_test_dirchanged()
2184 autocmd test_dirchanged DirChanged window call add(s:li, "lcd:")
2185 autocmd test_dirchanged DirChanged window call add(s:li, expand("<afile>"))
Bram Moolenaar3503d7c2019-11-09 20:10:17 +01002186 call chdir(s:dir_foo)
Bram Moolenaarb7407d32018-02-03 17:36:27 +01002187 call assert_equal([], s:li)
Bram Moolenaar3503d7c2019-11-09 20:10:17 +01002188 exe 'lcd ' .. fnameescape(s:dir_bar)
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01002189 call assert_equal(["lcd:", s:dir_bar], s:li)
Bram Moolenaar3503d7c2019-11-09 20:10:17 +01002190 exe 'lcd ' .. fnameescape(s:dir_bar)
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01002191 call assert_equal(["lcd:", s:dir_bar], s:li)
Bram Moolenaarb7407d32018-02-03 17:36:27 +01002192 call s:After_test_dirchanged()
2193endfunc
2194
2195function Test_dirchanged_auto()
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +02002196 CheckOption autochdir
Bram Moolenaarb7407d32018-02-03 17:36:27 +01002197 call s:Before_test_dirchanged()
2198 call test_autochdir()
Bram Moolenaar28e8f732022-02-09 12:58:20 +00002199 autocmd test_dirchanged DirChangedPre auto call add(s:li, "pre cd " .. v:event.directory)
Bram Moolenaarb7407d32018-02-03 17:36:27 +01002200 autocmd test_dirchanged DirChanged auto call add(s:li, "auto:")
2201 autocmd test_dirchanged DirChanged auto call add(s:li, expand("<afile>"))
2202 set acd
Bram Moolenaar3503d7c2019-11-09 20:10:17 +01002203 cd ..
Bram Moolenaarb7407d32018-02-03 17:36:27 +01002204 call assert_equal([], s:li)
Bram Moolenaar61abe7d2022-08-30 21:46:08 +01002205 exe 'edit ' . s:dir_foo . '/Xautofile'
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01002206 call assert_equal(s:dir_foo, getcwd())
Bram Moolenaar28e8f732022-02-09 12:58:20 +00002207 let expected = ["pre cd " .. s:dir_foo, "auto:", s:dir_foo]
2208 call assert_equal(expected, s:li)
Bram Moolenaarb7407d32018-02-03 17:36:27 +01002209 set noacd
2210 bwipe!
2211 call s:After_test_dirchanged()
2212endfunc
Bram Moolenaar5a093432018-02-10 18:15:19 +01002213
2214" Test TextChangedI and TextChangedP
2215func Test_ChangedP()
2216 new
2217 call setline(1, ['foo', 'bar', 'foobar'])
2218 call test_override("char_avail", 1)
2219 set complete=. completeopt=menuone
2220
2221 func! TextChangedAutocmd(char)
2222 let g:autocmd .= a:char
2223 endfunc
2224
Christian Brabandtdb3b4462021-10-16 11:58:55 +01002225 " TextChanged will not be triggered, only check that it isn't.
Bram Moolenaar5a093432018-02-10 18:15:19 +01002226 au! TextChanged <buffer> :call TextChangedAutocmd('N')
2227 au! TextChangedI <buffer> :call TextChangedAutocmd('I')
2228 au! TextChangedP <buffer> :call TextChangedAutocmd('P')
2229
2230 call cursor(3, 1)
2231 let g:autocmd = ''
2232 call feedkeys("o\<esc>", 'tnix')
2233 call assert_equal('I', g:autocmd)
2234
2235 let g:autocmd = ''
2236 call feedkeys("Sf", 'tnix')
2237 call assert_equal('II', g:autocmd)
2238
2239 let g:autocmd = ''
2240 call feedkeys("Sf\<C-N>", 'tnix')
2241 call assert_equal('IIP', g:autocmd)
2242
2243 let g:autocmd = ''
2244 call feedkeys("Sf\<C-N>\<C-N>", 'tnix')
2245 call assert_equal('IIPP', g:autocmd)
2246
2247 let g:autocmd = ''
2248 call feedkeys("Sf\<C-N>\<C-N>\<C-N>", 'tnix')
2249 call assert_equal('IIPPP', g:autocmd)
2250
2251 let g:autocmd = ''
2252 call feedkeys("Sf\<C-N>\<C-N>\<C-N>\<C-N>", 'tnix')
2253 call assert_equal('IIPPPP', g:autocmd)
2254
2255 call assert_equal(['foo', 'bar', 'foobar', 'foo'], getline(1, '$'))
2256 " TODO: how should it handle completeopt=noinsert,noselect?
2257
2258 " CleanUp
2259 call test_override("char_avail", 0)
2260 au! TextChanged
2261 au! TextChangedI
2262 au! TextChangedP
2263 delfu TextChangedAutocmd
2264 unlet! g:autocmd
2265 set complete&vim completeopt&vim
2266
2267 bw!
2268endfunc
Bram Moolenaar8c64a362018-03-23 22:39:31 +01002269
Bram Moolenaar91d2e782018-08-07 19:05:01 +02002270let g:setline_handled = v:false
Bram Moolenaar1e115362019-01-09 23:01:02 +01002271func SetLineOne()
Bram Moolenaar91d2e782018-08-07 19:05:01 +02002272 if !g:setline_handled
2273 call setline(1, "(x)")
2274 let g:setline_handled = v:true
2275 endif
2276endfunc
2277
2278func Test_TextChangedI_with_setline()
2279 new
2280 call test_override('char_avail', 1)
2281 autocmd TextChangedI <buffer> call SetLineOne()
2282 call feedkeys("i(\<CR>\<Esc>", 'tx')
2283 call assert_equal('(', getline(1))
2284 call assert_equal('x)', getline(2))
2285 undo
Bram Moolenaar91d2e782018-08-07 19:05:01 +02002286 call assert_equal('', getline(1))
Bram Moolenaar9fa95062018-08-08 22:08:32 +02002287 call assert_equal('', getline(2))
Bram Moolenaar91d2e782018-08-07 19:05:01 +02002288
Bram Moolenaarca34db32022-01-20 11:17:18 +00002289 call test_override('char_avail', 0)
Bram Moolenaar91d2e782018-08-07 19:05:01 +02002290 bwipe!
2291endfunc
2292
Bram Moolenaar8c64a362018-03-23 22:39:31 +01002293func Test_Changed_FirstTime()
Bram Moolenaar8c5a2782019-08-07 23:07:07 +02002294 CheckFeature terminal
2295 CheckNotGui
Bram Moolenaar3cdcb092020-03-18 19:18:10 +01002296 " Starting a terminal to run Vim is always considered flaky.
Bram Moolenaar30d53e22020-03-18 21:10:44 +01002297 let g:test_is_flaky = 1
Bram Moolenaar8c5a2782019-08-07 23:07:07 +02002298
Bram Moolenaar8c64a362018-03-23 22:39:31 +01002299 " Prepare file for TextChanged event.
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01002300 call writefile([''], 'Xchanged.txt', 'D')
Bram Moolenaar8c64a362018-03-23 22:39:31 +01002301 let buf = term_start([GetVimProg(), '--clean', '-c', 'set noswapfile'], {'term_rows': 3})
2302 call assert_equal('running', term_getstatus(buf))
Bram Moolenaar1834d372018-03-29 17:40:46 +02002303 " Wait for the ruler (in the status line) to be shown.
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01002304 " In ConPTY, there is additional character which is drawn up to the width of
2305 " the screen.
2306 if has('conpty')
2307 call WaitForAssert({-> assert_match('\<All.*$', term_getline(buf, 3))})
2308 else
2309 call WaitForAssert({-> assert_match('\<All$', term_getline(buf, 3))})
2310 endif
Bram Moolenaar8c64a362018-03-23 22:39:31 +01002311 " It's only adding autocmd, so that no event occurs.
2312 call term_sendkeys(buf, ":au! TextChanged <buffer> call writefile(['No'], 'Xchanged.txt')\<cr>")
2313 call term_sendkeys(buf, "\<C-\\>\<C-N>:qa!\<cr>")
Bram Moolenaar50182fa2018-04-28 21:34:40 +02002314 call WaitForAssert({-> assert_equal('finished', term_getstatus(buf))})
Bram Moolenaar8c64a362018-03-23 22:39:31 +01002315 call assert_equal([''], readfile('Xchanged.txt'))
2316
2317 " clean up
Bram Moolenaar8c64a362018-03-23 22:39:31 +01002318 bwipe!
2319endfunc
Bram Moolenaar0566e892019-01-24 19:37:40 +01002320
Bram Moolenaareb93f3f2019-04-04 15:04:56 +02002321func Test_autocmd_nested()
2322 let g:did_nested = 0
2323 augroup Testing
2324 au WinNew * edit somefile
2325 au BufNew * let g:did_nested = 1
2326 augroup END
2327 split
2328 call assert_equal(0, g:did_nested)
2329 close
2330 bwipe! somefile
2331
2332 " old nested argument still works
2333 augroup Testing
2334 au!
2335 au WinNew * nested edit somefile
2336 au BufNew * let g:did_nested = 1
2337 augroup END
2338 split
2339 call assert_equal(1, g:did_nested)
2340 close
2341 bwipe! somefile
2342
2343 " New ++nested argument works
2344 augroup Testing
2345 au!
2346 au WinNew * ++nested edit somefile
2347 au BufNew * let g:did_nested = 1
2348 augroup END
2349 split
2350 call assert_equal(1, g:did_nested)
2351 close
2352 bwipe! somefile
2353
Bram Moolenaarf0775142022-03-04 20:10:38 +00002354 " nested without ++ does not work in Vim9 script
2355 call assert_fails('vim9cmd au WinNew * nested echo fails', 'E1078:')
2356
Bram Moolenaareb93f3f2019-04-04 15:04:56 +02002357 augroup Testing
2358 au!
2359 augroup END
2360
2361 call assert_fails('au WinNew * ++nested ++nested echo bad', 'E983:')
2362 call assert_fails('au WinNew * nested nested echo bad', 'E983:')
2363endfunc
2364
Bram Moolenaar5fa9f232022-07-23 09:06:48 +01002365func Test_autocmd_nested_cursor_invalid()
2366 set laststatus=0
2367 copen
2368 cclose
2369 call setline(1, ['foo', 'bar', 'baz'])
2370 3
2371 augroup nested_inv
2372 autocmd User foo ++nested copen
2373 autocmd BufAdd * let &laststatus = 2 - &laststatus
2374 augroup END
2375 doautocmd User foo
2376
2377 augroup nested_inv
2378 au!
2379 augroup END
2380 set laststatus&
Bram Moolenaarb03950f2022-07-26 13:47:13 +01002381 cclose
Bram Moolenaar5fa9f232022-07-23 09:06:48 +01002382 bwipe!
2383endfunc
2384
Bram Moolenaar3d6ee8b2022-07-27 15:23:35 +01002385func Test_autocmd_nested_keeps_cursor_pos()
2386 enew
2387 call setline(1, 'foo')
2388 autocmd User foo ++nested normal! $a
2389 autocmd InsertLeave * :
2390 doautocmd User foo
2391 call assert_equal([0, 1, 3, 0], getpos('.'))
2392
2393 bwipe!
2394endfunc
2395
Bram Moolenaarb03950f2022-07-26 13:47:13 +01002396func Test_autocmd_nested_switch_window()
2397 " run this in a separate Vim so that SafeState works
2398 CheckRunVimInTerminal
2399
2400 let lines =<< trim END
2401 vim9script
2402 ['()']->writefile('Xautofile')
2403 autocmd VimEnter * ++nested edit Xautofile | split
2404 autocmd BufReadPost * autocmd SafeState * ++once foldclosed('.')
2405 autocmd WinEnter * matchadd('ErrorMsg', 'pat')
2406 END
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01002407 call writefile(lines, 'Xautoscript', 'D')
Bram Moolenaarb03950f2022-07-26 13:47:13 +01002408 let buf = RunVimInTerminal('-S Xautoscript', {'rows': 10})
2409 call VerifyScreenDump(buf, 'Test_autocmd_nested_switch', {})
2410
2411 call StopVimInTerminal(buf)
2412 call delete('Xautofile')
Bram Moolenaarb03950f2022-07-26 13:47:13 +01002413endfunc
2414
Bram Moolenaareb93f3f2019-04-04 15:04:56 +02002415func Test_autocmd_once()
2416 " Without ++once WinNew triggers twice
2417 let g:did_split = 0
2418 augroup Testing
2419 au WinNew * let g:did_split += 1
2420 augroup END
2421 split
2422 split
2423 call assert_equal(2, g:did_split)
2424 call assert_true(exists('#WinNew'))
2425 close
2426 close
2427
2428 " With ++once WinNew triggers once
2429 let g:did_split = 0
2430 augroup Testing
2431 au!
2432 au WinNew * ++once let g:did_split += 1
2433 augroup END
2434 split
2435 split
2436 call assert_equal(1, g:did_split)
2437 call assert_false(exists('#WinNew'))
2438 close
2439 close
2440
2441 call assert_fails('au WinNew * ++once ++once echo bad', 'E983:')
2442endfunc
2443
Bram Moolenaara68e5952019-04-25 22:22:01 +02002444func Test_autocmd_bufreadpre()
2445 new
2446 let b:bufreadpre = 1
Bram Moolenaarab505b12020-03-23 19:28:44 +01002447 call append(0, range(1000))
Bram Moolenaara68e5952019-04-25 22:22:01 +02002448 w! XAutocmdBufReadPre.txt
2449 autocmd BufReadPre <buffer> :let b:bufreadpre += 1
Bram Moolenaarab505b12020-03-23 19:28:44 +01002450 norm! 500gg
Bram Moolenaara68e5952019-04-25 22:22:01 +02002451 sp
Bram Moolenaarab505b12020-03-23 19:28:44 +01002452 norm! 1000gg
Bram Moolenaara68e5952019-04-25 22:22:01 +02002453 wincmd p
2454 let g:wsv1 = winsaveview()
2455 wincmd p
2456 let g:wsv2 = winsaveview()
2457 " triggers BufReadPre, should not move the cursor in either window
2458 " The topline may change one line in a large window.
2459 edit
2460 call assert_inrange(g:wsv2.topline - 1, g:wsv2.topline + 1, winsaveview().topline)
2461 call assert_equal(g:wsv2.lnum, winsaveview().lnum)
2462 call assert_equal(2, b:bufreadpre)
2463 wincmd p
2464 call assert_equal(g:wsv1.topline, winsaveview().topline)
2465 call assert_equal(g:wsv1.lnum, winsaveview().lnum)
2466 call assert_equal(2, b:bufreadpre)
2467 " Now set the cursor position in an BufReadPre autocommand
2468 " (even though the position will be invalid, this should make Vim reset the
2469 " cursor position in the other window.
2470 wincmd p
2471 set cpo+=g
2472 " won't do anything, but try to set the cursor on an invalid lnum
2473 autocmd BufReadPre <buffer> :norm! 70gg
2474 " triggers BufReadPre, should not move the cursor in either window
2475 e
2476 call assert_equal(1, winsaveview().topline)
2477 call assert_equal(1, winsaveview().lnum)
2478 call assert_equal(3, b:bufreadpre)
2479 wincmd p
2480 call assert_equal(g:wsv1.topline, winsaveview().topline)
2481 call assert_equal(g:wsv1.lnum, winsaveview().lnum)
2482 call assert_equal(3, b:bufreadpre)
2483 close
2484 close
2485 call delete('XAutocmdBufReadPre.txt')
2486 set cpo-=g
2487endfunc
2488
Bram Moolenaar5e66b422019-01-24 21:58:10 +01002489" FileChangedShell tested in test_filechanged.vim
Bram Moolenaar69ea5872019-04-25 20:29:00 +02002490
2491" Tests for the following autocommands:
2492" - FileWritePre writing a compressed file
2493" - FileReadPost reading a compressed file
2494" - BufNewFile reading a file template
2495" - BufReadPre decompressing the file to be read
2496" - FilterReadPre substituting characters in the temp file
2497" - FilterReadPost substituting characters after filtering
2498" - FileReadPre set options for decompression
2499" - FileReadPost decompress the file
2500func Test_ReadWrite_Autocmds()
2501 " Run this test only on Unix-like systems and if gzip is available
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +02002502 CheckUnix
2503 CheckExecutable gzip
Bram Moolenaar69ea5872019-04-25 20:29:00 +02002504
2505 " Make $GZIP empty, "-v" would cause trouble.
2506 let $GZIP = ""
2507
2508 " Use a FileChangedShell autocommand to avoid a prompt for 'Xtestfile.gz'
2509 " being modified outside of Vim (noticed on Solaris).
2510 au FileChangedShell * echo 'caught FileChangedShell'
2511
2512 " Test for the FileReadPost, FileWritePre and FileWritePost autocmds
2513 augroup Test1
2514 au!
2515 au FileWritePre *.gz '[,']!gzip
2516 au FileWritePost *.gz undo
2517 au FileReadPost *.gz '[,']!gzip -d
2518 augroup END
2519
2520 new
2521 set bin
2522 call append(0, [
2523 \ 'line 2 Abcdefghijklmnopqrstuvwxyz',
2524 \ 'line 3 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2525 \ 'line 4 Abcdefghijklmnopqrstuvwxyz',
2526 \ 'line 5 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2527 \ 'line 6 Abcdefghijklmnopqrstuvwxyz',
2528 \ 'line 7 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2529 \ 'line 8 Abcdefghijklmnopqrstuvwxyz',
2530 \ 'line 9 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2531 \ 'line 10 Abcdefghijklmnopqrstuvwxyz'
2532 \ ])
2533 1,9write! Xtestfile.gz
2534 enew! | close
2535
2536 new
2537 " Read and decompress the testfile
2538 0read Xtestfile.gz
2539 call assert_equal([
2540 \ 'line 2 Abcdefghijklmnopqrstuvwxyz',
2541 \ 'line 3 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2542 \ 'line 4 Abcdefghijklmnopqrstuvwxyz',
2543 \ 'line 5 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2544 \ 'line 6 Abcdefghijklmnopqrstuvwxyz',
2545 \ 'line 7 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2546 \ 'line 8 Abcdefghijklmnopqrstuvwxyz',
2547 \ 'line 9 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2548 \ 'line 10 Abcdefghijklmnopqrstuvwxyz'
2549 \ ], getline(1, 9))
2550 enew! | close
2551
2552 augroup Test1
2553 au!
2554 augroup END
2555
2556 " Test for the FileAppendPre and FileAppendPost autocmds
2557 augroup Test2
2558 au!
2559 au BufNewFile *.c read Xtest.c
2560 au FileAppendPre *.out '[,']s/new/NEW/
2561 au FileAppendPost *.out !cat Xtest.c >> test.out
2562 augroup END
2563
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01002564 call writefile(['/*', ' * Here is a new .c file', ' */'], 'Xtest.c', 'D')
Bram Moolenaar69ea5872019-04-25 20:29:00 +02002565 new foo.c " should load Xtest.c
2566 call assert_equal(['/*', ' * Here is a new .c file', ' */'], getline(2, 4))
2567 w! >> test.out " append it to the output file
2568
2569 let contents = readfile('test.out')
2570 call assert_equal(' * Here is a NEW .c file', contents[2])
2571 call assert_equal(' * Here is a new .c file', contents[5])
2572
2573 call delete('test.out')
2574 enew! | close
2575 augroup Test2
2576 au!
2577 augroup END
2578
2579 " Test for the BufReadPre and BufReadPost autocmds
2580 augroup Test3
2581 au!
2582 " setup autocommands to decompress before reading and re-compress
2583 " afterwards
2584 au BufReadPre *.gz exe '!gzip -d ' . shellescape(expand("<afile>"))
2585 au BufReadPre *.gz call rename(expand("<afile>:r"), expand("<afile>"))
2586 au BufReadPost *.gz call rename(expand("<afile>"), expand("<afile>:r"))
2587 au BufReadPost *.gz exe '!gzip ' . shellescape(expand("<afile>:r"))
2588 augroup END
2589
2590 e! Xtestfile.gz " Edit compressed file
2591 call assert_equal([
2592 \ 'line 2 Abcdefghijklmnopqrstuvwxyz',
2593 \ 'line 3 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2594 \ 'line 4 Abcdefghijklmnopqrstuvwxyz',
2595 \ 'line 5 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2596 \ 'line 6 Abcdefghijklmnopqrstuvwxyz',
2597 \ 'line 7 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2598 \ 'line 8 Abcdefghijklmnopqrstuvwxyz',
2599 \ 'line 9 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2600 \ 'line 10 Abcdefghijklmnopqrstuvwxyz'
2601 \ ], getline(1, 9))
2602
2603 w! >> test.out " Append it to the output file
2604
2605 augroup Test3
2606 au!
2607 augroup END
2608
2609 " Test for the FilterReadPre and FilterReadPost autocmds.
2610 set shelltemp " need temp files here
2611 augroup Test4
2612 au!
2613 au FilterReadPre *.out call rename(expand("<afile>"), expand("<afile>") . ".t")
2614 au FilterReadPre *.out exe 'silent !sed s/e/E/ ' . shellescape(expand("<afile>")) . ".t >" . shellescape(expand("<afile>"))
2615 au FilterReadPre *.out exe 'silent !rm ' . shellescape(expand("<afile>")) . '.t'
2616 au FilterReadPost *.out '[,']s/x/X/g
2617 augroup END
2618
2619 e! test.out " Edit the output file
2620 1,$!cat
2621 call assert_equal([
2622 \ 'linE 2 AbcdefghijklmnopqrstuvwXyz',
2623 \ 'linE 3 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
2624 \ 'linE 4 AbcdefghijklmnopqrstuvwXyz',
2625 \ 'linE 5 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
2626 \ 'linE 6 AbcdefghijklmnopqrstuvwXyz',
2627 \ 'linE 7 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
2628 \ 'linE 8 AbcdefghijklmnopqrstuvwXyz',
2629 \ 'linE 9 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
2630 \ 'linE 10 AbcdefghijklmnopqrstuvwXyz'
2631 \ ], getline(1, 9))
2632 call assert_equal([
2633 \ 'line 2 Abcdefghijklmnopqrstuvwxyz',
2634 \ 'line 3 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2635 \ 'line 4 Abcdefghijklmnopqrstuvwxyz',
2636 \ 'line 5 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2637 \ 'line 6 Abcdefghijklmnopqrstuvwxyz',
2638 \ 'line 7 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2639 \ 'line 8 Abcdefghijklmnopqrstuvwxyz',
2640 \ 'line 9 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2641 \ 'line 10 Abcdefghijklmnopqrstuvwxyz'
2642 \ ], readfile('test.out'))
2643
2644 augroup Test4
2645 au!
2646 augroup END
2647 set shelltemp&vim
2648
2649 " Test for the FileReadPre and FileReadPost autocmds.
2650 augroup Test5
2651 au!
2652 au FileReadPre *.gz exe 'silent !gzip -d ' . shellescape(expand("<afile>"))
2653 au FileReadPre *.gz call rename(expand("<afile>:r"), expand("<afile>"))
2654 au FileReadPost *.gz '[,']s/l/L/
2655 augroup END
2656
2657 new
2658 0r Xtestfile.gz " Read compressed file
2659 call assert_equal([
2660 \ 'Line 2 Abcdefghijklmnopqrstuvwxyz',
2661 \ 'Line 3 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2662 \ 'Line 4 Abcdefghijklmnopqrstuvwxyz',
2663 \ 'Line 5 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2664 \ 'Line 6 Abcdefghijklmnopqrstuvwxyz',
2665 \ 'Line 7 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2666 \ 'Line 8 Abcdefghijklmnopqrstuvwxyz',
2667 \ 'Line 9 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2668 \ 'Line 10 Abcdefghijklmnopqrstuvwxyz'
2669 \ ], getline(1, 9))
2670 call assert_equal([
2671 \ 'line 2 Abcdefghijklmnopqrstuvwxyz',
2672 \ 'line 3 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2673 \ 'line 4 Abcdefghijklmnopqrstuvwxyz',
2674 \ 'line 5 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2675 \ 'line 6 Abcdefghijklmnopqrstuvwxyz',
2676 \ 'line 7 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2677 \ 'line 8 Abcdefghijklmnopqrstuvwxyz',
2678 \ 'line 9 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2679 \ 'line 10 Abcdefghijklmnopqrstuvwxyz'
2680 \ ], readfile('Xtestfile.gz'))
2681
2682 augroup Test5
2683 au!
2684 augroup END
2685
2686 au! FileChangedShell
2687 call delete('Xtestfile.gz')
Bram Moolenaar69ea5872019-04-25 20:29:00 +02002688 call delete('test.out')
2689endfunc
Bram Moolenaar23b51392019-05-09 21:38:43 +02002690
2691func Test_throw_in_BufWritePre()
2692 new
2693 call setline(1, ['one', 'two', 'three'])
2694 call assert_false(filereadable('Xthefile'))
2695 augroup throwing
2696 au BufWritePre X* throw 'do not write'
2697 augroup END
2698 try
2699 w Xthefile
2700 catch
2701 let caught = 1
2702 endtry
2703 call assert_equal(1, caught)
2704 call assert_false(filereadable('Xthefile'))
2705
2706 bwipe!
2707 au! throwing
2708endfunc
Bram Moolenaarcadbe1b2019-09-22 21:50:09 +02002709
Bram Moolenaar40fa12a2021-09-22 14:18:13 +02002710func Test_autocmd_in_try_block()
Bram Moolenaar6f14da12022-09-07 21:30:44 +01002711 call mkdir('Xintrydir', 'R')
Bram Moolenaar40fa12a2021-09-22 14:18:13 +02002712 au BufEnter * let g:fname = expand('%')
2713 try
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +01002714 edit Xintrydir/
Bram Moolenaar40fa12a2021-09-22 14:18:13 +02002715 endtry
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +01002716 call assert_match('Xintrydir', g:fname)
Bram Moolenaar40fa12a2021-09-22 14:18:13 +02002717
2718 unlet g:fname
2719 au! BufEnter
Bram Moolenaar40fa12a2021-09-22 14:18:13 +02002720endfunc
2721
Bram Moolenaarcadbe1b2019-09-22 21:50:09 +02002722func Test_autocmd_SafeState()
2723 CheckRunVimInTerminal
2724
2725 let lines =<< trim END
2726 let g:safe = 0
2727 let g:again = ''
2728 au SafeState * let g:safe += 1
2729 au SafeStateAgain * let g:again ..= 'x'
2730 func CallTimer()
2731 call timer_start(10, {id -> execute('let g:again ..= "t"')})
2732 endfunc
2733 END
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01002734 call writefile(lines, 'XSafeState', 'D')
Bram Moolenaarcadbe1b2019-09-22 21:50:09 +02002735 let buf = RunVimInTerminal('-S XSafeState', #{rows: 6})
2736
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01002737 " Sometimes we loop to handle a K_IGNORE, SafeState may be triggered once or
Bram Moolenaar8fb1b472020-02-23 16:16:26 +01002738 " more often.
Bram Moolenaarcadbe1b2019-09-22 21:50:09 +02002739 call term_sendkeys(buf, ":echo g:safe\<CR>")
Bram Moolenaar8fb1b472020-02-23 16:16:26 +01002740 call WaitForAssert({-> assert_match('^\d ', term_getline(buf, 6))}, 1000)
Bram Moolenaarcadbe1b2019-09-22 21:50:09 +02002741
Bram Moolenaar8fb1b472020-02-23 16:16:26 +01002742 " SafeStateAgain should be invoked at least three times
Bram Moolenaarcadbe1b2019-09-22 21:50:09 +02002743 call term_sendkeys(buf, ":echo g:again\<CR>")
Bram Moolenaar8fb1b472020-02-23 16:16:26 +01002744 call WaitForAssert({-> assert_match('^xxx', term_getline(buf, 6))}, 1000)
Bram Moolenaarcadbe1b2019-09-22 21:50:09 +02002745
2746 call term_sendkeys(buf, ":let g:again = ''\<CR>:call CallTimer()\<CR>")
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02002747 call TermWait(buf, 50)
Bram Moolenaar0f6629a2019-09-22 23:24:13 +02002748 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02002749 call TermWait(buf, 50)
Bram Moolenaarcadbe1b2019-09-22 21:50:09 +02002750 call term_sendkeys(buf, ":echo g:again\<CR>")
2751 call WaitForAssert({-> assert_match('xtx', term_getline(buf, 6))}, 1000)
2752
2753 call StopVimInTerminal(buf)
Bram Moolenaarcadbe1b2019-09-22 21:50:09 +02002754endfunc
Bram Moolenaar23324a02019-10-01 17:39:04 +02002755
2756func Test_autocmd_CmdWinEnter()
2757 CheckRunVimInTerminal
Bram Moolenaar21829c52021-01-26 22:42:21 +01002758 CheckFeature cmdwin
2759
Bram Moolenaar23324a02019-10-01 17:39:04 +02002760 let lines =<< trim END
Egor Zvorykin125ffd22021-11-17 14:01:14 +00002761 augroup vimHints | au! | augroup END
Bram Moolenaar23324a02019-10-01 17:39:04 +02002762 let b:dummy_var = 'This is a dummy'
2763 autocmd CmdWinEnter * quit
2764 let winnr = winnr('$')
2765 END
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01002766 let filename = 'XCmdWinEnter'
Bram Moolenaar23324a02019-10-01 17:39:04 +02002767 call writefile(lines, filename)
2768 let buf = RunVimInTerminal('-S '.filename, #{rows: 6})
2769
2770 call term_sendkeys(buf, "q:")
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02002771 call TermWait(buf)
Bram Moolenaar23324a02019-10-01 17:39:04 +02002772 call term_sendkeys(buf, ":echo b:dummy_var\<cr>")
Bram Moolenaar353c3512020-03-15 14:19:26 +01002773 call WaitForAssert({-> assert_match('^This is a dummy', term_getline(buf, 6))}, 2000)
Bram Moolenaar23324a02019-10-01 17:39:04 +02002774 call term_sendkeys(buf, ":echo &buftype\<cr>")
2775 call WaitForAssert({-> assert_notmatch('^nofile', term_getline(buf, 6))}, 1000)
2776 call term_sendkeys(buf, ":echo winnr\<cr>")
2777 call WaitForAssert({-> assert_match('^1', term_getline(buf, 6))}, 1000)
2778
2779 " clean up
2780 call StopVimInTerminal(buf)
2781 call delete(filename)
2782endfunc
Bram Moolenaarec66c412019-10-11 21:19:13 +02002783
2784func Test_autocmd_was_using_freed_memory()
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01002785 CheckFeature quickfix
2786
Bram Moolenaarec66c412019-10-11 21:19:13 +02002787 pedit xx
2788 n x
Bram Moolenaar92bb83e2021-02-03 23:04:46 +01002789 augroup winenter
2790 au WinEnter * if winnr('$') > 2 | quit | endif
2791 augroup END
Bram Moolenaarec66c412019-10-11 21:19:13 +02002792 split
Bram Moolenaar92bb83e2021-02-03 23:04:46 +01002793
2794 augroup winenter
2795 au! WinEnter
2796 augroup END
2797
2798 bwipe xx
2799 bwipe x
2800 pclose
Bram Moolenaarec66c412019-10-11 21:19:13 +02002801endfunc
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +01002802
2803func Test_BufWrite_lockmarks()
Bram Moolenaarf08b0eb2021-10-16 13:00:14 +01002804 let g:test_is_flaky = 1
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +01002805 edit! Xtest
2806 call setline(1, ['a', 'b', 'c', 'd'])
2807
2808 " :lockmarks preserves the marks
2809 call SetChangeMarks(2, 3)
2810 lockmarks write
2811 call assert_equal([2, 3], [line("'["), line("']")])
2812
2813 " *WritePre autocmds get the correct line range, but lockmarks preserves the
2814 " original values for the user
2815 augroup lockmarks
2816 au!
2817 au BufWritePre,FilterWritePre * call assert_equal([1, 4], [line("'["), line("']")])
2818 au FileWritePre * call assert_equal([3, 4], [line("'["), line("']")])
2819 augroup END
2820
2821 lockmarks write
2822 call assert_equal([2, 3], [line("'["), line("']")])
2823
2824 if executable('cat')
2825 lockmarks %!cat
2826 call assert_equal([2, 3], [line("'["), line("']")])
2827 endif
2828
2829 lockmarks 3,4write Xtest2
2830 call assert_equal([2, 3], [line("'["), line("']")])
2831
2832 au! lockmarks
2833 augroup! lockmarks
2834 call delete('Xtest')
2835 call delete('Xtest2')
2836endfunc
Bram Moolenaarce6db022020-01-07 20:11:42 +01002837
2838func Test_FileType_spell()
2839 if !isdirectory('/tmp')
2840 throw "Skipped: requires /tmp directory"
2841 endif
2842
2843 " this was crashing with an invalid free()
2844 setglobal spellfile=/tmp/en.utf-8.add
2845 augroup crash
2846 autocmd!
2847 autocmd BufNewFile,BufReadPost crashfile setf somefiletype
2848 autocmd BufNewFile,BufReadPost crashfile set ft=anotherfiletype
2849 autocmd FileType anotherfiletype setlocal spell
2850 augroup END
2851 func! NoCrash() abort
2852 edit /tmp/crashfile
2853 endfunc
2854 call NoCrash()
2855
2856 au! crash
2857 setglobal spellfile=
2858endfunc
Bram Moolenaarbc2b71d2020-02-17 21:33:30 +01002859
Bram Moolenaar406cd902020-02-18 21:54:41 +01002860" Test closing a window or editing another buffer from a FileChangedRO handler
2861" in a readonly buffer
2862func Test_FileChangedRO_winclose()
Bram Moolenaar62cd26a2020-10-11 20:08:44 +02002863 call test_override('ui_delay', 10)
2864
Bram Moolenaar406cd902020-02-18 21:54:41 +01002865 augroup FileChangedROTest
2866 au!
2867 autocmd FileChangedRO * quit
2868 augroup END
2869 new
2870 set readonly
2871 call assert_fails('normal i', 'E788:')
2872 close
2873 augroup! FileChangedROTest
2874
2875 augroup FileChangedROTest
2876 au!
Bram Moolenaar61abe7d2022-08-30 21:46:08 +01002877 autocmd FileChangedRO * edit Xrofile
Bram Moolenaar406cd902020-02-18 21:54:41 +01002878 augroup END
2879 new
2880 set readonly
2881 call assert_fails('normal i', 'E788:')
2882 close
2883 augroup! FileChangedROTest
Bram Moolenaar62cd26a2020-10-11 20:08:44 +02002884 call test_override('ALL', 0)
Bram Moolenaar406cd902020-02-18 21:54:41 +01002885endfunc
2886
Bram Moolenaar0c81d1b2020-02-22 22:45:55 +01002887func LogACmd()
2888 call add(g:logged, line('$'))
2889endfunc
2890
2891func Test_TermChanged()
Bram Moolenaard28e0b32020-02-22 23:08:52 +01002892 CheckNotGui
2893
Bram Moolenaar0c81d1b2020-02-22 22:45:55 +01002894 enew!
2895 tabnew
2896 call setline(1, ['a', 'b', 'c', 'd'])
2897 $
2898 au TermChanged * call LogACmd()
2899 let g:logged = []
2900 let term_save = &term
2901 set term=xterm
2902 call assert_equal([1, 4], g:logged)
2903
2904 au! TermChanged
2905 let &term = term_save
2906 bwipe!
2907endfunc
2908
Bram Moolenaare3284872020-03-19 13:55:03 +01002909" Test for FileReadCmd autocmd
2910func Test_autocmd_FileReadCmd()
2911 func ReadFileCmd()
2912 call append(line('$'), "v:cmdarg = " .. v:cmdarg)
2913 endfunc
2914 augroup FileReadCmdTest
2915 au!
2916 au FileReadCmd Xtest call ReadFileCmd()
2917 augroup END
2918
2919 new
2920 read ++bin Xtest
2921 read ++nobin Xtest
2922 read ++edit Xtest
2923 read ++bad=keep Xtest
2924 read ++bad=drop Xtest
2925 read ++bad=- Xtest
2926 read ++ff=unix Xtest
2927 read ++ff=dos Xtest
2928 read ++ff=mac Xtest
2929 read ++enc=utf-8 Xtest
2930
2931 call assert_equal(['',
2932 \ 'v:cmdarg = ++bin',
2933 \ 'v:cmdarg = ++nobin',
2934 \ 'v:cmdarg = ++edit',
2935 \ 'v:cmdarg = ++bad=keep',
2936 \ 'v:cmdarg = ++bad=drop',
2937 \ 'v:cmdarg = ++bad=-',
2938 \ 'v:cmdarg = ++ff=unix',
2939 \ 'v:cmdarg = ++ff=dos',
2940 \ 'v:cmdarg = ++ff=mac',
2941 \ 'v:cmdarg = ++enc=utf-8'], getline(1, '$'))
2942
2943 close!
2944 augroup FileReadCmdTest
2945 au!
2946 augroup END
2947 delfunc ReadFileCmd
2948endfunc
2949
Bram Moolenaaree4e0c12020-04-06 21:35:05 +02002950" Test for passing invalid arguments to autocmd
2951func Test_autocmd_invalid_args()
2952 " Additional character after * for event
Bram Moolenaar61abe7d2022-08-30 21:46:08 +01002953 call assert_fails('autocmd *a Xinvfile set ff=unix', 'E215:')
Bram Moolenaaree4e0c12020-04-06 21:35:05 +02002954 augroup Test
2955 augroup END
2956 " Invalid autocmd event
Bram Moolenaar61abe7d2022-08-30 21:46:08 +01002957 call assert_fails('autocmd Bufabc Xinvfile set ft=vim', 'E216:')
Bram Moolenaaree4e0c12020-04-06 21:35:05 +02002958 " Invalid autocmd event in a autocmd group
Bram Moolenaar61abe7d2022-08-30 21:46:08 +01002959 call assert_fails('autocmd Test Bufabc Xinvfile set ft=vim', 'E216:')
Bram Moolenaaree4e0c12020-04-06 21:35:05 +02002960 augroup! Test
2961 " Execute all autocmds
2962 call assert_fails('doautocmd * BufEnter', 'E217:')
2963 call assert_fails('augroup! x1a2b3', 'E367:')
2964 call assert_fails('autocmd BufNew <buffer=999> pwd', 'E680:')
Bram Moolenaar531be472020-09-23 22:38:05 +02002965 call assert_fails('autocmd BufNew \) set ff=unix', 'E55:')
Bram Moolenaaree4e0c12020-04-06 21:35:05 +02002966endfunc
2967
2968" Test for deep nesting of autocmds
2969func Test_autocmd_deep_nesting()
Bram Moolenaar61abe7d2022-08-30 21:46:08 +01002970 autocmd BufEnter Xdeepfile doautocmd BufEnter Xdeepfile
2971 call assert_fails('doautocmd BufEnter Xdeepfile', 'E218:')
2972 autocmd! BufEnter Xdeepfile
Bram Moolenaaree4e0c12020-04-06 21:35:05 +02002973endfunc
2974
Bram Moolenaarbe5ee862020-06-10 20:56:58 +02002975" Tests for SigUSR1 autocmd event, which is only available on posix systems.
2976func Test_autocmd_sigusr1()
2977 CheckUnix
Bram Moolenaar62cd26a2020-10-11 20:08:44 +02002978 CheckExecutable /bin/kill
Bram Moolenaarbe5ee862020-06-10 20:56:58 +02002979
2980 let g:sigusr1_passed = 0
2981 au SigUSR1 * let g:sigusr1_passed = 1
2982 call system('/bin/kill -s usr1 ' . getpid())
2983 call WaitForAssert({-> assert_true(g:sigusr1_passed)})
2984
2985 au! SigUSR1
2986 unlet g:sigusr1_passed
2987endfunc
2988
Bram Moolenaarb340bae2020-06-15 19:51:56 +02002989" Test for BufReadPre autocmd deleting the file
2990func Test_BufReadPre_delfile()
2991 augroup TestAuCmd
2992 au!
Bram Moolenaare7cda972022-08-29 11:02:59 +01002993 autocmd BufReadPre XbufreadPre call delete('XbufreadPre')
Bram Moolenaarb340bae2020-06-15 19:51:56 +02002994 augroup END
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01002995 call writefile([], 'XbufreadPre', 'D')
Bram Moolenaare7cda972022-08-29 11:02:59 +01002996 call assert_fails('new XbufreadPre', 'E200:')
2997 call assert_equal('XbufreadPre', @%)
Bram Moolenaarb340bae2020-06-15 19:51:56 +02002998 call assert_equal(1, &readonly)
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01002999
Bram Moolenaarb340bae2020-06-15 19:51:56 +02003000 augroup TestAuCmd
3001 au!
3002 augroup END
3003 close!
3004endfunc
3005
3006" Test for BufReadPre autocmd changing the current buffer
3007func Test_BufReadPre_changebuf()
3008 augroup TestAuCmd
3009 au!
Bram Moolenaare7cda972022-08-29 11:02:59 +01003010 autocmd BufReadPre Xchangebuf edit Xsomeotherfile
Bram Moolenaarb340bae2020-06-15 19:51:56 +02003011 augroup END
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01003012 call writefile([], 'Xchangebuf', 'D')
Bram Moolenaare7cda972022-08-29 11:02:59 +01003013 call assert_fails('new Xchangebuf', 'E201:')
Bram Moolenaarb340bae2020-06-15 19:51:56 +02003014 call assert_equal('Xsomeotherfile', @%)
3015 call assert_equal(1, &readonly)
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01003016
Bram Moolenaarb340bae2020-06-15 19:51:56 +02003017 augroup TestAuCmd
3018 au!
3019 augroup END
3020 close!
3021endfunc
3022
3023" Test for BufWipeouti autocmd changing the current buffer when reading a file
3024" in an empty buffer with 'f' flag in 'cpo'
3025func Test_BufDelete_changebuf()
3026 new
3027 augroup TestAuCmd
3028 au!
3029 autocmd BufWipeout * let bufnr = bufadd('somefile') | exe "b " .. bufnr
3030 augroup END
3031 let save_cpo = &cpo
3032 set cpo+=f
Bram Moolenaarb18b4962022-09-02 21:55:50 +01003033 call assert_fails('r Xchangebuf', ['E812:', 'E484:'])
Bram Moolenaarb340bae2020-06-15 19:51:56 +02003034 call assert_equal('somefile', @%)
3035 let &cpo = save_cpo
3036 augroup TestAuCmd
3037 au!
3038 augroup END
3039 close!
3040endfunc
3041
Bram Moolenaar0fe937f2020-06-16 22:42:04 +02003042" Test for the temporary internal window used to execute autocmds
3043func Test_autocmd_window()
3044 %bw!
3045 edit one.txt
3046 tabnew two.txt
Bram Moolenaar41cd8032021-03-13 15:47:56 +01003047 vnew three.txt
3048 tabnew four.txt
3049 tabprevious
Bram Moolenaar0fe937f2020-06-16 22:42:04 +02003050 let g:blist = []
Bram Moolenaar832adf92020-06-25 19:01:36 +02003051 augroup aucmd_win_test1
Bram Moolenaar0fe937f2020-06-16 22:42:04 +02003052 au!
3053 au BufEnter * call add(g:blist, [expand('<afile>'),
3054 \ win_gettype(bufwinnr(expand('<afile>')))])
3055 augroup END
3056
3057 doautoall BufEnter
Bram Moolenaar41cd8032021-03-13 15:47:56 +01003058 call assert_equal([
3059 \ ['one.txt', 'autocmd'],
3060 \ ['two.txt', ''],
3061 \ ['four.txt', 'autocmd'],
3062 \ ['three.txt', ''],
3063 \ ], g:blist)
Bram Moolenaar0fe937f2020-06-16 22:42:04 +02003064
Bram Moolenaar832adf92020-06-25 19:01:36 +02003065 augroup aucmd_win_test1
Bram Moolenaar0fe937f2020-06-16 22:42:04 +02003066 au!
3067 augroup END
Bram Moolenaar832adf92020-06-25 19:01:36 +02003068 augroup! aucmd_win_test1
3069 %bw!
3070endfunc
3071
3072" Test for trying to close the temporary window used for executing an autocmd
3073func Test_close_autocmd_window()
3074 %bw!
3075 edit one.txt
3076 tabnew two.txt
3077 augroup aucmd_win_test2
3078 au!
3079 au BufEnter * if expand('<afile>') == 'one.txt' | 1close | endif
3080 augroup END
3081
3082 call assert_fails('doautoall BufEnter', 'E813:')
3083
3084 augroup aucmd_win_test2
3085 au!
3086 augroup END
3087 augroup! aucmd_win_test2
Bram Moolenaarcf844172020-06-26 19:44:06 +02003088 %bwipe!
3089endfunc
3090
3091" Test for trying to close the tab that has the temporary window for exeucing
3092" an autocmd.
3093func Test_close_autocmd_tab()
3094 edit one.txt
3095 tabnew two.txt
3096 augroup aucmd_win_test
3097 au!
3098 au BufEnter * if expand('<afile>') == 'one.txt' | tabfirst | tabonly | endif
3099 augroup END
3100
3101 call assert_fails('doautoall BufEnter', 'E813:')
3102
3103 tabonly
3104 augroup aucmd_win_test
3105 au!
3106 augroup END
3107 augroup! aucmd_win_test
3108 %bwipe!
Bram Moolenaar0fe937f2020-06-16 22:42:04 +02003109endfunc
3110
Bram Moolenaarcb1956d2022-01-07 15:45:18 +00003111func Test_Visual_doautoall_redraw()
3112 call setline(1, ['a', 'b'])
3113 new
3114 wincmd p
3115 call feedkeys("G\<C-V>", 'txn')
3116 autocmd User Explode ++once redraw
3117 doautoall User Explode
3118 %bwipe!
3119endfunc
3120
Bram Moolenaar6bcb8772021-02-03 21:23:29 +01003121" This was using freed memory.
3122func Test_BufNew_arglocal()
3123 arglocal
3124 au BufNew * arglocal
3125 call assert_fails('drop xx', 'E1156:')
3126
3127 au! BufNew
3128endfunc
3129
Bram Moolenaar8ab37572021-02-03 21:56:59 +01003130func Test_autocmd_closes_window()
3131 au BufNew,BufWinLeave * e %e
3132 file yyy
3133 au BufNew,BufWinLeave * ball
Bram Moolenaaraad5f9d2021-02-06 17:30:31 +01003134 n xxx
Bram Moolenaar8ab37572021-02-03 21:56:59 +01003135
Bram Moolenaaraad5f9d2021-02-06 17:30:31 +01003136 %bwipe
Bram Moolenaar8ab37572021-02-03 21:56:59 +01003137 au! BufNew
3138 au! BufWinLeave
3139endfunc
3140
Bram Moolenaar92bb83e2021-02-03 23:04:46 +01003141func Test_autocmd_quit_psearch()
3142 sn aa bb
3143 augroup aucmd_win_test
3144 au!
3145 au BufEnter,BufLeave,BufNew,WinEnter,WinLeave,WinNew * if winnr('$') > 1 | q | endif
3146 augroup END
3147 ps /
3148
3149 augroup aucmd_win_test
3150 au!
3151 augroup END
zeertzjq7851c692022-04-21 11:14:01 +01003152 new
3153 pclose
Bram Moolenaar92bb83e2021-02-03 23:04:46 +01003154endfunc
3155
Bram Moolenaaraad5f9d2021-02-06 17:30:31 +01003156" Fuzzer found some strange combination that caused a crash.
3157func Test_autocmd_normal_mess()
Bram Moolenaardd07c022021-02-07 13:32:46 +01003158 " For unknown reason this hangs on MS-Windows
3159 CheckNotMSWindows
3160
Bram Moolenaaraad5f9d2021-02-06 17:30:31 +01003161 augroup aucmd_normal_test
3162 au BufLeave,BufWinLeave,BufHidden,BufUnload,BufDelete,BufWipeout * norm 7q/qc
3163 augroup END
Bram Moolenaar983d83f2021-02-07 12:12:43 +01003164 call assert_fails('o4', 'E1159')
Bram Moolenaaraad5f9d2021-02-06 17:30:31 +01003165 silent! H
Bram Moolenaar983d83f2021-02-07 12:12:43 +01003166 call assert_fails('e xx', 'E1159')
Bram Moolenaaraad5f9d2021-02-06 17:30:31 +01003167 normal G
3168
3169 augroup aucmd_normal_test
3170 au!
3171 augroup END
3172endfunc
3173
Bram Moolenaar8c6951f2021-02-06 18:08:45 +01003174func Test_autocmd_closing_cmdwin()
Bram Moolenaardd07c022021-02-07 13:32:46 +01003175 " For unknown reason this hangs on MS-Windows
3176 CheckNotMSWindows
3177
Bram Moolenaar8c6951f2021-02-06 18:08:45 +01003178 au BufWinLeave * nested q
3179 call assert_fails("norm 7q?\n", 'E855:')
3180
3181 au! BufWinLeave
3182 new
3183 only
3184endfunc
3185
Bram Moolenaar2c7080b2021-02-06 19:19:42 +01003186func Test_autocmd_vimgrep()
3187 augroup aucmd_vimgrep
Charlie Grovesfef44852022-04-19 16:24:12 +01003188 au QuickfixCmdPre,BufNew,BufReadCmd * sb
zeertzjq7851c692022-04-21 11:14:01 +01003189 au QuickfixCmdPre,BufNew,BufReadCmd * q9
Bram Moolenaar2c7080b2021-02-06 19:19:42 +01003190 augroup END
Bram Moolenaardd07c022021-02-07 13:32:46 +01003191 call assert_fails('lv ?a? foo', 'E926:')
Bram Moolenaar2c7080b2021-02-06 19:19:42 +01003192
3193 augroup aucmd_vimgrep
3194 au!
3195 augroup END
3196endfunc
3197
Bram Moolenaar73b8b0a2021-08-01 14:52:32 +02003198func Test_autocmd_with_block()
3199 augroup block_testing
3200 au BufReadPost *.xml {
3201 setlocal matchpairs+=<:>
3202 /<start
3203 }
Bram Moolenaar63b91732021-08-05 20:40:03 +02003204 au CursorHold * {
3205 autocmd BufReadPre * ++once echo 'one' | echo 'two'
3206 g:gotSafeState = 77
3207 }
Bram Moolenaar73b8b0a2021-08-01 14:52:32 +02003208 augroup END
3209
3210 let expected = "\n--- Autocommands ---\nblock_testing BufRead\n *.xml {^@ setlocal matchpairs+=<:>^@ /<start^@ }"
3211 call assert_equal(expected, execute('au BufReadPost *.xml'))
3212
Bram Moolenaar63b91732021-08-05 20:40:03 +02003213 doautocmd CursorHold
3214 call assert_equal(77, g:gotSafeState)
3215 unlet g:gotSafeState
3216
Bram Moolenaar73b8b0a2021-08-01 14:52:32 +02003217 augroup block_testing
3218 au!
Bram Moolenaar75ebd2a2022-06-03 17:39:46 +01003219 autocmd CursorHold * {
3220 if true
3221 # comment
3222 && true
3223
3224 && true
3225 g:done = 'yes'
3226 endif
3227 }
3228 augroup END
3229 doautocmd CursorHold
3230 call assert_equal('yes', g:done)
3231
3232 unlet g:done
3233 augroup block_testing
3234 au!
Bram Moolenaar73b8b0a2021-08-01 14:52:32 +02003235 augroup END
3236endfunc
3237
Christian Brabandtdb3b4462021-10-16 11:58:55 +01003238" Test TextChangedI and TextChanged
3239func Test_Changed_ChangedI()
3240 new
3241 call test_override("char_avail", 1)
3242 let [g:autocmd_i, g:autocmd_n] = ['','']
3243
3244 func! TextChangedAutocmdI(char)
3245 let g:autocmd_{tolower(a:char)} = a:char .. b:changedtick
3246 endfunc
3247
3248 augroup Test_TextChanged
3249 au!
3250 au TextChanged <buffer> :call TextChangedAutocmdI('N')
3251 au TextChangedI <buffer> :call TextChangedAutocmdI('I')
3252 augroup END
3253
3254 call feedkeys("ifoo\<esc>", 'tnix')
3255 " TODO: Test test does not seem to trigger TextChanged autocommand, this
3256 " requires running Vim in a terminal window.
3257 " call assert_equal('N3', g:autocmd_n)
3258 call assert_equal('I3', g:autocmd_i)
3259
3260 call feedkeys("yyp", 'tnix')
3261 " TODO: Test test does not seem to trigger TextChanged autocommand.
3262 " call assert_equal('N4', g:autocmd_n)
3263 call assert_equal('I3', g:autocmd_i)
3264
3265 " CleanUp
3266 call test_override("char_avail", 0)
3267 au! TextChanged <buffer>
3268 au! TextChangedI <buffer>
3269 augroup! Test_TextChanged
3270 delfu TextChangedAutocmdI
3271 unlet! g:autocmd_i g:autocmd_n
3272
3273 bw!
3274endfunc
Bram Moolenaar2c7080b2021-02-06 19:19:42 +01003275
Bram Moolenaar6f2465d2022-03-22 18:13:01 +00003276func Test_closing_autocmd_window()
3277 let lines =<< trim END
3278 edit Xa.txt
3279 tabnew Xb.txt
3280 autocmd BufEnter Xa.txt unhide 1
3281 doautoall BufEnter
3282 END
3283 call v9.CheckScriptFailure(lines, 'E814:')
3284 au! BufEnter
3285 only!
3286 bwipe Xa.txt
3287 bwipe Xb.txt
3288endfunc
3289
Bram Moolenaar347538f2022-03-26 16:28:06 +00003290func Test_bufwipeout_changes_window()
3291 " This should not crash, but we don't have any expectations about what
3292 " happens, changing window in BufWipeout has unpredictable results.
3293 tabedit
3294 let g:window_id = win_getid()
3295 topleft new
3296 setlocal bufhidden=wipe
3297 autocmd BufWipeout <buffer> call win_gotoid(g:window_id)
3298 tabprevious
3299 +tabclose
3300
3301 unlet g:window_id
3302 au! BufWipeout
3303 %bwipe!
3304endfunc
3305
zeertzjq021996f2022-04-10 11:44:04 +01003306func Test_v_event_readonly()
3307 autocmd CompleteChanged * let v:event.width = 0
3308 call assert_fails("normal! i\<C-X>\<C-V>", 'E46:')
3309 au! CompleteChanged
3310
3311 autocmd DirChangedPre * let v:event.directory = ''
3312 call assert_fails('cd .', 'E46:')
3313 au! DirChangedPre
3314
3315 autocmd ModeChanged * let v:event.new_mode = ''
3316 call assert_fails('normal! cc', 'E46:')
3317 au! ModeChanged
3318
3319 autocmd TextYankPost * let v:event.operator = ''
3320 call assert_fails('normal! yy', 'E46:')
3321 au! TextYankPost
3322endfunc
3323
zeertzjqc9e8fd62022-07-26 18:12:38 +01003324" Test for ModeChanged pattern
3325func Test_mode_changes()
3326 let g:index = 0
3327 let g:mode_seq = ['n', 'i', 'n', 'v', 'V', 'i', 'ix', 'i', 'ic', 'i', 'n', 'no', 'n', 'V', 'v', 's', 'n']
3328 func! TestMode()
3329 call assert_equal(g:mode_seq[g:index], get(v:event, "old_mode"))
3330 call assert_equal(g:mode_seq[g:index + 1], get(v:event, "new_mode"))
3331 call assert_equal(mode(1), get(v:event, "new_mode"))
3332 let g:index += 1
3333 endfunc
3334
3335 au ModeChanged * :call TestMode()
3336 let g:n_to_any = 0
3337 au ModeChanged n:* let g:n_to_any += 1
3338 call feedkeys("i\<esc>vVca\<CR>\<C-X>\<C-L>\<esc>ggdG", 'tnix')
3339
3340 let g:V_to_v = 0
3341 au ModeChanged V:v let g:V_to_v += 1
3342 call feedkeys("Vv\<C-G>\<esc>", 'tnix')
3343 call assert_equal(len(filter(g:mode_seq[1:], {idx, val -> val == 'n'})), g:n_to_any)
3344 call assert_equal(1, g:V_to_v)
3345 call assert_equal(len(g:mode_seq) - 1, g:index)
3346
3347 let g:n_to_i = 0
3348 au ModeChanged n:i let g:n_to_i += 1
3349 let g:n_to_niI = 0
3350 au ModeChanged i:niI let g:n_to_niI += 1
3351 let g:niI_to_i = 0
3352 au ModeChanged niI:i let g:niI_to_i += 1
3353 let g:nany_to_i = 0
3354 au ModeChanged n*:i let g:nany_to_i += 1
3355 let g:i_to_n = 0
3356 au ModeChanged i:n let g:i_to_n += 1
3357 let g:nori_to_any = 0
3358 au ModeChanged [ni]:* let g:nori_to_any += 1
3359 let g:i_to_any = 0
3360 au ModeChanged i:* let g:i_to_any += 1
3361 let g:index = 0
3362 let g:mode_seq = ['n', 'i', 'niI', 'i', 'n']
3363 call feedkeys("a\<C-O>l\<esc>", 'tnix')
3364 call assert_equal(len(g:mode_seq) - 1, g:index)
3365 call assert_equal(1, g:n_to_i)
3366 call assert_equal(1, g:n_to_niI)
3367 call assert_equal(1, g:niI_to_i)
3368 call assert_equal(2, g:nany_to_i)
3369 call assert_equal(1, g:i_to_n)
3370 call assert_equal(2, g:i_to_any)
3371 call assert_equal(3, g:nori_to_any)
3372
3373 if has('terminal')
3374 let g:mode_seq += ['c', 'n', 't', 'nt', 'c', 'nt', 'n']
3375 call feedkeys(":term\<CR>\<C-W>N:bd!\<CR>", 'tnix')
3376 call assert_equal(len(g:mode_seq) - 1, g:index)
3377 call assert_equal(1, g:n_to_i)
3378 call assert_equal(1, g:n_to_niI)
3379 call assert_equal(1, g:niI_to_i)
3380 call assert_equal(2, g:nany_to_i)
3381 call assert_equal(1, g:i_to_n)
3382 call assert_equal(2, g:i_to_any)
3383 call assert_equal(5, g:nori_to_any)
3384 endif
3385
3386 if has('cmdwin')
3387 let g:n_to_c = 0
3388 au ModeChanged n:c let g:n_to_c += 1
3389 let g:c_to_n = 0
3390 au ModeChanged c:n let g:c_to_n += 1
3391 let g:mode_seq += ['c', 'n', 'c', 'n']
3392 call feedkeys("q:\<C-C>\<Esc>", 'tnix')
3393 call assert_equal(len(g:mode_seq) - 1, g:index)
3394 call assert_equal(2, g:n_to_c)
3395 call assert_equal(2, g:c_to_n)
3396 unlet g:n_to_c
3397 unlet g:c_to_n
3398 endif
3399
3400 au! ModeChanged
3401 delfunc TestMode
3402 unlet! g:mode_seq
3403 unlet! g:index
3404 unlet! g:n_to_any
3405 unlet! g:V_to_v
3406 unlet! g:n_to_i
3407 unlet! g:n_to_niI
3408 unlet! g:niI_to_i
3409 unlet! g:nany_to_i
3410 unlet! g:i_to_n
3411 unlet! g:nori_to_any
3412 unlet! g:i_to_any
3413endfunc
3414
3415func Test_recursive_ModeChanged()
3416 au! ModeChanged * norm 0u
3417 sil! norm 
3418 au! ModeChanged
3419endfunc
3420
3421func Test_ModeChanged_starts_visual()
3422 " This was triggering ModeChanged before setting VIsual, causing a crash.
3423 au! ModeChanged * norm 0u
3424 sil! norm 
3425
3426 au! ModeChanged
3427endfunc
Bram Moolenaar347538f2022-03-26 16:28:06 +00003428
Charlie Grovesfef44852022-04-19 16:24:12 +01003429func Test_noname_autocmd()
3430 augroup test_noname_autocmd_group
3431 autocmd!
3432 autocmd BufEnter * call add(s:li, ["BufEnter", expand("<afile>")])
3433 autocmd BufDelete * call add(s:li, ["BufDelete", expand("<afile>")])
3434 autocmd BufLeave * call add(s:li, ["BufLeave", expand("<afile>")])
3435 autocmd BufUnload * call add(s:li, ["BufUnload", expand("<afile>")])
3436 autocmd BufWipeout * call add(s:li, ["BufWipeout", expand("<afile>")])
3437 augroup END
3438
3439 let s:li = []
3440 edit foo
3441 call assert_equal([['BufUnload', ''], ['BufDelete', ''], ['BufWipeout', ''], ['BufEnter', 'foo']], s:li)
3442
3443 au! test_noname_autocmd_group
3444 augroup! test_noname_autocmd_group
3445endfunc
3446
Yegappan Lakshmanan1755a912022-05-19 10:31:47 +01003447" Test for the autocmd_get() function
3448func Test_autocmd_get()
3449 augroup TestAutoCmdFns
3450 au!
3451 autocmd BufAdd *.vim echo "bufadd-vim"
3452 autocmd BufAdd *.py echo "bufadd-py"
3453 autocmd BufHidden *.vim echo "bufhidden"
3454 augroup END
3455 augroup TestAutoCmdFns2
3456 autocmd BufAdd *.vim echo "bufadd-vim-2"
3457 autocmd BufRead *.a1b2c3 echo "bufadd-vim-2"
3458 augroup END
3459
3460 let l = autocmd_get()
3461 call assert_true(l->len() > 0)
3462
3463 " Test for getting all the autocmds in a group
3464 let expected = [
3465 \ #{cmd: 'echo "bufadd-vim"', group: 'TestAutoCmdFns',
3466 \ pattern: '*.vim', nested: v:false, once: v:false,
3467 \ event: 'BufAdd'},
3468 \ #{cmd: 'echo "bufadd-py"', group: 'TestAutoCmdFns',
3469 \ pattern: '*.py', nested: v:false, once: v:false,
3470 \ event: 'BufAdd'},
3471 \ #{cmd: 'echo "bufhidden"', group: 'TestAutoCmdFns',
3472 \ pattern: '*.vim', nested: v:false,
3473 \ once: v:false, event: 'BufHidden'}]
3474 call assert_equal(expected, autocmd_get(#{group: 'TestAutoCmdFns'}))
3475
3476 " Test for getting autocmds for all the patterns in a group
3477 call assert_equal(expected, autocmd_get(#{group: 'TestAutoCmdFns',
3478 \ event: '*'}))
3479
3480 " Test for getting autocmds for an event in a group
3481 let expected = [
3482 \ #{cmd: 'echo "bufadd-vim"', group: 'TestAutoCmdFns',
3483 \ pattern: '*.vim', nested: v:false, once: v:false,
3484 \ event: 'BufAdd'},
3485 \ #{cmd: 'echo "bufadd-py"', group: 'TestAutoCmdFns',
3486 \ pattern: '*.py', nested: v:false, once: v:false,
3487 \ event: 'BufAdd'}]
3488 call assert_equal(expected, autocmd_get(#{group: 'TestAutoCmdFns',
3489 \ event: 'BufAdd'}))
3490
3491 " Test for getting the autocmds for all the events in a group for particular
3492 " pattern
3493 call assert_equal([{'cmd': 'echo "bufadd-py"', 'group': 'TestAutoCmdFns',
3494 \ 'pattern': '*.py', 'nested': v:false, 'once': v:false,
3495 \ 'event': 'BufAdd'}],
3496 \ autocmd_get(#{group: 'TestAutoCmdFns', event: '*', pattern: '*.py'}))
3497
3498 " Test for getting the autocmds for an events in a group for particular
3499 " pattern
3500 let l = autocmd_get(#{group: 'TestAutoCmdFns', event: 'BufAdd',
3501 \ pattern: '*.vim'})
3502 call assert_equal([
3503 \ #{cmd: 'echo "bufadd-vim"', group: 'TestAutoCmdFns',
3504 \ pattern: '*.vim', nested: v:false, once: v:false,
3505 \ event: 'BufAdd'}], l)
3506
3507 " Test for getting the autocmds for a pattern in a group
3508 let l = autocmd_get(#{group: 'TestAutoCmdFns', pattern: '*.vim'})
3509 call assert_equal([
3510 \ #{cmd: 'echo "bufadd-vim"', group: 'TestAutoCmdFns',
3511 \ pattern: '*.vim', nested: v:false, once: v:false,
3512 \ event: 'BufAdd'},
3513 \ #{cmd: 'echo "bufhidden"', group: 'TestAutoCmdFns',
3514 \ pattern: '*.vim', nested: v:false,
3515 \ once: v:false, event: 'BufHidden'}], l)
3516
3517 " Test for getting the autocmds for a pattern in all the groups
3518 let l = autocmd_get(#{pattern: '*.a1b2c3'})
3519 call assert_equal([{'cmd': 'echo "bufadd-vim-2"', 'group': 'TestAutoCmdFns2',
3520 \ 'pattern': '*.a1b2c3', 'nested': v:false, 'once': v:false,
3521 \ 'event': 'BufRead'}], l)
3522
3523 " Test for getting autocmds for a pattern without any autocmds
3524 call assert_equal([], autocmd_get(#{group: 'TestAutoCmdFns',
3525 \ pattern: '*.abc'}))
3526 call assert_equal([], autocmd_get(#{group: 'TestAutoCmdFns',
3527 \ event: 'BufAdd', pattern: '*.abc'}))
3528 call assert_equal([], autocmd_get(#{group: 'TestAutoCmdFns',
3529 \ event: 'BufWipeout'}))
3530 call assert_fails("call autocmd_get(#{group: 'abc', event: 'BufAdd'})",
3531 \ 'E367:')
3532 let cmd = "echo autocmd_get(#{group: 'TestAutoCmdFns', event: 'abc'})"
3533 call assert_fails(cmd, 'E216:')
3534 call assert_fails("call autocmd_get(#{group: 'abc'})", 'E367:')
3535 call assert_fails("echo autocmd_get(#{event: 'abc'})", 'E216:')
3536
3537 augroup TestAutoCmdFns
3538 au!
3539 augroup END
3540 call assert_equal([], autocmd_get(#{group: 'TestAutoCmdFns'}))
3541
3542 " Test for nested and once autocmds
3543 augroup TestAutoCmdFns
3544 au!
3545 autocmd VimSuspend * ++nested echo "suspend"
3546 autocmd VimResume * ++once echo "resume"
3547 augroup END
3548
3549 let expected = [
3550 \ {'cmd': 'echo "suspend"', 'group': 'TestAutoCmdFns', 'pattern': '*',
3551 \ 'nested': v:true, 'once': v:false, 'event': 'VimSuspend'},
3552 \ {'cmd': 'echo "resume"', 'group': 'TestAutoCmdFns', 'pattern': '*',
3553 \ 'nested': v:false, 'once': v:true, 'event': 'VimResume'}]
3554 call assert_equal(expected, autocmd_get(#{group: 'TestAutoCmdFns'}))
3555
3556 " Test for buffer-local autocmd
3557 augroup TestAutoCmdFns
3558 au!
3559 autocmd TextYankPost <buffer> echo "textyankpost"
3560 augroup END
3561
3562 let expected = [
3563 \ {'cmd': 'echo "textyankpost"', 'group': 'TestAutoCmdFns',
3564 \ 'pattern': '<buffer=' .. bufnr() .. '>', 'nested': v:false,
3565 \ 'once': v:false, 'bufnr': bufnr(), 'event': 'TextYankPost'}]
3566 call assert_equal(expected, autocmd_get(#{group: 'TestAutoCmdFns'}))
3567
3568 augroup TestAutoCmdFns
3569 au!
3570 augroup END
3571 augroup! TestAutoCmdFns
3572 augroup TestAutoCmdFns2
3573 au!
3574 augroup END
3575 augroup! TestAutoCmdFns2
3576
3577 call assert_fails("echo autocmd_get(#{group: []})", 'E730:')
3578 call assert_fails("echo autocmd_get(#{event: {}})", 'E731:')
3579 call assert_fails("echo autocmd_get([])", 'E1206:')
3580endfunc
3581
3582" Test for the autocmd_add() function
3583func Test_autocmd_add()
3584 " Define a single autocmd in a group
3585 call autocmd_add([#{group: 'TestAcSet', event: 'BufAdd', pattern: '*.sh',
3586 \ cmd: 'echo "bufadd"', once: v:true, nested: v:true}])
3587 call assert_equal([#{cmd: 'echo "bufadd"', group: 'TestAcSet',
3588 \ pattern: '*.sh', nested: v:true, once: v:true,
3589 \ event: 'BufAdd'}], autocmd_get(#{group: 'TestAcSet'}))
3590
3591 " Define two autocmds in the same group
3592 call autocmd_delete([#{group: 'TestAcSet'}])
3593 call autocmd_add([#{group: 'TestAcSet', event: 'BufAdd', pattern: '*.sh',
3594 \ cmd: 'echo "bufadd"'},
3595 \ #{group: 'TestAcSet', event: 'BufEnter', pattern: '*.sh',
3596 \ cmd: 'echo "bufenter"'}])
3597 call assert_equal([
3598 \ #{cmd: 'echo "bufadd"', group: 'TestAcSet', pattern: '*.sh',
3599 \ nested: v:false, once: v:false, event: 'BufAdd'},
3600 \ #{cmd: 'echo "bufenter"', group: 'TestAcSet', pattern: '*.sh',
3601 \ nested: v:false, once: v:false, event: 'BufEnter'}],
3602 \ autocmd_get(#{group: 'TestAcSet'}))
3603
3604 " Define a buffer-local autocmd
3605 call autocmd_delete([#{group: 'TestAcSet'}])
3606 call autocmd_add([#{group: 'TestAcSet', event: 'CursorHold',
3607 \ bufnr: bufnr(), cmd: 'echo "cursorhold"'}])
3608 call assert_equal([
3609 \ #{cmd: 'echo "cursorhold"', group: 'TestAcSet',
3610 \ pattern: '<buffer=' .. bufnr() .. '>', nested: v:false,
3611 \ once: v:false, bufnr: bufnr(), event: 'CursorHold'}],
3612 \ autocmd_get(#{group: 'TestAcSet'}))
3613
3614 " Use an invalid buffer number
3615 call autocmd_delete([#{group: 'TestAcSet'}])
3616 call autocmd_add([#{group: 'TestAcSet', event: 'BufEnter',
3617 \ bufnr: -1, cmd: 'echo "bufenter"'}])
3618 let l = [#{group: 'TestAcSet', event: 'BufAdd', bufnr: 9999,
3619 \ cmd: 'echo "bufadd"'}]
3620 call assert_fails("echo autocmd_add(l)", 'E680:')
Yegappan Lakshmanan00e977c2022-06-01 12:31:53 +01003621 let l = [#{group: 'TestAcSet', event: 'BufAdd', bufnr: 9999,
3622 \ pattern: '*.py', cmd: 'echo "bufadd"'}]
3623 call assert_fails("echo autocmd_add(l)", 'E680:')
3624 let l = [#{group: 'TestAcSet', event: 'BufAdd', bufnr: 9999,
3625 \ pattern: ['*.py', '*.c'], cmd: 'echo "bufadd"'}]
3626 call assert_fails("echo autocmd_add(l)", 'E680:')
Yegappan Lakshmanan1755a912022-05-19 10:31:47 +01003627 let l = [#{group: 'TestAcSet', event: 'BufRead', bufnr: [],
3628 \ cmd: 'echo "bufread"'}]
3629 call assert_fails("echo autocmd_add(l)", 'E745:')
3630 call assert_equal([], autocmd_get(#{group: 'TestAcSet'}))
3631
3632 " Add two commands to the same group, event and pattern
3633 call autocmd_delete([#{group: 'TestAcSet'}])
3634 call autocmd_add([#{group: 'TestAcSet', event: 'BufUnload',
3635 \ pattern: 'abc', cmd: 'echo "cmd1"'}])
3636 call autocmd_add([#{group: 'TestAcSet', event: 'BufUnload',
3637 \ pattern: 'abc', cmd: 'echo "cmd2"'}])
3638 call assert_equal([
3639 \ #{cmd: 'echo "cmd1"', group: 'TestAcSet', pattern: 'abc',
3640 \ nested: v:false, once: v:false, event: 'BufUnload'},
3641 \ #{cmd: 'echo "cmd2"', group: 'TestAcSet', pattern: 'abc',
3642 \ nested: v:false, once: v:false, event: 'BufUnload'}],
3643 \ autocmd_get(#{group: 'TestAcSet'}))
3644
3645 " When adding a new autocmd, if the autocmd 'group' is not specified, then
3646 " the current autocmd group should be used.
3647 call autocmd_delete([#{group: 'TestAcSet'}])
3648 augroup TestAcSet
3649 call autocmd_add([#{event: 'BufHidden', pattern: 'abc', cmd: 'echo "abc"'}])
3650 augroup END
3651 call assert_equal([
3652 \ #{cmd: 'echo "abc"', group: 'TestAcSet', pattern: 'abc',
3653 \ nested: v:false, once: v:false, event: 'BufHidden'}],
3654 \ autocmd_get(#{group: 'TestAcSet'}))
3655
Yegappan Lakshmanan971f6822022-05-24 11:40:11 +01003656 " Test for replacing a cmd for an event in a group
3657 call autocmd_delete([#{group: 'TestAcSet'}])
3658 call autocmd_add([#{replace: v:true, group: 'TestAcSet', event: 'BufEnter',
3659 \ pattern: '*.py', cmd: 'echo "bufenter"'}])
3660 call autocmd_add([#{replace: v:true, group: 'TestAcSet', event: 'BufEnter',
3661 \ pattern: '*.py', cmd: 'echo "bufenter"'}])
3662 call assert_equal([
3663 \ #{cmd: 'echo "bufenter"', group: 'TestAcSet', pattern: '*.py',
3664 \ nested: v:false, once: v:false, event: 'BufEnter'}],
3665 \ autocmd_get(#{group: 'TestAcSet'}))
3666
3667 " Test for adding a command for an unsupported autocmd event
Yegappan Lakshmanan1755a912022-05-19 10:31:47 +01003668 let l = [#{group: 'TestAcSet', event: 'abc', pattern: '*.sh',
3669 \ cmd: 'echo "bufadd"'}]
3670 call assert_fails('call autocmd_add(l)', 'E216:')
3671
Yegappan Lakshmanane0ff3a72022-05-27 18:05:33 +01003672 " Test for using a list of events and patterns
3673 call autocmd_delete([#{group: 'TestAcSet'}])
3674 let l = [#{group: 'TestAcSet', event: ['BufEnter', 'BufLeave'],
3675 \ pattern: ['*.py', '*.sh'], cmd: 'echo "bufcmds"'}]
3676 call autocmd_add(l)
3677 call assert_equal([
3678 \ #{cmd: 'echo "bufcmds"', group: 'TestAcSet', pattern: '*.py',
3679 \ nested: v:false, once: v:false, event: 'BufEnter'},
3680 \ #{cmd: 'echo "bufcmds"', group: 'TestAcSet', pattern: '*.sh',
3681 \ nested: v:false, once: v:false, event: 'BufEnter'},
3682 \ #{cmd: 'echo "bufcmds"', group: 'TestAcSet', pattern: '*.py',
3683 \ nested: v:false, once: v:false, event: 'BufLeave'},
3684 \ #{cmd: 'echo "bufcmds"', group: 'TestAcSet', pattern: '*.sh',
3685 \ nested: v:false, once: v:false, event: 'BufLeave'}],
3686 \ autocmd_get(#{group: 'TestAcSet'}))
3687
3688 " Test for invalid values for 'event' item
3689 call autocmd_delete([#{group: 'TestAcSet'}])
3690 let l = [#{group: 'TestAcSet', event: test_null_string(),
3691 \ pattern: "*.py", cmd: 'echo "bufcmds"'}]
3692 call assert_fails('call autocmd_add(l)', 'E928:')
3693 let l = [#{group: 'TestAcSet', event: test_null_list(),
3694 \ pattern: "*.py", cmd: 'echo "bufcmds"'}]
3695 call assert_fails('call autocmd_add(l)', 'E714:')
3696 let l = [#{group: 'TestAcSet', event: {},
3697 \ pattern: "*.py", cmd: 'echo "bufcmds"'}]
3698 call assert_fails('call autocmd_add(l)', 'E777:')
3699 let l = [#{group: 'TestAcSet', event: [{}],
3700 \ pattern: "*.py", cmd: 'echo "bufcmds"'}]
3701 call assert_fails('call autocmd_add(l)', 'E928:')
3702 let l = [#{group: 'TestAcSet', event: [test_null_string()],
3703 \ pattern: "*.py", cmd: 'echo "bufcmds"'}]
3704 call assert_fails('call autocmd_add(l)', 'E928:')
3705 let l = [#{group: 'TestAcSet', event: 'BufEnter,BufLeave',
3706 \ pattern: '*.py', cmd: 'echo "bufcmds"'}]
3707 call assert_fails('call autocmd_add(l)', 'E216:')
3708 let l = [#{group: 'TestAcSet', event: [],
3709 \ pattern: "*.py", cmd: 'echo "bufcmds"'}]
3710 call autocmd_add(l)
3711 let l = [#{group: 'TestAcSet', event: [""],
3712 \ pattern: "*.py", cmd: 'echo "bufcmds"'}]
3713 call assert_fails('call autocmd_add(l)', 'E216:')
3714 let l = [#{group: 'TestAcSet', event: "",
3715 \ pattern: "*.py", cmd: 'echo "bufcmds"'}]
3716 call autocmd_add(l)
3717 call assert_equal([], autocmd_get(#{group: 'TestAcSet'}))
3718
3719 " Test for invalid values for 'pattern' item
3720 let l = [#{group: 'TestAcSet', event: "BufEnter",
3721 \ pattern: test_null_string(), cmd: 'echo "bufcmds"'}]
Yegappan Lakshmanan00e977c2022-06-01 12:31:53 +01003722 call assert_fails('call autocmd_add(l)', 'E928:')
Yegappan Lakshmanane0ff3a72022-05-27 18:05:33 +01003723 let l = [#{group: 'TestAcSet', event: "BufEnter",
3724 \ pattern: test_null_list(), cmd: 'echo "bufcmds"'}]
3725 call assert_fails('call autocmd_add(l)', 'E714:')
3726 let l = [#{group: 'TestAcSet', event: "BufEnter",
3727 \ pattern: {}, cmd: 'echo "bufcmds"'}]
3728 call assert_fails('call autocmd_add(l)', 'E777:')
3729 let l = [#{group: 'TestAcSet', event: "BufEnter",
3730 \ pattern: [{}], cmd: 'echo "bufcmds"'}]
3731 call assert_fails('call autocmd_add(l)', 'E928:')
3732 let l = [#{group: 'TestAcSet', event: "BufEnter",
3733 \ pattern: [test_null_string()], cmd: 'echo "bufcmds"'}]
3734 call assert_fails('call autocmd_add(l)', 'E928:')
3735 let l = [#{group: 'TestAcSet', event: "BufEnter",
3736 \ pattern: [], cmd: 'echo "bufcmds"'}]
3737 call autocmd_add(l)
3738 let l = [#{group: 'TestAcSet', event: "BufEnter",
3739 \ pattern: [""], cmd: 'echo "bufcmds"'}]
3740 call autocmd_add(l)
3741 let l = [#{group: 'TestAcSet', event: "BufEnter",
3742 \ pattern: "", cmd: 'echo "bufcmds"'}]
3743 call autocmd_add(l)
3744 call assert_equal([], autocmd_get(#{group: 'TestAcSet'}))
3745
3746 let l = [#{group: 'TestAcSet', event: 'BufEnter,abc,BufLeave',
3747 \ pattern: '*.py', cmd: 'echo "bufcmds"'}]
3748 call assert_fails('call autocmd_add(l)', 'E216:')
3749
Yegappan Lakshmanan1755a912022-05-19 10:31:47 +01003750 call assert_fails("call autocmd_add({})", 'E1211:')
3751 call assert_equal(v:false, autocmd_add(test_null_list()))
3752 call assert_true(autocmd_add([[]]))
3753 call assert_true(autocmd_add([test_null_dict()]))
3754
3755 augroup TestAcSet
3756 au!
3757 augroup END
3758
3759 call autocmd_add([#{group: 'TestAcSet'}])
3760 call autocmd_add([#{group: 'TestAcSet', event: 'BufAdd'}])
3761 call autocmd_add([#{group: 'TestAcSet', pat: '*.sh'}])
3762 call autocmd_add([#{group: 'TestAcSet', cmd: 'echo "a"'}])
3763 call autocmd_add([#{group: 'TestAcSet', event: 'BufAdd', pat: '*.sh'}])
3764 call autocmd_add([#{group: 'TestAcSet', event: 'BufAdd', cmd: 'echo "a"'}])
3765 call autocmd_add([#{group: 'TestAcSet', pat: '*.sh', cmd: 'echo "a"'}])
3766 call assert_equal([], autocmd_get(#{group: 'TestAcSet'}))
3767
3768 augroup! TestAcSet
3769endfunc
3770
3771" Test for deleting autocmd events and groups
3772func Test_autocmd_delete()
3773 " Delete an event in an autocmd group
3774 augroup TestAcSet
3775 au!
3776 au BufAdd *.sh echo "bufadd"
3777 au BufEnter *.sh echo "bufenter"
3778 augroup END
3779 call autocmd_delete([#{group: 'TestAcSet', event: 'BufAdd'}])
3780 call assert_equal([#{cmd: 'echo "bufenter"', group: 'TestAcSet',
3781 \ pattern: '*.sh', nested: v:false, once: v:false,
3782 \ event: 'BufEnter'}], autocmd_get(#{group: 'TestAcSet'}))
3783
3784 " Delete all the events in an autocmd group
3785 augroup TestAcSet
3786 au BufAdd *.sh echo "bufadd"
3787 augroup END
3788 call autocmd_delete([#{group: 'TestAcSet', event: '*'}])
3789 call assert_equal([], autocmd_get(#{group: 'TestAcSet'}))
3790
3791 " Delete a non-existing autocmd group
3792 call assert_fails("call autocmd_delete([#{group: 'abc'}])", 'E367:')
3793 " Delete a non-existing autocmd event
3794 let l = [#{group: 'TestAcSet', event: 'abc'}]
3795 call assert_fails("call autocmd_delete(l)", 'E216:')
3796 " Delete a non-existing autocmd pattern
3797 let l = [#{group: 'TestAcSet', event: 'BufAdd', pat: 'abc'}]
3798 call assert_true(autocmd_delete(l))
Yegappan Lakshmanan00e977c2022-06-01 12:31:53 +01003799 " Delete an autocmd for a non-existing buffer
3800 let l = [#{event: '*', bufnr: 9999, cmd: 'echo "x"'}]
3801 call assert_fails('call autocmd_delete(l)', 'E680:')
Yegappan Lakshmanan1755a912022-05-19 10:31:47 +01003802
3803 " Delete an autocmd group
3804 augroup TestAcSet
3805 au!
3806 au BufAdd *.sh echo "bufadd"
3807 au BufEnter *.sh echo "bufenter"
3808 augroup END
3809 call autocmd_delete([#{group: 'TestAcSet'}])
3810 call assert_fails("call autocmd_get(#{group: 'TestAcSet'})", 'E367:')
3811
3812 call assert_true(autocmd_delete([[]]))
3813 call assert_true(autocmd_delete([test_null_dict()]))
3814endfunc
3815
Bram Moolenaarbc2b71d2020-02-17 21:33:30 +01003816" vim: shiftwidth=2 sts=2 expandtab