blob: cf3d6fca133ade0ede043e08b1a58ad8b85d859e [file] [log] [blame]
Bram Moolenaar14735512016-03-26 21:00:08 +01001" Tests for autocommands
2
Bram Moolenaar8c64a362018-03-23 22:39:31 +01003source shared.vim
Bram Moolenaar8c5a2782019-08-07 23:07:07 +02004source check.vim
Bram Moolenaarcadbe1b2019-09-22 21:50:09 +02005source term_util.vim
LemonBoy09371822022-04-08 15:18:45 +01006source screendump.vim
Bram Moolenaar6f2465d2022-03-22 18:13:01 +00007import './vim9.vim' as v9
Bram Moolenaar8c64a362018-03-23 22:39:31 +01008
Bram Moolenaar1e115362019-01-09 23:01:02 +01009func s:cleanup_buffers() abort
Bram Moolenaarb3435b02016-09-29 20:54:59 +020010 for bnr in range(1, bufnr('$'))
11 if bufloaded(bnr) && bufnr('%') != bnr
12 execute 'bd! ' . bnr
13 endif
14 endfor
Bram Moolenaar04f62f82017-07-19 18:18:39 +020015endfunc
Bram Moolenaarb3435b02016-09-29 20:54:59 +020016
Bram Moolenaar14735512016-03-26 21:00:08 +010017func Test_vim_did_enter()
18 call assert_false(v:vim_did_enter)
19
20 " This script will never reach the main loop, can't check if v:vim_did_enter
21 " becomes one.
22endfunc
Bram Moolenaar40b1b542016-04-20 20:18:23 +020023
Bram Moolenaar75911162020-07-21 19:44:47 +020024" Test for the CursorHold autocmd
25func Test_CursorHold_autocmd()
26 CheckRunVimInTerminal
Bram Moolenaare1f3ab72022-09-04 21:29:08 +010027 call writefile(['one', 'two', 'three'], 'XoneTwoThree', 'D')
Bram Moolenaar75911162020-07-21 19:44:47 +020028 let before =<< trim END
29 set updatetime=10
Bram Moolenaare7cda972022-08-29 11:02:59 +010030 au CursorHold * call writefile([line('.')], 'XCHoutput', 'a')
Bram Moolenaar75911162020-07-21 19:44:47 +020031 END
Bram Moolenaare1f3ab72022-09-04 21:29:08 +010032 call writefile(before, 'XCHinit', 'D')
Bram Moolenaare7cda972022-08-29 11:02:59 +010033 let buf = RunVimInTerminal('-S XCHinit XoneTwoThree', {})
Bram Moolenaar17f67542020-08-20 18:29:13 +020034 call term_sendkeys(buf, "G")
Bram Moolenaar62cd26a2020-10-11 20:08:44 +020035 call term_wait(buf, 50)
Bram Moolenaar75911162020-07-21 19:44:47 +020036 call term_sendkeys(buf, "gg")
37 call term_wait(buf)
Bram Moolenaare7cda972022-08-29 11:02:59 +010038 call WaitForAssert({-> assert_equal(['1'], readfile('XCHoutput')[-1:-1])})
Bram Moolenaar75911162020-07-21 19:44:47 +020039 call term_sendkeys(buf, "j")
40 call term_wait(buf)
Bram Moolenaare7cda972022-08-29 11:02:59 +010041 call WaitForAssert({-> assert_equal(['1', '2'], readfile('XCHoutput')[-2:-1])})
Bram Moolenaar75911162020-07-21 19:44:47 +020042 call term_sendkeys(buf, "j")
43 call term_wait(buf)
Bram Moolenaare7cda972022-08-29 11:02:59 +010044 call WaitForAssert({-> assert_equal(['1', '2', '3'], readfile('XCHoutput')[-3:-1])})
Bram Moolenaar75911162020-07-21 19:44:47 +020045 call StopVimInTerminal(buf)
46
Bram Moolenaare7cda972022-08-29 11:02:59 +010047 call delete('XCHoutput')
Bram Moolenaar75911162020-07-21 19:44:47 +020048endfunc
49
Bram Moolenaarc67e8922016-05-24 16:07:40 +020050if has('timers')
Bram Moolenaar97b00752019-05-12 13:07:14 +020051
Bram Moolenaarc67e8922016-05-24 16:07:40 +020052 func ExitInsertMode(id)
53 call feedkeys("\<Esc>")
54 endfunc
55
56 func Test_cursorhold_insert()
Bram Moolenaarf18c4db2016-09-08 22:10:06 +020057 " Need to move the cursor.
58 call feedkeys("ggG", "xt")
59
Bram Moolenaarc67e8922016-05-24 16:07:40 +020060 let g:triggered = 0
61 au CursorHoldI * let g:triggered += 1
62 set updatetime=20
Bram Moolenaar92bb83e2021-02-03 23:04:46 +010063 call timer_start(200, 'ExitInsertMode')
Bram Moolenaarc67e8922016-05-24 16:07:40 +020064 call feedkeys('a', 'x!')
Bram Moolenaar3b014be2022-11-13 17:53:46 +000065 sleep 30m
Bram Moolenaarc67e8922016-05-24 16:07:40 +020066 call assert_equal(1, g:triggered)
Bram Moolenaar26d98212019-01-27 22:32:55 +010067 unlet g:triggered
68 au! CursorHoldI
69 set updatetime&
70 endfunc
71
72 func Test_cursorhold_insert_with_timer_interrupt()
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +020073 CheckFeature job
Bram Moolenaar26d98212019-01-27 22:32:55 +010074 " Need to move the cursor.
75 call feedkeys("ggG", "xt")
76
77 " Confirm the timer invoked in exit_cb of the job doesn't disturb
78 " CursorHoldI event.
79 let g:triggered = 0
80 au CursorHoldI * let g:triggered += 1
Bram Moolenaar62cd26a2020-10-11 20:08:44 +020081 set updatetime=100
Bram Moolenaar26d98212019-01-27 22:32:55 +010082 call job_start(has('win32') ? 'cmd /c echo:' : 'echo',
Bram Moolenaar62cd26a2020-10-11 20:08:44 +020083 \ {'exit_cb': {-> timer_start(200, 'ExitInsertMode')}})
Bram Moolenaar26d98212019-01-27 22:32:55 +010084 call feedkeys('a', 'x!')
85 call assert_equal(1, g:triggered)
86 unlet g:triggered
Bram Moolenaare99e8442016-07-26 20:43:40 +020087 au! CursorHoldI
Bram Moolenaaraeac9002016-09-06 22:15:08 +020088 set updatetime&
Bram Moolenaarc67e8922016-05-24 16:07:40 +020089 endfunc
90
91 func Test_cursorhold_insert_ctrl_x()
92 let g:triggered = 0
93 au CursorHoldI * let g:triggered += 1
94 set updatetime=20
95 call timer_start(100, 'ExitInsertMode')
96 " CursorHoldI does not trigger after CTRL-X
97 call feedkeys("a\<C-X>", 'x!')
98 call assert_equal(0, g:triggered)
Bram Moolenaar26d98212019-01-27 22:32:55 +010099 unlet g:triggered
Bram Moolenaare99e8442016-07-26 20:43:40 +0200100 au! CursorHoldI
Bram Moolenaaraeac9002016-09-06 22:15:08 +0200101 set updatetime&
Bram Moolenaarc67e8922016-05-24 16:07:40 +0200102 endfunc
Bram Moolenaar97b00752019-05-12 13:07:14 +0200103
Bram Moolenaar5a9357d2021-10-03 16:22:05 +0100104 func Test_cursorhold_insert_ctrl_g_U()
105 au CursorHoldI * :
106 set updatetime=20
107 new
108 call timer_start(100, { -> feedkeys("\<Left>foo\<Esc>", 't') })
109 call feedkeys("i()\<C-g>U", 'tx!')
110 sleep 200m
111 call assert_equal('(foo)', getline(1))
112 undo
113 call assert_equal('', getline(1))
114
115 bwipe!
116 au! CursorHoldI
117 set updatetime&
118 endfunc
119
Bram Moolenaar97b00752019-05-12 13:07:14 +0200120 func Test_OptionSet_modeline()
121 call test_override('starting', 1)
122 au! OptionSet
123 augroup set_tabstop
124 au OptionSet tabstop call timer_start(1, {-> execute("echo 'Handler called'", "")})
125 augroup END
Bram Moolenaare1f3ab72022-09-04 21:29:08 +0100126 call writefile(['vim: set ts=7 sw=5 :', 'something'], 'XoptionsetModeline', 'D')
Bram Moolenaar97b00752019-05-12 13:07:14 +0200127 set modeline
128 let v:errmsg = ''
129 call assert_fails('split XoptionsetModeline', 'E12:')
130 call assert_equal(7, &ts)
131 call assert_equal('', v:errmsg)
132
133 augroup set_tabstop
134 au!
135 augroup END
136 bwipe!
137 set ts&
Bram Moolenaar97b00752019-05-12 13:07:14 +0200138 call test_override('starting', 0)
139 endfunc
140
141endif "has('timers')
Bram Moolenaar40b1b542016-04-20 20:18:23 +0200142
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200143func Test_bufunload()
Bram Moolenaarc67e8922016-05-24 16:07:40 +0200144 augroup test_bufunload_group
145 autocmd!
146 autocmd BufUnload * call add(s:li, "bufunload")
147 autocmd BufDelete * call add(s:li, "bufdelete")
148 autocmd BufWipeout * call add(s:li, "bufwipeout")
149 augroup END
Bram Moolenaar40b1b542016-04-20 20:18:23 +0200150
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100151 let s:li = []
Bram Moolenaarc67e8922016-05-24 16:07:40 +0200152 new
153 setlocal bufhidden=
154 bunload
155 call assert_equal(["bufunload", "bufdelete"], s:li)
Bram Moolenaar40b1b542016-04-20 20:18:23 +0200156
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100157 let s:li = []
Bram Moolenaarc67e8922016-05-24 16:07:40 +0200158 new
159 setlocal bufhidden=delete
160 bunload
161 call assert_equal(["bufunload", "bufdelete"], s:li)
162
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100163 let s:li = []
Bram Moolenaarc67e8922016-05-24 16:07:40 +0200164 new
165 setlocal bufhidden=unload
166 bwipeout
167 call assert_equal(["bufunload", "bufdelete", "bufwipeout"], s:li)
168
Bram Moolenaare99e8442016-07-26 20:43:40 +0200169 au! test_bufunload_group
Bram Moolenaarc67e8922016-05-24 16:07:40 +0200170 augroup! test_bufunload_group
Bram Moolenaar40b1b542016-04-20 20:18:23 +0200171endfunc
Bram Moolenaar30445cb2016-07-09 15:21:02 +0200172
173" SEGV occurs in older versions. (At least 7.4.2005 or older)
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200174func Test_autocmd_bufunload_with_tabnext()
Bram Moolenaar30445cb2016-07-09 15:21:02 +0200175 tabedit
176 tabfirst
177
178 augroup test_autocmd_bufunload_with_tabnext_group
179 autocmd!
180 autocmd BufUnload <buffer> tabnext
181 augroup END
182
183 quit
184 call assert_equal(2, tabpagenr('$'))
185
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200186 autocmd! test_autocmd_bufunload_with_tabnext_group
Bram Moolenaar30445cb2016-07-09 15:21:02 +0200187 augroup! test_autocmd_bufunload_with_tabnext_group
188 tablast
189 quit
190endfunc
Bram Moolenaarc917da42016-07-19 22:31:36 +0200191
Bram Moolenaar5ed58c72021-01-28 14:24:55 +0100192func Test_argdelete_in_next()
193 au BufNew,BufEnter,BufLeave,BufWinEnter * argdel
194 call assert_fails('next a b', 'E1156:')
195 au! BufNew,BufEnter,BufLeave,BufWinEnter *
196endfunc
197
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200198func Test_autocmd_bufwinleave_with_tabfirst()
Bram Moolenaarf9e687e2016-09-04 21:33:09 +0200199 tabedit
200 augroup sample
201 autocmd!
202 autocmd BufWinLeave <buffer> tabfirst
203 augroup END
204 call setline(1, ['a', 'b', 'c'])
205 edit! a.txt
Bram Moolenaarf18c4db2016-09-08 22:10:06 +0200206 tabclose
Bram Moolenaarf9e687e2016-09-04 21:33:09 +0200207endfunc
208
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200209" SEGV occurs in older versions. (At least 7.4.2321 or older)
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200210func Test_autocmd_bufunload_avoiding_SEGV_01()
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200211 split aa.txt
212 let lastbuf = bufnr('$')
213
214 augroup test_autocmd_bufunload
215 autocmd!
216 exe 'autocmd BufUnload <buffer> ' . (lastbuf + 1) . 'bwipeout!'
217 augroup END
218
Bram Moolenaar28ee8922020-10-28 20:20:00 +0100219 call assert_fails('edit bb.txt', 'E937:')
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200220
221 autocmd! test_autocmd_bufunload
222 augroup! test_autocmd_bufunload
223 bwipe! aa.txt
224 bwipe! bb.txt
225endfunc
226
227" SEGV occurs in older versions. (At least 7.4.2321 or older)
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200228func Test_autocmd_bufunload_avoiding_SEGV_02()
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200229 setlocal buftype=nowrite
230 let lastbuf = bufnr('$')
231
232 augroup test_autocmd_bufunload
233 autocmd!
234 exe 'autocmd BufUnload <buffer> ' . (lastbuf + 1) . 'bwipeout!'
235 augroup END
236
237 normal! i1
238 call assert_fails('edit a.txt', 'E517:')
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200239
240 autocmd! test_autocmd_bufunload
241 augroup! test_autocmd_bufunload
242 bwipe! a.txt
243endfunc
244
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100245func Test_autocmd_dummy_wipeout()
246 " prepare files
Bram Moolenaare1f3ab72022-09-04 21:29:08 +0100247 call writefile([''], 'Xdummywipetest1.txt', 'D')
248 call writefile([''], 'Xdummywipetest2.txt', 'D')
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100249 augroup test_bufunload_group
250 autocmd!
251 autocmd BufUnload * call add(s:li, "bufunload")
252 autocmd BufDelete * call add(s:li, "bufdelete")
253 autocmd BufWipeout * call add(s:li, "bufwipeout")
254 augroup END
255
256 let s:li = []
257 split Xdummywipetest1.txt
258 silent! vimgrep /notmatched/ Xdummywipetest*
259 call assert_equal(["bufunload", "bufwipeout"], s:li)
260
261 bwipeout
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100262 au! test_bufunload_group
263 augroup! test_bufunload_group
264endfunc
265
Bram Moolenaarc917da42016-07-19 22:31:36 +0200266func Test_win_tab_autocmd()
267 let g:record = []
268
269 augroup testing
270 au WinNew * call add(g:record, 'WinNew')
naohiro ono23beefe2021-11-13 12:38:49 +0000271 au WinClosed * call add(g:record, 'WinClosed')
Bram Moolenaarc917da42016-07-19 22:31:36 +0200272 au WinEnter * call add(g:record, 'WinEnter')
273 au WinLeave * call add(g:record, 'WinLeave')
274 au TabNew * call add(g:record, 'TabNew')
Bram Moolenaar12c11d52016-07-19 23:13:03 +0200275 au TabClosed * call add(g:record, 'TabClosed')
Bram Moolenaarc917da42016-07-19 22:31:36 +0200276 au TabEnter * call add(g:record, 'TabEnter')
277 au TabLeave * call add(g:record, 'TabLeave')
278 augroup END
279
280 split
281 tabnew
282 close
283 close
284
285 call assert_equal([
286 \ 'WinLeave', 'WinNew', 'WinEnter',
287 \ 'WinLeave', 'TabLeave', 'WinNew', 'WinEnter', 'TabNew', 'TabEnter',
naohiro ono23beefe2021-11-13 12:38:49 +0000288 \ 'WinLeave', 'TabLeave', 'WinClosed', 'TabClosed', 'WinEnter', 'TabEnter',
289 \ 'WinLeave', 'WinClosed', 'WinEnter'
Bram Moolenaarc917da42016-07-19 22:31:36 +0200290 \ ], g:record)
291
Bram Moolenaar12c11d52016-07-19 23:13:03 +0200292 let g:record = []
293 tabnew somefile
294 tabnext
295 bwipe somefile
296
297 call assert_equal([
298 \ 'WinLeave', 'TabLeave', 'WinNew', 'WinEnter', 'TabNew', 'TabEnter',
299 \ 'WinLeave', 'TabLeave', 'WinEnter', 'TabEnter',
naohiro ono23beefe2021-11-13 12:38:49 +0000300 \ 'WinClosed', 'TabClosed'
Bram Moolenaar12c11d52016-07-19 23:13:03 +0200301 \ ], g:record)
302
Bram Moolenaarc917da42016-07-19 22:31:36 +0200303 augroup testing
304 au!
305 augroup END
306 unlet g:record
307endfunc
Bram Moolenaare99e8442016-07-26 20:43:40 +0200308
Bram Moolenaar35fc61c2022-11-22 12:40:50 +0000309func Test_WinResized()
310 CheckRunVimInTerminal
311
312 let lines =<< trim END
313 set scrolloff=0
314 call setline(1, ['111', '222'])
315 vnew
316 call setline(1, ['aaa', 'bbb'])
317 new
318 call setline(1, ['foo', 'bar'])
319
320 let g:resized = 0
321 au WinResized * let g:resized += 1
322
323 func WriteResizedEvent()
324 call writefile([json_encode(v:event)], 'XresizeEvent')
325 endfunc
326 au WinResized * call WriteResizedEvent()
327 END
328 call writefile(lines, 'Xtest_winresized', 'D')
329 let buf = RunVimInTerminal('-S Xtest_winresized', {'rows': 10})
330
331 " redraw now to avoid a redraw after the :echo command
332 call term_sendkeys(buf, ":redraw!\<CR>")
333 call TermWait(buf)
334
335 call term_sendkeys(buf, ":echo g:resized\<CR>")
336 call WaitForAssert({-> assert_match('^0$', term_getline(buf, 10))}, 1000)
337
338 " increase window height, two windows will be reported
339 call term_sendkeys(buf, "\<C-W>+")
340 call TermWait(buf)
341 call term_sendkeys(buf, ":echo g:resized\<CR>")
342 call WaitForAssert({-> assert_match('^1$', term_getline(buf, 10))}, 1000)
343
344 let event = readfile('XresizeEvent')[0]->json_decode()
345 call assert_equal({
346 \ 'windows': [1002, 1001],
347 \ }, event)
348
349 " increase window width, three windows will be reported
350 call term_sendkeys(buf, "\<C-W>>")
351 call TermWait(buf)
352 call term_sendkeys(buf, ":echo g:resized\<CR>")
353 call WaitForAssert({-> assert_match('^2$', term_getline(buf, 10))}, 1000)
354
355 let event = readfile('XresizeEvent')[0]->json_decode()
356 call assert_equal({
357 \ 'windows': [1002, 1001, 1000],
358 \ }, event)
359
360 call delete('XresizeEvent')
361 call StopVimInTerminal(buf)
362endfunc
363
LemonBoy09371822022-04-08 15:18:45 +0100364func Test_WinScrolled()
365 CheckRunVimInTerminal
366
367 let lines =<< trim END
zeertzjqd58862d2022-04-12 11:32:48 +0100368 set nowrap scrolloff=0
369 for ii in range(1, 18)
370 call setline(ii, repeat(nr2char(96 + ii), ii * 2))
371 endfor
372 let win_id = win_getid()
373 let g:matched = v:false
Bram Moolenaar35fc61c2022-11-22 12:40:50 +0000374 func WriteScrollEvent()
375 call writefile([json_encode(v:event)], 'XscrollEvent')
376 endfunc
zeertzjqd58862d2022-04-12 11:32:48 +0100377 execute 'au WinScrolled' win_id 'let g:matched = v:true'
378 let g:scrolled = 0
379 au WinScrolled * let g:scrolled += 1
380 au WinScrolled * let g:amatch = str2nr(expand('<amatch>'))
381 au WinScrolled * let g:afile = str2nr(expand('<afile>'))
Bram Moolenaar35fc61c2022-11-22 12:40:50 +0000382 au WinScrolled * call WriteScrollEvent()
LemonBoy09371822022-04-08 15:18:45 +0100383 END
Bram Moolenaare1f3ab72022-09-04 21:29:08 +0100384 call writefile(lines, 'Xtest_winscrolled', 'D')
LemonBoy09371822022-04-08 15:18:45 +0100385 let buf = RunVimInTerminal('-S Xtest_winscrolled', {'rows': 6})
386
387 call term_sendkeys(buf, ":echo g:scrolled\<CR>")
388 call WaitForAssert({-> assert_match('^0 ', term_getline(buf, 6))}, 1000)
389
390 " Scroll left/right in Normal mode.
391 call term_sendkeys(buf, "zlzh:echo g:scrolled\<CR>")
392 call WaitForAssert({-> assert_match('^2 ', term_getline(buf, 6))}, 1000)
393
Bram Moolenaar35fc61c2022-11-22 12:40:50 +0000394 let event = readfile('XscrollEvent')[0]->json_decode()
395 call assert_equal({
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)
643endfunc
644
naohiro ono23beefe2021-11-13 12:38:49 +0000645func Test_WinClosed()
646 " Test that the pattern is matched against the closed window's ID, and both
647 " <amatch> and <afile> are set to it.
648 new
649 let winid = win_getid()
650 let g:matched = v:false
651 augroup test-WinClosed
652 autocmd!
653 execute 'autocmd WinClosed' winid 'let g:matched = v:true'
654 autocmd WinClosed * let g:amatch = str2nr(expand('<amatch>'))
655 autocmd WinClosed * let g:afile = str2nr(expand('<afile>'))
656 augroup END
657 close
658 call assert_true(g:matched)
659 call assert_equal(winid, g:amatch)
660 call assert_equal(winid, g:afile)
661
662 " Test that WinClosed is non-recursive.
663 new
664 new
665 call assert_equal(3, winnr('$'))
666 let g:triggered = 0
667 augroup test-WinClosed
668 autocmd!
669 autocmd WinClosed * let g:triggered += 1
670 autocmd WinClosed * 2 wincmd c
671 augroup END
672 close
673 call assert_equal(1, winnr('$'))
674 call assert_equal(1, g:triggered)
675
676 autocmd! test-WinClosed
677 augroup! test-WinClosed
678 unlet g:matched
679 unlet g:amatch
680 unlet g:afile
681 unlet g:triggered
682endfunc
683
Bram Moolenaarc947b9a2022-04-06 17:59:21 +0100684func Test_WinClosed_throws()
685 vnew
686 let bnr = bufnr()
687 call assert_equal(1, bufloaded(bnr))
688 augroup test-WinClosed
689 autocmd WinClosed * throw 'foo'
690 augroup END
691 try
692 close
693 catch /.*/
694 endtry
695 call assert_equal(0, bufloaded(bnr))
696
697 autocmd! test-WinClosed
698 augroup! test-WinClosed
699endfunc
700
zeertzjq6a069402022-04-07 14:08:29 +0100701func Test_WinClosed_throws_with_tabs()
702 tabnew
703 let bnr = bufnr()
704 call assert_equal(1, bufloaded(bnr))
705 augroup test-WinClosed
706 autocmd WinClosed * throw 'foo'
707 augroup END
708 try
709 close
710 catch /.*/
711 endtry
712 call assert_equal(0, bufloaded(bnr))
713
714 autocmd! test-WinClosed
715 augroup! test-WinClosed
716endfunc
717
zeertzjq62de54b2022-09-22 18:08:37 +0100718" This used to trigger WinClosed twice for the same window, and the window's
719" buffer was NULL in the second autocommand.
720func Test_WinClosed_switch_tab()
721 edit Xa
722 split Xb
723 split Xc
724 tab split
725 new
726 augroup test-WinClosed
727 autocmd WinClosed * tabprev | bwipe!
728 augroup END
729 close
730 " Check that the tabline has been fully removed
731 call assert_equal([1, 1], win_screenpos(0))
732
733 autocmd! test-WinClosed
734 augroup! test-WinClosed
735 %bwipe!
736endfunc
737
Bram Moolenaare99e8442016-07-26 20:43:40 +0200738func s:AddAnAutocmd()
739 augroup vimBarTest
740 au BufReadCmd * echo 'hello'
741 augroup END
742 call assert_equal(3, len(split(execute('au vimBarTest'), "\n")))
743endfunc
744
745func Test_early_bar()
746 " test that a bar is recognized before the {event}
747 call s:AddAnAutocmd()
Bram Moolenaarb8e642f2021-11-20 10:38:25 +0000748 augroup vimBarTest | au! | let done = 77 | augroup END
Bram Moolenaare99e8442016-07-26 20:43:40 +0200749 call assert_equal(1, len(split(execute('au vimBarTest'), "\n")))
Bram Moolenaarb8e642f2021-11-20 10:38:25 +0000750 call assert_equal(77, done)
Bram Moolenaare99e8442016-07-26 20:43:40 +0200751
752 call s:AddAnAutocmd()
Bram Moolenaarb8e642f2021-11-20 10:38:25 +0000753 augroup vimBarTest| au!| let done = 88 | augroup END
Bram Moolenaare99e8442016-07-26 20:43:40 +0200754 call assert_equal(1, len(split(execute('au vimBarTest'), "\n")))
Bram Moolenaarb8e642f2021-11-20 10:38:25 +0000755 call assert_equal(88, done)
Bram Moolenaare99e8442016-07-26 20:43:40 +0200756
757 " test that a bar is recognized after the {event}
758 call s:AddAnAutocmd()
Bram Moolenaarb8e642f2021-11-20 10:38:25 +0000759 augroup vimBarTest| au!BufReadCmd| let done = 99 | augroup END
Bram Moolenaare99e8442016-07-26 20:43:40 +0200760 call assert_equal(1, len(split(execute('au vimBarTest'), "\n")))
Bram Moolenaarb8e642f2021-11-20 10:38:25 +0000761 call assert_equal(99, done)
Bram Moolenaare99e8442016-07-26 20:43:40 +0200762
763 " test that a bar is recognized after the {group}
764 call s:AddAnAutocmd()
765 au! vimBarTest|echo 'hello'
766 call assert_equal(1, len(split(execute('au vimBarTest'), "\n")))
767endfunc
Bram Moolenaarf2c4c392016-07-29 20:50:24 +0200768
Bram Moolenaar5c809082016-09-01 16:21:48 +0200769func RemoveGroup()
770 autocmd! StartOK
771 augroup! StartOK
772endfunc
773
Bram Moolenaarf2c4c392016-07-29 20:50:24 +0200774func Test_augroup_warning()
775 augroup TheWarning
776 au VimEnter * echo 'entering'
777 augroup END
Bram Moolenaar5dc4e2f2020-11-25 14:15:12 +0100778 call assert_match("TheWarning.*VimEnter", execute('au VimEnter'))
Bram Moolenaarf2c4c392016-07-29 20:50:24 +0200779 redir => res
780 augroup! TheWarning
781 redir END
Bram Moolenaar5dc4e2f2020-11-25 14:15:12 +0100782 call assert_match("W19:", res)
783 call assert_match("-Deleted-.*VimEnter", execute('au VimEnter'))
Bram Moolenaarf2c4c392016-07-29 20:50:24 +0200784
785 " check "Another" does not take the pace of the deleted entry
786 augroup Another
787 augroup END
Bram Moolenaar5dc4e2f2020-11-25 14:15:12 +0100788 call assert_match("-Deleted-.*VimEnter", execute('au VimEnter'))
Bram Moolenaaraeac9002016-09-06 22:15:08 +0200789 augroup! Another
Bram Moolenaar5c809082016-09-01 16:21:48 +0200790
791 " no warning for postpone aucmd delete
792 augroup StartOK
793 au VimEnter * call RemoveGroup()
794 augroup END
Bram Moolenaar5dc4e2f2020-11-25 14:15:12 +0100795 call assert_match("StartOK.*VimEnter", execute('au VimEnter'))
Bram Moolenaar5c809082016-09-01 16:21:48 +0200796 redir => res
797 doautocmd VimEnter
798 redir END
Bram Moolenaar5dc4e2f2020-11-25 14:15:12 +0100799 call assert_notmatch("W19:", res)
Bram Moolenaarde653f02016-09-03 16:59:06 +0200800 au! VimEnter
Bram Moolenaarad48e6c2020-04-21 22:19:45 +0200801
802 call assert_fails('augroup!', 'E471:')
Bram Moolenaarf2c4c392016-07-29 20:50:24 +0200803endfunc
Bram Moolenaarb62cc362016-09-03 16:43:53 +0200804
Bram Moolenaar8d84ff12017-10-26 16:42:16 +0200805func Test_BufReadCmdHelp()
806 " This used to cause access to free memory
807 au BufReadCmd * e +h
808 help
809
Bram Moolenaar8d84ff12017-10-26 16:42:16 +0200810 au! BufReadCmd
811endfunc
812
813func Test_BufReadCmdHelpJump()
814 " This used to cause access to free memory
815 au BufReadCmd * e +h{
Bram Moolenaarcf1ba352017-10-27 00:55:04 +0200816 " } to fix highlighting
817 call assert_fails('help', 'E434:')
Bram Moolenaar8d84ff12017-10-26 16:42:16 +0200818
Bram Moolenaar8d84ff12017-10-26 16:42:16 +0200819 au! BufReadCmd
820endfunc
821
zeertzjq93f72cc2022-08-26 15:34:52 +0100822" BufReadCmd is triggered for a "nofile" buffer. Check all values.
Bram Moolenaarb1d2c812022-08-26 11:55:01 +0100823func Test_BufReadCmdNofile()
zeertzjq93f72cc2022-08-26 15:34:52 +0100824 for val in ['nofile',
825 \ 'nowrite',
826 \ 'acwrite',
827 \ 'quickfix',
828 \ 'help',
829 \ 'terminal',
830 \ 'prompt',
831 \ 'popup',
832 \ ]
833 new somefile
834 exe 'set buftype=' .. val
835 au BufReadCmd somefile call setline(1, 'triggered')
836 edit
837 call assert_equal('triggered', getline(1))
Bram Moolenaarb1d2c812022-08-26 11:55:01 +0100838
zeertzjq93f72cc2022-08-26 15:34:52 +0100839 au! BufReadCmd
840 bwipe!
841 endfor
Bram Moolenaarb1d2c812022-08-26 11:55:01 +0100842endfunc
843
Bram Moolenaarb62cc362016-09-03 16:43:53 +0200844func Test_augroup_deleted()
Bram Moolenaarde653f02016-09-03 16:59:06 +0200845 " This caused a crash before E936 was introduced
Bram Moolenaarb62cc362016-09-03 16:43:53 +0200846 augroup x
Bram Moolenaarde653f02016-09-03 16:59:06 +0200847 call assert_fails('augroup! x', 'E936:')
848 au VimEnter * echo
849 augroup end
Bram Moolenaarb62cc362016-09-03 16:43:53 +0200850 augroup! x
Bram Moolenaar5dc4e2f2020-11-25 14:15:12 +0100851 call assert_match("-Deleted-.*VimEnter", execute('au VimEnter'))
Bram Moolenaarde653f02016-09-03 16:59:06 +0200852 au! VimEnter
Bram Moolenaarb62cc362016-09-03 16:43:53 +0200853endfunc
854
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200855" Tests for autocommands on :close command.
856" This used to be in test13.
857func Test_three_windows()
Bram Moolenaarb3435b02016-09-29 20:54:59 +0200858 " Clean up buffers, because in some cases this function fails.
859 call s:cleanup_buffers()
860
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200861 " Write three files and open them, each in a window.
862 " Then go to next window, with autocommand that deletes the previous one.
863 " Do this twice, writing the file.
864 e! Xtestje1
865 call setline(1, 'testje1')
866 w
867 sp Xtestje2
868 call setline(1, 'testje2')
869 w
870 sp Xtestje3
871 call setline(1, 'testje3')
872 w
873 wincmd w
874 au WinLeave Xtestje2 bwipe
875 wincmd w
876 call assert_equal('Xtestje1', expand('%'))
877
878 au WinLeave Xtestje1 bwipe Xtestje3
879 close
880 call assert_equal('Xtestje1', expand('%'))
881
882 " Test deleting the buffer on a Unload event. If this goes wrong there
883 " will be the ATTENTION prompt.
884 e Xtestje1
885 au!
886 au! BufUnload Xtestje1 bwipe
887 call assert_fails('e Xtestje3', 'E937:')
888 call assert_equal('Xtestje3', expand('%'))
889
890 e Xtestje2
891 sp Xtestje1
892 call assert_fails('e', 'E937:')
Bram Moolenaara997b452018-04-17 23:24:06 +0200893 call assert_equal('Xtestje1', expand('%'))
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200894
895 " Test changing buffers in a BufWipeout autocommand. If this goes wrong
896 " there are ml_line errors and/or a Crash.
897 au!
898 only
899 e Xanother
900 e Xtestje1
901 bwipe Xtestje2
902 bwipe Xtestje3
903 au BufWipeout Xtestje1 buf Xtestje1
904 bwipe
905 call assert_equal('Xanother', expand('%'))
906
907 only
908 help
909 wincmd w
910 1quit
911 call assert_equal('Xanother', expand('%'))
912
913 au!
Bram Moolenaar4520d442017-03-19 16:09:46 +0100914 enew
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200915 call delete('Xtestje1')
916 call delete('Xtestje2')
917 call delete('Xtestje3')
918endfunc
Bram Moolenaare13b9af2017-01-13 22:01:02 +0100919
920func Test_BufEnter()
921 au! BufEnter
922 au Bufenter * let val = val . '+'
923 let g:val = ''
924 split NewFile
925 call assert_equal('+', g:val)
926 bwipe!
927 call assert_equal('++', g:val)
928
929 " Also get BufEnter when editing a directory
Bram Moolenaar6f14da12022-09-07 21:30:44 +0100930 call mkdir('Xbufenterdir', 'D')
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +0100931 split Xbufenterdir
Bram Moolenaare13b9af2017-01-13 22:01:02 +0100932 call assert_equal('+++', g:val)
Bram Moolenaare94260f2017-03-21 15:50:12 +0100933
934 " On MS-Windows we can't edit the directory, make sure we wipe the right
935 " buffer.
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +0100936 bwipe! Xbufenterdir
Bram Moolenaare13b9af2017-01-13 22:01:02 +0100937 au! BufEnter
Bram Moolenaara9b5b852022-08-26 13:16:20 +0100938
939 " Editing a "nofile" buffer doesn't read the file but does trigger BufEnter
zeertzjq93f72cc2022-08-26 15:34:52 +0100940 " for historic reasons. Also test other 'buftype' values.
941 for val in ['nofile',
942 \ 'nowrite',
943 \ 'acwrite',
944 \ 'quickfix',
945 \ 'help',
946 \ 'terminal',
947 \ 'prompt',
948 \ 'popup',
949 \ ]
950 new somefile
951 exe 'set buftype=' .. val
952 au BufEnter somefile call setline(1, 'some text')
953 edit
954 call assert_equal('some text', getline(1))
955 bwipe!
956 au! BufEnter
957 endfor
Bram Moolenaar9fda8152022-11-19 13:14:10 +0000958
959 new
960 new
961 autocmd BufEnter * ++once close
962 call assert_fails('close', 'E1312:')
963
964 au! BufEnter
965 only
Bram Moolenaare13b9af2017-01-13 22:01:02 +0100966endfunc
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100967
968" Closing a window might cause an endless loop
969" E814 for older Vims
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200970func Test_autocmd_bufwipe_in_SessLoadPost()
Bram Moolenaar1d68d9b2017-10-13 22:33:32 +0200971 edit Xtest
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100972 tabnew
Bram Moolenaar1d68d9b2017-10-13 22:33:32 +0200973 file Xsomething
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100974 set noswapfile
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100975 mksession!
976
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200977 let content =<< trim [CODE]
Bram Moolenaar62cd26a2020-10-11 20:08:44 +0200978 call test_override('ui_delay', 10)
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200979 set nocp noswapfile
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100980 let v:swapchoice = "e"
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200981 augroup test_autocmd_sessionload
982 autocmd!
983 autocmd SessionLoadPost * exe bufnr("Xsomething") . "bw!"
984 augroup END
985
986 func WriteErrors()
Bram Moolenaarbe9fc5b2022-09-09 17:09:35 +0100987 call writefile([execute("messages")], "XerrorsBwipe")
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200988 endfunc
989 au VimLeave * call WriteErrors()
990 [CODE]
991
Bram Moolenaare1f3ab72022-09-04 21:29:08 +0100992 call writefile(content, 'Xvimrc', 'D')
Bram Moolenaar93344c22019-08-14 21:12:05 +0200993 call system(GetVimCommand('Xvimrc') .. ' --not-a-term --noplugins -S Session.vim -c cq')
Bram Moolenaarae04a602022-09-09 15:08:10 +0100994 sleep 100m
Bram Moolenaarbe9fc5b2022-09-09 17:09:35 +0100995 let errors = join(readfile('XerrorsBwipe'))
Bram Moolenaare2e40752020-09-04 21:18:46 +0200996 call assert_match('E814:', errors)
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100997
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100998 set swapfile
Bram Moolenaarbe9fc5b2022-09-09 17:09:35 +0100999 for file in ['Session.vim', 'XerrorsBwipe']
Bram Moolenaar8c752bd2017-03-19 17:09:56 +01001000 call delete(file)
1001 endfor
1002endfunc
1003
Bram Moolenaar797e63b2021-01-15 16:22:52 +01001004" Using :blast and :ball for many events caused a crash, because b_nwindows was
1005" not incremented correctly.
1006func Test_autocmd_blast_badd()
1007 let content =<< trim [CODE]
1008 au BufNew,BufAdd,BufWinEnter,BufEnter,BufLeave,BufWinLeave,BufUnload,VimEnter foo* blast
1009 edit foo1
1010 au BufNew,BufAdd,BufWinEnter,BufEnter,BufLeave,BufWinLeave,BufUnload,VimEnter foo* ball
1011 edit foo2
Bram Moolenaarbe9fc5b2022-09-09 17:09:35 +01001012 call writefile(['OK'], 'XerrorsBlast')
Bram Moolenaar797e63b2021-01-15 16:22:52 +01001013 qall
1014 [CODE]
1015
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01001016 call writefile(content, 'XblastBall', 'D')
Bram Moolenaar797e63b2021-01-15 16:22:52 +01001017 call system(GetVimCommand() .. ' --clean -S XblastBall')
Bram Moolenaarae04a602022-09-09 15:08:10 +01001018 sleep 100m
Bram Moolenaarbe9fc5b2022-09-09 17:09:35 +01001019 call assert_match('OK', readfile('XerrorsBlast')->join())
Bram Moolenaar797e63b2021-01-15 16:22:52 +01001020
Bram Moolenaarbe9fc5b2022-09-09 17:09:35 +01001021 call delete('XerrorsBlast')
Bram Moolenaar797e63b2021-01-15 16:22:52 +01001022endfunc
1023
Bram Moolenaar8c752bd2017-03-19 17:09:56 +01001024" SEGV occurs in older versions.
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001025func Test_autocmd_bufwipe_in_SessLoadPost2()
Bram Moolenaar8c752bd2017-03-19 17:09:56 +01001026 tabnew
1027 set noswapfile
Bram Moolenaar8c752bd2017-03-19 17:09:56 +01001028 mksession!
1029
Bram Moolenaarc79745a2019-05-20 22:12:34 +02001030 let content =<< trim [CODE]
1031 set nocp noswapfile
1032 function! DeleteInactiveBufs()
1033 tabfirst
1034 let tabblist = []
1035 for i in range(1, tabpagenr(''$''))
1036 call extend(tabblist, tabpagebuflist(i))
1037 endfor
1038 for b in range(1, bufnr(''$''))
1039 if bufexists(b) && buflisted(b) && (index(tabblist, b) == -1 || bufname(b) =~# ''^$'')
1040 exec ''bwipeout '' . b
1041 endif
1042 endfor
1043 echomsg "SessionLoadPost DONE"
1044 endfunction
1045 au SessionLoadPost * call DeleteInactiveBufs()
1046
1047 func WriteErrors()
Bram Moolenaarbe9fc5b2022-09-09 17:09:35 +01001048 call writefile([execute("messages")], "XerrorsPost")
Bram Moolenaarc79745a2019-05-20 22:12:34 +02001049 endfunc
1050 au VimLeave * call WriteErrors()
1051 [CODE]
1052
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01001053 call writefile(content, 'Xvimrc', 'D')
Bram Moolenaar93344c22019-08-14 21:12:05 +02001054 call system(GetVimCommand('Xvimrc') .. ' --not-a-term --noplugins -S Session.vim -c cq')
Bram Moolenaarae04a602022-09-09 15:08:10 +01001055 sleep 100m
Bram Moolenaarbe9fc5b2022-09-09 17:09:35 +01001056 let errors = join(readfile('XerrorsPost'))
Bram Moolenaare94260f2017-03-21 15:50:12 +01001057 " This probably only ever matches on unix.
1058 call assert_notmatch('Caught deadly signal SEGV', errors)
1059 call assert_match('SessionLoadPost DONE', errors)
Bram Moolenaar8c752bd2017-03-19 17:09:56 +01001060
Bram Moolenaar8c752bd2017-03-19 17:09:56 +01001061 set swapfile
Bram Moolenaarbe9fc5b2022-09-09 17:09:35 +01001062 for file in ['Session.vim', 'XerrorsPost']
Bram Moolenaar8c752bd2017-03-19 17:09:56 +01001063 call delete(file)
1064 endfor
1065endfunc
Bram Moolenaarfaf29d72017-07-09 11:07:16 +02001066
1067func Test_empty_doau()
1068 doau \|
1069endfunc
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001070
1071func s:AutoCommandOptionSet(match)
Bram Moolenaard7c96872019-06-15 17:12:48 +02001072 let template = "Option: <%s>, OldVal: <%s>, OldValLocal: <%s>, OldValGlobal: <%s>, NewVal: <%s>, Scope: <%s>, Command: <%s>\n"
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001073 let item = remove(g:options, 0)
Bram Moolenaard7c96872019-06-15 17:12:48 +02001074 let expected = printf(template, item[0], item[1], item[2], item[3], item[4], item[5], item[6])
1075 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 +02001076 let g:opt = [expected, actual]
1077 "call assert_equal(expected, actual)
1078endfunc
1079
1080func Test_OptionSet()
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +02001081 CheckOption autochdir
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001082
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +02001083 badd test_autocmd.vim
1084
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001085 call test_override('starting', 1)
1086 set nocp
1087 au OptionSet * :call s:AutoCommandOptionSet(expand("<amatch>"))
1088
1089 " 1: Setting number option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001090 let g:options = [['number', 0, 0, 0, 1, 'global', 'set']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001091 set nu
1092 call assert_equal([], g:options)
1093 call assert_equal(g:opt[0], g:opt[1])
1094
1095 " 2: Setting local number option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001096 let g:options = [['number', 1, 1, '', 0, 'local', 'setlocal']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001097 setlocal nonu
1098 call assert_equal([], g:options)
1099 call assert_equal(g:opt[0], g:opt[1])
1100
1101 " 3: Setting global number option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001102 let g:options = [['number', 1, '', 1, 0, 'global', 'setglobal']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001103 setglobal nonu
1104 call assert_equal([], g:options)
1105 call assert_equal(g:opt[0], g:opt[1])
1106
1107 " 4: Setting local autoindent option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001108 let g:options = [['autoindent', 0, 0, '', 1, 'local', 'setlocal']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001109 setlocal ai
1110 call assert_equal([], g:options)
1111 call assert_equal(g:opt[0], g:opt[1])
1112
1113 " 5: Setting global autoindent option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001114 let g:options = [['autoindent', 0, '', 0, 1, 'global', 'setglobal']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001115 setglobal ai
1116 call assert_equal([], g:options)
1117 call assert_equal(g:opt[0], g:opt[1])
1118
1119 " 6: Setting global autoindent option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001120 let g:options = [['autoindent', 1, 1, 1, 0, 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001121 set ai!
1122 call assert_equal([], g:options)
1123 call assert_equal(g:opt[0], g:opt[1])
1124
1125 " 6a: Setting global autoindent option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001126 let g:options = [['autoindent', 1, 1, 0, 0, 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001127 noa setlocal ai
1128 noa setglobal noai
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001129 set ai!
1130 call assert_equal([], g:options)
1131 call assert_equal(g:opt[0], g:opt[1])
1132
1133 " Should not print anything, use :noa
1134 " 7: don't trigger OptionSet"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001135 let g:options = [['invalid', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001136 noa set nonu
Bram Moolenaard7c96872019-06-15 17:12:48 +02001137 call assert_equal([['invalid', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid']], g:options)
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001138 call assert_equal(g:opt[0], g:opt[1])
1139
1140 " 8: Setting several global list and number option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001141 let g:options = [['list', 0, 0, 0, 1, 'global', 'set'], ['number', 0, 0, 0, 1, 'global', 'set']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001142 set list nu
1143 call assert_equal([], g:options)
1144 call assert_equal(g:opt[0], g:opt[1])
1145
1146 " 9: don't trigger OptionSet"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001147 let g:options = [['invalid', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid'], ['invalid', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001148 noa set nolist nonu
Bram Moolenaard7c96872019-06-15 17:12:48 +02001149 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 +02001150 call assert_equal(g:opt[0], g:opt[1])
1151
1152 " 10: Setting global acd"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001153 let g:options = [['autochdir', 0, 0, '', 1, 'local', 'setlocal']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001154 setlocal acd
1155 call assert_equal([], g:options)
1156 call assert_equal(g:opt[0], g:opt[1])
1157
1158 " 11: Setting global autoread (also sets local value)"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001159 let g:options = [['autoread', 0, 0, 0, 1, 'global', 'set']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001160 set ar
1161 call assert_equal([], g:options)
1162 call assert_equal(g:opt[0], g:opt[1])
1163
1164 " 12: Setting local autoread"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001165 let g:options = [['autoread', 1, 1, '', 1, 'local', 'setlocal']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001166 setlocal ar
1167 call assert_equal([], g:options)
1168 call assert_equal(g:opt[0], g:opt[1])
1169
1170 " 13: Setting global autoread"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001171 let g:options = [['autoread', 1, '', 1, 0, 'global', 'setglobal']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001172 setglobal invar
1173 call assert_equal([], g:options)
1174 call assert_equal(g:opt[0], g:opt[1])
1175
1176 " 14: Setting option backspace through :let"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001177 let g:options = [['backspace', '', '', '', 'eol,indent,start', 'global', 'set']]
1178 let &bs = "eol,indent,start"
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001179 call assert_equal([], g:options)
1180 call assert_equal(g:opt[0], g:opt[1])
1181
1182 " 15: Setting option backspace through setbufvar()"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001183 let g:options = [['backup', 0, 0, '', 1, 'local', 'setlocal']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001184 " try twice, first time, shouldn't trigger because option name is invalid,
1185 " second time, it should trigger
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +02001186 let bnum = bufnr('%')
Bram Moolenaare2e40752020-09-04 21:18:46 +02001187 call assert_fails("call setbufvar(bnum, '&l:bk', 1)", 'E355:')
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001188 " should trigger, use correct option name
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +02001189 call setbufvar(bnum, '&backup', 1)
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001190 call assert_equal([], g:options)
1191 call assert_equal(g:opt[0], g:opt[1])
1192
1193 " 16: Setting number option using setwinvar"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001194 let g:options = [['number', 0, 0, '', 1, 'local', 'setlocal']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001195 call setwinvar(0, '&number', 1)
1196 call assert_equal([], g:options)
1197 call assert_equal(g:opt[0], g:opt[1])
1198
1199 " 17: Setting key option, shouldn't trigger"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001200 let g:options = [['key', 'invalid', 'invalid1', 'invalid2', 'invalid3', 'invalid4', 'invalid5']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001201 setlocal key=blah
1202 setlocal key=
Bram Moolenaard7c96872019-06-15 17:12:48 +02001203 call assert_equal([['key', 'invalid', 'invalid1', 'invalid2', 'invalid3', 'invalid4', 'invalid5']], g:options)
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001204 call assert_equal(g:opt[0], g:opt[1])
1205
Bram Moolenaard7c96872019-06-15 17:12:48 +02001206
1207 " 18a: Setting string global option"
1208 let oldval = &backupext
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001209 let g:options = [['backupext', oldval, oldval, oldval, 'foo', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001210 set backupext=foo
1211 call assert_equal([], g:options)
1212 call assert_equal(g:opt[0], g:opt[1])
1213
1214 " 18b: Resetting string global option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001215 let g:options = [['backupext', 'foo', 'foo', 'foo', oldval, 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001216 set backupext&
1217 call assert_equal([], g:options)
1218 call assert_equal(g:opt[0], g:opt[1])
1219
1220 " 18c: Setting global string global option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001221 let g:options = [['backupext', oldval, '', oldval, 'bar', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001222 setglobal backupext=bar
1223 call assert_equal([], g:options)
1224 call assert_equal(g:opt[0], g:opt[1])
1225
1226 " 18d: Setting local string global option"
1227 " As this is a global option this sets the global value even though
1228 " :setlocal is used!
1229 noa set backupext& " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001230 let g:options = [['backupext', oldval, oldval, '', 'baz', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001231 setlocal backupext=baz
1232 call assert_equal([], g:options)
1233 call assert_equal(g:opt[0], g:opt[1])
1234
1235 " 18e: Setting again string global option"
1236 noa setglobal backupext=ext_global " Reset global and local value (without triggering autocmd)
1237 noa setlocal backupext=ext_local " Sets the global(!) value!
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001238 let g:options = [['backupext', 'ext_local', 'ext_local', 'ext_local', 'fuu', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001239 set backupext=fuu
1240 call assert_equal([], g:options)
1241 call assert_equal(g:opt[0], g:opt[1])
1242
1243
zeertzjqb811de52021-10-21 10:50:44 +01001244 " 19a: Setting string global-local (to buffer) option"
Bram Moolenaar8efa0262017-08-20 15:47:20 +02001245 let oldval = &tags
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001246 let g:options = [['tags', oldval, oldval, oldval, 'tagpath', 'global', 'set']]
Bram Moolenaar8efa0262017-08-20 15:47:20 +02001247 set tags=tagpath
1248 call assert_equal([], g:options)
1249 call assert_equal(g:opt[0], g:opt[1])
1250
zeertzjqb811de52021-10-21 10:50:44 +01001251 " 19b: Resetting string global-local (to buffer) option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001252 let g:options = [['tags', 'tagpath', 'tagpath', 'tagpath', oldval, 'global', 'set']]
Bram Moolenaar8efa0262017-08-20 15:47:20 +02001253 set tags&
1254 call assert_equal([], g:options)
1255 call assert_equal(g:opt[0], g:opt[1])
1256
zeertzjqb811de52021-10-21 10:50:44 +01001257 " 19c: Setting global string global-local (to buffer) option "
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001258 let g:options = [['tags', oldval, '', oldval, 'tagpath1', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001259 setglobal tags=tagpath1
1260 call assert_equal([], g:options)
1261 call assert_equal(g:opt[0], g:opt[1])
1262
zeertzjqb811de52021-10-21 10:50:44 +01001263 " 19d: Setting local string global-local (to buffer) option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001264 let g:options = [['tags', 'tagpath1', 'tagpath1', '', 'tagpath2', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001265 setlocal tags=tagpath2
1266 call assert_equal([], g:options)
1267 call assert_equal(g:opt[0], g:opt[1])
1268
zeertzjqb811de52021-10-21 10:50:44 +01001269 " 19e: Setting again string global-local (to buffer) option"
1270 " Note: v:option_old is the old global value for global-local string options
Bram Moolenaard7c96872019-06-15 17:12:48 +02001271 " but the old local value for all other kinds of options.
1272 noa setglobal tags=tag_global " Reset global and local value (without triggering autocmd)
1273 noa setlocal tags=tag_local
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001274 let g:options = [['tags', 'tag_global', 'tag_local', 'tag_global', 'tagpath', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001275 set tags=tagpath
1276 call assert_equal([], g:options)
1277 call assert_equal(g:opt[0], g:opt[1])
1278
zeertzjqb811de52021-10-21 10:50:44 +01001279 " 19f: Setting string global-local (to buffer) option to an empty string"
1280 " Note: v:option_old is the old global value for global-local string options
Bram Moolenaard7c96872019-06-15 17:12:48 +02001281 " but the old local value for all other kinds of options.
1282 noa set tags=tag_global " Reset global and local value (without triggering autocmd)
1283 noa setlocal tags= " empty string
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001284 let g:options = [['tags', 'tag_global', '', 'tag_global', 'tagpath', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001285 set tags=tagpath
1286 call assert_equal([], g:options)
1287 call assert_equal(g:opt[0], g:opt[1])
1288
1289
1290 " 20a: Setting string local (to buffer) option"
1291 let oldval = &spelllang
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001292 let g:options = [['spelllang', oldval, oldval, oldval, 'elvish,klingon', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001293 set spelllang=elvish,klingon
1294 call assert_equal([], g:options)
1295 call assert_equal(g:opt[0], g:opt[1])
1296
1297 " 20b: Resetting string local (to buffer) option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001298 let g:options = [['spelllang', 'elvish,klingon', 'elvish,klingon', 'elvish,klingon', oldval, 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001299 set spelllang&
1300 call assert_equal([], g:options)
1301 call assert_equal(g:opt[0], g:opt[1])
1302
1303 " 20c: Setting global string local (to buffer) option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001304 let g:options = [['spelllang', oldval, '', oldval, 'elvish', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001305 setglobal spelllang=elvish
1306 call assert_equal([], g:options)
1307 call assert_equal(g:opt[0], g:opt[1])
1308
1309 " 20d: Setting local string local (to buffer) option"
1310 noa set spelllang& " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001311 let g:options = [['spelllang', oldval, oldval, '', 'klingon', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001312 setlocal spelllang=klingon
1313 call assert_equal([], g:options)
1314 call assert_equal(g:opt[0], g:opt[1])
1315
1316 " 20e: Setting again string local (to buffer) option"
zeertzjqb811de52021-10-21 10:50:44 +01001317 " Note: v:option_old is the old global value for global-local string options
Bram Moolenaard7c96872019-06-15 17:12:48 +02001318 " but the old local value for all other kinds of options.
1319 noa setglobal spelllang=spellglobal " Reset global and local value (without triggering autocmd)
1320 noa setlocal spelllang=spelllocal
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001321 let g:options = [['spelllang', 'spelllocal', 'spelllocal', 'spellglobal', 'foo', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001322 set spelllang=foo
1323 call assert_equal([], g:options)
1324 call assert_equal(g:opt[0], g:opt[1])
1325
1326
zeertzjqb811de52021-10-21 10:50:44 +01001327 " 21a: Setting string global-local (to window) option"
Bram Moolenaard7c96872019-06-15 17:12:48 +02001328 let oldval = &statusline
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001329 let g:options = [['statusline', oldval, oldval, oldval, 'foo', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001330 set statusline=foo
1331 call assert_equal([], g:options)
1332 call assert_equal(g:opt[0], g:opt[1])
1333
zeertzjqb811de52021-10-21 10:50:44 +01001334 " 21b: Resetting string global-local (to window) option"
1335 " Note: v:option_old is the old global value for global-local string options
Bram Moolenaard7c96872019-06-15 17:12:48 +02001336 " but the old local value for all other kinds of options.
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001337 let g:options = [['statusline', 'foo', 'foo', 'foo', oldval, 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001338 set statusline&
1339 call assert_equal([], g:options)
1340 call assert_equal(g:opt[0], g:opt[1])
1341
zeertzjqb811de52021-10-21 10:50:44 +01001342 " 21c: Setting global string global-local (to window) option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001343 let g:options = [['statusline', oldval, '', oldval, 'bar', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001344 setglobal statusline=bar
1345 call assert_equal([], g:options)
1346 call assert_equal(g:opt[0], g:opt[1])
1347
zeertzjqb811de52021-10-21 10:50:44 +01001348 " 21d: Setting local string global-local (to window) option"
Bram Moolenaard7c96872019-06-15 17:12:48 +02001349 noa set statusline& " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001350 let g:options = [['statusline', oldval, oldval, '', 'baz', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001351 setlocal statusline=baz
1352 call assert_equal([], g:options)
1353 call assert_equal(g:opt[0], g:opt[1])
1354
zeertzjqb811de52021-10-21 10:50:44 +01001355 " 21e: Setting again string global-local (to window) option"
1356 " Note: v:option_old is the old global value for global-local string options
Bram Moolenaard7c96872019-06-15 17:12:48 +02001357 " but the old local value for all other kinds of options.
1358 noa setglobal statusline=bar " Reset global and local value (without triggering autocmd)
1359 noa setlocal statusline=baz
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001360 let g:options = [['statusline', 'bar', 'baz', 'bar', 'foo', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001361 set statusline=foo
1362 call assert_equal([], g:options)
1363 call assert_equal(g:opt[0], g:opt[1])
1364
1365
1366 " 22a: Setting string local (to window) option"
1367 let oldval = &foldignore
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001368 let g:options = [['foldignore', oldval, oldval, oldval, 'fo', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001369 set foldignore=fo
1370 call assert_equal([], g:options)
1371 call assert_equal(g:opt[0], g:opt[1])
1372
1373 " 22b: Resetting string local (to window) option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001374 let g:options = [['foldignore', 'fo', 'fo', 'fo', oldval, 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001375 set foldignore&
1376 call assert_equal([], g:options)
1377 call assert_equal(g:opt[0], g:opt[1])
1378
1379 " 22c: Setting global string local (to window) option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001380 let g:options = [['foldignore', oldval, '', oldval, 'bar', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001381 setglobal foldignore=bar
1382 call assert_equal([], g:options)
1383 call assert_equal(g:opt[0], g:opt[1])
1384
1385 " 22d: Setting local string local (to window) option"
1386 noa set foldignore& " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001387 let g:options = [['foldignore', oldval, oldval, '', 'baz', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001388 setlocal foldignore=baz
1389 call assert_equal([], g:options)
1390 call assert_equal(g:opt[0], g:opt[1])
1391
1392 " 22e: Setting again string local (to window) option"
1393 noa setglobal foldignore=glob " Reset global and local value (without triggering autocmd)
1394 noa setlocal foldignore=loc
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001395 let g:options = [['foldignore', 'loc', 'loc', 'glob', 'fo', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001396 set foldignore=fo
1397 call assert_equal([], g:options)
1398 call assert_equal(g:opt[0], g:opt[1])
1399
1400
zeertzjqb811de52021-10-21 10:50:44 +01001401 " 23a: Setting global number global option"
Bram Moolenaard7c96872019-06-15 17:12:48 +02001402 noa setglobal cmdheight=8 " Reset global and local value (without triggering autocmd)
1403 noa setlocal cmdheight=1 " Sets the global(!) value!
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001404 let g:options = [['cmdheight', '1', '', '1', '2', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001405 setglobal cmdheight=2
1406 call assert_equal([], g:options)
1407 call assert_equal(g:opt[0], g:opt[1])
1408
1409 " 23b: Setting local number global option"
1410 noa setglobal cmdheight=8 " Reset global and local value (without triggering autocmd)
1411 noa setlocal cmdheight=1 " Sets the global(!) value!
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001412 let g:options = [['cmdheight', '1', '1', '', '2', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001413 setlocal cmdheight=2
1414 call assert_equal([], g:options)
1415 call assert_equal(g:opt[0], g:opt[1])
1416
1417 " 23c: Setting again number global option"
1418 noa setglobal cmdheight=8 " Reset global and local value (without triggering autocmd)
1419 noa setlocal cmdheight=1 " Sets the global(!) value!
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001420 let g:options = [['cmdheight', '1', '1', '1', '2', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001421 set cmdheight=2
1422 call assert_equal([], g:options)
1423 call assert_equal(g:opt[0], g:opt[1])
1424
1425 " 23d: Setting again number global option"
1426 noa set cmdheight=8 " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001427 let g:options = [['cmdheight', '8', '8', '8', '2', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001428 set cmdheight=2
1429 call assert_equal([], g:options)
1430 call assert_equal(g:opt[0], g:opt[1])
1431
1432
1433 " 24a: Setting global number global-local (to buffer) option"
1434 noa setglobal undolevels=8 " Reset global and local value (without triggering autocmd)
1435 noa setlocal undolevels=1
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001436 let g:options = [['undolevels', '8', '', '8', '2', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001437 setglobal undolevels=2
1438 call assert_equal([], g:options)
1439 call assert_equal(g:opt[0], g:opt[1])
1440
1441 " 24b: Setting local number global-local (to buffer) option"
1442 noa setglobal undolevels=8 " Reset global and local value (without triggering autocmd)
1443 noa setlocal undolevels=1
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001444 let g:options = [['undolevels', '1', '1', '', '2', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001445 setlocal undolevels=2
1446 call assert_equal([], g:options)
1447 call assert_equal(g:opt[0], g:opt[1])
1448
1449 " 24c: Setting again number global-local (to buffer) option"
1450 noa setglobal undolevels=8 " Reset global and local value (without triggering autocmd)
1451 noa setlocal undolevels=1
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001452 let g:options = [['undolevels', '1', '1', '8', '2', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001453 set undolevels=2
1454 call assert_equal([], g:options)
1455 call assert_equal(g:opt[0], g:opt[1])
1456
1457 " 24d: Setting again global number global-local (to buffer) option"
1458 noa set undolevels=8 " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001459 let g:options = [['undolevels', '8', '8', '8', '2', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001460 set undolevels=2
1461 call assert_equal([], g:options)
1462 call assert_equal(g:opt[0], g:opt[1])
1463
1464
1465 " 25a: Setting global number local (to buffer) option"
1466 noa setglobal wrapmargin=8 " Reset global and local value (without triggering autocmd)
1467 noa setlocal wrapmargin=1
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001468 let g:options = [['wrapmargin', '8', '', '8', '2', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001469 setglobal wrapmargin=2
1470 call assert_equal([], g:options)
1471 call assert_equal(g:opt[0], g:opt[1])
1472
1473 " 25b: Setting local number local (to buffer) option"
1474 noa setglobal wrapmargin=8 " Reset global and local value (without triggering autocmd)
1475 noa setlocal wrapmargin=1
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001476 let g:options = [['wrapmargin', '1', '1', '', '2', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001477 setlocal wrapmargin=2
1478 call assert_equal([], g:options)
1479 call assert_equal(g:opt[0], g:opt[1])
1480
1481 " 25c: Setting again number local (to buffer) option"
1482 noa setglobal wrapmargin=8 " Reset global and local value (without triggering autocmd)
1483 noa setlocal wrapmargin=1
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001484 let g:options = [['wrapmargin', '1', '1', '8', '2', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001485 set wrapmargin=2
1486 call assert_equal([], g:options)
1487 call assert_equal(g:opt[0], g:opt[1])
1488
1489 " 25d: Setting again global number local (to buffer) option"
1490 noa set wrapmargin=8 " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001491 let g:options = [['wrapmargin', '8', '8', '8', '2', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001492 set wrapmargin=2
1493 call assert_equal([], g:options)
1494 call assert_equal(g:opt[0], g:opt[1])
1495
1496
1497 " 26: Setting number global-local (to window) option.
1498 " Such option does currently not exist.
1499
1500
1501 " 27a: Setting global number local (to window) option"
1502 noa setglobal foldcolumn=8 " Reset global and local value (without triggering autocmd)
1503 noa setlocal foldcolumn=1
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001504 let g:options = [['foldcolumn', '8', '', '8', '2', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001505 setglobal foldcolumn=2
1506 call assert_equal([], g:options)
1507 call assert_equal(g:opt[0], g:opt[1])
1508
1509 " 27b: Setting local number local (to window) option"
1510 noa setglobal foldcolumn=8 " Reset global and local value (without triggering autocmd)
1511 noa setlocal foldcolumn=1
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001512 let g:options = [['foldcolumn', '1', '1', '', '2', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001513 setlocal foldcolumn=2
1514 call assert_equal([], g:options)
1515 call assert_equal(g:opt[0], g:opt[1])
1516
1517 " 27c: Setting again number local (to window) option"
1518 noa setglobal foldcolumn=8 " Reset global and local value (without triggering autocmd)
1519 noa setlocal foldcolumn=1
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001520 let g:options = [['foldcolumn', '1', '1', '8', '2', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001521 set foldcolumn=2
1522 call assert_equal([], g:options)
1523 call assert_equal(g:opt[0], g:opt[1])
1524
zeertzjqb811de52021-10-21 10:50:44 +01001525 " 27d: Setting again global number local (to window) option"
Bram Moolenaard7c96872019-06-15 17:12:48 +02001526 noa set foldcolumn=8 " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001527 let g:options = [['foldcolumn', '8', '8', '8', '2', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001528 set foldcolumn=2
1529 call assert_equal([], g:options)
1530 call assert_equal(g:opt[0], g:opt[1])
1531
1532
1533 " 28a: Setting global boolean global option"
1534 noa setglobal nowrapscan " Reset global and local value (without triggering autocmd)
1535 noa setlocal wrapscan " Sets the global(!) value!
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001536 let g:options = [['wrapscan', '1', '', '1', '0', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001537 setglobal nowrapscan
1538 call assert_equal([], g:options)
1539 call assert_equal(g:opt[0], g:opt[1])
1540
1541 " 28b: Setting local boolean global option"
1542 noa setglobal nowrapscan " Reset global and local value (without triggering autocmd)
1543 noa setlocal wrapscan " Sets the global(!) value!
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001544 let g:options = [['wrapscan', '1', '1', '', '0', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001545 setlocal nowrapscan
1546 call assert_equal([], g:options)
1547 call assert_equal(g:opt[0], g:opt[1])
1548
1549 " 28c: Setting again boolean global option"
1550 noa setglobal nowrapscan " Reset global and local value (without triggering autocmd)
1551 noa setlocal wrapscan " Sets the global(!) value!
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001552 let g:options = [['wrapscan', '1', '1', '1', '0', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001553 set nowrapscan
1554 call assert_equal([], g:options)
1555 call assert_equal(g:opt[0], g:opt[1])
1556
1557 " 28d: Setting again global boolean global option"
1558 noa set nowrapscan " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001559 let g:options = [['wrapscan', '0', '0', '0', '1', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001560 set wrapscan
1561 call assert_equal([], g:options)
1562 call assert_equal(g:opt[0], g:opt[1])
1563
1564
1565 " 29a: Setting global boolean global-local (to buffer) option"
1566 noa setglobal noautoread " Reset global and local value (without triggering autocmd)
1567 noa setlocal autoread
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001568 let g:options = [['autoread', '0', '', '0', '1', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001569 setglobal autoread
1570 call assert_equal([], g:options)
1571 call assert_equal(g:opt[0], g:opt[1])
1572
1573 " 29b: Setting local boolean global-local (to buffer) option"
1574 noa setglobal noautoread " Reset global and local value (without triggering autocmd)
1575 noa setlocal autoread
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001576 let g:options = [['autoread', '1', '1', '', '0', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001577 setlocal noautoread
1578 call assert_equal([], g:options)
1579 call assert_equal(g:opt[0], g:opt[1])
1580
1581 " 29c: Setting again boolean global-local (to buffer) option"
1582 noa setglobal noautoread " Reset global and local value (without triggering autocmd)
1583 noa setlocal autoread
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001584 let g:options = [['autoread', '1', '1', '0', '1', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001585 set autoread
1586 call assert_equal([], g:options)
1587 call assert_equal(g:opt[0], g:opt[1])
1588
1589 " 29d: Setting again global boolean global-local (to buffer) option"
1590 noa set noautoread " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001591 let g:options = [['autoread', '0', '0', '0', '1', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001592 set autoread
1593 call assert_equal([], g:options)
1594 call assert_equal(g:opt[0], g:opt[1])
1595
1596
1597 " 30a: Setting global boolean local (to buffer) option"
1598 noa setglobal nocindent " Reset global and local value (without triggering autocmd)
1599 noa setlocal cindent
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001600 let g:options = [['cindent', '0', '', '0', '1', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001601 setglobal cindent
1602 call assert_equal([], g:options)
1603 call assert_equal(g:opt[0], g:opt[1])
1604
1605 " 30b: Setting local boolean local (to buffer) option"
1606 noa setglobal nocindent " Reset global and local value (without triggering autocmd)
1607 noa setlocal cindent
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001608 let g:options = [['cindent', '1', '1', '', '0', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001609 setlocal nocindent
1610 call assert_equal([], g:options)
1611 call assert_equal(g:opt[0], g:opt[1])
1612
1613 " 30c: Setting again boolean local (to buffer) option"
1614 noa setglobal nocindent " Reset global and local value (without triggering autocmd)
1615 noa setlocal cindent
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001616 let g:options = [['cindent', '1', '1', '0', '1', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001617 set cindent
1618 call assert_equal([], g:options)
1619 call assert_equal(g:opt[0], g:opt[1])
1620
1621 " 30d: Setting again global boolean local (to buffer) option"
1622 noa set nocindent " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001623 let g:options = [['cindent', '0', '0', '0', '1', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001624 set cindent
1625 call assert_equal([], g:options)
1626 call assert_equal(g:opt[0], g:opt[1])
1627
1628
1629 " 31: Setting boolean global-local (to window) option
1630 " Currently no such option exists.
1631
1632
1633 " 32a: Setting global boolean local (to window) option"
1634 noa setglobal nocursorcolumn " Reset global and local value (without triggering autocmd)
1635 noa setlocal cursorcolumn
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001636 let g:options = [['cursorcolumn', '0', '', '0', '1', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001637 setglobal cursorcolumn
1638 call assert_equal([], g:options)
1639 call assert_equal(g:opt[0], g:opt[1])
1640
1641 " 32b: Setting local boolean local (to window) option"
1642 noa setglobal nocursorcolumn " Reset global and local value (without triggering autocmd)
1643 noa setlocal cursorcolumn
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001644 let g:options = [['cursorcolumn', '1', '1', '', '0', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001645 setlocal nocursorcolumn
1646 call assert_equal([], g:options)
1647 call assert_equal(g:opt[0], g:opt[1])
1648
1649 " 32c: Setting again boolean local (to window) option"
1650 noa setglobal nocursorcolumn " Reset global and local value (without triggering autocmd)
1651 noa setlocal cursorcolumn
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001652 let g:options = [['cursorcolumn', '1', '1', '0', '1', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001653 set cursorcolumn
1654 call assert_equal([], g:options)
1655 call assert_equal(g:opt[0], g:opt[1])
1656
1657 " 32d: Setting again global boolean local (to window) option"
1658 noa set nocursorcolumn " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001659 let g:options = [['cursorcolumn', '0', '0', '0', '1', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001660 set cursorcolumn
1661 call assert_equal([], g:options)
1662 call assert_equal(g:opt[0], g:opt[1])
1663
1664
Bram Moolenaar1bc353b2019-09-01 14:45:28 +02001665 " 33: Test autocommands when an option value is converted internally.
Bram Moolenaard7c96872019-06-15 17:12:48 +02001666 noa set backspace=1 " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001667 let g:options = [['backspace', 'indent,eol', 'indent,eol', 'indent,eol', '2', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001668 set backspace=2
1669 call assert_equal([], g:options)
1670 call assert_equal(g:opt[0], g:opt[1])
1671
1672
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001673 " Cleanup
1674 au! OptionSet
Bram Moolenaar0331faf2019-06-15 18:40:37 +02001675 " set tags&
Bram Moolenaard7c96872019-06-15 17:12:48 +02001676 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 +02001677 exe printf(":set %s&vim", opt)
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001678 endfor
1679 call test_override('starting', 0)
1680 delfunc! AutoCommandOptionSet
1681endfunc
1682
1683func Test_OptionSet_diffmode()
1684 call test_override('starting', 1)
Bram Moolenaar26d98212019-01-27 22:32:55 +01001685 " 18: Changing an option when entering diff mode
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001686 new
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001687 au OptionSet diff :let &l:cul = v:option_new
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001688
1689 call setline(1, ['buffer 1', 'line2', 'line3', 'line4'])
1690 call assert_equal(0, &l:cul)
1691 diffthis
1692 call assert_equal(1, &l:cul)
1693
1694 vnew
1695 call setline(1, ['buffer 2', 'line 2', 'line 3', 'line4'])
1696 call assert_equal(0, &l:cul)
1697 diffthis
1698 call assert_equal(1, &l:cul)
1699
1700 diffoff
1701 call assert_equal(0, &l:cul)
1702 call assert_equal(1, getwinvar(2, '&l:cul'))
1703 bw!
1704
1705 call assert_equal(1, &l:cul)
1706 diffoff!
1707 call assert_equal(0, &l:cul)
1708 call assert_equal(0, getwinvar(1, '&l:cul'))
1709 bw!
1710
1711 " Cleanup
1712 au! OptionSet
1713 call test_override('starting', 0)
1714endfunc
1715
1716func Test_OptionSet_diffmode_close()
1717 call test_override('starting', 1)
1718 " 19: Try to close the current window when entering diff mode
1719 " should not segfault
1720 new
1721 au OptionSet diff close
1722
1723 call setline(1, ['buffer 1', 'line2', 'line3', 'line4'])
Bram Moolenaare2e40752020-09-04 21:18:46 +02001724 call assert_fails(':diffthis', 'E788:')
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001725 call assert_equal(1, &diff)
1726 vnew
1727 call setline(1, ['buffer 2', 'line 2', 'line 3', 'line4'])
Bram Moolenaare2e40752020-09-04 21:18:46 +02001728 call assert_fails(':diffthis', 'E788:')
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001729 call assert_equal(1, &diff)
Bram Moolenaara9aa86f2019-11-10 21:25:45 +01001730 set diffopt-=closeoff
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001731 bw!
Bram Moolenaare2e40752020-09-04 21:18:46 +02001732 call assert_fails(':diffoff!', 'E788:')
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001733 bw!
1734
1735 " Cleanup
1736 au! OptionSet
1737 call test_override('starting', 0)
1738 "delfunc! AutoCommandOptionSet
1739endfunc
Bram Moolenaar4a137b42017-08-04 22:37:11 +02001740
1741" Test for Bufleave autocommand that deletes the buffer we are about to edit.
1742func Test_BufleaveWithDelete()
Bram Moolenaare7cda972022-08-29 11:02:59 +01001743 new | edit XbufLeave1
Bram Moolenaar4a137b42017-08-04 22:37:11 +02001744
1745 augroup test_bufleavewithdelete
1746 autocmd!
Bram Moolenaare7cda972022-08-29 11:02:59 +01001747 autocmd BufLeave XbufLeave1 bwipe XbufLeave2
Bram Moolenaar4a137b42017-08-04 22:37:11 +02001748 augroup END
1749
Bram Moolenaare7cda972022-08-29 11:02:59 +01001750 call assert_fails('edit XbufLeave2', 'E143:')
1751 call assert_equal('XbufLeave1', bufname('%'))
Bram Moolenaar4a137b42017-08-04 22:37:11 +02001752
Bram Moolenaare7cda972022-08-29 11:02:59 +01001753 autocmd! test_bufleavewithdelete BufLeave XbufLeave1
Bram Moolenaar4a137b42017-08-04 22:37:11 +02001754 augroup! test_bufleavewithdelete
1755
1756 new
Bram Moolenaare7cda972022-08-29 11:02:59 +01001757 bwipe! XbufLeave1
Bram Moolenaar4a137b42017-08-04 22:37:11 +02001758endfunc
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +02001759
1760" Test for autocommand that changes the buffer list, when doing ":ball".
1761func Test_Acmd_BufAll()
1762 enew!
1763 %bwipe!
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01001764 call writefile(['Test file Xxx1'], 'Xxx1', 'D')
1765 call writefile(['Test file Xxx2'], 'Xxx2', 'D')
1766 call writefile(['Test file Xxx3'], 'Xxx3', 'D')
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +02001767
1768 " Add three files to the buffer list
1769 split Xxx1
1770 close
1771 split Xxx2
1772 close
1773 split Xxx3
1774 close
1775
1776 " Wipe the buffer when the buffer is opened
1777 au BufReadPost Xxx2 bwipe
1778
1779 call append(0, 'Test file Xxx4')
1780 ball
1781
1782 call assert_equal(2, winnr('$'))
1783 call assert_equal('Xxx1', bufname(winbufnr(winnr('$'))))
1784 wincmd t
1785
1786 au! BufReadPost
1787 %bwipe!
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +02001788 enew! | only
1789endfunc
1790
1791" Test for autocommand that changes current buffer on BufEnter event.
1792" Check if modelines are interpreted for the correct buffer.
1793func Test_Acmd_BufEnter()
1794 %bwipe!
1795 call writefile(['start of test file Xxx1',
1796 \ "\<Tab>this is a test",
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01001797 \ 'end of test file Xxx1'], 'Xxx1', 'D')
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +02001798 call writefile(['start of test file Xxx2',
1799 \ 'vim: set noai :',
1800 \ "\<Tab>this is a test",
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01001801 \ 'end of test file Xxx2'], 'Xxx2', 'D')
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +02001802
1803 au BufEnter Xxx2 brew
1804 set ai modeline modelines=3
1805 edit Xxx1
1806 " edit Xxx2, autocmd will do :brew
1807 edit Xxx2
1808 exe "normal G?this is a\<CR>"
1809 " Append text with autoindent to this file
1810 normal othis should be auto-indented
1811 call assert_equal("\<Tab>this should be auto-indented", getline('.'))
1812 call assert_equal(3, line('.'))
1813 " Remove autocmd and edit Xxx2 again
1814 au! BufEnter Xxx2
1815 buf! Xxx2
1816 exe "normal G?this is a\<CR>"
1817 " append text without autoindent to Xxx
1818 normal othis should be in column 1
1819 call assert_equal("this should be in column 1", getline('.'))
1820 call assert_equal(4, line('.'))
1821
1822 %bwipe!
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +02001823 set ai&vim modeline&vim modelines&vim
1824endfunc
1825
1826" Test for issue #57
1827" do not move cursor on <c-o> when autoindent is set
1828func Test_ai_CTRL_O()
1829 enew!
1830 set ai
1831 let save_fo = &fo
1832 set fo+=r
1833 exe "normal o# abcdef\<Esc>2hi\<CR>\<C-O>d0\<Esc>"
1834 exe "normal o# abcdef\<Esc>2hi\<C-O>d0\<Esc>"
1835 call assert_equal(['# abc', 'def', 'def'], getline(2, 4))
1836
1837 set ai&vim
1838 let &fo = save_fo
1839 enew!
1840endfunc
1841
1842" Test for autocommand that deletes the current buffer on BufLeave event.
1843" Also test deleting the last buffer, should give a new, empty buffer.
1844func Test_BufLeave_Wipe()
1845 %bwipe!
1846 let content = ['start of test file Xxx',
1847 \ 'this is a test',
1848 \ 'end of test file Xxx']
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01001849 call writefile(content, 'Xxx1', 'D')
1850 call writefile(content, 'Xxx2', 'D')
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +02001851
1852 au BufLeave Xxx2 bwipe
1853 edit Xxx1
1854 split Xxx2
1855 " delete buffer Xxx2, we should be back to Xxx1
1856 bwipe
1857 call assert_equal('Xxx1', bufname('%'))
1858 call assert_equal(1, winnr('$'))
1859
1860 " Create an alternate buffer
1861 %write! test.out
1862 call assert_equal('test.out', bufname('#'))
1863 " delete alternate buffer
1864 bwipe test.out
1865 call assert_equal('Xxx1', bufname('%'))
1866 call assert_equal('', bufname('#'))
1867
1868 au BufLeave Xxx1 bwipe
1869 " delete current buffer, get an empty one
1870 bwipe!
1871 call assert_equal(1, line('$'))
1872 call assert_equal('', bufname('%'))
Bram Moolenaarb2c87502017-10-14 21:15:58 +02001873 let g:bufinfo = getbufinfo()
1874 call assert_equal(1, len(g:bufinfo))
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +02001875
Bram Moolenaar53f0c962017-10-22 14:23:59 +02001876 call delete('test.out')
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +02001877 %bwipe
1878 au! BufLeave
Bram Moolenaarb2c87502017-10-14 21:15:58 +02001879
1880 " check that bufinfo doesn't contain a pointer to freed memory
1881 call test_garbagecollect_now()
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +02001882endfunc
Bram Moolenaar87ffb5c2017-10-19 12:37:42 +02001883
1884func Test_QuitPre()
1885 edit Xfoo
1886 let winid = win_getid(winnr())
1887 split Xbar
1888 au! QuitPre * let g:afile = expand('<afile>')
1889 " Close the other window, <afile> should be correct.
1890 exe win_id2win(winid) . 'q'
1891 call assert_equal('Xfoo', g:afile)
LemonBoy66e13ae2022-04-21 22:52:11 +01001892
Bram Moolenaar87ffb5c2017-10-19 12:37:42 +02001893 unlet g:afile
1894 bwipe Xfoo
1895 bwipe Xbar
1896endfunc
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02001897
1898func Test_Cmdline()
Bram Moolenaar153b7042018-01-31 15:48:32 +01001899 au! CmdlineChanged : let g:text = getcmdline()
1900 let g:text = 0
1901 call feedkeys(":echom 'hello'\<CR>", 'xt')
1902 call assert_equal("echom 'hello'", g:text)
1903 au! CmdlineChanged
1904
1905 au! CmdlineChanged : let g:entered = expand('<afile>')
1906 let g:entered = 0
1907 call feedkeys(":echom 'hello'\<CR>", 'xt')
1908 call assert_equal(':', g:entered)
1909 au! CmdlineChanged
1910
Bram Moolenaarbb393d82022-12-09 12:21:50 +00001911 let g:log = []
1912 cnoremap <F1> <Cmd>call setcmdline('ls')<CR>
1913 autocmd CmdlineChanged : let g:log += [getcmdline()]
1914 call feedkeys(":\<F1>", 'xt')
1915 call assert_equal(['ls'], g:log)
1916 unlet g:log
1917 au! CmdlineChanged
1918 cunmap <F1>
1919
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02001920 au! CmdlineEnter : let g:entered = expand('<afile>')
1921 au! CmdlineLeave : let g:left = expand('<afile>')
1922 let g:entered = 0
1923 let g:left = 0
1924 call feedkeys(":echo 'hello'\<CR>", 'xt')
1925 call assert_equal(':', g:entered)
1926 call assert_equal(':', g:left)
1927 au! CmdlineEnter
1928 au! CmdlineLeave
1929
Bram Moolenaara4baf5b2018-04-22 13:27:44 +02001930 let save_shellslash = &shellslash
1931 set noshellslash
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02001932 au! CmdlineEnter / let g:entered = expand('<afile>')
1933 au! CmdlineLeave / let g:left = expand('<afile>')
1934 let g:entered = 0
1935 let g:left = 0
Bram Moolenaar53f0c962017-10-22 14:23:59 +02001936 new
1937 call setline(1, 'hello')
1938 call feedkeys("/hello\<CR>", 'xt')
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02001939 call assert_equal('/', g:entered)
1940 call assert_equal('/', g:left)
Bram Moolenaar53f0c962017-10-22 14:23:59 +02001941 bwipe!
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02001942 au! CmdlineEnter
1943 au! CmdlineLeave
Bram Moolenaara4baf5b2018-04-22 13:27:44 +02001944 let &shellslash = save_shellslash
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02001945endfunc
Bram Moolenaar53f0c962017-10-22 14:23:59 +02001946
1947" Test for BufWritePre autocommand that deletes or unloads the buffer.
1948func Test_BufWritePre()
1949 %bwipe
1950 au BufWritePre Xxx1 bunload
1951 au BufWritePre Xxx2 bwipe
1952
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01001953 call writefile(['start of Xxx1', 'test', 'end of Xxx1'], 'Xxx1', 'D')
1954 call writefile(['start of Xxx2', 'test', 'end of Xxx2'], 'Xxx2', 'D')
Bram Moolenaar53f0c962017-10-22 14:23:59 +02001955
1956 edit Xtest
1957 e! Xxx2
1958 bdel Xtest
1959 e Xxx1
1960 " write it, will unload it and give an error msg
Bram Moolenaare2e40752020-09-04 21:18:46 +02001961 call assert_fails('w', 'E203:')
Bram Moolenaar53f0c962017-10-22 14:23:59 +02001962 call assert_equal('Xxx2', bufname('%'))
1963 edit Xtest
1964 e! Xxx2
1965 bwipe Xtest
1966 " write it, will delete the buffer and give an error msg
Bram Moolenaare2e40752020-09-04 21:18:46 +02001967 call assert_fails('w', 'E203:')
Bram Moolenaar53f0c962017-10-22 14:23:59 +02001968 call assert_equal('Xxx1', bufname('%'))
1969 au! BufWritePre
Bram Moolenaar53f0c962017-10-22 14:23:59 +02001970endfunc
1971
1972" Test for BufUnload autocommand that unloads all the other buffers
1973func Test_bufunload_all()
Bram Moolenaarf08b0eb2021-10-16 13:00:14 +01001974 let g:test_is_flaky = 1
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01001975 call writefile(['Test file Xxx1'], 'Xxx1', 'D')"
1976 call writefile(['Test file Xxx2'], 'Xxx2', 'D')"
Bram Moolenaar53f0c962017-10-22 14:23:59 +02001977
Bram Moolenaarc79745a2019-05-20 22:12:34 +02001978 let content =<< trim [CODE]
1979 func UnloadAllBufs()
1980 let i = 1
1981 while i <= bufnr('$')
1982 if i != bufnr('%') && bufloaded(i)
1983 exe i . 'bunload'
1984 endif
1985 let i += 1
1986 endwhile
1987 endfunc
1988 au BufUnload * call UnloadAllBufs()
1989 au VimLeave * call writefile(['Test Finished'], 'Xout')
1990 edit Xxx1
1991 split Xxx2
1992 q
1993 [CODE]
1994
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01001995 call writefile(content, 'Xbunloadtest', 'D')
Bram Moolenaar53f0c962017-10-22 14:23:59 +02001996
1997 call delete('Xout')
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01001998 call system(GetVimCommandClean() .. ' -N --not-a-term -S Xbunloadtest')
Bram Moolenaar53f0c962017-10-22 14:23:59 +02001999 call assert_true(filereadable('Xout'))
2000
Bram Moolenaar53f0c962017-10-22 14:23:59 +02002001 call delete('Xout')
2002endfunc
2003
2004" Some tests for buffer-local autocommands
2005func Test_buflocal_autocmd()
2006 let g:bname = ''
2007 edit xx
2008 au BufLeave <buffer> let g:bname = expand("%")
2009 " here, autocommand for xx should trigger.
2010 " but autocommand shall not apply to buffer named <buffer>.
2011 edit somefile
2012 call assert_equal('xx', g:bname)
2013 let g:bname = ''
2014 " here, autocommand shall be auto-deleted
2015 bwipe xx
2016 " autocmd should not trigger
2017 edit xx
2018 call assert_equal('', g:bname)
2019 " autocmd should not trigger
2020 edit somefile
2021 call assert_equal('', g:bname)
2022 enew
2023 unlet g:bname
2024endfunc
Bram Moolenaar430dc5d2017-11-02 21:04:47 +01002025
2026" Test for "*Cmd" autocommands
2027func Test_Cmd_Autocmds()
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01002028 call writefile(['start of Xxx', "\tabc2", 'end of Xxx'], 'Xxx', 'D')
Bram Moolenaar430dc5d2017-11-02 21:04:47 +01002029
2030 enew!
2031 au BufReadCmd XtestA 0r Xxx|$del
2032 edit XtestA " will read text of Xxd instead
2033 call assert_equal('start of Xxx', getline(1))
2034
2035 au BufWriteCmd XtestA call append(line("$"), "write")
2036 write " will append a line to the file
2037 call assert_equal('write', getline('$'))
Bram Moolenaare2e40752020-09-04 21:18:46 +02002038 call assert_fails('read XtestA', 'E484:') " should not read anything
Bram Moolenaar430dc5d2017-11-02 21:04:47 +01002039 call assert_equal('write', getline(4))
2040
2041 " now we have:
2042 " 1 start of Xxx
2043 " 2 abc2
2044 " 3 end of Xxx
2045 " 4 write
2046
2047 au FileReadCmd XtestB '[r Xxx
2048 2r XtestB " will read Xxx below line 2 instead
2049 call assert_equal('start of Xxx', getline(3))
2050
2051 " now we have:
2052 " 1 start of Xxx
2053 " 2 abc2
2054 " 3 start of Xxx
2055 " 4 abc2
2056 " 5 end of Xxx
2057 " 6 end of Xxx
2058 " 7 write
2059
2060 au FileWriteCmd XtestC '[,']copy $
2061 normal 4GA1
2062 4,5w XtestC " will copy lines 4 and 5 to the end
2063 call assert_equal("\tabc21", getline(8))
Bram Moolenaare2e40752020-09-04 21:18:46 +02002064 call assert_fails('r XtestC', 'E484:') " should not read anything
Bram Moolenaar430dc5d2017-11-02 21:04:47 +01002065 call assert_equal("end of Xxx", getline(9))
2066
2067 " now we have:
2068 " 1 start of Xxx
2069 " 2 abc2
2070 " 3 start of Xxx
2071 " 4 abc21
2072 " 5 end of Xxx
2073 " 6 end of Xxx
2074 " 7 write
2075 " 8 abc21
2076 " 9 end of Xxx
2077
2078 let g:lines = []
2079 au FileAppendCmd XtestD call extend(g:lines, getline(line("'["), line("']")))
2080 w >>XtestD " will add lines to 'lines'
2081 call assert_equal(9, len(g:lines))
Bram Moolenaare2e40752020-09-04 21:18:46 +02002082 call assert_fails('$r XtestD', 'E484:') " should not read anything
Bram Moolenaar430dc5d2017-11-02 21:04:47 +01002083 call assert_equal(9, line('$'))
2084 call assert_equal('end of Xxx', getline('$'))
2085
2086 au BufReadCmd XtestE 0r Xxx|$del
2087 sp XtestE " split window with test.out
2088 call assert_equal('end of Xxx', getline(3))
2089
2090 let g:lines = []
2091 exe "normal 2Goasdf\<Esc>\<C-W>\<C-W>"
2092 au BufWriteCmd XtestE call extend(g:lines, getline(0, '$'))
2093 wall " will write other window to 'lines'
2094 call assert_equal(4, len(g:lines), g:lines)
2095 call assert_equal('asdf', g:lines[2])
2096
2097 au! BufReadCmd
2098 au! BufWriteCmd
2099 au! FileReadCmd
2100 au! FileWriteCmd
2101 au! FileAppendCmd
2102 %bwipe!
Bram Moolenaar430dc5d2017-11-02 21:04:47 +01002103 enew!
2104endfunc
Bram Moolenaaraace2152017-11-05 16:23:10 +01002105
Bram Moolenaar0fff4412020-03-29 16:06:29 +02002106func s:ReadFile()
2107 setl noswapfile nomodified
2108 let filename = resolve(expand("<afile>:p"))
2109 execute 'read' fnameescape(filename)
2110 1d_
2111 exe 'file' fnameescape(filename)
2112 setl buftype=acwrite
2113endfunc
2114
2115func s:WriteFile()
2116 let filename = resolve(expand("<afile>:p"))
2117 setl buftype=
2118 noautocmd execute 'write' fnameescape(filename)
2119 setl buftype=acwrite
2120 setl nomodified
2121endfunc
2122
2123func Test_BufReadCmd()
2124 autocmd BufReadCmd *.test call s:ReadFile()
2125 autocmd BufWriteCmd *.test call s:WriteFile()
2126
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01002127 call writefile(['one', 'two', 'three'], 'Xcmd.test', 'D')
Bram Moolenaar0fff4412020-03-29 16:06:29 +02002128 edit Xcmd.test
2129 call assert_match('Xcmd.test" line 1 of 3', execute('file'))
2130 normal! Gofour
2131 write
2132 call assert_equal(['one', 'two', 'three', 'four'], readfile('Xcmd.test'))
2133
2134 bwipe!
Bram Moolenaar0fff4412020-03-29 16:06:29 +02002135 au! BufReadCmd
2136 au! BufWriteCmd
2137endfunc
2138
zeertzjq9c8f9462022-08-30 18:17:15 +01002139func Test_BufWriteCmd()
2140 autocmd BufWriteCmd Xbufwritecmd let g:written = 1
2141 new
2142 file Xbufwritecmd
2143 set buftype=acwrite
Bram Moolenaar6f14da12022-09-07 21:30:44 +01002144 call mkdir('Xbufwritecmd', 'D')
zeertzjq9c8f9462022-08-30 18:17:15 +01002145 write
2146 " BufWriteCmd should be triggered even if a directory has the same name
2147 call assert_equal(1, g:written)
zeertzjq9c8f9462022-08-30 18:17:15 +01002148 unlet g:written
2149 au! BufWriteCmd
2150 bwipe!
2151endfunc
2152
Bram Moolenaaraace2152017-11-05 16:23:10 +01002153func SetChangeMarks(start, end)
Bram Moolenaar97c69432021-01-15 16:45:21 +01002154 exe a:start .. 'mark ['
2155 exe a:end .. 'mark ]'
Bram Moolenaaraace2152017-11-05 16:23:10 +01002156endfunc
2157
2158" Verify the effects of autocmds on '[ and ']
2159func Test_change_mark_in_autocmds()
2160 edit! Xtest
Bram Moolenaar97c69432021-01-15 16:45:21 +01002161 call feedkeys("ia\<CR>b\<CR>c\<CR>d\<C-g>u\<Esc>", 'xtn')
Bram Moolenaaraace2152017-11-05 16:23:10 +01002162
2163 call SetChangeMarks(2, 3)
2164 write
2165 call assert_equal([1, 4], [line("'["), line("']")])
2166
2167 call SetChangeMarks(2, 3)
2168 au BufWritePre * call assert_equal([1, 4], [line("'["), line("']")])
2169 write
2170 au! BufWritePre
2171
Bram Moolenaar14ddd222020-08-05 12:02:40 +02002172 if has('unix')
Bram Moolenaaraace2152017-11-05 16:23:10 +01002173 write XtestFilter
2174 write >> XtestFilter
2175
2176 call SetChangeMarks(2, 3)
2177 " Marks are set to the entire range of the write
2178 au FilterWritePre * call assert_equal([1, 4], [line("'["), line("']")])
2179 " '[ is adjusted to just before the line that will receive the filtered
2180 " data
2181 au FilterReadPre * call assert_equal([4, 4], [line("'["), line("']")])
2182 " The filtered data is read into the buffer, and the source lines are
2183 " still present, so the range is after the source lines
2184 au FilterReadPost * call assert_equal([5, 12], [line("'["), line("']")])
2185 %!cat XtestFilter
2186 " After the filtered data is read, the original lines are deleted
2187 call assert_equal([1, 8], [line("'["), line("']")])
2188 au! FilterWritePre,FilterReadPre,FilterReadPost
2189 undo
2190
2191 call SetChangeMarks(1, 4)
2192 au FilterWritePre * call assert_equal([2, 3], [line("'["), line("']")])
2193 au FilterReadPre * call assert_equal([3, 3], [line("'["), line("']")])
2194 au FilterReadPost * call assert_equal([4, 11], [line("'["), line("']")])
2195 2,3!cat XtestFilter
2196 call assert_equal([2, 9], [line("'["), line("']")])
2197 au! FilterWritePre,FilterReadPre,FilterReadPost
2198 undo
2199
2200 call delete('XtestFilter')
2201 endif
2202
2203 call SetChangeMarks(1, 4)
2204 au FileWritePre * call assert_equal([2, 3], [line("'["), line("']")])
2205 2,3write Xtest2
2206 au! FileWritePre
2207
2208 call SetChangeMarks(2, 3)
2209 au FileAppendPre * call assert_equal([1, 4], [line("'["), line("']")])
2210 write >> Xtest2
2211 au! FileAppendPre
2212
2213 call SetChangeMarks(1, 4)
2214 au FileAppendPre * call assert_equal([2, 3], [line("'["), line("']")])
2215 2,3write >> Xtest2
2216 au! FileAppendPre
2217
2218 call SetChangeMarks(1, 1)
2219 au FileReadPre * call assert_equal([3, 1], [line("'["), line("']")])
2220 au FileReadPost * call assert_equal([4, 11], [line("'["), line("']")])
2221 3read Xtest2
2222 au! FileReadPre,FileReadPost
2223 undo
2224
2225 call SetChangeMarks(4, 4)
2226 " When the line is 0, it's adjusted to 1
2227 au FileReadPre * call assert_equal([1, 4], [line("'["), line("']")])
2228 au FileReadPost * call assert_equal([1, 8], [line("'["), line("']")])
2229 0read Xtest2
2230 au! FileReadPre,FileReadPost
2231 undo
2232
2233 call SetChangeMarks(4, 4)
2234 " When the line is 0, it's adjusted to 1
2235 au FileReadPre * call assert_equal([1, 4], [line("'["), line("']")])
2236 au FileReadPost * call assert_equal([2, 9], [line("'["), line("']")])
2237 1read Xtest2
2238 au! FileReadPre,FileReadPost
2239 undo
2240
2241 bwipe!
2242 call delete('Xtest')
2243 call delete('Xtest2')
2244endfunc
2245
2246func Test_Filter_noshelltemp()
Bram Moolenaaraeb313f2020-11-27 19:13:28 +01002247 CheckExecutable cat
Bram Moolenaaraace2152017-11-05 16:23:10 +01002248
2249 enew!
2250 call setline(1, ['a', 'b', 'c', 'd'])
2251
2252 let shelltemp = &shelltemp
2253 set shelltemp
2254
2255 let g:filter_au = 0
2256 au FilterWritePre * let g:filter_au += 1
2257 au FilterReadPre * let g:filter_au += 1
2258 au FilterReadPost * let g:filter_au += 1
2259 %!cat
2260 call assert_equal(3, g:filter_au)
2261
2262 if has('filterpipe')
2263 set noshelltemp
2264
2265 let g:filter_au = 0
2266 au FilterWritePre * let g:filter_au += 1
2267 au FilterReadPre * let g:filter_au += 1
2268 au FilterReadPost * let g:filter_au += 1
2269 %!cat
2270 call assert_equal(0, g:filter_au)
2271 endif
2272
2273 au! FilterWritePre,FilterReadPre,FilterReadPost
2274 let &shelltemp = shelltemp
2275 bwipe!
2276endfunc
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01002277
2278func Test_TextYankPost()
2279 enew!
2280 call setline(1, ['foo'])
2281
2282 let g:event = []
2283 au TextYankPost * let g:event = copy(v:event)
2284
2285 call assert_equal({}, v:event)
2286 call assert_fails('let v:event = {}', 'E46:')
2287 call assert_fails('let v:event.mykey = 0', 'E742:')
2288
2289 norm "ayiw
2290 call assert_equal(
Bram Moolenaara016eeb2022-04-09 11:37:38 +01002291 \ #{regcontents: ['foo'], regname: 'a', operator: 'y',
2292 \ regtype: 'v', visual: v:false, inclusive: v:true},
2293 \ g:event)
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01002294 norm y_
2295 call assert_equal(
Bram Moolenaara016eeb2022-04-09 11:37:38 +01002296 \ #{regcontents: ['foo'], regname: '', operator: 'y', regtype: 'V',
2297 \ visual: v:false, inclusive: v:false},
2298 \ g:event)
Bram Moolenaar37d16732020-06-12 22:09:01 +02002299 norm Vy
2300 call assert_equal(
Bram Moolenaara016eeb2022-04-09 11:37:38 +01002301 \ #{regcontents: ['foo'], regname: '', operator: 'y', regtype: 'V',
2302 \ visual: v:true, inclusive: v:true},
2303 \ g:event)
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01002304 call feedkeys("\<C-V>y", 'x')
2305 call assert_equal(
Bram Moolenaara016eeb2022-04-09 11:37:38 +01002306 \ #{regcontents: ['f'], regname: '', operator: 'y', regtype: "\x161",
2307 \ visual: v:true, inclusive: v:true},
2308 \ g:event)
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01002309 norm "xciwbar
2310 call assert_equal(
Bram Moolenaara016eeb2022-04-09 11:37:38 +01002311 \ #{regcontents: ['foo'], regname: 'x', operator: 'c', regtype: 'v',
2312 \ visual: v:false, inclusive: v:true},
2313 \ g:event)
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01002314 norm "bdiw
2315 call assert_equal(
Bram Moolenaara016eeb2022-04-09 11:37:38 +01002316 \ #{regcontents: ['bar'], regname: 'b', operator: 'd', regtype: 'v',
2317 \ visual: v:false, inclusive: v:true},
2318 \ g:event)
2319
2320 call setline(1, 'foobar')
2321 " exclusive motion
2322 norm $"ay0
2323 call assert_equal(
2324 \ #{regcontents: ['fooba'], regname: 'a', operator: 'y', regtype: 'v',
2325 \ visual: v:false, inclusive: v:false},
2326 \ g:event)
2327 " inclusive motion
2328 norm 0"ay$
2329 call assert_equal(
2330 \ #{regcontents: ['foobar'], regname: 'a', operator: 'y', regtype: 'v',
2331 \ visual: v:false, inclusive: v:true},
2332 \ g:event)
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01002333
2334 call assert_equal({}, v:event)
2335
Bram Moolenaarfccbf062020-11-26 20:34:00 +01002336 if has('clipboard_working') && !has('gui_running')
2337 " Test that when the visual selection is automatically copied to clipboard
2338 " register a TextYankPost is emitted
2339 call setline(1, ['foobar'])
2340
2341 let @* = ''
2342 set clipboard=autoselect
2343 exe "norm! ggviw\<Esc>"
2344 call assert_equal(
Bram Moolenaara016eeb2022-04-09 11:37:38 +01002345 \ #{regcontents: ['foobar'], regname: '*', operator: 'y',
2346 \ regtype: 'v', visual: v:true, inclusive: v:false},
2347 \ g:event)
Bram Moolenaarfccbf062020-11-26 20:34:00 +01002348
2349 let @+ = ''
2350 set clipboard=autoselectplus
2351 exe "norm! ggviw\<Esc>"
2352 call assert_equal(
Bram Moolenaara016eeb2022-04-09 11:37:38 +01002353 \ #{regcontents: ['foobar'], regname: '+', operator: 'y',
2354 \ regtype: 'v', visual: v:true, inclusive: v:false},
2355 \ g:event)
Bram Moolenaarfccbf062020-11-26 20:34:00 +01002356
2357 set clipboard&vim
2358 endif
2359
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01002360 au! TextYankPost
2361 unlet g:event
2362 bwipe!
2363endfunc
Bram Moolenaar9bca8052017-12-18 12:37:55 +01002364
Bram Moolenaar9a046fd2021-01-28 13:47:59 +01002365func Test_autocommand_all_events()
2366 call assert_fails('au * * bwipe', 'E1155:')
2367 call assert_fails('au * x bwipe', 'E1155:')
Bram Moolenaarb6db1462021-12-24 19:24:47 +00002368 call assert_fails('au! * x bwipe', 'E1155:')
Bram Moolenaar4fb921e2017-12-18 15:33:00 +01002369endfunc
Bram Moolenaarb7407d32018-02-03 17:36:27 +01002370
Bram Moolenaarf6246f52022-02-11 16:30:12 +00002371func Test_autocmd_user()
2372 au User MyEvent let s:res = [expand("<afile>"), expand("<amatch>")]
2373 doautocmd User MyEvent
2374 call assert_equal(['MyEvent', 'MyEvent'], s:res)
2375 au! User
2376 unlet s:res
2377endfunc
2378
Bram Moolenaar3b014be2022-11-13 17:53:46 +00002379func Test_autocmd_user_clear_group()
2380 CheckRunVimInTerminal
2381
2382 let lines =<< trim END
2383 autocmd! User
2384 for i in range(1, 999)
2385 exe 'autocmd User ' .. 'Foo' .. i .. ' bar'
2386 endfor
2387 au CmdlineLeave : call timer_start(0, {-> execute('autocmd! User')})
2388 END
2389 call writefile(lines, 'XautoUser', 'D')
2390 let buf = RunVimInTerminal('-S XautoUser', {'rows': 10})
2391
2392 " this was using freed memory
2393 call term_sendkeys(buf, ":autocmd User\<CR>")
2394 call TermWait(buf, 50)
2395 call term_sendkeys(buf, "G")
2396
2397 call StopVimInTerminal(buf)
2398endfunc
2399
Bram Moolenaaref2c3252022-11-25 16:31:51 +00002400func Test_autocmd_CmdlineLeave_unlet()
2401 CheckRunVimInTerminal
2402
2403 let lines =<< trim END
2404 for i in range(1, 999)
2405 exe 'let g:var' .. i '=' i
2406 endfor
2407 au CmdlineLeave : call timer_start(0, {-> execute('unlet g:var990')})
2408 END
2409 call writefile(lines, 'XleaveUnlet', 'D')
2410 let buf = RunVimInTerminal('-S XleaveUnlet', {'rows': 10})
2411
2412 " this was using freed memory
2413 call term_sendkeys(buf, ":let g:\<CR>")
2414 call TermWait(buf, 50)
2415 call term_sendkeys(buf, "G")
2416 call TermWait(buf, 50)
2417 call term_sendkeys(buf, "\<CR>") " for the hit-enter prompt
2418
2419 call StopVimInTerminal(buf)
2420endfunc
2421
Bram Moolenaarb7407d32018-02-03 17:36:27 +01002422function s:Before_test_dirchanged()
2423 augroup test_dirchanged
2424 autocmd!
2425 augroup END
2426 let s:li = []
2427 let s:dir_this = getcwd()
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02002428 let s:dir_foo = s:dir_this . '/Xfoo'
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01002429 call mkdir(s:dir_foo)
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02002430 let s:dir_bar = s:dir_this . '/Xbar'
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01002431 call mkdir(s:dir_bar)
Bram Moolenaarb7407d32018-02-03 17:36:27 +01002432endfunc
2433
2434function s:After_test_dirchanged()
Bram Moolenaar3503d7c2019-11-09 20:10:17 +01002435 call chdir(s:dir_this)
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01002436 call delete(s:dir_foo, 'd')
2437 call delete(s:dir_bar, 'd')
Bram Moolenaarb7407d32018-02-03 17:36:27 +01002438 augroup test_dirchanged
2439 autocmd!
2440 augroup END
2441endfunc
2442
2443function Test_dirchanged_global()
2444 call s:Before_test_dirchanged()
Bram Moolenaarf6246f52022-02-11 16:30:12 +00002445 autocmd test_dirchanged DirChangedPre global call add(s:li, expand("<amatch>") .. " pre cd " .. v:event.directory)
Bram Moolenaarb7407d32018-02-03 17:36:27 +01002446 autocmd test_dirchanged DirChanged global call add(s:li, "cd:")
2447 autocmd test_dirchanged DirChanged global call add(s:li, expand("<afile>"))
Bram Moolenaar3503d7c2019-11-09 20:10:17 +01002448 call chdir(s:dir_foo)
Bram Moolenaarf6246f52022-02-11 16:30:12 +00002449 let expected = ["global pre cd " .. s:dir_foo, "cd:", s:dir_foo]
Bram Moolenaar28e8f732022-02-09 12:58:20 +00002450 call assert_equal(expected, s:li)
Bram Moolenaar3503d7c2019-11-09 20:10:17 +01002451 call chdir(s:dir_foo)
Bram Moolenaar28e8f732022-02-09 12:58:20 +00002452 call assert_equal(expected, s:li)
Bram Moolenaar3503d7c2019-11-09 20:10:17 +01002453 exe 'lcd ' .. fnameescape(s:dir_bar)
Bram Moolenaar28e8f732022-02-09 12:58:20 +00002454 call assert_equal(expected, s:li)
Bram Moolenaard8c9d322022-06-12 11:49:16 +01002455
2456 exe 'cd ' .. s:dir_foo
2457 exe 'cd ' .. s:dir_bar
2458 autocmd! test_dirchanged DirChanged global let g:result = expand("<afile>")
2459 cd -
Bram Moolenaardb77c492022-06-12 23:26:50 +01002460 call assert_equal(s:dir_foo, substitute(g:result, '\\', '/', 'g'))
Bram Moolenaard8c9d322022-06-12 11:49:16 +01002461
Bram Moolenaarb7407d32018-02-03 17:36:27 +01002462 call s:After_test_dirchanged()
2463endfunc
2464
2465function Test_dirchanged_local()
2466 call s:Before_test_dirchanged()
2467 autocmd test_dirchanged DirChanged window call add(s:li, "lcd:")
2468 autocmd test_dirchanged DirChanged window call add(s:li, expand("<afile>"))
Bram Moolenaar3503d7c2019-11-09 20:10:17 +01002469 call chdir(s:dir_foo)
Bram Moolenaarb7407d32018-02-03 17:36:27 +01002470 call assert_equal([], s:li)
Bram Moolenaar3503d7c2019-11-09 20:10:17 +01002471 exe 'lcd ' .. fnameescape(s:dir_bar)
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01002472 call assert_equal(["lcd:", s:dir_bar], s:li)
Bram Moolenaar3503d7c2019-11-09 20:10:17 +01002473 exe 'lcd ' .. fnameescape(s:dir_bar)
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01002474 call assert_equal(["lcd:", s:dir_bar], s:li)
Bram Moolenaarb7407d32018-02-03 17:36:27 +01002475 call s:After_test_dirchanged()
2476endfunc
2477
2478function Test_dirchanged_auto()
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +02002479 CheckOption autochdir
Bram Moolenaarb7407d32018-02-03 17:36:27 +01002480 call s:Before_test_dirchanged()
2481 call test_autochdir()
Bram Moolenaar28e8f732022-02-09 12:58:20 +00002482 autocmd test_dirchanged DirChangedPre auto call add(s:li, "pre cd " .. v:event.directory)
Bram Moolenaarb7407d32018-02-03 17:36:27 +01002483 autocmd test_dirchanged DirChanged auto call add(s:li, "auto:")
2484 autocmd test_dirchanged DirChanged auto call add(s:li, expand("<afile>"))
2485 set acd
Bram Moolenaar3503d7c2019-11-09 20:10:17 +01002486 cd ..
Bram Moolenaarb7407d32018-02-03 17:36:27 +01002487 call assert_equal([], s:li)
Bram Moolenaar61abe7d2022-08-30 21:46:08 +01002488 exe 'edit ' . s:dir_foo . '/Xautofile'
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01002489 call assert_equal(s:dir_foo, getcwd())
Bram Moolenaar28e8f732022-02-09 12:58:20 +00002490 let expected = ["pre cd " .. s:dir_foo, "auto:", s:dir_foo]
2491 call assert_equal(expected, s:li)
Bram Moolenaarb7407d32018-02-03 17:36:27 +01002492 set noacd
2493 bwipe!
2494 call s:After_test_dirchanged()
2495endfunc
Bram Moolenaar5a093432018-02-10 18:15:19 +01002496
2497" Test TextChangedI and TextChangedP
2498func Test_ChangedP()
2499 new
2500 call setline(1, ['foo', 'bar', 'foobar'])
2501 call test_override("char_avail", 1)
2502 set complete=. completeopt=menuone
2503
2504 func! TextChangedAutocmd(char)
2505 let g:autocmd .= a:char
2506 endfunc
2507
Christian Brabandtdb3b4462021-10-16 11:58:55 +01002508 " TextChanged will not be triggered, only check that it isn't.
Bram Moolenaar5a093432018-02-10 18:15:19 +01002509 au! TextChanged <buffer> :call TextChangedAutocmd('N')
2510 au! TextChangedI <buffer> :call TextChangedAutocmd('I')
2511 au! TextChangedP <buffer> :call TextChangedAutocmd('P')
2512
2513 call cursor(3, 1)
2514 let g:autocmd = ''
2515 call feedkeys("o\<esc>", 'tnix')
2516 call assert_equal('I', g:autocmd)
2517
2518 let g:autocmd = ''
2519 call feedkeys("Sf", 'tnix')
2520 call assert_equal('II', g:autocmd)
2521
2522 let g:autocmd = ''
2523 call feedkeys("Sf\<C-N>", 'tnix')
2524 call assert_equal('IIP', g:autocmd)
2525
2526 let g:autocmd = ''
2527 call feedkeys("Sf\<C-N>\<C-N>", 'tnix')
2528 call assert_equal('IIPP', g:autocmd)
2529
2530 let g:autocmd = ''
2531 call feedkeys("Sf\<C-N>\<C-N>\<C-N>", 'tnix')
2532 call assert_equal('IIPPP', g:autocmd)
2533
2534 let g:autocmd = ''
2535 call feedkeys("Sf\<C-N>\<C-N>\<C-N>\<C-N>", 'tnix')
2536 call assert_equal('IIPPPP', g:autocmd)
2537
2538 call assert_equal(['foo', 'bar', 'foobar', 'foo'], getline(1, '$'))
2539 " TODO: how should it handle completeopt=noinsert,noselect?
2540
2541 " CleanUp
2542 call test_override("char_avail", 0)
2543 au! TextChanged
2544 au! TextChangedI
2545 au! TextChangedP
2546 delfu TextChangedAutocmd
2547 unlet! g:autocmd
2548 set complete&vim completeopt&vim
2549
2550 bw!
2551endfunc
Bram Moolenaar8c64a362018-03-23 22:39:31 +01002552
Bram Moolenaar91d2e782018-08-07 19:05:01 +02002553let g:setline_handled = v:false
Bram Moolenaar1e115362019-01-09 23:01:02 +01002554func SetLineOne()
Bram Moolenaar91d2e782018-08-07 19:05:01 +02002555 if !g:setline_handled
2556 call setline(1, "(x)")
2557 let g:setline_handled = v:true
2558 endif
2559endfunc
2560
2561func Test_TextChangedI_with_setline()
2562 new
2563 call test_override('char_avail', 1)
2564 autocmd TextChangedI <buffer> call SetLineOne()
2565 call feedkeys("i(\<CR>\<Esc>", 'tx')
2566 call assert_equal('(', getline(1))
2567 call assert_equal('x)', getline(2))
2568 undo
Bram Moolenaar91d2e782018-08-07 19:05:01 +02002569 call assert_equal('', getline(1))
Bram Moolenaar9fa95062018-08-08 22:08:32 +02002570 call assert_equal('', getline(2))
Bram Moolenaar91d2e782018-08-07 19:05:01 +02002571
Bram Moolenaarca34db32022-01-20 11:17:18 +00002572 call test_override('char_avail', 0)
Bram Moolenaar91d2e782018-08-07 19:05:01 +02002573 bwipe!
2574endfunc
2575
Bram Moolenaar8c64a362018-03-23 22:39:31 +01002576func Test_Changed_FirstTime()
Bram Moolenaar8c5a2782019-08-07 23:07:07 +02002577 CheckFeature terminal
2578 CheckNotGui
Bram Moolenaar3cdcb092020-03-18 19:18:10 +01002579 " Starting a terminal to run Vim is always considered flaky.
Bram Moolenaar30d53e22020-03-18 21:10:44 +01002580 let g:test_is_flaky = 1
Bram Moolenaar8c5a2782019-08-07 23:07:07 +02002581
Bram Moolenaar8c64a362018-03-23 22:39:31 +01002582 " Prepare file for TextChanged event.
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01002583 call writefile([''], 'Xchanged.txt', 'D')
Bram Moolenaar8c64a362018-03-23 22:39:31 +01002584 let buf = term_start([GetVimProg(), '--clean', '-c', 'set noswapfile'], {'term_rows': 3})
2585 call assert_equal('running', term_getstatus(buf))
Bram Moolenaar1834d372018-03-29 17:40:46 +02002586 " Wait for the ruler (in the status line) to be shown.
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01002587 " In ConPTY, there is additional character which is drawn up to the width of
2588 " the screen.
2589 if has('conpty')
2590 call WaitForAssert({-> assert_match('\<All.*$', term_getline(buf, 3))})
2591 else
2592 call WaitForAssert({-> assert_match('\<All$', term_getline(buf, 3))})
2593 endif
Bram Moolenaar8c64a362018-03-23 22:39:31 +01002594 " It's only adding autocmd, so that no event occurs.
2595 call term_sendkeys(buf, ":au! TextChanged <buffer> call writefile(['No'], 'Xchanged.txt')\<cr>")
2596 call term_sendkeys(buf, "\<C-\\>\<C-N>:qa!\<cr>")
Bram Moolenaar50182fa2018-04-28 21:34:40 +02002597 call WaitForAssert({-> assert_equal('finished', term_getstatus(buf))})
Bram Moolenaar8c64a362018-03-23 22:39:31 +01002598 call assert_equal([''], readfile('Xchanged.txt'))
2599
2600 " clean up
Bram Moolenaar8c64a362018-03-23 22:39:31 +01002601 bwipe!
2602endfunc
Bram Moolenaar0566e892019-01-24 19:37:40 +01002603
Bram Moolenaareb93f3f2019-04-04 15:04:56 +02002604func Test_autocmd_nested()
2605 let g:did_nested = 0
2606 augroup Testing
2607 au WinNew * edit somefile
2608 au BufNew * let g:did_nested = 1
2609 augroup END
2610 split
2611 call assert_equal(0, g:did_nested)
2612 close
2613 bwipe! somefile
2614
2615 " old nested argument still works
2616 augroup Testing
2617 au!
2618 au WinNew * nested edit somefile
2619 au BufNew * let g:did_nested = 1
2620 augroup END
2621 split
2622 call assert_equal(1, g:did_nested)
2623 close
2624 bwipe! somefile
2625
2626 " New ++nested argument works
2627 augroup Testing
2628 au!
2629 au WinNew * ++nested edit somefile
2630 au BufNew * let g:did_nested = 1
2631 augroup END
2632 split
2633 call assert_equal(1, g:did_nested)
2634 close
2635 bwipe! somefile
2636
Bram Moolenaarf0775142022-03-04 20:10:38 +00002637 " nested without ++ does not work in Vim9 script
2638 call assert_fails('vim9cmd au WinNew * nested echo fails', 'E1078:')
2639
Bram Moolenaareb93f3f2019-04-04 15:04:56 +02002640 augroup Testing
2641 au!
2642 augroup END
2643
2644 call assert_fails('au WinNew * ++nested ++nested echo bad', 'E983:')
2645 call assert_fails('au WinNew * nested nested echo bad', 'E983:')
2646endfunc
2647
Bram Moolenaar5fa9f232022-07-23 09:06:48 +01002648func Test_autocmd_nested_cursor_invalid()
2649 set laststatus=0
2650 copen
2651 cclose
2652 call setline(1, ['foo', 'bar', 'baz'])
2653 3
2654 augroup nested_inv
2655 autocmd User foo ++nested copen
2656 autocmd BufAdd * let &laststatus = 2 - &laststatus
2657 augroup END
2658 doautocmd User foo
2659
2660 augroup nested_inv
2661 au!
2662 augroup END
2663 set laststatus&
Bram Moolenaarb03950f2022-07-26 13:47:13 +01002664 cclose
Bram Moolenaar5fa9f232022-07-23 09:06:48 +01002665 bwipe!
2666endfunc
2667
Bram Moolenaar3d6ee8b2022-07-27 15:23:35 +01002668func Test_autocmd_nested_keeps_cursor_pos()
2669 enew
2670 call setline(1, 'foo')
2671 autocmd User foo ++nested normal! $a
2672 autocmd InsertLeave * :
2673 doautocmd User foo
2674 call assert_equal([0, 1, 3, 0], getpos('.'))
2675
2676 bwipe!
2677endfunc
2678
Bram Moolenaarb03950f2022-07-26 13:47:13 +01002679func Test_autocmd_nested_switch_window()
2680 " run this in a separate Vim so that SafeState works
2681 CheckRunVimInTerminal
2682
2683 let lines =<< trim END
2684 vim9script
2685 ['()']->writefile('Xautofile')
2686 autocmd VimEnter * ++nested edit Xautofile | split
2687 autocmd BufReadPost * autocmd SafeState * ++once foldclosed('.')
2688 autocmd WinEnter * matchadd('ErrorMsg', 'pat')
2689 END
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01002690 call writefile(lines, 'Xautoscript', 'D')
Bram Moolenaarb03950f2022-07-26 13:47:13 +01002691 let buf = RunVimInTerminal('-S Xautoscript', {'rows': 10})
2692 call VerifyScreenDump(buf, 'Test_autocmd_nested_switch', {})
2693
2694 call StopVimInTerminal(buf)
2695 call delete('Xautofile')
Bram Moolenaarb03950f2022-07-26 13:47:13 +01002696endfunc
2697
Bram Moolenaareb93f3f2019-04-04 15:04:56 +02002698func Test_autocmd_once()
2699 " Without ++once WinNew triggers twice
2700 let g:did_split = 0
2701 augroup Testing
2702 au WinNew * let g:did_split += 1
2703 augroup END
2704 split
2705 split
2706 call assert_equal(2, g:did_split)
2707 call assert_true(exists('#WinNew'))
2708 close
2709 close
2710
2711 " With ++once WinNew triggers once
2712 let g:did_split = 0
2713 augroup Testing
2714 au!
2715 au WinNew * ++once let g:did_split += 1
2716 augroup END
2717 split
2718 split
2719 call assert_equal(1, g:did_split)
2720 call assert_false(exists('#WinNew'))
2721 close
2722 close
2723
2724 call assert_fails('au WinNew * ++once ++once echo bad', 'E983:')
2725endfunc
2726
Bram Moolenaara68e5952019-04-25 22:22:01 +02002727func Test_autocmd_bufreadpre()
2728 new
2729 let b:bufreadpre = 1
Bram Moolenaarab505b12020-03-23 19:28:44 +01002730 call append(0, range(1000))
Bram Moolenaara68e5952019-04-25 22:22:01 +02002731 w! XAutocmdBufReadPre.txt
2732 autocmd BufReadPre <buffer> :let b:bufreadpre += 1
Bram Moolenaarab505b12020-03-23 19:28:44 +01002733 norm! 500gg
Bram Moolenaara68e5952019-04-25 22:22:01 +02002734 sp
Bram Moolenaarab505b12020-03-23 19:28:44 +01002735 norm! 1000gg
Bram Moolenaara68e5952019-04-25 22:22:01 +02002736 wincmd p
2737 let g:wsv1 = winsaveview()
2738 wincmd p
2739 let g:wsv2 = winsaveview()
2740 " triggers BufReadPre, should not move the cursor in either window
2741 " The topline may change one line in a large window.
2742 edit
2743 call assert_inrange(g:wsv2.topline - 1, g:wsv2.topline + 1, winsaveview().topline)
2744 call assert_equal(g:wsv2.lnum, winsaveview().lnum)
2745 call assert_equal(2, b:bufreadpre)
2746 wincmd p
2747 call assert_equal(g:wsv1.topline, winsaveview().topline)
2748 call assert_equal(g:wsv1.lnum, winsaveview().lnum)
2749 call assert_equal(2, b:bufreadpre)
2750 " Now set the cursor position in an BufReadPre autocommand
2751 " (even though the position will be invalid, this should make Vim reset the
2752 " cursor position in the other window.
2753 wincmd p
2754 set cpo+=g
2755 " won't do anything, but try to set the cursor on an invalid lnum
2756 autocmd BufReadPre <buffer> :norm! 70gg
2757 " triggers BufReadPre, should not move the cursor in either window
2758 e
2759 call assert_equal(1, winsaveview().topline)
2760 call assert_equal(1, winsaveview().lnum)
2761 call assert_equal(3, b:bufreadpre)
2762 wincmd p
2763 call assert_equal(g:wsv1.topline, winsaveview().topline)
2764 call assert_equal(g:wsv1.lnum, winsaveview().lnum)
2765 call assert_equal(3, b:bufreadpre)
2766 close
2767 close
2768 call delete('XAutocmdBufReadPre.txt')
2769 set cpo-=g
2770endfunc
2771
Bram Moolenaar5e66b422019-01-24 21:58:10 +01002772" FileChangedShell tested in test_filechanged.vim
Bram Moolenaar69ea5872019-04-25 20:29:00 +02002773
2774" Tests for the following autocommands:
2775" - FileWritePre writing a compressed file
2776" - FileReadPost reading a compressed file
2777" - BufNewFile reading a file template
2778" - BufReadPre decompressing the file to be read
2779" - FilterReadPre substituting characters in the temp file
2780" - FilterReadPost substituting characters after filtering
2781" - FileReadPre set options for decompression
2782" - FileReadPost decompress the file
2783func Test_ReadWrite_Autocmds()
2784 " Run this test only on Unix-like systems and if gzip is available
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +02002785 CheckUnix
2786 CheckExecutable gzip
Bram Moolenaar69ea5872019-04-25 20:29:00 +02002787
2788 " Make $GZIP empty, "-v" would cause trouble.
2789 let $GZIP = ""
2790
2791 " Use a FileChangedShell autocommand to avoid a prompt for 'Xtestfile.gz'
2792 " being modified outside of Vim (noticed on Solaris).
2793 au FileChangedShell * echo 'caught FileChangedShell'
2794
2795 " Test for the FileReadPost, FileWritePre and FileWritePost autocmds
2796 augroup Test1
2797 au!
2798 au FileWritePre *.gz '[,']!gzip
2799 au FileWritePost *.gz undo
2800 au FileReadPost *.gz '[,']!gzip -d
2801 augroup END
2802
2803 new
2804 set bin
2805 call append(0, [
2806 \ 'line 2 Abcdefghijklmnopqrstuvwxyz',
2807 \ 'line 3 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2808 \ 'line 4 Abcdefghijklmnopqrstuvwxyz',
2809 \ 'line 5 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2810 \ 'line 6 Abcdefghijklmnopqrstuvwxyz',
2811 \ 'line 7 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2812 \ 'line 8 Abcdefghijklmnopqrstuvwxyz',
2813 \ 'line 9 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2814 \ 'line 10 Abcdefghijklmnopqrstuvwxyz'
2815 \ ])
2816 1,9write! Xtestfile.gz
2817 enew! | close
2818
2819 new
2820 " Read and decompress the testfile
2821 0read Xtestfile.gz
2822 call assert_equal([
2823 \ 'line 2 Abcdefghijklmnopqrstuvwxyz',
2824 \ 'line 3 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2825 \ 'line 4 Abcdefghijklmnopqrstuvwxyz',
2826 \ 'line 5 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2827 \ 'line 6 Abcdefghijklmnopqrstuvwxyz',
2828 \ 'line 7 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2829 \ 'line 8 Abcdefghijklmnopqrstuvwxyz',
2830 \ 'line 9 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2831 \ 'line 10 Abcdefghijklmnopqrstuvwxyz'
2832 \ ], getline(1, 9))
2833 enew! | close
2834
2835 augroup Test1
2836 au!
2837 augroup END
2838
2839 " Test for the FileAppendPre and FileAppendPost autocmds
2840 augroup Test2
2841 au!
2842 au BufNewFile *.c read Xtest.c
2843 au FileAppendPre *.out '[,']s/new/NEW/
2844 au FileAppendPost *.out !cat Xtest.c >> test.out
2845 augroup END
2846
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01002847 call writefile(['/*', ' * Here is a new .c file', ' */'], 'Xtest.c', 'D')
Bram Moolenaar69ea5872019-04-25 20:29:00 +02002848 new foo.c " should load Xtest.c
2849 call assert_equal(['/*', ' * Here is a new .c file', ' */'], getline(2, 4))
2850 w! >> test.out " append it to the output file
2851
2852 let contents = readfile('test.out')
2853 call assert_equal(' * Here is a NEW .c file', contents[2])
2854 call assert_equal(' * Here is a new .c file', contents[5])
2855
2856 call delete('test.out')
2857 enew! | close
2858 augroup Test2
2859 au!
2860 augroup END
2861
2862 " Test for the BufReadPre and BufReadPost autocmds
2863 augroup Test3
2864 au!
2865 " setup autocommands to decompress before reading and re-compress
2866 " afterwards
2867 au BufReadPre *.gz exe '!gzip -d ' . shellescape(expand("<afile>"))
2868 au BufReadPre *.gz call rename(expand("<afile>:r"), expand("<afile>"))
2869 au BufReadPost *.gz call rename(expand("<afile>"), expand("<afile>:r"))
2870 au BufReadPost *.gz exe '!gzip ' . shellescape(expand("<afile>:r"))
2871 augroup END
2872
2873 e! Xtestfile.gz " Edit compressed file
2874 call assert_equal([
2875 \ 'line 2 Abcdefghijklmnopqrstuvwxyz',
2876 \ 'line 3 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2877 \ 'line 4 Abcdefghijklmnopqrstuvwxyz',
2878 \ 'line 5 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2879 \ 'line 6 Abcdefghijklmnopqrstuvwxyz',
2880 \ 'line 7 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2881 \ 'line 8 Abcdefghijklmnopqrstuvwxyz',
2882 \ 'line 9 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2883 \ 'line 10 Abcdefghijklmnopqrstuvwxyz'
2884 \ ], getline(1, 9))
2885
2886 w! >> test.out " Append it to the output file
2887
2888 augroup Test3
2889 au!
2890 augroup END
2891
2892 " Test for the FilterReadPre and FilterReadPost autocmds.
2893 set shelltemp " need temp files here
2894 augroup Test4
2895 au!
2896 au FilterReadPre *.out call rename(expand("<afile>"), expand("<afile>") . ".t")
2897 au FilterReadPre *.out exe 'silent !sed s/e/E/ ' . shellescape(expand("<afile>")) . ".t >" . shellescape(expand("<afile>"))
2898 au FilterReadPre *.out exe 'silent !rm ' . shellescape(expand("<afile>")) . '.t'
2899 au FilterReadPost *.out '[,']s/x/X/g
2900 augroup END
2901
2902 e! test.out " Edit the output file
2903 1,$!cat
2904 call assert_equal([
2905 \ 'linE 2 AbcdefghijklmnopqrstuvwXyz',
2906 \ 'linE 3 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
2907 \ 'linE 4 AbcdefghijklmnopqrstuvwXyz',
2908 \ 'linE 5 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
2909 \ 'linE 6 AbcdefghijklmnopqrstuvwXyz',
2910 \ 'linE 7 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
2911 \ 'linE 8 AbcdefghijklmnopqrstuvwXyz',
2912 \ 'linE 9 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
2913 \ 'linE 10 AbcdefghijklmnopqrstuvwXyz'
2914 \ ], getline(1, 9))
2915 call assert_equal([
2916 \ 'line 2 Abcdefghijklmnopqrstuvwxyz',
2917 \ 'line 3 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2918 \ 'line 4 Abcdefghijklmnopqrstuvwxyz',
2919 \ 'line 5 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2920 \ 'line 6 Abcdefghijklmnopqrstuvwxyz',
2921 \ 'line 7 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2922 \ 'line 8 Abcdefghijklmnopqrstuvwxyz',
2923 \ 'line 9 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2924 \ 'line 10 Abcdefghijklmnopqrstuvwxyz'
2925 \ ], readfile('test.out'))
2926
2927 augroup Test4
2928 au!
2929 augroup END
2930 set shelltemp&vim
2931
2932 " Test for the FileReadPre and FileReadPost autocmds.
2933 augroup Test5
2934 au!
2935 au FileReadPre *.gz exe 'silent !gzip -d ' . shellescape(expand("<afile>"))
2936 au FileReadPre *.gz call rename(expand("<afile>:r"), expand("<afile>"))
2937 au FileReadPost *.gz '[,']s/l/L/
2938 augroup END
2939
2940 new
2941 0r Xtestfile.gz " Read compressed file
2942 call assert_equal([
2943 \ 'Line 2 Abcdefghijklmnopqrstuvwxyz',
2944 \ 'Line 3 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2945 \ 'Line 4 Abcdefghijklmnopqrstuvwxyz',
2946 \ 'Line 5 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2947 \ 'Line 6 Abcdefghijklmnopqrstuvwxyz',
2948 \ 'Line 7 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2949 \ 'Line 8 Abcdefghijklmnopqrstuvwxyz',
2950 \ 'Line 9 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2951 \ 'Line 10 Abcdefghijklmnopqrstuvwxyz'
2952 \ ], getline(1, 9))
2953 call assert_equal([
2954 \ 'line 2 Abcdefghijklmnopqrstuvwxyz',
2955 \ 'line 3 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2956 \ 'line 4 Abcdefghijklmnopqrstuvwxyz',
2957 \ 'line 5 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2958 \ 'line 6 Abcdefghijklmnopqrstuvwxyz',
2959 \ 'line 7 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2960 \ 'line 8 Abcdefghijklmnopqrstuvwxyz',
2961 \ 'line 9 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2962 \ 'line 10 Abcdefghijklmnopqrstuvwxyz'
2963 \ ], readfile('Xtestfile.gz'))
2964
2965 augroup Test5
2966 au!
2967 augroup END
2968
2969 au! FileChangedShell
2970 call delete('Xtestfile.gz')
Bram Moolenaar69ea5872019-04-25 20:29:00 +02002971 call delete('test.out')
2972endfunc
Bram Moolenaar23b51392019-05-09 21:38:43 +02002973
2974func Test_throw_in_BufWritePre()
2975 new
2976 call setline(1, ['one', 'two', 'three'])
2977 call assert_false(filereadable('Xthefile'))
2978 augroup throwing
2979 au BufWritePre X* throw 'do not write'
2980 augroup END
2981 try
2982 w Xthefile
2983 catch
2984 let caught = 1
2985 endtry
2986 call assert_equal(1, caught)
2987 call assert_false(filereadable('Xthefile'))
2988
2989 bwipe!
2990 au! throwing
2991endfunc
Bram Moolenaarcadbe1b2019-09-22 21:50:09 +02002992
Bram Moolenaar40fa12a2021-09-22 14:18:13 +02002993func Test_autocmd_in_try_block()
Bram Moolenaar6f14da12022-09-07 21:30:44 +01002994 call mkdir('Xintrydir', 'R')
Bram Moolenaar40fa12a2021-09-22 14:18:13 +02002995 au BufEnter * let g:fname = expand('%')
2996 try
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +01002997 edit Xintrydir/
Bram Moolenaar40fa12a2021-09-22 14:18:13 +02002998 endtry
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +01002999 call assert_match('Xintrydir', g:fname)
Bram Moolenaar40fa12a2021-09-22 14:18:13 +02003000
3001 unlet g:fname
3002 au! BufEnter
Bram Moolenaar40fa12a2021-09-22 14:18:13 +02003003endfunc
3004
Bram Moolenaarcadbe1b2019-09-22 21:50:09 +02003005func Test_autocmd_SafeState()
3006 CheckRunVimInTerminal
3007
3008 let lines =<< trim END
3009 let g:safe = 0
3010 let g:again = ''
3011 au SafeState * let g:safe += 1
3012 au SafeStateAgain * let g:again ..= 'x'
3013 func CallTimer()
3014 call timer_start(10, {id -> execute('let g:again ..= "t"')})
3015 endfunc
3016 END
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01003017 call writefile(lines, 'XSafeState', 'D')
Bram Moolenaarcadbe1b2019-09-22 21:50:09 +02003018 let buf = RunVimInTerminal('-S XSafeState', #{rows: 6})
3019
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01003020 " Sometimes we loop to handle a K_IGNORE, SafeState may be triggered once or
Bram Moolenaar8fb1b472020-02-23 16:16:26 +01003021 " more often.
Bram Moolenaarcadbe1b2019-09-22 21:50:09 +02003022 call term_sendkeys(buf, ":echo g:safe\<CR>")
Bram Moolenaar8fb1b472020-02-23 16:16:26 +01003023 call WaitForAssert({-> assert_match('^\d ', term_getline(buf, 6))}, 1000)
Bram Moolenaarcadbe1b2019-09-22 21:50:09 +02003024
Bram Moolenaar8fb1b472020-02-23 16:16:26 +01003025 " SafeStateAgain should be invoked at least three times
Bram Moolenaarcadbe1b2019-09-22 21:50:09 +02003026 call term_sendkeys(buf, ":echo g:again\<CR>")
Bram Moolenaar8fb1b472020-02-23 16:16:26 +01003027 call WaitForAssert({-> assert_match('^xxx', term_getline(buf, 6))}, 1000)
Bram Moolenaarcadbe1b2019-09-22 21:50:09 +02003028
3029 call term_sendkeys(buf, ":let g:again = ''\<CR>:call CallTimer()\<CR>")
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02003030 call TermWait(buf, 50)
Bram Moolenaar0f6629a2019-09-22 23:24:13 +02003031 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02003032 call TermWait(buf, 50)
Bram Moolenaarcadbe1b2019-09-22 21:50:09 +02003033 call term_sendkeys(buf, ":echo g:again\<CR>")
3034 call WaitForAssert({-> assert_match('xtx', term_getline(buf, 6))}, 1000)
3035
3036 call StopVimInTerminal(buf)
Bram Moolenaarcadbe1b2019-09-22 21:50:09 +02003037endfunc
Bram Moolenaar23324a02019-10-01 17:39:04 +02003038
3039func Test_autocmd_CmdWinEnter()
3040 CheckRunVimInTerminal
Bram Moolenaar21829c52021-01-26 22:42:21 +01003041
Bram Moolenaar23324a02019-10-01 17:39:04 +02003042 let lines =<< trim END
Egor Zvorykin125ffd22021-11-17 14:01:14 +00003043 augroup vimHints | au! | augroup END
Bram Moolenaar23324a02019-10-01 17:39:04 +02003044 let b:dummy_var = 'This is a dummy'
3045 autocmd CmdWinEnter * quit
3046 let winnr = winnr('$')
3047 END
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01003048 let filename = 'XCmdWinEnter'
Bram Moolenaar23324a02019-10-01 17:39:04 +02003049 call writefile(lines, filename)
3050 let buf = RunVimInTerminal('-S '.filename, #{rows: 6})
3051
3052 call term_sendkeys(buf, "q:")
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02003053 call TermWait(buf)
Bram Moolenaar23324a02019-10-01 17:39:04 +02003054 call term_sendkeys(buf, ":echo b:dummy_var\<cr>")
Bram Moolenaar353c3512020-03-15 14:19:26 +01003055 call WaitForAssert({-> assert_match('^This is a dummy', term_getline(buf, 6))}, 2000)
Bram Moolenaar23324a02019-10-01 17:39:04 +02003056 call term_sendkeys(buf, ":echo &buftype\<cr>")
3057 call WaitForAssert({-> assert_notmatch('^nofile', term_getline(buf, 6))}, 1000)
3058 call term_sendkeys(buf, ":echo winnr\<cr>")
3059 call WaitForAssert({-> assert_match('^1', term_getline(buf, 6))}, 1000)
3060
3061 " clean up
3062 call StopVimInTerminal(buf)
3063 call delete(filename)
3064endfunc
Bram Moolenaarec66c412019-10-11 21:19:13 +02003065
3066func Test_autocmd_was_using_freed_memory()
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01003067 CheckFeature quickfix
3068
Bram Moolenaarec66c412019-10-11 21:19:13 +02003069 pedit xx
3070 n x
Bram Moolenaar92bb83e2021-02-03 23:04:46 +01003071 augroup winenter
3072 au WinEnter * if winnr('$') > 2 | quit | endif
3073 augroup END
Bram Moolenaarec66c412019-10-11 21:19:13 +02003074 split
Bram Moolenaar92bb83e2021-02-03 23:04:46 +01003075
3076 augroup winenter
3077 au! WinEnter
3078 augroup END
3079
3080 bwipe xx
3081 bwipe x
3082 pclose
Bram Moolenaarec66c412019-10-11 21:19:13 +02003083endfunc
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +01003084
3085func Test_BufWrite_lockmarks()
Bram Moolenaarf08b0eb2021-10-16 13:00:14 +01003086 let g:test_is_flaky = 1
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +01003087 edit! Xtest
3088 call setline(1, ['a', 'b', 'c', 'd'])
3089
3090 " :lockmarks preserves the marks
3091 call SetChangeMarks(2, 3)
3092 lockmarks write
3093 call assert_equal([2, 3], [line("'["), line("']")])
3094
3095 " *WritePre autocmds get the correct line range, but lockmarks preserves the
3096 " original values for the user
3097 augroup lockmarks
3098 au!
3099 au BufWritePre,FilterWritePre * call assert_equal([1, 4], [line("'["), line("']")])
3100 au FileWritePre * call assert_equal([3, 4], [line("'["), line("']")])
3101 augroup END
3102
3103 lockmarks write
3104 call assert_equal([2, 3], [line("'["), line("']")])
3105
3106 if executable('cat')
3107 lockmarks %!cat
3108 call assert_equal([2, 3], [line("'["), line("']")])
3109 endif
3110
3111 lockmarks 3,4write Xtest2
3112 call assert_equal([2, 3], [line("'["), line("']")])
3113
3114 au! lockmarks
3115 augroup! lockmarks
3116 call delete('Xtest')
3117 call delete('Xtest2')
3118endfunc
Bram Moolenaarce6db022020-01-07 20:11:42 +01003119
3120func Test_FileType_spell()
3121 if !isdirectory('/tmp')
3122 throw "Skipped: requires /tmp directory"
3123 endif
3124
3125 " this was crashing with an invalid free()
3126 setglobal spellfile=/tmp/en.utf-8.add
3127 augroup crash
3128 autocmd!
3129 autocmd BufNewFile,BufReadPost crashfile setf somefiletype
3130 autocmd BufNewFile,BufReadPost crashfile set ft=anotherfiletype
3131 autocmd FileType anotherfiletype setlocal spell
3132 augroup END
3133 func! NoCrash() abort
3134 edit /tmp/crashfile
3135 endfunc
3136 call NoCrash()
3137
3138 au! crash
3139 setglobal spellfile=
3140endfunc
Bram Moolenaarbc2b71d2020-02-17 21:33:30 +01003141
Bram Moolenaaref976322022-09-28 11:48:30 +01003142" this was wiping out the current buffer and using freed memory
3143func Test_SpellFileMissing_bwipe()
3144 next 0
3145 au SpellFileMissing 0 bwipe
3146 call assert_fails('set spell spelllang=0', 'E937:')
3147
3148 au! SpellFileMissing
Bram Moolenaar0a60f792022-11-19 21:18:11 +00003149 set nospell spelllang=en
Bram Moolenaaref976322022-09-28 11:48:30 +01003150 bwipe
3151endfunc
3152
Bram Moolenaar406cd902020-02-18 21:54:41 +01003153" Test closing a window or editing another buffer from a FileChangedRO handler
3154" in a readonly buffer
3155func Test_FileChangedRO_winclose()
Bram Moolenaar62cd26a2020-10-11 20:08:44 +02003156 call test_override('ui_delay', 10)
3157
Bram Moolenaar406cd902020-02-18 21:54:41 +01003158 augroup FileChangedROTest
3159 au!
3160 autocmd FileChangedRO * quit
3161 augroup END
3162 new
3163 set readonly
3164 call assert_fails('normal i', 'E788:')
3165 close
3166 augroup! FileChangedROTest
3167
3168 augroup FileChangedROTest
3169 au!
Bram Moolenaar61abe7d2022-08-30 21:46:08 +01003170 autocmd FileChangedRO * edit Xrofile
Bram Moolenaar406cd902020-02-18 21:54:41 +01003171 augroup END
3172 new
3173 set readonly
3174 call assert_fails('normal i', 'E788:')
3175 close
3176 augroup! FileChangedROTest
Bram Moolenaar62cd26a2020-10-11 20:08:44 +02003177 call test_override('ALL', 0)
Bram Moolenaar406cd902020-02-18 21:54:41 +01003178endfunc
3179
Bram Moolenaar0c81d1b2020-02-22 22:45:55 +01003180func LogACmd()
3181 call add(g:logged, line('$'))
3182endfunc
3183
3184func Test_TermChanged()
Bram Moolenaard28e0b32020-02-22 23:08:52 +01003185 CheckNotGui
3186
Bram Moolenaar0c81d1b2020-02-22 22:45:55 +01003187 enew!
3188 tabnew
3189 call setline(1, ['a', 'b', 'c', 'd'])
3190 $
3191 au TermChanged * call LogACmd()
3192 let g:logged = []
3193 let term_save = &term
3194 set term=xterm
3195 call assert_equal([1, 4], g:logged)
3196
3197 au! TermChanged
3198 let &term = term_save
3199 bwipe!
3200endfunc
3201
Bram Moolenaare3284872020-03-19 13:55:03 +01003202" Test for FileReadCmd autocmd
3203func Test_autocmd_FileReadCmd()
3204 func ReadFileCmd()
3205 call append(line('$'), "v:cmdarg = " .. v:cmdarg)
3206 endfunc
3207 augroup FileReadCmdTest
3208 au!
3209 au FileReadCmd Xtest call ReadFileCmd()
3210 augroup END
3211
3212 new
3213 read ++bin Xtest
3214 read ++nobin Xtest
3215 read ++edit Xtest
3216 read ++bad=keep Xtest
3217 read ++bad=drop Xtest
3218 read ++bad=- Xtest
3219 read ++ff=unix Xtest
3220 read ++ff=dos Xtest
3221 read ++ff=mac Xtest
3222 read ++enc=utf-8 Xtest
3223
3224 call assert_equal(['',
3225 \ 'v:cmdarg = ++bin',
3226 \ 'v:cmdarg = ++nobin',
3227 \ 'v:cmdarg = ++edit',
3228 \ 'v:cmdarg = ++bad=keep',
3229 \ 'v:cmdarg = ++bad=drop',
3230 \ 'v:cmdarg = ++bad=-',
3231 \ 'v:cmdarg = ++ff=unix',
3232 \ 'v:cmdarg = ++ff=dos',
3233 \ 'v:cmdarg = ++ff=mac',
3234 \ 'v:cmdarg = ++enc=utf-8'], getline(1, '$'))
3235
Bram Moolenaar23526d22022-12-05 15:50:41 +00003236 bwipe!
Bram Moolenaare3284872020-03-19 13:55:03 +01003237 augroup FileReadCmdTest
3238 au!
3239 augroup END
3240 delfunc ReadFileCmd
3241endfunc
3242
Bram Moolenaaree4e0c12020-04-06 21:35:05 +02003243" Test for passing invalid arguments to autocmd
3244func Test_autocmd_invalid_args()
3245 " Additional character after * for event
Bram Moolenaar61abe7d2022-08-30 21:46:08 +01003246 call assert_fails('autocmd *a Xinvfile set ff=unix', 'E215:')
Bram Moolenaaree4e0c12020-04-06 21:35:05 +02003247 augroup Test
3248 augroup END
3249 " Invalid autocmd event
Bram Moolenaar61abe7d2022-08-30 21:46:08 +01003250 call assert_fails('autocmd Bufabc Xinvfile set ft=vim', 'E216:')
Bram Moolenaaree4e0c12020-04-06 21:35:05 +02003251 " Invalid autocmd event in a autocmd group
Bram Moolenaar61abe7d2022-08-30 21:46:08 +01003252 call assert_fails('autocmd Test Bufabc Xinvfile set ft=vim', 'E216:')
Bram Moolenaaree4e0c12020-04-06 21:35:05 +02003253 augroup! Test
3254 " Execute all autocmds
3255 call assert_fails('doautocmd * BufEnter', 'E217:')
3256 call assert_fails('augroup! x1a2b3', 'E367:')
3257 call assert_fails('autocmd BufNew <buffer=999> pwd', 'E680:')
Bram Moolenaar531be472020-09-23 22:38:05 +02003258 call assert_fails('autocmd BufNew \) set ff=unix', 'E55:')
Bram Moolenaaree4e0c12020-04-06 21:35:05 +02003259endfunc
3260
3261" Test for deep nesting of autocmds
3262func Test_autocmd_deep_nesting()
Bram Moolenaar61abe7d2022-08-30 21:46:08 +01003263 autocmd BufEnter Xdeepfile doautocmd BufEnter Xdeepfile
3264 call assert_fails('doautocmd BufEnter Xdeepfile', 'E218:')
3265 autocmd! BufEnter Xdeepfile
Bram Moolenaaree4e0c12020-04-06 21:35:05 +02003266endfunc
3267
Bram Moolenaarbe5ee862020-06-10 20:56:58 +02003268" Tests for SigUSR1 autocmd event, which is only available on posix systems.
3269func Test_autocmd_sigusr1()
3270 CheckUnix
Bram Moolenaar0056ca72022-09-23 21:26:39 +01003271 " FIXME: should this work on MacOS M1?
3272 CheckNotMacM1
Bram Moolenaar62cd26a2020-10-11 20:08:44 +02003273 CheckExecutable /bin/kill
Bram Moolenaarbe5ee862020-06-10 20:56:58 +02003274
3275 let g:sigusr1_passed = 0
3276 au SigUSR1 * let g:sigusr1_passed = 1
3277 call system('/bin/kill -s usr1 ' . getpid())
3278 call WaitForAssert({-> assert_true(g:sigusr1_passed)})
3279
3280 au! SigUSR1
3281 unlet g:sigusr1_passed
3282endfunc
3283
Bram Moolenaarb340bae2020-06-15 19:51:56 +02003284" Test for BufReadPre autocmd deleting the file
3285func Test_BufReadPre_delfile()
3286 augroup TestAuCmd
3287 au!
Bram Moolenaare7cda972022-08-29 11:02:59 +01003288 autocmd BufReadPre XbufreadPre call delete('XbufreadPre')
Bram Moolenaarb340bae2020-06-15 19:51:56 +02003289 augroup END
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01003290 call writefile([], 'XbufreadPre', 'D')
Bram Moolenaare7cda972022-08-29 11:02:59 +01003291 call assert_fails('new XbufreadPre', 'E200:')
3292 call assert_equal('XbufreadPre', @%)
Bram Moolenaarb340bae2020-06-15 19:51:56 +02003293 call assert_equal(1, &readonly)
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01003294
Bram Moolenaarb340bae2020-06-15 19:51:56 +02003295 augroup TestAuCmd
3296 au!
3297 augroup END
3298 close!
3299endfunc
3300
3301" Test for BufReadPre autocmd changing the current buffer
3302func Test_BufReadPre_changebuf()
3303 augroup TestAuCmd
3304 au!
Bram Moolenaare7cda972022-08-29 11:02:59 +01003305 autocmd BufReadPre Xchangebuf edit Xsomeotherfile
Bram Moolenaarb340bae2020-06-15 19:51:56 +02003306 augroup END
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01003307 call writefile([], 'Xchangebuf', 'D')
Bram Moolenaare7cda972022-08-29 11:02:59 +01003308 call assert_fails('new Xchangebuf', 'E201:')
Bram Moolenaarb340bae2020-06-15 19:51:56 +02003309 call assert_equal('Xsomeotherfile', @%)
3310 call assert_equal(1, &readonly)
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01003311
Bram Moolenaarb340bae2020-06-15 19:51:56 +02003312 augroup TestAuCmd
3313 au!
3314 augroup END
3315 close!
3316endfunc
3317
3318" Test for BufWipeouti autocmd changing the current buffer when reading a file
3319" in an empty buffer with 'f' flag in 'cpo'
3320func Test_BufDelete_changebuf()
3321 new
3322 augroup TestAuCmd
3323 au!
3324 autocmd BufWipeout * let bufnr = bufadd('somefile') | exe "b " .. bufnr
3325 augroup END
3326 let save_cpo = &cpo
3327 set cpo+=f
Bram Moolenaarb18b4962022-09-02 21:55:50 +01003328 call assert_fails('r Xchangebuf', ['E812:', 'E484:'])
Bram Moolenaarb340bae2020-06-15 19:51:56 +02003329 call assert_equal('somefile', @%)
3330 let &cpo = save_cpo
3331 augroup TestAuCmd
3332 au!
3333 augroup END
3334 close!
3335endfunc
3336
Bram Moolenaar0fe937f2020-06-16 22:42:04 +02003337" Test for the temporary internal window used to execute autocmds
3338func Test_autocmd_window()
3339 %bw!
3340 edit one.txt
3341 tabnew two.txt
Bram Moolenaar41cd8032021-03-13 15:47:56 +01003342 vnew three.txt
3343 tabnew four.txt
3344 tabprevious
Bram Moolenaar0fe937f2020-06-16 22:42:04 +02003345 let g:blist = []
Bram Moolenaar832adf92020-06-25 19:01:36 +02003346 augroup aucmd_win_test1
Bram Moolenaar0fe937f2020-06-16 22:42:04 +02003347 au!
3348 au BufEnter * call add(g:blist, [expand('<afile>'),
3349 \ win_gettype(bufwinnr(expand('<afile>')))])
3350 augroup END
3351
3352 doautoall BufEnter
Bram Moolenaar41cd8032021-03-13 15:47:56 +01003353 call assert_equal([
3354 \ ['one.txt', 'autocmd'],
3355 \ ['two.txt', ''],
3356 \ ['four.txt', 'autocmd'],
3357 \ ['three.txt', ''],
3358 \ ], g:blist)
Bram Moolenaar0fe937f2020-06-16 22:42:04 +02003359
Bram Moolenaar832adf92020-06-25 19:01:36 +02003360 augroup aucmd_win_test1
Bram Moolenaar0fe937f2020-06-16 22:42:04 +02003361 au!
3362 augroup END
Bram Moolenaar832adf92020-06-25 19:01:36 +02003363 augroup! aucmd_win_test1
3364 %bw!
3365endfunc
3366
3367" Test for trying to close the temporary window used for executing an autocmd
3368func Test_close_autocmd_window()
3369 %bw!
3370 edit one.txt
3371 tabnew two.txt
3372 augroup aucmd_win_test2
3373 au!
3374 au BufEnter * if expand('<afile>') == 'one.txt' | 1close | endif
3375 augroup END
3376
3377 call assert_fails('doautoall BufEnter', 'E813:')
3378
3379 augroup aucmd_win_test2
3380 au!
3381 augroup END
3382 augroup! aucmd_win_test2
Bram Moolenaarcf844172020-06-26 19:44:06 +02003383 %bwipe!
3384endfunc
3385
3386" Test for trying to close the tab that has the temporary window for exeucing
3387" an autocmd.
3388func Test_close_autocmd_tab()
3389 edit one.txt
3390 tabnew two.txt
3391 augroup aucmd_win_test
3392 au!
3393 au BufEnter * if expand('<afile>') == 'one.txt' | tabfirst | tabonly | endif
3394 augroup END
3395
3396 call assert_fails('doautoall BufEnter', 'E813:')
3397
3398 tabonly
3399 augroup aucmd_win_test
3400 au!
3401 augroup END
3402 augroup! aucmd_win_test
3403 %bwipe!
Bram Moolenaar0fe937f2020-06-16 22:42:04 +02003404endfunc
3405
Bram Moolenaarcb1956d2022-01-07 15:45:18 +00003406func Test_Visual_doautoall_redraw()
3407 call setline(1, ['a', 'b'])
3408 new
3409 wincmd p
3410 call feedkeys("G\<C-V>", 'txn')
3411 autocmd User Explode ++once redraw
3412 doautoall User Explode
3413 %bwipe!
3414endfunc
3415
Bram Moolenaar6bcb8772021-02-03 21:23:29 +01003416" This was using freed memory.
3417func Test_BufNew_arglocal()
3418 arglocal
3419 au BufNew * arglocal
3420 call assert_fails('drop xx', 'E1156:')
3421
3422 au! BufNew
3423endfunc
3424
Bram Moolenaar8ab37572021-02-03 21:56:59 +01003425func Test_autocmd_closes_window()
3426 au BufNew,BufWinLeave * e %e
3427 file yyy
3428 au BufNew,BufWinLeave * ball
Bram Moolenaaraad5f9d2021-02-06 17:30:31 +01003429 n xxx
Bram Moolenaar8ab37572021-02-03 21:56:59 +01003430
Bram Moolenaaraad5f9d2021-02-06 17:30:31 +01003431 %bwipe
Bram Moolenaar8ab37572021-02-03 21:56:59 +01003432 au! BufNew
3433 au! BufWinLeave
3434endfunc
3435
Bram Moolenaar92bb83e2021-02-03 23:04:46 +01003436func Test_autocmd_quit_psearch()
3437 sn aa bb
3438 augroup aucmd_win_test
3439 au!
3440 au BufEnter,BufLeave,BufNew,WinEnter,WinLeave,WinNew * if winnr('$') > 1 | q | endif
3441 augroup END
3442 ps /
3443
3444 augroup aucmd_win_test
3445 au!
3446 augroup END
zeertzjq7851c692022-04-21 11:14:01 +01003447 new
3448 pclose
Bram Moolenaar92bb83e2021-02-03 23:04:46 +01003449endfunc
3450
Bram Moolenaaraad5f9d2021-02-06 17:30:31 +01003451" Fuzzer found some strange combination that caused a crash.
3452func Test_autocmd_normal_mess()
Bram Moolenaardd07c022021-02-07 13:32:46 +01003453 " For unknown reason this hangs on MS-Windows
3454 CheckNotMSWindows
3455
Bram Moolenaaraad5f9d2021-02-06 17:30:31 +01003456 augroup aucmd_normal_test
3457 au BufLeave,BufWinLeave,BufHidden,BufUnload,BufDelete,BufWipeout * norm 7q/qc
3458 augroup END
Bram Moolenaar983d83f2021-02-07 12:12:43 +01003459 call assert_fails('o4', 'E1159')
Bram Moolenaaraad5f9d2021-02-06 17:30:31 +01003460 silent! H
Bram Moolenaar983d83f2021-02-07 12:12:43 +01003461 call assert_fails('e xx', 'E1159')
Bram Moolenaaraad5f9d2021-02-06 17:30:31 +01003462 normal G
3463
3464 augroup aucmd_normal_test
3465 au!
3466 augroup END
3467endfunc
3468
Bram Moolenaar8c6951f2021-02-06 18:08:45 +01003469func Test_autocmd_closing_cmdwin()
Bram Moolenaardd07c022021-02-07 13:32:46 +01003470 " For unknown reason this hangs on MS-Windows
3471 CheckNotMSWindows
3472
Bram Moolenaar8c6951f2021-02-06 18:08:45 +01003473 au BufWinLeave * nested q
3474 call assert_fails("norm 7q?\n", 'E855:')
3475
3476 au! BufWinLeave
3477 new
3478 only
3479endfunc
3480
Bram Moolenaar2c7080b2021-02-06 19:19:42 +01003481func Test_autocmd_vimgrep()
3482 augroup aucmd_vimgrep
Charlie Grovesfef44852022-04-19 16:24:12 +01003483 au QuickfixCmdPre,BufNew,BufReadCmd * sb
zeertzjq7851c692022-04-21 11:14:01 +01003484 au QuickfixCmdPre,BufNew,BufReadCmd * q9
Bram Moolenaar2c7080b2021-02-06 19:19:42 +01003485 augroup END
Bram Moolenaardd07c022021-02-07 13:32:46 +01003486 call assert_fails('lv ?a? foo', 'E926:')
Bram Moolenaar2c7080b2021-02-06 19:19:42 +01003487
3488 augroup aucmd_vimgrep
3489 au!
3490 augroup END
3491endfunc
3492
Bram Moolenaar73b8b0a2021-08-01 14:52:32 +02003493func Test_autocmd_with_block()
3494 augroup block_testing
3495 au BufReadPost *.xml {
3496 setlocal matchpairs+=<:>
3497 /<start
3498 }
Bram Moolenaar63b91732021-08-05 20:40:03 +02003499 au CursorHold * {
3500 autocmd BufReadPre * ++once echo 'one' | echo 'two'
3501 g:gotSafeState = 77
3502 }
Bram Moolenaar73b8b0a2021-08-01 14:52:32 +02003503 augroup END
3504
3505 let expected = "\n--- Autocommands ---\nblock_testing BufRead\n *.xml {^@ setlocal matchpairs+=<:>^@ /<start^@ }"
3506 call assert_equal(expected, execute('au BufReadPost *.xml'))
3507
Bram Moolenaar63b91732021-08-05 20:40:03 +02003508 doautocmd CursorHold
3509 call assert_equal(77, g:gotSafeState)
3510 unlet g:gotSafeState
3511
Bram Moolenaar73b8b0a2021-08-01 14:52:32 +02003512 augroup block_testing
3513 au!
Bram Moolenaar75ebd2a2022-06-03 17:39:46 +01003514 autocmd CursorHold * {
3515 if true
3516 # comment
3517 && true
3518
3519 && true
3520 g:done = 'yes'
3521 endif
3522 }
3523 augroup END
3524 doautocmd CursorHold
3525 call assert_equal('yes', g:done)
3526
3527 unlet g:done
3528 augroup block_testing
3529 au!
Bram Moolenaar73b8b0a2021-08-01 14:52:32 +02003530 augroup END
3531endfunc
3532
Christian Brabandtdb3b4462021-10-16 11:58:55 +01003533" Test TextChangedI and TextChanged
3534func Test_Changed_ChangedI()
3535 new
3536 call test_override("char_avail", 1)
3537 let [g:autocmd_i, g:autocmd_n] = ['','']
3538
3539 func! TextChangedAutocmdI(char)
3540 let g:autocmd_{tolower(a:char)} = a:char .. b:changedtick
3541 endfunc
3542
3543 augroup Test_TextChanged
3544 au!
3545 au TextChanged <buffer> :call TextChangedAutocmdI('N')
3546 au TextChangedI <buffer> :call TextChangedAutocmdI('I')
3547 augroup END
3548
3549 call feedkeys("ifoo\<esc>", 'tnix')
3550 " TODO: Test test does not seem to trigger TextChanged autocommand, this
3551 " requires running Vim in a terminal window.
3552 " call assert_equal('N3', g:autocmd_n)
3553 call assert_equal('I3', g:autocmd_i)
3554
3555 call feedkeys("yyp", 'tnix')
3556 " TODO: Test test does not seem to trigger TextChanged autocommand.
3557 " call assert_equal('N4', g:autocmd_n)
3558 call assert_equal('I3', g:autocmd_i)
3559
3560 " CleanUp
3561 call test_override("char_avail", 0)
3562 au! TextChanged <buffer>
3563 au! TextChangedI <buffer>
3564 augroup! Test_TextChanged
3565 delfu TextChangedAutocmdI
3566 unlet! g:autocmd_i g:autocmd_n
3567
3568 bw!
3569endfunc
Bram Moolenaar2c7080b2021-02-06 19:19:42 +01003570
Bram Moolenaar6f2465d2022-03-22 18:13:01 +00003571func Test_closing_autocmd_window()
3572 let lines =<< trim END
3573 edit Xa.txt
3574 tabnew Xb.txt
3575 autocmd BufEnter Xa.txt unhide 1
3576 doautoall BufEnter
3577 END
3578 call v9.CheckScriptFailure(lines, 'E814:')
3579 au! BufEnter
3580 only!
3581 bwipe Xa.txt
3582 bwipe Xb.txt
3583endfunc
3584
Bram Moolenaar347538f2022-03-26 16:28:06 +00003585func Test_bufwipeout_changes_window()
3586 " This should not crash, but we don't have any expectations about what
3587 " happens, changing window in BufWipeout has unpredictable results.
3588 tabedit
3589 let g:window_id = win_getid()
3590 topleft new
3591 setlocal bufhidden=wipe
3592 autocmd BufWipeout <buffer> call win_gotoid(g:window_id)
3593 tabprevious
3594 +tabclose
3595
3596 unlet g:window_id
3597 au! BufWipeout
3598 %bwipe!
3599endfunc
3600
zeertzjq021996f2022-04-10 11:44:04 +01003601func Test_v_event_readonly()
3602 autocmd CompleteChanged * let v:event.width = 0
3603 call assert_fails("normal! i\<C-X>\<C-V>", 'E46:')
3604 au! CompleteChanged
3605
3606 autocmd DirChangedPre * let v:event.directory = ''
3607 call assert_fails('cd .', 'E46:')
3608 au! DirChangedPre
3609
3610 autocmd ModeChanged * let v:event.new_mode = ''
3611 call assert_fails('normal! cc', 'E46:')
3612 au! ModeChanged
3613
3614 autocmd TextYankPost * let v:event.operator = ''
3615 call assert_fails('normal! yy', 'E46:')
3616 au! TextYankPost
3617endfunc
3618
zeertzjqc9e8fd62022-07-26 18:12:38 +01003619" Test for ModeChanged pattern
3620func Test_mode_changes()
3621 let g:index = 0
3622 let g:mode_seq = ['n', 'i', 'n', 'v', 'V', 'i', 'ix', 'i', 'ic', 'i', 'n', 'no', 'n', 'V', 'v', 's', 'n']
3623 func! TestMode()
3624 call assert_equal(g:mode_seq[g:index], get(v:event, "old_mode"))
3625 call assert_equal(g:mode_seq[g:index + 1], get(v:event, "new_mode"))
3626 call assert_equal(mode(1), get(v:event, "new_mode"))
3627 let g:index += 1
3628 endfunc
3629
3630 au ModeChanged * :call TestMode()
3631 let g:n_to_any = 0
3632 au ModeChanged n:* let g:n_to_any += 1
3633 call feedkeys("i\<esc>vVca\<CR>\<C-X>\<C-L>\<esc>ggdG", 'tnix')
3634
3635 let g:V_to_v = 0
3636 au ModeChanged V:v let g:V_to_v += 1
3637 call feedkeys("Vv\<C-G>\<esc>", 'tnix')
3638 call assert_equal(len(filter(g:mode_seq[1:], {idx, val -> val == 'n'})), g:n_to_any)
3639 call assert_equal(1, g:V_to_v)
3640 call assert_equal(len(g:mode_seq) - 1, g:index)
3641
3642 let g:n_to_i = 0
3643 au ModeChanged n:i let g:n_to_i += 1
3644 let g:n_to_niI = 0
3645 au ModeChanged i:niI let g:n_to_niI += 1
3646 let g:niI_to_i = 0
3647 au ModeChanged niI:i let g:niI_to_i += 1
3648 let g:nany_to_i = 0
3649 au ModeChanged n*:i let g:nany_to_i += 1
3650 let g:i_to_n = 0
3651 au ModeChanged i:n let g:i_to_n += 1
3652 let g:nori_to_any = 0
3653 au ModeChanged [ni]:* let g:nori_to_any += 1
3654 let g:i_to_any = 0
3655 au ModeChanged i:* let g:i_to_any += 1
3656 let g:index = 0
3657 let g:mode_seq = ['n', 'i', 'niI', 'i', 'n']
3658 call feedkeys("a\<C-O>l\<esc>", 'tnix')
3659 call assert_equal(len(g:mode_seq) - 1, g:index)
3660 call assert_equal(1, g:n_to_i)
3661 call assert_equal(1, g:n_to_niI)
3662 call assert_equal(1, g:niI_to_i)
3663 call assert_equal(2, g:nany_to_i)
3664 call assert_equal(1, g:i_to_n)
3665 call assert_equal(2, g:i_to_any)
3666 call assert_equal(3, g:nori_to_any)
3667
3668 if has('terminal')
3669 let g:mode_seq += ['c', 'n', 't', 'nt', 'c', 'nt', 'n']
3670 call feedkeys(":term\<CR>\<C-W>N:bd!\<CR>", 'tnix')
3671 call assert_equal(len(g:mode_seq) - 1, g:index)
3672 call assert_equal(1, g:n_to_i)
3673 call assert_equal(1, g:n_to_niI)
3674 call assert_equal(1, g:niI_to_i)
3675 call assert_equal(2, g:nany_to_i)
3676 call assert_equal(1, g:i_to_n)
3677 call assert_equal(2, g:i_to_any)
3678 call assert_equal(5, g:nori_to_any)
3679 endif
3680
zeertzjqd1955982022-10-05 11:24:46 +01003681 let g:n_to_c = 0
3682 au ModeChanged n:c let g:n_to_c += 1
3683 let g:c_to_n = 0
3684 au ModeChanged c:n let g:c_to_n += 1
3685 let g:mode_seq += ['c', 'n', 'c', 'n']
3686 call feedkeys("q:\<C-C>\<Esc>", 'tnix')
3687 call assert_equal(len(g:mode_seq) - 1, g:index)
3688 call assert_equal(2, g:n_to_c)
3689 call assert_equal(2, g:c_to_n)
3690 unlet g:n_to_c
3691 unlet g:c_to_n
zeertzjqc9e8fd62022-07-26 18:12:38 +01003692
Bram Moolenaar61c4b042022-10-18 15:10:11 +01003693 let g:n_to_v = 0
3694 au ModeChanged n:v let g:n_to_v += 1
3695 let g:v_to_n = 0
3696 au ModeChanged v:n let g:v_to_n += 1
3697 let g:mode_seq += ['v', 'n']
3698 call feedkeys("v\<C-C>", 'tnix')
3699 call assert_equal(len(g:mode_seq) - 1, g:index)
3700 call assert_equal(1, g:n_to_v)
3701 call assert_equal(1, g:v_to_n)
3702 unlet g:n_to_v
3703 unlet g:v_to_n
3704
zeertzjqc9e8fd62022-07-26 18:12:38 +01003705 au! ModeChanged
3706 delfunc TestMode
3707 unlet! g:mode_seq
3708 unlet! g:index
3709 unlet! g:n_to_any
3710 unlet! g:V_to_v
3711 unlet! g:n_to_i
3712 unlet! g:n_to_niI
3713 unlet! g:niI_to_i
3714 unlet! g:nany_to_i
3715 unlet! g:i_to_n
3716 unlet! g:nori_to_any
3717 unlet! g:i_to_any
3718endfunc
3719
3720func Test_recursive_ModeChanged()
3721 au! ModeChanged * norm 0u
3722 sil! norm 
3723 au! ModeChanged
3724endfunc
3725
3726func Test_ModeChanged_starts_visual()
3727 " This was triggering ModeChanged before setting VIsual, causing a crash.
3728 au! ModeChanged * norm 0u
3729 sil! norm 
3730
3731 au! ModeChanged
3732endfunc
Bram Moolenaar347538f2022-03-26 16:28:06 +00003733
Charlie Grovesfef44852022-04-19 16:24:12 +01003734func Test_noname_autocmd()
3735 augroup test_noname_autocmd_group
3736 autocmd!
3737 autocmd BufEnter * call add(s:li, ["BufEnter", expand("<afile>")])
3738 autocmd BufDelete * call add(s:li, ["BufDelete", expand("<afile>")])
3739 autocmd BufLeave * call add(s:li, ["BufLeave", expand("<afile>")])
3740 autocmd BufUnload * call add(s:li, ["BufUnload", expand("<afile>")])
3741 autocmd BufWipeout * call add(s:li, ["BufWipeout", expand("<afile>")])
3742 augroup END
3743
3744 let s:li = []
3745 edit foo
3746 call assert_equal([['BufUnload', ''], ['BufDelete', ''], ['BufWipeout', ''], ['BufEnter', 'foo']], s:li)
3747
3748 au! test_noname_autocmd_group
3749 augroup! test_noname_autocmd_group
3750endfunc
3751
Yegappan Lakshmanan1755a912022-05-19 10:31:47 +01003752" Test for the autocmd_get() function
3753func Test_autocmd_get()
3754 augroup TestAutoCmdFns
3755 au!
3756 autocmd BufAdd *.vim echo "bufadd-vim"
3757 autocmd BufAdd *.py echo "bufadd-py"
3758 autocmd BufHidden *.vim echo "bufhidden"
3759 augroup END
3760 augroup TestAutoCmdFns2
3761 autocmd BufAdd *.vim echo "bufadd-vim-2"
3762 autocmd BufRead *.a1b2c3 echo "bufadd-vim-2"
3763 augroup END
3764
3765 let l = autocmd_get()
3766 call assert_true(l->len() > 0)
3767
3768 " Test for getting all the autocmds in a group
3769 let expected = [
3770 \ #{cmd: 'echo "bufadd-vim"', group: 'TestAutoCmdFns',
3771 \ pattern: '*.vim', nested: v:false, once: v:false,
3772 \ event: 'BufAdd'},
3773 \ #{cmd: 'echo "bufadd-py"', group: 'TestAutoCmdFns',
3774 \ pattern: '*.py', nested: v:false, once: v:false,
3775 \ event: 'BufAdd'},
3776 \ #{cmd: 'echo "bufhidden"', group: 'TestAutoCmdFns',
3777 \ pattern: '*.vim', nested: v:false,
3778 \ once: v:false, event: 'BufHidden'}]
3779 call assert_equal(expected, autocmd_get(#{group: 'TestAutoCmdFns'}))
3780
3781 " Test for getting autocmds for all the patterns in a group
3782 call assert_equal(expected, autocmd_get(#{group: 'TestAutoCmdFns',
3783 \ event: '*'}))
3784
3785 " Test for getting autocmds for an event in a group
3786 let expected = [
3787 \ #{cmd: 'echo "bufadd-vim"', group: 'TestAutoCmdFns',
3788 \ pattern: '*.vim', nested: v:false, once: v:false,
3789 \ event: 'BufAdd'},
3790 \ #{cmd: 'echo "bufadd-py"', group: 'TestAutoCmdFns',
3791 \ pattern: '*.py', nested: v:false, once: v:false,
3792 \ event: 'BufAdd'}]
3793 call assert_equal(expected, autocmd_get(#{group: 'TestAutoCmdFns',
3794 \ event: 'BufAdd'}))
3795
3796 " Test for getting the autocmds for all the events in a group for particular
3797 " pattern
3798 call assert_equal([{'cmd': 'echo "bufadd-py"', 'group': 'TestAutoCmdFns',
3799 \ 'pattern': '*.py', 'nested': v:false, 'once': v:false,
3800 \ 'event': 'BufAdd'}],
3801 \ autocmd_get(#{group: 'TestAutoCmdFns', event: '*', pattern: '*.py'}))
3802
3803 " Test for getting the autocmds for an events in a group for particular
3804 " pattern
3805 let l = autocmd_get(#{group: 'TestAutoCmdFns', event: 'BufAdd',
3806 \ pattern: '*.vim'})
3807 call assert_equal([
3808 \ #{cmd: 'echo "bufadd-vim"', group: 'TestAutoCmdFns',
3809 \ pattern: '*.vim', nested: v:false, once: v:false,
3810 \ event: 'BufAdd'}], l)
3811
3812 " Test for getting the autocmds for a pattern in a group
3813 let l = autocmd_get(#{group: 'TestAutoCmdFns', pattern: '*.vim'})
3814 call assert_equal([
3815 \ #{cmd: 'echo "bufadd-vim"', group: 'TestAutoCmdFns',
3816 \ pattern: '*.vim', nested: v:false, once: v:false,
3817 \ event: 'BufAdd'},
3818 \ #{cmd: 'echo "bufhidden"', group: 'TestAutoCmdFns',
3819 \ pattern: '*.vim', nested: v:false,
3820 \ once: v:false, event: 'BufHidden'}], l)
3821
3822 " Test for getting the autocmds for a pattern in all the groups
3823 let l = autocmd_get(#{pattern: '*.a1b2c3'})
3824 call assert_equal([{'cmd': 'echo "bufadd-vim-2"', 'group': 'TestAutoCmdFns2',
3825 \ 'pattern': '*.a1b2c3', 'nested': v:false, 'once': v:false,
3826 \ 'event': 'BufRead'}], l)
3827
3828 " Test for getting autocmds for a pattern without any autocmds
3829 call assert_equal([], autocmd_get(#{group: 'TestAutoCmdFns',
3830 \ pattern: '*.abc'}))
3831 call assert_equal([], autocmd_get(#{group: 'TestAutoCmdFns',
3832 \ event: 'BufAdd', pattern: '*.abc'}))
3833 call assert_equal([], autocmd_get(#{group: 'TestAutoCmdFns',
3834 \ event: 'BufWipeout'}))
3835 call assert_fails("call autocmd_get(#{group: 'abc', event: 'BufAdd'})",
3836 \ 'E367:')
3837 let cmd = "echo autocmd_get(#{group: 'TestAutoCmdFns', event: 'abc'})"
3838 call assert_fails(cmd, 'E216:')
3839 call assert_fails("call autocmd_get(#{group: 'abc'})", 'E367:')
3840 call assert_fails("echo autocmd_get(#{event: 'abc'})", 'E216:')
3841
3842 augroup TestAutoCmdFns
3843 au!
3844 augroup END
3845 call assert_equal([], autocmd_get(#{group: 'TestAutoCmdFns'}))
3846
3847 " Test for nested and once autocmds
3848 augroup TestAutoCmdFns
3849 au!
3850 autocmd VimSuspend * ++nested echo "suspend"
3851 autocmd VimResume * ++once echo "resume"
3852 augroup END
3853
3854 let expected = [
3855 \ {'cmd': 'echo "suspend"', 'group': 'TestAutoCmdFns', 'pattern': '*',
3856 \ 'nested': v:true, 'once': v:false, 'event': 'VimSuspend'},
3857 \ {'cmd': 'echo "resume"', 'group': 'TestAutoCmdFns', 'pattern': '*',
3858 \ 'nested': v:false, 'once': v:true, 'event': 'VimResume'}]
3859 call assert_equal(expected, autocmd_get(#{group: 'TestAutoCmdFns'}))
3860
3861 " Test for buffer-local autocmd
3862 augroup TestAutoCmdFns
3863 au!
3864 autocmd TextYankPost <buffer> echo "textyankpost"
3865 augroup END
3866
3867 let expected = [
3868 \ {'cmd': 'echo "textyankpost"', 'group': 'TestAutoCmdFns',
3869 \ 'pattern': '<buffer=' .. bufnr() .. '>', 'nested': v:false,
3870 \ 'once': v:false, 'bufnr': bufnr(), 'event': 'TextYankPost'}]
3871 call assert_equal(expected, autocmd_get(#{group: 'TestAutoCmdFns'}))
3872
3873 augroup TestAutoCmdFns
3874 au!
3875 augroup END
3876 augroup! TestAutoCmdFns
3877 augroup TestAutoCmdFns2
3878 au!
3879 augroup END
3880 augroup! TestAutoCmdFns2
3881
3882 call assert_fails("echo autocmd_get(#{group: []})", 'E730:')
3883 call assert_fails("echo autocmd_get(#{event: {}})", 'E731:')
3884 call assert_fails("echo autocmd_get([])", 'E1206:')
3885endfunc
3886
3887" Test for the autocmd_add() function
3888func Test_autocmd_add()
3889 " Define a single autocmd in a group
3890 call autocmd_add([#{group: 'TestAcSet', event: 'BufAdd', pattern: '*.sh',
3891 \ cmd: 'echo "bufadd"', once: v:true, nested: v:true}])
3892 call assert_equal([#{cmd: 'echo "bufadd"', group: 'TestAcSet',
3893 \ pattern: '*.sh', nested: v:true, once: v:true,
3894 \ event: 'BufAdd'}], autocmd_get(#{group: 'TestAcSet'}))
3895
3896 " Define two autocmds in the same group
3897 call autocmd_delete([#{group: 'TestAcSet'}])
3898 call autocmd_add([#{group: 'TestAcSet', event: 'BufAdd', pattern: '*.sh',
3899 \ cmd: 'echo "bufadd"'},
3900 \ #{group: 'TestAcSet', event: 'BufEnter', pattern: '*.sh',
3901 \ cmd: 'echo "bufenter"'}])
3902 call assert_equal([
3903 \ #{cmd: 'echo "bufadd"', group: 'TestAcSet', pattern: '*.sh',
3904 \ nested: v:false, once: v:false, event: 'BufAdd'},
3905 \ #{cmd: 'echo "bufenter"', group: 'TestAcSet', pattern: '*.sh',
3906 \ nested: v:false, once: v:false, event: 'BufEnter'}],
3907 \ autocmd_get(#{group: 'TestAcSet'}))
3908
3909 " Define a buffer-local autocmd
3910 call autocmd_delete([#{group: 'TestAcSet'}])
3911 call autocmd_add([#{group: 'TestAcSet', event: 'CursorHold',
3912 \ bufnr: bufnr(), cmd: 'echo "cursorhold"'}])
3913 call assert_equal([
3914 \ #{cmd: 'echo "cursorhold"', group: 'TestAcSet',
3915 \ pattern: '<buffer=' .. bufnr() .. '>', nested: v:false,
3916 \ once: v:false, bufnr: bufnr(), event: 'CursorHold'}],
3917 \ autocmd_get(#{group: 'TestAcSet'}))
3918
3919 " Use an invalid buffer number
3920 call autocmd_delete([#{group: 'TestAcSet'}])
3921 call autocmd_add([#{group: 'TestAcSet', event: 'BufEnter',
3922 \ bufnr: -1, cmd: 'echo "bufenter"'}])
3923 let l = [#{group: 'TestAcSet', event: 'BufAdd', bufnr: 9999,
3924 \ cmd: 'echo "bufadd"'}]
3925 call assert_fails("echo autocmd_add(l)", 'E680:')
Yegappan Lakshmanan00e977c2022-06-01 12:31:53 +01003926 let l = [#{group: 'TestAcSet', event: 'BufAdd', bufnr: 9999,
3927 \ pattern: '*.py', cmd: 'echo "bufadd"'}]
3928 call assert_fails("echo autocmd_add(l)", 'E680:')
3929 let l = [#{group: 'TestAcSet', event: 'BufAdd', bufnr: 9999,
3930 \ pattern: ['*.py', '*.c'], cmd: 'echo "bufadd"'}]
3931 call assert_fails("echo autocmd_add(l)", 'E680:')
Yegappan Lakshmanan1755a912022-05-19 10:31:47 +01003932 let l = [#{group: 'TestAcSet', event: 'BufRead', bufnr: [],
3933 \ cmd: 'echo "bufread"'}]
3934 call assert_fails("echo autocmd_add(l)", 'E745:')
3935 call assert_equal([], autocmd_get(#{group: 'TestAcSet'}))
3936
3937 " Add two commands to the same group, event and pattern
3938 call autocmd_delete([#{group: 'TestAcSet'}])
3939 call autocmd_add([#{group: 'TestAcSet', event: 'BufUnload',
3940 \ pattern: 'abc', cmd: 'echo "cmd1"'}])
3941 call autocmd_add([#{group: 'TestAcSet', event: 'BufUnload',
3942 \ pattern: 'abc', cmd: 'echo "cmd2"'}])
3943 call assert_equal([
3944 \ #{cmd: 'echo "cmd1"', group: 'TestAcSet', pattern: 'abc',
3945 \ nested: v:false, once: v:false, event: 'BufUnload'},
3946 \ #{cmd: 'echo "cmd2"', group: 'TestAcSet', pattern: 'abc',
3947 \ nested: v:false, once: v:false, event: 'BufUnload'}],
3948 \ autocmd_get(#{group: 'TestAcSet'}))
3949
3950 " When adding a new autocmd, if the autocmd 'group' is not specified, then
3951 " the current autocmd group should be used.
3952 call autocmd_delete([#{group: 'TestAcSet'}])
3953 augroup TestAcSet
3954 call autocmd_add([#{event: 'BufHidden', pattern: 'abc', cmd: 'echo "abc"'}])
3955 augroup END
3956 call assert_equal([
3957 \ #{cmd: 'echo "abc"', group: 'TestAcSet', pattern: 'abc',
3958 \ nested: v:false, once: v:false, event: 'BufHidden'}],
3959 \ autocmd_get(#{group: 'TestAcSet'}))
3960
Yegappan Lakshmanan971f6822022-05-24 11:40:11 +01003961 " Test for replacing a cmd for an event in a group
3962 call autocmd_delete([#{group: 'TestAcSet'}])
3963 call autocmd_add([#{replace: v:true, group: 'TestAcSet', event: 'BufEnter',
3964 \ pattern: '*.py', cmd: 'echo "bufenter"'}])
3965 call autocmd_add([#{replace: v:true, group: 'TestAcSet', event: 'BufEnter',
3966 \ pattern: '*.py', cmd: 'echo "bufenter"'}])
3967 call assert_equal([
3968 \ #{cmd: 'echo "bufenter"', group: 'TestAcSet', pattern: '*.py',
3969 \ nested: v:false, once: v:false, event: 'BufEnter'}],
3970 \ autocmd_get(#{group: 'TestAcSet'}))
3971
3972 " Test for adding a command for an unsupported autocmd event
Yegappan Lakshmanan1755a912022-05-19 10:31:47 +01003973 let l = [#{group: 'TestAcSet', event: 'abc', pattern: '*.sh',
3974 \ cmd: 'echo "bufadd"'}]
3975 call assert_fails('call autocmd_add(l)', 'E216:')
3976
Yegappan Lakshmanane0ff3a72022-05-27 18:05:33 +01003977 " Test for using a list of events and patterns
3978 call autocmd_delete([#{group: 'TestAcSet'}])
3979 let l = [#{group: 'TestAcSet', event: ['BufEnter', 'BufLeave'],
3980 \ pattern: ['*.py', '*.sh'], cmd: 'echo "bufcmds"'}]
3981 call autocmd_add(l)
3982 call assert_equal([
3983 \ #{cmd: 'echo "bufcmds"', group: 'TestAcSet', pattern: '*.py',
3984 \ nested: v:false, once: v:false, event: 'BufEnter'},
3985 \ #{cmd: 'echo "bufcmds"', group: 'TestAcSet', pattern: '*.sh',
3986 \ nested: v:false, once: v:false, event: 'BufEnter'},
3987 \ #{cmd: 'echo "bufcmds"', group: 'TestAcSet', pattern: '*.py',
3988 \ nested: v:false, once: v:false, event: 'BufLeave'},
3989 \ #{cmd: 'echo "bufcmds"', group: 'TestAcSet', pattern: '*.sh',
3990 \ nested: v:false, once: v:false, event: 'BufLeave'}],
3991 \ autocmd_get(#{group: 'TestAcSet'}))
3992
3993 " Test for invalid values for 'event' item
3994 call autocmd_delete([#{group: 'TestAcSet'}])
3995 let l = [#{group: 'TestAcSet', event: test_null_string(),
3996 \ pattern: "*.py", cmd: 'echo "bufcmds"'}]
3997 call assert_fails('call autocmd_add(l)', 'E928:')
3998 let l = [#{group: 'TestAcSet', event: test_null_list(),
3999 \ pattern: "*.py", cmd: 'echo "bufcmds"'}]
4000 call assert_fails('call autocmd_add(l)', 'E714:')
4001 let l = [#{group: 'TestAcSet', event: {},
4002 \ pattern: "*.py", cmd: 'echo "bufcmds"'}]
4003 call assert_fails('call autocmd_add(l)', 'E777:')
4004 let l = [#{group: 'TestAcSet', event: [{}],
4005 \ pattern: "*.py", cmd: 'echo "bufcmds"'}]
4006 call assert_fails('call autocmd_add(l)', 'E928:')
4007 let l = [#{group: 'TestAcSet', event: [test_null_string()],
4008 \ pattern: "*.py", cmd: 'echo "bufcmds"'}]
4009 call assert_fails('call autocmd_add(l)', 'E928:')
4010 let l = [#{group: 'TestAcSet', event: 'BufEnter,BufLeave',
4011 \ pattern: '*.py', cmd: 'echo "bufcmds"'}]
4012 call assert_fails('call autocmd_add(l)', 'E216:')
4013 let l = [#{group: 'TestAcSet', event: [],
4014 \ pattern: "*.py", cmd: 'echo "bufcmds"'}]
4015 call autocmd_add(l)
4016 let l = [#{group: 'TestAcSet', event: [""],
4017 \ pattern: "*.py", cmd: 'echo "bufcmds"'}]
4018 call assert_fails('call autocmd_add(l)', 'E216:')
4019 let l = [#{group: 'TestAcSet', event: "",
4020 \ pattern: "*.py", cmd: 'echo "bufcmds"'}]
4021 call autocmd_add(l)
4022 call assert_equal([], autocmd_get(#{group: 'TestAcSet'}))
4023
4024 " Test for invalid values for 'pattern' item
4025 let l = [#{group: 'TestAcSet', event: "BufEnter",
4026 \ pattern: test_null_string(), cmd: 'echo "bufcmds"'}]
Yegappan Lakshmanan00e977c2022-06-01 12:31:53 +01004027 call assert_fails('call autocmd_add(l)', 'E928:')
Yegappan Lakshmanane0ff3a72022-05-27 18:05:33 +01004028 let l = [#{group: 'TestAcSet', event: "BufEnter",
4029 \ pattern: test_null_list(), cmd: 'echo "bufcmds"'}]
4030 call assert_fails('call autocmd_add(l)', 'E714:')
4031 let l = [#{group: 'TestAcSet', event: "BufEnter",
4032 \ pattern: {}, cmd: 'echo "bufcmds"'}]
4033 call assert_fails('call autocmd_add(l)', 'E777:')
4034 let l = [#{group: 'TestAcSet', event: "BufEnter",
4035 \ pattern: [{}], cmd: 'echo "bufcmds"'}]
4036 call assert_fails('call autocmd_add(l)', 'E928:')
4037 let l = [#{group: 'TestAcSet', event: "BufEnter",
4038 \ pattern: [test_null_string()], cmd: 'echo "bufcmds"'}]
4039 call assert_fails('call autocmd_add(l)', 'E928:')
4040 let l = [#{group: 'TestAcSet', event: "BufEnter",
4041 \ pattern: [], cmd: 'echo "bufcmds"'}]
4042 call autocmd_add(l)
4043 let l = [#{group: 'TestAcSet', event: "BufEnter",
4044 \ pattern: [""], cmd: 'echo "bufcmds"'}]
4045 call autocmd_add(l)
4046 let l = [#{group: 'TestAcSet', event: "BufEnter",
4047 \ pattern: "", cmd: 'echo "bufcmds"'}]
4048 call autocmd_add(l)
4049 call assert_equal([], autocmd_get(#{group: 'TestAcSet'}))
4050
4051 let l = [#{group: 'TestAcSet', event: 'BufEnter,abc,BufLeave',
4052 \ pattern: '*.py', cmd: 'echo "bufcmds"'}]
4053 call assert_fails('call autocmd_add(l)', 'E216:')
4054
Yegappan Lakshmanan1755a912022-05-19 10:31:47 +01004055 call assert_fails("call autocmd_add({})", 'E1211:')
4056 call assert_equal(v:false, autocmd_add(test_null_list()))
4057 call assert_true(autocmd_add([[]]))
4058 call assert_true(autocmd_add([test_null_dict()]))
4059
4060 augroup TestAcSet
4061 au!
4062 augroup END
4063
4064 call autocmd_add([#{group: 'TestAcSet'}])
4065 call autocmd_add([#{group: 'TestAcSet', event: 'BufAdd'}])
4066 call autocmd_add([#{group: 'TestAcSet', pat: '*.sh'}])
4067 call autocmd_add([#{group: 'TestAcSet', cmd: 'echo "a"'}])
4068 call autocmd_add([#{group: 'TestAcSet', event: 'BufAdd', pat: '*.sh'}])
4069 call autocmd_add([#{group: 'TestAcSet', event: 'BufAdd', cmd: 'echo "a"'}])
4070 call autocmd_add([#{group: 'TestAcSet', pat: '*.sh', cmd: 'echo "a"'}])
4071 call assert_equal([], autocmd_get(#{group: 'TestAcSet'}))
4072
4073 augroup! TestAcSet
4074endfunc
4075
4076" Test for deleting autocmd events and groups
4077func Test_autocmd_delete()
4078 " Delete an event in an autocmd group
4079 augroup TestAcSet
4080 au!
4081 au BufAdd *.sh echo "bufadd"
4082 au BufEnter *.sh echo "bufenter"
4083 augroup END
4084 call autocmd_delete([#{group: 'TestAcSet', event: 'BufAdd'}])
4085 call assert_equal([#{cmd: 'echo "bufenter"', group: 'TestAcSet',
4086 \ pattern: '*.sh', nested: v:false, once: v:false,
4087 \ event: 'BufEnter'}], autocmd_get(#{group: 'TestAcSet'}))
4088
4089 " Delete all the events in an autocmd group
4090 augroup TestAcSet
4091 au BufAdd *.sh echo "bufadd"
4092 augroup END
4093 call autocmd_delete([#{group: 'TestAcSet', event: '*'}])
4094 call assert_equal([], autocmd_get(#{group: 'TestAcSet'}))
4095
4096 " Delete a non-existing autocmd group
4097 call assert_fails("call autocmd_delete([#{group: 'abc'}])", 'E367:')
4098 " Delete a non-existing autocmd event
4099 let l = [#{group: 'TestAcSet', event: 'abc'}]
4100 call assert_fails("call autocmd_delete(l)", 'E216:')
4101 " Delete a non-existing autocmd pattern
4102 let l = [#{group: 'TestAcSet', event: 'BufAdd', pat: 'abc'}]
4103 call assert_true(autocmd_delete(l))
Yegappan Lakshmanan00e977c2022-06-01 12:31:53 +01004104 " Delete an autocmd for a non-existing buffer
4105 let l = [#{event: '*', bufnr: 9999, cmd: 'echo "x"'}]
4106 call assert_fails('call autocmd_delete(l)', 'E680:')
Yegappan Lakshmanan1755a912022-05-19 10:31:47 +01004107
4108 " Delete an autocmd group
4109 augroup TestAcSet
4110 au!
4111 au BufAdd *.sh echo "bufadd"
4112 au BufEnter *.sh echo "bufenter"
4113 augroup END
4114 call autocmd_delete([#{group: 'TestAcSet'}])
4115 call assert_fails("call autocmd_get(#{group: 'TestAcSet'})", 'E367:')
4116
4117 call assert_true(autocmd_delete([[]]))
4118 call assert_true(autocmd_delete([test_null_dict()]))
4119endfunc
4120
Bram Moolenaar8f3c3c62022-10-18 17:05:54 +01004121func Test_autocmd_split_dummy()
4122 " Autocommand trying to split a window containing a dummy buffer.
4123 auto BufReadPre * exe "sbuf " .. expand("<abuf>")
4124 " Avoid the "W11" prompt
4125 au FileChangedShell * let v:fcs_choice = 'reload'
4126 func Xautocmd_changelist()
4127 cal writefile(['Xtestfile2:4:4'], 'Xerr')
4128 edit Xerr
4129 lex 'Xtestfile2:4:4'
4130 endfunc
4131 call Xautocmd_changelist()
Bram Moolenaar53c5c9f2022-10-18 17:25:03 +01004132 " Should get E86, but it doesn't always happen (timing?)
4133 silent! call Xautocmd_changelist()
Bram Moolenaar8f3c3c62022-10-18 17:05:54 +01004134
4135 au! BufReadPre
4136 au! FileChangedShell
4137 delfunc Xautocmd_changelist
4138 bwipe! Xerr
4139 call delete('Xerr')
4140endfunc
4141
Bram Moolenaare76062c2022-11-28 18:51:43 +00004142" This was crashing because there was only one window to execute autocommands
4143" in.
4144func Test_autocmd_nested_setbufvar()
4145 CheckFeature python3
4146
4147 set hidden
4148 edit Xaaa
4149 edit Xbbb
4150 call setline(1, 'bar')
4151 enew
4152 au BufWriteCmd Xbbb ++nested call setbufvar('Xaaa', '&ft', 'foo') | bw! Xaaa
4153 au FileType foo call py3eval('vim.current.buffer.options["cindent"]')
4154 wall
4155
4156 au! BufWriteCmd
4157 au! FileType foo
4158 set nohidden
4159 call delete('Xaaa')
4160 call delete('Xbbb')
4161 %bwipe!
4162endfunc
4163
Christian Brabandt9aee8ec2022-12-16 16:41:23 +00004164func SetupVimTest_shm()
4165 let g:bwe = []
4166 let g:brp = []
4167 set shortmess+=F
4168
4169 let dirname='XVimTestSHM'
4170 call mkdir(dirname, 'R')
4171 call writefile(['test'], dirname .. '/1')
4172 call writefile(['test'], dirname .. '/2')
4173 call writefile(['test'], dirname .. '/3')
4174
4175 augroup test
4176 autocmd!
4177 autocmd BufWinEnter * call add(g:bwe, $'BufWinEnter: {expand('<amatch>')}')
4178 autocmd BufReadPost * call add(g:brp, $'BufReadPost: {expand('<amatch>')}')
4179 augroup END
4180
4181 call setqflist([
4182 \ {'filename': dirname .. '/1', 'lnum': 1, 'col': 1, 'text': 'test', 'vcol': 0},
4183 \ {'filename': dirname .. '/2', 'lnum': 1, 'col': 1, 'text': 'test', 'vcol': 0},
4184 \ {'filename': dirname .. '/3', 'lnum': 1, 'col': 1, 'text': 'test', 'vcol': 0}
4185 \ ])
4186 cdo! substitute/test/TEST
4187
4188 " clean up
4189 noa enew!
4190 set shortmess&vim
4191 augroup test
4192 autocmd!
4193 augroup END
4194 augroup! test
4195endfunc
4196
4197func Test_autocmd_shortmess()
4198 CheckNotMSWindows
4199
4200 call SetupVimTest_shm()
4201 let output = execute(':mess')->split('\n')
4202
4203 let info = copy(output)->filter({idx, val -> val =~# '\d of 3'} )
4204 let bytes = copy(output)->filter({idx, val -> val =~# 'bytes'} )
4205
4206 " We test the following here:
4207 " BufReadPost should have been triggered 3 times, once per file
4208 " BufWinEnter should have been triggered 3 times, once per file
4209 " FileInfoMessage should have been shown 3 times, regardless of shm option
4210 " "(x of 3)" message from :cnext has been shown 3 times
4211
4212 call assert_equal(3, g:brp->len())
4213 call assert_equal(3, g:bwe->len())
4214 call assert_equal(3, info->len())
4215 call assert_equal(3, bytes->len())
4216
4217 delfunc SetupVimTest_shm
4218endfunc
Bram Moolenaare76062c2022-11-28 18:51:43 +00004219
Bram Moolenaarbc2b71d2020-02-17 21:33:30 +01004220" vim: shiftwidth=2 sts=2 expandtab