blob: 637fdb8c8e5d441720cdc8359c9e254cefe506c9 [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
410 call assert_equal(['123456'], readfile('Xtestout'))
411
zeertzjqd58862d2022-04-12 11:32:48 +0100412 call delete('Xtestout')
413endfunc
414
zeertzjq670ab032022-08-28 19:16:15 +0100415func Test_WinScrolled_long_wrapped()
416 CheckRunVimInTerminal
417
418 let lines =<< trim END
419 set scrolloff=0
420 let height = winheight(0)
421 let width = winwidth(0)
422 let g:scrolled = 0
423 au WinScrolled * let g:scrolled += 1
424 call setline(1, repeat('foo', height * width))
425 call cursor(1, height * width)
426 END
Bram Moolenaare1f3ab72022-09-04 21:29:08 +0100427 call writefile(lines, 'Xtest_winscrolled_long_wrapped', 'D')
zeertzjq670ab032022-08-28 19:16:15 +0100428 let buf = RunVimInTerminal('-S Xtest_winscrolled_long_wrapped', {'rows': 6})
429
430 call term_sendkeys(buf, ":echo g:scrolled\<CR>")
431 call WaitForAssert({-> assert_match('^0 ', term_getline(buf, 6))}, 1000)
432
433 call term_sendkeys(buf, 'gj')
434 call term_sendkeys(buf, ":echo g:scrolled\<CR>")
435 call WaitForAssert({-> assert_match('^1 ', term_getline(buf, 6))}, 1000)
436
437 call term_sendkeys(buf, '0')
438 call term_sendkeys(buf, ":echo g:scrolled\<CR>")
439 call WaitForAssert({-> assert_match('^2 ', term_getline(buf, 6))}, 1000)
440
441 call term_sendkeys(buf, '$')
442 call term_sendkeys(buf, ":echo g:scrolled\<CR>")
443 call WaitForAssert({-> assert_match('^3 ', term_getline(buf, 6))}, 1000)
zeertzjq670ab032022-08-28 19:16:15 +0100444endfunc
445
naohiro ono23beefe2021-11-13 12:38:49 +0000446func Test_WinClosed()
447 " Test that the pattern is matched against the closed window's ID, and both
448 " <amatch> and <afile> are set to it.
449 new
450 let winid = win_getid()
451 let g:matched = v:false
452 augroup test-WinClosed
453 autocmd!
454 execute 'autocmd WinClosed' winid 'let g:matched = v:true'
455 autocmd WinClosed * let g:amatch = str2nr(expand('<amatch>'))
456 autocmd WinClosed * let g:afile = str2nr(expand('<afile>'))
457 augroup END
458 close
459 call assert_true(g:matched)
460 call assert_equal(winid, g:amatch)
461 call assert_equal(winid, g:afile)
462
463 " Test that WinClosed is non-recursive.
464 new
465 new
466 call assert_equal(3, winnr('$'))
467 let g:triggered = 0
468 augroup test-WinClosed
469 autocmd!
470 autocmd WinClosed * let g:triggered += 1
471 autocmd WinClosed * 2 wincmd c
472 augroup END
473 close
474 call assert_equal(1, winnr('$'))
475 call assert_equal(1, g:triggered)
476
477 autocmd! test-WinClosed
478 augroup! test-WinClosed
479 unlet g:matched
480 unlet g:amatch
481 unlet g:afile
482 unlet g:triggered
483endfunc
484
Bram Moolenaarc947b9a2022-04-06 17:59:21 +0100485func Test_WinClosed_throws()
486 vnew
487 let bnr = bufnr()
488 call assert_equal(1, bufloaded(bnr))
489 augroup test-WinClosed
490 autocmd WinClosed * throw 'foo'
491 augroup END
492 try
493 close
494 catch /.*/
495 endtry
496 call assert_equal(0, bufloaded(bnr))
497
498 autocmd! test-WinClosed
499 augroup! test-WinClosed
500endfunc
501
zeertzjq6a069402022-04-07 14:08:29 +0100502func Test_WinClosed_throws_with_tabs()
503 tabnew
504 let bnr = bufnr()
505 call assert_equal(1, bufloaded(bnr))
506 augroup test-WinClosed
507 autocmd WinClosed * throw 'foo'
508 augroup END
509 try
510 close
511 catch /.*/
512 endtry
513 call assert_equal(0, bufloaded(bnr))
514
515 autocmd! test-WinClosed
516 augroup! test-WinClosed
517endfunc
518
zeertzjq62de54b2022-09-22 18:08:37 +0100519" This used to trigger WinClosed twice for the same window, and the window's
520" buffer was NULL in the second autocommand.
521func Test_WinClosed_switch_tab()
522 edit Xa
523 split Xb
524 split Xc
525 tab split
526 new
527 augroup test-WinClosed
528 autocmd WinClosed * tabprev | bwipe!
529 augroup END
530 close
531 " Check that the tabline has been fully removed
532 call assert_equal([1, 1], win_screenpos(0))
533
534 autocmd! test-WinClosed
535 augroup! test-WinClosed
536 %bwipe!
537endfunc
538
Bram Moolenaare99e8442016-07-26 20:43:40 +0200539func s:AddAnAutocmd()
540 augroup vimBarTest
541 au BufReadCmd * echo 'hello'
542 augroup END
543 call assert_equal(3, len(split(execute('au vimBarTest'), "\n")))
544endfunc
545
546func Test_early_bar()
547 " test that a bar is recognized before the {event}
548 call s:AddAnAutocmd()
Bram Moolenaarb8e642f2021-11-20 10:38:25 +0000549 augroup vimBarTest | au! | let done = 77 | augroup END
Bram Moolenaare99e8442016-07-26 20:43:40 +0200550 call assert_equal(1, len(split(execute('au vimBarTest'), "\n")))
Bram Moolenaarb8e642f2021-11-20 10:38:25 +0000551 call assert_equal(77, done)
Bram Moolenaare99e8442016-07-26 20:43:40 +0200552
553 call s:AddAnAutocmd()
Bram Moolenaarb8e642f2021-11-20 10:38:25 +0000554 augroup vimBarTest| au!| let done = 88 | augroup END
Bram Moolenaare99e8442016-07-26 20:43:40 +0200555 call assert_equal(1, len(split(execute('au vimBarTest'), "\n")))
Bram Moolenaarb8e642f2021-11-20 10:38:25 +0000556 call assert_equal(88, done)
Bram Moolenaare99e8442016-07-26 20:43:40 +0200557
558 " test that a bar is recognized after the {event}
559 call s:AddAnAutocmd()
Bram Moolenaarb8e642f2021-11-20 10:38:25 +0000560 augroup vimBarTest| au!BufReadCmd| let done = 99 | augroup END
Bram Moolenaare99e8442016-07-26 20:43:40 +0200561 call assert_equal(1, len(split(execute('au vimBarTest'), "\n")))
Bram Moolenaarb8e642f2021-11-20 10:38:25 +0000562 call assert_equal(99, done)
Bram Moolenaare99e8442016-07-26 20:43:40 +0200563
564 " test that a bar is recognized after the {group}
565 call s:AddAnAutocmd()
566 au! vimBarTest|echo 'hello'
567 call assert_equal(1, len(split(execute('au vimBarTest'), "\n")))
568endfunc
Bram Moolenaarf2c4c392016-07-29 20:50:24 +0200569
Bram Moolenaar5c809082016-09-01 16:21:48 +0200570func RemoveGroup()
571 autocmd! StartOK
572 augroup! StartOK
573endfunc
574
Bram Moolenaarf2c4c392016-07-29 20:50:24 +0200575func Test_augroup_warning()
576 augroup TheWarning
577 au VimEnter * echo 'entering'
578 augroup END
Bram Moolenaar5dc4e2f2020-11-25 14:15:12 +0100579 call assert_match("TheWarning.*VimEnter", execute('au VimEnter'))
Bram Moolenaarf2c4c392016-07-29 20:50:24 +0200580 redir => res
581 augroup! TheWarning
582 redir END
Bram Moolenaar5dc4e2f2020-11-25 14:15:12 +0100583 call assert_match("W19:", res)
584 call assert_match("-Deleted-.*VimEnter", execute('au VimEnter'))
Bram Moolenaarf2c4c392016-07-29 20:50:24 +0200585
586 " check "Another" does not take the pace of the deleted entry
587 augroup Another
588 augroup END
Bram Moolenaar5dc4e2f2020-11-25 14:15:12 +0100589 call assert_match("-Deleted-.*VimEnter", execute('au VimEnter'))
Bram Moolenaaraeac9002016-09-06 22:15:08 +0200590 augroup! Another
Bram Moolenaar5c809082016-09-01 16:21:48 +0200591
592 " no warning for postpone aucmd delete
593 augroup StartOK
594 au VimEnter * call RemoveGroup()
595 augroup END
Bram Moolenaar5dc4e2f2020-11-25 14:15:12 +0100596 call assert_match("StartOK.*VimEnter", execute('au VimEnter'))
Bram Moolenaar5c809082016-09-01 16:21:48 +0200597 redir => res
598 doautocmd VimEnter
599 redir END
Bram Moolenaar5dc4e2f2020-11-25 14:15:12 +0100600 call assert_notmatch("W19:", res)
Bram Moolenaarde653f02016-09-03 16:59:06 +0200601 au! VimEnter
Bram Moolenaarad48e6c2020-04-21 22:19:45 +0200602
603 call assert_fails('augroup!', 'E471:')
Bram Moolenaarf2c4c392016-07-29 20:50:24 +0200604endfunc
Bram Moolenaarb62cc362016-09-03 16:43:53 +0200605
Bram Moolenaar8d84ff12017-10-26 16:42:16 +0200606func Test_BufReadCmdHelp()
607 " This used to cause access to free memory
608 au BufReadCmd * e +h
609 help
610
Bram Moolenaar8d84ff12017-10-26 16:42:16 +0200611 au! BufReadCmd
612endfunc
613
614func Test_BufReadCmdHelpJump()
615 " This used to cause access to free memory
616 au BufReadCmd * e +h{
Bram Moolenaarcf1ba352017-10-27 00:55:04 +0200617 " } to fix highlighting
618 call assert_fails('help', 'E434:')
Bram Moolenaar8d84ff12017-10-26 16:42:16 +0200619
Bram Moolenaar8d84ff12017-10-26 16:42:16 +0200620 au! BufReadCmd
621endfunc
622
zeertzjq93f72cc2022-08-26 15:34:52 +0100623" BufReadCmd is triggered for a "nofile" buffer. Check all values.
Bram Moolenaarb1d2c812022-08-26 11:55:01 +0100624func Test_BufReadCmdNofile()
zeertzjq93f72cc2022-08-26 15:34:52 +0100625 for val in ['nofile',
626 \ 'nowrite',
627 \ 'acwrite',
628 \ 'quickfix',
629 \ 'help',
630 \ 'terminal',
631 \ 'prompt',
632 \ 'popup',
633 \ ]
634 new somefile
635 exe 'set buftype=' .. val
636 au BufReadCmd somefile call setline(1, 'triggered')
637 edit
638 call assert_equal('triggered', getline(1))
Bram Moolenaarb1d2c812022-08-26 11:55:01 +0100639
zeertzjq93f72cc2022-08-26 15:34:52 +0100640 au! BufReadCmd
641 bwipe!
642 endfor
Bram Moolenaarb1d2c812022-08-26 11:55:01 +0100643endfunc
644
Bram Moolenaarb62cc362016-09-03 16:43:53 +0200645func Test_augroup_deleted()
Bram Moolenaarde653f02016-09-03 16:59:06 +0200646 " This caused a crash before E936 was introduced
Bram Moolenaarb62cc362016-09-03 16:43:53 +0200647 augroup x
Bram Moolenaarde653f02016-09-03 16:59:06 +0200648 call assert_fails('augroup! x', 'E936:')
649 au VimEnter * echo
650 augroup end
Bram Moolenaarb62cc362016-09-03 16:43:53 +0200651 augroup! x
Bram Moolenaar5dc4e2f2020-11-25 14:15:12 +0100652 call assert_match("-Deleted-.*VimEnter", execute('au VimEnter'))
Bram Moolenaarde653f02016-09-03 16:59:06 +0200653 au! VimEnter
Bram Moolenaarb62cc362016-09-03 16:43:53 +0200654endfunc
655
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200656" Tests for autocommands on :close command.
657" This used to be in test13.
658func Test_three_windows()
Bram Moolenaarb3435b02016-09-29 20:54:59 +0200659 " Clean up buffers, because in some cases this function fails.
660 call s:cleanup_buffers()
661
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200662 " Write three files and open them, each in a window.
663 " Then go to next window, with autocommand that deletes the previous one.
664 " Do this twice, writing the file.
665 e! Xtestje1
666 call setline(1, 'testje1')
667 w
668 sp Xtestje2
669 call setline(1, 'testje2')
670 w
671 sp Xtestje3
672 call setline(1, 'testje3')
673 w
674 wincmd w
675 au WinLeave Xtestje2 bwipe
676 wincmd w
677 call assert_equal('Xtestje1', expand('%'))
678
679 au WinLeave Xtestje1 bwipe Xtestje3
680 close
681 call assert_equal('Xtestje1', expand('%'))
682
683 " Test deleting the buffer on a Unload event. If this goes wrong there
684 " will be the ATTENTION prompt.
685 e Xtestje1
686 au!
687 au! BufUnload Xtestje1 bwipe
688 call assert_fails('e Xtestje3', 'E937:')
689 call assert_equal('Xtestje3', expand('%'))
690
691 e Xtestje2
692 sp Xtestje1
693 call assert_fails('e', 'E937:')
Bram Moolenaara997b452018-04-17 23:24:06 +0200694 call assert_equal('Xtestje1', expand('%'))
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200695
696 " Test changing buffers in a BufWipeout autocommand. If this goes wrong
697 " there are ml_line errors and/or a Crash.
698 au!
699 only
700 e Xanother
701 e Xtestje1
702 bwipe Xtestje2
703 bwipe Xtestje3
704 au BufWipeout Xtestje1 buf Xtestje1
705 bwipe
706 call assert_equal('Xanother', expand('%'))
707
708 only
709 help
710 wincmd w
711 1quit
712 call assert_equal('Xanother', expand('%'))
713
714 au!
Bram Moolenaar4520d442017-03-19 16:09:46 +0100715 enew
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200716 call delete('Xtestje1')
717 call delete('Xtestje2')
718 call delete('Xtestje3')
719endfunc
Bram Moolenaare13b9af2017-01-13 22:01:02 +0100720
721func Test_BufEnter()
722 au! BufEnter
723 au Bufenter * let val = val . '+'
724 let g:val = ''
725 split NewFile
726 call assert_equal('+', g:val)
727 bwipe!
728 call assert_equal('++', g:val)
729
730 " Also get BufEnter when editing a directory
Bram Moolenaar6f14da12022-09-07 21:30:44 +0100731 call mkdir('Xbufenterdir', 'D')
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +0100732 split Xbufenterdir
Bram Moolenaare13b9af2017-01-13 22:01:02 +0100733 call assert_equal('+++', g:val)
Bram Moolenaare94260f2017-03-21 15:50:12 +0100734
735 " On MS-Windows we can't edit the directory, make sure we wipe the right
736 " buffer.
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +0100737 bwipe! Xbufenterdir
Bram Moolenaare13b9af2017-01-13 22:01:02 +0100738 au! BufEnter
Bram Moolenaara9b5b852022-08-26 13:16:20 +0100739
740 " Editing a "nofile" buffer doesn't read the file but does trigger BufEnter
zeertzjq93f72cc2022-08-26 15:34:52 +0100741 " for historic reasons. Also test other 'buftype' values.
742 for val in ['nofile',
743 \ 'nowrite',
744 \ 'acwrite',
745 \ 'quickfix',
746 \ 'help',
747 \ 'terminal',
748 \ 'prompt',
749 \ 'popup',
750 \ ]
751 new somefile
752 exe 'set buftype=' .. val
753 au BufEnter somefile call setline(1, 'some text')
754 edit
755 call assert_equal('some text', getline(1))
756 bwipe!
757 au! BufEnter
758 endfor
Bram Moolenaar9fda8152022-11-19 13:14:10 +0000759
760 new
761 new
762 autocmd BufEnter * ++once close
763 call assert_fails('close', 'E1312:')
764
765 au! BufEnter
766 only
Bram Moolenaare13b9af2017-01-13 22:01:02 +0100767endfunc
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100768
769" Closing a window might cause an endless loop
770" E814 for older Vims
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200771func Test_autocmd_bufwipe_in_SessLoadPost()
Bram Moolenaar1d68d9b2017-10-13 22:33:32 +0200772 edit Xtest
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100773 tabnew
Bram Moolenaar1d68d9b2017-10-13 22:33:32 +0200774 file Xsomething
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100775 set noswapfile
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100776 mksession!
777
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200778 let content =<< trim [CODE]
Bram Moolenaar62cd26a2020-10-11 20:08:44 +0200779 call test_override('ui_delay', 10)
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200780 set nocp noswapfile
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100781 let v:swapchoice = "e"
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200782 augroup test_autocmd_sessionload
783 autocmd!
784 autocmd SessionLoadPost * exe bufnr("Xsomething") . "bw!"
785 augroup END
786
787 func WriteErrors()
Bram Moolenaarbe9fc5b2022-09-09 17:09:35 +0100788 call writefile([execute("messages")], "XerrorsBwipe")
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200789 endfunc
790 au VimLeave * call WriteErrors()
791 [CODE]
792
Bram Moolenaare1f3ab72022-09-04 21:29:08 +0100793 call writefile(content, 'Xvimrc', 'D')
Bram Moolenaar93344c22019-08-14 21:12:05 +0200794 call system(GetVimCommand('Xvimrc') .. ' --not-a-term --noplugins -S Session.vim -c cq')
Bram Moolenaarae04a602022-09-09 15:08:10 +0100795 sleep 100m
Bram Moolenaarbe9fc5b2022-09-09 17:09:35 +0100796 let errors = join(readfile('XerrorsBwipe'))
Bram Moolenaare2e40752020-09-04 21:18:46 +0200797 call assert_match('E814:', errors)
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100798
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100799 set swapfile
Bram Moolenaarbe9fc5b2022-09-09 17:09:35 +0100800 for file in ['Session.vim', 'XerrorsBwipe']
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100801 call delete(file)
802 endfor
803endfunc
804
Bram Moolenaar797e63b2021-01-15 16:22:52 +0100805" Using :blast and :ball for many events caused a crash, because b_nwindows was
806" not incremented correctly.
807func Test_autocmd_blast_badd()
808 let content =<< trim [CODE]
809 au BufNew,BufAdd,BufWinEnter,BufEnter,BufLeave,BufWinLeave,BufUnload,VimEnter foo* blast
810 edit foo1
811 au BufNew,BufAdd,BufWinEnter,BufEnter,BufLeave,BufWinLeave,BufUnload,VimEnter foo* ball
812 edit foo2
Bram Moolenaarbe9fc5b2022-09-09 17:09:35 +0100813 call writefile(['OK'], 'XerrorsBlast')
Bram Moolenaar797e63b2021-01-15 16:22:52 +0100814 qall
815 [CODE]
816
Bram Moolenaare1f3ab72022-09-04 21:29:08 +0100817 call writefile(content, 'XblastBall', 'D')
Bram Moolenaar797e63b2021-01-15 16:22:52 +0100818 call system(GetVimCommand() .. ' --clean -S XblastBall')
Bram Moolenaarae04a602022-09-09 15:08:10 +0100819 sleep 100m
Bram Moolenaarbe9fc5b2022-09-09 17:09:35 +0100820 call assert_match('OK', readfile('XerrorsBlast')->join())
Bram Moolenaar797e63b2021-01-15 16:22:52 +0100821
Bram Moolenaarbe9fc5b2022-09-09 17:09:35 +0100822 call delete('XerrorsBlast')
Bram Moolenaar797e63b2021-01-15 16:22:52 +0100823endfunc
824
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100825" SEGV occurs in older versions.
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200826func Test_autocmd_bufwipe_in_SessLoadPost2()
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100827 tabnew
828 set noswapfile
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100829 mksession!
830
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200831 let content =<< trim [CODE]
832 set nocp noswapfile
833 function! DeleteInactiveBufs()
834 tabfirst
835 let tabblist = []
836 for i in range(1, tabpagenr(''$''))
837 call extend(tabblist, tabpagebuflist(i))
838 endfor
839 for b in range(1, bufnr(''$''))
840 if bufexists(b) && buflisted(b) && (index(tabblist, b) == -1 || bufname(b) =~# ''^$'')
841 exec ''bwipeout '' . b
842 endif
843 endfor
844 echomsg "SessionLoadPost DONE"
845 endfunction
846 au SessionLoadPost * call DeleteInactiveBufs()
847
848 func WriteErrors()
Bram Moolenaarbe9fc5b2022-09-09 17:09:35 +0100849 call writefile([execute("messages")], "XerrorsPost")
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200850 endfunc
851 au VimLeave * call WriteErrors()
852 [CODE]
853
Bram Moolenaare1f3ab72022-09-04 21:29:08 +0100854 call writefile(content, 'Xvimrc', 'D')
Bram Moolenaar93344c22019-08-14 21:12:05 +0200855 call system(GetVimCommand('Xvimrc') .. ' --not-a-term --noplugins -S Session.vim -c cq')
Bram Moolenaarae04a602022-09-09 15:08:10 +0100856 sleep 100m
Bram Moolenaarbe9fc5b2022-09-09 17:09:35 +0100857 let errors = join(readfile('XerrorsPost'))
Bram Moolenaare94260f2017-03-21 15:50:12 +0100858 " This probably only ever matches on unix.
859 call assert_notmatch('Caught deadly signal SEGV', errors)
860 call assert_match('SessionLoadPost DONE', errors)
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100861
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100862 set swapfile
Bram Moolenaarbe9fc5b2022-09-09 17:09:35 +0100863 for file in ['Session.vim', 'XerrorsPost']
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100864 call delete(file)
865 endfor
866endfunc
Bram Moolenaarfaf29d72017-07-09 11:07:16 +0200867
868func Test_empty_doau()
869 doau \|
870endfunc
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200871
872func s:AutoCommandOptionSet(match)
Bram Moolenaard7c96872019-06-15 17:12:48 +0200873 let template = "Option: <%s>, OldVal: <%s>, OldValLocal: <%s>, OldValGlobal: <%s>, NewVal: <%s>, Scope: <%s>, Command: <%s>\n"
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200874 let item = remove(g:options, 0)
Bram Moolenaard7c96872019-06-15 17:12:48 +0200875 let expected = printf(template, item[0], item[1], item[2], item[3], item[4], item[5], item[6])
876 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 +0200877 let g:opt = [expected, actual]
878 "call assert_equal(expected, actual)
879endfunc
880
881func Test_OptionSet()
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +0200882 CheckOption autochdir
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200883
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +0200884 badd test_autocmd.vim
885
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200886 call test_override('starting', 1)
887 set nocp
888 au OptionSet * :call s:AutoCommandOptionSet(expand("<amatch>"))
889
890 " 1: Setting number option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100891 let g:options = [['number', 0, 0, 0, 1, 'global', 'set']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200892 set nu
893 call assert_equal([], g:options)
894 call assert_equal(g:opt[0], g:opt[1])
895
896 " 2: Setting local number option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100897 let g:options = [['number', 1, 1, '', 0, 'local', 'setlocal']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200898 setlocal nonu
899 call assert_equal([], g:options)
900 call assert_equal(g:opt[0], g:opt[1])
901
902 " 3: Setting global number option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100903 let g:options = [['number', 1, '', 1, 0, 'global', 'setglobal']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200904 setglobal nonu
905 call assert_equal([], g:options)
906 call assert_equal(g:opt[0], g:opt[1])
907
908 " 4: Setting local autoindent option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100909 let g:options = [['autoindent', 0, 0, '', 1, 'local', 'setlocal']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200910 setlocal ai
911 call assert_equal([], g:options)
912 call assert_equal(g:opt[0], g:opt[1])
913
914 " 5: Setting global autoindent option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100915 let g:options = [['autoindent', 0, '', 0, 1, 'global', 'setglobal']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200916 setglobal ai
917 call assert_equal([], g:options)
918 call assert_equal(g:opt[0], g:opt[1])
919
920 " 6: Setting global autoindent option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100921 let g:options = [['autoindent', 1, 1, 1, 0, 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +0200922 set ai!
923 call assert_equal([], g:options)
924 call assert_equal(g:opt[0], g:opt[1])
925
926 " 6a: Setting global autoindent option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100927 let g:options = [['autoindent', 1, 1, 0, 0, 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +0200928 noa setlocal ai
929 noa setglobal noai
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200930 set ai!
931 call assert_equal([], g:options)
932 call assert_equal(g:opt[0], g:opt[1])
933
934 " Should not print anything, use :noa
935 " 7: don't trigger OptionSet"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100936 let g:options = [['invalid', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200937 noa set nonu
Bram Moolenaard7c96872019-06-15 17:12:48 +0200938 call assert_equal([['invalid', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid']], g:options)
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200939 call assert_equal(g:opt[0], g:opt[1])
940
941 " 8: Setting several global list and number option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100942 let g:options = [['list', 0, 0, 0, 1, 'global', 'set'], ['number', 0, 0, 0, 1, 'global', 'set']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200943 set list nu
944 call assert_equal([], g:options)
945 call assert_equal(g:opt[0], g:opt[1])
946
947 " 9: don't trigger OptionSet"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100948 let g:options = [['invalid', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid'], ['invalid', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200949 noa set nolist nonu
Bram Moolenaard7c96872019-06-15 17:12:48 +0200950 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 +0200951 call assert_equal(g:opt[0], g:opt[1])
952
953 " 10: Setting global acd"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100954 let g:options = [['autochdir', 0, 0, '', 1, 'local', 'setlocal']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200955 setlocal acd
956 call assert_equal([], g:options)
957 call assert_equal(g:opt[0], g:opt[1])
958
959 " 11: Setting global autoread (also sets local value)"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100960 let g:options = [['autoread', 0, 0, 0, 1, 'global', 'set']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200961 set ar
962 call assert_equal([], g:options)
963 call assert_equal(g:opt[0], g:opt[1])
964
965 " 12: Setting local autoread"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100966 let g:options = [['autoread', 1, 1, '', 1, 'local', 'setlocal']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200967 setlocal ar
968 call assert_equal([], g:options)
969 call assert_equal(g:opt[0], g:opt[1])
970
971 " 13: Setting global autoread"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100972 let g:options = [['autoread', 1, '', 1, 0, 'global', 'setglobal']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200973 setglobal invar
974 call assert_equal([], g:options)
975 call assert_equal(g:opt[0], g:opt[1])
976
977 " 14: Setting option backspace through :let"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100978 let g:options = [['backspace', '', '', '', 'eol,indent,start', 'global', 'set']]
979 let &bs = "eol,indent,start"
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200980 call assert_equal([], g:options)
981 call assert_equal(g:opt[0], g:opt[1])
982
983 " 15: Setting option backspace through setbufvar()"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100984 let g:options = [['backup', 0, 0, '', 1, 'local', 'setlocal']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200985 " try twice, first time, shouldn't trigger because option name is invalid,
986 " second time, it should trigger
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +0200987 let bnum = bufnr('%')
Bram Moolenaare2e40752020-09-04 21:18:46 +0200988 call assert_fails("call setbufvar(bnum, '&l:bk', 1)", 'E355:')
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200989 " should trigger, use correct option name
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +0200990 call setbufvar(bnum, '&backup', 1)
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200991 call assert_equal([], g:options)
992 call assert_equal(g:opt[0], g:opt[1])
993
994 " 16: Setting number option using setwinvar"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100995 let g:options = [['number', 0, 0, '', 1, 'local', 'setlocal']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200996 call setwinvar(0, '&number', 1)
997 call assert_equal([], g:options)
998 call assert_equal(g:opt[0], g:opt[1])
999
1000 " 17: Setting key option, shouldn't trigger"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001001 let g:options = [['key', 'invalid', 'invalid1', 'invalid2', 'invalid3', 'invalid4', 'invalid5']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001002 setlocal key=blah
1003 setlocal key=
Bram Moolenaard7c96872019-06-15 17:12:48 +02001004 call assert_equal([['key', 'invalid', 'invalid1', 'invalid2', 'invalid3', 'invalid4', 'invalid5']], g:options)
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001005 call assert_equal(g:opt[0], g:opt[1])
1006
Bram Moolenaard7c96872019-06-15 17:12:48 +02001007
1008 " 18a: Setting string global option"
1009 let oldval = &backupext
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001010 let g:options = [['backupext', oldval, oldval, oldval, 'foo', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001011 set backupext=foo
1012 call assert_equal([], g:options)
1013 call assert_equal(g:opt[0], g:opt[1])
1014
1015 " 18b: Resetting string global option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001016 let g:options = [['backupext', 'foo', 'foo', 'foo', oldval, 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001017 set backupext&
1018 call assert_equal([], g:options)
1019 call assert_equal(g:opt[0], g:opt[1])
1020
1021 " 18c: Setting global string global option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001022 let g:options = [['backupext', oldval, '', oldval, 'bar', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001023 setglobal backupext=bar
1024 call assert_equal([], g:options)
1025 call assert_equal(g:opt[0], g:opt[1])
1026
1027 " 18d: Setting local string global option"
1028 " As this is a global option this sets the global value even though
1029 " :setlocal is used!
1030 noa set backupext& " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001031 let g:options = [['backupext', oldval, oldval, '', 'baz', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001032 setlocal backupext=baz
1033 call assert_equal([], g:options)
1034 call assert_equal(g:opt[0], g:opt[1])
1035
1036 " 18e: Setting again string global option"
1037 noa setglobal backupext=ext_global " Reset global and local value (without triggering autocmd)
1038 noa setlocal backupext=ext_local " Sets the global(!) value!
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001039 let g:options = [['backupext', 'ext_local', 'ext_local', 'ext_local', 'fuu', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001040 set backupext=fuu
1041 call assert_equal([], g:options)
1042 call assert_equal(g:opt[0], g:opt[1])
1043
1044
zeertzjqb811de52021-10-21 10:50:44 +01001045 " 19a: Setting string global-local (to buffer) option"
Bram Moolenaar8efa0262017-08-20 15:47:20 +02001046 let oldval = &tags
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001047 let g:options = [['tags', oldval, oldval, oldval, 'tagpath', 'global', 'set']]
Bram Moolenaar8efa0262017-08-20 15:47:20 +02001048 set tags=tagpath
1049 call assert_equal([], g:options)
1050 call assert_equal(g:opt[0], g:opt[1])
1051
zeertzjqb811de52021-10-21 10:50:44 +01001052 " 19b: Resetting string global-local (to buffer) option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001053 let g:options = [['tags', 'tagpath', 'tagpath', 'tagpath', oldval, 'global', 'set']]
Bram Moolenaar8efa0262017-08-20 15:47:20 +02001054 set tags&
1055 call assert_equal([], g:options)
1056 call assert_equal(g:opt[0], g:opt[1])
1057
zeertzjqb811de52021-10-21 10:50:44 +01001058 " 19c: Setting global string global-local (to buffer) option "
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001059 let g:options = [['tags', oldval, '', oldval, 'tagpath1', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001060 setglobal tags=tagpath1
1061 call assert_equal([], g:options)
1062 call assert_equal(g:opt[0], g:opt[1])
1063
zeertzjqb811de52021-10-21 10:50:44 +01001064 " 19d: Setting local string global-local (to buffer) option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001065 let g:options = [['tags', 'tagpath1', 'tagpath1', '', 'tagpath2', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001066 setlocal tags=tagpath2
1067 call assert_equal([], g:options)
1068 call assert_equal(g:opt[0], g:opt[1])
1069
zeertzjqb811de52021-10-21 10:50:44 +01001070 " 19e: Setting again string global-local (to buffer) option"
1071 " Note: v:option_old is the old global value for global-local string options
Bram Moolenaard7c96872019-06-15 17:12:48 +02001072 " but the old local value for all other kinds of options.
1073 noa setglobal tags=tag_global " Reset global and local value (without triggering autocmd)
1074 noa setlocal tags=tag_local
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001075 let g:options = [['tags', 'tag_global', 'tag_local', 'tag_global', 'tagpath', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001076 set tags=tagpath
1077 call assert_equal([], g:options)
1078 call assert_equal(g:opt[0], g:opt[1])
1079
zeertzjqb811de52021-10-21 10:50:44 +01001080 " 19f: Setting string global-local (to buffer) option to an empty string"
1081 " Note: v:option_old is the old global value for global-local string options
Bram Moolenaard7c96872019-06-15 17:12:48 +02001082 " but the old local value for all other kinds of options.
1083 noa set tags=tag_global " Reset global and local value (without triggering autocmd)
1084 noa setlocal tags= " empty string
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001085 let g:options = [['tags', 'tag_global', '', 'tag_global', 'tagpath', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001086 set tags=tagpath
1087 call assert_equal([], g:options)
1088 call assert_equal(g:opt[0], g:opt[1])
1089
1090
1091 " 20a: Setting string local (to buffer) option"
1092 let oldval = &spelllang
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001093 let g:options = [['spelllang', oldval, oldval, oldval, 'elvish,klingon', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001094 set spelllang=elvish,klingon
1095 call assert_equal([], g:options)
1096 call assert_equal(g:opt[0], g:opt[1])
1097
1098 " 20b: Resetting string local (to buffer) option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001099 let g:options = [['spelllang', 'elvish,klingon', 'elvish,klingon', 'elvish,klingon', oldval, 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001100 set spelllang&
1101 call assert_equal([], g:options)
1102 call assert_equal(g:opt[0], g:opt[1])
1103
1104 " 20c: Setting global string local (to buffer) option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001105 let g:options = [['spelllang', oldval, '', oldval, 'elvish', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001106 setglobal spelllang=elvish
1107 call assert_equal([], g:options)
1108 call assert_equal(g:opt[0], g:opt[1])
1109
1110 " 20d: Setting local string local (to buffer) option"
1111 noa set spelllang& " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001112 let g:options = [['spelllang', oldval, oldval, '', 'klingon', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001113 setlocal spelllang=klingon
1114 call assert_equal([], g:options)
1115 call assert_equal(g:opt[0], g:opt[1])
1116
1117 " 20e: Setting again string local (to buffer) option"
zeertzjqb811de52021-10-21 10:50:44 +01001118 " Note: v:option_old is the old global value for global-local string options
Bram Moolenaard7c96872019-06-15 17:12:48 +02001119 " but the old local value for all other kinds of options.
1120 noa setglobal spelllang=spellglobal " Reset global and local value (without triggering autocmd)
1121 noa setlocal spelllang=spelllocal
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001122 let g:options = [['spelllang', 'spelllocal', 'spelllocal', 'spellglobal', 'foo', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001123 set spelllang=foo
1124 call assert_equal([], g:options)
1125 call assert_equal(g:opt[0], g:opt[1])
1126
1127
zeertzjqb811de52021-10-21 10:50:44 +01001128 " 21a: Setting string global-local (to window) option"
Bram Moolenaard7c96872019-06-15 17:12:48 +02001129 let oldval = &statusline
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001130 let g:options = [['statusline', oldval, oldval, oldval, 'foo', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001131 set statusline=foo
1132 call assert_equal([], g:options)
1133 call assert_equal(g:opt[0], g:opt[1])
1134
zeertzjqb811de52021-10-21 10:50:44 +01001135 " 21b: Resetting string global-local (to window) option"
1136 " Note: v:option_old is the old global value for global-local string options
Bram Moolenaard7c96872019-06-15 17:12:48 +02001137 " but the old local value for all other kinds of options.
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001138 let g:options = [['statusline', 'foo', 'foo', 'foo', oldval, 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001139 set statusline&
1140 call assert_equal([], g:options)
1141 call assert_equal(g:opt[0], g:opt[1])
1142
zeertzjqb811de52021-10-21 10:50:44 +01001143 " 21c: Setting global string global-local (to window) option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001144 let g:options = [['statusline', oldval, '', oldval, 'bar', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001145 setglobal statusline=bar
1146 call assert_equal([], g:options)
1147 call assert_equal(g:opt[0], g:opt[1])
1148
zeertzjqb811de52021-10-21 10:50:44 +01001149 " 21d: Setting local string global-local (to window) option"
Bram Moolenaard7c96872019-06-15 17:12:48 +02001150 noa set statusline& " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001151 let g:options = [['statusline', oldval, oldval, '', 'baz', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001152 setlocal statusline=baz
1153 call assert_equal([], g:options)
1154 call assert_equal(g:opt[0], g:opt[1])
1155
zeertzjqb811de52021-10-21 10:50:44 +01001156 " 21e: Setting again string global-local (to window) option"
1157 " Note: v:option_old is the old global value for global-local string options
Bram Moolenaard7c96872019-06-15 17:12:48 +02001158 " but the old local value for all other kinds of options.
1159 noa setglobal statusline=bar " Reset global and local value (without triggering autocmd)
1160 noa setlocal statusline=baz
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001161 let g:options = [['statusline', 'bar', 'baz', 'bar', 'foo', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001162 set statusline=foo
1163 call assert_equal([], g:options)
1164 call assert_equal(g:opt[0], g:opt[1])
1165
1166
1167 " 22a: Setting string local (to window) option"
1168 let oldval = &foldignore
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001169 let g:options = [['foldignore', oldval, oldval, oldval, 'fo', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001170 set foldignore=fo
1171 call assert_equal([], g:options)
1172 call assert_equal(g:opt[0], g:opt[1])
1173
1174 " 22b: Resetting string local (to window) option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001175 let g:options = [['foldignore', 'fo', 'fo', 'fo', oldval, 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001176 set foldignore&
1177 call assert_equal([], g:options)
1178 call assert_equal(g:opt[0], g:opt[1])
1179
1180 " 22c: Setting global string local (to window) option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001181 let g:options = [['foldignore', oldval, '', oldval, 'bar', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001182 setglobal foldignore=bar
1183 call assert_equal([], g:options)
1184 call assert_equal(g:opt[0], g:opt[1])
1185
1186 " 22d: Setting local string local (to window) option"
1187 noa set foldignore& " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001188 let g:options = [['foldignore', oldval, oldval, '', 'baz', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001189 setlocal foldignore=baz
1190 call assert_equal([], g:options)
1191 call assert_equal(g:opt[0], g:opt[1])
1192
1193 " 22e: Setting again string local (to window) option"
1194 noa setglobal foldignore=glob " Reset global and local value (without triggering autocmd)
1195 noa setlocal foldignore=loc
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001196 let g:options = [['foldignore', 'loc', 'loc', 'glob', 'fo', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001197 set foldignore=fo
1198 call assert_equal([], g:options)
1199 call assert_equal(g:opt[0], g:opt[1])
1200
1201
zeertzjqb811de52021-10-21 10:50:44 +01001202 " 23a: Setting global number global option"
Bram Moolenaard7c96872019-06-15 17:12:48 +02001203 noa setglobal cmdheight=8 " Reset global and local value (without triggering autocmd)
1204 noa setlocal cmdheight=1 " Sets the global(!) value!
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001205 let g:options = [['cmdheight', '1', '', '1', '2', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001206 setglobal cmdheight=2
1207 call assert_equal([], g:options)
1208 call assert_equal(g:opt[0], g:opt[1])
1209
1210 " 23b: Setting local number global option"
1211 noa setglobal cmdheight=8 " Reset global and local value (without triggering autocmd)
1212 noa setlocal cmdheight=1 " Sets the global(!) value!
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001213 let g:options = [['cmdheight', '1', '1', '', '2', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001214 setlocal cmdheight=2
1215 call assert_equal([], g:options)
1216 call assert_equal(g:opt[0], g:opt[1])
1217
1218 " 23c: Setting again number global option"
1219 noa setglobal cmdheight=8 " Reset global and local value (without triggering autocmd)
1220 noa setlocal cmdheight=1 " Sets the global(!) value!
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001221 let g:options = [['cmdheight', '1', '1', '1', '2', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001222 set cmdheight=2
1223 call assert_equal([], g:options)
1224 call assert_equal(g:opt[0], g:opt[1])
1225
1226 " 23d: Setting again number global option"
1227 noa set cmdheight=8 " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001228 let g:options = [['cmdheight', '8', '8', '8', '2', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001229 set cmdheight=2
1230 call assert_equal([], g:options)
1231 call assert_equal(g:opt[0], g:opt[1])
1232
1233
1234 " 24a: Setting global number global-local (to buffer) option"
1235 noa setglobal undolevels=8 " Reset global and local value (without triggering autocmd)
1236 noa setlocal undolevels=1
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001237 let g:options = [['undolevels', '8', '', '8', '2', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001238 setglobal undolevels=2
1239 call assert_equal([], g:options)
1240 call assert_equal(g:opt[0], g:opt[1])
1241
1242 " 24b: Setting local number global-local (to buffer) option"
1243 noa setglobal undolevels=8 " Reset global and local value (without triggering autocmd)
1244 noa setlocal undolevels=1
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001245 let g:options = [['undolevels', '1', '1', '', '2', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001246 setlocal undolevels=2
1247 call assert_equal([], g:options)
1248 call assert_equal(g:opt[0], g:opt[1])
1249
1250 " 24c: Setting again number global-local (to buffer) option"
1251 noa setglobal undolevels=8 " Reset global and local value (without triggering autocmd)
1252 noa setlocal undolevels=1
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001253 let g:options = [['undolevels', '1', '1', '8', '2', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001254 set undolevels=2
1255 call assert_equal([], g:options)
1256 call assert_equal(g:opt[0], g:opt[1])
1257
1258 " 24d: Setting again global number global-local (to buffer) option"
1259 noa set undolevels=8 " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001260 let g:options = [['undolevels', '8', '8', '8', '2', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001261 set undolevels=2
1262 call assert_equal([], g:options)
1263 call assert_equal(g:opt[0], g:opt[1])
1264
1265
1266 " 25a: Setting global number local (to buffer) option"
1267 noa setglobal wrapmargin=8 " Reset global and local value (without triggering autocmd)
1268 noa setlocal wrapmargin=1
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001269 let g:options = [['wrapmargin', '8', '', '8', '2', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001270 setglobal wrapmargin=2
1271 call assert_equal([], g:options)
1272 call assert_equal(g:opt[0], g:opt[1])
1273
1274 " 25b: Setting local number local (to buffer) option"
1275 noa setglobal wrapmargin=8 " Reset global and local value (without triggering autocmd)
1276 noa setlocal wrapmargin=1
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001277 let g:options = [['wrapmargin', '1', '1', '', '2', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001278 setlocal wrapmargin=2
1279 call assert_equal([], g:options)
1280 call assert_equal(g:opt[0], g:opt[1])
1281
1282 " 25c: Setting again number local (to buffer) option"
1283 noa setglobal wrapmargin=8 " Reset global and local value (without triggering autocmd)
1284 noa setlocal wrapmargin=1
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001285 let g:options = [['wrapmargin', '1', '1', '8', '2', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001286 set wrapmargin=2
1287 call assert_equal([], g:options)
1288 call assert_equal(g:opt[0], g:opt[1])
1289
1290 " 25d: Setting again global number local (to buffer) option"
1291 noa set wrapmargin=8 " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001292 let g:options = [['wrapmargin', '8', '8', '8', '2', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001293 set wrapmargin=2
1294 call assert_equal([], g:options)
1295 call assert_equal(g:opt[0], g:opt[1])
1296
1297
1298 " 26: Setting number global-local (to window) option.
1299 " Such option does currently not exist.
1300
1301
1302 " 27a: Setting global number local (to window) option"
1303 noa setglobal foldcolumn=8 " Reset global and local value (without triggering autocmd)
1304 noa setlocal foldcolumn=1
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001305 let g:options = [['foldcolumn', '8', '', '8', '2', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001306 setglobal foldcolumn=2
1307 call assert_equal([], g:options)
1308 call assert_equal(g:opt[0], g:opt[1])
1309
1310 " 27b: Setting local number local (to window) option"
1311 noa setglobal foldcolumn=8 " Reset global and local value (without triggering autocmd)
1312 noa setlocal foldcolumn=1
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001313 let g:options = [['foldcolumn', '1', '1', '', '2', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001314 setlocal foldcolumn=2
1315 call assert_equal([], g:options)
1316 call assert_equal(g:opt[0], g:opt[1])
1317
1318 " 27c: Setting again number local (to window) option"
1319 noa setglobal foldcolumn=8 " Reset global and local value (without triggering autocmd)
1320 noa setlocal foldcolumn=1
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001321 let g:options = [['foldcolumn', '1', '1', '8', '2', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001322 set foldcolumn=2
1323 call assert_equal([], g:options)
1324 call assert_equal(g:opt[0], g:opt[1])
1325
zeertzjqb811de52021-10-21 10:50:44 +01001326 " 27d: Setting again global number local (to window) option"
Bram Moolenaard7c96872019-06-15 17:12:48 +02001327 noa set foldcolumn=8 " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001328 let g:options = [['foldcolumn', '8', '8', '8', '2', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001329 set foldcolumn=2
1330 call assert_equal([], g:options)
1331 call assert_equal(g:opt[0], g:opt[1])
1332
1333
1334 " 28a: Setting global boolean global option"
1335 noa setglobal nowrapscan " Reset global and local value (without triggering autocmd)
1336 noa setlocal wrapscan " Sets the global(!) value!
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001337 let g:options = [['wrapscan', '1', '', '1', '0', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001338 setglobal nowrapscan
1339 call assert_equal([], g:options)
1340 call assert_equal(g:opt[0], g:opt[1])
1341
1342 " 28b: Setting local boolean global option"
1343 noa setglobal nowrapscan " Reset global and local value (without triggering autocmd)
1344 noa setlocal wrapscan " Sets the global(!) value!
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001345 let g:options = [['wrapscan', '1', '1', '', '0', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001346 setlocal nowrapscan
1347 call assert_equal([], g:options)
1348 call assert_equal(g:opt[0], g:opt[1])
1349
1350 " 28c: Setting again boolean global option"
1351 noa setglobal nowrapscan " Reset global and local value (without triggering autocmd)
1352 noa setlocal wrapscan " Sets the global(!) value!
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001353 let g:options = [['wrapscan', '1', '1', '1', '0', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001354 set nowrapscan
1355 call assert_equal([], g:options)
1356 call assert_equal(g:opt[0], g:opt[1])
1357
1358 " 28d: Setting again global boolean global option"
1359 noa set nowrapscan " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001360 let g:options = [['wrapscan', '0', '0', '0', '1', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001361 set wrapscan
1362 call assert_equal([], g:options)
1363 call assert_equal(g:opt[0], g:opt[1])
1364
1365
1366 " 29a: Setting global boolean global-local (to buffer) option"
1367 noa setglobal noautoread " Reset global and local value (without triggering autocmd)
1368 noa setlocal autoread
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001369 let g:options = [['autoread', '0', '', '0', '1', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001370 setglobal autoread
1371 call assert_equal([], g:options)
1372 call assert_equal(g:opt[0], g:opt[1])
1373
1374 " 29b: Setting local boolean global-local (to buffer) option"
1375 noa setglobal noautoread " Reset global and local value (without triggering autocmd)
1376 noa setlocal autoread
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001377 let g:options = [['autoread', '1', '1', '', '0', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001378 setlocal noautoread
1379 call assert_equal([], g:options)
1380 call assert_equal(g:opt[0], g:opt[1])
1381
1382 " 29c: Setting again boolean global-local (to buffer) option"
1383 noa setglobal noautoread " Reset global and local value (without triggering autocmd)
1384 noa setlocal autoread
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001385 let g:options = [['autoread', '1', '1', '0', '1', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001386 set autoread
1387 call assert_equal([], g:options)
1388 call assert_equal(g:opt[0], g:opt[1])
1389
1390 " 29d: Setting again global boolean global-local (to buffer) option"
1391 noa set noautoread " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001392 let g:options = [['autoread', '0', '0', '0', '1', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001393 set autoread
1394 call assert_equal([], g:options)
1395 call assert_equal(g:opt[0], g:opt[1])
1396
1397
1398 " 30a: Setting global boolean local (to buffer) option"
1399 noa setglobal nocindent " Reset global and local value (without triggering autocmd)
1400 noa setlocal cindent
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001401 let g:options = [['cindent', '0', '', '0', '1', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001402 setglobal cindent
1403 call assert_equal([], g:options)
1404 call assert_equal(g:opt[0], g:opt[1])
1405
1406 " 30b: Setting local boolean local (to buffer) option"
1407 noa setglobal nocindent " Reset global and local value (without triggering autocmd)
1408 noa setlocal cindent
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001409 let g:options = [['cindent', '1', '1', '', '0', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001410 setlocal nocindent
1411 call assert_equal([], g:options)
1412 call assert_equal(g:opt[0], g:opt[1])
1413
1414 " 30c: Setting again boolean local (to buffer) option"
1415 noa setglobal nocindent " Reset global and local value (without triggering autocmd)
1416 noa setlocal cindent
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001417 let g:options = [['cindent', '1', '1', '0', '1', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001418 set cindent
1419 call assert_equal([], g:options)
1420 call assert_equal(g:opt[0], g:opt[1])
1421
1422 " 30d: Setting again global boolean local (to buffer) option"
1423 noa set nocindent " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001424 let g:options = [['cindent', '0', '0', '0', '1', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001425 set cindent
1426 call assert_equal([], g:options)
1427 call assert_equal(g:opt[0], g:opt[1])
1428
1429
1430 " 31: Setting boolean global-local (to window) option
1431 " Currently no such option exists.
1432
1433
1434 " 32a: Setting global boolean local (to window) option"
1435 noa setglobal nocursorcolumn " Reset global and local value (without triggering autocmd)
1436 noa setlocal cursorcolumn
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001437 let g:options = [['cursorcolumn', '0', '', '0', '1', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001438 setglobal cursorcolumn
1439 call assert_equal([], g:options)
1440 call assert_equal(g:opt[0], g:opt[1])
1441
1442 " 32b: Setting local boolean local (to window) option"
1443 noa setglobal nocursorcolumn " Reset global and local value (without triggering autocmd)
1444 noa setlocal cursorcolumn
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001445 let g:options = [['cursorcolumn', '1', '1', '', '0', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001446 setlocal nocursorcolumn
1447 call assert_equal([], g:options)
1448 call assert_equal(g:opt[0], g:opt[1])
1449
1450 " 32c: Setting again boolean local (to window) option"
1451 noa setglobal nocursorcolumn " Reset global and local value (without triggering autocmd)
1452 noa setlocal cursorcolumn
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001453 let g:options = [['cursorcolumn', '1', '1', '0', '1', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001454 set cursorcolumn
1455 call assert_equal([], g:options)
1456 call assert_equal(g:opt[0], g:opt[1])
1457
1458 " 32d: Setting again global boolean local (to window) option"
1459 noa set nocursorcolumn " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001460 let g:options = [['cursorcolumn', '0', '0', '0', '1', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001461 set cursorcolumn
1462 call assert_equal([], g:options)
1463 call assert_equal(g:opt[0], g:opt[1])
1464
1465
Bram Moolenaar1bc353b2019-09-01 14:45:28 +02001466 " 33: Test autocommands when an option value is converted internally.
Bram Moolenaard7c96872019-06-15 17:12:48 +02001467 noa set backspace=1 " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001468 let g:options = [['backspace', 'indent,eol', 'indent,eol', 'indent,eol', '2', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001469 set backspace=2
1470 call assert_equal([], g:options)
1471 call assert_equal(g:opt[0], g:opt[1])
1472
1473
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001474 " Cleanup
1475 au! OptionSet
Bram Moolenaar0331faf2019-06-15 18:40:37 +02001476 " set tags&
Bram Moolenaard7c96872019-06-15 17:12:48 +02001477 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 +02001478 exe printf(":set %s&vim", opt)
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001479 endfor
1480 call test_override('starting', 0)
1481 delfunc! AutoCommandOptionSet
1482endfunc
1483
1484func Test_OptionSet_diffmode()
1485 call test_override('starting', 1)
Bram Moolenaar26d98212019-01-27 22:32:55 +01001486 " 18: Changing an option when entering diff mode
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001487 new
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001488 au OptionSet diff :let &l:cul = v:option_new
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001489
1490 call setline(1, ['buffer 1', 'line2', 'line3', 'line4'])
1491 call assert_equal(0, &l:cul)
1492 diffthis
1493 call assert_equal(1, &l:cul)
1494
1495 vnew
1496 call setline(1, ['buffer 2', 'line 2', 'line 3', 'line4'])
1497 call assert_equal(0, &l:cul)
1498 diffthis
1499 call assert_equal(1, &l:cul)
1500
1501 diffoff
1502 call assert_equal(0, &l:cul)
1503 call assert_equal(1, getwinvar(2, '&l:cul'))
1504 bw!
1505
1506 call assert_equal(1, &l:cul)
1507 diffoff!
1508 call assert_equal(0, &l:cul)
1509 call assert_equal(0, getwinvar(1, '&l:cul'))
1510 bw!
1511
1512 " Cleanup
1513 au! OptionSet
1514 call test_override('starting', 0)
1515endfunc
1516
1517func Test_OptionSet_diffmode_close()
1518 call test_override('starting', 1)
1519 " 19: Try to close the current window when entering diff mode
1520 " should not segfault
1521 new
1522 au OptionSet diff close
1523
1524 call setline(1, ['buffer 1', 'line2', 'line3', 'line4'])
Bram Moolenaare2e40752020-09-04 21:18:46 +02001525 call assert_fails(':diffthis', 'E788:')
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001526 call assert_equal(1, &diff)
1527 vnew
1528 call setline(1, ['buffer 2', 'line 2', 'line 3', 'line4'])
Bram Moolenaare2e40752020-09-04 21:18:46 +02001529 call assert_fails(':diffthis', 'E788:')
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001530 call assert_equal(1, &diff)
Bram Moolenaara9aa86f2019-11-10 21:25:45 +01001531 set diffopt-=closeoff
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001532 bw!
Bram Moolenaare2e40752020-09-04 21:18:46 +02001533 call assert_fails(':diffoff!', 'E788:')
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001534 bw!
1535
1536 " Cleanup
1537 au! OptionSet
1538 call test_override('starting', 0)
1539 "delfunc! AutoCommandOptionSet
1540endfunc
Bram Moolenaar4a137b42017-08-04 22:37:11 +02001541
1542" Test for Bufleave autocommand that deletes the buffer we are about to edit.
1543func Test_BufleaveWithDelete()
Bram Moolenaare7cda972022-08-29 11:02:59 +01001544 new | edit XbufLeave1
Bram Moolenaar4a137b42017-08-04 22:37:11 +02001545
1546 augroup test_bufleavewithdelete
1547 autocmd!
Bram Moolenaare7cda972022-08-29 11:02:59 +01001548 autocmd BufLeave XbufLeave1 bwipe XbufLeave2
Bram Moolenaar4a137b42017-08-04 22:37:11 +02001549 augroup END
1550
Bram Moolenaare7cda972022-08-29 11:02:59 +01001551 call assert_fails('edit XbufLeave2', 'E143:')
1552 call assert_equal('XbufLeave1', bufname('%'))
Bram Moolenaar4a137b42017-08-04 22:37:11 +02001553
Bram Moolenaare7cda972022-08-29 11:02:59 +01001554 autocmd! test_bufleavewithdelete BufLeave XbufLeave1
Bram Moolenaar4a137b42017-08-04 22:37:11 +02001555 augroup! test_bufleavewithdelete
1556
1557 new
Bram Moolenaare7cda972022-08-29 11:02:59 +01001558 bwipe! XbufLeave1
Bram Moolenaar4a137b42017-08-04 22:37:11 +02001559endfunc
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +02001560
1561" Test for autocommand that changes the buffer list, when doing ":ball".
1562func Test_Acmd_BufAll()
1563 enew!
1564 %bwipe!
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01001565 call writefile(['Test file Xxx1'], 'Xxx1', 'D')
1566 call writefile(['Test file Xxx2'], 'Xxx2', 'D')
1567 call writefile(['Test file Xxx3'], 'Xxx3', 'D')
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +02001568
1569 " Add three files to the buffer list
1570 split Xxx1
1571 close
1572 split Xxx2
1573 close
1574 split Xxx3
1575 close
1576
1577 " Wipe the buffer when the buffer is opened
1578 au BufReadPost Xxx2 bwipe
1579
1580 call append(0, 'Test file Xxx4')
1581 ball
1582
1583 call assert_equal(2, winnr('$'))
1584 call assert_equal('Xxx1', bufname(winbufnr(winnr('$'))))
1585 wincmd t
1586
1587 au! BufReadPost
1588 %bwipe!
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +02001589 enew! | only
1590endfunc
1591
1592" Test for autocommand that changes current buffer on BufEnter event.
1593" Check if modelines are interpreted for the correct buffer.
1594func Test_Acmd_BufEnter()
1595 %bwipe!
1596 call writefile(['start of test file Xxx1',
1597 \ "\<Tab>this is a test",
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01001598 \ 'end of test file Xxx1'], 'Xxx1', 'D')
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +02001599 call writefile(['start of test file Xxx2',
1600 \ 'vim: set noai :',
1601 \ "\<Tab>this is a test",
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01001602 \ 'end of test file Xxx2'], 'Xxx2', 'D')
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +02001603
1604 au BufEnter Xxx2 brew
1605 set ai modeline modelines=3
1606 edit Xxx1
1607 " edit Xxx2, autocmd will do :brew
1608 edit Xxx2
1609 exe "normal G?this is a\<CR>"
1610 " Append text with autoindent to this file
1611 normal othis should be auto-indented
1612 call assert_equal("\<Tab>this should be auto-indented", getline('.'))
1613 call assert_equal(3, line('.'))
1614 " Remove autocmd and edit Xxx2 again
1615 au! BufEnter Xxx2
1616 buf! Xxx2
1617 exe "normal G?this is a\<CR>"
1618 " append text without autoindent to Xxx
1619 normal othis should be in column 1
1620 call assert_equal("this should be in column 1", getline('.'))
1621 call assert_equal(4, line('.'))
1622
1623 %bwipe!
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +02001624 set ai&vim modeline&vim modelines&vim
1625endfunc
1626
1627" Test for issue #57
1628" do not move cursor on <c-o> when autoindent is set
1629func Test_ai_CTRL_O()
1630 enew!
1631 set ai
1632 let save_fo = &fo
1633 set fo+=r
1634 exe "normal o# abcdef\<Esc>2hi\<CR>\<C-O>d0\<Esc>"
1635 exe "normal o# abcdef\<Esc>2hi\<C-O>d0\<Esc>"
1636 call assert_equal(['# abc', 'def', 'def'], getline(2, 4))
1637
1638 set ai&vim
1639 let &fo = save_fo
1640 enew!
1641endfunc
1642
1643" Test for autocommand that deletes the current buffer on BufLeave event.
1644" Also test deleting the last buffer, should give a new, empty buffer.
1645func Test_BufLeave_Wipe()
1646 %bwipe!
1647 let content = ['start of test file Xxx',
1648 \ 'this is a test',
1649 \ 'end of test file Xxx']
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01001650 call writefile(content, 'Xxx1', 'D')
1651 call writefile(content, 'Xxx2', 'D')
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +02001652
1653 au BufLeave Xxx2 bwipe
1654 edit Xxx1
1655 split Xxx2
1656 " delete buffer Xxx2, we should be back to Xxx1
1657 bwipe
1658 call assert_equal('Xxx1', bufname('%'))
1659 call assert_equal(1, winnr('$'))
1660
1661 " Create an alternate buffer
1662 %write! test.out
1663 call assert_equal('test.out', bufname('#'))
1664 " delete alternate buffer
1665 bwipe test.out
1666 call assert_equal('Xxx1', bufname('%'))
1667 call assert_equal('', bufname('#'))
1668
1669 au BufLeave Xxx1 bwipe
1670 " delete current buffer, get an empty one
1671 bwipe!
1672 call assert_equal(1, line('$'))
1673 call assert_equal('', bufname('%'))
Bram Moolenaarb2c87502017-10-14 21:15:58 +02001674 let g:bufinfo = getbufinfo()
1675 call assert_equal(1, len(g:bufinfo))
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +02001676
Bram Moolenaar53f0c962017-10-22 14:23:59 +02001677 call delete('test.out')
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +02001678 %bwipe
1679 au! BufLeave
Bram Moolenaarb2c87502017-10-14 21:15:58 +02001680
1681 " check that bufinfo doesn't contain a pointer to freed memory
1682 call test_garbagecollect_now()
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +02001683endfunc
Bram Moolenaar87ffb5c2017-10-19 12:37:42 +02001684
1685func Test_QuitPre()
1686 edit Xfoo
1687 let winid = win_getid(winnr())
1688 split Xbar
1689 au! QuitPre * let g:afile = expand('<afile>')
1690 " Close the other window, <afile> should be correct.
1691 exe win_id2win(winid) . 'q'
1692 call assert_equal('Xfoo', g:afile)
LemonBoy66e13ae2022-04-21 22:52:11 +01001693
Bram Moolenaar87ffb5c2017-10-19 12:37:42 +02001694 unlet g:afile
1695 bwipe Xfoo
1696 bwipe Xbar
1697endfunc
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02001698
1699func Test_Cmdline()
Bram Moolenaar153b7042018-01-31 15:48:32 +01001700 au! CmdlineChanged : let g:text = getcmdline()
1701 let g:text = 0
1702 call feedkeys(":echom 'hello'\<CR>", 'xt')
1703 call assert_equal("echom 'hello'", g:text)
1704 au! CmdlineChanged
1705
1706 au! CmdlineChanged : let g:entered = expand('<afile>')
1707 let g:entered = 0
1708 call feedkeys(":echom 'hello'\<CR>", 'xt')
1709 call assert_equal(':', g:entered)
1710 au! CmdlineChanged
1711
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02001712 au! CmdlineEnter : let g:entered = expand('<afile>')
1713 au! CmdlineLeave : let g:left = expand('<afile>')
1714 let g:entered = 0
1715 let g:left = 0
1716 call feedkeys(":echo 'hello'\<CR>", 'xt')
1717 call assert_equal(':', g:entered)
1718 call assert_equal(':', g:left)
1719 au! CmdlineEnter
1720 au! CmdlineLeave
1721
Bram Moolenaara4baf5b2018-04-22 13:27:44 +02001722 let save_shellslash = &shellslash
1723 set noshellslash
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02001724 au! CmdlineEnter / let g:entered = expand('<afile>')
1725 au! CmdlineLeave / let g:left = expand('<afile>')
1726 let g:entered = 0
1727 let g:left = 0
Bram Moolenaar53f0c962017-10-22 14:23:59 +02001728 new
1729 call setline(1, 'hello')
1730 call feedkeys("/hello\<CR>", 'xt')
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02001731 call assert_equal('/', g:entered)
1732 call assert_equal('/', g:left)
Bram Moolenaar53f0c962017-10-22 14:23:59 +02001733 bwipe!
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02001734 au! CmdlineEnter
1735 au! CmdlineLeave
Bram Moolenaara4baf5b2018-04-22 13:27:44 +02001736 let &shellslash = save_shellslash
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02001737endfunc
Bram Moolenaar53f0c962017-10-22 14:23:59 +02001738
1739" Test for BufWritePre autocommand that deletes or unloads the buffer.
1740func Test_BufWritePre()
1741 %bwipe
1742 au BufWritePre Xxx1 bunload
1743 au BufWritePre Xxx2 bwipe
1744
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01001745 call writefile(['start of Xxx1', 'test', 'end of Xxx1'], 'Xxx1', 'D')
1746 call writefile(['start of Xxx2', 'test', 'end of Xxx2'], 'Xxx2', 'D')
Bram Moolenaar53f0c962017-10-22 14:23:59 +02001747
1748 edit Xtest
1749 e! Xxx2
1750 bdel Xtest
1751 e Xxx1
1752 " write it, will unload it and give an error msg
Bram Moolenaare2e40752020-09-04 21:18:46 +02001753 call assert_fails('w', 'E203:')
Bram Moolenaar53f0c962017-10-22 14:23:59 +02001754 call assert_equal('Xxx2', bufname('%'))
1755 edit Xtest
1756 e! Xxx2
1757 bwipe Xtest
1758 " write it, will delete the buffer and give an error msg
Bram Moolenaare2e40752020-09-04 21:18:46 +02001759 call assert_fails('w', 'E203:')
Bram Moolenaar53f0c962017-10-22 14:23:59 +02001760 call assert_equal('Xxx1', bufname('%'))
1761 au! BufWritePre
Bram Moolenaar53f0c962017-10-22 14:23:59 +02001762endfunc
1763
1764" Test for BufUnload autocommand that unloads all the other buffers
1765func Test_bufunload_all()
Bram Moolenaarf08b0eb2021-10-16 13:00:14 +01001766 let g:test_is_flaky = 1
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01001767 call writefile(['Test file Xxx1'], 'Xxx1', 'D')"
1768 call writefile(['Test file Xxx2'], 'Xxx2', 'D')"
Bram Moolenaar53f0c962017-10-22 14:23:59 +02001769
Bram Moolenaarc79745a2019-05-20 22:12:34 +02001770 let content =<< trim [CODE]
1771 func UnloadAllBufs()
1772 let i = 1
1773 while i <= bufnr('$')
1774 if i != bufnr('%') && bufloaded(i)
1775 exe i . 'bunload'
1776 endif
1777 let i += 1
1778 endwhile
1779 endfunc
1780 au BufUnload * call UnloadAllBufs()
1781 au VimLeave * call writefile(['Test Finished'], 'Xout')
1782 edit Xxx1
1783 split Xxx2
1784 q
1785 [CODE]
1786
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01001787 call writefile(content, 'Xbunloadtest', 'D')
Bram Moolenaar53f0c962017-10-22 14:23:59 +02001788
1789 call delete('Xout')
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01001790 call system(GetVimCommandClean() .. ' -N --not-a-term -S Xbunloadtest')
Bram Moolenaar53f0c962017-10-22 14:23:59 +02001791 call assert_true(filereadable('Xout'))
1792
Bram Moolenaar53f0c962017-10-22 14:23:59 +02001793 call delete('Xout')
1794endfunc
1795
1796" Some tests for buffer-local autocommands
1797func Test_buflocal_autocmd()
1798 let g:bname = ''
1799 edit xx
1800 au BufLeave <buffer> let g:bname = expand("%")
1801 " here, autocommand for xx should trigger.
1802 " but autocommand shall not apply to buffer named <buffer>.
1803 edit somefile
1804 call assert_equal('xx', g:bname)
1805 let g:bname = ''
1806 " here, autocommand shall be auto-deleted
1807 bwipe xx
1808 " autocmd should not trigger
1809 edit xx
1810 call assert_equal('', g:bname)
1811 " autocmd should not trigger
1812 edit somefile
1813 call assert_equal('', g:bname)
1814 enew
1815 unlet g:bname
1816endfunc
Bram Moolenaar430dc5d2017-11-02 21:04:47 +01001817
1818" Test for "*Cmd" autocommands
1819func Test_Cmd_Autocmds()
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01001820 call writefile(['start of Xxx', "\tabc2", 'end of Xxx'], 'Xxx', 'D')
Bram Moolenaar430dc5d2017-11-02 21:04:47 +01001821
1822 enew!
1823 au BufReadCmd XtestA 0r Xxx|$del
1824 edit XtestA " will read text of Xxd instead
1825 call assert_equal('start of Xxx', getline(1))
1826
1827 au BufWriteCmd XtestA call append(line("$"), "write")
1828 write " will append a line to the file
1829 call assert_equal('write', getline('$'))
Bram Moolenaare2e40752020-09-04 21:18:46 +02001830 call assert_fails('read XtestA', 'E484:') " should not read anything
Bram Moolenaar430dc5d2017-11-02 21:04:47 +01001831 call assert_equal('write', getline(4))
1832
1833 " now we have:
1834 " 1 start of Xxx
1835 " 2 abc2
1836 " 3 end of Xxx
1837 " 4 write
1838
1839 au FileReadCmd XtestB '[r Xxx
1840 2r XtestB " will read Xxx below line 2 instead
1841 call assert_equal('start of Xxx', getline(3))
1842
1843 " now we have:
1844 " 1 start of Xxx
1845 " 2 abc2
1846 " 3 start of Xxx
1847 " 4 abc2
1848 " 5 end of Xxx
1849 " 6 end of Xxx
1850 " 7 write
1851
1852 au FileWriteCmd XtestC '[,']copy $
1853 normal 4GA1
1854 4,5w XtestC " will copy lines 4 and 5 to the end
1855 call assert_equal("\tabc21", getline(8))
Bram Moolenaare2e40752020-09-04 21:18:46 +02001856 call assert_fails('r XtestC', 'E484:') " should not read anything
Bram Moolenaar430dc5d2017-11-02 21:04:47 +01001857 call assert_equal("end of Xxx", getline(9))
1858
1859 " now we have:
1860 " 1 start of Xxx
1861 " 2 abc2
1862 " 3 start of Xxx
1863 " 4 abc21
1864 " 5 end of Xxx
1865 " 6 end of Xxx
1866 " 7 write
1867 " 8 abc21
1868 " 9 end of Xxx
1869
1870 let g:lines = []
1871 au FileAppendCmd XtestD call extend(g:lines, getline(line("'["), line("']")))
1872 w >>XtestD " will add lines to 'lines'
1873 call assert_equal(9, len(g:lines))
Bram Moolenaare2e40752020-09-04 21:18:46 +02001874 call assert_fails('$r XtestD', 'E484:') " should not read anything
Bram Moolenaar430dc5d2017-11-02 21:04:47 +01001875 call assert_equal(9, line('$'))
1876 call assert_equal('end of Xxx', getline('$'))
1877
1878 au BufReadCmd XtestE 0r Xxx|$del
1879 sp XtestE " split window with test.out
1880 call assert_equal('end of Xxx', getline(3))
1881
1882 let g:lines = []
1883 exe "normal 2Goasdf\<Esc>\<C-W>\<C-W>"
1884 au BufWriteCmd XtestE call extend(g:lines, getline(0, '$'))
1885 wall " will write other window to 'lines'
1886 call assert_equal(4, len(g:lines), g:lines)
1887 call assert_equal('asdf', g:lines[2])
1888
1889 au! BufReadCmd
1890 au! BufWriteCmd
1891 au! FileReadCmd
1892 au! FileWriteCmd
1893 au! FileAppendCmd
1894 %bwipe!
Bram Moolenaar430dc5d2017-11-02 21:04:47 +01001895 enew!
1896endfunc
Bram Moolenaaraace2152017-11-05 16:23:10 +01001897
Bram Moolenaar0fff4412020-03-29 16:06:29 +02001898func s:ReadFile()
1899 setl noswapfile nomodified
1900 let filename = resolve(expand("<afile>:p"))
1901 execute 'read' fnameescape(filename)
1902 1d_
1903 exe 'file' fnameescape(filename)
1904 setl buftype=acwrite
1905endfunc
1906
1907func s:WriteFile()
1908 let filename = resolve(expand("<afile>:p"))
1909 setl buftype=
1910 noautocmd execute 'write' fnameescape(filename)
1911 setl buftype=acwrite
1912 setl nomodified
1913endfunc
1914
1915func Test_BufReadCmd()
1916 autocmd BufReadCmd *.test call s:ReadFile()
1917 autocmd BufWriteCmd *.test call s:WriteFile()
1918
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01001919 call writefile(['one', 'two', 'three'], 'Xcmd.test', 'D')
Bram Moolenaar0fff4412020-03-29 16:06:29 +02001920 edit Xcmd.test
1921 call assert_match('Xcmd.test" line 1 of 3', execute('file'))
1922 normal! Gofour
1923 write
1924 call assert_equal(['one', 'two', 'three', 'four'], readfile('Xcmd.test'))
1925
1926 bwipe!
Bram Moolenaar0fff4412020-03-29 16:06:29 +02001927 au! BufReadCmd
1928 au! BufWriteCmd
1929endfunc
1930
zeertzjq9c8f9462022-08-30 18:17:15 +01001931func Test_BufWriteCmd()
1932 autocmd BufWriteCmd Xbufwritecmd let g:written = 1
1933 new
1934 file Xbufwritecmd
1935 set buftype=acwrite
Bram Moolenaar6f14da12022-09-07 21:30:44 +01001936 call mkdir('Xbufwritecmd', 'D')
zeertzjq9c8f9462022-08-30 18:17:15 +01001937 write
1938 " BufWriteCmd should be triggered even if a directory has the same name
1939 call assert_equal(1, g:written)
zeertzjq9c8f9462022-08-30 18:17:15 +01001940 unlet g:written
1941 au! BufWriteCmd
1942 bwipe!
1943endfunc
1944
Bram Moolenaaraace2152017-11-05 16:23:10 +01001945func SetChangeMarks(start, end)
Bram Moolenaar97c69432021-01-15 16:45:21 +01001946 exe a:start .. 'mark ['
1947 exe a:end .. 'mark ]'
Bram Moolenaaraace2152017-11-05 16:23:10 +01001948endfunc
1949
1950" Verify the effects of autocmds on '[ and ']
1951func Test_change_mark_in_autocmds()
1952 edit! Xtest
Bram Moolenaar97c69432021-01-15 16:45:21 +01001953 call feedkeys("ia\<CR>b\<CR>c\<CR>d\<C-g>u\<Esc>", 'xtn')
Bram Moolenaaraace2152017-11-05 16:23:10 +01001954
1955 call SetChangeMarks(2, 3)
1956 write
1957 call assert_equal([1, 4], [line("'["), line("']")])
1958
1959 call SetChangeMarks(2, 3)
1960 au BufWritePre * call assert_equal([1, 4], [line("'["), line("']")])
1961 write
1962 au! BufWritePre
1963
Bram Moolenaar14ddd222020-08-05 12:02:40 +02001964 if has('unix')
Bram Moolenaaraace2152017-11-05 16:23:10 +01001965 write XtestFilter
1966 write >> XtestFilter
1967
1968 call SetChangeMarks(2, 3)
1969 " Marks are set to the entire range of the write
1970 au FilterWritePre * call assert_equal([1, 4], [line("'["), line("']")])
1971 " '[ is adjusted to just before the line that will receive the filtered
1972 " data
1973 au FilterReadPre * call assert_equal([4, 4], [line("'["), line("']")])
1974 " The filtered data is read into the buffer, and the source lines are
1975 " still present, so the range is after the source lines
1976 au FilterReadPost * call assert_equal([5, 12], [line("'["), line("']")])
1977 %!cat XtestFilter
1978 " After the filtered data is read, the original lines are deleted
1979 call assert_equal([1, 8], [line("'["), line("']")])
1980 au! FilterWritePre,FilterReadPre,FilterReadPost
1981 undo
1982
1983 call SetChangeMarks(1, 4)
1984 au FilterWritePre * call assert_equal([2, 3], [line("'["), line("']")])
1985 au FilterReadPre * call assert_equal([3, 3], [line("'["), line("']")])
1986 au FilterReadPost * call assert_equal([4, 11], [line("'["), line("']")])
1987 2,3!cat XtestFilter
1988 call assert_equal([2, 9], [line("'["), line("']")])
1989 au! FilterWritePre,FilterReadPre,FilterReadPost
1990 undo
1991
1992 call delete('XtestFilter')
1993 endif
1994
1995 call SetChangeMarks(1, 4)
1996 au FileWritePre * call assert_equal([2, 3], [line("'["), line("']")])
1997 2,3write Xtest2
1998 au! FileWritePre
1999
2000 call SetChangeMarks(2, 3)
2001 au FileAppendPre * call assert_equal([1, 4], [line("'["), line("']")])
2002 write >> Xtest2
2003 au! FileAppendPre
2004
2005 call SetChangeMarks(1, 4)
2006 au FileAppendPre * call assert_equal([2, 3], [line("'["), line("']")])
2007 2,3write >> Xtest2
2008 au! FileAppendPre
2009
2010 call SetChangeMarks(1, 1)
2011 au FileReadPre * call assert_equal([3, 1], [line("'["), line("']")])
2012 au FileReadPost * call assert_equal([4, 11], [line("'["), line("']")])
2013 3read Xtest2
2014 au! FileReadPre,FileReadPost
2015 undo
2016
2017 call SetChangeMarks(4, 4)
2018 " When the line is 0, it's adjusted to 1
2019 au FileReadPre * call assert_equal([1, 4], [line("'["), line("']")])
2020 au FileReadPost * call assert_equal([1, 8], [line("'["), line("']")])
2021 0read Xtest2
2022 au! FileReadPre,FileReadPost
2023 undo
2024
2025 call SetChangeMarks(4, 4)
2026 " When the line is 0, it's adjusted to 1
2027 au FileReadPre * call assert_equal([1, 4], [line("'["), line("']")])
2028 au FileReadPost * call assert_equal([2, 9], [line("'["), line("']")])
2029 1read Xtest2
2030 au! FileReadPre,FileReadPost
2031 undo
2032
2033 bwipe!
2034 call delete('Xtest')
2035 call delete('Xtest2')
2036endfunc
2037
2038func Test_Filter_noshelltemp()
Bram Moolenaaraeb313f2020-11-27 19:13:28 +01002039 CheckExecutable cat
Bram Moolenaaraace2152017-11-05 16:23:10 +01002040
2041 enew!
2042 call setline(1, ['a', 'b', 'c', 'd'])
2043
2044 let shelltemp = &shelltemp
2045 set shelltemp
2046
2047 let g:filter_au = 0
2048 au FilterWritePre * let g:filter_au += 1
2049 au FilterReadPre * let g:filter_au += 1
2050 au FilterReadPost * let g:filter_au += 1
2051 %!cat
2052 call assert_equal(3, g:filter_au)
2053
2054 if has('filterpipe')
2055 set noshelltemp
2056
2057 let g:filter_au = 0
2058 au FilterWritePre * let g:filter_au += 1
2059 au FilterReadPre * let g:filter_au += 1
2060 au FilterReadPost * let g:filter_au += 1
2061 %!cat
2062 call assert_equal(0, g:filter_au)
2063 endif
2064
2065 au! FilterWritePre,FilterReadPre,FilterReadPost
2066 let &shelltemp = shelltemp
2067 bwipe!
2068endfunc
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01002069
2070func Test_TextYankPost()
2071 enew!
2072 call setline(1, ['foo'])
2073
2074 let g:event = []
2075 au TextYankPost * let g:event = copy(v:event)
2076
2077 call assert_equal({}, v:event)
2078 call assert_fails('let v:event = {}', 'E46:')
2079 call assert_fails('let v:event.mykey = 0', 'E742:')
2080
2081 norm "ayiw
2082 call assert_equal(
Bram Moolenaara016eeb2022-04-09 11:37:38 +01002083 \ #{regcontents: ['foo'], regname: 'a', operator: 'y',
2084 \ regtype: 'v', visual: v:false, inclusive: v:true},
2085 \ g:event)
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01002086 norm y_
2087 call assert_equal(
Bram Moolenaara016eeb2022-04-09 11:37:38 +01002088 \ #{regcontents: ['foo'], regname: '', operator: 'y', regtype: 'V',
2089 \ visual: v:false, inclusive: v:false},
2090 \ g:event)
Bram Moolenaar37d16732020-06-12 22:09:01 +02002091 norm Vy
2092 call assert_equal(
Bram Moolenaara016eeb2022-04-09 11:37:38 +01002093 \ #{regcontents: ['foo'], regname: '', operator: 'y', regtype: 'V',
2094 \ visual: v:true, inclusive: v:true},
2095 \ g:event)
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01002096 call feedkeys("\<C-V>y", 'x')
2097 call assert_equal(
Bram Moolenaara016eeb2022-04-09 11:37:38 +01002098 \ #{regcontents: ['f'], regname: '', operator: 'y', regtype: "\x161",
2099 \ visual: v:true, inclusive: v:true},
2100 \ g:event)
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01002101 norm "xciwbar
2102 call assert_equal(
Bram Moolenaara016eeb2022-04-09 11:37:38 +01002103 \ #{regcontents: ['foo'], regname: 'x', operator: 'c', regtype: 'v',
2104 \ visual: v:false, inclusive: v:true},
2105 \ g:event)
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01002106 norm "bdiw
2107 call assert_equal(
Bram Moolenaara016eeb2022-04-09 11:37:38 +01002108 \ #{regcontents: ['bar'], regname: 'b', operator: 'd', regtype: 'v',
2109 \ visual: v:false, inclusive: v:true},
2110 \ g:event)
2111
2112 call setline(1, 'foobar')
2113 " exclusive motion
2114 norm $"ay0
2115 call assert_equal(
2116 \ #{regcontents: ['fooba'], regname: 'a', operator: 'y', regtype: 'v',
2117 \ visual: v:false, inclusive: v:false},
2118 \ g:event)
2119 " inclusive motion
2120 norm 0"ay$
2121 call assert_equal(
2122 \ #{regcontents: ['foobar'], regname: 'a', operator: 'y', regtype: 'v',
2123 \ visual: v:false, inclusive: v:true},
2124 \ g:event)
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01002125
2126 call assert_equal({}, v:event)
2127
Bram Moolenaarfccbf062020-11-26 20:34:00 +01002128 if has('clipboard_working') && !has('gui_running')
2129 " Test that when the visual selection is automatically copied to clipboard
2130 " register a TextYankPost is emitted
2131 call setline(1, ['foobar'])
2132
2133 let @* = ''
2134 set clipboard=autoselect
2135 exe "norm! ggviw\<Esc>"
2136 call assert_equal(
Bram Moolenaara016eeb2022-04-09 11:37:38 +01002137 \ #{regcontents: ['foobar'], regname: '*', operator: 'y',
2138 \ regtype: 'v', visual: v:true, inclusive: v:false},
2139 \ g:event)
Bram Moolenaarfccbf062020-11-26 20:34:00 +01002140
2141 let @+ = ''
2142 set clipboard=autoselectplus
2143 exe "norm! ggviw\<Esc>"
2144 call assert_equal(
Bram Moolenaara016eeb2022-04-09 11:37:38 +01002145 \ #{regcontents: ['foobar'], regname: '+', operator: 'y',
2146 \ regtype: 'v', visual: v:true, inclusive: v:false},
2147 \ g:event)
Bram Moolenaarfccbf062020-11-26 20:34:00 +01002148
2149 set clipboard&vim
2150 endif
2151
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01002152 au! TextYankPost
2153 unlet g:event
2154 bwipe!
2155endfunc
Bram Moolenaar9bca8052017-12-18 12:37:55 +01002156
Bram Moolenaar9a046fd2021-01-28 13:47:59 +01002157func Test_autocommand_all_events()
2158 call assert_fails('au * * bwipe', 'E1155:')
2159 call assert_fails('au * x bwipe', 'E1155:')
Bram Moolenaarb6db1462021-12-24 19:24:47 +00002160 call assert_fails('au! * x bwipe', 'E1155:')
Bram Moolenaar4fb921e2017-12-18 15:33:00 +01002161endfunc
Bram Moolenaarb7407d32018-02-03 17:36:27 +01002162
Bram Moolenaarf6246f52022-02-11 16:30:12 +00002163func Test_autocmd_user()
2164 au User MyEvent let s:res = [expand("<afile>"), expand("<amatch>")]
2165 doautocmd User MyEvent
2166 call assert_equal(['MyEvent', 'MyEvent'], s:res)
2167 au! User
2168 unlet s:res
2169endfunc
2170
Bram Moolenaar3b014be2022-11-13 17:53:46 +00002171func Test_autocmd_user_clear_group()
2172 CheckRunVimInTerminal
2173
2174 let lines =<< trim END
2175 autocmd! User
2176 for i in range(1, 999)
2177 exe 'autocmd User ' .. 'Foo' .. i .. ' bar'
2178 endfor
2179 au CmdlineLeave : call timer_start(0, {-> execute('autocmd! User')})
2180 END
2181 call writefile(lines, 'XautoUser', 'D')
2182 let buf = RunVimInTerminal('-S XautoUser', {'rows': 10})
2183
2184 " this was using freed memory
2185 call term_sendkeys(buf, ":autocmd User\<CR>")
2186 call TermWait(buf, 50)
2187 call term_sendkeys(buf, "G")
2188
2189 call StopVimInTerminal(buf)
2190endfunc
2191
Bram Moolenaarb7407d32018-02-03 17:36:27 +01002192function s:Before_test_dirchanged()
2193 augroup test_dirchanged
2194 autocmd!
2195 augroup END
2196 let s:li = []
2197 let s:dir_this = getcwd()
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02002198 let s:dir_foo = s:dir_this . '/Xfoo'
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01002199 call mkdir(s:dir_foo)
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02002200 let s:dir_bar = s:dir_this . '/Xbar'
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01002201 call mkdir(s:dir_bar)
Bram Moolenaarb7407d32018-02-03 17:36:27 +01002202endfunc
2203
2204function s:After_test_dirchanged()
Bram Moolenaar3503d7c2019-11-09 20:10:17 +01002205 call chdir(s:dir_this)
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01002206 call delete(s:dir_foo, 'd')
2207 call delete(s:dir_bar, 'd')
Bram Moolenaarb7407d32018-02-03 17:36:27 +01002208 augroup test_dirchanged
2209 autocmd!
2210 augroup END
2211endfunc
2212
2213function Test_dirchanged_global()
2214 call s:Before_test_dirchanged()
Bram Moolenaarf6246f52022-02-11 16:30:12 +00002215 autocmd test_dirchanged DirChangedPre global call add(s:li, expand("<amatch>") .. " pre cd " .. v:event.directory)
Bram Moolenaarb7407d32018-02-03 17:36:27 +01002216 autocmd test_dirchanged DirChanged global call add(s:li, "cd:")
2217 autocmd test_dirchanged DirChanged global call add(s:li, expand("<afile>"))
Bram Moolenaar3503d7c2019-11-09 20:10:17 +01002218 call chdir(s:dir_foo)
Bram Moolenaarf6246f52022-02-11 16:30:12 +00002219 let expected = ["global pre cd " .. s:dir_foo, "cd:", s:dir_foo]
Bram Moolenaar28e8f732022-02-09 12:58:20 +00002220 call assert_equal(expected, s:li)
Bram Moolenaar3503d7c2019-11-09 20:10:17 +01002221 call chdir(s:dir_foo)
Bram Moolenaar28e8f732022-02-09 12:58:20 +00002222 call assert_equal(expected, s:li)
Bram Moolenaar3503d7c2019-11-09 20:10:17 +01002223 exe 'lcd ' .. fnameescape(s:dir_bar)
Bram Moolenaar28e8f732022-02-09 12:58:20 +00002224 call assert_equal(expected, s:li)
Bram Moolenaard8c9d322022-06-12 11:49:16 +01002225
2226 exe 'cd ' .. s:dir_foo
2227 exe 'cd ' .. s:dir_bar
2228 autocmd! test_dirchanged DirChanged global let g:result = expand("<afile>")
2229 cd -
Bram Moolenaardb77c492022-06-12 23:26:50 +01002230 call assert_equal(s:dir_foo, substitute(g:result, '\\', '/', 'g'))
Bram Moolenaard8c9d322022-06-12 11:49:16 +01002231
Bram Moolenaarb7407d32018-02-03 17:36:27 +01002232 call s:After_test_dirchanged()
2233endfunc
2234
2235function Test_dirchanged_local()
2236 call s:Before_test_dirchanged()
2237 autocmd test_dirchanged DirChanged window call add(s:li, "lcd:")
2238 autocmd test_dirchanged DirChanged window call add(s:li, expand("<afile>"))
Bram Moolenaar3503d7c2019-11-09 20:10:17 +01002239 call chdir(s:dir_foo)
Bram Moolenaarb7407d32018-02-03 17:36:27 +01002240 call assert_equal([], s:li)
Bram Moolenaar3503d7c2019-11-09 20:10:17 +01002241 exe 'lcd ' .. fnameescape(s:dir_bar)
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01002242 call assert_equal(["lcd:", s:dir_bar], s:li)
Bram Moolenaar3503d7c2019-11-09 20:10:17 +01002243 exe 'lcd ' .. fnameescape(s:dir_bar)
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01002244 call assert_equal(["lcd:", s:dir_bar], s:li)
Bram Moolenaarb7407d32018-02-03 17:36:27 +01002245 call s:After_test_dirchanged()
2246endfunc
2247
2248function Test_dirchanged_auto()
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +02002249 CheckOption autochdir
Bram Moolenaarb7407d32018-02-03 17:36:27 +01002250 call s:Before_test_dirchanged()
2251 call test_autochdir()
Bram Moolenaar28e8f732022-02-09 12:58:20 +00002252 autocmd test_dirchanged DirChangedPre auto call add(s:li, "pre cd " .. v:event.directory)
Bram Moolenaarb7407d32018-02-03 17:36:27 +01002253 autocmd test_dirchanged DirChanged auto call add(s:li, "auto:")
2254 autocmd test_dirchanged DirChanged auto call add(s:li, expand("<afile>"))
2255 set acd
Bram Moolenaar3503d7c2019-11-09 20:10:17 +01002256 cd ..
Bram Moolenaarb7407d32018-02-03 17:36:27 +01002257 call assert_equal([], s:li)
Bram Moolenaar61abe7d2022-08-30 21:46:08 +01002258 exe 'edit ' . s:dir_foo . '/Xautofile'
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01002259 call assert_equal(s:dir_foo, getcwd())
Bram Moolenaar28e8f732022-02-09 12:58:20 +00002260 let expected = ["pre cd " .. s:dir_foo, "auto:", s:dir_foo]
2261 call assert_equal(expected, s:li)
Bram Moolenaarb7407d32018-02-03 17:36:27 +01002262 set noacd
2263 bwipe!
2264 call s:After_test_dirchanged()
2265endfunc
Bram Moolenaar5a093432018-02-10 18:15:19 +01002266
2267" Test TextChangedI and TextChangedP
2268func Test_ChangedP()
2269 new
2270 call setline(1, ['foo', 'bar', 'foobar'])
2271 call test_override("char_avail", 1)
2272 set complete=. completeopt=menuone
2273
2274 func! TextChangedAutocmd(char)
2275 let g:autocmd .= a:char
2276 endfunc
2277
Christian Brabandtdb3b4462021-10-16 11:58:55 +01002278 " TextChanged will not be triggered, only check that it isn't.
Bram Moolenaar5a093432018-02-10 18:15:19 +01002279 au! TextChanged <buffer> :call TextChangedAutocmd('N')
2280 au! TextChangedI <buffer> :call TextChangedAutocmd('I')
2281 au! TextChangedP <buffer> :call TextChangedAutocmd('P')
2282
2283 call cursor(3, 1)
2284 let g:autocmd = ''
2285 call feedkeys("o\<esc>", 'tnix')
2286 call assert_equal('I', g:autocmd)
2287
2288 let g:autocmd = ''
2289 call feedkeys("Sf", 'tnix')
2290 call assert_equal('II', g:autocmd)
2291
2292 let g:autocmd = ''
2293 call feedkeys("Sf\<C-N>", 'tnix')
2294 call assert_equal('IIP', g:autocmd)
2295
2296 let g:autocmd = ''
2297 call feedkeys("Sf\<C-N>\<C-N>", 'tnix')
2298 call assert_equal('IIPP', g:autocmd)
2299
2300 let g:autocmd = ''
2301 call feedkeys("Sf\<C-N>\<C-N>\<C-N>", 'tnix')
2302 call assert_equal('IIPPP', g:autocmd)
2303
2304 let g:autocmd = ''
2305 call feedkeys("Sf\<C-N>\<C-N>\<C-N>\<C-N>", 'tnix')
2306 call assert_equal('IIPPPP', g:autocmd)
2307
2308 call assert_equal(['foo', 'bar', 'foobar', 'foo'], getline(1, '$'))
2309 " TODO: how should it handle completeopt=noinsert,noselect?
2310
2311 " CleanUp
2312 call test_override("char_avail", 0)
2313 au! TextChanged
2314 au! TextChangedI
2315 au! TextChangedP
2316 delfu TextChangedAutocmd
2317 unlet! g:autocmd
2318 set complete&vim completeopt&vim
2319
2320 bw!
2321endfunc
Bram Moolenaar8c64a362018-03-23 22:39:31 +01002322
Bram Moolenaar91d2e782018-08-07 19:05:01 +02002323let g:setline_handled = v:false
Bram Moolenaar1e115362019-01-09 23:01:02 +01002324func SetLineOne()
Bram Moolenaar91d2e782018-08-07 19:05:01 +02002325 if !g:setline_handled
2326 call setline(1, "(x)")
2327 let g:setline_handled = v:true
2328 endif
2329endfunc
2330
2331func Test_TextChangedI_with_setline()
2332 new
2333 call test_override('char_avail', 1)
2334 autocmd TextChangedI <buffer> call SetLineOne()
2335 call feedkeys("i(\<CR>\<Esc>", 'tx')
2336 call assert_equal('(', getline(1))
2337 call assert_equal('x)', getline(2))
2338 undo
Bram Moolenaar91d2e782018-08-07 19:05:01 +02002339 call assert_equal('', getline(1))
Bram Moolenaar9fa95062018-08-08 22:08:32 +02002340 call assert_equal('', getline(2))
Bram Moolenaar91d2e782018-08-07 19:05:01 +02002341
Bram Moolenaarca34db32022-01-20 11:17:18 +00002342 call test_override('char_avail', 0)
Bram Moolenaar91d2e782018-08-07 19:05:01 +02002343 bwipe!
2344endfunc
2345
Bram Moolenaar8c64a362018-03-23 22:39:31 +01002346func Test_Changed_FirstTime()
Bram Moolenaar8c5a2782019-08-07 23:07:07 +02002347 CheckFeature terminal
2348 CheckNotGui
Bram Moolenaar3cdcb092020-03-18 19:18:10 +01002349 " Starting a terminal to run Vim is always considered flaky.
Bram Moolenaar30d53e22020-03-18 21:10:44 +01002350 let g:test_is_flaky = 1
Bram Moolenaar8c5a2782019-08-07 23:07:07 +02002351
Bram Moolenaar8c64a362018-03-23 22:39:31 +01002352 " Prepare file for TextChanged event.
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01002353 call writefile([''], 'Xchanged.txt', 'D')
Bram Moolenaar8c64a362018-03-23 22:39:31 +01002354 let buf = term_start([GetVimProg(), '--clean', '-c', 'set noswapfile'], {'term_rows': 3})
2355 call assert_equal('running', term_getstatus(buf))
Bram Moolenaar1834d372018-03-29 17:40:46 +02002356 " Wait for the ruler (in the status line) to be shown.
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01002357 " In ConPTY, there is additional character which is drawn up to the width of
2358 " the screen.
2359 if has('conpty')
2360 call WaitForAssert({-> assert_match('\<All.*$', term_getline(buf, 3))})
2361 else
2362 call WaitForAssert({-> assert_match('\<All$', term_getline(buf, 3))})
2363 endif
Bram Moolenaar8c64a362018-03-23 22:39:31 +01002364 " It's only adding autocmd, so that no event occurs.
2365 call term_sendkeys(buf, ":au! TextChanged <buffer> call writefile(['No'], 'Xchanged.txt')\<cr>")
2366 call term_sendkeys(buf, "\<C-\\>\<C-N>:qa!\<cr>")
Bram Moolenaar50182fa2018-04-28 21:34:40 +02002367 call WaitForAssert({-> assert_equal('finished', term_getstatus(buf))})
Bram Moolenaar8c64a362018-03-23 22:39:31 +01002368 call assert_equal([''], readfile('Xchanged.txt'))
2369
2370 " clean up
Bram Moolenaar8c64a362018-03-23 22:39:31 +01002371 bwipe!
2372endfunc
Bram Moolenaar0566e892019-01-24 19:37:40 +01002373
Bram Moolenaareb93f3f2019-04-04 15:04:56 +02002374func Test_autocmd_nested()
2375 let g:did_nested = 0
2376 augroup Testing
2377 au WinNew * edit somefile
2378 au BufNew * let g:did_nested = 1
2379 augroup END
2380 split
2381 call assert_equal(0, g:did_nested)
2382 close
2383 bwipe! somefile
2384
2385 " old nested argument still works
2386 augroup Testing
2387 au!
2388 au WinNew * nested edit somefile
2389 au BufNew * let g:did_nested = 1
2390 augroup END
2391 split
2392 call assert_equal(1, g:did_nested)
2393 close
2394 bwipe! somefile
2395
2396 " New ++nested argument works
2397 augroup Testing
2398 au!
2399 au WinNew * ++nested edit somefile
2400 au BufNew * let g:did_nested = 1
2401 augroup END
2402 split
2403 call assert_equal(1, g:did_nested)
2404 close
2405 bwipe! somefile
2406
Bram Moolenaarf0775142022-03-04 20:10:38 +00002407 " nested without ++ does not work in Vim9 script
2408 call assert_fails('vim9cmd au WinNew * nested echo fails', 'E1078:')
2409
Bram Moolenaareb93f3f2019-04-04 15:04:56 +02002410 augroup Testing
2411 au!
2412 augroup END
2413
2414 call assert_fails('au WinNew * ++nested ++nested echo bad', 'E983:')
2415 call assert_fails('au WinNew * nested nested echo bad', 'E983:')
2416endfunc
2417
Bram Moolenaar5fa9f232022-07-23 09:06:48 +01002418func Test_autocmd_nested_cursor_invalid()
2419 set laststatus=0
2420 copen
2421 cclose
2422 call setline(1, ['foo', 'bar', 'baz'])
2423 3
2424 augroup nested_inv
2425 autocmd User foo ++nested copen
2426 autocmd BufAdd * let &laststatus = 2 - &laststatus
2427 augroup END
2428 doautocmd User foo
2429
2430 augroup nested_inv
2431 au!
2432 augroup END
2433 set laststatus&
Bram Moolenaarb03950f2022-07-26 13:47:13 +01002434 cclose
Bram Moolenaar5fa9f232022-07-23 09:06:48 +01002435 bwipe!
2436endfunc
2437
Bram Moolenaar3d6ee8b2022-07-27 15:23:35 +01002438func Test_autocmd_nested_keeps_cursor_pos()
2439 enew
2440 call setline(1, 'foo')
2441 autocmd User foo ++nested normal! $a
2442 autocmd InsertLeave * :
2443 doautocmd User foo
2444 call assert_equal([0, 1, 3, 0], getpos('.'))
2445
2446 bwipe!
2447endfunc
2448
Bram Moolenaarb03950f2022-07-26 13:47:13 +01002449func Test_autocmd_nested_switch_window()
2450 " run this in a separate Vim so that SafeState works
2451 CheckRunVimInTerminal
2452
2453 let lines =<< trim END
2454 vim9script
2455 ['()']->writefile('Xautofile')
2456 autocmd VimEnter * ++nested edit Xautofile | split
2457 autocmd BufReadPost * autocmd SafeState * ++once foldclosed('.')
2458 autocmd WinEnter * matchadd('ErrorMsg', 'pat')
2459 END
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01002460 call writefile(lines, 'Xautoscript', 'D')
Bram Moolenaarb03950f2022-07-26 13:47:13 +01002461 let buf = RunVimInTerminal('-S Xautoscript', {'rows': 10})
2462 call VerifyScreenDump(buf, 'Test_autocmd_nested_switch', {})
2463
2464 call StopVimInTerminal(buf)
2465 call delete('Xautofile')
Bram Moolenaarb03950f2022-07-26 13:47:13 +01002466endfunc
2467
Bram Moolenaareb93f3f2019-04-04 15:04:56 +02002468func Test_autocmd_once()
2469 " Without ++once WinNew triggers twice
2470 let g:did_split = 0
2471 augroup Testing
2472 au WinNew * let g:did_split += 1
2473 augroup END
2474 split
2475 split
2476 call assert_equal(2, g:did_split)
2477 call assert_true(exists('#WinNew'))
2478 close
2479 close
2480
2481 " With ++once WinNew triggers once
2482 let g:did_split = 0
2483 augroup Testing
2484 au!
2485 au WinNew * ++once let g:did_split += 1
2486 augroup END
2487 split
2488 split
2489 call assert_equal(1, g:did_split)
2490 call assert_false(exists('#WinNew'))
2491 close
2492 close
2493
2494 call assert_fails('au WinNew * ++once ++once echo bad', 'E983:')
2495endfunc
2496
Bram Moolenaara68e5952019-04-25 22:22:01 +02002497func Test_autocmd_bufreadpre()
2498 new
2499 let b:bufreadpre = 1
Bram Moolenaarab505b12020-03-23 19:28:44 +01002500 call append(0, range(1000))
Bram Moolenaara68e5952019-04-25 22:22:01 +02002501 w! XAutocmdBufReadPre.txt
2502 autocmd BufReadPre <buffer> :let b:bufreadpre += 1
Bram Moolenaarab505b12020-03-23 19:28:44 +01002503 norm! 500gg
Bram Moolenaara68e5952019-04-25 22:22:01 +02002504 sp
Bram Moolenaarab505b12020-03-23 19:28:44 +01002505 norm! 1000gg
Bram Moolenaara68e5952019-04-25 22:22:01 +02002506 wincmd p
2507 let g:wsv1 = winsaveview()
2508 wincmd p
2509 let g:wsv2 = winsaveview()
2510 " triggers BufReadPre, should not move the cursor in either window
2511 " The topline may change one line in a large window.
2512 edit
2513 call assert_inrange(g:wsv2.topline - 1, g:wsv2.topline + 1, winsaveview().topline)
2514 call assert_equal(g:wsv2.lnum, winsaveview().lnum)
2515 call assert_equal(2, b:bufreadpre)
2516 wincmd p
2517 call assert_equal(g:wsv1.topline, winsaveview().topline)
2518 call assert_equal(g:wsv1.lnum, winsaveview().lnum)
2519 call assert_equal(2, b:bufreadpre)
2520 " Now set the cursor position in an BufReadPre autocommand
2521 " (even though the position will be invalid, this should make Vim reset the
2522 " cursor position in the other window.
2523 wincmd p
2524 set cpo+=g
2525 " won't do anything, but try to set the cursor on an invalid lnum
2526 autocmd BufReadPre <buffer> :norm! 70gg
2527 " triggers BufReadPre, should not move the cursor in either window
2528 e
2529 call assert_equal(1, winsaveview().topline)
2530 call assert_equal(1, winsaveview().lnum)
2531 call assert_equal(3, b:bufreadpre)
2532 wincmd p
2533 call assert_equal(g:wsv1.topline, winsaveview().topline)
2534 call assert_equal(g:wsv1.lnum, winsaveview().lnum)
2535 call assert_equal(3, b:bufreadpre)
2536 close
2537 close
2538 call delete('XAutocmdBufReadPre.txt')
2539 set cpo-=g
2540endfunc
2541
Bram Moolenaar5e66b422019-01-24 21:58:10 +01002542" FileChangedShell tested in test_filechanged.vim
Bram Moolenaar69ea5872019-04-25 20:29:00 +02002543
2544" Tests for the following autocommands:
2545" - FileWritePre writing a compressed file
2546" - FileReadPost reading a compressed file
2547" - BufNewFile reading a file template
2548" - BufReadPre decompressing the file to be read
2549" - FilterReadPre substituting characters in the temp file
2550" - FilterReadPost substituting characters after filtering
2551" - FileReadPre set options for decompression
2552" - FileReadPost decompress the file
2553func Test_ReadWrite_Autocmds()
2554 " Run this test only on Unix-like systems and if gzip is available
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +02002555 CheckUnix
2556 CheckExecutable gzip
Bram Moolenaar69ea5872019-04-25 20:29:00 +02002557
2558 " Make $GZIP empty, "-v" would cause trouble.
2559 let $GZIP = ""
2560
2561 " Use a FileChangedShell autocommand to avoid a prompt for 'Xtestfile.gz'
2562 " being modified outside of Vim (noticed on Solaris).
2563 au FileChangedShell * echo 'caught FileChangedShell'
2564
2565 " Test for the FileReadPost, FileWritePre and FileWritePost autocmds
2566 augroup Test1
2567 au!
2568 au FileWritePre *.gz '[,']!gzip
2569 au FileWritePost *.gz undo
2570 au FileReadPost *.gz '[,']!gzip -d
2571 augroup END
2572
2573 new
2574 set bin
2575 call append(0, [
2576 \ 'line 2 Abcdefghijklmnopqrstuvwxyz',
2577 \ 'line 3 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2578 \ 'line 4 Abcdefghijklmnopqrstuvwxyz',
2579 \ 'line 5 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2580 \ 'line 6 Abcdefghijklmnopqrstuvwxyz',
2581 \ 'line 7 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2582 \ 'line 8 Abcdefghijklmnopqrstuvwxyz',
2583 \ 'line 9 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2584 \ 'line 10 Abcdefghijklmnopqrstuvwxyz'
2585 \ ])
2586 1,9write! Xtestfile.gz
2587 enew! | close
2588
2589 new
2590 " Read and decompress the testfile
2591 0read Xtestfile.gz
2592 call assert_equal([
2593 \ 'line 2 Abcdefghijklmnopqrstuvwxyz',
2594 \ 'line 3 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2595 \ 'line 4 Abcdefghijklmnopqrstuvwxyz',
2596 \ 'line 5 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2597 \ 'line 6 Abcdefghijklmnopqrstuvwxyz',
2598 \ 'line 7 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2599 \ 'line 8 Abcdefghijklmnopqrstuvwxyz',
2600 \ 'line 9 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2601 \ 'line 10 Abcdefghijklmnopqrstuvwxyz'
2602 \ ], getline(1, 9))
2603 enew! | close
2604
2605 augroup Test1
2606 au!
2607 augroup END
2608
2609 " Test for the FileAppendPre and FileAppendPost autocmds
2610 augroup Test2
2611 au!
2612 au BufNewFile *.c read Xtest.c
2613 au FileAppendPre *.out '[,']s/new/NEW/
2614 au FileAppendPost *.out !cat Xtest.c >> test.out
2615 augroup END
2616
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01002617 call writefile(['/*', ' * Here is a new .c file', ' */'], 'Xtest.c', 'D')
Bram Moolenaar69ea5872019-04-25 20:29:00 +02002618 new foo.c " should load Xtest.c
2619 call assert_equal(['/*', ' * Here is a new .c file', ' */'], getline(2, 4))
2620 w! >> test.out " append it to the output file
2621
2622 let contents = readfile('test.out')
2623 call assert_equal(' * Here is a NEW .c file', contents[2])
2624 call assert_equal(' * Here is a new .c file', contents[5])
2625
2626 call delete('test.out')
2627 enew! | close
2628 augroup Test2
2629 au!
2630 augroup END
2631
2632 " Test for the BufReadPre and BufReadPost autocmds
2633 augroup Test3
2634 au!
2635 " setup autocommands to decompress before reading and re-compress
2636 " afterwards
2637 au BufReadPre *.gz exe '!gzip -d ' . shellescape(expand("<afile>"))
2638 au BufReadPre *.gz call rename(expand("<afile>:r"), expand("<afile>"))
2639 au BufReadPost *.gz call rename(expand("<afile>"), expand("<afile>:r"))
2640 au BufReadPost *.gz exe '!gzip ' . shellescape(expand("<afile>:r"))
2641 augroup END
2642
2643 e! Xtestfile.gz " Edit compressed file
2644 call assert_equal([
2645 \ 'line 2 Abcdefghijklmnopqrstuvwxyz',
2646 \ 'line 3 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2647 \ 'line 4 Abcdefghijklmnopqrstuvwxyz',
2648 \ 'line 5 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2649 \ 'line 6 Abcdefghijklmnopqrstuvwxyz',
2650 \ 'line 7 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2651 \ 'line 8 Abcdefghijklmnopqrstuvwxyz',
2652 \ 'line 9 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2653 \ 'line 10 Abcdefghijklmnopqrstuvwxyz'
2654 \ ], getline(1, 9))
2655
2656 w! >> test.out " Append it to the output file
2657
2658 augroup Test3
2659 au!
2660 augroup END
2661
2662 " Test for the FilterReadPre and FilterReadPost autocmds.
2663 set shelltemp " need temp files here
2664 augroup Test4
2665 au!
2666 au FilterReadPre *.out call rename(expand("<afile>"), expand("<afile>") . ".t")
2667 au FilterReadPre *.out exe 'silent !sed s/e/E/ ' . shellescape(expand("<afile>")) . ".t >" . shellescape(expand("<afile>"))
2668 au FilterReadPre *.out exe 'silent !rm ' . shellescape(expand("<afile>")) . '.t'
2669 au FilterReadPost *.out '[,']s/x/X/g
2670 augroup END
2671
2672 e! test.out " Edit the output file
2673 1,$!cat
2674 call assert_equal([
2675 \ 'linE 2 AbcdefghijklmnopqrstuvwXyz',
2676 \ 'linE 3 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
2677 \ 'linE 4 AbcdefghijklmnopqrstuvwXyz',
2678 \ 'linE 5 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
2679 \ 'linE 6 AbcdefghijklmnopqrstuvwXyz',
2680 \ 'linE 7 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
2681 \ 'linE 8 AbcdefghijklmnopqrstuvwXyz',
2682 \ 'linE 9 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
2683 \ 'linE 10 AbcdefghijklmnopqrstuvwXyz'
2684 \ ], getline(1, 9))
2685 call assert_equal([
2686 \ 'line 2 Abcdefghijklmnopqrstuvwxyz',
2687 \ 'line 3 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2688 \ 'line 4 Abcdefghijklmnopqrstuvwxyz',
2689 \ 'line 5 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2690 \ 'line 6 Abcdefghijklmnopqrstuvwxyz',
2691 \ 'line 7 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2692 \ 'line 8 Abcdefghijklmnopqrstuvwxyz',
2693 \ 'line 9 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2694 \ 'line 10 Abcdefghijklmnopqrstuvwxyz'
2695 \ ], readfile('test.out'))
2696
2697 augroup Test4
2698 au!
2699 augroup END
2700 set shelltemp&vim
2701
2702 " Test for the FileReadPre and FileReadPost autocmds.
2703 augroup Test5
2704 au!
2705 au FileReadPre *.gz exe 'silent !gzip -d ' . shellescape(expand("<afile>"))
2706 au FileReadPre *.gz call rename(expand("<afile>:r"), expand("<afile>"))
2707 au FileReadPost *.gz '[,']s/l/L/
2708 augroup END
2709
2710 new
2711 0r Xtestfile.gz " Read compressed file
2712 call assert_equal([
2713 \ 'Line 2 Abcdefghijklmnopqrstuvwxyz',
2714 \ 'Line 3 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2715 \ 'Line 4 Abcdefghijklmnopqrstuvwxyz',
2716 \ 'Line 5 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2717 \ 'Line 6 Abcdefghijklmnopqrstuvwxyz',
2718 \ 'Line 7 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2719 \ 'Line 8 Abcdefghijklmnopqrstuvwxyz',
2720 \ 'Line 9 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2721 \ 'Line 10 Abcdefghijklmnopqrstuvwxyz'
2722 \ ], getline(1, 9))
2723 call assert_equal([
2724 \ 'line 2 Abcdefghijklmnopqrstuvwxyz',
2725 \ 'line 3 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2726 \ 'line 4 Abcdefghijklmnopqrstuvwxyz',
2727 \ 'line 5 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2728 \ 'line 6 Abcdefghijklmnopqrstuvwxyz',
2729 \ 'line 7 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2730 \ 'line 8 Abcdefghijklmnopqrstuvwxyz',
2731 \ 'line 9 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2732 \ 'line 10 Abcdefghijklmnopqrstuvwxyz'
2733 \ ], readfile('Xtestfile.gz'))
2734
2735 augroup Test5
2736 au!
2737 augroup END
2738
2739 au! FileChangedShell
2740 call delete('Xtestfile.gz')
Bram Moolenaar69ea5872019-04-25 20:29:00 +02002741 call delete('test.out')
2742endfunc
Bram Moolenaar23b51392019-05-09 21:38:43 +02002743
2744func Test_throw_in_BufWritePre()
2745 new
2746 call setline(1, ['one', 'two', 'three'])
2747 call assert_false(filereadable('Xthefile'))
2748 augroup throwing
2749 au BufWritePre X* throw 'do not write'
2750 augroup END
2751 try
2752 w Xthefile
2753 catch
2754 let caught = 1
2755 endtry
2756 call assert_equal(1, caught)
2757 call assert_false(filereadable('Xthefile'))
2758
2759 bwipe!
2760 au! throwing
2761endfunc
Bram Moolenaarcadbe1b2019-09-22 21:50:09 +02002762
Bram Moolenaar40fa12a2021-09-22 14:18:13 +02002763func Test_autocmd_in_try_block()
Bram Moolenaar6f14da12022-09-07 21:30:44 +01002764 call mkdir('Xintrydir', 'R')
Bram Moolenaar40fa12a2021-09-22 14:18:13 +02002765 au BufEnter * let g:fname = expand('%')
2766 try
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +01002767 edit Xintrydir/
Bram Moolenaar40fa12a2021-09-22 14:18:13 +02002768 endtry
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +01002769 call assert_match('Xintrydir', g:fname)
Bram Moolenaar40fa12a2021-09-22 14:18:13 +02002770
2771 unlet g:fname
2772 au! BufEnter
Bram Moolenaar40fa12a2021-09-22 14:18:13 +02002773endfunc
2774
Bram Moolenaarcadbe1b2019-09-22 21:50:09 +02002775func Test_autocmd_SafeState()
2776 CheckRunVimInTerminal
2777
2778 let lines =<< trim END
2779 let g:safe = 0
2780 let g:again = ''
2781 au SafeState * let g:safe += 1
2782 au SafeStateAgain * let g:again ..= 'x'
2783 func CallTimer()
2784 call timer_start(10, {id -> execute('let g:again ..= "t"')})
2785 endfunc
2786 END
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01002787 call writefile(lines, 'XSafeState', 'D')
Bram Moolenaarcadbe1b2019-09-22 21:50:09 +02002788 let buf = RunVimInTerminal('-S XSafeState', #{rows: 6})
2789
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01002790 " Sometimes we loop to handle a K_IGNORE, SafeState may be triggered once or
Bram Moolenaar8fb1b472020-02-23 16:16:26 +01002791 " more often.
Bram Moolenaarcadbe1b2019-09-22 21:50:09 +02002792 call term_sendkeys(buf, ":echo g:safe\<CR>")
Bram Moolenaar8fb1b472020-02-23 16:16:26 +01002793 call WaitForAssert({-> assert_match('^\d ', term_getline(buf, 6))}, 1000)
Bram Moolenaarcadbe1b2019-09-22 21:50:09 +02002794
Bram Moolenaar8fb1b472020-02-23 16:16:26 +01002795 " SafeStateAgain should be invoked at least three times
Bram Moolenaarcadbe1b2019-09-22 21:50:09 +02002796 call term_sendkeys(buf, ":echo g:again\<CR>")
Bram Moolenaar8fb1b472020-02-23 16:16:26 +01002797 call WaitForAssert({-> assert_match('^xxx', term_getline(buf, 6))}, 1000)
Bram Moolenaarcadbe1b2019-09-22 21:50:09 +02002798
2799 call term_sendkeys(buf, ":let g:again = ''\<CR>:call CallTimer()\<CR>")
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02002800 call TermWait(buf, 50)
Bram Moolenaar0f6629a2019-09-22 23:24:13 +02002801 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02002802 call TermWait(buf, 50)
Bram Moolenaarcadbe1b2019-09-22 21:50:09 +02002803 call term_sendkeys(buf, ":echo g:again\<CR>")
2804 call WaitForAssert({-> assert_match('xtx', term_getline(buf, 6))}, 1000)
2805
2806 call StopVimInTerminal(buf)
Bram Moolenaarcadbe1b2019-09-22 21:50:09 +02002807endfunc
Bram Moolenaar23324a02019-10-01 17:39:04 +02002808
2809func Test_autocmd_CmdWinEnter()
2810 CheckRunVimInTerminal
Bram Moolenaar21829c52021-01-26 22:42:21 +01002811
Bram Moolenaar23324a02019-10-01 17:39:04 +02002812 let lines =<< trim END
Egor Zvorykin125ffd22021-11-17 14:01:14 +00002813 augroup vimHints | au! | augroup END
Bram Moolenaar23324a02019-10-01 17:39:04 +02002814 let b:dummy_var = 'This is a dummy'
2815 autocmd CmdWinEnter * quit
2816 let winnr = winnr('$')
2817 END
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01002818 let filename = 'XCmdWinEnter'
Bram Moolenaar23324a02019-10-01 17:39:04 +02002819 call writefile(lines, filename)
2820 let buf = RunVimInTerminal('-S '.filename, #{rows: 6})
2821
2822 call term_sendkeys(buf, "q:")
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02002823 call TermWait(buf)
Bram Moolenaar23324a02019-10-01 17:39:04 +02002824 call term_sendkeys(buf, ":echo b:dummy_var\<cr>")
Bram Moolenaar353c3512020-03-15 14:19:26 +01002825 call WaitForAssert({-> assert_match('^This is a dummy', term_getline(buf, 6))}, 2000)
Bram Moolenaar23324a02019-10-01 17:39:04 +02002826 call term_sendkeys(buf, ":echo &buftype\<cr>")
2827 call WaitForAssert({-> assert_notmatch('^nofile', term_getline(buf, 6))}, 1000)
2828 call term_sendkeys(buf, ":echo winnr\<cr>")
2829 call WaitForAssert({-> assert_match('^1', term_getline(buf, 6))}, 1000)
2830
2831 " clean up
2832 call StopVimInTerminal(buf)
2833 call delete(filename)
2834endfunc
Bram Moolenaarec66c412019-10-11 21:19:13 +02002835
2836func Test_autocmd_was_using_freed_memory()
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01002837 CheckFeature quickfix
2838
Bram Moolenaarec66c412019-10-11 21:19:13 +02002839 pedit xx
2840 n x
Bram Moolenaar92bb83e2021-02-03 23:04:46 +01002841 augroup winenter
2842 au WinEnter * if winnr('$') > 2 | quit | endif
2843 augroup END
Bram Moolenaarec66c412019-10-11 21:19:13 +02002844 split
Bram Moolenaar92bb83e2021-02-03 23:04:46 +01002845
2846 augroup winenter
2847 au! WinEnter
2848 augroup END
2849
2850 bwipe xx
2851 bwipe x
2852 pclose
Bram Moolenaarec66c412019-10-11 21:19:13 +02002853endfunc
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +01002854
2855func Test_BufWrite_lockmarks()
Bram Moolenaarf08b0eb2021-10-16 13:00:14 +01002856 let g:test_is_flaky = 1
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +01002857 edit! Xtest
2858 call setline(1, ['a', 'b', 'c', 'd'])
2859
2860 " :lockmarks preserves the marks
2861 call SetChangeMarks(2, 3)
2862 lockmarks write
2863 call assert_equal([2, 3], [line("'["), line("']")])
2864
2865 " *WritePre autocmds get the correct line range, but lockmarks preserves the
2866 " original values for the user
2867 augroup lockmarks
2868 au!
2869 au BufWritePre,FilterWritePre * call assert_equal([1, 4], [line("'["), line("']")])
2870 au FileWritePre * call assert_equal([3, 4], [line("'["), line("']")])
2871 augroup END
2872
2873 lockmarks write
2874 call assert_equal([2, 3], [line("'["), line("']")])
2875
2876 if executable('cat')
2877 lockmarks %!cat
2878 call assert_equal([2, 3], [line("'["), line("']")])
2879 endif
2880
2881 lockmarks 3,4write Xtest2
2882 call assert_equal([2, 3], [line("'["), line("']")])
2883
2884 au! lockmarks
2885 augroup! lockmarks
2886 call delete('Xtest')
2887 call delete('Xtest2')
2888endfunc
Bram Moolenaarce6db022020-01-07 20:11:42 +01002889
2890func Test_FileType_spell()
2891 if !isdirectory('/tmp')
2892 throw "Skipped: requires /tmp directory"
2893 endif
2894
2895 " this was crashing with an invalid free()
2896 setglobal spellfile=/tmp/en.utf-8.add
2897 augroup crash
2898 autocmd!
2899 autocmd BufNewFile,BufReadPost crashfile setf somefiletype
2900 autocmd BufNewFile,BufReadPost crashfile set ft=anotherfiletype
2901 autocmd FileType anotherfiletype setlocal spell
2902 augroup END
2903 func! NoCrash() abort
2904 edit /tmp/crashfile
2905 endfunc
2906 call NoCrash()
2907
2908 au! crash
2909 setglobal spellfile=
2910endfunc
Bram Moolenaarbc2b71d2020-02-17 21:33:30 +01002911
Bram Moolenaaref976322022-09-28 11:48:30 +01002912" this was wiping out the current buffer and using freed memory
2913func Test_SpellFileMissing_bwipe()
2914 next 0
2915 au SpellFileMissing 0 bwipe
2916 call assert_fails('set spell spelllang=0', 'E937:')
2917
2918 au! SpellFileMissing
2919 bwipe
2920endfunc
2921
Bram Moolenaar406cd902020-02-18 21:54:41 +01002922" Test closing a window or editing another buffer from a FileChangedRO handler
2923" in a readonly buffer
2924func Test_FileChangedRO_winclose()
Bram Moolenaar62cd26a2020-10-11 20:08:44 +02002925 call test_override('ui_delay', 10)
2926
Bram Moolenaar406cd902020-02-18 21:54:41 +01002927 augroup FileChangedROTest
2928 au!
2929 autocmd FileChangedRO * quit
2930 augroup END
2931 new
2932 set readonly
2933 call assert_fails('normal i', 'E788:')
2934 close
2935 augroup! FileChangedROTest
2936
2937 augroup FileChangedROTest
2938 au!
Bram Moolenaar61abe7d2022-08-30 21:46:08 +01002939 autocmd FileChangedRO * edit Xrofile
Bram Moolenaar406cd902020-02-18 21:54:41 +01002940 augroup END
2941 new
2942 set readonly
2943 call assert_fails('normal i', 'E788:')
2944 close
2945 augroup! FileChangedROTest
Bram Moolenaar62cd26a2020-10-11 20:08:44 +02002946 call test_override('ALL', 0)
Bram Moolenaar406cd902020-02-18 21:54:41 +01002947endfunc
2948
Bram Moolenaar0c81d1b2020-02-22 22:45:55 +01002949func LogACmd()
2950 call add(g:logged, line('$'))
2951endfunc
2952
2953func Test_TermChanged()
Bram Moolenaard28e0b32020-02-22 23:08:52 +01002954 CheckNotGui
2955
Bram Moolenaar0c81d1b2020-02-22 22:45:55 +01002956 enew!
2957 tabnew
2958 call setline(1, ['a', 'b', 'c', 'd'])
2959 $
2960 au TermChanged * call LogACmd()
2961 let g:logged = []
2962 let term_save = &term
2963 set term=xterm
2964 call assert_equal([1, 4], g:logged)
2965
2966 au! TermChanged
2967 let &term = term_save
2968 bwipe!
2969endfunc
2970
Bram Moolenaare3284872020-03-19 13:55:03 +01002971" Test for FileReadCmd autocmd
2972func Test_autocmd_FileReadCmd()
2973 func ReadFileCmd()
2974 call append(line('$'), "v:cmdarg = " .. v:cmdarg)
2975 endfunc
2976 augroup FileReadCmdTest
2977 au!
2978 au FileReadCmd Xtest call ReadFileCmd()
2979 augroup END
2980
2981 new
2982 read ++bin Xtest
2983 read ++nobin Xtest
2984 read ++edit Xtest
2985 read ++bad=keep Xtest
2986 read ++bad=drop Xtest
2987 read ++bad=- Xtest
2988 read ++ff=unix Xtest
2989 read ++ff=dos Xtest
2990 read ++ff=mac Xtest
2991 read ++enc=utf-8 Xtest
2992
2993 call assert_equal(['',
2994 \ 'v:cmdarg = ++bin',
2995 \ 'v:cmdarg = ++nobin',
2996 \ 'v:cmdarg = ++edit',
2997 \ 'v:cmdarg = ++bad=keep',
2998 \ 'v:cmdarg = ++bad=drop',
2999 \ 'v:cmdarg = ++bad=-',
3000 \ 'v:cmdarg = ++ff=unix',
3001 \ 'v:cmdarg = ++ff=dos',
3002 \ 'v:cmdarg = ++ff=mac',
3003 \ 'v:cmdarg = ++enc=utf-8'], getline(1, '$'))
3004
3005 close!
3006 augroup FileReadCmdTest
3007 au!
3008 augroup END
3009 delfunc ReadFileCmd
3010endfunc
3011
Bram Moolenaaree4e0c12020-04-06 21:35:05 +02003012" Test for passing invalid arguments to autocmd
3013func Test_autocmd_invalid_args()
3014 " Additional character after * for event
Bram Moolenaar61abe7d2022-08-30 21:46:08 +01003015 call assert_fails('autocmd *a Xinvfile set ff=unix', 'E215:')
Bram Moolenaaree4e0c12020-04-06 21:35:05 +02003016 augroup Test
3017 augroup END
3018 " Invalid autocmd event
Bram Moolenaar61abe7d2022-08-30 21:46:08 +01003019 call assert_fails('autocmd Bufabc Xinvfile set ft=vim', 'E216:')
Bram Moolenaaree4e0c12020-04-06 21:35:05 +02003020 " Invalid autocmd event in a autocmd group
Bram Moolenaar61abe7d2022-08-30 21:46:08 +01003021 call assert_fails('autocmd Test Bufabc Xinvfile set ft=vim', 'E216:')
Bram Moolenaaree4e0c12020-04-06 21:35:05 +02003022 augroup! Test
3023 " Execute all autocmds
3024 call assert_fails('doautocmd * BufEnter', 'E217:')
3025 call assert_fails('augroup! x1a2b3', 'E367:')
3026 call assert_fails('autocmd BufNew <buffer=999> pwd', 'E680:')
Bram Moolenaar531be472020-09-23 22:38:05 +02003027 call assert_fails('autocmd BufNew \) set ff=unix', 'E55:')
Bram Moolenaaree4e0c12020-04-06 21:35:05 +02003028endfunc
3029
3030" Test for deep nesting of autocmds
3031func Test_autocmd_deep_nesting()
Bram Moolenaar61abe7d2022-08-30 21:46:08 +01003032 autocmd BufEnter Xdeepfile doautocmd BufEnter Xdeepfile
3033 call assert_fails('doautocmd BufEnter Xdeepfile', 'E218:')
3034 autocmd! BufEnter Xdeepfile
Bram Moolenaaree4e0c12020-04-06 21:35:05 +02003035endfunc
3036
Bram Moolenaarbe5ee862020-06-10 20:56:58 +02003037" Tests for SigUSR1 autocmd event, which is only available on posix systems.
3038func Test_autocmd_sigusr1()
3039 CheckUnix
Bram Moolenaar0056ca72022-09-23 21:26:39 +01003040 " FIXME: should this work on MacOS M1?
3041 CheckNotMacM1
Bram Moolenaar62cd26a2020-10-11 20:08:44 +02003042 CheckExecutable /bin/kill
Bram Moolenaarbe5ee862020-06-10 20:56:58 +02003043
3044 let g:sigusr1_passed = 0
3045 au SigUSR1 * let g:sigusr1_passed = 1
3046 call system('/bin/kill -s usr1 ' . getpid())
3047 call WaitForAssert({-> assert_true(g:sigusr1_passed)})
3048
3049 au! SigUSR1
3050 unlet g:sigusr1_passed
3051endfunc
3052
Bram Moolenaarb340bae2020-06-15 19:51:56 +02003053" Test for BufReadPre autocmd deleting the file
3054func Test_BufReadPre_delfile()
3055 augroup TestAuCmd
3056 au!
Bram Moolenaare7cda972022-08-29 11:02:59 +01003057 autocmd BufReadPre XbufreadPre call delete('XbufreadPre')
Bram Moolenaarb340bae2020-06-15 19:51:56 +02003058 augroup END
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01003059 call writefile([], 'XbufreadPre', 'D')
Bram Moolenaare7cda972022-08-29 11:02:59 +01003060 call assert_fails('new XbufreadPre', 'E200:')
3061 call assert_equal('XbufreadPre', @%)
Bram Moolenaarb340bae2020-06-15 19:51:56 +02003062 call assert_equal(1, &readonly)
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01003063
Bram Moolenaarb340bae2020-06-15 19:51:56 +02003064 augroup TestAuCmd
3065 au!
3066 augroup END
3067 close!
3068endfunc
3069
3070" Test for BufReadPre autocmd changing the current buffer
3071func Test_BufReadPre_changebuf()
3072 augroup TestAuCmd
3073 au!
Bram Moolenaare7cda972022-08-29 11:02:59 +01003074 autocmd BufReadPre Xchangebuf edit Xsomeotherfile
Bram Moolenaarb340bae2020-06-15 19:51:56 +02003075 augroup END
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01003076 call writefile([], 'Xchangebuf', 'D')
Bram Moolenaare7cda972022-08-29 11:02:59 +01003077 call assert_fails('new Xchangebuf', 'E201:')
Bram Moolenaarb340bae2020-06-15 19:51:56 +02003078 call assert_equal('Xsomeotherfile', @%)
3079 call assert_equal(1, &readonly)
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01003080
Bram Moolenaarb340bae2020-06-15 19:51:56 +02003081 augroup TestAuCmd
3082 au!
3083 augroup END
3084 close!
3085endfunc
3086
3087" Test for BufWipeouti autocmd changing the current buffer when reading a file
3088" in an empty buffer with 'f' flag in 'cpo'
3089func Test_BufDelete_changebuf()
3090 new
3091 augroup TestAuCmd
3092 au!
3093 autocmd BufWipeout * let bufnr = bufadd('somefile') | exe "b " .. bufnr
3094 augroup END
3095 let save_cpo = &cpo
3096 set cpo+=f
Bram Moolenaarb18b4962022-09-02 21:55:50 +01003097 call assert_fails('r Xchangebuf', ['E812:', 'E484:'])
Bram Moolenaarb340bae2020-06-15 19:51:56 +02003098 call assert_equal('somefile', @%)
3099 let &cpo = save_cpo
3100 augroup TestAuCmd
3101 au!
3102 augroup END
3103 close!
3104endfunc
3105
Bram Moolenaar0fe937f2020-06-16 22:42:04 +02003106" Test for the temporary internal window used to execute autocmds
3107func Test_autocmd_window()
3108 %bw!
3109 edit one.txt
3110 tabnew two.txt
Bram Moolenaar41cd8032021-03-13 15:47:56 +01003111 vnew three.txt
3112 tabnew four.txt
3113 tabprevious
Bram Moolenaar0fe937f2020-06-16 22:42:04 +02003114 let g:blist = []
Bram Moolenaar832adf92020-06-25 19:01:36 +02003115 augroup aucmd_win_test1
Bram Moolenaar0fe937f2020-06-16 22:42:04 +02003116 au!
3117 au BufEnter * call add(g:blist, [expand('<afile>'),
3118 \ win_gettype(bufwinnr(expand('<afile>')))])
3119 augroup END
3120
3121 doautoall BufEnter
Bram Moolenaar41cd8032021-03-13 15:47:56 +01003122 call assert_equal([
3123 \ ['one.txt', 'autocmd'],
3124 \ ['two.txt', ''],
3125 \ ['four.txt', 'autocmd'],
3126 \ ['three.txt', ''],
3127 \ ], g:blist)
Bram Moolenaar0fe937f2020-06-16 22:42:04 +02003128
Bram Moolenaar832adf92020-06-25 19:01:36 +02003129 augroup aucmd_win_test1
Bram Moolenaar0fe937f2020-06-16 22:42:04 +02003130 au!
3131 augroup END
Bram Moolenaar832adf92020-06-25 19:01:36 +02003132 augroup! aucmd_win_test1
3133 %bw!
3134endfunc
3135
3136" Test for trying to close the temporary window used for executing an autocmd
3137func Test_close_autocmd_window()
3138 %bw!
3139 edit one.txt
3140 tabnew two.txt
3141 augroup aucmd_win_test2
3142 au!
3143 au BufEnter * if expand('<afile>') == 'one.txt' | 1close | endif
3144 augroup END
3145
3146 call assert_fails('doautoall BufEnter', 'E813:')
3147
3148 augroup aucmd_win_test2
3149 au!
3150 augroup END
3151 augroup! aucmd_win_test2
Bram Moolenaarcf844172020-06-26 19:44:06 +02003152 %bwipe!
3153endfunc
3154
3155" Test for trying to close the tab that has the temporary window for exeucing
3156" an autocmd.
3157func Test_close_autocmd_tab()
3158 edit one.txt
3159 tabnew two.txt
3160 augroup aucmd_win_test
3161 au!
3162 au BufEnter * if expand('<afile>') == 'one.txt' | tabfirst | tabonly | endif
3163 augroup END
3164
3165 call assert_fails('doautoall BufEnter', 'E813:')
3166
3167 tabonly
3168 augroup aucmd_win_test
3169 au!
3170 augroup END
3171 augroup! aucmd_win_test
3172 %bwipe!
Bram Moolenaar0fe937f2020-06-16 22:42:04 +02003173endfunc
3174
Bram Moolenaarcb1956d2022-01-07 15:45:18 +00003175func Test_Visual_doautoall_redraw()
3176 call setline(1, ['a', 'b'])
3177 new
3178 wincmd p
3179 call feedkeys("G\<C-V>", 'txn')
3180 autocmd User Explode ++once redraw
3181 doautoall User Explode
3182 %bwipe!
3183endfunc
3184
Bram Moolenaar6bcb8772021-02-03 21:23:29 +01003185" This was using freed memory.
3186func Test_BufNew_arglocal()
3187 arglocal
3188 au BufNew * arglocal
3189 call assert_fails('drop xx', 'E1156:')
3190
3191 au! BufNew
3192endfunc
3193
Bram Moolenaar8ab37572021-02-03 21:56:59 +01003194func Test_autocmd_closes_window()
3195 au BufNew,BufWinLeave * e %e
3196 file yyy
3197 au BufNew,BufWinLeave * ball
Bram Moolenaaraad5f9d2021-02-06 17:30:31 +01003198 n xxx
Bram Moolenaar8ab37572021-02-03 21:56:59 +01003199
Bram Moolenaaraad5f9d2021-02-06 17:30:31 +01003200 %bwipe
Bram Moolenaar8ab37572021-02-03 21:56:59 +01003201 au! BufNew
3202 au! BufWinLeave
3203endfunc
3204
Bram Moolenaar92bb83e2021-02-03 23:04:46 +01003205func Test_autocmd_quit_psearch()
3206 sn aa bb
3207 augroup aucmd_win_test
3208 au!
3209 au BufEnter,BufLeave,BufNew,WinEnter,WinLeave,WinNew * if winnr('$') > 1 | q | endif
3210 augroup END
3211 ps /
3212
3213 augroup aucmd_win_test
3214 au!
3215 augroup END
zeertzjq7851c692022-04-21 11:14:01 +01003216 new
3217 pclose
Bram Moolenaar92bb83e2021-02-03 23:04:46 +01003218endfunc
3219
Bram Moolenaaraad5f9d2021-02-06 17:30:31 +01003220" Fuzzer found some strange combination that caused a crash.
3221func Test_autocmd_normal_mess()
Bram Moolenaardd07c022021-02-07 13:32:46 +01003222 " For unknown reason this hangs on MS-Windows
3223 CheckNotMSWindows
3224
Bram Moolenaaraad5f9d2021-02-06 17:30:31 +01003225 augroup aucmd_normal_test
3226 au BufLeave,BufWinLeave,BufHidden,BufUnload,BufDelete,BufWipeout * norm 7q/qc
3227 augroup END
Bram Moolenaar983d83f2021-02-07 12:12:43 +01003228 call assert_fails('o4', 'E1159')
Bram Moolenaaraad5f9d2021-02-06 17:30:31 +01003229 silent! H
Bram Moolenaar983d83f2021-02-07 12:12:43 +01003230 call assert_fails('e xx', 'E1159')
Bram Moolenaaraad5f9d2021-02-06 17:30:31 +01003231 normal G
3232
3233 augroup aucmd_normal_test
3234 au!
3235 augroup END
3236endfunc
3237
Bram Moolenaar8c6951f2021-02-06 18:08:45 +01003238func Test_autocmd_closing_cmdwin()
Bram Moolenaardd07c022021-02-07 13:32:46 +01003239 " For unknown reason this hangs on MS-Windows
3240 CheckNotMSWindows
3241
Bram Moolenaar8c6951f2021-02-06 18:08:45 +01003242 au BufWinLeave * nested q
3243 call assert_fails("norm 7q?\n", 'E855:')
3244
3245 au! BufWinLeave
3246 new
3247 only
3248endfunc
3249
Bram Moolenaar2c7080b2021-02-06 19:19:42 +01003250func Test_autocmd_vimgrep()
3251 augroup aucmd_vimgrep
Charlie Grovesfef44852022-04-19 16:24:12 +01003252 au QuickfixCmdPre,BufNew,BufReadCmd * sb
zeertzjq7851c692022-04-21 11:14:01 +01003253 au QuickfixCmdPre,BufNew,BufReadCmd * q9
Bram Moolenaar2c7080b2021-02-06 19:19:42 +01003254 augroup END
Bram Moolenaardd07c022021-02-07 13:32:46 +01003255 call assert_fails('lv ?a? foo', 'E926:')
Bram Moolenaar2c7080b2021-02-06 19:19:42 +01003256
3257 augroup aucmd_vimgrep
3258 au!
3259 augroup END
3260endfunc
3261
Bram Moolenaar73b8b0a2021-08-01 14:52:32 +02003262func Test_autocmd_with_block()
3263 augroup block_testing
3264 au BufReadPost *.xml {
3265 setlocal matchpairs+=<:>
3266 /<start
3267 }
Bram Moolenaar63b91732021-08-05 20:40:03 +02003268 au CursorHold * {
3269 autocmd BufReadPre * ++once echo 'one' | echo 'two'
3270 g:gotSafeState = 77
3271 }
Bram Moolenaar73b8b0a2021-08-01 14:52:32 +02003272 augroup END
3273
3274 let expected = "\n--- Autocommands ---\nblock_testing BufRead\n *.xml {^@ setlocal matchpairs+=<:>^@ /<start^@ }"
3275 call assert_equal(expected, execute('au BufReadPost *.xml'))
3276
Bram Moolenaar63b91732021-08-05 20:40:03 +02003277 doautocmd CursorHold
3278 call assert_equal(77, g:gotSafeState)
3279 unlet g:gotSafeState
3280
Bram Moolenaar73b8b0a2021-08-01 14:52:32 +02003281 augroup block_testing
3282 au!
Bram Moolenaar75ebd2a2022-06-03 17:39:46 +01003283 autocmd CursorHold * {
3284 if true
3285 # comment
3286 && true
3287
3288 && true
3289 g:done = 'yes'
3290 endif
3291 }
3292 augroup END
3293 doautocmd CursorHold
3294 call assert_equal('yes', g:done)
3295
3296 unlet g:done
3297 augroup block_testing
3298 au!
Bram Moolenaar73b8b0a2021-08-01 14:52:32 +02003299 augroup END
3300endfunc
3301
Christian Brabandtdb3b4462021-10-16 11:58:55 +01003302" Test TextChangedI and TextChanged
3303func Test_Changed_ChangedI()
3304 new
3305 call test_override("char_avail", 1)
3306 let [g:autocmd_i, g:autocmd_n] = ['','']
3307
3308 func! TextChangedAutocmdI(char)
3309 let g:autocmd_{tolower(a:char)} = a:char .. b:changedtick
3310 endfunc
3311
3312 augroup Test_TextChanged
3313 au!
3314 au TextChanged <buffer> :call TextChangedAutocmdI('N')
3315 au TextChangedI <buffer> :call TextChangedAutocmdI('I')
3316 augroup END
3317
3318 call feedkeys("ifoo\<esc>", 'tnix')
3319 " TODO: Test test does not seem to trigger TextChanged autocommand, this
3320 " requires running Vim in a terminal window.
3321 " call assert_equal('N3', g:autocmd_n)
3322 call assert_equal('I3', g:autocmd_i)
3323
3324 call feedkeys("yyp", 'tnix')
3325 " TODO: Test test does not seem to trigger TextChanged autocommand.
3326 " call assert_equal('N4', g:autocmd_n)
3327 call assert_equal('I3', g:autocmd_i)
3328
3329 " CleanUp
3330 call test_override("char_avail", 0)
3331 au! TextChanged <buffer>
3332 au! TextChangedI <buffer>
3333 augroup! Test_TextChanged
3334 delfu TextChangedAutocmdI
3335 unlet! g:autocmd_i g:autocmd_n
3336
3337 bw!
3338endfunc
Bram Moolenaar2c7080b2021-02-06 19:19:42 +01003339
Bram Moolenaar6f2465d2022-03-22 18:13:01 +00003340func Test_closing_autocmd_window()
3341 let lines =<< trim END
3342 edit Xa.txt
3343 tabnew Xb.txt
3344 autocmd BufEnter Xa.txt unhide 1
3345 doautoall BufEnter
3346 END
3347 call v9.CheckScriptFailure(lines, 'E814:')
3348 au! BufEnter
3349 only!
3350 bwipe Xa.txt
3351 bwipe Xb.txt
3352endfunc
3353
Bram Moolenaar347538f2022-03-26 16:28:06 +00003354func Test_bufwipeout_changes_window()
3355 " This should not crash, but we don't have any expectations about what
3356 " happens, changing window in BufWipeout has unpredictable results.
3357 tabedit
3358 let g:window_id = win_getid()
3359 topleft new
3360 setlocal bufhidden=wipe
3361 autocmd BufWipeout <buffer> call win_gotoid(g:window_id)
3362 tabprevious
3363 +tabclose
3364
3365 unlet g:window_id
3366 au! BufWipeout
3367 %bwipe!
3368endfunc
3369
zeertzjq021996f2022-04-10 11:44:04 +01003370func Test_v_event_readonly()
3371 autocmd CompleteChanged * let v:event.width = 0
3372 call assert_fails("normal! i\<C-X>\<C-V>", 'E46:')
3373 au! CompleteChanged
3374
3375 autocmd DirChangedPre * let v:event.directory = ''
3376 call assert_fails('cd .', 'E46:')
3377 au! DirChangedPre
3378
3379 autocmd ModeChanged * let v:event.new_mode = ''
3380 call assert_fails('normal! cc', 'E46:')
3381 au! ModeChanged
3382
3383 autocmd TextYankPost * let v:event.operator = ''
3384 call assert_fails('normal! yy', 'E46:')
3385 au! TextYankPost
3386endfunc
3387
zeertzjqc9e8fd62022-07-26 18:12:38 +01003388" Test for ModeChanged pattern
3389func Test_mode_changes()
3390 let g:index = 0
3391 let g:mode_seq = ['n', 'i', 'n', 'v', 'V', 'i', 'ix', 'i', 'ic', 'i', 'n', 'no', 'n', 'V', 'v', 's', 'n']
3392 func! TestMode()
3393 call assert_equal(g:mode_seq[g:index], get(v:event, "old_mode"))
3394 call assert_equal(g:mode_seq[g:index + 1], get(v:event, "new_mode"))
3395 call assert_equal(mode(1), get(v:event, "new_mode"))
3396 let g:index += 1
3397 endfunc
3398
3399 au ModeChanged * :call TestMode()
3400 let g:n_to_any = 0
3401 au ModeChanged n:* let g:n_to_any += 1
3402 call feedkeys("i\<esc>vVca\<CR>\<C-X>\<C-L>\<esc>ggdG", 'tnix')
3403
3404 let g:V_to_v = 0
3405 au ModeChanged V:v let g:V_to_v += 1
3406 call feedkeys("Vv\<C-G>\<esc>", 'tnix')
3407 call assert_equal(len(filter(g:mode_seq[1:], {idx, val -> val == 'n'})), g:n_to_any)
3408 call assert_equal(1, g:V_to_v)
3409 call assert_equal(len(g:mode_seq) - 1, g:index)
3410
3411 let g:n_to_i = 0
3412 au ModeChanged n:i let g:n_to_i += 1
3413 let g:n_to_niI = 0
3414 au ModeChanged i:niI let g:n_to_niI += 1
3415 let g:niI_to_i = 0
3416 au ModeChanged niI:i let g:niI_to_i += 1
3417 let g:nany_to_i = 0
3418 au ModeChanged n*:i let g:nany_to_i += 1
3419 let g:i_to_n = 0
3420 au ModeChanged i:n let g:i_to_n += 1
3421 let g:nori_to_any = 0
3422 au ModeChanged [ni]:* let g:nori_to_any += 1
3423 let g:i_to_any = 0
3424 au ModeChanged i:* let g:i_to_any += 1
3425 let g:index = 0
3426 let g:mode_seq = ['n', 'i', 'niI', 'i', 'n']
3427 call feedkeys("a\<C-O>l\<esc>", 'tnix')
3428 call assert_equal(len(g:mode_seq) - 1, g:index)
3429 call assert_equal(1, g:n_to_i)
3430 call assert_equal(1, g:n_to_niI)
3431 call assert_equal(1, g:niI_to_i)
3432 call assert_equal(2, g:nany_to_i)
3433 call assert_equal(1, g:i_to_n)
3434 call assert_equal(2, g:i_to_any)
3435 call assert_equal(3, g:nori_to_any)
3436
3437 if has('terminal')
3438 let g:mode_seq += ['c', 'n', 't', 'nt', 'c', 'nt', 'n']
3439 call feedkeys(":term\<CR>\<C-W>N:bd!\<CR>", 'tnix')
3440 call assert_equal(len(g:mode_seq) - 1, g:index)
3441 call assert_equal(1, g:n_to_i)
3442 call assert_equal(1, g:n_to_niI)
3443 call assert_equal(1, g:niI_to_i)
3444 call assert_equal(2, g:nany_to_i)
3445 call assert_equal(1, g:i_to_n)
3446 call assert_equal(2, g:i_to_any)
3447 call assert_equal(5, g:nori_to_any)
3448 endif
3449
zeertzjqd1955982022-10-05 11:24:46 +01003450 let g:n_to_c = 0
3451 au ModeChanged n:c let g:n_to_c += 1
3452 let g:c_to_n = 0
3453 au ModeChanged c:n let g:c_to_n += 1
3454 let g:mode_seq += ['c', 'n', 'c', 'n']
3455 call feedkeys("q:\<C-C>\<Esc>", 'tnix')
3456 call assert_equal(len(g:mode_seq) - 1, g:index)
3457 call assert_equal(2, g:n_to_c)
3458 call assert_equal(2, g:c_to_n)
3459 unlet g:n_to_c
3460 unlet g:c_to_n
zeertzjqc9e8fd62022-07-26 18:12:38 +01003461
Bram Moolenaar61c4b042022-10-18 15:10:11 +01003462 let g:n_to_v = 0
3463 au ModeChanged n:v let g:n_to_v += 1
3464 let g:v_to_n = 0
3465 au ModeChanged v:n let g:v_to_n += 1
3466 let g:mode_seq += ['v', 'n']
3467 call feedkeys("v\<C-C>", 'tnix')
3468 call assert_equal(len(g:mode_seq) - 1, g:index)
3469 call assert_equal(1, g:n_to_v)
3470 call assert_equal(1, g:v_to_n)
3471 unlet g:n_to_v
3472 unlet g:v_to_n
3473
zeertzjqc9e8fd62022-07-26 18:12:38 +01003474 au! ModeChanged
3475 delfunc TestMode
3476 unlet! g:mode_seq
3477 unlet! g:index
3478 unlet! g:n_to_any
3479 unlet! g:V_to_v
3480 unlet! g:n_to_i
3481 unlet! g:n_to_niI
3482 unlet! g:niI_to_i
3483 unlet! g:nany_to_i
3484 unlet! g:i_to_n
3485 unlet! g:nori_to_any
3486 unlet! g:i_to_any
3487endfunc
3488
3489func Test_recursive_ModeChanged()
3490 au! ModeChanged * norm 0u
3491 sil! norm 
3492 au! ModeChanged
3493endfunc
3494
3495func Test_ModeChanged_starts_visual()
3496 " This was triggering ModeChanged before setting VIsual, causing a crash.
3497 au! ModeChanged * norm 0u
3498 sil! norm 
3499
3500 au! ModeChanged
3501endfunc
Bram Moolenaar347538f2022-03-26 16:28:06 +00003502
Charlie Grovesfef44852022-04-19 16:24:12 +01003503func Test_noname_autocmd()
3504 augroup test_noname_autocmd_group
3505 autocmd!
3506 autocmd BufEnter * call add(s:li, ["BufEnter", expand("<afile>")])
3507 autocmd BufDelete * call add(s:li, ["BufDelete", expand("<afile>")])
3508 autocmd BufLeave * call add(s:li, ["BufLeave", expand("<afile>")])
3509 autocmd BufUnload * call add(s:li, ["BufUnload", expand("<afile>")])
3510 autocmd BufWipeout * call add(s:li, ["BufWipeout", expand("<afile>")])
3511 augroup END
3512
3513 let s:li = []
3514 edit foo
3515 call assert_equal([['BufUnload', ''], ['BufDelete', ''], ['BufWipeout', ''], ['BufEnter', 'foo']], s:li)
3516
3517 au! test_noname_autocmd_group
3518 augroup! test_noname_autocmd_group
3519endfunc
3520
Yegappan Lakshmanan1755a912022-05-19 10:31:47 +01003521" Test for the autocmd_get() function
3522func Test_autocmd_get()
3523 augroup TestAutoCmdFns
3524 au!
3525 autocmd BufAdd *.vim echo "bufadd-vim"
3526 autocmd BufAdd *.py echo "bufadd-py"
3527 autocmd BufHidden *.vim echo "bufhidden"
3528 augroup END
3529 augroup TestAutoCmdFns2
3530 autocmd BufAdd *.vim echo "bufadd-vim-2"
3531 autocmd BufRead *.a1b2c3 echo "bufadd-vim-2"
3532 augroup END
3533
3534 let l = autocmd_get()
3535 call assert_true(l->len() > 0)
3536
3537 " Test for getting all the autocmds in a group
3538 let expected = [
3539 \ #{cmd: 'echo "bufadd-vim"', group: 'TestAutoCmdFns',
3540 \ pattern: '*.vim', nested: v:false, once: v:false,
3541 \ event: 'BufAdd'},
3542 \ #{cmd: 'echo "bufadd-py"', group: 'TestAutoCmdFns',
3543 \ pattern: '*.py', nested: v:false, once: v:false,
3544 \ event: 'BufAdd'},
3545 \ #{cmd: 'echo "bufhidden"', group: 'TestAutoCmdFns',
3546 \ pattern: '*.vim', nested: v:false,
3547 \ once: v:false, event: 'BufHidden'}]
3548 call assert_equal(expected, autocmd_get(#{group: 'TestAutoCmdFns'}))
3549
3550 " Test for getting autocmds for all the patterns in a group
3551 call assert_equal(expected, autocmd_get(#{group: 'TestAutoCmdFns',
3552 \ event: '*'}))
3553
3554 " Test for getting autocmds for an event in a group
3555 let expected = [
3556 \ #{cmd: 'echo "bufadd-vim"', group: 'TestAutoCmdFns',
3557 \ pattern: '*.vim', nested: v:false, once: v:false,
3558 \ event: 'BufAdd'},
3559 \ #{cmd: 'echo "bufadd-py"', group: 'TestAutoCmdFns',
3560 \ pattern: '*.py', nested: v:false, once: v:false,
3561 \ event: 'BufAdd'}]
3562 call assert_equal(expected, autocmd_get(#{group: 'TestAutoCmdFns',
3563 \ event: 'BufAdd'}))
3564
3565 " Test for getting the autocmds for all the events in a group for particular
3566 " pattern
3567 call assert_equal([{'cmd': 'echo "bufadd-py"', 'group': 'TestAutoCmdFns',
3568 \ 'pattern': '*.py', 'nested': v:false, 'once': v:false,
3569 \ 'event': 'BufAdd'}],
3570 \ autocmd_get(#{group: 'TestAutoCmdFns', event: '*', pattern: '*.py'}))
3571
3572 " Test for getting the autocmds for an events in a group for particular
3573 " pattern
3574 let l = autocmd_get(#{group: 'TestAutoCmdFns', event: 'BufAdd',
3575 \ pattern: '*.vim'})
3576 call assert_equal([
3577 \ #{cmd: 'echo "bufadd-vim"', group: 'TestAutoCmdFns',
3578 \ pattern: '*.vim', nested: v:false, once: v:false,
3579 \ event: 'BufAdd'}], l)
3580
3581 " Test for getting the autocmds for a pattern in a group
3582 let l = autocmd_get(#{group: 'TestAutoCmdFns', pattern: '*.vim'})
3583 call assert_equal([
3584 \ #{cmd: 'echo "bufadd-vim"', group: 'TestAutoCmdFns',
3585 \ pattern: '*.vim', nested: v:false, once: v:false,
3586 \ event: 'BufAdd'},
3587 \ #{cmd: 'echo "bufhidden"', group: 'TestAutoCmdFns',
3588 \ pattern: '*.vim', nested: v:false,
3589 \ once: v:false, event: 'BufHidden'}], l)
3590
3591 " Test for getting the autocmds for a pattern in all the groups
3592 let l = autocmd_get(#{pattern: '*.a1b2c3'})
3593 call assert_equal([{'cmd': 'echo "bufadd-vim-2"', 'group': 'TestAutoCmdFns2',
3594 \ 'pattern': '*.a1b2c3', 'nested': v:false, 'once': v:false,
3595 \ 'event': 'BufRead'}], l)
3596
3597 " Test for getting autocmds for a pattern without any autocmds
3598 call assert_equal([], autocmd_get(#{group: 'TestAutoCmdFns',
3599 \ pattern: '*.abc'}))
3600 call assert_equal([], autocmd_get(#{group: 'TestAutoCmdFns',
3601 \ event: 'BufAdd', pattern: '*.abc'}))
3602 call assert_equal([], autocmd_get(#{group: 'TestAutoCmdFns',
3603 \ event: 'BufWipeout'}))
3604 call assert_fails("call autocmd_get(#{group: 'abc', event: 'BufAdd'})",
3605 \ 'E367:')
3606 let cmd = "echo autocmd_get(#{group: 'TestAutoCmdFns', event: 'abc'})"
3607 call assert_fails(cmd, 'E216:')
3608 call assert_fails("call autocmd_get(#{group: 'abc'})", 'E367:')
3609 call assert_fails("echo autocmd_get(#{event: 'abc'})", 'E216:')
3610
3611 augroup TestAutoCmdFns
3612 au!
3613 augroup END
3614 call assert_equal([], autocmd_get(#{group: 'TestAutoCmdFns'}))
3615
3616 " Test for nested and once autocmds
3617 augroup TestAutoCmdFns
3618 au!
3619 autocmd VimSuspend * ++nested echo "suspend"
3620 autocmd VimResume * ++once echo "resume"
3621 augroup END
3622
3623 let expected = [
3624 \ {'cmd': 'echo "suspend"', 'group': 'TestAutoCmdFns', 'pattern': '*',
3625 \ 'nested': v:true, 'once': v:false, 'event': 'VimSuspend'},
3626 \ {'cmd': 'echo "resume"', 'group': 'TestAutoCmdFns', 'pattern': '*',
3627 \ 'nested': v:false, 'once': v:true, 'event': 'VimResume'}]
3628 call assert_equal(expected, autocmd_get(#{group: 'TestAutoCmdFns'}))
3629
3630 " Test for buffer-local autocmd
3631 augroup TestAutoCmdFns
3632 au!
3633 autocmd TextYankPost <buffer> echo "textyankpost"
3634 augroup END
3635
3636 let expected = [
3637 \ {'cmd': 'echo "textyankpost"', 'group': 'TestAutoCmdFns',
3638 \ 'pattern': '<buffer=' .. bufnr() .. '>', 'nested': v:false,
3639 \ 'once': v:false, 'bufnr': bufnr(), 'event': 'TextYankPost'}]
3640 call assert_equal(expected, autocmd_get(#{group: 'TestAutoCmdFns'}))
3641
3642 augroup TestAutoCmdFns
3643 au!
3644 augroup END
3645 augroup! TestAutoCmdFns
3646 augroup TestAutoCmdFns2
3647 au!
3648 augroup END
3649 augroup! TestAutoCmdFns2
3650
3651 call assert_fails("echo autocmd_get(#{group: []})", 'E730:')
3652 call assert_fails("echo autocmd_get(#{event: {}})", 'E731:')
3653 call assert_fails("echo autocmd_get([])", 'E1206:')
3654endfunc
3655
3656" Test for the autocmd_add() function
3657func Test_autocmd_add()
3658 " Define a single autocmd in a group
3659 call autocmd_add([#{group: 'TestAcSet', event: 'BufAdd', pattern: '*.sh',
3660 \ cmd: 'echo "bufadd"', once: v:true, nested: v:true}])
3661 call assert_equal([#{cmd: 'echo "bufadd"', group: 'TestAcSet',
3662 \ pattern: '*.sh', nested: v:true, once: v:true,
3663 \ event: 'BufAdd'}], autocmd_get(#{group: 'TestAcSet'}))
3664
3665 " Define two autocmds in the same group
3666 call autocmd_delete([#{group: 'TestAcSet'}])
3667 call autocmd_add([#{group: 'TestAcSet', event: 'BufAdd', pattern: '*.sh',
3668 \ cmd: 'echo "bufadd"'},
3669 \ #{group: 'TestAcSet', event: 'BufEnter', pattern: '*.sh',
3670 \ cmd: 'echo "bufenter"'}])
3671 call assert_equal([
3672 \ #{cmd: 'echo "bufadd"', group: 'TestAcSet', pattern: '*.sh',
3673 \ nested: v:false, once: v:false, event: 'BufAdd'},
3674 \ #{cmd: 'echo "bufenter"', group: 'TestAcSet', pattern: '*.sh',
3675 \ nested: v:false, once: v:false, event: 'BufEnter'}],
3676 \ autocmd_get(#{group: 'TestAcSet'}))
3677
3678 " Define a buffer-local autocmd
3679 call autocmd_delete([#{group: 'TestAcSet'}])
3680 call autocmd_add([#{group: 'TestAcSet', event: 'CursorHold',
3681 \ bufnr: bufnr(), cmd: 'echo "cursorhold"'}])
3682 call assert_equal([
3683 \ #{cmd: 'echo "cursorhold"', group: 'TestAcSet',
3684 \ pattern: '<buffer=' .. bufnr() .. '>', nested: v:false,
3685 \ once: v:false, bufnr: bufnr(), event: 'CursorHold'}],
3686 \ autocmd_get(#{group: 'TestAcSet'}))
3687
3688 " Use an invalid buffer number
3689 call autocmd_delete([#{group: 'TestAcSet'}])
3690 call autocmd_add([#{group: 'TestAcSet', event: 'BufEnter',
3691 \ bufnr: -1, cmd: 'echo "bufenter"'}])
3692 let l = [#{group: 'TestAcSet', event: 'BufAdd', bufnr: 9999,
3693 \ cmd: 'echo "bufadd"'}]
3694 call assert_fails("echo autocmd_add(l)", 'E680:')
Yegappan Lakshmanan00e977c2022-06-01 12:31:53 +01003695 let l = [#{group: 'TestAcSet', event: 'BufAdd', bufnr: 9999,
3696 \ pattern: '*.py', cmd: 'echo "bufadd"'}]
3697 call assert_fails("echo autocmd_add(l)", 'E680:')
3698 let l = [#{group: 'TestAcSet', event: 'BufAdd', bufnr: 9999,
3699 \ pattern: ['*.py', '*.c'], cmd: 'echo "bufadd"'}]
3700 call assert_fails("echo autocmd_add(l)", 'E680:')
Yegappan Lakshmanan1755a912022-05-19 10:31:47 +01003701 let l = [#{group: 'TestAcSet', event: 'BufRead', bufnr: [],
3702 \ cmd: 'echo "bufread"'}]
3703 call assert_fails("echo autocmd_add(l)", 'E745:')
3704 call assert_equal([], autocmd_get(#{group: 'TestAcSet'}))
3705
3706 " Add two commands to the same group, event and pattern
3707 call autocmd_delete([#{group: 'TestAcSet'}])
3708 call autocmd_add([#{group: 'TestAcSet', event: 'BufUnload',
3709 \ pattern: 'abc', cmd: 'echo "cmd1"'}])
3710 call autocmd_add([#{group: 'TestAcSet', event: 'BufUnload',
3711 \ pattern: 'abc', cmd: 'echo "cmd2"'}])
3712 call assert_equal([
3713 \ #{cmd: 'echo "cmd1"', group: 'TestAcSet', pattern: 'abc',
3714 \ nested: v:false, once: v:false, event: 'BufUnload'},
3715 \ #{cmd: 'echo "cmd2"', group: 'TestAcSet', pattern: 'abc',
3716 \ nested: v:false, once: v:false, event: 'BufUnload'}],
3717 \ autocmd_get(#{group: 'TestAcSet'}))
3718
3719 " When adding a new autocmd, if the autocmd 'group' is not specified, then
3720 " the current autocmd group should be used.
3721 call autocmd_delete([#{group: 'TestAcSet'}])
3722 augroup TestAcSet
3723 call autocmd_add([#{event: 'BufHidden', pattern: 'abc', cmd: 'echo "abc"'}])
3724 augroup END
3725 call assert_equal([
3726 \ #{cmd: 'echo "abc"', group: 'TestAcSet', pattern: 'abc',
3727 \ nested: v:false, once: v:false, event: 'BufHidden'}],
3728 \ autocmd_get(#{group: 'TestAcSet'}))
3729
Yegappan Lakshmanan971f6822022-05-24 11:40:11 +01003730 " Test for replacing a cmd for an event in a group
3731 call autocmd_delete([#{group: 'TestAcSet'}])
3732 call autocmd_add([#{replace: v:true, group: 'TestAcSet', event: 'BufEnter',
3733 \ pattern: '*.py', cmd: 'echo "bufenter"'}])
3734 call autocmd_add([#{replace: v:true, group: 'TestAcSet', event: 'BufEnter',
3735 \ pattern: '*.py', cmd: 'echo "bufenter"'}])
3736 call assert_equal([
3737 \ #{cmd: 'echo "bufenter"', group: 'TestAcSet', pattern: '*.py',
3738 \ nested: v:false, once: v:false, event: 'BufEnter'}],
3739 \ autocmd_get(#{group: 'TestAcSet'}))
3740
3741 " Test for adding a command for an unsupported autocmd event
Yegappan Lakshmanan1755a912022-05-19 10:31:47 +01003742 let l = [#{group: 'TestAcSet', event: 'abc', pattern: '*.sh',
3743 \ cmd: 'echo "bufadd"'}]
3744 call assert_fails('call autocmd_add(l)', 'E216:')
3745
Yegappan Lakshmanane0ff3a72022-05-27 18:05:33 +01003746 " Test for using a list of events and patterns
3747 call autocmd_delete([#{group: 'TestAcSet'}])
3748 let l = [#{group: 'TestAcSet', event: ['BufEnter', 'BufLeave'],
3749 \ pattern: ['*.py', '*.sh'], cmd: 'echo "bufcmds"'}]
3750 call autocmd_add(l)
3751 call assert_equal([
3752 \ #{cmd: 'echo "bufcmds"', group: 'TestAcSet', pattern: '*.py',
3753 \ nested: v:false, once: v:false, event: 'BufEnter'},
3754 \ #{cmd: 'echo "bufcmds"', group: 'TestAcSet', pattern: '*.sh',
3755 \ nested: v:false, once: v:false, event: 'BufEnter'},
3756 \ #{cmd: 'echo "bufcmds"', group: 'TestAcSet', pattern: '*.py',
3757 \ nested: v:false, once: v:false, event: 'BufLeave'},
3758 \ #{cmd: 'echo "bufcmds"', group: 'TestAcSet', pattern: '*.sh',
3759 \ nested: v:false, once: v:false, event: 'BufLeave'}],
3760 \ autocmd_get(#{group: 'TestAcSet'}))
3761
3762 " Test for invalid values for 'event' item
3763 call autocmd_delete([#{group: 'TestAcSet'}])
3764 let l = [#{group: 'TestAcSet', event: test_null_string(),
3765 \ pattern: "*.py", cmd: 'echo "bufcmds"'}]
3766 call assert_fails('call autocmd_add(l)', 'E928:')
3767 let l = [#{group: 'TestAcSet', event: test_null_list(),
3768 \ pattern: "*.py", cmd: 'echo "bufcmds"'}]
3769 call assert_fails('call autocmd_add(l)', 'E714:')
3770 let l = [#{group: 'TestAcSet', event: {},
3771 \ pattern: "*.py", cmd: 'echo "bufcmds"'}]
3772 call assert_fails('call autocmd_add(l)', 'E777:')
3773 let l = [#{group: 'TestAcSet', event: [{}],
3774 \ pattern: "*.py", cmd: 'echo "bufcmds"'}]
3775 call assert_fails('call autocmd_add(l)', 'E928:')
3776 let l = [#{group: 'TestAcSet', event: [test_null_string()],
3777 \ pattern: "*.py", cmd: 'echo "bufcmds"'}]
3778 call assert_fails('call autocmd_add(l)', 'E928:')
3779 let l = [#{group: 'TestAcSet', event: 'BufEnter,BufLeave',
3780 \ pattern: '*.py', cmd: 'echo "bufcmds"'}]
3781 call assert_fails('call autocmd_add(l)', 'E216:')
3782 let l = [#{group: 'TestAcSet', event: [],
3783 \ pattern: "*.py", cmd: 'echo "bufcmds"'}]
3784 call autocmd_add(l)
3785 let l = [#{group: 'TestAcSet', event: [""],
3786 \ pattern: "*.py", cmd: 'echo "bufcmds"'}]
3787 call assert_fails('call autocmd_add(l)', 'E216:')
3788 let l = [#{group: 'TestAcSet', event: "",
3789 \ pattern: "*.py", cmd: 'echo "bufcmds"'}]
3790 call autocmd_add(l)
3791 call assert_equal([], autocmd_get(#{group: 'TestAcSet'}))
3792
3793 " Test for invalid values for 'pattern' item
3794 let l = [#{group: 'TestAcSet', event: "BufEnter",
3795 \ pattern: test_null_string(), cmd: 'echo "bufcmds"'}]
Yegappan Lakshmanan00e977c2022-06-01 12:31:53 +01003796 call assert_fails('call autocmd_add(l)', 'E928:')
Yegappan Lakshmanane0ff3a72022-05-27 18:05:33 +01003797 let l = [#{group: 'TestAcSet', event: "BufEnter",
3798 \ pattern: test_null_list(), cmd: 'echo "bufcmds"'}]
3799 call assert_fails('call autocmd_add(l)', 'E714:')
3800 let l = [#{group: 'TestAcSet', event: "BufEnter",
3801 \ pattern: {}, cmd: 'echo "bufcmds"'}]
3802 call assert_fails('call autocmd_add(l)', 'E777:')
3803 let l = [#{group: 'TestAcSet', event: "BufEnter",
3804 \ pattern: [{}], cmd: 'echo "bufcmds"'}]
3805 call assert_fails('call autocmd_add(l)', 'E928:')
3806 let l = [#{group: 'TestAcSet', event: "BufEnter",
3807 \ pattern: [test_null_string()], cmd: 'echo "bufcmds"'}]
3808 call assert_fails('call autocmd_add(l)', 'E928:')
3809 let l = [#{group: 'TestAcSet', event: "BufEnter",
3810 \ pattern: [], cmd: 'echo "bufcmds"'}]
3811 call autocmd_add(l)
3812 let l = [#{group: 'TestAcSet', event: "BufEnter",
3813 \ pattern: [""], cmd: 'echo "bufcmds"'}]
3814 call autocmd_add(l)
3815 let l = [#{group: 'TestAcSet', event: "BufEnter",
3816 \ pattern: "", cmd: 'echo "bufcmds"'}]
3817 call autocmd_add(l)
3818 call assert_equal([], autocmd_get(#{group: 'TestAcSet'}))
3819
3820 let l = [#{group: 'TestAcSet', event: 'BufEnter,abc,BufLeave',
3821 \ pattern: '*.py', cmd: 'echo "bufcmds"'}]
3822 call assert_fails('call autocmd_add(l)', 'E216:')
3823
Yegappan Lakshmanan1755a912022-05-19 10:31:47 +01003824 call assert_fails("call autocmd_add({})", 'E1211:')
3825 call assert_equal(v:false, autocmd_add(test_null_list()))
3826 call assert_true(autocmd_add([[]]))
3827 call assert_true(autocmd_add([test_null_dict()]))
3828
3829 augroup TestAcSet
3830 au!
3831 augroup END
3832
3833 call autocmd_add([#{group: 'TestAcSet'}])
3834 call autocmd_add([#{group: 'TestAcSet', event: 'BufAdd'}])
3835 call autocmd_add([#{group: 'TestAcSet', pat: '*.sh'}])
3836 call autocmd_add([#{group: 'TestAcSet', cmd: 'echo "a"'}])
3837 call autocmd_add([#{group: 'TestAcSet', event: 'BufAdd', pat: '*.sh'}])
3838 call autocmd_add([#{group: 'TestAcSet', event: 'BufAdd', cmd: 'echo "a"'}])
3839 call autocmd_add([#{group: 'TestAcSet', pat: '*.sh', cmd: 'echo "a"'}])
3840 call assert_equal([], autocmd_get(#{group: 'TestAcSet'}))
3841
3842 augroup! TestAcSet
3843endfunc
3844
3845" Test for deleting autocmd events and groups
3846func Test_autocmd_delete()
3847 " Delete an event in an autocmd group
3848 augroup TestAcSet
3849 au!
3850 au BufAdd *.sh echo "bufadd"
3851 au BufEnter *.sh echo "bufenter"
3852 augroup END
3853 call autocmd_delete([#{group: 'TestAcSet', event: 'BufAdd'}])
3854 call assert_equal([#{cmd: 'echo "bufenter"', group: 'TestAcSet',
3855 \ pattern: '*.sh', nested: v:false, once: v:false,
3856 \ event: 'BufEnter'}], autocmd_get(#{group: 'TestAcSet'}))
3857
3858 " Delete all the events in an autocmd group
3859 augroup TestAcSet
3860 au BufAdd *.sh echo "bufadd"
3861 augroup END
3862 call autocmd_delete([#{group: 'TestAcSet', event: '*'}])
3863 call assert_equal([], autocmd_get(#{group: 'TestAcSet'}))
3864
3865 " Delete a non-existing autocmd group
3866 call assert_fails("call autocmd_delete([#{group: 'abc'}])", 'E367:')
3867 " Delete a non-existing autocmd event
3868 let l = [#{group: 'TestAcSet', event: 'abc'}]
3869 call assert_fails("call autocmd_delete(l)", 'E216:')
3870 " Delete a non-existing autocmd pattern
3871 let l = [#{group: 'TestAcSet', event: 'BufAdd', pat: 'abc'}]
3872 call assert_true(autocmd_delete(l))
Yegappan Lakshmanan00e977c2022-06-01 12:31:53 +01003873 " Delete an autocmd for a non-existing buffer
3874 let l = [#{event: '*', bufnr: 9999, cmd: 'echo "x"'}]
3875 call assert_fails('call autocmd_delete(l)', 'E680:')
Yegappan Lakshmanan1755a912022-05-19 10:31:47 +01003876
3877 " Delete an autocmd group
3878 augroup TestAcSet
3879 au!
3880 au BufAdd *.sh echo "bufadd"
3881 au BufEnter *.sh echo "bufenter"
3882 augroup END
3883 call autocmd_delete([#{group: 'TestAcSet'}])
3884 call assert_fails("call autocmd_get(#{group: 'TestAcSet'})", 'E367:')
3885
3886 call assert_true(autocmd_delete([[]]))
3887 call assert_true(autocmd_delete([test_null_dict()]))
3888endfunc
3889
Bram Moolenaar8f3c3c62022-10-18 17:05:54 +01003890func Test_autocmd_split_dummy()
3891 " Autocommand trying to split a window containing a dummy buffer.
3892 auto BufReadPre * exe "sbuf " .. expand("<abuf>")
3893 " Avoid the "W11" prompt
3894 au FileChangedShell * let v:fcs_choice = 'reload'
3895 func Xautocmd_changelist()
3896 cal writefile(['Xtestfile2:4:4'], 'Xerr')
3897 edit Xerr
3898 lex 'Xtestfile2:4:4'
3899 endfunc
3900 call Xautocmd_changelist()
Bram Moolenaar53c5c9f2022-10-18 17:25:03 +01003901 " Should get E86, but it doesn't always happen (timing?)
3902 silent! call Xautocmd_changelist()
Bram Moolenaar8f3c3c62022-10-18 17:05:54 +01003903
3904 au! BufReadPre
3905 au! FileChangedShell
3906 delfunc Xautocmd_changelist
3907 bwipe! Xerr
3908 call delete('Xerr')
3909endfunc
3910
Bram Moolenaarbc2b71d2020-02-17 21:33:30 +01003911" vim: shiftwidth=2 sts=2 expandtab