blob: f2f3725b3d770214e683bead59edcbf79607cbe3 [file] [log] [blame]
Bram Moolenaar14735512016-03-26 21:00:08 +01001" Tests for autocommands
2
Bram Moolenaar8c64a362018-03-23 22:39:31 +01003source shared.vim
Bram Moolenaar8c5a2782019-08-07 23:07:07 +02004source check.vim
Bram Moolenaarcadbe1b2019-09-22 21:50:09 +02005source term_util.vim
LemonBoy09371822022-04-08 15:18:45 +01006source screendump.vim
Bram Moolenaar6f2465d2022-03-22 18:13:01 +00007import './vim9.vim' as v9
Bram Moolenaar8c64a362018-03-23 22:39:31 +01008
Bram Moolenaar1e115362019-01-09 23:01:02 +01009func s:cleanup_buffers() abort
Bram Moolenaarb3435b02016-09-29 20:54:59 +020010 for bnr in range(1, bufnr('$'))
11 if bufloaded(bnr) && bufnr('%') != bnr
12 execute 'bd! ' . bnr
13 endif
14 endfor
Bram Moolenaar04f62f82017-07-19 18:18:39 +020015endfunc
Bram Moolenaarb3435b02016-09-29 20:54:59 +020016
Bram Moolenaar14735512016-03-26 21:00:08 +010017func Test_vim_did_enter()
18 call assert_false(v:vim_did_enter)
19
20 " This script will never reach the main loop, can't check if v:vim_did_enter
21 " becomes one.
22endfunc
Bram Moolenaar40b1b542016-04-20 20:18:23 +020023
Bram Moolenaar75911162020-07-21 19:44:47 +020024" Test for the CursorHold autocmd
25func Test_CursorHold_autocmd()
26 CheckRunVimInTerminal
Bram Moolenaare1f3ab72022-09-04 21:29:08 +010027 call writefile(['one', 'two', 'three'], 'XoneTwoThree', 'D')
Bram Moolenaar75911162020-07-21 19:44:47 +020028 let before =<< trim END
29 set updatetime=10
Bram Moolenaare7cda972022-08-29 11:02:59 +010030 au CursorHold * call writefile([line('.')], 'XCHoutput', 'a')
Bram Moolenaar75911162020-07-21 19:44:47 +020031 END
Bram Moolenaare1f3ab72022-09-04 21:29:08 +010032 call writefile(before, 'XCHinit', 'D')
Bram Moolenaare7cda972022-08-29 11:02:59 +010033 let buf = RunVimInTerminal('-S XCHinit XoneTwoThree', {})
Bram Moolenaar17f67542020-08-20 18:29:13 +020034 call term_sendkeys(buf, "G")
Bram Moolenaar62cd26a2020-10-11 20:08:44 +020035 call term_wait(buf, 50)
Bram Moolenaar75911162020-07-21 19:44:47 +020036 call term_sendkeys(buf, "gg")
37 call term_wait(buf)
Bram Moolenaare7cda972022-08-29 11:02:59 +010038 call WaitForAssert({-> assert_equal(['1'], readfile('XCHoutput')[-1:-1])})
Bram Moolenaar75911162020-07-21 19:44:47 +020039 call term_sendkeys(buf, "j")
40 call term_wait(buf)
Bram Moolenaare7cda972022-08-29 11:02:59 +010041 call WaitForAssert({-> assert_equal(['1', '2'], readfile('XCHoutput')[-2:-1])})
Bram Moolenaar75911162020-07-21 19:44:47 +020042 call term_sendkeys(buf, "j")
43 call term_wait(buf)
Bram Moolenaare7cda972022-08-29 11:02:59 +010044 call WaitForAssert({-> assert_equal(['1', '2', '3'], readfile('XCHoutput')[-3:-1])})
Bram Moolenaar75911162020-07-21 19:44:47 +020045 call StopVimInTerminal(buf)
46
Bram Moolenaare7cda972022-08-29 11:02:59 +010047 call delete('XCHoutput')
Bram Moolenaar75911162020-07-21 19:44:47 +020048endfunc
49
Bram Moolenaarc67e8922016-05-24 16:07:40 +020050if has('timers')
Bram Moolenaar97b00752019-05-12 13:07:14 +020051
Bram Moolenaarc67e8922016-05-24 16:07:40 +020052 func ExitInsertMode(id)
53 call feedkeys("\<Esc>")
54 endfunc
55
56 func Test_cursorhold_insert()
Bram Moolenaarf18c4db2016-09-08 22:10:06 +020057 " Need to move the cursor.
58 call feedkeys("ggG", "xt")
59
Bram Moolenaarc67e8922016-05-24 16:07:40 +020060 let g:triggered = 0
61 au CursorHoldI * let g:triggered += 1
62 set updatetime=20
Bram Moolenaar92bb83e2021-02-03 23:04:46 +010063 call timer_start(200, 'ExitInsertMode')
Bram Moolenaarc67e8922016-05-24 16:07:40 +020064 call feedkeys('a', 'x!')
Bram Moolenaar3b014be2022-11-13 17:53:46 +000065 sleep 30m
Bram Moolenaarc67e8922016-05-24 16:07:40 +020066 call assert_equal(1, g:triggered)
Bram Moolenaar26d98212019-01-27 22:32:55 +010067 unlet g:triggered
68 au! CursorHoldI
69 set updatetime&
70 endfunc
71
72 func Test_cursorhold_insert_with_timer_interrupt()
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +020073 CheckFeature job
Bram Moolenaar26d98212019-01-27 22:32:55 +010074 " Need to move the cursor.
75 call feedkeys("ggG", "xt")
76
77 " Confirm the timer invoked in exit_cb of the job doesn't disturb
78 " CursorHoldI event.
79 let g:triggered = 0
80 au CursorHoldI * let g:triggered += 1
Bram Moolenaar62cd26a2020-10-11 20:08:44 +020081 set updatetime=100
Bram Moolenaar26d98212019-01-27 22:32:55 +010082 call job_start(has('win32') ? 'cmd /c echo:' : 'echo',
Bram Moolenaar62cd26a2020-10-11 20:08:44 +020083 \ {'exit_cb': {-> timer_start(200, 'ExitInsertMode')}})
Bram Moolenaar26d98212019-01-27 22:32:55 +010084 call feedkeys('a', 'x!')
85 call assert_equal(1, g:triggered)
86 unlet g:triggered
Bram Moolenaare99e8442016-07-26 20:43:40 +020087 au! CursorHoldI
Bram Moolenaaraeac9002016-09-06 22:15:08 +020088 set updatetime&
Bram Moolenaarc67e8922016-05-24 16:07:40 +020089 endfunc
90
91 func Test_cursorhold_insert_ctrl_x()
92 let g:triggered = 0
93 au CursorHoldI * let g:triggered += 1
94 set updatetime=20
95 call timer_start(100, 'ExitInsertMode')
96 " CursorHoldI does not trigger after CTRL-X
97 call feedkeys("a\<C-X>", 'x!')
98 call assert_equal(0, g:triggered)
Bram Moolenaar26d98212019-01-27 22:32:55 +010099 unlet g:triggered
Bram Moolenaare99e8442016-07-26 20:43:40 +0200100 au! CursorHoldI
Bram Moolenaaraeac9002016-09-06 22:15:08 +0200101 set updatetime&
Bram Moolenaarc67e8922016-05-24 16:07:40 +0200102 endfunc
Bram Moolenaar97b00752019-05-12 13:07:14 +0200103
Bram Moolenaar5a9357d2021-10-03 16:22:05 +0100104 func Test_cursorhold_insert_ctrl_g_U()
105 au CursorHoldI * :
106 set updatetime=20
107 new
108 call timer_start(100, { -> feedkeys("\<Left>foo\<Esc>", 't') })
109 call feedkeys("i()\<C-g>U", 'tx!')
110 sleep 200m
111 call assert_equal('(foo)', getline(1))
112 undo
113 call assert_equal('', getline(1))
114
115 bwipe!
116 au! CursorHoldI
117 set updatetime&
118 endfunc
119
Bram Moolenaar97b00752019-05-12 13:07:14 +0200120 func Test_OptionSet_modeline()
121 call test_override('starting', 1)
122 au! OptionSet
123 augroup set_tabstop
124 au OptionSet tabstop call timer_start(1, {-> execute("echo 'Handler called'", "")})
125 augroup END
Bram Moolenaare1f3ab72022-09-04 21:29:08 +0100126 call writefile(['vim: set ts=7 sw=5 :', 'something'], 'XoptionsetModeline', 'D')
Bram Moolenaar97b00752019-05-12 13:07:14 +0200127 set modeline
128 let v:errmsg = ''
129 call assert_fails('split XoptionsetModeline', 'E12:')
130 call assert_equal(7, &ts)
131 call assert_equal('', v:errmsg)
132
133 augroup set_tabstop
134 au!
135 augroup END
136 bwipe!
137 set ts&
Bram Moolenaar97b00752019-05-12 13:07:14 +0200138 call test_override('starting', 0)
139 endfunc
140
141endif "has('timers')
Bram Moolenaar40b1b542016-04-20 20:18:23 +0200142
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200143func Test_bufunload()
Bram Moolenaarc67e8922016-05-24 16:07:40 +0200144 augroup test_bufunload_group
145 autocmd!
146 autocmd BufUnload * call add(s:li, "bufunload")
147 autocmd BufDelete * call add(s:li, "bufdelete")
148 autocmd BufWipeout * call add(s:li, "bufwipeout")
149 augroup END
Bram Moolenaar40b1b542016-04-20 20:18:23 +0200150
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100151 let s:li = []
Bram Moolenaarc67e8922016-05-24 16:07:40 +0200152 new
153 setlocal bufhidden=
154 bunload
155 call assert_equal(["bufunload", "bufdelete"], s:li)
Bram Moolenaar40b1b542016-04-20 20:18:23 +0200156
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100157 let s:li = []
Bram Moolenaarc67e8922016-05-24 16:07:40 +0200158 new
159 setlocal bufhidden=delete
160 bunload
161 call assert_equal(["bufunload", "bufdelete"], s:li)
162
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100163 let s:li = []
Bram Moolenaarc67e8922016-05-24 16:07:40 +0200164 new
165 setlocal bufhidden=unload
166 bwipeout
167 call assert_equal(["bufunload", "bufdelete", "bufwipeout"], s:li)
168
Bram Moolenaare99e8442016-07-26 20:43:40 +0200169 au! test_bufunload_group
Bram Moolenaarc67e8922016-05-24 16:07:40 +0200170 augroup! test_bufunload_group
Bram Moolenaar40b1b542016-04-20 20:18:23 +0200171endfunc
Bram Moolenaar30445cb2016-07-09 15:21:02 +0200172
173" SEGV occurs in older versions. (At least 7.4.2005 or older)
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200174func Test_autocmd_bufunload_with_tabnext()
Bram Moolenaar30445cb2016-07-09 15:21:02 +0200175 tabedit
176 tabfirst
177
178 augroup test_autocmd_bufunload_with_tabnext_group
179 autocmd!
180 autocmd BufUnload <buffer> tabnext
181 augroup END
182
183 quit
184 call assert_equal(2, tabpagenr('$'))
185
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200186 autocmd! test_autocmd_bufunload_with_tabnext_group
Bram Moolenaar30445cb2016-07-09 15:21:02 +0200187 augroup! test_autocmd_bufunload_with_tabnext_group
188 tablast
189 quit
190endfunc
Bram Moolenaarc917da42016-07-19 22:31:36 +0200191
Bram Moolenaar5ed58c72021-01-28 14:24:55 +0100192func Test_argdelete_in_next()
193 au BufNew,BufEnter,BufLeave,BufWinEnter * argdel
194 call assert_fails('next a b', 'E1156:')
195 au! BufNew,BufEnter,BufLeave,BufWinEnter *
196endfunc
197
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200198func Test_autocmd_bufwinleave_with_tabfirst()
Bram Moolenaarf9e687e2016-09-04 21:33:09 +0200199 tabedit
200 augroup sample
201 autocmd!
202 autocmd BufWinLeave <buffer> tabfirst
203 augroup END
204 call setline(1, ['a', 'b', 'c'])
205 edit! a.txt
Bram Moolenaarf18c4db2016-09-08 22:10:06 +0200206 tabclose
Bram Moolenaarf9e687e2016-09-04 21:33:09 +0200207endfunc
208
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200209" SEGV occurs in older versions. (At least 7.4.2321 or older)
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200210func Test_autocmd_bufunload_avoiding_SEGV_01()
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200211 split aa.txt
212 let lastbuf = bufnr('$')
213
214 augroup test_autocmd_bufunload
215 autocmd!
216 exe 'autocmd BufUnload <buffer> ' . (lastbuf + 1) . 'bwipeout!'
217 augroup END
218
Bram Moolenaar28ee8922020-10-28 20:20:00 +0100219 call assert_fails('edit bb.txt', 'E937:')
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200220
221 autocmd! test_autocmd_bufunload
222 augroup! test_autocmd_bufunload
223 bwipe! aa.txt
224 bwipe! bb.txt
225endfunc
226
227" SEGV occurs in older versions. (At least 7.4.2321 or older)
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200228func Test_autocmd_bufunload_avoiding_SEGV_02()
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200229 setlocal buftype=nowrite
230 let lastbuf = bufnr('$')
231
232 augroup test_autocmd_bufunload
233 autocmd!
234 exe 'autocmd BufUnload <buffer> ' . (lastbuf + 1) . 'bwipeout!'
235 augroup END
236
237 normal! i1
238 call assert_fails('edit a.txt', 'E517:')
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200239
240 autocmd! test_autocmd_bufunload
241 augroup! test_autocmd_bufunload
242 bwipe! a.txt
243endfunc
244
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100245func Test_autocmd_dummy_wipeout()
246 " prepare files
Bram Moolenaare1f3ab72022-09-04 21:29:08 +0100247 call writefile([''], 'Xdummywipetest1.txt', 'D')
248 call writefile([''], 'Xdummywipetest2.txt', 'D')
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100249 augroup test_bufunload_group
250 autocmd!
251 autocmd BufUnload * call add(s:li, "bufunload")
252 autocmd BufDelete * call add(s:li, "bufdelete")
253 autocmd BufWipeout * call add(s:li, "bufwipeout")
254 augroup END
255
256 let s:li = []
257 split Xdummywipetest1.txt
258 silent! vimgrep /notmatched/ Xdummywipetest*
259 call assert_equal(["bufunload", "bufwipeout"], s:li)
260
261 bwipeout
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100262 au! test_bufunload_group
263 augroup! test_bufunload_group
264endfunc
265
Bram Moolenaarc917da42016-07-19 22:31:36 +0200266func Test_win_tab_autocmd()
267 let g:record = []
268
269 augroup testing
270 au WinNew * call add(g:record, 'WinNew')
naohiro ono23beefe2021-11-13 12:38:49 +0000271 au WinClosed * call add(g:record, 'WinClosed')
Bram Moolenaarc917da42016-07-19 22:31:36 +0200272 au WinEnter * call add(g:record, 'WinEnter')
273 au WinLeave * call add(g:record, 'WinLeave')
274 au TabNew * call add(g:record, 'TabNew')
Bram Moolenaar12c11d52016-07-19 23:13:03 +0200275 au TabClosed * call add(g:record, 'TabClosed')
Bram Moolenaarc917da42016-07-19 22:31:36 +0200276 au TabEnter * call add(g:record, 'TabEnter')
277 au TabLeave * call add(g:record, 'TabLeave')
278 augroup END
279
280 split
281 tabnew
282 close
283 close
284
285 call assert_equal([
286 \ 'WinLeave', 'WinNew', 'WinEnter',
287 \ 'WinLeave', 'TabLeave', 'WinNew', 'WinEnter', 'TabNew', 'TabEnter',
naohiro ono23beefe2021-11-13 12:38:49 +0000288 \ 'WinLeave', 'TabLeave', 'WinClosed', 'TabClosed', 'WinEnter', 'TabEnter',
289 \ 'WinLeave', 'WinClosed', 'WinEnter'
Bram Moolenaarc917da42016-07-19 22:31:36 +0200290 \ ], g:record)
291
Bram Moolenaar12c11d52016-07-19 23:13:03 +0200292 let g:record = []
293 tabnew somefile
294 tabnext
295 bwipe somefile
296
297 call assert_equal([
298 \ 'WinLeave', 'TabLeave', 'WinNew', 'WinEnter', 'TabNew', 'TabEnter',
299 \ 'WinLeave', 'TabLeave', 'WinEnter', 'TabEnter',
naohiro ono23beefe2021-11-13 12:38:49 +0000300 \ 'WinClosed', 'TabClosed'
Bram Moolenaar12c11d52016-07-19 23:13:03 +0200301 \ ], g:record)
302
Bram Moolenaarc917da42016-07-19 22:31:36 +0200303 augroup testing
304 au!
305 augroup END
306 unlet g:record
307endfunc
Bram Moolenaare99e8442016-07-26 20:43:40 +0200308
LemonBoy09371822022-04-08 15:18:45 +0100309func Test_WinScrolled()
310 CheckRunVimInTerminal
311
312 let lines =<< trim END
zeertzjqd58862d2022-04-12 11:32:48 +0100313 set nowrap scrolloff=0
314 for ii in range(1, 18)
315 call setline(ii, repeat(nr2char(96 + ii), ii * 2))
316 endfor
317 let win_id = win_getid()
318 let g:matched = v:false
319 execute 'au WinScrolled' win_id 'let g:matched = v:true'
320 let g:scrolled = 0
321 au WinScrolled * let g:scrolled += 1
322 au WinScrolled * let g:amatch = str2nr(expand('<amatch>'))
323 au WinScrolled * let g:afile = str2nr(expand('<afile>'))
LemonBoy09371822022-04-08 15:18:45 +0100324 END
Bram Moolenaare1f3ab72022-09-04 21:29:08 +0100325 call writefile(lines, 'Xtest_winscrolled', 'D')
LemonBoy09371822022-04-08 15:18:45 +0100326 let buf = RunVimInTerminal('-S Xtest_winscrolled', {'rows': 6})
327
328 call term_sendkeys(buf, ":echo g:scrolled\<CR>")
329 call WaitForAssert({-> assert_match('^0 ', term_getline(buf, 6))}, 1000)
330
331 " Scroll left/right in Normal mode.
332 call term_sendkeys(buf, "zlzh:echo g:scrolled\<CR>")
333 call WaitForAssert({-> assert_match('^2 ', term_getline(buf, 6))}, 1000)
334
335 " Scroll up/down in Normal mode.
336 call term_sendkeys(buf, "\<c-e>\<c-y>:echo g:scrolled\<CR>")
337 call WaitForAssert({-> assert_match('^4 ', term_getline(buf, 6))}, 1000)
338
339 " Scroll up/down in Insert mode.
340 call term_sendkeys(buf, "Mi\<c-x>\<c-e>\<Esc>i\<c-x>\<c-y>\<Esc>")
341 call term_sendkeys(buf, ":echo g:scrolled\<CR>")
342 call WaitForAssert({-> assert_match('^6 ', term_getline(buf, 6))}, 1000)
343
344 " Scroll the window horizontally to focus the last letter of the third line
345 " containing only six characters. Moving to the previous and shorter lines
346 " should trigger another autocommand as Vim has to make them visible.
347 call term_sendkeys(buf, "5zl2k")
348 call term_sendkeys(buf, ":echo g:scrolled\<CR>")
349 call WaitForAssert({-> assert_match('^8 ', term_getline(buf, 6))}, 1000)
350
351 " Ensure the command was triggered for the specified window ID.
352 call term_sendkeys(buf, ":echo g:matched\<CR>")
353 call WaitForAssert({-> assert_match('^v:true ', term_getline(buf, 6))}, 1000)
354
355 " Ensure the expansion of <amatch> and <afile> matches the window ID.
356 call term_sendkeys(buf, ":echo g:amatch == win_id && g:afile == win_id\<CR>")
357 call WaitForAssert({-> assert_match('^v:true ', term_getline(buf, 6))}, 1000)
358
359 call StopVimInTerminal(buf)
LemonBoy09371822022-04-08 15:18:45 +0100360endfunc
361
LemonBoy66e13ae2022-04-21 22:52:11 +0100362func Test_WinScrolled_mouse()
363 CheckRunVimInTerminal
364
365 let lines =<< trim END
366 set nowrap scrolloff=0
367 set mouse=a term=xterm ttymouse=sgr mousetime=200 clipboard=
368 call setline(1, ['foo']->repeat(32))
369 split
370 let g:scrolled = 0
371 au WinScrolled * let g:scrolled += 1
372 END
Bram Moolenaare1f3ab72022-09-04 21:29:08 +0100373 call writefile(lines, 'Xtest_winscrolled_mouse', 'D')
LemonBoy66e13ae2022-04-21 22:52:11 +0100374 let buf = RunVimInTerminal('-S Xtest_winscrolled_mouse', {'rows': 10})
375
376 " With the upper split focused, send a scroll-down event to the unfocused one.
377 call test_setmouse(7, 1)
378 call term_sendkeys(buf, "\<ScrollWheelDown>")
379 call TermWait(buf)
380 call term_sendkeys(buf, ":echo g:scrolled\<CR>")
381 call WaitForAssert({-> assert_match('^1', term_getline(buf, 10))}, 1000)
382
383 " Again, but this time while we're in insert mode.
384 call term_sendkeys(buf, "i\<ScrollWheelDown>\<Esc>")
385 call TermWait(buf)
386 call term_sendkeys(buf, ":echo g:scrolled\<CR>")
387 call WaitForAssert({-> assert_match('^2', term_getline(buf, 10))}, 1000)
388
389 call StopVimInTerminal(buf)
LemonBoy66e13ae2022-04-21 22:52:11 +0100390endfunc
391
zeertzjqd58862d2022-04-12 11:32:48 +0100392func Test_WinScrolled_close_curwin()
393 CheckRunVimInTerminal
394
395 let lines =<< trim END
396 set nowrap scrolloff=0
397 call setline(1, ['aaa', 'bbb'])
398 vsplit
399 au WinScrolled * close
400 au VimLeave * call writefile(['123456'], 'Xtestout')
401 END
Bram Moolenaare1f3ab72022-09-04 21:29:08 +0100402 call writefile(lines, 'Xtest_winscrolled_close_curwin', 'D')
zeertzjqd58862d2022-04-12 11:32:48 +0100403 let buf = RunVimInTerminal('-S Xtest_winscrolled_close_curwin', {'rows': 6})
404
405 " This was using freed memory
406 call term_sendkeys(buf, "\<C-E>")
407 call TermWait(buf)
408 call StopVimInTerminal(buf)
409
410 call assert_equal(['123456'], readfile('Xtestout'))
411
zeertzjqd58862d2022-04-12 11:32:48 +0100412 call delete('Xtestout')
413endfunc
414
zeertzjq670ab032022-08-28 19:16:15 +0100415func Test_WinScrolled_long_wrapped()
416 CheckRunVimInTerminal
417
418 let lines =<< trim END
419 set scrolloff=0
420 let height = winheight(0)
421 let width = winwidth(0)
422 let g:scrolled = 0
423 au WinScrolled * let g:scrolled += 1
424 call setline(1, repeat('foo', height * width))
425 call cursor(1, height * width)
426 END
Bram Moolenaare1f3ab72022-09-04 21:29:08 +0100427 call writefile(lines, 'Xtest_winscrolled_long_wrapped', 'D')
zeertzjq670ab032022-08-28 19:16:15 +0100428 let buf = RunVimInTerminal('-S Xtest_winscrolled_long_wrapped', {'rows': 6})
429
430 call term_sendkeys(buf, ":echo g:scrolled\<CR>")
431 call WaitForAssert({-> assert_match('^0 ', term_getline(buf, 6))}, 1000)
432
433 call term_sendkeys(buf, 'gj')
434 call term_sendkeys(buf, ":echo g:scrolled\<CR>")
435 call WaitForAssert({-> assert_match('^1 ', term_getline(buf, 6))}, 1000)
436
437 call term_sendkeys(buf, '0')
438 call term_sendkeys(buf, ":echo g:scrolled\<CR>")
439 call WaitForAssert({-> assert_match('^2 ', term_getline(buf, 6))}, 1000)
440
441 call term_sendkeys(buf, '$')
442 call term_sendkeys(buf, ":echo g:scrolled\<CR>")
443 call WaitForAssert({-> assert_match('^3 ', term_getline(buf, 6))}, 1000)
zeertzjq670ab032022-08-28 19:16:15 +0100444endfunc
445
naohiro ono23beefe2021-11-13 12:38:49 +0000446func Test_WinClosed()
447 " Test that the pattern is matched against the closed window's ID, and both
448 " <amatch> and <afile> are set to it.
449 new
450 let winid = win_getid()
451 let g:matched = v:false
452 augroup test-WinClosed
453 autocmd!
454 execute 'autocmd WinClosed' winid 'let g:matched = v:true'
455 autocmd WinClosed * let g:amatch = str2nr(expand('<amatch>'))
456 autocmd WinClosed * let g:afile = str2nr(expand('<afile>'))
457 augroup END
458 close
459 call assert_true(g:matched)
460 call assert_equal(winid, g:amatch)
461 call assert_equal(winid, g:afile)
462
463 " Test that WinClosed is non-recursive.
464 new
465 new
466 call assert_equal(3, winnr('$'))
467 let g:triggered = 0
468 augroup test-WinClosed
469 autocmd!
470 autocmd WinClosed * let g:triggered += 1
471 autocmd WinClosed * 2 wincmd c
472 augroup END
473 close
474 call assert_equal(1, winnr('$'))
475 call assert_equal(1, g:triggered)
476
477 autocmd! test-WinClosed
478 augroup! test-WinClosed
479 unlet g:matched
480 unlet g:amatch
481 unlet g:afile
482 unlet g:triggered
483endfunc
484
Bram Moolenaarc947b9a2022-04-06 17:59:21 +0100485func Test_WinClosed_throws()
486 vnew
487 let bnr = bufnr()
488 call assert_equal(1, bufloaded(bnr))
489 augroup test-WinClosed
490 autocmd WinClosed * throw 'foo'
491 augroup END
492 try
493 close
494 catch /.*/
495 endtry
496 call assert_equal(0, bufloaded(bnr))
497
498 autocmd! test-WinClosed
499 augroup! test-WinClosed
500endfunc
501
zeertzjq6a069402022-04-07 14:08:29 +0100502func Test_WinClosed_throws_with_tabs()
503 tabnew
504 let bnr = bufnr()
505 call assert_equal(1, bufloaded(bnr))
506 augroup test-WinClosed
507 autocmd WinClosed * throw 'foo'
508 augroup END
509 try
510 close
511 catch /.*/
512 endtry
513 call assert_equal(0, bufloaded(bnr))
514
515 autocmd! test-WinClosed
516 augroup! test-WinClosed
517endfunc
518
zeertzjq62de54b2022-09-22 18:08:37 +0100519" This used to trigger WinClosed twice for the same window, and the window's
520" buffer was NULL in the second autocommand.
521func Test_WinClosed_switch_tab()
522 edit Xa
523 split Xb
524 split Xc
525 tab split
526 new
527 augroup test-WinClosed
528 autocmd WinClosed * tabprev | bwipe!
529 augroup END
530 close
531 " Check that the tabline has been fully removed
532 call assert_equal([1, 1], win_screenpos(0))
533
534 autocmd! test-WinClosed
535 augroup! test-WinClosed
536 %bwipe!
537endfunc
538
Bram Moolenaare99e8442016-07-26 20:43:40 +0200539func s:AddAnAutocmd()
540 augroup vimBarTest
541 au BufReadCmd * echo 'hello'
542 augroup END
543 call assert_equal(3, len(split(execute('au vimBarTest'), "\n")))
544endfunc
545
546func Test_early_bar()
547 " test that a bar is recognized before the {event}
548 call s:AddAnAutocmd()
Bram Moolenaarb8e642f2021-11-20 10:38:25 +0000549 augroup vimBarTest | au! | let done = 77 | augroup END
Bram Moolenaare99e8442016-07-26 20:43:40 +0200550 call assert_equal(1, len(split(execute('au vimBarTest'), "\n")))
Bram Moolenaarb8e642f2021-11-20 10:38:25 +0000551 call assert_equal(77, done)
Bram Moolenaare99e8442016-07-26 20:43:40 +0200552
553 call s:AddAnAutocmd()
Bram Moolenaarb8e642f2021-11-20 10:38:25 +0000554 augroup vimBarTest| au!| let done = 88 | augroup END
Bram Moolenaare99e8442016-07-26 20:43:40 +0200555 call assert_equal(1, len(split(execute('au vimBarTest'), "\n")))
Bram Moolenaarb8e642f2021-11-20 10:38:25 +0000556 call assert_equal(88, done)
Bram Moolenaare99e8442016-07-26 20:43:40 +0200557
558 " test that a bar is recognized after the {event}
559 call s:AddAnAutocmd()
Bram Moolenaarb8e642f2021-11-20 10:38:25 +0000560 augroup vimBarTest| au!BufReadCmd| let done = 99 | augroup END
Bram Moolenaare99e8442016-07-26 20:43:40 +0200561 call assert_equal(1, len(split(execute('au vimBarTest'), "\n")))
Bram Moolenaarb8e642f2021-11-20 10:38:25 +0000562 call assert_equal(99, done)
Bram Moolenaare99e8442016-07-26 20:43:40 +0200563
564 " test that a bar is recognized after the {group}
565 call s:AddAnAutocmd()
566 au! vimBarTest|echo 'hello'
567 call assert_equal(1, len(split(execute('au vimBarTest'), "\n")))
568endfunc
Bram Moolenaarf2c4c392016-07-29 20:50:24 +0200569
Bram Moolenaar5c809082016-09-01 16:21:48 +0200570func RemoveGroup()
571 autocmd! StartOK
572 augroup! StartOK
573endfunc
574
Bram Moolenaarf2c4c392016-07-29 20:50:24 +0200575func Test_augroup_warning()
576 augroup TheWarning
577 au VimEnter * echo 'entering'
578 augroup END
Bram Moolenaar5dc4e2f2020-11-25 14:15:12 +0100579 call assert_match("TheWarning.*VimEnter", execute('au VimEnter'))
Bram Moolenaarf2c4c392016-07-29 20:50:24 +0200580 redir => res
581 augroup! TheWarning
582 redir END
Bram Moolenaar5dc4e2f2020-11-25 14:15:12 +0100583 call assert_match("W19:", res)
584 call assert_match("-Deleted-.*VimEnter", execute('au VimEnter'))
Bram Moolenaarf2c4c392016-07-29 20:50:24 +0200585
586 " check "Another" does not take the pace of the deleted entry
587 augroup Another
588 augroup END
Bram Moolenaar5dc4e2f2020-11-25 14:15:12 +0100589 call assert_match("-Deleted-.*VimEnter", execute('au VimEnter'))
Bram Moolenaaraeac9002016-09-06 22:15:08 +0200590 augroup! Another
Bram Moolenaar5c809082016-09-01 16:21:48 +0200591
592 " no warning for postpone aucmd delete
593 augroup StartOK
594 au VimEnter * call RemoveGroup()
595 augroup END
Bram Moolenaar5dc4e2f2020-11-25 14:15:12 +0100596 call assert_match("StartOK.*VimEnter", execute('au VimEnter'))
Bram Moolenaar5c809082016-09-01 16:21:48 +0200597 redir => res
598 doautocmd VimEnter
599 redir END
Bram Moolenaar5dc4e2f2020-11-25 14:15:12 +0100600 call assert_notmatch("W19:", res)
Bram Moolenaarde653f02016-09-03 16:59:06 +0200601 au! VimEnter
Bram Moolenaarad48e6c2020-04-21 22:19:45 +0200602
603 call assert_fails('augroup!', 'E471:')
Bram Moolenaarf2c4c392016-07-29 20:50:24 +0200604endfunc
Bram Moolenaarb62cc362016-09-03 16:43:53 +0200605
Bram Moolenaar8d84ff12017-10-26 16:42:16 +0200606func Test_BufReadCmdHelp()
607 " This used to cause access to free memory
608 au BufReadCmd * e +h
609 help
610
Bram Moolenaar8d84ff12017-10-26 16:42:16 +0200611 au! BufReadCmd
612endfunc
613
614func Test_BufReadCmdHelpJump()
615 " This used to cause access to free memory
616 au BufReadCmd * e +h{
Bram Moolenaarcf1ba352017-10-27 00:55:04 +0200617 " } to fix highlighting
618 call assert_fails('help', 'E434:')
Bram Moolenaar8d84ff12017-10-26 16:42:16 +0200619
Bram Moolenaar8d84ff12017-10-26 16:42:16 +0200620 au! BufReadCmd
621endfunc
622
zeertzjq93f72cc2022-08-26 15:34:52 +0100623" BufReadCmd is triggered for a "nofile" buffer. Check all values.
Bram Moolenaarb1d2c812022-08-26 11:55:01 +0100624func Test_BufReadCmdNofile()
zeertzjq93f72cc2022-08-26 15:34:52 +0100625 for val in ['nofile',
626 \ 'nowrite',
627 \ 'acwrite',
628 \ 'quickfix',
629 \ 'help',
630 \ 'terminal',
631 \ 'prompt',
632 \ 'popup',
633 \ ]
634 new somefile
635 exe 'set buftype=' .. val
636 au BufReadCmd somefile call setline(1, 'triggered')
637 edit
638 call assert_equal('triggered', getline(1))
Bram Moolenaarb1d2c812022-08-26 11:55:01 +0100639
zeertzjq93f72cc2022-08-26 15:34:52 +0100640 au! BufReadCmd
641 bwipe!
642 endfor
Bram Moolenaarb1d2c812022-08-26 11:55:01 +0100643endfunc
644
Bram Moolenaarb62cc362016-09-03 16:43:53 +0200645func Test_augroup_deleted()
Bram Moolenaarde653f02016-09-03 16:59:06 +0200646 " This caused a crash before E936 was introduced
Bram Moolenaarb62cc362016-09-03 16:43:53 +0200647 augroup x
Bram Moolenaarde653f02016-09-03 16:59:06 +0200648 call assert_fails('augroup! x', 'E936:')
649 au VimEnter * echo
650 augroup end
Bram Moolenaarb62cc362016-09-03 16:43:53 +0200651 augroup! x
Bram Moolenaar5dc4e2f2020-11-25 14:15:12 +0100652 call assert_match("-Deleted-.*VimEnter", execute('au VimEnter'))
Bram Moolenaarde653f02016-09-03 16:59:06 +0200653 au! VimEnter
Bram Moolenaarb62cc362016-09-03 16:43:53 +0200654endfunc
655
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200656" Tests for autocommands on :close command.
657" This used to be in test13.
658func Test_three_windows()
Bram Moolenaarb3435b02016-09-29 20:54:59 +0200659 " Clean up buffers, because in some cases this function fails.
660 call s:cleanup_buffers()
661
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200662 " Write three files and open them, each in a window.
663 " Then go to next window, with autocommand that deletes the previous one.
664 " Do this twice, writing the file.
665 e! Xtestje1
666 call setline(1, 'testje1')
667 w
668 sp Xtestje2
669 call setline(1, 'testje2')
670 w
671 sp Xtestje3
672 call setline(1, 'testje3')
673 w
674 wincmd w
675 au WinLeave Xtestje2 bwipe
676 wincmd w
677 call assert_equal('Xtestje1', expand('%'))
678
679 au WinLeave Xtestje1 bwipe Xtestje3
680 close
681 call assert_equal('Xtestje1', expand('%'))
682
683 " Test deleting the buffer on a Unload event. If this goes wrong there
684 " will be the ATTENTION prompt.
685 e Xtestje1
686 au!
687 au! BufUnload Xtestje1 bwipe
688 call assert_fails('e Xtestje3', 'E937:')
689 call assert_equal('Xtestje3', expand('%'))
690
691 e Xtestje2
692 sp Xtestje1
693 call assert_fails('e', 'E937:')
Bram Moolenaara997b452018-04-17 23:24:06 +0200694 call assert_equal('Xtestje1', expand('%'))
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200695
696 " Test changing buffers in a BufWipeout autocommand. If this goes wrong
697 " there are ml_line errors and/or a Crash.
698 au!
699 only
700 e Xanother
701 e Xtestje1
702 bwipe Xtestje2
703 bwipe Xtestje3
704 au BufWipeout Xtestje1 buf Xtestje1
705 bwipe
706 call assert_equal('Xanother', expand('%'))
707
708 only
709 help
710 wincmd w
711 1quit
712 call assert_equal('Xanother', expand('%'))
713
714 au!
Bram Moolenaar4520d442017-03-19 16:09:46 +0100715 enew
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200716 call delete('Xtestje1')
717 call delete('Xtestje2')
718 call delete('Xtestje3')
719endfunc
Bram Moolenaare13b9af2017-01-13 22:01:02 +0100720
721func Test_BufEnter()
722 au! BufEnter
723 au Bufenter * let val = val . '+'
724 let g:val = ''
725 split NewFile
726 call assert_equal('+', g:val)
727 bwipe!
728 call assert_equal('++', g:val)
729
730 " Also get BufEnter when editing a directory
Bram Moolenaar6f14da12022-09-07 21:30:44 +0100731 call mkdir('Xbufenterdir', 'D')
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +0100732 split Xbufenterdir
Bram Moolenaare13b9af2017-01-13 22:01:02 +0100733 call assert_equal('+++', g:val)
Bram Moolenaare94260f2017-03-21 15:50:12 +0100734
735 " On MS-Windows we can't edit the directory, make sure we wipe the right
736 " buffer.
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +0100737 bwipe! Xbufenterdir
Bram Moolenaare13b9af2017-01-13 22:01:02 +0100738 au! BufEnter
Bram Moolenaara9b5b852022-08-26 13:16:20 +0100739
740 " Editing a "nofile" buffer doesn't read the file but does trigger BufEnter
zeertzjq93f72cc2022-08-26 15:34:52 +0100741 " for historic reasons. Also test other 'buftype' values.
742 for val in ['nofile',
743 \ 'nowrite',
744 \ 'acwrite',
745 \ 'quickfix',
746 \ 'help',
747 \ 'terminal',
748 \ 'prompt',
749 \ 'popup',
750 \ ]
751 new somefile
752 exe 'set buftype=' .. val
753 au BufEnter somefile call setline(1, 'some text')
754 edit
755 call assert_equal('some text', getline(1))
756 bwipe!
757 au! BufEnter
758 endfor
Bram Moolenaare13b9af2017-01-13 22:01:02 +0100759endfunc
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100760
761" Closing a window might cause an endless loop
762" E814 for older Vims
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200763func Test_autocmd_bufwipe_in_SessLoadPost()
Bram Moolenaar1d68d9b2017-10-13 22:33:32 +0200764 edit Xtest
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100765 tabnew
Bram Moolenaar1d68d9b2017-10-13 22:33:32 +0200766 file Xsomething
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100767 set noswapfile
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100768 mksession!
769
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200770 let content =<< trim [CODE]
Bram Moolenaar62cd26a2020-10-11 20:08:44 +0200771 call test_override('ui_delay', 10)
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200772 set nocp noswapfile
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100773 let v:swapchoice = "e"
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200774 augroup test_autocmd_sessionload
775 autocmd!
776 autocmd SessionLoadPost * exe bufnr("Xsomething") . "bw!"
777 augroup END
778
779 func WriteErrors()
Bram Moolenaarbe9fc5b2022-09-09 17:09:35 +0100780 call writefile([execute("messages")], "XerrorsBwipe")
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200781 endfunc
782 au VimLeave * call WriteErrors()
783 [CODE]
784
Bram Moolenaare1f3ab72022-09-04 21:29:08 +0100785 call writefile(content, 'Xvimrc', 'D')
Bram Moolenaar93344c22019-08-14 21:12:05 +0200786 call system(GetVimCommand('Xvimrc') .. ' --not-a-term --noplugins -S Session.vim -c cq')
Bram Moolenaarae04a602022-09-09 15:08:10 +0100787 sleep 100m
Bram Moolenaarbe9fc5b2022-09-09 17:09:35 +0100788 let errors = join(readfile('XerrorsBwipe'))
Bram Moolenaare2e40752020-09-04 21:18:46 +0200789 call assert_match('E814:', errors)
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100790
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100791 set swapfile
Bram Moolenaarbe9fc5b2022-09-09 17:09:35 +0100792 for file in ['Session.vim', 'XerrorsBwipe']
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100793 call delete(file)
794 endfor
795endfunc
796
Bram Moolenaar797e63b2021-01-15 16:22:52 +0100797" Using :blast and :ball for many events caused a crash, because b_nwindows was
798" not incremented correctly.
799func Test_autocmd_blast_badd()
800 let content =<< trim [CODE]
801 au BufNew,BufAdd,BufWinEnter,BufEnter,BufLeave,BufWinLeave,BufUnload,VimEnter foo* blast
802 edit foo1
803 au BufNew,BufAdd,BufWinEnter,BufEnter,BufLeave,BufWinLeave,BufUnload,VimEnter foo* ball
804 edit foo2
Bram Moolenaarbe9fc5b2022-09-09 17:09:35 +0100805 call writefile(['OK'], 'XerrorsBlast')
Bram Moolenaar797e63b2021-01-15 16:22:52 +0100806 qall
807 [CODE]
808
Bram Moolenaare1f3ab72022-09-04 21:29:08 +0100809 call writefile(content, 'XblastBall', 'D')
Bram Moolenaar797e63b2021-01-15 16:22:52 +0100810 call system(GetVimCommand() .. ' --clean -S XblastBall')
Bram Moolenaarae04a602022-09-09 15:08:10 +0100811 sleep 100m
Bram Moolenaarbe9fc5b2022-09-09 17:09:35 +0100812 call assert_match('OK', readfile('XerrorsBlast')->join())
Bram Moolenaar797e63b2021-01-15 16:22:52 +0100813
Bram Moolenaarbe9fc5b2022-09-09 17:09:35 +0100814 call delete('XerrorsBlast')
Bram Moolenaar797e63b2021-01-15 16:22:52 +0100815endfunc
816
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100817" SEGV occurs in older versions.
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200818func Test_autocmd_bufwipe_in_SessLoadPost2()
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100819 tabnew
820 set noswapfile
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100821 mksession!
822
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200823 let content =<< trim [CODE]
824 set nocp noswapfile
825 function! DeleteInactiveBufs()
826 tabfirst
827 let tabblist = []
828 for i in range(1, tabpagenr(''$''))
829 call extend(tabblist, tabpagebuflist(i))
830 endfor
831 for b in range(1, bufnr(''$''))
832 if bufexists(b) && buflisted(b) && (index(tabblist, b) == -1 || bufname(b) =~# ''^$'')
833 exec ''bwipeout '' . b
834 endif
835 endfor
836 echomsg "SessionLoadPost DONE"
837 endfunction
838 au SessionLoadPost * call DeleteInactiveBufs()
839
840 func WriteErrors()
Bram Moolenaarbe9fc5b2022-09-09 17:09:35 +0100841 call writefile([execute("messages")], "XerrorsPost")
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200842 endfunc
843 au VimLeave * call WriteErrors()
844 [CODE]
845
Bram Moolenaare1f3ab72022-09-04 21:29:08 +0100846 call writefile(content, 'Xvimrc', 'D')
Bram Moolenaar93344c22019-08-14 21:12:05 +0200847 call system(GetVimCommand('Xvimrc') .. ' --not-a-term --noplugins -S Session.vim -c cq')
Bram Moolenaarae04a602022-09-09 15:08:10 +0100848 sleep 100m
Bram Moolenaarbe9fc5b2022-09-09 17:09:35 +0100849 let errors = join(readfile('XerrorsPost'))
Bram Moolenaare94260f2017-03-21 15:50:12 +0100850 " This probably only ever matches on unix.
851 call assert_notmatch('Caught deadly signal SEGV', errors)
852 call assert_match('SessionLoadPost DONE', errors)
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100853
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100854 set swapfile
Bram Moolenaarbe9fc5b2022-09-09 17:09:35 +0100855 for file in ['Session.vim', 'XerrorsPost']
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100856 call delete(file)
857 endfor
858endfunc
Bram Moolenaarfaf29d72017-07-09 11:07:16 +0200859
860func Test_empty_doau()
861 doau \|
862endfunc
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200863
864func s:AutoCommandOptionSet(match)
Bram Moolenaard7c96872019-06-15 17:12:48 +0200865 let template = "Option: <%s>, OldVal: <%s>, OldValLocal: <%s>, OldValGlobal: <%s>, NewVal: <%s>, Scope: <%s>, Command: <%s>\n"
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200866 let item = remove(g:options, 0)
Bram Moolenaard7c96872019-06-15 17:12:48 +0200867 let expected = printf(template, item[0], item[1], item[2], item[3], item[4], item[5], item[6])
868 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 +0200869 let g:opt = [expected, actual]
870 "call assert_equal(expected, actual)
871endfunc
872
873func Test_OptionSet()
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +0200874 CheckOption autochdir
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200875
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +0200876 badd test_autocmd.vim
877
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200878 call test_override('starting', 1)
879 set nocp
880 au OptionSet * :call s:AutoCommandOptionSet(expand("<amatch>"))
881
882 " 1: Setting number option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100883 let g:options = [['number', 0, 0, 0, 1, 'global', 'set']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200884 set nu
885 call assert_equal([], g:options)
886 call assert_equal(g:opt[0], g:opt[1])
887
888 " 2: Setting local number option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100889 let g:options = [['number', 1, 1, '', 0, 'local', 'setlocal']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200890 setlocal nonu
891 call assert_equal([], g:options)
892 call assert_equal(g:opt[0], g:opt[1])
893
894 " 3: Setting global number option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100895 let g:options = [['number', 1, '', 1, 0, 'global', 'setglobal']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200896 setglobal nonu
897 call assert_equal([], g:options)
898 call assert_equal(g:opt[0], g:opt[1])
899
900 " 4: Setting local autoindent option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100901 let g:options = [['autoindent', 0, 0, '', 1, 'local', 'setlocal']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200902 setlocal ai
903 call assert_equal([], g:options)
904 call assert_equal(g:opt[0], g:opt[1])
905
906 " 5: Setting global autoindent option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100907 let g:options = [['autoindent', 0, '', 0, 1, 'global', 'setglobal']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200908 setglobal ai
909 call assert_equal([], g:options)
910 call assert_equal(g:opt[0], g:opt[1])
911
912 " 6: Setting global autoindent option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100913 let g:options = [['autoindent', 1, 1, 1, 0, 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +0200914 set ai!
915 call assert_equal([], g:options)
916 call assert_equal(g:opt[0], g:opt[1])
917
918 " 6a: Setting global autoindent option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100919 let g:options = [['autoindent', 1, 1, 0, 0, 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +0200920 noa setlocal ai
921 noa setglobal noai
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200922 set ai!
923 call assert_equal([], g:options)
924 call assert_equal(g:opt[0], g:opt[1])
925
926 " Should not print anything, use :noa
927 " 7: don't trigger OptionSet"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100928 let g:options = [['invalid', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200929 noa set nonu
Bram Moolenaard7c96872019-06-15 17:12:48 +0200930 call assert_equal([['invalid', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid']], g:options)
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200931 call assert_equal(g:opt[0], g:opt[1])
932
933 " 8: Setting several global list and number option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100934 let g:options = [['list', 0, 0, 0, 1, 'global', 'set'], ['number', 0, 0, 0, 1, 'global', 'set']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200935 set list nu
936 call assert_equal([], g:options)
937 call assert_equal(g:opt[0], g:opt[1])
938
939 " 9: don't trigger OptionSet"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100940 let g:options = [['invalid', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid'], ['invalid', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200941 noa set nolist nonu
Bram Moolenaard7c96872019-06-15 17:12:48 +0200942 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 +0200943 call assert_equal(g:opt[0], g:opt[1])
944
945 " 10: Setting global acd"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100946 let g:options = [['autochdir', 0, 0, '', 1, 'local', 'setlocal']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200947 setlocal acd
948 call assert_equal([], g:options)
949 call assert_equal(g:opt[0], g:opt[1])
950
951 " 11: Setting global autoread (also sets local value)"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100952 let g:options = [['autoread', 0, 0, 0, 1, 'global', 'set']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200953 set ar
954 call assert_equal([], g:options)
955 call assert_equal(g:opt[0], g:opt[1])
956
957 " 12: Setting local autoread"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100958 let g:options = [['autoread', 1, 1, '', 1, 'local', 'setlocal']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200959 setlocal ar
960 call assert_equal([], g:options)
961 call assert_equal(g:opt[0], g:opt[1])
962
963 " 13: Setting global autoread"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100964 let g:options = [['autoread', 1, '', 1, 0, 'global', 'setglobal']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200965 setglobal invar
966 call assert_equal([], g:options)
967 call assert_equal(g:opt[0], g:opt[1])
968
969 " 14: Setting option backspace through :let"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100970 let g:options = [['backspace', '', '', '', 'eol,indent,start', 'global', 'set']]
971 let &bs = "eol,indent,start"
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200972 call assert_equal([], g:options)
973 call assert_equal(g:opt[0], g:opt[1])
974
975 " 15: Setting option backspace through setbufvar()"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100976 let g:options = [['backup', 0, 0, '', 1, 'local', 'setlocal']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200977 " try twice, first time, shouldn't trigger because option name is invalid,
978 " second time, it should trigger
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +0200979 let bnum = bufnr('%')
Bram Moolenaare2e40752020-09-04 21:18:46 +0200980 call assert_fails("call setbufvar(bnum, '&l:bk', 1)", 'E355:')
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200981 " should trigger, use correct option name
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +0200982 call setbufvar(bnum, '&backup', 1)
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200983 call assert_equal([], g:options)
984 call assert_equal(g:opt[0], g:opt[1])
985
986 " 16: Setting number option using setwinvar"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100987 let g:options = [['number', 0, 0, '', 1, 'local', 'setlocal']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200988 call setwinvar(0, '&number', 1)
989 call assert_equal([], g:options)
990 call assert_equal(g:opt[0], g:opt[1])
991
992 " 17: Setting key option, shouldn't trigger"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100993 let g:options = [['key', 'invalid', 'invalid1', 'invalid2', 'invalid3', 'invalid4', 'invalid5']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200994 setlocal key=blah
995 setlocal key=
Bram Moolenaard7c96872019-06-15 17:12:48 +0200996 call assert_equal([['key', 'invalid', 'invalid1', 'invalid2', 'invalid3', 'invalid4', 'invalid5']], g:options)
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200997 call assert_equal(g:opt[0], g:opt[1])
998
Bram Moolenaard7c96872019-06-15 17:12:48 +0200999
1000 " 18a: Setting string global option"
1001 let oldval = &backupext
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001002 let g:options = [['backupext', oldval, oldval, oldval, 'foo', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001003 set backupext=foo
1004 call assert_equal([], g:options)
1005 call assert_equal(g:opt[0], g:opt[1])
1006
1007 " 18b: Resetting string global option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001008 let g:options = [['backupext', 'foo', 'foo', 'foo', oldval, 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001009 set backupext&
1010 call assert_equal([], g:options)
1011 call assert_equal(g:opt[0], g:opt[1])
1012
1013 " 18c: Setting global string global option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001014 let g:options = [['backupext', oldval, '', oldval, 'bar', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001015 setglobal backupext=bar
1016 call assert_equal([], g:options)
1017 call assert_equal(g:opt[0], g:opt[1])
1018
1019 " 18d: Setting local string global option"
1020 " As this is a global option this sets the global value even though
1021 " :setlocal is used!
1022 noa set backupext& " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001023 let g:options = [['backupext', oldval, oldval, '', 'baz', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001024 setlocal backupext=baz
1025 call assert_equal([], g:options)
1026 call assert_equal(g:opt[0], g:opt[1])
1027
1028 " 18e: Setting again string global option"
1029 noa setglobal backupext=ext_global " Reset global and local value (without triggering autocmd)
1030 noa setlocal backupext=ext_local " Sets the global(!) value!
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001031 let g:options = [['backupext', 'ext_local', 'ext_local', 'ext_local', 'fuu', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001032 set backupext=fuu
1033 call assert_equal([], g:options)
1034 call assert_equal(g:opt[0], g:opt[1])
1035
1036
zeertzjqb811de52021-10-21 10:50:44 +01001037 " 19a: Setting string global-local (to buffer) option"
Bram Moolenaar8efa0262017-08-20 15:47:20 +02001038 let oldval = &tags
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001039 let g:options = [['tags', oldval, oldval, oldval, 'tagpath', 'global', 'set']]
Bram Moolenaar8efa0262017-08-20 15:47:20 +02001040 set tags=tagpath
1041 call assert_equal([], g:options)
1042 call assert_equal(g:opt[0], g:opt[1])
1043
zeertzjqb811de52021-10-21 10:50:44 +01001044 " 19b: Resetting string global-local (to buffer) option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001045 let g:options = [['tags', 'tagpath', 'tagpath', 'tagpath', oldval, 'global', 'set']]
Bram Moolenaar8efa0262017-08-20 15:47:20 +02001046 set tags&
1047 call assert_equal([], g:options)
1048 call assert_equal(g:opt[0], g:opt[1])
1049
zeertzjqb811de52021-10-21 10:50:44 +01001050 " 19c: Setting global string global-local (to buffer) option "
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001051 let g:options = [['tags', oldval, '', oldval, 'tagpath1', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001052 setglobal tags=tagpath1
1053 call assert_equal([], g:options)
1054 call assert_equal(g:opt[0], g:opt[1])
1055
zeertzjqb811de52021-10-21 10:50:44 +01001056 " 19d: Setting local string global-local (to buffer) option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001057 let g:options = [['tags', 'tagpath1', 'tagpath1', '', 'tagpath2', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001058 setlocal tags=tagpath2
1059 call assert_equal([], g:options)
1060 call assert_equal(g:opt[0], g:opt[1])
1061
zeertzjqb811de52021-10-21 10:50:44 +01001062 " 19e: Setting again string global-local (to buffer) option"
1063 " Note: v:option_old is the old global value for global-local string options
Bram Moolenaard7c96872019-06-15 17:12:48 +02001064 " but the old local value for all other kinds of options.
1065 noa setglobal tags=tag_global " Reset global and local value (without triggering autocmd)
1066 noa setlocal tags=tag_local
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001067 let g:options = [['tags', 'tag_global', 'tag_local', 'tag_global', 'tagpath', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001068 set tags=tagpath
1069 call assert_equal([], g:options)
1070 call assert_equal(g:opt[0], g:opt[1])
1071
zeertzjqb811de52021-10-21 10:50:44 +01001072 " 19f: Setting string global-local (to buffer) option to an empty string"
1073 " Note: v:option_old is the old global value for global-local string options
Bram Moolenaard7c96872019-06-15 17:12:48 +02001074 " but the old local value for all other kinds of options.
1075 noa set tags=tag_global " Reset global and local value (without triggering autocmd)
1076 noa setlocal tags= " empty string
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001077 let g:options = [['tags', 'tag_global', '', 'tag_global', 'tagpath', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001078 set tags=tagpath
1079 call assert_equal([], g:options)
1080 call assert_equal(g:opt[0], g:opt[1])
1081
1082
1083 " 20a: Setting string local (to buffer) option"
1084 let oldval = &spelllang
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001085 let g:options = [['spelllang', oldval, oldval, oldval, 'elvish,klingon', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001086 set spelllang=elvish,klingon
1087 call assert_equal([], g:options)
1088 call assert_equal(g:opt[0], g:opt[1])
1089
1090 " 20b: Resetting string local (to buffer) option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001091 let g:options = [['spelllang', 'elvish,klingon', 'elvish,klingon', 'elvish,klingon', oldval, 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001092 set spelllang&
1093 call assert_equal([], g:options)
1094 call assert_equal(g:opt[0], g:opt[1])
1095
1096 " 20c: Setting global string local (to buffer) option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001097 let g:options = [['spelllang', oldval, '', oldval, 'elvish', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001098 setglobal spelllang=elvish
1099 call assert_equal([], g:options)
1100 call assert_equal(g:opt[0], g:opt[1])
1101
1102 " 20d: Setting local string local (to buffer) option"
1103 noa set spelllang& " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001104 let g:options = [['spelllang', oldval, oldval, '', 'klingon', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001105 setlocal spelllang=klingon
1106 call assert_equal([], g:options)
1107 call assert_equal(g:opt[0], g:opt[1])
1108
1109 " 20e: Setting again string local (to buffer) option"
zeertzjqb811de52021-10-21 10:50:44 +01001110 " Note: v:option_old is the old global value for global-local string options
Bram Moolenaard7c96872019-06-15 17:12:48 +02001111 " but the old local value for all other kinds of options.
1112 noa setglobal spelllang=spellglobal " Reset global and local value (without triggering autocmd)
1113 noa setlocal spelllang=spelllocal
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001114 let g:options = [['spelllang', 'spelllocal', 'spelllocal', 'spellglobal', 'foo', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001115 set spelllang=foo
1116 call assert_equal([], g:options)
1117 call assert_equal(g:opt[0], g:opt[1])
1118
1119
zeertzjqb811de52021-10-21 10:50:44 +01001120 " 21a: Setting string global-local (to window) option"
Bram Moolenaard7c96872019-06-15 17:12:48 +02001121 let oldval = &statusline
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001122 let g:options = [['statusline', oldval, oldval, oldval, 'foo', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001123 set statusline=foo
1124 call assert_equal([], g:options)
1125 call assert_equal(g:opt[0], g:opt[1])
1126
zeertzjqb811de52021-10-21 10:50:44 +01001127 " 21b: Resetting string global-local (to window) option"
1128 " Note: v:option_old is the old global value for global-local string options
Bram Moolenaard7c96872019-06-15 17:12:48 +02001129 " but the old local value for all other kinds of options.
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001130 let g:options = [['statusline', 'foo', 'foo', 'foo', oldval, 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001131 set statusline&
1132 call assert_equal([], g:options)
1133 call assert_equal(g:opt[0], g:opt[1])
1134
zeertzjqb811de52021-10-21 10:50:44 +01001135 " 21c: Setting global string global-local (to window) option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001136 let g:options = [['statusline', oldval, '', oldval, 'bar', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001137 setglobal statusline=bar
1138 call assert_equal([], g:options)
1139 call assert_equal(g:opt[0], g:opt[1])
1140
zeertzjqb811de52021-10-21 10:50:44 +01001141 " 21d: Setting local string global-local (to window) option"
Bram Moolenaard7c96872019-06-15 17:12:48 +02001142 noa set statusline& " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001143 let g:options = [['statusline', oldval, oldval, '', 'baz', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001144 setlocal statusline=baz
1145 call assert_equal([], g:options)
1146 call assert_equal(g:opt[0], g:opt[1])
1147
zeertzjqb811de52021-10-21 10:50:44 +01001148 " 21e: Setting again string global-local (to window) option"
1149 " Note: v:option_old is the old global value for global-local string options
Bram Moolenaard7c96872019-06-15 17:12:48 +02001150 " but the old local value for all other kinds of options.
1151 noa setglobal statusline=bar " Reset global and local value (without triggering autocmd)
1152 noa setlocal statusline=baz
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001153 let g:options = [['statusline', 'bar', 'baz', 'bar', 'foo', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001154 set statusline=foo
1155 call assert_equal([], g:options)
1156 call assert_equal(g:opt[0], g:opt[1])
1157
1158
1159 " 22a: Setting string local (to window) option"
1160 let oldval = &foldignore
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001161 let g:options = [['foldignore', oldval, oldval, oldval, 'fo', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001162 set foldignore=fo
1163 call assert_equal([], g:options)
1164 call assert_equal(g:opt[0], g:opt[1])
1165
1166 " 22b: Resetting string local (to window) option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001167 let g:options = [['foldignore', 'fo', 'fo', 'fo', oldval, 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001168 set foldignore&
1169 call assert_equal([], g:options)
1170 call assert_equal(g:opt[0], g:opt[1])
1171
1172 " 22c: Setting global string local (to window) option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001173 let g:options = [['foldignore', oldval, '', oldval, 'bar', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001174 setglobal foldignore=bar
1175 call assert_equal([], g:options)
1176 call assert_equal(g:opt[0], g:opt[1])
1177
1178 " 22d: Setting local string local (to window) option"
1179 noa set foldignore& " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001180 let g:options = [['foldignore', oldval, oldval, '', 'baz', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001181 setlocal foldignore=baz
1182 call assert_equal([], g:options)
1183 call assert_equal(g:opt[0], g:opt[1])
1184
1185 " 22e: Setting again string local (to window) option"
1186 noa setglobal foldignore=glob " Reset global and local value (without triggering autocmd)
1187 noa setlocal foldignore=loc
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001188 let g:options = [['foldignore', 'loc', 'loc', 'glob', 'fo', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001189 set foldignore=fo
1190 call assert_equal([], g:options)
1191 call assert_equal(g:opt[0], g:opt[1])
1192
1193
zeertzjqb811de52021-10-21 10:50:44 +01001194 " 23a: Setting global number global option"
Bram Moolenaard7c96872019-06-15 17:12:48 +02001195 noa setglobal cmdheight=8 " Reset global and local value (without triggering autocmd)
1196 noa setlocal cmdheight=1 " Sets the global(!) value!
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001197 let g:options = [['cmdheight', '1', '', '1', '2', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001198 setglobal cmdheight=2
1199 call assert_equal([], g:options)
1200 call assert_equal(g:opt[0], g:opt[1])
1201
1202 " 23b: Setting local number global option"
1203 noa setglobal cmdheight=8 " Reset global and local value (without triggering autocmd)
1204 noa setlocal cmdheight=1 " Sets the global(!) value!
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001205 let g:options = [['cmdheight', '1', '1', '', '2', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001206 setlocal cmdheight=2
1207 call assert_equal([], g:options)
1208 call assert_equal(g:opt[0], g:opt[1])
1209
1210 " 23c: Setting again number global option"
1211 noa setglobal cmdheight=8 " Reset global and local value (without triggering autocmd)
1212 noa setlocal cmdheight=1 " Sets the global(!) value!
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001213 let g:options = [['cmdheight', '1', '1', '1', '2', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001214 set cmdheight=2
1215 call assert_equal([], g:options)
1216 call assert_equal(g:opt[0], g:opt[1])
1217
1218 " 23d: Setting again number global option"
1219 noa set cmdheight=8 " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001220 let g:options = [['cmdheight', '8', '8', '8', '2', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001221 set cmdheight=2
1222 call assert_equal([], g:options)
1223 call assert_equal(g:opt[0], g:opt[1])
1224
1225
1226 " 24a: Setting global number global-local (to buffer) option"
1227 noa setglobal undolevels=8 " Reset global and local value (without triggering autocmd)
1228 noa setlocal undolevels=1
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001229 let g:options = [['undolevels', '8', '', '8', '2', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001230 setglobal undolevels=2
1231 call assert_equal([], g:options)
1232 call assert_equal(g:opt[0], g:opt[1])
1233
1234 " 24b: Setting local number global-local (to buffer) option"
1235 noa setglobal undolevels=8 " Reset global and local value (without triggering autocmd)
1236 noa setlocal undolevels=1
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001237 let g:options = [['undolevels', '1', '1', '', '2', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001238 setlocal undolevels=2
1239 call assert_equal([], g:options)
1240 call assert_equal(g:opt[0], g:opt[1])
1241
1242 " 24c: Setting again number global-local (to buffer) option"
1243 noa setglobal undolevels=8 " Reset global and local value (without triggering autocmd)
1244 noa setlocal undolevels=1
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001245 let g:options = [['undolevels', '1', '1', '8', '2', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001246 set undolevels=2
1247 call assert_equal([], g:options)
1248 call assert_equal(g:opt[0], g:opt[1])
1249
1250 " 24d: Setting again global number global-local (to buffer) option"
1251 noa set undolevels=8 " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001252 let g:options = [['undolevels', '8', '8', '8', '2', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001253 set undolevels=2
1254 call assert_equal([], g:options)
1255 call assert_equal(g:opt[0], g:opt[1])
1256
1257
1258 " 25a: Setting global number local (to buffer) option"
1259 noa setglobal wrapmargin=8 " Reset global and local value (without triggering autocmd)
1260 noa setlocal wrapmargin=1
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001261 let g:options = [['wrapmargin', '8', '', '8', '2', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001262 setglobal wrapmargin=2
1263 call assert_equal([], g:options)
1264 call assert_equal(g:opt[0], g:opt[1])
1265
1266 " 25b: Setting local number local (to buffer) option"
1267 noa setglobal wrapmargin=8 " Reset global and local value (without triggering autocmd)
1268 noa setlocal wrapmargin=1
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001269 let g:options = [['wrapmargin', '1', '1', '', '2', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001270 setlocal wrapmargin=2
1271 call assert_equal([], g:options)
1272 call assert_equal(g:opt[0], g:opt[1])
1273
1274 " 25c: Setting again number local (to buffer) option"
1275 noa setglobal wrapmargin=8 " Reset global and local value (without triggering autocmd)
1276 noa setlocal wrapmargin=1
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001277 let g:options = [['wrapmargin', '1', '1', '8', '2', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001278 set wrapmargin=2
1279 call assert_equal([], g:options)
1280 call assert_equal(g:opt[0], g:opt[1])
1281
1282 " 25d: Setting again global number local (to buffer) option"
1283 noa set wrapmargin=8 " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001284 let g:options = [['wrapmargin', '8', '8', '8', '2', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001285 set wrapmargin=2
1286 call assert_equal([], g:options)
1287 call assert_equal(g:opt[0], g:opt[1])
1288
1289
1290 " 26: Setting number global-local (to window) option.
1291 " Such option does currently not exist.
1292
1293
1294 " 27a: Setting global number local (to window) option"
1295 noa setglobal foldcolumn=8 " Reset global and local value (without triggering autocmd)
1296 noa setlocal foldcolumn=1
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001297 let g:options = [['foldcolumn', '8', '', '8', '2', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001298 setglobal foldcolumn=2
1299 call assert_equal([], g:options)
1300 call assert_equal(g:opt[0], g:opt[1])
1301
1302 " 27b: Setting local number local (to window) option"
1303 noa setglobal foldcolumn=8 " Reset global and local value (without triggering autocmd)
1304 noa setlocal foldcolumn=1
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001305 let g:options = [['foldcolumn', '1', '1', '', '2', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001306 setlocal foldcolumn=2
1307 call assert_equal([], g:options)
1308 call assert_equal(g:opt[0], g:opt[1])
1309
1310 " 27c: Setting again number local (to window) option"
1311 noa setglobal foldcolumn=8 " Reset global and local value (without triggering autocmd)
1312 noa setlocal foldcolumn=1
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001313 let g:options = [['foldcolumn', '1', '1', '8', '2', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001314 set foldcolumn=2
1315 call assert_equal([], g:options)
1316 call assert_equal(g:opt[0], g:opt[1])
1317
zeertzjqb811de52021-10-21 10:50:44 +01001318 " 27d: Setting again global number local (to window) option"
Bram Moolenaard7c96872019-06-15 17:12:48 +02001319 noa set foldcolumn=8 " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001320 let g:options = [['foldcolumn', '8', '8', '8', '2', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001321 set foldcolumn=2
1322 call assert_equal([], g:options)
1323 call assert_equal(g:opt[0], g:opt[1])
1324
1325
1326 " 28a: Setting global boolean global option"
1327 noa setglobal nowrapscan " Reset global and local value (without triggering autocmd)
1328 noa setlocal wrapscan " Sets the global(!) value!
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001329 let g:options = [['wrapscan', '1', '', '1', '0', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001330 setglobal nowrapscan
1331 call assert_equal([], g:options)
1332 call assert_equal(g:opt[0], g:opt[1])
1333
1334 " 28b: Setting local boolean global option"
1335 noa setglobal nowrapscan " Reset global and local value (without triggering autocmd)
1336 noa setlocal wrapscan " Sets the global(!) value!
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001337 let g:options = [['wrapscan', '1', '1', '', '0', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001338 setlocal nowrapscan
1339 call assert_equal([], g:options)
1340 call assert_equal(g:opt[0], g:opt[1])
1341
1342 " 28c: Setting again boolean global option"
1343 noa setglobal nowrapscan " Reset global and local value (without triggering autocmd)
1344 noa setlocal wrapscan " Sets the global(!) value!
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001345 let g:options = [['wrapscan', '1', '1', '1', '0', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001346 set nowrapscan
1347 call assert_equal([], g:options)
1348 call assert_equal(g:opt[0], g:opt[1])
1349
1350 " 28d: Setting again global boolean global option"
1351 noa set nowrapscan " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001352 let g:options = [['wrapscan', '0', '0', '0', '1', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001353 set wrapscan
1354 call assert_equal([], g:options)
1355 call assert_equal(g:opt[0], g:opt[1])
1356
1357
1358 " 29a: Setting global boolean global-local (to buffer) option"
1359 noa setglobal noautoread " Reset global and local value (without triggering autocmd)
1360 noa setlocal autoread
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001361 let g:options = [['autoread', '0', '', '0', '1', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001362 setglobal autoread
1363 call assert_equal([], g:options)
1364 call assert_equal(g:opt[0], g:opt[1])
1365
1366 " 29b: Setting local boolean global-local (to buffer) option"
1367 noa setglobal noautoread " Reset global and local value (without triggering autocmd)
1368 noa setlocal autoread
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001369 let g:options = [['autoread', '1', '1', '', '0', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001370 setlocal noautoread
1371 call assert_equal([], g:options)
1372 call assert_equal(g:opt[0], g:opt[1])
1373
1374 " 29c: Setting again boolean global-local (to buffer) option"
1375 noa setglobal noautoread " Reset global and local value (without triggering autocmd)
1376 noa setlocal autoread
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001377 let g:options = [['autoread', '1', '1', '0', '1', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001378 set autoread
1379 call assert_equal([], g:options)
1380 call assert_equal(g:opt[0], g:opt[1])
1381
1382 " 29d: Setting again global boolean global-local (to buffer) option"
1383 noa set noautoread " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001384 let g:options = [['autoread', '0', '0', '0', '1', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001385 set autoread
1386 call assert_equal([], g:options)
1387 call assert_equal(g:opt[0], g:opt[1])
1388
1389
1390 " 30a: Setting global boolean local (to buffer) option"
1391 noa setglobal nocindent " Reset global and local value (without triggering autocmd)
1392 noa setlocal cindent
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001393 let g:options = [['cindent', '0', '', '0', '1', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001394 setglobal cindent
1395 call assert_equal([], g:options)
1396 call assert_equal(g:opt[0], g:opt[1])
1397
1398 " 30b: Setting local boolean local (to buffer) option"
1399 noa setglobal nocindent " Reset global and local value (without triggering autocmd)
1400 noa setlocal cindent
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001401 let g:options = [['cindent', '1', '1', '', '0', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001402 setlocal nocindent
1403 call assert_equal([], g:options)
1404 call assert_equal(g:opt[0], g:opt[1])
1405
1406 " 30c: Setting again boolean local (to buffer) option"
1407 noa setglobal nocindent " Reset global and local value (without triggering autocmd)
1408 noa setlocal cindent
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001409 let g:options = [['cindent', '1', '1', '0', '1', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001410 set cindent
1411 call assert_equal([], g:options)
1412 call assert_equal(g:opt[0], g:opt[1])
1413
1414 " 30d: Setting again global boolean local (to buffer) option"
1415 noa set nocindent " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001416 let g:options = [['cindent', '0', '0', '0', '1', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001417 set cindent
1418 call assert_equal([], g:options)
1419 call assert_equal(g:opt[0], g:opt[1])
1420
1421
1422 " 31: Setting boolean global-local (to window) option
1423 " Currently no such option exists.
1424
1425
1426 " 32a: Setting global boolean local (to window) option"
1427 noa setglobal nocursorcolumn " Reset global and local value (without triggering autocmd)
1428 noa setlocal cursorcolumn
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001429 let g:options = [['cursorcolumn', '0', '', '0', '1', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001430 setglobal cursorcolumn
1431 call assert_equal([], g:options)
1432 call assert_equal(g:opt[0], g:opt[1])
1433
1434 " 32b: Setting local boolean local (to window) option"
1435 noa setglobal nocursorcolumn " Reset global and local value (without triggering autocmd)
1436 noa setlocal cursorcolumn
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001437 let g:options = [['cursorcolumn', '1', '1', '', '0', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001438 setlocal nocursorcolumn
1439 call assert_equal([], g:options)
1440 call assert_equal(g:opt[0], g:opt[1])
1441
1442 " 32c: Setting again boolean local (to window) option"
1443 noa setglobal nocursorcolumn " Reset global and local value (without triggering autocmd)
1444 noa setlocal cursorcolumn
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001445 let g:options = [['cursorcolumn', '1', '1', '0', '1', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001446 set cursorcolumn
1447 call assert_equal([], g:options)
1448 call assert_equal(g:opt[0], g:opt[1])
1449
1450 " 32d: Setting again global boolean local (to window) option"
1451 noa set nocursorcolumn " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001452 let g:options = [['cursorcolumn', '0', '0', '0', '1', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001453 set cursorcolumn
1454 call assert_equal([], g:options)
1455 call assert_equal(g:opt[0], g:opt[1])
1456
1457
Bram Moolenaar1bc353b2019-09-01 14:45:28 +02001458 " 33: Test autocommands when an option value is converted internally.
Bram Moolenaard7c96872019-06-15 17:12:48 +02001459 noa set backspace=1 " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001460 let g:options = [['backspace', 'indent,eol', 'indent,eol', 'indent,eol', '2', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001461 set backspace=2
1462 call assert_equal([], g:options)
1463 call assert_equal(g:opt[0], g:opt[1])
1464
1465
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001466 " Cleanup
1467 au! OptionSet
Bram Moolenaar0331faf2019-06-15 18:40:37 +02001468 " set tags&
Bram Moolenaard7c96872019-06-15 17:12:48 +02001469 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 +02001470 exe printf(":set %s&vim", opt)
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001471 endfor
1472 call test_override('starting', 0)
1473 delfunc! AutoCommandOptionSet
1474endfunc
1475
1476func Test_OptionSet_diffmode()
1477 call test_override('starting', 1)
Bram Moolenaar26d98212019-01-27 22:32:55 +01001478 " 18: Changing an option when entering diff mode
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001479 new
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001480 au OptionSet diff :let &l:cul = v:option_new
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001481
1482 call setline(1, ['buffer 1', 'line2', 'line3', 'line4'])
1483 call assert_equal(0, &l:cul)
1484 diffthis
1485 call assert_equal(1, &l:cul)
1486
1487 vnew
1488 call setline(1, ['buffer 2', 'line 2', 'line 3', 'line4'])
1489 call assert_equal(0, &l:cul)
1490 diffthis
1491 call assert_equal(1, &l:cul)
1492
1493 diffoff
1494 call assert_equal(0, &l:cul)
1495 call assert_equal(1, getwinvar(2, '&l:cul'))
1496 bw!
1497
1498 call assert_equal(1, &l:cul)
1499 diffoff!
1500 call assert_equal(0, &l:cul)
1501 call assert_equal(0, getwinvar(1, '&l:cul'))
1502 bw!
1503
1504 " Cleanup
1505 au! OptionSet
1506 call test_override('starting', 0)
1507endfunc
1508
1509func Test_OptionSet_diffmode_close()
1510 call test_override('starting', 1)
1511 " 19: Try to close the current window when entering diff mode
1512 " should not segfault
1513 new
1514 au OptionSet diff close
1515
1516 call setline(1, ['buffer 1', 'line2', 'line3', 'line4'])
Bram Moolenaare2e40752020-09-04 21:18:46 +02001517 call assert_fails(':diffthis', 'E788:')
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001518 call assert_equal(1, &diff)
1519 vnew
1520 call setline(1, ['buffer 2', 'line 2', 'line 3', 'line4'])
Bram Moolenaare2e40752020-09-04 21:18:46 +02001521 call assert_fails(':diffthis', 'E788:')
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001522 call assert_equal(1, &diff)
Bram Moolenaara9aa86f2019-11-10 21:25:45 +01001523 set diffopt-=closeoff
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001524 bw!
Bram Moolenaare2e40752020-09-04 21:18:46 +02001525 call assert_fails(':diffoff!', 'E788:')
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001526 bw!
1527
1528 " Cleanup
1529 au! OptionSet
1530 call test_override('starting', 0)
1531 "delfunc! AutoCommandOptionSet
1532endfunc
Bram Moolenaar4a137b42017-08-04 22:37:11 +02001533
1534" Test for Bufleave autocommand that deletes the buffer we are about to edit.
1535func Test_BufleaveWithDelete()
Bram Moolenaare7cda972022-08-29 11:02:59 +01001536 new | edit XbufLeave1
Bram Moolenaar4a137b42017-08-04 22:37:11 +02001537
1538 augroup test_bufleavewithdelete
1539 autocmd!
Bram Moolenaare7cda972022-08-29 11:02:59 +01001540 autocmd BufLeave XbufLeave1 bwipe XbufLeave2
Bram Moolenaar4a137b42017-08-04 22:37:11 +02001541 augroup END
1542
Bram Moolenaare7cda972022-08-29 11:02:59 +01001543 call assert_fails('edit XbufLeave2', 'E143:')
1544 call assert_equal('XbufLeave1', bufname('%'))
Bram Moolenaar4a137b42017-08-04 22:37:11 +02001545
Bram Moolenaare7cda972022-08-29 11:02:59 +01001546 autocmd! test_bufleavewithdelete BufLeave XbufLeave1
Bram Moolenaar4a137b42017-08-04 22:37:11 +02001547 augroup! test_bufleavewithdelete
1548
1549 new
Bram Moolenaare7cda972022-08-29 11:02:59 +01001550 bwipe! XbufLeave1
Bram Moolenaar4a137b42017-08-04 22:37:11 +02001551endfunc
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +02001552
1553" Test for autocommand that changes the buffer list, when doing ":ball".
1554func Test_Acmd_BufAll()
1555 enew!
1556 %bwipe!
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01001557 call writefile(['Test file Xxx1'], 'Xxx1', 'D')
1558 call writefile(['Test file Xxx2'], 'Xxx2', 'D')
1559 call writefile(['Test file Xxx3'], 'Xxx3', 'D')
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +02001560
1561 " Add three files to the buffer list
1562 split Xxx1
1563 close
1564 split Xxx2
1565 close
1566 split Xxx3
1567 close
1568
1569 " Wipe the buffer when the buffer is opened
1570 au BufReadPost Xxx2 bwipe
1571
1572 call append(0, 'Test file Xxx4')
1573 ball
1574
1575 call assert_equal(2, winnr('$'))
1576 call assert_equal('Xxx1', bufname(winbufnr(winnr('$'))))
1577 wincmd t
1578
1579 au! BufReadPost
1580 %bwipe!
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +02001581 enew! | only
1582endfunc
1583
1584" Test for autocommand that changes current buffer on BufEnter event.
1585" Check if modelines are interpreted for the correct buffer.
1586func Test_Acmd_BufEnter()
1587 %bwipe!
1588 call writefile(['start of test file Xxx1',
1589 \ "\<Tab>this is a test",
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01001590 \ 'end of test file Xxx1'], 'Xxx1', 'D')
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +02001591 call writefile(['start of test file Xxx2',
1592 \ 'vim: set noai :',
1593 \ "\<Tab>this is a test",
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01001594 \ 'end of test file Xxx2'], 'Xxx2', 'D')
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +02001595
1596 au BufEnter Xxx2 brew
1597 set ai modeline modelines=3
1598 edit Xxx1
1599 " edit Xxx2, autocmd will do :brew
1600 edit Xxx2
1601 exe "normal G?this is a\<CR>"
1602 " Append text with autoindent to this file
1603 normal othis should be auto-indented
1604 call assert_equal("\<Tab>this should be auto-indented", getline('.'))
1605 call assert_equal(3, line('.'))
1606 " Remove autocmd and edit Xxx2 again
1607 au! BufEnter Xxx2
1608 buf! Xxx2
1609 exe "normal G?this is a\<CR>"
1610 " append text without autoindent to Xxx
1611 normal othis should be in column 1
1612 call assert_equal("this should be in column 1", getline('.'))
1613 call assert_equal(4, line('.'))
1614
1615 %bwipe!
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +02001616 set ai&vim modeline&vim modelines&vim
1617endfunc
1618
1619" Test for issue #57
1620" do not move cursor on <c-o> when autoindent is set
1621func Test_ai_CTRL_O()
1622 enew!
1623 set ai
1624 let save_fo = &fo
1625 set fo+=r
1626 exe "normal o# abcdef\<Esc>2hi\<CR>\<C-O>d0\<Esc>"
1627 exe "normal o# abcdef\<Esc>2hi\<C-O>d0\<Esc>"
1628 call assert_equal(['# abc', 'def', 'def'], getline(2, 4))
1629
1630 set ai&vim
1631 let &fo = save_fo
1632 enew!
1633endfunc
1634
1635" Test for autocommand that deletes the current buffer on BufLeave event.
1636" Also test deleting the last buffer, should give a new, empty buffer.
1637func Test_BufLeave_Wipe()
1638 %bwipe!
1639 let content = ['start of test file Xxx',
1640 \ 'this is a test',
1641 \ 'end of test file Xxx']
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01001642 call writefile(content, 'Xxx1', 'D')
1643 call writefile(content, 'Xxx2', 'D')
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +02001644
1645 au BufLeave Xxx2 bwipe
1646 edit Xxx1
1647 split Xxx2
1648 " delete buffer Xxx2, we should be back to Xxx1
1649 bwipe
1650 call assert_equal('Xxx1', bufname('%'))
1651 call assert_equal(1, winnr('$'))
1652
1653 " Create an alternate buffer
1654 %write! test.out
1655 call assert_equal('test.out', bufname('#'))
1656 " delete alternate buffer
1657 bwipe test.out
1658 call assert_equal('Xxx1', bufname('%'))
1659 call assert_equal('', bufname('#'))
1660
1661 au BufLeave Xxx1 bwipe
1662 " delete current buffer, get an empty one
1663 bwipe!
1664 call assert_equal(1, line('$'))
1665 call assert_equal('', bufname('%'))
Bram Moolenaarb2c87502017-10-14 21:15:58 +02001666 let g:bufinfo = getbufinfo()
1667 call assert_equal(1, len(g:bufinfo))
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +02001668
Bram Moolenaar53f0c962017-10-22 14:23:59 +02001669 call delete('test.out')
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +02001670 %bwipe
1671 au! BufLeave
Bram Moolenaarb2c87502017-10-14 21:15:58 +02001672
1673 " check that bufinfo doesn't contain a pointer to freed memory
1674 call test_garbagecollect_now()
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +02001675endfunc
Bram Moolenaar87ffb5c2017-10-19 12:37:42 +02001676
1677func Test_QuitPre()
1678 edit Xfoo
1679 let winid = win_getid(winnr())
1680 split Xbar
1681 au! QuitPre * let g:afile = expand('<afile>')
1682 " Close the other window, <afile> should be correct.
1683 exe win_id2win(winid) . 'q'
1684 call assert_equal('Xfoo', g:afile)
LemonBoy66e13ae2022-04-21 22:52:11 +01001685
Bram Moolenaar87ffb5c2017-10-19 12:37:42 +02001686 unlet g:afile
1687 bwipe Xfoo
1688 bwipe Xbar
1689endfunc
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02001690
1691func Test_Cmdline()
Bram Moolenaar153b7042018-01-31 15:48:32 +01001692 au! CmdlineChanged : let g:text = getcmdline()
1693 let g:text = 0
1694 call feedkeys(":echom 'hello'\<CR>", 'xt')
1695 call assert_equal("echom 'hello'", g:text)
1696 au! CmdlineChanged
1697
1698 au! CmdlineChanged : let g:entered = expand('<afile>')
1699 let g:entered = 0
1700 call feedkeys(":echom 'hello'\<CR>", 'xt')
1701 call assert_equal(':', g:entered)
1702 au! CmdlineChanged
1703
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02001704 au! CmdlineEnter : let g:entered = expand('<afile>')
1705 au! CmdlineLeave : let g:left = expand('<afile>')
1706 let g:entered = 0
1707 let g:left = 0
1708 call feedkeys(":echo 'hello'\<CR>", 'xt')
1709 call assert_equal(':', g:entered)
1710 call assert_equal(':', g:left)
1711 au! CmdlineEnter
1712 au! CmdlineLeave
1713
Bram Moolenaara4baf5b2018-04-22 13:27:44 +02001714 let save_shellslash = &shellslash
1715 set noshellslash
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02001716 au! CmdlineEnter / let g:entered = expand('<afile>')
1717 au! CmdlineLeave / let g:left = expand('<afile>')
1718 let g:entered = 0
1719 let g:left = 0
Bram Moolenaar53f0c962017-10-22 14:23:59 +02001720 new
1721 call setline(1, 'hello')
1722 call feedkeys("/hello\<CR>", 'xt')
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02001723 call assert_equal('/', g:entered)
1724 call assert_equal('/', g:left)
Bram Moolenaar53f0c962017-10-22 14:23:59 +02001725 bwipe!
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02001726 au! CmdlineEnter
1727 au! CmdlineLeave
Bram Moolenaara4baf5b2018-04-22 13:27:44 +02001728 let &shellslash = save_shellslash
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02001729endfunc
Bram Moolenaar53f0c962017-10-22 14:23:59 +02001730
1731" Test for BufWritePre autocommand that deletes or unloads the buffer.
1732func Test_BufWritePre()
1733 %bwipe
1734 au BufWritePre Xxx1 bunload
1735 au BufWritePre Xxx2 bwipe
1736
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01001737 call writefile(['start of Xxx1', 'test', 'end of Xxx1'], 'Xxx1', 'D')
1738 call writefile(['start of Xxx2', 'test', 'end of Xxx2'], 'Xxx2', 'D')
Bram Moolenaar53f0c962017-10-22 14:23:59 +02001739
1740 edit Xtest
1741 e! Xxx2
1742 bdel Xtest
1743 e Xxx1
1744 " write it, will unload it and give an error msg
Bram Moolenaare2e40752020-09-04 21:18:46 +02001745 call assert_fails('w', 'E203:')
Bram Moolenaar53f0c962017-10-22 14:23:59 +02001746 call assert_equal('Xxx2', bufname('%'))
1747 edit Xtest
1748 e! Xxx2
1749 bwipe Xtest
1750 " write it, will delete the buffer and give an error msg
Bram Moolenaare2e40752020-09-04 21:18:46 +02001751 call assert_fails('w', 'E203:')
Bram Moolenaar53f0c962017-10-22 14:23:59 +02001752 call assert_equal('Xxx1', bufname('%'))
1753 au! BufWritePre
Bram Moolenaar53f0c962017-10-22 14:23:59 +02001754endfunc
1755
1756" Test for BufUnload autocommand that unloads all the other buffers
1757func Test_bufunload_all()
Bram Moolenaarf08b0eb2021-10-16 13:00:14 +01001758 let g:test_is_flaky = 1
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01001759 call writefile(['Test file Xxx1'], 'Xxx1', 'D')"
1760 call writefile(['Test file Xxx2'], 'Xxx2', 'D')"
Bram Moolenaar53f0c962017-10-22 14:23:59 +02001761
Bram Moolenaarc79745a2019-05-20 22:12:34 +02001762 let content =<< trim [CODE]
1763 func UnloadAllBufs()
1764 let i = 1
1765 while i <= bufnr('$')
1766 if i != bufnr('%') && bufloaded(i)
1767 exe i . 'bunload'
1768 endif
1769 let i += 1
1770 endwhile
1771 endfunc
1772 au BufUnload * call UnloadAllBufs()
1773 au VimLeave * call writefile(['Test Finished'], 'Xout')
1774 edit Xxx1
1775 split Xxx2
1776 q
1777 [CODE]
1778
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01001779 call writefile(content, 'Xbunloadtest', 'D')
Bram Moolenaar53f0c962017-10-22 14:23:59 +02001780
1781 call delete('Xout')
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01001782 call system(GetVimCommandClean() .. ' -N --not-a-term -S Xbunloadtest')
Bram Moolenaar53f0c962017-10-22 14:23:59 +02001783 call assert_true(filereadable('Xout'))
1784
Bram Moolenaar53f0c962017-10-22 14:23:59 +02001785 call delete('Xout')
1786endfunc
1787
1788" Some tests for buffer-local autocommands
1789func Test_buflocal_autocmd()
1790 let g:bname = ''
1791 edit xx
1792 au BufLeave <buffer> let g:bname = expand("%")
1793 " here, autocommand for xx should trigger.
1794 " but autocommand shall not apply to buffer named <buffer>.
1795 edit somefile
1796 call assert_equal('xx', g:bname)
1797 let g:bname = ''
1798 " here, autocommand shall be auto-deleted
1799 bwipe xx
1800 " autocmd should not trigger
1801 edit xx
1802 call assert_equal('', g:bname)
1803 " autocmd should not trigger
1804 edit somefile
1805 call assert_equal('', g:bname)
1806 enew
1807 unlet g:bname
1808endfunc
Bram Moolenaar430dc5d2017-11-02 21:04:47 +01001809
1810" Test for "*Cmd" autocommands
1811func Test_Cmd_Autocmds()
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01001812 call writefile(['start of Xxx', "\tabc2", 'end of Xxx'], 'Xxx', 'D')
Bram Moolenaar430dc5d2017-11-02 21:04:47 +01001813
1814 enew!
1815 au BufReadCmd XtestA 0r Xxx|$del
1816 edit XtestA " will read text of Xxd instead
1817 call assert_equal('start of Xxx', getline(1))
1818
1819 au BufWriteCmd XtestA call append(line("$"), "write")
1820 write " will append a line to the file
1821 call assert_equal('write', getline('$'))
Bram Moolenaare2e40752020-09-04 21:18:46 +02001822 call assert_fails('read XtestA', 'E484:') " should not read anything
Bram Moolenaar430dc5d2017-11-02 21:04:47 +01001823 call assert_equal('write', getline(4))
1824
1825 " now we have:
1826 " 1 start of Xxx
1827 " 2 abc2
1828 " 3 end of Xxx
1829 " 4 write
1830
1831 au FileReadCmd XtestB '[r Xxx
1832 2r XtestB " will read Xxx below line 2 instead
1833 call assert_equal('start of Xxx', getline(3))
1834
1835 " now we have:
1836 " 1 start of Xxx
1837 " 2 abc2
1838 " 3 start of Xxx
1839 " 4 abc2
1840 " 5 end of Xxx
1841 " 6 end of Xxx
1842 " 7 write
1843
1844 au FileWriteCmd XtestC '[,']copy $
1845 normal 4GA1
1846 4,5w XtestC " will copy lines 4 and 5 to the end
1847 call assert_equal("\tabc21", getline(8))
Bram Moolenaare2e40752020-09-04 21:18:46 +02001848 call assert_fails('r XtestC', 'E484:') " should not read anything
Bram Moolenaar430dc5d2017-11-02 21:04:47 +01001849 call assert_equal("end of Xxx", getline(9))
1850
1851 " now we have:
1852 " 1 start of Xxx
1853 " 2 abc2
1854 " 3 start of Xxx
1855 " 4 abc21
1856 " 5 end of Xxx
1857 " 6 end of Xxx
1858 " 7 write
1859 " 8 abc21
1860 " 9 end of Xxx
1861
1862 let g:lines = []
1863 au FileAppendCmd XtestD call extend(g:lines, getline(line("'["), line("']")))
1864 w >>XtestD " will add lines to 'lines'
1865 call assert_equal(9, len(g:lines))
Bram Moolenaare2e40752020-09-04 21:18:46 +02001866 call assert_fails('$r XtestD', 'E484:') " should not read anything
Bram Moolenaar430dc5d2017-11-02 21:04:47 +01001867 call assert_equal(9, line('$'))
1868 call assert_equal('end of Xxx', getline('$'))
1869
1870 au BufReadCmd XtestE 0r Xxx|$del
1871 sp XtestE " split window with test.out
1872 call assert_equal('end of Xxx', getline(3))
1873
1874 let g:lines = []
1875 exe "normal 2Goasdf\<Esc>\<C-W>\<C-W>"
1876 au BufWriteCmd XtestE call extend(g:lines, getline(0, '$'))
1877 wall " will write other window to 'lines'
1878 call assert_equal(4, len(g:lines), g:lines)
1879 call assert_equal('asdf', g:lines[2])
1880
1881 au! BufReadCmd
1882 au! BufWriteCmd
1883 au! FileReadCmd
1884 au! FileWriteCmd
1885 au! FileAppendCmd
1886 %bwipe!
Bram Moolenaar430dc5d2017-11-02 21:04:47 +01001887 enew!
1888endfunc
Bram Moolenaaraace2152017-11-05 16:23:10 +01001889
Bram Moolenaar0fff4412020-03-29 16:06:29 +02001890func s:ReadFile()
1891 setl noswapfile nomodified
1892 let filename = resolve(expand("<afile>:p"))
1893 execute 'read' fnameescape(filename)
1894 1d_
1895 exe 'file' fnameescape(filename)
1896 setl buftype=acwrite
1897endfunc
1898
1899func s:WriteFile()
1900 let filename = resolve(expand("<afile>:p"))
1901 setl buftype=
1902 noautocmd execute 'write' fnameescape(filename)
1903 setl buftype=acwrite
1904 setl nomodified
1905endfunc
1906
1907func Test_BufReadCmd()
1908 autocmd BufReadCmd *.test call s:ReadFile()
1909 autocmd BufWriteCmd *.test call s:WriteFile()
1910
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01001911 call writefile(['one', 'two', 'three'], 'Xcmd.test', 'D')
Bram Moolenaar0fff4412020-03-29 16:06:29 +02001912 edit Xcmd.test
1913 call assert_match('Xcmd.test" line 1 of 3', execute('file'))
1914 normal! Gofour
1915 write
1916 call assert_equal(['one', 'two', 'three', 'four'], readfile('Xcmd.test'))
1917
1918 bwipe!
Bram Moolenaar0fff4412020-03-29 16:06:29 +02001919 au! BufReadCmd
1920 au! BufWriteCmd
1921endfunc
1922
zeertzjq9c8f9462022-08-30 18:17:15 +01001923func Test_BufWriteCmd()
1924 autocmd BufWriteCmd Xbufwritecmd let g:written = 1
1925 new
1926 file Xbufwritecmd
1927 set buftype=acwrite
Bram Moolenaar6f14da12022-09-07 21:30:44 +01001928 call mkdir('Xbufwritecmd', 'D')
zeertzjq9c8f9462022-08-30 18:17:15 +01001929 write
1930 " BufWriteCmd should be triggered even if a directory has the same name
1931 call assert_equal(1, g:written)
zeertzjq9c8f9462022-08-30 18:17:15 +01001932 unlet g:written
1933 au! BufWriteCmd
1934 bwipe!
1935endfunc
1936
Bram Moolenaaraace2152017-11-05 16:23:10 +01001937func SetChangeMarks(start, end)
Bram Moolenaar97c69432021-01-15 16:45:21 +01001938 exe a:start .. 'mark ['
1939 exe a:end .. 'mark ]'
Bram Moolenaaraace2152017-11-05 16:23:10 +01001940endfunc
1941
1942" Verify the effects of autocmds on '[ and ']
1943func Test_change_mark_in_autocmds()
1944 edit! Xtest
Bram Moolenaar97c69432021-01-15 16:45:21 +01001945 call feedkeys("ia\<CR>b\<CR>c\<CR>d\<C-g>u\<Esc>", 'xtn')
Bram Moolenaaraace2152017-11-05 16:23:10 +01001946
1947 call SetChangeMarks(2, 3)
1948 write
1949 call assert_equal([1, 4], [line("'["), line("']")])
1950
1951 call SetChangeMarks(2, 3)
1952 au BufWritePre * call assert_equal([1, 4], [line("'["), line("']")])
1953 write
1954 au! BufWritePre
1955
Bram Moolenaar14ddd222020-08-05 12:02:40 +02001956 if has('unix')
Bram Moolenaaraace2152017-11-05 16:23:10 +01001957 write XtestFilter
1958 write >> XtestFilter
1959
1960 call SetChangeMarks(2, 3)
1961 " Marks are set to the entire range of the write
1962 au FilterWritePre * call assert_equal([1, 4], [line("'["), line("']")])
1963 " '[ is adjusted to just before the line that will receive the filtered
1964 " data
1965 au FilterReadPre * call assert_equal([4, 4], [line("'["), line("']")])
1966 " The filtered data is read into the buffer, and the source lines are
1967 " still present, so the range is after the source lines
1968 au FilterReadPost * call assert_equal([5, 12], [line("'["), line("']")])
1969 %!cat XtestFilter
1970 " After the filtered data is read, the original lines are deleted
1971 call assert_equal([1, 8], [line("'["), line("']")])
1972 au! FilterWritePre,FilterReadPre,FilterReadPost
1973 undo
1974
1975 call SetChangeMarks(1, 4)
1976 au FilterWritePre * call assert_equal([2, 3], [line("'["), line("']")])
1977 au FilterReadPre * call assert_equal([3, 3], [line("'["), line("']")])
1978 au FilterReadPost * call assert_equal([4, 11], [line("'["), line("']")])
1979 2,3!cat XtestFilter
1980 call assert_equal([2, 9], [line("'["), line("']")])
1981 au! FilterWritePre,FilterReadPre,FilterReadPost
1982 undo
1983
1984 call delete('XtestFilter')
1985 endif
1986
1987 call SetChangeMarks(1, 4)
1988 au FileWritePre * call assert_equal([2, 3], [line("'["), line("']")])
1989 2,3write Xtest2
1990 au! FileWritePre
1991
1992 call SetChangeMarks(2, 3)
1993 au FileAppendPre * call assert_equal([1, 4], [line("'["), line("']")])
1994 write >> Xtest2
1995 au! FileAppendPre
1996
1997 call SetChangeMarks(1, 4)
1998 au FileAppendPre * call assert_equal([2, 3], [line("'["), line("']")])
1999 2,3write >> Xtest2
2000 au! FileAppendPre
2001
2002 call SetChangeMarks(1, 1)
2003 au FileReadPre * call assert_equal([3, 1], [line("'["), line("']")])
2004 au FileReadPost * call assert_equal([4, 11], [line("'["), line("']")])
2005 3read Xtest2
2006 au! FileReadPre,FileReadPost
2007 undo
2008
2009 call SetChangeMarks(4, 4)
2010 " When the line is 0, it's adjusted to 1
2011 au FileReadPre * call assert_equal([1, 4], [line("'["), line("']")])
2012 au FileReadPost * call assert_equal([1, 8], [line("'["), line("']")])
2013 0read Xtest2
2014 au! FileReadPre,FileReadPost
2015 undo
2016
2017 call SetChangeMarks(4, 4)
2018 " When the line is 0, it's adjusted to 1
2019 au FileReadPre * call assert_equal([1, 4], [line("'["), line("']")])
2020 au FileReadPost * call assert_equal([2, 9], [line("'["), line("']")])
2021 1read Xtest2
2022 au! FileReadPre,FileReadPost
2023 undo
2024
2025 bwipe!
2026 call delete('Xtest')
2027 call delete('Xtest2')
2028endfunc
2029
2030func Test_Filter_noshelltemp()
Bram Moolenaaraeb313f2020-11-27 19:13:28 +01002031 CheckExecutable cat
Bram Moolenaaraace2152017-11-05 16:23:10 +01002032
2033 enew!
2034 call setline(1, ['a', 'b', 'c', 'd'])
2035
2036 let shelltemp = &shelltemp
2037 set shelltemp
2038
2039 let g:filter_au = 0
2040 au FilterWritePre * let g:filter_au += 1
2041 au FilterReadPre * let g:filter_au += 1
2042 au FilterReadPost * let g:filter_au += 1
2043 %!cat
2044 call assert_equal(3, g:filter_au)
2045
2046 if has('filterpipe')
2047 set noshelltemp
2048
2049 let g:filter_au = 0
2050 au FilterWritePre * let g:filter_au += 1
2051 au FilterReadPre * let g:filter_au += 1
2052 au FilterReadPost * let g:filter_au += 1
2053 %!cat
2054 call assert_equal(0, g:filter_au)
2055 endif
2056
2057 au! FilterWritePre,FilterReadPre,FilterReadPost
2058 let &shelltemp = shelltemp
2059 bwipe!
2060endfunc
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01002061
2062func Test_TextYankPost()
2063 enew!
2064 call setline(1, ['foo'])
2065
2066 let g:event = []
2067 au TextYankPost * let g:event = copy(v:event)
2068
2069 call assert_equal({}, v:event)
2070 call assert_fails('let v:event = {}', 'E46:')
2071 call assert_fails('let v:event.mykey = 0', 'E742:')
2072
2073 norm "ayiw
2074 call assert_equal(
Bram Moolenaara016eeb2022-04-09 11:37:38 +01002075 \ #{regcontents: ['foo'], regname: 'a', operator: 'y',
2076 \ regtype: 'v', visual: v:false, inclusive: v:true},
2077 \ g:event)
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01002078 norm y_
2079 call assert_equal(
Bram Moolenaara016eeb2022-04-09 11:37:38 +01002080 \ #{regcontents: ['foo'], regname: '', operator: 'y', regtype: 'V',
2081 \ visual: v:false, inclusive: v:false},
2082 \ g:event)
Bram Moolenaar37d16732020-06-12 22:09:01 +02002083 norm Vy
2084 call assert_equal(
Bram Moolenaara016eeb2022-04-09 11:37:38 +01002085 \ #{regcontents: ['foo'], regname: '', operator: 'y', regtype: 'V',
2086 \ visual: v:true, inclusive: v:true},
2087 \ g:event)
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01002088 call feedkeys("\<C-V>y", 'x')
2089 call assert_equal(
Bram Moolenaara016eeb2022-04-09 11:37:38 +01002090 \ #{regcontents: ['f'], regname: '', operator: 'y', regtype: "\x161",
2091 \ visual: v:true, inclusive: v:true},
2092 \ g:event)
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01002093 norm "xciwbar
2094 call assert_equal(
Bram Moolenaara016eeb2022-04-09 11:37:38 +01002095 \ #{regcontents: ['foo'], regname: 'x', operator: 'c', regtype: 'v',
2096 \ visual: v:false, inclusive: v:true},
2097 \ g:event)
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01002098 norm "bdiw
2099 call assert_equal(
Bram Moolenaara016eeb2022-04-09 11:37:38 +01002100 \ #{regcontents: ['bar'], regname: 'b', operator: 'd', regtype: 'v',
2101 \ visual: v:false, inclusive: v:true},
2102 \ g:event)
2103
2104 call setline(1, 'foobar')
2105 " exclusive motion
2106 norm $"ay0
2107 call assert_equal(
2108 \ #{regcontents: ['fooba'], regname: 'a', operator: 'y', regtype: 'v',
2109 \ visual: v:false, inclusive: v:false},
2110 \ g:event)
2111 " inclusive motion
2112 norm 0"ay$
2113 call assert_equal(
2114 \ #{regcontents: ['foobar'], regname: 'a', operator: 'y', regtype: 'v',
2115 \ visual: v:false, inclusive: v:true},
2116 \ g:event)
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01002117
2118 call assert_equal({}, v:event)
2119
Bram Moolenaarfccbf062020-11-26 20:34:00 +01002120 if has('clipboard_working') && !has('gui_running')
2121 " Test that when the visual selection is automatically copied to clipboard
2122 " register a TextYankPost is emitted
2123 call setline(1, ['foobar'])
2124
2125 let @* = ''
2126 set clipboard=autoselect
2127 exe "norm! ggviw\<Esc>"
2128 call assert_equal(
Bram Moolenaara016eeb2022-04-09 11:37:38 +01002129 \ #{regcontents: ['foobar'], regname: '*', operator: 'y',
2130 \ regtype: 'v', visual: v:true, inclusive: v:false},
2131 \ g:event)
Bram Moolenaarfccbf062020-11-26 20:34:00 +01002132
2133 let @+ = ''
2134 set clipboard=autoselectplus
2135 exe "norm! ggviw\<Esc>"
2136 call assert_equal(
Bram Moolenaara016eeb2022-04-09 11:37:38 +01002137 \ #{regcontents: ['foobar'], regname: '+', operator: 'y',
2138 \ regtype: 'v', visual: v:true, inclusive: v:false},
2139 \ g:event)
Bram Moolenaarfccbf062020-11-26 20:34:00 +01002140
2141 set clipboard&vim
2142 endif
2143
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01002144 au! TextYankPost
2145 unlet g:event
2146 bwipe!
2147endfunc
Bram Moolenaar9bca8052017-12-18 12:37:55 +01002148
Bram Moolenaar9a046fd2021-01-28 13:47:59 +01002149func Test_autocommand_all_events()
2150 call assert_fails('au * * bwipe', 'E1155:')
2151 call assert_fails('au * x bwipe', 'E1155:')
Bram Moolenaarb6db1462021-12-24 19:24:47 +00002152 call assert_fails('au! * x bwipe', 'E1155:')
Bram Moolenaar4fb921e2017-12-18 15:33:00 +01002153endfunc
Bram Moolenaarb7407d32018-02-03 17:36:27 +01002154
Bram Moolenaarf6246f52022-02-11 16:30:12 +00002155func Test_autocmd_user()
2156 au User MyEvent let s:res = [expand("<afile>"), expand("<amatch>")]
2157 doautocmd User MyEvent
2158 call assert_equal(['MyEvent', 'MyEvent'], s:res)
2159 au! User
2160 unlet s:res
2161endfunc
2162
Bram Moolenaar3b014be2022-11-13 17:53:46 +00002163func Test_autocmd_user_clear_group()
2164 CheckRunVimInTerminal
2165
2166 let lines =<< trim END
2167 autocmd! User
2168 for i in range(1, 999)
2169 exe 'autocmd User ' .. 'Foo' .. i .. ' bar'
2170 endfor
2171 au CmdlineLeave : call timer_start(0, {-> execute('autocmd! User')})
2172 END
2173 call writefile(lines, 'XautoUser', 'D')
2174 let buf = RunVimInTerminal('-S XautoUser', {'rows': 10})
2175
2176 " this was using freed memory
2177 call term_sendkeys(buf, ":autocmd User\<CR>")
2178 call TermWait(buf, 50)
2179 call term_sendkeys(buf, "G")
2180
2181 call StopVimInTerminal(buf)
2182endfunc
2183
Bram Moolenaarb7407d32018-02-03 17:36:27 +01002184function s:Before_test_dirchanged()
2185 augroup test_dirchanged
2186 autocmd!
2187 augroup END
2188 let s:li = []
2189 let s:dir_this = getcwd()
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02002190 let s:dir_foo = s:dir_this . '/Xfoo'
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01002191 call mkdir(s:dir_foo)
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02002192 let s:dir_bar = s:dir_this . '/Xbar'
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01002193 call mkdir(s:dir_bar)
Bram Moolenaarb7407d32018-02-03 17:36:27 +01002194endfunc
2195
2196function s:After_test_dirchanged()
Bram Moolenaar3503d7c2019-11-09 20:10:17 +01002197 call chdir(s:dir_this)
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01002198 call delete(s:dir_foo, 'd')
2199 call delete(s:dir_bar, 'd')
Bram Moolenaarb7407d32018-02-03 17:36:27 +01002200 augroup test_dirchanged
2201 autocmd!
2202 augroup END
2203endfunc
2204
2205function Test_dirchanged_global()
2206 call s:Before_test_dirchanged()
Bram Moolenaarf6246f52022-02-11 16:30:12 +00002207 autocmd test_dirchanged DirChangedPre global call add(s:li, expand("<amatch>") .. " pre cd " .. v:event.directory)
Bram Moolenaarb7407d32018-02-03 17:36:27 +01002208 autocmd test_dirchanged DirChanged global call add(s:li, "cd:")
2209 autocmd test_dirchanged DirChanged global call add(s:li, expand("<afile>"))
Bram Moolenaar3503d7c2019-11-09 20:10:17 +01002210 call chdir(s:dir_foo)
Bram Moolenaarf6246f52022-02-11 16:30:12 +00002211 let expected = ["global pre cd " .. s:dir_foo, "cd:", s:dir_foo]
Bram Moolenaar28e8f732022-02-09 12:58:20 +00002212 call assert_equal(expected, s:li)
Bram Moolenaar3503d7c2019-11-09 20:10:17 +01002213 call chdir(s:dir_foo)
Bram Moolenaar28e8f732022-02-09 12:58:20 +00002214 call assert_equal(expected, s:li)
Bram Moolenaar3503d7c2019-11-09 20:10:17 +01002215 exe 'lcd ' .. fnameescape(s:dir_bar)
Bram Moolenaar28e8f732022-02-09 12:58:20 +00002216 call assert_equal(expected, s:li)
Bram Moolenaard8c9d322022-06-12 11:49:16 +01002217
2218 exe 'cd ' .. s:dir_foo
2219 exe 'cd ' .. s:dir_bar
2220 autocmd! test_dirchanged DirChanged global let g:result = expand("<afile>")
2221 cd -
Bram Moolenaardb77c492022-06-12 23:26:50 +01002222 call assert_equal(s:dir_foo, substitute(g:result, '\\', '/', 'g'))
Bram Moolenaard8c9d322022-06-12 11:49:16 +01002223
Bram Moolenaarb7407d32018-02-03 17:36:27 +01002224 call s:After_test_dirchanged()
2225endfunc
2226
2227function Test_dirchanged_local()
2228 call s:Before_test_dirchanged()
2229 autocmd test_dirchanged DirChanged window call add(s:li, "lcd:")
2230 autocmd test_dirchanged DirChanged window call add(s:li, expand("<afile>"))
Bram Moolenaar3503d7c2019-11-09 20:10:17 +01002231 call chdir(s:dir_foo)
Bram Moolenaarb7407d32018-02-03 17:36:27 +01002232 call assert_equal([], s:li)
Bram Moolenaar3503d7c2019-11-09 20:10:17 +01002233 exe 'lcd ' .. fnameescape(s:dir_bar)
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01002234 call assert_equal(["lcd:", s:dir_bar], s:li)
Bram Moolenaar3503d7c2019-11-09 20:10:17 +01002235 exe 'lcd ' .. fnameescape(s:dir_bar)
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01002236 call assert_equal(["lcd:", s:dir_bar], s:li)
Bram Moolenaarb7407d32018-02-03 17:36:27 +01002237 call s:After_test_dirchanged()
2238endfunc
2239
2240function Test_dirchanged_auto()
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +02002241 CheckOption autochdir
Bram Moolenaarb7407d32018-02-03 17:36:27 +01002242 call s:Before_test_dirchanged()
2243 call test_autochdir()
Bram Moolenaar28e8f732022-02-09 12:58:20 +00002244 autocmd test_dirchanged DirChangedPre auto call add(s:li, "pre cd " .. v:event.directory)
Bram Moolenaarb7407d32018-02-03 17:36:27 +01002245 autocmd test_dirchanged DirChanged auto call add(s:li, "auto:")
2246 autocmd test_dirchanged DirChanged auto call add(s:li, expand("<afile>"))
2247 set acd
Bram Moolenaar3503d7c2019-11-09 20:10:17 +01002248 cd ..
Bram Moolenaarb7407d32018-02-03 17:36:27 +01002249 call assert_equal([], s:li)
Bram Moolenaar61abe7d2022-08-30 21:46:08 +01002250 exe 'edit ' . s:dir_foo . '/Xautofile'
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01002251 call assert_equal(s:dir_foo, getcwd())
Bram Moolenaar28e8f732022-02-09 12:58:20 +00002252 let expected = ["pre cd " .. s:dir_foo, "auto:", s:dir_foo]
2253 call assert_equal(expected, s:li)
Bram Moolenaarb7407d32018-02-03 17:36:27 +01002254 set noacd
2255 bwipe!
2256 call s:After_test_dirchanged()
2257endfunc
Bram Moolenaar5a093432018-02-10 18:15:19 +01002258
2259" Test TextChangedI and TextChangedP
2260func Test_ChangedP()
2261 new
2262 call setline(1, ['foo', 'bar', 'foobar'])
2263 call test_override("char_avail", 1)
2264 set complete=. completeopt=menuone
2265
2266 func! TextChangedAutocmd(char)
2267 let g:autocmd .= a:char
2268 endfunc
2269
Christian Brabandtdb3b4462021-10-16 11:58:55 +01002270 " TextChanged will not be triggered, only check that it isn't.
Bram Moolenaar5a093432018-02-10 18:15:19 +01002271 au! TextChanged <buffer> :call TextChangedAutocmd('N')
2272 au! TextChangedI <buffer> :call TextChangedAutocmd('I')
2273 au! TextChangedP <buffer> :call TextChangedAutocmd('P')
2274
2275 call cursor(3, 1)
2276 let g:autocmd = ''
2277 call feedkeys("o\<esc>", 'tnix')
2278 call assert_equal('I', g:autocmd)
2279
2280 let g:autocmd = ''
2281 call feedkeys("Sf", 'tnix')
2282 call assert_equal('II', g:autocmd)
2283
2284 let g:autocmd = ''
2285 call feedkeys("Sf\<C-N>", 'tnix')
2286 call assert_equal('IIP', g:autocmd)
2287
2288 let g:autocmd = ''
2289 call feedkeys("Sf\<C-N>\<C-N>", 'tnix')
2290 call assert_equal('IIPP', g:autocmd)
2291
2292 let g:autocmd = ''
2293 call feedkeys("Sf\<C-N>\<C-N>\<C-N>", 'tnix')
2294 call assert_equal('IIPPP', g:autocmd)
2295
2296 let g:autocmd = ''
2297 call feedkeys("Sf\<C-N>\<C-N>\<C-N>\<C-N>", 'tnix')
2298 call assert_equal('IIPPPP', g:autocmd)
2299
2300 call assert_equal(['foo', 'bar', 'foobar', 'foo'], getline(1, '$'))
2301 " TODO: how should it handle completeopt=noinsert,noselect?
2302
2303 " CleanUp
2304 call test_override("char_avail", 0)
2305 au! TextChanged
2306 au! TextChangedI
2307 au! TextChangedP
2308 delfu TextChangedAutocmd
2309 unlet! g:autocmd
2310 set complete&vim completeopt&vim
2311
2312 bw!
2313endfunc
Bram Moolenaar8c64a362018-03-23 22:39:31 +01002314
Bram Moolenaar91d2e782018-08-07 19:05:01 +02002315let g:setline_handled = v:false
Bram Moolenaar1e115362019-01-09 23:01:02 +01002316func SetLineOne()
Bram Moolenaar91d2e782018-08-07 19:05:01 +02002317 if !g:setline_handled
2318 call setline(1, "(x)")
2319 let g:setline_handled = v:true
2320 endif
2321endfunc
2322
2323func Test_TextChangedI_with_setline()
2324 new
2325 call test_override('char_avail', 1)
2326 autocmd TextChangedI <buffer> call SetLineOne()
2327 call feedkeys("i(\<CR>\<Esc>", 'tx')
2328 call assert_equal('(', getline(1))
2329 call assert_equal('x)', getline(2))
2330 undo
Bram Moolenaar91d2e782018-08-07 19:05:01 +02002331 call assert_equal('', getline(1))
Bram Moolenaar9fa95062018-08-08 22:08:32 +02002332 call assert_equal('', getline(2))
Bram Moolenaar91d2e782018-08-07 19:05:01 +02002333
Bram Moolenaarca34db32022-01-20 11:17:18 +00002334 call test_override('char_avail', 0)
Bram Moolenaar91d2e782018-08-07 19:05:01 +02002335 bwipe!
2336endfunc
2337
Bram Moolenaar8c64a362018-03-23 22:39:31 +01002338func Test_Changed_FirstTime()
Bram Moolenaar8c5a2782019-08-07 23:07:07 +02002339 CheckFeature terminal
2340 CheckNotGui
Bram Moolenaar3cdcb092020-03-18 19:18:10 +01002341 " Starting a terminal to run Vim is always considered flaky.
Bram Moolenaar30d53e22020-03-18 21:10:44 +01002342 let g:test_is_flaky = 1
Bram Moolenaar8c5a2782019-08-07 23:07:07 +02002343
Bram Moolenaar8c64a362018-03-23 22:39:31 +01002344 " Prepare file for TextChanged event.
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01002345 call writefile([''], 'Xchanged.txt', 'D')
Bram Moolenaar8c64a362018-03-23 22:39:31 +01002346 let buf = term_start([GetVimProg(), '--clean', '-c', 'set noswapfile'], {'term_rows': 3})
2347 call assert_equal('running', term_getstatus(buf))
Bram Moolenaar1834d372018-03-29 17:40:46 +02002348 " Wait for the ruler (in the status line) to be shown.
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01002349 " In ConPTY, there is additional character which is drawn up to the width of
2350 " the screen.
2351 if has('conpty')
2352 call WaitForAssert({-> assert_match('\<All.*$', term_getline(buf, 3))})
2353 else
2354 call WaitForAssert({-> assert_match('\<All$', term_getline(buf, 3))})
2355 endif
Bram Moolenaar8c64a362018-03-23 22:39:31 +01002356 " It's only adding autocmd, so that no event occurs.
2357 call term_sendkeys(buf, ":au! TextChanged <buffer> call writefile(['No'], 'Xchanged.txt')\<cr>")
2358 call term_sendkeys(buf, "\<C-\\>\<C-N>:qa!\<cr>")
Bram Moolenaar50182fa2018-04-28 21:34:40 +02002359 call WaitForAssert({-> assert_equal('finished', term_getstatus(buf))})
Bram Moolenaar8c64a362018-03-23 22:39:31 +01002360 call assert_equal([''], readfile('Xchanged.txt'))
2361
2362 " clean up
Bram Moolenaar8c64a362018-03-23 22:39:31 +01002363 bwipe!
2364endfunc
Bram Moolenaar0566e892019-01-24 19:37:40 +01002365
Bram Moolenaareb93f3f2019-04-04 15:04:56 +02002366func Test_autocmd_nested()
2367 let g:did_nested = 0
2368 augroup Testing
2369 au WinNew * edit somefile
2370 au BufNew * let g:did_nested = 1
2371 augroup END
2372 split
2373 call assert_equal(0, g:did_nested)
2374 close
2375 bwipe! somefile
2376
2377 " old nested argument still works
2378 augroup Testing
2379 au!
2380 au WinNew * nested edit somefile
2381 au BufNew * let g:did_nested = 1
2382 augroup END
2383 split
2384 call assert_equal(1, g:did_nested)
2385 close
2386 bwipe! somefile
2387
2388 " New ++nested argument works
2389 augroup Testing
2390 au!
2391 au WinNew * ++nested edit somefile
2392 au BufNew * let g:did_nested = 1
2393 augroup END
2394 split
2395 call assert_equal(1, g:did_nested)
2396 close
2397 bwipe! somefile
2398
Bram Moolenaarf0775142022-03-04 20:10:38 +00002399 " nested without ++ does not work in Vim9 script
2400 call assert_fails('vim9cmd au WinNew * nested echo fails', 'E1078:')
2401
Bram Moolenaareb93f3f2019-04-04 15:04:56 +02002402 augroup Testing
2403 au!
2404 augroup END
2405
2406 call assert_fails('au WinNew * ++nested ++nested echo bad', 'E983:')
2407 call assert_fails('au WinNew * nested nested echo bad', 'E983:')
2408endfunc
2409
Bram Moolenaar5fa9f232022-07-23 09:06:48 +01002410func Test_autocmd_nested_cursor_invalid()
2411 set laststatus=0
2412 copen
2413 cclose
2414 call setline(1, ['foo', 'bar', 'baz'])
2415 3
2416 augroup nested_inv
2417 autocmd User foo ++nested copen
2418 autocmd BufAdd * let &laststatus = 2 - &laststatus
2419 augroup END
2420 doautocmd User foo
2421
2422 augroup nested_inv
2423 au!
2424 augroup END
2425 set laststatus&
Bram Moolenaarb03950f2022-07-26 13:47:13 +01002426 cclose
Bram Moolenaar5fa9f232022-07-23 09:06:48 +01002427 bwipe!
2428endfunc
2429
Bram Moolenaar3d6ee8b2022-07-27 15:23:35 +01002430func Test_autocmd_nested_keeps_cursor_pos()
2431 enew
2432 call setline(1, 'foo')
2433 autocmd User foo ++nested normal! $a
2434 autocmd InsertLeave * :
2435 doautocmd User foo
2436 call assert_equal([0, 1, 3, 0], getpos('.'))
2437
2438 bwipe!
2439endfunc
2440
Bram Moolenaarb03950f2022-07-26 13:47:13 +01002441func Test_autocmd_nested_switch_window()
2442 " run this in a separate Vim so that SafeState works
2443 CheckRunVimInTerminal
2444
2445 let lines =<< trim END
2446 vim9script
2447 ['()']->writefile('Xautofile')
2448 autocmd VimEnter * ++nested edit Xautofile | split
2449 autocmd BufReadPost * autocmd SafeState * ++once foldclosed('.')
2450 autocmd WinEnter * matchadd('ErrorMsg', 'pat')
2451 END
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01002452 call writefile(lines, 'Xautoscript', 'D')
Bram Moolenaarb03950f2022-07-26 13:47:13 +01002453 let buf = RunVimInTerminal('-S Xautoscript', {'rows': 10})
2454 call VerifyScreenDump(buf, 'Test_autocmd_nested_switch', {})
2455
2456 call StopVimInTerminal(buf)
2457 call delete('Xautofile')
Bram Moolenaarb03950f2022-07-26 13:47:13 +01002458endfunc
2459
Bram Moolenaareb93f3f2019-04-04 15:04:56 +02002460func Test_autocmd_once()
2461 " Without ++once WinNew triggers twice
2462 let g:did_split = 0
2463 augroup Testing
2464 au WinNew * let g:did_split += 1
2465 augroup END
2466 split
2467 split
2468 call assert_equal(2, g:did_split)
2469 call assert_true(exists('#WinNew'))
2470 close
2471 close
2472
2473 " With ++once WinNew triggers once
2474 let g:did_split = 0
2475 augroup Testing
2476 au!
2477 au WinNew * ++once let g:did_split += 1
2478 augroup END
2479 split
2480 split
2481 call assert_equal(1, g:did_split)
2482 call assert_false(exists('#WinNew'))
2483 close
2484 close
2485
2486 call assert_fails('au WinNew * ++once ++once echo bad', 'E983:')
2487endfunc
2488
Bram Moolenaara68e5952019-04-25 22:22:01 +02002489func Test_autocmd_bufreadpre()
2490 new
2491 let b:bufreadpre = 1
Bram Moolenaarab505b12020-03-23 19:28:44 +01002492 call append(0, range(1000))
Bram Moolenaara68e5952019-04-25 22:22:01 +02002493 w! XAutocmdBufReadPre.txt
2494 autocmd BufReadPre <buffer> :let b:bufreadpre += 1
Bram Moolenaarab505b12020-03-23 19:28:44 +01002495 norm! 500gg
Bram Moolenaara68e5952019-04-25 22:22:01 +02002496 sp
Bram Moolenaarab505b12020-03-23 19:28:44 +01002497 norm! 1000gg
Bram Moolenaara68e5952019-04-25 22:22:01 +02002498 wincmd p
2499 let g:wsv1 = winsaveview()
2500 wincmd p
2501 let g:wsv2 = winsaveview()
2502 " triggers BufReadPre, should not move the cursor in either window
2503 " The topline may change one line in a large window.
2504 edit
2505 call assert_inrange(g:wsv2.topline - 1, g:wsv2.topline + 1, winsaveview().topline)
2506 call assert_equal(g:wsv2.lnum, winsaveview().lnum)
2507 call assert_equal(2, b:bufreadpre)
2508 wincmd p
2509 call assert_equal(g:wsv1.topline, winsaveview().topline)
2510 call assert_equal(g:wsv1.lnum, winsaveview().lnum)
2511 call assert_equal(2, b:bufreadpre)
2512 " Now set the cursor position in an BufReadPre autocommand
2513 " (even though the position will be invalid, this should make Vim reset the
2514 " cursor position in the other window.
2515 wincmd p
2516 set cpo+=g
2517 " won't do anything, but try to set the cursor on an invalid lnum
2518 autocmd BufReadPre <buffer> :norm! 70gg
2519 " triggers BufReadPre, should not move the cursor in either window
2520 e
2521 call assert_equal(1, winsaveview().topline)
2522 call assert_equal(1, winsaveview().lnum)
2523 call assert_equal(3, b:bufreadpre)
2524 wincmd p
2525 call assert_equal(g:wsv1.topline, winsaveview().topline)
2526 call assert_equal(g:wsv1.lnum, winsaveview().lnum)
2527 call assert_equal(3, b:bufreadpre)
2528 close
2529 close
2530 call delete('XAutocmdBufReadPre.txt')
2531 set cpo-=g
2532endfunc
2533
Bram Moolenaar5e66b422019-01-24 21:58:10 +01002534" FileChangedShell tested in test_filechanged.vim
Bram Moolenaar69ea5872019-04-25 20:29:00 +02002535
2536" Tests for the following autocommands:
2537" - FileWritePre writing a compressed file
2538" - FileReadPost reading a compressed file
2539" - BufNewFile reading a file template
2540" - BufReadPre decompressing the file to be read
2541" - FilterReadPre substituting characters in the temp file
2542" - FilterReadPost substituting characters after filtering
2543" - FileReadPre set options for decompression
2544" - FileReadPost decompress the file
2545func Test_ReadWrite_Autocmds()
2546 " Run this test only on Unix-like systems and if gzip is available
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +02002547 CheckUnix
2548 CheckExecutable gzip
Bram Moolenaar69ea5872019-04-25 20:29:00 +02002549
2550 " Make $GZIP empty, "-v" would cause trouble.
2551 let $GZIP = ""
2552
2553 " Use a FileChangedShell autocommand to avoid a prompt for 'Xtestfile.gz'
2554 " being modified outside of Vim (noticed on Solaris).
2555 au FileChangedShell * echo 'caught FileChangedShell'
2556
2557 " Test for the FileReadPost, FileWritePre and FileWritePost autocmds
2558 augroup Test1
2559 au!
2560 au FileWritePre *.gz '[,']!gzip
2561 au FileWritePost *.gz undo
2562 au FileReadPost *.gz '[,']!gzip -d
2563 augroup END
2564
2565 new
2566 set bin
2567 call append(0, [
2568 \ 'line 2 Abcdefghijklmnopqrstuvwxyz',
2569 \ 'line 3 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2570 \ 'line 4 Abcdefghijklmnopqrstuvwxyz',
2571 \ 'line 5 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2572 \ 'line 6 Abcdefghijklmnopqrstuvwxyz',
2573 \ 'line 7 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2574 \ 'line 8 Abcdefghijklmnopqrstuvwxyz',
2575 \ 'line 9 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2576 \ 'line 10 Abcdefghijklmnopqrstuvwxyz'
2577 \ ])
2578 1,9write! Xtestfile.gz
2579 enew! | close
2580
2581 new
2582 " Read and decompress the testfile
2583 0read Xtestfile.gz
2584 call assert_equal([
2585 \ 'line 2 Abcdefghijklmnopqrstuvwxyz',
2586 \ 'line 3 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2587 \ 'line 4 Abcdefghijklmnopqrstuvwxyz',
2588 \ 'line 5 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2589 \ 'line 6 Abcdefghijklmnopqrstuvwxyz',
2590 \ 'line 7 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2591 \ 'line 8 Abcdefghijklmnopqrstuvwxyz',
2592 \ 'line 9 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2593 \ 'line 10 Abcdefghijklmnopqrstuvwxyz'
2594 \ ], getline(1, 9))
2595 enew! | close
2596
2597 augroup Test1
2598 au!
2599 augroup END
2600
2601 " Test for the FileAppendPre and FileAppendPost autocmds
2602 augroup Test2
2603 au!
2604 au BufNewFile *.c read Xtest.c
2605 au FileAppendPre *.out '[,']s/new/NEW/
2606 au FileAppendPost *.out !cat Xtest.c >> test.out
2607 augroup END
2608
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01002609 call writefile(['/*', ' * Here is a new .c file', ' */'], 'Xtest.c', 'D')
Bram Moolenaar69ea5872019-04-25 20:29:00 +02002610 new foo.c " should load Xtest.c
2611 call assert_equal(['/*', ' * Here is a new .c file', ' */'], getline(2, 4))
2612 w! >> test.out " append it to the output file
2613
2614 let contents = readfile('test.out')
2615 call assert_equal(' * Here is a NEW .c file', contents[2])
2616 call assert_equal(' * Here is a new .c file', contents[5])
2617
2618 call delete('test.out')
2619 enew! | close
2620 augroup Test2
2621 au!
2622 augroup END
2623
2624 " Test for the BufReadPre and BufReadPost autocmds
2625 augroup Test3
2626 au!
2627 " setup autocommands to decompress before reading and re-compress
2628 " afterwards
2629 au BufReadPre *.gz exe '!gzip -d ' . shellescape(expand("<afile>"))
2630 au BufReadPre *.gz call rename(expand("<afile>:r"), expand("<afile>"))
2631 au BufReadPost *.gz call rename(expand("<afile>"), expand("<afile>:r"))
2632 au BufReadPost *.gz exe '!gzip ' . shellescape(expand("<afile>:r"))
2633 augroup END
2634
2635 e! Xtestfile.gz " Edit compressed file
2636 call assert_equal([
2637 \ 'line 2 Abcdefghijklmnopqrstuvwxyz',
2638 \ 'line 3 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2639 \ 'line 4 Abcdefghijklmnopqrstuvwxyz',
2640 \ 'line 5 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2641 \ 'line 6 Abcdefghijklmnopqrstuvwxyz',
2642 \ 'line 7 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2643 \ 'line 8 Abcdefghijklmnopqrstuvwxyz',
2644 \ 'line 9 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2645 \ 'line 10 Abcdefghijklmnopqrstuvwxyz'
2646 \ ], getline(1, 9))
2647
2648 w! >> test.out " Append it to the output file
2649
2650 augroup Test3
2651 au!
2652 augroup END
2653
2654 " Test for the FilterReadPre and FilterReadPost autocmds.
2655 set shelltemp " need temp files here
2656 augroup Test4
2657 au!
2658 au FilterReadPre *.out call rename(expand("<afile>"), expand("<afile>") . ".t")
2659 au FilterReadPre *.out exe 'silent !sed s/e/E/ ' . shellescape(expand("<afile>")) . ".t >" . shellescape(expand("<afile>"))
2660 au FilterReadPre *.out exe 'silent !rm ' . shellescape(expand("<afile>")) . '.t'
2661 au FilterReadPost *.out '[,']s/x/X/g
2662 augroup END
2663
2664 e! test.out " Edit the output file
2665 1,$!cat
2666 call assert_equal([
2667 \ 'linE 2 AbcdefghijklmnopqrstuvwXyz',
2668 \ 'linE 3 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
2669 \ 'linE 4 AbcdefghijklmnopqrstuvwXyz',
2670 \ 'linE 5 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
2671 \ 'linE 6 AbcdefghijklmnopqrstuvwXyz',
2672 \ 'linE 7 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
2673 \ 'linE 8 AbcdefghijklmnopqrstuvwXyz',
2674 \ 'linE 9 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
2675 \ 'linE 10 AbcdefghijklmnopqrstuvwXyz'
2676 \ ], getline(1, 9))
2677 call assert_equal([
2678 \ 'line 2 Abcdefghijklmnopqrstuvwxyz',
2679 \ 'line 3 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2680 \ 'line 4 Abcdefghijklmnopqrstuvwxyz',
2681 \ 'line 5 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2682 \ 'line 6 Abcdefghijklmnopqrstuvwxyz',
2683 \ 'line 7 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2684 \ 'line 8 Abcdefghijklmnopqrstuvwxyz',
2685 \ 'line 9 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2686 \ 'line 10 Abcdefghijklmnopqrstuvwxyz'
2687 \ ], readfile('test.out'))
2688
2689 augroup Test4
2690 au!
2691 augroup END
2692 set shelltemp&vim
2693
2694 " Test for the FileReadPre and FileReadPost autocmds.
2695 augroup Test5
2696 au!
2697 au FileReadPre *.gz exe 'silent !gzip -d ' . shellescape(expand("<afile>"))
2698 au FileReadPre *.gz call rename(expand("<afile>:r"), expand("<afile>"))
2699 au FileReadPost *.gz '[,']s/l/L/
2700 augroup END
2701
2702 new
2703 0r Xtestfile.gz " Read compressed file
2704 call assert_equal([
2705 \ 'Line 2 Abcdefghijklmnopqrstuvwxyz',
2706 \ 'Line 3 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2707 \ 'Line 4 Abcdefghijklmnopqrstuvwxyz',
2708 \ 'Line 5 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2709 \ 'Line 6 Abcdefghijklmnopqrstuvwxyz',
2710 \ 'Line 7 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2711 \ 'Line 8 Abcdefghijklmnopqrstuvwxyz',
2712 \ 'Line 9 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2713 \ 'Line 10 Abcdefghijklmnopqrstuvwxyz'
2714 \ ], getline(1, 9))
2715 call assert_equal([
2716 \ 'line 2 Abcdefghijklmnopqrstuvwxyz',
2717 \ 'line 3 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2718 \ 'line 4 Abcdefghijklmnopqrstuvwxyz',
2719 \ 'line 5 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2720 \ 'line 6 Abcdefghijklmnopqrstuvwxyz',
2721 \ 'line 7 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2722 \ 'line 8 Abcdefghijklmnopqrstuvwxyz',
2723 \ 'line 9 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2724 \ 'line 10 Abcdefghijklmnopqrstuvwxyz'
2725 \ ], readfile('Xtestfile.gz'))
2726
2727 augroup Test5
2728 au!
2729 augroup END
2730
2731 au! FileChangedShell
2732 call delete('Xtestfile.gz')
Bram Moolenaar69ea5872019-04-25 20:29:00 +02002733 call delete('test.out')
2734endfunc
Bram Moolenaar23b51392019-05-09 21:38:43 +02002735
2736func Test_throw_in_BufWritePre()
2737 new
2738 call setline(1, ['one', 'two', 'three'])
2739 call assert_false(filereadable('Xthefile'))
2740 augroup throwing
2741 au BufWritePre X* throw 'do not write'
2742 augroup END
2743 try
2744 w Xthefile
2745 catch
2746 let caught = 1
2747 endtry
2748 call assert_equal(1, caught)
2749 call assert_false(filereadable('Xthefile'))
2750
2751 bwipe!
2752 au! throwing
2753endfunc
Bram Moolenaarcadbe1b2019-09-22 21:50:09 +02002754
Bram Moolenaar40fa12a2021-09-22 14:18:13 +02002755func Test_autocmd_in_try_block()
Bram Moolenaar6f14da12022-09-07 21:30:44 +01002756 call mkdir('Xintrydir', 'R')
Bram Moolenaar40fa12a2021-09-22 14:18:13 +02002757 au BufEnter * let g:fname = expand('%')
2758 try
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +01002759 edit Xintrydir/
Bram Moolenaar40fa12a2021-09-22 14:18:13 +02002760 endtry
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +01002761 call assert_match('Xintrydir', g:fname)
Bram Moolenaar40fa12a2021-09-22 14:18:13 +02002762
2763 unlet g:fname
2764 au! BufEnter
Bram Moolenaar40fa12a2021-09-22 14:18:13 +02002765endfunc
2766
Bram Moolenaarcadbe1b2019-09-22 21:50:09 +02002767func Test_autocmd_SafeState()
2768 CheckRunVimInTerminal
2769
2770 let lines =<< trim END
2771 let g:safe = 0
2772 let g:again = ''
2773 au SafeState * let g:safe += 1
2774 au SafeStateAgain * let g:again ..= 'x'
2775 func CallTimer()
2776 call timer_start(10, {id -> execute('let g:again ..= "t"')})
2777 endfunc
2778 END
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01002779 call writefile(lines, 'XSafeState', 'D')
Bram Moolenaarcadbe1b2019-09-22 21:50:09 +02002780 let buf = RunVimInTerminal('-S XSafeState', #{rows: 6})
2781
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01002782 " Sometimes we loop to handle a K_IGNORE, SafeState may be triggered once or
Bram Moolenaar8fb1b472020-02-23 16:16:26 +01002783 " more often.
Bram Moolenaarcadbe1b2019-09-22 21:50:09 +02002784 call term_sendkeys(buf, ":echo g:safe\<CR>")
Bram Moolenaar8fb1b472020-02-23 16:16:26 +01002785 call WaitForAssert({-> assert_match('^\d ', term_getline(buf, 6))}, 1000)
Bram Moolenaarcadbe1b2019-09-22 21:50:09 +02002786
Bram Moolenaar8fb1b472020-02-23 16:16:26 +01002787 " SafeStateAgain should be invoked at least three times
Bram Moolenaarcadbe1b2019-09-22 21:50:09 +02002788 call term_sendkeys(buf, ":echo g:again\<CR>")
Bram Moolenaar8fb1b472020-02-23 16:16:26 +01002789 call WaitForAssert({-> assert_match('^xxx', term_getline(buf, 6))}, 1000)
Bram Moolenaarcadbe1b2019-09-22 21:50:09 +02002790
2791 call term_sendkeys(buf, ":let g:again = ''\<CR>:call CallTimer()\<CR>")
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02002792 call TermWait(buf, 50)
Bram Moolenaar0f6629a2019-09-22 23:24:13 +02002793 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02002794 call TermWait(buf, 50)
Bram Moolenaarcadbe1b2019-09-22 21:50:09 +02002795 call term_sendkeys(buf, ":echo g:again\<CR>")
2796 call WaitForAssert({-> assert_match('xtx', term_getline(buf, 6))}, 1000)
2797
2798 call StopVimInTerminal(buf)
Bram Moolenaarcadbe1b2019-09-22 21:50:09 +02002799endfunc
Bram Moolenaar23324a02019-10-01 17:39:04 +02002800
2801func Test_autocmd_CmdWinEnter()
2802 CheckRunVimInTerminal
Bram Moolenaar21829c52021-01-26 22:42:21 +01002803
Bram Moolenaar23324a02019-10-01 17:39:04 +02002804 let lines =<< trim END
Egor Zvorykin125ffd22021-11-17 14:01:14 +00002805 augroup vimHints | au! | augroup END
Bram Moolenaar23324a02019-10-01 17:39:04 +02002806 let b:dummy_var = 'This is a dummy'
2807 autocmd CmdWinEnter * quit
2808 let winnr = winnr('$')
2809 END
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01002810 let filename = 'XCmdWinEnter'
Bram Moolenaar23324a02019-10-01 17:39:04 +02002811 call writefile(lines, filename)
2812 let buf = RunVimInTerminal('-S '.filename, #{rows: 6})
2813
2814 call term_sendkeys(buf, "q:")
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02002815 call TermWait(buf)
Bram Moolenaar23324a02019-10-01 17:39:04 +02002816 call term_sendkeys(buf, ":echo b:dummy_var\<cr>")
Bram Moolenaar353c3512020-03-15 14:19:26 +01002817 call WaitForAssert({-> assert_match('^This is a dummy', term_getline(buf, 6))}, 2000)
Bram Moolenaar23324a02019-10-01 17:39:04 +02002818 call term_sendkeys(buf, ":echo &buftype\<cr>")
2819 call WaitForAssert({-> assert_notmatch('^nofile', term_getline(buf, 6))}, 1000)
2820 call term_sendkeys(buf, ":echo winnr\<cr>")
2821 call WaitForAssert({-> assert_match('^1', term_getline(buf, 6))}, 1000)
2822
2823 " clean up
2824 call StopVimInTerminal(buf)
2825 call delete(filename)
2826endfunc
Bram Moolenaarec66c412019-10-11 21:19:13 +02002827
2828func Test_autocmd_was_using_freed_memory()
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01002829 CheckFeature quickfix
2830
Bram Moolenaarec66c412019-10-11 21:19:13 +02002831 pedit xx
2832 n x
Bram Moolenaar92bb83e2021-02-03 23:04:46 +01002833 augroup winenter
2834 au WinEnter * if winnr('$') > 2 | quit | endif
2835 augroup END
Bram Moolenaarec66c412019-10-11 21:19:13 +02002836 split
Bram Moolenaar92bb83e2021-02-03 23:04:46 +01002837
2838 augroup winenter
2839 au! WinEnter
2840 augroup END
2841
2842 bwipe xx
2843 bwipe x
2844 pclose
Bram Moolenaarec66c412019-10-11 21:19:13 +02002845endfunc
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +01002846
2847func Test_BufWrite_lockmarks()
Bram Moolenaarf08b0eb2021-10-16 13:00:14 +01002848 let g:test_is_flaky = 1
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +01002849 edit! Xtest
2850 call setline(1, ['a', 'b', 'c', 'd'])
2851
2852 " :lockmarks preserves the marks
2853 call SetChangeMarks(2, 3)
2854 lockmarks write
2855 call assert_equal([2, 3], [line("'["), line("']")])
2856
2857 " *WritePre autocmds get the correct line range, but lockmarks preserves the
2858 " original values for the user
2859 augroup lockmarks
2860 au!
2861 au BufWritePre,FilterWritePre * call assert_equal([1, 4], [line("'["), line("']")])
2862 au FileWritePre * call assert_equal([3, 4], [line("'["), line("']")])
2863 augroup END
2864
2865 lockmarks write
2866 call assert_equal([2, 3], [line("'["), line("']")])
2867
2868 if executable('cat')
2869 lockmarks %!cat
2870 call assert_equal([2, 3], [line("'["), line("']")])
2871 endif
2872
2873 lockmarks 3,4write Xtest2
2874 call assert_equal([2, 3], [line("'["), line("']")])
2875
2876 au! lockmarks
2877 augroup! lockmarks
2878 call delete('Xtest')
2879 call delete('Xtest2')
2880endfunc
Bram Moolenaarce6db022020-01-07 20:11:42 +01002881
2882func Test_FileType_spell()
2883 if !isdirectory('/tmp')
2884 throw "Skipped: requires /tmp directory"
2885 endif
2886
2887 " this was crashing with an invalid free()
2888 setglobal spellfile=/tmp/en.utf-8.add
2889 augroup crash
2890 autocmd!
2891 autocmd BufNewFile,BufReadPost crashfile setf somefiletype
2892 autocmd BufNewFile,BufReadPost crashfile set ft=anotherfiletype
2893 autocmd FileType anotherfiletype setlocal spell
2894 augroup END
2895 func! NoCrash() abort
2896 edit /tmp/crashfile
2897 endfunc
2898 call NoCrash()
2899
2900 au! crash
2901 setglobal spellfile=
2902endfunc
Bram Moolenaarbc2b71d2020-02-17 21:33:30 +01002903
Bram Moolenaaref976322022-09-28 11:48:30 +01002904" this was wiping out the current buffer and using freed memory
2905func Test_SpellFileMissing_bwipe()
2906 next 0
2907 au SpellFileMissing 0 bwipe
2908 call assert_fails('set spell spelllang=0', 'E937:')
2909
2910 au! SpellFileMissing
2911 bwipe
2912endfunc
2913
Bram Moolenaar406cd902020-02-18 21:54:41 +01002914" Test closing a window or editing another buffer from a FileChangedRO handler
2915" in a readonly buffer
2916func Test_FileChangedRO_winclose()
Bram Moolenaar62cd26a2020-10-11 20:08:44 +02002917 call test_override('ui_delay', 10)
2918
Bram Moolenaar406cd902020-02-18 21:54:41 +01002919 augroup FileChangedROTest
2920 au!
2921 autocmd FileChangedRO * quit
2922 augroup END
2923 new
2924 set readonly
2925 call assert_fails('normal i', 'E788:')
2926 close
2927 augroup! FileChangedROTest
2928
2929 augroup FileChangedROTest
2930 au!
Bram Moolenaar61abe7d2022-08-30 21:46:08 +01002931 autocmd FileChangedRO * edit Xrofile
Bram Moolenaar406cd902020-02-18 21:54:41 +01002932 augroup END
2933 new
2934 set readonly
2935 call assert_fails('normal i', 'E788:')
2936 close
2937 augroup! FileChangedROTest
Bram Moolenaar62cd26a2020-10-11 20:08:44 +02002938 call test_override('ALL', 0)
Bram Moolenaar406cd902020-02-18 21:54:41 +01002939endfunc
2940
Bram Moolenaar0c81d1b2020-02-22 22:45:55 +01002941func LogACmd()
2942 call add(g:logged, line('$'))
2943endfunc
2944
2945func Test_TermChanged()
Bram Moolenaard28e0b32020-02-22 23:08:52 +01002946 CheckNotGui
2947
Bram Moolenaar0c81d1b2020-02-22 22:45:55 +01002948 enew!
2949 tabnew
2950 call setline(1, ['a', 'b', 'c', 'd'])
2951 $
2952 au TermChanged * call LogACmd()
2953 let g:logged = []
2954 let term_save = &term
2955 set term=xterm
2956 call assert_equal([1, 4], g:logged)
2957
2958 au! TermChanged
2959 let &term = term_save
2960 bwipe!
2961endfunc
2962
Bram Moolenaare3284872020-03-19 13:55:03 +01002963" Test for FileReadCmd autocmd
2964func Test_autocmd_FileReadCmd()
2965 func ReadFileCmd()
2966 call append(line('$'), "v:cmdarg = " .. v:cmdarg)
2967 endfunc
2968 augroup FileReadCmdTest
2969 au!
2970 au FileReadCmd Xtest call ReadFileCmd()
2971 augroup END
2972
2973 new
2974 read ++bin Xtest
2975 read ++nobin Xtest
2976 read ++edit Xtest
2977 read ++bad=keep Xtest
2978 read ++bad=drop Xtest
2979 read ++bad=- Xtest
2980 read ++ff=unix Xtest
2981 read ++ff=dos Xtest
2982 read ++ff=mac Xtest
2983 read ++enc=utf-8 Xtest
2984
2985 call assert_equal(['',
2986 \ 'v:cmdarg = ++bin',
2987 \ 'v:cmdarg = ++nobin',
2988 \ 'v:cmdarg = ++edit',
2989 \ 'v:cmdarg = ++bad=keep',
2990 \ 'v:cmdarg = ++bad=drop',
2991 \ 'v:cmdarg = ++bad=-',
2992 \ 'v:cmdarg = ++ff=unix',
2993 \ 'v:cmdarg = ++ff=dos',
2994 \ 'v:cmdarg = ++ff=mac',
2995 \ 'v:cmdarg = ++enc=utf-8'], getline(1, '$'))
2996
2997 close!
2998 augroup FileReadCmdTest
2999 au!
3000 augroup END
3001 delfunc ReadFileCmd
3002endfunc
3003
Bram Moolenaaree4e0c12020-04-06 21:35:05 +02003004" Test for passing invalid arguments to autocmd
3005func Test_autocmd_invalid_args()
3006 " Additional character after * for event
Bram Moolenaar61abe7d2022-08-30 21:46:08 +01003007 call assert_fails('autocmd *a Xinvfile set ff=unix', 'E215:')
Bram Moolenaaree4e0c12020-04-06 21:35:05 +02003008 augroup Test
3009 augroup END
3010 " Invalid autocmd event
Bram Moolenaar61abe7d2022-08-30 21:46:08 +01003011 call assert_fails('autocmd Bufabc Xinvfile set ft=vim', 'E216:')
Bram Moolenaaree4e0c12020-04-06 21:35:05 +02003012 " Invalid autocmd event in a autocmd group
Bram Moolenaar61abe7d2022-08-30 21:46:08 +01003013 call assert_fails('autocmd Test Bufabc Xinvfile set ft=vim', 'E216:')
Bram Moolenaaree4e0c12020-04-06 21:35:05 +02003014 augroup! Test
3015 " Execute all autocmds
3016 call assert_fails('doautocmd * BufEnter', 'E217:')
3017 call assert_fails('augroup! x1a2b3', 'E367:')
3018 call assert_fails('autocmd BufNew <buffer=999> pwd', 'E680:')
Bram Moolenaar531be472020-09-23 22:38:05 +02003019 call assert_fails('autocmd BufNew \) set ff=unix', 'E55:')
Bram Moolenaaree4e0c12020-04-06 21:35:05 +02003020endfunc
3021
3022" Test for deep nesting of autocmds
3023func Test_autocmd_deep_nesting()
Bram Moolenaar61abe7d2022-08-30 21:46:08 +01003024 autocmd BufEnter Xdeepfile doautocmd BufEnter Xdeepfile
3025 call assert_fails('doautocmd BufEnter Xdeepfile', 'E218:')
3026 autocmd! BufEnter Xdeepfile
Bram Moolenaaree4e0c12020-04-06 21:35:05 +02003027endfunc
3028
Bram Moolenaarbe5ee862020-06-10 20:56:58 +02003029" Tests for SigUSR1 autocmd event, which is only available on posix systems.
3030func Test_autocmd_sigusr1()
3031 CheckUnix
Bram Moolenaar0056ca72022-09-23 21:26:39 +01003032 " FIXME: should this work on MacOS M1?
3033 CheckNotMacM1
Bram Moolenaar62cd26a2020-10-11 20:08:44 +02003034 CheckExecutable /bin/kill
Bram Moolenaarbe5ee862020-06-10 20:56:58 +02003035
3036 let g:sigusr1_passed = 0
3037 au SigUSR1 * let g:sigusr1_passed = 1
3038 call system('/bin/kill -s usr1 ' . getpid())
3039 call WaitForAssert({-> assert_true(g:sigusr1_passed)})
3040
3041 au! SigUSR1
3042 unlet g:sigusr1_passed
3043endfunc
3044
Bram Moolenaarb340bae2020-06-15 19:51:56 +02003045" Test for BufReadPre autocmd deleting the file
3046func Test_BufReadPre_delfile()
3047 augroup TestAuCmd
3048 au!
Bram Moolenaare7cda972022-08-29 11:02:59 +01003049 autocmd BufReadPre XbufreadPre call delete('XbufreadPre')
Bram Moolenaarb340bae2020-06-15 19:51:56 +02003050 augroup END
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01003051 call writefile([], 'XbufreadPre', 'D')
Bram Moolenaare7cda972022-08-29 11:02:59 +01003052 call assert_fails('new XbufreadPre', 'E200:')
3053 call assert_equal('XbufreadPre', @%)
Bram Moolenaarb340bae2020-06-15 19:51:56 +02003054 call assert_equal(1, &readonly)
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01003055
Bram Moolenaarb340bae2020-06-15 19:51:56 +02003056 augroup TestAuCmd
3057 au!
3058 augroup END
3059 close!
3060endfunc
3061
3062" Test for BufReadPre autocmd changing the current buffer
3063func Test_BufReadPre_changebuf()
3064 augroup TestAuCmd
3065 au!
Bram Moolenaare7cda972022-08-29 11:02:59 +01003066 autocmd BufReadPre Xchangebuf edit Xsomeotherfile
Bram Moolenaarb340bae2020-06-15 19:51:56 +02003067 augroup END
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01003068 call writefile([], 'Xchangebuf', 'D')
Bram Moolenaare7cda972022-08-29 11:02:59 +01003069 call assert_fails('new Xchangebuf', 'E201:')
Bram Moolenaarb340bae2020-06-15 19:51:56 +02003070 call assert_equal('Xsomeotherfile', @%)
3071 call assert_equal(1, &readonly)
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01003072
Bram Moolenaarb340bae2020-06-15 19:51:56 +02003073 augroup TestAuCmd
3074 au!
3075 augroup END
3076 close!
3077endfunc
3078
3079" Test for BufWipeouti autocmd changing the current buffer when reading a file
3080" in an empty buffer with 'f' flag in 'cpo'
3081func Test_BufDelete_changebuf()
3082 new
3083 augroup TestAuCmd
3084 au!
3085 autocmd BufWipeout * let bufnr = bufadd('somefile') | exe "b " .. bufnr
3086 augroup END
3087 let save_cpo = &cpo
3088 set cpo+=f
Bram Moolenaarb18b4962022-09-02 21:55:50 +01003089 call assert_fails('r Xchangebuf', ['E812:', 'E484:'])
Bram Moolenaarb340bae2020-06-15 19:51:56 +02003090 call assert_equal('somefile', @%)
3091 let &cpo = save_cpo
3092 augroup TestAuCmd
3093 au!
3094 augroup END
3095 close!
3096endfunc
3097
Bram Moolenaar0fe937f2020-06-16 22:42:04 +02003098" Test for the temporary internal window used to execute autocmds
3099func Test_autocmd_window()
3100 %bw!
3101 edit one.txt
3102 tabnew two.txt
Bram Moolenaar41cd8032021-03-13 15:47:56 +01003103 vnew three.txt
3104 tabnew four.txt
3105 tabprevious
Bram Moolenaar0fe937f2020-06-16 22:42:04 +02003106 let g:blist = []
Bram Moolenaar832adf92020-06-25 19:01:36 +02003107 augroup aucmd_win_test1
Bram Moolenaar0fe937f2020-06-16 22:42:04 +02003108 au!
3109 au BufEnter * call add(g:blist, [expand('<afile>'),
3110 \ win_gettype(bufwinnr(expand('<afile>')))])
3111 augroup END
3112
3113 doautoall BufEnter
Bram Moolenaar41cd8032021-03-13 15:47:56 +01003114 call assert_equal([
3115 \ ['one.txt', 'autocmd'],
3116 \ ['two.txt', ''],
3117 \ ['four.txt', 'autocmd'],
3118 \ ['three.txt', ''],
3119 \ ], g:blist)
Bram Moolenaar0fe937f2020-06-16 22:42:04 +02003120
Bram Moolenaar832adf92020-06-25 19:01:36 +02003121 augroup aucmd_win_test1
Bram Moolenaar0fe937f2020-06-16 22:42:04 +02003122 au!
3123 augroup END
Bram Moolenaar832adf92020-06-25 19:01:36 +02003124 augroup! aucmd_win_test1
3125 %bw!
3126endfunc
3127
3128" Test for trying to close the temporary window used for executing an autocmd
3129func Test_close_autocmd_window()
3130 %bw!
3131 edit one.txt
3132 tabnew two.txt
3133 augroup aucmd_win_test2
3134 au!
3135 au BufEnter * if expand('<afile>') == 'one.txt' | 1close | endif
3136 augroup END
3137
3138 call assert_fails('doautoall BufEnter', 'E813:')
3139
3140 augroup aucmd_win_test2
3141 au!
3142 augroup END
3143 augroup! aucmd_win_test2
Bram Moolenaarcf844172020-06-26 19:44:06 +02003144 %bwipe!
3145endfunc
3146
3147" Test for trying to close the tab that has the temporary window for exeucing
3148" an autocmd.
3149func Test_close_autocmd_tab()
3150 edit one.txt
3151 tabnew two.txt
3152 augroup aucmd_win_test
3153 au!
3154 au BufEnter * if expand('<afile>') == 'one.txt' | tabfirst | tabonly | endif
3155 augroup END
3156
3157 call assert_fails('doautoall BufEnter', 'E813:')
3158
3159 tabonly
3160 augroup aucmd_win_test
3161 au!
3162 augroup END
3163 augroup! aucmd_win_test
3164 %bwipe!
Bram Moolenaar0fe937f2020-06-16 22:42:04 +02003165endfunc
3166
Bram Moolenaarcb1956d2022-01-07 15:45:18 +00003167func Test_Visual_doautoall_redraw()
3168 call setline(1, ['a', 'b'])
3169 new
3170 wincmd p
3171 call feedkeys("G\<C-V>", 'txn')
3172 autocmd User Explode ++once redraw
3173 doautoall User Explode
3174 %bwipe!
3175endfunc
3176
Bram Moolenaar6bcb8772021-02-03 21:23:29 +01003177" This was using freed memory.
3178func Test_BufNew_arglocal()
3179 arglocal
3180 au BufNew * arglocal
3181 call assert_fails('drop xx', 'E1156:')
3182
3183 au! BufNew
3184endfunc
3185
Bram Moolenaar8ab37572021-02-03 21:56:59 +01003186func Test_autocmd_closes_window()
3187 au BufNew,BufWinLeave * e %e
3188 file yyy
3189 au BufNew,BufWinLeave * ball
Bram Moolenaaraad5f9d2021-02-06 17:30:31 +01003190 n xxx
Bram Moolenaar8ab37572021-02-03 21:56:59 +01003191
Bram Moolenaaraad5f9d2021-02-06 17:30:31 +01003192 %bwipe
Bram Moolenaar8ab37572021-02-03 21:56:59 +01003193 au! BufNew
3194 au! BufWinLeave
3195endfunc
3196
Bram Moolenaar92bb83e2021-02-03 23:04:46 +01003197func Test_autocmd_quit_psearch()
3198 sn aa bb
3199 augroup aucmd_win_test
3200 au!
3201 au BufEnter,BufLeave,BufNew,WinEnter,WinLeave,WinNew * if winnr('$') > 1 | q | endif
3202 augroup END
3203 ps /
3204
3205 augroup aucmd_win_test
3206 au!
3207 augroup END
zeertzjq7851c692022-04-21 11:14:01 +01003208 new
3209 pclose
Bram Moolenaar92bb83e2021-02-03 23:04:46 +01003210endfunc
3211
Bram Moolenaaraad5f9d2021-02-06 17:30:31 +01003212" Fuzzer found some strange combination that caused a crash.
3213func Test_autocmd_normal_mess()
Bram Moolenaardd07c022021-02-07 13:32:46 +01003214 " For unknown reason this hangs on MS-Windows
3215 CheckNotMSWindows
3216
Bram Moolenaaraad5f9d2021-02-06 17:30:31 +01003217 augroup aucmd_normal_test
3218 au BufLeave,BufWinLeave,BufHidden,BufUnload,BufDelete,BufWipeout * norm 7q/qc
3219 augroup END
Bram Moolenaar983d83f2021-02-07 12:12:43 +01003220 call assert_fails('o4', 'E1159')
Bram Moolenaaraad5f9d2021-02-06 17:30:31 +01003221 silent! H
Bram Moolenaar983d83f2021-02-07 12:12:43 +01003222 call assert_fails('e xx', 'E1159')
Bram Moolenaaraad5f9d2021-02-06 17:30:31 +01003223 normal G
3224
3225 augroup aucmd_normal_test
3226 au!
3227 augroup END
3228endfunc
3229
Bram Moolenaar8c6951f2021-02-06 18:08:45 +01003230func Test_autocmd_closing_cmdwin()
Bram Moolenaardd07c022021-02-07 13:32:46 +01003231 " For unknown reason this hangs on MS-Windows
3232 CheckNotMSWindows
3233
Bram Moolenaar8c6951f2021-02-06 18:08:45 +01003234 au BufWinLeave * nested q
3235 call assert_fails("norm 7q?\n", 'E855:')
3236
3237 au! BufWinLeave
3238 new
3239 only
3240endfunc
3241
Bram Moolenaar2c7080b2021-02-06 19:19:42 +01003242func Test_autocmd_vimgrep()
3243 augroup aucmd_vimgrep
Charlie Grovesfef44852022-04-19 16:24:12 +01003244 au QuickfixCmdPre,BufNew,BufReadCmd * sb
zeertzjq7851c692022-04-21 11:14:01 +01003245 au QuickfixCmdPre,BufNew,BufReadCmd * q9
Bram Moolenaar2c7080b2021-02-06 19:19:42 +01003246 augroup END
Bram Moolenaardd07c022021-02-07 13:32:46 +01003247 call assert_fails('lv ?a? foo', 'E926:')
Bram Moolenaar2c7080b2021-02-06 19:19:42 +01003248
3249 augroup aucmd_vimgrep
3250 au!
3251 augroup END
3252endfunc
3253
Bram Moolenaar73b8b0a2021-08-01 14:52:32 +02003254func Test_autocmd_with_block()
3255 augroup block_testing
3256 au BufReadPost *.xml {
3257 setlocal matchpairs+=<:>
3258 /<start
3259 }
Bram Moolenaar63b91732021-08-05 20:40:03 +02003260 au CursorHold * {
3261 autocmd BufReadPre * ++once echo 'one' | echo 'two'
3262 g:gotSafeState = 77
3263 }
Bram Moolenaar73b8b0a2021-08-01 14:52:32 +02003264 augroup END
3265
3266 let expected = "\n--- Autocommands ---\nblock_testing BufRead\n *.xml {^@ setlocal matchpairs+=<:>^@ /<start^@ }"
3267 call assert_equal(expected, execute('au BufReadPost *.xml'))
3268
Bram Moolenaar63b91732021-08-05 20:40:03 +02003269 doautocmd CursorHold
3270 call assert_equal(77, g:gotSafeState)
3271 unlet g:gotSafeState
3272
Bram Moolenaar73b8b0a2021-08-01 14:52:32 +02003273 augroup block_testing
3274 au!
Bram Moolenaar75ebd2a2022-06-03 17:39:46 +01003275 autocmd CursorHold * {
3276 if true
3277 # comment
3278 && true
3279
3280 && true
3281 g:done = 'yes'
3282 endif
3283 }
3284 augroup END
3285 doautocmd CursorHold
3286 call assert_equal('yes', g:done)
3287
3288 unlet g:done
3289 augroup block_testing
3290 au!
Bram Moolenaar73b8b0a2021-08-01 14:52:32 +02003291 augroup END
3292endfunc
3293
Christian Brabandtdb3b4462021-10-16 11:58:55 +01003294" Test TextChangedI and TextChanged
3295func Test_Changed_ChangedI()
3296 new
3297 call test_override("char_avail", 1)
3298 let [g:autocmd_i, g:autocmd_n] = ['','']
3299
3300 func! TextChangedAutocmdI(char)
3301 let g:autocmd_{tolower(a:char)} = a:char .. b:changedtick
3302 endfunc
3303
3304 augroup Test_TextChanged
3305 au!
3306 au TextChanged <buffer> :call TextChangedAutocmdI('N')
3307 au TextChangedI <buffer> :call TextChangedAutocmdI('I')
3308 augroup END
3309
3310 call feedkeys("ifoo\<esc>", 'tnix')
3311 " TODO: Test test does not seem to trigger TextChanged autocommand, this
3312 " requires running Vim in a terminal window.
3313 " call assert_equal('N3', g:autocmd_n)
3314 call assert_equal('I3', g:autocmd_i)
3315
3316 call feedkeys("yyp", 'tnix')
3317 " TODO: Test test does not seem to trigger TextChanged autocommand.
3318 " call assert_equal('N4', g:autocmd_n)
3319 call assert_equal('I3', g:autocmd_i)
3320
3321 " CleanUp
3322 call test_override("char_avail", 0)
3323 au! TextChanged <buffer>
3324 au! TextChangedI <buffer>
3325 augroup! Test_TextChanged
3326 delfu TextChangedAutocmdI
3327 unlet! g:autocmd_i g:autocmd_n
3328
3329 bw!
3330endfunc
Bram Moolenaar2c7080b2021-02-06 19:19:42 +01003331
Bram Moolenaar6f2465d2022-03-22 18:13:01 +00003332func Test_closing_autocmd_window()
3333 let lines =<< trim END
3334 edit Xa.txt
3335 tabnew Xb.txt
3336 autocmd BufEnter Xa.txt unhide 1
3337 doautoall BufEnter
3338 END
3339 call v9.CheckScriptFailure(lines, 'E814:')
3340 au! BufEnter
3341 only!
3342 bwipe Xa.txt
3343 bwipe Xb.txt
3344endfunc
3345
Bram Moolenaar347538f2022-03-26 16:28:06 +00003346func Test_bufwipeout_changes_window()
3347 " This should not crash, but we don't have any expectations about what
3348 " happens, changing window in BufWipeout has unpredictable results.
3349 tabedit
3350 let g:window_id = win_getid()
3351 topleft new
3352 setlocal bufhidden=wipe
3353 autocmd BufWipeout <buffer> call win_gotoid(g:window_id)
3354 tabprevious
3355 +tabclose
3356
3357 unlet g:window_id
3358 au! BufWipeout
3359 %bwipe!
3360endfunc
3361
zeertzjq021996f2022-04-10 11:44:04 +01003362func Test_v_event_readonly()
3363 autocmd CompleteChanged * let v:event.width = 0
3364 call assert_fails("normal! i\<C-X>\<C-V>", 'E46:')
3365 au! CompleteChanged
3366
3367 autocmd DirChangedPre * let v:event.directory = ''
3368 call assert_fails('cd .', 'E46:')
3369 au! DirChangedPre
3370
3371 autocmd ModeChanged * let v:event.new_mode = ''
3372 call assert_fails('normal! cc', 'E46:')
3373 au! ModeChanged
3374
3375 autocmd TextYankPost * let v:event.operator = ''
3376 call assert_fails('normal! yy', 'E46:')
3377 au! TextYankPost
3378endfunc
3379
zeertzjqc9e8fd62022-07-26 18:12:38 +01003380" Test for ModeChanged pattern
3381func Test_mode_changes()
3382 let g:index = 0
3383 let g:mode_seq = ['n', 'i', 'n', 'v', 'V', 'i', 'ix', 'i', 'ic', 'i', 'n', 'no', 'n', 'V', 'v', 's', 'n']
3384 func! TestMode()
3385 call assert_equal(g:mode_seq[g:index], get(v:event, "old_mode"))
3386 call assert_equal(g:mode_seq[g:index + 1], get(v:event, "new_mode"))
3387 call assert_equal(mode(1), get(v:event, "new_mode"))
3388 let g:index += 1
3389 endfunc
3390
3391 au ModeChanged * :call TestMode()
3392 let g:n_to_any = 0
3393 au ModeChanged n:* let g:n_to_any += 1
3394 call feedkeys("i\<esc>vVca\<CR>\<C-X>\<C-L>\<esc>ggdG", 'tnix')
3395
3396 let g:V_to_v = 0
3397 au ModeChanged V:v let g:V_to_v += 1
3398 call feedkeys("Vv\<C-G>\<esc>", 'tnix')
3399 call assert_equal(len(filter(g:mode_seq[1:], {idx, val -> val == 'n'})), g:n_to_any)
3400 call assert_equal(1, g:V_to_v)
3401 call assert_equal(len(g:mode_seq) - 1, g:index)
3402
3403 let g:n_to_i = 0
3404 au ModeChanged n:i let g:n_to_i += 1
3405 let g:n_to_niI = 0
3406 au ModeChanged i:niI let g:n_to_niI += 1
3407 let g:niI_to_i = 0
3408 au ModeChanged niI:i let g:niI_to_i += 1
3409 let g:nany_to_i = 0
3410 au ModeChanged n*:i let g:nany_to_i += 1
3411 let g:i_to_n = 0
3412 au ModeChanged i:n let g:i_to_n += 1
3413 let g:nori_to_any = 0
3414 au ModeChanged [ni]:* let g:nori_to_any += 1
3415 let g:i_to_any = 0
3416 au ModeChanged i:* let g:i_to_any += 1
3417 let g:index = 0
3418 let g:mode_seq = ['n', 'i', 'niI', 'i', 'n']
3419 call feedkeys("a\<C-O>l\<esc>", 'tnix')
3420 call assert_equal(len(g:mode_seq) - 1, g:index)
3421 call assert_equal(1, g:n_to_i)
3422 call assert_equal(1, g:n_to_niI)
3423 call assert_equal(1, g:niI_to_i)
3424 call assert_equal(2, g:nany_to_i)
3425 call assert_equal(1, g:i_to_n)
3426 call assert_equal(2, g:i_to_any)
3427 call assert_equal(3, g:nori_to_any)
3428
3429 if has('terminal')
3430 let g:mode_seq += ['c', 'n', 't', 'nt', 'c', 'nt', 'n']
3431 call feedkeys(":term\<CR>\<C-W>N:bd!\<CR>", 'tnix')
3432 call assert_equal(len(g:mode_seq) - 1, g:index)
3433 call assert_equal(1, g:n_to_i)
3434 call assert_equal(1, g:n_to_niI)
3435 call assert_equal(1, g:niI_to_i)
3436 call assert_equal(2, g:nany_to_i)
3437 call assert_equal(1, g:i_to_n)
3438 call assert_equal(2, g:i_to_any)
3439 call assert_equal(5, g:nori_to_any)
3440 endif
3441
zeertzjqd1955982022-10-05 11:24:46 +01003442 let g:n_to_c = 0
3443 au ModeChanged n:c let g:n_to_c += 1
3444 let g:c_to_n = 0
3445 au ModeChanged c:n let g:c_to_n += 1
3446 let g:mode_seq += ['c', 'n', 'c', 'n']
3447 call feedkeys("q:\<C-C>\<Esc>", 'tnix')
3448 call assert_equal(len(g:mode_seq) - 1, g:index)
3449 call assert_equal(2, g:n_to_c)
3450 call assert_equal(2, g:c_to_n)
3451 unlet g:n_to_c
3452 unlet g:c_to_n
zeertzjqc9e8fd62022-07-26 18:12:38 +01003453
Bram Moolenaar61c4b042022-10-18 15:10:11 +01003454 let g:n_to_v = 0
3455 au ModeChanged n:v let g:n_to_v += 1
3456 let g:v_to_n = 0
3457 au ModeChanged v:n let g:v_to_n += 1
3458 let g:mode_seq += ['v', 'n']
3459 call feedkeys("v\<C-C>", 'tnix')
3460 call assert_equal(len(g:mode_seq) - 1, g:index)
3461 call assert_equal(1, g:n_to_v)
3462 call assert_equal(1, g:v_to_n)
3463 unlet g:n_to_v
3464 unlet g:v_to_n
3465
zeertzjqc9e8fd62022-07-26 18:12:38 +01003466 au! ModeChanged
3467 delfunc TestMode
3468 unlet! g:mode_seq
3469 unlet! g:index
3470 unlet! g:n_to_any
3471 unlet! g:V_to_v
3472 unlet! g:n_to_i
3473 unlet! g:n_to_niI
3474 unlet! g:niI_to_i
3475 unlet! g:nany_to_i
3476 unlet! g:i_to_n
3477 unlet! g:nori_to_any
3478 unlet! g:i_to_any
3479endfunc
3480
3481func Test_recursive_ModeChanged()
3482 au! ModeChanged * norm 0u
3483 sil! norm 
3484 au! ModeChanged
3485endfunc
3486
3487func Test_ModeChanged_starts_visual()
3488 " This was triggering ModeChanged before setting VIsual, causing a crash.
3489 au! ModeChanged * norm 0u
3490 sil! norm 
3491
3492 au! ModeChanged
3493endfunc
Bram Moolenaar347538f2022-03-26 16:28:06 +00003494
Charlie Grovesfef44852022-04-19 16:24:12 +01003495func Test_noname_autocmd()
3496 augroup test_noname_autocmd_group
3497 autocmd!
3498 autocmd BufEnter * call add(s:li, ["BufEnter", expand("<afile>")])
3499 autocmd BufDelete * call add(s:li, ["BufDelete", expand("<afile>")])
3500 autocmd BufLeave * call add(s:li, ["BufLeave", expand("<afile>")])
3501 autocmd BufUnload * call add(s:li, ["BufUnload", expand("<afile>")])
3502 autocmd BufWipeout * call add(s:li, ["BufWipeout", expand("<afile>")])
3503 augroup END
3504
3505 let s:li = []
3506 edit foo
3507 call assert_equal([['BufUnload', ''], ['BufDelete', ''], ['BufWipeout', ''], ['BufEnter', 'foo']], s:li)
3508
3509 au! test_noname_autocmd_group
3510 augroup! test_noname_autocmd_group
3511endfunc
3512
Yegappan Lakshmanan1755a912022-05-19 10:31:47 +01003513" Test for the autocmd_get() function
3514func Test_autocmd_get()
3515 augroup TestAutoCmdFns
3516 au!
3517 autocmd BufAdd *.vim echo "bufadd-vim"
3518 autocmd BufAdd *.py echo "bufadd-py"
3519 autocmd BufHidden *.vim echo "bufhidden"
3520 augroup END
3521 augroup TestAutoCmdFns2
3522 autocmd BufAdd *.vim echo "bufadd-vim-2"
3523 autocmd BufRead *.a1b2c3 echo "bufadd-vim-2"
3524 augroup END
3525
3526 let l = autocmd_get()
3527 call assert_true(l->len() > 0)
3528
3529 " Test for getting all the autocmds in a group
3530 let expected = [
3531 \ #{cmd: 'echo "bufadd-vim"', group: 'TestAutoCmdFns',
3532 \ pattern: '*.vim', nested: v:false, once: v:false,
3533 \ event: 'BufAdd'},
3534 \ #{cmd: 'echo "bufadd-py"', group: 'TestAutoCmdFns',
3535 \ pattern: '*.py', nested: v:false, once: v:false,
3536 \ event: 'BufAdd'},
3537 \ #{cmd: 'echo "bufhidden"', group: 'TestAutoCmdFns',
3538 \ pattern: '*.vim', nested: v:false,
3539 \ once: v:false, event: 'BufHidden'}]
3540 call assert_equal(expected, autocmd_get(#{group: 'TestAutoCmdFns'}))
3541
3542 " Test for getting autocmds for all the patterns in a group
3543 call assert_equal(expected, autocmd_get(#{group: 'TestAutoCmdFns',
3544 \ event: '*'}))
3545
3546 " Test for getting autocmds for an event in a group
3547 let expected = [
3548 \ #{cmd: 'echo "bufadd-vim"', group: 'TestAutoCmdFns',
3549 \ pattern: '*.vim', nested: v:false, once: v:false,
3550 \ event: 'BufAdd'},
3551 \ #{cmd: 'echo "bufadd-py"', group: 'TestAutoCmdFns',
3552 \ pattern: '*.py', nested: v:false, once: v:false,
3553 \ event: 'BufAdd'}]
3554 call assert_equal(expected, autocmd_get(#{group: 'TestAutoCmdFns',
3555 \ event: 'BufAdd'}))
3556
3557 " Test for getting the autocmds for all the events in a group for particular
3558 " pattern
3559 call assert_equal([{'cmd': 'echo "bufadd-py"', 'group': 'TestAutoCmdFns',
3560 \ 'pattern': '*.py', 'nested': v:false, 'once': v:false,
3561 \ 'event': 'BufAdd'}],
3562 \ autocmd_get(#{group: 'TestAutoCmdFns', event: '*', pattern: '*.py'}))
3563
3564 " Test for getting the autocmds for an events in a group for particular
3565 " pattern
3566 let l = autocmd_get(#{group: 'TestAutoCmdFns', event: 'BufAdd',
3567 \ pattern: '*.vim'})
3568 call assert_equal([
3569 \ #{cmd: 'echo "bufadd-vim"', group: 'TestAutoCmdFns',
3570 \ pattern: '*.vim', nested: v:false, once: v:false,
3571 \ event: 'BufAdd'}], l)
3572
3573 " Test for getting the autocmds for a pattern in a group
3574 let l = autocmd_get(#{group: 'TestAutoCmdFns', pattern: '*.vim'})
3575 call assert_equal([
3576 \ #{cmd: 'echo "bufadd-vim"', group: 'TestAutoCmdFns',
3577 \ pattern: '*.vim', nested: v:false, once: v:false,
3578 \ event: 'BufAdd'},
3579 \ #{cmd: 'echo "bufhidden"', group: 'TestAutoCmdFns',
3580 \ pattern: '*.vim', nested: v:false,
3581 \ once: v:false, event: 'BufHidden'}], l)
3582
3583 " Test for getting the autocmds for a pattern in all the groups
3584 let l = autocmd_get(#{pattern: '*.a1b2c3'})
3585 call assert_equal([{'cmd': 'echo "bufadd-vim-2"', 'group': 'TestAutoCmdFns2',
3586 \ 'pattern': '*.a1b2c3', 'nested': v:false, 'once': v:false,
3587 \ 'event': 'BufRead'}], l)
3588
3589 " Test for getting autocmds for a pattern without any autocmds
3590 call assert_equal([], autocmd_get(#{group: 'TestAutoCmdFns',
3591 \ pattern: '*.abc'}))
3592 call assert_equal([], autocmd_get(#{group: 'TestAutoCmdFns',
3593 \ event: 'BufAdd', pattern: '*.abc'}))
3594 call assert_equal([], autocmd_get(#{group: 'TestAutoCmdFns',
3595 \ event: 'BufWipeout'}))
3596 call assert_fails("call autocmd_get(#{group: 'abc', event: 'BufAdd'})",
3597 \ 'E367:')
3598 let cmd = "echo autocmd_get(#{group: 'TestAutoCmdFns', event: 'abc'})"
3599 call assert_fails(cmd, 'E216:')
3600 call assert_fails("call autocmd_get(#{group: 'abc'})", 'E367:')
3601 call assert_fails("echo autocmd_get(#{event: 'abc'})", 'E216:')
3602
3603 augroup TestAutoCmdFns
3604 au!
3605 augroup END
3606 call assert_equal([], autocmd_get(#{group: 'TestAutoCmdFns'}))
3607
3608 " Test for nested and once autocmds
3609 augroup TestAutoCmdFns
3610 au!
3611 autocmd VimSuspend * ++nested echo "suspend"
3612 autocmd VimResume * ++once echo "resume"
3613 augroup END
3614
3615 let expected = [
3616 \ {'cmd': 'echo "suspend"', 'group': 'TestAutoCmdFns', 'pattern': '*',
3617 \ 'nested': v:true, 'once': v:false, 'event': 'VimSuspend'},
3618 \ {'cmd': 'echo "resume"', 'group': 'TestAutoCmdFns', 'pattern': '*',
3619 \ 'nested': v:false, 'once': v:true, 'event': 'VimResume'}]
3620 call assert_equal(expected, autocmd_get(#{group: 'TestAutoCmdFns'}))
3621
3622 " Test for buffer-local autocmd
3623 augroup TestAutoCmdFns
3624 au!
3625 autocmd TextYankPost <buffer> echo "textyankpost"
3626 augroup END
3627
3628 let expected = [
3629 \ {'cmd': 'echo "textyankpost"', 'group': 'TestAutoCmdFns',
3630 \ 'pattern': '<buffer=' .. bufnr() .. '>', 'nested': v:false,
3631 \ 'once': v:false, 'bufnr': bufnr(), 'event': 'TextYankPost'}]
3632 call assert_equal(expected, autocmd_get(#{group: 'TestAutoCmdFns'}))
3633
3634 augroup TestAutoCmdFns
3635 au!
3636 augroup END
3637 augroup! TestAutoCmdFns
3638 augroup TestAutoCmdFns2
3639 au!
3640 augroup END
3641 augroup! TestAutoCmdFns2
3642
3643 call assert_fails("echo autocmd_get(#{group: []})", 'E730:')
3644 call assert_fails("echo autocmd_get(#{event: {}})", 'E731:')
3645 call assert_fails("echo autocmd_get([])", 'E1206:')
3646endfunc
3647
3648" Test for the autocmd_add() function
3649func Test_autocmd_add()
3650 " Define a single autocmd in a group
3651 call autocmd_add([#{group: 'TestAcSet', event: 'BufAdd', pattern: '*.sh',
3652 \ cmd: 'echo "bufadd"', once: v:true, nested: v:true}])
3653 call assert_equal([#{cmd: 'echo "bufadd"', group: 'TestAcSet',
3654 \ pattern: '*.sh', nested: v:true, once: v:true,
3655 \ event: 'BufAdd'}], autocmd_get(#{group: 'TestAcSet'}))
3656
3657 " Define two autocmds in the same group
3658 call autocmd_delete([#{group: 'TestAcSet'}])
3659 call autocmd_add([#{group: 'TestAcSet', event: 'BufAdd', pattern: '*.sh',
3660 \ cmd: 'echo "bufadd"'},
3661 \ #{group: 'TestAcSet', event: 'BufEnter', pattern: '*.sh',
3662 \ cmd: 'echo "bufenter"'}])
3663 call assert_equal([
3664 \ #{cmd: 'echo "bufadd"', group: 'TestAcSet', pattern: '*.sh',
3665 \ nested: v:false, once: v:false, event: 'BufAdd'},
3666 \ #{cmd: 'echo "bufenter"', group: 'TestAcSet', pattern: '*.sh',
3667 \ nested: v:false, once: v:false, event: 'BufEnter'}],
3668 \ autocmd_get(#{group: 'TestAcSet'}))
3669
3670 " Define a buffer-local autocmd
3671 call autocmd_delete([#{group: 'TestAcSet'}])
3672 call autocmd_add([#{group: 'TestAcSet', event: 'CursorHold',
3673 \ bufnr: bufnr(), cmd: 'echo "cursorhold"'}])
3674 call assert_equal([
3675 \ #{cmd: 'echo "cursorhold"', group: 'TestAcSet',
3676 \ pattern: '<buffer=' .. bufnr() .. '>', nested: v:false,
3677 \ once: v:false, bufnr: bufnr(), event: 'CursorHold'}],
3678 \ autocmd_get(#{group: 'TestAcSet'}))
3679
3680 " Use an invalid buffer number
3681 call autocmd_delete([#{group: 'TestAcSet'}])
3682 call autocmd_add([#{group: 'TestAcSet', event: 'BufEnter',
3683 \ bufnr: -1, cmd: 'echo "bufenter"'}])
3684 let l = [#{group: 'TestAcSet', event: 'BufAdd', bufnr: 9999,
3685 \ cmd: 'echo "bufadd"'}]
3686 call assert_fails("echo autocmd_add(l)", 'E680:')
Yegappan Lakshmanan00e977c2022-06-01 12:31:53 +01003687 let l = [#{group: 'TestAcSet', event: 'BufAdd', bufnr: 9999,
3688 \ pattern: '*.py', cmd: 'echo "bufadd"'}]
3689 call assert_fails("echo autocmd_add(l)", 'E680:')
3690 let l = [#{group: 'TestAcSet', event: 'BufAdd', bufnr: 9999,
3691 \ pattern: ['*.py', '*.c'], cmd: 'echo "bufadd"'}]
3692 call assert_fails("echo autocmd_add(l)", 'E680:')
Yegappan Lakshmanan1755a912022-05-19 10:31:47 +01003693 let l = [#{group: 'TestAcSet', event: 'BufRead', bufnr: [],
3694 \ cmd: 'echo "bufread"'}]
3695 call assert_fails("echo autocmd_add(l)", 'E745:')
3696 call assert_equal([], autocmd_get(#{group: 'TestAcSet'}))
3697
3698 " Add two commands to the same group, event and pattern
3699 call autocmd_delete([#{group: 'TestAcSet'}])
3700 call autocmd_add([#{group: 'TestAcSet', event: 'BufUnload',
3701 \ pattern: 'abc', cmd: 'echo "cmd1"'}])
3702 call autocmd_add([#{group: 'TestAcSet', event: 'BufUnload',
3703 \ pattern: 'abc', cmd: 'echo "cmd2"'}])
3704 call assert_equal([
3705 \ #{cmd: 'echo "cmd1"', group: 'TestAcSet', pattern: 'abc',
3706 \ nested: v:false, once: v:false, event: 'BufUnload'},
3707 \ #{cmd: 'echo "cmd2"', group: 'TestAcSet', pattern: 'abc',
3708 \ nested: v:false, once: v:false, event: 'BufUnload'}],
3709 \ autocmd_get(#{group: 'TestAcSet'}))
3710
3711 " When adding a new autocmd, if the autocmd 'group' is not specified, then
3712 " the current autocmd group should be used.
3713 call autocmd_delete([#{group: 'TestAcSet'}])
3714 augroup TestAcSet
3715 call autocmd_add([#{event: 'BufHidden', pattern: 'abc', cmd: 'echo "abc"'}])
3716 augroup END
3717 call assert_equal([
3718 \ #{cmd: 'echo "abc"', group: 'TestAcSet', pattern: 'abc',
3719 \ nested: v:false, once: v:false, event: 'BufHidden'}],
3720 \ autocmd_get(#{group: 'TestAcSet'}))
3721
Yegappan Lakshmanan971f6822022-05-24 11:40:11 +01003722 " Test for replacing a cmd for an event in a group
3723 call autocmd_delete([#{group: 'TestAcSet'}])
3724 call autocmd_add([#{replace: v:true, group: 'TestAcSet', event: 'BufEnter',
3725 \ pattern: '*.py', cmd: 'echo "bufenter"'}])
3726 call autocmd_add([#{replace: v:true, group: 'TestAcSet', event: 'BufEnter',
3727 \ pattern: '*.py', cmd: 'echo "bufenter"'}])
3728 call assert_equal([
3729 \ #{cmd: 'echo "bufenter"', group: 'TestAcSet', pattern: '*.py',
3730 \ nested: v:false, once: v:false, event: 'BufEnter'}],
3731 \ autocmd_get(#{group: 'TestAcSet'}))
3732
3733 " Test for adding a command for an unsupported autocmd event
Yegappan Lakshmanan1755a912022-05-19 10:31:47 +01003734 let l = [#{group: 'TestAcSet', event: 'abc', pattern: '*.sh',
3735 \ cmd: 'echo "bufadd"'}]
3736 call assert_fails('call autocmd_add(l)', 'E216:')
3737
Yegappan Lakshmanane0ff3a72022-05-27 18:05:33 +01003738 " Test for using a list of events and patterns
3739 call autocmd_delete([#{group: 'TestAcSet'}])
3740 let l = [#{group: 'TestAcSet', event: ['BufEnter', 'BufLeave'],
3741 \ pattern: ['*.py', '*.sh'], cmd: 'echo "bufcmds"'}]
3742 call autocmd_add(l)
3743 call assert_equal([
3744 \ #{cmd: 'echo "bufcmds"', group: 'TestAcSet', pattern: '*.py',
3745 \ nested: v:false, once: v:false, event: 'BufEnter'},
3746 \ #{cmd: 'echo "bufcmds"', group: 'TestAcSet', pattern: '*.sh',
3747 \ nested: v:false, once: v:false, event: 'BufEnter'},
3748 \ #{cmd: 'echo "bufcmds"', group: 'TestAcSet', pattern: '*.py',
3749 \ nested: v:false, once: v:false, event: 'BufLeave'},
3750 \ #{cmd: 'echo "bufcmds"', group: 'TestAcSet', pattern: '*.sh',
3751 \ nested: v:false, once: v:false, event: 'BufLeave'}],
3752 \ autocmd_get(#{group: 'TestAcSet'}))
3753
3754 " Test for invalid values for 'event' item
3755 call autocmd_delete([#{group: 'TestAcSet'}])
3756 let l = [#{group: 'TestAcSet', event: test_null_string(),
3757 \ pattern: "*.py", cmd: 'echo "bufcmds"'}]
3758 call assert_fails('call autocmd_add(l)', 'E928:')
3759 let l = [#{group: 'TestAcSet', event: test_null_list(),
3760 \ pattern: "*.py", cmd: 'echo "bufcmds"'}]
3761 call assert_fails('call autocmd_add(l)', 'E714:')
3762 let l = [#{group: 'TestAcSet', event: {},
3763 \ pattern: "*.py", cmd: 'echo "bufcmds"'}]
3764 call assert_fails('call autocmd_add(l)', 'E777:')
3765 let l = [#{group: 'TestAcSet', event: [{}],
3766 \ pattern: "*.py", cmd: 'echo "bufcmds"'}]
3767 call assert_fails('call autocmd_add(l)', 'E928:')
3768 let l = [#{group: 'TestAcSet', event: [test_null_string()],
3769 \ pattern: "*.py", cmd: 'echo "bufcmds"'}]
3770 call assert_fails('call autocmd_add(l)', 'E928:')
3771 let l = [#{group: 'TestAcSet', event: 'BufEnter,BufLeave',
3772 \ pattern: '*.py', cmd: 'echo "bufcmds"'}]
3773 call assert_fails('call autocmd_add(l)', 'E216:')
3774 let l = [#{group: 'TestAcSet', event: [],
3775 \ pattern: "*.py", cmd: 'echo "bufcmds"'}]
3776 call autocmd_add(l)
3777 let l = [#{group: 'TestAcSet', event: [""],
3778 \ pattern: "*.py", cmd: 'echo "bufcmds"'}]
3779 call assert_fails('call autocmd_add(l)', 'E216:')
3780 let l = [#{group: 'TestAcSet', event: "",
3781 \ pattern: "*.py", cmd: 'echo "bufcmds"'}]
3782 call autocmd_add(l)
3783 call assert_equal([], autocmd_get(#{group: 'TestAcSet'}))
3784
3785 " Test for invalid values for 'pattern' item
3786 let l = [#{group: 'TestAcSet', event: "BufEnter",
3787 \ pattern: test_null_string(), cmd: 'echo "bufcmds"'}]
Yegappan Lakshmanan00e977c2022-06-01 12:31:53 +01003788 call assert_fails('call autocmd_add(l)', 'E928:')
Yegappan Lakshmanane0ff3a72022-05-27 18:05:33 +01003789 let l = [#{group: 'TestAcSet', event: "BufEnter",
3790 \ pattern: test_null_list(), cmd: 'echo "bufcmds"'}]
3791 call assert_fails('call autocmd_add(l)', 'E714:')
3792 let l = [#{group: 'TestAcSet', event: "BufEnter",
3793 \ pattern: {}, cmd: 'echo "bufcmds"'}]
3794 call assert_fails('call autocmd_add(l)', 'E777:')
3795 let l = [#{group: 'TestAcSet', event: "BufEnter",
3796 \ pattern: [{}], cmd: 'echo "bufcmds"'}]
3797 call assert_fails('call autocmd_add(l)', 'E928:')
3798 let l = [#{group: 'TestAcSet', event: "BufEnter",
3799 \ pattern: [test_null_string()], cmd: 'echo "bufcmds"'}]
3800 call assert_fails('call autocmd_add(l)', 'E928:')
3801 let l = [#{group: 'TestAcSet', event: "BufEnter",
3802 \ pattern: [], cmd: 'echo "bufcmds"'}]
3803 call autocmd_add(l)
3804 let l = [#{group: 'TestAcSet', event: "BufEnter",
3805 \ pattern: [""], cmd: 'echo "bufcmds"'}]
3806 call autocmd_add(l)
3807 let l = [#{group: 'TestAcSet', event: "BufEnter",
3808 \ pattern: "", cmd: 'echo "bufcmds"'}]
3809 call autocmd_add(l)
3810 call assert_equal([], autocmd_get(#{group: 'TestAcSet'}))
3811
3812 let l = [#{group: 'TestAcSet', event: 'BufEnter,abc,BufLeave',
3813 \ pattern: '*.py', cmd: 'echo "bufcmds"'}]
3814 call assert_fails('call autocmd_add(l)', 'E216:')
3815
Yegappan Lakshmanan1755a912022-05-19 10:31:47 +01003816 call assert_fails("call autocmd_add({})", 'E1211:')
3817 call assert_equal(v:false, autocmd_add(test_null_list()))
3818 call assert_true(autocmd_add([[]]))
3819 call assert_true(autocmd_add([test_null_dict()]))
3820
3821 augroup TestAcSet
3822 au!
3823 augroup END
3824
3825 call autocmd_add([#{group: 'TestAcSet'}])
3826 call autocmd_add([#{group: 'TestAcSet', event: 'BufAdd'}])
3827 call autocmd_add([#{group: 'TestAcSet', pat: '*.sh'}])
3828 call autocmd_add([#{group: 'TestAcSet', cmd: 'echo "a"'}])
3829 call autocmd_add([#{group: 'TestAcSet', event: 'BufAdd', pat: '*.sh'}])
3830 call autocmd_add([#{group: 'TestAcSet', event: 'BufAdd', cmd: 'echo "a"'}])
3831 call autocmd_add([#{group: 'TestAcSet', pat: '*.sh', cmd: 'echo "a"'}])
3832 call assert_equal([], autocmd_get(#{group: 'TestAcSet'}))
3833
3834 augroup! TestAcSet
3835endfunc
3836
3837" Test for deleting autocmd events and groups
3838func Test_autocmd_delete()
3839 " Delete an event in an autocmd group
3840 augroup TestAcSet
3841 au!
3842 au BufAdd *.sh echo "bufadd"
3843 au BufEnter *.sh echo "bufenter"
3844 augroup END
3845 call autocmd_delete([#{group: 'TestAcSet', event: 'BufAdd'}])
3846 call assert_equal([#{cmd: 'echo "bufenter"', group: 'TestAcSet',
3847 \ pattern: '*.sh', nested: v:false, once: v:false,
3848 \ event: 'BufEnter'}], autocmd_get(#{group: 'TestAcSet'}))
3849
3850 " Delete all the events in an autocmd group
3851 augroup TestAcSet
3852 au BufAdd *.sh echo "bufadd"
3853 augroup END
3854 call autocmd_delete([#{group: 'TestAcSet', event: '*'}])
3855 call assert_equal([], autocmd_get(#{group: 'TestAcSet'}))
3856
3857 " Delete a non-existing autocmd group
3858 call assert_fails("call autocmd_delete([#{group: 'abc'}])", 'E367:')
3859 " Delete a non-existing autocmd event
3860 let l = [#{group: 'TestAcSet', event: 'abc'}]
3861 call assert_fails("call autocmd_delete(l)", 'E216:')
3862 " Delete a non-existing autocmd pattern
3863 let l = [#{group: 'TestAcSet', event: 'BufAdd', pat: 'abc'}]
3864 call assert_true(autocmd_delete(l))
Yegappan Lakshmanan00e977c2022-06-01 12:31:53 +01003865 " Delete an autocmd for a non-existing buffer
3866 let l = [#{event: '*', bufnr: 9999, cmd: 'echo "x"'}]
3867 call assert_fails('call autocmd_delete(l)', 'E680:')
Yegappan Lakshmanan1755a912022-05-19 10:31:47 +01003868
3869 " Delete an autocmd group
3870 augroup TestAcSet
3871 au!
3872 au BufAdd *.sh echo "bufadd"
3873 au BufEnter *.sh echo "bufenter"
3874 augroup END
3875 call autocmd_delete([#{group: 'TestAcSet'}])
3876 call assert_fails("call autocmd_get(#{group: 'TestAcSet'})", 'E367:')
3877
3878 call assert_true(autocmd_delete([[]]))
3879 call assert_true(autocmd_delete([test_null_dict()]))
3880endfunc
3881
Bram Moolenaar8f3c3c62022-10-18 17:05:54 +01003882func Test_autocmd_split_dummy()
3883 " Autocommand trying to split a window containing a dummy buffer.
3884 auto BufReadPre * exe "sbuf " .. expand("<abuf>")
3885 " Avoid the "W11" prompt
3886 au FileChangedShell * let v:fcs_choice = 'reload'
3887 func Xautocmd_changelist()
3888 cal writefile(['Xtestfile2:4:4'], 'Xerr')
3889 edit Xerr
3890 lex 'Xtestfile2:4:4'
3891 endfunc
3892 call Xautocmd_changelist()
Bram Moolenaar53c5c9f2022-10-18 17:25:03 +01003893 " Should get E86, but it doesn't always happen (timing?)
3894 silent! call Xautocmd_changelist()
Bram Moolenaar8f3c3c62022-10-18 17:05:54 +01003895
3896 au! BufReadPre
3897 au! FileChangedShell
3898 delfunc Xautocmd_changelist
3899 bwipe! Xerr
3900 call delete('Xerr')
3901endfunc
3902
Bram Moolenaarbc2b71d2020-02-17 21:33:30 +01003903" vim: shiftwidth=2 sts=2 expandtab