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