blob: 8103382fbf19cc1fb60bdee43c57d5b1a55283f3 [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 Moolenaarfafcf0d2017-10-19 18:35:51 +02001911 au! CmdlineEnter : let g:entered = expand('<afile>')
1912 au! CmdlineLeave : let g:left = expand('<afile>')
1913 let g:entered = 0
1914 let g:left = 0
1915 call feedkeys(":echo 'hello'\<CR>", 'xt')
1916 call assert_equal(':', g:entered)
1917 call assert_equal(':', g:left)
1918 au! CmdlineEnter
1919 au! CmdlineLeave
1920
Bram Moolenaara4baf5b2018-04-22 13:27:44 +02001921 let save_shellslash = &shellslash
1922 set noshellslash
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02001923 au! CmdlineEnter / let g:entered = expand('<afile>')
1924 au! CmdlineLeave / let g:left = expand('<afile>')
1925 let g:entered = 0
1926 let g:left = 0
Bram Moolenaar53f0c962017-10-22 14:23:59 +02001927 new
1928 call setline(1, 'hello')
1929 call feedkeys("/hello\<CR>", 'xt')
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02001930 call assert_equal('/', g:entered)
1931 call assert_equal('/', g:left)
Bram Moolenaar53f0c962017-10-22 14:23:59 +02001932 bwipe!
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02001933 au! CmdlineEnter
1934 au! CmdlineLeave
Bram Moolenaara4baf5b2018-04-22 13:27:44 +02001935 let &shellslash = save_shellslash
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02001936endfunc
Bram Moolenaar53f0c962017-10-22 14:23:59 +02001937
1938" Test for BufWritePre autocommand that deletes or unloads the buffer.
1939func Test_BufWritePre()
1940 %bwipe
1941 au BufWritePre Xxx1 bunload
1942 au BufWritePre Xxx2 bwipe
1943
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01001944 call writefile(['start of Xxx1', 'test', 'end of Xxx1'], 'Xxx1', 'D')
1945 call writefile(['start of Xxx2', 'test', 'end of Xxx2'], 'Xxx2', 'D')
Bram Moolenaar53f0c962017-10-22 14:23:59 +02001946
1947 edit Xtest
1948 e! Xxx2
1949 bdel Xtest
1950 e Xxx1
1951 " write it, will unload it and give an error msg
Bram Moolenaare2e40752020-09-04 21:18:46 +02001952 call assert_fails('w', 'E203:')
Bram Moolenaar53f0c962017-10-22 14:23:59 +02001953 call assert_equal('Xxx2', bufname('%'))
1954 edit Xtest
1955 e! Xxx2
1956 bwipe Xtest
1957 " write it, will delete the buffer and give an error msg
Bram Moolenaare2e40752020-09-04 21:18:46 +02001958 call assert_fails('w', 'E203:')
Bram Moolenaar53f0c962017-10-22 14:23:59 +02001959 call assert_equal('Xxx1', bufname('%'))
1960 au! BufWritePre
Bram Moolenaar53f0c962017-10-22 14:23:59 +02001961endfunc
1962
1963" Test for BufUnload autocommand that unloads all the other buffers
1964func Test_bufunload_all()
Bram Moolenaarf08b0eb2021-10-16 13:00:14 +01001965 let g:test_is_flaky = 1
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01001966 call writefile(['Test file Xxx1'], 'Xxx1', 'D')"
1967 call writefile(['Test file Xxx2'], 'Xxx2', 'D')"
Bram Moolenaar53f0c962017-10-22 14:23:59 +02001968
Bram Moolenaarc79745a2019-05-20 22:12:34 +02001969 let content =<< trim [CODE]
1970 func UnloadAllBufs()
1971 let i = 1
1972 while i <= bufnr('$')
1973 if i != bufnr('%') && bufloaded(i)
1974 exe i . 'bunload'
1975 endif
1976 let i += 1
1977 endwhile
1978 endfunc
1979 au BufUnload * call UnloadAllBufs()
1980 au VimLeave * call writefile(['Test Finished'], 'Xout')
1981 edit Xxx1
1982 split Xxx2
1983 q
1984 [CODE]
1985
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01001986 call writefile(content, 'Xbunloadtest', 'D')
Bram Moolenaar53f0c962017-10-22 14:23:59 +02001987
1988 call delete('Xout')
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01001989 call system(GetVimCommandClean() .. ' -N --not-a-term -S Xbunloadtest')
Bram Moolenaar53f0c962017-10-22 14:23:59 +02001990 call assert_true(filereadable('Xout'))
1991
Bram Moolenaar53f0c962017-10-22 14:23:59 +02001992 call delete('Xout')
1993endfunc
1994
1995" Some tests for buffer-local autocommands
1996func Test_buflocal_autocmd()
1997 let g:bname = ''
1998 edit xx
1999 au BufLeave <buffer> let g:bname = expand("%")
2000 " here, autocommand for xx should trigger.
2001 " but autocommand shall not apply to buffer named <buffer>.
2002 edit somefile
2003 call assert_equal('xx', g:bname)
2004 let g:bname = ''
2005 " here, autocommand shall be auto-deleted
2006 bwipe xx
2007 " autocmd should not trigger
2008 edit xx
2009 call assert_equal('', g:bname)
2010 " autocmd should not trigger
2011 edit somefile
2012 call assert_equal('', g:bname)
2013 enew
2014 unlet g:bname
2015endfunc
Bram Moolenaar430dc5d2017-11-02 21:04:47 +01002016
2017" Test for "*Cmd" autocommands
2018func Test_Cmd_Autocmds()
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01002019 call writefile(['start of Xxx', "\tabc2", 'end of Xxx'], 'Xxx', 'D')
Bram Moolenaar430dc5d2017-11-02 21:04:47 +01002020
2021 enew!
2022 au BufReadCmd XtestA 0r Xxx|$del
2023 edit XtestA " will read text of Xxd instead
2024 call assert_equal('start of Xxx', getline(1))
2025
2026 au BufWriteCmd XtestA call append(line("$"), "write")
2027 write " will append a line to the file
2028 call assert_equal('write', getline('$'))
Bram Moolenaare2e40752020-09-04 21:18:46 +02002029 call assert_fails('read XtestA', 'E484:') " should not read anything
Bram Moolenaar430dc5d2017-11-02 21:04:47 +01002030 call assert_equal('write', getline(4))
2031
2032 " now we have:
2033 " 1 start of Xxx
2034 " 2 abc2
2035 " 3 end of Xxx
2036 " 4 write
2037
2038 au FileReadCmd XtestB '[r Xxx
2039 2r XtestB " will read Xxx below line 2 instead
2040 call assert_equal('start of Xxx', getline(3))
2041
2042 " now we have:
2043 " 1 start of Xxx
2044 " 2 abc2
2045 " 3 start of Xxx
2046 " 4 abc2
2047 " 5 end of Xxx
2048 " 6 end of Xxx
2049 " 7 write
2050
2051 au FileWriteCmd XtestC '[,']copy $
2052 normal 4GA1
2053 4,5w XtestC " will copy lines 4 and 5 to the end
2054 call assert_equal("\tabc21", getline(8))
Bram Moolenaare2e40752020-09-04 21:18:46 +02002055 call assert_fails('r XtestC', 'E484:') " should not read anything
Bram Moolenaar430dc5d2017-11-02 21:04:47 +01002056 call assert_equal("end of Xxx", getline(9))
2057
2058 " now we have:
2059 " 1 start of Xxx
2060 " 2 abc2
2061 " 3 start of Xxx
2062 " 4 abc21
2063 " 5 end of Xxx
2064 " 6 end of Xxx
2065 " 7 write
2066 " 8 abc21
2067 " 9 end of Xxx
2068
2069 let g:lines = []
2070 au FileAppendCmd XtestD call extend(g:lines, getline(line("'["), line("']")))
2071 w >>XtestD " will add lines to 'lines'
2072 call assert_equal(9, len(g:lines))
Bram Moolenaare2e40752020-09-04 21:18:46 +02002073 call assert_fails('$r XtestD', 'E484:') " should not read anything
Bram Moolenaar430dc5d2017-11-02 21:04:47 +01002074 call assert_equal(9, line('$'))
2075 call assert_equal('end of Xxx', getline('$'))
2076
2077 au BufReadCmd XtestE 0r Xxx|$del
2078 sp XtestE " split window with test.out
2079 call assert_equal('end of Xxx', getline(3))
2080
2081 let g:lines = []
2082 exe "normal 2Goasdf\<Esc>\<C-W>\<C-W>"
2083 au BufWriteCmd XtestE call extend(g:lines, getline(0, '$'))
2084 wall " will write other window to 'lines'
2085 call assert_equal(4, len(g:lines), g:lines)
2086 call assert_equal('asdf', g:lines[2])
2087
2088 au! BufReadCmd
2089 au! BufWriteCmd
2090 au! FileReadCmd
2091 au! FileWriteCmd
2092 au! FileAppendCmd
2093 %bwipe!
Bram Moolenaar430dc5d2017-11-02 21:04:47 +01002094 enew!
2095endfunc
Bram Moolenaaraace2152017-11-05 16:23:10 +01002096
Bram Moolenaar0fff4412020-03-29 16:06:29 +02002097func s:ReadFile()
2098 setl noswapfile nomodified
2099 let filename = resolve(expand("<afile>:p"))
2100 execute 'read' fnameescape(filename)
2101 1d_
2102 exe 'file' fnameescape(filename)
2103 setl buftype=acwrite
2104endfunc
2105
2106func s:WriteFile()
2107 let filename = resolve(expand("<afile>:p"))
2108 setl buftype=
2109 noautocmd execute 'write' fnameescape(filename)
2110 setl buftype=acwrite
2111 setl nomodified
2112endfunc
2113
2114func Test_BufReadCmd()
2115 autocmd BufReadCmd *.test call s:ReadFile()
2116 autocmd BufWriteCmd *.test call s:WriteFile()
2117
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01002118 call writefile(['one', 'two', 'three'], 'Xcmd.test', 'D')
Bram Moolenaar0fff4412020-03-29 16:06:29 +02002119 edit Xcmd.test
2120 call assert_match('Xcmd.test" line 1 of 3', execute('file'))
2121 normal! Gofour
2122 write
2123 call assert_equal(['one', 'two', 'three', 'four'], readfile('Xcmd.test'))
2124
2125 bwipe!
Bram Moolenaar0fff4412020-03-29 16:06:29 +02002126 au! BufReadCmd
2127 au! BufWriteCmd
2128endfunc
2129
zeertzjq9c8f9462022-08-30 18:17:15 +01002130func Test_BufWriteCmd()
2131 autocmd BufWriteCmd Xbufwritecmd let g:written = 1
2132 new
2133 file Xbufwritecmd
2134 set buftype=acwrite
Bram Moolenaar6f14da12022-09-07 21:30:44 +01002135 call mkdir('Xbufwritecmd', 'D')
zeertzjq9c8f9462022-08-30 18:17:15 +01002136 write
2137 " BufWriteCmd should be triggered even if a directory has the same name
2138 call assert_equal(1, g:written)
zeertzjq9c8f9462022-08-30 18:17:15 +01002139 unlet g:written
2140 au! BufWriteCmd
2141 bwipe!
2142endfunc
2143
Bram Moolenaaraace2152017-11-05 16:23:10 +01002144func SetChangeMarks(start, end)
Bram Moolenaar97c69432021-01-15 16:45:21 +01002145 exe a:start .. 'mark ['
2146 exe a:end .. 'mark ]'
Bram Moolenaaraace2152017-11-05 16:23:10 +01002147endfunc
2148
2149" Verify the effects of autocmds on '[ and ']
2150func Test_change_mark_in_autocmds()
2151 edit! Xtest
Bram Moolenaar97c69432021-01-15 16:45:21 +01002152 call feedkeys("ia\<CR>b\<CR>c\<CR>d\<C-g>u\<Esc>", 'xtn')
Bram Moolenaaraace2152017-11-05 16:23:10 +01002153
2154 call SetChangeMarks(2, 3)
2155 write
2156 call assert_equal([1, 4], [line("'["), line("']")])
2157
2158 call SetChangeMarks(2, 3)
2159 au BufWritePre * call assert_equal([1, 4], [line("'["), line("']")])
2160 write
2161 au! BufWritePre
2162
Bram Moolenaar14ddd222020-08-05 12:02:40 +02002163 if has('unix')
Bram Moolenaaraace2152017-11-05 16:23:10 +01002164 write XtestFilter
2165 write >> XtestFilter
2166
2167 call SetChangeMarks(2, 3)
2168 " Marks are set to the entire range of the write
2169 au FilterWritePre * call assert_equal([1, 4], [line("'["), line("']")])
2170 " '[ is adjusted to just before the line that will receive the filtered
2171 " data
2172 au FilterReadPre * call assert_equal([4, 4], [line("'["), line("']")])
2173 " The filtered data is read into the buffer, and the source lines are
2174 " still present, so the range is after the source lines
2175 au FilterReadPost * call assert_equal([5, 12], [line("'["), line("']")])
2176 %!cat XtestFilter
2177 " After the filtered data is read, the original lines are deleted
2178 call assert_equal([1, 8], [line("'["), line("']")])
2179 au! FilterWritePre,FilterReadPre,FilterReadPost
2180 undo
2181
2182 call SetChangeMarks(1, 4)
2183 au FilterWritePre * call assert_equal([2, 3], [line("'["), line("']")])
2184 au FilterReadPre * call assert_equal([3, 3], [line("'["), line("']")])
2185 au FilterReadPost * call assert_equal([4, 11], [line("'["), line("']")])
2186 2,3!cat XtestFilter
2187 call assert_equal([2, 9], [line("'["), line("']")])
2188 au! FilterWritePre,FilterReadPre,FilterReadPost
2189 undo
2190
2191 call delete('XtestFilter')
2192 endif
2193
2194 call SetChangeMarks(1, 4)
2195 au FileWritePre * call assert_equal([2, 3], [line("'["), line("']")])
2196 2,3write Xtest2
2197 au! FileWritePre
2198
2199 call SetChangeMarks(2, 3)
2200 au FileAppendPre * call assert_equal([1, 4], [line("'["), line("']")])
2201 write >> Xtest2
2202 au! FileAppendPre
2203
2204 call SetChangeMarks(1, 4)
2205 au FileAppendPre * call assert_equal([2, 3], [line("'["), line("']")])
2206 2,3write >> Xtest2
2207 au! FileAppendPre
2208
2209 call SetChangeMarks(1, 1)
2210 au FileReadPre * call assert_equal([3, 1], [line("'["), line("']")])
2211 au FileReadPost * call assert_equal([4, 11], [line("'["), line("']")])
2212 3read Xtest2
2213 au! FileReadPre,FileReadPost
2214 undo
2215
2216 call SetChangeMarks(4, 4)
2217 " When the line is 0, it's adjusted to 1
2218 au FileReadPre * call assert_equal([1, 4], [line("'["), line("']")])
2219 au FileReadPost * call assert_equal([1, 8], [line("'["), line("']")])
2220 0read Xtest2
2221 au! FileReadPre,FileReadPost
2222 undo
2223
2224 call SetChangeMarks(4, 4)
2225 " When the line is 0, it's adjusted to 1
2226 au FileReadPre * call assert_equal([1, 4], [line("'["), line("']")])
2227 au FileReadPost * call assert_equal([2, 9], [line("'["), line("']")])
2228 1read Xtest2
2229 au! FileReadPre,FileReadPost
2230 undo
2231
2232 bwipe!
2233 call delete('Xtest')
2234 call delete('Xtest2')
2235endfunc
2236
2237func Test_Filter_noshelltemp()
Bram Moolenaaraeb313f2020-11-27 19:13:28 +01002238 CheckExecutable cat
Bram Moolenaaraace2152017-11-05 16:23:10 +01002239
2240 enew!
2241 call setline(1, ['a', 'b', 'c', 'd'])
2242
2243 let shelltemp = &shelltemp
2244 set shelltemp
2245
2246 let g:filter_au = 0
2247 au FilterWritePre * let g:filter_au += 1
2248 au FilterReadPre * let g:filter_au += 1
2249 au FilterReadPost * let g:filter_au += 1
2250 %!cat
2251 call assert_equal(3, g:filter_au)
2252
2253 if has('filterpipe')
2254 set noshelltemp
2255
2256 let g:filter_au = 0
2257 au FilterWritePre * let g:filter_au += 1
2258 au FilterReadPre * let g:filter_au += 1
2259 au FilterReadPost * let g:filter_au += 1
2260 %!cat
2261 call assert_equal(0, g:filter_au)
2262 endif
2263
2264 au! FilterWritePre,FilterReadPre,FilterReadPost
2265 let &shelltemp = shelltemp
2266 bwipe!
2267endfunc
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01002268
2269func Test_TextYankPost()
2270 enew!
2271 call setline(1, ['foo'])
2272
2273 let g:event = []
2274 au TextYankPost * let g:event = copy(v:event)
2275
2276 call assert_equal({}, v:event)
2277 call assert_fails('let v:event = {}', 'E46:')
2278 call assert_fails('let v:event.mykey = 0', 'E742:')
2279
2280 norm "ayiw
2281 call assert_equal(
Bram Moolenaara016eeb2022-04-09 11:37:38 +01002282 \ #{regcontents: ['foo'], regname: 'a', operator: 'y',
2283 \ regtype: 'v', visual: v:false, inclusive: v:true},
2284 \ g:event)
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01002285 norm y_
2286 call assert_equal(
Bram Moolenaara016eeb2022-04-09 11:37:38 +01002287 \ #{regcontents: ['foo'], regname: '', operator: 'y', regtype: 'V',
2288 \ visual: v:false, inclusive: v:false},
2289 \ g:event)
Bram Moolenaar37d16732020-06-12 22:09:01 +02002290 norm Vy
2291 call assert_equal(
Bram Moolenaara016eeb2022-04-09 11:37:38 +01002292 \ #{regcontents: ['foo'], regname: '', operator: 'y', regtype: 'V',
2293 \ visual: v:true, inclusive: v:true},
2294 \ g:event)
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01002295 call feedkeys("\<C-V>y", 'x')
2296 call assert_equal(
Bram Moolenaara016eeb2022-04-09 11:37:38 +01002297 \ #{regcontents: ['f'], regname: '', operator: 'y', regtype: "\x161",
2298 \ visual: v:true, inclusive: v:true},
2299 \ g:event)
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01002300 norm "xciwbar
2301 call assert_equal(
Bram Moolenaara016eeb2022-04-09 11:37:38 +01002302 \ #{regcontents: ['foo'], regname: 'x', operator: 'c', regtype: 'v',
2303 \ visual: v:false, inclusive: v:true},
2304 \ g:event)
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01002305 norm "bdiw
2306 call assert_equal(
Bram Moolenaara016eeb2022-04-09 11:37:38 +01002307 \ #{regcontents: ['bar'], regname: 'b', operator: 'd', regtype: 'v',
2308 \ visual: v:false, inclusive: v:true},
2309 \ g:event)
2310
2311 call setline(1, 'foobar')
2312 " exclusive motion
2313 norm $"ay0
2314 call assert_equal(
2315 \ #{regcontents: ['fooba'], regname: 'a', operator: 'y', regtype: 'v',
2316 \ visual: v:false, inclusive: v:false},
2317 \ g:event)
2318 " inclusive motion
2319 norm 0"ay$
2320 call assert_equal(
2321 \ #{regcontents: ['foobar'], regname: 'a', operator: 'y', regtype: 'v',
2322 \ visual: v:false, inclusive: v:true},
2323 \ g:event)
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01002324
2325 call assert_equal({}, v:event)
2326
Bram Moolenaarfccbf062020-11-26 20:34:00 +01002327 if has('clipboard_working') && !has('gui_running')
2328 " Test that when the visual selection is automatically copied to clipboard
2329 " register a TextYankPost is emitted
2330 call setline(1, ['foobar'])
2331
2332 let @* = ''
2333 set clipboard=autoselect
2334 exe "norm! ggviw\<Esc>"
2335 call assert_equal(
Bram Moolenaara016eeb2022-04-09 11:37:38 +01002336 \ #{regcontents: ['foobar'], regname: '*', operator: 'y',
2337 \ regtype: 'v', visual: v:true, inclusive: v:false},
2338 \ g:event)
Bram Moolenaarfccbf062020-11-26 20:34:00 +01002339
2340 let @+ = ''
2341 set clipboard=autoselectplus
2342 exe "norm! ggviw\<Esc>"
2343 call assert_equal(
Bram Moolenaara016eeb2022-04-09 11:37:38 +01002344 \ #{regcontents: ['foobar'], regname: '+', operator: 'y',
2345 \ regtype: 'v', visual: v:true, inclusive: v:false},
2346 \ g:event)
Bram Moolenaarfccbf062020-11-26 20:34:00 +01002347
2348 set clipboard&vim
2349 endif
2350
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01002351 au! TextYankPost
2352 unlet g:event
2353 bwipe!
2354endfunc
Bram Moolenaar9bca8052017-12-18 12:37:55 +01002355
Bram Moolenaar9a046fd2021-01-28 13:47:59 +01002356func Test_autocommand_all_events()
2357 call assert_fails('au * * bwipe', 'E1155:')
2358 call assert_fails('au * x bwipe', 'E1155:')
Bram Moolenaarb6db1462021-12-24 19:24:47 +00002359 call assert_fails('au! * x bwipe', 'E1155:')
Bram Moolenaar4fb921e2017-12-18 15:33:00 +01002360endfunc
Bram Moolenaarb7407d32018-02-03 17:36:27 +01002361
Bram Moolenaarf6246f52022-02-11 16:30:12 +00002362func Test_autocmd_user()
2363 au User MyEvent let s:res = [expand("<afile>"), expand("<amatch>")]
2364 doautocmd User MyEvent
2365 call assert_equal(['MyEvent', 'MyEvent'], s:res)
2366 au! User
2367 unlet s:res
2368endfunc
2369
Bram Moolenaar3b014be2022-11-13 17:53:46 +00002370func Test_autocmd_user_clear_group()
2371 CheckRunVimInTerminal
2372
2373 let lines =<< trim END
2374 autocmd! User
2375 for i in range(1, 999)
2376 exe 'autocmd User ' .. 'Foo' .. i .. ' bar'
2377 endfor
2378 au CmdlineLeave : call timer_start(0, {-> execute('autocmd! User')})
2379 END
2380 call writefile(lines, 'XautoUser', 'D')
2381 let buf = RunVimInTerminal('-S XautoUser', {'rows': 10})
2382
2383 " this was using freed memory
2384 call term_sendkeys(buf, ":autocmd User\<CR>")
2385 call TermWait(buf, 50)
2386 call term_sendkeys(buf, "G")
2387
2388 call StopVimInTerminal(buf)
2389endfunc
2390
Bram Moolenaaref2c3252022-11-25 16:31:51 +00002391func Test_autocmd_CmdlineLeave_unlet()
2392 CheckRunVimInTerminal
2393
2394 let lines =<< trim END
2395 for i in range(1, 999)
2396 exe 'let g:var' .. i '=' i
2397 endfor
2398 au CmdlineLeave : call timer_start(0, {-> execute('unlet g:var990')})
2399 END
2400 call writefile(lines, 'XleaveUnlet', 'D')
2401 let buf = RunVimInTerminal('-S XleaveUnlet', {'rows': 10})
2402
2403 " this was using freed memory
2404 call term_sendkeys(buf, ":let g:\<CR>")
2405 call TermWait(buf, 50)
2406 call term_sendkeys(buf, "G")
2407 call TermWait(buf, 50)
2408 call term_sendkeys(buf, "\<CR>") " for the hit-enter prompt
2409
2410 call StopVimInTerminal(buf)
2411endfunc
2412
Bram Moolenaarb7407d32018-02-03 17:36:27 +01002413function s:Before_test_dirchanged()
2414 augroup test_dirchanged
2415 autocmd!
2416 augroup END
2417 let s:li = []
2418 let s:dir_this = getcwd()
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02002419 let s:dir_foo = s:dir_this . '/Xfoo'
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01002420 call mkdir(s:dir_foo)
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02002421 let s:dir_bar = s:dir_this . '/Xbar'
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01002422 call mkdir(s:dir_bar)
Bram Moolenaarb7407d32018-02-03 17:36:27 +01002423endfunc
2424
2425function s:After_test_dirchanged()
Bram Moolenaar3503d7c2019-11-09 20:10:17 +01002426 call chdir(s:dir_this)
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01002427 call delete(s:dir_foo, 'd')
2428 call delete(s:dir_bar, 'd')
Bram Moolenaarb7407d32018-02-03 17:36:27 +01002429 augroup test_dirchanged
2430 autocmd!
2431 augroup END
2432endfunc
2433
2434function Test_dirchanged_global()
2435 call s:Before_test_dirchanged()
Bram Moolenaarf6246f52022-02-11 16:30:12 +00002436 autocmd test_dirchanged DirChangedPre global call add(s:li, expand("<amatch>") .. " pre cd " .. v:event.directory)
Bram Moolenaarb7407d32018-02-03 17:36:27 +01002437 autocmd test_dirchanged DirChanged global call add(s:li, "cd:")
2438 autocmd test_dirchanged DirChanged global call add(s:li, expand("<afile>"))
Bram Moolenaar3503d7c2019-11-09 20:10:17 +01002439 call chdir(s:dir_foo)
Bram Moolenaarf6246f52022-02-11 16:30:12 +00002440 let expected = ["global pre cd " .. s:dir_foo, "cd:", s:dir_foo]
Bram Moolenaar28e8f732022-02-09 12:58:20 +00002441 call assert_equal(expected, s:li)
Bram Moolenaar3503d7c2019-11-09 20:10:17 +01002442 call chdir(s:dir_foo)
Bram Moolenaar28e8f732022-02-09 12:58:20 +00002443 call assert_equal(expected, s:li)
Bram Moolenaar3503d7c2019-11-09 20:10:17 +01002444 exe 'lcd ' .. fnameescape(s:dir_bar)
Bram Moolenaar28e8f732022-02-09 12:58:20 +00002445 call assert_equal(expected, s:li)
Bram Moolenaard8c9d322022-06-12 11:49:16 +01002446
2447 exe 'cd ' .. s:dir_foo
2448 exe 'cd ' .. s:dir_bar
2449 autocmd! test_dirchanged DirChanged global let g:result = expand("<afile>")
2450 cd -
Bram Moolenaardb77c492022-06-12 23:26:50 +01002451 call assert_equal(s:dir_foo, substitute(g:result, '\\', '/', 'g'))
Bram Moolenaard8c9d322022-06-12 11:49:16 +01002452
Bram Moolenaarb7407d32018-02-03 17:36:27 +01002453 call s:After_test_dirchanged()
2454endfunc
2455
2456function Test_dirchanged_local()
2457 call s:Before_test_dirchanged()
2458 autocmd test_dirchanged DirChanged window call add(s:li, "lcd:")
2459 autocmd test_dirchanged DirChanged window call add(s:li, expand("<afile>"))
Bram Moolenaar3503d7c2019-11-09 20:10:17 +01002460 call chdir(s:dir_foo)
Bram Moolenaarb7407d32018-02-03 17:36:27 +01002461 call assert_equal([], s:li)
Bram Moolenaar3503d7c2019-11-09 20:10:17 +01002462 exe 'lcd ' .. fnameescape(s:dir_bar)
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01002463 call assert_equal(["lcd:", s:dir_bar], s:li)
Bram Moolenaar3503d7c2019-11-09 20:10:17 +01002464 exe 'lcd ' .. fnameescape(s:dir_bar)
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01002465 call assert_equal(["lcd:", s:dir_bar], s:li)
Bram Moolenaarb7407d32018-02-03 17:36:27 +01002466 call s:After_test_dirchanged()
2467endfunc
2468
2469function Test_dirchanged_auto()
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +02002470 CheckOption autochdir
Bram Moolenaarb7407d32018-02-03 17:36:27 +01002471 call s:Before_test_dirchanged()
2472 call test_autochdir()
Bram Moolenaar28e8f732022-02-09 12:58:20 +00002473 autocmd test_dirchanged DirChangedPre auto call add(s:li, "pre cd " .. v:event.directory)
Bram Moolenaarb7407d32018-02-03 17:36:27 +01002474 autocmd test_dirchanged DirChanged auto call add(s:li, "auto:")
2475 autocmd test_dirchanged DirChanged auto call add(s:li, expand("<afile>"))
2476 set acd
Bram Moolenaar3503d7c2019-11-09 20:10:17 +01002477 cd ..
Bram Moolenaarb7407d32018-02-03 17:36:27 +01002478 call assert_equal([], s:li)
Bram Moolenaar61abe7d2022-08-30 21:46:08 +01002479 exe 'edit ' . s:dir_foo . '/Xautofile'
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01002480 call assert_equal(s:dir_foo, getcwd())
Bram Moolenaar28e8f732022-02-09 12:58:20 +00002481 let expected = ["pre cd " .. s:dir_foo, "auto:", s:dir_foo]
2482 call assert_equal(expected, s:li)
Bram Moolenaarb7407d32018-02-03 17:36:27 +01002483 set noacd
2484 bwipe!
2485 call s:After_test_dirchanged()
2486endfunc
Bram Moolenaar5a093432018-02-10 18:15:19 +01002487
2488" Test TextChangedI and TextChangedP
2489func Test_ChangedP()
2490 new
2491 call setline(1, ['foo', 'bar', 'foobar'])
2492 call test_override("char_avail", 1)
2493 set complete=. completeopt=menuone
2494
2495 func! TextChangedAutocmd(char)
2496 let g:autocmd .= a:char
2497 endfunc
2498
Christian Brabandtdb3b4462021-10-16 11:58:55 +01002499 " TextChanged will not be triggered, only check that it isn't.
Bram Moolenaar5a093432018-02-10 18:15:19 +01002500 au! TextChanged <buffer> :call TextChangedAutocmd('N')
2501 au! TextChangedI <buffer> :call TextChangedAutocmd('I')
2502 au! TextChangedP <buffer> :call TextChangedAutocmd('P')
2503
2504 call cursor(3, 1)
2505 let g:autocmd = ''
2506 call feedkeys("o\<esc>", 'tnix')
2507 call assert_equal('I', g:autocmd)
2508
2509 let g:autocmd = ''
2510 call feedkeys("Sf", 'tnix')
2511 call assert_equal('II', g:autocmd)
2512
2513 let g:autocmd = ''
2514 call feedkeys("Sf\<C-N>", 'tnix')
2515 call assert_equal('IIP', g:autocmd)
2516
2517 let g:autocmd = ''
2518 call feedkeys("Sf\<C-N>\<C-N>", 'tnix')
2519 call assert_equal('IIPP', g:autocmd)
2520
2521 let g:autocmd = ''
2522 call feedkeys("Sf\<C-N>\<C-N>\<C-N>", 'tnix')
2523 call assert_equal('IIPPP', g:autocmd)
2524
2525 let g:autocmd = ''
2526 call feedkeys("Sf\<C-N>\<C-N>\<C-N>\<C-N>", 'tnix')
2527 call assert_equal('IIPPPP', g:autocmd)
2528
2529 call assert_equal(['foo', 'bar', 'foobar', 'foo'], getline(1, '$'))
2530 " TODO: how should it handle completeopt=noinsert,noselect?
2531
2532 " CleanUp
2533 call test_override("char_avail", 0)
2534 au! TextChanged
2535 au! TextChangedI
2536 au! TextChangedP
2537 delfu TextChangedAutocmd
2538 unlet! g:autocmd
2539 set complete&vim completeopt&vim
2540
2541 bw!
2542endfunc
Bram Moolenaar8c64a362018-03-23 22:39:31 +01002543
Bram Moolenaar91d2e782018-08-07 19:05:01 +02002544let g:setline_handled = v:false
Bram Moolenaar1e115362019-01-09 23:01:02 +01002545func SetLineOne()
Bram Moolenaar91d2e782018-08-07 19:05:01 +02002546 if !g:setline_handled
2547 call setline(1, "(x)")
2548 let g:setline_handled = v:true
2549 endif
2550endfunc
2551
2552func Test_TextChangedI_with_setline()
2553 new
2554 call test_override('char_avail', 1)
2555 autocmd TextChangedI <buffer> call SetLineOne()
2556 call feedkeys("i(\<CR>\<Esc>", 'tx')
2557 call assert_equal('(', getline(1))
2558 call assert_equal('x)', getline(2))
2559 undo
Bram Moolenaar91d2e782018-08-07 19:05:01 +02002560 call assert_equal('', getline(1))
Bram Moolenaar9fa95062018-08-08 22:08:32 +02002561 call assert_equal('', getline(2))
Bram Moolenaar91d2e782018-08-07 19:05:01 +02002562
Bram Moolenaarca34db32022-01-20 11:17:18 +00002563 call test_override('char_avail', 0)
Bram Moolenaar91d2e782018-08-07 19:05:01 +02002564 bwipe!
2565endfunc
2566
Bram Moolenaar8c64a362018-03-23 22:39:31 +01002567func Test_Changed_FirstTime()
Bram Moolenaar8c5a2782019-08-07 23:07:07 +02002568 CheckFeature terminal
2569 CheckNotGui
Bram Moolenaar3cdcb092020-03-18 19:18:10 +01002570 " Starting a terminal to run Vim is always considered flaky.
Bram Moolenaar30d53e22020-03-18 21:10:44 +01002571 let g:test_is_flaky = 1
Bram Moolenaar8c5a2782019-08-07 23:07:07 +02002572
Bram Moolenaar8c64a362018-03-23 22:39:31 +01002573 " Prepare file for TextChanged event.
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01002574 call writefile([''], 'Xchanged.txt', 'D')
Bram Moolenaar8c64a362018-03-23 22:39:31 +01002575 let buf = term_start([GetVimProg(), '--clean', '-c', 'set noswapfile'], {'term_rows': 3})
2576 call assert_equal('running', term_getstatus(buf))
Bram Moolenaar1834d372018-03-29 17:40:46 +02002577 " Wait for the ruler (in the status line) to be shown.
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01002578 " In ConPTY, there is additional character which is drawn up to the width of
2579 " the screen.
2580 if has('conpty')
2581 call WaitForAssert({-> assert_match('\<All.*$', term_getline(buf, 3))})
2582 else
2583 call WaitForAssert({-> assert_match('\<All$', term_getline(buf, 3))})
2584 endif
Bram Moolenaar8c64a362018-03-23 22:39:31 +01002585 " It's only adding autocmd, so that no event occurs.
2586 call term_sendkeys(buf, ":au! TextChanged <buffer> call writefile(['No'], 'Xchanged.txt')\<cr>")
2587 call term_sendkeys(buf, "\<C-\\>\<C-N>:qa!\<cr>")
Bram Moolenaar50182fa2018-04-28 21:34:40 +02002588 call WaitForAssert({-> assert_equal('finished', term_getstatus(buf))})
Bram Moolenaar8c64a362018-03-23 22:39:31 +01002589 call assert_equal([''], readfile('Xchanged.txt'))
2590
2591 " clean up
Bram Moolenaar8c64a362018-03-23 22:39:31 +01002592 bwipe!
2593endfunc
Bram Moolenaar0566e892019-01-24 19:37:40 +01002594
Bram Moolenaareb93f3f2019-04-04 15:04:56 +02002595func Test_autocmd_nested()
2596 let g:did_nested = 0
2597 augroup Testing
2598 au WinNew * edit somefile
2599 au BufNew * let g:did_nested = 1
2600 augroup END
2601 split
2602 call assert_equal(0, g:did_nested)
2603 close
2604 bwipe! somefile
2605
2606 " old nested argument still works
2607 augroup Testing
2608 au!
2609 au WinNew * nested edit somefile
2610 au BufNew * let g:did_nested = 1
2611 augroup END
2612 split
2613 call assert_equal(1, g:did_nested)
2614 close
2615 bwipe! somefile
2616
2617 " New ++nested argument works
2618 augroup Testing
2619 au!
2620 au WinNew * ++nested edit somefile
2621 au BufNew * let g:did_nested = 1
2622 augroup END
2623 split
2624 call assert_equal(1, g:did_nested)
2625 close
2626 bwipe! somefile
2627
Bram Moolenaarf0775142022-03-04 20:10:38 +00002628 " nested without ++ does not work in Vim9 script
2629 call assert_fails('vim9cmd au WinNew * nested echo fails', 'E1078:')
2630
Bram Moolenaareb93f3f2019-04-04 15:04:56 +02002631 augroup Testing
2632 au!
2633 augroup END
2634
2635 call assert_fails('au WinNew * ++nested ++nested echo bad', 'E983:')
2636 call assert_fails('au WinNew * nested nested echo bad', 'E983:')
2637endfunc
2638
Bram Moolenaar5fa9f232022-07-23 09:06:48 +01002639func Test_autocmd_nested_cursor_invalid()
2640 set laststatus=0
2641 copen
2642 cclose
2643 call setline(1, ['foo', 'bar', 'baz'])
2644 3
2645 augroup nested_inv
2646 autocmd User foo ++nested copen
2647 autocmd BufAdd * let &laststatus = 2 - &laststatus
2648 augroup END
2649 doautocmd User foo
2650
2651 augroup nested_inv
2652 au!
2653 augroup END
2654 set laststatus&
Bram Moolenaarb03950f2022-07-26 13:47:13 +01002655 cclose
Bram Moolenaar5fa9f232022-07-23 09:06:48 +01002656 bwipe!
2657endfunc
2658
Bram Moolenaar3d6ee8b2022-07-27 15:23:35 +01002659func Test_autocmd_nested_keeps_cursor_pos()
2660 enew
2661 call setline(1, 'foo')
2662 autocmd User foo ++nested normal! $a
2663 autocmd InsertLeave * :
2664 doautocmd User foo
2665 call assert_equal([0, 1, 3, 0], getpos('.'))
2666
2667 bwipe!
2668endfunc
2669
Bram Moolenaarb03950f2022-07-26 13:47:13 +01002670func Test_autocmd_nested_switch_window()
2671 " run this in a separate Vim so that SafeState works
2672 CheckRunVimInTerminal
2673
2674 let lines =<< trim END
2675 vim9script
2676 ['()']->writefile('Xautofile')
2677 autocmd VimEnter * ++nested edit Xautofile | split
2678 autocmd BufReadPost * autocmd SafeState * ++once foldclosed('.')
2679 autocmd WinEnter * matchadd('ErrorMsg', 'pat')
2680 END
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01002681 call writefile(lines, 'Xautoscript', 'D')
Bram Moolenaarb03950f2022-07-26 13:47:13 +01002682 let buf = RunVimInTerminal('-S Xautoscript', {'rows': 10})
2683 call VerifyScreenDump(buf, 'Test_autocmd_nested_switch', {})
2684
2685 call StopVimInTerminal(buf)
2686 call delete('Xautofile')
Bram Moolenaarb03950f2022-07-26 13:47:13 +01002687endfunc
2688
Bram Moolenaareb93f3f2019-04-04 15:04:56 +02002689func Test_autocmd_once()
2690 " Without ++once WinNew triggers twice
2691 let g:did_split = 0
2692 augroup Testing
2693 au WinNew * let g:did_split += 1
2694 augroup END
2695 split
2696 split
2697 call assert_equal(2, g:did_split)
2698 call assert_true(exists('#WinNew'))
2699 close
2700 close
2701
2702 " With ++once WinNew triggers once
2703 let g:did_split = 0
2704 augroup Testing
2705 au!
2706 au WinNew * ++once let g:did_split += 1
2707 augroup END
2708 split
2709 split
2710 call assert_equal(1, g:did_split)
2711 call assert_false(exists('#WinNew'))
2712 close
2713 close
2714
2715 call assert_fails('au WinNew * ++once ++once echo bad', 'E983:')
2716endfunc
2717
Bram Moolenaara68e5952019-04-25 22:22:01 +02002718func Test_autocmd_bufreadpre()
2719 new
2720 let b:bufreadpre = 1
Bram Moolenaarab505b12020-03-23 19:28:44 +01002721 call append(0, range(1000))
Bram Moolenaara68e5952019-04-25 22:22:01 +02002722 w! XAutocmdBufReadPre.txt
2723 autocmd BufReadPre <buffer> :let b:bufreadpre += 1
Bram Moolenaarab505b12020-03-23 19:28:44 +01002724 norm! 500gg
Bram Moolenaara68e5952019-04-25 22:22:01 +02002725 sp
Bram Moolenaarab505b12020-03-23 19:28:44 +01002726 norm! 1000gg
Bram Moolenaara68e5952019-04-25 22:22:01 +02002727 wincmd p
2728 let g:wsv1 = winsaveview()
2729 wincmd p
2730 let g:wsv2 = winsaveview()
2731 " triggers BufReadPre, should not move the cursor in either window
2732 " The topline may change one line in a large window.
2733 edit
2734 call assert_inrange(g:wsv2.topline - 1, g:wsv2.topline + 1, winsaveview().topline)
2735 call assert_equal(g:wsv2.lnum, winsaveview().lnum)
2736 call assert_equal(2, b:bufreadpre)
2737 wincmd p
2738 call assert_equal(g:wsv1.topline, winsaveview().topline)
2739 call assert_equal(g:wsv1.lnum, winsaveview().lnum)
2740 call assert_equal(2, b:bufreadpre)
2741 " Now set the cursor position in an BufReadPre autocommand
2742 " (even though the position will be invalid, this should make Vim reset the
2743 " cursor position in the other window.
2744 wincmd p
2745 set cpo+=g
2746 " won't do anything, but try to set the cursor on an invalid lnum
2747 autocmd BufReadPre <buffer> :norm! 70gg
2748 " triggers BufReadPre, should not move the cursor in either window
2749 e
2750 call assert_equal(1, winsaveview().topline)
2751 call assert_equal(1, winsaveview().lnum)
2752 call assert_equal(3, b:bufreadpre)
2753 wincmd p
2754 call assert_equal(g:wsv1.topline, winsaveview().topline)
2755 call assert_equal(g:wsv1.lnum, winsaveview().lnum)
2756 call assert_equal(3, b:bufreadpre)
2757 close
2758 close
2759 call delete('XAutocmdBufReadPre.txt')
2760 set cpo-=g
2761endfunc
2762
Bram Moolenaar5e66b422019-01-24 21:58:10 +01002763" FileChangedShell tested in test_filechanged.vim
Bram Moolenaar69ea5872019-04-25 20:29:00 +02002764
2765" Tests for the following autocommands:
2766" - FileWritePre writing a compressed file
2767" - FileReadPost reading a compressed file
2768" - BufNewFile reading a file template
2769" - BufReadPre decompressing the file to be read
2770" - FilterReadPre substituting characters in the temp file
2771" - FilterReadPost substituting characters after filtering
2772" - FileReadPre set options for decompression
2773" - FileReadPost decompress the file
2774func Test_ReadWrite_Autocmds()
2775 " Run this test only on Unix-like systems and if gzip is available
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +02002776 CheckUnix
2777 CheckExecutable gzip
Bram Moolenaar69ea5872019-04-25 20:29:00 +02002778
2779 " Make $GZIP empty, "-v" would cause trouble.
2780 let $GZIP = ""
2781
2782 " Use a FileChangedShell autocommand to avoid a prompt for 'Xtestfile.gz'
2783 " being modified outside of Vim (noticed on Solaris).
2784 au FileChangedShell * echo 'caught FileChangedShell'
2785
2786 " Test for the FileReadPost, FileWritePre and FileWritePost autocmds
2787 augroup Test1
2788 au!
2789 au FileWritePre *.gz '[,']!gzip
2790 au FileWritePost *.gz undo
2791 au FileReadPost *.gz '[,']!gzip -d
2792 augroup END
2793
2794 new
2795 set bin
2796 call append(0, [
2797 \ 'line 2 Abcdefghijklmnopqrstuvwxyz',
2798 \ 'line 3 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2799 \ 'line 4 Abcdefghijklmnopqrstuvwxyz',
2800 \ 'line 5 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2801 \ 'line 6 Abcdefghijklmnopqrstuvwxyz',
2802 \ 'line 7 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2803 \ 'line 8 Abcdefghijklmnopqrstuvwxyz',
2804 \ 'line 9 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2805 \ 'line 10 Abcdefghijklmnopqrstuvwxyz'
2806 \ ])
2807 1,9write! Xtestfile.gz
2808 enew! | close
2809
2810 new
2811 " Read and decompress the testfile
2812 0read Xtestfile.gz
2813 call assert_equal([
2814 \ 'line 2 Abcdefghijklmnopqrstuvwxyz',
2815 \ 'line 3 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2816 \ 'line 4 Abcdefghijklmnopqrstuvwxyz',
2817 \ 'line 5 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2818 \ 'line 6 Abcdefghijklmnopqrstuvwxyz',
2819 \ 'line 7 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2820 \ 'line 8 Abcdefghijklmnopqrstuvwxyz',
2821 \ 'line 9 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2822 \ 'line 10 Abcdefghijklmnopqrstuvwxyz'
2823 \ ], getline(1, 9))
2824 enew! | close
2825
2826 augroup Test1
2827 au!
2828 augroup END
2829
2830 " Test for the FileAppendPre and FileAppendPost autocmds
2831 augroup Test2
2832 au!
2833 au BufNewFile *.c read Xtest.c
2834 au FileAppendPre *.out '[,']s/new/NEW/
2835 au FileAppendPost *.out !cat Xtest.c >> test.out
2836 augroup END
2837
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01002838 call writefile(['/*', ' * Here is a new .c file', ' */'], 'Xtest.c', 'D')
Bram Moolenaar69ea5872019-04-25 20:29:00 +02002839 new foo.c " should load Xtest.c
2840 call assert_equal(['/*', ' * Here is a new .c file', ' */'], getline(2, 4))
2841 w! >> test.out " append it to the output file
2842
2843 let contents = readfile('test.out')
2844 call assert_equal(' * Here is a NEW .c file', contents[2])
2845 call assert_equal(' * Here is a new .c file', contents[5])
2846
2847 call delete('test.out')
2848 enew! | close
2849 augroup Test2
2850 au!
2851 augroup END
2852
2853 " Test for the BufReadPre and BufReadPost autocmds
2854 augroup Test3
2855 au!
2856 " setup autocommands to decompress before reading and re-compress
2857 " afterwards
2858 au BufReadPre *.gz exe '!gzip -d ' . shellescape(expand("<afile>"))
2859 au BufReadPre *.gz call rename(expand("<afile>:r"), expand("<afile>"))
2860 au BufReadPost *.gz call rename(expand("<afile>"), expand("<afile>:r"))
2861 au BufReadPost *.gz exe '!gzip ' . shellescape(expand("<afile>:r"))
2862 augroup END
2863
2864 e! Xtestfile.gz " Edit compressed file
2865 call assert_equal([
2866 \ 'line 2 Abcdefghijklmnopqrstuvwxyz',
2867 \ 'line 3 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2868 \ 'line 4 Abcdefghijklmnopqrstuvwxyz',
2869 \ 'line 5 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2870 \ 'line 6 Abcdefghijklmnopqrstuvwxyz',
2871 \ 'line 7 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2872 \ 'line 8 Abcdefghijklmnopqrstuvwxyz',
2873 \ 'line 9 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2874 \ 'line 10 Abcdefghijklmnopqrstuvwxyz'
2875 \ ], getline(1, 9))
2876
2877 w! >> test.out " Append it to the output file
2878
2879 augroup Test3
2880 au!
2881 augroup END
2882
2883 " Test for the FilterReadPre and FilterReadPost autocmds.
2884 set shelltemp " need temp files here
2885 augroup Test4
2886 au!
2887 au FilterReadPre *.out call rename(expand("<afile>"), expand("<afile>") . ".t")
2888 au FilterReadPre *.out exe 'silent !sed s/e/E/ ' . shellescape(expand("<afile>")) . ".t >" . shellescape(expand("<afile>"))
2889 au FilterReadPre *.out exe 'silent !rm ' . shellescape(expand("<afile>")) . '.t'
2890 au FilterReadPost *.out '[,']s/x/X/g
2891 augroup END
2892
2893 e! test.out " Edit the output file
2894 1,$!cat
2895 call assert_equal([
2896 \ 'linE 2 AbcdefghijklmnopqrstuvwXyz',
2897 \ 'linE 3 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
2898 \ 'linE 4 AbcdefghijklmnopqrstuvwXyz',
2899 \ 'linE 5 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
2900 \ 'linE 6 AbcdefghijklmnopqrstuvwXyz',
2901 \ 'linE 7 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
2902 \ 'linE 8 AbcdefghijklmnopqrstuvwXyz',
2903 \ 'linE 9 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
2904 \ 'linE 10 AbcdefghijklmnopqrstuvwXyz'
2905 \ ], getline(1, 9))
2906 call assert_equal([
2907 \ 'line 2 Abcdefghijklmnopqrstuvwxyz',
2908 \ 'line 3 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2909 \ 'line 4 Abcdefghijklmnopqrstuvwxyz',
2910 \ 'line 5 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2911 \ 'line 6 Abcdefghijklmnopqrstuvwxyz',
2912 \ 'line 7 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2913 \ 'line 8 Abcdefghijklmnopqrstuvwxyz',
2914 \ 'line 9 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2915 \ 'line 10 Abcdefghijklmnopqrstuvwxyz'
2916 \ ], readfile('test.out'))
2917
2918 augroup Test4
2919 au!
2920 augroup END
2921 set shelltemp&vim
2922
2923 " Test for the FileReadPre and FileReadPost autocmds.
2924 augroup Test5
2925 au!
2926 au FileReadPre *.gz exe 'silent !gzip -d ' . shellescape(expand("<afile>"))
2927 au FileReadPre *.gz call rename(expand("<afile>:r"), expand("<afile>"))
2928 au FileReadPost *.gz '[,']s/l/L/
2929 augroup END
2930
2931 new
2932 0r Xtestfile.gz " Read compressed file
2933 call assert_equal([
2934 \ 'Line 2 Abcdefghijklmnopqrstuvwxyz',
2935 \ 'Line 3 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2936 \ 'Line 4 Abcdefghijklmnopqrstuvwxyz',
2937 \ 'Line 5 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2938 \ 'Line 6 Abcdefghijklmnopqrstuvwxyz',
2939 \ 'Line 7 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2940 \ 'Line 8 Abcdefghijklmnopqrstuvwxyz',
2941 \ 'Line 9 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2942 \ 'Line 10 Abcdefghijklmnopqrstuvwxyz'
2943 \ ], getline(1, 9))
2944 call assert_equal([
2945 \ 'line 2 Abcdefghijklmnopqrstuvwxyz',
2946 \ 'line 3 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2947 \ 'line 4 Abcdefghijklmnopqrstuvwxyz',
2948 \ 'line 5 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2949 \ 'line 6 Abcdefghijklmnopqrstuvwxyz',
2950 \ 'line 7 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2951 \ 'line 8 Abcdefghijklmnopqrstuvwxyz',
2952 \ 'line 9 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2953 \ 'line 10 Abcdefghijklmnopqrstuvwxyz'
2954 \ ], readfile('Xtestfile.gz'))
2955
2956 augroup Test5
2957 au!
2958 augroup END
2959
2960 au! FileChangedShell
2961 call delete('Xtestfile.gz')
Bram Moolenaar69ea5872019-04-25 20:29:00 +02002962 call delete('test.out')
2963endfunc
Bram Moolenaar23b51392019-05-09 21:38:43 +02002964
2965func Test_throw_in_BufWritePre()
2966 new
2967 call setline(1, ['one', 'two', 'three'])
2968 call assert_false(filereadable('Xthefile'))
2969 augroup throwing
2970 au BufWritePre X* throw 'do not write'
2971 augroup END
2972 try
2973 w Xthefile
2974 catch
2975 let caught = 1
2976 endtry
2977 call assert_equal(1, caught)
2978 call assert_false(filereadable('Xthefile'))
2979
2980 bwipe!
2981 au! throwing
2982endfunc
Bram Moolenaarcadbe1b2019-09-22 21:50:09 +02002983
Bram Moolenaar40fa12a2021-09-22 14:18:13 +02002984func Test_autocmd_in_try_block()
Bram Moolenaar6f14da12022-09-07 21:30:44 +01002985 call mkdir('Xintrydir', 'R')
Bram Moolenaar40fa12a2021-09-22 14:18:13 +02002986 au BufEnter * let g:fname = expand('%')
2987 try
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +01002988 edit Xintrydir/
Bram Moolenaar40fa12a2021-09-22 14:18:13 +02002989 endtry
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +01002990 call assert_match('Xintrydir', g:fname)
Bram Moolenaar40fa12a2021-09-22 14:18:13 +02002991
2992 unlet g:fname
2993 au! BufEnter
Bram Moolenaar40fa12a2021-09-22 14:18:13 +02002994endfunc
2995
Bram Moolenaarcadbe1b2019-09-22 21:50:09 +02002996func Test_autocmd_SafeState()
2997 CheckRunVimInTerminal
2998
2999 let lines =<< trim END
3000 let g:safe = 0
3001 let g:again = ''
3002 au SafeState * let g:safe += 1
3003 au SafeStateAgain * let g:again ..= 'x'
3004 func CallTimer()
3005 call timer_start(10, {id -> execute('let g:again ..= "t"')})
3006 endfunc
3007 END
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01003008 call writefile(lines, 'XSafeState', 'D')
Bram Moolenaarcadbe1b2019-09-22 21:50:09 +02003009 let buf = RunVimInTerminal('-S XSafeState', #{rows: 6})
3010
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01003011 " Sometimes we loop to handle a K_IGNORE, SafeState may be triggered once or
Bram Moolenaar8fb1b472020-02-23 16:16:26 +01003012 " more often.
Bram Moolenaarcadbe1b2019-09-22 21:50:09 +02003013 call term_sendkeys(buf, ":echo g:safe\<CR>")
Bram Moolenaar8fb1b472020-02-23 16:16:26 +01003014 call WaitForAssert({-> assert_match('^\d ', term_getline(buf, 6))}, 1000)
Bram Moolenaarcadbe1b2019-09-22 21:50:09 +02003015
Bram Moolenaar8fb1b472020-02-23 16:16:26 +01003016 " SafeStateAgain should be invoked at least three times
Bram Moolenaarcadbe1b2019-09-22 21:50:09 +02003017 call term_sendkeys(buf, ":echo g:again\<CR>")
Bram Moolenaar8fb1b472020-02-23 16:16:26 +01003018 call WaitForAssert({-> assert_match('^xxx', term_getline(buf, 6))}, 1000)
Bram Moolenaarcadbe1b2019-09-22 21:50:09 +02003019
3020 call term_sendkeys(buf, ":let g:again = ''\<CR>:call CallTimer()\<CR>")
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02003021 call TermWait(buf, 50)
Bram Moolenaar0f6629a2019-09-22 23:24:13 +02003022 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02003023 call TermWait(buf, 50)
Bram Moolenaarcadbe1b2019-09-22 21:50:09 +02003024 call term_sendkeys(buf, ":echo g:again\<CR>")
3025 call WaitForAssert({-> assert_match('xtx', term_getline(buf, 6))}, 1000)
3026
3027 call StopVimInTerminal(buf)
Bram Moolenaarcadbe1b2019-09-22 21:50:09 +02003028endfunc
Bram Moolenaar23324a02019-10-01 17:39:04 +02003029
3030func Test_autocmd_CmdWinEnter()
3031 CheckRunVimInTerminal
Bram Moolenaar21829c52021-01-26 22:42:21 +01003032
Bram Moolenaar23324a02019-10-01 17:39:04 +02003033 let lines =<< trim END
Egor Zvorykin125ffd22021-11-17 14:01:14 +00003034 augroup vimHints | au! | augroup END
Bram Moolenaar23324a02019-10-01 17:39:04 +02003035 let b:dummy_var = 'This is a dummy'
3036 autocmd CmdWinEnter * quit
3037 let winnr = winnr('$')
3038 END
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01003039 let filename = 'XCmdWinEnter'
Bram Moolenaar23324a02019-10-01 17:39:04 +02003040 call writefile(lines, filename)
3041 let buf = RunVimInTerminal('-S '.filename, #{rows: 6})
3042
3043 call term_sendkeys(buf, "q:")
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02003044 call TermWait(buf)
Bram Moolenaar23324a02019-10-01 17:39:04 +02003045 call term_sendkeys(buf, ":echo b:dummy_var\<cr>")
Bram Moolenaar353c3512020-03-15 14:19:26 +01003046 call WaitForAssert({-> assert_match('^This is a dummy', term_getline(buf, 6))}, 2000)
Bram Moolenaar23324a02019-10-01 17:39:04 +02003047 call term_sendkeys(buf, ":echo &buftype\<cr>")
3048 call WaitForAssert({-> assert_notmatch('^nofile', term_getline(buf, 6))}, 1000)
3049 call term_sendkeys(buf, ":echo winnr\<cr>")
3050 call WaitForAssert({-> assert_match('^1', term_getline(buf, 6))}, 1000)
3051
3052 " clean up
3053 call StopVimInTerminal(buf)
3054 call delete(filename)
3055endfunc
Bram Moolenaarec66c412019-10-11 21:19:13 +02003056
3057func Test_autocmd_was_using_freed_memory()
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01003058 CheckFeature quickfix
3059
Bram Moolenaarec66c412019-10-11 21:19:13 +02003060 pedit xx
3061 n x
Bram Moolenaar92bb83e2021-02-03 23:04:46 +01003062 augroup winenter
3063 au WinEnter * if winnr('$') > 2 | quit | endif
3064 augroup END
Bram Moolenaarec66c412019-10-11 21:19:13 +02003065 split
Bram Moolenaar92bb83e2021-02-03 23:04:46 +01003066
3067 augroup winenter
3068 au! WinEnter
3069 augroup END
3070
3071 bwipe xx
3072 bwipe x
3073 pclose
Bram Moolenaarec66c412019-10-11 21:19:13 +02003074endfunc
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +01003075
3076func Test_BufWrite_lockmarks()
Bram Moolenaarf08b0eb2021-10-16 13:00:14 +01003077 let g:test_is_flaky = 1
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +01003078 edit! Xtest
3079 call setline(1, ['a', 'b', 'c', 'd'])
3080
3081 " :lockmarks preserves the marks
3082 call SetChangeMarks(2, 3)
3083 lockmarks write
3084 call assert_equal([2, 3], [line("'["), line("']")])
3085
3086 " *WritePre autocmds get the correct line range, but lockmarks preserves the
3087 " original values for the user
3088 augroup lockmarks
3089 au!
3090 au BufWritePre,FilterWritePre * call assert_equal([1, 4], [line("'["), line("']")])
3091 au FileWritePre * call assert_equal([3, 4], [line("'["), line("']")])
3092 augroup END
3093
3094 lockmarks write
3095 call assert_equal([2, 3], [line("'["), line("']")])
3096
3097 if executable('cat')
3098 lockmarks %!cat
3099 call assert_equal([2, 3], [line("'["), line("']")])
3100 endif
3101
3102 lockmarks 3,4write Xtest2
3103 call assert_equal([2, 3], [line("'["), line("']")])
3104
3105 au! lockmarks
3106 augroup! lockmarks
3107 call delete('Xtest')
3108 call delete('Xtest2')
3109endfunc
Bram Moolenaarce6db022020-01-07 20:11:42 +01003110
3111func Test_FileType_spell()
3112 if !isdirectory('/tmp')
3113 throw "Skipped: requires /tmp directory"
3114 endif
3115
3116 " this was crashing with an invalid free()
3117 setglobal spellfile=/tmp/en.utf-8.add
3118 augroup crash
3119 autocmd!
3120 autocmd BufNewFile,BufReadPost crashfile setf somefiletype
3121 autocmd BufNewFile,BufReadPost crashfile set ft=anotherfiletype
3122 autocmd FileType anotherfiletype setlocal spell
3123 augroup END
3124 func! NoCrash() abort
3125 edit /tmp/crashfile
3126 endfunc
3127 call NoCrash()
3128
3129 au! crash
3130 setglobal spellfile=
3131endfunc
Bram Moolenaarbc2b71d2020-02-17 21:33:30 +01003132
Bram Moolenaaref976322022-09-28 11:48:30 +01003133" this was wiping out the current buffer and using freed memory
3134func Test_SpellFileMissing_bwipe()
3135 next 0
3136 au SpellFileMissing 0 bwipe
3137 call assert_fails('set spell spelllang=0', 'E937:')
3138
3139 au! SpellFileMissing
Bram Moolenaar0a60f792022-11-19 21:18:11 +00003140 set nospell spelllang=en
Bram Moolenaaref976322022-09-28 11:48:30 +01003141 bwipe
3142endfunc
3143
Bram Moolenaar406cd902020-02-18 21:54:41 +01003144" Test closing a window or editing another buffer from a FileChangedRO handler
3145" in a readonly buffer
3146func Test_FileChangedRO_winclose()
Bram Moolenaar62cd26a2020-10-11 20:08:44 +02003147 call test_override('ui_delay', 10)
3148
Bram Moolenaar406cd902020-02-18 21:54:41 +01003149 augroup FileChangedROTest
3150 au!
3151 autocmd FileChangedRO * quit
3152 augroup END
3153 new
3154 set readonly
3155 call assert_fails('normal i', 'E788:')
3156 close
3157 augroup! FileChangedROTest
3158
3159 augroup FileChangedROTest
3160 au!
Bram Moolenaar61abe7d2022-08-30 21:46:08 +01003161 autocmd FileChangedRO * edit Xrofile
Bram Moolenaar406cd902020-02-18 21:54:41 +01003162 augroup END
3163 new
3164 set readonly
3165 call assert_fails('normal i', 'E788:')
3166 close
3167 augroup! FileChangedROTest
Bram Moolenaar62cd26a2020-10-11 20:08:44 +02003168 call test_override('ALL', 0)
Bram Moolenaar406cd902020-02-18 21:54:41 +01003169endfunc
3170
Bram Moolenaar0c81d1b2020-02-22 22:45:55 +01003171func LogACmd()
3172 call add(g:logged, line('$'))
3173endfunc
3174
3175func Test_TermChanged()
Bram Moolenaard28e0b32020-02-22 23:08:52 +01003176 CheckNotGui
3177
Bram Moolenaar0c81d1b2020-02-22 22:45:55 +01003178 enew!
3179 tabnew
3180 call setline(1, ['a', 'b', 'c', 'd'])
3181 $
3182 au TermChanged * call LogACmd()
3183 let g:logged = []
3184 let term_save = &term
3185 set term=xterm
3186 call assert_equal([1, 4], g:logged)
3187
3188 au! TermChanged
3189 let &term = term_save
3190 bwipe!
3191endfunc
3192
Bram Moolenaare3284872020-03-19 13:55:03 +01003193" Test for FileReadCmd autocmd
3194func Test_autocmd_FileReadCmd()
3195 func ReadFileCmd()
3196 call append(line('$'), "v:cmdarg = " .. v:cmdarg)
3197 endfunc
3198 augroup FileReadCmdTest
3199 au!
3200 au FileReadCmd Xtest call ReadFileCmd()
3201 augroup END
3202
3203 new
3204 read ++bin Xtest
3205 read ++nobin Xtest
3206 read ++edit Xtest
3207 read ++bad=keep Xtest
3208 read ++bad=drop Xtest
3209 read ++bad=- Xtest
3210 read ++ff=unix Xtest
3211 read ++ff=dos Xtest
3212 read ++ff=mac Xtest
3213 read ++enc=utf-8 Xtest
3214
3215 call assert_equal(['',
3216 \ 'v:cmdarg = ++bin',
3217 \ 'v:cmdarg = ++nobin',
3218 \ 'v:cmdarg = ++edit',
3219 \ 'v:cmdarg = ++bad=keep',
3220 \ 'v:cmdarg = ++bad=drop',
3221 \ 'v:cmdarg = ++bad=-',
3222 \ 'v:cmdarg = ++ff=unix',
3223 \ 'v:cmdarg = ++ff=dos',
3224 \ 'v:cmdarg = ++ff=mac',
3225 \ 'v:cmdarg = ++enc=utf-8'], getline(1, '$'))
3226
Bram Moolenaar23526d22022-12-05 15:50:41 +00003227 bwipe!
Bram Moolenaare3284872020-03-19 13:55:03 +01003228 augroup FileReadCmdTest
3229 au!
3230 augroup END
3231 delfunc ReadFileCmd
3232endfunc
3233
Bram Moolenaaree4e0c12020-04-06 21:35:05 +02003234" Test for passing invalid arguments to autocmd
3235func Test_autocmd_invalid_args()
3236 " Additional character after * for event
Bram Moolenaar61abe7d2022-08-30 21:46:08 +01003237 call assert_fails('autocmd *a Xinvfile set ff=unix', 'E215:')
Bram Moolenaaree4e0c12020-04-06 21:35:05 +02003238 augroup Test
3239 augroup END
3240 " Invalid autocmd event
Bram Moolenaar61abe7d2022-08-30 21:46:08 +01003241 call assert_fails('autocmd Bufabc Xinvfile set ft=vim', 'E216:')
Bram Moolenaaree4e0c12020-04-06 21:35:05 +02003242 " Invalid autocmd event in a autocmd group
Bram Moolenaar61abe7d2022-08-30 21:46:08 +01003243 call assert_fails('autocmd Test Bufabc Xinvfile set ft=vim', 'E216:')
Bram Moolenaaree4e0c12020-04-06 21:35:05 +02003244 augroup! Test
3245 " Execute all autocmds
3246 call assert_fails('doautocmd * BufEnter', 'E217:')
3247 call assert_fails('augroup! x1a2b3', 'E367:')
3248 call assert_fails('autocmd BufNew <buffer=999> pwd', 'E680:')
Bram Moolenaar531be472020-09-23 22:38:05 +02003249 call assert_fails('autocmd BufNew \) set ff=unix', 'E55:')
Bram Moolenaaree4e0c12020-04-06 21:35:05 +02003250endfunc
3251
3252" Test for deep nesting of autocmds
3253func Test_autocmd_deep_nesting()
Bram Moolenaar61abe7d2022-08-30 21:46:08 +01003254 autocmd BufEnter Xdeepfile doautocmd BufEnter Xdeepfile
3255 call assert_fails('doautocmd BufEnter Xdeepfile', 'E218:')
3256 autocmd! BufEnter Xdeepfile
Bram Moolenaaree4e0c12020-04-06 21:35:05 +02003257endfunc
3258
Bram Moolenaarbe5ee862020-06-10 20:56:58 +02003259" Tests for SigUSR1 autocmd event, which is only available on posix systems.
3260func Test_autocmd_sigusr1()
3261 CheckUnix
Bram Moolenaar0056ca72022-09-23 21:26:39 +01003262 " FIXME: should this work on MacOS M1?
3263 CheckNotMacM1
Bram Moolenaar62cd26a2020-10-11 20:08:44 +02003264 CheckExecutable /bin/kill
Bram Moolenaarbe5ee862020-06-10 20:56:58 +02003265
3266 let g:sigusr1_passed = 0
3267 au SigUSR1 * let g:sigusr1_passed = 1
3268 call system('/bin/kill -s usr1 ' . getpid())
3269 call WaitForAssert({-> assert_true(g:sigusr1_passed)})
3270
3271 au! SigUSR1
3272 unlet g:sigusr1_passed
3273endfunc
3274
Bram Moolenaarb340bae2020-06-15 19:51:56 +02003275" Test for BufReadPre autocmd deleting the file
3276func Test_BufReadPre_delfile()
3277 augroup TestAuCmd
3278 au!
Bram Moolenaare7cda972022-08-29 11:02:59 +01003279 autocmd BufReadPre XbufreadPre call delete('XbufreadPre')
Bram Moolenaarb340bae2020-06-15 19:51:56 +02003280 augroup END
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01003281 call writefile([], 'XbufreadPre', 'D')
Bram Moolenaare7cda972022-08-29 11:02:59 +01003282 call assert_fails('new XbufreadPre', 'E200:')
3283 call assert_equal('XbufreadPre', @%)
Bram Moolenaarb340bae2020-06-15 19:51:56 +02003284 call assert_equal(1, &readonly)
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01003285
Bram Moolenaarb340bae2020-06-15 19:51:56 +02003286 augroup TestAuCmd
3287 au!
3288 augroup END
3289 close!
3290endfunc
3291
3292" Test for BufReadPre autocmd changing the current buffer
3293func Test_BufReadPre_changebuf()
3294 augroup TestAuCmd
3295 au!
Bram Moolenaare7cda972022-08-29 11:02:59 +01003296 autocmd BufReadPre Xchangebuf edit Xsomeotherfile
Bram Moolenaarb340bae2020-06-15 19:51:56 +02003297 augroup END
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01003298 call writefile([], 'Xchangebuf', 'D')
Bram Moolenaare7cda972022-08-29 11:02:59 +01003299 call assert_fails('new Xchangebuf', 'E201:')
Bram Moolenaarb340bae2020-06-15 19:51:56 +02003300 call assert_equal('Xsomeotherfile', @%)
3301 call assert_equal(1, &readonly)
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01003302
Bram Moolenaarb340bae2020-06-15 19:51:56 +02003303 augroup TestAuCmd
3304 au!
3305 augroup END
3306 close!
3307endfunc
3308
3309" Test for BufWipeouti autocmd changing the current buffer when reading a file
3310" in an empty buffer with 'f' flag in 'cpo'
3311func Test_BufDelete_changebuf()
3312 new
3313 augroup TestAuCmd
3314 au!
3315 autocmd BufWipeout * let bufnr = bufadd('somefile') | exe "b " .. bufnr
3316 augroup END
3317 let save_cpo = &cpo
3318 set cpo+=f
Bram Moolenaarb18b4962022-09-02 21:55:50 +01003319 call assert_fails('r Xchangebuf', ['E812:', 'E484:'])
Bram Moolenaarb340bae2020-06-15 19:51:56 +02003320 call assert_equal('somefile', @%)
3321 let &cpo = save_cpo
3322 augroup TestAuCmd
3323 au!
3324 augroup END
3325 close!
3326endfunc
3327
Bram Moolenaar0fe937f2020-06-16 22:42:04 +02003328" Test for the temporary internal window used to execute autocmds
3329func Test_autocmd_window()
3330 %bw!
3331 edit one.txt
3332 tabnew two.txt
Bram Moolenaar41cd8032021-03-13 15:47:56 +01003333 vnew three.txt
3334 tabnew four.txt
3335 tabprevious
Bram Moolenaar0fe937f2020-06-16 22:42:04 +02003336 let g:blist = []
Bram Moolenaar832adf92020-06-25 19:01:36 +02003337 augroup aucmd_win_test1
Bram Moolenaar0fe937f2020-06-16 22:42:04 +02003338 au!
3339 au BufEnter * call add(g:blist, [expand('<afile>'),
3340 \ win_gettype(bufwinnr(expand('<afile>')))])
3341 augroup END
3342
3343 doautoall BufEnter
Bram Moolenaar41cd8032021-03-13 15:47:56 +01003344 call assert_equal([
3345 \ ['one.txt', 'autocmd'],
3346 \ ['two.txt', ''],
3347 \ ['four.txt', 'autocmd'],
3348 \ ['three.txt', ''],
3349 \ ], g:blist)
Bram Moolenaar0fe937f2020-06-16 22:42:04 +02003350
Bram Moolenaar832adf92020-06-25 19:01:36 +02003351 augroup aucmd_win_test1
Bram Moolenaar0fe937f2020-06-16 22:42:04 +02003352 au!
3353 augroup END
Bram Moolenaar832adf92020-06-25 19:01:36 +02003354 augroup! aucmd_win_test1
3355 %bw!
3356endfunc
3357
3358" Test for trying to close the temporary window used for executing an autocmd
3359func Test_close_autocmd_window()
3360 %bw!
3361 edit one.txt
3362 tabnew two.txt
3363 augroup aucmd_win_test2
3364 au!
3365 au BufEnter * if expand('<afile>') == 'one.txt' | 1close | endif
3366 augroup END
3367
3368 call assert_fails('doautoall BufEnter', 'E813:')
3369
3370 augroup aucmd_win_test2
3371 au!
3372 augroup END
3373 augroup! aucmd_win_test2
Bram Moolenaarcf844172020-06-26 19:44:06 +02003374 %bwipe!
3375endfunc
3376
3377" Test for trying to close the tab that has the temporary window for exeucing
3378" an autocmd.
3379func Test_close_autocmd_tab()
3380 edit one.txt
3381 tabnew two.txt
3382 augroup aucmd_win_test
3383 au!
3384 au BufEnter * if expand('<afile>') == 'one.txt' | tabfirst | tabonly | endif
3385 augroup END
3386
3387 call assert_fails('doautoall BufEnter', 'E813:')
3388
3389 tabonly
3390 augroup aucmd_win_test
3391 au!
3392 augroup END
3393 augroup! aucmd_win_test
3394 %bwipe!
Bram Moolenaar0fe937f2020-06-16 22:42:04 +02003395endfunc
3396
Bram Moolenaarcb1956d2022-01-07 15:45:18 +00003397func Test_Visual_doautoall_redraw()
3398 call setline(1, ['a', 'b'])
3399 new
3400 wincmd p
3401 call feedkeys("G\<C-V>", 'txn')
3402 autocmd User Explode ++once redraw
3403 doautoall User Explode
3404 %bwipe!
3405endfunc
3406
Bram Moolenaar6bcb8772021-02-03 21:23:29 +01003407" This was using freed memory.
3408func Test_BufNew_arglocal()
3409 arglocal
3410 au BufNew * arglocal
3411 call assert_fails('drop xx', 'E1156:')
3412
3413 au! BufNew
3414endfunc
3415
Bram Moolenaar8ab37572021-02-03 21:56:59 +01003416func Test_autocmd_closes_window()
3417 au BufNew,BufWinLeave * e %e
3418 file yyy
3419 au BufNew,BufWinLeave * ball
Bram Moolenaaraad5f9d2021-02-06 17:30:31 +01003420 n xxx
Bram Moolenaar8ab37572021-02-03 21:56:59 +01003421
Bram Moolenaaraad5f9d2021-02-06 17:30:31 +01003422 %bwipe
Bram Moolenaar8ab37572021-02-03 21:56:59 +01003423 au! BufNew
3424 au! BufWinLeave
3425endfunc
3426
Bram Moolenaar92bb83e2021-02-03 23:04:46 +01003427func Test_autocmd_quit_psearch()
3428 sn aa bb
3429 augroup aucmd_win_test
3430 au!
3431 au BufEnter,BufLeave,BufNew,WinEnter,WinLeave,WinNew * if winnr('$') > 1 | q | endif
3432 augroup END
3433 ps /
3434
3435 augroup aucmd_win_test
3436 au!
3437 augroup END
zeertzjq7851c692022-04-21 11:14:01 +01003438 new
3439 pclose
Bram Moolenaar92bb83e2021-02-03 23:04:46 +01003440endfunc
3441
Bram Moolenaaraad5f9d2021-02-06 17:30:31 +01003442" Fuzzer found some strange combination that caused a crash.
3443func Test_autocmd_normal_mess()
Bram Moolenaardd07c022021-02-07 13:32:46 +01003444 " For unknown reason this hangs on MS-Windows
3445 CheckNotMSWindows
3446
Bram Moolenaaraad5f9d2021-02-06 17:30:31 +01003447 augroup aucmd_normal_test
3448 au BufLeave,BufWinLeave,BufHidden,BufUnload,BufDelete,BufWipeout * norm 7q/qc
3449 augroup END
Bram Moolenaar983d83f2021-02-07 12:12:43 +01003450 call assert_fails('o4', 'E1159')
Bram Moolenaaraad5f9d2021-02-06 17:30:31 +01003451 silent! H
Bram Moolenaar983d83f2021-02-07 12:12:43 +01003452 call assert_fails('e xx', 'E1159')
Bram Moolenaaraad5f9d2021-02-06 17:30:31 +01003453 normal G
3454
3455 augroup aucmd_normal_test
3456 au!
3457 augroup END
3458endfunc
3459
Bram Moolenaar8c6951f2021-02-06 18:08:45 +01003460func Test_autocmd_closing_cmdwin()
Bram Moolenaardd07c022021-02-07 13:32:46 +01003461 " For unknown reason this hangs on MS-Windows
3462 CheckNotMSWindows
3463
Bram Moolenaar8c6951f2021-02-06 18:08:45 +01003464 au BufWinLeave * nested q
3465 call assert_fails("norm 7q?\n", 'E855:')
3466
3467 au! BufWinLeave
3468 new
3469 only
3470endfunc
3471
Bram Moolenaar2c7080b2021-02-06 19:19:42 +01003472func Test_autocmd_vimgrep()
3473 augroup aucmd_vimgrep
Charlie Grovesfef44852022-04-19 16:24:12 +01003474 au QuickfixCmdPre,BufNew,BufReadCmd * sb
zeertzjq7851c692022-04-21 11:14:01 +01003475 au QuickfixCmdPre,BufNew,BufReadCmd * q9
Bram Moolenaar2c7080b2021-02-06 19:19:42 +01003476 augroup END
Bram Moolenaardd07c022021-02-07 13:32:46 +01003477 call assert_fails('lv ?a? foo', 'E926:')
Bram Moolenaar2c7080b2021-02-06 19:19:42 +01003478
3479 augroup aucmd_vimgrep
3480 au!
3481 augroup END
3482endfunc
3483
Bram Moolenaar73b8b0a2021-08-01 14:52:32 +02003484func Test_autocmd_with_block()
3485 augroup block_testing
3486 au BufReadPost *.xml {
3487 setlocal matchpairs+=<:>
3488 /<start
3489 }
Bram Moolenaar63b91732021-08-05 20:40:03 +02003490 au CursorHold * {
3491 autocmd BufReadPre * ++once echo 'one' | echo 'two'
3492 g:gotSafeState = 77
3493 }
Bram Moolenaar73b8b0a2021-08-01 14:52:32 +02003494 augroup END
3495
3496 let expected = "\n--- Autocommands ---\nblock_testing BufRead\n *.xml {^@ setlocal matchpairs+=<:>^@ /<start^@ }"
3497 call assert_equal(expected, execute('au BufReadPost *.xml'))
3498
Bram Moolenaar63b91732021-08-05 20:40:03 +02003499 doautocmd CursorHold
3500 call assert_equal(77, g:gotSafeState)
3501 unlet g:gotSafeState
3502
Bram Moolenaar73b8b0a2021-08-01 14:52:32 +02003503 augroup block_testing
3504 au!
Bram Moolenaar75ebd2a2022-06-03 17:39:46 +01003505 autocmd CursorHold * {
3506 if true
3507 # comment
3508 && true
3509
3510 && true
3511 g:done = 'yes'
3512 endif
3513 }
3514 augroup END
3515 doautocmd CursorHold
3516 call assert_equal('yes', g:done)
3517
3518 unlet g:done
3519 augroup block_testing
3520 au!
Bram Moolenaar73b8b0a2021-08-01 14:52:32 +02003521 augroup END
3522endfunc
3523
Christian Brabandtdb3b4462021-10-16 11:58:55 +01003524" Test TextChangedI and TextChanged
3525func Test_Changed_ChangedI()
3526 new
3527 call test_override("char_avail", 1)
3528 let [g:autocmd_i, g:autocmd_n] = ['','']
3529
3530 func! TextChangedAutocmdI(char)
3531 let g:autocmd_{tolower(a:char)} = a:char .. b:changedtick
3532 endfunc
3533
3534 augroup Test_TextChanged
3535 au!
3536 au TextChanged <buffer> :call TextChangedAutocmdI('N')
3537 au TextChangedI <buffer> :call TextChangedAutocmdI('I')
3538 augroup END
3539
3540 call feedkeys("ifoo\<esc>", 'tnix')
3541 " TODO: Test test does not seem to trigger TextChanged autocommand, this
3542 " requires running Vim in a terminal window.
3543 " call assert_equal('N3', g:autocmd_n)
3544 call assert_equal('I3', g:autocmd_i)
3545
3546 call feedkeys("yyp", 'tnix')
3547 " TODO: Test test does not seem to trigger TextChanged autocommand.
3548 " call assert_equal('N4', g:autocmd_n)
3549 call assert_equal('I3', g:autocmd_i)
3550
3551 " CleanUp
3552 call test_override("char_avail", 0)
3553 au! TextChanged <buffer>
3554 au! TextChangedI <buffer>
3555 augroup! Test_TextChanged
3556 delfu TextChangedAutocmdI
3557 unlet! g:autocmd_i g:autocmd_n
3558
3559 bw!
3560endfunc
Bram Moolenaar2c7080b2021-02-06 19:19:42 +01003561
Bram Moolenaar6f2465d2022-03-22 18:13:01 +00003562func Test_closing_autocmd_window()
3563 let lines =<< trim END
3564 edit Xa.txt
3565 tabnew Xb.txt
3566 autocmd BufEnter Xa.txt unhide 1
3567 doautoall BufEnter
3568 END
3569 call v9.CheckScriptFailure(lines, 'E814:')
3570 au! BufEnter
3571 only!
3572 bwipe Xa.txt
3573 bwipe Xb.txt
3574endfunc
3575
Bram Moolenaar347538f2022-03-26 16:28:06 +00003576func Test_bufwipeout_changes_window()
3577 " This should not crash, but we don't have any expectations about what
3578 " happens, changing window in BufWipeout has unpredictable results.
3579 tabedit
3580 let g:window_id = win_getid()
3581 topleft new
3582 setlocal bufhidden=wipe
3583 autocmd BufWipeout <buffer> call win_gotoid(g:window_id)
3584 tabprevious
3585 +tabclose
3586
3587 unlet g:window_id
3588 au! BufWipeout
3589 %bwipe!
3590endfunc
3591
zeertzjq021996f2022-04-10 11:44:04 +01003592func Test_v_event_readonly()
3593 autocmd CompleteChanged * let v:event.width = 0
3594 call assert_fails("normal! i\<C-X>\<C-V>", 'E46:')
3595 au! CompleteChanged
3596
3597 autocmd DirChangedPre * let v:event.directory = ''
3598 call assert_fails('cd .', 'E46:')
3599 au! DirChangedPre
3600
3601 autocmd ModeChanged * let v:event.new_mode = ''
3602 call assert_fails('normal! cc', 'E46:')
3603 au! ModeChanged
3604
3605 autocmd TextYankPost * let v:event.operator = ''
3606 call assert_fails('normal! yy', 'E46:')
3607 au! TextYankPost
3608endfunc
3609
zeertzjqc9e8fd62022-07-26 18:12:38 +01003610" Test for ModeChanged pattern
3611func Test_mode_changes()
3612 let g:index = 0
3613 let g:mode_seq = ['n', 'i', 'n', 'v', 'V', 'i', 'ix', 'i', 'ic', 'i', 'n', 'no', 'n', 'V', 'v', 's', 'n']
3614 func! TestMode()
3615 call assert_equal(g:mode_seq[g:index], get(v:event, "old_mode"))
3616 call assert_equal(g:mode_seq[g:index + 1], get(v:event, "new_mode"))
3617 call assert_equal(mode(1), get(v:event, "new_mode"))
3618 let g:index += 1
3619 endfunc
3620
3621 au ModeChanged * :call TestMode()
3622 let g:n_to_any = 0
3623 au ModeChanged n:* let g:n_to_any += 1
3624 call feedkeys("i\<esc>vVca\<CR>\<C-X>\<C-L>\<esc>ggdG", 'tnix')
3625
3626 let g:V_to_v = 0
3627 au ModeChanged V:v let g:V_to_v += 1
3628 call feedkeys("Vv\<C-G>\<esc>", 'tnix')
3629 call assert_equal(len(filter(g:mode_seq[1:], {idx, val -> val == 'n'})), g:n_to_any)
3630 call assert_equal(1, g:V_to_v)
3631 call assert_equal(len(g:mode_seq) - 1, g:index)
3632
3633 let g:n_to_i = 0
3634 au ModeChanged n:i let g:n_to_i += 1
3635 let g:n_to_niI = 0
3636 au ModeChanged i:niI let g:n_to_niI += 1
3637 let g:niI_to_i = 0
3638 au ModeChanged niI:i let g:niI_to_i += 1
3639 let g:nany_to_i = 0
3640 au ModeChanged n*:i let g:nany_to_i += 1
3641 let g:i_to_n = 0
3642 au ModeChanged i:n let g:i_to_n += 1
3643 let g:nori_to_any = 0
3644 au ModeChanged [ni]:* let g:nori_to_any += 1
3645 let g:i_to_any = 0
3646 au ModeChanged i:* let g:i_to_any += 1
3647 let g:index = 0
3648 let g:mode_seq = ['n', 'i', 'niI', 'i', 'n']
3649 call feedkeys("a\<C-O>l\<esc>", 'tnix')
3650 call assert_equal(len(g:mode_seq) - 1, g:index)
3651 call assert_equal(1, g:n_to_i)
3652 call assert_equal(1, g:n_to_niI)
3653 call assert_equal(1, g:niI_to_i)
3654 call assert_equal(2, g:nany_to_i)
3655 call assert_equal(1, g:i_to_n)
3656 call assert_equal(2, g:i_to_any)
3657 call assert_equal(3, g:nori_to_any)
3658
3659 if has('terminal')
3660 let g:mode_seq += ['c', 'n', 't', 'nt', 'c', 'nt', 'n']
3661 call feedkeys(":term\<CR>\<C-W>N:bd!\<CR>", 'tnix')
3662 call assert_equal(len(g:mode_seq) - 1, g:index)
3663 call assert_equal(1, g:n_to_i)
3664 call assert_equal(1, g:n_to_niI)
3665 call assert_equal(1, g:niI_to_i)
3666 call assert_equal(2, g:nany_to_i)
3667 call assert_equal(1, g:i_to_n)
3668 call assert_equal(2, g:i_to_any)
3669 call assert_equal(5, g:nori_to_any)
3670 endif
3671
zeertzjqd1955982022-10-05 11:24:46 +01003672 let g:n_to_c = 0
3673 au ModeChanged n:c let g:n_to_c += 1
3674 let g:c_to_n = 0
3675 au ModeChanged c:n let g:c_to_n += 1
3676 let g:mode_seq += ['c', 'n', 'c', 'n']
3677 call feedkeys("q:\<C-C>\<Esc>", 'tnix')
3678 call assert_equal(len(g:mode_seq) - 1, g:index)
3679 call assert_equal(2, g:n_to_c)
3680 call assert_equal(2, g:c_to_n)
3681 unlet g:n_to_c
3682 unlet g:c_to_n
zeertzjqc9e8fd62022-07-26 18:12:38 +01003683
Bram Moolenaar61c4b042022-10-18 15:10:11 +01003684 let g:n_to_v = 0
3685 au ModeChanged n:v let g:n_to_v += 1
3686 let g:v_to_n = 0
3687 au ModeChanged v:n let g:v_to_n += 1
3688 let g:mode_seq += ['v', 'n']
3689 call feedkeys("v\<C-C>", 'tnix')
3690 call assert_equal(len(g:mode_seq) - 1, g:index)
3691 call assert_equal(1, g:n_to_v)
3692 call assert_equal(1, g:v_to_n)
3693 unlet g:n_to_v
3694 unlet g:v_to_n
3695
zeertzjqc9e8fd62022-07-26 18:12:38 +01003696 au! ModeChanged
3697 delfunc TestMode
3698 unlet! g:mode_seq
3699 unlet! g:index
3700 unlet! g:n_to_any
3701 unlet! g:V_to_v
3702 unlet! g:n_to_i
3703 unlet! g:n_to_niI
3704 unlet! g:niI_to_i
3705 unlet! g:nany_to_i
3706 unlet! g:i_to_n
3707 unlet! g:nori_to_any
3708 unlet! g:i_to_any
3709endfunc
3710
3711func Test_recursive_ModeChanged()
3712 au! ModeChanged * norm 0u
3713 sil! norm 
3714 au! ModeChanged
3715endfunc
3716
3717func Test_ModeChanged_starts_visual()
3718 " This was triggering ModeChanged before setting VIsual, causing a crash.
3719 au! ModeChanged * norm 0u
3720 sil! norm 
3721
3722 au! ModeChanged
3723endfunc
Bram Moolenaar347538f2022-03-26 16:28:06 +00003724
Charlie Grovesfef44852022-04-19 16:24:12 +01003725func Test_noname_autocmd()
3726 augroup test_noname_autocmd_group
3727 autocmd!
3728 autocmd BufEnter * call add(s:li, ["BufEnter", expand("<afile>")])
3729 autocmd BufDelete * call add(s:li, ["BufDelete", expand("<afile>")])
3730 autocmd BufLeave * call add(s:li, ["BufLeave", expand("<afile>")])
3731 autocmd BufUnload * call add(s:li, ["BufUnload", expand("<afile>")])
3732 autocmd BufWipeout * call add(s:li, ["BufWipeout", expand("<afile>")])
3733 augroup END
3734
3735 let s:li = []
3736 edit foo
3737 call assert_equal([['BufUnload', ''], ['BufDelete', ''], ['BufWipeout', ''], ['BufEnter', 'foo']], s:li)
3738
3739 au! test_noname_autocmd_group
3740 augroup! test_noname_autocmd_group
3741endfunc
3742
Yegappan Lakshmanan1755a912022-05-19 10:31:47 +01003743" Test for the autocmd_get() function
3744func Test_autocmd_get()
3745 augroup TestAutoCmdFns
3746 au!
3747 autocmd BufAdd *.vim echo "bufadd-vim"
3748 autocmd BufAdd *.py echo "bufadd-py"
3749 autocmd BufHidden *.vim echo "bufhidden"
3750 augroup END
3751 augroup TestAutoCmdFns2
3752 autocmd BufAdd *.vim echo "bufadd-vim-2"
3753 autocmd BufRead *.a1b2c3 echo "bufadd-vim-2"
3754 augroup END
3755
3756 let l = autocmd_get()
3757 call assert_true(l->len() > 0)
3758
3759 " Test for getting all the autocmds in a group
3760 let expected = [
3761 \ #{cmd: 'echo "bufadd-vim"', group: 'TestAutoCmdFns',
3762 \ pattern: '*.vim', nested: v:false, once: v:false,
3763 \ event: 'BufAdd'},
3764 \ #{cmd: 'echo "bufadd-py"', group: 'TestAutoCmdFns',
3765 \ pattern: '*.py', nested: v:false, once: v:false,
3766 \ event: 'BufAdd'},
3767 \ #{cmd: 'echo "bufhidden"', group: 'TestAutoCmdFns',
3768 \ pattern: '*.vim', nested: v:false,
3769 \ once: v:false, event: 'BufHidden'}]
3770 call assert_equal(expected, autocmd_get(#{group: 'TestAutoCmdFns'}))
3771
3772 " Test for getting autocmds for all the patterns in a group
3773 call assert_equal(expected, autocmd_get(#{group: 'TestAutoCmdFns',
3774 \ event: '*'}))
3775
3776 " Test for getting autocmds for an event in a group
3777 let expected = [
3778 \ #{cmd: 'echo "bufadd-vim"', group: 'TestAutoCmdFns',
3779 \ pattern: '*.vim', nested: v:false, once: v:false,
3780 \ event: 'BufAdd'},
3781 \ #{cmd: 'echo "bufadd-py"', group: 'TestAutoCmdFns',
3782 \ pattern: '*.py', nested: v:false, once: v:false,
3783 \ event: 'BufAdd'}]
3784 call assert_equal(expected, autocmd_get(#{group: 'TestAutoCmdFns',
3785 \ event: 'BufAdd'}))
3786
3787 " Test for getting the autocmds for all the events in a group for particular
3788 " pattern
3789 call assert_equal([{'cmd': 'echo "bufadd-py"', 'group': 'TestAutoCmdFns',
3790 \ 'pattern': '*.py', 'nested': v:false, 'once': v:false,
3791 \ 'event': 'BufAdd'}],
3792 \ autocmd_get(#{group: 'TestAutoCmdFns', event: '*', pattern: '*.py'}))
3793
3794 " Test for getting the autocmds for an events in a group for particular
3795 " pattern
3796 let l = autocmd_get(#{group: 'TestAutoCmdFns', event: 'BufAdd',
3797 \ pattern: '*.vim'})
3798 call assert_equal([
3799 \ #{cmd: 'echo "bufadd-vim"', group: 'TestAutoCmdFns',
3800 \ pattern: '*.vim', nested: v:false, once: v:false,
3801 \ event: 'BufAdd'}], l)
3802
3803 " Test for getting the autocmds for a pattern in a group
3804 let l = autocmd_get(#{group: 'TestAutoCmdFns', pattern: '*.vim'})
3805 call assert_equal([
3806 \ #{cmd: 'echo "bufadd-vim"', group: 'TestAutoCmdFns',
3807 \ pattern: '*.vim', nested: v:false, once: v:false,
3808 \ event: 'BufAdd'},
3809 \ #{cmd: 'echo "bufhidden"', group: 'TestAutoCmdFns',
3810 \ pattern: '*.vim', nested: v:false,
3811 \ once: v:false, event: 'BufHidden'}], l)
3812
3813 " Test for getting the autocmds for a pattern in all the groups
3814 let l = autocmd_get(#{pattern: '*.a1b2c3'})
3815 call assert_equal([{'cmd': 'echo "bufadd-vim-2"', 'group': 'TestAutoCmdFns2',
3816 \ 'pattern': '*.a1b2c3', 'nested': v:false, 'once': v:false,
3817 \ 'event': 'BufRead'}], l)
3818
3819 " Test for getting autocmds for a pattern without any autocmds
3820 call assert_equal([], autocmd_get(#{group: 'TestAutoCmdFns',
3821 \ pattern: '*.abc'}))
3822 call assert_equal([], autocmd_get(#{group: 'TestAutoCmdFns',
3823 \ event: 'BufAdd', pattern: '*.abc'}))
3824 call assert_equal([], autocmd_get(#{group: 'TestAutoCmdFns',
3825 \ event: 'BufWipeout'}))
3826 call assert_fails("call autocmd_get(#{group: 'abc', event: 'BufAdd'})",
3827 \ 'E367:')
3828 let cmd = "echo autocmd_get(#{group: 'TestAutoCmdFns', event: 'abc'})"
3829 call assert_fails(cmd, 'E216:')
3830 call assert_fails("call autocmd_get(#{group: 'abc'})", 'E367:')
3831 call assert_fails("echo autocmd_get(#{event: 'abc'})", 'E216:')
3832
3833 augroup TestAutoCmdFns
3834 au!
3835 augroup END
3836 call assert_equal([], autocmd_get(#{group: 'TestAutoCmdFns'}))
3837
3838 " Test for nested and once autocmds
3839 augroup TestAutoCmdFns
3840 au!
3841 autocmd VimSuspend * ++nested echo "suspend"
3842 autocmd VimResume * ++once echo "resume"
3843 augroup END
3844
3845 let expected = [
3846 \ {'cmd': 'echo "suspend"', 'group': 'TestAutoCmdFns', 'pattern': '*',
3847 \ 'nested': v:true, 'once': v:false, 'event': 'VimSuspend'},
3848 \ {'cmd': 'echo "resume"', 'group': 'TestAutoCmdFns', 'pattern': '*',
3849 \ 'nested': v:false, 'once': v:true, 'event': 'VimResume'}]
3850 call assert_equal(expected, autocmd_get(#{group: 'TestAutoCmdFns'}))
3851
3852 " Test for buffer-local autocmd
3853 augroup TestAutoCmdFns
3854 au!
3855 autocmd TextYankPost <buffer> echo "textyankpost"
3856 augroup END
3857
3858 let expected = [
3859 \ {'cmd': 'echo "textyankpost"', 'group': 'TestAutoCmdFns',
3860 \ 'pattern': '<buffer=' .. bufnr() .. '>', 'nested': v:false,
3861 \ 'once': v:false, 'bufnr': bufnr(), 'event': 'TextYankPost'}]
3862 call assert_equal(expected, autocmd_get(#{group: 'TestAutoCmdFns'}))
3863
3864 augroup TestAutoCmdFns
3865 au!
3866 augroup END
3867 augroup! TestAutoCmdFns
3868 augroup TestAutoCmdFns2
3869 au!
3870 augroup END
3871 augroup! TestAutoCmdFns2
3872
3873 call assert_fails("echo autocmd_get(#{group: []})", 'E730:')
3874 call assert_fails("echo autocmd_get(#{event: {}})", 'E731:')
3875 call assert_fails("echo autocmd_get([])", 'E1206:')
3876endfunc
3877
3878" Test for the autocmd_add() function
3879func Test_autocmd_add()
3880 " Define a single autocmd in a group
3881 call autocmd_add([#{group: 'TestAcSet', event: 'BufAdd', pattern: '*.sh',
3882 \ cmd: 'echo "bufadd"', once: v:true, nested: v:true}])
3883 call assert_equal([#{cmd: 'echo "bufadd"', group: 'TestAcSet',
3884 \ pattern: '*.sh', nested: v:true, once: v:true,
3885 \ event: 'BufAdd'}], autocmd_get(#{group: 'TestAcSet'}))
3886
3887 " Define two autocmds in the same group
3888 call autocmd_delete([#{group: 'TestAcSet'}])
3889 call autocmd_add([#{group: 'TestAcSet', event: 'BufAdd', pattern: '*.sh',
3890 \ cmd: 'echo "bufadd"'},
3891 \ #{group: 'TestAcSet', event: 'BufEnter', pattern: '*.sh',
3892 \ cmd: 'echo "bufenter"'}])
3893 call assert_equal([
3894 \ #{cmd: 'echo "bufadd"', group: 'TestAcSet', pattern: '*.sh',
3895 \ nested: v:false, once: v:false, event: 'BufAdd'},
3896 \ #{cmd: 'echo "bufenter"', group: 'TestAcSet', pattern: '*.sh',
3897 \ nested: v:false, once: v:false, event: 'BufEnter'}],
3898 \ autocmd_get(#{group: 'TestAcSet'}))
3899
3900 " Define a buffer-local autocmd
3901 call autocmd_delete([#{group: 'TestAcSet'}])
3902 call autocmd_add([#{group: 'TestAcSet', event: 'CursorHold',
3903 \ bufnr: bufnr(), cmd: 'echo "cursorhold"'}])
3904 call assert_equal([
3905 \ #{cmd: 'echo "cursorhold"', group: 'TestAcSet',
3906 \ pattern: '<buffer=' .. bufnr() .. '>', nested: v:false,
3907 \ once: v:false, bufnr: bufnr(), event: 'CursorHold'}],
3908 \ autocmd_get(#{group: 'TestAcSet'}))
3909
3910 " Use an invalid buffer number
3911 call autocmd_delete([#{group: 'TestAcSet'}])
3912 call autocmd_add([#{group: 'TestAcSet', event: 'BufEnter',
3913 \ bufnr: -1, cmd: 'echo "bufenter"'}])
3914 let l = [#{group: 'TestAcSet', event: 'BufAdd', bufnr: 9999,
3915 \ cmd: 'echo "bufadd"'}]
3916 call assert_fails("echo autocmd_add(l)", 'E680:')
Yegappan Lakshmanan00e977c2022-06-01 12:31:53 +01003917 let l = [#{group: 'TestAcSet', event: 'BufAdd', bufnr: 9999,
3918 \ pattern: '*.py', cmd: 'echo "bufadd"'}]
3919 call assert_fails("echo autocmd_add(l)", 'E680:')
3920 let l = [#{group: 'TestAcSet', event: 'BufAdd', bufnr: 9999,
3921 \ pattern: ['*.py', '*.c'], cmd: 'echo "bufadd"'}]
3922 call assert_fails("echo autocmd_add(l)", 'E680:')
Yegappan Lakshmanan1755a912022-05-19 10:31:47 +01003923 let l = [#{group: 'TestAcSet', event: 'BufRead', bufnr: [],
3924 \ cmd: 'echo "bufread"'}]
3925 call assert_fails("echo autocmd_add(l)", 'E745:')
3926 call assert_equal([], autocmd_get(#{group: 'TestAcSet'}))
3927
3928 " Add two commands to the same group, event and pattern
3929 call autocmd_delete([#{group: 'TestAcSet'}])
3930 call autocmd_add([#{group: 'TestAcSet', event: 'BufUnload',
3931 \ pattern: 'abc', cmd: 'echo "cmd1"'}])
3932 call autocmd_add([#{group: 'TestAcSet', event: 'BufUnload',
3933 \ pattern: 'abc', cmd: 'echo "cmd2"'}])
3934 call assert_equal([
3935 \ #{cmd: 'echo "cmd1"', group: 'TestAcSet', pattern: 'abc',
3936 \ nested: v:false, once: v:false, event: 'BufUnload'},
3937 \ #{cmd: 'echo "cmd2"', group: 'TestAcSet', pattern: 'abc',
3938 \ nested: v:false, once: v:false, event: 'BufUnload'}],
3939 \ autocmd_get(#{group: 'TestAcSet'}))
3940
3941 " When adding a new autocmd, if the autocmd 'group' is not specified, then
3942 " the current autocmd group should be used.
3943 call autocmd_delete([#{group: 'TestAcSet'}])
3944 augroup TestAcSet
3945 call autocmd_add([#{event: 'BufHidden', pattern: 'abc', cmd: 'echo "abc"'}])
3946 augroup END
3947 call assert_equal([
3948 \ #{cmd: 'echo "abc"', group: 'TestAcSet', pattern: 'abc',
3949 \ nested: v:false, once: v:false, event: 'BufHidden'}],
3950 \ autocmd_get(#{group: 'TestAcSet'}))
3951
Yegappan Lakshmanan971f6822022-05-24 11:40:11 +01003952 " Test for replacing a cmd for an event in a group
3953 call autocmd_delete([#{group: 'TestAcSet'}])
3954 call autocmd_add([#{replace: v:true, group: 'TestAcSet', event: 'BufEnter',
3955 \ pattern: '*.py', cmd: 'echo "bufenter"'}])
3956 call autocmd_add([#{replace: v:true, group: 'TestAcSet', event: 'BufEnter',
3957 \ pattern: '*.py', cmd: 'echo "bufenter"'}])
3958 call assert_equal([
3959 \ #{cmd: 'echo "bufenter"', group: 'TestAcSet', pattern: '*.py',
3960 \ nested: v:false, once: v:false, event: 'BufEnter'}],
3961 \ autocmd_get(#{group: 'TestAcSet'}))
3962
3963 " Test for adding a command for an unsupported autocmd event
Yegappan Lakshmanan1755a912022-05-19 10:31:47 +01003964 let l = [#{group: 'TestAcSet', event: 'abc', pattern: '*.sh',
3965 \ cmd: 'echo "bufadd"'}]
3966 call assert_fails('call autocmd_add(l)', 'E216:')
3967
Yegappan Lakshmanane0ff3a72022-05-27 18:05:33 +01003968 " Test for using a list of events and patterns
3969 call autocmd_delete([#{group: 'TestAcSet'}])
3970 let l = [#{group: 'TestAcSet', event: ['BufEnter', 'BufLeave'],
3971 \ pattern: ['*.py', '*.sh'], cmd: 'echo "bufcmds"'}]
3972 call autocmd_add(l)
3973 call assert_equal([
3974 \ #{cmd: 'echo "bufcmds"', group: 'TestAcSet', pattern: '*.py',
3975 \ nested: v:false, once: v:false, event: 'BufEnter'},
3976 \ #{cmd: 'echo "bufcmds"', group: 'TestAcSet', pattern: '*.sh',
3977 \ nested: v:false, once: v:false, event: 'BufEnter'},
3978 \ #{cmd: 'echo "bufcmds"', group: 'TestAcSet', pattern: '*.py',
3979 \ nested: v:false, once: v:false, event: 'BufLeave'},
3980 \ #{cmd: 'echo "bufcmds"', group: 'TestAcSet', pattern: '*.sh',
3981 \ nested: v:false, once: v:false, event: 'BufLeave'}],
3982 \ autocmd_get(#{group: 'TestAcSet'}))
3983
3984 " Test for invalid values for 'event' item
3985 call autocmd_delete([#{group: 'TestAcSet'}])
3986 let l = [#{group: 'TestAcSet', event: test_null_string(),
3987 \ pattern: "*.py", cmd: 'echo "bufcmds"'}]
3988 call assert_fails('call autocmd_add(l)', 'E928:')
3989 let l = [#{group: 'TestAcSet', event: test_null_list(),
3990 \ pattern: "*.py", cmd: 'echo "bufcmds"'}]
3991 call assert_fails('call autocmd_add(l)', 'E714:')
3992 let l = [#{group: 'TestAcSet', event: {},
3993 \ pattern: "*.py", cmd: 'echo "bufcmds"'}]
3994 call assert_fails('call autocmd_add(l)', 'E777:')
3995 let l = [#{group: 'TestAcSet', event: [{}],
3996 \ pattern: "*.py", cmd: 'echo "bufcmds"'}]
3997 call assert_fails('call autocmd_add(l)', 'E928:')
3998 let l = [#{group: 'TestAcSet', event: [test_null_string()],
3999 \ pattern: "*.py", cmd: 'echo "bufcmds"'}]
4000 call assert_fails('call autocmd_add(l)', 'E928:')
4001 let l = [#{group: 'TestAcSet', event: 'BufEnter,BufLeave',
4002 \ pattern: '*.py', cmd: 'echo "bufcmds"'}]
4003 call assert_fails('call autocmd_add(l)', 'E216:')
4004 let l = [#{group: 'TestAcSet', event: [],
4005 \ pattern: "*.py", cmd: 'echo "bufcmds"'}]
4006 call autocmd_add(l)
4007 let l = [#{group: 'TestAcSet', event: [""],
4008 \ pattern: "*.py", cmd: 'echo "bufcmds"'}]
4009 call assert_fails('call autocmd_add(l)', 'E216:')
4010 let l = [#{group: 'TestAcSet', event: "",
4011 \ pattern: "*.py", cmd: 'echo "bufcmds"'}]
4012 call autocmd_add(l)
4013 call assert_equal([], autocmd_get(#{group: 'TestAcSet'}))
4014
4015 " Test for invalid values for 'pattern' item
4016 let l = [#{group: 'TestAcSet', event: "BufEnter",
4017 \ pattern: test_null_string(), cmd: 'echo "bufcmds"'}]
Yegappan Lakshmanan00e977c2022-06-01 12:31:53 +01004018 call assert_fails('call autocmd_add(l)', 'E928:')
Yegappan Lakshmanane0ff3a72022-05-27 18:05:33 +01004019 let l = [#{group: 'TestAcSet', event: "BufEnter",
4020 \ pattern: test_null_list(), cmd: 'echo "bufcmds"'}]
4021 call assert_fails('call autocmd_add(l)', 'E714:')
4022 let l = [#{group: 'TestAcSet', event: "BufEnter",
4023 \ pattern: {}, cmd: 'echo "bufcmds"'}]
4024 call assert_fails('call autocmd_add(l)', 'E777:')
4025 let l = [#{group: 'TestAcSet', event: "BufEnter",
4026 \ pattern: [{}], cmd: 'echo "bufcmds"'}]
4027 call assert_fails('call autocmd_add(l)', 'E928:')
4028 let l = [#{group: 'TestAcSet', event: "BufEnter",
4029 \ pattern: [test_null_string()], cmd: 'echo "bufcmds"'}]
4030 call assert_fails('call autocmd_add(l)', 'E928:')
4031 let l = [#{group: 'TestAcSet', event: "BufEnter",
4032 \ pattern: [], cmd: 'echo "bufcmds"'}]
4033 call autocmd_add(l)
4034 let l = [#{group: 'TestAcSet', event: "BufEnter",
4035 \ pattern: [""], cmd: 'echo "bufcmds"'}]
4036 call autocmd_add(l)
4037 let l = [#{group: 'TestAcSet', event: "BufEnter",
4038 \ pattern: "", cmd: 'echo "bufcmds"'}]
4039 call autocmd_add(l)
4040 call assert_equal([], autocmd_get(#{group: 'TestAcSet'}))
4041
4042 let l = [#{group: 'TestAcSet', event: 'BufEnter,abc,BufLeave',
4043 \ pattern: '*.py', cmd: 'echo "bufcmds"'}]
4044 call assert_fails('call autocmd_add(l)', 'E216:')
4045
Yegappan Lakshmanan1755a912022-05-19 10:31:47 +01004046 call assert_fails("call autocmd_add({})", 'E1211:')
4047 call assert_equal(v:false, autocmd_add(test_null_list()))
4048 call assert_true(autocmd_add([[]]))
4049 call assert_true(autocmd_add([test_null_dict()]))
4050
4051 augroup TestAcSet
4052 au!
4053 augroup END
4054
4055 call autocmd_add([#{group: 'TestAcSet'}])
4056 call autocmd_add([#{group: 'TestAcSet', event: 'BufAdd'}])
4057 call autocmd_add([#{group: 'TestAcSet', pat: '*.sh'}])
4058 call autocmd_add([#{group: 'TestAcSet', cmd: 'echo "a"'}])
4059 call autocmd_add([#{group: 'TestAcSet', event: 'BufAdd', pat: '*.sh'}])
4060 call autocmd_add([#{group: 'TestAcSet', event: 'BufAdd', cmd: 'echo "a"'}])
4061 call autocmd_add([#{group: 'TestAcSet', pat: '*.sh', cmd: 'echo "a"'}])
4062 call assert_equal([], autocmd_get(#{group: 'TestAcSet'}))
4063
4064 augroup! TestAcSet
4065endfunc
4066
4067" Test for deleting autocmd events and groups
4068func Test_autocmd_delete()
4069 " Delete an event in an autocmd group
4070 augroup TestAcSet
4071 au!
4072 au BufAdd *.sh echo "bufadd"
4073 au BufEnter *.sh echo "bufenter"
4074 augroup END
4075 call autocmd_delete([#{group: 'TestAcSet', event: 'BufAdd'}])
4076 call assert_equal([#{cmd: 'echo "bufenter"', group: 'TestAcSet',
4077 \ pattern: '*.sh', nested: v:false, once: v:false,
4078 \ event: 'BufEnter'}], autocmd_get(#{group: 'TestAcSet'}))
4079
4080 " Delete all the events in an autocmd group
4081 augroup TestAcSet
4082 au BufAdd *.sh echo "bufadd"
4083 augroup END
4084 call autocmd_delete([#{group: 'TestAcSet', event: '*'}])
4085 call assert_equal([], autocmd_get(#{group: 'TestAcSet'}))
4086
4087 " Delete a non-existing autocmd group
4088 call assert_fails("call autocmd_delete([#{group: 'abc'}])", 'E367:')
4089 " Delete a non-existing autocmd event
4090 let l = [#{group: 'TestAcSet', event: 'abc'}]
4091 call assert_fails("call autocmd_delete(l)", 'E216:')
4092 " Delete a non-existing autocmd pattern
4093 let l = [#{group: 'TestAcSet', event: 'BufAdd', pat: 'abc'}]
4094 call assert_true(autocmd_delete(l))
Yegappan Lakshmanan00e977c2022-06-01 12:31:53 +01004095 " Delete an autocmd for a non-existing buffer
4096 let l = [#{event: '*', bufnr: 9999, cmd: 'echo "x"'}]
4097 call assert_fails('call autocmd_delete(l)', 'E680:')
Yegappan Lakshmanan1755a912022-05-19 10:31:47 +01004098
4099 " Delete an autocmd group
4100 augroup TestAcSet
4101 au!
4102 au BufAdd *.sh echo "bufadd"
4103 au BufEnter *.sh echo "bufenter"
4104 augroup END
4105 call autocmd_delete([#{group: 'TestAcSet'}])
4106 call assert_fails("call autocmd_get(#{group: 'TestAcSet'})", 'E367:')
4107
4108 call assert_true(autocmd_delete([[]]))
4109 call assert_true(autocmd_delete([test_null_dict()]))
4110endfunc
4111
Bram Moolenaar8f3c3c62022-10-18 17:05:54 +01004112func Test_autocmd_split_dummy()
4113 " Autocommand trying to split a window containing a dummy buffer.
4114 auto BufReadPre * exe "sbuf " .. expand("<abuf>")
4115 " Avoid the "W11" prompt
4116 au FileChangedShell * let v:fcs_choice = 'reload'
4117 func Xautocmd_changelist()
4118 cal writefile(['Xtestfile2:4:4'], 'Xerr')
4119 edit Xerr
4120 lex 'Xtestfile2:4:4'
4121 endfunc
4122 call Xautocmd_changelist()
Bram Moolenaar53c5c9f2022-10-18 17:25:03 +01004123 " Should get E86, but it doesn't always happen (timing?)
4124 silent! call Xautocmd_changelist()
Bram Moolenaar8f3c3c62022-10-18 17:05:54 +01004125
4126 au! BufReadPre
4127 au! FileChangedShell
4128 delfunc Xautocmd_changelist
4129 bwipe! Xerr
4130 call delete('Xerr')
4131endfunc
4132
Bram Moolenaare76062c2022-11-28 18:51:43 +00004133" This was crashing because there was only one window to execute autocommands
4134" in.
4135func Test_autocmd_nested_setbufvar()
4136 CheckFeature python3
4137
4138 set hidden
4139 edit Xaaa
4140 edit Xbbb
4141 call setline(1, 'bar')
4142 enew
4143 au BufWriteCmd Xbbb ++nested call setbufvar('Xaaa', '&ft', 'foo') | bw! Xaaa
4144 au FileType foo call py3eval('vim.current.buffer.options["cindent"]')
4145 wall
4146
4147 au! BufWriteCmd
4148 au! FileType foo
4149 set nohidden
4150 call delete('Xaaa')
4151 call delete('Xbbb')
4152 %bwipe!
4153endfunc
4154
4155
Bram Moolenaarbc2b71d2020-02-17 21:33:30 +01004156" vim: shiftwidth=2 sts=2 expandtab