blob: 8931678d74ed30728af3128b96d7bb1e4457cc50 [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 Moolenaar94722c52023-01-28 19:19:03 +0000272 au WinEnter * call add(g:record, 'WinEnter')
273 au WinLeave * call add(g:record, 'WinLeave')
Bram Moolenaarc917da42016-07-19 22:31:36 +0200274 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({
zeertzjq3fc84dc2022-12-07 09:17:59 +0000396 \ 'all': {'leftcol': 1, 'topline': 0, 'topfill': 0, 'width': 0, 'height': 0, 'skipcol': 0},
397 \ '1000': {'leftcol': -1, 'topline': 0, 'topfill': 0, 'width': 0, 'height': 0, 'skipcol': 0}
Bram Moolenaar35fc61c2022-11-22 12:40:50 +0000398 \ }, 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({
zeertzjq3fc84dc2022-12-07 09:17:59 +0000406 \ 'all': {'leftcol': 0, 'topline': 1, 'topfill': 0, 'width': 0, 'height': 0, 'skipcol': 0},
407 \ '1000': {'leftcol': 0, 'topline': -1, 'topfill': 0, 'width': 0, 'height': 0, 'skipcol': 0}
Bram Moolenaar35fc61c2022-11-22 12:40:50 +0000408 \ }, 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({
zeertzjq3fc84dc2022-12-07 09:17:59 +0000417 \ 'all': {'leftcol': 0, 'topline': 1, 'topfill': 0, 'width': 0, 'height': 0, 'skipcol': 0},
418 \ '1000': {'leftcol': 0, 'topline': -1, 'topfill': 0, 'width': 0, 'height': 0, 'skipcol': 0}
Bram Moolenaar35fc61c2022-11-22 12:40:50 +0000419 \ }, 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({
zeertzjq3fc84dc2022-12-07 09:17:59 +0000430 \ 'all': {'leftcol': 5, 'topline': 0, 'topfill': 0, 'width': 0, 'height': 0, 'skipcol': 0},
431 \ '1000': {'leftcol': -5, 'topline': 0, 'topfill': 0, 'width': 0, 'height': 0, 'skipcol': 0}
Bram Moolenaar35fc61c2022-11-22 12:40:50 +0000432 \ }, 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
zeertzjq3fc84dc2022-12-07 09:17:59 +0000585func Test_WinScrolled_diff()
586 CheckRunVimInTerminal
587
588 let lines =<< trim END
589 set diffopt+=foldcolumn:0
590 call setline(1, ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i'])
591 vnew
592 call setline(1, ['d', 'e', 'f', 'g', 'h', 'i'])
593 windo diffthis
594 func WriteScrollEvent()
595 call writefile([json_encode(v:event)], 'XscrollEvent')
596 endfunc
597 au WinScrolled * call WriteScrollEvent()
598 END
599 call writefile(lines, 'Xtest_winscrolled_diff', 'D')
600 let buf = RunVimInTerminal('-S Xtest_winscrolled_diff', {'rows': 8})
601
602 call term_sendkeys(buf, "\<C-E>")
603 call WaitForAssert({-> assert_match('^d', term_getline(buf, 3))}, 1000)
604
605 let event = readfile('XscrollEvent')[0]->json_decode()
606 call assert_equal({
607 \ 'all': {'leftcol': 0, 'topline': 1, 'topfill': 1, 'width': 0, 'height': 0, 'skipcol': 0},
608 \ '1000': {'leftcol': 0, 'topline': 1, 'topfill': 0, 'width': 0, 'height': 0, 'skipcol': 0},
609 \ '1001': {'leftcol': 0, 'topline': 0, 'topfill': -1, 'width': 0, 'height': 0, 'skipcol': 0}
610 \ }, event)
611
612 call term_sendkeys(buf, "2\<C-E>")
613 call WaitForAssert({-> assert_match('^f', term_getline(buf, 3))}, 1000)
614
615 let event = readfile('XscrollEvent')[0]->json_decode()
616 call assert_equal({
617 \ 'all': {'leftcol': 0, 'topline': 2, 'topfill': 2, 'width': 0, 'height': 0, 'skipcol': 0},
618 \ '1000': {'leftcol': 0, 'topline': 2, 'topfill': 0, 'width': 0, 'height': 0, 'skipcol': 0},
619 \ '1001': {'leftcol': 0, 'topline': 0, 'topfill': -2, 'width': 0, 'height': 0, 'skipcol': 0}
620 \ }, event)
621
622 call term_sendkeys(buf, "\<C-E>")
623 call WaitForAssert({-> assert_match('^g', term_getline(buf, 3))}, 1000)
624
625 let event = readfile('XscrollEvent')[0]->json_decode()
626 call assert_equal({
627 \ 'all': {'leftcol': 0, 'topline': 2, 'topfill': 0, 'width': 0, 'height': 0, 'skipcol': 0},
628 \ '1000': {'leftcol': 0, 'topline': 1, 'topfill': 0, 'width': 0, 'height': 0, 'skipcol': 0},
629 \ '1001': {'leftcol': 0, 'topline': 1, 'topfill': 0, 'width': 0, 'height': 0, 'skipcol': 0}
630 \ }, event)
631
632 call term_sendkeys(buf, "2\<C-Y>")
633 call WaitForAssert({-> assert_match('^e', term_getline(buf, 3))}, 1000)
634
635 let event = readfile('XscrollEvent')[0]->json_decode()
636 call assert_equal({
637 \ 'all': {'leftcol': 0, 'topline': 3, 'topfill': 1, 'width': 0, 'height': 0, 'skipcol': 0},
638 \ '1000': {'leftcol': 0, 'topline': -2, 'topfill': 0, 'width': 0, 'height': 0, 'skipcol': 0},
639 \ '1001': {'leftcol': 0, 'topline': -1, 'topfill': 1, 'width': 0, 'height': 0, 'skipcol': 0}
640 \ }, event)
641
642 call StopVimInTerminal(buf)
Dominique Pelle541c87c2023-01-17 21:20:44 +0000643 call delete('XscrollEvent')
zeertzjq3fc84dc2022-12-07 09:17:59 +0000644endfunc
645
naohiro ono23beefe2021-11-13 12:38:49 +0000646func Test_WinClosed()
647 " Test that the pattern is matched against the closed window's ID, and both
648 " <amatch> and <afile> are set to it.
649 new
650 let winid = win_getid()
651 let g:matched = v:false
652 augroup test-WinClosed
653 autocmd!
654 execute 'autocmd WinClosed' winid 'let g:matched = v:true'
655 autocmd WinClosed * let g:amatch = str2nr(expand('<amatch>'))
656 autocmd WinClosed * let g:afile = str2nr(expand('<afile>'))
657 augroup END
658 close
659 call assert_true(g:matched)
660 call assert_equal(winid, g:amatch)
661 call assert_equal(winid, g:afile)
662
663 " Test that WinClosed is non-recursive.
664 new
665 new
666 call assert_equal(3, winnr('$'))
667 let g:triggered = 0
668 augroup test-WinClosed
669 autocmd!
670 autocmd WinClosed * let g:triggered += 1
671 autocmd WinClosed * 2 wincmd c
672 augroup END
673 close
674 call assert_equal(1, winnr('$'))
675 call assert_equal(1, g:triggered)
676
677 autocmd! test-WinClosed
678 augroup! test-WinClosed
679 unlet g:matched
680 unlet g:amatch
681 unlet g:afile
682 unlet g:triggered
683endfunc
684
Bram Moolenaarc947b9a2022-04-06 17:59:21 +0100685func Test_WinClosed_throws()
686 vnew
687 let bnr = bufnr()
688 call assert_equal(1, bufloaded(bnr))
689 augroup test-WinClosed
690 autocmd WinClosed * throw 'foo'
691 augroup END
692 try
693 close
694 catch /.*/
695 endtry
696 call assert_equal(0, bufloaded(bnr))
697
698 autocmd! test-WinClosed
699 augroup! test-WinClosed
700endfunc
701
zeertzjq6a069402022-04-07 14:08:29 +0100702func Test_WinClosed_throws_with_tabs()
703 tabnew
704 let bnr = bufnr()
705 call assert_equal(1, bufloaded(bnr))
706 augroup test-WinClosed
707 autocmd WinClosed * throw 'foo'
708 augroup END
709 try
710 close
711 catch /.*/
712 endtry
713 call assert_equal(0, bufloaded(bnr))
714
715 autocmd! test-WinClosed
716 augroup! test-WinClosed
717endfunc
718
zeertzjq62de54b2022-09-22 18:08:37 +0100719" This used to trigger WinClosed twice for the same window, and the window's
720" buffer was NULL in the second autocommand.
721func Test_WinClosed_switch_tab()
722 edit Xa
723 split Xb
724 split Xc
725 tab split
726 new
727 augroup test-WinClosed
728 autocmd WinClosed * tabprev | bwipe!
729 augroup END
730 close
731 " Check that the tabline has been fully removed
732 call assert_equal([1, 1], win_screenpos(0))
733
734 autocmd! test-WinClosed
735 augroup! test-WinClosed
736 %bwipe!
737endfunc
738
Bram Moolenaare99e8442016-07-26 20:43:40 +0200739func s:AddAnAutocmd()
740 augroup vimBarTest
741 au BufReadCmd * echo 'hello'
742 augroup END
743 call assert_equal(3, len(split(execute('au vimBarTest'), "\n")))
744endfunc
745
746func Test_early_bar()
747 " test that a bar is recognized before the {event}
748 call s:AddAnAutocmd()
Bram Moolenaarb8e642f2021-11-20 10:38:25 +0000749 augroup vimBarTest | au! | let done = 77 | augroup END
Bram Moolenaare99e8442016-07-26 20:43:40 +0200750 call assert_equal(1, len(split(execute('au vimBarTest'), "\n")))
Bram Moolenaarb8e642f2021-11-20 10:38:25 +0000751 call assert_equal(77, done)
Bram Moolenaare99e8442016-07-26 20:43:40 +0200752
753 call s:AddAnAutocmd()
Bram Moolenaarb8e642f2021-11-20 10:38:25 +0000754 augroup vimBarTest| au!| let done = 88 | augroup END
Bram Moolenaare99e8442016-07-26 20:43:40 +0200755 call assert_equal(1, len(split(execute('au vimBarTest'), "\n")))
Bram Moolenaarb8e642f2021-11-20 10:38:25 +0000756 call assert_equal(88, done)
Bram Moolenaare99e8442016-07-26 20:43:40 +0200757
758 " test that a bar is recognized after the {event}
759 call s:AddAnAutocmd()
Bram Moolenaarb8e642f2021-11-20 10:38:25 +0000760 augroup vimBarTest| au!BufReadCmd| let done = 99 | augroup END
Bram Moolenaare99e8442016-07-26 20:43:40 +0200761 call assert_equal(1, len(split(execute('au vimBarTest'), "\n")))
Bram Moolenaarb8e642f2021-11-20 10:38:25 +0000762 call assert_equal(99, done)
Bram Moolenaare99e8442016-07-26 20:43:40 +0200763
764 " test that a bar is recognized after the {group}
765 call s:AddAnAutocmd()
766 au! vimBarTest|echo 'hello'
767 call assert_equal(1, len(split(execute('au vimBarTest'), "\n")))
768endfunc
Bram Moolenaarf2c4c392016-07-29 20:50:24 +0200769
Bram Moolenaar5c809082016-09-01 16:21:48 +0200770func RemoveGroup()
771 autocmd! StartOK
772 augroup! StartOK
773endfunc
774
Bram Moolenaarf2c4c392016-07-29 20:50:24 +0200775func Test_augroup_warning()
776 augroup TheWarning
777 au VimEnter * echo 'entering'
778 augroup END
Bram Moolenaar5dc4e2f2020-11-25 14:15:12 +0100779 call assert_match("TheWarning.*VimEnter", execute('au VimEnter'))
Bram Moolenaarf2c4c392016-07-29 20:50:24 +0200780 redir => res
781 augroup! TheWarning
782 redir END
Bram Moolenaar5dc4e2f2020-11-25 14:15:12 +0100783 call assert_match("W19:", res)
784 call assert_match("-Deleted-.*VimEnter", execute('au VimEnter'))
Bram Moolenaarf2c4c392016-07-29 20:50:24 +0200785
786 " check "Another" does not take the pace of the deleted entry
787 augroup Another
788 augroup END
Bram Moolenaar5dc4e2f2020-11-25 14:15:12 +0100789 call assert_match("-Deleted-.*VimEnter", execute('au VimEnter'))
Bram Moolenaaraeac9002016-09-06 22:15:08 +0200790 augroup! Another
Bram Moolenaar5c809082016-09-01 16:21:48 +0200791
792 " no warning for postpone aucmd delete
793 augroup StartOK
794 au VimEnter * call RemoveGroup()
795 augroup END
Bram Moolenaar5dc4e2f2020-11-25 14:15:12 +0100796 call assert_match("StartOK.*VimEnter", execute('au VimEnter'))
Bram Moolenaar5c809082016-09-01 16:21:48 +0200797 redir => res
798 doautocmd VimEnter
799 redir END
Bram Moolenaar5dc4e2f2020-11-25 14:15:12 +0100800 call assert_notmatch("W19:", res)
Bram Moolenaarde653f02016-09-03 16:59:06 +0200801 au! VimEnter
Bram Moolenaarad48e6c2020-04-21 22:19:45 +0200802
803 call assert_fails('augroup!', 'E471:')
Bram Moolenaarf2c4c392016-07-29 20:50:24 +0200804endfunc
Bram Moolenaarb62cc362016-09-03 16:43:53 +0200805
Bram Moolenaar8d84ff12017-10-26 16:42:16 +0200806func Test_BufReadCmdHelp()
807 " This used to cause access to free memory
808 au BufReadCmd * e +h
809 help
810
Bram Moolenaar8d84ff12017-10-26 16:42:16 +0200811 au! BufReadCmd
812endfunc
813
814func Test_BufReadCmdHelpJump()
815 " This used to cause access to free memory
816 au BufReadCmd * e +h{
Bram Moolenaarcf1ba352017-10-27 00:55:04 +0200817 " } to fix highlighting
818 call assert_fails('help', 'E434:')
Bram Moolenaar8d84ff12017-10-26 16:42:16 +0200819
Bram Moolenaar8d84ff12017-10-26 16:42:16 +0200820 au! BufReadCmd
821endfunc
822
zeertzjq93f72cc2022-08-26 15:34:52 +0100823" BufReadCmd is triggered for a "nofile" buffer. Check all values.
Bram Moolenaarb1d2c812022-08-26 11:55:01 +0100824func Test_BufReadCmdNofile()
zeertzjq93f72cc2022-08-26 15:34:52 +0100825 for val in ['nofile',
826 \ 'nowrite',
827 \ 'acwrite',
828 \ 'quickfix',
829 \ 'help',
830 \ 'terminal',
831 \ 'prompt',
832 \ 'popup',
833 \ ]
834 new somefile
835 exe 'set buftype=' .. val
836 au BufReadCmd somefile call setline(1, 'triggered')
837 edit
838 call assert_equal('triggered', getline(1))
Bram Moolenaarb1d2c812022-08-26 11:55:01 +0100839
zeertzjq93f72cc2022-08-26 15:34:52 +0100840 au! BufReadCmd
841 bwipe!
842 endfor
Bram Moolenaarb1d2c812022-08-26 11:55:01 +0100843endfunc
844
Bram Moolenaarb62cc362016-09-03 16:43:53 +0200845func Test_augroup_deleted()
Bram Moolenaarde653f02016-09-03 16:59:06 +0200846 " This caused a crash before E936 was introduced
Bram Moolenaarb62cc362016-09-03 16:43:53 +0200847 augroup x
Bram Moolenaarde653f02016-09-03 16:59:06 +0200848 call assert_fails('augroup! x', 'E936:')
849 au VimEnter * echo
850 augroup end
Bram Moolenaarb62cc362016-09-03 16:43:53 +0200851 augroup! x
Bram Moolenaar5dc4e2f2020-11-25 14:15:12 +0100852 call assert_match("-Deleted-.*VimEnter", execute('au VimEnter'))
Bram Moolenaarde653f02016-09-03 16:59:06 +0200853 au! VimEnter
Bram Moolenaarb62cc362016-09-03 16:43:53 +0200854endfunc
855
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200856" Tests for autocommands on :close command.
857" This used to be in test13.
858func Test_three_windows()
Bram Moolenaarb3435b02016-09-29 20:54:59 +0200859 " Clean up buffers, because in some cases this function fails.
860 call s:cleanup_buffers()
861
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200862 " Write three files and open them, each in a window.
863 " Then go to next window, with autocommand that deletes the previous one.
864 " Do this twice, writing the file.
865 e! Xtestje1
866 call setline(1, 'testje1')
867 w
868 sp Xtestje2
869 call setline(1, 'testje2')
870 w
871 sp Xtestje3
872 call setline(1, 'testje3')
873 w
874 wincmd w
875 au WinLeave Xtestje2 bwipe
876 wincmd w
877 call assert_equal('Xtestje1', expand('%'))
878
879 au WinLeave Xtestje1 bwipe Xtestje3
880 close
881 call assert_equal('Xtestje1', expand('%'))
882
883 " Test deleting the buffer on a Unload event. If this goes wrong there
884 " will be the ATTENTION prompt.
885 e Xtestje1
886 au!
887 au! BufUnload Xtestje1 bwipe
888 call assert_fails('e Xtestje3', 'E937:')
889 call assert_equal('Xtestje3', expand('%'))
890
891 e Xtestje2
892 sp Xtestje1
893 call assert_fails('e', 'E937:')
Bram Moolenaara997b452018-04-17 23:24:06 +0200894 call assert_equal('Xtestje1', expand('%'))
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200895
896 " Test changing buffers in a BufWipeout autocommand. If this goes wrong
897 " there are ml_line errors and/or a Crash.
898 au!
899 only
900 e Xanother
901 e Xtestje1
902 bwipe Xtestje2
903 bwipe Xtestje3
904 au BufWipeout Xtestje1 buf Xtestje1
905 bwipe
906 call assert_equal('Xanother', expand('%'))
907
908 only
909 help
910 wincmd w
911 1quit
912 call assert_equal('Xanother', expand('%'))
913
914 au!
Bram Moolenaar4520d442017-03-19 16:09:46 +0100915 enew
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200916 call delete('Xtestje1')
917 call delete('Xtestje2')
918 call delete('Xtestje3')
919endfunc
Bram Moolenaare13b9af2017-01-13 22:01:02 +0100920
921func Test_BufEnter()
922 au! BufEnter
923 au Bufenter * let val = val . '+'
924 let g:val = ''
925 split NewFile
926 call assert_equal('+', g:val)
927 bwipe!
928 call assert_equal('++', g:val)
929
930 " Also get BufEnter when editing a directory
Bram Moolenaar6f14da12022-09-07 21:30:44 +0100931 call mkdir('Xbufenterdir', 'D')
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +0100932 split Xbufenterdir
Bram Moolenaare13b9af2017-01-13 22:01:02 +0100933 call assert_equal('+++', g:val)
Bram Moolenaare94260f2017-03-21 15:50:12 +0100934
935 " On MS-Windows we can't edit the directory, make sure we wipe the right
936 " buffer.
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +0100937 bwipe! Xbufenterdir
Bram Moolenaare13b9af2017-01-13 22:01:02 +0100938 au! BufEnter
Bram Moolenaara9b5b852022-08-26 13:16:20 +0100939
940 " Editing a "nofile" buffer doesn't read the file but does trigger BufEnter
zeertzjq93f72cc2022-08-26 15:34:52 +0100941 " for historic reasons. Also test other 'buftype' values.
942 for val in ['nofile',
943 \ 'nowrite',
944 \ 'acwrite',
945 \ 'quickfix',
946 \ 'help',
947 \ 'terminal',
948 \ 'prompt',
949 \ 'popup',
950 \ ]
951 new somefile
952 exe 'set buftype=' .. val
953 au BufEnter somefile call setline(1, 'some text')
954 edit
955 call assert_equal('some text', getline(1))
956 bwipe!
957 au! BufEnter
958 endfor
Bram Moolenaar9fda8152022-11-19 13:14:10 +0000959
960 new
961 new
962 autocmd BufEnter * ++once close
963 call assert_fails('close', 'E1312:')
964
965 au! BufEnter
966 only
Bram Moolenaare13b9af2017-01-13 22:01:02 +0100967endfunc
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100968
969" Closing a window might cause an endless loop
970" E814 for older Vims
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200971func Test_autocmd_bufwipe_in_SessLoadPost()
Bram Moolenaar1d68d9b2017-10-13 22:33:32 +0200972 edit Xtest
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100973 tabnew
Bram Moolenaar1d68d9b2017-10-13 22:33:32 +0200974 file Xsomething
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100975 set noswapfile
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100976 mksession!
977
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200978 let content =<< trim [CODE]
Bram Moolenaar62cd26a2020-10-11 20:08:44 +0200979 call test_override('ui_delay', 10)
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200980 set nocp noswapfile
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100981 let v:swapchoice = "e"
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200982 augroup test_autocmd_sessionload
983 autocmd!
984 autocmd SessionLoadPost * exe bufnr("Xsomething") . "bw!"
985 augroup END
986
987 func WriteErrors()
Bram Moolenaarbe9fc5b2022-09-09 17:09:35 +0100988 call writefile([execute("messages")], "XerrorsBwipe")
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('XerrorsBwipe'))
Bram Moolenaare2e40752020-09-04 21:18:46 +0200997 call assert_match('E814:', errors)
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100998
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100999 set swapfile
Bram Moolenaarbe9fc5b2022-09-09 17:09:35 +01001000 for file in ['Session.vim', 'XerrorsBwipe']
Bram Moolenaar8c752bd2017-03-19 17:09:56 +01001001 call delete(file)
1002 endfor
1003endfunc
1004
Bram Moolenaar797e63b2021-01-15 16:22:52 +01001005" Using :blast and :ball for many events caused a crash, because b_nwindows was
1006" not incremented correctly.
1007func Test_autocmd_blast_badd()
1008 let content =<< trim [CODE]
1009 au BufNew,BufAdd,BufWinEnter,BufEnter,BufLeave,BufWinLeave,BufUnload,VimEnter foo* blast
1010 edit foo1
1011 au BufNew,BufAdd,BufWinEnter,BufEnter,BufLeave,BufWinLeave,BufUnload,VimEnter foo* ball
1012 edit foo2
Bram Moolenaarbe9fc5b2022-09-09 17:09:35 +01001013 call writefile(['OK'], 'XerrorsBlast')
Bram Moolenaar797e63b2021-01-15 16:22:52 +01001014 qall
1015 [CODE]
1016
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01001017 call writefile(content, 'XblastBall', 'D')
Bram Moolenaar797e63b2021-01-15 16:22:52 +01001018 call system(GetVimCommand() .. ' --clean -S XblastBall')
Bram Moolenaarae04a602022-09-09 15:08:10 +01001019 sleep 100m
Bram Moolenaarbe9fc5b2022-09-09 17:09:35 +01001020 call assert_match('OK', readfile('XerrorsBlast')->join())
Bram Moolenaar797e63b2021-01-15 16:22:52 +01001021
Bram Moolenaarbe9fc5b2022-09-09 17:09:35 +01001022 call delete('XerrorsBlast')
Bram Moolenaar797e63b2021-01-15 16:22:52 +01001023endfunc
1024
Bram Moolenaar8c752bd2017-03-19 17:09:56 +01001025" SEGV occurs in older versions.
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001026func Test_autocmd_bufwipe_in_SessLoadPost2()
Bram Moolenaar8c752bd2017-03-19 17:09:56 +01001027 tabnew
1028 set noswapfile
Bram Moolenaar8c752bd2017-03-19 17:09:56 +01001029 mksession!
1030
Bram Moolenaarc79745a2019-05-20 22:12:34 +02001031 let content =<< trim [CODE]
1032 set nocp noswapfile
1033 function! DeleteInactiveBufs()
1034 tabfirst
1035 let tabblist = []
1036 for i in range(1, tabpagenr(''$''))
1037 call extend(tabblist, tabpagebuflist(i))
1038 endfor
1039 for b in range(1, bufnr(''$''))
1040 if bufexists(b) && buflisted(b) && (index(tabblist, b) == -1 || bufname(b) =~# ''^$'')
1041 exec ''bwipeout '' . b
1042 endif
1043 endfor
1044 echomsg "SessionLoadPost DONE"
1045 endfunction
1046 au SessionLoadPost * call DeleteInactiveBufs()
1047
1048 func WriteErrors()
Bram Moolenaarbe9fc5b2022-09-09 17:09:35 +01001049 call writefile([execute("messages")], "XerrorsPost")
Bram Moolenaarc79745a2019-05-20 22:12:34 +02001050 endfunc
1051 au VimLeave * call WriteErrors()
1052 [CODE]
1053
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01001054 call writefile(content, 'Xvimrc', 'D')
Bram Moolenaar93344c22019-08-14 21:12:05 +02001055 call system(GetVimCommand('Xvimrc') .. ' --not-a-term --noplugins -S Session.vim -c cq')
Bram Moolenaarae04a602022-09-09 15:08:10 +01001056 sleep 100m
Bram Moolenaarbe9fc5b2022-09-09 17:09:35 +01001057 let errors = join(readfile('XerrorsPost'))
Bram Moolenaare94260f2017-03-21 15:50:12 +01001058 " This probably only ever matches on unix.
1059 call assert_notmatch('Caught deadly signal SEGV', errors)
1060 call assert_match('SessionLoadPost DONE', errors)
Bram Moolenaar8c752bd2017-03-19 17:09:56 +01001061
Bram Moolenaar8c752bd2017-03-19 17:09:56 +01001062 set swapfile
Bram Moolenaarbe9fc5b2022-09-09 17:09:35 +01001063 for file in ['Session.vim', 'XerrorsPost']
Bram Moolenaar8c752bd2017-03-19 17:09:56 +01001064 call delete(file)
1065 endfor
1066endfunc
Bram Moolenaarfaf29d72017-07-09 11:07:16 +02001067
1068func Test_empty_doau()
1069 doau \|
1070endfunc
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001071
1072func s:AutoCommandOptionSet(match)
Bram Moolenaard7c96872019-06-15 17:12:48 +02001073 let template = "Option: <%s>, OldVal: <%s>, OldValLocal: <%s>, OldValGlobal: <%s>, NewVal: <%s>, Scope: <%s>, Command: <%s>\n"
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001074 let item = remove(g:options, 0)
Bram Moolenaard7c96872019-06-15 17:12:48 +02001075 let expected = printf(template, item[0], item[1], item[2], item[3], item[4], item[5], item[6])
1076 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 +02001077 let g:opt = [expected, actual]
1078 "call assert_equal(expected, actual)
1079endfunc
1080
1081func Test_OptionSet()
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +02001082 CheckOption autochdir
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001083
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +02001084 badd test_autocmd.vim
1085
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001086 call test_override('starting', 1)
1087 set nocp
1088 au OptionSet * :call s:AutoCommandOptionSet(expand("<amatch>"))
1089
1090 " 1: Setting number option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001091 let g:options = [['number', 0, 0, 0, 1, 'global', 'set']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001092 set nu
1093 call assert_equal([], g:options)
1094 call assert_equal(g:opt[0], g:opt[1])
1095
1096 " 2: Setting local number option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001097 let g:options = [['number', 1, 1, '', 0, 'local', 'setlocal']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001098 setlocal nonu
1099 call assert_equal([], g:options)
1100 call assert_equal(g:opt[0], g:opt[1])
1101
1102 " 3: Setting global number option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001103 let g:options = [['number', 1, '', 1, 0, 'global', 'setglobal']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001104 setglobal nonu
1105 call assert_equal([], g:options)
1106 call assert_equal(g:opt[0], g:opt[1])
1107
1108 " 4: Setting local autoindent option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001109 let g:options = [['autoindent', 0, 0, '', 1, 'local', 'setlocal']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001110 setlocal ai
1111 call assert_equal([], g:options)
1112 call assert_equal(g:opt[0], g:opt[1])
1113
1114 " 5: Setting global autoindent option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001115 let g:options = [['autoindent', 0, '', 0, 1, 'global', 'setglobal']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001116 setglobal ai
1117 call assert_equal([], g:options)
1118 call assert_equal(g:opt[0], g:opt[1])
1119
1120 " 6: Setting global autoindent option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001121 let g:options = [['autoindent', 1, 1, 1, 0, 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001122 set ai!
1123 call assert_equal([], g:options)
1124 call assert_equal(g:opt[0], g:opt[1])
1125
1126 " 6a: Setting global autoindent option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001127 let g:options = [['autoindent', 1, 1, 0, 0, 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001128 noa setlocal ai
1129 noa setglobal noai
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001130 set ai!
1131 call assert_equal([], g:options)
1132 call assert_equal(g:opt[0], g:opt[1])
1133
1134 " Should not print anything, use :noa
1135 " 7: don't trigger OptionSet"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001136 let g:options = [['invalid', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001137 noa set nonu
Bram Moolenaard7c96872019-06-15 17:12:48 +02001138 call assert_equal([['invalid', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid']], g:options)
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001139 call assert_equal(g:opt[0], g:opt[1])
1140
1141 " 8: Setting several global list and number option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001142 let g:options = [['list', 0, 0, 0, 1, 'global', 'set'], ['number', 0, 0, 0, 1, 'global', 'set']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001143 set list nu
1144 call assert_equal([], g:options)
1145 call assert_equal(g:opt[0], g:opt[1])
1146
1147 " 9: don't trigger OptionSet"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001148 let g:options = [['invalid', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid'], ['invalid', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001149 noa set nolist nonu
Bram Moolenaard7c96872019-06-15 17:12:48 +02001150 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 +02001151 call assert_equal(g:opt[0], g:opt[1])
1152
1153 " 10: Setting global acd"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001154 let g:options = [['autochdir', 0, 0, '', 1, 'local', 'setlocal']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001155 setlocal acd
1156 call assert_equal([], g:options)
1157 call assert_equal(g:opt[0], g:opt[1])
1158
1159 " 11: Setting global autoread (also sets local value)"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001160 let g:options = [['autoread', 0, 0, 0, 1, 'global', 'set']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001161 set ar
1162 call assert_equal([], g:options)
1163 call assert_equal(g:opt[0], g:opt[1])
1164
1165 " 12: Setting local autoread"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001166 let g:options = [['autoread', 1, 1, '', 1, 'local', 'setlocal']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001167 setlocal ar
1168 call assert_equal([], g:options)
1169 call assert_equal(g:opt[0], g:opt[1])
1170
1171 " 13: Setting global autoread"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001172 let g:options = [['autoread', 1, '', 1, 0, 'global', 'setglobal']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001173 setglobal invar
1174 call assert_equal([], g:options)
1175 call assert_equal(g:opt[0], g:opt[1])
1176
1177 " 14: Setting option backspace through :let"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001178 let g:options = [['backspace', '', '', '', 'eol,indent,start', 'global', 'set']]
1179 let &bs = "eol,indent,start"
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001180 call assert_equal([], g:options)
1181 call assert_equal(g:opt[0], g:opt[1])
1182
1183 " 15: Setting option backspace through setbufvar()"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001184 let g:options = [['backup', 0, 0, '', 1, 'local', 'setlocal']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001185 " try twice, first time, shouldn't trigger because option name is invalid,
1186 " second time, it should trigger
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +02001187 let bnum = bufnr('%')
Bram Moolenaare2e40752020-09-04 21:18:46 +02001188 call assert_fails("call setbufvar(bnum, '&l:bk', 1)", 'E355:')
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001189 " should trigger, use correct option name
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +02001190 call setbufvar(bnum, '&backup', 1)
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001191 call assert_equal([], g:options)
1192 call assert_equal(g:opt[0], g:opt[1])
1193
1194 " 16: Setting number option using setwinvar"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001195 let g:options = [['number', 0, 0, '', 1, 'local', 'setlocal']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001196 call setwinvar(0, '&number', 1)
1197 call assert_equal([], g:options)
1198 call assert_equal(g:opt[0], g:opt[1])
1199
1200 " 17: Setting key option, shouldn't trigger"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001201 let g:options = [['key', 'invalid', 'invalid1', 'invalid2', 'invalid3', 'invalid4', 'invalid5']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001202 setlocal key=blah
1203 setlocal key=
Bram Moolenaard7c96872019-06-15 17:12:48 +02001204 call assert_equal([['key', 'invalid', 'invalid1', 'invalid2', 'invalid3', 'invalid4', 'invalid5']], g:options)
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001205 call assert_equal(g:opt[0], g:opt[1])
1206
Bram Moolenaard7c96872019-06-15 17:12:48 +02001207
1208 " 18a: Setting string global option"
1209 let oldval = &backupext
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001210 let g:options = [['backupext', oldval, oldval, oldval, 'foo', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001211 set backupext=foo
1212 call assert_equal([], g:options)
1213 call assert_equal(g:opt[0], g:opt[1])
1214
1215 " 18b: Resetting string global option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001216 let g:options = [['backupext', 'foo', 'foo', 'foo', oldval, 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001217 set backupext&
1218 call assert_equal([], g:options)
1219 call assert_equal(g:opt[0], g:opt[1])
1220
1221 " 18c: Setting global string global option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001222 let g:options = [['backupext', oldval, '', oldval, 'bar', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001223 setglobal backupext=bar
1224 call assert_equal([], g:options)
1225 call assert_equal(g:opt[0], g:opt[1])
1226
1227 " 18d: Setting local string global option"
1228 " As this is a global option this sets the global value even though
1229 " :setlocal is used!
1230 noa set backupext& " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001231 let g:options = [['backupext', oldval, oldval, '', 'baz', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001232 setlocal backupext=baz
1233 call assert_equal([], g:options)
1234 call assert_equal(g:opt[0], g:opt[1])
1235
1236 " 18e: Setting again string global option"
1237 noa setglobal backupext=ext_global " Reset global and local value (without triggering autocmd)
1238 noa setlocal backupext=ext_local " Sets the global(!) value!
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001239 let g:options = [['backupext', 'ext_local', 'ext_local', 'ext_local', 'fuu', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001240 set backupext=fuu
1241 call assert_equal([], g:options)
1242 call assert_equal(g:opt[0], g:opt[1])
1243
1244
zeertzjqb811de52021-10-21 10:50:44 +01001245 " 19a: Setting string global-local (to buffer) option"
Bram Moolenaar8efa0262017-08-20 15:47:20 +02001246 let oldval = &tags
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001247 let g:options = [['tags', oldval, oldval, oldval, 'tagpath', 'global', 'set']]
Bram Moolenaar8efa0262017-08-20 15:47:20 +02001248 set tags=tagpath
1249 call assert_equal([], g:options)
1250 call assert_equal(g:opt[0], g:opt[1])
1251
zeertzjqb811de52021-10-21 10:50:44 +01001252 " 19b: Resetting string global-local (to buffer) option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001253 let g:options = [['tags', 'tagpath', 'tagpath', 'tagpath', oldval, 'global', 'set']]
Bram Moolenaar8efa0262017-08-20 15:47:20 +02001254 set tags&
1255 call assert_equal([], g:options)
1256 call assert_equal(g:opt[0], g:opt[1])
1257
zeertzjqb811de52021-10-21 10:50:44 +01001258 " 19c: Setting global string global-local (to buffer) option "
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001259 let g:options = [['tags', oldval, '', oldval, 'tagpath1', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001260 setglobal tags=tagpath1
1261 call assert_equal([], g:options)
1262 call assert_equal(g:opt[0], g:opt[1])
1263
zeertzjqb811de52021-10-21 10:50:44 +01001264 " 19d: Setting local string global-local (to buffer) option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001265 let g:options = [['tags', 'tagpath1', 'tagpath1', '', 'tagpath2', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001266 setlocal tags=tagpath2
1267 call assert_equal([], g:options)
1268 call assert_equal(g:opt[0], g:opt[1])
1269
zeertzjqb811de52021-10-21 10:50:44 +01001270 " 19e: Setting again string global-local (to buffer) option"
1271 " Note: v:option_old is the old global value for global-local string options
Bram Moolenaard7c96872019-06-15 17:12:48 +02001272 " but the old local value for all other kinds of options.
1273 noa setglobal tags=tag_global " Reset global and local value (without triggering autocmd)
1274 noa setlocal tags=tag_local
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001275 let g:options = [['tags', 'tag_global', 'tag_local', 'tag_global', 'tagpath', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001276 set tags=tagpath
1277 call assert_equal([], g:options)
1278 call assert_equal(g:opt[0], g:opt[1])
1279
zeertzjqb811de52021-10-21 10:50:44 +01001280 " 19f: Setting string global-local (to buffer) option to an empty string"
1281 " Note: v:option_old is the old global value for global-local string options
Bram Moolenaard7c96872019-06-15 17:12:48 +02001282 " but the old local value for all other kinds of options.
1283 noa set tags=tag_global " Reset global and local value (without triggering autocmd)
1284 noa setlocal tags= " empty string
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001285 let g:options = [['tags', 'tag_global', '', 'tag_global', 'tagpath', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001286 set tags=tagpath
1287 call assert_equal([], g:options)
1288 call assert_equal(g:opt[0], g:opt[1])
1289
1290
1291 " 20a: Setting string local (to buffer) option"
1292 let oldval = &spelllang
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001293 let g:options = [['spelllang', oldval, oldval, oldval, 'elvish,klingon', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001294 set spelllang=elvish,klingon
1295 call assert_equal([], g:options)
1296 call assert_equal(g:opt[0], g:opt[1])
1297
1298 " 20b: Resetting string local (to buffer) option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001299 let g:options = [['spelllang', 'elvish,klingon', 'elvish,klingon', 'elvish,klingon', oldval, 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001300 set spelllang&
1301 call assert_equal([], g:options)
1302 call assert_equal(g:opt[0], g:opt[1])
1303
1304 " 20c: Setting global string local (to buffer) option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001305 let g:options = [['spelllang', oldval, '', oldval, 'elvish', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001306 setglobal spelllang=elvish
1307 call assert_equal([], g:options)
1308 call assert_equal(g:opt[0], g:opt[1])
1309
1310 " 20d: Setting local string local (to buffer) option"
1311 noa set spelllang& " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001312 let g:options = [['spelllang', oldval, oldval, '', 'klingon', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001313 setlocal spelllang=klingon
1314 call assert_equal([], g:options)
1315 call assert_equal(g:opt[0], g:opt[1])
1316
1317 " 20e: Setting again string local (to buffer) option"
zeertzjqb811de52021-10-21 10:50:44 +01001318 " Note: v:option_old is the old global value for global-local string options
Bram Moolenaard7c96872019-06-15 17:12:48 +02001319 " but the old local value for all other kinds of options.
1320 noa setglobal spelllang=spellglobal " Reset global and local value (without triggering autocmd)
1321 noa setlocal spelllang=spelllocal
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001322 let g:options = [['spelllang', 'spelllocal', 'spelllocal', 'spellglobal', 'foo', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001323 set spelllang=foo
1324 call assert_equal([], g:options)
1325 call assert_equal(g:opt[0], g:opt[1])
1326
1327
zeertzjqb811de52021-10-21 10:50:44 +01001328 " 21a: Setting string global-local (to window) option"
Bram Moolenaard7c96872019-06-15 17:12:48 +02001329 let oldval = &statusline
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001330 let g:options = [['statusline', oldval, oldval, oldval, 'foo', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001331 set statusline=foo
1332 call assert_equal([], g:options)
1333 call assert_equal(g:opt[0], g:opt[1])
1334
zeertzjqb811de52021-10-21 10:50:44 +01001335 " 21b: Resetting string global-local (to window) option"
1336 " Note: v:option_old is the old global value for global-local string options
Bram Moolenaard7c96872019-06-15 17:12:48 +02001337 " but the old local value for all other kinds of options.
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001338 let g:options = [['statusline', 'foo', 'foo', 'foo', oldval, 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001339 set statusline&
1340 call assert_equal([], g:options)
1341 call assert_equal(g:opt[0], g:opt[1])
1342
zeertzjqb811de52021-10-21 10:50:44 +01001343 " 21c: Setting global string global-local (to window) option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001344 let g:options = [['statusline', oldval, '', oldval, 'bar', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001345 setglobal statusline=bar
1346 call assert_equal([], g:options)
1347 call assert_equal(g:opt[0], g:opt[1])
1348
zeertzjqb811de52021-10-21 10:50:44 +01001349 " 21d: Setting local string global-local (to window) option"
Bram Moolenaard7c96872019-06-15 17:12:48 +02001350 noa set statusline& " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001351 let g:options = [['statusline', oldval, oldval, '', 'baz', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001352 setlocal statusline=baz
1353 call assert_equal([], g:options)
1354 call assert_equal(g:opt[0], g:opt[1])
1355
zeertzjqb811de52021-10-21 10:50:44 +01001356 " 21e: Setting again string global-local (to window) option"
1357 " Note: v:option_old is the old global value for global-local string options
Bram Moolenaard7c96872019-06-15 17:12:48 +02001358 " but the old local value for all other kinds of options.
1359 noa setglobal statusline=bar " Reset global and local value (without triggering autocmd)
1360 noa setlocal statusline=baz
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001361 let g:options = [['statusline', 'bar', 'baz', 'bar', 'foo', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001362 set statusline=foo
1363 call assert_equal([], g:options)
1364 call assert_equal(g:opt[0], g:opt[1])
1365
1366
1367 " 22a: Setting string local (to window) option"
1368 let oldval = &foldignore
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001369 let g:options = [['foldignore', oldval, oldval, oldval, 'fo', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001370 set foldignore=fo
1371 call assert_equal([], g:options)
1372 call assert_equal(g:opt[0], g:opt[1])
1373
1374 " 22b: Resetting string local (to window) option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001375 let g:options = [['foldignore', 'fo', 'fo', 'fo', oldval, 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001376 set foldignore&
1377 call assert_equal([], g:options)
1378 call assert_equal(g:opt[0], g:opt[1])
1379
1380 " 22c: Setting global string local (to window) option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001381 let g:options = [['foldignore', oldval, '', oldval, 'bar', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001382 setglobal foldignore=bar
1383 call assert_equal([], g:options)
1384 call assert_equal(g:opt[0], g:opt[1])
1385
1386 " 22d: Setting local string local (to window) option"
1387 noa set foldignore& " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001388 let g:options = [['foldignore', oldval, oldval, '', 'baz', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001389 setlocal foldignore=baz
1390 call assert_equal([], g:options)
1391 call assert_equal(g:opt[0], g:opt[1])
1392
1393 " 22e: Setting again string local (to window) option"
1394 noa setglobal foldignore=glob " Reset global and local value (without triggering autocmd)
1395 noa setlocal foldignore=loc
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001396 let g:options = [['foldignore', 'loc', 'loc', 'glob', 'fo', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001397 set foldignore=fo
1398 call assert_equal([], g:options)
1399 call assert_equal(g:opt[0], g:opt[1])
1400
1401
zeertzjqb811de52021-10-21 10:50:44 +01001402 " 23a: Setting global number global option"
Bram Moolenaard7c96872019-06-15 17:12:48 +02001403 noa setglobal cmdheight=8 " Reset global and local value (without triggering autocmd)
1404 noa setlocal cmdheight=1 " Sets the global(!) value!
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001405 let g:options = [['cmdheight', '1', '', '1', '2', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001406 setglobal cmdheight=2
1407 call assert_equal([], g:options)
1408 call assert_equal(g:opt[0], g:opt[1])
1409
1410 " 23b: Setting local number global option"
1411 noa setglobal cmdheight=8 " Reset global and local value (without triggering autocmd)
1412 noa setlocal cmdheight=1 " Sets the global(!) value!
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001413 let g:options = [['cmdheight', '1', '1', '', '2', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001414 setlocal cmdheight=2
1415 call assert_equal([], g:options)
1416 call assert_equal(g:opt[0], g:opt[1])
1417
1418 " 23c: Setting again number global option"
1419 noa setglobal cmdheight=8 " Reset global and local value (without triggering autocmd)
1420 noa setlocal cmdheight=1 " Sets the global(!) value!
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001421 let g:options = [['cmdheight', '1', '1', '1', '2', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001422 set cmdheight=2
1423 call assert_equal([], g:options)
1424 call assert_equal(g:opt[0], g:opt[1])
1425
1426 " 23d: Setting again number global option"
1427 noa set cmdheight=8 " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001428 let g:options = [['cmdheight', '8', '8', '8', '2', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001429 set cmdheight=2
1430 call assert_equal([], g:options)
1431 call assert_equal(g:opt[0], g:opt[1])
1432
1433
1434 " 24a: Setting global number global-local (to buffer) option"
1435 noa setglobal undolevels=8 " Reset global and local value (without triggering autocmd)
1436 noa setlocal undolevels=1
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001437 let g:options = [['undolevels', '8', '', '8', '2', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001438 setglobal undolevels=2
1439 call assert_equal([], g:options)
1440 call assert_equal(g:opt[0], g:opt[1])
1441
1442 " 24b: Setting local number global-local (to buffer) option"
1443 noa setglobal undolevels=8 " Reset global and local value (without triggering autocmd)
1444 noa setlocal undolevels=1
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001445 let g:options = [['undolevels', '1', '1', '', '2', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001446 setlocal undolevels=2
1447 call assert_equal([], g:options)
1448 call assert_equal(g:opt[0], g:opt[1])
1449
1450 " 24c: Setting again number global-local (to buffer) option"
1451 noa setglobal undolevels=8 " Reset global and local value (without triggering autocmd)
1452 noa setlocal undolevels=1
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001453 let g:options = [['undolevels', '1', '1', '8', '2', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001454 set undolevels=2
1455 call assert_equal([], g:options)
1456 call assert_equal(g:opt[0], g:opt[1])
1457
1458 " 24d: Setting again global number global-local (to buffer) option"
1459 noa set undolevels=8 " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001460 let g:options = [['undolevels', '8', '8', '8', '2', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001461 set undolevels=2
1462 call assert_equal([], g:options)
1463 call assert_equal(g:opt[0], g:opt[1])
1464
1465
1466 " 25a: Setting global number local (to buffer) option"
1467 noa setglobal wrapmargin=8 " Reset global and local value (without triggering autocmd)
1468 noa setlocal wrapmargin=1
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001469 let g:options = [['wrapmargin', '8', '', '8', '2', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001470 setglobal wrapmargin=2
1471 call assert_equal([], g:options)
1472 call assert_equal(g:opt[0], g:opt[1])
1473
1474 " 25b: Setting local number local (to buffer) option"
1475 noa setglobal wrapmargin=8 " Reset global and local value (without triggering autocmd)
1476 noa setlocal wrapmargin=1
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001477 let g:options = [['wrapmargin', '1', '1', '', '2', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001478 setlocal wrapmargin=2
1479 call assert_equal([], g:options)
1480 call assert_equal(g:opt[0], g:opt[1])
1481
1482 " 25c: Setting again number local (to buffer) option"
1483 noa setglobal wrapmargin=8 " Reset global and local value (without triggering autocmd)
1484 noa setlocal wrapmargin=1
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001485 let g:options = [['wrapmargin', '1', '1', '8', '2', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001486 set wrapmargin=2
1487 call assert_equal([], g:options)
1488 call assert_equal(g:opt[0], g:opt[1])
1489
1490 " 25d: Setting again global number local (to buffer) option"
1491 noa set wrapmargin=8 " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001492 let g:options = [['wrapmargin', '8', '8', '8', '2', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001493 set wrapmargin=2
1494 call assert_equal([], g:options)
1495 call assert_equal(g:opt[0], g:opt[1])
1496
1497
1498 " 26: Setting number global-local (to window) option.
1499 " Such option does currently not exist.
1500
1501
1502 " 27a: Setting global number local (to window) option"
1503 noa setglobal foldcolumn=8 " Reset global and local value (without triggering autocmd)
1504 noa setlocal foldcolumn=1
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001505 let g:options = [['foldcolumn', '8', '', '8', '2', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001506 setglobal foldcolumn=2
1507 call assert_equal([], g:options)
1508 call assert_equal(g:opt[0], g:opt[1])
1509
1510 " 27b: Setting local number local (to window) option"
1511 noa setglobal foldcolumn=8 " Reset global and local value (without triggering autocmd)
1512 noa setlocal foldcolumn=1
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001513 let g:options = [['foldcolumn', '1', '1', '', '2', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001514 setlocal foldcolumn=2
1515 call assert_equal([], g:options)
1516 call assert_equal(g:opt[0], g:opt[1])
1517
1518 " 27c: Setting again number local (to window) option"
1519 noa setglobal foldcolumn=8 " Reset global and local value (without triggering autocmd)
1520 noa setlocal foldcolumn=1
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001521 let g:options = [['foldcolumn', '1', '1', '8', '2', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001522 set foldcolumn=2
1523 call assert_equal([], g:options)
1524 call assert_equal(g:opt[0], g:opt[1])
1525
zeertzjqb811de52021-10-21 10:50:44 +01001526 " 27d: Setting again global number local (to window) option"
Bram Moolenaard7c96872019-06-15 17:12:48 +02001527 noa set foldcolumn=8 " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001528 let g:options = [['foldcolumn', '8', '8', '8', '2', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001529 set foldcolumn=2
1530 call assert_equal([], g:options)
1531 call assert_equal(g:opt[0], g:opt[1])
1532
1533
1534 " 28a: Setting global boolean global option"
1535 noa setglobal nowrapscan " Reset global and local value (without triggering autocmd)
1536 noa setlocal wrapscan " Sets the global(!) value!
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001537 let g:options = [['wrapscan', '1', '', '1', '0', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001538 setglobal nowrapscan
1539 call assert_equal([], g:options)
1540 call assert_equal(g:opt[0], g:opt[1])
1541
1542 " 28b: Setting local boolean global option"
1543 noa setglobal nowrapscan " Reset global and local value (without triggering autocmd)
1544 noa setlocal wrapscan " Sets the global(!) value!
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001545 let g:options = [['wrapscan', '1', '1', '', '0', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001546 setlocal nowrapscan
1547 call assert_equal([], g:options)
1548 call assert_equal(g:opt[0], g:opt[1])
1549
1550 " 28c: Setting again boolean global option"
1551 noa setglobal nowrapscan " Reset global and local value (without triggering autocmd)
1552 noa setlocal wrapscan " Sets the global(!) value!
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001553 let g:options = [['wrapscan', '1', '1', '1', '0', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001554 set nowrapscan
1555 call assert_equal([], g:options)
1556 call assert_equal(g:opt[0], g:opt[1])
1557
1558 " 28d: Setting again global boolean global option"
1559 noa set nowrapscan " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001560 let g:options = [['wrapscan', '0', '0', '0', '1', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001561 set wrapscan
1562 call assert_equal([], g:options)
1563 call assert_equal(g:opt[0], g:opt[1])
1564
1565
1566 " 29a: Setting global boolean global-local (to buffer) option"
1567 noa setglobal noautoread " Reset global and local value (without triggering autocmd)
1568 noa setlocal autoread
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001569 let g:options = [['autoread', '0', '', '0', '1', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001570 setglobal autoread
1571 call assert_equal([], g:options)
1572 call assert_equal(g:opt[0], g:opt[1])
1573
1574 " 29b: Setting local boolean global-local (to buffer) option"
1575 noa setglobal noautoread " Reset global and local value (without triggering autocmd)
1576 noa setlocal autoread
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001577 let g:options = [['autoread', '1', '1', '', '0', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001578 setlocal noautoread
1579 call assert_equal([], g:options)
1580 call assert_equal(g:opt[0], g:opt[1])
1581
1582 " 29c: Setting again boolean global-local (to buffer) option"
1583 noa setglobal noautoread " Reset global and local value (without triggering autocmd)
1584 noa setlocal autoread
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001585 let g:options = [['autoread', '1', '1', '0', '1', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001586 set autoread
1587 call assert_equal([], g:options)
1588 call assert_equal(g:opt[0], g:opt[1])
1589
1590 " 29d: Setting again global boolean global-local (to buffer) option"
1591 noa set noautoread " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001592 let g:options = [['autoread', '0', '0', '0', '1', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001593 set autoread
1594 call assert_equal([], g:options)
1595 call assert_equal(g:opt[0], g:opt[1])
1596
1597
1598 " 30a: Setting global boolean local (to buffer) option"
1599 noa setglobal nocindent " Reset global and local value (without triggering autocmd)
1600 noa setlocal cindent
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001601 let g:options = [['cindent', '0', '', '0', '1', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001602 setglobal cindent
1603 call assert_equal([], g:options)
1604 call assert_equal(g:opt[0], g:opt[1])
1605
1606 " 30b: Setting local boolean local (to buffer) option"
1607 noa setglobal nocindent " Reset global and local value (without triggering autocmd)
1608 noa setlocal cindent
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001609 let g:options = [['cindent', '1', '1', '', '0', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001610 setlocal nocindent
1611 call assert_equal([], g:options)
1612 call assert_equal(g:opt[0], g:opt[1])
1613
1614 " 30c: Setting again boolean local (to buffer) option"
1615 noa setglobal nocindent " Reset global and local value (without triggering autocmd)
1616 noa setlocal cindent
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001617 let g:options = [['cindent', '1', '1', '0', '1', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001618 set cindent
1619 call assert_equal([], g:options)
1620 call assert_equal(g:opt[0], g:opt[1])
1621
1622 " 30d: Setting again global boolean local (to buffer) option"
1623 noa set nocindent " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001624 let g:options = [['cindent', '0', '0', '0', '1', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001625 set cindent
1626 call assert_equal([], g:options)
1627 call assert_equal(g:opt[0], g:opt[1])
1628
1629
1630 " 31: Setting boolean global-local (to window) option
1631 " Currently no such option exists.
1632
1633
1634 " 32a: Setting global boolean local (to window) option"
1635 noa setglobal nocursorcolumn " Reset global and local value (without triggering autocmd)
1636 noa setlocal cursorcolumn
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001637 let g:options = [['cursorcolumn', '0', '', '0', '1', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001638 setglobal cursorcolumn
1639 call assert_equal([], g:options)
1640 call assert_equal(g:opt[0], g:opt[1])
1641
1642 " 32b: Setting local boolean local (to window) option"
1643 noa setglobal nocursorcolumn " Reset global and local value (without triggering autocmd)
1644 noa setlocal cursorcolumn
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001645 let g:options = [['cursorcolumn', '1', '1', '', '0', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001646 setlocal nocursorcolumn
1647 call assert_equal([], g:options)
1648 call assert_equal(g:opt[0], g:opt[1])
1649
1650 " 32c: Setting again boolean local (to window) option"
1651 noa setglobal nocursorcolumn " Reset global and local value (without triggering autocmd)
1652 noa setlocal cursorcolumn
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001653 let g:options = [['cursorcolumn', '1', '1', '0', '1', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001654 set cursorcolumn
1655 call assert_equal([], g:options)
1656 call assert_equal(g:opt[0], g:opt[1])
1657
1658 " 32d: Setting again global boolean local (to window) option"
1659 noa set nocursorcolumn " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001660 let g:options = [['cursorcolumn', '0', '0', '0', '1', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001661 set cursorcolumn
1662 call assert_equal([], g:options)
1663 call assert_equal(g:opt[0], g:opt[1])
1664
1665
Bram Moolenaar1bc353b2019-09-01 14:45:28 +02001666 " 33: Test autocommands when an option value is converted internally.
Bram Moolenaard7c96872019-06-15 17:12:48 +02001667 noa set backspace=1 " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001668 let g:options = [['backspace', 'indent,eol', 'indent,eol', 'indent,eol', '2', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001669 set backspace=2
1670 call assert_equal([], g:options)
1671 call assert_equal(g:opt[0], g:opt[1])
1672
1673
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001674 " Cleanup
1675 au! OptionSet
Bram Moolenaar0331faf2019-06-15 18:40:37 +02001676 " set tags&
Bram Moolenaard7c96872019-06-15 17:12:48 +02001677 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 +02001678 exe printf(":set %s&vim", opt)
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001679 endfor
1680 call test_override('starting', 0)
1681 delfunc! AutoCommandOptionSet
1682endfunc
1683
1684func Test_OptionSet_diffmode()
1685 call test_override('starting', 1)
Bram Moolenaar26d98212019-01-27 22:32:55 +01001686 " 18: Changing an option when entering diff mode
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001687 new
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001688 au OptionSet diff :let &l:cul = v:option_new
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001689
1690 call setline(1, ['buffer 1', 'line2', 'line3', 'line4'])
1691 call assert_equal(0, &l:cul)
1692 diffthis
1693 call assert_equal(1, &l:cul)
1694
1695 vnew
1696 call setline(1, ['buffer 2', 'line 2', 'line 3', 'line4'])
1697 call assert_equal(0, &l:cul)
1698 diffthis
1699 call assert_equal(1, &l:cul)
1700
1701 diffoff
1702 call assert_equal(0, &l:cul)
1703 call assert_equal(1, getwinvar(2, '&l:cul'))
1704 bw!
1705
1706 call assert_equal(1, &l:cul)
1707 diffoff!
1708 call assert_equal(0, &l:cul)
1709 call assert_equal(0, getwinvar(1, '&l:cul'))
1710 bw!
1711
1712 " Cleanup
1713 au! OptionSet
1714 call test_override('starting', 0)
1715endfunc
1716
1717func Test_OptionSet_diffmode_close()
1718 call test_override('starting', 1)
1719 " 19: Try to close the current window when entering diff mode
1720 " should not segfault
1721 new
1722 au OptionSet diff close
1723
1724 call setline(1, ['buffer 1', 'line2', 'line3', 'line4'])
Bram Moolenaare2e40752020-09-04 21:18:46 +02001725 call assert_fails(':diffthis', 'E788:')
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001726 call assert_equal(1, &diff)
1727 vnew
1728 call setline(1, ['buffer 2', 'line 2', 'line 3', 'line4'])
Bram Moolenaare2e40752020-09-04 21:18:46 +02001729 call assert_fails(':diffthis', 'E788:')
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001730 call assert_equal(1, &diff)
Bram Moolenaara9aa86f2019-11-10 21:25:45 +01001731 set diffopt-=closeoff
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001732 bw!
Bram Moolenaare2e40752020-09-04 21:18:46 +02001733 call assert_fails(':diffoff!', 'E788:')
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001734 bw!
1735
1736 " Cleanup
1737 au! OptionSet
1738 call test_override('starting', 0)
1739 "delfunc! AutoCommandOptionSet
1740endfunc
Bram Moolenaar4a137b42017-08-04 22:37:11 +02001741
1742" Test for Bufleave autocommand that deletes the buffer we are about to edit.
1743func Test_BufleaveWithDelete()
Bram Moolenaare7cda972022-08-29 11:02:59 +01001744 new | edit XbufLeave1
Bram Moolenaar4a137b42017-08-04 22:37:11 +02001745
1746 augroup test_bufleavewithdelete
1747 autocmd!
Bram Moolenaare7cda972022-08-29 11:02:59 +01001748 autocmd BufLeave XbufLeave1 bwipe XbufLeave2
Bram Moolenaar4a137b42017-08-04 22:37:11 +02001749 augroup END
1750
Bram Moolenaare7cda972022-08-29 11:02:59 +01001751 call assert_fails('edit XbufLeave2', 'E143:')
1752 call assert_equal('XbufLeave1', bufname('%'))
Bram Moolenaar4a137b42017-08-04 22:37:11 +02001753
Bram Moolenaare7cda972022-08-29 11:02:59 +01001754 autocmd! test_bufleavewithdelete BufLeave XbufLeave1
Bram Moolenaar4a137b42017-08-04 22:37:11 +02001755 augroup! test_bufleavewithdelete
1756
1757 new
Bram Moolenaare7cda972022-08-29 11:02:59 +01001758 bwipe! XbufLeave1
Bram Moolenaar4a137b42017-08-04 22:37:11 +02001759endfunc
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +02001760
1761" Test for autocommand that changes the buffer list, when doing ":ball".
1762func Test_Acmd_BufAll()
1763 enew!
1764 %bwipe!
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01001765 call writefile(['Test file Xxx1'], 'Xxx1', 'D')
1766 call writefile(['Test file Xxx2'], 'Xxx2', 'D')
1767 call writefile(['Test file Xxx3'], 'Xxx3', 'D')
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +02001768
1769 " Add three files to the buffer list
1770 split Xxx1
1771 close
1772 split Xxx2
1773 close
1774 split Xxx3
1775 close
1776
1777 " Wipe the buffer when the buffer is opened
1778 au BufReadPost Xxx2 bwipe
1779
1780 call append(0, 'Test file Xxx4')
1781 ball
1782
1783 call assert_equal(2, winnr('$'))
1784 call assert_equal('Xxx1', bufname(winbufnr(winnr('$'))))
1785 wincmd t
1786
1787 au! BufReadPost
1788 %bwipe!
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +02001789 enew! | only
1790endfunc
1791
1792" Test for autocommand that changes current buffer on BufEnter event.
1793" Check if modelines are interpreted for the correct buffer.
1794func Test_Acmd_BufEnter()
1795 %bwipe!
1796 call writefile(['start of test file Xxx1',
1797 \ "\<Tab>this is a test",
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01001798 \ 'end of test file Xxx1'], 'Xxx1', 'D')
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +02001799 call writefile(['start of test file Xxx2',
1800 \ 'vim: set noai :',
1801 \ "\<Tab>this is a test",
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01001802 \ 'end of test file Xxx2'], 'Xxx2', 'D')
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +02001803
1804 au BufEnter Xxx2 brew
1805 set ai modeline modelines=3
1806 edit Xxx1
1807 " edit Xxx2, autocmd will do :brew
1808 edit Xxx2
1809 exe "normal G?this is a\<CR>"
1810 " Append text with autoindent to this file
1811 normal othis should be auto-indented
1812 call assert_equal("\<Tab>this should be auto-indented", getline('.'))
1813 call assert_equal(3, line('.'))
1814 " Remove autocmd and edit Xxx2 again
1815 au! BufEnter Xxx2
1816 buf! Xxx2
1817 exe "normal G?this is a\<CR>"
1818 " append text without autoindent to Xxx
1819 normal othis should be in column 1
1820 call assert_equal("this should be in column 1", getline('.'))
1821 call assert_equal(4, line('.'))
1822
1823 %bwipe!
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +02001824 set ai&vim modeline&vim modelines&vim
1825endfunc
1826
1827" Test for issue #57
1828" do not move cursor on <c-o> when autoindent is set
1829func Test_ai_CTRL_O()
1830 enew!
1831 set ai
1832 let save_fo = &fo
1833 set fo+=r
1834 exe "normal o# abcdef\<Esc>2hi\<CR>\<C-O>d0\<Esc>"
1835 exe "normal o# abcdef\<Esc>2hi\<C-O>d0\<Esc>"
1836 call assert_equal(['# abc', 'def', 'def'], getline(2, 4))
1837
1838 set ai&vim
1839 let &fo = save_fo
1840 enew!
1841endfunc
1842
1843" Test for autocommand that deletes the current buffer on BufLeave event.
1844" Also test deleting the last buffer, should give a new, empty buffer.
1845func Test_BufLeave_Wipe()
1846 %bwipe!
1847 let content = ['start of test file Xxx',
1848 \ 'this is a test',
1849 \ 'end of test file Xxx']
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01001850 call writefile(content, 'Xxx1', 'D')
1851 call writefile(content, 'Xxx2', 'D')
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +02001852
1853 au BufLeave Xxx2 bwipe
1854 edit Xxx1
1855 split Xxx2
1856 " delete buffer Xxx2, we should be back to Xxx1
1857 bwipe
1858 call assert_equal('Xxx1', bufname('%'))
1859 call assert_equal(1, winnr('$'))
1860
1861 " Create an alternate buffer
1862 %write! test.out
1863 call assert_equal('test.out', bufname('#'))
1864 " delete alternate buffer
1865 bwipe test.out
1866 call assert_equal('Xxx1', bufname('%'))
1867 call assert_equal('', bufname('#'))
1868
1869 au BufLeave Xxx1 bwipe
1870 " delete current buffer, get an empty one
1871 bwipe!
1872 call assert_equal(1, line('$'))
1873 call assert_equal('', bufname('%'))
Bram Moolenaarb2c87502017-10-14 21:15:58 +02001874 let g:bufinfo = getbufinfo()
1875 call assert_equal(1, len(g:bufinfo))
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +02001876
Bram Moolenaar53f0c962017-10-22 14:23:59 +02001877 call delete('test.out')
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +02001878 %bwipe
1879 au! BufLeave
Bram Moolenaarb2c87502017-10-14 21:15:58 +02001880
1881 " check that bufinfo doesn't contain a pointer to freed memory
1882 call test_garbagecollect_now()
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +02001883endfunc
Bram Moolenaar87ffb5c2017-10-19 12:37:42 +02001884
1885func Test_QuitPre()
1886 edit Xfoo
1887 let winid = win_getid(winnr())
1888 split Xbar
1889 au! QuitPre * let g:afile = expand('<afile>')
1890 " Close the other window, <afile> should be correct.
1891 exe win_id2win(winid) . 'q'
1892 call assert_equal('Xfoo', g:afile)
LemonBoy66e13ae2022-04-21 22:52:11 +01001893
Bram Moolenaar87ffb5c2017-10-19 12:37:42 +02001894 unlet g:afile
1895 bwipe Xfoo
1896 bwipe Xbar
1897endfunc
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02001898
1899func Test_Cmdline()
Bram Moolenaar153b7042018-01-31 15:48:32 +01001900 au! CmdlineChanged : let g:text = getcmdline()
1901 let g:text = 0
1902 call feedkeys(":echom 'hello'\<CR>", 'xt')
1903 call assert_equal("echom 'hello'", g:text)
1904 au! CmdlineChanged
1905
1906 au! CmdlineChanged : let g:entered = expand('<afile>')
1907 let g:entered = 0
1908 call feedkeys(":echom 'hello'\<CR>", 'xt')
1909 call assert_equal(':', g:entered)
1910 au! CmdlineChanged
1911
Bram Moolenaarbb393d82022-12-09 12:21:50 +00001912 let g:log = []
1913 cnoremap <F1> <Cmd>call setcmdline('ls')<CR>
1914 autocmd CmdlineChanged : let g:log += [getcmdline()]
1915 call feedkeys(":\<F1>", 'xt')
1916 call assert_equal(['ls'], g:log)
1917 unlet g:log
1918 au! CmdlineChanged
1919 cunmap <F1>
1920
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02001921 au! CmdlineEnter : let g:entered = expand('<afile>')
1922 au! CmdlineLeave : let g:left = expand('<afile>')
1923 let g:entered = 0
1924 let g:left = 0
1925 call feedkeys(":echo 'hello'\<CR>", 'xt')
1926 call assert_equal(':', g:entered)
1927 call assert_equal(':', g:left)
1928 au! CmdlineEnter
1929 au! CmdlineLeave
1930
Bram Moolenaara4baf5b2018-04-22 13:27:44 +02001931 let save_shellslash = &shellslash
1932 set noshellslash
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02001933 au! CmdlineEnter / let g:entered = expand('<afile>')
1934 au! CmdlineLeave / let g:left = expand('<afile>')
1935 let g:entered = 0
1936 let g:left = 0
Bram Moolenaar53f0c962017-10-22 14:23:59 +02001937 new
1938 call setline(1, 'hello')
1939 call feedkeys("/hello\<CR>", 'xt')
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02001940 call assert_equal('/', g:entered)
1941 call assert_equal('/', g:left)
Bram Moolenaar53f0c962017-10-22 14:23:59 +02001942 bwipe!
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02001943 au! CmdlineEnter
1944 au! CmdlineLeave
Bram Moolenaara4baf5b2018-04-22 13:27:44 +02001945 let &shellslash = save_shellslash
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02001946endfunc
Bram Moolenaar53f0c962017-10-22 14:23:59 +02001947
1948" Test for BufWritePre autocommand that deletes or unloads the buffer.
1949func Test_BufWritePre()
1950 %bwipe
1951 au BufWritePre Xxx1 bunload
1952 au BufWritePre Xxx2 bwipe
1953
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01001954 call writefile(['start of Xxx1', 'test', 'end of Xxx1'], 'Xxx1', 'D')
1955 call writefile(['start of Xxx2', 'test', 'end of Xxx2'], 'Xxx2', 'D')
Bram Moolenaar53f0c962017-10-22 14:23:59 +02001956
1957 edit Xtest
1958 e! Xxx2
1959 bdel Xtest
1960 e Xxx1
1961 " write it, will unload it and give an error msg
Bram Moolenaare2e40752020-09-04 21:18:46 +02001962 call assert_fails('w', 'E203:')
Bram Moolenaar53f0c962017-10-22 14:23:59 +02001963 call assert_equal('Xxx2', bufname('%'))
1964 edit Xtest
1965 e! Xxx2
1966 bwipe Xtest
1967 " write it, will delete the buffer and give an error msg
Bram Moolenaare2e40752020-09-04 21:18:46 +02001968 call assert_fails('w', 'E203:')
Bram Moolenaar53f0c962017-10-22 14:23:59 +02001969 call assert_equal('Xxx1', bufname('%'))
1970 au! BufWritePre
Bram Moolenaar53f0c962017-10-22 14:23:59 +02001971endfunc
1972
1973" Test for BufUnload autocommand that unloads all the other buffers
1974func Test_bufunload_all()
Bram Moolenaarf08b0eb2021-10-16 13:00:14 +01001975 let g:test_is_flaky = 1
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01001976 call writefile(['Test file Xxx1'], 'Xxx1', 'D')"
1977 call writefile(['Test file Xxx2'], 'Xxx2', 'D')"
Bram Moolenaar53f0c962017-10-22 14:23:59 +02001978
Bram Moolenaarc79745a2019-05-20 22:12:34 +02001979 let content =<< trim [CODE]
1980 func UnloadAllBufs()
1981 let i = 1
1982 while i <= bufnr('$')
1983 if i != bufnr('%') && bufloaded(i)
1984 exe i . 'bunload'
1985 endif
1986 let i += 1
1987 endwhile
1988 endfunc
1989 au BufUnload * call UnloadAllBufs()
1990 au VimLeave * call writefile(['Test Finished'], 'Xout')
1991 edit Xxx1
1992 split Xxx2
1993 q
1994 [CODE]
1995
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01001996 call writefile(content, 'Xbunloadtest', 'D')
Bram Moolenaar53f0c962017-10-22 14:23:59 +02001997
1998 call delete('Xout')
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01001999 call system(GetVimCommandClean() .. ' -N --not-a-term -S Xbunloadtest')
Bram Moolenaar53f0c962017-10-22 14:23:59 +02002000 call assert_true(filereadable('Xout'))
2001
Bram Moolenaar53f0c962017-10-22 14:23:59 +02002002 call delete('Xout')
2003endfunc
2004
2005" Some tests for buffer-local autocommands
2006func Test_buflocal_autocmd()
2007 let g:bname = ''
2008 edit xx
2009 au BufLeave <buffer> let g:bname = expand("%")
2010 " here, autocommand for xx should trigger.
2011 " but autocommand shall not apply to buffer named <buffer>.
2012 edit somefile
2013 call assert_equal('xx', g:bname)
2014 let g:bname = ''
2015 " here, autocommand shall be auto-deleted
2016 bwipe xx
2017 " autocmd should not trigger
2018 edit xx
2019 call assert_equal('', g:bname)
2020 " autocmd should not trigger
2021 edit somefile
2022 call assert_equal('', g:bname)
2023 enew
2024 unlet g:bname
2025endfunc
Bram Moolenaar430dc5d2017-11-02 21:04:47 +01002026
2027" Test for "*Cmd" autocommands
2028func Test_Cmd_Autocmds()
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01002029 call writefile(['start of Xxx', "\tabc2", 'end of Xxx'], 'Xxx', 'D')
Bram Moolenaar430dc5d2017-11-02 21:04:47 +01002030
2031 enew!
2032 au BufReadCmd XtestA 0r Xxx|$del
2033 edit XtestA " will read text of Xxd instead
2034 call assert_equal('start of Xxx', getline(1))
2035
2036 au BufWriteCmd XtestA call append(line("$"), "write")
2037 write " will append a line to the file
2038 call assert_equal('write', getline('$'))
Bram Moolenaare2e40752020-09-04 21:18:46 +02002039 call assert_fails('read XtestA', 'E484:') " should not read anything
Bram Moolenaar430dc5d2017-11-02 21:04:47 +01002040 call assert_equal('write', getline(4))
2041
2042 " now we have:
2043 " 1 start of Xxx
2044 " 2 abc2
2045 " 3 end of Xxx
2046 " 4 write
2047
2048 au FileReadCmd XtestB '[r Xxx
2049 2r XtestB " will read Xxx below line 2 instead
2050 call assert_equal('start of Xxx', getline(3))
2051
2052 " now we have:
2053 " 1 start of Xxx
2054 " 2 abc2
2055 " 3 start of Xxx
2056 " 4 abc2
2057 " 5 end of Xxx
2058 " 6 end of Xxx
2059 " 7 write
2060
2061 au FileWriteCmd XtestC '[,']copy $
2062 normal 4GA1
2063 4,5w XtestC " will copy lines 4 and 5 to the end
2064 call assert_equal("\tabc21", getline(8))
Bram Moolenaare2e40752020-09-04 21:18:46 +02002065 call assert_fails('r XtestC', 'E484:') " should not read anything
Bram Moolenaar430dc5d2017-11-02 21:04:47 +01002066 call assert_equal("end of Xxx", getline(9))
2067
2068 " now we have:
2069 " 1 start of Xxx
2070 " 2 abc2
2071 " 3 start of Xxx
2072 " 4 abc21
2073 " 5 end of Xxx
2074 " 6 end of Xxx
2075 " 7 write
2076 " 8 abc21
2077 " 9 end of Xxx
2078
2079 let g:lines = []
2080 au FileAppendCmd XtestD call extend(g:lines, getline(line("'["), line("']")))
2081 w >>XtestD " will add lines to 'lines'
2082 call assert_equal(9, len(g:lines))
Bram Moolenaare2e40752020-09-04 21:18:46 +02002083 call assert_fails('$r XtestD', 'E484:') " should not read anything
Bram Moolenaar430dc5d2017-11-02 21:04:47 +01002084 call assert_equal(9, line('$'))
2085 call assert_equal('end of Xxx', getline('$'))
2086
2087 au BufReadCmd XtestE 0r Xxx|$del
2088 sp XtestE " split window with test.out
2089 call assert_equal('end of Xxx', getline(3))
2090
2091 let g:lines = []
2092 exe "normal 2Goasdf\<Esc>\<C-W>\<C-W>"
2093 au BufWriteCmd XtestE call extend(g:lines, getline(0, '$'))
2094 wall " will write other window to 'lines'
2095 call assert_equal(4, len(g:lines), g:lines)
2096 call assert_equal('asdf', g:lines[2])
2097
2098 au! BufReadCmd
2099 au! BufWriteCmd
2100 au! FileReadCmd
2101 au! FileWriteCmd
2102 au! FileAppendCmd
2103 %bwipe!
Bram Moolenaar430dc5d2017-11-02 21:04:47 +01002104 enew!
2105endfunc
Bram Moolenaaraace2152017-11-05 16:23:10 +01002106
Bram Moolenaar0fff4412020-03-29 16:06:29 +02002107func s:ReadFile()
2108 setl noswapfile nomodified
2109 let filename = resolve(expand("<afile>:p"))
2110 execute 'read' fnameescape(filename)
2111 1d_
2112 exe 'file' fnameescape(filename)
2113 setl buftype=acwrite
2114endfunc
2115
2116func s:WriteFile()
2117 let filename = resolve(expand("<afile>:p"))
2118 setl buftype=
2119 noautocmd execute 'write' fnameescape(filename)
2120 setl buftype=acwrite
2121 setl nomodified
2122endfunc
2123
2124func Test_BufReadCmd()
2125 autocmd BufReadCmd *.test call s:ReadFile()
2126 autocmd BufWriteCmd *.test call s:WriteFile()
2127
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01002128 call writefile(['one', 'two', 'three'], 'Xcmd.test', 'D')
Bram Moolenaar0fff4412020-03-29 16:06:29 +02002129 edit Xcmd.test
2130 call assert_match('Xcmd.test" line 1 of 3', execute('file'))
2131 normal! Gofour
2132 write
2133 call assert_equal(['one', 'two', 'three', 'four'], readfile('Xcmd.test'))
2134
2135 bwipe!
Bram Moolenaar0fff4412020-03-29 16:06:29 +02002136 au! BufReadCmd
2137 au! BufWriteCmd
2138endfunc
2139
zeertzjq9c8f9462022-08-30 18:17:15 +01002140func Test_BufWriteCmd()
2141 autocmd BufWriteCmd Xbufwritecmd let g:written = 1
2142 new
2143 file Xbufwritecmd
2144 set buftype=acwrite
Bram Moolenaar6f14da12022-09-07 21:30:44 +01002145 call mkdir('Xbufwritecmd', 'D')
zeertzjq9c8f9462022-08-30 18:17:15 +01002146 write
2147 " BufWriteCmd should be triggered even if a directory has the same name
2148 call assert_equal(1, g:written)
zeertzjq9c8f9462022-08-30 18:17:15 +01002149 unlet g:written
2150 au! BufWriteCmd
2151 bwipe!
2152endfunc
2153
Bram Moolenaaraace2152017-11-05 16:23:10 +01002154func SetChangeMarks(start, end)
Bram Moolenaar97c69432021-01-15 16:45:21 +01002155 exe a:start .. 'mark ['
2156 exe a:end .. 'mark ]'
Bram Moolenaaraace2152017-11-05 16:23:10 +01002157endfunc
2158
2159" Verify the effects of autocmds on '[ and ']
2160func Test_change_mark_in_autocmds()
2161 edit! Xtest
Bram Moolenaar97c69432021-01-15 16:45:21 +01002162 call feedkeys("ia\<CR>b\<CR>c\<CR>d\<C-g>u\<Esc>", 'xtn')
Bram Moolenaaraace2152017-11-05 16:23:10 +01002163
2164 call SetChangeMarks(2, 3)
2165 write
2166 call assert_equal([1, 4], [line("'["), line("']")])
2167
2168 call SetChangeMarks(2, 3)
2169 au BufWritePre * call assert_equal([1, 4], [line("'["), line("']")])
2170 write
2171 au! BufWritePre
2172
Bram Moolenaar14ddd222020-08-05 12:02:40 +02002173 if has('unix')
Bram Moolenaaraace2152017-11-05 16:23:10 +01002174 write XtestFilter
2175 write >> XtestFilter
2176
2177 call SetChangeMarks(2, 3)
2178 " Marks are set to the entire range of the write
2179 au FilterWritePre * call assert_equal([1, 4], [line("'["), line("']")])
2180 " '[ is adjusted to just before the line that will receive the filtered
2181 " data
2182 au FilterReadPre * call assert_equal([4, 4], [line("'["), line("']")])
2183 " The filtered data is read into the buffer, and the source lines are
2184 " still present, so the range is after the source lines
2185 au FilterReadPost * call assert_equal([5, 12], [line("'["), line("']")])
2186 %!cat XtestFilter
2187 " After the filtered data is read, the original lines are deleted
2188 call assert_equal([1, 8], [line("'["), line("']")])
2189 au! FilterWritePre,FilterReadPre,FilterReadPost
2190 undo
2191
2192 call SetChangeMarks(1, 4)
2193 au FilterWritePre * call assert_equal([2, 3], [line("'["), line("']")])
2194 au FilterReadPre * call assert_equal([3, 3], [line("'["), line("']")])
2195 au FilterReadPost * call assert_equal([4, 11], [line("'["), line("']")])
2196 2,3!cat XtestFilter
2197 call assert_equal([2, 9], [line("'["), line("']")])
2198 au! FilterWritePre,FilterReadPre,FilterReadPost
2199 undo
2200
2201 call delete('XtestFilter')
2202 endif
2203
2204 call SetChangeMarks(1, 4)
2205 au FileWritePre * call assert_equal([2, 3], [line("'["), line("']")])
2206 2,3write Xtest2
2207 au! FileWritePre
2208
2209 call SetChangeMarks(2, 3)
2210 au FileAppendPre * call assert_equal([1, 4], [line("'["), line("']")])
2211 write >> Xtest2
2212 au! FileAppendPre
2213
2214 call SetChangeMarks(1, 4)
2215 au FileAppendPre * call assert_equal([2, 3], [line("'["), line("']")])
2216 2,3write >> Xtest2
2217 au! FileAppendPre
2218
2219 call SetChangeMarks(1, 1)
2220 au FileReadPre * call assert_equal([3, 1], [line("'["), line("']")])
2221 au FileReadPost * call assert_equal([4, 11], [line("'["), line("']")])
2222 3read Xtest2
2223 au! FileReadPre,FileReadPost
2224 undo
2225
2226 call SetChangeMarks(4, 4)
2227 " When the line is 0, it's adjusted to 1
2228 au FileReadPre * call assert_equal([1, 4], [line("'["), line("']")])
2229 au FileReadPost * call assert_equal([1, 8], [line("'["), line("']")])
2230 0read Xtest2
2231 au! FileReadPre,FileReadPost
2232 undo
2233
2234 call SetChangeMarks(4, 4)
2235 " When the line is 0, it's adjusted to 1
2236 au FileReadPre * call assert_equal([1, 4], [line("'["), line("']")])
2237 au FileReadPost * call assert_equal([2, 9], [line("'["), line("']")])
2238 1read Xtest2
2239 au! FileReadPre,FileReadPost
2240 undo
2241
2242 bwipe!
2243 call delete('Xtest')
2244 call delete('Xtest2')
2245endfunc
2246
2247func Test_Filter_noshelltemp()
Bram Moolenaaraeb313f2020-11-27 19:13:28 +01002248 CheckExecutable cat
Bram Moolenaaraace2152017-11-05 16:23:10 +01002249
2250 enew!
2251 call setline(1, ['a', 'b', 'c', 'd'])
2252
2253 let shelltemp = &shelltemp
2254 set shelltemp
2255
2256 let g:filter_au = 0
2257 au FilterWritePre * let g:filter_au += 1
2258 au FilterReadPre * let g:filter_au += 1
2259 au FilterReadPost * let g:filter_au += 1
2260 %!cat
2261 call assert_equal(3, g:filter_au)
2262
2263 if has('filterpipe')
2264 set noshelltemp
2265
2266 let g:filter_au = 0
2267 au FilterWritePre * let g:filter_au += 1
2268 au FilterReadPre * let g:filter_au += 1
2269 au FilterReadPost * let g:filter_au += 1
2270 %!cat
2271 call assert_equal(0, g:filter_au)
2272 endif
2273
2274 au! FilterWritePre,FilterReadPre,FilterReadPost
2275 let &shelltemp = shelltemp
2276 bwipe!
2277endfunc
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01002278
2279func Test_TextYankPost()
2280 enew!
2281 call setline(1, ['foo'])
2282
2283 let g:event = []
2284 au TextYankPost * let g:event = copy(v:event)
2285
2286 call assert_equal({}, v:event)
2287 call assert_fails('let v:event = {}', 'E46:')
2288 call assert_fails('let v:event.mykey = 0', 'E742:')
2289
2290 norm "ayiw
2291 call assert_equal(
Bram Moolenaara016eeb2022-04-09 11:37:38 +01002292 \ #{regcontents: ['foo'], regname: 'a', operator: 'y',
2293 \ regtype: 'v', visual: v:false, inclusive: v:true},
2294 \ g:event)
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01002295 norm y_
2296 call assert_equal(
Bram Moolenaara016eeb2022-04-09 11:37:38 +01002297 \ #{regcontents: ['foo'], regname: '', operator: 'y', regtype: 'V',
2298 \ visual: v:false, inclusive: v:false},
2299 \ g:event)
Bram Moolenaar37d16732020-06-12 22:09:01 +02002300 norm Vy
2301 call assert_equal(
Bram Moolenaara016eeb2022-04-09 11:37:38 +01002302 \ #{regcontents: ['foo'], regname: '', operator: 'y', regtype: 'V',
2303 \ visual: v:true, inclusive: v:true},
2304 \ g:event)
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01002305 call feedkeys("\<C-V>y", 'x')
2306 call assert_equal(
Bram Moolenaara016eeb2022-04-09 11:37:38 +01002307 \ #{regcontents: ['f'], regname: '', operator: 'y', regtype: "\x161",
2308 \ visual: v:true, inclusive: v:true},
2309 \ g:event)
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01002310 norm "xciwbar
2311 call assert_equal(
Bram Moolenaara016eeb2022-04-09 11:37:38 +01002312 \ #{regcontents: ['foo'], regname: 'x', operator: 'c', regtype: 'v',
2313 \ visual: v:false, inclusive: v:true},
2314 \ g:event)
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01002315 norm "bdiw
2316 call assert_equal(
Bram Moolenaara016eeb2022-04-09 11:37:38 +01002317 \ #{regcontents: ['bar'], regname: 'b', operator: 'd', regtype: 'v',
2318 \ visual: v:false, inclusive: v:true},
2319 \ g:event)
2320
2321 call setline(1, 'foobar')
2322 " exclusive motion
2323 norm $"ay0
2324 call assert_equal(
2325 \ #{regcontents: ['fooba'], regname: 'a', operator: 'y', regtype: 'v',
2326 \ visual: v:false, inclusive: v:false},
2327 \ g:event)
2328 " inclusive motion
2329 norm 0"ay$
2330 call assert_equal(
2331 \ #{regcontents: ['foobar'], regname: 'a', operator: 'y', regtype: 'v',
2332 \ visual: v:false, inclusive: v:true},
2333 \ g:event)
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01002334
2335 call assert_equal({}, v:event)
2336
Bram Moolenaarfccbf062020-11-26 20:34:00 +01002337 if has('clipboard_working') && !has('gui_running')
2338 " Test that when the visual selection is automatically copied to clipboard
2339 " register a TextYankPost is emitted
2340 call setline(1, ['foobar'])
2341
2342 let @* = ''
2343 set clipboard=autoselect
2344 exe "norm! ggviw\<Esc>"
2345 call assert_equal(
Bram Moolenaara016eeb2022-04-09 11:37:38 +01002346 \ #{regcontents: ['foobar'], regname: '*', operator: 'y',
2347 \ regtype: 'v', visual: v:true, inclusive: v:false},
2348 \ g:event)
Bram Moolenaarfccbf062020-11-26 20:34:00 +01002349
2350 let @+ = ''
2351 set clipboard=autoselectplus
2352 exe "norm! ggviw\<Esc>"
2353 call assert_equal(
Bram Moolenaara016eeb2022-04-09 11:37:38 +01002354 \ #{regcontents: ['foobar'], regname: '+', operator: 'y',
2355 \ regtype: 'v', visual: v:true, inclusive: v:false},
2356 \ g:event)
Bram Moolenaarfccbf062020-11-26 20:34:00 +01002357
2358 set clipboard&vim
2359 endif
2360
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01002361 au! TextYankPost
2362 unlet g:event
2363 bwipe!
2364endfunc
Bram Moolenaar9bca8052017-12-18 12:37:55 +01002365
Bram Moolenaar9a046fd2021-01-28 13:47:59 +01002366func Test_autocommand_all_events()
2367 call assert_fails('au * * bwipe', 'E1155:')
2368 call assert_fails('au * x bwipe', 'E1155:')
Bram Moolenaarb6db1462021-12-24 19:24:47 +00002369 call assert_fails('au! * x bwipe', 'E1155:')
Bram Moolenaar4fb921e2017-12-18 15:33:00 +01002370endfunc
Bram Moolenaarb7407d32018-02-03 17:36:27 +01002371
Bram Moolenaarf6246f52022-02-11 16:30:12 +00002372func Test_autocmd_user()
2373 au User MyEvent let s:res = [expand("<afile>"), expand("<amatch>")]
2374 doautocmd User MyEvent
2375 call assert_equal(['MyEvent', 'MyEvent'], s:res)
2376 au! User
2377 unlet s:res
2378endfunc
2379
Bram Moolenaar3b014be2022-11-13 17:53:46 +00002380func Test_autocmd_user_clear_group()
2381 CheckRunVimInTerminal
2382
2383 let lines =<< trim END
2384 autocmd! User
2385 for i in range(1, 999)
2386 exe 'autocmd User ' .. 'Foo' .. i .. ' bar'
2387 endfor
2388 au CmdlineLeave : call timer_start(0, {-> execute('autocmd! User')})
2389 END
2390 call writefile(lines, 'XautoUser', 'D')
2391 let buf = RunVimInTerminal('-S XautoUser', {'rows': 10})
2392
2393 " this was using freed memory
2394 call term_sendkeys(buf, ":autocmd User\<CR>")
2395 call TermWait(buf, 50)
2396 call term_sendkeys(buf, "G")
2397
2398 call StopVimInTerminal(buf)
2399endfunc
2400
Bram Moolenaaref2c3252022-11-25 16:31:51 +00002401func Test_autocmd_CmdlineLeave_unlet()
2402 CheckRunVimInTerminal
2403
2404 let lines =<< trim END
2405 for i in range(1, 999)
2406 exe 'let g:var' .. i '=' i
2407 endfor
2408 au CmdlineLeave : call timer_start(0, {-> execute('unlet g:var990')})
2409 END
2410 call writefile(lines, 'XleaveUnlet', 'D')
2411 let buf = RunVimInTerminal('-S XleaveUnlet', {'rows': 10})
2412
2413 " this was using freed memory
2414 call term_sendkeys(buf, ":let g:\<CR>")
2415 call TermWait(buf, 50)
2416 call term_sendkeys(buf, "G")
2417 call TermWait(buf, 50)
2418 call term_sendkeys(buf, "\<CR>") " for the hit-enter prompt
2419
2420 call StopVimInTerminal(buf)
2421endfunc
2422
Bram Moolenaarb7407d32018-02-03 17:36:27 +01002423function s:Before_test_dirchanged()
2424 augroup test_dirchanged
2425 autocmd!
2426 augroup END
2427 let s:li = []
2428 let s:dir_this = getcwd()
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02002429 let s:dir_foo = s:dir_this . '/Xfoo'
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01002430 call mkdir(s:dir_foo)
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02002431 let s:dir_bar = s:dir_this . '/Xbar'
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01002432 call mkdir(s:dir_bar)
Bram Moolenaarb7407d32018-02-03 17:36:27 +01002433endfunc
2434
2435function s:After_test_dirchanged()
Bram Moolenaar3503d7c2019-11-09 20:10:17 +01002436 call chdir(s:dir_this)
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01002437 call delete(s:dir_foo, 'd')
2438 call delete(s:dir_bar, 'd')
Bram Moolenaarb7407d32018-02-03 17:36:27 +01002439 augroup test_dirchanged
2440 autocmd!
2441 augroup END
2442endfunc
2443
2444function Test_dirchanged_global()
2445 call s:Before_test_dirchanged()
Bram Moolenaarf6246f52022-02-11 16:30:12 +00002446 autocmd test_dirchanged DirChangedPre global call add(s:li, expand("<amatch>") .. " pre cd " .. v:event.directory)
Bram Moolenaarb7407d32018-02-03 17:36:27 +01002447 autocmd test_dirchanged DirChanged global call add(s:li, "cd:")
2448 autocmd test_dirchanged DirChanged global call add(s:li, expand("<afile>"))
Bram Moolenaar3503d7c2019-11-09 20:10:17 +01002449 call chdir(s:dir_foo)
Bram Moolenaarf6246f52022-02-11 16:30:12 +00002450 let expected = ["global pre cd " .. s:dir_foo, "cd:", s:dir_foo]
Bram Moolenaar28e8f732022-02-09 12:58:20 +00002451 call assert_equal(expected, s:li)
Bram Moolenaar3503d7c2019-11-09 20:10:17 +01002452 call chdir(s:dir_foo)
Bram Moolenaar28e8f732022-02-09 12:58:20 +00002453 call assert_equal(expected, s:li)
Bram Moolenaar3503d7c2019-11-09 20:10:17 +01002454 exe 'lcd ' .. fnameescape(s:dir_bar)
Bram Moolenaar28e8f732022-02-09 12:58:20 +00002455 call assert_equal(expected, s:li)
Bram Moolenaard8c9d322022-06-12 11:49:16 +01002456
2457 exe 'cd ' .. s:dir_foo
2458 exe 'cd ' .. s:dir_bar
2459 autocmd! test_dirchanged DirChanged global let g:result = expand("<afile>")
2460 cd -
Bram Moolenaardb77c492022-06-12 23:26:50 +01002461 call assert_equal(s:dir_foo, substitute(g:result, '\\', '/', 'g'))
Bram Moolenaard8c9d322022-06-12 11:49:16 +01002462
Bram Moolenaarb7407d32018-02-03 17:36:27 +01002463 call s:After_test_dirchanged()
2464endfunc
2465
2466function Test_dirchanged_local()
2467 call s:Before_test_dirchanged()
2468 autocmd test_dirchanged DirChanged window call add(s:li, "lcd:")
2469 autocmd test_dirchanged DirChanged window call add(s:li, expand("<afile>"))
Bram Moolenaar3503d7c2019-11-09 20:10:17 +01002470 call chdir(s:dir_foo)
Bram Moolenaarb7407d32018-02-03 17:36:27 +01002471 call assert_equal([], s:li)
Bram Moolenaar3503d7c2019-11-09 20:10:17 +01002472 exe 'lcd ' .. fnameescape(s:dir_bar)
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01002473 call assert_equal(["lcd:", s:dir_bar], s:li)
Bram Moolenaar3503d7c2019-11-09 20:10:17 +01002474 exe 'lcd ' .. fnameescape(s:dir_bar)
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01002475 call assert_equal(["lcd:", s:dir_bar], s:li)
Bram Moolenaarb7407d32018-02-03 17:36:27 +01002476 call s:After_test_dirchanged()
2477endfunc
2478
2479function Test_dirchanged_auto()
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +02002480 CheckOption autochdir
Bram Moolenaarb7407d32018-02-03 17:36:27 +01002481 call s:Before_test_dirchanged()
2482 call test_autochdir()
Bram Moolenaar28e8f732022-02-09 12:58:20 +00002483 autocmd test_dirchanged DirChangedPre auto call add(s:li, "pre cd " .. v:event.directory)
Bram Moolenaarb7407d32018-02-03 17:36:27 +01002484 autocmd test_dirchanged DirChanged auto call add(s:li, "auto:")
2485 autocmd test_dirchanged DirChanged auto call add(s:li, expand("<afile>"))
2486 set acd
Bram Moolenaar3503d7c2019-11-09 20:10:17 +01002487 cd ..
Bram Moolenaarb7407d32018-02-03 17:36:27 +01002488 call assert_equal([], s:li)
Bram Moolenaar61abe7d2022-08-30 21:46:08 +01002489 exe 'edit ' . s:dir_foo . '/Xautofile'
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01002490 call assert_equal(s:dir_foo, getcwd())
Bram Moolenaar28e8f732022-02-09 12:58:20 +00002491 let expected = ["pre cd " .. s:dir_foo, "auto:", s:dir_foo]
2492 call assert_equal(expected, s:li)
Bram Moolenaarb7407d32018-02-03 17:36:27 +01002493 set noacd
2494 bwipe!
2495 call s:After_test_dirchanged()
2496endfunc
Bram Moolenaar5a093432018-02-10 18:15:19 +01002497
2498" Test TextChangedI and TextChangedP
2499func Test_ChangedP()
2500 new
2501 call setline(1, ['foo', 'bar', 'foobar'])
2502 call test_override("char_avail", 1)
2503 set complete=. completeopt=menuone
2504
2505 func! TextChangedAutocmd(char)
2506 let g:autocmd .= a:char
2507 endfunc
2508
Christian Brabandtdb3b4462021-10-16 11:58:55 +01002509 " TextChanged will not be triggered, only check that it isn't.
Bram Moolenaar5a093432018-02-10 18:15:19 +01002510 au! TextChanged <buffer> :call TextChangedAutocmd('N')
2511 au! TextChangedI <buffer> :call TextChangedAutocmd('I')
2512 au! TextChangedP <buffer> :call TextChangedAutocmd('P')
2513
2514 call cursor(3, 1)
2515 let g:autocmd = ''
2516 call feedkeys("o\<esc>", 'tnix')
2517 call assert_equal('I', g:autocmd)
2518
2519 let g:autocmd = ''
2520 call feedkeys("Sf", 'tnix')
2521 call assert_equal('II', g:autocmd)
2522
2523 let g:autocmd = ''
2524 call feedkeys("Sf\<C-N>", 'tnix')
2525 call assert_equal('IIP', g:autocmd)
2526
2527 let g:autocmd = ''
2528 call feedkeys("Sf\<C-N>\<C-N>", 'tnix')
2529 call assert_equal('IIPP', g:autocmd)
2530
2531 let g:autocmd = ''
2532 call feedkeys("Sf\<C-N>\<C-N>\<C-N>", 'tnix')
2533 call assert_equal('IIPPP', g:autocmd)
2534
2535 let g:autocmd = ''
2536 call feedkeys("Sf\<C-N>\<C-N>\<C-N>\<C-N>", 'tnix')
2537 call assert_equal('IIPPPP', g:autocmd)
2538
2539 call assert_equal(['foo', 'bar', 'foobar', 'foo'], getline(1, '$'))
2540 " TODO: how should it handle completeopt=noinsert,noselect?
2541
2542 " CleanUp
2543 call test_override("char_avail", 0)
2544 au! TextChanged
2545 au! TextChangedI
2546 au! TextChangedP
2547 delfu TextChangedAutocmd
2548 unlet! g:autocmd
2549 set complete&vim completeopt&vim
2550
2551 bw!
2552endfunc
Bram Moolenaar8c64a362018-03-23 22:39:31 +01002553
Bram Moolenaar91d2e782018-08-07 19:05:01 +02002554let g:setline_handled = v:false
Bram Moolenaar1e115362019-01-09 23:01:02 +01002555func SetLineOne()
Bram Moolenaar91d2e782018-08-07 19:05:01 +02002556 if !g:setline_handled
2557 call setline(1, "(x)")
2558 let g:setline_handled = v:true
2559 endif
2560endfunc
2561
2562func Test_TextChangedI_with_setline()
2563 new
2564 call test_override('char_avail', 1)
2565 autocmd TextChangedI <buffer> call SetLineOne()
2566 call feedkeys("i(\<CR>\<Esc>", 'tx')
2567 call assert_equal('(', getline(1))
2568 call assert_equal('x)', getline(2))
2569 undo
Bram Moolenaar91d2e782018-08-07 19:05:01 +02002570 call assert_equal('', getline(1))
Bram Moolenaar9fa95062018-08-08 22:08:32 +02002571 call assert_equal('', getline(2))
Bram Moolenaar91d2e782018-08-07 19:05:01 +02002572
Bram Moolenaarca34db32022-01-20 11:17:18 +00002573 call test_override('char_avail', 0)
Bram Moolenaar91d2e782018-08-07 19:05:01 +02002574 bwipe!
2575endfunc
2576
Bram Moolenaar8c64a362018-03-23 22:39:31 +01002577func Test_Changed_FirstTime()
Bram Moolenaar8c5a2782019-08-07 23:07:07 +02002578 CheckFeature terminal
2579 CheckNotGui
Bram Moolenaar3cdcb092020-03-18 19:18:10 +01002580 " Starting a terminal to run Vim is always considered flaky.
Bram Moolenaar30d53e22020-03-18 21:10:44 +01002581 let g:test_is_flaky = 1
Bram Moolenaar8c5a2782019-08-07 23:07:07 +02002582
Bram Moolenaar8c64a362018-03-23 22:39:31 +01002583 " Prepare file for TextChanged event.
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01002584 call writefile([''], 'Xchanged.txt', 'D')
Bram Moolenaar8c64a362018-03-23 22:39:31 +01002585 let buf = term_start([GetVimProg(), '--clean', '-c', 'set noswapfile'], {'term_rows': 3})
2586 call assert_equal('running', term_getstatus(buf))
Bram Moolenaar1834d372018-03-29 17:40:46 +02002587 " Wait for the ruler (in the status line) to be shown.
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01002588 " In ConPTY, there is additional character which is drawn up to the width of
2589 " the screen.
2590 if has('conpty')
2591 call WaitForAssert({-> assert_match('\<All.*$', term_getline(buf, 3))})
2592 else
2593 call WaitForAssert({-> assert_match('\<All$', term_getline(buf, 3))})
2594 endif
Bram Moolenaar8c64a362018-03-23 22:39:31 +01002595 " It's only adding autocmd, so that no event occurs.
2596 call term_sendkeys(buf, ":au! TextChanged <buffer> call writefile(['No'], 'Xchanged.txt')\<cr>")
2597 call term_sendkeys(buf, "\<C-\\>\<C-N>:qa!\<cr>")
Bram Moolenaar50182fa2018-04-28 21:34:40 +02002598 call WaitForAssert({-> assert_equal('finished', term_getstatus(buf))})
Bram Moolenaar8c64a362018-03-23 22:39:31 +01002599 call assert_equal([''], readfile('Xchanged.txt'))
2600
2601 " clean up
Bram Moolenaar8c64a362018-03-23 22:39:31 +01002602 bwipe!
2603endfunc
Bram Moolenaar0566e892019-01-24 19:37:40 +01002604
Bram Moolenaareb93f3f2019-04-04 15:04:56 +02002605func Test_autocmd_nested()
2606 let g:did_nested = 0
2607 augroup Testing
2608 au WinNew * edit somefile
2609 au BufNew * let g:did_nested = 1
2610 augroup END
2611 split
2612 call assert_equal(0, g:did_nested)
2613 close
2614 bwipe! somefile
2615
2616 " old nested argument still works
2617 augroup Testing
2618 au!
2619 au WinNew * nested edit somefile
2620 au BufNew * let g:did_nested = 1
2621 augroup END
2622 split
2623 call assert_equal(1, g:did_nested)
2624 close
2625 bwipe! somefile
2626
2627 " New ++nested argument works
2628 augroup Testing
2629 au!
2630 au WinNew * ++nested edit somefile
2631 au BufNew * let g:did_nested = 1
2632 augroup END
2633 split
2634 call assert_equal(1, g:did_nested)
2635 close
2636 bwipe! somefile
2637
Bram Moolenaarf0775142022-03-04 20:10:38 +00002638 " nested without ++ does not work in Vim9 script
2639 call assert_fails('vim9cmd au WinNew * nested echo fails', 'E1078:')
2640
Bram Moolenaareb93f3f2019-04-04 15:04:56 +02002641 augroup Testing
2642 au!
2643 augroup END
2644
2645 call assert_fails('au WinNew * ++nested ++nested echo bad', 'E983:')
2646 call assert_fails('au WinNew * nested nested echo bad', 'E983:')
2647endfunc
2648
Bram Moolenaar5fa9f232022-07-23 09:06:48 +01002649func Test_autocmd_nested_cursor_invalid()
2650 set laststatus=0
2651 copen
2652 cclose
2653 call setline(1, ['foo', 'bar', 'baz'])
2654 3
2655 augroup nested_inv
2656 autocmd User foo ++nested copen
2657 autocmd BufAdd * let &laststatus = 2 - &laststatus
2658 augroup END
2659 doautocmd User foo
2660
2661 augroup nested_inv
2662 au!
2663 augroup END
2664 set laststatus&
Bram Moolenaarb03950f2022-07-26 13:47:13 +01002665 cclose
Bram Moolenaar5fa9f232022-07-23 09:06:48 +01002666 bwipe!
2667endfunc
2668
Bram Moolenaar3d6ee8b2022-07-27 15:23:35 +01002669func Test_autocmd_nested_keeps_cursor_pos()
2670 enew
2671 call setline(1, 'foo')
2672 autocmd User foo ++nested normal! $a
2673 autocmd InsertLeave * :
2674 doautocmd User foo
2675 call assert_equal([0, 1, 3, 0], getpos('.'))
2676
2677 bwipe!
2678endfunc
2679
Bram Moolenaarb03950f2022-07-26 13:47:13 +01002680func Test_autocmd_nested_switch_window()
2681 " run this in a separate Vim so that SafeState works
2682 CheckRunVimInTerminal
2683
2684 let lines =<< trim END
2685 vim9script
2686 ['()']->writefile('Xautofile')
2687 autocmd VimEnter * ++nested edit Xautofile | split
2688 autocmd BufReadPost * autocmd SafeState * ++once foldclosed('.')
2689 autocmd WinEnter * matchadd('ErrorMsg', 'pat')
2690 END
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01002691 call writefile(lines, 'Xautoscript', 'D')
Bram Moolenaarb03950f2022-07-26 13:47:13 +01002692 let buf = RunVimInTerminal('-S Xautoscript', {'rows': 10})
2693 call VerifyScreenDump(buf, 'Test_autocmd_nested_switch', {})
2694
2695 call StopVimInTerminal(buf)
2696 call delete('Xautofile')
Bram Moolenaarb03950f2022-07-26 13:47:13 +01002697endfunc
2698
Bram Moolenaareb93f3f2019-04-04 15:04:56 +02002699func Test_autocmd_once()
2700 " Without ++once WinNew triggers twice
2701 let g:did_split = 0
2702 augroup Testing
2703 au WinNew * let g:did_split += 1
2704 augroup END
2705 split
2706 split
2707 call assert_equal(2, g:did_split)
2708 call assert_true(exists('#WinNew'))
2709 close
2710 close
2711
2712 " With ++once WinNew triggers once
2713 let g:did_split = 0
2714 augroup Testing
2715 au!
2716 au WinNew * ++once let g:did_split += 1
2717 augroup END
2718 split
2719 split
2720 call assert_equal(1, g:did_split)
2721 call assert_false(exists('#WinNew'))
2722 close
2723 close
2724
2725 call assert_fails('au WinNew * ++once ++once echo bad', 'E983:')
2726endfunc
2727
Bram Moolenaara68e5952019-04-25 22:22:01 +02002728func Test_autocmd_bufreadpre()
2729 new
2730 let b:bufreadpre = 1
Bram Moolenaarab505b12020-03-23 19:28:44 +01002731 call append(0, range(1000))
Bram Moolenaara68e5952019-04-25 22:22:01 +02002732 w! XAutocmdBufReadPre.txt
2733 autocmd BufReadPre <buffer> :let b:bufreadpre += 1
Bram Moolenaarab505b12020-03-23 19:28:44 +01002734 norm! 500gg
Bram Moolenaara68e5952019-04-25 22:22:01 +02002735 sp
Bram Moolenaarab505b12020-03-23 19:28:44 +01002736 norm! 1000gg
Bram Moolenaara68e5952019-04-25 22:22:01 +02002737 wincmd p
2738 let g:wsv1 = winsaveview()
2739 wincmd p
2740 let g:wsv2 = winsaveview()
2741 " triggers BufReadPre, should not move the cursor in either window
2742 " The topline may change one line in a large window.
2743 edit
2744 call assert_inrange(g:wsv2.topline - 1, g:wsv2.topline + 1, winsaveview().topline)
2745 call assert_equal(g:wsv2.lnum, winsaveview().lnum)
2746 call assert_equal(2, b:bufreadpre)
2747 wincmd p
2748 call assert_equal(g:wsv1.topline, winsaveview().topline)
2749 call assert_equal(g:wsv1.lnum, winsaveview().lnum)
2750 call assert_equal(2, b:bufreadpre)
2751 " Now set the cursor position in an BufReadPre autocommand
2752 " (even though the position will be invalid, this should make Vim reset the
2753 " cursor position in the other window.
2754 wincmd p
2755 set cpo+=g
2756 " won't do anything, but try to set the cursor on an invalid lnum
2757 autocmd BufReadPre <buffer> :norm! 70gg
2758 " triggers BufReadPre, should not move the cursor in either window
2759 e
2760 call assert_equal(1, winsaveview().topline)
2761 call assert_equal(1, winsaveview().lnum)
2762 call assert_equal(3, b:bufreadpre)
2763 wincmd p
2764 call assert_equal(g:wsv1.topline, winsaveview().topline)
2765 call assert_equal(g:wsv1.lnum, winsaveview().lnum)
2766 call assert_equal(3, b:bufreadpre)
2767 close
2768 close
2769 call delete('XAutocmdBufReadPre.txt')
2770 set cpo-=g
2771endfunc
2772
Bram Moolenaar5e66b422019-01-24 21:58:10 +01002773" FileChangedShell tested in test_filechanged.vim
Bram Moolenaar69ea5872019-04-25 20:29:00 +02002774
2775" Tests for the following autocommands:
2776" - FileWritePre writing a compressed file
2777" - FileReadPost reading a compressed file
2778" - BufNewFile reading a file template
2779" - BufReadPre decompressing the file to be read
2780" - FilterReadPre substituting characters in the temp file
2781" - FilterReadPost substituting characters after filtering
2782" - FileReadPre set options for decompression
2783" - FileReadPost decompress the file
2784func Test_ReadWrite_Autocmds()
2785 " Run this test only on Unix-like systems and if gzip is available
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +02002786 CheckUnix
2787 CheckExecutable gzip
Bram Moolenaar69ea5872019-04-25 20:29:00 +02002788
2789 " Make $GZIP empty, "-v" would cause trouble.
2790 let $GZIP = ""
2791
2792 " Use a FileChangedShell autocommand to avoid a prompt for 'Xtestfile.gz'
2793 " being modified outside of Vim (noticed on Solaris).
2794 au FileChangedShell * echo 'caught FileChangedShell'
2795
2796 " Test for the FileReadPost, FileWritePre and FileWritePost autocmds
2797 augroup Test1
2798 au!
2799 au FileWritePre *.gz '[,']!gzip
2800 au FileWritePost *.gz undo
2801 au FileReadPost *.gz '[,']!gzip -d
2802 augroup END
2803
2804 new
2805 set bin
2806 call append(0, [
2807 \ 'line 2 Abcdefghijklmnopqrstuvwxyz',
2808 \ 'line 3 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2809 \ 'line 4 Abcdefghijklmnopqrstuvwxyz',
2810 \ 'line 5 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2811 \ 'line 6 Abcdefghijklmnopqrstuvwxyz',
2812 \ 'line 7 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2813 \ 'line 8 Abcdefghijklmnopqrstuvwxyz',
2814 \ 'line 9 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2815 \ 'line 10 Abcdefghijklmnopqrstuvwxyz'
2816 \ ])
2817 1,9write! Xtestfile.gz
2818 enew! | close
2819
2820 new
2821 " Read and decompress the testfile
2822 0read Xtestfile.gz
2823 call assert_equal([
2824 \ 'line 2 Abcdefghijklmnopqrstuvwxyz',
2825 \ 'line 3 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2826 \ 'line 4 Abcdefghijklmnopqrstuvwxyz',
2827 \ 'line 5 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2828 \ 'line 6 Abcdefghijklmnopqrstuvwxyz',
2829 \ 'line 7 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2830 \ 'line 8 Abcdefghijklmnopqrstuvwxyz',
2831 \ 'line 9 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2832 \ 'line 10 Abcdefghijklmnopqrstuvwxyz'
2833 \ ], getline(1, 9))
2834 enew! | close
2835
2836 augroup Test1
2837 au!
2838 augroup END
2839
2840 " Test for the FileAppendPre and FileAppendPost autocmds
2841 augroup Test2
2842 au!
2843 au BufNewFile *.c read Xtest.c
2844 au FileAppendPre *.out '[,']s/new/NEW/
2845 au FileAppendPost *.out !cat Xtest.c >> test.out
2846 augroup END
2847
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01002848 call writefile(['/*', ' * Here is a new .c file', ' */'], 'Xtest.c', 'D')
Bram Moolenaar69ea5872019-04-25 20:29:00 +02002849 new foo.c " should load Xtest.c
2850 call assert_equal(['/*', ' * Here is a new .c file', ' */'], getline(2, 4))
2851 w! >> test.out " append it to the output file
2852
2853 let contents = readfile('test.out')
2854 call assert_equal(' * Here is a NEW .c file', contents[2])
2855 call assert_equal(' * Here is a new .c file', contents[5])
2856
2857 call delete('test.out')
2858 enew! | close
2859 augroup Test2
2860 au!
2861 augroup END
2862
2863 " Test for the BufReadPre and BufReadPost autocmds
2864 augroup Test3
2865 au!
2866 " setup autocommands to decompress before reading and re-compress
2867 " afterwards
2868 au BufReadPre *.gz exe '!gzip -d ' . shellescape(expand("<afile>"))
2869 au BufReadPre *.gz call rename(expand("<afile>:r"), expand("<afile>"))
2870 au BufReadPost *.gz call rename(expand("<afile>"), expand("<afile>:r"))
2871 au BufReadPost *.gz exe '!gzip ' . shellescape(expand("<afile>:r"))
2872 augroup END
2873
2874 e! Xtestfile.gz " Edit compressed file
2875 call assert_equal([
2876 \ 'line 2 Abcdefghijklmnopqrstuvwxyz',
2877 \ 'line 3 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2878 \ 'line 4 Abcdefghijklmnopqrstuvwxyz',
2879 \ 'line 5 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2880 \ 'line 6 Abcdefghijklmnopqrstuvwxyz',
2881 \ 'line 7 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2882 \ 'line 8 Abcdefghijklmnopqrstuvwxyz',
2883 \ 'line 9 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2884 \ 'line 10 Abcdefghijklmnopqrstuvwxyz'
2885 \ ], getline(1, 9))
2886
2887 w! >> test.out " Append it to the output file
2888
2889 augroup Test3
2890 au!
2891 augroup END
2892
2893 " Test for the FilterReadPre and FilterReadPost autocmds.
2894 set shelltemp " need temp files here
2895 augroup Test4
2896 au!
2897 au FilterReadPre *.out call rename(expand("<afile>"), expand("<afile>") . ".t")
2898 au FilterReadPre *.out exe 'silent !sed s/e/E/ ' . shellescape(expand("<afile>")) . ".t >" . shellescape(expand("<afile>"))
2899 au FilterReadPre *.out exe 'silent !rm ' . shellescape(expand("<afile>")) . '.t'
2900 au FilterReadPost *.out '[,']s/x/X/g
2901 augroup END
2902
2903 e! test.out " Edit the output file
2904 1,$!cat
2905 call assert_equal([
2906 \ 'linE 2 AbcdefghijklmnopqrstuvwXyz',
2907 \ 'linE 3 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
2908 \ 'linE 4 AbcdefghijklmnopqrstuvwXyz',
2909 \ 'linE 5 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
2910 \ 'linE 6 AbcdefghijklmnopqrstuvwXyz',
2911 \ 'linE 7 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
2912 \ 'linE 8 AbcdefghijklmnopqrstuvwXyz',
2913 \ 'linE 9 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
2914 \ 'linE 10 AbcdefghijklmnopqrstuvwXyz'
2915 \ ], getline(1, 9))
2916 call assert_equal([
2917 \ 'line 2 Abcdefghijklmnopqrstuvwxyz',
2918 \ 'line 3 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2919 \ 'line 4 Abcdefghijklmnopqrstuvwxyz',
2920 \ 'line 5 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2921 \ 'line 6 Abcdefghijklmnopqrstuvwxyz',
2922 \ 'line 7 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2923 \ 'line 8 Abcdefghijklmnopqrstuvwxyz',
2924 \ 'line 9 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2925 \ 'line 10 Abcdefghijklmnopqrstuvwxyz'
2926 \ ], readfile('test.out'))
2927
2928 augroup Test4
2929 au!
2930 augroup END
2931 set shelltemp&vim
2932
2933 " Test for the FileReadPre and FileReadPost autocmds.
2934 augroup Test5
2935 au!
2936 au FileReadPre *.gz exe 'silent !gzip -d ' . shellescape(expand("<afile>"))
2937 au FileReadPre *.gz call rename(expand("<afile>:r"), expand("<afile>"))
2938 au FileReadPost *.gz '[,']s/l/L/
2939 augroup END
2940
2941 new
2942 0r Xtestfile.gz " Read compressed file
2943 call assert_equal([
2944 \ 'Line 2 Abcdefghijklmnopqrstuvwxyz',
2945 \ 'Line 3 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2946 \ 'Line 4 Abcdefghijklmnopqrstuvwxyz',
2947 \ 'Line 5 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2948 \ 'Line 6 Abcdefghijklmnopqrstuvwxyz',
2949 \ 'Line 7 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2950 \ 'Line 8 Abcdefghijklmnopqrstuvwxyz',
2951 \ 'Line 9 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2952 \ 'Line 10 Abcdefghijklmnopqrstuvwxyz'
2953 \ ], getline(1, 9))
2954 call assert_equal([
2955 \ 'line 2 Abcdefghijklmnopqrstuvwxyz',
2956 \ 'line 3 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2957 \ 'line 4 Abcdefghijklmnopqrstuvwxyz',
2958 \ 'line 5 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2959 \ 'line 6 Abcdefghijklmnopqrstuvwxyz',
2960 \ 'line 7 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2961 \ 'line 8 Abcdefghijklmnopqrstuvwxyz',
2962 \ 'line 9 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2963 \ 'line 10 Abcdefghijklmnopqrstuvwxyz'
2964 \ ], readfile('Xtestfile.gz'))
2965
2966 augroup Test5
2967 au!
2968 augroup END
2969
2970 au! FileChangedShell
2971 call delete('Xtestfile.gz')
Bram Moolenaar69ea5872019-04-25 20:29:00 +02002972 call delete('test.out')
2973endfunc
Bram Moolenaar23b51392019-05-09 21:38:43 +02002974
2975func Test_throw_in_BufWritePre()
2976 new
2977 call setline(1, ['one', 'two', 'three'])
2978 call assert_false(filereadable('Xthefile'))
2979 augroup throwing
2980 au BufWritePre X* throw 'do not write'
2981 augroup END
2982 try
2983 w Xthefile
2984 catch
2985 let caught = 1
2986 endtry
2987 call assert_equal(1, caught)
2988 call assert_false(filereadable('Xthefile'))
2989
2990 bwipe!
2991 au! throwing
2992endfunc
Bram Moolenaarcadbe1b2019-09-22 21:50:09 +02002993
Bram Moolenaar40fa12a2021-09-22 14:18:13 +02002994func Test_autocmd_in_try_block()
Bram Moolenaar6f14da12022-09-07 21:30:44 +01002995 call mkdir('Xintrydir', 'R')
Bram Moolenaar40fa12a2021-09-22 14:18:13 +02002996 au BufEnter * let g:fname = expand('%')
2997 try
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +01002998 edit Xintrydir/
Bram Moolenaar40fa12a2021-09-22 14:18:13 +02002999 endtry
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +01003000 call assert_match('Xintrydir', g:fname)
Bram Moolenaar40fa12a2021-09-22 14:18:13 +02003001
3002 unlet g:fname
3003 au! BufEnter
Bram Moolenaar40fa12a2021-09-22 14:18:13 +02003004endfunc
3005
Bram Moolenaarcadbe1b2019-09-22 21:50:09 +02003006func Test_autocmd_SafeState()
3007 CheckRunVimInTerminal
3008
3009 let lines =<< trim END
3010 let g:safe = 0
3011 let g:again = ''
3012 au SafeState * let g:safe += 1
3013 au SafeStateAgain * let g:again ..= 'x'
3014 func CallTimer()
3015 call timer_start(10, {id -> execute('let g:again ..= "t"')})
3016 endfunc
3017 END
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01003018 call writefile(lines, 'XSafeState', 'D')
Bram Moolenaarcadbe1b2019-09-22 21:50:09 +02003019 let buf = RunVimInTerminal('-S XSafeState', #{rows: 6})
3020
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01003021 " Sometimes we loop to handle a K_IGNORE, SafeState may be triggered once or
Bram Moolenaar8fb1b472020-02-23 16:16:26 +01003022 " more often.
Bram Moolenaarcadbe1b2019-09-22 21:50:09 +02003023 call term_sendkeys(buf, ":echo g:safe\<CR>")
Bram Moolenaar8fb1b472020-02-23 16:16:26 +01003024 call WaitForAssert({-> assert_match('^\d ', term_getline(buf, 6))}, 1000)
Bram Moolenaarcadbe1b2019-09-22 21:50:09 +02003025
Bram Moolenaar8fb1b472020-02-23 16:16:26 +01003026 " SafeStateAgain should be invoked at least three times
Bram Moolenaarcadbe1b2019-09-22 21:50:09 +02003027 call term_sendkeys(buf, ":echo g:again\<CR>")
Bram Moolenaar8fb1b472020-02-23 16:16:26 +01003028 call WaitForAssert({-> assert_match('^xxx', term_getline(buf, 6))}, 1000)
Bram Moolenaarcadbe1b2019-09-22 21:50:09 +02003029
3030 call term_sendkeys(buf, ":let g:again = ''\<CR>:call CallTimer()\<CR>")
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02003031 call TermWait(buf, 50)
Bram Moolenaar0f6629a2019-09-22 23:24:13 +02003032 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02003033 call TermWait(buf, 50)
Bram Moolenaarcadbe1b2019-09-22 21:50:09 +02003034 call term_sendkeys(buf, ":echo g:again\<CR>")
3035 call WaitForAssert({-> assert_match('xtx', term_getline(buf, 6))}, 1000)
3036
3037 call StopVimInTerminal(buf)
Bram Moolenaarcadbe1b2019-09-22 21:50:09 +02003038endfunc
Bram Moolenaar23324a02019-10-01 17:39:04 +02003039
3040func Test_autocmd_CmdWinEnter()
3041 CheckRunVimInTerminal
Bram Moolenaar21829c52021-01-26 22:42:21 +01003042
Bram Moolenaar23324a02019-10-01 17:39:04 +02003043 let lines =<< trim END
Egor Zvorykin125ffd22021-11-17 14:01:14 +00003044 augroup vimHints | au! | augroup END
Bram Moolenaar23324a02019-10-01 17:39:04 +02003045 let b:dummy_var = 'This is a dummy'
3046 autocmd CmdWinEnter * quit
3047 let winnr = winnr('$')
3048 END
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01003049 let filename = 'XCmdWinEnter'
Bram Moolenaar23324a02019-10-01 17:39:04 +02003050 call writefile(lines, filename)
3051 let buf = RunVimInTerminal('-S '.filename, #{rows: 6})
3052
3053 call term_sendkeys(buf, "q:")
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02003054 call TermWait(buf)
Bram Moolenaar23324a02019-10-01 17:39:04 +02003055 call term_sendkeys(buf, ":echo b:dummy_var\<cr>")
Bram Moolenaar353c3512020-03-15 14:19:26 +01003056 call WaitForAssert({-> assert_match('^This is a dummy', term_getline(buf, 6))}, 2000)
Bram Moolenaar23324a02019-10-01 17:39:04 +02003057 call term_sendkeys(buf, ":echo &buftype\<cr>")
3058 call WaitForAssert({-> assert_notmatch('^nofile', term_getline(buf, 6))}, 1000)
3059 call term_sendkeys(buf, ":echo winnr\<cr>")
3060 call WaitForAssert({-> assert_match('^1', term_getline(buf, 6))}, 1000)
3061
3062 " clean up
3063 call StopVimInTerminal(buf)
3064 call delete(filename)
3065endfunc
Bram Moolenaarec66c412019-10-11 21:19:13 +02003066
3067func Test_autocmd_was_using_freed_memory()
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01003068 CheckFeature quickfix
3069
Bram Moolenaarec66c412019-10-11 21:19:13 +02003070 pedit xx
3071 n x
Bram Moolenaar92bb83e2021-02-03 23:04:46 +01003072 augroup winenter
3073 au WinEnter * if winnr('$') > 2 | quit | endif
3074 augroup END
Bram Moolenaarec66c412019-10-11 21:19:13 +02003075 split
Bram Moolenaar92bb83e2021-02-03 23:04:46 +01003076
3077 augroup winenter
3078 au! WinEnter
3079 augroup END
3080
3081 bwipe xx
3082 bwipe x
3083 pclose
Bram Moolenaarec66c412019-10-11 21:19:13 +02003084endfunc
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +01003085
3086func Test_BufWrite_lockmarks()
Bram Moolenaarf08b0eb2021-10-16 13:00:14 +01003087 let g:test_is_flaky = 1
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +01003088 edit! Xtest
3089 call setline(1, ['a', 'b', 'c', 'd'])
3090
3091 " :lockmarks preserves the marks
3092 call SetChangeMarks(2, 3)
3093 lockmarks write
3094 call assert_equal([2, 3], [line("'["), line("']")])
3095
3096 " *WritePre autocmds get the correct line range, but lockmarks preserves the
3097 " original values for the user
3098 augroup lockmarks
3099 au!
3100 au BufWritePre,FilterWritePre * call assert_equal([1, 4], [line("'["), line("']")])
3101 au FileWritePre * call assert_equal([3, 4], [line("'["), line("']")])
3102 augroup END
3103
3104 lockmarks write
3105 call assert_equal([2, 3], [line("'["), line("']")])
3106
3107 if executable('cat')
3108 lockmarks %!cat
3109 call assert_equal([2, 3], [line("'["), line("']")])
3110 endif
3111
3112 lockmarks 3,4write Xtest2
3113 call assert_equal([2, 3], [line("'["), line("']")])
3114
3115 au! lockmarks
3116 augroup! lockmarks
3117 call delete('Xtest')
3118 call delete('Xtest2')
3119endfunc
Bram Moolenaarce6db022020-01-07 20:11:42 +01003120
3121func Test_FileType_spell()
3122 if !isdirectory('/tmp')
3123 throw "Skipped: requires /tmp directory"
3124 endif
3125
3126 " this was crashing with an invalid free()
3127 setglobal spellfile=/tmp/en.utf-8.add
3128 augroup crash
3129 autocmd!
3130 autocmd BufNewFile,BufReadPost crashfile setf somefiletype
3131 autocmd BufNewFile,BufReadPost crashfile set ft=anotherfiletype
3132 autocmd FileType anotherfiletype setlocal spell
3133 augroup END
3134 func! NoCrash() abort
3135 edit /tmp/crashfile
3136 endfunc
3137 call NoCrash()
3138
3139 au! crash
3140 setglobal spellfile=
3141endfunc
Bram Moolenaarbc2b71d2020-02-17 21:33:30 +01003142
Bram Moolenaaref976322022-09-28 11:48:30 +01003143" this was wiping out the current buffer and using freed memory
3144func Test_SpellFileMissing_bwipe()
3145 next 0
3146 au SpellFileMissing 0 bwipe
3147 call assert_fails('set spell spelllang=0', 'E937:')
3148
3149 au! SpellFileMissing
Bram Moolenaar0a60f792022-11-19 21:18:11 +00003150 set nospell spelllang=en
Bram Moolenaaref976322022-09-28 11:48:30 +01003151 bwipe
3152endfunc
3153
Bram Moolenaar406cd902020-02-18 21:54:41 +01003154" Test closing a window or editing another buffer from a FileChangedRO handler
3155" in a readonly buffer
3156func Test_FileChangedRO_winclose()
Bram Moolenaar62cd26a2020-10-11 20:08:44 +02003157 call test_override('ui_delay', 10)
3158
Bram Moolenaar406cd902020-02-18 21:54:41 +01003159 augroup FileChangedROTest
3160 au!
3161 autocmd FileChangedRO * quit
3162 augroup END
3163 new
3164 set readonly
3165 call assert_fails('normal i', 'E788:')
3166 close
3167 augroup! FileChangedROTest
3168
3169 augroup FileChangedROTest
3170 au!
Bram Moolenaar61abe7d2022-08-30 21:46:08 +01003171 autocmd FileChangedRO * edit Xrofile
Bram Moolenaar406cd902020-02-18 21:54:41 +01003172 augroup END
3173 new
3174 set readonly
3175 call assert_fails('normal i', 'E788:')
3176 close
3177 augroup! FileChangedROTest
Bram Moolenaar62cd26a2020-10-11 20:08:44 +02003178 call test_override('ALL', 0)
Bram Moolenaar406cd902020-02-18 21:54:41 +01003179endfunc
3180
Bram Moolenaar0c81d1b2020-02-22 22:45:55 +01003181func LogACmd()
3182 call add(g:logged, line('$'))
3183endfunc
3184
3185func Test_TermChanged()
Bram Moolenaard28e0b32020-02-22 23:08:52 +01003186 CheckNotGui
3187
Bram Moolenaar0c81d1b2020-02-22 22:45:55 +01003188 enew!
3189 tabnew
3190 call setline(1, ['a', 'b', 'c', 'd'])
3191 $
3192 au TermChanged * call LogACmd()
3193 let g:logged = []
3194 let term_save = &term
3195 set term=xterm
3196 call assert_equal([1, 4], g:logged)
3197
3198 au! TermChanged
3199 let &term = term_save
3200 bwipe!
3201endfunc
3202
Bram Moolenaare3284872020-03-19 13:55:03 +01003203" Test for FileReadCmd autocmd
3204func Test_autocmd_FileReadCmd()
3205 func ReadFileCmd()
3206 call append(line('$'), "v:cmdarg = " .. v:cmdarg)
3207 endfunc
3208 augroup FileReadCmdTest
3209 au!
3210 au FileReadCmd Xtest call ReadFileCmd()
3211 augroup END
3212
3213 new
3214 read ++bin Xtest
3215 read ++nobin Xtest
3216 read ++edit Xtest
3217 read ++bad=keep Xtest
3218 read ++bad=drop Xtest
3219 read ++bad=- Xtest
3220 read ++ff=unix Xtest
3221 read ++ff=dos Xtest
3222 read ++ff=mac Xtest
3223 read ++enc=utf-8 Xtest
3224
3225 call assert_equal(['',
3226 \ 'v:cmdarg = ++bin',
3227 \ 'v:cmdarg = ++nobin',
3228 \ 'v:cmdarg = ++edit',
3229 \ 'v:cmdarg = ++bad=keep',
3230 \ 'v:cmdarg = ++bad=drop',
3231 \ 'v:cmdarg = ++bad=-',
3232 \ 'v:cmdarg = ++ff=unix',
3233 \ 'v:cmdarg = ++ff=dos',
3234 \ 'v:cmdarg = ++ff=mac',
3235 \ 'v:cmdarg = ++enc=utf-8'], getline(1, '$'))
3236
Bram Moolenaar23526d22022-12-05 15:50:41 +00003237 bwipe!
Bram Moolenaare3284872020-03-19 13:55:03 +01003238 augroup FileReadCmdTest
3239 au!
3240 augroup END
3241 delfunc ReadFileCmd
3242endfunc
3243
Bram Moolenaaree4e0c12020-04-06 21:35:05 +02003244" Test for passing invalid arguments to autocmd
3245func Test_autocmd_invalid_args()
3246 " Additional character after * for event
Bram Moolenaar61abe7d2022-08-30 21:46:08 +01003247 call assert_fails('autocmd *a Xinvfile set ff=unix', 'E215:')
Bram Moolenaaree4e0c12020-04-06 21:35:05 +02003248 augroup Test
3249 augroup END
3250 " Invalid autocmd event
Bram Moolenaar61abe7d2022-08-30 21:46:08 +01003251 call assert_fails('autocmd Bufabc Xinvfile set ft=vim', 'E216:')
Bram Moolenaaree4e0c12020-04-06 21:35:05 +02003252 " Invalid autocmd event in a autocmd group
Bram Moolenaar61abe7d2022-08-30 21:46:08 +01003253 call assert_fails('autocmd Test Bufabc Xinvfile set ft=vim', 'E216:')
Bram Moolenaaree4e0c12020-04-06 21:35:05 +02003254 augroup! Test
3255 " Execute all autocmds
3256 call assert_fails('doautocmd * BufEnter', 'E217:')
3257 call assert_fails('augroup! x1a2b3', 'E367:')
3258 call assert_fails('autocmd BufNew <buffer=999> pwd', 'E680:')
Bram Moolenaar531be472020-09-23 22:38:05 +02003259 call assert_fails('autocmd BufNew \) set ff=unix', 'E55:')
Bram Moolenaaree4e0c12020-04-06 21:35:05 +02003260endfunc
3261
3262" Test for deep nesting of autocmds
3263func Test_autocmd_deep_nesting()
Bram Moolenaar61abe7d2022-08-30 21:46:08 +01003264 autocmd BufEnter Xdeepfile doautocmd BufEnter Xdeepfile
3265 call assert_fails('doautocmd BufEnter Xdeepfile', 'E218:')
3266 autocmd! BufEnter Xdeepfile
Bram Moolenaaree4e0c12020-04-06 21:35:05 +02003267endfunc
3268
Bram Moolenaarbe5ee862020-06-10 20:56:58 +02003269" Tests for SigUSR1 autocmd event, which is only available on posix systems.
3270func Test_autocmd_sigusr1()
3271 CheckUnix
Bram Moolenaar0056ca72022-09-23 21:26:39 +01003272 " FIXME: should this work on MacOS M1?
3273 CheckNotMacM1
Bram Moolenaar62cd26a2020-10-11 20:08:44 +02003274 CheckExecutable /bin/kill
Bram Moolenaarbe5ee862020-06-10 20:56:58 +02003275
3276 let g:sigusr1_passed = 0
3277 au SigUSR1 * let g:sigusr1_passed = 1
3278 call system('/bin/kill -s usr1 ' . getpid())
3279 call WaitForAssert({-> assert_true(g:sigusr1_passed)})
3280
3281 au! SigUSR1
3282 unlet g:sigusr1_passed
3283endfunc
3284
Bram Moolenaarb340bae2020-06-15 19:51:56 +02003285" Test for BufReadPre autocmd deleting the file
3286func Test_BufReadPre_delfile()
3287 augroup TestAuCmd
3288 au!
Bram Moolenaare7cda972022-08-29 11:02:59 +01003289 autocmd BufReadPre XbufreadPre call delete('XbufreadPre')
Bram Moolenaarb340bae2020-06-15 19:51:56 +02003290 augroup END
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01003291 call writefile([], 'XbufreadPre', 'D')
Bram Moolenaare7cda972022-08-29 11:02:59 +01003292 call assert_fails('new XbufreadPre', 'E200:')
3293 call assert_equal('XbufreadPre', @%)
Bram Moolenaarb340bae2020-06-15 19:51:56 +02003294 call assert_equal(1, &readonly)
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01003295
Bram Moolenaarb340bae2020-06-15 19:51:56 +02003296 augroup TestAuCmd
3297 au!
3298 augroup END
3299 close!
3300endfunc
3301
3302" Test for BufReadPre autocmd changing the current buffer
3303func Test_BufReadPre_changebuf()
3304 augroup TestAuCmd
3305 au!
Bram Moolenaare7cda972022-08-29 11:02:59 +01003306 autocmd BufReadPre Xchangebuf edit Xsomeotherfile
Bram Moolenaarb340bae2020-06-15 19:51:56 +02003307 augroup END
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01003308 call writefile([], 'Xchangebuf', 'D')
Bram Moolenaare7cda972022-08-29 11:02:59 +01003309 call assert_fails('new Xchangebuf', 'E201:')
Bram Moolenaarb340bae2020-06-15 19:51:56 +02003310 call assert_equal('Xsomeotherfile', @%)
3311 call assert_equal(1, &readonly)
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01003312
Bram Moolenaarb340bae2020-06-15 19:51:56 +02003313 augroup TestAuCmd
3314 au!
3315 augroup END
3316 close!
3317endfunc
3318
3319" Test for BufWipeouti autocmd changing the current buffer when reading a file
3320" in an empty buffer with 'f' flag in 'cpo'
3321func Test_BufDelete_changebuf()
3322 new
3323 augroup TestAuCmd
3324 au!
3325 autocmd BufWipeout * let bufnr = bufadd('somefile') | exe "b " .. bufnr
3326 augroup END
3327 let save_cpo = &cpo
3328 set cpo+=f
Bram Moolenaarb18b4962022-09-02 21:55:50 +01003329 call assert_fails('r Xchangebuf', ['E812:', 'E484:'])
Bram Moolenaarb340bae2020-06-15 19:51:56 +02003330 call assert_equal('somefile', @%)
3331 let &cpo = save_cpo
3332 augroup TestAuCmd
3333 au!
3334 augroup END
3335 close!
3336endfunc
3337
Bram Moolenaar0fe937f2020-06-16 22:42:04 +02003338" Test for the temporary internal window used to execute autocmds
3339func Test_autocmd_window()
3340 %bw!
3341 edit one.txt
3342 tabnew two.txt
Bram Moolenaar41cd8032021-03-13 15:47:56 +01003343 vnew three.txt
3344 tabnew four.txt
3345 tabprevious
Bram Moolenaar0fe937f2020-06-16 22:42:04 +02003346 let g:blist = []
Bram Moolenaar832adf92020-06-25 19:01:36 +02003347 augroup aucmd_win_test1
Bram Moolenaar0fe937f2020-06-16 22:42:04 +02003348 au!
3349 au BufEnter * call add(g:blist, [expand('<afile>'),
3350 \ win_gettype(bufwinnr(expand('<afile>')))])
3351 augroup END
3352
3353 doautoall BufEnter
Bram Moolenaar41cd8032021-03-13 15:47:56 +01003354 call assert_equal([
3355 \ ['one.txt', 'autocmd'],
3356 \ ['two.txt', ''],
3357 \ ['four.txt', 'autocmd'],
3358 \ ['three.txt', ''],
3359 \ ], g:blist)
Bram Moolenaar0fe937f2020-06-16 22:42:04 +02003360
Bram Moolenaar832adf92020-06-25 19:01:36 +02003361 augroup aucmd_win_test1
Bram Moolenaar0fe937f2020-06-16 22:42:04 +02003362 au!
3363 augroup END
Bram Moolenaar832adf92020-06-25 19:01:36 +02003364 augroup! aucmd_win_test1
3365 %bw!
3366endfunc
3367
3368" Test for trying to close the temporary window used for executing an autocmd
3369func Test_close_autocmd_window()
3370 %bw!
3371 edit one.txt
3372 tabnew two.txt
3373 augroup aucmd_win_test2
3374 au!
3375 au BufEnter * if expand('<afile>') == 'one.txt' | 1close | endif
3376 augroup END
3377
3378 call assert_fails('doautoall BufEnter', 'E813:')
3379
3380 augroup aucmd_win_test2
3381 au!
3382 augroup END
3383 augroup! aucmd_win_test2
Bram Moolenaarcf844172020-06-26 19:44:06 +02003384 %bwipe!
3385endfunc
3386
3387" Test for trying to close the tab that has the temporary window for exeucing
3388" an autocmd.
3389func Test_close_autocmd_tab()
3390 edit one.txt
3391 tabnew two.txt
3392 augroup aucmd_win_test
3393 au!
3394 au BufEnter * if expand('<afile>') == 'one.txt' | tabfirst | tabonly | endif
3395 augroup END
3396
3397 call assert_fails('doautoall BufEnter', 'E813:')
3398
3399 tabonly
3400 augroup aucmd_win_test
3401 au!
3402 augroup END
3403 augroup! aucmd_win_test
3404 %bwipe!
Bram Moolenaar0fe937f2020-06-16 22:42:04 +02003405endfunc
3406
Bram Moolenaarcb1956d2022-01-07 15:45:18 +00003407func Test_Visual_doautoall_redraw()
3408 call setline(1, ['a', 'b'])
Bram Moolenaar94722c52023-01-28 19:19:03 +00003409 new
Bram Moolenaarcb1956d2022-01-07 15:45:18 +00003410 wincmd p
3411 call feedkeys("G\<C-V>", 'txn')
3412 autocmd User Explode ++once redraw
3413 doautoall User Explode
3414 %bwipe!
3415endfunc
3416
Bram Moolenaar6bcb8772021-02-03 21:23:29 +01003417" This was using freed memory.
3418func Test_BufNew_arglocal()
3419 arglocal
3420 au BufNew * arglocal
3421 call assert_fails('drop xx', 'E1156:')
3422
3423 au! BufNew
3424endfunc
3425
Bram Moolenaar8ab37572021-02-03 21:56:59 +01003426func Test_autocmd_closes_window()
3427 au BufNew,BufWinLeave * e %e
3428 file yyy
3429 au BufNew,BufWinLeave * ball
Bram Moolenaaraad5f9d2021-02-06 17:30:31 +01003430 n xxx
Bram Moolenaar8ab37572021-02-03 21:56:59 +01003431
Bram Moolenaaraad5f9d2021-02-06 17:30:31 +01003432 %bwipe
Bram Moolenaar8ab37572021-02-03 21:56:59 +01003433 au! BufNew
3434 au! BufWinLeave
3435endfunc
3436
Bram Moolenaar92bb83e2021-02-03 23:04:46 +01003437func Test_autocmd_quit_psearch()
3438 sn aa bb
3439 augroup aucmd_win_test
3440 au!
3441 au BufEnter,BufLeave,BufNew,WinEnter,WinLeave,WinNew * if winnr('$') > 1 | q | endif
3442 augroup END
3443 ps /
3444
3445 augroup aucmd_win_test
3446 au!
3447 augroup END
zeertzjq7851c692022-04-21 11:14:01 +01003448 new
3449 pclose
Bram Moolenaar92bb83e2021-02-03 23:04:46 +01003450endfunc
3451
Bram Moolenaaraad5f9d2021-02-06 17:30:31 +01003452" Fuzzer found some strange combination that caused a crash.
3453func Test_autocmd_normal_mess()
Bram Moolenaardd07c022021-02-07 13:32:46 +01003454 " For unknown reason this hangs on MS-Windows
3455 CheckNotMSWindows
3456
Bram Moolenaaraad5f9d2021-02-06 17:30:31 +01003457 augroup aucmd_normal_test
3458 au BufLeave,BufWinLeave,BufHidden,BufUnload,BufDelete,BufWipeout * norm 7q/qc
3459 augroup END
Bram Moolenaar983d83f2021-02-07 12:12:43 +01003460 call assert_fails('o4', 'E1159')
Bram Moolenaaraad5f9d2021-02-06 17:30:31 +01003461 silent! H
Bram Moolenaar983d83f2021-02-07 12:12:43 +01003462 call assert_fails('e xx', 'E1159')
Bram Moolenaaraad5f9d2021-02-06 17:30:31 +01003463 normal G
3464
3465 augroup aucmd_normal_test
3466 au!
3467 augroup END
3468endfunc
3469
Bram Moolenaar8c6951f2021-02-06 18:08:45 +01003470func Test_autocmd_closing_cmdwin()
Bram Moolenaardd07c022021-02-07 13:32:46 +01003471 " For unknown reason this hangs on MS-Windows
3472 CheckNotMSWindows
3473
Bram Moolenaar8c6951f2021-02-06 18:08:45 +01003474 au BufWinLeave * nested q
3475 call assert_fails("norm 7q?\n", 'E855:')
3476
3477 au! BufWinLeave
3478 new
3479 only
3480endfunc
3481
Bram Moolenaar2c7080b2021-02-06 19:19:42 +01003482func Test_autocmd_vimgrep()
3483 augroup aucmd_vimgrep
Charlie Grovesfef44852022-04-19 16:24:12 +01003484 au QuickfixCmdPre,BufNew,BufReadCmd * sb
zeertzjq7851c692022-04-21 11:14:01 +01003485 au QuickfixCmdPre,BufNew,BufReadCmd * q9
Bram Moolenaar2c7080b2021-02-06 19:19:42 +01003486 augroup END
Bram Moolenaardd07c022021-02-07 13:32:46 +01003487 call assert_fails('lv ?a? foo', 'E926:')
Bram Moolenaar2c7080b2021-02-06 19:19:42 +01003488
3489 augroup aucmd_vimgrep
3490 au!
3491 augroup END
3492endfunc
3493
Bram Moolenaar73b8b0a2021-08-01 14:52:32 +02003494func Test_autocmd_with_block()
3495 augroup block_testing
3496 au BufReadPost *.xml {
3497 setlocal matchpairs+=<:>
3498 /<start
3499 }
Bram Moolenaar63b91732021-08-05 20:40:03 +02003500 au CursorHold * {
3501 autocmd BufReadPre * ++once echo 'one' | echo 'two'
3502 g:gotSafeState = 77
3503 }
Bram Moolenaar73b8b0a2021-08-01 14:52:32 +02003504 augroup END
3505
3506 let expected = "\n--- Autocommands ---\nblock_testing BufRead\n *.xml {^@ setlocal matchpairs+=<:>^@ /<start^@ }"
3507 call assert_equal(expected, execute('au BufReadPost *.xml'))
3508
Bram Moolenaar63b91732021-08-05 20:40:03 +02003509 doautocmd CursorHold
3510 call assert_equal(77, g:gotSafeState)
3511 unlet g:gotSafeState
3512
Bram Moolenaar73b8b0a2021-08-01 14:52:32 +02003513 augroup block_testing
3514 au!
Bram Moolenaar75ebd2a2022-06-03 17:39:46 +01003515 autocmd CursorHold * {
3516 if true
3517 # comment
3518 && true
3519
3520 && true
3521 g:done = 'yes'
3522 endif
3523 }
3524 augroup END
3525 doautocmd CursorHold
3526 call assert_equal('yes', g:done)
3527
3528 unlet g:done
3529 augroup block_testing
3530 au!
Bram Moolenaar73b8b0a2021-08-01 14:52:32 +02003531 augroup END
3532endfunc
3533
Christian Brabandtdb3b4462021-10-16 11:58:55 +01003534" Test TextChangedI and TextChanged
3535func Test_Changed_ChangedI()
3536 new
3537 call test_override("char_avail", 1)
3538 let [g:autocmd_i, g:autocmd_n] = ['','']
3539
3540 func! TextChangedAutocmdI(char)
3541 let g:autocmd_{tolower(a:char)} = a:char .. b:changedtick
3542 endfunc
3543
3544 augroup Test_TextChanged
3545 au!
3546 au TextChanged <buffer> :call TextChangedAutocmdI('N')
3547 au TextChangedI <buffer> :call TextChangedAutocmdI('I')
3548 augroup END
3549
3550 call feedkeys("ifoo\<esc>", 'tnix')
3551 " TODO: Test test does not seem to trigger TextChanged autocommand, this
3552 " requires running Vim in a terminal window.
3553 " call assert_equal('N3', g:autocmd_n)
3554 call assert_equal('I3', g:autocmd_i)
3555
3556 call feedkeys("yyp", 'tnix')
3557 " TODO: Test test does not seem to trigger TextChanged autocommand.
3558 " call assert_equal('N4', g:autocmd_n)
3559 call assert_equal('I3', g:autocmd_i)
3560
3561 " CleanUp
3562 call test_override("char_avail", 0)
3563 au! TextChanged <buffer>
3564 au! TextChangedI <buffer>
3565 augroup! Test_TextChanged
3566 delfu TextChangedAutocmdI
3567 unlet! g:autocmd_i g:autocmd_n
3568
3569 bw!
3570endfunc
Bram Moolenaar2c7080b2021-02-06 19:19:42 +01003571
Bram Moolenaar6f2465d2022-03-22 18:13:01 +00003572func Test_closing_autocmd_window()
3573 let lines =<< trim END
3574 edit Xa.txt
3575 tabnew Xb.txt
3576 autocmd BufEnter Xa.txt unhide 1
3577 doautoall BufEnter
3578 END
3579 call v9.CheckScriptFailure(lines, 'E814:')
3580 au! BufEnter
3581 only!
3582 bwipe Xa.txt
3583 bwipe Xb.txt
3584endfunc
3585
Bram Moolenaar347538f2022-03-26 16:28:06 +00003586func Test_bufwipeout_changes_window()
3587 " This should not crash, but we don't have any expectations about what
3588 " happens, changing window in BufWipeout has unpredictable results.
3589 tabedit
3590 let g:window_id = win_getid()
3591 topleft new
3592 setlocal bufhidden=wipe
3593 autocmd BufWipeout <buffer> call win_gotoid(g:window_id)
3594 tabprevious
3595 +tabclose
3596
3597 unlet g:window_id
3598 au! BufWipeout
3599 %bwipe!
3600endfunc
3601
zeertzjq021996f2022-04-10 11:44:04 +01003602func Test_v_event_readonly()
3603 autocmd CompleteChanged * let v:event.width = 0
3604 call assert_fails("normal! i\<C-X>\<C-V>", 'E46:')
3605 au! CompleteChanged
3606
3607 autocmd DirChangedPre * let v:event.directory = ''
3608 call assert_fails('cd .', 'E46:')
3609 au! DirChangedPre
3610
3611 autocmd ModeChanged * let v:event.new_mode = ''
3612 call assert_fails('normal! cc', 'E46:')
3613 au! ModeChanged
3614
3615 autocmd TextYankPost * let v:event.operator = ''
3616 call assert_fails('normal! yy', 'E46:')
3617 au! TextYankPost
3618endfunc
3619
zeertzjqc9e8fd62022-07-26 18:12:38 +01003620" Test for ModeChanged pattern
3621func Test_mode_changes()
3622 let g:index = 0
3623 let g:mode_seq = ['n', 'i', 'n', 'v', 'V', 'i', 'ix', 'i', 'ic', 'i', 'n', 'no', 'n', 'V', 'v', 's', 'n']
3624 func! TestMode()
3625 call assert_equal(g:mode_seq[g:index], get(v:event, "old_mode"))
3626 call assert_equal(g:mode_seq[g:index + 1], get(v:event, "new_mode"))
3627 call assert_equal(mode(1), get(v:event, "new_mode"))
3628 let g:index += 1
3629 endfunc
3630
3631 au ModeChanged * :call TestMode()
3632 let g:n_to_any = 0
3633 au ModeChanged n:* let g:n_to_any += 1
3634 call feedkeys("i\<esc>vVca\<CR>\<C-X>\<C-L>\<esc>ggdG", 'tnix')
3635
3636 let g:V_to_v = 0
3637 au ModeChanged V:v let g:V_to_v += 1
3638 call feedkeys("Vv\<C-G>\<esc>", 'tnix')
3639 call assert_equal(len(filter(g:mode_seq[1:], {idx, val -> val == 'n'})), g:n_to_any)
3640 call assert_equal(1, g:V_to_v)
3641 call assert_equal(len(g:mode_seq) - 1, g:index)
3642
3643 let g:n_to_i = 0
3644 au ModeChanged n:i let g:n_to_i += 1
3645 let g:n_to_niI = 0
3646 au ModeChanged i:niI let g:n_to_niI += 1
3647 let g:niI_to_i = 0
3648 au ModeChanged niI:i let g:niI_to_i += 1
3649 let g:nany_to_i = 0
3650 au ModeChanged n*:i let g:nany_to_i += 1
3651 let g:i_to_n = 0
3652 au ModeChanged i:n let g:i_to_n += 1
3653 let g:nori_to_any = 0
3654 au ModeChanged [ni]:* let g:nori_to_any += 1
3655 let g:i_to_any = 0
3656 au ModeChanged i:* let g:i_to_any += 1
3657 let g:index = 0
3658 let g:mode_seq = ['n', 'i', 'niI', 'i', 'n']
3659 call feedkeys("a\<C-O>l\<esc>", 'tnix')
3660 call assert_equal(len(g:mode_seq) - 1, g:index)
3661 call assert_equal(1, g:n_to_i)
3662 call assert_equal(1, g:n_to_niI)
3663 call assert_equal(1, g:niI_to_i)
3664 call assert_equal(2, g:nany_to_i)
3665 call assert_equal(1, g:i_to_n)
3666 call assert_equal(2, g:i_to_any)
3667 call assert_equal(3, g:nori_to_any)
3668
3669 if has('terminal')
3670 let g:mode_seq += ['c', 'n', 't', 'nt', 'c', 'nt', 'n']
3671 call feedkeys(":term\<CR>\<C-W>N:bd!\<CR>", 'tnix')
3672 call assert_equal(len(g:mode_seq) - 1, g:index)
3673 call assert_equal(1, g:n_to_i)
3674 call assert_equal(1, g:n_to_niI)
3675 call assert_equal(1, g:niI_to_i)
3676 call assert_equal(2, g:nany_to_i)
3677 call assert_equal(1, g:i_to_n)
3678 call assert_equal(2, g:i_to_any)
3679 call assert_equal(5, g:nori_to_any)
3680 endif
3681
zeertzjqd1955982022-10-05 11:24:46 +01003682 let g:n_to_c = 0
3683 au ModeChanged n:c let g:n_to_c += 1
3684 let g:c_to_n = 0
3685 au ModeChanged c:n let g:c_to_n += 1
3686 let g:mode_seq += ['c', 'n', 'c', 'n']
3687 call feedkeys("q:\<C-C>\<Esc>", 'tnix')
3688 call assert_equal(len(g:mode_seq) - 1, g:index)
3689 call assert_equal(2, g:n_to_c)
3690 call assert_equal(2, g:c_to_n)
3691 unlet g:n_to_c
3692 unlet g:c_to_n
zeertzjqc9e8fd62022-07-26 18:12:38 +01003693
Bram Moolenaar61c4b042022-10-18 15:10:11 +01003694 let g:n_to_v = 0
3695 au ModeChanged n:v let g:n_to_v += 1
3696 let g:v_to_n = 0
3697 au ModeChanged v:n let g:v_to_n += 1
3698 let g:mode_seq += ['v', 'n']
3699 call feedkeys("v\<C-C>", 'tnix')
3700 call assert_equal(len(g:mode_seq) - 1, g:index)
3701 call assert_equal(1, g:n_to_v)
3702 call assert_equal(1, g:v_to_n)
3703 unlet g:n_to_v
3704 unlet g:v_to_n
3705
zeertzjqc9e8fd62022-07-26 18:12:38 +01003706 au! ModeChanged
3707 delfunc TestMode
3708 unlet! g:mode_seq
3709 unlet! g:index
3710 unlet! g:n_to_any
3711 unlet! g:V_to_v
3712 unlet! g:n_to_i
3713 unlet! g:n_to_niI
3714 unlet! g:niI_to_i
3715 unlet! g:nany_to_i
3716 unlet! g:i_to_n
3717 unlet! g:nori_to_any
3718 unlet! g:i_to_any
3719endfunc
3720
3721func Test_recursive_ModeChanged()
3722 au! ModeChanged * norm 0u
3723 sil! norm 
3724 au! ModeChanged
3725endfunc
3726
3727func Test_ModeChanged_starts_visual()
3728 " This was triggering ModeChanged before setting VIsual, causing a crash.
3729 au! ModeChanged * norm 0u
3730 sil! norm 
3731
3732 au! ModeChanged
3733endfunc
Bram Moolenaar347538f2022-03-26 16:28:06 +00003734
Charlie Grovesfef44852022-04-19 16:24:12 +01003735func Test_noname_autocmd()
3736 augroup test_noname_autocmd_group
3737 autocmd!
3738 autocmd BufEnter * call add(s:li, ["BufEnter", expand("<afile>")])
3739 autocmd BufDelete * call add(s:li, ["BufDelete", expand("<afile>")])
3740 autocmd BufLeave * call add(s:li, ["BufLeave", expand("<afile>")])
3741 autocmd BufUnload * call add(s:li, ["BufUnload", expand("<afile>")])
3742 autocmd BufWipeout * call add(s:li, ["BufWipeout", expand("<afile>")])
3743 augroup END
3744
3745 let s:li = []
3746 edit foo
3747 call assert_equal([['BufUnload', ''], ['BufDelete', ''], ['BufWipeout', ''], ['BufEnter', 'foo']], s:li)
3748
3749 au! test_noname_autocmd_group
3750 augroup! test_noname_autocmd_group
3751endfunc
3752
Yegappan Lakshmanan1755a912022-05-19 10:31:47 +01003753" Test for the autocmd_get() function
3754func Test_autocmd_get()
3755 augroup TestAutoCmdFns
3756 au!
3757 autocmd BufAdd *.vim echo "bufadd-vim"
3758 autocmd BufAdd *.py echo "bufadd-py"
3759 autocmd BufHidden *.vim echo "bufhidden"
3760 augroup END
3761 augroup TestAutoCmdFns2
3762 autocmd BufAdd *.vim echo "bufadd-vim-2"
3763 autocmd BufRead *.a1b2c3 echo "bufadd-vim-2"
3764 augroup END
3765
3766 let l = autocmd_get()
3767 call assert_true(l->len() > 0)
3768
3769 " Test for getting all the autocmds in a group
3770 let expected = [
3771 \ #{cmd: 'echo "bufadd-vim"', group: 'TestAutoCmdFns',
3772 \ pattern: '*.vim', nested: v:false, once: v:false,
3773 \ event: 'BufAdd'},
3774 \ #{cmd: 'echo "bufadd-py"', group: 'TestAutoCmdFns',
3775 \ pattern: '*.py', nested: v:false, once: v:false,
3776 \ event: 'BufAdd'},
3777 \ #{cmd: 'echo "bufhidden"', group: 'TestAutoCmdFns',
3778 \ pattern: '*.vim', nested: v:false,
3779 \ once: v:false, event: 'BufHidden'}]
3780 call assert_equal(expected, autocmd_get(#{group: 'TestAutoCmdFns'}))
3781
3782 " Test for getting autocmds for all the patterns in a group
3783 call assert_equal(expected, autocmd_get(#{group: 'TestAutoCmdFns',
3784 \ event: '*'}))
3785
3786 " Test for getting autocmds for an event in a group
3787 let expected = [
3788 \ #{cmd: 'echo "bufadd-vim"', group: 'TestAutoCmdFns',
3789 \ pattern: '*.vim', nested: v:false, once: v:false,
3790 \ event: 'BufAdd'},
3791 \ #{cmd: 'echo "bufadd-py"', group: 'TestAutoCmdFns',
3792 \ pattern: '*.py', nested: v:false, once: v:false,
3793 \ event: 'BufAdd'}]
3794 call assert_equal(expected, autocmd_get(#{group: 'TestAutoCmdFns',
3795 \ event: 'BufAdd'}))
3796
3797 " Test for getting the autocmds for all the events in a group for particular
3798 " pattern
3799 call assert_equal([{'cmd': 'echo "bufadd-py"', 'group': 'TestAutoCmdFns',
3800 \ 'pattern': '*.py', 'nested': v:false, 'once': v:false,
3801 \ 'event': 'BufAdd'}],
3802 \ autocmd_get(#{group: 'TestAutoCmdFns', event: '*', pattern: '*.py'}))
3803
3804 " Test for getting the autocmds for an events in a group for particular
3805 " pattern
3806 let l = autocmd_get(#{group: 'TestAutoCmdFns', event: 'BufAdd',
3807 \ pattern: '*.vim'})
3808 call assert_equal([
3809 \ #{cmd: 'echo "bufadd-vim"', group: 'TestAutoCmdFns',
3810 \ pattern: '*.vim', nested: v:false, once: v:false,
3811 \ event: 'BufAdd'}], l)
3812
3813 " Test for getting the autocmds for a pattern in a group
3814 let l = autocmd_get(#{group: 'TestAutoCmdFns', pattern: '*.vim'})
3815 call assert_equal([
3816 \ #{cmd: 'echo "bufadd-vim"', group: 'TestAutoCmdFns',
3817 \ pattern: '*.vim', nested: v:false, once: v:false,
3818 \ event: 'BufAdd'},
3819 \ #{cmd: 'echo "bufhidden"', group: 'TestAutoCmdFns',
3820 \ pattern: '*.vim', nested: v:false,
3821 \ once: v:false, event: 'BufHidden'}], l)
3822
3823 " Test for getting the autocmds for a pattern in all the groups
3824 let l = autocmd_get(#{pattern: '*.a1b2c3'})
3825 call assert_equal([{'cmd': 'echo "bufadd-vim-2"', 'group': 'TestAutoCmdFns2',
3826 \ 'pattern': '*.a1b2c3', 'nested': v:false, 'once': v:false,
3827 \ 'event': 'BufRead'}], l)
3828
3829 " Test for getting autocmds for a pattern without any autocmds
3830 call assert_equal([], autocmd_get(#{group: 'TestAutoCmdFns',
3831 \ pattern: '*.abc'}))
3832 call assert_equal([], autocmd_get(#{group: 'TestAutoCmdFns',
3833 \ event: 'BufAdd', pattern: '*.abc'}))
3834 call assert_equal([], autocmd_get(#{group: 'TestAutoCmdFns',
3835 \ event: 'BufWipeout'}))
3836 call assert_fails("call autocmd_get(#{group: 'abc', event: 'BufAdd'})",
3837 \ 'E367:')
3838 let cmd = "echo autocmd_get(#{group: 'TestAutoCmdFns', event: 'abc'})"
3839 call assert_fails(cmd, 'E216:')
3840 call assert_fails("call autocmd_get(#{group: 'abc'})", 'E367:')
3841 call assert_fails("echo autocmd_get(#{event: 'abc'})", 'E216:')
3842
3843 augroup TestAutoCmdFns
3844 au!
3845 augroup END
3846 call assert_equal([], autocmd_get(#{group: 'TestAutoCmdFns'}))
3847
3848 " Test for nested and once autocmds
3849 augroup TestAutoCmdFns
3850 au!
3851 autocmd VimSuspend * ++nested echo "suspend"
3852 autocmd VimResume * ++once echo "resume"
3853 augroup END
3854
3855 let expected = [
3856 \ {'cmd': 'echo "suspend"', 'group': 'TestAutoCmdFns', 'pattern': '*',
3857 \ 'nested': v:true, 'once': v:false, 'event': 'VimSuspend'},
3858 \ {'cmd': 'echo "resume"', 'group': 'TestAutoCmdFns', 'pattern': '*',
3859 \ 'nested': v:false, 'once': v:true, 'event': 'VimResume'}]
3860 call assert_equal(expected, autocmd_get(#{group: 'TestAutoCmdFns'}))
3861
3862 " Test for buffer-local autocmd
3863 augroup TestAutoCmdFns
3864 au!
3865 autocmd TextYankPost <buffer> echo "textyankpost"
3866 augroup END
3867
3868 let expected = [
3869 \ {'cmd': 'echo "textyankpost"', 'group': 'TestAutoCmdFns',
3870 \ 'pattern': '<buffer=' .. bufnr() .. '>', 'nested': v:false,
3871 \ 'once': v:false, 'bufnr': bufnr(), 'event': 'TextYankPost'}]
3872 call assert_equal(expected, autocmd_get(#{group: 'TestAutoCmdFns'}))
3873
3874 augroup TestAutoCmdFns
3875 au!
3876 augroup END
3877 augroup! TestAutoCmdFns
3878 augroup TestAutoCmdFns2
3879 au!
3880 augroup END
3881 augroup! TestAutoCmdFns2
3882
3883 call assert_fails("echo autocmd_get(#{group: []})", 'E730:')
3884 call assert_fails("echo autocmd_get(#{event: {}})", 'E731:')
3885 call assert_fails("echo autocmd_get([])", 'E1206:')
3886endfunc
3887
3888" Test for the autocmd_add() function
3889func Test_autocmd_add()
3890 " Define a single autocmd in a group
3891 call autocmd_add([#{group: 'TestAcSet', event: 'BufAdd', pattern: '*.sh',
3892 \ cmd: 'echo "bufadd"', once: v:true, nested: v:true}])
3893 call assert_equal([#{cmd: 'echo "bufadd"', group: 'TestAcSet',
3894 \ pattern: '*.sh', nested: v:true, once: v:true,
3895 \ event: 'BufAdd'}], autocmd_get(#{group: 'TestAcSet'}))
3896
3897 " Define two autocmds in the same group
3898 call autocmd_delete([#{group: 'TestAcSet'}])
3899 call autocmd_add([#{group: 'TestAcSet', event: 'BufAdd', pattern: '*.sh',
3900 \ cmd: 'echo "bufadd"'},
3901 \ #{group: 'TestAcSet', event: 'BufEnter', pattern: '*.sh',
3902 \ cmd: 'echo "bufenter"'}])
3903 call assert_equal([
3904 \ #{cmd: 'echo "bufadd"', group: 'TestAcSet', pattern: '*.sh',
3905 \ nested: v:false, once: v:false, event: 'BufAdd'},
3906 \ #{cmd: 'echo "bufenter"', group: 'TestAcSet', pattern: '*.sh',
3907 \ nested: v:false, once: v:false, event: 'BufEnter'}],
3908 \ autocmd_get(#{group: 'TestAcSet'}))
3909
3910 " Define a buffer-local autocmd
3911 call autocmd_delete([#{group: 'TestAcSet'}])
3912 call autocmd_add([#{group: 'TestAcSet', event: 'CursorHold',
3913 \ bufnr: bufnr(), cmd: 'echo "cursorhold"'}])
3914 call assert_equal([
3915 \ #{cmd: 'echo "cursorhold"', group: 'TestAcSet',
3916 \ pattern: '<buffer=' .. bufnr() .. '>', nested: v:false,
3917 \ once: v:false, bufnr: bufnr(), event: 'CursorHold'}],
3918 \ autocmd_get(#{group: 'TestAcSet'}))
3919
3920 " Use an invalid buffer number
3921 call autocmd_delete([#{group: 'TestAcSet'}])
3922 call autocmd_add([#{group: 'TestAcSet', event: 'BufEnter',
3923 \ bufnr: -1, cmd: 'echo "bufenter"'}])
3924 let l = [#{group: 'TestAcSet', event: 'BufAdd', bufnr: 9999,
3925 \ cmd: 'echo "bufadd"'}]
3926 call assert_fails("echo autocmd_add(l)", 'E680:')
Yegappan Lakshmanan00e977c2022-06-01 12:31:53 +01003927 let l = [#{group: 'TestAcSet', event: 'BufAdd', bufnr: 9999,
3928 \ pattern: '*.py', cmd: 'echo "bufadd"'}]
3929 call assert_fails("echo autocmd_add(l)", 'E680:')
3930 let l = [#{group: 'TestAcSet', event: 'BufAdd', bufnr: 9999,
3931 \ pattern: ['*.py', '*.c'], cmd: 'echo "bufadd"'}]
3932 call assert_fails("echo autocmd_add(l)", 'E680:')
Yegappan Lakshmanan1755a912022-05-19 10:31:47 +01003933 let l = [#{group: 'TestAcSet', event: 'BufRead', bufnr: [],
3934 \ cmd: 'echo "bufread"'}]
3935 call assert_fails("echo autocmd_add(l)", 'E745:')
3936 call assert_equal([], autocmd_get(#{group: 'TestAcSet'}))
3937
3938 " Add two commands to the same group, event and pattern
3939 call autocmd_delete([#{group: 'TestAcSet'}])
3940 call autocmd_add([#{group: 'TestAcSet', event: 'BufUnload',
3941 \ pattern: 'abc', cmd: 'echo "cmd1"'}])
3942 call autocmd_add([#{group: 'TestAcSet', event: 'BufUnload',
3943 \ pattern: 'abc', cmd: 'echo "cmd2"'}])
3944 call assert_equal([
3945 \ #{cmd: 'echo "cmd1"', group: 'TestAcSet', pattern: 'abc',
3946 \ nested: v:false, once: v:false, event: 'BufUnload'},
3947 \ #{cmd: 'echo "cmd2"', group: 'TestAcSet', pattern: 'abc',
3948 \ nested: v:false, once: v:false, event: 'BufUnload'}],
3949 \ autocmd_get(#{group: 'TestAcSet'}))
3950
3951 " When adding a new autocmd, if the autocmd 'group' is not specified, then
3952 " the current autocmd group should be used.
3953 call autocmd_delete([#{group: 'TestAcSet'}])
3954 augroup TestAcSet
3955 call autocmd_add([#{event: 'BufHidden', pattern: 'abc', cmd: 'echo "abc"'}])
3956 augroup END
3957 call assert_equal([
3958 \ #{cmd: 'echo "abc"', group: 'TestAcSet', pattern: 'abc',
3959 \ nested: v:false, once: v:false, event: 'BufHidden'}],
3960 \ autocmd_get(#{group: 'TestAcSet'}))
3961
Yegappan Lakshmanan971f6822022-05-24 11:40:11 +01003962 " Test for replacing a cmd for an event in a group
3963 call autocmd_delete([#{group: 'TestAcSet'}])
3964 call autocmd_add([#{replace: v:true, group: 'TestAcSet', event: 'BufEnter',
3965 \ pattern: '*.py', cmd: 'echo "bufenter"'}])
3966 call autocmd_add([#{replace: v:true, group: 'TestAcSet', event: 'BufEnter',
3967 \ pattern: '*.py', cmd: 'echo "bufenter"'}])
3968 call assert_equal([
3969 \ #{cmd: 'echo "bufenter"', group: 'TestAcSet', pattern: '*.py',
3970 \ nested: v:false, once: v:false, event: 'BufEnter'}],
3971 \ autocmd_get(#{group: 'TestAcSet'}))
3972
3973 " Test for adding a command for an unsupported autocmd event
Yegappan Lakshmanan1755a912022-05-19 10:31:47 +01003974 let l = [#{group: 'TestAcSet', event: 'abc', pattern: '*.sh',
3975 \ cmd: 'echo "bufadd"'}]
3976 call assert_fails('call autocmd_add(l)', 'E216:')
3977
Yegappan Lakshmanane0ff3a72022-05-27 18:05:33 +01003978 " Test for using a list of events and patterns
3979 call autocmd_delete([#{group: 'TestAcSet'}])
3980 let l = [#{group: 'TestAcSet', event: ['BufEnter', 'BufLeave'],
3981 \ pattern: ['*.py', '*.sh'], cmd: 'echo "bufcmds"'}]
3982 call autocmd_add(l)
3983 call assert_equal([
3984 \ #{cmd: 'echo "bufcmds"', group: 'TestAcSet', pattern: '*.py',
3985 \ nested: v:false, once: v:false, event: 'BufEnter'},
3986 \ #{cmd: 'echo "bufcmds"', group: 'TestAcSet', pattern: '*.sh',
3987 \ nested: v:false, once: v:false, event: 'BufEnter'},
3988 \ #{cmd: 'echo "bufcmds"', group: 'TestAcSet', pattern: '*.py',
3989 \ nested: v:false, once: v:false, event: 'BufLeave'},
3990 \ #{cmd: 'echo "bufcmds"', group: 'TestAcSet', pattern: '*.sh',
3991 \ nested: v:false, once: v:false, event: 'BufLeave'}],
3992 \ autocmd_get(#{group: 'TestAcSet'}))
3993
3994 " Test for invalid values for 'event' item
3995 call autocmd_delete([#{group: 'TestAcSet'}])
3996 let l = [#{group: 'TestAcSet', event: test_null_string(),
3997 \ pattern: "*.py", cmd: 'echo "bufcmds"'}]
3998 call assert_fails('call autocmd_add(l)', 'E928:')
3999 let l = [#{group: 'TestAcSet', event: test_null_list(),
4000 \ pattern: "*.py", cmd: 'echo "bufcmds"'}]
4001 call assert_fails('call autocmd_add(l)', 'E714:')
4002 let l = [#{group: 'TestAcSet', event: {},
4003 \ pattern: "*.py", cmd: 'echo "bufcmds"'}]
4004 call assert_fails('call autocmd_add(l)', 'E777:')
4005 let l = [#{group: 'TestAcSet', event: [{}],
4006 \ pattern: "*.py", cmd: 'echo "bufcmds"'}]
4007 call assert_fails('call autocmd_add(l)', 'E928:')
4008 let l = [#{group: 'TestAcSet', event: [test_null_string()],
4009 \ pattern: "*.py", cmd: 'echo "bufcmds"'}]
4010 call assert_fails('call autocmd_add(l)', 'E928:')
4011 let l = [#{group: 'TestAcSet', event: 'BufEnter,BufLeave',
4012 \ pattern: '*.py', cmd: 'echo "bufcmds"'}]
4013 call assert_fails('call autocmd_add(l)', 'E216:')
4014 let l = [#{group: 'TestAcSet', event: [],
4015 \ pattern: "*.py", cmd: 'echo "bufcmds"'}]
4016 call autocmd_add(l)
4017 let l = [#{group: 'TestAcSet', event: [""],
4018 \ pattern: "*.py", cmd: 'echo "bufcmds"'}]
4019 call assert_fails('call autocmd_add(l)', 'E216:')
4020 let l = [#{group: 'TestAcSet', event: "",
4021 \ pattern: "*.py", cmd: 'echo "bufcmds"'}]
4022 call autocmd_add(l)
4023 call assert_equal([], autocmd_get(#{group: 'TestAcSet'}))
4024
4025 " Test for invalid values for 'pattern' item
4026 let l = [#{group: 'TestAcSet', event: "BufEnter",
4027 \ pattern: test_null_string(), cmd: 'echo "bufcmds"'}]
Yegappan Lakshmanan00e977c2022-06-01 12:31:53 +01004028 call assert_fails('call autocmd_add(l)', 'E928:')
Yegappan Lakshmanane0ff3a72022-05-27 18:05:33 +01004029 let l = [#{group: 'TestAcSet', event: "BufEnter",
4030 \ pattern: test_null_list(), cmd: 'echo "bufcmds"'}]
4031 call assert_fails('call autocmd_add(l)', 'E714:')
4032 let l = [#{group: 'TestAcSet', event: "BufEnter",
4033 \ pattern: {}, cmd: 'echo "bufcmds"'}]
4034 call assert_fails('call autocmd_add(l)', 'E777:')
4035 let l = [#{group: 'TestAcSet', event: "BufEnter",
4036 \ pattern: [{}], cmd: 'echo "bufcmds"'}]
4037 call assert_fails('call autocmd_add(l)', 'E928:')
4038 let l = [#{group: 'TestAcSet', event: "BufEnter",
4039 \ pattern: [test_null_string()], cmd: 'echo "bufcmds"'}]
4040 call assert_fails('call autocmd_add(l)', 'E928:')
4041 let l = [#{group: 'TestAcSet', event: "BufEnter",
4042 \ pattern: [], cmd: 'echo "bufcmds"'}]
4043 call autocmd_add(l)
4044 let l = [#{group: 'TestAcSet', event: "BufEnter",
4045 \ pattern: [""], cmd: 'echo "bufcmds"'}]
4046 call autocmd_add(l)
4047 let l = [#{group: 'TestAcSet', event: "BufEnter",
4048 \ pattern: "", cmd: 'echo "bufcmds"'}]
4049 call autocmd_add(l)
4050 call assert_equal([], autocmd_get(#{group: 'TestAcSet'}))
4051
4052 let l = [#{group: 'TestAcSet', event: 'BufEnter,abc,BufLeave',
4053 \ pattern: '*.py', cmd: 'echo "bufcmds"'}]
4054 call assert_fails('call autocmd_add(l)', 'E216:')
4055
Yegappan Lakshmanan1755a912022-05-19 10:31:47 +01004056 call assert_fails("call autocmd_add({})", 'E1211:')
4057 call assert_equal(v:false, autocmd_add(test_null_list()))
4058 call assert_true(autocmd_add([[]]))
4059 call assert_true(autocmd_add([test_null_dict()]))
4060
4061 augroup TestAcSet
4062 au!
4063 augroup END
4064
4065 call autocmd_add([#{group: 'TestAcSet'}])
4066 call autocmd_add([#{group: 'TestAcSet', event: 'BufAdd'}])
4067 call autocmd_add([#{group: 'TestAcSet', pat: '*.sh'}])
4068 call autocmd_add([#{group: 'TestAcSet', cmd: 'echo "a"'}])
4069 call autocmd_add([#{group: 'TestAcSet', event: 'BufAdd', pat: '*.sh'}])
4070 call autocmd_add([#{group: 'TestAcSet', event: 'BufAdd', cmd: 'echo "a"'}])
4071 call autocmd_add([#{group: 'TestAcSet', pat: '*.sh', cmd: 'echo "a"'}])
4072 call assert_equal([], autocmd_get(#{group: 'TestAcSet'}))
4073
4074 augroup! TestAcSet
4075endfunc
4076
4077" Test for deleting autocmd events and groups
4078func Test_autocmd_delete()
4079 " Delete an event in an autocmd group
4080 augroup TestAcSet
4081 au!
4082 au BufAdd *.sh echo "bufadd"
4083 au BufEnter *.sh echo "bufenter"
4084 augroup END
4085 call autocmd_delete([#{group: 'TestAcSet', event: 'BufAdd'}])
4086 call assert_equal([#{cmd: 'echo "bufenter"', group: 'TestAcSet',
4087 \ pattern: '*.sh', nested: v:false, once: v:false,
4088 \ event: 'BufEnter'}], autocmd_get(#{group: 'TestAcSet'}))
4089
4090 " Delete all the events in an autocmd group
4091 augroup TestAcSet
4092 au BufAdd *.sh echo "bufadd"
4093 augroup END
4094 call autocmd_delete([#{group: 'TestAcSet', event: '*'}])
4095 call assert_equal([], autocmd_get(#{group: 'TestAcSet'}))
4096
4097 " Delete a non-existing autocmd group
4098 call assert_fails("call autocmd_delete([#{group: 'abc'}])", 'E367:')
4099 " Delete a non-existing autocmd event
4100 let l = [#{group: 'TestAcSet', event: 'abc'}]
4101 call assert_fails("call autocmd_delete(l)", 'E216:')
4102 " Delete a non-existing autocmd pattern
4103 let l = [#{group: 'TestAcSet', event: 'BufAdd', pat: 'abc'}]
4104 call assert_true(autocmd_delete(l))
Yegappan Lakshmanan00e977c2022-06-01 12:31:53 +01004105 " Delete an autocmd for a non-existing buffer
4106 let l = [#{event: '*', bufnr: 9999, cmd: 'echo "x"'}]
4107 call assert_fails('call autocmd_delete(l)', 'E680:')
Yegappan Lakshmanan1755a912022-05-19 10:31:47 +01004108
4109 " Delete an autocmd group
4110 augroup TestAcSet
4111 au!
4112 au BufAdd *.sh echo "bufadd"
4113 au BufEnter *.sh echo "bufenter"
4114 augroup END
4115 call autocmd_delete([#{group: 'TestAcSet'}])
4116 call assert_fails("call autocmd_get(#{group: 'TestAcSet'})", 'E367:')
4117
4118 call assert_true(autocmd_delete([[]]))
4119 call assert_true(autocmd_delete([test_null_dict()]))
4120endfunc
4121
Bram Moolenaar8f3c3c62022-10-18 17:05:54 +01004122func Test_autocmd_split_dummy()
4123 " Autocommand trying to split a window containing a dummy buffer.
Bram Moolenaar94722c52023-01-28 19:19:03 +00004124 auto BufReadPre * exe "sbuf " .. expand("<abuf>")
Bram Moolenaar8f3c3c62022-10-18 17:05:54 +01004125 " Avoid the "W11" prompt
4126 au FileChangedShell * let v:fcs_choice = 'reload'
4127 func Xautocmd_changelist()
4128 cal writefile(['Xtestfile2:4:4'], 'Xerr')
4129 edit Xerr
4130 lex 'Xtestfile2:4:4'
4131 endfunc
4132 call Xautocmd_changelist()
Bram Moolenaar53c5c9f2022-10-18 17:25:03 +01004133 " Should get E86, but it doesn't always happen (timing?)
4134 silent! call Xautocmd_changelist()
Bram Moolenaar8f3c3c62022-10-18 17:05:54 +01004135
4136 au! BufReadPre
4137 au! FileChangedShell
4138 delfunc Xautocmd_changelist
4139 bwipe! Xerr
4140 call delete('Xerr')
4141endfunc
4142
Bram Moolenaare76062c2022-11-28 18:51:43 +00004143" This was crashing because there was only one window to execute autocommands
4144" in.
4145func Test_autocmd_nested_setbufvar()
4146 CheckFeature python3
4147
4148 set hidden
4149 edit Xaaa
4150 edit Xbbb
4151 call setline(1, 'bar')
4152 enew
4153 au BufWriteCmd Xbbb ++nested call setbufvar('Xaaa', '&ft', 'foo') | bw! Xaaa
4154 au FileType foo call py3eval('vim.current.buffer.options["cindent"]')
4155 wall
4156
4157 au! BufWriteCmd
4158 au! FileType foo
4159 set nohidden
4160 call delete('Xaaa')
4161 call delete('Xbbb')
4162 %bwipe!
4163endfunc
4164
Christian Brabandt9aee8ec2022-12-16 16:41:23 +00004165func SetupVimTest_shm()
4166 let g:bwe = []
4167 let g:brp = []
4168 set shortmess+=F
4169
4170 let dirname='XVimTestSHM'
4171 call mkdir(dirname, 'R')
4172 call writefile(['test'], dirname .. '/1')
4173 call writefile(['test'], dirname .. '/2')
4174 call writefile(['test'], dirname .. '/3')
4175
4176 augroup test
4177 autocmd!
4178 autocmd BufWinEnter * call add(g:bwe, $'BufWinEnter: {expand('<amatch>')}')
4179 autocmd BufReadPost * call add(g:brp, $'BufReadPost: {expand('<amatch>')}')
4180 augroup END
4181
4182 call setqflist([
4183 \ {'filename': dirname .. '/1', 'lnum': 1, 'col': 1, 'text': 'test', 'vcol': 0},
4184 \ {'filename': dirname .. '/2', 'lnum': 1, 'col': 1, 'text': 'test', 'vcol': 0},
4185 \ {'filename': dirname .. '/3', 'lnum': 1, 'col': 1, 'text': 'test', 'vcol': 0}
4186 \ ])
4187 cdo! substitute/test/TEST
4188
4189 " clean up
4190 noa enew!
4191 set shortmess&vim
4192 augroup test
4193 autocmd!
4194 augroup END
4195 augroup! test
4196endfunc
4197
4198func Test_autocmd_shortmess()
4199 CheckNotMSWindows
4200
4201 call SetupVimTest_shm()
4202 let output = execute(':mess')->split('\n')
4203
4204 let info = copy(output)->filter({idx, val -> val =~# '\d of 3'} )
4205 let bytes = copy(output)->filter({idx, val -> val =~# 'bytes'} )
4206
4207 " We test the following here:
4208 " BufReadPost should have been triggered 3 times, once per file
4209 " BufWinEnter should have been triggered 3 times, once per file
4210 " FileInfoMessage should have been shown 3 times, regardless of shm option
4211 " "(x of 3)" message from :cnext has been shown 3 times
4212
4213 call assert_equal(3, g:brp->len())
4214 call assert_equal(3, g:bwe->len())
4215 call assert_equal(3, info->len())
4216 call assert_equal(3, bytes->len())
4217
4218 delfunc SetupVimTest_shm
4219endfunc
Bram Moolenaare76062c2022-11-28 18:51:43 +00004220
Bram Moolenaarbc2b71d2020-02-17 21:33:30 +01004221" vim: shiftwidth=2 sts=2 expandtab