blob: bcd4c53d2af40c4fd346562d33fd687f0cb846c3 [file] [log] [blame]
Bram Moolenaar14735512016-03-26 21:00:08 +01001" Tests for autocommands
2
Bram Moolenaar8c64a362018-03-23 22:39:31 +01003source shared.vim
Bram Moolenaar8c5a2782019-08-07 23:07:07 +02004source check.vim
Bram Moolenaarcadbe1b2019-09-22 21:50:09 +02005source term_util.vim
LemonBoy09371822022-04-08 15:18:45 +01006source screendump.vim
Bram Moolenaar6f2465d2022-03-22 18:13:01 +00007import './vim9.vim' as v9
Bram Moolenaar8c64a362018-03-23 22:39:31 +01008
Bram Moolenaar1e115362019-01-09 23:01:02 +01009func s:cleanup_buffers() abort
Bram Moolenaarb3435b02016-09-29 20:54:59 +020010 for bnr in range(1, bufnr('$'))
11 if bufloaded(bnr) && bufnr('%') != bnr
12 execute 'bd! ' . bnr
13 endif
14 endfor
Bram Moolenaar04f62f82017-07-19 18:18:39 +020015endfunc
Bram Moolenaarb3435b02016-09-29 20:54:59 +020016
Bram Moolenaar14735512016-03-26 21:00:08 +010017func Test_vim_did_enter()
18 call assert_false(v:vim_did_enter)
19
20 " This script will never reach the main loop, can't check if v:vim_did_enter
21 " becomes one.
22endfunc
Bram Moolenaar40b1b542016-04-20 20:18:23 +020023
Bram Moolenaar75911162020-07-21 19:44:47 +020024" Test for the CursorHold autocmd
25func Test_CursorHold_autocmd()
26 CheckRunVimInTerminal
Bram Moolenaare1f3ab72022-09-04 21:29:08 +010027 call writefile(['one', 'two', 'three'], 'XoneTwoThree', 'D')
Bram Moolenaar75911162020-07-21 19:44:47 +020028 let before =<< trim END
29 set updatetime=10
Bram Moolenaare7cda972022-08-29 11:02:59 +010030 au CursorHold * call writefile([line('.')], 'XCHoutput', 'a')
Bram Moolenaar75911162020-07-21 19:44:47 +020031 END
Bram Moolenaare1f3ab72022-09-04 21:29:08 +010032 call writefile(before, 'XCHinit', 'D')
Bram Moolenaare7cda972022-08-29 11:02:59 +010033 let buf = RunVimInTerminal('-S XCHinit XoneTwoThree', {})
Bram Moolenaar17f67542020-08-20 18:29:13 +020034 call term_sendkeys(buf, "G")
Bram Moolenaar62cd26a2020-10-11 20:08:44 +020035 call term_wait(buf, 50)
Bram Moolenaar75911162020-07-21 19:44:47 +020036 call term_sendkeys(buf, "gg")
37 call term_wait(buf)
Bram Moolenaare7cda972022-08-29 11:02:59 +010038 call WaitForAssert({-> assert_equal(['1'], readfile('XCHoutput')[-1:-1])})
Bram Moolenaar75911162020-07-21 19:44:47 +020039 call term_sendkeys(buf, "j")
40 call term_wait(buf)
Bram Moolenaare7cda972022-08-29 11:02:59 +010041 call WaitForAssert({-> assert_equal(['1', '2'], readfile('XCHoutput')[-2:-1])})
Bram Moolenaar75911162020-07-21 19:44:47 +020042 call term_sendkeys(buf, "j")
43 call term_wait(buf)
Bram Moolenaare7cda972022-08-29 11:02:59 +010044 call WaitForAssert({-> assert_equal(['1', '2', '3'], readfile('XCHoutput')[-3:-1])})
Bram Moolenaar75911162020-07-21 19:44:47 +020045 call StopVimInTerminal(buf)
46
Bram Moolenaare7cda972022-08-29 11:02:59 +010047 call delete('XCHoutput')
Bram Moolenaar75911162020-07-21 19:44:47 +020048endfunc
49
Bram Moolenaarc67e8922016-05-24 16:07:40 +020050if has('timers')
Bram Moolenaar97b00752019-05-12 13:07:14 +020051
Bram Moolenaarc67e8922016-05-24 16:07:40 +020052 func ExitInsertMode(id)
53 call feedkeys("\<Esc>")
54 endfunc
55
56 func Test_cursorhold_insert()
Bram Moolenaarf18c4db2016-09-08 22:10:06 +020057 " Need to move the cursor.
58 call feedkeys("ggG", "xt")
59
Bram Moolenaarc67e8922016-05-24 16:07:40 +020060 let g:triggered = 0
61 au CursorHoldI * let g:triggered += 1
62 set updatetime=20
Bram Moolenaar92bb83e2021-02-03 23:04:46 +010063 call timer_start(200, 'ExitInsertMode')
Bram Moolenaarc67e8922016-05-24 16:07:40 +020064 call feedkeys('a', 'x!')
Bram Moolenaar3b014be2022-11-13 17:53:46 +000065 sleep 30m
Bram Moolenaarc67e8922016-05-24 16:07:40 +020066 call assert_equal(1, g:triggered)
Bram Moolenaar26d98212019-01-27 22:32:55 +010067 unlet g:triggered
68 au! CursorHoldI
69 set updatetime&
70 endfunc
71
72 func Test_cursorhold_insert_with_timer_interrupt()
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +020073 CheckFeature job
Bram Moolenaar26d98212019-01-27 22:32:55 +010074 " Need to move the cursor.
75 call feedkeys("ggG", "xt")
76
77 " Confirm the timer invoked in exit_cb of the job doesn't disturb
78 " CursorHoldI event.
79 let g:triggered = 0
80 au CursorHoldI * let g:triggered += 1
Bram Moolenaar62cd26a2020-10-11 20:08:44 +020081 set updatetime=100
Bram Moolenaar26d98212019-01-27 22:32:55 +010082 call job_start(has('win32') ? 'cmd /c echo:' : 'echo',
Bram Moolenaar62cd26a2020-10-11 20:08:44 +020083 \ {'exit_cb': {-> timer_start(200, 'ExitInsertMode')}})
Bram Moolenaar26d98212019-01-27 22:32:55 +010084 call feedkeys('a', 'x!')
85 call assert_equal(1, g:triggered)
86 unlet g:triggered
Bram Moolenaare99e8442016-07-26 20:43:40 +020087 au! CursorHoldI
Bram Moolenaaraeac9002016-09-06 22:15:08 +020088 set updatetime&
Bram Moolenaarc67e8922016-05-24 16:07:40 +020089 endfunc
90
91 func Test_cursorhold_insert_ctrl_x()
92 let g:triggered = 0
93 au CursorHoldI * let g:triggered += 1
94 set updatetime=20
95 call timer_start(100, 'ExitInsertMode')
96 " CursorHoldI does not trigger after CTRL-X
97 call feedkeys("a\<C-X>", 'x!')
98 call assert_equal(0, g:triggered)
Bram Moolenaar26d98212019-01-27 22:32:55 +010099 unlet g:triggered
Bram Moolenaare99e8442016-07-26 20:43:40 +0200100 au! CursorHoldI
Bram Moolenaaraeac9002016-09-06 22:15:08 +0200101 set updatetime&
Bram Moolenaarc67e8922016-05-24 16:07:40 +0200102 endfunc
Bram Moolenaar97b00752019-05-12 13:07:14 +0200103
Bram Moolenaar5a9357d2021-10-03 16:22:05 +0100104 func Test_cursorhold_insert_ctrl_g_U()
105 au CursorHoldI * :
106 set updatetime=20
107 new
108 call timer_start(100, { -> feedkeys("\<Left>foo\<Esc>", 't') })
109 call feedkeys("i()\<C-g>U", 'tx!')
110 sleep 200m
111 call assert_equal('(foo)', getline(1))
112 undo
113 call assert_equal('', getline(1))
114
115 bwipe!
116 au! CursorHoldI
117 set updatetime&
118 endfunc
119
Bram Moolenaar97b00752019-05-12 13:07:14 +0200120 func Test_OptionSet_modeline()
121 call test_override('starting', 1)
122 au! OptionSet
123 augroup set_tabstop
124 au OptionSet tabstop call timer_start(1, {-> execute("echo 'Handler called'", "")})
125 augroup END
Bram Moolenaare1f3ab72022-09-04 21:29:08 +0100126 call writefile(['vim: set ts=7 sw=5 :', 'something'], 'XoptionsetModeline', 'D')
Bram Moolenaar97b00752019-05-12 13:07:14 +0200127 set modeline
128 let v:errmsg = ''
129 call assert_fails('split XoptionsetModeline', 'E12:')
130 call assert_equal(7, &ts)
131 call assert_equal('', v:errmsg)
132
133 augroup set_tabstop
134 au!
135 augroup END
136 bwipe!
137 set ts&
Bram Moolenaar97b00752019-05-12 13:07:14 +0200138 call test_override('starting', 0)
139 endfunc
140
141endif "has('timers')
Bram Moolenaar40b1b542016-04-20 20:18:23 +0200142
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200143func Test_bufunload()
Bram Moolenaarc67e8922016-05-24 16:07:40 +0200144 augroup test_bufunload_group
145 autocmd!
146 autocmd BufUnload * call add(s:li, "bufunload")
147 autocmd BufDelete * call add(s:li, "bufdelete")
148 autocmd BufWipeout * call add(s:li, "bufwipeout")
149 augroup END
Bram Moolenaar40b1b542016-04-20 20:18:23 +0200150
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100151 let s:li = []
Bram Moolenaarc67e8922016-05-24 16:07:40 +0200152 new
153 setlocal bufhidden=
154 bunload
155 call assert_equal(["bufunload", "bufdelete"], s:li)
Bram Moolenaar40b1b542016-04-20 20:18:23 +0200156
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100157 let s:li = []
Bram Moolenaarc67e8922016-05-24 16:07:40 +0200158 new
159 setlocal bufhidden=delete
160 bunload
161 call assert_equal(["bufunload", "bufdelete"], s:li)
162
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100163 let s:li = []
Bram Moolenaarc67e8922016-05-24 16:07:40 +0200164 new
165 setlocal bufhidden=unload
166 bwipeout
167 call assert_equal(["bufunload", "bufdelete", "bufwipeout"], s:li)
168
Bram Moolenaare99e8442016-07-26 20:43:40 +0200169 au! test_bufunload_group
Bram Moolenaarc67e8922016-05-24 16:07:40 +0200170 augroup! test_bufunload_group
Bram Moolenaar40b1b542016-04-20 20:18:23 +0200171endfunc
Bram Moolenaar30445cb2016-07-09 15:21:02 +0200172
173" SEGV occurs in older versions. (At least 7.4.2005 or older)
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200174func Test_autocmd_bufunload_with_tabnext()
Bram Moolenaar30445cb2016-07-09 15:21:02 +0200175 tabedit
176 tabfirst
177
178 augroup test_autocmd_bufunload_with_tabnext_group
179 autocmd!
180 autocmd BufUnload <buffer> tabnext
181 augroup END
182
183 quit
184 call assert_equal(2, tabpagenr('$'))
185
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200186 autocmd! test_autocmd_bufunload_with_tabnext_group
Bram Moolenaar30445cb2016-07-09 15:21:02 +0200187 augroup! test_autocmd_bufunload_with_tabnext_group
188 tablast
189 quit
190endfunc
Bram Moolenaarc917da42016-07-19 22:31:36 +0200191
Bram Moolenaar5ed58c72021-01-28 14:24:55 +0100192func Test_argdelete_in_next()
193 au BufNew,BufEnter,BufLeave,BufWinEnter * argdel
194 call assert_fails('next a b', 'E1156:')
195 au! BufNew,BufEnter,BufLeave,BufWinEnter *
196endfunc
197
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200198func Test_autocmd_bufwinleave_with_tabfirst()
Bram Moolenaarf9e687e2016-09-04 21:33:09 +0200199 tabedit
200 augroup sample
201 autocmd!
202 autocmd BufWinLeave <buffer> tabfirst
203 augroup END
204 call setline(1, ['a', 'b', 'c'])
205 edit! a.txt
Bram Moolenaarf18c4db2016-09-08 22:10:06 +0200206 tabclose
Bram Moolenaarf9e687e2016-09-04 21:33:09 +0200207endfunc
208
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200209" SEGV occurs in older versions. (At least 7.4.2321 or older)
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200210func Test_autocmd_bufunload_avoiding_SEGV_01()
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200211 split aa.txt
212 let lastbuf = bufnr('$')
213
214 augroup test_autocmd_bufunload
215 autocmd!
216 exe 'autocmd BufUnload <buffer> ' . (lastbuf + 1) . 'bwipeout!'
217 augroup END
218
Bram Moolenaar28ee8922020-10-28 20:20:00 +0100219 call assert_fails('edit bb.txt', 'E937:')
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200220
221 autocmd! test_autocmd_bufunload
222 augroup! test_autocmd_bufunload
223 bwipe! aa.txt
224 bwipe! bb.txt
225endfunc
226
227" SEGV occurs in older versions. (At least 7.4.2321 or older)
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200228func Test_autocmd_bufunload_avoiding_SEGV_02()
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200229 setlocal buftype=nowrite
230 let lastbuf = bufnr('$')
231
232 augroup test_autocmd_bufunload
233 autocmd!
234 exe 'autocmd BufUnload <buffer> ' . (lastbuf + 1) . 'bwipeout!'
235 augroup END
236
237 normal! i1
238 call assert_fails('edit a.txt', 'E517:')
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200239
240 autocmd! test_autocmd_bufunload
241 augroup! test_autocmd_bufunload
242 bwipe! a.txt
243endfunc
244
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100245func Test_autocmd_dummy_wipeout()
246 " prepare files
Bram Moolenaare1f3ab72022-09-04 21:29:08 +0100247 call writefile([''], 'Xdummywipetest1.txt', 'D')
248 call writefile([''], 'Xdummywipetest2.txt', 'D')
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100249 augroup test_bufunload_group
250 autocmd!
251 autocmd BufUnload * call add(s:li, "bufunload")
252 autocmd BufDelete * call add(s:li, "bufdelete")
253 autocmd BufWipeout * call add(s:li, "bufwipeout")
254 augroup END
255
256 let s:li = []
257 split Xdummywipetest1.txt
258 silent! vimgrep /notmatched/ Xdummywipetest*
259 call assert_equal(["bufunload", "bufwipeout"], s:li)
260
261 bwipeout
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100262 au! test_bufunload_group
263 augroup! test_bufunload_group
264endfunc
265
Bram Moolenaarc917da42016-07-19 22:31:36 +0200266func Test_win_tab_autocmd()
267 let g:record = []
268
269 augroup testing
270 au WinNew * call add(g:record, 'WinNew')
naohiro ono23beefe2021-11-13 12:38:49 +0000271 au WinClosed * call add(g:record, 'WinClosed')
Bram Moolenaarc917da42016-07-19 22:31:36 +0200272 au WinEnter * call add(g:record, 'WinEnter')
273 au WinLeave * call add(g:record, 'WinLeave')
274 au TabNew * call add(g:record, 'TabNew')
Bram Moolenaar12c11d52016-07-19 23:13:03 +0200275 au TabClosed * call add(g:record, 'TabClosed')
Bram Moolenaarc917da42016-07-19 22:31:36 +0200276 au TabEnter * call add(g:record, 'TabEnter')
277 au TabLeave * call add(g:record, 'TabLeave')
278 augroup END
279
280 split
281 tabnew
282 close
283 close
284
285 call assert_equal([
286 \ 'WinLeave', 'WinNew', 'WinEnter',
287 \ 'WinLeave', 'TabLeave', 'WinNew', 'WinEnter', 'TabNew', 'TabEnter',
naohiro ono23beefe2021-11-13 12:38:49 +0000288 \ 'WinLeave', 'TabLeave', 'WinClosed', 'TabClosed', 'WinEnter', 'TabEnter',
289 \ 'WinLeave', 'WinClosed', 'WinEnter'
Bram Moolenaarc917da42016-07-19 22:31:36 +0200290 \ ], g:record)
291
Bram Moolenaar12c11d52016-07-19 23:13:03 +0200292 let g:record = []
293 tabnew somefile
294 tabnext
295 bwipe somefile
296
297 call assert_equal([
298 \ 'WinLeave', 'TabLeave', 'WinNew', 'WinEnter', 'TabNew', 'TabEnter',
299 \ 'WinLeave', 'TabLeave', 'WinEnter', 'TabEnter',
naohiro ono23beefe2021-11-13 12:38:49 +0000300 \ 'WinClosed', 'TabClosed'
Bram Moolenaar12c11d52016-07-19 23:13:03 +0200301 \ ], g:record)
302
Bram Moolenaarc917da42016-07-19 22:31:36 +0200303 augroup testing
304 au!
305 augroup END
306 unlet g:record
307endfunc
Bram Moolenaare99e8442016-07-26 20:43:40 +0200308
LemonBoy09371822022-04-08 15:18:45 +0100309func Test_WinScrolled()
310 CheckRunVimInTerminal
311
312 let lines =<< trim END
zeertzjqd58862d2022-04-12 11:32:48 +0100313 set nowrap scrolloff=0
314 for ii in range(1, 18)
315 call setline(ii, repeat(nr2char(96 + ii), ii * 2))
316 endfor
317 let win_id = win_getid()
318 let g:matched = v:false
319 execute 'au WinScrolled' win_id 'let g:matched = v:true'
320 let g:scrolled = 0
321 au WinScrolled * let g:scrolled += 1
322 au WinScrolled * let g:amatch = str2nr(expand('<amatch>'))
323 au WinScrolled * let g:afile = str2nr(expand('<afile>'))
LemonBoy09371822022-04-08 15:18:45 +0100324 END
Bram Moolenaare1f3ab72022-09-04 21:29:08 +0100325 call writefile(lines, 'Xtest_winscrolled', 'D')
LemonBoy09371822022-04-08 15:18:45 +0100326 let buf = RunVimInTerminal('-S Xtest_winscrolled', {'rows': 6})
327
328 call term_sendkeys(buf, ":echo g:scrolled\<CR>")
329 call WaitForAssert({-> assert_match('^0 ', term_getline(buf, 6))}, 1000)
330
331 " Scroll left/right in Normal mode.
332 call term_sendkeys(buf, "zlzh:echo g:scrolled\<CR>")
333 call WaitForAssert({-> assert_match('^2 ', term_getline(buf, 6))}, 1000)
334
335 " Scroll up/down in Normal mode.
336 call term_sendkeys(buf, "\<c-e>\<c-y>:echo g:scrolled\<CR>")
337 call WaitForAssert({-> assert_match('^4 ', term_getline(buf, 6))}, 1000)
338
339 " Scroll up/down in Insert mode.
340 call term_sendkeys(buf, "Mi\<c-x>\<c-e>\<Esc>i\<c-x>\<c-y>\<Esc>")
341 call term_sendkeys(buf, ":echo g:scrolled\<CR>")
342 call WaitForAssert({-> assert_match('^6 ', term_getline(buf, 6))}, 1000)
343
344 " Scroll the window horizontally to focus the last letter of the third line
345 " containing only six characters. Moving to the previous and shorter lines
346 " should trigger another autocommand as Vim has to make them visible.
347 call term_sendkeys(buf, "5zl2k")
348 call term_sendkeys(buf, ":echo g:scrolled\<CR>")
349 call WaitForAssert({-> assert_match('^8 ', term_getline(buf, 6))}, 1000)
350
351 " Ensure the command was triggered for the specified window ID.
352 call term_sendkeys(buf, ":echo g:matched\<CR>")
353 call WaitForAssert({-> assert_match('^v:true ', term_getline(buf, 6))}, 1000)
354
355 " Ensure the expansion of <amatch> and <afile> matches the window ID.
356 call term_sendkeys(buf, ":echo g:amatch == win_id && g:afile == win_id\<CR>")
357 call WaitForAssert({-> assert_match('^v:true ', term_getline(buf, 6))}, 1000)
358
359 call StopVimInTerminal(buf)
LemonBoy09371822022-04-08 15:18:45 +0100360endfunc
361
LemonBoy66e13ae2022-04-21 22:52:11 +0100362func Test_WinScrolled_mouse()
363 CheckRunVimInTerminal
364
365 let lines =<< trim END
366 set nowrap scrolloff=0
367 set mouse=a term=xterm ttymouse=sgr mousetime=200 clipboard=
368 call setline(1, ['foo']->repeat(32))
369 split
370 let g:scrolled = 0
371 au WinScrolled * let g:scrolled += 1
372 END
Bram Moolenaare1f3ab72022-09-04 21:29:08 +0100373 call writefile(lines, 'Xtest_winscrolled_mouse', 'D')
LemonBoy66e13ae2022-04-21 22:52:11 +0100374 let buf = RunVimInTerminal('-S Xtest_winscrolled_mouse', {'rows': 10})
375
376 " With the upper split focused, send a scroll-down event to the unfocused one.
377 call test_setmouse(7, 1)
378 call term_sendkeys(buf, "\<ScrollWheelDown>")
379 call TermWait(buf)
380 call term_sendkeys(buf, ":echo g:scrolled\<CR>")
381 call WaitForAssert({-> assert_match('^1', term_getline(buf, 10))}, 1000)
382
383 " Again, but this time while we're in insert mode.
384 call term_sendkeys(buf, "i\<ScrollWheelDown>\<Esc>")
385 call TermWait(buf)
386 call term_sendkeys(buf, ":echo g:scrolled\<CR>")
387 call WaitForAssert({-> assert_match('^2', term_getline(buf, 10))}, 1000)
388
389 call StopVimInTerminal(buf)
LemonBoy66e13ae2022-04-21 22:52:11 +0100390endfunc
391
zeertzjqd58862d2022-04-12 11:32:48 +0100392func Test_WinScrolled_close_curwin()
393 CheckRunVimInTerminal
394
395 let lines =<< trim END
396 set nowrap scrolloff=0
397 call setline(1, ['aaa', 'bbb'])
398 vsplit
399 au WinScrolled * close
400 au VimLeave * call writefile(['123456'], 'Xtestout')
401 END
Bram Moolenaare1f3ab72022-09-04 21:29:08 +0100402 call writefile(lines, 'Xtest_winscrolled_close_curwin', 'D')
zeertzjqd58862d2022-04-12 11:32:48 +0100403 let buf = RunVimInTerminal('-S Xtest_winscrolled_close_curwin', {'rows': 6})
404
405 " This was using freed memory
406 call term_sendkeys(buf, "\<C-E>")
407 call TermWait(buf)
408 call StopVimInTerminal(buf)
409
Bram Moolenaar0a60f792022-11-19 21:18:11 +0000410 " check the startup script finished to the end
zeertzjqd58862d2022-04-12 11:32:48 +0100411 call assert_equal(['123456'], readfile('Xtestout'))
zeertzjqd58862d2022-04-12 11:32:48 +0100412 call delete('Xtestout')
413endfunc
414
Bram Moolenaar0a60f792022-11-19 21:18:11 +0000415func Test_WinScrolled_once_only()
416 CheckRunVimInTerminal
417
418 let lines =<< trim END
419 set cmdheight=2
420 call setline(1, ['aaa', 'bbb'])
421 let trigger_count = 0
422 func ShowInfo(id)
423 echo g:trigger_count g:winid winlayout()
424 endfunc
425
426 vsplit
427 split
428 " use a timer to show the info after a redraw
429 au WinScrolled * let trigger_count += 1 | let winid = expand('<amatch>') | call timer_start(100, 'ShowInfo')
430 wincmd j
431 wincmd l
432 END
433 call writefile(lines, 'Xtest_winscrolled_once', 'D')
434 let buf = RunVimInTerminal('-S Xtest_winscrolled_once', #{rows: 10, cols: 60, statusoff: 2})
435
436 call term_sendkeys(buf, "\<C-E>")
437 call VerifyScreenDump(buf, 'Test_winscrolled_once_only_1', {})
438
439 call StopVimInTerminal(buf)
440endfunc
441
Bram Moolenaar29967732022-11-20 12:11:45 +0000442" Check that WinScrolled is not triggered immediately when defined and there
443" are split windows.
444func Test_WinScrolled_not_when_defined()
445 CheckRunVimInTerminal
446
447 let lines =<< trim END
448 call setline(1, ['aaa', 'bbb'])
449 echo 'nothing happened'
450 func ShowTriggered(id)
451 echo 'triggered'
452 endfunc
453 END
454 call writefile(lines, 'Xtest_winscrolled_not', 'D')
455 let buf = RunVimInTerminal('-S Xtest_winscrolled_not', #{rows: 10, cols: 60, statusoff: 2})
456 call term_sendkeys(buf, ":split\<CR>")
457 call TermWait(buf)
458 " use a timer to show the message after redrawing
459 call term_sendkeys(buf, ":au WinScrolled * call timer_start(100, 'ShowTriggered')\<CR>")
460 call VerifyScreenDump(buf, 'Test_winscrolled_not_when_defined_1', {})
461
462 call term_sendkeys(buf, "\<C-E>")
463 call VerifyScreenDump(buf, 'Test_winscrolled_not_when_defined_2', {})
464
465 call StopVimInTerminal(buf)
466endfunc
467
zeertzjq670ab032022-08-28 19:16:15 +0100468func Test_WinScrolled_long_wrapped()
469 CheckRunVimInTerminal
470
471 let lines =<< trim END
472 set scrolloff=0
473 let height = winheight(0)
474 let width = winwidth(0)
475 let g:scrolled = 0
476 au WinScrolled * let g:scrolled += 1
477 call setline(1, repeat('foo', height * width))
478 call cursor(1, height * width)
479 END
Bram Moolenaare1f3ab72022-09-04 21:29:08 +0100480 call writefile(lines, 'Xtest_winscrolled_long_wrapped', 'D')
zeertzjq670ab032022-08-28 19:16:15 +0100481 let buf = RunVimInTerminal('-S Xtest_winscrolled_long_wrapped', {'rows': 6})
482
483 call term_sendkeys(buf, ":echo g:scrolled\<CR>")
484 call WaitForAssert({-> assert_match('^0 ', term_getline(buf, 6))}, 1000)
485
486 call term_sendkeys(buf, 'gj')
487 call term_sendkeys(buf, ":echo g:scrolled\<CR>")
488 call WaitForAssert({-> assert_match('^1 ', term_getline(buf, 6))}, 1000)
489
490 call term_sendkeys(buf, '0')
491 call term_sendkeys(buf, ":echo g:scrolled\<CR>")
492 call WaitForAssert({-> assert_match('^2 ', term_getline(buf, 6))}, 1000)
493
494 call term_sendkeys(buf, '$')
495 call term_sendkeys(buf, ":echo g:scrolled\<CR>")
496 call WaitForAssert({-> assert_match('^3 ', term_getline(buf, 6))}, 1000)
zeertzjq670ab032022-08-28 19:16:15 +0100497endfunc
498
naohiro ono23beefe2021-11-13 12:38:49 +0000499func Test_WinClosed()
500 " Test that the pattern is matched against the closed window's ID, and both
501 " <amatch> and <afile> are set to it.
502 new
503 let winid = win_getid()
504 let g:matched = v:false
505 augroup test-WinClosed
506 autocmd!
507 execute 'autocmd WinClosed' winid 'let g:matched = v:true'
508 autocmd WinClosed * let g:amatch = str2nr(expand('<amatch>'))
509 autocmd WinClosed * let g:afile = str2nr(expand('<afile>'))
510 augroup END
511 close
512 call assert_true(g:matched)
513 call assert_equal(winid, g:amatch)
514 call assert_equal(winid, g:afile)
515
516 " Test that WinClosed is non-recursive.
517 new
518 new
519 call assert_equal(3, winnr('$'))
520 let g:triggered = 0
521 augroup test-WinClosed
522 autocmd!
523 autocmd WinClosed * let g:triggered += 1
524 autocmd WinClosed * 2 wincmd c
525 augroup END
526 close
527 call assert_equal(1, winnr('$'))
528 call assert_equal(1, g:triggered)
529
530 autocmd! test-WinClosed
531 augroup! test-WinClosed
532 unlet g:matched
533 unlet g:amatch
534 unlet g:afile
535 unlet g:triggered
536endfunc
537
Bram Moolenaarc947b9a2022-04-06 17:59:21 +0100538func Test_WinClosed_throws()
539 vnew
540 let bnr = bufnr()
541 call assert_equal(1, bufloaded(bnr))
542 augroup test-WinClosed
543 autocmd WinClosed * throw 'foo'
544 augroup END
545 try
546 close
547 catch /.*/
548 endtry
549 call assert_equal(0, bufloaded(bnr))
550
551 autocmd! test-WinClosed
552 augroup! test-WinClosed
553endfunc
554
zeertzjq6a069402022-04-07 14:08:29 +0100555func Test_WinClosed_throws_with_tabs()
556 tabnew
557 let bnr = bufnr()
558 call assert_equal(1, bufloaded(bnr))
559 augroup test-WinClosed
560 autocmd WinClosed * throw 'foo'
561 augroup END
562 try
563 close
564 catch /.*/
565 endtry
566 call assert_equal(0, bufloaded(bnr))
567
568 autocmd! test-WinClosed
569 augroup! test-WinClosed
570endfunc
571
zeertzjq62de54b2022-09-22 18:08:37 +0100572" This used to trigger WinClosed twice for the same window, and the window's
573" buffer was NULL in the second autocommand.
574func Test_WinClosed_switch_tab()
575 edit Xa
576 split Xb
577 split Xc
578 tab split
579 new
580 augroup test-WinClosed
581 autocmd WinClosed * tabprev | bwipe!
582 augroup END
583 close
584 " Check that the tabline has been fully removed
585 call assert_equal([1, 1], win_screenpos(0))
586
587 autocmd! test-WinClosed
588 augroup! test-WinClosed
589 %bwipe!
590endfunc
591
Bram Moolenaare99e8442016-07-26 20:43:40 +0200592func s:AddAnAutocmd()
593 augroup vimBarTest
594 au BufReadCmd * echo 'hello'
595 augroup END
596 call assert_equal(3, len(split(execute('au vimBarTest'), "\n")))
597endfunc
598
599func Test_early_bar()
600 " test that a bar is recognized before the {event}
601 call s:AddAnAutocmd()
Bram Moolenaarb8e642f2021-11-20 10:38:25 +0000602 augroup vimBarTest | au! | let done = 77 | augroup END
Bram Moolenaare99e8442016-07-26 20:43:40 +0200603 call assert_equal(1, len(split(execute('au vimBarTest'), "\n")))
Bram Moolenaarb8e642f2021-11-20 10:38:25 +0000604 call assert_equal(77, done)
Bram Moolenaare99e8442016-07-26 20:43:40 +0200605
606 call s:AddAnAutocmd()
Bram Moolenaarb8e642f2021-11-20 10:38:25 +0000607 augroup vimBarTest| au!| let done = 88 | augroup END
Bram Moolenaare99e8442016-07-26 20:43:40 +0200608 call assert_equal(1, len(split(execute('au vimBarTest'), "\n")))
Bram Moolenaarb8e642f2021-11-20 10:38:25 +0000609 call assert_equal(88, done)
Bram Moolenaare99e8442016-07-26 20:43:40 +0200610
611 " test that a bar is recognized after the {event}
612 call s:AddAnAutocmd()
Bram Moolenaarb8e642f2021-11-20 10:38:25 +0000613 augroup vimBarTest| au!BufReadCmd| let done = 99 | augroup END
Bram Moolenaare99e8442016-07-26 20:43:40 +0200614 call assert_equal(1, len(split(execute('au vimBarTest'), "\n")))
Bram Moolenaarb8e642f2021-11-20 10:38:25 +0000615 call assert_equal(99, done)
Bram Moolenaare99e8442016-07-26 20:43:40 +0200616
617 " test that a bar is recognized after the {group}
618 call s:AddAnAutocmd()
619 au! vimBarTest|echo 'hello'
620 call assert_equal(1, len(split(execute('au vimBarTest'), "\n")))
621endfunc
Bram Moolenaarf2c4c392016-07-29 20:50:24 +0200622
Bram Moolenaar5c809082016-09-01 16:21:48 +0200623func RemoveGroup()
624 autocmd! StartOK
625 augroup! StartOK
626endfunc
627
Bram Moolenaarf2c4c392016-07-29 20:50:24 +0200628func Test_augroup_warning()
629 augroup TheWarning
630 au VimEnter * echo 'entering'
631 augroup END
Bram Moolenaar5dc4e2f2020-11-25 14:15:12 +0100632 call assert_match("TheWarning.*VimEnter", execute('au VimEnter'))
Bram Moolenaarf2c4c392016-07-29 20:50:24 +0200633 redir => res
634 augroup! TheWarning
635 redir END
Bram Moolenaar5dc4e2f2020-11-25 14:15:12 +0100636 call assert_match("W19:", res)
637 call assert_match("-Deleted-.*VimEnter", execute('au VimEnter'))
Bram Moolenaarf2c4c392016-07-29 20:50:24 +0200638
639 " check "Another" does not take the pace of the deleted entry
640 augroup Another
641 augroup END
Bram Moolenaar5dc4e2f2020-11-25 14:15:12 +0100642 call assert_match("-Deleted-.*VimEnter", execute('au VimEnter'))
Bram Moolenaaraeac9002016-09-06 22:15:08 +0200643 augroup! Another
Bram Moolenaar5c809082016-09-01 16:21:48 +0200644
645 " no warning for postpone aucmd delete
646 augroup StartOK
647 au VimEnter * call RemoveGroup()
648 augroup END
Bram Moolenaar5dc4e2f2020-11-25 14:15:12 +0100649 call assert_match("StartOK.*VimEnter", execute('au VimEnter'))
Bram Moolenaar5c809082016-09-01 16:21:48 +0200650 redir => res
651 doautocmd VimEnter
652 redir END
Bram Moolenaar5dc4e2f2020-11-25 14:15:12 +0100653 call assert_notmatch("W19:", res)
Bram Moolenaarde653f02016-09-03 16:59:06 +0200654 au! VimEnter
Bram Moolenaarad48e6c2020-04-21 22:19:45 +0200655
656 call assert_fails('augroup!', 'E471:')
Bram Moolenaarf2c4c392016-07-29 20:50:24 +0200657endfunc
Bram Moolenaarb62cc362016-09-03 16:43:53 +0200658
Bram Moolenaar8d84ff12017-10-26 16:42:16 +0200659func Test_BufReadCmdHelp()
660 " This used to cause access to free memory
661 au BufReadCmd * e +h
662 help
663
Bram Moolenaar8d84ff12017-10-26 16:42:16 +0200664 au! BufReadCmd
665endfunc
666
667func Test_BufReadCmdHelpJump()
668 " This used to cause access to free memory
669 au BufReadCmd * e +h{
Bram Moolenaarcf1ba352017-10-27 00:55:04 +0200670 " } to fix highlighting
671 call assert_fails('help', 'E434:')
Bram Moolenaar8d84ff12017-10-26 16:42:16 +0200672
Bram Moolenaar8d84ff12017-10-26 16:42:16 +0200673 au! BufReadCmd
674endfunc
675
zeertzjq93f72cc2022-08-26 15:34:52 +0100676" BufReadCmd is triggered for a "nofile" buffer. Check all values.
Bram Moolenaarb1d2c812022-08-26 11:55:01 +0100677func Test_BufReadCmdNofile()
zeertzjq93f72cc2022-08-26 15:34:52 +0100678 for val in ['nofile',
679 \ 'nowrite',
680 \ 'acwrite',
681 \ 'quickfix',
682 \ 'help',
683 \ 'terminal',
684 \ 'prompt',
685 \ 'popup',
686 \ ]
687 new somefile
688 exe 'set buftype=' .. val
689 au BufReadCmd somefile call setline(1, 'triggered')
690 edit
691 call assert_equal('triggered', getline(1))
Bram Moolenaarb1d2c812022-08-26 11:55:01 +0100692
zeertzjq93f72cc2022-08-26 15:34:52 +0100693 au! BufReadCmd
694 bwipe!
695 endfor
Bram Moolenaarb1d2c812022-08-26 11:55:01 +0100696endfunc
697
Bram Moolenaarb62cc362016-09-03 16:43:53 +0200698func Test_augroup_deleted()
Bram Moolenaarde653f02016-09-03 16:59:06 +0200699 " This caused a crash before E936 was introduced
Bram Moolenaarb62cc362016-09-03 16:43:53 +0200700 augroup x
Bram Moolenaarde653f02016-09-03 16:59:06 +0200701 call assert_fails('augroup! x', 'E936:')
702 au VimEnter * echo
703 augroup end
Bram Moolenaarb62cc362016-09-03 16:43:53 +0200704 augroup! x
Bram Moolenaar5dc4e2f2020-11-25 14:15:12 +0100705 call assert_match("-Deleted-.*VimEnter", execute('au VimEnter'))
Bram Moolenaarde653f02016-09-03 16:59:06 +0200706 au! VimEnter
Bram Moolenaarb62cc362016-09-03 16:43:53 +0200707endfunc
708
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200709" Tests for autocommands on :close command.
710" This used to be in test13.
711func Test_three_windows()
Bram Moolenaarb3435b02016-09-29 20:54:59 +0200712 " Clean up buffers, because in some cases this function fails.
713 call s:cleanup_buffers()
714
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200715 " Write three files and open them, each in a window.
716 " Then go to next window, with autocommand that deletes the previous one.
717 " Do this twice, writing the file.
718 e! Xtestje1
719 call setline(1, 'testje1')
720 w
721 sp Xtestje2
722 call setline(1, 'testje2')
723 w
724 sp Xtestje3
725 call setline(1, 'testje3')
726 w
727 wincmd w
728 au WinLeave Xtestje2 bwipe
729 wincmd w
730 call assert_equal('Xtestje1', expand('%'))
731
732 au WinLeave Xtestje1 bwipe Xtestje3
733 close
734 call assert_equal('Xtestje1', expand('%'))
735
736 " Test deleting the buffer on a Unload event. If this goes wrong there
737 " will be the ATTENTION prompt.
738 e Xtestje1
739 au!
740 au! BufUnload Xtestje1 bwipe
741 call assert_fails('e Xtestje3', 'E937:')
742 call assert_equal('Xtestje3', expand('%'))
743
744 e Xtestje2
745 sp Xtestje1
746 call assert_fails('e', 'E937:')
Bram Moolenaara997b452018-04-17 23:24:06 +0200747 call assert_equal('Xtestje1', expand('%'))
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200748
749 " Test changing buffers in a BufWipeout autocommand. If this goes wrong
750 " there are ml_line errors and/or a Crash.
751 au!
752 only
753 e Xanother
754 e Xtestje1
755 bwipe Xtestje2
756 bwipe Xtestje3
757 au BufWipeout Xtestje1 buf Xtestje1
758 bwipe
759 call assert_equal('Xanother', expand('%'))
760
761 only
762 help
763 wincmd w
764 1quit
765 call assert_equal('Xanother', expand('%'))
766
767 au!
Bram Moolenaar4520d442017-03-19 16:09:46 +0100768 enew
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200769 call delete('Xtestje1')
770 call delete('Xtestje2')
771 call delete('Xtestje3')
772endfunc
Bram Moolenaare13b9af2017-01-13 22:01:02 +0100773
774func Test_BufEnter()
775 au! BufEnter
776 au Bufenter * let val = val . '+'
777 let g:val = ''
778 split NewFile
779 call assert_equal('+', g:val)
780 bwipe!
781 call assert_equal('++', g:val)
782
783 " Also get BufEnter when editing a directory
Bram Moolenaar6f14da12022-09-07 21:30:44 +0100784 call mkdir('Xbufenterdir', 'D')
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +0100785 split Xbufenterdir
Bram Moolenaare13b9af2017-01-13 22:01:02 +0100786 call assert_equal('+++', g:val)
Bram Moolenaare94260f2017-03-21 15:50:12 +0100787
788 " On MS-Windows we can't edit the directory, make sure we wipe the right
789 " buffer.
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +0100790 bwipe! Xbufenterdir
Bram Moolenaare13b9af2017-01-13 22:01:02 +0100791 au! BufEnter
Bram Moolenaara9b5b852022-08-26 13:16:20 +0100792
793 " Editing a "nofile" buffer doesn't read the file but does trigger BufEnter
zeertzjq93f72cc2022-08-26 15:34:52 +0100794 " for historic reasons. Also test other 'buftype' values.
795 for val in ['nofile',
796 \ 'nowrite',
797 \ 'acwrite',
798 \ 'quickfix',
799 \ 'help',
800 \ 'terminal',
801 \ 'prompt',
802 \ 'popup',
803 \ ]
804 new somefile
805 exe 'set buftype=' .. val
806 au BufEnter somefile call setline(1, 'some text')
807 edit
808 call assert_equal('some text', getline(1))
809 bwipe!
810 au! BufEnter
811 endfor
Bram Moolenaar9fda8152022-11-19 13:14:10 +0000812
813 new
814 new
815 autocmd BufEnter * ++once close
816 call assert_fails('close', 'E1312:')
817
818 au! BufEnter
819 only
Bram Moolenaare13b9af2017-01-13 22:01:02 +0100820endfunc
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100821
822" Closing a window might cause an endless loop
823" E814 for older Vims
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200824func Test_autocmd_bufwipe_in_SessLoadPost()
Bram Moolenaar1d68d9b2017-10-13 22:33:32 +0200825 edit Xtest
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100826 tabnew
Bram Moolenaar1d68d9b2017-10-13 22:33:32 +0200827 file Xsomething
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100828 set noswapfile
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100829 mksession!
830
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200831 let content =<< trim [CODE]
Bram Moolenaar62cd26a2020-10-11 20:08:44 +0200832 call test_override('ui_delay', 10)
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200833 set nocp noswapfile
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100834 let v:swapchoice = "e"
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200835 augroup test_autocmd_sessionload
836 autocmd!
837 autocmd SessionLoadPost * exe bufnr("Xsomething") . "bw!"
838 augroup END
839
840 func WriteErrors()
Bram Moolenaarbe9fc5b2022-09-09 17:09:35 +0100841 call writefile([execute("messages")], "XerrorsBwipe")
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200842 endfunc
843 au VimLeave * call WriteErrors()
844 [CODE]
845
Bram Moolenaare1f3ab72022-09-04 21:29:08 +0100846 call writefile(content, 'Xvimrc', 'D')
Bram Moolenaar93344c22019-08-14 21:12:05 +0200847 call system(GetVimCommand('Xvimrc') .. ' --not-a-term --noplugins -S Session.vim -c cq')
Bram Moolenaarae04a602022-09-09 15:08:10 +0100848 sleep 100m
Bram Moolenaarbe9fc5b2022-09-09 17:09:35 +0100849 let errors = join(readfile('XerrorsBwipe'))
Bram Moolenaare2e40752020-09-04 21:18:46 +0200850 call assert_match('E814:', errors)
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100851
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100852 set swapfile
Bram Moolenaarbe9fc5b2022-09-09 17:09:35 +0100853 for file in ['Session.vim', 'XerrorsBwipe']
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100854 call delete(file)
855 endfor
856endfunc
857
Bram Moolenaar797e63b2021-01-15 16:22:52 +0100858" Using :blast and :ball for many events caused a crash, because b_nwindows was
859" not incremented correctly.
860func Test_autocmd_blast_badd()
861 let content =<< trim [CODE]
862 au BufNew,BufAdd,BufWinEnter,BufEnter,BufLeave,BufWinLeave,BufUnload,VimEnter foo* blast
863 edit foo1
864 au BufNew,BufAdd,BufWinEnter,BufEnter,BufLeave,BufWinLeave,BufUnload,VimEnter foo* ball
865 edit foo2
Bram Moolenaarbe9fc5b2022-09-09 17:09:35 +0100866 call writefile(['OK'], 'XerrorsBlast')
Bram Moolenaar797e63b2021-01-15 16:22:52 +0100867 qall
868 [CODE]
869
Bram Moolenaare1f3ab72022-09-04 21:29:08 +0100870 call writefile(content, 'XblastBall', 'D')
Bram Moolenaar797e63b2021-01-15 16:22:52 +0100871 call system(GetVimCommand() .. ' --clean -S XblastBall')
Bram Moolenaarae04a602022-09-09 15:08:10 +0100872 sleep 100m
Bram Moolenaarbe9fc5b2022-09-09 17:09:35 +0100873 call assert_match('OK', readfile('XerrorsBlast')->join())
Bram Moolenaar797e63b2021-01-15 16:22:52 +0100874
Bram Moolenaarbe9fc5b2022-09-09 17:09:35 +0100875 call delete('XerrorsBlast')
Bram Moolenaar797e63b2021-01-15 16:22:52 +0100876endfunc
877
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100878" SEGV occurs in older versions.
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200879func Test_autocmd_bufwipe_in_SessLoadPost2()
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100880 tabnew
881 set noswapfile
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100882 mksession!
883
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200884 let content =<< trim [CODE]
885 set nocp noswapfile
886 function! DeleteInactiveBufs()
887 tabfirst
888 let tabblist = []
889 for i in range(1, tabpagenr(''$''))
890 call extend(tabblist, tabpagebuflist(i))
891 endfor
892 for b in range(1, bufnr(''$''))
893 if bufexists(b) && buflisted(b) && (index(tabblist, b) == -1 || bufname(b) =~# ''^$'')
894 exec ''bwipeout '' . b
895 endif
896 endfor
897 echomsg "SessionLoadPost DONE"
898 endfunction
899 au SessionLoadPost * call DeleteInactiveBufs()
900
901 func WriteErrors()
Bram Moolenaarbe9fc5b2022-09-09 17:09:35 +0100902 call writefile([execute("messages")], "XerrorsPost")
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200903 endfunc
904 au VimLeave * call WriteErrors()
905 [CODE]
906
Bram Moolenaare1f3ab72022-09-04 21:29:08 +0100907 call writefile(content, 'Xvimrc', 'D')
Bram Moolenaar93344c22019-08-14 21:12:05 +0200908 call system(GetVimCommand('Xvimrc') .. ' --not-a-term --noplugins -S Session.vim -c cq')
Bram Moolenaarae04a602022-09-09 15:08:10 +0100909 sleep 100m
Bram Moolenaarbe9fc5b2022-09-09 17:09:35 +0100910 let errors = join(readfile('XerrorsPost'))
Bram Moolenaare94260f2017-03-21 15:50:12 +0100911 " This probably only ever matches on unix.
912 call assert_notmatch('Caught deadly signal SEGV', errors)
913 call assert_match('SessionLoadPost DONE', errors)
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100914
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100915 set swapfile
Bram Moolenaarbe9fc5b2022-09-09 17:09:35 +0100916 for file in ['Session.vim', 'XerrorsPost']
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100917 call delete(file)
918 endfor
919endfunc
Bram Moolenaarfaf29d72017-07-09 11:07:16 +0200920
921func Test_empty_doau()
922 doau \|
923endfunc
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200924
925func s:AutoCommandOptionSet(match)
Bram Moolenaard7c96872019-06-15 17:12:48 +0200926 let template = "Option: <%s>, OldVal: <%s>, OldValLocal: <%s>, OldValGlobal: <%s>, NewVal: <%s>, Scope: <%s>, Command: <%s>\n"
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200927 let item = remove(g:options, 0)
Bram Moolenaard7c96872019-06-15 17:12:48 +0200928 let expected = printf(template, item[0], item[1], item[2], item[3], item[4], item[5], item[6])
929 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 +0200930 let g:opt = [expected, actual]
931 "call assert_equal(expected, actual)
932endfunc
933
934func Test_OptionSet()
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +0200935 CheckOption autochdir
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200936
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +0200937 badd test_autocmd.vim
938
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200939 call test_override('starting', 1)
940 set nocp
941 au OptionSet * :call s:AutoCommandOptionSet(expand("<amatch>"))
942
943 " 1: Setting number option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100944 let g:options = [['number', 0, 0, 0, 1, 'global', 'set']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200945 set nu
946 call assert_equal([], g:options)
947 call assert_equal(g:opt[0], g:opt[1])
948
949 " 2: Setting local number option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100950 let g:options = [['number', 1, 1, '', 0, 'local', 'setlocal']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200951 setlocal nonu
952 call assert_equal([], g:options)
953 call assert_equal(g:opt[0], g:opt[1])
954
955 " 3: Setting global number option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100956 let g:options = [['number', 1, '', 1, 0, 'global', 'setglobal']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200957 setglobal nonu
958 call assert_equal([], g:options)
959 call assert_equal(g:opt[0], g:opt[1])
960
961 " 4: Setting local autoindent option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100962 let g:options = [['autoindent', 0, 0, '', 1, 'local', 'setlocal']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200963 setlocal ai
964 call assert_equal([], g:options)
965 call assert_equal(g:opt[0], g:opt[1])
966
967 " 5: Setting global autoindent option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100968 let g:options = [['autoindent', 0, '', 0, 1, 'global', 'setglobal']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200969 setglobal ai
970 call assert_equal([], g:options)
971 call assert_equal(g:opt[0], g:opt[1])
972
973 " 6: Setting global autoindent option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100974 let g:options = [['autoindent', 1, 1, 1, 0, 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +0200975 set ai!
976 call assert_equal([], g:options)
977 call assert_equal(g:opt[0], g:opt[1])
978
979 " 6a: Setting global autoindent option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100980 let g:options = [['autoindent', 1, 1, 0, 0, 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +0200981 noa setlocal ai
982 noa setglobal noai
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200983 set ai!
984 call assert_equal([], g:options)
985 call assert_equal(g:opt[0], g:opt[1])
986
987 " Should not print anything, use :noa
988 " 7: don't trigger OptionSet"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100989 let g:options = [['invalid', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200990 noa set nonu
Bram Moolenaard7c96872019-06-15 17:12:48 +0200991 call assert_equal([['invalid', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid']], g:options)
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200992 call assert_equal(g:opt[0], g:opt[1])
993
994 " 8: Setting several global list and number option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100995 let g:options = [['list', 0, 0, 0, 1, 'global', 'set'], ['number', 0, 0, 0, 1, 'global', 'set']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200996 set list nu
997 call assert_equal([], g:options)
998 call assert_equal(g:opt[0], g:opt[1])
999
1000 " 9: don't trigger OptionSet"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001001 let g:options = [['invalid', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid'], ['invalid', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001002 noa set nolist nonu
Bram Moolenaard7c96872019-06-15 17:12:48 +02001003 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 +02001004 call assert_equal(g:opt[0], g:opt[1])
1005
1006 " 10: Setting global acd"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001007 let g:options = [['autochdir', 0, 0, '', 1, 'local', 'setlocal']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001008 setlocal acd
1009 call assert_equal([], g:options)
1010 call assert_equal(g:opt[0], g:opt[1])
1011
1012 " 11: Setting global autoread (also sets local value)"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001013 let g:options = [['autoread', 0, 0, 0, 1, 'global', 'set']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001014 set ar
1015 call assert_equal([], g:options)
1016 call assert_equal(g:opt[0], g:opt[1])
1017
1018 " 12: Setting local autoread"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001019 let g:options = [['autoread', 1, 1, '', 1, 'local', 'setlocal']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001020 setlocal ar
1021 call assert_equal([], g:options)
1022 call assert_equal(g:opt[0], g:opt[1])
1023
1024 " 13: Setting global autoread"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001025 let g:options = [['autoread', 1, '', 1, 0, 'global', 'setglobal']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001026 setglobal invar
1027 call assert_equal([], g:options)
1028 call assert_equal(g:opt[0], g:opt[1])
1029
1030 " 14: Setting option backspace through :let"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001031 let g:options = [['backspace', '', '', '', 'eol,indent,start', 'global', 'set']]
1032 let &bs = "eol,indent,start"
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001033 call assert_equal([], g:options)
1034 call assert_equal(g:opt[0], g:opt[1])
1035
1036 " 15: Setting option backspace through setbufvar()"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001037 let g:options = [['backup', 0, 0, '', 1, 'local', 'setlocal']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001038 " try twice, first time, shouldn't trigger because option name is invalid,
1039 " second time, it should trigger
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +02001040 let bnum = bufnr('%')
Bram Moolenaare2e40752020-09-04 21:18:46 +02001041 call assert_fails("call setbufvar(bnum, '&l:bk', 1)", 'E355:')
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001042 " should trigger, use correct option name
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +02001043 call setbufvar(bnum, '&backup', 1)
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001044 call assert_equal([], g:options)
1045 call assert_equal(g:opt[0], g:opt[1])
1046
1047 " 16: Setting number option using setwinvar"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001048 let g:options = [['number', 0, 0, '', 1, 'local', 'setlocal']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001049 call setwinvar(0, '&number', 1)
1050 call assert_equal([], g:options)
1051 call assert_equal(g:opt[0], g:opt[1])
1052
1053 " 17: Setting key option, shouldn't trigger"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001054 let g:options = [['key', 'invalid', 'invalid1', 'invalid2', 'invalid3', 'invalid4', 'invalid5']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001055 setlocal key=blah
1056 setlocal key=
Bram Moolenaard7c96872019-06-15 17:12:48 +02001057 call assert_equal([['key', 'invalid', 'invalid1', 'invalid2', 'invalid3', 'invalid4', 'invalid5']], g:options)
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001058 call assert_equal(g:opt[0], g:opt[1])
1059
Bram Moolenaard7c96872019-06-15 17:12:48 +02001060
1061 " 18a: Setting string global option"
1062 let oldval = &backupext
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001063 let g:options = [['backupext', oldval, oldval, oldval, 'foo', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001064 set backupext=foo
1065 call assert_equal([], g:options)
1066 call assert_equal(g:opt[0], g:opt[1])
1067
1068 " 18b: Resetting string global option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001069 let g:options = [['backupext', 'foo', 'foo', 'foo', oldval, 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001070 set backupext&
1071 call assert_equal([], g:options)
1072 call assert_equal(g:opt[0], g:opt[1])
1073
1074 " 18c: Setting global string global option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001075 let g:options = [['backupext', oldval, '', oldval, 'bar', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001076 setglobal backupext=bar
1077 call assert_equal([], g:options)
1078 call assert_equal(g:opt[0], g:opt[1])
1079
1080 " 18d: Setting local string global option"
1081 " As this is a global option this sets the global value even though
1082 " :setlocal is used!
1083 noa set backupext& " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001084 let g:options = [['backupext', oldval, oldval, '', 'baz', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001085 setlocal backupext=baz
1086 call assert_equal([], g:options)
1087 call assert_equal(g:opt[0], g:opt[1])
1088
1089 " 18e: Setting again string global option"
1090 noa setglobal backupext=ext_global " Reset global and local value (without triggering autocmd)
1091 noa setlocal backupext=ext_local " Sets the global(!) value!
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001092 let g:options = [['backupext', 'ext_local', 'ext_local', 'ext_local', 'fuu', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001093 set backupext=fuu
1094 call assert_equal([], g:options)
1095 call assert_equal(g:opt[0], g:opt[1])
1096
1097
zeertzjqb811de52021-10-21 10:50:44 +01001098 " 19a: Setting string global-local (to buffer) option"
Bram Moolenaar8efa0262017-08-20 15:47:20 +02001099 let oldval = &tags
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001100 let g:options = [['tags', oldval, oldval, oldval, 'tagpath', 'global', 'set']]
Bram Moolenaar8efa0262017-08-20 15:47:20 +02001101 set tags=tagpath
1102 call assert_equal([], g:options)
1103 call assert_equal(g:opt[0], g:opt[1])
1104
zeertzjqb811de52021-10-21 10:50:44 +01001105 " 19b: Resetting string global-local (to buffer) option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001106 let g:options = [['tags', 'tagpath', 'tagpath', 'tagpath', oldval, 'global', 'set']]
Bram Moolenaar8efa0262017-08-20 15:47:20 +02001107 set tags&
1108 call assert_equal([], g:options)
1109 call assert_equal(g:opt[0], g:opt[1])
1110
zeertzjqb811de52021-10-21 10:50:44 +01001111 " 19c: Setting global string global-local (to buffer) option "
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001112 let g:options = [['tags', oldval, '', oldval, 'tagpath1', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001113 setglobal tags=tagpath1
1114 call assert_equal([], g:options)
1115 call assert_equal(g:opt[0], g:opt[1])
1116
zeertzjqb811de52021-10-21 10:50:44 +01001117 " 19d: Setting local string global-local (to buffer) option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001118 let g:options = [['tags', 'tagpath1', 'tagpath1', '', 'tagpath2', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001119 setlocal tags=tagpath2
1120 call assert_equal([], g:options)
1121 call assert_equal(g:opt[0], g:opt[1])
1122
zeertzjqb811de52021-10-21 10:50:44 +01001123 " 19e: Setting again string global-local (to buffer) option"
1124 " Note: v:option_old is the old global value for global-local string options
Bram Moolenaard7c96872019-06-15 17:12:48 +02001125 " but the old local value for all other kinds of options.
1126 noa setglobal tags=tag_global " Reset global and local value (without triggering autocmd)
1127 noa setlocal tags=tag_local
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001128 let g:options = [['tags', 'tag_global', 'tag_local', 'tag_global', 'tagpath', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001129 set tags=tagpath
1130 call assert_equal([], g:options)
1131 call assert_equal(g:opt[0], g:opt[1])
1132
zeertzjqb811de52021-10-21 10:50:44 +01001133 " 19f: Setting string global-local (to buffer) option to an empty string"
1134 " Note: v:option_old is the old global value for global-local string options
Bram Moolenaard7c96872019-06-15 17:12:48 +02001135 " but the old local value for all other kinds of options.
1136 noa set tags=tag_global " Reset global and local value (without triggering autocmd)
1137 noa setlocal tags= " empty string
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001138 let g:options = [['tags', 'tag_global', '', 'tag_global', 'tagpath', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001139 set tags=tagpath
1140 call assert_equal([], g:options)
1141 call assert_equal(g:opt[0], g:opt[1])
1142
1143
1144 " 20a: Setting string local (to buffer) option"
1145 let oldval = &spelllang
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001146 let g:options = [['spelllang', oldval, oldval, oldval, 'elvish,klingon', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001147 set spelllang=elvish,klingon
1148 call assert_equal([], g:options)
1149 call assert_equal(g:opt[0], g:opt[1])
1150
1151 " 20b: Resetting string local (to buffer) option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001152 let g:options = [['spelllang', 'elvish,klingon', 'elvish,klingon', 'elvish,klingon', oldval, 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001153 set spelllang&
1154 call assert_equal([], g:options)
1155 call assert_equal(g:opt[0], g:opt[1])
1156
1157 " 20c: Setting global string local (to buffer) option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001158 let g:options = [['spelllang', oldval, '', oldval, 'elvish', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001159 setglobal spelllang=elvish
1160 call assert_equal([], g:options)
1161 call assert_equal(g:opt[0], g:opt[1])
1162
1163 " 20d: Setting local string local (to buffer) option"
1164 noa set spelllang& " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001165 let g:options = [['spelllang', oldval, oldval, '', 'klingon', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001166 setlocal spelllang=klingon
1167 call assert_equal([], g:options)
1168 call assert_equal(g:opt[0], g:opt[1])
1169
1170 " 20e: Setting again string local (to buffer) option"
zeertzjqb811de52021-10-21 10:50:44 +01001171 " Note: v:option_old is the old global value for global-local string options
Bram Moolenaard7c96872019-06-15 17:12:48 +02001172 " but the old local value for all other kinds of options.
1173 noa setglobal spelllang=spellglobal " Reset global and local value (without triggering autocmd)
1174 noa setlocal spelllang=spelllocal
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001175 let g:options = [['spelllang', 'spelllocal', 'spelllocal', 'spellglobal', 'foo', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001176 set spelllang=foo
1177 call assert_equal([], g:options)
1178 call assert_equal(g:opt[0], g:opt[1])
1179
1180
zeertzjqb811de52021-10-21 10:50:44 +01001181 " 21a: Setting string global-local (to window) option"
Bram Moolenaard7c96872019-06-15 17:12:48 +02001182 let oldval = &statusline
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001183 let g:options = [['statusline', oldval, oldval, oldval, 'foo', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001184 set statusline=foo
1185 call assert_equal([], g:options)
1186 call assert_equal(g:opt[0], g:opt[1])
1187
zeertzjqb811de52021-10-21 10:50:44 +01001188 " 21b: Resetting string global-local (to window) option"
1189 " Note: v:option_old is the old global value for global-local string options
Bram Moolenaard7c96872019-06-15 17:12:48 +02001190 " but the old local value for all other kinds of options.
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001191 let g:options = [['statusline', 'foo', 'foo', 'foo', oldval, 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001192 set statusline&
1193 call assert_equal([], g:options)
1194 call assert_equal(g:opt[0], g:opt[1])
1195
zeertzjqb811de52021-10-21 10:50:44 +01001196 " 21c: Setting global string global-local (to window) option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001197 let g:options = [['statusline', oldval, '', oldval, 'bar', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001198 setglobal statusline=bar
1199 call assert_equal([], g:options)
1200 call assert_equal(g:opt[0], g:opt[1])
1201
zeertzjqb811de52021-10-21 10:50:44 +01001202 " 21d: Setting local string global-local (to window) option"
Bram Moolenaard7c96872019-06-15 17:12:48 +02001203 noa set statusline& " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001204 let g:options = [['statusline', oldval, oldval, '', 'baz', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001205 setlocal statusline=baz
1206 call assert_equal([], g:options)
1207 call assert_equal(g:opt[0], g:opt[1])
1208
zeertzjqb811de52021-10-21 10:50:44 +01001209 " 21e: Setting again string global-local (to window) option"
1210 " Note: v:option_old is the old global value for global-local string options
Bram Moolenaard7c96872019-06-15 17:12:48 +02001211 " but the old local value for all other kinds of options.
1212 noa setglobal statusline=bar " Reset global and local value (without triggering autocmd)
1213 noa setlocal statusline=baz
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001214 let g:options = [['statusline', 'bar', 'baz', 'bar', 'foo', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001215 set statusline=foo
1216 call assert_equal([], g:options)
1217 call assert_equal(g:opt[0], g:opt[1])
1218
1219
1220 " 22a: Setting string local (to window) option"
1221 let oldval = &foldignore
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001222 let g:options = [['foldignore', oldval, oldval, oldval, 'fo', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001223 set foldignore=fo
1224 call assert_equal([], g:options)
1225 call assert_equal(g:opt[0], g:opt[1])
1226
1227 " 22b: Resetting string local (to window) option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001228 let g:options = [['foldignore', 'fo', 'fo', 'fo', oldval, 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001229 set foldignore&
1230 call assert_equal([], g:options)
1231 call assert_equal(g:opt[0], g:opt[1])
1232
1233 " 22c: Setting global string local (to window) option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001234 let g:options = [['foldignore', oldval, '', oldval, 'bar', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001235 setglobal foldignore=bar
1236 call assert_equal([], g:options)
1237 call assert_equal(g:opt[0], g:opt[1])
1238
1239 " 22d: Setting local string local (to window) option"
1240 noa set foldignore& " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001241 let g:options = [['foldignore', oldval, oldval, '', 'baz', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001242 setlocal foldignore=baz
1243 call assert_equal([], g:options)
1244 call assert_equal(g:opt[0], g:opt[1])
1245
1246 " 22e: Setting again string local (to window) option"
1247 noa setglobal foldignore=glob " Reset global and local value (without triggering autocmd)
1248 noa setlocal foldignore=loc
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001249 let g:options = [['foldignore', 'loc', 'loc', 'glob', 'fo', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001250 set foldignore=fo
1251 call assert_equal([], g:options)
1252 call assert_equal(g:opt[0], g:opt[1])
1253
1254
zeertzjqb811de52021-10-21 10:50:44 +01001255 " 23a: Setting global number global option"
Bram Moolenaard7c96872019-06-15 17:12:48 +02001256 noa setglobal cmdheight=8 " Reset global and local value (without triggering autocmd)
1257 noa setlocal cmdheight=1 " Sets the global(!) value!
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001258 let g:options = [['cmdheight', '1', '', '1', '2', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001259 setglobal cmdheight=2
1260 call assert_equal([], g:options)
1261 call assert_equal(g:opt[0], g:opt[1])
1262
1263 " 23b: Setting local number global option"
1264 noa setglobal cmdheight=8 " Reset global and local value (without triggering autocmd)
1265 noa setlocal cmdheight=1 " Sets the global(!) value!
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001266 let g:options = [['cmdheight', '1', '1', '', '2', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001267 setlocal cmdheight=2
1268 call assert_equal([], g:options)
1269 call assert_equal(g:opt[0], g:opt[1])
1270
1271 " 23c: Setting again number global option"
1272 noa setglobal cmdheight=8 " Reset global and local value (without triggering autocmd)
1273 noa setlocal cmdheight=1 " Sets the global(!) value!
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001274 let g:options = [['cmdheight', '1', '1', '1', '2', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001275 set cmdheight=2
1276 call assert_equal([], g:options)
1277 call assert_equal(g:opt[0], g:opt[1])
1278
1279 " 23d: Setting again number global option"
1280 noa set cmdheight=8 " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001281 let g:options = [['cmdheight', '8', '8', '8', '2', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001282 set cmdheight=2
1283 call assert_equal([], g:options)
1284 call assert_equal(g:opt[0], g:opt[1])
1285
1286
1287 " 24a: Setting global number global-local (to buffer) option"
1288 noa setglobal undolevels=8 " Reset global and local value (without triggering autocmd)
1289 noa setlocal undolevels=1
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001290 let g:options = [['undolevels', '8', '', '8', '2', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001291 setglobal undolevels=2
1292 call assert_equal([], g:options)
1293 call assert_equal(g:opt[0], g:opt[1])
1294
1295 " 24b: Setting local number global-local (to buffer) option"
1296 noa setglobal undolevels=8 " Reset global and local value (without triggering autocmd)
1297 noa setlocal undolevels=1
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001298 let g:options = [['undolevels', '1', '1', '', '2', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001299 setlocal undolevels=2
1300 call assert_equal([], g:options)
1301 call assert_equal(g:opt[0], g:opt[1])
1302
1303 " 24c: Setting again number global-local (to buffer) option"
1304 noa setglobal undolevels=8 " Reset global and local value (without triggering autocmd)
1305 noa setlocal undolevels=1
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001306 let g:options = [['undolevels', '1', '1', '8', '2', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001307 set undolevels=2
1308 call assert_equal([], g:options)
1309 call assert_equal(g:opt[0], g:opt[1])
1310
1311 " 24d: Setting again global number global-local (to buffer) option"
1312 noa set undolevels=8 " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001313 let g:options = [['undolevels', '8', '8', '8', '2', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001314 set undolevels=2
1315 call assert_equal([], g:options)
1316 call assert_equal(g:opt[0], g:opt[1])
1317
1318
1319 " 25a: Setting global number local (to buffer) option"
1320 noa setglobal wrapmargin=8 " Reset global and local value (without triggering autocmd)
1321 noa setlocal wrapmargin=1
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001322 let g:options = [['wrapmargin', '8', '', '8', '2', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001323 setglobal wrapmargin=2
1324 call assert_equal([], g:options)
1325 call assert_equal(g:opt[0], g:opt[1])
1326
1327 " 25b: Setting local number local (to buffer) option"
1328 noa setglobal wrapmargin=8 " Reset global and local value (without triggering autocmd)
1329 noa setlocal wrapmargin=1
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001330 let g:options = [['wrapmargin', '1', '1', '', '2', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001331 setlocal wrapmargin=2
1332 call assert_equal([], g:options)
1333 call assert_equal(g:opt[0], g:opt[1])
1334
1335 " 25c: Setting again number local (to buffer) option"
1336 noa setglobal wrapmargin=8 " Reset global and local value (without triggering autocmd)
1337 noa setlocal wrapmargin=1
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001338 let g:options = [['wrapmargin', '1', '1', '8', '2', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001339 set wrapmargin=2
1340 call assert_equal([], g:options)
1341 call assert_equal(g:opt[0], g:opt[1])
1342
1343 " 25d: Setting again global number local (to buffer) option"
1344 noa set wrapmargin=8 " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001345 let g:options = [['wrapmargin', '8', '8', '8', '2', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001346 set wrapmargin=2
1347 call assert_equal([], g:options)
1348 call assert_equal(g:opt[0], g:opt[1])
1349
1350
1351 " 26: Setting number global-local (to window) option.
1352 " Such option does currently not exist.
1353
1354
1355 " 27a: Setting global number local (to window) option"
1356 noa setglobal foldcolumn=8 " Reset global and local value (without triggering autocmd)
1357 noa setlocal foldcolumn=1
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001358 let g:options = [['foldcolumn', '8', '', '8', '2', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001359 setglobal foldcolumn=2
1360 call assert_equal([], g:options)
1361 call assert_equal(g:opt[0], g:opt[1])
1362
1363 " 27b: Setting local number local (to window) option"
1364 noa setglobal foldcolumn=8 " Reset global and local value (without triggering autocmd)
1365 noa setlocal foldcolumn=1
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001366 let g:options = [['foldcolumn', '1', '1', '', '2', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001367 setlocal foldcolumn=2
1368 call assert_equal([], g:options)
1369 call assert_equal(g:opt[0], g:opt[1])
1370
1371 " 27c: Setting again number local (to window) option"
1372 noa setglobal foldcolumn=8 " Reset global and local value (without triggering autocmd)
1373 noa setlocal foldcolumn=1
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001374 let g:options = [['foldcolumn', '1', '1', '8', '2', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001375 set foldcolumn=2
1376 call assert_equal([], g:options)
1377 call assert_equal(g:opt[0], g:opt[1])
1378
zeertzjqb811de52021-10-21 10:50:44 +01001379 " 27d: Setting again global number local (to window) option"
Bram Moolenaard7c96872019-06-15 17:12:48 +02001380 noa set foldcolumn=8 " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001381 let g:options = [['foldcolumn', '8', '8', '8', '2', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001382 set foldcolumn=2
1383 call assert_equal([], g:options)
1384 call assert_equal(g:opt[0], g:opt[1])
1385
1386
1387 " 28a: Setting global boolean global option"
1388 noa setglobal nowrapscan " Reset global and local value (without triggering autocmd)
1389 noa setlocal wrapscan " Sets the global(!) value!
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001390 let g:options = [['wrapscan', '1', '', '1', '0', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001391 setglobal nowrapscan
1392 call assert_equal([], g:options)
1393 call assert_equal(g:opt[0], g:opt[1])
1394
1395 " 28b: Setting local boolean global option"
1396 noa setglobal nowrapscan " Reset global and local value (without triggering autocmd)
1397 noa setlocal wrapscan " Sets the global(!) value!
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001398 let g:options = [['wrapscan', '1', '1', '', '0', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001399 setlocal nowrapscan
1400 call assert_equal([], g:options)
1401 call assert_equal(g:opt[0], g:opt[1])
1402
1403 " 28c: Setting again boolean global option"
1404 noa setglobal nowrapscan " Reset global and local value (without triggering autocmd)
1405 noa setlocal wrapscan " Sets the global(!) value!
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001406 let g:options = [['wrapscan', '1', '1', '1', '0', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001407 set nowrapscan
1408 call assert_equal([], g:options)
1409 call assert_equal(g:opt[0], g:opt[1])
1410
1411 " 28d: Setting again global boolean global option"
1412 noa set nowrapscan " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001413 let g:options = [['wrapscan', '0', '0', '0', '1', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001414 set wrapscan
1415 call assert_equal([], g:options)
1416 call assert_equal(g:opt[0], g:opt[1])
1417
1418
1419 " 29a: Setting global boolean global-local (to buffer) option"
1420 noa setglobal noautoread " Reset global and local value (without triggering autocmd)
1421 noa setlocal autoread
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001422 let g:options = [['autoread', '0', '', '0', '1', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001423 setglobal autoread
1424 call assert_equal([], g:options)
1425 call assert_equal(g:opt[0], g:opt[1])
1426
1427 " 29b: Setting local boolean global-local (to buffer) option"
1428 noa setglobal noautoread " Reset global and local value (without triggering autocmd)
1429 noa setlocal autoread
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001430 let g:options = [['autoread', '1', '1', '', '0', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001431 setlocal noautoread
1432 call assert_equal([], g:options)
1433 call assert_equal(g:opt[0], g:opt[1])
1434
1435 " 29c: Setting again boolean global-local (to buffer) option"
1436 noa setglobal noautoread " Reset global and local value (without triggering autocmd)
1437 noa setlocal autoread
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001438 let g:options = [['autoread', '1', '1', '0', '1', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001439 set autoread
1440 call assert_equal([], g:options)
1441 call assert_equal(g:opt[0], g:opt[1])
1442
1443 " 29d: Setting again global boolean global-local (to buffer) option"
1444 noa set noautoread " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001445 let g:options = [['autoread', '0', '0', '0', '1', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001446 set autoread
1447 call assert_equal([], g:options)
1448 call assert_equal(g:opt[0], g:opt[1])
1449
1450
1451 " 30a: Setting global boolean local (to buffer) option"
1452 noa setglobal nocindent " Reset global and local value (without triggering autocmd)
1453 noa setlocal cindent
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001454 let g:options = [['cindent', '0', '', '0', '1', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001455 setglobal cindent
1456 call assert_equal([], g:options)
1457 call assert_equal(g:opt[0], g:opt[1])
1458
1459 " 30b: Setting local boolean local (to buffer) option"
1460 noa setglobal nocindent " Reset global and local value (without triggering autocmd)
1461 noa setlocal cindent
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001462 let g:options = [['cindent', '1', '1', '', '0', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001463 setlocal nocindent
1464 call assert_equal([], g:options)
1465 call assert_equal(g:opt[0], g:opt[1])
1466
1467 " 30c: Setting again boolean local (to buffer) option"
1468 noa setglobal nocindent " Reset global and local value (without triggering autocmd)
1469 noa setlocal cindent
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001470 let g:options = [['cindent', '1', '1', '0', '1', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001471 set cindent
1472 call assert_equal([], g:options)
1473 call assert_equal(g:opt[0], g:opt[1])
1474
1475 " 30d: Setting again global boolean local (to buffer) option"
1476 noa set nocindent " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001477 let g:options = [['cindent', '0', '0', '0', '1', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001478 set cindent
1479 call assert_equal([], g:options)
1480 call assert_equal(g:opt[0], g:opt[1])
1481
1482
1483 " 31: Setting boolean global-local (to window) option
1484 " Currently no such option exists.
1485
1486
1487 " 32a: Setting global boolean local (to window) option"
1488 noa setglobal nocursorcolumn " Reset global and local value (without triggering autocmd)
1489 noa setlocal cursorcolumn
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001490 let g:options = [['cursorcolumn', '0', '', '0', '1', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001491 setglobal cursorcolumn
1492 call assert_equal([], g:options)
1493 call assert_equal(g:opt[0], g:opt[1])
1494
1495 " 32b: Setting local boolean local (to window) option"
1496 noa setglobal nocursorcolumn " Reset global and local value (without triggering autocmd)
1497 noa setlocal cursorcolumn
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001498 let g:options = [['cursorcolumn', '1', '1', '', '0', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001499 setlocal nocursorcolumn
1500 call assert_equal([], g:options)
1501 call assert_equal(g:opt[0], g:opt[1])
1502
1503 " 32c: Setting again boolean local (to window) option"
1504 noa setglobal nocursorcolumn " Reset global and local value (without triggering autocmd)
1505 noa setlocal cursorcolumn
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001506 let g:options = [['cursorcolumn', '1', '1', '0', '1', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001507 set cursorcolumn
1508 call assert_equal([], g:options)
1509 call assert_equal(g:opt[0], g:opt[1])
1510
1511 " 32d: Setting again global boolean local (to window) option"
1512 noa set nocursorcolumn " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001513 let g:options = [['cursorcolumn', '0', '0', '0', '1', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001514 set cursorcolumn
1515 call assert_equal([], g:options)
1516 call assert_equal(g:opt[0], g:opt[1])
1517
1518
Bram Moolenaar1bc353b2019-09-01 14:45:28 +02001519 " 33: Test autocommands when an option value is converted internally.
Bram Moolenaard7c96872019-06-15 17:12:48 +02001520 noa set backspace=1 " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001521 let g:options = [['backspace', 'indent,eol', 'indent,eol', 'indent,eol', '2', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001522 set backspace=2
1523 call assert_equal([], g:options)
1524 call assert_equal(g:opt[0], g:opt[1])
1525
1526
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001527 " Cleanup
1528 au! OptionSet
Bram Moolenaar0331faf2019-06-15 18:40:37 +02001529 " set tags&
Bram Moolenaard7c96872019-06-15 17:12:48 +02001530 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 +02001531 exe printf(":set %s&vim", opt)
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001532 endfor
1533 call test_override('starting', 0)
1534 delfunc! AutoCommandOptionSet
1535endfunc
1536
1537func Test_OptionSet_diffmode()
1538 call test_override('starting', 1)
Bram Moolenaar26d98212019-01-27 22:32:55 +01001539 " 18: Changing an option when entering diff mode
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001540 new
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001541 au OptionSet diff :let &l:cul = v:option_new
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001542
1543 call setline(1, ['buffer 1', 'line2', 'line3', 'line4'])
1544 call assert_equal(0, &l:cul)
1545 diffthis
1546 call assert_equal(1, &l:cul)
1547
1548 vnew
1549 call setline(1, ['buffer 2', 'line 2', 'line 3', 'line4'])
1550 call assert_equal(0, &l:cul)
1551 diffthis
1552 call assert_equal(1, &l:cul)
1553
1554 diffoff
1555 call assert_equal(0, &l:cul)
1556 call assert_equal(1, getwinvar(2, '&l:cul'))
1557 bw!
1558
1559 call assert_equal(1, &l:cul)
1560 diffoff!
1561 call assert_equal(0, &l:cul)
1562 call assert_equal(0, getwinvar(1, '&l:cul'))
1563 bw!
1564
1565 " Cleanup
1566 au! OptionSet
1567 call test_override('starting', 0)
1568endfunc
1569
1570func Test_OptionSet_diffmode_close()
1571 call test_override('starting', 1)
1572 " 19: Try to close the current window when entering diff mode
1573 " should not segfault
1574 new
1575 au OptionSet diff close
1576
1577 call setline(1, ['buffer 1', 'line2', 'line3', 'line4'])
Bram Moolenaare2e40752020-09-04 21:18:46 +02001578 call assert_fails(':diffthis', 'E788:')
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001579 call assert_equal(1, &diff)
1580 vnew
1581 call setline(1, ['buffer 2', 'line 2', 'line 3', 'line4'])
Bram Moolenaare2e40752020-09-04 21:18:46 +02001582 call assert_fails(':diffthis', 'E788:')
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001583 call assert_equal(1, &diff)
Bram Moolenaara9aa86f2019-11-10 21:25:45 +01001584 set diffopt-=closeoff
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001585 bw!
Bram Moolenaare2e40752020-09-04 21:18:46 +02001586 call assert_fails(':diffoff!', 'E788:')
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001587 bw!
1588
1589 " Cleanup
1590 au! OptionSet
1591 call test_override('starting', 0)
1592 "delfunc! AutoCommandOptionSet
1593endfunc
Bram Moolenaar4a137b42017-08-04 22:37:11 +02001594
1595" Test for Bufleave autocommand that deletes the buffer we are about to edit.
1596func Test_BufleaveWithDelete()
Bram Moolenaare7cda972022-08-29 11:02:59 +01001597 new | edit XbufLeave1
Bram Moolenaar4a137b42017-08-04 22:37:11 +02001598
1599 augroup test_bufleavewithdelete
1600 autocmd!
Bram Moolenaare7cda972022-08-29 11:02:59 +01001601 autocmd BufLeave XbufLeave1 bwipe XbufLeave2
Bram Moolenaar4a137b42017-08-04 22:37:11 +02001602 augroup END
1603
Bram Moolenaare7cda972022-08-29 11:02:59 +01001604 call assert_fails('edit XbufLeave2', 'E143:')
1605 call assert_equal('XbufLeave1', bufname('%'))
Bram Moolenaar4a137b42017-08-04 22:37:11 +02001606
Bram Moolenaare7cda972022-08-29 11:02:59 +01001607 autocmd! test_bufleavewithdelete BufLeave XbufLeave1
Bram Moolenaar4a137b42017-08-04 22:37:11 +02001608 augroup! test_bufleavewithdelete
1609
1610 new
Bram Moolenaare7cda972022-08-29 11:02:59 +01001611 bwipe! XbufLeave1
Bram Moolenaar4a137b42017-08-04 22:37:11 +02001612endfunc
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +02001613
1614" Test for autocommand that changes the buffer list, when doing ":ball".
1615func Test_Acmd_BufAll()
1616 enew!
1617 %bwipe!
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01001618 call writefile(['Test file Xxx1'], 'Xxx1', 'D')
1619 call writefile(['Test file Xxx2'], 'Xxx2', 'D')
1620 call writefile(['Test file Xxx3'], 'Xxx3', 'D')
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +02001621
1622 " Add three files to the buffer list
1623 split Xxx1
1624 close
1625 split Xxx2
1626 close
1627 split Xxx3
1628 close
1629
1630 " Wipe the buffer when the buffer is opened
1631 au BufReadPost Xxx2 bwipe
1632
1633 call append(0, 'Test file Xxx4')
1634 ball
1635
1636 call assert_equal(2, winnr('$'))
1637 call assert_equal('Xxx1', bufname(winbufnr(winnr('$'))))
1638 wincmd t
1639
1640 au! BufReadPost
1641 %bwipe!
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +02001642 enew! | only
1643endfunc
1644
1645" Test for autocommand that changes current buffer on BufEnter event.
1646" Check if modelines are interpreted for the correct buffer.
1647func Test_Acmd_BufEnter()
1648 %bwipe!
1649 call writefile(['start of test file Xxx1',
1650 \ "\<Tab>this is a test",
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01001651 \ 'end of test file Xxx1'], 'Xxx1', 'D')
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +02001652 call writefile(['start of test file Xxx2',
1653 \ 'vim: set noai :',
1654 \ "\<Tab>this is a test",
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01001655 \ 'end of test file Xxx2'], 'Xxx2', 'D')
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +02001656
1657 au BufEnter Xxx2 brew
1658 set ai modeline modelines=3
1659 edit Xxx1
1660 " edit Xxx2, autocmd will do :brew
1661 edit Xxx2
1662 exe "normal G?this is a\<CR>"
1663 " Append text with autoindent to this file
1664 normal othis should be auto-indented
1665 call assert_equal("\<Tab>this should be auto-indented", getline('.'))
1666 call assert_equal(3, line('.'))
1667 " Remove autocmd and edit Xxx2 again
1668 au! BufEnter Xxx2
1669 buf! Xxx2
1670 exe "normal G?this is a\<CR>"
1671 " append text without autoindent to Xxx
1672 normal othis should be in column 1
1673 call assert_equal("this should be in column 1", getline('.'))
1674 call assert_equal(4, line('.'))
1675
1676 %bwipe!
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +02001677 set ai&vim modeline&vim modelines&vim
1678endfunc
1679
1680" Test for issue #57
1681" do not move cursor on <c-o> when autoindent is set
1682func Test_ai_CTRL_O()
1683 enew!
1684 set ai
1685 let save_fo = &fo
1686 set fo+=r
1687 exe "normal o# abcdef\<Esc>2hi\<CR>\<C-O>d0\<Esc>"
1688 exe "normal o# abcdef\<Esc>2hi\<C-O>d0\<Esc>"
1689 call assert_equal(['# abc', 'def', 'def'], getline(2, 4))
1690
1691 set ai&vim
1692 let &fo = save_fo
1693 enew!
1694endfunc
1695
1696" Test for autocommand that deletes the current buffer on BufLeave event.
1697" Also test deleting the last buffer, should give a new, empty buffer.
1698func Test_BufLeave_Wipe()
1699 %bwipe!
1700 let content = ['start of test file Xxx',
1701 \ 'this is a test',
1702 \ 'end of test file Xxx']
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01001703 call writefile(content, 'Xxx1', 'D')
1704 call writefile(content, 'Xxx2', 'D')
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +02001705
1706 au BufLeave Xxx2 bwipe
1707 edit Xxx1
1708 split Xxx2
1709 " delete buffer Xxx2, we should be back to Xxx1
1710 bwipe
1711 call assert_equal('Xxx1', bufname('%'))
1712 call assert_equal(1, winnr('$'))
1713
1714 " Create an alternate buffer
1715 %write! test.out
1716 call assert_equal('test.out', bufname('#'))
1717 " delete alternate buffer
1718 bwipe test.out
1719 call assert_equal('Xxx1', bufname('%'))
1720 call assert_equal('', bufname('#'))
1721
1722 au BufLeave Xxx1 bwipe
1723 " delete current buffer, get an empty one
1724 bwipe!
1725 call assert_equal(1, line('$'))
1726 call assert_equal('', bufname('%'))
Bram Moolenaarb2c87502017-10-14 21:15:58 +02001727 let g:bufinfo = getbufinfo()
1728 call assert_equal(1, len(g:bufinfo))
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +02001729
Bram Moolenaar53f0c962017-10-22 14:23:59 +02001730 call delete('test.out')
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +02001731 %bwipe
1732 au! BufLeave
Bram Moolenaarb2c87502017-10-14 21:15:58 +02001733
1734 " check that bufinfo doesn't contain a pointer to freed memory
1735 call test_garbagecollect_now()
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +02001736endfunc
Bram Moolenaar87ffb5c2017-10-19 12:37:42 +02001737
1738func Test_QuitPre()
1739 edit Xfoo
1740 let winid = win_getid(winnr())
1741 split Xbar
1742 au! QuitPre * let g:afile = expand('<afile>')
1743 " Close the other window, <afile> should be correct.
1744 exe win_id2win(winid) . 'q'
1745 call assert_equal('Xfoo', g:afile)
LemonBoy66e13ae2022-04-21 22:52:11 +01001746
Bram Moolenaar87ffb5c2017-10-19 12:37:42 +02001747 unlet g:afile
1748 bwipe Xfoo
1749 bwipe Xbar
1750endfunc
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02001751
1752func Test_Cmdline()
Bram Moolenaar153b7042018-01-31 15:48:32 +01001753 au! CmdlineChanged : let g:text = getcmdline()
1754 let g:text = 0
1755 call feedkeys(":echom 'hello'\<CR>", 'xt')
1756 call assert_equal("echom 'hello'", g:text)
1757 au! CmdlineChanged
1758
1759 au! CmdlineChanged : let g:entered = expand('<afile>')
1760 let g:entered = 0
1761 call feedkeys(":echom 'hello'\<CR>", 'xt')
1762 call assert_equal(':', g:entered)
1763 au! CmdlineChanged
1764
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02001765 au! CmdlineEnter : let g:entered = expand('<afile>')
1766 au! CmdlineLeave : let g:left = expand('<afile>')
1767 let g:entered = 0
1768 let g:left = 0
1769 call feedkeys(":echo 'hello'\<CR>", 'xt')
1770 call assert_equal(':', g:entered)
1771 call assert_equal(':', g:left)
1772 au! CmdlineEnter
1773 au! CmdlineLeave
1774
Bram Moolenaara4baf5b2018-04-22 13:27:44 +02001775 let save_shellslash = &shellslash
1776 set noshellslash
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02001777 au! CmdlineEnter / let g:entered = expand('<afile>')
1778 au! CmdlineLeave / let g:left = expand('<afile>')
1779 let g:entered = 0
1780 let g:left = 0
Bram Moolenaar53f0c962017-10-22 14:23:59 +02001781 new
1782 call setline(1, 'hello')
1783 call feedkeys("/hello\<CR>", 'xt')
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02001784 call assert_equal('/', g:entered)
1785 call assert_equal('/', g:left)
Bram Moolenaar53f0c962017-10-22 14:23:59 +02001786 bwipe!
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02001787 au! CmdlineEnter
1788 au! CmdlineLeave
Bram Moolenaara4baf5b2018-04-22 13:27:44 +02001789 let &shellslash = save_shellslash
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02001790endfunc
Bram Moolenaar53f0c962017-10-22 14:23:59 +02001791
1792" Test for BufWritePre autocommand that deletes or unloads the buffer.
1793func Test_BufWritePre()
1794 %bwipe
1795 au BufWritePre Xxx1 bunload
1796 au BufWritePre Xxx2 bwipe
1797
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01001798 call writefile(['start of Xxx1', 'test', 'end of Xxx1'], 'Xxx1', 'D')
1799 call writefile(['start of Xxx2', 'test', 'end of Xxx2'], 'Xxx2', 'D')
Bram Moolenaar53f0c962017-10-22 14:23:59 +02001800
1801 edit Xtest
1802 e! Xxx2
1803 bdel Xtest
1804 e Xxx1
1805 " write it, will unload it and give an error msg
Bram Moolenaare2e40752020-09-04 21:18:46 +02001806 call assert_fails('w', 'E203:')
Bram Moolenaar53f0c962017-10-22 14:23:59 +02001807 call assert_equal('Xxx2', bufname('%'))
1808 edit Xtest
1809 e! Xxx2
1810 bwipe Xtest
1811 " write it, will delete the buffer and give an error msg
Bram Moolenaare2e40752020-09-04 21:18:46 +02001812 call assert_fails('w', 'E203:')
Bram Moolenaar53f0c962017-10-22 14:23:59 +02001813 call assert_equal('Xxx1', bufname('%'))
1814 au! BufWritePre
Bram Moolenaar53f0c962017-10-22 14:23:59 +02001815endfunc
1816
1817" Test for BufUnload autocommand that unloads all the other buffers
1818func Test_bufunload_all()
Bram Moolenaarf08b0eb2021-10-16 13:00:14 +01001819 let g:test_is_flaky = 1
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01001820 call writefile(['Test file Xxx1'], 'Xxx1', 'D')"
1821 call writefile(['Test file Xxx2'], 'Xxx2', 'D')"
Bram Moolenaar53f0c962017-10-22 14:23:59 +02001822
Bram Moolenaarc79745a2019-05-20 22:12:34 +02001823 let content =<< trim [CODE]
1824 func UnloadAllBufs()
1825 let i = 1
1826 while i <= bufnr('$')
1827 if i != bufnr('%') && bufloaded(i)
1828 exe i . 'bunload'
1829 endif
1830 let i += 1
1831 endwhile
1832 endfunc
1833 au BufUnload * call UnloadAllBufs()
1834 au VimLeave * call writefile(['Test Finished'], 'Xout')
1835 edit Xxx1
1836 split Xxx2
1837 q
1838 [CODE]
1839
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01001840 call writefile(content, 'Xbunloadtest', 'D')
Bram Moolenaar53f0c962017-10-22 14:23:59 +02001841
1842 call delete('Xout')
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01001843 call system(GetVimCommandClean() .. ' -N --not-a-term -S Xbunloadtest')
Bram Moolenaar53f0c962017-10-22 14:23:59 +02001844 call assert_true(filereadable('Xout'))
1845
Bram Moolenaar53f0c962017-10-22 14:23:59 +02001846 call delete('Xout')
1847endfunc
1848
1849" Some tests for buffer-local autocommands
1850func Test_buflocal_autocmd()
1851 let g:bname = ''
1852 edit xx
1853 au BufLeave <buffer> let g:bname = expand("%")
1854 " here, autocommand for xx should trigger.
1855 " but autocommand shall not apply to buffer named <buffer>.
1856 edit somefile
1857 call assert_equal('xx', g:bname)
1858 let g:bname = ''
1859 " here, autocommand shall be auto-deleted
1860 bwipe xx
1861 " autocmd should not trigger
1862 edit xx
1863 call assert_equal('', g:bname)
1864 " autocmd should not trigger
1865 edit somefile
1866 call assert_equal('', g:bname)
1867 enew
1868 unlet g:bname
1869endfunc
Bram Moolenaar430dc5d2017-11-02 21:04:47 +01001870
1871" Test for "*Cmd" autocommands
1872func Test_Cmd_Autocmds()
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01001873 call writefile(['start of Xxx', "\tabc2", 'end of Xxx'], 'Xxx', 'D')
Bram Moolenaar430dc5d2017-11-02 21:04:47 +01001874
1875 enew!
1876 au BufReadCmd XtestA 0r Xxx|$del
1877 edit XtestA " will read text of Xxd instead
1878 call assert_equal('start of Xxx', getline(1))
1879
1880 au BufWriteCmd XtestA call append(line("$"), "write")
1881 write " will append a line to the file
1882 call assert_equal('write', getline('$'))
Bram Moolenaare2e40752020-09-04 21:18:46 +02001883 call assert_fails('read XtestA', 'E484:') " should not read anything
Bram Moolenaar430dc5d2017-11-02 21:04:47 +01001884 call assert_equal('write', getline(4))
1885
1886 " now we have:
1887 " 1 start of Xxx
1888 " 2 abc2
1889 " 3 end of Xxx
1890 " 4 write
1891
1892 au FileReadCmd XtestB '[r Xxx
1893 2r XtestB " will read Xxx below line 2 instead
1894 call assert_equal('start of Xxx', getline(3))
1895
1896 " now we have:
1897 " 1 start of Xxx
1898 " 2 abc2
1899 " 3 start of Xxx
1900 " 4 abc2
1901 " 5 end of Xxx
1902 " 6 end of Xxx
1903 " 7 write
1904
1905 au FileWriteCmd XtestC '[,']copy $
1906 normal 4GA1
1907 4,5w XtestC " will copy lines 4 and 5 to the end
1908 call assert_equal("\tabc21", getline(8))
Bram Moolenaare2e40752020-09-04 21:18:46 +02001909 call assert_fails('r XtestC', 'E484:') " should not read anything
Bram Moolenaar430dc5d2017-11-02 21:04:47 +01001910 call assert_equal("end of Xxx", getline(9))
1911
1912 " now we have:
1913 " 1 start of Xxx
1914 " 2 abc2
1915 " 3 start of Xxx
1916 " 4 abc21
1917 " 5 end of Xxx
1918 " 6 end of Xxx
1919 " 7 write
1920 " 8 abc21
1921 " 9 end of Xxx
1922
1923 let g:lines = []
1924 au FileAppendCmd XtestD call extend(g:lines, getline(line("'["), line("']")))
1925 w >>XtestD " will add lines to 'lines'
1926 call assert_equal(9, len(g:lines))
Bram Moolenaare2e40752020-09-04 21:18:46 +02001927 call assert_fails('$r XtestD', 'E484:') " should not read anything
Bram Moolenaar430dc5d2017-11-02 21:04:47 +01001928 call assert_equal(9, line('$'))
1929 call assert_equal('end of Xxx', getline('$'))
1930
1931 au BufReadCmd XtestE 0r Xxx|$del
1932 sp XtestE " split window with test.out
1933 call assert_equal('end of Xxx', getline(3))
1934
1935 let g:lines = []
1936 exe "normal 2Goasdf\<Esc>\<C-W>\<C-W>"
1937 au BufWriteCmd XtestE call extend(g:lines, getline(0, '$'))
1938 wall " will write other window to 'lines'
1939 call assert_equal(4, len(g:lines), g:lines)
1940 call assert_equal('asdf', g:lines[2])
1941
1942 au! BufReadCmd
1943 au! BufWriteCmd
1944 au! FileReadCmd
1945 au! FileWriteCmd
1946 au! FileAppendCmd
1947 %bwipe!
Bram Moolenaar430dc5d2017-11-02 21:04:47 +01001948 enew!
1949endfunc
Bram Moolenaaraace2152017-11-05 16:23:10 +01001950
Bram Moolenaar0fff4412020-03-29 16:06:29 +02001951func s:ReadFile()
1952 setl noswapfile nomodified
1953 let filename = resolve(expand("<afile>:p"))
1954 execute 'read' fnameescape(filename)
1955 1d_
1956 exe 'file' fnameescape(filename)
1957 setl buftype=acwrite
1958endfunc
1959
1960func s:WriteFile()
1961 let filename = resolve(expand("<afile>:p"))
1962 setl buftype=
1963 noautocmd execute 'write' fnameescape(filename)
1964 setl buftype=acwrite
1965 setl nomodified
1966endfunc
1967
1968func Test_BufReadCmd()
1969 autocmd BufReadCmd *.test call s:ReadFile()
1970 autocmd BufWriteCmd *.test call s:WriteFile()
1971
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01001972 call writefile(['one', 'two', 'three'], 'Xcmd.test', 'D')
Bram Moolenaar0fff4412020-03-29 16:06:29 +02001973 edit Xcmd.test
1974 call assert_match('Xcmd.test" line 1 of 3', execute('file'))
1975 normal! Gofour
1976 write
1977 call assert_equal(['one', 'two', 'three', 'four'], readfile('Xcmd.test'))
1978
1979 bwipe!
Bram Moolenaar0fff4412020-03-29 16:06:29 +02001980 au! BufReadCmd
1981 au! BufWriteCmd
1982endfunc
1983
zeertzjq9c8f9462022-08-30 18:17:15 +01001984func Test_BufWriteCmd()
1985 autocmd BufWriteCmd Xbufwritecmd let g:written = 1
1986 new
1987 file Xbufwritecmd
1988 set buftype=acwrite
Bram Moolenaar6f14da12022-09-07 21:30:44 +01001989 call mkdir('Xbufwritecmd', 'D')
zeertzjq9c8f9462022-08-30 18:17:15 +01001990 write
1991 " BufWriteCmd should be triggered even if a directory has the same name
1992 call assert_equal(1, g:written)
zeertzjq9c8f9462022-08-30 18:17:15 +01001993 unlet g:written
1994 au! BufWriteCmd
1995 bwipe!
1996endfunc
1997
Bram Moolenaaraace2152017-11-05 16:23:10 +01001998func SetChangeMarks(start, end)
Bram Moolenaar97c69432021-01-15 16:45:21 +01001999 exe a:start .. 'mark ['
2000 exe a:end .. 'mark ]'
Bram Moolenaaraace2152017-11-05 16:23:10 +01002001endfunc
2002
2003" Verify the effects of autocmds on '[ and ']
2004func Test_change_mark_in_autocmds()
2005 edit! Xtest
Bram Moolenaar97c69432021-01-15 16:45:21 +01002006 call feedkeys("ia\<CR>b\<CR>c\<CR>d\<C-g>u\<Esc>", 'xtn')
Bram Moolenaaraace2152017-11-05 16:23:10 +01002007
2008 call SetChangeMarks(2, 3)
2009 write
2010 call assert_equal([1, 4], [line("'["), line("']")])
2011
2012 call SetChangeMarks(2, 3)
2013 au BufWritePre * call assert_equal([1, 4], [line("'["), line("']")])
2014 write
2015 au! BufWritePre
2016
Bram Moolenaar14ddd222020-08-05 12:02:40 +02002017 if has('unix')
Bram Moolenaaraace2152017-11-05 16:23:10 +01002018 write XtestFilter
2019 write >> XtestFilter
2020
2021 call SetChangeMarks(2, 3)
2022 " Marks are set to the entire range of the write
2023 au FilterWritePre * call assert_equal([1, 4], [line("'["), line("']")])
2024 " '[ is adjusted to just before the line that will receive the filtered
2025 " data
2026 au FilterReadPre * call assert_equal([4, 4], [line("'["), line("']")])
2027 " The filtered data is read into the buffer, and the source lines are
2028 " still present, so the range is after the source lines
2029 au FilterReadPost * call assert_equal([5, 12], [line("'["), line("']")])
2030 %!cat XtestFilter
2031 " After the filtered data is read, the original lines are deleted
2032 call assert_equal([1, 8], [line("'["), line("']")])
2033 au! FilterWritePre,FilterReadPre,FilterReadPost
2034 undo
2035
2036 call SetChangeMarks(1, 4)
2037 au FilterWritePre * call assert_equal([2, 3], [line("'["), line("']")])
2038 au FilterReadPre * call assert_equal([3, 3], [line("'["), line("']")])
2039 au FilterReadPost * call assert_equal([4, 11], [line("'["), line("']")])
2040 2,3!cat XtestFilter
2041 call assert_equal([2, 9], [line("'["), line("']")])
2042 au! FilterWritePre,FilterReadPre,FilterReadPost
2043 undo
2044
2045 call delete('XtestFilter')
2046 endif
2047
2048 call SetChangeMarks(1, 4)
2049 au FileWritePre * call assert_equal([2, 3], [line("'["), line("']")])
2050 2,3write Xtest2
2051 au! FileWritePre
2052
2053 call SetChangeMarks(2, 3)
2054 au FileAppendPre * call assert_equal([1, 4], [line("'["), line("']")])
2055 write >> Xtest2
2056 au! FileAppendPre
2057
2058 call SetChangeMarks(1, 4)
2059 au FileAppendPre * call assert_equal([2, 3], [line("'["), line("']")])
2060 2,3write >> Xtest2
2061 au! FileAppendPre
2062
2063 call SetChangeMarks(1, 1)
2064 au FileReadPre * call assert_equal([3, 1], [line("'["), line("']")])
2065 au FileReadPost * call assert_equal([4, 11], [line("'["), line("']")])
2066 3read Xtest2
2067 au! FileReadPre,FileReadPost
2068 undo
2069
2070 call SetChangeMarks(4, 4)
2071 " When the line is 0, it's adjusted to 1
2072 au FileReadPre * call assert_equal([1, 4], [line("'["), line("']")])
2073 au FileReadPost * call assert_equal([1, 8], [line("'["), line("']")])
2074 0read Xtest2
2075 au! FileReadPre,FileReadPost
2076 undo
2077
2078 call SetChangeMarks(4, 4)
2079 " When the line is 0, it's adjusted to 1
2080 au FileReadPre * call assert_equal([1, 4], [line("'["), line("']")])
2081 au FileReadPost * call assert_equal([2, 9], [line("'["), line("']")])
2082 1read Xtest2
2083 au! FileReadPre,FileReadPost
2084 undo
2085
2086 bwipe!
2087 call delete('Xtest')
2088 call delete('Xtest2')
2089endfunc
2090
2091func Test_Filter_noshelltemp()
Bram Moolenaaraeb313f2020-11-27 19:13:28 +01002092 CheckExecutable cat
Bram Moolenaaraace2152017-11-05 16:23:10 +01002093
2094 enew!
2095 call setline(1, ['a', 'b', 'c', 'd'])
2096
2097 let shelltemp = &shelltemp
2098 set shelltemp
2099
2100 let g:filter_au = 0
2101 au FilterWritePre * let g:filter_au += 1
2102 au FilterReadPre * let g:filter_au += 1
2103 au FilterReadPost * let g:filter_au += 1
2104 %!cat
2105 call assert_equal(3, g:filter_au)
2106
2107 if has('filterpipe')
2108 set noshelltemp
2109
2110 let g:filter_au = 0
2111 au FilterWritePre * let g:filter_au += 1
2112 au FilterReadPre * let g:filter_au += 1
2113 au FilterReadPost * let g:filter_au += 1
2114 %!cat
2115 call assert_equal(0, g:filter_au)
2116 endif
2117
2118 au! FilterWritePre,FilterReadPre,FilterReadPost
2119 let &shelltemp = shelltemp
2120 bwipe!
2121endfunc
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01002122
2123func Test_TextYankPost()
2124 enew!
2125 call setline(1, ['foo'])
2126
2127 let g:event = []
2128 au TextYankPost * let g:event = copy(v:event)
2129
2130 call assert_equal({}, v:event)
2131 call assert_fails('let v:event = {}', 'E46:')
2132 call assert_fails('let v:event.mykey = 0', 'E742:')
2133
2134 norm "ayiw
2135 call assert_equal(
Bram Moolenaara016eeb2022-04-09 11:37:38 +01002136 \ #{regcontents: ['foo'], regname: 'a', operator: 'y',
2137 \ regtype: 'v', visual: v:false, inclusive: v:true},
2138 \ g:event)
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01002139 norm y_
2140 call assert_equal(
Bram Moolenaara016eeb2022-04-09 11:37:38 +01002141 \ #{regcontents: ['foo'], regname: '', operator: 'y', regtype: 'V',
2142 \ visual: v:false, inclusive: v:false},
2143 \ g:event)
Bram Moolenaar37d16732020-06-12 22:09:01 +02002144 norm Vy
2145 call assert_equal(
Bram Moolenaara016eeb2022-04-09 11:37:38 +01002146 \ #{regcontents: ['foo'], regname: '', operator: 'y', regtype: 'V',
2147 \ visual: v:true, inclusive: v:true},
2148 \ g:event)
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01002149 call feedkeys("\<C-V>y", 'x')
2150 call assert_equal(
Bram Moolenaara016eeb2022-04-09 11:37:38 +01002151 \ #{regcontents: ['f'], regname: '', operator: 'y', regtype: "\x161",
2152 \ visual: v:true, inclusive: v:true},
2153 \ g:event)
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01002154 norm "xciwbar
2155 call assert_equal(
Bram Moolenaara016eeb2022-04-09 11:37:38 +01002156 \ #{regcontents: ['foo'], regname: 'x', operator: 'c', regtype: 'v',
2157 \ visual: v:false, inclusive: v:true},
2158 \ g:event)
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01002159 norm "bdiw
2160 call assert_equal(
Bram Moolenaara016eeb2022-04-09 11:37:38 +01002161 \ #{regcontents: ['bar'], regname: 'b', operator: 'd', regtype: 'v',
2162 \ visual: v:false, inclusive: v:true},
2163 \ g:event)
2164
2165 call setline(1, 'foobar')
2166 " exclusive motion
2167 norm $"ay0
2168 call assert_equal(
2169 \ #{regcontents: ['fooba'], regname: 'a', operator: 'y', regtype: 'v',
2170 \ visual: v:false, inclusive: v:false},
2171 \ g:event)
2172 " inclusive motion
2173 norm 0"ay$
2174 call assert_equal(
2175 \ #{regcontents: ['foobar'], regname: 'a', operator: 'y', regtype: 'v',
2176 \ visual: v:false, inclusive: v:true},
2177 \ g:event)
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01002178
2179 call assert_equal({}, v:event)
2180
Bram Moolenaarfccbf062020-11-26 20:34:00 +01002181 if has('clipboard_working') && !has('gui_running')
2182 " Test that when the visual selection is automatically copied to clipboard
2183 " register a TextYankPost is emitted
2184 call setline(1, ['foobar'])
2185
2186 let @* = ''
2187 set clipboard=autoselect
2188 exe "norm! ggviw\<Esc>"
2189 call assert_equal(
Bram Moolenaara016eeb2022-04-09 11:37:38 +01002190 \ #{regcontents: ['foobar'], regname: '*', operator: 'y',
2191 \ regtype: 'v', visual: v:true, inclusive: v:false},
2192 \ g:event)
Bram Moolenaarfccbf062020-11-26 20:34:00 +01002193
2194 let @+ = ''
2195 set clipboard=autoselectplus
2196 exe "norm! ggviw\<Esc>"
2197 call assert_equal(
Bram Moolenaara016eeb2022-04-09 11:37:38 +01002198 \ #{regcontents: ['foobar'], regname: '+', operator: 'y',
2199 \ regtype: 'v', visual: v:true, inclusive: v:false},
2200 \ g:event)
Bram Moolenaarfccbf062020-11-26 20:34:00 +01002201
2202 set clipboard&vim
2203 endif
2204
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01002205 au! TextYankPost
2206 unlet g:event
2207 bwipe!
2208endfunc
Bram Moolenaar9bca8052017-12-18 12:37:55 +01002209
Bram Moolenaar9a046fd2021-01-28 13:47:59 +01002210func Test_autocommand_all_events()
2211 call assert_fails('au * * bwipe', 'E1155:')
2212 call assert_fails('au * x bwipe', 'E1155:')
Bram Moolenaarb6db1462021-12-24 19:24:47 +00002213 call assert_fails('au! * x bwipe', 'E1155:')
Bram Moolenaar4fb921e2017-12-18 15:33:00 +01002214endfunc
Bram Moolenaarb7407d32018-02-03 17:36:27 +01002215
Bram Moolenaarf6246f52022-02-11 16:30:12 +00002216func Test_autocmd_user()
2217 au User MyEvent let s:res = [expand("<afile>"), expand("<amatch>")]
2218 doautocmd User MyEvent
2219 call assert_equal(['MyEvent', 'MyEvent'], s:res)
2220 au! User
2221 unlet s:res
2222endfunc
2223
Bram Moolenaar3b014be2022-11-13 17:53:46 +00002224func Test_autocmd_user_clear_group()
2225 CheckRunVimInTerminal
2226
2227 let lines =<< trim END
2228 autocmd! User
2229 for i in range(1, 999)
2230 exe 'autocmd User ' .. 'Foo' .. i .. ' bar'
2231 endfor
2232 au CmdlineLeave : call timer_start(0, {-> execute('autocmd! User')})
2233 END
2234 call writefile(lines, 'XautoUser', 'D')
2235 let buf = RunVimInTerminal('-S XautoUser', {'rows': 10})
2236
2237 " this was using freed memory
2238 call term_sendkeys(buf, ":autocmd User\<CR>")
2239 call TermWait(buf, 50)
2240 call term_sendkeys(buf, "G")
2241
2242 call StopVimInTerminal(buf)
2243endfunc
2244
Bram Moolenaarb7407d32018-02-03 17:36:27 +01002245function s:Before_test_dirchanged()
2246 augroup test_dirchanged
2247 autocmd!
2248 augroup END
2249 let s:li = []
2250 let s:dir_this = getcwd()
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02002251 let s:dir_foo = s:dir_this . '/Xfoo'
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01002252 call mkdir(s:dir_foo)
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02002253 let s:dir_bar = s:dir_this . '/Xbar'
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01002254 call mkdir(s:dir_bar)
Bram Moolenaarb7407d32018-02-03 17:36:27 +01002255endfunc
2256
2257function s:After_test_dirchanged()
Bram Moolenaar3503d7c2019-11-09 20:10:17 +01002258 call chdir(s:dir_this)
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01002259 call delete(s:dir_foo, 'd')
2260 call delete(s:dir_bar, 'd')
Bram Moolenaarb7407d32018-02-03 17:36:27 +01002261 augroup test_dirchanged
2262 autocmd!
2263 augroup END
2264endfunc
2265
2266function Test_dirchanged_global()
2267 call s:Before_test_dirchanged()
Bram Moolenaarf6246f52022-02-11 16:30:12 +00002268 autocmd test_dirchanged DirChangedPre global call add(s:li, expand("<amatch>") .. " pre cd " .. v:event.directory)
Bram Moolenaarb7407d32018-02-03 17:36:27 +01002269 autocmd test_dirchanged DirChanged global call add(s:li, "cd:")
2270 autocmd test_dirchanged DirChanged global call add(s:li, expand("<afile>"))
Bram Moolenaar3503d7c2019-11-09 20:10:17 +01002271 call chdir(s:dir_foo)
Bram Moolenaarf6246f52022-02-11 16:30:12 +00002272 let expected = ["global pre cd " .. s:dir_foo, "cd:", s:dir_foo]
Bram Moolenaar28e8f732022-02-09 12:58:20 +00002273 call assert_equal(expected, s:li)
Bram Moolenaar3503d7c2019-11-09 20:10:17 +01002274 call chdir(s:dir_foo)
Bram Moolenaar28e8f732022-02-09 12:58:20 +00002275 call assert_equal(expected, s:li)
Bram Moolenaar3503d7c2019-11-09 20:10:17 +01002276 exe 'lcd ' .. fnameescape(s:dir_bar)
Bram Moolenaar28e8f732022-02-09 12:58:20 +00002277 call assert_equal(expected, s:li)
Bram Moolenaard8c9d322022-06-12 11:49:16 +01002278
2279 exe 'cd ' .. s:dir_foo
2280 exe 'cd ' .. s:dir_bar
2281 autocmd! test_dirchanged DirChanged global let g:result = expand("<afile>")
2282 cd -
Bram Moolenaardb77c492022-06-12 23:26:50 +01002283 call assert_equal(s:dir_foo, substitute(g:result, '\\', '/', 'g'))
Bram Moolenaard8c9d322022-06-12 11:49:16 +01002284
Bram Moolenaarb7407d32018-02-03 17:36:27 +01002285 call s:After_test_dirchanged()
2286endfunc
2287
2288function Test_dirchanged_local()
2289 call s:Before_test_dirchanged()
2290 autocmd test_dirchanged DirChanged window call add(s:li, "lcd:")
2291 autocmd test_dirchanged DirChanged window call add(s:li, expand("<afile>"))
Bram Moolenaar3503d7c2019-11-09 20:10:17 +01002292 call chdir(s:dir_foo)
Bram Moolenaarb7407d32018-02-03 17:36:27 +01002293 call assert_equal([], s:li)
Bram Moolenaar3503d7c2019-11-09 20:10:17 +01002294 exe 'lcd ' .. fnameescape(s:dir_bar)
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01002295 call assert_equal(["lcd:", s:dir_bar], s:li)
Bram Moolenaar3503d7c2019-11-09 20:10:17 +01002296 exe 'lcd ' .. fnameescape(s:dir_bar)
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01002297 call assert_equal(["lcd:", s:dir_bar], s:li)
Bram Moolenaarb7407d32018-02-03 17:36:27 +01002298 call s:After_test_dirchanged()
2299endfunc
2300
2301function Test_dirchanged_auto()
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +02002302 CheckOption autochdir
Bram Moolenaarb7407d32018-02-03 17:36:27 +01002303 call s:Before_test_dirchanged()
2304 call test_autochdir()
Bram Moolenaar28e8f732022-02-09 12:58:20 +00002305 autocmd test_dirchanged DirChangedPre auto call add(s:li, "pre cd " .. v:event.directory)
Bram Moolenaarb7407d32018-02-03 17:36:27 +01002306 autocmd test_dirchanged DirChanged auto call add(s:li, "auto:")
2307 autocmd test_dirchanged DirChanged auto call add(s:li, expand("<afile>"))
2308 set acd
Bram Moolenaar3503d7c2019-11-09 20:10:17 +01002309 cd ..
Bram Moolenaarb7407d32018-02-03 17:36:27 +01002310 call assert_equal([], s:li)
Bram Moolenaar61abe7d2022-08-30 21:46:08 +01002311 exe 'edit ' . s:dir_foo . '/Xautofile'
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01002312 call assert_equal(s:dir_foo, getcwd())
Bram Moolenaar28e8f732022-02-09 12:58:20 +00002313 let expected = ["pre cd " .. s:dir_foo, "auto:", s:dir_foo]
2314 call assert_equal(expected, s:li)
Bram Moolenaarb7407d32018-02-03 17:36:27 +01002315 set noacd
2316 bwipe!
2317 call s:After_test_dirchanged()
2318endfunc
Bram Moolenaar5a093432018-02-10 18:15:19 +01002319
2320" Test TextChangedI and TextChangedP
2321func Test_ChangedP()
2322 new
2323 call setline(1, ['foo', 'bar', 'foobar'])
2324 call test_override("char_avail", 1)
2325 set complete=. completeopt=menuone
2326
2327 func! TextChangedAutocmd(char)
2328 let g:autocmd .= a:char
2329 endfunc
2330
Christian Brabandtdb3b4462021-10-16 11:58:55 +01002331 " TextChanged will not be triggered, only check that it isn't.
Bram Moolenaar5a093432018-02-10 18:15:19 +01002332 au! TextChanged <buffer> :call TextChangedAutocmd('N')
2333 au! TextChangedI <buffer> :call TextChangedAutocmd('I')
2334 au! TextChangedP <buffer> :call TextChangedAutocmd('P')
2335
2336 call cursor(3, 1)
2337 let g:autocmd = ''
2338 call feedkeys("o\<esc>", 'tnix')
2339 call assert_equal('I', g:autocmd)
2340
2341 let g:autocmd = ''
2342 call feedkeys("Sf", 'tnix')
2343 call assert_equal('II', g:autocmd)
2344
2345 let g:autocmd = ''
2346 call feedkeys("Sf\<C-N>", 'tnix')
2347 call assert_equal('IIP', g:autocmd)
2348
2349 let g:autocmd = ''
2350 call feedkeys("Sf\<C-N>\<C-N>", 'tnix')
2351 call assert_equal('IIPP', g:autocmd)
2352
2353 let g:autocmd = ''
2354 call feedkeys("Sf\<C-N>\<C-N>\<C-N>", 'tnix')
2355 call assert_equal('IIPPP', g:autocmd)
2356
2357 let g:autocmd = ''
2358 call feedkeys("Sf\<C-N>\<C-N>\<C-N>\<C-N>", 'tnix')
2359 call assert_equal('IIPPPP', g:autocmd)
2360
2361 call assert_equal(['foo', 'bar', 'foobar', 'foo'], getline(1, '$'))
2362 " TODO: how should it handle completeopt=noinsert,noselect?
2363
2364 " CleanUp
2365 call test_override("char_avail", 0)
2366 au! TextChanged
2367 au! TextChangedI
2368 au! TextChangedP
2369 delfu TextChangedAutocmd
2370 unlet! g:autocmd
2371 set complete&vim completeopt&vim
2372
2373 bw!
2374endfunc
Bram Moolenaar8c64a362018-03-23 22:39:31 +01002375
Bram Moolenaar91d2e782018-08-07 19:05:01 +02002376let g:setline_handled = v:false
Bram Moolenaar1e115362019-01-09 23:01:02 +01002377func SetLineOne()
Bram Moolenaar91d2e782018-08-07 19:05:01 +02002378 if !g:setline_handled
2379 call setline(1, "(x)")
2380 let g:setline_handled = v:true
2381 endif
2382endfunc
2383
2384func Test_TextChangedI_with_setline()
2385 new
2386 call test_override('char_avail', 1)
2387 autocmd TextChangedI <buffer> call SetLineOne()
2388 call feedkeys("i(\<CR>\<Esc>", 'tx')
2389 call assert_equal('(', getline(1))
2390 call assert_equal('x)', getline(2))
2391 undo
Bram Moolenaar91d2e782018-08-07 19:05:01 +02002392 call assert_equal('', getline(1))
Bram Moolenaar9fa95062018-08-08 22:08:32 +02002393 call assert_equal('', getline(2))
Bram Moolenaar91d2e782018-08-07 19:05:01 +02002394
Bram Moolenaarca34db32022-01-20 11:17:18 +00002395 call test_override('char_avail', 0)
Bram Moolenaar91d2e782018-08-07 19:05:01 +02002396 bwipe!
2397endfunc
2398
Bram Moolenaar8c64a362018-03-23 22:39:31 +01002399func Test_Changed_FirstTime()
Bram Moolenaar8c5a2782019-08-07 23:07:07 +02002400 CheckFeature terminal
2401 CheckNotGui
Bram Moolenaar3cdcb092020-03-18 19:18:10 +01002402 " Starting a terminal to run Vim is always considered flaky.
Bram Moolenaar30d53e22020-03-18 21:10:44 +01002403 let g:test_is_flaky = 1
Bram Moolenaar8c5a2782019-08-07 23:07:07 +02002404
Bram Moolenaar8c64a362018-03-23 22:39:31 +01002405 " Prepare file for TextChanged event.
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01002406 call writefile([''], 'Xchanged.txt', 'D')
Bram Moolenaar8c64a362018-03-23 22:39:31 +01002407 let buf = term_start([GetVimProg(), '--clean', '-c', 'set noswapfile'], {'term_rows': 3})
2408 call assert_equal('running', term_getstatus(buf))
Bram Moolenaar1834d372018-03-29 17:40:46 +02002409 " Wait for the ruler (in the status line) to be shown.
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01002410 " In ConPTY, there is additional character which is drawn up to the width of
2411 " the screen.
2412 if has('conpty')
2413 call WaitForAssert({-> assert_match('\<All.*$', term_getline(buf, 3))})
2414 else
2415 call WaitForAssert({-> assert_match('\<All$', term_getline(buf, 3))})
2416 endif
Bram Moolenaar8c64a362018-03-23 22:39:31 +01002417 " It's only adding autocmd, so that no event occurs.
2418 call term_sendkeys(buf, ":au! TextChanged <buffer> call writefile(['No'], 'Xchanged.txt')\<cr>")
2419 call term_sendkeys(buf, "\<C-\\>\<C-N>:qa!\<cr>")
Bram Moolenaar50182fa2018-04-28 21:34:40 +02002420 call WaitForAssert({-> assert_equal('finished', term_getstatus(buf))})
Bram Moolenaar8c64a362018-03-23 22:39:31 +01002421 call assert_equal([''], readfile('Xchanged.txt'))
2422
2423 " clean up
Bram Moolenaar8c64a362018-03-23 22:39:31 +01002424 bwipe!
2425endfunc
Bram Moolenaar0566e892019-01-24 19:37:40 +01002426
Bram Moolenaareb93f3f2019-04-04 15:04:56 +02002427func Test_autocmd_nested()
2428 let g:did_nested = 0
2429 augroup Testing
2430 au WinNew * edit somefile
2431 au BufNew * let g:did_nested = 1
2432 augroup END
2433 split
2434 call assert_equal(0, g:did_nested)
2435 close
2436 bwipe! somefile
2437
2438 " old nested argument still works
2439 augroup Testing
2440 au!
2441 au WinNew * nested edit somefile
2442 au BufNew * let g:did_nested = 1
2443 augroup END
2444 split
2445 call assert_equal(1, g:did_nested)
2446 close
2447 bwipe! somefile
2448
2449 " New ++nested argument works
2450 augroup Testing
2451 au!
2452 au WinNew * ++nested edit somefile
2453 au BufNew * let g:did_nested = 1
2454 augroup END
2455 split
2456 call assert_equal(1, g:did_nested)
2457 close
2458 bwipe! somefile
2459
Bram Moolenaarf0775142022-03-04 20:10:38 +00002460 " nested without ++ does not work in Vim9 script
2461 call assert_fails('vim9cmd au WinNew * nested echo fails', 'E1078:')
2462
Bram Moolenaareb93f3f2019-04-04 15:04:56 +02002463 augroup Testing
2464 au!
2465 augroup END
2466
2467 call assert_fails('au WinNew * ++nested ++nested echo bad', 'E983:')
2468 call assert_fails('au WinNew * nested nested echo bad', 'E983:')
2469endfunc
2470
Bram Moolenaar5fa9f232022-07-23 09:06:48 +01002471func Test_autocmd_nested_cursor_invalid()
2472 set laststatus=0
2473 copen
2474 cclose
2475 call setline(1, ['foo', 'bar', 'baz'])
2476 3
2477 augroup nested_inv
2478 autocmd User foo ++nested copen
2479 autocmd BufAdd * let &laststatus = 2 - &laststatus
2480 augroup END
2481 doautocmd User foo
2482
2483 augroup nested_inv
2484 au!
2485 augroup END
2486 set laststatus&
Bram Moolenaarb03950f2022-07-26 13:47:13 +01002487 cclose
Bram Moolenaar5fa9f232022-07-23 09:06:48 +01002488 bwipe!
2489endfunc
2490
Bram Moolenaar3d6ee8b2022-07-27 15:23:35 +01002491func Test_autocmd_nested_keeps_cursor_pos()
2492 enew
2493 call setline(1, 'foo')
2494 autocmd User foo ++nested normal! $a
2495 autocmd InsertLeave * :
2496 doautocmd User foo
2497 call assert_equal([0, 1, 3, 0], getpos('.'))
2498
2499 bwipe!
2500endfunc
2501
Bram Moolenaarb03950f2022-07-26 13:47:13 +01002502func Test_autocmd_nested_switch_window()
2503 " run this in a separate Vim so that SafeState works
2504 CheckRunVimInTerminal
2505
2506 let lines =<< trim END
2507 vim9script
2508 ['()']->writefile('Xautofile')
2509 autocmd VimEnter * ++nested edit Xautofile | split
2510 autocmd BufReadPost * autocmd SafeState * ++once foldclosed('.')
2511 autocmd WinEnter * matchadd('ErrorMsg', 'pat')
2512 END
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01002513 call writefile(lines, 'Xautoscript', 'D')
Bram Moolenaarb03950f2022-07-26 13:47:13 +01002514 let buf = RunVimInTerminal('-S Xautoscript', {'rows': 10})
2515 call VerifyScreenDump(buf, 'Test_autocmd_nested_switch', {})
2516
2517 call StopVimInTerminal(buf)
2518 call delete('Xautofile')
Bram Moolenaarb03950f2022-07-26 13:47:13 +01002519endfunc
2520
Bram Moolenaareb93f3f2019-04-04 15:04:56 +02002521func Test_autocmd_once()
2522 " Without ++once WinNew triggers twice
2523 let g:did_split = 0
2524 augroup Testing
2525 au WinNew * let g:did_split += 1
2526 augroup END
2527 split
2528 split
2529 call assert_equal(2, g:did_split)
2530 call assert_true(exists('#WinNew'))
2531 close
2532 close
2533
2534 " With ++once WinNew triggers once
2535 let g:did_split = 0
2536 augroup Testing
2537 au!
2538 au WinNew * ++once let g:did_split += 1
2539 augroup END
2540 split
2541 split
2542 call assert_equal(1, g:did_split)
2543 call assert_false(exists('#WinNew'))
2544 close
2545 close
2546
2547 call assert_fails('au WinNew * ++once ++once echo bad', 'E983:')
2548endfunc
2549
Bram Moolenaara68e5952019-04-25 22:22:01 +02002550func Test_autocmd_bufreadpre()
2551 new
2552 let b:bufreadpre = 1
Bram Moolenaarab505b12020-03-23 19:28:44 +01002553 call append(0, range(1000))
Bram Moolenaara68e5952019-04-25 22:22:01 +02002554 w! XAutocmdBufReadPre.txt
2555 autocmd BufReadPre <buffer> :let b:bufreadpre += 1
Bram Moolenaarab505b12020-03-23 19:28:44 +01002556 norm! 500gg
Bram Moolenaara68e5952019-04-25 22:22:01 +02002557 sp
Bram Moolenaarab505b12020-03-23 19:28:44 +01002558 norm! 1000gg
Bram Moolenaara68e5952019-04-25 22:22:01 +02002559 wincmd p
2560 let g:wsv1 = winsaveview()
2561 wincmd p
2562 let g:wsv2 = winsaveview()
2563 " triggers BufReadPre, should not move the cursor in either window
2564 " The topline may change one line in a large window.
2565 edit
2566 call assert_inrange(g:wsv2.topline - 1, g:wsv2.topline + 1, winsaveview().topline)
2567 call assert_equal(g:wsv2.lnum, winsaveview().lnum)
2568 call assert_equal(2, b:bufreadpre)
2569 wincmd p
2570 call assert_equal(g:wsv1.topline, winsaveview().topline)
2571 call assert_equal(g:wsv1.lnum, winsaveview().lnum)
2572 call assert_equal(2, b:bufreadpre)
2573 " Now set the cursor position in an BufReadPre autocommand
2574 " (even though the position will be invalid, this should make Vim reset the
2575 " cursor position in the other window.
2576 wincmd p
2577 set cpo+=g
2578 " won't do anything, but try to set the cursor on an invalid lnum
2579 autocmd BufReadPre <buffer> :norm! 70gg
2580 " triggers BufReadPre, should not move the cursor in either window
2581 e
2582 call assert_equal(1, winsaveview().topline)
2583 call assert_equal(1, winsaveview().lnum)
2584 call assert_equal(3, b:bufreadpre)
2585 wincmd p
2586 call assert_equal(g:wsv1.topline, winsaveview().topline)
2587 call assert_equal(g:wsv1.lnum, winsaveview().lnum)
2588 call assert_equal(3, b:bufreadpre)
2589 close
2590 close
2591 call delete('XAutocmdBufReadPre.txt')
2592 set cpo-=g
2593endfunc
2594
Bram Moolenaar5e66b422019-01-24 21:58:10 +01002595" FileChangedShell tested in test_filechanged.vim
Bram Moolenaar69ea5872019-04-25 20:29:00 +02002596
2597" Tests for the following autocommands:
2598" - FileWritePre writing a compressed file
2599" - FileReadPost reading a compressed file
2600" - BufNewFile reading a file template
2601" - BufReadPre decompressing the file to be read
2602" - FilterReadPre substituting characters in the temp file
2603" - FilterReadPost substituting characters after filtering
2604" - FileReadPre set options for decompression
2605" - FileReadPost decompress the file
2606func Test_ReadWrite_Autocmds()
2607 " Run this test only on Unix-like systems and if gzip is available
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +02002608 CheckUnix
2609 CheckExecutable gzip
Bram Moolenaar69ea5872019-04-25 20:29:00 +02002610
2611 " Make $GZIP empty, "-v" would cause trouble.
2612 let $GZIP = ""
2613
2614 " Use a FileChangedShell autocommand to avoid a prompt for 'Xtestfile.gz'
2615 " being modified outside of Vim (noticed on Solaris).
2616 au FileChangedShell * echo 'caught FileChangedShell'
2617
2618 " Test for the FileReadPost, FileWritePre and FileWritePost autocmds
2619 augroup Test1
2620 au!
2621 au FileWritePre *.gz '[,']!gzip
2622 au FileWritePost *.gz undo
2623 au FileReadPost *.gz '[,']!gzip -d
2624 augroup END
2625
2626 new
2627 set bin
2628 call append(0, [
2629 \ 'line 2 Abcdefghijklmnopqrstuvwxyz',
2630 \ 'line 3 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2631 \ 'line 4 Abcdefghijklmnopqrstuvwxyz',
2632 \ 'line 5 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2633 \ 'line 6 Abcdefghijklmnopqrstuvwxyz',
2634 \ 'line 7 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2635 \ 'line 8 Abcdefghijklmnopqrstuvwxyz',
2636 \ 'line 9 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2637 \ 'line 10 Abcdefghijklmnopqrstuvwxyz'
2638 \ ])
2639 1,9write! Xtestfile.gz
2640 enew! | close
2641
2642 new
2643 " Read and decompress the testfile
2644 0read Xtestfile.gz
2645 call assert_equal([
2646 \ 'line 2 Abcdefghijklmnopqrstuvwxyz',
2647 \ 'line 3 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2648 \ 'line 4 Abcdefghijklmnopqrstuvwxyz',
2649 \ 'line 5 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2650 \ 'line 6 Abcdefghijklmnopqrstuvwxyz',
2651 \ 'line 7 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2652 \ 'line 8 Abcdefghijklmnopqrstuvwxyz',
2653 \ 'line 9 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2654 \ 'line 10 Abcdefghijklmnopqrstuvwxyz'
2655 \ ], getline(1, 9))
2656 enew! | close
2657
2658 augroup Test1
2659 au!
2660 augroup END
2661
2662 " Test for the FileAppendPre and FileAppendPost autocmds
2663 augroup Test2
2664 au!
2665 au BufNewFile *.c read Xtest.c
2666 au FileAppendPre *.out '[,']s/new/NEW/
2667 au FileAppendPost *.out !cat Xtest.c >> test.out
2668 augroup END
2669
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01002670 call writefile(['/*', ' * Here is a new .c file', ' */'], 'Xtest.c', 'D')
Bram Moolenaar69ea5872019-04-25 20:29:00 +02002671 new foo.c " should load Xtest.c
2672 call assert_equal(['/*', ' * Here is a new .c file', ' */'], getline(2, 4))
2673 w! >> test.out " append it to the output file
2674
2675 let contents = readfile('test.out')
2676 call assert_equal(' * Here is a NEW .c file', contents[2])
2677 call assert_equal(' * Here is a new .c file', contents[5])
2678
2679 call delete('test.out')
2680 enew! | close
2681 augroup Test2
2682 au!
2683 augroup END
2684
2685 " Test for the BufReadPre and BufReadPost autocmds
2686 augroup Test3
2687 au!
2688 " setup autocommands to decompress before reading and re-compress
2689 " afterwards
2690 au BufReadPre *.gz exe '!gzip -d ' . shellescape(expand("<afile>"))
2691 au BufReadPre *.gz call rename(expand("<afile>:r"), expand("<afile>"))
2692 au BufReadPost *.gz call rename(expand("<afile>"), expand("<afile>:r"))
2693 au BufReadPost *.gz exe '!gzip ' . shellescape(expand("<afile>:r"))
2694 augroup END
2695
2696 e! Xtestfile.gz " Edit compressed file
2697 call assert_equal([
2698 \ 'line 2 Abcdefghijklmnopqrstuvwxyz',
2699 \ 'line 3 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2700 \ 'line 4 Abcdefghijklmnopqrstuvwxyz',
2701 \ 'line 5 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2702 \ 'line 6 Abcdefghijklmnopqrstuvwxyz',
2703 \ 'line 7 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2704 \ 'line 8 Abcdefghijklmnopqrstuvwxyz',
2705 \ 'line 9 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2706 \ 'line 10 Abcdefghijklmnopqrstuvwxyz'
2707 \ ], getline(1, 9))
2708
2709 w! >> test.out " Append it to the output file
2710
2711 augroup Test3
2712 au!
2713 augroup END
2714
2715 " Test for the FilterReadPre and FilterReadPost autocmds.
2716 set shelltemp " need temp files here
2717 augroup Test4
2718 au!
2719 au FilterReadPre *.out call rename(expand("<afile>"), expand("<afile>") . ".t")
2720 au FilterReadPre *.out exe 'silent !sed s/e/E/ ' . shellescape(expand("<afile>")) . ".t >" . shellescape(expand("<afile>"))
2721 au FilterReadPre *.out exe 'silent !rm ' . shellescape(expand("<afile>")) . '.t'
2722 au FilterReadPost *.out '[,']s/x/X/g
2723 augroup END
2724
2725 e! test.out " Edit the output file
2726 1,$!cat
2727 call assert_equal([
2728 \ 'linE 2 AbcdefghijklmnopqrstuvwXyz',
2729 \ 'linE 3 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
2730 \ 'linE 4 AbcdefghijklmnopqrstuvwXyz',
2731 \ 'linE 5 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
2732 \ 'linE 6 AbcdefghijklmnopqrstuvwXyz',
2733 \ 'linE 7 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
2734 \ 'linE 8 AbcdefghijklmnopqrstuvwXyz',
2735 \ 'linE 9 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
2736 \ 'linE 10 AbcdefghijklmnopqrstuvwXyz'
2737 \ ], getline(1, 9))
2738 call assert_equal([
2739 \ 'line 2 Abcdefghijklmnopqrstuvwxyz',
2740 \ 'line 3 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2741 \ 'line 4 Abcdefghijklmnopqrstuvwxyz',
2742 \ 'line 5 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2743 \ 'line 6 Abcdefghijklmnopqrstuvwxyz',
2744 \ 'line 7 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2745 \ 'line 8 Abcdefghijklmnopqrstuvwxyz',
2746 \ 'line 9 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2747 \ 'line 10 Abcdefghijklmnopqrstuvwxyz'
2748 \ ], readfile('test.out'))
2749
2750 augroup Test4
2751 au!
2752 augroup END
2753 set shelltemp&vim
2754
2755 " Test for the FileReadPre and FileReadPost autocmds.
2756 augroup Test5
2757 au!
2758 au FileReadPre *.gz exe 'silent !gzip -d ' . shellescape(expand("<afile>"))
2759 au FileReadPre *.gz call rename(expand("<afile>:r"), expand("<afile>"))
2760 au FileReadPost *.gz '[,']s/l/L/
2761 augroup END
2762
2763 new
2764 0r Xtestfile.gz " Read compressed file
2765 call assert_equal([
2766 \ 'Line 2 Abcdefghijklmnopqrstuvwxyz',
2767 \ 'Line 3 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2768 \ 'Line 4 Abcdefghijklmnopqrstuvwxyz',
2769 \ 'Line 5 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2770 \ 'Line 6 Abcdefghijklmnopqrstuvwxyz',
2771 \ 'Line 7 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2772 \ 'Line 8 Abcdefghijklmnopqrstuvwxyz',
2773 \ 'Line 9 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2774 \ 'Line 10 Abcdefghijklmnopqrstuvwxyz'
2775 \ ], getline(1, 9))
2776 call assert_equal([
2777 \ 'line 2 Abcdefghijklmnopqrstuvwxyz',
2778 \ 'line 3 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2779 \ 'line 4 Abcdefghijklmnopqrstuvwxyz',
2780 \ 'line 5 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2781 \ 'line 6 Abcdefghijklmnopqrstuvwxyz',
2782 \ 'line 7 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2783 \ 'line 8 Abcdefghijklmnopqrstuvwxyz',
2784 \ 'line 9 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2785 \ 'line 10 Abcdefghijklmnopqrstuvwxyz'
2786 \ ], readfile('Xtestfile.gz'))
2787
2788 augroup Test5
2789 au!
2790 augroup END
2791
2792 au! FileChangedShell
2793 call delete('Xtestfile.gz')
Bram Moolenaar69ea5872019-04-25 20:29:00 +02002794 call delete('test.out')
2795endfunc
Bram Moolenaar23b51392019-05-09 21:38:43 +02002796
2797func Test_throw_in_BufWritePre()
2798 new
2799 call setline(1, ['one', 'two', 'three'])
2800 call assert_false(filereadable('Xthefile'))
2801 augroup throwing
2802 au BufWritePre X* throw 'do not write'
2803 augroup END
2804 try
2805 w Xthefile
2806 catch
2807 let caught = 1
2808 endtry
2809 call assert_equal(1, caught)
2810 call assert_false(filereadable('Xthefile'))
2811
2812 bwipe!
2813 au! throwing
2814endfunc
Bram Moolenaarcadbe1b2019-09-22 21:50:09 +02002815
Bram Moolenaar40fa12a2021-09-22 14:18:13 +02002816func Test_autocmd_in_try_block()
Bram Moolenaar6f14da12022-09-07 21:30:44 +01002817 call mkdir('Xintrydir', 'R')
Bram Moolenaar40fa12a2021-09-22 14:18:13 +02002818 au BufEnter * let g:fname = expand('%')
2819 try
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +01002820 edit Xintrydir/
Bram Moolenaar40fa12a2021-09-22 14:18:13 +02002821 endtry
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +01002822 call assert_match('Xintrydir', g:fname)
Bram Moolenaar40fa12a2021-09-22 14:18:13 +02002823
2824 unlet g:fname
2825 au! BufEnter
Bram Moolenaar40fa12a2021-09-22 14:18:13 +02002826endfunc
2827
Bram Moolenaarcadbe1b2019-09-22 21:50:09 +02002828func Test_autocmd_SafeState()
2829 CheckRunVimInTerminal
2830
2831 let lines =<< trim END
2832 let g:safe = 0
2833 let g:again = ''
2834 au SafeState * let g:safe += 1
2835 au SafeStateAgain * let g:again ..= 'x'
2836 func CallTimer()
2837 call timer_start(10, {id -> execute('let g:again ..= "t"')})
2838 endfunc
2839 END
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01002840 call writefile(lines, 'XSafeState', 'D')
Bram Moolenaarcadbe1b2019-09-22 21:50:09 +02002841 let buf = RunVimInTerminal('-S XSafeState', #{rows: 6})
2842
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01002843 " Sometimes we loop to handle a K_IGNORE, SafeState may be triggered once or
Bram Moolenaar8fb1b472020-02-23 16:16:26 +01002844 " more often.
Bram Moolenaarcadbe1b2019-09-22 21:50:09 +02002845 call term_sendkeys(buf, ":echo g:safe\<CR>")
Bram Moolenaar8fb1b472020-02-23 16:16:26 +01002846 call WaitForAssert({-> assert_match('^\d ', term_getline(buf, 6))}, 1000)
Bram Moolenaarcadbe1b2019-09-22 21:50:09 +02002847
Bram Moolenaar8fb1b472020-02-23 16:16:26 +01002848 " SafeStateAgain should be invoked at least three times
Bram Moolenaarcadbe1b2019-09-22 21:50:09 +02002849 call term_sendkeys(buf, ":echo g:again\<CR>")
Bram Moolenaar8fb1b472020-02-23 16:16:26 +01002850 call WaitForAssert({-> assert_match('^xxx', term_getline(buf, 6))}, 1000)
Bram Moolenaarcadbe1b2019-09-22 21:50:09 +02002851
2852 call term_sendkeys(buf, ":let g:again = ''\<CR>:call CallTimer()\<CR>")
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02002853 call TermWait(buf, 50)
Bram Moolenaar0f6629a2019-09-22 23:24:13 +02002854 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02002855 call TermWait(buf, 50)
Bram Moolenaarcadbe1b2019-09-22 21:50:09 +02002856 call term_sendkeys(buf, ":echo g:again\<CR>")
2857 call WaitForAssert({-> assert_match('xtx', term_getline(buf, 6))}, 1000)
2858
2859 call StopVimInTerminal(buf)
Bram Moolenaarcadbe1b2019-09-22 21:50:09 +02002860endfunc
Bram Moolenaar23324a02019-10-01 17:39:04 +02002861
2862func Test_autocmd_CmdWinEnter()
2863 CheckRunVimInTerminal
Bram Moolenaar21829c52021-01-26 22:42:21 +01002864
Bram Moolenaar23324a02019-10-01 17:39:04 +02002865 let lines =<< trim END
Egor Zvorykin125ffd22021-11-17 14:01:14 +00002866 augroup vimHints | au! | augroup END
Bram Moolenaar23324a02019-10-01 17:39:04 +02002867 let b:dummy_var = 'This is a dummy'
2868 autocmd CmdWinEnter * quit
2869 let winnr = winnr('$')
2870 END
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01002871 let filename = 'XCmdWinEnter'
Bram Moolenaar23324a02019-10-01 17:39:04 +02002872 call writefile(lines, filename)
2873 let buf = RunVimInTerminal('-S '.filename, #{rows: 6})
2874
2875 call term_sendkeys(buf, "q:")
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02002876 call TermWait(buf)
Bram Moolenaar23324a02019-10-01 17:39:04 +02002877 call term_sendkeys(buf, ":echo b:dummy_var\<cr>")
Bram Moolenaar353c3512020-03-15 14:19:26 +01002878 call WaitForAssert({-> assert_match('^This is a dummy', term_getline(buf, 6))}, 2000)
Bram Moolenaar23324a02019-10-01 17:39:04 +02002879 call term_sendkeys(buf, ":echo &buftype\<cr>")
2880 call WaitForAssert({-> assert_notmatch('^nofile', term_getline(buf, 6))}, 1000)
2881 call term_sendkeys(buf, ":echo winnr\<cr>")
2882 call WaitForAssert({-> assert_match('^1', term_getline(buf, 6))}, 1000)
2883
2884 " clean up
2885 call StopVimInTerminal(buf)
2886 call delete(filename)
2887endfunc
Bram Moolenaarec66c412019-10-11 21:19:13 +02002888
2889func Test_autocmd_was_using_freed_memory()
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01002890 CheckFeature quickfix
2891
Bram Moolenaarec66c412019-10-11 21:19:13 +02002892 pedit xx
2893 n x
Bram Moolenaar92bb83e2021-02-03 23:04:46 +01002894 augroup winenter
2895 au WinEnter * if winnr('$') > 2 | quit | endif
2896 augroup END
Bram Moolenaarec66c412019-10-11 21:19:13 +02002897 split
Bram Moolenaar92bb83e2021-02-03 23:04:46 +01002898
2899 augroup winenter
2900 au! WinEnter
2901 augroup END
2902
2903 bwipe xx
2904 bwipe x
2905 pclose
Bram Moolenaarec66c412019-10-11 21:19:13 +02002906endfunc
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +01002907
2908func Test_BufWrite_lockmarks()
Bram Moolenaarf08b0eb2021-10-16 13:00:14 +01002909 let g:test_is_flaky = 1
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +01002910 edit! Xtest
2911 call setline(1, ['a', 'b', 'c', 'd'])
2912
2913 " :lockmarks preserves the marks
2914 call SetChangeMarks(2, 3)
2915 lockmarks write
2916 call assert_equal([2, 3], [line("'["), line("']")])
2917
2918 " *WritePre autocmds get the correct line range, but lockmarks preserves the
2919 " original values for the user
2920 augroup lockmarks
2921 au!
2922 au BufWritePre,FilterWritePre * call assert_equal([1, 4], [line("'["), line("']")])
2923 au FileWritePre * call assert_equal([3, 4], [line("'["), line("']")])
2924 augroup END
2925
2926 lockmarks write
2927 call assert_equal([2, 3], [line("'["), line("']")])
2928
2929 if executable('cat')
2930 lockmarks %!cat
2931 call assert_equal([2, 3], [line("'["), line("']")])
2932 endif
2933
2934 lockmarks 3,4write Xtest2
2935 call assert_equal([2, 3], [line("'["), line("']")])
2936
2937 au! lockmarks
2938 augroup! lockmarks
2939 call delete('Xtest')
2940 call delete('Xtest2')
2941endfunc
Bram Moolenaarce6db022020-01-07 20:11:42 +01002942
2943func Test_FileType_spell()
2944 if !isdirectory('/tmp')
2945 throw "Skipped: requires /tmp directory"
2946 endif
2947
2948 " this was crashing with an invalid free()
2949 setglobal spellfile=/tmp/en.utf-8.add
2950 augroup crash
2951 autocmd!
2952 autocmd BufNewFile,BufReadPost crashfile setf somefiletype
2953 autocmd BufNewFile,BufReadPost crashfile set ft=anotherfiletype
2954 autocmd FileType anotherfiletype setlocal spell
2955 augroup END
2956 func! NoCrash() abort
2957 edit /tmp/crashfile
2958 endfunc
2959 call NoCrash()
2960
2961 au! crash
2962 setglobal spellfile=
2963endfunc
Bram Moolenaarbc2b71d2020-02-17 21:33:30 +01002964
Bram Moolenaaref976322022-09-28 11:48:30 +01002965" this was wiping out the current buffer and using freed memory
2966func Test_SpellFileMissing_bwipe()
2967 next 0
2968 au SpellFileMissing 0 bwipe
2969 call assert_fails('set spell spelllang=0', 'E937:')
2970
2971 au! SpellFileMissing
Bram Moolenaar0a60f792022-11-19 21:18:11 +00002972 set nospell spelllang=en
Bram Moolenaaref976322022-09-28 11:48:30 +01002973 bwipe
2974endfunc
2975
Bram Moolenaar406cd902020-02-18 21:54:41 +01002976" Test closing a window or editing another buffer from a FileChangedRO handler
2977" in a readonly buffer
2978func Test_FileChangedRO_winclose()
Bram Moolenaar62cd26a2020-10-11 20:08:44 +02002979 call test_override('ui_delay', 10)
2980
Bram Moolenaar406cd902020-02-18 21:54:41 +01002981 augroup FileChangedROTest
2982 au!
2983 autocmd FileChangedRO * quit
2984 augroup END
2985 new
2986 set readonly
2987 call assert_fails('normal i', 'E788:')
2988 close
2989 augroup! FileChangedROTest
2990
2991 augroup FileChangedROTest
2992 au!
Bram Moolenaar61abe7d2022-08-30 21:46:08 +01002993 autocmd FileChangedRO * edit Xrofile
Bram Moolenaar406cd902020-02-18 21:54:41 +01002994 augroup END
2995 new
2996 set readonly
2997 call assert_fails('normal i', 'E788:')
2998 close
2999 augroup! FileChangedROTest
Bram Moolenaar62cd26a2020-10-11 20:08:44 +02003000 call test_override('ALL', 0)
Bram Moolenaar406cd902020-02-18 21:54:41 +01003001endfunc
3002
Bram Moolenaar0c81d1b2020-02-22 22:45:55 +01003003func LogACmd()
3004 call add(g:logged, line('$'))
3005endfunc
3006
3007func Test_TermChanged()
Bram Moolenaard28e0b32020-02-22 23:08:52 +01003008 CheckNotGui
3009
Bram Moolenaar0c81d1b2020-02-22 22:45:55 +01003010 enew!
3011 tabnew
3012 call setline(1, ['a', 'b', 'c', 'd'])
3013 $
3014 au TermChanged * call LogACmd()
3015 let g:logged = []
3016 let term_save = &term
3017 set term=xterm
3018 call assert_equal([1, 4], g:logged)
3019
3020 au! TermChanged
3021 let &term = term_save
3022 bwipe!
3023endfunc
3024
Bram Moolenaare3284872020-03-19 13:55:03 +01003025" Test for FileReadCmd autocmd
3026func Test_autocmd_FileReadCmd()
3027 func ReadFileCmd()
3028 call append(line('$'), "v:cmdarg = " .. v:cmdarg)
3029 endfunc
3030 augroup FileReadCmdTest
3031 au!
3032 au FileReadCmd Xtest call ReadFileCmd()
3033 augroup END
3034
3035 new
3036 read ++bin Xtest
3037 read ++nobin Xtest
3038 read ++edit Xtest
3039 read ++bad=keep Xtest
3040 read ++bad=drop Xtest
3041 read ++bad=- Xtest
3042 read ++ff=unix Xtest
3043 read ++ff=dos Xtest
3044 read ++ff=mac Xtest
3045 read ++enc=utf-8 Xtest
3046
3047 call assert_equal(['',
3048 \ 'v:cmdarg = ++bin',
3049 \ 'v:cmdarg = ++nobin',
3050 \ 'v:cmdarg = ++edit',
3051 \ 'v:cmdarg = ++bad=keep',
3052 \ 'v:cmdarg = ++bad=drop',
3053 \ 'v:cmdarg = ++bad=-',
3054 \ 'v:cmdarg = ++ff=unix',
3055 \ 'v:cmdarg = ++ff=dos',
3056 \ 'v:cmdarg = ++ff=mac',
3057 \ 'v:cmdarg = ++enc=utf-8'], getline(1, '$'))
3058
3059 close!
3060 augroup FileReadCmdTest
3061 au!
3062 augroup END
3063 delfunc ReadFileCmd
3064endfunc
3065
Bram Moolenaaree4e0c12020-04-06 21:35:05 +02003066" Test for passing invalid arguments to autocmd
3067func Test_autocmd_invalid_args()
3068 " Additional character after * for event
Bram Moolenaar61abe7d2022-08-30 21:46:08 +01003069 call assert_fails('autocmd *a Xinvfile set ff=unix', 'E215:')
Bram Moolenaaree4e0c12020-04-06 21:35:05 +02003070 augroup Test
3071 augroup END
3072 " Invalid autocmd event
Bram Moolenaar61abe7d2022-08-30 21:46:08 +01003073 call assert_fails('autocmd Bufabc Xinvfile set ft=vim', 'E216:')
Bram Moolenaaree4e0c12020-04-06 21:35:05 +02003074 " Invalid autocmd event in a autocmd group
Bram Moolenaar61abe7d2022-08-30 21:46:08 +01003075 call assert_fails('autocmd Test Bufabc Xinvfile set ft=vim', 'E216:')
Bram Moolenaaree4e0c12020-04-06 21:35:05 +02003076 augroup! Test
3077 " Execute all autocmds
3078 call assert_fails('doautocmd * BufEnter', 'E217:')
3079 call assert_fails('augroup! x1a2b3', 'E367:')
3080 call assert_fails('autocmd BufNew <buffer=999> pwd', 'E680:')
Bram Moolenaar531be472020-09-23 22:38:05 +02003081 call assert_fails('autocmd BufNew \) set ff=unix', 'E55:')
Bram Moolenaaree4e0c12020-04-06 21:35:05 +02003082endfunc
3083
3084" Test for deep nesting of autocmds
3085func Test_autocmd_deep_nesting()
Bram Moolenaar61abe7d2022-08-30 21:46:08 +01003086 autocmd BufEnter Xdeepfile doautocmd BufEnter Xdeepfile
3087 call assert_fails('doautocmd BufEnter Xdeepfile', 'E218:')
3088 autocmd! BufEnter Xdeepfile
Bram Moolenaaree4e0c12020-04-06 21:35:05 +02003089endfunc
3090
Bram Moolenaarbe5ee862020-06-10 20:56:58 +02003091" Tests for SigUSR1 autocmd event, which is only available on posix systems.
3092func Test_autocmd_sigusr1()
3093 CheckUnix
Bram Moolenaar0056ca72022-09-23 21:26:39 +01003094 " FIXME: should this work on MacOS M1?
3095 CheckNotMacM1
Bram Moolenaar62cd26a2020-10-11 20:08:44 +02003096 CheckExecutable /bin/kill
Bram Moolenaarbe5ee862020-06-10 20:56:58 +02003097
3098 let g:sigusr1_passed = 0
3099 au SigUSR1 * let g:sigusr1_passed = 1
3100 call system('/bin/kill -s usr1 ' . getpid())
3101 call WaitForAssert({-> assert_true(g:sigusr1_passed)})
3102
3103 au! SigUSR1
3104 unlet g:sigusr1_passed
3105endfunc
3106
Bram Moolenaarb340bae2020-06-15 19:51:56 +02003107" Test for BufReadPre autocmd deleting the file
3108func Test_BufReadPre_delfile()
3109 augroup TestAuCmd
3110 au!
Bram Moolenaare7cda972022-08-29 11:02:59 +01003111 autocmd BufReadPre XbufreadPre call delete('XbufreadPre')
Bram Moolenaarb340bae2020-06-15 19:51:56 +02003112 augroup END
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01003113 call writefile([], 'XbufreadPre', 'D')
Bram Moolenaare7cda972022-08-29 11:02:59 +01003114 call assert_fails('new XbufreadPre', 'E200:')
3115 call assert_equal('XbufreadPre', @%)
Bram Moolenaarb340bae2020-06-15 19:51:56 +02003116 call assert_equal(1, &readonly)
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01003117
Bram Moolenaarb340bae2020-06-15 19:51:56 +02003118 augroup TestAuCmd
3119 au!
3120 augroup END
3121 close!
3122endfunc
3123
3124" Test for BufReadPre autocmd changing the current buffer
3125func Test_BufReadPre_changebuf()
3126 augroup TestAuCmd
3127 au!
Bram Moolenaare7cda972022-08-29 11:02:59 +01003128 autocmd BufReadPre Xchangebuf edit Xsomeotherfile
Bram Moolenaarb340bae2020-06-15 19:51:56 +02003129 augroup END
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01003130 call writefile([], 'Xchangebuf', 'D')
Bram Moolenaare7cda972022-08-29 11:02:59 +01003131 call assert_fails('new Xchangebuf', 'E201:')
Bram Moolenaarb340bae2020-06-15 19:51:56 +02003132 call assert_equal('Xsomeotherfile', @%)
3133 call assert_equal(1, &readonly)
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01003134
Bram Moolenaarb340bae2020-06-15 19:51:56 +02003135 augroup TestAuCmd
3136 au!
3137 augroup END
3138 close!
3139endfunc
3140
3141" Test for BufWipeouti autocmd changing the current buffer when reading a file
3142" in an empty buffer with 'f' flag in 'cpo'
3143func Test_BufDelete_changebuf()
3144 new
3145 augroup TestAuCmd
3146 au!
3147 autocmd BufWipeout * let bufnr = bufadd('somefile') | exe "b " .. bufnr
3148 augroup END
3149 let save_cpo = &cpo
3150 set cpo+=f
Bram Moolenaarb18b4962022-09-02 21:55:50 +01003151 call assert_fails('r Xchangebuf', ['E812:', 'E484:'])
Bram Moolenaarb340bae2020-06-15 19:51:56 +02003152 call assert_equal('somefile', @%)
3153 let &cpo = save_cpo
3154 augroup TestAuCmd
3155 au!
3156 augroup END
3157 close!
3158endfunc
3159
Bram Moolenaar0fe937f2020-06-16 22:42:04 +02003160" Test for the temporary internal window used to execute autocmds
3161func Test_autocmd_window()
3162 %bw!
3163 edit one.txt
3164 tabnew two.txt
Bram Moolenaar41cd8032021-03-13 15:47:56 +01003165 vnew three.txt
3166 tabnew four.txt
3167 tabprevious
Bram Moolenaar0fe937f2020-06-16 22:42:04 +02003168 let g:blist = []
Bram Moolenaar832adf92020-06-25 19:01:36 +02003169 augroup aucmd_win_test1
Bram Moolenaar0fe937f2020-06-16 22:42:04 +02003170 au!
3171 au BufEnter * call add(g:blist, [expand('<afile>'),
3172 \ win_gettype(bufwinnr(expand('<afile>')))])
3173 augroup END
3174
3175 doautoall BufEnter
Bram Moolenaar41cd8032021-03-13 15:47:56 +01003176 call assert_equal([
3177 \ ['one.txt', 'autocmd'],
3178 \ ['two.txt', ''],
3179 \ ['four.txt', 'autocmd'],
3180 \ ['three.txt', ''],
3181 \ ], g:blist)
Bram Moolenaar0fe937f2020-06-16 22:42:04 +02003182
Bram Moolenaar832adf92020-06-25 19:01:36 +02003183 augroup aucmd_win_test1
Bram Moolenaar0fe937f2020-06-16 22:42:04 +02003184 au!
3185 augroup END
Bram Moolenaar832adf92020-06-25 19:01:36 +02003186 augroup! aucmd_win_test1
3187 %bw!
3188endfunc
3189
3190" Test for trying to close the temporary window used for executing an autocmd
3191func Test_close_autocmd_window()
3192 %bw!
3193 edit one.txt
3194 tabnew two.txt
3195 augroup aucmd_win_test2
3196 au!
3197 au BufEnter * if expand('<afile>') == 'one.txt' | 1close | endif
3198 augroup END
3199
3200 call assert_fails('doautoall BufEnter', 'E813:')
3201
3202 augroup aucmd_win_test2
3203 au!
3204 augroup END
3205 augroup! aucmd_win_test2
Bram Moolenaarcf844172020-06-26 19:44:06 +02003206 %bwipe!
3207endfunc
3208
3209" Test for trying to close the tab that has the temporary window for exeucing
3210" an autocmd.
3211func Test_close_autocmd_tab()
3212 edit one.txt
3213 tabnew two.txt
3214 augroup aucmd_win_test
3215 au!
3216 au BufEnter * if expand('<afile>') == 'one.txt' | tabfirst | tabonly | endif
3217 augroup END
3218
3219 call assert_fails('doautoall BufEnter', 'E813:')
3220
3221 tabonly
3222 augroup aucmd_win_test
3223 au!
3224 augroup END
3225 augroup! aucmd_win_test
3226 %bwipe!
Bram Moolenaar0fe937f2020-06-16 22:42:04 +02003227endfunc
3228
Bram Moolenaarcb1956d2022-01-07 15:45:18 +00003229func Test_Visual_doautoall_redraw()
3230 call setline(1, ['a', 'b'])
3231 new
3232 wincmd p
3233 call feedkeys("G\<C-V>", 'txn')
3234 autocmd User Explode ++once redraw
3235 doautoall User Explode
3236 %bwipe!
3237endfunc
3238
Bram Moolenaar6bcb8772021-02-03 21:23:29 +01003239" This was using freed memory.
3240func Test_BufNew_arglocal()
3241 arglocal
3242 au BufNew * arglocal
3243 call assert_fails('drop xx', 'E1156:')
3244
3245 au! BufNew
3246endfunc
3247
Bram Moolenaar8ab37572021-02-03 21:56:59 +01003248func Test_autocmd_closes_window()
3249 au BufNew,BufWinLeave * e %e
3250 file yyy
3251 au BufNew,BufWinLeave * ball
Bram Moolenaaraad5f9d2021-02-06 17:30:31 +01003252 n xxx
Bram Moolenaar8ab37572021-02-03 21:56:59 +01003253
Bram Moolenaaraad5f9d2021-02-06 17:30:31 +01003254 %bwipe
Bram Moolenaar8ab37572021-02-03 21:56:59 +01003255 au! BufNew
3256 au! BufWinLeave
3257endfunc
3258
Bram Moolenaar92bb83e2021-02-03 23:04:46 +01003259func Test_autocmd_quit_psearch()
3260 sn aa bb
3261 augroup aucmd_win_test
3262 au!
3263 au BufEnter,BufLeave,BufNew,WinEnter,WinLeave,WinNew * if winnr('$') > 1 | q | endif
3264 augroup END
3265 ps /
3266
3267 augroup aucmd_win_test
3268 au!
3269 augroup END
zeertzjq7851c692022-04-21 11:14:01 +01003270 new
3271 pclose
Bram Moolenaar92bb83e2021-02-03 23:04:46 +01003272endfunc
3273
Bram Moolenaaraad5f9d2021-02-06 17:30:31 +01003274" Fuzzer found some strange combination that caused a crash.
3275func Test_autocmd_normal_mess()
Bram Moolenaardd07c022021-02-07 13:32:46 +01003276 " For unknown reason this hangs on MS-Windows
3277 CheckNotMSWindows
3278
Bram Moolenaaraad5f9d2021-02-06 17:30:31 +01003279 augroup aucmd_normal_test
3280 au BufLeave,BufWinLeave,BufHidden,BufUnload,BufDelete,BufWipeout * norm 7q/qc
3281 augroup END
Bram Moolenaar983d83f2021-02-07 12:12:43 +01003282 call assert_fails('o4', 'E1159')
Bram Moolenaaraad5f9d2021-02-06 17:30:31 +01003283 silent! H
Bram Moolenaar983d83f2021-02-07 12:12:43 +01003284 call assert_fails('e xx', 'E1159')
Bram Moolenaaraad5f9d2021-02-06 17:30:31 +01003285 normal G
3286
3287 augroup aucmd_normal_test
3288 au!
3289 augroup END
3290endfunc
3291
Bram Moolenaar8c6951f2021-02-06 18:08:45 +01003292func Test_autocmd_closing_cmdwin()
Bram Moolenaardd07c022021-02-07 13:32:46 +01003293 " For unknown reason this hangs on MS-Windows
3294 CheckNotMSWindows
3295
Bram Moolenaar8c6951f2021-02-06 18:08:45 +01003296 au BufWinLeave * nested q
3297 call assert_fails("norm 7q?\n", 'E855:')
3298
3299 au! BufWinLeave
3300 new
3301 only
3302endfunc
3303
Bram Moolenaar2c7080b2021-02-06 19:19:42 +01003304func Test_autocmd_vimgrep()
3305 augroup aucmd_vimgrep
Charlie Grovesfef44852022-04-19 16:24:12 +01003306 au QuickfixCmdPre,BufNew,BufReadCmd * sb
zeertzjq7851c692022-04-21 11:14:01 +01003307 au QuickfixCmdPre,BufNew,BufReadCmd * q9
Bram Moolenaar2c7080b2021-02-06 19:19:42 +01003308 augroup END
Bram Moolenaardd07c022021-02-07 13:32:46 +01003309 call assert_fails('lv ?a? foo', 'E926:')
Bram Moolenaar2c7080b2021-02-06 19:19:42 +01003310
3311 augroup aucmd_vimgrep
3312 au!
3313 augroup END
3314endfunc
3315
Bram Moolenaar73b8b0a2021-08-01 14:52:32 +02003316func Test_autocmd_with_block()
3317 augroup block_testing
3318 au BufReadPost *.xml {
3319 setlocal matchpairs+=<:>
3320 /<start
3321 }
Bram Moolenaar63b91732021-08-05 20:40:03 +02003322 au CursorHold * {
3323 autocmd BufReadPre * ++once echo 'one' | echo 'two'
3324 g:gotSafeState = 77
3325 }
Bram Moolenaar73b8b0a2021-08-01 14:52:32 +02003326 augroup END
3327
3328 let expected = "\n--- Autocommands ---\nblock_testing BufRead\n *.xml {^@ setlocal matchpairs+=<:>^@ /<start^@ }"
3329 call assert_equal(expected, execute('au BufReadPost *.xml'))
3330
Bram Moolenaar63b91732021-08-05 20:40:03 +02003331 doautocmd CursorHold
3332 call assert_equal(77, g:gotSafeState)
3333 unlet g:gotSafeState
3334
Bram Moolenaar73b8b0a2021-08-01 14:52:32 +02003335 augroup block_testing
3336 au!
Bram Moolenaar75ebd2a2022-06-03 17:39:46 +01003337 autocmd CursorHold * {
3338 if true
3339 # comment
3340 && true
3341
3342 && true
3343 g:done = 'yes'
3344 endif
3345 }
3346 augroup END
3347 doautocmd CursorHold
3348 call assert_equal('yes', g:done)
3349
3350 unlet g:done
3351 augroup block_testing
3352 au!
Bram Moolenaar73b8b0a2021-08-01 14:52:32 +02003353 augroup END
3354endfunc
3355
Christian Brabandtdb3b4462021-10-16 11:58:55 +01003356" Test TextChangedI and TextChanged
3357func Test_Changed_ChangedI()
3358 new
3359 call test_override("char_avail", 1)
3360 let [g:autocmd_i, g:autocmd_n] = ['','']
3361
3362 func! TextChangedAutocmdI(char)
3363 let g:autocmd_{tolower(a:char)} = a:char .. b:changedtick
3364 endfunc
3365
3366 augroup Test_TextChanged
3367 au!
3368 au TextChanged <buffer> :call TextChangedAutocmdI('N')
3369 au TextChangedI <buffer> :call TextChangedAutocmdI('I')
3370 augroup END
3371
3372 call feedkeys("ifoo\<esc>", 'tnix')
3373 " TODO: Test test does not seem to trigger TextChanged autocommand, this
3374 " requires running Vim in a terminal window.
3375 " call assert_equal('N3', g:autocmd_n)
3376 call assert_equal('I3', g:autocmd_i)
3377
3378 call feedkeys("yyp", 'tnix')
3379 " TODO: Test test does not seem to trigger TextChanged autocommand.
3380 " call assert_equal('N4', g:autocmd_n)
3381 call assert_equal('I3', g:autocmd_i)
3382
3383 " CleanUp
3384 call test_override("char_avail", 0)
3385 au! TextChanged <buffer>
3386 au! TextChangedI <buffer>
3387 augroup! Test_TextChanged
3388 delfu TextChangedAutocmdI
3389 unlet! g:autocmd_i g:autocmd_n
3390
3391 bw!
3392endfunc
Bram Moolenaar2c7080b2021-02-06 19:19:42 +01003393
Bram Moolenaar6f2465d2022-03-22 18:13:01 +00003394func Test_closing_autocmd_window()
3395 let lines =<< trim END
3396 edit Xa.txt
3397 tabnew Xb.txt
3398 autocmd BufEnter Xa.txt unhide 1
3399 doautoall BufEnter
3400 END
3401 call v9.CheckScriptFailure(lines, 'E814:')
3402 au! BufEnter
3403 only!
3404 bwipe Xa.txt
3405 bwipe Xb.txt
3406endfunc
3407
Bram Moolenaar347538f2022-03-26 16:28:06 +00003408func Test_bufwipeout_changes_window()
3409 " This should not crash, but we don't have any expectations about what
3410 " happens, changing window in BufWipeout has unpredictable results.
3411 tabedit
3412 let g:window_id = win_getid()
3413 topleft new
3414 setlocal bufhidden=wipe
3415 autocmd BufWipeout <buffer> call win_gotoid(g:window_id)
3416 tabprevious
3417 +tabclose
3418
3419 unlet g:window_id
3420 au! BufWipeout
3421 %bwipe!
3422endfunc
3423
zeertzjq021996f2022-04-10 11:44:04 +01003424func Test_v_event_readonly()
3425 autocmd CompleteChanged * let v:event.width = 0
3426 call assert_fails("normal! i\<C-X>\<C-V>", 'E46:')
3427 au! CompleteChanged
3428
3429 autocmd DirChangedPre * let v:event.directory = ''
3430 call assert_fails('cd .', 'E46:')
3431 au! DirChangedPre
3432
3433 autocmd ModeChanged * let v:event.new_mode = ''
3434 call assert_fails('normal! cc', 'E46:')
3435 au! ModeChanged
3436
3437 autocmd TextYankPost * let v:event.operator = ''
3438 call assert_fails('normal! yy', 'E46:')
3439 au! TextYankPost
3440endfunc
3441
zeertzjqc9e8fd62022-07-26 18:12:38 +01003442" Test for ModeChanged pattern
3443func Test_mode_changes()
3444 let g:index = 0
3445 let g:mode_seq = ['n', 'i', 'n', 'v', 'V', 'i', 'ix', 'i', 'ic', 'i', 'n', 'no', 'n', 'V', 'v', 's', 'n']
3446 func! TestMode()
3447 call assert_equal(g:mode_seq[g:index], get(v:event, "old_mode"))
3448 call assert_equal(g:mode_seq[g:index + 1], get(v:event, "new_mode"))
3449 call assert_equal(mode(1), get(v:event, "new_mode"))
3450 let g:index += 1
3451 endfunc
3452
3453 au ModeChanged * :call TestMode()
3454 let g:n_to_any = 0
3455 au ModeChanged n:* let g:n_to_any += 1
3456 call feedkeys("i\<esc>vVca\<CR>\<C-X>\<C-L>\<esc>ggdG", 'tnix')
3457
3458 let g:V_to_v = 0
3459 au ModeChanged V:v let g:V_to_v += 1
3460 call feedkeys("Vv\<C-G>\<esc>", 'tnix')
3461 call assert_equal(len(filter(g:mode_seq[1:], {idx, val -> val == 'n'})), g:n_to_any)
3462 call assert_equal(1, g:V_to_v)
3463 call assert_equal(len(g:mode_seq) - 1, g:index)
3464
3465 let g:n_to_i = 0
3466 au ModeChanged n:i let g:n_to_i += 1
3467 let g:n_to_niI = 0
3468 au ModeChanged i:niI let g:n_to_niI += 1
3469 let g:niI_to_i = 0
3470 au ModeChanged niI:i let g:niI_to_i += 1
3471 let g:nany_to_i = 0
3472 au ModeChanged n*:i let g:nany_to_i += 1
3473 let g:i_to_n = 0
3474 au ModeChanged i:n let g:i_to_n += 1
3475 let g:nori_to_any = 0
3476 au ModeChanged [ni]:* let g:nori_to_any += 1
3477 let g:i_to_any = 0
3478 au ModeChanged i:* let g:i_to_any += 1
3479 let g:index = 0
3480 let g:mode_seq = ['n', 'i', 'niI', 'i', 'n']
3481 call feedkeys("a\<C-O>l\<esc>", 'tnix')
3482 call assert_equal(len(g:mode_seq) - 1, g:index)
3483 call assert_equal(1, g:n_to_i)
3484 call assert_equal(1, g:n_to_niI)
3485 call assert_equal(1, g:niI_to_i)
3486 call assert_equal(2, g:nany_to_i)
3487 call assert_equal(1, g:i_to_n)
3488 call assert_equal(2, g:i_to_any)
3489 call assert_equal(3, g:nori_to_any)
3490
3491 if has('terminal')
3492 let g:mode_seq += ['c', 'n', 't', 'nt', 'c', 'nt', 'n']
3493 call feedkeys(":term\<CR>\<C-W>N:bd!\<CR>", 'tnix')
3494 call assert_equal(len(g:mode_seq) - 1, g:index)
3495 call assert_equal(1, g:n_to_i)
3496 call assert_equal(1, g:n_to_niI)
3497 call assert_equal(1, g:niI_to_i)
3498 call assert_equal(2, g:nany_to_i)
3499 call assert_equal(1, g:i_to_n)
3500 call assert_equal(2, g:i_to_any)
3501 call assert_equal(5, g:nori_to_any)
3502 endif
3503
zeertzjqd1955982022-10-05 11:24:46 +01003504 let g:n_to_c = 0
3505 au ModeChanged n:c let g:n_to_c += 1
3506 let g:c_to_n = 0
3507 au ModeChanged c:n let g:c_to_n += 1
3508 let g:mode_seq += ['c', 'n', 'c', 'n']
3509 call feedkeys("q:\<C-C>\<Esc>", 'tnix')
3510 call assert_equal(len(g:mode_seq) - 1, g:index)
3511 call assert_equal(2, g:n_to_c)
3512 call assert_equal(2, g:c_to_n)
3513 unlet g:n_to_c
3514 unlet g:c_to_n
zeertzjqc9e8fd62022-07-26 18:12:38 +01003515
Bram Moolenaar61c4b042022-10-18 15:10:11 +01003516 let g:n_to_v = 0
3517 au ModeChanged n:v let g:n_to_v += 1
3518 let g:v_to_n = 0
3519 au ModeChanged v:n let g:v_to_n += 1
3520 let g:mode_seq += ['v', 'n']
3521 call feedkeys("v\<C-C>", 'tnix')
3522 call assert_equal(len(g:mode_seq) - 1, g:index)
3523 call assert_equal(1, g:n_to_v)
3524 call assert_equal(1, g:v_to_n)
3525 unlet g:n_to_v
3526 unlet g:v_to_n
3527
zeertzjqc9e8fd62022-07-26 18:12:38 +01003528 au! ModeChanged
3529 delfunc TestMode
3530 unlet! g:mode_seq
3531 unlet! g:index
3532 unlet! g:n_to_any
3533 unlet! g:V_to_v
3534 unlet! g:n_to_i
3535 unlet! g:n_to_niI
3536 unlet! g:niI_to_i
3537 unlet! g:nany_to_i
3538 unlet! g:i_to_n
3539 unlet! g:nori_to_any
3540 unlet! g:i_to_any
3541endfunc
3542
3543func Test_recursive_ModeChanged()
3544 au! ModeChanged * norm 0u
3545 sil! norm 
3546 au! ModeChanged
3547endfunc
3548
3549func Test_ModeChanged_starts_visual()
3550 " This was triggering ModeChanged before setting VIsual, causing a crash.
3551 au! ModeChanged * norm 0u
3552 sil! norm 
3553
3554 au! ModeChanged
3555endfunc
Bram Moolenaar347538f2022-03-26 16:28:06 +00003556
Charlie Grovesfef44852022-04-19 16:24:12 +01003557func Test_noname_autocmd()
3558 augroup test_noname_autocmd_group
3559 autocmd!
3560 autocmd BufEnter * call add(s:li, ["BufEnter", expand("<afile>")])
3561 autocmd BufDelete * call add(s:li, ["BufDelete", expand("<afile>")])
3562 autocmd BufLeave * call add(s:li, ["BufLeave", expand("<afile>")])
3563 autocmd BufUnload * call add(s:li, ["BufUnload", expand("<afile>")])
3564 autocmd BufWipeout * call add(s:li, ["BufWipeout", expand("<afile>")])
3565 augroup END
3566
3567 let s:li = []
3568 edit foo
3569 call assert_equal([['BufUnload', ''], ['BufDelete', ''], ['BufWipeout', ''], ['BufEnter', 'foo']], s:li)
3570
3571 au! test_noname_autocmd_group
3572 augroup! test_noname_autocmd_group
3573endfunc
3574
Yegappan Lakshmanan1755a912022-05-19 10:31:47 +01003575" Test for the autocmd_get() function
3576func Test_autocmd_get()
3577 augroup TestAutoCmdFns
3578 au!
3579 autocmd BufAdd *.vim echo "bufadd-vim"
3580 autocmd BufAdd *.py echo "bufadd-py"
3581 autocmd BufHidden *.vim echo "bufhidden"
3582 augroup END
3583 augroup TestAutoCmdFns2
3584 autocmd BufAdd *.vim echo "bufadd-vim-2"
3585 autocmd BufRead *.a1b2c3 echo "bufadd-vim-2"
3586 augroup END
3587
3588 let l = autocmd_get()
3589 call assert_true(l->len() > 0)
3590
3591 " Test for getting all the autocmds in a group
3592 let expected = [
3593 \ #{cmd: 'echo "bufadd-vim"', group: 'TestAutoCmdFns',
3594 \ pattern: '*.vim', nested: v:false, once: v:false,
3595 \ event: 'BufAdd'},
3596 \ #{cmd: 'echo "bufadd-py"', group: 'TestAutoCmdFns',
3597 \ pattern: '*.py', nested: v:false, once: v:false,
3598 \ event: 'BufAdd'},
3599 \ #{cmd: 'echo "bufhidden"', group: 'TestAutoCmdFns',
3600 \ pattern: '*.vim', nested: v:false,
3601 \ once: v:false, event: 'BufHidden'}]
3602 call assert_equal(expected, autocmd_get(#{group: 'TestAutoCmdFns'}))
3603
3604 " Test for getting autocmds for all the patterns in a group
3605 call assert_equal(expected, autocmd_get(#{group: 'TestAutoCmdFns',
3606 \ event: '*'}))
3607
3608 " Test for getting autocmds for an event in a group
3609 let expected = [
3610 \ #{cmd: 'echo "bufadd-vim"', group: 'TestAutoCmdFns',
3611 \ pattern: '*.vim', nested: v:false, once: v:false,
3612 \ event: 'BufAdd'},
3613 \ #{cmd: 'echo "bufadd-py"', group: 'TestAutoCmdFns',
3614 \ pattern: '*.py', nested: v:false, once: v:false,
3615 \ event: 'BufAdd'}]
3616 call assert_equal(expected, autocmd_get(#{group: 'TestAutoCmdFns',
3617 \ event: 'BufAdd'}))
3618
3619 " Test for getting the autocmds for all the events in a group for particular
3620 " pattern
3621 call assert_equal([{'cmd': 'echo "bufadd-py"', 'group': 'TestAutoCmdFns',
3622 \ 'pattern': '*.py', 'nested': v:false, 'once': v:false,
3623 \ 'event': 'BufAdd'}],
3624 \ autocmd_get(#{group: 'TestAutoCmdFns', event: '*', pattern: '*.py'}))
3625
3626 " Test for getting the autocmds for an events in a group for particular
3627 " pattern
3628 let l = autocmd_get(#{group: 'TestAutoCmdFns', event: 'BufAdd',
3629 \ pattern: '*.vim'})
3630 call assert_equal([
3631 \ #{cmd: 'echo "bufadd-vim"', group: 'TestAutoCmdFns',
3632 \ pattern: '*.vim', nested: v:false, once: v:false,
3633 \ event: 'BufAdd'}], l)
3634
3635 " Test for getting the autocmds for a pattern in a group
3636 let l = autocmd_get(#{group: 'TestAutoCmdFns', pattern: '*.vim'})
3637 call assert_equal([
3638 \ #{cmd: 'echo "bufadd-vim"', group: 'TestAutoCmdFns',
3639 \ pattern: '*.vim', nested: v:false, once: v:false,
3640 \ event: 'BufAdd'},
3641 \ #{cmd: 'echo "bufhidden"', group: 'TestAutoCmdFns',
3642 \ pattern: '*.vim', nested: v:false,
3643 \ once: v:false, event: 'BufHidden'}], l)
3644
3645 " Test for getting the autocmds for a pattern in all the groups
3646 let l = autocmd_get(#{pattern: '*.a1b2c3'})
3647 call assert_equal([{'cmd': 'echo "bufadd-vim-2"', 'group': 'TestAutoCmdFns2',
3648 \ 'pattern': '*.a1b2c3', 'nested': v:false, 'once': v:false,
3649 \ 'event': 'BufRead'}], l)
3650
3651 " Test for getting autocmds for a pattern without any autocmds
3652 call assert_equal([], autocmd_get(#{group: 'TestAutoCmdFns',
3653 \ pattern: '*.abc'}))
3654 call assert_equal([], autocmd_get(#{group: 'TestAutoCmdFns',
3655 \ event: 'BufAdd', pattern: '*.abc'}))
3656 call assert_equal([], autocmd_get(#{group: 'TestAutoCmdFns',
3657 \ event: 'BufWipeout'}))
3658 call assert_fails("call autocmd_get(#{group: 'abc', event: 'BufAdd'})",
3659 \ 'E367:')
3660 let cmd = "echo autocmd_get(#{group: 'TestAutoCmdFns', event: 'abc'})"
3661 call assert_fails(cmd, 'E216:')
3662 call assert_fails("call autocmd_get(#{group: 'abc'})", 'E367:')
3663 call assert_fails("echo autocmd_get(#{event: 'abc'})", 'E216:')
3664
3665 augroup TestAutoCmdFns
3666 au!
3667 augroup END
3668 call assert_equal([], autocmd_get(#{group: 'TestAutoCmdFns'}))
3669
3670 " Test for nested and once autocmds
3671 augroup TestAutoCmdFns
3672 au!
3673 autocmd VimSuspend * ++nested echo "suspend"
3674 autocmd VimResume * ++once echo "resume"
3675 augroup END
3676
3677 let expected = [
3678 \ {'cmd': 'echo "suspend"', 'group': 'TestAutoCmdFns', 'pattern': '*',
3679 \ 'nested': v:true, 'once': v:false, 'event': 'VimSuspend'},
3680 \ {'cmd': 'echo "resume"', 'group': 'TestAutoCmdFns', 'pattern': '*',
3681 \ 'nested': v:false, 'once': v:true, 'event': 'VimResume'}]
3682 call assert_equal(expected, autocmd_get(#{group: 'TestAutoCmdFns'}))
3683
3684 " Test for buffer-local autocmd
3685 augroup TestAutoCmdFns
3686 au!
3687 autocmd TextYankPost <buffer> echo "textyankpost"
3688 augroup END
3689
3690 let expected = [
3691 \ {'cmd': 'echo "textyankpost"', 'group': 'TestAutoCmdFns',
3692 \ 'pattern': '<buffer=' .. bufnr() .. '>', 'nested': v:false,
3693 \ 'once': v:false, 'bufnr': bufnr(), 'event': 'TextYankPost'}]
3694 call assert_equal(expected, autocmd_get(#{group: 'TestAutoCmdFns'}))
3695
3696 augroup TestAutoCmdFns
3697 au!
3698 augroup END
3699 augroup! TestAutoCmdFns
3700 augroup TestAutoCmdFns2
3701 au!
3702 augroup END
3703 augroup! TestAutoCmdFns2
3704
3705 call assert_fails("echo autocmd_get(#{group: []})", 'E730:')
3706 call assert_fails("echo autocmd_get(#{event: {}})", 'E731:')
3707 call assert_fails("echo autocmd_get([])", 'E1206:')
3708endfunc
3709
3710" Test for the autocmd_add() function
3711func Test_autocmd_add()
3712 " Define a single autocmd in a group
3713 call autocmd_add([#{group: 'TestAcSet', event: 'BufAdd', pattern: '*.sh',
3714 \ cmd: 'echo "bufadd"', once: v:true, nested: v:true}])
3715 call assert_equal([#{cmd: 'echo "bufadd"', group: 'TestAcSet',
3716 \ pattern: '*.sh', nested: v:true, once: v:true,
3717 \ event: 'BufAdd'}], autocmd_get(#{group: 'TestAcSet'}))
3718
3719 " Define two autocmds in the same group
3720 call autocmd_delete([#{group: 'TestAcSet'}])
3721 call autocmd_add([#{group: 'TestAcSet', event: 'BufAdd', pattern: '*.sh',
3722 \ cmd: 'echo "bufadd"'},
3723 \ #{group: 'TestAcSet', event: 'BufEnter', pattern: '*.sh',
3724 \ cmd: 'echo "bufenter"'}])
3725 call assert_equal([
3726 \ #{cmd: 'echo "bufadd"', group: 'TestAcSet', pattern: '*.sh',
3727 \ nested: v:false, once: v:false, event: 'BufAdd'},
3728 \ #{cmd: 'echo "bufenter"', group: 'TestAcSet', pattern: '*.sh',
3729 \ nested: v:false, once: v:false, event: 'BufEnter'}],
3730 \ autocmd_get(#{group: 'TestAcSet'}))
3731
3732 " Define a buffer-local autocmd
3733 call autocmd_delete([#{group: 'TestAcSet'}])
3734 call autocmd_add([#{group: 'TestAcSet', event: 'CursorHold',
3735 \ bufnr: bufnr(), cmd: 'echo "cursorhold"'}])
3736 call assert_equal([
3737 \ #{cmd: 'echo "cursorhold"', group: 'TestAcSet',
3738 \ pattern: '<buffer=' .. bufnr() .. '>', nested: v:false,
3739 \ once: v:false, bufnr: bufnr(), event: 'CursorHold'}],
3740 \ autocmd_get(#{group: 'TestAcSet'}))
3741
3742 " Use an invalid buffer number
3743 call autocmd_delete([#{group: 'TestAcSet'}])
3744 call autocmd_add([#{group: 'TestAcSet', event: 'BufEnter',
3745 \ bufnr: -1, cmd: 'echo "bufenter"'}])
3746 let l = [#{group: 'TestAcSet', event: 'BufAdd', bufnr: 9999,
3747 \ cmd: 'echo "bufadd"'}]
3748 call assert_fails("echo autocmd_add(l)", 'E680:')
Yegappan Lakshmanan00e977c2022-06-01 12:31:53 +01003749 let l = [#{group: 'TestAcSet', event: 'BufAdd', bufnr: 9999,
3750 \ pattern: '*.py', cmd: 'echo "bufadd"'}]
3751 call assert_fails("echo autocmd_add(l)", 'E680:')
3752 let l = [#{group: 'TestAcSet', event: 'BufAdd', bufnr: 9999,
3753 \ pattern: ['*.py', '*.c'], cmd: 'echo "bufadd"'}]
3754 call assert_fails("echo autocmd_add(l)", 'E680:')
Yegappan Lakshmanan1755a912022-05-19 10:31:47 +01003755 let l = [#{group: 'TestAcSet', event: 'BufRead', bufnr: [],
3756 \ cmd: 'echo "bufread"'}]
3757 call assert_fails("echo autocmd_add(l)", 'E745:')
3758 call assert_equal([], autocmd_get(#{group: 'TestAcSet'}))
3759
3760 " Add two commands to the same group, event and pattern
3761 call autocmd_delete([#{group: 'TestAcSet'}])
3762 call autocmd_add([#{group: 'TestAcSet', event: 'BufUnload',
3763 \ pattern: 'abc', cmd: 'echo "cmd1"'}])
3764 call autocmd_add([#{group: 'TestAcSet', event: 'BufUnload',
3765 \ pattern: 'abc', cmd: 'echo "cmd2"'}])
3766 call assert_equal([
3767 \ #{cmd: 'echo "cmd1"', group: 'TestAcSet', pattern: 'abc',
3768 \ nested: v:false, once: v:false, event: 'BufUnload'},
3769 \ #{cmd: 'echo "cmd2"', group: 'TestAcSet', pattern: 'abc',
3770 \ nested: v:false, once: v:false, event: 'BufUnload'}],
3771 \ autocmd_get(#{group: 'TestAcSet'}))
3772
3773 " When adding a new autocmd, if the autocmd 'group' is not specified, then
3774 " the current autocmd group should be used.
3775 call autocmd_delete([#{group: 'TestAcSet'}])
3776 augroup TestAcSet
3777 call autocmd_add([#{event: 'BufHidden', pattern: 'abc', cmd: 'echo "abc"'}])
3778 augroup END
3779 call assert_equal([
3780 \ #{cmd: 'echo "abc"', group: 'TestAcSet', pattern: 'abc',
3781 \ nested: v:false, once: v:false, event: 'BufHidden'}],
3782 \ autocmd_get(#{group: 'TestAcSet'}))
3783
Yegappan Lakshmanan971f6822022-05-24 11:40:11 +01003784 " Test for replacing a cmd for an event in a group
3785 call autocmd_delete([#{group: 'TestAcSet'}])
3786 call autocmd_add([#{replace: v:true, group: 'TestAcSet', event: 'BufEnter',
3787 \ pattern: '*.py', cmd: 'echo "bufenter"'}])
3788 call autocmd_add([#{replace: v:true, group: 'TestAcSet', event: 'BufEnter',
3789 \ pattern: '*.py', cmd: 'echo "bufenter"'}])
3790 call assert_equal([
3791 \ #{cmd: 'echo "bufenter"', group: 'TestAcSet', pattern: '*.py',
3792 \ nested: v:false, once: v:false, event: 'BufEnter'}],
3793 \ autocmd_get(#{group: 'TestAcSet'}))
3794
3795 " Test for adding a command for an unsupported autocmd event
Yegappan Lakshmanan1755a912022-05-19 10:31:47 +01003796 let l = [#{group: 'TestAcSet', event: 'abc', pattern: '*.sh',
3797 \ cmd: 'echo "bufadd"'}]
3798 call assert_fails('call autocmd_add(l)', 'E216:')
3799
Yegappan Lakshmanane0ff3a72022-05-27 18:05:33 +01003800 " Test for using a list of events and patterns
3801 call autocmd_delete([#{group: 'TestAcSet'}])
3802 let l = [#{group: 'TestAcSet', event: ['BufEnter', 'BufLeave'],
3803 \ pattern: ['*.py', '*.sh'], cmd: 'echo "bufcmds"'}]
3804 call autocmd_add(l)
3805 call assert_equal([
3806 \ #{cmd: 'echo "bufcmds"', group: 'TestAcSet', pattern: '*.py',
3807 \ nested: v:false, once: v:false, event: 'BufEnter'},
3808 \ #{cmd: 'echo "bufcmds"', group: 'TestAcSet', pattern: '*.sh',
3809 \ nested: v:false, once: v:false, event: 'BufEnter'},
3810 \ #{cmd: 'echo "bufcmds"', group: 'TestAcSet', pattern: '*.py',
3811 \ nested: v:false, once: v:false, event: 'BufLeave'},
3812 \ #{cmd: 'echo "bufcmds"', group: 'TestAcSet', pattern: '*.sh',
3813 \ nested: v:false, once: v:false, event: 'BufLeave'}],
3814 \ autocmd_get(#{group: 'TestAcSet'}))
3815
3816 " Test for invalid values for 'event' item
3817 call autocmd_delete([#{group: 'TestAcSet'}])
3818 let l = [#{group: 'TestAcSet', event: test_null_string(),
3819 \ pattern: "*.py", cmd: 'echo "bufcmds"'}]
3820 call assert_fails('call autocmd_add(l)', 'E928:')
3821 let l = [#{group: 'TestAcSet', event: test_null_list(),
3822 \ pattern: "*.py", cmd: 'echo "bufcmds"'}]
3823 call assert_fails('call autocmd_add(l)', 'E714:')
3824 let l = [#{group: 'TestAcSet', event: {},
3825 \ pattern: "*.py", cmd: 'echo "bufcmds"'}]
3826 call assert_fails('call autocmd_add(l)', 'E777:')
3827 let l = [#{group: 'TestAcSet', event: [{}],
3828 \ pattern: "*.py", cmd: 'echo "bufcmds"'}]
3829 call assert_fails('call autocmd_add(l)', 'E928:')
3830 let l = [#{group: 'TestAcSet', event: [test_null_string()],
3831 \ pattern: "*.py", cmd: 'echo "bufcmds"'}]
3832 call assert_fails('call autocmd_add(l)', 'E928:')
3833 let l = [#{group: 'TestAcSet', event: 'BufEnter,BufLeave',
3834 \ pattern: '*.py', cmd: 'echo "bufcmds"'}]
3835 call assert_fails('call autocmd_add(l)', 'E216:')
3836 let l = [#{group: 'TestAcSet', event: [],
3837 \ pattern: "*.py", cmd: 'echo "bufcmds"'}]
3838 call autocmd_add(l)
3839 let l = [#{group: 'TestAcSet', event: [""],
3840 \ pattern: "*.py", cmd: 'echo "bufcmds"'}]
3841 call assert_fails('call autocmd_add(l)', 'E216:')
3842 let l = [#{group: 'TestAcSet', event: "",
3843 \ pattern: "*.py", cmd: 'echo "bufcmds"'}]
3844 call autocmd_add(l)
3845 call assert_equal([], autocmd_get(#{group: 'TestAcSet'}))
3846
3847 " Test for invalid values for 'pattern' item
3848 let l = [#{group: 'TestAcSet', event: "BufEnter",
3849 \ pattern: test_null_string(), cmd: 'echo "bufcmds"'}]
Yegappan Lakshmanan00e977c2022-06-01 12:31:53 +01003850 call assert_fails('call autocmd_add(l)', 'E928:')
Yegappan Lakshmanane0ff3a72022-05-27 18:05:33 +01003851 let l = [#{group: 'TestAcSet', event: "BufEnter",
3852 \ pattern: test_null_list(), cmd: 'echo "bufcmds"'}]
3853 call assert_fails('call autocmd_add(l)', 'E714:')
3854 let l = [#{group: 'TestAcSet', event: "BufEnter",
3855 \ pattern: {}, cmd: 'echo "bufcmds"'}]
3856 call assert_fails('call autocmd_add(l)', 'E777:')
3857 let l = [#{group: 'TestAcSet', event: "BufEnter",
3858 \ pattern: [{}], cmd: 'echo "bufcmds"'}]
3859 call assert_fails('call autocmd_add(l)', 'E928:')
3860 let l = [#{group: 'TestAcSet', event: "BufEnter",
3861 \ pattern: [test_null_string()], cmd: 'echo "bufcmds"'}]
3862 call assert_fails('call autocmd_add(l)', 'E928:')
3863 let l = [#{group: 'TestAcSet', event: "BufEnter",
3864 \ pattern: [], cmd: 'echo "bufcmds"'}]
3865 call autocmd_add(l)
3866 let l = [#{group: 'TestAcSet', event: "BufEnter",
3867 \ pattern: [""], cmd: 'echo "bufcmds"'}]
3868 call autocmd_add(l)
3869 let l = [#{group: 'TestAcSet', event: "BufEnter",
3870 \ pattern: "", cmd: 'echo "bufcmds"'}]
3871 call autocmd_add(l)
3872 call assert_equal([], autocmd_get(#{group: 'TestAcSet'}))
3873
3874 let l = [#{group: 'TestAcSet', event: 'BufEnter,abc,BufLeave',
3875 \ pattern: '*.py', cmd: 'echo "bufcmds"'}]
3876 call assert_fails('call autocmd_add(l)', 'E216:')
3877
Yegappan Lakshmanan1755a912022-05-19 10:31:47 +01003878 call assert_fails("call autocmd_add({})", 'E1211:')
3879 call assert_equal(v:false, autocmd_add(test_null_list()))
3880 call assert_true(autocmd_add([[]]))
3881 call assert_true(autocmd_add([test_null_dict()]))
3882
3883 augroup TestAcSet
3884 au!
3885 augroup END
3886
3887 call autocmd_add([#{group: 'TestAcSet'}])
3888 call autocmd_add([#{group: 'TestAcSet', event: 'BufAdd'}])
3889 call autocmd_add([#{group: 'TestAcSet', pat: '*.sh'}])
3890 call autocmd_add([#{group: 'TestAcSet', cmd: 'echo "a"'}])
3891 call autocmd_add([#{group: 'TestAcSet', event: 'BufAdd', pat: '*.sh'}])
3892 call autocmd_add([#{group: 'TestAcSet', event: 'BufAdd', cmd: 'echo "a"'}])
3893 call autocmd_add([#{group: 'TestAcSet', pat: '*.sh', cmd: 'echo "a"'}])
3894 call assert_equal([], autocmd_get(#{group: 'TestAcSet'}))
3895
3896 augroup! TestAcSet
3897endfunc
3898
3899" Test for deleting autocmd events and groups
3900func Test_autocmd_delete()
3901 " Delete an event in an autocmd group
3902 augroup TestAcSet
3903 au!
3904 au BufAdd *.sh echo "bufadd"
3905 au BufEnter *.sh echo "bufenter"
3906 augroup END
3907 call autocmd_delete([#{group: 'TestAcSet', event: 'BufAdd'}])
3908 call assert_equal([#{cmd: 'echo "bufenter"', group: 'TestAcSet',
3909 \ pattern: '*.sh', nested: v:false, once: v:false,
3910 \ event: 'BufEnter'}], autocmd_get(#{group: 'TestAcSet'}))
3911
3912 " Delete all the events in an autocmd group
3913 augroup TestAcSet
3914 au BufAdd *.sh echo "bufadd"
3915 augroup END
3916 call autocmd_delete([#{group: 'TestAcSet', event: '*'}])
3917 call assert_equal([], autocmd_get(#{group: 'TestAcSet'}))
3918
3919 " Delete a non-existing autocmd group
3920 call assert_fails("call autocmd_delete([#{group: 'abc'}])", 'E367:')
3921 " Delete a non-existing autocmd event
3922 let l = [#{group: 'TestAcSet', event: 'abc'}]
3923 call assert_fails("call autocmd_delete(l)", 'E216:')
3924 " Delete a non-existing autocmd pattern
3925 let l = [#{group: 'TestAcSet', event: 'BufAdd', pat: 'abc'}]
3926 call assert_true(autocmd_delete(l))
Yegappan Lakshmanan00e977c2022-06-01 12:31:53 +01003927 " Delete an autocmd for a non-existing buffer
3928 let l = [#{event: '*', bufnr: 9999, cmd: 'echo "x"'}]
3929 call assert_fails('call autocmd_delete(l)', 'E680:')
Yegappan Lakshmanan1755a912022-05-19 10:31:47 +01003930
3931 " Delete an autocmd group
3932 augroup TestAcSet
3933 au!
3934 au BufAdd *.sh echo "bufadd"
3935 au BufEnter *.sh echo "bufenter"
3936 augroup END
3937 call autocmd_delete([#{group: 'TestAcSet'}])
3938 call assert_fails("call autocmd_get(#{group: 'TestAcSet'})", 'E367:')
3939
3940 call assert_true(autocmd_delete([[]]))
3941 call assert_true(autocmd_delete([test_null_dict()]))
3942endfunc
3943
Bram Moolenaar8f3c3c62022-10-18 17:05:54 +01003944func Test_autocmd_split_dummy()
3945 " Autocommand trying to split a window containing a dummy buffer.
3946 auto BufReadPre * exe "sbuf " .. expand("<abuf>")
3947 " Avoid the "W11" prompt
3948 au FileChangedShell * let v:fcs_choice = 'reload'
3949 func Xautocmd_changelist()
3950 cal writefile(['Xtestfile2:4:4'], 'Xerr')
3951 edit Xerr
3952 lex 'Xtestfile2:4:4'
3953 endfunc
3954 call Xautocmd_changelist()
Bram Moolenaar53c5c9f2022-10-18 17:25:03 +01003955 " Should get E86, but it doesn't always happen (timing?)
3956 silent! call Xautocmd_changelist()
Bram Moolenaar8f3c3c62022-10-18 17:05:54 +01003957
3958 au! BufReadPre
3959 au! FileChangedShell
3960 delfunc Xautocmd_changelist
3961 bwipe! Xerr
3962 call delete('Xerr')
3963endfunc
3964
Bram Moolenaarbc2b71d2020-02-17 21:33:30 +01003965" vim: shiftwidth=2 sts=2 expandtab