blob: 419005aa6eceabfe56cf7cd0db40277821a446a0 [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()
zeertzjq657b31f2023-04-15 21:28:02 +010057 " depends on timing
58 let g:test_is_flaky = 1
59
Bram Moolenaarf18c4db2016-09-08 22:10:06 +020060 " Need to move the cursor.
61 call feedkeys("ggG", "xt")
62
Bram Moolenaarc67e8922016-05-24 16:07:40 +020063 let g:triggered = 0
64 au CursorHoldI * let g:triggered += 1
65 set updatetime=20
Bram Moolenaar92bb83e2021-02-03 23:04:46 +010066 call timer_start(200, 'ExitInsertMode')
Bram Moolenaarc67e8922016-05-24 16:07:40 +020067 call feedkeys('a', 'x!')
Bram Moolenaar3b014be2022-11-13 17:53:46 +000068 sleep 30m
Bram Moolenaarc67e8922016-05-24 16:07:40 +020069 call assert_equal(1, g:triggered)
Bram Moolenaar26d98212019-01-27 22:32:55 +010070 unlet g:triggered
71 au! CursorHoldI
72 set updatetime&
73 endfunc
74
75 func Test_cursorhold_insert_with_timer_interrupt()
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +020076 CheckFeature job
Bram Moolenaar26d98212019-01-27 22:32:55 +010077 " Need to move the cursor.
78 call feedkeys("ggG", "xt")
79
80 " Confirm the timer invoked in exit_cb of the job doesn't disturb
81 " CursorHoldI event.
82 let g:triggered = 0
83 au CursorHoldI * let g:triggered += 1
Bram Moolenaar62cd26a2020-10-11 20:08:44 +020084 set updatetime=100
Bram Moolenaar26d98212019-01-27 22:32:55 +010085 call job_start(has('win32') ? 'cmd /c echo:' : 'echo',
Bram Moolenaar62cd26a2020-10-11 20:08:44 +020086 \ {'exit_cb': {-> timer_start(200, 'ExitInsertMode')}})
Bram Moolenaar26d98212019-01-27 22:32:55 +010087 call feedkeys('a', 'x!')
88 call assert_equal(1, g:triggered)
89 unlet g:triggered
Bram Moolenaare99e8442016-07-26 20:43:40 +020090 au! CursorHoldI
Bram Moolenaaraeac9002016-09-06 22:15:08 +020091 set updatetime&
Bram Moolenaarc67e8922016-05-24 16:07:40 +020092 endfunc
93
94 func Test_cursorhold_insert_ctrl_x()
95 let g:triggered = 0
96 au CursorHoldI * let g:triggered += 1
97 set updatetime=20
98 call timer_start(100, 'ExitInsertMode')
99 " CursorHoldI does not trigger after CTRL-X
100 call feedkeys("a\<C-X>", 'x!')
101 call assert_equal(0, g:triggered)
Bram Moolenaar26d98212019-01-27 22:32:55 +0100102 unlet g:triggered
Bram Moolenaare99e8442016-07-26 20:43:40 +0200103 au! CursorHoldI
Bram Moolenaaraeac9002016-09-06 22:15:08 +0200104 set updatetime&
Bram Moolenaarc67e8922016-05-24 16:07:40 +0200105 endfunc
Bram Moolenaar97b00752019-05-12 13:07:14 +0200106
Bram Moolenaar5a9357d2021-10-03 16:22:05 +0100107 func Test_cursorhold_insert_ctrl_g_U()
108 au CursorHoldI * :
109 set updatetime=20
110 new
111 call timer_start(100, { -> feedkeys("\<Left>foo\<Esc>", 't') })
112 call feedkeys("i()\<C-g>U", 'tx!')
113 sleep 200m
114 call assert_equal('(foo)', getline(1))
115 undo
116 call assert_equal('', getline(1))
117
118 bwipe!
119 au! CursorHoldI
120 set updatetime&
121 endfunc
122
Bram Moolenaar97b00752019-05-12 13:07:14 +0200123 func Test_OptionSet_modeline()
124 call test_override('starting', 1)
125 au! OptionSet
126 augroup set_tabstop
127 au OptionSet tabstop call timer_start(1, {-> execute("echo 'Handler called'", "")})
128 augroup END
Bram Moolenaare1f3ab72022-09-04 21:29:08 +0100129 call writefile(['vim: set ts=7 sw=5 :', 'something'], 'XoptionsetModeline', 'D')
Bram Moolenaar97b00752019-05-12 13:07:14 +0200130 set modeline
131 let v:errmsg = ''
132 call assert_fails('split XoptionsetModeline', 'E12:')
133 call assert_equal(7, &ts)
134 call assert_equal('', v:errmsg)
135
136 augroup set_tabstop
137 au!
138 augroup END
139 bwipe!
140 set ts&
Bram Moolenaar97b00752019-05-12 13:07:14 +0200141 call test_override('starting', 0)
142 endfunc
143
144endif "has('timers')
Bram Moolenaar40b1b542016-04-20 20:18:23 +0200145
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200146func Test_bufunload()
Bram Moolenaarc67e8922016-05-24 16:07:40 +0200147 augroup test_bufunload_group
148 autocmd!
149 autocmd BufUnload * call add(s:li, "bufunload")
150 autocmd BufDelete * call add(s:li, "bufdelete")
151 autocmd BufWipeout * call add(s:li, "bufwipeout")
152 augroup END
Bram Moolenaar40b1b542016-04-20 20:18:23 +0200153
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100154 let s:li = []
Bram Moolenaarc67e8922016-05-24 16:07:40 +0200155 new
156 setlocal bufhidden=
157 bunload
158 call assert_equal(["bufunload", "bufdelete"], s:li)
Bram Moolenaar40b1b542016-04-20 20:18:23 +0200159
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100160 let s:li = []
Bram Moolenaarc67e8922016-05-24 16:07:40 +0200161 new
162 setlocal bufhidden=delete
163 bunload
164 call assert_equal(["bufunload", "bufdelete"], s:li)
165
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100166 let s:li = []
Bram Moolenaarc67e8922016-05-24 16:07:40 +0200167 new
168 setlocal bufhidden=unload
169 bwipeout
170 call assert_equal(["bufunload", "bufdelete", "bufwipeout"], s:li)
171
Bram Moolenaare99e8442016-07-26 20:43:40 +0200172 au! test_bufunload_group
Bram Moolenaarc67e8922016-05-24 16:07:40 +0200173 augroup! test_bufunload_group
Bram Moolenaar40b1b542016-04-20 20:18:23 +0200174endfunc
Bram Moolenaar30445cb2016-07-09 15:21:02 +0200175
176" SEGV occurs in older versions. (At least 7.4.2005 or older)
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200177func Test_autocmd_bufunload_with_tabnext()
Bram Moolenaar30445cb2016-07-09 15:21:02 +0200178 tabedit
179 tabfirst
180
181 augroup test_autocmd_bufunload_with_tabnext_group
182 autocmd!
183 autocmd BufUnload <buffer> tabnext
184 augroup END
185
186 quit
187 call assert_equal(2, tabpagenr('$'))
188
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200189 autocmd! test_autocmd_bufunload_with_tabnext_group
Bram Moolenaar30445cb2016-07-09 15:21:02 +0200190 augroup! test_autocmd_bufunload_with_tabnext_group
191 tablast
192 quit
193endfunc
Bram Moolenaarc917da42016-07-19 22:31:36 +0200194
Bram Moolenaar5ed58c72021-01-28 14:24:55 +0100195func Test_argdelete_in_next()
196 au BufNew,BufEnter,BufLeave,BufWinEnter * argdel
197 call assert_fails('next a b', 'E1156:')
198 au! BufNew,BufEnter,BufLeave,BufWinEnter *
199endfunc
200
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200201func Test_autocmd_bufwinleave_with_tabfirst()
Bram Moolenaarf9e687e2016-09-04 21:33:09 +0200202 tabedit
203 augroup sample
204 autocmd!
205 autocmd BufWinLeave <buffer> tabfirst
206 augroup END
207 call setline(1, ['a', 'b', 'c'])
208 edit! a.txt
Bram Moolenaarf18c4db2016-09-08 22:10:06 +0200209 tabclose
Bram Moolenaarf9e687e2016-09-04 21:33:09 +0200210endfunc
211
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200212" SEGV occurs in older versions. (At least 7.4.2321 or older)
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200213func Test_autocmd_bufunload_avoiding_SEGV_01()
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200214 split aa.txt
215 let lastbuf = bufnr('$')
216
217 augroup test_autocmd_bufunload
218 autocmd!
219 exe 'autocmd BufUnload <buffer> ' . (lastbuf + 1) . 'bwipeout!'
220 augroup END
221
Bram Moolenaar28ee8922020-10-28 20:20:00 +0100222 call assert_fails('edit bb.txt', 'E937:')
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200223
224 autocmd! test_autocmd_bufunload
225 augroup! test_autocmd_bufunload
226 bwipe! aa.txt
227 bwipe! bb.txt
228endfunc
229
230" SEGV occurs in older versions. (At least 7.4.2321 or older)
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200231func Test_autocmd_bufunload_avoiding_SEGV_02()
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200232 setlocal buftype=nowrite
233 let lastbuf = bufnr('$')
234
235 augroup test_autocmd_bufunload
236 autocmd!
237 exe 'autocmd BufUnload <buffer> ' . (lastbuf + 1) . 'bwipeout!'
238 augroup END
239
240 normal! i1
241 call assert_fails('edit a.txt', 'E517:')
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200242
243 autocmd! test_autocmd_bufunload
244 augroup! test_autocmd_bufunload
245 bwipe! a.txt
246endfunc
247
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100248func Test_autocmd_dummy_wipeout()
249 " prepare files
Bram Moolenaare1f3ab72022-09-04 21:29:08 +0100250 call writefile([''], 'Xdummywipetest1.txt', 'D')
251 call writefile([''], 'Xdummywipetest2.txt', 'D')
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100252 augroup test_bufunload_group
253 autocmd!
254 autocmd BufUnload * call add(s:li, "bufunload")
255 autocmd BufDelete * call add(s:li, "bufdelete")
256 autocmd BufWipeout * call add(s:li, "bufwipeout")
257 augroup END
258
259 let s:li = []
260 split Xdummywipetest1.txt
261 silent! vimgrep /notmatched/ Xdummywipetest*
262 call assert_equal(["bufunload", "bufwipeout"], s:li)
263
264 bwipeout
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100265 au! test_bufunload_group
266 augroup! test_bufunload_group
267endfunc
268
Bram Moolenaarc917da42016-07-19 22:31:36 +0200269func Test_win_tab_autocmd()
270 let g:record = []
271
272 augroup testing
273 au WinNew * call add(g:record, 'WinNew')
naohiro ono23beefe2021-11-13 12:38:49 +0000274 au WinClosed * call add(g:record, 'WinClosed')
Bram Moolenaar94722c52023-01-28 19:19:03 +0000275 au WinEnter * call add(g:record, 'WinEnter')
276 au WinLeave * call add(g:record, 'WinLeave')
Bram Moolenaarc917da42016-07-19 22:31:36 +0200277 au TabNew * call add(g:record, 'TabNew')
Bram Moolenaar12c11d52016-07-19 23:13:03 +0200278 au TabClosed * call add(g:record, 'TabClosed')
Bram Moolenaarc917da42016-07-19 22:31:36 +0200279 au TabEnter * call add(g:record, 'TabEnter')
280 au TabLeave * call add(g:record, 'TabLeave')
281 augroup END
282
283 split
284 tabnew
285 close
286 close
287
288 call assert_equal([
289 \ 'WinLeave', 'WinNew', 'WinEnter',
290 \ 'WinLeave', 'TabLeave', 'WinNew', 'WinEnter', 'TabNew', 'TabEnter',
naohiro ono23beefe2021-11-13 12:38:49 +0000291 \ 'WinLeave', 'TabLeave', 'WinClosed', 'TabClosed', 'WinEnter', 'TabEnter',
292 \ 'WinLeave', 'WinClosed', 'WinEnter'
Bram Moolenaarc917da42016-07-19 22:31:36 +0200293 \ ], g:record)
294
Bram Moolenaar12c11d52016-07-19 23:13:03 +0200295 let g:record = []
296 tabnew somefile
297 tabnext
298 bwipe somefile
299
300 call assert_equal([
301 \ 'WinLeave', 'TabLeave', 'WinNew', 'WinEnter', 'TabNew', 'TabEnter',
302 \ 'WinLeave', 'TabLeave', 'WinEnter', 'TabEnter',
naohiro ono23beefe2021-11-13 12:38:49 +0000303 \ 'WinClosed', 'TabClosed'
Bram Moolenaar12c11d52016-07-19 23:13:03 +0200304 \ ], g:record)
305
Bram Moolenaarc917da42016-07-19 22:31:36 +0200306 augroup testing
307 au!
308 augroup END
309 unlet g:record
310endfunc
Bram Moolenaare99e8442016-07-26 20:43:40 +0200311
Bram Moolenaar35fc61c2022-11-22 12:40:50 +0000312func Test_WinResized()
313 CheckRunVimInTerminal
314
315 let lines =<< trim END
316 set scrolloff=0
317 call setline(1, ['111', '222'])
318 vnew
319 call setline(1, ['aaa', 'bbb'])
320 new
321 call setline(1, ['foo', 'bar'])
322
323 let g:resized = 0
324 au WinResized * let g:resized += 1
325
326 func WriteResizedEvent()
327 call writefile([json_encode(v:event)], 'XresizeEvent')
328 endfunc
329 au WinResized * call WriteResizedEvent()
330 END
331 call writefile(lines, 'Xtest_winresized', 'D')
332 let buf = RunVimInTerminal('-S Xtest_winresized', {'rows': 10})
333
334 " redraw now to avoid a redraw after the :echo command
335 call term_sendkeys(buf, ":redraw!\<CR>")
336 call TermWait(buf)
337
338 call term_sendkeys(buf, ":echo g:resized\<CR>")
339 call WaitForAssert({-> assert_match('^0$', term_getline(buf, 10))}, 1000)
340
341 " increase window height, two windows will be reported
342 call term_sendkeys(buf, "\<C-W>+")
343 call TermWait(buf)
344 call term_sendkeys(buf, ":echo g:resized\<CR>")
345 call WaitForAssert({-> assert_match('^1$', term_getline(buf, 10))}, 1000)
346
347 let event = readfile('XresizeEvent')[0]->json_decode()
348 call assert_equal({
349 \ 'windows': [1002, 1001],
350 \ }, event)
351
352 " increase window width, three windows will be reported
353 call term_sendkeys(buf, "\<C-W>>")
354 call TermWait(buf)
355 call term_sendkeys(buf, ":echo g:resized\<CR>")
356 call WaitForAssert({-> assert_match('^2$', term_getline(buf, 10))}, 1000)
357
358 let event = readfile('XresizeEvent')[0]->json_decode()
359 call assert_equal({
360 \ 'windows': [1002, 1001, 1000],
361 \ }, event)
362
363 call delete('XresizeEvent')
364 call StopVimInTerminal(buf)
365endfunc
366
LemonBoy09371822022-04-08 15:18:45 +0100367func Test_WinScrolled()
368 CheckRunVimInTerminal
369
370 let lines =<< trim END
zeertzjqd58862d2022-04-12 11:32:48 +0100371 set nowrap scrolloff=0
372 for ii in range(1, 18)
373 call setline(ii, repeat(nr2char(96 + ii), ii * 2))
374 endfor
375 let win_id = win_getid()
376 let g:matched = v:false
Bram Moolenaar35fc61c2022-11-22 12:40:50 +0000377 func WriteScrollEvent()
378 call writefile([json_encode(v:event)], 'XscrollEvent')
379 endfunc
zeertzjqd58862d2022-04-12 11:32:48 +0100380 execute 'au WinScrolled' win_id 'let g:matched = v:true'
381 let g:scrolled = 0
382 au WinScrolled * let g:scrolled += 1
383 au WinScrolled * let g:amatch = str2nr(expand('<amatch>'))
384 au WinScrolled * let g:afile = str2nr(expand('<afile>'))
Bram Moolenaar35fc61c2022-11-22 12:40:50 +0000385 au WinScrolled * call WriteScrollEvent()
LemonBoy09371822022-04-08 15:18:45 +0100386 END
Bram Moolenaare1f3ab72022-09-04 21:29:08 +0100387 call writefile(lines, 'Xtest_winscrolled', 'D')
LemonBoy09371822022-04-08 15:18:45 +0100388 let buf = RunVimInTerminal('-S Xtest_winscrolled', {'rows': 6})
389
390 call term_sendkeys(buf, ":echo g:scrolled\<CR>")
391 call WaitForAssert({-> assert_match('^0 ', term_getline(buf, 6))}, 1000)
392
393 " Scroll left/right in Normal mode.
394 call term_sendkeys(buf, "zlzh:echo g:scrolled\<CR>")
395 call WaitForAssert({-> assert_match('^2 ', term_getline(buf, 6))}, 1000)
396
Bram Moolenaar35fc61c2022-11-22 12:40:50 +0000397 let event = readfile('XscrollEvent')[0]->json_decode()
398 call assert_equal({
zeertzjq3fc84dc2022-12-07 09:17:59 +0000399 \ 'all': {'leftcol': 1, 'topline': 0, 'topfill': 0, 'width': 0, 'height': 0, 'skipcol': 0},
400 \ '1000': {'leftcol': -1, 'topline': 0, 'topfill': 0, 'width': 0, 'height': 0, 'skipcol': 0}
Bram Moolenaar35fc61c2022-11-22 12:40:50 +0000401 \ }, event)
402
LemonBoy09371822022-04-08 15:18:45 +0100403 " Scroll up/down in Normal mode.
404 call term_sendkeys(buf, "\<c-e>\<c-y>:echo g:scrolled\<CR>")
405 call WaitForAssert({-> assert_match('^4 ', term_getline(buf, 6))}, 1000)
406
Bram Moolenaar35fc61c2022-11-22 12:40:50 +0000407 let event = readfile('XscrollEvent')[0]->json_decode()
408 call assert_equal({
zeertzjq3fc84dc2022-12-07 09:17:59 +0000409 \ 'all': {'leftcol': 0, 'topline': 1, 'topfill': 0, 'width': 0, 'height': 0, 'skipcol': 0},
410 \ '1000': {'leftcol': 0, 'topline': -1, 'topfill': 0, 'width': 0, 'height': 0, 'skipcol': 0}
Bram Moolenaar35fc61c2022-11-22 12:40:50 +0000411 \ }, event)
412
LemonBoy09371822022-04-08 15:18:45 +0100413 " Scroll up/down in Insert mode.
414 call term_sendkeys(buf, "Mi\<c-x>\<c-e>\<Esc>i\<c-x>\<c-y>\<Esc>")
415 call term_sendkeys(buf, ":echo g:scrolled\<CR>")
416 call WaitForAssert({-> assert_match('^6 ', term_getline(buf, 6))}, 1000)
417
Bram Moolenaar35fc61c2022-11-22 12:40:50 +0000418 let event = readfile('XscrollEvent')[0]->json_decode()
419 call assert_equal({
zeertzjq3fc84dc2022-12-07 09:17:59 +0000420 \ 'all': {'leftcol': 0, 'topline': 1, 'topfill': 0, 'width': 0, 'height': 0, 'skipcol': 0},
421 \ '1000': {'leftcol': 0, 'topline': -1, 'topfill': 0, 'width': 0, 'height': 0, 'skipcol': 0}
Bram Moolenaar35fc61c2022-11-22 12:40:50 +0000422 \ }, event)
423
LemonBoy09371822022-04-08 15:18:45 +0100424 " Scroll the window horizontally to focus the last letter of the third line
425 " containing only six characters. Moving to the previous and shorter lines
426 " should trigger another autocommand as Vim has to make them visible.
427 call term_sendkeys(buf, "5zl2k")
428 call term_sendkeys(buf, ":echo g:scrolled\<CR>")
429 call WaitForAssert({-> assert_match('^8 ', term_getline(buf, 6))}, 1000)
430
Bram Moolenaar35fc61c2022-11-22 12:40:50 +0000431 let event = readfile('XscrollEvent')[0]->json_decode()
432 call assert_equal({
zeertzjq3fc84dc2022-12-07 09:17:59 +0000433 \ 'all': {'leftcol': 5, 'topline': 0, 'topfill': 0, 'width': 0, 'height': 0, 'skipcol': 0},
434 \ '1000': {'leftcol': -5, 'topline': 0, 'topfill': 0, 'width': 0, 'height': 0, 'skipcol': 0}
Bram Moolenaar35fc61c2022-11-22 12:40:50 +0000435 \ }, event)
436
LemonBoy09371822022-04-08 15:18:45 +0100437 " Ensure the command was triggered for the specified window ID.
438 call term_sendkeys(buf, ":echo g:matched\<CR>")
439 call WaitForAssert({-> assert_match('^v:true ', term_getline(buf, 6))}, 1000)
440
441 " Ensure the expansion of <amatch> and <afile> matches the window ID.
442 call term_sendkeys(buf, ":echo g:amatch == win_id && g:afile == win_id\<CR>")
443 call WaitForAssert({-> assert_match('^v:true ', term_getline(buf, 6))}, 1000)
444
Bram Moolenaar35fc61c2022-11-22 12:40:50 +0000445 call delete('XscrollEvent')
LemonBoy09371822022-04-08 15:18:45 +0100446 call StopVimInTerminal(buf)
LemonBoy09371822022-04-08 15:18:45 +0100447endfunc
448
LemonBoy66e13ae2022-04-21 22:52:11 +0100449func Test_WinScrolled_mouse()
450 CheckRunVimInTerminal
451
452 let lines =<< trim END
453 set nowrap scrolloff=0
454 set mouse=a term=xterm ttymouse=sgr mousetime=200 clipboard=
455 call setline(1, ['foo']->repeat(32))
456 split
457 let g:scrolled = 0
458 au WinScrolled * let g:scrolled += 1
459 END
Bram Moolenaare1f3ab72022-09-04 21:29:08 +0100460 call writefile(lines, 'Xtest_winscrolled_mouse', 'D')
LemonBoy66e13ae2022-04-21 22:52:11 +0100461 let buf = RunVimInTerminal('-S Xtest_winscrolled_mouse', {'rows': 10})
462
463 " With the upper split focused, send a scroll-down event to the unfocused one.
464 call test_setmouse(7, 1)
465 call term_sendkeys(buf, "\<ScrollWheelDown>")
466 call TermWait(buf)
467 call term_sendkeys(buf, ":echo g:scrolled\<CR>")
468 call WaitForAssert({-> assert_match('^1', term_getline(buf, 10))}, 1000)
469
470 " Again, but this time while we're in insert mode.
471 call term_sendkeys(buf, "i\<ScrollWheelDown>\<Esc>")
472 call TermWait(buf)
473 call term_sendkeys(buf, ":echo g:scrolled\<CR>")
474 call WaitForAssert({-> assert_match('^2', term_getline(buf, 10))}, 1000)
475
476 call StopVimInTerminal(buf)
LemonBoy66e13ae2022-04-21 22:52:11 +0100477endfunc
478
zeertzjqd58862d2022-04-12 11:32:48 +0100479func Test_WinScrolled_close_curwin()
480 CheckRunVimInTerminal
481
482 let lines =<< trim END
483 set nowrap scrolloff=0
484 call setline(1, ['aaa', 'bbb'])
485 vsplit
486 au WinScrolled * close
487 au VimLeave * call writefile(['123456'], 'Xtestout')
488 END
Bram Moolenaare1f3ab72022-09-04 21:29:08 +0100489 call writefile(lines, 'Xtest_winscrolled_close_curwin', 'D')
zeertzjqd58862d2022-04-12 11:32:48 +0100490 let buf = RunVimInTerminal('-S Xtest_winscrolled_close_curwin', {'rows': 6})
491
492 " This was using freed memory
493 call term_sendkeys(buf, "\<C-E>")
494 call TermWait(buf)
495 call StopVimInTerminal(buf)
496
Bram Moolenaar0a60f792022-11-19 21:18:11 +0000497 " check the startup script finished to the end
zeertzjqd58862d2022-04-12 11:32:48 +0100498 call assert_equal(['123456'], readfile('Xtestout'))
zeertzjqd58862d2022-04-12 11:32:48 +0100499 call delete('Xtestout')
500endfunc
501
Bram Moolenaar0a60f792022-11-19 21:18:11 +0000502func Test_WinScrolled_once_only()
503 CheckRunVimInTerminal
504
505 let lines =<< trim END
506 set cmdheight=2
507 call setline(1, ['aaa', 'bbb'])
508 let trigger_count = 0
509 func ShowInfo(id)
510 echo g:trigger_count g:winid winlayout()
511 endfunc
512
513 vsplit
514 split
515 " use a timer to show the info after a redraw
516 au WinScrolled * let trigger_count += 1 | let winid = expand('<amatch>') | call timer_start(100, 'ShowInfo')
517 wincmd j
518 wincmd l
519 END
520 call writefile(lines, 'Xtest_winscrolled_once', 'D')
521 let buf = RunVimInTerminal('-S Xtest_winscrolled_once', #{rows: 10, cols: 60, statusoff: 2})
522
523 call term_sendkeys(buf, "\<C-E>")
524 call VerifyScreenDump(buf, 'Test_winscrolled_once_only_1', {})
525
526 call StopVimInTerminal(buf)
527endfunc
528
Bram Moolenaar29967732022-11-20 12:11:45 +0000529" Check that WinScrolled is not triggered immediately when defined and there
530" are split windows.
531func Test_WinScrolled_not_when_defined()
532 CheckRunVimInTerminal
533
534 let lines =<< trim END
535 call setline(1, ['aaa', 'bbb'])
536 echo 'nothing happened'
537 func ShowTriggered(id)
538 echo 'triggered'
539 endfunc
540 END
541 call writefile(lines, 'Xtest_winscrolled_not', 'D')
542 let buf = RunVimInTerminal('-S Xtest_winscrolled_not', #{rows: 10, cols: 60, statusoff: 2})
543 call term_sendkeys(buf, ":split\<CR>")
544 call TermWait(buf)
545 " use a timer to show the message after redrawing
546 call term_sendkeys(buf, ":au WinScrolled * call timer_start(100, 'ShowTriggered')\<CR>")
547 call VerifyScreenDump(buf, 'Test_winscrolled_not_when_defined_1', {})
548
549 call term_sendkeys(buf, "\<C-E>")
550 call VerifyScreenDump(buf, 'Test_winscrolled_not_when_defined_2', {})
551
552 call StopVimInTerminal(buf)
553endfunc
554
zeertzjq670ab032022-08-28 19:16:15 +0100555func Test_WinScrolled_long_wrapped()
556 CheckRunVimInTerminal
557
558 let lines =<< trim END
559 set scrolloff=0
560 let height = winheight(0)
561 let width = winwidth(0)
562 let g:scrolled = 0
563 au WinScrolled * let g:scrolled += 1
564 call setline(1, repeat('foo', height * width))
565 call cursor(1, height * width)
566 END
Bram Moolenaare1f3ab72022-09-04 21:29:08 +0100567 call writefile(lines, 'Xtest_winscrolled_long_wrapped', 'D')
zeertzjq670ab032022-08-28 19:16:15 +0100568 let buf = RunVimInTerminal('-S Xtest_winscrolled_long_wrapped', {'rows': 6})
569
570 call term_sendkeys(buf, ":echo g:scrolled\<CR>")
571 call WaitForAssert({-> assert_match('^0 ', term_getline(buf, 6))}, 1000)
572
573 call term_sendkeys(buf, 'gj')
574 call term_sendkeys(buf, ":echo g:scrolled\<CR>")
575 call WaitForAssert({-> assert_match('^1 ', term_getline(buf, 6))}, 1000)
576
577 call term_sendkeys(buf, '0')
578 call term_sendkeys(buf, ":echo g:scrolled\<CR>")
579 call WaitForAssert({-> assert_match('^2 ', term_getline(buf, 6))}, 1000)
580
581 call term_sendkeys(buf, '$')
582 call term_sendkeys(buf, ":echo g:scrolled\<CR>")
583 call WaitForAssert({-> assert_match('^3 ', term_getline(buf, 6))}, 1000)
Bram Moolenaar23526d22022-12-05 15:50:41 +0000584
585 call StopVimInTerminal(buf)
zeertzjq670ab032022-08-28 19:16:15 +0100586endfunc
587
zeertzjq3fc84dc2022-12-07 09:17:59 +0000588func Test_WinScrolled_diff()
589 CheckRunVimInTerminal
590
591 let lines =<< trim END
592 set diffopt+=foldcolumn:0
593 call setline(1, ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i'])
594 vnew
595 call setline(1, ['d', 'e', 'f', 'g', 'h', 'i'])
596 windo diffthis
597 func WriteScrollEvent()
598 call writefile([json_encode(v:event)], 'XscrollEvent')
599 endfunc
600 au WinScrolled * call WriteScrollEvent()
601 END
602 call writefile(lines, 'Xtest_winscrolled_diff', 'D')
603 let buf = RunVimInTerminal('-S Xtest_winscrolled_diff', {'rows': 8})
604
605 call term_sendkeys(buf, "\<C-E>")
606 call WaitForAssert({-> assert_match('^d', term_getline(buf, 3))}, 1000)
607
608 let event = readfile('XscrollEvent')[0]->json_decode()
609 call assert_equal({
610 \ 'all': {'leftcol': 0, 'topline': 1, 'topfill': 1, 'width': 0, 'height': 0, 'skipcol': 0},
611 \ '1000': {'leftcol': 0, 'topline': 1, 'topfill': 0, 'width': 0, 'height': 0, 'skipcol': 0},
612 \ '1001': {'leftcol': 0, 'topline': 0, 'topfill': -1, 'width': 0, 'height': 0, 'skipcol': 0}
613 \ }, event)
614
615 call term_sendkeys(buf, "2\<C-E>")
616 call WaitForAssert({-> assert_match('^f', term_getline(buf, 3))}, 1000)
617
618 let event = readfile('XscrollEvent')[0]->json_decode()
619 call assert_equal({
620 \ 'all': {'leftcol': 0, 'topline': 2, 'topfill': 2, 'width': 0, 'height': 0, 'skipcol': 0},
621 \ '1000': {'leftcol': 0, 'topline': 2, 'topfill': 0, 'width': 0, 'height': 0, 'skipcol': 0},
622 \ '1001': {'leftcol': 0, 'topline': 0, 'topfill': -2, 'width': 0, 'height': 0, 'skipcol': 0}
623 \ }, event)
624
625 call term_sendkeys(buf, "\<C-E>")
626 call WaitForAssert({-> assert_match('^g', term_getline(buf, 3))}, 1000)
627
628 let event = readfile('XscrollEvent')[0]->json_decode()
629 call assert_equal({
630 \ 'all': {'leftcol': 0, 'topline': 2, 'topfill': 0, 'width': 0, 'height': 0, 'skipcol': 0},
631 \ '1000': {'leftcol': 0, 'topline': 1, 'topfill': 0, 'width': 0, 'height': 0, 'skipcol': 0},
632 \ '1001': {'leftcol': 0, 'topline': 1, 'topfill': 0, 'width': 0, 'height': 0, 'skipcol': 0}
633 \ }, event)
634
635 call term_sendkeys(buf, "2\<C-Y>")
636 call WaitForAssert({-> assert_match('^e', term_getline(buf, 3))}, 1000)
637
638 let event = readfile('XscrollEvent')[0]->json_decode()
639 call assert_equal({
640 \ 'all': {'leftcol': 0, 'topline': 3, 'topfill': 1, 'width': 0, 'height': 0, 'skipcol': 0},
641 \ '1000': {'leftcol': 0, 'topline': -2, 'topfill': 0, 'width': 0, 'height': 0, 'skipcol': 0},
642 \ '1001': {'leftcol': 0, 'topline': -1, 'topfill': 1, 'width': 0, 'height': 0, 'skipcol': 0}
643 \ }, event)
644
645 call StopVimInTerminal(buf)
Dominique Pelle541c87c2023-01-17 21:20:44 +0000646 call delete('XscrollEvent')
zeertzjq3fc84dc2022-12-07 09:17:59 +0000647endfunc
648
naohiro ono23beefe2021-11-13 12:38:49 +0000649func Test_WinClosed()
650 " Test that the pattern is matched against the closed window's ID, and both
651 " <amatch> and <afile> are set to it.
652 new
653 let winid = win_getid()
654 let g:matched = v:false
655 augroup test-WinClosed
656 autocmd!
657 execute 'autocmd WinClosed' winid 'let g:matched = v:true'
658 autocmd WinClosed * let g:amatch = str2nr(expand('<amatch>'))
659 autocmd WinClosed * let g:afile = str2nr(expand('<afile>'))
660 augroup END
661 close
662 call assert_true(g:matched)
663 call assert_equal(winid, g:amatch)
664 call assert_equal(winid, g:afile)
665
666 " Test that WinClosed is non-recursive.
667 new
668 new
669 call assert_equal(3, winnr('$'))
670 let g:triggered = 0
671 augroup test-WinClosed
672 autocmd!
673 autocmd WinClosed * let g:triggered += 1
674 autocmd WinClosed * 2 wincmd c
675 augroup END
676 close
677 call assert_equal(1, winnr('$'))
678 call assert_equal(1, g:triggered)
679
680 autocmd! test-WinClosed
681 augroup! test-WinClosed
682 unlet g:matched
683 unlet g:amatch
684 unlet g:afile
685 unlet g:triggered
686endfunc
687
Bram Moolenaarc947b9a2022-04-06 17:59:21 +0100688func Test_WinClosed_throws()
689 vnew
690 let bnr = bufnr()
691 call assert_equal(1, bufloaded(bnr))
692 augroup test-WinClosed
693 autocmd WinClosed * throw 'foo'
694 augroup END
695 try
696 close
697 catch /.*/
698 endtry
699 call assert_equal(0, bufloaded(bnr))
700
701 autocmd! test-WinClosed
702 augroup! test-WinClosed
703endfunc
704
zeertzjq6a069402022-04-07 14:08:29 +0100705func Test_WinClosed_throws_with_tabs()
706 tabnew
707 let bnr = bufnr()
708 call assert_equal(1, bufloaded(bnr))
709 augroup test-WinClosed
710 autocmd WinClosed * throw 'foo'
711 augroup END
712 try
713 close
714 catch /.*/
715 endtry
716 call assert_equal(0, bufloaded(bnr))
717
718 autocmd! test-WinClosed
719 augroup! test-WinClosed
720endfunc
721
zeertzjq62de54b2022-09-22 18:08:37 +0100722" This used to trigger WinClosed twice for the same window, and the window's
723" buffer was NULL in the second autocommand.
724func Test_WinClosed_switch_tab()
725 edit Xa
726 split Xb
727 split Xc
728 tab split
729 new
730 augroup test-WinClosed
731 autocmd WinClosed * tabprev | bwipe!
732 augroup END
733 close
734 " Check that the tabline has been fully removed
735 call assert_equal([1, 1], win_screenpos(0))
736
737 autocmd! test-WinClosed
738 augroup! test-WinClosed
739 %bwipe!
740endfunc
741
Bram Moolenaare99e8442016-07-26 20:43:40 +0200742func s:AddAnAutocmd()
743 augroup vimBarTest
744 au BufReadCmd * echo 'hello'
745 augroup END
746 call assert_equal(3, len(split(execute('au vimBarTest'), "\n")))
747endfunc
748
749func Test_early_bar()
750 " test that a bar is recognized before the {event}
751 call s:AddAnAutocmd()
Bram Moolenaarb8e642f2021-11-20 10:38:25 +0000752 augroup vimBarTest | au! | let done = 77 | augroup END
Bram Moolenaare99e8442016-07-26 20:43:40 +0200753 call assert_equal(1, len(split(execute('au vimBarTest'), "\n")))
Bram Moolenaarb8e642f2021-11-20 10:38:25 +0000754 call assert_equal(77, done)
Bram Moolenaare99e8442016-07-26 20:43:40 +0200755
756 call s:AddAnAutocmd()
Bram Moolenaarb8e642f2021-11-20 10:38:25 +0000757 augroup vimBarTest| au!| let done = 88 | augroup END
Bram Moolenaare99e8442016-07-26 20:43:40 +0200758 call assert_equal(1, len(split(execute('au vimBarTest'), "\n")))
Bram Moolenaarb8e642f2021-11-20 10:38:25 +0000759 call assert_equal(88, done)
Bram Moolenaare99e8442016-07-26 20:43:40 +0200760
761 " test that a bar is recognized after the {event}
762 call s:AddAnAutocmd()
Bram Moolenaarb8e642f2021-11-20 10:38:25 +0000763 augroup vimBarTest| au!BufReadCmd| let done = 99 | augroup END
Bram Moolenaare99e8442016-07-26 20:43:40 +0200764 call assert_equal(1, len(split(execute('au vimBarTest'), "\n")))
Bram Moolenaarb8e642f2021-11-20 10:38:25 +0000765 call assert_equal(99, done)
Bram Moolenaare99e8442016-07-26 20:43:40 +0200766
767 " test that a bar is recognized after the {group}
768 call s:AddAnAutocmd()
769 au! vimBarTest|echo 'hello'
770 call assert_equal(1, len(split(execute('au vimBarTest'), "\n")))
771endfunc
Bram Moolenaarf2c4c392016-07-29 20:50:24 +0200772
Bram Moolenaar5c809082016-09-01 16:21:48 +0200773func RemoveGroup()
774 autocmd! StartOK
775 augroup! StartOK
776endfunc
777
Bram Moolenaarf2c4c392016-07-29 20:50:24 +0200778func Test_augroup_warning()
779 augroup TheWarning
780 au VimEnter * echo 'entering'
781 augroup END
Bram Moolenaar5dc4e2f2020-11-25 14:15:12 +0100782 call assert_match("TheWarning.*VimEnter", execute('au VimEnter'))
Bram Moolenaarf2c4c392016-07-29 20:50:24 +0200783 redir => res
784 augroup! TheWarning
785 redir END
Bram Moolenaar5dc4e2f2020-11-25 14:15:12 +0100786 call assert_match("W19:", res)
787 call assert_match("-Deleted-.*VimEnter", execute('au VimEnter'))
Bram Moolenaarf2c4c392016-07-29 20:50:24 +0200788
789 " check "Another" does not take the pace of the deleted entry
790 augroup Another
791 augroup END
Bram Moolenaar5dc4e2f2020-11-25 14:15:12 +0100792 call assert_match("-Deleted-.*VimEnter", execute('au VimEnter'))
Bram Moolenaaraeac9002016-09-06 22:15:08 +0200793 augroup! Another
Bram Moolenaar5c809082016-09-01 16:21:48 +0200794
795 " no warning for postpone aucmd delete
796 augroup StartOK
797 au VimEnter * call RemoveGroup()
798 augroup END
Bram Moolenaar5dc4e2f2020-11-25 14:15:12 +0100799 call assert_match("StartOK.*VimEnter", execute('au VimEnter'))
Bram Moolenaar5c809082016-09-01 16:21:48 +0200800 redir => res
801 doautocmd VimEnter
802 redir END
Bram Moolenaar5dc4e2f2020-11-25 14:15:12 +0100803 call assert_notmatch("W19:", res)
Bram Moolenaarde653f02016-09-03 16:59:06 +0200804 au! VimEnter
Bram Moolenaarad48e6c2020-04-21 22:19:45 +0200805
806 call assert_fails('augroup!', 'E471:')
Bram Moolenaarf2c4c392016-07-29 20:50:24 +0200807endfunc
Bram Moolenaarb62cc362016-09-03 16:43:53 +0200808
Bram Moolenaar8d84ff12017-10-26 16:42:16 +0200809func Test_BufReadCmdHelp()
810 " This used to cause access to free memory
811 au BufReadCmd * e +h
812 help
813
Bram Moolenaar8d84ff12017-10-26 16:42:16 +0200814 au! BufReadCmd
815endfunc
816
817func Test_BufReadCmdHelpJump()
818 " This used to cause access to free memory
819 au BufReadCmd * e +h{
Bram Moolenaarcf1ba352017-10-27 00:55:04 +0200820 " } to fix highlighting
821 call assert_fails('help', 'E434:')
Bram Moolenaar8d84ff12017-10-26 16:42:16 +0200822
Bram Moolenaar8d84ff12017-10-26 16:42:16 +0200823 au! BufReadCmd
824endfunc
825
zeertzjq93f72cc2022-08-26 15:34:52 +0100826" BufReadCmd is triggered for a "nofile" buffer. Check all values.
Bram Moolenaarb1d2c812022-08-26 11:55:01 +0100827func Test_BufReadCmdNofile()
zeertzjq93f72cc2022-08-26 15:34:52 +0100828 for val in ['nofile',
829 \ 'nowrite',
830 \ 'acwrite',
831 \ 'quickfix',
832 \ 'help',
833 \ 'terminal',
834 \ 'prompt',
835 \ 'popup',
836 \ ]
837 new somefile
838 exe 'set buftype=' .. val
839 au BufReadCmd somefile call setline(1, 'triggered')
840 edit
841 call assert_equal('triggered', getline(1))
Bram Moolenaarb1d2c812022-08-26 11:55:01 +0100842
zeertzjq93f72cc2022-08-26 15:34:52 +0100843 au! BufReadCmd
844 bwipe!
845 endfor
Bram Moolenaarb1d2c812022-08-26 11:55:01 +0100846endfunc
847
Bram Moolenaarb62cc362016-09-03 16:43:53 +0200848func Test_augroup_deleted()
Bram Moolenaarde653f02016-09-03 16:59:06 +0200849 " This caused a crash before E936 was introduced
Bram Moolenaarb62cc362016-09-03 16:43:53 +0200850 augroup x
Bram Moolenaarde653f02016-09-03 16:59:06 +0200851 call assert_fails('augroup! x', 'E936:')
852 au VimEnter * echo
853 augroup end
Bram Moolenaarb62cc362016-09-03 16:43:53 +0200854 augroup! x
Bram Moolenaar5dc4e2f2020-11-25 14:15:12 +0100855 call assert_match("-Deleted-.*VimEnter", execute('au VimEnter'))
Bram Moolenaarde653f02016-09-03 16:59:06 +0200856 au! VimEnter
Bram Moolenaarb62cc362016-09-03 16:43:53 +0200857endfunc
858
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200859" Tests for autocommands on :close command.
860" This used to be in test13.
861func Test_three_windows()
Bram Moolenaarb3435b02016-09-29 20:54:59 +0200862 " Clean up buffers, because in some cases this function fails.
863 call s:cleanup_buffers()
864
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200865 " Write three files and open them, each in a window.
866 " Then go to next window, with autocommand that deletes the previous one.
867 " Do this twice, writing the file.
868 e! Xtestje1
869 call setline(1, 'testje1')
870 w
871 sp Xtestje2
872 call setline(1, 'testje2')
873 w
874 sp Xtestje3
875 call setline(1, 'testje3')
876 w
877 wincmd w
878 au WinLeave Xtestje2 bwipe
879 wincmd w
880 call assert_equal('Xtestje1', expand('%'))
881
882 au WinLeave Xtestje1 bwipe Xtestje3
883 close
884 call assert_equal('Xtestje1', expand('%'))
885
886 " Test deleting the buffer on a Unload event. If this goes wrong there
887 " will be the ATTENTION prompt.
888 e Xtestje1
889 au!
890 au! BufUnload Xtestje1 bwipe
891 call assert_fails('e Xtestje3', 'E937:')
892 call assert_equal('Xtestje3', expand('%'))
893
894 e Xtestje2
895 sp Xtestje1
896 call assert_fails('e', 'E937:')
Bram Moolenaara997b452018-04-17 23:24:06 +0200897 call assert_equal('Xtestje1', expand('%'))
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200898
899 " Test changing buffers in a BufWipeout autocommand. If this goes wrong
900 " there are ml_line errors and/or a Crash.
901 au!
902 only
903 e Xanother
904 e Xtestje1
905 bwipe Xtestje2
906 bwipe Xtestje3
907 au BufWipeout Xtestje1 buf Xtestje1
908 bwipe
909 call assert_equal('Xanother', expand('%'))
910
911 only
912 help
913 wincmd w
914 1quit
915 call assert_equal('Xanother', expand('%'))
916
917 au!
Bram Moolenaar4520d442017-03-19 16:09:46 +0100918 enew
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200919 call delete('Xtestje1')
920 call delete('Xtestje2')
921 call delete('Xtestje3')
922endfunc
Bram Moolenaare13b9af2017-01-13 22:01:02 +0100923
924func Test_BufEnter()
925 au! BufEnter
926 au Bufenter * let val = val . '+'
927 let g:val = ''
928 split NewFile
929 call assert_equal('+', g:val)
930 bwipe!
931 call assert_equal('++', g:val)
932
933 " Also get BufEnter when editing a directory
Bram Moolenaar6f14da12022-09-07 21:30:44 +0100934 call mkdir('Xbufenterdir', 'D')
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +0100935 split Xbufenterdir
Bram Moolenaare13b9af2017-01-13 22:01:02 +0100936 call assert_equal('+++', g:val)
Bram Moolenaare94260f2017-03-21 15:50:12 +0100937
938 " On MS-Windows we can't edit the directory, make sure we wipe the right
939 " buffer.
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +0100940 bwipe! Xbufenterdir
Bram Moolenaare13b9af2017-01-13 22:01:02 +0100941 au! BufEnter
Bram Moolenaara9b5b852022-08-26 13:16:20 +0100942
943 " Editing a "nofile" buffer doesn't read the file but does trigger BufEnter
zeertzjq93f72cc2022-08-26 15:34:52 +0100944 " for historic reasons. Also test other 'buftype' values.
945 for val in ['nofile',
946 \ 'nowrite',
947 \ 'acwrite',
948 \ 'quickfix',
949 \ 'help',
950 \ 'terminal',
951 \ 'prompt',
952 \ 'popup',
953 \ ]
954 new somefile
955 exe 'set buftype=' .. val
956 au BufEnter somefile call setline(1, 'some text')
957 edit
958 call assert_equal('some text', getline(1))
959 bwipe!
960 au! BufEnter
961 endfor
Bram Moolenaar9fda8152022-11-19 13:14:10 +0000962
963 new
964 new
965 autocmd BufEnter * ++once close
966 call assert_fails('close', 'E1312:')
967
968 au! BufEnter
969 only
Bram Moolenaare13b9af2017-01-13 22:01:02 +0100970endfunc
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100971
972" Closing a window might cause an endless loop
973" E814 for older Vims
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200974func Test_autocmd_bufwipe_in_SessLoadPost()
Bram Moolenaar1d68d9b2017-10-13 22:33:32 +0200975 edit Xtest
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100976 tabnew
Bram Moolenaar1d68d9b2017-10-13 22:33:32 +0200977 file Xsomething
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100978 set noswapfile
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100979 mksession!
980
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200981 let content =<< trim [CODE]
Bram Moolenaar62cd26a2020-10-11 20:08:44 +0200982 call test_override('ui_delay', 10)
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200983 set nocp noswapfile
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100984 let v:swapchoice = "e"
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200985 augroup test_autocmd_sessionload
986 autocmd!
987 autocmd SessionLoadPost * exe bufnr("Xsomething") . "bw!"
988 augroup END
989
990 func WriteErrors()
Bram Moolenaarbe9fc5b2022-09-09 17:09:35 +0100991 call writefile([execute("messages")], "XerrorsBwipe")
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200992 endfunc
993 au VimLeave * call WriteErrors()
994 [CODE]
995
Bram Moolenaare1f3ab72022-09-04 21:29:08 +0100996 call writefile(content, 'Xvimrc', 'D')
Bram Moolenaar93344c22019-08-14 21:12:05 +0200997 call system(GetVimCommand('Xvimrc') .. ' --not-a-term --noplugins -S Session.vim -c cq')
Bram Moolenaarae04a602022-09-09 15:08:10 +0100998 sleep 100m
Bram Moolenaarbe9fc5b2022-09-09 17:09:35 +0100999 let errors = join(readfile('XerrorsBwipe'))
Bram Moolenaare2e40752020-09-04 21:18:46 +02001000 call assert_match('E814:', errors)
Bram Moolenaar8c752bd2017-03-19 17:09:56 +01001001
Bram Moolenaar8c752bd2017-03-19 17:09:56 +01001002 set swapfile
Bram Moolenaarbe9fc5b2022-09-09 17:09:35 +01001003 for file in ['Session.vim', 'XerrorsBwipe']
Bram Moolenaar8c752bd2017-03-19 17:09:56 +01001004 call delete(file)
1005 endfor
1006endfunc
1007
Bram Moolenaar797e63b2021-01-15 16:22:52 +01001008" Using :blast and :ball for many events caused a crash, because b_nwindows was
1009" not incremented correctly.
1010func Test_autocmd_blast_badd()
1011 let content =<< trim [CODE]
1012 au BufNew,BufAdd,BufWinEnter,BufEnter,BufLeave,BufWinLeave,BufUnload,VimEnter foo* blast
1013 edit foo1
1014 au BufNew,BufAdd,BufWinEnter,BufEnter,BufLeave,BufWinLeave,BufUnload,VimEnter foo* ball
1015 edit foo2
Bram Moolenaarbe9fc5b2022-09-09 17:09:35 +01001016 call writefile(['OK'], 'XerrorsBlast')
Bram Moolenaar797e63b2021-01-15 16:22:52 +01001017 qall
1018 [CODE]
1019
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01001020 call writefile(content, 'XblastBall', 'D')
Bram Moolenaar797e63b2021-01-15 16:22:52 +01001021 call system(GetVimCommand() .. ' --clean -S XblastBall')
Bram Moolenaarae04a602022-09-09 15:08:10 +01001022 sleep 100m
Bram Moolenaarbe9fc5b2022-09-09 17:09:35 +01001023 call assert_match('OK', readfile('XerrorsBlast')->join())
Bram Moolenaar797e63b2021-01-15 16:22:52 +01001024
Bram Moolenaarbe9fc5b2022-09-09 17:09:35 +01001025 call delete('XerrorsBlast')
Bram Moolenaar797e63b2021-01-15 16:22:52 +01001026endfunc
1027
Bram Moolenaar8c752bd2017-03-19 17:09:56 +01001028" SEGV occurs in older versions.
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001029func Test_autocmd_bufwipe_in_SessLoadPost2()
Bram Moolenaar8c752bd2017-03-19 17:09:56 +01001030 tabnew
1031 set noswapfile
Bram Moolenaar8c752bd2017-03-19 17:09:56 +01001032 mksession!
1033
Bram Moolenaarc79745a2019-05-20 22:12:34 +02001034 let content =<< trim [CODE]
1035 set nocp noswapfile
1036 function! DeleteInactiveBufs()
1037 tabfirst
1038 let tabblist = []
1039 for i in range(1, tabpagenr(''$''))
1040 call extend(tabblist, tabpagebuflist(i))
1041 endfor
1042 for b in range(1, bufnr(''$''))
1043 if bufexists(b) && buflisted(b) && (index(tabblist, b) == -1 || bufname(b) =~# ''^$'')
1044 exec ''bwipeout '' . b
1045 endif
1046 endfor
1047 echomsg "SessionLoadPost DONE"
1048 endfunction
1049 au SessionLoadPost * call DeleteInactiveBufs()
1050
1051 func WriteErrors()
Bram Moolenaarbe9fc5b2022-09-09 17:09:35 +01001052 call writefile([execute("messages")], "XerrorsPost")
Bram Moolenaarc79745a2019-05-20 22:12:34 +02001053 endfunc
1054 au VimLeave * call WriteErrors()
1055 [CODE]
1056
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01001057 call writefile(content, 'Xvimrc', 'D')
Bram Moolenaar93344c22019-08-14 21:12:05 +02001058 call system(GetVimCommand('Xvimrc') .. ' --not-a-term --noplugins -S Session.vim -c cq')
Bram Moolenaarae04a602022-09-09 15:08:10 +01001059 sleep 100m
Bram Moolenaarbe9fc5b2022-09-09 17:09:35 +01001060 let errors = join(readfile('XerrorsPost'))
Bram Moolenaare94260f2017-03-21 15:50:12 +01001061 " This probably only ever matches on unix.
1062 call assert_notmatch('Caught deadly signal SEGV', errors)
1063 call assert_match('SessionLoadPost DONE', errors)
Bram Moolenaar8c752bd2017-03-19 17:09:56 +01001064
Bram Moolenaar8c752bd2017-03-19 17:09:56 +01001065 set swapfile
Bram Moolenaarbe9fc5b2022-09-09 17:09:35 +01001066 for file in ['Session.vim', 'XerrorsPost']
Bram Moolenaar8c752bd2017-03-19 17:09:56 +01001067 call delete(file)
1068 endfor
1069endfunc
Bram Moolenaarfaf29d72017-07-09 11:07:16 +02001070
1071func Test_empty_doau()
1072 doau \|
1073endfunc
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001074
1075func s:AutoCommandOptionSet(match)
Bram Moolenaard7c96872019-06-15 17:12:48 +02001076 let template = "Option: <%s>, OldVal: <%s>, OldValLocal: <%s>, OldValGlobal: <%s>, NewVal: <%s>, Scope: <%s>, Command: <%s>\n"
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001077 let item = remove(g:options, 0)
Bram Moolenaard7c96872019-06-15 17:12:48 +02001078 let expected = printf(template, item[0], item[1], item[2], item[3], item[4], item[5], item[6])
1079 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 +02001080 let g:opt = [expected, actual]
1081 "call assert_equal(expected, actual)
1082endfunc
1083
1084func Test_OptionSet()
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +02001085 CheckOption autochdir
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001086
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +02001087 badd test_autocmd.vim
1088
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001089 call test_override('starting', 1)
1090 set nocp
1091 au OptionSet * :call s:AutoCommandOptionSet(expand("<amatch>"))
1092
1093 " 1: Setting number option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001094 let g:options = [['number', 0, 0, 0, 1, 'global', 'set']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001095 set nu
1096 call assert_equal([], g:options)
1097 call assert_equal(g:opt[0], g:opt[1])
1098
1099 " 2: Setting local number option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001100 let g:options = [['number', 1, 1, '', 0, 'local', 'setlocal']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001101 setlocal nonu
1102 call assert_equal([], g:options)
1103 call assert_equal(g:opt[0], g:opt[1])
1104
1105 " 3: Setting global number option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001106 let g:options = [['number', 1, '', 1, 0, 'global', 'setglobal']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001107 setglobal nonu
1108 call assert_equal([], g:options)
1109 call assert_equal(g:opt[0], g:opt[1])
1110
1111 " 4: Setting local autoindent option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001112 let g:options = [['autoindent', 0, 0, '', 1, 'local', 'setlocal']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001113 setlocal ai
1114 call assert_equal([], g:options)
1115 call assert_equal(g:opt[0], g:opt[1])
1116
1117 " 5: Setting global autoindent option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001118 let g:options = [['autoindent', 0, '', 0, 1, 'global', 'setglobal']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001119 setglobal ai
1120 call assert_equal([], g:options)
1121 call assert_equal(g:opt[0], g:opt[1])
1122
1123 " 6: Setting global autoindent option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001124 let g:options = [['autoindent', 1, 1, 1, 0, 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001125 set ai!
1126 call assert_equal([], g:options)
1127 call assert_equal(g:opt[0], g:opt[1])
1128
1129 " 6a: Setting global autoindent option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001130 let g:options = [['autoindent', 1, 1, 0, 0, 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001131 noa setlocal ai
1132 noa setglobal noai
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001133 set ai!
1134 call assert_equal([], g:options)
1135 call assert_equal(g:opt[0], g:opt[1])
1136
1137 " Should not print anything, use :noa
1138 " 7: don't trigger OptionSet"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001139 let g:options = [['invalid', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001140 noa set nonu
Bram Moolenaard7c96872019-06-15 17:12:48 +02001141 call assert_equal([['invalid', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid']], g:options)
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001142 call assert_equal(g:opt[0], g:opt[1])
1143
1144 " 8: Setting several global list and number option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001145 let g:options = [['list', 0, 0, 0, 1, 'global', 'set'], ['number', 0, 0, 0, 1, 'global', 'set']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001146 set list nu
1147 call assert_equal([], g:options)
1148 call assert_equal(g:opt[0], g:opt[1])
1149
1150 " 9: don't trigger OptionSet"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001151 let g:options = [['invalid', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid'], ['invalid', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001152 noa set nolist nonu
Bram Moolenaard7c96872019-06-15 17:12:48 +02001153 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 +02001154 call assert_equal(g:opt[0], g:opt[1])
1155
1156 " 10: Setting global acd"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001157 let g:options = [['autochdir', 0, 0, '', 1, 'local', 'setlocal']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001158 setlocal acd
1159 call assert_equal([], g:options)
1160 call assert_equal(g:opt[0], g:opt[1])
1161
1162 " 11: Setting global autoread (also sets local value)"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001163 let g:options = [['autoread', 0, 0, 0, 1, 'global', 'set']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001164 set ar
1165 call assert_equal([], g:options)
1166 call assert_equal(g:opt[0], g:opt[1])
1167
1168 " 12: Setting local autoread"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001169 let g:options = [['autoread', 1, 1, '', 1, 'local', 'setlocal']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001170 setlocal ar
1171 call assert_equal([], g:options)
1172 call assert_equal(g:opt[0], g:opt[1])
1173
1174 " 13: Setting global autoread"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001175 let g:options = [['autoread', 1, '', 1, 0, 'global', 'setglobal']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001176 setglobal invar
1177 call assert_equal([], g:options)
1178 call assert_equal(g:opt[0], g:opt[1])
1179
1180 " 14: Setting option backspace through :let"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001181 let g:options = [['backspace', '', '', '', 'eol,indent,start', 'global', 'set']]
1182 let &bs = "eol,indent,start"
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001183 call assert_equal([], g:options)
1184 call assert_equal(g:opt[0], g:opt[1])
1185
1186 " 15: Setting option backspace through setbufvar()"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001187 let g:options = [['backup', 0, 0, '', 1, 'local', 'setlocal']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001188 " try twice, first time, shouldn't trigger because option name is invalid,
1189 " second time, it should trigger
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +02001190 let bnum = bufnr('%')
Bram Moolenaare2e40752020-09-04 21:18:46 +02001191 call assert_fails("call setbufvar(bnum, '&l:bk', 1)", 'E355:')
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001192 " should trigger, use correct option name
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +02001193 call setbufvar(bnum, '&backup', 1)
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001194 call assert_equal([], g:options)
1195 call assert_equal(g:opt[0], g:opt[1])
1196
1197 " 16: Setting number option using setwinvar"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001198 let g:options = [['number', 0, 0, '', 1, 'local', 'setlocal']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001199 call setwinvar(0, '&number', 1)
1200 call assert_equal([], g:options)
1201 call assert_equal(g:opt[0], g:opt[1])
1202
1203 " 17: Setting key option, shouldn't trigger"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001204 let g:options = [['key', 'invalid', 'invalid1', 'invalid2', 'invalid3', 'invalid4', 'invalid5']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001205 setlocal key=blah
1206 setlocal key=
Bram Moolenaard7c96872019-06-15 17:12:48 +02001207 call assert_equal([['key', 'invalid', 'invalid1', 'invalid2', 'invalid3', 'invalid4', 'invalid5']], g:options)
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001208 call assert_equal(g:opt[0], g:opt[1])
1209
Bram Moolenaard7c96872019-06-15 17:12:48 +02001210
1211 " 18a: Setting string global option"
1212 let oldval = &backupext
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001213 let g:options = [['backupext', oldval, oldval, oldval, 'foo', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001214 set backupext=foo
1215 call assert_equal([], g:options)
1216 call assert_equal(g:opt[0], g:opt[1])
1217
1218 " 18b: Resetting string global option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001219 let g:options = [['backupext', 'foo', 'foo', 'foo', oldval, 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001220 set backupext&
1221 call assert_equal([], g:options)
1222 call assert_equal(g:opt[0], g:opt[1])
1223
1224 " 18c: Setting global string global option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001225 let g:options = [['backupext', oldval, '', oldval, 'bar', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001226 setglobal backupext=bar
1227 call assert_equal([], g:options)
1228 call assert_equal(g:opt[0], g:opt[1])
1229
1230 " 18d: Setting local string global option"
1231 " As this is a global option this sets the global value even though
1232 " :setlocal is used!
1233 noa set backupext& " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001234 let g:options = [['backupext', oldval, oldval, '', 'baz', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001235 setlocal backupext=baz
1236 call assert_equal([], g:options)
1237 call assert_equal(g:opt[0], g:opt[1])
1238
1239 " 18e: Setting again string global option"
1240 noa setglobal backupext=ext_global " Reset global and local value (without triggering autocmd)
1241 noa setlocal backupext=ext_local " Sets the global(!) value!
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001242 let g:options = [['backupext', 'ext_local', 'ext_local', 'ext_local', 'fuu', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001243 set backupext=fuu
1244 call assert_equal([], g:options)
1245 call assert_equal(g:opt[0], g:opt[1])
1246
1247
zeertzjqb811de52021-10-21 10:50:44 +01001248 " 19a: Setting string global-local (to buffer) option"
Bram Moolenaar8efa0262017-08-20 15:47:20 +02001249 let oldval = &tags
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001250 let g:options = [['tags', oldval, oldval, oldval, 'tagpath', 'global', 'set']]
Bram Moolenaar8efa0262017-08-20 15:47:20 +02001251 set tags=tagpath
1252 call assert_equal([], g:options)
1253 call assert_equal(g:opt[0], g:opt[1])
1254
zeertzjqb811de52021-10-21 10:50:44 +01001255 " 19b: Resetting string global-local (to buffer) option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001256 let g:options = [['tags', 'tagpath', 'tagpath', 'tagpath', oldval, 'global', 'set']]
Bram Moolenaar8efa0262017-08-20 15:47:20 +02001257 set tags&
1258 call assert_equal([], g:options)
1259 call assert_equal(g:opt[0], g:opt[1])
1260
zeertzjqb811de52021-10-21 10:50:44 +01001261 " 19c: Setting global string global-local (to buffer) option "
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001262 let g:options = [['tags', oldval, '', oldval, 'tagpath1', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001263 setglobal tags=tagpath1
1264 call assert_equal([], g:options)
1265 call assert_equal(g:opt[0], g:opt[1])
1266
zeertzjqb811de52021-10-21 10:50:44 +01001267 " 19d: Setting local string global-local (to buffer) option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001268 let g:options = [['tags', 'tagpath1', 'tagpath1', '', 'tagpath2', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001269 setlocal tags=tagpath2
1270 call assert_equal([], g:options)
1271 call assert_equal(g:opt[0], g:opt[1])
1272
zeertzjqb811de52021-10-21 10:50:44 +01001273 " 19e: Setting again string global-local (to buffer) option"
1274 " Note: v:option_old is the old global value for global-local string options
Bram Moolenaard7c96872019-06-15 17:12:48 +02001275 " but the old local value for all other kinds of options.
1276 noa setglobal tags=tag_global " Reset global and local value (without triggering autocmd)
1277 noa setlocal tags=tag_local
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001278 let g:options = [['tags', 'tag_global', 'tag_local', 'tag_global', 'tagpath', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001279 set tags=tagpath
1280 call assert_equal([], g:options)
1281 call assert_equal(g:opt[0], g:opt[1])
1282
zeertzjqb811de52021-10-21 10:50:44 +01001283 " 19f: Setting string global-local (to buffer) option to an empty string"
1284 " Note: v:option_old is the old global value for global-local string options
Bram Moolenaard7c96872019-06-15 17:12:48 +02001285 " but the old local value for all other kinds of options.
1286 noa set tags=tag_global " Reset global and local value (without triggering autocmd)
1287 noa setlocal tags= " empty string
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001288 let g:options = [['tags', 'tag_global', '', 'tag_global', 'tagpath', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001289 set tags=tagpath
1290 call assert_equal([], g:options)
1291 call assert_equal(g:opt[0], g:opt[1])
1292
1293
1294 " 20a: Setting string local (to buffer) option"
1295 let oldval = &spelllang
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001296 let g:options = [['spelllang', oldval, oldval, oldval, 'elvish,klingon', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001297 set spelllang=elvish,klingon
1298 call assert_equal([], g:options)
1299 call assert_equal(g:opt[0], g:opt[1])
1300
1301 " 20b: Resetting string local (to buffer) option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001302 let g:options = [['spelllang', 'elvish,klingon', 'elvish,klingon', 'elvish,klingon', oldval, 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001303 set spelllang&
1304 call assert_equal([], g:options)
1305 call assert_equal(g:opt[0], g:opt[1])
1306
1307 " 20c: Setting global string local (to buffer) option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001308 let g:options = [['spelllang', oldval, '', oldval, 'elvish', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001309 setglobal spelllang=elvish
1310 call assert_equal([], g:options)
1311 call assert_equal(g:opt[0], g:opt[1])
1312
1313 " 20d: Setting local string local (to buffer) option"
1314 noa set spelllang& " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001315 let g:options = [['spelllang', oldval, oldval, '', 'klingon', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001316 setlocal spelllang=klingon
1317 call assert_equal([], g:options)
1318 call assert_equal(g:opt[0], g:opt[1])
1319
1320 " 20e: Setting again string local (to buffer) option"
zeertzjqb811de52021-10-21 10:50:44 +01001321 " Note: v:option_old is the old global value for global-local string options
Bram Moolenaard7c96872019-06-15 17:12:48 +02001322 " but the old local value for all other kinds of options.
1323 noa setglobal spelllang=spellglobal " Reset global and local value (without triggering autocmd)
1324 noa setlocal spelllang=spelllocal
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001325 let g:options = [['spelllang', 'spelllocal', 'spelllocal', 'spellglobal', 'foo', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001326 set spelllang=foo
1327 call assert_equal([], g:options)
1328 call assert_equal(g:opt[0], g:opt[1])
1329
1330
zeertzjqb811de52021-10-21 10:50:44 +01001331 " 21a: Setting string global-local (to window) option"
Bram Moolenaard7c96872019-06-15 17:12:48 +02001332 let oldval = &statusline
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001333 let g:options = [['statusline', oldval, oldval, oldval, 'foo', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001334 set statusline=foo
1335 call assert_equal([], g:options)
1336 call assert_equal(g:opt[0], g:opt[1])
1337
zeertzjqb811de52021-10-21 10:50:44 +01001338 " 21b: Resetting string global-local (to window) option"
1339 " Note: v:option_old is the old global value for global-local string options
Bram Moolenaard7c96872019-06-15 17:12:48 +02001340 " but the old local value for all other kinds of options.
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001341 let g:options = [['statusline', 'foo', 'foo', 'foo', oldval, 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001342 set statusline&
1343 call assert_equal([], g:options)
1344 call assert_equal(g:opt[0], g:opt[1])
1345
zeertzjqb811de52021-10-21 10:50:44 +01001346 " 21c: Setting global string global-local (to window) option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001347 let g:options = [['statusline', oldval, '', oldval, 'bar', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001348 setglobal statusline=bar
1349 call assert_equal([], g:options)
1350 call assert_equal(g:opt[0], g:opt[1])
1351
zeertzjqb811de52021-10-21 10:50:44 +01001352 " 21d: Setting local string global-local (to window) option"
Bram Moolenaard7c96872019-06-15 17:12:48 +02001353 noa set statusline& " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001354 let g:options = [['statusline', oldval, oldval, '', 'baz', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001355 setlocal statusline=baz
1356 call assert_equal([], g:options)
1357 call assert_equal(g:opt[0], g:opt[1])
1358
zeertzjqb811de52021-10-21 10:50:44 +01001359 " 21e: Setting again string global-local (to window) option"
1360 " Note: v:option_old is the old global value for global-local string options
Bram Moolenaard7c96872019-06-15 17:12:48 +02001361 " but the old local value for all other kinds of options.
1362 noa setglobal statusline=bar " Reset global and local value (without triggering autocmd)
1363 noa setlocal statusline=baz
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001364 let g:options = [['statusline', 'bar', 'baz', 'bar', 'foo', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001365 set statusline=foo
1366 call assert_equal([], g:options)
1367 call assert_equal(g:opt[0], g:opt[1])
1368
1369
1370 " 22a: Setting string local (to window) option"
1371 let oldval = &foldignore
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001372 let g:options = [['foldignore', oldval, oldval, oldval, 'fo', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001373 set foldignore=fo
1374 call assert_equal([], g:options)
1375 call assert_equal(g:opt[0], g:opt[1])
1376
1377 " 22b: Resetting string local (to window) option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001378 let g:options = [['foldignore', 'fo', 'fo', 'fo', oldval, 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001379 set foldignore&
1380 call assert_equal([], g:options)
1381 call assert_equal(g:opt[0], g:opt[1])
1382
1383 " 22c: Setting global string local (to window) option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001384 let g:options = [['foldignore', oldval, '', oldval, 'bar', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001385 setglobal foldignore=bar
1386 call assert_equal([], g:options)
1387 call assert_equal(g:opt[0], g:opt[1])
1388
1389 " 22d: Setting local string local (to window) option"
1390 noa set foldignore& " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001391 let g:options = [['foldignore', oldval, oldval, '', 'baz', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001392 setlocal foldignore=baz
1393 call assert_equal([], g:options)
1394 call assert_equal(g:opt[0], g:opt[1])
1395
1396 " 22e: Setting again string local (to window) option"
1397 noa setglobal foldignore=glob " Reset global and local value (without triggering autocmd)
1398 noa setlocal foldignore=loc
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001399 let g:options = [['foldignore', 'loc', 'loc', 'glob', 'fo', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001400 set foldignore=fo
1401 call assert_equal([], g:options)
1402 call assert_equal(g:opt[0], g:opt[1])
1403
1404
zeertzjqb811de52021-10-21 10:50:44 +01001405 " 23a: Setting global number global option"
Bram Moolenaard7c96872019-06-15 17:12:48 +02001406 noa setglobal cmdheight=8 " Reset global and local value (without triggering autocmd)
1407 noa setlocal cmdheight=1 " Sets the global(!) value!
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001408 let g:options = [['cmdheight', '1', '', '1', '2', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001409 setglobal cmdheight=2
1410 call assert_equal([], g:options)
1411 call assert_equal(g:opt[0], g:opt[1])
1412
1413 " 23b: Setting local number global option"
1414 noa setglobal cmdheight=8 " Reset global and local value (without triggering autocmd)
1415 noa setlocal cmdheight=1 " Sets the global(!) value!
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001416 let g:options = [['cmdheight', '1', '1', '', '2', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001417 setlocal cmdheight=2
1418 call assert_equal([], g:options)
1419 call assert_equal(g:opt[0], g:opt[1])
1420
1421 " 23c: Setting again number global option"
1422 noa setglobal cmdheight=8 " Reset global and local value (without triggering autocmd)
1423 noa setlocal cmdheight=1 " Sets the global(!) value!
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001424 let g:options = [['cmdheight', '1', '1', '1', '2', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001425 set cmdheight=2
1426 call assert_equal([], g:options)
1427 call assert_equal(g:opt[0], g:opt[1])
1428
1429 " 23d: Setting again number global option"
1430 noa set cmdheight=8 " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001431 let g:options = [['cmdheight', '8', '8', '8', '2', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001432 set cmdheight=2
1433 call assert_equal([], g:options)
1434 call assert_equal(g:opt[0], g:opt[1])
1435
1436
1437 " 24a: Setting global number global-local (to buffer) option"
1438 noa setglobal undolevels=8 " Reset global and local value (without triggering autocmd)
1439 noa setlocal undolevels=1
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001440 let g:options = [['undolevels', '8', '', '8', '2', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001441 setglobal undolevels=2
1442 call assert_equal([], g:options)
1443 call assert_equal(g:opt[0], g:opt[1])
1444
1445 " 24b: Setting local number global-local (to buffer) option"
1446 noa setglobal undolevels=8 " Reset global and local value (without triggering autocmd)
1447 noa setlocal undolevels=1
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001448 let g:options = [['undolevels', '1', '1', '', '2', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001449 setlocal undolevels=2
1450 call assert_equal([], g:options)
1451 call assert_equal(g:opt[0], g:opt[1])
1452
1453 " 24c: Setting again number global-local (to buffer) option"
1454 noa setglobal undolevels=8 " Reset global and local value (without triggering autocmd)
1455 noa setlocal undolevels=1
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001456 let g:options = [['undolevels', '1', '1', '8', '2', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001457 set undolevels=2
1458 call assert_equal([], g:options)
1459 call assert_equal(g:opt[0], g:opt[1])
1460
1461 " 24d: Setting again global number global-local (to buffer) option"
1462 noa set undolevels=8 " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001463 let g:options = [['undolevels', '8', '8', '8', '2', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001464 set undolevels=2
1465 call assert_equal([], g:options)
1466 call assert_equal(g:opt[0], g:opt[1])
1467
1468
1469 " 25a: Setting global number local (to buffer) option"
1470 noa setglobal wrapmargin=8 " Reset global and local value (without triggering autocmd)
1471 noa setlocal wrapmargin=1
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001472 let g:options = [['wrapmargin', '8', '', '8', '2', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001473 setglobal wrapmargin=2
1474 call assert_equal([], g:options)
1475 call assert_equal(g:opt[0], g:opt[1])
1476
1477 " 25b: Setting local number local (to buffer) option"
1478 noa setglobal wrapmargin=8 " Reset global and local value (without triggering autocmd)
1479 noa setlocal wrapmargin=1
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001480 let g:options = [['wrapmargin', '1', '1', '', '2', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001481 setlocal wrapmargin=2
1482 call assert_equal([], g:options)
1483 call assert_equal(g:opt[0], g:opt[1])
1484
1485 " 25c: Setting again number local (to buffer) option"
1486 noa setglobal wrapmargin=8 " Reset global and local value (without triggering autocmd)
1487 noa setlocal wrapmargin=1
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001488 let g:options = [['wrapmargin', '1', '1', '8', '2', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001489 set wrapmargin=2
1490 call assert_equal([], g:options)
1491 call assert_equal(g:opt[0], g:opt[1])
1492
1493 " 25d: Setting again global number local (to buffer) option"
1494 noa set wrapmargin=8 " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001495 let g:options = [['wrapmargin', '8', '8', '8', '2', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001496 set wrapmargin=2
1497 call assert_equal([], g:options)
1498 call assert_equal(g:opt[0], g:opt[1])
1499
1500
1501 " 26: Setting number global-local (to window) option.
1502 " Such option does currently not exist.
1503
1504
1505 " 27a: Setting global number local (to window) option"
1506 noa setglobal foldcolumn=8 " Reset global and local value (without triggering autocmd)
1507 noa setlocal foldcolumn=1
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001508 let g:options = [['foldcolumn', '8', '', '8', '2', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001509 setglobal foldcolumn=2
1510 call assert_equal([], g:options)
1511 call assert_equal(g:opt[0], g:opt[1])
1512
1513 " 27b: Setting local number local (to window) option"
1514 noa setglobal foldcolumn=8 " Reset global and local value (without triggering autocmd)
1515 noa setlocal foldcolumn=1
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001516 let g:options = [['foldcolumn', '1', '1', '', '2', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001517 setlocal foldcolumn=2
1518 call assert_equal([], g:options)
1519 call assert_equal(g:opt[0], g:opt[1])
1520
1521 " 27c: Setting again number local (to window) option"
1522 noa setglobal foldcolumn=8 " Reset global and local value (without triggering autocmd)
1523 noa setlocal foldcolumn=1
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001524 let g:options = [['foldcolumn', '1', '1', '8', '2', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001525 set foldcolumn=2
1526 call assert_equal([], g:options)
1527 call assert_equal(g:opt[0], g:opt[1])
1528
zeertzjqb811de52021-10-21 10:50:44 +01001529 " 27d: Setting again global number local (to window) option"
Bram Moolenaard7c96872019-06-15 17:12:48 +02001530 noa set foldcolumn=8 " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001531 let g:options = [['foldcolumn', '8', '8', '8', '2', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001532 set foldcolumn=2
1533 call assert_equal([], g:options)
1534 call assert_equal(g:opt[0], g:opt[1])
1535
1536
1537 " 28a: Setting global boolean global option"
1538 noa setglobal nowrapscan " Reset global and local value (without triggering autocmd)
1539 noa setlocal wrapscan " Sets the global(!) value!
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001540 let g:options = [['wrapscan', '1', '', '1', '0', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001541 setglobal nowrapscan
1542 call assert_equal([], g:options)
1543 call assert_equal(g:opt[0], g:opt[1])
1544
1545 " 28b: Setting local boolean global option"
1546 noa setglobal nowrapscan " Reset global and local value (without triggering autocmd)
1547 noa setlocal wrapscan " Sets the global(!) value!
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001548 let g:options = [['wrapscan', '1', '1', '', '0', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001549 setlocal nowrapscan
1550 call assert_equal([], g:options)
1551 call assert_equal(g:opt[0], g:opt[1])
1552
1553 " 28c: Setting again boolean global option"
1554 noa setglobal nowrapscan " Reset global and local value (without triggering autocmd)
1555 noa setlocal wrapscan " Sets the global(!) value!
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001556 let g:options = [['wrapscan', '1', '1', '1', '0', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001557 set nowrapscan
1558 call assert_equal([], g:options)
1559 call assert_equal(g:opt[0], g:opt[1])
1560
1561 " 28d: Setting again global boolean global option"
1562 noa set nowrapscan " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001563 let g:options = [['wrapscan', '0', '0', '0', '1', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001564 set wrapscan
1565 call assert_equal([], g:options)
1566 call assert_equal(g:opt[0], g:opt[1])
1567
1568
1569 " 29a: Setting global boolean global-local (to buffer) option"
1570 noa setglobal noautoread " Reset global and local value (without triggering autocmd)
1571 noa setlocal autoread
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001572 let g:options = [['autoread', '0', '', '0', '1', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001573 setglobal autoread
1574 call assert_equal([], g:options)
1575 call assert_equal(g:opt[0], g:opt[1])
1576
1577 " 29b: Setting local boolean global-local (to buffer) option"
1578 noa setglobal noautoread " Reset global and local value (without triggering autocmd)
1579 noa setlocal autoread
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001580 let g:options = [['autoread', '1', '1', '', '0', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001581 setlocal noautoread
1582 call assert_equal([], g:options)
1583 call assert_equal(g:opt[0], g:opt[1])
1584
1585 " 29c: Setting again boolean global-local (to buffer) option"
1586 noa setglobal noautoread " Reset global and local value (without triggering autocmd)
1587 noa setlocal autoread
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001588 let g:options = [['autoread', '1', '1', '0', '1', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001589 set autoread
1590 call assert_equal([], g:options)
1591 call assert_equal(g:opt[0], g:opt[1])
1592
1593 " 29d: Setting again global boolean global-local (to buffer) option"
1594 noa set noautoread " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001595 let g:options = [['autoread', '0', '0', '0', '1', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001596 set autoread
1597 call assert_equal([], g:options)
1598 call assert_equal(g:opt[0], g:opt[1])
1599
1600
1601 " 30a: Setting global boolean local (to buffer) option"
1602 noa setglobal nocindent " Reset global and local value (without triggering autocmd)
1603 noa setlocal cindent
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001604 let g:options = [['cindent', '0', '', '0', '1', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001605 setglobal cindent
1606 call assert_equal([], g:options)
1607 call assert_equal(g:opt[0], g:opt[1])
1608
1609 " 30b: Setting local boolean local (to buffer) option"
1610 noa setglobal nocindent " Reset global and local value (without triggering autocmd)
1611 noa setlocal cindent
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001612 let g:options = [['cindent', '1', '1', '', '0', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001613 setlocal nocindent
1614 call assert_equal([], g:options)
1615 call assert_equal(g:opt[0], g:opt[1])
1616
1617 " 30c: Setting again boolean local (to buffer) option"
1618 noa setglobal nocindent " Reset global and local value (without triggering autocmd)
1619 noa setlocal cindent
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001620 let g:options = [['cindent', '1', '1', '0', '1', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001621 set cindent
1622 call assert_equal([], g:options)
1623 call assert_equal(g:opt[0], g:opt[1])
1624
1625 " 30d: Setting again global boolean local (to buffer) option"
1626 noa set nocindent " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001627 let g:options = [['cindent', '0', '0', '0', '1', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001628 set cindent
1629 call assert_equal([], g:options)
1630 call assert_equal(g:opt[0], g:opt[1])
1631
1632
1633 " 31: Setting boolean global-local (to window) option
1634 " Currently no such option exists.
1635
1636
1637 " 32a: Setting global boolean local (to window) option"
1638 noa setglobal nocursorcolumn " Reset global and local value (without triggering autocmd)
1639 noa setlocal cursorcolumn
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001640 let g:options = [['cursorcolumn', '0', '', '0', '1', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001641 setglobal cursorcolumn
1642 call assert_equal([], g:options)
1643 call assert_equal(g:opt[0], g:opt[1])
1644
1645 " 32b: Setting local boolean local (to window) option"
1646 noa setglobal nocursorcolumn " Reset global and local value (without triggering autocmd)
1647 noa setlocal cursorcolumn
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001648 let g:options = [['cursorcolumn', '1', '1', '', '0', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001649 setlocal nocursorcolumn
1650 call assert_equal([], g:options)
1651 call assert_equal(g:opt[0], g:opt[1])
1652
1653 " 32c: Setting again boolean local (to window) option"
1654 noa setglobal nocursorcolumn " Reset global and local value (without triggering autocmd)
1655 noa setlocal cursorcolumn
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001656 let g:options = [['cursorcolumn', '1', '1', '0', '1', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001657 set cursorcolumn
1658 call assert_equal([], g:options)
1659 call assert_equal(g:opt[0], g:opt[1])
1660
1661 " 32d: Setting again global boolean local (to window) option"
1662 noa set nocursorcolumn " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001663 let g:options = [['cursorcolumn', '0', '0', '0', '1', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001664 set cursorcolumn
1665 call assert_equal([], g:options)
1666 call assert_equal(g:opt[0], g:opt[1])
1667
1668
Bram Moolenaar1bc353b2019-09-01 14:45:28 +02001669 " 33: Test autocommands when an option value is converted internally.
Bram Moolenaard7c96872019-06-15 17:12:48 +02001670 noa set backspace=1 " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001671 let g:options = [['backspace', 'indent,eol', 'indent,eol', 'indent,eol', '2', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001672 set backspace=2
1673 call assert_equal([], g:options)
1674 call assert_equal(g:opt[0], g:opt[1])
1675
1676
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001677 " Cleanup
1678 au! OptionSet
Bram Moolenaar0331faf2019-06-15 18:40:37 +02001679 " set tags&
Bram Moolenaard7c96872019-06-15 17:12:48 +02001680 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 +02001681 exe printf(":set %s&vim", opt)
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001682 endfor
1683 call test_override('starting', 0)
1684 delfunc! AutoCommandOptionSet
1685endfunc
1686
1687func Test_OptionSet_diffmode()
1688 call test_override('starting', 1)
Bram Moolenaar26d98212019-01-27 22:32:55 +01001689 " 18: Changing an option when entering diff mode
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001690 new
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001691 au OptionSet diff :let &l:cul = v:option_new
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001692
1693 call setline(1, ['buffer 1', 'line2', 'line3', 'line4'])
1694 call assert_equal(0, &l:cul)
1695 diffthis
1696 call assert_equal(1, &l:cul)
1697
1698 vnew
1699 call setline(1, ['buffer 2', 'line 2', 'line 3', 'line4'])
1700 call assert_equal(0, &l:cul)
1701 diffthis
1702 call assert_equal(1, &l:cul)
1703
1704 diffoff
1705 call assert_equal(0, &l:cul)
1706 call assert_equal(1, getwinvar(2, '&l:cul'))
1707 bw!
1708
1709 call assert_equal(1, &l:cul)
1710 diffoff!
1711 call assert_equal(0, &l:cul)
1712 call assert_equal(0, getwinvar(1, '&l:cul'))
1713 bw!
1714
1715 " Cleanup
1716 au! OptionSet
1717 call test_override('starting', 0)
1718endfunc
1719
1720func Test_OptionSet_diffmode_close()
1721 call test_override('starting', 1)
1722 " 19: Try to close the current window when entering diff mode
1723 " should not segfault
1724 new
1725 au OptionSet diff close
1726
1727 call setline(1, ['buffer 1', 'line2', 'line3', 'line4'])
Bram Moolenaare2e40752020-09-04 21:18:46 +02001728 call assert_fails(':diffthis', 'E788:')
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001729 call assert_equal(1, &diff)
1730 vnew
1731 call setline(1, ['buffer 2', 'line 2', 'line 3', 'line4'])
Bram Moolenaare2e40752020-09-04 21:18:46 +02001732 call assert_fails(':diffthis', 'E788:')
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001733 call assert_equal(1, &diff)
Bram Moolenaara9aa86f2019-11-10 21:25:45 +01001734 set diffopt-=closeoff
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001735 bw!
Bram Moolenaare2e40752020-09-04 21:18:46 +02001736 call assert_fails(':diffoff!', 'E788:')
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001737 bw!
1738
1739 " Cleanup
1740 au! OptionSet
1741 call test_override('starting', 0)
1742 "delfunc! AutoCommandOptionSet
1743endfunc
Bram Moolenaar4a137b42017-08-04 22:37:11 +02001744
1745" Test for Bufleave autocommand that deletes the buffer we are about to edit.
1746func Test_BufleaveWithDelete()
Bram Moolenaare7cda972022-08-29 11:02:59 +01001747 new | edit XbufLeave1
Bram Moolenaar4a137b42017-08-04 22:37:11 +02001748
1749 augroup test_bufleavewithdelete
1750 autocmd!
Bram Moolenaare7cda972022-08-29 11:02:59 +01001751 autocmd BufLeave XbufLeave1 bwipe XbufLeave2
Bram Moolenaar4a137b42017-08-04 22:37:11 +02001752 augroup END
1753
Bram Moolenaare7cda972022-08-29 11:02:59 +01001754 call assert_fails('edit XbufLeave2', 'E143:')
1755 call assert_equal('XbufLeave1', bufname('%'))
Bram Moolenaar4a137b42017-08-04 22:37:11 +02001756
Bram Moolenaare7cda972022-08-29 11:02:59 +01001757 autocmd! test_bufleavewithdelete BufLeave XbufLeave1
Bram Moolenaar4a137b42017-08-04 22:37:11 +02001758 augroup! test_bufleavewithdelete
1759
1760 new
Bram Moolenaare7cda972022-08-29 11:02:59 +01001761 bwipe! XbufLeave1
Bram Moolenaar4a137b42017-08-04 22:37:11 +02001762endfunc
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +02001763
1764" Test for autocommand that changes the buffer list, when doing ":ball".
1765func Test_Acmd_BufAll()
1766 enew!
1767 %bwipe!
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01001768 call writefile(['Test file Xxx1'], 'Xxx1', 'D')
1769 call writefile(['Test file Xxx2'], 'Xxx2', 'D')
1770 call writefile(['Test file Xxx3'], 'Xxx3', 'D')
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +02001771
1772 " Add three files to the buffer list
1773 split Xxx1
1774 close
1775 split Xxx2
1776 close
1777 split Xxx3
1778 close
1779
1780 " Wipe the buffer when the buffer is opened
1781 au BufReadPost Xxx2 bwipe
1782
1783 call append(0, 'Test file Xxx4')
1784 ball
1785
1786 call assert_equal(2, winnr('$'))
1787 call assert_equal('Xxx1', bufname(winbufnr(winnr('$'))))
1788 wincmd t
1789
1790 au! BufReadPost
1791 %bwipe!
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +02001792 enew! | only
1793endfunc
1794
1795" Test for autocommand that changes current buffer on BufEnter event.
1796" Check if modelines are interpreted for the correct buffer.
1797func Test_Acmd_BufEnter()
1798 %bwipe!
1799 call writefile(['start of test file Xxx1',
1800 \ "\<Tab>this is a test",
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01001801 \ 'end of test file Xxx1'], 'Xxx1', 'D')
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +02001802 call writefile(['start of test file Xxx2',
1803 \ 'vim: set noai :',
1804 \ "\<Tab>this is a test",
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01001805 \ 'end of test file Xxx2'], 'Xxx2', 'D')
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +02001806
1807 au BufEnter Xxx2 brew
1808 set ai modeline modelines=3
1809 edit Xxx1
1810 " edit Xxx2, autocmd will do :brew
1811 edit Xxx2
1812 exe "normal G?this is a\<CR>"
1813 " Append text with autoindent to this file
1814 normal othis should be auto-indented
1815 call assert_equal("\<Tab>this should be auto-indented", getline('.'))
1816 call assert_equal(3, line('.'))
1817 " Remove autocmd and edit Xxx2 again
1818 au! BufEnter Xxx2
1819 buf! Xxx2
1820 exe "normal G?this is a\<CR>"
1821 " append text without autoindent to Xxx
1822 normal othis should be in column 1
1823 call assert_equal("this should be in column 1", getline('.'))
1824 call assert_equal(4, line('.'))
1825
1826 %bwipe!
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +02001827 set ai&vim modeline&vim modelines&vim
1828endfunc
1829
1830" Test for issue #57
1831" do not move cursor on <c-o> when autoindent is set
1832func Test_ai_CTRL_O()
1833 enew!
1834 set ai
1835 let save_fo = &fo
1836 set fo+=r
1837 exe "normal o# abcdef\<Esc>2hi\<CR>\<C-O>d0\<Esc>"
1838 exe "normal o# abcdef\<Esc>2hi\<C-O>d0\<Esc>"
1839 call assert_equal(['# abc', 'def', 'def'], getline(2, 4))
1840
1841 set ai&vim
1842 let &fo = save_fo
1843 enew!
1844endfunc
1845
1846" Test for autocommand that deletes the current buffer on BufLeave event.
1847" Also test deleting the last buffer, should give a new, empty buffer.
1848func Test_BufLeave_Wipe()
1849 %bwipe!
1850 let content = ['start of test file Xxx',
1851 \ 'this is a test',
1852 \ 'end of test file Xxx']
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01001853 call writefile(content, 'Xxx1', 'D')
1854 call writefile(content, 'Xxx2', 'D')
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +02001855
1856 au BufLeave Xxx2 bwipe
1857 edit Xxx1
1858 split Xxx2
1859 " delete buffer Xxx2, we should be back to Xxx1
1860 bwipe
1861 call assert_equal('Xxx1', bufname('%'))
1862 call assert_equal(1, winnr('$'))
1863
1864 " Create an alternate buffer
1865 %write! test.out
1866 call assert_equal('test.out', bufname('#'))
1867 " delete alternate buffer
1868 bwipe test.out
1869 call assert_equal('Xxx1', bufname('%'))
1870 call assert_equal('', bufname('#'))
1871
1872 au BufLeave Xxx1 bwipe
1873 " delete current buffer, get an empty one
1874 bwipe!
1875 call assert_equal(1, line('$'))
1876 call assert_equal('', bufname('%'))
Bram Moolenaarb2c87502017-10-14 21:15:58 +02001877 let g:bufinfo = getbufinfo()
1878 call assert_equal(1, len(g:bufinfo))
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +02001879
Bram Moolenaar53f0c962017-10-22 14:23:59 +02001880 call delete('test.out')
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +02001881 %bwipe
1882 au! BufLeave
Bram Moolenaarb2c87502017-10-14 21:15:58 +02001883
1884 " check that bufinfo doesn't contain a pointer to freed memory
1885 call test_garbagecollect_now()
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +02001886endfunc
Bram Moolenaar87ffb5c2017-10-19 12:37:42 +02001887
1888func Test_QuitPre()
1889 edit Xfoo
1890 let winid = win_getid(winnr())
1891 split Xbar
1892 au! QuitPre * let g:afile = expand('<afile>')
1893 " Close the other window, <afile> should be correct.
1894 exe win_id2win(winid) . 'q'
1895 call assert_equal('Xfoo', g:afile)
LemonBoy66e13ae2022-04-21 22:52:11 +01001896
Bram Moolenaar87ffb5c2017-10-19 12:37:42 +02001897 unlet g:afile
1898 bwipe Xfoo
1899 bwipe Xbar
1900endfunc
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02001901
1902func Test_Cmdline()
Bram Moolenaar153b7042018-01-31 15:48:32 +01001903 au! CmdlineChanged : let g:text = getcmdline()
1904 let g:text = 0
1905 call feedkeys(":echom 'hello'\<CR>", 'xt')
1906 call assert_equal("echom 'hello'", g:text)
1907 au! CmdlineChanged
1908
1909 au! CmdlineChanged : let g:entered = expand('<afile>')
1910 let g:entered = 0
1911 call feedkeys(":echom 'hello'\<CR>", 'xt')
1912 call assert_equal(':', g:entered)
1913 au! CmdlineChanged
1914
zeertzjq412e0e42023-02-11 10:34:07 +00001915 autocmd CmdlineChanged : let g:log += [getcmdline()]
1916
Bram Moolenaarbb393d82022-12-09 12:21:50 +00001917 let g:log = []
1918 cnoremap <F1> <Cmd>call setcmdline('ls')<CR>
Bram Moolenaarbb393d82022-12-09 12:21:50 +00001919 call feedkeys(":\<F1>", 'xt')
1920 call assert_equal(['ls'], g:log)
Bram Moolenaarbb393d82022-12-09 12:21:50 +00001921 cunmap <F1>
1922
zeertzjqaf9e28a2023-02-06 20:58:09 +00001923 let g:log = []
zeertzjqaf9e28a2023-02-06 20:58:09 +00001924 call feedkeys(":sign \<Tab>\<Tab>\<C-N>\<C-P>\<S-Tab>\<S-Tab>\<Esc>", 'xt')
1925 call assert_equal([
1926 \ 's',
1927 \ 'si',
1928 \ 'sig',
1929 \ 'sign',
1930 \ 'sign ',
1931 \ 'sign define',
1932 \ 'sign jump',
1933 \ 'sign list',
1934 \ 'sign jump',
1935 \ 'sign define',
1936 \ 'sign ',
1937 \ ], g:log)
1938 let g:log = []
1939 set wildmenu wildoptions+=pum
1940 call feedkeys(":sign \<S-Tab>\<PageUp>\<kPageUp>\<kPageDown>\<PageDown>\<Esc>", 'xt')
1941 call assert_equal([
1942 \ 's',
1943 \ 'si',
1944 \ 'sig',
1945 \ 'sign',
1946 \ 'sign ',
1947 \ 'sign unplace',
1948 \ 'sign jump',
1949 \ 'sign define',
1950 \ 'sign undefine',
1951 \ 'sign unplace',
1952 \ ], g:log)
1953 set wildmenu& wildoptions&
zeertzjq412e0e42023-02-11 10:34:07 +00001954
1955 let g:log = []
1956 let @r = 'abc'
1957 call feedkeys(":0\<C-R>r1\<C-R>\<C-O>r2\<C-R>\<C-R>r3\<Esc>", 'xt')
1958 call assert_equal([
1959 \ '0',
1960 \ '0a',
1961 \ '0ab',
1962 \ '0abc',
1963 \ '0abc1',
1964 \ '0abc1abc',
1965 \ '0abc1abc2',
1966 \ '0abc1abc2abc',
1967 \ '0abc1abc2abc3',
1968 \ ], g:log)
1969
zeertzjqaf9e28a2023-02-06 20:58:09 +00001970 unlet g:log
1971 au! CmdlineChanged
1972
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02001973 au! CmdlineEnter : let g:entered = expand('<afile>')
1974 au! CmdlineLeave : let g:left = expand('<afile>')
1975 let g:entered = 0
1976 let g:left = 0
1977 call feedkeys(":echo 'hello'\<CR>", 'xt')
1978 call assert_equal(':', g:entered)
1979 call assert_equal(':', g:left)
1980 au! CmdlineEnter
1981 au! CmdlineLeave
1982
Bram Moolenaara4baf5b2018-04-22 13:27:44 +02001983 let save_shellslash = &shellslash
1984 set noshellslash
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02001985 au! CmdlineEnter / let g:entered = expand('<afile>')
1986 au! CmdlineLeave / let g:left = expand('<afile>')
1987 let g:entered = 0
1988 let g:left = 0
Bram Moolenaar53f0c962017-10-22 14:23:59 +02001989 new
1990 call setline(1, 'hello')
1991 call feedkeys("/hello\<CR>", 'xt')
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02001992 call assert_equal('/', g:entered)
1993 call assert_equal('/', g:left)
Bram Moolenaar53f0c962017-10-22 14:23:59 +02001994 bwipe!
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02001995 au! CmdlineEnter
1996 au! CmdlineLeave
Bram Moolenaara4baf5b2018-04-22 13:27:44 +02001997 let &shellslash = save_shellslash
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02001998endfunc
Bram Moolenaar53f0c962017-10-22 14:23:59 +02001999
2000" Test for BufWritePre autocommand that deletes or unloads the buffer.
2001func Test_BufWritePre()
2002 %bwipe
2003 au BufWritePre Xxx1 bunload
2004 au BufWritePre Xxx2 bwipe
2005
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01002006 call writefile(['start of Xxx1', 'test', 'end of Xxx1'], 'Xxx1', 'D')
2007 call writefile(['start of Xxx2', 'test', 'end of Xxx2'], 'Xxx2', 'D')
Bram Moolenaar53f0c962017-10-22 14:23:59 +02002008
2009 edit Xtest
2010 e! Xxx2
2011 bdel Xtest
2012 e Xxx1
2013 " write it, will unload it and give an error msg
Bram Moolenaare2e40752020-09-04 21:18:46 +02002014 call assert_fails('w', 'E203:')
Bram Moolenaar53f0c962017-10-22 14:23:59 +02002015 call assert_equal('Xxx2', bufname('%'))
2016 edit Xtest
2017 e! Xxx2
2018 bwipe Xtest
2019 " write it, will delete the buffer and give an error msg
Bram Moolenaare2e40752020-09-04 21:18:46 +02002020 call assert_fails('w', 'E203:')
Bram Moolenaar53f0c962017-10-22 14:23:59 +02002021 call assert_equal('Xxx1', bufname('%'))
2022 au! BufWritePre
Bram Moolenaar53f0c962017-10-22 14:23:59 +02002023endfunc
2024
2025" Test for BufUnload autocommand that unloads all the other buffers
2026func Test_bufunload_all()
Bram Moolenaarf08b0eb2021-10-16 13:00:14 +01002027 let g:test_is_flaky = 1
Christian Brabandtee17b6f2023-09-09 11:23:50 +02002028 call writefile(['Test file Xxx1'], 'Xxx1', 'D')
2029 call writefile(['Test file Xxx2'], 'Xxx2', 'D')
Bram Moolenaar53f0c962017-10-22 14:23:59 +02002030
Bram Moolenaarc79745a2019-05-20 22:12:34 +02002031 let content =<< trim [CODE]
2032 func UnloadAllBufs()
2033 let i = 1
2034 while i <= bufnr('$')
2035 if i != bufnr('%') && bufloaded(i)
2036 exe i . 'bunload'
2037 endif
2038 let i += 1
2039 endwhile
2040 endfunc
2041 au BufUnload * call UnloadAllBufs()
2042 au VimLeave * call writefile(['Test Finished'], 'Xout')
2043 edit Xxx1
2044 split Xxx2
2045 q
2046 [CODE]
2047
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01002048 call writefile(content, 'Xbunloadtest', 'D')
Bram Moolenaar53f0c962017-10-22 14:23:59 +02002049
2050 call delete('Xout')
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01002051 call system(GetVimCommandClean() .. ' -N --not-a-term -S Xbunloadtest')
Bram Moolenaar53f0c962017-10-22 14:23:59 +02002052 call assert_true(filereadable('Xout'))
2053
Bram Moolenaar53f0c962017-10-22 14:23:59 +02002054 call delete('Xout')
2055endfunc
2056
2057" Some tests for buffer-local autocommands
2058func Test_buflocal_autocmd()
2059 let g:bname = ''
2060 edit xx
2061 au BufLeave <buffer> let g:bname = expand("%")
2062 " here, autocommand for xx should trigger.
2063 " but autocommand shall not apply to buffer named <buffer>.
2064 edit somefile
2065 call assert_equal('xx', g:bname)
2066 let g:bname = ''
2067 " here, autocommand shall be auto-deleted
2068 bwipe xx
2069 " autocmd should not trigger
2070 edit xx
2071 call assert_equal('', g:bname)
2072 " autocmd should not trigger
2073 edit somefile
2074 call assert_equal('', g:bname)
2075 enew
2076 unlet g:bname
2077endfunc
Bram Moolenaar430dc5d2017-11-02 21:04:47 +01002078
2079" Test for "*Cmd" autocommands
2080func Test_Cmd_Autocmds()
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01002081 call writefile(['start of Xxx', "\tabc2", 'end of Xxx'], 'Xxx', 'D')
Bram Moolenaar430dc5d2017-11-02 21:04:47 +01002082
2083 enew!
2084 au BufReadCmd XtestA 0r Xxx|$del
2085 edit XtestA " will read text of Xxd instead
2086 call assert_equal('start of Xxx', getline(1))
2087
2088 au BufWriteCmd XtestA call append(line("$"), "write")
2089 write " will append a line to the file
2090 call assert_equal('write', getline('$'))
Bram Moolenaare2e40752020-09-04 21:18:46 +02002091 call assert_fails('read XtestA', 'E484:') " should not read anything
Bram Moolenaar430dc5d2017-11-02 21:04:47 +01002092 call assert_equal('write', getline(4))
2093
2094 " now we have:
2095 " 1 start of Xxx
2096 " 2 abc2
2097 " 3 end of Xxx
2098 " 4 write
2099
2100 au FileReadCmd XtestB '[r Xxx
2101 2r XtestB " will read Xxx below line 2 instead
2102 call assert_equal('start of Xxx', getline(3))
2103
2104 " now we have:
2105 " 1 start of Xxx
2106 " 2 abc2
2107 " 3 start of Xxx
2108 " 4 abc2
2109 " 5 end of Xxx
2110 " 6 end of Xxx
2111 " 7 write
2112
2113 au FileWriteCmd XtestC '[,']copy $
2114 normal 4GA1
2115 4,5w XtestC " will copy lines 4 and 5 to the end
2116 call assert_equal("\tabc21", getline(8))
Bram Moolenaare2e40752020-09-04 21:18:46 +02002117 call assert_fails('r XtestC', 'E484:') " should not read anything
Bram Moolenaar430dc5d2017-11-02 21:04:47 +01002118 call assert_equal("end of Xxx", getline(9))
2119
2120 " now we have:
2121 " 1 start of Xxx
2122 " 2 abc2
2123 " 3 start of Xxx
2124 " 4 abc21
2125 " 5 end of Xxx
2126 " 6 end of Xxx
2127 " 7 write
2128 " 8 abc21
2129 " 9 end of Xxx
2130
2131 let g:lines = []
2132 au FileAppendCmd XtestD call extend(g:lines, getline(line("'["), line("']")))
2133 w >>XtestD " will add lines to 'lines'
2134 call assert_equal(9, len(g:lines))
Bram Moolenaare2e40752020-09-04 21:18:46 +02002135 call assert_fails('$r XtestD', 'E484:') " should not read anything
Bram Moolenaar430dc5d2017-11-02 21:04:47 +01002136 call assert_equal(9, line('$'))
2137 call assert_equal('end of Xxx', getline('$'))
2138
2139 au BufReadCmd XtestE 0r Xxx|$del
2140 sp XtestE " split window with test.out
2141 call assert_equal('end of Xxx', getline(3))
2142
2143 let g:lines = []
2144 exe "normal 2Goasdf\<Esc>\<C-W>\<C-W>"
2145 au BufWriteCmd XtestE call extend(g:lines, getline(0, '$'))
2146 wall " will write other window to 'lines'
2147 call assert_equal(4, len(g:lines), g:lines)
2148 call assert_equal('asdf', g:lines[2])
2149
2150 au! BufReadCmd
2151 au! BufWriteCmd
2152 au! FileReadCmd
2153 au! FileWriteCmd
2154 au! FileAppendCmd
2155 %bwipe!
Bram Moolenaar430dc5d2017-11-02 21:04:47 +01002156 enew!
2157endfunc
Bram Moolenaaraace2152017-11-05 16:23:10 +01002158
Bram Moolenaar0fff4412020-03-29 16:06:29 +02002159func s:ReadFile()
2160 setl noswapfile nomodified
2161 let filename = resolve(expand("<afile>:p"))
2162 execute 'read' fnameescape(filename)
2163 1d_
2164 exe 'file' fnameescape(filename)
2165 setl buftype=acwrite
2166endfunc
2167
2168func s:WriteFile()
2169 let filename = resolve(expand("<afile>:p"))
2170 setl buftype=
2171 noautocmd execute 'write' fnameescape(filename)
2172 setl buftype=acwrite
2173 setl nomodified
2174endfunc
2175
2176func Test_BufReadCmd()
2177 autocmd BufReadCmd *.test call s:ReadFile()
2178 autocmd BufWriteCmd *.test call s:WriteFile()
2179
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01002180 call writefile(['one', 'two', 'three'], 'Xcmd.test', 'D')
Bram Moolenaar0fff4412020-03-29 16:06:29 +02002181 edit Xcmd.test
2182 call assert_match('Xcmd.test" line 1 of 3', execute('file'))
2183 normal! Gofour
2184 write
2185 call assert_equal(['one', 'two', 'three', 'four'], readfile('Xcmd.test'))
2186
2187 bwipe!
Bram Moolenaar0fff4412020-03-29 16:06:29 +02002188 au! BufReadCmd
2189 au! BufWriteCmd
2190endfunc
2191
zeertzjq9c8f9462022-08-30 18:17:15 +01002192func Test_BufWriteCmd()
2193 autocmd BufWriteCmd Xbufwritecmd let g:written = 1
2194 new
2195 file Xbufwritecmd
2196 set buftype=acwrite
Bram Moolenaar6f14da12022-09-07 21:30:44 +01002197 call mkdir('Xbufwritecmd', 'D')
zeertzjq9c8f9462022-08-30 18:17:15 +01002198 write
2199 " BufWriteCmd should be triggered even if a directory has the same name
2200 call assert_equal(1, g:written)
zeertzjq9c8f9462022-08-30 18:17:15 +01002201 unlet g:written
2202 au! BufWriteCmd
2203 bwipe!
2204endfunc
2205
Bram Moolenaaraace2152017-11-05 16:23:10 +01002206func SetChangeMarks(start, end)
Bram Moolenaar97c69432021-01-15 16:45:21 +01002207 exe a:start .. 'mark ['
2208 exe a:end .. 'mark ]'
Bram Moolenaaraace2152017-11-05 16:23:10 +01002209endfunc
2210
2211" Verify the effects of autocmds on '[ and ']
2212func Test_change_mark_in_autocmds()
2213 edit! Xtest
Bram Moolenaar97c69432021-01-15 16:45:21 +01002214 call feedkeys("ia\<CR>b\<CR>c\<CR>d\<C-g>u\<Esc>", 'xtn')
Bram Moolenaaraace2152017-11-05 16:23:10 +01002215
2216 call SetChangeMarks(2, 3)
2217 write
2218 call assert_equal([1, 4], [line("'["), line("']")])
2219
2220 call SetChangeMarks(2, 3)
2221 au BufWritePre * call assert_equal([1, 4], [line("'["), line("']")])
2222 write
2223 au! BufWritePre
2224
Bram Moolenaar14ddd222020-08-05 12:02:40 +02002225 if has('unix')
Bram Moolenaaraace2152017-11-05 16:23:10 +01002226 write XtestFilter
2227 write >> XtestFilter
2228
2229 call SetChangeMarks(2, 3)
2230 " Marks are set to the entire range of the write
2231 au FilterWritePre * call assert_equal([1, 4], [line("'["), line("']")])
2232 " '[ is adjusted to just before the line that will receive the filtered
2233 " data
2234 au FilterReadPre * call assert_equal([4, 4], [line("'["), line("']")])
2235 " The filtered data is read into the buffer, and the source lines are
2236 " still present, so the range is after the source lines
2237 au FilterReadPost * call assert_equal([5, 12], [line("'["), line("']")])
2238 %!cat XtestFilter
2239 " After the filtered data is read, the original lines are deleted
2240 call assert_equal([1, 8], [line("'["), line("']")])
2241 au! FilterWritePre,FilterReadPre,FilterReadPost
2242 undo
2243
2244 call SetChangeMarks(1, 4)
2245 au FilterWritePre * call assert_equal([2, 3], [line("'["), line("']")])
2246 au FilterReadPre * call assert_equal([3, 3], [line("'["), line("']")])
2247 au FilterReadPost * call assert_equal([4, 11], [line("'["), line("']")])
2248 2,3!cat XtestFilter
2249 call assert_equal([2, 9], [line("'["), line("']")])
2250 au! FilterWritePre,FilterReadPre,FilterReadPost
2251 undo
2252
2253 call delete('XtestFilter')
2254 endif
2255
2256 call SetChangeMarks(1, 4)
2257 au FileWritePre * call assert_equal([2, 3], [line("'["), line("']")])
2258 2,3write Xtest2
2259 au! FileWritePre
2260
2261 call SetChangeMarks(2, 3)
2262 au FileAppendPre * call assert_equal([1, 4], [line("'["), line("']")])
2263 write >> Xtest2
2264 au! FileAppendPre
2265
2266 call SetChangeMarks(1, 4)
2267 au FileAppendPre * call assert_equal([2, 3], [line("'["), line("']")])
2268 2,3write >> Xtest2
2269 au! FileAppendPre
2270
2271 call SetChangeMarks(1, 1)
2272 au FileReadPre * call assert_equal([3, 1], [line("'["), line("']")])
2273 au FileReadPost * call assert_equal([4, 11], [line("'["), line("']")])
2274 3read Xtest2
2275 au! FileReadPre,FileReadPost
2276 undo
2277
2278 call SetChangeMarks(4, 4)
2279 " When the line is 0, it's adjusted to 1
2280 au FileReadPre * call assert_equal([1, 4], [line("'["), line("']")])
2281 au FileReadPost * call assert_equal([1, 8], [line("'["), line("']")])
2282 0read Xtest2
2283 au! FileReadPre,FileReadPost
2284 undo
2285
2286 call SetChangeMarks(4, 4)
2287 " When the line is 0, it's adjusted to 1
2288 au FileReadPre * call assert_equal([1, 4], [line("'["), line("']")])
2289 au FileReadPost * call assert_equal([2, 9], [line("'["), line("']")])
2290 1read Xtest2
2291 au! FileReadPre,FileReadPost
2292 undo
2293
2294 bwipe!
2295 call delete('Xtest')
2296 call delete('Xtest2')
2297endfunc
2298
2299func Test_Filter_noshelltemp()
Bram Moolenaaraeb313f2020-11-27 19:13:28 +01002300 CheckExecutable cat
Bram Moolenaaraace2152017-11-05 16:23:10 +01002301
2302 enew!
2303 call setline(1, ['a', 'b', 'c', 'd'])
2304
2305 let shelltemp = &shelltemp
2306 set shelltemp
2307
2308 let g:filter_au = 0
2309 au FilterWritePre * let g:filter_au += 1
2310 au FilterReadPre * let g:filter_au += 1
2311 au FilterReadPost * let g:filter_au += 1
2312 %!cat
2313 call assert_equal(3, g:filter_au)
2314
2315 if has('filterpipe')
2316 set noshelltemp
2317
2318 let g:filter_au = 0
2319 au FilterWritePre * let g:filter_au += 1
2320 au FilterReadPre * let g:filter_au += 1
2321 au FilterReadPost * let g:filter_au += 1
2322 %!cat
2323 call assert_equal(0, g:filter_au)
2324 endif
2325
2326 au! FilterWritePre,FilterReadPre,FilterReadPost
2327 let &shelltemp = shelltemp
2328 bwipe!
2329endfunc
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01002330
2331func Test_TextYankPost()
2332 enew!
2333 call setline(1, ['foo'])
2334
2335 let g:event = []
2336 au TextYankPost * let g:event = copy(v:event)
2337
2338 call assert_equal({}, v:event)
2339 call assert_fails('let v:event = {}', 'E46:')
2340 call assert_fails('let v:event.mykey = 0', 'E742:')
2341
2342 norm "ayiw
2343 call assert_equal(
Bram Moolenaara016eeb2022-04-09 11:37:38 +01002344 \ #{regcontents: ['foo'], regname: 'a', operator: 'y',
2345 \ regtype: 'v', visual: v:false, inclusive: v:true},
2346 \ g:event)
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01002347 norm y_
2348 call assert_equal(
Bram Moolenaara016eeb2022-04-09 11:37:38 +01002349 \ #{regcontents: ['foo'], regname: '', operator: 'y', regtype: 'V',
2350 \ visual: v:false, inclusive: v:false},
2351 \ g:event)
Bram Moolenaar37d16732020-06-12 22:09:01 +02002352 norm Vy
2353 call assert_equal(
Bram Moolenaara016eeb2022-04-09 11:37:38 +01002354 \ #{regcontents: ['foo'], regname: '', operator: 'y', regtype: 'V',
2355 \ visual: v:true, inclusive: v:true},
2356 \ g:event)
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01002357 call feedkeys("\<C-V>y", 'x')
2358 call assert_equal(
Bram Moolenaara016eeb2022-04-09 11:37:38 +01002359 \ #{regcontents: ['f'], regname: '', operator: 'y', regtype: "\x161",
2360 \ visual: v:true, inclusive: v:true},
2361 \ g:event)
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01002362 norm "xciwbar
2363 call assert_equal(
Bram Moolenaara016eeb2022-04-09 11:37:38 +01002364 \ #{regcontents: ['foo'], regname: 'x', operator: 'c', regtype: 'v',
2365 \ visual: v:false, inclusive: v:true},
2366 \ g:event)
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01002367 norm "bdiw
2368 call assert_equal(
Bram Moolenaara016eeb2022-04-09 11:37:38 +01002369 \ #{regcontents: ['bar'], regname: 'b', operator: 'd', regtype: 'v',
2370 \ visual: v:false, inclusive: v:true},
2371 \ g:event)
2372
2373 call setline(1, 'foobar')
2374 " exclusive motion
2375 norm $"ay0
2376 call assert_equal(
2377 \ #{regcontents: ['fooba'], regname: 'a', operator: 'y', regtype: 'v',
2378 \ visual: v:false, inclusive: v:false},
2379 \ g:event)
2380 " inclusive motion
2381 norm 0"ay$
2382 call assert_equal(
2383 \ #{regcontents: ['foobar'], regname: 'a', operator: 'y', regtype: 'v',
2384 \ visual: v:false, inclusive: v:true},
2385 \ g:event)
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01002386
2387 call assert_equal({}, v:event)
2388
Bram Moolenaarfccbf062020-11-26 20:34:00 +01002389 if has('clipboard_working') && !has('gui_running')
2390 " Test that when the visual selection is automatically copied to clipboard
2391 " register a TextYankPost is emitted
2392 call setline(1, ['foobar'])
2393
2394 let @* = ''
2395 set clipboard=autoselect
2396 exe "norm! ggviw\<Esc>"
2397 call assert_equal(
Bram Moolenaara016eeb2022-04-09 11:37:38 +01002398 \ #{regcontents: ['foobar'], regname: '*', operator: 'y',
2399 \ regtype: 'v', visual: v:true, inclusive: v:false},
2400 \ g:event)
Bram Moolenaarfccbf062020-11-26 20:34:00 +01002401
2402 let @+ = ''
2403 set clipboard=autoselectplus
2404 exe "norm! ggviw\<Esc>"
2405 call assert_equal(
Bram Moolenaara016eeb2022-04-09 11:37:38 +01002406 \ #{regcontents: ['foobar'], regname: '+', operator: 'y',
2407 \ regtype: 'v', visual: v:true, inclusive: v:false},
2408 \ g:event)
Bram Moolenaarfccbf062020-11-26 20:34:00 +01002409
2410 set clipboard&vim
2411 endif
2412
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01002413 au! TextYankPost
2414 unlet g:event
2415 bwipe!
2416endfunc
Bram Moolenaar9bca8052017-12-18 12:37:55 +01002417
Bram Moolenaar9a046fd2021-01-28 13:47:59 +01002418func Test_autocommand_all_events()
2419 call assert_fails('au * * bwipe', 'E1155:')
2420 call assert_fails('au * x bwipe', 'E1155:')
Bram Moolenaarb6db1462021-12-24 19:24:47 +00002421 call assert_fails('au! * x bwipe', 'E1155:')
Bram Moolenaar4fb921e2017-12-18 15:33:00 +01002422endfunc
Bram Moolenaarb7407d32018-02-03 17:36:27 +01002423
Bram Moolenaarf6246f52022-02-11 16:30:12 +00002424func Test_autocmd_user()
2425 au User MyEvent let s:res = [expand("<afile>"), expand("<amatch>")]
2426 doautocmd User MyEvent
2427 call assert_equal(['MyEvent', 'MyEvent'], s:res)
2428 au! User
2429 unlet s:res
2430endfunc
2431
Bram Moolenaar3b014be2022-11-13 17:53:46 +00002432func Test_autocmd_user_clear_group()
2433 CheckRunVimInTerminal
2434
2435 let lines =<< trim END
2436 autocmd! User
2437 for i in range(1, 999)
2438 exe 'autocmd User ' .. 'Foo' .. i .. ' bar'
2439 endfor
2440 au CmdlineLeave : call timer_start(0, {-> execute('autocmd! User')})
2441 END
2442 call writefile(lines, 'XautoUser', 'D')
2443 let buf = RunVimInTerminal('-S XautoUser', {'rows': 10})
2444
2445 " this was using freed memory
2446 call term_sendkeys(buf, ":autocmd User\<CR>")
2447 call TermWait(buf, 50)
2448 call term_sendkeys(buf, "G")
2449
2450 call StopVimInTerminal(buf)
2451endfunc
2452
Bram Moolenaaref2c3252022-11-25 16:31:51 +00002453func Test_autocmd_CmdlineLeave_unlet()
2454 CheckRunVimInTerminal
2455
2456 let lines =<< trim END
2457 for i in range(1, 999)
2458 exe 'let g:var' .. i '=' i
2459 endfor
2460 au CmdlineLeave : call timer_start(0, {-> execute('unlet g:var990')})
2461 END
2462 call writefile(lines, 'XleaveUnlet', 'D')
2463 let buf = RunVimInTerminal('-S XleaveUnlet', {'rows': 10})
2464
2465 " this was using freed memory
2466 call term_sendkeys(buf, ":let g:\<CR>")
2467 call TermWait(buf, 50)
2468 call term_sendkeys(buf, "G")
2469 call TermWait(buf, 50)
2470 call term_sendkeys(buf, "\<CR>") " for the hit-enter prompt
2471
2472 call StopVimInTerminal(buf)
2473endfunc
2474
Bram Moolenaarb7407d32018-02-03 17:36:27 +01002475function s:Before_test_dirchanged()
2476 augroup test_dirchanged
2477 autocmd!
2478 augroup END
2479 let s:li = []
2480 let s:dir_this = getcwd()
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02002481 let s:dir_foo = s:dir_this . '/Xfoo'
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01002482 call mkdir(s:dir_foo)
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02002483 let s:dir_bar = s:dir_this . '/Xbar'
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01002484 call mkdir(s:dir_bar)
Bram Moolenaarb7407d32018-02-03 17:36:27 +01002485endfunc
2486
2487function s:After_test_dirchanged()
Bram Moolenaar3503d7c2019-11-09 20:10:17 +01002488 call chdir(s:dir_this)
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01002489 call delete(s:dir_foo, 'd')
2490 call delete(s:dir_bar, 'd')
Bram Moolenaarb7407d32018-02-03 17:36:27 +01002491 augroup test_dirchanged
2492 autocmd!
2493 augroup END
2494endfunc
2495
2496function Test_dirchanged_global()
2497 call s:Before_test_dirchanged()
Bram Moolenaarf6246f52022-02-11 16:30:12 +00002498 autocmd test_dirchanged DirChangedPre global call add(s:li, expand("<amatch>") .. " pre cd " .. v:event.directory)
Bram Moolenaarb7407d32018-02-03 17:36:27 +01002499 autocmd test_dirchanged DirChanged global call add(s:li, "cd:")
2500 autocmd test_dirchanged DirChanged global call add(s:li, expand("<afile>"))
Bram Moolenaar3503d7c2019-11-09 20:10:17 +01002501 call chdir(s:dir_foo)
Bram Moolenaarf6246f52022-02-11 16:30:12 +00002502 let expected = ["global pre cd " .. s:dir_foo, "cd:", s:dir_foo]
Bram Moolenaar28e8f732022-02-09 12:58:20 +00002503 call assert_equal(expected, s:li)
Bram Moolenaar3503d7c2019-11-09 20:10:17 +01002504 call chdir(s:dir_foo)
Bram Moolenaar28e8f732022-02-09 12:58:20 +00002505 call assert_equal(expected, s:li)
Bram Moolenaar3503d7c2019-11-09 20:10:17 +01002506 exe 'lcd ' .. fnameescape(s:dir_bar)
Bram Moolenaar28e8f732022-02-09 12:58:20 +00002507 call assert_equal(expected, s:li)
Bram Moolenaard8c9d322022-06-12 11:49:16 +01002508
2509 exe 'cd ' .. s:dir_foo
2510 exe 'cd ' .. s:dir_bar
2511 autocmd! test_dirchanged DirChanged global let g:result = expand("<afile>")
2512 cd -
Bram Moolenaardb77c492022-06-12 23:26:50 +01002513 call assert_equal(s:dir_foo, substitute(g:result, '\\', '/', 'g'))
Bram Moolenaard8c9d322022-06-12 11:49:16 +01002514
Bram Moolenaarb7407d32018-02-03 17:36:27 +01002515 call s:After_test_dirchanged()
2516endfunc
2517
2518function Test_dirchanged_local()
2519 call s:Before_test_dirchanged()
2520 autocmd test_dirchanged DirChanged window call add(s:li, "lcd:")
2521 autocmd test_dirchanged DirChanged window call add(s:li, expand("<afile>"))
Bram Moolenaar3503d7c2019-11-09 20:10:17 +01002522 call chdir(s:dir_foo)
Bram Moolenaarb7407d32018-02-03 17:36:27 +01002523 call assert_equal([], s:li)
Bram Moolenaar3503d7c2019-11-09 20:10:17 +01002524 exe 'lcd ' .. fnameescape(s:dir_bar)
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01002525 call assert_equal(["lcd:", s:dir_bar], s:li)
Bram Moolenaar3503d7c2019-11-09 20:10:17 +01002526 exe 'lcd ' .. fnameescape(s:dir_bar)
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01002527 call assert_equal(["lcd:", s:dir_bar], s:li)
Bram Moolenaarb7407d32018-02-03 17:36:27 +01002528 call s:After_test_dirchanged()
2529endfunc
2530
2531function Test_dirchanged_auto()
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +02002532 CheckOption autochdir
Bram Moolenaarb7407d32018-02-03 17:36:27 +01002533 call s:Before_test_dirchanged()
2534 call test_autochdir()
Bram Moolenaar28e8f732022-02-09 12:58:20 +00002535 autocmd test_dirchanged DirChangedPre auto call add(s:li, "pre cd " .. v:event.directory)
Bram Moolenaarb7407d32018-02-03 17:36:27 +01002536 autocmd test_dirchanged DirChanged auto call add(s:li, "auto:")
2537 autocmd test_dirchanged DirChanged auto call add(s:li, expand("<afile>"))
2538 set acd
Bram Moolenaar3503d7c2019-11-09 20:10:17 +01002539 cd ..
Bram Moolenaarb7407d32018-02-03 17:36:27 +01002540 call assert_equal([], s:li)
Bram Moolenaar61abe7d2022-08-30 21:46:08 +01002541 exe 'edit ' . s:dir_foo . '/Xautofile'
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01002542 call assert_equal(s:dir_foo, getcwd())
Bram Moolenaar28e8f732022-02-09 12:58:20 +00002543 let expected = ["pre cd " .. s:dir_foo, "auto:", s:dir_foo]
2544 call assert_equal(expected, s:li)
Bram Moolenaarb7407d32018-02-03 17:36:27 +01002545 set noacd
2546 bwipe!
2547 call s:After_test_dirchanged()
2548endfunc
Bram Moolenaar5a093432018-02-10 18:15:19 +01002549
2550" Test TextChangedI and TextChangedP
2551func Test_ChangedP()
2552 new
2553 call setline(1, ['foo', 'bar', 'foobar'])
2554 call test_override("char_avail", 1)
2555 set complete=. completeopt=menuone
2556
2557 func! TextChangedAutocmd(char)
2558 let g:autocmd .= a:char
2559 endfunc
2560
Christian Brabandtdb3b4462021-10-16 11:58:55 +01002561 " TextChanged will not be triggered, only check that it isn't.
Bram Moolenaar5a093432018-02-10 18:15:19 +01002562 au! TextChanged <buffer> :call TextChangedAutocmd('N')
2563 au! TextChangedI <buffer> :call TextChangedAutocmd('I')
2564 au! TextChangedP <buffer> :call TextChangedAutocmd('P')
2565
2566 call cursor(3, 1)
2567 let g:autocmd = ''
2568 call feedkeys("o\<esc>", 'tnix')
2569 call assert_equal('I', g:autocmd)
2570
2571 let g:autocmd = ''
2572 call feedkeys("Sf", 'tnix')
2573 call assert_equal('II', g:autocmd)
2574
2575 let g:autocmd = ''
2576 call feedkeys("Sf\<C-N>", 'tnix')
2577 call assert_equal('IIP', g:autocmd)
2578
2579 let g:autocmd = ''
2580 call feedkeys("Sf\<C-N>\<C-N>", 'tnix')
2581 call assert_equal('IIPP', g:autocmd)
2582
2583 let g:autocmd = ''
2584 call feedkeys("Sf\<C-N>\<C-N>\<C-N>", 'tnix')
2585 call assert_equal('IIPPP', g:autocmd)
2586
2587 let g:autocmd = ''
2588 call feedkeys("Sf\<C-N>\<C-N>\<C-N>\<C-N>", 'tnix')
2589 call assert_equal('IIPPPP', g:autocmd)
2590
2591 call assert_equal(['foo', 'bar', 'foobar', 'foo'], getline(1, '$'))
2592 " TODO: how should it handle completeopt=noinsert,noselect?
2593
2594 " CleanUp
2595 call test_override("char_avail", 0)
2596 au! TextChanged
2597 au! TextChangedI
2598 au! TextChangedP
2599 delfu TextChangedAutocmd
2600 unlet! g:autocmd
2601 set complete&vim completeopt&vim
2602
2603 bw!
2604endfunc
Bram Moolenaar8c64a362018-03-23 22:39:31 +01002605
Bram Moolenaar91d2e782018-08-07 19:05:01 +02002606let g:setline_handled = v:false
Bram Moolenaar1e115362019-01-09 23:01:02 +01002607func SetLineOne()
Bram Moolenaar91d2e782018-08-07 19:05:01 +02002608 if !g:setline_handled
2609 call setline(1, "(x)")
2610 let g:setline_handled = v:true
2611 endif
2612endfunc
2613
2614func Test_TextChangedI_with_setline()
2615 new
2616 call test_override('char_avail', 1)
2617 autocmd TextChangedI <buffer> call SetLineOne()
2618 call feedkeys("i(\<CR>\<Esc>", 'tx')
2619 call assert_equal('(', getline(1))
2620 call assert_equal('x)', getline(2))
2621 undo
Bram Moolenaar91d2e782018-08-07 19:05:01 +02002622 call assert_equal('', getline(1))
Bram Moolenaar9fa95062018-08-08 22:08:32 +02002623 call assert_equal('', getline(2))
Bram Moolenaar91d2e782018-08-07 19:05:01 +02002624
Bram Moolenaarca34db32022-01-20 11:17:18 +00002625 call test_override('char_avail', 0)
Bram Moolenaar91d2e782018-08-07 19:05:01 +02002626 bwipe!
2627endfunc
2628
Bram Moolenaar8c64a362018-03-23 22:39:31 +01002629func Test_Changed_FirstTime()
Bram Moolenaar8c5a2782019-08-07 23:07:07 +02002630 CheckFeature terminal
2631 CheckNotGui
Bram Moolenaar3cdcb092020-03-18 19:18:10 +01002632 " Starting a terminal to run Vim is always considered flaky.
Bram Moolenaar30d53e22020-03-18 21:10:44 +01002633 let g:test_is_flaky = 1
Bram Moolenaar8c5a2782019-08-07 23:07:07 +02002634
Bram Moolenaar8c64a362018-03-23 22:39:31 +01002635 " Prepare file for TextChanged event.
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01002636 call writefile([''], 'Xchanged.txt', 'D')
Bram Moolenaar8c64a362018-03-23 22:39:31 +01002637 let buf = term_start([GetVimProg(), '--clean', '-c', 'set noswapfile'], {'term_rows': 3})
2638 call assert_equal('running', term_getstatus(buf))
Bram Moolenaar1834d372018-03-29 17:40:46 +02002639 " Wait for the ruler (in the status line) to be shown.
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01002640 " In ConPTY, there is additional character which is drawn up to the width of
2641 " the screen.
2642 if has('conpty')
2643 call WaitForAssert({-> assert_match('\<All.*$', term_getline(buf, 3))})
2644 else
2645 call WaitForAssert({-> assert_match('\<All$', term_getline(buf, 3))})
2646 endif
Bram Moolenaar8c64a362018-03-23 22:39:31 +01002647 " It's only adding autocmd, so that no event occurs.
2648 call term_sendkeys(buf, ":au! TextChanged <buffer> call writefile(['No'], 'Xchanged.txt')\<cr>")
2649 call term_sendkeys(buf, "\<C-\\>\<C-N>:qa!\<cr>")
Bram Moolenaar50182fa2018-04-28 21:34:40 +02002650 call WaitForAssert({-> assert_equal('finished', term_getstatus(buf))})
Bram Moolenaar8c64a362018-03-23 22:39:31 +01002651 call assert_equal([''], readfile('Xchanged.txt'))
2652
2653 " clean up
Bram Moolenaar8c64a362018-03-23 22:39:31 +01002654 bwipe!
2655endfunc
Bram Moolenaar0566e892019-01-24 19:37:40 +01002656
Bram Moolenaareb93f3f2019-04-04 15:04:56 +02002657func Test_autocmd_nested()
2658 let g:did_nested = 0
2659 augroup Testing
2660 au WinNew * edit somefile
2661 au BufNew * let g:did_nested = 1
2662 augroup END
2663 split
2664 call assert_equal(0, g:did_nested)
2665 close
2666 bwipe! somefile
2667
2668 " old nested argument still works
2669 augroup Testing
2670 au!
2671 au WinNew * nested edit somefile
2672 au BufNew * let g:did_nested = 1
2673 augroup END
2674 split
2675 call assert_equal(1, g:did_nested)
2676 close
2677 bwipe! somefile
2678
2679 " New ++nested argument works
2680 augroup Testing
2681 au!
2682 au WinNew * ++nested edit somefile
2683 au BufNew * let g:did_nested = 1
2684 augroup END
2685 split
2686 call assert_equal(1, g:did_nested)
2687 close
2688 bwipe! somefile
2689
Bram Moolenaarf0775142022-03-04 20:10:38 +00002690 " nested without ++ does not work in Vim9 script
2691 call assert_fails('vim9cmd au WinNew * nested echo fails', 'E1078:')
2692
Bram Moolenaareb93f3f2019-04-04 15:04:56 +02002693 augroup Testing
2694 au!
2695 augroup END
2696
2697 call assert_fails('au WinNew * ++nested ++nested echo bad', 'E983:')
2698 call assert_fails('au WinNew * nested nested echo bad', 'E983:')
2699endfunc
2700
Bram Moolenaar5fa9f232022-07-23 09:06:48 +01002701func Test_autocmd_nested_cursor_invalid()
2702 set laststatus=0
2703 copen
2704 cclose
2705 call setline(1, ['foo', 'bar', 'baz'])
2706 3
2707 augroup nested_inv
2708 autocmd User foo ++nested copen
2709 autocmd BufAdd * let &laststatus = 2 - &laststatus
2710 augroup END
2711 doautocmd User foo
2712
2713 augroup nested_inv
2714 au!
2715 augroup END
2716 set laststatus&
Bram Moolenaarb03950f2022-07-26 13:47:13 +01002717 cclose
Bram Moolenaar5fa9f232022-07-23 09:06:48 +01002718 bwipe!
2719endfunc
2720
Bram Moolenaar3d6ee8b2022-07-27 15:23:35 +01002721func Test_autocmd_nested_keeps_cursor_pos()
2722 enew
2723 call setline(1, 'foo')
2724 autocmd User foo ++nested normal! $a
2725 autocmd InsertLeave * :
2726 doautocmd User foo
2727 call assert_equal([0, 1, 3, 0], getpos('.'))
2728
2729 bwipe!
2730endfunc
2731
Bram Moolenaarb03950f2022-07-26 13:47:13 +01002732func Test_autocmd_nested_switch_window()
2733 " run this in a separate Vim so that SafeState works
2734 CheckRunVimInTerminal
2735
2736 let lines =<< trim END
2737 vim9script
2738 ['()']->writefile('Xautofile')
2739 autocmd VimEnter * ++nested edit Xautofile | split
2740 autocmd BufReadPost * autocmd SafeState * ++once foldclosed('.')
2741 autocmd WinEnter * matchadd('ErrorMsg', 'pat')
2742 END
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01002743 call writefile(lines, 'Xautoscript', 'D')
Bram Moolenaarb03950f2022-07-26 13:47:13 +01002744 let buf = RunVimInTerminal('-S Xautoscript', {'rows': 10})
2745 call VerifyScreenDump(buf, 'Test_autocmd_nested_switch', {})
2746
2747 call StopVimInTerminal(buf)
2748 call delete('Xautofile')
Bram Moolenaarb03950f2022-07-26 13:47:13 +01002749endfunc
2750
Bram Moolenaareb93f3f2019-04-04 15:04:56 +02002751func Test_autocmd_once()
2752 " Without ++once WinNew triggers twice
2753 let g:did_split = 0
2754 augroup Testing
2755 au WinNew * let g:did_split += 1
2756 augroup END
2757 split
2758 split
2759 call assert_equal(2, g:did_split)
2760 call assert_true(exists('#WinNew'))
2761 close
2762 close
2763
2764 " With ++once WinNew triggers once
2765 let g:did_split = 0
2766 augroup Testing
2767 au!
2768 au WinNew * ++once let g:did_split += 1
2769 augroup END
2770 split
2771 split
2772 call assert_equal(1, g:did_split)
2773 call assert_false(exists('#WinNew'))
2774 close
2775 close
2776
2777 call assert_fails('au WinNew * ++once ++once echo bad', 'E983:')
2778endfunc
2779
Bram Moolenaara68e5952019-04-25 22:22:01 +02002780func Test_autocmd_bufreadpre()
2781 new
2782 let b:bufreadpre = 1
Bram Moolenaarab505b12020-03-23 19:28:44 +01002783 call append(0, range(1000))
Bram Moolenaara68e5952019-04-25 22:22:01 +02002784 w! XAutocmdBufReadPre.txt
2785 autocmd BufReadPre <buffer> :let b:bufreadpre += 1
Bram Moolenaarab505b12020-03-23 19:28:44 +01002786 norm! 500gg
Bram Moolenaara68e5952019-04-25 22:22:01 +02002787 sp
Bram Moolenaarab505b12020-03-23 19:28:44 +01002788 norm! 1000gg
Bram Moolenaara68e5952019-04-25 22:22:01 +02002789 wincmd p
2790 let g:wsv1 = winsaveview()
2791 wincmd p
2792 let g:wsv2 = winsaveview()
2793 " triggers BufReadPre, should not move the cursor in either window
2794 " The topline may change one line in a large window.
2795 edit
2796 call assert_inrange(g:wsv2.topline - 1, g:wsv2.topline + 1, winsaveview().topline)
2797 call assert_equal(g:wsv2.lnum, winsaveview().lnum)
2798 call assert_equal(2, b:bufreadpre)
2799 wincmd p
2800 call assert_equal(g:wsv1.topline, winsaveview().topline)
2801 call assert_equal(g:wsv1.lnum, winsaveview().lnum)
2802 call assert_equal(2, b:bufreadpre)
2803 " Now set the cursor position in an BufReadPre autocommand
2804 " (even though the position will be invalid, this should make Vim reset the
2805 " cursor position in the other window.
2806 wincmd p
2807 set cpo+=g
2808 " won't do anything, but try to set the cursor on an invalid lnum
2809 autocmd BufReadPre <buffer> :norm! 70gg
2810 " triggers BufReadPre, should not move the cursor in either window
2811 e
2812 call assert_equal(1, winsaveview().topline)
2813 call assert_equal(1, winsaveview().lnum)
2814 call assert_equal(3, b:bufreadpre)
2815 wincmd p
2816 call assert_equal(g:wsv1.topline, winsaveview().topline)
2817 call assert_equal(g:wsv1.lnum, winsaveview().lnum)
2818 call assert_equal(3, b:bufreadpre)
2819 close
2820 close
2821 call delete('XAutocmdBufReadPre.txt')
2822 set cpo-=g
2823endfunc
2824
Bram Moolenaar5e66b422019-01-24 21:58:10 +01002825" FileChangedShell tested in test_filechanged.vim
Bram Moolenaar69ea5872019-04-25 20:29:00 +02002826
2827" Tests for the following autocommands:
2828" - FileWritePre writing a compressed file
2829" - FileReadPost reading a compressed file
2830" - BufNewFile reading a file template
2831" - BufReadPre decompressing the file to be read
2832" - FilterReadPre substituting characters in the temp file
2833" - FilterReadPost substituting characters after filtering
2834" - FileReadPre set options for decompression
2835" - FileReadPost decompress the file
2836func Test_ReadWrite_Autocmds()
2837 " Run this test only on Unix-like systems and if gzip is available
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +02002838 CheckUnix
2839 CheckExecutable gzip
Bram Moolenaar69ea5872019-04-25 20:29:00 +02002840
2841 " Make $GZIP empty, "-v" would cause trouble.
2842 let $GZIP = ""
2843
2844 " Use a FileChangedShell autocommand to avoid a prompt for 'Xtestfile.gz'
2845 " being modified outside of Vim (noticed on Solaris).
2846 au FileChangedShell * echo 'caught FileChangedShell'
2847
2848 " Test for the FileReadPost, FileWritePre and FileWritePost autocmds
2849 augroup Test1
2850 au!
2851 au FileWritePre *.gz '[,']!gzip
2852 au FileWritePost *.gz undo
2853 au FileReadPost *.gz '[,']!gzip -d
2854 augroup END
2855
2856 new
2857 set bin
2858 call append(0, [
2859 \ 'line 2 Abcdefghijklmnopqrstuvwxyz',
2860 \ 'line 3 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2861 \ 'line 4 Abcdefghijklmnopqrstuvwxyz',
2862 \ 'line 5 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2863 \ 'line 6 Abcdefghijklmnopqrstuvwxyz',
2864 \ 'line 7 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2865 \ 'line 8 Abcdefghijklmnopqrstuvwxyz',
2866 \ 'line 9 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2867 \ 'line 10 Abcdefghijklmnopqrstuvwxyz'
2868 \ ])
2869 1,9write! Xtestfile.gz
2870 enew! | close
2871
2872 new
2873 " Read and decompress the testfile
2874 0read Xtestfile.gz
2875 call assert_equal([
2876 \ 'line 2 Abcdefghijklmnopqrstuvwxyz',
2877 \ 'line 3 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2878 \ 'line 4 Abcdefghijklmnopqrstuvwxyz',
2879 \ 'line 5 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2880 \ 'line 6 Abcdefghijklmnopqrstuvwxyz',
2881 \ 'line 7 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2882 \ 'line 8 Abcdefghijklmnopqrstuvwxyz',
2883 \ 'line 9 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2884 \ 'line 10 Abcdefghijklmnopqrstuvwxyz'
2885 \ ], getline(1, 9))
2886 enew! | close
2887
2888 augroup Test1
2889 au!
2890 augroup END
2891
2892 " Test for the FileAppendPre and FileAppendPost autocmds
2893 augroup Test2
2894 au!
2895 au BufNewFile *.c read Xtest.c
2896 au FileAppendPre *.out '[,']s/new/NEW/
2897 au FileAppendPost *.out !cat Xtest.c >> test.out
2898 augroup END
2899
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01002900 call writefile(['/*', ' * Here is a new .c file', ' */'], 'Xtest.c', 'D')
Bram Moolenaar69ea5872019-04-25 20:29:00 +02002901 new foo.c " should load Xtest.c
2902 call assert_equal(['/*', ' * Here is a new .c file', ' */'], getline(2, 4))
2903 w! >> test.out " append it to the output file
2904
2905 let contents = readfile('test.out')
2906 call assert_equal(' * Here is a NEW .c file', contents[2])
2907 call assert_equal(' * Here is a new .c file', contents[5])
2908
2909 call delete('test.out')
2910 enew! | close
2911 augroup Test2
2912 au!
2913 augroup END
2914
2915 " Test for the BufReadPre and BufReadPost autocmds
2916 augroup Test3
2917 au!
2918 " setup autocommands to decompress before reading and re-compress
2919 " afterwards
2920 au BufReadPre *.gz exe '!gzip -d ' . shellescape(expand("<afile>"))
2921 au BufReadPre *.gz call rename(expand("<afile>:r"), expand("<afile>"))
2922 au BufReadPost *.gz call rename(expand("<afile>"), expand("<afile>:r"))
2923 au BufReadPost *.gz exe '!gzip ' . shellescape(expand("<afile>:r"))
2924 augroup END
2925
2926 e! Xtestfile.gz " Edit compressed file
2927 call assert_equal([
2928 \ 'line 2 Abcdefghijklmnopqrstuvwxyz',
2929 \ 'line 3 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2930 \ 'line 4 Abcdefghijklmnopqrstuvwxyz',
2931 \ 'line 5 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2932 \ 'line 6 Abcdefghijklmnopqrstuvwxyz',
2933 \ 'line 7 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2934 \ 'line 8 Abcdefghijklmnopqrstuvwxyz',
2935 \ 'line 9 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2936 \ 'line 10 Abcdefghijklmnopqrstuvwxyz'
2937 \ ], getline(1, 9))
2938
2939 w! >> test.out " Append it to the output file
2940
2941 augroup Test3
2942 au!
2943 augroup END
2944
2945 " Test for the FilterReadPre and FilterReadPost autocmds.
2946 set shelltemp " need temp files here
2947 augroup Test4
2948 au!
2949 au FilterReadPre *.out call rename(expand("<afile>"), expand("<afile>") . ".t")
2950 au FilterReadPre *.out exe 'silent !sed s/e/E/ ' . shellescape(expand("<afile>")) . ".t >" . shellescape(expand("<afile>"))
2951 au FilterReadPre *.out exe 'silent !rm ' . shellescape(expand("<afile>")) . '.t'
2952 au FilterReadPost *.out '[,']s/x/X/g
2953 augroup END
2954
2955 e! test.out " Edit the output file
2956 1,$!cat
2957 call assert_equal([
2958 \ 'linE 2 AbcdefghijklmnopqrstuvwXyz',
2959 \ 'linE 3 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
2960 \ 'linE 4 AbcdefghijklmnopqrstuvwXyz',
2961 \ 'linE 5 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
2962 \ 'linE 6 AbcdefghijklmnopqrstuvwXyz',
2963 \ 'linE 7 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
2964 \ 'linE 8 AbcdefghijklmnopqrstuvwXyz',
2965 \ 'linE 9 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
2966 \ 'linE 10 AbcdefghijklmnopqrstuvwXyz'
2967 \ ], getline(1, 9))
2968 call assert_equal([
2969 \ 'line 2 Abcdefghijklmnopqrstuvwxyz',
2970 \ 'line 3 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2971 \ 'line 4 Abcdefghijklmnopqrstuvwxyz',
2972 \ 'line 5 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2973 \ 'line 6 Abcdefghijklmnopqrstuvwxyz',
2974 \ 'line 7 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2975 \ 'line 8 Abcdefghijklmnopqrstuvwxyz',
2976 \ 'line 9 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2977 \ 'line 10 Abcdefghijklmnopqrstuvwxyz'
2978 \ ], readfile('test.out'))
2979
2980 augroup Test4
2981 au!
2982 augroup END
2983 set shelltemp&vim
2984
2985 " Test for the FileReadPre and FileReadPost autocmds.
2986 augroup Test5
2987 au!
2988 au FileReadPre *.gz exe 'silent !gzip -d ' . shellescape(expand("<afile>"))
2989 au FileReadPre *.gz call rename(expand("<afile>:r"), expand("<afile>"))
2990 au FileReadPost *.gz '[,']s/l/L/
2991 augroup END
2992
2993 new
2994 0r Xtestfile.gz " Read compressed file
2995 call assert_equal([
2996 \ 'Line 2 Abcdefghijklmnopqrstuvwxyz',
2997 \ 'Line 3 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2998 \ 'Line 4 Abcdefghijklmnopqrstuvwxyz',
2999 \ 'Line 5 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
3000 \ 'Line 6 Abcdefghijklmnopqrstuvwxyz',
3001 \ 'Line 7 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
3002 \ 'Line 8 Abcdefghijklmnopqrstuvwxyz',
3003 \ 'Line 9 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
3004 \ 'Line 10 Abcdefghijklmnopqrstuvwxyz'
3005 \ ], getline(1, 9))
3006 call assert_equal([
3007 \ 'line 2 Abcdefghijklmnopqrstuvwxyz',
3008 \ 'line 3 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
3009 \ 'line 4 Abcdefghijklmnopqrstuvwxyz',
3010 \ 'line 5 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
3011 \ 'line 6 Abcdefghijklmnopqrstuvwxyz',
3012 \ 'line 7 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
3013 \ 'line 8 Abcdefghijklmnopqrstuvwxyz',
3014 \ 'line 9 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
3015 \ 'line 10 Abcdefghijklmnopqrstuvwxyz'
3016 \ ], readfile('Xtestfile.gz'))
3017
3018 augroup Test5
3019 au!
3020 augroup END
3021
3022 au! FileChangedShell
3023 call delete('Xtestfile.gz')
Bram Moolenaar69ea5872019-04-25 20:29:00 +02003024 call delete('test.out')
3025endfunc
Bram Moolenaar23b51392019-05-09 21:38:43 +02003026
3027func Test_throw_in_BufWritePre()
3028 new
3029 call setline(1, ['one', 'two', 'three'])
3030 call assert_false(filereadable('Xthefile'))
3031 augroup throwing
3032 au BufWritePre X* throw 'do not write'
3033 augroup END
3034 try
3035 w Xthefile
3036 catch
3037 let caught = 1
3038 endtry
3039 call assert_equal(1, caught)
3040 call assert_false(filereadable('Xthefile'))
3041
3042 bwipe!
3043 au! throwing
3044endfunc
Bram Moolenaarcadbe1b2019-09-22 21:50:09 +02003045
Bram Moolenaar40fa12a2021-09-22 14:18:13 +02003046func Test_autocmd_in_try_block()
Bram Moolenaar6f14da12022-09-07 21:30:44 +01003047 call mkdir('Xintrydir', 'R')
Bram Moolenaar40fa12a2021-09-22 14:18:13 +02003048 au BufEnter * let g:fname = expand('%')
3049 try
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +01003050 edit Xintrydir/
Bram Moolenaar40fa12a2021-09-22 14:18:13 +02003051 endtry
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +01003052 call assert_match('Xintrydir', g:fname)
Bram Moolenaar40fa12a2021-09-22 14:18:13 +02003053
3054 unlet g:fname
3055 au! BufEnter
Bram Moolenaar40fa12a2021-09-22 14:18:13 +02003056endfunc
3057
Bram Moolenaarcadbe1b2019-09-22 21:50:09 +02003058func Test_autocmd_SafeState()
3059 CheckRunVimInTerminal
3060
3061 let lines =<< trim END
3062 let g:safe = 0
3063 let g:again = ''
3064 au SafeState * let g:safe += 1
3065 au SafeStateAgain * let g:again ..= 'x'
3066 func CallTimer()
3067 call timer_start(10, {id -> execute('let g:again ..= "t"')})
3068 endfunc
3069 END
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01003070 call writefile(lines, 'XSafeState', 'D')
Bram Moolenaarcadbe1b2019-09-22 21:50:09 +02003071 let buf = RunVimInTerminal('-S XSafeState', #{rows: 6})
3072
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01003073 " Sometimes we loop to handle a K_IGNORE, SafeState may be triggered once or
Bram Moolenaar8fb1b472020-02-23 16:16:26 +01003074 " more often.
Bram Moolenaarcadbe1b2019-09-22 21:50:09 +02003075 call term_sendkeys(buf, ":echo g:safe\<CR>")
Bram Moolenaar8fb1b472020-02-23 16:16:26 +01003076 call WaitForAssert({-> assert_match('^\d ', term_getline(buf, 6))}, 1000)
Bram Moolenaarcadbe1b2019-09-22 21:50:09 +02003077
Bram Moolenaar8fb1b472020-02-23 16:16:26 +01003078 " SafeStateAgain should be invoked at least three times
Bram Moolenaarcadbe1b2019-09-22 21:50:09 +02003079 call term_sendkeys(buf, ":echo g:again\<CR>")
Bram Moolenaar8fb1b472020-02-23 16:16:26 +01003080 call WaitForAssert({-> assert_match('^xxx', term_getline(buf, 6))}, 1000)
Bram Moolenaarcadbe1b2019-09-22 21:50:09 +02003081
3082 call term_sendkeys(buf, ":let g:again = ''\<CR>:call CallTimer()\<CR>")
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02003083 call TermWait(buf, 50)
Bram Moolenaar0f6629a2019-09-22 23:24:13 +02003084 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02003085 call TermWait(buf, 50)
Bram Moolenaarcadbe1b2019-09-22 21:50:09 +02003086 call term_sendkeys(buf, ":echo g:again\<CR>")
3087 call WaitForAssert({-> assert_match('xtx', term_getline(buf, 6))}, 1000)
3088
3089 call StopVimInTerminal(buf)
Bram Moolenaarcadbe1b2019-09-22 21:50:09 +02003090endfunc
Bram Moolenaar23324a02019-10-01 17:39:04 +02003091
3092func Test_autocmd_CmdWinEnter()
3093 CheckRunVimInTerminal
Bram Moolenaar21829c52021-01-26 22:42:21 +01003094
Bram Moolenaar23324a02019-10-01 17:39:04 +02003095 let lines =<< trim END
Egor Zvorykin125ffd22021-11-17 14:01:14 +00003096 augroup vimHints | au! | augroup END
Bram Moolenaar23324a02019-10-01 17:39:04 +02003097 let b:dummy_var = 'This is a dummy'
3098 autocmd CmdWinEnter * quit
3099 let winnr = winnr('$')
3100 END
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01003101 let filename = 'XCmdWinEnter'
Bram Moolenaar23324a02019-10-01 17:39:04 +02003102 call writefile(lines, filename)
3103 let buf = RunVimInTerminal('-S '.filename, #{rows: 6})
3104
3105 call term_sendkeys(buf, "q:")
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02003106 call TermWait(buf)
Bram Moolenaar23324a02019-10-01 17:39:04 +02003107 call term_sendkeys(buf, ":echo b:dummy_var\<cr>")
Bram Moolenaar353c3512020-03-15 14:19:26 +01003108 call WaitForAssert({-> assert_match('^This is a dummy', term_getline(buf, 6))}, 2000)
Bram Moolenaar23324a02019-10-01 17:39:04 +02003109 call term_sendkeys(buf, ":echo &buftype\<cr>")
3110 call WaitForAssert({-> assert_notmatch('^nofile', term_getline(buf, 6))}, 1000)
3111 call term_sendkeys(buf, ":echo winnr\<cr>")
3112 call WaitForAssert({-> assert_match('^1', term_getline(buf, 6))}, 1000)
3113
3114 " clean up
3115 call StopVimInTerminal(buf)
3116 call delete(filename)
3117endfunc
Bram Moolenaarec66c412019-10-11 21:19:13 +02003118
3119func Test_autocmd_was_using_freed_memory()
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01003120 CheckFeature quickfix
3121
Bram Moolenaarec66c412019-10-11 21:19:13 +02003122 pedit xx
3123 n x
Bram Moolenaar92bb83e2021-02-03 23:04:46 +01003124 augroup winenter
3125 au WinEnter * if winnr('$') > 2 | quit | endif
3126 augroup END
Bram Moolenaarec66c412019-10-11 21:19:13 +02003127 split
Bram Moolenaar92bb83e2021-02-03 23:04:46 +01003128
3129 augroup winenter
3130 au! WinEnter
3131 augroup END
3132
3133 bwipe xx
3134 bwipe x
3135 pclose
Bram Moolenaarec66c412019-10-11 21:19:13 +02003136endfunc
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +01003137
3138func Test_BufWrite_lockmarks()
Bram Moolenaarf08b0eb2021-10-16 13:00:14 +01003139 let g:test_is_flaky = 1
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +01003140 edit! Xtest
3141 call setline(1, ['a', 'b', 'c', 'd'])
3142
3143 " :lockmarks preserves the marks
3144 call SetChangeMarks(2, 3)
3145 lockmarks write
3146 call assert_equal([2, 3], [line("'["), line("']")])
3147
3148 " *WritePre autocmds get the correct line range, but lockmarks preserves the
3149 " original values for the user
3150 augroup lockmarks
3151 au!
3152 au BufWritePre,FilterWritePre * call assert_equal([1, 4], [line("'["), line("']")])
3153 au FileWritePre * call assert_equal([3, 4], [line("'["), line("']")])
3154 augroup END
3155
3156 lockmarks write
3157 call assert_equal([2, 3], [line("'["), line("']")])
3158
3159 if executable('cat')
3160 lockmarks %!cat
3161 call assert_equal([2, 3], [line("'["), line("']")])
3162 endif
3163
3164 lockmarks 3,4write Xtest2
3165 call assert_equal([2, 3], [line("'["), line("']")])
3166
3167 au! lockmarks
3168 augroup! lockmarks
3169 call delete('Xtest')
3170 call delete('Xtest2')
3171endfunc
Bram Moolenaarce6db022020-01-07 20:11:42 +01003172
3173func Test_FileType_spell()
3174 if !isdirectory('/tmp')
3175 throw "Skipped: requires /tmp directory"
3176 endif
3177
3178 " this was crashing with an invalid free()
3179 setglobal spellfile=/tmp/en.utf-8.add
3180 augroup crash
3181 autocmd!
3182 autocmd BufNewFile,BufReadPost crashfile setf somefiletype
3183 autocmd BufNewFile,BufReadPost crashfile set ft=anotherfiletype
3184 autocmd FileType anotherfiletype setlocal spell
3185 augroup END
3186 func! NoCrash() abort
3187 edit /tmp/crashfile
3188 endfunc
3189 call NoCrash()
3190
3191 au! crash
3192 setglobal spellfile=
3193endfunc
Bram Moolenaarbc2b71d2020-02-17 21:33:30 +01003194
Bram Moolenaaref976322022-09-28 11:48:30 +01003195" this was wiping out the current buffer and using freed memory
3196func Test_SpellFileMissing_bwipe()
3197 next 0
3198 au SpellFileMissing 0 bwipe
3199 call assert_fails('set spell spelllang=0', 'E937:')
3200
3201 au! SpellFileMissing
Bram Moolenaar0a60f792022-11-19 21:18:11 +00003202 set nospell spelllang=en
Bram Moolenaaref976322022-09-28 11:48:30 +01003203 bwipe
3204endfunc
3205
Bram Moolenaar406cd902020-02-18 21:54:41 +01003206" Test closing a window or editing another buffer from a FileChangedRO handler
3207" in a readonly buffer
3208func Test_FileChangedRO_winclose()
Bram Moolenaar62cd26a2020-10-11 20:08:44 +02003209 call test_override('ui_delay', 10)
3210
Bram Moolenaar406cd902020-02-18 21:54:41 +01003211 augroup FileChangedROTest
3212 au!
3213 autocmd FileChangedRO * quit
3214 augroup END
3215 new
3216 set readonly
3217 call assert_fails('normal i', 'E788:')
3218 close
3219 augroup! FileChangedROTest
3220
3221 augroup FileChangedROTest
3222 au!
Bram Moolenaar61abe7d2022-08-30 21:46:08 +01003223 autocmd FileChangedRO * edit Xrofile
Bram Moolenaar406cd902020-02-18 21:54:41 +01003224 augroup END
3225 new
3226 set readonly
3227 call assert_fails('normal i', 'E788:')
3228 close
3229 augroup! FileChangedROTest
Bram Moolenaar62cd26a2020-10-11 20:08:44 +02003230 call test_override('ALL', 0)
Bram Moolenaar406cd902020-02-18 21:54:41 +01003231endfunc
3232
Bram Moolenaar0c81d1b2020-02-22 22:45:55 +01003233func LogACmd()
3234 call add(g:logged, line('$'))
3235endfunc
3236
3237func Test_TermChanged()
Bram Moolenaard28e0b32020-02-22 23:08:52 +01003238 CheckNotGui
3239
Bram Moolenaar0c81d1b2020-02-22 22:45:55 +01003240 enew!
3241 tabnew
3242 call setline(1, ['a', 'b', 'c', 'd'])
3243 $
3244 au TermChanged * call LogACmd()
3245 let g:logged = []
3246 let term_save = &term
3247 set term=xterm
3248 call assert_equal([1, 4], g:logged)
3249
3250 au! TermChanged
3251 let &term = term_save
3252 bwipe!
3253endfunc
3254
Bram Moolenaare3284872020-03-19 13:55:03 +01003255" Test for FileReadCmd autocmd
3256func Test_autocmd_FileReadCmd()
3257 func ReadFileCmd()
3258 call append(line('$'), "v:cmdarg = " .. v:cmdarg)
3259 endfunc
3260 augroup FileReadCmdTest
3261 au!
3262 au FileReadCmd Xtest call ReadFileCmd()
3263 augroup END
3264
3265 new
3266 read ++bin Xtest
3267 read ++nobin Xtest
3268 read ++edit Xtest
3269 read ++bad=keep Xtest
3270 read ++bad=drop Xtest
3271 read ++bad=- Xtest
3272 read ++ff=unix Xtest
3273 read ++ff=dos Xtest
3274 read ++ff=mac Xtest
3275 read ++enc=utf-8 Xtest
3276
3277 call assert_equal(['',
3278 \ 'v:cmdarg = ++bin',
3279 \ 'v:cmdarg = ++nobin',
3280 \ 'v:cmdarg = ++edit',
3281 \ 'v:cmdarg = ++bad=keep',
3282 \ 'v:cmdarg = ++bad=drop',
3283 \ 'v:cmdarg = ++bad=-',
3284 \ 'v:cmdarg = ++ff=unix',
3285 \ 'v:cmdarg = ++ff=dos',
3286 \ 'v:cmdarg = ++ff=mac',
3287 \ 'v:cmdarg = ++enc=utf-8'], getline(1, '$'))
3288
Bram Moolenaar23526d22022-12-05 15:50:41 +00003289 bwipe!
Bram Moolenaare3284872020-03-19 13:55:03 +01003290 augroup FileReadCmdTest
3291 au!
3292 augroup END
3293 delfunc ReadFileCmd
3294endfunc
3295
Bram Moolenaaree4e0c12020-04-06 21:35:05 +02003296" Test for passing invalid arguments to autocmd
3297func Test_autocmd_invalid_args()
3298 " Additional character after * for event
Bram Moolenaar61abe7d2022-08-30 21:46:08 +01003299 call assert_fails('autocmd *a Xinvfile set ff=unix', 'E215:')
Bram Moolenaaree4e0c12020-04-06 21:35:05 +02003300 augroup Test
3301 augroup END
3302 " Invalid autocmd event
Bram Moolenaar61abe7d2022-08-30 21:46:08 +01003303 call assert_fails('autocmd Bufabc Xinvfile set ft=vim', 'E216:')
Bram Moolenaaree4e0c12020-04-06 21:35:05 +02003304 " Invalid autocmd event in a autocmd group
Bram Moolenaar61abe7d2022-08-30 21:46:08 +01003305 call assert_fails('autocmd Test Bufabc Xinvfile set ft=vim', 'E216:')
Bram Moolenaaree4e0c12020-04-06 21:35:05 +02003306 augroup! Test
3307 " Execute all autocmds
3308 call assert_fails('doautocmd * BufEnter', 'E217:')
3309 call assert_fails('augroup! x1a2b3', 'E367:')
3310 call assert_fails('autocmd BufNew <buffer=999> pwd', 'E680:')
Bram Moolenaar531be472020-09-23 22:38:05 +02003311 call assert_fails('autocmd BufNew \) set ff=unix', 'E55:')
Bram Moolenaaree4e0c12020-04-06 21:35:05 +02003312endfunc
3313
3314" Test for deep nesting of autocmds
3315func Test_autocmd_deep_nesting()
Bram Moolenaar61abe7d2022-08-30 21:46:08 +01003316 autocmd BufEnter Xdeepfile doautocmd BufEnter Xdeepfile
3317 call assert_fails('doautocmd BufEnter Xdeepfile', 'E218:')
3318 autocmd! BufEnter Xdeepfile
Bram Moolenaaree4e0c12020-04-06 21:35:05 +02003319endfunc
3320
Bram Moolenaarbe5ee862020-06-10 20:56:58 +02003321" Tests for SigUSR1 autocmd event, which is only available on posix systems.
3322func Test_autocmd_sigusr1()
3323 CheckUnix
Bram Moolenaar0056ca72022-09-23 21:26:39 +01003324 " FIXME: should this work on MacOS M1?
3325 CheckNotMacM1
Bram Moolenaar62cd26a2020-10-11 20:08:44 +02003326 CheckExecutable /bin/kill
Bram Moolenaarbe5ee862020-06-10 20:56:58 +02003327
3328 let g:sigusr1_passed = 0
3329 au SigUSR1 * let g:sigusr1_passed = 1
3330 call system('/bin/kill -s usr1 ' . getpid())
3331 call WaitForAssert({-> assert_true(g:sigusr1_passed)})
3332
3333 au! SigUSR1
3334 unlet g:sigusr1_passed
3335endfunc
3336
Bram Moolenaarb340bae2020-06-15 19:51:56 +02003337" Test for BufReadPre autocmd deleting the file
3338func Test_BufReadPre_delfile()
3339 augroup TestAuCmd
3340 au!
Bram Moolenaare7cda972022-08-29 11:02:59 +01003341 autocmd BufReadPre XbufreadPre call delete('XbufreadPre')
Bram Moolenaarb340bae2020-06-15 19:51:56 +02003342 augroup END
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01003343 call writefile([], 'XbufreadPre', 'D')
Bram Moolenaare7cda972022-08-29 11:02:59 +01003344 call assert_fails('new XbufreadPre', 'E200:')
3345 call assert_equal('XbufreadPre', @%)
Bram Moolenaarb340bae2020-06-15 19:51:56 +02003346 call assert_equal(1, &readonly)
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01003347
Bram Moolenaarb340bae2020-06-15 19:51:56 +02003348 augroup TestAuCmd
3349 au!
3350 augroup END
3351 close!
3352endfunc
3353
3354" Test for BufReadPre autocmd changing the current buffer
3355func Test_BufReadPre_changebuf()
3356 augroup TestAuCmd
3357 au!
Bram Moolenaare7cda972022-08-29 11:02:59 +01003358 autocmd BufReadPre Xchangebuf edit Xsomeotherfile
Bram Moolenaarb340bae2020-06-15 19:51:56 +02003359 augroup END
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01003360 call writefile([], 'Xchangebuf', 'D')
Bram Moolenaare7cda972022-08-29 11:02:59 +01003361 call assert_fails('new Xchangebuf', 'E201:')
Bram Moolenaarb340bae2020-06-15 19:51:56 +02003362 call assert_equal('Xsomeotherfile', @%)
3363 call assert_equal(1, &readonly)
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01003364
Bram Moolenaarb340bae2020-06-15 19:51:56 +02003365 augroup TestAuCmd
3366 au!
3367 augroup END
3368 close!
3369endfunc
3370
3371" Test for BufWipeouti autocmd changing the current buffer when reading a file
3372" in an empty buffer with 'f' flag in 'cpo'
3373func Test_BufDelete_changebuf()
3374 new
3375 augroup TestAuCmd
3376 au!
3377 autocmd BufWipeout * let bufnr = bufadd('somefile') | exe "b " .. bufnr
3378 augroup END
3379 let save_cpo = &cpo
3380 set cpo+=f
Bram Moolenaarb18b4962022-09-02 21:55:50 +01003381 call assert_fails('r Xchangebuf', ['E812:', 'E484:'])
Bram Moolenaarb340bae2020-06-15 19:51:56 +02003382 call assert_equal('somefile', @%)
3383 let &cpo = save_cpo
3384 augroup TestAuCmd
3385 au!
3386 augroup END
3387 close!
3388endfunc
3389
Bram Moolenaar0fe937f2020-06-16 22:42:04 +02003390" Test for the temporary internal window used to execute autocmds
3391func Test_autocmd_window()
3392 %bw!
3393 edit one.txt
3394 tabnew two.txt
Bram Moolenaar41cd8032021-03-13 15:47:56 +01003395 vnew three.txt
3396 tabnew four.txt
3397 tabprevious
Bram Moolenaar0fe937f2020-06-16 22:42:04 +02003398 let g:blist = []
Bram Moolenaar832adf92020-06-25 19:01:36 +02003399 augroup aucmd_win_test1
Bram Moolenaar0fe937f2020-06-16 22:42:04 +02003400 au!
3401 au BufEnter * call add(g:blist, [expand('<afile>'),
3402 \ win_gettype(bufwinnr(expand('<afile>')))])
3403 augroup END
3404
3405 doautoall BufEnter
Bram Moolenaar41cd8032021-03-13 15:47:56 +01003406 call assert_equal([
3407 \ ['one.txt', 'autocmd'],
3408 \ ['two.txt', ''],
3409 \ ['four.txt', 'autocmd'],
3410 \ ['three.txt', ''],
3411 \ ], g:blist)
Bram Moolenaar0fe937f2020-06-16 22:42:04 +02003412
Bram Moolenaar832adf92020-06-25 19:01:36 +02003413 augroup aucmd_win_test1
Bram Moolenaar0fe937f2020-06-16 22:42:04 +02003414 au!
3415 augroup END
Bram Moolenaar832adf92020-06-25 19:01:36 +02003416 augroup! aucmd_win_test1
3417 %bw!
3418endfunc
3419
3420" Test for trying to close the temporary window used for executing an autocmd
3421func Test_close_autocmd_window()
3422 %bw!
3423 edit one.txt
3424 tabnew two.txt
3425 augroup aucmd_win_test2
3426 au!
3427 au BufEnter * if expand('<afile>') == 'one.txt' | 1close | endif
3428 augroup END
3429
3430 call assert_fails('doautoall BufEnter', 'E813:')
3431
3432 augroup aucmd_win_test2
3433 au!
3434 augroup END
3435 augroup! aucmd_win_test2
Bram Moolenaarcf844172020-06-26 19:44:06 +02003436 %bwipe!
3437endfunc
3438
3439" Test for trying to close the tab that has the temporary window for exeucing
3440" an autocmd.
3441func Test_close_autocmd_tab()
3442 edit one.txt
3443 tabnew two.txt
3444 augroup aucmd_win_test
3445 au!
3446 au BufEnter * if expand('<afile>') == 'one.txt' | tabfirst | tabonly | endif
3447 augroup END
3448
3449 call assert_fails('doautoall BufEnter', 'E813:')
3450
3451 tabonly
3452 augroup aucmd_win_test
3453 au!
3454 augroup END
3455 augroup! aucmd_win_test
3456 %bwipe!
Bram Moolenaar0fe937f2020-06-16 22:42:04 +02003457endfunc
3458
Bram Moolenaarcb1956d2022-01-07 15:45:18 +00003459func Test_Visual_doautoall_redraw()
3460 call setline(1, ['a', 'b'])
Bram Moolenaar94722c52023-01-28 19:19:03 +00003461 new
Bram Moolenaarcb1956d2022-01-07 15:45:18 +00003462 wincmd p
3463 call feedkeys("G\<C-V>", 'txn')
3464 autocmd User Explode ++once redraw
3465 doautoall User Explode
3466 %bwipe!
3467endfunc
3468
Bram Moolenaar6bcb8772021-02-03 21:23:29 +01003469" This was using freed memory.
3470func Test_BufNew_arglocal()
3471 arglocal
3472 au BufNew * arglocal
3473 call assert_fails('drop xx', 'E1156:')
3474
3475 au! BufNew
3476endfunc
3477
Bram Moolenaar8ab37572021-02-03 21:56:59 +01003478func Test_autocmd_closes_window()
3479 au BufNew,BufWinLeave * e %e
3480 file yyy
3481 au BufNew,BufWinLeave * ball
Bram Moolenaaraad5f9d2021-02-06 17:30:31 +01003482 n xxx
Bram Moolenaar8ab37572021-02-03 21:56:59 +01003483
Bram Moolenaaraad5f9d2021-02-06 17:30:31 +01003484 %bwipe
Bram Moolenaar8ab37572021-02-03 21:56:59 +01003485 au! BufNew
3486 au! BufWinLeave
3487endfunc
3488
Bram Moolenaar92bb83e2021-02-03 23:04:46 +01003489func Test_autocmd_quit_psearch()
3490 sn aa bb
3491 augroup aucmd_win_test
3492 au!
3493 au BufEnter,BufLeave,BufNew,WinEnter,WinLeave,WinNew * if winnr('$') > 1 | q | endif
3494 augroup END
3495 ps /
3496
3497 augroup aucmd_win_test
3498 au!
3499 augroup END
zeertzjq7851c692022-04-21 11:14:01 +01003500 new
3501 pclose
Bram Moolenaar92bb83e2021-02-03 23:04:46 +01003502endfunc
3503
Bram Moolenaaraad5f9d2021-02-06 17:30:31 +01003504" Fuzzer found some strange combination that caused a crash.
3505func Test_autocmd_normal_mess()
Bram Moolenaardd07c022021-02-07 13:32:46 +01003506 " For unknown reason this hangs on MS-Windows
3507 CheckNotMSWindows
3508
Bram Moolenaaraad5f9d2021-02-06 17:30:31 +01003509 augroup aucmd_normal_test
3510 au BufLeave,BufWinLeave,BufHidden,BufUnload,BufDelete,BufWipeout * norm 7q/qc
3511 augroup END
Bram Moolenaar983d83f2021-02-07 12:12:43 +01003512 call assert_fails('o4', 'E1159')
Bram Moolenaaraad5f9d2021-02-06 17:30:31 +01003513 silent! H
Bram Moolenaar983d83f2021-02-07 12:12:43 +01003514 call assert_fails('e xx', 'E1159')
Bram Moolenaaraad5f9d2021-02-06 17:30:31 +01003515 normal G
3516
3517 augroup aucmd_normal_test
3518 au!
3519 augroup END
3520endfunc
3521
Bram Moolenaar8c6951f2021-02-06 18:08:45 +01003522func Test_autocmd_closing_cmdwin()
Bram Moolenaardd07c022021-02-07 13:32:46 +01003523 " For unknown reason this hangs on MS-Windows
3524 CheckNotMSWindows
3525
Bram Moolenaar8c6951f2021-02-06 18:08:45 +01003526 au BufWinLeave * nested q
3527 call assert_fails("norm 7q?\n", 'E855:')
3528
3529 au! BufWinLeave
3530 new
3531 only
3532endfunc
3533
Bram Moolenaar2c7080b2021-02-06 19:19:42 +01003534func Test_autocmd_vimgrep()
3535 augroup aucmd_vimgrep
Charlie Grovesfef44852022-04-19 16:24:12 +01003536 au QuickfixCmdPre,BufNew,BufReadCmd * sb
zeertzjq7851c692022-04-21 11:14:01 +01003537 au QuickfixCmdPre,BufNew,BufReadCmd * q9
Bram Moolenaar2c7080b2021-02-06 19:19:42 +01003538 augroup END
Bram Moolenaardd07c022021-02-07 13:32:46 +01003539 call assert_fails('lv ?a? foo', 'E926:')
Bram Moolenaar2c7080b2021-02-06 19:19:42 +01003540
3541 augroup aucmd_vimgrep
3542 au!
3543 augroup END
3544endfunc
3545
Bram Moolenaar73b8b0a2021-08-01 14:52:32 +02003546func Test_autocmd_with_block()
3547 augroup block_testing
3548 au BufReadPost *.xml {
3549 setlocal matchpairs+=<:>
3550 /<start
3551 }
Bram Moolenaar63b91732021-08-05 20:40:03 +02003552 au CursorHold * {
3553 autocmd BufReadPre * ++once echo 'one' | echo 'two'
3554 g:gotSafeState = 77
3555 }
Bram Moolenaar73b8b0a2021-08-01 14:52:32 +02003556 augroup END
3557
3558 let expected = "\n--- Autocommands ---\nblock_testing BufRead\n *.xml {^@ setlocal matchpairs+=<:>^@ /<start^@ }"
3559 call assert_equal(expected, execute('au BufReadPost *.xml'))
3560
Bram Moolenaar63b91732021-08-05 20:40:03 +02003561 doautocmd CursorHold
3562 call assert_equal(77, g:gotSafeState)
3563 unlet g:gotSafeState
3564
Bram Moolenaar73b8b0a2021-08-01 14:52:32 +02003565 augroup block_testing
3566 au!
Bram Moolenaar75ebd2a2022-06-03 17:39:46 +01003567 autocmd CursorHold * {
3568 if true
3569 # comment
3570 && true
3571
3572 && true
3573 g:done = 'yes'
3574 endif
3575 }
3576 augroup END
3577 doautocmd CursorHold
3578 call assert_equal('yes', g:done)
3579
3580 unlet g:done
3581 augroup block_testing
3582 au!
Bram Moolenaar73b8b0a2021-08-01 14:52:32 +02003583 augroup END
3584endfunc
3585
Christian Brabandtdb3b4462021-10-16 11:58:55 +01003586" Test TextChangedI and TextChanged
3587func Test_Changed_ChangedI()
3588 new
3589 call test_override("char_avail", 1)
3590 let [g:autocmd_i, g:autocmd_n] = ['','']
3591
3592 func! TextChangedAutocmdI(char)
3593 let g:autocmd_{tolower(a:char)} = a:char .. b:changedtick
3594 endfunc
3595
3596 augroup Test_TextChanged
3597 au!
3598 au TextChanged <buffer> :call TextChangedAutocmdI('N')
3599 au TextChangedI <buffer> :call TextChangedAutocmdI('I')
3600 augroup END
3601
3602 call feedkeys("ifoo\<esc>", 'tnix')
3603 " TODO: Test test does not seem to trigger TextChanged autocommand, this
3604 " requires running Vim in a terminal window.
3605 " call assert_equal('N3', g:autocmd_n)
3606 call assert_equal('I3', g:autocmd_i)
3607
3608 call feedkeys("yyp", 'tnix')
3609 " TODO: Test test does not seem to trigger TextChanged autocommand.
3610 " call assert_equal('N4', g:autocmd_n)
3611 call assert_equal('I3', g:autocmd_i)
3612
3613 " CleanUp
3614 call test_override("char_avail", 0)
3615 au! TextChanged <buffer>
3616 au! TextChangedI <buffer>
3617 augroup! Test_TextChanged
3618 delfu TextChangedAutocmdI
3619 unlet! g:autocmd_i g:autocmd_n
3620
3621 bw!
3622endfunc
Bram Moolenaar2c7080b2021-02-06 19:19:42 +01003623
Bram Moolenaar6f2465d2022-03-22 18:13:01 +00003624func Test_closing_autocmd_window()
3625 let lines =<< trim END
3626 edit Xa.txt
3627 tabnew Xb.txt
3628 autocmd BufEnter Xa.txt unhide 1
3629 doautoall BufEnter
3630 END
3631 call v9.CheckScriptFailure(lines, 'E814:')
3632 au! BufEnter
Bram Moolenaar6f2465d2022-03-22 18:13:01 +00003633 bwipe Xa.txt
3634 bwipe Xb.txt
3635endfunc
3636
zeertzjq46bdae02023-09-24 23:16:08 +02003637func Test_switch_window_in_autocmd_window()
3638 edit Xa.txt
3639 tabnew Xb.txt
3640 autocmd BufEnter Xa.txt wincmd w
3641 doautoall BufEnter
3642 au! BufEnter
3643 bwipe Xa.txt
3644 call assert_false(bufexists('Xa.txt'))
3645 bwipe Xb.txt
3646 call assert_false(bufexists('Xb.txt'))
3647endfunc
3648
Bram Moolenaar347538f2022-03-26 16:28:06 +00003649func Test_bufwipeout_changes_window()
3650 " This should not crash, but we don't have any expectations about what
3651 " happens, changing window in BufWipeout has unpredictable results.
3652 tabedit
3653 let g:window_id = win_getid()
3654 topleft new
3655 setlocal bufhidden=wipe
3656 autocmd BufWipeout <buffer> call win_gotoid(g:window_id)
3657 tabprevious
3658 +tabclose
3659
3660 unlet g:window_id
3661 au! BufWipeout
3662 %bwipe!
3663endfunc
3664
zeertzjq021996f2022-04-10 11:44:04 +01003665func Test_v_event_readonly()
3666 autocmd CompleteChanged * let v:event.width = 0
3667 call assert_fails("normal! i\<C-X>\<C-V>", 'E46:')
3668 au! CompleteChanged
3669
3670 autocmd DirChangedPre * let v:event.directory = ''
3671 call assert_fails('cd .', 'E46:')
3672 au! DirChangedPre
3673
3674 autocmd ModeChanged * let v:event.new_mode = ''
3675 call assert_fails('normal! cc', 'E46:')
3676 au! ModeChanged
3677
3678 autocmd TextYankPost * let v:event.operator = ''
3679 call assert_fails('normal! yy', 'E46:')
3680 au! TextYankPost
3681endfunc
3682
zeertzjqc9e8fd62022-07-26 18:12:38 +01003683" Test for ModeChanged pattern
3684func Test_mode_changes()
3685 let g:index = 0
zeertzjq73916ba2023-04-26 16:50:19 +01003686 let g:mode_seq = ['n', 'i', 'n', 'v', 'V', 'i', 'ix', 'i', 'ic', 'i', 'n', 'no', 'noV', 'n', 'V', 'v', 's', 'n']
zeertzjqc9e8fd62022-07-26 18:12:38 +01003687 func! TestMode()
3688 call assert_equal(g:mode_seq[g:index], get(v:event, "old_mode"))
3689 call assert_equal(g:mode_seq[g:index + 1], get(v:event, "new_mode"))
3690 call assert_equal(mode(1), get(v:event, "new_mode"))
3691 let g:index += 1
3692 endfunc
3693
3694 au ModeChanged * :call TestMode()
3695 let g:n_to_any = 0
3696 au ModeChanged n:* let g:n_to_any += 1
zeertzjq73916ba2023-04-26 16:50:19 +01003697 call feedkeys("i\<esc>vVca\<CR>\<C-X>\<C-L>\<esc>ggdV\<MouseMove>G", 'tnix')
zeertzjqc9e8fd62022-07-26 18:12:38 +01003698
3699 let g:V_to_v = 0
3700 au ModeChanged V:v let g:V_to_v += 1
3701 call feedkeys("Vv\<C-G>\<esc>", 'tnix')
3702 call assert_equal(len(filter(g:mode_seq[1:], {idx, val -> val == 'n'})), g:n_to_any)
3703 call assert_equal(1, g:V_to_v)
3704 call assert_equal(len(g:mode_seq) - 1, g:index)
3705
3706 let g:n_to_i = 0
3707 au ModeChanged n:i let g:n_to_i += 1
3708 let g:n_to_niI = 0
3709 au ModeChanged i:niI let g:n_to_niI += 1
3710 let g:niI_to_i = 0
3711 au ModeChanged niI:i let g:niI_to_i += 1
3712 let g:nany_to_i = 0
3713 au ModeChanged n*:i let g:nany_to_i += 1
3714 let g:i_to_n = 0
3715 au ModeChanged i:n let g:i_to_n += 1
3716 let g:nori_to_any = 0
3717 au ModeChanged [ni]:* let g:nori_to_any += 1
3718 let g:i_to_any = 0
3719 au ModeChanged i:* let g:i_to_any += 1
3720 let g:index = 0
3721 let g:mode_seq = ['n', 'i', 'niI', 'i', 'n']
3722 call feedkeys("a\<C-O>l\<esc>", 'tnix')
3723 call assert_equal(len(g:mode_seq) - 1, g:index)
3724 call assert_equal(1, g:n_to_i)
3725 call assert_equal(1, g:n_to_niI)
3726 call assert_equal(1, g:niI_to_i)
3727 call assert_equal(2, g:nany_to_i)
3728 call assert_equal(1, g:i_to_n)
3729 call assert_equal(2, g:i_to_any)
3730 call assert_equal(3, g:nori_to_any)
3731
3732 if has('terminal')
3733 let g:mode_seq += ['c', 'n', 't', 'nt', 'c', 'nt', 'n']
3734 call feedkeys(":term\<CR>\<C-W>N:bd!\<CR>", 'tnix')
3735 call assert_equal(len(g:mode_seq) - 1, g:index)
3736 call assert_equal(1, g:n_to_i)
3737 call assert_equal(1, g:n_to_niI)
3738 call assert_equal(1, g:niI_to_i)
3739 call assert_equal(2, g:nany_to_i)
3740 call assert_equal(1, g:i_to_n)
3741 call assert_equal(2, g:i_to_any)
3742 call assert_equal(5, g:nori_to_any)
3743 endif
3744
zeertzjqd1955982022-10-05 11:24:46 +01003745 let g:n_to_c = 0
3746 au ModeChanged n:c let g:n_to_c += 1
3747 let g:c_to_n = 0
3748 au ModeChanged c:n let g:c_to_n += 1
3749 let g:mode_seq += ['c', 'n', 'c', 'n']
3750 call feedkeys("q:\<C-C>\<Esc>", 'tnix')
3751 call assert_equal(len(g:mode_seq) - 1, g:index)
3752 call assert_equal(2, g:n_to_c)
3753 call assert_equal(2, g:c_to_n)
3754 unlet g:n_to_c
3755 unlet g:c_to_n
zeertzjqc9e8fd62022-07-26 18:12:38 +01003756
Bram Moolenaar61c4b042022-10-18 15:10:11 +01003757 let g:n_to_v = 0
3758 au ModeChanged n:v let g:n_to_v += 1
3759 let g:v_to_n = 0
3760 au ModeChanged v:n let g:v_to_n += 1
3761 let g:mode_seq += ['v', 'n']
3762 call feedkeys("v\<C-C>", 'tnix')
3763 call assert_equal(len(g:mode_seq) - 1, g:index)
3764 call assert_equal(1, g:n_to_v)
3765 call assert_equal(1, g:v_to_n)
3766 unlet g:n_to_v
3767 unlet g:v_to_n
3768
zeertzjqc9e8fd62022-07-26 18:12:38 +01003769 au! ModeChanged
3770 delfunc TestMode
3771 unlet! g:mode_seq
3772 unlet! g:index
3773 unlet! g:n_to_any
3774 unlet! g:V_to_v
3775 unlet! g:n_to_i
3776 unlet! g:n_to_niI
3777 unlet! g:niI_to_i
3778 unlet! g:nany_to_i
3779 unlet! g:i_to_n
3780 unlet! g:nori_to_any
3781 unlet! g:i_to_any
3782endfunc
3783
3784func Test_recursive_ModeChanged()
3785 au! ModeChanged * norm 0u
3786 sil! norm 
3787 au! ModeChanged
3788endfunc
3789
3790func Test_ModeChanged_starts_visual()
3791 " This was triggering ModeChanged before setting VIsual, causing a crash.
3792 au! ModeChanged * norm 0u
3793 sil! norm 
3794
3795 au! ModeChanged
3796endfunc
Bram Moolenaar347538f2022-03-26 16:28:06 +00003797
Charlie Grovesfef44852022-04-19 16:24:12 +01003798func Test_noname_autocmd()
3799 augroup test_noname_autocmd_group
3800 autocmd!
3801 autocmd BufEnter * call add(s:li, ["BufEnter", expand("<afile>")])
3802 autocmd BufDelete * call add(s:li, ["BufDelete", expand("<afile>")])
3803 autocmd BufLeave * call add(s:li, ["BufLeave", expand("<afile>")])
3804 autocmd BufUnload * call add(s:li, ["BufUnload", expand("<afile>")])
3805 autocmd BufWipeout * call add(s:li, ["BufWipeout", expand("<afile>")])
3806 augroup END
3807
3808 let s:li = []
3809 edit foo
3810 call assert_equal([['BufUnload', ''], ['BufDelete', ''], ['BufWipeout', ''], ['BufEnter', 'foo']], s:li)
3811
3812 au! test_noname_autocmd_group
3813 augroup! test_noname_autocmd_group
3814endfunc
3815
Yegappan Lakshmanan1755a912022-05-19 10:31:47 +01003816" Test for the autocmd_get() function
3817func Test_autocmd_get()
3818 augroup TestAutoCmdFns
3819 au!
3820 autocmd BufAdd *.vim echo "bufadd-vim"
3821 autocmd BufAdd *.py echo "bufadd-py"
3822 autocmd BufHidden *.vim echo "bufhidden"
3823 augroup END
3824 augroup TestAutoCmdFns2
3825 autocmd BufAdd *.vim echo "bufadd-vim-2"
3826 autocmd BufRead *.a1b2c3 echo "bufadd-vim-2"
3827 augroup END
3828
3829 let l = autocmd_get()
3830 call assert_true(l->len() > 0)
3831
3832 " Test for getting all the autocmds in a group
3833 let expected = [
3834 \ #{cmd: 'echo "bufadd-vim"', group: 'TestAutoCmdFns',
3835 \ pattern: '*.vim', nested: v:false, once: v:false,
3836 \ event: 'BufAdd'},
3837 \ #{cmd: 'echo "bufadd-py"', group: 'TestAutoCmdFns',
3838 \ pattern: '*.py', nested: v:false, once: v:false,
3839 \ event: 'BufAdd'},
3840 \ #{cmd: 'echo "bufhidden"', group: 'TestAutoCmdFns',
3841 \ pattern: '*.vim', nested: v:false,
3842 \ once: v:false, event: 'BufHidden'}]
3843 call assert_equal(expected, autocmd_get(#{group: 'TestAutoCmdFns'}))
3844
3845 " Test for getting autocmds for all the patterns in a group
3846 call assert_equal(expected, autocmd_get(#{group: 'TestAutoCmdFns',
3847 \ event: '*'}))
3848
3849 " Test for getting autocmds for an event in a group
3850 let expected = [
3851 \ #{cmd: 'echo "bufadd-vim"', group: 'TestAutoCmdFns',
3852 \ pattern: '*.vim', nested: v:false, once: v:false,
3853 \ event: 'BufAdd'},
3854 \ #{cmd: 'echo "bufadd-py"', group: 'TestAutoCmdFns',
3855 \ pattern: '*.py', nested: v:false, once: v:false,
3856 \ event: 'BufAdd'}]
3857 call assert_equal(expected, autocmd_get(#{group: 'TestAutoCmdFns',
3858 \ event: 'BufAdd'}))
3859
3860 " Test for getting the autocmds for all the events in a group for particular
3861 " pattern
3862 call assert_equal([{'cmd': 'echo "bufadd-py"', 'group': 'TestAutoCmdFns',
3863 \ 'pattern': '*.py', 'nested': v:false, 'once': v:false,
3864 \ 'event': 'BufAdd'}],
3865 \ autocmd_get(#{group: 'TestAutoCmdFns', event: '*', pattern: '*.py'}))
3866
3867 " Test for getting the autocmds for an events in a group for particular
3868 " pattern
3869 let l = autocmd_get(#{group: 'TestAutoCmdFns', event: 'BufAdd',
3870 \ pattern: '*.vim'})
3871 call assert_equal([
3872 \ #{cmd: 'echo "bufadd-vim"', group: 'TestAutoCmdFns',
3873 \ pattern: '*.vim', nested: v:false, once: v:false,
3874 \ event: 'BufAdd'}], l)
3875
3876 " Test for getting the autocmds for a pattern in a group
3877 let l = autocmd_get(#{group: 'TestAutoCmdFns', pattern: '*.vim'})
3878 call assert_equal([
3879 \ #{cmd: 'echo "bufadd-vim"', group: 'TestAutoCmdFns',
3880 \ pattern: '*.vim', nested: v:false, once: v:false,
3881 \ event: 'BufAdd'},
3882 \ #{cmd: 'echo "bufhidden"', group: 'TestAutoCmdFns',
3883 \ pattern: '*.vim', nested: v:false,
3884 \ once: v:false, event: 'BufHidden'}], l)
3885
3886 " Test for getting the autocmds for a pattern in all the groups
3887 let l = autocmd_get(#{pattern: '*.a1b2c3'})
3888 call assert_equal([{'cmd': 'echo "bufadd-vim-2"', 'group': 'TestAutoCmdFns2',
3889 \ 'pattern': '*.a1b2c3', 'nested': v:false, 'once': v:false,
3890 \ 'event': 'BufRead'}], l)
3891
3892 " Test for getting autocmds for a pattern without any autocmds
3893 call assert_equal([], autocmd_get(#{group: 'TestAutoCmdFns',
3894 \ pattern: '*.abc'}))
3895 call assert_equal([], autocmd_get(#{group: 'TestAutoCmdFns',
3896 \ event: 'BufAdd', pattern: '*.abc'}))
3897 call assert_equal([], autocmd_get(#{group: 'TestAutoCmdFns',
3898 \ event: 'BufWipeout'}))
3899 call assert_fails("call autocmd_get(#{group: 'abc', event: 'BufAdd'})",
3900 \ 'E367:')
3901 let cmd = "echo autocmd_get(#{group: 'TestAutoCmdFns', event: 'abc'})"
3902 call assert_fails(cmd, 'E216:')
3903 call assert_fails("call autocmd_get(#{group: 'abc'})", 'E367:')
3904 call assert_fails("echo autocmd_get(#{event: 'abc'})", 'E216:')
3905
3906 augroup TestAutoCmdFns
3907 au!
3908 augroup END
3909 call assert_equal([], autocmd_get(#{group: 'TestAutoCmdFns'}))
3910
3911 " Test for nested and once autocmds
3912 augroup TestAutoCmdFns
3913 au!
3914 autocmd VimSuspend * ++nested echo "suspend"
3915 autocmd VimResume * ++once echo "resume"
3916 augroup END
3917
3918 let expected = [
3919 \ {'cmd': 'echo "suspend"', 'group': 'TestAutoCmdFns', 'pattern': '*',
3920 \ 'nested': v:true, 'once': v:false, 'event': 'VimSuspend'},
3921 \ {'cmd': 'echo "resume"', 'group': 'TestAutoCmdFns', 'pattern': '*',
3922 \ 'nested': v:false, 'once': v:true, 'event': 'VimResume'}]
3923 call assert_equal(expected, autocmd_get(#{group: 'TestAutoCmdFns'}))
3924
3925 " Test for buffer-local autocmd
3926 augroup TestAutoCmdFns
3927 au!
3928 autocmd TextYankPost <buffer> echo "textyankpost"
3929 augroup END
3930
3931 let expected = [
3932 \ {'cmd': 'echo "textyankpost"', 'group': 'TestAutoCmdFns',
3933 \ 'pattern': '<buffer=' .. bufnr() .. '>', 'nested': v:false,
3934 \ 'once': v:false, 'bufnr': bufnr(), 'event': 'TextYankPost'}]
3935 call assert_equal(expected, autocmd_get(#{group: 'TestAutoCmdFns'}))
3936
3937 augroup TestAutoCmdFns
3938 au!
3939 augroup END
3940 augroup! TestAutoCmdFns
3941 augroup TestAutoCmdFns2
3942 au!
3943 augroup END
3944 augroup! TestAutoCmdFns2
3945
3946 call assert_fails("echo autocmd_get(#{group: []})", 'E730:')
3947 call assert_fails("echo autocmd_get(#{event: {}})", 'E731:')
3948 call assert_fails("echo autocmd_get([])", 'E1206:')
3949endfunc
3950
3951" Test for the autocmd_add() function
3952func Test_autocmd_add()
3953 " Define a single autocmd in a group
3954 call autocmd_add([#{group: 'TestAcSet', event: 'BufAdd', pattern: '*.sh',
3955 \ cmd: 'echo "bufadd"', once: v:true, nested: v:true}])
3956 call assert_equal([#{cmd: 'echo "bufadd"', group: 'TestAcSet',
3957 \ pattern: '*.sh', nested: v:true, once: v:true,
3958 \ event: 'BufAdd'}], autocmd_get(#{group: 'TestAcSet'}))
3959
3960 " Define two autocmds in the same group
3961 call autocmd_delete([#{group: 'TestAcSet'}])
3962 call autocmd_add([#{group: 'TestAcSet', event: 'BufAdd', pattern: '*.sh',
3963 \ cmd: 'echo "bufadd"'},
3964 \ #{group: 'TestAcSet', event: 'BufEnter', pattern: '*.sh',
3965 \ cmd: 'echo "bufenter"'}])
3966 call assert_equal([
3967 \ #{cmd: 'echo "bufadd"', group: 'TestAcSet', pattern: '*.sh',
3968 \ nested: v:false, once: v:false, event: 'BufAdd'},
3969 \ #{cmd: 'echo "bufenter"', group: 'TestAcSet', pattern: '*.sh',
3970 \ nested: v:false, once: v:false, event: 'BufEnter'}],
3971 \ autocmd_get(#{group: 'TestAcSet'}))
3972
3973 " Define a buffer-local autocmd
3974 call autocmd_delete([#{group: 'TestAcSet'}])
3975 call autocmd_add([#{group: 'TestAcSet', event: 'CursorHold',
3976 \ bufnr: bufnr(), cmd: 'echo "cursorhold"'}])
3977 call assert_equal([
3978 \ #{cmd: 'echo "cursorhold"', group: 'TestAcSet',
3979 \ pattern: '<buffer=' .. bufnr() .. '>', nested: v:false,
3980 \ once: v:false, bufnr: bufnr(), event: 'CursorHold'}],
3981 \ autocmd_get(#{group: 'TestAcSet'}))
3982
3983 " Use an invalid buffer number
3984 call autocmd_delete([#{group: 'TestAcSet'}])
3985 call autocmd_add([#{group: 'TestAcSet', event: 'BufEnter',
3986 \ bufnr: -1, cmd: 'echo "bufenter"'}])
3987 let l = [#{group: 'TestAcSet', event: 'BufAdd', bufnr: 9999,
3988 \ cmd: 'echo "bufadd"'}]
3989 call assert_fails("echo autocmd_add(l)", 'E680:')
Yegappan Lakshmanan00e977c2022-06-01 12:31:53 +01003990 let l = [#{group: 'TestAcSet', event: 'BufAdd', bufnr: 9999,
3991 \ pattern: '*.py', cmd: 'echo "bufadd"'}]
3992 call assert_fails("echo autocmd_add(l)", 'E680:')
3993 let l = [#{group: 'TestAcSet', event: 'BufAdd', bufnr: 9999,
3994 \ pattern: ['*.py', '*.c'], cmd: 'echo "bufadd"'}]
3995 call assert_fails("echo autocmd_add(l)", 'E680:')
Yegappan Lakshmanan1755a912022-05-19 10:31:47 +01003996 let l = [#{group: 'TestAcSet', event: 'BufRead', bufnr: [],
3997 \ cmd: 'echo "bufread"'}]
3998 call assert_fails("echo autocmd_add(l)", 'E745:')
3999 call assert_equal([], autocmd_get(#{group: 'TestAcSet'}))
4000
4001 " Add two commands to the same group, event and pattern
4002 call autocmd_delete([#{group: 'TestAcSet'}])
4003 call autocmd_add([#{group: 'TestAcSet', event: 'BufUnload',
4004 \ pattern: 'abc', cmd: 'echo "cmd1"'}])
4005 call autocmd_add([#{group: 'TestAcSet', event: 'BufUnload',
4006 \ pattern: 'abc', cmd: 'echo "cmd2"'}])
4007 call assert_equal([
4008 \ #{cmd: 'echo "cmd1"', group: 'TestAcSet', pattern: 'abc',
4009 \ nested: v:false, once: v:false, event: 'BufUnload'},
4010 \ #{cmd: 'echo "cmd2"', group: 'TestAcSet', pattern: 'abc',
4011 \ nested: v:false, once: v:false, event: 'BufUnload'}],
4012 \ autocmd_get(#{group: 'TestAcSet'}))
4013
4014 " When adding a new autocmd, if the autocmd 'group' is not specified, then
4015 " the current autocmd group should be used.
4016 call autocmd_delete([#{group: 'TestAcSet'}])
4017 augroup TestAcSet
4018 call autocmd_add([#{event: 'BufHidden', pattern: 'abc', cmd: 'echo "abc"'}])
4019 augroup END
4020 call assert_equal([
4021 \ #{cmd: 'echo "abc"', group: 'TestAcSet', pattern: 'abc',
4022 \ nested: v:false, once: v:false, event: 'BufHidden'}],
4023 \ autocmd_get(#{group: 'TestAcSet'}))
4024
Yegappan Lakshmanan971f6822022-05-24 11:40:11 +01004025 " Test for replacing a cmd for an event in a group
4026 call autocmd_delete([#{group: 'TestAcSet'}])
4027 call autocmd_add([#{replace: v:true, group: 'TestAcSet', event: 'BufEnter',
4028 \ pattern: '*.py', cmd: 'echo "bufenter"'}])
4029 call autocmd_add([#{replace: v:true, group: 'TestAcSet', event: 'BufEnter',
4030 \ pattern: '*.py', cmd: 'echo "bufenter"'}])
4031 call assert_equal([
4032 \ #{cmd: 'echo "bufenter"', group: 'TestAcSet', pattern: '*.py',
4033 \ nested: v:false, once: v:false, event: 'BufEnter'}],
4034 \ autocmd_get(#{group: 'TestAcSet'}))
4035
4036 " Test for adding a command for an unsupported autocmd event
Yegappan Lakshmanan1755a912022-05-19 10:31:47 +01004037 let l = [#{group: 'TestAcSet', event: 'abc', pattern: '*.sh',
4038 \ cmd: 'echo "bufadd"'}]
4039 call assert_fails('call autocmd_add(l)', 'E216:')
4040
Yegappan Lakshmanane0ff3a72022-05-27 18:05:33 +01004041 " Test for using a list of events and patterns
4042 call autocmd_delete([#{group: 'TestAcSet'}])
4043 let l = [#{group: 'TestAcSet', event: ['BufEnter', 'BufLeave'],
4044 \ pattern: ['*.py', '*.sh'], cmd: 'echo "bufcmds"'}]
4045 call autocmd_add(l)
4046 call assert_equal([
4047 \ #{cmd: 'echo "bufcmds"', group: 'TestAcSet', pattern: '*.py',
4048 \ nested: v:false, once: v:false, event: 'BufEnter'},
4049 \ #{cmd: 'echo "bufcmds"', group: 'TestAcSet', pattern: '*.sh',
4050 \ nested: v:false, once: v:false, event: 'BufEnter'},
4051 \ #{cmd: 'echo "bufcmds"', group: 'TestAcSet', pattern: '*.py',
4052 \ nested: v:false, once: v:false, event: 'BufLeave'},
4053 \ #{cmd: 'echo "bufcmds"', group: 'TestAcSet', pattern: '*.sh',
4054 \ nested: v:false, once: v:false, event: 'BufLeave'}],
4055 \ autocmd_get(#{group: 'TestAcSet'}))
4056
4057 " Test for invalid values for 'event' item
4058 call autocmd_delete([#{group: 'TestAcSet'}])
4059 let l = [#{group: 'TestAcSet', event: test_null_string(),
4060 \ pattern: "*.py", cmd: 'echo "bufcmds"'}]
4061 call assert_fails('call autocmd_add(l)', 'E928:')
4062 let l = [#{group: 'TestAcSet', event: test_null_list(),
4063 \ pattern: "*.py", cmd: 'echo "bufcmds"'}]
4064 call assert_fails('call autocmd_add(l)', 'E714:')
4065 let l = [#{group: 'TestAcSet', event: {},
4066 \ pattern: "*.py", cmd: 'echo "bufcmds"'}]
4067 call assert_fails('call autocmd_add(l)', 'E777:')
4068 let l = [#{group: 'TestAcSet', event: [{}],
4069 \ pattern: "*.py", cmd: 'echo "bufcmds"'}]
4070 call assert_fails('call autocmd_add(l)', 'E928:')
4071 let l = [#{group: 'TestAcSet', event: [test_null_string()],
4072 \ pattern: "*.py", cmd: 'echo "bufcmds"'}]
4073 call assert_fails('call autocmd_add(l)', 'E928:')
4074 let l = [#{group: 'TestAcSet', event: 'BufEnter,BufLeave',
4075 \ pattern: '*.py', cmd: 'echo "bufcmds"'}]
4076 call assert_fails('call autocmd_add(l)', 'E216:')
4077 let l = [#{group: 'TestAcSet', event: [],
4078 \ pattern: "*.py", cmd: 'echo "bufcmds"'}]
4079 call autocmd_add(l)
4080 let l = [#{group: 'TestAcSet', event: [""],
4081 \ pattern: "*.py", cmd: 'echo "bufcmds"'}]
4082 call assert_fails('call autocmd_add(l)', 'E216:')
4083 let l = [#{group: 'TestAcSet', event: "",
4084 \ pattern: "*.py", cmd: 'echo "bufcmds"'}]
4085 call autocmd_add(l)
4086 call assert_equal([], autocmd_get(#{group: 'TestAcSet'}))
4087
4088 " Test for invalid values for 'pattern' item
4089 let l = [#{group: 'TestAcSet', event: "BufEnter",
4090 \ pattern: test_null_string(), cmd: 'echo "bufcmds"'}]
Yegappan Lakshmanan00e977c2022-06-01 12:31:53 +01004091 call assert_fails('call autocmd_add(l)', 'E928:')
Yegappan Lakshmanane0ff3a72022-05-27 18:05:33 +01004092 let l = [#{group: 'TestAcSet', event: "BufEnter",
4093 \ pattern: test_null_list(), cmd: 'echo "bufcmds"'}]
4094 call assert_fails('call autocmd_add(l)', 'E714:')
4095 let l = [#{group: 'TestAcSet', event: "BufEnter",
4096 \ pattern: {}, cmd: 'echo "bufcmds"'}]
4097 call assert_fails('call autocmd_add(l)', 'E777:')
4098 let l = [#{group: 'TestAcSet', event: "BufEnter",
4099 \ pattern: [{}], cmd: 'echo "bufcmds"'}]
4100 call assert_fails('call autocmd_add(l)', 'E928:')
4101 let l = [#{group: 'TestAcSet', event: "BufEnter",
4102 \ pattern: [test_null_string()], cmd: 'echo "bufcmds"'}]
4103 call assert_fails('call autocmd_add(l)', 'E928:')
4104 let l = [#{group: 'TestAcSet', event: "BufEnter",
4105 \ pattern: [], cmd: 'echo "bufcmds"'}]
4106 call autocmd_add(l)
4107 let l = [#{group: 'TestAcSet', event: "BufEnter",
4108 \ pattern: [""], cmd: 'echo "bufcmds"'}]
4109 call autocmd_add(l)
4110 let l = [#{group: 'TestAcSet', event: "BufEnter",
4111 \ pattern: "", cmd: 'echo "bufcmds"'}]
4112 call autocmd_add(l)
4113 call assert_equal([], autocmd_get(#{group: 'TestAcSet'}))
4114
4115 let l = [#{group: 'TestAcSet', event: 'BufEnter,abc,BufLeave',
4116 \ pattern: '*.py', cmd: 'echo "bufcmds"'}]
4117 call assert_fails('call autocmd_add(l)', 'E216:')
4118
Yegappan Lakshmanan1755a912022-05-19 10:31:47 +01004119 call assert_fails("call autocmd_add({})", 'E1211:')
4120 call assert_equal(v:false, autocmd_add(test_null_list()))
4121 call assert_true(autocmd_add([[]]))
4122 call assert_true(autocmd_add([test_null_dict()]))
4123
4124 augroup TestAcSet
4125 au!
4126 augroup END
4127
4128 call autocmd_add([#{group: 'TestAcSet'}])
4129 call autocmd_add([#{group: 'TestAcSet', event: 'BufAdd'}])
4130 call autocmd_add([#{group: 'TestAcSet', pat: '*.sh'}])
4131 call autocmd_add([#{group: 'TestAcSet', cmd: 'echo "a"'}])
4132 call autocmd_add([#{group: 'TestAcSet', event: 'BufAdd', pat: '*.sh'}])
4133 call autocmd_add([#{group: 'TestAcSet', event: 'BufAdd', cmd: 'echo "a"'}])
4134 call autocmd_add([#{group: 'TestAcSet', pat: '*.sh', cmd: 'echo "a"'}])
4135 call assert_equal([], autocmd_get(#{group: 'TestAcSet'}))
4136
4137 augroup! TestAcSet
4138endfunc
4139
4140" Test for deleting autocmd events and groups
4141func Test_autocmd_delete()
4142 " Delete an event in an autocmd group
4143 augroup TestAcSet
4144 au!
4145 au BufAdd *.sh echo "bufadd"
4146 au BufEnter *.sh echo "bufenter"
4147 augroup END
4148 call autocmd_delete([#{group: 'TestAcSet', event: 'BufAdd'}])
4149 call assert_equal([#{cmd: 'echo "bufenter"', group: 'TestAcSet',
4150 \ pattern: '*.sh', nested: v:false, once: v:false,
4151 \ event: 'BufEnter'}], autocmd_get(#{group: 'TestAcSet'}))
4152
4153 " Delete all the events in an autocmd group
4154 augroup TestAcSet
4155 au BufAdd *.sh echo "bufadd"
4156 augroup END
4157 call autocmd_delete([#{group: 'TestAcSet', event: '*'}])
4158 call assert_equal([], autocmd_get(#{group: 'TestAcSet'}))
4159
4160 " Delete a non-existing autocmd group
4161 call assert_fails("call autocmd_delete([#{group: 'abc'}])", 'E367:')
4162 " Delete a non-existing autocmd event
4163 let l = [#{group: 'TestAcSet', event: 'abc'}]
4164 call assert_fails("call autocmd_delete(l)", 'E216:')
4165 " Delete a non-existing autocmd pattern
4166 let l = [#{group: 'TestAcSet', event: 'BufAdd', pat: 'abc'}]
4167 call assert_true(autocmd_delete(l))
Yegappan Lakshmanan00e977c2022-06-01 12:31:53 +01004168 " Delete an autocmd for a non-existing buffer
4169 let l = [#{event: '*', bufnr: 9999, cmd: 'echo "x"'}]
4170 call assert_fails('call autocmd_delete(l)', 'E680:')
Yegappan Lakshmanan1755a912022-05-19 10:31:47 +01004171
4172 " Delete an autocmd group
4173 augroup TestAcSet
4174 au!
4175 au BufAdd *.sh echo "bufadd"
4176 au BufEnter *.sh echo "bufenter"
4177 augroup END
4178 call autocmd_delete([#{group: 'TestAcSet'}])
4179 call assert_fails("call autocmd_get(#{group: 'TestAcSet'})", 'E367:')
4180
4181 call assert_true(autocmd_delete([[]]))
4182 call assert_true(autocmd_delete([test_null_dict()]))
4183endfunc
4184
Bram Moolenaar8f3c3c62022-10-18 17:05:54 +01004185func Test_autocmd_split_dummy()
4186 " Autocommand trying to split a window containing a dummy buffer.
Bram Moolenaar94722c52023-01-28 19:19:03 +00004187 auto BufReadPre * exe "sbuf " .. expand("<abuf>")
Bram Moolenaar8f3c3c62022-10-18 17:05:54 +01004188 " Avoid the "W11" prompt
4189 au FileChangedShell * let v:fcs_choice = 'reload'
4190 func Xautocmd_changelist()
4191 cal writefile(['Xtestfile2:4:4'], 'Xerr')
4192 edit Xerr
4193 lex 'Xtestfile2:4:4'
4194 endfunc
4195 call Xautocmd_changelist()
Bram Moolenaar53c5c9f2022-10-18 17:25:03 +01004196 " Should get E86, but it doesn't always happen (timing?)
4197 silent! call Xautocmd_changelist()
Bram Moolenaar8f3c3c62022-10-18 17:05:54 +01004198
4199 au! BufReadPre
4200 au! FileChangedShell
4201 delfunc Xautocmd_changelist
4202 bwipe! Xerr
4203 call delete('Xerr')
4204endfunc
4205
Bram Moolenaare76062c2022-11-28 18:51:43 +00004206" This was crashing because there was only one window to execute autocommands
4207" in.
4208func Test_autocmd_nested_setbufvar()
4209 CheckFeature python3
4210
4211 set hidden
4212 edit Xaaa
4213 edit Xbbb
4214 call setline(1, 'bar')
4215 enew
4216 au BufWriteCmd Xbbb ++nested call setbufvar('Xaaa', '&ft', 'foo') | bw! Xaaa
4217 au FileType foo call py3eval('vim.current.buffer.options["cindent"]')
4218 wall
4219
4220 au! BufWriteCmd
4221 au! FileType foo
4222 set nohidden
4223 call delete('Xaaa')
4224 call delete('Xbbb')
4225 %bwipe!
4226endfunc
4227
Christian Brabandt9aee8ec2022-12-16 16:41:23 +00004228func SetupVimTest_shm()
4229 let g:bwe = []
4230 let g:brp = []
4231 set shortmess+=F
zeertzjq657b31f2023-04-15 21:28:02 +01004232 messages clear
Christian Brabandt9aee8ec2022-12-16 16:41:23 +00004233
4234 let dirname='XVimTestSHM'
4235 call mkdir(dirname, 'R')
4236 call writefile(['test'], dirname .. '/1')
4237 call writefile(['test'], dirname .. '/2')
4238 call writefile(['test'], dirname .. '/3')
4239
4240 augroup test
4241 autocmd!
4242 autocmd BufWinEnter * call add(g:bwe, $'BufWinEnter: {expand('<amatch>')}')
4243 autocmd BufReadPost * call add(g:brp, $'BufReadPost: {expand('<amatch>')}')
4244 augroup END
4245
4246 call setqflist([
4247 \ {'filename': dirname .. '/1', 'lnum': 1, 'col': 1, 'text': 'test', 'vcol': 0},
4248 \ {'filename': dirname .. '/2', 'lnum': 1, 'col': 1, 'text': 'test', 'vcol': 0},
4249 \ {'filename': dirname .. '/3', 'lnum': 1, 'col': 1, 'text': 'test', 'vcol': 0}
4250 \ ])
4251 cdo! substitute/test/TEST
4252
4253 " clean up
4254 noa enew!
4255 set shortmess&vim
4256 augroup test
4257 autocmd!
4258 augroup END
4259 augroup! test
4260endfunc
4261
4262func Test_autocmd_shortmess()
4263 CheckNotMSWindows
4264
4265 call SetupVimTest_shm()
4266 let output = execute(':mess')->split('\n')
4267
4268 let info = copy(output)->filter({idx, val -> val =~# '\d of 3'} )
4269 let bytes = copy(output)->filter({idx, val -> val =~# 'bytes'} )
4270
4271 " We test the following here:
4272 " BufReadPost should have been triggered 3 times, once per file
4273 " BufWinEnter should have been triggered 3 times, once per file
4274 " FileInfoMessage should have been shown 3 times, regardless of shm option
4275 " "(x of 3)" message from :cnext has been shown 3 times
4276
4277 call assert_equal(3, g:brp->len())
4278 call assert_equal(3, g:bwe->len())
4279 call assert_equal(3, info->len())
4280 call assert_equal(3, bytes->len())
4281
4282 delfunc SetupVimTest_shm
4283endfunc
Bram Moolenaare76062c2022-11-28 18:51:43 +00004284
Bram Moolenaarbc2b71d2020-02-17 21:33:30 +01004285" vim: shiftwidth=2 sts=2 expandtab