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