blob: f2a763add59e302ca6224f0aef924ed2f84bed84 [file] [log] [blame]
Bram Moolenaar14735512016-03-26 21:00:08 +01001" Tests for autocommands
2
Bram Moolenaar8c64a362018-03-23 22:39:31 +01003source shared.vim
Bram Moolenaar8c5a2782019-08-07 23:07:07 +02004source check.vim
Bram Moolenaarcadbe1b2019-09-22 21:50:09 +02005source term_util.vim
LemonBoy09371822022-04-08 15:18:45 +01006source screendump.vim
Bram Moolenaar6f2465d2022-03-22 18:13:01 +00007import './vim9.vim' as v9
Bram Moolenaar8c64a362018-03-23 22:39:31 +01008
Bram Moolenaar1e115362019-01-09 23:01:02 +01009func s:cleanup_buffers() abort
Bram Moolenaarb3435b02016-09-29 20:54:59 +020010 for bnr in range(1, bufnr('$'))
11 if bufloaded(bnr) && bufnr('%') != bnr
12 execute 'bd! ' . bnr
13 endif
14 endfor
Bram Moolenaar04f62f82017-07-19 18:18:39 +020015endfunc
Bram Moolenaarb3435b02016-09-29 20:54:59 +020016
Bram Moolenaar14735512016-03-26 21:00:08 +010017func Test_vim_did_enter()
18 call assert_false(v:vim_did_enter)
19
20 " This script will never reach the main loop, can't check if v:vim_did_enter
21 " becomes one.
22endfunc
Bram Moolenaar40b1b542016-04-20 20:18:23 +020023
Bram Moolenaar75911162020-07-21 19:44:47 +020024" Test for the CursorHold autocmd
25func Test_CursorHold_autocmd()
26 CheckRunVimInTerminal
Bram Moolenaare1f3ab72022-09-04 21:29:08 +010027 call writefile(['one', 'two', 'three'], 'XoneTwoThree', 'D')
Bram Moolenaar75911162020-07-21 19:44:47 +020028 let before =<< trim END
29 set updatetime=10
Bram Moolenaare7cda972022-08-29 11:02:59 +010030 au CursorHold * call writefile([line('.')], 'XCHoutput', 'a')
Bram Moolenaar75911162020-07-21 19:44:47 +020031 END
Bram Moolenaare1f3ab72022-09-04 21:29:08 +010032 call writefile(before, 'XCHinit', 'D')
Bram Moolenaare7cda972022-08-29 11:02:59 +010033 let buf = RunVimInTerminal('-S XCHinit XoneTwoThree', {})
Bram Moolenaar17f67542020-08-20 18:29:13 +020034 call term_sendkeys(buf, "G")
Bram Moolenaar62cd26a2020-10-11 20:08:44 +020035 call term_wait(buf, 50)
Bram Moolenaar75911162020-07-21 19:44:47 +020036 call term_sendkeys(buf, "gg")
37 call term_wait(buf)
Bram Moolenaare7cda972022-08-29 11:02:59 +010038 call WaitForAssert({-> assert_equal(['1'], readfile('XCHoutput')[-1:-1])})
Bram Moolenaar75911162020-07-21 19:44:47 +020039 call term_sendkeys(buf, "j")
40 call term_wait(buf)
Bram Moolenaare7cda972022-08-29 11:02:59 +010041 call WaitForAssert({-> assert_equal(['1', '2'], readfile('XCHoutput')[-2:-1])})
Bram Moolenaar75911162020-07-21 19:44:47 +020042 call term_sendkeys(buf, "j")
43 call term_wait(buf)
Bram Moolenaare7cda972022-08-29 11:02:59 +010044 call WaitForAssert({-> assert_equal(['1', '2', '3'], readfile('XCHoutput')[-3:-1])})
Bram Moolenaar75911162020-07-21 19:44:47 +020045 call StopVimInTerminal(buf)
46
Bram Moolenaare7cda972022-08-29 11:02:59 +010047 call delete('XCHoutput')
Bram Moolenaar75911162020-07-21 19:44:47 +020048endfunc
49
Bram Moolenaarc67e8922016-05-24 16:07:40 +020050if has('timers')
Bram Moolenaar97b00752019-05-12 13:07:14 +020051
Bram Moolenaarc67e8922016-05-24 16:07:40 +020052 func ExitInsertMode(id)
53 call feedkeys("\<Esc>")
54 endfunc
55
56 func Test_cursorhold_insert()
Bram Moolenaarf18c4db2016-09-08 22:10:06 +020057 " Need to move the cursor.
58 call feedkeys("ggG", "xt")
59
Bram Moolenaarc67e8922016-05-24 16:07:40 +020060 let g:triggered = 0
61 au CursorHoldI * let g:triggered += 1
62 set updatetime=20
Bram Moolenaar92bb83e2021-02-03 23:04:46 +010063 call timer_start(200, 'ExitInsertMode')
Bram Moolenaarc67e8922016-05-24 16:07:40 +020064 call feedkeys('a', 'x!')
Bram Moolenaar3b014be2022-11-13 17:53:46 +000065 sleep 30m
Bram Moolenaarc67e8922016-05-24 16:07:40 +020066 call assert_equal(1, g:triggered)
Bram Moolenaar26d98212019-01-27 22:32:55 +010067 unlet g:triggered
68 au! CursorHoldI
69 set updatetime&
70 endfunc
71
72 func Test_cursorhold_insert_with_timer_interrupt()
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +020073 CheckFeature job
Bram Moolenaar26d98212019-01-27 22:32:55 +010074 " Need to move the cursor.
75 call feedkeys("ggG", "xt")
76
77 " Confirm the timer invoked in exit_cb of the job doesn't disturb
78 " CursorHoldI event.
79 let g:triggered = 0
80 au CursorHoldI * let g:triggered += 1
Bram Moolenaar62cd26a2020-10-11 20:08:44 +020081 set updatetime=100
Bram Moolenaar26d98212019-01-27 22:32:55 +010082 call job_start(has('win32') ? 'cmd /c echo:' : 'echo',
Bram Moolenaar62cd26a2020-10-11 20:08:44 +020083 \ {'exit_cb': {-> timer_start(200, 'ExitInsertMode')}})
Bram Moolenaar26d98212019-01-27 22:32:55 +010084 call feedkeys('a', 'x!')
85 call assert_equal(1, g:triggered)
86 unlet g:triggered
Bram Moolenaare99e8442016-07-26 20:43:40 +020087 au! CursorHoldI
Bram Moolenaaraeac9002016-09-06 22:15:08 +020088 set updatetime&
Bram Moolenaarc67e8922016-05-24 16:07:40 +020089 endfunc
90
91 func Test_cursorhold_insert_ctrl_x()
92 let g:triggered = 0
93 au CursorHoldI * let g:triggered += 1
94 set updatetime=20
95 call timer_start(100, 'ExitInsertMode')
96 " CursorHoldI does not trigger after CTRL-X
97 call feedkeys("a\<C-X>", 'x!')
98 call assert_equal(0, g:triggered)
Bram Moolenaar26d98212019-01-27 22:32:55 +010099 unlet g:triggered
Bram Moolenaare99e8442016-07-26 20:43:40 +0200100 au! CursorHoldI
Bram Moolenaaraeac9002016-09-06 22:15:08 +0200101 set updatetime&
Bram Moolenaarc67e8922016-05-24 16:07:40 +0200102 endfunc
Bram Moolenaar97b00752019-05-12 13:07:14 +0200103
Bram Moolenaar5a9357d2021-10-03 16:22:05 +0100104 func Test_cursorhold_insert_ctrl_g_U()
105 au CursorHoldI * :
106 set updatetime=20
107 new
108 call timer_start(100, { -> feedkeys("\<Left>foo\<Esc>", 't') })
109 call feedkeys("i()\<C-g>U", 'tx!')
110 sleep 200m
111 call assert_equal('(foo)', getline(1))
112 undo
113 call assert_equal('', getline(1))
114
115 bwipe!
116 au! CursorHoldI
117 set updatetime&
118 endfunc
119
Bram Moolenaar97b00752019-05-12 13:07:14 +0200120 func Test_OptionSet_modeline()
121 call test_override('starting', 1)
122 au! OptionSet
123 augroup set_tabstop
124 au OptionSet tabstop call timer_start(1, {-> execute("echo 'Handler called'", "")})
125 augroup END
Bram Moolenaare1f3ab72022-09-04 21:29:08 +0100126 call writefile(['vim: set ts=7 sw=5 :', 'something'], 'XoptionsetModeline', 'D')
Bram Moolenaar97b00752019-05-12 13:07:14 +0200127 set modeline
128 let v:errmsg = ''
129 call assert_fails('split XoptionsetModeline', 'E12:')
130 call assert_equal(7, &ts)
131 call assert_equal('', v:errmsg)
132
133 augroup set_tabstop
134 au!
135 augroup END
136 bwipe!
137 set ts&
Bram Moolenaar97b00752019-05-12 13:07:14 +0200138 call test_override('starting', 0)
139 endfunc
140
141endif "has('timers')
Bram Moolenaar40b1b542016-04-20 20:18:23 +0200142
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200143func Test_bufunload()
Bram Moolenaarc67e8922016-05-24 16:07:40 +0200144 augroup test_bufunload_group
145 autocmd!
146 autocmd BufUnload * call add(s:li, "bufunload")
147 autocmd BufDelete * call add(s:li, "bufdelete")
148 autocmd BufWipeout * call add(s:li, "bufwipeout")
149 augroup END
Bram Moolenaar40b1b542016-04-20 20:18:23 +0200150
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100151 let s:li = []
Bram Moolenaarc67e8922016-05-24 16:07:40 +0200152 new
153 setlocal bufhidden=
154 bunload
155 call assert_equal(["bufunload", "bufdelete"], s:li)
Bram Moolenaar40b1b542016-04-20 20:18:23 +0200156
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100157 let s:li = []
Bram Moolenaarc67e8922016-05-24 16:07:40 +0200158 new
159 setlocal bufhidden=delete
160 bunload
161 call assert_equal(["bufunload", "bufdelete"], s:li)
162
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100163 let s:li = []
Bram Moolenaarc67e8922016-05-24 16:07:40 +0200164 new
165 setlocal bufhidden=unload
166 bwipeout
167 call assert_equal(["bufunload", "bufdelete", "bufwipeout"], s:li)
168
Bram Moolenaare99e8442016-07-26 20:43:40 +0200169 au! test_bufunload_group
Bram Moolenaarc67e8922016-05-24 16:07:40 +0200170 augroup! test_bufunload_group
Bram Moolenaar40b1b542016-04-20 20:18:23 +0200171endfunc
Bram Moolenaar30445cb2016-07-09 15:21:02 +0200172
173" SEGV occurs in older versions. (At least 7.4.2005 or older)
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200174func Test_autocmd_bufunload_with_tabnext()
Bram Moolenaar30445cb2016-07-09 15:21:02 +0200175 tabedit
176 tabfirst
177
178 augroup test_autocmd_bufunload_with_tabnext_group
179 autocmd!
180 autocmd BufUnload <buffer> tabnext
181 augroup END
182
183 quit
184 call assert_equal(2, tabpagenr('$'))
185
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200186 autocmd! test_autocmd_bufunload_with_tabnext_group
Bram Moolenaar30445cb2016-07-09 15:21:02 +0200187 augroup! test_autocmd_bufunload_with_tabnext_group
188 tablast
189 quit
190endfunc
Bram Moolenaarc917da42016-07-19 22:31:36 +0200191
Bram Moolenaar5ed58c72021-01-28 14:24:55 +0100192func Test_argdelete_in_next()
193 au BufNew,BufEnter,BufLeave,BufWinEnter * argdel
194 call assert_fails('next a b', 'E1156:')
195 au! BufNew,BufEnter,BufLeave,BufWinEnter *
196endfunc
197
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200198func Test_autocmd_bufwinleave_with_tabfirst()
Bram Moolenaarf9e687e2016-09-04 21:33:09 +0200199 tabedit
200 augroup sample
201 autocmd!
202 autocmd BufWinLeave <buffer> tabfirst
203 augroup END
204 call setline(1, ['a', 'b', 'c'])
205 edit! a.txt
Bram Moolenaarf18c4db2016-09-08 22:10:06 +0200206 tabclose
Bram Moolenaarf9e687e2016-09-04 21:33:09 +0200207endfunc
208
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200209" SEGV occurs in older versions. (At least 7.4.2321 or older)
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200210func Test_autocmd_bufunload_avoiding_SEGV_01()
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200211 split aa.txt
212 let lastbuf = bufnr('$')
213
214 augroup test_autocmd_bufunload
215 autocmd!
216 exe 'autocmd BufUnload <buffer> ' . (lastbuf + 1) . 'bwipeout!'
217 augroup END
218
Bram Moolenaar28ee8922020-10-28 20:20:00 +0100219 call assert_fails('edit bb.txt', 'E937:')
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200220
221 autocmd! test_autocmd_bufunload
222 augroup! test_autocmd_bufunload
223 bwipe! aa.txt
224 bwipe! bb.txt
225endfunc
226
227" SEGV occurs in older versions. (At least 7.4.2321 or older)
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200228func Test_autocmd_bufunload_avoiding_SEGV_02()
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200229 setlocal buftype=nowrite
230 let lastbuf = bufnr('$')
231
232 augroup test_autocmd_bufunload
233 autocmd!
234 exe 'autocmd BufUnload <buffer> ' . (lastbuf + 1) . 'bwipeout!'
235 augroup END
236
237 normal! i1
238 call assert_fails('edit a.txt', 'E517:')
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200239
240 autocmd! test_autocmd_bufunload
241 augroup! test_autocmd_bufunload
242 bwipe! a.txt
243endfunc
244
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100245func Test_autocmd_dummy_wipeout()
246 " prepare files
Bram Moolenaare1f3ab72022-09-04 21:29:08 +0100247 call writefile([''], 'Xdummywipetest1.txt', 'D')
248 call writefile([''], 'Xdummywipetest2.txt', 'D')
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100249 augroup test_bufunload_group
250 autocmd!
251 autocmd BufUnload * call add(s:li, "bufunload")
252 autocmd BufDelete * call add(s:li, "bufdelete")
253 autocmd BufWipeout * call add(s:li, "bufwipeout")
254 augroup END
255
256 let s:li = []
257 split Xdummywipetest1.txt
258 silent! vimgrep /notmatched/ Xdummywipetest*
259 call assert_equal(["bufunload", "bufwipeout"], s:li)
260
261 bwipeout
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100262 au! test_bufunload_group
263 augroup! test_bufunload_group
264endfunc
265
Bram Moolenaarc917da42016-07-19 22:31:36 +0200266func Test_win_tab_autocmd()
267 let g:record = []
268
269 augroup testing
270 au WinNew * call add(g:record, 'WinNew')
naohiro ono23beefe2021-11-13 12:38:49 +0000271 au WinClosed * call add(g:record, 'WinClosed')
Bram Moolenaar94722c52023-01-28 19:19:03 +0000272 au WinEnter * call add(g:record, 'WinEnter')
273 au WinLeave * call add(g:record, 'WinLeave')
Bram Moolenaarc917da42016-07-19 22:31:36 +0200274 au TabNew * call add(g:record, 'TabNew')
Bram Moolenaar12c11d52016-07-19 23:13:03 +0200275 au TabClosed * call add(g:record, 'TabClosed')
Bram Moolenaarc917da42016-07-19 22:31:36 +0200276 au TabEnter * call add(g:record, 'TabEnter')
277 au TabLeave * call add(g:record, 'TabLeave')
278 augroup END
279
280 split
281 tabnew
282 close
283 close
284
285 call assert_equal([
286 \ 'WinLeave', 'WinNew', 'WinEnter',
287 \ 'WinLeave', 'TabLeave', 'WinNew', 'WinEnter', 'TabNew', 'TabEnter',
naohiro ono23beefe2021-11-13 12:38:49 +0000288 \ 'WinLeave', 'TabLeave', 'WinClosed', 'TabClosed', 'WinEnter', 'TabEnter',
289 \ 'WinLeave', 'WinClosed', 'WinEnter'
Bram Moolenaarc917da42016-07-19 22:31:36 +0200290 \ ], g:record)
291
Bram Moolenaar12c11d52016-07-19 23:13:03 +0200292 let g:record = []
293 tabnew somefile
294 tabnext
295 bwipe somefile
296
297 call assert_equal([
298 \ 'WinLeave', 'TabLeave', 'WinNew', 'WinEnter', 'TabNew', 'TabEnter',
299 \ 'WinLeave', 'TabLeave', 'WinEnter', 'TabEnter',
naohiro ono23beefe2021-11-13 12:38:49 +0000300 \ 'WinClosed', 'TabClosed'
Bram Moolenaar12c11d52016-07-19 23:13:03 +0200301 \ ], g:record)
302
Bram Moolenaarc917da42016-07-19 22:31:36 +0200303 augroup testing
304 au!
305 augroup END
306 unlet g:record
307endfunc
Bram Moolenaare99e8442016-07-26 20:43:40 +0200308
Bram Moolenaar35fc61c2022-11-22 12:40:50 +0000309func Test_WinResized()
310 CheckRunVimInTerminal
311
312 let lines =<< trim END
313 set scrolloff=0
314 call setline(1, ['111', '222'])
315 vnew
316 call setline(1, ['aaa', 'bbb'])
317 new
318 call setline(1, ['foo', 'bar'])
319
320 let g:resized = 0
321 au WinResized * let g:resized += 1
322
323 func WriteResizedEvent()
324 call writefile([json_encode(v:event)], 'XresizeEvent')
325 endfunc
326 au WinResized * call WriteResizedEvent()
327 END
328 call writefile(lines, 'Xtest_winresized', 'D')
329 let buf = RunVimInTerminal('-S Xtest_winresized', {'rows': 10})
330
331 " redraw now to avoid a redraw after the :echo command
332 call term_sendkeys(buf, ":redraw!\<CR>")
333 call TermWait(buf)
334
335 call term_sendkeys(buf, ":echo g:resized\<CR>")
336 call WaitForAssert({-> assert_match('^0$', term_getline(buf, 10))}, 1000)
337
338 " increase window height, two windows will be reported
339 call term_sendkeys(buf, "\<C-W>+")
340 call TermWait(buf)
341 call term_sendkeys(buf, ":echo g:resized\<CR>")
342 call WaitForAssert({-> assert_match('^1$', term_getline(buf, 10))}, 1000)
343
344 let event = readfile('XresizeEvent')[0]->json_decode()
345 call assert_equal({
346 \ 'windows': [1002, 1001],
347 \ }, event)
348
349 " increase window width, three windows will be reported
350 call term_sendkeys(buf, "\<C-W>>")
351 call TermWait(buf)
352 call term_sendkeys(buf, ":echo g:resized\<CR>")
353 call WaitForAssert({-> assert_match('^2$', term_getline(buf, 10))}, 1000)
354
355 let event = readfile('XresizeEvent')[0]->json_decode()
356 call assert_equal({
357 \ 'windows': [1002, 1001, 1000],
358 \ }, event)
359
360 call delete('XresizeEvent')
361 call StopVimInTerminal(buf)
362endfunc
363
LemonBoy09371822022-04-08 15:18:45 +0100364func Test_WinScrolled()
365 CheckRunVimInTerminal
366
367 let lines =<< trim END
zeertzjqd58862d2022-04-12 11:32:48 +0100368 set nowrap scrolloff=0
369 for ii in range(1, 18)
370 call setline(ii, repeat(nr2char(96 + ii), ii * 2))
371 endfor
372 let win_id = win_getid()
373 let g:matched = v:false
Bram Moolenaar35fc61c2022-11-22 12:40:50 +0000374 func WriteScrollEvent()
375 call writefile([json_encode(v:event)], 'XscrollEvent')
376 endfunc
zeertzjqd58862d2022-04-12 11:32:48 +0100377 execute 'au WinScrolled' win_id 'let g:matched = v:true'
378 let g:scrolled = 0
379 au WinScrolled * let g:scrolled += 1
380 au WinScrolled * let g:amatch = str2nr(expand('<amatch>'))
381 au WinScrolled * let g:afile = str2nr(expand('<afile>'))
Bram Moolenaar35fc61c2022-11-22 12:40:50 +0000382 au WinScrolled * call WriteScrollEvent()
LemonBoy09371822022-04-08 15:18:45 +0100383 END
Bram Moolenaare1f3ab72022-09-04 21:29:08 +0100384 call writefile(lines, 'Xtest_winscrolled', 'D')
LemonBoy09371822022-04-08 15:18:45 +0100385 let buf = RunVimInTerminal('-S Xtest_winscrolled', {'rows': 6})
386
387 call term_sendkeys(buf, ":echo g:scrolled\<CR>")
388 call WaitForAssert({-> assert_match('^0 ', term_getline(buf, 6))}, 1000)
389
390 " Scroll left/right in Normal mode.
391 call term_sendkeys(buf, "zlzh:echo g:scrolled\<CR>")
392 call WaitForAssert({-> assert_match('^2 ', term_getline(buf, 6))}, 1000)
393
Bram Moolenaar35fc61c2022-11-22 12:40:50 +0000394 let event = readfile('XscrollEvent')[0]->json_decode()
395 call assert_equal({
zeertzjq3fc84dc2022-12-07 09:17:59 +0000396 \ 'all': {'leftcol': 1, 'topline': 0, 'topfill': 0, 'width': 0, 'height': 0, 'skipcol': 0},
397 \ '1000': {'leftcol': -1, 'topline': 0, 'topfill': 0, 'width': 0, 'height': 0, 'skipcol': 0}
Bram Moolenaar35fc61c2022-11-22 12:40:50 +0000398 \ }, event)
399
LemonBoy09371822022-04-08 15:18:45 +0100400 " Scroll up/down in Normal mode.
401 call term_sendkeys(buf, "\<c-e>\<c-y>:echo g:scrolled\<CR>")
402 call WaitForAssert({-> assert_match('^4 ', term_getline(buf, 6))}, 1000)
403
Bram Moolenaar35fc61c2022-11-22 12:40:50 +0000404 let event = readfile('XscrollEvent')[0]->json_decode()
405 call assert_equal({
zeertzjq3fc84dc2022-12-07 09:17:59 +0000406 \ 'all': {'leftcol': 0, 'topline': 1, 'topfill': 0, 'width': 0, 'height': 0, 'skipcol': 0},
407 \ '1000': {'leftcol': 0, 'topline': -1, 'topfill': 0, 'width': 0, 'height': 0, 'skipcol': 0}
Bram Moolenaar35fc61c2022-11-22 12:40:50 +0000408 \ }, event)
409
LemonBoy09371822022-04-08 15:18:45 +0100410 " Scroll up/down in Insert mode.
411 call term_sendkeys(buf, "Mi\<c-x>\<c-e>\<Esc>i\<c-x>\<c-y>\<Esc>")
412 call term_sendkeys(buf, ":echo g:scrolled\<CR>")
413 call WaitForAssert({-> assert_match('^6 ', term_getline(buf, 6))}, 1000)
414
Bram Moolenaar35fc61c2022-11-22 12:40:50 +0000415 let event = readfile('XscrollEvent')[0]->json_decode()
416 call assert_equal({
zeertzjq3fc84dc2022-12-07 09:17:59 +0000417 \ 'all': {'leftcol': 0, 'topline': 1, 'topfill': 0, 'width': 0, 'height': 0, 'skipcol': 0},
418 \ '1000': {'leftcol': 0, 'topline': -1, 'topfill': 0, 'width': 0, 'height': 0, 'skipcol': 0}
Bram Moolenaar35fc61c2022-11-22 12:40:50 +0000419 \ }, event)
420
LemonBoy09371822022-04-08 15:18:45 +0100421 " Scroll the window horizontally to focus the last letter of the third line
422 " containing only six characters. Moving to the previous and shorter lines
423 " should trigger another autocommand as Vim has to make them visible.
424 call term_sendkeys(buf, "5zl2k")
425 call term_sendkeys(buf, ":echo g:scrolled\<CR>")
426 call WaitForAssert({-> assert_match('^8 ', term_getline(buf, 6))}, 1000)
427
Bram Moolenaar35fc61c2022-11-22 12:40:50 +0000428 let event = readfile('XscrollEvent')[0]->json_decode()
429 call assert_equal({
zeertzjq3fc84dc2022-12-07 09:17:59 +0000430 \ 'all': {'leftcol': 5, 'topline': 0, 'topfill': 0, 'width': 0, 'height': 0, 'skipcol': 0},
431 \ '1000': {'leftcol': -5, 'topline': 0, 'topfill': 0, 'width': 0, 'height': 0, 'skipcol': 0}
Bram Moolenaar35fc61c2022-11-22 12:40:50 +0000432 \ }, event)
433
LemonBoy09371822022-04-08 15:18:45 +0100434 " Ensure the command was triggered for the specified window ID.
435 call term_sendkeys(buf, ":echo g:matched\<CR>")
436 call WaitForAssert({-> assert_match('^v:true ', term_getline(buf, 6))}, 1000)
437
438 " Ensure the expansion of <amatch> and <afile> matches the window ID.
439 call term_sendkeys(buf, ":echo g:amatch == win_id && g:afile == win_id\<CR>")
440 call WaitForAssert({-> assert_match('^v:true ', term_getline(buf, 6))}, 1000)
441
Bram Moolenaar35fc61c2022-11-22 12:40:50 +0000442 call delete('XscrollEvent')
LemonBoy09371822022-04-08 15:18:45 +0100443 call StopVimInTerminal(buf)
LemonBoy09371822022-04-08 15:18:45 +0100444endfunc
445
LemonBoy66e13ae2022-04-21 22:52:11 +0100446func Test_WinScrolled_mouse()
447 CheckRunVimInTerminal
448
449 let lines =<< trim END
450 set nowrap scrolloff=0
451 set mouse=a term=xterm ttymouse=sgr mousetime=200 clipboard=
452 call setline(1, ['foo']->repeat(32))
453 split
454 let g:scrolled = 0
455 au WinScrolled * let g:scrolled += 1
456 END
Bram Moolenaare1f3ab72022-09-04 21:29:08 +0100457 call writefile(lines, 'Xtest_winscrolled_mouse', 'D')
LemonBoy66e13ae2022-04-21 22:52:11 +0100458 let buf = RunVimInTerminal('-S Xtest_winscrolled_mouse', {'rows': 10})
459
460 " With the upper split focused, send a scroll-down event to the unfocused one.
461 call test_setmouse(7, 1)
462 call term_sendkeys(buf, "\<ScrollWheelDown>")
463 call TermWait(buf)
464 call term_sendkeys(buf, ":echo g:scrolled\<CR>")
465 call WaitForAssert({-> assert_match('^1', term_getline(buf, 10))}, 1000)
466
467 " Again, but this time while we're in insert mode.
468 call term_sendkeys(buf, "i\<ScrollWheelDown>\<Esc>")
469 call TermWait(buf)
470 call term_sendkeys(buf, ":echo g:scrolled\<CR>")
471 call WaitForAssert({-> assert_match('^2', term_getline(buf, 10))}, 1000)
472
473 call StopVimInTerminal(buf)
LemonBoy66e13ae2022-04-21 22:52:11 +0100474endfunc
475
zeertzjqd58862d2022-04-12 11:32:48 +0100476func Test_WinScrolled_close_curwin()
477 CheckRunVimInTerminal
478
479 let lines =<< trim END
480 set nowrap scrolloff=0
481 call setline(1, ['aaa', 'bbb'])
482 vsplit
483 au WinScrolled * close
484 au VimLeave * call writefile(['123456'], 'Xtestout')
485 END
Bram Moolenaare1f3ab72022-09-04 21:29:08 +0100486 call writefile(lines, 'Xtest_winscrolled_close_curwin', 'D')
zeertzjqd58862d2022-04-12 11:32:48 +0100487 let buf = RunVimInTerminal('-S Xtest_winscrolled_close_curwin', {'rows': 6})
488
489 " This was using freed memory
490 call term_sendkeys(buf, "\<C-E>")
491 call TermWait(buf)
492 call StopVimInTerminal(buf)
493
Bram Moolenaar0a60f792022-11-19 21:18:11 +0000494 " check the startup script finished to the end
zeertzjqd58862d2022-04-12 11:32:48 +0100495 call assert_equal(['123456'], readfile('Xtestout'))
zeertzjqd58862d2022-04-12 11:32:48 +0100496 call delete('Xtestout')
497endfunc
498
Bram Moolenaar0a60f792022-11-19 21:18:11 +0000499func Test_WinScrolled_once_only()
500 CheckRunVimInTerminal
501
502 let lines =<< trim END
503 set cmdheight=2
504 call setline(1, ['aaa', 'bbb'])
505 let trigger_count = 0
506 func ShowInfo(id)
507 echo g:trigger_count g:winid winlayout()
508 endfunc
509
510 vsplit
511 split
512 " use a timer to show the info after a redraw
513 au WinScrolled * let trigger_count += 1 | let winid = expand('<amatch>') | call timer_start(100, 'ShowInfo')
514 wincmd j
515 wincmd l
516 END
517 call writefile(lines, 'Xtest_winscrolled_once', 'D')
518 let buf = RunVimInTerminal('-S Xtest_winscrolled_once', #{rows: 10, cols: 60, statusoff: 2})
519
520 call term_sendkeys(buf, "\<C-E>")
521 call VerifyScreenDump(buf, 'Test_winscrolled_once_only_1', {})
522
523 call StopVimInTerminal(buf)
524endfunc
525
Bram Moolenaar29967732022-11-20 12:11:45 +0000526" Check that WinScrolled is not triggered immediately when defined and there
527" are split windows.
528func Test_WinScrolled_not_when_defined()
529 CheckRunVimInTerminal
530
531 let lines =<< trim END
532 call setline(1, ['aaa', 'bbb'])
533 echo 'nothing happened'
534 func ShowTriggered(id)
535 echo 'triggered'
536 endfunc
537 END
538 call writefile(lines, 'Xtest_winscrolled_not', 'D')
539 let buf = RunVimInTerminal('-S Xtest_winscrolled_not', #{rows: 10, cols: 60, statusoff: 2})
540 call term_sendkeys(buf, ":split\<CR>")
541 call TermWait(buf)
542 " use a timer to show the message after redrawing
543 call term_sendkeys(buf, ":au WinScrolled * call timer_start(100, 'ShowTriggered')\<CR>")
544 call VerifyScreenDump(buf, 'Test_winscrolled_not_when_defined_1', {})
545
546 call term_sendkeys(buf, "\<C-E>")
547 call VerifyScreenDump(buf, 'Test_winscrolled_not_when_defined_2', {})
548
549 call StopVimInTerminal(buf)
550endfunc
551
zeertzjq670ab032022-08-28 19:16:15 +0100552func Test_WinScrolled_long_wrapped()
553 CheckRunVimInTerminal
554
555 let lines =<< trim END
556 set scrolloff=0
557 let height = winheight(0)
558 let width = winwidth(0)
559 let g:scrolled = 0
560 au WinScrolled * let g:scrolled += 1
561 call setline(1, repeat('foo', height * width))
562 call cursor(1, height * width)
563 END
Bram Moolenaare1f3ab72022-09-04 21:29:08 +0100564 call writefile(lines, 'Xtest_winscrolled_long_wrapped', 'D')
zeertzjq670ab032022-08-28 19:16:15 +0100565 let buf = RunVimInTerminal('-S Xtest_winscrolled_long_wrapped', {'rows': 6})
566
567 call term_sendkeys(buf, ":echo g:scrolled\<CR>")
568 call WaitForAssert({-> assert_match('^0 ', term_getline(buf, 6))}, 1000)
569
570 call term_sendkeys(buf, 'gj')
571 call term_sendkeys(buf, ":echo g:scrolled\<CR>")
572 call WaitForAssert({-> assert_match('^1 ', term_getline(buf, 6))}, 1000)
573
574 call term_sendkeys(buf, '0')
575 call term_sendkeys(buf, ":echo g:scrolled\<CR>")
576 call WaitForAssert({-> assert_match('^2 ', term_getline(buf, 6))}, 1000)
577
578 call term_sendkeys(buf, '$')
579 call term_sendkeys(buf, ":echo g:scrolled\<CR>")
580 call WaitForAssert({-> assert_match('^3 ', term_getline(buf, 6))}, 1000)
Bram Moolenaar23526d22022-12-05 15:50:41 +0000581
582 call StopVimInTerminal(buf)
zeertzjq670ab032022-08-28 19:16:15 +0100583endfunc
584
zeertzjq3fc84dc2022-12-07 09:17:59 +0000585func Test_WinScrolled_diff()
586 CheckRunVimInTerminal
587
588 let lines =<< trim END
589 set diffopt+=foldcolumn:0
590 call setline(1, ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i'])
591 vnew
592 call setline(1, ['d', 'e', 'f', 'g', 'h', 'i'])
593 windo diffthis
594 func WriteScrollEvent()
595 call writefile([json_encode(v:event)], 'XscrollEvent')
596 endfunc
597 au WinScrolled * call WriteScrollEvent()
598 END
599 call writefile(lines, 'Xtest_winscrolled_diff', 'D')
600 let buf = RunVimInTerminal('-S Xtest_winscrolled_diff', {'rows': 8})
601
602 call term_sendkeys(buf, "\<C-E>")
603 call WaitForAssert({-> assert_match('^d', term_getline(buf, 3))}, 1000)
604
605 let event = readfile('XscrollEvent')[0]->json_decode()
606 call assert_equal({
607 \ 'all': {'leftcol': 0, 'topline': 1, 'topfill': 1, 'width': 0, 'height': 0, 'skipcol': 0},
608 \ '1000': {'leftcol': 0, 'topline': 1, 'topfill': 0, 'width': 0, 'height': 0, 'skipcol': 0},
609 \ '1001': {'leftcol': 0, 'topline': 0, 'topfill': -1, 'width': 0, 'height': 0, 'skipcol': 0}
610 \ }, event)
611
612 call term_sendkeys(buf, "2\<C-E>")
613 call WaitForAssert({-> assert_match('^f', term_getline(buf, 3))}, 1000)
614
615 let event = readfile('XscrollEvent')[0]->json_decode()
616 call assert_equal({
617 \ 'all': {'leftcol': 0, 'topline': 2, 'topfill': 2, 'width': 0, 'height': 0, 'skipcol': 0},
618 \ '1000': {'leftcol': 0, 'topline': 2, 'topfill': 0, 'width': 0, 'height': 0, 'skipcol': 0},
619 \ '1001': {'leftcol': 0, 'topline': 0, 'topfill': -2, 'width': 0, 'height': 0, 'skipcol': 0}
620 \ }, event)
621
622 call term_sendkeys(buf, "\<C-E>")
623 call WaitForAssert({-> assert_match('^g', term_getline(buf, 3))}, 1000)
624
625 let event = readfile('XscrollEvent')[0]->json_decode()
626 call assert_equal({
627 \ 'all': {'leftcol': 0, 'topline': 2, 'topfill': 0, 'width': 0, 'height': 0, 'skipcol': 0},
628 \ '1000': {'leftcol': 0, 'topline': 1, 'topfill': 0, 'width': 0, 'height': 0, 'skipcol': 0},
629 \ '1001': {'leftcol': 0, 'topline': 1, 'topfill': 0, 'width': 0, 'height': 0, 'skipcol': 0}
630 \ }, event)
631
632 call term_sendkeys(buf, "2\<C-Y>")
633 call WaitForAssert({-> assert_match('^e', term_getline(buf, 3))}, 1000)
634
635 let event = readfile('XscrollEvent')[0]->json_decode()
636 call assert_equal({
637 \ 'all': {'leftcol': 0, 'topline': 3, 'topfill': 1, 'width': 0, 'height': 0, 'skipcol': 0},
638 \ '1000': {'leftcol': 0, 'topline': -2, 'topfill': 0, 'width': 0, 'height': 0, 'skipcol': 0},
639 \ '1001': {'leftcol': 0, 'topline': -1, 'topfill': 1, 'width': 0, 'height': 0, 'skipcol': 0}
640 \ }, event)
641
642 call StopVimInTerminal(buf)
Dominique Pelle541c87c2023-01-17 21:20:44 +0000643 call delete('XscrollEvent')
zeertzjq3fc84dc2022-12-07 09:17:59 +0000644endfunc
645
naohiro ono23beefe2021-11-13 12:38:49 +0000646func Test_WinClosed()
647 " Test that the pattern is matched against the closed window's ID, and both
648 " <amatch> and <afile> are set to it.
649 new
650 let winid = win_getid()
651 let g:matched = v:false
652 augroup test-WinClosed
653 autocmd!
654 execute 'autocmd WinClosed' winid 'let g:matched = v:true'
655 autocmd WinClosed * let g:amatch = str2nr(expand('<amatch>'))
656 autocmd WinClosed * let g:afile = str2nr(expand('<afile>'))
657 augroup END
658 close
659 call assert_true(g:matched)
660 call assert_equal(winid, g:amatch)
661 call assert_equal(winid, g:afile)
662
663 " Test that WinClosed is non-recursive.
664 new
665 new
666 call assert_equal(3, winnr('$'))
667 let g:triggered = 0
668 augroup test-WinClosed
669 autocmd!
670 autocmd WinClosed * let g:triggered += 1
671 autocmd WinClosed * 2 wincmd c
672 augroup END
673 close
674 call assert_equal(1, winnr('$'))
675 call assert_equal(1, g:triggered)
676
677 autocmd! test-WinClosed
678 augroup! test-WinClosed
679 unlet g:matched
680 unlet g:amatch
681 unlet g:afile
682 unlet g:triggered
683endfunc
684
Bram Moolenaarc947b9a2022-04-06 17:59:21 +0100685func Test_WinClosed_throws()
686 vnew
687 let bnr = bufnr()
688 call assert_equal(1, bufloaded(bnr))
689 augroup test-WinClosed
690 autocmd WinClosed * throw 'foo'
691 augroup END
692 try
693 close
694 catch /.*/
695 endtry
696 call assert_equal(0, bufloaded(bnr))
697
698 autocmd! test-WinClosed
699 augroup! test-WinClosed
700endfunc
701
zeertzjq6a069402022-04-07 14:08:29 +0100702func Test_WinClosed_throws_with_tabs()
703 tabnew
704 let bnr = bufnr()
705 call assert_equal(1, bufloaded(bnr))
706 augroup test-WinClosed
707 autocmd WinClosed * throw 'foo'
708 augroup END
709 try
710 close
711 catch /.*/
712 endtry
713 call assert_equal(0, bufloaded(bnr))
714
715 autocmd! test-WinClosed
716 augroup! test-WinClosed
717endfunc
718
zeertzjq62de54b2022-09-22 18:08:37 +0100719" This used to trigger WinClosed twice for the same window, and the window's
720" buffer was NULL in the second autocommand.
721func Test_WinClosed_switch_tab()
722 edit Xa
723 split Xb
724 split Xc
725 tab split
726 new
727 augroup test-WinClosed
728 autocmd WinClosed * tabprev | bwipe!
729 augroup END
730 close
731 " Check that the tabline has been fully removed
732 call assert_equal([1, 1], win_screenpos(0))
733
734 autocmd! test-WinClosed
735 augroup! test-WinClosed
736 %bwipe!
737endfunc
738
Bram Moolenaare99e8442016-07-26 20:43:40 +0200739func s:AddAnAutocmd()
740 augroup vimBarTest
741 au BufReadCmd * echo 'hello'
742 augroup END
743 call assert_equal(3, len(split(execute('au vimBarTest'), "\n")))
744endfunc
745
746func Test_early_bar()
747 " test that a bar is recognized before the {event}
748 call s:AddAnAutocmd()
Bram Moolenaarb8e642f2021-11-20 10:38:25 +0000749 augroup vimBarTest | au! | let done = 77 | augroup END
Bram Moolenaare99e8442016-07-26 20:43:40 +0200750 call assert_equal(1, len(split(execute('au vimBarTest'), "\n")))
Bram Moolenaarb8e642f2021-11-20 10:38:25 +0000751 call assert_equal(77, done)
Bram Moolenaare99e8442016-07-26 20:43:40 +0200752
753 call s:AddAnAutocmd()
Bram Moolenaarb8e642f2021-11-20 10:38:25 +0000754 augroup vimBarTest| au!| let done = 88 | augroup END
Bram Moolenaare99e8442016-07-26 20:43:40 +0200755 call assert_equal(1, len(split(execute('au vimBarTest'), "\n")))
Bram Moolenaarb8e642f2021-11-20 10:38:25 +0000756 call assert_equal(88, done)
Bram Moolenaare99e8442016-07-26 20:43:40 +0200757
758 " test that a bar is recognized after the {event}
759 call s:AddAnAutocmd()
Bram Moolenaarb8e642f2021-11-20 10:38:25 +0000760 augroup vimBarTest| au!BufReadCmd| let done = 99 | augroup END
Bram Moolenaare99e8442016-07-26 20:43:40 +0200761 call assert_equal(1, len(split(execute('au vimBarTest'), "\n")))
Bram Moolenaarb8e642f2021-11-20 10:38:25 +0000762 call assert_equal(99, done)
Bram Moolenaare99e8442016-07-26 20:43:40 +0200763
764 " test that a bar is recognized after the {group}
765 call s:AddAnAutocmd()
766 au! vimBarTest|echo 'hello'
767 call assert_equal(1, len(split(execute('au vimBarTest'), "\n")))
768endfunc
Bram Moolenaarf2c4c392016-07-29 20:50:24 +0200769
Bram Moolenaar5c809082016-09-01 16:21:48 +0200770func RemoveGroup()
771 autocmd! StartOK
772 augroup! StartOK
773endfunc
774
Bram Moolenaarf2c4c392016-07-29 20:50:24 +0200775func Test_augroup_warning()
776 augroup TheWarning
777 au VimEnter * echo 'entering'
778 augroup END
Bram Moolenaar5dc4e2f2020-11-25 14:15:12 +0100779 call assert_match("TheWarning.*VimEnter", execute('au VimEnter'))
Bram Moolenaarf2c4c392016-07-29 20:50:24 +0200780 redir => res
781 augroup! TheWarning
782 redir END
Bram Moolenaar5dc4e2f2020-11-25 14:15:12 +0100783 call assert_match("W19:", res)
784 call assert_match("-Deleted-.*VimEnter", execute('au VimEnter'))
Bram Moolenaarf2c4c392016-07-29 20:50:24 +0200785
786 " check "Another" does not take the pace of the deleted entry
787 augroup Another
788 augroup END
Bram Moolenaar5dc4e2f2020-11-25 14:15:12 +0100789 call assert_match("-Deleted-.*VimEnter", execute('au VimEnter'))
Bram Moolenaaraeac9002016-09-06 22:15:08 +0200790 augroup! Another
Bram Moolenaar5c809082016-09-01 16:21:48 +0200791
792 " no warning for postpone aucmd delete
793 augroup StartOK
794 au VimEnter * call RemoveGroup()
795 augroup END
Bram Moolenaar5dc4e2f2020-11-25 14:15:12 +0100796 call assert_match("StartOK.*VimEnter", execute('au VimEnter'))
Bram Moolenaar5c809082016-09-01 16:21:48 +0200797 redir => res
798 doautocmd VimEnter
799 redir END
Bram Moolenaar5dc4e2f2020-11-25 14:15:12 +0100800 call assert_notmatch("W19:", res)
Bram Moolenaarde653f02016-09-03 16:59:06 +0200801 au! VimEnter
Bram Moolenaarad48e6c2020-04-21 22:19:45 +0200802
803 call assert_fails('augroup!', 'E471:')
Bram Moolenaarf2c4c392016-07-29 20:50:24 +0200804endfunc
Bram Moolenaarb62cc362016-09-03 16:43:53 +0200805
Bram Moolenaar8d84ff12017-10-26 16:42:16 +0200806func Test_BufReadCmdHelp()
807 " This used to cause access to free memory
808 au BufReadCmd * e +h
809 help
810
Bram Moolenaar8d84ff12017-10-26 16:42:16 +0200811 au! BufReadCmd
812endfunc
813
814func Test_BufReadCmdHelpJump()
815 " This used to cause access to free memory
816 au BufReadCmd * e +h{
Bram Moolenaarcf1ba352017-10-27 00:55:04 +0200817 " } to fix highlighting
818 call assert_fails('help', 'E434:')
Bram Moolenaar8d84ff12017-10-26 16:42:16 +0200819
Bram Moolenaar8d84ff12017-10-26 16:42:16 +0200820 au! BufReadCmd
821endfunc
822
zeertzjq93f72cc2022-08-26 15:34:52 +0100823" BufReadCmd is triggered for a "nofile" buffer. Check all values.
Bram Moolenaarb1d2c812022-08-26 11:55:01 +0100824func Test_BufReadCmdNofile()
zeertzjq93f72cc2022-08-26 15:34:52 +0100825 for val in ['nofile',
826 \ 'nowrite',
827 \ 'acwrite',
828 \ 'quickfix',
829 \ 'help',
830 \ 'terminal',
831 \ 'prompt',
832 \ 'popup',
833 \ ]
834 new somefile
835 exe 'set buftype=' .. val
836 au BufReadCmd somefile call setline(1, 'triggered')
837 edit
838 call assert_equal('triggered', getline(1))
Bram Moolenaarb1d2c812022-08-26 11:55:01 +0100839
zeertzjq93f72cc2022-08-26 15:34:52 +0100840 au! BufReadCmd
841 bwipe!
842 endfor
Bram Moolenaarb1d2c812022-08-26 11:55:01 +0100843endfunc
844
Bram Moolenaarb62cc362016-09-03 16:43:53 +0200845func Test_augroup_deleted()
Bram Moolenaarde653f02016-09-03 16:59:06 +0200846 " This caused a crash before E936 was introduced
Bram Moolenaarb62cc362016-09-03 16:43:53 +0200847 augroup x
Bram Moolenaarde653f02016-09-03 16:59:06 +0200848 call assert_fails('augroup! x', 'E936:')
849 au VimEnter * echo
850 augroup end
Bram Moolenaarb62cc362016-09-03 16:43:53 +0200851 augroup! x
Bram Moolenaar5dc4e2f2020-11-25 14:15:12 +0100852 call assert_match("-Deleted-.*VimEnter", execute('au VimEnter'))
Bram Moolenaarde653f02016-09-03 16:59:06 +0200853 au! VimEnter
Bram Moolenaarb62cc362016-09-03 16:43:53 +0200854endfunc
855
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200856" Tests for autocommands on :close command.
857" This used to be in test13.
858func Test_three_windows()
Bram Moolenaarb3435b02016-09-29 20:54:59 +0200859 " Clean up buffers, because in some cases this function fails.
860 call s:cleanup_buffers()
861
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200862 " Write three files and open them, each in a window.
863 " Then go to next window, with autocommand that deletes the previous one.
864 " Do this twice, writing the file.
865 e! Xtestje1
866 call setline(1, 'testje1')
867 w
868 sp Xtestje2
869 call setline(1, 'testje2')
870 w
871 sp Xtestje3
872 call setline(1, 'testje3')
873 w
874 wincmd w
875 au WinLeave Xtestje2 bwipe
876 wincmd w
877 call assert_equal('Xtestje1', expand('%'))
878
879 au WinLeave Xtestje1 bwipe Xtestje3
880 close
881 call assert_equal('Xtestje1', expand('%'))
882
883 " Test deleting the buffer on a Unload event. If this goes wrong there
884 " will be the ATTENTION prompt.
885 e Xtestje1
886 au!
887 au! BufUnload Xtestje1 bwipe
888 call assert_fails('e Xtestje3', 'E937:')
889 call assert_equal('Xtestje3', expand('%'))
890
891 e Xtestje2
892 sp Xtestje1
893 call assert_fails('e', 'E937:')
Bram Moolenaara997b452018-04-17 23:24:06 +0200894 call assert_equal('Xtestje1', expand('%'))
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200895
896 " Test changing buffers in a BufWipeout autocommand. If this goes wrong
897 " there are ml_line errors and/or a Crash.
898 au!
899 only
900 e Xanother
901 e Xtestje1
902 bwipe Xtestje2
903 bwipe Xtestje3
904 au BufWipeout Xtestje1 buf Xtestje1
905 bwipe
906 call assert_equal('Xanother', expand('%'))
907
908 only
909 help
910 wincmd w
911 1quit
912 call assert_equal('Xanother', expand('%'))
913
914 au!
Bram Moolenaar4520d442017-03-19 16:09:46 +0100915 enew
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200916 call delete('Xtestje1')
917 call delete('Xtestje2')
918 call delete('Xtestje3')
919endfunc
Bram Moolenaare13b9af2017-01-13 22:01:02 +0100920
921func Test_BufEnter()
922 au! BufEnter
923 au Bufenter * let val = val . '+'
924 let g:val = ''
925 split NewFile
926 call assert_equal('+', g:val)
927 bwipe!
928 call assert_equal('++', g:val)
929
930 " Also get BufEnter when editing a directory
Bram Moolenaar6f14da12022-09-07 21:30:44 +0100931 call mkdir('Xbufenterdir', 'D')
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +0100932 split Xbufenterdir
Bram Moolenaare13b9af2017-01-13 22:01:02 +0100933 call assert_equal('+++', g:val)
Bram Moolenaare94260f2017-03-21 15:50:12 +0100934
935 " On MS-Windows we can't edit the directory, make sure we wipe the right
936 " buffer.
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +0100937 bwipe! Xbufenterdir
Bram Moolenaare13b9af2017-01-13 22:01:02 +0100938 au! BufEnter
Bram Moolenaara9b5b852022-08-26 13:16:20 +0100939
940 " Editing a "nofile" buffer doesn't read the file but does trigger BufEnter
zeertzjq93f72cc2022-08-26 15:34:52 +0100941 " for historic reasons. Also test other 'buftype' values.
942 for val in ['nofile',
943 \ 'nowrite',
944 \ 'acwrite',
945 \ 'quickfix',
946 \ 'help',
947 \ 'terminal',
948 \ 'prompt',
949 \ 'popup',
950 \ ]
951 new somefile
952 exe 'set buftype=' .. val
953 au BufEnter somefile call setline(1, 'some text')
954 edit
955 call assert_equal('some text', getline(1))
956 bwipe!
957 au! BufEnter
958 endfor
Bram Moolenaar9fda8152022-11-19 13:14:10 +0000959
960 new
961 new
962 autocmd BufEnter * ++once close
963 call assert_fails('close', 'E1312:')
964
965 au! BufEnter
966 only
Bram Moolenaare13b9af2017-01-13 22:01:02 +0100967endfunc
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100968
969" Closing a window might cause an endless loop
970" E814 for older Vims
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200971func Test_autocmd_bufwipe_in_SessLoadPost()
Bram Moolenaar1d68d9b2017-10-13 22:33:32 +0200972 edit Xtest
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100973 tabnew
Bram Moolenaar1d68d9b2017-10-13 22:33:32 +0200974 file Xsomething
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100975 set noswapfile
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100976 mksession!
977
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200978 let content =<< trim [CODE]
Bram Moolenaar62cd26a2020-10-11 20:08:44 +0200979 call test_override('ui_delay', 10)
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200980 set nocp noswapfile
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100981 let v:swapchoice = "e"
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200982 augroup test_autocmd_sessionload
983 autocmd!
984 autocmd SessionLoadPost * exe bufnr("Xsomething") . "bw!"
985 augroup END
986
987 func WriteErrors()
Bram Moolenaarbe9fc5b2022-09-09 17:09:35 +0100988 call writefile([execute("messages")], "XerrorsBwipe")
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200989 endfunc
990 au VimLeave * call WriteErrors()
991 [CODE]
992
Bram Moolenaare1f3ab72022-09-04 21:29:08 +0100993 call writefile(content, 'Xvimrc', 'D')
Bram Moolenaar93344c22019-08-14 21:12:05 +0200994 call system(GetVimCommand('Xvimrc') .. ' --not-a-term --noplugins -S Session.vim -c cq')
Bram Moolenaarae04a602022-09-09 15:08:10 +0100995 sleep 100m
Bram Moolenaarbe9fc5b2022-09-09 17:09:35 +0100996 let errors = join(readfile('XerrorsBwipe'))
Bram Moolenaare2e40752020-09-04 21:18:46 +0200997 call assert_match('E814:', errors)
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100998
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100999 set swapfile
Bram Moolenaarbe9fc5b2022-09-09 17:09:35 +01001000 for file in ['Session.vim', 'XerrorsBwipe']
Bram Moolenaar8c752bd2017-03-19 17:09:56 +01001001 call delete(file)
1002 endfor
1003endfunc
1004
Bram Moolenaar797e63b2021-01-15 16:22:52 +01001005" Using :blast and :ball for many events caused a crash, because b_nwindows was
1006" not incremented correctly.
1007func Test_autocmd_blast_badd()
1008 let content =<< trim [CODE]
1009 au BufNew,BufAdd,BufWinEnter,BufEnter,BufLeave,BufWinLeave,BufUnload,VimEnter foo* blast
1010 edit foo1
1011 au BufNew,BufAdd,BufWinEnter,BufEnter,BufLeave,BufWinLeave,BufUnload,VimEnter foo* ball
1012 edit foo2
Bram Moolenaarbe9fc5b2022-09-09 17:09:35 +01001013 call writefile(['OK'], 'XerrorsBlast')
Bram Moolenaar797e63b2021-01-15 16:22:52 +01001014 qall
1015 [CODE]
1016
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01001017 call writefile(content, 'XblastBall', 'D')
Bram Moolenaar797e63b2021-01-15 16:22:52 +01001018 call system(GetVimCommand() .. ' --clean -S XblastBall')
Bram Moolenaarae04a602022-09-09 15:08:10 +01001019 sleep 100m
Bram Moolenaarbe9fc5b2022-09-09 17:09:35 +01001020 call assert_match('OK', readfile('XerrorsBlast')->join())
Bram Moolenaar797e63b2021-01-15 16:22:52 +01001021
Bram Moolenaarbe9fc5b2022-09-09 17:09:35 +01001022 call delete('XerrorsBlast')
Bram Moolenaar797e63b2021-01-15 16:22:52 +01001023endfunc
1024
Bram Moolenaar8c752bd2017-03-19 17:09:56 +01001025" SEGV occurs in older versions.
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001026func Test_autocmd_bufwipe_in_SessLoadPost2()
Bram Moolenaar8c752bd2017-03-19 17:09:56 +01001027 tabnew
1028 set noswapfile
Bram Moolenaar8c752bd2017-03-19 17:09:56 +01001029 mksession!
1030
Bram Moolenaarc79745a2019-05-20 22:12:34 +02001031 let content =<< trim [CODE]
1032 set nocp noswapfile
1033 function! DeleteInactiveBufs()
1034 tabfirst
1035 let tabblist = []
1036 for i in range(1, tabpagenr(''$''))
1037 call extend(tabblist, tabpagebuflist(i))
1038 endfor
1039 for b in range(1, bufnr(''$''))
1040 if bufexists(b) && buflisted(b) && (index(tabblist, b) == -1 || bufname(b) =~# ''^$'')
1041 exec ''bwipeout '' . b
1042 endif
1043 endfor
1044 echomsg "SessionLoadPost DONE"
1045 endfunction
1046 au SessionLoadPost * call DeleteInactiveBufs()
1047
1048 func WriteErrors()
Bram Moolenaarbe9fc5b2022-09-09 17:09:35 +01001049 call writefile([execute("messages")], "XerrorsPost")
Bram Moolenaarc79745a2019-05-20 22:12:34 +02001050 endfunc
1051 au VimLeave * call WriteErrors()
1052 [CODE]
1053
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01001054 call writefile(content, 'Xvimrc', 'D')
Bram Moolenaar93344c22019-08-14 21:12:05 +02001055 call system(GetVimCommand('Xvimrc') .. ' --not-a-term --noplugins -S Session.vim -c cq')
Bram Moolenaarae04a602022-09-09 15:08:10 +01001056 sleep 100m
Bram Moolenaarbe9fc5b2022-09-09 17:09:35 +01001057 let errors = join(readfile('XerrorsPost'))
Bram Moolenaare94260f2017-03-21 15:50:12 +01001058 " This probably only ever matches on unix.
1059 call assert_notmatch('Caught deadly signal SEGV', errors)
1060 call assert_match('SessionLoadPost DONE', errors)
Bram Moolenaar8c752bd2017-03-19 17:09:56 +01001061
Bram Moolenaar8c752bd2017-03-19 17:09:56 +01001062 set swapfile
Bram Moolenaarbe9fc5b2022-09-09 17:09:35 +01001063 for file in ['Session.vim', 'XerrorsPost']
Bram Moolenaar8c752bd2017-03-19 17:09:56 +01001064 call delete(file)
1065 endfor
1066endfunc
Bram Moolenaarfaf29d72017-07-09 11:07:16 +02001067
1068func Test_empty_doau()
1069 doau \|
1070endfunc
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001071
1072func s:AutoCommandOptionSet(match)
Bram Moolenaard7c96872019-06-15 17:12:48 +02001073 let template = "Option: <%s>, OldVal: <%s>, OldValLocal: <%s>, OldValGlobal: <%s>, NewVal: <%s>, Scope: <%s>, Command: <%s>\n"
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001074 let item = remove(g:options, 0)
Bram Moolenaard7c96872019-06-15 17:12:48 +02001075 let expected = printf(template, item[0], item[1], item[2], item[3], item[4], item[5], item[6])
1076 let actual = printf(template, a:match, v:option_old, v:option_oldlocal, v:option_oldglobal, v:option_new, v:option_type, v:option_command)
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001077 let g:opt = [expected, actual]
1078 "call assert_equal(expected, actual)
1079endfunc
1080
1081func Test_OptionSet()
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +02001082 CheckOption autochdir
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001083
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +02001084 badd test_autocmd.vim
1085
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001086 call test_override('starting', 1)
1087 set nocp
1088 au OptionSet * :call s:AutoCommandOptionSet(expand("<amatch>"))
1089
1090 " 1: Setting number option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001091 let g:options = [['number', 0, 0, 0, 1, 'global', 'set']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001092 set nu
1093 call assert_equal([], g:options)
1094 call assert_equal(g:opt[0], g:opt[1])
1095
1096 " 2: Setting local number option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001097 let g:options = [['number', 1, 1, '', 0, 'local', 'setlocal']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001098 setlocal nonu
1099 call assert_equal([], g:options)
1100 call assert_equal(g:opt[0], g:opt[1])
1101
1102 " 3: Setting global number option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001103 let g:options = [['number', 1, '', 1, 0, 'global', 'setglobal']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001104 setglobal nonu
1105 call assert_equal([], g:options)
1106 call assert_equal(g:opt[0], g:opt[1])
1107
1108 " 4: Setting local autoindent option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001109 let g:options = [['autoindent', 0, 0, '', 1, 'local', 'setlocal']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001110 setlocal ai
1111 call assert_equal([], g:options)
1112 call assert_equal(g:opt[0], g:opt[1])
1113
1114 " 5: Setting global autoindent option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001115 let g:options = [['autoindent', 0, '', 0, 1, 'global', 'setglobal']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001116 setglobal ai
1117 call assert_equal([], g:options)
1118 call assert_equal(g:opt[0], g:opt[1])
1119
1120 " 6: Setting global autoindent option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001121 let g:options = [['autoindent', 1, 1, 1, 0, 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001122 set ai!
1123 call assert_equal([], g:options)
1124 call assert_equal(g:opt[0], g:opt[1])
1125
1126 " 6a: Setting global autoindent option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001127 let g:options = [['autoindent', 1, 1, 0, 0, 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001128 noa setlocal ai
1129 noa setglobal noai
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001130 set ai!
1131 call assert_equal([], g:options)
1132 call assert_equal(g:opt[0], g:opt[1])
1133
1134 " Should not print anything, use :noa
1135 " 7: don't trigger OptionSet"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001136 let g:options = [['invalid', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001137 noa set nonu
Bram Moolenaard7c96872019-06-15 17:12:48 +02001138 call assert_equal([['invalid', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid']], g:options)
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001139 call assert_equal(g:opt[0], g:opt[1])
1140
1141 " 8: Setting several global list and number option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001142 let g:options = [['list', 0, 0, 0, 1, 'global', 'set'], ['number', 0, 0, 0, 1, 'global', 'set']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001143 set list nu
1144 call assert_equal([], g:options)
1145 call assert_equal(g:opt[0], g:opt[1])
1146
1147 " 9: don't trigger OptionSet"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001148 let g:options = [['invalid', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid'], ['invalid', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001149 noa set nolist nonu
Bram Moolenaard7c96872019-06-15 17:12:48 +02001150 call assert_equal([['invalid', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid'], ['invalid', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid']], g:options)
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001151 call assert_equal(g:opt[0], g:opt[1])
1152
1153 " 10: Setting global acd"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001154 let g:options = [['autochdir', 0, 0, '', 1, 'local', 'setlocal']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001155 setlocal acd
1156 call assert_equal([], g:options)
1157 call assert_equal(g:opt[0], g:opt[1])
1158
1159 " 11: Setting global autoread (also sets local value)"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001160 let g:options = [['autoread', 0, 0, 0, 1, 'global', 'set']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001161 set ar
1162 call assert_equal([], g:options)
1163 call assert_equal(g:opt[0], g:opt[1])
1164
1165 " 12: Setting local autoread"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001166 let g:options = [['autoread', 1, 1, '', 1, 'local', 'setlocal']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001167 setlocal ar
1168 call assert_equal([], g:options)
1169 call assert_equal(g:opt[0], g:opt[1])
1170
1171 " 13: Setting global autoread"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001172 let g:options = [['autoread', 1, '', 1, 0, 'global', 'setglobal']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001173 setglobal invar
1174 call assert_equal([], g:options)
1175 call assert_equal(g:opt[0], g:opt[1])
1176
1177 " 14: Setting option backspace through :let"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001178 let g:options = [['backspace', '', '', '', 'eol,indent,start', 'global', 'set']]
1179 let &bs = "eol,indent,start"
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001180 call assert_equal([], g:options)
1181 call assert_equal(g:opt[0], g:opt[1])
1182
1183 " 15: Setting option backspace through setbufvar()"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001184 let g:options = [['backup', 0, 0, '', 1, 'local', 'setlocal']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001185 " try twice, first time, shouldn't trigger because option name is invalid,
1186 " second time, it should trigger
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +02001187 let bnum = bufnr('%')
Bram Moolenaare2e40752020-09-04 21:18:46 +02001188 call assert_fails("call setbufvar(bnum, '&l:bk', 1)", 'E355:')
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001189 " should trigger, use correct option name
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +02001190 call setbufvar(bnum, '&backup', 1)
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001191 call assert_equal([], g:options)
1192 call assert_equal(g:opt[0], g:opt[1])
1193
1194 " 16: Setting number option using setwinvar"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001195 let g:options = [['number', 0, 0, '', 1, 'local', 'setlocal']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001196 call setwinvar(0, '&number', 1)
1197 call assert_equal([], g:options)
1198 call assert_equal(g:opt[0], g:opt[1])
1199
1200 " 17: Setting key option, shouldn't trigger"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001201 let g:options = [['key', 'invalid', 'invalid1', 'invalid2', 'invalid3', 'invalid4', 'invalid5']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001202 setlocal key=blah
1203 setlocal key=
Bram Moolenaard7c96872019-06-15 17:12:48 +02001204 call assert_equal([['key', 'invalid', 'invalid1', 'invalid2', 'invalid3', 'invalid4', 'invalid5']], g:options)
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001205 call assert_equal(g:opt[0], g:opt[1])
1206
Bram Moolenaard7c96872019-06-15 17:12:48 +02001207
1208 " 18a: Setting string global option"
1209 let oldval = &backupext
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001210 let g:options = [['backupext', oldval, oldval, oldval, 'foo', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001211 set backupext=foo
1212 call assert_equal([], g:options)
1213 call assert_equal(g:opt[0], g:opt[1])
1214
1215 " 18b: Resetting string global option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001216 let g:options = [['backupext', 'foo', 'foo', 'foo', oldval, 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001217 set backupext&
1218 call assert_equal([], g:options)
1219 call assert_equal(g:opt[0], g:opt[1])
1220
1221 " 18c: Setting global string global option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001222 let g:options = [['backupext', oldval, '', oldval, 'bar', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001223 setglobal backupext=bar
1224 call assert_equal([], g:options)
1225 call assert_equal(g:opt[0], g:opt[1])
1226
1227 " 18d: Setting local string global option"
1228 " As this is a global option this sets the global value even though
1229 " :setlocal is used!
1230 noa set backupext& " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001231 let g:options = [['backupext', oldval, oldval, '', 'baz', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001232 setlocal backupext=baz
1233 call assert_equal([], g:options)
1234 call assert_equal(g:opt[0], g:opt[1])
1235
1236 " 18e: Setting again string global option"
1237 noa setglobal backupext=ext_global " Reset global and local value (without triggering autocmd)
1238 noa setlocal backupext=ext_local " Sets the global(!) value!
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001239 let g:options = [['backupext', 'ext_local', 'ext_local', 'ext_local', 'fuu', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001240 set backupext=fuu
1241 call assert_equal([], g:options)
1242 call assert_equal(g:opt[0], g:opt[1])
1243
1244
zeertzjqb811de52021-10-21 10:50:44 +01001245 " 19a: Setting string global-local (to buffer) option"
Bram Moolenaar8efa0262017-08-20 15:47:20 +02001246 let oldval = &tags
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001247 let g:options = [['tags', oldval, oldval, oldval, 'tagpath', 'global', 'set']]
Bram Moolenaar8efa0262017-08-20 15:47:20 +02001248 set tags=tagpath
1249 call assert_equal([], g:options)
1250 call assert_equal(g:opt[0], g:opt[1])
1251
zeertzjqb811de52021-10-21 10:50:44 +01001252 " 19b: Resetting string global-local (to buffer) option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001253 let g:options = [['tags', 'tagpath', 'tagpath', 'tagpath', oldval, 'global', 'set']]
Bram Moolenaar8efa0262017-08-20 15:47:20 +02001254 set tags&
1255 call assert_equal([], g:options)
1256 call assert_equal(g:opt[0], g:opt[1])
1257
zeertzjqb811de52021-10-21 10:50:44 +01001258 " 19c: Setting global string global-local (to buffer) option "
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001259 let g:options = [['tags', oldval, '', oldval, 'tagpath1', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001260 setglobal tags=tagpath1
1261 call assert_equal([], g:options)
1262 call assert_equal(g:opt[0], g:opt[1])
1263
zeertzjqb811de52021-10-21 10:50:44 +01001264 " 19d: Setting local string global-local (to buffer) option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001265 let g:options = [['tags', 'tagpath1', 'tagpath1', '', 'tagpath2', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001266 setlocal tags=tagpath2
1267 call assert_equal([], g:options)
1268 call assert_equal(g:opt[0], g:opt[1])
1269
zeertzjqb811de52021-10-21 10:50:44 +01001270 " 19e: Setting again string global-local (to buffer) option"
1271 " Note: v:option_old is the old global value for global-local string options
Bram Moolenaard7c96872019-06-15 17:12:48 +02001272 " but the old local value for all other kinds of options.
1273 noa setglobal tags=tag_global " Reset global and local value (without triggering autocmd)
1274 noa setlocal tags=tag_local
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001275 let g:options = [['tags', 'tag_global', 'tag_local', 'tag_global', 'tagpath', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001276 set tags=tagpath
1277 call assert_equal([], g:options)
1278 call assert_equal(g:opt[0], g:opt[1])
1279
zeertzjqb811de52021-10-21 10:50:44 +01001280 " 19f: Setting string global-local (to buffer) option to an empty string"
1281 " Note: v:option_old is the old global value for global-local string options
Bram Moolenaard7c96872019-06-15 17:12:48 +02001282 " but the old local value for all other kinds of options.
1283 noa set tags=tag_global " Reset global and local value (without triggering autocmd)
1284 noa setlocal tags= " empty string
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001285 let g:options = [['tags', 'tag_global', '', 'tag_global', 'tagpath', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001286 set tags=tagpath
1287 call assert_equal([], g:options)
1288 call assert_equal(g:opt[0], g:opt[1])
1289
1290
1291 " 20a: Setting string local (to buffer) option"
1292 let oldval = &spelllang
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001293 let g:options = [['spelllang', oldval, oldval, oldval, 'elvish,klingon', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001294 set spelllang=elvish,klingon
1295 call assert_equal([], g:options)
1296 call assert_equal(g:opt[0], g:opt[1])
1297
1298 " 20b: Resetting string local (to buffer) option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001299 let g:options = [['spelllang', 'elvish,klingon', 'elvish,klingon', 'elvish,klingon', oldval, 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001300 set spelllang&
1301 call assert_equal([], g:options)
1302 call assert_equal(g:opt[0], g:opt[1])
1303
1304 " 20c: Setting global string local (to buffer) option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001305 let g:options = [['spelllang', oldval, '', oldval, 'elvish', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001306 setglobal spelllang=elvish
1307 call assert_equal([], g:options)
1308 call assert_equal(g:opt[0], g:opt[1])
1309
1310 " 20d: Setting local string local (to buffer) option"
1311 noa set spelllang& " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001312 let g:options = [['spelllang', oldval, oldval, '', 'klingon', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001313 setlocal spelllang=klingon
1314 call assert_equal([], g:options)
1315 call assert_equal(g:opt[0], g:opt[1])
1316
1317 " 20e: Setting again string local (to buffer) option"
zeertzjqb811de52021-10-21 10:50:44 +01001318 " Note: v:option_old is the old global value for global-local string options
Bram Moolenaard7c96872019-06-15 17:12:48 +02001319 " but the old local value for all other kinds of options.
1320 noa setglobal spelllang=spellglobal " Reset global and local value (without triggering autocmd)
1321 noa setlocal spelllang=spelllocal
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001322 let g:options = [['spelllang', 'spelllocal', 'spelllocal', 'spellglobal', 'foo', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001323 set spelllang=foo
1324 call assert_equal([], g:options)
1325 call assert_equal(g:opt[0], g:opt[1])
1326
1327
zeertzjqb811de52021-10-21 10:50:44 +01001328 " 21a: Setting string global-local (to window) option"
Bram Moolenaard7c96872019-06-15 17:12:48 +02001329 let oldval = &statusline
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001330 let g:options = [['statusline', oldval, oldval, oldval, 'foo', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001331 set statusline=foo
1332 call assert_equal([], g:options)
1333 call assert_equal(g:opt[0], g:opt[1])
1334
zeertzjqb811de52021-10-21 10:50:44 +01001335 " 21b: Resetting string global-local (to window) option"
1336 " Note: v:option_old is the old global value for global-local string options
Bram Moolenaard7c96872019-06-15 17:12:48 +02001337 " but the old local value for all other kinds of options.
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001338 let g:options = [['statusline', 'foo', 'foo', 'foo', oldval, 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001339 set statusline&
1340 call assert_equal([], g:options)
1341 call assert_equal(g:opt[0], g:opt[1])
1342
zeertzjqb811de52021-10-21 10:50:44 +01001343 " 21c: Setting global string global-local (to window) option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001344 let g:options = [['statusline', oldval, '', oldval, 'bar', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001345 setglobal statusline=bar
1346 call assert_equal([], g:options)
1347 call assert_equal(g:opt[0], g:opt[1])
1348
zeertzjqb811de52021-10-21 10:50:44 +01001349 " 21d: Setting local string global-local (to window) option"
Bram Moolenaard7c96872019-06-15 17:12:48 +02001350 noa set statusline& " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001351 let g:options = [['statusline', oldval, oldval, '', 'baz', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001352 setlocal statusline=baz
1353 call assert_equal([], g:options)
1354 call assert_equal(g:opt[0], g:opt[1])
1355
zeertzjqb811de52021-10-21 10:50:44 +01001356 " 21e: Setting again string global-local (to window) option"
1357 " Note: v:option_old is the old global value for global-local string options
Bram Moolenaard7c96872019-06-15 17:12:48 +02001358 " but the old local value for all other kinds of options.
1359 noa setglobal statusline=bar " Reset global and local value (without triggering autocmd)
1360 noa setlocal statusline=baz
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001361 let g:options = [['statusline', 'bar', 'baz', 'bar', 'foo', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001362 set statusline=foo
1363 call assert_equal([], g:options)
1364 call assert_equal(g:opt[0], g:opt[1])
1365
1366
1367 " 22a: Setting string local (to window) option"
1368 let oldval = &foldignore
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001369 let g:options = [['foldignore', oldval, oldval, oldval, 'fo', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001370 set foldignore=fo
1371 call assert_equal([], g:options)
1372 call assert_equal(g:opt[0], g:opt[1])
1373
1374 " 22b: Resetting string local (to window) option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001375 let g:options = [['foldignore', 'fo', 'fo', 'fo', oldval, 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001376 set foldignore&
1377 call assert_equal([], g:options)
1378 call assert_equal(g:opt[0], g:opt[1])
1379
1380 " 22c: Setting global string local (to window) option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001381 let g:options = [['foldignore', oldval, '', oldval, 'bar', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001382 setglobal foldignore=bar
1383 call assert_equal([], g:options)
1384 call assert_equal(g:opt[0], g:opt[1])
1385
1386 " 22d: Setting local string local (to window) option"
1387 noa set foldignore& " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001388 let g:options = [['foldignore', oldval, oldval, '', 'baz', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001389 setlocal foldignore=baz
1390 call assert_equal([], g:options)
1391 call assert_equal(g:opt[0], g:opt[1])
1392
1393 " 22e: Setting again string local (to window) option"
1394 noa setglobal foldignore=glob " Reset global and local value (without triggering autocmd)
1395 noa setlocal foldignore=loc
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001396 let g:options = [['foldignore', 'loc', 'loc', 'glob', 'fo', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001397 set foldignore=fo
1398 call assert_equal([], g:options)
1399 call assert_equal(g:opt[0], g:opt[1])
1400
1401
zeertzjqb811de52021-10-21 10:50:44 +01001402 " 23a: Setting global number global option"
Bram Moolenaard7c96872019-06-15 17:12:48 +02001403 noa setglobal cmdheight=8 " Reset global and local value (without triggering autocmd)
1404 noa setlocal cmdheight=1 " Sets the global(!) value!
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001405 let g:options = [['cmdheight', '1', '', '1', '2', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001406 setglobal cmdheight=2
1407 call assert_equal([], g:options)
1408 call assert_equal(g:opt[0], g:opt[1])
1409
1410 " 23b: Setting local number global option"
1411 noa setglobal cmdheight=8 " Reset global and local value (without triggering autocmd)
1412 noa setlocal cmdheight=1 " Sets the global(!) value!
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001413 let g:options = [['cmdheight', '1', '1', '', '2', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001414 setlocal cmdheight=2
1415 call assert_equal([], g:options)
1416 call assert_equal(g:opt[0], g:opt[1])
1417
1418 " 23c: Setting again number global option"
1419 noa setglobal cmdheight=8 " Reset global and local value (without triggering autocmd)
1420 noa setlocal cmdheight=1 " Sets the global(!) value!
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001421 let g:options = [['cmdheight', '1', '1', '1', '2', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001422 set cmdheight=2
1423 call assert_equal([], g:options)
1424 call assert_equal(g:opt[0], g:opt[1])
1425
1426 " 23d: Setting again number global option"
1427 noa set cmdheight=8 " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001428 let g:options = [['cmdheight', '8', '8', '8', '2', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001429 set cmdheight=2
1430 call assert_equal([], g:options)
1431 call assert_equal(g:opt[0], g:opt[1])
1432
1433
1434 " 24a: Setting global number global-local (to buffer) option"
1435 noa setglobal undolevels=8 " Reset global and local value (without triggering autocmd)
1436 noa setlocal undolevels=1
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001437 let g:options = [['undolevels', '8', '', '8', '2', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001438 setglobal undolevels=2
1439 call assert_equal([], g:options)
1440 call assert_equal(g:opt[0], g:opt[1])
1441
1442 " 24b: Setting local number global-local (to buffer) option"
1443 noa setglobal undolevels=8 " Reset global and local value (without triggering autocmd)
1444 noa setlocal undolevels=1
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001445 let g:options = [['undolevels', '1', '1', '', '2', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001446 setlocal undolevels=2
1447 call assert_equal([], g:options)
1448 call assert_equal(g:opt[0], g:opt[1])
1449
1450 " 24c: Setting again number global-local (to buffer) option"
1451 noa setglobal undolevels=8 " Reset global and local value (without triggering autocmd)
1452 noa setlocal undolevels=1
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001453 let g:options = [['undolevels', '1', '1', '8', '2', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001454 set undolevels=2
1455 call assert_equal([], g:options)
1456 call assert_equal(g:opt[0], g:opt[1])
1457
1458 " 24d: Setting again global number global-local (to buffer) option"
1459 noa set undolevels=8 " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001460 let g:options = [['undolevels', '8', '8', '8', '2', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001461 set undolevels=2
1462 call assert_equal([], g:options)
1463 call assert_equal(g:opt[0], g:opt[1])
1464
1465
1466 " 25a: Setting global number local (to buffer) option"
1467 noa setglobal wrapmargin=8 " Reset global and local value (without triggering autocmd)
1468 noa setlocal wrapmargin=1
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001469 let g:options = [['wrapmargin', '8', '', '8', '2', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001470 setglobal wrapmargin=2
1471 call assert_equal([], g:options)
1472 call assert_equal(g:opt[0], g:opt[1])
1473
1474 " 25b: Setting local number local (to buffer) option"
1475 noa setglobal wrapmargin=8 " Reset global and local value (without triggering autocmd)
1476 noa setlocal wrapmargin=1
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001477 let g:options = [['wrapmargin', '1', '1', '', '2', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001478 setlocal wrapmargin=2
1479 call assert_equal([], g:options)
1480 call assert_equal(g:opt[0], g:opt[1])
1481
1482 " 25c: Setting again number local (to buffer) option"
1483 noa setglobal wrapmargin=8 " Reset global and local value (without triggering autocmd)
1484 noa setlocal wrapmargin=1
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001485 let g:options = [['wrapmargin', '1', '1', '8', '2', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001486 set wrapmargin=2
1487 call assert_equal([], g:options)
1488 call assert_equal(g:opt[0], g:opt[1])
1489
1490 " 25d: Setting again global number local (to buffer) option"
1491 noa set wrapmargin=8 " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001492 let g:options = [['wrapmargin', '8', '8', '8', '2', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001493 set wrapmargin=2
1494 call assert_equal([], g:options)
1495 call assert_equal(g:opt[0], g:opt[1])
1496
1497
1498 " 26: Setting number global-local (to window) option.
1499 " Such option does currently not exist.
1500
1501
1502 " 27a: Setting global number local (to window) option"
1503 noa setglobal foldcolumn=8 " Reset global and local value (without triggering autocmd)
1504 noa setlocal foldcolumn=1
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001505 let g:options = [['foldcolumn', '8', '', '8', '2', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001506 setglobal foldcolumn=2
1507 call assert_equal([], g:options)
1508 call assert_equal(g:opt[0], g:opt[1])
1509
1510 " 27b: Setting local number local (to window) option"
1511 noa setglobal foldcolumn=8 " Reset global and local value (without triggering autocmd)
1512 noa setlocal foldcolumn=1
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001513 let g:options = [['foldcolumn', '1', '1', '', '2', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001514 setlocal foldcolumn=2
1515 call assert_equal([], g:options)
1516 call assert_equal(g:opt[0], g:opt[1])
1517
1518 " 27c: Setting again number local (to window) option"
1519 noa setglobal foldcolumn=8 " Reset global and local value (without triggering autocmd)
1520 noa setlocal foldcolumn=1
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001521 let g:options = [['foldcolumn', '1', '1', '8', '2', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001522 set foldcolumn=2
1523 call assert_equal([], g:options)
1524 call assert_equal(g:opt[0], g:opt[1])
1525
zeertzjqb811de52021-10-21 10:50:44 +01001526 " 27d: Setting again global number local (to window) option"
Bram Moolenaard7c96872019-06-15 17:12:48 +02001527 noa set foldcolumn=8 " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001528 let g:options = [['foldcolumn', '8', '8', '8', '2', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001529 set foldcolumn=2
1530 call assert_equal([], g:options)
1531 call assert_equal(g:opt[0], g:opt[1])
1532
1533
1534 " 28a: Setting global boolean global option"
1535 noa setglobal nowrapscan " Reset global and local value (without triggering autocmd)
1536 noa setlocal wrapscan " Sets the global(!) value!
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001537 let g:options = [['wrapscan', '1', '', '1', '0', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001538 setglobal nowrapscan
1539 call assert_equal([], g:options)
1540 call assert_equal(g:opt[0], g:opt[1])
1541
1542 " 28b: Setting local boolean global option"
1543 noa setglobal nowrapscan " Reset global and local value (without triggering autocmd)
1544 noa setlocal wrapscan " Sets the global(!) value!
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001545 let g:options = [['wrapscan', '1', '1', '', '0', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001546 setlocal nowrapscan
1547 call assert_equal([], g:options)
1548 call assert_equal(g:opt[0], g:opt[1])
1549
1550 " 28c: Setting again boolean global option"
1551 noa setglobal nowrapscan " Reset global and local value (without triggering autocmd)
1552 noa setlocal wrapscan " Sets the global(!) value!
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001553 let g:options = [['wrapscan', '1', '1', '1', '0', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001554 set nowrapscan
1555 call assert_equal([], g:options)
1556 call assert_equal(g:opt[0], g:opt[1])
1557
1558 " 28d: Setting again global boolean global option"
1559 noa set nowrapscan " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001560 let g:options = [['wrapscan', '0', '0', '0', '1', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001561 set wrapscan
1562 call assert_equal([], g:options)
1563 call assert_equal(g:opt[0], g:opt[1])
1564
1565
1566 " 29a: Setting global boolean global-local (to buffer) option"
1567 noa setglobal noautoread " Reset global and local value (without triggering autocmd)
1568 noa setlocal autoread
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001569 let g:options = [['autoread', '0', '', '0', '1', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001570 setglobal autoread
1571 call assert_equal([], g:options)
1572 call assert_equal(g:opt[0], g:opt[1])
1573
1574 " 29b: Setting local boolean global-local (to buffer) option"
1575 noa setglobal noautoread " Reset global and local value (without triggering autocmd)
1576 noa setlocal autoread
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001577 let g:options = [['autoread', '1', '1', '', '0', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001578 setlocal noautoread
1579 call assert_equal([], g:options)
1580 call assert_equal(g:opt[0], g:opt[1])
1581
1582 " 29c: Setting again boolean global-local (to buffer) option"
1583 noa setglobal noautoread " Reset global and local value (without triggering autocmd)
1584 noa setlocal autoread
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001585 let g:options = [['autoread', '1', '1', '0', '1', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001586 set autoread
1587 call assert_equal([], g:options)
1588 call assert_equal(g:opt[0], g:opt[1])
1589
1590 " 29d: Setting again global boolean global-local (to buffer) option"
1591 noa set noautoread " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001592 let g:options = [['autoread', '0', '0', '0', '1', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001593 set autoread
1594 call assert_equal([], g:options)
1595 call assert_equal(g:opt[0], g:opt[1])
1596
1597
1598 " 30a: Setting global boolean local (to buffer) option"
1599 noa setglobal nocindent " Reset global and local value (without triggering autocmd)
1600 noa setlocal cindent
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001601 let g:options = [['cindent', '0', '', '0', '1', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001602 setglobal cindent
1603 call assert_equal([], g:options)
1604 call assert_equal(g:opt[0], g:opt[1])
1605
1606 " 30b: Setting local boolean local (to buffer) option"
1607 noa setglobal nocindent " Reset global and local value (without triggering autocmd)
1608 noa setlocal cindent
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001609 let g:options = [['cindent', '1', '1', '', '0', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001610 setlocal nocindent
1611 call assert_equal([], g:options)
1612 call assert_equal(g:opt[0], g:opt[1])
1613
1614 " 30c: Setting again boolean local (to buffer) option"
1615 noa setglobal nocindent " Reset global and local value (without triggering autocmd)
1616 noa setlocal cindent
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001617 let g:options = [['cindent', '1', '1', '0', '1', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001618 set cindent
1619 call assert_equal([], g:options)
1620 call assert_equal(g:opt[0], g:opt[1])
1621
1622 " 30d: Setting again global boolean local (to buffer) option"
1623 noa set nocindent " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001624 let g:options = [['cindent', '0', '0', '0', '1', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001625 set cindent
1626 call assert_equal([], g:options)
1627 call assert_equal(g:opt[0], g:opt[1])
1628
1629
1630 " 31: Setting boolean global-local (to window) option
1631 " Currently no such option exists.
1632
1633
1634 " 32a: Setting global boolean local (to window) option"
1635 noa setglobal nocursorcolumn " Reset global and local value (without triggering autocmd)
1636 noa setlocal cursorcolumn
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001637 let g:options = [['cursorcolumn', '0', '', '0', '1', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001638 setglobal cursorcolumn
1639 call assert_equal([], g:options)
1640 call assert_equal(g:opt[0], g:opt[1])
1641
1642 " 32b: Setting local boolean local (to window) option"
1643 noa setglobal nocursorcolumn " Reset global and local value (without triggering autocmd)
1644 noa setlocal cursorcolumn
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001645 let g:options = [['cursorcolumn', '1', '1', '', '0', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001646 setlocal nocursorcolumn
1647 call assert_equal([], g:options)
1648 call assert_equal(g:opt[0], g:opt[1])
1649
1650 " 32c: Setting again boolean local (to window) option"
1651 noa setglobal nocursorcolumn " Reset global and local value (without triggering autocmd)
1652 noa setlocal cursorcolumn
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001653 let g:options = [['cursorcolumn', '1', '1', '0', '1', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001654 set cursorcolumn
1655 call assert_equal([], g:options)
1656 call assert_equal(g:opt[0], g:opt[1])
1657
1658 " 32d: Setting again global boolean local (to window) option"
1659 noa set nocursorcolumn " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001660 let g:options = [['cursorcolumn', '0', '0', '0', '1', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001661 set cursorcolumn
1662 call assert_equal([], g:options)
1663 call assert_equal(g:opt[0], g:opt[1])
1664
1665
Bram Moolenaar1bc353b2019-09-01 14:45:28 +02001666 " 33: Test autocommands when an option value is converted internally.
Bram Moolenaard7c96872019-06-15 17:12:48 +02001667 noa set backspace=1 " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001668 let g:options = [['backspace', 'indent,eol', 'indent,eol', 'indent,eol', '2', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001669 set backspace=2
1670 call assert_equal([], g:options)
1671 call assert_equal(g:opt[0], g:opt[1])
1672
1673
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001674 " Cleanup
1675 au! OptionSet
Bram Moolenaar0331faf2019-06-15 18:40:37 +02001676 " set tags&
Bram Moolenaard7c96872019-06-15 17:12:48 +02001677 for opt in ['nu', 'ai', 'acd', 'ar', 'bs', 'backup', 'cul', 'cp', 'backupext', 'tags', 'spelllang', 'statusline', 'foldignore', 'cmdheight', 'undolevels', 'wrapmargin', 'foldcolumn', 'wrapscan', 'autoread', 'cindent', 'cursorcolumn']
Bram Moolenaar91d2e782018-08-07 19:05:01 +02001678 exe printf(":set %s&vim", opt)
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001679 endfor
1680 call test_override('starting', 0)
1681 delfunc! AutoCommandOptionSet
1682endfunc
1683
1684func Test_OptionSet_diffmode()
1685 call test_override('starting', 1)
Bram Moolenaar26d98212019-01-27 22:32:55 +01001686 " 18: Changing an option when entering diff mode
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001687 new
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001688 au OptionSet diff :let &l:cul = v:option_new
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001689
1690 call setline(1, ['buffer 1', 'line2', 'line3', 'line4'])
1691 call assert_equal(0, &l:cul)
1692 diffthis
1693 call assert_equal(1, &l:cul)
1694
1695 vnew
1696 call setline(1, ['buffer 2', 'line 2', 'line 3', 'line4'])
1697 call assert_equal(0, &l:cul)
1698 diffthis
1699 call assert_equal(1, &l:cul)
1700
1701 diffoff
1702 call assert_equal(0, &l:cul)
1703 call assert_equal(1, getwinvar(2, '&l:cul'))
1704 bw!
1705
1706 call assert_equal(1, &l:cul)
1707 diffoff!
1708 call assert_equal(0, &l:cul)
1709 call assert_equal(0, getwinvar(1, '&l:cul'))
1710 bw!
1711
1712 " Cleanup
1713 au! OptionSet
1714 call test_override('starting', 0)
1715endfunc
1716
1717func Test_OptionSet_diffmode_close()
1718 call test_override('starting', 1)
1719 " 19: Try to close the current window when entering diff mode
1720 " should not segfault
1721 new
1722 au OptionSet diff close
1723
1724 call setline(1, ['buffer 1', 'line2', 'line3', 'line4'])
Bram Moolenaare2e40752020-09-04 21:18:46 +02001725 call assert_fails(':diffthis', 'E788:')
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001726 call assert_equal(1, &diff)
1727 vnew
1728 call setline(1, ['buffer 2', 'line 2', 'line 3', 'line4'])
Bram Moolenaare2e40752020-09-04 21:18:46 +02001729 call assert_fails(':diffthis', 'E788:')
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001730 call assert_equal(1, &diff)
Bram Moolenaara9aa86f2019-11-10 21:25:45 +01001731 set diffopt-=closeoff
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001732 bw!
Bram Moolenaare2e40752020-09-04 21:18:46 +02001733 call assert_fails(':diffoff!', 'E788:')
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001734 bw!
1735
1736 " Cleanup
1737 au! OptionSet
1738 call test_override('starting', 0)
1739 "delfunc! AutoCommandOptionSet
1740endfunc
Bram Moolenaar4a137b42017-08-04 22:37:11 +02001741
1742" Test for Bufleave autocommand that deletes the buffer we are about to edit.
1743func Test_BufleaveWithDelete()
Bram Moolenaare7cda972022-08-29 11:02:59 +01001744 new | edit XbufLeave1
Bram Moolenaar4a137b42017-08-04 22:37:11 +02001745
1746 augroup test_bufleavewithdelete
1747 autocmd!
Bram Moolenaare7cda972022-08-29 11:02:59 +01001748 autocmd BufLeave XbufLeave1 bwipe XbufLeave2
Bram Moolenaar4a137b42017-08-04 22:37:11 +02001749 augroup END
1750
Bram Moolenaare7cda972022-08-29 11:02:59 +01001751 call assert_fails('edit XbufLeave2', 'E143:')
1752 call assert_equal('XbufLeave1', bufname('%'))
Bram Moolenaar4a137b42017-08-04 22:37:11 +02001753
Bram Moolenaare7cda972022-08-29 11:02:59 +01001754 autocmd! test_bufleavewithdelete BufLeave XbufLeave1
Bram Moolenaar4a137b42017-08-04 22:37:11 +02001755 augroup! test_bufleavewithdelete
1756
1757 new
Bram Moolenaare7cda972022-08-29 11:02:59 +01001758 bwipe! XbufLeave1
Bram Moolenaar4a137b42017-08-04 22:37:11 +02001759endfunc
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +02001760
1761" Test for autocommand that changes the buffer list, when doing ":ball".
1762func Test_Acmd_BufAll()
1763 enew!
1764 %bwipe!
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01001765 call writefile(['Test file Xxx1'], 'Xxx1', 'D')
1766 call writefile(['Test file Xxx2'], 'Xxx2', 'D')
1767 call writefile(['Test file Xxx3'], 'Xxx3', 'D')
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +02001768
1769 " Add three files to the buffer list
1770 split Xxx1
1771 close
1772 split Xxx2
1773 close
1774 split Xxx3
1775 close
1776
1777 " Wipe the buffer when the buffer is opened
1778 au BufReadPost Xxx2 bwipe
1779
1780 call append(0, 'Test file Xxx4')
1781 ball
1782
1783 call assert_equal(2, winnr('$'))
1784 call assert_equal('Xxx1', bufname(winbufnr(winnr('$'))))
1785 wincmd t
1786
1787 au! BufReadPost
1788 %bwipe!
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +02001789 enew! | only
1790endfunc
1791
1792" Test for autocommand that changes current buffer on BufEnter event.
1793" Check if modelines are interpreted for the correct buffer.
1794func Test_Acmd_BufEnter()
1795 %bwipe!
1796 call writefile(['start of test file Xxx1',
1797 \ "\<Tab>this is a test",
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01001798 \ 'end of test file Xxx1'], 'Xxx1', 'D')
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +02001799 call writefile(['start of test file Xxx2',
1800 \ 'vim: set noai :',
1801 \ "\<Tab>this is a test",
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01001802 \ 'end of test file Xxx2'], 'Xxx2', 'D')
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +02001803
1804 au BufEnter Xxx2 brew
1805 set ai modeline modelines=3
1806 edit Xxx1
1807 " edit Xxx2, autocmd will do :brew
1808 edit Xxx2
1809 exe "normal G?this is a\<CR>"
1810 " Append text with autoindent to this file
1811 normal othis should be auto-indented
1812 call assert_equal("\<Tab>this should be auto-indented", getline('.'))
1813 call assert_equal(3, line('.'))
1814 " Remove autocmd and edit Xxx2 again
1815 au! BufEnter Xxx2
1816 buf! Xxx2
1817 exe "normal G?this is a\<CR>"
1818 " append text without autoindent to Xxx
1819 normal othis should be in column 1
1820 call assert_equal("this should be in column 1", getline('.'))
1821 call assert_equal(4, line('.'))
1822
1823 %bwipe!
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +02001824 set ai&vim modeline&vim modelines&vim
1825endfunc
1826
1827" Test for issue #57
1828" do not move cursor on <c-o> when autoindent is set
1829func Test_ai_CTRL_O()
1830 enew!
1831 set ai
1832 let save_fo = &fo
1833 set fo+=r
1834 exe "normal o# abcdef\<Esc>2hi\<CR>\<C-O>d0\<Esc>"
1835 exe "normal o# abcdef\<Esc>2hi\<C-O>d0\<Esc>"
1836 call assert_equal(['# abc', 'def', 'def'], getline(2, 4))
1837
1838 set ai&vim
1839 let &fo = save_fo
1840 enew!
1841endfunc
1842
1843" Test for autocommand that deletes the current buffer on BufLeave event.
1844" Also test deleting the last buffer, should give a new, empty buffer.
1845func Test_BufLeave_Wipe()
1846 %bwipe!
1847 let content = ['start of test file Xxx',
1848 \ 'this is a test',
1849 \ 'end of test file Xxx']
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01001850 call writefile(content, 'Xxx1', 'D')
1851 call writefile(content, 'Xxx2', 'D')
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +02001852
1853 au BufLeave Xxx2 bwipe
1854 edit Xxx1
1855 split Xxx2
1856 " delete buffer Xxx2, we should be back to Xxx1
1857 bwipe
1858 call assert_equal('Xxx1', bufname('%'))
1859 call assert_equal(1, winnr('$'))
1860
1861 " Create an alternate buffer
1862 %write! test.out
1863 call assert_equal('test.out', bufname('#'))
1864 " delete alternate buffer
1865 bwipe test.out
1866 call assert_equal('Xxx1', bufname('%'))
1867 call assert_equal('', bufname('#'))
1868
1869 au BufLeave Xxx1 bwipe
1870 " delete current buffer, get an empty one
1871 bwipe!
1872 call assert_equal(1, line('$'))
1873 call assert_equal('', bufname('%'))
Bram Moolenaarb2c87502017-10-14 21:15:58 +02001874 let g:bufinfo = getbufinfo()
1875 call assert_equal(1, len(g:bufinfo))
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +02001876
Bram Moolenaar53f0c962017-10-22 14:23:59 +02001877 call delete('test.out')
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +02001878 %bwipe
1879 au! BufLeave
Bram Moolenaarb2c87502017-10-14 21:15:58 +02001880
1881 " check that bufinfo doesn't contain a pointer to freed memory
1882 call test_garbagecollect_now()
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +02001883endfunc
Bram Moolenaar87ffb5c2017-10-19 12:37:42 +02001884
1885func Test_QuitPre()
1886 edit Xfoo
1887 let winid = win_getid(winnr())
1888 split Xbar
1889 au! QuitPre * let g:afile = expand('<afile>')
1890 " Close the other window, <afile> should be correct.
1891 exe win_id2win(winid) . 'q'
1892 call assert_equal('Xfoo', g:afile)
LemonBoy66e13ae2022-04-21 22:52:11 +01001893
Bram Moolenaar87ffb5c2017-10-19 12:37:42 +02001894 unlet g:afile
1895 bwipe Xfoo
1896 bwipe Xbar
1897endfunc
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02001898
1899func Test_Cmdline()
Bram Moolenaar153b7042018-01-31 15:48:32 +01001900 au! CmdlineChanged : let g:text = getcmdline()
1901 let g:text = 0
1902 call feedkeys(":echom 'hello'\<CR>", 'xt')
1903 call assert_equal("echom 'hello'", g:text)
1904 au! CmdlineChanged
1905
1906 au! CmdlineChanged : let g:entered = expand('<afile>')
1907 let g:entered = 0
1908 call feedkeys(":echom 'hello'\<CR>", 'xt')
1909 call assert_equal(':', g:entered)
1910 au! CmdlineChanged
1911
zeertzjq412e0e42023-02-11 10:34:07 +00001912 autocmd CmdlineChanged : let g:log += [getcmdline()]
1913
Bram Moolenaarbb393d82022-12-09 12:21:50 +00001914 let g:log = []
1915 cnoremap <F1> <Cmd>call setcmdline('ls')<CR>
Bram Moolenaarbb393d82022-12-09 12:21:50 +00001916 call feedkeys(":\<F1>", 'xt')
1917 call assert_equal(['ls'], g:log)
Bram Moolenaarbb393d82022-12-09 12:21:50 +00001918 cunmap <F1>
1919
zeertzjqaf9e28a2023-02-06 20:58:09 +00001920 let g:log = []
zeertzjqaf9e28a2023-02-06 20:58:09 +00001921 call feedkeys(":sign \<Tab>\<Tab>\<C-N>\<C-P>\<S-Tab>\<S-Tab>\<Esc>", 'xt')
1922 call assert_equal([
1923 \ 's',
1924 \ 'si',
1925 \ 'sig',
1926 \ 'sign',
1927 \ 'sign ',
1928 \ 'sign define',
1929 \ 'sign jump',
1930 \ 'sign list',
1931 \ 'sign jump',
1932 \ 'sign define',
1933 \ 'sign ',
1934 \ ], g:log)
1935 let g:log = []
1936 set wildmenu wildoptions+=pum
1937 call feedkeys(":sign \<S-Tab>\<PageUp>\<kPageUp>\<kPageDown>\<PageDown>\<Esc>", 'xt')
1938 call assert_equal([
1939 \ 's',
1940 \ 'si',
1941 \ 'sig',
1942 \ 'sign',
1943 \ 'sign ',
1944 \ 'sign unplace',
1945 \ 'sign jump',
1946 \ 'sign define',
1947 \ 'sign undefine',
1948 \ 'sign unplace',
1949 \ ], g:log)
1950 set wildmenu& wildoptions&
zeertzjq412e0e42023-02-11 10:34:07 +00001951
1952 let g:log = []
1953 let @r = 'abc'
1954 call feedkeys(":0\<C-R>r1\<C-R>\<C-O>r2\<C-R>\<C-R>r3\<Esc>", 'xt')
1955 call assert_equal([
1956 \ '0',
1957 \ '0a',
1958 \ '0ab',
1959 \ '0abc',
1960 \ '0abc1',
1961 \ '0abc1abc',
1962 \ '0abc1abc2',
1963 \ '0abc1abc2abc',
1964 \ '0abc1abc2abc3',
1965 \ ], g:log)
1966
zeertzjqaf9e28a2023-02-06 20:58:09 +00001967 unlet g:log
1968 au! CmdlineChanged
1969
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02001970 au! CmdlineEnter : let g:entered = expand('<afile>')
1971 au! CmdlineLeave : let g:left = expand('<afile>')
1972 let g:entered = 0
1973 let g:left = 0
1974 call feedkeys(":echo 'hello'\<CR>", 'xt')
1975 call assert_equal(':', g:entered)
1976 call assert_equal(':', g:left)
1977 au! CmdlineEnter
1978 au! CmdlineLeave
1979
Bram Moolenaara4baf5b2018-04-22 13:27:44 +02001980 let save_shellslash = &shellslash
1981 set noshellslash
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02001982 au! CmdlineEnter / let g:entered = expand('<afile>')
1983 au! CmdlineLeave / let g:left = expand('<afile>')
1984 let g:entered = 0
1985 let g:left = 0
Bram Moolenaar53f0c962017-10-22 14:23:59 +02001986 new
1987 call setline(1, 'hello')
1988 call feedkeys("/hello\<CR>", 'xt')
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02001989 call assert_equal('/', g:entered)
1990 call assert_equal('/', g:left)
Bram Moolenaar53f0c962017-10-22 14:23:59 +02001991 bwipe!
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02001992 au! CmdlineEnter
1993 au! CmdlineLeave
Bram Moolenaara4baf5b2018-04-22 13:27:44 +02001994 let &shellslash = save_shellslash
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02001995endfunc
Bram Moolenaar53f0c962017-10-22 14:23:59 +02001996
1997" Test for BufWritePre autocommand that deletes or unloads the buffer.
1998func Test_BufWritePre()
1999 %bwipe
2000 au BufWritePre Xxx1 bunload
2001 au BufWritePre Xxx2 bwipe
2002
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01002003 call writefile(['start of Xxx1', 'test', 'end of Xxx1'], 'Xxx1', 'D')
2004 call writefile(['start of Xxx2', 'test', 'end of Xxx2'], 'Xxx2', 'D')
Bram Moolenaar53f0c962017-10-22 14:23:59 +02002005
2006 edit Xtest
2007 e! Xxx2
2008 bdel Xtest
2009 e Xxx1
2010 " write it, will unload it and give an error msg
Bram Moolenaare2e40752020-09-04 21:18:46 +02002011 call assert_fails('w', 'E203:')
Bram Moolenaar53f0c962017-10-22 14:23:59 +02002012 call assert_equal('Xxx2', bufname('%'))
2013 edit Xtest
2014 e! Xxx2
2015 bwipe Xtest
2016 " write it, will delete the buffer and give an error msg
Bram Moolenaare2e40752020-09-04 21:18:46 +02002017 call assert_fails('w', 'E203:')
Bram Moolenaar53f0c962017-10-22 14:23:59 +02002018 call assert_equal('Xxx1', bufname('%'))
2019 au! BufWritePre
Bram Moolenaar53f0c962017-10-22 14:23:59 +02002020endfunc
2021
2022" Test for BufUnload autocommand that unloads all the other buffers
2023func Test_bufunload_all()
Bram Moolenaarf08b0eb2021-10-16 13:00:14 +01002024 let g:test_is_flaky = 1
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01002025 call writefile(['Test file Xxx1'], 'Xxx1', 'D')"
2026 call writefile(['Test file Xxx2'], 'Xxx2', 'D')"
Bram Moolenaar53f0c962017-10-22 14:23:59 +02002027
Bram Moolenaarc79745a2019-05-20 22:12:34 +02002028 let content =<< trim [CODE]
2029 func UnloadAllBufs()
2030 let i = 1
2031 while i <= bufnr('$')
2032 if i != bufnr('%') && bufloaded(i)
2033 exe i . 'bunload'
2034 endif
2035 let i += 1
2036 endwhile
2037 endfunc
2038 au BufUnload * call UnloadAllBufs()
2039 au VimLeave * call writefile(['Test Finished'], 'Xout')
2040 edit Xxx1
2041 split Xxx2
2042 q
2043 [CODE]
2044
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01002045 call writefile(content, 'Xbunloadtest', 'D')
Bram Moolenaar53f0c962017-10-22 14:23:59 +02002046
2047 call delete('Xout')
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01002048 call system(GetVimCommandClean() .. ' -N --not-a-term -S Xbunloadtest')
Bram Moolenaar53f0c962017-10-22 14:23:59 +02002049 call assert_true(filereadable('Xout'))
2050
Bram Moolenaar53f0c962017-10-22 14:23:59 +02002051 call delete('Xout')
2052endfunc
2053
2054" Some tests for buffer-local autocommands
2055func Test_buflocal_autocmd()
2056 let g:bname = ''
2057 edit xx
2058 au BufLeave <buffer> let g:bname = expand("%")
2059 " here, autocommand for xx should trigger.
2060 " but autocommand shall not apply to buffer named <buffer>.
2061 edit somefile
2062 call assert_equal('xx', g:bname)
2063 let g:bname = ''
2064 " here, autocommand shall be auto-deleted
2065 bwipe xx
2066 " autocmd should not trigger
2067 edit xx
2068 call assert_equal('', g:bname)
2069 " autocmd should not trigger
2070 edit somefile
2071 call assert_equal('', g:bname)
2072 enew
2073 unlet g:bname
2074endfunc
Bram Moolenaar430dc5d2017-11-02 21:04:47 +01002075
2076" Test for "*Cmd" autocommands
2077func Test_Cmd_Autocmds()
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01002078 call writefile(['start of Xxx', "\tabc2", 'end of Xxx'], 'Xxx', 'D')
Bram Moolenaar430dc5d2017-11-02 21:04:47 +01002079
2080 enew!
2081 au BufReadCmd XtestA 0r Xxx|$del
2082 edit XtestA " will read text of Xxd instead
2083 call assert_equal('start of Xxx', getline(1))
2084
2085 au BufWriteCmd XtestA call append(line("$"), "write")
2086 write " will append a line to the file
2087 call assert_equal('write', getline('$'))
Bram Moolenaare2e40752020-09-04 21:18:46 +02002088 call assert_fails('read XtestA', 'E484:') " should not read anything
Bram Moolenaar430dc5d2017-11-02 21:04:47 +01002089 call assert_equal('write', getline(4))
2090
2091 " now we have:
2092 " 1 start of Xxx
2093 " 2 abc2
2094 " 3 end of Xxx
2095 " 4 write
2096
2097 au FileReadCmd XtestB '[r Xxx
2098 2r XtestB " will read Xxx below line 2 instead
2099 call assert_equal('start of Xxx', getline(3))
2100
2101 " now we have:
2102 " 1 start of Xxx
2103 " 2 abc2
2104 " 3 start of Xxx
2105 " 4 abc2
2106 " 5 end of Xxx
2107 " 6 end of Xxx
2108 " 7 write
2109
2110 au FileWriteCmd XtestC '[,']copy $
2111 normal 4GA1
2112 4,5w XtestC " will copy lines 4 and 5 to the end
2113 call assert_equal("\tabc21", getline(8))
Bram Moolenaare2e40752020-09-04 21:18:46 +02002114 call assert_fails('r XtestC', 'E484:') " should not read anything
Bram Moolenaar430dc5d2017-11-02 21:04:47 +01002115 call assert_equal("end of Xxx", getline(9))
2116
2117 " now we have:
2118 " 1 start of Xxx
2119 " 2 abc2
2120 " 3 start of Xxx
2121 " 4 abc21
2122 " 5 end of Xxx
2123 " 6 end of Xxx
2124 " 7 write
2125 " 8 abc21
2126 " 9 end of Xxx
2127
2128 let g:lines = []
2129 au FileAppendCmd XtestD call extend(g:lines, getline(line("'["), line("']")))
2130 w >>XtestD " will add lines to 'lines'
2131 call assert_equal(9, len(g:lines))
Bram Moolenaare2e40752020-09-04 21:18:46 +02002132 call assert_fails('$r XtestD', 'E484:') " should not read anything
Bram Moolenaar430dc5d2017-11-02 21:04:47 +01002133 call assert_equal(9, line('$'))
2134 call assert_equal('end of Xxx', getline('$'))
2135
2136 au BufReadCmd XtestE 0r Xxx|$del
2137 sp XtestE " split window with test.out
2138 call assert_equal('end of Xxx', getline(3))
2139
2140 let g:lines = []
2141 exe "normal 2Goasdf\<Esc>\<C-W>\<C-W>"
2142 au BufWriteCmd XtestE call extend(g:lines, getline(0, '$'))
2143 wall " will write other window to 'lines'
2144 call assert_equal(4, len(g:lines), g:lines)
2145 call assert_equal('asdf', g:lines[2])
2146
2147 au! BufReadCmd
2148 au! BufWriteCmd
2149 au! FileReadCmd
2150 au! FileWriteCmd
2151 au! FileAppendCmd
2152 %bwipe!
Bram Moolenaar430dc5d2017-11-02 21:04:47 +01002153 enew!
2154endfunc
Bram Moolenaaraace2152017-11-05 16:23:10 +01002155
Bram Moolenaar0fff4412020-03-29 16:06:29 +02002156func s:ReadFile()
2157 setl noswapfile nomodified
2158 let filename = resolve(expand("<afile>:p"))
2159 execute 'read' fnameescape(filename)
2160 1d_
2161 exe 'file' fnameescape(filename)
2162 setl buftype=acwrite
2163endfunc
2164
2165func s:WriteFile()
2166 let filename = resolve(expand("<afile>:p"))
2167 setl buftype=
2168 noautocmd execute 'write' fnameescape(filename)
2169 setl buftype=acwrite
2170 setl nomodified
2171endfunc
2172
2173func Test_BufReadCmd()
2174 autocmd BufReadCmd *.test call s:ReadFile()
2175 autocmd BufWriteCmd *.test call s:WriteFile()
2176
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01002177 call writefile(['one', 'two', 'three'], 'Xcmd.test', 'D')
Bram Moolenaar0fff4412020-03-29 16:06:29 +02002178 edit Xcmd.test
2179 call assert_match('Xcmd.test" line 1 of 3', execute('file'))
2180 normal! Gofour
2181 write
2182 call assert_equal(['one', 'two', 'three', 'four'], readfile('Xcmd.test'))
2183
2184 bwipe!
Bram Moolenaar0fff4412020-03-29 16:06:29 +02002185 au! BufReadCmd
2186 au! BufWriteCmd
2187endfunc
2188
zeertzjq9c8f9462022-08-30 18:17:15 +01002189func Test_BufWriteCmd()
2190 autocmd BufWriteCmd Xbufwritecmd let g:written = 1
2191 new
2192 file Xbufwritecmd
2193 set buftype=acwrite
Bram Moolenaar6f14da12022-09-07 21:30:44 +01002194 call mkdir('Xbufwritecmd', 'D')
zeertzjq9c8f9462022-08-30 18:17:15 +01002195 write
2196 " BufWriteCmd should be triggered even if a directory has the same name
2197 call assert_equal(1, g:written)
zeertzjq9c8f9462022-08-30 18:17:15 +01002198 unlet g:written
2199 au! BufWriteCmd
2200 bwipe!
2201endfunc
2202
Bram Moolenaaraace2152017-11-05 16:23:10 +01002203func SetChangeMarks(start, end)
Bram Moolenaar97c69432021-01-15 16:45:21 +01002204 exe a:start .. 'mark ['
2205 exe a:end .. 'mark ]'
Bram Moolenaaraace2152017-11-05 16:23:10 +01002206endfunc
2207
2208" Verify the effects of autocmds on '[ and ']
2209func Test_change_mark_in_autocmds()
2210 edit! Xtest
Bram Moolenaar97c69432021-01-15 16:45:21 +01002211 call feedkeys("ia\<CR>b\<CR>c\<CR>d\<C-g>u\<Esc>", 'xtn')
Bram Moolenaaraace2152017-11-05 16:23:10 +01002212
2213 call SetChangeMarks(2, 3)
2214 write
2215 call assert_equal([1, 4], [line("'["), line("']")])
2216
2217 call SetChangeMarks(2, 3)
2218 au BufWritePre * call assert_equal([1, 4], [line("'["), line("']")])
2219 write
2220 au! BufWritePre
2221
Bram Moolenaar14ddd222020-08-05 12:02:40 +02002222 if has('unix')
Bram Moolenaaraace2152017-11-05 16:23:10 +01002223 write XtestFilter
2224 write >> XtestFilter
2225
2226 call SetChangeMarks(2, 3)
2227 " Marks are set to the entire range of the write
2228 au FilterWritePre * call assert_equal([1, 4], [line("'["), line("']")])
2229 " '[ is adjusted to just before the line that will receive the filtered
2230 " data
2231 au FilterReadPre * call assert_equal([4, 4], [line("'["), line("']")])
2232 " The filtered data is read into the buffer, and the source lines are
2233 " still present, so the range is after the source lines
2234 au FilterReadPost * call assert_equal([5, 12], [line("'["), line("']")])
2235 %!cat XtestFilter
2236 " After the filtered data is read, the original lines are deleted
2237 call assert_equal([1, 8], [line("'["), line("']")])
2238 au! FilterWritePre,FilterReadPre,FilterReadPost
2239 undo
2240
2241 call SetChangeMarks(1, 4)
2242 au FilterWritePre * call assert_equal([2, 3], [line("'["), line("']")])
2243 au FilterReadPre * call assert_equal([3, 3], [line("'["), line("']")])
2244 au FilterReadPost * call assert_equal([4, 11], [line("'["), line("']")])
2245 2,3!cat XtestFilter
2246 call assert_equal([2, 9], [line("'["), line("']")])
2247 au! FilterWritePre,FilterReadPre,FilterReadPost
2248 undo
2249
2250 call delete('XtestFilter')
2251 endif
2252
2253 call SetChangeMarks(1, 4)
2254 au FileWritePre * call assert_equal([2, 3], [line("'["), line("']")])
2255 2,3write Xtest2
2256 au! FileWritePre
2257
2258 call SetChangeMarks(2, 3)
2259 au FileAppendPre * call assert_equal([1, 4], [line("'["), line("']")])
2260 write >> Xtest2
2261 au! FileAppendPre
2262
2263 call SetChangeMarks(1, 4)
2264 au FileAppendPre * call assert_equal([2, 3], [line("'["), line("']")])
2265 2,3write >> Xtest2
2266 au! FileAppendPre
2267
2268 call SetChangeMarks(1, 1)
2269 au FileReadPre * call assert_equal([3, 1], [line("'["), line("']")])
2270 au FileReadPost * call assert_equal([4, 11], [line("'["), line("']")])
2271 3read Xtest2
2272 au! FileReadPre,FileReadPost
2273 undo
2274
2275 call SetChangeMarks(4, 4)
2276 " When the line is 0, it's adjusted to 1
2277 au FileReadPre * call assert_equal([1, 4], [line("'["), line("']")])
2278 au FileReadPost * call assert_equal([1, 8], [line("'["), line("']")])
2279 0read Xtest2
2280 au! FileReadPre,FileReadPost
2281 undo
2282
2283 call SetChangeMarks(4, 4)
2284 " When the line is 0, it's adjusted to 1
2285 au FileReadPre * call assert_equal([1, 4], [line("'["), line("']")])
2286 au FileReadPost * call assert_equal([2, 9], [line("'["), line("']")])
2287 1read Xtest2
2288 au! FileReadPre,FileReadPost
2289 undo
2290
2291 bwipe!
2292 call delete('Xtest')
2293 call delete('Xtest2')
2294endfunc
2295
2296func Test_Filter_noshelltemp()
Bram Moolenaaraeb313f2020-11-27 19:13:28 +01002297 CheckExecutable cat
Bram Moolenaaraace2152017-11-05 16:23:10 +01002298
2299 enew!
2300 call setline(1, ['a', 'b', 'c', 'd'])
2301
2302 let shelltemp = &shelltemp
2303 set shelltemp
2304
2305 let g:filter_au = 0
2306 au FilterWritePre * let g:filter_au += 1
2307 au FilterReadPre * let g:filter_au += 1
2308 au FilterReadPost * let g:filter_au += 1
2309 %!cat
2310 call assert_equal(3, g:filter_au)
2311
2312 if has('filterpipe')
2313 set noshelltemp
2314
2315 let g:filter_au = 0
2316 au FilterWritePre * let g:filter_au += 1
2317 au FilterReadPre * let g:filter_au += 1
2318 au FilterReadPost * let g:filter_au += 1
2319 %!cat
2320 call assert_equal(0, g:filter_au)
2321 endif
2322
2323 au! FilterWritePre,FilterReadPre,FilterReadPost
2324 let &shelltemp = shelltemp
2325 bwipe!
2326endfunc
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01002327
2328func Test_TextYankPost()
2329 enew!
2330 call setline(1, ['foo'])
2331
2332 let g:event = []
2333 au TextYankPost * let g:event = copy(v:event)
2334
2335 call assert_equal({}, v:event)
2336 call assert_fails('let v:event = {}', 'E46:')
2337 call assert_fails('let v:event.mykey = 0', 'E742:')
2338
2339 norm "ayiw
2340 call assert_equal(
Bram Moolenaara016eeb2022-04-09 11:37:38 +01002341 \ #{regcontents: ['foo'], regname: 'a', operator: 'y',
2342 \ regtype: 'v', visual: v:false, inclusive: v:true},
2343 \ g:event)
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01002344 norm y_
2345 call assert_equal(
Bram Moolenaara016eeb2022-04-09 11:37:38 +01002346 \ #{regcontents: ['foo'], regname: '', operator: 'y', regtype: 'V',
2347 \ visual: v:false, inclusive: v:false},
2348 \ g:event)
Bram Moolenaar37d16732020-06-12 22:09:01 +02002349 norm Vy
2350 call assert_equal(
Bram Moolenaara016eeb2022-04-09 11:37:38 +01002351 \ #{regcontents: ['foo'], regname: '', operator: 'y', regtype: 'V',
2352 \ visual: v:true, inclusive: v:true},
2353 \ g:event)
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01002354 call feedkeys("\<C-V>y", 'x')
2355 call assert_equal(
Bram Moolenaara016eeb2022-04-09 11:37:38 +01002356 \ #{regcontents: ['f'], regname: '', operator: 'y', regtype: "\x161",
2357 \ visual: v:true, inclusive: v:true},
2358 \ g:event)
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01002359 norm "xciwbar
2360 call assert_equal(
Bram Moolenaara016eeb2022-04-09 11:37:38 +01002361 \ #{regcontents: ['foo'], regname: 'x', operator: 'c', regtype: 'v',
2362 \ visual: v:false, inclusive: v:true},
2363 \ g:event)
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01002364 norm "bdiw
2365 call assert_equal(
Bram Moolenaara016eeb2022-04-09 11:37:38 +01002366 \ #{regcontents: ['bar'], regname: 'b', operator: 'd', regtype: 'v',
2367 \ visual: v:false, inclusive: v:true},
2368 \ g:event)
2369
2370 call setline(1, 'foobar')
2371 " exclusive motion
2372 norm $"ay0
2373 call assert_equal(
2374 \ #{regcontents: ['fooba'], regname: 'a', operator: 'y', regtype: 'v',
2375 \ visual: v:false, inclusive: v:false},
2376 \ g:event)
2377 " inclusive motion
2378 norm 0"ay$
2379 call assert_equal(
2380 \ #{regcontents: ['foobar'], regname: 'a', operator: 'y', regtype: 'v',
2381 \ visual: v:false, inclusive: v:true},
2382 \ g:event)
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01002383
2384 call assert_equal({}, v:event)
2385
Bram Moolenaarfccbf062020-11-26 20:34:00 +01002386 if has('clipboard_working') && !has('gui_running')
2387 " Test that when the visual selection is automatically copied to clipboard
2388 " register a TextYankPost is emitted
2389 call setline(1, ['foobar'])
2390
2391 let @* = ''
2392 set clipboard=autoselect
2393 exe "norm! ggviw\<Esc>"
2394 call assert_equal(
Bram Moolenaara016eeb2022-04-09 11:37:38 +01002395 \ #{regcontents: ['foobar'], regname: '*', operator: 'y',
2396 \ regtype: 'v', visual: v:true, inclusive: v:false},
2397 \ g:event)
Bram Moolenaarfccbf062020-11-26 20:34:00 +01002398
2399 let @+ = ''
2400 set clipboard=autoselectplus
2401 exe "norm! ggviw\<Esc>"
2402 call assert_equal(
Bram Moolenaara016eeb2022-04-09 11:37:38 +01002403 \ #{regcontents: ['foobar'], regname: '+', operator: 'y',
2404 \ regtype: 'v', visual: v:true, inclusive: v:false},
2405 \ g:event)
Bram Moolenaarfccbf062020-11-26 20:34:00 +01002406
2407 set clipboard&vim
2408 endif
2409
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01002410 au! TextYankPost
2411 unlet g:event
2412 bwipe!
2413endfunc
Bram Moolenaar9bca8052017-12-18 12:37:55 +01002414
Bram Moolenaar9a046fd2021-01-28 13:47:59 +01002415func Test_autocommand_all_events()
2416 call assert_fails('au * * bwipe', 'E1155:')
2417 call assert_fails('au * x bwipe', 'E1155:')
Bram Moolenaarb6db1462021-12-24 19:24:47 +00002418 call assert_fails('au! * x bwipe', 'E1155:')
Bram Moolenaar4fb921e2017-12-18 15:33:00 +01002419endfunc
Bram Moolenaarb7407d32018-02-03 17:36:27 +01002420
Bram Moolenaarf6246f52022-02-11 16:30:12 +00002421func Test_autocmd_user()
2422 au User MyEvent let s:res = [expand("<afile>"), expand("<amatch>")]
2423 doautocmd User MyEvent
2424 call assert_equal(['MyEvent', 'MyEvent'], s:res)
2425 au! User
2426 unlet s:res
2427endfunc
2428
Bram Moolenaar3b014be2022-11-13 17:53:46 +00002429func Test_autocmd_user_clear_group()
2430 CheckRunVimInTerminal
2431
2432 let lines =<< trim END
2433 autocmd! User
2434 for i in range(1, 999)
2435 exe 'autocmd User ' .. 'Foo' .. i .. ' bar'
2436 endfor
2437 au CmdlineLeave : call timer_start(0, {-> execute('autocmd! User')})
2438 END
2439 call writefile(lines, 'XautoUser', 'D')
2440 let buf = RunVimInTerminal('-S XautoUser', {'rows': 10})
2441
2442 " this was using freed memory
2443 call term_sendkeys(buf, ":autocmd User\<CR>")
2444 call TermWait(buf, 50)
2445 call term_sendkeys(buf, "G")
2446
2447 call StopVimInTerminal(buf)
2448endfunc
2449
Bram Moolenaaref2c3252022-11-25 16:31:51 +00002450func Test_autocmd_CmdlineLeave_unlet()
2451 CheckRunVimInTerminal
2452
2453 let lines =<< trim END
2454 for i in range(1, 999)
2455 exe 'let g:var' .. i '=' i
2456 endfor
2457 au CmdlineLeave : call timer_start(0, {-> execute('unlet g:var990')})
2458 END
2459 call writefile(lines, 'XleaveUnlet', 'D')
2460 let buf = RunVimInTerminal('-S XleaveUnlet', {'rows': 10})
2461
2462 " this was using freed memory
2463 call term_sendkeys(buf, ":let g:\<CR>")
2464 call TermWait(buf, 50)
2465 call term_sendkeys(buf, "G")
2466 call TermWait(buf, 50)
2467 call term_sendkeys(buf, "\<CR>") " for the hit-enter prompt
2468
2469 call StopVimInTerminal(buf)
2470endfunc
2471
Bram Moolenaarb7407d32018-02-03 17:36:27 +01002472function s:Before_test_dirchanged()
2473 augroup test_dirchanged
2474 autocmd!
2475 augroup END
2476 let s:li = []
2477 let s:dir_this = getcwd()
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02002478 let s:dir_foo = s:dir_this . '/Xfoo'
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01002479 call mkdir(s:dir_foo)
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02002480 let s:dir_bar = s:dir_this . '/Xbar'
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01002481 call mkdir(s:dir_bar)
Bram Moolenaarb7407d32018-02-03 17:36:27 +01002482endfunc
2483
2484function s:After_test_dirchanged()
Bram Moolenaar3503d7c2019-11-09 20:10:17 +01002485 call chdir(s:dir_this)
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01002486 call delete(s:dir_foo, 'd')
2487 call delete(s:dir_bar, 'd')
Bram Moolenaarb7407d32018-02-03 17:36:27 +01002488 augroup test_dirchanged
2489 autocmd!
2490 augroup END
2491endfunc
2492
2493function Test_dirchanged_global()
2494 call s:Before_test_dirchanged()
Bram Moolenaarf6246f52022-02-11 16:30:12 +00002495 autocmd test_dirchanged DirChangedPre global call add(s:li, expand("<amatch>") .. " pre cd " .. v:event.directory)
Bram Moolenaarb7407d32018-02-03 17:36:27 +01002496 autocmd test_dirchanged DirChanged global call add(s:li, "cd:")
2497 autocmd test_dirchanged DirChanged global call add(s:li, expand("<afile>"))
Bram Moolenaar3503d7c2019-11-09 20:10:17 +01002498 call chdir(s:dir_foo)
Bram Moolenaarf6246f52022-02-11 16:30:12 +00002499 let expected = ["global pre cd " .. s:dir_foo, "cd:", s:dir_foo]
Bram Moolenaar28e8f732022-02-09 12:58:20 +00002500 call assert_equal(expected, s:li)
Bram Moolenaar3503d7c2019-11-09 20:10:17 +01002501 call chdir(s:dir_foo)
Bram Moolenaar28e8f732022-02-09 12:58:20 +00002502 call assert_equal(expected, s:li)
Bram Moolenaar3503d7c2019-11-09 20:10:17 +01002503 exe 'lcd ' .. fnameescape(s:dir_bar)
Bram Moolenaar28e8f732022-02-09 12:58:20 +00002504 call assert_equal(expected, s:li)
Bram Moolenaard8c9d322022-06-12 11:49:16 +01002505
2506 exe 'cd ' .. s:dir_foo
2507 exe 'cd ' .. s:dir_bar
2508 autocmd! test_dirchanged DirChanged global let g:result = expand("<afile>")
2509 cd -
Bram Moolenaardb77c492022-06-12 23:26:50 +01002510 call assert_equal(s:dir_foo, substitute(g:result, '\\', '/', 'g'))
Bram Moolenaard8c9d322022-06-12 11:49:16 +01002511
Bram Moolenaarb7407d32018-02-03 17:36:27 +01002512 call s:After_test_dirchanged()
2513endfunc
2514
2515function Test_dirchanged_local()
2516 call s:Before_test_dirchanged()
2517 autocmd test_dirchanged DirChanged window call add(s:li, "lcd:")
2518 autocmd test_dirchanged DirChanged window call add(s:li, expand("<afile>"))
Bram Moolenaar3503d7c2019-11-09 20:10:17 +01002519 call chdir(s:dir_foo)
Bram Moolenaarb7407d32018-02-03 17:36:27 +01002520 call assert_equal([], s:li)
Bram Moolenaar3503d7c2019-11-09 20:10:17 +01002521 exe 'lcd ' .. fnameescape(s:dir_bar)
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01002522 call assert_equal(["lcd:", s:dir_bar], s:li)
Bram Moolenaar3503d7c2019-11-09 20:10:17 +01002523 exe 'lcd ' .. fnameescape(s:dir_bar)
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01002524 call assert_equal(["lcd:", s:dir_bar], s:li)
Bram Moolenaarb7407d32018-02-03 17:36:27 +01002525 call s:After_test_dirchanged()
2526endfunc
2527
2528function Test_dirchanged_auto()
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +02002529 CheckOption autochdir
Bram Moolenaarb7407d32018-02-03 17:36:27 +01002530 call s:Before_test_dirchanged()
2531 call test_autochdir()
Bram Moolenaar28e8f732022-02-09 12:58:20 +00002532 autocmd test_dirchanged DirChangedPre auto call add(s:li, "pre cd " .. v:event.directory)
Bram Moolenaarb7407d32018-02-03 17:36:27 +01002533 autocmd test_dirchanged DirChanged auto call add(s:li, "auto:")
2534 autocmd test_dirchanged DirChanged auto call add(s:li, expand("<afile>"))
2535 set acd
Bram Moolenaar3503d7c2019-11-09 20:10:17 +01002536 cd ..
Bram Moolenaarb7407d32018-02-03 17:36:27 +01002537 call assert_equal([], s:li)
Bram Moolenaar61abe7d2022-08-30 21:46:08 +01002538 exe 'edit ' . s:dir_foo . '/Xautofile'
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01002539 call assert_equal(s:dir_foo, getcwd())
Bram Moolenaar28e8f732022-02-09 12:58:20 +00002540 let expected = ["pre cd " .. s:dir_foo, "auto:", s:dir_foo]
2541 call assert_equal(expected, s:li)
Bram Moolenaarb7407d32018-02-03 17:36:27 +01002542 set noacd
2543 bwipe!
2544 call s:After_test_dirchanged()
2545endfunc
Bram Moolenaar5a093432018-02-10 18:15:19 +01002546
2547" Test TextChangedI and TextChangedP
2548func Test_ChangedP()
2549 new
2550 call setline(1, ['foo', 'bar', 'foobar'])
2551 call test_override("char_avail", 1)
2552 set complete=. completeopt=menuone
2553
2554 func! TextChangedAutocmd(char)
2555 let g:autocmd .= a:char
2556 endfunc
2557
Christian Brabandtdb3b4462021-10-16 11:58:55 +01002558 " TextChanged will not be triggered, only check that it isn't.
Bram Moolenaar5a093432018-02-10 18:15:19 +01002559 au! TextChanged <buffer> :call TextChangedAutocmd('N')
2560 au! TextChangedI <buffer> :call TextChangedAutocmd('I')
2561 au! TextChangedP <buffer> :call TextChangedAutocmd('P')
2562
2563 call cursor(3, 1)
2564 let g:autocmd = ''
2565 call feedkeys("o\<esc>", 'tnix')
2566 call assert_equal('I', g:autocmd)
2567
2568 let g:autocmd = ''
2569 call feedkeys("Sf", 'tnix')
2570 call assert_equal('II', g:autocmd)
2571
2572 let g:autocmd = ''
2573 call feedkeys("Sf\<C-N>", 'tnix')
2574 call assert_equal('IIP', g:autocmd)
2575
2576 let g:autocmd = ''
2577 call feedkeys("Sf\<C-N>\<C-N>", 'tnix')
2578 call assert_equal('IIPP', g:autocmd)
2579
2580 let g:autocmd = ''
2581 call feedkeys("Sf\<C-N>\<C-N>\<C-N>", 'tnix')
2582 call assert_equal('IIPPP', g:autocmd)
2583
2584 let g:autocmd = ''
2585 call feedkeys("Sf\<C-N>\<C-N>\<C-N>\<C-N>", 'tnix')
2586 call assert_equal('IIPPPP', g:autocmd)
2587
2588 call assert_equal(['foo', 'bar', 'foobar', 'foo'], getline(1, '$'))
2589 " TODO: how should it handle completeopt=noinsert,noselect?
2590
2591 " CleanUp
2592 call test_override("char_avail", 0)
2593 au! TextChanged
2594 au! TextChangedI
2595 au! TextChangedP
2596 delfu TextChangedAutocmd
2597 unlet! g:autocmd
2598 set complete&vim completeopt&vim
2599
2600 bw!
2601endfunc
Bram Moolenaar8c64a362018-03-23 22:39:31 +01002602
Bram Moolenaar91d2e782018-08-07 19:05:01 +02002603let g:setline_handled = v:false
Bram Moolenaar1e115362019-01-09 23:01:02 +01002604func SetLineOne()
Bram Moolenaar91d2e782018-08-07 19:05:01 +02002605 if !g:setline_handled
2606 call setline(1, "(x)")
2607 let g:setline_handled = v:true
2608 endif
2609endfunc
2610
2611func Test_TextChangedI_with_setline()
2612 new
2613 call test_override('char_avail', 1)
2614 autocmd TextChangedI <buffer> call SetLineOne()
2615 call feedkeys("i(\<CR>\<Esc>", 'tx')
2616 call assert_equal('(', getline(1))
2617 call assert_equal('x)', getline(2))
2618 undo
Bram Moolenaar91d2e782018-08-07 19:05:01 +02002619 call assert_equal('', getline(1))
Bram Moolenaar9fa95062018-08-08 22:08:32 +02002620 call assert_equal('', getline(2))
Bram Moolenaar91d2e782018-08-07 19:05:01 +02002621
Bram Moolenaarca34db32022-01-20 11:17:18 +00002622 call test_override('char_avail', 0)
Bram Moolenaar91d2e782018-08-07 19:05:01 +02002623 bwipe!
2624endfunc
2625
Bram Moolenaar8c64a362018-03-23 22:39:31 +01002626func Test_Changed_FirstTime()
Bram Moolenaar8c5a2782019-08-07 23:07:07 +02002627 CheckFeature terminal
2628 CheckNotGui
Bram Moolenaar3cdcb092020-03-18 19:18:10 +01002629 " Starting a terminal to run Vim is always considered flaky.
Bram Moolenaar30d53e22020-03-18 21:10:44 +01002630 let g:test_is_flaky = 1
Bram Moolenaar8c5a2782019-08-07 23:07:07 +02002631
Bram Moolenaar8c64a362018-03-23 22:39:31 +01002632 " Prepare file for TextChanged event.
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01002633 call writefile([''], 'Xchanged.txt', 'D')
Bram Moolenaar8c64a362018-03-23 22:39:31 +01002634 let buf = term_start([GetVimProg(), '--clean', '-c', 'set noswapfile'], {'term_rows': 3})
2635 call assert_equal('running', term_getstatus(buf))
Bram Moolenaar1834d372018-03-29 17:40:46 +02002636 " Wait for the ruler (in the status line) to be shown.
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01002637 " In ConPTY, there is additional character which is drawn up to the width of
2638 " the screen.
2639 if has('conpty')
2640 call WaitForAssert({-> assert_match('\<All.*$', term_getline(buf, 3))})
2641 else
2642 call WaitForAssert({-> assert_match('\<All$', term_getline(buf, 3))})
2643 endif
Bram Moolenaar8c64a362018-03-23 22:39:31 +01002644 " It's only adding autocmd, so that no event occurs.
2645 call term_sendkeys(buf, ":au! TextChanged <buffer> call writefile(['No'], 'Xchanged.txt')\<cr>")
2646 call term_sendkeys(buf, "\<C-\\>\<C-N>:qa!\<cr>")
Bram Moolenaar50182fa2018-04-28 21:34:40 +02002647 call WaitForAssert({-> assert_equal('finished', term_getstatus(buf))})
Bram Moolenaar8c64a362018-03-23 22:39:31 +01002648 call assert_equal([''], readfile('Xchanged.txt'))
2649
2650 " clean up
Bram Moolenaar8c64a362018-03-23 22:39:31 +01002651 bwipe!
2652endfunc
Bram Moolenaar0566e892019-01-24 19:37:40 +01002653
Bram Moolenaareb93f3f2019-04-04 15:04:56 +02002654func Test_autocmd_nested()
2655 let g:did_nested = 0
2656 augroup Testing
2657 au WinNew * edit somefile
2658 au BufNew * let g:did_nested = 1
2659 augroup END
2660 split
2661 call assert_equal(0, g:did_nested)
2662 close
2663 bwipe! somefile
2664
2665 " old nested argument still works
2666 augroup Testing
2667 au!
2668 au WinNew * nested edit somefile
2669 au BufNew * let g:did_nested = 1
2670 augroup END
2671 split
2672 call assert_equal(1, g:did_nested)
2673 close
2674 bwipe! somefile
2675
2676 " New ++nested argument works
2677 augroup Testing
2678 au!
2679 au WinNew * ++nested edit somefile
2680 au BufNew * let g:did_nested = 1
2681 augroup END
2682 split
2683 call assert_equal(1, g:did_nested)
2684 close
2685 bwipe! somefile
2686
Bram Moolenaarf0775142022-03-04 20:10:38 +00002687 " nested without ++ does not work in Vim9 script
2688 call assert_fails('vim9cmd au WinNew * nested echo fails', 'E1078:')
2689
Bram Moolenaareb93f3f2019-04-04 15:04:56 +02002690 augroup Testing
2691 au!
2692 augroup END
2693
2694 call assert_fails('au WinNew * ++nested ++nested echo bad', 'E983:')
2695 call assert_fails('au WinNew * nested nested echo bad', 'E983:')
2696endfunc
2697
Bram Moolenaar5fa9f232022-07-23 09:06:48 +01002698func Test_autocmd_nested_cursor_invalid()
2699 set laststatus=0
2700 copen
2701 cclose
2702 call setline(1, ['foo', 'bar', 'baz'])
2703 3
2704 augroup nested_inv
2705 autocmd User foo ++nested copen
2706 autocmd BufAdd * let &laststatus = 2 - &laststatus
2707 augroup END
2708 doautocmd User foo
2709
2710 augroup nested_inv
2711 au!
2712 augroup END
2713 set laststatus&
Bram Moolenaarb03950f2022-07-26 13:47:13 +01002714 cclose
Bram Moolenaar5fa9f232022-07-23 09:06:48 +01002715 bwipe!
2716endfunc
2717
Bram Moolenaar3d6ee8b2022-07-27 15:23:35 +01002718func Test_autocmd_nested_keeps_cursor_pos()
2719 enew
2720 call setline(1, 'foo')
2721 autocmd User foo ++nested normal! $a
2722 autocmd InsertLeave * :
2723 doautocmd User foo
2724 call assert_equal([0, 1, 3, 0], getpos('.'))
2725
2726 bwipe!
2727endfunc
2728
Bram Moolenaarb03950f2022-07-26 13:47:13 +01002729func Test_autocmd_nested_switch_window()
2730 " run this in a separate Vim so that SafeState works
2731 CheckRunVimInTerminal
2732
2733 let lines =<< trim END
2734 vim9script
2735 ['()']->writefile('Xautofile')
2736 autocmd VimEnter * ++nested edit Xautofile | split
2737 autocmd BufReadPost * autocmd SafeState * ++once foldclosed('.')
2738 autocmd WinEnter * matchadd('ErrorMsg', 'pat')
2739 END
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01002740 call writefile(lines, 'Xautoscript', 'D')
Bram Moolenaarb03950f2022-07-26 13:47:13 +01002741 let buf = RunVimInTerminal('-S Xautoscript', {'rows': 10})
2742 call VerifyScreenDump(buf, 'Test_autocmd_nested_switch', {})
2743
2744 call StopVimInTerminal(buf)
2745 call delete('Xautofile')
Bram Moolenaarb03950f2022-07-26 13:47:13 +01002746endfunc
2747
Bram Moolenaareb93f3f2019-04-04 15:04:56 +02002748func Test_autocmd_once()
2749 " Without ++once WinNew triggers twice
2750 let g:did_split = 0
2751 augroup Testing
2752 au WinNew * let g:did_split += 1
2753 augroup END
2754 split
2755 split
2756 call assert_equal(2, g:did_split)
2757 call assert_true(exists('#WinNew'))
2758 close
2759 close
2760
2761 " With ++once WinNew triggers once
2762 let g:did_split = 0
2763 augroup Testing
2764 au!
2765 au WinNew * ++once let g:did_split += 1
2766 augroup END
2767 split
2768 split
2769 call assert_equal(1, g:did_split)
2770 call assert_false(exists('#WinNew'))
2771 close
2772 close
2773
2774 call assert_fails('au WinNew * ++once ++once echo bad', 'E983:')
2775endfunc
2776
Bram Moolenaara68e5952019-04-25 22:22:01 +02002777func Test_autocmd_bufreadpre()
2778 new
2779 let b:bufreadpre = 1
Bram Moolenaarab505b12020-03-23 19:28:44 +01002780 call append(0, range(1000))
Bram Moolenaara68e5952019-04-25 22:22:01 +02002781 w! XAutocmdBufReadPre.txt
2782 autocmd BufReadPre <buffer> :let b:bufreadpre += 1
Bram Moolenaarab505b12020-03-23 19:28:44 +01002783 norm! 500gg
Bram Moolenaara68e5952019-04-25 22:22:01 +02002784 sp
Bram Moolenaarab505b12020-03-23 19:28:44 +01002785 norm! 1000gg
Bram Moolenaara68e5952019-04-25 22:22:01 +02002786 wincmd p
2787 let g:wsv1 = winsaveview()
2788 wincmd p
2789 let g:wsv2 = winsaveview()
2790 " triggers BufReadPre, should not move the cursor in either window
2791 " The topline may change one line in a large window.
2792 edit
2793 call assert_inrange(g:wsv2.topline - 1, g:wsv2.topline + 1, winsaveview().topline)
2794 call assert_equal(g:wsv2.lnum, winsaveview().lnum)
2795 call assert_equal(2, b:bufreadpre)
2796 wincmd p
2797 call assert_equal(g:wsv1.topline, winsaveview().topline)
2798 call assert_equal(g:wsv1.lnum, winsaveview().lnum)
2799 call assert_equal(2, b:bufreadpre)
2800 " Now set the cursor position in an BufReadPre autocommand
2801 " (even though the position will be invalid, this should make Vim reset the
2802 " cursor position in the other window.
2803 wincmd p
2804 set cpo+=g
2805 " won't do anything, but try to set the cursor on an invalid lnum
2806 autocmd BufReadPre <buffer> :norm! 70gg
2807 " triggers BufReadPre, should not move the cursor in either window
2808 e
2809 call assert_equal(1, winsaveview().topline)
2810 call assert_equal(1, winsaveview().lnum)
2811 call assert_equal(3, b:bufreadpre)
2812 wincmd p
2813 call assert_equal(g:wsv1.topline, winsaveview().topline)
2814 call assert_equal(g:wsv1.lnum, winsaveview().lnum)
2815 call assert_equal(3, b:bufreadpre)
2816 close
2817 close
2818 call delete('XAutocmdBufReadPre.txt')
2819 set cpo-=g
2820endfunc
2821
Bram Moolenaar5e66b422019-01-24 21:58:10 +01002822" FileChangedShell tested in test_filechanged.vim
Bram Moolenaar69ea5872019-04-25 20:29:00 +02002823
2824" Tests for the following autocommands:
2825" - FileWritePre writing a compressed file
2826" - FileReadPost reading a compressed file
2827" - BufNewFile reading a file template
2828" - BufReadPre decompressing the file to be read
2829" - FilterReadPre substituting characters in the temp file
2830" - FilterReadPost substituting characters after filtering
2831" - FileReadPre set options for decompression
2832" - FileReadPost decompress the file
2833func Test_ReadWrite_Autocmds()
2834 " Run this test only on Unix-like systems and if gzip is available
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +02002835 CheckUnix
2836 CheckExecutable gzip
Bram Moolenaar69ea5872019-04-25 20:29:00 +02002837
2838 " Make $GZIP empty, "-v" would cause trouble.
2839 let $GZIP = ""
2840
2841 " Use a FileChangedShell autocommand to avoid a prompt for 'Xtestfile.gz'
2842 " being modified outside of Vim (noticed on Solaris).
2843 au FileChangedShell * echo 'caught FileChangedShell'
2844
2845 " Test for the FileReadPost, FileWritePre and FileWritePost autocmds
2846 augroup Test1
2847 au!
2848 au FileWritePre *.gz '[,']!gzip
2849 au FileWritePost *.gz undo
2850 au FileReadPost *.gz '[,']!gzip -d
2851 augroup END
2852
2853 new
2854 set bin
2855 call append(0, [
2856 \ 'line 2 Abcdefghijklmnopqrstuvwxyz',
2857 \ 'line 3 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2858 \ 'line 4 Abcdefghijklmnopqrstuvwxyz',
2859 \ 'line 5 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2860 \ 'line 6 Abcdefghijklmnopqrstuvwxyz',
2861 \ 'line 7 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2862 \ 'line 8 Abcdefghijklmnopqrstuvwxyz',
2863 \ 'line 9 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2864 \ 'line 10 Abcdefghijklmnopqrstuvwxyz'
2865 \ ])
2866 1,9write! Xtestfile.gz
2867 enew! | close
2868
2869 new
2870 " Read and decompress the testfile
2871 0read Xtestfile.gz
2872 call assert_equal([
2873 \ 'line 2 Abcdefghijklmnopqrstuvwxyz',
2874 \ 'line 3 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2875 \ 'line 4 Abcdefghijklmnopqrstuvwxyz',
2876 \ 'line 5 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2877 \ 'line 6 Abcdefghijklmnopqrstuvwxyz',
2878 \ 'line 7 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2879 \ 'line 8 Abcdefghijklmnopqrstuvwxyz',
2880 \ 'line 9 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2881 \ 'line 10 Abcdefghijklmnopqrstuvwxyz'
2882 \ ], getline(1, 9))
2883 enew! | close
2884
2885 augroup Test1
2886 au!
2887 augroup END
2888
2889 " Test for the FileAppendPre and FileAppendPost autocmds
2890 augroup Test2
2891 au!
2892 au BufNewFile *.c read Xtest.c
2893 au FileAppendPre *.out '[,']s/new/NEW/
2894 au FileAppendPost *.out !cat Xtest.c >> test.out
2895 augroup END
2896
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01002897 call writefile(['/*', ' * Here is a new .c file', ' */'], 'Xtest.c', 'D')
Bram Moolenaar69ea5872019-04-25 20:29:00 +02002898 new foo.c " should load Xtest.c
2899 call assert_equal(['/*', ' * Here is a new .c file', ' */'], getline(2, 4))
2900 w! >> test.out " append it to the output file
2901
2902 let contents = readfile('test.out')
2903 call assert_equal(' * Here is a NEW .c file', contents[2])
2904 call assert_equal(' * Here is a new .c file', contents[5])
2905
2906 call delete('test.out')
2907 enew! | close
2908 augroup Test2
2909 au!
2910 augroup END
2911
2912 " Test for the BufReadPre and BufReadPost autocmds
2913 augroup Test3
2914 au!
2915 " setup autocommands to decompress before reading and re-compress
2916 " afterwards
2917 au BufReadPre *.gz exe '!gzip -d ' . shellescape(expand("<afile>"))
2918 au BufReadPre *.gz call rename(expand("<afile>:r"), expand("<afile>"))
2919 au BufReadPost *.gz call rename(expand("<afile>"), expand("<afile>:r"))
2920 au BufReadPost *.gz exe '!gzip ' . shellescape(expand("<afile>:r"))
2921 augroup END
2922
2923 e! Xtestfile.gz " Edit compressed file
2924 call assert_equal([
2925 \ 'line 2 Abcdefghijklmnopqrstuvwxyz',
2926 \ 'line 3 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2927 \ 'line 4 Abcdefghijklmnopqrstuvwxyz',
2928 \ 'line 5 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2929 \ 'line 6 Abcdefghijklmnopqrstuvwxyz',
2930 \ 'line 7 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2931 \ 'line 8 Abcdefghijklmnopqrstuvwxyz',
2932 \ 'line 9 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2933 \ 'line 10 Abcdefghijklmnopqrstuvwxyz'
2934 \ ], getline(1, 9))
2935
2936 w! >> test.out " Append it to the output file
2937
2938 augroup Test3
2939 au!
2940 augroup END
2941
2942 " Test for the FilterReadPre and FilterReadPost autocmds.
2943 set shelltemp " need temp files here
2944 augroup Test4
2945 au!
2946 au FilterReadPre *.out call rename(expand("<afile>"), expand("<afile>") . ".t")
2947 au FilterReadPre *.out exe 'silent !sed s/e/E/ ' . shellescape(expand("<afile>")) . ".t >" . shellescape(expand("<afile>"))
2948 au FilterReadPre *.out exe 'silent !rm ' . shellescape(expand("<afile>")) . '.t'
2949 au FilterReadPost *.out '[,']s/x/X/g
2950 augroup END
2951
2952 e! test.out " Edit the output file
2953 1,$!cat
2954 call assert_equal([
2955 \ 'linE 2 AbcdefghijklmnopqrstuvwXyz',
2956 \ 'linE 3 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
2957 \ 'linE 4 AbcdefghijklmnopqrstuvwXyz',
2958 \ 'linE 5 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
2959 \ 'linE 6 AbcdefghijklmnopqrstuvwXyz',
2960 \ 'linE 7 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
2961 \ 'linE 8 AbcdefghijklmnopqrstuvwXyz',
2962 \ 'linE 9 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
2963 \ 'linE 10 AbcdefghijklmnopqrstuvwXyz'
2964 \ ], getline(1, 9))
2965 call assert_equal([
2966 \ 'line 2 Abcdefghijklmnopqrstuvwxyz',
2967 \ 'line 3 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2968 \ 'line 4 Abcdefghijklmnopqrstuvwxyz',
2969 \ 'line 5 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2970 \ 'line 6 Abcdefghijklmnopqrstuvwxyz',
2971 \ 'line 7 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2972 \ 'line 8 Abcdefghijklmnopqrstuvwxyz',
2973 \ 'line 9 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2974 \ 'line 10 Abcdefghijklmnopqrstuvwxyz'
2975 \ ], readfile('test.out'))
2976
2977 augroup Test4
2978 au!
2979 augroup END
2980 set shelltemp&vim
2981
2982 " Test for the FileReadPre and FileReadPost autocmds.
2983 augroup Test5
2984 au!
2985 au FileReadPre *.gz exe 'silent !gzip -d ' . shellescape(expand("<afile>"))
2986 au FileReadPre *.gz call rename(expand("<afile>:r"), expand("<afile>"))
2987 au FileReadPost *.gz '[,']s/l/L/
2988 augroup END
2989
2990 new
2991 0r Xtestfile.gz " Read compressed file
2992 call assert_equal([
2993 \ 'Line 2 Abcdefghijklmnopqrstuvwxyz',
2994 \ 'Line 3 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2995 \ 'Line 4 Abcdefghijklmnopqrstuvwxyz',
2996 \ 'Line 5 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2997 \ 'Line 6 Abcdefghijklmnopqrstuvwxyz',
2998 \ 'Line 7 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2999 \ 'Line 8 Abcdefghijklmnopqrstuvwxyz',
3000 \ 'Line 9 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
3001 \ 'Line 10 Abcdefghijklmnopqrstuvwxyz'
3002 \ ], getline(1, 9))
3003 call assert_equal([
3004 \ 'line 2 Abcdefghijklmnopqrstuvwxyz',
3005 \ 'line 3 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
3006 \ 'line 4 Abcdefghijklmnopqrstuvwxyz',
3007 \ 'line 5 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
3008 \ 'line 6 Abcdefghijklmnopqrstuvwxyz',
3009 \ 'line 7 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
3010 \ 'line 8 Abcdefghijklmnopqrstuvwxyz',
3011 \ 'line 9 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
3012 \ 'line 10 Abcdefghijklmnopqrstuvwxyz'
3013 \ ], readfile('Xtestfile.gz'))
3014
3015 augroup Test5
3016 au!
3017 augroup END
3018
3019 au! FileChangedShell
3020 call delete('Xtestfile.gz')
Bram Moolenaar69ea5872019-04-25 20:29:00 +02003021 call delete('test.out')
3022endfunc
Bram Moolenaar23b51392019-05-09 21:38:43 +02003023
3024func Test_throw_in_BufWritePre()
3025 new
3026 call setline(1, ['one', 'two', 'three'])
3027 call assert_false(filereadable('Xthefile'))
3028 augroup throwing
3029 au BufWritePre X* throw 'do not write'
3030 augroup END
3031 try
3032 w Xthefile
3033 catch
3034 let caught = 1
3035 endtry
3036 call assert_equal(1, caught)
3037 call assert_false(filereadable('Xthefile'))
3038
3039 bwipe!
3040 au! throwing
3041endfunc
Bram Moolenaarcadbe1b2019-09-22 21:50:09 +02003042
Bram Moolenaar40fa12a2021-09-22 14:18:13 +02003043func Test_autocmd_in_try_block()
Bram Moolenaar6f14da12022-09-07 21:30:44 +01003044 call mkdir('Xintrydir', 'R')
Bram Moolenaar40fa12a2021-09-22 14:18:13 +02003045 au BufEnter * let g:fname = expand('%')
3046 try
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +01003047 edit Xintrydir/
Bram Moolenaar40fa12a2021-09-22 14:18:13 +02003048 endtry
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +01003049 call assert_match('Xintrydir', g:fname)
Bram Moolenaar40fa12a2021-09-22 14:18:13 +02003050
3051 unlet g:fname
3052 au! BufEnter
Bram Moolenaar40fa12a2021-09-22 14:18:13 +02003053endfunc
3054
Bram Moolenaarcadbe1b2019-09-22 21:50:09 +02003055func Test_autocmd_SafeState()
3056 CheckRunVimInTerminal
3057
3058 let lines =<< trim END
3059 let g:safe = 0
3060 let g:again = ''
3061 au SafeState * let g:safe += 1
3062 au SafeStateAgain * let g:again ..= 'x'
3063 func CallTimer()
3064 call timer_start(10, {id -> execute('let g:again ..= "t"')})
3065 endfunc
3066 END
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01003067 call writefile(lines, 'XSafeState', 'D')
Bram Moolenaarcadbe1b2019-09-22 21:50:09 +02003068 let buf = RunVimInTerminal('-S XSafeState', #{rows: 6})
3069
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01003070 " Sometimes we loop to handle a K_IGNORE, SafeState may be triggered once or
Bram Moolenaar8fb1b472020-02-23 16:16:26 +01003071 " more often.
Bram Moolenaarcadbe1b2019-09-22 21:50:09 +02003072 call term_sendkeys(buf, ":echo g:safe\<CR>")
Bram Moolenaar8fb1b472020-02-23 16:16:26 +01003073 call WaitForAssert({-> assert_match('^\d ', term_getline(buf, 6))}, 1000)
Bram Moolenaarcadbe1b2019-09-22 21:50:09 +02003074
Bram Moolenaar8fb1b472020-02-23 16:16:26 +01003075 " SafeStateAgain should be invoked at least three times
Bram Moolenaarcadbe1b2019-09-22 21:50:09 +02003076 call term_sendkeys(buf, ":echo g:again\<CR>")
Bram Moolenaar8fb1b472020-02-23 16:16:26 +01003077 call WaitForAssert({-> assert_match('^xxx', term_getline(buf, 6))}, 1000)
Bram Moolenaarcadbe1b2019-09-22 21:50:09 +02003078
3079 call term_sendkeys(buf, ":let g:again = ''\<CR>:call CallTimer()\<CR>")
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02003080 call TermWait(buf, 50)
Bram Moolenaar0f6629a2019-09-22 23:24:13 +02003081 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02003082 call TermWait(buf, 50)
Bram Moolenaarcadbe1b2019-09-22 21:50:09 +02003083 call term_sendkeys(buf, ":echo g:again\<CR>")
3084 call WaitForAssert({-> assert_match('xtx', term_getline(buf, 6))}, 1000)
3085
3086 call StopVimInTerminal(buf)
Bram Moolenaarcadbe1b2019-09-22 21:50:09 +02003087endfunc
Bram Moolenaar23324a02019-10-01 17:39:04 +02003088
3089func Test_autocmd_CmdWinEnter()
3090 CheckRunVimInTerminal
Bram Moolenaar21829c52021-01-26 22:42:21 +01003091
Bram Moolenaar23324a02019-10-01 17:39:04 +02003092 let lines =<< trim END
Egor Zvorykin125ffd22021-11-17 14:01:14 +00003093 augroup vimHints | au! | augroup END
Bram Moolenaar23324a02019-10-01 17:39:04 +02003094 let b:dummy_var = 'This is a dummy'
3095 autocmd CmdWinEnter * quit
3096 let winnr = winnr('$')
3097 END
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01003098 let filename = 'XCmdWinEnter'
Bram Moolenaar23324a02019-10-01 17:39:04 +02003099 call writefile(lines, filename)
3100 let buf = RunVimInTerminal('-S '.filename, #{rows: 6})
3101
3102 call term_sendkeys(buf, "q:")
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02003103 call TermWait(buf)
Bram Moolenaar23324a02019-10-01 17:39:04 +02003104 call term_sendkeys(buf, ":echo b:dummy_var\<cr>")
Bram Moolenaar353c3512020-03-15 14:19:26 +01003105 call WaitForAssert({-> assert_match('^This is a dummy', term_getline(buf, 6))}, 2000)
Bram Moolenaar23324a02019-10-01 17:39:04 +02003106 call term_sendkeys(buf, ":echo &buftype\<cr>")
3107 call WaitForAssert({-> assert_notmatch('^nofile', term_getline(buf, 6))}, 1000)
3108 call term_sendkeys(buf, ":echo winnr\<cr>")
3109 call WaitForAssert({-> assert_match('^1', term_getline(buf, 6))}, 1000)
3110
3111 " clean up
3112 call StopVimInTerminal(buf)
3113 call delete(filename)
3114endfunc
Bram Moolenaarec66c412019-10-11 21:19:13 +02003115
3116func Test_autocmd_was_using_freed_memory()
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01003117 CheckFeature quickfix
3118
Bram Moolenaarec66c412019-10-11 21:19:13 +02003119 pedit xx
3120 n x
Bram Moolenaar92bb83e2021-02-03 23:04:46 +01003121 augroup winenter
3122 au WinEnter * if winnr('$') > 2 | quit | endif
3123 augroup END
Bram Moolenaarec66c412019-10-11 21:19:13 +02003124 split
Bram Moolenaar92bb83e2021-02-03 23:04:46 +01003125
3126 augroup winenter
3127 au! WinEnter
3128 augroup END
3129
3130 bwipe xx
3131 bwipe x
3132 pclose
Bram Moolenaarec66c412019-10-11 21:19:13 +02003133endfunc
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +01003134
3135func Test_BufWrite_lockmarks()
Bram Moolenaarf08b0eb2021-10-16 13:00:14 +01003136 let g:test_is_flaky = 1
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +01003137 edit! Xtest
3138 call setline(1, ['a', 'b', 'c', 'd'])
3139
3140 " :lockmarks preserves the marks
3141 call SetChangeMarks(2, 3)
3142 lockmarks write
3143 call assert_equal([2, 3], [line("'["), line("']")])
3144
3145 " *WritePre autocmds get the correct line range, but lockmarks preserves the
3146 " original values for the user
3147 augroup lockmarks
3148 au!
3149 au BufWritePre,FilterWritePre * call assert_equal([1, 4], [line("'["), line("']")])
3150 au FileWritePre * call assert_equal([3, 4], [line("'["), line("']")])
3151 augroup END
3152
3153 lockmarks write
3154 call assert_equal([2, 3], [line("'["), line("']")])
3155
3156 if executable('cat')
3157 lockmarks %!cat
3158 call assert_equal([2, 3], [line("'["), line("']")])
3159 endif
3160
3161 lockmarks 3,4write Xtest2
3162 call assert_equal([2, 3], [line("'["), line("']")])
3163
3164 au! lockmarks
3165 augroup! lockmarks
3166 call delete('Xtest')
3167 call delete('Xtest2')
3168endfunc
Bram Moolenaarce6db022020-01-07 20:11:42 +01003169
3170func Test_FileType_spell()
3171 if !isdirectory('/tmp')
3172 throw "Skipped: requires /tmp directory"
3173 endif
3174
3175 " this was crashing with an invalid free()
3176 setglobal spellfile=/tmp/en.utf-8.add
3177 augroup crash
3178 autocmd!
3179 autocmd BufNewFile,BufReadPost crashfile setf somefiletype
3180 autocmd BufNewFile,BufReadPost crashfile set ft=anotherfiletype
3181 autocmd FileType anotherfiletype setlocal spell
3182 augroup END
3183 func! NoCrash() abort
3184 edit /tmp/crashfile
3185 endfunc
3186 call NoCrash()
3187
3188 au! crash
3189 setglobal spellfile=
3190endfunc
Bram Moolenaarbc2b71d2020-02-17 21:33:30 +01003191
Bram Moolenaaref976322022-09-28 11:48:30 +01003192" this was wiping out the current buffer and using freed memory
3193func Test_SpellFileMissing_bwipe()
3194 next 0
3195 au SpellFileMissing 0 bwipe
3196 call assert_fails('set spell spelllang=0', 'E937:')
3197
3198 au! SpellFileMissing
Bram Moolenaar0a60f792022-11-19 21:18:11 +00003199 set nospell spelllang=en
Bram Moolenaaref976322022-09-28 11:48:30 +01003200 bwipe
3201endfunc
3202
Bram Moolenaar406cd902020-02-18 21:54:41 +01003203" Test closing a window or editing another buffer from a FileChangedRO handler
3204" in a readonly buffer
3205func Test_FileChangedRO_winclose()
Bram Moolenaar62cd26a2020-10-11 20:08:44 +02003206 call test_override('ui_delay', 10)
3207
Bram Moolenaar406cd902020-02-18 21:54:41 +01003208 augroup FileChangedROTest
3209 au!
3210 autocmd FileChangedRO * quit
3211 augroup END
3212 new
3213 set readonly
3214 call assert_fails('normal i', 'E788:')
3215 close
3216 augroup! FileChangedROTest
3217
3218 augroup FileChangedROTest
3219 au!
Bram Moolenaar61abe7d2022-08-30 21:46:08 +01003220 autocmd FileChangedRO * edit Xrofile
Bram Moolenaar406cd902020-02-18 21:54:41 +01003221 augroup END
3222 new
3223 set readonly
3224 call assert_fails('normal i', 'E788:')
3225 close
3226 augroup! FileChangedROTest
Bram Moolenaar62cd26a2020-10-11 20:08:44 +02003227 call test_override('ALL', 0)
Bram Moolenaar406cd902020-02-18 21:54:41 +01003228endfunc
3229
Bram Moolenaar0c81d1b2020-02-22 22:45:55 +01003230func LogACmd()
3231 call add(g:logged, line('$'))
3232endfunc
3233
3234func Test_TermChanged()
Bram Moolenaard28e0b32020-02-22 23:08:52 +01003235 CheckNotGui
3236
Bram Moolenaar0c81d1b2020-02-22 22:45:55 +01003237 enew!
3238 tabnew
3239 call setline(1, ['a', 'b', 'c', 'd'])
3240 $
3241 au TermChanged * call LogACmd()
3242 let g:logged = []
3243 let term_save = &term
3244 set term=xterm
3245 call assert_equal([1, 4], g:logged)
3246
3247 au! TermChanged
3248 let &term = term_save
3249 bwipe!
3250endfunc
3251
Bram Moolenaare3284872020-03-19 13:55:03 +01003252" Test for FileReadCmd autocmd
3253func Test_autocmd_FileReadCmd()
3254 func ReadFileCmd()
3255 call append(line('$'), "v:cmdarg = " .. v:cmdarg)
3256 endfunc
3257 augroup FileReadCmdTest
3258 au!
3259 au FileReadCmd Xtest call ReadFileCmd()
3260 augroup END
3261
3262 new
3263 read ++bin Xtest
3264 read ++nobin Xtest
3265 read ++edit Xtest
3266 read ++bad=keep Xtest
3267 read ++bad=drop Xtest
3268 read ++bad=- Xtest
3269 read ++ff=unix Xtest
3270 read ++ff=dos Xtest
3271 read ++ff=mac Xtest
3272 read ++enc=utf-8 Xtest
3273
3274 call assert_equal(['',
3275 \ 'v:cmdarg = ++bin',
3276 \ 'v:cmdarg = ++nobin',
3277 \ 'v:cmdarg = ++edit',
3278 \ 'v:cmdarg = ++bad=keep',
3279 \ 'v:cmdarg = ++bad=drop',
3280 \ 'v:cmdarg = ++bad=-',
3281 \ 'v:cmdarg = ++ff=unix',
3282 \ 'v:cmdarg = ++ff=dos',
3283 \ 'v:cmdarg = ++ff=mac',
3284 \ 'v:cmdarg = ++enc=utf-8'], getline(1, '$'))
3285
Bram Moolenaar23526d22022-12-05 15:50:41 +00003286 bwipe!
Bram Moolenaare3284872020-03-19 13:55:03 +01003287 augroup FileReadCmdTest
3288 au!
3289 augroup END
3290 delfunc ReadFileCmd
3291endfunc
3292
Bram Moolenaaree4e0c12020-04-06 21:35:05 +02003293" Test for passing invalid arguments to autocmd
3294func Test_autocmd_invalid_args()
3295 " Additional character after * for event
Bram Moolenaar61abe7d2022-08-30 21:46:08 +01003296 call assert_fails('autocmd *a Xinvfile set ff=unix', 'E215:')
Bram Moolenaaree4e0c12020-04-06 21:35:05 +02003297 augroup Test
3298 augroup END
3299 " Invalid autocmd event
Bram Moolenaar61abe7d2022-08-30 21:46:08 +01003300 call assert_fails('autocmd Bufabc Xinvfile set ft=vim', 'E216:')
Bram Moolenaaree4e0c12020-04-06 21:35:05 +02003301 " Invalid autocmd event in a autocmd group
Bram Moolenaar61abe7d2022-08-30 21:46:08 +01003302 call assert_fails('autocmd Test Bufabc Xinvfile set ft=vim', 'E216:')
Bram Moolenaaree4e0c12020-04-06 21:35:05 +02003303 augroup! Test
3304 " Execute all autocmds
3305 call assert_fails('doautocmd * BufEnter', 'E217:')
3306 call assert_fails('augroup! x1a2b3', 'E367:')
3307 call assert_fails('autocmd BufNew <buffer=999> pwd', 'E680:')
Bram Moolenaar531be472020-09-23 22:38:05 +02003308 call assert_fails('autocmd BufNew \) set ff=unix', 'E55:')
Bram Moolenaaree4e0c12020-04-06 21:35:05 +02003309endfunc
3310
3311" Test for deep nesting of autocmds
3312func Test_autocmd_deep_nesting()
Bram Moolenaar61abe7d2022-08-30 21:46:08 +01003313 autocmd BufEnter Xdeepfile doautocmd BufEnter Xdeepfile
3314 call assert_fails('doautocmd BufEnter Xdeepfile', 'E218:')
3315 autocmd! BufEnter Xdeepfile
Bram Moolenaaree4e0c12020-04-06 21:35:05 +02003316endfunc
3317
Bram Moolenaarbe5ee862020-06-10 20:56:58 +02003318" Tests for SigUSR1 autocmd event, which is only available on posix systems.
3319func Test_autocmd_sigusr1()
3320 CheckUnix
Bram Moolenaar0056ca72022-09-23 21:26:39 +01003321 " FIXME: should this work on MacOS M1?
3322 CheckNotMacM1
Bram Moolenaar62cd26a2020-10-11 20:08:44 +02003323 CheckExecutable /bin/kill
Bram Moolenaarbe5ee862020-06-10 20:56:58 +02003324
3325 let g:sigusr1_passed = 0
3326 au SigUSR1 * let g:sigusr1_passed = 1
3327 call system('/bin/kill -s usr1 ' . getpid())
3328 call WaitForAssert({-> assert_true(g:sigusr1_passed)})
3329
3330 au! SigUSR1
3331 unlet g:sigusr1_passed
3332endfunc
3333
Bram Moolenaarb340bae2020-06-15 19:51:56 +02003334" Test for BufReadPre autocmd deleting the file
3335func Test_BufReadPre_delfile()
3336 augroup TestAuCmd
3337 au!
Bram Moolenaare7cda972022-08-29 11:02:59 +01003338 autocmd BufReadPre XbufreadPre call delete('XbufreadPre')
Bram Moolenaarb340bae2020-06-15 19:51:56 +02003339 augroup END
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01003340 call writefile([], 'XbufreadPre', 'D')
Bram Moolenaare7cda972022-08-29 11:02:59 +01003341 call assert_fails('new XbufreadPre', 'E200:')
3342 call assert_equal('XbufreadPre', @%)
Bram Moolenaarb340bae2020-06-15 19:51:56 +02003343 call assert_equal(1, &readonly)
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01003344
Bram Moolenaarb340bae2020-06-15 19:51:56 +02003345 augroup TestAuCmd
3346 au!
3347 augroup END
3348 close!
3349endfunc
3350
3351" Test for BufReadPre autocmd changing the current buffer
3352func Test_BufReadPre_changebuf()
3353 augroup TestAuCmd
3354 au!
Bram Moolenaare7cda972022-08-29 11:02:59 +01003355 autocmd BufReadPre Xchangebuf edit Xsomeotherfile
Bram Moolenaarb340bae2020-06-15 19:51:56 +02003356 augroup END
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01003357 call writefile([], 'Xchangebuf', 'D')
Bram Moolenaare7cda972022-08-29 11:02:59 +01003358 call assert_fails('new Xchangebuf', 'E201:')
Bram Moolenaarb340bae2020-06-15 19:51:56 +02003359 call assert_equal('Xsomeotherfile', @%)
3360 call assert_equal(1, &readonly)
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01003361
Bram Moolenaarb340bae2020-06-15 19:51:56 +02003362 augroup TestAuCmd
3363 au!
3364 augroup END
3365 close!
3366endfunc
3367
3368" Test for BufWipeouti autocmd changing the current buffer when reading a file
3369" in an empty buffer with 'f' flag in 'cpo'
3370func Test_BufDelete_changebuf()
3371 new
3372 augroup TestAuCmd
3373 au!
3374 autocmd BufWipeout * let bufnr = bufadd('somefile') | exe "b " .. bufnr
3375 augroup END
3376 let save_cpo = &cpo
3377 set cpo+=f
Bram Moolenaarb18b4962022-09-02 21:55:50 +01003378 call assert_fails('r Xchangebuf', ['E812:', 'E484:'])
Bram Moolenaarb340bae2020-06-15 19:51:56 +02003379 call assert_equal('somefile', @%)
3380 let &cpo = save_cpo
3381 augroup TestAuCmd
3382 au!
3383 augroup END
3384 close!
3385endfunc
3386
Bram Moolenaar0fe937f2020-06-16 22:42:04 +02003387" Test for the temporary internal window used to execute autocmds
3388func Test_autocmd_window()
3389 %bw!
3390 edit one.txt
3391 tabnew two.txt
Bram Moolenaar41cd8032021-03-13 15:47:56 +01003392 vnew three.txt
3393 tabnew four.txt
3394 tabprevious
Bram Moolenaar0fe937f2020-06-16 22:42:04 +02003395 let g:blist = []
Bram Moolenaar832adf92020-06-25 19:01:36 +02003396 augroup aucmd_win_test1
Bram Moolenaar0fe937f2020-06-16 22:42:04 +02003397 au!
3398 au BufEnter * call add(g:blist, [expand('<afile>'),
3399 \ win_gettype(bufwinnr(expand('<afile>')))])
3400 augroup END
3401
3402 doautoall BufEnter
Bram Moolenaar41cd8032021-03-13 15:47:56 +01003403 call assert_equal([
3404 \ ['one.txt', 'autocmd'],
3405 \ ['two.txt', ''],
3406 \ ['four.txt', 'autocmd'],
3407 \ ['three.txt', ''],
3408 \ ], g:blist)
Bram Moolenaar0fe937f2020-06-16 22:42:04 +02003409
Bram Moolenaar832adf92020-06-25 19:01:36 +02003410 augroup aucmd_win_test1
Bram Moolenaar0fe937f2020-06-16 22:42:04 +02003411 au!
3412 augroup END
Bram Moolenaar832adf92020-06-25 19:01:36 +02003413 augroup! aucmd_win_test1
3414 %bw!
3415endfunc
3416
3417" Test for trying to close the temporary window used for executing an autocmd
3418func Test_close_autocmd_window()
3419 %bw!
3420 edit one.txt
3421 tabnew two.txt
3422 augroup aucmd_win_test2
3423 au!
3424 au BufEnter * if expand('<afile>') == 'one.txt' | 1close | endif
3425 augroup END
3426
3427 call assert_fails('doautoall BufEnter', 'E813:')
3428
3429 augroup aucmd_win_test2
3430 au!
3431 augroup END
3432 augroup! aucmd_win_test2
Bram Moolenaarcf844172020-06-26 19:44:06 +02003433 %bwipe!
3434endfunc
3435
3436" Test for trying to close the tab that has the temporary window for exeucing
3437" an autocmd.
3438func Test_close_autocmd_tab()
3439 edit one.txt
3440 tabnew two.txt
3441 augroup aucmd_win_test
3442 au!
3443 au BufEnter * if expand('<afile>') == 'one.txt' | tabfirst | tabonly | endif
3444 augroup END
3445
3446 call assert_fails('doautoall BufEnter', 'E813:')
3447
3448 tabonly
3449 augroup aucmd_win_test
3450 au!
3451 augroup END
3452 augroup! aucmd_win_test
3453 %bwipe!
Bram Moolenaar0fe937f2020-06-16 22:42:04 +02003454endfunc
3455
Bram Moolenaarcb1956d2022-01-07 15:45:18 +00003456func Test_Visual_doautoall_redraw()
3457 call setline(1, ['a', 'b'])
Bram Moolenaar94722c52023-01-28 19:19:03 +00003458 new
Bram Moolenaarcb1956d2022-01-07 15:45:18 +00003459 wincmd p
3460 call feedkeys("G\<C-V>", 'txn')
3461 autocmd User Explode ++once redraw
3462 doautoall User Explode
3463 %bwipe!
3464endfunc
3465
Bram Moolenaar6bcb8772021-02-03 21:23:29 +01003466" This was using freed memory.
3467func Test_BufNew_arglocal()
3468 arglocal
3469 au BufNew * arglocal
3470 call assert_fails('drop xx', 'E1156:')
3471
3472 au! BufNew
3473endfunc
3474
Bram Moolenaar8ab37572021-02-03 21:56:59 +01003475func Test_autocmd_closes_window()
3476 au BufNew,BufWinLeave * e %e
3477 file yyy
3478 au BufNew,BufWinLeave * ball
Bram Moolenaaraad5f9d2021-02-06 17:30:31 +01003479 n xxx
Bram Moolenaar8ab37572021-02-03 21:56:59 +01003480
Bram Moolenaaraad5f9d2021-02-06 17:30:31 +01003481 %bwipe
Bram Moolenaar8ab37572021-02-03 21:56:59 +01003482 au! BufNew
3483 au! BufWinLeave
3484endfunc
3485
Bram Moolenaar92bb83e2021-02-03 23:04:46 +01003486func Test_autocmd_quit_psearch()
3487 sn aa bb
3488 augroup aucmd_win_test
3489 au!
3490 au BufEnter,BufLeave,BufNew,WinEnter,WinLeave,WinNew * if winnr('$') > 1 | q | endif
3491 augroup END
3492 ps /
3493
3494 augroup aucmd_win_test
3495 au!
3496 augroup END
zeertzjq7851c692022-04-21 11:14:01 +01003497 new
3498 pclose
Bram Moolenaar92bb83e2021-02-03 23:04:46 +01003499endfunc
3500
Bram Moolenaaraad5f9d2021-02-06 17:30:31 +01003501" Fuzzer found some strange combination that caused a crash.
3502func Test_autocmd_normal_mess()
Bram Moolenaardd07c022021-02-07 13:32:46 +01003503 " For unknown reason this hangs on MS-Windows
3504 CheckNotMSWindows
3505
Bram Moolenaaraad5f9d2021-02-06 17:30:31 +01003506 augroup aucmd_normal_test
3507 au BufLeave,BufWinLeave,BufHidden,BufUnload,BufDelete,BufWipeout * norm 7q/qc
3508 augroup END
Bram Moolenaar983d83f2021-02-07 12:12:43 +01003509 call assert_fails('o4', 'E1159')
Bram Moolenaaraad5f9d2021-02-06 17:30:31 +01003510 silent! H
Bram Moolenaar983d83f2021-02-07 12:12:43 +01003511 call assert_fails('e xx', 'E1159')
Bram Moolenaaraad5f9d2021-02-06 17:30:31 +01003512 normal G
3513
3514 augroup aucmd_normal_test
3515 au!
3516 augroup END
3517endfunc
3518
Bram Moolenaar8c6951f2021-02-06 18:08:45 +01003519func Test_autocmd_closing_cmdwin()
Bram Moolenaardd07c022021-02-07 13:32:46 +01003520 " For unknown reason this hangs on MS-Windows
3521 CheckNotMSWindows
3522
Bram Moolenaar8c6951f2021-02-06 18:08:45 +01003523 au BufWinLeave * nested q
3524 call assert_fails("norm 7q?\n", 'E855:')
3525
3526 au! BufWinLeave
3527 new
3528 only
3529endfunc
3530
Bram Moolenaar2c7080b2021-02-06 19:19:42 +01003531func Test_autocmd_vimgrep()
3532 augroup aucmd_vimgrep
Charlie Grovesfef44852022-04-19 16:24:12 +01003533 au QuickfixCmdPre,BufNew,BufReadCmd * sb
zeertzjq7851c692022-04-21 11:14:01 +01003534 au QuickfixCmdPre,BufNew,BufReadCmd * q9
Bram Moolenaar2c7080b2021-02-06 19:19:42 +01003535 augroup END
Bram Moolenaardd07c022021-02-07 13:32:46 +01003536 call assert_fails('lv ?a? foo', 'E926:')
Bram Moolenaar2c7080b2021-02-06 19:19:42 +01003537
3538 augroup aucmd_vimgrep
3539 au!
3540 augroup END
3541endfunc
3542
Bram Moolenaar73b8b0a2021-08-01 14:52:32 +02003543func Test_autocmd_with_block()
3544 augroup block_testing
3545 au BufReadPost *.xml {
3546 setlocal matchpairs+=<:>
3547 /<start
3548 }
Bram Moolenaar63b91732021-08-05 20:40:03 +02003549 au CursorHold * {
3550 autocmd BufReadPre * ++once echo 'one' | echo 'two'
3551 g:gotSafeState = 77
3552 }
Bram Moolenaar73b8b0a2021-08-01 14:52:32 +02003553 augroup END
3554
3555 let expected = "\n--- Autocommands ---\nblock_testing BufRead\n *.xml {^@ setlocal matchpairs+=<:>^@ /<start^@ }"
3556 call assert_equal(expected, execute('au BufReadPost *.xml'))
3557
Bram Moolenaar63b91732021-08-05 20:40:03 +02003558 doautocmd CursorHold
3559 call assert_equal(77, g:gotSafeState)
3560 unlet g:gotSafeState
3561
Bram Moolenaar73b8b0a2021-08-01 14:52:32 +02003562 augroup block_testing
3563 au!
Bram Moolenaar75ebd2a2022-06-03 17:39:46 +01003564 autocmd CursorHold * {
3565 if true
3566 # comment
3567 && true
3568
3569 && true
3570 g:done = 'yes'
3571 endif
3572 }
3573 augroup END
3574 doautocmd CursorHold
3575 call assert_equal('yes', g:done)
3576
3577 unlet g:done
3578 augroup block_testing
3579 au!
Bram Moolenaar73b8b0a2021-08-01 14:52:32 +02003580 augroup END
3581endfunc
3582
Christian Brabandtdb3b4462021-10-16 11:58:55 +01003583" Test TextChangedI and TextChanged
3584func Test_Changed_ChangedI()
3585 new
3586 call test_override("char_avail", 1)
3587 let [g:autocmd_i, g:autocmd_n] = ['','']
3588
3589 func! TextChangedAutocmdI(char)
3590 let g:autocmd_{tolower(a:char)} = a:char .. b:changedtick
3591 endfunc
3592
3593 augroup Test_TextChanged
3594 au!
3595 au TextChanged <buffer> :call TextChangedAutocmdI('N')
3596 au TextChangedI <buffer> :call TextChangedAutocmdI('I')
3597 augroup END
3598
3599 call feedkeys("ifoo\<esc>", 'tnix')
3600 " TODO: Test test does not seem to trigger TextChanged autocommand, this
3601 " requires running Vim in a terminal window.
3602 " call assert_equal('N3', g:autocmd_n)
3603 call assert_equal('I3', g:autocmd_i)
3604
3605 call feedkeys("yyp", 'tnix')
3606 " TODO: Test test does not seem to trigger TextChanged autocommand.
3607 " call assert_equal('N4', g:autocmd_n)
3608 call assert_equal('I3', g:autocmd_i)
3609
3610 " CleanUp
3611 call test_override("char_avail", 0)
3612 au! TextChanged <buffer>
3613 au! TextChangedI <buffer>
3614 augroup! Test_TextChanged
3615 delfu TextChangedAutocmdI
3616 unlet! g:autocmd_i g:autocmd_n
3617
3618 bw!
3619endfunc
Bram Moolenaar2c7080b2021-02-06 19:19:42 +01003620
Bram Moolenaar6f2465d2022-03-22 18:13:01 +00003621func Test_closing_autocmd_window()
3622 let lines =<< trim END
3623 edit Xa.txt
3624 tabnew Xb.txt
3625 autocmd BufEnter Xa.txt unhide 1
3626 doautoall BufEnter
3627 END
3628 call v9.CheckScriptFailure(lines, 'E814:')
3629 au! BufEnter
3630 only!
3631 bwipe Xa.txt
3632 bwipe Xb.txt
3633endfunc
3634
Bram Moolenaar347538f2022-03-26 16:28:06 +00003635func Test_bufwipeout_changes_window()
3636 " This should not crash, but we don't have any expectations about what
3637 " happens, changing window in BufWipeout has unpredictable results.
3638 tabedit
3639 let g:window_id = win_getid()
3640 topleft new
3641 setlocal bufhidden=wipe
3642 autocmd BufWipeout <buffer> call win_gotoid(g:window_id)
3643 tabprevious
3644 +tabclose
3645
3646 unlet g:window_id
3647 au! BufWipeout
3648 %bwipe!
3649endfunc
3650
zeertzjq021996f2022-04-10 11:44:04 +01003651func Test_v_event_readonly()
3652 autocmd CompleteChanged * let v:event.width = 0
3653 call assert_fails("normal! i\<C-X>\<C-V>", 'E46:')
3654 au! CompleteChanged
3655
3656 autocmd DirChangedPre * let v:event.directory = ''
3657 call assert_fails('cd .', 'E46:')
3658 au! DirChangedPre
3659
3660 autocmd ModeChanged * let v:event.new_mode = ''
3661 call assert_fails('normal! cc', 'E46:')
3662 au! ModeChanged
3663
3664 autocmd TextYankPost * let v:event.operator = ''
3665 call assert_fails('normal! yy', 'E46:')
3666 au! TextYankPost
3667endfunc
3668
zeertzjqc9e8fd62022-07-26 18:12:38 +01003669" Test for ModeChanged pattern
3670func Test_mode_changes()
3671 let g:index = 0
3672 let g:mode_seq = ['n', 'i', 'n', 'v', 'V', 'i', 'ix', 'i', 'ic', 'i', 'n', 'no', 'n', 'V', 'v', 's', 'n']
3673 func! TestMode()
3674 call assert_equal(g:mode_seq[g:index], get(v:event, "old_mode"))
3675 call assert_equal(g:mode_seq[g:index + 1], get(v:event, "new_mode"))
3676 call assert_equal(mode(1), get(v:event, "new_mode"))
3677 let g:index += 1
3678 endfunc
3679
3680 au ModeChanged * :call TestMode()
3681 let g:n_to_any = 0
3682 au ModeChanged n:* let g:n_to_any += 1
3683 call feedkeys("i\<esc>vVca\<CR>\<C-X>\<C-L>\<esc>ggdG", 'tnix')
3684
3685 let g:V_to_v = 0
3686 au ModeChanged V:v let g:V_to_v += 1
3687 call feedkeys("Vv\<C-G>\<esc>", 'tnix')
3688 call assert_equal(len(filter(g:mode_seq[1:], {idx, val -> val == 'n'})), g:n_to_any)
3689 call assert_equal(1, g:V_to_v)
3690 call assert_equal(len(g:mode_seq) - 1, g:index)
3691
3692 let g:n_to_i = 0
3693 au ModeChanged n:i let g:n_to_i += 1
3694 let g:n_to_niI = 0
3695 au ModeChanged i:niI let g:n_to_niI += 1
3696 let g:niI_to_i = 0
3697 au ModeChanged niI:i let g:niI_to_i += 1
3698 let g:nany_to_i = 0
3699 au ModeChanged n*:i let g:nany_to_i += 1
3700 let g:i_to_n = 0
3701 au ModeChanged i:n let g:i_to_n += 1
3702 let g:nori_to_any = 0
3703 au ModeChanged [ni]:* let g:nori_to_any += 1
3704 let g:i_to_any = 0
3705 au ModeChanged i:* let g:i_to_any += 1
3706 let g:index = 0
3707 let g:mode_seq = ['n', 'i', 'niI', 'i', 'n']
3708 call feedkeys("a\<C-O>l\<esc>", 'tnix')
3709 call assert_equal(len(g:mode_seq) - 1, g:index)
3710 call assert_equal(1, g:n_to_i)
3711 call assert_equal(1, g:n_to_niI)
3712 call assert_equal(1, g:niI_to_i)
3713 call assert_equal(2, g:nany_to_i)
3714 call assert_equal(1, g:i_to_n)
3715 call assert_equal(2, g:i_to_any)
3716 call assert_equal(3, g:nori_to_any)
3717
3718 if has('terminal')
3719 let g:mode_seq += ['c', 'n', 't', 'nt', 'c', 'nt', 'n']
3720 call feedkeys(":term\<CR>\<C-W>N:bd!\<CR>", 'tnix')
3721 call assert_equal(len(g:mode_seq) - 1, g:index)
3722 call assert_equal(1, g:n_to_i)
3723 call assert_equal(1, g:n_to_niI)
3724 call assert_equal(1, g:niI_to_i)
3725 call assert_equal(2, g:nany_to_i)
3726 call assert_equal(1, g:i_to_n)
3727 call assert_equal(2, g:i_to_any)
3728 call assert_equal(5, g:nori_to_any)
3729 endif
3730
zeertzjqd1955982022-10-05 11:24:46 +01003731 let g:n_to_c = 0
3732 au ModeChanged n:c let g:n_to_c += 1
3733 let g:c_to_n = 0
3734 au ModeChanged c:n let g:c_to_n += 1
3735 let g:mode_seq += ['c', 'n', 'c', 'n']
3736 call feedkeys("q:\<C-C>\<Esc>", 'tnix')
3737 call assert_equal(len(g:mode_seq) - 1, g:index)
3738 call assert_equal(2, g:n_to_c)
3739 call assert_equal(2, g:c_to_n)
3740 unlet g:n_to_c
3741 unlet g:c_to_n
zeertzjqc9e8fd62022-07-26 18:12:38 +01003742
Bram Moolenaar61c4b042022-10-18 15:10:11 +01003743 let g:n_to_v = 0
3744 au ModeChanged n:v let g:n_to_v += 1
3745 let g:v_to_n = 0
3746 au ModeChanged v:n let g:v_to_n += 1
3747 let g:mode_seq += ['v', 'n']
3748 call feedkeys("v\<C-C>", 'tnix')
3749 call assert_equal(len(g:mode_seq) - 1, g:index)
3750 call assert_equal(1, g:n_to_v)
3751 call assert_equal(1, g:v_to_n)
3752 unlet g:n_to_v
3753 unlet g:v_to_n
3754
zeertzjqc9e8fd62022-07-26 18:12:38 +01003755 au! ModeChanged
3756 delfunc TestMode
3757 unlet! g:mode_seq
3758 unlet! g:index
3759 unlet! g:n_to_any
3760 unlet! g:V_to_v
3761 unlet! g:n_to_i
3762 unlet! g:n_to_niI
3763 unlet! g:niI_to_i
3764 unlet! g:nany_to_i
3765 unlet! g:i_to_n
3766 unlet! g:nori_to_any
3767 unlet! g:i_to_any
3768endfunc
3769
3770func Test_recursive_ModeChanged()
3771 au! ModeChanged * norm 0u
3772 sil! norm 
3773 au! ModeChanged
3774endfunc
3775
3776func Test_ModeChanged_starts_visual()
3777 " This was triggering ModeChanged before setting VIsual, causing a crash.
3778 au! ModeChanged * norm 0u
3779 sil! norm 
3780
3781 au! ModeChanged
3782endfunc
Bram Moolenaar347538f2022-03-26 16:28:06 +00003783
Charlie Grovesfef44852022-04-19 16:24:12 +01003784func Test_noname_autocmd()
3785 augroup test_noname_autocmd_group
3786 autocmd!
3787 autocmd BufEnter * call add(s:li, ["BufEnter", expand("<afile>")])
3788 autocmd BufDelete * call add(s:li, ["BufDelete", expand("<afile>")])
3789 autocmd BufLeave * call add(s:li, ["BufLeave", expand("<afile>")])
3790 autocmd BufUnload * call add(s:li, ["BufUnload", expand("<afile>")])
3791 autocmd BufWipeout * call add(s:li, ["BufWipeout", expand("<afile>")])
3792 augroup END
3793
3794 let s:li = []
3795 edit foo
3796 call assert_equal([['BufUnload', ''], ['BufDelete', ''], ['BufWipeout', ''], ['BufEnter', 'foo']], s:li)
3797
3798 au! test_noname_autocmd_group
3799 augroup! test_noname_autocmd_group
3800endfunc
3801
Yegappan Lakshmanan1755a912022-05-19 10:31:47 +01003802" Test for the autocmd_get() function
3803func Test_autocmd_get()
3804 augroup TestAutoCmdFns
3805 au!
3806 autocmd BufAdd *.vim echo "bufadd-vim"
3807 autocmd BufAdd *.py echo "bufadd-py"
3808 autocmd BufHidden *.vim echo "bufhidden"
3809 augroup END
3810 augroup TestAutoCmdFns2
3811 autocmd BufAdd *.vim echo "bufadd-vim-2"
3812 autocmd BufRead *.a1b2c3 echo "bufadd-vim-2"
3813 augroup END
3814
3815 let l = autocmd_get()
3816 call assert_true(l->len() > 0)
3817
3818 " Test for getting all the autocmds in a group
3819 let expected = [
3820 \ #{cmd: 'echo "bufadd-vim"', group: 'TestAutoCmdFns',
3821 \ pattern: '*.vim', nested: v:false, once: v:false,
3822 \ event: 'BufAdd'},
3823 \ #{cmd: 'echo "bufadd-py"', group: 'TestAutoCmdFns',
3824 \ pattern: '*.py', nested: v:false, once: v:false,
3825 \ event: 'BufAdd'},
3826 \ #{cmd: 'echo "bufhidden"', group: 'TestAutoCmdFns',
3827 \ pattern: '*.vim', nested: v:false,
3828 \ once: v:false, event: 'BufHidden'}]
3829 call assert_equal(expected, autocmd_get(#{group: 'TestAutoCmdFns'}))
3830
3831 " Test for getting autocmds for all the patterns in a group
3832 call assert_equal(expected, autocmd_get(#{group: 'TestAutoCmdFns',
3833 \ event: '*'}))
3834
3835 " Test for getting autocmds for an event in a group
3836 let expected = [
3837 \ #{cmd: 'echo "bufadd-vim"', group: 'TestAutoCmdFns',
3838 \ pattern: '*.vim', nested: v:false, once: v:false,
3839 \ event: 'BufAdd'},
3840 \ #{cmd: 'echo "bufadd-py"', group: 'TestAutoCmdFns',
3841 \ pattern: '*.py', nested: v:false, once: v:false,
3842 \ event: 'BufAdd'}]
3843 call assert_equal(expected, autocmd_get(#{group: 'TestAutoCmdFns',
3844 \ event: 'BufAdd'}))
3845
3846 " Test for getting the autocmds for all the events in a group for particular
3847 " pattern
3848 call assert_equal([{'cmd': 'echo "bufadd-py"', 'group': 'TestAutoCmdFns',
3849 \ 'pattern': '*.py', 'nested': v:false, 'once': v:false,
3850 \ 'event': 'BufAdd'}],
3851 \ autocmd_get(#{group: 'TestAutoCmdFns', event: '*', pattern: '*.py'}))
3852
3853 " Test for getting the autocmds for an events in a group for particular
3854 " pattern
3855 let l = autocmd_get(#{group: 'TestAutoCmdFns', event: 'BufAdd',
3856 \ pattern: '*.vim'})
3857 call assert_equal([
3858 \ #{cmd: 'echo "bufadd-vim"', group: 'TestAutoCmdFns',
3859 \ pattern: '*.vim', nested: v:false, once: v:false,
3860 \ event: 'BufAdd'}], l)
3861
3862 " Test for getting the autocmds for a pattern in a group
3863 let l = autocmd_get(#{group: 'TestAutoCmdFns', pattern: '*.vim'})
3864 call assert_equal([
3865 \ #{cmd: 'echo "bufadd-vim"', group: 'TestAutoCmdFns',
3866 \ pattern: '*.vim', nested: v:false, once: v:false,
3867 \ event: 'BufAdd'},
3868 \ #{cmd: 'echo "bufhidden"', group: 'TestAutoCmdFns',
3869 \ pattern: '*.vim', nested: v:false,
3870 \ once: v:false, event: 'BufHidden'}], l)
3871
3872 " Test for getting the autocmds for a pattern in all the groups
3873 let l = autocmd_get(#{pattern: '*.a1b2c3'})
3874 call assert_equal([{'cmd': 'echo "bufadd-vim-2"', 'group': 'TestAutoCmdFns2',
3875 \ 'pattern': '*.a1b2c3', 'nested': v:false, 'once': v:false,
3876 \ 'event': 'BufRead'}], l)
3877
3878 " Test for getting autocmds for a pattern without any autocmds
3879 call assert_equal([], autocmd_get(#{group: 'TestAutoCmdFns',
3880 \ pattern: '*.abc'}))
3881 call assert_equal([], autocmd_get(#{group: 'TestAutoCmdFns',
3882 \ event: 'BufAdd', pattern: '*.abc'}))
3883 call assert_equal([], autocmd_get(#{group: 'TestAutoCmdFns',
3884 \ event: 'BufWipeout'}))
3885 call assert_fails("call autocmd_get(#{group: 'abc', event: 'BufAdd'})",
3886 \ 'E367:')
3887 let cmd = "echo autocmd_get(#{group: 'TestAutoCmdFns', event: 'abc'})"
3888 call assert_fails(cmd, 'E216:')
3889 call assert_fails("call autocmd_get(#{group: 'abc'})", 'E367:')
3890 call assert_fails("echo autocmd_get(#{event: 'abc'})", 'E216:')
3891
3892 augroup TestAutoCmdFns
3893 au!
3894 augroup END
3895 call assert_equal([], autocmd_get(#{group: 'TestAutoCmdFns'}))
3896
3897 " Test for nested and once autocmds
3898 augroup TestAutoCmdFns
3899 au!
3900 autocmd VimSuspend * ++nested echo "suspend"
3901 autocmd VimResume * ++once echo "resume"
3902 augroup END
3903
3904 let expected = [
3905 \ {'cmd': 'echo "suspend"', 'group': 'TestAutoCmdFns', 'pattern': '*',
3906 \ 'nested': v:true, 'once': v:false, 'event': 'VimSuspend'},
3907 \ {'cmd': 'echo "resume"', 'group': 'TestAutoCmdFns', 'pattern': '*',
3908 \ 'nested': v:false, 'once': v:true, 'event': 'VimResume'}]
3909 call assert_equal(expected, autocmd_get(#{group: 'TestAutoCmdFns'}))
3910
3911 " Test for buffer-local autocmd
3912 augroup TestAutoCmdFns
3913 au!
3914 autocmd TextYankPost <buffer> echo "textyankpost"
3915 augroup END
3916
3917 let expected = [
3918 \ {'cmd': 'echo "textyankpost"', 'group': 'TestAutoCmdFns',
3919 \ 'pattern': '<buffer=' .. bufnr() .. '>', 'nested': v:false,
3920 \ 'once': v:false, 'bufnr': bufnr(), 'event': 'TextYankPost'}]
3921 call assert_equal(expected, autocmd_get(#{group: 'TestAutoCmdFns'}))
3922
3923 augroup TestAutoCmdFns
3924 au!
3925 augroup END
3926 augroup! TestAutoCmdFns
3927 augroup TestAutoCmdFns2
3928 au!
3929 augroup END
3930 augroup! TestAutoCmdFns2
3931
3932 call assert_fails("echo autocmd_get(#{group: []})", 'E730:')
3933 call assert_fails("echo autocmd_get(#{event: {}})", 'E731:')
3934 call assert_fails("echo autocmd_get([])", 'E1206:')
3935endfunc
3936
3937" Test for the autocmd_add() function
3938func Test_autocmd_add()
3939 " Define a single autocmd in a group
3940 call autocmd_add([#{group: 'TestAcSet', event: 'BufAdd', pattern: '*.sh',
3941 \ cmd: 'echo "bufadd"', once: v:true, nested: v:true}])
3942 call assert_equal([#{cmd: 'echo "bufadd"', group: 'TestAcSet',
3943 \ pattern: '*.sh', nested: v:true, once: v:true,
3944 \ event: 'BufAdd'}], autocmd_get(#{group: 'TestAcSet'}))
3945
3946 " Define two autocmds in the same group
3947 call autocmd_delete([#{group: 'TestAcSet'}])
3948 call autocmd_add([#{group: 'TestAcSet', event: 'BufAdd', pattern: '*.sh',
3949 \ cmd: 'echo "bufadd"'},
3950 \ #{group: 'TestAcSet', event: 'BufEnter', pattern: '*.sh',
3951 \ cmd: 'echo "bufenter"'}])
3952 call assert_equal([
3953 \ #{cmd: 'echo "bufadd"', group: 'TestAcSet', pattern: '*.sh',
3954 \ nested: v:false, once: v:false, event: 'BufAdd'},
3955 \ #{cmd: 'echo "bufenter"', group: 'TestAcSet', pattern: '*.sh',
3956 \ nested: v:false, once: v:false, event: 'BufEnter'}],
3957 \ autocmd_get(#{group: 'TestAcSet'}))
3958
3959 " Define a buffer-local autocmd
3960 call autocmd_delete([#{group: 'TestAcSet'}])
3961 call autocmd_add([#{group: 'TestAcSet', event: 'CursorHold',
3962 \ bufnr: bufnr(), cmd: 'echo "cursorhold"'}])
3963 call assert_equal([
3964 \ #{cmd: 'echo "cursorhold"', group: 'TestAcSet',
3965 \ pattern: '<buffer=' .. bufnr() .. '>', nested: v:false,
3966 \ once: v:false, bufnr: bufnr(), event: 'CursorHold'}],
3967 \ autocmd_get(#{group: 'TestAcSet'}))
3968
3969 " Use an invalid buffer number
3970 call autocmd_delete([#{group: 'TestAcSet'}])
3971 call autocmd_add([#{group: 'TestAcSet', event: 'BufEnter',
3972 \ bufnr: -1, cmd: 'echo "bufenter"'}])
3973 let l = [#{group: 'TestAcSet', event: 'BufAdd', bufnr: 9999,
3974 \ cmd: 'echo "bufadd"'}]
3975 call assert_fails("echo autocmd_add(l)", 'E680:')
Yegappan Lakshmanan00e977c2022-06-01 12:31:53 +01003976 let l = [#{group: 'TestAcSet', event: 'BufAdd', bufnr: 9999,
3977 \ pattern: '*.py', cmd: 'echo "bufadd"'}]
3978 call assert_fails("echo autocmd_add(l)", 'E680:')
3979 let l = [#{group: 'TestAcSet', event: 'BufAdd', bufnr: 9999,
3980 \ pattern: ['*.py', '*.c'], cmd: 'echo "bufadd"'}]
3981 call assert_fails("echo autocmd_add(l)", 'E680:')
Yegappan Lakshmanan1755a912022-05-19 10:31:47 +01003982 let l = [#{group: 'TestAcSet', event: 'BufRead', bufnr: [],
3983 \ cmd: 'echo "bufread"'}]
3984 call assert_fails("echo autocmd_add(l)", 'E745:')
3985 call assert_equal([], autocmd_get(#{group: 'TestAcSet'}))
3986
3987 " Add two commands to the same group, event and pattern
3988 call autocmd_delete([#{group: 'TestAcSet'}])
3989 call autocmd_add([#{group: 'TestAcSet', event: 'BufUnload',
3990 \ pattern: 'abc', cmd: 'echo "cmd1"'}])
3991 call autocmd_add([#{group: 'TestAcSet', event: 'BufUnload',
3992 \ pattern: 'abc', cmd: 'echo "cmd2"'}])
3993 call assert_equal([
3994 \ #{cmd: 'echo "cmd1"', group: 'TestAcSet', pattern: 'abc',
3995 \ nested: v:false, once: v:false, event: 'BufUnload'},
3996 \ #{cmd: 'echo "cmd2"', group: 'TestAcSet', pattern: 'abc',
3997 \ nested: v:false, once: v:false, event: 'BufUnload'}],
3998 \ autocmd_get(#{group: 'TestAcSet'}))
3999
4000 " When adding a new autocmd, if the autocmd 'group' is not specified, then
4001 " the current autocmd group should be used.
4002 call autocmd_delete([#{group: 'TestAcSet'}])
4003 augroup TestAcSet
4004 call autocmd_add([#{event: 'BufHidden', pattern: 'abc', cmd: 'echo "abc"'}])
4005 augroup END
4006 call assert_equal([
4007 \ #{cmd: 'echo "abc"', group: 'TestAcSet', pattern: 'abc',
4008 \ nested: v:false, once: v:false, event: 'BufHidden'}],
4009 \ autocmd_get(#{group: 'TestAcSet'}))
4010
Yegappan Lakshmanan971f6822022-05-24 11:40:11 +01004011 " Test for replacing a cmd for an event in a group
4012 call autocmd_delete([#{group: 'TestAcSet'}])
4013 call autocmd_add([#{replace: v:true, group: 'TestAcSet', event: 'BufEnter',
4014 \ pattern: '*.py', cmd: 'echo "bufenter"'}])
4015 call autocmd_add([#{replace: v:true, group: 'TestAcSet', event: 'BufEnter',
4016 \ pattern: '*.py', cmd: 'echo "bufenter"'}])
4017 call assert_equal([
4018 \ #{cmd: 'echo "bufenter"', group: 'TestAcSet', pattern: '*.py',
4019 \ nested: v:false, once: v:false, event: 'BufEnter'}],
4020 \ autocmd_get(#{group: 'TestAcSet'}))
4021
4022 " Test for adding a command for an unsupported autocmd event
Yegappan Lakshmanan1755a912022-05-19 10:31:47 +01004023 let l = [#{group: 'TestAcSet', event: 'abc', pattern: '*.sh',
4024 \ cmd: 'echo "bufadd"'}]
4025 call assert_fails('call autocmd_add(l)', 'E216:')
4026
Yegappan Lakshmanane0ff3a72022-05-27 18:05:33 +01004027 " Test for using a list of events and patterns
4028 call autocmd_delete([#{group: 'TestAcSet'}])
4029 let l = [#{group: 'TestAcSet', event: ['BufEnter', 'BufLeave'],
4030 \ pattern: ['*.py', '*.sh'], cmd: 'echo "bufcmds"'}]
4031 call autocmd_add(l)
4032 call assert_equal([
4033 \ #{cmd: 'echo "bufcmds"', group: 'TestAcSet', pattern: '*.py',
4034 \ nested: v:false, once: v:false, event: 'BufEnter'},
4035 \ #{cmd: 'echo "bufcmds"', group: 'TestAcSet', pattern: '*.sh',
4036 \ nested: v:false, once: v:false, event: 'BufEnter'},
4037 \ #{cmd: 'echo "bufcmds"', group: 'TestAcSet', pattern: '*.py',
4038 \ nested: v:false, once: v:false, event: 'BufLeave'},
4039 \ #{cmd: 'echo "bufcmds"', group: 'TestAcSet', pattern: '*.sh',
4040 \ nested: v:false, once: v:false, event: 'BufLeave'}],
4041 \ autocmd_get(#{group: 'TestAcSet'}))
4042
4043 " Test for invalid values for 'event' item
4044 call autocmd_delete([#{group: 'TestAcSet'}])
4045 let l = [#{group: 'TestAcSet', event: test_null_string(),
4046 \ pattern: "*.py", cmd: 'echo "bufcmds"'}]
4047 call assert_fails('call autocmd_add(l)', 'E928:')
4048 let l = [#{group: 'TestAcSet', event: test_null_list(),
4049 \ pattern: "*.py", cmd: 'echo "bufcmds"'}]
4050 call assert_fails('call autocmd_add(l)', 'E714:')
4051 let l = [#{group: 'TestAcSet', event: {},
4052 \ pattern: "*.py", cmd: 'echo "bufcmds"'}]
4053 call assert_fails('call autocmd_add(l)', 'E777:')
4054 let l = [#{group: 'TestAcSet', event: [{}],
4055 \ pattern: "*.py", cmd: 'echo "bufcmds"'}]
4056 call assert_fails('call autocmd_add(l)', 'E928:')
4057 let l = [#{group: 'TestAcSet', event: [test_null_string()],
4058 \ pattern: "*.py", cmd: 'echo "bufcmds"'}]
4059 call assert_fails('call autocmd_add(l)', 'E928:')
4060 let l = [#{group: 'TestAcSet', event: 'BufEnter,BufLeave',
4061 \ pattern: '*.py', cmd: 'echo "bufcmds"'}]
4062 call assert_fails('call autocmd_add(l)', 'E216:')
4063 let l = [#{group: 'TestAcSet', event: [],
4064 \ pattern: "*.py", cmd: 'echo "bufcmds"'}]
4065 call autocmd_add(l)
4066 let l = [#{group: 'TestAcSet', event: [""],
4067 \ pattern: "*.py", cmd: 'echo "bufcmds"'}]
4068 call assert_fails('call autocmd_add(l)', 'E216:')
4069 let l = [#{group: 'TestAcSet', event: "",
4070 \ pattern: "*.py", cmd: 'echo "bufcmds"'}]
4071 call autocmd_add(l)
4072 call assert_equal([], autocmd_get(#{group: 'TestAcSet'}))
4073
4074 " Test for invalid values for 'pattern' item
4075 let l = [#{group: 'TestAcSet', event: "BufEnter",
4076 \ pattern: test_null_string(), cmd: 'echo "bufcmds"'}]
Yegappan Lakshmanan00e977c2022-06-01 12:31:53 +01004077 call assert_fails('call autocmd_add(l)', 'E928:')
Yegappan Lakshmanane0ff3a72022-05-27 18:05:33 +01004078 let l = [#{group: 'TestAcSet', event: "BufEnter",
4079 \ pattern: test_null_list(), cmd: 'echo "bufcmds"'}]
4080 call assert_fails('call autocmd_add(l)', 'E714:')
4081 let l = [#{group: 'TestAcSet', event: "BufEnter",
4082 \ pattern: {}, cmd: 'echo "bufcmds"'}]
4083 call assert_fails('call autocmd_add(l)', 'E777:')
4084 let l = [#{group: 'TestAcSet', event: "BufEnter",
4085 \ pattern: [{}], cmd: 'echo "bufcmds"'}]
4086 call assert_fails('call autocmd_add(l)', 'E928:')
4087 let l = [#{group: 'TestAcSet', event: "BufEnter",
4088 \ pattern: [test_null_string()], cmd: 'echo "bufcmds"'}]
4089 call assert_fails('call autocmd_add(l)', 'E928:')
4090 let l = [#{group: 'TestAcSet', event: "BufEnter",
4091 \ pattern: [], cmd: 'echo "bufcmds"'}]
4092 call autocmd_add(l)
4093 let l = [#{group: 'TestAcSet', event: "BufEnter",
4094 \ pattern: [""], cmd: 'echo "bufcmds"'}]
4095 call autocmd_add(l)
4096 let l = [#{group: 'TestAcSet', event: "BufEnter",
4097 \ pattern: "", cmd: 'echo "bufcmds"'}]
4098 call autocmd_add(l)
4099 call assert_equal([], autocmd_get(#{group: 'TestAcSet'}))
4100
4101 let l = [#{group: 'TestAcSet', event: 'BufEnter,abc,BufLeave',
4102 \ pattern: '*.py', cmd: 'echo "bufcmds"'}]
4103 call assert_fails('call autocmd_add(l)', 'E216:')
4104
Yegappan Lakshmanan1755a912022-05-19 10:31:47 +01004105 call assert_fails("call autocmd_add({})", 'E1211:')
4106 call assert_equal(v:false, autocmd_add(test_null_list()))
4107 call assert_true(autocmd_add([[]]))
4108 call assert_true(autocmd_add([test_null_dict()]))
4109
4110 augroup TestAcSet
4111 au!
4112 augroup END
4113
4114 call autocmd_add([#{group: 'TestAcSet'}])
4115 call autocmd_add([#{group: 'TestAcSet', event: 'BufAdd'}])
4116 call autocmd_add([#{group: 'TestAcSet', pat: '*.sh'}])
4117 call autocmd_add([#{group: 'TestAcSet', cmd: 'echo "a"'}])
4118 call autocmd_add([#{group: 'TestAcSet', event: 'BufAdd', pat: '*.sh'}])
4119 call autocmd_add([#{group: 'TestAcSet', event: 'BufAdd', cmd: 'echo "a"'}])
4120 call autocmd_add([#{group: 'TestAcSet', pat: '*.sh', cmd: 'echo "a"'}])
4121 call assert_equal([], autocmd_get(#{group: 'TestAcSet'}))
4122
4123 augroup! TestAcSet
4124endfunc
4125
4126" Test for deleting autocmd events and groups
4127func Test_autocmd_delete()
4128 " Delete an event in an autocmd group
4129 augroup TestAcSet
4130 au!
4131 au BufAdd *.sh echo "bufadd"
4132 au BufEnter *.sh echo "bufenter"
4133 augroup END
4134 call autocmd_delete([#{group: 'TestAcSet', event: 'BufAdd'}])
4135 call assert_equal([#{cmd: 'echo "bufenter"', group: 'TestAcSet',
4136 \ pattern: '*.sh', nested: v:false, once: v:false,
4137 \ event: 'BufEnter'}], autocmd_get(#{group: 'TestAcSet'}))
4138
4139 " Delete all the events in an autocmd group
4140 augroup TestAcSet
4141 au BufAdd *.sh echo "bufadd"
4142 augroup END
4143 call autocmd_delete([#{group: 'TestAcSet', event: '*'}])
4144 call assert_equal([], autocmd_get(#{group: 'TestAcSet'}))
4145
4146 " Delete a non-existing autocmd group
4147 call assert_fails("call autocmd_delete([#{group: 'abc'}])", 'E367:')
4148 " Delete a non-existing autocmd event
4149 let l = [#{group: 'TestAcSet', event: 'abc'}]
4150 call assert_fails("call autocmd_delete(l)", 'E216:')
4151 " Delete a non-existing autocmd pattern
4152 let l = [#{group: 'TestAcSet', event: 'BufAdd', pat: 'abc'}]
4153 call assert_true(autocmd_delete(l))
Yegappan Lakshmanan00e977c2022-06-01 12:31:53 +01004154 " Delete an autocmd for a non-existing buffer
4155 let l = [#{event: '*', bufnr: 9999, cmd: 'echo "x"'}]
4156 call assert_fails('call autocmd_delete(l)', 'E680:')
Yegappan Lakshmanan1755a912022-05-19 10:31:47 +01004157
4158 " Delete an autocmd group
4159 augroup TestAcSet
4160 au!
4161 au BufAdd *.sh echo "bufadd"
4162 au BufEnter *.sh echo "bufenter"
4163 augroup END
4164 call autocmd_delete([#{group: 'TestAcSet'}])
4165 call assert_fails("call autocmd_get(#{group: 'TestAcSet'})", 'E367:')
4166
4167 call assert_true(autocmd_delete([[]]))
4168 call assert_true(autocmd_delete([test_null_dict()]))
4169endfunc
4170
Bram Moolenaar8f3c3c62022-10-18 17:05:54 +01004171func Test_autocmd_split_dummy()
4172 " Autocommand trying to split a window containing a dummy buffer.
Bram Moolenaar94722c52023-01-28 19:19:03 +00004173 auto BufReadPre * exe "sbuf " .. expand("<abuf>")
Bram Moolenaar8f3c3c62022-10-18 17:05:54 +01004174 " Avoid the "W11" prompt
4175 au FileChangedShell * let v:fcs_choice = 'reload'
4176 func Xautocmd_changelist()
4177 cal writefile(['Xtestfile2:4:4'], 'Xerr')
4178 edit Xerr
4179 lex 'Xtestfile2:4:4'
4180 endfunc
4181 call Xautocmd_changelist()
Bram Moolenaar53c5c9f2022-10-18 17:25:03 +01004182 " Should get E86, but it doesn't always happen (timing?)
4183 silent! call Xautocmd_changelist()
Bram Moolenaar8f3c3c62022-10-18 17:05:54 +01004184
4185 au! BufReadPre
4186 au! FileChangedShell
4187 delfunc Xautocmd_changelist
4188 bwipe! Xerr
4189 call delete('Xerr')
4190endfunc
4191
Bram Moolenaare76062c2022-11-28 18:51:43 +00004192" This was crashing because there was only one window to execute autocommands
4193" in.
4194func Test_autocmd_nested_setbufvar()
4195 CheckFeature python3
4196
4197 set hidden
4198 edit Xaaa
4199 edit Xbbb
4200 call setline(1, 'bar')
4201 enew
4202 au BufWriteCmd Xbbb ++nested call setbufvar('Xaaa', '&ft', 'foo') | bw! Xaaa
4203 au FileType foo call py3eval('vim.current.buffer.options["cindent"]')
4204 wall
4205
4206 au! BufWriteCmd
4207 au! FileType foo
4208 set nohidden
4209 call delete('Xaaa')
4210 call delete('Xbbb')
4211 %bwipe!
4212endfunc
4213
Christian Brabandt9aee8ec2022-12-16 16:41:23 +00004214func SetupVimTest_shm()
4215 let g:bwe = []
4216 let g:brp = []
4217 set shortmess+=F
4218
4219 let dirname='XVimTestSHM'
4220 call mkdir(dirname, 'R')
4221 call writefile(['test'], dirname .. '/1')
4222 call writefile(['test'], dirname .. '/2')
4223 call writefile(['test'], dirname .. '/3')
4224
4225 augroup test
4226 autocmd!
4227 autocmd BufWinEnter * call add(g:bwe, $'BufWinEnter: {expand('<amatch>')}')
4228 autocmd BufReadPost * call add(g:brp, $'BufReadPost: {expand('<amatch>')}')
4229 augroup END
4230
4231 call setqflist([
4232 \ {'filename': dirname .. '/1', 'lnum': 1, 'col': 1, 'text': 'test', 'vcol': 0},
4233 \ {'filename': dirname .. '/2', 'lnum': 1, 'col': 1, 'text': 'test', 'vcol': 0},
4234 \ {'filename': dirname .. '/3', 'lnum': 1, 'col': 1, 'text': 'test', 'vcol': 0}
4235 \ ])
4236 cdo! substitute/test/TEST
4237
4238 " clean up
4239 noa enew!
4240 set shortmess&vim
4241 augroup test
4242 autocmd!
4243 augroup END
4244 augroup! test
4245endfunc
4246
4247func Test_autocmd_shortmess()
4248 CheckNotMSWindows
4249
4250 call SetupVimTest_shm()
4251 let output = execute(':mess')->split('\n')
4252
4253 let info = copy(output)->filter({idx, val -> val =~# '\d of 3'} )
4254 let bytes = copy(output)->filter({idx, val -> val =~# 'bytes'} )
4255
4256 " We test the following here:
4257 " BufReadPost should have been triggered 3 times, once per file
4258 " BufWinEnter should have been triggered 3 times, once per file
4259 " FileInfoMessage should have been shown 3 times, regardless of shm option
4260 " "(x of 3)" message from :cnext has been shown 3 times
4261
4262 call assert_equal(3, g:brp->len())
4263 call assert_equal(3, g:bwe->len())
4264 call assert_equal(3, info->len())
4265 call assert_equal(3, bytes->len())
4266
4267 delfunc SetupVimTest_shm
4268endfunc
Bram Moolenaare76062c2022-11-28 18:51:43 +00004269
Bram Moolenaarbc2b71d2020-02-17 21:33:30 +01004270" vim: shiftwidth=2 sts=2 expandtab