blob: aa204c4f37777553bb8e2a4122accad0f0e4fed3 [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!')
Bram Moolenaar3b014be2022-11-13 17:53:46 +000065 sleep 30m
Bram Moolenaarc67e8922016-05-24 16:07:40 +020066 call assert_equal(1, g:triggered)
Bram Moolenaar26d98212019-01-27 22:32:55 +010067 unlet g:triggered
68 au! CursorHoldI
69 set updatetime&
70 endfunc
71
72 func Test_cursorhold_insert_with_timer_interrupt()
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +020073 CheckFeature job
Bram Moolenaar26d98212019-01-27 22:32:55 +010074 " Need to move the cursor.
75 call feedkeys("ggG", "xt")
76
77 " Confirm the timer invoked in exit_cb of the job doesn't disturb
78 " CursorHoldI event.
79 let g:triggered = 0
80 au CursorHoldI * let g:triggered += 1
Bram Moolenaar62cd26a2020-10-11 20:08:44 +020081 set updatetime=100
Bram Moolenaar26d98212019-01-27 22:32:55 +010082 call job_start(has('win32') ? 'cmd /c echo:' : 'echo',
Bram Moolenaar62cd26a2020-10-11 20:08:44 +020083 \ {'exit_cb': {-> timer_start(200, 'ExitInsertMode')}})
Bram Moolenaar26d98212019-01-27 22:32:55 +010084 call feedkeys('a', 'x!')
85 call assert_equal(1, g:triggered)
86 unlet g:triggered
Bram Moolenaare99e8442016-07-26 20:43:40 +020087 au! CursorHoldI
Bram Moolenaaraeac9002016-09-06 22:15:08 +020088 set updatetime&
Bram Moolenaarc67e8922016-05-24 16:07:40 +020089 endfunc
90
91 func Test_cursorhold_insert_ctrl_x()
92 let g:triggered = 0
93 au CursorHoldI * let g:triggered += 1
94 set updatetime=20
95 call timer_start(100, 'ExitInsertMode')
96 " CursorHoldI does not trigger after CTRL-X
97 call feedkeys("a\<C-X>", 'x!')
98 call assert_equal(0, g:triggered)
Bram Moolenaar26d98212019-01-27 22:32:55 +010099 unlet g:triggered
Bram Moolenaare99e8442016-07-26 20:43:40 +0200100 au! CursorHoldI
Bram Moolenaaraeac9002016-09-06 22:15:08 +0200101 set updatetime&
Bram Moolenaarc67e8922016-05-24 16:07:40 +0200102 endfunc
Bram Moolenaar97b00752019-05-12 13:07:14 +0200103
Bram Moolenaar5a9357d2021-10-03 16:22:05 +0100104 func Test_cursorhold_insert_ctrl_g_U()
105 au CursorHoldI * :
106 set updatetime=20
107 new
108 call timer_start(100, { -> feedkeys("\<Left>foo\<Esc>", 't') })
109 call feedkeys("i()\<C-g>U", 'tx!')
110 sleep 200m
111 call assert_equal('(foo)', getline(1))
112 undo
113 call assert_equal('', getline(1))
114
115 bwipe!
116 au! CursorHoldI
117 set updatetime&
118 endfunc
119
Bram Moolenaar97b00752019-05-12 13:07:14 +0200120 func Test_OptionSet_modeline()
121 call test_override('starting', 1)
122 au! OptionSet
123 augroup set_tabstop
124 au OptionSet tabstop call timer_start(1, {-> execute("echo 'Handler called'", "")})
125 augroup END
Bram Moolenaare1f3ab72022-09-04 21:29:08 +0100126 call writefile(['vim: set ts=7 sw=5 :', 'something'], 'XoptionsetModeline', 'D')
Bram Moolenaar97b00752019-05-12 13:07:14 +0200127 set modeline
128 let v:errmsg = ''
129 call assert_fails('split XoptionsetModeline', 'E12:')
130 call assert_equal(7, &ts)
131 call assert_equal('', v:errmsg)
132
133 augroup set_tabstop
134 au!
135 augroup END
136 bwipe!
137 set ts&
Bram Moolenaar97b00752019-05-12 13:07:14 +0200138 call test_override('starting', 0)
139 endfunc
140
141endif "has('timers')
Bram Moolenaar40b1b542016-04-20 20:18:23 +0200142
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200143func Test_bufunload()
Bram Moolenaarc67e8922016-05-24 16:07:40 +0200144 augroup test_bufunload_group
145 autocmd!
146 autocmd BufUnload * call add(s:li, "bufunload")
147 autocmd BufDelete * call add(s:li, "bufdelete")
148 autocmd BufWipeout * call add(s:li, "bufwipeout")
149 augroup END
Bram Moolenaar40b1b542016-04-20 20:18:23 +0200150
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100151 let s:li = []
Bram Moolenaarc67e8922016-05-24 16:07:40 +0200152 new
153 setlocal bufhidden=
154 bunload
155 call assert_equal(["bufunload", "bufdelete"], s:li)
Bram Moolenaar40b1b542016-04-20 20:18:23 +0200156
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100157 let s:li = []
Bram Moolenaarc67e8922016-05-24 16:07:40 +0200158 new
159 setlocal bufhidden=delete
160 bunload
161 call assert_equal(["bufunload", "bufdelete"], s:li)
162
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100163 let s:li = []
Bram Moolenaarc67e8922016-05-24 16:07:40 +0200164 new
165 setlocal bufhidden=unload
166 bwipeout
167 call assert_equal(["bufunload", "bufdelete", "bufwipeout"], s:li)
168
Bram Moolenaare99e8442016-07-26 20:43:40 +0200169 au! test_bufunload_group
Bram Moolenaarc67e8922016-05-24 16:07:40 +0200170 augroup! test_bufunload_group
Bram Moolenaar40b1b542016-04-20 20:18:23 +0200171endfunc
Bram Moolenaar30445cb2016-07-09 15:21:02 +0200172
173" SEGV occurs in older versions. (At least 7.4.2005 or older)
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200174func Test_autocmd_bufunload_with_tabnext()
Bram Moolenaar30445cb2016-07-09 15:21:02 +0200175 tabedit
176 tabfirst
177
178 augroup test_autocmd_bufunload_with_tabnext_group
179 autocmd!
180 autocmd BufUnload <buffer> tabnext
181 augroup END
182
183 quit
184 call assert_equal(2, tabpagenr('$'))
185
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200186 autocmd! test_autocmd_bufunload_with_tabnext_group
Bram Moolenaar30445cb2016-07-09 15:21:02 +0200187 augroup! test_autocmd_bufunload_with_tabnext_group
188 tablast
189 quit
190endfunc
Bram Moolenaarc917da42016-07-19 22:31:36 +0200191
Bram Moolenaar5ed58c72021-01-28 14:24:55 +0100192func Test_argdelete_in_next()
193 au BufNew,BufEnter,BufLeave,BufWinEnter * argdel
194 call assert_fails('next a b', 'E1156:')
195 au! BufNew,BufEnter,BufLeave,BufWinEnter *
196endfunc
197
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200198func Test_autocmd_bufwinleave_with_tabfirst()
Bram Moolenaarf9e687e2016-09-04 21:33:09 +0200199 tabedit
200 augroup sample
201 autocmd!
202 autocmd BufWinLeave <buffer> tabfirst
203 augroup END
204 call setline(1, ['a', 'b', 'c'])
205 edit! a.txt
Bram Moolenaarf18c4db2016-09-08 22:10:06 +0200206 tabclose
Bram Moolenaarf9e687e2016-09-04 21:33:09 +0200207endfunc
208
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200209" SEGV occurs in older versions. (At least 7.4.2321 or older)
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200210func Test_autocmd_bufunload_avoiding_SEGV_01()
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200211 split aa.txt
212 let lastbuf = bufnr('$')
213
214 augroup test_autocmd_bufunload
215 autocmd!
216 exe 'autocmd BufUnload <buffer> ' . (lastbuf + 1) . 'bwipeout!'
217 augroup END
218
Bram Moolenaar28ee8922020-10-28 20:20:00 +0100219 call assert_fails('edit bb.txt', 'E937:')
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200220
221 autocmd! test_autocmd_bufunload
222 augroup! test_autocmd_bufunload
223 bwipe! aa.txt
224 bwipe! bb.txt
225endfunc
226
227" SEGV occurs in older versions. (At least 7.4.2321 or older)
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200228func Test_autocmd_bufunload_avoiding_SEGV_02()
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200229 setlocal buftype=nowrite
230 let lastbuf = bufnr('$')
231
232 augroup test_autocmd_bufunload
233 autocmd!
234 exe 'autocmd BufUnload <buffer> ' . (lastbuf + 1) . 'bwipeout!'
235 augroup END
236
237 normal! i1
238 call assert_fails('edit a.txt', 'E517:')
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200239
240 autocmd! test_autocmd_bufunload
241 augroup! test_autocmd_bufunload
242 bwipe! a.txt
243endfunc
244
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100245func Test_autocmd_dummy_wipeout()
246 " prepare files
Bram Moolenaare1f3ab72022-09-04 21:29:08 +0100247 call writefile([''], 'Xdummywipetest1.txt', 'D')
248 call writefile([''], 'Xdummywipetest2.txt', 'D')
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100249 augroup test_bufunload_group
250 autocmd!
251 autocmd BufUnload * call add(s:li, "bufunload")
252 autocmd BufDelete * call add(s:li, "bufdelete")
253 autocmd BufWipeout * call add(s:li, "bufwipeout")
254 augroup END
255
256 let s:li = []
257 split Xdummywipetest1.txt
258 silent! vimgrep /notmatched/ Xdummywipetest*
259 call assert_equal(["bufunload", "bufwipeout"], s:li)
260
261 bwipeout
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100262 au! test_bufunload_group
263 augroup! test_bufunload_group
264endfunc
265
Bram Moolenaarc917da42016-07-19 22:31:36 +0200266func Test_win_tab_autocmd()
267 let g:record = []
268
269 augroup testing
270 au WinNew * call add(g:record, 'WinNew')
naohiro ono23beefe2021-11-13 12:38:49 +0000271 au WinClosed * call add(g:record, 'WinClosed')
Bram Moolenaarc917da42016-07-19 22:31:36 +0200272 au WinEnter * call add(g:record, 'WinEnter')
273 au WinLeave * call add(g:record, 'WinLeave')
274 au TabNew * call add(g:record, 'TabNew')
Bram Moolenaar12c11d52016-07-19 23:13:03 +0200275 au TabClosed * call add(g:record, 'TabClosed')
Bram Moolenaarc917da42016-07-19 22:31:36 +0200276 au TabEnter * call add(g:record, 'TabEnter')
277 au TabLeave * call add(g:record, 'TabLeave')
278 augroup END
279
280 split
281 tabnew
282 close
283 close
284
285 call assert_equal([
286 \ 'WinLeave', 'WinNew', 'WinEnter',
287 \ 'WinLeave', 'TabLeave', 'WinNew', 'WinEnter', 'TabNew', 'TabEnter',
naohiro ono23beefe2021-11-13 12:38:49 +0000288 \ 'WinLeave', 'TabLeave', 'WinClosed', 'TabClosed', 'WinEnter', 'TabEnter',
289 \ 'WinLeave', 'WinClosed', 'WinEnter'
Bram Moolenaarc917da42016-07-19 22:31:36 +0200290 \ ], g:record)
291
Bram Moolenaar12c11d52016-07-19 23:13:03 +0200292 let g:record = []
293 tabnew somefile
294 tabnext
295 bwipe somefile
296
297 call assert_equal([
298 \ 'WinLeave', 'TabLeave', 'WinNew', 'WinEnter', 'TabNew', 'TabEnter',
299 \ 'WinLeave', 'TabLeave', 'WinEnter', 'TabEnter',
naohiro ono23beefe2021-11-13 12:38:49 +0000300 \ 'WinClosed', 'TabClosed'
Bram Moolenaar12c11d52016-07-19 23:13:03 +0200301 \ ], g:record)
302
Bram Moolenaarc917da42016-07-19 22:31:36 +0200303 augroup testing
304 au!
305 augroup END
306 unlet g:record
307endfunc
Bram Moolenaare99e8442016-07-26 20:43:40 +0200308
Bram Moolenaar35fc61c2022-11-22 12:40:50 +0000309func Test_WinResized()
310 CheckRunVimInTerminal
311
312 let lines =<< trim END
313 set scrolloff=0
314 call setline(1, ['111', '222'])
315 vnew
316 call setline(1, ['aaa', 'bbb'])
317 new
318 call setline(1, ['foo', 'bar'])
319
320 let g:resized = 0
321 au WinResized * let g:resized += 1
322
323 func WriteResizedEvent()
324 call writefile([json_encode(v:event)], 'XresizeEvent')
325 endfunc
326 au WinResized * call WriteResizedEvent()
327 END
328 call writefile(lines, 'Xtest_winresized', 'D')
329 let buf = RunVimInTerminal('-S Xtest_winresized', {'rows': 10})
330
331 " redraw now to avoid a redraw after the :echo command
332 call term_sendkeys(buf, ":redraw!\<CR>")
333 call TermWait(buf)
334
335 call term_sendkeys(buf, ":echo g:resized\<CR>")
336 call WaitForAssert({-> assert_match('^0$', term_getline(buf, 10))}, 1000)
337
338 " increase window height, two windows will be reported
339 call term_sendkeys(buf, "\<C-W>+")
340 call TermWait(buf)
341 call term_sendkeys(buf, ":echo g:resized\<CR>")
342 call WaitForAssert({-> assert_match('^1$', term_getline(buf, 10))}, 1000)
343
344 let event = readfile('XresizeEvent')[0]->json_decode()
345 call assert_equal({
346 \ 'windows': [1002, 1001],
347 \ }, event)
348
349 " increase window width, three windows will be reported
350 call term_sendkeys(buf, "\<C-W>>")
351 call TermWait(buf)
352 call term_sendkeys(buf, ":echo g:resized\<CR>")
353 call WaitForAssert({-> assert_match('^2$', term_getline(buf, 10))}, 1000)
354
355 let event = readfile('XresizeEvent')[0]->json_decode()
356 call assert_equal({
357 \ 'windows': [1002, 1001, 1000],
358 \ }, event)
359
360 call delete('XresizeEvent')
361 call StopVimInTerminal(buf)
362endfunc
363
LemonBoy09371822022-04-08 15:18:45 +0100364func Test_WinScrolled()
365 CheckRunVimInTerminal
366
367 let lines =<< trim END
zeertzjqd58862d2022-04-12 11:32:48 +0100368 set nowrap scrolloff=0
369 for ii in range(1, 18)
370 call setline(ii, repeat(nr2char(96 + ii), ii * 2))
371 endfor
372 let win_id = win_getid()
373 let g:matched = v:false
Bram Moolenaar35fc61c2022-11-22 12:40:50 +0000374 func WriteScrollEvent()
375 call writefile([json_encode(v:event)], 'XscrollEvent')
376 endfunc
zeertzjqd58862d2022-04-12 11:32:48 +0100377 execute 'au WinScrolled' win_id 'let g:matched = v:true'
378 let g:scrolled = 0
379 au WinScrolled * let g:scrolled += 1
380 au WinScrolled * let g:amatch = str2nr(expand('<amatch>'))
381 au WinScrolled * let g:afile = str2nr(expand('<afile>'))
Bram Moolenaar35fc61c2022-11-22 12:40:50 +0000382 au WinScrolled * call WriteScrollEvent()
LemonBoy09371822022-04-08 15:18:45 +0100383 END
Bram Moolenaare1f3ab72022-09-04 21:29:08 +0100384 call writefile(lines, 'Xtest_winscrolled', 'D')
LemonBoy09371822022-04-08 15:18:45 +0100385 let buf = RunVimInTerminal('-S Xtest_winscrolled', {'rows': 6})
386
387 call term_sendkeys(buf, ":echo g:scrolled\<CR>")
388 call WaitForAssert({-> assert_match('^0 ', term_getline(buf, 6))}, 1000)
389
390 " Scroll left/right in Normal mode.
391 call term_sendkeys(buf, "zlzh:echo g:scrolled\<CR>")
392 call WaitForAssert({-> assert_match('^2 ', term_getline(buf, 6))}, 1000)
393
Bram Moolenaar35fc61c2022-11-22 12:40:50 +0000394 let event = readfile('XscrollEvent')[0]->json_decode()
395 call assert_equal({
396 \ 'all': {'leftcol': 1, 'topline': 0, 'width': 0, 'height': 0, 'skipcol': 0},
397 \ '1000': {'leftcol': -1, 'topline': 0, 'width': 0, 'height': 0, 'skipcol': 0}
398 \ }, event)
399
LemonBoy09371822022-04-08 15:18:45 +0100400 " Scroll up/down in Normal mode.
401 call term_sendkeys(buf, "\<c-e>\<c-y>:echo g:scrolled\<CR>")
402 call WaitForAssert({-> assert_match('^4 ', term_getline(buf, 6))}, 1000)
403
Bram Moolenaar35fc61c2022-11-22 12:40:50 +0000404 let event = readfile('XscrollEvent')[0]->json_decode()
405 call assert_equal({
406 \ 'all': {'leftcol': 0, 'topline': 1, 'width': 0, 'height': 0, 'skipcol': 0},
407 \ '1000': {'leftcol': 0, 'topline': -1, 'width': 0, 'height': 0, 'skipcol': 0}
408 \ }, event)
409
LemonBoy09371822022-04-08 15:18:45 +0100410 " Scroll up/down in Insert mode.
411 call term_sendkeys(buf, "Mi\<c-x>\<c-e>\<Esc>i\<c-x>\<c-y>\<Esc>")
412 call term_sendkeys(buf, ":echo g:scrolled\<CR>")
413 call WaitForAssert({-> assert_match('^6 ', term_getline(buf, 6))}, 1000)
414
Bram Moolenaar35fc61c2022-11-22 12:40:50 +0000415 let event = readfile('XscrollEvent')[0]->json_decode()
416 call assert_equal({
417 \ 'all': {'leftcol': 0, 'topline': 1, 'width': 0, 'height': 0, 'skipcol': 0},
418 \ '1000': {'leftcol': 0, 'topline': -1, 'width': 0, 'height': 0, 'skipcol': 0}
419 \ }, event)
420
LemonBoy09371822022-04-08 15:18:45 +0100421 " Scroll the window horizontally to focus the last letter of the third line
422 " containing only six characters. Moving to the previous and shorter lines
423 " should trigger another autocommand as Vim has to make them visible.
424 call term_sendkeys(buf, "5zl2k")
425 call term_sendkeys(buf, ":echo g:scrolled\<CR>")
426 call WaitForAssert({-> assert_match('^8 ', term_getline(buf, 6))}, 1000)
427
Bram Moolenaar35fc61c2022-11-22 12:40:50 +0000428 let event = readfile('XscrollEvent')[0]->json_decode()
429 call assert_equal({
430 \ 'all': {'leftcol': 5, 'topline': 0, 'width': 0, 'height': 0, 'skipcol': 0},
431 \ '1000': {'leftcol': -5, 'topline': 0, 'width': 0, 'height': 0, 'skipcol': 0}
432 \ }, event)
433
LemonBoy09371822022-04-08 15:18:45 +0100434 " Ensure the command was triggered for the specified window ID.
435 call term_sendkeys(buf, ":echo g:matched\<CR>")
436 call WaitForAssert({-> assert_match('^v:true ', term_getline(buf, 6))}, 1000)
437
438 " Ensure the expansion of <amatch> and <afile> matches the window ID.
439 call term_sendkeys(buf, ":echo g:amatch == win_id && g:afile == win_id\<CR>")
440 call WaitForAssert({-> assert_match('^v:true ', term_getline(buf, 6))}, 1000)
441
Bram Moolenaar35fc61c2022-11-22 12:40:50 +0000442 call delete('XscrollEvent')
LemonBoy09371822022-04-08 15:18:45 +0100443 call StopVimInTerminal(buf)
LemonBoy09371822022-04-08 15:18:45 +0100444endfunc
445
LemonBoy66e13ae2022-04-21 22:52:11 +0100446func Test_WinScrolled_mouse()
447 CheckRunVimInTerminal
448
449 let lines =<< trim END
450 set nowrap scrolloff=0
451 set mouse=a term=xterm ttymouse=sgr mousetime=200 clipboard=
452 call setline(1, ['foo']->repeat(32))
453 split
454 let g:scrolled = 0
455 au WinScrolled * let g:scrolled += 1
456 END
Bram Moolenaare1f3ab72022-09-04 21:29:08 +0100457 call writefile(lines, 'Xtest_winscrolled_mouse', 'D')
LemonBoy66e13ae2022-04-21 22:52:11 +0100458 let buf = RunVimInTerminal('-S Xtest_winscrolled_mouse', {'rows': 10})
459
460 " With the upper split focused, send a scroll-down event to the unfocused one.
461 call test_setmouse(7, 1)
462 call term_sendkeys(buf, "\<ScrollWheelDown>")
463 call TermWait(buf)
464 call term_sendkeys(buf, ":echo g:scrolled\<CR>")
465 call WaitForAssert({-> assert_match('^1', term_getline(buf, 10))}, 1000)
466
467 " Again, but this time while we're in insert mode.
468 call term_sendkeys(buf, "i\<ScrollWheelDown>\<Esc>")
469 call TermWait(buf)
470 call term_sendkeys(buf, ":echo g:scrolled\<CR>")
471 call WaitForAssert({-> assert_match('^2', term_getline(buf, 10))}, 1000)
472
473 call StopVimInTerminal(buf)
LemonBoy66e13ae2022-04-21 22:52:11 +0100474endfunc
475
zeertzjqd58862d2022-04-12 11:32:48 +0100476func Test_WinScrolled_close_curwin()
477 CheckRunVimInTerminal
478
479 let lines =<< trim END
480 set nowrap scrolloff=0
481 call setline(1, ['aaa', 'bbb'])
482 vsplit
483 au WinScrolled * close
484 au VimLeave * call writefile(['123456'], 'Xtestout')
485 END
Bram Moolenaare1f3ab72022-09-04 21:29:08 +0100486 call writefile(lines, 'Xtest_winscrolled_close_curwin', 'D')
zeertzjqd58862d2022-04-12 11:32:48 +0100487 let buf = RunVimInTerminal('-S Xtest_winscrolled_close_curwin', {'rows': 6})
488
489 " This was using freed memory
490 call term_sendkeys(buf, "\<C-E>")
491 call TermWait(buf)
492 call StopVimInTerminal(buf)
493
Bram Moolenaar0a60f792022-11-19 21:18:11 +0000494 " check the startup script finished to the end
zeertzjqd58862d2022-04-12 11:32:48 +0100495 call assert_equal(['123456'], readfile('Xtestout'))
zeertzjqd58862d2022-04-12 11:32:48 +0100496 call delete('Xtestout')
497endfunc
498
Bram Moolenaar0a60f792022-11-19 21:18:11 +0000499func Test_WinScrolled_once_only()
500 CheckRunVimInTerminal
501
502 let lines =<< trim END
503 set cmdheight=2
504 call setline(1, ['aaa', 'bbb'])
505 let trigger_count = 0
506 func ShowInfo(id)
507 echo g:trigger_count g:winid winlayout()
508 endfunc
509
510 vsplit
511 split
512 " use a timer to show the info after a redraw
513 au WinScrolled * let trigger_count += 1 | let winid = expand('<amatch>') | call timer_start(100, 'ShowInfo')
514 wincmd j
515 wincmd l
516 END
517 call writefile(lines, 'Xtest_winscrolled_once', 'D')
518 let buf = RunVimInTerminal('-S Xtest_winscrolled_once', #{rows: 10, cols: 60, statusoff: 2})
519
520 call term_sendkeys(buf, "\<C-E>")
521 call VerifyScreenDump(buf, 'Test_winscrolled_once_only_1', {})
522
523 call StopVimInTerminal(buf)
524endfunc
525
Bram Moolenaar29967732022-11-20 12:11:45 +0000526" Check that WinScrolled is not triggered immediately when defined and there
527" are split windows.
528func Test_WinScrolled_not_when_defined()
529 CheckRunVimInTerminal
530
531 let lines =<< trim END
532 call setline(1, ['aaa', 'bbb'])
533 echo 'nothing happened'
534 func ShowTriggered(id)
535 echo 'triggered'
536 endfunc
537 END
538 call writefile(lines, 'Xtest_winscrolled_not', 'D')
539 let buf = RunVimInTerminal('-S Xtest_winscrolled_not', #{rows: 10, cols: 60, statusoff: 2})
540 call term_sendkeys(buf, ":split\<CR>")
541 call TermWait(buf)
542 " use a timer to show the message after redrawing
543 call term_sendkeys(buf, ":au WinScrolled * call timer_start(100, 'ShowTriggered')\<CR>")
544 call VerifyScreenDump(buf, 'Test_winscrolled_not_when_defined_1', {})
545
546 call term_sendkeys(buf, "\<C-E>")
547 call VerifyScreenDump(buf, 'Test_winscrolled_not_when_defined_2', {})
548
549 call StopVimInTerminal(buf)
550endfunc
551
zeertzjq670ab032022-08-28 19:16:15 +0100552func Test_WinScrolled_long_wrapped()
553 CheckRunVimInTerminal
554
555 let lines =<< trim END
556 set scrolloff=0
557 let height = winheight(0)
558 let width = winwidth(0)
559 let g:scrolled = 0
560 au WinScrolled * let g:scrolled += 1
561 call setline(1, repeat('foo', height * width))
562 call cursor(1, height * width)
563 END
Bram Moolenaare1f3ab72022-09-04 21:29:08 +0100564 call writefile(lines, 'Xtest_winscrolled_long_wrapped', 'D')
zeertzjq670ab032022-08-28 19:16:15 +0100565 let buf = RunVimInTerminal('-S Xtest_winscrolled_long_wrapped', {'rows': 6})
566
567 call term_sendkeys(buf, ":echo g:scrolled\<CR>")
568 call WaitForAssert({-> assert_match('^0 ', term_getline(buf, 6))}, 1000)
569
570 call term_sendkeys(buf, 'gj')
571 call term_sendkeys(buf, ":echo g:scrolled\<CR>")
572 call WaitForAssert({-> assert_match('^1 ', term_getline(buf, 6))}, 1000)
573
574 call term_sendkeys(buf, '0')
575 call term_sendkeys(buf, ":echo g:scrolled\<CR>")
576 call WaitForAssert({-> assert_match('^2 ', term_getline(buf, 6))}, 1000)
577
578 call term_sendkeys(buf, '$')
579 call term_sendkeys(buf, ":echo g:scrolled\<CR>")
580 call WaitForAssert({-> assert_match('^3 ', term_getline(buf, 6))}, 1000)
zeertzjq670ab032022-08-28 19:16:15 +0100581endfunc
582
naohiro ono23beefe2021-11-13 12:38:49 +0000583func Test_WinClosed()
584 " Test that the pattern is matched against the closed window's ID, and both
585 " <amatch> and <afile> are set to it.
586 new
587 let winid = win_getid()
588 let g:matched = v:false
589 augroup test-WinClosed
590 autocmd!
591 execute 'autocmd WinClosed' winid 'let g:matched = v:true'
592 autocmd WinClosed * let g:amatch = str2nr(expand('<amatch>'))
593 autocmd WinClosed * let g:afile = str2nr(expand('<afile>'))
594 augroup END
595 close
596 call assert_true(g:matched)
597 call assert_equal(winid, g:amatch)
598 call assert_equal(winid, g:afile)
599
600 " Test that WinClosed is non-recursive.
601 new
602 new
603 call assert_equal(3, winnr('$'))
604 let g:triggered = 0
605 augroup test-WinClosed
606 autocmd!
607 autocmd WinClosed * let g:triggered += 1
608 autocmd WinClosed * 2 wincmd c
609 augroup END
610 close
611 call assert_equal(1, winnr('$'))
612 call assert_equal(1, g:triggered)
613
614 autocmd! test-WinClosed
615 augroup! test-WinClosed
616 unlet g:matched
617 unlet g:amatch
618 unlet g:afile
619 unlet g:triggered
620endfunc
621
Bram Moolenaarc947b9a2022-04-06 17:59:21 +0100622func Test_WinClosed_throws()
623 vnew
624 let bnr = bufnr()
625 call assert_equal(1, bufloaded(bnr))
626 augroup test-WinClosed
627 autocmd WinClosed * throw 'foo'
628 augroup END
629 try
630 close
631 catch /.*/
632 endtry
633 call assert_equal(0, bufloaded(bnr))
634
635 autocmd! test-WinClosed
636 augroup! test-WinClosed
637endfunc
638
zeertzjq6a069402022-04-07 14:08:29 +0100639func Test_WinClosed_throws_with_tabs()
640 tabnew
641 let bnr = bufnr()
642 call assert_equal(1, bufloaded(bnr))
643 augroup test-WinClosed
644 autocmd WinClosed * throw 'foo'
645 augroup END
646 try
647 close
648 catch /.*/
649 endtry
650 call assert_equal(0, bufloaded(bnr))
651
652 autocmd! test-WinClosed
653 augroup! test-WinClosed
654endfunc
655
zeertzjq62de54b2022-09-22 18:08:37 +0100656" This used to trigger WinClosed twice for the same window, and the window's
657" buffer was NULL in the second autocommand.
658func Test_WinClosed_switch_tab()
659 edit Xa
660 split Xb
661 split Xc
662 tab split
663 new
664 augroup test-WinClosed
665 autocmd WinClosed * tabprev | bwipe!
666 augroup END
667 close
668 " Check that the tabline has been fully removed
669 call assert_equal([1, 1], win_screenpos(0))
670
671 autocmd! test-WinClosed
672 augroup! test-WinClosed
673 %bwipe!
674endfunc
675
Bram Moolenaare99e8442016-07-26 20:43:40 +0200676func s:AddAnAutocmd()
677 augroup vimBarTest
678 au BufReadCmd * echo 'hello'
679 augroup END
680 call assert_equal(3, len(split(execute('au vimBarTest'), "\n")))
681endfunc
682
683func Test_early_bar()
684 " test that a bar is recognized before the {event}
685 call s:AddAnAutocmd()
Bram Moolenaarb8e642f2021-11-20 10:38:25 +0000686 augroup vimBarTest | au! | let done = 77 | augroup END
Bram Moolenaare99e8442016-07-26 20:43:40 +0200687 call assert_equal(1, len(split(execute('au vimBarTest'), "\n")))
Bram Moolenaarb8e642f2021-11-20 10:38:25 +0000688 call assert_equal(77, done)
Bram Moolenaare99e8442016-07-26 20:43:40 +0200689
690 call s:AddAnAutocmd()
Bram Moolenaarb8e642f2021-11-20 10:38:25 +0000691 augroup vimBarTest| au!| let done = 88 | augroup END
Bram Moolenaare99e8442016-07-26 20:43:40 +0200692 call assert_equal(1, len(split(execute('au vimBarTest'), "\n")))
Bram Moolenaarb8e642f2021-11-20 10:38:25 +0000693 call assert_equal(88, done)
Bram Moolenaare99e8442016-07-26 20:43:40 +0200694
695 " test that a bar is recognized after the {event}
696 call s:AddAnAutocmd()
Bram Moolenaarb8e642f2021-11-20 10:38:25 +0000697 augroup vimBarTest| au!BufReadCmd| let done = 99 | augroup END
Bram Moolenaare99e8442016-07-26 20:43:40 +0200698 call assert_equal(1, len(split(execute('au vimBarTest'), "\n")))
Bram Moolenaarb8e642f2021-11-20 10:38:25 +0000699 call assert_equal(99, done)
Bram Moolenaare99e8442016-07-26 20:43:40 +0200700
701 " test that a bar is recognized after the {group}
702 call s:AddAnAutocmd()
703 au! vimBarTest|echo 'hello'
704 call assert_equal(1, len(split(execute('au vimBarTest'), "\n")))
705endfunc
Bram Moolenaarf2c4c392016-07-29 20:50:24 +0200706
Bram Moolenaar5c809082016-09-01 16:21:48 +0200707func RemoveGroup()
708 autocmd! StartOK
709 augroup! StartOK
710endfunc
711
Bram Moolenaarf2c4c392016-07-29 20:50:24 +0200712func Test_augroup_warning()
713 augroup TheWarning
714 au VimEnter * echo 'entering'
715 augroup END
Bram Moolenaar5dc4e2f2020-11-25 14:15:12 +0100716 call assert_match("TheWarning.*VimEnter", execute('au VimEnter'))
Bram Moolenaarf2c4c392016-07-29 20:50:24 +0200717 redir => res
718 augroup! TheWarning
719 redir END
Bram Moolenaar5dc4e2f2020-11-25 14:15:12 +0100720 call assert_match("W19:", res)
721 call assert_match("-Deleted-.*VimEnter", execute('au VimEnter'))
Bram Moolenaarf2c4c392016-07-29 20:50:24 +0200722
723 " check "Another" does not take the pace of the deleted entry
724 augroup Another
725 augroup END
Bram Moolenaar5dc4e2f2020-11-25 14:15:12 +0100726 call assert_match("-Deleted-.*VimEnter", execute('au VimEnter'))
Bram Moolenaaraeac9002016-09-06 22:15:08 +0200727 augroup! Another
Bram Moolenaar5c809082016-09-01 16:21:48 +0200728
729 " no warning for postpone aucmd delete
730 augroup StartOK
731 au VimEnter * call RemoveGroup()
732 augroup END
Bram Moolenaar5dc4e2f2020-11-25 14:15:12 +0100733 call assert_match("StartOK.*VimEnter", execute('au VimEnter'))
Bram Moolenaar5c809082016-09-01 16:21:48 +0200734 redir => res
735 doautocmd VimEnter
736 redir END
Bram Moolenaar5dc4e2f2020-11-25 14:15:12 +0100737 call assert_notmatch("W19:", res)
Bram Moolenaarde653f02016-09-03 16:59:06 +0200738 au! VimEnter
Bram Moolenaarad48e6c2020-04-21 22:19:45 +0200739
740 call assert_fails('augroup!', 'E471:')
Bram Moolenaarf2c4c392016-07-29 20:50:24 +0200741endfunc
Bram Moolenaarb62cc362016-09-03 16:43:53 +0200742
Bram Moolenaar8d84ff12017-10-26 16:42:16 +0200743func Test_BufReadCmdHelp()
744 " This used to cause access to free memory
745 au BufReadCmd * e +h
746 help
747
Bram Moolenaar8d84ff12017-10-26 16:42:16 +0200748 au! BufReadCmd
749endfunc
750
751func Test_BufReadCmdHelpJump()
752 " This used to cause access to free memory
753 au BufReadCmd * e +h{
Bram Moolenaarcf1ba352017-10-27 00:55:04 +0200754 " } to fix highlighting
755 call assert_fails('help', 'E434:')
Bram Moolenaar8d84ff12017-10-26 16:42:16 +0200756
Bram Moolenaar8d84ff12017-10-26 16:42:16 +0200757 au! BufReadCmd
758endfunc
759
zeertzjq93f72cc2022-08-26 15:34:52 +0100760" BufReadCmd is triggered for a "nofile" buffer. Check all values.
Bram Moolenaarb1d2c812022-08-26 11:55:01 +0100761func Test_BufReadCmdNofile()
zeertzjq93f72cc2022-08-26 15:34:52 +0100762 for val in ['nofile',
763 \ 'nowrite',
764 \ 'acwrite',
765 \ 'quickfix',
766 \ 'help',
767 \ 'terminal',
768 \ 'prompt',
769 \ 'popup',
770 \ ]
771 new somefile
772 exe 'set buftype=' .. val
773 au BufReadCmd somefile call setline(1, 'triggered')
774 edit
775 call assert_equal('triggered', getline(1))
Bram Moolenaarb1d2c812022-08-26 11:55:01 +0100776
zeertzjq93f72cc2022-08-26 15:34:52 +0100777 au! BufReadCmd
778 bwipe!
779 endfor
Bram Moolenaarb1d2c812022-08-26 11:55:01 +0100780endfunc
781
Bram Moolenaarb62cc362016-09-03 16:43:53 +0200782func Test_augroup_deleted()
Bram Moolenaarde653f02016-09-03 16:59:06 +0200783 " This caused a crash before E936 was introduced
Bram Moolenaarb62cc362016-09-03 16:43:53 +0200784 augroup x
Bram Moolenaarde653f02016-09-03 16:59:06 +0200785 call assert_fails('augroup! x', 'E936:')
786 au VimEnter * echo
787 augroup end
Bram Moolenaarb62cc362016-09-03 16:43:53 +0200788 augroup! x
Bram Moolenaar5dc4e2f2020-11-25 14:15:12 +0100789 call assert_match("-Deleted-.*VimEnter", execute('au VimEnter'))
Bram Moolenaarde653f02016-09-03 16:59:06 +0200790 au! VimEnter
Bram Moolenaarb62cc362016-09-03 16:43:53 +0200791endfunc
792
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200793" Tests for autocommands on :close command.
794" This used to be in test13.
795func Test_three_windows()
Bram Moolenaarb3435b02016-09-29 20:54:59 +0200796 " Clean up buffers, because in some cases this function fails.
797 call s:cleanup_buffers()
798
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200799 " Write three files and open them, each in a window.
800 " Then go to next window, with autocommand that deletes the previous one.
801 " Do this twice, writing the file.
802 e! Xtestje1
803 call setline(1, 'testje1')
804 w
805 sp Xtestje2
806 call setline(1, 'testje2')
807 w
808 sp Xtestje3
809 call setline(1, 'testje3')
810 w
811 wincmd w
812 au WinLeave Xtestje2 bwipe
813 wincmd w
814 call assert_equal('Xtestje1', expand('%'))
815
816 au WinLeave Xtestje1 bwipe Xtestje3
817 close
818 call assert_equal('Xtestje1', expand('%'))
819
820 " Test deleting the buffer on a Unload event. If this goes wrong there
821 " will be the ATTENTION prompt.
822 e Xtestje1
823 au!
824 au! BufUnload Xtestje1 bwipe
825 call assert_fails('e Xtestje3', 'E937:')
826 call assert_equal('Xtestje3', expand('%'))
827
828 e Xtestje2
829 sp Xtestje1
830 call assert_fails('e', 'E937:')
Bram Moolenaara997b452018-04-17 23:24:06 +0200831 call assert_equal('Xtestje1', expand('%'))
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200832
833 " Test changing buffers in a BufWipeout autocommand. If this goes wrong
834 " there are ml_line errors and/or a Crash.
835 au!
836 only
837 e Xanother
838 e Xtestje1
839 bwipe Xtestje2
840 bwipe Xtestje3
841 au BufWipeout Xtestje1 buf Xtestje1
842 bwipe
843 call assert_equal('Xanother', expand('%'))
844
845 only
846 help
847 wincmd w
848 1quit
849 call assert_equal('Xanother', expand('%'))
850
851 au!
Bram Moolenaar4520d442017-03-19 16:09:46 +0100852 enew
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200853 call delete('Xtestje1')
854 call delete('Xtestje2')
855 call delete('Xtestje3')
856endfunc
Bram Moolenaare13b9af2017-01-13 22:01:02 +0100857
858func Test_BufEnter()
859 au! BufEnter
860 au Bufenter * let val = val . '+'
861 let g:val = ''
862 split NewFile
863 call assert_equal('+', g:val)
864 bwipe!
865 call assert_equal('++', g:val)
866
867 " Also get BufEnter when editing a directory
Bram Moolenaar6f14da12022-09-07 21:30:44 +0100868 call mkdir('Xbufenterdir', 'D')
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +0100869 split Xbufenterdir
Bram Moolenaare13b9af2017-01-13 22:01:02 +0100870 call assert_equal('+++', g:val)
Bram Moolenaare94260f2017-03-21 15:50:12 +0100871
872 " On MS-Windows we can't edit the directory, make sure we wipe the right
873 " buffer.
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +0100874 bwipe! Xbufenterdir
Bram Moolenaare13b9af2017-01-13 22:01:02 +0100875 au! BufEnter
Bram Moolenaara9b5b852022-08-26 13:16:20 +0100876
877 " Editing a "nofile" buffer doesn't read the file but does trigger BufEnter
zeertzjq93f72cc2022-08-26 15:34:52 +0100878 " for historic reasons. Also test other 'buftype' values.
879 for val in ['nofile',
880 \ 'nowrite',
881 \ 'acwrite',
882 \ 'quickfix',
883 \ 'help',
884 \ 'terminal',
885 \ 'prompt',
886 \ 'popup',
887 \ ]
888 new somefile
889 exe 'set buftype=' .. val
890 au BufEnter somefile call setline(1, 'some text')
891 edit
892 call assert_equal('some text', getline(1))
893 bwipe!
894 au! BufEnter
895 endfor
Bram Moolenaar9fda8152022-11-19 13:14:10 +0000896
897 new
898 new
899 autocmd BufEnter * ++once close
900 call assert_fails('close', 'E1312:')
901
902 au! BufEnter
903 only
Bram Moolenaare13b9af2017-01-13 22:01:02 +0100904endfunc
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100905
906" Closing a window might cause an endless loop
907" E814 for older Vims
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200908func Test_autocmd_bufwipe_in_SessLoadPost()
Bram Moolenaar1d68d9b2017-10-13 22:33:32 +0200909 edit Xtest
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100910 tabnew
Bram Moolenaar1d68d9b2017-10-13 22:33:32 +0200911 file Xsomething
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100912 set noswapfile
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100913 mksession!
914
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200915 let content =<< trim [CODE]
Bram Moolenaar62cd26a2020-10-11 20:08:44 +0200916 call test_override('ui_delay', 10)
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200917 set nocp noswapfile
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100918 let v:swapchoice = "e"
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200919 augroup test_autocmd_sessionload
920 autocmd!
921 autocmd SessionLoadPost * exe bufnr("Xsomething") . "bw!"
922 augroup END
923
924 func WriteErrors()
Bram Moolenaarbe9fc5b2022-09-09 17:09:35 +0100925 call writefile([execute("messages")], "XerrorsBwipe")
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200926 endfunc
927 au VimLeave * call WriteErrors()
928 [CODE]
929
Bram Moolenaare1f3ab72022-09-04 21:29:08 +0100930 call writefile(content, 'Xvimrc', 'D')
Bram Moolenaar93344c22019-08-14 21:12:05 +0200931 call system(GetVimCommand('Xvimrc') .. ' --not-a-term --noplugins -S Session.vim -c cq')
Bram Moolenaarae04a602022-09-09 15:08:10 +0100932 sleep 100m
Bram Moolenaarbe9fc5b2022-09-09 17:09:35 +0100933 let errors = join(readfile('XerrorsBwipe'))
Bram Moolenaare2e40752020-09-04 21:18:46 +0200934 call assert_match('E814:', errors)
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100935
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100936 set swapfile
Bram Moolenaarbe9fc5b2022-09-09 17:09:35 +0100937 for file in ['Session.vim', 'XerrorsBwipe']
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100938 call delete(file)
939 endfor
940endfunc
941
Bram Moolenaar797e63b2021-01-15 16:22:52 +0100942" Using :blast and :ball for many events caused a crash, because b_nwindows was
943" not incremented correctly.
944func Test_autocmd_blast_badd()
945 let content =<< trim [CODE]
946 au BufNew,BufAdd,BufWinEnter,BufEnter,BufLeave,BufWinLeave,BufUnload,VimEnter foo* blast
947 edit foo1
948 au BufNew,BufAdd,BufWinEnter,BufEnter,BufLeave,BufWinLeave,BufUnload,VimEnter foo* ball
949 edit foo2
Bram Moolenaarbe9fc5b2022-09-09 17:09:35 +0100950 call writefile(['OK'], 'XerrorsBlast')
Bram Moolenaar797e63b2021-01-15 16:22:52 +0100951 qall
952 [CODE]
953
Bram Moolenaare1f3ab72022-09-04 21:29:08 +0100954 call writefile(content, 'XblastBall', 'D')
Bram Moolenaar797e63b2021-01-15 16:22:52 +0100955 call system(GetVimCommand() .. ' --clean -S XblastBall')
Bram Moolenaarae04a602022-09-09 15:08:10 +0100956 sleep 100m
Bram Moolenaarbe9fc5b2022-09-09 17:09:35 +0100957 call assert_match('OK', readfile('XerrorsBlast')->join())
Bram Moolenaar797e63b2021-01-15 16:22:52 +0100958
Bram Moolenaarbe9fc5b2022-09-09 17:09:35 +0100959 call delete('XerrorsBlast')
Bram Moolenaar797e63b2021-01-15 16:22:52 +0100960endfunc
961
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100962" SEGV occurs in older versions.
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200963func Test_autocmd_bufwipe_in_SessLoadPost2()
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100964 tabnew
965 set noswapfile
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100966 mksession!
967
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200968 let content =<< trim [CODE]
969 set nocp noswapfile
970 function! DeleteInactiveBufs()
971 tabfirst
972 let tabblist = []
973 for i in range(1, tabpagenr(''$''))
974 call extend(tabblist, tabpagebuflist(i))
975 endfor
976 for b in range(1, bufnr(''$''))
977 if bufexists(b) && buflisted(b) && (index(tabblist, b) == -1 || bufname(b) =~# ''^$'')
978 exec ''bwipeout '' . b
979 endif
980 endfor
981 echomsg "SessionLoadPost DONE"
982 endfunction
983 au SessionLoadPost * call DeleteInactiveBufs()
984
985 func WriteErrors()
Bram Moolenaarbe9fc5b2022-09-09 17:09:35 +0100986 call writefile([execute("messages")], "XerrorsPost")
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200987 endfunc
988 au VimLeave * call WriteErrors()
989 [CODE]
990
Bram Moolenaare1f3ab72022-09-04 21:29:08 +0100991 call writefile(content, 'Xvimrc', 'D')
Bram Moolenaar93344c22019-08-14 21:12:05 +0200992 call system(GetVimCommand('Xvimrc') .. ' --not-a-term --noplugins -S Session.vim -c cq')
Bram Moolenaarae04a602022-09-09 15:08:10 +0100993 sleep 100m
Bram Moolenaarbe9fc5b2022-09-09 17:09:35 +0100994 let errors = join(readfile('XerrorsPost'))
Bram Moolenaare94260f2017-03-21 15:50:12 +0100995 " This probably only ever matches on unix.
996 call assert_notmatch('Caught deadly signal SEGV', errors)
997 call assert_match('SessionLoadPost DONE', errors)
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100998
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100999 set swapfile
Bram Moolenaarbe9fc5b2022-09-09 17:09:35 +01001000 for file in ['Session.vim', 'XerrorsPost']
Bram Moolenaar8c752bd2017-03-19 17:09:56 +01001001 call delete(file)
1002 endfor
1003endfunc
Bram Moolenaarfaf29d72017-07-09 11:07:16 +02001004
1005func Test_empty_doau()
1006 doau \|
1007endfunc
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001008
1009func s:AutoCommandOptionSet(match)
Bram Moolenaard7c96872019-06-15 17:12:48 +02001010 let template = "Option: <%s>, OldVal: <%s>, OldValLocal: <%s>, OldValGlobal: <%s>, NewVal: <%s>, Scope: <%s>, Command: <%s>\n"
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001011 let item = remove(g:options, 0)
Bram Moolenaard7c96872019-06-15 17:12:48 +02001012 let expected = printf(template, item[0], item[1], item[2], item[3], item[4], item[5], item[6])
1013 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 +02001014 let g:opt = [expected, actual]
1015 "call assert_equal(expected, actual)
1016endfunc
1017
1018func Test_OptionSet()
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +02001019 CheckOption autochdir
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001020
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +02001021 badd test_autocmd.vim
1022
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001023 call test_override('starting', 1)
1024 set nocp
1025 au OptionSet * :call s:AutoCommandOptionSet(expand("<amatch>"))
1026
1027 " 1: Setting number option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001028 let g:options = [['number', 0, 0, 0, 1, 'global', 'set']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001029 set nu
1030 call assert_equal([], g:options)
1031 call assert_equal(g:opt[0], g:opt[1])
1032
1033 " 2: Setting local number option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001034 let g:options = [['number', 1, 1, '', 0, 'local', 'setlocal']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001035 setlocal nonu
1036 call assert_equal([], g:options)
1037 call assert_equal(g:opt[0], g:opt[1])
1038
1039 " 3: Setting global number option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001040 let g:options = [['number', 1, '', 1, 0, 'global', 'setglobal']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001041 setglobal nonu
1042 call assert_equal([], g:options)
1043 call assert_equal(g:opt[0], g:opt[1])
1044
1045 " 4: Setting local autoindent option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001046 let g:options = [['autoindent', 0, 0, '', 1, 'local', 'setlocal']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001047 setlocal ai
1048 call assert_equal([], g:options)
1049 call assert_equal(g:opt[0], g:opt[1])
1050
1051 " 5: Setting global autoindent option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001052 let g:options = [['autoindent', 0, '', 0, 1, 'global', 'setglobal']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001053 setglobal ai
1054 call assert_equal([], g:options)
1055 call assert_equal(g:opt[0], g:opt[1])
1056
1057 " 6: Setting global autoindent option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001058 let g:options = [['autoindent', 1, 1, 1, 0, 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001059 set ai!
1060 call assert_equal([], g:options)
1061 call assert_equal(g:opt[0], g:opt[1])
1062
1063 " 6a: Setting global autoindent option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001064 let g:options = [['autoindent', 1, 1, 0, 0, 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001065 noa setlocal ai
1066 noa setglobal noai
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001067 set ai!
1068 call assert_equal([], g:options)
1069 call assert_equal(g:opt[0], g:opt[1])
1070
1071 " Should not print anything, use :noa
1072 " 7: don't trigger OptionSet"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001073 let g:options = [['invalid', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001074 noa set nonu
Bram Moolenaard7c96872019-06-15 17:12:48 +02001075 call assert_equal([['invalid', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid']], g:options)
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001076 call assert_equal(g:opt[0], g:opt[1])
1077
1078 " 8: Setting several global list and number option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001079 let g:options = [['list', 0, 0, 0, 1, 'global', 'set'], ['number', 0, 0, 0, 1, 'global', 'set']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001080 set list nu
1081 call assert_equal([], g:options)
1082 call assert_equal(g:opt[0], g:opt[1])
1083
1084 " 9: don't trigger OptionSet"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001085 let g:options = [['invalid', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid'], ['invalid', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001086 noa set nolist nonu
Bram Moolenaard7c96872019-06-15 17:12:48 +02001087 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 +02001088 call assert_equal(g:opt[0], g:opt[1])
1089
1090 " 10: Setting global acd"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001091 let g:options = [['autochdir', 0, 0, '', 1, 'local', 'setlocal']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001092 setlocal acd
1093 call assert_equal([], g:options)
1094 call assert_equal(g:opt[0], g:opt[1])
1095
1096 " 11: Setting global autoread (also sets local value)"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001097 let g:options = [['autoread', 0, 0, 0, 1, 'global', 'set']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001098 set ar
1099 call assert_equal([], g:options)
1100 call assert_equal(g:opt[0], g:opt[1])
1101
1102 " 12: Setting local autoread"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001103 let g:options = [['autoread', 1, 1, '', 1, 'local', 'setlocal']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001104 setlocal ar
1105 call assert_equal([], g:options)
1106 call assert_equal(g:opt[0], g:opt[1])
1107
1108 " 13: Setting global autoread"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001109 let g:options = [['autoread', 1, '', 1, 0, 'global', 'setglobal']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001110 setglobal invar
1111 call assert_equal([], g:options)
1112 call assert_equal(g:opt[0], g:opt[1])
1113
1114 " 14: Setting option backspace through :let"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001115 let g:options = [['backspace', '', '', '', 'eol,indent,start', 'global', 'set']]
1116 let &bs = "eol,indent,start"
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001117 call assert_equal([], g:options)
1118 call assert_equal(g:opt[0], g:opt[1])
1119
1120 " 15: Setting option backspace through setbufvar()"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001121 let g:options = [['backup', 0, 0, '', 1, 'local', 'setlocal']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001122 " try twice, first time, shouldn't trigger because option name is invalid,
1123 " second time, it should trigger
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +02001124 let bnum = bufnr('%')
Bram Moolenaare2e40752020-09-04 21:18:46 +02001125 call assert_fails("call setbufvar(bnum, '&l:bk', 1)", 'E355:')
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001126 " should trigger, use correct option name
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +02001127 call setbufvar(bnum, '&backup', 1)
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001128 call assert_equal([], g:options)
1129 call assert_equal(g:opt[0], g:opt[1])
1130
1131 " 16: Setting number option using setwinvar"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001132 let g:options = [['number', 0, 0, '', 1, 'local', 'setlocal']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001133 call setwinvar(0, '&number', 1)
1134 call assert_equal([], g:options)
1135 call assert_equal(g:opt[0], g:opt[1])
1136
1137 " 17: Setting key option, shouldn't trigger"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001138 let g:options = [['key', 'invalid', 'invalid1', 'invalid2', 'invalid3', 'invalid4', 'invalid5']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001139 setlocal key=blah
1140 setlocal key=
Bram Moolenaard7c96872019-06-15 17:12:48 +02001141 call assert_equal([['key', 'invalid', 'invalid1', 'invalid2', 'invalid3', 'invalid4', 'invalid5']], g:options)
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001142 call assert_equal(g:opt[0], g:opt[1])
1143
Bram Moolenaard7c96872019-06-15 17:12:48 +02001144
1145 " 18a: Setting string global option"
1146 let oldval = &backupext
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001147 let g:options = [['backupext', oldval, oldval, oldval, 'foo', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001148 set backupext=foo
1149 call assert_equal([], g:options)
1150 call assert_equal(g:opt[0], g:opt[1])
1151
1152 " 18b: Resetting string global option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001153 let g:options = [['backupext', 'foo', 'foo', 'foo', oldval, 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001154 set backupext&
1155 call assert_equal([], g:options)
1156 call assert_equal(g:opt[0], g:opt[1])
1157
1158 " 18c: Setting global string global option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001159 let g:options = [['backupext', oldval, '', oldval, 'bar', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001160 setglobal backupext=bar
1161 call assert_equal([], g:options)
1162 call assert_equal(g:opt[0], g:opt[1])
1163
1164 " 18d: Setting local string global option"
1165 " As this is a global option this sets the global value even though
1166 " :setlocal is used!
1167 noa set backupext& " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001168 let g:options = [['backupext', oldval, oldval, '', 'baz', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001169 setlocal backupext=baz
1170 call assert_equal([], g:options)
1171 call assert_equal(g:opt[0], g:opt[1])
1172
1173 " 18e: Setting again string global option"
1174 noa setglobal backupext=ext_global " Reset global and local value (without triggering autocmd)
1175 noa setlocal backupext=ext_local " Sets the global(!) value!
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001176 let g:options = [['backupext', 'ext_local', 'ext_local', 'ext_local', 'fuu', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001177 set backupext=fuu
1178 call assert_equal([], g:options)
1179 call assert_equal(g:opt[0], g:opt[1])
1180
1181
zeertzjqb811de52021-10-21 10:50:44 +01001182 " 19a: Setting string global-local (to buffer) option"
Bram Moolenaar8efa0262017-08-20 15:47:20 +02001183 let oldval = &tags
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001184 let g:options = [['tags', oldval, oldval, oldval, 'tagpath', 'global', 'set']]
Bram Moolenaar8efa0262017-08-20 15:47:20 +02001185 set tags=tagpath
1186 call assert_equal([], g:options)
1187 call assert_equal(g:opt[0], g:opt[1])
1188
zeertzjqb811de52021-10-21 10:50:44 +01001189 " 19b: Resetting string global-local (to buffer) option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001190 let g:options = [['tags', 'tagpath', 'tagpath', 'tagpath', oldval, 'global', 'set']]
Bram Moolenaar8efa0262017-08-20 15:47:20 +02001191 set tags&
1192 call assert_equal([], g:options)
1193 call assert_equal(g:opt[0], g:opt[1])
1194
zeertzjqb811de52021-10-21 10:50:44 +01001195 " 19c: Setting global string global-local (to buffer) option "
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001196 let g:options = [['tags', oldval, '', oldval, 'tagpath1', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001197 setglobal tags=tagpath1
1198 call assert_equal([], g:options)
1199 call assert_equal(g:opt[0], g:opt[1])
1200
zeertzjqb811de52021-10-21 10:50:44 +01001201 " 19d: Setting local string global-local (to buffer) option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001202 let g:options = [['tags', 'tagpath1', 'tagpath1', '', 'tagpath2', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001203 setlocal tags=tagpath2
1204 call assert_equal([], g:options)
1205 call assert_equal(g:opt[0], g:opt[1])
1206
zeertzjqb811de52021-10-21 10:50:44 +01001207 " 19e: Setting again string global-local (to buffer) option"
1208 " Note: v:option_old is the old global value for global-local string options
Bram Moolenaard7c96872019-06-15 17:12:48 +02001209 " but the old local value for all other kinds of options.
1210 noa setglobal tags=tag_global " Reset global and local value (without triggering autocmd)
1211 noa setlocal tags=tag_local
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001212 let g:options = [['tags', 'tag_global', 'tag_local', 'tag_global', 'tagpath', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001213 set tags=tagpath
1214 call assert_equal([], g:options)
1215 call assert_equal(g:opt[0], g:opt[1])
1216
zeertzjqb811de52021-10-21 10:50:44 +01001217 " 19f: Setting string global-local (to buffer) option to an empty string"
1218 " Note: v:option_old is the old global value for global-local string options
Bram Moolenaard7c96872019-06-15 17:12:48 +02001219 " but the old local value for all other kinds of options.
1220 noa set tags=tag_global " Reset global and local value (without triggering autocmd)
1221 noa setlocal tags= " empty string
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001222 let g:options = [['tags', 'tag_global', '', 'tag_global', 'tagpath', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001223 set tags=tagpath
1224 call assert_equal([], g:options)
1225 call assert_equal(g:opt[0], g:opt[1])
1226
1227
1228 " 20a: Setting string local (to buffer) option"
1229 let oldval = &spelllang
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001230 let g:options = [['spelllang', oldval, oldval, oldval, 'elvish,klingon', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001231 set spelllang=elvish,klingon
1232 call assert_equal([], g:options)
1233 call assert_equal(g:opt[0], g:opt[1])
1234
1235 " 20b: Resetting string local (to buffer) option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001236 let g:options = [['spelllang', 'elvish,klingon', 'elvish,klingon', 'elvish,klingon', oldval, 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001237 set spelllang&
1238 call assert_equal([], g:options)
1239 call assert_equal(g:opt[0], g:opt[1])
1240
1241 " 20c: Setting global string local (to buffer) option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001242 let g:options = [['spelllang', oldval, '', oldval, 'elvish', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001243 setglobal spelllang=elvish
1244 call assert_equal([], g:options)
1245 call assert_equal(g:opt[0], g:opt[1])
1246
1247 " 20d: Setting local string local (to buffer) option"
1248 noa set spelllang& " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001249 let g:options = [['spelllang', oldval, oldval, '', 'klingon', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001250 setlocal spelllang=klingon
1251 call assert_equal([], g:options)
1252 call assert_equal(g:opt[0], g:opt[1])
1253
1254 " 20e: Setting again string local (to buffer) option"
zeertzjqb811de52021-10-21 10:50:44 +01001255 " Note: v:option_old is the old global value for global-local string options
Bram Moolenaard7c96872019-06-15 17:12:48 +02001256 " but the old local value for all other kinds of options.
1257 noa setglobal spelllang=spellglobal " Reset global and local value (without triggering autocmd)
1258 noa setlocal spelllang=spelllocal
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001259 let g:options = [['spelllang', 'spelllocal', 'spelllocal', 'spellglobal', 'foo', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001260 set spelllang=foo
1261 call assert_equal([], g:options)
1262 call assert_equal(g:opt[0], g:opt[1])
1263
1264
zeertzjqb811de52021-10-21 10:50:44 +01001265 " 21a: Setting string global-local (to window) option"
Bram Moolenaard7c96872019-06-15 17:12:48 +02001266 let oldval = &statusline
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001267 let g:options = [['statusline', oldval, oldval, oldval, 'foo', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001268 set statusline=foo
1269 call assert_equal([], g:options)
1270 call assert_equal(g:opt[0], g:opt[1])
1271
zeertzjqb811de52021-10-21 10:50:44 +01001272 " 21b: Resetting string global-local (to window) option"
1273 " Note: v:option_old is the old global value for global-local string options
Bram Moolenaard7c96872019-06-15 17:12:48 +02001274 " but the old local value for all other kinds of options.
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001275 let g:options = [['statusline', 'foo', 'foo', 'foo', oldval, 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001276 set statusline&
1277 call assert_equal([], g:options)
1278 call assert_equal(g:opt[0], g:opt[1])
1279
zeertzjqb811de52021-10-21 10:50:44 +01001280 " 21c: Setting global string global-local (to window) option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001281 let g:options = [['statusline', oldval, '', oldval, 'bar', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001282 setglobal statusline=bar
1283 call assert_equal([], g:options)
1284 call assert_equal(g:opt[0], g:opt[1])
1285
zeertzjqb811de52021-10-21 10:50:44 +01001286 " 21d: Setting local string global-local (to window) option"
Bram Moolenaard7c96872019-06-15 17:12:48 +02001287 noa set statusline& " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001288 let g:options = [['statusline', oldval, oldval, '', 'baz', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001289 setlocal statusline=baz
1290 call assert_equal([], g:options)
1291 call assert_equal(g:opt[0], g:opt[1])
1292
zeertzjqb811de52021-10-21 10:50:44 +01001293 " 21e: Setting again string global-local (to window) option"
1294 " Note: v:option_old is the old global value for global-local string options
Bram Moolenaard7c96872019-06-15 17:12:48 +02001295 " but the old local value for all other kinds of options.
1296 noa setglobal statusline=bar " Reset global and local value (without triggering autocmd)
1297 noa setlocal statusline=baz
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001298 let g:options = [['statusline', 'bar', 'baz', 'bar', 'foo', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001299 set statusline=foo
1300 call assert_equal([], g:options)
1301 call assert_equal(g:opt[0], g:opt[1])
1302
1303
1304 " 22a: Setting string local (to window) option"
1305 let oldval = &foldignore
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001306 let g:options = [['foldignore', oldval, oldval, oldval, 'fo', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001307 set foldignore=fo
1308 call assert_equal([], g:options)
1309 call assert_equal(g:opt[0], g:opt[1])
1310
1311 " 22b: Resetting string local (to window) option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001312 let g:options = [['foldignore', 'fo', 'fo', 'fo', oldval, 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001313 set foldignore&
1314 call assert_equal([], g:options)
1315 call assert_equal(g:opt[0], g:opt[1])
1316
1317 " 22c: Setting global string local (to window) option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001318 let g:options = [['foldignore', oldval, '', oldval, 'bar', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001319 setglobal foldignore=bar
1320 call assert_equal([], g:options)
1321 call assert_equal(g:opt[0], g:opt[1])
1322
1323 " 22d: Setting local string local (to window) option"
1324 noa set foldignore& " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001325 let g:options = [['foldignore', oldval, oldval, '', 'baz', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001326 setlocal foldignore=baz
1327 call assert_equal([], g:options)
1328 call assert_equal(g:opt[0], g:opt[1])
1329
1330 " 22e: Setting again string local (to window) option"
1331 noa setglobal foldignore=glob " Reset global and local value (without triggering autocmd)
1332 noa setlocal foldignore=loc
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001333 let g:options = [['foldignore', 'loc', 'loc', 'glob', 'fo', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001334 set foldignore=fo
1335 call assert_equal([], g:options)
1336 call assert_equal(g:opt[0], g:opt[1])
1337
1338
zeertzjqb811de52021-10-21 10:50:44 +01001339 " 23a: Setting global number global option"
Bram Moolenaard7c96872019-06-15 17:12:48 +02001340 noa setglobal cmdheight=8 " Reset global and local value (without triggering autocmd)
1341 noa setlocal cmdheight=1 " Sets the global(!) value!
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001342 let g:options = [['cmdheight', '1', '', '1', '2', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001343 setglobal cmdheight=2
1344 call assert_equal([], g:options)
1345 call assert_equal(g:opt[0], g:opt[1])
1346
1347 " 23b: Setting local number global option"
1348 noa setglobal cmdheight=8 " Reset global and local value (without triggering autocmd)
1349 noa setlocal cmdheight=1 " Sets the global(!) value!
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001350 let g:options = [['cmdheight', '1', '1', '', '2', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001351 setlocal cmdheight=2
1352 call assert_equal([], g:options)
1353 call assert_equal(g:opt[0], g:opt[1])
1354
1355 " 23c: Setting again number global option"
1356 noa setglobal cmdheight=8 " Reset global and local value (without triggering autocmd)
1357 noa setlocal cmdheight=1 " Sets the global(!) value!
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001358 let g:options = [['cmdheight', '1', '1', '1', '2', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001359 set cmdheight=2
1360 call assert_equal([], g:options)
1361 call assert_equal(g:opt[0], g:opt[1])
1362
1363 " 23d: Setting again number global option"
1364 noa set cmdheight=8 " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001365 let g:options = [['cmdheight', '8', '8', '8', '2', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001366 set cmdheight=2
1367 call assert_equal([], g:options)
1368 call assert_equal(g:opt[0], g:opt[1])
1369
1370
1371 " 24a: Setting global number global-local (to buffer) option"
1372 noa setglobal undolevels=8 " Reset global and local value (without triggering autocmd)
1373 noa setlocal undolevels=1
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001374 let g:options = [['undolevels', '8', '', '8', '2', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001375 setglobal undolevels=2
1376 call assert_equal([], g:options)
1377 call assert_equal(g:opt[0], g:opt[1])
1378
1379 " 24b: Setting local number global-local (to buffer) option"
1380 noa setglobal undolevels=8 " Reset global and local value (without triggering autocmd)
1381 noa setlocal undolevels=1
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001382 let g:options = [['undolevels', '1', '1', '', '2', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001383 setlocal undolevels=2
1384 call assert_equal([], g:options)
1385 call assert_equal(g:opt[0], g:opt[1])
1386
1387 " 24c: Setting again number global-local (to buffer) option"
1388 noa setglobal undolevels=8 " Reset global and local value (without triggering autocmd)
1389 noa setlocal undolevels=1
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001390 let g:options = [['undolevels', '1', '1', '8', '2', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001391 set undolevels=2
1392 call assert_equal([], g:options)
1393 call assert_equal(g:opt[0], g:opt[1])
1394
1395 " 24d: Setting again global number global-local (to buffer) option"
1396 noa set undolevels=8 " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001397 let g:options = [['undolevels', '8', '8', '8', '2', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001398 set undolevels=2
1399 call assert_equal([], g:options)
1400 call assert_equal(g:opt[0], g:opt[1])
1401
1402
1403 " 25a: Setting global number local (to buffer) option"
1404 noa setglobal wrapmargin=8 " Reset global and local value (without triggering autocmd)
1405 noa setlocal wrapmargin=1
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001406 let g:options = [['wrapmargin', '8', '', '8', '2', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001407 setglobal wrapmargin=2
1408 call assert_equal([], g:options)
1409 call assert_equal(g:opt[0], g:opt[1])
1410
1411 " 25b: Setting local number local (to buffer) option"
1412 noa setglobal wrapmargin=8 " Reset global and local value (without triggering autocmd)
1413 noa setlocal wrapmargin=1
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001414 let g:options = [['wrapmargin', '1', '1', '', '2', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001415 setlocal wrapmargin=2
1416 call assert_equal([], g:options)
1417 call assert_equal(g:opt[0], g:opt[1])
1418
1419 " 25c: Setting again number local (to buffer) option"
1420 noa setglobal wrapmargin=8 " Reset global and local value (without triggering autocmd)
1421 noa setlocal wrapmargin=1
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001422 let g:options = [['wrapmargin', '1', '1', '8', '2', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001423 set wrapmargin=2
1424 call assert_equal([], g:options)
1425 call assert_equal(g:opt[0], g:opt[1])
1426
1427 " 25d: Setting again global number local (to buffer) option"
1428 noa set wrapmargin=8 " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001429 let g:options = [['wrapmargin', '8', '8', '8', '2', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001430 set wrapmargin=2
1431 call assert_equal([], g:options)
1432 call assert_equal(g:opt[0], g:opt[1])
1433
1434
1435 " 26: Setting number global-local (to window) option.
1436 " Such option does currently not exist.
1437
1438
1439 " 27a: Setting global number local (to window) option"
1440 noa setglobal foldcolumn=8 " Reset global and local value (without triggering autocmd)
1441 noa setlocal foldcolumn=1
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001442 let g:options = [['foldcolumn', '8', '', '8', '2', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001443 setglobal foldcolumn=2
1444 call assert_equal([], g:options)
1445 call assert_equal(g:opt[0], g:opt[1])
1446
1447 " 27b: Setting local number local (to window) option"
1448 noa setglobal foldcolumn=8 " Reset global and local value (without triggering autocmd)
1449 noa setlocal foldcolumn=1
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001450 let g:options = [['foldcolumn', '1', '1', '', '2', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001451 setlocal foldcolumn=2
1452 call assert_equal([], g:options)
1453 call assert_equal(g:opt[0], g:opt[1])
1454
1455 " 27c: Setting again number local (to window) option"
1456 noa setglobal foldcolumn=8 " Reset global and local value (without triggering autocmd)
1457 noa setlocal foldcolumn=1
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001458 let g:options = [['foldcolumn', '1', '1', '8', '2', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001459 set foldcolumn=2
1460 call assert_equal([], g:options)
1461 call assert_equal(g:opt[0], g:opt[1])
1462
zeertzjqb811de52021-10-21 10:50:44 +01001463 " 27d: Setting again global number local (to window) option"
Bram Moolenaard7c96872019-06-15 17:12:48 +02001464 noa set foldcolumn=8 " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001465 let g:options = [['foldcolumn', '8', '8', '8', '2', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001466 set foldcolumn=2
1467 call assert_equal([], g:options)
1468 call assert_equal(g:opt[0], g:opt[1])
1469
1470
1471 " 28a: Setting global boolean global option"
1472 noa setglobal nowrapscan " Reset global and local value (without triggering autocmd)
1473 noa setlocal wrapscan " Sets the global(!) value!
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001474 let g:options = [['wrapscan', '1', '', '1', '0', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001475 setglobal nowrapscan
1476 call assert_equal([], g:options)
1477 call assert_equal(g:opt[0], g:opt[1])
1478
1479 " 28b: Setting local boolean global option"
1480 noa setglobal nowrapscan " Reset global and local value (without triggering autocmd)
1481 noa setlocal wrapscan " Sets the global(!) value!
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001482 let g:options = [['wrapscan', '1', '1', '', '0', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001483 setlocal nowrapscan
1484 call assert_equal([], g:options)
1485 call assert_equal(g:opt[0], g:opt[1])
1486
1487 " 28c: Setting again boolean global option"
1488 noa setglobal nowrapscan " Reset global and local value (without triggering autocmd)
1489 noa setlocal wrapscan " Sets the global(!) value!
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001490 let g:options = [['wrapscan', '1', '1', '1', '0', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001491 set nowrapscan
1492 call assert_equal([], g:options)
1493 call assert_equal(g:opt[0], g:opt[1])
1494
1495 " 28d: Setting again global boolean global option"
1496 noa set nowrapscan " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001497 let g:options = [['wrapscan', '0', '0', '0', '1', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001498 set wrapscan
1499 call assert_equal([], g:options)
1500 call assert_equal(g:opt[0], g:opt[1])
1501
1502
1503 " 29a: Setting global boolean global-local (to buffer) option"
1504 noa setglobal noautoread " Reset global and local value (without triggering autocmd)
1505 noa setlocal autoread
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001506 let g:options = [['autoread', '0', '', '0', '1', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001507 setglobal autoread
1508 call assert_equal([], g:options)
1509 call assert_equal(g:opt[0], g:opt[1])
1510
1511 " 29b: Setting local boolean global-local (to buffer) option"
1512 noa setglobal noautoread " Reset global and local value (without triggering autocmd)
1513 noa setlocal autoread
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001514 let g:options = [['autoread', '1', '1', '', '0', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001515 setlocal noautoread
1516 call assert_equal([], g:options)
1517 call assert_equal(g:opt[0], g:opt[1])
1518
1519 " 29c: Setting again boolean global-local (to buffer) option"
1520 noa setglobal noautoread " Reset global and local value (without triggering autocmd)
1521 noa setlocal autoread
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001522 let g:options = [['autoread', '1', '1', '0', '1', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001523 set autoread
1524 call assert_equal([], g:options)
1525 call assert_equal(g:opt[0], g:opt[1])
1526
1527 " 29d: Setting again global boolean global-local (to buffer) option"
1528 noa set noautoread " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001529 let g:options = [['autoread', '0', '0', '0', '1', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001530 set autoread
1531 call assert_equal([], g:options)
1532 call assert_equal(g:opt[0], g:opt[1])
1533
1534
1535 " 30a: Setting global boolean local (to buffer) option"
1536 noa setglobal nocindent " Reset global and local value (without triggering autocmd)
1537 noa setlocal cindent
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001538 let g:options = [['cindent', '0', '', '0', '1', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001539 setglobal cindent
1540 call assert_equal([], g:options)
1541 call assert_equal(g:opt[0], g:opt[1])
1542
1543 " 30b: Setting local boolean local (to buffer) option"
1544 noa setglobal nocindent " Reset global and local value (without triggering autocmd)
1545 noa setlocal cindent
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001546 let g:options = [['cindent', '1', '1', '', '0', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001547 setlocal nocindent
1548 call assert_equal([], g:options)
1549 call assert_equal(g:opt[0], g:opt[1])
1550
1551 " 30c: Setting again boolean local (to buffer) option"
1552 noa setglobal nocindent " Reset global and local value (without triggering autocmd)
1553 noa setlocal cindent
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001554 let g:options = [['cindent', '1', '1', '0', '1', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001555 set cindent
1556 call assert_equal([], g:options)
1557 call assert_equal(g:opt[0], g:opt[1])
1558
1559 " 30d: Setting again global boolean local (to buffer) option"
1560 noa set nocindent " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001561 let g:options = [['cindent', '0', '0', '0', '1', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001562 set cindent
1563 call assert_equal([], g:options)
1564 call assert_equal(g:opt[0], g:opt[1])
1565
1566
1567 " 31: Setting boolean global-local (to window) option
1568 " Currently no such option exists.
1569
1570
1571 " 32a: Setting global boolean local (to window) option"
1572 noa setglobal nocursorcolumn " Reset global and local value (without triggering autocmd)
1573 noa setlocal cursorcolumn
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001574 let g:options = [['cursorcolumn', '0', '', '0', '1', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001575 setglobal cursorcolumn
1576 call assert_equal([], g:options)
1577 call assert_equal(g:opt[0], g:opt[1])
1578
1579 " 32b: Setting local boolean local (to window) option"
1580 noa setglobal nocursorcolumn " Reset global and local value (without triggering autocmd)
1581 noa setlocal cursorcolumn
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001582 let g:options = [['cursorcolumn', '1', '1', '', '0', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001583 setlocal nocursorcolumn
1584 call assert_equal([], g:options)
1585 call assert_equal(g:opt[0], g:opt[1])
1586
1587 " 32c: Setting again boolean local (to window) option"
1588 noa setglobal nocursorcolumn " Reset global and local value (without triggering autocmd)
1589 noa setlocal cursorcolumn
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001590 let g:options = [['cursorcolumn', '1', '1', '0', '1', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001591 set cursorcolumn
1592 call assert_equal([], g:options)
1593 call assert_equal(g:opt[0], g:opt[1])
1594
1595 " 32d: Setting again global boolean local (to window) option"
1596 noa set nocursorcolumn " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001597 let g:options = [['cursorcolumn', '0', '0', '0', '1', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001598 set cursorcolumn
1599 call assert_equal([], g:options)
1600 call assert_equal(g:opt[0], g:opt[1])
1601
1602
Bram Moolenaar1bc353b2019-09-01 14:45:28 +02001603 " 33: Test autocommands when an option value is converted internally.
Bram Moolenaard7c96872019-06-15 17:12:48 +02001604 noa set backspace=1 " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001605 let g:options = [['backspace', 'indent,eol', 'indent,eol', 'indent,eol', '2', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001606 set backspace=2
1607 call assert_equal([], g:options)
1608 call assert_equal(g:opt[0], g:opt[1])
1609
1610
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001611 " Cleanup
1612 au! OptionSet
Bram Moolenaar0331faf2019-06-15 18:40:37 +02001613 " set tags&
Bram Moolenaard7c96872019-06-15 17:12:48 +02001614 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 +02001615 exe printf(":set %s&vim", opt)
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001616 endfor
1617 call test_override('starting', 0)
1618 delfunc! AutoCommandOptionSet
1619endfunc
1620
1621func Test_OptionSet_diffmode()
1622 call test_override('starting', 1)
Bram Moolenaar26d98212019-01-27 22:32:55 +01001623 " 18: Changing an option when entering diff mode
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001624 new
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001625 au OptionSet diff :let &l:cul = v:option_new
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001626
1627 call setline(1, ['buffer 1', 'line2', 'line3', 'line4'])
1628 call assert_equal(0, &l:cul)
1629 diffthis
1630 call assert_equal(1, &l:cul)
1631
1632 vnew
1633 call setline(1, ['buffer 2', 'line 2', 'line 3', 'line4'])
1634 call assert_equal(0, &l:cul)
1635 diffthis
1636 call assert_equal(1, &l:cul)
1637
1638 diffoff
1639 call assert_equal(0, &l:cul)
1640 call assert_equal(1, getwinvar(2, '&l:cul'))
1641 bw!
1642
1643 call assert_equal(1, &l:cul)
1644 diffoff!
1645 call assert_equal(0, &l:cul)
1646 call assert_equal(0, getwinvar(1, '&l:cul'))
1647 bw!
1648
1649 " Cleanup
1650 au! OptionSet
1651 call test_override('starting', 0)
1652endfunc
1653
1654func Test_OptionSet_diffmode_close()
1655 call test_override('starting', 1)
1656 " 19: Try to close the current window when entering diff mode
1657 " should not segfault
1658 new
1659 au OptionSet diff close
1660
1661 call setline(1, ['buffer 1', 'line2', 'line3', 'line4'])
Bram Moolenaare2e40752020-09-04 21:18:46 +02001662 call assert_fails(':diffthis', 'E788:')
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001663 call assert_equal(1, &diff)
1664 vnew
1665 call setline(1, ['buffer 2', 'line 2', 'line 3', 'line4'])
Bram Moolenaare2e40752020-09-04 21:18:46 +02001666 call assert_fails(':diffthis', 'E788:')
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001667 call assert_equal(1, &diff)
Bram Moolenaara9aa86f2019-11-10 21:25:45 +01001668 set diffopt-=closeoff
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001669 bw!
Bram Moolenaare2e40752020-09-04 21:18:46 +02001670 call assert_fails(':diffoff!', 'E788:')
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001671 bw!
1672
1673 " Cleanup
1674 au! OptionSet
1675 call test_override('starting', 0)
1676 "delfunc! AutoCommandOptionSet
1677endfunc
Bram Moolenaar4a137b42017-08-04 22:37:11 +02001678
1679" Test for Bufleave autocommand that deletes the buffer we are about to edit.
1680func Test_BufleaveWithDelete()
Bram Moolenaare7cda972022-08-29 11:02:59 +01001681 new | edit XbufLeave1
Bram Moolenaar4a137b42017-08-04 22:37:11 +02001682
1683 augroup test_bufleavewithdelete
1684 autocmd!
Bram Moolenaare7cda972022-08-29 11:02:59 +01001685 autocmd BufLeave XbufLeave1 bwipe XbufLeave2
Bram Moolenaar4a137b42017-08-04 22:37:11 +02001686 augroup END
1687
Bram Moolenaare7cda972022-08-29 11:02:59 +01001688 call assert_fails('edit XbufLeave2', 'E143:')
1689 call assert_equal('XbufLeave1', bufname('%'))
Bram Moolenaar4a137b42017-08-04 22:37:11 +02001690
Bram Moolenaare7cda972022-08-29 11:02:59 +01001691 autocmd! test_bufleavewithdelete BufLeave XbufLeave1
Bram Moolenaar4a137b42017-08-04 22:37:11 +02001692 augroup! test_bufleavewithdelete
1693
1694 new
Bram Moolenaare7cda972022-08-29 11:02:59 +01001695 bwipe! XbufLeave1
Bram Moolenaar4a137b42017-08-04 22:37:11 +02001696endfunc
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +02001697
1698" Test for autocommand that changes the buffer list, when doing ":ball".
1699func Test_Acmd_BufAll()
1700 enew!
1701 %bwipe!
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01001702 call writefile(['Test file Xxx1'], 'Xxx1', 'D')
1703 call writefile(['Test file Xxx2'], 'Xxx2', 'D')
1704 call writefile(['Test file Xxx3'], 'Xxx3', 'D')
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +02001705
1706 " Add three files to the buffer list
1707 split Xxx1
1708 close
1709 split Xxx2
1710 close
1711 split Xxx3
1712 close
1713
1714 " Wipe the buffer when the buffer is opened
1715 au BufReadPost Xxx2 bwipe
1716
1717 call append(0, 'Test file Xxx4')
1718 ball
1719
1720 call assert_equal(2, winnr('$'))
1721 call assert_equal('Xxx1', bufname(winbufnr(winnr('$'))))
1722 wincmd t
1723
1724 au! BufReadPost
1725 %bwipe!
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +02001726 enew! | only
1727endfunc
1728
1729" Test for autocommand that changes current buffer on BufEnter event.
1730" Check if modelines are interpreted for the correct buffer.
1731func Test_Acmd_BufEnter()
1732 %bwipe!
1733 call writefile(['start of test file Xxx1',
1734 \ "\<Tab>this is a test",
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01001735 \ 'end of test file Xxx1'], 'Xxx1', 'D')
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +02001736 call writefile(['start of test file Xxx2',
1737 \ 'vim: set noai :',
1738 \ "\<Tab>this is a test",
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01001739 \ 'end of test file Xxx2'], 'Xxx2', 'D')
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +02001740
1741 au BufEnter Xxx2 brew
1742 set ai modeline modelines=3
1743 edit Xxx1
1744 " edit Xxx2, autocmd will do :brew
1745 edit Xxx2
1746 exe "normal G?this is a\<CR>"
1747 " Append text with autoindent to this file
1748 normal othis should be auto-indented
1749 call assert_equal("\<Tab>this should be auto-indented", getline('.'))
1750 call assert_equal(3, line('.'))
1751 " Remove autocmd and edit Xxx2 again
1752 au! BufEnter Xxx2
1753 buf! Xxx2
1754 exe "normal G?this is a\<CR>"
1755 " append text without autoindent to Xxx
1756 normal othis should be in column 1
1757 call assert_equal("this should be in column 1", getline('.'))
1758 call assert_equal(4, line('.'))
1759
1760 %bwipe!
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +02001761 set ai&vim modeline&vim modelines&vim
1762endfunc
1763
1764" Test for issue #57
1765" do not move cursor on <c-o> when autoindent is set
1766func Test_ai_CTRL_O()
1767 enew!
1768 set ai
1769 let save_fo = &fo
1770 set fo+=r
1771 exe "normal o# abcdef\<Esc>2hi\<CR>\<C-O>d0\<Esc>"
1772 exe "normal o# abcdef\<Esc>2hi\<C-O>d0\<Esc>"
1773 call assert_equal(['# abc', 'def', 'def'], getline(2, 4))
1774
1775 set ai&vim
1776 let &fo = save_fo
1777 enew!
1778endfunc
1779
1780" Test for autocommand that deletes the current buffer on BufLeave event.
1781" Also test deleting the last buffer, should give a new, empty buffer.
1782func Test_BufLeave_Wipe()
1783 %bwipe!
1784 let content = ['start of test file Xxx',
1785 \ 'this is a test',
1786 \ 'end of test file Xxx']
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01001787 call writefile(content, 'Xxx1', 'D')
1788 call writefile(content, 'Xxx2', 'D')
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +02001789
1790 au BufLeave Xxx2 bwipe
1791 edit Xxx1
1792 split Xxx2
1793 " delete buffer Xxx2, we should be back to Xxx1
1794 bwipe
1795 call assert_equal('Xxx1', bufname('%'))
1796 call assert_equal(1, winnr('$'))
1797
1798 " Create an alternate buffer
1799 %write! test.out
1800 call assert_equal('test.out', bufname('#'))
1801 " delete alternate buffer
1802 bwipe test.out
1803 call assert_equal('Xxx1', bufname('%'))
1804 call assert_equal('', bufname('#'))
1805
1806 au BufLeave Xxx1 bwipe
1807 " delete current buffer, get an empty one
1808 bwipe!
1809 call assert_equal(1, line('$'))
1810 call assert_equal('', bufname('%'))
Bram Moolenaarb2c87502017-10-14 21:15:58 +02001811 let g:bufinfo = getbufinfo()
1812 call assert_equal(1, len(g:bufinfo))
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +02001813
Bram Moolenaar53f0c962017-10-22 14:23:59 +02001814 call delete('test.out')
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +02001815 %bwipe
1816 au! BufLeave
Bram Moolenaarb2c87502017-10-14 21:15:58 +02001817
1818 " check that bufinfo doesn't contain a pointer to freed memory
1819 call test_garbagecollect_now()
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +02001820endfunc
Bram Moolenaar87ffb5c2017-10-19 12:37:42 +02001821
1822func Test_QuitPre()
1823 edit Xfoo
1824 let winid = win_getid(winnr())
1825 split Xbar
1826 au! QuitPre * let g:afile = expand('<afile>')
1827 " Close the other window, <afile> should be correct.
1828 exe win_id2win(winid) . 'q'
1829 call assert_equal('Xfoo', g:afile)
LemonBoy66e13ae2022-04-21 22:52:11 +01001830
Bram Moolenaar87ffb5c2017-10-19 12:37:42 +02001831 unlet g:afile
1832 bwipe Xfoo
1833 bwipe Xbar
1834endfunc
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02001835
1836func Test_Cmdline()
Bram Moolenaar153b7042018-01-31 15:48:32 +01001837 au! CmdlineChanged : let g:text = getcmdline()
1838 let g:text = 0
1839 call feedkeys(":echom 'hello'\<CR>", 'xt')
1840 call assert_equal("echom 'hello'", g:text)
1841 au! CmdlineChanged
1842
1843 au! CmdlineChanged : let g:entered = expand('<afile>')
1844 let g:entered = 0
1845 call feedkeys(":echom 'hello'\<CR>", 'xt')
1846 call assert_equal(':', g:entered)
1847 au! CmdlineChanged
1848
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02001849 au! CmdlineEnter : let g:entered = expand('<afile>')
1850 au! CmdlineLeave : let g:left = expand('<afile>')
1851 let g:entered = 0
1852 let g:left = 0
1853 call feedkeys(":echo 'hello'\<CR>", 'xt')
1854 call assert_equal(':', g:entered)
1855 call assert_equal(':', g:left)
1856 au! CmdlineEnter
1857 au! CmdlineLeave
1858
Bram Moolenaara4baf5b2018-04-22 13:27:44 +02001859 let save_shellslash = &shellslash
1860 set noshellslash
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02001861 au! CmdlineEnter / let g:entered = expand('<afile>')
1862 au! CmdlineLeave / let g:left = expand('<afile>')
1863 let g:entered = 0
1864 let g:left = 0
Bram Moolenaar53f0c962017-10-22 14:23:59 +02001865 new
1866 call setline(1, 'hello')
1867 call feedkeys("/hello\<CR>", 'xt')
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02001868 call assert_equal('/', g:entered)
1869 call assert_equal('/', g:left)
Bram Moolenaar53f0c962017-10-22 14:23:59 +02001870 bwipe!
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02001871 au! CmdlineEnter
1872 au! CmdlineLeave
Bram Moolenaara4baf5b2018-04-22 13:27:44 +02001873 let &shellslash = save_shellslash
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02001874endfunc
Bram Moolenaar53f0c962017-10-22 14:23:59 +02001875
1876" Test for BufWritePre autocommand that deletes or unloads the buffer.
1877func Test_BufWritePre()
1878 %bwipe
1879 au BufWritePre Xxx1 bunload
1880 au BufWritePre Xxx2 bwipe
1881
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01001882 call writefile(['start of Xxx1', 'test', 'end of Xxx1'], 'Xxx1', 'D')
1883 call writefile(['start of Xxx2', 'test', 'end of Xxx2'], 'Xxx2', 'D')
Bram Moolenaar53f0c962017-10-22 14:23:59 +02001884
1885 edit Xtest
1886 e! Xxx2
1887 bdel Xtest
1888 e Xxx1
1889 " write it, will unload it and give an error msg
Bram Moolenaare2e40752020-09-04 21:18:46 +02001890 call assert_fails('w', 'E203:')
Bram Moolenaar53f0c962017-10-22 14:23:59 +02001891 call assert_equal('Xxx2', bufname('%'))
1892 edit Xtest
1893 e! Xxx2
1894 bwipe Xtest
1895 " write it, will delete the buffer and give an error msg
Bram Moolenaare2e40752020-09-04 21:18:46 +02001896 call assert_fails('w', 'E203:')
Bram Moolenaar53f0c962017-10-22 14:23:59 +02001897 call assert_equal('Xxx1', bufname('%'))
1898 au! BufWritePre
Bram Moolenaar53f0c962017-10-22 14:23:59 +02001899endfunc
1900
1901" Test for BufUnload autocommand that unloads all the other buffers
1902func Test_bufunload_all()
Bram Moolenaarf08b0eb2021-10-16 13:00:14 +01001903 let g:test_is_flaky = 1
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01001904 call writefile(['Test file Xxx1'], 'Xxx1', 'D')"
1905 call writefile(['Test file Xxx2'], 'Xxx2', 'D')"
Bram Moolenaar53f0c962017-10-22 14:23:59 +02001906
Bram Moolenaarc79745a2019-05-20 22:12:34 +02001907 let content =<< trim [CODE]
1908 func UnloadAllBufs()
1909 let i = 1
1910 while i <= bufnr('$')
1911 if i != bufnr('%') && bufloaded(i)
1912 exe i . 'bunload'
1913 endif
1914 let i += 1
1915 endwhile
1916 endfunc
1917 au BufUnload * call UnloadAllBufs()
1918 au VimLeave * call writefile(['Test Finished'], 'Xout')
1919 edit Xxx1
1920 split Xxx2
1921 q
1922 [CODE]
1923
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01001924 call writefile(content, 'Xbunloadtest', 'D')
Bram Moolenaar53f0c962017-10-22 14:23:59 +02001925
1926 call delete('Xout')
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01001927 call system(GetVimCommandClean() .. ' -N --not-a-term -S Xbunloadtest')
Bram Moolenaar53f0c962017-10-22 14:23:59 +02001928 call assert_true(filereadable('Xout'))
1929
Bram Moolenaar53f0c962017-10-22 14:23:59 +02001930 call delete('Xout')
1931endfunc
1932
1933" Some tests for buffer-local autocommands
1934func Test_buflocal_autocmd()
1935 let g:bname = ''
1936 edit xx
1937 au BufLeave <buffer> let g:bname = expand("%")
1938 " here, autocommand for xx should trigger.
1939 " but autocommand shall not apply to buffer named <buffer>.
1940 edit somefile
1941 call assert_equal('xx', g:bname)
1942 let g:bname = ''
1943 " here, autocommand shall be auto-deleted
1944 bwipe xx
1945 " autocmd should not trigger
1946 edit xx
1947 call assert_equal('', g:bname)
1948 " autocmd should not trigger
1949 edit somefile
1950 call assert_equal('', g:bname)
1951 enew
1952 unlet g:bname
1953endfunc
Bram Moolenaar430dc5d2017-11-02 21:04:47 +01001954
1955" Test for "*Cmd" autocommands
1956func Test_Cmd_Autocmds()
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01001957 call writefile(['start of Xxx', "\tabc2", 'end of Xxx'], 'Xxx', 'D')
Bram Moolenaar430dc5d2017-11-02 21:04:47 +01001958
1959 enew!
1960 au BufReadCmd XtestA 0r Xxx|$del
1961 edit XtestA " will read text of Xxd instead
1962 call assert_equal('start of Xxx', getline(1))
1963
1964 au BufWriteCmd XtestA call append(line("$"), "write")
1965 write " will append a line to the file
1966 call assert_equal('write', getline('$'))
Bram Moolenaare2e40752020-09-04 21:18:46 +02001967 call assert_fails('read XtestA', 'E484:') " should not read anything
Bram Moolenaar430dc5d2017-11-02 21:04:47 +01001968 call assert_equal('write', getline(4))
1969
1970 " now we have:
1971 " 1 start of Xxx
1972 " 2 abc2
1973 " 3 end of Xxx
1974 " 4 write
1975
1976 au FileReadCmd XtestB '[r Xxx
1977 2r XtestB " will read Xxx below line 2 instead
1978 call assert_equal('start of Xxx', getline(3))
1979
1980 " now we have:
1981 " 1 start of Xxx
1982 " 2 abc2
1983 " 3 start of Xxx
1984 " 4 abc2
1985 " 5 end of Xxx
1986 " 6 end of Xxx
1987 " 7 write
1988
1989 au FileWriteCmd XtestC '[,']copy $
1990 normal 4GA1
1991 4,5w XtestC " will copy lines 4 and 5 to the end
1992 call assert_equal("\tabc21", getline(8))
Bram Moolenaare2e40752020-09-04 21:18:46 +02001993 call assert_fails('r XtestC', 'E484:') " should not read anything
Bram Moolenaar430dc5d2017-11-02 21:04:47 +01001994 call assert_equal("end of Xxx", getline(9))
1995
1996 " now we have:
1997 " 1 start of Xxx
1998 " 2 abc2
1999 " 3 start of Xxx
2000 " 4 abc21
2001 " 5 end of Xxx
2002 " 6 end of Xxx
2003 " 7 write
2004 " 8 abc21
2005 " 9 end of Xxx
2006
2007 let g:lines = []
2008 au FileAppendCmd XtestD call extend(g:lines, getline(line("'["), line("']")))
2009 w >>XtestD " will add lines to 'lines'
2010 call assert_equal(9, len(g:lines))
Bram Moolenaare2e40752020-09-04 21:18:46 +02002011 call assert_fails('$r XtestD', 'E484:') " should not read anything
Bram Moolenaar430dc5d2017-11-02 21:04:47 +01002012 call assert_equal(9, line('$'))
2013 call assert_equal('end of Xxx', getline('$'))
2014
2015 au BufReadCmd XtestE 0r Xxx|$del
2016 sp XtestE " split window with test.out
2017 call assert_equal('end of Xxx', getline(3))
2018
2019 let g:lines = []
2020 exe "normal 2Goasdf\<Esc>\<C-W>\<C-W>"
2021 au BufWriteCmd XtestE call extend(g:lines, getline(0, '$'))
2022 wall " will write other window to 'lines'
2023 call assert_equal(4, len(g:lines), g:lines)
2024 call assert_equal('asdf', g:lines[2])
2025
2026 au! BufReadCmd
2027 au! BufWriteCmd
2028 au! FileReadCmd
2029 au! FileWriteCmd
2030 au! FileAppendCmd
2031 %bwipe!
Bram Moolenaar430dc5d2017-11-02 21:04:47 +01002032 enew!
2033endfunc
Bram Moolenaaraace2152017-11-05 16:23:10 +01002034
Bram Moolenaar0fff4412020-03-29 16:06:29 +02002035func s:ReadFile()
2036 setl noswapfile nomodified
2037 let filename = resolve(expand("<afile>:p"))
2038 execute 'read' fnameescape(filename)
2039 1d_
2040 exe 'file' fnameescape(filename)
2041 setl buftype=acwrite
2042endfunc
2043
2044func s:WriteFile()
2045 let filename = resolve(expand("<afile>:p"))
2046 setl buftype=
2047 noautocmd execute 'write' fnameescape(filename)
2048 setl buftype=acwrite
2049 setl nomodified
2050endfunc
2051
2052func Test_BufReadCmd()
2053 autocmd BufReadCmd *.test call s:ReadFile()
2054 autocmd BufWriteCmd *.test call s:WriteFile()
2055
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01002056 call writefile(['one', 'two', 'three'], 'Xcmd.test', 'D')
Bram Moolenaar0fff4412020-03-29 16:06:29 +02002057 edit Xcmd.test
2058 call assert_match('Xcmd.test" line 1 of 3', execute('file'))
2059 normal! Gofour
2060 write
2061 call assert_equal(['one', 'two', 'three', 'four'], readfile('Xcmd.test'))
2062
2063 bwipe!
Bram Moolenaar0fff4412020-03-29 16:06:29 +02002064 au! BufReadCmd
2065 au! BufWriteCmd
2066endfunc
2067
zeertzjq9c8f9462022-08-30 18:17:15 +01002068func Test_BufWriteCmd()
2069 autocmd BufWriteCmd Xbufwritecmd let g:written = 1
2070 new
2071 file Xbufwritecmd
2072 set buftype=acwrite
Bram Moolenaar6f14da12022-09-07 21:30:44 +01002073 call mkdir('Xbufwritecmd', 'D')
zeertzjq9c8f9462022-08-30 18:17:15 +01002074 write
2075 " BufWriteCmd should be triggered even if a directory has the same name
2076 call assert_equal(1, g:written)
zeertzjq9c8f9462022-08-30 18:17:15 +01002077 unlet g:written
2078 au! BufWriteCmd
2079 bwipe!
2080endfunc
2081
Bram Moolenaaraace2152017-11-05 16:23:10 +01002082func SetChangeMarks(start, end)
Bram Moolenaar97c69432021-01-15 16:45:21 +01002083 exe a:start .. 'mark ['
2084 exe a:end .. 'mark ]'
Bram Moolenaaraace2152017-11-05 16:23:10 +01002085endfunc
2086
2087" Verify the effects of autocmds on '[ and ']
2088func Test_change_mark_in_autocmds()
2089 edit! Xtest
Bram Moolenaar97c69432021-01-15 16:45:21 +01002090 call feedkeys("ia\<CR>b\<CR>c\<CR>d\<C-g>u\<Esc>", 'xtn')
Bram Moolenaaraace2152017-11-05 16:23:10 +01002091
2092 call SetChangeMarks(2, 3)
2093 write
2094 call assert_equal([1, 4], [line("'["), line("']")])
2095
2096 call SetChangeMarks(2, 3)
2097 au BufWritePre * call assert_equal([1, 4], [line("'["), line("']")])
2098 write
2099 au! BufWritePre
2100
Bram Moolenaar14ddd222020-08-05 12:02:40 +02002101 if has('unix')
Bram Moolenaaraace2152017-11-05 16:23:10 +01002102 write XtestFilter
2103 write >> XtestFilter
2104
2105 call SetChangeMarks(2, 3)
2106 " Marks are set to the entire range of the write
2107 au FilterWritePre * call assert_equal([1, 4], [line("'["), line("']")])
2108 " '[ is adjusted to just before the line that will receive the filtered
2109 " data
2110 au FilterReadPre * call assert_equal([4, 4], [line("'["), line("']")])
2111 " The filtered data is read into the buffer, and the source lines are
2112 " still present, so the range is after the source lines
2113 au FilterReadPost * call assert_equal([5, 12], [line("'["), line("']")])
2114 %!cat XtestFilter
2115 " After the filtered data is read, the original lines are deleted
2116 call assert_equal([1, 8], [line("'["), line("']")])
2117 au! FilterWritePre,FilterReadPre,FilterReadPost
2118 undo
2119
2120 call SetChangeMarks(1, 4)
2121 au FilterWritePre * call assert_equal([2, 3], [line("'["), line("']")])
2122 au FilterReadPre * call assert_equal([3, 3], [line("'["), line("']")])
2123 au FilterReadPost * call assert_equal([4, 11], [line("'["), line("']")])
2124 2,3!cat XtestFilter
2125 call assert_equal([2, 9], [line("'["), line("']")])
2126 au! FilterWritePre,FilterReadPre,FilterReadPost
2127 undo
2128
2129 call delete('XtestFilter')
2130 endif
2131
2132 call SetChangeMarks(1, 4)
2133 au FileWritePre * call assert_equal([2, 3], [line("'["), line("']")])
2134 2,3write Xtest2
2135 au! FileWritePre
2136
2137 call SetChangeMarks(2, 3)
2138 au FileAppendPre * call assert_equal([1, 4], [line("'["), line("']")])
2139 write >> Xtest2
2140 au! FileAppendPre
2141
2142 call SetChangeMarks(1, 4)
2143 au FileAppendPre * call assert_equal([2, 3], [line("'["), line("']")])
2144 2,3write >> Xtest2
2145 au! FileAppendPre
2146
2147 call SetChangeMarks(1, 1)
2148 au FileReadPre * call assert_equal([3, 1], [line("'["), line("']")])
2149 au FileReadPost * call assert_equal([4, 11], [line("'["), line("']")])
2150 3read Xtest2
2151 au! FileReadPre,FileReadPost
2152 undo
2153
2154 call SetChangeMarks(4, 4)
2155 " When the line is 0, it's adjusted to 1
2156 au FileReadPre * call assert_equal([1, 4], [line("'["), line("']")])
2157 au FileReadPost * call assert_equal([1, 8], [line("'["), line("']")])
2158 0read Xtest2
2159 au! FileReadPre,FileReadPost
2160 undo
2161
2162 call SetChangeMarks(4, 4)
2163 " When the line is 0, it's adjusted to 1
2164 au FileReadPre * call assert_equal([1, 4], [line("'["), line("']")])
2165 au FileReadPost * call assert_equal([2, 9], [line("'["), line("']")])
2166 1read Xtest2
2167 au! FileReadPre,FileReadPost
2168 undo
2169
2170 bwipe!
2171 call delete('Xtest')
2172 call delete('Xtest2')
2173endfunc
2174
2175func Test_Filter_noshelltemp()
Bram Moolenaaraeb313f2020-11-27 19:13:28 +01002176 CheckExecutable cat
Bram Moolenaaraace2152017-11-05 16:23:10 +01002177
2178 enew!
2179 call setline(1, ['a', 'b', 'c', 'd'])
2180
2181 let shelltemp = &shelltemp
2182 set shelltemp
2183
2184 let g:filter_au = 0
2185 au FilterWritePre * let g:filter_au += 1
2186 au FilterReadPre * let g:filter_au += 1
2187 au FilterReadPost * let g:filter_au += 1
2188 %!cat
2189 call assert_equal(3, g:filter_au)
2190
2191 if has('filterpipe')
2192 set noshelltemp
2193
2194 let g:filter_au = 0
2195 au FilterWritePre * let g:filter_au += 1
2196 au FilterReadPre * let g:filter_au += 1
2197 au FilterReadPost * let g:filter_au += 1
2198 %!cat
2199 call assert_equal(0, g:filter_au)
2200 endif
2201
2202 au! FilterWritePre,FilterReadPre,FilterReadPost
2203 let &shelltemp = shelltemp
2204 bwipe!
2205endfunc
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01002206
2207func Test_TextYankPost()
2208 enew!
2209 call setline(1, ['foo'])
2210
2211 let g:event = []
2212 au TextYankPost * let g:event = copy(v:event)
2213
2214 call assert_equal({}, v:event)
2215 call assert_fails('let v:event = {}', 'E46:')
2216 call assert_fails('let v:event.mykey = 0', 'E742:')
2217
2218 norm "ayiw
2219 call assert_equal(
Bram Moolenaara016eeb2022-04-09 11:37:38 +01002220 \ #{regcontents: ['foo'], regname: 'a', operator: 'y',
2221 \ regtype: 'v', visual: v:false, inclusive: v:true},
2222 \ g:event)
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01002223 norm y_
2224 call assert_equal(
Bram Moolenaara016eeb2022-04-09 11:37:38 +01002225 \ #{regcontents: ['foo'], regname: '', operator: 'y', regtype: 'V',
2226 \ visual: v:false, inclusive: v:false},
2227 \ g:event)
Bram Moolenaar37d16732020-06-12 22:09:01 +02002228 norm Vy
2229 call assert_equal(
Bram Moolenaara016eeb2022-04-09 11:37:38 +01002230 \ #{regcontents: ['foo'], regname: '', operator: 'y', regtype: 'V',
2231 \ visual: v:true, inclusive: v:true},
2232 \ g:event)
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01002233 call feedkeys("\<C-V>y", 'x')
2234 call assert_equal(
Bram Moolenaara016eeb2022-04-09 11:37:38 +01002235 \ #{regcontents: ['f'], regname: '', operator: 'y', regtype: "\x161",
2236 \ visual: v:true, inclusive: v:true},
2237 \ g:event)
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01002238 norm "xciwbar
2239 call assert_equal(
Bram Moolenaara016eeb2022-04-09 11:37:38 +01002240 \ #{regcontents: ['foo'], regname: 'x', operator: 'c', regtype: 'v',
2241 \ visual: v:false, inclusive: v:true},
2242 \ g:event)
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01002243 norm "bdiw
2244 call assert_equal(
Bram Moolenaara016eeb2022-04-09 11:37:38 +01002245 \ #{regcontents: ['bar'], regname: 'b', operator: 'd', regtype: 'v',
2246 \ visual: v:false, inclusive: v:true},
2247 \ g:event)
2248
2249 call setline(1, 'foobar')
2250 " exclusive motion
2251 norm $"ay0
2252 call assert_equal(
2253 \ #{regcontents: ['fooba'], regname: 'a', operator: 'y', regtype: 'v',
2254 \ visual: v:false, inclusive: v:false},
2255 \ g:event)
2256 " inclusive motion
2257 norm 0"ay$
2258 call assert_equal(
2259 \ #{regcontents: ['foobar'], regname: 'a', operator: 'y', regtype: 'v',
2260 \ visual: v:false, inclusive: v:true},
2261 \ g:event)
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01002262
2263 call assert_equal({}, v:event)
2264
Bram Moolenaarfccbf062020-11-26 20:34:00 +01002265 if has('clipboard_working') && !has('gui_running')
2266 " Test that when the visual selection is automatically copied to clipboard
2267 " register a TextYankPost is emitted
2268 call setline(1, ['foobar'])
2269
2270 let @* = ''
2271 set clipboard=autoselect
2272 exe "norm! ggviw\<Esc>"
2273 call assert_equal(
Bram Moolenaara016eeb2022-04-09 11:37:38 +01002274 \ #{regcontents: ['foobar'], regname: '*', operator: 'y',
2275 \ regtype: 'v', visual: v:true, inclusive: v:false},
2276 \ g:event)
Bram Moolenaarfccbf062020-11-26 20:34:00 +01002277
2278 let @+ = ''
2279 set clipboard=autoselectplus
2280 exe "norm! ggviw\<Esc>"
2281 call assert_equal(
Bram Moolenaara016eeb2022-04-09 11:37:38 +01002282 \ #{regcontents: ['foobar'], regname: '+', operator: 'y',
2283 \ regtype: 'v', visual: v:true, inclusive: v:false},
2284 \ g:event)
Bram Moolenaarfccbf062020-11-26 20:34:00 +01002285
2286 set clipboard&vim
2287 endif
2288
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01002289 au! TextYankPost
2290 unlet g:event
2291 bwipe!
2292endfunc
Bram Moolenaar9bca8052017-12-18 12:37:55 +01002293
Bram Moolenaar9a046fd2021-01-28 13:47:59 +01002294func Test_autocommand_all_events()
2295 call assert_fails('au * * bwipe', 'E1155:')
2296 call assert_fails('au * x bwipe', 'E1155:')
Bram Moolenaarb6db1462021-12-24 19:24:47 +00002297 call assert_fails('au! * x bwipe', 'E1155:')
Bram Moolenaar4fb921e2017-12-18 15:33:00 +01002298endfunc
Bram Moolenaarb7407d32018-02-03 17:36:27 +01002299
Bram Moolenaarf6246f52022-02-11 16:30:12 +00002300func Test_autocmd_user()
2301 au User MyEvent let s:res = [expand("<afile>"), expand("<amatch>")]
2302 doautocmd User MyEvent
2303 call assert_equal(['MyEvent', 'MyEvent'], s:res)
2304 au! User
2305 unlet s:res
2306endfunc
2307
Bram Moolenaar3b014be2022-11-13 17:53:46 +00002308func Test_autocmd_user_clear_group()
2309 CheckRunVimInTerminal
2310
2311 let lines =<< trim END
2312 autocmd! User
2313 for i in range(1, 999)
2314 exe 'autocmd User ' .. 'Foo' .. i .. ' bar'
2315 endfor
2316 au CmdlineLeave : call timer_start(0, {-> execute('autocmd! User')})
2317 END
2318 call writefile(lines, 'XautoUser', 'D')
2319 let buf = RunVimInTerminal('-S XautoUser', {'rows': 10})
2320
2321 " this was using freed memory
2322 call term_sendkeys(buf, ":autocmd User\<CR>")
2323 call TermWait(buf, 50)
2324 call term_sendkeys(buf, "G")
2325
2326 call StopVimInTerminal(buf)
2327endfunc
2328
Bram Moolenaarb7407d32018-02-03 17:36:27 +01002329function s:Before_test_dirchanged()
2330 augroup test_dirchanged
2331 autocmd!
2332 augroup END
2333 let s:li = []
2334 let s:dir_this = getcwd()
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02002335 let s:dir_foo = s:dir_this . '/Xfoo'
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01002336 call mkdir(s:dir_foo)
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02002337 let s:dir_bar = s:dir_this . '/Xbar'
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01002338 call mkdir(s:dir_bar)
Bram Moolenaarb7407d32018-02-03 17:36:27 +01002339endfunc
2340
2341function s:After_test_dirchanged()
Bram Moolenaar3503d7c2019-11-09 20:10:17 +01002342 call chdir(s:dir_this)
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01002343 call delete(s:dir_foo, 'd')
2344 call delete(s:dir_bar, 'd')
Bram Moolenaarb7407d32018-02-03 17:36:27 +01002345 augroup test_dirchanged
2346 autocmd!
2347 augroup END
2348endfunc
2349
2350function Test_dirchanged_global()
2351 call s:Before_test_dirchanged()
Bram Moolenaarf6246f52022-02-11 16:30:12 +00002352 autocmd test_dirchanged DirChangedPre global call add(s:li, expand("<amatch>") .. " pre cd " .. v:event.directory)
Bram Moolenaarb7407d32018-02-03 17:36:27 +01002353 autocmd test_dirchanged DirChanged global call add(s:li, "cd:")
2354 autocmd test_dirchanged DirChanged global call add(s:li, expand("<afile>"))
Bram Moolenaar3503d7c2019-11-09 20:10:17 +01002355 call chdir(s:dir_foo)
Bram Moolenaarf6246f52022-02-11 16:30:12 +00002356 let expected = ["global pre cd " .. s:dir_foo, "cd:", s:dir_foo]
Bram Moolenaar28e8f732022-02-09 12:58:20 +00002357 call assert_equal(expected, s:li)
Bram Moolenaar3503d7c2019-11-09 20:10:17 +01002358 call chdir(s:dir_foo)
Bram Moolenaar28e8f732022-02-09 12:58:20 +00002359 call assert_equal(expected, s:li)
Bram Moolenaar3503d7c2019-11-09 20:10:17 +01002360 exe 'lcd ' .. fnameescape(s:dir_bar)
Bram Moolenaar28e8f732022-02-09 12:58:20 +00002361 call assert_equal(expected, s:li)
Bram Moolenaard8c9d322022-06-12 11:49:16 +01002362
2363 exe 'cd ' .. s:dir_foo
2364 exe 'cd ' .. s:dir_bar
2365 autocmd! test_dirchanged DirChanged global let g:result = expand("<afile>")
2366 cd -
Bram Moolenaardb77c492022-06-12 23:26:50 +01002367 call assert_equal(s:dir_foo, substitute(g:result, '\\', '/', 'g'))
Bram Moolenaard8c9d322022-06-12 11:49:16 +01002368
Bram Moolenaarb7407d32018-02-03 17:36:27 +01002369 call s:After_test_dirchanged()
2370endfunc
2371
2372function Test_dirchanged_local()
2373 call s:Before_test_dirchanged()
2374 autocmd test_dirchanged DirChanged window call add(s:li, "lcd:")
2375 autocmd test_dirchanged DirChanged window call add(s:li, expand("<afile>"))
Bram Moolenaar3503d7c2019-11-09 20:10:17 +01002376 call chdir(s:dir_foo)
Bram Moolenaarb7407d32018-02-03 17:36:27 +01002377 call assert_equal([], s:li)
Bram Moolenaar3503d7c2019-11-09 20:10:17 +01002378 exe 'lcd ' .. fnameescape(s:dir_bar)
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01002379 call assert_equal(["lcd:", s:dir_bar], s:li)
Bram Moolenaar3503d7c2019-11-09 20:10:17 +01002380 exe 'lcd ' .. fnameescape(s:dir_bar)
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01002381 call assert_equal(["lcd:", s:dir_bar], s:li)
Bram Moolenaarb7407d32018-02-03 17:36:27 +01002382 call s:After_test_dirchanged()
2383endfunc
2384
2385function Test_dirchanged_auto()
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +02002386 CheckOption autochdir
Bram Moolenaarb7407d32018-02-03 17:36:27 +01002387 call s:Before_test_dirchanged()
2388 call test_autochdir()
Bram Moolenaar28e8f732022-02-09 12:58:20 +00002389 autocmd test_dirchanged DirChangedPre auto call add(s:li, "pre cd " .. v:event.directory)
Bram Moolenaarb7407d32018-02-03 17:36:27 +01002390 autocmd test_dirchanged DirChanged auto call add(s:li, "auto:")
2391 autocmd test_dirchanged DirChanged auto call add(s:li, expand("<afile>"))
2392 set acd
Bram Moolenaar3503d7c2019-11-09 20:10:17 +01002393 cd ..
Bram Moolenaarb7407d32018-02-03 17:36:27 +01002394 call assert_equal([], s:li)
Bram Moolenaar61abe7d2022-08-30 21:46:08 +01002395 exe 'edit ' . s:dir_foo . '/Xautofile'
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01002396 call assert_equal(s:dir_foo, getcwd())
Bram Moolenaar28e8f732022-02-09 12:58:20 +00002397 let expected = ["pre cd " .. s:dir_foo, "auto:", s:dir_foo]
2398 call assert_equal(expected, s:li)
Bram Moolenaarb7407d32018-02-03 17:36:27 +01002399 set noacd
2400 bwipe!
2401 call s:After_test_dirchanged()
2402endfunc
Bram Moolenaar5a093432018-02-10 18:15:19 +01002403
2404" Test TextChangedI and TextChangedP
2405func Test_ChangedP()
2406 new
2407 call setline(1, ['foo', 'bar', 'foobar'])
2408 call test_override("char_avail", 1)
2409 set complete=. completeopt=menuone
2410
2411 func! TextChangedAutocmd(char)
2412 let g:autocmd .= a:char
2413 endfunc
2414
Christian Brabandtdb3b4462021-10-16 11:58:55 +01002415 " TextChanged will not be triggered, only check that it isn't.
Bram Moolenaar5a093432018-02-10 18:15:19 +01002416 au! TextChanged <buffer> :call TextChangedAutocmd('N')
2417 au! TextChangedI <buffer> :call TextChangedAutocmd('I')
2418 au! TextChangedP <buffer> :call TextChangedAutocmd('P')
2419
2420 call cursor(3, 1)
2421 let g:autocmd = ''
2422 call feedkeys("o\<esc>", 'tnix')
2423 call assert_equal('I', g:autocmd)
2424
2425 let g:autocmd = ''
2426 call feedkeys("Sf", 'tnix')
2427 call assert_equal('II', g:autocmd)
2428
2429 let g:autocmd = ''
2430 call feedkeys("Sf\<C-N>", 'tnix')
2431 call assert_equal('IIP', g:autocmd)
2432
2433 let g:autocmd = ''
2434 call feedkeys("Sf\<C-N>\<C-N>", 'tnix')
2435 call assert_equal('IIPP', g:autocmd)
2436
2437 let g:autocmd = ''
2438 call feedkeys("Sf\<C-N>\<C-N>\<C-N>", 'tnix')
2439 call assert_equal('IIPPP', g:autocmd)
2440
2441 let g:autocmd = ''
2442 call feedkeys("Sf\<C-N>\<C-N>\<C-N>\<C-N>", 'tnix')
2443 call assert_equal('IIPPPP', g:autocmd)
2444
2445 call assert_equal(['foo', 'bar', 'foobar', 'foo'], getline(1, '$'))
2446 " TODO: how should it handle completeopt=noinsert,noselect?
2447
2448 " CleanUp
2449 call test_override("char_avail", 0)
2450 au! TextChanged
2451 au! TextChangedI
2452 au! TextChangedP
2453 delfu TextChangedAutocmd
2454 unlet! g:autocmd
2455 set complete&vim completeopt&vim
2456
2457 bw!
2458endfunc
Bram Moolenaar8c64a362018-03-23 22:39:31 +01002459
Bram Moolenaar91d2e782018-08-07 19:05:01 +02002460let g:setline_handled = v:false
Bram Moolenaar1e115362019-01-09 23:01:02 +01002461func SetLineOne()
Bram Moolenaar91d2e782018-08-07 19:05:01 +02002462 if !g:setline_handled
2463 call setline(1, "(x)")
2464 let g:setline_handled = v:true
2465 endif
2466endfunc
2467
2468func Test_TextChangedI_with_setline()
2469 new
2470 call test_override('char_avail', 1)
2471 autocmd TextChangedI <buffer> call SetLineOne()
2472 call feedkeys("i(\<CR>\<Esc>", 'tx')
2473 call assert_equal('(', getline(1))
2474 call assert_equal('x)', getline(2))
2475 undo
Bram Moolenaar91d2e782018-08-07 19:05:01 +02002476 call assert_equal('', getline(1))
Bram Moolenaar9fa95062018-08-08 22:08:32 +02002477 call assert_equal('', getline(2))
Bram Moolenaar91d2e782018-08-07 19:05:01 +02002478
Bram Moolenaarca34db32022-01-20 11:17:18 +00002479 call test_override('char_avail', 0)
Bram Moolenaar91d2e782018-08-07 19:05:01 +02002480 bwipe!
2481endfunc
2482
Bram Moolenaar8c64a362018-03-23 22:39:31 +01002483func Test_Changed_FirstTime()
Bram Moolenaar8c5a2782019-08-07 23:07:07 +02002484 CheckFeature terminal
2485 CheckNotGui
Bram Moolenaar3cdcb092020-03-18 19:18:10 +01002486 " Starting a terminal to run Vim is always considered flaky.
Bram Moolenaar30d53e22020-03-18 21:10:44 +01002487 let g:test_is_flaky = 1
Bram Moolenaar8c5a2782019-08-07 23:07:07 +02002488
Bram Moolenaar8c64a362018-03-23 22:39:31 +01002489 " Prepare file for TextChanged event.
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01002490 call writefile([''], 'Xchanged.txt', 'D')
Bram Moolenaar8c64a362018-03-23 22:39:31 +01002491 let buf = term_start([GetVimProg(), '--clean', '-c', 'set noswapfile'], {'term_rows': 3})
2492 call assert_equal('running', term_getstatus(buf))
Bram Moolenaar1834d372018-03-29 17:40:46 +02002493 " Wait for the ruler (in the status line) to be shown.
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01002494 " In ConPTY, there is additional character which is drawn up to the width of
2495 " the screen.
2496 if has('conpty')
2497 call WaitForAssert({-> assert_match('\<All.*$', term_getline(buf, 3))})
2498 else
2499 call WaitForAssert({-> assert_match('\<All$', term_getline(buf, 3))})
2500 endif
Bram Moolenaar8c64a362018-03-23 22:39:31 +01002501 " It's only adding autocmd, so that no event occurs.
2502 call term_sendkeys(buf, ":au! TextChanged <buffer> call writefile(['No'], 'Xchanged.txt')\<cr>")
2503 call term_sendkeys(buf, "\<C-\\>\<C-N>:qa!\<cr>")
Bram Moolenaar50182fa2018-04-28 21:34:40 +02002504 call WaitForAssert({-> assert_equal('finished', term_getstatus(buf))})
Bram Moolenaar8c64a362018-03-23 22:39:31 +01002505 call assert_equal([''], readfile('Xchanged.txt'))
2506
2507 " clean up
Bram Moolenaar8c64a362018-03-23 22:39:31 +01002508 bwipe!
2509endfunc
Bram Moolenaar0566e892019-01-24 19:37:40 +01002510
Bram Moolenaareb93f3f2019-04-04 15:04:56 +02002511func Test_autocmd_nested()
2512 let g:did_nested = 0
2513 augroup Testing
2514 au WinNew * edit somefile
2515 au BufNew * let g:did_nested = 1
2516 augroup END
2517 split
2518 call assert_equal(0, g:did_nested)
2519 close
2520 bwipe! somefile
2521
2522 " old nested argument still works
2523 augroup Testing
2524 au!
2525 au WinNew * nested edit somefile
2526 au BufNew * let g:did_nested = 1
2527 augroup END
2528 split
2529 call assert_equal(1, g:did_nested)
2530 close
2531 bwipe! somefile
2532
2533 " New ++nested argument works
2534 augroup Testing
2535 au!
2536 au WinNew * ++nested edit somefile
2537 au BufNew * let g:did_nested = 1
2538 augroup END
2539 split
2540 call assert_equal(1, g:did_nested)
2541 close
2542 bwipe! somefile
2543
Bram Moolenaarf0775142022-03-04 20:10:38 +00002544 " nested without ++ does not work in Vim9 script
2545 call assert_fails('vim9cmd au WinNew * nested echo fails', 'E1078:')
2546
Bram Moolenaareb93f3f2019-04-04 15:04:56 +02002547 augroup Testing
2548 au!
2549 augroup END
2550
2551 call assert_fails('au WinNew * ++nested ++nested echo bad', 'E983:')
2552 call assert_fails('au WinNew * nested nested echo bad', 'E983:')
2553endfunc
2554
Bram Moolenaar5fa9f232022-07-23 09:06:48 +01002555func Test_autocmd_nested_cursor_invalid()
2556 set laststatus=0
2557 copen
2558 cclose
2559 call setline(1, ['foo', 'bar', 'baz'])
2560 3
2561 augroup nested_inv
2562 autocmd User foo ++nested copen
2563 autocmd BufAdd * let &laststatus = 2 - &laststatus
2564 augroup END
2565 doautocmd User foo
2566
2567 augroup nested_inv
2568 au!
2569 augroup END
2570 set laststatus&
Bram Moolenaarb03950f2022-07-26 13:47:13 +01002571 cclose
Bram Moolenaar5fa9f232022-07-23 09:06:48 +01002572 bwipe!
2573endfunc
2574
Bram Moolenaar3d6ee8b2022-07-27 15:23:35 +01002575func Test_autocmd_nested_keeps_cursor_pos()
2576 enew
2577 call setline(1, 'foo')
2578 autocmd User foo ++nested normal! $a
2579 autocmd InsertLeave * :
2580 doautocmd User foo
2581 call assert_equal([0, 1, 3, 0], getpos('.'))
2582
2583 bwipe!
2584endfunc
2585
Bram Moolenaarb03950f2022-07-26 13:47:13 +01002586func Test_autocmd_nested_switch_window()
2587 " run this in a separate Vim so that SafeState works
2588 CheckRunVimInTerminal
2589
2590 let lines =<< trim END
2591 vim9script
2592 ['()']->writefile('Xautofile')
2593 autocmd VimEnter * ++nested edit Xautofile | split
2594 autocmd BufReadPost * autocmd SafeState * ++once foldclosed('.')
2595 autocmd WinEnter * matchadd('ErrorMsg', 'pat')
2596 END
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01002597 call writefile(lines, 'Xautoscript', 'D')
Bram Moolenaarb03950f2022-07-26 13:47:13 +01002598 let buf = RunVimInTerminal('-S Xautoscript', {'rows': 10})
2599 call VerifyScreenDump(buf, 'Test_autocmd_nested_switch', {})
2600
2601 call StopVimInTerminal(buf)
2602 call delete('Xautofile')
Bram Moolenaarb03950f2022-07-26 13:47:13 +01002603endfunc
2604
Bram Moolenaareb93f3f2019-04-04 15:04:56 +02002605func Test_autocmd_once()
2606 " Without ++once WinNew triggers twice
2607 let g:did_split = 0
2608 augroup Testing
2609 au WinNew * let g:did_split += 1
2610 augroup END
2611 split
2612 split
2613 call assert_equal(2, g:did_split)
2614 call assert_true(exists('#WinNew'))
2615 close
2616 close
2617
2618 " With ++once WinNew triggers once
2619 let g:did_split = 0
2620 augroup Testing
2621 au!
2622 au WinNew * ++once let g:did_split += 1
2623 augroup END
2624 split
2625 split
2626 call assert_equal(1, g:did_split)
2627 call assert_false(exists('#WinNew'))
2628 close
2629 close
2630
2631 call assert_fails('au WinNew * ++once ++once echo bad', 'E983:')
2632endfunc
2633
Bram Moolenaara68e5952019-04-25 22:22:01 +02002634func Test_autocmd_bufreadpre()
2635 new
2636 let b:bufreadpre = 1
Bram Moolenaarab505b12020-03-23 19:28:44 +01002637 call append(0, range(1000))
Bram Moolenaara68e5952019-04-25 22:22:01 +02002638 w! XAutocmdBufReadPre.txt
2639 autocmd BufReadPre <buffer> :let b:bufreadpre += 1
Bram Moolenaarab505b12020-03-23 19:28:44 +01002640 norm! 500gg
Bram Moolenaara68e5952019-04-25 22:22:01 +02002641 sp
Bram Moolenaarab505b12020-03-23 19:28:44 +01002642 norm! 1000gg
Bram Moolenaara68e5952019-04-25 22:22:01 +02002643 wincmd p
2644 let g:wsv1 = winsaveview()
2645 wincmd p
2646 let g:wsv2 = winsaveview()
2647 " triggers BufReadPre, should not move the cursor in either window
2648 " The topline may change one line in a large window.
2649 edit
2650 call assert_inrange(g:wsv2.topline - 1, g:wsv2.topline + 1, winsaveview().topline)
2651 call assert_equal(g:wsv2.lnum, winsaveview().lnum)
2652 call assert_equal(2, b:bufreadpre)
2653 wincmd p
2654 call assert_equal(g:wsv1.topline, winsaveview().topline)
2655 call assert_equal(g:wsv1.lnum, winsaveview().lnum)
2656 call assert_equal(2, b:bufreadpre)
2657 " Now set the cursor position in an BufReadPre autocommand
2658 " (even though the position will be invalid, this should make Vim reset the
2659 " cursor position in the other window.
2660 wincmd p
2661 set cpo+=g
2662 " won't do anything, but try to set the cursor on an invalid lnum
2663 autocmd BufReadPre <buffer> :norm! 70gg
2664 " triggers BufReadPre, should not move the cursor in either window
2665 e
2666 call assert_equal(1, winsaveview().topline)
2667 call assert_equal(1, winsaveview().lnum)
2668 call assert_equal(3, b:bufreadpre)
2669 wincmd p
2670 call assert_equal(g:wsv1.topline, winsaveview().topline)
2671 call assert_equal(g:wsv1.lnum, winsaveview().lnum)
2672 call assert_equal(3, b:bufreadpre)
2673 close
2674 close
2675 call delete('XAutocmdBufReadPre.txt')
2676 set cpo-=g
2677endfunc
2678
Bram Moolenaar5e66b422019-01-24 21:58:10 +01002679" FileChangedShell tested in test_filechanged.vim
Bram Moolenaar69ea5872019-04-25 20:29:00 +02002680
2681" Tests for the following autocommands:
2682" - FileWritePre writing a compressed file
2683" - FileReadPost reading a compressed file
2684" - BufNewFile reading a file template
2685" - BufReadPre decompressing the file to be read
2686" - FilterReadPre substituting characters in the temp file
2687" - FilterReadPost substituting characters after filtering
2688" - FileReadPre set options for decompression
2689" - FileReadPost decompress the file
2690func Test_ReadWrite_Autocmds()
2691 " Run this test only on Unix-like systems and if gzip is available
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +02002692 CheckUnix
2693 CheckExecutable gzip
Bram Moolenaar69ea5872019-04-25 20:29:00 +02002694
2695 " Make $GZIP empty, "-v" would cause trouble.
2696 let $GZIP = ""
2697
2698 " Use a FileChangedShell autocommand to avoid a prompt for 'Xtestfile.gz'
2699 " being modified outside of Vim (noticed on Solaris).
2700 au FileChangedShell * echo 'caught FileChangedShell'
2701
2702 " Test for the FileReadPost, FileWritePre and FileWritePost autocmds
2703 augroup Test1
2704 au!
2705 au FileWritePre *.gz '[,']!gzip
2706 au FileWritePost *.gz undo
2707 au FileReadPost *.gz '[,']!gzip -d
2708 augroup END
2709
2710 new
2711 set bin
2712 call append(0, [
2713 \ 'line 2 Abcdefghijklmnopqrstuvwxyz',
2714 \ 'line 3 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2715 \ 'line 4 Abcdefghijklmnopqrstuvwxyz',
2716 \ 'line 5 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2717 \ 'line 6 Abcdefghijklmnopqrstuvwxyz',
2718 \ 'line 7 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2719 \ 'line 8 Abcdefghijklmnopqrstuvwxyz',
2720 \ 'line 9 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2721 \ 'line 10 Abcdefghijklmnopqrstuvwxyz'
2722 \ ])
2723 1,9write! Xtestfile.gz
2724 enew! | close
2725
2726 new
2727 " Read and decompress the testfile
2728 0read Xtestfile.gz
2729 call assert_equal([
2730 \ 'line 2 Abcdefghijklmnopqrstuvwxyz',
2731 \ 'line 3 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2732 \ 'line 4 Abcdefghijklmnopqrstuvwxyz',
2733 \ 'line 5 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2734 \ 'line 6 Abcdefghijklmnopqrstuvwxyz',
2735 \ 'line 7 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2736 \ 'line 8 Abcdefghijklmnopqrstuvwxyz',
2737 \ 'line 9 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2738 \ 'line 10 Abcdefghijklmnopqrstuvwxyz'
2739 \ ], getline(1, 9))
2740 enew! | close
2741
2742 augroup Test1
2743 au!
2744 augroup END
2745
2746 " Test for the FileAppendPre and FileAppendPost autocmds
2747 augroup Test2
2748 au!
2749 au BufNewFile *.c read Xtest.c
2750 au FileAppendPre *.out '[,']s/new/NEW/
2751 au FileAppendPost *.out !cat Xtest.c >> test.out
2752 augroup END
2753
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01002754 call writefile(['/*', ' * Here is a new .c file', ' */'], 'Xtest.c', 'D')
Bram Moolenaar69ea5872019-04-25 20:29:00 +02002755 new foo.c " should load Xtest.c
2756 call assert_equal(['/*', ' * Here is a new .c file', ' */'], getline(2, 4))
2757 w! >> test.out " append it to the output file
2758
2759 let contents = readfile('test.out')
2760 call assert_equal(' * Here is a NEW .c file', contents[2])
2761 call assert_equal(' * Here is a new .c file', contents[5])
2762
2763 call delete('test.out')
2764 enew! | close
2765 augroup Test2
2766 au!
2767 augroup END
2768
2769 " Test for the BufReadPre and BufReadPost autocmds
2770 augroup Test3
2771 au!
2772 " setup autocommands to decompress before reading and re-compress
2773 " afterwards
2774 au BufReadPre *.gz exe '!gzip -d ' . shellescape(expand("<afile>"))
2775 au BufReadPre *.gz call rename(expand("<afile>:r"), expand("<afile>"))
2776 au BufReadPost *.gz call rename(expand("<afile>"), expand("<afile>:r"))
2777 au BufReadPost *.gz exe '!gzip ' . shellescape(expand("<afile>:r"))
2778 augroup END
2779
2780 e! Xtestfile.gz " Edit compressed file
2781 call assert_equal([
2782 \ 'line 2 Abcdefghijklmnopqrstuvwxyz',
2783 \ 'line 3 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2784 \ 'line 4 Abcdefghijklmnopqrstuvwxyz',
2785 \ 'line 5 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2786 \ 'line 6 Abcdefghijklmnopqrstuvwxyz',
2787 \ 'line 7 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2788 \ 'line 8 Abcdefghijklmnopqrstuvwxyz',
2789 \ 'line 9 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2790 \ 'line 10 Abcdefghijklmnopqrstuvwxyz'
2791 \ ], getline(1, 9))
2792
2793 w! >> test.out " Append it to the output file
2794
2795 augroup Test3
2796 au!
2797 augroup END
2798
2799 " Test for the FilterReadPre and FilterReadPost autocmds.
2800 set shelltemp " need temp files here
2801 augroup Test4
2802 au!
2803 au FilterReadPre *.out call rename(expand("<afile>"), expand("<afile>") . ".t")
2804 au FilterReadPre *.out exe 'silent !sed s/e/E/ ' . shellescape(expand("<afile>")) . ".t >" . shellescape(expand("<afile>"))
2805 au FilterReadPre *.out exe 'silent !rm ' . shellescape(expand("<afile>")) . '.t'
2806 au FilterReadPost *.out '[,']s/x/X/g
2807 augroup END
2808
2809 e! test.out " Edit the output file
2810 1,$!cat
2811 call assert_equal([
2812 \ 'linE 2 AbcdefghijklmnopqrstuvwXyz',
2813 \ 'linE 3 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
2814 \ 'linE 4 AbcdefghijklmnopqrstuvwXyz',
2815 \ 'linE 5 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
2816 \ 'linE 6 AbcdefghijklmnopqrstuvwXyz',
2817 \ 'linE 7 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
2818 \ 'linE 8 AbcdefghijklmnopqrstuvwXyz',
2819 \ 'linE 9 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
2820 \ 'linE 10 AbcdefghijklmnopqrstuvwXyz'
2821 \ ], getline(1, 9))
2822 call assert_equal([
2823 \ 'line 2 Abcdefghijklmnopqrstuvwxyz',
2824 \ 'line 3 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2825 \ 'line 4 Abcdefghijklmnopqrstuvwxyz',
2826 \ 'line 5 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2827 \ 'line 6 Abcdefghijklmnopqrstuvwxyz',
2828 \ 'line 7 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2829 \ 'line 8 Abcdefghijklmnopqrstuvwxyz',
2830 \ 'line 9 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2831 \ 'line 10 Abcdefghijklmnopqrstuvwxyz'
2832 \ ], readfile('test.out'))
2833
2834 augroup Test4
2835 au!
2836 augroup END
2837 set shelltemp&vim
2838
2839 " Test for the FileReadPre and FileReadPost autocmds.
2840 augroup Test5
2841 au!
2842 au FileReadPre *.gz exe 'silent !gzip -d ' . shellescape(expand("<afile>"))
2843 au FileReadPre *.gz call rename(expand("<afile>:r"), expand("<afile>"))
2844 au FileReadPost *.gz '[,']s/l/L/
2845 augroup END
2846
2847 new
2848 0r Xtestfile.gz " Read compressed file
2849 call assert_equal([
2850 \ 'Line 2 Abcdefghijklmnopqrstuvwxyz',
2851 \ 'Line 3 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2852 \ 'Line 4 Abcdefghijklmnopqrstuvwxyz',
2853 \ 'Line 5 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2854 \ 'Line 6 Abcdefghijklmnopqrstuvwxyz',
2855 \ 'Line 7 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2856 \ 'Line 8 Abcdefghijklmnopqrstuvwxyz',
2857 \ 'Line 9 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2858 \ 'Line 10 Abcdefghijklmnopqrstuvwxyz'
2859 \ ], getline(1, 9))
2860 call assert_equal([
2861 \ 'line 2 Abcdefghijklmnopqrstuvwxyz',
2862 \ 'line 3 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2863 \ 'line 4 Abcdefghijklmnopqrstuvwxyz',
2864 \ 'line 5 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2865 \ 'line 6 Abcdefghijklmnopqrstuvwxyz',
2866 \ 'line 7 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2867 \ 'line 8 Abcdefghijklmnopqrstuvwxyz',
2868 \ 'line 9 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2869 \ 'line 10 Abcdefghijklmnopqrstuvwxyz'
2870 \ ], readfile('Xtestfile.gz'))
2871
2872 augroup Test5
2873 au!
2874 augroup END
2875
2876 au! FileChangedShell
2877 call delete('Xtestfile.gz')
Bram Moolenaar69ea5872019-04-25 20:29:00 +02002878 call delete('test.out')
2879endfunc
Bram Moolenaar23b51392019-05-09 21:38:43 +02002880
2881func Test_throw_in_BufWritePre()
2882 new
2883 call setline(1, ['one', 'two', 'three'])
2884 call assert_false(filereadable('Xthefile'))
2885 augroup throwing
2886 au BufWritePre X* throw 'do not write'
2887 augroup END
2888 try
2889 w Xthefile
2890 catch
2891 let caught = 1
2892 endtry
2893 call assert_equal(1, caught)
2894 call assert_false(filereadable('Xthefile'))
2895
2896 bwipe!
2897 au! throwing
2898endfunc
Bram Moolenaarcadbe1b2019-09-22 21:50:09 +02002899
Bram Moolenaar40fa12a2021-09-22 14:18:13 +02002900func Test_autocmd_in_try_block()
Bram Moolenaar6f14da12022-09-07 21:30:44 +01002901 call mkdir('Xintrydir', 'R')
Bram Moolenaar40fa12a2021-09-22 14:18:13 +02002902 au BufEnter * let g:fname = expand('%')
2903 try
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +01002904 edit Xintrydir/
Bram Moolenaar40fa12a2021-09-22 14:18:13 +02002905 endtry
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +01002906 call assert_match('Xintrydir', g:fname)
Bram Moolenaar40fa12a2021-09-22 14:18:13 +02002907
2908 unlet g:fname
2909 au! BufEnter
Bram Moolenaar40fa12a2021-09-22 14:18:13 +02002910endfunc
2911
Bram Moolenaarcadbe1b2019-09-22 21:50:09 +02002912func Test_autocmd_SafeState()
2913 CheckRunVimInTerminal
2914
2915 let lines =<< trim END
2916 let g:safe = 0
2917 let g:again = ''
2918 au SafeState * let g:safe += 1
2919 au SafeStateAgain * let g:again ..= 'x'
2920 func CallTimer()
2921 call timer_start(10, {id -> execute('let g:again ..= "t"')})
2922 endfunc
2923 END
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01002924 call writefile(lines, 'XSafeState', 'D')
Bram Moolenaarcadbe1b2019-09-22 21:50:09 +02002925 let buf = RunVimInTerminal('-S XSafeState', #{rows: 6})
2926
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01002927 " Sometimes we loop to handle a K_IGNORE, SafeState may be triggered once or
Bram Moolenaar8fb1b472020-02-23 16:16:26 +01002928 " more often.
Bram Moolenaarcadbe1b2019-09-22 21:50:09 +02002929 call term_sendkeys(buf, ":echo g:safe\<CR>")
Bram Moolenaar8fb1b472020-02-23 16:16:26 +01002930 call WaitForAssert({-> assert_match('^\d ', term_getline(buf, 6))}, 1000)
Bram Moolenaarcadbe1b2019-09-22 21:50:09 +02002931
Bram Moolenaar8fb1b472020-02-23 16:16:26 +01002932 " SafeStateAgain should be invoked at least three times
Bram Moolenaarcadbe1b2019-09-22 21:50:09 +02002933 call term_sendkeys(buf, ":echo g:again\<CR>")
Bram Moolenaar8fb1b472020-02-23 16:16:26 +01002934 call WaitForAssert({-> assert_match('^xxx', term_getline(buf, 6))}, 1000)
Bram Moolenaarcadbe1b2019-09-22 21:50:09 +02002935
2936 call term_sendkeys(buf, ":let g:again = ''\<CR>:call CallTimer()\<CR>")
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02002937 call TermWait(buf, 50)
Bram Moolenaar0f6629a2019-09-22 23:24:13 +02002938 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02002939 call TermWait(buf, 50)
Bram Moolenaarcadbe1b2019-09-22 21:50:09 +02002940 call term_sendkeys(buf, ":echo g:again\<CR>")
2941 call WaitForAssert({-> assert_match('xtx', term_getline(buf, 6))}, 1000)
2942
2943 call StopVimInTerminal(buf)
Bram Moolenaarcadbe1b2019-09-22 21:50:09 +02002944endfunc
Bram Moolenaar23324a02019-10-01 17:39:04 +02002945
2946func Test_autocmd_CmdWinEnter()
2947 CheckRunVimInTerminal
Bram Moolenaar21829c52021-01-26 22:42:21 +01002948
Bram Moolenaar23324a02019-10-01 17:39:04 +02002949 let lines =<< trim END
Egor Zvorykin125ffd22021-11-17 14:01:14 +00002950 augroup vimHints | au! | augroup END
Bram Moolenaar23324a02019-10-01 17:39:04 +02002951 let b:dummy_var = 'This is a dummy'
2952 autocmd CmdWinEnter * quit
2953 let winnr = winnr('$')
2954 END
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01002955 let filename = 'XCmdWinEnter'
Bram Moolenaar23324a02019-10-01 17:39:04 +02002956 call writefile(lines, filename)
2957 let buf = RunVimInTerminal('-S '.filename, #{rows: 6})
2958
2959 call term_sendkeys(buf, "q:")
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02002960 call TermWait(buf)
Bram Moolenaar23324a02019-10-01 17:39:04 +02002961 call term_sendkeys(buf, ":echo b:dummy_var\<cr>")
Bram Moolenaar353c3512020-03-15 14:19:26 +01002962 call WaitForAssert({-> assert_match('^This is a dummy', term_getline(buf, 6))}, 2000)
Bram Moolenaar23324a02019-10-01 17:39:04 +02002963 call term_sendkeys(buf, ":echo &buftype\<cr>")
2964 call WaitForAssert({-> assert_notmatch('^nofile', term_getline(buf, 6))}, 1000)
2965 call term_sendkeys(buf, ":echo winnr\<cr>")
2966 call WaitForAssert({-> assert_match('^1', term_getline(buf, 6))}, 1000)
2967
2968 " clean up
2969 call StopVimInTerminal(buf)
2970 call delete(filename)
2971endfunc
Bram Moolenaarec66c412019-10-11 21:19:13 +02002972
2973func Test_autocmd_was_using_freed_memory()
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01002974 CheckFeature quickfix
2975
Bram Moolenaarec66c412019-10-11 21:19:13 +02002976 pedit xx
2977 n x
Bram Moolenaar92bb83e2021-02-03 23:04:46 +01002978 augroup winenter
2979 au WinEnter * if winnr('$') > 2 | quit | endif
2980 augroup END
Bram Moolenaarec66c412019-10-11 21:19:13 +02002981 split
Bram Moolenaar92bb83e2021-02-03 23:04:46 +01002982
2983 augroup winenter
2984 au! WinEnter
2985 augroup END
2986
2987 bwipe xx
2988 bwipe x
2989 pclose
Bram Moolenaarec66c412019-10-11 21:19:13 +02002990endfunc
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +01002991
2992func Test_BufWrite_lockmarks()
Bram Moolenaarf08b0eb2021-10-16 13:00:14 +01002993 let g:test_is_flaky = 1
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +01002994 edit! Xtest
2995 call setline(1, ['a', 'b', 'c', 'd'])
2996
2997 " :lockmarks preserves the marks
2998 call SetChangeMarks(2, 3)
2999 lockmarks write
3000 call assert_equal([2, 3], [line("'["), line("']")])
3001
3002 " *WritePre autocmds get the correct line range, but lockmarks preserves the
3003 " original values for the user
3004 augroup lockmarks
3005 au!
3006 au BufWritePre,FilterWritePre * call assert_equal([1, 4], [line("'["), line("']")])
3007 au FileWritePre * call assert_equal([3, 4], [line("'["), line("']")])
3008 augroup END
3009
3010 lockmarks write
3011 call assert_equal([2, 3], [line("'["), line("']")])
3012
3013 if executable('cat')
3014 lockmarks %!cat
3015 call assert_equal([2, 3], [line("'["), line("']")])
3016 endif
3017
3018 lockmarks 3,4write Xtest2
3019 call assert_equal([2, 3], [line("'["), line("']")])
3020
3021 au! lockmarks
3022 augroup! lockmarks
3023 call delete('Xtest')
3024 call delete('Xtest2')
3025endfunc
Bram Moolenaarce6db022020-01-07 20:11:42 +01003026
3027func Test_FileType_spell()
3028 if !isdirectory('/tmp')
3029 throw "Skipped: requires /tmp directory"
3030 endif
3031
3032 " this was crashing with an invalid free()
3033 setglobal spellfile=/tmp/en.utf-8.add
3034 augroup crash
3035 autocmd!
3036 autocmd BufNewFile,BufReadPost crashfile setf somefiletype
3037 autocmd BufNewFile,BufReadPost crashfile set ft=anotherfiletype
3038 autocmd FileType anotherfiletype setlocal spell
3039 augroup END
3040 func! NoCrash() abort
3041 edit /tmp/crashfile
3042 endfunc
3043 call NoCrash()
3044
3045 au! crash
3046 setglobal spellfile=
3047endfunc
Bram Moolenaarbc2b71d2020-02-17 21:33:30 +01003048
Bram Moolenaaref976322022-09-28 11:48:30 +01003049" this was wiping out the current buffer and using freed memory
3050func Test_SpellFileMissing_bwipe()
3051 next 0
3052 au SpellFileMissing 0 bwipe
3053 call assert_fails('set spell spelllang=0', 'E937:')
3054
3055 au! SpellFileMissing
Bram Moolenaar0a60f792022-11-19 21:18:11 +00003056 set nospell spelllang=en
Bram Moolenaaref976322022-09-28 11:48:30 +01003057 bwipe
3058endfunc
3059
Bram Moolenaar406cd902020-02-18 21:54:41 +01003060" Test closing a window or editing another buffer from a FileChangedRO handler
3061" in a readonly buffer
3062func Test_FileChangedRO_winclose()
Bram Moolenaar62cd26a2020-10-11 20:08:44 +02003063 call test_override('ui_delay', 10)
3064
Bram Moolenaar406cd902020-02-18 21:54:41 +01003065 augroup FileChangedROTest
3066 au!
3067 autocmd FileChangedRO * quit
3068 augroup END
3069 new
3070 set readonly
3071 call assert_fails('normal i', 'E788:')
3072 close
3073 augroup! FileChangedROTest
3074
3075 augroup FileChangedROTest
3076 au!
Bram Moolenaar61abe7d2022-08-30 21:46:08 +01003077 autocmd FileChangedRO * edit Xrofile
Bram Moolenaar406cd902020-02-18 21:54:41 +01003078 augroup END
3079 new
3080 set readonly
3081 call assert_fails('normal i', 'E788:')
3082 close
3083 augroup! FileChangedROTest
Bram Moolenaar62cd26a2020-10-11 20:08:44 +02003084 call test_override('ALL', 0)
Bram Moolenaar406cd902020-02-18 21:54:41 +01003085endfunc
3086
Bram Moolenaar0c81d1b2020-02-22 22:45:55 +01003087func LogACmd()
3088 call add(g:logged, line('$'))
3089endfunc
3090
3091func Test_TermChanged()
Bram Moolenaard28e0b32020-02-22 23:08:52 +01003092 CheckNotGui
3093
Bram Moolenaar0c81d1b2020-02-22 22:45:55 +01003094 enew!
3095 tabnew
3096 call setline(1, ['a', 'b', 'c', 'd'])
3097 $
3098 au TermChanged * call LogACmd()
3099 let g:logged = []
3100 let term_save = &term
3101 set term=xterm
3102 call assert_equal([1, 4], g:logged)
3103
3104 au! TermChanged
3105 let &term = term_save
3106 bwipe!
3107endfunc
3108
Bram Moolenaare3284872020-03-19 13:55:03 +01003109" Test for FileReadCmd autocmd
3110func Test_autocmd_FileReadCmd()
3111 func ReadFileCmd()
3112 call append(line('$'), "v:cmdarg = " .. v:cmdarg)
3113 endfunc
3114 augroup FileReadCmdTest
3115 au!
3116 au FileReadCmd Xtest call ReadFileCmd()
3117 augroup END
3118
3119 new
3120 read ++bin Xtest
3121 read ++nobin Xtest
3122 read ++edit Xtest
3123 read ++bad=keep Xtest
3124 read ++bad=drop Xtest
3125 read ++bad=- Xtest
3126 read ++ff=unix Xtest
3127 read ++ff=dos Xtest
3128 read ++ff=mac Xtest
3129 read ++enc=utf-8 Xtest
3130
3131 call assert_equal(['',
3132 \ 'v:cmdarg = ++bin',
3133 \ 'v:cmdarg = ++nobin',
3134 \ 'v:cmdarg = ++edit',
3135 \ 'v:cmdarg = ++bad=keep',
3136 \ 'v:cmdarg = ++bad=drop',
3137 \ 'v:cmdarg = ++bad=-',
3138 \ 'v:cmdarg = ++ff=unix',
3139 \ 'v:cmdarg = ++ff=dos',
3140 \ 'v:cmdarg = ++ff=mac',
3141 \ 'v:cmdarg = ++enc=utf-8'], getline(1, '$'))
3142
3143 close!
3144 augroup FileReadCmdTest
3145 au!
3146 augroup END
3147 delfunc ReadFileCmd
3148endfunc
3149
Bram Moolenaaree4e0c12020-04-06 21:35:05 +02003150" Test for passing invalid arguments to autocmd
3151func Test_autocmd_invalid_args()
3152 " Additional character after * for event
Bram Moolenaar61abe7d2022-08-30 21:46:08 +01003153 call assert_fails('autocmd *a Xinvfile set ff=unix', 'E215:')
Bram Moolenaaree4e0c12020-04-06 21:35:05 +02003154 augroup Test
3155 augroup END
3156 " Invalid autocmd event
Bram Moolenaar61abe7d2022-08-30 21:46:08 +01003157 call assert_fails('autocmd Bufabc Xinvfile set ft=vim', 'E216:')
Bram Moolenaaree4e0c12020-04-06 21:35:05 +02003158 " Invalid autocmd event in a autocmd group
Bram Moolenaar61abe7d2022-08-30 21:46:08 +01003159 call assert_fails('autocmd Test Bufabc Xinvfile set ft=vim', 'E216:')
Bram Moolenaaree4e0c12020-04-06 21:35:05 +02003160 augroup! Test
3161 " Execute all autocmds
3162 call assert_fails('doautocmd * BufEnter', 'E217:')
3163 call assert_fails('augroup! x1a2b3', 'E367:')
3164 call assert_fails('autocmd BufNew <buffer=999> pwd', 'E680:')
Bram Moolenaar531be472020-09-23 22:38:05 +02003165 call assert_fails('autocmd BufNew \) set ff=unix', 'E55:')
Bram Moolenaaree4e0c12020-04-06 21:35:05 +02003166endfunc
3167
3168" Test for deep nesting of autocmds
3169func Test_autocmd_deep_nesting()
Bram Moolenaar61abe7d2022-08-30 21:46:08 +01003170 autocmd BufEnter Xdeepfile doautocmd BufEnter Xdeepfile
3171 call assert_fails('doautocmd BufEnter Xdeepfile', 'E218:')
3172 autocmd! BufEnter Xdeepfile
Bram Moolenaaree4e0c12020-04-06 21:35:05 +02003173endfunc
3174
Bram Moolenaarbe5ee862020-06-10 20:56:58 +02003175" Tests for SigUSR1 autocmd event, which is only available on posix systems.
3176func Test_autocmd_sigusr1()
3177 CheckUnix
Bram Moolenaar0056ca72022-09-23 21:26:39 +01003178 " FIXME: should this work on MacOS M1?
3179 CheckNotMacM1
Bram Moolenaar62cd26a2020-10-11 20:08:44 +02003180 CheckExecutable /bin/kill
Bram Moolenaarbe5ee862020-06-10 20:56:58 +02003181
3182 let g:sigusr1_passed = 0
3183 au SigUSR1 * let g:sigusr1_passed = 1
3184 call system('/bin/kill -s usr1 ' . getpid())
3185 call WaitForAssert({-> assert_true(g:sigusr1_passed)})
3186
3187 au! SigUSR1
3188 unlet g:sigusr1_passed
3189endfunc
3190
Bram Moolenaarb340bae2020-06-15 19:51:56 +02003191" Test for BufReadPre autocmd deleting the file
3192func Test_BufReadPre_delfile()
3193 augroup TestAuCmd
3194 au!
Bram Moolenaare7cda972022-08-29 11:02:59 +01003195 autocmd BufReadPre XbufreadPre call delete('XbufreadPre')
Bram Moolenaarb340bae2020-06-15 19:51:56 +02003196 augroup END
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01003197 call writefile([], 'XbufreadPre', 'D')
Bram Moolenaare7cda972022-08-29 11:02:59 +01003198 call assert_fails('new XbufreadPre', 'E200:')
3199 call assert_equal('XbufreadPre', @%)
Bram Moolenaarb340bae2020-06-15 19:51:56 +02003200 call assert_equal(1, &readonly)
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01003201
Bram Moolenaarb340bae2020-06-15 19:51:56 +02003202 augroup TestAuCmd
3203 au!
3204 augroup END
3205 close!
3206endfunc
3207
3208" Test for BufReadPre autocmd changing the current buffer
3209func Test_BufReadPre_changebuf()
3210 augroup TestAuCmd
3211 au!
Bram Moolenaare7cda972022-08-29 11:02:59 +01003212 autocmd BufReadPre Xchangebuf edit Xsomeotherfile
Bram Moolenaarb340bae2020-06-15 19:51:56 +02003213 augroup END
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01003214 call writefile([], 'Xchangebuf', 'D')
Bram Moolenaare7cda972022-08-29 11:02:59 +01003215 call assert_fails('new Xchangebuf', 'E201:')
Bram Moolenaarb340bae2020-06-15 19:51:56 +02003216 call assert_equal('Xsomeotherfile', @%)
3217 call assert_equal(1, &readonly)
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01003218
Bram Moolenaarb340bae2020-06-15 19:51:56 +02003219 augroup TestAuCmd
3220 au!
3221 augroup END
3222 close!
3223endfunc
3224
3225" Test for BufWipeouti autocmd changing the current buffer when reading a file
3226" in an empty buffer with 'f' flag in 'cpo'
3227func Test_BufDelete_changebuf()
3228 new
3229 augroup TestAuCmd
3230 au!
3231 autocmd BufWipeout * let bufnr = bufadd('somefile') | exe "b " .. bufnr
3232 augroup END
3233 let save_cpo = &cpo
3234 set cpo+=f
Bram Moolenaarb18b4962022-09-02 21:55:50 +01003235 call assert_fails('r Xchangebuf', ['E812:', 'E484:'])
Bram Moolenaarb340bae2020-06-15 19:51:56 +02003236 call assert_equal('somefile', @%)
3237 let &cpo = save_cpo
3238 augroup TestAuCmd
3239 au!
3240 augroup END
3241 close!
3242endfunc
3243
Bram Moolenaar0fe937f2020-06-16 22:42:04 +02003244" Test for the temporary internal window used to execute autocmds
3245func Test_autocmd_window()
3246 %bw!
3247 edit one.txt
3248 tabnew two.txt
Bram Moolenaar41cd8032021-03-13 15:47:56 +01003249 vnew three.txt
3250 tabnew four.txt
3251 tabprevious
Bram Moolenaar0fe937f2020-06-16 22:42:04 +02003252 let g:blist = []
Bram Moolenaar832adf92020-06-25 19:01:36 +02003253 augroup aucmd_win_test1
Bram Moolenaar0fe937f2020-06-16 22:42:04 +02003254 au!
3255 au BufEnter * call add(g:blist, [expand('<afile>'),
3256 \ win_gettype(bufwinnr(expand('<afile>')))])
3257 augroup END
3258
3259 doautoall BufEnter
Bram Moolenaar41cd8032021-03-13 15:47:56 +01003260 call assert_equal([
3261 \ ['one.txt', 'autocmd'],
3262 \ ['two.txt', ''],
3263 \ ['four.txt', 'autocmd'],
3264 \ ['three.txt', ''],
3265 \ ], g:blist)
Bram Moolenaar0fe937f2020-06-16 22:42:04 +02003266
Bram Moolenaar832adf92020-06-25 19:01:36 +02003267 augroup aucmd_win_test1
Bram Moolenaar0fe937f2020-06-16 22:42:04 +02003268 au!
3269 augroup END
Bram Moolenaar832adf92020-06-25 19:01:36 +02003270 augroup! aucmd_win_test1
3271 %bw!
3272endfunc
3273
3274" Test for trying to close the temporary window used for executing an autocmd
3275func Test_close_autocmd_window()
3276 %bw!
3277 edit one.txt
3278 tabnew two.txt
3279 augroup aucmd_win_test2
3280 au!
3281 au BufEnter * if expand('<afile>') == 'one.txt' | 1close | endif
3282 augroup END
3283
3284 call assert_fails('doautoall BufEnter', 'E813:')
3285
3286 augroup aucmd_win_test2
3287 au!
3288 augroup END
3289 augroup! aucmd_win_test2
Bram Moolenaarcf844172020-06-26 19:44:06 +02003290 %bwipe!
3291endfunc
3292
3293" Test for trying to close the tab that has the temporary window for exeucing
3294" an autocmd.
3295func Test_close_autocmd_tab()
3296 edit one.txt
3297 tabnew two.txt
3298 augroup aucmd_win_test
3299 au!
3300 au BufEnter * if expand('<afile>') == 'one.txt' | tabfirst | tabonly | endif
3301 augroup END
3302
3303 call assert_fails('doautoall BufEnter', 'E813:')
3304
3305 tabonly
3306 augroup aucmd_win_test
3307 au!
3308 augroup END
3309 augroup! aucmd_win_test
3310 %bwipe!
Bram Moolenaar0fe937f2020-06-16 22:42:04 +02003311endfunc
3312
Bram Moolenaarcb1956d2022-01-07 15:45:18 +00003313func Test_Visual_doautoall_redraw()
3314 call setline(1, ['a', 'b'])
3315 new
3316 wincmd p
3317 call feedkeys("G\<C-V>", 'txn')
3318 autocmd User Explode ++once redraw
3319 doautoall User Explode
3320 %bwipe!
3321endfunc
3322
Bram Moolenaar6bcb8772021-02-03 21:23:29 +01003323" This was using freed memory.
3324func Test_BufNew_arglocal()
3325 arglocal
3326 au BufNew * arglocal
3327 call assert_fails('drop xx', 'E1156:')
3328
3329 au! BufNew
3330endfunc
3331
Bram Moolenaar8ab37572021-02-03 21:56:59 +01003332func Test_autocmd_closes_window()
3333 au BufNew,BufWinLeave * e %e
3334 file yyy
3335 au BufNew,BufWinLeave * ball
Bram Moolenaaraad5f9d2021-02-06 17:30:31 +01003336 n xxx
Bram Moolenaar8ab37572021-02-03 21:56:59 +01003337
Bram Moolenaaraad5f9d2021-02-06 17:30:31 +01003338 %bwipe
Bram Moolenaar8ab37572021-02-03 21:56:59 +01003339 au! BufNew
3340 au! BufWinLeave
3341endfunc
3342
Bram Moolenaar92bb83e2021-02-03 23:04:46 +01003343func Test_autocmd_quit_psearch()
3344 sn aa bb
3345 augroup aucmd_win_test
3346 au!
3347 au BufEnter,BufLeave,BufNew,WinEnter,WinLeave,WinNew * if winnr('$') > 1 | q | endif
3348 augroup END
3349 ps /
3350
3351 augroup aucmd_win_test
3352 au!
3353 augroup END
zeertzjq7851c692022-04-21 11:14:01 +01003354 new
3355 pclose
Bram Moolenaar92bb83e2021-02-03 23:04:46 +01003356endfunc
3357
Bram Moolenaaraad5f9d2021-02-06 17:30:31 +01003358" Fuzzer found some strange combination that caused a crash.
3359func Test_autocmd_normal_mess()
Bram Moolenaardd07c022021-02-07 13:32:46 +01003360 " For unknown reason this hangs on MS-Windows
3361 CheckNotMSWindows
3362
Bram Moolenaaraad5f9d2021-02-06 17:30:31 +01003363 augroup aucmd_normal_test
3364 au BufLeave,BufWinLeave,BufHidden,BufUnload,BufDelete,BufWipeout * norm 7q/qc
3365 augroup END
Bram Moolenaar983d83f2021-02-07 12:12:43 +01003366 call assert_fails('o4', 'E1159')
Bram Moolenaaraad5f9d2021-02-06 17:30:31 +01003367 silent! H
Bram Moolenaar983d83f2021-02-07 12:12:43 +01003368 call assert_fails('e xx', 'E1159')
Bram Moolenaaraad5f9d2021-02-06 17:30:31 +01003369 normal G
3370
3371 augroup aucmd_normal_test
3372 au!
3373 augroup END
3374endfunc
3375
Bram Moolenaar8c6951f2021-02-06 18:08:45 +01003376func Test_autocmd_closing_cmdwin()
Bram Moolenaardd07c022021-02-07 13:32:46 +01003377 " For unknown reason this hangs on MS-Windows
3378 CheckNotMSWindows
3379
Bram Moolenaar8c6951f2021-02-06 18:08:45 +01003380 au BufWinLeave * nested q
3381 call assert_fails("norm 7q?\n", 'E855:')
3382
3383 au! BufWinLeave
3384 new
3385 only
3386endfunc
3387
Bram Moolenaar2c7080b2021-02-06 19:19:42 +01003388func Test_autocmd_vimgrep()
3389 augroup aucmd_vimgrep
Charlie Grovesfef44852022-04-19 16:24:12 +01003390 au QuickfixCmdPre,BufNew,BufReadCmd * sb
zeertzjq7851c692022-04-21 11:14:01 +01003391 au QuickfixCmdPre,BufNew,BufReadCmd * q9
Bram Moolenaar2c7080b2021-02-06 19:19:42 +01003392 augroup END
Bram Moolenaardd07c022021-02-07 13:32:46 +01003393 call assert_fails('lv ?a? foo', 'E926:')
Bram Moolenaar2c7080b2021-02-06 19:19:42 +01003394
3395 augroup aucmd_vimgrep
3396 au!
3397 augroup END
3398endfunc
3399
Bram Moolenaar73b8b0a2021-08-01 14:52:32 +02003400func Test_autocmd_with_block()
3401 augroup block_testing
3402 au BufReadPost *.xml {
3403 setlocal matchpairs+=<:>
3404 /<start
3405 }
Bram Moolenaar63b91732021-08-05 20:40:03 +02003406 au CursorHold * {
3407 autocmd BufReadPre * ++once echo 'one' | echo 'two'
3408 g:gotSafeState = 77
3409 }
Bram Moolenaar73b8b0a2021-08-01 14:52:32 +02003410 augroup END
3411
3412 let expected = "\n--- Autocommands ---\nblock_testing BufRead\n *.xml {^@ setlocal matchpairs+=<:>^@ /<start^@ }"
3413 call assert_equal(expected, execute('au BufReadPost *.xml'))
3414
Bram Moolenaar63b91732021-08-05 20:40:03 +02003415 doautocmd CursorHold
3416 call assert_equal(77, g:gotSafeState)
3417 unlet g:gotSafeState
3418
Bram Moolenaar73b8b0a2021-08-01 14:52:32 +02003419 augroup block_testing
3420 au!
Bram Moolenaar75ebd2a2022-06-03 17:39:46 +01003421 autocmd CursorHold * {
3422 if true
3423 # comment
3424 && true
3425
3426 && true
3427 g:done = 'yes'
3428 endif
3429 }
3430 augroup END
3431 doautocmd CursorHold
3432 call assert_equal('yes', g:done)
3433
3434 unlet g:done
3435 augroup block_testing
3436 au!
Bram Moolenaar73b8b0a2021-08-01 14:52:32 +02003437 augroup END
3438endfunc
3439
Christian Brabandtdb3b4462021-10-16 11:58:55 +01003440" Test TextChangedI and TextChanged
3441func Test_Changed_ChangedI()
3442 new
3443 call test_override("char_avail", 1)
3444 let [g:autocmd_i, g:autocmd_n] = ['','']
3445
3446 func! TextChangedAutocmdI(char)
3447 let g:autocmd_{tolower(a:char)} = a:char .. b:changedtick
3448 endfunc
3449
3450 augroup Test_TextChanged
3451 au!
3452 au TextChanged <buffer> :call TextChangedAutocmdI('N')
3453 au TextChangedI <buffer> :call TextChangedAutocmdI('I')
3454 augroup END
3455
3456 call feedkeys("ifoo\<esc>", 'tnix')
3457 " TODO: Test test does not seem to trigger TextChanged autocommand, this
3458 " requires running Vim in a terminal window.
3459 " call assert_equal('N3', g:autocmd_n)
3460 call assert_equal('I3', g:autocmd_i)
3461
3462 call feedkeys("yyp", 'tnix')
3463 " TODO: Test test does not seem to trigger TextChanged autocommand.
3464 " call assert_equal('N4', g:autocmd_n)
3465 call assert_equal('I3', g:autocmd_i)
3466
3467 " CleanUp
3468 call test_override("char_avail", 0)
3469 au! TextChanged <buffer>
3470 au! TextChangedI <buffer>
3471 augroup! Test_TextChanged
3472 delfu TextChangedAutocmdI
3473 unlet! g:autocmd_i g:autocmd_n
3474
3475 bw!
3476endfunc
Bram Moolenaar2c7080b2021-02-06 19:19:42 +01003477
Bram Moolenaar6f2465d2022-03-22 18:13:01 +00003478func Test_closing_autocmd_window()
3479 let lines =<< trim END
3480 edit Xa.txt
3481 tabnew Xb.txt
3482 autocmd BufEnter Xa.txt unhide 1
3483 doautoall BufEnter
3484 END
3485 call v9.CheckScriptFailure(lines, 'E814:')
3486 au! BufEnter
3487 only!
3488 bwipe Xa.txt
3489 bwipe Xb.txt
3490endfunc
3491
Bram Moolenaar347538f2022-03-26 16:28:06 +00003492func Test_bufwipeout_changes_window()
3493 " This should not crash, but we don't have any expectations about what
3494 " happens, changing window in BufWipeout has unpredictable results.
3495 tabedit
3496 let g:window_id = win_getid()
3497 topleft new
3498 setlocal bufhidden=wipe
3499 autocmd BufWipeout <buffer> call win_gotoid(g:window_id)
3500 tabprevious
3501 +tabclose
3502
3503 unlet g:window_id
3504 au! BufWipeout
3505 %bwipe!
3506endfunc
3507
zeertzjq021996f2022-04-10 11:44:04 +01003508func Test_v_event_readonly()
3509 autocmd CompleteChanged * let v:event.width = 0
3510 call assert_fails("normal! i\<C-X>\<C-V>", 'E46:')
3511 au! CompleteChanged
3512
3513 autocmd DirChangedPre * let v:event.directory = ''
3514 call assert_fails('cd .', 'E46:')
3515 au! DirChangedPre
3516
3517 autocmd ModeChanged * let v:event.new_mode = ''
3518 call assert_fails('normal! cc', 'E46:')
3519 au! ModeChanged
3520
3521 autocmd TextYankPost * let v:event.operator = ''
3522 call assert_fails('normal! yy', 'E46:')
3523 au! TextYankPost
3524endfunc
3525
zeertzjqc9e8fd62022-07-26 18:12:38 +01003526" Test for ModeChanged pattern
3527func Test_mode_changes()
3528 let g:index = 0
3529 let g:mode_seq = ['n', 'i', 'n', 'v', 'V', 'i', 'ix', 'i', 'ic', 'i', 'n', 'no', 'n', 'V', 'v', 's', 'n']
3530 func! TestMode()
3531 call assert_equal(g:mode_seq[g:index], get(v:event, "old_mode"))
3532 call assert_equal(g:mode_seq[g:index + 1], get(v:event, "new_mode"))
3533 call assert_equal(mode(1), get(v:event, "new_mode"))
3534 let g:index += 1
3535 endfunc
3536
3537 au ModeChanged * :call TestMode()
3538 let g:n_to_any = 0
3539 au ModeChanged n:* let g:n_to_any += 1
3540 call feedkeys("i\<esc>vVca\<CR>\<C-X>\<C-L>\<esc>ggdG", 'tnix')
3541
3542 let g:V_to_v = 0
3543 au ModeChanged V:v let g:V_to_v += 1
3544 call feedkeys("Vv\<C-G>\<esc>", 'tnix')
3545 call assert_equal(len(filter(g:mode_seq[1:], {idx, val -> val == 'n'})), g:n_to_any)
3546 call assert_equal(1, g:V_to_v)
3547 call assert_equal(len(g:mode_seq) - 1, g:index)
3548
3549 let g:n_to_i = 0
3550 au ModeChanged n:i let g:n_to_i += 1
3551 let g:n_to_niI = 0
3552 au ModeChanged i:niI let g:n_to_niI += 1
3553 let g:niI_to_i = 0
3554 au ModeChanged niI:i let g:niI_to_i += 1
3555 let g:nany_to_i = 0
3556 au ModeChanged n*:i let g:nany_to_i += 1
3557 let g:i_to_n = 0
3558 au ModeChanged i:n let g:i_to_n += 1
3559 let g:nori_to_any = 0
3560 au ModeChanged [ni]:* let g:nori_to_any += 1
3561 let g:i_to_any = 0
3562 au ModeChanged i:* let g:i_to_any += 1
3563 let g:index = 0
3564 let g:mode_seq = ['n', 'i', 'niI', 'i', 'n']
3565 call feedkeys("a\<C-O>l\<esc>", 'tnix')
3566 call assert_equal(len(g:mode_seq) - 1, g:index)
3567 call assert_equal(1, g:n_to_i)
3568 call assert_equal(1, g:n_to_niI)
3569 call assert_equal(1, g:niI_to_i)
3570 call assert_equal(2, g:nany_to_i)
3571 call assert_equal(1, g:i_to_n)
3572 call assert_equal(2, g:i_to_any)
3573 call assert_equal(3, g:nori_to_any)
3574
3575 if has('terminal')
3576 let g:mode_seq += ['c', 'n', 't', 'nt', 'c', 'nt', 'n']
3577 call feedkeys(":term\<CR>\<C-W>N:bd!\<CR>", 'tnix')
3578 call assert_equal(len(g:mode_seq) - 1, g:index)
3579 call assert_equal(1, g:n_to_i)
3580 call assert_equal(1, g:n_to_niI)
3581 call assert_equal(1, g:niI_to_i)
3582 call assert_equal(2, g:nany_to_i)
3583 call assert_equal(1, g:i_to_n)
3584 call assert_equal(2, g:i_to_any)
3585 call assert_equal(5, g:nori_to_any)
3586 endif
3587
zeertzjqd1955982022-10-05 11:24:46 +01003588 let g:n_to_c = 0
3589 au ModeChanged n:c let g:n_to_c += 1
3590 let g:c_to_n = 0
3591 au ModeChanged c:n let g:c_to_n += 1
3592 let g:mode_seq += ['c', 'n', 'c', 'n']
3593 call feedkeys("q:\<C-C>\<Esc>", 'tnix')
3594 call assert_equal(len(g:mode_seq) - 1, g:index)
3595 call assert_equal(2, g:n_to_c)
3596 call assert_equal(2, g:c_to_n)
3597 unlet g:n_to_c
3598 unlet g:c_to_n
zeertzjqc9e8fd62022-07-26 18:12:38 +01003599
Bram Moolenaar61c4b042022-10-18 15:10:11 +01003600 let g:n_to_v = 0
3601 au ModeChanged n:v let g:n_to_v += 1
3602 let g:v_to_n = 0
3603 au ModeChanged v:n let g:v_to_n += 1
3604 let g:mode_seq += ['v', 'n']
3605 call feedkeys("v\<C-C>", 'tnix')
3606 call assert_equal(len(g:mode_seq) - 1, g:index)
3607 call assert_equal(1, g:n_to_v)
3608 call assert_equal(1, g:v_to_n)
3609 unlet g:n_to_v
3610 unlet g:v_to_n
3611
zeertzjqc9e8fd62022-07-26 18:12:38 +01003612 au! ModeChanged
3613 delfunc TestMode
3614 unlet! g:mode_seq
3615 unlet! g:index
3616 unlet! g:n_to_any
3617 unlet! g:V_to_v
3618 unlet! g:n_to_i
3619 unlet! g:n_to_niI
3620 unlet! g:niI_to_i
3621 unlet! g:nany_to_i
3622 unlet! g:i_to_n
3623 unlet! g:nori_to_any
3624 unlet! g:i_to_any
3625endfunc
3626
3627func Test_recursive_ModeChanged()
3628 au! ModeChanged * norm 0u
3629 sil! norm 
3630 au! ModeChanged
3631endfunc
3632
3633func Test_ModeChanged_starts_visual()
3634 " This was triggering ModeChanged before setting VIsual, causing a crash.
3635 au! ModeChanged * norm 0u
3636 sil! norm 
3637
3638 au! ModeChanged
3639endfunc
Bram Moolenaar347538f2022-03-26 16:28:06 +00003640
Charlie Grovesfef44852022-04-19 16:24:12 +01003641func Test_noname_autocmd()
3642 augroup test_noname_autocmd_group
3643 autocmd!
3644 autocmd BufEnter * call add(s:li, ["BufEnter", expand("<afile>")])
3645 autocmd BufDelete * call add(s:li, ["BufDelete", expand("<afile>")])
3646 autocmd BufLeave * call add(s:li, ["BufLeave", expand("<afile>")])
3647 autocmd BufUnload * call add(s:li, ["BufUnload", expand("<afile>")])
3648 autocmd BufWipeout * call add(s:li, ["BufWipeout", expand("<afile>")])
3649 augroup END
3650
3651 let s:li = []
3652 edit foo
3653 call assert_equal([['BufUnload', ''], ['BufDelete', ''], ['BufWipeout', ''], ['BufEnter', 'foo']], s:li)
3654
3655 au! test_noname_autocmd_group
3656 augroup! test_noname_autocmd_group
3657endfunc
3658
Yegappan Lakshmanan1755a912022-05-19 10:31:47 +01003659" Test for the autocmd_get() function
3660func Test_autocmd_get()
3661 augroup TestAutoCmdFns
3662 au!
3663 autocmd BufAdd *.vim echo "bufadd-vim"
3664 autocmd BufAdd *.py echo "bufadd-py"
3665 autocmd BufHidden *.vim echo "bufhidden"
3666 augroup END
3667 augroup TestAutoCmdFns2
3668 autocmd BufAdd *.vim echo "bufadd-vim-2"
3669 autocmd BufRead *.a1b2c3 echo "bufadd-vim-2"
3670 augroup END
3671
3672 let l = autocmd_get()
3673 call assert_true(l->len() > 0)
3674
3675 " Test for getting all the autocmds in a group
3676 let expected = [
3677 \ #{cmd: 'echo "bufadd-vim"', group: 'TestAutoCmdFns',
3678 \ pattern: '*.vim', nested: v:false, once: v:false,
3679 \ event: 'BufAdd'},
3680 \ #{cmd: 'echo "bufadd-py"', group: 'TestAutoCmdFns',
3681 \ pattern: '*.py', nested: v:false, once: v:false,
3682 \ event: 'BufAdd'},
3683 \ #{cmd: 'echo "bufhidden"', group: 'TestAutoCmdFns',
3684 \ pattern: '*.vim', nested: v:false,
3685 \ once: v:false, event: 'BufHidden'}]
3686 call assert_equal(expected, autocmd_get(#{group: 'TestAutoCmdFns'}))
3687
3688 " Test for getting autocmds for all the patterns in a group
3689 call assert_equal(expected, autocmd_get(#{group: 'TestAutoCmdFns',
3690 \ event: '*'}))
3691
3692 " Test for getting autocmds for an event in a group
3693 let expected = [
3694 \ #{cmd: 'echo "bufadd-vim"', group: 'TestAutoCmdFns',
3695 \ pattern: '*.vim', nested: v:false, once: v:false,
3696 \ event: 'BufAdd'},
3697 \ #{cmd: 'echo "bufadd-py"', group: 'TestAutoCmdFns',
3698 \ pattern: '*.py', nested: v:false, once: v:false,
3699 \ event: 'BufAdd'}]
3700 call assert_equal(expected, autocmd_get(#{group: 'TestAutoCmdFns',
3701 \ event: 'BufAdd'}))
3702
3703 " Test for getting the autocmds for all the events in a group for particular
3704 " pattern
3705 call assert_equal([{'cmd': 'echo "bufadd-py"', 'group': 'TestAutoCmdFns',
3706 \ 'pattern': '*.py', 'nested': v:false, 'once': v:false,
3707 \ 'event': 'BufAdd'}],
3708 \ autocmd_get(#{group: 'TestAutoCmdFns', event: '*', pattern: '*.py'}))
3709
3710 " Test for getting the autocmds for an events in a group for particular
3711 " pattern
3712 let l = autocmd_get(#{group: 'TestAutoCmdFns', event: 'BufAdd',
3713 \ pattern: '*.vim'})
3714 call assert_equal([
3715 \ #{cmd: 'echo "bufadd-vim"', group: 'TestAutoCmdFns',
3716 \ pattern: '*.vim', nested: v:false, once: v:false,
3717 \ event: 'BufAdd'}], l)
3718
3719 " Test for getting the autocmds for a pattern in a group
3720 let l = autocmd_get(#{group: 'TestAutoCmdFns', pattern: '*.vim'})
3721 call assert_equal([
3722 \ #{cmd: 'echo "bufadd-vim"', group: 'TestAutoCmdFns',
3723 \ pattern: '*.vim', nested: v:false, once: v:false,
3724 \ event: 'BufAdd'},
3725 \ #{cmd: 'echo "bufhidden"', group: 'TestAutoCmdFns',
3726 \ pattern: '*.vim', nested: v:false,
3727 \ once: v:false, event: 'BufHidden'}], l)
3728
3729 " Test for getting the autocmds for a pattern in all the groups
3730 let l = autocmd_get(#{pattern: '*.a1b2c3'})
3731 call assert_equal([{'cmd': 'echo "bufadd-vim-2"', 'group': 'TestAutoCmdFns2',
3732 \ 'pattern': '*.a1b2c3', 'nested': v:false, 'once': v:false,
3733 \ 'event': 'BufRead'}], l)
3734
3735 " Test for getting autocmds for a pattern without any autocmds
3736 call assert_equal([], autocmd_get(#{group: 'TestAutoCmdFns',
3737 \ pattern: '*.abc'}))
3738 call assert_equal([], autocmd_get(#{group: 'TestAutoCmdFns',
3739 \ event: 'BufAdd', pattern: '*.abc'}))
3740 call assert_equal([], autocmd_get(#{group: 'TestAutoCmdFns',
3741 \ event: 'BufWipeout'}))
3742 call assert_fails("call autocmd_get(#{group: 'abc', event: 'BufAdd'})",
3743 \ 'E367:')
3744 let cmd = "echo autocmd_get(#{group: 'TestAutoCmdFns', event: 'abc'})"
3745 call assert_fails(cmd, 'E216:')
3746 call assert_fails("call autocmd_get(#{group: 'abc'})", 'E367:')
3747 call assert_fails("echo autocmd_get(#{event: 'abc'})", 'E216:')
3748
3749 augroup TestAutoCmdFns
3750 au!
3751 augroup END
3752 call assert_equal([], autocmd_get(#{group: 'TestAutoCmdFns'}))
3753
3754 " Test for nested and once autocmds
3755 augroup TestAutoCmdFns
3756 au!
3757 autocmd VimSuspend * ++nested echo "suspend"
3758 autocmd VimResume * ++once echo "resume"
3759 augroup END
3760
3761 let expected = [
3762 \ {'cmd': 'echo "suspend"', 'group': 'TestAutoCmdFns', 'pattern': '*',
3763 \ 'nested': v:true, 'once': v:false, 'event': 'VimSuspend'},
3764 \ {'cmd': 'echo "resume"', 'group': 'TestAutoCmdFns', 'pattern': '*',
3765 \ 'nested': v:false, 'once': v:true, 'event': 'VimResume'}]
3766 call assert_equal(expected, autocmd_get(#{group: 'TestAutoCmdFns'}))
3767
3768 " Test for buffer-local autocmd
3769 augroup TestAutoCmdFns
3770 au!
3771 autocmd TextYankPost <buffer> echo "textyankpost"
3772 augroup END
3773
3774 let expected = [
3775 \ {'cmd': 'echo "textyankpost"', 'group': 'TestAutoCmdFns',
3776 \ 'pattern': '<buffer=' .. bufnr() .. '>', 'nested': v:false,
3777 \ 'once': v:false, 'bufnr': bufnr(), 'event': 'TextYankPost'}]
3778 call assert_equal(expected, autocmd_get(#{group: 'TestAutoCmdFns'}))
3779
3780 augroup TestAutoCmdFns
3781 au!
3782 augroup END
3783 augroup! TestAutoCmdFns
3784 augroup TestAutoCmdFns2
3785 au!
3786 augroup END
3787 augroup! TestAutoCmdFns2
3788
3789 call assert_fails("echo autocmd_get(#{group: []})", 'E730:')
3790 call assert_fails("echo autocmd_get(#{event: {}})", 'E731:')
3791 call assert_fails("echo autocmd_get([])", 'E1206:')
3792endfunc
3793
3794" Test for the autocmd_add() function
3795func Test_autocmd_add()
3796 " Define a single autocmd in a group
3797 call autocmd_add([#{group: 'TestAcSet', event: 'BufAdd', pattern: '*.sh',
3798 \ cmd: 'echo "bufadd"', once: v:true, nested: v:true}])
3799 call assert_equal([#{cmd: 'echo "bufadd"', group: 'TestAcSet',
3800 \ pattern: '*.sh', nested: v:true, once: v:true,
3801 \ event: 'BufAdd'}], autocmd_get(#{group: 'TestAcSet'}))
3802
3803 " Define two autocmds in the same group
3804 call autocmd_delete([#{group: 'TestAcSet'}])
3805 call autocmd_add([#{group: 'TestAcSet', event: 'BufAdd', pattern: '*.sh',
3806 \ cmd: 'echo "bufadd"'},
3807 \ #{group: 'TestAcSet', event: 'BufEnter', pattern: '*.sh',
3808 \ cmd: 'echo "bufenter"'}])
3809 call assert_equal([
3810 \ #{cmd: 'echo "bufadd"', group: 'TestAcSet', pattern: '*.sh',
3811 \ nested: v:false, once: v:false, event: 'BufAdd'},
3812 \ #{cmd: 'echo "bufenter"', group: 'TestAcSet', pattern: '*.sh',
3813 \ nested: v:false, once: v:false, event: 'BufEnter'}],
3814 \ autocmd_get(#{group: 'TestAcSet'}))
3815
3816 " Define a buffer-local autocmd
3817 call autocmd_delete([#{group: 'TestAcSet'}])
3818 call autocmd_add([#{group: 'TestAcSet', event: 'CursorHold',
3819 \ bufnr: bufnr(), cmd: 'echo "cursorhold"'}])
3820 call assert_equal([
3821 \ #{cmd: 'echo "cursorhold"', group: 'TestAcSet',
3822 \ pattern: '<buffer=' .. bufnr() .. '>', nested: v:false,
3823 \ once: v:false, bufnr: bufnr(), event: 'CursorHold'}],
3824 \ autocmd_get(#{group: 'TestAcSet'}))
3825
3826 " Use an invalid buffer number
3827 call autocmd_delete([#{group: 'TestAcSet'}])
3828 call autocmd_add([#{group: 'TestAcSet', event: 'BufEnter',
3829 \ bufnr: -1, cmd: 'echo "bufenter"'}])
3830 let l = [#{group: 'TestAcSet', event: 'BufAdd', bufnr: 9999,
3831 \ cmd: 'echo "bufadd"'}]
3832 call assert_fails("echo autocmd_add(l)", 'E680:')
Yegappan Lakshmanan00e977c2022-06-01 12:31:53 +01003833 let l = [#{group: 'TestAcSet', event: 'BufAdd', bufnr: 9999,
3834 \ pattern: '*.py', cmd: 'echo "bufadd"'}]
3835 call assert_fails("echo autocmd_add(l)", 'E680:')
3836 let l = [#{group: 'TestAcSet', event: 'BufAdd', bufnr: 9999,
3837 \ pattern: ['*.py', '*.c'], cmd: 'echo "bufadd"'}]
3838 call assert_fails("echo autocmd_add(l)", 'E680:')
Yegappan Lakshmanan1755a912022-05-19 10:31:47 +01003839 let l = [#{group: 'TestAcSet', event: 'BufRead', bufnr: [],
3840 \ cmd: 'echo "bufread"'}]
3841 call assert_fails("echo autocmd_add(l)", 'E745:')
3842 call assert_equal([], autocmd_get(#{group: 'TestAcSet'}))
3843
3844 " Add two commands to the same group, event and pattern
3845 call autocmd_delete([#{group: 'TestAcSet'}])
3846 call autocmd_add([#{group: 'TestAcSet', event: 'BufUnload',
3847 \ pattern: 'abc', cmd: 'echo "cmd1"'}])
3848 call autocmd_add([#{group: 'TestAcSet', event: 'BufUnload',
3849 \ pattern: 'abc', cmd: 'echo "cmd2"'}])
3850 call assert_equal([
3851 \ #{cmd: 'echo "cmd1"', group: 'TestAcSet', pattern: 'abc',
3852 \ nested: v:false, once: v:false, event: 'BufUnload'},
3853 \ #{cmd: 'echo "cmd2"', group: 'TestAcSet', pattern: 'abc',
3854 \ nested: v:false, once: v:false, event: 'BufUnload'}],
3855 \ autocmd_get(#{group: 'TestAcSet'}))
3856
3857 " When adding a new autocmd, if the autocmd 'group' is not specified, then
3858 " the current autocmd group should be used.
3859 call autocmd_delete([#{group: 'TestAcSet'}])
3860 augroup TestAcSet
3861 call autocmd_add([#{event: 'BufHidden', pattern: 'abc', cmd: 'echo "abc"'}])
3862 augroup END
3863 call assert_equal([
3864 \ #{cmd: 'echo "abc"', group: 'TestAcSet', pattern: 'abc',
3865 \ nested: v:false, once: v:false, event: 'BufHidden'}],
3866 \ autocmd_get(#{group: 'TestAcSet'}))
3867
Yegappan Lakshmanan971f6822022-05-24 11:40:11 +01003868 " Test for replacing a cmd for an event in a group
3869 call autocmd_delete([#{group: 'TestAcSet'}])
3870 call autocmd_add([#{replace: v:true, group: 'TestAcSet', event: 'BufEnter',
3871 \ pattern: '*.py', cmd: 'echo "bufenter"'}])
3872 call autocmd_add([#{replace: v:true, group: 'TestAcSet', event: 'BufEnter',
3873 \ pattern: '*.py', cmd: 'echo "bufenter"'}])
3874 call assert_equal([
3875 \ #{cmd: 'echo "bufenter"', group: 'TestAcSet', pattern: '*.py',
3876 \ nested: v:false, once: v:false, event: 'BufEnter'}],
3877 \ autocmd_get(#{group: 'TestAcSet'}))
3878
3879 " Test for adding a command for an unsupported autocmd event
Yegappan Lakshmanan1755a912022-05-19 10:31:47 +01003880 let l = [#{group: 'TestAcSet', event: 'abc', pattern: '*.sh',
3881 \ cmd: 'echo "bufadd"'}]
3882 call assert_fails('call autocmd_add(l)', 'E216:')
3883
Yegappan Lakshmanane0ff3a72022-05-27 18:05:33 +01003884 " Test for using a list of events and patterns
3885 call autocmd_delete([#{group: 'TestAcSet'}])
3886 let l = [#{group: 'TestAcSet', event: ['BufEnter', 'BufLeave'],
3887 \ pattern: ['*.py', '*.sh'], cmd: 'echo "bufcmds"'}]
3888 call autocmd_add(l)
3889 call assert_equal([
3890 \ #{cmd: 'echo "bufcmds"', group: 'TestAcSet', pattern: '*.py',
3891 \ nested: v:false, once: v:false, event: 'BufEnter'},
3892 \ #{cmd: 'echo "bufcmds"', group: 'TestAcSet', pattern: '*.sh',
3893 \ nested: v:false, once: v:false, event: 'BufEnter'},
3894 \ #{cmd: 'echo "bufcmds"', group: 'TestAcSet', pattern: '*.py',
3895 \ nested: v:false, once: v:false, event: 'BufLeave'},
3896 \ #{cmd: 'echo "bufcmds"', group: 'TestAcSet', pattern: '*.sh',
3897 \ nested: v:false, once: v:false, event: 'BufLeave'}],
3898 \ autocmd_get(#{group: 'TestAcSet'}))
3899
3900 " Test for invalid values for 'event' item
3901 call autocmd_delete([#{group: 'TestAcSet'}])
3902 let l = [#{group: 'TestAcSet', event: test_null_string(),
3903 \ pattern: "*.py", cmd: 'echo "bufcmds"'}]
3904 call assert_fails('call autocmd_add(l)', 'E928:')
3905 let l = [#{group: 'TestAcSet', event: test_null_list(),
3906 \ pattern: "*.py", cmd: 'echo "bufcmds"'}]
3907 call assert_fails('call autocmd_add(l)', 'E714:')
3908 let l = [#{group: 'TestAcSet', event: {},
3909 \ pattern: "*.py", cmd: 'echo "bufcmds"'}]
3910 call assert_fails('call autocmd_add(l)', 'E777:')
3911 let l = [#{group: 'TestAcSet', event: [{}],
3912 \ pattern: "*.py", cmd: 'echo "bufcmds"'}]
3913 call assert_fails('call autocmd_add(l)', 'E928:')
3914 let l = [#{group: 'TestAcSet', event: [test_null_string()],
3915 \ pattern: "*.py", cmd: 'echo "bufcmds"'}]
3916 call assert_fails('call autocmd_add(l)', 'E928:')
3917 let l = [#{group: 'TestAcSet', event: 'BufEnter,BufLeave',
3918 \ pattern: '*.py', cmd: 'echo "bufcmds"'}]
3919 call assert_fails('call autocmd_add(l)', 'E216:')
3920 let l = [#{group: 'TestAcSet', event: [],
3921 \ pattern: "*.py", cmd: 'echo "bufcmds"'}]
3922 call autocmd_add(l)
3923 let l = [#{group: 'TestAcSet', event: [""],
3924 \ pattern: "*.py", cmd: 'echo "bufcmds"'}]
3925 call assert_fails('call autocmd_add(l)', 'E216:')
3926 let l = [#{group: 'TestAcSet', event: "",
3927 \ pattern: "*.py", cmd: 'echo "bufcmds"'}]
3928 call autocmd_add(l)
3929 call assert_equal([], autocmd_get(#{group: 'TestAcSet'}))
3930
3931 " Test for invalid values for 'pattern' item
3932 let l = [#{group: 'TestAcSet', event: "BufEnter",
3933 \ pattern: test_null_string(), cmd: 'echo "bufcmds"'}]
Yegappan Lakshmanan00e977c2022-06-01 12:31:53 +01003934 call assert_fails('call autocmd_add(l)', 'E928:')
Yegappan Lakshmanane0ff3a72022-05-27 18:05:33 +01003935 let l = [#{group: 'TestAcSet', event: "BufEnter",
3936 \ pattern: test_null_list(), cmd: 'echo "bufcmds"'}]
3937 call assert_fails('call autocmd_add(l)', 'E714:')
3938 let l = [#{group: 'TestAcSet', event: "BufEnter",
3939 \ pattern: {}, cmd: 'echo "bufcmds"'}]
3940 call assert_fails('call autocmd_add(l)', 'E777:')
3941 let l = [#{group: 'TestAcSet', event: "BufEnter",
3942 \ pattern: [{}], cmd: 'echo "bufcmds"'}]
3943 call assert_fails('call autocmd_add(l)', 'E928:')
3944 let l = [#{group: 'TestAcSet', event: "BufEnter",
3945 \ pattern: [test_null_string()], cmd: 'echo "bufcmds"'}]
3946 call assert_fails('call autocmd_add(l)', 'E928:')
3947 let l = [#{group: 'TestAcSet', event: "BufEnter",
3948 \ pattern: [], cmd: 'echo "bufcmds"'}]
3949 call autocmd_add(l)
3950 let l = [#{group: 'TestAcSet', event: "BufEnter",
3951 \ pattern: [""], cmd: 'echo "bufcmds"'}]
3952 call autocmd_add(l)
3953 let l = [#{group: 'TestAcSet', event: "BufEnter",
3954 \ pattern: "", cmd: 'echo "bufcmds"'}]
3955 call autocmd_add(l)
3956 call assert_equal([], autocmd_get(#{group: 'TestAcSet'}))
3957
3958 let l = [#{group: 'TestAcSet', event: 'BufEnter,abc,BufLeave',
3959 \ pattern: '*.py', cmd: 'echo "bufcmds"'}]
3960 call assert_fails('call autocmd_add(l)', 'E216:')
3961
Yegappan Lakshmanan1755a912022-05-19 10:31:47 +01003962 call assert_fails("call autocmd_add({})", 'E1211:')
3963 call assert_equal(v:false, autocmd_add(test_null_list()))
3964 call assert_true(autocmd_add([[]]))
3965 call assert_true(autocmd_add([test_null_dict()]))
3966
3967 augroup TestAcSet
3968 au!
3969 augroup END
3970
3971 call autocmd_add([#{group: 'TestAcSet'}])
3972 call autocmd_add([#{group: 'TestAcSet', event: 'BufAdd'}])
3973 call autocmd_add([#{group: 'TestAcSet', pat: '*.sh'}])
3974 call autocmd_add([#{group: 'TestAcSet', cmd: 'echo "a"'}])
3975 call autocmd_add([#{group: 'TestAcSet', event: 'BufAdd', pat: '*.sh'}])
3976 call autocmd_add([#{group: 'TestAcSet', event: 'BufAdd', cmd: 'echo "a"'}])
3977 call autocmd_add([#{group: 'TestAcSet', pat: '*.sh', cmd: 'echo "a"'}])
3978 call assert_equal([], autocmd_get(#{group: 'TestAcSet'}))
3979
3980 augroup! TestAcSet
3981endfunc
3982
3983" Test for deleting autocmd events and groups
3984func Test_autocmd_delete()
3985 " Delete an event in an autocmd group
3986 augroup TestAcSet
3987 au!
3988 au BufAdd *.sh echo "bufadd"
3989 au BufEnter *.sh echo "bufenter"
3990 augroup END
3991 call autocmd_delete([#{group: 'TestAcSet', event: 'BufAdd'}])
3992 call assert_equal([#{cmd: 'echo "bufenter"', group: 'TestAcSet',
3993 \ pattern: '*.sh', nested: v:false, once: v:false,
3994 \ event: 'BufEnter'}], autocmd_get(#{group: 'TestAcSet'}))
3995
3996 " Delete all the events in an autocmd group
3997 augroup TestAcSet
3998 au BufAdd *.sh echo "bufadd"
3999 augroup END
4000 call autocmd_delete([#{group: 'TestAcSet', event: '*'}])
4001 call assert_equal([], autocmd_get(#{group: 'TestAcSet'}))
4002
4003 " Delete a non-existing autocmd group
4004 call assert_fails("call autocmd_delete([#{group: 'abc'}])", 'E367:')
4005 " Delete a non-existing autocmd event
4006 let l = [#{group: 'TestAcSet', event: 'abc'}]
4007 call assert_fails("call autocmd_delete(l)", 'E216:')
4008 " Delete a non-existing autocmd pattern
4009 let l = [#{group: 'TestAcSet', event: 'BufAdd', pat: 'abc'}]
4010 call assert_true(autocmd_delete(l))
Yegappan Lakshmanan00e977c2022-06-01 12:31:53 +01004011 " Delete an autocmd for a non-existing buffer
4012 let l = [#{event: '*', bufnr: 9999, cmd: 'echo "x"'}]
4013 call assert_fails('call autocmd_delete(l)', 'E680:')
Yegappan Lakshmanan1755a912022-05-19 10:31:47 +01004014
4015 " Delete an autocmd group
4016 augroup TestAcSet
4017 au!
4018 au BufAdd *.sh echo "bufadd"
4019 au BufEnter *.sh echo "bufenter"
4020 augroup END
4021 call autocmd_delete([#{group: 'TestAcSet'}])
4022 call assert_fails("call autocmd_get(#{group: 'TestAcSet'})", 'E367:')
4023
4024 call assert_true(autocmd_delete([[]]))
4025 call assert_true(autocmd_delete([test_null_dict()]))
4026endfunc
4027
Bram Moolenaar8f3c3c62022-10-18 17:05:54 +01004028func Test_autocmd_split_dummy()
4029 " Autocommand trying to split a window containing a dummy buffer.
4030 auto BufReadPre * exe "sbuf " .. expand("<abuf>")
4031 " Avoid the "W11" prompt
4032 au FileChangedShell * let v:fcs_choice = 'reload'
4033 func Xautocmd_changelist()
4034 cal writefile(['Xtestfile2:4:4'], 'Xerr')
4035 edit Xerr
4036 lex 'Xtestfile2:4:4'
4037 endfunc
4038 call Xautocmd_changelist()
Bram Moolenaar53c5c9f2022-10-18 17:25:03 +01004039 " Should get E86, but it doesn't always happen (timing?)
4040 silent! call Xautocmd_changelist()
Bram Moolenaar8f3c3c62022-10-18 17:05:54 +01004041
4042 au! BufReadPre
4043 au! FileChangedShell
4044 delfunc Xautocmd_changelist
4045 bwipe! Xerr
4046 call delete('Xerr')
4047endfunc
4048
Bram Moolenaarbc2b71d2020-02-17 21:33:30 +01004049" vim: shiftwidth=2 sts=2 expandtab