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