blob: be73c18197ea5995ff0f2a5780458180c94d24b4 [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
Sergey Vlasov1f47db72024-01-25 23:07:00 +0100273 au WinNewPre * call add(g:record, 'WinNewPre')
Bram Moolenaarc917da42016-07-19 22:31:36 +0200274 au WinNew * call add(g:record, 'WinNew')
naohiro ono23beefe2021-11-13 12:38:49 +0000275 au WinClosed * call add(g:record, 'WinClosed')
Bram Moolenaar94722c52023-01-28 19:19:03 +0000276 au WinEnter * call add(g:record, 'WinEnter')
277 au WinLeave * call add(g:record, 'WinLeave')
Bram Moolenaarc917da42016-07-19 22:31:36 +0200278 au TabNew * call add(g:record, 'TabNew')
Bram Moolenaar12c11d52016-07-19 23:13:03 +0200279 au TabClosed * call add(g:record, 'TabClosed')
Bram Moolenaarc917da42016-07-19 22:31:36 +0200280 au TabEnter * call add(g:record, 'TabEnter')
281 au TabLeave * call add(g:record, 'TabLeave')
282 augroup END
283
284 split
285 tabnew
286 close
287 close
288
289 call assert_equal([
Sergey Vlasov1f47db72024-01-25 23:07:00 +0100290 \ 'WinNewPre', 'WinLeave', 'WinNew', 'WinEnter',
291 \ 'WinLeave', 'TabLeave', 'WinNewPre', 'WinNew', 'WinEnter', 'TabNew', 'TabEnter',
naohiro ono23beefe2021-11-13 12:38:49 +0000292 \ 'WinLeave', 'TabLeave', 'WinClosed', 'TabClosed', 'WinEnter', 'TabEnter',
293 \ 'WinLeave', 'WinClosed', 'WinEnter'
Bram Moolenaarc917da42016-07-19 22:31:36 +0200294 \ ], g:record)
295
Bram Moolenaar12c11d52016-07-19 23:13:03 +0200296 let g:record = []
297 tabnew somefile
298 tabnext
299 bwipe somefile
300
301 call assert_equal([
Sergey Vlasov1f47db72024-01-25 23:07:00 +0100302 \ 'WinLeave', 'TabLeave', 'WinNewPre', 'WinNew', 'WinEnter', 'TabNew', 'TabEnter',
Bram Moolenaar12c11d52016-07-19 23:13:03 +0200303 \ 'WinLeave', 'TabLeave', 'WinEnter', 'TabEnter',
naohiro ono23beefe2021-11-13 12:38:49 +0000304 \ 'WinClosed', 'TabClosed'
Bram Moolenaar12c11d52016-07-19 23:13:03 +0200305 \ ], g:record)
306
Sergey Vlasov1f47db72024-01-25 23:07:00 +0100307 let g:record = []
308 copen
309 help
310 tabnext
311 vnew
312
313 call assert_equal([
314 \ 'WinNewPre', 'WinLeave', 'WinNew', 'WinEnter',
315 \ 'WinNewPre', 'WinLeave', 'WinNew', 'WinEnter',
316 \ 'WinNewPre', 'WinLeave', 'WinNew', 'WinEnter'
317 \ ], g:record)
318
Bram Moolenaarc917da42016-07-19 22:31:36 +0200319 augroup testing
320 au!
321 augroup END
322 unlet g:record
323endfunc
Bram Moolenaare99e8442016-07-26 20:43:40 +0200324
Sergey Vlasov1f47db72024-01-25 23:07:00 +0100325func Test_WinNewPre()
326 " Test that the old window layout can be accessed before a new window is created.
327 let g:layouts_pre = []
328 let g:layouts_post = []
329 augroup testing
330 au WinNewPre * call add(g:layouts_pre, winlayout())
331 au WinNew * call add(g:layouts_post, winlayout())
332 augroup END
333 split
334 call assert_notequal(g:layouts_pre[0], g:layouts_post[0])
335 split
336 call assert_equal(g:layouts_pre[1], g:layouts_post[0])
337 call assert_notequal(g:layouts_pre[1], g:layouts_post[1])
338 tabnew
339 call assert_notequal(g:layouts_pre[2], g:layouts_post[1])
340 call assert_notequal(g:layouts_pre[2], g:layouts_post[2])
341 augroup testing
342 au!
343 augroup END
344 unlet g:layouts_pre
345 unlet g:layouts_post
346
347 " Test modifying window layout during WinNewPre throws.
348 let g:caught = 0
349 augroup testing
350 au!
351 au WinNewPre * split
352 augroup END
353 try
354 vnew
355 catch
356 let g:caught += 1
357 endtry
358 augroup testing
359 au!
360 au WinNewPre * tabnew
361 augroup END
362 try
363 vnew
364 catch
365 let g:caught += 1
366 endtry
367 augroup testing
368 au!
369 au WinNewPre * close
370 augroup END
371 try
372 vnew
373 catch
374 let g:caught += 1
375 endtry
376 augroup testing
377 au!
378 au WinNewPre * tabclose
379 augroup END
380 try
381 vnew
382 catch
383 let g:caught += 1
384 endtry
385 call assert_equal(4, g:caught)
386 augroup testing
387 au!
388 augroup END
389 unlet g:caught
390endfunc
391
Bram Moolenaar35fc61c2022-11-22 12:40:50 +0000392func Test_WinResized()
393 CheckRunVimInTerminal
394
395 let lines =<< trim END
396 set scrolloff=0
397 call setline(1, ['111', '222'])
398 vnew
399 call setline(1, ['aaa', 'bbb'])
400 new
401 call setline(1, ['foo', 'bar'])
402
403 let g:resized = 0
404 au WinResized * let g:resized += 1
405
406 func WriteResizedEvent()
407 call writefile([json_encode(v:event)], 'XresizeEvent')
408 endfunc
409 au WinResized * call WriteResizedEvent()
410 END
411 call writefile(lines, 'Xtest_winresized', 'D')
412 let buf = RunVimInTerminal('-S Xtest_winresized', {'rows': 10})
413
414 " redraw now to avoid a redraw after the :echo command
415 call term_sendkeys(buf, ":redraw!\<CR>")
416 call TermWait(buf)
417
418 call term_sendkeys(buf, ":echo g:resized\<CR>")
419 call WaitForAssert({-> assert_match('^0$', term_getline(buf, 10))}, 1000)
420
421 " increase window height, two windows will be reported
422 call term_sendkeys(buf, "\<C-W>+")
423 call TermWait(buf)
424 call term_sendkeys(buf, ":echo g:resized\<CR>")
425 call WaitForAssert({-> assert_match('^1$', term_getline(buf, 10))}, 1000)
426
427 let event = readfile('XresizeEvent')[0]->json_decode()
428 call assert_equal({
429 \ 'windows': [1002, 1001],
430 \ }, event)
431
432 " increase window width, three windows will be reported
433 call term_sendkeys(buf, "\<C-W>>")
434 call TermWait(buf)
435 call term_sendkeys(buf, ":echo g:resized\<CR>")
436 call WaitForAssert({-> assert_match('^2$', term_getline(buf, 10))}, 1000)
437
438 let event = readfile('XresizeEvent')[0]->json_decode()
439 call assert_equal({
440 \ 'windows': [1002, 1001, 1000],
441 \ }, event)
442
443 call delete('XresizeEvent')
444 call StopVimInTerminal(buf)
445endfunc
446
LemonBoy09371822022-04-08 15:18:45 +0100447func Test_WinScrolled()
448 CheckRunVimInTerminal
449
450 let lines =<< trim END
zeertzjqd58862d2022-04-12 11:32:48 +0100451 set nowrap scrolloff=0
452 for ii in range(1, 18)
453 call setline(ii, repeat(nr2char(96 + ii), ii * 2))
454 endfor
455 let win_id = win_getid()
456 let g:matched = v:false
Bram Moolenaar35fc61c2022-11-22 12:40:50 +0000457 func WriteScrollEvent()
458 call writefile([json_encode(v:event)], 'XscrollEvent')
459 endfunc
zeertzjqd58862d2022-04-12 11:32:48 +0100460 execute 'au WinScrolled' win_id 'let g:matched = v:true'
461 let g:scrolled = 0
462 au WinScrolled * let g:scrolled += 1
463 au WinScrolled * let g:amatch = str2nr(expand('<amatch>'))
464 au WinScrolled * let g:afile = str2nr(expand('<afile>'))
Bram Moolenaar35fc61c2022-11-22 12:40:50 +0000465 au WinScrolled * call WriteScrollEvent()
LemonBoy09371822022-04-08 15:18:45 +0100466 END
Bram Moolenaare1f3ab72022-09-04 21:29:08 +0100467 call writefile(lines, 'Xtest_winscrolled', 'D')
LemonBoy09371822022-04-08 15:18:45 +0100468 let buf = RunVimInTerminal('-S Xtest_winscrolled', {'rows': 6})
469
470 call term_sendkeys(buf, ":echo g:scrolled\<CR>")
471 call WaitForAssert({-> assert_match('^0 ', term_getline(buf, 6))}, 1000)
472
473 " Scroll left/right in Normal mode.
474 call term_sendkeys(buf, "zlzh:echo g:scrolled\<CR>")
475 call WaitForAssert({-> assert_match('^2 ', term_getline(buf, 6))}, 1000)
476
Bram Moolenaar35fc61c2022-11-22 12:40:50 +0000477 let event = readfile('XscrollEvent')[0]->json_decode()
478 call assert_equal({
zeertzjq3fc84dc2022-12-07 09:17:59 +0000479 \ 'all': {'leftcol': 1, 'topline': 0, 'topfill': 0, 'width': 0, 'height': 0, 'skipcol': 0},
480 \ '1000': {'leftcol': -1, 'topline': 0, 'topfill': 0, 'width': 0, 'height': 0, 'skipcol': 0}
Bram Moolenaar35fc61c2022-11-22 12:40:50 +0000481 \ }, event)
482
LemonBoy09371822022-04-08 15:18:45 +0100483 " Scroll up/down in Normal mode.
484 call term_sendkeys(buf, "\<c-e>\<c-y>:echo g:scrolled\<CR>")
485 call WaitForAssert({-> assert_match('^4 ', term_getline(buf, 6))}, 1000)
486
Bram Moolenaar35fc61c2022-11-22 12:40:50 +0000487 let event = readfile('XscrollEvent')[0]->json_decode()
488 call assert_equal({
zeertzjq3fc84dc2022-12-07 09:17:59 +0000489 \ 'all': {'leftcol': 0, 'topline': 1, 'topfill': 0, 'width': 0, 'height': 0, 'skipcol': 0},
490 \ '1000': {'leftcol': 0, 'topline': -1, 'topfill': 0, 'width': 0, 'height': 0, 'skipcol': 0}
Bram Moolenaar35fc61c2022-11-22 12:40:50 +0000491 \ }, event)
492
LemonBoy09371822022-04-08 15:18:45 +0100493 " Scroll up/down in Insert mode.
494 call term_sendkeys(buf, "Mi\<c-x>\<c-e>\<Esc>i\<c-x>\<c-y>\<Esc>")
495 call term_sendkeys(buf, ":echo g:scrolled\<CR>")
496 call WaitForAssert({-> assert_match('^6 ', term_getline(buf, 6))}, 1000)
497
Bram Moolenaar35fc61c2022-11-22 12:40:50 +0000498 let event = readfile('XscrollEvent')[0]->json_decode()
499 call assert_equal({
zeertzjq3fc84dc2022-12-07 09:17:59 +0000500 \ 'all': {'leftcol': 0, 'topline': 1, 'topfill': 0, 'width': 0, 'height': 0, 'skipcol': 0},
501 \ '1000': {'leftcol': 0, 'topline': -1, 'topfill': 0, 'width': 0, 'height': 0, 'skipcol': 0}
Bram Moolenaar35fc61c2022-11-22 12:40:50 +0000502 \ }, event)
503
LemonBoy09371822022-04-08 15:18:45 +0100504 " Scroll the window horizontally to focus the last letter of the third line
505 " containing only six characters. Moving to the previous and shorter lines
506 " should trigger another autocommand as Vim has to make them visible.
507 call term_sendkeys(buf, "5zl2k")
508 call term_sendkeys(buf, ":echo g:scrolled\<CR>")
509 call WaitForAssert({-> assert_match('^8 ', term_getline(buf, 6))}, 1000)
510
Bram Moolenaar35fc61c2022-11-22 12:40:50 +0000511 let event = readfile('XscrollEvent')[0]->json_decode()
512 call assert_equal({
zeertzjq3fc84dc2022-12-07 09:17:59 +0000513 \ 'all': {'leftcol': 5, 'topline': 0, 'topfill': 0, 'width': 0, 'height': 0, 'skipcol': 0},
514 \ '1000': {'leftcol': -5, 'topline': 0, 'topfill': 0, 'width': 0, 'height': 0, 'skipcol': 0}
Bram Moolenaar35fc61c2022-11-22 12:40:50 +0000515 \ }, event)
516
LemonBoy09371822022-04-08 15:18:45 +0100517 " Ensure the command was triggered for the specified window ID.
518 call term_sendkeys(buf, ":echo g:matched\<CR>")
519 call WaitForAssert({-> assert_match('^v:true ', term_getline(buf, 6))}, 1000)
520
521 " Ensure the expansion of <amatch> and <afile> matches the window ID.
522 call term_sendkeys(buf, ":echo g:amatch == win_id && g:afile == win_id\<CR>")
523 call WaitForAssert({-> assert_match('^v:true ', term_getline(buf, 6))}, 1000)
524
Bram Moolenaar35fc61c2022-11-22 12:40:50 +0000525 call delete('XscrollEvent')
LemonBoy09371822022-04-08 15:18:45 +0100526 call StopVimInTerminal(buf)
LemonBoy09371822022-04-08 15:18:45 +0100527endfunc
528
LemonBoy66e13ae2022-04-21 22:52:11 +0100529func Test_WinScrolled_mouse()
530 CheckRunVimInTerminal
531
532 let lines =<< trim END
533 set nowrap scrolloff=0
534 set mouse=a term=xterm ttymouse=sgr mousetime=200 clipboard=
535 call setline(1, ['foo']->repeat(32))
536 split
537 let g:scrolled = 0
538 au WinScrolled * let g:scrolled += 1
539 END
Bram Moolenaare1f3ab72022-09-04 21:29:08 +0100540 call writefile(lines, 'Xtest_winscrolled_mouse', 'D')
LemonBoy66e13ae2022-04-21 22:52:11 +0100541 let buf = RunVimInTerminal('-S Xtest_winscrolled_mouse', {'rows': 10})
542
543 " With the upper split focused, send a scroll-down event to the unfocused one.
544 call test_setmouse(7, 1)
545 call term_sendkeys(buf, "\<ScrollWheelDown>")
546 call TermWait(buf)
547 call term_sendkeys(buf, ":echo g:scrolled\<CR>")
548 call WaitForAssert({-> assert_match('^1', term_getline(buf, 10))}, 1000)
549
550 " Again, but this time while we're in insert mode.
551 call term_sendkeys(buf, "i\<ScrollWheelDown>\<Esc>")
552 call TermWait(buf)
553 call term_sendkeys(buf, ":echo g:scrolled\<CR>")
554 call WaitForAssert({-> assert_match('^2', term_getline(buf, 10))}, 1000)
555
556 call StopVimInTerminal(buf)
LemonBoy66e13ae2022-04-21 22:52:11 +0100557endfunc
558
zeertzjqd58862d2022-04-12 11:32:48 +0100559func Test_WinScrolled_close_curwin()
560 CheckRunVimInTerminal
561
562 let lines =<< trim END
563 set nowrap scrolloff=0
564 call setline(1, ['aaa', 'bbb'])
565 vsplit
566 au WinScrolled * close
567 au VimLeave * call writefile(['123456'], 'Xtestout')
568 END
Bram Moolenaare1f3ab72022-09-04 21:29:08 +0100569 call writefile(lines, 'Xtest_winscrolled_close_curwin', 'D')
zeertzjqd58862d2022-04-12 11:32:48 +0100570 let buf = RunVimInTerminal('-S Xtest_winscrolled_close_curwin', {'rows': 6})
571
572 " This was using freed memory
573 call term_sendkeys(buf, "\<C-E>")
574 call TermWait(buf)
575 call StopVimInTerminal(buf)
576
Bram Moolenaar0a60f792022-11-19 21:18:11 +0000577 " check the startup script finished to the end
zeertzjqd58862d2022-04-12 11:32:48 +0100578 call assert_equal(['123456'], readfile('Xtestout'))
zeertzjqd58862d2022-04-12 11:32:48 +0100579 call delete('Xtestout')
580endfunc
581
Bram Moolenaar0a60f792022-11-19 21:18:11 +0000582func Test_WinScrolled_once_only()
583 CheckRunVimInTerminal
584
585 let lines =<< trim END
586 set cmdheight=2
587 call setline(1, ['aaa', 'bbb'])
588 let trigger_count = 0
589 func ShowInfo(id)
590 echo g:trigger_count g:winid winlayout()
591 endfunc
592
593 vsplit
594 split
595 " use a timer to show the info after a redraw
596 au WinScrolled * let trigger_count += 1 | let winid = expand('<amatch>') | call timer_start(100, 'ShowInfo')
597 wincmd j
598 wincmd l
599 END
600 call writefile(lines, 'Xtest_winscrolled_once', 'D')
601 let buf = RunVimInTerminal('-S Xtest_winscrolled_once', #{rows: 10, cols: 60, statusoff: 2})
602
603 call term_sendkeys(buf, "\<C-E>")
604 call VerifyScreenDump(buf, 'Test_winscrolled_once_only_1', {})
605
606 call StopVimInTerminal(buf)
607endfunc
608
Bram Moolenaar29967732022-11-20 12:11:45 +0000609" Check that WinScrolled is not triggered immediately when defined and there
610" are split windows.
611func Test_WinScrolled_not_when_defined()
612 CheckRunVimInTerminal
613
614 let lines =<< trim END
615 call setline(1, ['aaa', 'bbb'])
616 echo 'nothing happened'
617 func ShowTriggered(id)
618 echo 'triggered'
619 endfunc
620 END
621 call writefile(lines, 'Xtest_winscrolled_not', 'D')
622 let buf = RunVimInTerminal('-S Xtest_winscrolled_not', #{rows: 10, cols: 60, statusoff: 2})
623 call term_sendkeys(buf, ":split\<CR>")
624 call TermWait(buf)
625 " use a timer to show the message after redrawing
626 call term_sendkeys(buf, ":au WinScrolled * call timer_start(100, 'ShowTriggered')\<CR>")
627 call VerifyScreenDump(buf, 'Test_winscrolled_not_when_defined_1', {})
628
629 call term_sendkeys(buf, "\<C-E>")
630 call VerifyScreenDump(buf, 'Test_winscrolled_not_when_defined_2', {})
631
632 call StopVimInTerminal(buf)
633endfunc
634
zeertzjq670ab032022-08-28 19:16:15 +0100635func Test_WinScrolled_long_wrapped()
636 CheckRunVimInTerminal
637
638 let lines =<< trim END
639 set scrolloff=0
640 let height = winheight(0)
641 let width = winwidth(0)
642 let g:scrolled = 0
643 au WinScrolled * let g:scrolled += 1
644 call setline(1, repeat('foo', height * width))
645 call cursor(1, height * width)
646 END
Bram Moolenaare1f3ab72022-09-04 21:29:08 +0100647 call writefile(lines, 'Xtest_winscrolled_long_wrapped', 'D')
zeertzjq670ab032022-08-28 19:16:15 +0100648 let buf = RunVimInTerminal('-S Xtest_winscrolled_long_wrapped', {'rows': 6})
649
650 call term_sendkeys(buf, ":echo g:scrolled\<CR>")
651 call WaitForAssert({-> assert_match('^0 ', term_getline(buf, 6))}, 1000)
652
653 call term_sendkeys(buf, 'gj')
654 call term_sendkeys(buf, ":echo g:scrolled\<CR>")
655 call WaitForAssert({-> assert_match('^1 ', term_getline(buf, 6))}, 1000)
656
657 call term_sendkeys(buf, '0')
658 call term_sendkeys(buf, ":echo g:scrolled\<CR>")
659 call WaitForAssert({-> assert_match('^2 ', term_getline(buf, 6))}, 1000)
660
661 call term_sendkeys(buf, '$')
662 call term_sendkeys(buf, ":echo g:scrolled\<CR>")
663 call WaitForAssert({-> assert_match('^3 ', term_getline(buf, 6))}, 1000)
Bram Moolenaar23526d22022-12-05 15:50:41 +0000664
665 call StopVimInTerminal(buf)
zeertzjq670ab032022-08-28 19:16:15 +0100666endfunc
667
zeertzjq3fc84dc2022-12-07 09:17:59 +0000668func Test_WinScrolled_diff()
669 CheckRunVimInTerminal
670
671 let lines =<< trim END
672 set diffopt+=foldcolumn:0
673 call setline(1, ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i'])
674 vnew
675 call setline(1, ['d', 'e', 'f', 'g', 'h', 'i'])
676 windo diffthis
677 func WriteScrollEvent()
678 call writefile([json_encode(v:event)], 'XscrollEvent')
679 endfunc
680 au WinScrolled * call WriteScrollEvent()
681 END
682 call writefile(lines, 'Xtest_winscrolled_diff', 'D')
683 let buf = RunVimInTerminal('-S Xtest_winscrolled_diff', {'rows': 8})
684
685 call term_sendkeys(buf, "\<C-E>")
686 call WaitForAssert({-> assert_match('^d', term_getline(buf, 3))}, 1000)
687
688 let event = readfile('XscrollEvent')[0]->json_decode()
689 call assert_equal({
690 \ 'all': {'leftcol': 0, 'topline': 1, 'topfill': 1, 'width': 0, 'height': 0, 'skipcol': 0},
691 \ '1000': {'leftcol': 0, 'topline': 1, 'topfill': 0, 'width': 0, 'height': 0, 'skipcol': 0},
692 \ '1001': {'leftcol': 0, 'topline': 0, 'topfill': -1, 'width': 0, 'height': 0, 'skipcol': 0}
693 \ }, event)
694
695 call term_sendkeys(buf, "2\<C-E>")
696 call WaitForAssert({-> assert_match('^f', term_getline(buf, 3))}, 1000)
697
698 let event = readfile('XscrollEvent')[0]->json_decode()
699 call assert_equal({
700 \ 'all': {'leftcol': 0, 'topline': 2, 'topfill': 2, 'width': 0, 'height': 0, 'skipcol': 0},
701 \ '1000': {'leftcol': 0, 'topline': 2, 'topfill': 0, 'width': 0, 'height': 0, 'skipcol': 0},
702 \ '1001': {'leftcol': 0, 'topline': 0, 'topfill': -2, 'width': 0, 'height': 0, 'skipcol': 0}
703 \ }, event)
704
705 call term_sendkeys(buf, "\<C-E>")
706 call WaitForAssert({-> assert_match('^g', term_getline(buf, 3))}, 1000)
707
708 let event = readfile('XscrollEvent')[0]->json_decode()
709 call assert_equal({
710 \ 'all': {'leftcol': 0, 'topline': 2, 'topfill': 0, 'width': 0, 'height': 0, 'skipcol': 0},
711 \ '1000': {'leftcol': 0, 'topline': 1, 'topfill': 0, 'width': 0, 'height': 0, 'skipcol': 0},
712 \ '1001': {'leftcol': 0, 'topline': 1, 'topfill': 0, 'width': 0, 'height': 0, 'skipcol': 0}
713 \ }, event)
714
715 call term_sendkeys(buf, "2\<C-Y>")
716 call WaitForAssert({-> assert_match('^e', term_getline(buf, 3))}, 1000)
717
718 let event = readfile('XscrollEvent')[0]->json_decode()
719 call assert_equal({
720 \ 'all': {'leftcol': 0, 'topline': 3, 'topfill': 1, 'width': 0, 'height': 0, 'skipcol': 0},
721 \ '1000': {'leftcol': 0, 'topline': -2, 'topfill': 0, 'width': 0, 'height': 0, 'skipcol': 0},
722 \ '1001': {'leftcol': 0, 'topline': -1, 'topfill': 1, 'width': 0, 'height': 0, 'skipcol': 0}
723 \ }, event)
724
725 call StopVimInTerminal(buf)
Dominique Pelle541c87c2023-01-17 21:20:44 +0000726 call delete('XscrollEvent')
zeertzjq3fc84dc2022-12-07 09:17:59 +0000727endfunc
728
naohiro ono23beefe2021-11-13 12:38:49 +0000729func Test_WinClosed()
730 " Test that the pattern is matched against the closed window's ID, and both
731 " <amatch> and <afile> are set to it.
732 new
733 let winid = win_getid()
734 let g:matched = v:false
735 augroup test-WinClosed
736 autocmd!
737 execute 'autocmd WinClosed' winid 'let g:matched = v:true'
738 autocmd WinClosed * let g:amatch = str2nr(expand('<amatch>'))
739 autocmd WinClosed * let g:afile = str2nr(expand('<afile>'))
740 augroup END
741 close
742 call assert_true(g:matched)
743 call assert_equal(winid, g:amatch)
744 call assert_equal(winid, g:afile)
745
746 " Test that WinClosed is non-recursive.
747 new
748 new
749 call assert_equal(3, winnr('$'))
750 let g:triggered = 0
751 augroup test-WinClosed
752 autocmd!
753 autocmd WinClosed * let g:triggered += 1
754 autocmd WinClosed * 2 wincmd c
755 augroup END
756 close
757 call assert_equal(1, winnr('$'))
758 call assert_equal(1, g:triggered)
759
760 autocmd! test-WinClosed
761 augroup! test-WinClosed
762 unlet g:matched
763 unlet g:amatch
764 unlet g:afile
765 unlet g:triggered
766endfunc
767
Bram Moolenaarc947b9a2022-04-06 17:59:21 +0100768func Test_WinClosed_throws()
769 vnew
770 let bnr = bufnr()
771 call assert_equal(1, bufloaded(bnr))
772 augroup test-WinClosed
773 autocmd WinClosed * throw 'foo'
774 augroup END
775 try
776 close
777 catch /.*/
778 endtry
779 call assert_equal(0, bufloaded(bnr))
780
781 autocmd! test-WinClosed
782 augroup! test-WinClosed
783endfunc
784
zeertzjq6a069402022-04-07 14:08:29 +0100785func Test_WinClosed_throws_with_tabs()
786 tabnew
787 let bnr = bufnr()
788 call assert_equal(1, bufloaded(bnr))
789 augroup test-WinClosed
790 autocmd WinClosed * throw 'foo'
791 augroup END
792 try
793 close
794 catch /.*/
795 endtry
796 call assert_equal(0, bufloaded(bnr))
797
798 autocmd! test-WinClosed
799 augroup! test-WinClosed
800endfunc
801
zeertzjq62de54b2022-09-22 18:08:37 +0100802" This used to trigger WinClosed twice for the same window, and the window's
803" buffer was NULL in the second autocommand.
804func Test_WinClosed_switch_tab()
805 edit Xa
806 split Xb
807 split Xc
808 tab split
809 new
810 augroup test-WinClosed
811 autocmd WinClosed * tabprev | bwipe!
812 augroup END
813 close
814 " Check that the tabline has been fully removed
815 call assert_equal([1, 1], win_screenpos(0))
816
817 autocmd! test-WinClosed
818 augroup! test-WinClosed
819 %bwipe!
820endfunc
821
Bram Moolenaare99e8442016-07-26 20:43:40 +0200822func s:AddAnAutocmd()
823 augroup vimBarTest
824 au BufReadCmd * echo 'hello'
825 augroup END
826 call assert_equal(3, len(split(execute('au vimBarTest'), "\n")))
827endfunc
828
829func Test_early_bar()
830 " test that a bar is recognized before the {event}
831 call s:AddAnAutocmd()
Bram Moolenaarb8e642f2021-11-20 10:38:25 +0000832 augroup vimBarTest | au! | let done = 77 | augroup END
Bram Moolenaare99e8442016-07-26 20:43:40 +0200833 call assert_equal(1, len(split(execute('au vimBarTest'), "\n")))
Bram Moolenaarb8e642f2021-11-20 10:38:25 +0000834 call assert_equal(77, done)
Bram Moolenaare99e8442016-07-26 20:43:40 +0200835
836 call s:AddAnAutocmd()
Bram Moolenaarb8e642f2021-11-20 10:38:25 +0000837 augroup vimBarTest| au!| let done = 88 | augroup END
Bram Moolenaare99e8442016-07-26 20:43:40 +0200838 call assert_equal(1, len(split(execute('au vimBarTest'), "\n")))
Bram Moolenaarb8e642f2021-11-20 10:38:25 +0000839 call assert_equal(88, done)
Bram Moolenaare99e8442016-07-26 20:43:40 +0200840
841 " test that a bar is recognized after the {event}
842 call s:AddAnAutocmd()
Bram Moolenaarb8e642f2021-11-20 10:38:25 +0000843 augroup vimBarTest| au!BufReadCmd| let done = 99 | augroup END
Bram Moolenaare99e8442016-07-26 20:43:40 +0200844 call assert_equal(1, len(split(execute('au vimBarTest'), "\n")))
Bram Moolenaarb8e642f2021-11-20 10:38:25 +0000845 call assert_equal(99, done)
Bram Moolenaare99e8442016-07-26 20:43:40 +0200846
847 " test that a bar is recognized after the {group}
848 call s:AddAnAutocmd()
849 au! vimBarTest|echo 'hello'
850 call assert_equal(1, len(split(execute('au vimBarTest'), "\n")))
851endfunc
Bram Moolenaarf2c4c392016-07-29 20:50:24 +0200852
Bram Moolenaar5c809082016-09-01 16:21:48 +0200853func RemoveGroup()
854 autocmd! StartOK
855 augroup! StartOK
856endfunc
857
Bram Moolenaarf2c4c392016-07-29 20:50:24 +0200858func Test_augroup_warning()
859 augroup TheWarning
860 au VimEnter * echo 'entering'
861 augroup END
Bram Moolenaar5dc4e2f2020-11-25 14:15:12 +0100862 call assert_match("TheWarning.*VimEnter", execute('au VimEnter'))
Bram Moolenaarf2c4c392016-07-29 20:50:24 +0200863 redir => res
864 augroup! TheWarning
865 redir END
Bram Moolenaar5dc4e2f2020-11-25 14:15:12 +0100866 call assert_match("W19:", res)
867 call assert_match("-Deleted-.*VimEnter", execute('au VimEnter'))
Bram Moolenaarf2c4c392016-07-29 20:50:24 +0200868
869 " check "Another" does not take the pace of the deleted entry
870 augroup Another
871 augroup END
Bram Moolenaar5dc4e2f2020-11-25 14:15:12 +0100872 call assert_match("-Deleted-.*VimEnter", execute('au VimEnter'))
Bram Moolenaaraeac9002016-09-06 22:15:08 +0200873 augroup! Another
Bram Moolenaar5c809082016-09-01 16:21:48 +0200874
875 " no warning for postpone aucmd delete
876 augroup StartOK
877 au VimEnter * call RemoveGroup()
878 augroup END
Bram Moolenaar5dc4e2f2020-11-25 14:15:12 +0100879 call assert_match("StartOK.*VimEnter", execute('au VimEnter'))
Bram Moolenaar5c809082016-09-01 16:21:48 +0200880 redir => res
881 doautocmd VimEnter
882 redir END
Bram Moolenaar5dc4e2f2020-11-25 14:15:12 +0100883 call assert_notmatch("W19:", res)
Bram Moolenaarde653f02016-09-03 16:59:06 +0200884 au! VimEnter
Bram Moolenaarad48e6c2020-04-21 22:19:45 +0200885
886 call assert_fails('augroup!', 'E471:')
Bram Moolenaarf2c4c392016-07-29 20:50:24 +0200887endfunc
Bram Moolenaarb62cc362016-09-03 16:43:53 +0200888
Bram Moolenaar8d84ff12017-10-26 16:42:16 +0200889func Test_BufReadCmdHelp()
890 " This used to cause access to free memory
891 au BufReadCmd * e +h
892 help
893
Bram Moolenaar8d84ff12017-10-26 16:42:16 +0200894 au! BufReadCmd
895endfunc
896
897func Test_BufReadCmdHelpJump()
898 " This used to cause access to free memory
899 au BufReadCmd * e +h{
Bram Moolenaarcf1ba352017-10-27 00:55:04 +0200900 " } to fix highlighting
901 call assert_fails('help', 'E434:')
Bram Moolenaar8d84ff12017-10-26 16:42:16 +0200902
Bram Moolenaar8d84ff12017-10-26 16:42:16 +0200903 au! BufReadCmd
904endfunc
905
zeertzjq93f72cc2022-08-26 15:34:52 +0100906" BufReadCmd is triggered for a "nofile" buffer. Check all values.
Bram Moolenaarb1d2c812022-08-26 11:55:01 +0100907func Test_BufReadCmdNofile()
zeertzjq93f72cc2022-08-26 15:34:52 +0100908 for val in ['nofile',
909 \ 'nowrite',
910 \ 'acwrite',
911 \ 'quickfix',
912 \ 'help',
913 \ 'terminal',
914 \ 'prompt',
915 \ 'popup',
916 \ ]
917 new somefile
918 exe 'set buftype=' .. val
919 au BufReadCmd somefile call setline(1, 'triggered')
920 edit
921 call assert_equal('triggered', getline(1))
Bram Moolenaarb1d2c812022-08-26 11:55:01 +0100922
zeertzjq93f72cc2022-08-26 15:34:52 +0100923 au! BufReadCmd
924 bwipe!
925 endfor
Bram Moolenaarb1d2c812022-08-26 11:55:01 +0100926endfunc
927
Bram Moolenaarb62cc362016-09-03 16:43:53 +0200928func Test_augroup_deleted()
Bram Moolenaarde653f02016-09-03 16:59:06 +0200929 " This caused a crash before E936 was introduced
Bram Moolenaarb62cc362016-09-03 16:43:53 +0200930 augroup x
Bram Moolenaarde653f02016-09-03 16:59:06 +0200931 call assert_fails('augroup! x', 'E936:')
932 au VimEnter * echo
933 augroup end
Bram Moolenaarb62cc362016-09-03 16:43:53 +0200934 augroup! x
Bram Moolenaar5dc4e2f2020-11-25 14:15:12 +0100935 call assert_match("-Deleted-.*VimEnter", execute('au VimEnter'))
Bram Moolenaarde653f02016-09-03 16:59:06 +0200936 au! VimEnter
Bram Moolenaarb62cc362016-09-03 16:43:53 +0200937endfunc
938
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200939" Tests for autocommands on :close command.
940" This used to be in test13.
941func Test_three_windows()
Bram Moolenaarb3435b02016-09-29 20:54:59 +0200942 " Clean up buffers, because in some cases this function fails.
943 call s:cleanup_buffers()
944
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200945 " Write three files and open them, each in a window.
946 " Then go to next window, with autocommand that deletes the previous one.
947 " Do this twice, writing the file.
948 e! Xtestje1
949 call setline(1, 'testje1')
950 w
951 sp Xtestje2
952 call setline(1, 'testje2')
953 w
954 sp Xtestje3
955 call setline(1, 'testje3')
956 w
957 wincmd w
958 au WinLeave Xtestje2 bwipe
959 wincmd w
960 call assert_equal('Xtestje1', expand('%'))
961
962 au WinLeave Xtestje1 bwipe Xtestje3
963 close
964 call assert_equal('Xtestje1', expand('%'))
965
966 " Test deleting the buffer on a Unload event. If this goes wrong there
967 " will be the ATTENTION prompt.
968 e Xtestje1
969 au!
970 au! BufUnload Xtestje1 bwipe
971 call assert_fails('e Xtestje3', 'E937:')
972 call assert_equal('Xtestje3', expand('%'))
973
974 e Xtestje2
975 sp Xtestje1
976 call assert_fails('e', 'E937:')
Bram Moolenaara997b452018-04-17 23:24:06 +0200977 call assert_equal('Xtestje1', expand('%'))
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200978
979 " Test changing buffers in a BufWipeout autocommand. If this goes wrong
980 " there are ml_line errors and/or a Crash.
981 au!
982 only
983 e Xanother
984 e Xtestje1
985 bwipe Xtestje2
986 bwipe Xtestje3
987 au BufWipeout Xtestje1 buf Xtestje1
988 bwipe
989 call assert_equal('Xanother', expand('%'))
990
991 only
992 help
993 wincmd w
994 1quit
995 call assert_equal('Xanother', expand('%'))
996
997 au!
Bram Moolenaar4520d442017-03-19 16:09:46 +0100998 enew
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200999 call delete('Xtestje1')
1000 call delete('Xtestje2')
1001 call delete('Xtestje3')
1002endfunc
Bram Moolenaare13b9af2017-01-13 22:01:02 +01001003
1004func Test_BufEnter()
1005 au! BufEnter
1006 au Bufenter * let val = val . '+'
1007 let g:val = ''
1008 split NewFile
1009 call assert_equal('+', g:val)
1010 bwipe!
1011 call assert_equal('++', g:val)
1012
1013 " Also get BufEnter when editing a directory
Bram Moolenaar6f14da12022-09-07 21:30:44 +01001014 call mkdir('Xbufenterdir', 'D')
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +01001015 split Xbufenterdir
Bram Moolenaare13b9af2017-01-13 22:01:02 +01001016 call assert_equal('+++', g:val)
Bram Moolenaare94260f2017-03-21 15:50:12 +01001017
1018 " On MS-Windows we can't edit the directory, make sure we wipe the right
1019 " buffer.
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +01001020 bwipe! Xbufenterdir
Bram Moolenaare13b9af2017-01-13 22:01:02 +01001021 au! BufEnter
Bram Moolenaara9b5b852022-08-26 13:16:20 +01001022
1023 " Editing a "nofile" buffer doesn't read the file but does trigger BufEnter
zeertzjq93f72cc2022-08-26 15:34:52 +01001024 " for historic reasons. Also test other 'buftype' values.
1025 for val in ['nofile',
1026 \ 'nowrite',
1027 \ 'acwrite',
1028 \ 'quickfix',
1029 \ 'help',
1030 \ 'terminal',
1031 \ 'prompt',
1032 \ 'popup',
1033 \ ]
1034 new somefile
1035 exe 'set buftype=' .. val
1036 au BufEnter somefile call setline(1, 'some text')
1037 edit
1038 call assert_equal('some text', getline(1))
1039 bwipe!
1040 au! BufEnter
1041 endfor
Bram Moolenaar9fda8152022-11-19 13:14:10 +00001042
1043 new
1044 new
1045 autocmd BufEnter * ++once close
1046 call assert_fails('close', 'E1312:')
1047
1048 au! BufEnter
1049 only
Bram Moolenaare13b9af2017-01-13 22:01:02 +01001050endfunc
Bram Moolenaar8c752bd2017-03-19 17:09:56 +01001051
1052" Closing a window might cause an endless loop
1053" E814 for older Vims
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001054func Test_autocmd_bufwipe_in_SessLoadPost()
Bram Moolenaar1d68d9b2017-10-13 22:33:32 +02001055 edit Xtest
Bram Moolenaar8c752bd2017-03-19 17:09:56 +01001056 tabnew
Bram Moolenaar1d68d9b2017-10-13 22:33:32 +02001057 file Xsomething
Bram Moolenaar8c752bd2017-03-19 17:09:56 +01001058 set noswapfile
Bram Moolenaar8c752bd2017-03-19 17:09:56 +01001059 mksession!
1060
Bram Moolenaarc79745a2019-05-20 22:12:34 +02001061 let content =<< trim [CODE]
Bram Moolenaar62cd26a2020-10-11 20:08:44 +02001062 call test_override('ui_delay', 10)
Bram Moolenaarc79745a2019-05-20 22:12:34 +02001063 set nocp noswapfile
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001064 let v:swapchoice = "e"
Bram Moolenaarc79745a2019-05-20 22:12:34 +02001065 augroup test_autocmd_sessionload
1066 autocmd!
1067 autocmd SessionLoadPost * exe bufnr("Xsomething") . "bw!"
1068 augroup END
1069
1070 func WriteErrors()
Bram Moolenaarbe9fc5b2022-09-09 17:09:35 +01001071 call writefile([execute("messages")], "XerrorsBwipe")
Bram Moolenaarc79745a2019-05-20 22:12:34 +02001072 endfunc
1073 au VimLeave * call WriteErrors()
1074 [CODE]
1075
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01001076 call writefile(content, 'Xvimrc', 'D')
Bram Moolenaar93344c22019-08-14 21:12:05 +02001077 call system(GetVimCommand('Xvimrc') .. ' --not-a-term --noplugins -S Session.vim -c cq')
Bram Moolenaarae04a602022-09-09 15:08:10 +01001078 sleep 100m
Bram Moolenaarbe9fc5b2022-09-09 17:09:35 +01001079 let errors = join(readfile('XerrorsBwipe'))
Bram Moolenaare2e40752020-09-04 21:18:46 +02001080 call assert_match('E814:', errors)
Bram Moolenaar8c752bd2017-03-19 17:09:56 +01001081
Bram Moolenaar8c752bd2017-03-19 17:09:56 +01001082 set swapfile
Bram Moolenaarbe9fc5b2022-09-09 17:09:35 +01001083 for file in ['Session.vim', 'XerrorsBwipe']
Bram Moolenaar8c752bd2017-03-19 17:09:56 +01001084 call delete(file)
1085 endfor
1086endfunc
1087
Bram Moolenaar797e63b2021-01-15 16:22:52 +01001088" Using :blast and :ball for many events caused a crash, because b_nwindows was
1089" not incremented correctly.
1090func Test_autocmd_blast_badd()
1091 let content =<< trim [CODE]
1092 au BufNew,BufAdd,BufWinEnter,BufEnter,BufLeave,BufWinLeave,BufUnload,VimEnter foo* blast
1093 edit foo1
1094 au BufNew,BufAdd,BufWinEnter,BufEnter,BufLeave,BufWinLeave,BufUnload,VimEnter foo* ball
1095 edit foo2
Bram Moolenaarbe9fc5b2022-09-09 17:09:35 +01001096 call writefile(['OK'], 'XerrorsBlast')
Bram Moolenaar797e63b2021-01-15 16:22:52 +01001097 qall
1098 [CODE]
1099
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01001100 call writefile(content, 'XblastBall', 'D')
Bram Moolenaar797e63b2021-01-15 16:22:52 +01001101 call system(GetVimCommand() .. ' --clean -S XblastBall')
Bram Moolenaarae04a602022-09-09 15:08:10 +01001102 sleep 100m
Bram Moolenaarbe9fc5b2022-09-09 17:09:35 +01001103 call assert_match('OK', readfile('XerrorsBlast')->join())
Bram Moolenaar797e63b2021-01-15 16:22:52 +01001104
Bram Moolenaarbe9fc5b2022-09-09 17:09:35 +01001105 call delete('XerrorsBlast')
Bram Moolenaar797e63b2021-01-15 16:22:52 +01001106endfunc
1107
Bram Moolenaar8c752bd2017-03-19 17:09:56 +01001108" SEGV occurs in older versions.
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001109func Test_autocmd_bufwipe_in_SessLoadPost2()
Bram Moolenaar8c752bd2017-03-19 17:09:56 +01001110 tabnew
1111 set noswapfile
Bram Moolenaar8c752bd2017-03-19 17:09:56 +01001112 mksession!
1113
Bram Moolenaarc79745a2019-05-20 22:12:34 +02001114 let content =<< trim [CODE]
1115 set nocp noswapfile
1116 function! DeleteInactiveBufs()
1117 tabfirst
1118 let tabblist = []
1119 for i in range(1, tabpagenr(''$''))
1120 call extend(tabblist, tabpagebuflist(i))
1121 endfor
1122 for b in range(1, bufnr(''$''))
1123 if bufexists(b) && buflisted(b) && (index(tabblist, b) == -1 || bufname(b) =~# ''^$'')
1124 exec ''bwipeout '' . b
1125 endif
1126 endfor
1127 echomsg "SessionLoadPost DONE"
1128 endfunction
1129 au SessionLoadPost * call DeleteInactiveBufs()
1130
1131 func WriteErrors()
Bram Moolenaarbe9fc5b2022-09-09 17:09:35 +01001132 call writefile([execute("messages")], "XerrorsPost")
Bram Moolenaarc79745a2019-05-20 22:12:34 +02001133 endfunc
1134 au VimLeave * call WriteErrors()
1135 [CODE]
1136
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01001137 call writefile(content, 'Xvimrc', 'D')
Bram Moolenaar93344c22019-08-14 21:12:05 +02001138 call system(GetVimCommand('Xvimrc') .. ' --not-a-term --noplugins -S Session.vim -c cq')
Bram Moolenaarae04a602022-09-09 15:08:10 +01001139 sleep 100m
Bram Moolenaarbe9fc5b2022-09-09 17:09:35 +01001140 let errors = join(readfile('XerrorsPost'))
Bram Moolenaare94260f2017-03-21 15:50:12 +01001141 " This probably only ever matches on unix.
1142 call assert_notmatch('Caught deadly signal SEGV', errors)
1143 call assert_match('SessionLoadPost DONE', errors)
Bram Moolenaar8c752bd2017-03-19 17:09:56 +01001144
Bram Moolenaar8c752bd2017-03-19 17:09:56 +01001145 set swapfile
Bram Moolenaarbe9fc5b2022-09-09 17:09:35 +01001146 for file in ['Session.vim', 'XerrorsPost']
Bram Moolenaar8c752bd2017-03-19 17:09:56 +01001147 call delete(file)
1148 endfor
1149endfunc
Bram Moolenaarfaf29d72017-07-09 11:07:16 +02001150
1151func Test_empty_doau()
1152 doau \|
1153endfunc
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001154
1155func s:AutoCommandOptionSet(match)
Bram Moolenaard7c96872019-06-15 17:12:48 +02001156 let template = "Option: <%s>, OldVal: <%s>, OldValLocal: <%s>, OldValGlobal: <%s>, NewVal: <%s>, Scope: <%s>, Command: <%s>\n"
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001157 let item = remove(g:options, 0)
Bram Moolenaard7c96872019-06-15 17:12:48 +02001158 let expected = printf(template, item[0], item[1], item[2], item[3], item[4], item[5], item[6])
1159 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 +02001160 let g:opt = [expected, actual]
1161 "call assert_equal(expected, actual)
1162endfunc
1163
1164func Test_OptionSet()
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +02001165 CheckOption autochdir
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001166
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +02001167 badd test_autocmd.vim
1168
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001169 call test_override('starting', 1)
1170 set nocp
1171 au OptionSet * :call s:AutoCommandOptionSet(expand("<amatch>"))
1172
1173 " 1: Setting number option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001174 let g:options = [['number', 0, 0, 0, 1, 'global', 'set']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001175 set nu
1176 call assert_equal([], g:options)
1177 call assert_equal(g:opt[0], g:opt[1])
1178
1179 " 2: Setting local number option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001180 let g:options = [['number', 1, 1, '', 0, 'local', 'setlocal']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001181 setlocal nonu
1182 call assert_equal([], g:options)
1183 call assert_equal(g:opt[0], g:opt[1])
1184
1185 " 3: Setting global number option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001186 let g:options = [['number', 1, '', 1, 0, 'global', 'setglobal']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001187 setglobal nonu
1188 call assert_equal([], g:options)
1189 call assert_equal(g:opt[0], g:opt[1])
1190
1191 " 4: Setting local autoindent option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001192 let g:options = [['autoindent', 0, 0, '', 1, 'local', 'setlocal']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001193 setlocal ai
1194 call assert_equal([], g:options)
1195 call assert_equal(g:opt[0], g:opt[1])
1196
1197 " 5: Setting global autoindent option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001198 let g:options = [['autoindent', 0, '', 0, 1, 'global', 'setglobal']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001199 setglobal ai
1200 call assert_equal([], g:options)
1201 call assert_equal(g:opt[0], g:opt[1])
1202
1203 " 6: Setting global autoindent option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001204 let g:options = [['autoindent', 1, 1, 1, 0, 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001205 set ai!
1206 call assert_equal([], g:options)
1207 call assert_equal(g:opt[0], g:opt[1])
1208
1209 " 6a: Setting global autoindent option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001210 let g:options = [['autoindent', 1, 1, 0, 0, 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001211 noa setlocal ai
1212 noa setglobal noai
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001213 set ai!
1214 call assert_equal([], g:options)
1215 call assert_equal(g:opt[0], g:opt[1])
1216
1217 " Should not print anything, use :noa
1218 " 7: don't trigger OptionSet"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001219 let g:options = [['invalid', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001220 noa set nonu
Bram Moolenaard7c96872019-06-15 17:12:48 +02001221 call assert_equal([['invalid', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid']], g:options)
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001222 call assert_equal(g:opt[0], g:opt[1])
1223
1224 " 8: Setting several global list and number option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001225 let g:options = [['list', 0, 0, 0, 1, 'global', 'set'], ['number', 0, 0, 0, 1, 'global', 'set']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001226 set list nu
1227 call assert_equal([], g:options)
1228 call assert_equal(g:opt[0], g:opt[1])
1229
1230 " 9: don't trigger OptionSet"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001231 let g:options = [['invalid', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid'], ['invalid', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001232 noa set nolist nonu
Bram Moolenaard7c96872019-06-15 17:12:48 +02001233 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 +02001234 call assert_equal(g:opt[0], g:opt[1])
1235
1236 " 10: Setting global acd"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001237 let g:options = [['autochdir', 0, 0, '', 1, 'local', 'setlocal']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001238 setlocal acd
1239 call assert_equal([], g:options)
1240 call assert_equal(g:opt[0], g:opt[1])
1241
1242 " 11: Setting global autoread (also sets local value)"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001243 let g:options = [['autoread', 0, 0, 0, 1, 'global', 'set']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001244 set ar
1245 call assert_equal([], g:options)
1246 call assert_equal(g:opt[0], g:opt[1])
1247
1248 " 12: Setting local autoread"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001249 let g:options = [['autoread', 1, 1, '', 1, 'local', 'setlocal']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001250 setlocal ar
1251 call assert_equal([], g:options)
1252 call assert_equal(g:opt[0], g:opt[1])
1253
1254 " 13: Setting global autoread"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001255 let g:options = [['autoread', 1, '', 1, 0, 'global', 'setglobal']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001256 setglobal invar
1257 call assert_equal([], g:options)
1258 call assert_equal(g:opt[0], g:opt[1])
1259
1260 " 14: Setting option backspace through :let"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001261 let g:options = [['backspace', '', '', '', 'eol,indent,start', 'global', 'set']]
1262 let &bs = "eol,indent,start"
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001263 call assert_equal([], g:options)
1264 call assert_equal(g:opt[0], g:opt[1])
1265
1266 " 15: Setting option backspace through setbufvar()"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001267 let g:options = [['backup', 0, 0, '', 1, 'local', 'setlocal']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001268 " try twice, first time, shouldn't trigger because option name is invalid,
1269 " second time, it should trigger
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +02001270 let bnum = bufnr('%')
Bram Moolenaare2e40752020-09-04 21:18:46 +02001271 call assert_fails("call setbufvar(bnum, '&l:bk', 1)", 'E355:')
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001272 " should trigger, use correct option name
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +02001273 call setbufvar(bnum, '&backup', 1)
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001274 call assert_equal([], g:options)
1275 call assert_equal(g:opt[0], g:opt[1])
1276
1277 " 16: Setting number option using setwinvar"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001278 let g:options = [['number', 0, 0, '', 1, 'local', 'setlocal']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001279 call setwinvar(0, '&number', 1)
1280 call assert_equal([], g:options)
1281 call assert_equal(g:opt[0], g:opt[1])
1282
1283 " 17: Setting key option, shouldn't trigger"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001284 let g:options = [['key', 'invalid', 'invalid1', 'invalid2', 'invalid3', 'invalid4', 'invalid5']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001285 setlocal key=blah
1286 setlocal key=
Bram Moolenaard7c96872019-06-15 17:12:48 +02001287 call assert_equal([['key', 'invalid', 'invalid1', 'invalid2', 'invalid3', 'invalid4', 'invalid5']], g:options)
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001288 call assert_equal(g:opt[0], g:opt[1])
1289
Bram Moolenaard7c96872019-06-15 17:12:48 +02001290
1291 " 18a: Setting string global option"
1292 let oldval = &backupext
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001293 let g:options = [['backupext', oldval, oldval, oldval, 'foo', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001294 set backupext=foo
1295 call assert_equal([], g:options)
1296 call assert_equal(g:opt[0], g:opt[1])
1297
1298 " 18b: Resetting string global option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001299 let g:options = [['backupext', 'foo', 'foo', 'foo', oldval, 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001300 set backupext&
1301 call assert_equal([], g:options)
1302 call assert_equal(g:opt[0], g:opt[1])
1303
1304 " 18c: Setting global string global option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001305 let g:options = [['backupext', oldval, '', oldval, 'bar', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001306 setglobal backupext=bar
1307 call assert_equal([], g:options)
1308 call assert_equal(g:opt[0], g:opt[1])
1309
1310 " 18d: Setting local string global option"
1311 " As this is a global option this sets the global value even though
1312 " :setlocal is used!
1313 noa set backupext& " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001314 let g:options = [['backupext', oldval, oldval, '', 'baz', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001315 setlocal backupext=baz
1316 call assert_equal([], g:options)
1317 call assert_equal(g:opt[0], g:opt[1])
1318
1319 " 18e: Setting again string global option"
1320 noa setglobal backupext=ext_global " Reset global and local value (without triggering autocmd)
1321 noa setlocal backupext=ext_local " Sets the global(!) value!
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001322 let g:options = [['backupext', 'ext_local', 'ext_local', 'ext_local', 'fuu', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001323 set backupext=fuu
1324 call assert_equal([], g:options)
1325 call assert_equal(g:opt[0], g:opt[1])
1326
1327
zeertzjqb811de52021-10-21 10:50:44 +01001328 " 19a: Setting string global-local (to buffer) option"
Bram Moolenaar8efa0262017-08-20 15:47:20 +02001329 let oldval = &tags
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001330 let g:options = [['tags', oldval, oldval, oldval, 'tagpath', 'global', 'set']]
Bram Moolenaar8efa0262017-08-20 15:47:20 +02001331 set tags=tagpath
1332 call assert_equal([], g:options)
1333 call assert_equal(g:opt[0], g:opt[1])
1334
zeertzjqb811de52021-10-21 10:50:44 +01001335 " 19b: Resetting string global-local (to buffer) option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001336 let g:options = [['tags', 'tagpath', 'tagpath', 'tagpath', oldval, 'global', 'set']]
Bram Moolenaar8efa0262017-08-20 15:47:20 +02001337 set tags&
1338 call assert_equal([], g:options)
1339 call assert_equal(g:opt[0], g:opt[1])
1340
zeertzjqb811de52021-10-21 10:50:44 +01001341 " 19c: Setting global string global-local (to buffer) option "
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001342 let g:options = [['tags', oldval, '', oldval, 'tagpath1', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001343 setglobal tags=tagpath1
1344 call assert_equal([], g:options)
1345 call assert_equal(g:opt[0], g:opt[1])
1346
zeertzjqb811de52021-10-21 10:50:44 +01001347 " 19d: Setting local string global-local (to buffer) option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001348 let g:options = [['tags', 'tagpath1', 'tagpath1', '', 'tagpath2', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001349 setlocal tags=tagpath2
1350 call assert_equal([], g:options)
1351 call assert_equal(g:opt[0], g:opt[1])
1352
zeertzjqb811de52021-10-21 10:50:44 +01001353 " 19e: Setting again string global-local (to buffer) option"
1354 " Note: v:option_old is the old global value for global-local string options
Bram Moolenaard7c96872019-06-15 17:12:48 +02001355 " but the old local value for all other kinds of options.
1356 noa setglobal tags=tag_global " Reset global and local value (without triggering autocmd)
1357 noa setlocal tags=tag_local
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001358 let g:options = [['tags', 'tag_global', 'tag_local', 'tag_global', 'tagpath', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001359 set tags=tagpath
1360 call assert_equal([], g:options)
1361 call assert_equal(g:opt[0], g:opt[1])
1362
zeertzjqb811de52021-10-21 10:50:44 +01001363 " 19f: Setting string global-local (to buffer) option to an empty string"
1364 " Note: v:option_old is the old global value for global-local string options
Bram Moolenaard7c96872019-06-15 17:12:48 +02001365 " but the old local value for all other kinds of options.
1366 noa set tags=tag_global " Reset global and local value (without triggering autocmd)
1367 noa setlocal tags= " empty string
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001368 let g:options = [['tags', 'tag_global', '', 'tag_global', 'tagpath', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001369 set tags=tagpath
1370 call assert_equal([], g:options)
1371 call assert_equal(g:opt[0], g:opt[1])
1372
1373
1374 " 20a: Setting string local (to buffer) option"
1375 let oldval = &spelllang
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001376 let g:options = [['spelllang', oldval, oldval, oldval, 'elvish,klingon', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001377 set spelllang=elvish,klingon
1378 call assert_equal([], g:options)
1379 call assert_equal(g:opt[0], g:opt[1])
1380
1381 " 20b: Resetting string local (to buffer) option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001382 let g:options = [['spelllang', 'elvish,klingon', 'elvish,klingon', 'elvish,klingon', oldval, 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001383 set spelllang&
1384 call assert_equal([], g:options)
1385 call assert_equal(g:opt[0], g:opt[1])
1386
1387 " 20c: Setting global string local (to buffer) option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001388 let g:options = [['spelllang', oldval, '', oldval, 'elvish', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001389 setglobal spelllang=elvish
1390 call assert_equal([], g:options)
1391 call assert_equal(g:opt[0], g:opt[1])
1392
1393 " 20d: Setting local string local (to buffer) option"
1394 noa set spelllang& " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001395 let g:options = [['spelllang', oldval, oldval, '', 'klingon', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001396 setlocal spelllang=klingon
1397 call assert_equal([], g:options)
1398 call assert_equal(g:opt[0], g:opt[1])
1399
1400 " 20e: Setting again string local (to buffer) option"
zeertzjqb811de52021-10-21 10:50:44 +01001401 " Note: v:option_old is the old global value for global-local string options
Bram Moolenaard7c96872019-06-15 17:12:48 +02001402 " but the old local value for all other kinds of options.
1403 noa setglobal spelllang=spellglobal " Reset global and local value (without triggering autocmd)
1404 noa setlocal spelllang=spelllocal
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001405 let g:options = [['spelllang', 'spelllocal', 'spelllocal', 'spellglobal', 'foo', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001406 set spelllang=foo
1407 call assert_equal([], g:options)
1408 call assert_equal(g:opt[0], g:opt[1])
1409
1410
zeertzjqb811de52021-10-21 10:50:44 +01001411 " 21a: Setting string global-local (to window) option"
Bram Moolenaard7c96872019-06-15 17:12:48 +02001412 let oldval = &statusline
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001413 let g:options = [['statusline', oldval, oldval, oldval, 'foo', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001414 set statusline=foo
1415 call assert_equal([], g:options)
1416 call assert_equal(g:opt[0], g:opt[1])
1417
zeertzjqb811de52021-10-21 10:50:44 +01001418 " 21b: Resetting string global-local (to window) option"
1419 " Note: v:option_old is the old global value for global-local string options
Bram Moolenaard7c96872019-06-15 17:12:48 +02001420 " but the old local value for all other kinds of options.
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001421 let g:options = [['statusline', 'foo', 'foo', 'foo', oldval, 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001422 set statusline&
1423 call assert_equal([], g:options)
1424 call assert_equal(g:opt[0], g:opt[1])
1425
zeertzjqb811de52021-10-21 10:50:44 +01001426 " 21c: Setting global string global-local (to window) option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001427 let g:options = [['statusline', oldval, '', oldval, 'bar', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001428 setglobal statusline=bar
1429 call assert_equal([], g:options)
1430 call assert_equal(g:opt[0], g:opt[1])
1431
zeertzjqb811de52021-10-21 10:50:44 +01001432 " 21d: Setting local string global-local (to window) option"
Bram Moolenaard7c96872019-06-15 17:12:48 +02001433 noa set statusline& " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001434 let g:options = [['statusline', oldval, oldval, '', 'baz', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001435 setlocal statusline=baz
1436 call assert_equal([], g:options)
1437 call assert_equal(g:opt[0], g:opt[1])
1438
zeertzjqb811de52021-10-21 10:50:44 +01001439 " 21e: Setting again string global-local (to window) option"
1440 " Note: v:option_old is the old global value for global-local string options
Bram Moolenaard7c96872019-06-15 17:12:48 +02001441 " but the old local value for all other kinds of options.
1442 noa setglobal statusline=bar " Reset global and local value (without triggering autocmd)
1443 noa setlocal statusline=baz
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001444 let g:options = [['statusline', 'bar', 'baz', 'bar', 'foo', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001445 set statusline=foo
1446 call assert_equal([], g:options)
1447 call assert_equal(g:opt[0], g:opt[1])
1448
1449
1450 " 22a: Setting string local (to window) option"
1451 let oldval = &foldignore
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001452 let g:options = [['foldignore', oldval, oldval, oldval, 'fo', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001453 set foldignore=fo
1454 call assert_equal([], g:options)
1455 call assert_equal(g:opt[0], g:opt[1])
1456
1457 " 22b: Resetting string local (to window) option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001458 let g:options = [['foldignore', 'fo', 'fo', 'fo', oldval, 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001459 set foldignore&
1460 call assert_equal([], g:options)
1461 call assert_equal(g:opt[0], g:opt[1])
1462
1463 " 22c: Setting global string local (to window) option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001464 let g:options = [['foldignore', oldval, '', oldval, 'bar', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001465 setglobal foldignore=bar
1466 call assert_equal([], g:options)
1467 call assert_equal(g:opt[0], g:opt[1])
1468
1469 " 22d: Setting local string local (to window) option"
1470 noa set foldignore& " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001471 let g:options = [['foldignore', oldval, oldval, '', 'baz', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001472 setlocal foldignore=baz
1473 call assert_equal([], g:options)
1474 call assert_equal(g:opt[0], g:opt[1])
1475
1476 " 22e: Setting again string local (to window) option"
1477 noa setglobal foldignore=glob " Reset global and local value (without triggering autocmd)
1478 noa setlocal foldignore=loc
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001479 let g:options = [['foldignore', 'loc', 'loc', 'glob', 'fo', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001480 set foldignore=fo
1481 call assert_equal([], g:options)
1482 call assert_equal(g:opt[0], g:opt[1])
1483
1484
zeertzjqb811de52021-10-21 10:50:44 +01001485 " 23a: Setting global number global option"
Bram Moolenaard7c96872019-06-15 17:12:48 +02001486 noa setglobal cmdheight=8 " Reset global and local value (without triggering autocmd)
1487 noa setlocal cmdheight=1 " Sets the global(!) value!
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001488 let g:options = [['cmdheight', '1', '', '1', '2', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001489 setglobal cmdheight=2
1490 call assert_equal([], g:options)
1491 call assert_equal(g:opt[0], g:opt[1])
1492
1493 " 23b: Setting local number global option"
1494 noa setglobal cmdheight=8 " Reset global and local value (without triggering autocmd)
1495 noa setlocal cmdheight=1 " Sets the global(!) value!
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001496 let g:options = [['cmdheight', '1', '1', '', '2', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001497 setlocal cmdheight=2
1498 call assert_equal([], g:options)
1499 call assert_equal(g:opt[0], g:opt[1])
1500
1501 " 23c: Setting again number global option"
1502 noa setglobal cmdheight=8 " Reset global and local value (without triggering autocmd)
1503 noa setlocal cmdheight=1 " Sets the global(!) value!
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001504 let g:options = [['cmdheight', '1', '1', '1', '2', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001505 set cmdheight=2
1506 call assert_equal([], g:options)
1507 call assert_equal(g:opt[0], g:opt[1])
1508
1509 " 23d: Setting again number global option"
1510 noa set cmdheight=8 " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001511 let g:options = [['cmdheight', '8', '8', '8', '2', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001512 set cmdheight=2
1513 call assert_equal([], g:options)
1514 call assert_equal(g:opt[0], g:opt[1])
1515
1516
1517 " 24a: Setting global number global-local (to buffer) option"
1518 noa setglobal undolevels=8 " Reset global and local value (without triggering autocmd)
1519 noa setlocal undolevels=1
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001520 let g:options = [['undolevels', '8', '', '8', '2', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001521 setglobal undolevels=2
1522 call assert_equal([], g:options)
1523 call assert_equal(g:opt[0], g:opt[1])
1524
1525 " 24b: Setting local number global-local (to buffer) option"
1526 noa setglobal undolevels=8 " Reset global and local value (without triggering autocmd)
1527 noa setlocal undolevels=1
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001528 let g:options = [['undolevels', '1', '1', '', '2', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001529 setlocal undolevels=2
1530 call assert_equal([], g:options)
1531 call assert_equal(g:opt[0], g:opt[1])
1532
1533 " 24c: Setting again number global-local (to buffer) option"
1534 noa setglobal undolevels=8 " Reset global and local value (without triggering autocmd)
1535 noa setlocal undolevels=1
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001536 let g:options = [['undolevels', '1', '1', '8', '2', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001537 set undolevels=2
1538 call assert_equal([], g:options)
1539 call assert_equal(g:opt[0], g:opt[1])
1540
1541 " 24d: Setting again global number global-local (to buffer) option"
1542 noa set undolevels=8 " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001543 let g:options = [['undolevels', '8', '8', '8', '2', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001544 set undolevels=2
1545 call assert_equal([], g:options)
1546 call assert_equal(g:opt[0], g:opt[1])
1547
1548
1549 " 25a: Setting global number local (to buffer) option"
1550 noa setglobal wrapmargin=8 " Reset global and local value (without triggering autocmd)
1551 noa setlocal wrapmargin=1
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001552 let g:options = [['wrapmargin', '8', '', '8', '2', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001553 setglobal wrapmargin=2
1554 call assert_equal([], g:options)
1555 call assert_equal(g:opt[0], g:opt[1])
1556
1557 " 25b: Setting local number local (to buffer) option"
1558 noa setglobal wrapmargin=8 " Reset global and local value (without triggering autocmd)
1559 noa setlocal wrapmargin=1
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001560 let g:options = [['wrapmargin', '1', '1', '', '2', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001561 setlocal wrapmargin=2
1562 call assert_equal([], g:options)
1563 call assert_equal(g:opt[0], g:opt[1])
1564
1565 " 25c: Setting again number local (to buffer) option"
1566 noa setglobal wrapmargin=8 " Reset global and local value (without triggering autocmd)
1567 noa setlocal wrapmargin=1
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001568 let g:options = [['wrapmargin', '1', '1', '8', '2', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001569 set wrapmargin=2
1570 call assert_equal([], g:options)
1571 call assert_equal(g:opt[0], g:opt[1])
1572
1573 " 25d: Setting again global number local (to buffer) option"
1574 noa set wrapmargin=8 " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001575 let g:options = [['wrapmargin', '8', '8', '8', '2', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001576 set wrapmargin=2
1577 call assert_equal([], g:options)
1578 call assert_equal(g:opt[0], g:opt[1])
1579
1580
1581 " 26: Setting number global-local (to window) option.
1582 " Such option does currently not exist.
1583
1584
1585 " 27a: Setting global number local (to window) option"
1586 noa setglobal foldcolumn=8 " Reset global and local value (without triggering autocmd)
1587 noa setlocal foldcolumn=1
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001588 let g:options = [['foldcolumn', '8', '', '8', '2', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001589 setglobal foldcolumn=2
1590 call assert_equal([], g:options)
1591 call assert_equal(g:opt[0], g:opt[1])
1592
1593 " 27b: Setting local number local (to window) option"
1594 noa setglobal foldcolumn=8 " Reset global and local value (without triggering autocmd)
1595 noa setlocal foldcolumn=1
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001596 let g:options = [['foldcolumn', '1', '1', '', '2', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001597 setlocal foldcolumn=2
1598 call assert_equal([], g:options)
1599 call assert_equal(g:opt[0], g:opt[1])
1600
1601 " 27c: Setting again number local (to window) option"
1602 noa setglobal foldcolumn=8 " Reset global and local value (without triggering autocmd)
1603 noa setlocal foldcolumn=1
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001604 let g:options = [['foldcolumn', '1', '1', '8', '2', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001605 set foldcolumn=2
1606 call assert_equal([], g:options)
1607 call assert_equal(g:opt[0], g:opt[1])
1608
zeertzjqb811de52021-10-21 10:50:44 +01001609 " 27d: Setting again global number local (to window) option"
Bram Moolenaard7c96872019-06-15 17:12:48 +02001610 noa set foldcolumn=8 " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001611 let g:options = [['foldcolumn', '8', '8', '8', '2', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001612 set foldcolumn=2
1613 call assert_equal([], g:options)
1614 call assert_equal(g:opt[0], g:opt[1])
1615
1616
1617 " 28a: Setting global boolean global option"
1618 noa setglobal nowrapscan " Reset global and local value (without triggering autocmd)
1619 noa setlocal wrapscan " Sets the global(!) value!
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001620 let g:options = [['wrapscan', '1', '', '1', '0', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001621 setglobal nowrapscan
1622 call assert_equal([], g:options)
1623 call assert_equal(g:opt[0], g:opt[1])
1624
1625 " 28b: Setting local boolean global option"
1626 noa setglobal nowrapscan " Reset global and local value (without triggering autocmd)
1627 noa setlocal wrapscan " Sets the global(!) value!
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001628 let g:options = [['wrapscan', '1', '1', '', '0', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001629 setlocal nowrapscan
1630 call assert_equal([], g:options)
1631 call assert_equal(g:opt[0], g:opt[1])
1632
1633 " 28c: Setting again boolean global option"
1634 noa setglobal nowrapscan " Reset global and local value (without triggering autocmd)
1635 noa setlocal wrapscan " Sets the global(!) value!
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001636 let g:options = [['wrapscan', '1', '1', '1', '0', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001637 set nowrapscan
1638 call assert_equal([], g:options)
1639 call assert_equal(g:opt[0], g:opt[1])
1640
1641 " 28d: Setting again global boolean global option"
1642 noa set nowrapscan " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001643 let g:options = [['wrapscan', '0', '0', '0', '1', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001644 set wrapscan
1645 call assert_equal([], g:options)
1646 call assert_equal(g:opt[0], g:opt[1])
1647
1648
1649 " 29a: Setting global boolean global-local (to buffer) option"
1650 noa setglobal noautoread " Reset global and local value (without triggering autocmd)
1651 noa setlocal autoread
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001652 let g:options = [['autoread', '0', '', '0', '1', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001653 setglobal autoread
1654 call assert_equal([], g:options)
1655 call assert_equal(g:opt[0], g:opt[1])
1656
1657 " 29b: Setting local boolean global-local (to buffer) option"
1658 noa setglobal noautoread " Reset global and local value (without triggering autocmd)
1659 noa setlocal autoread
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001660 let g:options = [['autoread', '1', '1', '', '0', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001661 setlocal noautoread
1662 call assert_equal([], g:options)
1663 call assert_equal(g:opt[0], g:opt[1])
1664
1665 " 29c: Setting again boolean global-local (to buffer) option"
1666 noa setglobal noautoread " Reset global and local value (without triggering autocmd)
1667 noa setlocal autoread
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001668 let g:options = [['autoread', '1', '1', '0', '1', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001669 set autoread
1670 call assert_equal([], g:options)
1671 call assert_equal(g:opt[0], g:opt[1])
1672
1673 " 29d: Setting again global boolean global-local (to buffer) option"
1674 noa set noautoread " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001675 let g:options = [['autoread', '0', '0', '0', '1', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001676 set autoread
1677 call assert_equal([], g:options)
1678 call assert_equal(g:opt[0], g:opt[1])
1679
1680
1681 " 30a: Setting global boolean local (to buffer) option"
1682 noa setglobal nocindent " Reset global and local value (without triggering autocmd)
1683 noa setlocal cindent
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001684 let g:options = [['cindent', '0', '', '0', '1', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001685 setglobal cindent
1686 call assert_equal([], g:options)
1687 call assert_equal(g:opt[0], g:opt[1])
1688
1689 " 30b: Setting local boolean local (to buffer) option"
1690 noa setglobal nocindent " Reset global and local value (without triggering autocmd)
1691 noa setlocal cindent
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001692 let g:options = [['cindent', '1', '1', '', '0', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001693 setlocal nocindent
1694 call assert_equal([], g:options)
1695 call assert_equal(g:opt[0], g:opt[1])
1696
1697 " 30c: Setting again boolean local (to buffer) option"
1698 noa setglobal nocindent " Reset global and local value (without triggering autocmd)
1699 noa setlocal cindent
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001700 let g:options = [['cindent', '1', '1', '0', '1', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001701 set cindent
1702 call assert_equal([], g:options)
1703 call assert_equal(g:opt[0], g:opt[1])
1704
1705 " 30d: Setting again global boolean local (to buffer) option"
1706 noa set nocindent " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001707 let g:options = [['cindent', '0', '0', '0', '1', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001708 set cindent
1709 call assert_equal([], g:options)
1710 call assert_equal(g:opt[0], g:opt[1])
1711
1712
1713 " 31: Setting boolean global-local (to window) option
1714 " Currently no such option exists.
1715
1716
1717 " 32a: Setting global boolean local (to window) option"
1718 noa setglobal nocursorcolumn " Reset global and local value (without triggering autocmd)
1719 noa setlocal cursorcolumn
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001720 let g:options = [['cursorcolumn', '0', '', '0', '1', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001721 setglobal cursorcolumn
1722 call assert_equal([], g:options)
1723 call assert_equal(g:opt[0], g:opt[1])
1724
1725 " 32b: Setting local boolean local (to window) option"
1726 noa setglobal nocursorcolumn " Reset global and local value (without triggering autocmd)
1727 noa setlocal cursorcolumn
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001728 let g:options = [['cursorcolumn', '1', '1', '', '0', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001729 setlocal nocursorcolumn
1730 call assert_equal([], g:options)
1731 call assert_equal(g:opt[0], g:opt[1])
1732
1733 " 32c: Setting again boolean local (to window) option"
1734 noa setglobal nocursorcolumn " Reset global and local value (without triggering autocmd)
1735 noa setlocal cursorcolumn
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001736 let g:options = [['cursorcolumn', '1', '1', '0', '1', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001737 set cursorcolumn
1738 call assert_equal([], g:options)
1739 call assert_equal(g:opt[0], g:opt[1])
1740
1741 " 32d: Setting again global boolean local (to window) option"
1742 noa set nocursorcolumn " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001743 let g:options = [['cursorcolumn', '0', '0', '0', '1', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001744 set cursorcolumn
1745 call assert_equal([], g:options)
1746 call assert_equal(g:opt[0], g:opt[1])
1747
1748
Bram Moolenaar1bc353b2019-09-01 14:45:28 +02001749 " 33: Test autocommands when an option value is converted internally.
Bram Moolenaard7c96872019-06-15 17:12:48 +02001750 noa set backspace=1 " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001751 let g:options = [['backspace', 'indent,eol', 'indent,eol', 'indent,eol', '2', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001752 set backspace=2
1753 call assert_equal([], g:options)
1754 call assert_equal(g:opt[0], g:opt[1])
1755
1756
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001757 " Cleanup
1758 au! OptionSet
Bram Moolenaar0331faf2019-06-15 18:40:37 +02001759 " set tags&
Bram Moolenaard7c96872019-06-15 17:12:48 +02001760 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 +02001761 exe printf(":set %s&vim", opt)
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001762 endfor
1763 call test_override('starting', 0)
1764 delfunc! AutoCommandOptionSet
1765endfunc
1766
1767func Test_OptionSet_diffmode()
1768 call test_override('starting', 1)
Bram Moolenaar26d98212019-01-27 22:32:55 +01001769 " 18: Changing an option when entering diff mode
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001770 new
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001771 au OptionSet diff :let &l:cul = v:option_new
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001772
1773 call setline(1, ['buffer 1', 'line2', 'line3', 'line4'])
1774 call assert_equal(0, &l:cul)
1775 diffthis
1776 call assert_equal(1, &l:cul)
1777
1778 vnew
1779 call setline(1, ['buffer 2', 'line 2', 'line 3', 'line4'])
1780 call assert_equal(0, &l:cul)
1781 diffthis
1782 call assert_equal(1, &l:cul)
1783
1784 diffoff
1785 call assert_equal(0, &l:cul)
1786 call assert_equal(1, getwinvar(2, '&l:cul'))
1787 bw!
1788
1789 call assert_equal(1, &l:cul)
1790 diffoff!
1791 call assert_equal(0, &l:cul)
1792 call assert_equal(0, getwinvar(1, '&l:cul'))
1793 bw!
1794
1795 " Cleanup
1796 au! OptionSet
1797 call test_override('starting', 0)
1798endfunc
1799
1800func Test_OptionSet_diffmode_close()
1801 call test_override('starting', 1)
1802 " 19: Try to close the current window when entering diff mode
1803 " should not segfault
1804 new
1805 au OptionSet diff close
1806
1807 call setline(1, ['buffer 1', 'line2', 'line3', 'line4'])
Bram Moolenaare2e40752020-09-04 21:18:46 +02001808 call assert_fails(':diffthis', 'E788:')
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001809 call assert_equal(1, &diff)
1810 vnew
1811 call setline(1, ['buffer 2', 'line 2', 'line 3', 'line4'])
Bram Moolenaare2e40752020-09-04 21:18:46 +02001812 call assert_fails(':diffthis', 'E788:')
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001813 call assert_equal(1, &diff)
Bram Moolenaara9aa86f2019-11-10 21:25:45 +01001814 set diffopt-=closeoff
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001815 bw!
Bram Moolenaare2e40752020-09-04 21:18:46 +02001816 call assert_fails(':diffoff!', 'E788:')
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001817 bw!
1818
1819 " Cleanup
1820 au! OptionSet
1821 call test_override('starting', 0)
1822 "delfunc! AutoCommandOptionSet
1823endfunc
Bram Moolenaar4a137b42017-08-04 22:37:11 +02001824
1825" Test for Bufleave autocommand that deletes the buffer we are about to edit.
1826func Test_BufleaveWithDelete()
Bram Moolenaare7cda972022-08-29 11:02:59 +01001827 new | edit XbufLeave1
Bram Moolenaar4a137b42017-08-04 22:37:11 +02001828
1829 augroup test_bufleavewithdelete
1830 autocmd!
Bram Moolenaare7cda972022-08-29 11:02:59 +01001831 autocmd BufLeave XbufLeave1 bwipe XbufLeave2
Bram Moolenaar4a137b42017-08-04 22:37:11 +02001832 augroup END
1833
Bram Moolenaare7cda972022-08-29 11:02:59 +01001834 call assert_fails('edit XbufLeave2', 'E143:')
1835 call assert_equal('XbufLeave1', bufname('%'))
Bram Moolenaar4a137b42017-08-04 22:37:11 +02001836
Bram Moolenaare7cda972022-08-29 11:02:59 +01001837 autocmd! test_bufleavewithdelete BufLeave XbufLeave1
Bram Moolenaar4a137b42017-08-04 22:37:11 +02001838 augroup! test_bufleavewithdelete
1839
1840 new
Bram Moolenaare7cda972022-08-29 11:02:59 +01001841 bwipe! XbufLeave1
Bram Moolenaar4a137b42017-08-04 22:37:11 +02001842endfunc
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +02001843
1844" Test for autocommand that changes the buffer list, when doing ":ball".
1845func Test_Acmd_BufAll()
1846 enew!
1847 %bwipe!
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01001848 call writefile(['Test file Xxx1'], 'Xxx1', 'D')
1849 call writefile(['Test file Xxx2'], 'Xxx2', 'D')
1850 call writefile(['Test file Xxx3'], 'Xxx3', 'D')
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +02001851
1852 " Add three files to the buffer list
1853 split Xxx1
1854 close
1855 split Xxx2
1856 close
1857 split Xxx3
1858 close
1859
1860 " Wipe the buffer when the buffer is opened
1861 au BufReadPost Xxx2 bwipe
1862
1863 call append(0, 'Test file Xxx4')
1864 ball
1865
1866 call assert_equal(2, winnr('$'))
1867 call assert_equal('Xxx1', bufname(winbufnr(winnr('$'))))
1868 wincmd t
1869
1870 au! BufReadPost
1871 %bwipe!
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +02001872 enew! | only
1873endfunc
1874
1875" Test for autocommand that changes current buffer on BufEnter event.
1876" Check if modelines are interpreted for the correct buffer.
1877func Test_Acmd_BufEnter()
1878 %bwipe!
1879 call writefile(['start of test file Xxx1',
1880 \ "\<Tab>this is a test",
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01001881 \ 'end of test file Xxx1'], 'Xxx1', 'D')
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +02001882 call writefile(['start of test file Xxx2',
1883 \ 'vim: set noai :',
1884 \ "\<Tab>this is a test",
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01001885 \ 'end of test file Xxx2'], 'Xxx2', 'D')
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +02001886
1887 au BufEnter Xxx2 brew
1888 set ai modeline modelines=3
1889 edit Xxx1
1890 " edit Xxx2, autocmd will do :brew
1891 edit Xxx2
1892 exe "normal G?this is a\<CR>"
1893 " Append text with autoindent to this file
1894 normal othis should be auto-indented
1895 call assert_equal("\<Tab>this should be auto-indented", getline('.'))
1896 call assert_equal(3, line('.'))
1897 " Remove autocmd and edit Xxx2 again
1898 au! BufEnter Xxx2
1899 buf! Xxx2
1900 exe "normal G?this is a\<CR>"
1901 " append text without autoindent to Xxx
1902 normal othis should be in column 1
1903 call assert_equal("this should be in column 1", getline('.'))
1904 call assert_equal(4, line('.'))
1905
1906 %bwipe!
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +02001907 set ai&vim modeline&vim modelines&vim
1908endfunc
1909
1910" Test for issue #57
1911" do not move cursor on <c-o> when autoindent is set
1912func Test_ai_CTRL_O()
1913 enew!
1914 set ai
1915 let save_fo = &fo
1916 set fo+=r
1917 exe "normal o# abcdef\<Esc>2hi\<CR>\<C-O>d0\<Esc>"
1918 exe "normal o# abcdef\<Esc>2hi\<C-O>d0\<Esc>"
1919 call assert_equal(['# abc', 'def', 'def'], getline(2, 4))
1920
1921 set ai&vim
1922 let &fo = save_fo
1923 enew!
1924endfunc
1925
1926" Test for autocommand that deletes the current buffer on BufLeave event.
1927" Also test deleting the last buffer, should give a new, empty buffer.
1928func Test_BufLeave_Wipe()
1929 %bwipe!
1930 let content = ['start of test file Xxx',
1931 \ 'this is a test',
1932 \ 'end of test file Xxx']
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01001933 call writefile(content, 'Xxx1', 'D')
1934 call writefile(content, 'Xxx2', 'D')
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +02001935
1936 au BufLeave Xxx2 bwipe
1937 edit Xxx1
1938 split Xxx2
1939 " delete buffer Xxx2, we should be back to Xxx1
1940 bwipe
1941 call assert_equal('Xxx1', bufname('%'))
1942 call assert_equal(1, winnr('$'))
1943
1944 " Create an alternate buffer
1945 %write! test.out
1946 call assert_equal('test.out', bufname('#'))
1947 " delete alternate buffer
1948 bwipe test.out
1949 call assert_equal('Xxx1', bufname('%'))
1950 call assert_equal('', bufname('#'))
1951
1952 au BufLeave Xxx1 bwipe
1953 " delete current buffer, get an empty one
1954 bwipe!
1955 call assert_equal(1, line('$'))
1956 call assert_equal('', bufname('%'))
Bram Moolenaarb2c87502017-10-14 21:15:58 +02001957 let g:bufinfo = getbufinfo()
1958 call assert_equal(1, len(g:bufinfo))
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +02001959
Bram Moolenaar53f0c962017-10-22 14:23:59 +02001960 call delete('test.out')
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +02001961 %bwipe
1962 au! BufLeave
Bram Moolenaarb2c87502017-10-14 21:15:58 +02001963
1964 " check that bufinfo doesn't contain a pointer to freed memory
1965 call test_garbagecollect_now()
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +02001966endfunc
Bram Moolenaar87ffb5c2017-10-19 12:37:42 +02001967
1968func Test_QuitPre()
1969 edit Xfoo
1970 let winid = win_getid(winnr())
1971 split Xbar
1972 au! QuitPre * let g:afile = expand('<afile>')
1973 " Close the other window, <afile> should be correct.
1974 exe win_id2win(winid) . 'q'
1975 call assert_equal('Xfoo', g:afile)
LemonBoy66e13ae2022-04-21 22:52:11 +01001976
Bram Moolenaar87ffb5c2017-10-19 12:37:42 +02001977 unlet g:afile
1978 bwipe Xfoo
1979 bwipe Xbar
1980endfunc
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02001981
1982func Test_Cmdline()
Bram Moolenaar153b7042018-01-31 15:48:32 +01001983 au! CmdlineChanged : let g:text = getcmdline()
1984 let g:text = 0
1985 call feedkeys(":echom 'hello'\<CR>", 'xt')
1986 call assert_equal("echom 'hello'", g:text)
1987 au! CmdlineChanged
1988
1989 au! CmdlineChanged : let g:entered = expand('<afile>')
1990 let g:entered = 0
1991 call feedkeys(":echom 'hello'\<CR>", 'xt')
1992 call assert_equal(':', g:entered)
1993 au! CmdlineChanged
1994
zeertzjq412e0e42023-02-11 10:34:07 +00001995 autocmd CmdlineChanged : let g:log += [getcmdline()]
1996
Bram Moolenaarbb393d82022-12-09 12:21:50 +00001997 let g:log = []
1998 cnoremap <F1> <Cmd>call setcmdline('ls')<CR>
Bram Moolenaarbb393d82022-12-09 12:21:50 +00001999 call feedkeys(":\<F1>", 'xt')
2000 call assert_equal(['ls'], g:log)
Bram Moolenaarbb393d82022-12-09 12:21:50 +00002001 cunmap <F1>
2002
zeertzjqaf9e28a2023-02-06 20:58:09 +00002003 let g:log = []
zeertzjqaf9e28a2023-02-06 20:58:09 +00002004 call feedkeys(":sign \<Tab>\<Tab>\<C-N>\<C-P>\<S-Tab>\<S-Tab>\<Esc>", 'xt')
2005 call assert_equal([
2006 \ 's',
2007 \ 'si',
2008 \ 'sig',
2009 \ 'sign',
2010 \ 'sign ',
2011 \ 'sign define',
2012 \ 'sign jump',
2013 \ 'sign list',
2014 \ 'sign jump',
2015 \ 'sign define',
2016 \ 'sign ',
2017 \ ], g:log)
2018 let g:log = []
2019 set wildmenu wildoptions+=pum
2020 call feedkeys(":sign \<S-Tab>\<PageUp>\<kPageUp>\<kPageDown>\<PageDown>\<Esc>", 'xt')
2021 call assert_equal([
2022 \ 's',
2023 \ 'si',
2024 \ 'sig',
2025 \ 'sign',
2026 \ 'sign ',
2027 \ 'sign unplace',
2028 \ 'sign jump',
2029 \ 'sign define',
2030 \ 'sign undefine',
2031 \ 'sign unplace',
2032 \ ], g:log)
2033 set wildmenu& wildoptions&
zeertzjq412e0e42023-02-11 10:34:07 +00002034
2035 let g:log = []
2036 let @r = 'abc'
2037 call feedkeys(":0\<C-R>r1\<C-R>\<C-O>r2\<C-R>\<C-R>r3\<Esc>", 'xt')
2038 call assert_equal([
2039 \ '0',
2040 \ '0a',
2041 \ '0ab',
2042 \ '0abc',
2043 \ '0abc1',
2044 \ '0abc1abc',
2045 \ '0abc1abc2',
2046 \ '0abc1abc2abc',
2047 \ '0abc1abc2abc3',
2048 \ ], g:log)
2049
zeertzjqaf9e28a2023-02-06 20:58:09 +00002050 unlet g:log
2051 au! CmdlineChanged
2052
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02002053 au! CmdlineEnter : let g:entered = expand('<afile>')
2054 au! CmdlineLeave : let g:left = expand('<afile>')
2055 let g:entered = 0
2056 let g:left = 0
2057 call feedkeys(":echo 'hello'\<CR>", 'xt')
2058 call assert_equal(':', g:entered)
2059 call assert_equal(':', g:left)
2060 au! CmdlineEnter
2061 au! CmdlineLeave
2062
Bram Moolenaara4baf5b2018-04-22 13:27:44 +02002063 let save_shellslash = &shellslash
2064 set noshellslash
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02002065 au! CmdlineEnter / let g:entered = expand('<afile>')
2066 au! CmdlineLeave / let g:left = expand('<afile>')
2067 let g:entered = 0
2068 let g:left = 0
Bram Moolenaar53f0c962017-10-22 14:23:59 +02002069 new
2070 call setline(1, 'hello')
2071 call feedkeys("/hello\<CR>", 'xt')
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02002072 call assert_equal('/', g:entered)
2073 call assert_equal('/', g:left)
Bram Moolenaar53f0c962017-10-22 14:23:59 +02002074 bwipe!
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02002075 au! CmdlineEnter
2076 au! CmdlineLeave
Bram Moolenaara4baf5b2018-04-22 13:27:44 +02002077 let &shellslash = save_shellslash
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02002078endfunc
Bram Moolenaar53f0c962017-10-22 14:23:59 +02002079
2080" Test for BufWritePre autocommand that deletes or unloads the buffer.
2081func Test_BufWritePre()
2082 %bwipe
2083 au BufWritePre Xxx1 bunload
2084 au BufWritePre Xxx2 bwipe
2085
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01002086 call writefile(['start of Xxx1', 'test', 'end of Xxx1'], 'Xxx1', 'D')
2087 call writefile(['start of Xxx2', 'test', 'end of Xxx2'], 'Xxx2', 'D')
Bram Moolenaar53f0c962017-10-22 14:23:59 +02002088
2089 edit Xtest
2090 e! Xxx2
2091 bdel Xtest
2092 e Xxx1
2093 " write it, will unload it and give an error msg
Bram Moolenaare2e40752020-09-04 21:18:46 +02002094 call assert_fails('w', 'E203:')
Bram Moolenaar53f0c962017-10-22 14:23:59 +02002095 call assert_equal('Xxx2', bufname('%'))
2096 edit Xtest
2097 e! Xxx2
2098 bwipe Xtest
2099 " write it, will delete the buffer and give an error msg
Bram Moolenaare2e40752020-09-04 21:18:46 +02002100 call assert_fails('w', 'E203:')
Bram Moolenaar53f0c962017-10-22 14:23:59 +02002101 call assert_equal('Xxx1', bufname('%'))
2102 au! BufWritePre
Bram Moolenaar53f0c962017-10-22 14:23:59 +02002103endfunc
2104
2105" Test for BufUnload autocommand that unloads all the other buffers
2106func Test_bufunload_all()
Bram Moolenaarf08b0eb2021-10-16 13:00:14 +01002107 let g:test_is_flaky = 1
Christian Brabandtee17b6f2023-09-09 11:23:50 +02002108 call writefile(['Test file Xxx1'], 'Xxx1', 'D')
2109 call writefile(['Test file Xxx2'], 'Xxx2', 'D')
Bram Moolenaar53f0c962017-10-22 14:23:59 +02002110
Bram Moolenaarc79745a2019-05-20 22:12:34 +02002111 let content =<< trim [CODE]
2112 func UnloadAllBufs()
2113 let i = 1
2114 while i <= bufnr('$')
2115 if i != bufnr('%') && bufloaded(i)
2116 exe i . 'bunload'
2117 endif
2118 let i += 1
2119 endwhile
2120 endfunc
2121 au BufUnload * call UnloadAllBufs()
2122 au VimLeave * call writefile(['Test Finished'], 'Xout')
2123 edit Xxx1
2124 split Xxx2
2125 q
2126 [CODE]
2127
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01002128 call writefile(content, 'Xbunloadtest', 'D')
Bram Moolenaar53f0c962017-10-22 14:23:59 +02002129
2130 call delete('Xout')
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01002131 call system(GetVimCommandClean() .. ' -N --not-a-term -S Xbunloadtest')
Bram Moolenaar53f0c962017-10-22 14:23:59 +02002132 call assert_true(filereadable('Xout'))
2133
Bram Moolenaar53f0c962017-10-22 14:23:59 +02002134 call delete('Xout')
2135endfunc
2136
2137" Some tests for buffer-local autocommands
2138func Test_buflocal_autocmd()
2139 let g:bname = ''
2140 edit xx
2141 au BufLeave <buffer> let g:bname = expand("%")
2142 " here, autocommand for xx should trigger.
2143 " but autocommand shall not apply to buffer named <buffer>.
2144 edit somefile
2145 call assert_equal('xx', g:bname)
2146 let g:bname = ''
2147 " here, autocommand shall be auto-deleted
2148 bwipe xx
2149 " autocmd should not trigger
2150 edit xx
2151 call assert_equal('', g:bname)
2152 " autocmd should not trigger
2153 edit somefile
2154 call assert_equal('', g:bname)
2155 enew
2156 unlet g:bname
2157endfunc
Bram Moolenaar430dc5d2017-11-02 21:04:47 +01002158
2159" Test for "*Cmd" autocommands
2160func Test_Cmd_Autocmds()
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01002161 call writefile(['start of Xxx', "\tabc2", 'end of Xxx'], 'Xxx', 'D')
Bram Moolenaar430dc5d2017-11-02 21:04:47 +01002162
2163 enew!
2164 au BufReadCmd XtestA 0r Xxx|$del
2165 edit XtestA " will read text of Xxd instead
2166 call assert_equal('start of Xxx', getline(1))
2167
2168 au BufWriteCmd XtestA call append(line("$"), "write")
2169 write " will append a line to the file
2170 call assert_equal('write', getline('$'))
Bram Moolenaare2e40752020-09-04 21:18:46 +02002171 call assert_fails('read XtestA', 'E484:') " should not read anything
Bram Moolenaar430dc5d2017-11-02 21:04:47 +01002172 call assert_equal('write', getline(4))
2173
2174 " now we have:
2175 " 1 start of Xxx
2176 " 2 abc2
2177 " 3 end of Xxx
2178 " 4 write
2179
2180 au FileReadCmd XtestB '[r Xxx
2181 2r XtestB " will read Xxx below line 2 instead
2182 call assert_equal('start of Xxx', getline(3))
2183
2184 " now we have:
2185 " 1 start of Xxx
2186 " 2 abc2
2187 " 3 start of Xxx
2188 " 4 abc2
2189 " 5 end of Xxx
2190 " 6 end of Xxx
2191 " 7 write
2192
2193 au FileWriteCmd XtestC '[,']copy $
2194 normal 4GA1
2195 4,5w XtestC " will copy lines 4 and 5 to the end
2196 call assert_equal("\tabc21", getline(8))
Bram Moolenaare2e40752020-09-04 21:18:46 +02002197 call assert_fails('r XtestC', 'E484:') " should not read anything
Bram Moolenaar430dc5d2017-11-02 21:04:47 +01002198 call assert_equal("end of Xxx", getline(9))
2199
2200 " now we have:
2201 " 1 start of Xxx
2202 " 2 abc2
2203 " 3 start of Xxx
2204 " 4 abc21
2205 " 5 end of Xxx
2206 " 6 end of Xxx
2207 " 7 write
2208 " 8 abc21
2209 " 9 end of Xxx
2210
2211 let g:lines = []
2212 au FileAppendCmd XtestD call extend(g:lines, getline(line("'["), line("']")))
2213 w >>XtestD " will add lines to 'lines'
2214 call assert_equal(9, len(g:lines))
Bram Moolenaare2e40752020-09-04 21:18:46 +02002215 call assert_fails('$r XtestD', 'E484:') " should not read anything
Bram Moolenaar430dc5d2017-11-02 21:04:47 +01002216 call assert_equal(9, line('$'))
2217 call assert_equal('end of Xxx', getline('$'))
2218
2219 au BufReadCmd XtestE 0r Xxx|$del
2220 sp XtestE " split window with test.out
2221 call assert_equal('end of Xxx', getline(3))
2222
2223 let g:lines = []
2224 exe "normal 2Goasdf\<Esc>\<C-W>\<C-W>"
2225 au BufWriteCmd XtestE call extend(g:lines, getline(0, '$'))
2226 wall " will write other window to 'lines'
2227 call assert_equal(4, len(g:lines), g:lines)
2228 call assert_equal('asdf', g:lines[2])
2229
2230 au! BufReadCmd
2231 au! BufWriteCmd
2232 au! FileReadCmd
2233 au! FileWriteCmd
2234 au! FileAppendCmd
2235 %bwipe!
Bram Moolenaar430dc5d2017-11-02 21:04:47 +01002236 enew!
2237endfunc
Bram Moolenaaraace2152017-11-05 16:23:10 +01002238
Bram Moolenaar0fff4412020-03-29 16:06:29 +02002239func s:ReadFile()
2240 setl noswapfile nomodified
2241 let filename = resolve(expand("<afile>:p"))
2242 execute 'read' fnameescape(filename)
2243 1d_
2244 exe 'file' fnameescape(filename)
2245 setl buftype=acwrite
2246endfunc
2247
2248func s:WriteFile()
2249 let filename = resolve(expand("<afile>:p"))
2250 setl buftype=
2251 noautocmd execute 'write' fnameescape(filename)
2252 setl buftype=acwrite
2253 setl nomodified
2254endfunc
2255
2256func Test_BufReadCmd()
2257 autocmd BufReadCmd *.test call s:ReadFile()
2258 autocmd BufWriteCmd *.test call s:WriteFile()
2259
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01002260 call writefile(['one', 'two', 'three'], 'Xcmd.test', 'D')
Bram Moolenaar0fff4412020-03-29 16:06:29 +02002261 edit Xcmd.test
2262 call assert_match('Xcmd.test" line 1 of 3', execute('file'))
2263 normal! Gofour
2264 write
2265 call assert_equal(['one', 'two', 'three', 'four'], readfile('Xcmd.test'))
2266
2267 bwipe!
Bram Moolenaar0fff4412020-03-29 16:06:29 +02002268 au! BufReadCmd
2269 au! BufWriteCmd
2270endfunc
2271
zeertzjq9c8f9462022-08-30 18:17:15 +01002272func Test_BufWriteCmd()
2273 autocmd BufWriteCmd Xbufwritecmd let g:written = 1
2274 new
2275 file Xbufwritecmd
2276 set buftype=acwrite
Bram Moolenaar6f14da12022-09-07 21:30:44 +01002277 call mkdir('Xbufwritecmd', 'D')
zeertzjq9c8f9462022-08-30 18:17:15 +01002278 write
2279 " BufWriteCmd should be triggered even if a directory has the same name
2280 call assert_equal(1, g:written)
zeertzjq9c8f9462022-08-30 18:17:15 +01002281 unlet g:written
2282 au! BufWriteCmd
2283 bwipe!
2284endfunc
2285
Bram Moolenaaraace2152017-11-05 16:23:10 +01002286func SetChangeMarks(start, end)
Bram Moolenaar97c69432021-01-15 16:45:21 +01002287 exe a:start .. 'mark ['
2288 exe a:end .. 'mark ]'
Bram Moolenaaraace2152017-11-05 16:23:10 +01002289endfunc
2290
2291" Verify the effects of autocmds on '[ and ']
2292func Test_change_mark_in_autocmds()
2293 edit! Xtest
Bram Moolenaar97c69432021-01-15 16:45:21 +01002294 call feedkeys("ia\<CR>b\<CR>c\<CR>d\<C-g>u\<Esc>", 'xtn')
Bram Moolenaaraace2152017-11-05 16:23:10 +01002295
2296 call SetChangeMarks(2, 3)
2297 write
2298 call assert_equal([1, 4], [line("'["), line("']")])
2299
2300 call SetChangeMarks(2, 3)
2301 au BufWritePre * call assert_equal([1, 4], [line("'["), line("']")])
2302 write
2303 au! BufWritePre
2304
Bram Moolenaar14ddd222020-08-05 12:02:40 +02002305 if has('unix')
Bram Moolenaaraace2152017-11-05 16:23:10 +01002306 write XtestFilter
2307 write >> XtestFilter
2308
2309 call SetChangeMarks(2, 3)
2310 " Marks are set to the entire range of the write
2311 au FilterWritePre * call assert_equal([1, 4], [line("'["), line("']")])
2312 " '[ is adjusted to just before the line that will receive the filtered
2313 " data
2314 au FilterReadPre * call assert_equal([4, 4], [line("'["), line("']")])
2315 " The filtered data is read into the buffer, and the source lines are
2316 " still present, so the range is after the source lines
2317 au FilterReadPost * call assert_equal([5, 12], [line("'["), line("']")])
2318 %!cat XtestFilter
2319 " After the filtered data is read, the original lines are deleted
2320 call assert_equal([1, 8], [line("'["), line("']")])
2321 au! FilterWritePre,FilterReadPre,FilterReadPost
2322 undo
2323
2324 call SetChangeMarks(1, 4)
2325 au FilterWritePre * call assert_equal([2, 3], [line("'["), line("']")])
2326 au FilterReadPre * call assert_equal([3, 3], [line("'["), line("']")])
2327 au FilterReadPost * call assert_equal([4, 11], [line("'["), line("']")])
2328 2,3!cat XtestFilter
2329 call assert_equal([2, 9], [line("'["), line("']")])
2330 au! FilterWritePre,FilterReadPre,FilterReadPost
2331 undo
2332
2333 call delete('XtestFilter')
2334 endif
2335
2336 call SetChangeMarks(1, 4)
2337 au FileWritePre * call assert_equal([2, 3], [line("'["), line("']")])
2338 2,3write Xtest2
2339 au! FileWritePre
2340
2341 call SetChangeMarks(2, 3)
2342 au FileAppendPre * call assert_equal([1, 4], [line("'["), line("']")])
2343 write >> Xtest2
2344 au! FileAppendPre
2345
2346 call SetChangeMarks(1, 4)
2347 au FileAppendPre * call assert_equal([2, 3], [line("'["), line("']")])
2348 2,3write >> Xtest2
2349 au! FileAppendPre
2350
2351 call SetChangeMarks(1, 1)
2352 au FileReadPre * call assert_equal([3, 1], [line("'["), line("']")])
2353 au FileReadPost * call assert_equal([4, 11], [line("'["), line("']")])
2354 3read Xtest2
2355 au! FileReadPre,FileReadPost
2356 undo
2357
2358 call SetChangeMarks(4, 4)
2359 " When the line is 0, it's adjusted to 1
2360 au FileReadPre * call assert_equal([1, 4], [line("'["), line("']")])
2361 au FileReadPost * call assert_equal([1, 8], [line("'["), line("']")])
2362 0read Xtest2
2363 au! FileReadPre,FileReadPost
2364 undo
2365
2366 call SetChangeMarks(4, 4)
2367 " When the line is 0, it's adjusted to 1
2368 au FileReadPre * call assert_equal([1, 4], [line("'["), line("']")])
2369 au FileReadPost * call assert_equal([2, 9], [line("'["), line("']")])
2370 1read Xtest2
2371 au! FileReadPre,FileReadPost
2372 undo
2373
2374 bwipe!
2375 call delete('Xtest')
2376 call delete('Xtest2')
2377endfunc
2378
2379func Test_Filter_noshelltemp()
Bram Moolenaaraeb313f2020-11-27 19:13:28 +01002380 CheckExecutable cat
Bram Moolenaaraace2152017-11-05 16:23:10 +01002381
2382 enew!
2383 call setline(1, ['a', 'b', 'c', 'd'])
2384
2385 let shelltemp = &shelltemp
2386 set shelltemp
2387
2388 let g:filter_au = 0
2389 au FilterWritePre * let g:filter_au += 1
2390 au FilterReadPre * let g:filter_au += 1
2391 au FilterReadPost * let g:filter_au += 1
2392 %!cat
2393 call assert_equal(3, g:filter_au)
2394
2395 if has('filterpipe')
2396 set noshelltemp
2397
2398 let g:filter_au = 0
2399 au FilterWritePre * let g:filter_au += 1
2400 au FilterReadPre * let g:filter_au += 1
2401 au FilterReadPost * let g:filter_au += 1
2402 %!cat
2403 call assert_equal(0, g:filter_au)
2404 endif
2405
2406 au! FilterWritePre,FilterReadPre,FilterReadPost
2407 let &shelltemp = shelltemp
2408 bwipe!
2409endfunc
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01002410
2411func Test_TextYankPost()
2412 enew!
2413 call setline(1, ['foo'])
2414
2415 let g:event = []
2416 au TextYankPost * let g:event = copy(v:event)
2417
2418 call assert_equal({}, v:event)
2419 call assert_fails('let v:event = {}', 'E46:')
2420 call assert_fails('let v:event.mykey = 0', 'E742:')
2421
2422 norm "ayiw
2423 call assert_equal(
Bram Moolenaara016eeb2022-04-09 11:37:38 +01002424 \ #{regcontents: ['foo'], regname: 'a', operator: 'y',
2425 \ regtype: 'v', visual: v:false, inclusive: v:true},
2426 \ g:event)
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01002427 norm y_
2428 call assert_equal(
Bram Moolenaara016eeb2022-04-09 11:37:38 +01002429 \ #{regcontents: ['foo'], regname: '', operator: 'y', regtype: 'V',
2430 \ visual: v:false, inclusive: v:false},
2431 \ g:event)
Bram Moolenaar37d16732020-06-12 22:09:01 +02002432 norm Vy
2433 call assert_equal(
Bram Moolenaara016eeb2022-04-09 11:37:38 +01002434 \ #{regcontents: ['foo'], regname: '', operator: 'y', regtype: 'V',
2435 \ visual: v:true, inclusive: v:true},
2436 \ g:event)
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01002437 call feedkeys("\<C-V>y", 'x')
2438 call assert_equal(
Bram Moolenaara016eeb2022-04-09 11:37:38 +01002439 \ #{regcontents: ['f'], regname: '', operator: 'y', regtype: "\x161",
2440 \ visual: v:true, inclusive: v:true},
2441 \ g:event)
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01002442 norm "xciwbar
2443 call assert_equal(
Bram Moolenaara016eeb2022-04-09 11:37:38 +01002444 \ #{regcontents: ['foo'], regname: 'x', operator: 'c', regtype: 'v',
2445 \ visual: v:false, inclusive: v:true},
2446 \ g:event)
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01002447 norm "bdiw
2448 call assert_equal(
Bram Moolenaara016eeb2022-04-09 11:37:38 +01002449 \ #{regcontents: ['bar'], regname: 'b', operator: 'd', regtype: 'v',
2450 \ visual: v:false, inclusive: v:true},
2451 \ g:event)
2452
2453 call setline(1, 'foobar')
2454 " exclusive motion
2455 norm $"ay0
2456 call assert_equal(
2457 \ #{regcontents: ['fooba'], regname: 'a', operator: 'y', regtype: 'v',
2458 \ visual: v:false, inclusive: v:false},
2459 \ g:event)
2460 " inclusive motion
2461 norm 0"ay$
2462 call assert_equal(
2463 \ #{regcontents: ['foobar'], regname: 'a', operator: 'y', regtype: 'v',
2464 \ visual: v:false, inclusive: v:true},
2465 \ g:event)
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01002466
2467 call assert_equal({}, v:event)
2468
Bram Moolenaarfccbf062020-11-26 20:34:00 +01002469 if has('clipboard_working') && !has('gui_running')
2470 " Test that when the visual selection is automatically copied to clipboard
2471 " register a TextYankPost is emitted
2472 call setline(1, ['foobar'])
2473
2474 let @* = ''
2475 set clipboard=autoselect
2476 exe "norm! ggviw\<Esc>"
2477 call assert_equal(
Bram Moolenaara016eeb2022-04-09 11:37:38 +01002478 \ #{regcontents: ['foobar'], regname: '*', operator: 'y',
2479 \ regtype: 'v', visual: v:true, inclusive: v:false},
2480 \ g:event)
Bram Moolenaarfccbf062020-11-26 20:34:00 +01002481
2482 let @+ = ''
2483 set clipboard=autoselectplus
2484 exe "norm! ggviw\<Esc>"
2485 call assert_equal(
Bram Moolenaara016eeb2022-04-09 11:37:38 +01002486 \ #{regcontents: ['foobar'], regname: '+', operator: 'y',
2487 \ regtype: 'v', visual: v:true, inclusive: v:false},
2488 \ g:event)
Bram Moolenaarfccbf062020-11-26 20:34:00 +01002489
2490 set clipboard&vim
2491 endif
2492
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01002493 au! TextYankPost
2494 unlet g:event
2495 bwipe!
2496endfunc
Bram Moolenaar9bca8052017-12-18 12:37:55 +01002497
Bram Moolenaar9a046fd2021-01-28 13:47:59 +01002498func Test_autocommand_all_events()
2499 call assert_fails('au * * bwipe', 'E1155:')
2500 call assert_fails('au * x bwipe', 'E1155:')
Bram Moolenaarb6db1462021-12-24 19:24:47 +00002501 call assert_fails('au! * x bwipe', 'E1155:')
Bram Moolenaar4fb921e2017-12-18 15:33:00 +01002502endfunc
Bram Moolenaarb7407d32018-02-03 17:36:27 +01002503
Bram Moolenaarf6246f52022-02-11 16:30:12 +00002504func Test_autocmd_user()
2505 au User MyEvent let s:res = [expand("<afile>"), expand("<amatch>")]
2506 doautocmd User MyEvent
2507 call assert_equal(['MyEvent', 'MyEvent'], s:res)
2508 au! User
2509 unlet s:res
2510endfunc
2511
Bram Moolenaar3b014be2022-11-13 17:53:46 +00002512func Test_autocmd_user_clear_group()
2513 CheckRunVimInTerminal
2514
2515 let lines =<< trim END
2516 autocmd! User
2517 for i in range(1, 999)
2518 exe 'autocmd User ' .. 'Foo' .. i .. ' bar'
2519 endfor
2520 au CmdlineLeave : call timer_start(0, {-> execute('autocmd! User')})
2521 END
2522 call writefile(lines, 'XautoUser', 'D')
2523 let buf = RunVimInTerminal('-S XautoUser', {'rows': 10})
2524
2525 " this was using freed memory
2526 call term_sendkeys(buf, ":autocmd User\<CR>")
2527 call TermWait(buf, 50)
2528 call term_sendkeys(buf, "G")
2529
2530 call StopVimInTerminal(buf)
2531endfunc
2532
Bram Moolenaaref2c3252022-11-25 16:31:51 +00002533func Test_autocmd_CmdlineLeave_unlet()
2534 CheckRunVimInTerminal
2535
2536 let lines =<< trim END
2537 for i in range(1, 999)
2538 exe 'let g:var' .. i '=' i
2539 endfor
2540 au CmdlineLeave : call timer_start(0, {-> execute('unlet g:var990')})
2541 END
2542 call writefile(lines, 'XleaveUnlet', 'D')
2543 let buf = RunVimInTerminal('-S XleaveUnlet', {'rows': 10})
2544
2545 " this was using freed memory
2546 call term_sendkeys(buf, ":let g:\<CR>")
2547 call TermWait(buf, 50)
2548 call term_sendkeys(buf, "G")
2549 call TermWait(buf, 50)
2550 call term_sendkeys(buf, "\<CR>") " for the hit-enter prompt
2551
2552 call StopVimInTerminal(buf)
2553endfunc
2554
Bram Moolenaarb7407d32018-02-03 17:36:27 +01002555function s:Before_test_dirchanged()
2556 augroup test_dirchanged
2557 autocmd!
2558 augroup END
2559 let s:li = []
2560 let s:dir_this = getcwd()
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02002561 let s:dir_foo = s:dir_this . '/Xfoo'
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01002562 call mkdir(s:dir_foo)
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02002563 let s:dir_bar = s:dir_this . '/Xbar'
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01002564 call mkdir(s:dir_bar)
Bram Moolenaarb7407d32018-02-03 17:36:27 +01002565endfunc
2566
2567function s:After_test_dirchanged()
Bram Moolenaar3503d7c2019-11-09 20:10:17 +01002568 call chdir(s:dir_this)
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01002569 call delete(s:dir_foo, 'd')
2570 call delete(s:dir_bar, 'd')
Bram Moolenaarb7407d32018-02-03 17:36:27 +01002571 augroup test_dirchanged
2572 autocmd!
2573 augroup END
2574endfunc
2575
2576function Test_dirchanged_global()
2577 call s:Before_test_dirchanged()
Bram Moolenaarf6246f52022-02-11 16:30:12 +00002578 autocmd test_dirchanged DirChangedPre global call add(s:li, expand("<amatch>") .. " pre cd " .. v:event.directory)
Bram Moolenaarb7407d32018-02-03 17:36:27 +01002579 autocmd test_dirchanged DirChanged global call add(s:li, "cd:")
2580 autocmd test_dirchanged DirChanged global call add(s:li, expand("<afile>"))
Bram Moolenaar3503d7c2019-11-09 20:10:17 +01002581 call chdir(s:dir_foo)
Bram Moolenaarf6246f52022-02-11 16:30:12 +00002582 let expected = ["global pre cd " .. s:dir_foo, "cd:", s:dir_foo]
Bram Moolenaar28e8f732022-02-09 12:58:20 +00002583 call assert_equal(expected, s:li)
Bram Moolenaar3503d7c2019-11-09 20:10:17 +01002584 call chdir(s:dir_foo)
Bram Moolenaar28e8f732022-02-09 12:58:20 +00002585 call assert_equal(expected, s:li)
Bram Moolenaar3503d7c2019-11-09 20:10:17 +01002586 exe 'lcd ' .. fnameescape(s:dir_bar)
Bram Moolenaar28e8f732022-02-09 12:58:20 +00002587 call assert_equal(expected, s:li)
Bram Moolenaard8c9d322022-06-12 11:49:16 +01002588
2589 exe 'cd ' .. s:dir_foo
2590 exe 'cd ' .. s:dir_bar
2591 autocmd! test_dirchanged DirChanged global let g:result = expand("<afile>")
2592 cd -
Bram Moolenaardb77c492022-06-12 23:26:50 +01002593 call assert_equal(s:dir_foo, substitute(g:result, '\\', '/', 'g'))
Bram Moolenaard8c9d322022-06-12 11:49:16 +01002594
Bram Moolenaarb7407d32018-02-03 17:36:27 +01002595 call s:After_test_dirchanged()
2596endfunc
2597
2598function Test_dirchanged_local()
2599 call s:Before_test_dirchanged()
2600 autocmd test_dirchanged DirChanged window call add(s:li, "lcd:")
2601 autocmd test_dirchanged DirChanged window call add(s:li, expand("<afile>"))
Bram Moolenaar3503d7c2019-11-09 20:10:17 +01002602 call chdir(s:dir_foo)
Bram Moolenaarb7407d32018-02-03 17:36:27 +01002603 call assert_equal([], s:li)
Bram Moolenaar3503d7c2019-11-09 20:10:17 +01002604 exe 'lcd ' .. fnameescape(s:dir_bar)
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01002605 call assert_equal(["lcd:", s:dir_bar], s:li)
Bram Moolenaar3503d7c2019-11-09 20:10:17 +01002606 exe 'lcd ' .. fnameescape(s:dir_bar)
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01002607 call assert_equal(["lcd:", s:dir_bar], s:li)
Bram Moolenaarb7407d32018-02-03 17:36:27 +01002608 call s:After_test_dirchanged()
2609endfunc
2610
2611function Test_dirchanged_auto()
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +02002612 CheckOption autochdir
Bram Moolenaarb7407d32018-02-03 17:36:27 +01002613 call s:Before_test_dirchanged()
2614 call test_autochdir()
Bram Moolenaar28e8f732022-02-09 12:58:20 +00002615 autocmd test_dirchanged DirChangedPre auto call add(s:li, "pre cd " .. v:event.directory)
Bram Moolenaarb7407d32018-02-03 17:36:27 +01002616 autocmd test_dirchanged DirChanged auto call add(s:li, "auto:")
2617 autocmd test_dirchanged DirChanged auto call add(s:li, expand("<afile>"))
2618 set acd
Bram Moolenaar3503d7c2019-11-09 20:10:17 +01002619 cd ..
Bram Moolenaarb7407d32018-02-03 17:36:27 +01002620 call assert_equal([], s:li)
Bram Moolenaar61abe7d2022-08-30 21:46:08 +01002621 exe 'edit ' . s:dir_foo . '/Xautofile'
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01002622 call assert_equal(s:dir_foo, getcwd())
Bram Moolenaar28e8f732022-02-09 12:58:20 +00002623 let expected = ["pre cd " .. s:dir_foo, "auto:", s:dir_foo]
2624 call assert_equal(expected, s:li)
Bram Moolenaarb7407d32018-02-03 17:36:27 +01002625 set noacd
2626 bwipe!
2627 call s:After_test_dirchanged()
2628endfunc
Bram Moolenaar5a093432018-02-10 18:15:19 +01002629
2630" Test TextChangedI and TextChangedP
2631func Test_ChangedP()
2632 new
2633 call setline(1, ['foo', 'bar', 'foobar'])
2634 call test_override("char_avail", 1)
2635 set complete=. completeopt=menuone
2636
2637 func! TextChangedAutocmd(char)
2638 let g:autocmd .= a:char
2639 endfunc
2640
Christian Brabandtdb3b4462021-10-16 11:58:55 +01002641 " TextChanged will not be triggered, only check that it isn't.
Bram Moolenaar5a093432018-02-10 18:15:19 +01002642 au! TextChanged <buffer> :call TextChangedAutocmd('N')
2643 au! TextChangedI <buffer> :call TextChangedAutocmd('I')
2644 au! TextChangedP <buffer> :call TextChangedAutocmd('P')
2645
2646 call cursor(3, 1)
2647 let g:autocmd = ''
2648 call feedkeys("o\<esc>", 'tnix')
Evgeni Chasnovskid7ae2632023-10-15 09:59:00 +02002649 call assert_equal('I', g:autocmd)
Bram Moolenaar5a093432018-02-10 18:15:19 +01002650
2651 let g:autocmd = ''
Christian Brabandt4bca4892023-10-27 19:26:49 +02002652 call feedkeys("Sf", 'tnix')
2653 call assert_equal('II', g:autocmd)
2654
2655 let g:autocmd = ''
Bram Moolenaar5a093432018-02-10 18:15:19 +01002656 call feedkeys("Sf\<C-N>", 'tnix')
Christian Brabandt4bca4892023-10-27 19:26:49 +02002657 call assert_equal('IIP', g:autocmd)
Bram Moolenaar5a093432018-02-10 18:15:19 +01002658
2659 let g:autocmd = ''
2660 call feedkeys("Sf\<C-N>\<C-N>", 'tnix')
Christian Brabandt4bca4892023-10-27 19:26:49 +02002661 call assert_equal('IIPP', g:autocmd)
Bram Moolenaar5a093432018-02-10 18:15:19 +01002662
2663 let g:autocmd = ''
2664 call feedkeys("Sf\<C-N>\<C-N>\<C-N>", 'tnix')
Christian Brabandt4bca4892023-10-27 19:26:49 +02002665 call assert_equal('IIPPP', g:autocmd)
Bram Moolenaar5a093432018-02-10 18:15:19 +01002666
2667 let g:autocmd = ''
2668 call feedkeys("Sf\<C-N>\<C-N>\<C-N>\<C-N>", 'tnix')
Christian Brabandt4bca4892023-10-27 19:26:49 +02002669 call assert_equal('IIPPPP', g:autocmd)
Bram Moolenaar5a093432018-02-10 18:15:19 +01002670
2671 call assert_equal(['foo', 'bar', 'foobar', 'foo'], getline(1, '$'))
2672 " TODO: how should it handle completeopt=noinsert,noselect?
2673
2674 " CleanUp
2675 call test_override("char_avail", 0)
2676 au! TextChanged
2677 au! TextChangedI
2678 au! TextChangedP
2679 delfu TextChangedAutocmd
2680 unlet! g:autocmd
2681 set complete&vim completeopt&vim
2682
2683 bw!
2684endfunc
Bram Moolenaar8c64a362018-03-23 22:39:31 +01002685
Bram Moolenaar91d2e782018-08-07 19:05:01 +02002686let g:setline_handled = v:false
Bram Moolenaar1e115362019-01-09 23:01:02 +01002687func SetLineOne()
Bram Moolenaar91d2e782018-08-07 19:05:01 +02002688 if !g:setline_handled
2689 call setline(1, "(x)")
2690 let g:setline_handled = v:true
2691 endif
2692endfunc
2693
2694func Test_TextChangedI_with_setline()
2695 new
2696 call test_override('char_avail', 1)
2697 autocmd TextChangedI <buffer> call SetLineOne()
2698 call feedkeys("i(\<CR>\<Esc>", 'tx')
2699 call assert_equal('(', getline(1))
2700 call assert_equal('x)', getline(2))
2701 undo
Bram Moolenaar91d2e782018-08-07 19:05:01 +02002702 call assert_equal('', getline(1))
Bram Moolenaar9fa95062018-08-08 22:08:32 +02002703 call assert_equal('', getline(2))
Bram Moolenaar91d2e782018-08-07 19:05:01 +02002704
Bram Moolenaarca34db32022-01-20 11:17:18 +00002705 call test_override('char_avail', 0)
Bram Moolenaar91d2e782018-08-07 19:05:01 +02002706 bwipe!
2707endfunc
2708
Bram Moolenaar8c64a362018-03-23 22:39:31 +01002709func Test_Changed_FirstTime()
Bram Moolenaar8c5a2782019-08-07 23:07:07 +02002710 CheckFeature terminal
2711 CheckNotGui
Bram Moolenaar3cdcb092020-03-18 19:18:10 +01002712 " Starting a terminal to run Vim is always considered flaky.
Bram Moolenaar30d53e22020-03-18 21:10:44 +01002713 let g:test_is_flaky = 1
Bram Moolenaar8c5a2782019-08-07 23:07:07 +02002714
Bram Moolenaar8c64a362018-03-23 22:39:31 +01002715 " Prepare file for TextChanged event.
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01002716 call writefile([''], 'Xchanged.txt', 'D')
Bram Moolenaar8c64a362018-03-23 22:39:31 +01002717 let buf = term_start([GetVimProg(), '--clean', '-c', 'set noswapfile'], {'term_rows': 3})
2718 call assert_equal('running', term_getstatus(buf))
Bram Moolenaar1834d372018-03-29 17:40:46 +02002719 " Wait for the ruler (in the status line) to be shown.
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01002720 " In ConPTY, there is additional character which is drawn up to the width of
2721 " the screen.
2722 if has('conpty')
2723 call WaitForAssert({-> assert_match('\<All.*$', term_getline(buf, 3))})
2724 else
2725 call WaitForAssert({-> assert_match('\<All$', term_getline(buf, 3))})
2726 endif
Bram Moolenaar8c64a362018-03-23 22:39:31 +01002727 " It's only adding autocmd, so that no event occurs.
2728 call term_sendkeys(buf, ":au! TextChanged <buffer> call writefile(['No'], 'Xchanged.txt')\<cr>")
2729 call term_sendkeys(buf, "\<C-\\>\<C-N>:qa!\<cr>")
Bram Moolenaar50182fa2018-04-28 21:34:40 +02002730 call WaitForAssert({-> assert_equal('finished', term_getstatus(buf))})
Bram Moolenaar8c64a362018-03-23 22:39:31 +01002731 call assert_equal([''], readfile('Xchanged.txt'))
2732
2733 " clean up
Bram Moolenaar8c64a362018-03-23 22:39:31 +01002734 bwipe!
2735endfunc
Bram Moolenaar0566e892019-01-24 19:37:40 +01002736
Bram Moolenaareb93f3f2019-04-04 15:04:56 +02002737func Test_autocmd_nested()
2738 let g:did_nested = 0
2739 augroup Testing
2740 au WinNew * edit somefile
2741 au BufNew * let g:did_nested = 1
2742 augroup END
2743 split
2744 call assert_equal(0, g:did_nested)
2745 close
2746 bwipe! somefile
2747
2748 " old nested argument still works
2749 augroup Testing
2750 au!
2751 au WinNew * nested edit somefile
2752 au BufNew * let g:did_nested = 1
2753 augroup END
2754 split
2755 call assert_equal(1, g:did_nested)
2756 close
2757 bwipe! somefile
2758
2759 " New ++nested argument works
2760 augroup Testing
2761 au!
2762 au WinNew * ++nested edit somefile
2763 au BufNew * let g:did_nested = 1
2764 augroup END
2765 split
2766 call assert_equal(1, g:did_nested)
2767 close
2768 bwipe! somefile
2769
Bram Moolenaarf0775142022-03-04 20:10:38 +00002770 " nested without ++ does not work in Vim9 script
2771 call assert_fails('vim9cmd au WinNew * nested echo fails', 'E1078:')
2772
Bram Moolenaareb93f3f2019-04-04 15:04:56 +02002773 augroup Testing
2774 au!
2775 augroup END
2776
2777 call assert_fails('au WinNew * ++nested ++nested echo bad', 'E983:')
2778 call assert_fails('au WinNew * nested nested echo bad', 'E983:')
2779endfunc
2780
Bram Moolenaar5fa9f232022-07-23 09:06:48 +01002781func Test_autocmd_nested_cursor_invalid()
2782 set laststatus=0
2783 copen
2784 cclose
2785 call setline(1, ['foo', 'bar', 'baz'])
2786 3
2787 augroup nested_inv
2788 autocmd User foo ++nested copen
2789 autocmd BufAdd * let &laststatus = 2 - &laststatus
2790 augroup END
2791 doautocmd User foo
2792
2793 augroup nested_inv
2794 au!
2795 augroup END
2796 set laststatus&
Bram Moolenaarb03950f2022-07-26 13:47:13 +01002797 cclose
Bram Moolenaar5fa9f232022-07-23 09:06:48 +01002798 bwipe!
2799endfunc
2800
Bram Moolenaar3d6ee8b2022-07-27 15:23:35 +01002801func Test_autocmd_nested_keeps_cursor_pos()
2802 enew
2803 call setline(1, 'foo')
2804 autocmd User foo ++nested normal! $a
2805 autocmd InsertLeave * :
2806 doautocmd User foo
2807 call assert_equal([0, 1, 3, 0], getpos('.'))
2808
2809 bwipe!
2810endfunc
2811
Bram Moolenaarb03950f2022-07-26 13:47:13 +01002812func Test_autocmd_nested_switch_window()
2813 " run this in a separate Vim so that SafeState works
2814 CheckRunVimInTerminal
2815
2816 let lines =<< trim END
2817 vim9script
2818 ['()']->writefile('Xautofile')
2819 autocmd VimEnter * ++nested edit Xautofile | split
2820 autocmd BufReadPost * autocmd SafeState * ++once foldclosed('.')
2821 autocmd WinEnter * matchadd('ErrorMsg', 'pat')
2822 END
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01002823 call writefile(lines, 'Xautoscript', 'D')
Bram Moolenaarb03950f2022-07-26 13:47:13 +01002824 let buf = RunVimInTerminal('-S Xautoscript', {'rows': 10})
2825 call VerifyScreenDump(buf, 'Test_autocmd_nested_switch', {})
2826
2827 call StopVimInTerminal(buf)
2828 call delete('Xautofile')
Bram Moolenaarb03950f2022-07-26 13:47:13 +01002829endfunc
2830
Bram Moolenaareb93f3f2019-04-04 15:04:56 +02002831func Test_autocmd_once()
2832 " Without ++once WinNew triggers twice
2833 let g:did_split = 0
2834 augroup Testing
2835 au WinNew * let g:did_split += 1
2836 augroup END
2837 split
2838 split
2839 call assert_equal(2, g:did_split)
2840 call assert_true(exists('#WinNew'))
2841 close
2842 close
2843
2844 " With ++once WinNew triggers once
2845 let g:did_split = 0
2846 augroup Testing
2847 au!
2848 au WinNew * ++once let g:did_split += 1
2849 augroup END
2850 split
2851 split
2852 call assert_equal(1, g:did_split)
2853 call assert_false(exists('#WinNew'))
2854 close
2855 close
2856
2857 call assert_fails('au WinNew * ++once ++once echo bad', 'E983:')
2858endfunc
2859
Bram Moolenaara68e5952019-04-25 22:22:01 +02002860func Test_autocmd_bufreadpre()
2861 new
2862 let b:bufreadpre = 1
Bram Moolenaarab505b12020-03-23 19:28:44 +01002863 call append(0, range(1000))
Bram Moolenaara68e5952019-04-25 22:22:01 +02002864 w! XAutocmdBufReadPre.txt
2865 autocmd BufReadPre <buffer> :let b:bufreadpre += 1
Bram Moolenaarab505b12020-03-23 19:28:44 +01002866 norm! 500gg
Bram Moolenaara68e5952019-04-25 22:22:01 +02002867 sp
Bram Moolenaarab505b12020-03-23 19:28:44 +01002868 norm! 1000gg
Bram Moolenaara68e5952019-04-25 22:22:01 +02002869 wincmd p
2870 let g:wsv1 = winsaveview()
2871 wincmd p
2872 let g:wsv2 = winsaveview()
2873 " triggers BufReadPre, should not move the cursor in either window
2874 " The topline may change one line in a large window.
2875 edit
2876 call assert_inrange(g:wsv2.topline - 1, g:wsv2.topline + 1, winsaveview().topline)
2877 call assert_equal(g:wsv2.lnum, winsaveview().lnum)
2878 call assert_equal(2, b:bufreadpre)
2879 wincmd p
2880 call assert_equal(g:wsv1.topline, winsaveview().topline)
2881 call assert_equal(g:wsv1.lnum, winsaveview().lnum)
2882 call assert_equal(2, b:bufreadpre)
2883 " Now set the cursor position in an BufReadPre autocommand
2884 " (even though the position will be invalid, this should make Vim reset the
2885 " cursor position in the other window.
2886 wincmd p
2887 set cpo+=g
2888 " won't do anything, but try to set the cursor on an invalid lnum
2889 autocmd BufReadPre <buffer> :norm! 70gg
2890 " triggers BufReadPre, should not move the cursor in either window
2891 e
2892 call assert_equal(1, winsaveview().topline)
2893 call assert_equal(1, winsaveview().lnum)
2894 call assert_equal(3, b:bufreadpre)
2895 wincmd p
2896 call assert_equal(g:wsv1.topline, winsaveview().topline)
2897 call assert_equal(g:wsv1.lnum, winsaveview().lnum)
2898 call assert_equal(3, b:bufreadpre)
2899 close
2900 close
2901 call delete('XAutocmdBufReadPre.txt')
2902 set cpo-=g
2903endfunc
2904
Bram Moolenaar5e66b422019-01-24 21:58:10 +01002905" FileChangedShell tested in test_filechanged.vim
Bram Moolenaar69ea5872019-04-25 20:29:00 +02002906
2907" Tests for the following autocommands:
2908" - FileWritePre writing a compressed file
2909" - FileReadPost reading a compressed file
2910" - BufNewFile reading a file template
2911" - BufReadPre decompressing the file to be read
2912" - FilterReadPre substituting characters in the temp file
2913" - FilterReadPost substituting characters after filtering
2914" - FileReadPre set options for decompression
2915" - FileReadPost decompress the file
2916func Test_ReadWrite_Autocmds()
2917 " Run this test only on Unix-like systems and if gzip is available
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +02002918 CheckUnix
2919 CheckExecutable gzip
Bram Moolenaar69ea5872019-04-25 20:29:00 +02002920
2921 " Make $GZIP empty, "-v" would cause trouble.
2922 let $GZIP = ""
2923
2924 " Use a FileChangedShell autocommand to avoid a prompt for 'Xtestfile.gz'
2925 " being modified outside of Vim (noticed on Solaris).
2926 au FileChangedShell * echo 'caught FileChangedShell'
2927
2928 " Test for the FileReadPost, FileWritePre and FileWritePost autocmds
2929 augroup Test1
2930 au!
2931 au FileWritePre *.gz '[,']!gzip
2932 au FileWritePost *.gz undo
2933 au FileReadPost *.gz '[,']!gzip -d
2934 augroup END
2935
2936 new
2937 set bin
2938 call append(0, [
2939 \ 'line 2 Abcdefghijklmnopqrstuvwxyz',
2940 \ 'line 3 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2941 \ 'line 4 Abcdefghijklmnopqrstuvwxyz',
2942 \ 'line 5 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2943 \ 'line 6 Abcdefghijklmnopqrstuvwxyz',
2944 \ 'line 7 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2945 \ 'line 8 Abcdefghijklmnopqrstuvwxyz',
2946 \ 'line 9 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2947 \ 'line 10 Abcdefghijklmnopqrstuvwxyz'
2948 \ ])
2949 1,9write! Xtestfile.gz
2950 enew! | close
2951
2952 new
2953 " Read and decompress the testfile
2954 0read Xtestfile.gz
2955 call assert_equal([
2956 \ 'line 2 Abcdefghijklmnopqrstuvwxyz',
2957 \ 'line 3 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2958 \ 'line 4 Abcdefghijklmnopqrstuvwxyz',
2959 \ 'line 5 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2960 \ 'line 6 Abcdefghijklmnopqrstuvwxyz',
2961 \ 'line 7 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2962 \ 'line 8 Abcdefghijklmnopqrstuvwxyz',
2963 \ 'line 9 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2964 \ 'line 10 Abcdefghijklmnopqrstuvwxyz'
2965 \ ], getline(1, 9))
2966 enew! | close
2967
2968 augroup Test1
2969 au!
2970 augroup END
2971
2972 " Test for the FileAppendPre and FileAppendPost autocmds
2973 augroup Test2
2974 au!
2975 au BufNewFile *.c read Xtest.c
2976 au FileAppendPre *.out '[,']s/new/NEW/
2977 au FileAppendPost *.out !cat Xtest.c >> test.out
2978 augroup END
2979
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01002980 call writefile(['/*', ' * Here is a new .c file', ' */'], 'Xtest.c', 'D')
Bram Moolenaar69ea5872019-04-25 20:29:00 +02002981 new foo.c " should load Xtest.c
2982 call assert_equal(['/*', ' * Here is a new .c file', ' */'], getline(2, 4))
2983 w! >> test.out " append it to the output file
2984
2985 let contents = readfile('test.out')
2986 call assert_equal(' * Here is a NEW .c file', contents[2])
2987 call assert_equal(' * Here is a new .c file', contents[5])
2988
2989 call delete('test.out')
2990 enew! | close
2991 augroup Test2
2992 au!
2993 augroup END
2994
2995 " Test for the BufReadPre and BufReadPost autocmds
2996 augroup Test3
2997 au!
2998 " setup autocommands to decompress before reading and re-compress
2999 " afterwards
3000 au BufReadPre *.gz exe '!gzip -d ' . shellescape(expand("<afile>"))
3001 au BufReadPre *.gz call rename(expand("<afile>:r"), expand("<afile>"))
3002 au BufReadPost *.gz call rename(expand("<afile>"), expand("<afile>:r"))
3003 au BufReadPost *.gz exe '!gzip ' . shellescape(expand("<afile>:r"))
3004 augroup END
3005
3006 e! Xtestfile.gz " Edit compressed file
3007 call assert_equal([
3008 \ 'line 2 Abcdefghijklmnopqrstuvwxyz',
3009 \ 'line 3 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
3010 \ 'line 4 Abcdefghijklmnopqrstuvwxyz',
3011 \ 'line 5 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
3012 \ 'line 6 Abcdefghijklmnopqrstuvwxyz',
3013 \ 'line 7 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
3014 \ 'line 8 Abcdefghijklmnopqrstuvwxyz',
3015 \ 'line 9 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
3016 \ 'line 10 Abcdefghijklmnopqrstuvwxyz'
3017 \ ], getline(1, 9))
3018
3019 w! >> test.out " Append it to the output file
3020
3021 augroup Test3
3022 au!
3023 augroup END
3024
3025 " Test for the FilterReadPre and FilterReadPost autocmds.
3026 set shelltemp " need temp files here
3027 augroup Test4
3028 au!
3029 au FilterReadPre *.out call rename(expand("<afile>"), expand("<afile>") . ".t")
3030 au FilterReadPre *.out exe 'silent !sed s/e/E/ ' . shellescape(expand("<afile>")) . ".t >" . shellescape(expand("<afile>"))
3031 au FilterReadPre *.out exe 'silent !rm ' . shellescape(expand("<afile>")) . '.t'
3032 au FilterReadPost *.out '[,']s/x/X/g
3033 augroup END
3034
3035 e! test.out " Edit the output file
3036 1,$!cat
3037 call assert_equal([
3038 \ 'linE 2 AbcdefghijklmnopqrstuvwXyz',
3039 \ 'linE 3 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
3040 \ 'linE 4 AbcdefghijklmnopqrstuvwXyz',
3041 \ 'linE 5 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
3042 \ 'linE 6 AbcdefghijklmnopqrstuvwXyz',
3043 \ 'linE 7 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
3044 \ 'linE 8 AbcdefghijklmnopqrstuvwXyz',
3045 \ 'linE 9 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
3046 \ 'linE 10 AbcdefghijklmnopqrstuvwXyz'
3047 \ ], getline(1, 9))
3048 call assert_equal([
3049 \ 'line 2 Abcdefghijklmnopqrstuvwxyz',
3050 \ 'line 3 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
3051 \ 'line 4 Abcdefghijklmnopqrstuvwxyz',
3052 \ 'line 5 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
3053 \ 'line 6 Abcdefghijklmnopqrstuvwxyz',
3054 \ 'line 7 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
3055 \ 'line 8 Abcdefghijklmnopqrstuvwxyz',
3056 \ 'line 9 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
3057 \ 'line 10 Abcdefghijklmnopqrstuvwxyz'
3058 \ ], readfile('test.out'))
3059
3060 augroup Test4
3061 au!
3062 augroup END
3063 set shelltemp&vim
3064
3065 " Test for the FileReadPre and FileReadPost autocmds.
3066 augroup Test5
3067 au!
3068 au FileReadPre *.gz exe 'silent !gzip -d ' . shellescape(expand("<afile>"))
3069 au FileReadPre *.gz call rename(expand("<afile>:r"), expand("<afile>"))
3070 au FileReadPost *.gz '[,']s/l/L/
3071 augroup END
3072
3073 new
3074 0r Xtestfile.gz " Read compressed file
3075 call assert_equal([
3076 \ 'Line 2 Abcdefghijklmnopqrstuvwxyz',
3077 \ 'Line 3 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
3078 \ 'Line 4 Abcdefghijklmnopqrstuvwxyz',
3079 \ 'Line 5 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
3080 \ 'Line 6 Abcdefghijklmnopqrstuvwxyz',
3081 \ 'Line 7 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
3082 \ 'Line 8 Abcdefghijklmnopqrstuvwxyz',
3083 \ 'Line 9 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
3084 \ 'Line 10 Abcdefghijklmnopqrstuvwxyz'
3085 \ ], getline(1, 9))
3086 call assert_equal([
3087 \ 'line 2 Abcdefghijklmnopqrstuvwxyz',
3088 \ 'line 3 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
3089 \ 'line 4 Abcdefghijklmnopqrstuvwxyz',
3090 \ 'line 5 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
3091 \ 'line 6 Abcdefghijklmnopqrstuvwxyz',
3092 \ 'line 7 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
3093 \ 'line 8 Abcdefghijklmnopqrstuvwxyz',
3094 \ 'line 9 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
3095 \ 'line 10 Abcdefghijklmnopqrstuvwxyz'
3096 \ ], readfile('Xtestfile.gz'))
3097
3098 augroup Test5
3099 au!
3100 augroup END
3101
3102 au! FileChangedShell
3103 call delete('Xtestfile.gz')
Bram Moolenaar69ea5872019-04-25 20:29:00 +02003104 call delete('test.out')
3105endfunc
Bram Moolenaar23b51392019-05-09 21:38:43 +02003106
3107func Test_throw_in_BufWritePre()
3108 new
3109 call setline(1, ['one', 'two', 'three'])
3110 call assert_false(filereadable('Xthefile'))
3111 augroup throwing
3112 au BufWritePre X* throw 'do not write'
3113 augroup END
3114 try
3115 w Xthefile
3116 catch
3117 let caught = 1
3118 endtry
3119 call assert_equal(1, caught)
3120 call assert_false(filereadable('Xthefile'))
3121
3122 bwipe!
3123 au! throwing
3124endfunc
Bram Moolenaarcadbe1b2019-09-22 21:50:09 +02003125
Bram Moolenaar40fa12a2021-09-22 14:18:13 +02003126func Test_autocmd_in_try_block()
Bram Moolenaar6f14da12022-09-07 21:30:44 +01003127 call mkdir('Xintrydir', 'R')
Bram Moolenaar40fa12a2021-09-22 14:18:13 +02003128 au BufEnter * let g:fname = expand('%')
3129 try
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +01003130 edit Xintrydir/
Bram Moolenaar40fa12a2021-09-22 14:18:13 +02003131 endtry
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +01003132 call assert_match('Xintrydir', g:fname)
Bram Moolenaar40fa12a2021-09-22 14:18:13 +02003133
3134 unlet g:fname
3135 au! BufEnter
Bram Moolenaar40fa12a2021-09-22 14:18:13 +02003136endfunc
3137
Bram Moolenaarcadbe1b2019-09-22 21:50:09 +02003138func Test_autocmd_SafeState()
3139 CheckRunVimInTerminal
3140
3141 let lines =<< trim END
3142 let g:safe = 0
3143 let g:again = ''
3144 au SafeState * let g:safe += 1
3145 au SafeStateAgain * let g:again ..= 'x'
3146 func CallTimer()
3147 call timer_start(10, {id -> execute('let g:again ..= "t"')})
3148 endfunc
3149 END
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01003150 call writefile(lines, 'XSafeState', 'D')
Bram Moolenaarcadbe1b2019-09-22 21:50:09 +02003151 let buf = RunVimInTerminal('-S XSafeState', #{rows: 6})
3152
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01003153 " Sometimes we loop to handle a K_IGNORE, SafeState may be triggered once or
Bram Moolenaar8fb1b472020-02-23 16:16:26 +01003154 " more often.
Bram Moolenaarcadbe1b2019-09-22 21:50:09 +02003155 call term_sendkeys(buf, ":echo g:safe\<CR>")
Bram Moolenaar8fb1b472020-02-23 16:16:26 +01003156 call WaitForAssert({-> assert_match('^\d ', term_getline(buf, 6))}, 1000)
Bram Moolenaarcadbe1b2019-09-22 21:50:09 +02003157
Bram Moolenaar8fb1b472020-02-23 16:16:26 +01003158 " SafeStateAgain should be invoked at least three times
Bram Moolenaarcadbe1b2019-09-22 21:50:09 +02003159 call term_sendkeys(buf, ":echo g:again\<CR>")
Bram Moolenaar8fb1b472020-02-23 16:16:26 +01003160 call WaitForAssert({-> assert_match('^xxx', term_getline(buf, 6))}, 1000)
Bram Moolenaarcadbe1b2019-09-22 21:50:09 +02003161
3162 call term_sendkeys(buf, ":let g:again = ''\<CR>:call CallTimer()\<CR>")
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02003163 call TermWait(buf, 50)
Bram Moolenaar0f6629a2019-09-22 23:24:13 +02003164 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02003165 call TermWait(buf, 50)
Bram Moolenaarcadbe1b2019-09-22 21:50:09 +02003166 call term_sendkeys(buf, ":echo g:again\<CR>")
3167 call WaitForAssert({-> assert_match('xtx', term_getline(buf, 6))}, 1000)
3168
3169 call StopVimInTerminal(buf)
Bram Moolenaarcadbe1b2019-09-22 21:50:09 +02003170endfunc
Bram Moolenaar23324a02019-10-01 17:39:04 +02003171
3172func Test_autocmd_CmdWinEnter()
3173 CheckRunVimInTerminal
Bram Moolenaar21829c52021-01-26 22:42:21 +01003174
Bram Moolenaar23324a02019-10-01 17:39:04 +02003175 let lines =<< trim END
Egor Zvorykin125ffd22021-11-17 14:01:14 +00003176 augroup vimHints | au! | augroup END
Bram Moolenaar23324a02019-10-01 17:39:04 +02003177 let b:dummy_var = 'This is a dummy'
3178 autocmd CmdWinEnter * quit
3179 let winnr = winnr('$')
3180 END
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01003181 let filename = 'XCmdWinEnter'
Bram Moolenaar23324a02019-10-01 17:39:04 +02003182 call writefile(lines, filename)
3183 let buf = RunVimInTerminal('-S '.filename, #{rows: 6})
3184
3185 call term_sendkeys(buf, "q:")
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02003186 call TermWait(buf)
Bram Moolenaar23324a02019-10-01 17:39:04 +02003187 call term_sendkeys(buf, ":echo b:dummy_var\<cr>")
Bram Moolenaar353c3512020-03-15 14:19:26 +01003188 call WaitForAssert({-> assert_match('^This is a dummy', term_getline(buf, 6))}, 2000)
Bram Moolenaar23324a02019-10-01 17:39:04 +02003189 call term_sendkeys(buf, ":echo &buftype\<cr>")
3190 call WaitForAssert({-> assert_notmatch('^nofile', term_getline(buf, 6))}, 1000)
3191 call term_sendkeys(buf, ":echo winnr\<cr>")
3192 call WaitForAssert({-> assert_match('^1', term_getline(buf, 6))}, 1000)
3193
3194 " clean up
3195 call StopVimInTerminal(buf)
3196 call delete(filename)
3197endfunc
Bram Moolenaarec66c412019-10-11 21:19:13 +02003198
3199func Test_autocmd_was_using_freed_memory()
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01003200 CheckFeature quickfix
3201
Bram Moolenaarec66c412019-10-11 21:19:13 +02003202 pedit xx
3203 n x
Bram Moolenaar92bb83e2021-02-03 23:04:46 +01003204 augroup winenter
3205 au WinEnter * if winnr('$') > 2 | quit | endif
3206 augroup END
Bram Moolenaarec66c412019-10-11 21:19:13 +02003207 split
Bram Moolenaar92bb83e2021-02-03 23:04:46 +01003208
3209 augroup winenter
3210 au! WinEnter
3211 augroup END
3212
3213 bwipe xx
3214 bwipe x
3215 pclose
Bram Moolenaarec66c412019-10-11 21:19:13 +02003216endfunc
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +01003217
3218func Test_BufWrite_lockmarks()
Bram Moolenaarf08b0eb2021-10-16 13:00:14 +01003219 let g:test_is_flaky = 1
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +01003220 edit! Xtest
3221 call setline(1, ['a', 'b', 'c', 'd'])
3222
3223 " :lockmarks preserves the marks
3224 call SetChangeMarks(2, 3)
3225 lockmarks write
3226 call assert_equal([2, 3], [line("'["), line("']")])
3227
3228 " *WritePre autocmds get the correct line range, but lockmarks preserves the
3229 " original values for the user
3230 augroup lockmarks
3231 au!
3232 au BufWritePre,FilterWritePre * call assert_equal([1, 4], [line("'["), line("']")])
3233 au FileWritePre * call assert_equal([3, 4], [line("'["), line("']")])
3234 augroup END
3235
3236 lockmarks write
3237 call assert_equal([2, 3], [line("'["), line("']")])
3238
3239 if executable('cat')
3240 lockmarks %!cat
3241 call assert_equal([2, 3], [line("'["), line("']")])
3242 endif
3243
3244 lockmarks 3,4write Xtest2
3245 call assert_equal([2, 3], [line("'["), line("']")])
3246
3247 au! lockmarks
3248 augroup! lockmarks
3249 call delete('Xtest')
3250 call delete('Xtest2')
3251endfunc
Bram Moolenaarce6db022020-01-07 20:11:42 +01003252
3253func Test_FileType_spell()
3254 if !isdirectory('/tmp')
3255 throw "Skipped: requires /tmp directory"
3256 endif
3257
3258 " this was crashing with an invalid free()
3259 setglobal spellfile=/tmp/en.utf-8.add
3260 augroup crash
3261 autocmd!
3262 autocmd BufNewFile,BufReadPost crashfile setf somefiletype
3263 autocmd BufNewFile,BufReadPost crashfile set ft=anotherfiletype
3264 autocmd FileType anotherfiletype setlocal spell
3265 augroup END
3266 func! NoCrash() abort
3267 edit /tmp/crashfile
3268 endfunc
3269 call NoCrash()
3270
3271 au! crash
3272 setglobal spellfile=
3273endfunc
Bram Moolenaarbc2b71d2020-02-17 21:33:30 +01003274
Bram Moolenaaref976322022-09-28 11:48:30 +01003275" this was wiping out the current buffer and using freed memory
3276func Test_SpellFileMissing_bwipe()
3277 next 0
3278 au SpellFileMissing 0 bwipe
3279 call assert_fails('set spell spelllang=0', 'E937:')
3280
3281 au! SpellFileMissing
Bram Moolenaar0a60f792022-11-19 21:18:11 +00003282 set nospell spelllang=en
Bram Moolenaaref976322022-09-28 11:48:30 +01003283 bwipe
3284endfunc
3285
Bram Moolenaar406cd902020-02-18 21:54:41 +01003286" Test closing a window or editing another buffer from a FileChangedRO handler
3287" in a readonly buffer
3288func Test_FileChangedRO_winclose()
Bram Moolenaar62cd26a2020-10-11 20:08:44 +02003289 call test_override('ui_delay', 10)
3290
Bram Moolenaar406cd902020-02-18 21:54:41 +01003291 augroup FileChangedROTest
3292 au!
3293 autocmd FileChangedRO * quit
3294 augroup END
3295 new
3296 set readonly
3297 call assert_fails('normal i', 'E788:')
3298 close
3299 augroup! FileChangedROTest
3300
3301 augroup FileChangedROTest
3302 au!
Bram Moolenaar61abe7d2022-08-30 21:46:08 +01003303 autocmd FileChangedRO * edit Xrofile
Bram Moolenaar406cd902020-02-18 21:54:41 +01003304 augroup END
3305 new
3306 set readonly
3307 call assert_fails('normal i', 'E788:')
3308 close
3309 augroup! FileChangedROTest
Bram Moolenaar62cd26a2020-10-11 20:08:44 +02003310 call test_override('ALL', 0)
Bram Moolenaar406cd902020-02-18 21:54:41 +01003311endfunc
3312
Bram Moolenaar0c81d1b2020-02-22 22:45:55 +01003313func LogACmd()
3314 call add(g:logged, line('$'))
3315endfunc
3316
3317func Test_TermChanged()
Bram Moolenaard28e0b32020-02-22 23:08:52 +01003318 CheckNotGui
3319
Bram Moolenaar0c81d1b2020-02-22 22:45:55 +01003320 enew!
3321 tabnew
3322 call setline(1, ['a', 'b', 'c', 'd'])
3323 $
3324 au TermChanged * call LogACmd()
3325 let g:logged = []
3326 let term_save = &term
3327 set term=xterm
3328 call assert_equal([1, 4], g:logged)
3329
3330 au! TermChanged
3331 let &term = term_save
3332 bwipe!
3333endfunc
3334
Bram Moolenaare3284872020-03-19 13:55:03 +01003335" Test for FileReadCmd autocmd
3336func Test_autocmd_FileReadCmd()
3337 func ReadFileCmd()
3338 call append(line('$'), "v:cmdarg = " .. v:cmdarg)
3339 endfunc
3340 augroup FileReadCmdTest
3341 au!
3342 au FileReadCmd Xtest call ReadFileCmd()
3343 augroup END
3344
3345 new
3346 read ++bin Xtest
3347 read ++nobin Xtest
3348 read ++edit Xtest
3349 read ++bad=keep Xtest
3350 read ++bad=drop Xtest
3351 read ++bad=- Xtest
3352 read ++ff=unix Xtest
3353 read ++ff=dos Xtest
3354 read ++ff=mac Xtest
3355 read ++enc=utf-8 Xtest
3356
3357 call assert_equal(['',
3358 \ 'v:cmdarg = ++bin',
3359 \ 'v:cmdarg = ++nobin',
3360 \ 'v:cmdarg = ++edit',
3361 \ 'v:cmdarg = ++bad=keep',
3362 \ 'v:cmdarg = ++bad=drop',
3363 \ 'v:cmdarg = ++bad=-',
3364 \ 'v:cmdarg = ++ff=unix',
3365 \ 'v:cmdarg = ++ff=dos',
3366 \ 'v:cmdarg = ++ff=mac',
3367 \ 'v:cmdarg = ++enc=utf-8'], getline(1, '$'))
3368
Bram Moolenaar23526d22022-12-05 15:50:41 +00003369 bwipe!
Bram Moolenaare3284872020-03-19 13:55:03 +01003370 augroup FileReadCmdTest
3371 au!
3372 augroup END
3373 delfunc ReadFileCmd
3374endfunc
3375
Bram Moolenaaree4e0c12020-04-06 21:35:05 +02003376" Test for passing invalid arguments to autocmd
3377func Test_autocmd_invalid_args()
3378 " Additional character after * for event
Bram Moolenaar61abe7d2022-08-30 21:46:08 +01003379 call assert_fails('autocmd *a Xinvfile set ff=unix', 'E215:')
Bram Moolenaaree4e0c12020-04-06 21:35:05 +02003380 augroup Test
3381 augroup END
3382 " Invalid autocmd event
Bram Moolenaar61abe7d2022-08-30 21:46:08 +01003383 call assert_fails('autocmd Bufabc Xinvfile set ft=vim', 'E216:')
Bram Moolenaaree4e0c12020-04-06 21:35:05 +02003384 " Invalid autocmd event in a autocmd group
Bram Moolenaar61abe7d2022-08-30 21:46:08 +01003385 call assert_fails('autocmd Test Bufabc Xinvfile set ft=vim', 'E216:')
Bram Moolenaaree4e0c12020-04-06 21:35:05 +02003386 augroup! Test
3387 " Execute all autocmds
3388 call assert_fails('doautocmd * BufEnter', 'E217:')
3389 call assert_fails('augroup! x1a2b3', 'E367:')
3390 call assert_fails('autocmd BufNew <buffer=999> pwd', 'E680:')
Bram Moolenaar531be472020-09-23 22:38:05 +02003391 call assert_fails('autocmd BufNew \) set ff=unix', 'E55:')
Bram Moolenaaree4e0c12020-04-06 21:35:05 +02003392endfunc
3393
3394" Test for deep nesting of autocmds
3395func Test_autocmd_deep_nesting()
Bram Moolenaar61abe7d2022-08-30 21:46:08 +01003396 autocmd BufEnter Xdeepfile doautocmd BufEnter Xdeepfile
3397 call assert_fails('doautocmd BufEnter Xdeepfile', 'E218:')
3398 autocmd! BufEnter Xdeepfile
Bram Moolenaaree4e0c12020-04-06 21:35:05 +02003399endfunc
3400
Bram Moolenaarbe5ee862020-06-10 20:56:58 +02003401" Tests for SigUSR1 autocmd event, which is only available on posix systems.
3402func Test_autocmd_sigusr1()
3403 CheckUnix
Bram Moolenaar0056ca72022-09-23 21:26:39 +01003404 " FIXME: should this work on MacOS M1?
3405 CheckNotMacM1
Bram Moolenaar62cd26a2020-10-11 20:08:44 +02003406 CheckExecutable /bin/kill
Bram Moolenaarbe5ee862020-06-10 20:56:58 +02003407
3408 let g:sigusr1_passed = 0
3409 au SigUSR1 * let g:sigusr1_passed = 1
3410 call system('/bin/kill -s usr1 ' . getpid())
3411 call WaitForAssert({-> assert_true(g:sigusr1_passed)})
3412
3413 au! SigUSR1
3414 unlet g:sigusr1_passed
3415endfunc
3416
Bram Moolenaarb340bae2020-06-15 19:51:56 +02003417" Test for BufReadPre autocmd deleting the file
3418func Test_BufReadPre_delfile()
3419 augroup TestAuCmd
3420 au!
Bram Moolenaare7cda972022-08-29 11:02:59 +01003421 autocmd BufReadPre XbufreadPre call delete('XbufreadPre')
Bram Moolenaarb340bae2020-06-15 19:51:56 +02003422 augroup END
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01003423 call writefile([], 'XbufreadPre', 'D')
Bram Moolenaare7cda972022-08-29 11:02:59 +01003424 call assert_fails('new XbufreadPre', 'E200:')
3425 call assert_equal('XbufreadPre', @%)
Bram Moolenaarb340bae2020-06-15 19:51:56 +02003426 call assert_equal(1, &readonly)
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01003427
Bram Moolenaarb340bae2020-06-15 19:51:56 +02003428 augroup TestAuCmd
3429 au!
3430 augroup END
3431 close!
3432endfunc
3433
3434" Test for BufReadPre autocmd changing the current buffer
3435func Test_BufReadPre_changebuf()
3436 augroup TestAuCmd
3437 au!
Bram Moolenaare7cda972022-08-29 11:02:59 +01003438 autocmd BufReadPre Xchangebuf edit Xsomeotherfile
Bram Moolenaarb340bae2020-06-15 19:51:56 +02003439 augroup END
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01003440 call writefile([], 'Xchangebuf', 'D')
Bram Moolenaare7cda972022-08-29 11:02:59 +01003441 call assert_fails('new Xchangebuf', 'E201:')
Bram Moolenaarb340bae2020-06-15 19:51:56 +02003442 call assert_equal('Xsomeotherfile', @%)
3443 call assert_equal(1, &readonly)
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01003444
Bram Moolenaarb340bae2020-06-15 19:51:56 +02003445 augroup TestAuCmd
3446 au!
3447 augroup END
3448 close!
3449endfunc
3450
3451" Test for BufWipeouti autocmd changing the current buffer when reading a file
3452" in an empty buffer with 'f' flag in 'cpo'
3453func Test_BufDelete_changebuf()
3454 new
3455 augroup TestAuCmd
3456 au!
3457 autocmd BufWipeout * let bufnr = bufadd('somefile') | exe "b " .. bufnr
3458 augroup END
3459 let save_cpo = &cpo
3460 set cpo+=f
Bram Moolenaarb18b4962022-09-02 21:55:50 +01003461 call assert_fails('r Xchangebuf', ['E812:', 'E484:'])
Bram Moolenaarb340bae2020-06-15 19:51:56 +02003462 call assert_equal('somefile', @%)
3463 let &cpo = save_cpo
3464 augroup TestAuCmd
3465 au!
3466 augroup END
3467 close!
3468endfunc
3469
Bram Moolenaar0fe937f2020-06-16 22:42:04 +02003470" Test for the temporary internal window used to execute autocmds
3471func Test_autocmd_window()
3472 %bw!
3473 edit one.txt
3474 tabnew two.txt
Bram Moolenaar41cd8032021-03-13 15:47:56 +01003475 vnew three.txt
3476 tabnew four.txt
3477 tabprevious
Bram Moolenaar0fe937f2020-06-16 22:42:04 +02003478 let g:blist = []
Bram Moolenaar832adf92020-06-25 19:01:36 +02003479 augroup aucmd_win_test1
Bram Moolenaar0fe937f2020-06-16 22:42:04 +02003480 au!
3481 au BufEnter * call add(g:blist, [expand('<afile>'),
3482 \ win_gettype(bufwinnr(expand('<afile>')))])
3483 augroup END
3484
3485 doautoall BufEnter
Bram Moolenaar41cd8032021-03-13 15:47:56 +01003486 call assert_equal([
3487 \ ['one.txt', 'autocmd'],
3488 \ ['two.txt', ''],
3489 \ ['four.txt', 'autocmd'],
3490 \ ['three.txt', ''],
3491 \ ], g:blist)
Bram Moolenaar0fe937f2020-06-16 22:42:04 +02003492
Bram Moolenaar832adf92020-06-25 19:01:36 +02003493 augroup aucmd_win_test1
Bram Moolenaar0fe937f2020-06-16 22:42:04 +02003494 au!
3495 augroup END
Bram Moolenaar832adf92020-06-25 19:01:36 +02003496 augroup! aucmd_win_test1
3497 %bw!
3498endfunc
3499
3500" Test for trying to close the temporary window used for executing an autocmd
3501func Test_close_autocmd_window()
3502 %bw!
3503 edit one.txt
3504 tabnew two.txt
3505 augroup aucmd_win_test2
3506 au!
3507 au BufEnter * if expand('<afile>') == 'one.txt' | 1close | endif
3508 augroup END
3509
3510 call assert_fails('doautoall BufEnter', 'E813:')
3511
3512 augroup aucmd_win_test2
3513 au!
3514 augroup END
3515 augroup! aucmd_win_test2
Bram Moolenaarcf844172020-06-26 19:44:06 +02003516 %bwipe!
3517endfunc
3518
3519" Test for trying to close the tab that has the temporary window for exeucing
3520" an autocmd.
3521func Test_close_autocmd_tab()
3522 edit one.txt
3523 tabnew two.txt
3524 augroup aucmd_win_test
3525 au!
3526 au BufEnter * if expand('<afile>') == 'one.txt' | tabfirst | tabonly | endif
3527 augroup END
3528
3529 call assert_fails('doautoall BufEnter', 'E813:')
3530
3531 tabonly
3532 augroup aucmd_win_test
3533 au!
3534 augroup END
3535 augroup! aucmd_win_test
3536 %bwipe!
Bram Moolenaar0fe937f2020-06-16 22:42:04 +02003537endfunc
3538
Bram Moolenaarcb1956d2022-01-07 15:45:18 +00003539func Test_Visual_doautoall_redraw()
3540 call setline(1, ['a', 'b'])
Bram Moolenaar94722c52023-01-28 19:19:03 +00003541 new
Bram Moolenaarcb1956d2022-01-07 15:45:18 +00003542 wincmd p
3543 call feedkeys("G\<C-V>", 'txn')
3544 autocmd User Explode ++once redraw
3545 doautoall User Explode
3546 %bwipe!
3547endfunc
3548
Bram Moolenaar6bcb8772021-02-03 21:23:29 +01003549" This was using freed memory.
3550func Test_BufNew_arglocal()
3551 arglocal
3552 au BufNew * arglocal
3553 call assert_fails('drop xx', 'E1156:')
3554
3555 au! BufNew
3556endfunc
3557
Bram Moolenaar8ab37572021-02-03 21:56:59 +01003558func Test_autocmd_closes_window()
3559 au BufNew,BufWinLeave * e %e
3560 file yyy
3561 au BufNew,BufWinLeave * ball
Bram Moolenaaraad5f9d2021-02-06 17:30:31 +01003562 n xxx
Bram Moolenaar8ab37572021-02-03 21:56:59 +01003563
Bram Moolenaaraad5f9d2021-02-06 17:30:31 +01003564 %bwipe
Bram Moolenaar8ab37572021-02-03 21:56:59 +01003565 au! BufNew
3566 au! BufWinLeave
3567endfunc
3568
Bram Moolenaar92bb83e2021-02-03 23:04:46 +01003569func Test_autocmd_quit_psearch()
3570 sn aa bb
3571 augroup aucmd_win_test
3572 au!
3573 au BufEnter,BufLeave,BufNew,WinEnter,WinLeave,WinNew * if winnr('$') > 1 | q | endif
3574 augroup END
3575 ps /
3576
3577 augroup aucmd_win_test
3578 au!
3579 augroup END
zeertzjq7851c692022-04-21 11:14:01 +01003580 new
3581 pclose
Bram Moolenaar92bb83e2021-02-03 23:04:46 +01003582endfunc
3583
Bram Moolenaaraad5f9d2021-02-06 17:30:31 +01003584" Fuzzer found some strange combination that caused a crash.
3585func Test_autocmd_normal_mess()
Bram Moolenaardd07c022021-02-07 13:32:46 +01003586 " For unknown reason this hangs on MS-Windows
3587 CheckNotMSWindows
3588
Bram Moolenaaraad5f9d2021-02-06 17:30:31 +01003589 augroup aucmd_normal_test
3590 au BufLeave,BufWinLeave,BufHidden,BufUnload,BufDelete,BufWipeout * norm 7q/qc
3591 augroup END
Bram Moolenaar983d83f2021-02-07 12:12:43 +01003592 call assert_fails('o4', 'E1159')
Bram Moolenaaraad5f9d2021-02-06 17:30:31 +01003593 silent! H
Bram Moolenaar983d83f2021-02-07 12:12:43 +01003594 call assert_fails('e xx', 'E1159')
Bram Moolenaaraad5f9d2021-02-06 17:30:31 +01003595 normal G
3596
3597 augroup aucmd_normal_test
3598 au!
3599 augroup END
3600endfunc
3601
Bram Moolenaar8c6951f2021-02-06 18:08:45 +01003602func Test_autocmd_closing_cmdwin()
Bram Moolenaardd07c022021-02-07 13:32:46 +01003603 " For unknown reason this hangs on MS-Windows
3604 CheckNotMSWindows
3605
Bram Moolenaar8c6951f2021-02-06 18:08:45 +01003606 au BufWinLeave * nested q
3607 call assert_fails("norm 7q?\n", 'E855:')
3608
3609 au! BufWinLeave
3610 new
3611 only
3612endfunc
3613
Bram Moolenaar2c7080b2021-02-06 19:19:42 +01003614func Test_autocmd_vimgrep()
3615 augroup aucmd_vimgrep
Charlie Grovesfef44852022-04-19 16:24:12 +01003616 au QuickfixCmdPre,BufNew,BufReadCmd * sb
zeertzjq7851c692022-04-21 11:14:01 +01003617 au QuickfixCmdPre,BufNew,BufReadCmd * q9
Bram Moolenaar2c7080b2021-02-06 19:19:42 +01003618 augroup END
Bram Moolenaardd07c022021-02-07 13:32:46 +01003619 call assert_fails('lv ?a? foo', 'E926:')
Bram Moolenaar2c7080b2021-02-06 19:19:42 +01003620
3621 augroup aucmd_vimgrep
3622 au!
3623 augroup END
3624endfunc
3625
Bram Moolenaar73b8b0a2021-08-01 14:52:32 +02003626func Test_autocmd_with_block()
3627 augroup block_testing
3628 au BufReadPost *.xml {
3629 setlocal matchpairs+=<:>
3630 /<start
3631 }
Bram Moolenaar63b91732021-08-05 20:40:03 +02003632 au CursorHold * {
3633 autocmd BufReadPre * ++once echo 'one' | echo 'two'
3634 g:gotSafeState = 77
3635 }
Bram Moolenaar73b8b0a2021-08-01 14:52:32 +02003636 augroup END
3637
3638 let expected = "\n--- Autocommands ---\nblock_testing BufRead\n *.xml {^@ setlocal matchpairs+=<:>^@ /<start^@ }"
3639 call assert_equal(expected, execute('au BufReadPost *.xml'))
3640
Bram Moolenaar63b91732021-08-05 20:40:03 +02003641 doautocmd CursorHold
3642 call assert_equal(77, g:gotSafeState)
3643 unlet g:gotSafeState
3644
Bram Moolenaar73b8b0a2021-08-01 14:52:32 +02003645 augroup block_testing
3646 au!
Bram Moolenaar75ebd2a2022-06-03 17:39:46 +01003647 autocmd CursorHold * {
3648 if true
3649 # comment
3650 && true
3651
3652 && true
3653 g:done = 'yes'
3654 endif
3655 }
3656 augroup END
3657 doautocmd CursorHold
3658 call assert_equal('yes', g:done)
3659
3660 unlet g:done
3661 augroup block_testing
3662 au!
Bram Moolenaar73b8b0a2021-08-01 14:52:32 +02003663 augroup END
3664endfunc
3665
Christian Brabandtdb3b4462021-10-16 11:58:55 +01003666" Test TextChangedI and TextChanged
3667func Test_Changed_ChangedI()
3668 new
3669 call test_override("char_avail", 1)
3670 let [g:autocmd_i, g:autocmd_n] = ['','']
3671
3672 func! TextChangedAutocmdI(char)
3673 let g:autocmd_{tolower(a:char)} = a:char .. b:changedtick
3674 endfunc
3675
3676 augroup Test_TextChanged
3677 au!
3678 au TextChanged <buffer> :call TextChangedAutocmdI('N')
3679 au TextChangedI <buffer> :call TextChangedAutocmdI('I')
3680 augroup END
3681
3682 call feedkeys("ifoo\<esc>", 'tnix')
3683 " TODO: Test test does not seem to trigger TextChanged autocommand, this
3684 " requires running Vim in a terminal window.
3685 " call assert_equal('N3', g:autocmd_n)
3686 call assert_equal('I3', g:autocmd_i)
3687
3688 call feedkeys("yyp", 'tnix')
3689 " TODO: Test test does not seem to trigger TextChanged autocommand.
3690 " call assert_equal('N4', g:autocmd_n)
3691 call assert_equal('I3', g:autocmd_i)
3692
Evgeni Chasnovskid7ae2632023-10-15 09:59:00 +02003693 " TextChangedI should only trigger if change was done in Insert mode
3694 let g:autocmd_i = ''
3695 call feedkeys("yypi\<esc>", 'tnix')
3696 call assert_equal('', g:autocmd_i)
3697
3698 " TextChanged should only trigger if change was done in Normal mode
3699 let g:autocmd_n = ''
3700 call feedkeys("ibar\<esc>", 'tnix')
3701 call assert_equal('', g:autocmd_n)
3702
Christian Brabandt4bca4892023-10-27 19:26:49 +02003703 " If change is a mix of Normal and Insert modes, TextChangedI should trigger
3704 func s:validate_mixed_textchangedi(keys)
3705 call feedkeys("ifoo\<esc>", 'tnix')
3706 let g:autocmd_i = ''
3707 let g:autocmd_n = ''
3708 call feedkeys(a:keys, 'tnix')
3709 call assert_notequal('', g:autocmd_i)
3710 call assert_equal('', g:autocmd_n)
3711 endfunc
3712
3713 call s:validate_mixed_textchangedi("o\<esc>")
3714 call s:validate_mixed_textchangedi("O\<esc>")
3715 call s:validate_mixed_textchangedi("ciw\<esc>")
3716 call s:validate_mixed_textchangedi("cc\<esc>")
3717 call s:validate_mixed_textchangedi("C\<esc>")
3718 call s:validate_mixed_textchangedi("s\<esc>")
3719 call s:validate_mixed_textchangedi("S\<esc>")
3720
3721
Christian Brabandtdb3b4462021-10-16 11:58:55 +01003722 " CleanUp
3723 call test_override("char_avail", 0)
3724 au! TextChanged <buffer>
3725 au! TextChangedI <buffer>
3726 augroup! Test_TextChanged
3727 delfu TextChangedAutocmdI
3728 unlet! g:autocmd_i g:autocmd_n
3729
3730 bw!
3731endfunc
Bram Moolenaar2c7080b2021-02-06 19:19:42 +01003732
Bram Moolenaar6f2465d2022-03-22 18:13:01 +00003733func Test_closing_autocmd_window()
3734 let lines =<< trim END
3735 edit Xa.txt
3736 tabnew Xb.txt
3737 autocmd BufEnter Xa.txt unhide 1
3738 doautoall BufEnter
3739 END
3740 call v9.CheckScriptFailure(lines, 'E814:')
3741 au! BufEnter
Bram Moolenaar6f2465d2022-03-22 18:13:01 +00003742 bwipe Xa.txt
3743 bwipe Xb.txt
3744endfunc
3745
zeertzjq46bdae02023-09-24 23:16:08 +02003746func Test_switch_window_in_autocmd_window()
3747 edit Xa.txt
3748 tabnew Xb.txt
3749 autocmd BufEnter Xa.txt wincmd w
3750 doautoall BufEnter
3751 au! BufEnter
3752 bwipe Xa.txt
3753 call assert_false(bufexists('Xa.txt'))
3754 bwipe Xb.txt
3755 call assert_false(bufexists('Xb.txt'))
3756endfunc
3757
Bram Moolenaar347538f2022-03-26 16:28:06 +00003758func Test_bufwipeout_changes_window()
3759 " This should not crash, but we don't have any expectations about what
3760 " happens, changing window in BufWipeout has unpredictable results.
3761 tabedit
3762 let g:window_id = win_getid()
3763 topleft new
3764 setlocal bufhidden=wipe
3765 autocmd BufWipeout <buffer> call win_gotoid(g:window_id)
3766 tabprevious
3767 +tabclose
3768
3769 unlet g:window_id
3770 au! BufWipeout
3771 %bwipe!
3772endfunc
3773
zeertzjq021996f2022-04-10 11:44:04 +01003774func Test_v_event_readonly()
3775 autocmd CompleteChanged * let v:event.width = 0
3776 call assert_fails("normal! i\<C-X>\<C-V>", 'E46:')
3777 au! CompleteChanged
3778
3779 autocmd DirChangedPre * let v:event.directory = ''
3780 call assert_fails('cd .', 'E46:')
3781 au! DirChangedPre
3782
3783 autocmd ModeChanged * let v:event.new_mode = ''
3784 call assert_fails('normal! cc', 'E46:')
3785 au! ModeChanged
3786
3787 autocmd TextYankPost * let v:event.operator = ''
3788 call assert_fails('normal! yy', 'E46:')
3789 au! TextYankPost
3790endfunc
3791
zeertzjqc9e8fd62022-07-26 18:12:38 +01003792" Test for ModeChanged pattern
3793func Test_mode_changes()
3794 let g:index = 0
zeertzjq73916ba2023-04-26 16:50:19 +01003795 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 +01003796 func! TestMode()
3797 call assert_equal(g:mode_seq[g:index], get(v:event, "old_mode"))
3798 call assert_equal(g:mode_seq[g:index + 1], get(v:event, "new_mode"))
3799 call assert_equal(mode(1), get(v:event, "new_mode"))
3800 let g:index += 1
3801 endfunc
3802
3803 au ModeChanged * :call TestMode()
3804 let g:n_to_any = 0
3805 au ModeChanged n:* let g:n_to_any += 1
zeertzjq73916ba2023-04-26 16:50:19 +01003806 call feedkeys("i\<esc>vVca\<CR>\<C-X>\<C-L>\<esc>ggdV\<MouseMove>G", 'tnix')
zeertzjqc9e8fd62022-07-26 18:12:38 +01003807
3808 let g:V_to_v = 0
3809 au ModeChanged V:v let g:V_to_v += 1
3810 call feedkeys("Vv\<C-G>\<esc>", 'tnix')
3811 call assert_equal(len(filter(g:mode_seq[1:], {idx, val -> val == 'n'})), g:n_to_any)
3812 call assert_equal(1, g:V_to_v)
3813 call assert_equal(len(g:mode_seq) - 1, g:index)
3814
3815 let g:n_to_i = 0
3816 au ModeChanged n:i let g:n_to_i += 1
3817 let g:n_to_niI = 0
3818 au ModeChanged i:niI let g:n_to_niI += 1
3819 let g:niI_to_i = 0
3820 au ModeChanged niI:i let g:niI_to_i += 1
3821 let g:nany_to_i = 0
3822 au ModeChanged n*:i let g:nany_to_i += 1
3823 let g:i_to_n = 0
3824 au ModeChanged i:n let g:i_to_n += 1
3825 let g:nori_to_any = 0
3826 au ModeChanged [ni]:* let g:nori_to_any += 1
3827 let g:i_to_any = 0
3828 au ModeChanged i:* let g:i_to_any += 1
3829 let g:index = 0
3830 let g:mode_seq = ['n', 'i', 'niI', 'i', 'n']
3831 call feedkeys("a\<C-O>l\<esc>", 'tnix')
3832 call assert_equal(len(g:mode_seq) - 1, g:index)
3833 call assert_equal(1, g:n_to_i)
3834 call assert_equal(1, g:n_to_niI)
3835 call assert_equal(1, g:niI_to_i)
3836 call assert_equal(2, g:nany_to_i)
3837 call assert_equal(1, g:i_to_n)
3838 call assert_equal(2, g:i_to_any)
3839 call assert_equal(3, g:nori_to_any)
3840
3841 if has('terminal')
3842 let g:mode_seq += ['c', 'n', 't', 'nt', 'c', 'nt', 'n']
3843 call feedkeys(":term\<CR>\<C-W>N:bd!\<CR>", 'tnix')
3844 call assert_equal(len(g:mode_seq) - 1, g:index)
3845 call assert_equal(1, g:n_to_i)
3846 call assert_equal(1, g:n_to_niI)
3847 call assert_equal(1, g:niI_to_i)
3848 call assert_equal(2, g:nany_to_i)
3849 call assert_equal(1, g:i_to_n)
3850 call assert_equal(2, g:i_to_any)
3851 call assert_equal(5, g:nori_to_any)
3852 endif
3853
zeertzjqd1955982022-10-05 11:24:46 +01003854 let g:n_to_c = 0
3855 au ModeChanged n:c let g:n_to_c += 1
3856 let g:c_to_n = 0
3857 au ModeChanged c:n let g:c_to_n += 1
3858 let g:mode_seq += ['c', 'n', 'c', 'n']
3859 call feedkeys("q:\<C-C>\<Esc>", 'tnix')
3860 call assert_equal(len(g:mode_seq) - 1, g:index)
3861 call assert_equal(2, g:n_to_c)
3862 call assert_equal(2, g:c_to_n)
zeertzjqc9e8fd62022-07-26 18:12:38 +01003863
Bram Moolenaar61c4b042022-10-18 15:10:11 +01003864 let g:n_to_v = 0
3865 au ModeChanged n:v let g:n_to_v += 1
3866 let g:v_to_n = 0
3867 au ModeChanged v:n let g:v_to_n += 1
3868 let g:mode_seq += ['v', 'n']
3869 call feedkeys("v\<C-C>", 'tnix')
3870 call assert_equal(len(g:mode_seq) - 1, g:index)
3871 call assert_equal(1, g:n_to_v)
3872 call assert_equal(1, g:v_to_n)
zeertzjqfcaeb3d2023-11-28 20:46:29 +01003873
3874 let g:mode_seq += ['c', 'cr', 'c', 'cr', 'n']
3875 call feedkeys(":\<Insert>\<Insert>\<Insert>\<CR>", 'tnix')
3876 call assert_equal(len(g:mode_seq) - 1, g:index)
Bram Moolenaar61c4b042022-10-18 15:10:11 +01003877
zeertzjqc9e8fd62022-07-26 18:12:38 +01003878 au! ModeChanged
3879 delfunc TestMode
3880 unlet! g:mode_seq
3881 unlet! g:index
3882 unlet! g:n_to_any
3883 unlet! g:V_to_v
3884 unlet! g:n_to_i
3885 unlet! g:n_to_niI
3886 unlet! g:niI_to_i
3887 unlet! g:nany_to_i
3888 unlet! g:i_to_n
3889 unlet! g:nori_to_any
3890 unlet! g:i_to_any
zeertzjqfcaeb3d2023-11-28 20:46:29 +01003891 unlet! g:n_to_c
3892 unlet! g:c_to_n
3893 unlet! g:n_to_v
3894 unlet! g:v_to_n
zeertzjqc9e8fd62022-07-26 18:12:38 +01003895endfunc
3896
3897func Test_recursive_ModeChanged()
3898 au! ModeChanged * norm 0u
3899 sil! norm 
3900 au! ModeChanged
3901endfunc
3902
3903func Test_ModeChanged_starts_visual()
3904 " This was triggering ModeChanged before setting VIsual, causing a crash.
3905 au! ModeChanged * norm 0u
3906 sil! norm 
3907
3908 au! ModeChanged
3909endfunc
Bram Moolenaar347538f2022-03-26 16:28:06 +00003910
Charlie Grovesfef44852022-04-19 16:24:12 +01003911func Test_noname_autocmd()
3912 augroup test_noname_autocmd_group
3913 autocmd!
3914 autocmd BufEnter * call add(s:li, ["BufEnter", expand("<afile>")])
3915 autocmd BufDelete * call add(s:li, ["BufDelete", expand("<afile>")])
3916 autocmd BufLeave * call add(s:li, ["BufLeave", expand("<afile>")])
3917 autocmd BufUnload * call add(s:li, ["BufUnload", expand("<afile>")])
3918 autocmd BufWipeout * call add(s:li, ["BufWipeout", expand("<afile>")])
3919 augroup END
3920
3921 let s:li = []
3922 edit foo
3923 call assert_equal([['BufUnload', ''], ['BufDelete', ''], ['BufWipeout', ''], ['BufEnter', 'foo']], s:li)
3924
3925 au! test_noname_autocmd_group
3926 augroup! test_noname_autocmd_group
3927endfunc
3928
Yegappan Lakshmanan1755a912022-05-19 10:31:47 +01003929" Test for the autocmd_get() function
3930func Test_autocmd_get()
3931 augroup TestAutoCmdFns
3932 au!
3933 autocmd BufAdd *.vim echo "bufadd-vim"
3934 autocmd BufAdd *.py echo "bufadd-py"
3935 autocmd BufHidden *.vim echo "bufhidden"
3936 augroup END
3937 augroup TestAutoCmdFns2
3938 autocmd BufAdd *.vim echo "bufadd-vim-2"
3939 autocmd BufRead *.a1b2c3 echo "bufadd-vim-2"
3940 augroup END
3941
3942 let l = autocmd_get()
3943 call assert_true(l->len() > 0)
3944
3945 " Test for getting all the autocmds in a group
3946 let expected = [
3947 \ #{cmd: 'echo "bufadd-vim"', group: 'TestAutoCmdFns',
3948 \ pattern: '*.vim', nested: v:false, once: v:false,
3949 \ event: 'BufAdd'},
3950 \ #{cmd: 'echo "bufadd-py"', group: 'TestAutoCmdFns',
3951 \ pattern: '*.py', nested: v:false, once: v:false,
3952 \ event: 'BufAdd'},
3953 \ #{cmd: 'echo "bufhidden"', group: 'TestAutoCmdFns',
3954 \ pattern: '*.vim', nested: v:false,
3955 \ once: v:false, event: 'BufHidden'}]
3956 call assert_equal(expected, autocmd_get(#{group: 'TestAutoCmdFns'}))
3957
3958 " Test for getting autocmds for all the patterns in a group
3959 call assert_equal(expected, autocmd_get(#{group: 'TestAutoCmdFns',
3960 \ event: '*'}))
3961
3962 " Test for getting autocmds for an event in a group
3963 let expected = [
3964 \ #{cmd: 'echo "bufadd-vim"', group: 'TestAutoCmdFns',
3965 \ pattern: '*.vim', nested: v:false, once: v:false,
3966 \ event: 'BufAdd'},
3967 \ #{cmd: 'echo "bufadd-py"', group: 'TestAutoCmdFns',
3968 \ pattern: '*.py', nested: v:false, once: v:false,
3969 \ event: 'BufAdd'}]
3970 call assert_equal(expected, autocmd_get(#{group: 'TestAutoCmdFns',
3971 \ event: 'BufAdd'}))
3972
3973 " Test for getting the autocmds for all the events in a group for particular
3974 " pattern
3975 call assert_equal([{'cmd': 'echo "bufadd-py"', 'group': 'TestAutoCmdFns',
3976 \ 'pattern': '*.py', 'nested': v:false, 'once': v:false,
3977 \ 'event': 'BufAdd'}],
3978 \ autocmd_get(#{group: 'TestAutoCmdFns', event: '*', pattern: '*.py'}))
3979
3980 " Test for getting the autocmds for an events in a group for particular
3981 " pattern
3982 let l = autocmd_get(#{group: 'TestAutoCmdFns', event: 'BufAdd',
3983 \ pattern: '*.vim'})
3984 call assert_equal([
3985 \ #{cmd: 'echo "bufadd-vim"', group: 'TestAutoCmdFns',
3986 \ pattern: '*.vim', nested: v:false, once: v:false,
3987 \ event: 'BufAdd'}], l)
3988
3989 " Test for getting the autocmds for a pattern in a group
3990 let l = autocmd_get(#{group: 'TestAutoCmdFns', pattern: '*.vim'})
3991 call assert_equal([
3992 \ #{cmd: 'echo "bufadd-vim"', group: 'TestAutoCmdFns',
3993 \ pattern: '*.vim', nested: v:false, once: v:false,
3994 \ event: 'BufAdd'},
3995 \ #{cmd: 'echo "bufhidden"', group: 'TestAutoCmdFns',
3996 \ pattern: '*.vim', nested: v:false,
3997 \ once: v:false, event: 'BufHidden'}], l)
3998
3999 " Test for getting the autocmds for a pattern in all the groups
4000 let l = autocmd_get(#{pattern: '*.a1b2c3'})
4001 call assert_equal([{'cmd': 'echo "bufadd-vim-2"', 'group': 'TestAutoCmdFns2',
4002 \ 'pattern': '*.a1b2c3', 'nested': v:false, 'once': v:false,
4003 \ 'event': 'BufRead'}], l)
4004
4005 " Test for getting autocmds for a pattern without any autocmds
4006 call assert_equal([], autocmd_get(#{group: 'TestAutoCmdFns',
4007 \ pattern: '*.abc'}))
4008 call assert_equal([], autocmd_get(#{group: 'TestAutoCmdFns',
4009 \ event: 'BufAdd', pattern: '*.abc'}))
4010 call assert_equal([], autocmd_get(#{group: 'TestAutoCmdFns',
4011 \ event: 'BufWipeout'}))
4012 call assert_fails("call autocmd_get(#{group: 'abc', event: 'BufAdd'})",
4013 \ 'E367:')
4014 let cmd = "echo autocmd_get(#{group: 'TestAutoCmdFns', event: 'abc'})"
4015 call assert_fails(cmd, 'E216:')
4016 call assert_fails("call autocmd_get(#{group: 'abc'})", 'E367:')
4017 call assert_fails("echo autocmd_get(#{event: 'abc'})", 'E216:')
4018
4019 augroup TestAutoCmdFns
4020 au!
4021 augroup END
4022 call assert_equal([], autocmd_get(#{group: 'TestAutoCmdFns'}))
4023
4024 " Test for nested and once autocmds
4025 augroup TestAutoCmdFns
4026 au!
4027 autocmd VimSuspend * ++nested echo "suspend"
4028 autocmd VimResume * ++once echo "resume"
4029 augroup END
4030
4031 let expected = [
4032 \ {'cmd': 'echo "suspend"', 'group': 'TestAutoCmdFns', 'pattern': '*',
4033 \ 'nested': v:true, 'once': v:false, 'event': 'VimSuspend'},
4034 \ {'cmd': 'echo "resume"', 'group': 'TestAutoCmdFns', 'pattern': '*',
4035 \ 'nested': v:false, 'once': v:true, 'event': 'VimResume'}]
4036 call assert_equal(expected, autocmd_get(#{group: 'TestAutoCmdFns'}))
4037
4038 " Test for buffer-local autocmd
4039 augroup TestAutoCmdFns
4040 au!
4041 autocmd TextYankPost <buffer> echo "textyankpost"
4042 augroup END
4043
4044 let expected = [
4045 \ {'cmd': 'echo "textyankpost"', 'group': 'TestAutoCmdFns',
4046 \ 'pattern': '<buffer=' .. bufnr() .. '>', 'nested': v:false,
4047 \ 'once': v:false, 'bufnr': bufnr(), 'event': 'TextYankPost'}]
4048 call assert_equal(expected, autocmd_get(#{group: 'TestAutoCmdFns'}))
4049
4050 augroup TestAutoCmdFns
4051 au!
4052 augroup END
4053 augroup! TestAutoCmdFns
4054 augroup TestAutoCmdFns2
4055 au!
4056 augroup END
4057 augroup! TestAutoCmdFns2
4058
4059 call assert_fails("echo autocmd_get(#{group: []})", 'E730:')
4060 call assert_fails("echo autocmd_get(#{event: {}})", 'E731:')
4061 call assert_fails("echo autocmd_get([])", 'E1206:')
4062endfunc
4063
4064" Test for the autocmd_add() function
4065func Test_autocmd_add()
4066 " Define a single autocmd in a group
4067 call autocmd_add([#{group: 'TestAcSet', event: 'BufAdd', pattern: '*.sh',
4068 \ cmd: 'echo "bufadd"', once: v:true, nested: v:true}])
4069 call assert_equal([#{cmd: 'echo "bufadd"', group: 'TestAcSet',
4070 \ pattern: '*.sh', nested: v:true, once: v:true,
4071 \ event: 'BufAdd'}], autocmd_get(#{group: 'TestAcSet'}))
4072
4073 " Define two autocmds in the same group
4074 call autocmd_delete([#{group: 'TestAcSet'}])
4075 call autocmd_add([#{group: 'TestAcSet', event: 'BufAdd', pattern: '*.sh',
4076 \ cmd: 'echo "bufadd"'},
4077 \ #{group: 'TestAcSet', event: 'BufEnter', pattern: '*.sh',
4078 \ cmd: 'echo "bufenter"'}])
4079 call assert_equal([
4080 \ #{cmd: 'echo "bufadd"', group: 'TestAcSet', pattern: '*.sh',
4081 \ nested: v:false, once: v:false, event: 'BufAdd'},
4082 \ #{cmd: 'echo "bufenter"', group: 'TestAcSet', pattern: '*.sh',
4083 \ nested: v:false, once: v:false, event: 'BufEnter'}],
4084 \ autocmd_get(#{group: 'TestAcSet'}))
4085
4086 " Define a buffer-local autocmd
4087 call autocmd_delete([#{group: 'TestAcSet'}])
4088 call autocmd_add([#{group: 'TestAcSet', event: 'CursorHold',
4089 \ bufnr: bufnr(), cmd: 'echo "cursorhold"'}])
4090 call assert_equal([
4091 \ #{cmd: 'echo "cursorhold"', group: 'TestAcSet',
4092 \ pattern: '<buffer=' .. bufnr() .. '>', nested: v:false,
4093 \ once: v:false, bufnr: bufnr(), event: 'CursorHold'}],
4094 \ autocmd_get(#{group: 'TestAcSet'}))
4095
4096 " Use an invalid buffer number
4097 call autocmd_delete([#{group: 'TestAcSet'}])
4098 call autocmd_add([#{group: 'TestAcSet', event: 'BufEnter',
4099 \ bufnr: -1, cmd: 'echo "bufenter"'}])
4100 let l = [#{group: 'TestAcSet', event: 'BufAdd', bufnr: 9999,
4101 \ cmd: 'echo "bufadd"'}]
4102 call assert_fails("echo autocmd_add(l)", 'E680:')
Yegappan Lakshmanan00e977c2022-06-01 12:31:53 +01004103 let l = [#{group: 'TestAcSet', event: 'BufAdd', bufnr: 9999,
4104 \ pattern: '*.py', cmd: 'echo "bufadd"'}]
4105 call assert_fails("echo autocmd_add(l)", 'E680:')
4106 let l = [#{group: 'TestAcSet', event: 'BufAdd', bufnr: 9999,
4107 \ pattern: ['*.py', '*.c'], cmd: 'echo "bufadd"'}]
4108 call assert_fails("echo autocmd_add(l)", 'E680:')
Yegappan Lakshmanan1755a912022-05-19 10:31:47 +01004109 let l = [#{group: 'TestAcSet', event: 'BufRead', bufnr: [],
4110 \ cmd: 'echo "bufread"'}]
4111 call assert_fails("echo autocmd_add(l)", 'E745:')
4112 call assert_equal([], autocmd_get(#{group: 'TestAcSet'}))
4113
4114 " Add two commands to the same group, event and pattern
4115 call autocmd_delete([#{group: 'TestAcSet'}])
4116 call autocmd_add([#{group: 'TestAcSet', event: 'BufUnload',
4117 \ pattern: 'abc', cmd: 'echo "cmd1"'}])
4118 call autocmd_add([#{group: 'TestAcSet', event: 'BufUnload',
4119 \ pattern: 'abc', cmd: 'echo "cmd2"'}])
4120 call assert_equal([
4121 \ #{cmd: 'echo "cmd1"', group: 'TestAcSet', pattern: 'abc',
4122 \ nested: v:false, once: v:false, event: 'BufUnload'},
4123 \ #{cmd: 'echo "cmd2"', group: 'TestAcSet', pattern: 'abc',
4124 \ nested: v:false, once: v:false, event: 'BufUnload'}],
4125 \ autocmd_get(#{group: 'TestAcSet'}))
4126
4127 " When adding a new autocmd, if the autocmd 'group' is not specified, then
4128 " the current autocmd group should be used.
4129 call autocmd_delete([#{group: 'TestAcSet'}])
4130 augroup TestAcSet
4131 call autocmd_add([#{event: 'BufHidden', pattern: 'abc', cmd: 'echo "abc"'}])
4132 augroup END
4133 call assert_equal([
4134 \ #{cmd: 'echo "abc"', group: 'TestAcSet', pattern: 'abc',
4135 \ nested: v:false, once: v:false, event: 'BufHidden'}],
4136 \ autocmd_get(#{group: 'TestAcSet'}))
4137
Yegappan Lakshmanan971f6822022-05-24 11:40:11 +01004138 " Test for replacing a cmd for an event in a group
4139 call autocmd_delete([#{group: 'TestAcSet'}])
4140 call autocmd_add([#{replace: v:true, group: 'TestAcSet', event: 'BufEnter',
4141 \ pattern: '*.py', cmd: 'echo "bufenter"'}])
4142 call autocmd_add([#{replace: v:true, group: 'TestAcSet', event: 'BufEnter',
4143 \ pattern: '*.py', cmd: 'echo "bufenter"'}])
4144 call assert_equal([
4145 \ #{cmd: 'echo "bufenter"', group: 'TestAcSet', pattern: '*.py',
4146 \ nested: v:false, once: v:false, event: 'BufEnter'}],
4147 \ autocmd_get(#{group: 'TestAcSet'}))
4148
4149 " Test for adding a command for an unsupported autocmd event
Yegappan Lakshmanan1755a912022-05-19 10:31:47 +01004150 let l = [#{group: 'TestAcSet', event: 'abc', pattern: '*.sh',
4151 \ cmd: 'echo "bufadd"'}]
4152 call assert_fails('call autocmd_add(l)', 'E216:')
4153
Yegappan Lakshmanane0ff3a72022-05-27 18:05:33 +01004154 " Test for using a list of events and patterns
4155 call autocmd_delete([#{group: 'TestAcSet'}])
4156 let l = [#{group: 'TestAcSet', event: ['BufEnter', 'BufLeave'],
4157 \ pattern: ['*.py', '*.sh'], cmd: 'echo "bufcmds"'}]
4158 call autocmd_add(l)
4159 call assert_equal([
4160 \ #{cmd: 'echo "bufcmds"', group: 'TestAcSet', pattern: '*.py',
4161 \ nested: v:false, once: v:false, event: 'BufEnter'},
4162 \ #{cmd: 'echo "bufcmds"', group: 'TestAcSet', pattern: '*.sh',
4163 \ nested: v:false, once: v:false, event: 'BufEnter'},
4164 \ #{cmd: 'echo "bufcmds"', group: 'TestAcSet', pattern: '*.py',
4165 \ nested: v:false, once: v:false, event: 'BufLeave'},
4166 \ #{cmd: 'echo "bufcmds"', group: 'TestAcSet', pattern: '*.sh',
4167 \ nested: v:false, once: v:false, event: 'BufLeave'}],
4168 \ autocmd_get(#{group: 'TestAcSet'}))
4169
4170 " Test for invalid values for 'event' item
4171 call autocmd_delete([#{group: 'TestAcSet'}])
4172 let l = [#{group: 'TestAcSet', event: test_null_string(),
4173 \ pattern: "*.py", cmd: 'echo "bufcmds"'}]
4174 call assert_fails('call autocmd_add(l)', 'E928:')
4175 let l = [#{group: 'TestAcSet', event: test_null_list(),
4176 \ pattern: "*.py", cmd: 'echo "bufcmds"'}]
4177 call assert_fails('call autocmd_add(l)', 'E714:')
4178 let l = [#{group: 'TestAcSet', event: {},
4179 \ pattern: "*.py", cmd: 'echo "bufcmds"'}]
4180 call assert_fails('call autocmd_add(l)', 'E777:')
4181 let l = [#{group: 'TestAcSet', event: [{}],
4182 \ pattern: "*.py", cmd: 'echo "bufcmds"'}]
4183 call assert_fails('call autocmd_add(l)', 'E928:')
4184 let l = [#{group: 'TestAcSet', event: [test_null_string()],
4185 \ pattern: "*.py", cmd: 'echo "bufcmds"'}]
4186 call assert_fails('call autocmd_add(l)', 'E928:')
4187 let l = [#{group: 'TestAcSet', event: 'BufEnter,BufLeave',
4188 \ pattern: '*.py', cmd: 'echo "bufcmds"'}]
4189 call assert_fails('call autocmd_add(l)', 'E216:')
4190 let l = [#{group: 'TestAcSet', event: [],
4191 \ pattern: "*.py", cmd: 'echo "bufcmds"'}]
4192 call autocmd_add(l)
4193 let l = [#{group: 'TestAcSet', event: [""],
4194 \ pattern: "*.py", cmd: 'echo "bufcmds"'}]
4195 call assert_fails('call autocmd_add(l)', 'E216:')
4196 let l = [#{group: 'TestAcSet', event: "",
4197 \ pattern: "*.py", cmd: 'echo "bufcmds"'}]
4198 call autocmd_add(l)
4199 call assert_equal([], autocmd_get(#{group: 'TestAcSet'}))
4200
4201 " Test for invalid values for 'pattern' item
4202 let l = [#{group: 'TestAcSet', event: "BufEnter",
4203 \ pattern: test_null_string(), cmd: 'echo "bufcmds"'}]
Yegappan Lakshmanan00e977c2022-06-01 12:31:53 +01004204 call assert_fails('call autocmd_add(l)', 'E928:')
Yegappan Lakshmanane0ff3a72022-05-27 18:05:33 +01004205 let l = [#{group: 'TestAcSet', event: "BufEnter",
4206 \ pattern: test_null_list(), cmd: 'echo "bufcmds"'}]
4207 call assert_fails('call autocmd_add(l)', 'E714:')
4208 let l = [#{group: 'TestAcSet', event: "BufEnter",
4209 \ pattern: {}, cmd: 'echo "bufcmds"'}]
4210 call assert_fails('call autocmd_add(l)', 'E777:')
4211 let l = [#{group: 'TestAcSet', event: "BufEnter",
4212 \ pattern: [{}], cmd: 'echo "bufcmds"'}]
4213 call assert_fails('call autocmd_add(l)', 'E928:')
4214 let l = [#{group: 'TestAcSet', event: "BufEnter",
4215 \ pattern: [test_null_string()], cmd: 'echo "bufcmds"'}]
4216 call assert_fails('call autocmd_add(l)', 'E928:')
4217 let l = [#{group: 'TestAcSet', event: "BufEnter",
4218 \ pattern: [], cmd: 'echo "bufcmds"'}]
4219 call autocmd_add(l)
4220 let l = [#{group: 'TestAcSet', event: "BufEnter",
4221 \ pattern: [""], cmd: 'echo "bufcmds"'}]
4222 call autocmd_add(l)
4223 let l = [#{group: 'TestAcSet', event: "BufEnter",
4224 \ pattern: "", cmd: 'echo "bufcmds"'}]
4225 call autocmd_add(l)
4226 call assert_equal([], autocmd_get(#{group: 'TestAcSet'}))
4227
4228 let l = [#{group: 'TestAcSet', event: 'BufEnter,abc,BufLeave',
4229 \ pattern: '*.py', cmd: 'echo "bufcmds"'}]
4230 call assert_fails('call autocmd_add(l)', 'E216:')
4231
Yegappan Lakshmanan1755a912022-05-19 10:31:47 +01004232 call assert_fails("call autocmd_add({})", 'E1211:')
4233 call assert_equal(v:false, autocmd_add(test_null_list()))
4234 call assert_true(autocmd_add([[]]))
4235 call assert_true(autocmd_add([test_null_dict()]))
4236
4237 augroup TestAcSet
4238 au!
4239 augroup END
4240
4241 call autocmd_add([#{group: 'TestAcSet'}])
4242 call autocmd_add([#{group: 'TestAcSet', event: 'BufAdd'}])
4243 call autocmd_add([#{group: 'TestAcSet', pat: '*.sh'}])
4244 call autocmd_add([#{group: 'TestAcSet', cmd: 'echo "a"'}])
4245 call autocmd_add([#{group: 'TestAcSet', event: 'BufAdd', pat: '*.sh'}])
4246 call autocmd_add([#{group: 'TestAcSet', event: 'BufAdd', cmd: 'echo "a"'}])
4247 call autocmd_add([#{group: 'TestAcSet', pat: '*.sh', cmd: 'echo "a"'}])
4248 call assert_equal([], autocmd_get(#{group: 'TestAcSet'}))
4249
4250 augroup! TestAcSet
4251endfunc
4252
4253" Test for deleting autocmd events and groups
4254func Test_autocmd_delete()
4255 " Delete an event in an autocmd group
4256 augroup TestAcSet
4257 au!
4258 au BufAdd *.sh echo "bufadd"
4259 au BufEnter *.sh echo "bufenter"
4260 augroup END
4261 call autocmd_delete([#{group: 'TestAcSet', event: 'BufAdd'}])
4262 call assert_equal([#{cmd: 'echo "bufenter"', group: 'TestAcSet',
4263 \ pattern: '*.sh', nested: v:false, once: v:false,
4264 \ event: 'BufEnter'}], autocmd_get(#{group: 'TestAcSet'}))
4265
4266 " Delete all the events in an autocmd group
4267 augroup TestAcSet
4268 au BufAdd *.sh echo "bufadd"
4269 augroup END
4270 call autocmd_delete([#{group: 'TestAcSet', event: '*'}])
4271 call assert_equal([], autocmd_get(#{group: 'TestAcSet'}))
4272
4273 " Delete a non-existing autocmd group
4274 call assert_fails("call autocmd_delete([#{group: 'abc'}])", 'E367:')
4275 " Delete a non-existing autocmd event
4276 let l = [#{group: 'TestAcSet', event: 'abc'}]
4277 call assert_fails("call autocmd_delete(l)", 'E216:')
4278 " Delete a non-existing autocmd pattern
4279 let l = [#{group: 'TestAcSet', event: 'BufAdd', pat: 'abc'}]
4280 call assert_true(autocmd_delete(l))
Yegappan Lakshmanan00e977c2022-06-01 12:31:53 +01004281 " Delete an autocmd for a non-existing buffer
4282 let l = [#{event: '*', bufnr: 9999, cmd: 'echo "x"'}]
4283 call assert_fails('call autocmd_delete(l)', 'E680:')
Yegappan Lakshmanan1755a912022-05-19 10:31:47 +01004284
4285 " Delete an autocmd group
4286 augroup TestAcSet
4287 au!
4288 au BufAdd *.sh echo "bufadd"
4289 au BufEnter *.sh echo "bufenter"
4290 augroup END
4291 call autocmd_delete([#{group: 'TestAcSet'}])
4292 call assert_fails("call autocmd_get(#{group: 'TestAcSet'})", 'E367:')
4293
4294 call assert_true(autocmd_delete([[]]))
4295 call assert_true(autocmd_delete([test_null_dict()]))
4296endfunc
4297
Bram Moolenaar8f3c3c62022-10-18 17:05:54 +01004298func Test_autocmd_split_dummy()
4299 " Autocommand trying to split a window containing a dummy buffer.
Bram Moolenaar94722c52023-01-28 19:19:03 +00004300 auto BufReadPre * exe "sbuf " .. expand("<abuf>")
Bram Moolenaar8f3c3c62022-10-18 17:05:54 +01004301 " Avoid the "W11" prompt
4302 au FileChangedShell * let v:fcs_choice = 'reload'
4303 func Xautocmd_changelist()
4304 cal writefile(['Xtestfile2:4:4'], 'Xerr')
4305 edit Xerr
4306 lex 'Xtestfile2:4:4'
4307 endfunc
4308 call Xautocmd_changelist()
Bram Moolenaar53c5c9f2022-10-18 17:25:03 +01004309 " Should get E86, but it doesn't always happen (timing?)
4310 silent! call Xautocmd_changelist()
Bram Moolenaar8f3c3c62022-10-18 17:05:54 +01004311
4312 au! BufReadPre
4313 au! FileChangedShell
4314 delfunc Xautocmd_changelist
4315 bwipe! Xerr
4316 call delete('Xerr')
4317endfunc
4318
Bram Moolenaare76062c2022-11-28 18:51:43 +00004319" This was crashing because there was only one window to execute autocommands
4320" in.
4321func Test_autocmd_nested_setbufvar()
4322 CheckFeature python3
4323
4324 set hidden
4325 edit Xaaa
4326 edit Xbbb
4327 call setline(1, 'bar')
4328 enew
4329 au BufWriteCmd Xbbb ++nested call setbufvar('Xaaa', '&ft', 'foo') | bw! Xaaa
4330 au FileType foo call py3eval('vim.current.buffer.options["cindent"]')
4331 wall
4332
4333 au! BufWriteCmd
4334 au! FileType foo
4335 set nohidden
4336 call delete('Xaaa')
4337 call delete('Xbbb')
4338 %bwipe!
4339endfunc
4340
Christian Brabandt9aee8ec2022-12-16 16:41:23 +00004341func SetupVimTest_shm()
4342 let g:bwe = []
4343 let g:brp = []
4344 set shortmess+=F
zeertzjq657b31f2023-04-15 21:28:02 +01004345 messages clear
Christian Brabandt9aee8ec2022-12-16 16:41:23 +00004346
4347 let dirname='XVimTestSHM'
4348 call mkdir(dirname, 'R')
4349 call writefile(['test'], dirname .. '/1')
4350 call writefile(['test'], dirname .. '/2')
4351 call writefile(['test'], dirname .. '/3')
4352
4353 augroup test
4354 autocmd!
4355 autocmd BufWinEnter * call add(g:bwe, $'BufWinEnter: {expand('<amatch>')}')
4356 autocmd BufReadPost * call add(g:brp, $'BufReadPost: {expand('<amatch>')}')
4357 augroup END
4358
4359 call setqflist([
4360 \ {'filename': dirname .. '/1', 'lnum': 1, 'col': 1, 'text': 'test', 'vcol': 0},
4361 \ {'filename': dirname .. '/2', 'lnum': 1, 'col': 1, 'text': 'test', 'vcol': 0},
4362 \ {'filename': dirname .. '/3', 'lnum': 1, 'col': 1, 'text': 'test', 'vcol': 0}
4363 \ ])
4364 cdo! substitute/test/TEST
4365
4366 " clean up
4367 noa enew!
4368 set shortmess&vim
4369 augroup test
4370 autocmd!
4371 augroup END
4372 augroup! test
4373endfunc
4374
4375func Test_autocmd_shortmess()
4376 CheckNotMSWindows
4377
4378 call SetupVimTest_shm()
4379 let output = execute(':mess')->split('\n')
4380
4381 let info = copy(output)->filter({idx, val -> val =~# '\d of 3'} )
4382 let bytes = copy(output)->filter({idx, val -> val =~# 'bytes'} )
4383
4384 " We test the following here:
4385 " BufReadPost should have been triggered 3 times, once per file
4386 " BufWinEnter should have been triggered 3 times, once per file
4387 " FileInfoMessage should have been shown 3 times, regardless of shm option
4388 " "(x of 3)" message from :cnext has been shown 3 times
4389
4390 call assert_equal(3, g:brp->len())
4391 call assert_equal(3, g:bwe->len())
4392 call assert_equal(3, info->len())
4393 call assert_equal(3, bytes->len())
4394
4395 delfunc SetupVimTest_shm
4396endfunc
Bram Moolenaare76062c2022-11-28 18:51:43 +00004397
Bram Moolenaarbc2b71d2020-02-17 21:33:30 +01004398" vim: shiftwidth=2 sts=2 expandtab