blob: 24a5f6138f9f80b42a795d04a0651fdebad32012 [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
Christian Brabandtfb3f9692024-08-11 20:09:17 +020017func CleanUpTestAuGroup()
18 augroup testing
19 au!
20 augroup END
21 augroup! testing
22endfunc
23
Bram Moolenaar14735512016-03-26 21:00:08 +010024func Test_vim_did_enter()
25 call assert_false(v:vim_did_enter)
26
27 " This script will never reach the main loop, can't check if v:vim_did_enter
28 " becomes one.
29endfunc
Bram Moolenaar40b1b542016-04-20 20:18:23 +020030
Bram Moolenaar75911162020-07-21 19:44:47 +020031" Test for the CursorHold autocmd
32func Test_CursorHold_autocmd()
33 CheckRunVimInTerminal
Bram Moolenaare1f3ab72022-09-04 21:29:08 +010034 call writefile(['one', 'two', 'three'], 'XoneTwoThree', 'D')
Bram Moolenaar75911162020-07-21 19:44:47 +020035 let before =<< trim END
36 set updatetime=10
Bram Moolenaare7cda972022-08-29 11:02:59 +010037 au CursorHold * call writefile([line('.')], 'XCHoutput', 'a')
Bram Moolenaar75911162020-07-21 19:44:47 +020038 END
Bram Moolenaare1f3ab72022-09-04 21:29:08 +010039 call writefile(before, 'XCHinit', 'D')
Bram Moolenaare7cda972022-08-29 11:02:59 +010040 let buf = RunVimInTerminal('-S XCHinit XoneTwoThree', {})
Bram Moolenaar17f67542020-08-20 18:29:13 +020041 call term_sendkeys(buf, "G")
Bram Moolenaar62cd26a2020-10-11 20:08:44 +020042 call term_wait(buf, 50)
Bram Moolenaar75911162020-07-21 19:44:47 +020043 call term_sendkeys(buf, "gg")
44 call term_wait(buf)
Bram Moolenaare7cda972022-08-29 11:02:59 +010045 call WaitForAssert({-> assert_equal(['1'], readfile('XCHoutput')[-1:-1])})
Bram Moolenaar75911162020-07-21 19:44:47 +020046 call term_sendkeys(buf, "j")
47 call term_wait(buf)
Bram Moolenaare7cda972022-08-29 11:02:59 +010048 call WaitForAssert({-> assert_equal(['1', '2'], readfile('XCHoutput')[-2:-1])})
Bram Moolenaar75911162020-07-21 19:44:47 +020049 call term_sendkeys(buf, "j")
50 call term_wait(buf)
Bram Moolenaare7cda972022-08-29 11:02:59 +010051 call WaitForAssert({-> assert_equal(['1', '2', '3'], readfile('XCHoutput')[-3:-1])})
Bram Moolenaar75911162020-07-21 19:44:47 +020052 call StopVimInTerminal(buf)
53
Bram Moolenaare7cda972022-08-29 11:02:59 +010054 call delete('XCHoutput')
Bram Moolenaar75911162020-07-21 19:44:47 +020055endfunc
56
Bram Moolenaarc67e8922016-05-24 16:07:40 +020057if has('timers')
Bram Moolenaar97b00752019-05-12 13:07:14 +020058
Bram Moolenaarc67e8922016-05-24 16:07:40 +020059 func ExitInsertMode(id)
60 call feedkeys("\<Esc>")
61 endfunc
62
63 func Test_cursorhold_insert()
zeertzjq657b31f2023-04-15 21:28:02 +010064 " depends on timing
65 let g:test_is_flaky = 1
66
Bram Moolenaarf18c4db2016-09-08 22:10:06 +020067 " Need to move the cursor.
68 call feedkeys("ggG", "xt")
69
Bram Moolenaarc67e8922016-05-24 16:07:40 +020070 let g:triggered = 0
71 au CursorHoldI * let g:triggered += 1
72 set updatetime=20
Bram Moolenaar92bb83e2021-02-03 23:04:46 +010073 call timer_start(200, 'ExitInsertMode')
Bram Moolenaarc67e8922016-05-24 16:07:40 +020074 call feedkeys('a', 'x!')
Bram Moolenaar3b014be2022-11-13 17:53:46 +000075 sleep 30m
Bram Moolenaarc67e8922016-05-24 16:07:40 +020076 call assert_equal(1, g:triggered)
Bram Moolenaar26d98212019-01-27 22:32:55 +010077 unlet g:triggered
78 au! CursorHoldI
79 set updatetime&
80 endfunc
81
82 func Test_cursorhold_insert_with_timer_interrupt()
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +020083 CheckFeature job
Bram Moolenaar26d98212019-01-27 22:32:55 +010084 " Need to move the cursor.
85 call feedkeys("ggG", "xt")
86
87 " Confirm the timer invoked in exit_cb of the job doesn't disturb
88 " CursorHoldI event.
89 let g:triggered = 0
90 au CursorHoldI * let g:triggered += 1
Bram Moolenaar62cd26a2020-10-11 20:08:44 +020091 set updatetime=100
Milly4f5681d2024-10-20 11:06:00 +020092 call job_start(has('win32') ? 'cmd /D /c echo:' : 'echo',
Bram Moolenaar62cd26a2020-10-11 20:08:44 +020093 \ {'exit_cb': {-> timer_start(200, 'ExitInsertMode')}})
Bram Moolenaar26d98212019-01-27 22:32:55 +010094 call feedkeys('a', 'x!')
95 call assert_equal(1, g:triggered)
96 unlet g:triggered
Bram Moolenaare99e8442016-07-26 20:43:40 +020097 au! CursorHoldI
Bram Moolenaaraeac9002016-09-06 22:15:08 +020098 set updatetime&
Bram Moolenaarc67e8922016-05-24 16:07:40 +020099 endfunc
100
101 func Test_cursorhold_insert_ctrl_x()
102 let g:triggered = 0
103 au CursorHoldI * let g:triggered += 1
104 set updatetime=20
105 call timer_start(100, 'ExitInsertMode')
106 " CursorHoldI does not trigger after CTRL-X
107 call feedkeys("a\<C-X>", 'x!')
108 call assert_equal(0, g:triggered)
Bram Moolenaar26d98212019-01-27 22:32:55 +0100109 unlet g:triggered
Bram Moolenaare99e8442016-07-26 20:43:40 +0200110 au! CursorHoldI
Bram Moolenaaraeac9002016-09-06 22:15:08 +0200111 set updatetime&
Bram Moolenaarc67e8922016-05-24 16:07:40 +0200112 endfunc
Bram Moolenaar97b00752019-05-12 13:07:14 +0200113
Bram Moolenaar5a9357d2021-10-03 16:22:05 +0100114 func Test_cursorhold_insert_ctrl_g_U()
115 au CursorHoldI * :
116 set updatetime=20
117 new
118 call timer_start(100, { -> feedkeys("\<Left>foo\<Esc>", 't') })
119 call feedkeys("i()\<C-g>U", 'tx!')
120 sleep 200m
121 call assert_equal('(foo)', getline(1))
122 undo
123 call assert_equal('', getline(1))
124
125 bwipe!
126 au! CursorHoldI
127 set updatetime&
128 endfunc
129
Bram Moolenaar97b00752019-05-12 13:07:14 +0200130 func Test_OptionSet_modeline()
131 call test_override('starting', 1)
132 au! OptionSet
133 augroup set_tabstop
134 au OptionSet tabstop call timer_start(1, {-> execute("echo 'Handler called'", "")})
135 augroup END
Bram Moolenaare1f3ab72022-09-04 21:29:08 +0100136 call writefile(['vim: set ts=7 sw=5 :', 'something'], 'XoptionsetModeline', 'D')
Bram Moolenaar97b00752019-05-12 13:07:14 +0200137 set modeline
138 let v:errmsg = ''
139 call assert_fails('split XoptionsetModeline', 'E12:')
140 call assert_equal(7, &ts)
141 call assert_equal('', v:errmsg)
142
143 augroup set_tabstop
144 au!
145 augroup END
146 bwipe!
147 set ts&
Bram Moolenaar97b00752019-05-12 13:07:14 +0200148 call test_override('starting', 0)
149 endfunc
150
151endif "has('timers')
Bram Moolenaar40b1b542016-04-20 20:18:23 +0200152
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200153func Test_bufunload()
Bram Moolenaarc67e8922016-05-24 16:07:40 +0200154 augroup test_bufunload_group
155 autocmd!
156 autocmd BufUnload * call add(s:li, "bufunload")
157 autocmd BufDelete * call add(s:li, "bufdelete")
158 autocmd BufWipeout * call add(s:li, "bufwipeout")
159 augroup END
Bram Moolenaar40b1b542016-04-20 20:18:23 +0200160
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100161 let s:li = []
Bram Moolenaarc67e8922016-05-24 16:07:40 +0200162 new
163 setlocal bufhidden=
164 bunload
165 call assert_equal(["bufunload", "bufdelete"], s:li)
Bram Moolenaar40b1b542016-04-20 20:18:23 +0200166
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100167 let s:li = []
Bram Moolenaarc67e8922016-05-24 16:07:40 +0200168 new
169 setlocal bufhidden=delete
170 bunload
171 call assert_equal(["bufunload", "bufdelete"], s:li)
172
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100173 let s:li = []
Bram Moolenaarc67e8922016-05-24 16:07:40 +0200174 new
175 setlocal bufhidden=unload
176 bwipeout
177 call assert_equal(["bufunload", "bufdelete", "bufwipeout"], s:li)
178
Bram Moolenaare99e8442016-07-26 20:43:40 +0200179 au! test_bufunload_group
Bram Moolenaarc67e8922016-05-24 16:07:40 +0200180 augroup! test_bufunload_group
Bram Moolenaar40b1b542016-04-20 20:18:23 +0200181endfunc
Bram Moolenaar30445cb2016-07-09 15:21:02 +0200182
183" SEGV occurs in older versions. (At least 7.4.2005 or older)
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200184func Test_autocmd_bufunload_with_tabnext()
Bram Moolenaar30445cb2016-07-09 15:21:02 +0200185 tabedit
186 tabfirst
187
188 augroup test_autocmd_bufunload_with_tabnext_group
189 autocmd!
190 autocmd BufUnload <buffer> tabnext
191 augroup END
192
193 quit
194 call assert_equal(2, tabpagenr('$'))
195
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200196 autocmd! test_autocmd_bufunload_with_tabnext_group
Bram Moolenaar30445cb2016-07-09 15:21:02 +0200197 augroup! test_autocmd_bufunload_with_tabnext_group
198 tablast
199 quit
200endfunc
Bram Moolenaarc917da42016-07-19 22:31:36 +0200201
Bram Moolenaar5ed58c72021-01-28 14:24:55 +0100202func Test_argdelete_in_next()
203 au BufNew,BufEnter,BufLeave,BufWinEnter * argdel
204 call assert_fails('next a b', 'E1156:')
205 au! BufNew,BufEnter,BufLeave,BufWinEnter *
206endfunc
207
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200208func Test_autocmd_bufwinleave_with_tabfirst()
Bram Moolenaarf9e687e2016-09-04 21:33:09 +0200209 tabedit
210 augroup sample
211 autocmd!
212 autocmd BufWinLeave <buffer> tabfirst
213 augroup END
214 call setline(1, ['a', 'b', 'c'])
215 edit! a.txt
Bram Moolenaarf18c4db2016-09-08 22:10:06 +0200216 tabclose
Bram Moolenaarf9e687e2016-09-04 21:33:09 +0200217endfunc
218
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200219" SEGV occurs in older versions. (At least 7.4.2321 or older)
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200220func Test_autocmd_bufunload_avoiding_SEGV_01()
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200221 split aa.txt
222 let lastbuf = bufnr('$')
223
224 augroup test_autocmd_bufunload
225 autocmd!
226 exe 'autocmd BufUnload <buffer> ' . (lastbuf + 1) . 'bwipeout!'
227 augroup END
228
Bram Moolenaar28ee8922020-10-28 20:20:00 +0100229 call assert_fails('edit bb.txt', 'E937:')
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200230
231 autocmd! test_autocmd_bufunload
232 augroup! test_autocmd_bufunload
233 bwipe! aa.txt
234 bwipe! bb.txt
235endfunc
236
237" SEGV occurs in older versions. (At least 7.4.2321 or older)
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200238func Test_autocmd_bufunload_avoiding_SEGV_02()
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200239 setlocal buftype=nowrite
240 let lastbuf = bufnr('$')
241
242 augroup test_autocmd_bufunload
243 autocmd!
244 exe 'autocmd BufUnload <buffer> ' . (lastbuf + 1) . 'bwipeout!'
245 augroup END
246
247 normal! i1
248 call assert_fails('edit a.txt', 'E517:')
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200249
250 autocmd! test_autocmd_bufunload
251 augroup! test_autocmd_bufunload
252 bwipe! a.txt
253endfunc
254
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100255func Test_autocmd_dummy_wipeout()
256 " prepare files
Bram Moolenaare1f3ab72022-09-04 21:29:08 +0100257 call writefile([''], 'Xdummywipetest1.txt', 'D')
258 call writefile([''], 'Xdummywipetest2.txt', 'D')
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100259 augroup test_bufunload_group
260 autocmd!
261 autocmd BufUnload * call add(s:li, "bufunload")
262 autocmd BufDelete * call add(s:li, "bufdelete")
263 autocmd BufWipeout * call add(s:li, "bufwipeout")
264 augroup END
265
266 let s:li = []
267 split Xdummywipetest1.txt
268 silent! vimgrep /notmatched/ Xdummywipetest*
269 call assert_equal(["bufunload", "bufwipeout"], s:li)
270
271 bwipeout
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100272 au! test_bufunload_group
273 augroup! test_bufunload_group
274endfunc
275
Bram Moolenaarc917da42016-07-19 22:31:36 +0200276func Test_win_tab_autocmd()
277 let g:record = []
278
Christian Brabandtfb3f9692024-08-11 20:09:17 +0200279 defer CleanUpTestAuGroup()
Bram Moolenaarc917da42016-07-19 22:31:36 +0200280 augroup testing
Sergey Vlasov1f47db72024-01-25 23:07:00 +0100281 au WinNewPre * call add(g:record, 'WinNewPre')
Bram Moolenaarc917da42016-07-19 22:31:36 +0200282 au WinNew * call add(g:record, 'WinNew')
naohiro ono23beefe2021-11-13 12:38:49 +0000283 au WinClosed * call add(g:record, 'WinClosed')
Bram Moolenaar94722c52023-01-28 19:19:03 +0000284 au WinEnter * call add(g:record, 'WinEnter')
285 au WinLeave * call add(g:record, 'WinLeave')
Bram Moolenaarc917da42016-07-19 22:31:36 +0200286 au TabNew * call add(g:record, 'TabNew')
Bram Moolenaar12c11d52016-07-19 23:13:03 +0200287 au TabClosed * call add(g:record, 'TabClosed')
Bram Moolenaarc917da42016-07-19 22:31:36 +0200288 au TabEnter * call add(g:record, 'TabEnter')
289 au TabLeave * call add(g:record, 'TabLeave')
290 augroup END
291
292 split
293 tabnew
294 close
295 close
296
297 call assert_equal([
Sergey Vlasov1f47db72024-01-25 23:07:00 +0100298 \ 'WinNewPre', 'WinLeave', 'WinNew', 'WinEnter',
Christian Brabandtfb3f9692024-08-11 20:09:17 +0200299 \ 'WinLeave', 'TabLeave', 'WinNew', 'WinEnter', 'TabNew', 'TabEnter',
naohiro ono23beefe2021-11-13 12:38:49 +0000300 \ 'WinLeave', 'TabLeave', 'WinClosed', 'TabClosed', 'WinEnter', 'TabEnter',
301 \ 'WinLeave', 'WinClosed', 'WinEnter'
Bram Moolenaarc917da42016-07-19 22:31:36 +0200302 \ ], g:record)
303
Bram Moolenaar12c11d52016-07-19 23:13:03 +0200304 let g:record = []
305 tabnew somefile
306 tabnext
307 bwipe somefile
308
309 call assert_equal([
Christian Brabandtfb3f9692024-08-11 20:09:17 +0200310 \ 'WinLeave', 'TabLeave', 'WinNew', 'WinEnter', 'TabNew', 'TabEnter',
Bram Moolenaar12c11d52016-07-19 23:13:03 +0200311 \ 'WinLeave', 'TabLeave', 'WinEnter', 'TabEnter',
naohiro ono23beefe2021-11-13 12:38:49 +0000312 \ 'WinClosed', 'TabClosed'
Bram Moolenaar12c11d52016-07-19 23:13:03 +0200313 \ ], g:record)
314
Sergey Vlasov1f47db72024-01-25 23:07:00 +0100315 let g:record = []
316 copen
317 help
318 tabnext
319 vnew
320
321 call assert_equal([
322 \ 'WinNewPre', 'WinLeave', 'WinNew', 'WinEnter',
323 \ 'WinNewPre', 'WinLeave', 'WinNew', 'WinEnter',
324 \ 'WinNewPre', 'WinLeave', 'WinNew', 'WinEnter'
325 \ ], g:record)
326
Bram Moolenaarc917da42016-07-19 22:31:36 +0200327 unlet g:record
328endfunc
Bram Moolenaare99e8442016-07-26 20:43:40 +0200329
Sergey Vlasov1f47db72024-01-25 23:07:00 +0100330func Test_WinNewPre()
331 " Test that the old window layout can be accessed before a new window is created.
332 let g:layouts_pre = []
333 let g:layouts_post = []
334 augroup testing
335 au WinNewPre * call add(g:layouts_pre, winlayout())
336 au WinNew * call add(g:layouts_post, winlayout())
337 augroup END
Christian Brabandtfb3f9692024-08-11 20:09:17 +0200338 defer CleanUpTestAuGroup()
Sergey Vlasov1f47db72024-01-25 23:07:00 +0100339 split
340 call assert_notequal(g:layouts_pre[0], g:layouts_post[0])
341 split
342 call assert_equal(g:layouts_pre[1], g:layouts_post[0])
343 call assert_notequal(g:layouts_pre[1], g:layouts_post[1])
Christian Brabandtfb3f9692024-08-11 20:09:17 +0200344 " not triggered for tabnew
Sergey Vlasov1f47db72024-01-25 23:07:00 +0100345 tabnew
Christian Brabandtfb3f9692024-08-11 20:09:17 +0200346 call assert_equal(2, len(g:layouts_pre))
Sergey Vlasov1f47db72024-01-25 23:07:00 +0100347 unlet g:layouts_pre
348 unlet g:layouts_post
349
350 " Test modifying window layout during WinNewPre throws.
351 let g:caught = 0
352 augroup testing
353 au!
354 au WinNewPre * split
355 augroup END
356 try
357 vnew
358 catch
359 let g:caught += 1
360 endtry
361 augroup testing
362 au!
363 au WinNewPre * tabnew
364 augroup END
365 try
366 vnew
367 catch
368 let g:caught += 1
369 endtry
370 augroup testing
371 au!
372 au WinNewPre * close
373 augroup END
374 try
375 vnew
376 catch
377 let g:caught += 1
378 endtry
379 augroup testing
380 au!
381 au WinNewPre * tabclose
382 augroup END
383 try
384 vnew
385 catch
386 let g:caught += 1
387 endtry
388 call assert_equal(4, g:caught)
Sergey Vlasov1f47db72024-01-25 23:07:00 +0100389 unlet g:caught
390endfunc
391
Bram Moolenaar35fc61c2022-11-22 12:40:50 +0000392func Test_WinResized()
393 CheckRunVimInTerminal
394
395 let lines =<< trim END
396 set scrolloff=0
397 call setline(1, ['111', '222'])
398 vnew
399 call setline(1, ['aaa', 'bbb'])
400 new
401 call setline(1, ['foo', 'bar'])
402
403 let g:resized = 0
404 au WinResized * let g:resized += 1
405
406 func WriteResizedEvent()
407 call writefile([json_encode(v:event)], 'XresizeEvent')
408 endfunc
409 au WinResized * call WriteResizedEvent()
410 END
411 call writefile(lines, 'Xtest_winresized', 'D')
412 let buf = RunVimInTerminal('-S Xtest_winresized', {'rows': 10})
413
414 " redraw now to avoid a redraw after the :echo command
415 call term_sendkeys(buf, ":redraw!\<CR>")
416 call TermWait(buf)
417
418 call term_sendkeys(buf, ":echo g:resized\<CR>")
419 call WaitForAssert({-> assert_match('^0$', term_getline(buf, 10))}, 1000)
420
421 " increase window height, two windows will be reported
422 call term_sendkeys(buf, "\<C-W>+")
423 call TermWait(buf)
424 call term_sendkeys(buf, ":echo g:resized\<CR>")
425 call WaitForAssert({-> assert_match('^1$', term_getline(buf, 10))}, 1000)
426
427 let event = readfile('XresizeEvent')[0]->json_decode()
428 call assert_equal({
429 \ 'windows': [1002, 1001],
430 \ }, event)
431
432 " increase window width, three windows will be reported
433 call term_sendkeys(buf, "\<C-W>>")
434 call TermWait(buf)
435 call term_sendkeys(buf, ":echo g:resized\<CR>")
436 call WaitForAssert({-> assert_match('^2$', term_getline(buf, 10))}, 1000)
437
438 let event = readfile('XresizeEvent')[0]->json_decode()
439 call assert_equal({
440 \ 'windows': [1002, 1001, 1000],
441 \ }, event)
442
443 call delete('XresizeEvent')
444 call StopVimInTerminal(buf)
445endfunc
446
LemonBoy09371822022-04-08 15:18:45 +0100447func Test_WinScrolled()
448 CheckRunVimInTerminal
449
450 let lines =<< trim END
zeertzjqd58862d2022-04-12 11:32:48 +0100451 set nowrap scrolloff=0
452 for ii in range(1, 18)
453 call setline(ii, repeat(nr2char(96 + ii), ii * 2))
454 endfor
455 let win_id = win_getid()
456 let g:matched = v:false
Bram Moolenaar35fc61c2022-11-22 12:40:50 +0000457 func WriteScrollEvent()
458 call writefile([json_encode(v:event)], 'XscrollEvent')
459 endfunc
zeertzjqd58862d2022-04-12 11:32:48 +0100460 execute 'au WinScrolled' win_id 'let g:matched = v:true'
461 let g:scrolled = 0
462 au WinScrolled * let g:scrolled += 1
463 au WinScrolled * let g:amatch = str2nr(expand('<amatch>'))
464 au WinScrolled * let g:afile = str2nr(expand('<afile>'))
Bram Moolenaar35fc61c2022-11-22 12:40:50 +0000465 au WinScrolled * call WriteScrollEvent()
LemonBoy09371822022-04-08 15:18:45 +0100466 END
Bram Moolenaare1f3ab72022-09-04 21:29:08 +0100467 call writefile(lines, 'Xtest_winscrolled', 'D')
LemonBoy09371822022-04-08 15:18:45 +0100468 let buf = RunVimInTerminal('-S Xtest_winscrolled', {'rows': 6})
469
470 call term_sendkeys(buf, ":echo g:scrolled\<CR>")
471 call WaitForAssert({-> assert_match('^0 ', term_getline(buf, 6))}, 1000)
472
473 " Scroll left/right in Normal mode.
474 call term_sendkeys(buf, "zlzh:echo g:scrolled\<CR>")
475 call WaitForAssert({-> assert_match('^2 ', term_getline(buf, 6))}, 1000)
476
Bram Moolenaar35fc61c2022-11-22 12:40:50 +0000477 let event = readfile('XscrollEvent')[0]->json_decode()
478 call assert_equal({
zeertzjq3fc84dc2022-12-07 09:17:59 +0000479 \ 'all': {'leftcol': 1, 'topline': 0, 'topfill': 0, 'width': 0, 'height': 0, 'skipcol': 0},
480 \ '1000': {'leftcol': -1, 'topline': 0, 'topfill': 0, 'width': 0, 'height': 0, 'skipcol': 0}
Bram Moolenaar35fc61c2022-11-22 12:40:50 +0000481 \ }, event)
482
LemonBoy09371822022-04-08 15:18:45 +0100483 " Scroll up/down in Normal mode.
484 call term_sendkeys(buf, "\<c-e>\<c-y>:echo g:scrolled\<CR>")
485 call WaitForAssert({-> assert_match('^4 ', term_getline(buf, 6))}, 1000)
486
Bram Moolenaar35fc61c2022-11-22 12:40:50 +0000487 let event = readfile('XscrollEvent')[0]->json_decode()
488 call assert_equal({
zeertzjq3fc84dc2022-12-07 09:17:59 +0000489 \ 'all': {'leftcol': 0, 'topline': 1, 'topfill': 0, 'width': 0, 'height': 0, 'skipcol': 0},
490 \ '1000': {'leftcol': 0, 'topline': -1, 'topfill': 0, 'width': 0, 'height': 0, 'skipcol': 0}
Bram Moolenaar35fc61c2022-11-22 12:40:50 +0000491 \ }, event)
492
LemonBoy09371822022-04-08 15:18:45 +0100493 " Scroll up/down in Insert mode.
494 call term_sendkeys(buf, "Mi\<c-x>\<c-e>\<Esc>i\<c-x>\<c-y>\<Esc>")
495 call term_sendkeys(buf, ":echo g:scrolled\<CR>")
496 call WaitForAssert({-> assert_match('^6 ', term_getline(buf, 6))}, 1000)
497
Bram Moolenaar35fc61c2022-11-22 12:40:50 +0000498 let event = readfile('XscrollEvent')[0]->json_decode()
499 call assert_equal({
zeertzjq3fc84dc2022-12-07 09:17:59 +0000500 \ 'all': {'leftcol': 0, 'topline': 1, 'topfill': 0, 'width': 0, 'height': 0, 'skipcol': 0},
501 \ '1000': {'leftcol': 0, 'topline': -1, 'topfill': 0, 'width': 0, 'height': 0, 'skipcol': 0}
Bram Moolenaar35fc61c2022-11-22 12:40:50 +0000502 \ }, event)
503
LemonBoy09371822022-04-08 15:18:45 +0100504 " Scroll the window horizontally to focus the last letter of the third line
505 " containing only six characters. Moving to the previous and shorter lines
506 " should trigger another autocommand as Vim has to make them visible.
507 call term_sendkeys(buf, "5zl2k")
508 call term_sendkeys(buf, ":echo g:scrolled\<CR>")
509 call WaitForAssert({-> assert_match('^8 ', term_getline(buf, 6))}, 1000)
510
Bram Moolenaar35fc61c2022-11-22 12:40:50 +0000511 let event = readfile('XscrollEvent')[0]->json_decode()
512 call assert_equal({
zeertzjq3fc84dc2022-12-07 09:17:59 +0000513 \ 'all': {'leftcol': 5, 'topline': 0, 'topfill': 0, 'width': 0, 'height': 0, 'skipcol': 0},
514 \ '1000': {'leftcol': -5, 'topline': 0, 'topfill': 0, 'width': 0, 'height': 0, 'skipcol': 0}
Bram Moolenaar35fc61c2022-11-22 12:40:50 +0000515 \ }, event)
516
LemonBoy09371822022-04-08 15:18:45 +0100517 " Ensure the command was triggered for the specified window ID.
518 call term_sendkeys(buf, ":echo g:matched\<CR>")
519 call WaitForAssert({-> assert_match('^v:true ', term_getline(buf, 6))}, 1000)
520
521 " Ensure the expansion of <amatch> and <afile> matches the window ID.
522 call term_sendkeys(buf, ":echo g:amatch == win_id && g:afile == win_id\<CR>")
523 call WaitForAssert({-> assert_match('^v:true ', term_getline(buf, 6))}, 1000)
524
Bram Moolenaar35fc61c2022-11-22 12:40:50 +0000525 call delete('XscrollEvent')
LemonBoy09371822022-04-08 15:18:45 +0100526 call StopVimInTerminal(buf)
LemonBoy09371822022-04-08 15:18:45 +0100527endfunc
528
LemonBoy66e13ae2022-04-21 22:52:11 +0100529func Test_WinScrolled_mouse()
530 CheckRunVimInTerminal
531
532 let lines =<< trim END
533 set nowrap scrolloff=0
534 set mouse=a term=xterm ttymouse=sgr mousetime=200 clipboard=
535 call setline(1, ['foo']->repeat(32))
536 split
537 let g:scrolled = 0
538 au WinScrolled * let g:scrolled += 1
539 END
Bram Moolenaare1f3ab72022-09-04 21:29:08 +0100540 call writefile(lines, 'Xtest_winscrolled_mouse', 'D')
LemonBoy66e13ae2022-04-21 22:52:11 +0100541 let buf = RunVimInTerminal('-S Xtest_winscrolled_mouse', {'rows': 10})
542
543 " With the upper split focused, send a scroll-down event to the unfocused one.
544 call test_setmouse(7, 1)
545 call term_sendkeys(buf, "\<ScrollWheelDown>")
546 call TermWait(buf)
547 call term_sendkeys(buf, ":echo g:scrolled\<CR>")
548 call WaitForAssert({-> assert_match('^1', term_getline(buf, 10))}, 1000)
549
550 " Again, but this time while we're in insert mode.
551 call term_sendkeys(buf, "i\<ScrollWheelDown>\<Esc>")
552 call TermWait(buf)
553 call term_sendkeys(buf, ":echo g:scrolled\<CR>")
554 call WaitForAssert({-> assert_match('^2', term_getline(buf, 10))}, 1000)
555
556 call StopVimInTerminal(buf)
LemonBoy66e13ae2022-04-21 22:52:11 +0100557endfunc
558
zeertzjqd58862d2022-04-12 11:32:48 +0100559func Test_WinScrolled_close_curwin()
560 CheckRunVimInTerminal
561
562 let lines =<< trim END
563 set nowrap scrolloff=0
564 call setline(1, ['aaa', 'bbb'])
565 vsplit
566 au WinScrolled * close
567 au VimLeave * call writefile(['123456'], 'Xtestout')
568 END
Bram Moolenaare1f3ab72022-09-04 21:29:08 +0100569 call writefile(lines, 'Xtest_winscrolled_close_curwin', 'D')
zeertzjqd58862d2022-04-12 11:32:48 +0100570 let buf = RunVimInTerminal('-S Xtest_winscrolled_close_curwin', {'rows': 6})
571
572 " This was using freed memory
573 call term_sendkeys(buf, "\<C-E>")
574 call TermWait(buf)
575 call StopVimInTerminal(buf)
576
Bram Moolenaar0a60f792022-11-19 21:18:11 +0000577 " check the startup script finished to the end
zeertzjqd58862d2022-04-12 11:32:48 +0100578 call assert_equal(['123456'], readfile('Xtestout'))
zeertzjqd58862d2022-04-12 11:32:48 +0100579 call delete('Xtestout')
580endfunc
581
Bram Moolenaar0a60f792022-11-19 21:18:11 +0000582func Test_WinScrolled_once_only()
583 CheckRunVimInTerminal
584
585 let lines =<< trim END
586 set cmdheight=2
587 call setline(1, ['aaa', 'bbb'])
588 let trigger_count = 0
589 func ShowInfo(id)
590 echo g:trigger_count g:winid winlayout()
591 endfunc
592
593 vsplit
594 split
595 " use a timer to show the info after a redraw
596 au WinScrolled * let trigger_count += 1 | let winid = expand('<amatch>') | call timer_start(100, 'ShowInfo')
597 wincmd j
598 wincmd l
599 END
600 call writefile(lines, 'Xtest_winscrolled_once', 'D')
601 let buf = RunVimInTerminal('-S Xtest_winscrolled_once', #{rows: 10, cols: 60, statusoff: 2})
602
603 call term_sendkeys(buf, "\<C-E>")
604 call VerifyScreenDump(buf, 'Test_winscrolled_once_only_1', {})
605
606 call StopVimInTerminal(buf)
607endfunc
608
Bram Moolenaar29967732022-11-20 12:11:45 +0000609" Check that WinScrolled is not triggered immediately when defined and there
610" are split windows.
611func Test_WinScrolled_not_when_defined()
612 CheckRunVimInTerminal
613
614 let lines =<< trim END
615 call setline(1, ['aaa', 'bbb'])
616 echo 'nothing happened'
617 func ShowTriggered(id)
618 echo 'triggered'
619 endfunc
620 END
621 call writefile(lines, 'Xtest_winscrolled_not', 'D')
622 let buf = RunVimInTerminal('-S Xtest_winscrolled_not', #{rows: 10, cols: 60, statusoff: 2})
623 call term_sendkeys(buf, ":split\<CR>")
624 call TermWait(buf)
625 " use a timer to show the message after redrawing
626 call term_sendkeys(buf, ":au WinScrolled * call timer_start(100, 'ShowTriggered')\<CR>")
627 call VerifyScreenDump(buf, 'Test_winscrolled_not_when_defined_1', {})
628
629 call term_sendkeys(buf, "\<C-E>")
630 call VerifyScreenDump(buf, 'Test_winscrolled_not_when_defined_2', {})
631
632 call StopVimInTerminal(buf)
633endfunc
634
zeertzjq670ab032022-08-28 19:16:15 +0100635func Test_WinScrolled_long_wrapped()
636 CheckRunVimInTerminal
637
638 let lines =<< trim END
639 set scrolloff=0
640 let height = winheight(0)
641 let width = winwidth(0)
642 let g:scrolled = 0
643 au WinScrolled * let g:scrolled += 1
644 call setline(1, repeat('foo', height * width))
645 call cursor(1, height * width)
646 END
Bram Moolenaare1f3ab72022-09-04 21:29:08 +0100647 call writefile(lines, 'Xtest_winscrolled_long_wrapped', 'D')
zeertzjq670ab032022-08-28 19:16:15 +0100648 let buf = RunVimInTerminal('-S Xtest_winscrolled_long_wrapped', {'rows': 6})
649
650 call term_sendkeys(buf, ":echo g:scrolled\<CR>")
651 call WaitForAssert({-> assert_match('^0 ', term_getline(buf, 6))}, 1000)
652
653 call term_sendkeys(buf, 'gj')
654 call term_sendkeys(buf, ":echo g:scrolled\<CR>")
655 call WaitForAssert({-> assert_match('^1 ', term_getline(buf, 6))}, 1000)
656
657 call term_sendkeys(buf, '0')
658 call term_sendkeys(buf, ":echo g:scrolled\<CR>")
659 call WaitForAssert({-> assert_match('^2 ', term_getline(buf, 6))}, 1000)
660
661 call term_sendkeys(buf, '$')
662 call term_sendkeys(buf, ":echo g:scrolled\<CR>")
663 call WaitForAssert({-> assert_match('^3 ', term_getline(buf, 6))}, 1000)
Bram Moolenaar23526d22022-12-05 15:50:41 +0000664
665 call StopVimInTerminal(buf)
zeertzjq670ab032022-08-28 19:16:15 +0100666endfunc
667
zeertzjq3fc84dc2022-12-07 09:17:59 +0000668func Test_WinScrolled_diff()
669 CheckRunVimInTerminal
670
671 let lines =<< trim END
672 set diffopt+=foldcolumn:0
673 call setline(1, ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i'])
674 vnew
675 call setline(1, ['d', 'e', 'f', 'g', 'h', 'i'])
676 windo diffthis
677 func WriteScrollEvent()
678 call writefile([json_encode(v:event)], 'XscrollEvent')
679 endfunc
680 au WinScrolled * call WriteScrollEvent()
681 END
682 call writefile(lines, 'Xtest_winscrolled_diff', 'D')
683 let buf = RunVimInTerminal('-S Xtest_winscrolled_diff', {'rows': 8})
684
685 call term_sendkeys(buf, "\<C-E>")
686 call WaitForAssert({-> assert_match('^d', term_getline(buf, 3))}, 1000)
687
688 let event = readfile('XscrollEvent')[0]->json_decode()
689 call assert_equal({
690 \ 'all': {'leftcol': 0, 'topline': 1, 'topfill': 1, 'width': 0, 'height': 0, 'skipcol': 0},
691 \ '1000': {'leftcol': 0, 'topline': 1, 'topfill': 0, 'width': 0, 'height': 0, 'skipcol': 0},
692 \ '1001': {'leftcol': 0, 'topline': 0, 'topfill': -1, 'width': 0, 'height': 0, 'skipcol': 0}
693 \ }, event)
694
695 call term_sendkeys(buf, "2\<C-E>")
696 call WaitForAssert({-> assert_match('^f', term_getline(buf, 3))}, 1000)
697
698 let event = readfile('XscrollEvent')[0]->json_decode()
699 call assert_equal({
700 \ 'all': {'leftcol': 0, 'topline': 2, 'topfill': 2, 'width': 0, 'height': 0, 'skipcol': 0},
701 \ '1000': {'leftcol': 0, 'topline': 2, 'topfill': 0, 'width': 0, 'height': 0, 'skipcol': 0},
702 \ '1001': {'leftcol': 0, 'topline': 0, 'topfill': -2, 'width': 0, 'height': 0, 'skipcol': 0}
703 \ }, event)
704
705 call term_sendkeys(buf, "\<C-E>")
706 call WaitForAssert({-> assert_match('^g', term_getline(buf, 3))}, 1000)
707
708 let event = readfile('XscrollEvent')[0]->json_decode()
709 call assert_equal({
710 \ 'all': {'leftcol': 0, 'topline': 2, 'topfill': 0, 'width': 0, 'height': 0, 'skipcol': 0},
711 \ '1000': {'leftcol': 0, 'topline': 1, 'topfill': 0, 'width': 0, 'height': 0, 'skipcol': 0},
712 \ '1001': {'leftcol': 0, 'topline': 1, 'topfill': 0, 'width': 0, 'height': 0, 'skipcol': 0}
713 \ }, event)
714
715 call term_sendkeys(buf, "2\<C-Y>")
716 call WaitForAssert({-> assert_match('^e', term_getline(buf, 3))}, 1000)
717
718 let event = readfile('XscrollEvent')[0]->json_decode()
719 call assert_equal({
720 \ 'all': {'leftcol': 0, 'topline': 3, 'topfill': 1, 'width': 0, 'height': 0, 'skipcol': 0},
721 \ '1000': {'leftcol': 0, 'topline': -2, 'topfill': 0, 'width': 0, 'height': 0, 'skipcol': 0},
722 \ '1001': {'leftcol': 0, 'topline': -1, 'topfill': 1, 'width': 0, 'height': 0, 'skipcol': 0}
723 \ }, event)
724
725 call StopVimInTerminal(buf)
Dominique Pelle541c87c2023-01-17 21:20:44 +0000726 call delete('XscrollEvent')
zeertzjq3fc84dc2022-12-07 09:17:59 +0000727endfunc
728
naohiro ono23beefe2021-11-13 12:38:49 +0000729func Test_WinClosed()
730 " Test that the pattern is matched against the closed window's ID, and both
731 " <amatch> and <afile> are set to it.
732 new
733 let winid = win_getid()
734 let g:matched = v:false
735 augroup test-WinClosed
736 autocmd!
737 execute 'autocmd WinClosed' winid 'let g:matched = v:true'
738 autocmd WinClosed * let g:amatch = str2nr(expand('<amatch>'))
739 autocmd WinClosed * let g:afile = str2nr(expand('<afile>'))
740 augroup END
741 close
742 call assert_true(g:matched)
743 call assert_equal(winid, g:amatch)
744 call assert_equal(winid, g:afile)
745
746 " Test that WinClosed is non-recursive.
747 new
748 new
749 call assert_equal(3, winnr('$'))
750 let g:triggered = 0
751 augroup test-WinClosed
752 autocmd!
753 autocmd WinClosed * let g:triggered += 1
754 autocmd WinClosed * 2 wincmd c
755 augroup END
756 close
757 call assert_equal(1, winnr('$'))
758 call assert_equal(1, g:triggered)
759
760 autocmd! test-WinClosed
761 augroup! test-WinClosed
762 unlet g:matched
763 unlet g:amatch
764 unlet g:afile
765 unlet g:triggered
766endfunc
767
Bram Moolenaarc947b9a2022-04-06 17:59:21 +0100768func Test_WinClosed_throws()
769 vnew
770 let bnr = bufnr()
771 call assert_equal(1, bufloaded(bnr))
772 augroup test-WinClosed
773 autocmd WinClosed * throw 'foo'
774 augroup END
775 try
776 close
777 catch /.*/
778 endtry
779 call assert_equal(0, bufloaded(bnr))
780
781 autocmd! test-WinClosed
782 augroup! test-WinClosed
783endfunc
784
zeertzjq6a069402022-04-07 14:08:29 +0100785func Test_WinClosed_throws_with_tabs()
786 tabnew
787 let bnr = bufnr()
788 call assert_equal(1, bufloaded(bnr))
789 augroup test-WinClosed
790 autocmd WinClosed * throw 'foo'
791 augroup END
792 try
793 close
794 catch /.*/
795 endtry
796 call assert_equal(0, bufloaded(bnr))
797
798 autocmd! test-WinClosed
799 augroup! test-WinClosed
800endfunc
801
zeertzjq62de54b2022-09-22 18:08:37 +0100802" This used to trigger WinClosed twice for the same window, and the window's
803" buffer was NULL in the second autocommand.
804func Test_WinClosed_switch_tab()
805 edit Xa
806 split Xb
807 split Xc
808 tab split
809 new
810 augroup test-WinClosed
811 autocmd WinClosed * tabprev | bwipe!
812 augroup END
813 close
814 " Check that the tabline has been fully removed
815 call assert_equal([1, 1], win_screenpos(0))
816
817 autocmd! test-WinClosed
818 augroup! test-WinClosed
819 %bwipe!
820endfunc
821
zeertzjqb2ec0da2024-03-09 15:39:27 +0100822" This used to trigger WinClosed twice for the same window, and the window's
823" buffer was NULL in the second autocommand.
824func Test_WinClosed_BufUnload_close_other()
825 tabnew
826 let g:tab = tabpagenr()
827 let g:buf = bufnr()
828 new
829 setlocal bufhidden=wipe
830 augroup test-WinClosed
831 autocmd BufUnload * ++once exe g:buf .. 'bwipe!'
832 autocmd WinClosed * call tabpagebuflist(g:tab)
833 augroup END
834 close
835
836 unlet g:tab
837 unlet g:buf
838 autocmd! test-WinClosed
839 augroup! test-WinClosed
840 %bwipe!
841endfunc
842
Bram Moolenaare99e8442016-07-26 20:43:40 +0200843func s:AddAnAutocmd()
844 augroup vimBarTest
845 au BufReadCmd * echo 'hello'
846 augroup END
847 call assert_equal(3, len(split(execute('au vimBarTest'), "\n")))
848endfunc
849
850func Test_early_bar()
851 " test that a bar is recognized before the {event}
852 call s:AddAnAutocmd()
Bram Moolenaarb8e642f2021-11-20 10:38:25 +0000853 augroup vimBarTest | au! | let done = 77 | augroup END
Bram Moolenaare99e8442016-07-26 20:43:40 +0200854 call assert_equal(1, len(split(execute('au vimBarTest'), "\n")))
Bram Moolenaarb8e642f2021-11-20 10:38:25 +0000855 call assert_equal(77, done)
Bram Moolenaare99e8442016-07-26 20:43:40 +0200856
857 call s:AddAnAutocmd()
Bram Moolenaarb8e642f2021-11-20 10:38:25 +0000858 augroup vimBarTest| au!| let done = 88 | augroup END
Bram Moolenaare99e8442016-07-26 20:43:40 +0200859 call assert_equal(1, len(split(execute('au vimBarTest'), "\n")))
Bram Moolenaarb8e642f2021-11-20 10:38:25 +0000860 call assert_equal(88, done)
Bram Moolenaare99e8442016-07-26 20:43:40 +0200861
862 " test that a bar is recognized after the {event}
863 call s:AddAnAutocmd()
Bram Moolenaarb8e642f2021-11-20 10:38:25 +0000864 augroup vimBarTest| au!BufReadCmd| let done = 99 | augroup END
Bram Moolenaare99e8442016-07-26 20:43:40 +0200865 call assert_equal(1, len(split(execute('au vimBarTest'), "\n")))
Bram Moolenaarb8e642f2021-11-20 10:38:25 +0000866 call assert_equal(99, done)
Bram Moolenaare99e8442016-07-26 20:43:40 +0200867
868 " test that a bar is recognized after the {group}
869 call s:AddAnAutocmd()
870 au! vimBarTest|echo 'hello'
871 call assert_equal(1, len(split(execute('au vimBarTest'), "\n")))
872endfunc
Bram Moolenaarf2c4c392016-07-29 20:50:24 +0200873
Bram Moolenaar5c809082016-09-01 16:21:48 +0200874func RemoveGroup()
875 autocmd! StartOK
876 augroup! StartOK
877endfunc
878
Bram Moolenaarf2c4c392016-07-29 20:50:24 +0200879func Test_augroup_warning()
880 augroup TheWarning
881 au VimEnter * echo 'entering'
882 augroup END
Bram Moolenaar5dc4e2f2020-11-25 14:15:12 +0100883 call assert_match("TheWarning.*VimEnter", execute('au VimEnter'))
Bram Moolenaarf2c4c392016-07-29 20:50:24 +0200884 redir => res
885 augroup! TheWarning
886 redir END
Bram Moolenaar5dc4e2f2020-11-25 14:15:12 +0100887 call assert_match("W19:", res)
888 call assert_match("-Deleted-.*VimEnter", execute('au VimEnter'))
Bram Moolenaarf2c4c392016-07-29 20:50:24 +0200889
890 " check "Another" does not take the pace of the deleted entry
891 augroup Another
892 augroup END
Bram Moolenaar5dc4e2f2020-11-25 14:15:12 +0100893 call assert_match("-Deleted-.*VimEnter", execute('au VimEnter'))
Bram Moolenaaraeac9002016-09-06 22:15:08 +0200894 augroup! Another
Bram Moolenaar5c809082016-09-01 16:21:48 +0200895
896 " no warning for postpone aucmd delete
897 augroup StartOK
898 au VimEnter * call RemoveGroup()
899 augroup END
Bram Moolenaar5dc4e2f2020-11-25 14:15:12 +0100900 call assert_match("StartOK.*VimEnter", execute('au VimEnter'))
Bram Moolenaar5c809082016-09-01 16:21:48 +0200901 redir => res
902 doautocmd VimEnter
903 redir END
Bram Moolenaar5dc4e2f2020-11-25 14:15:12 +0100904 call assert_notmatch("W19:", res)
Bram Moolenaarde653f02016-09-03 16:59:06 +0200905 au! VimEnter
Bram Moolenaarad48e6c2020-04-21 22:19:45 +0200906
907 call assert_fails('augroup!', 'E471:')
Bram Moolenaarf2c4c392016-07-29 20:50:24 +0200908endfunc
Bram Moolenaarb62cc362016-09-03 16:43:53 +0200909
Bram Moolenaar8d84ff12017-10-26 16:42:16 +0200910func Test_BufReadCmdHelp()
911 " This used to cause access to free memory
912 au BufReadCmd * e +h
913 help
914
Bram Moolenaar8d84ff12017-10-26 16:42:16 +0200915 au! BufReadCmd
916endfunc
917
918func Test_BufReadCmdHelpJump()
919 " This used to cause access to free memory
920 au BufReadCmd * e +h{
Bram Moolenaarcf1ba352017-10-27 00:55:04 +0200921 " } to fix highlighting
922 call assert_fails('help', 'E434:')
Bram Moolenaar8d84ff12017-10-26 16:42:16 +0200923
Bram Moolenaar8d84ff12017-10-26 16:42:16 +0200924 au! BufReadCmd
925endfunc
926
zeertzjq93f72cc2022-08-26 15:34:52 +0100927" BufReadCmd is triggered for a "nofile" buffer. Check all values.
Bram Moolenaarb1d2c812022-08-26 11:55:01 +0100928func Test_BufReadCmdNofile()
zeertzjq93f72cc2022-08-26 15:34:52 +0100929 for val in ['nofile',
930 \ 'nowrite',
931 \ 'acwrite',
932 \ 'quickfix',
933 \ 'help',
934 \ 'terminal',
935 \ 'prompt',
936 \ 'popup',
937 \ ]
938 new somefile
939 exe 'set buftype=' .. val
940 au BufReadCmd somefile call setline(1, 'triggered')
941 edit
942 call assert_equal('triggered', getline(1))
Bram Moolenaarb1d2c812022-08-26 11:55:01 +0100943
zeertzjq93f72cc2022-08-26 15:34:52 +0100944 au! BufReadCmd
945 bwipe!
946 endfor
Bram Moolenaarb1d2c812022-08-26 11:55:01 +0100947endfunc
948
Bram Moolenaarb62cc362016-09-03 16:43:53 +0200949func Test_augroup_deleted()
Bram Moolenaarde653f02016-09-03 16:59:06 +0200950 " This caused a crash before E936 was introduced
Bram Moolenaarb62cc362016-09-03 16:43:53 +0200951 augroup x
Bram Moolenaarde653f02016-09-03 16:59:06 +0200952 call assert_fails('augroup! x', 'E936:')
953 au VimEnter * echo
954 augroup end
Bram Moolenaarb62cc362016-09-03 16:43:53 +0200955 augroup! x
Bram Moolenaar5dc4e2f2020-11-25 14:15:12 +0100956 call assert_match("-Deleted-.*VimEnter", execute('au VimEnter'))
Bram Moolenaarde653f02016-09-03 16:59:06 +0200957 au! VimEnter
Bram Moolenaarb62cc362016-09-03 16:43:53 +0200958endfunc
959
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200960" Tests for autocommands on :close command.
961" This used to be in test13.
962func Test_three_windows()
Bram Moolenaarb3435b02016-09-29 20:54:59 +0200963 " Clean up buffers, because in some cases this function fails.
964 call s:cleanup_buffers()
965
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200966 " Write three files and open them, each in a window.
967 " Then go to next window, with autocommand that deletes the previous one.
968 " Do this twice, writing the file.
969 e! Xtestje1
970 call setline(1, 'testje1')
971 w
972 sp Xtestje2
973 call setline(1, 'testje2')
974 w
975 sp Xtestje3
976 call setline(1, 'testje3')
977 w
978 wincmd w
979 au WinLeave Xtestje2 bwipe
980 wincmd w
981 call assert_equal('Xtestje1', expand('%'))
982
983 au WinLeave Xtestje1 bwipe Xtestje3
984 close
985 call assert_equal('Xtestje1', expand('%'))
986
987 " Test deleting the buffer on a Unload event. If this goes wrong there
988 " will be the ATTENTION prompt.
989 e Xtestje1
990 au!
991 au! BufUnload Xtestje1 bwipe
992 call assert_fails('e Xtestje3', 'E937:')
993 call assert_equal('Xtestje3', expand('%'))
994
995 e Xtestje2
996 sp Xtestje1
997 call assert_fails('e', 'E937:')
Bram Moolenaara997b452018-04-17 23:24:06 +0200998 call assert_equal('Xtestje1', expand('%'))
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200999
1000 " Test changing buffers in a BufWipeout autocommand. If this goes wrong
1001 " there are ml_line errors and/or a Crash.
1002 au!
1003 only
1004 e Xanother
1005 e Xtestje1
1006 bwipe Xtestje2
1007 bwipe Xtestje3
1008 au BufWipeout Xtestje1 buf Xtestje1
1009 bwipe
1010 call assert_equal('Xanother', expand('%'))
1011
1012 only
1013 help
1014 wincmd w
1015 1quit
1016 call assert_equal('Xanother', expand('%'))
1017
1018 au!
Bram Moolenaar4520d442017-03-19 16:09:46 +01001019 enew
Bram Moolenaare0ab94e2016-09-04 19:50:54 +02001020 call delete('Xtestje1')
1021 call delete('Xtestje2')
1022 call delete('Xtestje3')
1023endfunc
Bram Moolenaare13b9af2017-01-13 22:01:02 +01001024
1025func Test_BufEnter()
1026 au! BufEnter
1027 au Bufenter * let val = val . '+'
1028 let g:val = ''
1029 split NewFile
1030 call assert_equal('+', g:val)
1031 bwipe!
1032 call assert_equal('++', g:val)
1033
1034 " Also get BufEnter when editing a directory
Bram Moolenaar6f14da12022-09-07 21:30:44 +01001035 call mkdir('Xbufenterdir', 'D')
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +01001036 split Xbufenterdir
Bram Moolenaare13b9af2017-01-13 22:01:02 +01001037 call assert_equal('+++', g:val)
Bram Moolenaare94260f2017-03-21 15:50:12 +01001038
1039 " On MS-Windows we can't edit the directory, make sure we wipe the right
1040 " buffer.
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +01001041 bwipe! Xbufenterdir
Bram Moolenaare13b9af2017-01-13 22:01:02 +01001042 au! BufEnter
Bram Moolenaara9b5b852022-08-26 13:16:20 +01001043
1044 " Editing a "nofile" buffer doesn't read the file but does trigger BufEnter
zeertzjq93f72cc2022-08-26 15:34:52 +01001045 " for historic reasons. Also test other 'buftype' values.
1046 for val in ['nofile',
1047 \ 'nowrite',
1048 \ 'acwrite',
1049 \ 'quickfix',
1050 \ 'help',
1051 \ 'terminal',
1052 \ 'prompt',
1053 \ 'popup',
1054 \ ]
1055 new somefile
1056 exe 'set buftype=' .. val
1057 au BufEnter somefile call setline(1, 'some text')
1058 edit
1059 call assert_equal('some text', getline(1))
1060 bwipe!
1061 au! BufEnter
1062 endfor
Bram Moolenaar9fda8152022-11-19 13:14:10 +00001063
1064 new
1065 new
1066 autocmd BufEnter * ++once close
1067 call assert_fails('close', 'E1312:')
1068
1069 au! BufEnter
1070 only
Bram Moolenaare13b9af2017-01-13 22:01:02 +01001071endfunc
Bram Moolenaar8c752bd2017-03-19 17:09:56 +01001072
1073" Closing a window might cause an endless loop
1074" E814 for older Vims
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001075func Test_autocmd_bufwipe_in_SessLoadPost()
Bram Moolenaar1d68d9b2017-10-13 22:33:32 +02001076 edit Xtest
Bram Moolenaar8c752bd2017-03-19 17:09:56 +01001077 tabnew
Bram Moolenaar1d68d9b2017-10-13 22:33:32 +02001078 file Xsomething
Bram Moolenaar8c752bd2017-03-19 17:09:56 +01001079 set noswapfile
Bram Moolenaar8c752bd2017-03-19 17:09:56 +01001080 mksession!
1081
Bram Moolenaarc79745a2019-05-20 22:12:34 +02001082 let content =<< trim [CODE]
Bram Moolenaar62cd26a2020-10-11 20:08:44 +02001083 call test_override('ui_delay', 10)
Bram Moolenaarc79745a2019-05-20 22:12:34 +02001084 set nocp noswapfile
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001085 let v:swapchoice = "e"
Bram Moolenaarc79745a2019-05-20 22:12:34 +02001086 augroup test_autocmd_sessionload
1087 autocmd!
1088 autocmd SessionLoadPost * exe bufnr("Xsomething") . "bw!"
1089 augroup END
1090
1091 func WriteErrors()
Bram Moolenaarbe9fc5b2022-09-09 17:09:35 +01001092 call writefile([execute("messages")], "XerrorsBwipe")
Bram Moolenaarc79745a2019-05-20 22:12:34 +02001093 endfunc
1094 au VimLeave * call WriteErrors()
1095 [CODE]
1096
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01001097 call writefile(content, 'Xvimrc', 'D')
Bram Moolenaar93344c22019-08-14 21:12:05 +02001098 call system(GetVimCommand('Xvimrc') .. ' --not-a-term --noplugins -S Session.vim -c cq')
Bram Moolenaarae04a602022-09-09 15:08:10 +01001099 sleep 100m
Bram Moolenaarbe9fc5b2022-09-09 17:09:35 +01001100 let errors = join(readfile('XerrorsBwipe'))
Bram Moolenaare2e40752020-09-04 21:18:46 +02001101 call assert_match('E814:', errors)
Bram Moolenaar8c752bd2017-03-19 17:09:56 +01001102
Bram Moolenaar8c752bd2017-03-19 17:09:56 +01001103 set swapfile
Bram Moolenaarbe9fc5b2022-09-09 17:09:35 +01001104 for file in ['Session.vim', 'XerrorsBwipe']
Bram Moolenaar8c752bd2017-03-19 17:09:56 +01001105 call delete(file)
1106 endfor
1107endfunc
1108
Bram Moolenaar797e63b2021-01-15 16:22:52 +01001109" Using :blast and :ball for many events caused a crash, because b_nwindows was
1110" not incremented correctly.
1111func Test_autocmd_blast_badd()
1112 let content =<< trim [CODE]
1113 au BufNew,BufAdd,BufWinEnter,BufEnter,BufLeave,BufWinLeave,BufUnload,VimEnter foo* blast
1114 edit foo1
1115 au BufNew,BufAdd,BufWinEnter,BufEnter,BufLeave,BufWinLeave,BufUnload,VimEnter foo* ball
1116 edit foo2
Bram Moolenaarbe9fc5b2022-09-09 17:09:35 +01001117 call writefile(['OK'], 'XerrorsBlast')
Bram Moolenaar797e63b2021-01-15 16:22:52 +01001118 qall
1119 [CODE]
1120
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01001121 call writefile(content, 'XblastBall', 'D')
Bram Moolenaar797e63b2021-01-15 16:22:52 +01001122 call system(GetVimCommand() .. ' --clean -S XblastBall')
Bram Moolenaarae04a602022-09-09 15:08:10 +01001123 sleep 100m
Bram Moolenaarbe9fc5b2022-09-09 17:09:35 +01001124 call assert_match('OK', readfile('XerrorsBlast')->join())
Bram Moolenaar797e63b2021-01-15 16:22:52 +01001125
Bram Moolenaarbe9fc5b2022-09-09 17:09:35 +01001126 call delete('XerrorsBlast')
Bram Moolenaar797e63b2021-01-15 16:22:52 +01001127endfunc
1128
Bram Moolenaar8c752bd2017-03-19 17:09:56 +01001129" SEGV occurs in older versions.
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001130func Test_autocmd_bufwipe_in_SessLoadPost2()
Bram Moolenaar8c752bd2017-03-19 17:09:56 +01001131 tabnew
1132 set noswapfile
Bram Moolenaar8c752bd2017-03-19 17:09:56 +01001133 mksession!
1134
Bram Moolenaarc79745a2019-05-20 22:12:34 +02001135 let content =<< trim [CODE]
1136 set nocp noswapfile
1137 function! DeleteInactiveBufs()
1138 tabfirst
1139 let tabblist = []
1140 for i in range(1, tabpagenr(''$''))
1141 call extend(tabblist, tabpagebuflist(i))
1142 endfor
1143 for b in range(1, bufnr(''$''))
1144 if bufexists(b) && buflisted(b) && (index(tabblist, b) == -1 || bufname(b) =~# ''^$'')
1145 exec ''bwipeout '' . b
1146 endif
1147 endfor
1148 echomsg "SessionLoadPost DONE"
1149 endfunction
1150 au SessionLoadPost * call DeleteInactiveBufs()
1151
1152 func WriteErrors()
Bram Moolenaarbe9fc5b2022-09-09 17:09:35 +01001153 call writefile([execute("messages")], "XerrorsPost")
Bram Moolenaarc79745a2019-05-20 22:12:34 +02001154 endfunc
1155 au VimLeave * call WriteErrors()
1156 [CODE]
1157
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01001158 call writefile(content, 'Xvimrc', 'D')
Bram Moolenaar93344c22019-08-14 21:12:05 +02001159 call system(GetVimCommand('Xvimrc') .. ' --not-a-term --noplugins -S Session.vim -c cq')
Bram Moolenaarae04a602022-09-09 15:08:10 +01001160 sleep 100m
Bram Moolenaarbe9fc5b2022-09-09 17:09:35 +01001161 let errors = join(readfile('XerrorsPost'))
Bram Moolenaare94260f2017-03-21 15:50:12 +01001162 " This probably only ever matches on unix.
1163 call assert_notmatch('Caught deadly signal SEGV', errors)
1164 call assert_match('SessionLoadPost DONE', errors)
Bram Moolenaar8c752bd2017-03-19 17:09:56 +01001165
Bram Moolenaar8c752bd2017-03-19 17:09:56 +01001166 set swapfile
Bram Moolenaarbe9fc5b2022-09-09 17:09:35 +01001167 for file in ['Session.vim', 'XerrorsPost']
Bram Moolenaar8c752bd2017-03-19 17:09:56 +01001168 call delete(file)
1169 endfor
1170endfunc
Bram Moolenaarfaf29d72017-07-09 11:07:16 +02001171
1172func Test_empty_doau()
1173 doau \|
1174endfunc
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001175
1176func s:AutoCommandOptionSet(match)
Bram Moolenaard7c96872019-06-15 17:12:48 +02001177 let template = "Option: <%s>, OldVal: <%s>, OldValLocal: <%s>, OldValGlobal: <%s>, NewVal: <%s>, Scope: <%s>, Command: <%s>\n"
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001178 let item = remove(g:options, 0)
Bram Moolenaard7c96872019-06-15 17:12:48 +02001179 let expected = printf(template, item[0], item[1], item[2], item[3], item[4], item[5], item[6])
1180 let actual = printf(template, a:match, v:option_old, v:option_oldlocal, v:option_oldglobal, v:option_new, v:option_type, v:option_command)
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001181 let g:opt = [expected, actual]
1182 "call assert_equal(expected, actual)
1183endfunc
1184
1185func Test_OptionSet()
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +02001186 CheckOption autochdir
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001187
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +02001188 badd test_autocmd.vim
1189
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001190 call test_override('starting', 1)
1191 set nocp
1192 au OptionSet * :call s:AutoCommandOptionSet(expand("<amatch>"))
1193
1194 " 1: Setting number option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001195 let g:options = [['number', 0, 0, 0, 1, 'global', 'set']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001196 set nu
1197 call assert_equal([], g:options)
1198 call assert_equal(g:opt[0], g:opt[1])
1199
1200 " 2: Setting local number option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001201 let g:options = [['number', 1, 1, '', 0, 'local', 'setlocal']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001202 setlocal nonu
1203 call assert_equal([], g:options)
1204 call assert_equal(g:opt[0], g:opt[1])
1205
1206 " 3: Setting global number option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001207 let g:options = [['number', 1, '', 1, 0, 'global', 'setglobal']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001208 setglobal nonu
1209 call assert_equal([], g:options)
1210 call assert_equal(g:opt[0], g:opt[1])
1211
1212 " 4: Setting local autoindent option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001213 let g:options = [['autoindent', 0, 0, '', 1, 'local', 'setlocal']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001214 setlocal ai
1215 call assert_equal([], g:options)
1216 call assert_equal(g:opt[0], g:opt[1])
1217
1218 " 5: Setting global autoindent option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001219 let g:options = [['autoindent', 0, '', 0, 1, 'global', 'setglobal']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001220 setglobal ai
1221 call assert_equal([], g:options)
1222 call assert_equal(g:opt[0], g:opt[1])
1223
1224 " 6: Setting global autoindent option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001225 let g:options = [['autoindent', 1, 1, 1, 0, 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001226 set ai!
1227 call assert_equal([], g:options)
1228 call assert_equal(g:opt[0], g:opt[1])
1229
1230 " 6a: Setting global autoindent option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001231 let g:options = [['autoindent', 1, 1, 0, 0, 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001232 noa setlocal ai
1233 noa setglobal noai
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001234 set ai!
1235 call assert_equal([], g:options)
1236 call assert_equal(g:opt[0], g:opt[1])
1237
1238 " Should not print anything, use :noa
1239 " 7: don't trigger OptionSet"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001240 let g:options = [['invalid', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001241 noa set nonu
Bram Moolenaard7c96872019-06-15 17:12:48 +02001242 call assert_equal([['invalid', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid']], g:options)
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001243 call assert_equal(g:opt[0], g:opt[1])
1244
1245 " 8: Setting several global list and number option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001246 let g:options = [['list', 0, 0, 0, 1, 'global', 'set'], ['number', 0, 0, 0, 1, 'global', 'set']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001247 set list nu
1248 call assert_equal([], g:options)
1249 call assert_equal(g:opt[0], g:opt[1])
1250
1251 " 9: don't trigger OptionSet"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001252 let g:options = [['invalid', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid'], ['invalid', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001253 noa set nolist nonu
Bram Moolenaard7c96872019-06-15 17:12:48 +02001254 call assert_equal([['invalid', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid'], ['invalid', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid']], g:options)
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001255 call assert_equal(g:opt[0], g:opt[1])
1256
1257 " 10: Setting global acd"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001258 let g:options = [['autochdir', 0, 0, '', 1, 'local', 'setlocal']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001259 setlocal acd
1260 call assert_equal([], g:options)
1261 call assert_equal(g:opt[0], g:opt[1])
1262
1263 " 11: Setting global autoread (also sets local value)"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001264 let g:options = [['autoread', 0, 0, 0, 1, 'global', 'set']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001265 set ar
1266 call assert_equal([], g:options)
1267 call assert_equal(g:opt[0], g:opt[1])
1268
1269 " 12: Setting local autoread"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001270 let g:options = [['autoread', 1, 1, '', 1, 'local', 'setlocal']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001271 setlocal ar
1272 call assert_equal([], g:options)
1273 call assert_equal(g:opt[0], g:opt[1])
1274
1275 " 13: Setting global autoread"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001276 let g:options = [['autoread', 1, '', 1, 0, 'global', 'setglobal']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001277 setglobal invar
1278 call assert_equal([], g:options)
1279 call assert_equal(g:opt[0], g:opt[1])
1280
1281 " 14: Setting option backspace through :let"
Luca Saccarola959ef612024-12-01 16:25:53 +01001282 let g:options = [['backspace', 'indent,eol,start', 'indent,eol,start', 'indent,eol,start', '', 'global', 'set']]
1283 let &bs = ''
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001284 call assert_equal([], g:options)
1285 call assert_equal(g:opt[0], g:opt[1])
1286
1287 " 15: Setting option backspace through setbufvar()"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001288 let g:options = [['backup', 0, 0, '', 1, 'local', 'setlocal']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001289 " try twice, first time, shouldn't trigger because option name is invalid,
1290 " second time, it should trigger
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +02001291 let bnum = bufnr('%')
Bram Moolenaare2e40752020-09-04 21:18:46 +02001292 call assert_fails("call setbufvar(bnum, '&l:bk', 1)", 'E355:')
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001293 " should trigger, use correct option name
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +02001294 call setbufvar(bnum, '&backup', 1)
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001295 call assert_equal([], g:options)
1296 call assert_equal(g:opt[0], g:opt[1])
1297
1298 " 16: Setting number option using setwinvar"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001299 let g:options = [['number', 0, 0, '', 1, 'local', 'setlocal']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001300 call setwinvar(0, '&number', 1)
1301 call assert_equal([], g:options)
1302 call assert_equal(g:opt[0], g:opt[1])
1303
1304 " 17: Setting key option, shouldn't trigger"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001305 let g:options = [['key', 'invalid', 'invalid1', 'invalid2', 'invalid3', 'invalid4', 'invalid5']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001306 setlocal key=blah
1307 setlocal key=
Bram Moolenaard7c96872019-06-15 17:12:48 +02001308 call assert_equal([['key', 'invalid', 'invalid1', 'invalid2', 'invalid3', 'invalid4', 'invalid5']], g:options)
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001309 call assert_equal(g:opt[0], g:opt[1])
1310
Bram Moolenaard7c96872019-06-15 17:12:48 +02001311
1312 " 18a: Setting string global option"
1313 let oldval = &backupext
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001314 let g:options = [['backupext', oldval, oldval, oldval, 'foo', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001315 set backupext=foo
1316 call assert_equal([], g:options)
1317 call assert_equal(g:opt[0], g:opt[1])
1318
1319 " 18b: Resetting string global option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001320 let g:options = [['backupext', 'foo', 'foo', 'foo', oldval, 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001321 set backupext&
1322 call assert_equal([], g:options)
1323 call assert_equal(g:opt[0], g:opt[1])
1324
1325 " 18c: Setting global string global option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001326 let g:options = [['backupext', oldval, '', oldval, 'bar', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001327 setglobal backupext=bar
1328 call assert_equal([], g:options)
1329 call assert_equal(g:opt[0], g:opt[1])
1330
1331 " 18d: Setting local string global option"
1332 " As this is a global option this sets the global value even though
1333 " :setlocal is used!
1334 noa set backupext& " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001335 let g:options = [['backupext', oldval, oldval, '', 'baz', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001336 setlocal backupext=baz
1337 call assert_equal([], g:options)
1338 call assert_equal(g:opt[0], g:opt[1])
1339
1340 " 18e: Setting again string global option"
1341 noa setglobal backupext=ext_global " Reset global and local value (without triggering autocmd)
1342 noa setlocal backupext=ext_local " Sets the global(!) value!
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001343 let g:options = [['backupext', 'ext_local', 'ext_local', 'ext_local', 'fuu', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001344 set backupext=fuu
1345 call assert_equal([], g:options)
1346 call assert_equal(g:opt[0], g:opt[1])
1347
1348
zeertzjqb811de52021-10-21 10:50:44 +01001349 " 19a: Setting string global-local (to buffer) option"
Bram Moolenaar8efa0262017-08-20 15:47:20 +02001350 let oldval = &tags
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001351 let g:options = [['tags', oldval, oldval, oldval, 'tagpath', 'global', 'set']]
Bram Moolenaar8efa0262017-08-20 15:47:20 +02001352 set tags=tagpath
1353 call assert_equal([], g:options)
1354 call assert_equal(g:opt[0], g:opt[1])
1355
zeertzjqb811de52021-10-21 10:50:44 +01001356 " 19b: Resetting string global-local (to buffer) option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001357 let g:options = [['tags', 'tagpath', 'tagpath', 'tagpath', oldval, 'global', 'set']]
Bram Moolenaar8efa0262017-08-20 15:47:20 +02001358 set tags&
1359 call assert_equal([], g:options)
1360 call assert_equal(g:opt[0], g:opt[1])
1361
zeertzjqb811de52021-10-21 10:50:44 +01001362 " 19c: Setting global string global-local (to buffer) option "
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001363 let g:options = [['tags', oldval, '', oldval, 'tagpath1', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001364 setglobal tags=tagpath1
1365 call assert_equal([], g:options)
1366 call assert_equal(g:opt[0], g:opt[1])
1367
zeertzjqb811de52021-10-21 10:50:44 +01001368 " 19d: Setting local string global-local (to buffer) option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001369 let g:options = [['tags', 'tagpath1', 'tagpath1', '', 'tagpath2', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001370 setlocal tags=tagpath2
1371 call assert_equal([], g:options)
1372 call assert_equal(g:opt[0], g:opt[1])
1373
zeertzjqb811de52021-10-21 10:50:44 +01001374 " 19e: Setting again string global-local (to buffer) option"
1375 " Note: v:option_old is the old global value for global-local string options
Bram Moolenaard7c96872019-06-15 17:12:48 +02001376 " but the old local value for all other kinds of options.
1377 noa setglobal tags=tag_global " Reset global and local value (without triggering autocmd)
1378 noa setlocal tags=tag_local
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001379 let g:options = [['tags', 'tag_global', 'tag_local', 'tag_global', 'tagpath', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001380 set tags=tagpath
1381 call assert_equal([], g:options)
1382 call assert_equal(g:opt[0], g:opt[1])
1383
zeertzjqb811de52021-10-21 10:50:44 +01001384 " 19f: Setting string global-local (to buffer) option to an empty string"
1385 " Note: v:option_old is the old global value for global-local string options
Bram Moolenaard7c96872019-06-15 17:12:48 +02001386 " but the old local value for all other kinds of options.
1387 noa set tags=tag_global " Reset global and local value (without triggering autocmd)
1388 noa setlocal tags= " empty string
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001389 let g:options = [['tags', 'tag_global', '', 'tag_global', 'tagpath', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001390 set tags=tagpath
1391 call assert_equal([], g:options)
1392 call assert_equal(g:opt[0], g:opt[1])
1393
1394
1395 " 20a: Setting string local (to buffer) option"
1396 let oldval = &spelllang
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001397 let g:options = [['spelllang', oldval, oldval, oldval, 'elvish,klingon', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001398 set spelllang=elvish,klingon
1399 call assert_equal([], g:options)
1400 call assert_equal(g:opt[0], g:opt[1])
1401
1402 " 20b: Resetting string local (to buffer) option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001403 let g:options = [['spelllang', 'elvish,klingon', 'elvish,klingon', 'elvish,klingon', oldval, 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001404 set spelllang&
1405 call assert_equal([], g:options)
1406 call assert_equal(g:opt[0], g:opt[1])
1407
1408 " 20c: Setting global string local (to buffer) option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001409 let g:options = [['spelllang', oldval, '', oldval, 'elvish', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001410 setglobal spelllang=elvish
1411 call assert_equal([], g:options)
1412 call assert_equal(g:opt[0], g:opt[1])
1413
1414 " 20d: Setting local string local (to buffer) option"
1415 noa set spelllang& " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001416 let g:options = [['spelllang', oldval, oldval, '', 'klingon', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001417 setlocal spelllang=klingon
1418 call assert_equal([], g:options)
1419 call assert_equal(g:opt[0], g:opt[1])
1420
1421 " 20e: Setting again string local (to buffer) option"
zeertzjqb811de52021-10-21 10:50:44 +01001422 " Note: v:option_old is the old global value for global-local string options
Bram Moolenaard7c96872019-06-15 17:12:48 +02001423 " but the old local value for all other kinds of options.
1424 noa setglobal spelllang=spellglobal " Reset global and local value (without triggering autocmd)
1425 noa setlocal spelllang=spelllocal
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001426 let g:options = [['spelllang', 'spelllocal', 'spelllocal', 'spellglobal', 'foo', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001427 set spelllang=foo
1428 call assert_equal([], g:options)
1429 call assert_equal(g:opt[0], g:opt[1])
1430
1431
zeertzjqb811de52021-10-21 10:50:44 +01001432 " 21a: Setting string global-local (to window) option"
Bram Moolenaard7c96872019-06-15 17:12:48 +02001433 let oldval = &statusline
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001434 let g:options = [['statusline', oldval, oldval, oldval, 'foo', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001435 set statusline=foo
1436 call assert_equal([], g:options)
1437 call assert_equal(g:opt[0], g:opt[1])
1438
zeertzjqb811de52021-10-21 10:50:44 +01001439 " 21b: Resetting string global-local (to window) option"
1440 " Note: v:option_old is the old global value for global-local string options
Bram Moolenaard7c96872019-06-15 17:12:48 +02001441 " but the old local value for all other kinds of options.
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001442 let g:options = [['statusline', 'foo', 'foo', 'foo', oldval, 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001443 set statusline&
1444 call assert_equal([], g:options)
1445 call assert_equal(g:opt[0], g:opt[1])
1446
zeertzjqb811de52021-10-21 10:50:44 +01001447 " 21c: Setting global string global-local (to window) option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001448 let g:options = [['statusline', oldval, '', oldval, 'bar', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001449 setglobal statusline=bar
1450 call assert_equal([], g:options)
1451 call assert_equal(g:opt[0], g:opt[1])
1452
zeertzjqb811de52021-10-21 10:50:44 +01001453 " 21d: Setting local string global-local (to window) option"
Bram Moolenaard7c96872019-06-15 17:12:48 +02001454 noa set statusline& " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001455 let g:options = [['statusline', oldval, oldval, '', 'baz', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001456 setlocal statusline=baz
1457 call assert_equal([], g:options)
1458 call assert_equal(g:opt[0], g:opt[1])
1459
zeertzjqb811de52021-10-21 10:50:44 +01001460 " 21e: Setting again string global-local (to window) option"
1461 " Note: v:option_old is the old global value for global-local string options
Bram Moolenaard7c96872019-06-15 17:12:48 +02001462 " but the old local value for all other kinds of options.
1463 noa setglobal statusline=bar " Reset global and local value (without triggering autocmd)
1464 noa setlocal statusline=baz
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001465 let g:options = [['statusline', 'bar', 'baz', 'bar', 'foo', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001466 set statusline=foo
1467 call assert_equal([], g:options)
1468 call assert_equal(g:opt[0], g:opt[1])
1469
1470
1471 " 22a: Setting string local (to window) option"
1472 let oldval = &foldignore
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001473 let g:options = [['foldignore', oldval, oldval, oldval, 'fo', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001474 set foldignore=fo
1475 call assert_equal([], g:options)
1476 call assert_equal(g:opt[0], g:opt[1])
1477
1478 " 22b: Resetting string local (to window) option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001479 let g:options = [['foldignore', 'fo', 'fo', 'fo', oldval, 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001480 set foldignore&
1481 call assert_equal([], g:options)
1482 call assert_equal(g:opt[0], g:opt[1])
1483
1484 " 22c: Setting global string local (to window) option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001485 let g:options = [['foldignore', oldval, '', oldval, 'bar', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001486 setglobal foldignore=bar
1487 call assert_equal([], g:options)
1488 call assert_equal(g:opt[0], g:opt[1])
1489
1490 " 22d: Setting local string local (to window) option"
1491 noa set foldignore& " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001492 let g:options = [['foldignore', oldval, oldval, '', 'baz', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001493 setlocal foldignore=baz
1494 call assert_equal([], g:options)
1495 call assert_equal(g:opt[0], g:opt[1])
1496
1497 " 22e: Setting again string local (to window) option"
1498 noa setglobal foldignore=glob " Reset global and local value (without triggering autocmd)
1499 noa setlocal foldignore=loc
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001500 let g:options = [['foldignore', 'loc', 'loc', 'glob', 'fo', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001501 set foldignore=fo
1502 call assert_equal([], g:options)
1503 call assert_equal(g:opt[0], g:opt[1])
1504
1505
zeertzjqb811de52021-10-21 10:50:44 +01001506 " 23a: Setting global number global option"
Bram Moolenaard7c96872019-06-15 17:12:48 +02001507 noa setglobal cmdheight=8 " Reset global and local value (without triggering autocmd)
1508 noa setlocal cmdheight=1 " Sets the global(!) value!
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001509 let g:options = [['cmdheight', '1', '', '1', '2', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001510 setglobal cmdheight=2
1511 call assert_equal([], g:options)
1512 call assert_equal(g:opt[0], g:opt[1])
1513
1514 " 23b: Setting local number global option"
1515 noa setglobal cmdheight=8 " Reset global and local value (without triggering autocmd)
1516 noa setlocal cmdheight=1 " Sets the global(!) value!
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001517 let g:options = [['cmdheight', '1', '1', '', '2', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001518 setlocal cmdheight=2
1519 call assert_equal([], g:options)
1520 call assert_equal(g:opt[0], g:opt[1])
1521
1522 " 23c: Setting again number global option"
1523 noa setglobal cmdheight=8 " Reset global and local value (without triggering autocmd)
1524 noa setlocal cmdheight=1 " Sets the global(!) value!
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001525 let g:options = [['cmdheight', '1', '1', '1', '2', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001526 set cmdheight=2
1527 call assert_equal([], g:options)
1528 call assert_equal(g:opt[0], g:opt[1])
1529
1530 " 23d: Setting again number global option"
1531 noa set cmdheight=8 " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001532 let g:options = [['cmdheight', '8', '8', '8', '2', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001533 set cmdheight=2
1534 call assert_equal([], g:options)
1535 call assert_equal(g:opt[0], g:opt[1])
1536
1537
1538 " 24a: Setting global number global-local (to buffer) option"
1539 noa setglobal undolevels=8 " Reset global and local value (without triggering autocmd)
1540 noa setlocal undolevels=1
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001541 let g:options = [['undolevels', '8', '', '8', '2', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001542 setglobal undolevels=2
1543 call assert_equal([], g:options)
1544 call assert_equal(g:opt[0], g:opt[1])
1545
1546 " 24b: Setting local number global-local (to buffer) option"
1547 noa setglobal undolevels=8 " Reset global and local value (without triggering autocmd)
1548 noa setlocal undolevels=1
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001549 let g:options = [['undolevels', '1', '1', '', '2', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001550 setlocal undolevels=2
1551 call assert_equal([], g:options)
1552 call assert_equal(g:opt[0], g:opt[1])
1553
1554 " 24c: Setting again number global-local (to buffer) option"
1555 noa setglobal undolevels=8 " Reset global and local value (without triggering autocmd)
1556 noa setlocal undolevels=1
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001557 let g:options = [['undolevels', '1', '1', '8', '2', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001558 set undolevels=2
1559 call assert_equal([], g:options)
1560 call assert_equal(g:opt[0], g:opt[1])
1561
1562 " 24d: Setting again global number global-local (to buffer) option"
1563 noa set undolevels=8 " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001564 let g:options = [['undolevels', '8', '8', '8', '2', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001565 set undolevels=2
1566 call assert_equal([], g:options)
1567 call assert_equal(g:opt[0], g:opt[1])
1568
1569
1570 " 25a: Setting global number local (to buffer) option"
1571 noa setglobal wrapmargin=8 " Reset global and local value (without triggering autocmd)
1572 noa setlocal wrapmargin=1
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001573 let g:options = [['wrapmargin', '8', '', '8', '2', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001574 setglobal wrapmargin=2
1575 call assert_equal([], g:options)
1576 call assert_equal(g:opt[0], g:opt[1])
1577
1578 " 25b: Setting local number local (to buffer) option"
1579 noa setglobal wrapmargin=8 " Reset global and local value (without triggering autocmd)
1580 noa setlocal wrapmargin=1
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001581 let g:options = [['wrapmargin', '1', '1', '', '2', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001582 setlocal wrapmargin=2
1583 call assert_equal([], g:options)
1584 call assert_equal(g:opt[0], g:opt[1])
1585
1586 " 25c: Setting again number local (to buffer) option"
1587 noa setglobal wrapmargin=8 " Reset global and local value (without triggering autocmd)
1588 noa setlocal wrapmargin=1
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001589 let g:options = [['wrapmargin', '1', '1', '8', '2', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001590 set wrapmargin=2
1591 call assert_equal([], g:options)
1592 call assert_equal(g:opt[0], g:opt[1])
1593
1594 " 25d: Setting again global number local (to buffer) option"
1595 noa set wrapmargin=8 " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001596 let g:options = [['wrapmargin', '8', '8', '8', '2', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001597 set wrapmargin=2
1598 call assert_equal([], g:options)
1599 call assert_equal(g:opt[0], g:opt[1])
1600
1601
1602 " 26: Setting number global-local (to window) option.
1603 " Such option does currently not exist.
1604
1605
1606 " 27a: Setting global number local (to window) option"
1607 noa setglobal foldcolumn=8 " Reset global and local value (without triggering autocmd)
1608 noa setlocal foldcolumn=1
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001609 let g:options = [['foldcolumn', '8', '', '8', '2', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001610 setglobal foldcolumn=2
1611 call assert_equal([], g:options)
1612 call assert_equal(g:opt[0], g:opt[1])
1613
1614 " 27b: Setting local number local (to window) option"
1615 noa setglobal foldcolumn=8 " Reset global and local value (without triggering autocmd)
1616 noa setlocal foldcolumn=1
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001617 let g:options = [['foldcolumn', '1', '1', '', '2', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001618 setlocal foldcolumn=2
1619 call assert_equal([], g:options)
1620 call assert_equal(g:opt[0], g:opt[1])
1621
1622 " 27c: Setting again number local (to window) option"
1623 noa setglobal foldcolumn=8 " Reset global and local value (without triggering autocmd)
1624 noa setlocal foldcolumn=1
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001625 let g:options = [['foldcolumn', '1', '1', '8', '2', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001626 set foldcolumn=2
1627 call assert_equal([], g:options)
1628 call assert_equal(g:opt[0], g:opt[1])
1629
zeertzjqb811de52021-10-21 10:50:44 +01001630 " 27d: Setting again global number local (to window) option"
Bram Moolenaard7c96872019-06-15 17:12:48 +02001631 noa set foldcolumn=8 " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001632 let g:options = [['foldcolumn', '8', '8', '8', '2', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001633 set foldcolumn=2
1634 call assert_equal([], g:options)
1635 call assert_equal(g:opt[0], g:opt[1])
1636
1637
1638 " 28a: Setting global boolean global option"
1639 noa setglobal nowrapscan " Reset global and local value (without triggering autocmd)
1640 noa setlocal wrapscan " Sets the global(!) value!
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001641 let g:options = [['wrapscan', '1', '', '1', '0', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001642 setglobal nowrapscan
1643 call assert_equal([], g:options)
1644 call assert_equal(g:opt[0], g:opt[1])
1645
1646 " 28b: Setting local boolean global option"
1647 noa setglobal nowrapscan " Reset global and local value (without triggering autocmd)
1648 noa setlocal wrapscan " Sets the global(!) value!
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001649 let g:options = [['wrapscan', '1', '1', '', '0', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001650 setlocal nowrapscan
1651 call assert_equal([], g:options)
1652 call assert_equal(g:opt[0], g:opt[1])
1653
1654 " 28c: Setting again boolean global option"
1655 noa setglobal nowrapscan " Reset global and local value (without triggering autocmd)
1656 noa setlocal wrapscan " Sets the global(!) value!
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001657 let g:options = [['wrapscan', '1', '1', '1', '0', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001658 set nowrapscan
1659 call assert_equal([], g:options)
1660 call assert_equal(g:opt[0], g:opt[1])
1661
1662 " 28d: Setting again global boolean global option"
1663 noa set nowrapscan " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001664 let g:options = [['wrapscan', '0', '0', '0', '1', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001665 set wrapscan
1666 call assert_equal([], g:options)
1667 call assert_equal(g:opt[0], g:opt[1])
1668
1669
1670 " 29a: Setting global boolean global-local (to buffer) option"
1671 noa setglobal noautoread " Reset global and local value (without triggering autocmd)
1672 noa setlocal autoread
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001673 let g:options = [['autoread', '0', '', '0', '1', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001674 setglobal autoread
1675 call assert_equal([], g:options)
1676 call assert_equal(g:opt[0], g:opt[1])
1677
1678 " 29b: Setting local boolean global-local (to buffer) option"
1679 noa setglobal noautoread " Reset global and local value (without triggering autocmd)
1680 noa setlocal autoread
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001681 let g:options = [['autoread', '1', '1', '', '0', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001682 setlocal noautoread
1683 call assert_equal([], g:options)
1684 call assert_equal(g:opt[0], g:opt[1])
1685
1686 " 29c: Setting again boolean global-local (to buffer) option"
1687 noa setglobal noautoread " Reset global and local value (without triggering autocmd)
1688 noa setlocal autoread
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001689 let g:options = [['autoread', '1', '1', '0', '1', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001690 set autoread
1691 call assert_equal([], g:options)
1692 call assert_equal(g:opt[0], g:opt[1])
1693
1694 " 29d: Setting again global boolean global-local (to buffer) option"
1695 noa set noautoread " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001696 let g:options = [['autoread', '0', '0', '0', '1', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001697 set autoread
1698 call assert_equal([], g:options)
1699 call assert_equal(g:opt[0], g:opt[1])
1700
1701
1702 " 30a: Setting global boolean local (to buffer) option"
1703 noa setglobal nocindent " Reset global and local value (without triggering autocmd)
1704 noa setlocal cindent
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001705 let g:options = [['cindent', '0', '', '0', '1', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001706 setglobal cindent
1707 call assert_equal([], g:options)
1708 call assert_equal(g:opt[0], g:opt[1])
1709
1710 " 30b: Setting local boolean local (to buffer) option"
1711 noa setglobal nocindent " Reset global and local value (without triggering autocmd)
1712 noa setlocal cindent
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001713 let g:options = [['cindent', '1', '1', '', '0', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001714 setlocal nocindent
1715 call assert_equal([], g:options)
1716 call assert_equal(g:opt[0], g:opt[1])
1717
1718 " 30c: Setting again boolean local (to buffer) option"
1719 noa setglobal nocindent " Reset global and local value (without triggering autocmd)
1720 noa setlocal cindent
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001721 let g:options = [['cindent', '1', '1', '0', '1', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001722 set cindent
1723 call assert_equal([], g:options)
1724 call assert_equal(g:opt[0], g:opt[1])
1725
1726 " 30d: Setting again global boolean local (to buffer) option"
1727 noa set nocindent " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001728 let g:options = [['cindent', '0', '0', '0', '1', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001729 set cindent
1730 call assert_equal([], g:options)
1731 call assert_equal(g:opt[0], g:opt[1])
1732
1733
1734 " 31: Setting boolean global-local (to window) option
1735 " Currently no such option exists.
1736
1737
1738 " 32a: Setting global boolean local (to window) option"
1739 noa setglobal nocursorcolumn " Reset global and local value (without triggering autocmd)
1740 noa setlocal cursorcolumn
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001741 let g:options = [['cursorcolumn', '0', '', '0', '1', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001742 setglobal cursorcolumn
1743 call assert_equal([], g:options)
1744 call assert_equal(g:opt[0], g:opt[1])
1745
1746 " 32b: Setting local boolean local (to window) option"
1747 noa setglobal nocursorcolumn " Reset global and local value (without triggering autocmd)
1748 noa setlocal cursorcolumn
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001749 let g:options = [['cursorcolumn', '1', '1', '', '0', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001750 setlocal nocursorcolumn
1751 call assert_equal([], g:options)
1752 call assert_equal(g:opt[0], g:opt[1])
1753
1754 " 32c: Setting again boolean local (to window) option"
1755 noa setglobal nocursorcolumn " Reset global and local value (without triggering autocmd)
1756 noa setlocal cursorcolumn
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001757 let g:options = [['cursorcolumn', '1', '1', '0', '1', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001758 set cursorcolumn
1759 call assert_equal([], g:options)
1760 call assert_equal(g:opt[0], g:opt[1])
1761
1762 " 32d: Setting again global boolean local (to window) option"
1763 noa set nocursorcolumn " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001764 let g:options = [['cursorcolumn', '0', '0', '0', '1', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001765 set cursorcolumn
1766 call assert_equal([], g:options)
1767 call assert_equal(g:opt[0], g:opt[1])
1768
1769
Bram Moolenaar1bc353b2019-09-01 14:45:28 +02001770 " 33: Test autocommands when an option value is converted internally.
Bram Moolenaard7c96872019-06-15 17:12:48 +02001771 noa set backspace=1 " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001772 let g:options = [['backspace', 'indent,eol', 'indent,eol', 'indent,eol', '2', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001773 set backspace=2
1774 call assert_equal([], g:options)
1775 call assert_equal(g:opt[0], g:opt[1])
1776
1777
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001778 " Cleanup
1779 au! OptionSet
Bram Moolenaar0331faf2019-06-15 18:40:37 +02001780 " set tags&
Bram Moolenaard7c96872019-06-15 17:12:48 +02001781 for opt in ['nu', 'ai', 'acd', 'ar', 'bs', 'backup', 'cul', 'cp', 'backupext', 'tags', 'spelllang', 'statusline', 'foldignore', 'cmdheight', 'undolevels', 'wrapmargin', 'foldcolumn', 'wrapscan', 'autoread', 'cindent', 'cursorcolumn']
Bram Moolenaar91d2e782018-08-07 19:05:01 +02001782 exe printf(":set %s&vim", opt)
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001783 endfor
1784 call test_override('starting', 0)
1785 delfunc! AutoCommandOptionSet
1786endfunc
1787
1788func Test_OptionSet_diffmode()
1789 call test_override('starting', 1)
Bram Moolenaar26d98212019-01-27 22:32:55 +01001790 " 18: Changing an option when entering diff mode
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001791 new
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001792 au OptionSet diff :let &l:cul = v:option_new
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001793
1794 call setline(1, ['buffer 1', 'line2', 'line3', 'line4'])
1795 call assert_equal(0, &l:cul)
1796 diffthis
1797 call assert_equal(1, &l:cul)
1798
1799 vnew
1800 call setline(1, ['buffer 2', 'line 2', 'line 3', 'line4'])
1801 call assert_equal(0, &l:cul)
1802 diffthis
1803 call assert_equal(1, &l:cul)
1804
1805 diffoff
1806 call assert_equal(0, &l:cul)
1807 call assert_equal(1, getwinvar(2, '&l:cul'))
1808 bw!
1809
1810 call assert_equal(1, &l:cul)
1811 diffoff!
1812 call assert_equal(0, &l:cul)
1813 call assert_equal(0, getwinvar(1, '&l:cul'))
1814 bw!
1815
1816 " Cleanup
1817 au! OptionSet
1818 call test_override('starting', 0)
1819endfunc
1820
1821func Test_OptionSet_diffmode_close()
1822 call test_override('starting', 1)
1823 " 19: Try to close the current window when entering diff mode
1824 " should not segfault
1825 new
1826 au OptionSet diff close
1827
1828 call setline(1, ['buffer 1', 'line2', 'line3', 'line4'])
Bram Moolenaare2e40752020-09-04 21:18:46 +02001829 call assert_fails(':diffthis', 'E788:')
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001830 call assert_equal(1, &diff)
1831 vnew
1832 call setline(1, ['buffer 2', 'line 2', 'line 3', 'line4'])
Bram Moolenaare2e40752020-09-04 21:18:46 +02001833 call assert_fails(':diffthis', 'E788:')
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001834 call assert_equal(1, &diff)
Bram Moolenaara9aa86f2019-11-10 21:25:45 +01001835 set diffopt-=closeoff
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001836 bw!
Bram Moolenaare2e40752020-09-04 21:18:46 +02001837 call assert_fails(':diffoff!', 'E788:')
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001838 bw!
1839
1840 " Cleanup
1841 au! OptionSet
1842 call test_override('starting', 0)
1843 "delfunc! AutoCommandOptionSet
1844endfunc
Bram Moolenaar4a137b42017-08-04 22:37:11 +02001845
1846" Test for Bufleave autocommand that deletes the buffer we are about to edit.
1847func Test_BufleaveWithDelete()
Bram Moolenaare7cda972022-08-29 11:02:59 +01001848 new | edit XbufLeave1
Bram Moolenaar4a137b42017-08-04 22:37:11 +02001849
1850 augroup test_bufleavewithdelete
1851 autocmd!
Bram Moolenaare7cda972022-08-29 11:02:59 +01001852 autocmd BufLeave XbufLeave1 bwipe XbufLeave2
Bram Moolenaar4a137b42017-08-04 22:37:11 +02001853 augroup END
1854
Bram Moolenaare7cda972022-08-29 11:02:59 +01001855 call assert_fails('edit XbufLeave2', 'E143:')
1856 call assert_equal('XbufLeave1', bufname('%'))
Bram Moolenaar4a137b42017-08-04 22:37:11 +02001857
Bram Moolenaare7cda972022-08-29 11:02:59 +01001858 autocmd! test_bufleavewithdelete BufLeave XbufLeave1
Bram Moolenaar4a137b42017-08-04 22:37:11 +02001859 augroup! test_bufleavewithdelete
1860
1861 new
Bram Moolenaare7cda972022-08-29 11:02:59 +01001862 bwipe! XbufLeave1
Bram Moolenaar4a137b42017-08-04 22:37:11 +02001863endfunc
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +02001864
1865" Test for autocommand that changes the buffer list, when doing ":ball".
1866func Test_Acmd_BufAll()
1867 enew!
1868 %bwipe!
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01001869 call writefile(['Test file Xxx1'], 'Xxx1', 'D')
1870 call writefile(['Test file Xxx2'], 'Xxx2', 'D')
1871 call writefile(['Test file Xxx3'], 'Xxx3', 'D')
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +02001872
1873 " Add three files to the buffer list
1874 split Xxx1
1875 close
1876 split Xxx2
1877 close
1878 split Xxx3
1879 close
1880
1881 " Wipe the buffer when the buffer is opened
1882 au BufReadPost Xxx2 bwipe
1883
1884 call append(0, 'Test file Xxx4')
1885 ball
1886
1887 call assert_equal(2, winnr('$'))
1888 call assert_equal('Xxx1', bufname(winbufnr(winnr('$'))))
1889 wincmd t
1890
1891 au! BufReadPost
1892 %bwipe!
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +02001893 enew! | only
1894endfunc
1895
1896" Test for autocommand that changes current buffer on BufEnter event.
1897" Check if modelines are interpreted for the correct buffer.
1898func Test_Acmd_BufEnter()
1899 %bwipe!
1900 call writefile(['start of test file Xxx1',
1901 \ "\<Tab>this is a test",
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01001902 \ 'end of test file Xxx1'], 'Xxx1', 'D')
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +02001903 call writefile(['start of test file Xxx2',
1904 \ 'vim: set noai :',
1905 \ "\<Tab>this is a test",
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01001906 \ 'end of test file Xxx2'], 'Xxx2', 'D')
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +02001907
1908 au BufEnter Xxx2 brew
1909 set ai modeline modelines=3
1910 edit Xxx1
1911 " edit Xxx2, autocmd will do :brew
1912 edit Xxx2
1913 exe "normal G?this is a\<CR>"
1914 " Append text with autoindent to this file
1915 normal othis should be auto-indented
1916 call assert_equal("\<Tab>this should be auto-indented", getline('.'))
1917 call assert_equal(3, line('.'))
1918 " Remove autocmd and edit Xxx2 again
1919 au! BufEnter Xxx2
1920 buf! Xxx2
1921 exe "normal G?this is a\<CR>"
1922 " append text without autoindent to Xxx
1923 normal othis should be in column 1
1924 call assert_equal("this should be in column 1", getline('.'))
1925 call assert_equal(4, line('.'))
1926
1927 %bwipe!
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +02001928 set ai&vim modeline&vim modelines&vim
1929endfunc
1930
1931" Test for issue #57
1932" do not move cursor on <c-o> when autoindent is set
1933func Test_ai_CTRL_O()
1934 enew!
1935 set ai
1936 let save_fo = &fo
1937 set fo+=r
1938 exe "normal o# abcdef\<Esc>2hi\<CR>\<C-O>d0\<Esc>"
1939 exe "normal o# abcdef\<Esc>2hi\<C-O>d0\<Esc>"
1940 call assert_equal(['# abc', 'def', 'def'], getline(2, 4))
1941
1942 set ai&vim
1943 let &fo = save_fo
1944 enew!
1945endfunc
1946
1947" Test for autocommand that deletes the current buffer on BufLeave event.
1948" Also test deleting the last buffer, should give a new, empty buffer.
1949func Test_BufLeave_Wipe()
1950 %bwipe!
1951 let content = ['start of test file Xxx',
1952 \ 'this is a test',
1953 \ 'end of test file Xxx']
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01001954 call writefile(content, 'Xxx1', 'D')
1955 call writefile(content, 'Xxx2', 'D')
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +02001956
1957 au BufLeave Xxx2 bwipe
1958 edit Xxx1
1959 split Xxx2
1960 " delete buffer Xxx2, we should be back to Xxx1
1961 bwipe
1962 call assert_equal('Xxx1', bufname('%'))
1963 call assert_equal(1, winnr('$'))
1964
1965 " Create an alternate buffer
1966 %write! test.out
1967 call assert_equal('test.out', bufname('#'))
1968 " delete alternate buffer
1969 bwipe test.out
1970 call assert_equal('Xxx1', bufname('%'))
1971 call assert_equal('', bufname('#'))
1972
1973 au BufLeave Xxx1 bwipe
1974 " delete current buffer, get an empty one
1975 bwipe!
1976 call assert_equal(1, line('$'))
1977 call assert_equal('', bufname('%'))
Bram Moolenaarb2c87502017-10-14 21:15:58 +02001978 let g:bufinfo = getbufinfo()
1979 call assert_equal(1, len(g:bufinfo))
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +02001980
Bram Moolenaar53f0c962017-10-22 14:23:59 +02001981 call delete('test.out')
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +02001982 %bwipe
1983 au! BufLeave
Bram Moolenaarb2c87502017-10-14 21:15:58 +02001984
1985 " check that bufinfo doesn't contain a pointer to freed memory
1986 call test_garbagecollect_now()
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +02001987endfunc
Bram Moolenaar87ffb5c2017-10-19 12:37:42 +02001988
1989func Test_QuitPre()
1990 edit Xfoo
1991 let winid = win_getid(winnr())
1992 split Xbar
1993 au! QuitPre * let g:afile = expand('<afile>')
1994 " Close the other window, <afile> should be correct.
1995 exe win_id2win(winid) . 'q'
1996 call assert_equal('Xfoo', g:afile)
LemonBoy66e13ae2022-04-21 22:52:11 +01001997
Bram Moolenaar87ffb5c2017-10-19 12:37:42 +02001998 unlet g:afile
1999 bwipe Xfoo
2000 bwipe Xbar
2001endfunc
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02002002
2003func Test_Cmdline()
Bram Moolenaar153b7042018-01-31 15:48:32 +01002004 au! CmdlineChanged : let g:text = getcmdline()
2005 let g:text = 0
2006 call feedkeys(":echom 'hello'\<CR>", 'xt')
2007 call assert_equal("echom 'hello'", g:text)
2008 au! CmdlineChanged
2009
2010 au! CmdlineChanged : let g:entered = expand('<afile>')
2011 let g:entered = 0
2012 call feedkeys(":echom 'hello'\<CR>", 'xt')
2013 call assert_equal(':', g:entered)
2014 au! CmdlineChanged
2015
zeertzjq412e0e42023-02-11 10:34:07 +00002016 autocmd CmdlineChanged : let g:log += [getcmdline()]
2017
Bram Moolenaarbb393d82022-12-09 12:21:50 +00002018 let g:log = []
2019 cnoremap <F1> <Cmd>call setcmdline('ls')<CR>
Bram Moolenaarbb393d82022-12-09 12:21:50 +00002020 call feedkeys(":\<F1>", 'xt')
2021 call assert_equal(['ls'], g:log)
Bram Moolenaarbb393d82022-12-09 12:21:50 +00002022 cunmap <F1>
2023
zeertzjqaf9e28a2023-02-06 20:58:09 +00002024 let g:log = []
zeertzjqaf9e28a2023-02-06 20:58:09 +00002025 call feedkeys(":sign \<Tab>\<Tab>\<C-N>\<C-P>\<S-Tab>\<S-Tab>\<Esc>", 'xt')
2026 call assert_equal([
2027 \ 's',
2028 \ 'si',
2029 \ 'sig',
2030 \ 'sign',
2031 \ 'sign ',
2032 \ 'sign define',
2033 \ 'sign jump',
2034 \ 'sign list',
2035 \ 'sign jump',
2036 \ 'sign define',
2037 \ 'sign ',
2038 \ ], g:log)
2039 let g:log = []
2040 set wildmenu wildoptions+=pum
2041 call feedkeys(":sign \<S-Tab>\<PageUp>\<kPageUp>\<kPageDown>\<PageDown>\<Esc>", 'xt')
2042 call assert_equal([
2043 \ 's',
2044 \ 'si',
2045 \ 'sig',
2046 \ 'sign',
2047 \ 'sign ',
2048 \ 'sign unplace',
2049 \ 'sign jump',
2050 \ 'sign define',
2051 \ 'sign undefine',
2052 \ 'sign unplace',
2053 \ ], g:log)
2054 set wildmenu& wildoptions&
zeertzjq412e0e42023-02-11 10:34:07 +00002055
2056 let g:log = []
2057 let @r = 'abc'
2058 call feedkeys(":0\<C-R>r1\<C-R>\<C-O>r2\<C-R>\<C-R>r3\<Esc>", 'xt')
2059 call assert_equal([
2060 \ '0',
2061 \ '0a',
2062 \ '0ab',
2063 \ '0abc',
2064 \ '0abc1',
2065 \ '0abc1abc',
2066 \ '0abc1abc2',
2067 \ '0abc1abc2abc',
2068 \ '0abc1abc2abc3',
2069 \ ], g:log)
2070
zeertzjqaf9e28a2023-02-06 20:58:09 +00002071 unlet g:log
2072 au! CmdlineChanged
2073
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02002074 au! CmdlineEnter : let g:entered = expand('<afile>')
2075 au! CmdlineLeave : let g:left = expand('<afile>')
2076 let g:entered = 0
2077 let g:left = 0
2078 call feedkeys(":echo 'hello'\<CR>", 'xt')
2079 call assert_equal(':', g:entered)
2080 call assert_equal(':', g:left)
2081 au! CmdlineEnter
2082 au! CmdlineLeave
2083
Bram Moolenaara4baf5b2018-04-22 13:27:44 +02002084 let save_shellslash = &shellslash
2085 set noshellslash
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02002086 au! CmdlineEnter / let g:entered = expand('<afile>')
2087 au! CmdlineLeave / let g:left = expand('<afile>')
2088 let g:entered = 0
2089 let g:left = 0
Bram Moolenaar53f0c962017-10-22 14:23:59 +02002090 new
2091 call setline(1, 'hello')
2092 call feedkeys("/hello\<CR>", 'xt')
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02002093 call assert_equal('/', g:entered)
2094 call assert_equal('/', g:left)
Bram Moolenaar53f0c962017-10-22 14:23:59 +02002095 bwipe!
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02002096 au! CmdlineEnter
2097 au! CmdlineLeave
Bram Moolenaara4baf5b2018-04-22 13:27:44 +02002098 let &shellslash = save_shellslash
Shougo Matsushitad0952142024-06-20 22:05:16 +02002099
zeertzjqbc6f9672024-06-21 07:51:40 +02002100 au! CursorMovedC : let g:pos += [getcmdpos()]
2101 let g:pos = []
zeertzjq81456202024-07-07 20:48:25 +02002102 call feedkeys(":foo bar baz\<C-W>\<C-W>\<C-W>\<Esc>", 'xt')
2103 call assert_equal([2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 9, 5, 1], g:pos)
2104 let g:pos = []
2105 call feedkeys(":hello\<C-B>\<Esc>", 'xt')
2106 call assert_equal([2, 3, 4, 5, 6, 1], g:pos)
2107 let g:pos = []
2108 call feedkeys(":hello\<C-U>\<Esc>", 'xt')
2109 call assert_equal([2, 3, 4, 5, 6, 1], g:pos)
2110 let g:pos = []
zeertzjqbc6f9672024-06-21 07:51:40 +02002111 call feedkeys(":hello\<Left>\<C-R>=''\<CR>\<Left>\<Right>\<Esc>", 'xt')
zeertzjq81456202024-07-07 20:48:25 +02002112 call assert_equal([2, 3, 4, 5, 6, 5, 4, 5], g:pos)
zeertzjqbc6f9672024-06-21 07:51:40 +02002113 let g:pos = []
2114 call feedkeys(":12345678\<C-R>=setcmdpos(3)??''\<CR>\<Esc>", 'xt')
zeertzjq81456202024-07-07 20:48:25 +02002115 call assert_equal([2, 3, 4, 5, 6, 7, 8, 9, 3], g:pos)
zeertzjqbc6f9672024-06-21 07:51:40 +02002116 let g:pos = []
2117 call feedkeys(":12345678\<C-R>=setcmdpos(3)??''\<CR>\<Left>\<Esc>", 'xt')
zeertzjq81456202024-07-07 20:48:25 +02002118 call assert_equal([2, 3, 4, 5, 6, 7, 8, 9, 3, 2], g:pos)
Shougo Matsushitad0952142024-06-20 22:05:16 +02002119 au! CursorMovedC
2120
zeertzjqbc6f9672024-06-21 07:51:40 +02002121 " setcmdpos() is no-op inside an autocommand
2122 au! CursorMovedC : let g:pos += [getcmdpos()] | call setcmdpos(1)
2123 let g:pos = []
2124 call feedkeys(":hello\<Left>\<Left>\<Esc>", 'xt')
zeertzjq81456202024-07-07 20:48:25 +02002125 call assert_equal([2, 3, 4, 5, 6, 5, 4], g:pos)
Shougo Matsushitad0952142024-06-20 22:05:16 +02002126 au! CursorMovedC
zeertzjqbc6f9672024-06-21 07:51:40 +02002127
2128 unlet g:entered
2129 unlet g:left
2130 unlet g:pos
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02002131endfunc
Bram Moolenaar53f0c962017-10-22 14:23:59 +02002132
2133" Test for BufWritePre autocommand that deletes or unloads the buffer.
2134func Test_BufWritePre()
2135 %bwipe
2136 au BufWritePre Xxx1 bunload
2137 au BufWritePre Xxx2 bwipe
2138
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01002139 call writefile(['start of Xxx1', 'test', 'end of Xxx1'], 'Xxx1', 'D')
2140 call writefile(['start of Xxx2', 'test', 'end of Xxx2'], 'Xxx2', 'D')
Bram Moolenaar53f0c962017-10-22 14:23:59 +02002141
2142 edit Xtest
2143 e! Xxx2
2144 bdel Xtest
2145 e Xxx1
2146 " write it, will unload it and give an error msg
Bram Moolenaare2e40752020-09-04 21:18:46 +02002147 call assert_fails('w', 'E203:')
Bram Moolenaar53f0c962017-10-22 14:23:59 +02002148 call assert_equal('Xxx2', bufname('%'))
2149 edit Xtest
2150 e! Xxx2
2151 bwipe Xtest
2152 " write it, will delete the buffer and give an error msg
Bram Moolenaare2e40752020-09-04 21:18:46 +02002153 call assert_fails('w', 'E203:')
Bram Moolenaar53f0c962017-10-22 14:23:59 +02002154 call assert_equal('Xxx1', bufname('%'))
2155 au! BufWritePre
Bram Moolenaar53f0c962017-10-22 14:23:59 +02002156endfunc
2157
2158" Test for BufUnload autocommand that unloads all the other buffers
2159func Test_bufunload_all()
Bram Moolenaarf08b0eb2021-10-16 13:00:14 +01002160 let g:test_is_flaky = 1
Christian Brabandtee17b6f2023-09-09 11:23:50 +02002161 call writefile(['Test file Xxx1'], 'Xxx1', 'D')
2162 call writefile(['Test file Xxx2'], 'Xxx2', 'D')
Bram Moolenaar53f0c962017-10-22 14:23:59 +02002163
Bram Moolenaarc79745a2019-05-20 22:12:34 +02002164 let content =<< trim [CODE]
2165 func UnloadAllBufs()
2166 let i = 1
2167 while i <= bufnr('$')
2168 if i != bufnr('%') && bufloaded(i)
2169 exe i . 'bunload'
2170 endif
2171 let i += 1
2172 endwhile
2173 endfunc
2174 au BufUnload * call UnloadAllBufs()
2175 au VimLeave * call writefile(['Test Finished'], 'Xout')
2176 edit Xxx1
2177 split Xxx2
2178 q
2179 [CODE]
2180
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01002181 call writefile(content, 'Xbunloadtest', 'D')
Bram Moolenaar53f0c962017-10-22 14:23:59 +02002182
2183 call delete('Xout')
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01002184 call system(GetVimCommandClean() .. ' -N --not-a-term -S Xbunloadtest')
Bram Moolenaar53f0c962017-10-22 14:23:59 +02002185 call assert_true(filereadable('Xout'))
2186
Bram Moolenaar53f0c962017-10-22 14:23:59 +02002187 call delete('Xout')
2188endfunc
2189
2190" Some tests for buffer-local autocommands
2191func Test_buflocal_autocmd()
2192 let g:bname = ''
2193 edit xx
2194 au BufLeave <buffer> let g:bname = expand("%")
2195 " here, autocommand for xx should trigger.
2196 " but autocommand shall not apply to buffer named <buffer>.
2197 edit somefile
2198 call assert_equal('xx', g:bname)
2199 let g:bname = ''
2200 " here, autocommand shall be auto-deleted
2201 bwipe xx
2202 " autocmd should not trigger
2203 edit xx
2204 call assert_equal('', g:bname)
2205 " autocmd should not trigger
2206 edit somefile
2207 call assert_equal('', g:bname)
2208 enew
2209 unlet g:bname
2210endfunc
Bram Moolenaar430dc5d2017-11-02 21:04:47 +01002211
2212" Test for "*Cmd" autocommands
2213func Test_Cmd_Autocmds()
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01002214 call writefile(['start of Xxx', "\tabc2", 'end of Xxx'], 'Xxx', 'D')
Bram Moolenaar430dc5d2017-11-02 21:04:47 +01002215
2216 enew!
2217 au BufReadCmd XtestA 0r Xxx|$del
2218 edit XtestA " will read text of Xxd instead
2219 call assert_equal('start of Xxx', getline(1))
2220
2221 au BufWriteCmd XtestA call append(line("$"), "write")
2222 write " will append a line to the file
2223 call assert_equal('write', getline('$'))
Bram Moolenaare2e40752020-09-04 21:18:46 +02002224 call assert_fails('read XtestA', 'E484:') " should not read anything
Bram Moolenaar430dc5d2017-11-02 21:04:47 +01002225 call assert_equal('write', getline(4))
2226
2227 " now we have:
2228 " 1 start of Xxx
2229 " 2 abc2
2230 " 3 end of Xxx
2231 " 4 write
2232
2233 au FileReadCmd XtestB '[r Xxx
2234 2r XtestB " will read Xxx below line 2 instead
2235 call assert_equal('start of Xxx', getline(3))
2236
2237 " now we have:
2238 " 1 start of Xxx
2239 " 2 abc2
2240 " 3 start of Xxx
2241 " 4 abc2
2242 " 5 end of Xxx
2243 " 6 end of Xxx
2244 " 7 write
2245
2246 au FileWriteCmd XtestC '[,']copy $
2247 normal 4GA1
2248 4,5w XtestC " will copy lines 4 and 5 to the end
2249 call assert_equal("\tabc21", getline(8))
Bram Moolenaare2e40752020-09-04 21:18:46 +02002250 call assert_fails('r XtestC', 'E484:') " should not read anything
Bram Moolenaar430dc5d2017-11-02 21:04:47 +01002251 call assert_equal("end of Xxx", getline(9))
2252
2253 " now we have:
2254 " 1 start of Xxx
2255 " 2 abc2
2256 " 3 start of Xxx
2257 " 4 abc21
2258 " 5 end of Xxx
2259 " 6 end of Xxx
2260 " 7 write
2261 " 8 abc21
2262 " 9 end of Xxx
2263
2264 let g:lines = []
2265 au FileAppendCmd XtestD call extend(g:lines, getline(line("'["), line("']")))
2266 w >>XtestD " will add lines to 'lines'
2267 call assert_equal(9, len(g:lines))
Bram Moolenaare2e40752020-09-04 21:18:46 +02002268 call assert_fails('$r XtestD', 'E484:') " should not read anything
Bram Moolenaar430dc5d2017-11-02 21:04:47 +01002269 call assert_equal(9, line('$'))
2270 call assert_equal('end of Xxx', getline('$'))
2271
2272 au BufReadCmd XtestE 0r Xxx|$del
2273 sp XtestE " split window with test.out
2274 call assert_equal('end of Xxx', getline(3))
2275
2276 let g:lines = []
2277 exe "normal 2Goasdf\<Esc>\<C-W>\<C-W>"
2278 au BufWriteCmd XtestE call extend(g:lines, getline(0, '$'))
2279 wall " will write other window to 'lines'
2280 call assert_equal(4, len(g:lines), g:lines)
2281 call assert_equal('asdf', g:lines[2])
2282
2283 au! BufReadCmd
2284 au! BufWriteCmd
2285 au! FileReadCmd
2286 au! FileWriteCmd
2287 au! FileAppendCmd
2288 %bwipe!
Bram Moolenaar430dc5d2017-11-02 21:04:47 +01002289 enew!
2290endfunc
Bram Moolenaaraace2152017-11-05 16:23:10 +01002291
Bram Moolenaar0fff4412020-03-29 16:06:29 +02002292func s:ReadFile()
2293 setl noswapfile nomodified
2294 let filename = resolve(expand("<afile>:p"))
2295 execute 'read' fnameescape(filename)
2296 1d_
2297 exe 'file' fnameescape(filename)
2298 setl buftype=acwrite
2299endfunc
2300
2301func s:WriteFile()
2302 let filename = resolve(expand("<afile>:p"))
2303 setl buftype=
2304 noautocmd execute 'write' fnameescape(filename)
2305 setl buftype=acwrite
2306 setl nomodified
2307endfunc
2308
2309func Test_BufReadCmd()
2310 autocmd BufReadCmd *.test call s:ReadFile()
2311 autocmd BufWriteCmd *.test call s:WriteFile()
2312
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01002313 call writefile(['one', 'two', 'three'], 'Xcmd.test', 'D')
Bram Moolenaar0fff4412020-03-29 16:06:29 +02002314 edit Xcmd.test
2315 call assert_match('Xcmd.test" line 1 of 3', execute('file'))
2316 normal! Gofour
2317 write
2318 call assert_equal(['one', 'two', 'three', 'four'], readfile('Xcmd.test'))
2319
2320 bwipe!
Bram Moolenaar0fff4412020-03-29 16:06:29 +02002321 au! BufReadCmd
2322 au! BufWriteCmd
2323endfunc
2324
zeertzjq9c8f9462022-08-30 18:17:15 +01002325func Test_BufWriteCmd()
2326 autocmd BufWriteCmd Xbufwritecmd let g:written = 1
2327 new
2328 file Xbufwritecmd
2329 set buftype=acwrite
Bram Moolenaar6f14da12022-09-07 21:30:44 +01002330 call mkdir('Xbufwritecmd', 'D')
zeertzjq9c8f9462022-08-30 18:17:15 +01002331 write
2332 " BufWriteCmd should be triggered even if a directory has the same name
2333 call assert_equal(1, g:written)
zeertzjq9c8f9462022-08-30 18:17:15 +01002334 unlet g:written
2335 au! BufWriteCmd
2336 bwipe!
2337endfunc
2338
Bram Moolenaaraace2152017-11-05 16:23:10 +01002339func SetChangeMarks(start, end)
Bram Moolenaar97c69432021-01-15 16:45:21 +01002340 exe a:start .. 'mark ['
2341 exe a:end .. 'mark ]'
Bram Moolenaaraace2152017-11-05 16:23:10 +01002342endfunc
2343
2344" Verify the effects of autocmds on '[ and ']
2345func Test_change_mark_in_autocmds()
2346 edit! Xtest
Bram Moolenaar97c69432021-01-15 16:45:21 +01002347 call feedkeys("ia\<CR>b\<CR>c\<CR>d\<C-g>u\<Esc>", 'xtn')
Bram Moolenaaraace2152017-11-05 16:23:10 +01002348
2349 call SetChangeMarks(2, 3)
2350 write
2351 call assert_equal([1, 4], [line("'["), line("']")])
2352
2353 call SetChangeMarks(2, 3)
2354 au BufWritePre * call assert_equal([1, 4], [line("'["), line("']")])
2355 write
2356 au! BufWritePre
2357
Bram Moolenaar14ddd222020-08-05 12:02:40 +02002358 if has('unix')
Bram Moolenaaraace2152017-11-05 16:23:10 +01002359 write XtestFilter
2360 write >> XtestFilter
2361
2362 call SetChangeMarks(2, 3)
2363 " Marks are set to the entire range of the write
2364 au FilterWritePre * call assert_equal([1, 4], [line("'["), line("']")])
2365 " '[ is adjusted to just before the line that will receive the filtered
2366 " data
2367 au FilterReadPre * call assert_equal([4, 4], [line("'["), line("']")])
2368 " The filtered data is read into the buffer, and the source lines are
2369 " still present, so the range is after the source lines
2370 au FilterReadPost * call assert_equal([5, 12], [line("'["), line("']")])
2371 %!cat XtestFilter
2372 " After the filtered data is read, the original lines are deleted
2373 call assert_equal([1, 8], [line("'["), line("']")])
2374 au! FilterWritePre,FilterReadPre,FilterReadPost
2375 undo
2376
2377 call SetChangeMarks(1, 4)
2378 au FilterWritePre * call assert_equal([2, 3], [line("'["), line("']")])
2379 au FilterReadPre * call assert_equal([3, 3], [line("'["), line("']")])
2380 au FilterReadPost * call assert_equal([4, 11], [line("'["), line("']")])
2381 2,3!cat XtestFilter
2382 call assert_equal([2, 9], [line("'["), line("']")])
2383 au! FilterWritePre,FilterReadPre,FilterReadPost
2384 undo
2385
2386 call delete('XtestFilter')
2387 endif
2388
2389 call SetChangeMarks(1, 4)
2390 au FileWritePre * call assert_equal([2, 3], [line("'["), line("']")])
2391 2,3write Xtest2
2392 au! FileWritePre
2393
2394 call SetChangeMarks(2, 3)
2395 au FileAppendPre * call assert_equal([1, 4], [line("'["), line("']")])
2396 write >> Xtest2
2397 au! FileAppendPre
2398
2399 call SetChangeMarks(1, 4)
2400 au FileAppendPre * call assert_equal([2, 3], [line("'["), line("']")])
2401 2,3write >> Xtest2
2402 au! FileAppendPre
2403
2404 call SetChangeMarks(1, 1)
2405 au FileReadPre * call assert_equal([3, 1], [line("'["), line("']")])
2406 au FileReadPost * call assert_equal([4, 11], [line("'["), line("']")])
2407 3read Xtest2
2408 au! FileReadPre,FileReadPost
2409 undo
2410
2411 call SetChangeMarks(4, 4)
2412 " When the line is 0, it's adjusted to 1
2413 au FileReadPre * call assert_equal([1, 4], [line("'["), line("']")])
2414 au FileReadPost * call assert_equal([1, 8], [line("'["), line("']")])
2415 0read Xtest2
2416 au! FileReadPre,FileReadPost
2417 undo
2418
2419 call SetChangeMarks(4, 4)
2420 " When the line is 0, it's adjusted to 1
2421 au FileReadPre * call assert_equal([1, 4], [line("'["), line("']")])
2422 au FileReadPost * call assert_equal([2, 9], [line("'["), line("']")])
2423 1read Xtest2
2424 au! FileReadPre,FileReadPost
2425 undo
2426
2427 bwipe!
2428 call delete('Xtest')
2429 call delete('Xtest2')
2430endfunc
2431
2432func Test_Filter_noshelltemp()
Bram Moolenaaraeb313f2020-11-27 19:13:28 +01002433 CheckExecutable cat
Bram Moolenaaraace2152017-11-05 16:23:10 +01002434
2435 enew!
2436 call setline(1, ['a', 'b', 'c', 'd'])
2437
2438 let shelltemp = &shelltemp
2439 set shelltemp
2440
2441 let g:filter_au = 0
2442 au FilterWritePre * let g:filter_au += 1
2443 au FilterReadPre * let g:filter_au += 1
2444 au FilterReadPost * let g:filter_au += 1
2445 %!cat
2446 call assert_equal(3, g:filter_au)
2447
2448 if has('filterpipe')
2449 set noshelltemp
2450
2451 let g:filter_au = 0
2452 au FilterWritePre * let g:filter_au += 1
2453 au FilterReadPre * let g:filter_au += 1
2454 au FilterReadPost * let g:filter_au += 1
2455 %!cat
2456 call assert_equal(0, g:filter_au)
2457 endif
2458
2459 au! FilterWritePre,FilterReadPre,FilterReadPost
2460 let &shelltemp = shelltemp
2461 bwipe!
2462endfunc
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01002463
2464func Test_TextYankPost()
2465 enew!
2466 call setline(1, ['foo'])
2467
2468 let g:event = []
2469 au TextYankPost * let g:event = copy(v:event)
2470
2471 call assert_equal({}, v:event)
2472 call assert_fails('let v:event = {}', 'E46:')
2473 call assert_fails('let v:event.mykey = 0', 'E742:')
2474
2475 norm "ayiw
2476 call assert_equal(
Bram Moolenaara016eeb2022-04-09 11:37:38 +01002477 \ #{regcontents: ['foo'], regname: 'a', operator: 'y',
2478 \ regtype: 'v', visual: v:false, inclusive: v:true},
2479 \ g:event)
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01002480 norm y_
2481 call assert_equal(
Bram Moolenaara016eeb2022-04-09 11:37:38 +01002482 \ #{regcontents: ['foo'], regname: '', operator: 'y', regtype: 'V',
2483 \ visual: v:false, inclusive: v:false},
2484 \ g:event)
Bram Moolenaar37d16732020-06-12 22:09:01 +02002485 norm Vy
2486 call assert_equal(
Bram Moolenaara016eeb2022-04-09 11:37:38 +01002487 \ #{regcontents: ['foo'], regname: '', operator: 'y', regtype: 'V',
2488 \ visual: v:true, inclusive: v:true},
2489 \ g:event)
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01002490 call feedkeys("\<C-V>y", 'x')
2491 call assert_equal(
Bram Moolenaara016eeb2022-04-09 11:37:38 +01002492 \ #{regcontents: ['f'], regname: '', operator: 'y', regtype: "\x161",
2493 \ visual: v:true, inclusive: v:true},
2494 \ g:event)
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01002495 norm "xciwbar
2496 call assert_equal(
Bram Moolenaara016eeb2022-04-09 11:37:38 +01002497 \ #{regcontents: ['foo'], regname: 'x', operator: 'c', regtype: 'v',
2498 \ visual: v:false, inclusive: v:true},
2499 \ g:event)
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01002500 norm "bdiw
2501 call assert_equal(
Bram Moolenaara016eeb2022-04-09 11:37:38 +01002502 \ #{regcontents: ['bar'], regname: 'b', operator: 'd', regtype: 'v',
2503 \ visual: v:false, inclusive: v:true},
2504 \ g:event)
2505
2506 call setline(1, 'foobar')
2507 " exclusive motion
2508 norm $"ay0
2509 call assert_equal(
2510 \ #{regcontents: ['fooba'], regname: 'a', operator: 'y', regtype: 'v',
2511 \ visual: v:false, inclusive: v:false},
2512 \ g:event)
2513 " inclusive motion
2514 norm 0"ay$
2515 call assert_equal(
2516 \ #{regcontents: ['foobar'], regname: 'a', operator: 'y', regtype: 'v',
2517 \ visual: v:false, inclusive: v:true},
2518 \ g:event)
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01002519
2520 call assert_equal({}, v:event)
2521
Bram Moolenaarfccbf062020-11-26 20:34:00 +01002522 if has('clipboard_working') && !has('gui_running')
2523 " Test that when the visual selection is automatically copied to clipboard
2524 " register a TextYankPost is emitted
2525 call setline(1, ['foobar'])
2526
2527 let @* = ''
2528 set clipboard=autoselect
2529 exe "norm! ggviw\<Esc>"
2530 call assert_equal(
Bram Moolenaara016eeb2022-04-09 11:37:38 +01002531 \ #{regcontents: ['foobar'], regname: '*', operator: 'y',
2532 \ regtype: 'v', visual: v:true, inclusive: v:false},
2533 \ g:event)
Bram Moolenaarfccbf062020-11-26 20:34:00 +01002534
2535 let @+ = ''
2536 set clipboard=autoselectplus
2537 exe "norm! ggviw\<Esc>"
2538 call assert_equal(
Bram Moolenaara016eeb2022-04-09 11:37:38 +01002539 \ #{regcontents: ['foobar'], regname: '+', operator: 'y',
2540 \ regtype: 'v', visual: v:true, inclusive: v:false},
2541 \ g:event)
Bram Moolenaarfccbf062020-11-26 20:34:00 +01002542
2543 set clipboard&vim
2544 endif
2545
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01002546 au! TextYankPost
2547 unlet g:event
2548 bwipe!
2549endfunc
Bram Moolenaar9bca8052017-12-18 12:37:55 +01002550
Bram Moolenaar9a046fd2021-01-28 13:47:59 +01002551func Test_autocommand_all_events()
2552 call assert_fails('au * * bwipe', 'E1155:')
2553 call assert_fails('au * x bwipe', 'E1155:')
Bram Moolenaarb6db1462021-12-24 19:24:47 +00002554 call assert_fails('au! * x bwipe', 'E1155:')
Bram Moolenaar4fb921e2017-12-18 15:33:00 +01002555endfunc
Bram Moolenaarb7407d32018-02-03 17:36:27 +01002556
Bram Moolenaarf6246f52022-02-11 16:30:12 +00002557func Test_autocmd_user()
2558 au User MyEvent let s:res = [expand("<afile>"), expand("<amatch>")]
2559 doautocmd User MyEvent
2560 call assert_equal(['MyEvent', 'MyEvent'], s:res)
2561 au! User
2562 unlet s:res
2563endfunc
2564
Bram Moolenaar3b014be2022-11-13 17:53:46 +00002565func Test_autocmd_user_clear_group()
2566 CheckRunVimInTerminal
2567
2568 let lines =<< trim END
2569 autocmd! User
2570 for i in range(1, 999)
2571 exe 'autocmd User ' .. 'Foo' .. i .. ' bar'
2572 endfor
2573 au CmdlineLeave : call timer_start(0, {-> execute('autocmd! User')})
2574 END
2575 call writefile(lines, 'XautoUser', 'D')
2576 let buf = RunVimInTerminal('-S XautoUser', {'rows': 10})
2577
2578 " this was using freed memory
2579 call term_sendkeys(buf, ":autocmd User\<CR>")
2580 call TermWait(buf, 50)
2581 call term_sendkeys(buf, "G")
2582
2583 call StopVimInTerminal(buf)
2584endfunc
2585
Bram Moolenaaref2c3252022-11-25 16:31:51 +00002586func Test_autocmd_CmdlineLeave_unlet()
2587 CheckRunVimInTerminal
2588
2589 let lines =<< trim END
2590 for i in range(1, 999)
2591 exe 'let g:var' .. i '=' i
2592 endfor
2593 au CmdlineLeave : call timer_start(0, {-> execute('unlet g:var990')})
2594 END
2595 call writefile(lines, 'XleaveUnlet', 'D')
2596 let buf = RunVimInTerminal('-S XleaveUnlet', {'rows': 10})
2597
2598 " this was using freed memory
2599 call term_sendkeys(buf, ":let g:\<CR>")
2600 call TermWait(buf, 50)
2601 call term_sendkeys(buf, "G")
2602 call TermWait(buf, 50)
2603 call term_sendkeys(buf, "\<CR>") " for the hit-enter prompt
2604
2605 call StopVimInTerminal(buf)
2606endfunc
2607
Bram Moolenaarb7407d32018-02-03 17:36:27 +01002608function s:Before_test_dirchanged()
2609 augroup test_dirchanged
2610 autocmd!
2611 augroup END
2612 let s:li = []
2613 let s:dir_this = getcwd()
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02002614 let s:dir_foo = s:dir_this . '/Xfoo'
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01002615 call mkdir(s:dir_foo)
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02002616 let s:dir_bar = s:dir_this . '/Xbar'
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01002617 call mkdir(s:dir_bar)
Bram Moolenaarb7407d32018-02-03 17:36:27 +01002618endfunc
2619
2620function s:After_test_dirchanged()
Bram Moolenaar3503d7c2019-11-09 20:10:17 +01002621 call chdir(s:dir_this)
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01002622 call delete(s:dir_foo, 'd')
2623 call delete(s:dir_bar, 'd')
Bram Moolenaarb7407d32018-02-03 17:36:27 +01002624 augroup test_dirchanged
2625 autocmd!
2626 augroup END
2627endfunc
2628
2629function Test_dirchanged_global()
2630 call s:Before_test_dirchanged()
Bram Moolenaarf6246f52022-02-11 16:30:12 +00002631 autocmd test_dirchanged DirChangedPre global call add(s:li, expand("<amatch>") .. " pre cd " .. v:event.directory)
Bram Moolenaarb7407d32018-02-03 17:36:27 +01002632 autocmd test_dirchanged DirChanged global call add(s:li, "cd:")
2633 autocmd test_dirchanged DirChanged global call add(s:li, expand("<afile>"))
Bram Moolenaar3503d7c2019-11-09 20:10:17 +01002634 call chdir(s:dir_foo)
Bram Moolenaarf6246f52022-02-11 16:30:12 +00002635 let expected = ["global pre cd " .. s:dir_foo, "cd:", s:dir_foo]
Bram Moolenaar28e8f732022-02-09 12:58:20 +00002636 call assert_equal(expected, s:li)
Bram Moolenaar3503d7c2019-11-09 20:10:17 +01002637 call chdir(s:dir_foo)
Bram Moolenaar28e8f732022-02-09 12:58:20 +00002638 call assert_equal(expected, s:li)
Bram Moolenaar3503d7c2019-11-09 20:10:17 +01002639 exe 'lcd ' .. fnameescape(s:dir_bar)
Bram Moolenaar28e8f732022-02-09 12:58:20 +00002640 call assert_equal(expected, s:li)
Bram Moolenaard8c9d322022-06-12 11:49:16 +01002641
2642 exe 'cd ' .. s:dir_foo
2643 exe 'cd ' .. s:dir_bar
2644 autocmd! test_dirchanged DirChanged global let g:result = expand("<afile>")
2645 cd -
Bram Moolenaardb77c492022-06-12 23:26:50 +01002646 call assert_equal(s:dir_foo, substitute(g:result, '\\', '/', 'g'))
Bram Moolenaard8c9d322022-06-12 11:49:16 +01002647
Bram Moolenaarb7407d32018-02-03 17:36:27 +01002648 call s:After_test_dirchanged()
2649endfunc
2650
2651function Test_dirchanged_local()
2652 call s:Before_test_dirchanged()
2653 autocmd test_dirchanged DirChanged window call add(s:li, "lcd:")
2654 autocmd test_dirchanged DirChanged window call add(s:li, expand("<afile>"))
Bram Moolenaar3503d7c2019-11-09 20:10:17 +01002655 call chdir(s:dir_foo)
Bram Moolenaarb7407d32018-02-03 17:36:27 +01002656 call assert_equal([], s:li)
Bram Moolenaar3503d7c2019-11-09 20:10:17 +01002657 exe 'lcd ' .. fnameescape(s:dir_bar)
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01002658 call assert_equal(["lcd:", s:dir_bar], s:li)
Bram Moolenaar3503d7c2019-11-09 20:10:17 +01002659 exe 'lcd ' .. fnameescape(s:dir_bar)
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01002660 call assert_equal(["lcd:", s:dir_bar], s:li)
Bram Moolenaarb7407d32018-02-03 17:36:27 +01002661 call s:After_test_dirchanged()
2662endfunc
2663
2664function Test_dirchanged_auto()
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +02002665 CheckOption autochdir
Bram Moolenaarb7407d32018-02-03 17:36:27 +01002666 call s:Before_test_dirchanged()
2667 call test_autochdir()
Bram Moolenaar28e8f732022-02-09 12:58:20 +00002668 autocmd test_dirchanged DirChangedPre auto call add(s:li, "pre cd " .. v:event.directory)
Bram Moolenaarb7407d32018-02-03 17:36:27 +01002669 autocmd test_dirchanged DirChanged auto call add(s:li, "auto:")
2670 autocmd test_dirchanged DirChanged auto call add(s:li, expand("<afile>"))
2671 set acd
Bram Moolenaar3503d7c2019-11-09 20:10:17 +01002672 cd ..
Bram Moolenaarb7407d32018-02-03 17:36:27 +01002673 call assert_equal([], s:li)
Bram Moolenaar61abe7d2022-08-30 21:46:08 +01002674 exe 'edit ' . s:dir_foo . '/Xautofile'
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01002675 call assert_equal(s:dir_foo, getcwd())
Bram Moolenaar28e8f732022-02-09 12:58:20 +00002676 let expected = ["pre cd " .. s:dir_foo, "auto:", s:dir_foo]
2677 call assert_equal(expected, s:li)
Bram Moolenaarb7407d32018-02-03 17:36:27 +01002678 set noacd
2679 bwipe!
2680 call s:After_test_dirchanged()
2681endfunc
Bram Moolenaar5a093432018-02-10 18:15:19 +01002682
2683" Test TextChangedI and TextChangedP
2684func Test_ChangedP()
2685 new
2686 call setline(1, ['foo', 'bar', 'foobar'])
2687 call test_override("char_avail", 1)
2688 set complete=. completeopt=menuone
2689
2690 func! TextChangedAutocmd(char)
2691 let g:autocmd .= a:char
2692 endfunc
2693
Christian Brabandtdb3b4462021-10-16 11:58:55 +01002694 " TextChanged will not be triggered, only check that it isn't.
Bram Moolenaar5a093432018-02-10 18:15:19 +01002695 au! TextChanged <buffer> :call TextChangedAutocmd('N')
2696 au! TextChangedI <buffer> :call TextChangedAutocmd('I')
2697 au! TextChangedP <buffer> :call TextChangedAutocmd('P')
2698
2699 call cursor(3, 1)
2700 let g:autocmd = ''
2701 call feedkeys("o\<esc>", 'tnix')
Evgeni Chasnovskid7ae2632023-10-15 09:59:00 +02002702 call assert_equal('I', g:autocmd)
Bram Moolenaar5a093432018-02-10 18:15:19 +01002703
2704 let g:autocmd = ''
Christian Brabandt4bca4892023-10-27 19:26:49 +02002705 call feedkeys("Sf", 'tnix')
2706 call assert_equal('II', g:autocmd)
2707
2708 let g:autocmd = ''
Bram Moolenaar5a093432018-02-10 18:15:19 +01002709 call feedkeys("Sf\<C-N>", 'tnix')
Christian Brabandt4bca4892023-10-27 19:26:49 +02002710 call assert_equal('IIP', g:autocmd)
Bram Moolenaar5a093432018-02-10 18:15:19 +01002711
2712 let g:autocmd = ''
2713 call feedkeys("Sf\<C-N>\<C-N>", 'tnix')
Christian Brabandt4bca4892023-10-27 19:26:49 +02002714 call assert_equal('IIPP', g:autocmd)
Bram Moolenaar5a093432018-02-10 18:15:19 +01002715
2716 let g:autocmd = ''
2717 call feedkeys("Sf\<C-N>\<C-N>\<C-N>", 'tnix')
Christian Brabandt4bca4892023-10-27 19:26:49 +02002718 call assert_equal('IIPPP', g:autocmd)
Bram Moolenaar5a093432018-02-10 18:15:19 +01002719
2720 let g:autocmd = ''
2721 call feedkeys("Sf\<C-N>\<C-N>\<C-N>\<C-N>", 'tnix')
Christian Brabandt4bca4892023-10-27 19:26:49 +02002722 call assert_equal('IIPPPP', g:autocmd)
Bram Moolenaar5a093432018-02-10 18:15:19 +01002723
2724 call assert_equal(['foo', 'bar', 'foobar', 'foo'], getline(1, '$'))
2725 " TODO: how should it handle completeopt=noinsert,noselect?
2726
2727 " CleanUp
2728 call test_override("char_avail", 0)
2729 au! TextChanged
2730 au! TextChangedI
2731 au! TextChangedP
2732 delfu TextChangedAutocmd
2733 unlet! g:autocmd
2734 set complete&vim completeopt&vim
2735
2736 bw!
2737endfunc
Bram Moolenaar8c64a362018-03-23 22:39:31 +01002738
Bram Moolenaar91d2e782018-08-07 19:05:01 +02002739let g:setline_handled = v:false
Bram Moolenaar1e115362019-01-09 23:01:02 +01002740func SetLineOne()
Bram Moolenaar91d2e782018-08-07 19:05:01 +02002741 if !g:setline_handled
2742 call setline(1, "(x)")
2743 let g:setline_handled = v:true
2744 endif
2745endfunc
2746
2747func Test_TextChangedI_with_setline()
2748 new
2749 call test_override('char_avail', 1)
2750 autocmd TextChangedI <buffer> call SetLineOne()
2751 call feedkeys("i(\<CR>\<Esc>", 'tx')
2752 call assert_equal('(', getline(1))
2753 call assert_equal('x)', getline(2))
2754 undo
Bram Moolenaar91d2e782018-08-07 19:05:01 +02002755 call assert_equal('', getline(1))
Bram Moolenaar9fa95062018-08-08 22:08:32 +02002756 call assert_equal('', getline(2))
Bram Moolenaar91d2e782018-08-07 19:05:01 +02002757
Bram Moolenaarca34db32022-01-20 11:17:18 +00002758 call test_override('char_avail', 0)
Bram Moolenaar91d2e782018-08-07 19:05:01 +02002759 bwipe!
2760endfunc
2761
Christian Brabandtc9e79e52024-02-09 19:34:36 +01002762func Test_TextChanged_with_norm()
2763 " For unknown reason this fails on MS-Windows
2764 CheckNotMSWindows
2765 CheckFeature terminal
2766 let buf = term_start([GetVimProg(), '--clean', '-c', 'set noswapfile'], {'term_rows': 3})
2767 call assert_equal('running', term_getstatus(buf))
2768 call term_sendkeys(buf, ":let g:a=0\<cr>")
2769 call term_wait(buf, 50)
2770 call term_sendkeys(buf, ":au! TextChanged * :let g:a+=1\<cr>")
2771 call term_wait(buf, 50)
2772 call term_sendkeys(buf, ":norm! ia\<cr>")
2773 call term_wait(buf, 50)
2774 call term_sendkeys(buf, ":echo g:a\<cr>")
2775 call term_wait(buf, 50)
2776 call WaitForAssert({-> assert_match('^1.*$', term_getline(buf, 3))})
2777 bwipe!
2778endfunc
2779
Bram Moolenaar8c64a362018-03-23 22:39:31 +01002780func Test_Changed_FirstTime()
Bram Moolenaar8c5a2782019-08-07 23:07:07 +02002781 CheckFeature terminal
2782 CheckNotGui
Bram Moolenaar3cdcb092020-03-18 19:18:10 +01002783 " Starting a terminal to run Vim is always considered flaky.
Bram Moolenaar30d53e22020-03-18 21:10:44 +01002784 let g:test_is_flaky = 1
Bram Moolenaar8c5a2782019-08-07 23:07:07 +02002785
Bram Moolenaar8c64a362018-03-23 22:39:31 +01002786 " Prepare file for TextChanged event.
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01002787 call writefile([''], 'Xchanged.txt', 'D')
Bram Moolenaar8c64a362018-03-23 22:39:31 +01002788 let buf = term_start([GetVimProg(), '--clean', '-c', 'set noswapfile'], {'term_rows': 3})
2789 call assert_equal('running', term_getstatus(buf))
Bram Moolenaar1834d372018-03-29 17:40:46 +02002790 " Wait for the ruler (in the status line) to be shown.
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01002791 " In ConPTY, there is additional character which is drawn up to the width of
2792 " the screen.
2793 if has('conpty')
2794 call WaitForAssert({-> assert_match('\<All.*$', term_getline(buf, 3))})
2795 else
2796 call WaitForAssert({-> assert_match('\<All$', term_getline(buf, 3))})
2797 endif
Bram Moolenaar8c64a362018-03-23 22:39:31 +01002798 " It's only adding autocmd, so that no event occurs.
2799 call term_sendkeys(buf, ":au! TextChanged <buffer> call writefile(['No'], 'Xchanged.txt')\<cr>")
2800 call term_sendkeys(buf, "\<C-\\>\<C-N>:qa!\<cr>")
Bram Moolenaar50182fa2018-04-28 21:34:40 +02002801 call WaitForAssert({-> assert_equal('finished', term_getstatus(buf))})
Bram Moolenaar8c64a362018-03-23 22:39:31 +01002802 call assert_equal([''], readfile('Xchanged.txt'))
2803
2804 " clean up
Bram Moolenaar8c64a362018-03-23 22:39:31 +01002805 bwipe!
2806endfunc
Bram Moolenaar0566e892019-01-24 19:37:40 +01002807
Bram Moolenaareb93f3f2019-04-04 15:04:56 +02002808func Test_autocmd_nested()
2809 let g:did_nested = 0
Christian Brabandtfb3f9692024-08-11 20:09:17 +02002810 defer CleanUpTestAuGroup()
2811 augroup testing
Bram Moolenaareb93f3f2019-04-04 15:04:56 +02002812 au WinNew * edit somefile
2813 au BufNew * let g:did_nested = 1
2814 augroup END
2815 split
2816 call assert_equal(0, g:did_nested)
2817 close
2818 bwipe! somefile
2819
2820 " old nested argument still works
Christian Brabandtfb3f9692024-08-11 20:09:17 +02002821 augroup testing
Bram Moolenaareb93f3f2019-04-04 15:04:56 +02002822 au!
2823 au WinNew * nested edit somefile
2824 au BufNew * let g:did_nested = 1
2825 augroup END
2826 split
2827 call assert_equal(1, g:did_nested)
2828 close
2829 bwipe! somefile
2830
2831 " New ++nested argument works
2832 augroup Testing
2833 au!
2834 au WinNew * ++nested edit somefile
2835 au BufNew * let g:did_nested = 1
2836 augroup END
2837 split
2838 call assert_equal(1, g:did_nested)
2839 close
2840 bwipe! somefile
2841
Bram Moolenaarf0775142022-03-04 20:10:38 +00002842 " nested without ++ does not work in Vim9 script
2843 call assert_fails('vim9cmd au WinNew * nested echo fails', 'E1078:')
2844
Bram Moolenaareb93f3f2019-04-04 15:04:56 +02002845 augroup Testing
2846 au!
2847 augroup END
2848
2849 call assert_fails('au WinNew * ++nested ++nested echo bad', 'E983:')
2850 call assert_fails('au WinNew * nested nested echo bad', 'E983:')
2851endfunc
2852
Bram Moolenaar5fa9f232022-07-23 09:06:48 +01002853func Test_autocmd_nested_cursor_invalid()
2854 set laststatus=0
2855 copen
2856 cclose
2857 call setline(1, ['foo', 'bar', 'baz'])
2858 3
2859 augroup nested_inv
2860 autocmd User foo ++nested copen
2861 autocmd BufAdd * let &laststatus = 2 - &laststatus
2862 augroup END
2863 doautocmd User foo
2864
2865 augroup nested_inv
2866 au!
2867 augroup END
2868 set laststatus&
Bram Moolenaarb03950f2022-07-26 13:47:13 +01002869 cclose
Bram Moolenaar5fa9f232022-07-23 09:06:48 +01002870 bwipe!
2871endfunc
2872
Bram Moolenaar3d6ee8b2022-07-27 15:23:35 +01002873func Test_autocmd_nested_keeps_cursor_pos()
2874 enew
2875 call setline(1, 'foo')
2876 autocmd User foo ++nested normal! $a
2877 autocmd InsertLeave * :
2878 doautocmd User foo
2879 call assert_equal([0, 1, 3, 0], getpos('.'))
2880
2881 bwipe!
2882endfunc
2883
Bram Moolenaarb03950f2022-07-26 13:47:13 +01002884func Test_autocmd_nested_switch_window()
2885 " run this in a separate Vim so that SafeState works
2886 CheckRunVimInTerminal
2887
2888 let lines =<< trim END
2889 vim9script
2890 ['()']->writefile('Xautofile')
2891 autocmd VimEnter * ++nested edit Xautofile | split
2892 autocmd BufReadPost * autocmd SafeState * ++once foldclosed('.')
2893 autocmd WinEnter * matchadd('ErrorMsg', 'pat')
2894 END
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01002895 call writefile(lines, 'Xautoscript', 'D')
Bram Moolenaarb03950f2022-07-26 13:47:13 +01002896 let buf = RunVimInTerminal('-S Xautoscript', {'rows': 10})
2897 call VerifyScreenDump(buf, 'Test_autocmd_nested_switch', {})
2898
2899 call StopVimInTerminal(buf)
2900 call delete('Xautofile')
Bram Moolenaarb03950f2022-07-26 13:47:13 +01002901endfunc
2902
Bram Moolenaareb93f3f2019-04-04 15:04:56 +02002903func Test_autocmd_once()
2904 " Without ++once WinNew triggers twice
2905 let g:did_split = 0
2906 augroup Testing
2907 au WinNew * let g:did_split += 1
2908 augroup END
2909 split
2910 split
2911 call assert_equal(2, g:did_split)
2912 call assert_true(exists('#WinNew'))
2913 close
2914 close
2915
2916 " With ++once WinNew triggers once
2917 let g:did_split = 0
2918 augroup Testing
2919 au!
2920 au WinNew * ++once let g:did_split += 1
2921 augroup END
2922 split
2923 split
2924 call assert_equal(1, g:did_split)
2925 call assert_false(exists('#WinNew'))
2926 close
2927 close
2928
2929 call assert_fails('au WinNew * ++once ++once echo bad', 'E983:')
2930endfunc
2931
Bram Moolenaara68e5952019-04-25 22:22:01 +02002932func Test_autocmd_bufreadpre()
2933 new
2934 let b:bufreadpre = 1
Bram Moolenaarab505b12020-03-23 19:28:44 +01002935 call append(0, range(1000))
Bram Moolenaara68e5952019-04-25 22:22:01 +02002936 w! XAutocmdBufReadPre.txt
2937 autocmd BufReadPre <buffer> :let b:bufreadpre += 1
Bram Moolenaarab505b12020-03-23 19:28:44 +01002938 norm! 500gg
Bram Moolenaara68e5952019-04-25 22:22:01 +02002939 sp
Bram Moolenaarab505b12020-03-23 19:28:44 +01002940 norm! 1000gg
Bram Moolenaara68e5952019-04-25 22:22:01 +02002941 wincmd p
2942 let g:wsv1 = winsaveview()
2943 wincmd p
2944 let g:wsv2 = winsaveview()
2945 " triggers BufReadPre, should not move the cursor in either window
2946 " The topline may change one line in a large window.
2947 edit
2948 call assert_inrange(g:wsv2.topline - 1, g:wsv2.topline + 1, winsaveview().topline)
2949 call assert_equal(g:wsv2.lnum, winsaveview().lnum)
2950 call assert_equal(2, b:bufreadpre)
2951 wincmd p
2952 call assert_equal(g:wsv1.topline, winsaveview().topline)
2953 call assert_equal(g:wsv1.lnum, winsaveview().lnum)
2954 call assert_equal(2, b:bufreadpre)
2955 " Now set the cursor position in an BufReadPre autocommand
2956 " (even though the position will be invalid, this should make Vim reset the
2957 " cursor position in the other window.
2958 wincmd p
2959 set cpo+=g
2960 " won't do anything, but try to set the cursor on an invalid lnum
2961 autocmd BufReadPre <buffer> :norm! 70gg
2962 " triggers BufReadPre, should not move the cursor in either window
2963 e
2964 call assert_equal(1, winsaveview().topline)
2965 call assert_equal(1, winsaveview().lnum)
2966 call assert_equal(3, b:bufreadpre)
2967 wincmd p
2968 call assert_equal(g:wsv1.topline, winsaveview().topline)
2969 call assert_equal(g:wsv1.lnum, winsaveview().lnum)
2970 call assert_equal(3, b:bufreadpre)
2971 close
2972 close
2973 call delete('XAutocmdBufReadPre.txt')
2974 set cpo-=g
2975endfunc
2976
Bram Moolenaar5e66b422019-01-24 21:58:10 +01002977" FileChangedShell tested in test_filechanged.vim
Bram Moolenaar69ea5872019-04-25 20:29:00 +02002978
2979" Tests for the following autocommands:
2980" - FileWritePre writing a compressed file
2981" - FileReadPost reading a compressed file
2982" - BufNewFile reading a file template
2983" - BufReadPre decompressing the file to be read
2984" - FilterReadPre substituting characters in the temp file
2985" - FilterReadPost substituting characters after filtering
2986" - FileReadPre set options for decompression
2987" - FileReadPost decompress the file
2988func Test_ReadWrite_Autocmds()
2989 " Run this test only on Unix-like systems and if gzip is available
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +02002990 CheckUnix
2991 CheckExecutable gzip
Bram Moolenaar69ea5872019-04-25 20:29:00 +02002992
2993 " Make $GZIP empty, "-v" would cause trouble.
2994 let $GZIP = ""
2995
2996 " Use a FileChangedShell autocommand to avoid a prompt for 'Xtestfile.gz'
2997 " being modified outside of Vim (noticed on Solaris).
2998 au FileChangedShell * echo 'caught FileChangedShell'
2999
3000 " Test for the FileReadPost, FileWritePre and FileWritePost autocmds
3001 augroup Test1
3002 au!
3003 au FileWritePre *.gz '[,']!gzip
3004 au FileWritePost *.gz undo
3005 au FileReadPost *.gz '[,']!gzip -d
3006 augroup END
3007
3008 new
3009 set bin
3010 call append(0, [
3011 \ 'line 2 Abcdefghijklmnopqrstuvwxyz',
3012 \ 'line 3 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
3013 \ 'line 4 Abcdefghijklmnopqrstuvwxyz',
3014 \ 'line 5 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
3015 \ 'line 6 Abcdefghijklmnopqrstuvwxyz',
3016 \ 'line 7 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
3017 \ 'line 8 Abcdefghijklmnopqrstuvwxyz',
3018 \ 'line 9 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
3019 \ 'line 10 Abcdefghijklmnopqrstuvwxyz'
3020 \ ])
3021 1,9write! Xtestfile.gz
3022 enew! | close
3023
3024 new
3025 " Read and decompress the testfile
3026 0read Xtestfile.gz
3027 call assert_equal([
3028 \ 'line 2 Abcdefghijklmnopqrstuvwxyz',
3029 \ 'line 3 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
3030 \ 'line 4 Abcdefghijklmnopqrstuvwxyz',
3031 \ 'line 5 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
3032 \ 'line 6 Abcdefghijklmnopqrstuvwxyz',
3033 \ 'line 7 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
3034 \ 'line 8 Abcdefghijklmnopqrstuvwxyz',
3035 \ 'line 9 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
3036 \ 'line 10 Abcdefghijklmnopqrstuvwxyz'
3037 \ ], getline(1, 9))
3038 enew! | close
3039
3040 augroup Test1
3041 au!
3042 augroup END
3043
3044 " Test for the FileAppendPre and FileAppendPost autocmds
3045 augroup Test2
3046 au!
3047 au BufNewFile *.c read Xtest.c
3048 au FileAppendPre *.out '[,']s/new/NEW/
3049 au FileAppendPost *.out !cat Xtest.c >> test.out
3050 augroup END
3051
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01003052 call writefile(['/*', ' * Here is a new .c file', ' */'], 'Xtest.c', 'D')
Bram Moolenaar69ea5872019-04-25 20:29:00 +02003053 new foo.c " should load Xtest.c
3054 call assert_equal(['/*', ' * Here is a new .c file', ' */'], getline(2, 4))
3055 w! >> test.out " append it to the output file
3056
3057 let contents = readfile('test.out')
3058 call assert_equal(' * Here is a NEW .c file', contents[2])
3059 call assert_equal(' * Here is a new .c file', contents[5])
3060
3061 call delete('test.out')
3062 enew! | close
3063 augroup Test2
3064 au!
3065 augroup END
3066
3067 " Test for the BufReadPre and BufReadPost autocmds
3068 augroup Test3
3069 au!
3070 " setup autocommands to decompress before reading and re-compress
3071 " afterwards
3072 au BufReadPre *.gz exe '!gzip -d ' . shellescape(expand("<afile>"))
3073 au BufReadPre *.gz call rename(expand("<afile>:r"), expand("<afile>"))
3074 au BufReadPost *.gz call rename(expand("<afile>"), expand("<afile>:r"))
3075 au BufReadPost *.gz exe '!gzip ' . shellescape(expand("<afile>:r"))
3076 augroup END
3077
3078 e! Xtestfile.gz " Edit compressed file
3079 call assert_equal([
3080 \ 'line 2 Abcdefghijklmnopqrstuvwxyz',
3081 \ 'line 3 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
3082 \ 'line 4 Abcdefghijklmnopqrstuvwxyz',
3083 \ 'line 5 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
3084 \ 'line 6 Abcdefghijklmnopqrstuvwxyz',
3085 \ 'line 7 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
3086 \ 'line 8 Abcdefghijklmnopqrstuvwxyz',
3087 \ 'line 9 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
3088 \ 'line 10 Abcdefghijklmnopqrstuvwxyz'
3089 \ ], getline(1, 9))
3090
3091 w! >> test.out " Append it to the output file
3092
3093 augroup Test3
3094 au!
3095 augroup END
3096
3097 " Test for the FilterReadPre and FilterReadPost autocmds.
3098 set shelltemp " need temp files here
3099 augroup Test4
3100 au!
3101 au FilterReadPre *.out call rename(expand("<afile>"), expand("<afile>") . ".t")
3102 au FilterReadPre *.out exe 'silent !sed s/e/E/ ' . shellescape(expand("<afile>")) . ".t >" . shellescape(expand("<afile>"))
3103 au FilterReadPre *.out exe 'silent !rm ' . shellescape(expand("<afile>")) . '.t'
3104 au FilterReadPost *.out '[,']s/x/X/g
3105 augroup END
3106
3107 e! test.out " Edit the output file
3108 1,$!cat
3109 call assert_equal([
3110 \ 'linE 2 AbcdefghijklmnopqrstuvwXyz',
3111 \ 'linE 3 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
3112 \ 'linE 4 AbcdefghijklmnopqrstuvwXyz',
3113 \ 'linE 5 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
3114 \ 'linE 6 AbcdefghijklmnopqrstuvwXyz',
3115 \ 'linE 7 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
3116 \ 'linE 8 AbcdefghijklmnopqrstuvwXyz',
3117 \ 'linE 9 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
3118 \ 'linE 10 AbcdefghijklmnopqrstuvwXyz'
3119 \ ], getline(1, 9))
3120 call assert_equal([
3121 \ 'line 2 Abcdefghijklmnopqrstuvwxyz',
3122 \ 'line 3 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
3123 \ 'line 4 Abcdefghijklmnopqrstuvwxyz',
3124 \ 'line 5 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
3125 \ 'line 6 Abcdefghijklmnopqrstuvwxyz',
3126 \ 'line 7 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
3127 \ 'line 8 Abcdefghijklmnopqrstuvwxyz',
3128 \ 'line 9 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
3129 \ 'line 10 Abcdefghijklmnopqrstuvwxyz'
3130 \ ], readfile('test.out'))
3131
3132 augroup Test4
3133 au!
3134 augroup END
3135 set shelltemp&vim
3136
3137 " Test for the FileReadPre and FileReadPost autocmds.
3138 augroup Test5
3139 au!
3140 au FileReadPre *.gz exe 'silent !gzip -d ' . shellescape(expand("<afile>"))
3141 au FileReadPre *.gz call rename(expand("<afile>:r"), expand("<afile>"))
3142 au FileReadPost *.gz '[,']s/l/L/
3143 augroup END
3144
3145 new
3146 0r Xtestfile.gz " Read compressed file
3147 call assert_equal([
3148 \ 'Line 2 Abcdefghijklmnopqrstuvwxyz',
3149 \ 'Line 3 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
3150 \ 'Line 4 Abcdefghijklmnopqrstuvwxyz',
3151 \ 'Line 5 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
3152 \ 'Line 6 Abcdefghijklmnopqrstuvwxyz',
3153 \ 'Line 7 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
3154 \ 'Line 8 Abcdefghijklmnopqrstuvwxyz',
3155 \ 'Line 9 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
3156 \ 'Line 10 Abcdefghijklmnopqrstuvwxyz'
3157 \ ], getline(1, 9))
3158 call assert_equal([
3159 \ 'line 2 Abcdefghijklmnopqrstuvwxyz',
3160 \ 'line 3 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
3161 \ 'line 4 Abcdefghijklmnopqrstuvwxyz',
3162 \ 'line 5 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
3163 \ 'line 6 Abcdefghijklmnopqrstuvwxyz',
3164 \ 'line 7 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
3165 \ 'line 8 Abcdefghijklmnopqrstuvwxyz',
3166 \ 'line 9 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
3167 \ 'line 10 Abcdefghijklmnopqrstuvwxyz'
3168 \ ], readfile('Xtestfile.gz'))
3169
3170 augroup Test5
3171 au!
3172 augroup END
3173
3174 au! FileChangedShell
3175 call delete('Xtestfile.gz')
Bram Moolenaar69ea5872019-04-25 20:29:00 +02003176 call delete('test.out')
3177endfunc
Bram Moolenaar23b51392019-05-09 21:38:43 +02003178
3179func Test_throw_in_BufWritePre()
3180 new
3181 call setline(1, ['one', 'two', 'three'])
3182 call assert_false(filereadable('Xthefile'))
3183 augroup throwing
3184 au BufWritePre X* throw 'do not write'
3185 augroup END
3186 try
3187 w Xthefile
3188 catch
3189 let caught = 1
3190 endtry
3191 call assert_equal(1, caught)
3192 call assert_false(filereadable('Xthefile'))
3193
3194 bwipe!
3195 au! throwing
3196endfunc
Bram Moolenaarcadbe1b2019-09-22 21:50:09 +02003197
Bram Moolenaar40fa12a2021-09-22 14:18:13 +02003198func Test_autocmd_in_try_block()
Bram Moolenaar6f14da12022-09-07 21:30:44 +01003199 call mkdir('Xintrydir', 'R')
Bram Moolenaar40fa12a2021-09-22 14:18:13 +02003200 au BufEnter * let g:fname = expand('%')
3201 try
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +01003202 edit Xintrydir/
Bram Moolenaar40fa12a2021-09-22 14:18:13 +02003203 endtry
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +01003204 call assert_match('Xintrydir', g:fname)
Bram Moolenaar40fa12a2021-09-22 14:18:13 +02003205
3206 unlet g:fname
3207 au! BufEnter
Bram Moolenaar40fa12a2021-09-22 14:18:13 +02003208endfunc
3209
Bram Moolenaarcadbe1b2019-09-22 21:50:09 +02003210func Test_autocmd_SafeState()
3211 CheckRunVimInTerminal
3212
3213 let lines =<< trim END
3214 let g:safe = 0
3215 let g:again = ''
3216 au SafeState * let g:safe += 1
3217 au SafeStateAgain * let g:again ..= 'x'
3218 func CallTimer()
3219 call timer_start(10, {id -> execute('let g:again ..= "t"')})
3220 endfunc
3221 END
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01003222 call writefile(lines, 'XSafeState', 'D')
Bram Moolenaarcadbe1b2019-09-22 21:50:09 +02003223 let buf = RunVimInTerminal('-S XSafeState', #{rows: 6})
3224
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01003225 " Sometimes we loop to handle a K_IGNORE, SafeState may be triggered once or
Bram Moolenaar8fb1b472020-02-23 16:16:26 +01003226 " more often.
Bram Moolenaarcadbe1b2019-09-22 21:50:09 +02003227 call term_sendkeys(buf, ":echo g:safe\<CR>")
Bram Moolenaar8fb1b472020-02-23 16:16:26 +01003228 call WaitForAssert({-> assert_match('^\d ', term_getline(buf, 6))}, 1000)
Bram Moolenaarcadbe1b2019-09-22 21:50:09 +02003229
Bram Moolenaar8fb1b472020-02-23 16:16:26 +01003230 " SafeStateAgain should be invoked at least three times
Bram Moolenaarcadbe1b2019-09-22 21:50:09 +02003231 call term_sendkeys(buf, ":echo g:again\<CR>")
Bram Moolenaar8fb1b472020-02-23 16:16:26 +01003232 call WaitForAssert({-> assert_match('^xxx', term_getline(buf, 6))}, 1000)
Bram Moolenaarcadbe1b2019-09-22 21:50:09 +02003233
3234 call term_sendkeys(buf, ":let g:again = ''\<CR>:call CallTimer()\<CR>")
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02003235 call TermWait(buf, 50)
Bram Moolenaar0f6629a2019-09-22 23:24:13 +02003236 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02003237 call TermWait(buf, 50)
Bram Moolenaarcadbe1b2019-09-22 21:50:09 +02003238 call term_sendkeys(buf, ":echo g:again\<CR>")
3239 call WaitForAssert({-> assert_match('xtx', term_getline(buf, 6))}, 1000)
3240
3241 call StopVimInTerminal(buf)
Bram Moolenaarcadbe1b2019-09-22 21:50:09 +02003242endfunc
Bram Moolenaar23324a02019-10-01 17:39:04 +02003243
3244func Test_autocmd_CmdWinEnter()
3245 CheckRunVimInTerminal
Bram Moolenaar21829c52021-01-26 22:42:21 +01003246
Bram Moolenaar23324a02019-10-01 17:39:04 +02003247 let lines =<< trim END
Egor Zvorykin125ffd22021-11-17 14:01:14 +00003248 augroup vimHints | au! | augroup END
Bram Moolenaar23324a02019-10-01 17:39:04 +02003249 let b:dummy_var = 'This is a dummy'
3250 autocmd CmdWinEnter * quit
3251 let winnr = winnr('$')
3252 END
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01003253 let filename = 'XCmdWinEnter'
Bram Moolenaar23324a02019-10-01 17:39:04 +02003254 call writefile(lines, filename)
3255 let buf = RunVimInTerminal('-S '.filename, #{rows: 6})
3256
3257 call term_sendkeys(buf, "q:")
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02003258 call TermWait(buf)
Bram Moolenaar23324a02019-10-01 17:39:04 +02003259 call term_sendkeys(buf, ":echo b:dummy_var\<cr>")
Bram Moolenaar353c3512020-03-15 14:19:26 +01003260 call WaitForAssert({-> assert_match('^This is a dummy', term_getline(buf, 6))}, 2000)
Bram Moolenaar23324a02019-10-01 17:39:04 +02003261 call term_sendkeys(buf, ":echo &buftype\<cr>")
3262 call WaitForAssert({-> assert_notmatch('^nofile', term_getline(buf, 6))}, 1000)
3263 call term_sendkeys(buf, ":echo winnr\<cr>")
3264 call WaitForAssert({-> assert_match('^1', term_getline(buf, 6))}, 1000)
3265
3266 " clean up
3267 call StopVimInTerminal(buf)
3268 call delete(filename)
3269endfunc
Bram Moolenaarec66c412019-10-11 21:19:13 +02003270
3271func Test_autocmd_was_using_freed_memory()
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01003272 CheckFeature quickfix
3273
Bram Moolenaarec66c412019-10-11 21:19:13 +02003274 pedit xx
3275 n x
Bram Moolenaar92bb83e2021-02-03 23:04:46 +01003276 augroup winenter
3277 au WinEnter * if winnr('$') > 2 | quit | endif
3278 augroup END
Bram Moolenaarec66c412019-10-11 21:19:13 +02003279 split
Bram Moolenaar92bb83e2021-02-03 23:04:46 +01003280
3281 augroup winenter
3282 au! WinEnter
3283 augroup END
3284
3285 bwipe xx
3286 bwipe x
3287 pclose
Bram Moolenaarec66c412019-10-11 21:19:13 +02003288endfunc
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +01003289
3290func Test_BufWrite_lockmarks()
Bram Moolenaarf08b0eb2021-10-16 13:00:14 +01003291 let g:test_is_flaky = 1
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +01003292 edit! Xtest
3293 call setline(1, ['a', 'b', 'c', 'd'])
3294
3295 " :lockmarks preserves the marks
3296 call SetChangeMarks(2, 3)
3297 lockmarks write
3298 call assert_equal([2, 3], [line("'["), line("']")])
3299
3300 " *WritePre autocmds get the correct line range, but lockmarks preserves the
3301 " original values for the user
3302 augroup lockmarks
3303 au!
3304 au BufWritePre,FilterWritePre * call assert_equal([1, 4], [line("'["), line("']")])
3305 au FileWritePre * call assert_equal([3, 4], [line("'["), line("']")])
3306 augroup END
3307
3308 lockmarks write
3309 call assert_equal([2, 3], [line("'["), line("']")])
3310
3311 if executable('cat')
3312 lockmarks %!cat
3313 call assert_equal([2, 3], [line("'["), line("']")])
3314 endif
3315
3316 lockmarks 3,4write Xtest2
3317 call assert_equal([2, 3], [line("'["), line("']")])
3318
3319 au! lockmarks
3320 augroup! lockmarks
3321 call delete('Xtest')
3322 call delete('Xtest2')
3323endfunc
Bram Moolenaarce6db022020-01-07 20:11:42 +01003324
3325func Test_FileType_spell()
3326 if !isdirectory('/tmp')
3327 throw "Skipped: requires /tmp directory"
3328 endif
3329
3330 " this was crashing with an invalid free()
3331 setglobal spellfile=/tmp/en.utf-8.add
3332 augroup crash
3333 autocmd!
3334 autocmd BufNewFile,BufReadPost crashfile setf somefiletype
3335 autocmd BufNewFile,BufReadPost crashfile set ft=anotherfiletype
3336 autocmd FileType anotherfiletype setlocal spell
3337 augroup END
3338 func! NoCrash() abort
3339 edit /tmp/crashfile
3340 endfunc
3341 call NoCrash()
3342
3343 au! crash
3344 setglobal spellfile=
3345endfunc
Bram Moolenaarbc2b71d2020-02-17 21:33:30 +01003346
Bram Moolenaaref976322022-09-28 11:48:30 +01003347" this was wiping out the current buffer and using freed memory
3348func Test_SpellFileMissing_bwipe()
3349 next 0
3350 au SpellFileMissing 0 bwipe
3351 call assert_fails('set spell spelllang=0', 'E937:')
3352
3353 au! SpellFileMissing
Bram Moolenaar0a60f792022-11-19 21:18:11 +00003354 set nospell spelllang=en
Bram Moolenaaref976322022-09-28 11:48:30 +01003355 bwipe
3356endfunc
3357
Bram Moolenaar406cd902020-02-18 21:54:41 +01003358" Test closing a window or editing another buffer from a FileChangedRO handler
3359" in a readonly buffer
3360func Test_FileChangedRO_winclose()
Bram Moolenaar62cd26a2020-10-11 20:08:44 +02003361 call test_override('ui_delay', 10)
3362
Bram Moolenaar406cd902020-02-18 21:54:41 +01003363 augroup FileChangedROTest
3364 au!
3365 autocmd FileChangedRO * quit
3366 augroup END
3367 new
3368 set readonly
3369 call assert_fails('normal i', 'E788:')
3370 close
3371 augroup! FileChangedROTest
3372
3373 augroup FileChangedROTest
3374 au!
Bram Moolenaar61abe7d2022-08-30 21:46:08 +01003375 autocmd FileChangedRO * edit Xrofile
Bram Moolenaar406cd902020-02-18 21:54:41 +01003376 augroup END
3377 new
3378 set readonly
3379 call assert_fails('normal i', 'E788:')
3380 close
3381 augroup! FileChangedROTest
Bram Moolenaar62cd26a2020-10-11 20:08:44 +02003382 call test_override('ALL', 0)
Bram Moolenaar406cd902020-02-18 21:54:41 +01003383endfunc
3384
Bram Moolenaar0c81d1b2020-02-22 22:45:55 +01003385func LogACmd()
3386 call add(g:logged, line('$'))
3387endfunc
3388
3389func Test_TermChanged()
Bram Moolenaard28e0b32020-02-22 23:08:52 +01003390 CheckNotGui
3391
Bram Moolenaar0c81d1b2020-02-22 22:45:55 +01003392 enew!
3393 tabnew
3394 call setline(1, ['a', 'b', 'c', 'd'])
3395 $
3396 au TermChanged * call LogACmd()
3397 let g:logged = []
3398 let term_save = &term
3399 set term=xterm
3400 call assert_equal([1, 4], g:logged)
3401
3402 au! TermChanged
3403 let &term = term_save
3404 bwipe!
3405endfunc
3406
Bram Moolenaare3284872020-03-19 13:55:03 +01003407" Test for FileReadCmd autocmd
3408func Test_autocmd_FileReadCmd()
3409 func ReadFileCmd()
3410 call append(line('$'), "v:cmdarg = " .. v:cmdarg)
3411 endfunc
3412 augroup FileReadCmdTest
3413 au!
3414 au FileReadCmd Xtest call ReadFileCmd()
3415 augroup END
3416
3417 new
3418 read ++bin Xtest
3419 read ++nobin Xtest
3420 read ++edit Xtest
3421 read ++bad=keep Xtest
3422 read ++bad=drop Xtest
3423 read ++bad=- Xtest
3424 read ++ff=unix Xtest
3425 read ++ff=dos Xtest
3426 read ++ff=mac Xtest
3427 read ++enc=utf-8 Xtest
3428
3429 call assert_equal(['',
3430 \ 'v:cmdarg = ++bin',
3431 \ 'v:cmdarg = ++nobin',
3432 \ 'v:cmdarg = ++edit',
3433 \ 'v:cmdarg = ++bad=keep',
3434 \ 'v:cmdarg = ++bad=drop',
3435 \ 'v:cmdarg = ++bad=-',
3436 \ 'v:cmdarg = ++ff=unix',
3437 \ 'v:cmdarg = ++ff=dos',
3438 \ 'v:cmdarg = ++ff=mac',
3439 \ 'v:cmdarg = ++enc=utf-8'], getline(1, '$'))
3440
Bram Moolenaar23526d22022-12-05 15:50:41 +00003441 bwipe!
Bram Moolenaare3284872020-03-19 13:55:03 +01003442 augroup FileReadCmdTest
3443 au!
3444 augroup END
3445 delfunc ReadFileCmd
3446endfunc
3447
Bram Moolenaaree4e0c12020-04-06 21:35:05 +02003448" Test for passing invalid arguments to autocmd
3449func Test_autocmd_invalid_args()
3450 " Additional character after * for event
Bram Moolenaar61abe7d2022-08-30 21:46:08 +01003451 call assert_fails('autocmd *a Xinvfile set ff=unix', 'E215:')
Bram Moolenaaree4e0c12020-04-06 21:35:05 +02003452 augroup Test
3453 augroup END
3454 " Invalid autocmd event
Bram Moolenaar61abe7d2022-08-30 21:46:08 +01003455 call assert_fails('autocmd Bufabc Xinvfile set ft=vim', 'E216:')
Bram Moolenaaree4e0c12020-04-06 21:35:05 +02003456 " Invalid autocmd event in a autocmd group
Bram Moolenaar61abe7d2022-08-30 21:46:08 +01003457 call assert_fails('autocmd Test Bufabc Xinvfile set ft=vim', 'E216:')
Bram Moolenaaree4e0c12020-04-06 21:35:05 +02003458 augroup! Test
3459 " Execute all autocmds
3460 call assert_fails('doautocmd * BufEnter', 'E217:')
3461 call assert_fails('augroup! x1a2b3', 'E367:')
3462 call assert_fails('autocmd BufNew <buffer=999> pwd', 'E680:')
Bram Moolenaar531be472020-09-23 22:38:05 +02003463 call assert_fails('autocmd BufNew \) set ff=unix', 'E55:')
Bram Moolenaaree4e0c12020-04-06 21:35:05 +02003464endfunc
3465
3466" Test for deep nesting of autocmds
3467func Test_autocmd_deep_nesting()
Bram Moolenaar61abe7d2022-08-30 21:46:08 +01003468 autocmd BufEnter Xdeepfile doautocmd BufEnter Xdeepfile
3469 call assert_fails('doautocmd BufEnter Xdeepfile', 'E218:')
3470 autocmd! BufEnter Xdeepfile
Bram Moolenaaree4e0c12020-04-06 21:35:05 +02003471endfunc
3472
Bram Moolenaarbe5ee862020-06-10 20:56:58 +02003473" Tests for SigUSR1 autocmd event, which is only available on posix systems.
3474func Test_autocmd_sigusr1()
3475 CheckUnix
Bram Moolenaar0056ca72022-09-23 21:26:39 +01003476 " FIXME: should this work on MacOS M1?
3477 CheckNotMacM1
Bram Moolenaar62cd26a2020-10-11 20:08:44 +02003478 CheckExecutable /bin/kill
Bram Moolenaarbe5ee862020-06-10 20:56:58 +02003479
3480 let g:sigusr1_passed = 0
3481 au SigUSR1 * let g:sigusr1_passed = 1
3482 call system('/bin/kill -s usr1 ' . getpid())
3483 call WaitForAssert({-> assert_true(g:sigusr1_passed)})
3484
3485 au! SigUSR1
3486 unlet g:sigusr1_passed
3487endfunc
3488
Bram Moolenaarb340bae2020-06-15 19:51:56 +02003489" Test for BufReadPre autocmd deleting the file
3490func Test_BufReadPre_delfile()
3491 augroup TestAuCmd
3492 au!
Bram Moolenaare7cda972022-08-29 11:02:59 +01003493 autocmd BufReadPre XbufreadPre call delete('XbufreadPre')
Bram Moolenaarb340bae2020-06-15 19:51:56 +02003494 augroup END
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01003495 call writefile([], 'XbufreadPre', 'D')
Bram Moolenaare7cda972022-08-29 11:02:59 +01003496 call assert_fails('new XbufreadPre', 'E200:')
3497 call assert_equal('XbufreadPre', @%)
Bram Moolenaarb340bae2020-06-15 19:51:56 +02003498 call assert_equal(1, &readonly)
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01003499
Bram Moolenaarb340bae2020-06-15 19:51:56 +02003500 augroup TestAuCmd
3501 au!
3502 augroup END
3503 close!
3504endfunc
3505
3506" Test for BufReadPre autocmd changing the current buffer
3507func Test_BufReadPre_changebuf()
3508 augroup TestAuCmd
3509 au!
Bram Moolenaare7cda972022-08-29 11:02:59 +01003510 autocmd BufReadPre Xchangebuf edit Xsomeotherfile
Bram Moolenaarb340bae2020-06-15 19:51:56 +02003511 augroup END
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01003512 call writefile([], 'Xchangebuf', 'D')
Bram Moolenaare7cda972022-08-29 11:02:59 +01003513 call assert_fails('new Xchangebuf', 'E201:')
Bram Moolenaarb340bae2020-06-15 19:51:56 +02003514 call assert_equal('Xsomeotherfile', @%)
3515 call assert_equal(1, &readonly)
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01003516
Bram Moolenaarb340bae2020-06-15 19:51:56 +02003517 augroup TestAuCmd
3518 au!
3519 augroup END
3520 close!
3521endfunc
3522
3523" Test for BufWipeouti autocmd changing the current buffer when reading a file
3524" in an empty buffer with 'f' flag in 'cpo'
3525func Test_BufDelete_changebuf()
3526 new
3527 augroup TestAuCmd
3528 au!
3529 autocmd BufWipeout * let bufnr = bufadd('somefile') | exe "b " .. bufnr
3530 augroup END
3531 let save_cpo = &cpo
3532 set cpo+=f
Bram Moolenaarb18b4962022-09-02 21:55:50 +01003533 call assert_fails('r Xchangebuf', ['E812:', 'E484:'])
Bram Moolenaarb340bae2020-06-15 19:51:56 +02003534 call assert_equal('somefile', @%)
3535 let &cpo = save_cpo
3536 augroup TestAuCmd
3537 au!
3538 augroup END
3539 close!
3540endfunc
3541
Bram Moolenaar0fe937f2020-06-16 22:42:04 +02003542" Test for the temporary internal window used to execute autocmds
3543func Test_autocmd_window()
3544 %bw!
3545 edit one.txt
3546 tabnew two.txt
Bram Moolenaar41cd8032021-03-13 15:47:56 +01003547 vnew three.txt
3548 tabnew four.txt
3549 tabprevious
Bram Moolenaar0fe937f2020-06-16 22:42:04 +02003550 let g:blist = []
Bram Moolenaar832adf92020-06-25 19:01:36 +02003551 augroup aucmd_win_test1
Bram Moolenaar0fe937f2020-06-16 22:42:04 +02003552 au!
3553 au BufEnter * call add(g:blist, [expand('<afile>'),
3554 \ win_gettype(bufwinnr(expand('<afile>')))])
3555 augroup END
3556
3557 doautoall BufEnter
Bram Moolenaar41cd8032021-03-13 15:47:56 +01003558 call assert_equal([
3559 \ ['one.txt', 'autocmd'],
3560 \ ['two.txt', ''],
3561 \ ['four.txt', 'autocmd'],
3562 \ ['three.txt', ''],
3563 \ ], g:blist)
Bram Moolenaar0fe937f2020-06-16 22:42:04 +02003564
Bram Moolenaar832adf92020-06-25 19:01:36 +02003565 augroup aucmd_win_test1
Bram Moolenaar0fe937f2020-06-16 22:42:04 +02003566 au!
3567 augroup END
Bram Moolenaar832adf92020-06-25 19:01:36 +02003568 augroup! aucmd_win_test1
3569 %bw!
3570endfunc
3571
3572" Test for trying to close the temporary window used for executing an autocmd
3573func Test_close_autocmd_window()
3574 %bw!
3575 edit one.txt
3576 tabnew two.txt
3577 augroup aucmd_win_test2
3578 au!
3579 au BufEnter * if expand('<afile>') == 'one.txt' | 1close | endif
3580 augroup END
3581
3582 call assert_fails('doautoall BufEnter', 'E813:')
3583
3584 augroup aucmd_win_test2
3585 au!
3586 augroup END
3587 augroup! aucmd_win_test2
Bram Moolenaarcf844172020-06-26 19:44:06 +02003588 %bwipe!
3589endfunc
3590
3591" Test for trying to close the tab that has the temporary window for exeucing
3592" an autocmd.
3593func Test_close_autocmd_tab()
3594 edit one.txt
3595 tabnew two.txt
3596 augroup aucmd_win_test
3597 au!
3598 au BufEnter * if expand('<afile>') == 'one.txt' | tabfirst | tabonly | endif
3599 augroup END
3600
3601 call assert_fails('doautoall BufEnter', 'E813:')
3602
3603 tabonly
3604 augroup aucmd_win_test
3605 au!
3606 augroup END
3607 augroup! aucmd_win_test
3608 %bwipe!
Bram Moolenaar0fe937f2020-06-16 22:42:04 +02003609endfunc
3610
Bram Moolenaarcb1956d2022-01-07 15:45:18 +00003611func Test_Visual_doautoall_redraw()
3612 call setline(1, ['a', 'b'])
Bram Moolenaar94722c52023-01-28 19:19:03 +00003613 new
Bram Moolenaarcb1956d2022-01-07 15:45:18 +00003614 wincmd p
3615 call feedkeys("G\<C-V>", 'txn')
3616 autocmd User Explode ++once redraw
3617 doautoall User Explode
3618 %bwipe!
3619endfunc
3620
Bram Moolenaar6bcb8772021-02-03 21:23:29 +01003621" This was using freed memory.
3622func Test_BufNew_arglocal()
3623 arglocal
3624 au BufNew * arglocal
3625 call assert_fails('drop xx', 'E1156:')
3626
3627 au! BufNew
3628endfunc
3629
Bram Moolenaar8ab37572021-02-03 21:56:59 +01003630func Test_autocmd_closes_window()
3631 au BufNew,BufWinLeave * e %e
3632 file yyy
3633 au BufNew,BufWinLeave * ball
Bram Moolenaaraad5f9d2021-02-06 17:30:31 +01003634 n xxx
Bram Moolenaar8ab37572021-02-03 21:56:59 +01003635
Bram Moolenaaraad5f9d2021-02-06 17:30:31 +01003636 %bwipe
Bram Moolenaar8ab37572021-02-03 21:56:59 +01003637 au! BufNew
3638 au! BufWinLeave
3639endfunc
3640
Bram Moolenaar92bb83e2021-02-03 23:04:46 +01003641func Test_autocmd_quit_psearch()
3642 sn aa bb
3643 augroup aucmd_win_test
3644 au!
3645 au BufEnter,BufLeave,BufNew,WinEnter,WinLeave,WinNew * if winnr('$') > 1 | q | endif
3646 augroup END
3647 ps /
3648
3649 augroup aucmd_win_test
3650 au!
3651 augroup END
zeertzjq7851c692022-04-21 11:14:01 +01003652 new
3653 pclose
Bram Moolenaar92bb83e2021-02-03 23:04:46 +01003654endfunc
3655
Bram Moolenaaraad5f9d2021-02-06 17:30:31 +01003656" Fuzzer found some strange combination that caused a crash.
3657func Test_autocmd_normal_mess()
Bram Moolenaardd07c022021-02-07 13:32:46 +01003658 " For unknown reason this hangs on MS-Windows
3659 CheckNotMSWindows
3660
Bram Moolenaaraad5f9d2021-02-06 17:30:31 +01003661 augroup aucmd_normal_test
3662 au BufLeave,BufWinLeave,BufHidden,BufUnload,BufDelete,BufWipeout * norm 7q/qc
3663 augroup END
zeertzjq67fe77d2025-04-20 10:21:18 +02003664 call assert_fails('o4', 'E1159:')
Bram Moolenaaraad5f9d2021-02-06 17:30:31 +01003665 silent! H
zeertzjq67fe77d2025-04-20 10:21:18 +02003666 call assert_fails('e xx', 'E1159:')
Bram Moolenaaraad5f9d2021-02-06 17:30:31 +01003667 normal G
3668
3669 augroup aucmd_normal_test
3670 au!
3671 augroup END
3672endfunc
3673
Bram Moolenaar8c6951f2021-02-06 18:08:45 +01003674func Test_autocmd_closing_cmdwin()
Bram Moolenaardd07c022021-02-07 13:32:46 +01003675 " For unknown reason this hangs on MS-Windows
3676 CheckNotMSWindows
3677
Bram Moolenaar8c6951f2021-02-06 18:08:45 +01003678 au BufWinLeave * nested q
3679 call assert_fails("norm 7q?\n", 'E855:')
3680
3681 au! BufWinLeave
3682 new
3683 only
3684endfunc
3685
Bram Moolenaar2c7080b2021-02-06 19:19:42 +01003686func Test_autocmd_vimgrep()
3687 augroup aucmd_vimgrep
Charlie Grovesfef44852022-04-19 16:24:12 +01003688 au QuickfixCmdPre,BufNew,BufReadCmd * sb
zeertzjq7851c692022-04-21 11:14:01 +01003689 au QuickfixCmdPre,BufNew,BufReadCmd * q9
Bram Moolenaar2c7080b2021-02-06 19:19:42 +01003690 augroup END
Bram Moolenaardd07c022021-02-07 13:32:46 +01003691 call assert_fails('lv ?a? foo', 'E926:')
Bram Moolenaar2c7080b2021-02-06 19:19:42 +01003692
3693 augroup aucmd_vimgrep
3694 au!
3695 augroup END
3696endfunc
3697
Bram Moolenaar73b8b0a2021-08-01 14:52:32 +02003698func Test_autocmd_with_block()
3699 augroup block_testing
3700 au BufReadPost *.xml {
3701 setlocal matchpairs+=<:>
3702 /<start
3703 }
Bram Moolenaar63b91732021-08-05 20:40:03 +02003704 au CursorHold * {
3705 autocmd BufReadPre * ++once echo 'one' | echo 'two'
3706 g:gotSafeState = 77
3707 }
Bram Moolenaar73b8b0a2021-08-01 14:52:32 +02003708 augroup END
3709
Ken Takataeccc9272024-09-03 23:01:55 +02003710 let expected = gettext("\n--- Autocommands ---") .. "\nblock_testing BufRead\n *.xml {^@ setlocal matchpairs+=<:>^@ /<start^@ }"
Bram Moolenaar73b8b0a2021-08-01 14:52:32 +02003711 call assert_equal(expected, execute('au BufReadPost *.xml'))
3712
Bram Moolenaar63b91732021-08-05 20:40:03 +02003713 doautocmd CursorHold
3714 call assert_equal(77, g:gotSafeState)
3715 unlet g:gotSafeState
3716
Bram Moolenaar73b8b0a2021-08-01 14:52:32 +02003717 augroup block_testing
3718 au!
Bram Moolenaar75ebd2a2022-06-03 17:39:46 +01003719 autocmd CursorHold * {
3720 if true
3721 # comment
3722 && true
3723
3724 && true
3725 g:done = 'yes'
3726 endif
3727 }
3728 augroup END
3729 doautocmd CursorHold
3730 call assert_equal('yes', g:done)
3731
3732 unlet g:done
3733 augroup block_testing
3734 au!
Bram Moolenaar73b8b0a2021-08-01 14:52:32 +02003735 augroup END
3736endfunc
3737
Bram Moolenaar6f2465d2022-03-22 18:13:01 +00003738func Test_closing_autocmd_window()
3739 let lines =<< trim END
3740 edit Xa.txt
3741 tabnew Xb.txt
3742 autocmd BufEnter Xa.txt unhide 1
3743 doautoall BufEnter
3744 END
3745 call v9.CheckScriptFailure(lines, 'E814:')
3746 au! BufEnter
Bram Moolenaar6f2465d2022-03-22 18:13:01 +00003747 bwipe Xa.txt
3748 bwipe Xb.txt
3749endfunc
3750
zeertzjq46bdae02023-09-24 23:16:08 +02003751func Test_switch_window_in_autocmd_window()
3752 edit Xa.txt
3753 tabnew Xb.txt
3754 autocmd BufEnter Xa.txt wincmd w
3755 doautoall BufEnter
3756 au! BufEnter
3757 bwipe Xa.txt
3758 call assert_false(bufexists('Xa.txt'))
3759 bwipe Xb.txt
3760 call assert_false(bufexists('Xb.txt'))
3761endfunc
3762
zeertzjq9d956ee2024-04-07 18:16:10 +02003763" Test that using the autocommand window doesn't change current directory.
3764func Test_autocmd_window_cwd()
3765 let saveddir = getcwd()
3766 call mkdir('Xcwd/a/b/c/d', 'pR')
3767
3768 new Xa.txt
3769 tabnew
3770 new Xb.txt
3771
3772 tabprev
3773 cd Xcwd
3774 call assert_match('/Xcwd$', getcwd())
3775 call assert_match('\[global\] .*/Xcwd$', trim(execute('verbose pwd')))
3776
3777 autocmd BufEnter Xb.txt lcd ./a/b/c/d
3778 doautoall BufEnter
3779 au! BufEnter
3780 call assert_match('/Xcwd$', getcwd())
3781 call assert_match('\[global\] .*/Xcwd$', trim(execute('verbose pwd')))
3782
3783 tabnext
3784 cd ./a
3785 tcd ./b
3786 lcd ./c
3787 call assert_match('/Xcwd/a/b/c$', getcwd())
3788 call assert_match('\[window\] .*/Xcwd/a/b/c$', trim(execute('verbose pwd')))
3789
3790 autocmd BufEnter Xa.txt call assert_match('Xcwd/a/b/c$', getcwd())
3791 doautoall BufEnter
3792 au! BufEnter
3793 call assert_match('/Xcwd/a/b/c$', getcwd())
3794 call assert_match('\[window\] .*/Xcwd/a/b/c$', trim(execute('verbose pwd')))
3795 bwipe!
3796 call assert_match('/Xcwd/a/b$', getcwd())
3797 call assert_match('\[tabpage\] .*/Xcwd/a/b$', trim(execute('verbose pwd')))
3798 bwipe!
3799 call assert_match('/Xcwd/a$', getcwd())
3800 call assert_match('\[global\] .*/Xcwd/a$', trim(execute('verbose pwd')))
3801 bwipe!
3802
3803 call chdir(saveddir)
3804endfunc
3805
Bram Moolenaar347538f2022-03-26 16:28:06 +00003806func Test_bufwipeout_changes_window()
3807 " This should not crash, but we don't have any expectations about what
3808 " happens, changing window in BufWipeout has unpredictable results.
3809 tabedit
3810 let g:window_id = win_getid()
3811 topleft new
3812 setlocal bufhidden=wipe
3813 autocmd BufWipeout <buffer> call win_gotoid(g:window_id)
3814 tabprevious
3815 +tabclose
3816
3817 unlet g:window_id
3818 au! BufWipeout
3819 %bwipe!
3820endfunc
3821
zeertzjq021996f2022-04-10 11:44:04 +01003822func Test_v_event_readonly()
3823 autocmd CompleteChanged * let v:event.width = 0
3824 call assert_fails("normal! i\<C-X>\<C-V>", 'E46:')
3825 au! CompleteChanged
3826
3827 autocmd DirChangedPre * let v:event.directory = ''
3828 call assert_fails('cd .', 'E46:')
3829 au! DirChangedPre
3830
3831 autocmd ModeChanged * let v:event.new_mode = ''
3832 call assert_fails('normal! cc', 'E46:')
3833 au! ModeChanged
3834
3835 autocmd TextYankPost * let v:event.operator = ''
3836 call assert_fails('normal! yy', 'E46:')
3837 au! TextYankPost
3838endfunc
3839
zeertzjqc9e8fd62022-07-26 18:12:38 +01003840" Test for ModeChanged pattern
3841func Test_mode_changes()
3842 let g:index = 0
zeertzjq73916ba2023-04-26 16:50:19 +01003843 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 +01003844 func! TestMode()
3845 call assert_equal(g:mode_seq[g:index], get(v:event, "old_mode"))
3846 call assert_equal(g:mode_seq[g:index + 1], get(v:event, "new_mode"))
3847 call assert_equal(mode(1), get(v:event, "new_mode"))
3848 let g:index += 1
3849 endfunc
3850
3851 au ModeChanged * :call TestMode()
3852 let g:n_to_any = 0
3853 au ModeChanged n:* let g:n_to_any += 1
zeertzjq73916ba2023-04-26 16:50:19 +01003854 call feedkeys("i\<esc>vVca\<CR>\<C-X>\<C-L>\<esc>ggdV\<MouseMove>G", 'tnix')
zeertzjqc9e8fd62022-07-26 18:12:38 +01003855
3856 let g:V_to_v = 0
3857 au ModeChanged V:v let g:V_to_v += 1
3858 call feedkeys("Vv\<C-G>\<esc>", 'tnix')
3859 call assert_equal(len(filter(g:mode_seq[1:], {idx, val -> val == 'n'})), g:n_to_any)
3860 call assert_equal(1, g:V_to_v)
3861 call assert_equal(len(g:mode_seq) - 1, g:index)
3862
3863 let g:n_to_i = 0
3864 au ModeChanged n:i let g:n_to_i += 1
3865 let g:n_to_niI = 0
3866 au ModeChanged i:niI let g:n_to_niI += 1
3867 let g:niI_to_i = 0
3868 au ModeChanged niI:i let g:niI_to_i += 1
3869 let g:nany_to_i = 0
3870 au ModeChanged n*:i let g:nany_to_i += 1
3871 let g:i_to_n = 0
3872 au ModeChanged i:n let g:i_to_n += 1
3873 let g:nori_to_any = 0
3874 au ModeChanged [ni]:* let g:nori_to_any += 1
3875 let g:i_to_any = 0
3876 au ModeChanged i:* let g:i_to_any += 1
3877 let g:index = 0
3878 let g:mode_seq = ['n', 'i', 'niI', 'i', 'n']
3879 call feedkeys("a\<C-O>l\<esc>", 'tnix')
3880 call assert_equal(len(g:mode_seq) - 1, g:index)
3881 call assert_equal(1, g:n_to_i)
3882 call assert_equal(1, g:n_to_niI)
3883 call assert_equal(1, g:niI_to_i)
3884 call assert_equal(2, g:nany_to_i)
3885 call assert_equal(1, g:i_to_n)
3886 call assert_equal(2, g:i_to_any)
3887 call assert_equal(3, g:nori_to_any)
3888
3889 if has('terminal')
3890 let g:mode_seq += ['c', 'n', 't', 'nt', 'c', 'nt', 'n']
3891 call feedkeys(":term\<CR>\<C-W>N:bd!\<CR>", 'tnix')
3892 call assert_equal(len(g:mode_seq) - 1, g:index)
3893 call assert_equal(1, g:n_to_i)
3894 call assert_equal(1, g:n_to_niI)
3895 call assert_equal(1, g:niI_to_i)
3896 call assert_equal(2, g:nany_to_i)
3897 call assert_equal(1, g:i_to_n)
3898 call assert_equal(2, g:i_to_any)
3899 call assert_equal(5, g:nori_to_any)
3900 endif
3901
zeertzjqd1955982022-10-05 11:24:46 +01003902 let g:n_to_c = 0
3903 au ModeChanged n:c let g:n_to_c += 1
3904 let g:c_to_n = 0
3905 au ModeChanged c:n let g:c_to_n += 1
3906 let g:mode_seq += ['c', 'n', 'c', 'n']
3907 call feedkeys("q:\<C-C>\<Esc>", 'tnix')
3908 call assert_equal(len(g:mode_seq) - 1, g:index)
3909 call assert_equal(2, g:n_to_c)
3910 call assert_equal(2, g:c_to_n)
zeertzjqc9e8fd62022-07-26 18:12:38 +01003911
Bram Moolenaar61c4b042022-10-18 15:10:11 +01003912 let g:n_to_v = 0
3913 au ModeChanged n:v let g:n_to_v += 1
3914 let g:v_to_n = 0
3915 au ModeChanged v:n let g:v_to_n += 1
3916 let g:mode_seq += ['v', 'n']
3917 call feedkeys("v\<C-C>", 'tnix')
3918 call assert_equal(len(g:mode_seq) - 1, g:index)
3919 call assert_equal(1, g:n_to_v)
3920 call assert_equal(1, g:v_to_n)
zeertzjqfcaeb3d2023-11-28 20:46:29 +01003921
3922 let g:mode_seq += ['c', 'cr', 'c', 'cr', 'n']
3923 call feedkeys(":\<Insert>\<Insert>\<Insert>\<CR>", 'tnix')
3924 call assert_equal(len(g:mode_seq) - 1, g:index)
Bram Moolenaar61c4b042022-10-18 15:10:11 +01003925
zeertzjqc9e8fd62022-07-26 18:12:38 +01003926 au! ModeChanged
3927 delfunc TestMode
3928 unlet! g:mode_seq
3929 unlet! g:index
3930 unlet! g:n_to_any
3931 unlet! g:V_to_v
3932 unlet! g:n_to_i
3933 unlet! g:n_to_niI
3934 unlet! g:niI_to_i
3935 unlet! g:nany_to_i
3936 unlet! g:i_to_n
3937 unlet! g:nori_to_any
3938 unlet! g:i_to_any
zeertzjqfcaeb3d2023-11-28 20:46:29 +01003939 unlet! g:n_to_c
3940 unlet! g:c_to_n
3941 unlet! g:n_to_v
3942 unlet! g:v_to_n
zeertzjqc9e8fd62022-07-26 18:12:38 +01003943endfunc
3944
3945func Test_recursive_ModeChanged()
3946 au! ModeChanged * norm 0u
3947 sil! norm 
3948 au! ModeChanged
3949endfunc
3950
3951func Test_ModeChanged_starts_visual()
3952 " This was triggering ModeChanged before setting VIsual, causing a crash.
3953 au! ModeChanged * norm 0u
3954 sil! norm 
3955
3956 au! ModeChanged
3957endfunc
Bram Moolenaar347538f2022-03-26 16:28:06 +00003958
Charlie Grovesfef44852022-04-19 16:24:12 +01003959func Test_noname_autocmd()
3960 augroup test_noname_autocmd_group
3961 autocmd!
3962 autocmd BufEnter * call add(s:li, ["BufEnter", expand("<afile>")])
3963 autocmd BufDelete * call add(s:li, ["BufDelete", expand("<afile>")])
3964 autocmd BufLeave * call add(s:li, ["BufLeave", expand("<afile>")])
3965 autocmd BufUnload * call add(s:li, ["BufUnload", expand("<afile>")])
3966 autocmd BufWipeout * call add(s:li, ["BufWipeout", expand("<afile>")])
3967 augroup END
3968
3969 let s:li = []
3970 edit foo
3971 call assert_equal([['BufUnload', ''], ['BufDelete', ''], ['BufWipeout', ''], ['BufEnter', 'foo']], s:li)
3972
3973 au! test_noname_autocmd_group
3974 augroup! test_noname_autocmd_group
3975endfunc
3976
Yegappan Lakshmanan1755a912022-05-19 10:31:47 +01003977" Test for the autocmd_get() function
3978func Test_autocmd_get()
3979 augroup TestAutoCmdFns
3980 au!
3981 autocmd BufAdd *.vim echo "bufadd-vim"
3982 autocmd BufAdd *.py echo "bufadd-py"
3983 autocmd BufHidden *.vim echo "bufhidden"
3984 augroup END
3985 augroup TestAutoCmdFns2
3986 autocmd BufAdd *.vim echo "bufadd-vim-2"
3987 autocmd BufRead *.a1b2c3 echo "bufadd-vim-2"
3988 augroup END
3989
3990 let l = autocmd_get()
3991 call assert_true(l->len() > 0)
3992
3993 " Test for getting all the autocmds in a group
3994 let expected = [
3995 \ #{cmd: 'echo "bufadd-vim"', group: 'TestAutoCmdFns',
3996 \ pattern: '*.vim', nested: v:false, once: v:false,
3997 \ event: 'BufAdd'},
3998 \ #{cmd: 'echo "bufadd-py"', group: 'TestAutoCmdFns',
3999 \ pattern: '*.py', nested: v:false, once: v:false,
4000 \ event: 'BufAdd'},
4001 \ #{cmd: 'echo "bufhidden"', group: 'TestAutoCmdFns',
4002 \ pattern: '*.vim', nested: v:false,
4003 \ once: v:false, event: 'BufHidden'}]
4004 call assert_equal(expected, autocmd_get(#{group: 'TestAutoCmdFns'}))
4005
4006 " Test for getting autocmds for all the patterns in a group
4007 call assert_equal(expected, autocmd_get(#{group: 'TestAutoCmdFns',
4008 \ event: '*'}))
4009
4010 " Test for getting autocmds for an event in a group
4011 let expected = [
4012 \ #{cmd: 'echo "bufadd-vim"', group: 'TestAutoCmdFns',
4013 \ pattern: '*.vim', nested: v:false, once: v:false,
4014 \ event: 'BufAdd'},
4015 \ #{cmd: 'echo "bufadd-py"', group: 'TestAutoCmdFns',
4016 \ pattern: '*.py', nested: v:false, once: v:false,
4017 \ event: 'BufAdd'}]
4018 call assert_equal(expected, autocmd_get(#{group: 'TestAutoCmdFns',
4019 \ event: 'BufAdd'}))
4020
4021 " Test for getting the autocmds for all the events in a group for particular
4022 " pattern
4023 call assert_equal([{'cmd': 'echo "bufadd-py"', 'group': 'TestAutoCmdFns',
4024 \ 'pattern': '*.py', 'nested': v:false, 'once': v:false,
4025 \ 'event': 'BufAdd'}],
4026 \ autocmd_get(#{group: 'TestAutoCmdFns', event: '*', pattern: '*.py'}))
4027
4028 " Test for getting the autocmds for an events in a group for particular
4029 " pattern
4030 let l = autocmd_get(#{group: 'TestAutoCmdFns', event: 'BufAdd',
4031 \ pattern: '*.vim'})
4032 call assert_equal([
4033 \ #{cmd: 'echo "bufadd-vim"', group: 'TestAutoCmdFns',
4034 \ pattern: '*.vim', nested: v:false, once: v:false,
4035 \ event: 'BufAdd'}], l)
4036
4037 " Test for getting the autocmds for a pattern in a group
4038 let l = autocmd_get(#{group: 'TestAutoCmdFns', pattern: '*.vim'})
4039 call assert_equal([
4040 \ #{cmd: 'echo "bufadd-vim"', group: 'TestAutoCmdFns',
4041 \ pattern: '*.vim', nested: v:false, once: v:false,
4042 \ event: 'BufAdd'},
4043 \ #{cmd: 'echo "bufhidden"', group: 'TestAutoCmdFns',
4044 \ pattern: '*.vim', nested: v:false,
4045 \ once: v:false, event: 'BufHidden'}], l)
4046
4047 " Test for getting the autocmds for a pattern in all the groups
4048 let l = autocmd_get(#{pattern: '*.a1b2c3'})
4049 call assert_equal([{'cmd': 'echo "bufadd-vim-2"', 'group': 'TestAutoCmdFns2',
4050 \ 'pattern': '*.a1b2c3', 'nested': v:false, 'once': v:false,
4051 \ 'event': 'BufRead'}], l)
4052
4053 " Test for getting autocmds for a pattern without any autocmds
4054 call assert_equal([], autocmd_get(#{group: 'TestAutoCmdFns',
4055 \ pattern: '*.abc'}))
4056 call assert_equal([], autocmd_get(#{group: 'TestAutoCmdFns',
4057 \ event: 'BufAdd', pattern: '*.abc'}))
4058 call assert_equal([], autocmd_get(#{group: 'TestAutoCmdFns',
4059 \ event: 'BufWipeout'}))
zeertzjq2d1d5c62024-06-09 16:44:33 +02004060
4061 " Test for getting autocmds after removing one inside an autocmd
4062 func CheckAutocmdGet()
4063 augroup TestAutoCmdFns
4064 autocmd! BufAdd *.vim
4065 augroup END
4066
4067 let expected = [
4068 \ #{cmd: 'echo "bufadd-py"', group: 'TestAutoCmdFns',
4069 \ pattern: '*.py', nested: v:false, once: v:false,
4070 \ event: 'BufAdd'},
4071 \ #{cmd: 'echo "bufhidden"', group: 'TestAutoCmdFns',
4072 \ pattern: '*.vim', nested: v:false,
4073 \ once: v:false, event: 'BufHidden'}]
4074
4075 call assert_equal(expected, autocmd_get(#{group: 'TestAutoCmdFns'}))
4076 call assert_equal([expected[0]],
4077 \ autocmd_get(#{group: 'TestAutoCmdFns', pattern: '*.py'}))
4078 call assert_equal([expected[1]],
4079 \ autocmd_get(#{group: 'TestAutoCmdFns', pattern: '*.vim'}))
4080 endfunc
4081
4082 autocmd User Xauget call CheckAutocmdGet()
4083 doautocmd User Xauget
4084 autocmd! User Xauget
4085
Yegappan Lakshmanan1755a912022-05-19 10:31:47 +01004086 call assert_fails("call autocmd_get(#{group: 'abc', event: 'BufAdd'})",
4087 \ 'E367:')
4088 let cmd = "echo autocmd_get(#{group: 'TestAutoCmdFns', event: 'abc'})"
4089 call assert_fails(cmd, 'E216:')
4090 call assert_fails("call autocmd_get(#{group: 'abc'})", 'E367:')
4091 call assert_fails("echo autocmd_get(#{event: 'abc'})", 'E216:')
4092
4093 augroup TestAutoCmdFns
4094 au!
4095 augroup END
4096 call assert_equal([], autocmd_get(#{group: 'TestAutoCmdFns'}))
4097
4098 " Test for nested and once autocmds
4099 augroup TestAutoCmdFns
4100 au!
4101 autocmd VimSuspend * ++nested echo "suspend"
4102 autocmd VimResume * ++once echo "resume"
4103 augroup END
4104
4105 let expected = [
Yegappan Lakshmanan1755a912022-05-19 10:31:47 +01004106 \ {'cmd': 'echo "resume"', 'group': 'TestAutoCmdFns', 'pattern': '*',
Luuk van Baalb7147f82025-02-08 18:52:39 +01004107 \ 'nested': v:false, 'once': v:true, 'event': 'VimResume'},
4108 \ {'cmd': 'echo "suspend"', 'group': 'TestAutoCmdFns', 'pattern': '*',
4109 \ 'nested': v:true, 'once': v:false, 'event': 'VimSuspend'}]
Yegappan Lakshmanan1755a912022-05-19 10:31:47 +01004110 call assert_equal(expected, autocmd_get(#{group: 'TestAutoCmdFns'}))
4111
4112 " Test for buffer-local autocmd
4113 augroup TestAutoCmdFns
4114 au!
4115 autocmd TextYankPost <buffer> echo "textyankpost"
4116 augroup END
4117
4118 let expected = [
4119 \ {'cmd': 'echo "textyankpost"', 'group': 'TestAutoCmdFns',
4120 \ 'pattern': '<buffer=' .. bufnr() .. '>', 'nested': v:false,
4121 \ 'once': v:false, 'bufnr': bufnr(), 'event': 'TextYankPost'}]
4122 call assert_equal(expected, autocmd_get(#{group: 'TestAutoCmdFns'}))
4123
4124 augroup TestAutoCmdFns
4125 au!
4126 augroup END
4127 augroup! TestAutoCmdFns
4128 augroup TestAutoCmdFns2
4129 au!
4130 augroup END
4131 augroup! TestAutoCmdFns2
4132
4133 call assert_fails("echo autocmd_get(#{group: []})", 'E730:')
4134 call assert_fails("echo autocmd_get(#{event: {}})", 'E731:')
4135 call assert_fails("echo autocmd_get([])", 'E1206:')
4136endfunc
4137
4138" Test for the autocmd_add() function
4139func Test_autocmd_add()
4140 " Define a single autocmd in a group
4141 call autocmd_add([#{group: 'TestAcSet', event: 'BufAdd', pattern: '*.sh',
4142 \ cmd: 'echo "bufadd"', once: v:true, nested: v:true}])
4143 call assert_equal([#{cmd: 'echo "bufadd"', group: 'TestAcSet',
4144 \ pattern: '*.sh', nested: v:true, once: v:true,
4145 \ event: 'BufAdd'}], autocmd_get(#{group: 'TestAcSet'}))
4146
4147 " Define two autocmds in the same group
4148 call autocmd_delete([#{group: 'TestAcSet'}])
4149 call autocmd_add([#{group: 'TestAcSet', event: 'BufAdd', pattern: '*.sh',
4150 \ cmd: 'echo "bufadd"'},
4151 \ #{group: 'TestAcSet', event: 'BufEnter', pattern: '*.sh',
4152 \ cmd: 'echo "bufenter"'}])
4153 call assert_equal([
4154 \ #{cmd: 'echo "bufadd"', group: 'TestAcSet', pattern: '*.sh',
4155 \ nested: v:false, once: v:false, event: 'BufAdd'},
4156 \ #{cmd: 'echo "bufenter"', group: 'TestAcSet', pattern: '*.sh',
4157 \ nested: v:false, once: v:false, event: 'BufEnter'}],
4158 \ autocmd_get(#{group: 'TestAcSet'}))
4159
4160 " Define a buffer-local autocmd
4161 call autocmd_delete([#{group: 'TestAcSet'}])
4162 call autocmd_add([#{group: 'TestAcSet', event: 'CursorHold',
4163 \ bufnr: bufnr(), cmd: 'echo "cursorhold"'}])
4164 call assert_equal([
4165 \ #{cmd: 'echo "cursorhold"', group: 'TestAcSet',
4166 \ pattern: '<buffer=' .. bufnr() .. '>', nested: v:false,
4167 \ once: v:false, bufnr: bufnr(), event: 'CursorHold'}],
4168 \ autocmd_get(#{group: 'TestAcSet'}))
4169
4170 " Use an invalid buffer number
4171 call autocmd_delete([#{group: 'TestAcSet'}])
4172 call autocmd_add([#{group: 'TestAcSet', event: 'BufEnter',
4173 \ bufnr: -1, cmd: 'echo "bufenter"'}])
4174 let l = [#{group: 'TestAcSet', event: 'BufAdd', bufnr: 9999,
4175 \ cmd: 'echo "bufadd"'}]
4176 call assert_fails("echo autocmd_add(l)", 'E680:')
Yegappan Lakshmanan00e977c2022-06-01 12:31:53 +01004177 let l = [#{group: 'TestAcSet', event: 'BufAdd', bufnr: 9999,
4178 \ pattern: '*.py', cmd: 'echo "bufadd"'}]
4179 call assert_fails("echo autocmd_add(l)", 'E680:')
4180 let l = [#{group: 'TestAcSet', event: 'BufAdd', bufnr: 9999,
4181 \ pattern: ['*.py', '*.c'], cmd: 'echo "bufadd"'}]
4182 call assert_fails("echo autocmd_add(l)", 'E680:')
Yegappan Lakshmanan1755a912022-05-19 10:31:47 +01004183 let l = [#{group: 'TestAcSet', event: 'BufRead', bufnr: [],
4184 \ cmd: 'echo "bufread"'}]
4185 call assert_fails("echo autocmd_add(l)", 'E745:')
4186 call assert_equal([], autocmd_get(#{group: 'TestAcSet'}))
4187
4188 " Add two commands to the same group, event and pattern
4189 call autocmd_delete([#{group: 'TestAcSet'}])
4190 call autocmd_add([#{group: 'TestAcSet', event: 'BufUnload',
4191 \ pattern: 'abc', cmd: 'echo "cmd1"'}])
4192 call autocmd_add([#{group: 'TestAcSet', event: 'BufUnload',
4193 \ pattern: 'abc', cmd: 'echo "cmd2"'}])
4194 call assert_equal([
4195 \ #{cmd: 'echo "cmd1"', group: 'TestAcSet', pattern: 'abc',
4196 \ nested: v:false, once: v:false, event: 'BufUnload'},
4197 \ #{cmd: 'echo "cmd2"', group: 'TestAcSet', pattern: 'abc',
4198 \ nested: v:false, once: v:false, event: 'BufUnload'}],
4199 \ autocmd_get(#{group: 'TestAcSet'}))
4200
4201 " When adding a new autocmd, if the autocmd 'group' is not specified, then
4202 " the current autocmd group should be used.
4203 call autocmd_delete([#{group: 'TestAcSet'}])
4204 augroup TestAcSet
4205 call autocmd_add([#{event: 'BufHidden', pattern: 'abc', cmd: 'echo "abc"'}])
4206 augroup END
4207 call assert_equal([
4208 \ #{cmd: 'echo "abc"', group: 'TestAcSet', pattern: 'abc',
4209 \ nested: v:false, once: v:false, event: 'BufHidden'}],
4210 \ autocmd_get(#{group: 'TestAcSet'}))
4211
Yegappan Lakshmanan971f6822022-05-24 11:40:11 +01004212 " Test for replacing a cmd for an event in a group
4213 call autocmd_delete([#{group: 'TestAcSet'}])
4214 call autocmd_add([#{replace: v:true, group: 'TestAcSet', event: 'BufEnter',
4215 \ pattern: '*.py', cmd: 'echo "bufenter"'}])
4216 call autocmd_add([#{replace: v:true, group: 'TestAcSet', event: 'BufEnter',
4217 \ pattern: '*.py', cmd: 'echo "bufenter"'}])
4218 call assert_equal([
4219 \ #{cmd: 'echo "bufenter"', group: 'TestAcSet', pattern: '*.py',
4220 \ nested: v:false, once: v:false, event: 'BufEnter'}],
4221 \ autocmd_get(#{group: 'TestAcSet'}))
4222
4223 " Test for adding a command for an unsupported autocmd event
Yegappan Lakshmanan1755a912022-05-19 10:31:47 +01004224 let l = [#{group: 'TestAcSet', event: 'abc', pattern: '*.sh',
4225 \ cmd: 'echo "bufadd"'}]
4226 call assert_fails('call autocmd_add(l)', 'E216:')
4227
Yegappan Lakshmanane0ff3a72022-05-27 18:05:33 +01004228 " Test for using a list of events and patterns
4229 call autocmd_delete([#{group: 'TestAcSet'}])
4230 let l = [#{group: 'TestAcSet', event: ['BufEnter', 'BufLeave'],
4231 \ pattern: ['*.py', '*.sh'], cmd: 'echo "bufcmds"'}]
4232 call autocmd_add(l)
4233 call assert_equal([
4234 \ #{cmd: 'echo "bufcmds"', group: 'TestAcSet', pattern: '*.py',
4235 \ nested: v:false, once: v:false, event: 'BufEnter'},
4236 \ #{cmd: 'echo "bufcmds"', group: 'TestAcSet', pattern: '*.sh',
4237 \ nested: v:false, once: v:false, event: 'BufEnter'},
4238 \ #{cmd: 'echo "bufcmds"', group: 'TestAcSet', pattern: '*.py',
4239 \ nested: v:false, once: v:false, event: 'BufLeave'},
4240 \ #{cmd: 'echo "bufcmds"', group: 'TestAcSet', pattern: '*.sh',
4241 \ nested: v:false, once: v:false, event: 'BufLeave'}],
4242 \ autocmd_get(#{group: 'TestAcSet'}))
4243
4244 " Test for invalid values for 'event' item
4245 call autocmd_delete([#{group: 'TestAcSet'}])
4246 let l = [#{group: 'TestAcSet', event: test_null_string(),
4247 \ pattern: "*.py", cmd: 'echo "bufcmds"'}]
4248 call assert_fails('call autocmd_add(l)', 'E928:')
4249 let l = [#{group: 'TestAcSet', event: test_null_list(),
4250 \ pattern: "*.py", cmd: 'echo "bufcmds"'}]
4251 call assert_fails('call autocmd_add(l)', 'E714:')
4252 let l = [#{group: 'TestAcSet', event: {},
4253 \ pattern: "*.py", cmd: 'echo "bufcmds"'}]
4254 call assert_fails('call autocmd_add(l)', 'E777:')
4255 let l = [#{group: 'TestAcSet', event: [{}],
4256 \ pattern: "*.py", cmd: 'echo "bufcmds"'}]
4257 call assert_fails('call autocmd_add(l)', 'E928:')
4258 let l = [#{group: 'TestAcSet', event: [test_null_string()],
4259 \ pattern: "*.py", cmd: 'echo "bufcmds"'}]
4260 call assert_fails('call autocmd_add(l)', 'E928:')
4261 let l = [#{group: 'TestAcSet', event: 'BufEnter,BufLeave',
4262 \ pattern: '*.py', cmd: 'echo "bufcmds"'}]
4263 call assert_fails('call autocmd_add(l)', 'E216:')
4264 let l = [#{group: 'TestAcSet', event: [],
4265 \ pattern: "*.py", cmd: 'echo "bufcmds"'}]
4266 call autocmd_add(l)
4267 let l = [#{group: 'TestAcSet', event: [""],
4268 \ pattern: "*.py", cmd: 'echo "bufcmds"'}]
4269 call assert_fails('call autocmd_add(l)', 'E216:')
4270 let l = [#{group: 'TestAcSet', event: "",
4271 \ pattern: "*.py", cmd: 'echo "bufcmds"'}]
4272 call autocmd_add(l)
4273 call assert_equal([], autocmd_get(#{group: 'TestAcSet'}))
4274
4275 " Test for invalid values for 'pattern' item
4276 let l = [#{group: 'TestAcSet', event: "BufEnter",
4277 \ pattern: test_null_string(), cmd: 'echo "bufcmds"'}]
Yegappan Lakshmanan00e977c2022-06-01 12:31:53 +01004278 call assert_fails('call autocmd_add(l)', 'E928:')
Yegappan Lakshmanane0ff3a72022-05-27 18:05:33 +01004279 let l = [#{group: 'TestAcSet', event: "BufEnter",
4280 \ pattern: test_null_list(), cmd: 'echo "bufcmds"'}]
4281 call assert_fails('call autocmd_add(l)', 'E714:')
4282 let l = [#{group: 'TestAcSet', event: "BufEnter",
4283 \ pattern: {}, cmd: 'echo "bufcmds"'}]
4284 call assert_fails('call autocmd_add(l)', 'E777:')
4285 let l = [#{group: 'TestAcSet', event: "BufEnter",
4286 \ pattern: [{}], cmd: 'echo "bufcmds"'}]
4287 call assert_fails('call autocmd_add(l)', 'E928:')
4288 let l = [#{group: 'TestAcSet', event: "BufEnter",
4289 \ pattern: [test_null_string()], cmd: 'echo "bufcmds"'}]
4290 call assert_fails('call autocmd_add(l)', 'E928:')
4291 let l = [#{group: 'TestAcSet', event: "BufEnter",
4292 \ pattern: [], cmd: 'echo "bufcmds"'}]
4293 call autocmd_add(l)
4294 let l = [#{group: 'TestAcSet', event: "BufEnter",
4295 \ pattern: [""], cmd: 'echo "bufcmds"'}]
4296 call autocmd_add(l)
4297 let l = [#{group: 'TestAcSet', event: "BufEnter",
4298 \ pattern: "", cmd: 'echo "bufcmds"'}]
4299 call autocmd_add(l)
4300 call assert_equal([], autocmd_get(#{group: 'TestAcSet'}))
4301
4302 let l = [#{group: 'TestAcSet', event: 'BufEnter,abc,BufLeave',
4303 \ pattern: '*.py', cmd: 'echo "bufcmds"'}]
4304 call assert_fails('call autocmd_add(l)', 'E216:')
4305
Yegappan Lakshmanan1755a912022-05-19 10:31:47 +01004306 call assert_fails("call autocmd_add({})", 'E1211:')
4307 call assert_equal(v:false, autocmd_add(test_null_list()))
4308 call assert_true(autocmd_add([[]]))
4309 call assert_true(autocmd_add([test_null_dict()]))
4310
4311 augroup TestAcSet
4312 au!
4313 augroup END
4314
4315 call autocmd_add([#{group: 'TestAcSet'}])
4316 call autocmd_add([#{group: 'TestAcSet', event: 'BufAdd'}])
4317 call autocmd_add([#{group: 'TestAcSet', pat: '*.sh'}])
4318 call autocmd_add([#{group: 'TestAcSet', cmd: 'echo "a"'}])
4319 call autocmd_add([#{group: 'TestAcSet', event: 'BufAdd', pat: '*.sh'}])
4320 call autocmd_add([#{group: 'TestAcSet', event: 'BufAdd', cmd: 'echo "a"'}])
4321 call autocmd_add([#{group: 'TestAcSet', pat: '*.sh', cmd: 'echo "a"'}])
4322 call assert_equal([], autocmd_get(#{group: 'TestAcSet'}))
4323
4324 augroup! TestAcSet
4325endfunc
4326
4327" Test for deleting autocmd events and groups
4328func Test_autocmd_delete()
4329 " Delete an event in an autocmd group
4330 augroup TestAcSet
4331 au!
4332 au BufAdd *.sh echo "bufadd"
4333 au BufEnter *.sh echo "bufenter"
4334 augroup END
4335 call autocmd_delete([#{group: 'TestAcSet', event: 'BufAdd'}])
4336 call assert_equal([#{cmd: 'echo "bufenter"', group: 'TestAcSet',
4337 \ pattern: '*.sh', nested: v:false, once: v:false,
4338 \ event: 'BufEnter'}], autocmd_get(#{group: 'TestAcSet'}))
4339
4340 " Delete all the events in an autocmd group
4341 augroup TestAcSet
4342 au BufAdd *.sh echo "bufadd"
4343 augroup END
4344 call autocmd_delete([#{group: 'TestAcSet', event: '*'}])
4345 call assert_equal([], autocmd_get(#{group: 'TestAcSet'}))
4346
4347 " Delete a non-existing autocmd group
4348 call assert_fails("call autocmd_delete([#{group: 'abc'}])", 'E367:')
4349 " Delete a non-existing autocmd event
4350 let l = [#{group: 'TestAcSet', event: 'abc'}]
4351 call assert_fails("call autocmd_delete(l)", 'E216:')
4352 " Delete a non-existing autocmd pattern
4353 let l = [#{group: 'TestAcSet', event: 'BufAdd', pat: 'abc'}]
4354 call assert_true(autocmd_delete(l))
Yegappan Lakshmanan00e977c2022-06-01 12:31:53 +01004355 " Delete an autocmd for a non-existing buffer
4356 let l = [#{event: '*', bufnr: 9999, cmd: 'echo "x"'}]
4357 call assert_fails('call autocmd_delete(l)', 'E680:')
Yegappan Lakshmanan1755a912022-05-19 10:31:47 +01004358
4359 " Delete an autocmd group
4360 augroup TestAcSet
4361 au!
4362 au BufAdd *.sh echo "bufadd"
4363 au BufEnter *.sh echo "bufenter"
4364 augroup END
4365 call autocmd_delete([#{group: 'TestAcSet'}])
4366 call assert_fails("call autocmd_get(#{group: 'TestAcSet'})", 'E367:')
4367
4368 call assert_true(autocmd_delete([[]]))
4369 call assert_true(autocmd_delete([test_null_dict()]))
4370endfunc
4371
Bram Moolenaar8f3c3c62022-10-18 17:05:54 +01004372func Test_autocmd_split_dummy()
4373 " Autocommand trying to split a window containing a dummy buffer.
Bram Moolenaar94722c52023-01-28 19:19:03 +00004374 auto BufReadPre * exe "sbuf " .. expand("<abuf>")
Bram Moolenaar8f3c3c62022-10-18 17:05:54 +01004375 " Avoid the "W11" prompt
4376 au FileChangedShell * let v:fcs_choice = 'reload'
4377 func Xautocmd_changelist()
4378 cal writefile(['Xtestfile2:4:4'], 'Xerr')
4379 edit Xerr
4380 lex 'Xtestfile2:4:4'
4381 endfunc
4382 call Xautocmd_changelist()
Bram Moolenaar53c5c9f2022-10-18 17:25:03 +01004383 " Should get E86, but it doesn't always happen (timing?)
4384 silent! call Xautocmd_changelist()
Bram Moolenaar8f3c3c62022-10-18 17:05:54 +01004385
4386 au! BufReadPre
4387 au! FileChangedShell
4388 delfunc Xautocmd_changelist
4389 bwipe! Xerr
4390 call delete('Xerr')
4391endfunc
4392
Bram Moolenaare76062c2022-11-28 18:51:43 +00004393" This was crashing because there was only one window to execute autocommands
4394" in.
4395func Test_autocmd_nested_setbufvar()
4396 CheckFeature python3
4397
4398 set hidden
4399 edit Xaaa
4400 edit Xbbb
4401 call setline(1, 'bar')
4402 enew
4403 au BufWriteCmd Xbbb ++nested call setbufvar('Xaaa', '&ft', 'foo') | bw! Xaaa
4404 au FileType foo call py3eval('vim.current.buffer.options["cindent"]')
4405 wall
4406
4407 au! BufWriteCmd
4408 au! FileType foo
4409 set nohidden
4410 call delete('Xaaa')
4411 call delete('Xbbb')
4412 %bwipe!
4413endfunc
4414
Christian Brabandt9aee8ec2022-12-16 16:41:23 +00004415func SetupVimTest_shm()
4416 let g:bwe = []
4417 let g:brp = []
4418 set shortmess+=F
zeertzjq657b31f2023-04-15 21:28:02 +01004419 messages clear
Christian Brabandt9aee8ec2022-12-16 16:41:23 +00004420
4421 let dirname='XVimTestSHM'
4422 call mkdir(dirname, 'R')
4423 call writefile(['test'], dirname .. '/1')
4424 call writefile(['test'], dirname .. '/2')
4425 call writefile(['test'], dirname .. '/3')
4426
4427 augroup test
4428 autocmd!
4429 autocmd BufWinEnter * call add(g:bwe, $'BufWinEnter: {expand('<amatch>')}')
4430 autocmd BufReadPost * call add(g:brp, $'BufReadPost: {expand('<amatch>')}')
4431 augroup END
4432
4433 call setqflist([
4434 \ {'filename': dirname .. '/1', 'lnum': 1, 'col': 1, 'text': 'test', 'vcol': 0},
4435 \ {'filename': dirname .. '/2', 'lnum': 1, 'col': 1, 'text': 'test', 'vcol': 0},
4436 \ {'filename': dirname .. '/3', 'lnum': 1, 'col': 1, 'text': 'test', 'vcol': 0}
4437 \ ])
4438 cdo! substitute/test/TEST
4439
4440 " clean up
4441 noa enew!
4442 set shortmess&vim
4443 augroup test
4444 autocmd!
4445 augroup END
4446 augroup! test
4447endfunc
4448
4449func Test_autocmd_shortmess()
4450 CheckNotMSWindows
4451
4452 call SetupVimTest_shm()
4453 let output = execute(':mess')->split('\n')
4454
4455 let info = copy(output)->filter({idx, val -> val =~# '\d of 3'} )
4456 let bytes = copy(output)->filter({idx, val -> val =~# 'bytes'} )
4457
4458 " We test the following here:
4459 " BufReadPost should have been triggered 3 times, once per file
4460 " BufWinEnter should have been triggered 3 times, once per file
4461 " FileInfoMessage should have been shown 3 times, regardless of shm option
4462 " "(x of 3)" message from :cnext has been shown 3 times
4463
4464 call assert_equal(3, g:brp->len())
4465 call assert_equal(3, g:bwe->len())
4466 call assert_equal(3, info->len())
4467 call assert_equal(3, bytes->len())
4468
4469 delfunc SetupVimTest_shm
4470endfunc
Bram Moolenaare76062c2022-11-28 18:51:43 +00004471
Christian Brabandtf0d3d4a2024-02-15 20:15:04 +01004472func Test_autocmd_invalidates_undo_on_textchanged()
4473 CheckRunVimInTerminal
4474 let script =<< trim END
4475 set hidden
4476 " create quickfix list (at least 2 lines to move line)
4477 vimgrep /u/j %
4478
4479 " enter quickfix window
4480 cwindow
4481
4482 " set modifiable
4483 setlocal modifiable
4484
4485 " set autocmd to clear quickfix list
4486
4487 autocmd! TextChanged <buffer> call setqflist([])
4488 " move line
4489 move+1
4490 END
4491 call writefile(script, 'XTest_autocmd_invalidates_undo_on_textchanged', 'D')
4492 let buf = RunVimInTerminal('XTest_autocmd_invalidates_undo_on_textchanged', {'rows': 20})
4493 call term_sendkeys(buf, ":so %\<cr>")
4494 call term_sendkeys(buf, "G")
4495 call WaitForAssert({-> assert_match('^XTest_autocmd_invalidates_undo_on_textchanged\s*$', term_getline(buf, 20))}, 1000)
4496
4497 call StopVimInTerminal(buf)
4498endfunc
4499
Christian Brabandt55f8bba2024-02-28 23:32:00 +01004500func Test_autocmd_creates_new_buffer_on_bufleave()
4501 e a.txt
4502 e b.txt
4503 setlocal bufhidden=wipe
4504 autocmd BufLeave <buffer> diffsplit c.txt
4505 bn
4506 call assert_equal(1, winnr('$'))
4507 call assert_equal('a.txt', bufname('%'))
4508 bw a.txt
4509 bw c.txt
4510endfunc
4511
Colin Kennedye5f22802024-03-26 18:20:16 +01004512" Ensure `expected` was just recently written as a Vim session
4513func s:assert_session_path(expected)
4514 call assert_equal(a:expected, v:this_session)
4515endfunc
4516
4517" Check for `expected` after a session is written to-disk.
4518func s:watch_for_session_path(expected)
4519 execute 'autocmd SessionWritePost * ++once execute "call s:assert_session_path(\"'
4520 \ . a:expected
4521 \ . '\")"'
4522endfunc
4523
4524" Ensure v:this_session gets the full session path, if explicitly stated
4525func Test_explicit_session_absolute_path()
4526 %bwipeout!
4527
4528 let directory = getcwd()
4529
4530 let v:this_session = ""
4531 let name = "some_file.vim"
4532 let expected = fnamemodify(name, ":p")
4533 call s:watch_for_session_path(expected)
4534 execute "mksession! " .. expected
4535
4536 call delete(expected)
4537endfunc
4538
4539" Ensure v:this_session gets the full session path, if explicitly stated
4540func Test_explicit_session_relative_path()
4541 %bwipeout!
4542
4543 let directory = getcwd()
4544
4545 let v:this_session = ""
4546 let name = "some_file.vim"
4547 let expected = fnamemodify(name, ":p")
4548 call s:watch_for_session_path(expected)
4549 execute "mksession! " .. name
4550
4551 call delete(expected)
4552endfunc
4553
4554" Ensure v:this_session gets the full session path, if not specified
4555func Test_implicit_session()
4556 %bwipeout!
4557
4558 let directory = getcwd()
4559
4560 let v:this_session = ""
4561 let expected = fnamemodify("Session.vim", ":p")
4562 call s:watch_for_session_path(expected)
4563 mksession!
4564
4565 call delete(expected)
4566endfunc
4567
Christian Brabandt86032702024-03-31 18:38:09 +02004568" Test TextChangedI and TextChanged
zeertzjqc4226622024-04-03 22:38:07 +02004569func Test_Changed_ChangedI()
zeertzjq8eb75232024-04-01 14:46:20 +02004570 " Run this test in a terminal because it requires running the main loop.
zeertzjqc4226622024-04-03 22:38:07 +02004571 " Don't use CheckRunVimInTerminal as that will skip the test on Windows.
4572 CheckFeature terminal
4573 CheckNotGui
4574 " Starting a terminal to run Vim is always considered flaky.
4575 let g:test_is_flaky = 1
4576
Christian Brabandt86032702024-03-31 18:38:09 +02004577 call writefile(['one', 'two', 'three'], 'XTextChangedI2', 'D')
4578 let before =<< trim END
zeertzjqc4226622024-04-03 22:38:07 +02004579 set ttimeout ttimeoutlen=10
zeertzjq8eb75232024-04-01 14:46:20 +02004580 let [g:autocmd_n, g:autocmd_i] = ['','']
4581
4582 func TextChangedAutocmd(char)
4583 let g:autocmd_{tolower(a:char)} = a:char .. b:changedtick
zeertzjqc4226622024-04-03 22:38:07 +02004584 call writefile([$'{g:autocmd_n},{g:autocmd_i}'], 'XTextChangedI3')
zeertzjq8eb75232024-04-01 14:46:20 +02004585 endfunc
4586
4587 au TextChanged <buffer> :call TextChangedAutocmd('N')
4588 au TextChangedI <buffer> :call TextChangedAutocmd('I')
4589
Christian Brabandt86032702024-03-31 18:38:09 +02004590 nnoremap <CR> o<Esc>
zeertzjq4a653912024-04-04 21:33:36 +02004591 autocmd SafeState * ++once call writefile([''], 'XTextChangedI3')
Christian Brabandt86032702024-03-31 18:38:09 +02004592 END
4593
4594 call writefile(before, 'Xinit', 'D')
zeertzjqc4226622024-04-03 22:38:07 +02004595 let buf = term_start(
4596 \ GetVimCommandCleanTerm() .. '-n -S Xinit XTextChangedI2',
4597 \ {'term_rows': 10})
4598 call assert_equal('running', term_getstatus(buf))
zeertzjq8eb75232024-04-01 14:46:20 +02004599 call WaitForAssert({-> assert_true(filereadable('XTextChangedI3'))})
zeertzjqe9ff79a2024-04-05 20:07:39 +02004600 defer delete('XTextChangedI3')
zeertzjq4a653912024-04-04 21:33:36 +02004601 call WaitForAssert({-> assert_equal([''], readfile('XTextChangedI3'))})
Christian Brabandt86032702024-03-31 18:38:09 +02004602
zeertzjqc4226622024-04-03 22:38:07 +02004603 " TextChanged should trigger if a mapping enters and leaves Insert mode.
4604 call term_sendkeys(buf, "\<CR>")
zeertzjqe9ff79a2024-04-05 20:07:39 +02004605 call WaitForAssert({-> assert_equal('N4,', readfile('XTextChangedI3')->join("\n"))})
zeertzjqc4226622024-04-03 22:38:07 +02004606
4607 call term_sendkeys(buf, "i")
4608 call WaitForAssert({-> assert_match('^-- INSERT --', term_getline(buf, 10))})
zeertzjqe9ff79a2024-04-05 20:07:39 +02004609 call WaitForAssert({-> assert_equal('N4,', readfile('XTextChangedI3')->join("\n"))})
zeertzjqc4226622024-04-03 22:38:07 +02004610 " TextChangedI should trigger if change is done in Insert mode.
4611 call term_sendkeys(buf, "f")
zeertzjqe9ff79a2024-04-05 20:07:39 +02004612 call WaitForAssert({-> assert_equal('N4,I5', readfile('XTextChangedI3')->join("\n"))})
zeertzjqc4226622024-04-03 22:38:07 +02004613 call term_sendkeys(buf, "o")
zeertzjqe9ff79a2024-04-05 20:07:39 +02004614 call WaitForAssert({-> assert_equal('N4,I6', readfile('XTextChangedI3')->join("\n"))})
zeertzjqc4226622024-04-03 22:38:07 +02004615 call term_sendkeys(buf, "o")
zeertzjqe9ff79a2024-04-05 20:07:39 +02004616 call WaitForAssert({-> assert_equal('N4,I7', readfile('XTextChangedI3')->join("\n"))})
zeertzjqc4226622024-04-03 22:38:07 +02004617 " TextChanged shouldn't trigger when leaving Insert mode and TextChangedI
4618 " has been triggered.
4619 call term_sendkeys(buf, "\<Esc>")
4620 call WaitForAssert({-> assert_notmatch('^-- INSERT --', term_getline(buf, 10))})
zeertzjqe9ff79a2024-04-05 20:07:39 +02004621 call WaitForAssert({-> assert_equal('N4,I7', readfile('XTextChangedI3')->join("\n"))})
zeertzjqc4226622024-04-03 22:38:07 +02004622
4623 " TextChanged should trigger if change is done in Normal mode.
4624 call term_sendkeys(buf, "yyp")
zeertzjqe9ff79a2024-04-05 20:07:39 +02004625 call WaitForAssert({-> assert_equal('N8,I7', readfile('XTextChangedI3')->join("\n"))})
zeertzjqc4226622024-04-03 22:38:07 +02004626
4627 " TextChangedI shouldn't trigger if change isn't done in Insert mode.
4628 call term_sendkeys(buf, "i")
4629 call WaitForAssert({-> assert_match('^-- INSERT --', term_getline(buf, 10))})
zeertzjqe9ff79a2024-04-05 20:07:39 +02004630 call WaitForAssert({-> assert_equal('N8,I7', readfile('XTextChangedI3')->join("\n"))})
zeertzjqc4226622024-04-03 22:38:07 +02004631 call term_sendkeys(buf, "\<Esc>")
4632 call WaitForAssert({-> assert_notmatch('^-- INSERT --', term_getline(buf, 10))})
zeertzjqe9ff79a2024-04-05 20:07:39 +02004633 call WaitForAssert({-> assert_equal('N8,I7', readfile('XTextChangedI3')->join("\n"))})
zeertzjqc4226622024-04-03 22:38:07 +02004634
4635 " TextChangedI should trigger if change is a mix of Normal and Insert modes.
4636 func! s:validate_mixed_textchangedi(buf, keys)
4637 let buf = a:buf
4638 call term_sendkeys(buf, "ifoo")
4639 call WaitForAssert({-> assert_match('^-- INSERT --', term_getline(buf, 10))})
4640 call term_sendkeys(buf, "\<Esc>")
4641 call WaitForAssert({-> assert_notmatch('^-- INSERT --', term_getline(buf, 10))})
4642 call term_sendkeys(buf, ":let [g:autocmd_n, g:autocmd_i] = ['', '']\<CR>")
zeertzjqe9ff79a2024-04-05 20:07:39 +02004643 call writefile([], 'XTextChangedI3')
zeertzjqc4226622024-04-03 22:38:07 +02004644 call term_sendkeys(buf, a:keys)
4645 call WaitForAssert({-> assert_match('^-- INSERT --', term_getline(buf, 10))})
zeertzjqe9ff79a2024-04-05 20:07:39 +02004646 call WaitForAssert({-> assert_match('^,I\d\+', readfile('XTextChangedI3')->join("\n"))})
zeertzjqc4226622024-04-03 22:38:07 +02004647 call term_sendkeys(buf, "\<Esc>")
4648 call WaitForAssert({-> assert_notmatch('^-- INSERT --', term_getline(buf, 10))})
zeertzjqe9ff79a2024-04-05 20:07:39 +02004649 call WaitForAssert({-> assert_match('^,I\d\+', readfile('XTextChangedI3')->join("\n"))})
zeertzjqc4226622024-04-03 22:38:07 +02004650 endfunc
4651
4652 call s:validate_mixed_textchangedi(buf, "o")
4653 call s:validate_mixed_textchangedi(buf, "O")
4654 call s:validate_mixed_textchangedi(buf, "ciw")
4655 call s:validate_mixed_textchangedi(buf, "cc")
4656 call s:validate_mixed_textchangedi(buf, "C")
4657 call s:validate_mixed_textchangedi(buf, "s")
4658 call s:validate_mixed_textchangedi(buf, "S")
4659
4660 " clean up
4661 bwipe!
Christian Brabandt86032702024-03-31 18:38:09 +02004662endfunc
4663
zeertzjq5bf6c212024-03-31 18:41:27 +02004664" Test that filetype detection still works when SwapExists autocommand sets
4665" filetype in another buffer.
4666func Test_SwapExists_set_other_buf_filetype()
4667 let lines =<< trim END
4668 set nocompatible directory=.
4669 filetype on
4670
4671 let g:buf = bufnr()
4672 new
4673
4674 func SwapExists()
4675 let v:swapchoice = 'o'
4676 call setbufvar(g:buf, '&filetype', 'text')
4677 endfunc
4678
4679 func SafeState()
4680 edit <script>
4681 redir! > XftSwapExists.out
4682 set readonly? filetype?
4683 redir END
4684 qall!
4685 endfunc
4686
4687 autocmd SwapExists * ++nested call SwapExists()
4688 autocmd SafeState * ++nested ++once call SafeState()
4689 END
4690 call writefile(lines, 'XftSwapExists.vim', 'D')
4691
4692 new XftSwapExists.vim
4693 if RunVim('', '', ' -S XftSwapExists.vim')
4694 call assert_equal(
4695 \ ['', ' readonly', ' filetype=vim'],
4696 \ readfile('XftSwapExists.out'))
4697 call delete('XftSwapExists.out')
4698 endif
4699
4700 bwipe!
4701endfunc
4702
4703" Test that file is not marked as modified when SwapExists autocommand sets
4704" 'modified' in another buffer.
4705func Test_SwapExists_set_other_buf_modified()
4706 let lines =<< trim END
4707 set nocompatible directory=.
4708
4709 let g:buf = bufnr()
4710 new
4711
4712 func SwapExists()
4713 let v:swapchoice = 'o'
4714 call setbufvar(g:buf, '&modified', 1)
4715 endfunc
4716
4717 func SafeState()
4718 edit <script>
4719 redir! > XmodSwapExists.out
4720 set readonly? modified?
4721 redir END
4722 qall!
4723 endfunc
4724
4725 autocmd SwapExists * ++nested call SwapExists()
4726 autocmd SafeState * ++nested ++once call SafeState()
4727 END
4728 call writefile(lines, 'XmodSwapExists.vim', 'D')
4729
4730 new XmodSwapExists.vim
4731 if RunVim('', '', ' -S XmodSwapExists.vim')
4732 call assert_equal(
4733 \ ['', ' readonly', 'nomodified'],
4734 \ readfile('XmodSwapExists.out'))
4735 call delete('XmodSwapExists.out')
4736 endif
4737
4738 bwipe!
4739endfunc
4740
Jaehwang Jungeb80b832024-04-26 18:48:48 +02004741func Test_BufEnter_botline()
4742 set hidden
4743 call writefile(range(10), 'Xxx1', 'D')
4744 call writefile(range(20), 'Xxx2', 'D')
4745 edit Xxx1
4746 edit Xxx2
4747 au BufEnter Xxx1 call assert_true(line('w$') > 1)
4748 edit Xxx1
zeertzjq340643e2024-04-27 11:33:24 +02004749
4750 bwipe! Xxx1
4751 bwipe! Xxx2
Jaehwang Jungeb80b832024-04-26 18:48:48 +02004752 au! BufEnter Xxx1
4753 set hidden&vim
4754endfunc
4755
Shougo Matsushita83678842024-07-11 22:05:12 +02004756func Test_KeyInputPre()
4757 " Consume previous keys
4758 call feedkeys('', 'ntx')
4759
4760 " KeyInputPre can record input keys.
4761 let s:keys = []
4762 au KeyInputPre n call add(s:keys, v:char)
4763
4764 call feedkeys('jkjkjjj', 'ntx')
4765 call assert_equal(
4766 \ ['j', 'k', 'j', 'k', 'j', 'j', 'j'],
4767 \ s:keys)
4768
4769 unlet s:keys
4770 au! KeyInputPre
4771
4772 " KeyInputPre can handle multibyte.
4773 let s:keys = []
4774 au KeyInputPre * call add(s:keys, v:char)
4775 edit Xxx1
4776
4777 call feedkeys("iあ\<ESC>", 'ntx')
4778 call assert_equal(['i', "あ", "\<ESC>"], s:keys)
4779
4780 bwipe! Xxx1
4781 unlet s:keys
4782 au! KeyInputPre
4783
4784 " KeyInputPre can change input keys.
4785 au KeyInputPre i if v:char ==# 'a' | let v:char = 'b' | endif
4786 edit Xxx1
4787
4788 call feedkeys("iaabb\<ESC>", 'ntx')
4789 call assert_equal(getline('.'), 'bbbb')
4790
4791 bwipe! Xxx1
4792 au! KeyInputPre
4793
4794 " KeyInputPre returns multiple characters.
4795 au KeyInputPre i if v:char ==# 'a' | let v:char = 'cccc' | endif
4796 edit Xxx1
4797
4798 call feedkeys("iaabb\<ESC>", 'ntx')
4799 call assert_equal(getline('.'), 'ccbb')
4800
4801 bwipe! Xxx1
4802 au! KeyInputPre
4803
4804 " KeyInputPre can use special keys.
4805 au KeyInputPre i if v:char ==# 'a' | let v:char = "\<Ignore>" | endif
4806 edit Xxx1
4807
4808 call feedkeys("iaabb\<ESC>", 'ntx')
4809 call assert_equal(getline('.'), 'bb')
4810
4811 bwipe! Xxx1
4812 au! KeyInputPre
4813
4814 " Test for v:event.typed
4815 au KeyInputPre n call assert_true(v:event.typed)
4816 call feedkeys('j', 'ntx')
4817
4818 au! KeyInputPre
4819
4820 au KeyInputPre n call assert_false(v:event.typed)
4821 call feedkeys('j', 'nx')
4822
4823 au! KeyInputPre
Shougo Matsushitafcc1b572024-07-17 20:25:22 +02004824
4825 " Test for v:event.typedchar
4826 nnoremap j k
4827 au KeyInputPre n
4828 \ call assert_equal(v:event.typedchar, 'j')
4829 \ | call assert_equal(v:char, 'k')
4830 call feedkeys('j', 'tx')
4831
4832 au! KeyInputPre
Shougo Matsushita83678842024-07-11 22:05:12 +02004833endfunc
4834
Christian Brabandtfb3f9692024-08-11 20:09:17 +02004835" those commands caused null pointer access, see #15464
4836func Test_WinNewPre_crash()
4837 defer CleanUpTestAuGroup()
4838 let _cmdheight=&cmdheight
4839 augroup testing
4840 au!
4841 autocmd WinNewPre * redraw
4842 augroup END
4843 tabnew
4844 tabclose
4845 augroup testing
4846 au!
4847 autocmd WinNewPre * wincmd t
4848 augroup END
4849 tabnew
4850 tabclose
4851 augroup testing
4852 au!
4853 autocmd WinNewPre * wincmd b
4854 augroup END
4855 tabnew
4856 tabclose
4857 augroup testing
4858 au!
4859 autocmd WinNewPre * set cmdheight+=1
4860 augroup END
4861 tabnew
4862 tabclose
4863 let &cmdheight=_cmdheight
4864endfunc
4865
Christian Brabandt84e31752024-09-02 09:59:18 +02004866" The specifics of the turkish locale may
4867" cause that Vim will not treat the GuiEnter autocommand
4868" as case insensitive and instead issues an error
4869func Test_GuiEnter_Turkish_locale()
4870 try
4871 let lng = v:lang
4872 lang tr_TR.UTF-8
4873 let result = execute(':au GuiEnter')
Ken Takataeccc9272024-09-03 23:01:55 +02004874 call assert_equal(gettext("\n--- Autocommands ---"), result)
Christian Brabandt84e31752024-09-02 09:59:18 +02004875 let result = execute(':au GUIENTER')
Ken Takataeccc9272024-09-03 23:01:55 +02004876 call assert_equal(gettext("\n--- Autocommands ---"), result)
Christian Brabandt84e31752024-09-02 09:59:18 +02004877 let result = execute(':au guienter')
Ken Takataeccc9272024-09-03 23:01:55 +02004878 call assert_equal(gettext("\n--- Autocommands ---"), result)
Christian Brabandt84e31752024-09-02 09:59:18 +02004879 exe ":lang" lng
4880 catch /E197:/
4881 " can't use Turkish locale
4882 throw 'Skipped: Turkish locale not available'
4883 endtry
4884endfunc
Christian Brabandtfb3f9692024-08-11 20:09:17 +02004885
Christian Brabandt51b62382024-10-06 17:31:10 +02004886" This was using freed memory
4887func Test_autocmd_BufWinLeave_with_vsp()
4888 new
4889 let fname = 'XXXBufWinLeaveUAF.txt'
4890 let dummy = 'XXXDummy.txt'
4891 call writefile([], fname)
4892 call writefile([], dummy)
4893 defer delete(fname)
4894 defer delete(dummy)
4895 exe "e " fname
4896 vsp
4897 augroup testing
4898 exe "au BufWinLeave " .. fname .. " :e " dummy .. "| vsp " .. fname
4899 augroup END
4900 bw
4901 call CleanUpTestAuGroup()
4902 exe "bw! " .. dummy
4903endfunc
4904
Luuk van Baale15cbc12025-01-04 17:18:08 +01004905func Test_OptionSet_cmdheight()
4906 set mouse=a laststatus=2
4907 au OptionSet cmdheight :let &l:ch = v:option_new
4908
4909 resize -1
4910 call assert_equal(2, &l:ch)
4911 resize +1
4912 call assert_equal(1, &l:ch)
4913
4914 call test_setmouse(&lines - 1, 1)
4915 call feedkeys("\<LeftMouse>", 'xt')
4916 call test_setmouse(&lines - 2, 1)
4917 call feedkeys("\<LeftDrag>", 'xt')
4918 call assert_equal(2, &l:ch)
4919
4920 tabnew | resize +1
4921 call assert_equal(1, &l:ch)
4922 tabfirst
4923 call assert_equal(2, &l:ch)
4924
4925 tabonly
4926 set cmdheight& mouse& laststatus&
4927endfunc
4928
Luuk van Baalb7147f82025-02-08 18:52:39 +01004929func Test_eventignorewin()
4930 defer CleanUpTestAuGroup()
4931 augroup testing
4932 au WinEnter * :call add(g:evs, ["WinEnter", expand("<afile>")])
4933 au WinLeave * :call add(g:evs, ["WinLeave", expand("<afile>")])
4934 au BufWinEnter * :call add(g:evs, ["BufWinEnter", expand("<afile>")])
4935 augroup END
4936
4937 let g:evs = []
4938 set eventignorewin=WinLeave,WinEnter
4939 split foo
4940 call assert_equal([['BufWinEnter', 'foo']], g:evs)
4941 set eventignorewin=all
4942 edit bar
4943 call assert_equal([['BufWinEnter', 'foo']], g:evs)
4944 set eventignorewin=
4945 wincmd w
4946 call assert_equal([['BufWinEnter', 'foo'], ['WinLeave', 'bar']], g:evs)
4947
4948 only!
4949 %bwipe!
4950 set eventignorewin&
4951 unlet g:evs
4952endfunc
4953
4954func Test_WinScrolled_Resized_eiw()
4955 CheckRunVimInTerminal
4956
4957 let lines =<< trim END
4958 call setline(1, ['foo']->repeat(32))
4959 set eventignorewin=WinScrolled,WinResized
4960 split
4961 let [g:afile,g:resized,g:scrolled] = ['none',0,0]
4962 au WinScrolled * let [g:afile,g:scrolled] = [expand('<afile>'),g:scrolled+1]
4963 au WinResized * let [g:afile,g:resized] = [expand('<afile>'),g:resized+1]
4964 END
Christian Brabandtbfc77192025-02-11 20:03:10 +01004965 call writefile(lines, 'Xtest_winscrolled_eiw', 'D')
4966 let buf = RunVimInTerminal('-S Xtest_winscrolled_eiw', {'rows': 10})
Luuk van Baalb7147f82025-02-08 18:52:39 +01004967
4968 " Both windows are ignoring resize events
4969 call term_sendkeys(buf, "\<C-W>-")
4970 call TermWait(buf)
4971 call term_sendkeys(buf, ":echo g:afile g:resized g:scrolled\<CR>")
4972 call WaitForAssert({-> assert_equal('none 0 0', term_getline(buf, 10))}, 1000)
4973
4974 " And scroll events
4975 call term_sendkeys(buf, "Ggg")
4976 call TermWait(buf)
4977 call term_sendkeys(buf, ":echo g:afile g:resized g:scrolled\<CR>")
4978 call WaitForAssert({-> assert_equal('none 0 0', term_getline(buf, 10))}, 1000)
4979
4980 " Un-ignore events in second window, make first window current and resize
4981 call term_sendkeys(buf, ":set eventignorewin=\<CR>\<C-W>w\<C-W>+")
4982 call TermWait(buf)
4983 call term_sendkeys(buf, ":echo win_getid() g:afile g:resized g:scrolled\<CR>")
4984 call WaitForAssert({-> assert_equal('1000 1001 1 1', term_getline(buf, 10))}, 1000)
4985
4986 call StopVimInTerminal(buf)
4987endfunc
4988
Jim Zhou5606ca52025-03-13 21:58:25 +01004989" Test that TabClosedPre and TabClosed are triggered when closing a tab.
4990func Test_autocmd_tabclosedpre()
4991 augroup testing
4992 au TabClosedPre * call add(g:tabpagenr_pre, t:testvar)
4993 au TabClosed * call add(g:tabpagenr_post, t:testvar)
4994 augroup END
4995
4996 " Test 'tabclose' triggering
4997 let g:tabpagenr_pre = []
4998 let g:tabpagenr_post = []
4999 let t:testvar = 1
5000 tabnew
5001 let t:testvar = 2
5002 tabnew
5003 let t:testvar = 3
5004 tabnew
5005 let t:testvar = 4
5006 tabnext
5007 tabclose
5008 tabclose
5009 tabclose
5010 call assert_equal([1, 2, 3], g:tabpagenr_pre)
5011 call assert_equal([2, 3, 4], g:tabpagenr_post)
5012
5013 " Test 'tabclose {count}' triggering
5014 let g:tabpagenr_pre = []
5015 let g:tabpagenr_post = []
5016 let t:testvar = 1
5017 tabnew
5018 let t:testvar = 2
5019 tabnew
5020 let t:testvar = 3
5021 tabclose 2
5022 tabclose 2
5023 call assert_equal([2, 3], g:tabpagenr_pre)
5024 call assert_equal([3, 1], g:tabpagenr_post)
5025
5026 " Test 'tabonly' triggering
5027 let g:tabpagenr_pre = []
5028 let g:tabpagenr_post = []
5029 let t:testvar = 1
5030 tabnew
5031 let t:testvar = 2
5032 tabonly
5033 call assert_equal([1], g:tabpagenr_pre)
5034 call assert_equal([2], g:tabpagenr_post)
5035
5036 " Test 'q' and 'close' triggering (closing the last window in a tab)
5037 let g:tabpagenr_pre = []
5038 let g:tabpagenr_post = []
5039 split
5040 let t:testvar = 1
5041 tabnew
5042 let t:testvar = 2
5043 split
5044 vsplit
5045 tabnew
5046 let t:testvar = 3
5047 tabnext
5048 only
5049 quit
5050 quit
5051 close
5052 close
5053 call assert_equal([1, 2], g:tabpagenr_pre)
5054 call assert_equal([2, 3], g:tabpagenr_post)
5055
5056 func ClearAutomcdAndCreateTabs()
5057 au! TabClosedPre
5058 bw!
5059 e Z
5060 tabonly
5061 tabnew A
5062 tabnew B
5063 tabnew C
5064 endfunc
5065
5066 func GetTabs()
5067 redir => tabsout
5068 tabs
5069 redir END
5070 let tabsout = substitute(tabsout, '\n', '', 'g')
5071 let tabsout = substitute(tabsout, 'Tab page ', '', 'g')
5072 let tabsout = substitute(tabsout, ' ', '', 'g')
5073 return tabsout
5074 endfunc
5075
5076 call CleanUpTestAuGroup()
5077
5078 " Close tab in TabClosedPre autocmd
5079 call ClearAutomcdAndCreateTabs()
5080 au TabClosedPre * tabclose
zeertzjq67fe77d2025-04-20 10:21:18 +02005081 call assert_fails('tabclose', 'E1312:')
Jim Zhou5606ca52025-03-13 21:58:25 +01005082 call ClearAutomcdAndCreateTabs()
5083 au TabClosedPre * tabclose
zeertzjq67fe77d2025-04-20 10:21:18 +02005084 call assert_fails('tabclose 2', 'E1312:')
Jim Zhou5606ca52025-03-13 21:58:25 +01005085 call ClearAutomcdAndCreateTabs()
5086 au TabClosedPre * tabclose 1
zeertzjq67fe77d2025-04-20 10:21:18 +02005087 call assert_fails('tabclose', 'E1312:')
Jim Zhou5606ca52025-03-13 21:58:25 +01005088
5089 " Close other (all) tabs in TabClosedPre autocmd
5090 call ClearAutomcdAndCreateTabs()
5091 au TabClosedPre * tabonly
zeertzjq67fe77d2025-04-20 10:21:18 +02005092 call assert_fails('tabclose', 'E1312:')
Jim Zhou5606ca52025-03-13 21:58:25 +01005093 call ClearAutomcdAndCreateTabs()
5094 au TabClosedPre * tabonly
zeertzjq67fe77d2025-04-20 10:21:18 +02005095 call assert_fails('tabclose 2', 'E1312:')
Jim Zhou5606ca52025-03-13 21:58:25 +01005096 call ClearAutomcdAndCreateTabs()
5097 au TabClosedPre * tabclose 4
zeertzjq67fe77d2025-04-20 10:21:18 +02005098 call assert_fails('tabclose 2', 'E1312:')
Jim Zhou5606ca52025-03-13 21:58:25 +01005099
5100 " Open new tabs in TabClosedPre autocmd
5101 call ClearAutomcdAndCreateTabs()
5102 au TabClosedPre * tabnew D
zeertzjq67fe77d2025-04-20 10:21:18 +02005103 call assert_fails('tabclose', 'E1312:')
Jim Zhou5606ca52025-03-13 21:58:25 +01005104 call ClearAutomcdAndCreateTabs()
5105 au TabClosedPre * tabnew D
zeertzjq67fe77d2025-04-20 10:21:18 +02005106 call assert_fails('tabclose 1', 'E1312:')
Jim Zhou5606ca52025-03-13 21:58:25 +01005107
5108 " Moving the tab page in TabClosedPre autocmd
5109 call ClearAutomcdAndCreateTabs()
5110 au TabClosedPre * tabmove 0
5111 tabclose
Jim Zhoubcf66e02025-03-16 20:24:57 +01005112 call assert_equal('1>Z2A3B', GetTabs())
Jim Zhou5606ca52025-03-13 21:58:25 +01005113 call ClearAutomcdAndCreateTabs()
5114 au TabClosedPre * tabmove 0
5115 tabclose 1
5116 call assert_equal('1A2B3>C', GetTabs())
5117 tabonly
5118 call assert_equal('1>C', GetTabs())
5119
5120 " Switching tab page in TabClosedPre autocmd
5121 call ClearAutomcdAndCreateTabs()
5122 au TabClosedPre * tabnext | e Y
5123 tabclose
5124 call assert_equal('1Y2A3>B', GetTabs())
5125 call ClearAutomcdAndCreateTabs()
5126 au TabClosedPre * tabnext | e Y
5127 tabclose 1
5128 call assert_equal('1Y2B3>C', GetTabs())
5129 tabonly
5130 call assert_equal('1>Y', GetTabs())
5131
5132 " Create new windows in TabClosedPre autocmd
5133 call ClearAutomcdAndCreateTabs()
5134 au TabClosedPre * split | e X| vsplit | e Y | split | e Z
zeertzjq67fe77d2025-04-20 10:21:18 +02005135 call assert_fails('tabclose', 'E242:')
Jim Zhou5606ca52025-03-13 21:58:25 +01005136 call ClearAutomcdAndCreateTabs()
5137 au TabClosedPre * new X | new Y | new Z
zeertzjq67fe77d2025-04-20 10:21:18 +02005138 call assert_fails('tabclose 1', 'E242:')
Jim Zhou5606ca52025-03-13 21:58:25 +01005139
Jim Zhoubcf66e02025-03-16 20:24:57 +01005140 " Test directly closing the tab page with ':tabclose'
5141 au!
5142 tabonly
5143 bw!
5144 e Z
5145 au TabClosedPre * mksession!
5146 tabnew A
5147 sp
5148 tabclose
5149 source Session.vim
5150 call assert_equal('1Z2>AA', GetTabs())
5151
5152 " Test directly closing the tab page with ':tabonly'
5153 " Z is closed before A. Hence A overwrites the session.
5154 au!
5155 tabonly
5156 bw!
5157 e Z
5158 au TabClosedPre * mksession!
5159 tabnew A
5160 tabnew B
5161 tabonly
5162 source Session.vim
5163 call assert_equal('1>A2B', GetTabs())
5164
Jim Zhou5606ca52025-03-13 21:58:25 +01005165 " Clean up
Jim Zhoubcf66e02025-03-16 20:24:57 +01005166 call delete('Session.vim')
Jim Zhou5606ca52025-03-13 21:58:25 +01005167 au!
5168 only
5169 tabonly
5170 bw!
5171endfunc
5172
Bram Moolenaarbc2b71d2020-02-17 21:33:30 +01005173" vim: shiftwidth=2 sts=2 expandtab