blob: 5d3e0acdbe2d6974632194c435df93f215ee7a45 [file] [log] [blame]
Bram Moolenaar14735512016-03-26 21:00:08 +01001" Tests for autocommands
2
Bram Moolenaar8c64a362018-03-23 22:39:31 +01003source shared.vim
Bram Moolenaar8c5a2782019-08-07 23:07:07 +02004source check.vim
Bram Moolenaarcadbe1b2019-09-22 21:50:09 +02005source term_util.vim
LemonBoy09371822022-04-08 15:18:45 +01006source screendump.vim
Bram Moolenaar6f2465d2022-03-22 18:13:01 +00007import './vim9.vim' as v9
Bram Moolenaar8c64a362018-03-23 22:39:31 +01008
Bram Moolenaar1e115362019-01-09 23:01:02 +01009func s:cleanup_buffers() abort
Bram Moolenaarb3435b02016-09-29 20:54:59 +020010 for bnr in range(1, bufnr('$'))
11 if bufloaded(bnr) && bufnr('%') != bnr
12 execute 'bd! ' . bnr
13 endif
14 endfor
Bram Moolenaar04f62f82017-07-19 18:18:39 +020015endfunc
Bram Moolenaarb3435b02016-09-29 20:54:59 +020016
Bram Moolenaar14735512016-03-26 21:00:08 +010017func Test_vim_did_enter()
18 call assert_false(v:vim_did_enter)
19
20 " This script will never reach the main loop, can't check if v:vim_did_enter
21 " becomes one.
22endfunc
Bram Moolenaar40b1b542016-04-20 20:18:23 +020023
Bram Moolenaar75911162020-07-21 19:44:47 +020024" Test for the CursorHold autocmd
25func Test_CursorHold_autocmd()
26 CheckRunVimInTerminal
Bram Moolenaare1f3ab72022-09-04 21:29:08 +010027 call writefile(['one', 'two', 'three'], 'XoneTwoThree', 'D')
Bram Moolenaar75911162020-07-21 19:44:47 +020028 let before =<< trim END
29 set updatetime=10
Bram Moolenaare7cda972022-08-29 11:02:59 +010030 au CursorHold * call writefile([line('.')], 'XCHoutput', 'a')
Bram Moolenaar75911162020-07-21 19:44:47 +020031 END
Bram Moolenaare1f3ab72022-09-04 21:29:08 +010032 call writefile(before, 'XCHinit', 'D')
Bram Moolenaare7cda972022-08-29 11:02:59 +010033 let buf = RunVimInTerminal('-S XCHinit XoneTwoThree', {})
Bram Moolenaar17f67542020-08-20 18:29:13 +020034 call term_sendkeys(buf, "G")
Bram Moolenaar62cd26a2020-10-11 20:08:44 +020035 call term_wait(buf, 50)
Bram Moolenaar75911162020-07-21 19:44:47 +020036 call term_sendkeys(buf, "gg")
37 call term_wait(buf)
Bram Moolenaare7cda972022-08-29 11:02:59 +010038 call WaitForAssert({-> assert_equal(['1'], readfile('XCHoutput')[-1:-1])})
Bram Moolenaar75911162020-07-21 19:44:47 +020039 call term_sendkeys(buf, "j")
40 call term_wait(buf)
Bram Moolenaare7cda972022-08-29 11:02:59 +010041 call WaitForAssert({-> assert_equal(['1', '2'], readfile('XCHoutput')[-2:-1])})
Bram Moolenaar75911162020-07-21 19:44:47 +020042 call term_sendkeys(buf, "j")
43 call term_wait(buf)
Bram Moolenaare7cda972022-08-29 11:02:59 +010044 call WaitForAssert({-> assert_equal(['1', '2', '3'], readfile('XCHoutput')[-3:-1])})
Bram Moolenaar75911162020-07-21 19:44:47 +020045 call StopVimInTerminal(buf)
46
Bram Moolenaare7cda972022-08-29 11:02:59 +010047 call delete('XCHoutput')
Bram Moolenaar75911162020-07-21 19:44:47 +020048endfunc
49
Bram Moolenaarc67e8922016-05-24 16:07:40 +020050if has('timers')
Bram Moolenaar97b00752019-05-12 13:07:14 +020051
Bram Moolenaarc67e8922016-05-24 16:07:40 +020052 func ExitInsertMode(id)
53 call feedkeys("\<Esc>")
54 endfunc
55
56 func Test_cursorhold_insert()
Bram Moolenaarf18c4db2016-09-08 22:10:06 +020057 " Need to move the cursor.
58 call feedkeys("ggG", "xt")
59
Bram Moolenaarc67e8922016-05-24 16:07:40 +020060 let g:triggered = 0
61 au CursorHoldI * let g:triggered += 1
62 set updatetime=20
Bram Moolenaar92bb83e2021-02-03 23:04:46 +010063 call timer_start(200, 'ExitInsertMode')
Bram Moolenaarc67e8922016-05-24 16:07:40 +020064 call feedkeys('a', 'x!')
Bram Moolenaar3b014be2022-11-13 17:53:46 +000065 sleep 30m
Bram Moolenaarc67e8922016-05-24 16:07:40 +020066 call assert_equal(1, g:triggered)
Bram Moolenaar26d98212019-01-27 22:32:55 +010067 unlet g:triggered
68 au! CursorHoldI
69 set updatetime&
70 endfunc
71
72 func Test_cursorhold_insert_with_timer_interrupt()
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +020073 CheckFeature job
Bram Moolenaar26d98212019-01-27 22:32:55 +010074 " Need to move the cursor.
75 call feedkeys("ggG", "xt")
76
77 " Confirm the timer invoked in exit_cb of the job doesn't disturb
78 " CursorHoldI event.
79 let g:triggered = 0
80 au CursorHoldI * let g:triggered += 1
Bram Moolenaar62cd26a2020-10-11 20:08:44 +020081 set updatetime=100
Bram Moolenaar26d98212019-01-27 22:32:55 +010082 call job_start(has('win32') ? 'cmd /c echo:' : 'echo',
Bram Moolenaar62cd26a2020-10-11 20:08:44 +020083 \ {'exit_cb': {-> timer_start(200, 'ExitInsertMode')}})
Bram Moolenaar26d98212019-01-27 22:32:55 +010084 call feedkeys('a', 'x!')
85 call assert_equal(1, g:triggered)
86 unlet g:triggered
Bram Moolenaare99e8442016-07-26 20:43:40 +020087 au! CursorHoldI
Bram Moolenaaraeac9002016-09-06 22:15:08 +020088 set updatetime&
Bram Moolenaarc67e8922016-05-24 16:07:40 +020089 endfunc
90
91 func Test_cursorhold_insert_ctrl_x()
92 let g:triggered = 0
93 au CursorHoldI * let g:triggered += 1
94 set updatetime=20
95 call timer_start(100, 'ExitInsertMode')
96 " CursorHoldI does not trigger after CTRL-X
97 call feedkeys("a\<C-X>", 'x!')
98 call assert_equal(0, g:triggered)
Bram Moolenaar26d98212019-01-27 22:32:55 +010099 unlet g:triggered
Bram Moolenaare99e8442016-07-26 20:43:40 +0200100 au! CursorHoldI
Bram Moolenaaraeac9002016-09-06 22:15:08 +0200101 set updatetime&
Bram Moolenaarc67e8922016-05-24 16:07:40 +0200102 endfunc
Bram Moolenaar97b00752019-05-12 13:07:14 +0200103
Bram Moolenaar5a9357d2021-10-03 16:22:05 +0100104 func Test_cursorhold_insert_ctrl_g_U()
105 au CursorHoldI * :
106 set updatetime=20
107 new
108 call timer_start(100, { -> feedkeys("\<Left>foo\<Esc>", 't') })
109 call feedkeys("i()\<C-g>U", 'tx!')
110 sleep 200m
111 call assert_equal('(foo)', getline(1))
112 undo
113 call assert_equal('', getline(1))
114
115 bwipe!
116 au! CursorHoldI
117 set updatetime&
118 endfunc
119
Bram Moolenaar97b00752019-05-12 13:07:14 +0200120 func Test_OptionSet_modeline()
121 call test_override('starting', 1)
122 au! OptionSet
123 augroup set_tabstop
124 au OptionSet tabstop call timer_start(1, {-> execute("echo 'Handler called'", "")})
125 augroup END
Bram Moolenaare1f3ab72022-09-04 21:29:08 +0100126 call writefile(['vim: set ts=7 sw=5 :', 'something'], 'XoptionsetModeline', 'D')
Bram Moolenaar97b00752019-05-12 13:07:14 +0200127 set modeline
128 let v:errmsg = ''
129 call assert_fails('split XoptionsetModeline', 'E12:')
130 call assert_equal(7, &ts)
131 call assert_equal('', v:errmsg)
132
133 augroup set_tabstop
134 au!
135 augroup END
136 bwipe!
137 set ts&
Bram Moolenaar97b00752019-05-12 13:07:14 +0200138 call test_override('starting', 0)
139 endfunc
140
141endif "has('timers')
Bram Moolenaar40b1b542016-04-20 20:18:23 +0200142
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200143func Test_bufunload()
Bram Moolenaarc67e8922016-05-24 16:07:40 +0200144 augroup test_bufunload_group
145 autocmd!
146 autocmd BufUnload * call add(s:li, "bufunload")
147 autocmd BufDelete * call add(s:li, "bufdelete")
148 autocmd BufWipeout * call add(s:li, "bufwipeout")
149 augroup END
Bram Moolenaar40b1b542016-04-20 20:18:23 +0200150
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100151 let s:li = []
Bram Moolenaarc67e8922016-05-24 16:07:40 +0200152 new
153 setlocal bufhidden=
154 bunload
155 call assert_equal(["bufunload", "bufdelete"], s:li)
Bram Moolenaar40b1b542016-04-20 20:18:23 +0200156
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100157 let s:li = []
Bram Moolenaarc67e8922016-05-24 16:07:40 +0200158 new
159 setlocal bufhidden=delete
160 bunload
161 call assert_equal(["bufunload", "bufdelete"], s:li)
162
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100163 let s:li = []
Bram Moolenaarc67e8922016-05-24 16:07:40 +0200164 new
165 setlocal bufhidden=unload
166 bwipeout
167 call assert_equal(["bufunload", "bufdelete", "bufwipeout"], s:li)
168
Bram Moolenaare99e8442016-07-26 20:43:40 +0200169 au! test_bufunload_group
Bram Moolenaarc67e8922016-05-24 16:07:40 +0200170 augroup! test_bufunload_group
Bram Moolenaar40b1b542016-04-20 20:18:23 +0200171endfunc
Bram Moolenaar30445cb2016-07-09 15:21:02 +0200172
173" SEGV occurs in older versions. (At least 7.4.2005 or older)
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200174func Test_autocmd_bufunload_with_tabnext()
Bram Moolenaar30445cb2016-07-09 15:21:02 +0200175 tabedit
176 tabfirst
177
178 augroup test_autocmd_bufunload_with_tabnext_group
179 autocmd!
180 autocmd BufUnload <buffer> tabnext
181 augroup END
182
183 quit
184 call assert_equal(2, tabpagenr('$'))
185
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200186 autocmd! test_autocmd_bufunload_with_tabnext_group
Bram Moolenaar30445cb2016-07-09 15:21:02 +0200187 augroup! test_autocmd_bufunload_with_tabnext_group
188 tablast
189 quit
190endfunc
Bram Moolenaarc917da42016-07-19 22:31:36 +0200191
Bram Moolenaar5ed58c72021-01-28 14:24:55 +0100192func Test_argdelete_in_next()
193 au BufNew,BufEnter,BufLeave,BufWinEnter * argdel
194 call assert_fails('next a b', 'E1156:')
195 au! BufNew,BufEnter,BufLeave,BufWinEnter *
196endfunc
197
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200198func Test_autocmd_bufwinleave_with_tabfirst()
Bram Moolenaarf9e687e2016-09-04 21:33:09 +0200199 tabedit
200 augroup sample
201 autocmd!
202 autocmd BufWinLeave <buffer> tabfirst
203 augroup END
204 call setline(1, ['a', 'b', 'c'])
205 edit! a.txt
Bram Moolenaarf18c4db2016-09-08 22:10:06 +0200206 tabclose
Bram Moolenaarf9e687e2016-09-04 21:33:09 +0200207endfunc
208
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200209" SEGV occurs in older versions. (At least 7.4.2321 or older)
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200210func Test_autocmd_bufunload_avoiding_SEGV_01()
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200211 split aa.txt
212 let lastbuf = bufnr('$')
213
214 augroup test_autocmd_bufunload
215 autocmd!
216 exe 'autocmd BufUnload <buffer> ' . (lastbuf + 1) . 'bwipeout!'
217 augroup END
218
Bram Moolenaar28ee8922020-10-28 20:20:00 +0100219 call assert_fails('edit bb.txt', 'E937:')
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200220
221 autocmd! test_autocmd_bufunload
222 augroup! test_autocmd_bufunload
223 bwipe! aa.txt
224 bwipe! bb.txt
225endfunc
226
227" SEGV occurs in older versions. (At least 7.4.2321 or older)
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200228func Test_autocmd_bufunload_avoiding_SEGV_02()
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200229 setlocal buftype=nowrite
230 let lastbuf = bufnr('$')
231
232 augroup test_autocmd_bufunload
233 autocmd!
234 exe 'autocmd BufUnload <buffer> ' . (lastbuf + 1) . 'bwipeout!'
235 augroup END
236
237 normal! i1
238 call assert_fails('edit a.txt', 'E517:')
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200239
240 autocmd! test_autocmd_bufunload
241 augroup! test_autocmd_bufunload
242 bwipe! a.txt
243endfunc
244
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100245func Test_autocmd_dummy_wipeout()
246 " prepare files
Bram Moolenaare1f3ab72022-09-04 21:29:08 +0100247 call writefile([''], 'Xdummywipetest1.txt', 'D')
248 call writefile([''], 'Xdummywipetest2.txt', 'D')
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100249 augroup test_bufunload_group
250 autocmd!
251 autocmd BufUnload * call add(s:li, "bufunload")
252 autocmd BufDelete * call add(s:li, "bufdelete")
253 autocmd BufWipeout * call add(s:li, "bufwipeout")
254 augroup END
255
256 let s:li = []
257 split Xdummywipetest1.txt
258 silent! vimgrep /notmatched/ Xdummywipetest*
259 call assert_equal(["bufunload", "bufwipeout"], s:li)
260
261 bwipeout
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100262 au! test_bufunload_group
263 augroup! test_bufunload_group
264endfunc
265
Bram Moolenaarc917da42016-07-19 22:31:36 +0200266func Test_win_tab_autocmd()
267 let g:record = []
268
269 augroup testing
270 au WinNew * call add(g:record, 'WinNew')
naohiro ono23beefe2021-11-13 12:38:49 +0000271 au WinClosed * call add(g:record, 'WinClosed')
Bram Moolenaarc917da42016-07-19 22:31:36 +0200272 au WinEnter * call add(g:record, 'WinEnter')
273 au WinLeave * call add(g:record, 'WinLeave')
274 au TabNew * call add(g:record, 'TabNew')
Bram Moolenaar12c11d52016-07-19 23:13:03 +0200275 au TabClosed * call add(g:record, 'TabClosed')
Bram Moolenaarc917da42016-07-19 22:31:36 +0200276 au TabEnter * call add(g:record, 'TabEnter')
277 au TabLeave * call add(g:record, 'TabLeave')
278 augroup END
279
280 split
281 tabnew
282 close
283 close
284
285 call assert_equal([
286 \ 'WinLeave', 'WinNew', 'WinEnter',
287 \ 'WinLeave', 'TabLeave', 'WinNew', 'WinEnter', 'TabNew', 'TabEnter',
naohiro ono23beefe2021-11-13 12:38:49 +0000288 \ 'WinLeave', 'TabLeave', 'WinClosed', 'TabClosed', 'WinEnter', 'TabEnter',
289 \ 'WinLeave', 'WinClosed', 'WinEnter'
Bram Moolenaarc917da42016-07-19 22:31:36 +0200290 \ ], g:record)
291
Bram Moolenaar12c11d52016-07-19 23:13:03 +0200292 let g:record = []
293 tabnew somefile
294 tabnext
295 bwipe somefile
296
297 call assert_equal([
298 \ 'WinLeave', 'TabLeave', 'WinNew', 'WinEnter', 'TabNew', 'TabEnter',
299 \ 'WinLeave', 'TabLeave', 'WinEnter', 'TabEnter',
naohiro ono23beefe2021-11-13 12:38:49 +0000300 \ 'WinClosed', 'TabClosed'
Bram Moolenaar12c11d52016-07-19 23:13:03 +0200301 \ ], g:record)
302
Bram Moolenaarc917da42016-07-19 22:31:36 +0200303 augroup testing
304 au!
305 augroup END
306 unlet g:record
307endfunc
Bram Moolenaare99e8442016-07-26 20:43:40 +0200308
LemonBoy09371822022-04-08 15:18:45 +0100309func Test_WinScrolled()
310 CheckRunVimInTerminal
311
312 let lines =<< trim END
zeertzjqd58862d2022-04-12 11:32:48 +0100313 set nowrap scrolloff=0
314 for ii in range(1, 18)
315 call setline(ii, repeat(nr2char(96 + ii), ii * 2))
316 endfor
317 let win_id = win_getid()
318 let g:matched = v:false
319 execute 'au WinScrolled' win_id 'let g:matched = v:true'
320 let g:scrolled = 0
321 au WinScrolled * let g:scrolled += 1
322 au WinScrolled * let g:amatch = str2nr(expand('<amatch>'))
323 au WinScrolled * let g:afile = str2nr(expand('<afile>'))
LemonBoy09371822022-04-08 15:18:45 +0100324 END
Bram Moolenaare1f3ab72022-09-04 21:29:08 +0100325 call writefile(lines, 'Xtest_winscrolled', 'D')
LemonBoy09371822022-04-08 15:18:45 +0100326 let buf = RunVimInTerminal('-S Xtest_winscrolled', {'rows': 6})
327
328 call term_sendkeys(buf, ":echo g:scrolled\<CR>")
329 call WaitForAssert({-> assert_match('^0 ', term_getline(buf, 6))}, 1000)
330
331 " Scroll left/right in Normal mode.
332 call term_sendkeys(buf, "zlzh:echo g:scrolled\<CR>")
333 call WaitForAssert({-> assert_match('^2 ', term_getline(buf, 6))}, 1000)
334
335 " Scroll up/down in Normal mode.
336 call term_sendkeys(buf, "\<c-e>\<c-y>:echo g:scrolled\<CR>")
337 call WaitForAssert({-> assert_match('^4 ', term_getline(buf, 6))}, 1000)
338
339 " Scroll up/down in Insert mode.
340 call term_sendkeys(buf, "Mi\<c-x>\<c-e>\<Esc>i\<c-x>\<c-y>\<Esc>")
341 call term_sendkeys(buf, ":echo g:scrolled\<CR>")
342 call WaitForAssert({-> assert_match('^6 ', term_getline(buf, 6))}, 1000)
343
344 " Scroll the window horizontally to focus the last letter of the third line
345 " containing only six characters. Moving to the previous and shorter lines
346 " should trigger another autocommand as Vim has to make them visible.
347 call term_sendkeys(buf, "5zl2k")
348 call term_sendkeys(buf, ":echo g:scrolled\<CR>")
349 call WaitForAssert({-> assert_match('^8 ', term_getline(buf, 6))}, 1000)
350
351 " Ensure the command was triggered for the specified window ID.
352 call term_sendkeys(buf, ":echo g:matched\<CR>")
353 call WaitForAssert({-> assert_match('^v:true ', term_getline(buf, 6))}, 1000)
354
355 " Ensure the expansion of <amatch> and <afile> matches the window ID.
356 call term_sendkeys(buf, ":echo g:amatch == win_id && g:afile == win_id\<CR>")
357 call WaitForAssert({-> assert_match('^v:true ', term_getline(buf, 6))}, 1000)
358
359 call StopVimInTerminal(buf)
LemonBoy09371822022-04-08 15:18:45 +0100360endfunc
361
LemonBoy66e13ae2022-04-21 22:52:11 +0100362func Test_WinScrolled_mouse()
363 CheckRunVimInTerminal
364
365 let lines =<< trim END
366 set nowrap scrolloff=0
367 set mouse=a term=xterm ttymouse=sgr mousetime=200 clipboard=
368 call setline(1, ['foo']->repeat(32))
369 split
370 let g:scrolled = 0
371 au WinScrolled * let g:scrolled += 1
372 END
Bram Moolenaare1f3ab72022-09-04 21:29:08 +0100373 call writefile(lines, 'Xtest_winscrolled_mouse', 'D')
LemonBoy66e13ae2022-04-21 22:52:11 +0100374 let buf = RunVimInTerminal('-S Xtest_winscrolled_mouse', {'rows': 10})
375
376 " With the upper split focused, send a scroll-down event to the unfocused one.
377 call test_setmouse(7, 1)
378 call term_sendkeys(buf, "\<ScrollWheelDown>")
379 call TermWait(buf)
380 call term_sendkeys(buf, ":echo g:scrolled\<CR>")
381 call WaitForAssert({-> assert_match('^1', term_getline(buf, 10))}, 1000)
382
383 " Again, but this time while we're in insert mode.
384 call term_sendkeys(buf, "i\<ScrollWheelDown>\<Esc>")
385 call TermWait(buf)
386 call term_sendkeys(buf, ":echo g:scrolled\<CR>")
387 call WaitForAssert({-> assert_match('^2', term_getline(buf, 10))}, 1000)
388
389 call StopVimInTerminal(buf)
LemonBoy66e13ae2022-04-21 22:52:11 +0100390endfunc
391
zeertzjqd58862d2022-04-12 11:32:48 +0100392func Test_WinScrolled_close_curwin()
393 CheckRunVimInTerminal
394
395 let lines =<< trim END
396 set nowrap scrolloff=0
397 call setline(1, ['aaa', 'bbb'])
398 vsplit
399 au WinScrolled * close
400 au VimLeave * call writefile(['123456'], 'Xtestout')
401 END
Bram Moolenaare1f3ab72022-09-04 21:29:08 +0100402 call writefile(lines, 'Xtest_winscrolled_close_curwin', 'D')
zeertzjqd58862d2022-04-12 11:32:48 +0100403 let buf = RunVimInTerminal('-S Xtest_winscrolled_close_curwin', {'rows': 6})
404
405 " This was using freed memory
406 call term_sendkeys(buf, "\<C-E>")
407 call TermWait(buf)
408 call StopVimInTerminal(buf)
409
Bram Moolenaar0a60f792022-11-19 21:18:11 +0000410 " check the startup script finished to the end
zeertzjqd58862d2022-04-12 11:32:48 +0100411 call assert_equal(['123456'], readfile('Xtestout'))
zeertzjqd58862d2022-04-12 11:32:48 +0100412 call delete('Xtestout')
413endfunc
414
Bram Moolenaar0a60f792022-11-19 21:18:11 +0000415func Test_WinScrolled_once_only()
416 CheckRunVimInTerminal
417
418 let lines =<< trim END
419 set cmdheight=2
420 call setline(1, ['aaa', 'bbb'])
421 let trigger_count = 0
422 func ShowInfo(id)
423 echo g:trigger_count g:winid winlayout()
424 endfunc
425
426 vsplit
427 split
428 " use a timer to show the info after a redraw
429 au WinScrolled * let trigger_count += 1 | let winid = expand('<amatch>') | call timer_start(100, 'ShowInfo')
430 wincmd j
431 wincmd l
432 END
433 call writefile(lines, 'Xtest_winscrolled_once', 'D')
434 let buf = RunVimInTerminal('-S Xtest_winscrolled_once', #{rows: 10, cols: 60, statusoff: 2})
435
436 call term_sendkeys(buf, "\<C-E>")
437 call VerifyScreenDump(buf, 'Test_winscrolled_once_only_1', {})
438
439 call StopVimInTerminal(buf)
440endfunc
441
zeertzjq670ab032022-08-28 19:16:15 +0100442func Test_WinScrolled_long_wrapped()
443 CheckRunVimInTerminal
444
445 let lines =<< trim END
446 set scrolloff=0
447 let height = winheight(0)
448 let width = winwidth(0)
449 let g:scrolled = 0
450 au WinScrolled * let g:scrolled += 1
451 call setline(1, repeat('foo', height * width))
452 call cursor(1, height * width)
453 END
Bram Moolenaare1f3ab72022-09-04 21:29:08 +0100454 call writefile(lines, 'Xtest_winscrolled_long_wrapped', 'D')
zeertzjq670ab032022-08-28 19:16:15 +0100455 let buf = RunVimInTerminal('-S Xtest_winscrolled_long_wrapped', {'rows': 6})
456
457 call term_sendkeys(buf, ":echo g:scrolled\<CR>")
458 call WaitForAssert({-> assert_match('^0 ', term_getline(buf, 6))}, 1000)
459
460 call term_sendkeys(buf, 'gj')
461 call term_sendkeys(buf, ":echo g:scrolled\<CR>")
462 call WaitForAssert({-> assert_match('^1 ', term_getline(buf, 6))}, 1000)
463
464 call term_sendkeys(buf, '0')
465 call term_sendkeys(buf, ":echo g:scrolled\<CR>")
466 call WaitForAssert({-> assert_match('^2 ', term_getline(buf, 6))}, 1000)
467
468 call term_sendkeys(buf, '$')
469 call term_sendkeys(buf, ":echo g:scrolled\<CR>")
470 call WaitForAssert({-> assert_match('^3 ', term_getline(buf, 6))}, 1000)
zeertzjq670ab032022-08-28 19:16:15 +0100471endfunc
472
naohiro ono23beefe2021-11-13 12:38:49 +0000473func Test_WinClosed()
474 " Test that the pattern is matched against the closed window's ID, and both
475 " <amatch> and <afile> are set to it.
476 new
477 let winid = win_getid()
478 let g:matched = v:false
479 augroup test-WinClosed
480 autocmd!
481 execute 'autocmd WinClosed' winid 'let g:matched = v:true'
482 autocmd WinClosed * let g:amatch = str2nr(expand('<amatch>'))
483 autocmd WinClosed * let g:afile = str2nr(expand('<afile>'))
484 augroup END
485 close
486 call assert_true(g:matched)
487 call assert_equal(winid, g:amatch)
488 call assert_equal(winid, g:afile)
489
490 " Test that WinClosed is non-recursive.
491 new
492 new
493 call assert_equal(3, winnr('$'))
494 let g:triggered = 0
495 augroup test-WinClosed
496 autocmd!
497 autocmd WinClosed * let g:triggered += 1
498 autocmd WinClosed * 2 wincmd c
499 augroup END
500 close
501 call assert_equal(1, winnr('$'))
502 call assert_equal(1, g:triggered)
503
504 autocmd! test-WinClosed
505 augroup! test-WinClosed
506 unlet g:matched
507 unlet g:amatch
508 unlet g:afile
509 unlet g:triggered
510endfunc
511
Bram Moolenaarc947b9a2022-04-06 17:59:21 +0100512func Test_WinClosed_throws()
513 vnew
514 let bnr = bufnr()
515 call assert_equal(1, bufloaded(bnr))
516 augroup test-WinClosed
517 autocmd WinClosed * throw 'foo'
518 augroup END
519 try
520 close
521 catch /.*/
522 endtry
523 call assert_equal(0, bufloaded(bnr))
524
525 autocmd! test-WinClosed
526 augroup! test-WinClosed
527endfunc
528
zeertzjq6a069402022-04-07 14:08:29 +0100529func Test_WinClosed_throws_with_tabs()
530 tabnew
531 let bnr = bufnr()
532 call assert_equal(1, bufloaded(bnr))
533 augroup test-WinClosed
534 autocmd WinClosed * throw 'foo'
535 augroup END
536 try
537 close
538 catch /.*/
539 endtry
540 call assert_equal(0, bufloaded(bnr))
541
542 autocmd! test-WinClosed
543 augroup! test-WinClosed
544endfunc
545
zeertzjq62de54b2022-09-22 18:08:37 +0100546" This used to trigger WinClosed twice for the same window, and the window's
547" buffer was NULL in the second autocommand.
548func Test_WinClosed_switch_tab()
549 edit Xa
550 split Xb
551 split Xc
552 tab split
553 new
554 augroup test-WinClosed
555 autocmd WinClosed * tabprev | bwipe!
556 augroup END
557 close
558 " Check that the tabline has been fully removed
559 call assert_equal([1, 1], win_screenpos(0))
560
561 autocmd! test-WinClosed
562 augroup! test-WinClosed
563 %bwipe!
564endfunc
565
Bram Moolenaare99e8442016-07-26 20:43:40 +0200566func s:AddAnAutocmd()
567 augroup vimBarTest
568 au BufReadCmd * echo 'hello'
569 augroup END
570 call assert_equal(3, len(split(execute('au vimBarTest'), "\n")))
571endfunc
572
573func Test_early_bar()
574 " test that a bar is recognized before the {event}
575 call s:AddAnAutocmd()
Bram Moolenaarb8e642f2021-11-20 10:38:25 +0000576 augroup vimBarTest | au! | let done = 77 | augroup END
Bram Moolenaare99e8442016-07-26 20:43:40 +0200577 call assert_equal(1, len(split(execute('au vimBarTest'), "\n")))
Bram Moolenaarb8e642f2021-11-20 10:38:25 +0000578 call assert_equal(77, done)
Bram Moolenaare99e8442016-07-26 20:43:40 +0200579
580 call s:AddAnAutocmd()
Bram Moolenaarb8e642f2021-11-20 10:38:25 +0000581 augroup vimBarTest| au!| let done = 88 | augroup END
Bram Moolenaare99e8442016-07-26 20:43:40 +0200582 call assert_equal(1, len(split(execute('au vimBarTest'), "\n")))
Bram Moolenaarb8e642f2021-11-20 10:38:25 +0000583 call assert_equal(88, done)
Bram Moolenaare99e8442016-07-26 20:43:40 +0200584
585 " test that a bar is recognized after the {event}
586 call s:AddAnAutocmd()
Bram Moolenaarb8e642f2021-11-20 10:38:25 +0000587 augroup vimBarTest| au!BufReadCmd| let done = 99 | augroup END
Bram Moolenaare99e8442016-07-26 20:43:40 +0200588 call assert_equal(1, len(split(execute('au vimBarTest'), "\n")))
Bram Moolenaarb8e642f2021-11-20 10:38:25 +0000589 call assert_equal(99, done)
Bram Moolenaare99e8442016-07-26 20:43:40 +0200590
591 " test that a bar is recognized after the {group}
592 call s:AddAnAutocmd()
593 au! vimBarTest|echo 'hello'
594 call assert_equal(1, len(split(execute('au vimBarTest'), "\n")))
595endfunc
Bram Moolenaarf2c4c392016-07-29 20:50:24 +0200596
Bram Moolenaar5c809082016-09-01 16:21:48 +0200597func RemoveGroup()
598 autocmd! StartOK
599 augroup! StartOK
600endfunc
601
Bram Moolenaarf2c4c392016-07-29 20:50:24 +0200602func Test_augroup_warning()
603 augroup TheWarning
604 au VimEnter * echo 'entering'
605 augroup END
Bram Moolenaar5dc4e2f2020-11-25 14:15:12 +0100606 call assert_match("TheWarning.*VimEnter", execute('au VimEnter'))
Bram Moolenaarf2c4c392016-07-29 20:50:24 +0200607 redir => res
608 augroup! TheWarning
609 redir END
Bram Moolenaar5dc4e2f2020-11-25 14:15:12 +0100610 call assert_match("W19:", res)
611 call assert_match("-Deleted-.*VimEnter", execute('au VimEnter'))
Bram Moolenaarf2c4c392016-07-29 20:50:24 +0200612
613 " check "Another" does not take the pace of the deleted entry
614 augroup Another
615 augroup END
Bram Moolenaar5dc4e2f2020-11-25 14:15:12 +0100616 call assert_match("-Deleted-.*VimEnter", execute('au VimEnter'))
Bram Moolenaaraeac9002016-09-06 22:15:08 +0200617 augroup! Another
Bram Moolenaar5c809082016-09-01 16:21:48 +0200618
619 " no warning for postpone aucmd delete
620 augroup StartOK
621 au VimEnter * call RemoveGroup()
622 augroup END
Bram Moolenaar5dc4e2f2020-11-25 14:15:12 +0100623 call assert_match("StartOK.*VimEnter", execute('au VimEnter'))
Bram Moolenaar5c809082016-09-01 16:21:48 +0200624 redir => res
625 doautocmd VimEnter
626 redir END
Bram Moolenaar5dc4e2f2020-11-25 14:15:12 +0100627 call assert_notmatch("W19:", res)
Bram Moolenaarde653f02016-09-03 16:59:06 +0200628 au! VimEnter
Bram Moolenaarad48e6c2020-04-21 22:19:45 +0200629
630 call assert_fails('augroup!', 'E471:')
Bram Moolenaarf2c4c392016-07-29 20:50:24 +0200631endfunc
Bram Moolenaarb62cc362016-09-03 16:43:53 +0200632
Bram Moolenaar8d84ff12017-10-26 16:42:16 +0200633func Test_BufReadCmdHelp()
634 " This used to cause access to free memory
635 au BufReadCmd * e +h
636 help
637
Bram Moolenaar8d84ff12017-10-26 16:42:16 +0200638 au! BufReadCmd
639endfunc
640
641func Test_BufReadCmdHelpJump()
642 " This used to cause access to free memory
643 au BufReadCmd * e +h{
Bram Moolenaarcf1ba352017-10-27 00:55:04 +0200644 " } to fix highlighting
645 call assert_fails('help', 'E434:')
Bram Moolenaar8d84ff12017-10-26 16:42:16 +0200646
Bram Moolenaar8d84ff12017-10-26 16:42:16 +0200647 au! BufReadCmd
648endfunc
649
zeertzjq93f72cc2022-08-26 15:34:52 +0100650" BufReadCmd is triggered for a "nofile" buffer. Check all values.
Bram Moolenaarb1d2c812022-08-26 11:55:01 +0100651func Test_BufReadCmdNofile()
zeertzjq93f72cc2022-08-26 15:34:52 +0100652 for val in ['nofile',
653 \ 'nowrite',
654 \ 'acwrite',
655 \ 'quickfix',
656 \ 'help',
657 \ 'terminal',
658 \ 'prompt',
659 \ 'popup',
660 \ ]
661 new somefile
662 exe 'set buftype=' .. val
663 au BufReadCmd somefile call setline(1, 'triggered')
664 edit
665 call assert_equal('triggered', getline(1))
Bram Moolenaarb1d2c812022-08-26 11:55:01 +0100666
zeertzjq93f72cc2022-08-26 15:34:52 +0100667 au! BufReadCmd
668 bwipe!
669 endfor
Bram Moolenaarb1d2c812022-08-26 11:55:01 +0100670endfunc
671
Bram Moolenaarb62cc362016-09-03 16:43:53 +0200672func Test_augroup_deleted()
Bram Moolenaarde653f02016-09-03 16:59:06 +0200673 " This caused a crash before E936 was introduced
Bram Moolenaarb62cc362016-09-03 16:43:53 +0200674 augroup x
Bram Moolenaarde653f02016-09-03 16:59:06 +0200675 call assert_fails('augroup! x', 'E936:')
676 au VimEnter * echo
677 augroup end
Bram Moolenaarb62cc362016-09-03 16:43:53 +0200678 augroup! x
Bram Moolenaar5dc4e2f2020-11-25 14:15:12 +0100679 call assert_match("-Deleted-.*VimEnter", execute('au VimEnter'))
Bram Moolenaarde653f02016-09-03 16:59:06 +0200680 au! VimEnter
Bram Moolenaarb62cc362016-09-03 16:43:53 +0200681endfunc
682
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200683" Tests for autocommands on :close command.
684" This used to be in test13.
685func Test_three_windows()
Bram Moolenaarb3435b02016-09-29 20:54:59 +0200686 " Clean up buffers, because in some cases this function fails.
687 call s:cleanup_buffers()
688
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200689 " Write three files and open them, each in a window.
690 " Then go to next window, with autocommand that deletes the previous one.
691 " Do this twice, writing the file.
692 e! Xtestje1
693 call setline(1, 'testje1')
694 w
695 sp Xtestje2
696 call setline(1, 'testje2')
697 w
698 sp Xtestje3
699 call setline(1, 'testje3')
700 w
701 wincmd w
702 au WinLeave Xtestje2 bwipe
703 wincmd w
704 call assert_equal('Xtestje1', expand('%'))
705
706 au WinLeave Xtestje1 bwipe Xtestje3
707 close
708 call assert_equal('Xtestje1', expand('%'))
709
710 " Test deleting the buffer on a Unload event. If this goes wrong there
711 " will be the ATTENTION prompt.
712 e Xtestje1
713 au!
714 au! BufUnload Xtestje1 bwipe
715 call assert_fails('e Xtestje3', 'E937:')
716 call assert_equal('Xtestje3', expand('%'))
717
718 e Xtestje2
719 sp Xtestje1
720 call assert_fails('e', 'E937:')
Bram Moolenaara997b452018-04-17 23:24:06 +0200721 call assert_equal('Xtestje1', expand('%'))
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200722
723 " Test changing buffers in a BufWipeout autocommand. If this goes wrong
724 " there are ml_line errors and/or a Crash.
725 au!
726 only
727 e Xanother
728 e Xtestje1
729 bwipe Xtestje2
730 bwipe Xtestje3
731 au BufWipeout Xtestje1 buf Xtestje1
732 bwipe
733 call assert_equal('Xanother', expand('%'))
734
735 only
736 help
737 wincmd w
738 1quit
739 call assert_equal('Xanother', expand('%'))
740
741 au!
Bram Moolenaar4520d442017-03-19 16:09:46 +0100742 enew
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200743 call delete('Xtestje1')
744 call delete('Xtestje2')
745 call delete('Xtestje3')
746endfunc
Bram Moolenaare13b9af2017-01-13 22:01:02 +0100747
748func Test_BufEnter()
749 au! BufEnter
750 au Bufenter * let val = val . '+'
751 let g:val = ''
752 split NewFile
753 call assert_equal('+', g:val)
754 bwipe!
755 call assert_equal('++', g:val)
756
757 " Also get BufEnter when editing a directory
Bram Moolenaar6f14da12022-09-07 21:30:44 +0100758 call mkdir('Xbufenterdir', 'D')
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +0100759 split Xbufenterdir
Bram Moolenaare13b9af2017-01-13 22:01:02 +0100760 call assert_equal('+++', g:val)
Bram Moolenaare94260f2017-03-21 15:50:12 +0100761
762 " On MS-Windows we can't edit the directory, make sure we wipe the right
763 " buffer.
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +0100764 bwipe! Xbufenterdir
Bram Moolenaare13b9af2017-01-13 22:01:02 +0100765 au! BufEnter
Bram Moolenaara9b5b852022-08-26 13:16:20 +0100766
767 " Editing a "nofile" buffer doesn't read the file but does trigger BufEnter
zeertzjq93f72cc2022-08-26 15:34:52 +0100768 " for historic reasons. Also test other 'buftype' values.
769 for val in ['nofile',
770 \ 'nowrite',
771 \ 'acwrite',
772 \ 'quickfix',
773 \ 'help',
774 \ 'terminal',
775 \ 'prompt',
776 \ 'popup',
777 \ ]
778 new somefile
779 exe 'set buftype=' .. val
780 au BufEnter somefile call setline(1, 'some text')
781 edit
782 call assert_equal('some text', getline(1))
783 bwipe!
784 au! BufEnter
785 endfor
Bram Moolenaar9fda8152022-11-19 13:14:10 +0000786
787 new
788 new
789 autocmd BufEnter * ++once close
790 call assert_fails('close', 'E1312:')
791
792 au! BufEnter
793 only
Bram Moolenaare13b9af2017-01-13 22:01:02 +0100794endfunc
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100795
796" Closing a window might cause an endless loop
797" E814 for older Vims
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200798func Test_autocmd_bufwipe_in_SessLoadPost()
Bram Moolenaar1d68d9b2017-10-13 22:33:32 +0200799 edit Xtest
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100800 tabnew
Bram Moolenaar1d68d9b2017-10-13 22:33:32 +0200801 file Xsomething
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100802 set noswapfile
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100803 mksession!
804
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200805 let content =<< trim [CODE]
Bram Moolenaar62cd26a2020-10-11 20:08:44 +0200806 call test_override('ui_delay', 10)
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200807 set nocp noswapfile
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100808 let v:swapchoice = "e"
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200809 augroup test_autocmd_sessionload
810 autocmd!
811 autocmd SessionLoadPost * exe bufnr("Xsomething") . "bw!"
812 augroup END
813
814 func WriteErrors()
Bram Moolenaarbe9fc5b2022-09-09 17:09:35 +0100815 call writefile([execute("messages")], "XerrorsBwipe")
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200816 endfunc
817 au VimLeave * call WriteErrors()
818 [CODE]
819
Bram Moolenaare1f3ab72022-09-04 21:29:08 +0100820 call writefile(content, 'Xvimrc', 'D')
Bram Moolenaar93344c22019-08-14 21:12:05 +0200821 call system(GetVimCommand('Xvimrc') .. ' --not-a-term --noplugins -S Session.vim -c cq')
Bram Moolenaarae04a602022-09-09 15:08:10 +0100822 sleep 100m
Bram Moolenaarbe9fc5b2022-09-09 17:09:35 +0100823 let errors = join(readfile('XerrorsBwipe'))
Bram Moolenaare2e40752020-09-04 21:18:46 +0200824 call assert_match('E814:', errors)
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100825
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100826 set swapfile
Bram Moolenaarbe9fc5b2022-09-09 17:09:35 +0100827 for file in ['Session.vim', 'XerrorsBwipe']
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100828 call delete(file)
829 endfor
830endfunc
831
Bram Moolenaar797e63b2021-01-15 16:22:52 +0100832" Using :blast and :ball for many events caused a crash, because b_nwindows was
833" not incremented correctly.
834func Test_autocmd_blast_badd()
835 let content =<< trim [CODE]
836 au BufNew,BufAdd,BufWinEnter,BufEnter,BufLeave,BufWinLeave,BufUnload,VimEnter foo* blast
837 edit foo1
838 au BufNew,BufAdd,BufWinEnter,BufEnter,BufLeave,BufWinLeave,BufUnload,VimEnter foo* ball
839 edit foo2
Bram Moolenaarbe9fc5b2022-09-09 17:09:35 +0100840 call writefile(['OK'], 'XerrorsBlast')
Bram Moolenaar797e63b2021-01-15 16:22:52 +0100841 qall
842 [CODE]
843
Bram Moolenaare1f3ab72022-09-04 21:29:08 +0100844 call writefile(content, 'XblastBall', 'D')
Bram Moolenaar797e63b2021-01-15 16:22:52 +0100845 call system(GetVimCommand() .. ' --clean -S XblastBall')
Bram Moolenaarae04a602022-09-09 15:08:10 +0100846 sleep 100m
Bram Moolenaarbe9fc5b2022-09-09 17:09:35 +0100847 call assert_match('OK', readfile('XerrorsBlast')->join())
Bram Moolenaar797e63b2021-01-15 16:22:52 +0100848
Bram Moolenaarbe9fc5b2022-09-09 17:09:35 +0100849 call delete('XerrorsBlast')
Bram Moolenaar797e63b2021-01-15 16:22:52 +0100850endfunc
851
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100852" SEGV occurs in older versions.
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200853func Test_autocmd_bufwipe_in_SessLoadPost2()
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100854 tabnew
855 set noswapfile
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100856 mksession!
857
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200858 let content =<< trim [CODE]
859 set nocp noswapfile
860 function! DeleteInactiveBufs()
861 tabfirst
862 let tabblist = []
863 for i in range(1, tabpagenr(''$''))
864 call extend(tabblist, tabpagebuflist(i))
865 endfor
866 for b in range(1, bufnr(''$''))
867 if bufexists(b) && buflisted(b) && (index(tabblist, b) == -1 || bufname(b) =~# ''^$'')
868 exec ''bwipeout '' . b
869 endif
870 endfor
871 echomsg "SessionLoadPost DONE"
872 endfunction
873 au SessionLoadPost * call DeleteInactiveBufs()
874
875 func WriteErrors()
Bram Moolenaarbe9fc5b2022-09-09 17:09:35 +0100876 call writefile([execute("messages")], "XerrorsPost")
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200877 endfunc
878 au VimLeave * call WriteErrors()
879 [CODE]
880
Bram Moolenaare1f3ab72022-09-04 21:29:08 +0100881 call writefile(content, 'Xvimrc', 'D')
Bram Moolenaar93344c22019-08-14 21:12:05 +0200882 call system(GetVimCommand('Xvimrc') .. ' --not-a-term --noplugins -S Session.vim -c cq')
Bram Moolenaarae04a602022-09-09 15:08:10 +0100883 sleep 100m
Bram Moolenaarbe9fc5b2022-09-09 17:09:35 +0100884 let errors = join(readfile('XerrorsPost'))
Bram Moolenaare94260f2017-03-21 15:50:12 +0100885 " This probably only ever matches on unix.
886 call assert_notmatch('Caught deadly signal SEGV', errors)
887 call assert_match('SessionLoadPost DONE', errors)
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100888
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100889 set swapfile
Bram Moolenaarbe9fc5b2022-09-09 17:09:35 +0100890 for file in ['Session.vim', 'XerrorsPost']
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100891 call delete(file)
892 endfor
893endfunc
Bram Moolenaarfaf29d72017-07-09 11:07:16 +0200894
895func Test_empty_doau()
896 doau \|
897endfunc
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200898
899func s:AutoCommandOptionSet(match)
Bram Moolenaard7c96872019-06-15 17:12:48 +0200900 let template = "Option: <%s>, OldVal: <%s>, OldValLocal: <%s>, OldValGlobal: <%s>, NewVal: <%s>, Scope: <%s>, Command: <%s>\n"
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200901 let item = remove(g:options, 0)
Bram Moolenaard7c96872019-06-15 17:12:48 +0200902 let expected = printf(template, item[0], item[1], item[2], item[3], item[4], item[5], item[6])
903 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 +0200904 let g:opt = [expected, actual]
905 "call assert_equal(expected, actual)
906endfunc
907
908func Test_OptionSet()
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +0200909 CheckOption autochdir
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200910
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +0200911 badd test_autocmd.vim
912
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200913 call test_override('starting', 1)
914 set nocp
915 au OptionSet * :call s:AutoCommandOptionSet(expand("<amatch>"))
916
917 " 1: Setting number option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100918 let g:options = [['number', 0, 0, 0, 1, 'global', 'set']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200919 set nu
920 call assert_equal([], g:options)
921 call assert_equal(g:opt[0], g:opt[1])
922
923 " 2: Setting local number option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100924 let g:options = [['number', 1, 1, '', 0, 'local', 'setlocal']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200925 setlocal nonu
926 call assert_equal([], g:options)
927 call assert_equal(g:opt[0], g:opt[1])
928
929 " 3: Setting global number option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100930 let g:options = [['number', 1, '', 1, 0, 'global', 'setglobal']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200931 setglobal nonu
932 call assert_equal([], g:options)
933 call assert_equal(g:opt[0], g:opt[1])
934
935 " 4: Setting local autoindent option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100936 let g:options = [['autoindent', 0, 0, '', 1, 'local', 'setlocal']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200937 setlocal ai
938 call assert_equal([], g:options)
939 call assert_equal(g:opt[0], g:opt[1])
940
941 " 5: Setting global autoindent option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100942 let g:options = [['autoindent', 0, '', 0, 1, 'global', 'setglobal']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200943 setglobal ai
944 call assert_equal([], g:options)
945 call assert_equal(g:opt[0], g:opt[1])
946
947 " 6: Setting global autoindent option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100948 let g:options = [['autoindent', 1, 1, 1, 0, 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +0200949 set ai!
950 call assert_equal([], g:options)
951 call assert_equal(g:opt[0], g:opt[1])
952
953 " 6a: Setting global autoindent option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100954 let g:options = [['autoindent', 1, 1, 0, 0, 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +0200955 noa setlocal ai
956 noa setglobal noai
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200957 set ai!
958 call assert_equal([], g:options)
959 call assert_equal(g:opt[0], g:opt[1])
960
961 " Should not print anything, use :noa
962 " 7: don't trigger OptionSet"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100963 let g:options = [['invalid', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200964 noa set nonu
Bram Moolenaard7c96872019-06-15 17:12:48 +0200965 call assert_equal([['invalid', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid']], g:options)
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200966 call assert_equal(g:opt[0], g:opt[1])
967
968 " 8: Setting several global list and number option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100969 let g:options = [['list', 0, 0, 0, 1, 'global', 'set'], ['number', 0, 0, 0, 1, 'global', 'set']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200970 set list nu
971 call assert_equal([], g:options)
972 call assert_equal(g:opt[0], g:opt[1])
973
974 " 9: don't trigger OptionSet"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100975 let g:options = [['invalid', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid'], ['invalid', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200976 noa set nolist nonu
Bram Moolenaard7c96872019-06-15 17:12:48 +0200977 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 +0200978 call assert_equal(g:opt[0], g:opt[1])
979
980 " 10: Setting global acd"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100981 let g:options = [['autochdir', 0, 0, '', 1, 'local', 'setlocal']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200982 setlocal acd
983 call assert_equal([], g:options)
984 call assert_equal(g:opt[0], g:opt[1])
985
986 " 11: Setting global autoread (also sets local value)"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100987 let g:options = [['autoread', 0, 0, 0, 1, 'global', 'set']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200988 set ar
989 call assert_equal([], g:options)
990 call assert_equal(g:opt[0], g:opt[1])
991
992 " 12: Setting local autoread"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100993 let g:options = [['autoread', 1, 1, '', 1, 'local', 'setlocal']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200994 setlocal ar
995 call assert_equal([], g:options)
996 call assert_equal(g:opt[0], g:opt[1])
997
998 " 13: Setting global autoread"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100999 let g:options = [['autoread', 1, '', 1, 0, 'global', 'setglobal']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001000 setglobal invar
1001 call assert_equal([], g:options)
1002 call assert_equal(g:opt[0], g:opt[1])
1003
1004 " 14: Setting option backspace through :let"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001005 let g:options = [['backspace', '', '', '', 'eol,indent,start', 'global', 'set']]
1006 let &bs = "eol,indent,start"
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001007 call assert_equal([], g:options)
1008 call assert_equal(g:opt[0], g:opt[1])
1009
1010 " 15: Setting option backspace through setbufvar()"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001011 let g:options = [['backup', 0, 0, '', 1, 'local', 'setlocal']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001012 " try twice, first time, shouldn't trigger because option name is invalid,
1013 " second time, it should trigger
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +02001014 let bnum = bufnr('%')
Bram Moolenaare2e40752020-09-04 21:18:46 +02001015 call assert_fails("call setbufvar(bnum, '&l:bk', 1)", 'E355:')
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001016 " should trigger, use correct option name
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +02001017 call setbufvar(bnum, '&backup', 1)
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001018 call assert_equal([], g:options)
1019 call assert_equal(g:opt[0], g:opt[1])
1020
1021 " 16: Setting number option using setwinvar"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001022 let g:options = [['number', 0, 0, '', 1, 'local', 'setlocal']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001023 call setwinvar(0, '&number', 1)
1024 call assert_equal([], g:options)
1025 call assert_equal(g:opt[0], g:opt[1])
1026
1027 " 17: Setting key option, shouldn't trigger"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001028 let g:options = [['key', 'invalid', 'invalid1', 'invalid2', 'invalid3', 'invalid4', 'invalid5']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001029 setlocal key=blah
1030 setlocal key=
Bram Moolenaard7c96872019-06-15 17:12:48 +02001031 call assert_equal([['key', 'invalid', 'invalid1', 'invalid2', 'invalid3', 'invalid4', 'invalid5']], g:options)
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001032 call assert_equal(g:opt[0], g:opt[1])
1033
Bram Moolenaard7c96872019-06-15 17:12:48 +02001034
1035 " 18a: Setting string global option"
1036 let oldval = &backupext
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001037 let g:options = [['backupext', oldval, oldval, oldval, 'foo', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001038 set backupext=foo
1039 call assert_equal([], g:options)
1040 call assert_equal(g:opt[0], g:opt[1])
1041
1042 " 18b: Resetting string global option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001043 let g:options = [['backupext', 'foo', 'foo', 'foo', oldval, 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001044 set backupext&
1045 call assert_equal([], g:options)
1046 call assert_equal(g:opt[0], g:opt[1])
1047
1048 " 18c: Setting global string global option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001049 let g:options = [['backupext', oldval, '', oldval, 'bar', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001050 setglobal backupext=bar
1051 call assert_equal([], g:options)
1052 call assert_equal(g:opt[0], g:opt[1])
1053
1054 " 18d: Setting local string global option"
1055 " As this is a global option this sets the global value even though
1056 " :setlocal is used!
1057 noa set backupext& " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001058 let g:options = [['backupext', oldval, oldval, '', 'baz', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001059 setlocal backupext=baz
1060 call assert_equal([], g:options)
1061 call assert_equal(g:opt[0], g:opt[1])
1062
1063 " 18e: Setting again string global option"
1064 noa setglobal backupext=ext_global " Reset global and local value (without triggering autocmd)
1065 noa setlocal backupext=ext_local " Sets the global(!) value!
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001066 let g:options = [['backupext', 'ext_local', 'ext_local', 'ext_local', 'fuu', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001067 set backupext=fuu
1068 call assert_equal([], g:options)
1069 call assert_equal(g:opt[0], g:opt[1])
1070
1071
zeertzjqb811de52021-10-21 10:50:44 +01001072 " 19a: Setting string global-local (to buffer) option"
Bram Moolenaar8efa0262017-08-20 15:47:20 +02001073 let oldval = &tags
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001074 let g:options = [['tags', oldval, oldval, oldval, 'tagpath', 'global', 'set']]
Bram Moolenaar8efa0262017-08-20 15:47:20 +02001075 set tags=tagpath
1076 call assert_equal([], g:options)
1077 call assert_equal(g:opt[0], g:opt[1])
1078
zeertzjqb811de52021-10-21 10:50:44 +01001079 " 19b: Resetting string global-local (to buffer) option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001080 let g:options = [['tags', 'tagpath', 'tagpath', 'tagpath', oldval, 'global', 'set']]
Bram Moolenaar8efa0262017-08-20 15:47:20 +02001081 set tags&
1082 call assert_equal([], g:options)
1083 call assert_equal(g:opt[0], g:opt[1])
1084
zeertzjqb811de52021-10-21 10:50:44 +01001085 " 19c: Setting global string global-local (to buffer) option "
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001086 let g:options = [['tags', oldval, '', oldval, 'tagpath1', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001087 setglobal tags=tagpath1
1088 call assert_equal([], g:options)
1089 call assert_equal(g:opt[0], g:opt[1])
1090
zeertzjqb811de52021-10-21 10:50:44 +01001091 " 19d: Setting local string global-local (to buffer) option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001092 let g:options = [['tags', 'tagpath1', 'tagpath1', '', 'tagpath2', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001093 setlocal tags=tagpath2
1094 call assert_equal([], g:options)
1095 call assert_equal(g:opt[0], g:opt[1])
1096
zeertzjqb811de52021-10-21 10:50:44 +01001097 " 19e: Setting again string global-local (to buffer) option"
1098 " Note: v:option_old is the old global value for global-local string options
Bram Moolenaard7c96872019-06-15 17:12:48 +02001099 " but the old local value for all other kinds of options.
1100 noa setglobal tags=tag_global " Reset global and local value (without triggering autocmd)
1101 noa setlocal tags=tag_local
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001102 let g:options = [['tags', 'tag_global', 'tag_local', 'tag_global', 'tagpath', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001103 set tags=tagpath
1104 call assert_equal([], g:options)
1105 call assert_equal(g:opt[0], g:opt[1])
1106
zeertzjqb811de52021-10-21 10:50:44 +01001107 " 19f: Setting string global-local (to buffer) option to an empty string"
1108 " Note: v:option_old is the old global value for global-local string options
Bram Moolenaard7c96872019-06-15 17:12:48 +02001109 " but the old local value for all other kinds of options.
1110 noa set tags=tag_global " Reset global and local value (without triggering autocmd)
1111 noa setlocal tags= " empty string
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001112 let g:options = [['tags', 'tag_global', '', 'tag_global', 'tagpath', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001113 set tags=tagpath
1114 call assert_equal([], g:options)
1115 call assert_equal(g:opt[0], g:opt[1])
1116
1117
1118 " 20a: Setting string local (to buffer) option"
1119 let oldval = &spelllang
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001120 let g:options = [['spelllang', oldval, oldval, oldval, 'elvish,klingon', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001121 set spelllang=elvish,klingon
1122 call assert_equal([], g:options)
1123 call assert_equal(g:opt[0], g:opt[1])
1124
1125 " 20b: Resetting string local (to buffer) option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001126 let g:options = [['spelllang', 'elvish,klingon', 'elvish,klingon', 'elvish,klingon', oldval, 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001127 set spelllang&
1128 call assert_equal([], g:options)
1129 call assert_equal(g:opt[0], g:opt[1])
1130
1131 " 20c: Setting global string local (to buffer) option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001132 let g:options = [['spelllang', oldval, '', oldval, 'elvish', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001133 setglobal spelllang=elvish
1134 call assert_equal([], g:options)
1135 call assert_equal(g:opt[0], g:opt[1])
1136
1137 " 20d: Setting local string local (to buffer) option"
1138 noa set spelllang& " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001139 let g:options = [['spelllang', oldval, oldval, '', 'klingon', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001140 setlocal spelllang=klingon
1141 call assert_equal([], g:options)
1142 call assert_equal(g:opt[0], g:opt[1])
1143
1144 " 20e: Setting again string local (to buffer) option"
zeertzjqb811de52021-10-21 10:50:44 +01001145 " Note: v:option_old is the old global value for global-local string options
Bram Moolenaard7c96872019-06-15 17:12:48 +02001146 " but the old local value for all other kinds of options.
1147 noa setglobal spelllang=spellglobal " Reset global and local value (without triggering autocmd)
1148 noa setlocal spelllang=spelllocal
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001149 let g:options = [['spelllang', 'spelllocal', 'spelllocal', 'spellglobal', 'foo', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001150 set spelllang=foo
1151 call assert_equal([], g:options)
1152 call assert_equal(g:opt[0], g:opt[1])
1153
1154
zeertzjqb811de52021-10-21 10:50:44 +01001155 " 21a: Setting string global-local (to window) option"
Bram Moolenaard7c96872019-06-15 17:12:48 +02001156 let oldval = &statusline
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001157 let g:options = [['statusline', oldval, oldval, oldval, 'foo', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001158 set statusline=foo
1159 call assert_equal([], g:options)
1160 call assert_equal(g:opt[0], g:opt[1])
1161
zeertzjqb811de52021-10-21 10:50:44 +01001162 " 21b: Resetting string global-local (to window) option"
1163 " Note: v:option_old is the old global value for global-local string options
Bram Moolenaard7c96872019-06-15 17:12:48 +02001164 " but the old local value for all other kinds of options.
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001165 let g:options = [['statusline', 'foo', 'foo', 'foo', oldval, 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001166 set statusline&
1167 call assert_equal([], g:options)
1168 call assert_equal(g:opt[0], g:opt[1])
1169
zeertzjqb811de52021-10-21 10:50:44 +01001170 " 21c: Setting global string global-local (to window) option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001171 let g:options = [['statusline', oldval, '', oldval, 'bar', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001172 setglobal statusline=bar
1173 call assert_equal([], g:options)
1174 call assert_equal(g:opt[0], g:opt[1])
1175
zeertzjqb811de52021-10-21 10:50:44 +01001176 " 21d: Setting local string global-local (to window) option"
Bram Moolenaard7c96872019-06-15 17:12:48 +02001177 noa set statusline& " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001178 let g:options = [['statusline', oldval, oldval, '', 'baz', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001179 setlocal statusline=baz
1180 call assert_equal([], g:options)
1181 call assert_equal(g:opt[0], g:opt[1])
1182
zeertzjqb811de52021-10-21 10:50:44 +01001183 " 21e: Setting again string global-local (to window) option"
1184 " Note: v:option_old is the old global value for global-local string options
Bram Moolenaard7c96872019-06-15 17:12:48 +02001185 " but the old local value for all other kinds of options.
1186 noa setglobal statusline=bar " Reset global and local value (without triggering autocmd)
1187 noa setlocal statusline=baz
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001188 let g:options = [['statusline', 'bar', 'baz', 'bar', 'foo', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001189 set statusline=foo
1190 call assert_equal([], g:options)
1191 call assert_equal(g:opt[0], g:opt[1])
1192
1193
1194 " 22a: Setting string local (to window) option"
1195 let oldval = &foldignore
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001196 let g:options = [['foldignore', oldval, oldval, oldval, '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 " 22b: Resetting string local (to window) option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001202 let g:options = [['foldignore', 'fo', 'fo', 'fo', oldval, 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001203 set foldignore&
1204 call assert_equal([], g:options)
1205 call assert_equal(g:opt[0], g:opt[1])
1206
1207 " 22c: Setting global string local (to window) option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001208 let g:options = [['foldignore', oldval, '', oldval, 'bar', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001209 setglobal foldignore=bar
1210 call assert_equal([], g:options)
1211 call assert_equal(g:opt[0], g:opt[1])
1212
1213 " 22d: Setting local string local (to window) option"
1214 noa set foldignore& " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001215 let g:options = [['foldignore', oldval, oldval, '', 'baz', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001216 setlocal foldignore=baz
1217 call assert_equal([], g:options)
1218 call assert_equal(g:opt[0], g:opt[1])
1219
1220 " 22e: Setting again string local (to window) option"
1221 noa setglobal foldignore=glob " Reset global and local value (without triggering autocmd)
1222 noa setlocal foldignore=loc
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001223 let g:options = [['foldignore', 'loc', 'loc', 'glob', 'fo', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001224 set foldignore=fo
1225 call assert_equal([], g:options)
1226 call assert_equal(g:opt[0], g:opt[1])
1227
1228
zeertzjqb811de52021-10-21 10:50:44 +01001229 " 23a: Setting global number global option"
Bram Moolenaard7c96872019-06-15 17:12:48 +02001230 noa setglobal cmdheight=8 " Reset global and local value (without triggering autocmd)
1231 noa setlocal cmdheight=1 " Sets the global(!) value!
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001232 let g:options = [['cmdheight', '1', '', '1', '2', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001233 setglobal cmdheight=2
1234 call assert_equal([], g:options)
1235 call assert_equal(g:opt[0], g:opt[1])
1236
1237 " 23b: Setting local number global option"
1238 noa setglobal cmdheight=8 " Reset global and local value (without triggering autocmd)
1239 noa setlocal cmdheight=1 " Sets the global(!) value!
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001240 let g:options = [['cmdheight', '1', '1', '', '2', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001241 setlocal cmdheight=2
1242 call assert_equal([], g:options)
1243 call assert_equal(g:opt[0], g:opt[1])
1244
1245 " 23c: Setting again number global option"
1246 noa setglobal cmdheight=8 " Reset global and local value (without triggering autocmd)
1247 noa setlocal cmdheight=1 " Sets the global(!) value!
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001248 let g:options = [['cmdheight', '1', '1', '1', '2', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001249 set cmdheight=2
1250 call assert_equal([], g:options)
1251 call assert_equal(g:opt[0], g:opt[1])
1252
1253 " 23d: Setting again number global option"
1254 noa set cmdheight=8 " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001255 let g:options = [['cmdheight', '8', '8', '8', '2', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001256 set cmdheight=2
1257 call assert_equal([], g:options)
1258 call assert_equal(g:opt[0], g:opt[1])
1259
1260
1261 " 24a: Setting global number global-local (to buffer) option"
1262 noa setglobal undolevels=8 " Reset global and local value (without triggering autocmd)
1263 noa setlocal undolevels=1
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001264 let g:options = [['undolevels', '8', '', '8', '2', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001265 setglobal undolevels=2
1266 call assert_equal([], g:options)
1267 call assert_equal(g:opt[0], g:opt[1])
1268
1269 " 24b: Setting local number global-local (to buffer) option"
1270 noa setglobal undolevels=8 " Reset global and local value (without triggering autocmd)
1271 noa setlocal undolevels=1
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001272 let g:options = [['undolevels', '1', '1', '', '2', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001273 setlocal undolevels=2
1274 call assert_equal([], g:options)
1275 call assert_equal(g:opt[0], g:opt[1])
1276
1277 " 24c: Setting again number global-local (to buffer) option"
1278 noa setglobal undolevels=8 " Reset global and local value (without triggering autocmd)
1279 noa setlocal undolevels=1
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001280 let g:options = [['undolevels', '1', '1', '8', '2', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001281 set undolevels=2
1282 call assert_equal([], g:options)
1283 call assert_equal(g:opt[0], g:opt[1])
1284
1285 " 24d: Setting again global number global-local (to buffer) option"
1286 noa set undolevels=8 " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001287 let g:options = [['undolevels', '8', '8', '8', '2', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001288 set undolevels=2
1289 call assert_equal([], g:options)
1290 call assert_equal(g:opt[0], g:opt[1])
1291
1292
1293 " 25a: Setting global number local (to buffer) option"
1294 noa setglobal wrapmargin=8 " Reset global and local value (without triggering autocmd)
1295 noa setlocal wrapmargin=1
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001296 let g:options = [['wrapmargin', '8', '', '8', '2', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001297 setglobal wrapmargin=2
1298 call assert_equal([], g:options)
1299 call assert_equal(g:opt[0], g:opt[1])
1300
1301 " 25b: Setting local number local (to buffer) option"
1302 noa setglobal wrapmargin=8 " Reset global and local value (without triggering autocmd)
1303 noa setlocal wrapmargin=1
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001304 let g:options = [['wrapmargin', '1', '1', '', '2', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001305 setlocal wrapmargin=2
1306 call assert_equal([], g:options)
1307 call assert_equal(g:opt[0], g:opt[1])
1308
1309 " 25c: Setting again number local (to buffer) option"
1310 noa setglobal wrapmargin=8 " Reset global and local value (without triggering autocmd)
1311 noa setlocal wrapmargin=1
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001312 let g:options = [['wrapmargin', '1', '1', '8', '2', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001313 set wrapmargin=2
1314 call assert_equal([], g:options)
1315 call assert_equal(g:opt[0], g:opt[1])
1316
1317 " 25d: Setting again global number local (to buffer) option"
1318 noa set wrapmargin=8 " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001319 let g:options = [['wrapmargin', '8', '8', '8', '2', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001320 set wrapmargin=2
1321 call assert_equal([], g:options)
1322 call assert_equal(g:opt[0], g:opt[1])
1323
1324
1325 " 26: Setting number global-local (to window) option.
1326 " Such option does currently not exist.
1327
1328
1329 " 27a: Setting global number local (to window) option"
1330 noa setglobal foldcolumn=8 " Reset global and local value (without triggering autocmd)
1331 noa setlocal foldcolumn=1
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001332 let g:options = [['foldcolumn', '8', '', '8', '2', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001333 setglobal foldcolumn=2
1334 call assert_equal([], g:options)
1335 call assert_equal(g:opt[0], g:opt[1])
1336
1337 " 27b: Setting local number local (to window) option"
1338 noa setglobal foldcolumn=8 " Reset global and local value (without triggering autocmd)
1339 noa setlocal foldcolumn=1
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001340 let g:options = [['foldcolumn', '1', '1', '', '2', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001341 setlocal foldcolumn=2
1342 call assert_equal([], g:options)
1343 call assert_equal(g:opt[0], g:opt[1])
1344
1345 " 27c: Setting again number local (to window) option"
1346 noa setglobal foldcolumn=8 " Reset global and local value (without triggering autocmd)
1347 noa setlocal foldcolumn=1
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001348 let g:options = [['foldcolumn', '1', '1', '8', '2', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001349 set foldcolumn=2
1350 call assert_equal([], g:options)
1351 call assert_equal(g:opt[0], g:opt[1])
1352
zeertzjqb811de52021-10-21 10:50:44 +01001353 " 27d: Setting again global number local (to window) option"
Bram Moolenaard7c96872019-06-15 17:12:48 +02001354 noa set foldcolumn=8 " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001355 let g:options = [['foldcolumn', '8', '8', '8', '2', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001356 set foldcolumn=2
1357 call assert_equal([], g:options)
1358 call assert_equal(g:opt[0], g:opt[1])
1359
1360
1361 " 28a: Setting global boolean global option"
1362 noa setglobal nowrapscan " Reset global and local value (without triggering autocmd)
1363 noa setlocal wrapscan " Sets the global(!) value!
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001364 let g:options = [['wrapscan', '1', '', '1', '0', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001365 setglobal nowrapscan
1366 call assert_equal([], g:options)
1367 call assert_equal(g:opt[0], g:opt[1])
1368
1369 " 28b: Setting local boolean global option"
1370 noa setglobal nowrapscan " Reset global and local value (without triggering autocmd)
1371 noa setlocal wrapscan " Sets the global(!) value!
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001372 let g:options = [['wrapscan', '1', '1', '', '0', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001373 setlocal nowrapscan
1374 call assert_equal([], g:options)
1375 call assert_equal(g:opt[0], g:opt[1])
1376
1377 " 28c: Setting again boolean global option"
1378 noa setglobal nowrapscan " Reset global and local value (without triggering autocmd)
1379 noa setlocal wrapscan " Sets the global(!) value!
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001380 let g:options = [['wrapscan', '1', '1', '1', '0', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001381 set nowrapscan
1382 call assert_equal([], g:options)
1383 call assert_equal(g:opt[0], g:opt[1])
1384
1385 " 28d: Setting again global boolean global option"
1386 noa set nowrapscan " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001387 let g:options = [['wrapscan', '0', '0', '0', '1', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001388 set wrapscan
1389 call assert_equal([], g:options)
1390 call assert_equal(g:opt[0], g:opt[1])
1391
1392
1393 " 29a: Setting global boolean global-local (to buffer) option"
1394 noa setglobal noautoread " Reset global and local value (without triggering autocmd)
1395 noa setlocal autoread
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001396 let g:options = [['autoread', '0', '', '0', '1', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001397 setglobal autoread
1398 call assert_equal([], g:options)
1399 call assert_equal(g:opt[0], g:opt[1])
1400
1401 " 29b: Setting local boolean global-local (to buffer) option"
1402 noa setglobal noautoread " Reset global and local value (without triggering autocmd)
1403 noa setlocal autoread
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001404 let g:options = [['autoread', '1', '1', '', '0', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001405 setlocal noautoread
1406 call assert_equal([], g:options)
1407 call assert_equal(g:opt[0], g:opt[1])
1408
1409 " 29c: Setting again boolean global-local (to buffer) option"
1410 noa setglobal noautoread " Reset global and local value (without triggering autocmd)
1411 noa setlocal autoread
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001412 let g:options = [['autoread', '1', '1', '0', '1', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001413 set autoread
1414 call assert_equal([], g:options)
1415 call assert_equal(g:opt[0], g:opt[1])
1416
1417 " 29d: Setting again global boolean global-local (to buffer) option"
1418 noa set noautoread " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001419 let g:options = [['autoread', '0', '0', '0', '1', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001420 set autoread
1421 call assert_equal([], g:options)
1422 call assert_equal(g:opt[0], g:opt[1])
1423
1424
1425 " 30a: Setting global boolean local (to buffer) option"
1426 noa setglobal nocindent " Reset global and local value (without triggering autocmd)
1427 noa setlocal cindent
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001428 let g:options = [['cindent', '0', '', '0', '1', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001429 setglobal cindent
1430 call assert_equal([], g:options)
1431 call assert_equal(g:opt[0], g:opt[1])
1432
1433 " 30b: Setting local boolean local (to buffer) option"
1434 noa setglobal nocindent " Reset global and local value (without triggering autocmd)
1435 noa setlocal cindent
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001436 let g:options = [['cindent', '1', '1', '', '0', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001437 setlocal nocindent
1438 call assert_equal([], g:options)
1439 call assert_equal(g:opt[0], g:opt[1])
1440
1441 " 30c: Setting again boolean local (to buffer) option"
1442 noa setglobal nocindent " Reset global and local value (without triggering autocmd)
1443 noa setlocal cindent
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001444 let g:options = [['cindent', '1', '1', '0', '1', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001445 set cindent
1446 call assert_equal([], g:options)
1447 call assert_equal(g:opt[0], g:opt[1])
1448
1449 " 30d: Setting again global boolean local (to buffer) option"
1450 noa set nocindent " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001451 let g:options = [['cindent', '0', '0', '0', '1', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001452 set cindent
1453 call assert_equal([], g:options)
1454 call assert_equal(g:opt[0], g:opt[1])
1455
1456
1457 " 31: Setting boolean global-local (to window) option
1458 " Currently no such option exists.
1459
1460
1461 " 32a: Setting global boolean local (to window) option"
1462 noa setglobal nocursorcolumn " Reset global and local value (without triggering autocmd)
1463 noa setlocal cursorcolumn
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001464 let g:options = [['cursorcolumn', '0', '', '0', '1', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001465 setglobal cursorcolumn
1466 call assert_equal([], g:options)
1467 call assert_equal(g:opt[0], g:opt[1])
1468
1469 " 32b: Setting local boolean local (to window) option"
1470 noa setglobal nocursorcolumn " Reset global and local value (without triggering autocmd)
1471 noa setlocal cursorcolumn
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001472 let g:options = [['cursorcolumn', '1', '1', '', '0', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001473 setlocal nocursorcolumn
1474 call assert_equal([], g:options)
1475 call assert_equal(g:opt[0], g:opt[1])
1476
1477 " 32c: Setting again boolean local (to window) option"
1478 noa setglobal nocursorcolumn " Reset global and local value (without triggering autocmd)
1479 noa setlocal cursorcolumn
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001480 let g:options = [['cursorcolumn', '1', '1', '0', '1', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001481 set cursorcolumn
1482 call assert_equal([], g:options)
1483 call assert_equal(g:opt[0], g:opt[1])
1484
1485 " 32d: Setting again global boolean local (to window) option"
1486 noa set nocursorcolumn " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001487 let g:options = [['cursorcolumn', '0', '0', '0', '1', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001488 set cursorcolumn
1489 call assert_equal([], g:options)
1490 call assert_equal(g:opt[0], g:opt[1])
1491
1492
Bram Moolenaar1bc353b2019-09-01 14:45:28 +02001493 " 33: Test autocommands when an option value is converted internally.
Bram Moolenaard7c96872019-06-15 17:12:48 +02001494 noa set backspace=1 " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001495 let g:options = [['backspace', 'indent,eol', 'indent,eol', 'indent,eol', '2', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001496 set backspace=2
1497 call assert_equal([], g:options)
1498 call assert_equal(g:opt[0], g:opt[1])
1499
1500
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001501 " Cleanup
1502 au! OptionSet
Bram Moolenaar0331faf2019-06-15 18:40:37 +02001503 " set tags&
Bram Moolenaard7c96872019-06-15 17:12:48 +02001504 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 +02001505 exe printf(":set %s&vim", opt)
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001506 endfor
1507 call test_override('starting', 0)
1508 delfunc! AutoCommandOptionSet
1509endfunc
1510
1511func Test_OptionSet_diffmode()
1512 call test_override('starting', 1)
Bram Moolenaar26d98212019-01-27 22:32:55 +01001513 " 18: Changing an option when entering diff mode
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001514 new
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001515 au OptionSet diff :let &l:cul = v:option_new
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001516
1517 call setline(1, ['buffer 1', 'line2', 'line3', 'line4'])
1518 call assert_equal(0, &l:cul)
1519 diffthis
1520 call assert_equal(1, &l:cul)
1521
1522 vnew
1523 call setline(1, ['buffer 2', 'line 2', 'line 3', 'line4'])
1524 call assert_equal(0, &l:cul)
1525 diffthis
1526 call assert_equal(1, &l:cul)
1527
1528 diffoff
1529 call assert_equal(0, &l:cul)
1530 call assert_equal(1, getwinvar(2, '&l:cul'))
1531 bw!
1532
1533 call assert_equal(1, &l:cul)
1534 diffoff!
1535 call assert_equal(0, &l:cul)
1536 call assert_equal(0, getwinvar(1, '&l:cul'))
1537 bw!
1538
1539 " Cleanup
1540 au! OptionSet
1541 call test_override('starting', 0)
1542endfunc
1543
1544func Test_OptionSet_diffmode_close()
1545 call test_override('starting', 1)
1546 " 19: Try to close the current window when entering diff mode
1547 " should not segfault
1548 new
1549 au OptionSet diff close
1550
1551 call setline(1, ['buffer 1', 'line2', 'line3', 'line4'])
Bram Moolenaare2e40752020-09-04 21:18:46 +02001552 call assert_fails(':diffthis', 'E788:')
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001553 call assert_equal(1, &diff)
1554 vnew
1555 call setline(1, ['buffer 2', 'line 2', 'line 3', 'line4'])
Bram Moolenaare2e40752020-09-04 21:18:46 +02001556 call assert_fails(':diffthis', 'E788:')
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001557 call assert_equal(1, &diff)
Bram Moolenaara9aa86f2019-11-10 21:25:45 +01001558 set diffopt-=closeoff
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001559 bw!
Bram Moolenaare2e40752020-09-04 21:18:46 +02001560 call assert_fails(':diffoff!', 'E788:')
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001561 bw!
1562
1563 " Cleanup
1564 au! OptionSet
1565 call test_override('starting', 0)
1566 "delfunc! AutoCommandOptionSet
1567endfunc
Bram Moolenaar4a137b42017-08-04 22:37:11 +02001568
1569" Test for Bufleave autocommand that deletes the buffer we are about to edit.
1570func Test_BufleaveWithDelete()
Bram Moolenaare7cda972022-08-29 11:02:59 +01001571 new | edit XbufLeave1
Bram Moolenaar4a137b42017-08-04 22:37:11 +02001572
1573 augroup test_bufleavewithdelete
1574 autocmd!
Bram Moolenaare7cda972022-08-29 11:02:59 +01001575 autocmd BufLeave XbufLeave1 bwipe XbufLeave2
Bram Moolenaar4a137b42017-08-04 22:37:11 +02001576 augroup END
1577
Bram Moolenaare7cda972022-08-29 11:02:59 +01001578 call assert_fails('edit XbufLeave2', 'E143:')
1579 call assert_equal('XbufLeave1', bufname('%'))
Bram Moolenaar4a137b42017-08-04 22:37:11 +02001580
Bram Moolenaare7cda972022-08-29 11:02:59 +01001581 autocmd! test_bufleavewithdelete BufLeave XbufLeave1
Bram Moolenaar4a137b42017-08-04 22:37:11 +02001582 augroup! test_bufleavewithdelete
1583
1584 new
Bram Moolenaare7cda972022-08-29 11:02:59 +01001585 bwipe! XbufLeave1
Bram Moolenaar4a137b42017-08-04 22:37:11 +02001586endfunc
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +02001587
1588" Test for autocommand that changes the buffer list, when doing ":ball".
1589func Test_Acmd_BufAll()
1590 enew!
1591 %bwipe!
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01001592 call writefile(['Test file Xxx1'], 'Xxx1', 'D')
1593 call writefile(['Test file Xxx2'], 'Xxx2', 'D')
1594 call writefile(['Test file Xxx3'], 'Xxx3', 'D')
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +02001595
1596 " Add three files to the buffer list
1597 split Xxx1
1598 close
1599 split Xxx2
1600 close
1601 split Xxx3
1602 close
1603
1604 " Wipe the buffer when the buffer is opened
1605 au BufReadPost Xxx2 bwipe
1606
1607 call append(0, 'Test file Xxx4')
1608 ball
1609
1610 call assert_equal(2, winnr('$'))
1611 call assert_equal('Xxx1', bufname(winbufnr(winnr('$'))))
1612 wincmd t
1613
1614 au! BufReadPost
1615 %bwipe!
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +02001616 enew! | only
1617endfunc
1618
1619" Test for autocommand that changes current buffer on BufEnter event.
1620" Check if modelines are interpreted for the correct buffer.
1621func Test_Acmd_BufEnter()
1622 %bwipe!
1623 call writefile(['start of test file Xxx1',
1624 \ "\<Tab>this is a test",
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01001625 \ 'end of test file Xxx1'], 'Xxx1', 'D')
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +02001626 call writefile(['start of test file Xxx2',
1627 \ 'vim: set noai :',
1628 \ "\<Tab>this is a test",
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01001629 \ 'end of test file Xxx2'], 'Xxx2', 'D')
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +02001630
1631 au BufEnter Xxx2 brew
1632 set ai modeline modelines=3
1633 edit Xxx1
1634 " edit Xxx2, autocmd will do :brew
1635 edit Xxx2
1636 exe "normal G?this is a\<CR>"
1637 " Append text with autoindent to this file
1638 normal othis should be auto-indented
1639 call assert_equal("\<Tab>this should be auto-indented", getline('.'))
1640 call assert_equal(3, line('.'))
1641 " Remove autocmd and edit Xxx2 again
1642 au! BufEnter Xxx2
1643 buf! Xxx2
1644 exe "normal G?this is a\<CR>"
1645 " append text without autoindent to Xxx
1646 normal othis should be in column 1
1647 call assert_equal("this should be in column 1", getline('.'))
1648 call assert_equal(4, line('.'))
1649
1650 %bwipe!
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +02001651 set ai&vim modeline&vim modelines&vim
1652endfunc
1653
1654" Test for issue #57
1655" do not move cursor on <c-o> when autoindent is set
1656func Test_ai_CTRL_O()
1657 enew!
1658 set ai
1659 let save_fo = &fo
1660 set fo+=r
1661 exe "normal o# abcdef\<Esc>2hi\<CR>\<C-O>d0\<Esc>"
1662 exe "normal o# abcdef\<Esc>2hi\<C-O>d0\<Esc>"
1663 call assert_equal(['# abc', 'def', 'def'], getline(2, 4))
1664
1665 set ai&vim
1666 let &fo = save_fo
1667 enew!
1668endfunc
1669
1670" Test for autocommand that deletes the current buffer on BufLeave event.
1671" Also test deleting the last buffer, should give a new, empty buffer.
1672func Test_BufLeave_Wipe()
1673 %bwipe!
1674 let content = ['start of test file Xxx',
1675 \ 'this is a test',
1676 \ 'end of test file Xxx']
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01001677 call writefile(content, 'Xxx1', 'D')
1678 call writefile(content, 'Xxx2', 'D')
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +02001679
1680 au BufLeave Xxx2 bwipe
1681 edit Xxx1
1682 split Xxx2
1683 " delete buffer Xxx2, we should be back to Xxx1
1684 bwipe
1685 call assert_equal('Xxx1', bufname('%'))
1686 call assert_equal(1, winnr('$'))
1687
1688 " Create an alternate buffer
1689 %write! test.out
1690 call assert_equal('test.out', bufname('#'))
1691 " delete alternate buffer
1692 bwipe test.out
1693 call assert_equal('Xxx1', bufname('%'))
1694 call assert_equal('', bufname('#'))
1695
1696 au BufLeave Xxx1 bwipe
1697 " delete current buffer, get an empty one
1698 bwipe!
1699 call assert_equal(1, line('$'))
1700 call assert_equal('', bufname('%'))
Bram Moolenaarb2c87502017-10-14 21:15:58 +02001701 let g:bufinfo = getbufinfo()
1702 call assert_equal(1, len(g:bufinfo))
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +02001703
Bram Moolenaar53f0c962017-10-22 14:23:59 +02001704 call delete('test.out')
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +02001705 %bwipe
1706 au! BufLeave
Bram Moolenaarb2c87502017-10-14 21:15:58 +02001707
1708 " check that bufinfo doesn't contain a pointer to freed memory
1709 call test_garbagecollect_now()
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +02001710endfunc
Bram Moolenaar87ffb5c2017-10-19 12:37:42 +02001711
1712func Test_QuitPre()
1713 edit Xfoo
1714 let winid = win_getid(winnr())
1715 split Xbar
1716 au! QuitPre * let g:afile = expand('<afile>')
1717 " Close the other window, <afile> should be correct.
1718 exe win_id2win(winid) . 'q'
1719 call assert_equal('Xfoo', g:afile)
LemonBoy66e13ae2022-04-21 22:52:11 +01001720
Bram Moolenaar87ffb5c2017-10-19 12:37:42 +02001721 unlet g:afile
1722 bwipe Xfoo
1723 bwipe Xbar
1724endfunc
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02001725
1726func Test_Cmdline()
Bram Moolenaar153b7042018-01-31 15:48:32 +01001727 au! CmdlineChanged : let g:text = getcmdline()
1728 let g:text = 0
1729 call feedkeys(":echom 'hello'\<CR>", 'xt')
1730 call assert_equal("echom 'hello'", g:text)
1731 au! CmdlineChanged
1732
1733 au! CmdlineChanged : let g:entered = expand('<afile>')
1734 let g:entered = 0
1735 call feedkeys(":echom 'hello'\<CR>", 'xt')
1736 call assert_equal(':', g:entered)
1737 au! CmdlineChanged
1738
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02001739 au! CmdlineEnter : let g:entered = expand('<afile>')
1740 au! CmdlineLeave : let g:left = expand('<afile>')
1741 let g:entered = 0
1742 let g:left = 0
1743 call feedkeys(":echo 'hello'\<CR>", 'xt')
1744 call assert_equal(':', g:entered)
1745 call assert_equal(':', g:left)
1746 au! CmdlineEnter
1747 au! CmdlineLeave
1748
Bram Moolenaara4baf5b2018-04-22 13:27:44 +02001749 let save_shellslash = &shellslash
1750 set noshellslash
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02001751 au! CmdlineEnter / let g:entered = expand('<afile>')
1752 au! CmdlineLeave / let g:left = expand('<afile>')
1753 let g:entered = 0
1754 let g:left = 0
Bram Moolenaar53f0c962017-10-22 14:23:59 +02001755 new
1756 call setline(1, 'hello')
1757 call feedkeys("/hello\<CR>", 'xt')
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02001758 call assert_equal('/', g:entered)
1759 call assert_equal('/', g:left)
Bram Moolenaar53f0c962017-10-22 14:23:59 +02001760 bwipe!
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02001761 au! CmdlineEnter
1762 au! CmdlineLeave
Bram Moolenaara4baf5b2018-04-22 13:27:44 +02001763 let &shellslash = save_shellslash
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02001764endfunc
Bram Moolenaar53f0c962017-10-22 14:23:59 +02001765
1766" Test for BufWritePre autocommand that deletes or unloads the buffer.
1767func Test_BufWritePre()
1768 %bwipe
1769 au BufWritePre Xxx1 bunload
1770 au BufWritePre Xxx2 bwipe
1771
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01001772 call writefile(['start of Xxx1', 'test', 'end of Xxx1'], 'Xxx1', 'D')
1773 call writefile(['start of Xxx2', 'test', 'end of Xxx2'], 'Xxx2', 'D')
Bram Moolenaar53f0c962017-10-22 14:23:59 +02001774
1775 edit Xtest
1776 e! Xxx2
1777 bdel Xtest
1778 e Xxx1
1779 " write it, will unload it and give an error msg
Bram Moolenaare2e40752020-09-04 21:18:46 +02001780 call assert_fails('w', 'E203:')
Bram Moolenaar53f0c962017-10-22 14:23:59 +02001781 call assert_equal('Xxx2', bufname('%'))
1782 edit Xtest
1783 e! Xxx2
1784 bwipe Xtest
1785 " write it, will delete the buffer and give an error msg
Bram Moolenaare2e40752020-09-04 21:18:46 +02001786 call assert_fails('w', 'E203:')
Bram Moolenaar53f0c962017-10-22 14:23:59 +02001787 call assert_equal('Xxx1', bufname('%'))
1788 au! BufWritePre
Bram Moolenaar53f0c962017-10-22 14:23:59 +02001789endfunc
1790
1791" Test for BufUnload autocommand that unloads all the other buffers
1792func Test_bufunload_all()
Bram Moolenaarf08b0eb2021-10-16 13:00:14 +01001793 let g:test_is_flaky = 1
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01001794 call writefile(['Test file Xxx1'], 'Xxx1', 'D')"
1795 call writefile(['Test file Xxx2'], 'Xxx2', 'D')"
Bram Moolenaar53f0c962017-10-22 14:23:59 +02001796
Bram Moolenaarc79745a2019-05-20 22:12:34 +02001797 let content =<< trim [CODE]
1798 func UnloadAllBufs()
1799 let i = 1
1800 while i <= bufnr('$')
1801 if i != bufnr('%') && bufloaded(i)
1802 exe i . 'bunload'
1803 endif
1804 let i += 1
1805 endwhile
1806 endfunc
1807 au BufUnload * call UnloadAllBufs()
1808 au VimLeave * call writefile(['Test Finished'], 'Xout')
1809 edit Xxx1
1810 split Xxx2
1811 q
1812 [CODE]
1813
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01001814 call writefile(content, 'Xbunloadtest', 'D')
Bram Moolenaar53f0c962017-10-22 14:23:59 +02001815
1816 call delete('Xout')
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01001817 call system(GetVimCommandClean() .. ' -N --not-a-term -S Xbunloadtest')
Bram Moolenaar53f0c962017-10-22 14:23:59 +02001818 call assert_true(filereadable('Xout'))
1819
Bram Moolenaar53f0c962017-10-22 14:23:59 +02001820 call delete('Xout')
1821endfunc
1822
1823" Some tests for buffer-local autocommands
1824func Test_buflocal_autocmd()
1825 let g:bname = ''
1826 edit xx
1827 au BufLeave <buffer> let g:bname = expand("%")
1828 " here, autocommand for xx should trigger.
1829 " but autocommand shall not apply to buffer named <buffer>.
1830 edit somefile
1831 call assert_equal('xx', g:bname)
1832 let g:bname = ''
1833 " here, autocommand shall be auto-deleted
1834 bwipe xx
1835 " autocmd should not trigger
1836 edit xx
1837 call assert_equal('', g:bname)
1838 " autocmd should not trigger
1839 edit somefile
1840 call assert_equal('', g:bname)
1841 enew
1842 unlet g:bname
1843endfunc
Bram Moolenaar430dc5d2017-11-02 21:04:47 +01001844
1845" Test for "*Cmd" autocommands
1846func Test_Cmd_Autocmds()
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01001847 call writefile(['start of Xxx', "\tabc2", 'end of Xxx'], 'Xxx', 'D')
Bram Moolenaar430dc5d2017-11-02 21:04:47 +01001848
1849 enew!
1850 au BufReadCmd XtestA 0r Xxx|$del
1851 edit XtestA " will read text of Xxd instead
1852 call assert_equal('start of Xxx', getline(1))
1853
1854 au BufWriteCmd XtestA call append(line("$"), "write")
1855 write " will append a line to the file
1856 call assert_equal('write', getline('$'))
Bram Moolenaare2e40752020-09-04 21:18:46 +02001857 call assert_fails('read XtestA', 'E484:') " should not read anything
Bram Moolenaar430dc5d2017-11-02 21:04:47 +01001858 call assert_equal('write', getline(4))
1859
1860 " now we have:
1861 " 1 start of Xxx
1862 " 2 abc2
1863 " 3 end of Xxx
1864 " 4 write
1865
1866 au FileReadCmd XtestB '[r Xxx
1867 2r XtestB " will read Xxx below line 2 instead
1868 call assert_equal('start of Xxx', getline(3))
1869
1870 " now we have:
1871 " 1 start of Xxx
1872 " 2 abc2
1873 " 3 start of Xxx
1874 " 4 abc2
1875 " 5 end of Xxx
1876 " 6 end of Xxx
1877 " 7 write
1878
1879 au FileWriteCmd XtestC '[,']copy $
1880 normal 4GA1
1881 4,5w XtestC " will copy lines 4 and 5 to the end
1882 call assert_equal("\tabc21", getline(8))
Bram Moolenaare2e40752020-09-04 21:18:46 +02001883 call assert_fails('r XtestC', 'E484:') " should not read anything
Bram Moolenaar430dc5d2017-11-02 21:04:47 +01001884 call assert_equal("end of Xxx", getline(9))
1885
1886 " now we have:
1887 " 1 start of Xxx
1888 " 2 abc2
1889 " 3 start of Xxx
1890 " 4 abc21
1891 " 5 end of Xxx
1892 " 6 end of Xxx
1893 " 7 write
1894 " 8 abc21
1895 " 9 end of Xxx
1896
1897 let g:lines = []
1898 au FileAppendCmd XtestD call extend(g:lines, getline(line("'["), line("']")))
1899 w >>XtestD " will add lines to 'lines'
1900 call assert_equal(9, len(g:lines))
Bram Moolenaare2e40752020-09-04 21:18:46 +02001901 call assert_fails('$r XtestD', 'E484:') " should not read anything
Bram Moolenaar430dc5d2017-11-02 21:04:47 +01001902 call assert_equal(9, line('$'))
1903 call assert_equal('end of Xxx', getline('$'))
1904
1905 au BufReadCmd XtestE 0r Xxx|$del
1906 sp XtestE " split window with test.out
1907 call assert_equal('end of Xxx', getline(3))
1908
1909 let g:lines = []
1910 exe "normal 2Goasdf\<Esc>\<C-W>\<C-W>"
1911 au BufWriteCmd XtestE call extend(g:lines, getline(0, '$'))
1912 wall " will write other window to 'lines'
1913 call assert_equal(4, len(g:lines), g:lines)
1914 call assert_equal('asdf', g:lines[2])
1915
1916 au! BufReadCmd
1917 au! BufWriteCmd
1918 au! FileReadCmd
1919 au! FileWriteCmd
1920 au! FileAppendCmd
1921 %bwipe!
Bram Moolenaar430dc5d2017-11-02 21:04:47 +01001922 enew!
1923endfunc
Bram Moolenaaraace2152017-11-05 16:23:10 +01001924
Bram Moolenaar0fff4412020-03-29 16:06:29 +02001925func s:ReadFile()
1926 setl noswapfile nomodified
1927 let filename = resolve(expand("<afile>:p"))
1928 execute 'read' fnameescape(filename)
1929 1d_
1930 exe 'file' fnameescape(filename)
1931 setl buftype=acwrite
1932endfunc
1933
1934func s:WriteFile()
1935 let filename = resolve(expand("<afile>:p"))
1936 setl buftype=
1937 noautocmd execute 'write' fnameescape(filename)
1938 setl buftype=acwrite
1939 setl nomodified
1940endfunc
1941
1942func Test_BufReadCmd()
1943 autocmd BufReadCmd *.test call s:ReadFile()
1944 autocmd BufWriteCmd *.test call s:WriteFile()
1945
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01001946 call writefile(['one', 'two', 'three'], 'Xcmd.test', 'D')
Bram Moolenaar0fff4412020-03-29 16:06:29 +02001947 edit Xcmd.test
1948 call assert_match('Xcmd.test" line 1 of 3', execute('file'))
1949 normal! Gofour
1950 write
1951 call assert_equal(['one', 'two', 'three', 'four'], readfile('Xcmd.test'))
1952
1953 bwipe!
Bram Moolenaar0fff4412020-03-29 16:06:29 +02001954 au! BufReadCmd
1955 au! BufWriteCmd
1956endfunc
1957
zeertzjq9c8f9462022-08-30 18:17:15 +01001958func Test_BufWriteCmd()
1959 autocmd BufWriteCmd Xbufwritecmd let g:written = 1
1960 new
1961 file Xbufwritecmd
1962 set buftype=acwrite
Bram Moolenaar6f14da12022-09-07 21:30:44 +01001963 call mkdir('Xbufwritecmd', 'D')
zeertzjq9c8f9462022-08-30 18:17:15 +01001964 write
1965 " BufWriteCmd should be triggered even if a directory has the same name
1966 call assert_equal(1, g:written)
zeertzjq9c8f9462022-08-30 18:17:15 +01001967 unlet g:written
1968 au! BufWriteCmd
1969 bwipe!
1970endfunc
1971
Bram Moolenaaraace2152017-11-05 16:23:10 +01001972func SetChangeMarks(start, end)
Bram Moolenaar97c69432021-01-15 16:45:21 +01001973 exe a:start .. 'mark ['
1974 exe a:end .. 'mark ]'
Bram Moolenaaraace2152017-11-05 16:23:10 +01001975endfunc
1976
1977" Verify the effects of autocmds on '[ and ']
1978func Test_change_mark_in_autocmds()
1979 edit! Xtest
Bram Moolenaar97c69432021-01-15 16:45:21 +01001980 call feedkeys("ia\<CR>b\<CR>c\<CR>d\<C-g>u\<Esc>", 'xtn')
Bram Moolenaaraace2152017-11-05 16:23:10 +01001981
1982 call SetChangeMarks(2, 3)
1983 write
1984 call assert_equal([1, 4], [line("'["), line("']")])
1985
1986 call SetChangeMarks(2, 3)
1987 au BufWritePre * call assert_equal([1, 4], [line("'["), line("']")])
1988 write
1989 au! BufWritePre
1990
Bram Moolenaar14ddd222020-08-05 12:02:40 +02001991 if has('unix')
Bram Moolenaaraace2152017-11-05 16:23:10 +01001992 write XtestFilter
1993 write >> XtestFilter
1994
1995 call SetChangeMarks(2, 3)
1996 " Marks are set to the entire range of the write
1997 au FilterWritePre * call assert_equal([1, 4], [line("'["), line("']")])
1998 " '[ is adjusted to just before the line that will receive the filtered
1999 " data
2000 au FilterReadPre * call assert_equal([4, 4], [line("'["), line("']")])
2001 " The filtered data is read into the buffer, and the source lines are
2002 " still present, so the range is after the source lines
2003 au FilterReadPost * call assert_equal([5, 12], [line("'["), line("']")])
2004 %!cat XtestFilter
2005 " After the filtered data is read, the original lines are deleted
2006 call assert_equal([1, 8], [line("'["), line("']")])
2007 au! FilterWritePre,FilterReadPre,FilterReadPost
2008 undo
2009
2010 call SetChangeMarks(1, 4)
2011 au FilterWritePre * call assert_equal([2, 3], [line("'["), line("']")])
2012 au FilterReadPre * call assert_equal([3, 3], [line("'["), line("']")])
2013 au FilterReadPost * call assert_equal([4, 11], [line("'["), line("']")])
2014 2,3!cat XtestFilter
2015 call assert_equal([2, 9], [line("'["), line("']")])
2016 au! FilterWritePre,FilterReadPre,FilterReadPost
2017 undo
2018
2019 call delete('XtestFilter')
2020 endif
2021
2022 call SetChangeMarks(1, 4)
2023 au FileWritePre * call assert_equal([2, 3], [line("'["), line("']")])
2024 2,3write Xtest2
2025 au! FileWritePre
2026
2027 call SetChangeMarks(2, 3)
2028 au FileAppendPre * call assert_equal([1, 4], [line("'["), line("']")])
2029 write >> Xtest2
2030 au! FileAppendPre
2031
2032 call SetChangeMarks(1, 4)
2033 au FileAppendPre * call assert_equal([2, 3], [line("'["), line("']")])
2034 2,3write >> Xtest2
2035 au! FileAppendPre
2036
2037 call SetChangeMarks(1, 1)
2038 au FileReadPre * call assert_equal([3, 1], [line("'["), line("']")])
2039 au FileReadPost * call assert_equal([4, 11], [line("'["), line("']")])
2040 3read Xtest2
2041 au! FileReadPre,FileReadPost
2042 undo
2043
2044 call SetChangeMarks(4, 4)
2045 " When the line is 0, it's adjusted to 1
2046 au FileReadPre * call assert_equal([1, 4], [line("'["), line("']")])
2047 au FileReadPost * call assert_equal([1, 8], [line("'["), line("']")])
2048 0read Xtest2
2049 au! FileReadPre,FileReadPost
2050 undo
2051
2052 call SetChangeMarks(4, 4)
2053 " When the line is 0, it's adjusted to 1
2054 au FileReadPre * call assert_equal([1, 4], [line("'["), line("']")])
2055 au FileReadPost * call assert_equal([2, 9], [line("'["), line("']")])
2056 1read Xtest2
2057 au! FileReadPre,FileReadPost
2058 undo
2059
2060 bwipe!
2061 call delete('Xtest')
2062 call delete('Xtest2')
2063endfunc
2064
2065func Test_Filter_noshelltemp()
Bram Moolenaaraeb313f2020-11-27 19:13:28 +01002066 CheckExecutable cat
Bram Moolenaaraace2152017-11-05 16:23:10 +01002067
2068 enew!
2069 call setline(1, ['a', 'b', 'c', 'd'])
2070
2071 let shelltemp = &shelltemp
2072 set shelltemp
2073
2074 let g:filter_au = 0
2075 au FilterWritePre * let g:filter_au += 1
2076 au FilterReadPre * let g:filter_au += 1
2077 au FilterReadPost * let g:filter_au += 1
2078 %!cat
2079 call assert_equal(3, g:filter_au)
2080
2081 if has('filterpipe')
2082 set noshelltemp
2083
2084 let g:filter_au = 0
2085 au FilterWritePre * let g:filter_au += 1
2086 au FilterReadPre * let g:filter_au += 1
2087 au FilterReadPost * let g:filter_au += 1
2088 %!cat
2089 call assert_equal(0, g:filter_au)
2090 endif
2091
2092 au! FilterWritePre,FilterReadPre,FilterReadPost
2093 let &shelltemp = shelltemp
2094 bwipe!
2095endfunc
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01002096
2097func Test_TextYankPost()
2098 enew!
2099 call setline(1, ['foo'])
2100
2101 let g:event = []
2102 au TextYankPost * let g:event = copy(v:event)
2103
2104 call assert_equal({}, v:event)
2105 call assert_fails('let v:event = {}', 'E46:')
2106 call assert_fails('let v:event.mykey = 0', 'E742:')
2107
2108 norm "ayiw
2109 call assert_equal(
Bram Moolenaara016eeb2022-04-09 11:37:38 +01002110 \ #{regcontents: ['foo'], regname: 'a', operator: 'y',
2111 \ regtype: 'v', visual: v:false, inclusive: v:true},
2112 \ g:event)
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01002113 norm y_
2114 call assert_equal(
Bram Moolenaara016eeb2022-04-09 11:37:38 +01002115 \ #{regcontents: ['foo'], regname: '', operator: 'y', regtype: 'V',
2116 \ visual: v:false, inclusive: v:false},
2117 \ g:event)
Bram Moolenaar37d16732020-06-12 22:09:01 +02002118 norm Vy
2119 call assert_equal(
Bram Moolenaara016eeb2022-04-09 11:37:38 +01002120 \ #{regcontents: ['foo'], regname: '', operator: 'y', regtype: 'V',
2121 \ visual: v:true, inclusive: v:true},
2122 \ g:event)
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01002123 call feedkeys("\<C-V>y", 'x')
2124 call assert_equal(
Bram Moolenaara016eeb2022-04-09 11:37:38 +01002125 \ #{regcontents: ['f'], regname: '', operator: 'y', regtype: "\x161",
2126 \ visual: v:true, inclusive: v:true},
2127 \ g:event)
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01002128 norm "xciwbar
2129 call assert_equal(
Bram Moolenaara016eeb2022-04-09 11:37:38 +01002130 \ #{regcontents: ['foo'], regname: 'x', operator: 'c', regtype: 'v',
2131 \ visual: v:false, inclusive: v:true},
2132 \ g:event)
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01002133 norm "bdiw
2134 call assert_equal(
Bram Moolenaara016eeb2022-04-09 11:37:38 +01002135 \ #{regcontents: ['bar'], regname: 'b', operator: 'd', regtype: 'v',
2136 \ visual: v:false, inclusive: v:true},
2137 \ g:event)
2138
2139 call setline(1, 'foobar')
2140 " exclusive motion
2141 norm $"ay0
2142 call assert_equal(
2143 \ #{regcontents: ['fooba'], regname: 'a', operator: 'y', regtype: 'v',
2144 \ visual: v:false, inclusive: v:false},
2145 \ g:event)
2146 " inclusive motion
2147 norm 0"ay$
2148 call assert_equal(
2149 \ #{regcontents: ['foobar'], regname: 'a', operator: 'y', regtype: 'v',
2150 \ visual: v:false, inclusive: v:true},
2151 \ g:event)
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01002152
2153 call assert_equal({}, v:event)
2154
Bram Moolenaarfccbf062020-11-26 20:34:00 +01002155 if has('clipboard_working') && !has('gui_running')
2156 " Test that when the visual selection is automatically copied to clipboard
2157 " register a TextYankPost is emitted
2158 call setline(1, ['foobar'])
2159
2160 let @* = ''
2161 set clipboard=autoselect
2162 exe "norm! ggviw\<Esc>"
2163 call assert_equal(
Bram Moolenaara016eeb2022-04-09 11:37:38 +01002164 \ #{regcontents: ['foobar'], regname: '*', operator: 'y',
2165 \ regtype: 'v', visual: v:true, inclusive: v:false},
2166 \ g:event)
Bram Moolenaarfccbf062020-11-26 20:34:00 +01002167
2168 let @+ = ''
2169 set clipboard=autoselectplus
2170 exe "norm! ggviw\<Esc>"
2171 call assert_equal(
Bram Moolenaara016eeb2022-04-09 11:37:38 +01002172 \ #{regcontents: ['foobar'], regname: '+', operator: 'y',
2173 \ regtype: 'v', visual: v:true, inclusive: v:false},
2174 \ g:event)
Bram Moolenaarfccbf062020-11-26 20:34:00 +01002175
2176 set clipboard&vim
2177 endif
2178
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01002179 au! TextYankPost
2180 unlet g:event
2181 bwipe!
2182endfunc
Bram Moolenaar9bca8052017-12-18 12:37:55 +01002183
Bram Moolenaar9a046fd2021-01-28 13:47:59 +01002184func Test_autocommand_all_events()
2185 call assert_fails('au * * bwipe', 'E1155:')
2186 call assert_fails('au * x bwipe', 'E1155:')
Bram Moolenaarb6db1462021-12-24 19:24:47 +00002187 call assert_fails('au! * x bwipe', 'E1155:')
Bram Moolenaar4fb921e2017-12-18 15:33:00 +01002188endfunc
Bram Moolenaarb7407d32018-02-03 17:36:27 +01002189
Bram Moolenaarf6246f52022-02-11 16:30:12 +00002190func Test_autocmd_user()
2191 au User MyEvent let s:res = [expand("<afile>"), expand("<amatch>")]
2192 doautocmd User MyEvent
2193 call assert_equal(['MyEvent', 'MyEvent'], s:res)
2194 au! User
2195 unlet s:res
2196endfunc
2197
Bram Moolenaar3b014be2022-11-13 17:53:46 +00002198func Test_autocmd_user_clear_group()
2199 CheckRunVimInTerminal
2200
2201 let lines =<< trim END
2202 autocmd! User
2203 for i in range(1, 999)
2204 exe 'autocmd User ' .. 'Foo' .. i .. ' bar'
2205 endfor
2206 au CmdlineLeave : call timer_start(0, {-> execute('autocmd! User')})
2207 END
2208 call writefile(lines, 'XautoUser', 'D')
2209 let buf = RunVimInTerminal('-S XautoUser', {'rows': 10})
2210
2211 " this was using freed memory
2212 call term_sendkeys(buf, ":autocmd User\<CR>")
2213 call TermWait(buf, 50)
2214 call term_sendkeys(buf, "G")
2215
2216 call StopVimInTerminal(buf)
2217endfunc
2218
Bram Moolenaarb7407d32018-02-03 17:36:27 +01002219function s:Before_test_dirchanged()
2220 augroup test_dirchanged
2221 autocmd!
2222 augroup END
2223 let s:li = []
2224 let s:dir_this = getcwd()
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02002225 let s:dir_foo = s:dir_this . '/Xfoo'
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01002226 call mkdir(s:dir_foo)
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02002227 let s:dir_bar = s:dir_this . '/Xbar'
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01002228 call mkdir(s:dir_bar)
Bram Moolenaarb7407d32018-02-03 17:36:27 +01002229endfunc
2230
2231function s:After_test_dirchanged()
Bram Moolenaar3503d7c2019-11-09 20:10:17 +01002232 call chdir(s:dir_this)
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01002233 call delete(s:dir_foo, 'd')
2234 call delete(s:dir_bar, 'd')
Bram Moolenaarb7407d32018-02-03 17:36:27 +01002235 augroup test_dirchanged
2236 autocmd!
2237 augroup END
2238endfunc
2239
2240function Test_dirchanged_global()
2241 call s:Before_test_dirchanged()
Bram Moolenaarf6246f52022-02-11 16:30:12 +00002242 autocmd test_dirchanged DirChangedPre global call add(s:li, expand("<amatch>") .. " pre cd " .. v:event.directory)
Bram Moolenaarb7407d32018-02-03 17:36:27 +01002243 autocmd test_dirchanged DirChanged global call add(s:li, "cd:")
2244 autocmd test_dirchanged DirChanged global call add(s:li, expand("<afile>"))
Bram Moolenaar3503d7c2019-11-09 20:10:17 +01002245 call chdir(s:dir_foo)
Bram Moolenaarf6246f52022-02-11 16:30:12 +00002246 let expected = ["global pre cd " .. s:dir_foo, "cd:", s:dir_foo]
Bram Moolenaar28e8f732022-02-09 12:58:20 +00002247 call assert_equal(expected, s:li)
Bram Moolenaar3503d7c2019-11-09 20:10:17 +01002248 call chdir(s:dir_foo)
Bram Moolenaar28e8f732022-02-09 12:58:20 +00002249 call assert_equal(expected, s:li)
Bram Moolenaar3503d7c2019-11-09 20:10:17 +01002250 exe 'lcd ' .. fnameescape(s:dir_bar)
Bram Moolenaar28e8f732022-02-09 12:58:20 +00002251 call assert_equal(expected, s:li)
Bram Moolenaard8c9d322022-06-12 11:49:16 +01002252
2253 exe 'cd ' .. s:dir_foo
2254 exe 'cd ' .. s:dir_bar
2255 autocmd! test_dirchanged DirChanged global let g:result = expand("<afile>")
2256 cd -
Bram Moolenaardb77c492022-06-12 23:26:50 +01002257 call assert_equal(s:dir_foo, substitute(g:result, '\\', '/', 'g'))
Bram Moolenaard8c9d322022-06-12 11:49:16 +01002258
Bram Moolenaarb7407d32018-02-03 17:36:27 +01002259 call s:After_test_dirchanged()
2260endfunc
2261
2262function Test_dirchanged_local()
2263 call s:Before_test_dirchanged()
2264 autocmd test_dirchanged DirChanged window call add(s:li, "lcd:")
2265 autocmd test_dirchanged DirChanged window call add(s:li, expand("<afile>"))
Bram Moolenaar3503d7c2019-11-09 20:10:17 +01002266 call chdir(s:dir_foo)
Bram Moolenaarb7407d32018-02-03 17:36:27 +01002267 call assert_equal([], s:li)
Bram Moolenaar3503d7c2019-11-09 20:10:17 +01002268 exe 'lcd ' .. fnameescape(s:dir_bar)
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01002269 call assert_equal(["lcd:", s:dir_bar], s:li)
Bram Moolenaar3503d7c2019-11-09 20:10:17 +01002270 exe 'lcd ' .. fnameescape(s:dir_bar)
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01002271 call assert_equal(["lcd:", s:dir_bar], s:li)
Bram Moolenaarb7407d32018-02-03 17:36:27 +01002272 call s:After_test_dirchanged()
2273endfunc
2274
2275function Test_dirchanged_auto()
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +02002276 CheckOption autochdir
Bram Moolenaarb7407d32018-02-03 17:36:27 +01002277 call s:Before_test_dirchanged()
2278 call test_autochdir()
Bram Moolenaar28e8f732022-02-09 12:58:20 +00002279 autocmd test_dirchanged DirChangedPre auto call add(s:li, "pre cd " .. v:event.directory)
Bram Moolenaarb7407d32018-02-03 17:36:27 +01002280 autocmd test_dirchanged DirChanged auto call add(s:li, "auto:")
2281 autocmd test_dirchanged DirChanged auto call add(s:li, expand("<afile>"))
2282 set acd
Bram Moolenaar3503d7c2019-11-09 20:10:17 +01002283 cd ..
Bram Moolenaarb7407d32018-02-03 17:36:27 +01002284 call assert_equal([], s:li)
Bram Moolenaar61abe7d2022-08-30 21:46:08 +01002285 exe 'edit ' . s:dir_foo . '/Xautofile'
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01002286 call assert_equal(s:dir_foo, getcwd())
Bram Moolenaar28e8f732022-02-09 12:58:20 +00002287 let expected = ["pre cd " .. s:dir_foo, "auto:", s:dir_foo]
2288 call assert_equal(expected, s:li)
Bram Moolenaarb7407d32018-02-03 17:36:27 +01002289 set noacd
2290 bwipe!
2291 call s:After_test_dirchanged()
2292endfunc
Bram Moolenaar5a093432018-02-10 18:15:19 +01002293
2294" Test TextChangedI and TextChangedP
2295func Test_ChangedP()
2296 new
2297 call setline(1, ['foo', 'bar', 'foobar'])
2298 call test_override("char_avail", 1)
2299 set complete=. completeopt=menuone
2300
2301 func! TextChangedAutocmd(char)
2302 let g:autocmd .= a:char
2303 endfunc
2304
Christian Brabandtdb3b4462021-10-16 11:58:55 +01002305 " TextChanged will not be triggered, only check that it isn't.
Bram Moolenaar5a093432018-02-10 18:15:19 +01002306 au! TextChanged <buffer> :call TextChangedAutocmd('N')
2307 au! TextChangedI <buffer> :call TextChangedAutocmd('I')
2308 au! TextChangedP <buffer> :call TextChangedAutocmd('P')
2309
2310 call cursor(3, 1)
2311 let g:autocmd = ''
2312 call feedkeys("o\<esc>", 'tnix')
2313 call assert_equal('I', g:autocmd)
2314
2315 let g:autocmd = ''
2316 call feedkeys("Sf", 'tnix')
2317 call assert_equal('II', g:autocmd)
2318
2319 let g:autocmd = ''
2320 call feedkeys("Sf\<C-N>", 'tnix')
2321 call assert_equal('IIP', g:autocmd)
2322
2323 let g:autocmd = ''
2324 call feedkeys("Sf\<C-N>\<C-N>", 'tnix')
2325 call assert_equal('IIPP', g:autocmd)
2326
2327 let g:autocmd = ''
2328 call feedkeys("Sf\<C-N>\<C-N>\<C-N>", 'tnix')
2329 call assert_equal('IIPPP', g:autocmd)
2330
2331 let g:autocmd = ''
2332 call feedkeys("Sf\<C-N>\<C-N>\<C-N>\<C-N>", 'tnix')
2333 call assert_equal('IIPPPP', g:autocmd)
2334
2335 call assert_equal(['foo', 'bar', 'foobar', 'foo'], getline(1, '$'))
2336 " TODO: how should it handle completeopt=noinsert,noselect?
2337
2338 " CleanUp
2339 call test_override("char_avail", 0)
2340 au! TextChanged
2341 au! TextChangedI
2342 au! TextChangedP
2343 delfu TextChangedAutocmd
2344 unlet! g:autocmd
2345 set complete&vim completeopt&vim
2346
2347 bw!
2348endfunc
Bram Moolenaar8c64a362018-03-23 22:39:31 +01002349
Bram Moolenaar91d2e782018-08-07 19:05:01 +02002350let g:setline_handled = v:false
Bram Moolenaar1e115362019-01-09 23:01:02 +01002351func SetLineOne()
Bram Moolenaar91d2e782018-08-07 19:05:01 +02002352 if !g:setline_handled
2353 call setline(1, "(x)")
2354 let g:setline_handled = v:true
2355 endif
2356endfunc
2357
2358func Test_TextChangedI_with_setline()
2359 new
2360 call test_override('char_avail', 1)
2361 autocmd TextChangedI <buffer> call SetLineOne()
2362 call feedkeys("i(\<CR>\<Esc>", 'tx')
2363 call assert_equal('(', getline(1))
2364 call assert_equal('x)', getline(2))
2365 undo
Bram Moolenaar91d2e782018-08-07 19:05:01 +02002366 call assert_equal('', getline(1))
Bram Moolenaar9fa95062018-08-08 22:08:32 +02002367 call assert_equal('', getline(2))
Bram Moolenaar91d2e782018-08-07 19:05:01 +02002368
Bram Moolenaarca34db32022-01-20 11:17:18 +00002369 call test_override('char_avail', 0)
Bram Moolenaar91d2e782018-08-07 19:05:01 +02002370 bwipe!
2371endfunc
2372
Bram Moolenaar8c64a362018-03-23 22:39:31 +01002373func Test_Changed_FirstTime()
Bram Moolenaar8c5a2782019-08-07 23:07:07 +02002374 CheckFeature terminal
2375 CheckNotGui
Bram Moolenaar3cdcb092020-03-18 19:18:10 +01002376 " Starting a terminal to run Vim is always considered flaky.
Bram Moolenaar30d53e22020-03-18 21:10:44 +01002377 let g:test_is_flaky = 1
Bram Moolenaar8c5a2782019-08-07 23:07:07 +02002378
Bram Moolenaar8c64a362018-03-23 22:39:31 +01002379 " Prepare file for TextChanged event.
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01002380 call writefile([''], 'Xchanged.txt', 'D')
Bram Moolenaar8c64a362018-03-23 22:39:31 +01002381 let buf = term_start([GetVimProg(), '--clean', '-c', 'set noswapfile'], {'term_rows': 3})
2382 call assert_equal('running', term_getstatus(buf))
Bram Moolenaar1834d372018-03-29 17:40:46 +02002383 " Wait for the ruler (in the status line) to be shown.
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01002384 " In ConPTY, there is additional character which is drawn up to the width of
2385 " the screen.
2386 if has('conpty')
2387 call WaitForAssert({-> assert_match('\<All.*$', term_getline(buf, 3))})
2388 else
2389 call WaitForAssert({-> assert_match('\<All$', term_getline(buf, 3))})
2390 endif
Bram Moolenaar8c64a362018-03-23 22:39:31 +01002391 " It's only adding autocmd, so that no event occurs.
2392 call term_sendkeys(buf, ":au! TextChanged <buffer> call writefile(['No'], 'Xchanged.txt')\<cr>")
2393 call term_sendkeys(buf, "\<C-\\>\<C-N>:qa!\<cr>")
Bram Moolenaar50182fa2018-04-28 21:34:40 +02002394 call WaitForAssert({-> assert_equal('finished', term_getstatus(buf))})
Bram Moolenaar8c64a362018-03-23 22:39:31 +01002395 call assert_equal([''], readfile('Xchanged.txt'))
2396
2397 " clean up
Bram Moolenaar8c64a362018-03-23 22:39:31 +01002398 bwipe!
2399endfunc
Bram Moolenaar0566e892019-01-24 19:37:40 +01002400
Bram Moolenaareb93f3f2019-04-04 15:04:56 +02002401func Test_autocmd_nested()
2402 let g:did_nested = 0
2403 augroup Testing
2404 au WinNew * edit somefile
2405 au BufNew * let g:did_nested = 1
2406 augroup END
2407 split
2408 call assert_equal(0, g:did_nested)
2409 close
2410 bwipe! somefile
2411
2412 " old nested argument still works
2413 augroup Testing
2414 au!
2415 au WinNew * nested edit somefile
2416 au BufNew * let g:did_nested = 1
2417 augroup END
2418 split
2419 call assert_equal(1, g:did_nested)
2420 close
2421 bwipe! somefile
2422
2423 " New ++nested argument works
2424 augroup Testing
2425 au!
2426 au WinNew * ++nested edit somefile
2427 au BufNew * let g:did_nested = 1
2428 augroup END
2429 split
2430 call assert_equal(1, g:did_nested)
2431 close
2432 bwipe! somefile
2433
Bram Moolenaarf0775142022-03-04 20:10:38 +00002434 " nested without ++ does not work in Vim9 script
2435 call assert_fails('vim9cmd au WinNew * nested echo fails', 'E1078:')
2436
Bram Moolenaareb93f3f2019-04-04 15:04:56 +02002437 augroup Testing
2438 au!
2439 augroup END
2440
2441 call assert_fails('au WinNew * ++nested ++nested echo bad', 'E983:')
2442 call assert_fails('au WinNew * nested nested echo bad', 'E983:')
2443endfunc
2444
Bram Moolenaar5fa9f232022-07-23 09:06:48 +01002445func Test_autocmd_nested_cursor_invalid()
2446 set laststatus=0
2447 copen
2448 cclose
2449 call setline(1, ['foo', 'bar', 'baz'])
2450 3
2451 augroup nested_inv
2452 autocmd User foo ++nested copen
2453 autocmd BufAdd * let &laststatus = 2 - &laststatus
2454 augroup END
2455 doautocmd User foo
2456
2457 augroup nested_inv
2458 au!
2459 augroup END
2460 set laststatus&
Bram Moolenaarb03950f2022-07-26 13:47:13 +01002461 cclose
Bram Moolenaar5fa9f232022-07-23 09:06:48 +01002462 bwipe!
2463endfunc
2464
Bram Moolenaar3d6ee8b2022-07-27 15:23:35 +01002465func Test_autocmd_nested_keeps_cursor_pos()
2466 enew
2467 call setline(1, 'foo')
2468 autocmd User foo ++nested normal! $a
2469 autocmd InsertLeave * :
2470 doautocmd User foo
2471 call assert_equal([0, 1, 3, 0], getpos('.'))
2472
2473 bwipe!
2474endfunc
2475
Bram Moolenaarb03950f2022-07-26 13:47:13 +01002476func Test_autocmd_nested_switch_window()
2477 " run this in a separate Vim so that SafeState works
2478 CheckRunVimInTerminal
2479
2480 let lines =<< trim END
2481 vim9script
2482 ['()']->writefile('Xautofile')
2483 autocmd VimEnter * ++nested edit Xautofile | split
2484 autocmd BufReadPost * autocmd SafeState * ++once foldclosed('.')
2485 autocmd WinEnter * matchadd('ErrorMsg', 'pat')
2486 END
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01002487 call writefile(lines, 'Xautoscript', 'D')
Bram Moolenaarb03950f2022-07-26 13:47:13 +01002488 let buf = RunVimInTerminal('-S Xautoscript', {'rows': 10})
2489 call VerifyScreenDump(buf, 'Test_autocmd_nested_switch', {})
2490
2491 call StopVimInTerminal(buf)
2492 call delete('Xautofile')
Bram Moolenaarb03950f2022-07-26 13:47:13 +01002493endfunc
2494
Bram Moolenaareb93f3f2019-04-04 15:04:56 +02002495func Test_autocmd_once()
2496 " Without ++once WinNew triggers twice
2497 let g:did_split = 0
2498 augroup Testing
2499 au WinNew * let g:did_split += 1
2500 augroup END
2501 split
2502 split
2503 call assert_equal(2, g:did_split)
2504 call assert_true(exists('#WinNew'))
2505 close
2506 close
2507
2508 " With ++once WinNew triggers once
2509 let g:did_split = 0
2510 augroup Testing
2511 au!
2512 au WinNew * ++once let g:did_split += 1
2513 augroup END
2514 split
2515 split
2516 call assert_equal(1, g:did_split)
2517 call assert_false(exists('#WinNew'))
2518 close
2519 close
2520
2521 call assert_fails('au WinNew * ++once ++once echo bad', 'E983:')
2522endfunc
2523
Bram Moolenaara68e5952019-04-25 22:22:01 +02002524func Test_autocmd_bufreadpre()
2525 new
2526 let b:bufreadpre = 1
Bram Moolenaarab505b12020-03-23 19:28:44 +01002527 call append(0, range(1000))
Bram Moolenaara68e5952019-04-25 22:22:01 +02002528 w! XAutocmdBufReadPre.txt
2529 autocmd BufReadPre <buffer> :let b:bufreadpre += 1
Bram Moolenaarab505b12020-03-23 19:28:44 +01002530 norm! 500gg
Bram Moolenaara68e5952019-04-25 22:22:01 +02002531 sp
Bram Moolenaarab505b12020-03-23 19:28:44 +01002532 norm! 1000gg
Bram Moolenaara68e5952019-04-25 22:22:01 +02002533 wincmd p
2534 let g:wsv1 = winsaveview()
2535 wincmd p
2536 let g:wsv2 = winsaveview()
2537 " triggers BufReadPre, should not move the cursor in either window
2538 " The topline may change one line in a large window.
2539 edit
2540 call assert_inrange(g:wsv2.topline - 1, g:wsv2.topline + 1, winsaveview().topline)
2541 call assert_equal(g:wsv2.lnum, winsaveview().lnum)
2542 call assert_equal(2, b:bufreadpre)
2543 wincmd p
2544 call assert_equal(g:wsv1.topline, winsaveview().topline)
2545 call assert_equal(g:wsv1.lnum, winsaveview().lnum)
2546 call assert_equal(2, b:bufreadpre)
2547 " Now set the cursor position in an BufReadPre autocommand
2548 " (even though the position will be invalid, this should make Vim reset the
2549 " cursor position in the other window.
2550 wincmd p
2551 set cpo+=g
2552 " won't do anything, but try to set the cursor on an invalid lnum
2553 autocmd BufReadPre <buffer> :norm! 70gg
2554 " triggers BufReadPre, should not move the cursor in either window
2555 e
2556 call assert_equal(1, winsaveview().topline)
2557 call assert_equal(1, winsaveview().lnum)
2558 call assert_equal(3, b:bufreadpre)
2559 wincmd p
2560 call assert_equal(g:wsv1.topline, winsaveview().topline)
2561 call assert_equal(g:wsv1.lnum, winsaveview().lnum)
2562 call assert_equal(3, b:bufreadpre)
2563 close
2564 close
2565 call delete('XAutocmdBufReadPre.txt')
2566 set cpo-=g
2567endfunc
2568
Bram Moolenaar5e66b422019-01-24 21:58:10 +01002569" FileChangedShell tested in test_filechanged.vim
Bram Moolenaar69ea5872019-04-25 20:29:00 +02002570
2571" Tests for the following autocommands:
2572" - FileWritePre writing a compressed file
2573" - FileReadPost reading a compressed file
2574" - BufNewFile reading a file template
2575" - BufReadPre decompressing the file to be read
2576" - FilterReadPre substituting characters in the temp file
2577" - FilterReadPost substituting characters after filtering
2578" - FileReadPre set options for decompression
2579" - FileReadPost decompress the file
2580func Test_ReadWrite_Autocmds()
2581 " Run this test only on Unix-like systems and if gzip is available
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +02002582 CheckUnix
2583 CheckExecutable gzip
Bram Moolenaar69ea5872019-04-25 20:29:00 +02002584
2585 " Make $GZIP empty, "-v" would cause trouble.
2586 let $GZIP = ""
2587
2588 " Use a FileChangedShell autocommand to avoid a prompt for 'Xtestfile.gz'
2589 " being modified outside of Vim (noticed on Solaris).
2590 au FileChangedShell * echo 'caught FileChangedShell'
2591
2592 " Test for the FileReadPost, FileWritePre and FileWritePost autocmds
2593 augroup Test1
2594 au!
2595 au FileWritePre *.gz '[,']!gzip
2596 au FileWritePost *.gz undo
2597 au FileReadPost *.gz '[,']!gzip -d
2598 augroup END
2599
2600 new
2601 set bin
2602 call append(0, [
2603 \ 'line 2 Abcdefghijklmnopqrstuvwxyz',
2604 \ 'line 3 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2605 \ 'line 4 Abcdefghijklmnopqrstuvwxyz',
2606 \ 'line 5 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2607 \ 'line 6 Abcdefghijklmnopqrstuvwxyz',
2608 \ 'line 7 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2609 \ 'line 8 Abcdefghijklmnopqrstuvwxyz',
2610 \ 'line 9 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2611 \ 'line 10 Abcdefghijklmnopqrstuvwxyz'
2612 \ ])
2613 1,9write! Xtestfile.gz
2614 enew! | close
2615
2616 new
2617 " Read and decompress the testfile
2618 0read Xtestfile.gz
2619 call assert_equal([
2620 \ 'line 2 Abcdefghijklmnopqrstuvwxyz',
2621 \ 'line 3 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2622 \ 'line 4 Abcdefghijklmnopqrstuvwxyz',
2623 \ 'line 5 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2624 \ 'line 6 Abcdefghijklmnopqrstuvwxyz',
2625 \ 'line 7 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2626 \ 'line 8 Abcdefghijklmnopqrstuvwxyz',
2627 \ 'line 9 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2628 \ 'line 10 Abcdefghijklmnopqrstuvwxyz'
2629 \ ], getline(1, 9))
2630 enew! | close
2631
2632 augroup Test1
2633 au!
2634 augroup END
2635
2636 " Test for the FileAppendPre and FileAppendPost autocmds
2637 augroup Test2
2638 au!
2639 au BufNewFile *.c read Xtest.c
2640 au FileAppendPre *.out '[,']s/new/NEW/
2641 au FileAppendPost *.out !cat Xtest.c >> test.out
2642 augroup END
2643
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01002644 call writefile(['/*', ' * Here is a new .c file', ' */'], 'Xtest.c', 'D')
Bram Moolenaar69ea5872019-04-25 20:29:00 +02002645 new foo.c " should load Xtest.c
2646 call assert_equal(['/*', ' * Here is a new .c file', ' */'], getline(2, 4))
2647 w! >> test.out " append it to the output file
2648
2649 let contents = readfile('test.out')
2650 call assert_equal(' * Here is a NEW .c file', contents[2])
2651 call assert_equal(' * Here is a new .c file', contents[5])
2652
2653 call delete('test.out')
2654 enew! | close
2655 augroup Test2
2656 au!
2657 augroup END
2658
2659 " Test for the BufReadPre and BufReadPost autocmds
2660 augroup Test3
2661 au!
2662 " setup autocommands to decompress before reading and re-compress
2663 " afterwards
2664 au BufReadPre *.gz exe '!gzip -d ' . shellescape(expand("<afile>"))
2665 au BufReadPre *.gz call rename(expand("<afile>:r"), expand("<afile>"))
2666 au BufReadPost *.gz call rename(expand("<afile>"), expand("<afile>:r"))
2667 au BufReadPost *.gz exe '!gzip ' . shellescape(expand("<afile>:r"))
2668 augroup END
2669
2670 e! Xtestfile.gz " Edit compressed file
2671 call assert_equal([
2672 \ 'line 2 Abcdefghijklmnopqrstuvwxyz',
2673 \ 'line 3 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2674 \ 'line 4 Abcdefghijklmnopqrstuvwxyz',
2675 \ 'line 5 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2676 \ 'line 6 Abcdefghijklmnopqrstuvwxyz',
2677 \ 'line 7 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2678 \ 'line 8 Abcdefghijklmnopqrstuvwxyz',
2679 \ 'line 9 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2680 \ 'line 10 Abcdefghijklmnopqrstuvwxyz'
2681 \ ], getline(1, 9))
2682
2683 w! >> test.out " Append it to the output file
2684
2685 augroup Test3
2686 au!
2687 augroup END
2688
2689 " Test for the FilterReadPre and FilterReadPost autocmds.
2690 set shelltemp " need temp files here
2691 augroup Test4
2692 au!
2693 au FilterReadPre *.out call rename(expand("<afile>"), expand("<afile>") . ".t")
2694 au FilterReadPre *.out exe 'silent !sed s/e/E/ ' . shellescape(expand("<afile>")) . ".t >" . shellescape(expand("<afile>"))
2695 au FilterReadPre *.out exe 'silent !rm ' . shellescape(expand("<afile>")) . '.t'
2696 au FilterReadPost *.out '[,']s/x/X/g
2697 augroup END
2698
2699 e! test.out " Edit the output file
2700 1,$!cat
2701 call assert_equal([
2702 \ 'linE 2 AbcdefghijklmnopqrstuvwXyz',
2703 \ 'linE 3 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
2704 \ 'linE 4 AbcdefghijklmnopqrstuvwXyz',
2705 \ 'linE 5 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
2706 \ 'linE 6 AbcdefghijklmnopqrstuvwXyz',
2707 \ 'linE 7 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
2708 \ 'linE 8 AbcdefghijklmnopqrstuvwXyz',
2709 \ 'linE 9 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
2710 \ 'linE 10 AbcdefghijklmnopqrstuvwXyz'
2711 \ ], getline(1, 9))
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 \ ], readfile('test.out'))
2723
2724 augroup Test4
2725 au!
2726 augroup END
2727 set shelltemp&vim
2728
2729 " Test for the FileReadPre and FileReadPost autocmds.
2730 augroup Test5
2731 au!
2732 au FileReadPre *.gz exe 'silent !gzip -d ' . shellescape(expand("<afile>"))
2733 au FileReadPre *.gz call rename(expand("<afile>:r"), expand("<afile>"))
2734 au FileReadPost *.gz '[,']s/l/L/
2735 augroup END
2736
2737 new
2738 0r Xtestfile.gz " Read compressed file
2739 call assert_equal([
2740 \ 'Line 2 Abcdefghijklmnopqrstuvwxyz',
2741 \ 'Line 3 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2742 \ 'Line 4 Abcdefghijklmnopqrstuvwxyz',
2743 \ 'Line 5 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2744 \ 'Line 6 Abcdefghijklmnopqrstuvwxyz',
2745 \ 'Line 7 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2746 \ 'Line 8 Abcdefghijklmnopqrstuvwxyz',
2747 \ 'Line 9 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2748 \ 'Line 10 Abcdefghijklmnopqrstuvwxyz'
2749 \ ], getline(1, 9))
2750 call assert_equal([
2751 \ 'line 2 Abcdefghijklmnopqrstuvwxyz',
2752 \ 'line 3 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2753 \ 'line 4 Abcdefghijklmnopqrstuvwxyz',
2754 \ 'line 5 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2755 \ 'line 6 Abcdefghijklmnopqrstuvwxyz',
2756 \ 'line 7 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2757 \ 'line 8 Abcdefghijklmnopqrstuvwxyz',
2758 \ 'line 9 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2759 \ 'line 10 Abcdefghijklmnopqrstuvwxyz'
2760 \ ], readfile('Xtestfile.gz'))
2761
2762 augroup Test5
2763 au!
2764 augroup END
2765
2766 au! FileChangedShell
2767 call delete('Xtestfile.gz')
Bram Moolenaar69ea5872019-04-25 20:29:00 +02002768 call delete('test.out')
2769endfunc
Bram Moolenaar23b51392019-05-09 21:38:43 +02002770
2771func Test_throw_in_BufWritePre()
2772 new
2773 call setline(1, ['one', 'two', 'three'])
2774 call assert_false(filereadable('Xthefile'))
2775 augroup throwing
2776 au BufWritePre X* throw 'do not write'
2777 augroup END
2778 try
2779 w Xthefile
2780 catch
2781 let caught = 1
2782 endtry
2783 call assert_equal(1, caught)
2784 call assert_false(filereadable('Xthefile'))
2785
2786 bwipe!
2787 au! throwing
2788endfunc
Bram Moolenaarcadbe1b2019-09-22 21:50:09 +02002789
Bram Moolenaar40fa12a2021-09-22 14:18:13 +02002790func Test_autocmd_in_try_block()
Bram Moolenaar6f14da12022-09-07 21:30:44 +01002791 call mkdir('Xintrydir', 'R')
Bram Moolenaar40fa12a2021-09-22 14:18:13 +02002792 au BufEnter * let g:fname = expand('%')
2793 try
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +01002794 edit Xintrydir/
Bram Moolenaar40fa12a2021-09-22 14:18:13 +02002795 endtry
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +01002796 call assert_match('Xintrydir', g:fname)
Bram Moolenaar40fa12a2021-09-22 14:18:13 +02002797
2798 unlet g:fname
2799 au! BufEnter
Bram Moolenaar40fa12a2021-09-22 14:18:13 +02002800endfunc
2801
Bram Moolenaarcadbe1b2019-09-22 21:50:09 +02002802func Test_autocmd_SafeState()
2803 CheckRunVimInTerminal
2804
2805 let lines =<< trim END
2806 let g:safe = 0
2807 let g:again = ''
2808 au SafeState * let g:safe += 1
2809 au SafeStateAgain * let g:again ..= 'x'
2810 func CallTimer()
2811 call timer_start(10, {id -> execute('let g:again ..= "t"')})
2812 endfunc
2813 END
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01002814 call writefile(lines, 'XSafeState', 'D')
Bram Moolenaarcadbe1b2019-09-22 21:50:09 +02002815 let buf = RunVimInTerminal('-S XSafeState', #{rows: 6})
2816
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01002817 " Sometimes we loop to handle a K_IGNORE, SafeState may be triggered once or
Bram Moolenaar8fb1b472020-02-23 16:16:26 +01002818 " more often.
Bram Moolenaarcadbe1b2019-09-22 21:50:09 +02002819 call term_sendkeys(buf, ":echo g:safe\<CR>")
Bram Moolenaar8fb1b472020-02-23 16:16:26 +01002820 call WaitForAssert({-> assert_match('^\d ', term_getline(buf, 6))}, 1000)
Bram Moolenaarcadbe1b2019-09-22 21:50:09 +02002821
Bram Moolenaar8fb1b472020-02-23 16:16:26 +01002822 " SafeStateAgain should be invoked at least three times
Bram Moolenaarcadbe1b2019-09-22 21:50:09 +02002823 call term_sendkeys(buf, ":echo g:again\<CR>")
Bram Moolenaar8fb1b472020-02-23 16:16:26 +01002824 call WaitForAssert({-> assert_match('^xxx', term_getline(buf, 6))}, 1000)
Bram Moolenaarcadbe1b2019-09-22 21:50:09 +02002825
2826 call term_sendkeys(buf, ":let g:again = ''\<CR>:call CallTimer()\<CR>")
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02002827 call TermWait(buf, 50)
Bram Moolenaar0f6629a2019-09-22 23:24:13 +02002828 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02002829 call TermWait(buf, 50)
Bram Moolenaarcadbe1b2019-09-22 21:50:09 +02002830 call term_sendkeys(buf, ":echo g:again\<CR>")
2831 call WaitForAssert({-> assert_match('xtx', term_getline(buf, 6))}, 1000)
2832
2833 call StopVimInTerminal(buf)
Bram Moolenaarcadbe1b2019-09-22 21:50:09 +02002834endfunc
Bram Moolenaar23324a02019-10-01 17:39:04 +02002835
2836func Test_autocmd_CmdWinEnter()
2837 CheckRunVimInTerminal
Bram Moolenaar21829c52021-01-26 22:42:21 +01002838
Bram Moolenaar23324a02019-10-01 17:39:04 +02002839 let lines =<< trim END
Egor Zvorykin125ffd22021-11-17 14:01:14 +00002840 augroup vimHints | au! | augroup END
Bram Moolenaar23324a02019-10-01 17:39:04 +02002841 let b:dummy_var = 'This is a dummy'
2842 autocmd CmdWinEnter * quit
2843 let winnr = winnr('$')
2844 END
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01002845 let filename = 'XCmdWinEnter'
Bram Moolenaar23324a02019-10-01 17:39:04 +02002846 call writefile(lines, filename)
2847 let buf = RunVimInTerminal('-S '.filename, #{rows: 6})
2848
2849 call term_sendkeys(buf, "q:")
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02002850 call TermWait(buf)
Bram Moolenaar23324a02019-10-01 17:39:04 +02002851 call term_sendkeys(buf, ":echo b:dummy_var\<cr>")
Bram Moolenaar353c3512020-03-15 14:19:26 +01002852 call WaitForAssert({-> assert_match('^This is a dummy', term_getline(buf, 6))}, 2000)
Bram Moolenaar23324a02019-10-01 17:39:04 +02002853 call term_sendkeys(buf, ":echo &buftype\<cr>")
2854 call WaitForAssert({-> assert_notmatch('^nofile', term_getline(buf, 6))}, 1000)
2855 call term_sendkeys(buf, ":echo winnr\<cr>")
2856 call WaitForAssert({-> assert_match('^1', term_getline(buf, 6))}, 1000)
2857
2858 " clean up
2859 call StopVimInTerminal(buf)
2860 call delete(filename)
2861endfunc
Bram Moolenaarec66c412019-10-11 21:19:13 +02002862
2863func Test_autocmd_was_using_freed_memory()
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01002864 CheckFeature quickfix
2865
Bram Moolenaarec66c412019-10-11 21:19:13 +02002866 pedit xx
2867 n x
Bram Moolenaar92bb83e2021-02-03 23:04:46 +01002868 augroup winenter
2869 au WinEnter * if winnr('$') > 2 | quit | endif
2870 augroup END
Bram Moolenaarec66c412019-10-11 21:19:13 +02002871 split
Bram Moolenaar92bb83e2021-02-03 23:04:46 +01002872
2873 augroup winenter
2874 au! WinEnter
2875 augroup END
2876
2877 bwipe xx
2878 bwipe x
2879 pclose
Bram Moolenaarec66c412019-10-11 21:19:13 +02002880endfunc
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +01002881
2882func Test_BufWrite_lockmarks()
Bram Moolenaarf08b0eb2021-10-16 13:00:14 +01002883 let g:test_is_flaky = 1
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +01002884 edit! Xtest
2885 call setline(1, ['a', 'b', 'c', 'd'])
2886
2887 " :lockmarks preserves the marks
2888 call SetChangeMarks(2, 3)
2889 lockmarks write
2890 call assert_equal([2, 3], [line("'["), line("']")])
2891
2892 " *WritePre autocmds get the correct line range, but lockmarks preserves the
2893 " original values for the user
2894 augroup lockmarks
2895 au!
2896 au BufWritePre,FilterWritePre * call assert_equal([1, 4], [line("'["), line("']")])
2897 au FileWritePre * call assert_equal([3, 4], [line("'["), line("']")])
2898 augroup END
2899
2900 lockmarks write
2901 call assert_equal([2, 3], [line("'["), line("']")])
2902
2903 if executable('cat')
2904 lockmarks %!cat
2905 call assert_equal([2, 3], [line("'["), line("']")])
2906 endif
2907
2908 lockmarks 3,4write Xtest2
2909 call assert_equal([2, 3], [line("'["), line("']")])
2910
2911 au! lockmarks
2912 augroup! lockmarks
2913 call delete('Xtest')
2914 call delete('Xtest2')
2915endfunc
Bram Moolenaarce6db022020-01-07 20:11:42 +01002916
2917func Test_FileType_spell()
2918 if !isdirectory('/tmp')
2919 throw "Skipped: requires /tmp directory"
2920 endif
2921
2922 " this was crashing with an invalid free()
2923 setglobal spellfile=/tmp/en.utf-8.add
2924 augroup crash
2925 autocmd!
2926 autocmd BufNewFile,BufReadPost crashfile setf somefiletype
2927 autocmd BufNewFile,BufReadPost crashfile set ft=anotherfiletype
2928 autocmd FileType anotherfiletype setlocal spell
2929 augroup END
2930 func! NoCrash() abort
2931 edit /tmp/crashfile
2932 endfunc
2933 call NoCrash()
2934
2935 au! crash
2936 setglobal spellfile=
2937endfunc
Bram Moolenaarbc2b71d2020-02-17 21:33:30 +01002938
Bram Moolenaaref976322022-09-28 11:48:30 +01002939" this was wiping out the current buffer and using freed memory
2940func Test_SpellFileMissing_bwipe()
2941 next 0
2942 au SpellFileMissing 0 bwipe
2943 call assert_fails('set spell spelllang=0', 'E937:')
2944
2945 au! SpellFileMissing
Bram Moolenaar0a60f792022-11-19 21:18:11 +00002946 set nospell spelllang=en
Bram Moolenaaref976322022-09-28 11:48:30 +01002947 bwipe
2948endfunc
2949
Bram Moolenaar406cd902020-02-18 21:54:41 +01002950" Test closing a window or editing another buffer from a FileChangedRO handler
2951" in a readonly buffer
2952func Test_FileChangedRO_winclose()
Bram Moolenaar62cd26a2020-10-11 20:08:44 +02002953 call test_override('ui_delay', 10)
2954
Bram Moolenaar406cd902020-02-18 21:54:41 +01002955 augroup FileChangedROTest
2956 au!
2957 autocmd FileChangedRO * quit
2958 augroup END
2959 new
2960 set readonly
2961 call assert_fails('normal i', 'E788:')
2962 close
2963 augroup! FileChangedROTest
2964
2965 augroup FileChangedROTest
2966 au!
Bram Moolenaar61abe7d2022-08-30 21:46:08 +01002967 autocmd FileChangedRO * edit Xrofile
Bram Moolenaar406cd902020-02-18 21:54:41 +01002968 augroup END
2969 new
2970 set readonly
2971 call assert_fails('normal i', 'E788:')
2972 close
2973 augroup! FileChangedROTest
Bram Moolenaar62cd26a2020-10-11 20:08:44 +02002974 call test_override('ALL', 0)
Bram Moolenaar406cd902020-02-18 21:54:41 +01002975endfunc
2976
Bram Moolenaar0c81d1b2020-02-22 22:45:55 +01002977func LogACmd()
2978 call add(g:logged, line('$'))
2979endfunc
2980
2981func Test_TermChanged()
Bram Moolenaard28e0b32020-02-22 23:08:52 +01002982 CheckNotGui
2983
Bram Moolenaar0c81d1b2020-02-22 22:45:55 +01002984 enew!
2985 tabnew
2986 call setline(1, ['a', 'b', 'c', 'd'])
2987 $
2988 au TermChanged * call LogACmd()
2989 let g:logged = []
2990 let term_save = &term
2991 set term=xterm
2992 call assert_equal([1, 4], g:logged)
2993
2994 au! TermChanged
2995 let &term = term_save
2996 bwipe!
2997endfunc
2998
Bram Moolenaare3284872020-03-19 13:55:03 +01002999" Test for FileReadCmd autocmd
3000func Test_autocmd_FileReadCmd()
3001 func ReadFileCmd()
3002 call append(line('$'), "v:cmdarg = " .. v:cmdarg)
3003 endfunc
3004 augroup FileReadCmdTest
3005 au!
3006 au FileReadCmd Xtest call ReadFileCmd()
3007 augroup END
3008
3009 new
3010 read ++bin Xtest
3011 read ++nobin Xtest
3012 read ++edit Xtest
3013 read ++bad=keep Xtest
3014 read ++bad=drop Xtest
3015 read ++bad=- Xtest
3016 read ++ff=unix Xtest
3017 read ++ff=dos Xtest
3018 read ++ff=mac Xtest
3019 read ++enc=utf-8 Xtest
3020
3021 call assert_equal(['',
3022 \ 'v:cmdarg = ++bin',
3023 \ 'v:cmdarg = ++nobin',
3024 \ 'v:cmdarg = ++edit',
3025 \ 'v:cmdarg = ++bad=keep',
3026 \ 'v:cmdarg = ++bad=drop',
3027 \ 'v:cmdarg = ++bad=-',
3028 \ 'v:cmdarg = ++ff=unix',
3029 \ 'v:cmdarg = ++ff=dos',
3030 \ 'v:cmdarg = ++ff=mac',
3031 \ 'v:cmdarg = ++enc=utf-8'], getline(1, '$'))
3032
3033 close!
3034 augroup FileReadCmdTest
3035 au!
3036 augroup END
3037 delfunc ReadFileCmd
3038endfunc
3039
Bram Moolenaaree4e0c12020-04-06 21:35:05 +02003040" Test for passing invalid arguments to autocmd
3041func Test_autocmd_invalid_args()
3042 " Additional character after * for event
Bram Moolenaar61abe7d2022-08-30 21:46:08 +01003043 call assert_fails('autocmd *a Xinvfile set ff=unix', 'E215:')
Bram Moolenaaree4e0c12020-04-06 21:35:05 +02003044 augroup Test
3045 augroup END
3046 " Invalid autocmd event
Bram Moolenaar61abe7d2022-08-30 21:46:08 +01003047 call assert_fails('autocmd Bufabc Xinvfile set ft=vim', 'E216:')
Bram Moolenaaree4e0c12020-04-06 21:35:05 +02003048 " Invalid autocmd event in a autocmd group
Bram Moolenaar61abe7d2022-08-30 21:46:08 +01003049 call assert_fails('autocmd Test Bufabc Xinvfile set ft=vim', 'E216:')
Bram Moolenaaree4e0c12020-04-06 21:35:05 +02003050 augroup! Test
3051 " Execute all autocmds
3052 call assert_fails('doautocmd * BufEnter', 'E217:')
3053 call assert_fails('augroup! x1a2b3', 'E367:')
3054 call assert_fails('autocmd BufNew <buffer=999> pwd', 'E680:')
Bram Moolenaar531be472020-09-23 22:38:05 +02003055 call assert_fails('autocmd BufNew \) set ff=unix', 'E55:')
Bram Moolenaaree4e0c12020-04-06 21:35:05 +02003056endfunc
3057
3058" Test for deep nesting of autocmds
3059func Test_autocmd_deep_nesting()
Bram Moolenaar61abe7d2022-08-30 21:46:08 +01003060 autocmd BufEnter Xdeepfile doautocmd BufEnter Xdeepfile
3061 call assert_fails('doautocmd BufEnter Xdeepfile', 'E218:')
3062 autocmd! BufEnter Xdeepfile
Bram Moolenaaree4e0c12020-04-06 21:35:05 +02003063endfunc
3064
Bram Moolenaarbe5ee862020-06-10 20:56:58 +02003065" Tests for SigUSR1 autocmd event, which is only available on posix systems.
3066func Test_autocmd_sigusr1()
3067 CheckUnix
Bram Moolenaar0056ca72022-09-23 21:26:39 +01003068 " FIXME: should this work on MacOS M1?
3069 CheckNotMacM1
Bram Moolenaar62cd26a2020-10-11 20:08:44 +02003070 CheckExecutable /bin/kill
Bram Moolenaarbe5ee862020-06-10 20:56:58 +02003071
3072 let g:sigusr1_passed = 0
3073 au SigUSR1 * let g:sigusr1_passed = 1
3074 call system('/bin/kill -s usr1 ' . getpid())
3075 call WaitForAssert({-> assert_true(g:sigusr1_passed)})
3076
3077 au! SigUSR1
3078 unlet g:sigusr1_passed
3079endfunc
3080
Bram Moolenaarb340bae2020-06-15 19:51:56 +02003081" Test for BufReadPre autocmd deleting the file
3082func Test_BufReadPre_delfile()
3083 augroup TestAuCmd
3084 au!
Bram Moolenaare7cda972022-08-29 11:02:59 +01003085 autocmd BufReadPre XbufreadPre call delete('XbufreadPre')
Bram Moolenaarb340bae2020-06-15 19:51:56 +02003086 augroup END
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01003087 call writefile([], 'XbufreadPre', 'D')
Bram Moolenaare7cda972022-08-29 11:02:59 +01003088 call assert_fails('new XbufreadPre', 'E200:')
3089 call assert_equal('XbufreadPre', @%)
Bram Moolenaarb340bae2020-06-15 19:51:56 +02003090 call assert_equal(1, &readonly)
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01003091
Bram Moolenaarb340bae2020-06-15 19:51:56 +02003092 augroup TestAuCmd
3093 au!
3094 augroup END
3095 close!
3096endfunc
3097
3098" Test for BufReadPre autocmd changing the current buffer
3099func Test_BufReadPre_changebuf()
3100 augroup TestAuCmd
3101 au!
Bram Moolenaare7cda972022-08-29 11:02:59 +01003102 autocmd BufReadPre Xchangebuf edit Xsomeotherfile
Bram Moolenaarb340bae2020-06-15 19:51:56 +02003103 augroup END
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01003104 call writefile([], 'Xchangebuf', 'D')
Bram Moolenaare7cda972022-08-29 11:02:59 +01003105 call assert_fails('new Xchangebuf', 'E201:')
Bram Moolenaarb340bae2020-06-15 19:51:56 +02003106 call assert_equal('Xsomeotherfile', @%)
3107 call assert_equal(1, &readonly)
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01003108
Bram Moolenaarb340bae2020-06-15 19:51:56 +02003109 augroup TestAuCmd
3110 au!
3111 augroup END
3112 close!
3113endfunc
3114
3115" Test for BufWipeouti autocmd changing the current buffer when reading a file
3116" in an empty buffer with 'f' flag in 'cpo'
3117func Test_BufDelete_changebuf()
3118 new
3119 augroup TestAuCmd
3120 au!
3121 autocmd BufWipeout * let bufnr = bufadd('somefile') | exe "b " .. bufnr
3122 augroup END
3123 let save_cpo = &cpo
3124 set cpo+=f
Bram Moolenaarb18b4962022-09-02 21:55:50 +01003125 call assert_fails('r Xchangebuf', ['E812:', 'E484:'])
Bram Moolenaarb340bae2020-06-15 19:51:56 +02003126 call assert_equal('somefile', @%)
3127 let &cpo = save_cpo
3128 augroup TestAuCmd
3129 au!
3130 augroup END
3131 close!
3132endfunc
3133
Bram Moolenaar0fe937f2020-06-16 22:42:04 +02003134" Test for the temporary internal window used to execute autocmds
3135func Test_autocmd_window()
3136 %bw!
3137 edit one.txt
3138 tabnew two.txt
Bram Moolenaar41cd8032021-03-13 15:47:56 +01003139 vnew three.txt
3140 tabnew four.txt
3141 tabprevious
Bram Moolenaar0fe937f2020-06-16 22:42:04 +02003142 let g:blist = []
Bram Moolenaar832adf92020-06-25 19:01:36 +02003143 augroup aucmd_win_test1
Bram Moolenaar0fe937f2020-06-16 22:42:04 +02003144 au!
3145 au BufEnter * call add(g:blist, [expand('<afile>'),
3146 \ win_gettype(bufwinnr(expand('<afile>')))])
3147 augroup END
3148
3149 doautoall BufEnter
Bram Moolenaar41cd8032021-03-13 15:47:56 +01003150 call assert_equal([
3151 \ ['one.txt', 'autocmd'],
3152 \ ['two.txt', ''],
3153 \ ['four.txt', 'autocmd'],
3154 \ ['three.txt', ''],
3155 \ ], g:blist)
Bram Moolenaar0fe937f2020-06-16 22:42:04 +02003156
Bram Moolenaar832adf92020-06-25 19:01:36 +02003157 augroup aucmd_win_test1
Bram Moolenaar0fe937f2020-06-16 22:42:04 +02003158 au!
3159 augroup END
Bram Moolenaar832adf92020-06-25 19:01:36 +02003160 augroup! aucmd_win_test1
3161 %bw!
3162endfunc
3163
3164" Test for trying to close the temporary window used for executing an autocmd
3165func Test_close_autocmd_window()
3166 %bw!
3167 edit one.txt
3168 tabnew two.txt
3169 augroup aucmd_win_test2
3170 au!
3171 au BufEnter * if expand('<afile>') == 'one.txt' | 1close | endif
3172 augroup END
3173
3174 call assert_fails('doautoall BufEnter', 'E813:')
3175
3176 augroup aucmd_win_test2
3177 au!
3178 augroup END
3179 augroup! aucmd_win_test2
Bram Moolenaarcf844172020-06-26 19:44:06 +02003180 %bwipe!
3181endfunc
3182
3183" Test for trying to close the tab that has the temporary window for exeucing
3184" an autocmd.
3185func Test_close_autocmd_tab()
3186 edit one.txt
3187 tabnew two.txt
3188 augroup aucmd_win_test
3189 au!
3190 au BufEnter * if expand('<afile>') == 'one.txt' | tabfirst | tabonly | endif
3191 augroup END
3192
3193 call assert_fails('doautoall BufEnter', 'E813:')
3194
3195 tabonly
3196 augroup aucmd_win_test
3197 au!
3198 augroup END
3199 augroup! aucmd_win_test
3200 %bwipe!
Bram Moolenaar0fe937f2020-06-16 22:42:04 +02003201endfunc
3202
Bram Moolenaarcb1956d2022-01-07 15:45:18 +00003203func Test_Visual_doautoall_redraw()
3204 call setline(1, ['a', 'b'])
3205 new
3206 wincmd p
3207 call feedkeys("G\<C-V>", 'txn')
3208 autocmd User Explode ++once redraw
3209 doautoall User Explode
3210 %bwipe!
3211endfunc
3212
Bram Moolenaar6bcb8772021-02-03 21:23:29 +01003213" This was using freed memory.
3214func Test_BufNew_arglocal()
3215 arglocal
3216 au BufNew * arglocal
3217 call assert_fails('drop xx', 'E1156:')
3218
3219 au! BufNew
3220endfunc
3221
Bram Moolenaar8ab37572021-02-03 21:56:59 +01003222func Test_autocmd_closes_window()
3223 au BufNew,BufWinLeave * e %e
3224 file yyy
3225 au BufNew,BufWinLeave * ball
Bram Moolenaaraad5f9d2021-02-06 17:30:31 +01003226 n xxx
Bram Moolenaar8ab37572021-02-03 21:56:59 +01003227
Bram Moolenaaraad5f9d2021-02-06 17:30:31 +01003228 %bwipe
Bram Moolenaar8ab37572021-02-03 21:56:59 +01003229 au! BufNew
3230 au! BufWinLeave
3231endfunc
3232
Bram Moolenaar92bb83e2021-02-03 23:04:46 +01003233func Test_autocmd_quit_psearch()
3234 sn aa bb
3235 augroup aucmd_win_test
3236 au!
3237 au BufEnter,BufLeave,BufNew,WinEnter,WinLeave,WinNew * if winnr('$') > 1 | q | endif
3238 augroup END
3239 ps /
3240
3241 augroup aucmd_win_test
3242 au!
3243 augroup END
zeertzjq7851c692022-04-21 11:14:01 +01003244 new
3245 pclose
Bram Moolenaar92bb83e2021-02-03 23:04:46 +01003246endfunc
3247
Bram Moolenaaraad5f9d2021-02-06 17:30:31 +01003248" Fuzzer found some strange combination that caused a crash.
3249func Test_autocmd_normal_mess()
Bram Moolenaardd07c022021-02-07 13:32:46 +01003250 " For unknown reason this hangs on MS-Windows
3251 CheckNotMSWindows
3252
Bram Moolenaaraad5f9d2021-02-06 17:30:31 +01003253 augroup aucmd_normal_test
3254 au BufLeave,BufWinLeave,BufHidden,BufUnload,BufDelete,BufWipeout * norm 7q/qc
3255 augroup END
Bram Moolenaar983d83f2021-02-07 12:12:43 +01003256 call assert_fails('o4', 'E1159')
Bram Moolenaaraad5f9d2021-02-06 17:30:31 +01003257 silent! H
Bram Moolenaar983d83f2021-02-07 12:12:43 +01003258 call assert_fails('e xx', 'E1159')
Bram Moolenaaraad5f9d2021-02-06 17:30:31 +01003259 normal G
3260
3261 augroup aucmd_normal_test
3262 au!
3263 augroup END
3264endfunc
3265
Bram Moolenaar8c6951f2021-02-06 18:08:45 +01003266func Test_autocmd_closing_cmdwin()
Bram Moolenaardd07c022021-02-07 13:32:46 +01003267 " For unknown reason this hangs on MS-Windows
3268 CheckNotMSWindows
3269
Bram Moolenaar8c6951f2021-02-06 18:08:45 +01003270 au BufWinLeave * nested q
3271 call assert_fails("norm 7q?\n", 'E855:')
3272
3273 au! BufWinLeave
3274 new
3275 only
3276endfunc
3277
Bram Moolenaar2c7080b2021-02-06 19:19:42 +01003278func Test_autocmd_vimgrep()
3279 augroup aucmd_vimgrep
Charlie Grovesfef44852022-04-19 16:24:12 +01003280 au QuickfixCmdPre,BufNew,BufReadCmd * sb
zeertzjq7851c692022-04-21 11:14:01 +01003281 au QuickfixCmdPre,BufNew,BufReadCmd * q9
Bram Moolenaar2c7080b2021-02-06 19:19:42 +01003282 augroup END
Bram Moolenaardd07c022021-02-07 13:32:46 +01003283 call assert_fails('lv ?a? foo', 'E926:')
Bram Moolenaar2c7080b2021-02-06 19:19:42 +01003284
3285 augroup aucmd_vimgrep
3286 au!
3287 augroup END
3288endfunc
3289
Bram Moolenaar73b8b0a2021-08-01 14:52:32 +02003290func Test_autocmd_with_block()
3291 augroup block_testing
3292 au BufReadPost *.xml {
3293 setlocal matchpairs+=<:>
3294 /<start
3295 }
Bram Moolenaar63b91732021-08-05 20:40:03 +02003296 au CursorHold * {
3297 autocmd BufReadPre * ++once echo 'one' | echo 'two'
3298 g:gotSafeState = 77
3299 }
Bram Moolenaar73b8b0a2021-08-01 14:52:32 +02003300 augroup END
3301
3302 let expected = "\n--- Autocommands ---\nblock_testing BufRead\n *.xml {^@ setlocal matchpairs+=<:>^@ /<start^@ }"
3303 call assert_equal(expected, execute('au BufReadPost *.xml'))
3304
Bram Moolenaar63b91732021-08-05 20:40:03 +02003305 doautocmd CursorHold
3306 call assert_equal(77, g:gotSafeState)
3307 unlet g:gotSafeState
3308
Bram Moolenaar73b8b0a2021-08-01 14:52:32 +02003309 augroup block_testing
3310 au!
Bram Moolenaar75ebd2a2022-06-03 17:39:46 +01003311 autocmd CursorHold * {
3312 if true
3313 # comment
3314 && true
3315
3316 && true
3317 g:done = 'yes'
3318 endif
3319 }
3320 augroup END
3321 doautocmd CursorHold
3322 call assert_equal('yes', g:done)
3323
3324 unlet g:done
3325 augroup block_testing
3326 au!
Bram Moolenaar73b8b0a2021-08-01 14:52:32 +02003327 augroup END
3328endfunc
3329
Christian Brabandtdb3b4462021-10-16 11:58:55 +01003330" Test TextChangedI and TextChanged
3331func Test_Changed_ChangedI()
3332 new
3333 call test_override("char_avail", 1)
3334 let [g:autocmd_i, g:autocmd_n] = ['','']
3335
3336 func! TextChangedAutocmdI(char)
3337 let g:autocmd_{tolower(a:char)} = a:char .. b:changedtick
3338 endfunc
3339
3340 augroup Test_TextChanged
3341 au!
3342 au TextChanged <buffer> :call TextChangedAutocmdI('N')
3343 au TextChangedI <buffer> :call TextChangedAutocmdI('I')
3344 augroup END
3345
3346 call feedkeys("ifoo\<esc>", 'tnix')
3347 " TODO: Test test does not seem to trigger TextChanged autocommand, this
3348 " requires running Vim in a terminal window.
3349 " call assert_equal('N3', g:autocmd_n)
3350 call assert_equal('I3', g:autocmd_i)
3351
3352 call feedkeys("yyp", 'tnix')
3353 " TODO: Test test does not seem to trigger TextChanged autocommand.
3354 " call assert_equal('N4', g:autocmd_n)
3355 call assert_equal('I3', g:autocmd_i)
3356
3357 " CleanUp
3358 call test_override("char_avail", 0)
3359 au! TextChanged <buffer>
3360 au! TextChangedI <buffer>
3361 augroup! Test_TextChanged
3362 delfu TextChangedAutocmdI
3363 unlet! g:autocmd_i g:autocmd_n
3364
3365 bw!
3366endfunc
Bram Moolenaar2c7080b2021-02-06 19:19:42 +01003367
Bram Moolenaar6f2465d2022-03-22 18:13:01 +00003368func Test_closing_autocmd_window()
3369 let lines =<< trim END
3370 edit Xa.txt
3371 tabnew Xb.txt
3372 autocmd BufEnter Xa.txt unhide 1
3373 doautoall BufEnter
3374 END
3375 call v9.CheckScriptFailure(lines, 'E814:')
3376 au! BufEnter
3377 only!
3378 bwipe Xa.txt
3379 bwipe Xb.txt
3380endfunc
3381
Bram Moolenaar347538f2022-03-26 16:28:06 +00003382func Test_bufwipeout_changes_window()
3383 " This should not crash, but we don't have any expectations about what
3384 " happens, changing window in BufWipeout has unpredictable results.
3385 tabedit
3386 let g:window_id = win_getid()
3387 topleft new
3388 setlocal bufhidden=wipe
3389 autocmd BufWipeout <buffer> call win_gotoid(g:window_id)
3390 tabprevious
3391 +tabclose
3392
3393 unlet g:window_id
3394 au! BufWipeout
3395 %bwipe!
3396endfunc
3397
zeertzjq021996f2022-04-10 11:44:04 +01003398func Test_v_event_readonly()
3399 autocmd CompleteChanged * let v:event.width = 0
3400 call assert_fails("normal! i\<C-X>\<C-V>", 'E46:')
3401 au! CompleteChanged
3402
3403 autocmd DirChangedPre * let v:event.directory = ''
3404 call assert_fails('cd .', 'E46:')
3405 au! DirChangedPre
3406
3407 autocmd ModeChanged * let v:event.new_mode = ''
3408 call assert_fails('normal! cc', 'E46:')
3409 au! ModeChanged
3410
3411 autocmd TextYankPost * let v:event.operator = ''
3412 call assert_fails('normal! yy', 'E46:')
3413 au! TextYankPost
3414endfunc
3415
zeertzjqc9e8fd62022-07-26 18:12:38 +01003416" Test for ModeChanged pattern
3417func Test_mode_changes()
3418 let g:index = 0
3419 let g:mode_seq = ['n', 'i', 'n', 'v', 'V', 'i', 'ix', 'i', 'ic', 'i', 'n', 'no', 'n', 'V', 'v', 's', 'n']
3420 func! TestMode()
3421 call assert_equal(g:mode_seq[g:index], get(v:event, "old_mode"))
3422 call assert_equal(g:mode_seq[g:index + 1], get(v:event, "new_mode"))
3423 call assert_equal(mode(1), get(v:event, "new_mode"))
3424 let g:index += 1
3425 endfunc
3426
3427 au ModeChanged * :call TestMode()
3428 let g:n_to_any = 0
3429 au ModeChanged n:* let g:n_to_any += 1
3430 call feedkeys("i\<esc>vVca\<CR>\<C-X>\<C-L>\<esc>ggdG", 'tnix')
3431
3432 let g:V_to_v = 0
3433 au ModeChanged V:v let g:V_to_v += 1
3434 call feedkeys("Vv\<C-G>\<esc>", 'tnix')
3435 call assert_equal(len(filter(g:mode_seq[1:], {idx, val -> val == 'n'})), g:n_to_any)
3436 call assert_equal(1, g:V_to_v)
3437 call assert_equal(len(g:mode_seq) - 1, g:index)
3438
3439 let g:n_to_i = 0
3440 au ModeChanged n:i let g:n_to_i += 1
3441 let g:n_to_niI = 0
3442 au ModeChanged i:niI let g:n_to_niI += 1
3443 let g:niI_to_i = 0
3444 au ModeChanged niI:i let g:niI_to_i += 1
3445 let g:nany_to_i = 0
3446 au ModeChanged n*:i let g:nany_to_i += 1
3447 let g:i_to_n = 0
3448 au ModeChanged i:n let g:i_to_n += 1
3449 let g:nori_to_any = 0
3450 au ModeChanged [ni]:* let g:nori_to_any += 1
3451 let g:i_to_any = 0
3452 au ModeChanged i:* let g:i_to_any += 1
3453 let g:index = 0
3454 let g:mode_seq = ['n', 'i', 'niI', 'i', 'n']
3455 call feedkeys("a\<C-O>l\<esc>", 'tnix')
3456 call assert_equal(len(g:mode_seq) - 1, g:index)
3457 call assert_equal(1, g:n_to_i)
3458 call assert_equal(1, g:n_to_niI)
3459 call assert_equal(1, g:niI_to_i)
3460 call assert_equal(2, g:nany_to_i)
3461 call assert_equal(1, g:i_to_n)
3462 call assert_equal(2, g:i_to_any)
3463 call assert_equal(3, g:nori_to_any)
3464
3465 if has('terminal')
3466 let g:mode_seq += ['c', 'n', 't', 'nt', 'c', 'nt', 'n']
3467 call feedkeys(":term\<CR>\<C-W>N:bd!\<CR>", 'tnix')
3468 call assert_equal(len(g:mode_seq) - 1, g:index)
3469 call assert_equal(1, g:n_to_i)
3470 call assert_equal(1, g:n_to_niI)
3471 call assert_equal(1, g:niI_to_i)
3472 call assert_equal(2, g:nany_to_i)
3473 call assert_equal(1, g:i_to_n)
3474 call assert_equal(2, g:i_to_any)
3475 call assert_equal(5, g:nori_to_any)
3476 endif
3477
zeertzjqd1955982022-10-05 11:24:46 +01003478 let g:n_to_c = 0
3479 au ModeChanged n:c let g:n_to_c += 1
3480 let g:c_to_n = 0
3481 au ModeChanged c:n let g:c_to_n += 1
3482 let g:mode_seq += ['c', 'n', 'c', 'n']
3483 call feedkeys("q:\<C-C>\<Esc>", 'tnix')
3484 call assert_equal(len(g:mode_seq) - 1, g:index)
3485 call assert_equal(2, g:n_to_c)
3486 call assert_equal(2, g:c_to_n)
3487 unlet g:n_to_c
3488 unlet g:c_to_n
zeertzjqc9e8fd62022-07-26 18:12:38 +01003489
Bram Moolenaar61c4b042022-10-18 15:10:11 +01003490 let g:n_to_v = 0
3491 au ModeChanged n:v let g:n_to_v += 1
3492 let g:v_to_n = 0
3493 au ModeChanged v:n let g:v_to_n += 1
3494 let g:mode_seq += ['v', 'n']
3495 call feedkeys("v\<C-C>", 'tnix')
3496 call assert_equal(len(g:mode_seq) - 1, g:index)
3497 call assert_equal(1, g:n_to_v)
3498 call assert_equal(1, g:v_to_n)
3499 unlet g:n_to_v
3500 unlet g:v_to_n
3501
zeertzjqc9e8fd62022-07-26 18:12:38 +01003502 au! ModeChanged
3503 delfunc TestMode
3504 unlet! g:mode_seq
3505 unlet! g:index
3506 unlet! g:n_to_any
3507 unlet! g:V_to_v
3508 unlet! g:n_to_i
3509 unlet! g:n_to_niI
3510 unlet! g:niI_to_i
3511 unlet! g:nany_to_i
3512 unlet! g:i_to_n
3513 unlet! g:nori_to_any
3514 unlet! g:i_to_any
3515endfunc
3516
3517func Test_recursive_ModeChanged()
3518 au! ModeChanged * norm 0u
3519 sil! norm 
3520 au! ModeChanged
3521endfunc
3522
3523func Test_ModeChanged_starts_visual()
3524 " This was triggering ModeChanged before setting VIsual, causing a crash.
3525 au! ModeChanged * norm 0u
3526 sil! norm 
3527
3528 au! ModeChanged
3529endfunc
Bram Moolenaar347538f2022-03-26 16:28:06 +00003530
Charlie Grovesfef44852022-04-19 16:24:12 +01003531func Test_noname_autocmd()
3532 augroup test_noname_autocmd_group
3533 autocmd!
3534 autocmd BufEnter * call add(s:li, ["BufEnter", expand("<afile>")])
3535 autocmd BufDelete * call add(s:li, ["BufDelete", expand("<afile>")])
3536 autocmd BufLeave * call add(s:li, ["BufLeave", expand("<afile>")])
3537 autocmd BufUnload * call add(s:li, ["BufUnload", expand("<afile>")])
3538 autocmd BufWipeout * call add(s:li, ["BufWipeout", expand("<afile>")])
3539 augroup END
3540
3541 let s:li = []
3542 edit foo
3543 call assert_equal([['BufUnload', ''], ['BufDelete', ''], ['BufWipeout', ''], ['BufEnter', 'foo']], s:li)
3544
3545 au! test_noname_autocmd_group
3546 augroup! test_noname_autocmd_group
3547endfunc
3548
Yegappan Lakshmanan1755a912022-05-19 10:31:47 +01003549" Test for the autocmd_get() function
3550func Test_autocmd_get()
3551 augroup TestAutoCmdFns
3552 au!
3553 autocmd BufAdd *.vim echo "bufadd-vim"
3554 autocmd BufAdd *.py echo "bufadd-py"
3555 autocmd BufHidden *.vim echo "bufhidden"
3556 augroup END
3557 augroup TestAutoCmdFns2
3558 autocmd BufAdd *.vim echo "bufadd-vim-2"
3559 autocmd BufRead *.a1b2c3 echo "bufadd-vim-2"
3560 augroup END
3561
3562 let l = autocmd_get()
3563 call assert_true(l->len() > 0)
3564
3565 " Test for getting all the autocmds in a group
3566 let expected = [
3567 \ #{cmd: 'echo "bufadd-vim"', group: 'TestAutoCmdFns',
3568 \ pattern: '*.vim', nested: v:false, once: v:false,
3569 \ event: 'BufAdd'},
3570 \ #{cmd: 'echo "bufadd-py"', group: 'TestAutoCmdFns',
3571 \ pattern: '*.py', nested: v:false, once: v:false,
3572 \ event: 'BufAdd'},
3573 \ #{cmd: 'echo "bufhidden"', group: 'TestAutoCmdFns',
3574 \ pattern: '*.vim', nested: v:false,
3575 \ once: v:false, event: 'BufHidden'}]
3576 call assert_equal(expected, autocmd_get(#{group: 'TestAutoCmdFns'}))
3577
3578 " Test for getting autocmds for all the patterns in a group
3579 call assert_equal(expected, autocmd_get(#{group: 'TestAutoCmdFns',
3580 \ event: '*'}))
3581
3582 " Test for getting autocmds for an event in a group
3583 let expected = [
3584 \ #{cmd: 'echo "bufadd-vim"', group: 'TestAutoCmdFns',
3585 \ pattern: '*.vim', nested: v:false, once: v:false,
3586 \ event: 'BufAdd'},
3587 \ #{cmd: 'echo "bufadd-py"', group: 'TestAutoCmdFns',
3588 \ pattern: '*.py', nested: v:false, once: v:false,
3589 \ event: 'BufAdd'}]
3590 call assert_equal(expected, autocmd_get(#{group: 'TestAutoCmdFns',
3591 \ event: 'BufAdd'}))
3592
3593 " Test for getting the autocmds for all the events in a group for particular
3594 " pattern
3595 call assert_equal([{'cmd': 'echo "bufadd-py"', 'group': 'TestAutoCmdFns',
3596 \ 'pattern': '*.py', 'nested': v:false, 'once': v:false,
3597 \ 'event': 'BufAdd'}],
3598 \ autocmd_get(#{group: 'TestAutoCmdFns', event: '*', pattern: '*.py'}))
3599
3600 " Test for getting the autocmds for an events in a group for particular
3601 " pattern
3602 let l = autocmd_get(#{group: 'TestAutoCmdFns', event: 'BufAdd',
3603 \ pattern: '*.vim'})
3604 call assert_equal([
3605 \ #{cmd: 'echo "bufadd-vim"', group: 'TestAutoCmdFns',
3606 \ pattern: '*.vim', nested: v:false, once: v:false,
3607 \ event: 'BufAdd'}], l)
3608
3609 " Test for getting the autocmds for a pattern in a group
3610 let l = autocmd_get(#{group: 'TestAutoCmdFns', pattern: '*.vim'})
3611 call assert_equal([
3612 \ #{cmd: 'echo "bufadd-vim"', group: 'TestAutoCmdFns',
3613 \ pattern: '*.vim', nested: v:false, once: v:false,
3614 \ event: 'BufAdd'},
3615 \ #{cmd: 'echo "bufhidden"', group: 'TestAutoCmdFns',
3616 \ pattern: '*.vim', nested: v:false,
3617 \ once: v:false, event: 'BufHidden'}], l)
3618
3619 " Test for getting the autocmds for a pattern in all the groups
3620 let l = autocmd_get(#{pattern: '*.a1b2c3'})
3621 call assert_equal([{'cmd': 'echo "bufadd-vim-2"', 'group': 'TestAutoCmdFns2',
3622 \ 'pattern': '*.a1b2c3', 'nested': v:false, 'once': v:false,
3623 \ 'event': 'BufRead'}], l)
3624
3625 " Test for getting autocmds for a pattern without any autocmds
3626 call assert_equal([], autocmd_get(#{group: 'TestAutoCmdFns',
3627 \ pattern: '*.abc'}))
3628 call assert_equal([], autocmd_get(#{group: 'TestAutoCmdFns',
3629 \ event: 'BufAdd', pattern: '*.abc'}))
3630 call assert_equal([], autocmd_get(#{group: 'TestAutoCmdFns',
3631 \ event: 'BufWipeout'}))
3632 call assert_fails("call autocmd_get(#{group: 'abc', event: 'BufAdd'})",
3633 \ 'E367:')
3634 let cmd = "echo autocmd_get(#{group: 'TestAutoCmdFns', event: 'abc'})"
3635 call assert_fails(cmd, 'E216:')
3636 call assert_fails("call autocmd_get(#{group: 'abc'})", 'E367:')
3637 call assert_fails("echo autocmd_get(#{event: 'abc'})", 'E216:')
3638
3639 augroup TestAutoCmdFns
3640 au!
3641 augroup END
3642 call assert_equal([], autocmd_get(#{group: 'TestAutoCmdFns'}))
3643
3644 " Test for nested and once autocmds
3645 augroup TestAutoCmdFns
3646 au!
3647 autocmd VimSuspend * ++nested echo "suspend"
3648 autocmd VimResume * ++once echo "resume"
3649 augroup END
3650
3651 let expected = [
3652 \ {'cmd': 'echo "suspend"', 'group': 'TestAutoCmdFns', 'pattern': '*',
3653 \ 'nested': v:true, 'once': v:false, 'event': 'VimSuspend'},
3654 \ {'cmd': 'echo "resume"', 'group': 'TestAutoCmdFns', 'pattern': '*',
3655 \ 'nested': v:false, 'once': v:true, 'event': 'VimResume'}]
3656 call assert_equal(expected, autocmd_get(#{group: 'TestAutoCmdFns'}))
3657
3658 " Test for buffer-local autocmd
3659 augroup TestAutoCmdFns
3660 au!
3661 autocmd TextYankPost <buffer> echo "textyankpost"
3662 augroup END
3663
3664 let expected = [
3665 \ {'cmd': 'echo "textyankpost"', 'group': 'TestAutoCmdFns',
3666 \ 'pattern': '<buffer=' .. bufnr() .. '>', 'nested': v:false,
3667 \ 'once': v:false, 'bufnr': bufnr(), 'event': 'TextYankPost'}]
3668 call assert_equal(expected, autocmd_get(#{group: 'TestAutoCmdFns'}))
3669
3670 augroup TestAutoCmdFns
3671 au!
3672 augroup END
3673 augroup! TestAutoCmdFns
3674 augroup TestAutoCmdFns2
3675 au!
3676 augroup END
3677 augroup! TestAutoCmdFns2
3678
3679 call assert_fails("echo autocmd_get(#{group: []})", 'E730:')
3680 call assert_fails("echo autocmd_get(#{event: {}})", 'E731:')
3681 call assert_fails("echo autocmd_get([])", 'E1206:')
3682endfunc
3683
3684" Test for the autocmd_add() function
3685func Test_autocmd_add()
3686 " Define a single autocmd in a group
3687 call autocmd_add([#{group: 'TestAcSet', event: 'BufAdd', pattern: '*.sh',
3688 \ cmd: 'echo "bufadd"', once: v:true, nested: v:true}])
3689 call assert_equal([#{cmd: 'echo "bufadd"', group: 'TestAcSet',
3690 \ pattern: '*.sh', nested: v:true, once: v:true,
3691 \ event: 'BufAdd'}], autocmd_get(#{group: 'TestAcSet'}))
3692
3693 " Define two autocmds in the same group
3694 call autocmd_delete([#{group: 'TestAcSet'}])
3695 call autocmd_add([#{group: 'TestAcSet', event: 'BufAdd', pattern: '*.sh',
3696 \ cmd: 'echo "bufadd"'},
3697 \ #{group: 'TestAcSet', event: 'BufEnter', pattern: '*.sh',
3698 \ cmd: 'echo "bufenter"'}])
3699 call assert_equal([
3700 \ #{cmd: 'echo "bufadd"', group: 'TestAcSet', pattern: '*.sh',
3701 \ nested: v:false, once: v:false, event: 'BufAdd'},
3702 \ #{cmd: 'echo "bufenter"', group: 'TestAcSet', pattern: '*.sh',
3703 \ nested: v:false, once: v:false, event: 'BufEnter'}],
3704 \ autocmd_get(#{group: 'TestAcSet'}))
3705
3706 " Define a buffer-local autocmd
3707 call autocmd_delete([#{group: 'TestAcSet'}])
3708 call autocmd_add([#{group: 'TestAcSet', event: 'CursorHold',
3709 \ bufnr: bufnr(), cmd: 'echo "cursorhold"'}])
3710 call assert_equal([
3711 \ #{cmd: 'echo "cursorhold"', group: 'TestAcSet',
3712 \ pattern: '<buffer=' .. bufnr() .. '>', nested: v:false,
3713 \ once: v:false, bufnr: bufnr(), event: 'CursorHold'}],
3714 \ autocmd_get(#{group: 'TestAcSet'}))
3715
3716 " Use an invalid buffer number
3717 call autocmd_delete([#{group: 'TestAcSet'}])
3718 call autocmd_add([#{group: 'TestAcSet', event: 'BufEnter',
3719 \ bufnr: -1, cmd: 'echo "bufenter"'}])
3720 let l = [#{group: 'TestAcSet', event: 'BufAdd', bufnr: 9999,
3721 \ cmd: 'echo "bufadd"'}]
3722 call assert_fails("echo autocmd_add(l)", 'E680:')
Yegappan Lakshmanan00e977c2022-06-01 12:31:53 +01003723 let l = [#{group: 'TestAcSet', event: 'BufAdd', bufnr: 9999,
3724 \ pattern: '*.py', cmd: 'echo "bufadd"'}]
3725 call assert_fails("echo autocmd_add(l)", 'E680:')
3726 let l = [#{group: 'TestAcSet', event: 'BufAdd', bufnr: 9999,
3727 \ pattern: ['*.py', '*.c'], cmd: 'echo "bufadd"'}]
3728 call assert_fails("echo autocmd_add(l)", 'E680:')
Yegappan Lakshmanan1755a912022-05-19 10:31:47 +01003729 let l = [#{group: 'TestAcSet', event: 'BufRead', bufnr: [],
3730 \ cmd: 'echo "bufread"'}]
3731 call assert_fails("echo autocmd_add(l)", 'E745:')
3732 call assert_equal([], autocmd_get(#{group: 'TestAcSet'}))
3733
3734 " Add two commands to the same group, event and pattern
3735 call autocmd_delete([#{group: 'TestAcSet'}])
3736 call autocmd_add([#{group: 'TestAcSet', event: 'BufUnload',
3737 \ pattern: 'abc', cmd: 'echo "cmd1"'}])
3738 call autocmd_add([#{group: 'TestAcSet', event: 'BufUnload',
3739 \ pattern: 'abc', cmd: 'echo "cmd2"'}])
3740 call assert_equal([
3741 \ #{cmd: 'echo "cmd1"', group: 'TestAcSet', pattern: 'abc',
3742 \ nested: v:false, once: v:false, event: 'BufUnload'},
3743 \ #{cmd: 'echo "cmd2"', group: 'TestAcSet', pattern: 'abc',
3744 \ nested: v:false, once: v:false, event: 'BufUnload'}],
3745 \ autocmd_get(#{group: 'TestAcSet'}))
3746
3747 " When adding a new autocmd, if the autocmd 'group' is not specified, then
3748 " the current autocmd group should be used.
3749 call autocmd_delete([#{group: 'TestAcSet'}])
3750 augroup TestAcSet
3751 call autocmd_add([#{event: 'BufHidden', pattern: 'abc', cmd: 'echo "abc"'}])
3752 augroup END
3753 call assert_equal([
3754 \ #{cmd: 'echo "abc"', group: 'TestAcSet', pattern: 'abc',
3755 \ nested: v:false, once: v:false, event: 'BufHidden'}],
3756 \ autocmd_get(#{group: 'TestAcSet'}))
3757
Yegappan Lakshmanan971f6822022-05-24 11:40:11 +01003758 " Test for replacing a cmd for an event in a group
3759 call autocmd_delete([#{group: 'TestAcSet'}])
3760 call autocmd_add([#{replace: v:true, group: 'TestAcSet', event: 'BufEnter',
3761 \ pattern: '*.py', cmd: 'echo "bufenter"'}])
3762 call autocmd_add([#{replace: v:true, group: 'TestAcSet', event: 'BufEnter',
3763 \ pattern: '*.py', cmd: 'echo "bufenter"'}])
3764 call assert_equal([
3765 \ #{cmd: 'echo "bufenter"', group: 'TestAcSet', pattern: '*.py',
3766 \ nested: v:false, once: v:false, event: 'BufEnter'}],
3767 \ autocmd_get(#{group: 'TestAcSet'}))
3768
3769 " Test for adding a command for an unsupported autocmd event
Yegappan Lakshmanan1755a912022-05-19 10:31:47 +01003770 let l = [#{group: 'TestAcSet', event: 'abc', pattern: '*.sh',
3771 \ cmd: 'echo "bufadd"'}]
3772 call assert_fails('call autocmd_add(l)', 'E216:')
3773
Yegappan Lakshmanane0ff3a72022-05-27 18:05:33 +01003774 " Test for using a list of events and patterns
3775 call autocmd_delete([#{group: 'TestAcSet'}])
3776 let l = [#{group: 'TestAcSet', event: ['BufEnter', 'BufLeave'],
3777 \ pattern: ['*.py', '*.sh'], cmd: 'echo "bufcmds"'}]
3778 call autocmd_add(l)
3779 call assert_equal([
3780 \ #{cmd: 'echo "bufcmds"', group: 'TestAcSet', pattern: '*.py',
3781 \ nested: v:false, once: v:false, event: 'BufEnter'},
3782 \ #{cmd: 'echo "bufcmds"', group: 'TestAcSet', pattern: '*.sh',
3783 \ nested: v:false, once: v:false, event: 'BufEnter'},
3784 \ #{cmd: 'echo "bufcmds"', group: 'TestAcSet', pattern: '*.py',
3785 \ nested: v:false, once: v:false, event: 'BufLeave'},
3786 \ #{cmd: 'echo "bufcmds"', group: 'TestAcSet', pattern: '*.sh',
3787 \ nested: v:false, once: v:false, event: 'BufLeave'}],
3788 \ autocmd_get(#{group: 'TestAcSet'}))
3789
3790 " Test for invalid values for 'event' item
3791 call autocmd_delete([#{group: 'TestAcSet'}])
3792 let l = [#{group: 'TestAcSet', event: test_null_string(),
3793 \ pattern: "*.py", cmd: 'echo "bufcmds"'}]
3794 call assert_fails('call autocmd_add(l)', 'E928:')
3795 let l = [#{group: 'TestAcSet', event: test_null_list(),
3796 \ pattern: "*.py", cmd: 'echo "bufcmds"'}]
3797 call assert_fails('call autocmd_add(l)', 'E714:')
3798 let l = [#{group: 'TestAcSet', event: {},
3799 \ pattern: "*.py", cmd: 'echo "bufcmds"'}]
3800 call assert_fails('call autocmd_add(l)', 'E777:')
3801 let l = [#{group: 'TestAcSet', event: [{}],
3802 \ pattern: "*.py", cmd: 'echo "bufcmds"'}]
3803 call assert_fails('call autocmd_add(l)', 'E928:')
3804 let l = [#{group: 'TestAcSet', event: [test_null_string()],
3805 \ pattern: "*.py", cmd: 'echo "bufcmds"'}]
3806 call assert_fails('call autocmd_add(l)', 'E928:')
3807 let l = [#{group: 'TestAcSet', event: 'BufEnter,BufLeave',
3808 \ pattern: '*.py', cmd: 'echo "bufcmds"'}]
3809 call assert_fails('call autocmd_add(l)', 'E216:')
3810 let l = [#{group: 'TestAcSet', event: [],
3811 \ pattern: "*.py", cmd: 'echo "bufcmds"'}]
3812 call autocmd_add(l)
3813 let l = [#{group: 'TestAcSet', event: [""],
3814 \ pattern: "*.py", cmd: 'echo "bufcmds"'}]
3815 call assert_fails('call autocmd_add(l)', 'E216:')
3816 let l = [#{group: 'TestAcSet', event: "",
3817 \ pattern: "*.py", cmd: 'echo "bufcmds"'}]
3818 call autocmd_add(l)
3819 call assert_equal([], autocmd_get(#{group: 'TestAcSet'}))
3820
3821 " Test for invalid values for 'pattern' item
3822 let l = [#{group: 'TestAcSet', event: "BufEnter",
3823 \ pattern: test_null_string(), cmd: 'echo "bufcmds"'}]
Yegappan Lakshmanan00e977c2022-06-01 12:31:53 +01003824 call assert_fails('call autocmd_add(l)', 'E928:')
Yegappan Lakshmanane0ff3a72022-05-27 18:05:33 +01003825 let l = [#{group: 'TestAcSet', event: "BufEnter",
3826 \ pattern: test_null_list(), cmd: 'echo "bufcmds"'}]
3827 call assert_fails('call autocmd_add(l)', 'E714:')
3828 let l = [#{group: 'TestAcSet', event: "BufEnter",
3829 \ pattern: {}, cmd: 'echo "bufcmds"'}]
3830 call assert_fails('call autocmd_add(l)', 'E777:')
3831 let l = [#{group: 'TestAcSet', event: "BufEnter",
3832 \ pattern: [{}], cmd: 'echo "bufcmds"'}]
3833 call assert_fails('call autocmd_add(l)', 'E928:')
3834 let l = [#{group: 'TestAcSet', event: "BufEnter",
3835 \ pattern: [test_null_string()], cmd: 'echo "bufcmds"'}]
3836 call assert_fails('call autocmd_add(l)', 'E928:')
3837 let l = [#{group: 'TestAcSet', event: "BufEnter",
3838 \ pattern: [], cmd: 'echo "bufcmds"'}]
3839 call autocmd_add(l)
3840 let l = [#{group: 'TestAcSet', event: "BufEnter",
3841 \ pattern: [""], cmd: 'echo "bufcmds"'}]
3842 call autocmd_add(l)
3843 let l = [#{group: 'TestAcSet', event: "BufEnter",
3844 \ pattern: "", cmd: 'echo "bufcmds"'}]
3845 call autocmd_add(l)
3846 call assert_equal([], autocmd_get(#{group: 'TestAcSet'}))
3847
3848 let l = [#{group: 'TestAcSet', event: 'BufEnter,abc,BufLeave',
3849 \ pattern: '*.py', cmd: 'echo "bufcmds"'}]
3850 call assert_fails('call autocmd_add(l)', 'E216:')
3851
Yegappan Lakshmanan1755a912022-05-19 10:31:47 +01003852 call assert_fails("call autocmd_add({})", 'E1211:')
3853 call assert_equal(v:false, autocmd_add(test_null_list()))
3854 call assert_true(autocmd_add([[]]))
3855 call assert_true(autocmd_add([test_null_dict()]))
3856
3857 augroup TestAcSet
3858 au!
3859 augroup END
3860
3861 call autocmd_add([#{group: 'TestAcSet'}])
3862 call autocmd_add([#{group: 'TestAcSet', event: 'BufAdd'}])
3863 call autocmd_add([#{group: 'TestAcSet', pat: '*.sh'}])
3864 call autocmd_add([#{group: 'TestAcSet', cmd: 'echo "a"'}])
3865 call autocmd_add([#{group: 'TestAcSet', event: 'BufAdd', pat: '*.sh'}])
3866 call autocmd_add([#{group: 'TestAcSet', event: 'BufAdd', cmd: 'echo "a"'}])
3867 call autocmd_add([#{group: 'TestAcSet', pat: '*.sh', cmd: 'echo "a"'}])
3868 call assert_equal([], autocmd_get(#{group: 'TestAcSet'}))
3869
3870 augroup! TestAcSet
3871endfunc
3872
3873" Test for deleting autocmd events and groups
3874func Test_autocmd_delete()
3875 " Delete an event in an autocmd group
3876 augroup TestAcSet
3877 au!
3878 au BufAdd *.sh echo "bufadd"
3879 au BufEnter *.sh echo "bufenter"
3880 augroup END
3881 call autocmd_delete([#{group: 'TestAcSet', event: 'BufAdd'}])
3882 call assert_equal([#{cmd: 'echo "bufenter"', group: 'TestAcSet',
3883 \ pattern: '*.sh', nested: v:false, once: v:false,
3884 \ event: 'BufEnter'}], autocmd_get(#{group: 'TestAcSet'}))
3885
3886 " Delete all the events in an autocmd group
3887 augroup TestAcSet
3888 au BufAdd *.sh echo "bufadd"
3889 augroup END
3890 call autocmd_delete([#{group: 'TestAcSet', event: '*'}])
3891 call assert_equal([], autocmd_get(#{group: 'TestAcSet'}))
3892
3893 " Delete a non-existing autocmd group
3894 call assert_fails("call autocmd_delete([#{group: 'abc'}])", 'E367:')
3895 " Delete a non-existing autocmd event
3896 let l = [#{group: 'TestAcSet', event: 'abc'}]
3897 call assert_fails("call autocmd_delete(l)", 'E216:')
3898 " Delete a non-existing autocmd pattern
3899 let l = [#{group: 'TestAcSet', event: 'BufAdd', pat: 'abc'}]
3900 call assert_true(autocmd_delete(l))
Yegappan Lakshmanan00e977c2022-06-01 12:31:53 +01003901 " Delete an autocmd for a non-existing buffer
3902 let l = [#{event: '*', bufnr: 9999, cmd: 'echo "x"'}]
3903 call assert_fails('call autocmd_delete(l)', 'E680:')
Yegappan Lakshmanan1755a912022-05-19 10:31:47 +01003904
3905 " Delete an autocmd group
3906 augroup TestAcSet
3907 au!
3908 au BufAdd *.sh echo "bufadd"
3909 au BufEnter *.sh echo "bufenter"
3910 augroup END
3911 call autocmd_delete([#{group: 'TestAcSet'}])
3912 call assert_fails("call autocmd_get(#{group: 'TestAcSet'})", 'E367:')
3913
3914 call assert_true(autocmd_delete([[]]))
3915 call assert_true(autocmd_delete([test_null_dict()]))
3916endfunc
3917
Bram Moolenaar8f3c3c62022-10-18 17:05:54 +01003918func Test_autocmd_split_dummy()
3919 " Autocommand trying to split a window containing a dummy buffer.
3920 auto BufReadPre * exe "sbuf " .. expand("<abuf>")
3921 " Avoid the "W11" prompt
3922 au FileChangedShell * let v:fcs_choice = 'reload'
3923 func Xautocmd_changelist()
3924 cal writefile(['Xtestfile2:4:4'], 'Xerr')
3925 edit Xerr
3926 lex 'Xtestfile2:4:4'
3927 endfunc
3928 call Xautocmd_changelist()
Bram Moolenaar53c5c9f2022-10-18 17:25:03 +01003929 " Should get E86, but it doesn't always happen (timing?)
3930 silent! call Xautocmd_changelist()
Bram Moolenaar8f3c3c62022-10-18 17:05:54 +01003931
3932 au! BufReadPre
3933 au! FileChangedShell
3934 delfunc Xautocmd_changelist
3935 bwipe! Xerr
3936 call delete('Xerr')
3937endfunc
3938
Bram Moolenaarbc2b71d2020-02-17 21:33:30 +01003939" vim: shiftwidth=2 sts=2 expandtab