blob: 371abcbd752e33a711156a9f066daf8a7d49efce [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
zeertzjqb2ec0da2024-03-09 15:39:27 +0100822" This used to trigger WinClosed twice for the same window, and the window's
823" buffer was NULL in the second autocommand.
824func Test_WinClosed_BufUnload_close_other()
825 tabnew
826 let g:tab = tabpagenr()
827 let g:buf = bufnr()
828 new
829 setlocal bufhidden=wipe
830 augroup test-WinClosed
831 autocmd BufUnload * ++once exe g:buf .. 'bwipe!'
832 autocmd WinClosed * call tabpagebuflist(g:tab)
833 augroup END
834 close
835
836 unlet g:tab
837 unlet g:buf
838 autocmd! test-WinClosed
839 augroup! test-WinClosed
840 %bwipe!
841endfunc
842
Bram Moolenaare99e8442016-07-26 20:43:40 +0200843func s:AddAnAutocmd()
844 augroup vimBarTest
845 au BufReadCmd * echo 'hello'
846 augroup END
847 call assert_equal(3, len(split(execute('au vimBarTest'), "\n")))
848endfunc
849
850func Test_early_bar()
851 " test that a bar is recognized before the {event}
852 call s:AddAnAutocmd()
Bram Moolenaarb8e642f2021-11-20 10:38:25 +0000853 augroup vimBarTest | au! | let done = 77 | augroup END
Bram Moolenaare99e8442016-07-26 20:43:40 +0200854 call assert_equal(1, len(split(execute('au vimBarTest'), "\n")))
Bram Moolenaarb8e642f2021-11-20 10:38:25 +0000855 call assert_equal(77, done)
Bram Moolenaare99e8442016-07-26 20:43:40 +0200856
857 call s:AddAnAutocmd()
Bram Moolenaarb8e642f2021-11-20 10:38:25 +0000858 augroup vimBarTest| au!| let done = 88 | augroup END
Bram Moolenaare99e8442016-07-26 20:43:40 +0200859 call assert_equal(1, len(split(execute('au vimBarTest'), "\n")))
Bram Moolenaarb8e642f2021-11-20 10:38:25 +0000860 call assert_equal(88, done)
Bram Moolenaare99e8442016-07-26 20:43:40 +0200861
862 " test that a bar is recognized after the {event}
863 call s:AddAnAutocmd()
Bram Moolenaarb8e642f2021-11-20 10:38:25 +0000864 augroup vimBarTest| au!BufReadCmd| let done = 99 | augroup END
Bram Moolenaare99e8442016-07-26 20:43:40 +0200865 call assert_equal(1, len(split(execute('au vimBarTest'), "\n")))
Bram Moolenaarb8e642f2021-11-20 10:38:25 +0000866 call assert_equal(99, done)
Bram Moolenaare99e8442016-07-26 20:43:40 +0200867
868 " test that a bar is recognized after the {group}
869 call s:AddAnAutocmd()
870 au! vimBarTest|echo 'hello'
871 call assert_equal(1, len(split(execute('au vimBarTest'), "\n")))
872endfunc
Bram Moolenaarf2c4c392016-07-29 20:50:24 +0200873
Bram Moolenaar5c809082016-09-01 16:21:48 +0200874func RemoveGroup()
875 autocmd! StartOK
876 augroup! StartOK
877endfunc
878
Bram Moolenaarf2c4c392016-07-29 20:50:24 +0200879func Test_augroup_warning()
880 augroup TheWarning
881 au VimEnter * echo 'entering'
882 augroup END
Bram Moolenaar5dc4e2f2020-11-25 14:15:12 +0100883 call assert_match("TheWarning.*VimEnter", execute('au VimEnter'))
Bram Moolenaarf2c4c392016-07-29 20:50:24 +0200884 redir => res
885 augroup! TheWarning
886 redir END
Bram Moolenaar5dc4e2f2020-11-25 14:15:12 +0100887 call assert_match("W19:", res)
888 call assert_match("-Deleted-.*VimEnter", execute('au VimEnter'))
Bram Moolenaarf2c4c392016-07-29 20:50:24 +0200889
890 " check "Another" does not take the pace of the deleted entry
891 augroup Another
892 augroup END
Bram Moolenaar5dc4e2f2020-11-25 14:15:12 +0100893 call assert_match("-Deleted-.*VimEnter", execute('au VimEnter'))
Bram Moolenaaraeac9002016-09-06 22:15:08 +0200894 augroup! Another
Bram Moolenaar5c809082016-09-01 16:21:48 +0200895
896 " no warning for postpone aucmd delete
897 augroup StartOK
898 au VimEnter * call RemoveGroup()
899 augroup END
Bram Moolenaar5dc4e2f2020-11-25 14:15:12 +0100900 call assert_match("StartOK.*VimEnter", execute('au VimEnter'))
Bram Moolenaar5c809082016-09-01 16:21:48 +0200901 redir => res
902 doautocmd VimEnter
903 redir END
Bram Moolenaar5dc4e2f2020-11-25 14:15:12 +0100904 call assert_notmatch("W19:", res)
Bram Moolenaarde653f02016-09-03 16:59:06 +0200905 au! VimEnter
Bram Moolenaarad48e6c2020-04-21 22:19:45 +0200906
907 call assert_fails('augroup!', 'E471:')
Bram Moolenaarf2c4c392016-07-29 20:50:24 +0200908endfunc
Bram Moolenaarb62cc362016-09-03 16:43:53 +0200909
Bram Moolenaar8d84ff12017-10-26 16:42:16 +0200910func Test_BufReadCmdHelp()
911 " This used to cause access to free memory
912 au BufReadCmd * e +h
913 help
914
Bram Moolenaar8d84ff12017-10-26 16:42:16 +0200915 au! BufReadCmd
916endfunc
917
918func Test_BufReadCmdHelpJump()
919 " This used to cause access to free memory
920 au BufReadCmd * e +h{
Bram Moolenaarcf1ba352017-10-27 00:55:04 +0200921 " } to fix highlighting
922 call assert_fails('help', 'E434:')
Bram Moolenaar8d84ff12017-10-26 16:42:16 +0200923
Bram Moolenaar8d84ff12017-10-26 16:42:16 +0200924 au! BufReadCmd
925endfunc
926
zeertzjq93f72cc2022-08-26 15:34:52 +0100927" BufReadCmd is triggered for a "nofile" buffer. Check all values.
Bram Moolenaarb1d2c812022-08-26 11:55:01 +0100928func Test_BufReadCmdNofile()
zeertzjq93f72cc2022-08-26 15:34:52 +0100929 for val in ['nofile',
930 \ 'nowrite',
931 \ 'acwrite',
932 \ 'quickfix',
933 \ 'help',
934 \ 'terminal',
935 \ 'prompt',
936 \ 'popup',
937 \ ]
938 new somefile
939 exe 'set buftype=' .. val
940 au BufReadCmd somefile call setline(1, 'triggered')
941 edit
942 call assert_equal('triggered', getline(1))
Bram Moolenaarb1d2c812022-08-26 11:55:01 +0100943
zeertzjq93f72cc2022-08-26 15:34:52 +0100944 au! BufReadCmd
945 bwipe!
946 endfor
Bram Moolenaarb1d2c812022-08-26 11:55:01 +0100947endfunc
948
Bram Moolenaarb62cc362016-09-03 16:43:53 +0200949func Test_augroup_deleted()
Bram Moolenaarde653f02016-09-03 16:59:06 +0200950 " This caused a crash before E936 was introduced
Bram Moolenaarb62cc362016-09-03 16:43:53 +0200951 augroup x
Bram Moolenaarde653f02016-09-03 16:59:06 +0200952 call assert_fails('augroup! x', 'E936:')
953 au VimEnter * echo
954 augroup end
Bram Moolenaarb62cc362016-09-03 16:43:53 +0200955 augroup! x
Bram Moolenaar5dc4e2f2020-11-25 14:15:12 +0100956 call assert_match("-Deleted-.*VimEnter", execute('au VimEnter'))
Bram Moolenaarde653f02016-09-03 16:59:06 +0200957 au! VimEnter
Bram Moolenaarb62cc362016-09-03 16:43:53 +0200958endfunc
959
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200960" Tests for autocommands on :close command.
961" This used to be in test13.
962func Test_three_windows()
Bram Moolenaarb3435b02016-09-29 20:54:59 +0200963 " Clean up buffers, because in some cases this function fails.
964 call s:cleanup_buffers()
965
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200966 " Write three files and open them, each in a window.
967 " Then go to next window, with autocommand that deletes the previous one.
968 " Do this twice, writing the file.
969 e! Xtestje1
970 call setline(1, 'testje1')
971 w
972 sp Xtestje2
973 call setline(1, 'testje2')
974 w
975 sp Xtestje3
976 call setline(1, 'testje3')
977 w
978 wincmd w
979 au WinLeave Xtestje2 bwipe
980 wincmd w
981 call assert_equal('Xtestje1', expand('%'))
982
983 au WinLeave Xtestje1 bwipe Xtestje3
984 close
985 call assert_equal('Xtestje1', expand('%'))
986
987 " Test deleting the buffer on a Unload event. If this goes wrong there
988 " will be the ATTENTION prompt.
989 e Xtestje1
990 au!
991 au! BufUnload Xtestje1 bwipe
992 call assert_fails('e Xtestje3', 'E937:')
993 call assert_equal('Xtestje3', expand('%'))
994
995 e Xtestje2
996 sp Xtestje1
997 call assert_fails('e', 'E937:')
Bram Moolenaara997b452018-04-17 23:24:06 +0200998 call assert_equal('Xtestje1', expand('%'))
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200999
1000 " Test changing buffers in a BufWipeout autocommand. If this goes wrong
1001 " there are ml_line errors and/or a Crash.
1002 au!
1003 only
1004 e Xanother
1005 e Xtestje1
1006 bwipe Xtestje2
1007 bwipe Xtestje3
1008 au BufWipeout Xtestje1 buf Xtestje1
1009 bwipe
1010 call assert_equal('Xanother', expand('%'))
1011
1012 only
1013 help
1014 wincmd w
1015 1quit
1016 call assert_equal('Xanother', expand('%'))
1017
1018 au!
Bram Moolenaar4520d442017-03-19 16:09:46 +01001019 enew
Bram Moolenaare0ab94e2016-09-04 19:50:54 +02001020 call delete('Xtestje1')
1021 call delete('Xtestje2')
1022 call delete('Xtestje3')
1023endfunc
Bram Moolenaare13b9af2017-01-13 22:01:02 +01001024
1025func Test_BufEnter()
1026 au! BufEnter
1027 au Bufenter * let val = val . '+'
1028 let g:val = ''
1029 split NewFile
1030 call assert_equal('+', g:val)
1031 bwipe!
1032 call assert_equal('++', g:val)
1033
1034 " Also get BufEnter when editing a directory
Bram Moolenaar6f14da12022-09-07 21:30:44 +01001035 call mkdir('Xbufenterdir', 'D')
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +01001036 split Xbufenterdir
Bram Moolenaare13b9af2017-01-13 22:01:02 +01001037 call assert_equal('+++', g:val)
Bram Moolenaare94260f2017-03-21 15:50:12 +01001038
1039 " On MS-Windows we can't edit the directory, make sure we wipe the right
1040 " buffer.
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +01001041 bwipe! Xbufenterdir
Bram Moolenaare13b9af2017-01-13 22:01:02 +01001042 au! BufEnter
Bram Moolenaara9b5b852022-08-26 13:16:20 +01001043
1044 " Editing a "nofile" buffer doesn't read the file but does trigger BufEnter
zeertzjq93f72cc2022-08-26 15:34:52 +01001045 " for historic reasons. Also test other 'buftype' values.
1046 for val in ['nofile',
1047 \ 'nowrite',
1048 \ 'acwrite',
1049 \ 'quickfix',
1050 \ 'help',
1051 \ 'terminal',
1052 \ 'prompt',
1053 \ 'popup',
1054 \ ]
1055 new somefile
1056 exe 'set buftype=' .. val
1057 au BufEnter somefile call setline(1, 'some text')
1058 edit
1059 call assert_equal('some text', getline(1))
1060 bwipe!
1061 au! BufEnter
1062 endfor
Bram Moolenaar9fda8152022-11-19 13:14:10 +00001063
1064 new
1065 new
1066 autocmd BufEnter * ++once close
1067 call assert_fails('close', 'E1312:')
1068
1069 au! BufEnter
1070 only
Bram Moolenaare13b9af2017-01-13 22:01:02 +01001071endfunc
Bram Moolenaar8c752bd2017-03-19 17:09:56 +01001072
1073" Closing a window might cause an endless loop
1074" E814 for older Vims
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001075func Test_autocmd_bufwipe_in_SessLoadPost()
Bram Moolenaar1d68d9b2017-10-13 22:33:32 +02001076 edit Xtest
Bram Moolenaar8c752bd2017-03-19 17:09:56 +01001077 tabnew
Bram Moolenaar1d68d9b2017-10-13 22:33:32 +02001078 file Xsomething
Bram Moolenaar8c752bd2017-03-19 17:09:56 +01001079 set noswapfile
Bram Moolenaar8c752bd2017-03-19 17:09:56 +01001080 mksession!
1081
Bram Moolenaarc79745a2019-05-20 22:12:34 +02001082 let content =<< trim [CODE]
Bram Moolenaar62cd26a2020-10-11 20:08:44 +02001083 call test_override('ui_delay', 10)
Bram Moolenaarc79745a2019-05-20 22:12:34 +02001084 set nocp noswapfile
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001085 let v:swapchoice = "e"
Bram Moolenaarc79745a2019-05-20 22:12:34 +02001086 augroup test_autocmd_sessionload
1087 autocmd!
1088 autocmd SessionLoadPost * exe bufnr("Xsomething") . "bw!"
1089 augroup END
1090
1091 func WriteErrors()
Bram Moolenaarbe9fc5b2022-09-09 17:09:35 +01001092 call writefile([execute("messages")], "XerrorsBwipe")
Bram Moolenaarc79745a2019-05-20 22:12:34 +02001093 endfunc
1094 au VimLeave * call WriteErrors()
1095 [CODE]
1096
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01001097 call writefile(content, 'Xvimrc', 'D')
Bram Moolenaar93344c22019-08-14 21:12:05 +02001098 call system(GetVimCommand('Xvimrc') .. ' --not-a-term --noplugins -S Session.vim -c cq')
Bram Moolenaarae04a602022-09-09 15:08:10 +01001099 sleep 100m
Bram Moolenaarbe9fc5b2022-09-09 17:09:35 +01001100 let errors = join(readfile('XerrorsBwipe'))
Bram Moolenaare2e40752020-09-04 21:18:46 +02001101 call assert_match('E814:', errors)
Bram Moolenaar8c752bd2017-03-19 17:09:56 +01001102
Bram Moolenaar8c752bd2017-03-19 17:09:56 +01001103 set swapfile
Bram Moolenaarbe9fc5b2022-09-09 17:09:35 +01001104 for file in ['Session.vim', 'XerrorsBwipe']
Bram Moolenaar8c752bd2017-03-19 17:09:56 +01001105 call delete(file)
1106 endfor
1107endfunc
1108
Bram Moolenaar797e63b2021-01-15 16:22:52 +01001109" Using :blast and :ball for many events caused a crash, because b_nwindows was
1110" not incremented correctly.
1111func Test_autocmd_blast_badd()
1112 let content =<< trim [CODE]
1113 au BufNew,BufAdd,BufWinEnter,BufEnter,BufLeave,BufWinLeave,BufUnload,VimEnter foo* blast
1114 edit foo1
1115 au BufNew,BufAdd,BufWinEnter,BufEnter,BufLeave,BufWinLeave,BufUnload,VimEnter foo* ball
1116 edit foo2
Bram Moolenaarbe9fc5b2022-09-09 17:09:35 +01001117 call writefile(['OK'], 'XerrorsBlast')
Bram Moolenaar797e63b2021-01-15 16:22:52 +01001118 qall
1119 [CODE]
1120
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01001121 call writefile(content, 'XblastBall', 'D')
Bram Moolenaar797e63b2021-01-15 16:22:52 +01001122 call system(GetVimCommand() .. ' --clean -S XblastBall')
Bram Moolenaarae04a602022-09-09 15:08:10 +01001123 sleep 100m
Bram Moolenaarbe9fc5b2022-09-09 17:09:35 +01001124 call assert_match('OK', readfile('XerrorsBlast')->join())
Bram Moolenaar797e63b2021-01-15 16:22:52 +01001125
Bram Moolenaarbe9fc5b2022-09-09 17:09:35 +01001126 call delete('XerrorsBlast')
Bram Moolenaar797e63b2021-01-15 16:22:52 +01001127endfunc
1128
Bram Moolenaar8c752bd2017-03-19 17:09:56 +01001129" SEGV occurs in older versions.
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001130func Test_autocmd_bufwipe_in_SessLoadPost2()
Bram Moolenaar8c752bd2017-03-19 17:09:56 +01001131 tabnew
1132 set noswapfile
Bram Moolenaar8c752bd2017-03-19 17:09:56 +01001133 mksession!
1134
Bram Moolenaarc79745a2019-05-20 22:12:34 +02001135 let content =<< trim [CODE]
1136 set nocp noswapfile
1137 function! DeleteInactiveBufs()
1138 tabfirst
1139 let tabblist = []
1140 for i in range(1, tabpagenr(''$''))
1141 call extend(tabblist, tabpagebuflist(i))
1142 endfor
1143 for b in range(1, bufnr(''$''))
1144 if bufexists(b) && buflisted(b) && (index(tabblist, b) == -1 || bufname(b) =~# ''^$'')
1145 exec ''bwipeout '' . b
1146 endif
1147 endfor
1148 echomsg "SessionLoadPost DONE"
1149 endfunction
1150 au SessionLoadPost * call DeleteInactiveBufs()
1151
1152 func WriteErrors()
Bram Moolenaarbe9fc5b2022-09-09 17:09:35 +01001153 call writefile([execute("messages")], "XerrorsPost")
Bram Moolenaarc79745a2019-05-20 22:12:34 +02001154 endfunc
1155 au VimLeave * call WriteErrors()
1156 [CODE]
1157
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01001158 call writefile(content, 'Xvimrc', 'D')
Bram Moolenaar93344c22019-08-14 21:12:05 +02001159 call system(GetVimCommand('Xvimrc') .. ' --not-a-term --noplugins -S Session.vim -c cq')
Bram Moolenaarae04a602022-09-09 15:08:10 +01001160 sleep 100m
Bram Moolenaarbe9fc5b2022-09-09 17:09:35 +01001161 let errors = join(readfile('XerrorsPost'))
Bram Moolenaare94260f2017-03-21 15:50:12 +01001162 " This probably only ever matches on unix.
1163 call assert_notmatch('Caught deadly signal SEGV', errors)
1164 call assert_match('SessionLoadPost DONE', errors)
Bram Moolenaar8c752bd2017-03-19 17:09:56 +01001165
Bram Moolenaar8c752bd2017-03-19 17:09:56 +01001166 set swapfile
Bram Moolenaarbe9fc5b2022-09-09 17:09:35 +01001167 for file in ['Session.vim', 'XerrorsPost']
Bram Moolenaar8c752bd2017-03-19 17:09:56 +01001168 call delete(file)
1169 endfor
1170endfunc
Bram Moolenaarfaf29d72017-07-09 11:07:16 +02001171
1172func Test_empty_doau()
1173 doau \|
1174endfunc
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001175
1176func s:AutoCommandOptionSet(match)
Bram Moolenaard7c96872019-06-15 17:12:48 +02001177 let template = "Option: <%s>, OldVal: <%s>, OldValLocal: <%s>, OldValGlobal: <%s>, NewVal: <%s>, Scope: <%s>, Command: <%s>\n"
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001178 let item = remove(g:options, 0)
Bram Moolenaard7c96872019-06-15 17:12:48 +02001179 let expected = printf(template, item[0], item[1], item[2], item[3], item[4], item[5], item[6])
1180 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 +02001181 let g:opt = [expected, actual]
1182 "call assert_equal(expected, actual)
1183endfunc
1184
1185func Test_OptionSet()
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +02001186 CheckOption autochdir
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001187
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +02001188 badd test_autocmd.vim
1189
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001190 call test_override('starting', 1)
1191 set nocp
1192 au OptionSet * :call s:AutoCommandOptionSet(expand("<amatch>"))
1193
1194 " 1: Setting number option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001195 let g:options = [['number', 0, 0, 0, 1, 'global', 'set']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001196 set nu
1197 call assert_equal([], g:options)
1198 call assert_equal(g:opt[0], g:opt[1])
1199
1200 " 2: Setting local number option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001201 let g:options = [['number', 1, 1, '', 0, 'local', 'setlocal']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001202 setlocal nonu
1203 call assert_equal([], g:options)
1204 call assert_equal(g:opt[0], g:opt[1])
1205
1206 " 3: Setting global number option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001207 let g:options = [['number', 1, '', 1, 0, 'global', 'setglobal']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001208 setglobal nonu
1209 call assert_equal([], g:options)
1210 call assert_equal(g:opt[0], g:opt[1])
1211
1212 " 4: Setting local autoindent option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001213 let g:options = [['autoindent', 0, 0, '', 1, 'local', 'setlocal']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001214 setlocal ai
1215 call assert_equal([], g:options)
1216 call assert_equal(g:opt[0], g:opt[1])
1217
1218 " 5: Setting global autoindent option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001219 let g:options = [['autoindent', 0, '', 0, 1, 'global', 'setglobal']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001220 setglobal ai
1221 call assert_equal([], g:options)
1222 call assert_equal(g:opt[0], g:opt[1])
1223
1224 " 6: Setting global autoindent option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001225 let g:options = [['autoindent', 1, 1, 1, 0, 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001226 set ai!
1227 call assert_equal([], g:options)
1228 call assert_equal(g:opt[0], g:opt[1])
1229
1230 " 6a: Setting global autoindent option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001231 let g:options = [['autoindent', 1, 1, 0, 0, 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001232 noa setlocal ai
1233 noa setglobal noai
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001234 set ai!
1235 call assert_equal([], g:options)
1236 call assert_equal(g:opt[0], g:opt[1])
1237
1238 " Should not print anything, use :noa
1239 " 7: don't trigger OptionSet"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001240 let g:options = [['invalid', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001241 noa set nonu
Bram Moolenaard7c96872019-06-15 17:12:48 +02001242 call assert_equal([['invalid', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid']], g:options)
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001243 call assert_equal(g:opt[0], g:opt[1])
1244
1245 " 8: Setting several global list and number option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001246 let g:options = [['list', 0, 0, 0, 1, 'global', 'set'], ['number', 0, 0, 0, 1, 'global', 'set']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001247 set list nu
1248 call assert_equal([], g:options)
1249 call assert_equal(g:opt[0], g:opt[1])
1250
1251 " 9: don't trigger OptionSet"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001252 let g:options = [['invalid', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid'], ['invalid', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001253 noa set nolist nonu
Bram Moolenaard7c96872019-06-15 17:12:48 +02001254 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 +02001255 call assert_equal(g:opt[0], g:opt[1])
1256
1257 " 10: Setting global acd"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001258 let g:options = [['autochdir', 0, 0, '', 1, 'local', 'setlocal']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001259 setlocal acd
1260 call assert_equal([], g:options)
1261 call assert_equal(g:opt[0], g:opt[1])
1262
1263 " 11: Setting global autoread (also sets local value)"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001264 let g:options = [['autoread', 0, 0, 0, 1, 'global', 'set']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001265 set ar
1266 call assert_equal([], g:options)
1267 call assert_equal(g:opt[0], g:opt[1])
1268
1269 " 12: Setting local autoread"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001270 let g:options = [['autoread', 1, 1, '', 1, 'local', 'setlocal']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001271 setlocal ar
1272 call assert_equal([], g:options)
1273 call assert_equal(g:opt[0], g:opt[1])
1274
1275 " 13: Setting global autoread"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001276 let g:options = [['autoread', 1, '', 1, 0, 'global', 'setglobal']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001277 setglobal invar
1278 call assert_equal([], g:options)
1279 call assert_equal(g:opt[0], g:opt[1])
1280
1281 " 14: Setting option backspace through :let"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001282 let g:options = [['backspace', '', '', '', 'eol,indent,start', 'global', 'set']]
1283 let &bs = "eol,indent,start"
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001284 call assert_equal([], g:options)
1285 call assert_equal(g:opt[0], g:opt[1])
1286
1287 " 15: Setting option backspace through setbufvar()"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001288 let g:options = [['backup', 0, 0, '', 1, 'local', 'setlocal']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001289 " try twice, first time, shouldn't trigger because option name is invalid,
1290 " second time, it should trigger
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +02001291 let bnum = bufnr('%')
Bram Moolenaare2e40752020-09-04 21:18:46 +02001292 call assert_fails("call setbufvar(bnum, '&l:bk', 1)", 'E355:')
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001293 " should trigger, use correct option name
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +02001294 call setbufvar(bnum, '&backup', 1)
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001295 call assert_equal([], g:options)
1296 call assert_equal(g:opt[0], g:opt[1])
1297
1298 " 16: Setting number option using setwinvar"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001299 let g:options = [['number', 0, 0, '', 1, 'local', 'setlocal']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001300 call setwinvar(0, '&number', 1)
1301 call assert_equal([], g:options)
1302 call assert_equal(g:opt[0], g:opt[1])
1303
1304 " 17: Setting key option, shouldn't trigger"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001305 let g:options = [['key', 'invalid', 'invalid1', 'invalid2', 'invalid3', 'invalid4', 'invalid5']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001306 setlocal key=blah
1307 setlocal key=
Bram Moolenaard7c96872019-06-15 17:12:48 +02001308 call assert_equal([['key', 'invalid', 'invalid1', 'invalid2', 'invalid3', 'invalid4', 'invalid5']], g:options)
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001309 call assert_equal(g:opt[0], g:opt[1])
1310
Bram Moolenaard7c96872019-06-15 17:12:48 +02001311
1312 " 18a: Setting string global option"
1313 let oldval = &backupext
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001314 let g:options = [['backupext', oldval, oldval, oldval, 'foo', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001315 set backupext=foo
1316 call assert_equal([], g:options)
1317 call assert_equal(g:opt[0], g:opt[1])
1318
1319 " 18b: Resetting string global option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001320 let g:options = [['backupext', 'foo', 'foo', 'foo', oldval, 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001321 set backupext&
1322 call assert_equal([], g:options)
1323 call assert_equal(g:opt[0], g:opt[1])
1324
1325 " 18c: Setting global string global option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001326 let g:options = [['backupext', oldval, '', oldval, 'bar', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001327 setglobal backupext=bar
1328 call assert_equal([], g:options)
1329 call assert_equal(g:opt[0], g:opt[1])
1330
1331 " 18d: Setting local string global option"
1332 " As this is a global option this sets the global value even though
1333 " :setlocal is used!
1334 noa set backupext& " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001335 let g:options = [['backupext', oldval, oldval, '', 'baz', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001336 setlocal backupext=baz
1337 call assert_equal([], g:options)
1338 call assert_equal(g:opt[0], g:opt[1])
1339
1340 " 18e: Setting again string global option"
1341 noa setglobal backupext=ext_global " Reset global and local value (without triggering autocmd)
1342 noa setlocal backupext=ext_local " Sets the global(!) value!
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001343 let g:options = [['backupext', 'ext_local', 'ext_local', 'ext_local', 'fuu', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001344 set backupext=fuu
1345 call assert_equal([], g:options)
1346 call assert_equal(g:opt[0], g:opt[1])
1347
1348
zeertzjqb811de52021-10-21 10:50:44 +01001349 " 19a: Setting string global-local (to buffer) option"
Bram Moolenaar8efa0262017-08-20 15:47:20 +02001350 let oldval = &tags
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001351 let g:options = [['tags', oldval, oldval, oldval, 'tagpath', 'global', 'set']]
Bram Moolenaar8efa0262017-08-20 15:47:20 +02001352 set tags=tagpath
1353 call assert_equal([], g:options)
1354 call assert_equal(g:opt[0], g:opt[1])
1355
zeertzjqb811de52021-10-21 10:50:44 +01001356 " 19b: Resetting string global-local (to buffer) option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001357 let g:options = [['tags', 'tagpath', 'tagpath', 'tagpath', oldval, 'global', 'set']]
Bram Moolenaar8efa0262017-08-20 15:47:20 +02001358 set tags&
1359 call assert_equal([], g:options)
1360 call assert_equal(g:opt[0], g:opt[1])
1361
zeertzjqb811de52021-10-21 10:50:44 +01001362 " 19c: Setting global string global-local (to buffer) option "
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001363 let g:options = [['tags', oldval, '', oldval, 'tagpath1', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001364 setglobal tags=tagpath1
1365 call assert_equal([], g:options)
1366 call assert_equal(g:opt[0], g:opt[1])
1367
zeertzjqb811de52021-10-21 10:50:44 +01001368 " 19d: Setting local string global-local (to buffer) option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001369 let g:options = [['tags', 'tagpath1', 'tagpath1', '', 'tagpath2', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001370 setlocal tags=tagpath2
1371 call assert_equal([], g:options)
1372 call assert_equal(g:opt[0], g:opt[1])
1373
zeertzjqb811de52021-10-21 10:50:44 +01001374 " 19e: Setting again string global-local (to buffer) option"
1375 " Note: v:option_old is the old global value for global-local string options
Bram Moolenaard7c96872019-06-15 17:12:48 +02001376 " but the old local value for all other kinds of options.
1377 noa setglobal tags=tag_global " Reset global and local value (without triggering autocmd)
1378 noa setlocal tags=tag_local
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001379 let g:options = [['tags', 'tag_global', 'tag_local', 'tag_global', 'tagpath', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001380 set tags=tagpath
1381 call assert_equal([], g:options)
1382 call assert_equal(g:opt[0], g:opt[1])
1383
zeertzjqb811de52021-10-21 10:50:44 +01001384 " 19f: Setting string global-local (to buffer) option to an empty string"
1385 " Note: v:option_old is the old global value for global-local string options
Bram Moolenaard7c96872019-06-15 17:12:48 +02001386 " but the old local value for all other kinds of options.
1387 noa set tags=tag_global " Reset global and local value (without triggering autocmd)
1388 noa setlocal tags= " empty string
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001389 let g:options = [['tags', 'tag_global', '', 'tag_global', 'tagpath', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001390 set tags=tagpath
1391 call assert_equal([], g:options)
1392 call assert_equal(g:opt[0], g:opt[1])
1393
1394
1395 " 20a: Setting string local (to buffer) option"
1396 let oldval = &spelllang
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001397 let g:options = [['spelllang', oldval, oldval, oldval, 'elvish,klingon', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001398 set spelllang=elvish,klingon
1399 call assert_equal([], g:options)
1400 call assert_equal(g:opt[0], g:opt[1])
1401
1402 " 20b: Resetting string local (to buffer) option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001403 let g:options = [['spelllang', 'elvish,klingon', 'elvish,klingon', 'elvish,klingon', oldval, 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001404 set spelllang&
1405 call assert_equal([], g:options)
1406 call assert_equal(g:opt[0], g:opt[1])
1407
1408 " 20c: Setting global string local (to buffer) option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001409 let g:options = [['spelllang', oldval, '', oldval, 'elvish', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001410 setglobal spelllang=elvish
1411 call assert_equal([], g:options)
1412 call assert_equal(g:opt[0], g:opt[1])
1413
1414 " 20d: Setting local string local (to buffer) option"
1415 noa set spelllang& " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001416 let g:options = [['spelllang', oldval, oldval, '', 'klingon', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001417 setlocal spelllang=klingon
1418 call assert_equal([], g:options)
1419 call assert_equal(g:opt[0], g:opt[1])
1420
1421 " 20e: Setting again string local (to buffer) option"
zeertzjqb811de52021-10-21 10:50:44 +01001422 " Note: v:option_old is the old global value for global-local string options
Bram Moolenaard7c96872019-06-15 17:12:48 +02001423 " but the old local value for all other kinds of options.
1424 noa setglobal spelllang=spellglobal " Reset global and local value (without triggering autocmd)
1425 noa setlocal spelllang=spelllocal
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001426 let g:options = [['spelllang', 'spelllocal', 'spelllocal', 'spellglobal', 'foo', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001427 set spelllang=foo
1428 call assert_equal([], g:options)
1429 call assert_equal(g:opt[0], g:opt[1])
1430
1431
zeertzjqb811de52021-10-21 10:50:44 +01001432 " 21a: Setting string global-local (to window) option"
Bram Moolenaard7c96872019-06-15 17:12:48 +02001433 let oldval = &statusline
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001434 let g:options = [['statusline', oldval, oldval, oldval, 'foo', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001435 set statusline=foo
1436 call assert_equal([], g:options)
1437 call assert_equal(g:opt[0], g:opt[1])
1438
zeertzjqb811de52021-10-21 10:50:44 +01001439 " 21b: Resetting 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.
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001442 let g:options = [['statusline', 'foo', 'foo', 'foo', oldval, 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001443 set statusline&
1444 call assert_equal([], g:options)
1445 call assert_equal(g:opt[0], g:opt[1])
1446
zeertzjqb811de52021-10-21 10:50:44 +01001447 " 21c: Setting global string global-local (to window) option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001448 let g:options = [['statusline', oldval, '', oldval, 'bar', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001449 setglobal statusline=bar
1450 call assert_equal([], g:options)
1451 call assert_equal(g:opt[0], g:opt[1])
1452
zeertzjqb811de52021-10-21 10:50:44 +01001453 " 21d: Setting local string global-local (to window) option"
Bram Moolenaard7c96872019-06-15 17:12:48 +02001454 noa set statusline& " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001455 let g:options = [['statusline', oldval, oldval, '', 'baz', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001456 setlocal statusline=baz
1457 call assert_equal([], g:options)
1458 call assert_equal(g:opt[0], g:opt[1])
1459
zeertzjqb811de52021-10-21 10:50:44 +01001460 " 21e: Setting again string global-local (to window) option"
1461 " Note: v:option_old is the old global value for global-local string options
Bram Moolenaard7c96872019-06-15 17:12:48 +02001462 " but the old local value for all other kinds of options.
1463 noa setglobal statusline=bar " Reset global and local value (without triggering autocmd)
1464 noa setlocal statusline=baz
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001465 let g:options = [['statusline', 'bar', 'baz', 'bar', 'foo', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001466 set statusline=foo
1467 call assert_equal([], g:options)
1468 call assert_equal(g:opt[0], g:opt[1])
1469
1470
1471 " 22a: Setting string local (to window) option"
1472 let oldval = &foldignore
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001473 let g:options = [['foldignore', oldval, oldval, oldval, 'fo', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001474 set foldignore=fo
1475 call assert_equal([], g:options)
1476 call assert_equal(g:opt[0], g:opt[1])
1477
1478 " 22b: Resetting string local (to window) option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001479 let g:options = [['foldignore', 'fo', 'fo', 'fo', oldval, 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001480 set foldignore&
1481 call assert_equal([], g:options)
1482 call assert_equal(g:opt[0], g:opt[1])
1483
1484 " 22c: Setting global string local (to window) option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001485 let g:options = [['foldignore', oldval, '', oldval, 'bar', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001486 setglobal foldignore=bar
1487 call assert_equal([], g:options)
1488 call assert_equal(g:opt[0], g:opt[1])
1489
1490 " 22d: Setting local string local (to window) option"
1491 noa set foldignore& " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001492 let g:options = [['foldignore', oldval, oldval, '', 'baz', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001493 setlocal foldignore=baz
1494 call assert_equal([], g:options)
1495 call assert_equal(g:opt[0], g:opt[1])
1496
1497 " 22e: Setting again string local (to window) option"
1498 noa setglobal foldignore=glob " Reset global and local value (without triggering autocmd)
1499 noa setlocal foldignore=loc
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001500 let g:options = [['foldignore', 'loc', 'loc', 'glob', 'fo', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001501 set foldignore=fo
1502 call assert_equal([], g:options)
1503 call assert_equal(g:opt[0], g:opt[1])
1504
1505
zeertzjqb811de52021-10-21 10:50:44 +01001506 " 23a: Setting global number global option"
Bram Moolenaard7c96872019-06-15 17:12:48 +02001507 noa setglobal cmdheight=8 " Reset global and local value (without triggering autocmd)
1508 noa setlocal cmdheight=1 " Sets the global(!) value!
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001509 let g:options = [['cmdheight', '1', '', '1', '2', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001510 setglobal cmdheight=2
1511 call assert_equal([], g:options)
1512 call assert_equal(g:opt[0], g:opt[1])
1513
1514 " 23b: Setting local number global option"
1515 noa setglobal cmdheight=8 " Reset global and local value (without triggering autocmd)
1516 noa setlocal cmdheight=1 " Sets the global(!) value!
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001517 let g:options = [['cmdheight', '1', '1', '', '2', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001518 setlocal cmdheight=2
1519 call assert_equal([], g:options)
1520 call assert_equal(g:opt[0], g:opt[1])
1521
1522 " 23c: Setting again number global option"
1523 noa setglobal cmdheight=8 " Reset global and local value (without triggering autocmd)
1524 noa setlocal cmdheight=1 " Sets the global(!) value!
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001525 let g:options = [['cmdheight', '1', '1', '1', '2', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001526 set cmdheight=2
1527 call assert_equal([], g:options)
1528 call assert_equal(g:opt[0], g:opt[1])
1529
1530 " 23d: Setting again number global option"
1531 noa set cmdheight=8 " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001532 let g:options = [['cmdheight', '8', '8', '8', '2', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001533 set cmdheight=2
1534 call assert_equal([], g:options)
1535 call assert_equal(g:opt[0], g:opt[1])
1536
1537
1538 " 24a: Setting global number global-local (to buffer) option"
1539 noa setglobal undolevels=8 " Reset global and local value (without triggering autocmd)
1540 noa setlocal undolevels=1
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001541 let g:options = [['undolevels', '8', '', '8', '2', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001542 setglobal undolevels=2
1543 call assert_equal([], g:options)
1544 call assert_equal(g:opt[0], g:opt[1])
1545
1546 " 24b: Setting local number global-local (to buffer) option"
1547 noa setglobal undolevels=8 " Reset global and local value (without triggering autocmd)
1548 noa setlocal undolevels=1
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001549 let g:options = [['undolevels', '1', '1', '', '2', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001550 setlocal undolevels=2
1551 call assert_equal([], g:options)
1552 call assert_equal(g:opt[0], g:opt[1])
1553
1554 " 24c: Setting again number global-local (to buffer) option"
1555 noa setglobal undolevels=8 " Reset global and local value (without triggering autocmd)
1556 noa setlocal undolevels=1
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001557 let g:options = [['undolevels', '1', '1', '8', '2', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001558 set undolevels=2
1559 call assert_equal([], g:options)
1560 call assert_equal(g:opt[0], g:opt[1])
1561
1562 " 24d: Setting again global number global-local (to buffer) option"
1563 noa set undolevels=8 " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001564 let g:options = [['undolevels', '8', '8', '8', '2', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001565 set undolevels=2
1566 call assert_equal([], g:options)
1567 call assert_equal(g:opt[0], g:opt[1])
1568
1569
1570 " 25a: Setting global number local (to buffer) option"
1571 noa setglobal wrapmargin=8 " Reset global and local value (without triggering autocmd)
1572 noa setlocal wrapmargin=1
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001573 let g:options = [['wrapmargin', '8', '', '8', '2', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001574 setglobal wrapmargin=2
1575 call assert_equal([], g:options)
1576 call assert_equal(g:opt[0], g:opt[1])
1577
1578 " 25b: Setting local number local (to buffer) option"
1579 noa setglobal wrapmargin=8 " Reset global and local value (without triggering autocmd)
1580 noa setlocal wrapmargin=1
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001581 let g:options = [['wrapmargin', '1', '1', '', '2', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001582 setlocal wrapmargin=2
1583 call assert_equal([], g:options)
1584 call assert_equal(g:opt[0], g:opt[1])
1585
1586 " 25c: Setting again number local (to buffer) option"
1587 noa setglobal wrapmargin=8 " Reset global and local value (without triggering autocmd)
1588 noa setlocal wrapmargin=1
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001589 let g:options = [['wrapmargin', '1', '1', '8', '2', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001590 set wrapmargin=2
1591 call assert_equal([], g:options)
1592 call assert_equal(g:opt[0], g:opt[1])
1593
1594 " 25d: Setting again global number local (to buffer) option"
1595 noa set wrapmargin=8 " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001596 let g:options = [['wrapmargin', '8', '8', '8', '2', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001597 set wrapmargin=2
1598 call assert_equal([], g:options)
1599 call assert_equal(g:opt[0], g:opt[1])
1600
1601
1602 " 26: Setting number global-local (to window) option.
1603 " Such option does currently not exist.
1604
1605
1606 " 27a: Setting global number local (to window) option"
1607 noa setglobal foldcolumn=8 " Reset global and local value (without triggering autocmd)
1608 noa setlocal foldcolumn=1
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001609 let g:options = [['foldcolumn', '8', '', '8', '2', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001610 setglobal foldcolumn=2
1611 call assert_equal([], g:options)
1612 call assert_equal(g:opt[0], g:opt[1])
1613
1614 " 27b: Setting local number local (to window) option"
1615 noa setglobal foldcolumn=8 " Reset global and local value (without triggering autocmd)
1616 noa setlocal foldcolumn=1
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001617 let g:options = [['foldcolumn', '1', '1', '', '2', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001618 setlocal foldcolumn=2
1619 call assert_equal([], g:options)
1620 call assert_equal(g:opt[0], g:opt[1])
1621
1622 " 27c: Setting again number local (to window) option"
1623 noa setglobal foldcolumn=8 " Reset global and local value (without triggering autocmd)
1624 noa setlocal foldcolumn=1
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001625 let g:options = [['foldcolumn', '1', '1', '8', '2', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001626 set foldcolumn=2
1627 call assert_equal([], g:options)
1628 call assert_equal(g:opt[0], g:opt[1])
1629
zeertzjqb811de52021-10-21 10:50:44 +01001630 " 27d: Setting again global number local (to window) option"
Bram Moolenaard7c96872019-06-15 17:12:48 +02001631 noa set foldcolumn=8 " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001632 let g:options = [['foldcolumn', '8', '8', '8', '2', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001633 set foldcolumn=2
1634 call assert_equal([], g:options)
1635 call assert_equal(g:opt[0], g:opt[1])
1636
1637
1638 " 28a: Setting global boolean global option"
1639 noa setglobal nowrapscan " Reset global and local value (without triggering autocmd)
1640 noa setlocal wrapscan " Sets the global(!) value!
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001641 let g:options = [['wrapscan', '1', '', '1', '0', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001642 setglobal nowrapscan
1643 call assert_equal([], g:options)
1644 call assert_equal(g:opt[0], g:opt[1])
1645
1646 " 28b: Setting local boolean global option"
1647 noa setglobal nowrapscan " Reset global and local value (without triggering autocmd)
1648 noa setlocal wrapscan " Sets the global(!) value!
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001649 let g:options = [['wrapscan', '1', '1', '', '0', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001650 setlocal nowrapscan
1651 call assert_equal([], g:options)
1652 call assert_equal(g:opt[0], g:opt[1])
1653
1654 " 28c: Setting again boolean global option"
1655 noa setglobal nowrapscan " Reset global and local value (without triggering autocmd)
1656 noa setlocal wrapscan " Sets the global(!) value!
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001657 let g:options = [['wrapscan', '1', '1', '1', '0', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001658 set nowrapscan
1659 call assert_equal([], g:options)
1660 call assert_equal(g:opt[0], g:opt[1])
1661
1662 " 28d: Setting again global boolean global option"
1663 noa set nowrapscan " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001664 let g:options = [['wrapscan', '0', '0', '0', '1', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001665 set wrapscan
1666 call assert_equal([], g:options)
1667 call assert_equal(g:opt[0], g:opt[1])
1668
1669
1670 " 29a: Setting global boolean global-local (to buffer) option"
1671 noa setglobal noautoread " Reset global and local value (without triggering autocmd)
1672 noa setlocal autoread
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001673 let g:options = [['autoread', '0', '', '0', '1', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001674 setglobal autoread
1675 call assert_equal([], g:options)
1676 call assert_equal(g:opt[0], g:opt[1])
1677
1678 " 29b: Setting local boolean global-local (to buffer) option"
1679 noa setglobal noautoread " Reset global and local value (without triggering autocmd)
1680 noa setlocal autoread
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001681 let g:options = [['autoread', '1', '1', '', '0', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001682 setlocal noautoread
1683 call assert_equal([], g:options)
1684 call assert_equal(g:opt[0], g:opt[1])
1685
1686 " 29c: Setting again boolean global-local (to buffer) option"
1687 noa setglobal noautoread " Reset global and local value (without triggering autocmd)
1688 noa setlocal autoread
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001689 let g:options = [['autoread', '1', '1', '0', '1', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001690 set autoread
1691 call assert_equal([], g:options)
1692 call assert_equal(g:opt[0], g:opt[1])
1693
1694 " 29d: Setting again global boolean global-local (to buffer) option"
1695 noa set noautoread " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001696 let g:options = [['autoread', '0', '0', '0', '1', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001697 set autoread
1698 call assert_equal([], g:options)
1699 call assert_equal(g:opt[0], g:opt[1])
1700
1701
1702 " 30a: Setting global boolean local (to buffer) option"
1703 noa setglobal nocindent " Reset global and local value (without triggering autocmd)
1704 noa setlocal cindent
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001705 let g:options = [['cindent', '0', '', '0', '1', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001706 setglobal cindent
1707 call assert_equal([], g:options)
1708 call assert_equal(g:opt[0], g:opt[1])
1709
1710 " 30b: Setting local boolean local (to buffer) option"
1711 noa setglobal nocindent " Reset global and local value (without triggering autocmd)
1712 noa setlocal cindent
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001713 let g:options = [['cindent', '1', '1', '', '0', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001714 setlocal nocindent
1715 call assert_equal([], g:options)
1716 call assert_equal(g:opt[0], g:opt[1])
1717
1718 " 30c: Setting again boolean local (to buffer) option"
1719 noa setglobal nocindent " Reset global and local value (without triggering autocmd)
1720 noa setlocal cindent
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001721 let g:options = [['cindent', '1', '1', '0', '1', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001722 set cindent
1723 call assert_equal([], g:options)
1724 call assert_equal(g:opt[0], g:opt[1])
1725
1726 " 30d: Setting again global boolean local (to buffer) option"
1727 noa set nocindent " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001728 let g:options = [['cindent', '0', '0', '0', '1', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001729 set cindent
1730 call assert_equal([], g:options)
1731 call assert_equal(g:opt[0], g:opt[1])
1732
1733
1734 " 31: Setting boolean global-local (to window) option
1735 " Currently no such option exists.
1736
1737
1738 " 32a: Setting global boolean local (to window) option"
1739 noa setglobal nocursorcolumn " Reset global and local value (without triggering autocmd)
1740 noa setlocal cursorcolumn
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001741 let g:options = [['cursorcolumn', '0', '', '0', '1', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001742 setglobal cursorcolumn
1743 call assert_equal([], g:options)
1744 call assert_equal(g:opt[0], g:opt[1])
1745
1746 " 32b: Setting local boolean local (to window) option"
1747 noa setglobal nocursorcolumn " Reset global and local value (without triggering autocmd)
1748 noa setlocal cursorcolumn
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001749 let g:options = [['cursorcolumn', '1', '1', '', '0', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001750 setlocal nocursorcolumn
1751 call assert_equal([], g:options)
1752 call assert_equal(g:opt[0], g:opt[1])
1753
1754 " 32c: Setting again boolean local (to window) option"
1755 noa setglobal nocursorcolumn " Reset global and local value (without triggering autocmd)
1756 noa setlocal cursorcolumn
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001757 let g:options = [['cursorcolumn', '1', '1', '0', '1', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001758 set cursorcolumn
1759 call assert_equal([], g:options)
1760 call assert_equal(g:opt[0], g:opt[1])
1761
1762 " 32d: Setting again global boolean local (to window) option"
1763 noa set nocursorcolumn " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001764 let g:options = [['cursorcolumn', '0', '0', '0', '1', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001765 set cursorcolumn
1766 call assert_equal([], g:options)
1767 call assert_equal(g:opt[0], g:opt[1])
1768
1769
Bram Moolenaar1bc353b2019-09-01 14:45:28 +02001770 " 33: Test autocommands when an option value is converted internally.
Bram Moolenaard7c96872019-06-15 17:12:48 +02001771 noa set backspace=1 " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001772 let g:options = [['backspace', 'indent,eol', 'indent,eol', 'indent,eol', '2', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001773 set backspace=2
1774 call assert_equal([], g:options)
1775 call assert_equal(g:opt[0], g:opt[1])
1776
1777
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001778 " Cleanup
1779 au! OptionSet
Bram Moolenaar0331faf2019-06-15 18:40:37 +02001780 " set tags&
Bram Moolenaard7c96872019-06-15 17:12:48 +02001781 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 +02001782 exe printf(":set %s&vim", opt)
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001783 endfor
1784 call test_override('starting', 0)
1785 delfunc! AutoCommandOptionSet
1786endfunc
1787
1788func Test_OptionSet_diffmode()
1789 call test_override('starting', 1)
Bram Moolenaar26d98212019-01-27 22:32:55 +01001790 " 18: Changing an option when entering diff mode
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001791 new
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001792 au OptionSet diff :let &l:cul = v:option_new
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001793
1794 call setline(1, ['buffer 1', 'line2', 'line3', 'line4'])
1795 call assert_equal(0, &l:cul)
1796 diffthis
1797 call assert_equal(1, &l:cul)
1798
1799 vnew
1800 call setline(1, ['buffer 2', 'line 2', 'line 3', 'line4'])
1801 call assert_equal(0, &l:cul)
1802 diffthis
1803 call assert_equal(1, &l:cul)
1804
1805 diffoff
1806 call assert_equal(0, &l:cul)
1807 call assert_equal(1, getwinvar(2, '&l:cul'))
1808 bw!
1809
1810 call assert_equal(1, &l:cul)
1811 diffoff!
1812 call assert_equal(0, &l:cul)
1813 call assert_equal(0, getwinvar(1, '&l:cul'))
1814 bw!
1815
1816 " Cleanup
1817 au! OptionSet
1818 call test_override('starting', 0)
1819endfunc
1820
1821func Test_OptionSet_diffmode_close()
1822 call test_override('starting', 1)
1823 " 19: Try to close the current window when entering diff mode
1824 " should not segfault
1825 new
1826 au OptionSet diff close
1827
1828 call setline(1, ['buffer 1', 'line2', 'line3', 'line4'])
Bram Moolenaare2e40752020-09-04 21:18:46 +02001829 call assert_fails(':diffthis', 'E788:')
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001830 call assert_equal(1, &diff)
1831 vnew
1832 call setline(1, ['buffer 2', 'line 2', 'line 3', 'line4'])
Bram Moolenaare2e40752020-09-04 21:18:46 +02001833 call assert_fails(':diffthis', 'E788:')
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001834 call assert_equal(1, &diff)
Bram Moolenaara9aa86f2019-11-10 21:25:45 +01001835 set diffopt-=closeoff
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001836 bw!
Bram Moolenaare2e40752020-09-04 21:18:46 +02001837 call assert_fails(':diffoff!', 'E788:')
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001838 bw!
1839
1840 " Cleanup
1841 au! OptionSet
1842 call test_override('starting', 0)
1843 "delfunc! AutoCommandOptionSet
1844endfunc
Bram Moolenaar4a137b42017-08-04 22:37:11 +02001845
1846" Test for Bufleave autocommand that deletes the buffer we are about to edit.
1847func Test_BufleaveWithDelete()
Bram Moolenaare7cda972022-08-29 11:02:59 +01001848 new | edit XbufLeave1
Bram Moolenaar4a137b42017-08-04 22:37:11 +02001849
1850 augroup test_bufleavewithdelete
1851 autocmd!
Bram Moolenaare7cda972022-08-29 11:02:59 +01001852 autocmd BufLeave XbufLeave1 bwipe XbufLeave2
Bram Moolenaar4a137b42017-08-04 22:37:11 +02001853 augroup END
1854
Bram Moolenaare7cda972022-08-29 11:02:59 +01001855 call assert_fails('edit XbufLeave2', 'E143:')
1856 call assert_equal('XbufLeave1', bufname('%'))
Bram Moolenaar4a137b42017-08-04 22:37:11 +02001857
Bram Moolenaare7cda972022-08-29 11:02:59 +01001858 autocmd! test_bufleavewithdelete BufLeave XbufLeave1
Bram Moolenaar4a137b42017-08-04 22:37:11 +02001859 augroup! test_bufleavewithdelete
1860
1861 new
Bram Moolenaare7cda972022-08-29 11:02:59 +01001862 bwipe! XbufLeave1
Bram Moolenaar4a137b42017-08-04 22:37:11 +02001863endfunc
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +02001864
1865" Test for autocommand that changes the buffer list, when doing ":ball".
1866func Test_Acmd_BufAll()
1867 enew!
1868 %bwipe!
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01001869 call writefile(['Test file Xxx1'], 'Xxx1', 'D')
1870 call writefile(['Test file Xxx2'], 'Xxx2', 'D')
1871 call writefile(['Test file Xxx3'], 'Xxx3', 'D')
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +02001872
1873 " Add three files to the buffer list
1874 split Xxx1
1875 close
1876 split Xxx2
1877 close
1878 split Xxx3
1879 close
1880
1881 " Wipe the buffer when the buffer is opened
1882 au BufReadPost Xxx2 bwipe
1883
1884 call append(0, 'Test file Xxx4')
1885 ball
1886
1887 call assert_equal(2, winnr('$'))
1888 call assert_equal('Xxx1', bufname(winbufnr(winnr('$'))))
1889 wincmd t
1890
1891 au! BufReadPost
1892 %bwipe!
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +02001893 enew! | only
1894endfunc
1895
1896" Test for autocommand that changes current buffer on BufEnter event.
1897" Check if modelines are interpreted for the correct buffer.
1898func Test_Acmd_BufEnter()
1899 %bwipe!
1900 call writefile(['start of test file Xxx1',
1901 \ "\<Tab>this is a test",
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01001902 \ 'end of test file Xxx1'], 'Xxx1', 'D')
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +02001903 call writefile(['start of test file Xxx2',
1904 \ 'vim: set noai :',
1905 \ "\<Tab>this is a test",
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01001906 \ 'end of test file Xxx2'], 'Xxx2', 'D')
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +02001907
1908 au BufEnter Xxx2 brew
1909 set ai modeline modelines=3
1910 edit Xxx1
1911 " edit Xxx2, autocmd will do :brew
1912 edit Xxx2
1913 exe "normal G?this is a\<CR>"
1914 " Append text with autoindent to this file
1915 normal othis should be auto-indented
1916 call assert_equal("\<Tab>this should be auto-indented", getline('.'))
1917 call assert_equal(3, line('.'))
1918 " Remove autocmd and edit Xxx2 again
1919 au! BufEnter Xxx2
1920 buf! Xxx2
1921 exe "normal G?this is a\<CR>"
1922 " append text without autoindent to Xxx
1923 normal othis should be in column 1
1924 call assert_equal("this should be in column 1", getline('.'))
1925 call assert_equal(4, line('.'))
1926
1927 %bwipe!
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +02001928 set ai&vim modeline&vim modelines&vim
1929endfunc
1930
1931" Test for issue #57
1932" do not move cursor on <c-o> when autoindent is set
1933func Test_ai_CTRL_O()
1934 enew!
1935 set ai
1936 let save_fo = &fo
1937 set fo+=r
1938 exe "normal o# abcdef\<Esc>2hi\<CR>\<C-O>d0\<Esc>"
1939 exe "normal o# abcdef\<Esc>2hi\<C-O>d0\<Esc>"
1940 call assert_equal(['# abc', 'def', 'def'], getline(2, 4))
1941
1942 set ai&vim
1943 let &fo = save_fo
1944 enew!
1945endfunc
1946
1947" Test for autocommand that deletes the current buffer on BufLeave event.
1948" Also test deleting the last buffer, should give a new, empty buffer.
1949func Test_BufLeave_Wipe()
1950 %bwipe!
1951 let content = ['start of test file Xxx',
1952 \ 'this is a test',
1953 \ 'end of test file Xxx']
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01001954 call writefile(content, 'Xxx1', 'D')
1955 call writefile(content, 'Xxx2', 'D')
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +02001956
1957 au BufLeave Xxx2 bwipe
1958 edit Xxx1
1959 split Xxx2
1960 " delete buffer Xxx2, we should be back to Xxx1
1961 bwipe
1962 call assert_equal('Xxx1', bufname('%'))
1963 call assert_equal(1, winnr('$'))
1964
1965 " Create an alternate buffer
1966 %write! test.out
1967 call assert_equal('test.out', bufname('#'))
1968 " delete alternate buffer
1969 bwipe test.out
1970 call assert_equal('Xxx1', bufname('%'))
1971 call assert_equal('', bufname('#'))
1972
1973 au BufLeave Xxx1 bwipe
1974 " delete current buffer, get an empty one
1975 bwipe!
1976 call assert_equal(1, line('$'))
1977 call assert_equal('', bufname('%'))
Bram Moolenaarb2c87502017-10-14 21:15:58 +02001978 let g:bufinfo = getbufinfo()
1979 call assert_equal(1, len(g:bufinfo))
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +02001980
Bram Moolenaar53f0c962017-10-22 14:23:59 +02001981 call delete('test.out')
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +02001982 %bwipe
1983 au! BufLeave
Bram Moolenaarb2c87502017-10-14 21:15:58 +02001984
1985 " check that bufinfo doesn't contain a pointer to freed memory
1986 call test_garbagecollect_now()
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +02001987endfunc
Bram Moolenaar87ffb5c2017-10-19 12:37:42 +02001988
1989func Test_QuitPre()
1990 edit Xfoo
1991 let winid = win_getid(winnr())
1992 split Xbar
1993 au! QuitPre * let g:afile = expand('<afile>')
1994 " Close the other window, <afile> should be correct.
1995 exe win_id2win(winid) . 'q'
1996 call assert_equal('Xfoo', g:afile)
LemonBoy66e13ae2022-04-21 22:52:11 +01001997
Bram Moolenaar87ffb5c2017-10-19 12:37:42 +02001998 unlet g:afile
1999 bwipe Xfoo
2000 bwipe Xbar
2001endfunc
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02002002
2003func Test_Cmdline()
Bram Moolenaar153b7042018-01-31 15:48:32 +01002004 au! CmdlineChanged : let g:text = getcmdline()
2005 let g:text = 0
2006 call feedkeys(":echom 'hello'\<CR>", 'xt')
2007 call assert_equal("echom 'hello'", g:text)
2008 au! CmdlineChanged
2009
2010 au! CmdlineChanged : let g:entered = expand('<afile>')
2011 let g:entered = 0
2012 call feedkeys(":echom 'hello'\<CR>", 'xt')
2013 call assert_equal(':', g:entered)
2014 au! CmdlineChanged
2015
zeertzjq412e0e42023-02-11 10:34:07 +00002016 autocmd CmdlineChanged : let g:log += [getcmdline()]
2017
Bram Moolenaarbb393d82022-12-09 12:21:50 +00002018 let g:log = []
2019 cnoremap <F1> <Cmd>call setcmdline('ls')<CR>
Bram Moolenaarbb393d82022-12-09 12:21:50 +00002020 call feedkeys(":\<F1>", 'xt')
2021 call assert_equal(['ls'], g:log)
Bram Moolenaarbb393d82022-12-09 12:21:50 +00002022 cunmap <F1>
2023
zeertzjqaf9e28a2023-02-06 20:58:09 +00002024 let g:log = []
zeertzjqaf9e28a2023-02-06 20:58:09 +00002025 call feedkeys(":sign \<Tab>\<Tab>\<C-N>\<C-P>\<S-Tab>\<S-Tab>\<Esc>", 'xt')
2026 call assert_equal([
2027 \ 's',
2028 \ 'si',
2029 \ 'sig',
2030 \ 'sign',
2031 \ 'sign ',
2032 \ 'sign define',
2033 \ 'sign jump',
2034 \ 'sign list',
2035 \ 'sign jump',
2036 \ 'sign define',
2037 \ 'sign ',
2038 \ ], g:log)
2039 let g:log = []
2040 set wildmenu wildoptions+=pum
2041 call feedkeys(":sign \<S-Tab>\<PageUp>\<kPageUp>\<kPageDown>\<PageDown>\<Esc>", 'xt')
2042 call assert_equal([
2043 \ 's',
2044 \ 'si',
2045 \ 'sig',
2046 \ 'sign',
2047 \ 'sign ',
2048 \ 'sign unplace',
2049 \ 'sign jump',
2050 \ 'sign define',
2051 \ 'sign undefine',
2052 \ 'sign unplace',
2053 \ ], g:log)
2054 set wildmenu& wildoptions&
zeertzjq412e0e42023-02-11 10:34:07 +00002055
2056 let g:log = []
2057 let @r = 'abc'
2058 call feedkeys(":0\<C-R>r1\<C-R>\<C-O>r2\<C-R>\<C-R>r3\<Esc>", 'xt')
2059 call assert_equal([
2060 \ '0',
2061 \ '0a',
2062 \ '0ab',
2063 \ '0abc',
2064 \ '0abc1',
2065 \ '0abc1abc',
2066 \ '0abc1abc2',
2067 \ '0abc1abc2abc',
2068 \ '0abc1abc2abc3',
2069 \ ], g:log)
2070
zeertzjqaf9e28a2023-02-06 20:58:09 +00002071 unlet g:log
2072 au! CmdlineChanged
2073
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02002074 au! CmdlineEnter : let g:entered = expand('<afile>')
2075 au! CmdlineLeave : let g:left = expand('<afile>')
2076 let g:entered = 0
2077 let g:left = 0
2078 call feedkeys(":echo 'hello'\<CR>", 'xt')
2079 call assert_equal(':', g:entered)
2080 call assert_equal(':', g:left)
2081 au! CmdlineEnter
2082 au! CmdlineLeave
2083
Bram Moolenaara4baf5b2018-04-22 13:27:44 +02002084 let save_shellslash = &shellslash
2085 set noshellslash
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02002086 au! CmdlineEnter / let g:entered = expand('<afile>')
2087 au! CmdlineLeave / let g:left = expand('<afile>')
2088 let g:entered = 0
2089 let g:left = 0
Bram Moolenaar53f0c962017-10-22 14:23:59 +02002090 new
2091 call setline(1, 'hello')
2092 call feedkeys("/hello\<CR>", 'xt')
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02002093 call assert_equal('/', g:entered)
2094 call assert_equal('/', g:left)
Bram Moolenaar53f0c962017-10-22 14:23:59 +02002095 bwipe!
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02002096 au! CmdlineEnter
2097 au! CmdlineLeave
Bram Moolenaara4baf5b2018-04-22 13:27:44 +02002098 let &shellslash = save_shellslash
Shougo Matsushitad0952142024-06-20 22:05:16 +02002099
zeertzjqbc6f9672024-06-21 07:51:40 +02002100 au! CursorMovedC : let g:pos += [getcmdpos()]
2101 let g:pos = []
2102 call feedkeys(":hello\<Left>\<C-R>=''\<CR>\<Left>\<Right>\<Esc>", 'xt')
2103 call assert_equal([5, 4, 5], g:pos)
2104 let g:pos = []
2105 call feedkeys(":12345678\<C-R>=setcmdpos(3)??''\<CR>\<Esc>", 'xt')
2106 call assert_equal([3], g:pos)
2107 let g:pos = []
2108 call feedkeys(":12345678\<C-R>=setcmdpos(3)??''\<CR>\<Left>\<Esc>", 'xt')
2109 call assert_equal([3, 2], g:pos)
Shougo Matsushitad0952142024-06-20 22:05:16 +02002110 au! CursorMovedC
2111
zeertzjqbc6f9672024-06-21 07:51:40 +02002112 " setcmdpos() is no-op inside an autocommand
2113 au! CursorMovedC : let g:pos += [getcmdpos()] | call setcmdpos(1)
2114 let g:pos = []
2115 call feedkeys(":hello\<Left>\<Left>\<Esc>", 'xt')
2116 call assert_equal([5, 4], g:pos)
Shougo Matsushitad0952142024-06-20 22:05:16 +02002117 au! CursorMovedC
zeertzjqbc6f9672024-06-21 07:51:40 +02002118
2119 unlet g:entered
2120 unlet g:left
2121 unlet g:pos
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02002122endfunc
Bram Moolenaar53f0c962017-10-22 14:23:59 +02002123
2124" Test for BufWritePre autocommand that deletes or unloads the buffer.
2125func Test_BufWritePre()
2126 %bwipe
2127 au BufWritePre Xxx1 bunload
2128 au BufWritePre Xxx2 bwipe
2129
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01002130 call writefile(['start of Xxx1', 'test', 'end of Xxx1'], 'Xxx1', 'D')
2131 call writefile(['start of Xxx2', 'test', 'end of Xxx2'], 'Xxx2', 'D')
Bram Moolenaar53f0c962017-10-22 14:23:59 +02002132
2133 edit Xtest
2134 e! Xxx2
2135 bdel Xtest
2136 e Xxx1
2137 " write it, will unload it and give an error msg
Bram Moolenaare2e40752020-09-04 21:18:46 +02002138 call assert_fails('w', 'E203:')
Bram Moolenaar53f0c962017-10-22 14:23:59 +02002139 call assert_equal('Xxx2', bufname('%'))
2140 edit Xtest
2141 e! Xxx2
2142 bwipe Xtest
2143 " write it, will delete the buffer and give an error msg
Bram Moolenaare2e40752020-09-04 21:18:46 +02002144 call assert_fails('w', 'E203:')
Bram Moolenaar53f0c962017-10-22 14:23:59 +02002145 call assert_equal('Xxx1', bufname('%'))
2146 au! BufWritePre
Bram Moolenaar53f0c962017-10-22 14:23:59 +02002147endfunc
2148
2149" Test for BufUnload autocommand that unloads all the other buffers
2150func Test_bufunload_all()
Bram Moolenaarf08b0eb2021-10-16 13:00:14 +01002151 let g:test_is_flaky = 1
Christian Brabandtee17b6f2023-09-09 11:23:50 +02002152 call writefile(['Test file Xxx1'], 'Xxx1', 'D')
2153 call writefile(['Test file Xxx2'], 'Xxx2', 'D')
Bram Moolenaar53f0c962017-10-22 14:23:59 +02002154
Bram Moolenaarc79745a2019-05-20 22:12:34 +02002155 let content =<< trim [CODE]
2156 func UnloadAllBufs()
2157 let i = 1
2158 while i <= bufnr('$')
2159 if i != bufnr('%') && bufloaded(i)
2160 exe i . 'bunload'
2161 endif
2162 let i += 1
2163 endwhile
2164 endfunc
2165 au BufUnload * call UnloadAllBufs()
2166 au VimLeave * call writefile(['Test Finished'], 'Xout')
2167 edit Xxx1
2168 split Xxx2
2169 q
2170 [CODE]
2171
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01002172 call writefile(content, 'Xbunloadtest', 'D')
Bram Moolenaar53f0c962017-10-22 14:23:59 +02002173
2174 call delete('Xout')
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01002175 call system(GetVimCommandClean() .. ' -N --not-a-term -S Xbunloadtest')
Bram Moolenaar53f0c962017-10-22 14:23:59 +02002176 call assert_true(filereadable('Xout'))
2177
Bram Moolenaar53f0c962017-10-22 14:23:59 +02002178 call delete('Xout')
2179endfunc
2180
2181" Some tests for buffer-local autocommands
2182func Test_buflocal_autocmd()
2183 let g:bname = ''
2184 edit xx
2185 au BufLeave <buffer> let g:bname = expand("%")
2186 " here, autocommand for xx should trigger.
2187 " but autocommand shall not apply to buffer named <buffer>.
2188 edit somefile
2189 call assert_equal('xx', g:bname)
2190 let g:bname = ''
2191 " here, autocommand shall be auto-deleted
2192 bwipe xx
2193 " autocmd should not trigger
2194 edit xx
2195 call assert_equal('', g:bname)
2196 " autocmd should not trigger
2197 edit somefile
2198 call assert_equal('', g:bname)
2199 enew
2200 unlet g:bname
2201endfunc
Bram Moolenaar430dc5d2017-11-02 21:04:47 +01002202
2203" Test for "*Cmd" autocommands
2204func Test_Cmd_Autocmds()
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01002205 call writefile(['start of Xxx', "\tabc2", 'end of Xxx'], 'Xxx', 'D')
Bram Moolenaar430dc5d2017-11-02 21:04:47 +01002206
2207 enew!
2208 au BufReadCmd XtestA 0r Xxx|$del
2209 edit XtestA " will read text of Xxd instead
2210 call assert_equal('start of Xxx', getline(1))
2211
2212 au BufWriteCmd XtestA call append(line("$"), "write")
2213 write " will append a line to the file
2214 call assert_equal('write', getline('$'))
Bram Moolenaare2e40752020-09-04 21:18:46 +02002215 call assert_fails('read XtestA', 'E484:') " should not read anything
Bram Moolenaar430dc5d2017-11-02 21:04:47 +01002216 call assert_equal('write', getline(4))
2217
2218 " now we have:
2219 " 1 start of Xxx
2220 " 2 abc2
2221 " 3 end of Xxx
2222 " 4 write
2223
2224 au FileReadCmd XtestB '[r Xxx
2225 2r XtestB " will read Xxx below line 2 instead
2226 call assert_equal('start of Xxx', getline(3))
2227
2228 " now we have:
2229 " 1 start of Xxx
2230 " 2 abc2
2231 " 3 start of Xxx
2232 " 4 abc2
2233 " 5 end of Xxx
2234 " 6 end of Xxx
2235 " 7 write
2236
2237 au FileWriteCmd XtestC '[,']copy $
2238 normal 4GA1
2239 4,5w XtestC " will copy lines 4 and 5 to the end
2240 call assert_equal("\tabc21", getline(8))
Bram Moolenaare2e40752020-09-04 21:18:46 +02002241 call assert_fails('r XtestC', 'E484:') " should not read anything
Bram Moolenaar430dc5d2017-11-02 21:04:47 +01002242 call assert_equal("end of Xxx", getline(9))
2243
2244 " now we have:
2245 " 1 start of Xxx
2246 " 2 abc2
2247 " 3 start of Xxx
2248 " 4 abc21
2249 " 5 end of Xxx
2250 " 6 end of Xxx
2251 " 7 write
2252 " 8 abc21
2253 " 9 end of Xxx
2254
2255 let g:lines = []
2256 au FileAppendCmd XtestD call extend(g:lines, getline(line("'["), line("']")))
2257 w >>XtestD " will add lines to 'lines'
2258 call assert_equal(9, len(g:lines))
Bram Moolenaare2e40752020-09-04 21:18:46 +02002259 call assert_fails('$r XtestD', 'E484:') " should not read anything
Bram Moolenaar430dc5d2017-11-02 21:04:47 +01002260 call assert_equal(9, line('$'))
2261 call assert_equal('end of Xxx', getline('$'))
2262
2263 au BufReadCmd XtestE 0r Xxx|$del
2264 sp XtestE " split window with test.out
2265 call assert_equal('end of Xxx', getline(3))
2266
2267 let g:lines = []
2268 exe "normal 2Goasdf\<Esc>\<C-W>\<C-W>"
2269 au BufWriteCmd XtestE call extend(g:lines, getline(0, '$'))
2270 wall " will write other window to 'lines'
2271 call assert_equal(4, len(g:lines), g:lines)
2272 call assert_equal('asdf', g:lines[2])
2273
2274 au! BufReadCmd
2275 au! BufWriteCmd
2276 au! FileReadCmd
2277 au! FileWriteCmd
2278 au! FileAppendCmd
2279 %bwipe!
Bram Moolenaar430dc5d2017-11-02 21:04:47 +01002280 enew!
2281endfunc
Bram Moolenaaraace2152017-11-05 16:23:10 +01002282
Bram Moolenaar0fff4412020-03-29 16:06:29 +02002283func s:ReadFile()
2284 setl noswapfile nomodified
2285 let filename = resolve(expand("<afile>:p"))
2286 execute 'read' fnameescape(filename)
2287 1d_
2288 exe 'file' fnameescape(filename)
2289 setl buftype=acwrite
2290endfunc
2291
2292func s:WriteFile()
2293 let filename = resolve(expand("<afile>:p"))
2294 setl buftype=
2295 noautocmd execute 'write' fnameescape(filename)
2296 setl buftype=acwrite
2297 setl nomodified
2298endfunc
2299
2300func Test_BufReadCmd()
2301 autocmd BufReadCmd *.test call s:ReadFile()
2302 autocmd BufWriteCmd *.test call s:WriteFile()
2303
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01002304 call writefile(['one', 'two', 'three'], 'Xcmd.test', 'D')
Bram Moolenaar0fff4412020-03-29 16:06:29 +02002305 edit Xcmd.test
2306 call assert_match('Xcmd.test" line 1 of 3', execute('file'))
2307 normal! Gofour
2308 write
2309 call assert_equal(['one', 'two', 'three', 'four'], readfile('Xcmd.test'))
2310
2311 bwipe!
Bram Moolenaar0fff4412020-03-29 16:06:29 +02002312 au! BufReadCmd
2313 au! BufWriteCmd
2314endfunc
2315
zeertzjq9c8f9462022-08-30 18:17:15 +01002316func Test_BufWriteCmd()
2317 autocmd BufWriteCmd Xbufwritecmd let g:written = 1
2318 new
2319 file Xbufwritecmd
2320 set buftype=acwrite
Bram Moolenaar6f14da12022-09-07 21:30:44 +01002321 call mkdir('Xbufwritecmd', 'D')
zeertzjq9c8f9462022-08-30 18:17:15 +01002322 write
2323 " BufWriteCmd should be triggered even if a directory has the same name
2324 call assert_equal(1, g:written)
zeertzjq9c8f9462022-08-30 18:17:15 +01002325 unlet g:written
2326 au! BufWriteCmd
2327 bwipe!
2328endfunc
2329
Bram Moolenaaraace2152017-11-05 16:23:10 +01002330func SetChangeMarks(start, end)
Bram Moolenaar97c69432021-01-15 16:45:21 +01002331 exe a:start .. 'mark ['
2332 exe a:end .. 'mark ]'
Bram Moolenaaraace2152017-11-05 16:23:10 +01002333endfunc
2334
2335" Verify the effects of autocmds on '[ and ']
2336func Test_change_mark_in_autocmds()
2337 edit! Xtest
Bram Moolenaar97c69432021-01-15 16:45:21 +01002338 call feedkeys("ia\<CR>b\<CR>c\<CR>d\<C-g>u\<Esc>", 'xtn')
Bram Moolenaaraace2152017-11-05 16:23:10 +01002339
2340 call SetChangeMarks(2, 3)
2341 write
2342 call assert_equal([1, 4], [line("'["), line("']")])
2343
2344 call SetChangeMarks(2, 3)
2345 au BufWritePre * call assert_equal([1, 4], [line("'["), line("']")])
2346 write
2347 au! BufWritePre
2348
Bram Moolenaar14ddd222020-08-05 12:02:40 +02002349 if has('unix')
Bram Moolenaaraace2152017-11-05 16:23:10 +01002350 write XtestFilter
2351 write >> XtestFilter
2352
2353 call SetChangeMarks(2, 3)
2354 " Marks are set to the entire range of the write
2355 au FilterWritePre * call assert_equal([1, 4], [line("'["), line("']")])
2356 " '[ is adjusted to just before the line that will receive the filtered
2357 " data
2358 au FilterReadPre * call assert_equal([4, 4], [line("'["), line("']")])
2359 " The filtered data is read into the buffer, and the source lines are
2360 " still present, so the range is after the source lines
2361 au FilterReadPost * call assert_equal([5, 12], [line("'["), line("']")])
2362 %!cat XtestFilter
2363 " After the filtered data is read, the original lines are deleted
2364 call assert_equal([1, 8], [line("'["), line("']")])
2365 au! FilterWritePre,FilterReadPre,FilterReadPost
2366 undo
2367
2368 call SetChangeMarks(1, 4)
2369 au FilterWritePre * call assert_equal([2, 3], [line("'["), line("']")])
2370 au FilterReadPre * call assert_equal([3, 3], [line("'["), line("']")])
2371 au FilterReadPost * call assert_equal([4, 11], [line("'["), line("']")])
2372 2,3!cat XtestFilter
2373 call assert_equal([2, 9], [line("'["), line("']")])
2374 au! FilterWritePre,FilterReadPre,FilterReadPost
2375 undo
2376
2377 call delete('XtestFilter')
2378 endif
2379
2380 call SetChangeMarks(1, 4)
2381 au FileWritePre * call assert_equal([2, 3], [line("'["), line("']")])
2382 2,3write Xtest2
2383 au! FileWritePre
2384
2385 call SetChangeMarks(2, 3)
2386 au FileAppendPre * call assert_equal([1, 4], [line("'["), line("']")])
2387 write >> Xtest2
2388 au! FileAppendPre
2389
2390 call SetChangeMarks(1, 4)
2391 au FileAppendPre * call assert_equal([2, 3], [line("'["), line("']")])
2392 2,3write >> Xtest2
2393 au! FileAppendPre
2394
2395 call SetChangeMarks(1, 1)
2396 au FileReadPre * call assert_equal([3, 1], [line("'["), line("']")])
2397 au FileReadPost * call assert_equal([4, 11], [line("'["), line("']")])
2398 3read Xtest2
2399 au! FileReadPre,FileReadPost
2400 undo
2401
2402 call SetChangeMarks(4, 4)
2403 " When the line is 0, it's adjusted to 1
2404 au FileReadPre * call assert_equal([1, 4], [line("'["), line("']")])
2405 au FileReadPost * call assert_equal([1, 8], [line("'["), line("']")])
2406 0read Xtest2
2407 au! FileReadPre,FileReadPost
2408 undo
2409
2410 call SetChangeMarks(4, 4)
2411 " When the line is 0, it's adjusted to 1
2412 au FileReadPre * call assert_equal([1, 4], [line("'["), line("']")])
2413 au FileReadPost * call assert_equal([2, 9], [line("'["), line("']")])
2414 1read Xtest2
2415 au! FileReadPre,FileReadPost
2416 undo
2417
2418 bwipe!
2419 call delete('Xtest')
2420 call delete('Xtest2')
2421endfunc
2422
2423func Test_Filter_noshelltemp()
Bram Moolenaaraeb313f2020-11-27 19:13:28 +01002424 CheckExecutable cat
Bram Moolenaaraace2152017-11-05 16:23:10 +01002425
2426 enew!
2427 call setline(1, ['a', 'b', 'c', 'd'])
2428
2429 let shelltemp = &shelltemp
2430 set shelltemp
2431
2432 let g:filter_au = 0
2433 au FilterWritePre * let g:filter_au += 1
2434 au FilterReadPre * let g:filter_au += 1
2435 au FilterReadPost * let g:filter_au += 1
2436 %!cat
2437 call assert_equal(3, g:filter_au)
2438
2439 if has('filterpipe')
2440 set noshelltemp
2441
2442 let g:filter_au = 0
2443 au FilterWritePre * let g:filter_au += 1
2444 au FilterReadPre * let g:filter_au += 1
2445 au FilterReadPost * let g:filter_au += 1
2446 %!cat
2447 call assert_equal(0, g:filter_au)
2448 endif
2449
2450 au! FilterWritePre,FilterReadPre,FilterReadPost
2451 let &shelltemp = shelltemp
2452 bwipe!
2453endfunc
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01002454
2455func Test_TextYankPost()
2456 enew!
2457 call setline(1, ['foo'])
2458
2459 let g:event = []
2460 au TextYankPost * let g:event = copy(v:event)
2461
2462 call assert_equal({}, v:event)
2463 call assert_fails('let v:event = {}', 'E46:')
2464 call assert_fails('let v:event.mykey = 0', 'E742:')
2465
2466 norm "ayiw
2467 call assert_equal(
Bram Moolenaara016eeb2022-04-09 11:37:38 +01002468 \ #{regcontents: ['foo'], regname: 'a', operator: 'y',
2469 \ regtype: 'v', visual: v:false, inclusive: v:true},
2470 \ g:event)
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01002471 norm y_
2472 call assert_equal(
Bram Moolenaara016eeb2022-04-09 11:37:38 +01002473 \ #{regcontents: ['foo'], regname: '', operator: 'y', regtype: 'V',
2474 \ visual: v:false, inclusive: v:false},
2475 \ g:event)
Bram Moolenaar37d16732020-06-12 22:09:01 +02002476 norm Vy
2477 call assert_equal(
Bram Moolenaara016eeb2022-04-09 11:37:38 +01002478 \ #{regcontents: ['foo'], regname: '', operator: 'y', regtype: 'V',
2479 \ visual: v:true, inclusive: v:true},
2480 \ g:event)
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01002481 call feedkeys("\<C-V>y", 'x')
2482 call assert_equal(
Bram Moolenaara016eeb2022-04-09 11:37:38 +01002483 \ #{regcontents: ['f'], regname: '', operator: 'y', regtype: "\x161",
2484 \ visual: v:true, inclusive: v:true},
2485 \ g:event)
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01002486 norm "xciwbar
2487 call assert_equal(
Bram Moolenaara016eeb2022-04-09 11:37:38 +01002488 \ #{regcontents: ['foo'], regname: 'x', operator: 'c', regtype: 'v',
2489 \ visual: v:false, inclusive: v:true},
2490 \ g:event)
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01002491 norm "bdiw
2492 call assert_equal(
Bram Moolenaara016eeb2022-04-09 11:37:38 +01002493 \ #{regcontents: ['bar'], regname: 'b', operator: 'd', regtype: 'v',
2494 \ visual: v:false, inclusive: v:true},
2495 \ g:event)
2496
2497 call setline(1, 'foobar')
2498 " exclusive motion
2499 norm $"ay0
2500 call assert_equal(
2501 \ #{regcontents: ['fooba'], regname: 'a', operator: 'y', regtype: 'v',
2502 \ visual: v:false, inclusive: v:false},
2503 \ g:event)
2504 " inclusive motion
2505 norm 0"ay$
2506 call assert_equal(
2507 \ #{regcontents: ['foobar'], regname: 'a', operator: 'y', regtype: 'v',
2508 \ visual: v:false, inclusive: v:true},
2509 \ g:event)
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01002510
2511 call assert_equal({}, v:event)
2512
Bram Moolenaarfccbf062020-11-26 20:34:00 +01002513 if has('clipboard_working') && !has('gui_running')
2514 " Test that when the visual selection is automatically copied to clipboard
2515 " register a TextYankPost is emitted
2516 call setline(1, ['foobar'])
2517
2518 let @* = ''
2519 set clipboard=autoselect
2520 exe "norm! ggviw\<Esc>"
2521 call assert_equal(
Bram Moolenaara016eeb2022-04-09 11:37:38 +01002522 \ #{regcontents: ['foobar'], regname: '*', operator: 'y',
2523 \ regtype: 'v', visual: v:true, inclusive: v:false},
2524 \ g:event)
Bram Moolenaarfccbf062020-11-26 20:34:00 +01002525
2526 let @+ = ''
2527 set clipboard=autoselectplus
2528 exe "norm! ggviw\<Esc>"
2529 call assert_equal(
Bram Moolenaara016eeb2022-04-09 11:37:38 +01002530 \ #{regcontents: ['foobar'], regname: '+', operator: 'y',
2531 \ regtype: 'v', visual: v:true, inclusive: v:false},
2532 \ g:event)
Bram Moolenaarfccbf062020-11-26 20:34:00 +01002533
2534 set clipboard&vim
2535 endif
2536
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01002537 au! TextYankPost
2538 unlet g:event
2539 bwipe!
2540endfunc
Bram Moolenaar9bca8052017-12-18 12:37:55 +01002541
Bram Moolenaar9a046fd2021-01-28 13:47:59 +01002542func Test_autocommand_all_events()
2543 call assert_fails('au * * bwipe', 'E1155:')
2544 call assert_fails('au * x bwipe', 'E1155:')
Bram Moolenaarb6db1462021-12-24 19:24:47 +00002545 call assert_fails('au! * x bwipe', 'E1155:')
Bram Moolenaar4fb921e2017-12-18 15:33:00 +01002546endfunc
Bram Moolenaarb7407d32018-02-03 17:36:27 +01002547
Bram Moolenaarf6246f52022-02-11 16:30:12 +00002548func Test_autocmd_user()
2549 au User MyEvent let s:res = [expand("<afile>"), expand("<amatch>")]
2550 doautocmd User MyEvent
2551 call assert_equal(['MyEvent', 'MyEvent'], s:res)
2552 au! User
2553 unlet s:res
2554endfunc
2555
Bram Moolenaar3b014be2022-11-13 17:53:46 +00002556func Test_autocmd_user_clear_group()
2557 CheckRunVimInTerminal
2558
2559 let lines =<< trim END
2560 autocmd! User
2561 for i in range(1, 999)
2562 exe 'autocmd User ' .. 'Foo' .. i .. ' bar'
2563 endfor
2564 au CmdlineLeave : call timer_start(0, {-> execute('autocmd! User')})
2565 END
2566 call writefile(lines, 'XautoUser', 'D')
2567 let buf = RunVimInTerminal('-S XautoUser', {'rows': 10})
2568
2569 " this was using freed memory
2570 call term_sendkeys(buf, ":autocmd User\<CR>")
2571 call TermWait(buf, 50)
2572 call term_sendkeys(buf, "G")
2573
2574 call StopVimInTerminal(buf)
2575endfunc
2576
Bram Moolenaaref2c3252022-11-25 16:31:51 +00002577func Test_autocmd_CmdlineLeave_unlet()
2578 CheckRunVimInTerminal
2579
2580 let lines =<< trim END
2581 for i in range(1, 999)
2582 exe 'let g:var' .. i '=' i
2583 endfor
2584 au CmdlineLeave : call timer_start(0, {-> execute('unlet g:var990')})
2585 END
2586 call writefile(lines, 'XleaveUnlet', 'D')
2587 let buf = RunVimInTerminal('-S XleaveUnlet', {'rows': 10})
2588
2589 " this was using freed memory
2590 call term_sendkeys(buf, ":let g:\<CR>")
2591 call TermWait(buf, 50)
2592 call term_sendkeys(buf, "G")
2593 call TermWait(buf, 50)
2594 call term_sendkeys(buf, "\<CR>") " for the hit-enter prompt
2595
2596 call StopVimInTerminal(buf)
2597endfunc
2598
Bram Moolenaarb7407d32018-02-03 17:36:27 +01002599function s:Before_test_dirchanged()
2600 augroup test_dirchanged
2601 autocmd!
2602 augroup END
2603 let s:li = []
2604 let s:dir_this = getcwd()
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02002605 let s:dir_foo = s:dir_this . '/Xfoo'
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01002606 call mkdir(s:dir_foo)
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02002607 let s:dir_bar = s:dir_this . '/Xbar'
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01002608 call mkdir(s:dir_bar)
Bram Moolenaarb7407d32018-02-03 17:36:27 +01002609endfunc
2610
2611function s:After_test_dirchanged()
Bram Moolenaar3503d7c2019-11-09 20:10:17 +01002612 call chdir(s:dir_this)
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01002613 call delete(s:dir_foo, 'd')
2614 call delete(s:dir_bar, 'd')
Bram Moolenaarb7407d32018-02-03 17:36:27 +01002615 augroup test_dirchanged
2616 autocmd!
2617 augroup END
2618endfunc
2619
2620function Test_dirchanged_global()
2621 call s:Before_test_dirchanged()
Bram Moolenaarf6246f52022-02-11 16:30:12 +00002622 autocmd test_dirchanged DirChangedPre global call add(s:li, expand("<amatch>") .. " pre cd " .. v:event.directory)
Bram Moolenaarb7407d32018-02-03 17:36:27 +01002623 autocmd test_dirchanged DirChanged global call add(s:li, "cd:")
2624 autocmd test_dirchanged DirChanged global call add(s:li, expand("<afile>"))
Bram Moolenaar3503d7c2019-11-09 20:10:17 +01002625 call chdir(s:dir_foo)
Bram Moolenaarf6246f52022-02-11 16:30:12 +00002626 let expected = ["global pre cd " .. s:dir_foo, "cd:", s:dir_foo]
Bram Moolenaar28e8f732022-02-09 12:58:20 +00002627 call assert_equal(expected, s:li)
Bram Moolenaar3503d7c2019-11-09 20:10:17 +01002628 call chdir(s:dir_foo)
Bram Moolenaar28e8f732022-02-09 12:58:20 +00002629 call assert_equal(expected, s:li)
Bram Moolenaar3503d7c2019-11-09 20:10:17 +01002630 exe 'lcd ' .. fnameescape(s:dir_bar)
Bram Moolenaar28e8f732022-02-09 12:58:20 +00002631 call assert_equal(expected, s:li)
Bram Moolenaard8c9d322022-06-12 11:49:16 +01002632
2633 exe 'cd ' .. s:dir_foo
2634 exe 'cd ' .. s:dir_bar
2635 autocmd! test_dirchanged DirChanged global let g:result = expand("<afile>")
2636 cd -
Bram Moolenaardb77c492022-06-12 23:26:50 +01002637 call assert_equal(s:dir_foo, substitute(g:result, '\\', '/', 'g'))
Bram Moolenaard8c9d322022-06-12 11:49:16 +01002638
Bram Moolenaarb7407d32018-02-03 17:36:27 +01002639 call s:After_test_dirchanged()
2640endfunc
2641
2642function Test_dirchanged_local()
2643 call s:Before_test_dirchanged()
2644 autocmd test_dirchanged DirChanged window call add(s:li, "lcd:")
2645 autocmd test_dirchanged DirChanged window call add(s:li, expand("<afile>"))
Bram Moolenaar3503d7c2019-11-09 20:10:17 +01002646 call chdir(s:dir_foo)
Bram Moolenaarb7407d32018-02-03 17:36:27 +01002647 call assert_equal([], s:li)
Bram Moolenaar3503d7c2019-11-09 20:10:17 +01002648 exe 'lcd ' .. fnameescape(s:dir_bar)
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01002649 call assert_equal(["lcd:", s:dir_bar], s:li)
Bram Moolenaar3503d7c2019-11-09 20:10:17 +01002650 exe 'lcd ' .. fnameescape(s:dir_bar)
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01002651 call assert_equal(["lcd:", s:dir_bar], s:li)
Bram Moolenaarb7407d32018-02-03 17:36:27 +01002652 call s:After_test_dirchanged()
2653endfunc
2654
2655function Test_dirchanged_auto()
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +02002656 CheckOption autochdir
Bram Moolenaarb7407d32018-02-03 17:36:27 +01002657 call s:Before_test_dirchanged()
2658 call test_autochdir()
Bram Moolenaar28e8f732022-02-09 12:58:20 +00002659 autocmd test_dirchanged DirChangedPre auto call add(s:li, "pre cd " .. v:event.directory)
Bram Moolenaarb7407d32018-02-03 17:36:27 +01002660 autocmd test_dirchanged DirChanged auto call add(s:li, "auto:")
2661 autocmd test_dirchanged DirChanged auto call add(s:li, expand("<afile>"))
2662 set acd
Bram Moolenaar3503d7c2019-11-09 20:10:17 +01002663 cd ..
Bram Moolenaarb7407d32018-02-03 17:36:27 +01002664 call assert_equal([], s:li)
Bram Moolenaar61abe7d2022-08-30 21:46:08 +01002665 exe 'edit ' . s:dir_foo . '/Xautofile'
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01002666 call assert_equal(s:dir_foo, getcwd())
Bram Moolenaar28e8f732022-02-09 12:58:20 +00002667 let expected = ["pre cd " .. s:dir_foo, "auto:", s:dir_foo]
2668 call assert_equal(expected, s:li)
Bram Moolenaarb7407d32018-02-03 17:36:27 +01002669 set noacd
2670 bwipe!
2671 call s:After_test_dirchanged()
2672endfunc
Bram Moolenaar5a093432018-02-10 18:15:19 +01002673
2674" Test TextChangedI and TextChangedP
2675func Test_ChangedP()
2676 new
2677 call setline(1, ['foo', 'bar', 'foobar'])
2678 call test_override("char_avail", 1)
2679 set complete=. completeopt=menuone
2680
2681 func! TextChangedAutocmd(char)
2682 let g:autocmd .= a:char
2683 endfunc
2684
Christian Brabandtdb3b4462021-10-16 11:58:55 +01002685 " TextChanged will not be triggered, only check that it isn't.
Bram Moolenaar5a093432018-02-10 18:15:19 +01002686 au! TextChanged <buffer> :call TextChangedAutocmd('N')
2687 au! TextChangedI <buffer> :call TextChangedAutocmd('I')
2688 au! TextChangedP <buffer> :call TextChangedAutocmd('P')
2689
2690 call cursor(3, 1)
2691 let g:autocmd = ''
2692 call feedkeys("o\<esc>", 'tnix')
Evgeni Chasnovskid7ae2632023-10-15 09:59:00 +02002693 call assert_equal('I', g:autocmd)
Bram Moolenaar5a093432018-02-10 18:15:19 +01002694
2695 let g:autocmd = ''
Christian Brabandt4bca4892023-10-27 19:26:49 +02002696 call feedkeys("Sf", 'tnix')
2697 call assert_equal('II', g:autocmd)
2698
2699 let g:autocmd = ''
Bram Moolenaar5a093432018-02-10 18:15:19 +01002700 call feedkeys("Sf\<C-N>", 'tnix')
Christian Brabandt4bca4892023-10-27 19:26:49 +02002701 call assert_equal('IIP', g:autocmd)
Bram Moolenaar5a093432018-02-10 18:15:19 +01002702
2703 let g:autocmd = ''
2704 call feedkeys("Sf\<C-N>\<C-N>", 'tnix')
Christian Brabandt4bca4892023-10-27 19:26:49 +02002705 call assert_equal('IIPP', g:autocmd)
Bram Moolenaar5a093432018-02-10 18:15:19 +01002706
2707 let g:autocmd = ''
2708 call feedkeys("Sf\<C-N>\<C-N>\<C-N>", 'tnix')
Christian Brabandt4bca4892023-10-27 19:26:49 +02002709 call assert_equal('IIPPP', g:autocmd)
Bram Moolenaar5a093432018-02-10 18:15:19 +01002710
2711 let g:autocmd = ''
2712 call feedkeys("Sf\<C-N>\<C-N>\<C-N>\<C-N>", 'tnix')
Christian Brabandt4bca4892023-10-27 19:26:49 +02002713 call assert_equal('IIPPPP', g:autocmd)
Bram Moolenaar5a093432018-02-10 18:15:19 +01002714
2715 call assert_equal(['foo', 'bar', 'foobar', 'foo'], getline(1, '$'))
2716 " TODO: how should it handle completeopt=noinsert,noselect?
2717
2718 " CleanUp
2719 call test_override("char_avail", 0)
2720 au! TextChanged
2721 au! TextChangedI
2722 au! TextChangedP
2723 delfu TextChangedAutocmd
2724 unlet! g:autocmd
2725 set complete&vim completeopt&vim
2726
2727 bw!
2728endfunc
Bram Moolenaar8c64a362018-03-23 22:39:31 +01002729
Bram Moolenaar91d2e782018-08-07 19:05:01 +02002730let g:setline_handled = v:false
Bram Moolenaar1e115362019-01-09 23:01:02 +01002731func SetLineOne()
Bram Moolenaar91d2e782018-08-07 19:05:01 +02002732 if !g:setline_handled
2733 call setline(1, "(x)")
2734 let g:setline_handled = v:true
2735 endif
2736endfunc
2737
2738func Test_TextChangedI_with_setline()
2739 new
2740 call test_override('char_avail', 1)
2741 autocmd TextChangedI <buffer> call SetLineOne()
2742 call feedkeys("i(\<CR>\<Esc>", 'tx')
2743 call assert_equal('(', getline(1))
2744 call assert_equal('x)', getline(2))
2745 undo
Bram Moolenaar91d2e782018-08-07 19:05:01 +02002746 call assert_equal('', getline(1))
Bram Moolenaar9fa95062018-08-08 22:08:32 +02002747 call assert_equal('', getline(2))
Bram Moolenaar91d2e782018-08-07 19:05:01 +02002748
Bram Moolenaarca34db32022-01-20 11:17:18 +00002749 call test_override('char_avail', 0)
Bram Moolenaar91d2e782018-08-07 19:05:01 +02002750 bwipe!
2751endfunc
2752
Christian Brabandtc9e79e52024-02-09 19:34:36 +01002753func Test_TextChanged_with_norm()
2754 " For unknown reason this fails on MS-Windows
2755 CheckNotMSWindows
2756 CheckFeature terminal
2757 let buf = term_start([GetVimProg(), '--clean', '-c', 'set noswapfile'], {'term_rows': 3})
2758 call assert_equal('running', term_getstatus(buf))
2759 call term_sendkeys(buf, ":let g:a=0\<cr>")
2760 call term_wait(buf, 50)
2761 call term_sendkeys(buf, ":au! TextChanged * :let g:a+=1\<cr>")
2762 call term_wait(buf, 50)
2763 call term_sendkeys(buf, ":norm! ia\<cr>")
2764 call term_wait(buf, 50)
2765 call term_sendkeys(buf, ":echo g:a\<cr>")
2766 call term_wait(buf, 50)
2767 call WaitForAssert({-> assert_match('^1.*$', term_getline(buf, 3))})
2768 bwipe!
2769endfunc
2770
Bram Moolenaar8c64a362018-03-23 22:39:31 +01002771func Test_Changed_FirstTime()
Bram Moolenaar8c5a2782019-08-07 23:07:07 +02002772 CheckFeature terminal
2773 CheckNotGui
Bram Moolenaar3cdcb092020-03-18 19:18:10 +01002774 " Starting a terminal to run Vim is always considered flaky.
Bram Moolenaar30d53e22020-03-18 21:10:44 +01002775 let g:test_is_flaky = 1
Bram Moolenaar8c5a2782019-08-07 23:07:07 +02002776
Bram Moolenaar8c64a362018-03-23 22:39:31 +01002777 " Prepare file for TextChanged event.
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01002778 call writefile([''], 'Xchanged.txt', 'D')
Bram Moolenaar8c64a362018-03-23 22:39:31 +01002779 let buf = term_start([GetVimProg(), '--clean', '-c', 'set noswapfile'], {'term_rows': 3})
2780 call assert_equal('running', term_getstatus(buf))
Bram Moolenaar1834d372018-03-29 17:40:46 +02002781 " Wait for the ruler (in the status line) to be shown.
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01002782 " In ConPTY, there is additional character which is drawn up to the width of
2783 " the screen.
2784 if has('conpty')
2785 call WaitForAssert({-> assert_match('\<All.*$', term_getline(buf, 3))})
2786 else
2787 call WaitForAssert({-> assert_match('\<All$', term_getline(buf, 3))})
2788 endif
Bram Moolenaar8c64a362018-03-23 22:39:31 +01002789 " It's only adding autocmd, so that no event occurs.
2790 call term_sendkeys(buf, ":au! TextChanged <buffer> call writefile(['No'], 'Xchanged.txt')\<cr>")
2791 call term_sendkeys(buf, "\<C-\\>\<C-N>:qa!\<cr>")
Bram Moolenaar50182fa2018-04-28 21:34:40 +02002792 call WaitForAssert({-> assert_equal('finished', term_getstatus(buf))})
Bram Moolenaar8c64a362018-03-23 22:39:31 +01002793 call assert_equal([''], readfile('Xchanged.txt'))
2794
2795 " clean up
Bram Moolenaar8c64a362018-03-23 22:39:31 +01002796 bwipe!
2797endfunc
Bram Moolenaar0566e892019-01-24 19:37:40 +01002798
Bram Moolenaareb93f3f2019-04-04 15:04:56 +02002799func Test_autocmd_nested()
2800 let g:did_nested = 0
2801 augroup Testing
2802 au WinNew * edit somefile
2803 au BufNew * let g:did_nested = 1
2804 augroup END
2805 split
2806 call assert_equal(0, g:did_nested)
2807 close
2808 bwipe! somefile
2809
2810 " old nested argument still works
2811 augroup Testing
2812 au!
2813 au WinNew * nested edit somefile
2814 au BufNew * let g:did_nested = 1
2815 augroup END
2816 split
2817 call assert_equal(1, g:did_nested)
2818 close
2819 bwipe! somefile
2820
2821 " New ++nested argument works
2822 augroup Testing
2823 au!
2824 au WinNew * ++nested edit somefile
2825 au BufNew * let g:did_nested = 1
2826 augroup END
2827 split
2828 call assert_equal(1, g:did_nested)
2829 close
2830 bwipe! somefile
2831
Bram Moolenaarf0775142022-03-04 20:10:38 +00002832 " nested without ++ does not work in Vim9 script
2833 call assert_fails('vim9cmd au WinNew * nested echo fails', 'E1078:')
2834
Bram Moolenaareb93f3f2019-04-04 15:04:56 +02002835 augroup Testing
2836 au!
2837 augroup END
2838
2839 call assert_fails('au WinNew * ++nested ++nested echo bad', 'E983:')
2840 call assert_fails('au WinNew * nested nested echo bad', 'E983:')
2841endfunc
2842
Bram Moolenaar5fa9f232022-07-23 09:06:48 +01002843func Test_autocmd_nested_cursor_invalid()
2844 set laststatus=0
2845 copen
2846 cclose
2847 call setline(1, ['foo', 'bar', 'baz'])
2848 3
2849 augroup nested_inv
2850 autocmd User foo ++nested copen
2851 autocmd BufAdd * let &laststatus = 2 - &laststatus
2852 augroup END
2853 doautocmd User foo
2854
2855 augroup nested_inv
2856 au!
2857 augroup END
2858 set laststatus&
Bram Moolenaarb03950f2022-07-26 13:47:13 +01002859 cclose
Bram Moolenaar5fa9f232022-07-23 09:06:48 +01002860 bwipe!
2861endfunc
2862
Bram Moolenaar3d6ee8b2022-07-27 15:23:35 +01002863func Test_autocmd_nested_keeps_cursor_pos()
2864 enew
2865 call setline(1, 'foo')
2866 autocmd User foo ++nested normal! $a
2867 autocmd InsertLeave * :
2868 doautocmd User foo
2869 call assert_equal([0, 1, 3, 0], getpos('.'))
2870
2871 bwipe!
2872endfunc
2873
Bram Moolenaarb03950f2022-07-26 13:47:13 +01002874func Test_autocmd_nested_switch_window()
2875 " run this in a separate Vim so that SafeState works
2876 CheckRunVimInTerminal
2877
2878 let lines =<< trim END
2879 vim9script
2880 ['()']->writefile('Xautofile')
2881 autocmd VimEnter * ++nested edit Xautofile | split
2882 autocmd BufReadPost * autocmd SafeState * ++once foldclosed('.')
2883 autocmd WinEnter * matchadd('ErrorMsg', 'pat')
2884 END
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01002885 call writefile(lines, 'Xautoscript', 'D')
Bram Moolenaarb03950f2022-07-26 13:47:13 +01002886 let buf = RunVimInTerminal('-S Xautoscript', {'rows': 10})
2887 call VerifyScreenDump(buf, 'Test_autocmd_nested_switch', {})
2888
2889 call StopVimInTerminal(buf)
2890 call delete('Xautofile')
Bram Moolenaarb03950f2022-07-26 13:47:13 +01002891endfunc
2892
Bram Moolenaareb93f3f2019-04-04 15:04:56 +02002893func Test_autocmd_once()
2894 " Without ++once WinNew triggers twice
2895 let g:did_split = 0
2896 augroup Testing
2897 au WinNew * let g:did_split += 1
2898 augroup END
2899 split
2900 split
2901 call assert_equal(2, g:did_split)
2902 call assert_true(exists('#WinNew'))
2903 close
2904 close
2905
2906 " With ++once WinNew triggers once
2907 let g:did_split = 0
2908 augroup Testing
2909 au!
2910 au WinNew * ++once let g:did_split += 1
2911 augroup END
2912 split
2913 split
2914 call assert_equal(1, g:did_split)
2915 call assert_false(exists('#WinNew'))
2916 close
2917 close
2918
2919 call assert_fails('au WinNew * ++once ++once echo bad', 'E983:')
2920endfunc
2921
Bram Moolenaara68e5952019-04-25 22:22:01 +02002922func Test_autocmd_bufreadpre()
2923 new
2924 let b:bufreadpre = 1
Bram Moolenaarab505b12020-03-23 19:28:44 +01002925 call append(0, range(1000))
Bram Moolenaara68e5952019-04-25 22:22:01 +02002926 w! XAutocmdBufReadPre.txt
2927 autocmd BufReadPre <buffer> :let b:bufreadpre += 1
Bram Moolenaarab505b12020-03-23 19:28:44 +01002928 norm! 500gg
Bram Moolenaara68e5952019-04-25 22:22:01 +02002929 sp
Bram Moolenaarab505b12020-03-23 19:28:44 +01002930 norm! 1000gg
Bram Moolenaara68e5952019-04-25 22:22:01 +02002931 wincmd p
2932 let g:wsv1 = winsaveview()
2933 wincmd p
2934 let g:wsv2 = winsaveview()
2935 " triggers BufReadPre, should not move the cursor in either window
2936 " The topline may change one line in a large window.
2937 edit
2938 call assert_inrange(g:wsv2.topline - 1, g:wsv2.topline + 1, winsaveview().topline)
2939 call assert_equal(g:wsv2.lnum, winsaveview().lnum)
2940 call assert_equal(2, b:bufreadpre)
2941 wincmd p
2942 call assert_equal(g:wsv1.topline, winsaveview().topline)
2943 call assert_equal(g:wsv1.lnum, winsaveview().lnum)
2944 call assert_equal(2, b:bufreadpre)
2945 " Now set the cursor position in an BufReadPre autocommand
2946 " (even though the position will be invalid, this should make Vim reset the
2947 " cursor position in the other window.
2948 wincmd p
2949 set cpo+=g
2950 " won't do anything, but try to set the cursor on an invalid lnum
2951 autocmd BufReadPre <buffer> :norm! 70gg
2952 " triggers BufReadPre, should not move the cursor in either window
2953 e
2954 call assert_equal(1, winsaveview().topline)
2955 call assert_equal(1, winsaveview().lnum)
2956 call assert_equal(3, b:bufreadpre)
2957 wincmd p
2958 call assert_equal(g:wsv1.topline, winsaveview().topline)
2959 call assert_equal(g:wsv1.lnum, winsaveview().lnum)
2960 call assert_equal(3, b:bufreadpre)
2961 close
2962 close
2963 call delete('XAutocmdBufReadPre.txt')
2964 set cpo-=g
2965endfunc
2966
Bram Moolenaar5e66b422019-01-24 21:58:10 +01002967" FileChangedShell tested in test_filechanged.vim
Bram Moolenaar69ea5872019-04-25 20:29:00 +02002968
2969" Tests for the following autocommands:
2970" - FileWritePre writing a compressed file
2971" - FileReadPost reading a compressed file
2972" - BufNewFile reading a file template
2973" - BufReadPre decompressing the file to be read
2974" - FilterReadPre substituting characters in the temp file
2975" - FilterReadPost substituting characters after filtering
2976" - FileReadPre set options for decompression
2977" - FileReadPost decompress the file
2978func Test_ReadWrite_Autocmds()
2979 " Run this test only on Unix-like systems and if gzip is available
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +02002980 CheckUnix
2981 CheckExecutable gzip
Bram Moolenaar69ea5872019-04-25 20:29:00 +02002982
2983 " Make $GZIP empty, "-v" would cause trouble.
2984 let $GZIP = ""
2985
2986 " Use a FileChangedShell autocommand to avoid a prompt for 'Xtestfile.gz'
2987 " being modified outside of Vim (noticed on Solaris).
2988 au FileChangedShell * echo 'caught FileChangedShell'
2989
2990 " Test for the FileReadPost, FileWritePre and FileWritePost autocmds
2991 augroup Test1
2992 au!
2993 au FileWritePre *.gz '[,']!gzip
2994 au FileWritePost *.gz undo
2995 au FileReadPost *.gz '[,']!gzip -d
2996 augroup END
2997
2998 new
2999 set bin
3000 call append(0, [
3001 \ 'line 2 Abcdefghijklmnopqrstuvwxyz',
3002 \ 'line 3 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
3003 \ 'line 4 Abcdefghijklmnopqrstuvwxyz',
3004 \ 'line 5 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
3005 \ 'line 6 Abcdefghijklmnopqrstuvwxyz',
3006 \ 'line 7 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
3007 \ 'line 8 Abcdefghijklmnopqrstuvwxyz',
3008 \ 'line 9 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
3009 \ 'line 10 Abcdefghijklmnopqrstuvwxyz'
3010 \ ])
3011 1,9write! Xtestfile.gz
3012 enew! | close
3013
3014 new
3015 " Read and decompress the testfile
3016 0read Xtestfile.gz
3017 call assert_equal([
3018 \ 'line 2 Abcdefghijklmnopqrstuvwxyz',
3019 \ 'line 3 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
3020 \ 'line 4 Abcdefghijklmnopqrstuvwxyz',
3021 \ 'line 5 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
3022 \ 'line 6 Abcdefghijklmnopqrstuvwxyz',
3023 \ 'line 7 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
3024 \ 'line 8 Abcdefghijklmnopqrstuvwxyz',
3025 \ 'line 9 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
3026 \ 'line 10 Abcdefghijklmnopqrstuvwxyz'
3027 \ ], getline(1, 9))
3028 enew! | close
3029
3030 augroup Test1
3031 au!
3032 augroup END
3033
3034 " Test for the FileAppendPre and FileAppendPost autocmds
3035 augroup Test2
3036 au!
3037 au BufNewFile *.c read Xtest.c
3038 au FileAppendPre *.out '[,']s/new/NEW/
3039 au FileAppendPost *.out !cat Xtest.c >> test.out
3040 augroup END
3041
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01003042 call writefile(['/*', ' * Here is a new .c file', ' */'], 'Xtest.c', 'D')
Bram Moolenaar69ea5872019-04-25 20:29:00 +02003043 new foo.c " should load Xtest.c
3044 call assert_equal(['/*', ' * Here is a new .c file', ' */'], getline(2, 4))
3045 w! >> test.out " append it to the output file
3046
3047 let contents = readfile('test.out')
3048 call assert_equal(' * Here is a NEW .c file', contents[2])
3049 call assert_equal(' * Here is a new .c file', contents[5])
3050
3051 call delete('test.out')
3052 enew! | close
3053 augroup Test2
3054 au!
3055 augroup END
3056
3057 " Test for the BufReadPre and BufReadPost autocmds
3058 augroup Test3
3059 au!
3060 " setup autocommands to decompress before reading and re-compress
3061 " afterwards
3062 au BufReadPre *.gz exe '!gzip -d ' . shellescape(expand("<afile>"))
3063 au BufReadPre *.gz call rename(expand("<afile>:r"), expand("<afile>"))
3064 au BufReadPost *.gz call rename(expand("<afile>"), expand("<afile>:r"))
3065 au BufReadPost *.gz exe '!gzip ' . shellescape(expand("<afile>:r"))
3066 augroup END
3067
3068 e! Xtestfile.gz " Edit compressed file
3069 call assert_equal([
3070 \ 'line 2 Abcdefghijklmnopqrstuvwxyz',
3071 \ 'line 3 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
3072 \ 'line 4 Abcdefghijklmnopqrstuvwxyz',
3073 \ 'line 5 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
3074 \ 'line 6 Abcdefghijklmnopqrstuvwxyz',
3075 \ 'line 7 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
3076 \ 'line 8 Abcdefghijklmnopqrstuvwxyz',
3077 \ 'line 9 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
3078 \ 'line 10 Abcdefghijklmnopqrstuvwxyz'
3079 \ ], getline(1, 9))
3080
3081 w! >> test.out " Append it to the output file
3082
3083 augroup Test3
3084 au!
3085 augroup END
3086
3087 " Test for the FilterReadPre and FilterReadPost autocmds.
3088 set shelltemp " need temp files here
3089 augroup Test4
3090 au!
3091 au FilterReadPre *.out call rename(expand("<afile>"), expand("<afile>") . ".t")
3092 au FilterReadPre *.out exe 'silent !sed s/e/E/ ' . shellescape(expand("<afile>")) . ".t >" . shellescape(expand("<afile>"))
3093 au FilterReadPre *.out exe 'silent !rm ' . shellescape(expand("<afile>")) . '.t'
3094 au FilterReadPost *.out '[,']s/x/X/g
3095 augroup END
3096
3097 e! test.out " Edit the output file
3098 1,$!cat
3099 call assert_equal([
3100 \ 'linE 2 AbcdefghijklmnopqrstuvwXyz',
3101 \ 'linE 3 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
3102 \ 'linE 4 AbcdefghijklmnopqrstuvwXyz',
3103 \ 'linE 5 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
3104 \ 'linE 6 AbcdefghijklmnopqrstuvwXyz',
3105 \ 'linE 7 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
3106 \ 'linE 8 AbcdefghijklmnopqrstuvwXyz',
3107 \ 'linE 9 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
3108 \ 'linE 10 AbcdefghijklmnopqrstuvwXyz'
3109 \ ], getline(1, 9))
3110 call assert_equal([
3111 \ 'line 2 Abcdefghijklmnopqrstuvwxyz',
3112 \ 'line 3 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
3113 \ 'line 4 Abcdefghijklmnopqrstuvwxyz',
3114 \ 'line 5 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
3115 \ 'line 6 Abcdefghijklmnopqrstuvwxyz',
3116 \ 'line 7 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
3117 \ 'line 8 Abcdefghijklmnopqrstuvwxyz',
3118 \ 'line 9 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
3119 \ 'line 10 Abcdefghijklmnopqrstuvwxyz'
3120 \ ], readfile('test.out'))
3121
3122 augroup Test4
3123 au!
3124 augroup END
3125 set shelltemp&vim
3126
3127 " Test for the FileReadPre and FileReadPost autocmds.
3128 augroup Test5
3129 au!
3130 au FileReadPre *.gz exe 'silent !gzip -d ' . shellescape(expand("<afile>"))
3131 au FileReadPre *.gz call rename(expand("<afile>:r"), expand("<afile>"))
3132 au FileReadPost *.gz '[,']s/l/L/
3133 augroup END
3134
3135 new
3136 0r Xtestfile.gz " Read compressed file
3137 call assert_equal([
3138 \ 'Line 2 Abcdefghijklmnopqrstuvwxyz',
3139 \ 'Line 3 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
3140 \ 'Line 4 Abcdefghijklmnopqrstuvwxyz',
3141 \ 'Line 5 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
3142 \ 'Line 6 Abcdefghijklmnopqrstuvwxyz',
3143 \ 'Line 7 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
3144 \ 'Line 8 Abcdefghijklmnopqrstuvwxyz',
3145 \ 'Line 9 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
3146 \ 'Line 10 Abcdefghijklmnopqrstuvwxyz'
3147 \ ], getline(1, 9))
3148 call assert_equal([
3149 \ 'line 2 Abcdefghijklmnopqrstuvwxyz',
3150 \ 'line 3 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
3151 \ 'line 4 Abcdefghijklmnopqrstuvwxyz',
3152 \ 'line 5 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
3153 \ 'line 6 Abcdefghijklmnopqrstuvwxyz',
3154 \ 'line 7 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
3155 \ 'line 8 Abcdefghijklmnopqrstuvwxyz',
3156 \ 'line 9 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
3157 \ 'line 10 Abcdefghijklmnopqrstuvwxyz'
3158 \ ], readfile('Xtestfile.gz'))
3159
3160 augroup Test5
3161 au!
3162 augroup END
3163
3164 au! FileChangedShell
3165 call delete('Xtestfile.gz')
Bram Moolenaar69ea5872019-04-25 20:29:00 +02003166 call delete('test.out')
3167endfunc
Bram Moolenaar23b51392019-05-09 21:38:43 +02003168
3169func Test_throw_in_BufWritePre()
3170 new
3171 call setline(1, ['one', 'two', 'three'])
3172 call assert_false(filereadable('Xthefile'))
3173 augroup throwing
3174 au BufWritePre X* throw 'do not write'
3175 augroup END
3176 try
3177 w Xthefile
3178 catch
3179 let caught = 1
3180 endtry
3181 call assert_equal(1, caught)
3182 call assert_false(filereadable('Xthefile'))
3183
3184 bwipe!
3185 au! throwing
3186endfunc
Bram Moolenaarcadbe1b2019-09-22 21:50:09 +02003187
Bram Moolenaar40fa12a2021-09-22 14:18:13 +02003188func Test_autocmd_in_try_block()
Bram Moolenaar6f14da12022-09-07 21:30:44 +01003189 call mkdir('Xintrydir', 'R')
Bram Moolenaar40fa12a2021-09-22 14:18:13 +02003190 au BufEnter * let g:fname = expand('%')
3191 try
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +01003192 edit Xintrydir/
Bram Moolenaar40fa12a2021-09-22 14:18:13 +02003193 endtry
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +01003194 call assert_match('Xintrydir', g:fname)
Bram Moolenaar40fa12a2021-09-22 14:18:13 +02003195
3196 unlet g:fname
3197 au! BufEnter
Bram Moolenaar40fa12a2021-09-22 14:18:13 +02003198endfunc
3199
Bram Moolenaarcadbe1b2019-09-22 21:50:09 +02003200func Test_autocmd_SafeState()
3201 CheckRunVimInTerminal
3202
3203 let lines =<< trim END
3204 let g:safe = 0
3205 let g:again = ''
3206 au SafeState * let g:safe += 1
3207 au SafeStateAgain * let g:again ..= 'x'
3208 func CallTimer()
3209 call timer_start(10, {id -> execute('let g:again ..= "t"')})
3210 endfunc
3211 END
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01003212 call writefile(lines, 'XSafeState', 'D')
Bram Moolenaarcadbe1b2019-09-22 21:50:09 +02003213 let buf = RunVimInTerminal('-S XSafeState', #{rows: 6})
3214
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01003215 " Sometimes we loop to handle a K_IGNORE, SafeState may be triggered once or
Bram Moolenaar8fb1b472020-02-23 16:16:26 +01003216 " more often.
Bram Moolenaarcadbe1b2019-09-22 21:50:09 +02003217 call term_sendkeys(buf, ":echo g:safe\<CR>")
Bram Moolenaar8fb1b472020-02-23 16:16:26 +01003218 call WaitForAssert({-> assert_match('^\d ', term_getline(buf, 6))}, 1000)
Bram Moolenaarcadbe1b2019-09-22 21:50:09 +02003219
Bram Moolenaar8fb1b472020-02-23 16:16:26 +01003220 " SafeStateAgain should be invoked at least three times
Bram Moolenaarcadbe1b2019-09-22 21:50:09 +02003221 call term_sendkeys(buf, ":echo g:again\<CR>")
Bram Moolenaar8fb1b472020-02-23 16:16:26 +01003222 call WaitForAssert({-> assert_match('^xxx', term_getline(buf, 6))}, 1000)
Bram Moolenaarcadbe1b2019-09-22 21:50:09 +02003223
3224 call term_sendkeys(buf, ":let g:again = ''\<CR>:call CallTimer()\<CR>")
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02003225 call TermWait(buf, 50)
Bram Moolenaar0f6629a2019-09-22 23:24:13 +02003226 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02003227 call TermWait(buf, 50)
Bram Moolenaarcadbe1b2019-09-22 21:50:09 +02003228 call term_sendkeys(buf, ":echo g:again\<CR>")
3229 call WaitForAssert({-> assert_match('xtx', term_getline(buf, 6))}, 1000)
3230
3231 call StopVimInTerminal(buf)
Bram Moolenaarcadbe1b2019-09-22 21:50:09 +02003232endfunc
Bram Moolenaar23324a02019-10-01 17:39:04 +02003233
3234func Test_autocmd_CmdWinEnter()
3235 CheckRunVimInTerminal
Bram Moolenaar21829c52021-01-26 22:42:21 +01003236
Bram Moolenaar23324a02019-10-01 17:39:04 +02003237 let lines =<< trim END
Egor Zvorykin125ffd22021-11-17 14:01:14 +00003238 augroup vimHints | au! | augroup END
Bram Moolenaar23324a02019-10-01 17:39:04 +02003239 let b:dummy_var = 'This is a dummy'
3240 autocmd CmdWinEnter * quit
3241 let winnr = winnr('$')
3242 END
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01003243 let filename = 'XCmdWinEnter'
Bram Moolenaar23324a02019-10-01 17:39:04 +02003244 call writefile(lines, filename)
3245 let buf = RunVimInTerminal('-S '.filename, #{rows: 6})
3246
3247 call term_sendkeys(buf, "q:")
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02003248 call TermWait(buf)
Bram Moolenaar23324a02019-10-01 17:39:04 +02003249 call term_sendkeys(buf, ":echo b:dummy_var\<cr>")
Bram Moolenaar353c3512020-03-15 14:19:26 +01003250 call WaitForAssert({-> assert_match('^This is a dummy', term_getline(buf, 6))}, 2000)
Bram Moolenaar23324a02019-10-01 17:39:04 +02003251 call term_sendkeys(buf, ":echo &buftype\<cr>")
3252 call WaitForAssert({-> assert_notmatch('^nofile', term_getline(buf, 6))}, 1000)
3253 call term_sendkeys(buf, ":echo winnr\<cr>")
3254 call WaitForAssert({-> assert_match('^1', term_getline(buf, 6))}, 1000)
3255
3256 " clean up
3257 call StopVimInTerminal(buf)
3258 call delete(filename)
3259endfunc
Bram Moolenaarec66c412019-10-11 21:19:13 +02003260
3261func Test_autocmd_was_using_freed_memory()
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01003262 CheckFeature quickfix
3263
Bram Moolenaarec66c412019-10-11 21:19:13 +02003264 pedit xx
3265 n x
Bram Moolenaar92bb83e2021-02-03 23:04:46 +01003266 augroup winenter
3267 au WinEnter * if winnr('$') > 2 | quit | endif
3268 augroup END
Bram Moolenaarec66c412019-10-11 21:19:13 +02003269 split
Bram Moolenaar92bb83e2021-02-03 23:04:46 +01003270
3271 augroup winenter
3272 au! WinEnter
3273 augroup END
3274
3275 bwipe xx
3276 bwipe x
3277 pclose
Bram Moolenaarec66c412019-10-11 21:19:13 +02003278endfunc
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +01003279
3280func Test_BufWrite_lockmarks()
Bram Moolenaarf08b0eb2021-10-16 13:00:14 +01003281 let g:test_is_flaky = 1
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +01003282 edit! Xtest
3283 call setline(1, ['a', 'b', 'c', 'd'])
3284
3285 " :lockmarks preserves the marks
3286 call SetChangeMarks(2, 3)
3287 lockmarks write
3288 call assert_equal([2, 3], [line("'["), line("']")])
3289
3290 " *WritePre autocmds get the correct line range, but lockmarks preserves the
3291 " original values for the user
3292 augroup lockmarks
3293 au!
3294 au BufWritePre,FilterWritePre * call assert_equal([1, 4], [line("'["), line("']")])
3295 au FileWritePre * call assert_equal([3, 4], [line("'["), line("']")])
3296 augroup END
3297
3298 lockmarks write
3299 call assert_equal([2, 3], [line("'["), line("']")])
3300
3301 if executable('cat')
3302 lockmarks %!cat
3303 call assert_equal([2, 3], [line("'["), line("']")])
3304 endif
3305
3306 lockmarks 3,4write Xtest2
3307 call assert_equal([2, 3], [line("'["), line("']")])
3308
3309 au! lockmarks
3310 augroup! lockmarks
3311 call delete('Xtest')
3312 call delete('Xtest2')
3313endfunc
Bram Moolenaarce6db022020-01-07 20:11:42 +01003314
3315func Test_FileType_spell()
3316 if !isdirectory('/tmp')
3317 throw "Skipped: requires /tmp directory"
3318 endif
3319
3320 " this was crashing with an invalid free()
3321 setglobal spellfile=/tmp/en.utf-8.add
3322 augroup crash
3323 autocmd!
3324 autocmd BufNewFile,BufReadPost crashfile setf somefiletype
3325 autocmd BufNewFile,BufReadPost crashfile set ft=anotherfiletype
3326 autocmd FileType anotherfiletype setlocal spell
3327 augroup END
3328 func! NoCrash() abort
3329 edit /tmp/crashfile
3330 endfunc
3331 call NoCrash()
3332
3333 au! crash
3334 setglobal spellfile=
3335endfunc
Bram Moolenaarbc2b71d2020-02-17 21:33:30 +01003336
Bram Moolenaaref976322022-09-28 11:48:30 +01003337" this was wiping out the current buffer and using freed memory
3338func Test_SpellFileMissing_bwipe()
3339 next 0
3340 au SpellFileMissing 0 bwipe
3341 call assert_fails('set spell spelllang=0', 'E937:')
3342
3343 au! SpellFileMissing
Bram Moolenaar0a60f792022-11-19 21:18:11 +00003344 set nospell spelllang=en
Bram Moolenaaref976322022-09-28 11:48:30 +01003345 bwipe
3346endfunc
3347
Bram Moolenaar406cd902020-02-18 21:54:41 +01003348" Test closing a window or editing another buffer from a FileChangedRO handler
3349" in a readonly buffer
3350func Test_FileChangedRO_winclose()
Bram Moolenaar62cd26a2020-10-11 20:08:44 +02003351 call test_override('ui_delay', 10)
3352
Bram Moolenaar406cd902020-02-18 21:54:41 +01003353 augroup FileChangedROTest
3354 au!
3355 autocmd FileChangedRO * quit
3356 augroup END
3357 new
3358 set readonly
3359 call assert_fails('normal i', 'E788:')
3360 close
3361 augroup! FileChangedROTest
3362
3363 augroup FileChangedROTest
3364 au!
Bram Moolenaar61abe7d2022-08-30 21:46:08 +01003365 autocmd FileChangedRO * edit Xrofile
Bram Moolenaar406cd902020-02-18 21:54:41 +01003366 augroup END
3367 new
3368 set readonly
3369 call assert_fails('normal i', 'E788:')
3370 close
3371 augroup! FileChangedROTest
Bram Moolenaar62cd26a2020-10-11 20:08:44 +02003372 call test_override('ALL', 0)
Bram Moolenaar406cd902020-02-18 21:54:41 +01003373endfunc
3374
Bram Moolenaar0c81d1b2020-02-22 22:45:55 +01003375func LogACmd()
3376 call add(g:logged, line('$'))
3377endfunc
3378
3379func Test_TermChanged()
Bram Moolenaard28e0b32020-02-22 23:08:52 +01003380 CheckNotGui
3381
Bram Moolenaar0c81d1b2020-02-22 22:45:55 +01003382 enew!
3383 tabnew
3384 call setline(1, ['a', 'b', 'c', 'd'])
3385 $
3386 au TermChanged * call LogACmd()
3387 let g:logged = []
3388 let term_save = &term
3389 set term=xterm
3390 call assert_equal([1, 4], g:logged)
3391
3392 au! TermChanged
3393 let &term = term_save
3394 bwipe!
3395endfunc
3396
Bram Moolenaare3284872020-03-19 13:55:03 +01003397" Test for FileReadCmd autocmd
3398func Test_autocmd_FileReadCmd()
3399 func ReadFileCmd()
3400 call append(line('$'), "v:cmdarg = " .. v:cmdarg)
3401 endfunc
3402 augroup FileReadCmdTest
3403 au!
3404 au FileReadCmd Xtest call ReadFileCmd()
3405 augroup END
3406
3407 new
3408 read ++bin Xtest
3409 read ++nobin Xtest
3410 read ++edit Xtest
3411 read ++bad=keep Xtest
3412 read ++bad=drop Xtest
3413 read ++bad=- Xtest
3414 read ++ff=unix Xtest
3415 read ++ff=dos Xtest
3416 read ++ff=mac Xtest
3417 read ++enc=utf-8 Xtest
3418
3419 call assert_equal(['',
3420 \ 'v:cmdarg = ++bin',
3421 \ 'v:cmdarg = ++nobin',
3422 \ 'v:cmdarg = ++edit',
3423 \ 'v:cmdarg = ++bad=keep',
3424 \ 'v:cmdarg = ++bad=drop',
3425 \ 'v:cmdarg = ++bad=-',
3426 \ 'v:cmdarg = ++ff=unix',
3427 \ 'v:cmdarg = ++ff=dos',
3428 \ 'v:cmdarg = ++ff=mac',
3429 \ 'v:cmdarg = ++enc=utf-8'], getline(1, '$'))
3430
Bram Moolenaar23526d22022-12-05 15:50:41 +00003431 bwipe!
Bram Moolenaare3284872020-03-19 13:55:03 +01003432 augroup FileReadCmdTest
3433 au!
3434 augroup END
3435 delfunc ReadFileCmd
3436endfunc
3437
Bram Moolenaaree4e0c12020-04-06 21:35:05 +02003438" Test for passing invalid arguments to autocmd
3439func Test_autocmd_invalid_args()
3440 " Additional character after * for event
Bram Moolenaar61abe7d2022-08-30 21:46:08 +01003441 call assert_fails('autocmd *a Xinvfile set ff=unix', 'E215:')
Bram Moolenaaree4e0c12020-04-06 21:35:05 +02003442 augroup Test
3443 augroup END
3444 " Invalid autocmd event
Bram Moolenaar61abe7d2022-08-30 21:46:08 +01003445 call assert_fails('autocmd Bufabc Xinvfile set ft=vim', 'E216:')
Bram Moolenaaree4e0c12020-04-06 21:35:05 +02003446 " Invalid autocmd event in a autocmd group
Bram Moolenaar61abe7d2022-08-30 21:46:08 +01003447 call assert_fails('autocmd Test Bufabc Xinvfile set ft=vim', 'E216:')
Bram Moolenaaree4e0c12020-04-06 21:35:05 +02003448 augroup! Test
3449 " Execute all autocmds
3450 call assert_fails('doautocmd * BufEnter', 'E217:')
3451 call assert_fails('augroup! x1a2b3', 'E367:')
3452 call assert_fails('autocmd BufNew <buffer=999> pwd', 'E680:')
Bram Moolenaar531be472020-09-23 22:38:05 +02003453 call assert_fails('autocmd BufNew \) set ff=unix', 'E55:')
Bram Moolenaaree4e0c12020-04-06 21:35:05 +02003454endfunc
3455
3456" Test for deep nesting of autocmds
3457func Test_autocmd_deep_nesting()
Bram Moolenaar61abe7d2022-08-30 21:46:08 +01003458 autocmd BufEnter Xdeepfile doautocmd BufEnter Xdeepfile
3459 call assert_fails('doautocmd BufEnter Xdeepfile', 'E218:')
3460 autocmd! BufEnter Xdeepfile
Bram Moolenaaree4e0c12020-04-06 21:35:05 +02003461endfunc
3462
Bram Moolenaarbe5ee862020-06-10 20:56:58 +02003463" Tests for SigUSR1 autocmd event, which is only available on posix systems.
3464func Test_autocmd_sigusr1()
3465 CheckUnix
Bram Moolenaar0056ca72022-09-23 21:26:39 +01003466 " FIXME: should this work on MacOS M1?
3467 CheckNotMacM1
Bram Moolenaar62cd26a2020-10-11 20:08:44 +02003468 CheckExecutable /bin/kill
Bram Moolenaarbe5ee862020-06-10 20:56:58 +02003469
3470 let g:sigusr1_passed = 0
3471 au SigUSR1 * let g:sigusr1_passed = 1
3472 call system('/bin/kill -s usr1 ' . getpid())
3473 call WaitForAssert({-> assert_true(g:sigusr1_passed)})
3474
3475 au! SigUSR1
3476 unlet g:sigusr1_passed
3477endfunc
3478
Bram Moolenaarb340bae2020-06-15 19:51:56 +02003479" Test for BufReadPre autocmd deleting the file
3480func Test_BufReadPre_delfile()
3481 augroup TestAuCmd
3482 au!
Bram Moolenaare7cda972022-08-29 11:02:59 +01003483 autocmd BufReadPre XbufreadPre call delete('XbufreadPre')
Bram Moolenaarb340bae2020-06-15 19:51:56 +02003484 augroup END
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01003485 call writefile([], 'XbufreadPre', 'D')
Bram Moolenaare7cda972022-08-29 11:02:59 +01003486 call assert_fails('new XbufreadPre', 'E200:')
3487 call assert_equal('XbufreadPre', @%)
Bram Moolenaarb340bae2020-06-15 19:51:56 +02003488 call assert_equal(1, &readonly)
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01003489
Bram Moolenaarb340bae2020-06-15 19:51:56 +02003490 augroup TestAuCmd
3491 au!
3492 augroup END
3493 close!
3494endfunc
3495
3496" Test for BufReadPre autocmd changing the current buffer
3497func Test_BufReadPre_changebuf()
3498 augroup TestAuCmd
3499 au!
Bram Moolenaare7cda972022-08-29 11:02:59 +01003500 autocmd BufReadPre Xchangebuf edit Xsomeotherfile
Bram Moolenaarb340bae2020-06-15 19:51:56 +02003501 augroup END
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01003502 call writefile([], 'Xchangebuf', 'D')
Bram Moolenaare7cda972022-08-29 11:02:59 +01003503 call assert_fails('new Xchangebuf', 'E201:')
Bram Moolenaarb340bae2020-06-15 19:51:56 +02003504 call assert_equal('Xsomeotherfile', @%)
3505 call assert_equal(1, &readonly)
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01003506
Bram Moolenaarb340bae2020-06-15 19:51:56 +02003507 augroup TestAuCmd
3508 au!
3509 augroup END
3510 close!
3511endfunc
3512
3513" Test for BufWipeouti autocmd changing the current buffer when reading a file
3514" in an empty buffer with 'f' flag in 'cpo'
3515func Test_BufDelete_changebuf()
3516 new
3517 augroup TestAuCmd
3518 au!
3519 autocmd BufWipeout * let bufnr = bufadd('somefile') | exe "b " .. bufnr
3520 augroup END
3521 let save_cpo = &cpo
3522 set cpo+=f
Bram Moolenaarb18b4962022-09-02 21:55:50 +01003523 call assert_fails('r Xchangebuf', ['E812:', 'E484:'])
Bram Moolenaarb340bae2020-06-15 19:51:56 +02003524 call assert_equal('somefile', @%)
3525 let &cpo = save_cpo
3526 augroup TestAuCmd
3527 au!
3528 augroup END
3529 close!
3530endfunc
3531
Bram Moolenaar0fe937f2020-06-16 22:42:04 +02003532" Test for the temporary internal window used to execute autocmds
3533func Test_autocmd_window()
3534 %bw!
3535 edit one.txt
3536 tabnew two.txt
Bram Moolenaar41cd8032021-03-13 15:47:56 +01003537 vnew three.txt
3538 tabnew four.txt
3539 tabprevious
Bram Moolenaar0fe937f2020-06-16 22:42:04 +02003540 let g:blist = []
Bram Moolenaar832adf92020-06-25 19:01:36 +02003541 augroup aucmd_win_test1
Bram Moolenaar0fe937f2020-06-16 22:42:04 +02003542 au!
3543 au BufEnter * call add(g:blist, [expand('<afile>'),
3544 \ win_gettype(bufwinnr(expand('<afile>')))])
3545 augroup END
3546
3547 doautoall BufEnter
Bram Moolenaar41cd8032021-03-13 15:47:56 +01003548 call assert_equal([
3549 \ ['one.txt', 'autocmd'],
3550 \ ['two.txt', ''],
3551 \ ['four.txt', 'autocmd'],
3552 \ ['three.txt', ''],
3553 \ ], g:blist)
Bram Moolenaar0fe937f2020-06-16 22:42:04 +02003554
Bram Moolenaar832adf92020-06-25 19:01:36 +02003555 augroup aucmd_win_test1
Bram Moolenaar0fe937f2020-06-16 22:42:04 +02003556 au!
3557 augroup END
Bram Moolenaar832adf92020-06-25 19:01:36 +02003558 augroup! aucmd_win_test1
3559 %bw!
3560endfunc
3561
3562" Test for trying to close the temporary window used for executing an autocmd
3563func Test_close_autocmd_window()
3564 %bw!
3565 edit one.txt
3566 tabnew two.txt
3567 augroup aucmd_win_test2
3568 au!
3569 au BufEnter * if expand('<afile>') == 'one.txt' | 1close | endif
3570 augroup END
3571
3572 call assert_fails('doautoall BufEnter', 'E813:')
3573
3574 augroup aucmd_win_test2
3575 au!
3576 augroup END
3577 augroup! aucmd_win_test2
Bram Moolenaarcf844172020-06-26 19:44:06 +02003578 %bwipe!
3579endfunc
3580
3581" Test for trying to close the tab that has the temporary window for exeucing
3582" an autocmd.
3583func Test_close_autocmd_tab()
3584 edit one.txt
3585 tabnew two.txt
3586 augroup aucmd_win_test
3587 au!
3588 au BufEnter * if expand('<afile>') == 'one.txt' | tabfirst | tabonly | endif
3589 augroup END
3590
3591 call assert_fails('doautoall BufEnter', 'E813:')
3592
3593 tabonly
3594 augroup aucmd_win_test
3595 au!
3596 augroup END
3597 augroup! aucmd_win_test
3598 %bwipe!
Bram Moolenaar0fe937f2020-06-16 22:42:04 +02003599endfunc
3600
Bram Moolenaarcb1956d2022-01-07 15:45:18 +00003601func Test_Visual_doautoall_redraw()
3602 call setline(1, ['a', 'b'])
Bram Moolenaar94722c52023-01-28 19:19:03 +00003603 new
Bram Moolenaarcb1956d2022-01-07 15:45:18 +00003604 wincmd p
3605 call feedkeys("G\<C-V>", 'txn')
3606 autocmd User Explode ++once redraw
3607 doautoall User Explode
3608 %bwipe!
3609endfunc
3610
Bram Moolenaar6bcb8772021-02-03 21:23:29 +01003611" This was using freed memory.
3612func Test_BufNew_arglocal()
3613 arglocal
3614 au BufNew * arglocal
3615 call assert_fails('drop xx', 'E1156:')
3616
3617 au! BufNew
3618endfunc
3619
Bram Moolenaar8ab37572021-02-03 21:56:59 +01003620func Test_autocmd_closes_window()
3621 au BufNew,BufWinLeave * e %e
3622 file yyy
3623 au BufNew,BufWinLeave * ball
Bram Moolenaaraad5f9d2021-02-06 17:30:31 +01003624 n xxx
Bram Moolenaar8ab37572021-02-03 21:56:59 +01003625
Bram Moolenaaraad5f9d2021-02-06 17:30:31 +01003626 %bwipe
Bram Moolenaar8ab37572021-02-03 21:56:59 +01003627 au! BufNew
3628 au! BufWinLeave
3629endfunc
3630
Bram Moolenaar92bb83e2021-02-03 23:04:46 +01003631func Test_autocmd_quit_psearch()
3632 sn aa bb
3633 augroup aucmd_win_test
3634 au!
3635 au BufEnter,BufLeave,BufNew,WinEnter,WinLeave,WinNew * if winnr('$') > 1 | q | endif
3636 augroup END
3637 ps /
3638
3639 augroup aucmd_win_test
3640 au!
3641 augroup END
zeertzjq7851c692022-04-21 11:14:01 +01003642 new
3643 pclose
Bram Moolenaar92bb83e2021-02-03 23:04:46 +01003644endfunc
3645
Bram Moolenaaraad5f9d2021-02-06 17:30:31 +01003646" Fuzzer found some strange combination that caused a crash.
3647func Test_autocmd_normal_mess()
Bram Moolenaardd07c022021-02-07 13:32:46 +01003648 " For unknown reason this hangs on MS-Windows
3649 CheckNotMSWindows
3650
Bram Moolenaaraad5f9d2021-02-06 17:30:31 +01003651 augroup aucmd_normal_test
3652 au BufLeave,BufWinLeave,BufHidden,BufUnload,BufDelete,BufWipeout * norm 7q/qc
3653 augroup END
Bram Moolenaar983d83f2021-02-07 12:12:43 +01003654 call assert_fails('o4', 'E1159')
Bram Moolenaaraad5f9d2021-02-06 17:30:31 +01003655 silent! H
Bram Moolenaar983d83f2021-02-07 12:12:43 +01003656 call assert_fails('e xx', 'E1159')
Bram Moolenaaraad5f9d2021-02-06 17:30:31 +01003657 normal G
3658
3659 augroup aucmd_normal_test
3660 au!
3661 augroup END
3662endfunc
3663
Bram Moolenaar8c6951f2021-02-06 18:08:45 +01003664func Test_autocmd_closing_cmdwin()
Bram Moolenaardd07c022021-02-07 13:32:46 +01003665 " For unknown reason this hangs on MS-Windows
3666 CheckNotMSWindows
3667
Bram Moolenaar8c6951f2021-02-06 18:08:45 +01003668 au BufWinLeave * nested q
3669 call assert_fails("norm 7q?\n", 'E855:')
3670
3671 au! BufWinLeave
3672 new
3673 only
3674endfunc
3675
Bram Moolenaar2c7080b2021-02-06 19:19:42 +01003676func Test_autocmd_vimgrep()
3677 augroup aucmd_vimgrep
Charlie Grovesfef44852022-04-19 16:24:12 +01003678 au QuickfixCmdPre,BufNew,BufReadCmd * sb
zeertzjq7851c692022-04-21 11:14:01 +01003679 au QuickfixCmdPre,BufNew,BufReadCmd * q9
Bram Moolenaar2c7080b2021-02-06 19:19:42 +01003680 augroup END
Bram Moolenaardd07c022021-02-07 13:32:46 +01003681 call assert_fails('lv ?a? foo', 'E926:')
Bram Moolenaar2c7080b2021-02-06 19:19:42 +01003682
3683 augroup aucmd_vimgrep
3684 au!
3685 augroup END
3686endfunc
3687
Bram Moolenaar73b8b0a2021-08-01 14:52:32 +02003688func Test_autocmd_with_block()
3689 augroup block_testing
3690 au BufReadPost *.xml {
3691 setlocal matchpairs+=<:>
3692 /<start
3693 }
Bram Moolenaar63b91732021-08-05 20:40:03 +02003694 au CursorHold * {
3695 autocmd BufReadPre * ++once echo 'one' | echo 'two'
3696 g:gotSafeState = 77
3697 }
Bram Moolenaar73b8b0a2021-08-01 14:52:32 +02003698 augroup END
3699
3700 let expected = "\n--- Autocommands ---\nblock_testing BufRead\n *.xml {^@ setlocal matchpairs+=<:>^@ /<start^@ }"
3701 call assert_equal(expected, execute('au BufReadPost *.xml'))
3702
Bram Moolenaar63b91732021-08-05 20:40:03 +02003703 doautocmd CursorHold
3704 call assert_equal(77, g:gotSafeState)
3705 unlet g:gotSafeState
3706
Bram Moolenaar73b8b0a2021-08-01 14:52:32 +02003707 augroup block_testing
3708 au!
Bram Moolenaar75ebd2a2022-06-03 17:39:46 +01003709 autocmd CursorHold * {
3710 if true
3711 # comment
3712 && true
3713
3714 && true
3715 g:done = 'yes'
3716 endif
3717 }
3718 augroup END
3719 doautocmd CursorHold
3720 call assert_equal('yes', g:done)
3721
3722 unlet g:done
3723 augroup block_testing
3724 au!
Bram Moolenaar73b8b0a2021-08-01 14:52:32 +02003725 augroup END
3726endfunc
3727
Bram Moolenaar6f2465d2022-03-22 18:13:01 +00003728func Test_closing_autocmd_window()
3729 let lines =<< trim END
3730 edit Xa.txt
3731 tabnew Xb.txt
3732 autocmd BufEnter Xa.txt unhide 1
3733 doautoall BufEnter
3734 END
3735 call v9.CheckScriptFailure(lines, 'E814:')
3736 au! BufEnter
Bram Moolenaar6f2465d2022-03-22 18:13:01 +00003737 bwipe Xa.txt
3738 bwipe Xb.txt
3739endfunc
3740
zeertzjq46bdae02023-09-24 23:16:08 +02003741func Test_switch_window_in_autocmd_window()
3742 edit Xa.txt
3743 tabnew Xb.txt
3744 autocmd BufEnter Xa.txt wincmd w
3745 doautoall BufEnter
3746 au! BufEnter
3747 bwipe Xa.txt
3748 call assert_false(bufexists('Xa.txt'))
3749 bwipe Xb.txt
3750 call assert_false(bufexists('Xb.txt'))
3751endfunc
3752
zeertzjq9d956ee2024-04-07 18:16:10 +02003753" Test that using the autocommand window doesn't change current directory.
3754func Test_autocmd_window_cwd()
3755 let saveddir = getcwd()
3756 call mkdir('Xcwd/a/b/c/d', 'pR')
3757
3758 new Xa.txt
3759 tabnew
3760 new Xb.txt
3761
3762 tabprev
3763 cd Xcwd
3764 call assert_match('/Xcwd$', getcwd())
3765 call assert_match('\[global\] .*/Xcwd$', trim(execute('verbose pwd')))
3766
3767 autocmd BufEnter Xb.txt lcd ./a/b/c/d
3768 doautoall BufEnter
3769 au! BufEnter
3770 call assert_match('/Xcwd$', getcwd())
3771 call assert_match('\[global\] .*/Xcwd$', trim(execute('verbose pwd')))
3772
3773 tabnext
3774 cd ./a
3775 tcd ./b
3776 lcd ./c
3777 call assert_match('/Xcwd/a/b/c$', getcwd())
3778 call assert_match('\[window\] .*/Xcwd/a/b/c$', trim(execute('verbose pwd')))
3779
3780 autocmd BufEnter Xa.txt call assert_match('Xcwd/a/b/c$', getcwd())
3781 doautoall BufEnter
3782 au! BufEnter
3783 call assert_match('/Xcwd/a/b/c$', getcwd())
3784 call assert_match('\[window\] .*/Xcwd/a/b/c$', trim(execute('verbose pwd')))
3785 bwipe!
3786 call assert_match('/Xcwd/a/b$', getcwd())
3787 call assert_match('\[tabpage\] .*/Xcwd/a/b$', trim(execute('verbose pwd')))
3788 bwipe!
3789 call assert_match('/Xcwd/a$', getcwd())
3790 call assert_match('\[global\] .*/Xcwd/a$', trim(execute('verbose pwd')))
3791 bwipe!
3792
3793 call chdir(saveddir)
3794endfunc
3795
Bram Moolenaar347538f2022-03-26 16:28:06 +00003796func Test_bufwipeout_changes_window()
3797 " This should not crash, but we don't have any expectations about what
3798 " happens, changing window in BufWipeout has unpredictable results.
3799 tabedit
3800 let g:window_id = win_getid()
3801 topleft new
3802 setlocal bufhidden=wipe
3803 autocmd BufWipeout <buffer> call win_gotoid(g:window_id)
3804 tabprevious
3805 +tabclose
3806
3807 unlet g:window_id
3808 au! BufWipeout
3809 %bwipe!
3810endfunc
3811
zeertzjq021996f2022-04-10 11:44:04 +01003812func Test_v_event_readonly()
3813 autocmd CompleteChanged * let v:event.width = 0
3814 call assert_fails("normal! i\<C-X>\<C-V>", 'E46:')
3815 au! CompleteChanged
3816
3817 autocmd DirChangedPre * let v:event.directory = ''
3818 call assert_fails('cd .', 'E46:')
3819 au! DirChangedPre
3820
3821 autocmd ModeChanged * let v:event.new_mode = ''
3822 call assert_fails('normal! cc', 'E46:')
3823 au! ModeChanged
3824
3825 autocmd TextYankPost * let v:event.operator = ''
3826 call assert_fails('normal! yy', 'E46:')
3827 au! TextYankPost
3828endfunc
3829
zeertzjqc9e8fd62022-07-26 18:12:38 +01003830" Test for ModeChanged pattern
3831func Test_mode_changes()
3832 let g:index = 0
zeertzjq73916ba2023-04-26 16:50:19 +01003833 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 +01003834 func! TestMode()
3835 call assert_equal(g:mode_seq[g:index], get(v:event, "old_mode"))
3836 call assert_equal(g:mode_seq[g:index + 1], get(v:event, "new_mode"))
3837 call assert_equal(mode(1), get(v:event, "new_mode"))
3838 let g:index += 1
3839 endfunc
3840
3841 au ModeChanged * :call TestMode()
3842 let g:n_to_any = 0
3843 au ModeChanged n:* let g:n_to_any += 1
zeertzjq73916ba2023-04-26 16:50:19 +01003844 call feedkeys("i\<esc>vVca\<CR>\<C-X>\<C-L>\<esc>ggdV\<MouseMove>G", 'tnix')
zeertzjqc9e8fd62022-07-26 18:12:38 +01003845
3846 let g:V_to_v = 0
3847 au ModeChanged V:v let g:V_to_v += 1
3848 call feedkeys("Vv\<C-G>\<esc>", 'tnix')
3849 call assert_equal(len(filter(g:mode_seq[1:], {idx, val -> val == 'n'})), g:n_to_any)
3850 call assert_equal(1, g:V_to_v)
3851 call assert_equal(len(g:mode_seq) - 1, g:index)
3852
3853 let g:n_to_i = 0
3854 au ModeChanged n:i let g:n_to_i += 1
3855 let g:n_to_niI = 0
3856 au ModeChanged i:niI let g:n_to_niI += 1
3857 let g:niI_to_i = 0
3858 au ModeChanged niI:i let g:niI_to_i += 1
3859 let g:nany_to_i = 0
3860 au ModeChanged n*:i let g:nany_to_i += 1
3861 let g:i_to_n = 0
3862 au ModeChanged i:n let g:i_to_n += 1
3863 let g:nori_to_any = 0
3864 au ModeChanged [ni]:* let g:nori_to_any += 1
3865 let g:i_to_any = 0
3866 au ModeChanged i:* let g:i_to_any += 1
3867 let g:index = 0
3868 let g:mode_seq = ['n', 'i', 'niI', 'i', 'n']
3869 call feedkeys("a\<C-O>l\<esc>", 'tnix')
3870 call assert_equal(len(g:mode_seq) - 1, g:index)
3871 call assert_equal(1, g:n_to_i)
3872 call assert_equal(1, g:n_to_niI)
3873 call assert_equal(1, g:niI_to_i)
3874 call assert_equal(2, g:nany_to_i)
3875 call assert_equal(1, g:i_to_n)
3876 call assert_equal(2, g:i_to_any)
3877 call assert_equal(3, g:nori_to_any)
3878
3879 if has('terminal')
3880 let g:mode_seq += ['c', 'n', 't', 'nt', 'c', 'nt', 'n']
3881 call feedkeys(":term\<CR>\<C-W>N:bd!\<CR>", 'tnix')
3882 call assert_equal(len(g:mode_seq) - 1, g:index)
3883 call assert_equal(1, g:n_to_i)
3884 call assert_equal(1, g:n_to_niI)
3885 call assert_equal(1, g:niI_to_i)
3886 call assert_equal(2, g:nany_to_i)
3887 call assert_equal(1, g:i_to_n)
3888 call assert_equal(2, g:i_to_any)
3889 call assert_equal(5, g:nori_to_any)
3890 endif
3891
zeertzjqd1955982022-10-05 11:24:46 +01003892 let g:n_to_c = 0
3893 au ModeChanged n:c let g:n_to_c += 1
3894 let g:c_to_n = 0
3895 au ModeChanged c:n let g:c_to_n += 1
3896 let g:mode_seq += ['c', 'n', 'c', 'n']
3897 call feedkeys("q:\<C-C>\<Esc>", 'tnix')
3898 call assert_equal(len(g:mode_seq) - 1, g:index)
3899 call assert_equal(2, g:n_to_c)
3900 call assert_equal(2, g:c_to_n)
zeertzjqc9e8fd62022-07-26 18:12:38 +01003901
Bram Moolenaar61c4b042022-10-18 15:10:11 +01003902 let g:n_to_v = 0
3903 au ModeChanged n:v let g:n_to_v += 1
3904 let g:v_to_n = 0
3905 au ModeChanged v:n let g:v_to_n += 1
3906 let g:mode_seq += ['v', 'n']
3907 call feedkeys("v\<C-C>", 'tnix')
3908 call assert_equal(len(g:mode_seq) - 1, g:index)
3909 call assert_equal(1, g:n_to_v)
3910 call assert_equal(1, g:v_to_n)
zeertzjqfcaeb3d2023-11-28 20:46:29 +01003911
3912 let g:mode_seq += ['c', 'cr', 'c', 'cr', 'n']
3913 call feedkeys(":\<Insert>\<Insert>\<Insert>\<CR>", 'tnix')
3914 call assert_equal(len(g:mode_seq) - 1, g:index)
Bram Moolenaar61c4b042022-10-18 15:10:11 +01003915
zeertzjqc9e8fd62022-07-26 18:12:38 +01003916 au! ModeChanged
3917 delfunc TestMode
3918 unlet! g:mode_seq
3919 unlet! g:index
3920 unlet! g:n_to_any
3921 unlet! g:V_to_v
3922 unlet! g:n_to_i
3923 unlet! g:n_to_niI
3924 unlet! g:niI_to_i
3925 unlet! g:nany_to_i
3926 unlet! g:i_to_n
3927 unlet! g:nori_to_any
3928 unlet! g:i_to_any
zeertzjqfcaeb3d2023-11-28 20:46:29 +01003929 unlet! g:n_to_c
3930 unlet! g:c_to_n
3931 unlet! g:n_to_v
3932 unlet! g:v_to_n
zeertzjqc9e8fd62022-07-26 18:12:38 +01003933endfunc
3934
3935func Test_recursive_ModeChanged()
3936 au! ModeChanged * norm 0u
3937 sil! norm 
3938 au! ModeChanged
3939endfunc
3940
3941func Test_ModeChanged_starts_visual()
3942 " This was triggering ModeChanged before setting VIsual, causing a crash.
3943 au! ModeChanged * norm 0u
3944 sil! norm 
3945
3946 au! ModeChanged
3947endfunc
Bram Moolenaar347538f2022-03-26 16:28:06 +00003948
Charlie Grovesfef44852022-04-19 16:24:12 +01003949func Test_noname_autocmd()
3950 augroup test_noname_autocmd_group
3951 autocmd!
3952 autocmd BufEnter * call add(s:li, ["BufEnter", expand("<afile>")])
3953 autocmd BufDelete * call add(s:li, ["BufDelete", expand("<afile>")])
3954 autocmd BufLeave * call add(s:li, ["BufLeave", expand("<afile>")])
3955 autocmd BufUnload * call add(s:li, ["BufUnload", expand("<afile>")])
3956 autocmd BufWipeout * call add(s:li, ["BufWipeout", expand("<afile>")])
3957 augroup END
3958
3959 let s:li = []
3960 edit foo
3961 call assert_equal([['BufUnload', ''], ['BufDelete', ''], ['BufWipeout', ''], ['BufEnter', 'foo']], s:li)
3962
3963 au! test_noname_autocmd_group
3964 augroup! test_noname_autocmd_group
3965endfunc
3966
Yegappan Lakshmanan1755a912022-05-19 10:31:47 +01003967" Test for the autocmd_get() function
3968func Test_autocmd_get()
3969 augroup TestAutoCmdFns
3970 au!
3971 autocmd BufAdd *.vim echo "bufadd-vim"
3972 autocmd BufAdd *.py echo "bufadd-py"
3973 autocmd BufHidden *.vim echo "bufhidden"
3974 augroup END
3975 augroup TestAutoCmdFns2
3976 autocmd BufAdd *.vim echo "bufadd-vim-2"
3977 autocmd BufRead *.a1b2c3 echo "bufadd-vim-2"
3978 augroup END
3979
3980 let l = autocmd_get()
3981 call assert_true(l->len() > 0)
3982
3983 " Test for getting all the autocmds in a group
3984 let expected = [
3985 \ #{cmd: 'echo "bufadd-vim"', group: 'TestAutoCmdFns',
3986 \ pattern: '*.vim', nested: v:false, once: v:false,
3987 \ event: 'BufAdd'},
3988 \ #{cmd: 'echo "bufadd-py"', group: 'TestAutoCmdFns',
3989 \ pattern: '*.py', nested: v:false, once: v:false,
3990 \ event: 'BufAdd'},
3991 \ #{cmd: 'echo "bufhidden"', group: 'TestAutoCmdFns',
3992 \ pattern: '*.vim', nested: v:false,
3993 \ once: v:false, event: 'BufHidden'}]
3994 call assert_equal(expected, autocmd_get(#{group: 'TestAutoCmdFns'}))
3995
3996 " Test for getting autocmds for all the patterns in a group
3997 call assert_equal(expected, autocmd_get(#{group: 'TestAutoCmdFns',
3998 \ event: '*'}))
3999
4000 " Test for getting autocmds for an event in a group
4001 let expected = [
4002 \ #{cmd: 'echo "bufadd-vim"', group: 'TestAutoCmdFns',
4003 \ pattern: '*.vim', nested: v:false, once: v:false,
4004 \ event: 'BufAdd'},
4005 \ #{cmd: 'echo "bufadd-py"', group: 'TestAutoCmdFns',
4006 \ pattern: '*.py', nested: v:false, once: v:false,
4007 \ event: 'BufAdd'}]
4008 call assert_equal(expected, autocmd_get(#{group: 'TestAutoCmdFns',
4009 \ event: 'BufAdd'}))
4010
4011 " Test for getting the autocmds for all the events in a group for particular
4012 " pattern
4013 call assert_equal([{'cmd': 'echo "bufadd-py"', 'group': 'TestAutoCmdFns',
4014 \ 'pattern': '*.py', 'nested': v:false, 'once': v:false,
4015 \ 'event': 'BufAdd'}],
4016 \ autocmd_get(#{group: 'TestAutoCmdFns', event: '*', pattern: '*.py'}))
4017
4018 " Test for getting the autocmds for an events in a group for particular
4019 " pattern
4020 let l = autocmd_get(#{group: 'TestAutoCmdFns', event: 'BufAdd',
4021 \ pattern: '*.vim'})
4022 call assert_equal([
4023 \ #{cmd: 'echo "bufadd-vim"', group: 'TestAutoCmdFns',
4024 \ pattern: '*.vim', nested: v:false, once: v:false,
4025 \ event: 'BufAdd'}], l)
4026
4027 " Test for getting the autocmds for a pattern in a group
4028 let l = autocmd_get(#{group: 'TestAutoCmdFns', pattern: '*.vim'})
4029 call assert_equal([
4030 \ #{cmd: 'echo "bufadd-vim"', group: 'TestAutoCmdFns',
4031 \ pattern: '*.vim', nested: v:false, once: v:false,
4032 \ event: 'BufAdd'},
4033 \ #{cmd: 'echo "bufhidden"', group: 'TestAutoCmdFns',
4034 \ pattern: '*.vim', nested: v:false,
4035 \ once: v:false, event: 'BufHidden'}], l)
4036
4037 " Test for getting the autocmds for a pattern in all the groups
4038 let l = autocmd_get(#{pattern: '*.a1b2c3'})
4039 call assert_equal([{'cmd': 'echo "bufadd-vim-2"', 'group': 'TestAutoCmdFns2',
4040 \ 'pattern': '*.a1b2c3', 'nested': v:false, 'once': v:false,
4041 \ 'event': 'BufRead'}], l)
4042
4043 " Test for getting autocmds for a pattern without any autocmds
4044 call assert_equal([], autocmd_get(#{group: 'TestAutoCmdFns',
4045 \ pattern: '*.abc'}))
4046 call assert_equal([], autocmd_get(#{group: 'TestAutoCmdFns',
4047 \ event: 'BufAdd', pattern: '*.abc'}))
4048 call assert_equal([], autocmd_get(#{group: 'TestAutoCmdFns',
4049 \ event: 'BufWipeout'}))
zeertzjq2d1d5c62024-06-09 16:44:33 +02004050
4051 " Test for getting autocmds after removing one inside an autocmd
4052 func CheckAutocmdGet()
4053 augroup TestAutoCmdFns
4054 autocmd! BufAdd *.vim
4055 augroup END
4056
4057 let expected = [
4058 \ #{cmd: 'echo "bufadd-py"', group: 'TestAutoCmdFns',
4059 \ pattern: '*.py', nested: v:false, once: v:false,
4060 \ event: 'BufAdd'},
4061 \ #{cmd: 'echo "bufhidden"', group: 'TestAutoCmdFns',
4062 \ pattern: '*.vim', nested: v:false,
4063 \ once: v:false, event: 'BufHidden'}]
4064
4065 call assert_equal(expected, autocmd_get(#{group: 'TestAutoCmdFns'}))
4066 call assert_equal([expected[0]],
4067 \ autocmd_get(#{group: 'TestAutoCmdFns', pattern: '*.py'}))
4068 call assert_equal([expected[1]],
4069 \ autocmd_get(#{group: 'TestAutoCmdFns', pattern: '*.vim'}))
4070 endfunc
4071
4072 autocmd User Xauget call CheckAutocmdGet()
4073 doautocmd User Xauget
4074 autocmd! User Xauget
4075
Yegappan Lakshmanan1755a912022-05-19 10:31:47 +01004076 call assert_fails("call autocmd_get(#{group: 'abc', event: 'BufAdd'})",
4077 \ 'E367:')
4078 let cmd = "echo autocmd_get(#{group: 'TestAutoCmdFns', event: 'abc'})"
4079 call assert_fails(cmd, 'E216:')
4080 call assert_fails("call autocmd_get(#{group: 'abc'})", 'E367:')
4081 call assert_fails("echo autocmd_get(#{event: 'abc'})", 'E216:')
4082
4083 augroup TestAutoCmdFns
4084 au!
4085 augroup END
4086 call assert_equal([], autocmd_get(#{group: 'TestAutoCmdFns'}))
4087
4088 " Test for nested and once autocmds
4089 augroup TestAutoCmdFns
4090 au!
4091 autocmd VimSuspend * ++nested echo "suspend"
4092 autocmd VimResume * ++once echo "resume"
4093 augroup END
4094
4095 let expected = [
4096 \ {'cmd': 'echo "suspend"', 'group': 'TestAutoCmdFns', 'pattern': '*',
4097 \ 'nested': v:true, 'once': v:false, 'event': 'VimSuspend'},
4098 \ {'cmd': 'echo "resume"', 'group': 'TestAutoCmdFns', 'pattern': '*',
4099 \ 'nested': v:false, 'once': v:true, 'event': 'VimResume'}]
4100 call assert_equal(expected, autocmd_get(#{group: 'TestAutoCmdFns'}))
4101
4102 " Test for buffer-local autocmd
4103 augroup TestAutoCmdFns
4104 au!
4105 autocmd TextYankPost <buffer> echo "textyankpost"
4106 augroup END
4107
4108 let expected = [
4109 \ {'cmd': 'echo "textyankpost"', 'group': 'TestAutoCmdFns',
4110 \ 'pattern': '<buffer=' .. bufnr() .. '>', 'nested': v:false,
4111 \ 'once': v:false, 'bufnr': bufnr(), 'event': 'TextYankPost'}]
4112 call assert_equal(expected, autocmd_get(#{group: 'TestAutoCmdFns'}))
4113
4114 augroup TestAutoCmdFns
4115 au!
4116 augroup END
4117 augroup! TestAutoCmdFns
4118 augroup TestAutoCmdFns2
4119 au!
4120 augroup END
4121 augroup! TestAutoCmdFns2
4122
4123 call assert_fails("echo autocmd_get(#{group: []})", 'E730:')
4124 call assert_fails("echo autocmd_get(#{event: {}})", 'E731:')
4125 call assert_fails("echo autocmd_get([])", 'E1206:')
4126endfunc
4127
4128" Test for the autocmd_add() function
4129func Test_autocmd_add()
4130 " Define a single autocmd in a group
4131 call autocmd_add([#{group: 'TestAcSet', event: 'BufAdd', pattern: '*.sh',
4132 \ cmd: 'echo "bufadd"', once: v:true, nested: v:true}])
4133 call assert_equal([#{cmd: 'echo "bufadd"', group: 'TestAcSet',
4134 \ pattern: '*.sh', nested: v:true, once: v:true,
4135 \ event: 'BufAdd'}], autocmd_get(#{group: 'TestAcSet'}))
4136
4137 " Define two autocmds in the same group
4138 call autocmd_delete([#{group: 'TestAcSet'}])
4139 call autocmd_add([#{group: 'TestAcSet', event: 'BufAdd', pattern: '*.sh',
4140 \ cmd: 'echo "bufadd"'},
4141 \ #{group: 'TestAcSet', event: 'BufEnter', pattern: '*.sh',
4142 \ cmd: 'echo "bufenter"'}])
4143 call assert_equal([
4144 \ #{cmd: 'echo "bufadd"', group: 'TestAcSet', pattern: '*.sh',
4145 \ nested: v:false, once: v:false, event: 'BufAdd'},
4146 \ #{cmd: 'echo "bufenter"', group: 'TestAcSet', pattern: '*.sh',
4147 \ nested: v:false, once: v:false, event: 'BufEnter'}],
4148 \ autocmd_get(#{group: 'TestAcSet'}))
4149
4150 " Define a buffer-local autocmd
4151 call autocmd_delete([#{group: 'TestAcSet'}])
4152 call autocmd_add([#{group: 'TestAcSet', event: 'CursorHold',
4153 \ bufnr: bufnr(), cmd: 'echo "cursorhold"'}])
4154 call assert_equal([
4155 \ #{cmd: 'echo "cursorhold"', group: 'TestAcSet',
4156 \ pattern: '<buffer=' .. bufnr() .. '>', nested: v:false,
4157 \ once: v:false, bufnr: bufnr(), event: 'CursorHold'}],
4158 \ autocmd_get(#{group: 'TestAcSet'}))
4159
4160 " Use an invalid buffer number
4161 call autocmd_delete([#{group: 'TestAcSet'}])
4162 call autocmd_add([#{group: 'TestAcSet', event: 'BufEnter',
4163 \ bufnr: -1, cmd: 'echo "bufenter"'}])
4164 let l = [#{group: 'TestAcSet', event: 'BufAdd', bufnr: 9999,
4165 \ cmd: 'echo "bufadd"'}]
4166 call assert_fails("echo autocmd_add(l)", 'E680:')
Yegappan Lakshmanan00e977c2022-06-01 12:31:53 +01004167 let l = [#{group: 'TestAcSet', event: 'BufAdd', bufnr: 9999,
4168 \ pattern: '*.py', cmd: 'echo "bufadd"'}]
4169 call assert_fails("echo autocmd_add(l)", 'E680:')
4170 let l = [#{group: 'TestAcSet', event: 'BufAdd', bufnr: 9999,
4171 \ pattern: ['*.py', '*.c'], cmd: 'echo "bufadd"'}]
4172 call assert_fails("echo autocmd_add(l)", 'E680:')
Yegappan Lakshmanan1755a912022-05-19 10:31:47 +01004173 let l = [#{group: 'TestAcSet', event: 'BufRead', bufnr: [],
4174 \ cmd: 'echo "bufread"'}]
4175 call assert_fails("echo autocmd_add(l)", 'E745:')
4176 call assert_equal([], autocmd_get(#{group: 'TestAcSet'}))
4177
4178 " Add two commands to the same group, event and pattern
4179 call autocmd_delete([#{group: 'TestAcSet'}])
4180 call autocmd_add([#{group: 'TestAcSet', event: 'BufUnload',
4181 \ pattern: 'abc', cmd: 'echo "cmd1"'}])
4182 call autocmd_add([#{group: 'TestAcSet', event: 'BufUnload',
4183 \ pattern: 'abc', cmd: 'echo "cmd2"'}])
4184 call assert_equal([
4185 \ #{cmd: 'echo "cmd1"', group: 'TestAcSet', pattern: 'abc',
4186 \ nested: v:false, once: v:false, event: 'BufUnload'},
4187 \ #{cmd: 'echo "cmd2"', group: 'TestAcSet', pattern: 'abc',
4188 \ nested: v:false, once: v:false, event: 'BufUnload'}],
4189 \ autocmd_get(#{group: 'TestAcSet'}))
4190
4191 " When adding a new autocmd, if the autocmd 'group' is not specified, then
4192 " the current autocmd group should be used.
4193 call autocmd_delete([#{group: 'TestAcSet'}])
4194 augroup TestAcSet
4195 call autocmd_add([#{event: 'BufHidden', pattern: 'abc', cmd: 'echo "abc"'}])
4196 augroup END
4197 call assert_equal([
4198 \ #{cmd: 'echo "abc"', group: 'TestAcSet', pattern: 'abc',
4199 \ nested: v:false, once: v:false, event: 'BufHidden'}],
4200 \ autocmd_get(#{group: 'TestAcSet'}))
4201
Yegappan Lakshmanan971f6822022-05-24 11:40:11 +01004202 " Test for replacing a cmd for an event in a group
4203 call autocmd_delete([#{group: 'TestAcSet'}])
4204 call autocmd_add([#{replace: v:true, group: 'TestAcSet', event: 'BufEnter',
4205 \ pattern: '*.py', cmd: 'echo "bufenter"'}])
4206 call autocmd_add([#{replace: v:true, group: 'TestAcSet', event: 'BufEnter',
4207 \ pattern: '*.py', cmd: 'echo "bufenter"'}])
4208 call assert_equal([
4209 \ #{cmd: 'echo "bufenter"', group: 'TestAcSet', pattern: '*.py',
4210 \ nested: v:false, once: v:false, event: 'BufEnter'}],
4211 \ autocmd_get(#{group: 'TestAcSet'}))
4212
4213 " Test for adding a command for an unsupported autocmd event
Yegappan Lakshmanan1755a912022-05-19 10:31:47 +01004214 let l = [#{group: 'TestAcSet', event: 'abc', pattern: '*.sh',
4215 \ cmd: 'echo "bufadd"'}]
4216 call assert_fails('call autocmd_add(l)', 'E216:')
4217
Yegappan Lakshmanane0ff3a72022-05-27 18:05:33 +01004218 " Test for using a list of events and patterns
4219 call autocmd_delete([#{group: 'TestAcSet'}])
4220 let l = [#{group: 'TestAcSet', event: ['BufEnter', 'BufLeave'],
4221 \ pattern: ['*.py', '*.sh'], cmd: 'echo "bufcmds"'}]
4222 call autocmd_add(l)
4223 call assert_equal([
4224 \ #{cmd: 'echo "bufcmds"', group: 'TestAcSet', pattern: '*.py',
4225 \ nested: v:false, once: v:false, event: 'BufEnter'},
4226 \ #{cmd: 'echo "bufcmds"', group: 'TestAcSet', pattern: '*.sh',
4227 \ nested: v:false, once: v:false, event: 'BufEnter'},
4228 \ #{cmd: 'echo "bufcmds"', group: 'TestAcSet', pattern: '*.py',
4229 \ nested: v:false, once: v:false, event: 'BufLeave'},
4230 \ #{cmd: 'echo "bufcmds"', group: 'TestAcSet', pattern: '*.sh',
4231 \ nested: v:false, once: v:false, event: 'BufLeave'}],
4232 \ autocmd_get(#{group: 'TestAcSet'}))
4233
4234 " Test for invalid values for 'event' item
4235 call autocmd_delete([#{group: 'TestAcSet'}])
4236 let l = [#{group: 'TestAcSet', event: test_null_string(),
4237 \ pattern: "*.py", cmd: 'echo "bufcmds"'}]
4238 call assert_fails('call autocmd_add(l)', 'E928:')
4239 let l = [#{group: 'TestAcSet', event: test_null_list(),
4240 \ pattern: "*.py", cmd: 'echo "bufcmds"'}]
4241 call assert_fails('call autocmd_add(l)', 'E714:')
4242 let l = [#{group: 'TestAcSet', event: {},
4243 \ pattern: "*.py", cmd: 'echo "bufcmds"'}]
4244 call assert_fails('call autocmd_add(l)', 'E777:')
4245 let l = [#{group: 'TestAcSet', event: [{}],
4246 \ pattern: "*.py", cmd: 'echo "bufcmds"'}]
4247 call assert_fails('call autocmd_add(l)', 'E928:')
4248 let l = [#{group: 'TestAcSet', event: [test_null_string()],
4249 \ pattern: "*.py", cmd: 'echo "bufcmds"'}]
4250 call assert_fails('call autocmd_add(l)', 'E928:')
4251 let l = [#{group: 'TestAcSet', event: 'BufEnter,BufLeave',
4252 \ pattern: '*.py', cmd: 'echo "bufcmds"'}]
4253 call assert_fails('call autocmd_add(l)', 'E216:')
4254 let l = [#{group: 'TestAcSet', event: [],
4255 \ pattern: "*.py", cmd: 'echo "bufcmds"'}]
4256 call autocmd_add(l)
4257 let l = [#{group: 'TestAcSet', event: [""],
4258 \ pattern: "*.py", cmd: 'echo "bufcmds"'}]
4259 call assert_fails('call autocmd_add(l)', 'E216:')
4260 let l = [#{group: 'TestAcSet', event: "",
4261 \ pattern: "*.py", cmd: 'echo "bufcmds"'}]
4262 call autocmd_add(l)
4263 call assert_equal([], autocmd_get(#{group: 'TestAcSet'}))
4264
4265 " Test for invalid values for 'pattern' item
4266 let l = [#{group: 'TestAcSet', event: "BufEnter",
4267 \ pattern: test_null_string(), cmd: 'echo "bufcmds"'}]
Yegappan Lakshmanan00e977c2022-06-01 12:31:53 +01004268 call assert_fails('call autocmd_add(l)', 'E928:')
Yegappan Lakshmanane0ff3a72022-05-27 18:05:33 +01004269 let l = [#{group: 'TestAcSet', event: "BufEnter",
4270 \ pattern: test_null_list(), cmd: 'echo "bufcmds"'}]
4271 call assert_fails('call autocmd_add(l)', 'E714:')
4272 let l = [#{group: 'TestAcSet', event: "BufEnter",
4273 \ pattern: {}, cmd: 'echo "bufcmds"'}]
4274 call assert_fails('call autocmd_add(l)', 'E777:')
4275 let l = [#{group: 'TestAcSet', event: "BufEnter",
4276 \ pattern: [{}], cmd: 'echo "bufcmds"'}]
4277 call assert_fails('call autocmd_add(l)', 'E928:')
4278 let l = [#{group: 'TestAcSet', event: "BufEnter",
4279 \ pattern: [test_null_string()], cmd: 'echo "bufcmds"'}]
4280 call assert_fails('call autocmd_add(l)', 'E928:')
4281 let l = [#{group: 'TestAcSet', event: "BufEnter",
4282 \ pattern: [], cmd: 'echo "bufcmds"'}]
4283 call autocmd_add(l)
4284 let l = [#{group: 'TestAcSet', event: "BufEnter",
4285 \ pattern: [""], cmd: 'echo "bufcmds"'}]
4286 call autocmd_add(l)
4287 let l = [#{group: 'TestAcSet', event: "BufEnter",
4288 \ pattern: "", cmd: 'echo "bufcmds"'}]
4289 call autocmd_add(l)
4290 call assert_equal([], autocmd_get(#{group: 'TestAcSet'}))
4291
4292 let l = [#{group: 'TestAcSet', event: 'BufEnter,abc,BufLeave',
4293 \ pattern: '*.py', cmd: 'echo "bufcmds"'}]
4294 call assert_fails('call autocmd_add(l)', 'E216:')
4295
Yegappan Lakshmanan1755a912022-05-19 10:31:47 +01004296 call assert_fails("call autocmd_add({})", 'E1211:')
4297 call assert_equal(v:false, autocmd_add(test_null_list()))
4298 call assert_true(autocmd_add([[]]))
4299 call assert_true(autocmd_add([test_null_dict()]))
4300
4301 augroup TestAcSet
4302 au!
4303 augroup END
4304
4305 call autocmd_add([#{group: 'TestAcSet'}])
4306 call autocmd_add([#{group: 'TestAcSet', event: 'BufAdd'}])
4307 call autocmd_add([#{group: 'TestAcSet', pat: '*.sh'}])
4308 call autocmd_add([#{group: 'TestAcSet', cmd: 'echo "a"'}])
4309 call autocmd_add([#{group: 'TestAcSet', event: 'BufAdd', pat: '*.sh'}])
4310 call autocmd_add([#{group: 'TestAcSet', event: 'BufAdd', cmd: 'echo "a"'}])
4311 call autocmd_add([#{group: 'TestAcSet', pat: '*.sh', cmd: 'echo "a"'}])
4312 call assert_equal([], autocmd_get(#{group: 'TestAcSet'}))
4313
4314 augroup! TestAcSet
4315endfunc
4316
4317" Test for deleting autocmd events and groups
4318func Test_autocmd_delete()
4319 " Delete an event in an autocmd group
4320 augroup TestAcSet
4321 au!
4322 au BufAdd *.sh echo "bufadd"
4323 au BufEnter *.sh echo "bufenter"
4324 augroup END
4325 call autocmd_delete([#{group: 'TestAcSet', event: 'BufAdd'}])
4326 call assert_equal([#{cmd: 'echo "bufenter"', group: 'TestAcSet',
4327 \ pattern: '*.sh', nested: v:false, once: v:false,
4328 \ event: 'BufEnter'}], autocmd_get(#{group: 'TestAcSet'}))
4329
4330 " Delete all the events in an autocmd group
4331 augroup TestAcSet
4332 au BufAdd *.sh echo "bufadd"
4333 augroup END
4334 call autocmd_delete([#{group: 'TestAcSet', event: '*'}])
4335 call assert_equal([], autocmd_get(#{group: 'TestAcSet'}))
4336
4337 " Delete a non-existing autocmd group
4338 call assert_fails("call autocmd_delete([#{group: 'abc'}])", 'E367:')
4339 " Delete a non-existing autocmd event
4340 let l = [#{group: 'TestAcSet', event: 'abc'}]
4341 call assert_fails("call autocmd_delete(l)", 'E216:')
4342 " Delete a non-existing autocmd pattern
4343 let l = [#{group: 'TestAcSet', event: 'BufAdd', pat: 'abc'}]
4344 call assert_true(autocmd_delete(l))
Yegappan Lakshmanan00e977c2022-06-01 12:31:53 +01004345 " Delete an autocmd for a non-existing buffer
4346 let l = [#{event: '*', bufnr: 9999, cmd: 'echo "x"'}]
4347 call assert_fails('call autocmd_delete(l)', 'E680:')
Yegappan Lakshmanan1755a912022-05-19 10:31:47 +01004348
4349 " Delete an autocmd group
4350 augroup TestAcSet
4351 au!
4352 au BufAdd *.sh echo "bufadd"
4353 au BufEnter *.sh echo "bufenter"
4354 augroup END
4355 call autocmd_delete([#{group: 'TestAcSet'}])
4356 call assert_fails("call autocmd_get(#{group: 'TestAcSet'})", 'E367:')
4357
4358 call assert_true(autocmd_delete([[]]))
4359 call assert_true(autocmd_delete([test_null_dict()]))
4360endfunc
4361
Bram Moolenaar8f3c3c62022-10-18 17:05:54 +01004362func Test_autocmd_split_dummy()
4363 " Autocommand trying to split a window containing a dummy buffer.
Bram Moolenaar94722c52023-01-28 19:19:03 +00004364 auto BufReadPre * exe "sbuf " .. expand("<abuf>")
Bram Moolenaar8f3c3c62022-10-18 17:05:54 +01004365 " Avoid the "W11" prompt
4366 au FileChangedShell * let v:fcs_choice = 'reload'
4367 func Xautocmd_changelist()
4368 cal writefile(['Xtestfile2:4:4'], 'Xerr')
4369 edit Xerr
4370 lex 'Xtestfile2:4:4'
4371 endfunc
4372 call Xautocmd_changelist()
Bram Moolenaar53c5c9f2022-10-18 17:25:03 +01004373 " Should get E86, but it doesn't always happen (timing?)
4374 silent! call Xautocmd_changelist()
Bram Moolenaar8f3c3c62022-10-18 17:05:54 +01004375
4376 au! BufReadPre
4377 au! FileChangedShell
4378 delfunc Xautocmd_changelist
4379 bwipe! Xerr
4380 call delete('Xerr')
4381endfunc
4382
Bram Moolenaare76062c2022-11-28 18:51:43 +00004383" This was crashing because there was only one window to execute autocommands
4384" in.
4385func Test_autocmd_nested_setbufvar()
4386 CheckFeature python3
4387
4388 set hidden
4389 edit Xaaa
4390 edit Xbbb
4391 call setline(1, 'bar')
4392 enew
4393 au BufWriteCmd Xbbb ++nested call setbufvar('Xaaa', '&ft', 'foo') | bw! Xaaa
4394 au FileType foo call py3eval('vim.current.buffer.options["cindent"]')
4395 wall
4396
4397 au! BufWriteCmd
4398 au! FileType foo
4399 set nohidden
4400 call delete('Xaaa')
4401 call delete('Xbbb')
4402 %bwipe!
4403endfunc
4404
Christian Brabandt9aee8ec2022-12-16 16:41:23 +00004405func SetupVimTest_shm()
4406 let g:bwe = []
4407 let g:brp = []
4408 set shortmess+=F
zeertzjq657b31f2023-04-15 21:28:02 +01004409 messages clear
Christian Brabandt9aee8ec2022-12-16 16:41:23 +00004410
4411 let dirname='XVimTestSHM'
4412 call mkdir(dirname, 'R')
4413 call writefile(['test'], dirname .. '/1')
4414 call writefile(['test'], dirname .. '/2')
4415 call writefile(['test'], dirname .. '/3')
4416
4417 augroup test
4418 autocmd!
4419 autocmd BufWinEnter * call add(g:bwe, $'BufWinEnter: {expand('<amatch>')}')
4420 autocmd BufReadPost * call add(g:brp, $'BufReadPost: {expand('<amatch>')}')
4421 augroup END
4422
4423 call setqflist([
4424 \ {'filename': dirname .. '/1', 'lnum': 1, 'col': 1, 'text': 'test', 'vcol': 0},
4425 \ {'filename': dirname .. '/2', 'lnum': 1, 'col': 1, 'text': 'test', 'vcol': 0},
4426 \ {'filename': dirname .. '/3', 'lnum': 1, 'col': 1, 'text': 'test', 'vcol': 0}
4427 \ ])
4428 cdo! substitute/test/TEST
4429
4430 " clean up
4431 noa enew!
4432 set shortmess&vim
4433 augroup test
4434 autocmd!
4435 augroup END
4436 augroup! test
4437endfunc
4438
4439func Test_autocmd_shortmess()
4440 CheckNotMSWindows
4441
4442 call SetupVimTest_shm()
4443 let output = execute(':mess')->split('\n')
4444
4445 let info = copy(output)->filter({idx, val -> val =~# '\d of 3'} )
4446 let bytes = copy(output)->filter({idx, val -> val =~# 'bytes'} )
4447
4448 " We test the following here:
4449 " BufReadPost should have been triggered 3 times, once per file
4450 " BufWinEnter should have been triggered 3 times, once per file
4451 " FileInfoMessage should have been shown 3 times, regardless of shm option
4452 " "(x of 3)" message from :cnext has been shown 3 times
4453
4454 call assert_equal(3, g:brp->len())
4455 call assert_equal(3, g:bwe->len())
4456 call assert_equal(3, info->len())
4457 call assert_equal(3, bytes->len())
4458
4459 delfunc SetupVimTest_shm
4460endfunc
Bram Moolenaare76062c2022-11-28 18:51:43 +00004461
Christian Brabandtf0d3d4a2024-02-15 20:15:04 +01004462func Test_autocmd_invalidates_undo_on_textchanged()
4463 CheckRunVimInTerminal
4464 let script =<< trim END
4465 set hidden
4466 " create quickfix list (at least 2 lines to move line)
4467 vimgrep /u/j %
4468
4469 " enter quickfix window
4470 cwindow
4471
4472 " set modifiable
4473 setlocal modifiable
4474
4475 " set autocmd to clear quickfix list
4476
4477 autocmd! TextChanged <buffer> call setqflist([])
4478 " move line
4479 move+1
4480 END
4481 call writefile(script, 'XTest_autocmd_invalidates_undo_on_textchanged', 'D')
4482 let buf = RunVimInTerminal('XTest_autocmd_invalidates_undo_on_textchanged', {'rows': 20})
4483 call term_sendkeys(buf, ":so %\<cr>")
4484 call term_sendkeys(buf, "G")
4485 call WaitForAssert({-> assert_match('^XTest_autocmd_invalidates_undo_on_textchanged\s*$', term_getline(buf, 20))}, 1000)
4486
4487 call StopVimInTerminal(buf)
4488endfunc
4489
Christian Brabandt55f8bba2024-02-28 23:32:00 +01004490func Test_autocmd_creates_new_buffer_on_bufleave()
4491 e a.txt
4492 e b.txt
4493 setlocal bufhidden=wipe
4494 autocmd BufLeave <buffer> diffsplit c.txt
4495 bn
4496 call assert_equal(1, winnr('$'))
4497 call assert_equal('a.txt', bufname('%'))
4498 bw a.txt
4499 bw c.txt
4500endfunc
4501
Colin Kennedye5f22802024-03-26 18:20:16 +01004502" Ensure `expected` was just recently written as a Vim session
4503func s:assert_session_path(expected)
4504 call assert_equal(a:expected, v:this_session)
4505endfunc
4506
4507" Check for `expected` after a session is written to-disk.
4508func s:watch_for_session_path(expected)
4509 execute 'autocmd SessionWritePost * ++once execute "call s:assert_session_path(\"'
4510 \ . a:expected
4511 \ . '\")"'
4512endfunc
4513
4514" Ensure v:this_session gets the full session path, if explicitly stated
4515func Test_explicit_session_absolute_path()
4516 %bwipeout!
4517
4518 let directory = getcwd()
4519
4520 let v:this_session = ""
4521 let name = "some_file.vim"
4522 let expected = fnamemodify(name, ":p")
4523 call s:watch_for_session_path(expected)
4524 execute "mksession! " .. expected
4525
4526 call delete(expected)
4527endfunc
4528
4529" Ensure v:this_session gets the full session path, if explicitly stated
4530func Test_explicit_session_relative_path()
4531 %bwipeout!
4532
4533 let directory = getcwd()
4534
4535 let v:this_session = ""
4536 let name = "some_file.vim"
4537 let expected = fnamemodify(name, ":p")
4538 call s:watch_for_session_path(expected)
4539 execute "mksession! " .. name
4540
4541 call delete(expected)
4542endfunc
4543
4544" Ensure v:this_session gets the full session path, if not specified
4545func Test_implicit_session()
4546 %bwipeout!
4547
4548 let directory = getcwd()
4549
4550 let v:this_session = ""
4551 let expected = fnamemodify("Session.vim", ":p")
4552 call s:watch_for_session_path(expected)
4553 mksession!
4554
4555 call delete(expected)
4556endfunc
4557
Christian Brabandt86032702024-03-31 18:38:09 +02004558" Test TextChangedI and TextChanged
zeertzjqc4226622024-04-03 22:38:07 +02004559func Test_Changed_ChangedI()
zeertzjq8eb75232024-04-01 14:46:20 +02004560 " Run this test in a terminal because it requires running the main loop.
zeertzjqc4226622024-04-03 22:38:07 +02004561 " Don't use CheckRunVimInTerminal as that will skip the test on Windows.
4562 CheckFeature terminal
4563 CheckNotGui
4564 " Starting a terminal to run Vim is always considered flaky.
4565 let g:test_is_flaky = 1
4566
Christian Brabandt86032702024-03-31 18:38:09 +02004567 call writefile(['one', 'two', 'three'], 'XTextChangedI2', 'D')
4568 let before =<< trim END
zeertzjqc4226622024-04-03 22:38:07 +02004569 set ttimeout ttimeoutlen=10
zeertzjq8eb75232024-04-01 14:46:20 +02004570 let [g:autocmd_n, g:autocmd_i] = ['','']
4571
4572 func TextChangedAutocmd(char)
4573 let g:autocmd_{tolower(a:char)} = a:char .. b:changedtick
zeertzjqc4226622024-04-03 22:38:07 +02004574 call writefile([$'{g:autocmd_n},{g:autocmd_i}'], 'XTextChangedI3')
zeertzjq8eb75232024-04-01 14:46:20 +02004575 endfunc
4576
4577 au TextChanged <buffer> :call TextChangedAutocmd('N')
4578 au TextChangedI <buffer> :call TextChangedAutocmd('I')
4579
Christian Brabandt86032702024-03-31 18:38:09 +02004580 nnoremap <CR> o<Esc>
zeertzjq4a653912024-04-04 21:33:36 +02004581 autocmd SafeState * ++once call writefile([''], 'XTextChangedI3')
Christian Brabandt86032702024-03-31 18:38:09 +02004582 END
4583
4584 call writefile(before, 'Xinit', 'D')
zeertzjqc4226622024-04-03 22:38:07 +02004585 let buf = term_start(
4586 \ GetVimCommandCleanTerm() .. '-n -S Xinit XTextChangedI2',
4587 \ {'term_rows': 10})
4588 call assert_equal('running', term_getstatus(buf))
zeertzjq8eb75232024-04-01 14:46:20 +02004589 call WaitForAssert({-> assert_true(filereadable('XTextChangedI3'))})
zeertzjqe9ff79a2024-04-05 20:07:39 +02004590 defer delete('XTextChangedI3')
zeertzjq4a653912024-04-04 21:33:36 +02004591 call WaitForAssert({-> assert_equal([''], readfile('XTextChangedI3'))})
Christian Brabandt86032702024-03-31 18:38:09 +02004592
zeertzjqc4226622024-04-03 22:38:07 +02004593 " TextChanged should trigger if a mapping enters and leaves Insert mode.
4594 call term_sendkeys(buf, "\<CR>")
zeertzjqe9ff79a2024-04-05 20:07:39 +02004595 call WaitForAssert({-> assert_equal('N4,', readfile('XTextChangedI3')->join("\n"))})
zeertzjqc4226622024-04-03 22:38:07 +02004596
4597 call term_sendkeys(buf, "i")
4598 call WaitForAssert({-> assert_match('^-- INSERT --', term_getline(buf, 10))})
zeertzjqe9ff79a2024-04-05 20:07:39 +02004599 call WaitForAssert({-> assert_equal('N4,', readfile('XTextChangedI3')->join("\n"))})
zeertzjqc4226622024-04-03 22:38:07 +02004600 " TextChangedI should trigger if change is done in Insert mode.
4601 call term_sendkeys(buf, "f")
zeertzjqe9ff79a2024-04-05 20:07:39 +02004602 call WaitForAssert({-> assert_equal('N4,I5', readfile('XTextChangedI3')->join("\n"))})
zeertzjqc4226622024-04-03 22:38:07 +02004603 call term_sendkeys(buf, "o")
zeertzjqe9ff79a2024-04-05 20:07:39 +02004604 call WaitForAssert({-> assert_equal('N4,I6', readfile('XTextChangedI3')->join("\n"))})
zeertzjqc4226622024-04-03 22:38:07 +02004605 call term_sendkeys(buf, "o")
zeertzjqe9ff79a2024-04-05 20:07:39 +02004606 call WaitForAssert({-> assert_equal('N4,I7', readfile('XTextChangedI3')->join("\n"))})
zeertzjqc4226622024-04-03 22:38:07 +02004607 " TextChanged shouldn't trigger when leaving Insert mode and TextChangedI
4608 " has been triggered.
4609 call term_sendkeys(buf, "\<Esc>")
4610 call WaitForAssert({-> assert_notmatch('^-- INSERT --', term_getline(buf, 10))})
zeertzjqe9ff79a2024-04-05 20:07:39 +02004611 call WaitForAssert({-> assert_equal('N4,I7', readfile('XTextChangedI3')->join("\n"))})
zeertzjqc4226622024-04-03 22:38:07 +02004612
4613 " TextChanged should trigger if change is done in Normal mode.
4614 call term_sendkeys(buf, "yyp")
zeertzjqe9ff79a2024-04-05 20:07:39 +02004615 call WaitForAssert({-> assert_equal('N8,I7', readfile('XTextChangedI3')->join("\n"))})
zeertzjqc4226622024-04-03 22:38:07 +02004616
4617 " TextChangedI shouldn't trigger if change isn't done in Insert mode.
4618 call term_sendkeys(buf, "i")
4619 call WaitForAssert({-> assert_match('^-- INSERT --', term_getline(buf, 10))})
zeertzjqe9ff79a2024-04-05 20:07:39 +02004620 call WaitForAssert({-> assert_equal('N8,I7', readfile('XTextChangedI3')->join("\n"))})
zeertzjqc4226622024-04-03 22:38:07 +02004621 call term_sendkeys(buf, "\<Esc>")
4622 call WaitForAssert({-> assert_notmatch('^-- INSERT --', term_getline(buf, 10))})
zeertzjqe9ff79a2024-04-05 20:07:39 +02004623 call WaitForAssert({-> assert_equal('N8,I7', readfile('XTextChangedI3')->join("\n"))})
zeertzjqc4226622024-04-03 22:38:07 +02004624
4625 " TextChangedI should trigger if change is a mix of Normal and Insert modes.
4626 func! s:validate_mixed_textchangedi(buf, keys)
4627 let buf = a:buf
4628 call term_sendkeys(buf, "ifoo")
4629 call WaitForAssert({-> assert_match('^-- INSERT --', term_getline(buf, 10))})
4630 call term_sendkeys(buf, "\<Esc>")
4631 call WaitForAssert({-> assert_notmatch('^-- INSERT --', term_getline(buf, 10))})
4632 call term_sendkeys(buf, ":let [g:autocmd_n, g:autocmd_i] = ['', '']\<CR>")
zeertzjqe9ff79a2024-04-05 20:07:39 +02004633 call writefile([], 'XTextChangedI3')
zeertzjqc4226622024-04-03 22:38:07 +02004634 call term_sendkeys(buf, a:keys)
4635 call WaitForAssert({-> assert_match('^-- INSERT --', term_getline(buf, 10))})
zeertzjqe9ff79a2024-04-05 20:07:39 +02004636 call WaitForAssert({-> assert_match('^,I\d\+', readfile('XTextChangedI3')->join("\n"))})
zeertzjqc4226622024-04-03 22:38:07 +02004637 call term_sendkeys(buf, "\<Esc>")
4638 call WaitForAssert({-> assert_notmatch('^-- INSERT --', term_getline(buf, 10))})
zeertzjqe9ff79a2024-04-05 20:07:39 +02004639 call WaitForAssert({-> assert_match('^,I\d\+', readfile('XTextChangedI3')->join("\n"))})
zeertzjqc4226622024-04-03 22:38:07 +02004640 endfunc
4641
4642 call s:validate_mixed_textchangedi(buf, "o")
4643 call s:validate_mixed_textchangedi(buf, "O")
4644 call s:validate_mixed_textchangedi(buf, "ciw")
4645 call s:validate_mixed_textchangedi(buf, "cc")
4646 call s:validate_mixed_textchangedi(buf, "C")
4647 call s:validate_mixed_textchangedi(buf, "s")
4648 call s:validate_mixed_textchangedi(buf, "S")
4649
4650 " clean up
4651 bwipe!
Christian Brabandt86032702024-03-31 18:38:09 +02004652endfunc
4653
zeertzjq5bf6c212024-03-31 18:41:27 +02004654" Test that filetype detection still works when SwapExists autocommand sets
4655" filetype in another buffer.
4656func Test_SwapExists_set_other_buf_filetype()
4657 let lines =<< trim END
4658 set nocompatible directory=.
4659 filetype on
4660
4661 let g:buf = bufnr()
4662 new
4663
4664 func SwapExists()
4665 let v:swapchoice = 'o'
4666 call setbufvar(g:buf, '&filetype', 'text')
4667 endfunc
4668
4669 func SafeState()
4670 edit <script>
4671 redir! > XftSwapExists.out
4672 set readonly? filetype?
4673 redir END
4674 qall!
4675 endfunc
4676
4677 autocmd SwapExists * ++nested call SwapExists()
4678 autocmd SafeState * ++nested ++once call SafeState()
4679 END
4680 call writefile(lines, 'XftSwapExists.vim', 'D')
4681
4682 new XftSwapExists.vim
4683 if RunVim('', '', ' -S XftSwapExists.vim')
4684 call assert_equal(
4685 \ ['', ' readonly', ' filetype=vim'],
4686 \ readfile('XftSwapExists.out'))
4687 call delete('XftSwapExists.out')
4688 endif
4689
4690 bwipe!
4691endfunc
4692
4693" Test that file is not marked as modified when SwapExists autocommand sets
4694" 'modified' in another buffer.
4695func Test_SwapExists_set_other_buf_modified()
4696 let lines =<< trim END
4697 set nocompatible directory=.
4698
4699 let g:buf = bufnr()
4700 new
4701
4702 func SwapExists()
4703 let v:swapchoice = 'o'
4704 call setbufvar(g:buf, '&modified', 1)
4705 endfunc
4706
4707 func SafeState()
4708 edit <script>
4709 redir! > XmodSwapExists.out
4710 set readonly? modified?
4711 redir END
4712 qall!
4713 endfunc
4714
4715 autocmd SwapExists * ++nested call SwapExists()
4716 autocmd SafeState * ++nested ++once call SafeState()
4717 END
4718 call writefile(lines, 'XmodSwapExists.vim', 'D')
4719
4720 new XmodSwapExists.vim
4721 if RunVim('', '', ' -S XmodSwapExists.vim')
4722 call assert_equal(
4723 \ ['', ' readonly', 'nomodified'],
4724 \ readfile('XmodSwapExists.out'))
4725 call delete('XmodSwapExists.out')
4726 endif
4727
4728 bwipe!
4729endfunc
4730
Jaehwang Jungeb80b832024-04-26 18:48:48 +02004731func Test_BufEnter_botline()
4732 set hidden
4733 call writefile(range(10), 'Xxx1', 'D')
4734 call writefile(range(20), 'Xxx2', 'D')
4735 edit Xxx1
4736 edit Xxx2
4737 au BufEnter Xxx1 call assert_true(line('w$') > 1)
4738 edit Xxx1
zeertzjq340643e2024-04-27 11:33:24 +02004739
4740 bwipe! Xxx1
4741 bwipe! Xxx2
Jaehwang Jungeb80b832024-04-26 18:48:48 +02004742 au! BufEnter Xxx1
4743 set hidden&vim
4744endfunc
4745
Bram Moolenaarbc2b71d2020-02-17 21:33:30 +01004746" vim: shiftwidth=2 sts=2 expandtab