blob: a0d83ecf355fead3a88db564dadb4772eb585fa8 [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 Moolenaaref2c3252022-11-25 16:31:51 +00002329func Test_autocmd_CmdlineLeave_unlet()
2330 CheckRunVimInTerminal
2331
2332 let lines =<< trim END
2333 for i in range(1, 999)
2334 exe 'let g:var' .. i '=' i
2335 endfor
2336 au CmdlineLeave : call timer_start(0, {-> execute('unlet g:var990')})
2337 END
2338 call writefile(lines, 'XleaveUnlet', 'D')
2339 let buf = RunVimInTerminal('-S XleaveUnlet', {'rows': 10})
2340
2341 " this was using freed memory
2342 call term_sendkeys(buf, ":let g:\<CR>")
2343 call TermWait(buf, 50)
2344 call term_sendkeys(buf, "G")
2345 call TermWait(buf, 50)
2346 call term_sendkeys(buf, "\<CR>") " for the hit-enter prompt
2347
2348 call StopVimInTerminal(buf)
2349endfunc
2350
Bram Moolenaarb7407d32018-02-03 17:36:27 +01002351function s:Before_test_dirchanged()
2352 augroup test_dirchanged
2353 autocmd!
2354 augroup END
2355 let s:li = []
2356 let s:dir_this = getcwd()
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02002357 let s:dir_foo = s:dir_this . '/Xfoo'
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01002358 call mkdir(s:dir_foo)
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02002359 let s:dir_bar = s:dir_this . '/Xbar'
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01002360 call mkdir(s:dir_bar)
Bram Moolenaarb7407d32018-02-03 17:36:27 +01002361endfunc
2362
2363function s:After_test_dirchanged()
Bram Moolenaar3503d7c2019-11-09 20:10:17 +01002364 call chdir(s:dir_this)
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01002365 call delete(s:dir_foo, 'd')
2366 call delete(s:dir_bar, 'd')
Bram Moolenaarb7407d32018-02-03 17:36:27 +01002367 augroup test_dirchanged
2368 autocmd!
2369 augroup END
2370endfunc
2371
2372function Test_dirchanged_global()
2373 call s:Before_test_dirchanged()
Bram Moolenaarf6246f52022-02-11 16:30:12 +00002374 autocmd test_dirchanged DirChangedPre global call add(s:li, expand("<amatch>") .. " pre cd " .. v:event.directory)
Bram Moolenaarb7407d32018-02-03 17:36:27 +01002375 autocmd test_dirchanged DirChanged global call add(s:li, "cd:")
2376 autocmd test_dirchanged DirChanged global call add(s:li, expand("<afile>"))
Bram Moolenaar3503d7c2019-11-09 20:10:17 +01002377 call chdir(s:dir_foo)
Bram Moolenaarf6246f52022-02-11 16:30:12 +00002378 let expected = ["global pre cd " .. s:dir_foo, "cd:", s:dir_foo]
Bram Moolenaar28e8f732022-02-09 12:58:20 +00002379 call assert_equal(expected, s:li)
Bram Moolenaar3503d7c2019-11-09 20:10:17 +01002380 call chdir(s:dir_foo)
Bram Moolenaar28e8f732022-02-09 12:58:20 +00002381 call assert_equal(expected, s:li)
Bram Moolenaar3503d7c2019-11-09 20:10:17 +01002382 exe 'lcd ' .. fnameescape(s:dir_bar)
Bram Moolenaar28e8f732022-02-09 12:58:20 +00002383 call assert_equal(expected, s:li)
Bram Moolenaard8c9d322022-06-12 11:49:16 +01002384
2385 exe 'cd ' .. s:dir_foo
2386 exe 'cd ' .. s:dir_bar
2387 autocmd! test_dirchanged DirChanged global let g:result = expand("<afile>")
2388 cd -
Bram Moolenaardb77c492022-06-12 23:26:50 +01002389 call assert_equal(s:dir_foo, substitute(g:result, '\\', '/', 'g'))
Bram Moolenaard8c9d322022-06-12 11:49:16 +01002390
Bram Moolenaarb7407d32018-02-03 17:36:27 +01002391 call s:After_test_dirchanged()
2392endfunc
2393
2394function Test_dirchanged_local()
2395 call s:Before_test_dirchanged()
2396 autocmd test_dirchanged DirChanged window call add(s:li, "lcd:")
2397 autocmd test_dirchanged DirChanged window call add(s:li, expand("<afile>"))
Bram Moolenaar3503d7c2019-11-09 20:10:17 +01002398 call chdir(s:dir_foo)
Bram Moolenaarb7407d32018-02-03 17:36:27 +01002399 call assert_equal([], s:li)
Bram Moolenaar3503d7c2019-11-09 20:10:17 +01002400 exe 'lcd ' .. fnameescape(s:dir_bar)
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01002401 call assert_equal(["lcd:", s:dir_bar], s:li)
Bram Moolenaar3503d7c2019-11-09 20:10:17 +01002402 exe 'lcd ' .. fnameescape(s:dir_bar)
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01002403 call assert_equal(["lcd:", s:dir_bar], s:li)
Bram Moolenaarb7407d32018-02-03 17:36:27 +01002404 call s:After_test_dirchanged()
2405endfunc
2406
2407function Test_dirchanged_auto()
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +02002408 CheckOption autochdir
Bram Moolenaarb7407d32018-02-03 17:36:27 +01002409 call s:Before_test_dirchanged()
2410 call test_autochdir()
Bram Moolenaar28e8f732022-02-09 12:58:20 +00002411 autocmd test_dirchanged DirChangedPre auto call add(s:li, "pre cd " .. v:event.directory)
Bram Moolenaarb7407d32018-02-03 17:36:27 +01002412 autocmd test_dirchanged DirChanged auto call add(s:li, "auto:")
2413 autocmd test_dirchanged DirChanged auto call add(s:li, expand("<afile>"))
2414 set acd
Bram Moolenaar3503d7c2019-11-09 20:10:17 +01002415 cd ..
Bram Moolenaarb7407d32018-02-03 17:36:27 +01002416 call assert_equal([], s:li)
Bram Moolenaar61abe7d2022-08-30 21:46:08 +01002417 exe 'edit ' . s:dir_foo . '/Xautofile'
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01002418 call assert_equal(s:dir_foo, getcwd())
Bram Moolenaar28e8f732022-02-09 12:58:20 +00002419 let expected = ["pre cd " .. s:dir_foo, "auto:", s:dir_foo]
2420 call assert_equal(expected, s:li)
Bram Moolenaarb7407d32018-02-03 17:36:27 +01002421 set noacd
2422 bwipe!
2423 call s:After_test_dirchanged()
2424endfunc
Bram Moolenaar5a093432018-02-10 18:15:19 +01002425
2426" Test TextChangedI and TextChangedP
2427func Test_ChangedP()
2428 new
2429 call setline(1, ['foo', 'bar', 'foobar'])
2430 call test_override("char_avail", 1)
2431 set complete=. completeopt=menuone
2432
2433 func! TextChangedAutocmd(char)
2434 let g:autocmd .= a:char
2435 endfunc
2436
Christian Brabandtdb3b4462021-10-16 11:58:55 +01002437 " TextChanged will not be triggered, only check that it isn't.
Bram Moolenaar5a093432018-02-10 18:15:19 +01002438 au! TextChanged <buffer> :call TextChangedAutocmd('N')
2439 au! TextChangedI <buffer> :call TextChangedAutocmd('I')
2440 au! TextChangedP <buffer> :call TextChangedAutocmd('P')
2441
2442 call cursor(3, 1)
2443 let g:autocmd = ''
2444 call feedkeys("o\<esc>", 'tnix')
2445 call assert_equal('I', g:autocmd)
2446
2447 let g:autocmd = ''
2448 call feedkeys("Sf", 'tnix')
2449 call assert_equal('II', g:autocmd)
2450
2451 let g:autocmd = ''
2452 call feedkeys("Sf\<C-N>", 'tnix')
2453 call assert_equal('IIP', g:autocmd)
2454
2455 let g:autocmd = ''
2456 call feedkeys("Sf\<C-N>\<C-N>", 'tnix')
2457 call assert_equal('IIPP', g:autocmd)
2458
2459 let g:autocmd = ''
2460 call feedkeys("Sf\<C-N>\<C-N>\<C-N>", 'tnix')
2461 call assert_equal('IIPPP', g:autocmd)
2462
2463 let g:autocmd = ''
2464 call feedkeys("Sf\<C-N>\<C-N>\<C-N>\<C-N>", 'tnix')
2465 call assert_equal('IIPPPP', g:autocmd)
2466
2467 call assert_equal(['foo', 'bar', 'foobar', 'foo'], getline(1, '$'))
2468 " TODO: how should it handle completeopt=noinsert,noselect?
2469
2470 " CleanUp
2471 call test_override("char_avail", 0)
2472 au! TextChanged
2473 au! TextChangedI
2474 au! TextChangedP
2475 delfu TextChangedAutocmd
2476 unlet! g:autocmd
2477 set complete&vim completeopt&vim
2478
2479 bw!
2480endfunc
Bram Moolenaar8c64a362018-03-23 22:39:31 +01002481
Bram Moolenaar91d2e782018-08-07 19:05:01 +02002482let g:setline_handled = v:false
Bram Moolenaar1e115362019-01-09 23:01:02 +01002483func SetLineOne()
Bram Moolenaar91d2e782018-08-07 19:05:01 +02002484 if !g:setline_handled
2485 call setline(1, "(x)")
2486 let g:setline_handled = v:true
2487 endif
2488endfunc
2489
2490func Test_TextChangedI_with_setline()
2491 new
2492 call test_override('char_avail', 1)
2493 autocmd TextChangedI <buffer> call SetLineOne()
2494 call feedkeys("i(\<CR>\<Esc>", 'tx')
2495 call assert_equal('(', getline(1))
2496 call assert_equal('x)', getline(2))
2497 undo
Bram Moolenaar91d2e782018-08-07 19:05:01 +02002498 call assert_equal('', getline(1))
Bram Moolenaar9fa95062018-08-08 22:08:32 +02002499 call assert_equal('', getline(2))
Bram Moolenaar91d2e782018-08-07 19:05:01 +02002500
Bram Moolenaarca34db32022-01-20 11:17:18 +00002501 call test_override('char_avail', 0)
Bram Moolenaar91d2e782018-08-07 19:05:01 +02002502 bwipe!
2503endfunc
2504
Bram Moolenaar8c64a362018-03-23 22:39:31 +01002505func Test_Changed_FirstTime()
Bram Moolenaar8c5a2782019-08-07 23:07:07 +02002506 CheckFeature terminal
2507 CheckNotGui
Bram Moolenaar3cdcb092020-03-18 19:18:10 +01002508 " Starting a terminal to run Vim is always considered flaky.
Bram Moolenaar30d53e22020-03-18 21:10:44 +01002509 let g:test_is_flaky = 1
Bram Moolenaar8c5a2782019-08-07 23:07:07 +02002510
Bram Moolenaar8c64a362018-03-23 22:39:31 +01002511 " Prepare file for TextChanged event.
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01002512 call writefile([''], 'Xchanged.txt', 'D')
Bram Moolenaar8c64a362018-03-23 22:39:31 +01002513 let buf = term_start([GetVimProg(), '--clean', '-c', 'set noswapfile'], {'term_rows': 3})
2514 call assert_equal('running', term_getstatus(buf))
Bram Moolenaar1834d372018-03-29 17:40:46 +02002515 " Wait for the ruler (in the status line) to be shown.
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01002516 " In ConPTY, there is additional character which is drawn up to the width of
2517 " the screen.
2518 if has('conpty')
2519 call WaitForAssert({-> assert_match('\<All.*$', term_getline(buf, 3))})
2520 else
2521 call WaitForAssert({-> assert_match('\<All$', term_getline(buf, 3))})
2522 endif
Bram Moolenaar8c64a362018-03-23 22:39:31 +01002523 " It's only adding autocmd, so that no event occurs.
2524 call term_sendkeys(buf, ":au! TextChanged <buffer> call writefile(['No'], 'Xchanged.txt')\<cr>")
2525 call term_sendkeys(buf, "\<C-\\>\<C-N>:qa!\<cr>")
Bram Moolenaar50182fa2018-04-28 21:34:40 +02002526 call WaitForAssert({-> assert_equal('finished', term_getstatus(buf))})
Bram Moolenaar8c64a362018-03-23 22:39:31 +01002527 call assert_equal([''], readfile('Xchanged.txt'))
2528
2529 " clean up
Bram Moolenaar8c64a362018-03-23 22:39:31 +01002530 bwipe!
2531endfunc
Bram Moolenaar0566e892019-01-24 19:37:40 +01002532
Bram Moolenaareb93f3f2019-04-04 15:04:56 +02002533func Test_autocmd_nested()
2534 let g:did_nested = 0
2535 augroup Testing
2536 au WinNew * edit somefile
2537 au BufNew * let g:did_nested = 1
2538 augroup END
2539 split
2540 call assert_equal(0, g:did_nested)
2541 close
2542 bwipe! somefile
2543
2544 " old nested argument still works
2545 augroup Testing
2546 au!
2547 au WinNew * nested edit somefile
2548 au BufNew * let g:did_nested = 1
2549 augroup END
2550 split
2551 call assert_equal(1, g:did_nested)
2552 close
2553 bwipe! somefile
2554
2555 " New ++nested argument works
2556 augroup Testing
2557 au!
2558 au WinNew * ++nested edit somefile
2559 au BufNew * let g:did_nested = 1
2560 augroup END
2561 split
2562 call assert_equal(1, g:did_nested)
2563 close
2564 bwipe! somefile
2565
Bram Moolenaarf0775142022-03-04 20:10:38 +00002566 " nested without ++ does not work in Vim9 script
2567 call assert_fails('vim9cmd au WinNew * nested echo fails', 'E1078:')
2568
Bram Moolenaareb93f3f2019-04-04 15:04:56 +02002569 augroup Testing
2570 au!
2571 augroup END
2572
2573 call assert_fails('au WinNew * ++nested ++nested echo bad', 'E983:')
2574 call assert_fails('au WinNew * nested nested echo bad', 'E983:')
2575endfunc
2576
Bram Moolenaar5fa9f232022-07-23 09:06:48 +01002577func Test_autocmd_nested_cursor_invalid()
2578 set laststatus=0
2579 copen
2580 cclose
2581 call setline(1, ['foo', 'bar', 'baz'])
2582 3
2583 augroup nested_inv
2584 autocmd User foo ++nested copen
2585 autocmd BufAdd * let &laststatus = 2 - &laststatus
2586 augroup END
2587 doautocmd User foo
2588
2589 augroup nested_inv
2590 au!
2591 augroup END
2592 set laststatus&
Bram Moolenaarb03950f2022-07-26 13:47:13 +01002593 cclose
Bram Moolenaar5fa9f232022-07-23 09:06:48 +01002594 bwipe!
2595endfunc
2596
Bram Moolenaar3d6ee8b2022-07-27 15:23:35 +01002597func Test_autocmd_nested_keeps_cursor_pos()
2598 enew
2599 call setline(1, 'foo')
2600 autocmd User foo ++nested normal! $a
2601 autocmd InsertLeave * :
2602 doautocmd User foo
2603 call assert_equal([0, 1, 3, 0], getpos('.'))
2604
2605 bwipe!
2606endfunc
2607
Bram Moolenaarb03950f2022-07-26 13:47:13 +01002608func Test_autocmd_nested_switch_window()
2609 " run this in a separate Vim so that SafeState works
2610 CheckRunVimInTerminal
2611
2612 let lines =<< trim END
2613 vim9script
2614 ['()']->writefile('Xautofile')
2615 autocmd VimEnter * ++nested edit Xautofile | split
2616 autocmd BufReadPost * autocmd SafeState * ++once foldclosed('.')
2617 autocmd WinEnter * matchadd('ErrorMsg', 'pat')
2618 END
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01002619 call writefile(lines, 'Xautoscript', 'D')
Bram Moolenaarb03950f2022-07-26 13:47:13 +01002620 let buf = RunVimInTerminal('-S Xautoscript', {'rows': 10})
2621 call VerifyScreenDump(buf, 'Test_autocmd_nested_switch', {})
2622
2623 call StopVimInTerminal(buf)
2624 call delete('Xautofile')
Bram Moolenaarb03950f2022-07-26 13:47:13 +01002625endfunc
2626
Bram Moolenaareb93f3f2019-04-04 15:04:56 +02002627func Test_autocmd_once()
2628 " Without ++once WinNew triggers twice
2629 let g:did_split = 0
2630 augroup Testing
2631 au WinNew * let g:did_split += 1
2632 augroup END
2633 split
2634 split
2635 call assert_equal(2, g:did_split)
2636 call assert_true(exists('#WinNew'))
2637 close
2638 close
2639
2640 " With ++once WinNew triggers once
2641 let g:did_split = 0
2642 augroup Testing
2643 au!
2644 au WinNew * ++once let g:did_split += 1
2645 augroup END
2646 split
2647 split
2648 call assert_equal(1, g:did_split)
2649 call assert_false(exists('#WinNew'))
2650 close
2651 close
2652
2653 call assert_fails('au WinNew * ++once ++once echo bad', 'E983:')
2654endfunc
2655
Bram Moolenaara68e5952019-04-25 22:22:01 +02002656func Test_autocmd_bufreadpre()
2657 new
2658 let b:bufreadpre = 1
Bram Moolenaarab505b12020-03-23 19:28:44 +01002659 call append(0, range(1000))
Bram Moolenaara68e5952019-04-25 22:22:01 +02002660 w! XAutocmdBufReadPre.txt
2661 autocmd BufReadPre <buffer> :let b:bufreadpre += 1
Bram Moolenaarab505b12020-03-23 19:28:44 +01002662 norm! 500gg
Bram Moolenaara68e5952019-04-25 22:22:01 +02002663 sp
Bram Moolenaarab505b12020-03-23 19:28:44 +01002664 norm! 1000gg
Bram Moolenaara68e5952019-04-25 22:22:01 +02002665 wincmd p
2666 let g:wsv1 = winsaveview()
2667 wincmd p
2668 let g:wsv2 = winsaveview()
2669 " triggers BufReadPre, should not move the cursor in either window
2670 " The topline may change one line in a large window.
2671 edit
2672 call assert_inrange(g:wsv2.topline - 1, g:wsv2.topline + 1, winsaveview().topline)
2673 call assert_equal(g:wsv2.lnum, winsaveview().lnum)
2674 call assert_equal(2, b:bufreadpre)
2675 wincmd p
2676 call assert_equal(g:wsv1.topline, winsaveview().topline)
2677 call assert_equal(g:wsv1.lnum, winsaveview().lnum)
2678 call assert_equal(2, b:bufreadpre)
2679 " Now set the cursor position in an BufReadPre autocommand
2680 " (even though the position will be invalid, this should make Vim reset the
2681 " cursor position in the other window.
2682 wincmd p
2683 set cpo+=g
2684 " won't do anything, but try to set the cursor on an invalid lnum
2685 autocmd BufReadPre <buffer> :norm! 70gg
2686 " triggers BufReadPre, should not move the cursor in either window
2687 e
2688 call assert_equal(1, winsaveview().topline)
2689 call assert_equal(1, winsaveview().lnum)
2690 call assert_equal(3, b:bufreadpre)
2691 wincmd p
2692 call assert_equal(g:wsv1.topline, winsaveview().topline)
2693 call assert_equal(g:wsv1.lnum, winsaveview().lnum)
2694 call assert_equal(3, b:bufreadpre)
2695 close
2696 close
2697 call delete('XAutocmdBufReadPre.txt')
2698 set cpo-=g
2699endfunc
2700
Bram Moolenaar5e66b422019-01-24 21:58:10 +01002701" FileChangedShell tested in test_filechanged.vim
Bram Moolenaar69ea5872019-04-25 20:29:00 +02002702
2703" Tests for the following autocommands:
2704" - FileWritePre writing a compressed file
2705" - FileReadPost reading a compressed file
2706" - BufNewFile reading a file template
2707" - BufReadPre decompressing the file to be read
2708" - FilterReadPre substituting characters in the temp file
2709" - FilterReadPost substituting characters after filtering
2710" - FileReadPre set options for decompression
2711" - FileReadPost decompress the file
2712func Test_ReadWrite_Autocmds()
2713 " Run this test only on Unix-like systems and if gzip is available
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +02002714 CheckUnix
2715 CheckExecutable gzip
Bram Moolenaar69ea5872019-04-25 20:29:00 +02002716
2717 " Make $GZIP empty, "-v" would cause trouble.
2718 let $GZIP = ""
2719
2720 " Use a FileChangedShell autocommand to avoid a prompt for 'Xtestfile.gz'
2721 " being modified outside of Vim (noticed on Solaris).
2722 au FileChangedShell * echo 'caught FileChangedShell'
2723
2724 " Test for the FileReadPost, FileWritePre and FileWritePost autocmds
2725 augroup Test1
2726 au!
2727 au FileWritePre *.gz '[,']!gzip
2728 au FileWritePost *.gz undo
2729 au FileReadPost *.gz '[,']!gzip -d
2730 augroup END
2731
2732 new
2733 set bin
2734 call append(0, [
2735 \ 'line 2 Abcdefghijklmnopqrstuvwxyz',
2736 \ 'line 3 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2737 \ 'line 4 Abcdefghijklmnopqrstuvwxyz',
2738 \ 'line 5 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2739 \ 'line 6 Abcdefghijklmnopqrstuvwxyz',
2740 \ 'line 7 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2741 \ 'line 8 Abcdefghijklmnopqrstuvwxyz',
2742 \ 'line 9 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2743 \ 'line 10 Abcdefghijklmnopqrstuvwxyz'
2744 \ ])
2745 1,9write! Xtestfile.gz
2746 enew! | close
2747
2748 new
2749 " Read and decompress the testfile
2750 0read Xtestfile.gz
2751 call assert_equal([
2752 \ 'line 2 Abcdefghijklmnopqrstuvwxyz',
2753 \ 'line 3 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2754 \ 'line 4 Abcdefghijklmnopqrstuvwxyz',
2755 \ 'line 5 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2756 \ 'line 6 Abcdefghijklmnopqrstuvwxyz',
2757 \ 'line 7 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2758 \ 'line 8 Abcdefghijklmnopqrstuvwxyz',
2759 \ 'line 9 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2760 \ 'line 10 Abcdefghijklmnopqrstuvwxyz'
2761 \ ], getline(1, 9))
2762 enew! | close
2763
2764 augroup Test1
2765 au!
2766 augroup END
2767
2768 " Test for the FileAppendPre and FileAppendPost autocmds
2769 augroup Test2
2770 au!
2771 au BufNewFile *.c read Xtest.c
2772 au FileAppendPre *.out '[,']s/new/NEW/
2773 au FileAppendPost *.out !cat Xtest.c >> test.out
2774 augroup END
2775
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01002776 call writefile(['/*', ' * Here is a new .c file', ' */'], 'Xtest.c', 'D')
Bram Moolenaar69ea5872019-04-25 20:29:00 +02002777 new foo.c " should load Xtest.c
2778 call assert_equal(['/*', ' * Here is a new .c file', ' */'], getline(2, 4))
2779 w! >> test.out " append it to the output file
2780
2781 let contents = readfile('test.out')
2782 call assert_equal(' * Here is a NEW .c file', contents[2])
2783 call assert_equal(' * Here is a new .c file', contents[5])
2784
2785 call delete('test.out')
2786 enew! | close
2787 augroup Test2
2788 au!
2789 augroup END
2790
2791 " Test for the BufReadPre and BufReadPost autocmds
2792 augroup Test3
2793 au!
2794 " setup autocommands to decompress before reading and re-compress
2795 " afterwards
2796 au BufReadPre *.gz exe '!gzip -d ' . shellescape(expand("<afile>"))
2797 au BufReadPre *.gz call rename(expand("<afile>:r"), expand("<afile>"))
2798 au BufReadPost *.gz call rename(expand("<afile>"), expand("<afile>:r"))
2799 au BufReadPost *.gz exe '!gzip ' . shellescape(expand("<afile>:r"))
2800 augroup END
2801
2802 e! Xtestfile.gz " Edit compressed file
2803 call assert_equal([
2804 \ 'line 2 Abcdefghijklmnopqrstuvwxyz',
2805 \ 'line 3 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2806 \ 'line 4 Abcdefghijklmnopqrstuvwxyz',
2807 \ 'line 5 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2808 \ 'line 6 Abcdefghijklmnopqrstuvwxyz',
2809 \ 'line 7 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2810 \ 'line 8 Abcdefghijklmnopqrstuvwxyz',
2811 \ 'line 9 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2812 \ 'line 10 Abcdefghijklmnopqrstuvwxyz'
2813 \ ], getline(1, 9))
2814
2815 w! >> test.out " Append it to the output file
2816
2817 augroup Test3
2818 au!
2819 augroup END
2820
2821 " Test for the FilterReadPre and FilterReadPost autocmds.
2822 set shelltemp " need temp files here
2823 augroup Test4
2824 au!
2825 au FilterReadPre *.out call rename(expand("<afile>"), expand("<afile>") . ".t")
2826 au FilterReadPre *.out exe 'silent !sed s/e/E/ ' . shellescape(expand("<afile>")) . ".t >" . shellescape(expand("<afile>"))
2827 au FilterReadPre *.out exe 'silent !rm ' . shellescape(expand("<afile>")) . '.t'
2828 au FilterReadPost *.out '[,']s/x/X/g
2829 augroup END
2830
2831 e! test.out " Edit the output file
2832 1,$!cat
2833 call assert_equal([
2834 \ 'linE 2 AbcdefghijklmnopqrstuvwXyz',
2835 \ 'linE 3 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
2836 \ 'linE 4 AbcdefghijklmnopqrstuvwXyz',
2837 \ 'linE 5 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
2838 \ 'linE 6 AbcdefghijklmnopqrstuvwXyz',
2839 \ 'linE 7 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
2840 \ 'linE 8 AbcdefghijklmnopqrstuvwXyz',
2841 \ 'linE 9 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
2842 \ 'linE 10 AbcdefghijklmnopqrstuvwXyz'
2843 \ ], getline(1, 9))
2844 call assert_equal([
2845 \ 'line 2 Abcdefghijklmnopqrstuvwxyz',
2846 \ 'line 3 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2847 \ 'line 4 Abcdefghijklmnopqrstuvwxyz',
2848 \ 'line 5 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2849 \ 'line 6 Abcdefghijklmnopqrstuvwxyz',
2850 \ 'line 7 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2851 \ 'line 8 Abcdefghijklmnopqrstuvwxyz',
2852 \ 'line 9 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2853 \ 'line 10 Abcdefghijklmnopqrstuvwxyz'
2854 \ ], readfile('test.out'))
2855
2856 augroup Test4
2857 au!
2858 augroup END
2859 set shelltemp&vim
2860
2861 " Test for the FileReadPre and FileReadPost autocmds.
2862 augroup Test5
2863 au!
2864 au FileReadPre *.gz exe 'silent !gzip -d ' . shellescape(expand("<afile>"))
2865 au FileReadPre *.gz call rename(expand("<afile>:r"), expand("<afile>"))
2866 au FileReadPost *.gz '[,']s/l/L/
2867 augroup END
2868
2869 new
2870 0r Xtestfile.gz " Read compressed file
2871 call assert_equal([
2872 \ 'Line 2 Abcdefghijklmnopqrstuvwxyz',
2873 \ 'Line 3 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2874 \ 'Line 4 Abcdefghijklmnopqrstuvwxyz',
2875 \ 'Line 5 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2876 \ 'Line 6 Abcdefghijklmnopqrstuvwxyz',
2877 \ 'Line 7 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2878 \ 'Line 8 Abcdefghijklmnopqrstuvwxyz',
2879 \ 'Line 9 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2880 \ 'Line 10 Abcdefghijklmnopqrstuvwxyz'
2881 \ ], getline(1, 9))
2882 call assert_equal([
2883 \ 'line 2 Abcdefghijklmnopqrstuvwxyz',
2884 \ 'line 3 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2885 \ 'line 4 Abcdefghijklmnopqrstuvwxyz',
2886 \ 'line 5 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2887 \ 'line 6 Abcdefghijklmnopqrstuvwxyz',
2888 \ 'line 7 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2889 \ 'line 8 Abcdefghijklmnopqrstuvwxyz',
2890 \ 'line 9 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2891 \ 'line 10 Abcdefghijklmnopqrstuvwxyz'
2892 \ ], readfile('Xtestfile.gz'))
2893
2894 augroup Test5
2895 au!
2896 augroup END
2897
2898 au! FileChangedShell
2899 call delete('Xtestfile.gz')
Bram Moolenaar69ea5872019-04-25 20:29:00 +02002900 call delete('test.out')
2901endfunc
Bram Moolenaar23b51392019-05-09 21:38:43 +02002902
2903func Test_throw_in_BufWritePre()
2904 new
2905 call setline(1, ['one', 'two', 'three'])
2906 call assert_false(filereadable('Xthefile'))
2907 augroup throwing
2908 au BufWritePre X* throw 'do not write'
2909 augroup END
2910 try
2911 w Xthefile
2912 catch
2913 let caught = 1
2914 endtry
2915 call assert_equal(1, caught)
2916 call assert_false(filereadable('Xthefile'))
2917
2918 bwipe!
2919 au! throwing
2920endfunc
Bram Moolenaarcadbe1b2019-09-22 21:50:09 +02002921
Bram Moolenaar40fa12a2021-09-22 14:18:13 +02002922func Test_autocmd_in_try_block()
Bram Moolenaar6f14da12022-09-07 21:30:44 +01002923 call mkdir('Xintrydir', 'R')
Bram Moolenaar40fa12a2021-09-22 14:18:13 +02002924 au BufEnter * let g:fname = expand('%')
2925 try
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +01002926 edit Xintrydir/
Bram Moolenaar40fa12a2021-09-22 14:18:13 +02002927 endtry
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +01002928 call assert_match('Xintrydir', g:fname)
Bram Moolenaar40fa12a2021-09-22 14:18:13 +02002929
2930 unlet g:fname
2931 au! BufEnter
Bram Moolenaar40fa12a2021-09-22 14:18:13 +02002932endfunc
2933
Bram Moolenaarcadbe1b2019-09-22 21:50:09 +02002934func Test_autocmd_SafeState()
2935 CheckRunVimInTerminal
2936
2937 let lines =<< trim END
2938 let g:safe = 0
2939 let g:again = ''
2940 au SafeState * let g:safe += 1
2941 au SafeStateAgain * let g:again ..= 'x'
2942 func CallTimer()
2943 call timer_start(10, {id -> execute('let g:again ..= "t"')})
2944 endfunc
2945 END
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01002946 call writefile(lines, 'XSafeState', 'D')
Bram Moolenaarcadbe1b2019-09-22 21:50:09 +02002947 let buf = RunVimInTerminal('-S XSafeState', #{rows: 6})
2948
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01002949 " Sometimes we loop to handle a K_IGNORE, SafeState may be triggered once or
Bram Moolenaar8fb1b472020-02-23 16:16:26 +01002950 " more often.
Bram Moolenaarcadbe1b2019-09-22 21:50:09 +02002951 call term_sendkeys(buf, ":echo g:safe\<CR>")
Bram Moolenaar8fb1b472020-02-23 16:16:26 +01002952 call WaitForAssert({-> assert_match('^\d ', term_getline(buf, 6))}, 1000)
Bram Moolenaarcadbe1b2019-09-22 21:50:09 +02002953
Bram Moolenaar8fb1b472020-02-23 16:16:26 +01002954 " SafeStateAgain should be invoked at least three times
Bram Moolenaarcadbe1b2019-09-22 21:50:09 +02002955 call term_sendkeys(buf, ":echo g:again\<CR>")
Bram Moolenaar8fb1b472020-02-23 16:16:26 +01002956 call WaitForAssert({-> assert_match('^xxx', term_getline(buf, 6))}, 1000)
Bram Moolenaarcadbe1b2019-09-22 21:50:09 +02002957
2958 call term_sendkeys(buf, ":let g:again = ''\<CR>:call CallTimer()\<CR>")
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02002959 call TermWait(buf, 50)
Bram Moolenaar0f6629a2019-09-22 23:24:13 +02002960 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02002961 call TermWait(buf, 50)
Bram Moolenaarcadbe1b2019-09-22 21:50:09 +02002962 call term_sendkeys(buf, ":echo g:again\<CR>")
2963 call WaitForAssert({-> assert_match('xtx', term_getline(buf, 6))}, 1000)
2964
2965 call StopVimInTerminal(buf)
Bram Moolenaarcadbe1b2019-09-22 21:50:09 +02002966endfunc
Bram Moolenaar23324a02019-10-01 17:39:04 +02002967
2968func Test_autocmd_CmdWinEnter()
2969 CheckRunVimInTerminal
Bram Moolenaar21829c52021-01-26 22:42:21 +01002970
Bram Moolenaar23324a02019-10-01 17:39:04 +02002971 let lines =<< trim END
Egor Zvorykin125ffd22021-11-17 14:01:14 +00002972 augroup vimHints | au! | augroup END
Bram Moolenaar23324a02019-10-01 17:39:04 +02002973 let b:dummy_var = 'This is a dummy'
2974 autocmd CmdWinEnter * quit
2975 let winnr = winnr('$')
2976 END
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01002977 let filename = 'XCmdWinEnter'
Bram Moolenaar23324a02019-10-01 17:39:04 +02002978 call writefile(lines, filename)
2979 let buf = RunVimInTerminal('-S '.filename, #{rows: 6})
2980
2981 call term_sendkeys(buf, "q:")
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02002982 call TermWait(buf)
Bram Moolenaar23324a02019-10-01 17:39:04 +02002983 call term_sendkeys(buf, ":echo b:dummy_var\<cr>")
Bram Moolenaar353c3512020-03-15 14:19:26 +01002984 call WaitForAssert({-> assert_match('^This is a dummy', term_getline(buf, 6))}, 2000)
Bram Moolenaar23324a02019-10-01 17:39:04 +02002985 call term_sendkeys(buf, ":echo &buftype\<cr>")
2986 call WaitForAssert({-> assert_notmatch('^nofile', term_getline(buf, 6))}, 1000)
2987 call term_sendkeys(buf, ":echo winnr\<cr>")
2988 call WaitForAssert({-> assert_match('^1', term_getline(buf, 6))}, 1000)
2989
2990 " clean up
2991 call StopVimInTerminal(buf)
2992 call delete(filename)
2993endfunc
Bram Moolenaarec66c412019-10-11 21:19:13 +02002994
2995func Test_autocmd_was_using_freed_memory()
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01002996 CheckFeature quickfix
2997
Bram Moolenaarec66c412019-10-11 21:19:13 +02002998 pedit xx
2999 n x
Bram Moolenaar92bb83e2021-02-03 23:04:46 +01003000 augroup winenter
3001 au WinEnter * if winnr('$') > 2 | quit | endif
3002 augroup END
Bram Moolenaarec66c412019-10-11 21:19:13 +02003003 split
Bram Moolenaar92bb83e2021-02-03 23:04:46 +01003004
3005 augroup winenter
3006 au! WinEnter
3007 augroup END
3008
3009 bwipe xx
3010 bwipe x
3011 pclose
Bram Moolenaarec66c412019-10-11 21:19:13 +02003012endfunc
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +01003013
3014func Test_BufWrite_lockmarks()
Bram Moolenaarf08b0eb2021-10-16 13:00:14 +01003015 let g:test_is_flaky = 1
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +01003016 edit! Xtest
3017 call setline(1, ['a', 'b', 'c', 'd'])
3018
3019 " :lockmarks preserves the marks
3020 call SetChangeMarks(2, 3)
3021 lockmarks write
3022 call assert_equal([2, 3], [line("'["), line("']")])
3023
3024 " *WritePre autocmds get the correct line range, but lockmarks preserves the
3025 " original values for the user
3026 augroup lockmarks
3027 au!
3028 au BufWritePre,FilterWritePre * call assert_equal([1, 4], [line("'["), line("']")])
3029 au FileWritePre * call assert_equal([3, 4], [line("'["), line("']")])
3030 augroup END
3031
3032 lockmarks write
3033 call assert_equal([2, 3], [line("'["), line("']")])
3034
3035 if executable('cat')
3036 lockmarks %!cat
3037 call assert_equal([2, 3], [line("'["), line("']")])
3038 endif
3039
3040 lockmarks 3,4write Xtest2
3041 call assert_equal([2, 3], [line("'["), line("']")])
3042
3043 au! lockmarks
3044 augroup! lockmarks
3045 call delete('Xtest')
3046 call delete('Xtest2')
3047endfunc
Bram Moolenaarce6db022020-01-07 20:11:42 +01003048
3049func Test_FileType_spell()
3050 if !isdirectory('/tmp')
3051 throw "Skipped: requires /tmp directory"
3052 endif
3053
3054 " this was crashing with an invalid free()
3055 setglobal spellfile=/tmp/en.utf-8.add
3056 augroup crash
3057 autocmd!
3058 autocmd BufNewFile,BufReadPost crashfile setf somefiletype
3059 autocmd BufNewFile,BufReadPost crashfile set ft=anotherfiletype
3060 autocmd FileType anotherfiletype setlocal spell
3061 augroup END
3062 func! NoCrash() abort
3063 edit /tmp/crashfile
3064 endfunc
3065 call NoCrash()
3066
3067 au! crash
3068 setglobal spellfile=
3069endfunc
Bram Moolenaarbc2b71d2020-02-17 21:33:30 +01003070
Bram Moolenaaref976322022-09-28 11:48:30 +01003071" this was wiping out the current buffer and using freed memory
3072func Test_SpellFileMissing_bwipe()
3073 next 0
3074 au SpellFileMissing 0 bwipe
3075 call assert_fails('set spell spelllang=0', 'E937:')
3076
3077 au! SpellFileMissing
Bram Moolenaar0a60f792022-11-19 21:18:11 +00003078 set nospell spelllang=en
Bram Moolenaaref976322022-09-28 11:48:30 +01003079 bwipe
3080endfunc
3081
Bram Moolenaar406cd902020-02-18 21:54:41 +01003082" Test closing a window or editing another buffer from a FileChangedRO handler
3083" in a readonly buffer
3084func Test_FileChangedRO_winclose()
Bram Moolenaar62cd26a2020-10-11 20:08:44 +02003085 call test_override('ui_delay', 10)
3086
Bram Moolenaar406cd902020-02-18 21:54:41 +01003087 augroup FileChangedROTest
3088 au!
3089 autocmd FileChangedRO * quit
3090 augroup END
3091 new
3092 set readonly
3093 call assert_fails('normal i', 'E788:')
3094 close
3095 augroup! FileChangedROTest
3096
3097 augroup FileChangedROTest
3098 au!
Bram Moolenaar61abe7d2022-08-30 21:46:08 +01003099 autocmd FileChangedRO * edit Xrofile
Bram Moolenaar406cd902020-02-18 21:54:41 +01003100 augroup END
3101 new
3102 set readonly
3103 call assert_fails('normal i', 'E788:')
3104 close
3105 augroup! FileChangedROTest
Bram Moolenaar62cd26a2020-10-11 20:08:44 +02003106 call test_override('ALL', 0)
Bram Moolenaar406cd902020-02-18 21:54:41 +01003107endfunc
3108
Bram Moolenaar0c81d1b2020-02-22 22:45:55 +01003109func LogACmd()
3110 call add(g:logged, line('$'))
3111endfunc
3112
3113func Test_TermChanged()
Bram Moolenaard28e0b32020-02-22 23:08:52 +01003114 CheckNotGui
3115
Bram Moolenaar0c81d1b2020-02-22 22:45:55 +01003116 enew!
3117 tabnew
3118 call setline(1, ['a', 'b', 'c', 'd'])
3119 $
3120 au TermChanged * call LogACmd()
3121 let g:logged = []
3122 let term_save = &term
3123 set term=xterm
3124 call assert_equal([1, 4], g:logged)
3125
3126 au! TermChanged
3127 let &term = term_save
3128 bwipe!
3129endfunc
3130
Bram Moolenaare3284872020-03-19 13:55:03 +01003131" Test for FileReadCmd autocmd
3132func Test_autocmd_FileReadCmd()
3133 func ReadFileCmd()
3134 call append(line('$'), "v:cmdarg = " .. v:cmdarg)
3135 endfunc
3136 augroup FileReadCmdTest
3137 au!
3138 au FileReadCmd Xtest call ReadFileCmd()
3139 augroup END
3140
3141 new
3142 read ++bin Xtest
3143 read ++nobin Xtest
3144 read ++edit Xtest
3145 read ++bad=keep Xtest
3146 read ++bad=drop Xtest
3147 read ++bad=- Xtest
3148 read ++ff=unix Xtest
3149 read ++ff=dos Xtest
3150 read ++ff=mac Xtest
3151 read ++enc=utf-8 Xtest
3152
3153 call assert_equal(['',
3154 \ 'v:cmdarg = ++bin',
3155 \ 'v:cmdarg = ++nobin',
3156 \ 'v:cmdarg = ++edit',
3157 \ 'v:cmdarg = ++bad=keep',
3158 \ 'v:cmdarg = ++bad=drop',
3159 \ 'v:cmdarg = ++bad=-',
3160 \ 'v:cmdarg = ++ff=unix',
3161 \ 'v:cmdarg = ++ff=dos',
3162 \ 'v:cmdarg = ++ff=mac',
3163 \ 'v:cmdarg = ++enc=utf-8'], getline(1, '$'))
3164
3165 close!
3166 augroup FileReadCmdTest
3167 au!
3168 augroup END
3169 delfunc ReadFileCmd
3170endfunc
3171
Bram Moolenaaree4e0c12020-04-06 21:35:05 +02003172" Test for passing invalid arguments to autocmd
3173func Test_autocmd_invalid_args()
3174 " Additional character after * for event
Bram Moolenaar61abe7d2022-08-30 21:46:08 +01003175 call assert_fails('autocmd *a Xinvfile set ff=unix', 'E215:')
Bram Moolenaaree4e0c12020-04-06 21:35:05 +02003176 augroup Test
3177 augroup END
3178 " Invalid autocmd event
Bram Moolenaar61abe7d2022-08-30 21:46:08 +01003179 call assert_fails('autocmd Bufabc Xinvfile set ft=vim', 'E216:')
Bram Moolenaaree4e0c12020-04-06 21:35:05 +02003180 " Invalid autocmd event in a autocmd group
Bram Moolenaar61abe7d2022-08-30 21:46:08 +01003181 call assert_fails('autocmd Test Bufabc Xinvfile set ft=vim', 'E216:')
Bram Moolenaaree4e0c12020-04-06 21:35:05 +02003182 augroup! Test
3183 " Execute all autocmds
3184 call assert_fails('doautocmd * BufEnter', 'E217:')
3185 call assert_fails('augroup! x1a2b3', 'E367:')
3186 call assert_fails('autocmd BufNew <buffer=999> pwd', 'E680:')
Bram Moolenaar531be472020-09-23 22:38:05 +02003187 call assert_fails('autocmd BufNew \) set ff=unix', 'E55:')
Bram Moolenaaree4e0c12020-04-06 21:35:05 +02003188endfunc
3189
3190" Test for deep nesting of autocmds
3191func Test_autocmd_deep_nesting()
Bram Moolenaar61abe7d2022-08-30 21:46:08 +01003192 autocmd BufEnter Xdeepfile doautocmd BufEnter Xdeepfile
3193 call assert_fails('doautocmd BufEnter Xdeepfile', 'E218:')
3194 autocmd! BufEnter Xdeepfile
Bram Moolenaaree4e0c12020-04-06 21:35:05 +02003195endfunc
3196
Bram Moolenaarbe5ee862020-06-10 20:56:58 +02003197" Tests for SigUSR1 autocmd event, which is only available on posix systems.
3198func Test_autocmd_sigusr1()
3199 CheckUnix
Bram Moolenaar0056ca72022-09-23 21:26:39 +01003200 " FIXME: should this work on MacOS M1?
3201 CheckNotMacM1
Bram Moolenaar62cd26a2020-10-11 20:08:44 +02003202 CheckExecutable /bin/kill
Bram Moolenaarbe5ee862020-06-10 20:56:58 +02003203
3204 let g:sigusr1_passed = 0
3205 au SigUSR1 * let g:sigusr1_passed = 1
3206 call system('/bin/kill -s usr1 ' . getpid())
3207 call WaitForAssert({-> assert_true(g:sigusr1_passed)})
3208
3209 au! SigUSR1
3210 unlet g:sigusr1_passed
3211endfunc
3212
Bram Moolenaarb340bae2020-06-15 19:51:56 +02003213" Test for BufReadPre autocmd deleting the file
3214func Test_BufReadPre_delfile()
3215 augroup TestAuCmd
3216 au!
Bram Moolenaare7cda972022-08-29 11:02:59 +01003217 autocmd BufReadPre XbufreadPre call delete('XbufreadPre')
Bram Moolenaarb340bae2020-06-15 19:51:56 +02003218 augroup END
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01003219 call writefile([], 'XbufreadPre', 'D')
Bram Moolenaare7cda972022-08-29 11:02:59 +01003220 call assert_fails('new XbufreadPre', 'E200:')
3221 call assert_equal('XbufreadPre', @%)
Bram Moolenaarb340bae2020-06-15 19:51:56 +02003222 call assert_equal(1, &readonly)
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01003223
Bram Moolenaarb340bae2020-06-15 19:51:56 +02003224 augroup TestAuCmd
3225 au!
3226 augroup END
3227 close!
3228endfunc
3229
3230" Test for BufReadPre autocmd changing the current buffer
3231func Test_BufReadPre_changebuf()
3232 augroup TestAuCmd
3233 au!
Bram Moolenaare7cda972022-08-29 11:02:59 +01003234 autocmd BufReadPre Xchangebuf edit Xsomeotherfile
Bram Moolenaarb340bae2020-06-15 19:51:56 +02003235 augroup END
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01003236 call writefile([], 'Xchangebuf', 'D')
Bram Moolenaare7cda972022-08-29 11:02:59 +01003237 call assert_fails('new Xchangebuf', 'E201:')
Bram Moolenaarb340bae2020-06-15 19:51:56 +02003238 call assert_equal('Xsomeotherfile', @%)
3239 call assert_equal(1, &readonly)
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01003240
Bram Moolenaarb340bae2020-06-15 19:51:56 +02003241 augroup TestAuCmd
3242 au!
3243 augroup END
3244 close!
3245endfunc
3246
3247" Test for BufWipeouti autocmd changing the current buffer when reading a file
3248" in an empty buffer with 'f' flag in 'cpo'
3249func Test_BufDelete_changebuf()
3250 new
3251 augroup TestAuCmd
3252 au!
3253 autocmd BufWipeout * let bufnr = bufadd('somefile') | exe "b " .. bufnr
3254 augroup END
3255 let save_cpo = &cpo
3256 set cpo+=f
Bram Moolenaarb18b4962022-09-02 21:55:50 +01003257 call assert_fails('r Xchangebuf', ['E812:', 'E484:'])
Bram Moolenaarb340bae2020-06-15 19:51:56 +02003258 call assert_equal('somefile', @%)
3259 let &cpo = save_cpo
3260 augroup TestAuCmd
3261 au!
3262 augroup END
3263 close!
3264endfunc
3265
Bram Moolenaar0fe937f2020-06-16 22:42:04 +02003266" Test for the temporary internal window used to execute autocmds
3267func Test_autocmd_window()
3268 %bw!
3269 edit one.txt
3270 tabnew two.txt
Bram Moolenaar41cd8032021-03-13 15:47:56 +01003271 vnew three.txt
3272 tabnew four.txt
3273 tabprevious
Bram Moolenaar0fe937f2020-06-16 22:42:04 +02003274 let g:blist = []
Bram Moolenaar832adf92020-06-25 19:01:36 +02003275 augroup aucmd_win_test1
Bram Moolenaar0fe937f2020-06-16 22:42:04 +02003276 au!
3277 au BufEnter * call add(g:blist, [expand('<afile>'),
3278 \ win_gettype(bufwinnr(expand('<afile>')))])
3279 augroup END
3280
3281 doautoall BufEnter
Bram Moolenaar41cd8032021-03-13 15:47:56 +01003282 call assert_equal([
3283 \ ['one.txt', 'autocmd'],
3284 \ ['two.txt', ''],
3285 \ ['four.txt', 'autocmd'],
3286 \ ['three.txt', ''],
3287 \ ], g:blist)
Bram Moolenaar0fe937f2020-06-16 22:42:04 +02003288
Bram Moolenaar832adf92020-06-25 19:01:36 +02003289 augroup aucmd_win_test1
Bram Moolenaar0fe937f2020-06-16 22:42:04 +02003290 au!
3291 augroup END
Bram Moolenaar832adf92020-06-25 19:01:36 +02003292 augroup! aucmd_win_test1
3293 %bw!
3294endfunc
3295
3296" Test for trying to close the temporary window used for executing an autocmd
3297func Test_close_autocmd_window()
3298 %bw!
3299 edit one.txt
3300 tabnew two.txt
3301 augroup aucmd_win_test2
3302 au!
3303 au BufEnter * if expand('<afile>') == 'one.txt' | 1close | endif
3304 augroup END
3305
3306 call assert_fails('doautoall BufEnter', 'E813:')
3307
3308 augroup aucmd_win_test2
3309 au!
3310 augroup END
3311 augroup! aucmd_win_test2
Bram Moolenaarcf844172020-06-26 19:44:06 +02003312 %bwipe!
3313endfunc
3314
3315" Test for trying to close the tab that has the temporary window for exeucing
3316" an autocmd.
3317func Test_close_autocmd_tab()
3318 edit one.txt
3319 tabnew two.txt
3320 augroup aucmd_win_test
3321 au!
3322 au BufEnter * if expand('<afile>') == 'one.txt' | tabfirst | tabonly | endif
3323 augroup END
3324
3325 call assert_fails('doautoall BufEnter', 'E813:')
3326
3327 tabonly
3328 augroup aucmd_win_test
3329 au!
3330 augroup END
3331 augroup! aucmd_win_test
3332 %bwipe!
Bram Moolenaar0fe937f2020-06-16 22:42:04 +02003333endfunc
3334
Bram Moolenaarcb1956d2022-01-07 15:45:18 +00003335func Test_Visual_doautoall_redraw()
3336 call setline(1, ['a', 'b'])
3337 new
3338 wincmd p
3339 call feedkeys("G\<C-V>", 'txn')
3340 autocmd User Explode ++once redraw
3341 doautoall User Explode
3342 %bwipe!
3343endfunc
3344
Bram Moolenaar6bcb8772021-02-03 21:23:29 +01003345" This was using freed memory.
3346func Test_BufNew_arglocal()
3347 arglocal
3348 au BufNew * arglocal
3349 call assert_fails('drop xx', 'E1156:')
3350
3351 au! BufNew
3352endfunc
3353
Bram Moolenaar8ab37572021-02-03 21:56:59 +01003354func Test_autocmd_closes_window()
3355 au BufNew,BufWinLeave * e %e
3356 file yyy
3357 au BufNew,BufWinLeave * ball
Bram Moolenaaraad5f9d2021-02-06 17:30:31 +01003358 n xxx
Bram Moolenaar8ab37572021-02-03 21:56:59 +01003359
Bram Moolenaaraad5f9d2021-02-06 17:30:31 +01003360 %bwipe
Bram Moolenaar8ab37572021-02-03 21:56:59 +01003361 au! BufNew
3362 au! BufWinLeave
3363endfunc
3364
Bram Moolenaar92bb83e2021-02-03 23:04:46 +01003365func Test_autocmd_quit_psearch()
3366 sn aa bb
3367 augroup aucmd_win_test
3368 au!
3369 au BufEnter,BufLeave,BufNew,WinEnter,WinLeave,WinNew * if winnr('$') > 1 | q | endif
3370 augroup END
3371 ps /
3372
3373 augroup aucmd_win_test
3374 au!
3375 augroup END
zeertzjq7851c692022-04-21 11:14:01 +01003376 new
3377 pclose
Bram Moolenaar92bb83e2021-02-03 23:04:46 +01003378endfunc
3379
Bram Moolenaaraad5f9d2021-02-06 17:30:31 +01003380" Fuzzer found some strange combination that caused a crash.
3381func Test_autocmd_normal_mess()
Bram Moolenaardd07c022021-02-07 13:32:46 +01003382 " For unknown reason this hangs on MS-Windows
3383 CheckNotMSWindows
3384
Bram Moolenaaraad5f9d2021-02-06 17:30:31 +01003385 augroup aucmd_normal_test
3386 au BufLeave,BufWinLeave,BufHidden,BufUnload,BufDelete,BufWipeout * norm 7q/qc
3387 augroup END
Bram Moolenaar983d83f2021-02-07 12:12:43 +01003388 call assert_fails('o4', 'E1159')
Bram Moolenaaraad5f9d2021-02-06 17:30:31 +01003389 silent! H
Bram Moolenaar983d83f2021-02-07 12:12:43 +01003390 call assert_fails('e xx', 'E1159')
Bram Moolenaaraad5f9d2021-02-06 17:30:31 +01003391 normal G
3392
3393 augroup aucmd_normal_test
3394 au!
3395 augroup END
3396endfunc
3397
Bram Moolenaar8c6951f2021-02-06 18:08:45 +01003398func Test_autocmd_closing_cmdwin()
Bram Moolenaardd07c022021-02-07 13:32:46 +01003399 " For unknown reason this hangs on MS-Windows
3400 CheckNotMSWindows
3401
Bram Moolenaar8c6951f2021-02-06 18:08:45 +01003402 au BufWinLeave * nested q
3403 call assert_fails("norm 7q?\n", 'E855:')
3404
3405 au! BufWinLeave
3406 new
3407 only
3408endfunc
3409
Bram Moolenaar2c7080b2021-02-06 19:19:42 +01003410func Test_autocmd_vimgrep()
3411 augroup aucmd_vimgrep
Charlie Grovesfef44852022-04-19 16:24:12 +01003412 au QuickfixCmdPre,BufNew,BufReadCmd * sb
zeertzjq7851c692022-04-21 11:14:01 +01003413 au QuickfixCmdPre,BufNew,BufReadCmd * q9
Bram Moolenaar2c7080b2021-02-06 19:19:42 +01003414 augroup END
Bram Moolenaardd07c022021-02-07 13:32:46 +01003415 call assert_fails('lv ?a? foo', 'E926:')
Bram Moolenaar2c7080b2021-02-06 19:19:42 +01003416
3417 augroup aucmd_vimgrep
3418 au!
3419 augroup END
3420endfunc
3421
Bram Moolenaar73b8b0a2021-08-01 14:52:32 +02003422func Test_autocmd_with_block()
3423 augroup block_testing
3424 au BufReadPost *.xml {
3425 setlocal matchpairs+=<:>
3426 /<start
3427 }
Bram Moolenaar63b91732021-08-05 20:40:03 +02003428 au CursorHold * {
3429 autocmd BufReadPre * ++once echo 'one' | echo 'two'
3430 g:gotSafeState = 77
3431 }
Bram Moolenaar73b8b0a2021-08-01 14:52:32 +02003432 augroup END
3433
3434 let expected = "\n--- Autocommands ---\nblock_testing BufRead\n *.xml {^@ setlocal matchpairs+=<:>^@ /<start^@ }"
3435 call assert_equal(expected, execute('au BufReadPost *.xml'))
3436
Bram Moolenaar63b91732021-08-05 20:40:03 +02003437 doautocmd CursorHold
3438 call assert_equal(77, g:gotSafeState)
3439 unlet g:gotSafeState
3440
Bram Moolenaar73b8b0a2021-08-01 14:52:32 +02003441 augroup block_testing
3442 au!
Bram Moolenaar75ebd2a2022-06-03 17:39:46 +01003443 autocmd CursorHold * {
3444 if true
3445 # comment
3446 && true
3447
3448 && true
3449 g:done = 'yes'
3450 endif
3451 }
3452 augroup END
3453 doautocmd CursorHold
3454 call assert_equal('yes', g:done)
3455
3456 unlet g:done
3457 augroup block_testing
3458 au!
Bram Moolenaar73b8b0a2021-08-01 14:52:32 +02003459 augroup END
3460endfunc
3461
Christian Brabandtdb3b4462021-10-16 11:58:55 +01003462" Test TextChangedI and TextChanged
3463func Test_Changed_ChangedI()
3464 new
3465 call test_override("char_avail", 1)
3466 let [g:autocmd_i, g:autocmd_n] = ['','']
3467
3468 func! TextChangedAutocmdI(char)
3469 let g:autocmd_{tolower(a:char)} = a:char .. b:changedtick
3470 endfunc
3471
3472 augroup Test_TextChanged
3473 au!
3474 au TextChanged <buffer> :call TextChangedAutocmdI('N')
3475 au TextChangedI <buffer> :call TextChangedAutocmdI('I')
3476 augroup END
3477
3478 call feedkeys("ifoo\<esc>", 'tnix')
3479 " TODO: Test test does not seem to trigger TextChanged autocommand, this
3480 " requires running Vim in a terminal window.
3481 " call assert_equal('N3', g:autocmd_n)
3482 call assert_equal('I3', g:autocmd_i)
3483
3484 call feedkeys("yyp", 'tnix')
3485 " TODO: Test test does not seem to trigger TextChanged autocommand.
3486 " call assert_equal('N4', g:autocmd_n)
3487 call assert_equal('I3', g:autocmd_i)
3488
3489 " CleanUp
3490 call test_override("char_avail", 0)
3491 au! TextChanged <buffer>
3492 au! TextChangedI <buffer>
3493 augroup! Test_TextChanged
3494 delfu TextChangedAutocmdI
3495 unlet! g:autocmd_i g:autocmd_n
3496
3497 bw!
3498endfunc
Bram Moolenaar2c7080b2021-02-06 19:19:42 +01003499
Bram Moolenaar6f2465d2022-03-22 18:13:01 +00003500func Test_closing_autocmd_window()
3501 let lines =<< trim END
3502 edit Xa.txt
3503 tabnew Xb.txt
3504 autocmd BufEnter Xa.txt unhide 1
3505 doautoall BufEnter
3506 END
3507 call v9.CheckScriptFailure(lines, 'E814:')
3508 au! BufEnter
3509 only!
3510 bwipe Xa.txt
3511 bwipe Xb.txt
3512endfunc
3513
Bram Moolenaar347538f2022-03-26 16:28:06 +00003514func Test_bufwipeout_changes_window()
3515 " This should not crash, but we don't have any expectations about what
3516 " happens, changing window in BufWipeout has unpredictable results.
3517 tabedit
3518 let g:window_id = win_getid()
3519 topleft new
3520 setlocal bufhidden=wipe
3521 autocmd BufWipeout <buffer> call win_gotoid(g:window_id)
3522 tabprevious
3523 +tabclose
3524
3525 unlet g:window_id
3526 au! BufWipeout
3527 %bwipe!
3528endfunc
3529
zeertzjq021996f2022-04-10 11:44:04 +01003530func Test_v_event_readonly()
3531 autocmd CompleteChanged * let v:event.width = 0
3532 call assert_fails("normal! i\<C-X>\<C-V>", 'E46:')
3533 au! CompleteChanged
3534
3535 autocmd DirChangedPre * let v:event.directory = ''
3536 call assert_fails('cd .', 'E46:')
3537 au! DirChangedPre
3538
3539 autocmd ModeChanged * let v:event.new_mode = ''
3540 call assert_fails('normal! cc', 'E46:')
3541 au! ModeChanged
3542
3543 autocmd TextYankPost * let v:event.operator = ''
3544 call assert_fails('normal! yy', 'E46:')
3545 au! TextYankPost
3546endfunc
3547
zeertzjqc9e8fd62022-07-26 18:12:38 +01003548" Test for ModeChanged pattern
3549func Test_mode_changes()
3550 let g:index = 0
3551 let g:mode_seq = ['n', 'i', 'n', 'v', 'V', 'i', 'ix', 'i', 'ic', 'i', 'n', 'no', 'n', 'V', 'v', 's', 'n']
3552 func! TestMode()
3553 call assert_equal(g:mode_seq[g:index], get(v:event, "old_mode"))
3554 call assert_equal(g:mode_seq[g:index + 1], get(v:event, "new_mode"))
3555 call assert_equal(mode(1), get(v:event, "new_mode"))
3556 let g:index += 1
3557 endfunc
3558
3559 au ModeChanged * :call TestMode()
3560 let g:n_to_any = 0
3561 au ModeChanged n:* let g:n_to_any += 1
3562 call feedkeys("i\<esc>vVca\<CR>\<C-X>\<C-L>\<esc>ggdG", 'tnix')
3563
3564 let g:V_to_v = 0
3565 au ModeChanged V:v let g:V_to_v += 1
3566 call feedkeys("Vv\<C-G>\<esc>", 'tnix')
3567 call assert_equal(len(filter(g:mode_seq[1:], {idx, val -> val == 'n'})), g:n_to_any)
3568 call assert_equal(1, g:V_to_v)
3569 call assert_equal(len(g:mode_seq) - 1, g:index)
3570
3571 let g:n_to_i = 0
3572 au ModeChanged n:i let g:n_to_i += 1
3573 let g:n_to_niI = 0
3574 au ModeChanged i:niI let g:n_to_niI += 1
3575 let g:niI_to_i = 0
3576 au ModeChanged niI:i let g:niI_to_i += 1
3577 let g:nany_to_i = 0
3578 au ModeChanged n*:i let g:nany_to_i += 1
3579 let g:i_to_n = 0
3580 au ModeChanged i:n let g:i_to_n += 1
3581 let g:nori_to_any = 0
3582 au ModeChanged [ni]:* let g:nori_to_any += 1
3583 let g:i_to_any = 0
3584 au ModeChanged i:* let g:i_to_any += 1
3585 let g:index = 0
3586 let g:mode_seq = ['n', 'i', 'niI', 'i', 'n']
3587 call feedkeys("a\<C-O>l\<esc>", 'tnix')
3588 call assert_equal(len(g:mode_seq) - 1, g:index)
3589 call assert_equal(1, g:n_to_i)
3590 call assert_equal(1, g:n_to_niI)
3591 call assert_equal(1, g:niI_to_i)
3592 call assert_equal(2, g:nany_to_i)
3593 call assert_equal(1, g:i_to_n)
3594 call assert_equal(2, g:i_to_any)
3595 call assert_equal(3, g:nori_to_any)
3596
3597 if has('terminal')
3598 let g:mode_seq += ['c', 'n', 't', 'nt', 'c', 'nt', 'n']
3599 call feedkeys(":term\<CR>\<C-W>N:bd!\<CR>", 'tnix')
3600 call assert_equal(len(g:mode_seq) - 1, g:index)
3601 call assert_equal(1, g:n_to_i)
3602 call assert_equal(1, g:n_to_niI)
3603 call assert_equal(1, g:niI_to_i)
3604 call assert_equal(2, g:nany_to_i)
3605 call assert_equal(1, g:i_to_n)
3606 call assert_equal(2, g:i_to_any)
3607 call assert_equal(5, g:nori_to_any)
3608 endif
3609
zeertzjqd1955982022-10-05 11:24:46 +01003610 let g:n_to_c = 0
3611 au ModeChanged n:c let g:n_to_c += 1
3612 let g:c_to_n = 0
3613 au ModeChanged c:n let g:c_to_n += 1
3614 let g:mode_seq += ['c', 'n', 'c', 'n']
3615 call feedkeys("q:\<C-C>\<Esc>", 'tnix')
3616 call assert_equal(len(g:mode_seq) - 1, g:index)
3617 call assert_equal(2, g:n_to_c)
3618 call assert_equal(2, g:c_to_n)
3619 unlet g:n_to_c
3620 unlet g:c_to_n
zeertzjqc9e8fd62022-07-26 18:12:38 +01003621
Bram Moolenaar61c4b042022-10-18 15:10:11 +01003622 let g:n_to_v = 0
3623 au ModeChanged n:v let g:n_to_v += 1
3624 let g:v_to_n = 0
3625 au ModeChanged v:n let g:v_to_n += 1
3626 let g:mode_seq += ['v', 'n']
3627 call feedkeys("v\<C-C>", 'tnix')
3628 call assert_equal(len(g:mode_seq) - 1, g:index)
3629 call assert_equal(1, g:n_to_v)
3630 call assert_equal(1, g:v_to_n)
3631 unlet g:n_to_v
3632 unlet g:v_to_n
3633
zeertzjqc9e8fd62022-07-26 18:12:38 +01003634 au! ModeChanged
3635 delfunc TestMode
3636 unlet! g:mode_seq
3637 unlet! g:index
3638 unlet! g:n_to_any
3639 unlet! g:V_to_v
3640 unlet! g:n_to_i
3641 unlet! g:n_to_niI
3642 unlet! g:niI_to_i
3643 unlet! g:nany_to_i
3644 unlet! g:i_to_n
3645 unlet! g:nori_to_any
3646 unlet! g:i_to_any
3647endfunc
3648
3649func Test_recursive_ModeChanged()
3650 au! ModeChanged * norm 0u
3651 sil! norm 
3652 au! ModeChanged
3653endfunc
3654
3655func Test_ModeChanged_starts_visual()
3656 " This was triggering ModeChanged before setting VIsual, causing a crash.
3657 au! ModeChanged * norm 0u
3658 sil! norm 
3659
3660 au! ModeChanged
3661endfunc
Bram Moolenaar347538f2022-03-26 16:28:06 +00003662
Charlie Grovesfef44852022-04-19 16:24:12 +01003663func Test_noname_autocmd()
3664 augroup test_noname_autocmd_group
3665 autocmd!
3666 autocmd BufEnter * call add(s:li, ["BufEnter", expand("<afile>")])
3667 autocmd BufDelete * call add(s:li, ["BufDelete", expand("<afile>")])
3668 autocmd BufLeave * call add(s:li, ["BufLeave", expand("<afile>")])
3669 autocmd BufUnload * call add(s:li, ["BufUnload", expand("<afile>")])
3670 autocmd BufWipeout * call add(s:li, ["BufWipeout", expand("<afile>")])
3671 augroup END
3672
3673 let s:li = []
3674 edit foo
3675 call assert_equal([['BufUnload', ''], ['BufDelete', ''], ['BufWipeout', ''], ['BufEnter', 'foo']], s:li)
3676
3677 au! test_noname_autocmd_group
3678 augroup! test_noname_autocmd_group
3679endfunc
3680
Yegappan Lakshmanan1755a912022-05-19 10:31:47 +01003681" Test for the autocmd_get() function
3682func Test_autocmd_get()
3683 augroup TestAutoCmdFns
3684 au!
3685 autocmd BufAdd *.vim echo "bufadd-vim"
3686 autocmd BufAdd *.py echo "bufadd-py"
3687 autocmd BufHidden *.vim echo "bufhidden"
3688 augroup END
3689 augroup TestAutoCmdFns2
3690 autocmd BufAdd *.vim echo "bufadd-vim-2"
3691 autocmd BufRead *.a1b2c3 echo "bufadd-vim-2"
3692 augroup END
3693
3694 let l = autocmd_get()
3695 call assert_true(l->len() > 0)
3696
3697 " Test for getting all the autocmds in a group
3698 let expected = [
3699 \ #{cmd: 'echo "bufadd-vim"', group: 'TestAutoCmdFns',
3700 \ pattern: '*.vim', nested: v:false, once: v:false,
3701 \ event: 'BufAdd'},
3702 \ #{cmd: 'echo "bufadd-py"', group: 'TestAutoCmdFns',
3703 \ pattern: '*.py', nested: v:false, once: v:false,
3704 \ event: 'BufAdd'},
3705 \ #{cmd: 'echo "bufhidden"', group: 'TestAutoCmdFns',
3706 \ pattern: '*.vim', nested: v:false,
3707 \ once: v:false, event: 'BufHidden'}]
3708 call assert_equal(expected, autocmd_get(#{group: 'TestAutoCmdFns'}))
3709
3710 " Test for getting autocmds for all the patterns in a group
3711 call assert_equal(expected, autocmd_get(#{group: 'TestAutoCmdFns',
3712 \ event: '*'}))
3713
3714 " Test for getting autocmds for an event in a group
3715 let expected = [
3716 \ #{cmd: 'echo "bufadd-vim"', group: 'TestAutoCmdFns',
3717 \ pattern: '*.vim', nested: v:false, once: v:false,
3718 \ event: 'BufAdd'},
3719 \ #{cmd: 'echo "bufadd-py"', group: 'TestAutoCmdFns',
3720 \ pattern: '*.py', nested: v:false, once: v:false,
3721 \ event: 'BufAdd'}]
3722 call assert_equal(expected, autocmd_get(#{group: 'TestAutoCmdFns',
3723 \ event: 'BufAdd'}))
3724
3725 " Test for getting the autocmds for all the events in a group for particular
3726 " pattern
3727 call assert_equal([{'cmd': 'echo "bufadd-py"', 'group': 'TestAutoCmdFns',
3728 \ 'pattern': '*.py', 'nested': v:false, 'once': v:false,
3729 \ 'event': 'BufAdd'}],
3730 \ autocmd_get(#{group: 'TestAutoCmdFns', event: '*', pattern: '*.py'}))
3731
3732 " Test for getting the autocmds for an events in a group for particular
3733 " pattern
3734 let l = autocmd_get(#{group: 'TestAutoCmdFns', event: 'BufAdd',
3735 \ pattern: '*.vim'})
3736 call assert_equal([
3737 \ #{cmd: 'echo "bufadd-vim"', group: 'TestAutoCmdFns',
3738 \ pattern: '*.vim', nested: v:false, once: v:false,
3739 \ event: 'BufAdd'}], l)
3740
3741 " Test for getting the autocmds for a pattern in a group
3742 let l = autocmd_get(#{group: 'TestAutoCmdFns', pattern: '*.vim'})
3743 call assert_equal([
3744 \ #{cmd: 'echo "bufadd-vim"', group: 'TestAutoCmdFns',
3745 \ pattern: '*.vim', nested: v:false, once: v:false,
3746 \ event: 'BufAdd'},
3747 \ #{cmd: 'echo "bufhidden"', group: 'TestAutoCmdFns',
3748 \ pattern: '*.vim', nested: v:false,
3749 \ once: v:false, event: 'BufHidden'}], l)
3750
3751 " Test for getting the autocmds for a pattern in all the groups
3752 let l = autocmd_get(#{pattern: '*.a1b2c3'})
3753 call assert_equal([{'cmd': 'echo "bufadd-vim-2"', 'group': 'TestAutoCmdFns2',
3754 \ 'pattern': '*.a1b2c3', 'nested': v:false, 'once': v:false,
3755 \ 'event': 'BufRead'}], l)
3756
3757 " Test for getting autocmds for a pattern without any autocmds
3758 call assert_equal([], autocmd_get(#{group: 'TestAutoCmdFns',
3759 \ pattern: '*.abc'}))
3760 call assert_equal([], autocmd_get(#{group: 'TestAutoCmdFns',
3761 \ event: 'BufAdd', pattern: '*.abc'}))
3762 call assert_equal([], autocmd_get(#{group: 'TestAutoCmdFns',
3763 \ event: 'BufWipeout'}))
3764 call assert_fails("call autocmd_get(#{group: 'abc', event: 'BufAdd'})",
3765 \ 'E367:')
3766 let cmd = "echo autocmd_get(#{group: 'TestAutoCmdFns', event: 'abc'})"
3767 call assert_fails(cmd, 'E216:')
3768 call assert_fails("call autocmd_get(#{group: 'abc'})", 'E367:')
3769 call assert_fails("echo autocmd_get(#{event: 'abc'})", 'E216:')
3770
3771 augroup TestAutoCmdFns
3772 au!
3773 augroup END
3774 call assert_equal([], autocmd_get(#{group: 'TestAutoCmdFns'}))
3775
3776 " Test for nested and once autocmds
3777 augroup TestAutoCmdFns
3778 au!
3779 autocmd VimSuspend * ++nested echo "suspend"
3780 autocmd VimResume * ++once echo "resume"
3781 augroup END
3782
3783 let expected = [
3784 \ {'cmd': 'echo "suspend"', 'group': 'TestAutoCmdFns', 'pattern': '*',
3785 \ 'nested': v:true, 'once': v:false, 'event': 'VimSuspend'},
3786 \ {'cmd': 'echo "resume"', 'group': 'TestAutoCmdFns', 'pattern': '*',
3787 \ 'nested': v:false, 'once': v:true, 'event': 'VimResume'}]
3788 call assert_equal(expected, autocmd_get(#{group: 'TestAutoCmdFns'}))
3789
3790 " Test for buffer-local autocmd
3791 augroup TestAutoCmdFns
3792 au!
3793 autocmd TextYankPost <buffer> echo "textyankpost"
3794 augroup END
3795
3796 let expected = [
3797 \ {'cmd': 'echo "textyankpost"', 'group': 'TestAutoCmdFns',
3798 \ 'pattern': '<buffer=' .. bufnr() .. '>', 'nested': v:false,
3799 \ 'once': v:false, 'bufnr': bufnr(), 'event': 'TextYankPost'}]
3800 call assert_equal(expected, autocmd_get(#{group: 'TestAutoCmdFns'}))
3801
3802 augroup TestAutoCmdFns
3803 au!
3804 augroup END
3805 augroup! TestAutoCmdFns
3806 augroup TestAutoCmdFns2
3807 au!
3808 augroup END
3809 augroup! TestAutoCmdFns2
3810
3811 call assert_fails("echo autocmd_get(#{group: []})", 'E730:')
3812 call assert_fails("echo autocmd_get(#{event: {}})", 'E731:')
3813 call assert_fails("echo autocmd_get([])", 'E1206:')
3814endfunc
3815
3816" Test for the autocmd_add() function
3817func Test_autocmd_add()
3818 " Define a single autocmd in a group
3819 call autocmd_add([#{group: 'TestAcSet', event: 'BufAdd', pattern: '*.sh',
3820 \ cmd: 'echo "bufadd"', once: v:true, nested: v:true}])
3821 call assert_equal([#{cmd: 'echo "bufadd"', group: 'TestAcSet',
3822 \ pattern: '*.sh', nested: v:true, once: v:true,
3823 \ event: 'BufAdd'}], autocmd_get(#{group: 'TestAcSet'}))
3824
3825 " Define two autocmds in the same group
3826 call autocmd_delete([#{group: 'TestAcSet'}])
3827 call autocmd_add([#{group: 'TestAcSet', event: 'BufAdd', pattern: '*.sh',
3828 \ cmd: 'echo "bufadd"'},
3829 \ #{group: 'TestAcSet', event: 'BufEnter', pattern: '*.sh',
3830 \ cmd: 'echo "bufenter"'}])
3831 call assert_equal([
3832 \ #{cmd: 'echo "bufadd"', group: 'TestAcSet', pattern: '*.sh',
3833 \ nested: v:false, once: v:false, event: 'BufAdd'},
3834 \ #{cmd: 'echo "bufenter"', group: 'TestAcSet', pattern: '*.sh',
3835 \ nested: v:false, once: v:false, event: 'BufEnter'}],
3836 \ autocmd_get(#{group: 'TestAcSet'}))
3837
3838 " Define a buffer-local autocmd
3839 call autocmd_delete([#{group: 'TestAcSet'}])
3840 call autocmd_add([#{group: 'TestAcSet', event: 'CursorHold',
3841 \ bufnr: bufnr(), cmd: 'echo "cursorhold"'}])
3842 call assert_equal([
3843 \ #{cmd: 'echo "cursorhold"', group: 'TestAcSet',
3844 \ pattern: '<buffer=' .. bufnr() .. '>', nested: v:false,
3845 \ once: v:false, bufnr: bufnr(), event: 'CursorHold'}],
3846 \ autocmd_get(#{group: 'TestAcSet'}))
3847
3848 " Use an invalid buffer number
3849 call autocmd_delete([#{group: 'TestAcSet'}])
3850 call autocmd_add([#{group: 'TestAcSet', event: 'BufEnter',
3851 \ bufnr: -1, cmd: 'echo "bufenter"'}])
3852 let l = [#{group: 'TestAcSet', event: 'BufAdd', bufnr: 9999,
3853 \ cmd: 'echo "bufadd"'}]
3854 call assert_fails("echo autocmd_add(l)", 'E680:')
Yegappan Lakshmanan00e977c2022-06-01 12:31:53 +01003855 let l = [#{group: 'TestAcSet', event: 'BufAdd', bufnr: 9999,
3856 \ pattern: '*.py', cmd: 'echo "bufadd"'}]
3857 call assert_fails("echo autocmd_add(l)", 'E680:')
3858 let l = [#{group: 'TestAcSet', event: 'BufAdd', bufnr: 9999,
3859 \ pattern: ['*.py', '*.c'], cmd: 'echo "bufadd"'}]
3860 call assert_fails("echo autocmd_add(l)", 'E680:')
Yegappan Lakshmanan1755a912022-05-19 10:31:47 +01003861 let l = [#{group: 'TestAcSet', event: 'BufRead', bufnr: [],
3862 \ cmd: 'echo "bufread"'}]
3863 call assert_fails("echo autocmd_add(l)", 'E745:')
3864 call assert_equal([], autocmd_get(#{group: 'TestAcSet'}))
3865
3866 " Add two commands to the same group, event and pattern
3867 call autocmd_delete([#{group: 'TestAcSet'}])
3868 call autocmd_add([#{group: 'TestAcSet', event: 'BufUnload',
3869 \ pattern: 'abc', cmd: 'echo "cmd1"'}])
3870 call autocmd_add([#{group: 'TestAcSet', event: 'BufUnload',
3871 \ pattern: 'abc', cmd: 'echo "cmd2"'}])
3872 call assert_equal([
3873 \ #{cmd: 'echo "cmd1"', group: 'TestAcSet', pattern: 'abc',
3874 \ nested: v:false, once: v:false, event: 'BufUnload'},
3875 \ #{cmd: 'echo "cmd2"', group: 'TestAcSet', pattern: 'abc',
3876 \ nested: v:false, once: v:false, event: 'BufUnload'}],
3877 \ autocmd_get(#{group: 'TestAcSet'}))
3878
3879 " When adding a new autocmd, if the autocmd 'group' is not specified, then
3880 " the current autocmd group should be used.
3881 call autocmd_delete([#{group: 'TestAcSet'}])
3882 augroup TestAcSet
3883 call autocmd_add([#{event: 'BufHidden', pattern: 'abc', cmd: 'echo "abc"'}])
3884 augroup END
3885 call assert_equal([
3886 \ #{cmd: 'echo "abc"', group: 'TestAcSet', pattern: 'abc',
3887 \ nested: v:false, once: v:false, event: 'BufHidden'}],
3888 \ autocmd_get(#{group: 'TestAcSet'}))
3889
Yegappan Lakshmanan971f6822022-05-24 11:40:11 +01003890 " Test for replacing a cmd for an event in a group
3891 call autocmd_delete([#{group: 'TestAcSet'}])
3892 call autocmd_add([#{replace: v:true, group: 'TestAcSet', event: 'BufEnter',
3893 \ pattern: '*.py', cmd: 'echo "bufenter"'}])
3894 call autocmd_add([#{replace: v:true, group: 'TestAcSet', event: 'BufEnter',
3895 \ pattern: '*.py', cmd: 'echo "bufenter"'}])
3896 call assert_equal([
3897 \ #{cmd: 'echo "bufenter"', group: 'TestAcSet', pattern: '*.py',
3898 \ nested: v:false, once: v:false, event: 'BufEnter'}],
3899 \ autocmd_get(#{group: 'TestAcSet'}))
3900
3901 " Test for adding a command for an unsupported autocmd event
Yegappan Lakshmanan1755a912022-05-19 10:31:47 +01003902 let l = [#{group: 'TestAcSet', event: 'abc', pattern: '*.sh',
3903 \ cmd: 'echo "bufadd"'}]
3904 call assert_fails('call autocmd_add(l)', 'E216:')
3905
Yegappan Lakshmanane0ff3a72022-05-27 18:05:33 +01003906 " Test for using a list of events and patterns
3907 call autocmd_delete([#{group: 'TestAcSet'}])
3908 let l = [#{group: 'TestAcSet', event: ['BufEnter', 'BufLeave'],
3909 \ pattern: ['*.py', '*.sh'], cmd: 'echo "bufcmds"'}]
3910 call autocmd_add(l)
3911 call assert_equal([
3912 \ #{cmd: 'echo "bufcmds"', group: 'TestAcSet', pattern: '*.py',
3913 \ nested: v:false, once: v:false, event: 'BufEnter'},
3914 \ #{cmd: 'echo "bufcmds"', group: 'TestAcSet', pattern: '*.sh',
3915 \ nested: v:false, once: v:false, event: 'BufEnter'},
3916 \ #{cmd: 'echo "bufcmds"', group: 'TestAcSet', pattern: '*.py',
3917 \ nested: v:false, once: v:false, event: 'BufLeave'},
3918 \ #{cmd: 'echo "bufcmds"', group: 'TestAcSet', pattern: '*.sh',
3919 \ nested: v:false, once: v:false, event: 'BufLeave'}],
3920 \ autocmd_get(#{group: 'TestAcSet'}))
3921
3922 " Test for invalid values for 'event' item
3923 call autocmd_delete([#{group: 'TestAcSet'}])
3924 let l = [#{group: 'TestAcSet', event: test_null_string(),
3925 \ pattern: "*.py", cmd: 'echo "bufcmds"'}]
3926 call assert_fails('call autocmd_add(l)', 'E928:')
3927 let l = [#{group: 'TestAcSet', event: test_null_list(),
3928 \ pattern: "*.py", cmd: 'echo "bufcmds"'}]
3929 call assert_fails('call autocmd_add(l)', 'E714:')
3930 let l = [#{group: 'TestAcSet', event: {},
3931 \ pattern: "*.py", cmd: 'echo "bufcmds"'}]
3932 call assert_fails('call autocmd_add(l)', 'E777:')
3933 let l = [#{group: 'TestAcSet', event: [{}],
3934 \ pattern: "*.py", cmd: 'echo "bufcmds"'}]
3935 call assert_fails('call autocmd_add(l)', 'E928:')
3936 let l = [#{group: 'TestAcSet', event: [test_null_string()],
3937 \ pattern: "*.py", cmd: 'echo "bufcmds"'}]
3938 call assert_fails('call autocmd_add(l)', 'E928:')
3939 let l = [#{group: 'TestAcSet', event: 'BufEnter,BufLeave',
3940 \ pattern: '*.py', cmd: 'echo "bufcmds"'}]
3941 call assert_fails('call autocmd_add(l)', 'E216:')
3942 let l = [#{group: 'TestAcSet', event: [],
3943 \ pattern: "*.py", cmd: 'echo "bufcmds"'}]
3944 call autocmd_add(l)
3945 let l = [#{group: 'TestAcSet', event: [""],
3946 \ pattern: "*.py", cmd: 'echo "bufcmds"'}]
3947 call assert_fails('call autocmd_add(l)', 'E216:')
3948 let l = [#{group: 'TestAcSet', event: "",
3949 \ pattern: "*.py", cmd: 'echo "bufcmds"'}]
3950 call autocmd_add(l)
3951 call assert_equal([], autocmd_get(#{group: 'TestAcSet'}))
3952
3953 " Test for invalid values for 'pattern' item
3954 let l = [#{group: 'TestAcSet', event: "BufEnter",
3955 \ pattern: test_null_string(), cmd: 'echo "bufcmds"'}]
Yegappan Lakshmanan00e977c2022-06-01 12:31:53 +01003956 call assert_fails('call autocmd_add(l)', 'E928:')
Yegappan Lakshmanane0ff3a72022-05-27 18:05:33 +01003957 let l = [#{group: 'TestAcSet', event: "BufEnter",
3958 \ pattern: test_null_list(), cmd: 'echo "bufcmds"'}]
3959 call assert_fails('call autocmd_add(l)', 'E714:')
3960 let l = [#{group: 'TestAcSet', event: "BufEnter",
3961 \ pattern: {}, cmd: 'echo "bufcmds"'}]
3962 call assert_fails('call autocmd_add(l)', 'E777:')
3963 let l = [#{group: 'TestAcSet', event: "BufEnter",
3964 \ pattern: [{}], cmd: 'echo "bufcmds"'}]
3965 call assert_fails('call autocmd_add(l)', 'E928:')
3966 let l = [#{group: 'TestAcSet', event: "BufEnter",
3967 \ pattern: [test_null_string()], cmd: 'echo "bufcmds"'}]
3968 call assert_fails('call autocmd_add(l)', 'E928:')
3969 let l = [#{group: 'TestAcSet', event: "BufEnter",
3970 \ pattern: [], cmd: 'echo "bufcmds"'}]
3971 call autocmd_add(l)
3972 let l = [#{group: 'TestAcSet', event: "BufEnter",
3973 \ pattern: [""], cmd: 'echo "bufcmds"'}]
3974 call autocmd_add(l)
3975 let l = [#{group: 'TestAcSet', event: "BufEnter",
3976 \ pattern: "", cmd: 'echo "bufcmds"'}]
3977 call autocmd_add(l)
3978 call assert_equal([], autocmd_get(#{group: 'TestAcSet'}))
3979
3980 let l = [#{group: 'TestAcSet', event: 'BufEnter,abc,BufLeave',
3981 \ pattern: '*.py', cmd: 'echo "bufcmds"'}]
3982 call assert_fails('call autocmd_add(l)', 'E216:')
3983
Yegappan Lakshmanan1755a912022-05-19 10:31:47 +01003984 call assert_fails("call autocmd_add({})", 'E1211:')
3985 call assert_equal(v:false, autocmd_add(test_null_list()))
3986 call assert_true(autocmd_add([[]]))
3987 call assert_true(autocmd_add([test_null_dict()]))
3988
3989 augroup TestAcSet
3990 au!
3991 augroup END
3992
3993 call autocmd_add([#{group: 'TestAcSet'}])
3994 call autocmd_add([#{group: 'TestAcSet', event: 'BufAdd'}])
3995 call autocmd_add([#{group: 'TestAcSet', pat: '*.sh'}])
3996 call autocmd_add([#{group: 'TestAcSet', cmd: 'echo "a"'}])
3997 call autocmd_add([#{group: 'TestAcSet', event: 'BufAdd', pat: '*.sh'}])
3998 call autocmd_add([#{group: 'TestAcSet', event: 'BufAdd', cmd: 'echo "a"'}])
3999 call autocmd_add([#{group: 'TestAcSet', pat: '*.sh', cmd: 'echo "a"'}])
4000 call assert_equal([], autocmd_get(#{group: 'TestAcSet'}))
4001
4002 augroup! TestAcSet
4003endfunc
4004
4005" Test for deleting autocmd events and groups
4006func Test_autocmd_delete()
4007 " Delete an event in an autocmd group
4008 augroup TestAcSet
4009 au!
4010 au BufAdd *.sh echo "bufadd"
4011 au BufEnter *.sh echo "bufenter"
4012 augroup END
4013 call autocmd_delete([#{group: 'TestAcSet', event: 'BufAdd'}])
4014 call assert_equal([#{cmd: 'echo "bufenter"', group: 'TestAcSet',
4015 \ pattern: '*.sh', nested: v:false, once: v:false,
4016 \ event: 'BufEnter'}], autocmd_get(#{group: 'TestAcSet'}))
4017
4018 " Delete all the events in an autocmd group
4019 augroup TestAcSet
4020 au BufAdd *.sh echo "bufadd"
4021 augroup END
4022 call autocmd_delete([#{group: 'TestAcSet', event: '*'}])
4023 call assert_equal([], autocmd_get(#{group: 'TestAcSet'}))
4024
4025 " Delete a non-existing autocmd group
4026 call assert_fails("call autocmd_delete([#{group: 'abc'}])", 'E367:')
4027 " Delete a non-existing autocmd event
4028 let l = [#{group: 'TestAcSet', event: 'abc'}]
4029 call assert_fails("call autocmd_delete(l)", 'E216:')
4030 " Delete a non-existing autocmd pattern
4031 let l = [#{group: 'TestAcSet', event: 'BufAdd', pat: 'abc'}]
4032 call assert_true(autocmd_delete(l))
Yegappan Lakshmanan00e977c2022-06-01 12:31:53 +01004033 " Delete an autocmd for a non-existing buffer
4034 let l = [#{event: '*', bufnr: 9999, cmd: 'echo "x"'}]
4035 call assert_fails('call autocmd_delete(l)', 'E680:')
Yegappan Lakshmanan1755a912022-05-19 10:31:47 +01004036
4037 " Delete an autocmd group
4038 augroup TestAcSet
4039 au!
4040 au BufAdd *.sh echo "bufadd"
4041 au BufEnter *.sh echo "bufenter"
4042 augroup END
4043 call autocmd_delete([#{group: 'TestAcSet'}])
4044 call assert_fails("call autocmd_get(#{group: 'TestAcSet'})", 'E367:')
4045
4046 call assert_true(autocmd_delete([[]]))
4047 call assert_true(autocmd_delete([test_null_dict()]))
4048endfunc
4049
Bram Moolenaar8f3c3c62022-10-18 17:05:54 +01004050func Test_autocmd_split_dummy()
4051 " Autocommand trying to split a window containing a dummy buffer.
4052 auto BufReadPre * exe "sbuf " .. expand("<abuf>")
4053 " Avoid the "W11" prompt
4054 au FileChangedShell * let v:fcs_choice = 'reload'
4055 func Xautocmd_changelist()
4056 cal writefile(['Xtestfile2:4:4'], 'Xerr')
4057 edit Xerr
4058 lex 'Xtestfile2:4:4'
4059 endfunc
4060 call Xautocmd_changelist()
Bram Moolenaar53c5c9f2022-10-18 17:25:03 +01004061 " Should get E86, but it doesn't always happen (timing?)
4062 silent! call Xautocmd_changelist()
Bram Moolenaar8f3c3c62022-10-18 17:05:54 +01004063
4064 au! BufReadPre
4065 au! FileChangedShell
4066 delfunc Xautocmd_changelist
4067 bwipe! Xerr
4068 call delete('Xerr')
4069endfunc
4070
Bram Moolenaarbc2b71d2020-02-17 21:33:30 +01004071" vim: shiftwidth=2 sts=2 expandtab