blob: 04f3e1431c13229a1bfe697739b2aa8d59958103 [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!')
65 call assert_equal(1, g:triggered)
Bram Moolenaar26d98212019-01-27 22:32:55 +010066 unlet g:triggered
67 au! CursorHoldI
68 set updatetime&
69 endfunc
70
71 func Test_cursorhold_insert_with_timer_interrupt()
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +020072 CheckFeature job
Bram Moolenaar26d98212019-01-27 22:32:55 +010073 " Need to move the cursor.
74 call feedkeys("ggG", "xt")
75
76 " Confirm the timer invoked in exit_cb of the job doesn't disturb
77 " CursorHoldI event.
78 let g:triggered = 0
79 au CursorHoldI * let g:triggered += 1
Bram Moolenaar62cd26a2020-10-11 20:08:44 +020080 set updatetime=100
Bram Moolenaar26d98212019-01-27 22:32:55 +010081 call job_start(has('win32') ? 'cmd /c echo:' : 'echo',
Bram Moolenaar62cd26a2020-10-11 20:08:44 +020082 \ {'exit_cb': {-> timer_start(200, 'ExitInsertMode')}})
Bram Moolenaar26d98212019-01-27 22:32:55 +010083 call feedkeys('a', 'x!')
84 call assert_equal(1, g:triggered)
85 unlet g:triggered
Bram Moolenaare99e8442016-07-26 20:43:40 +020086 au! CursorHoldI
Bram Moolenaaraeac9002016-09-06 22:15:08 +020087 set updatetime&
Bram Moolenaarc67e8922016-05-24 16:07:40 +020088 endfunc
89
90 func Test_cursorhold_insert_ctrl_x()
91 let g:triggered = 0
92 au CursorHoldI * let g:triggered += 1
93 set updatetime=20
94 call timer_start(100, 'ExitInsertMode')
95 " CursorHoldI does not trigger after CTRL-X
96 call feedkeys("a\<C-X>", 'x!')
97 call assert_equal(0, g:triggered)
Bram Moolenaar26d98212019-01-27 22:32:55 +010098 unlet g:triggered
Bram Moolenaare99e8442016-07-26 20:43:40 +020099 au! CursorHoldI
Bram Moolenaaraeac9002016-09-06 22:15:08 +0200100 set updatetime&
Bram Moolenaarc67e8922016-05-24 16:07:40 +0200101 endfunc
Bram Moolenaar97b00752019-05-12 13:07:14 +0200102
Bram Moolenaar5a9357d2021-10-03 16:22:05 +0100103 func Test_cursorhold_insert_ctrl_g_U()
104 au CursorHoldI * :
105 set updatetime=20
106 new
107 call timer_start(100, { -> feedkeys("\<Left>foo\<Esc>", 't') })
108 call feedkeys("i()\<C-g>U", 'tx!')
109 sleep 200m
110 call assert_equal('(foo)', getline(1))
111 undo
112 call assert_equal('', getline(1))
113
114 bwipe!
115 au! CursorHoldI
116 set updatetime&
117 endfunc
118
Bram Moolenaar97b00752019-05-12 13:07:14 +0200119 func Test_OptionSet_modeline()
120 call test_override('starting', 1)
121 au! OptionSet
122 augroup set_tabstop
123 au OptionSet tabstop call timer_start(1, {-> execute("echo 'Handler called'", "")})
124 augroup END
Bram Moolenaare1f3ab72022-09-04 21:29:08 +0100125 call writefile(['vim: set ts=7 sw=5 :', 'something'], 'XoptionsetModeline', 'D')
Bram Moolenaar97b00752019-05-12 13:07:14 +0200126 set modeline
127 let v:errmsg = ''
128 call assert_fails('split XoptionsetModeline', 'E12:')
129 call assert_equal(7, &ts)
130 call assert_equal('', v:errmsg)
131
132 augroup set_tabstop
133 au!
134 augroup END
135 bwipe!
136 set ts&
Bram Moolenaar97b00752019-05-12 13:07:14 +0200137 call test_override('starting', 0)
138 endfunc
139
140endif "has('timers')
Bram Moolenaar40b1b542016-04-20 20:18:23 +0200141
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200142func Test_bufunload()
Bram Moolenaarc67e8922016-05-24 16:07:40 +0200143 augroup test_bufunload_group
144 autocmd!
145 autocmd BufUnload * call add(s:li, "bufunload")
146 autocmd BufDelete * call add(s:li, "bufdelete")
147 autocmd BufWipeout * call add(s:li, "bufwipeout")
148 augroup END
Bram Moolenaar40b1b542016-04-20 20:18:23 +0200149
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100150 let s:li = []
Bram Moolenaarc67e8922016-05-24 16:07:40 +0200151 new
152 setlocal bufhidden=
153 bunload
154 call assert_equal(["bufunload", "bufdelete"], s:li)
Bram Moolenaar40b1b542016-04-20 20:18:23 +0200155
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100156 let s:li = []
Bram Moolenaarc67e8922016-05-24 16:07:40 +0200157 new
158 setlocal bufhidden=delete
159 bunload
160 call assert_equal(["bufunload", "bufdelete"], s:li)
161
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100162 let s:li = []
Bram Moolenaarc67e8922016-05-24 16:07:40 +0200163 new
164 setlocal bufhidden=unload
165 bwipeout
166 call assert_equal(["bufunload", "bufdelete", "bufwipeout"], s:li)
167
Bram Moolenaare99e8442016-07-26 20:43:40 +0200168 au! test_bufunload_group
Bram Moolenaarc67e8922016-05-24 16:07:40 +0200169 augroup! test_bufunload_group
Bram Moolenaar40b1b542016-04-20 20:18:23 +0200170endfunc
Bram Moolenaar30445cb2016-07-09 15:21:02 +0200171
172" SEGV occurs in older versions. (At least 7.4.2005 or older)
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200173func Test_autocmd_bufunload_with_tabnext()
Bram Moolenaar30445cb2016-07-09 15:21:02 +0200174 tabedit
175 tabfirst
176
177 augroup test_autocmd_bufunload_with_tabnext_group
178 autocmd!
179 autocmd BufUnload <buffer> tabnext
180 augroup END
181
182 quit
183 call assert_equal(2, tabpagenr('$'))
184
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200185 autocmd! test_autocmd_bufunload_with_tabnext_group
Bram Moolenaar30445cb2016-07-09 15:21:02 +0200186 augroup! test_autocmd_bufunload_with_tabnext_group
187 tablast
188 quit
189endfunc
Bram Moolenaarc917da42016-07-19 22:31:36 +0200190
Bram Moolenaar5ed58c72021-01-28 14:24:55 +0100191func Test_argdelete_in_next()
192 au BufNew,BufEnter,BufLeave,BufWinEnter * argdel
193 call assert_fails('next a b', 'E1156:')
194 au! BufNew,BufEnter,BufLeave,BufWinEnter *
195endfunc
196
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200197func Test_autocmd_bufwinleave_with_tabfirst()
Bram Moolenaarf9e687e2016-09-04 21:33:09 +0200198 tabedit
199 augroup sample
200 autocmd!
201 autocmd BufWinLeave <buffer> tabfirst
202 augroup END
203 call setline(1, ['a', 'b', 'c'])
204 edit! a.txt
Bram Moolenaarf18c4db2016-09-08 22:10:06 +0200205 tabclose
Bram Moolenaarf9e687e2016-09-04 21:33:09 +0200206endfunc
207
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200208" SEGV occurs in older versions. (At least 7.4.2321 or older)
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200209func Test_autocmd_bufunload_avoiding_SEGV_01()
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200210 split aa.txt
211 let lastbuf = bufnr('$')
212
213 augroup test_autocmd_bufunload
214 autocmd!
215 exe 'autocmd BufUnload <buffer> ' . (lastbuf + 1) . 'bwipeout!'
216 augroup END
217
Bram Moolenaar28ee8922020-10-28 20:20:00 +0100218 call assert_fails('edit bb.txt', 'E937:')
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200219
220 autocmd! test_autocmd_bufunload
221 augroup! test_autocmd_bufunload
222 bwipe! aa.txt
223 bwipe! bb.txt
224endfunc
225
226" SEGV occurs in older versions. (At least 7.4.2321 or older)
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200227func Test_autocmd_bufunload_avoiding_SEGV_02()
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200228 setlocal buftype=nowrite
229 let lastbuf = bufnr('$')
230
231 augroup test_autocmd_bufunload
232 autocmd!
233 exe 'autocmd BufUnload <buffer> ' . (lastbuf + 1) . 'bwipeout!'
234 augroup END
235
236 normal! i1
237 call assert_fails('edit a.txt', 'E517:')
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200238
239 autocmd! test_autocmd_bufunload
240 augroup! test_autocmd_bufunload
241 bwipe! a.txt
242endfunc
243
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100244func Test_autocmd_dummy_wipeout()
245 " prepare files
Bram Moolenaare1f3ab72022-09-04 21:29:08 +0100246 call writefile([''], 'Xdummywipetest1.txt', 'D')
247 call writefile([''], 'Xdummywipetest2.txt', 'D')
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100248 augroup test_bufunload_group
249 autocmd!
250 autocmd BufUnload * call add(s:li, "bufunload")
251 autocmd BufDelete * call add(s:li, "bufdelete")
252 autocmd BufWipeout * call add(s:li, "bufwipeout")
253 augroup END
254
255 let s:li = []
256 split Xdummywipetest1.txt
257 silent! vimgrep /notmatched/ Xdummywipetest*
258 call assert_equal(["bufunload", "bufwipeout"], s:li)
259
260 bwipeout
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100261 au! test_bufunload_group
262 augroup! test_bufunload_group
263endfunc
264
Bram Moolenaarc917da42016-07-19 22:31:36 +0200265func Test_win_tab_autocmd()
266 let g:record = []
267
268 augroup testing
269 au WinNew * call add(g:record, 'WinNew')
naohiro ono23beefe2021-11-13 12:38:49 +0000270 au WinClosed * call add(g:record, 'WinClosed')
Bram Moolenaarc917da42016-07-19 22:31:36 +0200271 au WinEnter * call add(g:record, 'WinEnter')
272 au WinLeave * call add(g:record, 'WinLeave')
273 au TabNew * call add(g:record, 'TabNew')
Bram Moolenaar12c11d52016-07-19 23:13:03 +0200274 au TabClosed * call add(g:record, 'TabClosed')
Bram Moolenaarc917da42016-07-19 22:31:36 +0200275 au TabEnter * call add(g:record, 'TabEnter')
276 au TabLeave * call add(g:record, 'TabLeave')
277 augroup END
278
279 split
280 tabnew
281 close
282 close
283
284 call assert_equal([
285 \ 'WinLeave', 'WinNew', 'WinEnter',
286 \ 'WinLeave', 'TabLeave', 'WinNew', 'WinEnter', 'TabNew', 'TabEnter',
naohiro ono23beefe2021-11-13 12:38:49 +0000287 \ 'WinLeave', 'TabLeave', 'WinClosed', 'TabClosed', 'WinEnter', 'TabEnter',
288 \ 'WinLeave', 'WinClosed', 'WinEnter'
Bram Moolenaarc917da42016-07-19 22:31:36 +0200289 \ ], g:record)
290
Bram Moolenaar12c11d52016-07-19 23:13:03 +0200291 let g:record = []
292 tabnew somefile
293 tabnext
294 bwipe somefile
295
296 call assert_equal([
297 \ 'WinLeave', 'TabLeave', 'WinNew', 'WinEnter', 'TabNew', 'TabEnter',
298 \ 'WinLeave', 'TabLeave', 'WinEnter', 'TabEnter',
naohiro ono23beefe2021-11-13 12:38:49 +0000299 \ 'WinClosed', 'TabClosed'
Bram Moolenaar12c11d52016-07-19 23:13:03 +0200300 \ ], g:record)
301
Bram Moolenaarc917da42016-07-19 22:31:36 +0200302 augroup testing
303 au!
304 augroup END
305 unlet g:record
306endfunc
Bram Moolenaare99e8442016-07-26 20:43:40 +0200307
LemonBoy09371822022-04-08 15:18:45 +0100308func Test_WinScrolled()
309 CheckRunVimInTerminal
310
311 let lines =<< trim END
zeertzjqd58862d2022-04-12 11:32:48 +0100312 set nowrap scrolloff=0
313 for ii in range(1, 18)
314 call setline(ii, repeat(nr2char(96 + ii), ii * 2))
315 endfor
316 let win_id = win_getid()
317 let g:matched = v:false
318 execute 'au WinScrolled' win_id 'let g:matched = v:true'
319 let g:scrolled = 0
320 au WinScrolled * let g:scrolled += 1
321 au WinScrolled * let g:amatch = str2nr(expand('<amatch>'))
322 au WinScrolled * let g:afile = str2nr(expand('<afile>'))
LemonBoy09371822022-04-08 15:18:45 +0100323 END
Bram Moolenaare1f3ab72022-09-04 21:29:08 +0100324 call writefile(lines, 'Xtest_winscrolled', 'D')
LemonBoy09371822022-04-08 15:18:45 +0100325 let buf = RunVimInTerminal('-S Xtest_winscrolled', {'rows': 6})
326
327 call term_sendkeys(buf, ":echo g:scrolled\<CR>")
328 call WaitForAssert({-> assert_match('^0 ', term_getline(buf, 6))}, 1000)
329
330 " Scroll left/right in Normal mode.
331 call term_sendkeys(buf, "zlzh:echo g:scrolled\<CR>")
332 call WaitForAssert({-> assert_match('^2 ', term_getline(buf, 6))}, 1000)
333
334 " Scroll up/down in Normal mode.
335 call term_sendkeys(buf, "\<c-e>\<c-y>:echo g:scrolled\<CR>")
336 call WaitForAssert({-> assert_match('^4 ', term_getline(buf, 6))}, 1000)
337
338 " Scroll up/down in Insert mode.
339 call term_sendkeys(buf, "Mi\<c-x>\<c-e>\<Esc>i\<c-x>\<c-y>\<Esc>")
340 call term_sendkeys(buf, ":echo g:scrolled\<CR>")
341 call WaitForAssert({-> assert_match('^6 ', term_getline(buf, 6))}, 1000)
342
343 " Scroll the window horizontally to focus the last letter of the third line
344 " containing only six characters. Moving to the previous and shorter lines
345 " should trigger another autocommand as Vim has to make them visible.
346 call term_sendkeys(buf, "5zl2k")
347 call term_sendkeys(buf, ":echo g:scrolled\<CR>")
348 call WaitForAssert({-> assert_match('^8 ', term_getline(buf, 6))}, 1000)
349
350 " Ensure the command was triggered for the specified window ID.
351 call term_sendkeys(buf, ":echo g:matched\<CR>")
352 call WaitForAssert({-> assert_match('^v:true ', term_getline(buf, 6))}, 1000)
353
354 " Ensure the expansion of <amatch> and <afile> matches the window ID.
355 call term_sendkeys(buf, ":echo g:amatch == win_id && g:afile == win_id\<CR>")
356 call WaitForAssert({-> assert_match('^v:true ', term_getline(buf, 6))}, 1000)
357
358 call StopVimInTerminal(buf)
LemonBoy09371822022-04-08 15:18:45 +0100359endfunc
360
LemonBoy66e13ae2022-04-21 22:52:11 +0100361func Test_WinScrolled_mouse()
362 CheckRunVimInTerminal
363
364 let lines =<< trim END
365 set nowrap scrolloff=0
366 set mouse=a term=xterm ttymouse=sgr mousetime=200 clipboard=
367 call setline(1, ['foo']->repeat(32))
368 split
369 let g:scrolled = 0
370 au WinScrolled * let g:scrolled += 1
371 END
Bram Moolenaare1f3ab72022-09-04 21:29:08 +0100372 call writefile(lines, 'Xtest_winscrolled_mouse', 'D')
LemonBoy66e13ae2022-04-21 22:52:11 +0100373 let buf = RunVimInTerminal('-S Xtest_winscrolled_mouse', {'rows': 10})
374
375 " With the upper split focused, send a scroll-down event to the unfocused one.
376 call test_setmouse(7, 1)
377 call term_sendkeys(buf, "\<ScrollWheelDown>")
378 call TermWait(buf)
379 call term_sendkeys(buf, ":echo g:scrolled\<CR>")
380 call WaitForAssert({-> assert_match('^1', term_getline(buf, 10))}, 1000)
381
382 " Again, but this time while we're in insert mode.
383 call term_sendkeys(buf, "i\<ScrollWheelDown>\<Esc>")
384 call TermWait(buf)
385 call term_sendkeys(buf, ":echo g:scrolled\<CR>")
386 call WaitForAssert({-> assert_match('^2', term_getline(buf, 10))}, 1000)
387
388 call StopVimInTerminal(buf)
LemonBoy66e13ae2022-04-21 22:52:11 +0100389endfunc
390
zeertzjqd58862d2022-04-12 11:32:48 +0100391func Test_WinScrolled_close_curwin()
392 CheckRunVimInTerminal
393
394 let lines =<< trim END
395 set nowrap scrolloff=0
396 call setline(1, ['aaa', 'bbb'])
397 vsplit
398 au WinScrolled * close
399 au VimLeave * call writefile(['123456'], 'Xtestout')
400 END
Bram Moolenaare1f3ab72022-09-04 21:29:08 +0100401 call writefile(lines, 'Xtest_winscrolled_close_curwin', 'D')
zeertzjqd58862d2022-04-12 11:32:48 +0100402 let buf = RunVimInTerminal('-S Xtest_winscrolled_close_curwin', {'rows': 6})
403
404 " This was using freed memory
405 call term_sendkeys(buf, "\<C-E>")
406 call TermWait(buf)
407 call StopVimInTerminal(buf)
408
409 call assert_equal(['123456'], readfile('Xtestout'))
410
zeertzjqd58862d2022-04-12 11:32:48 +0100411 call delete('Xtestout')
412endfunc
413
zeertzjq670ab032022-08-28 19:16:15 +0100414func Test_WinScrolled_long_wrapped()
415 CheckRunVimInTerminal
416
417 let lines =<< trim END
418 set scrolloff=0
419 let height = winheight(0)
420 let width = winwidth(0)
421 let g:scrolled = 0
422 au WinScrolled * let g:scrolled += 1
423 call setline(1, repeat('foo', height * width))
424 call cursor(1, height * width)
425 END
Bram Moolenaare1f3ab72022-09-04 21:29:08 +0100426 call writefile(lines, 'Xtest_winscrolled_long_wrapped', 'D')
zeertzjq670ab032022-08-28 19:16:15 +0100427 let buf = RunVimInTerminal('-S Xtest_winscrolled_long_wrapped', {'rows': 6})
428
429 call term_sendkeys(buf, ":echo g:scrolled\<CR>")
430 call WaitForAssert({-> assert_match('^0 ', term_getline(buf, 6))}, 1000)
431
432 call term_sendkeys(buf, 'gj')
433 call term_sendkeys(buf, ":echo g:scrolled\<CR>")
434 call WaitForAssert({-> assert_match('^1 ', term_getline(buf, 6))}, 1000)
435
436 call term_sendkeys(buf, '0')
437 call term_sendkeys(buf, ":echo g:scrolled\<CR>")
438 call WaitForAssert({-> assert_match('^2 ', term_getline(buf, 6))}, 1000)
439
440 call term_sendkeys(buf, '$')
441 call term_sendkeys(buf, ":echo g:scrolled\<CR>")
442 call WaitForAssert({-> assert_match('^3 ', term_getline(buf, 6))}, 1000)
zeertzjq670ab032022-08-28 19:16:15 +0100443endfunc
444
naohiro ono23beefe2021-11-13 12:38:49 +0000445func Test_WinClosed()
446 " Test that the pattern is matched against the closed window's ID, and both
447 " <amatch> and <afile> are set to it.
448 new
449 let winid = win_getid()
450 let g:matched = v:false
451 augroup test-WinClosed
452 autocmd!
453 execute 'autocmd WinClosed' winid 'let g:matched = v:true'
454 autocmd WinClosed * let g:amatch = str2nr(expand('<amatch>'))
455 autocmd WinClosed * let g:afile = str2nr(expand('<afile>'))
456 augroup END
457 close
458 call assert_true(g:matched)
459 call assert_equal(winid, g:amatch)
460 call assert_equal(winid, g:afile)
461
462 " Test that WinClosed is non-recursive.
463 new
464 new
465 call assert_equal(3, winnr('$'))
466 let g:triggered = 0
467 augroup test-WinClosed
468 autocmd!
469 autocmd WinClosed * let g:triggered += 1
470 autocmd WinClosed * 2 wincmd c
471 augroup END
472 close
473 call assert_equal(1, winnr('$'))
474 call assert_equal(1, g:triggered)
475
476 autocmd! test-WinClosed
477 augroup! test-WinClosed
478 unlet g:matched
479 unlet g:amatch
480 unlet g:afile
481 unlet g:triggered
482endfunc
483
Bram Moolenaarc947b9a2022-04-06 17:59:21 +0100484func Test_WinClosed_throws()
485 vnew
486 let bnr = bufnr()
487 call assert_equal(1, bufloaded(bnr))
488 augroup test-WinClosed
489 autocmd WinClosed * throw 'foo'
490 augroup END
491 try
492 close
493 catch /.*/
494 endtry
495 call assert_equal(0, bufloaded(bnr))
496
497 autocmd! test-WinClosed
498 augroup! test-WinClosed
499endfunc
500
zeertzjq6a069402022-04-07 14:08:29 +0100501func Test_WinClosed_throws_with_tabs()
502 tabnew
503 let bnr = bufnr()
504 call assert_equal(1, bufloaded(bnr))
505 augroup test-WinClosed
506 autocmd WinClosed * throw 'foo'
507 augroup END
508 try
509 close
510 catch /.*/
511 endtry
512 call assert_equal(0, bufloaded(bnr))
513
514 autocmd! test-WinClosed
515 augroup! test-WinClosed
516endfunc
517
zeertzjq62de54b2022-09-22 18:08:37 +0100518" This used to trigger WinClosed twice for the same window, and the window's
519" buffer was NULL in the second autocommand.
520func Test_WinClosed_switch_tab()
521 edit Xa
522 split Xb
523 split Xc
524 tab split
525 new
526 augroup test-WinClosed
527 autocmd WinClosed * tabprev | bwipe!
528 augroup END
529 close
530 " Check that the tabline has been fully removed
531 call assert_equal([1, 1], win_screenpos(0))
532
533 autocmd! test-WinClosed
534 augroup! test-WinClosed
535 %bwipe!
536endfunc
537
Bram Moolenaare99e8442016-07-26 20:43:40 +0200538func s:AddAnAutocmd()
539 augroup vimBarTest
540 au BufReadCmd * echo 'hello'
541 augroup END
542 call assert_equal(3, len(split(execute('au vimBarTest'), "\n")))
543endfunc
544
545func Test_early_bar()
546 " test that a bar is recognized before the {event}
547 call s:AddAnAutocmd()
Bram Moolenaarb8e642f2021-11-20 10:38:25 +0000548 augroup vimBarTest | au! | let done = 77 | augroup END
Bram Moolenaare99e8442016-07-26 20:43:40 +0200549 call assert_equal(1, len(split(execute('au vimBarTest'), "\n")))
Bram Moolenaarb8e642f2021-11-20 10:38:25 +0000550 call assert_equal(77, done)
Bram Moolenaare99e8442016-07-26 20:43:40 +0200551
552 call s:AddAnAutocmd()
Bram Moolenaarb8e642f2021-11-20 10:38:25 +0000553 augroup vimBarTest| au!| let done = 88 | augroup END
Bram Moolenaare99e8442016-07-26 20:43:40 +0200554 call assert_equal(1, len(split(execute('au vimBarTest'), "\n")))
Bram Moolenaarb8e642f2021-11-20 10:38:25 +0000555 call assert_equal(88, done)
Bram Moolenaare99e8442016-07-26 20:43:40 +0200556
557 " test that a bar is recognized after the {event}
558 call s:AddAnAutocmd()
Bram Moolenaarb8e642f2021-11-20 10:38:25 +0000559 augroup vimBarTest| au!BufReadCmd| let done = 99 | augroup END
Bram Moolenaare99e8442016-07-26 20:43:40 +0200560 call assert_equal(1, len(split(execute('au vimBarTest'), "\n")))
Bram Moolenaarb8e642f2021-11-20 10:38:25 +0000561 call assert_equal(99, done)
Bram Moolenaare99e8442016-07-26 20:43:40 +0200562
563 " test that a bar is recognized after the {group}
564 call s:AddAnAutocmd()
565 au! vimBarTest|echo 'hello'
566 call assert_equal(1, len(split(execute('au vimBarTest'), "\n")))
567endfunc
Bram Moolenaarf2c4c392016-07-29 20:50:24 +0200568
Bram Moolenaar5c809082016-09-01 16:21:48 +0200569func RemoveGroup()
570 autocmd! StartOK
571 augroup! StartOK
572endfunc
573
Bram Moolenaarf2c4c392016-07-29 20:50:24 +0200574func Test_augroup_warning()
575 augroup TheWarning
576 au VimEnter * echo 'entering'
577 augroup END
Bram Moolenaar5dc4e2f2020-11-25 14:15:12 +0100578 call assert_match("TheWarning.*VimEnter", execute('au VimEnter'))
Bram Moolenaarf2c4c392016-07-29 20:50:24 +0200579 redir => res
580 augroup! TheWarning
581 redir END
Bram Moolenaar5dc4e2f2020-11-25 14:15:12 +0100582 call assert_match("W19:", res)
583 call assert_match("-Deleted-.*VimEnter", execute('au VimEnter'))
Bram Moolenaarf2c4c392016-07-29 20:50:24 +0200584
585 " check "Another" does not take the pace of the deleted entry
586 augroup Another
587 augroup END
Bram Moolenaar5dc4e2f2020-11-25 14:15:12 +0100588 call assert_match("-Deleted-.*VimEnter", execute('au VimEnter'))
Bram Moolenaaraeac9002016-09-06 22:15:08 +0200589 augroup! Another
Bram Moolenaar5c809082016-09-01 16:21:48 +0200590
591 " no warning for postpone aucmd delete
592 augroup StartOK
593 au VimEnter * call RemoveGroup()
594 augroup END
Bram Moolenaar5dc4e2f2020-11-25 14:15:12 +0100595 call assert_match("StartOK.*VimEnter", execute('au VimEnter'))
Bram Moolenaar5c809082016-09-01 16:21:48 +0200596 redir => res
597 doautocmd VimEnter
598 redir END
Bram Moolenaar5dc4e2f2020-11-25 14:15:12 +0100599 call assert_notmatch("W19:", res)
Bram Moolenaarde653f02016-09-03 16:59:06 +0200600 au! VimEnter
Bram Moolenaarad48e6c2020-04-21 22:19:45 +0200601
602 call assert_fails('augroup!', 'E471:')
Bram Moolenaarf2c4c392016-07-29 20:50:24 +0200603endfunc
Bram Moolenaarb62cc362016-09-03 16:43:53 +0200604
Bram Moolenaar8d84ff12017-10-26 16:42:16 +0200605func Test_BufReadCmdHelp()
606 " This used to cause access to free memory
607 au BufReadCmd * e +h
608 help
609
Bram Moolenaar8d84ff12017-10-26 16:42:16 +0200610 au! BufReadCmd
611endfunc
612
613func Test_BufReadCmdHelpJump()
614 " This used to cause access to free memory
615 au BufReadCmd * e +h{
Bram Moolenaarcf1ba352017-10-27 00:55:04 +0200616 " } to fix highlighting
617 call assert_fails('help', 'E434:')
Bram Moolenaar8d84ff12017-10-26 16:42:16 +0200618
Bram Moolenaar8d84ff12017-10-26 16:42:16 +0200619 au! BufReadCmd
620endfunc
621
zeertzjq93f72cc2022-08-26 15:34:52 +0100622" BufReadCmd is triggered for a "nofile" buffer. Check all values.
Bram Moolenaarb1d2c812022-08-26 11:55:01 +0100623func Test_BufReadCmdNofile()
zeertzjq93f72cc2022-08-26 15:34:52 +0100624 for val in ['nofile',
625 \ 'nowrite',
626 \ 'acwrite',
627 \ 'quickfix',
628 \ 'help',
629 \ 'terminal',
630 \ 'prompt',
631 \ 'popup',
632 \ ]
633 new somefile
634 exe 'set buftype=' .. val
635 au BufReadCmd somefile call setline(1, 'triggered')
636 edit
637 call assert_equal('triggered', getline(1))
Bram Moolenaarb1d2c812022-08-26 11:55:01 +0100638
zeertzjq93f72cc2022-08-26 15:34:52 +0100639 au! BufReadCmd
640 bwipe!
641 endfor
Bram Moolenaarb1d2c812022-08-26 11:55:01 +0100642endfunc
643
Bram Moolenaarb62cc362016-09-03 16:43:53 +0200644func Test_augroup_deleted()
Bram Moolenaarde653f02016-09-03 16:59:06 +0200645 " This caused a crash before E936 was introduced
Bram Moolenaarb62cc362016-09-03 16:43:53 +0200646 augroup x
Bram Moolenaarde653f02016-09-03 16:59:06 +0200647 call assert_fails('augroup! x', 'E936:')
648 au VimEnter * echo
649 augroup end
Bram Moolenaarb62cc362016-09-03 16:43:53 +0200650 augroup! x
Bram Moolenaar5dc4e2f2020-11-25 14:15:12 +0100651 call assert_match("-Deleted-.*VimEnter", execute('au VimEnter'))
Bram Moolenaarde653f02016-09-03 16:59:06 +0200652 au! VimEnter
Bram Moolenaarb62cc362016-09-03 16:43:53 +0200653endfunc
654
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200655" Tests for autocommands on :close command.
656" This used to be in test13.
657func Test_three_windows()
Bram Moolenaarb3435b02016-09-29 20:54:59 +0200658 " Clean up buffers, because in some cases this function fails.
659 call s:cleanup_buffers()
660
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200661 " Write three files and open them, each in a window.
662 " Then go to next window, with autocommand that deletes the previous one.
663 " Do this twice, writing the file.
664 e! Xtestje1
665 call setline(1, 'testje1')
666 w
667 sp Xtestje2
668 call setline(1, 'testje2')
669 w
670 sp Xtestje3
671 call setline(1, 'testje3')
672 w
673 wincmd w
674 au WinLeave Xtestje2 bwipe
675 wincmd w
676 call assert_equal('Xtestje1', expand('%'))
677
678 au WinLeave Xtestje1 bwipe Xtestje3
679 close
680 call assert_equal('Xtestje1', expand('%'))
681
682 " Test deleting the buffer on a Unload event. If this goes wrong there
683 " will be the ATTENTION prompt.
684 e Xtestje1
685 au!
686 au! BufUnload Xtestje1 bwipe
687 call assert_fails('e Xtestje3', 'E937:')
688 call assert_equal('Xtestje3', expand('%'))
689
690 e Xtestje2
691 sp Xtestje1
692 call assert_fails('e', 'E937:')
Bram Moolenaara997b452018-04-17 23:24:06 +0200693 call assert_equal('Xtestje1', expand('%'))
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200694
695 " Test changing buffers in a BufWipeout autocommand. If this goes wrong
696 " there are ml_line errors and/or a Crash.
697 au!
698 only
699 e Xanother
700 e Xtestje1
701 bwipe Xtestje2
702 bwipe Xtestje3
703 au BufWipeout Xtestje1 buf Xtestje1
704 bwipe
705 call assert_equal('Xanother', expand('%'))
706
707 only
708 help
709 wincmd w
710 1quit
711 call assert_equal('Xanother', expand('%'))
712
713 au!
Bram Moolenaar4520d442017-03-19 16:09:46 +0100714 enew
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200715 call delete('Xtestje1')
716 call delete('Xtestje2')
717 call delete('Xtestje3')
718endfunc
Bram Moolenaare13b9af2017-01-13 22:01:02 +0100719
720func Test_BufEnter()
721 au! BufEnter
722 au Bufenter * let val = val . '+'
723 let g:val = ''
724 split NewFile
725 call assert_equal('+', g:val)
726 bwipe!
727 call assert_equal('++', g:val)
728
729 " Also get BufEnter when editing a directory
Bram Moolenaar6f14da12022-09-07 21:30:44 +0100730 call mkdir('Xbufenterdir', 'D')
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +0100731 split Xbufenterdir
Bram Moolenaare13b9af2017-01-13 22:01:02 +0100732 call assert_equal('+++', g:val)
Bram Moolenaare94260f2017-03-21 15:50:12 +0100733
734 " On MS-Windows we can't edit the directory, make sure we wipe the right
735 " buffer.
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +0100736 bwipe! Xbufenterdir
Bram Moolenaare13b9af2017-01-13 22:01:02 +0100737 au! BufEnter
Bram Moolenaara9b5b852022-08-26 13:16:20 +0100738
739 " Editing a "nofile" buffer doesn't read the file but does trigger BufEnter
zeertzjq93f72cc2022-08-26 15:34:52 +0100740 " for historic reasons. Also test other 'buftype' values.
741 for val in ['nofile',
742 \ 'nowrite',
743 \ 'acwrite',
744 \ 'quickfix',
745 \ 'help',
746 \ 'terminal',
747 \ 'prompt',
748 \ 'popup',
749 \ ]
750 new somefile
751 exe 'set buftype=' .. val
752 au BufEnter somefile call setline(1, 'some text')
753 edit
754 call assert_equal('some text', getline(1))
755 bwipe!
756 au! BufEnter
757 endfor
Bram Moolenaare13b9af2017-01-13 22:01:02 +0100758endfunc
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100759
760" Closing a window might cause an endless loop
761" E814 for older Vims
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200762func Test_autocmd_bufwipe_in_SessLoadPost()
Bram Moolenaar1d68d9b2017-10-13 22:33:32 +0200763 edit Xtest
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100764 tabnew
Bram Moolenaar1d68d9b2017-10-13 22:33:32 +0200765 file Xsomething
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100766 set noswapfile
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100767 mksession!
768
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200769 let content =<< trim [CODE]
Bram Moolenaar62cd26a2020-10-11 20:08:44 +0200770 call test_override('ui_delay', 10)
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200771 set nocp noswapfile
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100772 let v:swapchoice = "e"
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200773 augroup test_autocmd_sessionload
774 autocmd!
775 autocmd SessionLoadPost * exe bufnr("Xsomething") . "bw!"
776 augroup END
777
778 func WriteErrors()
Bram Moolenaarbe9fc5b2022-09-09 17:09:35 +0100779 call writefile([execute("messages")], "XerrorsBwipe")
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200780 endfunc
781 au VimLeave * call WriteErrors()
782 [CODE]
783
Bram Moolenaare1f3ab72022-09-04 21:29:08 +0100784 call writefile(content, 'Xvimrc', 'D')
Bram Moolenaar93344c22019-08-14 21:12:05 +0200785 call system(GetVimCommand('Xvimrc') .. ' --not-a-term --noplugins -S Session.vim -c cq')
Bram Moolenaarae04a602022-09-09 15:08:10 +0100786 sleep 100m
Bram Moolenaarbe9fc5b2022-09-09 17:09:35 +0100787 let errors = join(readfile('XerrorsBwipe'))
Bram Moolenaare2e40752020-09-04 21:18:46 +0200788 call assert_match('E814:', errors)
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100789
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100790 set swapfile
Bram Moolenaarbe9fc5b2022-09-09 17:09:35 +0100791 for file in ['Session.vim', 'XerrorsBwipe']
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100792 call delete(file)
793 endfor
794endfunc
795
Bram Moolenaar797e63b2021-01-15 16:22:52 +0100796" Using :blast and :ball for many events caused a crash, because b_nwindows was
797" not incremented correctly.
798func Test_autocmd_blast_badd()
799 let content =<< trim [CODE]
800 au BufNew,BufAdd,BufWinEnter,BufEnter,BufLeave,BufWinLeave,BufUnload,VimEnter foo* blast
801 edit foo1
802 au BufNew,BufAdd,BufWinEnter,BufEnter,BufLeave,BufWinLeave,BufUnload,VimEnter foo* ball
803 edit foo2
Bram Moolenaarbe9fc5b2022-09-09 17:09:35 +0100804 call writefile(['OK'], 'XerrorsBlast')
Bram Moolenaar797e63b2021-01-15 16:22:52 +0100805 qall
806 [CODE]
807
Bram Moolenaare1f3ab72022-09-04 21:29:08 +0100808 call writefile(content, 'XblastBall', 'D')
Bram Moolenaar797e63b2021-01-15 16:22:52 +0100809 call system(GetVimCommand() .. ' --clean -S XblastBall')
Bram Moolenaarae04a602022-09-09 15:08:10 +0100810 sleep 100m
Bram Moolenaarbe9fc5b2022-09-09 17:09:35 +0100811 call assert_match('OK', readfile('XerrorsBlast')->join())
Bram Moolenaar797e63b2021-01-15 16:22:52 +0100812
Bram Moolenaarbe9fc5b2022-09-09 17:09:35 +0100813 call delete('XerrorsBlast')
Bram Moolenaar797e63b2021-01-15 16:22:52 +0100814endfunc
815
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100816" SEGV occurs in older versions.
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200817func Test_autocmd_bufwipe_in_SessLoadPost2()
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100818 tabnew
819 set noswapfile
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100820 mksession!
821
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200822 let content =<< trim [CODE]
823 set nocp noswapfile
824 function! DeleteInactiveBufs()
825 tabfirst
826 let tabblist = []
827 for i in range(1, tabpagenr(''$''))
828 call extend(tabblist, tabpagebuflist(i))
829 endfor
830 for b in range(1, bufnr(''$''))
831 if bufexists(b) && buflisted(b) && (index(tabblist, b) == -1 || bufname(b) =~# ''^$'')
832 exec ''bwipeout '' . b
833 endif
834 endfor
835 echomsg "SessionLoadPost DONE"
836 endfunction
837 au SessionLoadPost * call DeleteInactiveBufs()
838
839 func WriteErrors()
Bram Moolenaarbe9fc5b2022-09-09 17:09:35 +0100840 call writefile([execute("messages")], "XerrorsPost")
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200841 endfunc
842 au VimLeave * call WriteErrors()
843 [CODE]
844
Bram Moolenaare1f3ab72022-09-04 21:29:08 +0100845 call writefile(content, 'Xvimrc', 'D')
Bram Moolenaar93344c22019-08-14 21:12:05 +0200846 call system(GetVimCommand('Xvimrc') .. ' --not-a-term --noplugins -S Session.vim -c cq')
Bram Moolenaarae04a602022-09-09 15:08:10 +0100847 sleep 100m
Bram Moolenaarbe9fc5b2022-09-09 17:09:35 +0100848 let errors = join(readfile('XerrorsPost'))
Bram Moolenaare94260f2017-03-21 15:50:12 +0100849 " This probably only ever matches on unix.
850 call assert_notmatch('Caught deadly signal SEGV', errors)
851 call assert_match('SessionLoadPost DONE', errors)
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100852
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100853 set swapfile
Bram Moolenaarbe9fc5b2022-09-09 17:09:35 +0100854 for file in ['Session.vim', 'XerrorsPost']
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100855 call delete(file)
856 endfor
857endfunc
Bram Moolenaarfaf29d72017-07-09 11:07:16 +0200858
859func Test_empty_doau()
860 doau \|
861endfunc
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200862
863func s:AutoCommandOptionSet(match)
Bram Moolenaard7c96872019-06-15 17:12:48 +0200864 let template = "Option: <%s>, OldVal: <%s>, OldValLocal: <%s>, OldValGlobal: <%s>, NewVal: <%s>, Scope: <%s>, Command: <%s>\n"
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200865 let item = remove(g:options, 0)
Bram Moolenaard7c96872019-06-15 17:12:48 +0200866 let expected = printf(template, item[0], item[1], item[2], item[3], item[4], item[5], item[6])
867 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 +0200868 let g:opt = [expected, actual]
869 "call assert_equal(expected, actual)
870endfunc
871
872func Test_OptionSet()
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +0200873 CheckOption autochdir
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200874
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +0200875 badd test_autocmd.vim
876
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200877 call test_override('starting', 1)
878 set nocp
879 au OptionSet * :call s:AutoCommandOptionSet(expand("<amatch>"))
880
881 " 1: Setting number option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100882 let g:options = [['number', 0, 0, 0, 1, 'global', 'set']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200883 set nu
884 call assert_equal([], g:options)
885 call assert_equal(g:opt[0], g:opt[1])
886
887 " 2: Setting local number option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100888 let g:options = [['number', 1, 1, '', 0, 'local', 'setlocal']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200889 setlocal nonu
890 call assert_equal([], g:options)
891 call assert_equal(g:opt[0], g:opt[1])
892
893 " 3: Setting global number option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100894 let g:options = [['number', 1, '', 1, 0, 'global', 'setglobal']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200895 setglobal nonu
896 call assert_equal([], g:options)
897 call assert_equal(g:opt[0], g:opt[1])
898
899 " 4: Setting local autoindent option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100900 let g:options = [['autoindent', 0, 0, '', 1, 'local', 'setlocal']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200901 setlocal ai
902 call assert_equal([], g:options)
903 call assert_equal(g:opt[0], g:opt[1])
904
905 " 5: Setting global autoindent option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100906 let g:options = [['autoindent', 0, '', 0, 1, 'global', 'setglobal']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200907 setglobal ai
908 call assert_equal([], g:options)
909 call assert_equal(g:opt[0], g:opt[1])
910
911 " 6: Setting global autoindent option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100912 let g:options = [['autoindent', 1, 1, 1, 0, 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +0200913 set ai!
914 call assert_equal([], g:options)
915 call assert_equal(g:opt[0], g:opt[1])
916
917 " 6a: Setting global autoindent option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100918 let g:options = [['autoindent', 1, 1, 0, 0, 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +0200919 noa setlocal ai
920 noa setglobal noai
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200921 set ai!
922 call assert_equal([], g:options)
923 call assert_equal(g:opt[0], g:opt[1])
924
925 " Should not print anything, use :noa
926 " 7: don't trigger OptionSet"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100927 let g:options = [['invalid', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200928 noa set nonu
Bram Moolenaard7c96872019-06-15 17:12:48 +0200929 call assert_equal([['invalid', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid']], g:options)
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200930 call assert_equal(g:opt[0], g:opt[1])
931
932 " 8: Setting several global list and number option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100933 let g:options = [['list', 0, 0, 0, 1, 'global', 'set'], ['number', 0, 0, 0, 1, 'global', 'set']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200934 set list nu
935 call assert_equal([], g:options)
936 call assert_equal(g:opt[0], g:opt[1])
937
938 " 9: don't trigger OptionSet"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100939 let g:options = [['invalid', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid'], ['invalid', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200940 noa set nolist nonu
Bram Moolenaard7c96872019-06-15 17:12:48 +0200941 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 +0200942 call assert_equal(g:opt[0], g:opt[1])
943
944 " 10: Setting global acd"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100945 let g:options = [['autochdir', 0, 0, '', 1, 'local', 'setlocal']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200946 setlocal acd
947 call assert_equal([], g:options)
948 call assert_equal(g:opt[0], g:opt[1])
949
950 " 11: Setting global autoread (also sets local value)"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100951 let g:options = [['autoread', 0, 0, 0, 1, 'global', 'set']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200952 set ar
953 call assert_equal([], g:options)
954 call assert_equal(g:opt[0], g:opt[1])
955
956 " 12: Setting local autoread"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100957 let g:options = [['autoread', 1, 1, '', 1, 'local', 'setlocal']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200958 setlocal ar
959 call assert_equal([], g:options)
960 call assert_equal(g:opt[0], g:opt[1])
961
962 " 13: Setting global autoread"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100963 let g:options = [['autoread', 1, '', 1, 0, 'global', 'setglobal']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200964 setglobal invar
965 call assert_equal([], g:options)
966 call assert_equal(g:opt[0], g:opt[1])
967
968 " 14: Setting option backspace through :let"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100969 let g:options = [['backspace', '', '', '', 'eol,indent,start', 'global', 'set']]
970 let &bs = "eol,indent,start"
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200971 call assert_equal([], g:options)
972 call assert_equal(g:opt[0], g:opt[1])
973
974 " 15: Setting option backspace through setbufvar()"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100975 let g:options = [['backup', 0, 0, '', 1, 'local', 'setlocal']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200976 " try twice, first time, shouldn't trigger because option name is invalid,
977 " second time, it should trigger
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +0200978 let bnum = bufnr('%')
Bram Moolenaare2e40752020-09-04 21:18:46 +0200979 call assert_fails("call setbufvar(bnum, '&l:bk', 1)", 'E355:')
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200980 " should trigger, use correct option name
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +0200981 call setbufvar(bnum, '&backup', 1)
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200982 call assert_equal([], g:options)
983 call assert_equal(g:opt[0], g:opt[1])
984
985 " 16: Setting number option using setwinvar"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100986 let g:options = [['number', 0, 0, '', 1, 'local', 'setlocal']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200987 call setwinvar(0, '&number', 1)
988 call assert_equal([], g:options)
989 call assert_equal(g:opt[0], g:opt[1])
990
991 " 17: Setting key option, shouldn't trigger"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100992 let g:options = [['key', 'invalid', 'invalid1', 'invalid2', 'invalid3', 'invalid4', 'invalid5']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200993 setlocal key=blah
994 setlocal key=
Bram Moolenaard7c96872019-06-15 17:12:48 +0200995 call assert_equal([['key', 'invalid', 'invalid1', 'invalid2', 'invalid3', 'invalid4', 'invalid5']], g:options)
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200996 call assert_equal(g:opt[0], g:opt[1])
997
Bram Moolenaard7c96872019-06-15 17:12:48 +0200998
999 " 18a: Setting string global option"
1000 let oldval = &backupext
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001001 let g:options = [['backupext', oldval, oldval, oldval, 'foo', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001002 set backupext=foo
1003 call assert_equal([], g:options)
1004 call assert_equal(g:opt[0], g:opt[1])
1005
1006 " 18b: Resetting string global option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001007 let g:options = [['backupext', 'foo', 'foo', 'foo', oldval, 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001008 set backupext&
1009 call assert_equal([], g:options)
1010 call assert_equal(g:opt[0], g:opt[1])
1011
1012 " 18c: Setting global string global option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001013 let g:options = [['backupext', oldval, '', oldval, 'bar', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001014 setglobal backupext=bar
1015 call assert_equal([], g:options)
1016 call assert_equal(g:opt[0], g:opt[1])
1017
1018 " 18d: Setting local string global option"
1019 " As this is a global option this sets the global value even though
1020 " :setlocal is used!
1021 noa set backupext& " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001022 let g:options = [['backupext', oldval, oldval, '', 'baz', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001023 setlocal backupext=baz
1024 call assert_equal([], g:options)
1025 call assert_equal(g:opt[0], g:opt[1])
1026
1027 " 18e: Setting again string global option"
1028 noa setglobal backupext=ext_global " Reset global and local value (without triggering autocmd)
1029 noa setlocal backupext=ext_local " Sets the global(!) value!
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001030 let g:options = [['backupext', 'ext_local', 'ext_local', 'ext_local', 'fuu', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001031 set backupext=fuu
1032 call assert_equal([], g:options)
1033 call assert_equal(g:opt[0], g:opt[1])
1034
1035
zeertzjqb811de52021-10-21 10:50:44 +01001036 " 19a: Setting string global-local (to buffer) option"
Bram Moolenaar8efa0262017-08-20 15:47:20 +02001037 let oldval = &tags
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001038 let g:options = [['tags', oldval, oldval, oldval, 'tagpath', 'global', 'set']]
Bram Moolenaar8efa0262017-08-20 15:47:20 +02001039 set tags=tagpath
1040 call assert_equal([], g:options)
1041 call assert_equal(g:opt[0], g:opt[1])
1042
zeertzjqb811de52021-10-21 10:50:44 +01001043 " 19b: Resetting string global-local (to buffer) option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001044 let g:options = [['tags', 'tagpath', 'tagpath', 'tagpath', oldval, 'global', 'set']]
Bram Moolenaar8efa0262017-08-20 15:47:20 +02001045 set tags&
1046 call assert_equal([], g:options)
1047 call assert_equal(g:opt[0], g:opt[1])
1048
zeertzjqb811de52021-10-21 10:50:44 +01001049 " 19c: Setting global string global-local (to buffer) option "
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001050 let g:options = [['tags', oldval, '', oldval, 'tagpath1', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001051 setglobal tags=tagpath1
1052 call assert_equal([], g:options)
1053 call assert_equal(g:opt[0], g:opt[1])
1054
zeertzjqb811de52021-10-21 10:50:44 +01001055 " 19d: Setting local string global-local (to buffer) option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001056 let g:options = [['tags', 'tagpath1', 'tagpath1', '', 'tagpath2', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001057 setlocal tags=tagpath2
1058 call assert_equal([], g:options)
1059 call assert_equal(g:opt[0], g:opt[1])
1060
zeertzjqb811de52021-10-21 10:50:44 +01001061 " 19e: Setting again string global-local (to buffer) option"
1062 " Note: v:option_old is the old global value for global-local string options
Bram Moolenaard7c96872019-06-15 17:12:48 +02001063 " but the old local value for all other kinds of options.
1064 noa setglobal tags=tag_global " Reset global and local value (without triggering autocmd)
1065 noa setlocal tags=tag_local
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001066 let g:options = [['tags', 'tag_global', 'tag_local', 'tag_global', 'tagpath', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001067 set tags=tagpath
1068 call assert_equal([], g:options)
1069 call assert_equal(g:opt[0], g:opt[1])
1070
zeertzjqb811de52021-10-21 10:50:44 +01001071 " 19f: Setting string global-local (to buffer) option to an empty string"
1072 " Note: v:option_old is the old global value for global-local string options
Bram Moolenaard7c96872019-06-15 17:12:48 +02001073 " but the old local value for all other kinds of options.
1074 noa set tags=tag_global " Reset global and local value (without triggering autocmd)
1075 noa setlocal tags= " empty string
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001076 let g:options = [['tags', 'tag_global', '', 'tag_global', 'tagpath', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001077 set tags=tagpath
1078 call assert_equal([], g:options)
1079 call assert_equal(g:opt[0], g:opt[1])
1080
1081
1082 " 20a: Setting string local (to buffer) option"
1083 let oldval = &spelllang
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001084 let g:options = [['spelllang', oldval, oldval, oldval, 'elvish,klingon', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001085 set spelllang=elvish,klingon
1086 call assert_equal([], g:options)
1087 call assert_equal(g:opt[0], g:opt[1])
1088
1089 " 20b: Resetting string local (to buffer) option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001090 let g:options = [['spelllang', 'elvish,klingon', 'elvish,klingon', 'elvish,klingon', oldval, 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001091 set spelllang&
1092 call assert_equal([], g:options)
1093 call assert_equal(g:opt[0], g:opt[1])
1094
1095 " 20c: Setting global string local (to buffer) option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001096 let g:options = [['spelllang', oldval, '', oldval, 'elvish', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001097 setglobal spelllang=elvish
1098 call assert_equal([], g:options)
1099 call assert_equal(g:opt[0], g:opt[1])
1100
1101 " 20d: Setting local string local (to buffer) option"
1102 noa set spelllang& " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001103 let g:options = [['spelllang', oldval, oldval, '', 'klingon', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001104 setlocal spelllang=klingon
1105 call assert_equal([], g:options)
1106 call assert_equal(g:opt[0], g:opt[1])
1107
1108 " 20e: Setting again string local (to buffer) option"
zeertzjqb811de52021-10-21 10:50:44 +01001109 " Note: v:option_old is the old global value for global-local string options
Bram Moolenaard7c96872019-06-15 17:12:48 +02001110 " but the old local value for all other kinds of options.
1111 noa setglobal spelllang=spellglobal " Reset global and local value (without triggering autocmd)
1112 noa setlocal spelllang=spelllocal
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001113 let g:options = [['spelllang', 'spelllocal', 'spelllocal', 'spellglobal', 'foo', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001114 set spelllang=foo
1115 call assert_equal([], g:options)
1116 call assert_equal(g:opt[0], g:opt[1])
1117
1118
zeertzjqb811de52021-10-21 10:50:44 +01001119 " 21a: Setting string global-local (to window) option"
Bram Moolenaard7c96872019-06-15 17:12:48 +02001120 let oldval = &statusline
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001121 let g:options = [['statusline', oldval, oldval, oldval, 'foo', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001122 set statusline=foo
1123 call assert_equal([], g:options)
1124 call assert_equal(g:opt[0], g:opt[1])
1125
zeertzjqb811de52021-10-21 10:50:44 +01001126 " 21b: Resetting string global-local (to window) option"
1127 " Note: v:option_old is the old global value for global-local string options
Bram Moolenaard7c96872019-06-15 17:12:48 +02001128 " but the old local value for all other kinds of options.
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001129 let g:options = [['statusline', 'foo', 'foo', 'foo', oldval, 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001130 set statusline&
1131 call assert_equal([], g:options)
1132 call assert_equal(g:opt[0], g:opt[1])
1133
zeertzjqb811de52021-10-21 10:50:44 +01001134 " 21c: Setting global string global-local (to window) option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001135 let g:options = [['statusline', oldval, '', oldval, 'bar', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001136 setglobal statusline=bar
1137 call assert_equal([], g:options)
1138 call assert_equal(g:opt[0], g:opt[1])
1139
zeertzjqb811de52021-10-21 10:50:44 +01001140 " 21d: Setting local string global-local (to window) option"
Bram Moolenaard7c96872019-06-15 17:12:48 +02001141 noa set statusline& " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001142 let g:options = [['statusline', oldval, oldval, '', 'baz', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001143 setlocal statusline=baz
1144 call assert_equal([], g:options)
1145 call assert_equal(g:opt[0], g:opt[1])
1146
zeertzjqb811de52021-10-21 10:50:44 +01001147 " 21e: Setting again string global-local (to window) option"
1148 " Note: v:option_old is the old global value for global-local string options
Bram Moolenaard7c96872019-06-15 17:12:48 +02001149 " but the old local value for all other kinds of options.
1150 noa setglobal statusline=bar " Reset global and local value (without triggering autocmd)
1151 noa setlocal statusline=baz
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001152 let g:options = [['statusline', 'bar', 'baz', 'bar', 'foo', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001153 set statusline=foo
1154 call assert_equal([], g:options)
1155 call assert_equal(g:opt[0], g:opt[1])
1156
1157
1158 " 22a: Setting string local (to window) option"
1159 let oldval = &foldignore
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001160 let g:options = [['foldignore', oldval, oldval, oldval, 'fo', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001161 set foldignore=fo
1162 call assert_equal([], g:options)
1163 call assert_equal(g:opt[0], g:opt[1])
1164
1165 " 22b: Resetting string local (to window) option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001166 let g:options = [['foldignore', 'fo', 'fo', 'fo', oldval, 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001167 set foldignore&
1168 call assert_equal([], g:options)
1169 call assert_equal(g:opt[0], g:opt[1])
1170
1171 " 22c: Setting global string local (to window) option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001172 let g:options = [['foldignore', oldval, '', oldval, 'bar', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001173 setglobal foldignore=bar
1174 call assert_equal([], g:options)
1175 call assert_equal(g:opt[0], g:opt[1])
1176
1177 " 22d: Setting local string local (to window) option"
1178 noa set foldignore& " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001179 let g:options = [['foldignore', oldval, oldval, '', 'baz', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001180 setlocal foldignore=baz
1181 call assert_equal([], g:options)
1182 call assert_equal(g:opt[0], g:opt[1])
1183
1184 " 22e: Setting again string local (to window) option"
1185 noa setglobal foldignore=glob " Reset global and local value (without triggering autocmd)
1186 noa setlocal foldignore=loc
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001187 let g:options = [['foldignore', 'loc', 'loc', 'glob', 'fo', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001188 set foldignore=fo
1189 call assert_equal([], g:options)
1190 call assert_equal(g:opt[0], g:opt[1])
1191
1192
zeertzjqb811de52021-10-21 10:50:44 +01001193 " 23a: Setting global number global option"
Bram Moolenaard7c96872019-06-15 17:12:48 +02001194 noa setglobal cmdheight=8 " Reset global and local value (without triggering autocmd)
1195 noa setlocal cmdheight=1 " Sets the global(!) value!
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001196 let g:options = [['cmdheight', '1', '', '1', '2', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001197 setglobal cmdheight=2
1198 call assert_equal([], g:options)
1199 call assert_equal(g:opt[0], g:opt[1])
1200
1201 " 23b: Setting local number global option"
1202 noa setglobal cmdheight=8 " Reset global and local value (without triggering autocmd)
1203 noa setlocal cmdheight=1 " Sets the global(!) value!
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001204 let g:options = [['cmdheight', '1', '1', '', '2', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001205 setlocal cmdheight=2
1206 call assert_equal([], g:options)
1207 call assert_equal(g:opt[0], g:opt[1])
1208
1209 " 23c: Setting again number global option"
1210 noa setglobal cmdheight=8 " Reset global and local value (without triggering autocmd)
1211 noa setlocal cmdheight=1 " Sets the global(!) value!
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001212 let g:options = [['cmdheight', '1', '1', '1', '2', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001213 set cmdheight=2
1214 call assert_equal([], g:options)
1215 call assert_equal(g:opt[0], g:opt[1])
1216
1217 " 23d: Setting again number global option"
1218 noa set cmdheight=8 " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001219 let g:options = [['cmdheight', '8', '8', '8', '2', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001220 set cmdheight=2
1221 call assert_equal([], g:options)
1222 call assert_equal(g:opt[0], g:opt[1])
1223
1224
1225 " 24a: Setting global number global-local (to buffer) option"
1226 noa setglobal undolevels=8 " Reset global and local value (without triggering autocmd)
1227 noa setlocal undolevels=1
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001228 let g:options = [['undolevels', '8', '', '8', '2', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001229 setglobal undolevels=2
1230 call assert_equal([], g:options)
1231 call assert_equal(g:opt[0], g:opt[1])
1232
1233 " 24b: Setting local number global-local (to buffer) option"
1234 noa setglobal undolevels=8 " Reset global and local value (without triggering autocmd)
1235 noa setlocal undolevels=1
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001236 let g:options = [['undolevels', '1', '1', '', '2', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001237 setlocal undolevels=2
1238 call assert_equal([], g:options)
1239 call assert_equal(g:opt[0], g:opt[1])
1240
1241 " 24c: Setting again number global-local (to buffer) option"
1242 noa setglobal undolevels=8 " Reset global and local value (without triggering autocmd)
1243 noa setlocal undolevels=1
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001244 let g:options = [['undolevels', '1', '1', '8', '2', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001245 set undolevels=2
1246 call assert_equal([], g:options)
1247 call assert_equal(g:opt[0], g:opt[1])
1248
1249 " 24d: Setting again global number global-local (to buffer) option"
1250 noa set undolevels=8 " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001251 let g:options = [['undolevels', '8', '8', '8', '2', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001252 set undolevels=2
1253 call assert_equal([], g:options)
1254 call assert_equal(g:opt[0], g:opt[1])
1255
1256
1257 " 25a: Setting global number local (to buffer) option"
1258 noa setglobal wrapmargin=8 " Reset global and local value (without triggering autocmd)
1259 noa setlocal wrapmargin=1
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001260 let g:options = [['wrapmargin', '8', '', '8', '2', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001261 setglobal wrapmargin=2
1262 call assert_equal([], g:options)
1263 call assert_equal(g:opt[0], g:opt[1])
1264
1265 " 25b: Setting local number local (to buffer) option"
1266 noa setglobal wrapmargin=8 " Reset global and local value (without triggering autocmd)
1267 noa setlocal wrapmargin=1
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001268 let g:options = [['wrapmargin', '1', '1', '', '2', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001269 setlocal wrapmargin=2
1270 call assert_equal([], g:options)
1271 call assert_equal(g:opt[0], g:opt[1])
1272
1273 " 25c: Setting again number local (to buffer) option"
1274 noa setglobal wrapmargin=8 " Reset global and local value (without triggering autocmd)
1275 noa setlocal wrapmargin=1
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001276 let g:options = [['wrapmargin', '1', '1', '8', '2', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001277 set wrapmargin=2
1278 call assert_equal([], g:options)
1279 call assert_equal(g:opt[0], g:opt[1])
1280
1281 " 25d: Setting again global number local (to buffer) option"
1282 noa set wrapmargin=8 " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001283 let g:options = [['wrapmargin', '8', '8', '8', '2', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001284 set wrapmargin=2
1285 call assert_equal([], g:options)
1286 call assert_equal(g:opt[0], g:opt[1])
1287
1288
1289 " 26: Setting number global-local (to window) option.
1290 " Such option does currently not exist.
1291
1292
1293 " 27a: Setting global number local (to window) option"
1294 noa setglobal foldcolumn=8 " Reset global and local value (without triggering autocmd)
1295 noa setlocal foldcolumn=1
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001296 let g:options = [['foldcolumn', '8', '', '8', '2', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001297 setglobal foldcolumn=2
1298 call assert_equal([], g:options)
1299 call assert_equal(g:opt[0], g:opt[1])
1300
1301 " 27b: Setting local number local (to window) option"
1302 noa setglobal foldcolumn=8 " Reset global and local value (without triggering autocmd)
1303 noa setlocal foldcolumn=1
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001304 let g:options = [['foldcolumn', '1', '1', '', '2', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001305 setlocal foldcolumn=2
1306 call assert_equal([], g:options)
1307 call assert_equal(g:opt[0], g:opt[1])
1308
1309 " 27c: Setting again number local (to window) option"
1310 noa setglobal foldcolumn=8 " Reset global and local value (without triggering autocmd)
1311 noa setlocal foldcolumn=1
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001312 let g:options = [['foldcolumn', '1', '1', '8', '2', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001313 set foldcolumn=2
1314 call assert_equal([], g:options)
1315 call assert_equal(g:opt[0], g:opt[1])
1316
zeertzjqb811de52021-10-21 10:50:44 +01001317 " 27d: Setting again global number local (to window) option"
Bram Moolenaard7c96872019-06-15 17:12:48 +02001318 noa set foldcolumn=8 " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001319 let g:options = [['foldcolumn', '8', '8', '8', '2', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001320 set foldcolumn=2
1321 call assert_equal([], g:options)
1322 call assert_equal(g:opt[0], g:opt[1])
1323
1324
1325 " 28a: Setting global boolean global option"
1326 noa setglobal nowrapscan " Reset global and local value (without triggering autocmd)
1327 noa setlocal wrapscan " Sets the global(!) value!
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001328 let g:options = [['wrapscan', '1', '', '1', '0', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001329 setglobal nowrapscan
1330 call assert_equal([], g:options)
1331 call assert_equal(g:opt[0], g:opt[1])
1332
1333 " 28b: Setting local boolean global option"
1334 noa setglobal nowrapscan " Reset global and local value (without triggering autocmd)
1335 noa setlocal wrapscan " Sets the global(!) value!
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001336 let g:options = [['wrapscan', '1', '1', '', '0', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001337 setlocal nowrapscan
1338 call assert_equal([], g:options)
1339 call assert_equal(g:opt[0], g:opt[1])
1340
1341 " 28c: Setting again boolean global option"
1342 noa setglobal nowrapscan " Reset global and local value (without triggering autocmd)
1343 noa setlocal wrapscan " Sets the global(!) value!
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001344 let g:options = [['wrapscan', '1', '1', '1', '0', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001345 set nowrapscan
1346 call assert_equal([], g:options)
1347 call assert_equal(g:opt[0], g:opt[1])
1348
1349 " 28d: Setting again global boolean global option"
1350 noa set nowrapscan " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001351 let g:options = [['wrapscan', '0', '0', '0', '1', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001352 set wrapscan
1353 call assert_equal([], g:options)
1354 call assert_equal(g:opt[0], g:opt[1])
1355
1356
1357 " 29a: Setting global boolean global-local (to buffer) option"
1358 noa setglobal noautoread " Reset global and local value (without triggering autocmd)
1359 noa setlocal autoread
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001360 let g:options = [['autoread', '0', '', '0', '1', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001361 setglobal autoread
1362 call assert_equal([], g:options)
1363 call assert_equal(g:opt[0], g:opt[1])
1364
1365 " 29b: Setting local boolean global-local (to buffer) option"
1366 noa setglobal noautoread " Reset global and local value (without triggering autocmd)
1367 noa setlocal autoread
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001368 let g:options = [['autoread', '1', '1', '', '0', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001369 setlocal noautoread
1370 call assert_equal([], g:options)
1371 call assert_equal(g:opt[0], g:opt[1])
1372
1373 " 29c: Setting again boolean global-local (to buffer) option"
1374 noa setglobal noautoread " Reset global and local value (without triggering autocmd)
1375 noa setlocal autoread
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001376 let g:options = [['autoread', '1', '1', '0', '1', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001377 set autoread
1378 call assert_equal([], g:options)
1379 call assert_equal(g:opt[0], g:opt[1])
1380
1381 " 29d: Setting again global boolean global-local (to buffer) option"
1382 noa set noautoread " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001383 let g:options = [['autoread', '0', '0', '0', '1', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001384 set autoread
1385 call assert_equal([], g:options)
1386 call assert_equal(g:opt[0], g:opt[1])
1387
1388
1389 " 30a: Setting global boolean local (to buffer) option"
1390 noa setglobal nocindent " Reset global and local value (without triggering autocmd)
1391 noa setlocal cindent
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001392 let g:options = [['cindent', '0', '', '0', '1', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001393 setglobal cindent
1394 call assert_equal([], g:options)
1395 call assert_equal(g:opt[0], g:opt[1])
1396
1397 " 30b: Setting local boolean local (to buffer) option"
1398 noa setglobal nocindent " Reset global and local value (without triggering autocmd)
1399 noa setlocal cindent
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001400 let g:options = [['cindent', '1', '1', '', '0', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001401 setlocal nocindent
1402 call assert_equal([], g:options)
1403 call assert_equal(g:opt[0], g:opt[1])
1404
1405 " 30c: Setting again boolean local (to buffer) option"
1406 noa setglobal nocindent " Reset global and local value (without triggering autocmd)
1407 noa setlocal cindent
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001408 let g:options = [['cindent', '1', '1', '0', '1', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001409 set cindent
1410 call assert_equal([], g:options)
1411 call assert_equal(g:opt[0], g:opt[1])
1412
1413 " 30d: Setting again global boolean local (to buffer) option"
1414 noa set nocindent " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001415 let g:options = [['cindent', '0', '0', '0', '1', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001416 set cindent
1417 call assert_equal([], g:options)
1418 call assert_equal(g:opt[0], g:opt[1])
1419
1420
1421 " 31: Setting boolean global-local (to window) option
1422 " Currently no such option exists.
1423
1424
1425 " 32a: Setting global boolean local (to window) option"
1426 noa setglobal nocursorcolumn " Reset global and local value (without triggering autocmd)
1427 noa setlocal cursorcolumn
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001428 let g:options = [['cursorcolumn', '0', '', '0', '1', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001429 setglobal cursorcolumn
1430 call assert_equal([], g:options)
1431 call assert_equal(g:opt[0], g:opt[1])
1432
1433 " 32b: Setting local boolean local (to window) option"
1434 noa setglobal nocursorcolumn " Reset global and local value (without triggering autocmd)
1435 noa setlocal cursorcolumn
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001436 let g:options = [['cursorcolumn', '1', '1', '', '0', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001437 setlocal nocursorcolumn
1438 call assert_equal([], g:options)
1439 call assert_equal(g:opt[0], g:opt[1])
1440
1441 " 32c: Setting again boolean local (to window) option"
1442 noa setglobal nocursorcolumn " Reset global and local value (without triggering autocmd)
1443 noa setlocal cursorcolumn
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001444 let g:options = [['cursorcolumn', '1', '1', '0', '1', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001445 set cursorcolumn
1446 call assert_equal([], g:options)
1447 call assert_equal(g:opt[0], g:opt[1])
1448
1449 " 32d: Setting again global boolean local (to window) option"
1450 noa set nocursorcolumn " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001451 let g:options = [['cursorcolumn', '0', '0', '0', '1', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001452 set cursorcolumn
1453 call assert_equal([], g:options)
1454 call assert_equal(g:opt[0], g:opt[1])
1455
1456
Bram Moolenaar1bc353b2019-09-01 14:45:28 +02001457 " 33: Test autocommands when an option value is converted internally.
Bram Moolenaard7c96872019-06-15 17:12:48 +02001458 noa set backspace=1 " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001459 let g:options = [['backspace', 'indent,eol', 'indent,eol', 'indent,eol', '2', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001460 set backspace=2
1461 call assert_equal([], g:options)
1462 call assert_equal(g:opt[0], g:opt[1])
1463
1464
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001465 " Cleanup
1466 au! OptionSet
Bram Moolenaar0331faf2019-06-15 18:40:37 +02001467 " set tags&
Bram Moolenaard7c96872019-06-15 17:12:48 +02001468 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 +02001469 exe printf(":set %s&vim", opt)
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001470 endfor
1471 call test_override('starting', 0)
1472 delfunc! AutoCommandOptionSet
1473endfunc
1474
1475func Test_OptionSet_diffmode()
1476 call test_override('starting', 1)
Bram Moolenaar26d98212019-01-27 22:32:55 +01001477 " 18: Changing an option when entering diff mode
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001478 new
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001479 au OptionSet diff :let &l:cul = v:option_new
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001480
1481 call setline(1, ['buffer 1', 'line2', 'line3', 'line4'])
1482 call assert_equal(0, &l:cul)
1483 diffthis
1484 call assert_equal(1, &l:cul)
1485
1486 vnew
1487 call setline(1, ['buffer 2', 'line 2', 'line 3', 'line4'])
1488 call assert_equal(0, &l:cul)
1489 diffthis
1490 call assert_equal(1, &l:cul)
1491
1492 diffoff
1493 call assert_equal(0, &l:cul)
1494 call assert_equal(1, getwinvar(2, '&l:cul'))
1495 bw!
1496
1497 call assert_equal(1, &l:cul)
1498 diffoff!
1499 call assert_equal(0, &l:cul)
1500 call assert_equal(0, getwinvar(1, '&l:cul'))
1501 bw!
1502
1503 " Cleanup
1504 au! OptionSet
1505 call test_override('starting', 0)
1506endfunc
1507
1508func Test_OptionSet_diffmode_close()
1509 call test_override('starting', 1)
1510 " 19: Try to close the current window when entering diff mode
1511 " should not segfault
1512 new
1513 au OptionSet diff close
1514
1515 call setline(1, ['buffer 1', 'line2', 'line3', 'line4'])
Bram Moolenaare2e40752020-09-04 21:18:46 +02001516 call assert_fails(':diffthis', 'E788:')
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001517 call assert_equal(1, &diff)
1518 vnew
1519 call setline(1, ['buffer 2', 'line 2', 'line 3', 'line4'])
Bram Moolenaare2e40752020-09-04 21:18:46 +02001520 call assert_fails(':diffthis', 'E788:')
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001521 call assert_equal(1, &diff)
Bram Moolenaara9aa86f2019-11-10 21:25:45 +01001522 set diffopt-=closeoff
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001523 bw!
Bram Moolenaare2e40752020-09-04 21:18:46 +02001524 call assert_fails(':diffoff!', 'E788:')
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001525 bw!
1526
1527 " Cleanup
1528 au! OptionSet
1529 call test_override('starting', 0)
1530 "delfunc! AutoCommandOptionSet
1531endfunc
Bram Moolenaar4a137b42017-08-04 22:37:11 +02001532
1533" Test for Bufleave autocommand that deletes the buffer we are about to edit.
1534func Test_BufleaveWithDelete()
Bram Moolenaare7cda972022-08-29 11:02:59 +01001535 new | edit XbufLeave1
Bram Moolenaar4a137b42017-08-04 22:37:11 +02001536
1537 augroup test_bufleavewithdelete
1538 autocmd!
Bram Moolenaare7cda972022-08-29 11:02:59 +01001539 autocmd BufLeave XbufLeave1 bwipe XbufLeave2
Bram Moolenaar4a137b42017-08-04 22:37:11 +02001540 augroup END
1541
Bram Moolenaare7cda972022-08-29 11:02:59 +01001542 call assert_fails('edit XbufLeave2', 'E143:')
1543 call assert_equal('XbufLeave1', bufname('%'))
Bram Moolenaar4a137b42017-08-04 22:37:11 +02001544
Bram Moolenaare7cda972022-08-29 11:02:59 +01001545 autocmd! test_bufleavewithdelete BufLeave XbufLeave1
Bram Moolenaar4a137b42017-08-04 22:37:11 +02001546 augroup! test_bufleavewithdelete
1547
1548 new
Bram Moolenaare7cda972022-08-29 11:02:59 +01001549 bwipe! XbufLeave1
Bram Moolenaar4a137b42017-08-04 22:37:11 +02001550endfunc
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +02001551
1552" Test for autocommand that changes the buffer list, when doing ":ball".
1553func Test_Acmd_BufAll()
1554 enew!
1555 %bwipe!
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01001556 call writefile(['Test file Xxx1'], 'Xxx1', 'D')
1557 call writefile(['Test file Xxx2'], 'Xxx2', 'D')
1558 call writefile(['Test file Xxx3'], 'Xxx3', 'D')
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +02001559
1560 " Add three files to the buffer list
1561 split Xxx1
1562 close
1563 split Xxx2
1564 close
1565 split Xxx3
1566 close
1567
1568 " Wipe the buffer when the buffer is opened
1569 au BufReadPost Xxx2 bwipe
1570
1571 call append(0, 'Test file Xxx4')
1572 ball
1573
1574 call assert_equal(2, winnr('$'))
1575 call assert_equal('Xxx1', bufname(winbufnr(winnr('$'))))
1576 wincmd t
1577
1578 au! BufReadPost
1579 %bwipe!
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +02001580 enew! | only
1581endfunc
1582
1583" Test for autocommand that changes current buffer on BufEnter event.
1584" Check if modelines are interpreted for the correct buffer.
1585func Test_Acmd_BufEnter()
1586 %bwipe!
1587 call writefile(['start of test file Xxx1',
1588 \ "\<Tab>this is a test",
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01001589 \ 'end of test file Xxx1'], 'Xxx1', 'D')
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +02001590 call writefile(['start of test file Xxx2',
1591 \ 'vim: set noai :',
1592 \ "\<Tab>this is a test",
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01001593 \ 'end of test file Xxx2'], 'Xxx2', 'D')
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +02001594
1595 au BufEnter Xxx2 brew
1596 set ai modeline modelines=3
1597 edit Xxx1
1598 " edit Xxx2, autocmd will do :brew
1599 edit Xxx2
1600 exe "normal G?this is a\<CR>"
1601 " Append text with autoindent to this file
1602 normal othis should be auto-indented
1603 call assert_equal("\<Tab>this should be auto-indented", getline('.'))
1604 call assert_equal(3, line('.'))
1605 " Remove autocmd and edit Xxx2 again
1606 au! BufEnter Xxx2
1607 buf! Xxx2
1608 exe "normal G?this is a\<CR>"
1609 " append text without autoindent to Xxx
1610 normal othis should be in column 1
1611 call assert_equal("this should be in column 1", getline('.'))
1612 call assert_equal(4, line('.'))
1613
1614 %bwipe!
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +02001615 set ai&vim modeline&vim modelines&vim
1616endfunc
1617
1618" Test for issue #57
1619" do not move cursor on <c-o> when autoindent is set
1620func Test_ai_CTRL_O()
1621 enew!
1622 set ai
1623 let save_fo = &fo
1624 set fo+=r
1625 exe "normal o# abcdef\<Esc>2hi\<CR>\<C-O>d0\<Esc>"
1626 exe "normal o# abcdef\<Esc>2hi\<C-O>d0\<Esc>"
1627 call assert_equal(['# abc', 'def', 'def'], getline(2, 4))
1628
1629 set ai&vim
1630 let &fo = save_fo
1631 enew!
1632endfunc
1633
1634" Test for autocommand that deletes the current buffer on BufLeave event.
1635" Also test deleting the last buffer, should give a new, empty buffer.
1636func Test_BufLeave_Wipe()
1637 %bwipe!
1638 let content = ['start of test file Xxx',
1639 \ 'this is a test',
1640 \ 'end of test file Xxx']
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01001641 call writefile(content, 'Xxx1', 'D')
1642 call writefile(content, 'Xxx2', 'D')
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +02001643
1644 au BufLeave Xxx2 bwipe
1645 edit Xxx1
1646 split Xxx2
1647 " delete buffer Xxx2, we should be back to Xxx1
1648 bwipe
1649 call assert_equal('Xxx1', bufname('%'))
1650 call assert_equal(1, winnr('$'))
1651
1652 " Create an alternate buffer
1653 %write! test.out
1654 call assert_equal('test.out', bufname('#'))
1655 " delete alternate buffer
1656 bwipe test.out
1657 call assert_equal('Xxx1', bufname('%'))
1658 call assert_equal('', bufname('#'))
1659
1660 au BufLeave Xxx1 bwipe
1661 " delete current buffer, get an empty one
1662 bwipe!
1663 call assert_equal(1, line('$'))
1664 call assert_equal('', bufname('%'))
Bram Moolenaarb2c87502017-10-14 21:15:58 +02001665 let g:bufinfo = getbufinfo()
1666 call assert_equal(1, len(g:bufinfo))
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +02001667
Bram Moolenaar53f0c962017-10-22 14:23:59 +02001668 call delete('test.out')
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +02001669 %bwipe
1670 au! BufLeave
Bram Moolenaarb2c87502017-10-14 21:15:58 +02001671
1672 " check that bufinfo doesn't contain a pointer to freed memory
1673 call test_garbagecollect_now()
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +02001674endfunc
Bram Moolenaar87ffb5c2017-10-19 12:37:42 +02001675
1676func Test_QuitPre()
1677 edit Xfoo
1678 let winid = win_getid(winnr())
1679 split Xbar
1680 au! QuitPre * let g:afile = expand('<afile>')
1681 " Close the other window, <afile> should be correct.
1682 exe win_id2win(winid) . 'q'
1683 call assert_equal('Xfoo', g:afile)
LemonBoy66e13ae2022-04-21 22:52:11 +01001684
Bram Moolenaar87ffb5c2017-10-19 12:37:42 +02001685 unlet g:afile
1686 bwipe Xfoo
1687 bwipe Xbar
1688endfunc
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02001689
1690func Test_Cmdline()
Bram Moolenaar153b7042018-01-31 15:48:32 +01001691 au! CmdlineChanged : let g:text = getcmdline()
1692 let g:text = 0
1693 call feedkeys(":echom 'hello'\<CR>", 'xt')
1694 call assert_equal("echom 'hello'", g:text)
1695 au! CmdlineChanged
1696
1697 au! CmdlineChanged : let g:entered = expand('<afile>')
1698 let g:entered = 0
1699 call feedkeys(":echom 'hello'\<CR>", 'xt')
1700 call assert_equal(':', g:entered)
1701 au! CmdlineChanged
1702
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02001703 au! CmdlineEnter : let g:entered = expand('<afile>')
1704 au! CmdlineLeave : let g:left = expand('<afile>')
1705 let g:entered = 0
1706 let g:left = 0
1707 call feedkeys(":echo 'hello'\<CR>", 'xt')
1708 call assert_equal(':', g:entered)
1709 call assert_equal(':', g:left)
1710 au! CmdlineEnter
1711 au! CmdlineLeave
1712
Bram Moolenaara4baf5b2018-04-22 13:27:44 +02001713 let save_shellslash = &shellslash
1714 set noshellslash
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02001715 au! CmdlineEnter / let g:entered = expand('<afile>')
1716 au! CmdlineLeave / let g:left = expand('<afile>')
1717 let g:entered = 0
1718 let g:left = 0
Bram Moolenaar53f0c962017-10-22 14:23:59 +02001719 new
1720 call setline(1, 'hello')
1721 call feedkeys("/hello\<CR>", 'xt')
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02001722 call assert_equal('/', g:entered)
1723 call assert_equal('/', g:left)
Bram Moolenaar53f0c962017-10-22 14:23:59 +02001724 bwipe!
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02001725 au! CmdlineEnter
1726 au! CmdlineLeave
Bram Moolenaara4baf5b2018-04-22 13:27:44 +02001727 let &shellslash = save_shellslash
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02001728endfunc
Bram Moolenaar53f0c962017-10-22 14:23:59 +02001729
1730" Test for BufWritePre autocommand that deletes or unloads the buffer.
1731func Test_BufWritePre()
1732 %bwipe
1733 au BufWritePre Xxx1 bunload
1734 au BufWritePre Xxx2 bwipe
1735
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01001736 call writefile(['start of Xxx1', 'test', 'end of Xxx1'], 'Xxx1', 'D')
1737 call writefile(['start of Xxx2', 'test', 'end of Xxx2'], 'Xxx2', 'D')
Bram Moolenaar53f0c962017-10-22 14:23:59 +02001738
1739 edit Xtest
1740 e! Xxx2
1741 bdel Xtest
1742 e Xxx1
1743 " write it, will unload it and give an error msg
Bram Moolenaare2e40752020-09-04 21:18:46 +02001744 call assert_fails('w', 'E203:')
Bram Moolenaar53f0c962017-10-22 14:23:59 +02001745 call assert_equal('Xxx2', bufname('%'))
1746 edit Xtest
1747 e! Xxx2
1748 bwipe Xtest
1749 " write it, will delete the buffer and give an error msg
Bram Moolenaare2e40752020-09-04 21:18:46 +02001750 call assert_fails('w', 'E203:')
Bram Moolenaar53f0c962017-10-22 14:23:59 +02001751 call assert_equal('Xxx1', bufname('%'))
1752 au! BufWritePre
Bram Moolenaar53f0c962017-10-22 14:23:59 +02001753endfunc
1754
1755" Test for BufUnload autocommand that unloads all the other buffers
1756func Test_bufunload_all()
Bram Moolenaarf08b0eb2021-10-16 13:00:14 +01001757 let g:test_is_flaky = 1
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01001758 call writefile(['Test file Xxx1'], 'Xxx1', 'D')"
1759 call writefile(['Test file Xxx2'], 'Xxx2', 'D')"
Bram Moolenaar53f0c962017-10-22 14:23:59 +02001760
Bram Moolenaarc79745a2019-05-20 22:12:34 +02001761 let content =<< trim [CODE]
1762 func UnloadAllBufs()
1763 let i = 1
1764 while i <= bufnr('$')
1765 if i != bufnr('%') && bufloaded(i)
1766 exe i . 'bunload'
1767 endif
1768 let i += 1
1769 endwhile
1770 endfunc
1771 au BufUnload * call UnloadAllBufs()
1772 au VimLeave * call writefile(['Test Finished'], 'Xout')
1773 edit Xxx1
1774 split Xxx2
1775 q
1776 [CODE]
1777
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01001778 call writefile(content, 'Xbunloadtest', 'D')
Bram Moolenaar53f0c962017-10-22 14:23:59 +02001779
1780 call delete('Xout')
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01001781 call system(GetVimCommandClean() .. ' -N --not-a-term -S Xbunloadtest')
Bram Moolenaar53f0c962017-10-22 14:23:59 +02001782 call assert_true(filereadable('Xout'))
1783
Bram Moolenaar53f0c962017-10-22 14:23:59 +02001784 call delete('Xout')
1785endfunc
1786
1787" Some tests for buffer-local autocommands
1788func Test_buflocal_autocmd()
1789 let g:bname = ''
1790 edit xx
1791 au BufLeave <buffer> let g:bname = expand("%")
1792 " here, autocommand for xx should trigger.
1793 " but autocommand shall not apply to buffer named <buffer>.
1794 edit somefile
1795 call assert_equal('xx', g:bname)
1796 let g:bname = ''
1797 " here, autocommand shall be auto-deleted
1798 bwipe xx
1799 " autocmd should not trigger
1800 edit xx
1801 call assert_equal('', g:bname)
1802 " autocmd should not trigger
1803 edit somefile
1804 call assert_equal('', g:bname)
1805 enew
1806 unlet g:bname
1807endfunc
Bram Moolenaar430dc5d2017-11-02 21:04:47 +01001808
1809" Test for "*Cmd" autocommands
1810func Test_Cmd_Autocmds()
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01001811 call writefile(['start of Xxx', "\tabc2", 'end of Xxx'], 'Xxx', 'D')
Bram Moolenaar430dc5d2017-11-02 21:04:47 +01001812
1813 enew!
1814 au BufReadCmd XtestA 0r Xxx|$del
1815 edit XtestA " will read text of Xxd instead
1816 call assert_equal('start of Xxx', getline(1))
1817
1818 au BufWriteCmd XtestA call append(line("$"), "write")
1819 write " will append a line to the file
1820 call assert_equal('write', getline('$'))
Bram Moolenaare2e40752020-09-04 21:18:46 +02001821 call assert_fails('read XtestA', 'E484:') " should not read anything
Bram Moolenaar430dc5d2017-11-02 21:04:47 +01001822 call assert_equal('write', getline(4))
1823
1824 " now we have:
1825 " 1 start of Xxx
1826 " 2 abc2
1827 " 3 end of Xxx
1828 " 4 write
1829
1830 au FileReadCmd XtestB '[r Xxx
1831 2r XtestB " will read Xxx below line 2 instead
1832 call assert_equal('start of Xxx', getline(3))
1833
1834 " now we have:
1835 " 1 start of Xxx
1836 " 2 abc2
1837 " 3 start of Xxx
1838 " 4 abc2
1839 " 5 end of Xxx
1840 " 6 end of Xxx
1841 " 7 write
1842
1843 au FileWriteCmd XtestC '[,']copy $
1844 normal 4GA1
1845 4,5w XtestC " will copy lines 4 and 5 to the end
1846 call assert_equal("\tabc21", getline(8))
Bram Moolenaare2e40752020-09-04 21:18:46 +02001847 call assert_fails('r XtestC', 'E484:') " should not read anything
Bram Moolenaar430dc5d2017-11-02 21:04:47 +01001848 call assert_equal("end of Xxx", getline(9))
1849
1850 " now we have:
1851 " 1 start of Xxx
1852 " 2 abc2
1853 " 3 start of Xxx
1854 " 4 abc21
1855 " 5 end of Xxx
1856 " 6 end of Xxx
1857 " 7 write
1858 " 8 abc21
1859 " 9 end of Xxx
1860
1861 let g:lines = []
1862 au FileAppendCmd XtestD call extend(g:lines, getline(line("'["), line("']")))
1863 w >>XtestD " will add lines to 'lines'
1864 call assert_equal(9, len(g:lines))
Bram Moolenaare2e40752020-09-04 21:18:46 +02001865 call assert_fails('$r XtestD', 'E484:') " should not read anything
Bram Moolenaar430dc5d2017-11-02 21:04:47 +01001866 call assert_equal(9, line('$'))
1867 call assert_equal('end of Xxx', getline('$'))
1868
1869 au BufReadCmd XtestE 0r Xxx|$del
1870 sp XtestE " split window with test.out
1871 call assert_equal('end of Xxx', getline(3))
1872
1873 let g:lines = []
1874 exe "normal 2Goasdf\<Esc>\<C-W>\<C-W>"
1875 au BufWriteCmd XtestE call extend(g:lines, getline(0, '$'))
1876 wall " will write other window to 'lines'
1877 call assert_equal(4, len(g:lines), g:lines)
1878 call assert_equal('asdf', g:lines[2])
1879
1880 au! BufReadCmd
1881 au! BufWriteCmd
1882 au! FileReadCmd
1883 au! FileWriteCmd
1884 au! FileAppendCmd
1885 %bwipe!
Bram Moolenaar430dc5d2017-11-02 21:04:47 +01001886 enew!
1887endfunc
Bram Moolenaaraace2152017-11-05 16:23:10 +01001888
Bram Moolenaar0fff4412020-03-29 16:06:29 +02001889func s:ReadFile()
1890 setl noswapfile nomodified
1891 let filename = resolve(expand("<afile>:p"))
1892 execute 'read' fnameescape(filename)
1893 1d_
1894 exe 'file' fnameescape(filename)
1895 setl buftype=acwrite
1896endfunc
1897
1898func s:WriteFile()
1899 let filename = resolve(expand("<afile>:p"))
1900 setl buftype=
1901 noautocmd execute 'write' fnameescape(filename)
1902 setl buftype=acwrite
1903 setl nomodified
1904endfunc
1905
1906func Test_BufReadCmd()
1907 autocmd BufReadCmd *.test call s:ReadFile()
1908 autocmd BufWriteCmd *.test call s:WriteFile()
1909
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01001910 call writefile(['one', 'two', 'three'], 'Xcmd.test', 'D')
Bram Moolenaar0fff4412020-03-29 16:06:29 +02001911 edit Xcmd.test
1912 call assert_match('Xcmd.test" line 1 of 3', execute('file'))
1913 normal! Gofour
1914 write
1915 call assert_equal(['one', 'two', 'three', 'four'], readfile('Xcmd.test'))
1916
1917 bwipe!
Bram Moolenaar0fff4412020-03-29 16:06:29 +02001918 au! BufReadCmd
1919 au! BufWriteCmd
1920endfunc
1921
zeertzjq9c8f9462022-08-30 18:17:15 +01001922func Test_BufWriteCmd()
1923 autocmd BufWriteCmd Xbufwritecmd let g:written = 1
1924 new
1925 file Xbufwritecmd
1926 set buftype=acwrite
Bram Moolenaar6f14da12022-09-07 21:30:44 +01001927 call mkdir('Xbufwritecmd', 'D')
zeertzjq9c8f9462022-08-30 18:17:15 +01001928 write
1929 " BufWriteCmd should be triggered even if a directory has the same name
1930 call assert_equal(1, g:written)
zeertzjq9c8f9462022-08-30 18:17:15 +01001931 unlet g:written
1932 au! BufWriteCmd
1933 bwipe!
1934endfunc
1935
Bram Moolenaaraace2152017-11-05 16:23:10 +01001936func SetChangeMarks(start, end)
Bram Moolenaar97c69432021-01-15 16:45:21 +01001937 exe a:start .. 'mark ['
1938 exe a:end .. 'mark ]'
Bram Moolenaaraace2152017-11-05 16:23:10 +01001939endfunc
1940
1941" Verify the effects of autocmds on '[ and ']
1942func Test_change_mark_in_autocmds()
1943 edit! Xtest
Bram Moolenaar97c69432021-01-15 16:45:21 +01001944 call feedkeys("ia\<CR>b\<CR>c\<CR>d\<C-g>u\<Esc>", 'xtn')
Bram Moolenaaraace2152017-11-05 16:23:10 +01001945
1946 call SetChangeMarks(2, 3)
1947 write
1948 call assert_equal([1, 4], [line("'["), line("']")])
1949
1950 call SetChangeMarks(2, 3)
1951 au BufWritePre * call assert_equal([1, 4], [line("'["), line("']")])
1952 write
1953 au! BufWritePre
1954
Bram Moolenaar14ddd222020-08-05 12:02:40 +02001955 if has('unix')
Bram Moolenaaraace2152017-11-05 16:23:10 +01001956 write XtestFilter
1957 write >> XtestFilter
1958
1959 call SetChangeMarks(2, 3)
1960 " Marks are set to the entire range of the write
1961 au FilterWritePre * call assert_equal([1, 4], [line("'["), line("']")])
1962 " '[ is adjusted to just before the line that will receive the filtered
1963 " data
1964 au FilterReadPre * call assert_equal([4, 4], [line("'["), line("']")])
1965 " The filtered data is read into the buffer, and the source lines are
1966 " still present, so the range is after the source lines
1967 au FilterReadPost * call assert_equal([5, 12], [line("'["), line("']")])
1968 %!cat XtestFilter
1969 " After the filtered data is read, the original lines are deleted
1970 call assert_equal([1, 8], [line("'["), line("']")])
1971 au! FilterWritePre,FilterReadPre,FilterReadPost
1972 undo
1973
1974 call SetChangeMarks(1, 4)
1975 au FilterWritePre * call assert_equal([2, 3], [line("'["), line("']")])
1976 au FilterReadPre * call assert_equal([3, 3], [line("'["), line("']")])
1977 au FilterReadPost * call assert_equal([4, 11], [line("'["), line("']")])
1978 2,3!cat XtestFilter
1979 call assert_equal([2, 9], [line("'["), line("']")])
1980 au! FilterWritePre,FilterReadPre,FilterReadPost
1981 undo
1982
1983 call delete('XtestFilter')
1984 endif
1985
1986 call SetChangeMarks(1, 4)
1987 au FileWritePre * call assert_equal([2, 3], [line("'["), line("']")])
1988 2,3write Xtest2
1989 au! FileWritePre
1990
1991 call SetChangeMarks(2, 3)
1992 au FileAppendPre * call assert_equal([1, 4], [line("'["), line("']")])
1993 write >> Xtest2
1994 au! FileAppendPre
1995
1996 call SetChangeMarks(1, 4)
1997 au FileAppendPre * call assert_equal([2, 3], [line("'["), line("']")])
1998 2,3write >> Xtest2
1999 au! FileAppendPre
2000
2001 call SetChangeMarks(1, 1)
2002 au FileReadPre * call assert_equal([3, 1], [line("'["), line("']")])
2003 au FileReadPost * call assert_equal([4, 11], [line("'["), line("']")])
2004 3read Xtest2
2005 au! FileReadPre,FileReadPost
2006 undo
2007
2008 call SetChangeMarks(4, 4)
2009 " When the line is 0, it's adjusted to 1
2010 au FileReadPre * call assert_equal([1, 4], [line("'["), line("']")])
2011 au FileReadPost * call assert_equal([1, 8], [line("'["), line("']")])
2012 0read Xtest2
2013 au! FileReadPre,FileReadPost
2014 undo
2015
2016 call SetChangeMarks(4, 4)
2017 " When the line is 0, it's adjusted to 1
2018 au FileReadPre * call assert_equal([1, 4], [line("'["), line("']")])
2019 au FileReadPost * call assert_equal([2, 9], [line("'["), line("']")])
2020 1read Xtest2
2021 au! FileReadPre,FileReadPost
2022 undo
2023
2024 bwipe!
2025 call delete('Xtest')
2026 call delete('Xtest2')
2027endfunc
2028
2029func Test_Filter_noshelltemp()
Bram Moolenaaraeb313f2020-11-27 19:13:28 +01002030 CheckExecutable cat
Bram Moolenaaraace2152017-11-05 16:23:10 +01002031
2032 enew!
2033 call setline(1, ['a', 'b', 'c', 'd'])
2034
2035 let shelltemp = &shelltemp
2036 set shelltemp
2037
2038 let g:filter_au = 0
2039 au FilterWritePre * let g:filter_au += 1
2040 au FilterReadPre * let g:filter_au += 1
2041 au FilterReadPost * let g:filter_au += 1
2042 %!cat
2043 call assert_equal(3, g:filter_au)
2044
2045 if has('filterpipe')
2046 set noshelltemp
2047
2048 let g:filter_au = 0
2049 au FilterWritePre * let g:filter_au += 1
2050 au FilterReadPre * let g:filter_au += 1
2051 au FilterReadPost * let g:filter_au += 1
2052 %!cat
2053 call assert_equal(0, g:filter_au)
2054 endif
2055
2056 au! FilterWritePre,FilterReadPre,FilterReadPost
2057 let &shelltemp = shelltemp
2058 bwipe!
2059endfunc
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01002060
2061func Test_TextYankPost()
2062 enew!
2063 call setline(1, ['foo'])
2064
2065 let g:event = []
2066 au TextYankPost * let g:event = copy(v:event)
2067
2068 call assert_equal({}, v:event)
2069 call assert_fails('let v:event = {}', 'E46:')
2070 call assert_fails('let v:event.mykey = 0', 'E742:')
2071
2072 norm "ayiw
2073 call assert_equal(
Bram Moolenaara016eeb2022-04-09 11:37:38 +01002074 \ #{regcontents: ['foo'], regname: 'a', operator: 'y',
2075 \ regtype: 'v', visual: v:false, inclusive: v:true},
2076 \ g:event)
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01002077 norm y_
2078 call assert_equal(
Bram Moolenaara016eeb2022-04-09 11:37:38 +01002079 \ #{regcontents: ['foo'], regname: '', operator: 'y', regtype: 'V',
2080 \ visual: v:false, inclusive: v:false},
2081 \ g:event)
Bram Moolenaar37d16732020-06-12 22:09:01 +02002082 norm Vy
2083 call assert_equal(
Bram Moolenaara016eeb2022-04-09 11:37:38 +01002084 \ #{regcontents: ['foo'], regname: '', operator: 'y', regtype: 'V',
2085 \ visual: v:true, inclusive: v:true},
2086 \ g:event)
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01002087 call feedkeys("\<C-V>y", 'x')
2088 call assert_equal(
Bram Moolenaara016eeb2022-04-09 11:37:38 +01002089 \ #{regcontents: ['f'], regname: '', operator: 'y', regtype: "\x161",
2090 \ visual: v:true, inclusive: v:true},
2091 \ g:event)
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01002092 norm "xciwbar
2093 call assert_equal(
Bram Moolenaara016eeb2022-04-09 11:37:38 +01002094 \ #{regcontents: ['foo'], regname: 'x', operator: 'c', regtype: 'v',
2095 \ visual: v:false, inclusive: v:true},
2096 \ g:event)
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01002097 norm "bdiw
2098 call assert_equal(
Bram Moolenaara016eeb2022-04-09 11:37:38 +01002099 \ #{regcontents: ['bar'], regname: 'b', operator: 'd', regtype: 'v',
2100 \ visual: v:false, inclusive: v:true},
2101 \ g:event)
2102
2103 call setline(1, 'foobar')
2104 " exclusive motion
2105 norm $"ay0
2106 call assert_equal(
2107 \ #{regcontents: ['fooba'], regname: 'a', operator: 'y', regtype: 'v',
2108 \ visual: v:false, inclusive: v:false},
2109 \ g:event)
2110 " inclusive motion
2111 norm 0"ay$
2112 call assert_equal(
2113 \ #{regcontents: ['foobar'], regname: 'a', operator: 'y', regtype: 'v',
2114 \ visual: v:false, inclusive: v:true},
2115 \ g:event)
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01002116
2117 call assert_equal({}, v:event)
2118
Bram Moolenaarfccbf062020-11-26 20:34:00 +01002119 if has('clipboard_working') && !has('gui_running')
2120 " Test that when the visual selection is automatically copied to clipboard
2121 " register a TextYankPost is emitted
2122 call setline(1, ['foobar'])
2123
2124 let @* = ''
2125 set clipboard=autoselect
2126 exe "norm! ggviw\<Esc>"
2127 call assert_equal(
Bram Moolenaara016eeb2022-04-09 11:37:38 +01002128 \ #{regcontents: ['foobar'], regname: '*', operator: 'y',
2129 \ regtype: 'v', visual: v:true, inclusive: v:false},
2130 \ g:event)
Bram Moolenaarfccbf062020-11-26 20:34:00 +01002131
2132 let @+ = ''
2133 set clipboard=autoselectplus
2134 exe "norm! ggviw\<Esc>"
2135 call assert_equal(
Bram Moolenaara016eeb2022-04-09 11:37:38 +01002136 \ #{regcontents: ['foobar'], regname: '+', operator: 'y',
2137 \ regtype: 'v', visual: v:true, inclusive: v:false},
2138 \ g:event)
Bram Moolenaarfccbf062020-11-26 20:34:00 +01002139
2140 set clipboard&vim
2141 endif
2142
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01002143 au! TextYankPost
2144 unlet g:event
2145 bwipe!
2146endfunc
Bram Moolenaar9bca8052017-12-18 12:37:55 +01002147
Bram Moolenaar9a046fd2021-01-28 13:47:59 +01002148func Test_autocommand_all_events()
2149 call assert_fails('au * * bwipe', 'E1155:')
2150 call assert_fails('au * x bwipe', 'E1155:')
Bram Moolenaarb6db1462021-12-24 19:24:47 +00002151 call assert_fails('au! * x bwipe', 'E1155:')
Bram Moolenaar4fb921e2017-12-18 15:33:00 +01002152endfunc
Bram Moolenaarb7407d32018-02-03 17:36:27 +01002153
Bram Moolenaarf6246f52022-02-11 16:30:12 +00002154func Test_autocmd_user()
2155 au User MyEvent let s:res = [expand("<afile>"), expand("<amatch>")]
2156 doautocmd User MyEvent
2157 call assert_equal(['MyEvent', 'MyEvent'], s:res)
2158 au! User
2159 unlet s:res
2160endfunc
2161
Bram Moolenaarb7407d32018-02-03 17:36:27 +01002162function s:Before_test_dirchanged()
2163 augroup test_dirchanged
2164 autocmd!
2165 augroup END
2166 let s:li = []
2167 let s:dir_this = getcwd()
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02002168 let s:dir_foo = s:dir_this . '/Xfoo'
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01002169 call mkdir(s:dir_foo)
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02002170 let s:dir_bar = s:dir_this . '/Xbar'
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01002171 call mkdir(s:dir_bar)
Bram Moolenaarb7407d32018-02-03 17:36:27 +01002172endfunc
2173
2174function s:After_test_dirchanged()
Bram Moolenaar3503d7c2019-11-09 20:10:17 +01002175 call chdir(s:dir_this)
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01002176 call delete(s:dir_foo, 'd')
2177 call delete(s:dir_bar, 'd')
Bram Moolenaarb7407d32018-02-03 17:36:27 +01002178 augroup test_dirchanged
2179 autocmd!
2180 augroup END
2181endfunc
2182
2183function Test_dirchanged_global()
2184 call s:Before_test_dirchanged()
Bram Moolenaarf6246f52022-02-11 16:30:12 +00002185 autocmd test_dirchanged DirChangedPre global call add(s:li, expand("<amatch>") .. " pre cd " .. v:event.directory)
Bram Moolenaarb7407d32018-02-03 17:36:27 +01002186 autocmd test_dirchanged DirChanged global call add(s:li, "cd:")
2187 autocmd test_dirchanged DirChanged global call add(s:li, expand("<afile>"))
Bram Moolenaar3503d7c2019-11-09 20:10:17 +01002188 call chdir(s:dir_foo)
Bram Moolenaarf6246f52022-02-11 16:30:12 +00002189 let expected = ["global pre cd " .. s:dir_foo, "cd:", s:dir_foo]
Bram Moolenaar28e8f732022-02-09 12:58:20 +00002190 call assert_equal(expected, s:li)
Bram Moolenaar3503d7c2019-11-09 20:10:17 +01002191 call chdir(s:dir_foo)
Bram Moolenaar28e8f732022-02-09 12:58:20 +00002192 call assert_equal(expected, s:li)
Bram Moolenaar3503d7c2019-11-09 20:10:17 +01002193 exe 'lcd ' .. fnameescape(s:dir_bar)
Bram Moolenaar28e8f732022-02-09 12:58:20 +00002194 call assert_equal(expected, s:li)
Bram Moolenaard8c9d322022-06-12 11:49:16 +01002195
2196 exe 'cd ' .. s:dir_foo
2197 exe 'cd ' .. s:dir_bar
2198 autocmd! test_dirchanged DirChanged global let g:result = expand("<afile>")
2199 cd -
Bram Moolenaardb77c492022-06-12 23:26:50 +01002200 call assert_equal(s:dir_foo, substitute(g:result, '\\', '/', 'g'))
Bram Moolenaard8c9d322022-06-12 11:49:16 +01002201
Bram Moolenaarb7407d32018-02-03 17:36:27 +01002202 call s:After_test_dirchanged()
2203endfunc
2204
2205function Test_dirchanged_local()
2206 call s:Before_test_dirchanged()
2207 autocmd test_dirchanged DirChanged window call add(s:li, "lcd:")
2208 autocmd test_dirchanged DirChanged window call add(s:li, expand("<afile>"))
Bram Moolenaar3503d7c2019-11-09 20:10:17 +01002209 call chdir(s:dir_foo)
Bram Moolenaarb7407d32018-02-03 17:36:27 +01002210 call assert_equal([], s:li)
Bram Moolenaar3503d7c2019-11-09 20:10:17 +01002211 exe 'lcd ' .. fnameescape(s:dir_bar)
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01002212 call assert_equal(["lcd:", s:dir_bar], s:li)
Bram Moolenaar3503d7c2019-11-09 20:10:17 +01002213 exe 'lcd ' .. fnameescape(s:dir_bar)
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01002214 call assert_equal(["lcd:", s:dir_bar], s:li)
Bram Moolenaarb7407d32018-02-03 17:36:27 +01002215 call s:After_test_dirchanged()
2216endfunc
2217
2218function Test_dirchanged_auto()
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +02002219 CheckOption autochdir
Bram Moolenaarb7407d32018-02-03 17:36:27 +01002220 call s:Before_test_dirchanged()
2221 call test_autochdir()
Bram Moolenaar28e8f732022-02-09 12:58:20 +00002222 autocmd test_dirchanged DirChangedPre auto call add(s:li, "pre cd " .. v:event.directory)
Bram Moolenaarb7407d32018-02-03 17:36:27 +01002223 autocmd test_dirchanged DirChanged auto call add(s:li, "auto:")
2224 autocmd test_dirchanged DirChanged auto call add(s:li, expand("<afile>"))
2225 set acd
Bram Moolenaar3503d7c2019-11-09 20:10:17 +01002226 cd ..
Bram Moolenaarb7407d32018-02-03 17:36:27 +01002227 call assert_equal([], s:li)
Bram Moolenaar61abe7d2022-08-30 21:46:08 +01002228 exe 'edit ' . s:dir_foo . '/Xautofile'
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01002229 call assert_equal(s:dir_foo, getcwd())
Bram Moolenaar28e8f732022-02-09 12:58:20 +00002230 let expected = ["pre cd " .. s:dir_foo, "auto:", s:dir_foo]
2231 call assert_equal(expected, s:li)
Bram Moolenaarb7407d32018-02-03 17:36:27 +01002232 set noacd
2233 bwipe!
2234 call s:After_test_dirchanged()
2235endfunc
Bram Moolenaar5a093432018-02-10 18:15:19 +01002236
2237" Test TextChangedI and TextChangedP
2238func Test_ChangedP()
2239 new
2240 call setline(1, ['foo', 'bar', 'foobar'])
2241 call test_override("char_avail", 1)
2242 set complete=. completeopt=menuone
2243
2244 func! TextChangedAutocmd(char)
2245 let g:autocmd .= a:char
2246 endfunc
2247
Christian Brabandtdb3b4462021-10-16 11:58:55 +01002248 " TextChanged will not be triggered, only check that it isn't.
Bram Moolenaar5a093432018-02-10 18:15:19 +01002249 au! TextChanged <buffer> :call TextChangedAutocmd('N')
2250 au! TextChangedI <buffer> :call TextChangedAutocmd('I')
2251 au! TextChangedP <buffer> :call TextChangedAutocmd('P')
2252
2253 call cursor(3, 1)
2254 let g:autocmd = ''
2255 call feedkeys("o\<esc>", 'tnix')
2256 call assert_equal('I', g:autocmd)
2257
2258 let g:autocmd = ''
2259 call feedkeys("Sf", 'tnix')
2260 call assert_equal('II', g:autocmd)
2261
2262 let g:autocmd = ''
2263 call feedkeys("Sf\<C-N>", 'tnix')
2264 call assert_equal('IIP', g:autocmd)
2265
2266 let g:autocmd = ''
2267 call feedkeys("Sf\<C-N>\<C-N>", 'tnix')
2268 call assert_equal('IIPP', g:autocmd)
2269
2270 let g:autocmd = ''
2271 call feedkeys("Sf\<C-N>\<C-N>\<C-N>", 'tnix')
2272 call assert_equal('IIPPP', g:autocmd)
2273
2274 let g:autocmd = ''
2275 call feedkeys("Sf\<C-N>\<C-N>\<C-N>\<C-N>", 'tnix')
2276 call assert_equal('IIPPPP', g:autocmd)
2277
2278 call assert_equal(['foo', 'bar', 'foobar', 'foo'], getline(1, '$'))
2279 " TODO: how should it handle completeopt=noinsert,noselect?
2280
2281 " CleanUp
2282 call test_override("char_avail", 0)
2283 au! TextChanged
2284 au! TextChangedI
2285 au! TextChangedP
2286 delfu TextChangedAutocmd
2287 unlet! g:autocmd
2288 set complete&vim completeopt&vim
2289
2290 bw!
2291endfunc
Bram Moolenaar8c64a362018-03-23 22:39:31 +01002292
Bram Moolenaar91d2e782018-08-07 19:05:01 +02002293let g:setline_handled = v:false
Bram Moolenaar1e115362019-01-09 23:01:02 +01002294func SetLineOne()
Bram Moolenaar91d2e782018-08-07 19:05:01 +02002295 if !g:setline_handled
2296 call setline(1, "(x)")
2297 let g:setline_handled = v:true
2298 endif
2299endfunc
2300
2301func Test_TextChangedI_with_setline()
2302 new
2303 call test_override('char_avail', 1)
2304 autocmd TextChangedI <buffer> call SetLineOne()
2305 call feedkeys("i(\<CR>\<Esc>", 'tx')
2306 call assert_equal('(', getline(1))
2307 call assert_equal('x)', getline(2))
2308 undo
Bram Moolenaar91d2e782018-08-07 19:05:01 +02002309 call assert_equal('', getline(1))
Bram Moolenaar9fa95062018-08-08 22:08:32 +02002310 call assert_equal('', getline(2))
Bram Moolenaar91d2e782018-08-07 19:05:01 +02002311
Bram Moolenaarca34db32022-01-20 11:17:18 +00002312 call test_override('char_avail', 0)
Bram Moolenaar91d2e782018-08-07 19:05:01 +02002313 bwipe!
2314endfunc
2315
Bram Moolenaar8c64a362018-03-23 22:39:31 +01002316func Test_Changed_FirstTime()
Bram Moolenaar8c5a2782019-08-07 23:07:07 +02002317 CheckFeature terminal
2318 CheckNotGui
Bram Moolenaar3cdcb092020-03-18 19:18:10 +01002319 " Starting a terminal to run Vim is always considered flaky.
Bram Moolenaar30d53e22020-03-18 21:10:44 +01002320 let g:test_is_flaky = 1
Bram Moolenaar8c5a2782019-08-07 23:07:07 +02002321
Bram Moolenaar8c64a362018-03-23 22:39:31 +01002322 " Prepare file for TextChanged event.
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01002323 call writefile([''], 'Xchanged.txt', 'D')
Bram Moolenaar8c64a362018-03-23 22:39:31 +01002324 let buf = term_start([GetVimProg(), '--clean', '-c', 'set noswapfile'], {'term_rows': 3})
2325 call assert_equal('running', term_getstatus(buf))
Bram Moolenaar1834d372018-03-29 17:40:46 +02002326 " Wait for the ruler (in the status line) to be shown.
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01002327 " In ConPTY, there is additional character which is drawn up to the width of
2328 " the screen.
2329 if has('conpty')
2330 call WaitForAssert({-> assert_match('\<All.*$', term_getline(buf, 3))})
2331 else
2332 call WaitForAssert({-> assert_match('\<All$', term_getline(buf, 3))})
2333 endif
Bram Moolenaar8c64a362018-03-23 22:39:31 +01002334 " It's only adding autocmd, so that no event occurs.
2335 call term_sendkeys(buf, ":au! TextChanged <buffer> call writefile(['No'], 'Xchanged.txt')\<cr>")
2336 call term_sendkeys(buf, "\<C-\\>\<C-N>:qa!\<cr>")
Bram Moolenaar50182fa2018-04-28 21:34:40 +02002337 call WaitForAssert({-> assert_equal('finished', term_getstatus(buf))})
Bram Moolenaar8c64a362018-03-23 22:39:31 +01002338 call assert_equal([''], readfile('Xchanged.txt'))
2339
2340 " clean up
Bram Moolenaar8c64a362018-03-23 22:39:31 +01002341 bwipe!
2342endfunc
Bram Moolenaar0566e892019-01-24 19:37:40 +01002343
Bram Moolenaareb93f3f2019-04-04 15:04:56 +02002344func Test_autocmd_nested()
2345 let g:did_nested = 0
2346 augroup Testing
2347 au WinNew * edit somefile
2348 au BufNew * let g:did_nested = 1
2349 augroup END
2350 split
2351 call assert_equal(0, g:did_nested)
2352 close
2353 bwipe! somefile
2354
2355 " old nested argument still works
2356 augroup Testing
2357 au!
2358 au WinNew * nested edit somefile
2359 au BufNew * let g:did_nested = 1
2360 augroup END
2361 split
2362 call assert_equal(1, g:did_nested)
2363 close
2364 bwipe! somefile
2365
2366 " New ++nested argument works
2367 augroup Testing
2368 au!
2369 au WinNew * ++nested edit somefile
2370 au BufNew * let g:did_nested = 1
2371 augroup END
2372 split
2373 call assert_equal(1, g:did_nested)
2374 close
2375 bwipe! somefile
2376
Bram Moolenaarf0775142022-03-04 20:10:38 +00002377 " nested without ++ does not work in Vim9 script
2378 call assert_fails('vim9cmd au WinNew * nested echo fails', 'E1078:')
2379
Bram Moolenaareb93f3f2019-04-04 15:04:56 +02002380 augroup Testing
2381 au!
2382 augroup END
2383
2384 call assert_fails('au WinNew * ++nested ++nested echo bad', 'E983:')
2385 call assert_fails('au WinNew * nested nested echo bad', 'E983:')
2386endfunc
2387
Bram Moolenaar5fa9f232022-07-23 09:06:48 +01002388func Test_autocmd_nested_cursor_invalid()
2389 set laststatus=0
2390 copen
2391 cclose
2392 call setline(1, ['foo', 'bar', 'baz'])
2393 3
2394 augroup nested_inv
2395 autocmd User foo ++nested copen
2396 autocmd BufAdd * let &laststatus = 2 - &laststatus
2397 augroup END
2398 doautocmd User foo
2399
2400 augroup nested_inv
2401 au!
2402 augroup END
2403 set laststatus&
Bram Moolenaarb03950f2022-07-26 13:47:13 +01002404 cclose
Bram Moolenaar5fa9f232022-07-23 09:06:48 +01002405 bwipe!
2406endfunc
2407
Bram Moolenaar3d6ee8b2022-07-27 15:23:35 +01002408func Test_autocmd_nested_keeps_cursor_pos()
2409 enew
2410 call setline(1, 'foo')
2411 autocmd User foo ++nested normal! $a
2412 autocmd InsertLeave * :
2413 doautocmd User foo
2414 call assert_equal([0, 1, 3, 0], getpos('.'))
2415
2416 bwipe!
2417endfunc
2418
Bram Moolenaarb03950f2022-07-26 13:47:13 +01002419func Test_autocmd_nested_switch_window()
2420 " run this in a separate Vim so that SafeState works
2421 CheckRunVimInTerminal
2422
2423 let lines =<< trim END
2424 vim9script
2425 ['()']->writefile('Xautofile')
2426 autocmd VimEnter * ++nested edit Xautofile | split
2427 autocmd BufReadPost * autocmd SafeState * ++once foldclosed('.')
2428 autocmd WinEnter * matchadd('ErrorMsg', 'pat')
2429 END
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01002430 call writefile(lines, 'Xautoscript', 'D')
Bram Moolenaarb03950f2022-07-26 13:47:13 +01002431 let buf = RunVimInTerminal('-S Xautoscript', {'rows': 10})
2432 call VerifyScreenDump(buf, 'Test_autocmd_nested_switch', {})
2433
2434 call StopVimInTerminal(buf)
2435 call delete('Xautofile')
Bram Moolenaarb03950f2022-07-26 13:47:13 +01002436endfunc
2437
Bram Moolenaareb93f3f2019-04-04 15:04:56 +02002438func Test_autocmd_once()
2439 " Without ++once WinNew triggers twice
2440 let g:did_split = 0
2441 augroup Testing
2442 au WinNew * let g:did_split += 1
2443 augroup END
2444 split
2445 split
2446 call assert_equal(2, g:did_split)
2447 call assert_true(exists('#WinNew'))
2448 close
2449 close
2450
2451 " With ++once WinNew triggers once
2452 let g:did_split = 0
2453 augroup Testing
2454 au!
2455 au WinNew * ++once let g:did_split += 1
2456 augroup END
2457 split
2458 split
2459 call assert_equal(1, g:did_split)
2460 call assert_false(exists('#WinNew'))
2461 close
2462 close
2463
2464 call assert_fails('au WinNew * ++once ++once echo bad', 'E983:')
2465endfunc
2466
Bram Moolenaara68e5952019-04-25 22:22:01 +02002467func Test_autocmd_bufreadpre()
2468 new
2469 let b:bufreadpre = 1
Bram Moolenaarab505b12020-03-23 19:28:44 +01002470 call append(0, range(1000))
Bram Moolenaara68e5952019-04-25 22:22:01 +02002471 w! XAutocmdBufReadPre.txt
2472 autocmd BufReadPre <buffer> :let b:bufreadpre += 1
Bram Moolenaarab505b12020-03-23 19:28:44 +01002473 norm! 500gg
Bram Moolenaara68e5952019-04-25 22:22:01 +02002474 sp
Bram Moolenaarab505b12020-03-23 19:28:44 +01002475 norm! 1000gg
Bram Moolenaara68e5952019-04-25 22:22:01 +02002476 wincmd p
2477 let g:wsv1 = winsaveview()
2478 wincmd p
2479 let g:wsv2 = winsaveview()
2480 " triggers BufReadPre, should not move the cursor in either window
2481 " The topline may change one line in a large window.
2482 edit
2483 call assert_inrange(g:wsv2.topline - 1, g:wsv2.topline + 1, winsaveview().topline)
2484 call assert_equal(g:wsv2.lnum, winsaveview().lnum)
2485 call assert_equal(2, b:bufreadpre)
2486 wincmd p
2487 call assert_equal(g:wsv1.topline, winsaveview().topline)
2488 call assert_equal(g:wsv1.lnum, winsaveview().lnum)
2489 call assert_equal(2, b:bufreadpre)
2490 " Now set the cursor position in an BufReadPre autocommand
2491 " (even though the position will be invalid, this should make Vim reset the
2492 " cursor position in the other window.
2493 wincmd p
2494 set cpo+=g
2495 " won't do anything, but try to set the cursor on an invalid lnum
2496 autocmd BufReadPre <buffer> :norm! 70gg
2497 " triggers BufReadPre, should not move the cursor in either window
2498 e
2499 call assert_equal(1, winsaveview().topline)
2500 call assert_equal(1, winsaveview().lnum)
2501 call assert_equal(3, b:bufreadpre)
2502 wincmd p
2503 call assert_equal(g:wsv1.topline, winsaveview().topline)
2504 call assert_equal(g:wsv1.lnum, winsaveview().lnum)
2505 call assert_equal(3, b:bufreadpre)
2506 close
2507 close
2508 call delete('XAutocmdBufReadPre.txt')
2509 set cpo-=g
2510endfunc
2511
Bram Moolenaar5e66b422019-01-24 21:58:10 +01002512" FileChangedShell tested in test_filechanged.vim
Bram Moolenaar69ea5872019-04-25 20:29:00 +02002513
2514" Tests for the following autocommands:
2515" - FileWritePre writing a compressed file
2516" - FileReadPost reading a compressed file
2517" - BufNewFile reading a file template
2518" - BufReadPre decompressing the file to be read
2519" - FilterReadPre substituting characters in the temp file
2520" - FilterReadPost substituting characters after filtering
2521" - FileReadPre set options for decompression
2522" - FileReadPost decompress the file
2523func Test_ReadWrite_Autocmds()
2524 " Run this test only on Unix-like systems and if gzip is available
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +02002525 CheckUnix
2526 CheckExecutable gzip
Bram Moolenaar69ea5872019-04-25 20:29:00 +02002527
2528 " Make $GZIP empty, "-v" would cause trouble.
2529 let $GZIP = ""
2530
2531 " Use a FileChangedShell autocommand to avoid a prompt for 'Xtestfile.gz'
2532 " being modified outside of Vim (noticed on Solaris).
2533 au FileChangedShell * echo 'caught FileChangedShell'
2534
2535 " Test for the FileReadPost, FileWritePre and FileWritePost autocmds
2536 augroup Test1
2537 au!
2538 au FileWritePre *.gz '[,']!gzip
2539 au FileWritePost *.gz undo
2540 au FileReadPost *.gz '[,']!gzip -d
2541 augroup END
2542
2543 new
2544 set bin
2545 call append(0, [
2546 \ 'line 2 Abcdefghijklmnopqrstuvwxyz',
2547 \ 'line 3 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2548 \ 'line 4 Abcdefghijklmnopqrstuvwxyz',
2549 \ 'line 5 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2550 \ 'line 6 Abcdefghijklmnopqrstuvwxyz',
2551 \ 'line 7 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2552 \ 'line 8 Abcdefghijklmnopqrstuvwxyz',
2553 \ 'line 9 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2554 \ 'line 10 Abcdefghijklmnopqrstuvwxyz'
2555 \ ])
2556 1,9write! Xtestfile.gz
2557 enew! | close
2558
2559 new
2560 " Read and decompress the testfile
2561 0read Xtestfile.gz
2562 call assert_equal([
2563 \ 'line 2 Abcdefghijklmnopqrstuvwxyz',
2564 \ 'line 3 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2565 \ 'line 4 Abcdefghijklmnopqrstuvwxyz',
2566 \ 'line 5 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2567 \ 'line 6 Abcdefghijklmnopqrstuvwxyz',
2568 \ 'line 7 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2569 \ 'line 8 Abcdefghijklmnopqrstuvwxyz',
2570 \ 'line 9 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2571 \ 'line 10 Abcdefghijklmnopqrstuvwxyz'
2572 \ ], getline(1, 9))
2573 enew! | close
2574
2575 augroup Test1
2576 au!
2577 augroup END
2578
2579 " Test for the FileAppendPre and FileAppendPost autocmds
2580 augroup Test2
2581 au!
2582 au BufNewFile *.c read Xtest.c
2583 au FileAppendPre *.out '[,']s/new/NEW/
2584 au FileAppendPost *.out !cat Xtest.c >> test.out
2585 augroup END
2586
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01002587 call writefile(['/*', ' * Here is a new .c file', ' */'], 'Xtest.c', 'D')
Bram Moolenaar69ea5872019-04-25 20:29:00 +02002588 new foo.c " should load Xtest.c
2589 call assert_equal(['/*', ' * Here is a new .c file', ' */'], getline(2, 4))
2590 w! >> test.out " append it to the output file
2591
2592 let contents = readfile('test.out')
2593 call assert_equal(' * Here is a NEW .c file', contents[2])
2594 call assert_equal(' * Here is a new .c file', contents[5])
2595
2596 call delete('test.out')
2597 enew! | close
2598 augroup Test2
2599 au!
2600 augroup END
2601
2602 " Test for the BufReadPre and BufReadPost autocmds
2603 augroup Test3
2604 au!
2605 " setup autocommands to decompress before reading and re-compress
2606 " afterwards
2607 au BufReadPre *.gz exe '!gzip -d ' . shellescape(expand("<afile>"))
2608 au BufReadPre *.gz call rename(expand("<afile>:r"), expand("<afile>"))
2609 au BufReadPost *.gz call rename(expand("<afile>"), expand("<afile>:r"))
2610 au BufReadPost *.gz exe '!gzip ' . shellescape(expand("<afile>:r"))
2611 augroup END
2612
2613 e! Xtestfile.gz " Edit compressed file
2614 call assert_equal([
2615 \ 'line 2 Abcdefghijklmnopqrstuvwxyz',
2616 \ 'line 3 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2617 \ 'line 4 Abcdefghijklmnopqrstuvwxyz',
2618 \ 'line 5 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2619 \ 'line 6 Abcdefghijklmnopqrstuvwxyz',
2620 \ 'line 7 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2621 \ 'line 8 Abcdefghijklmnopqrstuvwxyz',
2622 \ 'line 9 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2623 \ 'line 10 Abcdefghijklmnopqrstuvwxyz'
2624 \ ], getline(1, 9))
2625
2626 w! >> test.out " Append it to the output file
2627
2628 augroup Test3
2629 au!
2630 augroup END
2631
2632 " Test for the FilterReadPre and FilterReadPost autocmds.
2633 set shelltemp " need temp files here
2634 augroup Test4
2635 au!
2636 au FilterReadPre *.out call rename(expand("<afile>"), expand("<afile>") . ".t")
2637 au FilterReadPre *.out exe 'silent !sed s/e/E/ ' . shellescape(expand("<afile>")) . ".t >" . shellescape(expand("<afile>"))
2638 au FilterReadPre *.out exe 'silent !rm ' . shellescape(expand("<afile>")) . '.t'
2639 au FilterReadPost *.out '[,']s/x/X/g
2640 augroup END
2641
2642 e! test.out " Edit the output file
2643 1,$!cat
2644 call assert_equal([
2645 \ 'linE 2 AbcdefghijklmnopqrstuvwXyz',
2646 \ 'linE 3 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
2647 \ 'linE 4 AbcdefghijklmnopqrstuvwXyz',
2648 \ 'linE 5 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
2649 \ 'linE 6 AbcdefghijklmnopqrstuvwXyz',
2650 \ 'linE 7 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
2651 \ 'linE 8 AbcdefghijklmnopqrstuvwXyz',
2652 \ 'linE 9 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
2653 \ 'linE 10 AbcdefghijklmnopqrstuvwXyz'
2654 \ ], getline(1, 9))
2655 call assert_equal([
2656 \ 'line 2 Abcdefghijklmnopqrstuvwxyz',
2657 \ 'line 3 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2658 \ 'line 4 Abcdefghijklmnopqrstuvwxyz',
2659 \ 'line 5 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2660 \ 'line 6 Abcdefghijklmnopqrstuvwxyz',
2661 \ 'line 7 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2662 \ 'line 8 Abcdefghijklmnopqrstuvwxyz',
2663 \ 'line 9 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2664 \ 'line 10 Abcdefghijklmnopqrstuvwxyz'
2665 \ ], readfile('test.out'))
2666
2667 augroup Test4
2668 au!
2669 augroup END
2670 set shelltemp&vim
2671
2672 " Test for the FileReadPre and FileReadPost autocmds.
2673 augroup Test5
2674 au!
2675 au FileReadPre *.gz exe 'silent !gzip -d ' . shellescape(expand("<afile>"))
2676 au FileReadPre *.gz call rename(expand("<afile>:r"), expand("<afile>"))
2677 au FileReadPost *.gz '[,']s/l/L/
2678 augroup END
2679
2680 new
2681 0r Xtestfile.gz " Read compressed file
2682 call assert_equal([
2683 \ 'Line 2 Abcdefghijklmnopqrstuvwxyz',
2684 \ 'Line 3 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2685 \ 'Line 4 Abcdefghijklmnopqrstuvwxyz',
2686 \ 'Line 5 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2687 \ 'Line 6 Abcdefghijklmnopqrstuvwxyz',
2688 \ 'Line 7 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2689 \ 'Line 8 Abcdefghijklmnopqrstuvwxyz',
2690 \ 'Line 9 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2691 \ 'Line 10 Abcdefghijklmnopqrstuvwxyz'
2692 \ ], getline(1, 9))
2693 call assert_equal([
2694 \ 'line 2 Abcdefghijklmnopqrstuvwxyz',
2695 \ 'line 3 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2696 \ 'line 4 Abcdefghijklmnopqrstuvwxyz',
2697 \ 'line 5 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2698 \ 'line 6 Abcdefghijklmnopqrstuvwxyz',
2699 \ 'line 7 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2700 \ 'line 8 Abcdefghijklmnopqrstuvwxyz',
2701 \ 'line 9 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2702 \ 'line 10 Abcdefghijklmnopqrstuvwxyz'
2703 \ ], readfile('Xtestfile.gz'))
2704
2705 augroup Test5
2706 au!
2707 augroup END
2708
2709 au! FileChangedShell
2710 call delete('Xtestfile.gz')
Bram Moolenaar69ea5872019-04-25 20:29:00 +02002711 call delete('test.out')
2712endfunc
Bram Moolenaar23b51392019-05-09 21:38:43 +02002713
2714func Test_throw_in_BufWritePre()
2715 new
2716 call setline(1, ['one', 'two', 'three'])
2717 call assert_false(filereadable('Xthefile'))
2718 augroup throwing
2719 au BufWritePre X* throw 'do not write'
2720 augroup END
2721 try
2722 w Xthefile
2723 catch
2724 let caught = 1
2725 endtry
2726 call assert_equal(1, caught)
2727 call assert_false(filereadable('Xthefile'))
2728
2729 bwipe!
2730 au! throwing
2731endfunc
Bram Moolenaarcadbe1b2019-09-22 21:50:09 +02002732
Bram Moolenaar40fa12a2021-09-22 14:18:13 +02002733func Test_autocmd_in_try_block()
Bram Moolenaar6f14da12022-09-07 21:30:44 +01002734 call mkdir('Xintrydir', 'R')
Bram Moolenaar40fa12a2021-09-22 14:18:13 +02002735 au BufEnter * let g:fname = expand('%')
2736 try
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +01002737 edit Xintrydir/
Bram Moolenaar40fa12a2021-09-22 14:18:13 +02002738 endtry
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +01002739 call assert_match('Xintrydir', g:fname)
Bram Moolenaar40fa12a2021-09-22 14:18:13 +02002740
2741 unlet g:fname
2742 au! BufEnter
Bram Moolenaar40fa12a2021-09-22 14:18:13 +02002743endfunc
2744
Bram Moolenaarcadbe1b2019-09-22 21:50:09 +02002745func Test_autocmd_SafeState()
2746 CheckRunVimInTerminal
2747
2748 let lines =<< trim END
2749 let g:safe = 0
2750 let g:again = ''
2751 au SafeState * let g:safe += 1
2752 au SafeStateAgain * let g:again ..= 'x'
2753 func CallTimer()
2754 call timer_start(10, {id -> execute('let g:again ..= "t"')})
2755 endfunc
2756 END
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01002757 call writefile(lines, 'XSafeState', 'D')
Bram Moolenaarcadbe1b2019-09-22 21:50:09 +02002758 let buf = RunVimInTerminal('-S XSafeState', #{rows: 6})
2759
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01002760 " Sometimes we loop to handle a K_IGNORE, SafeState may be triggered once or
Bram Moolenaar8fb1b472020-02-23 16:16:26 +01002761 " more often.
Bram Moolenaarcadbe1b2019-09-22 21:50:09 +02002762 call term_sendkeys(buf, ":echo g:safe\<CR>")
Bram Moolenaar8fb1b472020-02-23 16:16:26 +01002763 call WaitForAssert({-> assert_match('^\d ', term_getline(buf, 6))}, 1000)
Bram Moolenaarcadbe1b2019-09-22 21:50:09 +02002764
Bram Moolenaar8fb1b472020-02-23 16:16:26 +01002765 " SafeStateAgain should be invoked at least three times
Bram Moolenaarcadbe1b2019-09-22 21:50:09 +02002766 call term_sendkeys(buf, ":echo g:again\<CR>")
Bram Moolenaar8fb1b472020-02-23 16:16:26 +01002767 call WaitForAssert({-> assert_match('^xxx', term_getline(buf, 6))}, 1000)
Bram Moolenaarcadbe1b2019-09-22 21:50:09 +02002768
2769 call term_sendkeys(buf, ":let g:again = ''\<CR>:call CallTimer()\<CR>")
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02002770 call TermWait(buf, 50)
Bram Moolenaar0f6629a2019-09-22 23:24:13 +02002771 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02002772 call TermWait(buf, 50)
Bram Moolenaarcadbe1b2019-09-22 21:50:09 +02002773 call term_sendkeys(buf, ":echo g:again\<CR>")
2774 call WaitForAssert({-> assert_match('xtx', term_getline(buf, 6))}, 1000)
2775
2776 call StopVimInTerminal(buf)
Bram Moolenaarcadbe1b2019-09-22 21:50:09 +02002777endfunc
Bram Moolenaar23324a02019-10-01 17:39:04 +02002778
2779func Test_autocmd_CmdWinEnter()
2780 CheckRunVimInTerminal
Bram Moolenaar21829c52021-01-26 22:42:21 +01002781
Bram Moolenaar23324a02019-10-01 17:39:04 +02002782 let lines =<< trim END
Egor Zvorykin125ffd22021-11-17 14:01:14 +00002783 augroup vimHints | au! | augroup END
Bram Moolenaar23324a02019-10-01 17:39:04 +02002784 let b:dummy_var = 'This is a dummy'
2785 autocmd CmdWinEnter * quit
2786 let winnr = winnr('$')
2787 END
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01002788 let filename = 'XCmdWinEnter'
Bram Moolenaar23324a02019-10-01 17:39:04 +02002789 call writefile(lines, filename)
2790 let buf = RunVimInTerminal('-S '.filename, #{rows: 6})
2791
2792 call term_sendkeys(buf, "q:")
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02002793 call TermWait(buf)
Bram Moolenaar23324a02019-10-01 17:39:04 +02002794 call term_sendkeys(buf, ":echo b:dummy_var\<cr>")
Bram Moolenaar353c3512020-03-15 14:19:26 +01002795 call WaitForAssert({-> assert_match('^This is a dummy', term_getline(buf, 6))}, 2000)
Bram Moolenaar23324a02019-10-01 17:39:04 +02002796 call term_sendkeys(buf, ":echo &buftype\<cr>")
2797 call WaitForAssert({-> assert_notmatch('^nofile', term_getline(buf, 6))}, 1000)
2798 call term_sendkeys(buf, ":echo winnr\<cr>")
2799 call WaitForAssert({-> assert_match('^1', term_getline(buf, 6))}, 1000)
2800
2801 " clean up
2802 call StopVimInTerminal(buf)
2803 call delete(filename)
2804endfunc
Bram Moolenaarec66c412019-10-11 21:19:13 +02002805
2806func Test_autocmd_was_using_freed_memory()
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01002807 CheckFeature quickfix
2808
Bram Moolenaarec66c412019-10-11 21:19:13 +02002809 pedit xx
2810 n x
Bram Moolenaar92bb83e2021-02-03 23:04:46 +01002811 augroup winenter
2812 au WinEnter * if winnr('$') > 2 | quit | endif
2813 augroup END
Bram Moolenaarec66c412019-10-11 21:19:13 +02002814 split
Bram Moolenaar92bb83e2021-02-03 23:04:46 +01002815
2816 augroup winenter
2817 au! WinEnter
2818 augroup END
2819
2820 bwipe xx
2821 bwipe x
2822 pclose
Bram Moolenaarec66c412019-10-11 21:19:13 +02002823endfunc
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +01002824
2825func Test_BufWrite_lockmarks()
Bram Moolenaarf08b0eb2021-10-16 13:00:14 +01002826 let g:test_is_flaky = 1
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +01002827 edit! Xtest
2828 call setline(1, ['a', 'b', 'c', 'd'])
2829
2830 " :lockmarks preserves the marks
2831 call SetChangeMarks(2, 3)
2832 lockmarks write
2833 call assert_equal([2, 3], [line("'["), line("']")])
2834
2835 " *WritePre autocmds get the correct line range, but lockmarks preserves the
2836 " original values for the user
2837 augroup lockmarks
2838 au!
2839 au BufWritePre,FilterWritePre * call assert_equal([1, 4], [line("'["), line("']")])
2840 au FileWritePre * call assert_equal([3, 4], [line("'["), line("']")])
2841 augroup END
2842
2843 lockmarks write
2844 call assert_equal([2, 3], [line("'["), line("']")])
2845
2846 if executable('cat')
2847 lockmarks %!cat
2848 call assert_equal([2, 3], [line("'["), line("']")])
2849 endif
2850
2851 lockmarks 3,4write Xtest2
2852 call assert_equal([2, 3], [line("'["), line("']")])
2853
2854 au! lockmarks
2855 augroup! lockmarks
2856 call delete('Xtest')
2857 call delete('Xtest2')
2858endfunc
Bram Moolenaarce6db022020-01-07 20:11:42 +01002859
2860func Test_FileType_spell()
2861 if !isdirectory('/tmp')
2862 throw "Skipped: requires /tmp directory"
2863 endif
2864
2865 " this was crashing with an invalid free()
2866 setglobal spellfile=/tmp/en.utf-8.add
2867 augroup crash
2868 autocmd!
2869 autocmd BufNewFile,BufReadPost crashfile setf somefiletype
2870 autocmd BufNewFile,BufReadPost crashfile set ft=anotherfiletype
2871 autocmd FileType anotherfiletype setlocal spell
2872 augroup END
2873 func! NoCrash() abort
2874 edit /tmp/crashfile
2875 endfunc
2876 call NoCrash()
2877
2878 au! crash
2879 setglobal spellfile=
2880endfunc
Bram Moolenaarbc2b71d2020-02-17 21:33:30 +01002881
Bram Moolenaaref976322022-09-28 11:48:30 +01002882" this was wiping out the current buffer and using freed memory
2883func Test_SpellFileMissing_bwipe()
2884 next 0
2885 au SpellFileMissing 0 bwipe
2886 call assert_fails('set spell spelllang=0', 'E937:')
2887
2888 au! SpellFileMissing
2889 bwipe
2890endfunc
2891
Bram Moolenaar406cd902020-02-18 21:54:41 +01002892" Test closing a window or editing another buffer from a FileChangedRO handler
2893" in a readonly buffer
2894func Test_FileChangedRO_winclose()
Bram Moolenaar62cd26a2020-10-11 20:08:44 +02002895 call test_override('ui_delay', 10)
2896
Bram Moolenaar406cd902020-02-18 21:54:41 +01002897 augroup FileChangedROTest
2898 au!
2899 autocmd FileChangedRO * quit
2900 augroup END
2901 new
2902 set readonly
2903 call assert_fails('normal i', 'E788:')
2904 close
2905 augroup! FileChangedROTest
2906
2907 augroup FileChangedROTest
2908 au!
Bram Moolenaar61abe7d2022-08-30 21:46:08 +01002909 autocmd FileChangedRO * edit Xrofile
Bram Moolenaar406cd902020-02-18 21:54:41 +01002910 augroup END
2911 new
2912 set readonly
2913 call assert_fails('normal i', 'E788:')
2914 close
2915 augroup! FileChangedROTest
Bram Moolenaar62cd26a2020-10-11 20:08:44 +02002916 call test_override('ALL', 0)
Bram Moolenaar406cd902020-02-18 21:54:41 +01002917endfunc
2918
Bram Moolenaar0c81d1b2020-02-22 22:45:55 +01002919func LogACmd()
2920 call add(g:logged, line('$'))
2921endfunc
2922
2923func Test_TermChanged()
Bram Moolenaard28e0b32020-02-22 23:08:52 +01002924 CheckNotGui
2925
Bram Moolenaar0c81d1b2020-02-22 22:45:55 +01002926 enew!
2927 tabnew
2928 call setline(1, ['a', 'b', 'c', 'd'])
2929 $
2930 au TermChanged * call LogACmd()
2931 let g:logged = []
2932 let term_save = &term
2933 set term=xterm
2934 call assert_equal([1, 4], g:logged)
2935
2936 au! TermChanged
2937 let &term = term_save
2938 bwipe!
2939endfunc
2940
Bram Moolenaare3284872020-03-19 13:55:03 +01002941" Test for FileReadCmd autocmd
2942func Test_autocmd_FileReadCmd()
2943 func ReadFileCmd()
2944 call append(line('$'), "v:cmdarg = " .. v:cmdarg)
2945 endfunc
2946 augroup FileReadCmdTest
2947 au!
2948 au FileReadCmd Xtest call ReadFileCmd()
2949 augroup END
2950
2951 new
2952 read ++bin Xtest
2953 read ++nobin Xtest
2954 read ++edit Xtest
2955 read ++bad=keep Xtest
2956 read ++bad=drop Xtest
2957 read ++bad=- Xtest
2958 read ++ff=unix Xtest
2959 read ++ff=dos Xtest
2960 read ++ff=mac Xtest
2961 read ++enc=utf-8 Xtest
2962
2963 call assert_equal(['',
2964 \ 'v:cmdarg = ++bin',
2965 \ 'v:cmdarg = ++nobin',
2966 \ 'v:cmdarg = ++edit',
2967 \ 'v:cmdarg = ++bad=keep',
2968 \ 'v:cmdarg = ++bad=drop',
2969 \ 'v:cmdarg = ++bad=-',
2970 \ 'v:cmdarg = ++ff=unix',
2971 \ 'v:cmdarg = ++ff=dos',
2972 \ 'v:cmdarg = ++ff=mac',
2973 \ 'v:cmdarg = ++enc=utf-8'], getline(1, '$'))
2974
2975 close!
2976 augroup FileReadCmdTest
2977 au!
2978 augroup END
2979 delfunc ReadFileCmd
2980endfunc
2981
Bram Moolenaaree4e0c12020-04-06 21:35:05 +02002982" Test for passing invalid arguments to autocmd
2983func Test_autocmd_invalid_args()
2984 " Additional character after * for event
Bram Moolenaar61abe7d2022-08-30 21:46:08 +01002985 call assert_fails('autocmd *a Xinvfile set ff=unix', 'E215:')
Bram Moolenaaree4e0c12020-04-06 21:35:05 +02002986 augroup Test
2987 augroup END
2988 " Invalid autocmd event
Bram Moolenaar61abe7d2022-08-30 21:46:08 +01002989 call assert_fails('autocmd Bufabc Xinvfile set ft=vim', 'E216:')
Bram Moolenaaree4e0c12020-04-06 21:35:05 +02002990 " Invalid autocmd event in a autocmd group
Bram Moolenaar61abe7d2022-08-30 21:46:08 +01002991 call assert_fails('autocmd Test Bufabc Xinvfile set ft=vim', 'E216:')
Bram Moolenaaree4e0c12020-04-06 21:35:05 +02002992 augroup! Test
2993 " Execute all autocmds
2994 call assert_fails('doautocmd * BufEnter', 'E217:')
2995 call assert_fails('augroup! x1a2b3', 'E367:')
2996 call assert_fails('autocmd BufNew <buffer=999> pwd', 'E680:')
Bram Moolenaar531be472020-09-23 22:38:05 +02002997 call assert_fails('autocmd BufNew \) set ff=unix', 'E55:')
Bram Moolenaaree4e0c12020-04-06 21:35:05 +02002998endfunc
2999
3000" Test for deep nesting of autocmds
3001func Test_autocmd_deep_nesting()
Bram Moolenaar61abe7d2022-08-30 21:46:08 +01003002 autocmd BufEnter Xdeepfile doautocmd BufEnter Xdeepfile
3003 call assert_fails('doautocmd BufEnter Xdeepfile', 'E218:')
3004 autocmd! BufEnter Xdeepfile
Bram Moolenaaree4e0c12020-04-06 21:35:05 +02003005endfunc
3006
Bram Moolenaarbe5ee862020-06-10 20:56:58 +02003007" Tests for SigUSR1 autocmd event, which is only available on posix systems.
3008func Test_autocmd_sigusr1()
3009 CheckUnix
Bram Moolenaar0056ca72022-09-23 21:26:39 +01003010 " FIXME: should this work on MacOS M1?
3011 CheckNotMacM1
Bram Moolenaar62cd26a2020-10-11 20:08:44 +02003012 CheckExecutable /bin/kill
Bram Moolenaarbe5ee862020-06-10 20:56:58 +02003013
3014 let g:sigusr1_passed = 0
3015 au SigUSR1 * let g:sigusr1_passed = 1
3016 call system('/bin/kill -s usr1 ' . getpid())
3017 call WaitForAssert({-> assert_true(g:sigusr1_passed)})
3018
3019 au! SigUSR1
3020 unlet g:sigusr1_passed
3021endfunc
3022
Bram Moolenaarb340bae2020-06-15 19:51:56 +02003023" Test for BufReadPre autocmd deleting the file
3024func Test_BufReadPre_delfile()
3025 augroup TestAuCmd
3026 au!
Bram Moolenaare7cda972022-08-29 11:02:59 +01003027 autocmd BufReadPre XbufreadPre call delete('XbufreadPre')
Bram Moolenaarb340bae2020-06-15 19:51:56 +02003028 augroup END
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01003029 call writefile([], 'XbufreadPre', 'D')
Bram Moolenaare7cda972022-08-29 11:02:59 +01003030 call assert_fails('new XbufreadPre', 'E200:')
3031 call assert_equal('XbufreadPre', @%)
Bram Moolenaarb340bae2020-06-15 19:51:56 +02003032 call assert_equal(1, &readonly)
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01003033
Bram Moolenaarb340bae2020-06-15 19:51:56 +02003034 augroup TestAuCmd
3035 au!
3036 augroup END
3037 close!
3038endfunc
3039
3040" Test for BufReadPre autocmd changing the current buffer
3041func Test_BufReadPre_changebuf()
3042 augroup TestAuCmd
3043 au!
Bram Moolenaare7cda972022-08-29 11:02:59 +01003044 autocmd BufReadPre Xchangebuf edit Xsomeotherfile
Bram Moolenaarb340bae2020-06-15 19:51:56 +02003045 augroup END
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01003046 call writefile([], 'Xchangebuf', 'D')
Bram Moolenaare7cda972022-08-29 11:02:59 +01003047 call assert_fails('new Xchangebuf', 'E201:')
Bram Moolenaarb340bae2020-06-15 19:51:56 +02003048 call assert_equal('Xsomeotherfile', @%)
3049 call assert_equal(1, &readonly)
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01003050
Bram Moolenaarb340bae2020-06-15 19:51:56 +02003051 augroup TestAuCmd
3052 au!
3053 augroup END
3054 close!
3055endfunc
3056
3057" Test for BufWipeouti autocmd changing the current buffer when reading a file
3058" in an empty buffer with 'f' flag in 'cpo'
3059func Test_BufDelete_changebuf()
3060 new
3061 augroup TestAuCmd
3062 au!
3063 autocmd BufWipeout * let bufnr = bufadd('somefile') | exe "b " .. bufnr
3064 augroup END
3065 let save_cpo = &cpo
3066 set cpo+=f
Bram Moolenaarb18b4962022-09-02 21:55:50 +01003067 call assert_fails('r Xchangebuf', ['E812:', 'E484:'])
Bram Moolenaarb340bae2020-06-15 19:51:56 +02003068 call assert_equal('somefile', @%)
3069 let &cpo = save_cpo
3070 augroup TestAuCmd
3071 au!
3072 augroup END
3073 close!
3074endfunc
3075
Bram Moolenaar0fe937f2020-06-16 22:42:04 +02003076" Test for the temporary internal window used to execute autocmds
3077func Test_autocmd_window()
3078 %bw!
3079 edit one.txt
3080 tabnew two.txt
Bram Moolenaar41cd8032021-03-13 15:47:56 +01003081 vnew three.txt
3082 tabnew four.txt
3083 tabprevious
Bram Moolenaar0fe937f2020-06-16 22:42:04 +02003084 let g:blist = []
Bram Moolenaar832adf92020-06-25 19:01:36 +02003085 augroup aucmd_win_test1
Bram Moolenaar0fe937f2020-06-16 22:42:04 +02003086 au!
3087 au BufEnter * call add(g:blist, [expand('<afile>'),
3088 \ win_gettype(bufwinnr(expand('<afile>')))])
3089 augroup END
3090
3091 doautoall BufEnter
Bram Moolenaar41cd8032021-03-13 15:47:56 +01003092 call assert_equal([
3093 \ ['one.txt', 'autocmd'],
3094 \ ['two.txt', ''],
3095 \ ['four.txt', 'autocmd'],
3096 \ ['three.txt', ''],
3097 \ ], g:blist)
Bram Moolenaar0fe937f2020-06-16 22:42:04 +02003098
Bram Moolenaar832adf92020-06-25 19:01:36 +02003099 augroup aucmd_win_test1
Bram Moolenaar0fe937f2020-06-16 22:42:04 +02003100 au!
3101 augroup END
Bram Moolenaar832adf92020-06-25 19:01:36 +02003102 augroup! aucmd_win_test1
3103 %bw!
3104endfunc
3105
3106" Test for trying to close the temporary window used for executing an autocmd
3107func Test_close_autocmd_window()
3108 %bw!
3109 edit one.txt
3110 tabnew two.txt
3111 augroup aucmd_win_test2
3112 au!
3113 au BufEnter * if expand('<afile>') == 'one.txt' | 1close | endif
3114 augroup END
3115
3116 call assert_fails('doautoall BufEnter', 'E813:')
3117
3118 augroup aucmd_win_test2
3119 au!
3120 augroup END
3121 augroup! aucmd_win_test2
Bram Moolenaarcf844172020-06-26 19:44:06 +02003122 %bwipe!
3123endfunc
3124
3125" Test for trying to close the tab that has the temporary window for exeucing
3126" an autocmd.
3127func Test_close_autocmd_tab()
3128 edit one.txt
3129 tabnew two.txt
3130 augroup aucmd_win_test
3131 au!
3132 au BufEnter * if expand('<afile>') == 'one.txt' | tabfirst | tabonly | endif
3133 augroup END
3134
3135 call assert_fails('doautoall BufEnter', 'E813:')
3136
3137 tabonly
3138 augroup aucmd_win_test
3139 au!
3140 augroup END
3141 augroup! aucmd_win_test
3142 %bwipe!
Bram Moolenaar0fe937f2020-06-16 22:42:04 +02003143endfunc
3144
Bram Moolenaarcb1956d2022-01-07 15:45:18 +00003145func Test_Visual_doautoall_redraw()
3146 call setline(1, ['a', 'b'])
3147 new
3148 wincmd p
3149 call feedkeys("G\<C-V>", 'txn')
3150 autocmd User Explode ++once redraw
3151 doautoall User Explode
3152 %bwipe!
3153endfunc
3154
Bram Moolenaar6bcb8772021-02-03 21:23:29 +01003155" This was using freed memory.
3156func Test_BufNew_arglocal()
3157 arglocal
3158 au BufNew * arglocal
3159 call assert_fails('drop xx', 'E1156:')
3160
3161 au! BufNew
3162endfunc
3163
Bram Moolenaar8ab37572021-02-03 21:56:59 +01003164func Test_autocmd_closes_window()
3165 au BufNew,BufWinLeave * e %e
3166 file yyy
3167 au BufNew,BufWinLeave * ball
Bram Moolenaaraad5f9d2021-02-06 17:30:31 +01003168 n xxx
Bram Moolenaar8ab37572021-02-03 21:56:59 +01003169
Bram Moolenaaraad5f9d2021-02-06 17:30:31 +01003170 %bwipe
Bram Moolenaar8ab37572021-02-03 21:56:59 +01003171 au! BufNew
3172 au! BufWinLeave
3173endfunc
3174
Bram Moolenaar92bb83e2021-02-03 23:04:46 +01003175func Test_autocmd_quit_psearch()
3176 sn aa bb
3177 augroup aucmd_win_test
3178 au!
3179 au BufEnter,BufLeave,BufNew,WinEnter,WinLeave,WinNew * if winnr('$') > 1 | q | endif
3180 augroup END
3181 ps /
3182
3183 augroup aucmd_win_test
3184 au!
3185 augroup END
zeertzjq7851c692022-04-21 11:14:01 +01003186 new
3187 pclose
Bram Moolenaar92bb83e2021-02-03 23:04:46 +01003188endfunc
3189
Bram Moolenaaraad5f9d2021-02-06 17:30:31 +01003190" Fuzzer found some strange combination that caused a crash.
3191func Test_autocmd_normal_mess()
Bram Moolenaardd07c022021-02-07 13:32:46 +01003192 " For unknown reason this hangs on MS-Windows
3193 CheckNotMSWindows
3194
Bram Moolenaaraad5f9d2021-02-06 17:30:31 +01003195 augroup aucmd_normal_test
3196 au BufLeave,BufWinLeave,BufHidden,BufUnload,BufDelete,BufWipeout * norm 7q/qc
3197 augroup END
Bram Moolenaar983d83f2021-02-07 12:12:43 +01003198 call assert_fails('o4', 'E1159')
Bram Moolenaaraad5f9d2021-02-06 17:30:31 +01003199 silent! H
Bram Moolenaar983d83f2021-02-07 12:12:43 +01003200 call assert_fails('e xx', 'E1159')
Bram Moolenaaraad5f9d2021-02-06 17:30:31 +01003201 normal G
3202
3203 augroup aucmd_normal_test
3204 au!
3205 augroup END
3206endfunc
3207
Bram Moolenaar8c6951f2021-02-06 18:08:45 +01003208func Test_autocmd_closing_cmdwin()
Bram Moolenaardd07c022021-02-07 13:32:46 +01003209 " For unknown reason this hangs on MS-Windows
3210 CheckNotMSWindows
3211
Bram Moolenaar8c6951f2021-02-06 18:08:45 +01003212 au BufWinLeave * nested q
3213 call assert_fails("norm 7q?\n", 'E855:')
3214
3215 au! BufWinLeave
3216 new
3217 only
3218endfunc
3219
Bram Moolenaar2c7080b2021-02-06 19:19:42 +01003220func Test_autocmd_vimgrep()
3221 augroup aucmd_vimgrep
Charlie Grovesfef44852022-04-19 16:24:12 +01003222 au QuickfixCmdPre,BufNew,BufReadCmd * sb
zeertzjq7851c692022-04-21 11:14:01 +01003223 au QuickfixCmdPre,BufNew,BufReadCmd * q9
Bram Moolenaar2c7080b2021-02-06 19:19:42 +01003224 augroup END
Bram Moolenaardd07c022021-02-07 13:32:46 +01003225 call assert_fails('lv ?a? foo', 'E926:')
Bram Moolenaar2c7080b2021-02-06 19:19:42 +01003226
3227 augroup aucmd_vimgrep
3228 au!
3229 augroup END
3230endfunc
3231
Bram Moolenaar73b8b0a2021-08-01 14:52:32 +02003232func Test_autocmd_with_block()
3233 augroup block_testing
3234 au BufReadPost *.xml {
3235 setlocal matchpairs+=<:>
3236 /<start
3237 }
Bram Moolenaar63b91732021-08-05 20:40:03 +02003238 au CursorHold * {
3239 autocmd BufReadPre * ++once echo 'one' | echo 'two'
3240 g:gotSafeState = 77
3241 }
Bram Moolenaar73b8b0a2021-08-01 14:52:32 +02003242 augroup END
3243
3244 let expected = "\n--- Autocommands ---\nblock_testing BufRead\n *.xml {^@ setlocal matchpairs+=<:>^@ /<start^@ }"
3245 call assert_equal(expected, execute('au BufReadPost *.xml'))
3246
Bram Moolenaar63b91732021-08-05 20:40:03 +02003247 doautocmd CursorHold
3248 call assert_equal(77, g:gotSafeState)
3249 unlet g:gotSafeState
3250
Bram Moolenaar73b8b0a2021-08-01 14:52:32 +02003251 augroup block_testing
3252 au!
Bram Moolenaar75ebd2a2022-06-03 17:39:46 +01003253 autocmd CursorHold * {
3254 if true
3255 # comment
3256 && true
3257
3258 && true
3259 g:done = 'yes'
3260 endif
3261 }
3262 augroup END
3263 doautocmd CursorHold
3264 call assert_equal('yes', g:done)
3265
3266 unlet g:done
3267 augroup block_testing
3268 au!
Bram Moolenaar73b8b0a2021-08-01 14:52:32 +02003269 augroup END
3270endfunc
3271
Christian Brabandtdb3b4462021-10-16 11:58:55 +01003272" Test TextChangedI and TextChanged
3273func Test_Changed_ChangedI()
3274 new
3275 call test_override("char_avail", 1)
3276 let [g:autocmd_i, g:autocmd_n] = ['','']
3277
3278 func! TextChangedAutocmdI(char)
3279 let g:autocmd_{tolower(a:char)} = a:char .. b:changedtick
3280 endfunc
3281
3282 augroup Test_TextChanged
3283 au!
3284 au TextChanged <buffer> :call TextChangedAutocmdI('N')
3285 au TextChangedI <buffer> :call TextChangedAutocmdI('I')
3286 augroup END
3287
3288 call feedkeys("ifoo\<esc>", 'tnix')
3289 " TODO: Test test does not seem to trigger TextChanged autocommand, this
3290 " requires running Vim in a terminal window.
3291 " call assert_equal('N3', g:autocmd_n)
3292 call assert_equal('I3', g:autocmd_i)
3293
3294 call feedkeys("yyp", 'tnix')
3295 " TODO: Test test does not seem to trigger TextChanged autocommand.
3296 " call assert_equal('N4', g:autocmd_n)
3297 call assert_equal('I3', g:autocmd_i)
3298
3299 " CleanUp
3300 call test_override("char_avail", 0)
3301 au! TextChanged <buffer>
3302 au! TextChangedI <buffer>
3303 augroup! Test_TextChanged
3304 delfu TextChangedAutocmdI
3305 unlet! g:autocmd_i g:autocmd_n
3306
3307 bw!
3308endfunc
Bram Moolenaar2c7080b2021-02-06 19:19:42 +01003309
Bram Moolenaar6f2465d2022-03-22 18:13:01 +00003310func Test_closing_autocmd_window()
3311 let lines =<< trim END
3312 edit Xa.txt
3313 tabnew Xb.txt
3314 autocmd BufEnter Xa.txt unhide 1
3315 doautoall BufEnter
3316 END
3317 call v9.CheckScriptFailure(lines, 'E814:')
3318 au! BufEnter
3319 only!
3320 bwipe Xa.txt
3321 bwipe Xb.txt
3322endfunc
3323
Bram Moolenaar347538f2022-03-26 16:28:06 +00003324func Test_bufwipeout_changes_window()
3325 " This should not crash, but we don't have any expectations about what
3326 " happens, changing window in BufWipeout has unpredictable results.
3327 tabedit
3328 let g:window_id = win_getid()
3329 topleft new
3330 setlocal bufhidden=wipe
3331 autocmd BufWipeout <buffer> call win_gotoid(g:window_id)
3332 tabprevious
3333 +tabclose
3334
3335 unlet g:window_id
3336 au! BufWipeout
3337 %bwipe!
3338endfunc
3339
zeertzjq021996f2022-04-10 11:44:04 +01003340func Test_v_event_readonly()
3341 autocmd CompleteChanged * let v:event.width = 0
3342 call assert_fails("normal! i\<C-X>\<C-V>", 'E46:')
3343 au! CompleteChanged
3344
3345 autocmd DirChangedPre * let v:event.directory = ''
3346 call assert_fails('cd .', 'E46:')
3347 au! DirChangedPre
3348
3349 autocmd ModeChanged * let v:event.new_mode = ''
3350 call assert_fails('normal! cc', 'E46:')
3351 au! ModeChanged
3352
3353 autocmd TextYankPost * let v:event.operator = ''
3354 call assert_fails('normal! yy', 'E46:')
3355 au! TextYankPost
3356endfunc
3357
zeertzjqc9e8fd62022-07-26 18:12:38 +01003358" Test for ModeChanged pattern
3359func Test_mode_changes()
3360 let g:index = 0
3361 let g:mode_seq = ['n', 'i', 'n', 'v', 'V', 'i', 'ix', 'i', 'ic', 'i', 'n', 'no', 'n', 'V', 'v', 's', 'n']
3362 func! TestMode()
3363 call assert_equal(g:mode_seq[g:index], get(v:event, "old_mode"))
3364 call assert_equal(g:mode_seq[g:index + 1], get(v:event, "new_mode"))
3365 call assert_equal(mode(1), get(v:event, "new_mode"))
3366 let g:index += 1
3367 endfunc
3368
3369 au ModeChanged * :call TestMode()
3370 let g:n_to_any = 0
3371 au ModeChanged n:* let g:n_to_any += 1
3372 call feedkeys("i\<esc>vVca\<CR>\<C-X>\<C-L>\<esc>ggdG", 'tnix')
3373
3374 let g:V_to_v = 0
3375 au ModeChanged V:v let g:V_to_v += 1
3376 call feedkeys("Vv\<C-G>\<esc>", 'tnix')
3377 call assert_equal(len(filter(g:mode_seq[1:], {idx, val -> val == 'n'})), g:n_to_any)
3378 call assert_equal(1, g:V_to_v)
3379 call assert_equal(len(g:mode_seq) - 1, g:index)
3380
3381 let g:n_to_i = 0
3382 au ModeChanged n:i let g:n_to_i += 1
3383 let g:n_to_niI = 0
3384 au ModeChanged i:niI let g:n_to_niI += 1
3385 let g:niI_to_i = 0
3386 au ModeChanged niI:i let g:niI_to_i += 1
3387 let g:nany_to_i = 0
3388 au ModeChanged n*:i let g:nany_to_i += 1
3389 let g:i_to_n = 0
3390 au ModeChanged i:n let g:i_to_n += 1
3391 let g:nori_to_any = 0
3392 au ModeChanged [ni]:* let g:nori_to_any += 1
3393 let g:i_to_any = 0
3394 au ModeChanged i:* let g:i_to_any += 1
3395 let g:index = 0
3396 let g:mode_seq = ['n', 'i', 'niI', 'i', 'n']
3397 call feedkeys("a\<C-O>l\<esc>", 'tnix')
3398 call assert_equal(len(g:mode_seq) - 1, g:index)
3399 call assert_equal(1, g:n_to_i)
3400 call assert_equal(1, g:n_to_niI)
3401 call assert_equal(1, g:niI_to_i)
3402 call assert_equal(2, g:nany_to_i)
3403 call assert_equal(1, g:i_to_n)
3404 call assert_equal(2, g:i_to_any)
3405 call assert_equal(3, g:nori_to_any)
3406
3407 if has('terminal')
3408 let g:mode_seq += ['c', 'n', 't', 'nt', 'c', 'nt', 'n']
3409 call feedkeys(":term\<CR>\<C-W>N:bd!\<CR>", 'tnix')
3410 call assert_equal(len(g:mode_seq) - 1, g:index)
3411 call assert_equal(1, g:n_to_i)
3412 call assert_equal(1, g:n_to_niI)
3413 call assert_equal(1, g:niI_to_i)
3414 call assert_equal(2, g:nany_to_i)
3415 call assert_equal(1, g:i_to_n)
3416 call assert_equal(2, g:i_to_any)
3417 call assert_equal(5, g:nori_to_any)
3418 endif
3419
zeertzjqd1955982022-10-05 11:24:46 +01003420 let g:n_to_c = 0
3421 au ModeChanged n:c let g:n_to_c += 1
3422 let g:c_to_n = 0
3423 au ModeChanged c:n let g:c_to_n += 1
3424 let g:mode_seq += ['c', 'n', 'c', 'n']
3425 call feedkeys("q:\<C-C>\<Esc>", 'tnix')
3426 call assert_equal(len(g:mode_seq) - 1, g:index)
3427 call assert_equal(2, g:n_to_c)
3428 call assert_equal(2, g:c_to_n)
3429 unlet g:n_to_c
3430 unlet g:c_to_n
zeertzjqc9e8fd62022-07-26 18:12:38 +01003431
Bram Moolenaar61c4b042022-10-18 15:10:11 +01003432 let g:n_to_v = 0
3433 au ModeChanged n:v let g:n_to_v += 1
3434 let g:v_to_n = 0
3435 au ModeChanged v:n let g:v_to_n += 1
3436 let g:mode_seq += ['v', 'n']
3437 call feedkeys("v\<C-C>", 'tnix')
3438 call assert_equal(len(g:mode_seq) - 1, g:index)
3439 call assert_equal(1, g:n_to_v)
3440 call assert_equal(1, g:v_to_n)
3441 unlet g:n_to_v
3442 unlet g:v_to_n
3443
zeertzjqc9e8fd62022-07-26 18:12:38 +01003444 au! ModeChanged
3445 delfunc TestMode
3446 unlet! g:mode_seq
3447 unlet! g:index
3448 unlet! g:n_to_any
3449 unlet! g:V_to_v
3450 unlet! g:n_to_i
3451 unlet! g:n_to_niI
3452 unlet! g:niI_to_i
3453 unlet! g:nany_to_i
3454 unlet! g:i_to_n
3455 unlet! g:nori_to_any
3456 unlet! g:i_to_any
3457endfunc
3458
3459func Test_recursive_ModeChanged()
3460 au! ModeChanged * norm 0u
3461 sil! norm 
3462 au! ModeChanged
3463endfunc
3464
3465func Test_ModeChanged_starts_visual()
3466 " This was triggering ModeChanged before setting VIsual, causing a crash.
3467 au! ModeChanged * norm 0u
3468 sil! norm 
3469
3470 au! ModeChanged
3471endfunc
Bram Moolenaar347538f2022-03-26 16:28:06 +00003472
Charlie Grovesfef44852022-04-19 16:24:12 +01003473func Test_noname_autocmd()
3474 augroup test_noname_autocmd_group
3475 autocmd!
3476 autocmd BufEnter * call add(s:li, ["BufEnter", expand("<afile>")])
3477 autocmd BufDelete * call add(s:li, ["BufDelete", expand("<afile>")])
3478 autocmd BufLeave * call add(s:li, ["BufLeave", expand("<afile>")])
3479 autocmd BufUnload * call add(s:li, ["BufUnload", expand("<afile>")])
3480 autocmd BufWipeout * call add(s:li, ["BufWipeout", expand("<afile>")])
3481 augroup END
3482
3483 let s:li = []
3484 edit foo
3485 call assert_equal([['BufUnload', ''], ['BufDelete', ''], ['BufWipeout', ''], ['BufEnter', 'foo']], s:li)
3486
3487 au! test_noname_autocmd_group
3488 augroup! test_noname_autocmd_group
3489endfunc
3490
Yegappan Lakshmanan1755a912022-05-19 10:31:47 +01003491" Test for the autocmd_get() function
3492func Test_autocmd_get()
3493 augroup TestAutoCmdFns
3494 au!
3495 autocmd BufAdd *.vim echo "bufadd-vim"
3496 autocmd BufAdd *.py echo "bufadd-py"
3497 autocmd BufHidden *.vim echo "bufhidden"
3498 augroup END
3499 augroup TestAutoCmdFns2
3500 autocmd BufAdd *.vim echo "bufadd-vim-2"
3501 autocmd BufRead *.a1b2c3 echo "bufadd-vim-2"
3502 augroup END
3503
3504 let l = autocmd_get()
3505 call assert_true(l->len() > 0)
3506
3507 " Test for getting all the autocmds in a group
3508 let expected = [
3509 \ #{cmd: 'echo "bufadd-vim"', group: 'TestAutoCmdFns',
3510 \ pattern: '*.vim', nested: v:false, once: v:false,
3511 \ event: 'BufAdd'},
3512 \ #{cmd: 'echo "bufadd-py"', group: 'TestAutoCmdFns',
3513 \ pattern: '*.py', nested: v:false, once: v:false,
3514 \ event: 'BufAdd'},
3515 \ #{cmd: 'echo "bufhidden"', group: 'TestAutoCmdFns',
3516 \ pattern: '*.vim', nested: v:false,
3517 \ once: v:false, event: 'BufHidden'}]
3518 call assert_equal(expected, autocmd_get(#{group: 'TestAutoCmdFns'}))
3519
3520 " Test for getting autocmds for all the patterns in a group
3521 call assert_equal(expected, autocmd_get(#{group: 'TestAutoCmdFns',
3522 \ event: '*'}))
3523
3524 " Test for getting autocmds for an event in a group
3525 let expected = [
3526 \ #{cmd: 'echo "bufadd-vim"', group: 'TestAutoCmdFns',
3527 \ pattern: '*.vim', nested: v:false, once: v:false,
3528 \ event: 'BufAdd'},
3529 \ #{cmd: 'echo "bufadd-py"', group: 'TestAutoCmdFns',
3530 \ pattern: '*.py', nested: v:false, once: v:false,
3531 \ event: 'BufAdd'}]
3532 call assert_equal(expected, autocmd_get(#{group: 'TestAutoCmdFns',
3533 \ event: 'BufAdd'}))
3534
3535 " Test for getting the autocmds for all the events in a group for particular
3536 " pattern
3537 call assert_equal([{'cmd': 'echo "bufadd-py"', 'group': 'TestAutoCmdFns',
3538 \ 'pattern': '*.py', 'nested': v:false, 'once': v:false,
3539 \ 'event': 'BufAdd'}],
3540 \ autocmd_get(#{group: 'TestAutoCmdFns', event: '*', pattern: '*.py'}))
3541
3542 " Test for getting the autocmds for an events in a group for particular
3543 " pattern
3544 let l = autocmd_get(#{group: 'TestAutoCmdFns', event: 'BufAdd',
3545 \ pattern: '*.vim'})
3546 call assert_equal([
3547 \ #{cmd: 'echo "bufadd-vim"', group: 'TestAutoCmdFns',
3548 \ pattern: '*.vim', nested: v:false, once: v:false,
3549 \ event: 'BufAdd'}], l)
3550
3551 " Test for getting the autocmds for a pattern in a group
3552 let l = autocmd_get(#{group: 'TestAutoCmdFns', pattern: '*.vim'})
3553 call assert_equal([
3554 \ #{cmd: 'echo "bufadd-vim"', group: 'TestAutoCmdFns',
3555 \ pattern: '*.vim', nested: v:false, once: v:false,
3556 \ event: 'BufAdd'},
3557 \ #{cmd: 'echo "bufhidden"', group: 'TestAutoCmdFns',
3558 \ pattern: '*.vim', nested: v:false,
3559 \ once: v:false, event: 'BufHidden'}], l)
3560
3561 " Test for getting the autocmds for a pattern in all the groups
3562 let l = autocmd_get(#{pattern: '*.a1b2c3'})
3563 call assert_equal([{'cmd': 'echo "bufadd-vim-2"', 'group': 'TestAutoCmdFns2',
3564 \ 'pattern': '*.a1b2c3', 'nested': v:false, 'once': v:false,
3565 \ 'event': 'BufRead'}], l)
3566
3567 " Test for getting autocmds for a pattern without any autocmds
3568 call assert_equal([], autocmd_get(#{group: 'TestAutoCmdFns',
3569 \ pattern: '*.abc'}))
3570 call assert_equal([], autocmd_get(#{group: 'TestAutoCmdFns',
3571 \ event: 'BufAdd', pattern: '*.abc'}))
3572 call assert_equal([], autocmd_get(#{group: 'TestAutoCmdFns',
3573 \ event: 'BufWipeout'}))
3574 call assert_fails("call autocmd_get(#{group: 'abc', event: 'BufAdd'})",
3575 \ 'E367:')
3576 let cmd = "echo autocmd_get(#{group: 'TestAutoCmdFns', event: 'abc'})"
3577 call assert_fails(cmd, 'E216:')
3578 call assert_fails("call autocmd_get(#{group: 'abc'})", 'E367:')
3579 call assert_fails("echo autocmd_get(#{event: 'abc'})", 'E216:')
3580
3581 augroup TestAutoCmdFns
3582 au!
3583 augroup END
3584 call assert_equal([], autocmd_get(#{group: 'TestAutoCmdFns'}))
3585
3586 " Test for nested and once autocmds
3587 augroup TestAutoCmdFns
3588 au!
3589 autocmd VimSuspend * ++nested echo "suspend"
3590 autocmd VimResume * ++once echo "resume"
3591 augroup END
3592
3593 let expected = [
3594 \ {'cmd': 'echo "suspend"', 'group': 'TestAutoCmdFns', 'pattern': '*',
3595 \ 'nested': v:true, 'once': v:false, 'event': 'VimSuspend'},
3596 \ {'cmd': 'echo "resume"', 'group': 'TestAutoCmdFns', 'pattern': '*',
3597 \ 'nested': v:false, 'once': v:true, 'event': 'VimResume'}]
3598 call assert_equal(expected, autocmd_get(#{group: 'TestAutoCmdFns'}))
3599
3600 " Test for buffer-local autocmd
3601 augroup TestAutoCmdFns
3602 au!
3603 autocmd TextYankPost <buffer> echo "textyankpost"
3604 augroup END
3605
3606 let expected = [
3607 \ {'cmd': 'echo "textyankpost"', 'group': 'TestAutoCmdFns',
3608 \ 'pattern': '<buffer=' .. bufnr() .. '>', 'nested': v:false,
3609 \ 'once': v:false, 'bufnr': bufnr(), 'event': 'TextYankPost'}]
3610 call assert_equal(expected, autocmd_get(#{group: 'TestAutoCmdFns'}))
3611
3612 augroup TestAutoCmdFns
3613 au!
3614 augroup END
3615 augroup! TestAutoCmdFns
3616 augroup TestAutoCmdFns2
3617 au!
3618 augroup END
3619 augroup! TestAutoCmdFns2
3620
3621 call assert_fails("echo autocmd_get(#{group: []})", 'E730:')
3622 call assert_fails("echo autocmd_get(#{event: {}})", 'E731:')
3623 call assert_fails("echo autocmd_get([])", 'E1206:')
3624endfunc
3625
3626" Test for the autocmd_add() function
3627func Test_autocmd_add()
3628 " Define a single autocmd in a group
3629 call autocmd_add([#{group: 'TestAcSet', event: 'BufAdd', pattern: '*.sh',
3630 \ cmd: 'echo "bufadd"', once: v:true, nested: v:true}])
3631 call assert_equal([#{cmd: 'echo "bufadd"', group: 'TestAcSet',
3632 \ pattern: '*.sh', nested: v:true, once: v:true,
3633 \ event: 'BufAdd'}], autocmd_get(#{group: 'TestAcSet'}))
3634
3635 " Define two autocmds in the same group
3636 call autocmd_delete([#{group: 'TestAcSet'}])
3637 call autocmd_add([#{group: 'TestAcSet', event: 'BufAdd', pattern: '*.sh',
3638 \ cmd: 'echo "bufadd"'},
3639 \ #{group: 'TestAcSet', event: 'BufEnter', pattern: '*.sh',
3640 \ cmd: 'echo "bufenter"'}])
3641 call assert_equal([
3642 \ #{cmd: 'echo "bufadd"', group: 'TestAcSet', pattern: '*.sh',
3643 \ nested: v:false, once: v:false, event: 'BufAdd'},
3644 \ #{cmd: 'echo "bufenter"', group: 'TestAcSet', pattern: '*.sh',
3645 \ nested: v:false, once: v:false, event: 'BufEnter'}],
3646 \ autocmd_get(#{group: 'TestAcSet'}))
3647
3648 " Define a buffer-local autocmd
3649 call autocmd_delete([#{group: 'TestAcSet'}])
3650 call autocmd_add([#{group: 'TestAcSet', event: 'CursorHold',
3651 \ bufnr: bufnr(), cmd: 'echo "cursorhold"'}])
3652 call assert_equal([
3653 \ #{cmd: 'echo "cursorhold"', group: 'TestAcSet',
3654 \ pattern: '<buffer=' .. bufnr() .. '>', nested: v:false,
3655 \ once: v:false, bufnr: bufnr(), event: 'CursorHold'}],
3656 \ autocmd_get(#{group: 'TestAcSet'}))
3657
3658 " Use an invalid buffer number
3659 call autocmd_delete([#{group: 'TestAcSet'}])
3660 call autocmd_add([#{group: 'TestAcSet', event: 'BufEnter',
3661 \ bufnr: -1, cmd: 'echo "bufenter"'}])
3662 let l = [#{group: 'TestAcSet', event: 'BufAdd', bufnr: 9999,
3663 \ cmd: 'echo "bufadd"'}]
3664 call assert_fails("echo autocmd_add(l)", 'E680:')
Yegappan Lakshmanan00e977c2022-06-01 12:31:53 +01003665 let l = [#{group: 'TestAcSet', event: 'BufAdd', bufnr: 9999,
3666 \ pattern: '*.py', cmd: 'echo "bufadd"'}]
3667 call assert_fails("echo autocmd_add(l)", 'E680:')
3668 let l = [#{group: 'TestAcSet', event: 'BufAdd', bufnr: 9999,
3669 \ pattern: ['*.py', '*.c'], cmd: 'echo "bufadd"'}]
3670 call assert_fails("echo autocmd_add(l)", 'E680:')
Yegappan Lakshmanan1755a912022-05-19 10:31:47 +01003671 let l = [#{group: 'TestAcSet', event: 'BufRead', bufnr: [],
3672 \ cmd: 'echo "bufread"'}]
3673 call assert_fails("echo autocmd_add(l)", 'E745:')
3674 call assert_equal([], autocmd_get(#{group: 'TestAcSet'}))
3675
3676 " Add two commands to the same group, event and pattern
3677 call autocmd_delete([#{group: 'TestAcSet'}])
3678 call autocmd_add([#{group: 'TestAcSet', event: 'BufUnload',
3679 \ pattern: 'abc', cmd: 'echo "cmd1"'}])
3680 call autocmd_add([#{group: 'TestAcSet', event: 'BufUnload',
3681 \ pattern: 'abc', cmd: 'echo "cmd2"'}])
3682 call assert_equal([
3683 \ #{cmd: 'echo "cmd1"', group: 'TestAcSet', pattern: 'abc',
3684 \ nested: v:false, once: v:false, event: 'BufUnload'},
3685 \ #{cmd: 'echo "cmd2"', group: 'TestAcSet', pattern: 'abc',
3686 \ nested: v:false, once: v:false, event: 'BufUnload'}],
3687 \ autocmd_get(#{group: 'TestAcSet'}))
3688
3689 " When adding a new autocmd, if the autocmd 'group' is not specified, then
3690 " the current autocmd group should be used.
3691 call autocmd_delete([#{group: 'TestAcSet'}])
3692 augroup TestAcSet
3693 call autocmd_add([#{event: 'BufHidden', pattern: 'abc', cmd: 'echo "abc"'}])
3694 augroup END
3695 call assert_equal([
3696 \ #{cmd: 'echo "abc"', group: 'TestAcSet', pattern: 'abc',
3697 \ nested: v:false, once: v:false, event: 'BufHidden'}],
3698 \ autocmd_get(#{group: 'TestAcSet'}))
3699
Yegappan Lakshmanan971f6822022-05-24 11:40:11 +01003700 " Test for replacing a cmd for an event in a group
3701 call autocmd_delete([#{group: 'TestAcSet'}])
3702 call autocmd_add([#{replace: v:true, group: 'TestAcSet', event: 'BufEnter',
3703 \ pattern: '*.py', cmd: 'echo "bufenter"'}])
3704 call autocmd_add([#{replace: v:true, group: 'TestAcSet', event: 'BufEnter',
3705 \ pattern: '*.py', cmd: 'echo "bufenter"'}])
3706 call assert_equal([
3707 \ #{cmd: 'echo "bufenter"', group: 'TestAcSet', pattern: '*.py',
3708 \ nested: v:false, once: v:false, event: 'BufEnter'}],
3709 \ autocmd_get(#{group: 'TestAcSet'}))
3710
3711 " Test for adding a command for an unsupported autocmd event
Yegappan Lakshmanan1755a912022-05-19 10:31:47 +01003712 let l = [#{group: 'TestAcSet', event: 'abc', pattern: '*.sh',
3713 \ cmd: 'echo "bufadd"'}]
3714 call assert_fails('call autocmd_add(l)', 'E216:')
3715
Yegappan Lakshmanane0ff3a72022-05-27 18:05:33 +01003716 " Test for using a list of events and patterns
3717 call autocmd_delete([#{group: 'TestAcSet'}])
3718 let l = [#{group: 'TestAcSet', event: ['BufEnter', 'BufLeave'],
3719 \ pattern: ['*.py', '*.sh'], cmd: 'echo "bufcmds"'}]
3720 call autocmd_add(l)
3721 call assert_equal([
3722 \ #{cmd: 'echo "bufcmds"', group: 'TestAcSet', pattern: '*.py',
3723 \ nested: v:false, once: v:false, event: 'BufEnter'},
3724 \ #{cmd: 'echo "bufcmds"', group: 'TestAcSet', pattern: '*.sh',
3725 \ nested: v:false, once: v:false, event: 'BufEnter'},
3726 \ #{cmd: 'echo "bufcmds"', group: 'TestAcSet', pattern: '*.py',
3727 \ nested: v:false, once: v:false, event: 'BufLeave'},
3728 \ #{cmd: 'echo "bufcmds"', group: 'TestAcSet', pattern: '*.sh',
3729 \ nested: v:false, once: v:false, event: 'BufLeave'}],
3730 \ autocmd_get(#{group: 'TestAcSet'}))
3731
3732 " Test for invalid values for 'event' item
3733 call autocmd_delete([#{group: 'TestAcSet'}])
3734 let l = [#{group: 'TestAcSet', event: test_null_string(),
3735 \ pattern: "*.py", cmd: 'echo "bufcmds"'}]
3736 call assert_fails('call autocmd_add(l)', 'E928:')
3737 let l = [#{group: 'TestAcSet', event: test_null_list(),
3738 \ pattern: "*.py", cmd: 'echo "bufcmds"'}]
3739 call assert_fails('call autocmd_add(l)', 'E714:')
3740 let l = [#{group: 'TestAcSet', event: {},
3741 \ pattern: "*.py", cmd: 'echo "bufcmds"'}]
3742 call assert_fails('call autocmd_add(l)', 'E777:')
3743 let l = [#{group: 'TestAcSet', event: [{}],
3744 \ pattern: "*.py", cmd: 'echo "bufcmds"'}]
3745 call assert_fails('call autocmd_add(l)', 'E928:')
3746 let l = [#{group: 'TestAcSet', event: [test_null_string()],
3747 \ pattern: "*.py", cmd: 'echo "bufcmds"'}]
3748 call assert_fails('call autocmd_add(l)', 'E928:')
3749 let l = [#{group: 'TestAcSet', event: 'BufEnter,BufLeave',
3750 \ pattern: '*.py', cmd: 'echo "bufcmds"'}]
3751 call assert_fails('call autocmd_add(l)', 'E216:')
3752 let l = [#{group: 'TestAcSet', event: [],
3753 \ pattern: "*.py", cmd: 'echo "bufcmds"'}]
3754 call autocmd_add(l)
3755 let l = [#{group: 'TestAcSet', event: [""],
3756 \ pattern: "*.py", cmd: 'echo "bufcmds"'}]
3757 call assert_fails('call autocmd_add(l)', 'E216:')
3758 let l = [#{group: 'TestAcSet', event: "",
3759 \ pattern: "*.py", cmd: 'echo "bufcmds"'}]
3760 call autocmd_add(l)
3761 call assert_equal([], autocmd_get(#{group: 'TestAcSet'}))
3762
3763 " Test for invalid values for 'pattern' item
3764 let l = [#{group: 'TestAcSet', event: "BufEnter",
3765 \ pattern: test_null_string(), cmd: 'echo "bufcmds"'}]
Yegappan Lakshmanan00e977c2022-06-01 12:31:53 +01003766 call assert_fails('call autocmd_add(l)', 'E928:')
Yegappan Lakshmanane0ff3a72022-05-27 18:05:33 +01003767 let l = [#{group: 'TestAcSet', event: "BufEnter",
3768 \ pattern: test_null_list(), cmd: 'echo "bufcmds"'}]
3769 call assert_fails('call autocmd_add(l)', 'E714:')
3770 let l = [#{group: 'TestAcSet', event: "BufEnter",
3771 \ pattern: {}, cmd: 'echo "bufcmds"'}]
3772 call assert_fails('call autocmd_add(l)', 'E777:')
3773 let l = [#{group: 'TestAcSet', event: "BufEnter",
3774 \ pattern: [{}], cmd: 'echo "bufcmds"'}]
3775 call assert_fails('call autocmd_add(l)', 'E928:')
3776 let l = [#{group: 'TestAcSet', event: "BufEnter",
3777 \ pattern: [test_null_string()], cmd: 'echo "bufcmds"'}]
3778 call assert_fails('call autocmd_add(l)', 'E928:')
3779 let l = [#{group: 'TestAcSet', event: "BufEnter",
3780 \ pattern: [], cmd: 'echo "bufcmds"'}]
3781 call autocmd_add(l)
3782 let l = [#{group: 'TestAcSet', event: "BufEnter",
3783 \ pattern: [""], cmd: 'echo "bufcmds"'}]
3784 call autocmd_add(l)
3785 let l = [#{group: 'TestAcSet', event: "BufEnter",
3786 \ pattern: "", cmd: 'echo "bufcmds"'}]
3787 call autocmd_add(l)
3788 call assert_equal([], autocmd_get(#{group: 'TestAcSet'}))
3789
3790 let l = [#{group: 'TestAcSet', event: 'BufEnter,abc,BufLeave',
3791 \ pattern: '*.py', cmd: 'echo "bufcmds"'}]
3792 call assert_fails('call autocmd_add(l)', 'E216:')
3793
Yegappan Lakshmanan1755a912022-05-19 10:31:47 +01003794 call assert_fails("call autocmd_add({})", 'E1211:')
3795 call assert_equal(v:false, autocmd_add(test_null_list()))
3796 call assert_true(autocmd_add([[]]))
3797 call assert_true(autocmd_add([test_null_dict()]))
3798
3799 augroup TestAcSet
3800 au!
3801 augroup END
3802
3803 call autocmd_add([#{group: 'TestAcSet'}])
3804 call autocmd_add([#{group: 'TestAcSet', event: 'BufAdd'}])
3805 call autocmd_add([#{group: 'TestAcSet', pat: '*.sh'}])
3806 call autocmd_add([#{group: 'TestAcSet', cmd: 'echo "a"'}])
3807 call autocmd_add([#{group: 'TestAcSet', event: 'BufAdd', pat: '*.sh'}])
3808 call autocmd_add([#{group: 'TestAcSet', event: 'BufAdd', cmd: 'echo "a"'}])
3809 call autocmd_add([#{group: 'TestAcSet', pat: '*.sh', cmd: 'echo "a"'}])
3810 call assert_equal([], autocmd_get(#{group: 'TestAcSet'}))
3811
3812 augroup! TestAcSet
3813endfunc
3814
3815" Test for deleting autocmd events and groups
3816func Test_autocmd_delete()
3817 " Delete an event in an autocmd group
3818 augroup TestAcSet
3819 au!
3820 au BufAdd *.sh echo "bufadd"
3821 au BufEnter *.sh echo "bufenter"
3822 augroup END
3823 call autocmd_delete([#{group: 'TestAcSet', event: 'BufAdd'}])
3824 call assert_equal([#{cmd: 'echo "bufenter"', group: 'TestAcSet',
3825 \ pattern: '*.sh', nested: v:false, once: v:false,
3826 \ event: 'BufEnter'}], autocmd_get(#{group: 'TestAcSet'}))
3827
3828 " Delete all the events in an autocmd group
3829 augroup TestAcSet
3830 au BufAdd *.sh echo "bufadd"
3831 augroup END
3832 call autocmd_delete([#{group: 'TestAcSet', event: '*'}])
3833 call assert_equal([], autocmd_get(#{group: 'TestAcSet'}))
3834
3835 " Delete a non-existing autocmd group
3836 call assert_fails("call autocmd_delete([#{group: 'abc'}])", 'E367:')
3837 " Delete a non-existing autocmd event
3838 let l = [#{group: 'TestAcSet', event: 'abc'}]
3839 call assert_fails("call autocmd_delete(l)", 'E216:')
3840 " Delete a non-existing autocmd pattern
3841 let l = [#{group: 'TestAcSet', event: 'BufAdd', pat: 'abc'}]
3842 call assert_true(autocmd_delete(l))
Yegappan Lakshmanan00e977c2022-06-01 12:31:53 +01003843 " Delete an autocmd for a non-existing buffer
3844 let l = [#{event: '*', bufnr: 9999, cmd: 'echo "x"'}]
3845 call assert_fails('call autocmd_delete(l)', 'E680:')
Yegappan Lakshmanan1755a912022-05-19 10:31:47 +01003846
3847 " Delete an autocmd group
3848 augroup TestAcSet
3849 au!
3850 au BufAdd *.sh echo "bufadd"
3851 au BufEnter *.sh echo "bufenter"
3852 augroup END
3853 call autocmd_delete([#{group: 'TestAcSet'}])
3854 call assert_fails("call autocmd_get(#{group: 'TestAcSet'})", 'E367:')
3855
3856 call assert_true(autocmd_delete([[]]))
3857 call assert_true(autocmd_delete([test_null_dict()]))
3858endfunc
3859
Bram Moolenaar8f3c3c62022-10-18 17:05:54 +01003860func Test_autocmd_split_dummy()
3861 " Autocommand trying to split a window containing a dummy buffer.
3862 auto BufReadPre * exe "sbuf " .. expand("<abuf>")
3863 " Avoid the "W11" prompt
3864 au FileChangedShell * let v:fcs_choice = 'reload'
3865 func Xautocmd_changelist()
3866 cal writefile(['Xtestfile2:4:4'], 'Xerr')
3867 edit Xerr
3868 lex 'Xtestfile2:4:4'
3869 endfunc
3870 call Xautocmd_changelist()
Bram Moolenaar53c5c9f2022-10-18 17:25:03 +01003871 " Should get E86, but it doesn't always happen (timing?)
3872 silent! call Xautocmd_changelist()
Bram Moolenaar8f3c3c62022-10-18 17:05:54 +01003873
3874 au! BufReadPre
3875 au! FileChangedShell
3876 delfunc Xautocmd_changelist
3877 bwipe! Xerr
3878 call delete('Xerr')
3879endfunc
3880
Bram Moolenaarbc2b71d2020-02-17 21:33:30 +01003881" vim: shiftwidth=2 sts=2 expandtab