blob: 2df70b4df7393b4b13ea72179e191d6293583719 [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)
Bram Moolenaar23526d22022-12-05 15:50:41 +0000581
582 call StopVimInTerminal(buf)
zeertzjq670ab032022-08-28 19:16:15 +0100583endfunc
584
naohiro ono23beefe2021-11-13 12:38:49 +0000585func Test_WinClosed()
586 " Test that the pattern is matched against the closed window's ID, and both
587 " <amatch> and <afile> are set to it.
588 new
589 let winid = win_getid()
590 let g:matched = v:false
591 augroup test-WinClosed
592 autocmd!
593 execute 'autocmd WinClosed' winid 'let g:matched = v:true'
594 autocmd WinClosed * let g:amatch = str2nr(expand('<amatch>'))
595 autocmd WinClosed * let g:afile = str2nr(expand('<afile>'))
596 augroup END
597 close
598 call assert_true(g:matched)
599 call assert_equal(winid, g:amatch)
600 call assert_equal(winid, g:afile)
601
602 " Test that WinClosed is non-recursive.
603 new
604 new
605 call assert_equal(3, winnr('$'))
606 let g:triggered = 0
607 augroup test-WinClosed
608 autocmd!
609 autocmd WinClosed * let g:triggered += 1
610 autocmd WinClosed * 2 wincmd c
611 augroup END
612 close
613 call assert_equal(1, winnr('$'))
614 call assert_equal(1, g:triggered)
615
616 autocmd! test-WinClosed
617 augroup! test-WinClosed
618 unlet g:matched
619 unlet g:amatch
620 unlet g:afile
621 unlet g:triggered
622endfunc
623
Bram Moolenaarc947b9a2022-04-06 17:59:21 +0100624func Test_WinClosed_throws()
625 vnew
626 let bnr = bufnr()
627 call assert_equal(1, bufloaded(bnr))
628 augroup test-WinClosed
629 autocmd WinClosed * throw 'foo'
630 augroup END
631 try
632 close
633 catch /.*/
634 endtry
635 call assert_equal(0, bufloaded(bnr))
636
637 autocmd! test-WinClosed
638 augroup! test-WinClosed
639endfunc
640
zeertzjq6a069402022-04-07 14:08:29 +0100641func Test_WinClosed_throws_with_tabs()
642 tabnew
643 let bnr = bufnr()
644 call assert_equal(1, bufloaded(bnr))
645 augroup test-WinClosed
646 autocmd WinClosed * throw 'foo'
647 augroup END
648 try
649 close
650 catch /.*/
651 endtry
652 call assert_equal(0, bufloaded(bnr))
653
654 autocmd! test-WinClosed
655 augroup! test-WinClosed
656endfunc
657
zeertzjq62de54b2022-09-22 18:08:37 +0100658" This used to trigger WinClosed twice for the same window, and the window's
659" buffer was NULL in the second autocommand.
660func Test_WinClosed_switch_tab()
661 edit Xa
662 split Xb
663 split Xc
664 tab split
665 new
666 augroup test-WinClosed
667 autocmd WinClosed * tabprev | bwipe!
668 augroup END
669 close
670 " Check that the tabline has been fully removed
671 call assert_equal([1, 1], win_screenpos(0))
672
673 autocmd! test-WinClosed
674 augroup! test-WinClosed
675 %bwipe!
676endfunc
677
Bram Moolenaare99e8442016-07-26 20:43:40 +0200678func s:AddAnAutocmd()
679 augroup vimBarTest
680 au BufReadCmd * echo 'hello'
681 augroup END
682 call assert_equal(3, len(split(execute('au vimBarTest'), "\n")))
683endfunc
684
685func Test_early_bar()
686 " test that a bar is recognized before the {event}
687 call s:AddAnAutocmd()
Bram Moolenaarb8e642f2021-11-20 10:38:25 +0000688 augroup vimBarTest | au! | let done = 77 | augroup END
Bram Moolenaare99e8442016-07-26 20:43:40 +0200689 call assert_equal(1, len(split(execute('au vimBarTest'), "\n")))
Bram Moolenaarb8e642f2021-11-20 10:38:25 +0000690 call assert_equal(77, done)
Bram Moolenaare99e8442016-07-26 20:43:40 +0200691
692 call s:AddAnAutocmd()
Bram Moolenaarb8e642f2021-11-20 10:38:25 +0000693 augroup vimBarTest| au!| let done = 88 | augroup END
Bram Moolenaare99e8442016-07-26 20:43:40 +0200694 call assert_equal(1, len(split(execute('au vimBarTest'), "\n")))
Bram Moolenaarb8e642f2021-11-20 10:38:25 +0000695 call assert_equal(88, done)
Bram Moolenaare99e8442016-07-26 20:43:40 +0200696
697 " test that a bar is recognized after the {event}
698 call s:AddAnAutocmd()
Bram Moolenaarb8e642f2021-11-20 10:38:25 +0000699 augroup vimBarTest| au!BufReadCmd| let done = 99 | augroup END
Bram Moolenaare99e8442016-07-26 20:43:40 +0200700 call assert_equal(1, len(split(execute('au vimBarTest'), "\n")))
Bram Moolenaarb8e642f2021-11-20 10:38:25 +0000701 call assert_equal(99, done)
Bram Moolenaare99e8442016-07-26 20:43:40 +0200702
703 " test that a bar is recognized after the {group}
704 call s:AddAnAutocmd()
705 au! vimBarTest|echo 'hello'
706 call assert_equal(1, len(split(execute('au vimBarTest'), "\n")))
707endfunc
Bram Moolenaarf2c4c392016-07-29 20:50:24 +0200708
Bram Moolenaar5c809082016-09-01 16:21:48 +0200709func RemoveGroup()
710 autocmd! StartOK
711 augroup! StartOK
712endfunc
713
Bram Moolenaarf2c4c392016-07-29 20:50:24 +0200714func Test_augroup_warning()
715 augroup TheWarning
716 au VimEnter * echo 'entering'
717 augroup END
Bram Moolenaar5dc4e2f2020-11-25 14:15:12 +0100718 call assert_match("TheWarning.*VimEnter", execute('au VimEnter'))
Bram Moolenaarf2c4c392016-07-29 20:50:24 +0200719 redir => res
720 augroup! TheWarning
721 redir END
Bram Moolenaar5dc4e2f2020-11-25 14:15:12 +0100722 call assert_match("W19:", res)
723 call assert_match("-Deleted-.*VimEnter", execute('au VimEnter'))
Bram Moolenaarf2c4c392016-07-29 20:50:24 +0200724
725 " check "Another" does not take the pace of the deleted entry
726 augroup Another
727 augroup END
Bram Moolenaar5dc4e2f2020-11-25 14:15:12 +0100728 call assert_match("-Deleted-.*VimEnter", execute('au VimEnter'))
Bram Moolenaaraeac9002016-09-06 22:15:08 +0200729 augroup! Another
Bram Moolenaar5c809082016-09-01 16:21:48 +0200730
731 " no warning for postpone aucmd delete
732 augroup StartOK
733 au VimEnter * call RemoveGroup()
734 augroup END
Bram Moolenaar5dc4e2f2020-11-25 14:15:12 +0100735 call assert_match("StartOK.*VimEnter", execute('au VimEnter'))
Bram Moolenaar5c809082016-09-01 16:21:48 +0200736 redir => res
737 doautocmd VimEnter
738 redir END
Bram Moolenaar5dc4e2f2020-11-25 14:15:12 +0100739 call assert_notmatch("W19:", res)
Bram Moolenaarde653f02016-09-03 16:59:06 +0200740 au! VimEnter
Bram Moolenaarad48e6c2020-04-21 22:19:45 +0200741
742 call assert_fails('augroup!', 'E471:')
Bram Moolenaarf2c4c392016-07-29 20:50:24 +0200743endfunc
Bram Moolenaarb62cc362016-09-03 16:43:53 +0200744
Bram Moolenaar8d84ff12017-10-26 16:42:16 +0200745func Test_BufReadCmdHelp()
746 " This used to cause access to free memory
747 au BufReadCmd * e +h
748 help
749
Bram Moolenaar8d84ff12017-10-26 16:42:16 +0200750 au! BufReadCmd
751endfunc
752
753func Test_BufReadCmdHelpJump()
754 " This used to cause access to free memory
755 au BufReadCmd * e +h{
Bram Moolenaarcf1ba352017-10-27 00:55:04 +0200756 " } to fix highlighting
757 call assert_fails('help', 'E434:')
Bram Moolenaar8d84ff12017-10-26 16:42:16 +0200758
Bram Moolenaar8d84ff12017-10-26 16:42:16 +0200759 au! BufReadCmd
760endfunc
761
zeertzjq93f72cc2022-08-26 15:34:52 +0100762" BufReadCmd is triggered for a "nofile" buffer. Check all values.
Bram Moolenaarb1d2c812022-08-26 11:55:01 +0100763func Test_BufReadCmdNofile()
zeertzjq93f72cc2022-08-26 15:34:52 +0100764 for val in ['nofile',
765 \ 'nowrite',
766 \ 'acwrite',
767 \ 'quickfix',
768 \ 'help',
769 \ 'terminal',
770 \ 'prompt',
771 \ 'popup',
772 \ ]
773 new somefile
774 exe 'set buftype=' .. val
775 au BufReadCmd somefile call setline(1, 'triggered')
776 edit
777 call assert_equal('triggered', getline(1))
Bram Moolenaarb1d2c812022-08-26 11:55:01 +0100778
zeertzjq93f72cc2022-08-26 15:34:52 +0100779 au! BufReadCmd
780 bwipe!
781 endfor
Bram Moolenaarb1d2c812022-08-26 11:55:01 +0100782endfunc
783
Bram Moolenaarb62cc362016-09-03 16:43:53 +0200784func Test_augroup_deleted()
Bram Moolenaarde653f02016-09-03 16:59:06 +0200785 " This caused a crash before E936 was introduced
Bram Moolenaarb62cc362016-09-03 16:43:53 +0200786 augroup x
Bram Moolenaarde653f02016-09-03 16:59:06 +0200787 call assert_fails('augroup! x', 'E936:')
788 au VimEnter * echo
789 augroup end
Bram Moolenaarb62cc362016-09-03 16:43:53 +0200790 augroup! x
Bram Moolenaar5dc4e2f2020-11-25 14:15:12 +0100791 call assert_match("-Deleted-.*VimEnter", execute('au VimEnter'))
Bram Moolenaarde653f02016-09-03 16:59:06 +0200792 au! VimEnter
Bram Moolenaarb62cc362016-09-03 16:43:53 +0200793endfunc
794
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200795" Tests for autocommands on :close command.
796" This used to be in test13.
797func Test_three_windows()
Bram Moolenaarb3435b02016-09-29 20:54:59 +0200798 " Clean up buffers, because in some cases this function fails.
799 call s:cleanup_buffers()
800
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200801 " Write three files and open them, each in a window.
802 " Then go to next window, with autocommand that deletes the previous one.
803 " Do this twice, writing the file.
804 e! Xtestje1
805 call setline(1, 'testje1')
806 w
807 sp Xtestje2
808 call setline(1, 'testje2')
809 w
810 sp Xtestje3
811 call setline(1, 'testje3')
812 w
813 wincmd w
814 au WinLeave Xtestje2 bwipe
815 wincmd w
816 call assert_equal('Xtestje1', expand('%'))
817
818 au WinLeave Xtestje1 bwipe Xtestje3
819 close
820 call assert_equal('Xtestje1', expand('%'))
821
822 " Test deleting the buffer on a Unload event. If this goes wrong there
823 " will be the ATTENTION prompt.
824 e Xtestje1
825 au!
826 au! BufUnload Xtestje1 bwipe
827 call assert_fails('e Xtestje3', 'E937:')
828 call assert_equal('Xtestje3', expand('%'))
829
830 e Xtestje2
831 sp Xtestje1
832 call assert_fails('e', 'E937:')
Bram Moolenaara997b452018-04-17 23:24:06 +0200833 call assert_equal('Xtestje1', expand('%'))
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200834
835 " Test changing buffers in a BufWipeout autocommand. If this goes wrong
836 " there are ml_line errors and/or a Crash.
837 au!
838 only
839 e Xanother
840 e Xtestje1
841 bwipe Xtestje2
842 bwipe Xtestje3
843 au BufWipeout Xtestje1 buf Xtestje1
844 bwipe
845 call assert_equal('Xanother', expand('%'))
846
847 only
848 help
849 wincmd w
850 1quit
851 call assert_equal('Xanother', expand('%'))
852
853 au!
Bram Moolenaar4520d442017-03-19 16:09:46 +0100854 enew
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200855 call delete('Xtestje1')
856 call delete('Xtestje2')
857 call delete('Xtestje3')
858endfunc
Bram Moolenaare13b9af2017-01-13 22:01:02 +0100859
860func Test_BufEnter()
861 au! BufEnter
862 au Bufenter * let val = val . '+'
863 let g:val = ''
864 split NewFile
865 call assert_equal('+', g:val)
866 bwipe!
867 call assert_equal('++', g:val)
868
869 " Also get BufEnter when editing a directory
Bram Moolenaar6f14da12022-09-07 21:30:44 +0100870 call mkdir('Xbufenterdir', 'D')
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +0100871 split Xbufenterdir
Bram Moolenaare13b9af2017-01-13 22:01:02 +0100872 call assert_equal('+++', g:val)
Bram Moolenaare94260f2017-03-21 15:50:12 +0100873
874 " On MS-Windows we can't edit the directory, make sure we wipe the right
875 " buffer.
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +0100876 bwipe! Xbufenterdir
Bram Moolenaare13b9af2017-01-13 22:01:02 +0100877 au! BufEnter
Bram Moolenaara9b5b852022-08-26 13:16:20 +0100878
879 " Editing a "nofile" buffer doesn't read the file but does trigger BufEnter
zeertzjq93f72cc2022-08-26 15:34:52 +0100880 " for historic reasons. Also test other 'buftype' values.
881 for val in ['nofile',
882 \ 'nowrite',
883 \ 'acwrite',
884 \ 'quickfix',
885 \ 'help',
886 \ 'terminal',
887 \ 'prompt',
888 \ 'popup',
889 \ ]
890 new somefile
891 exe 'set buftype=' .. val
892 au BufEnter somefile call setline(1, 'some text')
893 edit
894 call assert_equal('some text', getline(1))
895 bwipe!
896 au! BufEnter
897 endfor
Bram Moolenaar9fda8152022-11-19 13:14:10 +0000898
899 new
900 new
901 autocmd BufEnter * ++once close
902 call assert_fails('close', 'E1312:')
903
904 au! BufEnter
905 only
Bram Moolenaare13b9af2017-01-13 22:01:02 +0100906endfunc
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100907
908" Closing a window might cause an endless loop
909" E814 for older Vims
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200910func Test_autocmd_bufwipe_in_SessLoadPost()
Bram Moolenaar1d68d9b2017-10-13 22:33:32 +0200911 edit Xtest
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100912 tabnew
Bram Moolenaar1d68d9b2017-10-13 22:33:32 +0200913 file Xsomething
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100914 set noswapfile
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100915 mksession!
916
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200917 let content =<< trim [CODE]
Bram Moolenaar62cd26a2020-10-11 20:08:44 +0200918 call test_override('ui_delay', 10)
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200919 set nocp noswapfile
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100920 let v:swapchoice = "e"
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200921 augroup test_autocmd_sessionload
922 autocmd!
923 autocmd SessionLoadPost * exe bufnr("Xsomething") . "bw!"
924 augroup END
925
926 func WriteErrors()
Bram Moolenaarbe9fc5b2022-09-09 17:09:35 +0100927 call writefile([execute("messages")], "XerrorsBwipe")
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200928 endfunc
929 au VimLeave * call WriteErrors()
930 [CODE]
931
Bram Moolenaare1f3ab72022-09-04 21:29:08 +0100932 call writefile(content, 'Xvimrc', 'D')
Bram Moolenaar93344c22019-08-14 21:12:05 +0200933 call system(GetVimCommand('Xvimrc') .. ' --not-a-term --noplugins -S Session.vim -c cq')
Bram Moolenaarae04a602022-09-09 15:08:10 +0100934 sleep 100m
Bram Moolenaarbe9fc5b2022-09-09 17:09:35 +0100935 let errors = join(readfile('XerrorsBwipe'))
Bram Moolenaare2e40752020-09-04 21:18:46 +0200936 call assert_match('E814:', errors)
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100937
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100938 set swapfile
Bram Moolenaarbe9fc5b2022-09-09 17:09:35 +0100939 for file in ['Session.vim', 'XerrorsBwipe']
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100940 call delete(file)
941 endfor
942endfunc
943
Bram Moolenaar797e63b2021-01-15 16:22:52 +0100944" Using :blast and :ball for many events caused a crash, because b_nwindows was
945" not incremented correctly.
946func Test_autocmd_blast_badd()
947 let content =<< trim [CODE]
948 au BufNew,BufAdd,BufWinEnter,BufEnter,BufLeave,BufWinLeave,BufUnload,VimEnter foo* blast
949 edit foo1
950 au BufNew,BufAdd,BufWinEnter,BufEnter,BufLeave,BufWinLeave,BufUnload,VimEnter foo* ball
951 edit foo2
Bram Moolenaarbe9fc5b2022-09-09 17:09:35 +0100952 call writefile(['OK'], 'XerrorsBlast')
Bram Moolenaar797e63b2021-01-15 16:22:52 +0100953 qall
954 [CODE]
955
Bram Moolenaare1f3ab72022-09-04 21:29:08 +0100956 call writefile(content, 'XblastBall', 'D')
Bram Moolenaar797e63b2021-01-15 16:22:52 +0100957 call system(GetVimCommand() .. ' --clean -S XblastBall')
Bram Moolenaarae04a602022-09-09 15:08:10 +0100958 sleep 100m
Bram Moolenaarbe9fc5b2022-09-09 17:09:35 +0100959 call assert_match('OK', readfile('XerrorsBlast')->join())
Bram Moolenaar797e63b2021-01-15 16:22:52 +0100960
Bram Moolenaarbe9fc5b2022-09-09 17:09:35 +0100961 call delete('XerrorsBlast')
Bram Moolenaar797e63b2021-01-15 16:22:52 +0100962endfunc
963
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100964" SEGV occurs in older versions.
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200965func Test_autocmd_bufwipe_in_SessLoadPost2()
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100966 tabnew
967 set noswapfile
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100968 mksession!
969
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200970 let content =<< trim [CODE]
971 set nocp noswapfile
972 function! DeleteInactiveBufs()
973 tabfirst
974 let tabblist = []
975 for i in range(1, tabpagenr(''$''))
976 call extend(tabblist, tabpagebuflist(i))
977 endfor
978 for b in range(1, bufnr(''$''))
979 if bufexists(b) && buflisted(b) && (index(tabblist, b) == -1 || bufname(b) =~# ''^$'')
980 exec ''bwipeout '' . b
981 endif
982 endfor
983 echomsg "SessionLoadPost DONE"
984 endfunction
985 au SessionLoadPost * call DeleteInactiveBufs()
986
987 func WriteErrors()
Bram Moolenaarbe9fc5b2022-09-09 17:09:35 +0100988 call writefile([execute("messages")], "XerrorsPost")
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200989 endfunc
990 au VimLeave * call WriteErrors()
991 [CODE]
992
Bram Moolenaare1f3ab72022-09-04 21:29:08 +0100993 call writefile(content, 'Xvimrc', 'D')
Bram Moolenaar93344c22019-08-14 21:12:05 +0200994 call system(GetVimCommand('Xvimrc') .. ' --not-a-term --noplugins -S Session.vim -c cq')
Bram Moolenaarae04a602022-09-09 15:08:10 +0100995 sleep 100m
Bram Moolenaarbe9fc5b2022-09-09 17:09:35 +0100996 let errors = join(readfile('XerrorsPost'))
Bram Moolenaare94260f2017-03-21 15:50:12 +0100997 " This probably only ever matches on unix.
998 call assert_notmatch('Caught deadly signal SEGV', errors)
999 call assert_match('SessionLoadPost DONE', errors)
Bram Moolenaar8c752bd2017-03-19 17:09:56 +01001000
Bram Moolenaar8c752bd2017-03-19 17:09:56 +01001001 set swapfile
Bram Moolenaarbe9fc5b2022-09-09 17:09:35 +01001002 for file in ['Session.vim', 'XerrorsPost']
Bram Moolenaar8c752bd2017-03-19 17:09:56 +01001003 call delete(file)
1004 endfor
1005endfunc
Bram Moolenaarfaf29d72017-07-09 11:07:16 +02001006
1007func Test_empty_doau()
1008 doau \|
1009endfunc
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001010
1011func s:AutoCommandOptionSet(match)
Bram Moolenaard7c96872019-06-15 17:12:48 +02001012 let template = "Option: <%s>, OldVal: <%s>, OldValLocal: <%s>, OldValGlobal: <%s>, NewVal: <%s>, Scope: <%s>, Command: <%s>\n"
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001013 let item = remove(g:options, 0)
Bram Moolenaard7c96872019-06-15 17:12:48 +02001014 let expected = printf(template, item[0], item[1], item[2], item[3], item[4], item[5], item[6])
1015 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 +02001016 let g:opt = [expected, actual]
1017 "call assert_equal(expected, actual)
1018endfunc
1019
1020func Test_OptionSet()
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +02001021 CheckOption autochdir
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001022
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +02001023 badd test_autocmd.vim
1024
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001025 call test_override('starting', 1)
1026 set nocp
1027 au OptionSet * :call s:AutoCommandOptionSet(expand("<amatch>"))
1028
1029 " 1: Setting number option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001030 let g:options = [['number', 0, 0, 0, 1, 'global', 'set']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001031 set nu
1032 call assert_equal([], g:options)
1033 call assert_equal(g:opt[0], g:opt[1])
1034
1035 " 2: Setting local number option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001036 let g:options = [['number', 1, 1, '', 0, 'local', 'setlocal']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001037 setlocal nonu
1038 call assert_equal([], g:options)
1039 call assert_equal(g:opt[0], g:opt[1])
1040
1041 " 3: Setting global number option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001042 let g:options = [['number', 1, '', 1, 0, 'global', 'setglobal']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001043 setglobal nonu
1044 call assert_equal([], g:options)
1045 call assert_equal(g:opt[0], g:opt[1])
1046
1047 " 4: Setting local autoindent option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001048 let g:options = [['autoindent', 0, 0, '', 1, 'local', 'setlocal']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001049 setlocal ai
1050 call assert_equal([], g:options)
1051 call assert_equal(g:opt[0], g:opt[1])
1052
1053 " 5: Setting global autoindent option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001054 let g:options = [['autoindent', 0, '', 0, 1, 'global', 'setglobal']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001055 setglobal ai
1056 call assert_equal([], g:options)
1057 call assert_equal(g:opt[0], g:opt[1])
1058
1059 " 6: Setting global autoindent option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001060 let g:options = [['autoindent', 1, 1, 1, 0, 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001061 set ai!
1062 call assert_equal([], g:options)
1063 call assert_equal(g:opt[0], g:opt[1])
1064
1065 " 6a: Setting global autoindent option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001066 let g:options = [['autoindent', 1, 1, 0, 0, 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001067 noa setlocal ai
1068 noa setglobal noai
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001069 set ai!
1070 call assert_equal([], g:options)
1071 call assert_equal(g:opt[0], g:opt[1])
1072
1073 " Should not print anything, use :noa
1074 " 7: don't trigger OptionSet"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001075 let g:options = [['invalid', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001076 noa set nonu
Bram Moolenaard7c96872019-06-15 17:12:48 +02001077 call assert_equal([['invalid', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid']], g:options)
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001078 call assert_equal(g:opt[0], g:opt[1])
1079
1080 " 8: Setting several global list and number option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001081 let g:options = [['list', 0, 0, 0, 1, 'global', 'set'], ['number', 0, 0, 0, 1, 'global', 'set']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001082 set list nu
1083 call assert_equal([], g:options)
1084 call assert_equal(g:opt[0], g:opt[1])
1085
1086 " 9: don't trigger OptionSet"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001087 let g:options = [['invalid', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid'], ['invalid', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001088 noa set nolist nonu
Bram Moolenaard7c96872019-06-15 17:12:48 +02001089 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 +02001090 call assert_equal(g:opt[0], g:opt[1])
1091
1092 " 10: Setting global acd"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001093 let g:options = [['autochdir', 0, 0, '', 1, 'local', 'setlocal']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001094 setlocal acd
1095 call assert_equal([], g:options)
1096 call assert_equal(g:opt[0], g:opt[1])
1097
1098 " 11: Setting global autoread (also sets local value)"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001099 let g:options = [['autoread', 0, 0, 0, 1, 'global', 'set']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001100 set ar
1101 call assert_equal([], g:options)
1102 call assert_equal(g:opt[0], g:opt[1])
1103
1104 " 12: Setting local autoread"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001105 let g:options = [['autoread', 1, 1, '', 1, 'local', 'setlocal']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001106 setlocal ar
1107 call assert_equal([], g:options)
1108 call assert_equal(g:opt[0], g:opt[1])
1109
1110 " 13: Setting global autoread"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001111 let g:options = [['autoread', 1, '', 1, 0, 'global', 'setglobal']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001112 setglobal invar
1113 call assert_equal([], g:options)
1114 call assert_equal(g:opt[0], g:opt[1])
1115
1116 " 14: Setting option backspace through :let"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001117 let g:options = [['backspace', '', '', '', 'eol,indent,start', 'global', 'set']]
1118 let &bs = "eol,indent,start"
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001119 call assert_equal([], g:options)
1120 call assert_equal(g:opt[0], g:opt[1])
1121
1122 " 15: Setting option backspace through setbufvar()"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001123 let g:options = [['backup', 0, 0, '', 1, 'local', 'setlocal']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001124 " try twice, first time, shouldn't trigger because option name is invalid,
1125 " second time, it should trigger
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +02001126 let bnum = bufnr('%')
Bram Moolenaare2e40752020-09-04 21:18:46 +02001127 call assert_fails("call setbufvar(bnum, '&l:bk', 1)", 'E355:')
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001128 " should trigger, use correct option name
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +02001129 call setbufvar(bnum, '&backup', 1)
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001130 call assert_equal([], g:options)
1131 call assert_equal(g:opt[0], g:opt[1])
1132
1133 " 16: Setting number option using setwinvar"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001134 let g:options = [['number', 0, 0, '', 1, 'local', 'setlocal']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001135 call setwinvar(0, '&number', 1)
1136 call assert_equal([], g:options)
1137 call assert_equal(g:opt[0], g:opt[1])
1138
1139 " 17: Setting key option, shouldn't trigger"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001140 let g:options = [['key', 'invalid', 'invalid1', 'invalid2', 'invalid3', 'invalid4', 'invalid5']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001141 setlocal key=blah
1142 setlocal key=
Bram Moolenaard7c96872019-06-15 17:12:48 +02001143 call assert_equal([['key', 'invalid', 'invalid1', 'invalid2', 'invalid3', 'invalid4', 'invalid5']], g:options)
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001144 call assert_equal(g:opt[0], g:opt[1])
1145
Bram Moolenaard7c96872019-06-15 17:12:48 +02001146
1147 " 18a: Setting string global option"
1148 let oldval = &backupext
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001149 let g:options = [['backupext', oldval, oldval, oldval, 'foo', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001150 set backupext=foo
1151 call assert_equal([], g:options)
1152 call assert_equal(g:opt[0], g:opt[1])
1153
1154 " 18b: Resetting string global option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001155 let g:options = [['backupext', 'foo', 'foo', 'foo', oldval, 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001156 set backupext&
1157 call assert_equal([], g:options)
1158 call assert_equal(g:opt[0], g:opt[1])
1159
1160 " 18c: Setting global string global option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001161 let g:options = [['backupext', oldval, '', oldval, 'bar', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001162 setglobal backupext=bar
1163 call assert_equal([], g:options)
1164 call assert_equal(g:opt[0], g:opt[1])
1165
1166 " 18d: Setting local string global option"
1167 " As this is a global option this sets the global value even though
1168 " :setlocal is used!
1169 noa set backupext& " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001170 let g:options = [['backupext', oldval, oldval, '', 'baz', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001171 setlocal backupext=baz
1172 call assert_equal([], g:options)
1173 call assert_equal(g:opt[0], g:opt[1])
1174
1175 " 18e: Setting again string global option"
1176 noa setglobal backupext=ext_global " Reset global and local value (without triggering autocmd)
1177 noa setlocal backupext=ext_local " Sets the global(!) value!
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001178 let g:options = [['backupext', 'ext_local', 'ext_local', 'ext_local', 'fuu', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001179 set backupext=fuu
1180 call assert_equal([], g:options)
1181 call assert_equal(g:opt[0], g:opt[1])
1182
1183
zeertzjqb811de52021-10-21 10:50:44 +01001184 " 19a: Setting string global-local (to buffer) option"
Bram Moolenaar8efa0262017-08-20 15:47:20 +02001185 let oldval = &tags
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001186 let g:options = [['tags', oldval, oldval, oldval, 'tagpath', 'global', 'set']]
Bram Moolenaar8efa0262017-08-20 15:47:20 +02001187 set tags=tagpath
1188 call assert_equal([], g:options)
1189 call assert_equal(g:opt[0], g:opt[1])
1190
zeertzjqb811de52021-10-21 10:50:44 +01001191 " 19b: Resetting string global-local (to buffer) option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001192 let g:options = [['tags', 'tagpath', 'tagpath', 'tagpath', oldval, 'global', 'set']]
Bram Moolenaar8efa0262017-08-20 15:47:20 +02001193 set tags&
1194 call assert_equal([], g:options)
1195 call assert_equal(g:opt[0], g:opt[1])
1196
zeertzjqb811de52021-10-21 10:50:44 +01001197 " 19c: Setting global string global-local (to buffer) option "
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001198 let g:options = [['tags', oldval, '', oldval, 'tagpath1', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001199 setglobal tags=tagpath1
1200 call assert_equal([], g:options)
1201 call assert_equal(g:opt[0], g:opt[1])
1202
zeertzjqb811de52021-10-21 10:50:44 +01001203 " 19d: Setting local string global-local (to buffer) option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001204 let g:options = [['tags', 'tagpath1', 'tagpath1', '', 'tagpath2', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001205 setlocal tags=tagpath2
1206 call assert_equal([], g:options)
1207 call assert_equal(g:opt[0], g:opt[1])
1208
zeertzjqb811de52021-10-21 10:50:44 +01001209 " 19e: Setting again string global-local (to buffer) option"
1210 " Note: v:option_old is the old global value for global-local string options
Bram Moolenaard7c96872019-06-15 17:12:48 +02001211 " but the old local value for all other kinds of options.
1212 noa setglobal tags=tag_global " Reset global and local value (without triggering autocmd)
1213 noa setlocal tags=tag_local
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001214 let g:options = [['tags', 'tag_global', 'tag_local', 'tag_global', 'tagpath', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001215 set tags=tagpath
1216 call assert_equal([], g:options)
1217 call assert_equal(g:opt[0], g:opt[1])
1218
zeertzjqb811de52021-10-21 10:50:44 +01001219 " 19f: Setting string global-local (to buffer) option to an empty string"
1220 " Note: v:option_old is the old global value for global-local string options
Bram Moolenaard7c96872019-06-15 17:12:48 +02001221 " but the old local value for all other kinds of options.
1222 noa set tags=tag_global " Reset global and local value (without triggering autocmd)
1223 noa setlocal tags= " empty string
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001224 let g:options = [['tags', 'tag_global', '', 'tag_global', 'tagpath', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001225 set tags=tagpath
1226 call assert_equal([], g:options)
1227 call assert_equal(g:opt[0], g:opt[1])
1228
1229
1230 " 20a: Setting string local (to buffer) option"
1231 let oldval = &spelllang
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001232 let g:options = [['spelllang', oldval, oldval, oldval, 'elvish,klingon', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001233 set spelllang=elvish,klingon
1234 call assert_equal([], g:options)
1235 call assert_equal(g:opt[0], g:opt[1])
1236
1237 " 20b: Resetting string local (to buffer) option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001238 let g:options = [['spelllang', 'elvish,klingon', 'elvish,klingon', 'elvish,klingon', oldval, 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001239 set spelllang&
1240 call assert_equal([], g:options)
1241 call assert_equal(g:opt[0], g:opt[1])
1242
1243 " 20c: Setting global string local (to buffer) option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001244 let g:options = [['spelllang', oldval, '', oldval, 'elvish', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001245 setglobal spelllang=elvish
1246 call assert_equal([], g:options)
1247 call assert_equal(g:opt[0], g:opt[1])
1248
1249 " 20d: Setting local string local (to buffer) option"
1250 noa set spelllang& " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001251 let g:options = [['spelllang', oldval, oldval, '', 'klingon', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001252 setlocal spelllang=klingon
1253 call assert_equal([], g:options)
1254 call assert_equal(g:opt[0], g:opt[1])
1255
1256 " 20e: Setting again string local (to buffer) option"
zeertzjqb811de52021-10-21 10:50:44 +01001257 " Note: v:option_old is the old global value for global-local string options
Bram Moolenaard7c96872019-06-15 17:12:48 +02001258 " but the old local value for all other kinds of options.
1259 noa setglobal spelllang=spellglobal " Reset global and local value (without triggering autocmd)
1260 noa setlocal spelllang=spelllocal
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001261 let g:options = [['spelllang', 'spelllocal', 'spelllocal', 'spellglobal', 'foo', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001262 set spelllang=foo
1263 call assert_equal([], g:options)
1264 call assert_equal(g:opt[0], g:opt[1])
1265
1266
zeertzjqb811de52021-10-21 10:50:44 +01001267 " 21a: Setting string global-local (to window) option"
Bram Moolenaard7c96872019-06-15 17:12:48 +02001268 let oldval = &statusline
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001269 let g:options = [['statusline', oldval, oldval, oldval, 'foo', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001270 set statusline=foo
1271 call assert_equal([], g:options)
1272 call assert_equal(g:opt[0], g:opt[1])
1273
zeertzjqb811de52021-10-21 10:50:44 +01001274 " 21b: Resetting string global-local (to window) option"
1275 " Note: v:option_old is the old global value for global-local string options
Bram Moolenaard7c96872019-06-15 17:12:48 +02001276 " but the old local value for all other kinds of options.
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001277 let g:options = [['statusline', 'foo', 'foo', 'foo', oldval, 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001278 set statusline&
1279 call assert_equal([], g:options)
1280 call assert_equal(g:opt[0], g:opt[1])
1281
zeertzjqb811de52021-10-21 10:50:44 +01001282 " 21c: Setting global string global-local (to window) option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001283 let g:options = [['statusline', oldval, '', oldval, 'bar', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001284 setglobal statusline=bar
1285 call assert_equal([], g:options)
1286 call assert_equal(g:opt[0], g:opt[1])
1287
zeertzjqb811de52021-10-21 10:50:44 +01001288 " 21d: Setting local string global-local (to window) option"
Bram Moolenaard7c96872019-06-15 17:12:48 +02001289 noa set statusline& " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001290 let g:options = [['statusline', oldval, oldval, '', 'baz', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001291 setlocal statusline=baz
1292 call assert_equal([], g:options)
1293 call assert_equal(g:opt[0], g:opt[1])
1294
zeertzjqb811de52021-10-21 10:50:44 +01001295 " 21e: Setting again string global-local (to window) option"
1296 " Note: v:option_old is the old global value for global-local string options
Bram Moolenaard7c96872019-06-15 17:12:48 +02001297 " but the old local value for all other kinds of options.
1298 noa setglobal statusline=bar " Reset global and local value (without triggering autocmd)
1299 noa setlocal statusline=baz
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001300 let g:options = [['statusline', 'bar', 'baz', 'bar', 'foo', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001301 set statusline=foo
1302 call assert_equal([], g:options)
1303 call assert_equal(g:opt[0], g:opt[1])
1304
1305
1306 " 22a: Setting string local (to window) option"
1307 let oldval = &foldignore
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001308 let g:options = [['foldignore', oldval, oldval, oldval, 'fo', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001309 set foldignore=fo
1310 call assert_equal([], g:options)
1311 call assert_equal(g:opt[0], g:opt[1])
1312
1313 " 22b: Resetting string local (to window) option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001314 let g:options = [['foldignore', 'fo', 'fo', 'fo', oldval, 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001315 set foldignore&
1316 call assert_equal([], g:options)
1317 call assert_equal(g:opt[0], g:opt[1])
1318
1319 " 22c: Setting global string local (to window) option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001320 let g:options = [['foldignore', oldval, '', oldval, 'bar', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001321 setglobal foldignore=bar
1322 call assert_equal([], g:options)
1323 call assert_equal(g:opt[0], g:opt[1])
1324
1325 " 22d: Setting local string local (to window) option"
1326 noa set foldignore& " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001327 let g:options = [['foldignore', oldval, oldval, '', 'baz', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001328 setlocal foldignore=baz
1329 call assert_equal([], g:options)
1330 call assert_equal(g:opt[0], g:opt[1])
1331
1332 " 22e: Setting again string local (to window) option"
1333 noa setglobal foldignore=glob " Reset global and local value (without triggering autocmd)
1334 noa setlocal foldignore=loc
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001335 let g:options = [['foldignore', 'loc', 'loc', 'glob', 'fo', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001336 set foldignore=fo
1337 call assert_equal([], g:options)
1338 call assert_equal(g:opt[0], g:opt[1])
1339
1340
zeertzjqb811de52021-10-21 10:50:44 +01001341 " 23a: Setting global number global option"
Bram Moolenaard7c96872019-06-15 17:12:48 +02001342 noa setglobal cmdheight=8 " Reset global and local value (without triggering autocmd)
1343 noa setlocal cmdheight=1 " Sets the global(!) value!
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001344 let g:options = [['cmdheight', '1', '', '1', '2', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001345 setglobal cmdheight=2
1346 call assert_equal([], g:options)
1347 call assert_equal(g:opt[0], g:opt[1])
1348
1349 " 23b: Setting local number global option"
1350 noa setglobal cmdheight=8 " Reset global and local value (without triggering autocmd)
1351 noa setlocal cmdheight=1 " Sets the global(!) value!
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001352 let g:options = [['cmdheight', '1', '1', '', '2', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001353 setlocal cmdheight=2
1354 call assert_equal([], g:options)
1355 call assert_equal(g:opt[0], g:opt[1])
1356
1357 " 23c: Setting again number global option"
1358 noa setglobal cmdheight=8 " Reset global and local value (without triggering autocmd)
1359 noa setlocal cmdheight=1 " Sets the global(!) value!
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001360 let g:options = [['cmdheight', '1', '1', '1', '2', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001361 set cmdheight=2
1362 call assert_equal([], g:options)
1363 call assert_equal(g:opt[0], g:opt[1])
1364
1365 " 23d: Setting again number global option"
1366 noa set cmdheight=8 " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001367 let g:options = [['cmdheight', '8', '8', '8', '2', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001368 set cmdheight=2
1369 call assert_equal([], g:options)
1370 call assert_equal(g:opt[0], g:opt[1])
1371
1372
1373 " 24a: Setting global number global-local (to buffer) option"
1374 noa setglobal undolevels=8 " Reset global and local value (without triggering autocmd)
1375 noa setlocal undolevels=1
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001376 let g:options = [['undolevels', '8', '', '8', '2', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001377 setglobal undolevels=2
1378 call assert_equal([], g:options)
1379 call assert_equal(g:opt[0], g:opt[1])
1380
1381 " 24b: Setting local number global-local (to buffer) option"
1382 noa setglobal undolevels=8 " Reset global and local value (without triggering autocmd)
1383 noa setlocal undolevels=1
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001384 let g:options = [['undolevels', '1', '1', '', '2', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001385 setlocal undolevels=2
1386 call assert_equal([], g:options)
1387 call assert_equal(g:opt[0], g:opt[1])
1388
1389 " 24c: Setting again number global-local (to buffer) option"
1390 noa setglobal undolevels=8 " Reset global and local value (without triggering autocmd)
1391 noa setlocal undolevels=1
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001392 let g:options = [['undolevels', '1', '1', '8', '2', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001393 set undolevels=2
1394 call assert_equal([], g:options)
1395 call assert_equal(g:opt[0], g:opt[1])
1396
1397 " 24d: Setting again global number global-local (to buffer) option"
1398 noa set undolevels=8 " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001399 let g:options = [['undolevels', '8', '8', '8', '2', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001400 set undolevels=2
1401 call assert_equal([], g:options)
1402 call assert_equal(g:opt[0], g:opt[1])
1403
1404
1405 " 25a: Setting global number local (to buffer) option"
1406 noa setglobal wrapmargin=8 " Reset global and local value (without triggering autocmd)
1407 noa setlocal wrapmargin=1
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001408 let g:options = [['wrapmargin', '8', '', '8', '2', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001409 setglobal wrapmargin=2
1410 call assert_equal([], g:options)
1411 call assert_equal(g:opt[0], g:opt[1])
1412
1413 " 25b: Setting local number local (to buffer) option"
1414 noa setglobal wrapmargin=8 " Reset global and local value (without triggering autocmd)
1415 noa setlocal wrapmargin=1
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001416 let g:options = [['wrapmargin', '1', '1', '', '2', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001417 setlocal wrapmargin=2
1418 call assert_equal([], g:options)
1419 call assert_equal(g:opt[0], g:opt[1])
1420
1421 " 25c: Setting again number local (to buffer) option"
1422 noa setglobal wrapmargin=8 " Reset global and local value (without triggering autocmd)
1423 noa setlocal wrapmargin=1
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001424 let g:options = [['wrapmargin', '1', '1', '8', '2', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001425 set wrapmargin=2
1426 call assert_equal([], g:options)
1427 call assert_equal(g:opt[0], g:opt[1])
1428
1429 " 25d: Setting again global number local (to buffer) option"
1430 noa set wrapmargin=8 " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001431 let g:options = [['wrapmargin', '8', '8', '8', '2', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001432 set wrapmargin=2
1433 call assert_equal([], g:options)
1434 call assert_equal(g:opt[0], g:opt[1])
1435
1436
1437 " 26: Setting number global-local (to window) option.
1438 " Such option does currently not exist.
1439
1440
1441 " 27a: Setting global number local (to window) option"
1442 noa setglobal foldcolumn=8 " Reset global and local value (without triggering autocmd)
1443 noa setlocal foldcolumn=1
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001444 let g:options = [['foldcolumn', '8', '', '8', '2', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001445 setglobal foldcolumn=2
1446 call assert_equal([], g:options)
1447 call assert_equal(g:opt[0], g:opt[1])
1448
1449 " 27b: Setting local number local (to window) option"
1450 noa setglobal foldcolumn=8 " Reset global and local value (without triggering autocmd)
1451 noa setlocal foldcolumn=1
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001452 let g:options = [['foldcolumn', '1', '1', '', '2', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001453 setlocal foldcolumn=2
1454 call assert_equal([], g:options)
1455 call assert_equal(g:opt[0], g:opt[1])
1456
1457 " 27c: Setting again number local (to window) option"
1458 noa setglobal foldcolumn=8 " Reset global and local value (without triggering autocmd)
1459 noa setlocal foldcolumn=1
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001460 let g:options = [['foldcolumn', '1', '1', '8', '2', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001461 set foldcolumn=2
1462 call assert_equal([], g:options)
1463 call assert_equal(g:opt[0], g:opt[1])
1464
zeertzjqb811de52021-10-21 10:50:44 +01001465 " 27d: Setting again global number local (to window) option"
Bram Moolenaard7c96872019-06-15 17:12:48 +02001466 noa set foldcolumn=8 " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001467 let g:options = [['foldcolumn', '8', '8', '8', '2', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001468 set foldcolumn=2
1469 call assert_equal([], g:options)
1470 call assert_equal(g:opt[0], g:opt[1])
1471
1472
1473 " 28a: Setting global boolean global option"
1474 noa setglobal nowrapscan " Reset global and local value (without triggering autocmd)
1475 noa setlocal wrapscan " Sets the global(!) value!
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001476 let g:options = [['wrapscan', '1', '', '1', '0', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001477 setglobal nowrapscan
1478 call assert_equal([], g:options)
1479 call assert_equal(g:opt[0], g:opt[1])
1480
1481 " 28b: Setting local boolean global option"
1482 noa setglobal nowrapscan " Reset global and local value (without triggering autocmd)
1483 noa setlocal wrapscan " Sets the global(!) value!
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001484 let g:options = [['wrapscan', '1', '1', '', '0', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001485 setlocal nowrapscan
1486 call assert_equal([], g:options)
1487 call assert_equal(g:opt[0], g:opt[1])
1488
1489 " 28c: Setting again boolean global option"
1490 noa setglobal nowrapscan " Reset global and local value (without triggering autocmd)
1491 noa setlocal wrapscan " Sets the global(!) value!
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001492 let g:options = [['wrapscan', '1', '1', '1', '0', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001493 set nowrapscan
1494 call assert_equal([], g:options)
1495 call assert_equal(g:opt[0], g:opt[1])
1496
1497 " 28d: Setting again global boolean global option"
1498 noa set nowrapscan " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001499 let g:options = [['wrapscan', '0', '0', '0', '1', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001500 set wrapscan
1501 call assert_equal([], g:options)
1502 call assert_equal(g:opt[0], g:opt[1])
1503
1504
1505 " 29a: Setting global boolean global-local (to buffer) option"
1506 noa setglobal noautoread " Reset global and local value (without triggering autocmd)
1507 noa setlocal autoread
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001508 let g:options = [['autoread', '0', '', '0', '1', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001509 setglobal autoread
1510 call assert_equal([], g:options)
1511 call assert_equal(g:opt[0], g:opt[1])
1512
1513 " 29b: Setting local boolean global-local (to buffer) option"
1514 noa setglobal noautoread " Reset global and local value (without triggering autocmd)
1515 noa setlocal autoread
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001516 let g:options = [['autoread', '1', '1', '', '0', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001517 setlocal noautoread
1518 call assert_equal([], g:options)
1519 call assert_equal(g:opt[0], g:opt[1])
1520
1521 " 29c: Setting again boolean global-local (to buffer) option"
1522 noa setglobal noautoread " Reset global and local value (without triggering autocmd)
1523 noa setlocal autoread
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001524 let g:options = [['autoread', '1', '1', '0', '1', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001525 set autoread
1526 call assert_equal([], g:options)
1527 call assert_equal(g:opt[0], g:opt[1])
1528
1529 " 29d: Setting again global boolean global-local (to buffer) option"
1530 noa set noautoread " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001531 let g:options = [['autoread', '0', '0', '0', '1', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001532 set autoread
1533 call assert_equal([], g:options)
1534 call assert_equal(g:opt[0], g:opt[1])
1535
1536
1537 " 30a: Setting global boolean local (to buffer) option"
1538 noa setglobal nocindent " Reset global and local value (without triggering autocmd)
1539 noa setlocal cindent
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001540 let g:options = [['cindent', '0', '', '0', '1', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001541 setglobal cindent
1542 call assert_equal([], g:options)
1543 call assert_equal(g:opt[0], g:opt[1])
1544
1545 " 30b: Setting local boolean local (to buffer) option"
1546 noa setglobal nocindent " Reset global and local value (without triggering autocmd)
1547 noa setlocal cindent
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001548 let g:options = [['cindent', '1', '1', '', '0', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001549 setlocal nocindent
1550 call assert_equal([], g:options)
1551 call assert_equal(g:opt[0], g:opt[1])
1552
1553 " 30c: Setting again boolean local (to buffer) option"
1554 noa setglobal nocindent " Reset global and local value (without triggering autocmd)
1555 noa setlocal cindent
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001556 let g:options = [['cindent', '1', '1', '0', '1', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001557 set cindent
1558 call assert_equal([], g:options)
1559 call assert_equal(g:opt[0], g:opt[1])
1560
1561 " 30d: Setting again global boolean local (to buffer) option"
1562 noa set nocindent " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001563 let g:options = [['cindent', '0', '0', '0', '1', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001564 set cindent
1565 call assert_equal([], g:options)
1566 call assert_equal(g:opt[0], g:opt[1])
1567
1568
1569 " 31: Setting boolean global-local (to window) option
1570 " Currently no such option exists.
1571
1572
1573 " 32a: Setting global boolean local (to window) option"
1574 noa setglobal nocursorcolumn " Reset global and local value (without triggering autocmd)
1575 noa setlocal cursorcolumn
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001576 let g:options = [['cursorcolumn', '0', '', '0', '1', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001577 setglobal cursorcolumn
1578 call assert_equal([], g:options)
1579 call assert_equal(g:opt[0], g:opt[1])
1580
1581 " 32b: Setting local boolean local (to window) option"
1582 noa setglobal nocursorcolumn " Reset global and local value (without triggering autocmd)
1583 noa setlocal cursorcolumn
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001584 let g:options = [['cursorcolumn', '1', '1', '', '0', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001585 setlocal nocursorcolumn
1586 call assert_equal([], g:options)
1587 call assert_equal(g:opt[0], g:opt[1])
1588
1589 " 32c: Setting again boolean local (to window) option"
1590 noa setglobal nocursorcolumn " Reset global and local value (without triggering autocmd)
1591 noa setlocal cursorcolumn
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001592 let g:options = [['cursorcolumn', '1', '1', '0', '1', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001593 set cursorcolumn
1594 call assert_equal([], g:options)
1595 call assert_equal(g:opt[0], g:opt[1])
1596
1597 " 32d: Setting again global boolean local (to window) option"
1598 noa set nocursorcolumn " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001599 let g:options = [['cursorcolumn', '0', '0', '0', '1', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001600 set cursorcolumn
1601 call assert_equal([], g:options)
1602 call assert_equal(g:opt[0], g:opt[1])
1603
1604
Bram Moolenaar1bc353b2019-09-01 14:45:28 +02001605 " 33: Test autocommands when an option value is converted internally.
Bram Moolenaard7c96872019-06-15 17:12:48 +02001606 noa set backspace=1 " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001607 let g:options = [['backspace', 'indent,eol', 'indent,eol', 'indent,eol', '2', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001608 set backspace=2
1609 call assert_equal([], g:options)
1610 call assert_equal(g:opt[0], g:opt[1])
1611
1612
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001613 " Cleanup
1614 au! OptionSet
Bram Moolenaar0331faf2019-06-15 18:40:37 +02001615 " set tags&
Bram Moolenaard7c96872019-06-15 17:12:48 +02001616 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 +02001617 exe printf(":set %s&vim", opt)
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001618 endfor
1619 call test_override('starting', 0)
1620 delfunc! AutoCommandOptionSet
1621endfunc
1622
1623func Test_OptionSet_diffmode()
1624 call test_override('starting', 1)
Bram Moolenaar26d98212019-01-27 22:32:55 +01001625 " 18: Changing an option when entering diff mode
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001626 new
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001627 au OptionSet diff :let &l:cul = v:option_new
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001628
1629 call setline(1, ['buffer 1', 'line2', 'line3', 'line4'])
1630 call assert_equal(0, &l:cul)
1631 diffthis
1632 call assert_equal(1, &l:cul)
1633
1634 vnew
1635 call setline(1, ['buffer 2', 'line 2', 'line 3', 'line4'])
1636 call assert_equal(0, &l:cul)
1637 diffthis
1638 call assert_equal(1, &l:cul)
1639
1640 diffoff
1641 call assert_equal(0, &l:cul)
1642 call assert_equal(1, getwinvar(2, '&l:cul'))
1643 bw!
1644
1645 call assert_equal(1, &l:cul)
1646 diffoff!
1647 call assert_equal(0, &l:cul)
1648 call assert_equal(0, getwinvar(1, '&l:cul'))
1649 bw!
1650
1651 " Cleanup
1652 au! OptionSet
1653 call test_override('starting', 0)
1654endfunc
1655
1656func Test_OptionSet_diffmode_close()
1657 call test_override('starting', 1)
1658 " 19: Try to close the current window when entering diff mode
1659 " should not segfault
1660 new
1661 au OptionSet diff close
1662
1663 call setline(1, ['buffer 1', 'line2', 'line3', 'line4'])
Bram Moolenaare2e40752020-09-04 21:18:46 +02001664 call assert_fails(':diffthis', 'E788:')
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001665 call assert_equal(1, &diff)
1666 vnew
1667 call setline(1, ['buffer 2', 'line 2', 'line 3', 'line4'])
Bram Moolenaare2e40752020-09-04 21:18:46 +02001668 call assert_fails(':diffthis', 'E788:')
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001669 call assert_equal(1, &diff)
Bram Moolenaara9aa86f2019-11-10 21:25:45 +01001670 set diffopt-=closeoff
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001671 bw!
Bram Moolenaare2e40752020-09-04 21:18:46 +02001672 call assert_fails(':diffoff!', 'E788:')
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001673 bw!
1674
1675 " Cleanup
1676 au! OptionSet
1677 call test_override('starting', 0)
1678 "delfunc! AutoCommandOptionSet
1679endfunc
Bram Moolenaar4a137b42017-08-04 22:37:11 +02001680
1681" Test for Bufleave autocommand that deletes the buffer we are about to edit.
1682func Test_BufleaveWithDelete()
Bram Moolenaare7cda972022-08-29 11:02:59 +01001683 new | edit XbufLeave1
Bram Moolenaar4a137b42017-08-04 22:37:11 +02001684
1685 augroup test_bufleavewithdelete
1686 autocmd!
Bram Moolenaare7cda972022-08-29 11:02:59 +01001687 autocmd BufLeave XbufLeave1 bwipe XbufLeave2
Bram Moolenaar4a137b42017-08-04 22:37:11 +02001688 augroup END
1689
Bram Moolenaare7cda972022-08-29 11:02:59 +01001690 call assert_fails('edit XbufLeave2', 'E143:')
1691 call assert_equal('XbufLeave1', bufname('%'))
Bram Moolenaar4a137b42017-08-04 22:37:11 +02001692
Bram Moolenaare7cda972022-08-29 11:02:59 +01001693 autocmd! test_bufleavewithdelete BufLeave XbufLeave1
Bram Moolenaar4a137b42017-08-04 22:37:11 +02001694 augroup! test_bufleavewithdelete
1695
1696 new
Bram Moolenaare7cda972022-08-29 11:02:59 +01001697 bwipe! XbufLeave1
Bram Moolenaar4a137b42017-08-04 22:37:11 +02001698endfunc
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +02001699
1700" Test for autocommand that changes the buffer list, when doing ":ball".
1701func Test_Acmd_BufAll()
1702 enew!
1703 %bwipe!
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01001704 call writefile(['Test file Xxx1'], 'Xxx1', 'D')
1705 call writefile(['Test file Xxx2'], 'Xxx2', 'D')
1706 call writefile(['Test file Xxx3'], 'Xxx3', 'D')
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +02001707
1708 " Add three files to the buffer list
1709 split Xxx1
1710 close
1711 split Xxx2
1712 close
1713 split Xxx3
1714 close
1715
1716 " Wipe the buffer when the buffer is opened
1717 au BufReadPost Xxx2 bwipe
1718
1719 call append(0, 'Test file Xxx4')
1720 ball
1721
1722 call assert_equal(2, winnr('$'))
1723 call assert_equal('Xxx1', bufname(winbufnr(winnr('$'))))
1724 wincmd t
1725
1726 au! BufReadPost
1727 %bwipe!
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +02001728 enew! | only
1729endfunc
1730
1731" Test for autocommand that changes current buffer on BufEnter event.
1732" Check if modelines are interpreted for the correct buffer.
1733func Test_Acmd_BufEnter()
1734 %bwipe!
1735 call writefile(['start of test file Xxx1',
1736 \ "\<Tab>this is a test",
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01001737 \ 'end of test file Xxx1'], 'Xxx1', 'D')
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +02001738 call writefile(['start of test file Xxx2',
1739 \ 'vim: set noai :',
1740 \ "\<Tab>this is a test",
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01001741 \ 'end of test file Xxx2'], 'Xxx2', 'D')
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +02001742
1743 au BufEnter Xxx2 brew
1744 set ai modeline modelines=3
1745 edit Xxx1
1746 " edit Xxx2, autocmd will do :brew
1747 edit Xxx2
1748 exe "normal G?this is a\<CR>"
1749 " Append text with autoindent to this file
1750 normal othis should be auto-indented
1751 call assert_equal("\<Tab>this should be auto-indented", getline('.'))
1752 call assert_equal(3, line('.'))
1753 " Remove autocmd and edit Xxx2 again
1754 au! BufEnter Xxx2
1755 buf! Xxx2
1756 exe "normal G?this is a\<CR>"
1757 " append text without autoindent to Xxx
1758 normal othis should be in column 1
1759 call assert_equal("this should be in column 1", getline('.'))
1760 call assert_equal(4, line('.'))
1761
1762 %bwipe!
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +02001763 set ai&vim modeline&vim modelines&vim
1764endfunc
1765
1766" Test for issue #57
1767" do not move cursor on <c-o> when autoindent is set
1768func Test_ai_CTRL_O()
1769 enew!
1770 set ai
1771 let save_fo = &fo
1772 set fo+=r
1773 exe "normal o# abcdef\<Esc>2hi\<CR>\<C-O>d0\<Esc>"
1774 exe "normal o# abcdef\<Esc>2hi\<C-O>d0\<Esc>"
1775 call assert_equal(['# abc', 'def', 'def'], getline(2, 4))
1776
1777 set ai&vim
1778 let &fo = save_fo
1779 enew!
1780endfunc
1781
1782" Test for autocommand that deletes the current buffer on BufLeave event.
1783" Also test deleting the last buffer, should give a new, empty buffer.
1784func Test_BufLeave_Wipe()
1785 %bwipe!
1786 let content = ['start of test file Xxx',
1787 \ 'this is a test',
1788 \ 'end of test file Xxx']
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01001789 call writefile(content, 'Xxx1', 'D')
1790 call writefile(content, 'Xxx2', 'D')
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +02001791
1792 au BufLeave Xxx2 bwipe
1793 edit Xxx1
1794 split Xxx2
1795 " delete buffer Xxx2, we should be back to Xxx1
1796 bwipe
1797 call assert_equal('Xxx1', bufname('%'))
1798 call assert_equal(1, winnr('$'))
1799
1800 " Create an alternate buffer
1801 %write! test.out
1802 call assert_equal('test.out', bufname('#'))
1803 " delete alternate buffer
1804 bwipe test.out
1805 call assert_equal('Xxx1', bufname('%'))
1806 call assert_equal('', bufname('#'))
1807
1808 au BufLeave Xxx1 bwipe
1809 " delete current buffer, get an empty one
1810 bwipe!
1811 call assert_equal(1, line('$'))
1812 call assert_equal('', bufname('%'))
Bram Moolenaarb2c87502017-10-14 21:15:58 +02001813 let g:bufinfo = getbufinfo()
1814 call assert_equal(1, len(g:bufinfo))
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +02001815
Bram Moolenaar53f0c962017-10-22 14:23:59 +02001816 call delete('test.out')
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +02001817 %bwipe
1818 au! BufLeave
Bram Moolenaarb2c87502017-10-14 21:15:58 +02001819
1820 " check that bufinfo doesn't contain a pointer to freed memory
1821 call test_garbagecollect_now()
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +02001822endfunc
Bram Moolenaar87ffb5c2017-10-19 12:37:42 +02001823
1824func Test_QuitPre()
1825 edit Xfoo
1826 let winid = win_getid(winnr())
1827 split Xbar
1828 au! QuitPre * let g:afile = expand('<afile>')
1829 " Close the other window, <afile> should be correct.
1830 exe win_id2win(winid) . 'q'
1831 call assert_equal('Xfoo', g:afile)
LemonBoy66e13ae2022-04-21 22:52:11 +01001832
Bram Moolenaar87ffb5c2017-10-19 12:37:42 +02001833 unlet g:afile
1834 bwipe Xfoo
1835 bwipe Xbar
1836endfunc
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02001837
1838func Test_Cmdline()
Bram Moolenaar153b7042018-01-31 15:48:32 +01001839 au! CmdlineChanged : let g:text = getcmdline()
1840 let g:text = 0
1841 call feedkeys(":echom 'hello'\<CR>", 'xt')
1842 call assert_equal("echom 'hello'", g:text)
1843 au! CmdlineChanged
1844
1845 au! CmdlineChanged : let g:entered = expand('<afile>')
1846 let g:entered = 0
1847 call feedkeys(":echom 'hello'\<CR>", 'xt')
1848 call assert_equal(':', g:entered)
1849 au! CmdlineChanged
1850
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02001851 au! CmdlineEnter : let g:entered = expand('<afile>')
1852 au! CmdlineLeave : let g:left = expand('<afile>')
1853 let g:entered = 0
1854 let g:left = 0
1855 call feedkeys(":echo 'hello'\<CR>", 'xt')
1856 call assert_equal(':', g:entered)
1857 call assert_equal(':', g:left)
1858 au! CmdlineEnter
1859 au! CmdlineLeave
1860
Bram Moolenaara4baf5b2018-04-22 13:27:44 +02001861 let save_shellslash = &shellslash
1862 set noshellslash
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02001863 au! CmdlineEnter / let g:entered = expand('<afile>')
1864 au! CmdlineLeave / let g:left = expand('<afile>')
1865 let g:entered = 0
1866 let g:left = 0
Bram Moolenaar53f0c962017-10-22 14:23:59 +02001867 new
1868 call setline(1, 'hello')
1869 call feedkeys("/hello\<CR>", 'xt')
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02001870 call assert_equal('/', g:entered)
1871 call assert_equal('/', g:left)
Bram Moolenaar53f0c962017-10-22 14:23:59 +02001872 bwipe!
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02001873 au! CmdlineEnter
1874 au! CmdlineLeave
Bram Moolenaara4baf5b2018-04-22 13:27:44 +02001875 let &shellslash = save_shellslash
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02001876endfunc
Bram Moolenaar53f0c962017-10-22 14:23:59 +02001877
1878" Test for BufWritePre autocommand that deletes or unloads the buffer.
1879func Test_BufWritePre()
1880 %bwipe
1881 au BufWritePre Xxx1 bunload
1882 au BufWritePre Xxx2 bwipe
1883
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01001884 call writefile(['start of Xxx1', 'test', 'end of Xxx1'], 'Xxx1', 'D')
1885 call writefile(['start of Xxx2', 'test', 'end of Xxx2'], 'Xxx2', 'D')
Bram Moolenaar53f0c962017-10-22 14:23:59 +02001886
1887 edit Xtest
1888 e! Xxx2
1889 bdel Xtest
1890 e Xxx1
1891 " write it, will unload it and give an error msg
Bram Moolenaare2e40752020-09-04 21:18:46 +02001892 call assert_fails('w', 'E203:')
Bram Moolenaar53f0c962017-10-22 14:23:59 +02001893 call assert_equal('Xxx2', bufname('%'))
1894 edit Xtest
1895 e! Xxx2
1896 bwipe Xtest
1897 " write it, will delete the buffer and give an error msg
Bram Moolenaare2e40752020-09-04 21:18:46 +02001898 call assert_fails('w', 'E203:')
Bram Moolenaar53f0c962017-10-22 14:23:59 +02001899 call assert_equal('Xxx1', bufname('%'))
1900 au! BufWritePre
Bram Moolenaar53f0c962017-10-22 14:23:59 +02001901endfunc
1902
1903" Test for BufUnload autocommand that unloads all the other buffers
1904func Test_bufunload_all()
Bram Moolenaarf08b0eb2021-10-16 13:00:14 +01001905 let g:test_is_flaky = 1
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01001906 call writefile(['Test file Xxx1'], 'Xxx1', 'D')"
1907 call writefile(['Test file Xxx2'], 'Xxx2', 'D')"
Bram Moolenaar53f0c962017-10-22 14:23:59 +02001908
Bram Moolenaarc79745a2019-05-20 22:12:34 +02001909 let content =<< trim [CODE]
1910 func UnloadAllBufs()
1911 let i = 1
1912 while i <= bufnr('$')
1913 if i != bufnr('%') && bufloaded(i)
1914 exe i . 'bunload'
1915 endif
1916 let i += 1
1917 endwhile
1918 endfunc
1919 au BufUnload * call UnloadAllBufs()
1920 au VimLeave * call writefile(['Test Finished'], 'Xout')
1921 edit Xxx1
1922 split Xxx2
1923 q
1924 [CODE]
1925
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01001926 call writefile(content, 'Xbunloadtest', 'D')
Bram Moolenaar53f0c962017-10-22 14:23:59 +02001927
1928 call delete('Xout')
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01001929 call system(GetVimCommandClean() .. ' -N --not-a-term -S Xbunloadtest')
Bram Moolenaar53f0c962017-10-22 14:23:59 +02001930 call assert_true(filereadable('Xout'))
1931
Bram Moolenaar53f0c962017-10-22 14:23:59 +02001932 call delete('Xout')
1933endfunc
1934
1935" Some tests for buffer-local autocommands
1936func Test_buflocal_autocmd()
1937 let g:bname = ''
1938 edit xx
1939 au BufLeave <buffer> let g:bname = expand("%")
1940 " here, autocommand for xx should trigger.
1941 " but autocommand shall not apply to buffer named <buffer>.
1942 edit somefile
1943 call assert_equal('xx', g:bname)
1944 let g:bname = ''
1945 " here, autocommand shall be auto-deleted
1946 bwipe xx
1947 " autocmd should not trigger
1948 edit xx
1949 call assert_equal('', g:bname)
1950 " autocmd should not trigger
1951 edit somefile
1952 call assert_equal('', g:bname)
1953 enew
1954 unlet g:bname
1955endfunc
Bram Moolenaar430dc5d2017-11-02 21:04:47 +01001956
1957" Test for "*Cmd" autocommands
1958func Test_Cmd_Autocmds()
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01001959 call writefile(['start of Xxx', "\tabc2", 'end of Xxx'], 'Xxx', 'D')
Bram Moolenaar430dc5d2017-11-02 21:04:47 +01001960
1961 enew!
1962 au BufReadCmd XtestA 0r Xxx|$del
1963 edit XtestA " will read text of Xxd instead
1964 call assert_equal('start of Xxx', getline(1))
1965
1966 au BufWriteCmd XtestA call append(line("$"), "write")
1967 write " will append a line to the file
1968 call assert_equal('write', getline('$'))
Bram Moolenaare2e40752020-09-04 21:18:46 +02001969 call assert_fails('read XtestA', 'E484:') " should not read anything
Bram Moolenaar430dc5d2017-11-02 21:04:47 +01001970 call assert_equal('write', getline(4))
1971
1972 " now we have:
1973 " 1 start of Xxx
1974 " 2 abc2
1975 " 3 end of Xxx
1976 " 4 write
1977
1978 au FileReadCmd XtestB '[r Xxx
1979 2r XtestB " will read Xxx below line 2 instead
1980 call assert_equal('start of Xxx', getline(3))
1981
1982 " now we have:
1983 " 1 start of Xxx
1984 " 2 abc2
1985 " 3 start of Xxx
1986 " 4 abc2
1987 " 5 end of Xxx
1988 " 6 end of Xxx
1989 " 7 write
1990
1991 au FileWriteCmd XtestC '[,']copy $
1992 normal 4GA1
1993 4,5w XtestC " will copy lines 4 and 5 to the end
1994 call assert_equal("\tabc21", getline(8))
Bram Moolenaare2e40752020-09-04 21:18:46 +02001995 call assert_fails('r XtestC', 'E484:') " should not read anything
Bram Moolenaar430dc5d2017-11-02 21:04:47 +01001996 call assert_equal("end of Xxx", getline(9))
1997
1998 " now we have:
1999 " 1 start of Xxx
2000 " 2 abc2
2001 " 3 start of Xxx
2002 " 4 abc21
2003 " 5 end of Xxx
2004 " 6 end of Xxx
2005 " 7 write
2006 " 8 abc21
2007 " 9 end of Xxx
2008
2009 let g:lines = []
2010 au FileAppendCmd XtestD call extend(g:lines, getline(line("'["), line("']")))
2011 w >>XtestD " will add lines to 'lines'
2012 call assert_equal(9, len(g:lines))
Bram Moolenaare2e40752020-09-04 21:18:46 +02002013 call assert_fails('$r XtestD', 'E484:') " should not read anything
Bram Moolenaar430dc5d2017-11-02 21:04:47 +01002014 call assert_equal(9, line('$'))
2015 call assert_equal('end of Xxx', getline('$'))
2016
2017 au BufReadCmd XtestE 0r Xxx|$del
2018 sp XtestE " split window with test.out
2019 call assert_equal('end of Xxx', getline(3))
2020
2021 let g:lines = []
2022 exe "normal 2Goasdf\<Esc>\<C-W>\<C-W>"
2023 au BufWriteCmd XtestE call extend(g:lines, getline(0, '$'))
2024 wall " will write other window to 'lines'
2025 call assert_equal(4, len(g:lines), g:lines)
2026 call assert_equal('asdf', g:lines[2])
2027
2028 au! BufReadCmd
2029 au! BufWriteCmd
2030 au! FileReadCmd
2031 au! FileWriteCmd
2032 au! FileAppendCmd
2033 %bwipe!
Bram Moolenaar430dc5d2017-11-02 21:04:47 +01002034 enew!
2035endfunc
Bram Moolenaaraace2152017-11-05 16:23:10 +01002036
Bram Moolenaar0fff4412020-03-29 16:06:29 +02002037func s:ReadFile()
2038 setl noswapfile nomodified
2039 let filename = resolve(expand("<afile>:p"))
2040 execute 'read' fnameescape(filename)
2041 1d_
2042 exe 'file' fnameescape(filename)
2043 setl buftype=acwrite
2044endfunc
2045
2046func s:WriteFile()
2047 let filename = resolve(expand("<afile>:p"))
2048 setl buftype=
2049 noautocmd execute 'write' fnameescape(filename)
2050 setl buftype=acwrite
2051 setl nomodified
2052endfunc
2053
2054func Test_BufReadCmd()
2055 autocmd BufReadCmd *.test call s:ReadFile()
2056 autocmd BufWriteCmd *.test call s:WriteFile()
2057
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01002058 call writefile(['one', 'two', 'three'], 'Xcmd.test', 'D')
Bram Moolenaar0fff4412020-03-29 16:06:29 +02002059 edit Xcmd.test
2060 call assert_match('Xcmd.test" line 1 of 3', execute('file'))
2061 normal! Gofour
2062 write
2063 call assert_equal(['one', 'two', 'three', 'four'], readfile('Xcmd.test'))
2064
2065 bwipe!
Bram Moolenaar0fff4412020-03-29 16:06:29 +02002066 au! BufReadCmd
2067 au! BufWriteCmd
2068endfunc
2069
zeertzjq9c8f9462022-08-30 18:17:15 +01002070func Test_BufWriteCmd()
2071 autocmd BufWriteCmd Xbufwritecmd let g:written = 1
2072 new
2073 file Xbufwritecmd
2074 set buftype=acwrite
Bram Moolenaar6f14da12022-09-07 21:30:44 +01002075 call mkdir('Xbufwritecmd', 'D')
zeertzjq9c8f9462022-08-30 18:17:15 +01002076 write
2077 " BufWriteCmd should be triggered even if a directory has the same name
2078 call assert_equal(1, g:written)
zeertzjq9c8f9462022-08-30 18:17:15 +01002079 unlet g:written
2080 au! BufWriteCmd
2081 bwipe!
2082endfunc
2083
Bram Moolenaaraace2152017-11-05 16:23:10 +01002084func SetChangeMarks(start, end)
Bram Moolenaar97c69432021-01-15 16:45:21 +01002085 exe a:start .. 'mark ['
2086 exe a:end .. 'mark ]'
Bram Moolenaaraace2152017-11-05 16:23:10 +01002087endfunc
2088
2089" Verify the effects of autocmds on '[ and ']
2090func Test_change_mark_in_autocmds()
2091 edit! Xtest
Bram Moolenaar97c69432021-01-15 16:45:21 +01002092 call feedkeys("ia\<CR>b\<CR>c\<CR>d\<C-g>u\<Esc>", 'xtn')
Bram Moolenaaraace2152017-11-05 16:23:10 +01002093
2094 call SetChangeMarks(2, 3)
2095 write
2096 call assert_equal([1, 4], [line("'["), line("']")])
2097
2098 call SetChangeMarks(2, 3)
2099 au BufWritePre * call assert_equal([1, 4], [line("'["), line("']")])
2100 write
2101 au! BufWritePre
2102
Bram Moolenaar14ddd222020-08-05 12:02:40 +02002103 if has('unix')
Bram Moolenaaraace2152017-11-05 16:23:10 +01002104 write XtestFilter
2105 write >> XtestFilter
2106
2107 call SetChangeMarks(2, 3)
2108 " Marks are set to the entire range of the write
2109 au FilterWritePre * call assert_equal([1, 4], [line("'["), line("']")])
2110 " '[ is adjusted to just before the line that will receive the filtered
2111 " data
2112 au FilterReadPre * call assert_equal([4, 4], [line("'["), line("']")])
2113 " The filtered data is read into the buffer, and the source lines are
2114 " still present, so the range is after the source lines
2115 au FilterReadPost * call assert_equal([5, 12], [line("'["), line("']")])
2116 %!cat XtestFilter
2117 " After the filtered data is read, the original lines are deleted
2118 call assert_equal([1, 8], [line("'["), line("']")])
2119 au! FilterWritePre,FilterReadPre,FilterReadPost
2120 undo
2121
2122 call SetChangeMarks(1, 4)
2123 au FilterWritePre * call assert_equal([2, 3], [line("'["), line("']")])
2124 au FilterReadPre * call assert_equal([3, 3], [line("'["), line("']")])
2125 au FilterReadPost * call assert_equal([4, 11], [line("'["), line("']")])
2126 2,3!cat XtestFilter
2127 call assert_equal([2, 9], [line("'["), line("']")])
2128 au! FilterWritePre,FilterReadPre,FilterReadPost
2129 undo
2130
2131 call delete('XtestFilter')
2132 endif
2133
2134 call SetChangeMarks(1, 4)
2135 au FileWritePre * call assert_equal([2, 3], [line("'["), line("']")])
2136 2,3write Xtest2
2137 au! FileWritePre
2138
2139 call SetChangeMarks(2, 3)
2140 au FileAppendPre * call assert_equal([1, 4], [line("'["), line("']")])
2141 write >> Xtest2
2142 au! FileAppendPre
2143
2144 call SetChangeMarks(1, 4)
2145 au FileAppendPre * call assert_equal([2, 3], [line("'["), line("']")])
2146 2,3write >> Xtest2
2147 au! FileAppendPre
2148
2149 call SetChangeMarks(1, 1)
2150 au FileReadPre * call assert_equal([3, 1], [line("'["), line("']")])
2151 au FileReadPost * call assert_equal([4, 11], [line("'["), line("']")])
2152 3read Xtest2
2153 au! FileReadPre,FileReadPost
2154 undo
2155
2156 call SetChangeMarks(4, 4)
2157 " When the line is 0, it's adjusted to 1
2158 au FileReadPre * call assert_equal([1, 4], [line("'["), line("']")])
2159 au FileReadPost * call assert_equal([1, 8], [line("'["), line("']")])
2160 0read Xtest2
2161 au! FileReadPre,FileReadPost
2162 undo
2163
2164 call SetChangeMarks(4, 4)
2165 " When the line is 0, it's adjusted to 1
2166 au FileReadPre * call assert_equal([1, 4], [line("'["), line("']")])
2167 au FileReadPost * call assert_equal([2, 9], [line("'["), line("']")])
2168 1read Xtest2
2169 au! FileReadPre,FileReadPost
2170 undo
2171
2172 bwipe!
2173 call delete('Xtest')
2174 call delete('Xtest2')
2175endfunc
2176
2177func Test_Filter_noshelltemp()
Bram Moolenaaraeb313f2020-11-27 19:13:28 +01002178 CheckExecutable cat
Bram Moolenaaraace2152017-11-05 16:23:10 +01002179
2180 enew!
2181 call setline(1, ['a', 'b', 'c', 'd'])
2182
2183 let shelltemp = &shelltemp
2184 set shelltemp
2185
2186 let g:filter_au = 0
2187 au FilterWritePre * let g:filter_au += 1
2188 au FilterReadPre * let g:filter_au += 1
2189 au FilterReadPost * let g:filter_au += 1
2190 %!cat
2191 call assert_equal(3, g:filter_au)
2192
2193 if has('filterpipe')
2194 set noshelltemp
2195
2196 let g:filter_au = 0
2197 au FilterWritePre * let g:filter_au += 1
2198 au FilterReadPre * let g:filter_au += 1
2199 au FilterReadPost * let g:filter_au += 1
2200 %!cat
2201 call assert_equal(0, g:filter_au)
2202 endif
2203
2204 au! FilterWritePre,FilterReadPre,FilterReadPost
2205 let &shelltemp = shelltemp
2206 bwipe!
2207endfunc
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01002208
2209func Test_TextYankPost()
2210 enew!
2211 call setline(1, ['foo'])
2212
2213 let g:event = []
2214 au TextYankPost * let g:event = copy(v:event)
2215
2216 call assert_equal({}, v:event)
2217 call assert_fails('let v:event = {}', 'E46:')
2218 call assert_fails('let v:event.mykey = 0', 'E742:')
2219
2220 norm "ayiw
2221 call assert_equal(
Bram Moolenaara016eeb2022-04-09 11:37:38 +01002222 \ #{regcontents: ['foo'], regname: 'a', operator: 'y',
2223 \ regtype: 'v', visual: v:false, inclusive: v:true},
2224 \ g:event)
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01002225 norm y_
2226 call assert_equal(
Bram Moolenaara016eeb2022-04-09 11:37:38 +01002227 \ #{regcontents: ['foo'], regname: '', operator: 'y', regtype: 'V',
2228 \ visual: v:false, inclusive: v:false},
2229 \ g:event)
Bram Moolenaar37d16732020-06-12 22:09:01 +02002230 norm Vy
2231 call assert_equal(
Bram Moolenaara016eeb2022-04-09 11:37:38 +01002232 \ #{regcontents: ['foo'], regname: '', operator: 'y', regtype: 'V',
2233 \ visual: v:true, inclusive: v:true},
2234 \ g:event)
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01002235 call feedkeys("\<C-V>y", 'x')
2236 call assert_equal(
Bram Moolenaara016eeb2022-04-09 11:37:38 +01002237 \ #{regcontents: ['f'], regname: '', operator: 'y', regtype: "\x161",
2238 \ visual: v:true, inclusive: v:true},
2239 \ g:event)
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01002240 norm "xciwbar
2241 call assert_equal(
Bram Moolenaara016eeb2022-04-09 11:37:38 +01002242 \ #{regcontents: ['foo'], regname: 'x', operator: 'c', regtype: 'v',
2243 \ visual: v:false, inclusive: v:true},
2244 \ g:event)
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01002245 norm "bdiw
2246 call assert_equal(
Bram Moolenaara016eeb2022-04-09 11:37:38 +01002247 \ #{regcontents: ['bar'], regname: 'b', operator: 'd', regtype: 'v',
2248 \ visual: v:false, inclusive: v:true},
2249 \ g:event)
2250
2251 call setline(1, 'foobar')
2252 " exclusive motion
2253 norm $"ay0
2254 call assert_equal(
2255 \ #{regcontents: ['fooba'], regname: 'a', operator: 'y', regtype: 'v',
2256 \ visual: v:false, inclusive: v:false},
2257 \ g:event)
2258 " inclusive motion
2259 norm 0"ay$
2260 call assert_equal(
2261 \ #{regcontents: ['foobar'], regname: 'a', operator: 'y', regtype: 'v',
2262 \ visual: v:false, inclusive: v:true},
2263 \ g:event)
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01002264
2265 call assert_equal({}, v:event)
2266
Bram Moolenaarfccbf062020-11-26 20:34:00 +01002267 if has('clipboard_working') && !has('gui_running')
2268 " Test that when the visual selection is automatically copied to clipboard
2269 " register a TextYankPost is emitted
2270 call setline(1, ['foobar'])
2271
2272 let @* = ''
2273 set clipboard=autoselect
2274 exe "norm! ggviw\<Esc>"
2275 call assert_equal(
Bram Moolenaara016eeb2022-04-09 11:37:38 +01002276 \ #{regcontents: ['foobar'], regname: '*', operator: 'y',
2277 \ regtype: 'v', visual: v:true, inclusive: v:false},
2278 \ g:event)
Bram Moolenaarfccbf062020-11-26 20:34:00 +01002279
2280 let @+ = ''
2281 set clipboard=autoselectplus
2282 exe "norm! ggviw\<Esc>"
2283 call assert_equal(
Bram Moolenaara016eeb2022-04-09 11:37:38 +01002284 \ #{regcontents: ['foobar'], regname: '+', operator: 'y',
2285 \ regtype: 'v', visual: v:true, inclusive: v:false},
2286 \ g:event)
Bram Moolenaarfccbf062020-11-26 20:34:00 +01002287
2288 set clipboard&vim
2289 endif
2290
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01002291 au! TextYankPost
2292 unlet g:event
2293 bwipe!
2294endfunc
Bram Moolenaar9bca8052017-12-18 12:37:55 +01002295
Bram Moolenaar9a046fd2021-01-28 13:47:59 +01002296func Test_autocommand_all_events()
2297 call assert_fails('au * * bwipe', 'E1155:')
2298 call assert_fails('au * x bwipe', 'E1155:')
Bram Moolenaarb6db1462021-12-24 19:24:47 +00002299 call assert_fails('au! * x bwipe', 'E1155:')
Bram Moolenaar4fb921e2017-12-18 15:33:00 +01002300endfunc
Bram Moolenaarb7407d32018-02-03 17:36:27 +01002301
Bram Moolenaarf6246f52022-02-11 16:30:12 +00002302func Test_autocmd_user()
2303 au User MyEvent let s:res = [expand("<afile>"), expand("<amatch>")]
2304 doautocmd User MyEvent
2305 call assert_equal(['MyEvent', 'MyEvent'], s:res)
2306 au! User
2307 unlet s:res
2308endfunc
2309
Bram Moolenaar3b014be2022-11-13 17:53:46 +00002310func Test_autocmd_user_clear_group()
2311 CheckRunVimInTerminal
2312
2313 let lines =<< trim END
2314 autocmd! User
2315 for i in range(1, 999)
2316 exe 'autocmd User ' .. 'Foo' .. i .. ' bar'
2317 endfor
2318 au CmdlineLeave : call timer_start(0, {-> execute('autocmd! User')})
2319 END
2320 call writefile(lines, 'XautoUser', 'D')
2321 let buf = RunVimInTerminal('-S XautoUser', {'rows': 10})
2322
2323 " this was using freed memory
2324 call term_sendkeys(buf, ":autocmd User\<CR>")
2325 call TermWait(buf, 50)
2326 call term_sendkeys(buf, "G")
2327
2328 call StopVimInTerminal(buf)
2329endfunc
2330
Bram Moolenaaref2c3252022-11-25 16:31:51 +00002331func Test_autocmd_CmdlineLeave_unlet()
2332 CheckRunVimInTerminal
2333
2334 let lines =<< trim END
2335 for i in range(1, 999)
2336 exe 'let g:var' .. i '=' i
2337 endfor
2338 au CmdlineLeave : call timer_start(0, {-> execute('unlet g:var990')})
2339 END
2340 call writefile(lines, 'XleaveUnlet', 'D')
2341 let buf = RunVimInTerminal('-S XleaveUnlet', {'rows': 10})
2342
2343 " this was using freed memory
2344 call term_sendkeys(buf, ":let g:\<CR>")
2345 call TermWait(buf, 50)
2346 call term_sendkeys(buf, "G")
2347 call TermWait(buf, 50)
2348 call term_sendkeys(buf, "\<CR>") " for the hit-enter prompt
2349
2350 call StopVimInTerminal(buf)
2351endfunc
2352
Bram Moolenaarb7407d32018-02-03 17:36:27 +01002353function s:Before_test_dirchanged()
2354 augroup test_dirchanged
2355 autocmd!
2356 augroup END
2357 let s:li = []
2358 let s:dir_this = getcwd()
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02002359 let s:dir_foo = s:dir_this . '/Xfoo'
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01002360 call mkdir(s:dir_foo)
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02002361 let s:dir_bar = s:dir_this . '/Xbar'
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01002362 call mkdir(s:dir_bar)
Bram Moolenaarb7407d32018-02-03 17:36:27 +01002363endfunc
2364
2365function s:After_test_dirchanged()
Bram Moolenaar3503d7c2019-11-09 20:10:17 +01002366 call chdir(s:dir_this)
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01002367 call delete(s:dir_foo, 'd')
2368 call delete(s:dir_bar, 'd')
Bram Moolenaarb7407d32018-02-03 17:36:27 +01002369 augroup test_dirchanged
2370 autocmd!
2371 augroup END
2372endfunc
2373
2374function Test_dirchanged_global()
2375 call s:Before_test_dirchanged()
Bram Moolenaarf6246f52022-02-11 16:30:12 +00002376 autocmd test_dirchanged DirChangedPre global call add(s:li, expand("<amatch>") .. " pre cd " .. v:event.directory)
Bram Moolenaarb7407d32018-02-03 17:36:27 +01002377 autocmd test_dirchanged DirChanged global call add(s:li, "cd:")
2378 autocmd test_dirchanged DirChanged global call add(s:li, expand("<afile>"))
Bram Moolenaar3503d7c2019-11-09 20:10:17 +01002379 call chdir(s:dir_foo)
Bram Moolenaarf6246f52022-02-11 16:30:12 +00002380 let expected = ["global pre cd " .. s:dir_foo, "cd:", 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 call chdir(s:dir_foo)
Bram Moolenaar28e8f732022-02-09 12:58:20 +00002383 call assert_equal(expected, s:li)
Bram Moolenaar3503d7c2019-11-09 20:10:17 +01002384 exe 'lcd ' .. fnameescape(s:dir_bar)
Bram Moolenaar28e8f732022-02-09 12:58:20 +00002385 call assert_equal(expected, s:li)
Bram Moolenaard8c9d322022-06-12 11:49:16 +01002386
2387 exe 'cd ' .. s:dir_foo
2388 exe 'cd ' .. s:dir_bar
2389 autocmd! test_dirchanged DirChanged global let g:result = expand("<afile>")
2390 cd -
Bram Moolenaardb77c492022-06-12 23:26:50 +01002391 call assert_equal(s:dir_foo, substitute(g:result, '\\', '/', 'g'))
Bram Moolenaard8c9d322022-06-12 11:49:16 +01002392
Bram Moolenaarb7407d32018-02-03 17:36:27 +01002393 call s:After_test_dirchanged()
2394endfunc
2395
2396function Test_dirchanged_local()
2397 call s:Before_test_dirchanged()
2398 autocmd test_dirchanged DirChanged window call add(s:li, "lcd:")
2399 autocmd test_dirchanged DirChanged window call add(s:li, expand("<afile>"))
Bram Moolenaar3503d7c2019-11-09 20:10:17 +01002400 call chdir(s:dir_foo)
Bram Moolenaarb7407d32018-02-03 17:36:27 +01002401 call assert_equal([], 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 Moolenaar3503d7c2019-11-09 20:10:17 +01002404 exe 'lcd ' .. fnameescape(s:dir_bar)
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01002405 call assert_equal(["lcd:", s:dir_bar], s:li)
Bram Moolenaarb7407d32018-02-03 17:36:27 +01002406 call s:After_test_dirchanged()
2407endfunc
2408
2409function Test_dirchanged_auto()
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +02002410 CheckOption autochdir
Bram Moolenaarb7407d32018-02-03 17:36:27 +01002411 call s:Before_test_dirchanged()
2412 call test_autochdir()
Bram Moolenaar28e8f732022-02-09 12:58:20 +00002413 autocmd test_dirchanged DirChangedPre auto call add(s:li, "pre cd " .. v:event.directory)
Bram Moolenaarb7407d32018-02-03 17:36:27 +01002414 autocmd test_dirchanged DirChanged auto call add(s:li, "auto:")
2415 autocmd test_dirchanged DirChanged auto call add(s:li, expand("<afile>"))
2416 set acd
Bram Moolenaar3503d7c2019-11-09 20:10:17 +01002417 cd ..
Bram Moolenaarb7407d32018-02-03 17:36:27 +01002418 call assert_equal([], s:li)
Bram Moolenaar61abe7d2022-08-30 21:46:08 +01002419 exe 'edit ' . s:dir_foo . '/Xautofile'
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01002420 call assert_equal(s:dir_foo, getcwd())
Bram Moolenaar28e8f732022-02-09 12:58:20 +00002421 let expected = ["pre cd " .. s:dir_foo, "auto:", s:dir_foo]
2422 call assert_equal(expected, s:li)
Bram Moolenaarb7407d32018-02-03 17:36:27 +01002423 set noacd
2424 bwipe!
2425 call s:After_test_dirchanged()
2426endfunc
Bram Moolenaar5a093432018-02-10 18:15:19 +01002427
2428" Test TextChangedI and TextChangedP
2429func Test_ChangedP()
2430 new
2431 call setline(1, ['foo', 'bar', 'foobar'])
2432 call test_override("char_avail", 1)
2433 set complete=. completeopt=menuone
2434
2435 func! TextChangedAutocmd(char)
2436 let g:autocmd .= a:char
2437 endfunc
2438
Christian Brabandtdb3b4462021-10-16 11:58:55 +01002439 " TextChanged will not be triggered, only check that it isn't.
Bram Moolenaar5a093432018-02-10 18:15:19 +01002440 au! TextChanged <buffer> :call TextChangedAutocmd('N')
2441 au! TextChangedI <buffer> :call TextChangedAutocmd('I')
2442 au! TextChangedP <buffer> :call TextChangedAutocmd('P')
2443
2444 call cursor(3, 1)
2445 let g:autocmd = ''
2446 call feedkeys("o\<esc>", 'tnix')
2447 call assert_equal('I', g:autocmd)
2448
2449 let g:autocmd = ''
2450 call feedkeys("Sf", 'tnix')
2451 call assert_equal('II', g:autocmd)
2452
2453 let g:autocmd = ''
2454 call feedkeys("Sf\<C-N>", 'tnix')
2455 call assert_equal('IIP', g:autocmd)
2456
2457 let g:autocmd = ''
2458 call feedkeys("Sf\<C-N>\<C-N>", 'tnix')
2459 call assert_equal('IIPP', g:autocmd)
2460
2461 let g:autocmd = ''
2462 call feedkeys("Sf\<C-N>\<C-N>\<C-N>", 'tnix')
2463 call assert_equal('IIPPP', g:autocmd)
2464
2465 let g:autocmd = ''
2466 call feedkeys("Sf\<C-N>\<C-N>\<C-N>\<C-N>", 'tnix')
2467 call assert_equal('IIPPPP', g:autocmd)
2468
2469 call assert_equal(['foo', 'bar', 'foobar', 'foo'], getline(1, '$'))
2470 " TODO: how should it handle completeopt=noinsert,noselect?
2471
2472 " CleanUp
2473 call test_override("char_avail", 0)
2474 au! TextChanged
2475 au! TextChangedI
2476 au! TextChangedP
2477 delfu TextChangedAutocmd
2478 unlet! g:autocmd
2479 set complete&vim completeopt&vim
2480
2481 bw!
2482endfunc
Bram Moolenaar8c64a362018-03-23 22:39:31 +01002483
Bram Moolenaar91d2e782018-08-07 19:05:01 +02002484let g:setline_handled = v:false
Bram Moolenaar1e115362019-01-09 23:01:02 +01002485func SetLineOne()
Bram Moolenaar91d2e782018-08-07 19:05:01 +02002486 if !g:setline_handled
2487 call setline(1, "(x)")
2488 let g:setline_handled = v:true
2489 endif
2490endfunc
2491
2492func Test_TextChangedI_with_setline()
2493 new
2494 call test_override('char_avail', 1)
2495 autocmd TextChangedI <buffer> call SetLineOne()
2496 call feedkeys("i(\<CR>\<Esc>", 'tx')
2497 call assert_equal('(', getline(1))
2498 call assert_equal('x)', getline(2))
2499 undo
Bram Moolenaar91d2e782018-08-07 19:05:01 +02002500 call assert_equal('', getline(1))
Bram Moolenaar9fa95062018-08-08 22:08:32 +02002501 call assert_equal('', getline(2))
Bram Moolenaar91d2e782018-08-07 19:05:01 +02002502
Bram Moolenaarca34db32022-01-20 11:17:18 +00002503 call test_override('char_avail', 0)
Bram Moolenaar91d2e782018-08-07 19:05:01 +02002504 bwipe!
2505endfunc
2506
Bram Moolenaar8c64a362018-03-23 22:39:31 +01002507func Test_Changed_FirstTime()
Bram Moolenaar8c5a2782019-08-07 23:07:07 +02002508 CheckFeature terminal
2509 CheckNotGui
Bram Moolenaar3cdcb092020-03-18 19:18:10 +01002510 " Starting a terminal to run Vim is always considered flaky.
Bram Moolenaar30d53e22020-03-18 21:10:44 +01002511 let g:test_is_flaky = 1
Bram Moolenaar8c5a2782019-08-07 23:07:07 +02002512
Bram Moolenaar8c64a362018-03-23 22:39:31 +01002513 " Prepare file for TextChanged event.
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01002514 call writefile([''], 'Xchanged.txt', 'D')
Bram Moolenaar8c64a362018-03-23 22:39:31 +01002515 let buf = term_start([GetVimProg(), '--clean', '-c', 'set noswapfile'], {'term_rows': 3})
2516 call assert_equal('running', term_getstatus(buf))
Bram Moolenaar1834d372018-03-29 17:40:46 +02002517 " Wait for the ruler (in the status line) to be shown.
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01002518 " In ConPTY, there is additional character which is drawn up to the width of
2519 " the screen.
2520 if has('conpty')
2521 call WaitForAssert({-> assert_match('\<All.*$', term_getline(buf, 3))})
2522 else
2523 call WaitForAssert({-> assert_match('\<All$', term_getline(buf, 3))})
2524 endif
Bram Moolenaar8c64a362018-03-23 22:39:31 +01002525 " It's only adding autocmd, so that no event occurs.
2526 call term_sendkeys(buf, ":au! TextChanged <buffer> call writefile(['No'], 'Xchanged.txt')\<cr>")
2527 call term_sendkeys(buf, "\<C-\\>\<C-N>:qa!\<cr>")
Bram Moolenaar50182fa2018-04-28 21:34:40 +02002528 call WaitForAssert({-> assert_equal('finished', term_getstatus(buf))})
Bram Moolenaar8c64a362018-03-23 22:39:31 +01002529 call assert_equal([''], readfile('Xchanged.txt'))
2530
2531 " clean up
Bram Moolenaar8c64a362018-03-23 22:39:31 +01002532 bwipe!
2533endfunc
Bram Moolenaar0566e892019-01-24 19:37:40 +01002534
Bram Moolenaareb93f3f2019-04-04 15:04:56 +02002535func Test_autocmd_nested()
2536 let g:did_nested = 0
2537 augroup Testing
2538 au WinNew * edit somefile
2539 au BufNew * let g:did_nested = 1
2540 augroup END
2541 split
2542 call assert_equal(0, g:did_nested)
2543 close
2544 bwipe! somefile
2545
2546 " old nested argument still works
2547 augroup Testing
2548 au!
2549 au WinNew * nested edit somefile
2550 au BufNew * let g:did_nested = 1
2551 augroup END
2552 split
2553 call assert_equal(1, g:did_nested)
2554 close
2555 bwipe! somefile
2556
2557 " New ++nested argument works
2558 augroup Testing
2559 au!
2560 au WinNew * ++nested edit somefile
2561 au BufNew * let g:did_nested = 1
2562 augroup END
2563 split
2564 call assert_equal(1, g:did_nested)
2565 close
2566 bwipe! somefile
2567
Bram Moolenaarf0775142022-03-04 20:10:38 +00002568 " nested without ++ does not work in Vim9 script
2569 call assert_fails('vim9cmd au WinNew * nested echo fails', 'E1078:')
2570
Bram Moolenaareb93f3f2019-04-04 15:04:56 +02002571 augroup Testing
2572 au!
2573 augroup END
2574
2575 call assert_fails('au WinNew * ++nested ++nested echo bad', 'E983:')
2576 call assert_fails('au WinNew * nested nested echo bad', 'E983:')
2577endfunc
2578
Bram Moolenaar5fa9f232022-07-23 09:06:48 +01002579func Test_autocmd_nested_cursor_invalid()
2580 set laststatus=0
2581 copen
2582 cclose
2583 call setline(1, ['foo', 'bar', 'baz'])
2584 3
2585 augroup nested_inv
2586 autocmd User foo ++nested copen
2587 autocmd BufAdd * let &laststatus = 2 - &laststatus
2588 augroup END
2589 doautocmd User foo
2590
2591 augroup nested_inv
2592 au!
2593 augroup END
2594 set laststatus&
Bram Moolenaarb03950f2022-07-26 13:47:13 +01002595 cclose
Bram Moolenaar5fa9f232022-07-23 09:06:48 +01002596 bwipe!
2597endfunc
2598
Bram Moolenaar3d6ee8b2022-07-27 15:23:35 +01002599func Test_autocmd_nested_keeps_cursor_pos()
2600 enew
2601 call setline(1, 'foo')
2602 autocmd User foo ++nested normal! $a
2603 autocmd InsertLeave * :
2604 doautocmd User foo
2605 call assert_equal([0, 1, 3, 0], getpos('.'))
2606
2607 bwipe!
2608endfunc
2609
Bram Moolenaarb03950f2022-07-26 13:47:13 +01002610func Test_autocmd_nested_switch_window()
2611 " run this in a separate Vim so that SafeState works
2612 CheckRunVimInTerminal
2613
2614 let lines =<< trim END
2615 vim9script
2616 ['()']->writefile('Xautofile')
2617 autocmd VimEnter * ++nested edit Xautofile | split
2618 autocmd BufReadPost * autocmd SafeState * ++once foldclosed('.')
2619 autocmd WinEnter * matchadd('ErrorMsg', 'pat')
2620 END
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01002621 call writefile(lines, 'Xautoscript', 'D')
Bram Moolenaarb03950f2022-07-26 13:47:13 +01002622 let buf = RunVimInTerminal('-S Xautoscript', {'rows': 10})
2623 call VerifyScreenDump(buf, 'Test_autocmd_nested_switch', {})
2624
2625 call StopVimInTerminal(buf)
2626 call delete('Xautofile')
Bram Moolenaarb03950f2022-07-26 13:47:13 +01002627endfunc
2628
Bram Moolenaareb93f3f2019-04-04 15:04:56 +02002629func Test_autocmd_once()
2630 " Without ++once WinNew triggers twice
2631 let g:did_split = 0
2632 augroup Testing
2633 au WinNew * let g:did_split += 1
2634 augroup END
2635 split
2636 split
2637 call assert_equal(2, g:did_split)
2638 call assert_true(exists('#WinNew'))
2639 close
2640 close
2641
2642 " With ++once WinNew triggers once
2643 let g:did_split = 0
2644 augroup Testing
2645 au!
2646 au WinNew * ++once let g:did_split += 1
2647 augroup END
2648 split
2649 split
2650 call assert_equal(1, g:did_split)
2651 call assert_false(exists('#WinNew'))
2652 close
2653 close
2654
2655 call assert_fails('au WinNew * ++once ++once echo bad', 'E983:')
2656endfunc
2657
Bram Moolenaara68e5952019-04-25 22:22:01 +02002658func Test_autocmd_bufreadpre()
2659 new
2660 let b:bufreadpre = 1
Bram Moolenaarab505b12020-03-23 19:28:44 +01002661 call append(0, range(1000))
Bram Moolenaara68e5952019-04-25 22:22:01 +02002662 w! XAutocmdBufReadPre.txt
2663 autocmd BufReadPre <buffer> :let b:bufreadpre += 1
Bram Moolenaarab505b12020-03-23 19:28:44 +01002664 norm! 500gg
Bram Moolenaara68e5952019-04-25 22:22:01 +02002665 sp
Bram Moolenaarab505b12020-03-23 19:28:44 +01002666 norm! 1000gg
Bram Moolenaara68e5952019-04-25 22:22:01 +02002667 wincmd p
2668 let g:wsv1 = winsaveview()
2669 wincmd p
2670 let g:wsv2 = winsaveview()
2671 " triggers BufReadPre, should not move the cursor in either window
2672 " The topline may change one line in a large window.
2673 edit
2674 call assert_inrange(g:wsv2.topline - 1, g:wsv2.topline + 1, winsaveview().topline)
2675 call assert_equal(g:wsv2.lnum, winsaveview().lnum)
2676 call assert_equal(2, b:bufreadpre)
2677 wincmd p
2678 call assert_equal(g:wsv1.topline, winsaveview().topline)
2679 call assert_equal(g:wsv1.lnum, winsaveview().lnum)
2680 call assert_equal(2, b:bufreadpre)
2681 " Now set the cursor position in an BufReadPre autocommand
2682 " (even though the position will be invalid, this should make Vim reset the
2683 " cursor position in the other window.
2684 wincmd p
2685 set cpo+=g
2686 " won't do anything, but try to set the cursor on an invalid lnum
2687 autocmd BufReadPre <buffer> :norm! 70gg
2688 " triggers BufReadPre, should not move the cursor in either window
2689 e
2690 call assert_equal(1, winsaveview().topline)
2691 call assert_equal(1, winsaveview().lnum)
2692 call assert_equal(3, b:bufreadpre)
2693 wincmd p
2694 call assert_equal(g:wsv1.topline, winsaveview().topline)
2695 call assert_equal(g:wsv1.lnum, winsaveview().lnum)
2696 call assert_equal(3, b:bufreadpre)
2697 close
2698 close
2699 call delete('XAutocmdBufReadPre.txt')
2700 set cpo-=g
2701endfunc
2702
Bram Moolenaar5e66b422019-01-24 21:58:10 +01002703" FileChangedShell tested in test_filechanged.vim
Bram Moolenaar69ea5872019-04-25 20:29:00 +02002704
2705" Tests for the following autocommands:
2706" - FileWritePre writing a compressed file
2707" - FileReadPost reading a compressed file
2708" - BufNewFile reading a file template
2709" - BufReadPre decompressing the file to be read
2710" - FilterReadPre substituting characters in the temp file
2711" - FilterReadPost substituting characters after filtering
2712" - FileReadPre set options for decompression
2713" - FileReadPost decompress the file
2714func Test_ReadWrite_Autocmds()
2715 " Run this test only on Unix-like systems and if gzip is available
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +02002716 CheckUnix
2717 CheckExecutable gzip
Bram Moolenaar69ea5872019-04-25 20:29:00 +02002718
2719 " Make $GZIP empty, "-v" would cause trouble.
2720 let $GZIP = ""
2721
2722 " Use a FileChangedShell autocommand to avoid a prompt for 'Xtestfile.gz'
2723 " being modified outside of Vim (noticed on Solaris).
2724 au FileChangedShell * echo 'caught FileChangedShell'
2725
2726 " Test for the FileReadPost, FileWritePre and FileWritePost autocmds
2727 augroup Test1
2728 au!
2729 au FileWritePre *.gz '[,']!gzip
2730 au FileWritePost *.gz undo
2731 au FileReadPost *.gz '[,']!gzip -d
2732 augroup END
2733
2734 new
2735 set bin
2736 call append(0, [
2737 \ 'line 2 Abcdefghijklmnopqrstuvwxyz',
2738 \ 'line 3 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2739 \ 'line 4 Abcdefghijklmnopqrstuvwxyz',
2740 \ 'line 5 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2741 \ 'line 6 Abcdefghijklmnopqrstuvwxyz',
2742 \ 'line 7 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2743 \ 'line 8 Abcdefghijklmnopqrstuvwxyz',
2744 \ 'line 9 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2745 \ 'line 10 Abcdefghijklmnopqrstuvwxyz'
2746 \ ])
2747 1,9write! Xtestfile.gz
2748 enew! | close
2749
2750 new
2751 " Read and decompress the testfile
2752 0read Xtestfile.gz
2753 call assert_equal([
2754 \ 'line 2 Abcdefghijklmnopqrstuvwxyz',
2755 \ 'line 3 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2756 \ 'line 4 Abcdefghijklmnopqrstuvwxyz',
2757 \ 'line 5 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2758 \ 'line 6 Abcdefghijklmnopqrstuvwxyz',
2759 \ 'line 7 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2760 \ 'line 8 Abcdefghijklmnopqrstuvwxyz',
2761 \ 'line 9 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2762 \ 'line 10 Abcdefghijklmnopqrstuvwxyz'
2763 \ ], getline(1, 9))
2764 enew! | close
2765
2766 augroup Test1
2767 au!
2768 augroup END
2769
2770 " Test for the FileAppendPre and FileAppendPost autocmds
2771 augroup Test2
2772 au!
2773 au BufNewFile *.c read Xtest.c
2774 au FileAppendPre *.out '[,']s/new/NEW/
2775 au FileAppendPost *.out !cat Xtest.c >> test.out
2776 augroup END
2777
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01002778 call writefile(['/*', ' * Here is a new .c file', ' */'], 'Xtest.c', 'D')
Bram Moolenaar69ea5872019-04-25 20:29:00 +02002779 new foo.c " should load Xtest.c
2780 call assert_equal(['/*', ' * Here is a new .c file', ' */'], getline(2, 4))
2781 w! >> test.out " append it to the output file
2782
2783 let contents = readfile('test.out')
2784 call assert_equal(' * Here is a NEW .c file', contents[2])
2785 call assert_equal(' * Here is a new .c file', contents[5])
2786
2787 call delete('test.out')
2788 enew! | close
2789 augroup Test2
2790 au!
2791 augroup END
2792
2793 " Test for the BufReadPre and BufReadPost autocmds
2794 augroup Test3
2795 au!
2796 " setup autocommands to decompress before reading and re-compress
2797 " afterwards
2798 au BufReadPre *.gz exe '!gzip -d ' . shellescape(expand("<afile>"))
2799 au BufReadPre *.gz call rename(expand("<afile>:r"), expand("<afile>"))
2800 au BufReadPost *.gz call rename(expand("<afile>"), expand("<afile>:r"))
2801 au BufReadPost *.gz exe '!gzip ' . shellescape(expand("<afile>:r"))
2802 augroup END
2803
2804 e! Xtestfile.gz " Edit compressed file
2805 call assert_equal([
2806 \ 'line 2 Abcdefghijklmnopqrstuvwxyz',
2807 \ 'line 3 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2808 \ 'line 4 Abcdefghijklmnopqrstuvwxyz',
2809 \ 'line 5 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2810 \ 'line 6 Abcdefghijklmnopqrstuvwxyz',
2811 \ 'line 7 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2812 \ 'line 8 Abcdefghijklmnopqrstuvwxyz',
2813 \ 'line 9 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2814 \ 'line 10 Abcdefghijklmnopqrstuvwxyz'
2815 \ ], getline(1, 9))
2816
2817 w! >> test.out " Append it to the output file
2818
2819 augroup Test3
2820 au!
2821 augroup END
2822
2823 " Test for the FilterReadPre and FilterReadPost autocmds.
2824 set shelltemp " need temp files here
2825 augroup Test4
2826 au!
2827 au FilterReadPre *.out call rename(expand("<afile>"), expand("<afile>") . ".t")
2828 au FilterReadPre *.out exe 'silent !sed s/e/E/ ' . shellescape(expand("<afile>")) . ".t >" . shellescape(expand("<afile>"))
2829 au FilterReadPre *.out exe 'silent !rm ' . shellescape(expand("<afile>")) . '.t'
2830 au FilterReadPost *.out '[,']s/x/X/g
2831 augroup END
2832
2833 e! test.out " Edit the output file
2834 1,$!cat
2835 call assert_equal([
2836 \ 'linE 2 AbcdefghijklmnopqrstuvwXyz',
2837 \ 'linE 3 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
2838 \ 'linE 4 AbcdefghijklmnopqrstuvwXyz',
2839 \ 'linE 5 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
2840 \ 'linE 6 AbcdefghijklmnopqrstuvwXyz',
2841 \ 'linE 7 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
2842 \ 'linE 8 AbcdefghijklmnopqrstuvwXyz',
2843 \ 'linE 9 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
2844 \ 'linE 10 AbcdefghijklmnopqrstuvwXyz'
2845 \ ], getline(1, 9))
2846 call assert_equal([
2847 \ 'line 2 Abcdefghijklmnopqrstuvwxyz',
2848 \ 'line 3 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2849 \ 'line 4 Abcdefghijklmnopqrstuvwxyz',
2850 \ 'line 5 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2851 \ 'line 6 Abcdefghijklmnopqrstuvwxyz',
2852 \ 'line 7 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2853 \ 'line 8 Abcdefghijklmnopqrstuvwxyz',
2854 \ 'line 9 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2855 \ 'line 10 Abcdefghijklmnopqrstuvwxyz'
2856 \ ], readfile('test.out'))
2857
2858 augroup Test4
2859 au!
2860 augroup END
2861 set shelltemp&vim
2862
2863 " Test for the FileReadPre and FileReadPost autocmds.
2864 augroup Test5
2865 au!
2866 au FileReadPre *.gz exe 'silent !gzip -d ' . shellescape(expand("<afile>"))
2867 au FileReadPre *.gz call rename(expand("<afile>:r"), expand("<afile>"))
2868 au FileReadPost *.gz '[,']s/l/L/
2869 augroup END
2870
2871 new
2872 0r Xtestfile.gz " Read compressed file
2873 call assert_equal([
2874 \ 'Line 2 Abcdefghijklmnopqrstuvwxyz',
2875 \ 'Line 3 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2876 \ 'Line 4 Abcdefghijklmnopqrstuvwxyz',
2877 \ 'Line 5 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2878 \ 'Line 6 Abcdefghijklmnopqrstuvwxyz',
2879 \ 'Line 7 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2880 \ 'Line 8 Abcdefghijklmnopqrstuvwxyz',
2881 \ 'Line 9 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2882 \ 'Line 10 Abcdefghijklmnopqrstuvwxyz'
2883 \ ], getline(1, 9))
2884 call assert_equal([
2885 \ 'line 2 Abcdefghijklmnopqrstuvwxyz',
2886 \ 'line 3 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2887 \ 'line 4 Abcdefghijklmnopqrstuvwxyz',
2888 \ 'line 5 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2889 \ 'line 6 Abcdefghijklmnopqrstuvwxyz',
2890 \ 'line 7 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2891 \ 'line 8 Abcdefghijklmnopqrstuvwxyz',
2892 \ 'line 9 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2893 \ 'line 10 Abcdefghijklmnopqrstuvwxyz'
2894 \ ], readfile('Xtestfile.gz'))
2895
2896 augroup Test5
2897 au!
2898 augroup END
2899
2900 au! FileChangedShell
2901 call delete('Xtestfile.gz')
Bram Moolenaar69ea5872019-04-25 20:29:00 +02002902 call delete('test.out')
2903endfunc
Bram Moolenaar23b51392019-05-09 21:38:43 +02002904
2905func Test_throw_in_BufWritePre()
2906 new
2907 call setline(1, ['one', 'two', 'three'])
2908 call assert_false(filereadable('Xthefile'))
2909 augroup throwing
2910 au BufWritePre X* throw 'do not write'
2911 augroup END
2912 try
2913 w Xthefile
2914 catch
2915 let caught = 1
2916 endtry
2917 call assert_equal(1, caught)
2918 call assert_false(filereadable('Xthefile'))
2919
2920 bwipe!
2921 au! throwing
2922endfunc
Bram Moolenaarcadbe1b2019-09-22 21:50:09 +02002923
Bram Moolenaar40fa12a2021-09-22 14:18:13 +02002924func Test_autocmd_in_try_block()
Bram Moolenaar6f14da12022-09-07 21:30:44 +01002925 call mkdir('Xintrydir', 'R')
Bram Moolenaar40fa12a2021-09-22 14:18:13 +02002926 au BufEnter * let g:fname = expand('%')
2927 try
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +01002928 edit Xintrydir/
Bram Moolenaar40fa12a2021-09-22 14:18:13 +02002929 endtry
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +01002930 call assert_match('Xintrydir', g:fname)
Bram Moolenaar40fa12a2021-09-22 14:18:13 +02002931
2932 unlet g:fname
2933 au! BufEnter
Bram Moolenaar40fa12a2021-09-22 14:18:13 +02002934endfunc
2935
Bram Moolenaarcadbe1b2019-09-22 21:50:09 +02002936func Test_autocmd_SafeState()
2937 CheckRunVimInTerminal
2938
2939 let lines =<< trim END
2940 let g:safe = 0
2941 let g:again = ''
2942 au SafeState * let g:safe += 1
2943 au SafeStateAgain * let g:again ..= 'x'
2944 func CallTimer()
2945 call timer_start(10, {id -> execute('let g:again ..= "t"')})
2946 endfunc
2947 END
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01002948 call writefile(lines, 'XSafeState', 'D')
Bram Moolenaarcadbe1b2019-09-22 21:50:09 +02002949 let buf = RunVimInTerminal('-S XSafeState', #{rows: 6})
2950
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01002951 " Sometimes we loop to handle a K_IGNORE, SafeState may be triggered once or
Bram Moolenaar8fb1b472020-02-23 16:16:26 +01002952 " more often.
Bram Moolenaarcadbe1b2019-09-22 21:50:09 +02002953 call term_sendkeys(buf, ":echo g:safe\<CR>")
Bram Moolenaar8fb1b472020-02-23 16:16:26 +01002954 call WaitForAssert({-> assert_match('^\d ', term_getline(buf, 6))}, 1000)
Bram Moolenaarcadbe1b2019-09-22 21:50:09 +02002955
Bram Moolenaar8fb1b472020-02-23 16:16:26 +01002956 " SafeStateAgain should be invoked at least three times
Bram Moolenaarcadbe1b2019-09-22 21:50:09 +02002957 call term_sendkeys(buf, ":echo g:again\<CR>")
Bram Moolenaar8fb1b472020-02-23 16:16:26 +01002958 call WaitForAssert({-> assert_match('^xxx', term_getline(buf, 6))}, 1000)
Bram Moolenaarcadbe1b2019-09-22 21:50:09 +02002959
2960 call term_sendkeys(buf, ":let g:again = ''\<CR>:call CallTimer()\<CR>")
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02002961 call TermWait(buf, 50)
Bram Moolenaar0f6629a2019-09-22 23:24:13 +02002962 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02002963 call TermWait(buf, 50)
Bram Moolenaarcadbe1b2019-09-22 21:50:09 +02002964 call term_sendkeys(buf, ":echo g:again\<CR>")
2965 call WaitForAssert({-> assert_match('xtx', term_getline(buf, 6))}, 1000)
2966
2967 call StopVimInTerminal(buf)
Bram Moolenaarcadbe1b2019-09-22 21:50:09 +02002968endfunc
Bram Moolenaar23324a02019-10-01 17:39:04 +02002969
2970func Test_autocmd_CmdWinEnter()
2971 CheckRunVimInTerminal
Bram Moolenaar21829c52021-01-26 22:42:21 +01002972
Bram Moolenaar23324a02019-10-01 17:39:04 +02002973 let lines =<< trim END
Egor Zvorykin125ffd22021-11-17 14:01:14 +00002974 augroup vimHints | au! | augroup END
Bram Moolenaar23324a02019-10-01 17:39:04 +02002975 let b:dummy_var = 'This is a dummy'
2976 autocmd CmdWinEnter * quit
2977 let winnr = winnr('$')
2978 END
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01002979 let filename = 'XCmdWinEnter'
Bram Moolenaar23324a02019-10-01 17:39:04 +02002980 call writefile(lines, filename)
2981 let buf = RunVimInTerminal('-S '.filename, #{rows: 6})
2982
2983 call term_sendkeys(buf, "q:")
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02002984 call TermWait(buf)
Bram Moolenaar23324a02019-10-01 17:39:04 +02002985 call term_sendkeys(buf, ":echo b:dummy_var\<cr>")
Bram Moolenaar353c3512020-03-15 14:19:26 +01002986 call WaitForAssert({-> assert_match('^This is a dummy', term_getline(buf, 6))}, 2000)
Bram Moolenaar23324a02019-10-01 17:39:04 +02002987 call term_sendkeys(buf, ":echo &buftype\<cr>")
2988 call WaitForAssert({-> assert_notmatch('^nofile', term_getline(buf, 6))}, 1000)
2989 call term_sendkeys(buf, ":echo winnr\<cr>")
2990 call WaitForAssert({-> assert_match('^1', term_getline(buf, 6))}, 1000)
2991
2992 " clean up
2993 call StopVimInTerminal(buf)
2994 call delete(filename)
2995endfunc
Bram Moolenaarec66c412019-10-11 21:19:13 +02002996
2997func Test_autocmd_was_using_freed_memory()
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01002998 CheckFeature quickfix
2999
Bram Moolenaarec66c412019-10-11 21:19:13 +02003000 pedit xx
3001 n x
Bram Moolenaar92bb83e2021-02-03 23:04:46 +01003002 augroup winenter
3003 au WinEnter * if winnr('$') > 2 | quit | endif
3004 augroup END
Bram Moolenaarec66c412019-10-11 21:19:13 +02003005 split
Bram Moolenaar92bb83e2021-02-03 23:04:46 +01003006
3007 augroup winenter
3008 au! WinEnter
3009 augroup END
3010
3011 bwipe xx
3012 bwipe x
3013 pclose
Bram Moolenaarec66c412019-10-11 21:19:13 +02003014endfunc
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +01003015
3016func Test_BufWrite_lockmarks()
Bram Moolenaarf08b0eb2021-10-16 13:00:14 +01003017 let g:test_is_flaky = 1
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +01003018 edit! Xtest
3019 call setline(1, ['a', 'b', 'c', 'd'])
3020
3021 " :lockmarks preserves the marks
3022 call SetChangeMarks(2, 3)
3023 lockmarks write
3024 call assert_equal([2, 3], [line("'["), line("']")])
3025
3026 " *WritePre autocmds get the correct line range, but lockmarks preserves the
3027 " original values for the user
3028 augroup lockmarks
3029 au!
3030 au BufWritePre,FilterWritePre * call assert_equal([1, 4], [line("'["), line("']")])
3031 au FileWritePre * call assert_equal([3, 4], [line("'["), line("']")])
3032 augroup END
3033
3034 lockmarks write
3035 call assert_equal([2, 3], [line("'["), line("']")])
3036
3037 if executable('cat')
3038 lockmarks %!cat
3039 call assert_equal([2, 3], [line("'["), line("']")])
3040 endif
3041
3042 lockmarks 3,4write Xtest2
3043 call assert_equal([2, 3], [line("'["), line("']")])
3044
3045 au! lockmarks
3046 augroup! lockmarks
3047 call delete('Xtest')
3048 call delete('Xtest2')
3049endfunc
Bram Moolenaarce6db022020-01-07 20:11:42 +01003050
3051func Test_FileType_spell()
3052 if !isdirectory('/tmp')
3053 throw "Skipped: requires /tmp directory"
3054 endif
3055
3056 " this was crashing with an invalid free()
3057 setglobal spellfile=/tmp/en.utf-8.add
3058 augroup crash
3059 autocmd!
3060 autocmd BufNewFile,BufReadPost crashfile setf somefiletype
3061 autocmd BufNewFile,BufReadPost crashfile set ft=anotherfiletype
3062 autocmd FileType anotherfiletype setlocal spell
3063 augroup END
3064 func! NoCrash() abort
3065 edit /tmp/crashfile
3066 endfunc
3067 call NoCrash()
3068
3069 au! crash
3070 setglobal spellfile=
3071endfunc
Bram Moolenaarbc2b71d2020-02-17 21:33:30 +01003072
Bram Moolenaaref976322022-09-28 11:48:30 +01003073" this was wiping out the current buffer and using freed memory
3074func Test_SpellFileMissing_bwipe()
3075 next 0
3076 au SpellFileMissing 0 bwipe
3077 call assert_fails('set spell spelllang=0', 'E937:')
3078
3079 au! SpellFileMissing
Bram Moolenaar0a60f792022-11-19 21:18:11 +00003080 set nospell spelllang=en
Bram Moolenaaref976322022-09-28 11:48:30 +01003081 bwipe
3082endfunc
3083
Bram Moolenaar406cd902020-02-18 21:54:41 +01003084" Test closing a window or editing another buffer from a FileChangedRO handler
3085" in a readonly buffer
3086func Test_FileChangedRO_winclose()
Bram Moolenaar62cd26a2020-10-11 20:08:44 +02003087 call test_override('ui_delay', 10)
3088
Bram Moolenaar406cd902020-02-18 21:54:41 +01003089 augroup FileChangedROTest
3090 au!
3091 autocmd FileChangedRO * quit
3092 augroup END
3093 new
3094 set readonly
3095 call assert_fails('normal i', 'E788:')
3096 close
3097 augroup! FileChangedROTest
3098
3099 augroup FileChangedROTest
3100 au!
Bram Moolenaar61abe7d2022-08-30 21:46:08 +01003101 autocmd FileChangedRO * edit Xrofile
Bram Moolenaar406cd902020-02-18 21:54:41 +01003102 augroup END
3103 new
3104 set readonly
3105 call assert_fails('normal i', 'E788:')
3106 close
3107 augroup! FileChangedROTest
Bram Moolenaar62cd26a2020-10-11 20:08:44 +02003108 call test_override('ALL', 0)
Bram Moolenaar406cd902020-02-18 21:54:41 +01003109endfunc
3110
Bram Moolenaar0c81d1b2020-02-22 22:45:55 +01003111func LogACmd()
3112 call add(g:logged, line('$'))
3113endfunc
3114
3115func Test_TermChanged()
Bram Moolenaard28e0b32020-02-22 23:08:52 +01003116 CheckNotGui
3117
Bram Moolenaar0c81d1b2020-02-22 22:45:55 +01003118 enew!
3119 tabnew
3120 call setline(1, ['a', 'b', 'c', 'd'])
3121 $
3122 au TermChanged * call LogACmd()
3123 let g:logged = []
3124 let term_save = &term
3125 set term=xterm
3126 call assert_equal([1, 4], g:logged)
3127
3128 au! TermChanged
3129 let &term = term_save
3130 bwipe!
3131endfunc
3132
Bram Moolenaare3284872020-03-19 13:55:03 +01003133" Test for FileReadCmd autocmd
3134func Test_autocmd_FileReadCmd()
3135 func ReadFileCmd()
3136 call append(line('$'), "v:cmdarg = " .. v:cmdarg)
3137 endfunc
3138 augroup FileReadCmdTest
3139 au!
3140 au FileReadCmd Xtest call ReadFileCmd()
3141 augroup END
3142
3143 new
3144 read ++bin Xtest
3145 read ++nobin Xtest
3146 read ++edit Xtest
3147 read ++bad=keep Xtest
3148 read ++bad=drop Xtest
3149 read ++bad=- Xtest
3150 read ++ff=unix Xtest
3151 read ++ff=dos Xtest
3152 read ++ff=mac Xtest
3153 read ++enc=utf-8 Xtest
3154
3155 call assert_equal(['',
3156 \ 'v:cmdarg = ++bin',
3157 \ 'v:cmdarg = ++nobin',
3158 \ 'v:cmdarg = ++edit',
3159 \ 'v:cmdarg = ++bad=keep',
3160 \ 'v:cmdarg = ++bad=drop',
3161 \ 'v:cmdarg = ++bad=-',
3162 \ 'v:cmdarg = ++ff=unix',
3163 \ 'v:cmdarg = ++ff=dos',
3164 \ 'v:cmdarg = ++ff=mac',
3165 \ 'v:cmdarg = ++enc=utf-8'], getline(1, '$'))
3166
Bram Moolenaar23526d22022-12-05 15:50:41 +00003167 bwipe!
Bram Moolenaare3284872020-03-19 13:55:03 +01003168 augroup FileReadCmdTest
3169 au!
3170 augroup END
3171 delfunc ReadFileCmd
3172endfunc
3173
Bram Moolenaaree4e0c12020-04-06 21:35:05 +02003174" Test for passing invalid arguments to autocmd
3175func Test_autocmd_invalid_args()
3176 " Additional character after * for event
Bram Moolenaar61abe7d2022-08-30 21:46:08 +01003177 call assert_fails('autocmd *a Xinvfile set ff=unix', 'E215:')
Bram Moolenaaree4e0c12020-04-06 21:35:05 +02003178 augroup Test
3179 augroup END
3180 " Invalid autocmd event
Bram Moolenaar61abe7d2022-08-30 21:46:08 +01003181 call assert_fails('autocmd Bufabc Xinvfile set ft=vim', 'E216:')
Bram Moolenaaree4e0c12020-04-06 21:35:05 +02003182 " Invalid autocmd event in a autocmd group
Bram Moolenaar61abe7d2022-08-30 21:46:08 +01003183 call assert_fails('autocmd Test Bufabc Xinvfile set ft=vim', 'E216:')
Bram Moolenaaree4e0c12020-04-06 21:35:05 +02003184 augroup! Test
3185 " Execute all autocmds
3186 call assert_fails('doautocmd * BufEnter', 'E217:')
3187 call assert_fails('augroup! x1a2b3', 'E367:')
3188 call assert_fails('autocmd BufNew <buffer=999> pwd', 'E680:')
Bram Moolenaar531be472020-09-23 22:38:05 +02003189 call assert_fails('autocmd BufNew \) set ff=unix', 'E55:')
Bram Moolenaaree4e0c12020-04-06 21:35:05 +02003190endfunc
3191
3192" Test for deep nesting of autocmds
3193func Test_autocmd_deep_nesting()
Bram Moolenaar61abe7d2022-08-30 21:46:08 +01003194 autocmd BufEnter Xdeepfile doautocmd BufEnter Xdeepfile
3195 call assert_fails('doautocmd BufEnter Xdeepfile', 'E218:')
3196 autocmd! BufEnter Xdeepfile
Bram Moolenaaree4e0c12020-04-06 21:35:05 +02003197endfunc
3198
Bram Moolenaarbe5ee862020-06-10 20:56:58 +02003199" Tests for SigUSR1 autocmd event, which is only available on posix systems.
3200func Test_autocmd_sigusr1()
3201 CheckUnix
Bram Moolenaar0056ca72022-09-23 21:26:39 +01003202 " FIXME: should this work on MacOS M1?
3203 CheckNotMacM1
Bram Moolenaar62cd26a2020-10-11 20:08:44 +02003204 CheckExecutable /bin/kill
Bram Moolenaarbe5ee862020-06-10 20:56:58 +02003205
3206 let g:sigusr1_passed = 0
3207 au SigUSR1 * let g:sigusr1_passed = 1
3208 call system('/bin/kill -s usr1 ' . getpid())
3209 call WaitForAssert({-> assert_true(g:sigusr1_passed)})
3210
3211 au! SigUSR1
3212 unlet g:sigusr1_passed
3213endfunc
3214
Bram Moolenaarb340bae2020-06-15 19:51:56 +02003215" Test for BufReadPre autocmd deleting the file
3216func Test_BufReadPre_delfile()
3217 augroup TestAuCmd
3218 au!
Bram Moolenaare7cda972022-08-29 11:02:59 +01003219 autocmd BufReadPre XbufreadPre call delete('XbufreadPre')
Bram Moolenaarb340bae2020-06-15 19:51:56 +02003220 augroup END
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01003221 call writefile([], 'XbufreadPre', 'D')
Bram Moolenaare7cda972022-08-29 11:02:59 +01003222 call assert_fails('new XbufreadPre', 'E200:')
3223 call assert_equal('XbufreadPre', @%)
Bram Moolenaarb340bae2020-06-15 19:51:56 +02003224 call assert_equal(1, &readonly)
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01003225
Bram Moolenaarb340bae2020-06-15 19:51:56 +02003226 augroup TestAuCmd
3227 au!
3228 augroup END
3229 close!
3230endfunc
3231
3232" Test for BufReadPre autocmd changing the current buffer
3233func Test_BufReadPre_changebuf()
3234 augroup TestAuCmd
3235 au!
Bram Moolenaare7cda972022-08-29 11:02:59 +01003236 autocmd BufReadPre Xchangebuf edit Xsomeotherfile
Bram Moolenaarb340bae2020-06-15 19:51:56 +02003237 augroup END
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01003238 call writefile([], 'Xchangebuf', 'D')
Bram Moolenaare7cda972022-08-29 11:02:59 +01003239 call assert_fails('new Xchangebuf', 'E201:')
Bram Moolenaarb340bae2020-06-15 19:51:56 +02003240 call assert_equal('Xsomeotherfile', @%)
3241 call assert_equal(1, &readonly)
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01003242
Bram Moolenaarb340bae2020-06-15 19:51:56 +02003243 augroup TestAuCmd
3244 au!
3245 augroup END
3246 close!
3247endfunc
3248
3249" Test for BufWipeouti autocmd changing the current buffer when reading a file
3250" in an empty buffer with 'f' flag in 'cpo'
3251func Test_BufDelete_changebuf()
3252 new
3253 augroup TestAuCmd
3254 au!
3255 autocmd BufWipeout * let bufnr = bufadd('somefile') | exe "b " .. bufnr
3256 augroup END
3257 let save_cpo = &cpo
3258 set cpo+=f
Bram Moolenaarb18b4962022-09-02 21:55:50 +01003259 call assert_fails('r Xchangebuf', ['E812:', 'E484:'])
Bram Moolenaarb340bae2020-06-15 19:51:56 +02003260 call assert_equal('somefile', @%)
3261 let &cpo = save_cpo
3262 augroup TestAuCmd
3263 au!
3264 augroup END
3265 close!
3266endfunc
3267
Bram Moolenaar0fe937f2020-06-16 22:42:04 +02003268" Test for the temporary internal window used to execute autocmds
3269func Test_autocmd_window()
3270 %bw!
3271 edit one.txt
3272 tabnew two.txt
Bram Moolenaar41cd8032021-03-13 15:47:56 +01003273 vnew three.txt
3274 tabnew four.txt
3275 tabprevious
Bram Moolenaar0fe937f2020-06-16 22:42:04 +02003276 let g:blist = []
Bram Moolenaar832adf92020-06-25 19:01:36 +02003277 augroup aucmd_win_test1
Bram Moolenaar0fe937f2020-06-16 22:42:04 +02003278 au!
3279 au BufEnter * call add(g:blist, [expand('<afile>'),
3280 \ win_gettype(bufwinnr(expand('<afile>')))])
3281 augroup END
3282
3283 doautoall BufEnter
Bram Moolenaar41cd8032021-03-13 15:47:56 +01003284 call assert_equal([
3285 \ ['one.txt', 'autocmd'],
3286 \ ['two.txt', ''],
3287 \ ['four.txt', 'autocmd'],
3288 \ ['three.txt', ''],
3289 \ ], g:blist)
Bram Moolenaar0fe937f2020-06-16 22:42:04 +02003290
Bram Moolenaar832adf92020-06-25 19:01:36 +02003291 augroup aucmd_win_test1
Bram Moolenaar0fe937f2020-06-16 22:42:04 +02003292 au!
3293 augroup END
Bram Moolenaar832adf92020-06-25 19:01:36 +02003294 augroup! aucmd_win_test1
3295 %bw!
3296endfunc
3297
3298" Test for trying to close the temporary window used for executing an autocmd
3299func Test_close_autocmd_window()
3300 %bw!
3301 edit one.txt
3302 tabnew two.txt
3303 augroup aucmd_win_test2
3304 au!
3305 au BufEnter * if expand('<afile>') == 'one.txt' | 1close | endif
3306 augroup END
3307
3308 call assert_fails('doautoall BufEnter', 'E813:')
3309
3310 augroup aucmd_win_test2
3311 au!
3312 augroup END
3313 augroup! aucmd_win_test2
Bram Moolenaarcf844172020-06-26 19:44:06 +02003314 %bwipe!
3315endfunc
3316
3317" Test for trying to close the tab that has the temporary window for exeucing
3318" an autocmd.
3319func Test_close_autocmd_tab()
3320 edit one.txt
3321 tabnew two.txt
3322 augroup aucmd_win_test
3323 au!
3324 au BufEnter * if expand('<afile>') == 'one.txt' | tabfirst | tabonly | endif
3325 augroup END
3326
3327 call assert_fails('doautoall BufEnter', 'E813:')
3328
3329 tabonly
3330 augroup aucmd_win_test
3331 au!
3332 augroup END
3333 augroup! aucmd_win_test
3334 %bwipe!
Bram Moolenaar0fe937f2020-06-16 22:42:04 +02003335endfunc
3336
Bram Moolenaarcb1956d2022-01-07 15:45:18 +00003337func Test_Visual_doautoall_redraw()
3338 call setline(1, ['a', 'b'])
3339 new
3340 wincmd p
3341 call feedkeys("G\<C-V>", 'txn')
3342 autocmd User Explode ++once redraw
3343 doautoall User Explode
3344 %bwipe!
3345endfunc
3346
Bram Moolenaar6bcb8772021-02-03 21:23:29 +01003347" This was using freed memory.
3348func Test_BufNew_arglocal()
3349 arglocal
3350 au BufNew * arglocal
3351 call assert_fails('drop xx', 'E1156:')
3352
3353 au! BufNew
3354endfunc
3355
Bram Moolenaar8ab37572021-02-03 21:56:59 +01003356func Test_autocmd_closes_window()
3357 au BufNew,BufWinLeave * e %e
3358 file yyy
3359 au BufNew,BufWinLeave * ball
Bram Moolenaaraad5f9d2021-02-06 17:30:31 +01003360 n xxx
Bram Moolenaar8ab37572021-02-03 21:56:59 +01003361
Bram Moolenaaraad5f9d2021-02-06 17:30:31 +01003362 %bwipe
Bram Moolenaar8ab37572021-02-03 21:56:59 +01003363 au! BufNew
3364 au! BufWinLeave
3365endfunc
3366
Bram Moolenaar92bb83e2021-02-03 23:04:46 +01003367func Test_autocmd_quit_psearch()
3368 sn aa bb
3369 augroup aucmd_win_test
3370 au!
3371 au BufEnter,BufLeave,BufNew,WinEnter,WinLeave,WinNew * if winnr('$') > 1 | q | endif
3372 augroup END
3373 ps /
3374
3375 augroup aucmd_win_test
3376 au!
3377 augroup END
zeertzjq7851c692022-04-21 11:14:01 +01003378 new
3379 pclose
Bram Moolenaar92bb83e2021-02-03 23:04:46 +01003380endfunc
3381
Bram Moolenaaraad5f9d2021-02-06 17:30:31 +01003382" Fuzzer found some strange combination that caused a crash.
3383func Test_autocmd_normal_mess()
Bram Moolenaardd07c022021-02-07 13:32:46 +01003384 " For unknown reason this hangs on MS-Windows
3385 CheckNotMSWindows
3386
Bram Moolenaaraad5f9d2021-02-06 17:30:31 +01003387 augroup aucmd_normal_test
3388 au BufLeave,BufWinLeave,BufHidden,BufUnload,BufDelete,BufWipeout * norm 7q/qc
3389 augroup END
Bram Moolenaar983d83f2021-02-07 12:12:43 +01003390 call assert_fails('o4', 'E1159')
Bram Moolenaaraad5f9d2021-02-06 17:30:31 +01003391 silent! H
Bram Moolenaar983d83f2021-02-07 12:12:43 +01003392 call assert_fails('e xx', 'E1159')
Bram Moolenaaraad5f9d2021-02-06 17:30:31 +01003393 normal G
3394
3395 augroup aucmd_normal_test
3396 au!
3397 augroup END
3398endfunc
3399
Bram Moolenaar8c6951f2021-02-06 18:08:45 +01003400func Test_autocmd_closing_cmdwin()
Bram Moolenaardd07c022021-02-07 13:32:46 +01003401 " For unknown reason this hangs on MS-Windows
3402 CheckNotMSWindows
3403
Bram Moolenaar8c6951f2021-02-06 18:08:45 +01003404 au BufWinLeave * nested q
3405 call assert_fails("norm 7q?\n", 'E855:')
3406
3407 au! BufWinLeave
3408 new
3409 only
3410endfunc
3411
Bram Moolenaar2c7080b2021-02-06 19:19:42 +01003412func Test_autocmd_vimgrep()
3413 augroup aucmd_vimgrep
Charlie Grovesfef44852022-04-19 16:24:12 +01003414 au QuickfixCmdPre,BufNew,BufReadCmd * sb
zeertzjq7851c692022-04-21 11:14:01 +01003415 au QuickfixCmdPre,BufNew,BufReadCmd * q9
Bram Moolenaar2c7080b2021-02-06 19:19:42 +01003416 augroup END
Bram Moolenaardd07c022021-02-07 13:32:46 +01003417 call assert_fails('lv ?a? foo', 'E926:')
Bram Moolenaar2c7080b2021-02-06 19:19:42 +01003418
3419 augroup aucmd_vimgrep
3420 au!
3421 augroup END
3422endfunc
3423
Bram Moolenaar73b8b0a2021-08-01 14:52:32 +02003424func Test_autocmd_with_block()
3425 augroup block_testing
3426 au BufReadPost *.xml {
3427 setlocal matchpairs+=<:>
3428 /<start
3429 }
Bram Moolenaar63b91732021-08-05 20:40:03 +02003430 au CursorHold * {
3431 autocmd BufReadPre * ++once echo 'one' | echo 'two'
3432 g:gotSafeState = 77
3433 }
Bram Moolenaar73b8b0a2021-08-01 14:52:32 +02003434 augroup END
3435
3436 let expected = "\n--- Autocommands ---\nblock_testing BufRead\n *.xml {^@ setlocal matchpairs+=<:>^@ /<start^@ }"
3437 call assert_equal(expected, execute('au BufReadPost *.xml'))
3438
Bram Moolenaar63b91732021-08-05 20:40:03 +02003439 doautocmd CursorHold
3440 call assert_equal(77, g:gotSafeState)
3441 unlet g:gotSafeState
3442
Bram Moolenaar73b8b0a2021-08-01 14:52:32 +02003443 augroup block_testing
3444 au!
Bram Moolenaar75ebd2a2022-06-03 17:39:46 +01003445 autocmd CursorHold * {
3446 if true
3447 # comment
3448 && true
3449
3450 && true
3451 g:done = 'yes'
3452 endif
3453 }
3454 augroup END
3455 doautocmd CursorHold
3456 call assert_equal('yes', g:done)
3457
3458 unlet g:done
3459 augroup block_testing
3460 au!
Bram Moolenaar73b8b0a2021-08-01 14:52:32 +02003461 augroup END
3462endfunc
3463
Christian Brabandtdb3b4462021-10-16 11:58:55 +01003464" Test TextChangedI and TextChanged
3465func Test_Changed_ChangedI()
3466 new
3467 call test_override("char_avail", 1)
3468 let [g:autocmd_i, g:autocmd_n] = ['','']
3469
3470 func! TextChangedAutocmdI(char)
3471 let g:autocmd_{tolower(a:char)} = a:char .. b:changedtick
3472 endfunc
3473
3474 augroup Test_TextChanged
3475 au!
3476 au TextChanged <buffer> :call TextChangedAutocmdI('N')
3477 au TextChangedI <buffer> :call TextChangedAutocmdI('I')
3478 augroup END
3479
3480 call feedkeys("ifoo\<esc>", 'tnix')
3481 " TODO: Test test does not seem to trigger TextChanged autocommand, this
3482 " requires running Vim in a terminal window.
3483 " call assert_equal('N3', g:autocmd_n)
3484 call assert_equal('I3', g:autocmd_i)
3485
3486 call feedkeys("yyp", 'tnix')
3487 " TODO: Test test does not seem to trigger TextChanged autocommand.
3488 " call assert_equal('N4', g:autocmd_n)
3489 call assert_equal('I3', g:autocmd_i)
3490
3491 " CleanUp
3492 call test_override("char_avail", 0)
3493 au! TextChanged <buffer>
3494 au! TextChangedI <buffer>
3495 augroup! Test_TextChanged
3496 delfu TextChangedAutocmdI
3497 unlet! g:autocmd_i g:autocmd_n
3498
3499 bw!
3500endfunc
Bram Moolenaar2c7080b2021-02-06 19:19:42 +01003501
Bram Moolenaar6f2465d2022-03-22 18:13:01 +00003502func Test_closing_autocmd_window()
3503 let lines =<< trim END
3504 edit Xa.txt
3505 tabnew Xb.txt
3506 autocmd BufEnter Xa.txt unhide 1
3507 doautoall BufEnter
3508 END
3509 call v9.CheckScriptFailure(lines, 'E814:')
3510 au! BufEnter
3511 only!
3512 bwipe Xa.txt
3513 bwipe Xb.txt
3514endfunc
3515
Bram Moolenaar347538f2022-03-26 16:28:06 +00003516func Test_bufwipeout_changes_window()
3517 " This should not crash, but we don't have any expectations about what
3518 " happens, changing window in BufWipeout has unpredictable results.
3519 tabedit
3520 let g:window_id = win_getid()
3521 topleft new
3522 setlocal bufhidden=wipe
3523 autocmd BufWipeout <buffer> call win_gotoid(g:window_id)
3524 tabprevious
3525 +tabclose
3526
3527 unlet g:window_id
3528 au! BufWipeout
3529 %bwipe!
3530endfunc
3531
zeertzjq021996f2022-04-10 11:44:04 +01003532func Test_v_event_readonly()
3533 autocmd CompleteChanged * let v:event.width = 0
3534 call assert_fails("normal! i\<C-X>\<C-V>", 'E46:')
3535 au! CompleteChanged
3536
3537 autocmd DirChangedPre * let v:event.directory = ''
3538 call assert_fails('cd .', 'E46:')
3539 au! DirChangedPre
3540
3541 autocmd ModeChanged * let v:event.new_mode = ''
3542 call assert_fails('normal! cc', 'E46:')
3543 au! ModeChanged
3544
3545 autocmd TextYankPost * let v:event.operator = ''
3546 call assert_fails('normal! yy', 'E46:')
3547 au! TextYankPost
3548endfunc
3549
zeertzjqc9e8fd62022-07-26 18:12:38 +01003550" Test for ModeChanged pattern
3551func Test_mode_changes()
3552 let g:index = 0
3553 let g:mode_seq = ['n', 'i', 'n', 'v', 'V', 'i', 'ix', 'i', 'ic', 'i', 'n', 'no', 'n', 'V', 'v', 's', 'n']
3554 func! TestMode()
3555 call assert_equal(g:mode_seq[g:index], get(v:event, "old_mode"))
3556 call assert_equal(g:mode_seq[g:index + 1], get(v:event, "new_mode"))
3557 call assert_equal(mode(1), get(v:event, "new_mode"))
3558 let g:index += 1
3559 endfunc
3560
3561 au ModeChanged * :call TestMode()
3562 let g:n_to_any = 0
3563 au ModeChanged n:* let g:n_to_any += 1
3564 call feedkeys("i\<esc>vVca\<CR>\<C-X>\<C-L>\<esc>ggdG", 'tnix')
3565
3566 let g:V_to_v = 0
3567 au ModeChanged V:v let g:V_to_v += 1
3568 call feedkeys("Vv\<C-G>\<esc>", 'tnix')
3569 call assert_equal(len(filter(g:mode_seq[1:], {idx, val -> val == 'n'})), g:n_to_any)
3570 call assert_equal(1, g:V_to_v)
3571 call assert_equal(len(g:mode_seq) - 1, g:index)
3572
3573 let g:n_to_i = 0
3574 au ModeChanged n:i let g:n_to_i += 1
3575 let g:n_to_niI = 0
3576 au ModeChanged i:niI let g:n_to_niI += 1
3577 let g:niI_to_i = 0
3578 au ModeChanged niI:i let g:niI_to_i += 1
3579 let g:nany_to_i = 0
3580 au ModeChanged n*:i let g:nany_to_i += 1
3581 let g:i_to_n = 0
3582 au ModeChanged i:n let g:i_to_n += 1
3583 let g:nori_to_any = 0
3584 au ModeChanged [ni]:* let g:nori_to_any += 1
3585 let g:i_to_any = 0
3586 au ModeChanged i:* let g:i_to_any += 1
3587 let g:index = 0
3588 let g:mode_seq = ['n', 'i', 'niI', 'i', 'n']
3589 call feedkeys("a\<C-O>l\<esc>", 'tnix')
3590 call assert_equal(len(g:mode_seq) - 1, g:index)
3591 call assert_equal(1, g:n_to_i)
3592 call assert_equal(1, g:n_to_niI)
3593 call assert_equal(1, g:niI_to_i)
3594 call assert_equal(2, g:nany_to_i)
3595 call assert_equal(1, g:i_to_n)
3596 call assert_equal(2, g:i_to_any)
3597 call assert_equal(3, g:nori_to_any)
3598
3599 if has('terminal')
3600 let g:mode_seq += ['c', 'n', 't', 'nt', 'c', 'nt', 'n']
3601 call feedkeys(":term\<CR>\<C-W>N:bd!\<CR>", 'tnix')
3602 call assert_equal(len(g:mode_seq) - 1, g:index)
3603 call assert_equal(1, g:n_to_i)
3604 call assert_equal(1, g:n_to_niI)
3605 call assert_equal(1, g:niI_to_i)
3606 call assert_equal(2, g:nany_to_i)
3607 call assert_equal(1, g:i_to_n)
3608 call assert_equal(2, g:i_to_any)
3609 call assert_equal(5, g:nori_to_any)
3610 endif
3611
zeertzjqd1955982022-10-05 11:24:46 +01003612 let g:n_to_c = 0
3613 au ModeChanged n:c let g:n_to_c += 1
3614 let g:c_to_n = 0
3615 au ModeChanged c:n let g:c_to_n += 1
3616 let g:mode_seq += ['c', 'n', 'c', 'n']
3617 call feedkeys("q:\<C-C>\<Esc>", 'tnix')
3618 call assert_equal(len(g:mode_seq) - 1, g:index)
3619 call assert_equal(2, g:n_to_c)
3620 call assert_equal(2, g:c_to_n)
3621 unlet g:n_to_c
3622 unlet g:c_to_n
zeertzjqc9e8fd62022-07-26 18:12:38 +01003623
Bram Moolenaar61c4b042022-10-18 15:10:11 +01003624 let g:n_to_v = 0
3625 au ModeChanged n:v let g:n_to_v += 1
3626 let g:v_to_n = 0
3627 au ModeChanged v:n let g:v_to_n += 1
3628 let g:mode_seq += ['v', 'n']
3629 call feedkeys("v\<C-C>", 'tnix')
3630 call assert_equal(len(g:mode_seq) - 1, g:index)
3631 call assert_equal(1, g:n_to_v)
3632 call assert_equal(1, g:v_to_n)
3633 unlet g:n_to_v
3634 unlet g:v_to_n
3635
zeertzjqc9e8fd62022-07-26 18:12:38 +01003636 au! ModeChanged
3637 delfunc TestMode
3638 unlet! g:mode_seq
3639 unlet! g:index
3640 unlet! g:n_to_any
3641 unlet! g:V_to_v
3642 unlet! g:n_to_i
3643 unlet! g:n_to_niI
3644 unlet! g:niI_to_i
3645 unlet! g:nany_to_i
3646 unlet! g:i_to_n
3647 unlet! g:nori_to_any
3648 unlet! g:i_to_any
3649endfunc
3650
3651func Test_recursive_ModeChanged()
3652 au! ModeChanged * norm 0u
3653 sil! norm 
3654 au! ModeChanged
3655endfunc
3656
3657func Test_ModeChanged_starts_visual()
3658 " This was triggering ModeChanged before setting VIsual, causing a crash.
3659 au! ModeChanged * norm 0u
3660 sil! norm 
3661
3662 au! ModeChanged
3663endfunc
Bram Moolenaar347538f2022-03-26 16:28:06 +00003664
Charlie Grovesfef44852022-04-19 16:24:12 +01003665func Test_noname_autocmd()
3666 augroup test_noname_autocmd_group
3667 autocmd!
3668 autocmd BufEnter * call add(s:li, ["BufEnter", expand("<afile>")])
3669 autocmd BufDelete * call add(s:li, ["BufDelete", expand("<afile>")])
3670 autocmd BufLeave * call add(s:li, ["BufLeave", expand("<afile>")])
3671 autocmd BufUnload * call add(s:li, ["BufUnload", expand("<afile>")])
3672 autocmd BufWipeout * call add(s:li, ["BufWipeout", expand("<afile>")])
3673 augroup END
3674
3675 let s:li = []
3676 edit foo
3677 call assert_equal([['BufUnload', ''], ['BufDelete', ''], ['BufWipeout', ''], ['BufEnter', 'foo']], s:li)
3678
3679 au! test_noname_autocmd_group
3680 augroup! test_noname_autocmd_group
3681endfunc
3682
Yegappan Lakshmanan1755a912022-05-19 10:31:47 +01003683" Test for the autocmd_get() function
3684func Test_autocmd_get()
3685 augroup TestAutoCmdFns
3686 au!
3687 autocmd BufAdd *.vim echo "bufadd-vim"
3688 autocmd BufAdd *.py echo "bufadd-py"
3689 autocmd BufHidden *.vim echo "bufhidden"
3690 augroup END
3691 augroup TestAutoCmdFns2
3692 autocmd BufAdd *.vim echo "bufadd-vim-2"
3693 autocmd BufRead *.a1b2c3 echo "bufadd-vim-2"
3694 augroup END
3695
3696 let l = autocmd_get()
3697 call assert_true(l->len() > 0)
3698
3699 " Test for getting all the autocmds in a group
3700 let expected = [
3701 \ #{cmd: 'echo "bufadd-vim"', group: 'TestAutoCmdFns',
3702 \ pattern: '*.vim', nested: v:false, once: v:false,
3703 \ event: 'BufAdd'},
3704 \ #{cmd: 'echo "bufadd-py"', group: 'TestAutoCmdFns',
3705 \ pattern: '*.py', nested: v:false, once: v:false,
3706 \ event: 'BufAdd'},
3707 \ #{cmd: 'echo "bufhidden"', group: 'TestAutoCmdFns',
3708 \ pattern: '*.vim', nested: v:false,
3709 \ once: v:false, event: 'BufHidden'}]
3710 call assert_equal(expected, autocmd_get(#{group: 'TestAutoCmdFns'}))
3711
3712 " Test for getting autocmds for all the patterns in a group
3713 call assert_equal(expected, autocmd_get(#{group: 'TestAutoCmdFns',
3714 \ event: '*'}))
3715
3716 " Test for getting autocmds for an event in a group
3717 let expected = [
3718 \ #{cmd: 'echo "bufadd-vim"', group: 'TestAutoCmdFns',
3719 \ pattern: '*.vim', nested: v:false, once: v:false,
3720 \ event: 'BufAdd'},
3721 \ #{cmd: 'echo "bufadd-py"', group: 'TestAutoCmdFns',
3722 \ pattern: '*.py', nested: v:false, once: v:false,
3723 \ event: 'BufAdd'}]
3724 call assert_equal(expected, autocmd_get(#{group: 'TestAutoCmdFns',
3725 \ event: 'BufAdd'}))
3726
3727 " Test for getting the autocmds for all the events in a group for particular
3728 " pattern
3729 call assert_equal([{'cmd': 'echo "bufadd-py"', 'group': 'TestAutoCmdFns',
3730 \ 'pattern': '*.py', 'nested': v:false, 'once': v:false,
3731 \ 'event': 'BufAdd'}],
3732 \ autocmd_get(#{group: 'TestAutoCmdFns', event: '*', pattern: '*.py'}))
3733
3734 " Test for getting the autocmds for an events in a group for particular
3735 " pattern
3736 let l = autocmd_get(#{group: 'TestAutoCmdFns', event: 'BufAdd',
3737 \ pattern: '*.vim'})
3738 call assert_equal([
3739 \ #{cmd: 'echo "bufadd-vim"', group: 'TestAutoCmdFns',
3740 \ pattern: '*.vim', nested: v:false, once: v:false,
3741 \ event: 'BufAdd'}], l)
3742
3743 " Test for getting the autocmds for a pattern in a group
3744 let l = autocmd_get(#{group: 'TestAutoCmdFns', pattern: '*.vim'})
3745 call assert_equal([
3746 \ #{cmd: 'echo "bufadd-vim"', group: 'TestAutoCmdFns',
3747 \ pattern: '*.vim', nested: v:false, once: v:false,
3748 \ event: 'BufAdd'},
3749 \ #{cmd: 'echo "bufhidden"', group: 'TestAutoCmdFns',
3750 \ pattern: '*.vim', nested: v:false,
3751 \ once: v:false, event: 'BufHidden'}], l)
3752
3753 " Test for getting the autocmds for a pattern in all the groups
3754 let l = autocmd_get(#{pattern: '*.a1b2c3'})
3755 call assert_equal([{'cmd': 'echo "bufadd-vim-2"', 'group': 'TestAutoCmdFns2',
3756 \ 'pattern': '*.a1b2c3', 'nested': v:false, 'once': v:false,
3757 \ 'event': 'BufRead'}], l)
3758
3759 " Test for getting autocmds for a pattern without any autocmds
3760 call assert_equal([], autocmd_get(#{group: 'TestAutoCmdFns',
3761 \ pattern: '*.abc'}))
3762 call assert_equal([], autocmd_get(#{group: 'TestAutoCmdFns',
3763 \ event: 'BufAdd', pattern: '*.abc'}))
3764 call assert_equal([], autocmd_get(#{group: 'TestAutoCmdFns',
3765 \ event: 'BufWipeout'}))
3766 call assert_fails("call autocmd_get(#{group: 'abc', event: 'BufAdd'})",
3767 \ 'E367:')
3768 let cmd = "echo autocmd_get(#{group: 'TestAutoCmdFns', event: 'abc'})"
3769 call assert_fails(cmd, 'E216:')
3770 call assert_fails("call autocmd_get(#{group: 'abc'})", 'E367:')
3771 call assert_fails("echo autocmd_get(#{event: 'abc'})", 'E216:')
3772
3773 augroup TestAutoCmdFns
3774 au!
3775 augroup END
3776 call assert_equal([], autocmd_get(#{group: 'TestAutoCmdFns'}))
3777
3778 " Test for nested and once autocmds
3779 augroup TestAutoCmdFns
3780 au!
3781 autocmd VimSuspend * ++nested echo "suspend"
3782 autocmd VimResume * ++once echo "resume"
3783 augroup END
3784
3785 let expected = [
3786 \ {'cmd': 'echo "suspend"', 'group': 'TestAutoCmdFns', 'pattern': '*',
3787 \ 'nested': v:true, 'once': v:false, 'event': 'VimSuspend'},
3788 \ {'cmd': 'echo "resume"', 'group': 'TestAutoCmdFns', 'pattern': '*',
3789 \ 'nested': v:false, 'once': v:true, 'event': 'VimResume'}]
3790 call assert_equal(expected, autocmd_get(#{group: 'TestAutoCmdFns'}))
3791
3792 " Test for buffer-local autocmd
3793 augroup TestAutoCmdFns
3794 au!
3795 autocmd TextYankPost <buffer> echo "textyankpost"
3796 augroup END
3797
3798 let expected = [
3799 \ {'cmd': 'echo "textyankpost"', 'group': 'TestAutoCmdFns',
3800 \ 'pattern': '<buffer=' .. bufnr() .. '>', 'nested': v:false,
3801 \ 'once': v:false, 'bufnr': bufnr(), 'event': 'TextYankPost'}]
3802 call assert_equal(expected, autocmd_get(#{group: 'TestAutoCmdFns'}))
3803
3804 augroup TestAutoCmdFns
3805 au!
3806 augroup END
3807 augroup! TestAutoCmdFns
3808 augroup TestAutoCmdFns2
3809 au!
3810 augroup END
3811 augroup! TestAutoCmdFns2
3812
3813 call assert_fails("echo autocmd_get(#{group: []})", 'E730:')
3814 call assert_fails("echo autocmd_get(#{event: {}})", 'E731:')
3815 call assert_fails("echo autocmd_get([])", 'E1206:')
3816endfunc
3817
3818" Test for the autocmd_add() function
3819func Test_autocmd_add()
3820 " Define a single autocmd in a group
3821 call autocmd_add([#{group: 'TestAcSet', event: 'BufAdd', pattern: '*.sh',
3822 \ cmd: 'echo "bufadd"', once: v:true, nested: v:true}])
3823 call assert_equal([#{cmd: 'echo "bufadd"', group: 'TestAcSet',
3824 \ pattern: '*.sh', nested: v:true, once: v:true,
3825 \ event: 'BufAdd'}], autocmd_get(#{group: 'TestAcSet'}))
3826
3827 " Define two autocmds in the same group
3828 call autocmd_delete([#{group: 'TestAcSet'}])
3829 call autocmd_add([#{group: 'TestAcSet', event: 'BufAdd', pattern: '*.sh',
3830 \ cmd: 'echo "bufadd"'},
3831 \ #{group: 'TestAcSet', event: 'BufEnter', pattern: '*.sh',
3832 \ cmd: 'echo "bufenter"'}])
3833 call assert_equal([
3834 \ #{cmd: 'echo "bufadd"', group: 'TestAcSet', pattern: '*.sh',
3835 \ nested: v:false, once: v:false, event: 'BufAdd'},
3836 \ #{cmd: 'echo "bufenter"', group: 'TestAcSet', pattern: '*.sh',
3837 \ nested: v:false, once: v:false, event: 'BufEnter'}],
3838 \ autocmd_get(#{group: 'TestAcSet'}))
3839
3840 " Define a buffer-local autocmd
3841 call autocmd_delete([#{group: 'TestAcSet'}])
3842 call autocmd_add([#{group: 'TestAcSet', event: 'CursorHold',
3843 \ bufnr: bufnr(), cmd: 'echo "cursorhold"'}])
3844 call assert_equal([
3845 \ #{cmd: 'echo "cursorhold"', group: 'TestAcSet',
3846 \ pattern: '<buffer=' .. bufnr() .. '>', nested: v:false,
3847 \ once: v:false, bufnr: bufnr(), event: 'CursorHold'}],
3848 \ autocmd_get(#{group: 'TestAcSet'}))
3849
3850 " Use an invalid buffer number
3851 call autocmd_delete([#{group: 'TestAcSet'}])
3852 call autocmd_add([#{group: 'TestAcSet', event: 'BufEnter',
3853 \ bufnr: -1, cmd: 'echo "bufenter"'}])
3854 let l = [#{group: 'TestAcSet', event: 'BufAdd', bufnr: 9999,
3855 \ cmd: 'echo "bufadd"'}]
3856 call assert_fails("echo autocmd_add(l)", 'E680:')
Yegappan Lakshmanan00e977c2022-06-01 12:31:53 +01003857 let l = [#{group: 'TestAcSet', event: 'BufAdd', bufnr: 9999,
3858 \ pattern: '*.py', cmd: 'echo "bufadd"'}]
3859 call assert_fails("echo autocmd_add(l)", 'E680:')
3860 let l = [#{group: 'TestAcSet', event: 'BufAdd', bufnr: 9999,
3861 \ pattern: ['*.py', '*.c'], cmd: 'echo "bufadd"'}]
3862 call assert_fails("echo autocmd_add(l)", 'E680:')
Yegappan Lakshmanan1755a912022-05-19 10:31:47 +01003863 let l = [#{group: 'TestAcSet', event: 'BufRead', bufnr: [],
3864 \ cmd: 'echo "bufread"'}]
3865 call assert_fails("echo autocmd_add(l)", 'E745:')
3866 call assert_equal([], autocmd_get(#{group: 'TestAcSet'}))
3867
3868 " Add two commands to the same group, event and pattern
3869 call autocmd_delete([#{group: 'TestAcSet'}])
3870 call autocmd_add([#{group: 'TestAcSet', event: 'BufUnload',
3871 \ pattern: 'abc', cmd: 'echo "cmd1"'}])
3872 call autocmd_add([#{group: 'TestAcSet', event: 'BufUnload',
3873 \ pattern: 'abc', cmd: 'echo "cmd2"'}])
3874 call assert_equal([
3875 \ #{cmd: 'echo "cmd1"', group: 'TestAcSet', pattern: 'abc',
3876 \ nested: v:false, once: v:false, event: 'BufUnload'},
3877 \ #{cmd: 'echo "cmd2"', group: 'TestAcSet', pattern: 'abc',
3878 \ nested: v:false, once: v:false, event: 'BufUnload'}],
3879 \ autocmd_get(#{group: 'TestAcSet'}))
3880
3881 " When adding a new autocmd, if the autocmd 'group' is not specified, then
3882 " the current autocmd group should be used.
3883 call autocmd_delete([#{group: 'TestAcSet'}])
3884 augroup TestAcSet
3885 call autocmd_add([#{event: 'BufHidden', pattern: 'abc', cmd: 'echo "abc"'}])
3886 augroup END
3887 call assert_equal([
3888 \ #{cmd: 'echo "abc"', group: 'TestAcSet', pattern: 'abc',
3889 \ nested: v:false, once: v:false, event: 'BufHidden'}],
3890 \ autocmd_get(#{group: 'TestAcSet'}))
3891
Yegappan Lakshmanan971f6822022-05-24 11:40:11 +01003892 " Test for replacing a cmd for an event in a group
3893 call autocmd_delete([#{group: 'TestAcSet'}])
3894 call autocmd_add([#{replace: v:true, group: 'TestAcSet', event: 'BufEnter',
3895 \ pattern: '*.py', cmd: 'echo "bufenter"'}])
3896 call autocmd_add([#{replace: v:true, group: 'TestAcSet', event: 'BufEnter',
3897 \ pattern: '*.py', cmd: 'echo "bufenter"'}])
3898 call assert_equal([
3899 \ #{cmd: 'echo "bufenter"', group: 'TestAcSet', pattern: '*.py',
3900 \ nested: v:false, once: v:false, event: 'BufEnter'}],
3901 \ autocmd_get(#{group: 'TestAcSet'}))
3902
3903 " Test for adding a command for an unsupported autocmd event
Yegappan Lakshmanan1755a912022-05-19 10:31:47 +01003904 let l = [#{group: 'TestAcSet', event: 'abc', pattern: '*.sh',
3905 \ cmd: 'echo "bufadd"'}]
3906 call assert_fails('call autocmd_add(l)', 'E216:')
3907
Yegappan Lakshmanane0ff3a72022-05-27 18:05:33 +01003908 " Test for using a list of events and patterns
3909 call autocmd_delete([#{group: 'TestAcSet'}])
3910 let l = [#{group: 'TestAcSet', event: ['BufEnter', 'BufLeave'],
3911 \ pattern: ['*.py', '*.sh'], cmd: 'echo "bufcmds"'}]
3912 call autocmd_add(l)
3913 call assert_equal([
3914 \ #{cmd: 'echo "bufcmds"', group: 'TestAcSet', pattern: '*.py',
3915 \ nested: v:false, once: v:false, event: 'BufEnter'},
3916 \ #{cmd: 'echo "bufcmds"', group: 'TestAcSet', pattern: '*.sh',
3917 \ nested: v:false, once: v:false, event: 'BufEnter'},
3918 \ #{cmd: 'echo "bufcmds"', group: 'TestAcSet', pattern: '*.py',
3919 \ nested: v:false, once: v:false, event: 'BufLeave'},
3920 \ #{cmd: 'echo "bufcmds"', group: 'TestAcSet', pattern: '*.sh',
3921 \ nested: v:false, once: v:false, event: 'BufLeave'}],
3922 \ autocmd_get(#{group: 'TestAcSet'}))
3923
3924 " Test for invalid values for 'event' item
3925 call autocmd_delete([#{group: 'TestAcSet'}])
3926 let l = [#{group: 'TestAcSet', event: test_null_string(),
3927 \ pattern: "*.py", cmd: 'echo "bufcmds"'}]
3928 call assert_fails('call autocmd_add(l)', 'E928:')
3929 let l = [#{group: 'TestAcSet', event: test_null_list(),
3930 \ pattern: "*.py", cmd: 'echo "bufcmds"'}]
3931 call assert_fails('call autocmd_add(l)', 'E714:')
3932 let l = [#{group: 'TestAcSet', event: {},
3933 \ pattern: "*.py", cmd: 'echo "bufcmds"'}]
3934 call assert_fails('call autocmd_add(l)', 'E777:')
3935 let l = [#{group: 'TestAcSet', event: [{}],
3936 \ pattern: "*.py", cmd: 'echo "bufcmds"'}]
3937 call assert_fails('call autocmd_add(l)', 'E928:')
3938 let l = [#{group: 'TestAcSet', event: [test_null_string()],
3939 \ pattern: "*.py", cmd: 'echo "bufcmds"'}]
3940 call assert_fails('call autocmd_add(l)', 'E928:')
3941 let l = [#{group: 'TestAcSet', event: 'BufEnter,BufLeave',
3942 \ pattern: '*.py', cmd: 'echo "bufcmds"'}]
3943 call assert_fails('call autocmd_add(l)', 'E216:')
3944 let l = [#{group: 'TestAcSet', event: [],
3945 \ pattern: "*.py", cmd: 'echo "bufcmds"'}]
3946 call autocmd_add(l)
3947 let l = [#{group: 'TestAcSet', event: [""],
3948 \ pattern: "*.py", cmd: 'echo "bufcmds"'}]
3949 call assert_fails('call autocmd_add(l)', 'E216:')
3950 let l = [#{group: 'TestAcSet', event: "",
3951 \ pattern: "*.py", cmd: 'echo "bufcmds"'}]
3952 call autocmd_add(l)
3953 call assert_equal([], autocmd_get(#{group: 'TestAcSet'}))
3954
3955 " Test for invalid values for 'pattern' item
3956 let l = [#{group: 'TestAcSet', event: "BufEnter",
3957 \ pattern: test_null_string(), cmd: 'echo "bufcmds"'}]
Yegappan Lakshmanan00e977c2022-06-01 12:31:53 +01003958 call assert_fails('call autocmd_add(l)', 'E928:')
Yegappan Lakshmanane0ff3a72022-05-27 18:05:33 +01003959 let l = [#{group: 'TestAcSet', event: "BufEnter",
3960 \ pattern: test_null_list(), cmd: 'echo "bufcmds"'}]
3961 call assert_fails('call autocmd_add(l)', 'E714:')
3962 let l = [#{group: 'TestAcSet', event: "BufEnter",
3963 \ pattern: {}, cmd: 'echo "bufcmds"'}]
3964 call assert_fails('call autocmd_add(l)', 'E777:')
3965 let l = [#{group: 'TestAcSet', event: "BufEnter",
3966 \ pattern: [{}], cmd: 'echo "bufcmds"'}]
3967 call assert_fails('call autocmd_add(l)', 'E928:')
3968 let l = [#{group: 'TestAcSet', event: "BufEnter",
3969 \ pattern: [test_null_string()], cmd: 'echo "bufcmds"'}]
3970 call assert_fails('call autocmd_add(l)', 'E928:')
3971 let l = [#{group: 'TestAcSet', event: "BufEnter",
3972 \ pattern: [], cmd: 'echo "bufcmds"'}]
3973 call autocmd_add(l)
3974 let l = [#{group: 'TestAcSet', event: "BufEnter",
3975 \ pattern: [""], cmd: 'echo "bufcmds"'}]
3976 call autocmd_add(l)
3977 let l = [#{group: 'TestAcSet', event: "BufEnter",
3978 \ pattern: "", cmd: 'echo "bufcmds"'}]
3979 call autocmd_add(l)
3980 call assert_equal([], autocmd_get(#{group: 'TestAcSet'}))
3981
3982 let l = [#{group: 'TestAcSet', event: 'BufEnter,abc,BufLeave',
3983 \ pattern: '*.py', cmd: 'echo "bufcmds"'}]
3984 call assert_fails('call autocmd_add(l)', 'E216:')
3985
Yegappan Lakshmanan1755a912022-05-19 10:31:47 +01003986 call assert_fails("call autocmd_add({})", 'E1211:')
3987 call assert_equal(v:false, autocmd_add(test_null_list()))
3988 call assert_true(autocmd_add([[]]))
3989 call assert_true(autocmd_add([test_null_dict()]))
3990
3991 augroup TestAcSet
3992 au!
3993 augroup END
3994
3995 call autocmd_add([#{group: 'TestAcSet'}])
3996 call autocmd_add([#{group: 'TestAcSet', event: 'BufAdd'}])
3997 call autocmd_add([#{group: 'TestAcSet', pat: '*.sh'}])
3998 call autocmd_add([#{group: 'TestAcSet', cmd: 'echo "a"'}])
3999 call autocmd_add([#{group: 'TestAcSet', event: 'BufAdd', pat: '*.sh'}])
4000 call autocmd_add([#{group: 'TestAcSet', event: 'BufAdd', cmd: 'echo "a"'}])
4001 call autocmd_add([#{group: 'TestAcSet', pat: '*.sh', cmd: 'echo "a"'}])
4002 call assert_equal([], autocmd_get(#{group: 'TestAcSet'}))
4003
4004 augroup! TestAcSet
4005endfunc
4006
4007" Test for deleting autocmd events and groups
4008func Test_autocmd_delete()
4009 " Delete an event in an autocmd group
4010 augroup TestAcSet
4011 au!
4012 au BufAdd *.sh echo "bufadd"
4013 au BufEnter *.sh echo "bufenter"
4014 augroup END
4015 call autocmd_delete([#{group: 'TestAcSet', event: 'BufAdd'}])
4016 call assert_equal([#{cmd: 'echo "bufenter"', group: 'TestAcSet',
4017 \ pattern: '*.sh', nested: v:false, once: v:false,
4018 \ event: 'BufEnter'}], autocmd_get(#{group: 'TestAcSet'}))
4019
4020 " Delete all the events in an autocmd group
4021 augroup TestAcSet
4022 au BufAdd *.sh echo "bufadd"
4023 augroup END
4024 call autocmd_delete([#{group: 'TestAcSet', event: '*'}])
4025 call assert_equal([], autocmd_get(#{group: 'TestAcSet'}))
4026
4027 " Delete a non-existing autocmd group
4028 call assert_fails("call autocmd_delete([#{group: 'abc'}])", 'E367:')
4029 " Delete a non-existing autocmd event
4030 let l = [#{group: 'TestAcSet', event: 'abc'}]
4031 call assert_fails("call autocmd_delete(l)", 'E216:')
4032 " Delete a non-existing autocmd pattern
4033 let l = [#{group: 'TestAcSet', event: 'BufAdd', pat: 'abc'}]
4034 call assert_true(autocmd_delete(l))
Yegappan Lakshmanan00e977c2022-06-01 12:31:53 +01004035 " Delete an autocmd for a non-existing buffer
4036 let l = [#{event: '*', bufnr: 9999, cmd: 'echo "x"'}]
4037 call assert_fails('call autocmd_delete(l)', 'E680:')
Yegappan Lakshmanan1755a912022-05-19 10:31:47 +01004038
4039 " Delete an autocmd group
4040 augroup TestAcSet
4041 au!
4042 au BufAdd *.sh echo "bufadd"
4043 au BufEnter *.sh echo "bufenter"
4044 augroup END
4045 call autocmd_delete([#{group: 'TestAcSet'}])
4046 call assert_fails("call autocmd_get(#{group: 'TestAcSet'})", 'E367:')
4047
4048 call assert_true(autocmd_delete([[]]))
4049 call assert_true(autocmd_delete([test_null_dict()]))
4050endfunc
4051
Bram Moolenaar8f3c3c62022-10-18 17:05:54 +01004052func Test_autocmd_split_dummy()
4053 " Autocommand trying to split a window containing a dummy buffer.
4054 auto BufReadPre * exe "sbuf " .. expand("<abuf>")
4055 " Avoid the "W11" prompt
4056 au FileChangedShell * let v:fcs_choice = 'reload'
4057 func Xautocmd_changelist()
4058 cal writefile(['Xtestfile2:4:4'], 'Xerr')
4059 edit Xerr
4060 lex 'Xtestfile2:4:4'
4061 endfunc
4062 call Xautocmd_changelist()
Bram Moolenaar53c5c9f2022-10-18 17:25:03 +01004063 " Should get E86, but it doesn't always happen (timing?)
4064 silent! call Xautocmd_changelist()
Bram Moolenaar8f3c3c62022-10-18 17:05:54 +01004065
4066 au! BufReadPre
4067 au! FileChangedShell
4068 delfunc Xautocmd_changelist
4069 bwipe! Xerr
4070 call delete('Xerr')
4071endfunc
4072
Bram Moolenaare76062c2022-11-28 18:51:43 +00004073" This was crashing because there was only one window to execute autocommands
4074" in.
4075func Test_autocmd_nested_setbufvar()
4076 CheckFeature python3
4077
4078 set hidden
4079 edit Xaaa
4080 edit Xbbb
4081 call setline(1, 'bar')
4082 enew
4083 au BufWriteCmd Xbbb ++nested call setbufvar('Xaaa', '&ft', 'foo') | bw! Xaaa
4084 au FileType foo call py3eval('vim.current.buffer.options["cindent"]')
4085 wall
4086
4087 au! BufWriteCmd
4088 au! FileType foo
4089 set nohidden
4090 call delete('Xaaa')
4091 call delete('Xbbb')
4092 %bwipe!
4093endfunc
4094
4095
Bram Moolenaarbc2b71d2020-02-17 21:33:30 +01004096" vim: shiftwidth=2 sts=2 expandtab