blob: 1988807f75c66cf34d4e8eb929b8f99ba1e49fc9 [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()
Drew Vogelea67ba72025-05-07 22:05:17 +0200583 CheckScreendump
Bram Moolenaar0a60f792022-11-19 21:18:11 +0000584 CheckRunVimInTerminal
585
586 let lines =<< trim END
587 set cmdheight=2
588 call setline(1, ['aaa', 'bbb'])
589 let trigger_count = 0
590 func ShowInfo(id)
591 echo g:trigger_count g:winid winlayout()
592 endfunc
593
594 vsplit
595 split
596 " use a timer to show the info after a redraw
597 au WinScrolled * let trigger_count += 1 | let winid = expand('<amatch>') | call timer_start(100, 'ShowInfo')
598 wincmd j
599 wincmd l
600 END
601 call writefile(lines, 'Xtest_winscrolled_once', 'D')
602 let buf = RunVimInTerminal('-S Xtest_winscrolled_once', #{rows: 10, cols: 60, statusoff: 2})
603
604 call term_sendkeys(buf, "\<C-E>")
605 call VerifyScreenDump(buf, 'Test_winscrolled_once_only_1', {})
606
607 call StopVimInTerminal(buf)
608endfunc
609
Bram Moolenaar29967732022-11-20 12:11:45 +0000610" Check that WinScrolled is not triggered immediately when defined and there
611" are split windows.
612func Test_WinScrolled_not_when_defined()
Drew Vogelea67ba72025-05-07 22:05:17 +0200613 CheckScreendump
Bram Moolenaar29967732022-11-20 12:11:45 +0000614 CheckRunVimInTerminal
615
616 let lines =<< trim END
617 call setline(1, ['aaa', 'bbb'])
618 echo 'nothing happened'
619 func ShowTriggered(id)
620 echo 'triggered'
621 endfunc
622 END
623 call writefile(lines, 'Xtest_winscrolled_not', 'D')
624 let buf = RunVimInTerminal('-S Xtest_winscrolled_not', #{rows: 10, cols: 60, statusoff: 2})
625 call term_sendkeys(buf, ":split\<CR>")
626 call TermWait(buf)
627 " use a timer to show the message after redrawing
628 call term_sendkeys(buf, ":au WinScrolled * call timer_start(100, 'ShowTriggered')\<CR>")
629 call VerifyScreenDump(buf, 'Test_winscrolled_not_when_defined_1', {})
630
631 call term_sendkeys(buf, "\<C-E>")
632 call VerifyScreenDump(buf, 'Test_winscrolled_not_when_defined_2', {})
633
634 call StopVimInTerminal(buf)
635endfunc
636
zeertzjq670ab032022-08-28 19:16:15 +0100637func Test_WinScrolled_long_wrapped()
638 CheckRunVimInTerminal
639
640 let lines =<< trim END
641 set scrolloff=0
642 let height = winheight(0)
643 let width = winwidth(0)
644 let g:scrolled = 0
645 au WinScrolled * let g:scrolled += 1
646 call setline(1, repeat('foo', height * width))
647 call cursor(1, height * width)
648 END
Bram Moolenaare1f3ab72022-09-04 21:29:08 +0100649 call writefile(lines, 'Xtest_winscrolled_long_wrapped', 'D')
zeertzjq670ab032022-08-28 19:16:15 +0100650 let buf = RunVimInTerminal('-S Xtest_winscrolled_long_wrapped', {'rows': 6})
651
652 call term_sendkeys(buf, ":echo g:scrolled\<CR>")
653 call WaitForAssert({-> assert_match('^0 ', term_getline(buf, 6))}, 1000)
654
655 call term_sendkeys(buf, 'gj')
656 call term_sendkeys(buf, ":echo g:scrolled\<CR>")
657 call WaitForAssert({-> assert_match('^1 ', term_getline(buf, 6))}, 1000)
658
659 call term_sendkeys(buf, '0')
660 call term_sendkeys(buf, ":echo g:scrolled\<CR>")
661 call WaitForAssert({-> assert_match('^2 ', term_getline(buf, 6))}, 1000)
662
663 call term_sendkeys(buf, '$')
664 call term_sendkeys(buf, ":echo g:scrolled\<CR>")
665 call WaitForAssert({-> assert_match('^3 ', term_getline(buf, 6))}, 1000)
Bram Moolenaar23526d22022-12-05 15:50:41 +0000666
667 call StopVimInTerminal(buf)
zeertzjq670ab032022-08-28 19:16:15 +0100668endfunc
669
zeertzjq3fc84dc2022-12-07 09:17:59 +0000670func Test_WinScrolled_diff()
671 CheckRunVimInTerminal
672
673 let lines =<< trim END
674 set diffopt+=foldcolumn:0
675 call setline(1, ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i'])
676 vnew
677 call setline(1, ['d', 'e', 'f', 'g', 'h', 'i'])
678 windo diffthis
679 func WriteScrollEvent()
680 call writefile([json_encode(v:event)], 'XscrollEvent')
681 endfunc
682 au WinScrolled * call WriteScrollEvent()
683 END
684 call writefile(lines, 'Xtest_winscrolled_diff', 'D')
685 let buf = RunVimInTerminal('-S Xtest_winscrolled_diff', {'rows': 8})
686
687 call term_sendkeys(buf, "\<C-E>")
688 call WaitForAssert({-> assert_match('^d', term_getline(buf, 3))}, 1000)
689
690 let event = readfile('XscrollEvent')[0]->json_decode()
691 call assert_equal({
692 \ 'all': {'leftcol': 0, 'topline': 1, 'topfill': 1, 'width': 0, 'height': 0, 'skipcol': 0},
693 \ '1000': {'leftcol': 0, 'topline': 1, 'topfill': 0, 'width': 0, 'height': 0, 'skipcol': 0},
694 \ '1001': {'leftcol': 0, 'topline': 0, 'topfill': -1, 'width': 0, 'height': 0, 'skipcol': 0}
695 \ }, event)
696
697 call term_sendkeys(buf, "2\<C-E>")
698 call WaitForAssert({-> assert_match('^f', term_getline(buf, 3))}, 1000)
699
700 let event = readfile('XscrollEvent')[0]->json_decode()
701 call assert_equal({
702 \ 'all': {'leftcol': 0, 'topline': 2, 'topfill': 2, 'width': 0, 'height': 0, 'skipcol': 0},
703 \ '1000': {'leftcol': 0, 'topline': 2, 'topfill': 0, 'width': 0, 'height': 0, 'skipcol': 0},
704 \ '1001': {'leftcol': 0, 'topline': 0, 'topfill': -2, 'width': 0, 'height': 0, 'skipcol': 0}
705 \ }, event)
706
707 call term_sendkeys(buf, "\<C-E>")
708 call WaitForAssert({-> assert_match('^g', term_getline(buf, 3))}, 1000)
709
710 let event = readfile('XscrollEvent')[0]->json_decode()
711 call assert_equal({
712 \ 'all': {'leftcol': 0, 'topline': 2, 'topfill': 0, 'width': 0, 'height': 0, 'skipcol': 0},
713 \ '1000': {'leftcol': 0, 'topline': 1, 'topfill': 0, 'width': 0, 'height': 0, 'skipcol': 0},
714 \ '1001': {'leftcol': 0, 'topline': 1, 'topfill': 0, 'width': 0, 'height': 0, 'skipcol': 0}
715 \ }, event)
716
717 call term_sendkeys(buf, "2\<C-Y>")
718 call WaitForAssert({-> assert_match('^e', term_getline(buf, 3))}, 1000)
719
720 let event = readfile('XscrollEvent')[0]->json_decode()
721 call assert_equal({
722 \ 'all': {'leftcol': 0, 'topline': 3, 'topfill': 1, 'width': 0, 'height': 0, 'skipcol': 0},
723 \ '1000': {'leftcol': 0, 'topline': -2, 'topfill': 0, 'width': 0, 'height': 0, 'skipcol': 0},
724 \ '1001': {'leftcol': 0, 'topline': -1, 'topfill': 1, 'width': 0, 'height': 0, 'skipcol': 0}
725 \ }, event)
726
727 call StopVimInTerminal(buf)
Dominique Pelle541c87c2023-01-17 21:20:44 +0000728 call delete('XscrollEvent')
zeertzjq3fc84dc2022-12-07 09:17:59 +0000729endfunc
730
naohiro ono23beefe2021-11-13 12:38:49 +0000731func Test_WinClosed()
732 " Test that the pattern is matched against the closed window's ID, and both
733 " <amatch> and <afile> are set to it.
734 new
735 let winid = win_getid()
736 let g:matched = v:false
737 augroup test-WinClosed
738 autocmd!
739 execute 'autocmd WinClosed' winid 'let g:matched = v:true'
740 autocmd WinClosed * let g:amatch = str2nr(expand('<amatch>'))
741 autocmd WinClosed * let g:afile = str2nr(expand('<afile>'))
742 augroup END
743 close
744 call assert_true(g:matched)
745 call assert_equal(winid, g:amatch)
746 call assert_equal(winid, g:afile)
747
748 " Test that WinClosed is non-recursive.
749 new
750 new
751 call assert_equal(3, winnr('$'))
752 let g:triggered = 0
753 augroup test-WinClosed
754 autocmd!
755 autocmd WinClosed * let g:triggered += 1
756 autocmd WinClosed * 2 wincmd c
757 augroup END
758 close
759 call assert_equal(1, winnr('$'))
760 call assert_equal(1, g:triggered)
761
762 autocmd! test-WinClosed
763 augroup! test-WinClosed
764 unlet g:matched
765 unlet g:amatch
766 unlet g:afile
767 unlet g:triggered
768endfunc
769
Bram Moolenaarc947b9a2022-04-06 17:59:21 +0100770func Test_WinClosed_throws()
771 vnew
772 let bnr = bufnr()
773 call assert_equal(1, bufloaded(bnr))
774 augroup test-WinClosed
775 autocmd WinClosed * throw 'foo'
776 augroup END
777 try
778 close
779 catch /.*/
780 endtry
781 call assert_equal(0, bufloaded(bnr))
782
783 autocmd! test-WinClosed
784 augroup! test-WinClosed
785endfunc
786
zeertzjq6a069402022-04-07 14:08:29 +0100787func Test_WinClosed_throws_with_tabs()
788 tabnew
789 let bnr = bufnr()
790 call assert_equal(1, bufloaded(bnr))
791 augroup test-WinClosed
792 autocmd WinClosed * throw 'foo'
793 augroup END
794 try
795 close
796 catch /.*/
797 endtry
798 call assert_equal(0, bufloaded(bnr))
799
800 autocmd! test-WinClosed
801 augroup! test-WinClosed
802endfunc
803
zeertzjq62de54b2022-09-22 18:08:37 +0100804" This used to trigger WinClosed twice for the same window, and the window's
805" buffer was NULL in the second autocommand.
806func Test_WinClosed_switch_tab()
807 edit Xa
808 split Xb
809 split Xc
810 tab split
811 new
812 augroup test-WinClosed
813 autocmd WinClosed * tabprev | bwipe!
814 augroup END
815 close
816 " Check that the tabline has been fully removed
817 call assert_equal([1, 1], win_screenpos(0))
818
819 autocmd! test-WinClosed
820 augroup! test-WinClosed
821 %bwipe!
822endfunc
823
zeertzjqb2ec0da2024-03-09 15:39:27 +0100824" This used to trigger WinClosed twice for the same window, and the window's
825" buffer was NULL in the second autocommand.
826func Test_WinClosed_BufUnload_close_other()
827 tabnew
828 let g:tab = tabpagenr()
829 let g:buf = bufnr()
830 new
831 setlocal bufhidden=wipe
832 augroup test-WinClosed
833 autocmd BufUnload * ++once exe g:buf .. 'bwipe!'
834 autocmd WinClosed * call tabpagebuflist(g:tab)
835 augroup END
836 close
837
838 unlet g:tab
839 unlet g:buf
840 autocmd! test-WinClosed
841 augroup! test-WinClosed
842 %bwipe!
843endfunc
844
Bram Moolenaare99e8442016-07-26 20:43:40 +0200845func s:AddAnAutocmd()
846 augroup vimBarTest
847 au BufReadCmd * echo 'hello'
848 augroup END
849 call assert_equal(3, len(split(execute('au vimBarTest'), "\n")))
850endfunc
851
852func Test_early_bar()
853 " test that a bar is recognized before the {event}
854 call s:AddAnAutocmd()
Bram Moolenaarb8e642f2021-11-20 10:38:25 +0000855 augroup vimBarTest | au! | let done = 77 | augroup END
Bram Moolenaare99e8442016-07-26 20:43:40 +0200856 call assert_equal(1, len(split(execute('au vimBarTest'), "\n")))
Bram Moolenaarb8e642f2021-11-20 10:38:25 +0000857 call assert_equal(77, done)
Bram Moolenaare99e8442016-07-26 20:43:40 +0200858
859 call s:AddAnAutocmd()
Bram Moolenaarb8e642f2021-11-20 10:38:25 +0000860 augroup vimBarTest| au!| let done = 88 | augroup END
Bram Moolenaare99e8442016-07-26 20:43:40 +0200861 call assert_equal(1, len(split(execute('au vimBarTest'), "\n")))
Bram Moolenaarb8e642f2021-11-20 10:38:25 +0000862 call assert_equal(88, done)
Bram Moolenaare99e8442016-07-26 20:43:40 +0200863
864 " test that a bar is recognized after the {event}
865 call s:AddAnAutocmd()
Bram Moolenaarb8e642f2021-11-20 10:38:25 +0000866 augroup vimBarTest| au!BufReadCmd| let done = 99 | augroup END
Bram Moolenaare99e8442016-07-26 20:43:40 +0200867 call assert_equal(1, len(split(execute('au vimBarTest'), "\n")))
Bram Moolenaarb8e642f2021-11-20 10:38:25 +0000868 call assert_equal(99, done)
Bram Moolenaare99e8442016-07-26 20:43:40 +0200869
870 " test that a bar is recognized after the {group}
871 call s:AddAnAutocmd()
872 au! vimBarTest|echo 'hello'
873 call assert_equal(1, len(split(execute('au vimBarTest'), "\n")))
874endfunc
Bram Moolenaarf2c4c392016-07-29 20:50:24 +0200875
Bram Moolenaar5c809082016-09-01 16:21:48 +0200876func RemoveGroup()
877 autocmd! StartOK
878 augroup! StartOK
879endfunc
880
Bram Moolenaarf2c4c392016-07-29 20:50:24 +0200881func Test_augroup_warning()
882 augroup TheWarning
883 au VimEnter * echo 'entering'
884 augroup END
Bram Moolenaar5dc4e2f2020-11-25 14:15:12 +0100885 call assert_match("TheWarning.*VimEnter", execute('au VimEnter'))
Bram Moolenaarf2c4c392016-07-29 20:50:24 +0200886 redir => res
887 augroup! TheWarning
888 redir END
Bram Moolenaar5dc4e2f2020-11-25 14:15:12 +0100889 call assert_match("W19:", res)
890 call assert_match("-Deleted-.*VimEnter", execute('au VimEnter'))
Bram Moolenaarf2c4c392016-07-29 20:50:24 +0200891
892 " check "Another" does not take the pace of the deleted entry
893 augroup Another
894 augroup END
Bram Moolenaar5dc4e2f2020-11-25 14:15:12 +0100895 call assert_match("-Deleted-.*VimEnter", execute('au VimEnter'))
Bram Moolenaaraeac9002016-09-06 22:15:08 +0200896 augroup! Another
Bram Moolenaar5c809082016-09-01 16:21:48 +0200897
898 " no warning for postpone aucmd delete
899 augroup StartOK
900 au VimEnter * call RemoveGroup()
901 augroup END
Bram Moolenaar5dc4e2f2020-11-25 14:15:12 +0100902 call assert_match("StartOK.*VimEnter", execute('au VimEnter'))
Bram Moolenaar5c809082016-09-01 16:21:48 +0200903 redir => res
904 doautocmd VimEnter
905 redir END
Bram Moolenaar5dc4e2f2020-11-25 14:15:12 +0100906 call assert_notmatch("W19:", res)
Bram Moolenaarde653f02016-09-03 16:59:06 +0200907 au! VimEnter
Bram Moolenaarad48e6c2020-04-21 22:19:45 +0200908
909 call assert_fails('augroup!', 'E471:')
Bram Moolenaarf2c4c392016-07-29 20:50:24 +0200910endfunc
Bram Moolenaarb62cc362016-09-03 16:43:53 +0200911
Bram Moolenaar8d84ff12017-10-26 16:42:16 +0200912func Test_BufReadCmdHelp()
913 " This used to cause access to free memory
914 au BufReadCmd * e +h
915 help
916
Bram Moolenaar8d84ff12017-10-26 16:42:16 +0200917 au! BufReadCmd
918endfunc
919
920func Test_BufReadCmdHelpJump()
921 " This used to cause access to free memory
922 au BufReadCmd * e +h{
Bram Moolenaarcf1ba352017-10-27 00:55:04 +0200923 " } to fix highlighting
924 call assert_fails('help', 'E434:')
Bram Moolenaar8d84ff12017-10-26 16:42:16 +0200925
Bram Moolenaar8d84ff12017-10-26 16:42:16 +0200926 au! BufReadCmd
927endfunc
928
zeertzjq93f72cc2022-08-26 15:34:52 +0100929" BufReadCmd is triggered for a "nofile" buffer. Check all values.
Bram Moolenaarb1d2c812022-08-26 11:55:01 +0100930func Test_BufReadCmdNofile()
zeertzjq93f72cc2022-08-26 15:34:52 +0100931 for val in ['nofile',
932 \ 'nowrite',
933 \ 'acwrite',
934 \ 'quickfix',
935 \ 'help',
936 \ 'terminal',
937 \ 'prompt',
938 \ 'popup',
939 \ ]
940 new somefile
941 exe 'set buftype=' .. val
942 au BufReadCmd somefile call setline(1, 'triggered')
943 edit
944 call assert_equal('triggered', getline(1))
Bram Moolenaarb1d2c812022-08-26 11:55:01 +0100945
zeertzjq93f72cc2022-08-26 15:34:52 +0100946 au! BufReadCmd
947 bwipe!
948 endfor
Bram Moolenaarb1d2c812022-08-26 11:55:01 +0100949endfunc
950
Bram Moolenaarb62cc362016-09-03 16:43:53 +0200951func Test_augroup_deleted()
Bram Moolenaarde653f02016-09-03 16:59:06 +0200952 " This caused a crash before E936 was introduced
Bram Moolenaarb62cc362016-09-03 16:43:53 +0200953 augroup x
Bram Moolenaarde653f02016-09-03 16:59:06 +0200954 call assert_fails('augroup! x', 'E936:')
955 au VimEnter * echo
956 augroup end
Bram Moolenaarb62cc362016-09-03 16:43:53 +0200957 augroup! x
Bram Moolenaar5dc4e2f2020-11-25 14:15:12 +0100958 call assert_match("-Deleted-.*VimEnter", execute('au VimEnter'))
Bram Moolenaarde653f02016-09-03 16:59:06 +0200959 au! VimEnter
Bram Moolenaarb62cc362016-09-03 16:43:53 +0200960endfunc
961
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200962" Tests for autocommands on :close command.
963" This used to be in test13.
964func Test_three_windows()
Bram Moolenaarb3435b02016-09-29 20:54:59 +0200965 " Clean up buffers, because in some cases this function fails.
966 call s:cleanup_buffers()
967
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200968 " Write three files and open them, each in a window.
969 " Then go to next window, with autocommand that deletes the previous one.
970 " Do this twice, writing the file.
971 e! Xtestje1
972 call setline(1, 'testje1')
973 w
974 sp Xtestje2
975 call setline(1, 'testje2')
976 w
977 sp Xtestje3
978 call setline(1, 'testje3')
979 w
980 wincmd w
981 au WinLeave Xtestje2 bwipe
982 wincmd w
983 call assert_equal('Xtestje1', expand('%'))
984
985 au WinLeave Xtestje1 bwipe Xtestje3
986 close
987 call assert_equal('Xtestje1', expand('%'))
988
989 " Test deleting the buffer on a Unload event. If this goes wrong there
990 " will be the ATTENTION prompt.
991 e Xtestje1
992 au!
993 au! BufUnload Xtestje1 bwipe
994 call assert_fails('e Xtestje3', 'E937:')
995 call assert_equal('Xtestje3', expand('%'))
996
997 e Xtestje2
998 sp Xtestje1
999 call assert_fails('e', 'E937:')
Bram Moolenaara997b452018-04-17 23:24:06 +02001000 call assert_equal('Xtestje1', expand('%'))
Bram Moolenaare0ab94e2016-09-04 19:50:54 +02001001
1002 " Test changing buffers in a BufWipeout autocommand. If this goes wrong
1003 " there are ml_line errors and/or a Crash.
1004 au!
1005 only
1006 e Xanother
1007 e Xtestje1
1008 bwipe Xtestje2
1009 bwipe Xtestje3
1010 au BufWipeout Xtestje1 buf Xtestje1
1011 bwipe
1012 call assert_equal('Xanother', expand('%'))
1013
1014 only
1015 help
1016 wincmd w
1017 1quit
1018 call assert_equal('Xanother', expand('%'))
1019
1020 au!
Bram Moolenaar4520d442017-03-19 16:09:46 +01001021 enew
Bram Moolenaare0ab94e2016-09-04 19:50:54 +02001022 call delete('Xtestje1')
1023 call delete('Xtestje2')
1024 call delete('Xtestje3')
1025endfunc
Bram Moolenaare13b9af2017-01-13 22:01:02 +01001026
1027func Test_BufEnter()
1028 au! BufEnter
1029 au Bufenter * let val = val . '+'
1030 let g:val = ''
1031 split NewFile
1032 call assert_equal('+', g:val)
1033 bwipe!
1034 call assert_equal('++', g:val)
1035
1036 " Also get BufEnter when editing a directory
Bram Moolenaar6f14da12022-09-07 21:30:44 +01001037 call mkdir('Xbufenterdir', 'D')
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +01001038 split Xbufenterdir
Bram Moolenaare13b9af2017-01-13 22:01:02 +01001039 call assert_equal('+++', g:val)
Bram Moolenaare94260f2017-03-21 15:50:12 +01001040
1041 " On MS-Windows we can't edit the directory, make sure we wipe the right
1042 " buffer.
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +01001043 bwipe! Xbufenterdir
Bram Moolenaare13b9af2017-01-13 22:01:02 +01001044 au! BufEnter
Bram Moolenaara9b5b852022-08-26 13:16:20 +01001045
1046 " Editing a "nofile" buffer doesn't read the file but does trigger BufEnter
zeertzjq93f72cc2022-08-26 15:34:52 +01001047 " for historic reasons. Also test other 'buftype' values.
1048 for val in ['nofile',
1049 \ 'nowrite',
1050 \ 'acwrite',
1051 \ 'quickfix',
1052 \ 'help',
1053 \ 'terminal',
1054 \ 'prompt',
1055 \ 'popup',
1056 \ ]
1057 new somefile
1058 exe 'set buftype=' .. val
1059 au BufEnter somefile call setline(1, 'some text')
1060 edit
1061 call assert_equal('some text', getline(1))
1062 bwipe!
1063 au! BufEnter
1064 endfor
Bram Moolenaar9fda8152022-11-19 13:14:10 +00001065
1066 new
1067 new
1068 autocmd BufEnter * ++once close
1069 call assert_fails('close', 'E1312:')
1070
1071 au! BufEnter
1072 only
Bram Moolenaare13b9af2017-01-13 22:01:02 +01001073endfunc
Bram Moolenaar8c752bd2017-03-19 17:09:56 +01001074
1075" Closing a window might cause an endless loop
1076" E814 for older Vims
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001077func Test_autocmd_bufwipe_in_SessLoadPost()
Bram Moolenaar1d68d9b2017-10-13 22:33:32 +02001078 edit Xtest
Bram Moolenaar8c752bd2017-03-19 17:09:56 +01001079 tabnew
Bram Moolenaar1d68d9b2017-10-13 22:33:32 +02001080 file Xsomething
Bram Moolenaar8c752bd2017-03-19 17:09:56 +01001081 set noswapfile
Bram Moolenaar8c752bd2017-03-19 17:09:56 +01001082 mksession!
1083
Bram Moolenaarc79745a2019-05-20 22:12:34 +02001084 let content =<< trim [CODE]
Bram Moolenaar62cd26a2020-10-11 20:08:44 +02001085 call test_override('ui_delay', 10)
Bram Moolenaarc79745a2019-05-20 22:12:34 +02001086 set nocp noswapfile
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001087 let v:swapchoice = "e"
Bram Moolenaarc79745a2019-05-20 22:12:34 +02001088 augroup test_autocmd_sessionload
1089 autocmd!
1090 autocmd SessionLoadPost * exe bufnr("Xsomething") . "bw!"
1091 augroup END
1092
1093 func WriteErrors()
Bram Moolenaarbe9fc5b2022-09-09 17:09:35 +01001094 call writefile([execute("messages")], "XerrorsBwipe")
Bram Moolenaarc79745a2019-05-20 22:12:34 +02001095 endfunc
1096 au VimLeave * call WriteErrors()
1097 [CODE]
1098
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01001099 call writefile(content, 'Xvimrc', 'D')
Bram Moolenaar93344c22019-08-14 21:12:05 +02001100 call system(GetVimCommand('Xvimrc') .. ' --not-a-term --noplugins -S Session.vim -c cq')
Bram Moolenaarae04a602022-09-09 15:08:10 +01001101 sleep 100m
Bram Moolenaarbe9fc5b2022-09-09 17:09:35 +01001102 let errors = join(readfile('XerrorsBwipe'))
Bram Moolenaare2e40752020-09-04 21:18:46 +02001103 call assert_match('E814:', errors)
Bram Moolenaar8c752bd2017-03-19 17:09:56 +01001104
Bram Moolenaar8c752bd2017-03-19 17:09:56 +01001105 set swapfile
Bram Moolenaarbe9fc5b2022-09-09 17:09:35 +01001106 for file in ['Session.vim', 'XerrorsBwipe']
Bram Moolenaar8c752bd2017-03-19 17:09:56 +01001107 call delete(file)
1108 endfor
1109endfunc
1110
Bram Moolenaar797e63b2021-01-15 16:22:52 +01001111" Using :blast and :ball for many events caused a crash, because b_nwindows was
1112" not incremented correctly.
1113func Test_autocmd_blast_badd()
1114 let content =<< trim [CODE]
1115 au BufNew,BufAdd,BufWinEnter,BufEnter,BufLeave,BufWinLeave,BufUnload,VimEnter foo* blast
1116 edit foo1
1117 au BufNew,BufAdd,BufWinEnter,BufEnter,BufLeave,BufWinLeave,BufUnload,VimEnter foo* ball
1118 edit foo2
Bram Moolenaarbe9fc5b2022-09-09 17:09:35 +01001119 call writefile(['OK'], 'XerrorsBlast')
Bram Moolenaar797e63b2021-01-15 16:22:52 +01001120 qall
1121 [CODE]
1122
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01001123 call writefile(content, 'XblastBall', 'D')
Bram Moolenaar797e63b2021-01-15 16:22:52 +01001124 call system(GetVimCommand() .. ' --clean -S XblastBall')
Bram Moolenaarae04a602022-09-09 15:08:10 +01001125 sleep 100m
Bram Moolenaarbe9fc5b2022-09-09 17:09:35 +01001126 call assert_match('OK', readfile('XerrorsBlast')->join())
Bram Moolenaar797e63b2021-01-15 16:22:52 +01001127
Bram Moolenaarbe9fc5b2022-09-09 17:09:35 +01001128 call delete('XerrorsBlast')
Bram Moolenaar797e63b2021-01-15 16:22:52 +01001129endfunc
1130
Bram Moolenaar8c752bd2017-03-19 17:09:56 +01001131" SEGV occurs in older versions.
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001132func Test_autocmd_bufwipe_in_SessLoadPost2()
Bram Moolenaar8c752bd2017-03-19 17:09:56 +01001133 tabnew
1134 set noswapfile
Bram Moolenaar8c752bd2017-03-19 17:09:56 +01001135 mksession!
1136
Bram Moolenaarc79745a2019-05-20 22:12:34 +02001137 let content =<< trim [CODE]
1138 set nocp noswapfile
1139 function! DeleteInactiveBufs()
1140 tabfirst
1141 let tabblist = []
1142 for i in range(1, tabpagenr(''$''))
1143 call extend(tabblist, tabpagebuflist(i))
1144 endfor
1145 for b in range(1, bufnr(''$''))
1146 if bufexists(b) && buflisted(b) && (index(tabblist, b) == -1 || bufname(b) =~# ''^$'')
1147 exec ''bwipeout '' . b
1148 endif
1149 endfor
1150 echomsg "SessionLoadPost DONE"
1151 endfunction
1152 au SessionLoadPost * call DeleteInactiveBufs()
1153
1154 func WriteErrors()
Bram Moolenaarbe9fc5b2022-09-09 17:09:35 +01001155 call writefile([execute("messages")], "XerrorsPost")
Bram Moolenaarc79745a2019-05-20 22:12:34 +02001156 endfunc
1157 au VimLeave * call WriteErrors()
1158 [CODE]
1159
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01001160 call writefile(content, 'Xvimrc', 'D')
Bram Moolenaar93344c22019-08-14 21:12:05 +02001161 call system(GetVimCommand('Xvimrc') .. ' --not-a-term --noplugins -S Session.vim -c cq')
Bram Moolenaarae04a602022-09-09 15:08:10 +01001162 sleep 100m
Bram Moolenaarbe9fc5b2022-09-09 17:09:35 +01001163 let errors = join(readfile('XerrorsPost'))
Bram Moolenaare94260f2017-03-21 15:50:12 +01001164 " This probably only ever matches on unix.
1165 call assert_notmatch('Caught deadly signal SEGV', errors)
1166 call assert_match('SessionLoadPost DONE', errors)
Bram Moolenaar8c752bd2017-03-19 17:09:56 +01001167
Bram Moolenaar8c752bd2017-03-19 17:09:56 +01001168 set swapfile
Bram Moolenaarbe9fc5b2022-09-09 17:09:35 +01001169 for file in ['Session.vim', 'XerrorsPost']
Bram Moolenaar8c752bd2017-03-19 17:09:56 +01001170 call delete(file)
1171 endfor
1172endfunc
Bram Moolenaarfaf29d72017-07-09 11:07:16 +02001173
1174func Test_empty_doau()
1175 doau \|
1176endfunc
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001177
1178func s:AutoCommandOptionSet(match)
Bram Moolenaard7c96872019-06-15 17:12:48 +02001179 let template = "Option: <%s>, OldVal: <%s>, OldValLocal: <%s>, OldValGlobal: <%s>, NewVal: <%s>, Scope: <%s>, Command: <%s>\n"
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001180 let item = remove(g:options, 0)
Bram Moolenaard7c96872019-06-15 17:12:48 +02001181 let expected = printf(template, item[0], item[1], item[2], item[3], item[4], item[5], item[6])
1182 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 +02001183 let g:opt = [expected, actual]
1184 "call assert_equal(expected, actual)
1185endfunc
1186
1187func Test_OptionSet()
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +02001188 CheckOption autochdir
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001189
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +02001190 badd test_autocmd.vim
1191
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001192 call test_override('starting', 1)
1193 set nocp
1194 au OptionSet * :call s:AutoCommandOptionSet(expand("<amatch>"))
1195
1196 " 1: Setting number option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001197 let g:options = [['number', 0, 0, 0, 1, 'global', 'set']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001198 set nu
1199 call assert_equal([], g:options)
1200 call assert_equal(g:opt[0], g:opt[1])
1201
1202 " 2: Setting local number option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001203 let g:options = [['number', 1, 1, '', 0, 'local', 'setlocal']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001204 setlocal nonu
1205 call assert_equal([], g:options)
1206 call assert_equal(g:opt[0], g:opt[1])
1207
1208 " 3: Setting global number option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001209 let g:options = [['number', 1, '', 1, 0, 'global', 'setglobal']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001210 setglobal nonu
1211 call assert_equal([], g:options)
1212 call assert_equal(g:opt[0], g:opt[1])
1213
1214 " 4: Setting local autoindent option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001215 let g:options = [['autoindent', 0, 0, '', 1, 'local', 'setlocal']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001216 setlocal ai
1217 call assert_equal([], g:options)
1218 call assert_equal(g:opt[0], g:opt[1])
1219
1220 " 5: Setting global autoindent option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001221 let g:options = [['autoindent', 0, '', 0, 1, 'global', 'setglobal']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001222 setglobal ai
1223 call assert_equal([], g:options)
1224 call assert_equal(g:opt[0], g:opt[1])
1225
1226 " 6: Setting global autoindent option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001227 let g:options = [['autoindent', 1, 1, 1, 0, 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001228 set ai!
1229 call assert_equal([], g:options)
1230 call assert_equal(g:opt[0], g:opt[1])
1231
1232 " 6a: Setting global autoindent option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001233 let g:options = [['autoindent', 1, 1, 0, 0, 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001234 noa setlocal ai
1235 noa setglobal noai
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001236 set ai!
1237 call assert_equal([], g:options)
1238 call assert_equal(g:opt[0], g:opt[1])
1239
1240 " Should not print anything, use :noa
1241 " 7: don't trigger OptionSet"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001242 let g:options = [['invalid', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001243 noa set nonu
Bram Moolenaard7c96872019-06-15 17:12:48 +02001244 call assert_equal([['invalid', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid']], g:options)
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001245 call assert_equal(g:opt[0], g:opt[1])
1246
1247 " 8: Setting several global list and number option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001248 let g:options = [['list', 0, 0, 0, 1, 'global', 'set'], ['number', 0, 0, 0, 1, 'global', 'set']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001249 set list nu
1250 call assert_equal([], g:options)
1251 call assert_equal(g:opt[0], g:opt[1])
1252
1253 " 9: don't trigger OptionSet"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001254 let g:options = [['invalid', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid'], ['invalid', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001255 noa set nolist nonu
Bram Moolenaard7c96872019-06-15 17:12:48 +02001256 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 +02001257 call assert_equal(g:opt[0], g:opt[1])
1258
1259 " 10: Setting global acd"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001260 let g:options = [['autochdir', 0, 0, '', 1, 'local', 'setlocal']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001261 setlocal acd
1262 call assert_equal([], g:options)
1263 call assert_equal(g:opt[0], g:opt[1])
1264
1265 " 11: Setting global autoread (also sets local value)"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001266 let g:options = [['autoread', 0, 0, 0, 1, 'global', 'set']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001267 set ar
1268 call assert_equal([], g:options)
1269 call assert_equal(g:opt[0], g:opt[1])
1270
1271 " 12: Setting local autoread"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001272 let g:options = [['autoread', 1, 1, '', 1, 'local', 'setlocal']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001273 setlocal ar
1274 call assert_equal([], g:options)
1275 call assert_equal(g:opt[0], g:opt[1])
1276
1277 " 13: Setting global autoread"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001278 let g:options = [['autoread', 1, '', 1, 0, 'global', 'setglobal']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001279 setglobal invar
1280 call assert_equal([], g:options)
1281 call assert_equal(g:opt[0], g:opt[1])
1282
1283 " 14: Setting option backspace through :let"
Luca Saccarola959ef612024-12-01 16:25:53 +01001284 let g:options = [['backspace', 'indent,eol,start', 'indent,eol,start', 'indent,eol,start', '', 'global', 'set']]
1285 let &bs = ''
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001286 call assert_equal([], g:options)
1287 call assert_equal(g:opt[0], g:opt[1])
1288
1289 " 15: Setting option backspace through setbufvar()"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001290 let g:options = [['backup', 0, 0, '', 1, 'local', 'setlocal']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001291 " try twice, first time, shouldn't trigger because option name is invalid,
1292 " second time, it should trigger
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +02001293 let bnum = bufnr('%')
Bram Moolenaare2e40752020-09-04 21:18:46 +02001294 call assert_fails("call setbufvar(bnum, '&l:bk', 1)", 'E355:')
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001295 " should trigger, use correct option name
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +02001296 call setbufvar(bnum, '&backup', 1)
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001297 call assert_equal([], g:options)
1298 call assert_equal(g:opt[0], g:opt[1])
1299
1300 " 16: Setting number option using setwinvar"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001301 let g:options = [['number', 0, 0, '', 1, 'local', 'setlocal']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001302 call setwinvar(0, '&number', 1)
1303 call assert_equal([], g:options)
1304 call assert_equal(g:opt[0], g:opt[1])
1305
1306 " 17: Setting key option, shouldn't trigger"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001307 let g:options = [['key', 'invalid', 'invalid1', 'invalid2', 'invalid3', 'invalid4', 'invalid5']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001308 setlocal key=blah
1309 setlocal key=
Bram Moolenaard7c96872019-06-15 17:12:48 +02001310 call assert_equal([['key', 'invalid', 'invalid1', 'invalid2', 'invalid3', 'invalid4', 'invalid5']], g:options)
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001311 call assert_equal(g:opt[0], g:opt[1])
1312
Bram Moolenaard7c96872019-06-15 17:12:48 +02001313
1314 " 18a: Setting string global option"
1315 let oldval = &backupext
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001316 let g:options = [['backupext', oldval, oldval, oldval, 'foo', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001317 set backupext=foo
1318 call assert_equal([], g:options)
1319 call assert_equal(g:opt[0], g:opt[1])
1320
1321 " 18b: Resetting string global option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001322 let g:options = [['backupext', 'foo', 'foo', 'foo', oldval, 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001323 set backupext&
1324 call assert_equal([], g:options)
1325 call assert_equal(g:opt[0], g:opt[1])
1326
1327 " 18c: Setting global string global option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001328 let g:options = [['backupext', oldval, '', oldval, 'bar', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001329 setglobal backupext=bar
1330 call assert_equal([], g:options)
1331 call assert_equal(g:opt[0], g:opt[1])
1332
1333 " 18d: Setting local string global option"
1334 " As this is a global option this sets the global value even though
1335 " :setlocal is used!
1336 noa set backupext& " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001337 let g:options = [['backupext', oldval, oldval, '', 'baz', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001338 setlocal backupext=baz
1339 call assert_equal([], g:options)
1340 call assert_equal(g:opt[0], g:opt[1])
1341
1342 " 18e: Setting again string global option"
1343 noa setglobal backupext=ext_global " Reset global and local value (without triggering autocmd)
1344 noa setlocal backupext=ext_local " Sets the global(!) value!
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001345 let g:options = [['backupext', 'ext_local', 'ext_local', 'ext_local', 'fuu', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001346 set backupext=fuu
1347 call assert_equal([], g:options)
1348 call assert_equal(g:opt[0], g:opt[1])
1349
1350
zeertzjqb811de52021-10-21 10:50:44 +01001351 " 19a: Setting string global-local (to buffer) option"
Bram Moolenaar8efa0262017-08-20 15:47:20 +02001352 let oldval = &tags
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001353 let g:options = [['tags', oldval, oldval, oldval, 'tagpath', 'global', 'set']]
Bram Moolenaar8efa0262017-08-20 15:47:20 +02001354 set tags=tagpath
1355 call assert_equal([], g:options)
1356 call assert_equal(g:opt[0], g:opt[1])
1357
zeertzjqb811de52021-10-21 10:50:44 +01001358 " 19b: Resetting string global-local (to buffer) option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001359 let g:options = [['tags', 'tagpath', 'tagpath', 'tagpath', oldval, 'global', 'set']]
Bram Moolenaar8efa0262017-08-20 15:47:20 +02001360 set tags&
1361 call assert_equal([], g:options)
1362 call assert_equal(g:opt[0], g:opt[1])
1363
zeertzjqb811de52021-10-21 10:50:44 +01001364 " 19c: Setting global string global-local (to buffer) option "
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001365 let g:options = [['tags', oldval, '', oldval, 'tagpath1', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001366 setglobal tags=tagpath1
1367 call assert_equal([], g:options)
1368 call assert_equal(g:opt[0], g:opt[1])
1369
zeertzjqb811de52021-10-21 10:50:44 +01001370 " 19d: Setting local string global-local (to buffer) option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001371 let g:options = [['tags', 'tagpath1', 'tagpath1', '', 'tagpath2', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001372 setlocal tags=tagpath2
1373 call assert_equal([], g:options)
1374 call assert_equal(g:opt[0], g:opt[1])
1375
zeertzjqb811de52021-10-21 10:50:44 +01001376 " 19e: Setting again string global-local (to buffer) option"
1377 " Note: v:option_old is the old global value for global-local string options
Bram Moolenaard7c96872019-06-15 17:12:48 +02001378 " but the old local value for all other kinds of options.
1379 noa setglobal tags=tag_global " Reset global and local value (without triggering autocmd)
1380 noa setlocal tags=tag_local
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001381 let g:options = [['tags', 'tag_global', 'tag_local', 'tag_global', 'tagpath', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001382 set tags=tagpath
1383 call assert_equal([], g:options)
1384 call assert_equal(g:opt[0], g:opt[1])
1385
zeertzjqb811de52021-10-21 10:50:44 +01001386 " 19f: Setting string global-local (to buffer) option to an empty string"
1387 " Note: v:option_old is the old global value for global-local string options
Bram Moolenaard7c96872019-06-15 17:12:48 +02001388 " but the old local value for all other kinds of options.
1389 noa set tags=tag_global " Reset global and local value (without triggering autocmd)
1390 noa setlocal tags= " empty string
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001391 let g:options = [['tags', 'tag_global', '', 'tag_global', 'tagpath', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001392 set tags=tagpath
1393 call assert_equal([], g:options)
1394 call assert_equal(g:opt[0], g:opt[1])
1395
1396
1397 " 20a: Setting string local (to buffer) option"
1398 let oldval = &spelllang
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001399 let g:options = [['spelllang', oldval, oldval, oldval, 'elvish,klingon', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001400 set spelllang=elvish,klingon
1401 call assert_equal([], g:options)
1402 call assert_equal(g:opt[0], g:opt[1])
1403
1404 " 20b: Resetting string local (to buffer) option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001405 let g:options = [['spelllang', 'elvish,klingon', 'elvish,klingon', 'elvish,klingon', oldval, 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001406 set spelllang&
1407 call assert_equal([], g:options)
1408 call assert_equal(g:opt[0], g:opt[1])
1409
1410 " 20c: Setting global string local (to buffer) option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001411 let g:options = [['spelllang', oldval, '', oldval, 'elvish', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001412 setglobal spelllang=elvish
1413 call assert_equal([], g:options)
1414 call assert_equal(g:opt[0], g:opt[1])
1415
1416 " 20d: Setting local string local (to buffer) option"
1417 noa set spelllang& " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001418 let g:options = [['spelllang', oldval, oldval, '', 'klingon', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001419 setlocal spelllang=klingon
1420 call assert_equal([], g:options)
1421 call assert_equal(g:opt[0], g:opt[1])
1422
1423 " 20e: Setting again string local (to buffer) option"
zeertzjqb811de52021-10-21 10:50:44 +01001424 " Note: v:option_old is the old global value for global-local string options
Bram Moolenaard7c96872019-06-15 17:12:48 +02001425 " but the old local value for all other kinds of options.
1426 noa setglobal spelllang=spellglobal " Reset global and local value (without triggering autocmd)
1427 noa setlocal spelllang=spelllocal
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001428 let g:options = [['spelllang', 'spelllocal', 'spelllocal', 'spellglobal', 'foo', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001429 set spelllang=foo
1430 call assert_equal([], g:options)
1431 call assert_equal(g:opt[0], g:opt[1])
1432
1433
zeertzjqb811de52021-10-21 10:50:44 +01001434 " 21a: Setting string global-local (to window) option"
Bram Moolenaard7c96872019-06-15 17:12:48 +02001435 let oldval = &statusline
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001436 let g:options = [['statusline', oldval, oldval, oldval, 'foo', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001437 set statusline=foo
1438 call assert_equal([], g:options)
1439 call assert_equal(g:opt[0], g:opt[1])
1440
zeertzjqb811de52021-10-21 10:50:44 +01001441 " 21b: Resetting string global-local (to window) option"
1442 " Note: v:option_old is the old global value for global-local string options
Bram Moolenaard7c96872019-06-15 17:12:48 +02001443 " but the old local value for all other kinds of options.
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001444 let g:options = [['statusline', 'foo', 'foo', 'foo', oldval, 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001445 set statusline&
1446 call assert_equal([], g:options)
1447 call assert_equal(g:opt[0], g:opt[1])
1448
zeertzjqb811de52021-10-21 10:50:44 +01001449 " 21c: Setting global string global-local (to window) option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001450 let g:options = [['statusline', oldval, '', oldval, 'bar', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001451 setglobal statusline=bar
1452 call assert_equal([], g:options)
1453 call assert_equal(g:opt[0], g:opt[1])
1454
zeertzjqb811de52021-10-21 10:50:44 +01001455 " 21d: Setting local string global-local (to window) option"
Bram Moolenaard7c96872019-06-15 17:12:48 +02001456 noa set statusline& " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001457 let g:options = [['statusline', oldval, oldval, '', 'baz', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001458 setlocal statusline=baz
1459 call assert_equal([], g:options)
1460 call assert_equal(g:opt[0], g:opt[1])
1461
zeertzjqb811de52021-10-21 10:50:44 +01001462 " 21e: Setting again string global-local (to window) option"
1463 " Note: v:option_old is the old global value for global-local string options
Bram Moolenaard7c96872019-06-15 17:12:48 +02001464 " but the old local value for all other kinds of options.
1465 noa setglobal statusline=bar " Reset global and local value (without triggering autocmd)
1466 noa setlocal statusline=baz
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001467 let g:options = [['statusline', 'bar', 'baz', 'bar', 'foo', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001468 set statusline=foo
1469 call assert_equal([], g:options)
1470 call assert_equal(g:opt[0], g:opt[1])
1471
1472
1473 " 22a: Setting string local (to window) option"
1474 let oldval = &foldignore
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001475 let g:options = [['foldignore', oldval, oldval, oldval, 'fo', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001476 set foldignore=fo
1477 call assert_equal([], g:options)
1478 call assert_equal(g:opt[0], g:opt[1])
1479
1480 " 22b: Resetting string local (to window) option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001481 let g:options = [['foldignore', 'fo', 'fo', 'fo', oldval, 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001482 set foldignore&
1483 call assert_equal([], g:options)
1484 call assert_equal(g:opt[0], g:opt[1])
1485
1486 " 22c: Setting global string local (to window) option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001487 let g:options = [['foldignore', oldval, '', oldval, 'bar', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001488 setglobal foldignore=bar
1489 call assert_equal([], g:options)
1490 call assert_equal(g:opt[0], g:opt[1])
1491
1492 " 22d: Setting local string local (to window) option"
1493 noa set foldignore& " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001494 let g:options = [['foldignore', oldval, oldval, '', 'baz', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001495 setlocal foldignore=baz
1496 call assert_equal([], g:options)
1497 call assert_equal(g:opt[0], g:opt[1])
1498
1499 " 22e: Setting again string local (to window) option"
1500 noa setglobal foldignore=glob " Reset global and local value (without triggering autocmd)
1501 noa setlocal foldignore=loc
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001502 let g:options = [['foldignore', 'loc', 'loc', 'glob', 'fo', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001503 set foldignore=fo
1504 call assert_equal([], g:options)
1505 call assert_equal(g:opt[0], g:opt[1])
1506
1507
zeertzjqb811de52021-10-21 10:50:44 +01001508 " 23a: Setting global number global option"
Bram Moolenaard7c96872019-06-15 17:12:48 +02001509 noa setglobal cmdheight=8 " Reset global and local value (without triggering autocmd)
1510 noa setlocal cmdheight=1 " Sets the global(!) value!
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001511 let g:options = [['cmdheight', '1', '', '1', '2', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001512 setglobal cmdheight=2
1513 call assert_equal([], g:options)
1514 call assert_equal(g:opt[0], g:opt[1])
1515
1516 " 23b: Setting local number global option"
1517 noa setglobal cmdheight=8 " Reset global and local value (without triggering autocmd)
1518 noa setlocal cmdheight=1 " Sets the global(!) value!
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001519 let g:options = [['cmdheight', '1', '1', '', '2', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001520 setlocal cmdheight=2
1521 call assert_equal([], g:options)
1522 call assert_equal(g:opt[0], g:opt[1])
1523
1524 " 23c: Setting again number global option"
1525 noa setglobal cmdheight=8 " Reset global and local value (without triggering autocmd)
1526 noa setlocal cmdheight=1 " Sets the global(!) value!
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001527 let g:options = [['cmdheight', '1', '1', '1', '2', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001528 set cmdheight=2
1529 call assert_equal([], g:options)
1530 call assert_equal(g:opt[0], g:opt[1])
1531
1532 " 23d: Setting again number global option"
1533 noa set cmdheight=8 " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001534 let g:options = [['cmdheight', '8', '8', '8', '2', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001535 set cmdheight=2
1536 call assert_equal([], g:options)
1537 call assert_equal(g:opt[0], g:opt[1])
1538
1539
1540 " 24a: Setting global number global-local (to buffer) option"
1541 noa setglobal undolevels=8 " Reset global and local value (without triggering autocmd)
1542 noa setlocal undolevels=1
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001543 let g:options = [['undolevels', '8', '', '8', '2', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001544 setglobal undolevels=2
1545 call assert_equal([], g:options)
1546 call assert_equal(g:opt[0], g:opt[1])
1547
1548 " 24b: Setting local number global-local (to buffer) option"
1549 noa setglobal undolevels=8 " Reset global and local value (without triggering autocmd)
1550 noa setlocal undolevels=1
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001551 let g:options = [['undolevels', '1', '1', '', '2', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001552 setlocal undolevels=2
1553 call assert_equal([], g:options)
1554 call assert_equal(g:opt[0], g:opt[1])
1555
1556 " 24c: Setting again number global-local (to buffer) option"
1557 noa setglobal undolevels=8 " Reset global and local value (without triggering autocmd)
1558 noa setlocal undolevels=1
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001559 let g:options = [['undolevels', '1', '1', '8', '2', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001560 set undolevels=2
1561 call assert_equal([], g:options)
1562 call assert_equal(g:opt[0], g:opt[1])
1563
1564 " 24d: Setting again global number global-local (to buffer) option"
1565 noa set undolevels=8 " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001566 let g:options = [['undolevels', '8', '8', '8', '2', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001567 set undolevels=2
1568 call assert_equal([], g:options)
1569 call assert_equal(g:opt[0], g:opt[1])
1570
1571
1572 " 25a: Setting global number local (to buffer) option"
1573 noa setglobal wrapmargin=8 " Reset global and local value (without triggering autocmd)
1574 noa setlocal wrapmargin=1
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001575 let g:options = [['wrapmargin', '8', '', '8', '2', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001576 setglobal wrapmargin=2
1577 call assert_equal([], g:options)
1578 call assert_equal(g:opt[0], g:opt[1])
1579
1580 " 25b: Setting local number local (to buffer) option"
1581 noa setglobal wrapmargin=8 " Reset global and local value (without triggering autocmd)
1582 noa setlocal wrapmargin=1
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001583 let g:options = [['wrapmargin', '1', '1', '', '2', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001584 setlocal wrapmargin=2
1585 call assert_equal([], g:options)
1586 call assert_equal(g:opt[0], g:opt[1])
1587
1588 " 25c: Setting again number local (to buffer) option"
1589 noa setglobal wrapmargin=8 " Reset global and local value (without triggering autocmd)
1590 noa setlocal wrapmargin=1
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001591 let g:options = [['wrapmargin', '1', '1', '8', '2', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001592 set wrapmargin=2
1593 call assert_equal([], g:options)
1594 call assert_equal(g:opt[0], g:opt[1])
1595
1596 " 25d: Setting again global number local (to buffer) option"
1597 noa set wrapmargin=8 " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001598 let g:options = [['wrapmargin', '8', '8', '8', '2', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001599 set wrapmargin=2
1600 call assert_equal([], g:options)
1601 call assert_equal(g:opt[0], g:opt[1])
1602
1603
1604 " 26: Setting number global-local (to window) option.
1605 " Such option does currently not exist.
1606
1607
1608 " 27a: Setting global number local (to window) option"
1609 noa setglobal foldcolumn=8 " Reset global and local value (without triggering autocmd)
1610 noa setlocal foldcolumn=1
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001611 let g:options = [['foldcolumn', '8', '', '8', '2', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001612 setglobal foldcolumn=2
1613 call assert_equal([], g:options)
1614 call assert_equal(g:opt[0], g:opt[1])
1615
1616 " 27b: Setting local number local (to window) option"
1617 noa setglobal foldcolumn=8 " Reset global and local value (without triggering autocmd)
1618 noa setlocal foldcolumn=1
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001619 let g:options = [['foldcolumn', '1', '1', '', '2', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001620 setlocal foldcolumn=2
1621 call assert_equal([], g:options)
1622 call assert_equal(g:opt[0], g:opt[1])
1623
1624 " 27c: Setting again number local (to window) option"
1625 noa setglobal foldcolumn=8 " Reset global and local value (without triggering autocmd)
1626 noa setlocal foldcolumn=1
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001627 let g:options = [['foldcolumn', '1', '1', '8', '2', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001628 set foldcolumn=2
1629 call assert_equal([], g:options)
1630 call assert_equal(g:opt[0], g:opt[1])
1631
zeertzjqb811de52021-10-21 10:50:44 +01001632 " 27d: Setting again global number local (to window) option"
Bram Moolenaard7c96872019-06-15 17:12:48 +02001633 noa set foldcolumn=8 " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001634 let g:options = [['foldcolumn', '8', '8', '8', '2', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001635 set foldcolumn=2
1636 call assert_equal([], g:options)
1637 call assert_equal(g:opt[0], g:opt[1])
1638
1639
1640 " 28a: Setting global boolean global option"
1641 noa setglobal nowrapscan " Reset global and local value (without triggering autocmd)
1642 noa setlocal wrapscan " Sets the global(!) value!
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001643 let g:options = [['wrapscan', '1', '', '1', '0', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001644 setglobal nowrapscan
1645 call assert_equal([], g:options)
1646 call assert_equal(g:opt[0], g:opt[1])
1647
1648 " 28b: Setting local boolean global option"
1649 noa setglobal nowrapscan " Reset global and local value (without triggering autocmd)
1650 noa setlocal wrapscan " Sets the global(!) value!
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001651 let g:options = [['wrapscan', '1', '1', '', '0', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001652 setlocal nowrapscan
1653 call assert_equal([], g:options)
1654 call assert_equal(g:opt[0], g:opt[1])
1655
1656 " 28c: Setting again boolean global option"
1657 noa setglobal nowrapscan " Reset global and local value (without triggering autocmd)
1658 noa setlocal wrapscan " Sets the global(!) value!
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001659 let g:options = [['wrapscan', '1', '1', '1', '0', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001660 set nowrapscan
1661 call assert_equal([], g:options)
1662 call assert_equal(g:opt[0], g:opt[1])
1663
1664 " 28d: Setting again global boolean global option"
1665 noa set nowrapscan " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001666 let g:options = [['wrapscan', '0', '0', '0', '1', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001667 set wrapscan
1668 call assert_equal([], g:options)
1669 call assert_equal(g:opt[0], g:opt[1])
1670
1671
1672 " 29a: Setting global boolean global-local (to buffer) option"
1673 noa setglobal noautoread " Reset global and local value (without triggering autocmd)
1674 noa setlocal autoread
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001675 let g:options = [['autoread', '0', '', '0', '1', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001676 setglobal autoread
1677 call assert_equal([], g:options)
1678 call assert_equal(g:opt[0], g:opt[1])
1679
1680 " 29b: Setting local boolean global-local (to buffer) option"
1681 noa setglobal noautoread " Reset global and local value (without triggering autocmd)
1682 noa setlocal autoread
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001683 let g:options = [['autoread', '1', '1', '', '0', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001684 setlocal noautoread
1685 call assert_equal([], g:options)
1686 call assert_equal(g:opt[0], g:opt[1])
1687
1688 " 29c: Setting again boolean global-local (to buffer) option"
1689 noa setglobal noautoread " Reset global and local value (without triggering autocmd)
1690 noa setlocal autoread
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001691 let g:options = [['autoread', '1', '1', '0', '1', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001692 set autoread
1693 call assert_equal([], g:options)
1694 call assert_equal(g:opt[0], g:opt[1])
1695
1696 " 29d: Setting again global boolean global-local (to buffer) option"
1697 noa set noautoread " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001698 let g:options = [['autoread', '0', '0', '0', '1', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001699 set autoread
1700 call assert_equal([], g:options)
1701 call assert_equal(g:opt[0], g:opt[1])
1702
1703
1704 " 30a: Setting global boolean local (to buffer) option"
1705 noa setglobal nocindent " Reset global and local value (without triggering autocmd)
1706 noa setlocal cindent
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001707 let g:options = [['cindent', '0', '', '0', '1', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001708 setglobal cindent
1709 call assert_equal([], g:options)
1710 call assert_equal(g:opt[0], g:opt[1])
1711
1712 " 30b: Setting local boolean local (to buffer) option"
1713 noa setglobal nocindent " Reset global and local value (without triggering autocmd)
1714 noa setlocal cindent
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001715 let g:options = [['cindent', '1', '1', '', '0', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001716 setlocal nocindent
1717 call assert_equal([], g:options)
1718 call assert_equal(g:opt[0], g:opt[1])
1719
1720 " 30c: Setting again boolean local (to buffer) option"
1721 noa setglobal nocindent " Reset global and local value (without triggering autocmd)
1722 noa setlocal cindent
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001723 let g:options = [['cindent', '1', '1', '0', '1', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001724 set cindent
1725 call assert_equal([], g:options)
1726 call assert_equal(g:opt[0], g:opt[1])
1727
1728 " 30d: Setting again global boolean local (to buffer) option"
1729 noa set nocindent " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001730 let g:options = [['cindent', '0', '0', '0', '1', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001731 set cindent
1732 call assert_equal([], g:options)
1733 call assert_equal(g:opt[0], g:opt[1])
1734
1735
1736 " 31: Setting boolean global-local (to window) option
1737 " Currently no such option exists.
1738
1739
1740 " 32a: Setting global boolean local (to window) option"
1741 noa setglobal nocursorcolumn " Reset global and local value (without triggering autocmd)
1742 noa setlocal cursorcolumn
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001743 let g:options = [['cursorcolumn', '0', '', '0', '1', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001744 setglobal cursorcolumn
1745 call assert_equal([], g:options)
1746 call assert_equal(g:opt[0], g:opt[1])
1747
1748 " 32b: Setting local boolean local (to window) option"
1749 noa setglobal nocursorcolumn " Reset global and local value (without triggering autocmd)
1750 noa setlocal cursorcolumn
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001751 let g:options = [['cursorcolumn', '1', '1', '', '0', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001752 setlocal nocursorcolumn
1753 call assert_equal([], g:options)
1754 call assert_equal(g:opt[0], g:opt[1])
1755
1756 " 32c: Setting again boolean local (to window) option"
1757 noa setglobal nocursorcolumn " Reset global and local value (without triggering autocmd)
1758 noa setlocal cursorcolumn
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001759 let g:options = [['cursorcolumn', '1', '1', '0', '1', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001760 set cursorcolumn
1761 call assert_equal([], g:options)
1762 call assert_equal(g:opt[0], g:opt[1])
1763
1764 " 32d: Setting again global boolean local (to window) option"
1765 noa set nocursorcolumn " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001766 let g:options = [['cursorcolumn', '0', '0', '0', '1', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001767 set cursorcolumn
1768 call assert_equal([], g:options)
1769 call assert_equal(g:opt[0], g:opt[1])
1770
1771
Bram Moolenaar1bc353b2019-09-01 14:45:28 +02001772 " 33: Test autocommands when an option value is converted internally.
Bram Moolenaard7c96872019-06-15 17:12:48 +02001773 noa set backspace=1 " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001774 let g:options = [['backspace', 'indent,eol', 'indent,eol', 'indent,eol', '2', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001775 set backspace=2
1776 call assert_equal([], g:options)
1777 call assert_equal(g:opt[0], g:opt[1])
1778
1779
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001780 " Cleanup
1781 au! OptionSet
Bram Moolenaar0331faf2019-06-15 18:40:37 +02001782 " set tags&
Bram Moolenaard7c96872019-06-15 17:12:48 +02001783 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 +02001784 exe printf(":set %s&vim", opt)
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001785 endfor
1786 call test_override('starting', 0)
1787 delfunc! AutoCommandOptionSet
1788endfunc
1789
1790func Test_OptionSet_diffmode()
1791 call test_override('starting', 1)
Bram Moolenaar26d98212019-01-27 22:32:55 +01001792 " 18: Changing an option when entering diff mode
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001793 new
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001794 au OptionSet diff :let &l:cul = v:option_new
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001795
1796 call setline(1, ['buffer 1', 'line2', 'line3', 'line4'])
1797 call assert_equal(0, &l:cul)
1798 diffthis
1799 call assert_equal(1, &l:cul)
1800
1801 vnew
1802 call setline(1, ['buffer 2', 'line 2', 'line 3', 'line4'])
1803 call assert_equal(0, &l:cul)
1804 diffthis
1805 call assert_equal(1, &l:cul)
1806
1807 diffoff
1808 call assert_equal(0, &l:cul)
1809 call assert_equal(1, getwinvar(2, '&l:cul'))
1810 bw!
1811
1812 call assert_equal(1, &l:cul)
1813 diffoff!
1814 call assert_equal(0, &l:cul)
1815 call assert_equal(0, getwinvar(1, '&l:cul'))
1816 bw!
1817
1818 " Cleanup
1819 au! OptionSet
1820 call test_override('starting', 0)
1821endfunc
1822
1823func Test_OptionSet_diffmode_close()
1824 call test_override('starting', 1)
1825 " 19: Try to close the current window when entering diff mode
1826 " should not segfault
1827 new
1828 au OptionSet diff close
1829
1830 call setline(1, ['buffer 1', 'line2', 'line3', 'line4'])
Bram Moolenaare2e40752020-09-04 21:18:46 +02001831 call assert_fails(':diffthis', 'E788:')
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001832 call assert_equal(1, &diff)
1833 vnew
1834 call setline(1, ['buffer 2', 'line 2', 'line 3', 'line4'])
Bram Moolenaare2e40752020-09-04 21:18:46 +02001835 call assert_fails(':diffthis', 'E788:')
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001836 call assert_equal(1, &diff)
Bram Moolenaara9aa86f2019-11-10 21:25:45 +01001837 set diffopt-=closeoff
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001838 bw!
Bram Moolenaare2e40752020-09-04 21:18:46 +02001839 call assert_fails(':diffoff!', 'E788:')
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001840 bw!
1841
1842 " Cleanup
1843 au! OptionSet
1844 call test_override('starting', 0)
1845 "delfunc! AutoCommandOptionSet
1846endfunc
Bram Moolenaar4a137b42017-08-04 22:37:11 +02001847
1848" Test for Bufleave autocommand that deletes the buffer we are about to edit.
1849func Test_BufleaveWithDelete()
Bram Moolenaare7cda972022-08-29 11:02:59 +01001850 new | edit XbufLeave1
Bram Moolenaar4a137b42017-08-04 22:37:11 +02001851
1852 augroup test_bufleavewithdelete
1853 autocmd!
Bram Moolenaare7cda972022-08-29 11:02:59 +01001854 autocmd BufLeave XbufLeave1 bwipe XbufLeave2
Bram Moolenaar4a137b42017-08-04 22:37:11 +02001855 augroup END
1856
Bram Moolenaare7cda972022-08-29 11:02:59 +01001857 call assert_fails('edit XbufLeave2', 'E143:')
1858 call assert_equal('XbufLeave1', bufname('%'))
Bram Moolenaar4a137b42017-08-04 22:37:11 +02001859
Bram Moolenaare7cda972022-08-29 11:02:59 +01001860 autocmd! test_bufleavewithdelete BufLeave XbufLeave1
Bram Moolenaar4a137b42017-08-04 22:37:11 +02001861 augroup! test_bufleavewithdelete
1862
1863 new
Bram Moolenaare7cda972022-08-29 11:02:59 +01001864 bwipe! XbufLeave1
Bram Moolenaar4a137b42017-08-04 22:37:11 +02001865endfunc
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +02001866
1867" Test for autocommand that changes the buffer list, when doing ":ball".
1868func Test_Acmd_BufAll()
1869 enew!
1870 %bwipe!
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01001871 call writefile(['Test file Xxx1'], 'Xxx1', 'D')
1872 call writefile(['Test file Xxx2'], 'Xxx2', 'D')
1873 call writefile(['Test file Xxx3'], 'Xxx3', 'D')
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +02001874
1875 " Add three files to the buffer list
1876 split Xxx1
1877 close
1878 split Xxx2
1879 close
1880 split Xxx3
1881 close
1882
1883 " Wipe the buffer when the buffer is opened
1884 au BufReadPost Xxx2 bwipe
1885
1886 call append(0, 'Test file Xxx4')
1887 ball
1888
1889 call assert_equal(2, winnr('$'))
1890 call assert_equal('Xxx1', bufname(winbufnr(winnr('$'))))
1891 wincmd t
1892
1893 au! BufReadPost
1894 %bwipe!
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +02001895 enew! | only
1896endfunc
1897
1898" Test for autocommand that changes current buffer on BufEnter event.
1899" Check if modelines are interpreted for the correct buffer.
1900func Test_Acmd_BufEnter()
1901 %bwipe!
1902 call writefile(['start of test file Xxx1',
1903 \ "\<Tab>this is a test",
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01001904 \ 'end of test file Xxx1'], 'Xxx1', 'D')
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +02001905 call writefile(['start of test file Xxx2',
1906 \ 'vim: set noai :',
1907 \ "\<Tab>this is a test",
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01001908 \ 'end of test file Xxx2'], 'Xxx2', 'D')
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +02001909
1910 au BufEnter Xxx2 brew
1911 set ai modeline modelines=3
1912 edit Xxx1
1913 " edit Xxx2, autocmd will do :brew
1914 edit Xxx2
1915 exe "normal G?this is a\<CR>"
1916 " Append text with autoindent to this file
1917 normal othis should be auto-indented
1918 call assert_equal("\<Tab>this should be auto-indented", getline('.'))
1919 call assert_equal(3, line('.'))
1920 " Remove autocmd and edit Xxx2 again
1921 au! BufEnter Xxx2
1922 buf! Xxx2
1923 exe "normal G?this is a\<CR>"
1924 " append text without autoindent to Xxx
1925 normal othis should be in column 1
1926 call assert_equal("this should be in column 1", getline('.'))
1927 call assert_equal(4, line('.'))
1928
1929 %bwipe!
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +02001930 set ai&vim modeline&vim modelines&vim
1931endfunc
1932
1933" Test for issue #57
1934" do not move cursor on <c-o> when autoindent is set
1935func Test_ai_CTRL_O()
1936 enew!
1937 set ai
1938 let save_fo = &fo
1939 set fo+=r
1940 exe "normal o# abcdef\<Esc>2hi\<CR>\<C-O>d0\<Esc>"
1941 exe "normal o# abcdef\<Esc>2hi\<C-O>d0\<Esc>"
1942 call assert_equal(['# abc', 'def', 'def'], getline(2, 4))
1943
1944 set ai&vim
1945 let &fo = save_fo
1946 enew!
1947endfunc
1948
1949" Test for autocommand that deletes the current buffer on BufLeave event.
1950" Also test deleting the last buffer, should give a new, empty buffer.
1951func Test_BufLeave_Wipe()
1952 %bwipe!
1953 let content = ['start of test file Xxx',
1954 \ 'this is a test',
1955 \ 'end of test file Xxx']
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01001956 call writefile(content, 'Xxx1', 'D')
1957 call writefile(content, 'Xxx2', 'D')
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +02001958
1959 au BufLeave Xxx2 bwipe
1960 edit Xxx1
1961 split Xxx2
1962 " delete buffer Xxx2, we should be back to Xxx1
1963 bwipe
1964 call assert_equal('Xxx1', bufname('%'))
1965 call assert_equal(1, winnr('$'))
1966
1967 " Create an alternate buffer
1968 %write! test.out
1969 call assert_equal('test.out', bufname('#'))
1970 " delete alternate buffer
1971 bwipe test.out
1972 call assert_equal('Xxx1', bufname('%'))
1973 call assert_equal('', bufname('#'))
1974
1975 au BufLeave Xxx1 bwipe
1976 " delete current buffer, get an empty one
1977 bwipe!
1978 call assert_equal(1, line('$'))
1979 call assert_equal('', bufname('%'))
Bram Moolenaarb2c87502017-10-14 21:15:58 +02001980 let g:bufinfo = getbufinfo()
1981 call assert_equal(1, len(g:bufinfo))
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +02001982
Bram Moolenaar53f0c962017-10-22 14:23:59 +02001983 call delete('test.out')
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +02001984 %bwipe
1985 au! BufLeave
Bram Moolenaarb2c87502017-10-14 21:15:58 +02001986
1987 " check that bufinfo doesn't contain a pointer to freed memory
1988 call test_garbagecollect_now()
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +02001989endfunc
Bram Moolenaar87ffb5c2017-10-19 12:37:42 +02001990
1991func Test_QuitPre()
1992 edit Xfoo
1993 let winid = win_getid(winnr())
1994 split Xbar
1995 au! QuitPre * let g:afile = expand('<afile>')
1996 " Close the other window, <afile> should be correct.
1997 exe win_id2win(winid) . 'q'
1998 call assert_equal('Xfoo', g:afile)
LemonBoy66e13ae2022-04-21 22:52:11 +01001999
Bram Moolenaar87ffb5c2017-10-19 12:37:42 +02002000 unlet g:afile
2001 bwipe Xfoo
2002 bwipe Xbar
2003endfunc
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02002004
Girish Palya92f68e22025-04-21 11:12:41 +02002005func Test_Cmdline_Trigger()
2006 autocmd CmdlineLeavePre : let g:log = "CmdlineLeavePre"
Girish Palya612f63b2025-04-28 18:29:52 +02002007 autocmd CmdlineLeave : let g:log2 = "CmdlineLeave"
Girish Palya92f68e22025-04-21 11:12:41 +02002008 new
2009 let g:log = ''
Girish Palya46755e62025-04-27 19:28:06 +02002010 let g:log2 = ''
Girish Palya92f68e22025-04-21 11:12:41 +02002011 nnoremap <F1> <Cmd>echo "hello"<CR>
2012 call feedkeys("\<F1>", 'x')
2013 call assert_equal('', g:log)
Girish Palya46755e62025-04-27 19:28:06 +02002014 call assert_equal('', g:log2)
Girish Palya92f68e22025-04-21 11:12:41 +02002015 nunmap <F1>
Girish Palya46755e62025-04-27 19:28:06 +02002016
Girish Palya92f68e22025-04-21 11:12:41 +02002017 let g:log = ''
Girish Palya46755e62025-04-27 19:28:06 +02002018 let g:log2 = ''
Girish Palya92f68e22025-04-21 11:12:41 +02002019 nnoremap <F1> :echo "hello"<CR>
2020 call feedkeys("\<F1>", 'x')
2021 call assert_equal('CmdlineLeavePre', g:log)
Girish Palya46755e62025-04-27 19:28:06 +02002022 call assert_equal('CmdlineLeave', g:log2)
Girish Palya92f68e22025-04-21 11:12:41 +02002023 nunmap <F1>
Girish Palya46755e62025-04-27 19:28:06 +02002024
Girish Palya92f68e22025-04-21 11:12:41 +02002025 let g:log = ''
Girish Palya46755e62025-04-27 19:28:06 +02002026 let g:log2 = ''
2027 call feedkeys(":\<bs>", "tx")
2028 call assert_equal('CmdlineLeavePre', g:log)
2029 call assert_equal('CmdlineLeave', g:log2)
2030
2031 let g:log = ''
2032 let g:log2 = ''
Girish Palya92f68e22025-04-21 11:12:41 +02002033 split
2034 call assert_equal('', g:log)
2035 call feedkeys(":echo hello", "tx")
2036 call assert_equal('CmdlineLeavePre', g:log)
Girish Palya46755e62025-04-27 19:28:06 +02002037 call assert_equal('CmdlineLeave', g:log2)
2038
Girish Palya92f68e22025-04-21 11:12:41 +02002039 let g:log = ''
Girish Palya46755e62025-04-27 19:28:06 +02002040 let g:log2 = ''
Girish Palya92f68e22025-04-21 11:12:41 +02002041 close
2042 call assert_equal('', g:log)
2043 call feedkeys(":echo hello", "tx")
2044 call assert_equal('CmdlineLeavePre', g:log)
Girish Palya46755e62025-04-27 19:28:06 +02002045 call assert_equal('CmdlineLeave', g:log2)
2046
Girish Palya92f68e22025-04-21 11:12:41 +02002047 let g:log = ''
Girish Palya46755e62025-04-27 19:28:06 +02002048 let g:log2 = ''
Girish Palya92f68e22025-04-21 11:12:41 +02002049 tabnew
2050 call assert_equal('', g:log)
2051 call feedkeys(":echo hello", "tx")
2052 call assert_equal('CmdlineLeavePre', g:log)
Girish Palya46755e62025-04-27 19:28:06 +02002053 call assert_equal('CmdlineLeave', g:log2)
2054
Girish Palya92f68e22025-04-21 11:12:41 +02002055 let g:log = ''
Girish Palya46755e62025-04-27 19:28:06 +02002056 let g:log2 = ''
Girish Palya92f68e22025-04-21 11:12:41 +02002057 split
2058 call assert_equal('', g:log)
2059 call feedkeys(":echo hello", "tx")
2060 call assert_equal('CmdlineLeavePre', g:log)
Girish Palya46755e62025-04-27 19:28:06 +02002061 call assert_equal('CmdlineLeave', g:log2)
2062
Girish Palya92f68e22025-04-21 11:12:41 +02002063 let g:log = ''
Girish Palya46755e62025-04-27 19:28:06 +02002064 let g:log2 = ''
Girish Palya92f68e22025-04-21 11:12:41 +02002065 tabclose
2066 call assert_equal('', g:log)
2067 call feedkeys(":echo hello", "tx")
2068 call assert_equal('CmdlineLeavePre', g:log)
Girish Palya46755e62025-04-27 19:28:06 +02002069 call assert_equal('CmdlineLeave', g:log2)
2070
zeertzjq92403692025-04-28 18:04:00 +02002071 autocmd CmdlineLeavePre * let g:cmdline += [getcmdline()]
2072
2073 for end_keys in ["\<CR>", "\<NL>", "\<kEnter>", "\<C-C>", "\<Esc>",
2074 \ "\<C-\>\<C-N>", "\<C-\>\<C-G>"]
2075 let g:cmdline = []
2076 let g:log = ''
2077 let g:log2 = ''
2078 call assert_equal('', g:log)
2079 let keys = $':echo "hello"{end_keys}'
2080 let msg = keytrans(keys)
2081 call feedkeys(keys, "tx")
2082 call assert_equal(['echo "hello"'], g:cmdline, msg)
2083 call assert_equal('CmdlineLeavePre', g:log, msg)
2084 call assert_equal('CmdlineLeave', g:log2, msg)
2085 endfor
2086
2087 let g:cmdline = []
2088 call feedkeys(":let c = input('? ')\<cr>ABCDE\<cr>", "tx")
2089 call assert_equal(["let c = input('? ')", 'ABCDE'], g:cmdline)
2090
2091 au! CmdlineLeavePre
2092 unlet! g:cmdline
Girish Palya5c3d1e32025-04-22 19:52:16 +02002093 unlet! g:log
Girish Palya46755e62025-04-27 19:28:06 +02002094 unlet! g:log2
Girish Palya92f68e22025-04-21 11:12:41 +02002095 bw!
2096endfunc
2097
Girish Palya46755e62025-04-27 19:28:06 +02002098" Ensure :cabbr does not cause a spurious CmdlineLeavePre.
2099func Test_CmdlineLeavePre_cabbr()
Girish Palya6220bba2025-04-28 18:00:40 +02002100 " For unknown reason this fails intermittently on MS-Windows
2101 CheckNotMSWindows
Girish Palya46755e62025-04-27 19:28:06 +02002102 CheckFeature terminal
2103 let buf = term_start([GetVimProg(), '--clean', '-c', 'set noswapfile'], {'term_rows': 3})
2104 call assert_equal('running', term_getstatus(buf))
2105 call term_sendkeys(buf, ":let g:a=0\<cr>")
2106 call term_wait(buf, 50)
2107 call term_sendkeys(buf, ":cabbr v v\<cr>")
2108 call term_wait(buf, 50)
2109 call term_sendkeys(buf, ":command! -nargs=* Foo echo\<cr>")
2110 call term_wait(buf, 50)
2111 call term_sendkeys(buf, ":au! CmdlineLeavePre * :let g:a+=1\<cr>")
2112 call term_wait(buf, 50)
2113 call term_sendkeys(buf, ":Foo v\<cr>")
2114 call term_wait(buf, 50)
2115 call term_sendkeys(buf, ":echo g:a\<cr>")
2116 call term_wait(buf, 50)
2117 call WaitForAssert({-> assert_match('^2.*$', term_getline(buf, 3))})
2118 bwipe!
2119endfunc
2120
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02002121func Test_Cmdline()
Bram Moolenaar153b7042018-01-31 15:48:32 +01002122 au! CmdlineChanged : let g:text = getcmdline()
2123 let g:text = 0
2124 call feedkeys(":echom 'hello'\<CR>", 'xt')
2125 call assert_equal("echom 'hello'", g:text)
2126 au! CmdlineChanged
2127
2128 au! CmdlineChanged : let g:entered = expand('<afile>')
2129 let g:entered = 0
2130 call feedkeys(":echom 'hello'\<CR>", 'xt')
2131 call assert_equal(':', g:entered)
2132 au! CmdlineChanged
2133
zeertzjq412e0e42023-02-11 10:34:07 +00002134 autocmd CmdlineChanged : let g:log += [getcmdline()]
2135
Bram Moolenaarbb393d82022-12-09 12:21:50 +00002136 let g:log = []
2137 cnoremap <F1> <Cmd>call setcmdline('ls')<CR>
Bram Moolenaarbb393d82022-12-09 12:21:50 +00002138 call feedkeys(":\<F1>", 'xt')
2139 call assert_equal(['ls'], g:log)
Bram Moolenaarbb393d82022-12-09 12:21:50 +00002140 cunmap <F1>
2141
zeertzjqaf9e28a2023-02-06 20:58:09 +00002142 let g:log = []
zeertzjqaf9e28a2023-02-06 20:58:09 +00002143 call feedkeys(":sign \<Tab>\<Tab>\<C-N>\<C-P>\<S-Tab>\<S-Tab>\<Esc>", 'xt')
2144 call assert_equal([
2145 \ 's',
2146 \ 'si',
2147 \ 'sig',
2148 \ 'sign',
2149 \ 'sign ',
2150 \ 'sign define',
2151 \ 'sign jump',
2152 \ 'sign list',
2153 \ 'sign jump',
2154 \ 'sign define',
2155 \ 'sign ',
2156 \ ], g:log)
2157 let g:log = []
2158 set wildmenu wildoptions+=pum
2159 call feedkeys(":sign \<S-Tab>\<PageUp>\<kPageUp>\<kPageDown>\<PageDown>\<Esc>", 'xt')
2160 call assert_equal([
2161 \ 's',
2162 \ 'si',
2163 \ 'sig',
2164 \ 'sign',
2165 \ 'sign ',
2166 \ 'sign unplace',
2167 \ 'sign jump',
2168 \ 'sign define',
2169 \ 'sign undefine',
2170 \ 'sign unplace',
2171 \ ], g:log)
2172 set wildmenu& wildoptions&
zeertzjq412e0e42023-02-11 10:34:07 +00002173
2174 let g:log = []
2175 let @r = 'abc'
2176 call feedkeys(":0\<C-R>r1\<C-R>\<C-O>r2\<C-R>\<C-R>r3\<Esc>", 'xt')
2177 call assert_equal([
2178 \ '0',
2179 \ '0a',
2180 \ '0ab',
2181 \ '0abc',
2182 \ '0abc1',
2183 \ '0abc1abc',
2184 \ '0abc1abc2',
2185 \ '0abc1abc2abc',
2186 \ '0abc1abc2abc3',
2187 \ ], g:log)
2188
zeertzjqaf9e28a2023-02-06 20:58:09 +00002189 unlet g:log
2190 au! CmdlineChanged
2191
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02002192 au! CmdlineEnter : let g:entered = expand('<afile>')
2193 au! CmdlineLeave : let g:left = expand('<afile>')
Girish Palya92f68e22025-04-21 11:12:41 +02002194 au! CmdlineLeavePre : let g:leftpre = expand('<afile>')
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02002195 let g:entered = 0
2196 let g:left = 0
Girish Palya92f68e22025-04-21 11:12:41 +02002197 let g:leftpre = 0
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02002198 call feedkeys(":echo 'hello'\<CR>", 'xt')
2199 call assert_equal(':', g:entered)
2200 call assert_equal(':', g:left)
Girish Palya92f68e22025-04-21 11:12:41 +02002201 call assert_equal(':', g:leftpre)
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02002202 au! CmdlineEnter
2203 au! CmdlineLeave
Girish Palya92f68e22025-04-21 11:12:41 +02002204 au! CmdlineLeavePre
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02002205
Bram Moolenaara4baf5b2018-04-22 13:27:44 +02002206 let save_shellslash = &shellslash
2207 set noshellslash
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02002208 au! CmdlineEnter / let g:entered = expand('<afile>')
2209 au! CmdlineLeave / let g:left = expand('<afile>')
Girish Palya92f68e22025-04-21 11:12:41 +02002210 au! CmdlineLeavePre / let g:leftpre = expand('<afile>')
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02002211 let g:entered = 0
2212 let g:left = 0
Girish Palya92f68e22025-04-21 11:12:41 +02002213 let g:leftpre = 0
Bram Moolenaar53f0c962017-10-22 14:23:59 +02002214 new
2215 call setline(1, 'hello')
2216 call feedkeys("/hello\<CR>", 'xt')
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02002217 call assert_equal('/', g:entered)
2218 call assert_equal('/', g:left)
Girish Palya92f68e22025-04-21 11:12:41 +02002219 call assert_equal('/', g:leftpre)
Bram Moolenaar53f0c962017-10-22 14:23:59 +02002220 bwipe!
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02002221 au! CmdlineEnter
2222 au! CmdlineLeave
Girish Palya92f68e22025-04-21 11:12:41 +02002223 au! CmdlineLeavePre
Bram Moolenaara4baf5b2018-04-22 13:27:44 +02002224 let &shellslash = save_shellslash
Shougo Matsushitad0952142024-06-20 22:05:16 +02002225
Girish Palya92f68e22025-04-21 11:12:41 +02002226 let g:left = "cancelled"
2227 let g:leftpre = "cancelled"
2228 au! CmdlineLeave : let g:left = "triggered"
2229 au! CmdlineLeavePre : let g:leftpre = "triggered"
2230 call feedkeys(":echo 'hello'\<esc>", 'xt')
2231 call assert_equal('triggered', g:left)
2232 call assert_equal('triggered', g:leftpre)
2233 let g:left = "cancelled"
2234 let g:leftpre = "cancelled"
2235 au! CmdlineLeave : let g:left = "triggered"
2236 call feedkeys(":echo 'hello'\<c-c>", 'xt')
2237 call assert_equal('triggered', g:left)
2238 call assert_equal('triggered', g:leftpre)
2239 au! CmdlineLeave
2240 au! CmdlineLeavePre
2241
zeertzjqbc6f9672024-06-21 07:51:40 +02002242 au! CursorMovedC : let g:pos += [getcmdpos()]
2243 let g:pos = []
zeertzjq81456202024-07-07 20:48:25 +02002244 call feedkeys(":foo bar baz\<C-W>\<C-W>\<C-W>\<Esc>", 'xt')
2245 call assert_equal([2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 9, 5, 1], g:pos)
2246 let g:pos = []
2247 call feedkeys(":hello\<C-B>\<Esc>", 'xt')
2248 call assert_equal([2, 3, 4, 5, 6, 1], g:pos)
2249 let g:pos = []
2250 call feedkeys(":hello\<C-U>\<Esc>", 'xt')
2251 call assert_equal([2, 3, 4, 5, 6, 1], g:pos)
2252 let g:pos = []
zeertzjqbc6f9672024-06-21 07:51:40 +02002253 call feedkeys(":hello\<Left>\<C-R>=''\<CR>\<Left>\<Right>\<Esc>", 'xt')
zeertzjq81456202024-07-07 20:48:25 +02002254 call assert_equal([2, 3, 4, 5, 6, 5, 4, 5], g:pos)
zeertzjqbc6f9672024-06-21 07:51:40 +02002255 let g:pos = []
2256 call feedkeys(":12345678\<C-R>=setcmdpos(3)??''\<CR>\<Esc>", 'xt')
zeertzjq81456202024-07-07 20:48:25 +02002257 call assert_equal([2, 3, 4, 5, 6, 7, 8, 9, 3], g:pos)
zeertzjqbc6f9672024-06-21 07:51:40 +02002258 let g:pos = []
2259 call feedkeys(":12345678\<C-R>=setcmdpos(3)??''\<CR>\<Left>\<Esc>", 'xt')
zeertzjq81456202024-07-07 20:48:25 +02002260 call assert_equal([2, 3, 4, 5, 6, 7, 8, 9, 3, 2], g:pos)
Shougo Matsushitad0952142024-06-20 22:05:16 +02002261 au! CursorMovedC
2262
zeertzjqbc6f9672024-06-21 07:51:40 +02002263 " setcmdpos() is no-op inside an autocommand
2264 au! CursorMovedC : let g:pos += [getcmdpos()] | call setcmdpos(1)
2265 let g:pos = []
2266 call feedkeys(":hello\<Left>\<Left>\<Esc>", 'xt')
zeertzjq81456202024-07-07 20:48:25 +02002267 call assert_equal([2, 3, 4, 5, 6, 5, 4], g:pos)
Shougo Matsushitad0952142024-06-20 22:05:16 +02002268 au! CursorMovedC
zeertzjqbc6f9672024-06-21 07:51:40 +02002269
2270 unlet g:entered
2271 unlet g:left
2272 unlet g:pos
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02002273endfunc
Bram Moolenaar53f0c962017-10-22 14:23:59 +02002274
2275" Test for BufWritePre autocommand that deletes or unloads the buffer.
2276func Test_BufWritePre()
2277 %bwipe
2278 au BufWritePre Xxx1 bunload
2279 au BufWritePre Xxx2 bwipe
2280
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01002281 call writefile(['start of Xxx1', 'test', 'end of Xxx1'], 'Xxx1', 'D')
2282 call writefile(['start of Xxx2', 'test', 'end of Xxx2'], 'Xxx2', 'D')
Bram Moolenaar53f0c962017-10-22 14:23:59 +02002283
2284 edit Xtest
2285 e! Xxx2
2286 bdel Xtest
2287 e Xxx1
2288 " write it, will unload it and give an error msg
Bram Moolenaare2e40752020-09-04 21:18:46 +02002289 call assert_fails('w', 'E203:')
Bram Moolenaar53f0c962017-10-22 14:23:59 +02002290 call assert_equal('Xxx2', bufname('%'))
2291 edit Xtest
2292 e! Xxx2
2293 bwipe Xtest
2294 " write it, will delete the buffer and give an error msg
Bram Moolenaare2e40752020-09-04 21:18:46 +02002295 call assert_fails('w', 'E203:')
Bram Moolenaar53f0c962017-10-22 14:23:59 +02002296 call assert_equal('Xxx1', bufname('%'))
2297 au! BufWritePre
Bram Moolenaar53f0c962017-10-22 14:23:59 +02002298endfunc
2299
2300" Test for BufUnload autocommand that unloads all the other buffers
2301func Test_bufunload_all()
Bram Moolenaarf08b0eb2021-10-16 13:00:14 +01002302 let g:test_is_flaky = 1
Christian Brabandtee17b6f2023-09-09 11:23:50 +02002303 call writefile(['Test file Xxx1'], 'Xxx1', 'D')
2304 call writefile(['Test file Xxx2'], 'Xxx2', 'D')
Bram Moolenaar53f0c962017-10-22 14:23:59 +02002305
Bram Moolenaarc79745a2019-05-20 22:12:34 +02002306 let content =<< trim [CODE]
2307 func UnloadAllBufs()
2308 let i = 1
2309 while i <= bufnr('$')
2310 if i != bufnr('%') && bufloaded(i)
2311 exe i . 'bunload'
2312 endif
2313 let i += 1
2314 endwhile
2315 endfunc
2316 au BufUnload * call UnloadAllBufs()
2317 au VimLeave * call writefile(['Test Finished'], 'Xout')
2318 edit Xxx1
2319 split Xxx2
2320 q
2321 [CODE]
2322
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01002323 call writefile(content, 'Xbunloadtest', 'D')
Bram Moolenaar53f0c962017-10-22 14:23:59 +02002324
2325 call delete('Xout')
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01002326 call system(GetVimCommandClean() .. ' -N --not-a-term -S Xbunloadtest')
Bram Moolenaar53f0c962017-10-22 14:23:59 +02002327 call assert_true(filereadable('Xout'))
2328
Bram Moolenaar53f0c962017-10-22 14:23:59 +02002329 call delete('Xout')
2330endfunc
2331
2332" Some tests for buffer-local autocommands
2333func Test_buflocal_autocmd()
2334 let g:bname = ''
2335 edit xx
2336 au BufLeave <buffer> let g:bname = expand("%")
2337 " here, autocommand for xx should trigger.
2338 " but autocommand shall not apply to buffer named <buffer>.
2339 edit somefile
2340 call assert_equal('xx', g:bname)
2341 let g:bname = ''
2342 " here, autocommand shall be auto-deleted
2343 bwipe xx
2344 " autocmd should not trigger
2345 edit xx
2346 call assert_equal('', g:bname)
2347 " autocmd should not trigger
2348 edit somefile
2349 call assert_equal('', g:bname)
2350 enew
2351 unlet g:bname
2352endfunc
Bram Moolenaar430dc5d2017-11-02 21:04:47 +01002353
2354" Test for "*Cmd" autocommands
2355func Test_Cmd_Autocmds()
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01002356 call writefile(['start of Xxx', "\tabc2", 'end of Xxx'], 'Xxx', 'D')
Bram Moolenaar430dc5d2017-11-02 21:04:47 +01002357
2358 enew!
2359 au BufReadCmd XtestA 0r Xxx|$del
2360 edit XtestA " will read text of Xxd instead
2361 call assert_equal('start of Xxx', getline(1))
2362
2363 au BufWriteCmd XtestA call append(line("$"), "write")
2364 write " will append a line to the file
2365 call assert_equal('write', getline('$'))
Bram Moolenaare2e40752020-09-04 21:18:46 +02002366 call assert_fails('read XtestA', 'E484:') " should not read anything
Bram Moolenaar430dc5d2017-11-02 21:04:47 +01002367 call assert_equal('write', getline(4))
2368
2369 " now we have:
2370 " 1 start of Xxx
2371 " 2 abc2
2372 " 3 end of Xxx
2373 " 4 write
2374
2375 au FileReadCmd XtestB '[r Xxx
2376 2r XtestB " will read Xxx below line 2 instead
2377 call assert_equal('start of Xxx', getline(3))
2378
2379 " now we have:
2380 " 1 start of Xxx
2381 " 2 abc2
2382 " 3 start of Xxx
2383 " 4 abc2
2384 " 5 end of Xxx
2385 " 6 end of Xxx
2386 " 7 write
2387
2388 au FileWriteCmd XtestC '[,']copy $
2389 normal 4GA1
2390 4,5w XtestC " will copy lines 4 and 5 to the end
2391 call assert_equal("\tabc21", getline(8))
Bram Moolenaare2e40752020-09-04 21:18:46 +02002392 call assert_fails('r XtestC', 'E484:') " should not read anything
Bram Moolenaar430dc5d2017-11-02 21:04:47 +01002393 call assert_equal("end of Xxx", getline(9))
2394
2395 " now we have:
2396 " 1 start of Xxx
2397 " 2 abc2
2398 " 3 start of Xxx
2399 " 4 abc21
2400 " 5 end of Xxx
2401 " 6 end of Xxx
2402 " 7 write
2403 " 8 abc21
2404 " 9 end of Xxx
2405
2406 let g:lines = []
2407 au FileAppendCmd XtestD call extend(g:lines, getline(line("'["), line("']")))
2408 w >>XtestD " will add lines to 'lines'
2409 call assert_equal(9, len(g:lines))
Bram Moolenaare2e40752020-09-04 21:18:46 +02002410 call assert_fails('$r XtestD', 'E484:') " should not read anything
Bram Moolenaar430dc5d2017-11-02 21:04:47 +01002411 call assert_equal(9, line('$'))
2412 call assert_equal('end of Xxx', getline('$'))
2413
2414 au BufReadCmd XtestE 0r Xxx|$del
2415 sp XtestE " split window with test.out
2416 call assert_equal('end of Xxx', getline(3))
2417
2418 let g:lines = []
2419 exe "normal 2Goasdf\<Esc>\<C-W>\<C-W>"
2420 au BufWriteCmd XtestE call extend(g:lines, getline(0, '$'))
2421 wall " will write other window to 'lines'
2422 call assert_equal(4, len(g:lines), g:lines)
2423 call assert_equal('asdf', g:lines[2])
2424
2425 au! BufReadCmd
2426 au! BufWriteCmd
2427 au! FileReadCmd
2428 au! FileWriteCmd
2429 au! FileAppendCmd
2430 %bwipe!
Bram Moolenaar430dc5d2017-11-02 21:04:47 +01002431 enew!
2432endfunc
Bram Moolenaaraace2152017-11-05 16:23:10 +01002433
Bram Moolenaar0fff4412020-03-29 16:06:29 +02002434func s:ReadFile()
2435 setl noswapfile nomodified
2436 let filename = resolve(expand("<afile>:p"))
2437 execute 'read' fnameescape(filename)
2438 1d_
2439 exe 'file' fnameescape(filename)
2440 setl buftype=acwrite
2441endfunc
2442
2443func s:WriteFile()
2444 let filename = resolve(expand("<afile>:p"))
2445 setl buftype=
2446 noautocmd execute 'write' fnameescape(filename)
2447 setl buftype=acwrite
2448 setl nomodified
2449endfunc
2450
2451func Test_BufReadCmd()
2452 autocmd BufReadCmd *.test call s:ReadFile()
2453 autocmd BufWriteCmd *.test call s:WriteFile()
2454
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01002455 call writefile(['one', 'two', 'three'], 'Xcmd.test', 'D')
Bram Moolenaar0fff4412020-03-29 16:06:29 +02002456 edit Xcmd.test
2457 call assert_match('Xcmd.test" line 1 of 3', execute('file'))
2458 normal! Gofour
2459 write
2460 call assert_equal(['one', 'two', 'three', 'four'], readfile('Xcmd.test'))
2461
2462 bwipe!
Bram Moolenaar0fff4412020-03-29 16:06:29 +02002463 au! BufReadCmd
2464 au! BufWriteCmd
2465endfunc
2466
zeertzjq9c8f9462022-08-30 18:17:15 +01002467func Test_BufWriteCmd()
2468 autocmd BufWriteCmd Xbufwritecmd let g:written = 1
2469 new
2470 file Xbufwritecmd
2471 set buftype=acwrite
Bram Moolenaar6f14da12022-09-07 21:30:44 +01002472 call mkdir('Xbufwritecmd', 'D')
zeertzjq9c8f9462022-08-30 18:17:15 +01002473 write
2474 " BufWriteCmd should be triggered even if a directory has the same name
2475 call assert_equal(1, g:written)
zeertzjq9c8f9462022-08-30 18:17:15 +01002476 unlet g:written
2477 au! BufWriteCmd
2478 bwipe!
2479endfunc
2480
Bram Moolenaaraace2152017-11-05 16:23:10 +01002481func SetChangeMarks(start, end)
Bram Moolenaar97c69432021-01-15 16:45:21 +01002482 exe a:start .. 'mark ['
2483 exe a:end .. 'mark ]'
Bram Moolenaaraace2152017-11-05 16:23:10 +01002484endfunc
2485
2486" Verify the effects of autocmds on '[ and ']
2487func Test_change_mark_in_autocmds()
2488 edit! Xtest
Bram Moolenaar97c69432021-01-15 16:45:21 +01002489 call feedkeys("ia\<CR>b\<CR>c\<CR>d\<C-g>u\<Esc>", 'xtn')
Bram Moolenaaraace2152017-11-05 16:23:10 +01002490
2491 call SetChangeMarks(2, 3)
2492 write
2493 call assert_equal([1, 4], [line("'["), line("']")])
2494
2495 call SetChangeMarks(2, 3)
2496 au BufWritePre * call assert_equal([1, 4], [line("'["), line("']")])
2497 write
2498 au! BufWritePre
2499
Bram Moolenaar14ddd222020-08-05 12:02:40 +02002500 if has('unix')
Bram Moolenaaraace2152017-11-05 16:23:10 +01002501 write XtestFilter
2502 write >> XtestFilter
2503
2504 call SetChangeMarks(2, 3)
2505 " Marks are set to the entire range of the write
2506 au FilterWritePre * call assert_equal([1, 4], [line("'["), line("']")])
2507 " '[ is adjusted to just before the line that will receive the filtered
2508 " data
2509 au FilterReadPre * call assert_equal([4, 4], [line("'["), line("']")])
2510 " The filtered data is read into the buffer, and the source lines are
2511 " still present, so the range is after the source lines
2512 au FilterReadPost * call assert_equal([5, 12], [line("'["), line("']")])
2513 %!cat XtestFilter
2514 " After the filtered data is read, the original lines are deleted
2515 call assert_equal([1, 8], [line("'["), line("']")])
2516 au! FilterWritePre,FilterReadPre,FilterReadPost
2517 undo
2518
2519 call SetChangeMarks(1, 4)
2520 au FilterWritePre * call assert_equal([2, 3], [line("'["), line("']")])
2521 au FilterReadPre * call assert_equal([3, 3], [line("'["), line("']")])
2522 au FilterReadPost * call assert_equal([4, 11], [line("'["), line("']")])
2523 2,3!cat XtestFilter
2524 call assert_equal([2, 9], [line("'["), line("']")])
2525 au! FilterWritePre,FilterReadPre,FilterReadPost
2526 undo
2527
2528 call delete('XtestFilter')
2529 endif
2530
2531 call SetChangeMarks(1, 4)
2532 au FileWritePre * call assert_equal([2, 3], [line("'["), line("']")])
2533 2,3write Xtest2
2534 au! FileWritePre
2535
2536 call SetChangeMarks(2, 3)
2537 au FileAppendPre * call assert_equal([1, 4], [line("'["), line("']")])
2538 write >> Xtest2
2539 au! FileAppendPre
2540
2541 call SetChangeMarks(1, 4)
2542 au FileAppendPre * call assert_equal([2, 3], [line("'["), line("']")])
2543 2,3write >> Xtest2
2544 au! FileAppendPre
2545
2546 call SetChangeMarks(1, 1)
2547 au FileReadPre * call assert_equal([3, 1], [line("'["), line("']")])
2548 au FileReadPost * call assert_equal([4, 11], [line("'["), line("']")])
2549 3read Xtest2
2550 au! FileReadPre,FileReadPost
2551 undo
2552
2553 call SetChangeMarks(4, 4)
2554 " When the line is 0, it's adjusted to 1
2555 au FileReadPre * call assert_equal([1, 4], [line("'["), line("']")])
2556 au FileReadPost * call assert_equal([1, 8], [line("'["), line("']")])
2557 0read Xtest2
2558 au! FileReadPre,FileReadPost
2559 undo
2560
2561 call SetChangeMarks(4, 4)
2562 " When the line is 0, it's adjusted to 1
2563 au FileReadPre * call assert_equal([1, 4], [line("'["), line("']")])
2564 au FileReadPost * call assert_equal([2, 9], [line("'["), line("']")])
2565 1read Xtest2
2566 au! FileReadPre,FileReadPost
2567 undo
2568
2569 bwipe!
2570 call delete('Xtest')
2571 call delete('Xtest2')
2572endfunc
2573
2574func Test_Filter_noshelltemp()
Bram Moolenaaraeb313f2020-11-27 19:13:28 +01002575 CheckExecutable cat
Bram Moolenaaraace2152017-11-05 16:23:10 +01002576
2577 enew!
2578 call setline(1, ['a', 'b', 'c', 'd'])
2579
2580 let shelltemp = &shelltemp
2581 set shelltemp
2582
2583 let g:filter_au = 0
2584 au FilterWritePre * let g:filter_au += 1
2585 au FilterReadPre * let g:filter_au += 1
2586 au FilterReadPost * let g:filter_au += 1
2587 %!cat
2588 call assert_equal(3, g:filter_au)
2589
2590 if has('filterpipe')
2591 set noshelltemp
2592
2593 let g:filter_au = 0
2594 au FilterWritePre * let g:filter_au += 1
2595 au FilterReadPre * let g:filter_au += 1
2596 au FilterReadPost * let g:filter_au += 1
2597 %!cat
2598 call assert_equal(0, g:filter_au)
2599 endif
2600
2601 au! FilterWritePre,FilterReadPre,FilterReadPost
2602 let &shelltemp = shelltemp
2603 bwipe!
2604endfunc
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01002605
2606func Test_TextYankPost()
2607 enew!
2608 call setline(1, ['foo'])
2609
2610 let g:event = []
2611 au TextYankPost * let g:event = copy(v:event)
2612
2613 call assert_equal({}, v:event)
2614 call assert_fails('let v:event = {}', 'E46:')
2615 call assert_fails('let v:event.mykey = 0', 'E742:')
2616
2617 norm "ayiw
2618 call assert_equal(
Bram Moolenaara016eeb2022-04-09 11:37:38 +01002619 \ #{regcontents: ['foo'], regname: 'a', operator: 'y',
2620 \ regtype: 'v', visual: v:false, inclusive: v:true},
2621 \ g:event)
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01002622 norm y_
2623 call assert_equal(
Bram Moolenaara016eeb2022-04-09 11:37:38 +01002624 \ #{regcontents: ['foo'], regname: '', operator: 'y', regtype: 'V',
2625 \ visual: v:false, inclusive: v:false},
2626 \ g:event)
Bram Moolenaar37d16732020-06-12 22:09:01 +02002627 norm Vy
2628 call assert_equal(
Bram Moolenaara016eeb2022-04-09 11:37:38 +01002629 \ #{regcontents: ['foo'], regname: '', operator: 'y', regtype: 'V',
2630 \ visual: v:true, inclusive: v:true},
2631 \ g:event)
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01002632 call feedkeys("\<C-V>y", 'x')
2633 call assert_equal(
Bram Moolenaara016eeb2022-04-09 11:37:38 +01002634 \ #{regcontents: ['f'], regname: '', operator: 'y', regtype: "\x161",
2635 \ visual: v:true, inclusive: v:true},
2636 \ g:event)
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01002637 norm "xciwbar
2638 call assert_equal(
Bram Moolenaara016eeb2022-04-09 11:37:38 +01002639 \ #{regcontents: ['foo'], regname: 'x', operator: 'c', regtype: 'v',
2640 \ visual: v:false, inclusive: v:true},
2641 \ g:event)
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01002642 norm "bdiw
2643 call assert_equal(
Bram Moolenaara016eeb2022-04-09 11:37:38 +01002644 \ #{regcontents: ['bar'], regname: 'b', operator: 'd', regtype: 'v',
2645 \ visual: v:false, inclusive: v:true},
2646 \ g:event)
2647
2648 call setline(1, 'foobar')
2649 " exclusive motion
2650 norm $"ay0
2651 call assert_equal(
2652 \ #{regcontents: ['fooba'], regname: 'a', operator: 'y', regtype: 'v',
2653 \ visual: v:false, inclusive: v:false},
2654 \ g:event)
2655 " inclusive motion
2656 norm 0"ay$
2657 call assert_equal(
2658 \ #{regcontents: ['foobar'], regname: 'a', operator: 'y', regtype: 'v',
2659 \ visual: v:false, inclusive: v:true},
2660 \ g:event)
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01002661
2662 call assert_equal({}, v:event)
2663
Bram Moolenaarfccbf062020-11-26 20:34:00 +01002664 if has('clipboard_working') && !has('gui_running')
2665 " Test that when the visual selection is automatically copied to clipboard
2666 " register a TextYankPost is emitted
2667 call setline(1, ['foobar'])
2668
2669 let @* = ''
2670 set clipboard=autoselect
2671 exe "norm! ggviw\<Esc>"
2672 call assert_equal(
Bram Moolenaara016eeb2022-04-09 11:37:38 +01002673 \ #{regcontents: ['foobar'], regname: '*', operator: 'y',
2674 \ regtype: 'v', visual: v:true, inclusive: v:false},
2675 \ g:event)
Bram Moolenaarfccbf062020-11-26 20:34:00 +01002676
2677 let @+ = ''
2678 set clipboard=autoselectplus
2679 exe "norm! ggviw\<Esc>"
2680 call assert_equal(
Bram Moolenaara016eeb2022-04-09 11:37:38 +01002681 \ #{regcontents: ['foobar'], regname: '+', operator: 'y',
2682 \ regtype: 'v', visual: v:true, inclusive: v:false},
2683 \ g:event)
Bram Moolenaarfccbf062020-11-26 20:34:00 +01002684
2685 set clipboard&vim
2686 endif
2687
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01002688 au! TextYankPost
2689 unlet g:event
2690 bwipe!
2691endfunc
Bram Moolenaar9bca8052017-12-18 12:37:55 +01002692
Bram Moolenaar9a046fd2021-01-28 13:47:59 +01002693func Test_autocommand_all_events()
2694 call assert_fails('au * * bwipe', 'E1155:')
2695 call assert_fails('au * x bwipe', 'E1155:')
Bram Moolenaarb6db1462021-12-24 19:24:47 +00002696 call assert_fails('au! * x bwipe', 'E1155:')
Bram Moolenaar4fb921e2017-12-18 15:33:00 +01002697endfunc
Bram Moolenaarb7407d32018-02-03 17:36:27 +01002698
Bram Moolenaarf6246f52022-02-11 16:30:12 +00002699func Test_autocmd_user()
2700 au User MyEvent let s:res = [expand("<afile>"), expand("<amatch>")]
2701 doautocmd User MyEvent
2702 call assert_equal(['MyEvent', 'MyEvent'], s:res)
2703 au! User
2704 unlet s:res
2705endfunc
2706
Bram Moolenaar3b014be2022-11-13 17:53:46 +00002707func Test_autocmd_user_clear_group()
2708 CheckRunVimInTerminal
2709
2710 let lines =<< trim END
2711 autocmd! User
2712 for i in range(1, 999)
2713 exe 'autocmd User ' .. 'Foo' .. i .. ' bar'
2714 endfor
2715 au CmdlineLeave : call timer_start(0, {-> execute('autocmd! User')})
2716 END
2717 call writefile(lines, 'XautoUser', 'D')
2718 let buf = RunVimInTerminal('-S XautoUser', {'rows': 10})
2719
2720 " this was using freed memory
2721 call term_sendkeys(buf, ":autocmd User\<CR>")
2722 call TermWait(buf, 50)
2723 call term_sendkeys(buf, "G")
2724
2725 call StopVimInTerminal(buf)
2726endfunc
2727
Bram Moolenaaref2c3252022-11-25 16:31:51 +00002728func Test_autocmd_CmdlineLeave_unlet()
2729 CheckRunVimInTerminal
2730
2731 let lines =<< trim END
2732 for i in range(1, 999)
2733 exe 'let g:var' .. i '=' i
2734 endfor
2735 au CmdlineLeave : call timer_start(0, {-> execute('unlet g:var990')})
2736 END
2737 call writefile(lines, 'XleaveUnlet', 'D')
2738 let buf = RunVimInTerminal('-S XleaveUnlet', {'rows': 10})
2739
2740 " this was using freed memory
2741 call term_sendkeys(buf, ":let g:\<CR>")
2742 call TermWait(buf, 50)
2743 call term_sendkeys(buf, "G")
2744 call TermWait(buf, 50)
2745 call term_sendkeys(buf, "\<CR>") " for the hit-enter prompt
2746
2747 call StopVimInTerminal(buf)
2748endfunc
2749
Bram Moolenaarb7407d32018-02-03 17:36:27 +01002750function s:Before_test_dirchanged()
2751 augroup test_dirchanged
2752 autocmd!
2753 augroup END
2754 let s:li = []
2755 let s:dir_this = getcwd()
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02002756 let s:dir_foo = s:dir_this . '/Xfoo'
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01002757 call mkdir(s:dir_foo)
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02002758 let s:dir_bar = s:dir_this . '/Xbar'
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01002759 call mkdir(s:dir_bar)
Bram Moolenaarb7407d32018-02-03 17:36:27 +01002760endfunc
2761
2762function s:After_test_dirchanged()
Bram Moolenaar3503d7c2019-11-09 20:10:17 +01002763 call chdir(s:dir_this)
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01002764 call delete(s:dir_foo, 'd')
2765 call delete(s:dir_bar, 'd')
Bram Moolenaarb7407d32018-02-03 17:36:27 +01002766 augroup test_dirchanged
2767 autocmd!
2768 augroup END
2769endfunc
2770
2771function Test_dirchanged_global()
2772 call s:Before_test_dirchanged()
Bram Moolenaarf6246f52022-02-11 16:30:12 +00002773 autocmd test_dirchanged DirChangedPre global call add(s:li, expand("<amatch>") .. " pre cd " .. v:event.directory)
Bram Moolenaarb7407d32018-02-03 17:36:27 +01002774 autocmd test_dirchanged DirChanged global call add(s:li, "cd:")
2775 autocmd test_dirchanged DirChanged global call add(s:li, expand("<afile>"))
Bram Moolenaar3503d7c2019-11-09 20:10:17 +01002776 call chdir(s:dir_foo)
Bram Moolenaarf6246f52022-02-11 16:30:12 +00002777 let expected = ["global pre cd " .. s:dir_foo, "cd:", s:dir_foo]
Bram Moolenaar28e8f732022-02-09 12:58:20 +00002778 call assert_equal(expected, s:li)
Bram Moolenaar3503d7c2019-11-09 20:10:17 +01002779 call chdir(s:dir_foo)
Bram Moolenaar28e8f732022-02-09 12:58:20 +00002780 call assert_equal(expected, s:li)
Bram Moolenaar3503d7c2019-11-09 20:10:17 +01002781 exe 'lcd ' .. fnameescape(s:dir_bar)
Bram Moolenaar28e8f732022-02-09 12:58:20 +00002782 call assert_equal(expected, s:li)
Bram Moolenaard8c9d322022-06-12 11:49:16 +01002783
2784 exe 'cd ' .. s:dir_foo
2785 exe 'cd ' .. s:dir_bar
2786 autocmd! test_dirchanged DirChanged global let g:result = expand("<afile>")
2787 cd -
Bram Moolenaardb77c492022-06-12 23:26:50 +01002788 call assert_equal(s:dir_foo, substitute(g:result, '\\', '/', 'g'))
Bram Moolenaard8c9d322022-06-12 11:49:16 +01002789
Bram Moolenaarb7407d32018-02-03 17:36:27 +01002790 call s:After_test_dirchanged()
2791endfunc
2792
2793function Test_dirchanged_local()
2794 call s:Before_test_dirchanged()
2795 autocmd test_dirchanged DirChanged window call add(s:li, "lcd:")
2796 autocmd test_dirchanged DirChanged window call add(s:li, expand("<afile>"))
Bram Moolenaar3503d7c2019-11-09 20:10:17 +01002797 call chdir(s:dir_foo)
Bram Moolenaarb7407d32018-02-03 17:36:27 +01002798 call assert_equal([], s:li)
Bram Moolenaar3503d7c2019-11-09 20:10:17 +01002799 exe 'lcd ' .. fnameescape(s:dir_bar)
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01002800 call assert_equal(["lcd:", s:dir_bar], s:li)
Bram Moolenaar3503d7c2019-11-09 20:10:17 +01002801 exe 'lcd ' .. fnameescape(s:dir_bar)
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01002802 call assert_equal(["lcd:", s:dir_bar], s:li)
Bram Moolenaarb7407d32018-02-03 17:36:27 +01002803 call s:After_test_dirchanged()
2804endfunc
2805
2806function Test_dirchanged_auto()
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +02002807 CheckOption autochdir
Bram Moolenaarb7407d32018-02-03 17:36:27 +01002808 call s:Before_test_dirchanged()
2809 call test_autochdir()
Bram Moolenaar28e8f732022-02-09 12:58:20 +00002810 autocmd test_dirchanged DirChangedPre auto call add(s:li, "pre cd " .. v:event.directory)
Bram Moolenaarb7407d32018-02-03 17:36:27 +01002811 autocmd test_dirchanged DirChanged auto call add(s:li, "auto:")
2812 autocmd test_dirchanged DirChanged auto call add(s:li, expand("<afile>"))
2813 set acd
Bram Moolenaar3503d7c2019-11-09 20:10:17 +01002814 cd ..
Bram Moolenaarb7407d32018-02-03 17:36:27 +01002815 call assert_equal([], s:li)
Bram Moolenaar61abe7d2022-08-30 21:46:08 +01002816 exe 'edit ' . s:dir_foo . '/Xautofile'
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01002817 call assert_equal(s:dir_foo, getcwd())
Bram Moolenaar28e8f732022-02-09 12:58:20 +00002818 let expected = ["pre cd " .. s:dir_foo, "auto:", s:dir_foo]
2819 call assert_equal(expected, s:li)
Bram Moolenaarb7407d32018-02-03 17:36:27 +01002820 set noacd
2821 bwipe!
2822 call s:After_test_dirchanged()
2823endfunc
Bram Moolenaar5a093432018-02-10 18:15:19 +01002824
2825" Test TextChangedI and TextChangedP
2826func Test_ChangedP()
2827 new
2828 call setline(1, ['foo', 'bar', 'foobar'])
2829 call test_override("char_avail", 1)
2830 set complete=. completeopt=menuone
2831
2832 func! TextChangedAutocmd(char)
2833 let g:autocmd .= a:char
2834 endfunc
2835
Christian Brabandtdb3b4462021-10-16 11:58:55 +01002836 " TextChanged will not be triggered, only check that it isn't.
Bram Moolenaar5a093432018-02-10 18:15:19 +01002837 au! TextChanged <buffer> :call TextChangedAutocmd('N')
2838 au! TextChangedI <buffer> :call TextChangedAutocmd('I')
2839 au! TextChangedP <buffer> :call TextChangedAutocmd('P')
2840
2841 call cursor(3, 1)
2842 let g:autocmd = ''
2843 call feedkeys("o\<esc>", 'tnix')
Evgeni Chasnovskid7ae2632023-10-15 09:59:00 +02002844 call assert_equal('I', g:autocmd)
Bram Moolenaar5a093432018-02-10 18:15:19 +01002845
2846 let g:autocmd = ''
Christian Brabandt4bca4892023-10-27 19:26:49 +02002847 call feedkeys("Sf", 'tnix')
2848 call assert_equal('II', g:autocmd)
2849
2850 let g:autocmd = ''
Bram Moolenaar5a093432018-02-10 18:15:19 +01002851 call feedkeys("Sf\<C-N>", 'tnix')
Christian Brabandt4bca4892023-10-27 19:26:49 +02002852 call assert_equal('IIP', g:autocmd)
Bram Moolenaar5a093432018-02-10 18:15:19 +01002853
2854 let g:autocmd = ''
2855 call feedkeys("Sf\<C-N>\<C-N>", 'tnix')
Christian Brabandt4bca4892023-10-27 19:26:49 +02002856 call assert_equal('IIPP', g:autocmd)
Bram Moolenaar5a093432018-02-10 18:15:19 +01002857
2858 let g:autocmd = ''
2859 call feedkeys("Sf\<C-N>\<C-N>\<C-N>", 'tnix')
Christian Brabandt4bca4892023-10-27 19:26:49 +02002860 call assert_equal('IIPPP', g:autocmd)
Bram Moolenaar5a093432018-02-10 18:15:19 +01002861
2862 let g:autocmd = ''
2863 call feedkeys("Sf\<C-N>\<C-N>\<C-N>\<C-N>", 'tnix')
Christian Brabandt4bca4892023-10-27 19:26:49 +02002864 call assert_equal('IIPPPP', g:autocmd)
Bram Moolenaar5a093432018-02-10 18:15:19 +01002865
2866 call assert_equal(['foo', 'bar', 'foobar', 'foo'], getline(1, '$'))
2867 " TODO: how should it handle completeopt=noinsert,noselect?
2868
2869 " CleanUp
2870 call test_override("char_avail", 0)
2871 au! TextChanged
2872 au! TextChangedI
2873 au! TextChangedP
2874 delfu TextChangedAutocmd
2875 unlet! g:autocmd
2876 set complete&vim completeopt&vim
2877
2878 bw!
2879endfunc
Bram Moolenaar8c64a362018-03-23 22:39:31 +01002880
Bram Moolenaar91d2e782018-08-07 19:05:01 +02002881let g:setline_handled = v:false
Bram Moolenaar1e115362019-01-09 23:01:02 +01002882func SetLineOne()
Bram Moolenaar91d2e782018-08-07 19:05:01 +02002883 if !g:setline_handled
2884 call setline(1, "(x)")
2885 let g:setline_handled = v:true
2886 endif
2887endfunc
2888
2889func Test_TextChangedI_with_setline()
2890 new
2891 call test_override('char_avail', 1)
2892 autocmd TextChangedI <buffer> call SetLineOne()
2893 call feedkeys("i(\<CR>\<Esc>", 'tx')
2894 call assert_equal('(', getline(1))
2895 call assert_equal('x)', getline(2))
2896 undo
Bram Moolenaar91d2e782018-08-07 19:05:01 +02002897 call assert_equal('', getline(1))
Bram Moolenaar9fa95062018-08-08 22:08:32 +02002898 call assert_equal('', getline(2))
Bram Moolenaar91d2e782018-08-07 19:05:01 +02002899
Bram Moolenaarca34db32022-01-20 11:17:18 +00002900 call test_override('char_avail', 0)
Bram Moolenaar91d2e782018-08-07 19:05:01 +02002901 bwipe!
2902endfunc
2903
Christian Brabandtc9e79e52024-02-09 19:34:36 +01002904func Test_TextChanged_with_norm()
2905 " For unknown reason this fails on MS-Windows
2906 CheckNotMSWindows
2907 CheckFeature terminal
2908 let buf = term_start([GetVimProg(), '--clean', '-c', 'set noswapfile'], {'term_rows': 3})
2909 call assert_equal('running', term_getstatus(buf))
2910 call term_sendkeys(buf, ":let g:a=0\<cr>")
2911 call term_wait(buf, 50)
2912 call term_sendkeys(buf, ":au! TextChanged * :let g:a+=1\<cr>")
2913 call term_wait(buf, 50)
2914 call term_sendkeys(buf, ":norm! ia\<cr>")
2915 call term_wait(buf, 50)
2916 call term_sendkeys(buf, ":echo g:a\<cr>")
2917 call term_wait(buf, 50)
2918 call WaitForAssert({-> assert_match('^1.*$', term_getline(buf, 3))})
2919 bwipe!
2920endfunc
2921
Bram Moolenaar8c64a362018-03-23 22:39:31 +01002922func Test_Changed_FirstTime()
Bram Moolenaar8c5a2782019-08-07 23:07:07 +02002923 CheckFeature terminal
2924 CheckNotGui
Bram Moolenaar3cdcb092020-03-18 19:18:10 +01002925 " Starting a terminal to run Vim is always considered flaky.
Bram Moolenaar30d53e22020-03-18 21:10:44 +01002926 let g:test_is_flaky = 1
Bram Moolenaar8c5a2782019-08-07 23:07:07 +02002927
Bram Moolenaar8c64a362018-03-23 22:39:31 +01002928 " Prepare file for TextChanged event.
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01002929 call writefile([''], 'Xchanged.txt', 'D')
Bram Moolenaar8c64a362018-03-23 22:39:31 +01002930 let buf = term_start([GetVimProg(), '--clean', '-c', 'set noswapfile'], {'term_rows': 3})
2931 call assert_equal('running', term_getstatus(buf))
Bram Moolenaar1834d372018-03-29 17:40:46 +02002932 " Wait for the ruler (in the status line) to be shown.
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01002933 " In ConPTY, there is additional character which is drawn up to the width of
2934 " the screen.
2935 if has('conpty')
2936 call WaitForAssert({-> assert_match('\<All.*$', term_getline(buf, 3))})
2937 else
2938 call WaitForAssert({-> assert_match('\<All$', term_getline(buf, 3))})
2939 endif
Bram Moolenaar8c64a362018-03-23 22:39:31 +01002940 " It's only adding autocmd, so that no event occurs.
2941 call term_sendkeys(buf, ":au! TextChanged <buffer> call writefile(['No'], 'Xchanged.txt')\<cr>")
2942 call term_sendkeys(buf, "\<C-\\>\<C-N>:qa!\<cr>")
Bram Moolenaar50182fa2018-04-28 21:34:40 +02002943 call WaitForAssert({-> assert_equal('finished', term_getstatus(buf))})
Bram Moolenaar8c64a362018-03-23 22:39:31 +01002944 call assert_equal([''], readfile('Xchanged.txt'))
2945
2946 " clean up
Bram Moolenaar8c64a362018-03-23 22:39:31 +01002947 bwipe!
2948endfunc
Bram Moolenaar0566e892019-01-24 19:37:40 +01002949
Bram Moolenaareb93f3f2019-04-04 15:04:56 +02002950func Test_autocmd_nested()
2951 let g:did_nested = 0
Christian Brabandtfb3f9692024-08-11 20:09:17 +02002952 defer CleanUpTestAuGroup()
2953 augroup testing
Bram Moolenaareb93f3f2019-04-04 15:04:56 +02002954 au WinNew * edit somefile
2955 au BufNew * let g:did_nested = 1
2956 augroup END
2957 split
2958 call assert_equal(0, g:did_nested)
2959 close
2960 bwipe! somefile
2961
2962 " old nested argument still works
Christian Brabandtfb3f9692024-08-11 20:09:17 +02002963 augroup testing
Bram Moolenaareb93f3f2019-04-04 15:04:56 +02002964 au!
2965 au WinNew * nested edit somefile
2966 au BufNew * let g:did_nested = 1
2967 augroup END
2968 split
2969 call assert_equal(1, g:did_nested)
2970 close
2971 bwipe! somefile
2972
2973 " New ++nested argument works
2974 augroup Testing
2975 au!
2976 au WinNew * ++nested edit somefile
2977 au BufNew * let g:did_nested = 1
2978 augroup END
2979 split
2980 call assert_equal(1, g:did_nested)
2981 close
2982 bwipe! somefile
2983
Bram Moolenaarf0775142022-03-04 20:10:38 +00002984 " nested without ++ does not work in Vim9 script
2985 call assert_fails('vim9cmd au WinNew * nested echo fails', 'E1078:')
2986
Bram Moolenaareb93f3f2019-04-04 15:04:56 +02002987 augroup Testing
2988 au!
2989 augroup END
2990
2991 call assert_fails('au WinNew * ++nested ++nested echo bad', 'E983:')
2992 call assert_fails('au WinNew * nested nested echo bad', 'E983:')
2993endfunc
2994
Bram Moolenaar5fa9f232022-07-23 09:06:48 +01002995func Test_autocmd_nested_cursor_invalid()
2996 set laststatus=0
2997 copen
2998 cclose
2999 call setline(1, ['foo', 'bar', 'baz'])
3000 3
3001 augroup nested_inv
3002 autocmd User foo ++nested copen
3003 autocmd BufAdd * let &laststatus = 2 - &laststatus
3004 augroup END
3005 doautocmd User foo
3006
3007 augroup nested_inv
3008 au!
3009 augroup END
3010 set laststatus&
Bram Moolenaarb03950f2022-07-26 13:47:13 +01003011 cclose
Bram Moolenaar5fa9f232022-07-23 09:06:48 +01003012 bwipe!
3013endfunc
3014
Bram Moolenaar3d6ee8b2022-07-27 15:23:35 +01003015func Test_autocmd_nested_keeps_cursor_pos()
3016 enew
3017 call setline(1, 'foo')
3018 autocmd User foo ++nested normal! $a
3019 autocmd InsertLeave * :
3020 doautocmd User foo
3021 call assert_equal([0, 1, 3, 0], getpos('.'))
3022
3023 bwipe!
3024endfunc
3025
Bram Moolenaarb03950f2022-07-26 13:47:13 +01003026func Test_autocmd_nested_switch_window()
3027 " run this in a separate Vim so that SafeState works
3028 CheckRunVimInTerminal
Drew Vogelea67ba72025-05-07 22:05:17 +02003029 CheckScreendump
Bram Moolenaarb03950f2022-07-26 13:47:13 +01003030
3031 let lines =<< trim END
3032 vim9script
3033 ['()']->writefile('Xautofile')
3034 autocmd VimEnter * ++nested edit Xautofile | split
3035 autocmd BufReadPost * autocmd SafeState * ++once foldclosed('.')
3036 autocmd WinEnter * matchadd('ErrorMsg', 'pat')
3037 END
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01003038 call writefile(lines, 'Xautoscript', 'D')
Bram Moolenaarb03950f2022-07-26 13:47:13 +01003039 let buf = RunVimInTerminal('-S Xautoscript', {'rows': 10})
3040 call VerifyScreenDump(buf, 'Test_autocmd_nested_switch', {})
3041
3042 call StopVimInTerminal(buf)
3043 call delete('Xautofile')
Bram Moolenaarb03950f2022-07-26 13:47:13 +01003044endfunc
3045
Bram Moolenaareb93f3f2019-04-04 15:04:56 +02003046func Test_autocmd_once()
3047 " Without ++once WinNew triggers twice
3048 let g:did_split = 0
3049 augroup Testing
3050 au WinNew * let g:did_split += 1
3051 augroup END
3052 split
3053 split
3054 call assert_equal(2, g:did_split)
3055 call assert_true(exists('#WinNew'))
3056 close
3057 close
3058
3059 " With ++once WinNew triggers once
3060 let g:did_split = 0
3061 augroup Testing
3062 au!
3063 au WinNew * ++once let g:did_split += 1
3064 augroup END
3065 split
3066 split
3067 call assert_equal(1, g:did_split)
3068 call assert_false(exists('#WinNew'))
3069 close
3070 close
3071
3072 call assert_fails('au WinNew * ++once ++once echo bad', 'E983:')
3073endfunc
3074
Bram Moolenaara68e5952019-04-25 22:22:01 +02003075func Test_autocmd_bufreadpre()
3076 new
3077 let b:bufreadpre = 1
Bram Moolenaarab505b12020-03-23 19:28:44 +01003078 call append(0, range(1000))
Bram Moolenaara68e5952019-04-25 22:22:01 +02003079 w! XAutocmdBufReadPre.txt
3080 autocmd BufReadPre <buffer> :let b:bufreadpre += 1
Bram Moolenaarab505b12020-03-23 19:28:44 +01003081 norm! 500gg
Bram Moolenaara68e5952019-04-25 22:22:01 +02003082 sp
Bram Moolenaarab505b12020-03-23 19:28:44 +01003083 norm! 1000gg
Bram Moolenaara68e5952019-04-25 22:22:01 +02003084 wincmd p
3085 let g:wsv1 = winsaveview()
3086 wincmd p
3087 let g:wsv2 = winsaveview()
3088 " triggers BufReadPre, should not move the cursor in either window
3089 " The topline may change one line in a large window.
3090 edit
3091 call assert_inrange(g:wsv2.topline - 1, g:wsv2.topline + 1, winsaveview().topline)
3092 call assert_equal(g:wsv2.lnum, winsaveview().lnum)
3093 call assert_equal(2, b:bufreadpre)
3094 wincmd p
3095 call assert_equal(g:wsv1.topline, winsaveview().topline)
3096 call assert_equal(g:wsv1.lnum, winsaveview().lnum)
3097 call assert_equal(2, b:bufreadpre)
3098 " Now set the cursor position in an BufReadPre autocommand
3099 " (even though the position will be invalid, this should make Vim reset the
3100 " cursor position in the other window.
3101 wincmd p
3102 set cpo+=g
3103 " won't do anything, but try to set the cursor on an invalid lnum
3104 autocmd BufReadPre <buffer> :norm! 70gg
3105 " triggers BufReadPre, should not move the cursor in either window
3106 e
3107 call assert_equal(1, winsaveview().topline)
3108 call assert_equal(1, winsaveview().lnum)
3109 call assert_equal(3, b:bufreadpre)
3110 wincmd p
3111 call assert_equal(g:wsv1.topline, winsaveview().topline)
3112 call assert_equal(g:wsv1.lnum, winsaveview().lnum)
3113 call assert_equal(3, b:bufreadpre)
3114 close
3115 close
3116 call delete('XAutocmdBufReadPre.txt')
3117 set cpo-=g
3118endfunc
3119
Bram Moolenaar5e66b422019-01-24 21:58:10 +01003120" FileChangedShell tested in test_filechanged.vim
Bram Moolenaar69ea5872019-04-25 20:29:00 +02003121
3122" Tests for the following autocommands:
3123" - FileWritePre writing a compressed file
3124" - FileReadPost reading a compressed file
3125" - BufNewFile reading a file template
3126" - BufReadPre decompressing the file to be read
3127" - FilterReadPre substituting characters in the temp file
3128" - FilterReadPost substituting characters after filtering
3129" - FileReadPre set options for decompression
3130" - FileReadPost decompress the file
3131func Test_ReadWrite_Autocmds()
3132 " Run this test only on Unix-like systems and if gzip is available
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +02003133 CheckUnix
3134 CheckExecutable gzip
Bram Moolenaar69ea5872019-04-25 20:29:00 +02003135
3136 " Make $GZIP empty, "-v" would cause trouble.
3137 let $GZIP = ""
3138
3139 " Use a FileChangedShell autocommand to avoid a prompt for 'Xtestfile.gz'
3140 " being modified outside of Vim (noticed on Solaris).
3141 au FileChangedShell * echo 'caught FileChangedShell'
3142
3143 " Test for the FileReadPost, FileWritePre and FileWritePost autocmds
3144 augroup Test1
3145 au!
3146 au FileWritePre *.gz '[,']!gzip
3147 au FileWritePost *.gz undo
3148 au FileReadPost *.gz '[,']!gzip -d
3149 augroup END
3150
3151 new
3152 set bin
3153 call append(0, [
3154 \ 'line 2 Abcdefghijklmnopqrstuvwxyz',
3155 \ 'line 3 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
3156 \ 'line 4 Abcdefghijklmnopqrstuvwxyz',
3157 \ 'line 5 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
3158 \ 'line 6 Abcdefghijklmnopqrstuvwxyz',
3159 \ 'line 7 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
3160 \ 'line 8 Abcdefghijklmnopqrstuvwxyz',
3161 \ 'line 9 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
3162 \ 'line 10 Abcdefghijklmnopqrstuvwxyz'
3163 \ ])
3164 1,9write! Xtestfile.gz
3165 enew! | close
3166
3167 new
3168 " Read and decompress the testfile
3169 0read Xtestfile.gz
3170 call assert_equal([
3171 \ 'line 2 Abcdefghijklmnopqrstuvwxyz',
3172 \ 'line 3 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
3173 \ 'line 4 Abcdefghijklmnopqrstuvwxyz',
3174 \ 'line 5 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
3175 \ 'line 6 Abcdefghijklmnopqrstuvwxyz',
3176 \ 'line 7 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
3177 \ 'line 8 Abcdefghijklmnopqrstuvwxyz',
3178 \ 'line 9 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
3179 \ 'line 10 Abcdefghijklmnopqrstuvwxyz'
3180 \ ], getline(1, 9))
3181 enew! | close
3182
3183 augroup Test1
3184 au!
3185 augroup END
3186
3187 " Test for the FileAppendPre and FileAppendPost autocmds
3188 augroup Test2
3189 au!
3190 au BufNewFile *.c read Xtest.c
3191 au FileAppendPre *.out '[,']s/new/NEW/
3192 au FileAppendPost *.out !cat Xtest.c >> test.out
3193 augroup END
3194
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01003195 call writefile(['/*', ' * Here is a new .c file', ' */'], 'Xtest.c', 'D')
Bram Moolenaar69ea5872019-04-25 20:29:00 +02003196 new foo.c " should load Xtest.c
3197 call assert_equal(['/*', ' * Here is a new .c file', ' */'], getline(2, 4))
3198 w! >> test.out " append it to the output file
3199
3200 let contents = readfile('test.out')
3201 call assert_equal(' * Here is a NEW .c file', contents[2])
3202 call assert_equal(' * Here is a new .c file', contents[5])
3203
3204 call delete('test.out')
3205 enew! | close
3206 augroup Test2
3207 au!
3208 augroup END
3209
3210 " Test for the BufReadPre and BufReadPost autocmds
3211 augroup Test3
3212 au!
3213 " setup autocommands to decompress before reading and re-compress
3214 " afterwards
3215 au BufReadPre *.gz exe '!gzip -d ' . shellescape(expand("<afile>"))
3216 au BufReadPre *.gz call rename(expand("<afile>:r"), expand("<afile>"))
3217 au BufReadPost *.gz call rename(expand("<afile>"), expand("<afile>:r"))
3218 au BufReadPost *.gz exe '!gzip ' . shellescape(expand("<afile>:r"))
3219 augroup END
3220
3221 e! Xtestfile.gz " Edit compressed file
3222 call assert_equal([
3223 \ 'line 2 Abcdefghijklmnopqrstuvwxyz',
3224 \ 'line 3 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
3225 \ 'line 4 Abcdefghijklmnopqrstuvwxyz',
3226 \ 'line 5 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
3227 \ 'line 6 Abcdefghijklmnopqrstuvwxyz',
3228 \ 'line 7 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
3229 \ 'line 8 Abcdefghijklmnopqrstuvwxyz',
3230 \ 'line 9 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
3231 \ 'line 10 Abcdefghijklmnopqrstuvwxyz'
3232 \ ], getline(1, 9))
3233
3234 w! >> test.out " Append it to the output file
3235
3236 augroup Test3
3237 au!
3238 augroup END
3239
3240 " Test for the FilterReadPre and FilterReadPost autocmds.
3241 set shelltemp " need temp files here
3242 augroup Test4
3243 au!
3244 au FilterReadPre *.out call rename(expand("<afile>"), expand("<afile>") . ".t")
3245 au FilterReadPre *.out exe 'silent !sed s/e/E/ ' . shellescape(expand("<afile>")) . ".t >" . shellescape(expand("<afile>"))
3246 au FilterReadPre *.out exe 'silent !rm ' . shellescape(expand("<afile>")) . '.t'
3247 au FilterReadPost *.out '[,']s/x/X/g
3248 augroup END
3249
3250 e! test.out " Edit the output file
3251 1,$!cat
3252 call assert_equal([
3253 \ 'linE 2 AbcdefghijklmnopqrstuvwXyz',
3254 \ 'linE 3 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
3255 \ 'linE 4 AbcdefghijklmnopqrstuvwXyz',
3256 \ 'linE 5 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
3257 \ 'linE 6 AbcdefghijklmnopqrstuvwXyz',
3258 \ 'linE 7 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
3259 \ 'linE 8 AbcdefghijklmnopqrstuvwXyz',
3260 \ 'linE 9 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
3261 \ 'linE 10 AbcdefghijklmnopqrstuvwXyz'
3262 \ ], getline(1, 9))
3263 call assert_equal([
3264 \ 'line 2 Abcdefghijklmnopqrstuvwxyz',
3265 \ 'line 3 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
3266 \ 'line 4 Abcdefghijklmnopqrstuvwxyz',
3267 \ 'line 5 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
3268 \ 'line 6 Abcdefghijklmnopqrstuvwxyz',
3269 \ 'line 7 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
3270 \ 'line 8 Abcdefghijklmnopqrstuvwxyz',
3271 \ 'line 9 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
3272 \ 'line 10 Abcdefghijklmnopqrstuvwxyz'
3273 \ ], readfile('test.out'))
3274
3275 augroup Test4
3276 au!
3277 augroup END
3278 set shelltemp&vim
3279
3280 " Test for the FileReadPre and FileReadPost autocmds.
3281 augroup Test5
3282 au!
3283 au FileReadPre *.gz exe 'silent !gzip -d ' . shellescape(expand("<afile>"))
3284 au FileReadPre *.gz call rename(expand("<afile>:r"), expand("<afile>"))
3285 au FileReadPost *.gz '[,']s/l/L/
3286 augroup END
3287
3288 new
3289 0r Xtestfile.gz " Read compressed file
3290 call assert_equal([
3291 \ 'Line 2 Abcdefghijklmnopqrstuvwxyz',
3292 \ 'Line 3 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
3293 \ 'Line 4 Abcdefghijklmnopqrstuvwxyz',
3294 \ 'Line 5 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
3295 \ 'Line 6 Abcdefghijklmnopqrstuvwxyz',
3296 \ 'Line 7 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
3297 \ 'Line 8 Abcdefghijklmnopqrstuvwxyz',
3298 \ 'Line 9 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
3299 \ 'Line 10 Abcdefghijklmnopqrstuvwxyz'
3300 \ ], getline(1, 9))
3301 call assert_equal([
3302 \ 'line 2 Abcdefghijklmnopqrstuvwxyz',
3303 \ 'line 3 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
3304 \ 'line 4 Abcdefghijklmnopqrstuvwxyz',
3305 \ 'line 5 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
3306 \ 'line 6 Abcdefghijklmnopqrstuvwxyz',
3307 \ 'line 7 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
3308 \ 'line 8 Abcdefghijklmnopqrstuvwxyz',
3309 \ 'line 9 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
3310 \ 'line 10 Abcdefghijklmnopqrstuvwxyz'
3311 \ ], readfile('Xtestfile.gz'))
3312
3313 augroup Test5
3314 au!
3315 augroup END
3316
3317 au! FileChangedShell
3318 call delete('Xtestfile.gz')
Bram Moolenaar69ea5872019-04-25 20:29:00 +02003319 call delete('test.out')
3320endfunc
Bram Moolenaar23b51392019-05-09 21:38:43 +02003321
3322func Test_throw_in_BufWritePre()
3323 new
3324 call setline(1, ['one', 'two', 'three'])
3325 call assert_false(filereadable('Xthefile'))
3326 augroup throwing
3327 au BufWritePre X* throw 'do not write'
3328 augroup END
3329 try
3330 w Xthefile
3331 catch
3332 let caught = 1
3333 endtry
3334 call assert_equal(1, caught)
3335 call assert_false(filereadable('Xthefile'))
3336
3337 bwipe!
3338 au! throwing
3339endfunc
Bram Moolenaarcadbe1b2019-09-22 21:50:09 +02003340
Bram Moolenaar40fa12a2021-09-22 14:18:13 +02003341func Test_autocmd_in_try_block()
Bram Moolenaar6f14da12022-09-07 21:30:44 +01003342 call mkdir('Xintrydir', 'R')
Bram Moolenaar40fa12a2021-09-22 14:18:13 +02003343 au BufEnter * let g:fname = expand('%')
3344 try
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +01003345 edit Xintrydir/
Bram Moolenaar40fa12a2021-09-22 14:18:13 +02003346 endtry
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +01003347 call assert_match('Xintrydir', g:fname)
Bram Moolenaar40fa12a2021-09-22 14:18:13 +02003348
3349 unlet g:fname
3350 au! BufEnter
Bram Moolenaar40fa12a2021-09-22 14:18:13 +02003351endfunc
3352
Bram Moolenaarcadbe1b2019-09-22 21:50:09 +02003353func Test_autocmd_SafeState()
3354 CheckRunVimInTerminal
3355
3356 let lines =<< trim END
3357 let g:safe = 0
3358 let g:again = ''
3359 au SafeState * let g:safe += 1
3360 au SafeStateAgain * let g:again ..= 'x'
3361 func CallTimer()
3362 call timer_start(10, {id -> execute('let g:again ..= "t"')})
3363 endfunc
3364 END
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01003365 call writefile(lines, 'XSafeState', 'D')
Bram Moolenaarcadbe1b2019-09-22 21:50:09 +02003366 let buf = RunVimInTerminal('-S XSafeState', #{rows: 6})
3367
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01003368 " Sometimes we loop to handle a K_IGNORE, SafeState may be triggered once or
Bram Moolenaar8fb1b472020-02-23 16:16:26 +01003369 " more often.
Bram Moolenaarcadbe1b2019-09-22 21:50:09 +02003370 call term_sendkeys(buf, ":echo g:safe\<CR>")
Bram Moolenaar8fb1b472020-02-23 16:16:26 +01003371 call WaitForAssert({-> assert_match('^\d ', term_getline(buf, 6))}, 1000)
Bram Moolenaarcadbe1b2019-09-22 21:50:09 +02003372
Bram Moolenaar8fb1b472020-02-23 16:16:26 +01003373 " SafeStateAgain should be invoked at least three times
Bram Moolenaarcadbe1b2019-09-22 21:50:09 +02003374 call term_sendkeys(buf, ":echo g:again\<CR>")
Bram Moolenaar8fb1b472020-02-23 16:16:26 +01003375 call WaitForAssert({-> assert_match('^xxx', term_getline(buf, 6))}, 1000)
Bram Moolenaarcadbe1b2019-09-22 21:50:09 +02003376
3377 call term_sendkeys(buf, ":let g:again = ''\<CR>:call CallTimer()\<CR>")
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02003378 call TermWait(buf, 50)
Bram Moolenaar0f6629a2019-09-22 23:24:13 +02003379 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02003380 call TermWait(buf, 50)
Bram Moolenaarcadbe1b2019-09-22 21:50:09 +02003381 call term_sendkeys(buf, ":echo g:again\<CR>")
3382 call WaitForAssert({-> assert_match('xtx', term_getline(buf, 6))}, 1000)
3383
3384 call StopVimInTerminal(buf)
Bram Moolenaarcadbe1b2019-09-22 21:50:09 +02003385endfunc
Bram Moolenaar23324a02019-10-01 17:39:04 +02003386
3387func Test_autocmd_CmdWinEnter()
3388 CheckRunVimInTerminal
Bram Moolenaar21829c52021-01-26 22:42:21 +01003389
Bram Moolenaar23324a02019-10-01 17:39:04 +02003390 let lines =<< trim END
Egor Zvorykin125ffd22021-11-17 14:01:14 +00003391 augroup vimHints | au! | augroup END
Bram Moolenaar23324a02019-10-01 17:39:04 +02003392 let b:dummy_var = 'This is a dummy'
3393 autocmd CmdWinEnter * quit
3394 let winnr = winnr('$')
3395 END
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01003396 let filename = 'XCmdWinEnter'
Bram Moolenaar23324a02019-10-01 17:39:04 +02003397 call writefile(lines, filename)
3398 let buf = RunVimInTerminal('-S '.filename, #{rows: 6})
3399
3400 call term_sendkeys(buf, "q:")
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02003401 call TermWait(buf)
Bram Moolenaar23324a02019-10-01 17:39:04 +02003402 call term_sendkeys(buf, ":echo b:dummy_var\<cr>")
Bram Moolenaar353c3512020-03-15 14:19:26 +01003403 call WaitForAssert({-> assert_match('^This is a dummy', term_getline(buf, 6))}, 2000)
Bram Moolenaar23324a02019-10-01 17:39:04 +02003404 call term_sendkeys(buf, ":echo &buftype\<cr>")
3405 call WaitForAssert({-> assert_notmatch('^nofile', term_getline(buf, 6))}, 1000)
3406 call term_sendkeys(buf, ":echo winnr\<cr>")
3407 call WaitForAssert({-> assert_match('^1', term_getline(buf, 6))}, 1000)
3408
3409 " clean up
3410 call StopVimInTerminal(buf)
3411 call delete(filename)
3412endfunc
Bram Moolenaarec66c412019-10-11 21:19:13 +02003413
3414func Test_autocmd_was_using_freed_memory()
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01003415 CheckFeature quickfix
3416
Bram Moolenaarec66c412019-10-11 21:19:13 +02003417 pedit xx
3418 n x
Bram Moolenaar92bb83e2021-02-03 23:04:46 +01003419 augroup winenter
3420 au WinEnter * if winnr('$') > 2 | quit | endif
3421 augroup END
Bram Moolenaarec66c412019-10-11 21:19:13 +02003422 split
Bram Moolenaar92bb83e2021-02-03 23:04:46 +01003423
3424 augroup winenter
3425 au! WinEnter
3426 augroup END
3427
3428 bwipe xx
3429 bwipe x
3430 pclose
Bram Moolenaarec66c412019-10-11 21:19:13 +02003431endfunc
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +01003432
3433func Test_BufWrite_lockmarks()
Bram Moolenaarf08b0eb2021-10-16 13:00:14 +01003434 let g:test_is_flaky = 1
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +01003435 edit! Xtest
3436 call setline(1, ['a', 'b', 'c', 'd'])
3437
3438 " :lockmarks preserves the marks
3439 call SetChangeMarks(2, 3)
3440 lockmarks write
3441 call assert_equal([2, 3], [line("'["), line("']")])
3442
3443 " *WritePre autocmds get the correct line range, but lockmarks preserves the
3444 " original values for the user
3445 augroup lockmarks
3446 au!
3447 au BufWritePre,FilterWritePre * call assert_equal([1, 4], [line("'["), line("']")])
3448 au FileWritePre * call assert_equal([3, 4], [line("'["), line("']")])
3449 augroup END
3450
3451 lockmarks write
3452 call assert_equal([2, 3], [line("'["), line("']")])
3453
3454 if executable('cat')
3455 lockmarks %!cat
3456 call assert_equal([2, 3], [line("'["), line("']")])
3457 endif
3458
3459 lockmarks 3,4write Xtest2
3460 call assert_equal([2, 3], [line("'["), line("']")])
3461
3462 au! lockmarks
3463 augroup! lockmarks
3464 call delete('Xtest')
3465 call delete('Xtest2')
3466endfunc
Bram Moolenaarce6db022020-01-07 20:11:42 +01003467
3468func Test_FileType_spell()
3469 if !isdirectory('/tmp')
3470 throw "Skipped: requires /tmp directory"
3471 endif
3472
3473 " this was crashing with an invalid free()
3474 setglobal spellfile=/tmp/en.utf-8.add
3475 augroup crash
3476 autocmd!
3477 autocmd BufNewFile,BufReadPost crashfile setf somefiletype
3478 autocmd BufNewFile,BufReadPost crashfile set ft=anotherfiletype
3479 autocmd FileType anotherfiletype setlocal spell
3480 augroup END
3481 func! NoCrash() abort
3482 edit /tmp/crashfile
3483 endfunc
3484 call NoCrash()
3485
3486 au! crash
3487 setglobal spellfile=
3488endfunc
Bram Moolenaarbc2b71d2020-02-17 21:33:30 +01003489
Bram Moolenaaref976322022-09-28 11:48:30 +01003490" this was wiping out the current buffer and using freed memory
3491func Test_SpellFileMissing_bwipe()
3492 next 0
3493 au SpellFileMissing 0 bwipe
3494 call assert_fails('set spell spelllang=0', 'E937:')
3495
3496 au! SpellFileMissing
Bram Moolenaar0a60f792022-11-19 21:18:11 +00003497 set nospell spelllang=en
Bram Moolenaaref976322022-09-28 11:48:30 +01003498 bwipe
3499endfunc
3500
Bram Moolenaar406cd902020-02-18 21:54:41 +01003501" Test closing a window or editing another buffer from a FileChangedRO handler
3502" in a readonly buffer
3503func Test_FileChangedRO_winclose()
Bram Moolenaar62cd26a2020-10-11 20:08:44 +02003504 call test_override('ui_delay', 10)
3505
Bram Moolenaar406cd902020-02-18 21:54:41 +01003506 augroup FileChangedROTest
3507 au!
3508 autocmd FileChangedRO * quit
3509 augroup END
3510 new
3511 set readonly
3512 call assert_fails('normal i', 'E788:')
3513 close
3514 augroup! FileChangedROTest
3515
3516 augroup FileChangedROTest
3517 au!
Bram Moolenaar61abe7d2022-08-30 21:46:08 +01003518 autocmd FileChangedRO * edit Xrofile
Bram Moolenaar406cd902020-02-18 21:54:41 +01003519 augroup END
3520 new
3521 set readonly
3522 call assert_fails('normal i', 'E788:')
3523 close
3524 augroup! FileChangedROTest
Bram Moolenaar62cd26a2020-10-11 20:08:44 +02003525 call test_override('ALL', 0)
Bram Moolenaar406cd902020-02-18 21:54:41 +01003526endfunc
3527
Bram Moolenaar0c81d1b2020-02-22 22:45:55 +01003528func LogACmd()
3529 call add(g:logged, line('$'))
3530endfunc
3531
3532func Test_TermChanged()
Bram Moolenaard28e0b32020-02-22 23:08:52 +01003533 CheckNotGui
3534
Bram Moolenaar0c81d1b2020-02-22 22:45:55 +01003535 enew!
3536 tabnew
3537 call setline(1, ['a', 'b', 'c', 'd'])
3538 $
3539 au TermChanged * call LogACmd()
3540 let g:logged = []
3541 let term_save = &term
3542 set term=xterm
3543 call assert_equal([1, 4], g:logged)
3544
3545 au! TermChanged
3546 let &term = term_save
3547 bwipe!
3548endfunc
3549
Bram Moolenaare3284872020-03-19 13:55:03 +01003550" Test for FileReadCmd autocmd
3551func Test_autocmd_FileReadCmd()
3552 func ReadFileCmd()
3553 call append(line('$'), "v:cmdarg = " .. v:cmdarg)
3554 endfunc
3555 augroup FileReadCmdTest
3556 au!
3557 au FileReadCmd Xtest call ReadFileCmd()
3558 augroup END
3559
3560 new
3561 read ++bin Xtest
3562 read ++nobin Xtest
3563 read ++edit Xtest
3564 read ++bad=keep Xtest
3565 read ++bad=drop Xtest
3566 read ++bad=- Xtest
3567 read ++ff=unix Xtest
3568 read ++ff=dos Xtest
3569 read ++ff=mac Xtest
3570 read ++enc=utf-8 Xtest
3571
3572 call assert_equal(['',
3573 \ 'v:cmdarg = ++bin',
3574 \ 'v:cmdarg = ++nobin',
3575 \ 'v:cmdarg = ++edit',
3576 \ 'v:cmdarg = ++bad=keep',
3577 \ 'v:cmdarg = ++bad=drop',
3578 \ 'v:cmdarg = ++bad=-',
3579 \ 'v:cmdarg = ++ff=unix',
3580 \ 'v:cmdarg = ++ff=dos',
3581 \ 'v:cmdarg = ++ff=mac',
3582 \ 'v:cmdarg = ++enc=utf-8'], getline(1, '$'))
3583
Bram Moolenaar23526d22022-12-05 15:50:41 +00003584 bwipe!
Bram Moolenaare3284872020-03-19 13:55:03 +01003585 augroup FileReadCmdTest
3586 au!
3587 augroup END
3588 delfunc ReadFileCmd
3589endfunc
3590
Bram Moolenaaree4e0c12020-04-06 21:35:05 +02003591" Test for passing invalid arguments to autocmd
3592func Test_autocmd_invalid_args()
3593 " Additional character after * for event
Bram Moolenaar61abe7d2022-08-30 21:46:08 +01003594 call assert_fails('autocmd *a Xinvfile set ff=unix', 'E215:')
Bram Moolenaaree4e0c12020-04-06 21:35:05 +02003595 augroup Test
3596 augroup END
3597 " Invalid autocmd event
Bram Moolenaar61abe7d2022-08-30 21:46:08 +01003598 call assert_fails('autocmd Bufabc Xinvfile set ft=vim', 'E216:')
Bram Moolenaaree4e0c12020-04-06 21:35:05 +02003599 " Invalid autocmd event in a autocmd group
Bram Moolenaar61abe7d2022-08-30 21:46:08 +01003600 call assert_fails('autocmd Test Bufabc Xinvfile set ft=vim', 'E216:')
Bram Moolenaaree4e0c12020-04-06 21:35:05 +02003601 augroup! Test
3602 " Execute all autocmds
3603 call assert_fails('doautocmd * BufEnter', 'E217:')
3604 call assert_fails('augroup! x1a2b3', 'E367:')
3605 call assert_fails('autocmd BufNew <buffer=999> pwd', 'E680:')
Bram Moolenaar531be472020-09-23 22:38:05 +02003606 call assert_fails('autocmd BufNew \) set ff=unix', 'E55:')
Bram Moolenaaree4e0c12020-04-06 21:35:05 +02003607endfunc
3608
3609" Test for deep nesting of autocmds
3610func Test_autocmd_deep_nesting()
Bram Moolenaar61abe7d2022-08-30 21:46:08 +01003611 autocmd BufEnter Xdeepfile doautocmd BufEnter Xdeepfile
3612 call assert_fails('doautocmd BufEnter Xdeepfile', 'E218:')
3613 autocmd! BufEnter Xdeepfile
Bram Moolenaaree4e0c12020-04-06 21:35:05 +02003614endfunc
3615
Bram Moolenaarbe5ee862020-06-10 20:56:58 +02003616" Tests for SigUSR1 autocmd event, which is only available on posix systems.
3617func Test_autocmd_sigusr1()
3618 CheckUnix
Bram Moolenaar0056ca72022-09-23 21:26:39 +01003619 " FIXME: should this work on MacOS M1?
3620 CheckNotMacM1
Bram Moolenaar62cd26a2020-10-11 20:08:44 +02003621 CheckExecutable /bin/kill
Bram Moolenaarbe5ee862020-06-10 20:56:58 +02003622
3623 let g:sigusr1_passed = 0
3624 au SigUSR1 * let g:sigusr1_passed = 1
3625 call system('/bin/kill -s usr1 ' . getpid())
3626 call WaitForAssert({-> assert_true(g:sigusr1_passed)})
3627
3628 au! SigUSR1
3629 unlet g:sigusr1_passed
3630endfunc
3631
Bram Moolenaarb340bae2020-06-15 19:51:56 +02003632" Test for BufReadPre autocmd deleting the file
3633func Test_BufReadPre_delfile()
3634 augroup TestAuCmd
3635 au!
Bram Moolenaare7cda972022-08-29 11:02:59 +01003636 autocmd BufReadPre XbufreadPre call delete('XbufreadPre')
Bram Moolenaarb340bae2020-06-15 19:51:56 +02003637 augroup END
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01003638 call writefile([], 'XbufreadPre', 'D')
Bram Moolenaare7cda972022-08-29 11:02:59 +01003639 call assert_fails('new XbufreadPre', 'E200:')
3640 call assert_equal('XbufreadPre', @%)
Bram Moolenaarb340bae2020-06-15 19:51:56 +02003641 call assert_equal(1, &readonly)
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01003642
Bram Moolenaarb340bae2020-06-15 19:51:56 +02003643 augroup TestAuCmd
3644 au!
3645 augroup END
3646 close!
3647endfunc
3648
3649" Test for BufReadPre autocmd changing the current buffer
3650func Test_BufReadPre_changebuf()
3651 augroup TestAuCmd
3652 au!
Bram Moolenaare7cda972022-08-29 11:02:59 +01003653 autocmd BufReadPre Xchangebuf edit Xsomeotherfile
Bram Moolenaarb340bae2020-06-15 19:51:56 +02003654 augroup END
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01003655 call writefile([], 'Xchangebuf', 'D')
Bram Moolenaare7cda972022-08-29 11:02:59 +01003656 call assert_fails('new Xchangebuf', 'E201:')
Bram Moolenaarb340bae2020-06-15 19:51:56 +02003657 call assert_equal('Xsomeotherfile', @%)
3658 call assert_equal(1, &readonly)
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01003659
Bram Moolenaarb340bae2020-06-15 19:51:56 +02003660 augroup TestAuCmd
3661 au!
3662 augroup END
3663 close!
3664endfunc
3665
3666" Test for BufWipeouti autocmd changing the current buffer when reading a file
3667" in an empty buffer with 'f' flag in 'cpo'
3668func Test_BufDelete_changebuf()
3669 new
3670 augroup TestAuCmd
3671 au!
3672 autocmd BufWipeout * let bufnr = bufadd('somefile') | exe "b " .. bufnr
3673 augroup END
3674 let save_cpo = &cpo
3675 set cpo+=f
Bram Moolenaarb18b4962022-09-02 21:55:50 +01003676 call assert_fails('r Xchangebuf', ['E812:', 'E484:'])
Bram Moolenaarb340bae2020-06-15 19:51:56 +02003677 call assert_equal('somefile', @%)
3678 let &cpo = save_cpo
3679 augroup TestAuCmd
3680 au!
3681 augroup END
3682 close!
3683endfunc
3684
Bram Moolenaar0fe937f2020-06-16 22:42:04 +02003685" Test for the temporary internal window used to execute autocmds
3686func Test_autocmd_window()
3687 %bw!
3688 edit one.txt
3689 tabnew two.txt
Bram Moolenaar41cd8032021-03-13 15:47:56 +01003690 vnew three.txt
3691 tabnew four.txt
3692 tabprevious
Bram Moolenaar0fe937f2020-06-16 22:42:04 +02003693 let g:blist = []
Bram Moolenaar832adf92020-06-25 19:01:36 +02003694 augroup aucmd_win_test1
Bram Moolenaar0fe937f2020-06-16 22:42:04 +02003695 au!
3696 au BufEnter * call add(g:blist, [expand('<afile>'),
3697 \ win_gettype(bufwinnr(expand('<afile>')))])
3698 augroup END
3699
3700 doautoall BufEnter
Bram Moolenaar41cd8032021-03-13 15:47:56 +01003701 call assert_equal([
3702 \ ['one.txt', 'autocmd'],
3703 \ ['two.txt', ''],
3704 \ ['four.txt', 'autocmd'],
3705 \ ['three.txt', ''],
3706 \ ], g:blist)
Bram Moolenaar0fe937f2020-06-16 22:42:04 +02003707
Bram Moolenaar832adf92020-06-25 19:01:36 +02003708 augroup aucmd_win_test1
Bram Moolenaar0fe937f2020-06-16 22:42:04 +02003709 au!
3710 augroup END
Bram Moolenaar832adf92020-06-25 19:01:36 +02003711 augroup! aucmd_win_test1
3712 %bw!
3713endfunc
3714
3715" Test for trying to close the temporary window used for executing an autocmd
3716func Test_close_autocmd_window()
3717 %bw!
3718 edit one.txt
3719 tabnew two.txt
3720 augroup aucmd_win_test2
3721 au!
3722 au BufEnter * if expand('<afile>') == 'one.txt' | 1close | endif
3723 augroup END
3724
3725 call assert_fails('doautoall BufEnter', 'E813:')
3726
3727 augroup aucmd_win_test2
3728 au!
3729 augroup END
3730 augroup! aucmd_win_test2
Bram Moolenaarcf844172020-06-26 19:44:06 +02003731 %bwipe!
3732endfunc
3733
3734" Test for trying to close the tab that has the temporary window for exeucing
3735" an autocmd.
3736func Test_close_autocmd_tab()
3737 edit one.txt
3738 tabnew two.txt
3739 augroup aucmd_win_test
3740 au!
3741 au BufEnter * if expand('<afile>') == 'one.txt' | tabfirst | tabonly | endif
3742 augroup END
3743
3744 call assert_fails('doautoall BufEnter', 'E813:')
3745
3746 tabonly
3747 augroup aucmd_win_test
3748 au!
3749 augroup END
3750 augroup! aucmd_win_test
3751 %bwipe!
Bram Moolenaar0fe937f2020-06-16 22:42:04 +02003752endfunc
3753
Bram Moolenaarcb1956d2022-01-07 15:45:18 +00003754func Test_Visual_doautoall_redraw()
3755 call setline(1, ['a', 'b'])
Bram Moolenaar94722c52023-01-28 19:19:03 +00003756 new
Bram Moolenaarcb1956d2022-01-07 15:45:18 +00003757 wincmd p
3758 call feedkeys("G\<C-V>", 'txn')
3759 autocmd User Explode ++once redraw
3760 doautoall User Explode
3761 %bwipe!
3762endfunc
3763
Bram Moolenaar6bcb8772021-02-03 21:23:29 +01003764" This was using freed memory.
3765func Test_BufNew_arglocal()
3766 arglocal
3767 au BufNew * arglocal
3768 call assert_fails('drop xx', 'E1156:')
3769
3770 au! BufNew
3771endfunc
3772
Bram Moolenaar8ab37572021-02-03 21:56:59 +01003773func Test_autocmd_closes_window()
3774 au BufNew,BufWinLeave * e %e
3775 file yyy
3776 au BufNew,BufWinLeave * ball
Bram Moolenaaraad5f9d2021-02-06 17:30:31 +01003777 n xxx
Bram Moolenaar8ab37572021-02-03 21:56:59 +01003778
Bram Moolenaaraad5f9d2021-02-06 17:30:31 +01003779 %bwipe
Bram Moolenaar8ab37572021-02-03 21:56:59 +01003780 au! BufNew
3781 au! BufWinLeave
3782endfunc
3783
Bram Moolenaar92bb83e2021-02-03 23:04:46 +01003784func Test_autocmd_quit_psearch()
3785 sn aa bb
3786 augroup aucmd_win_test
3787 au!
3788 au BufEnter,BufLeave,BufNew,WinEnter,WinLeave,WinNew * if winnr('$') > 1 | q | endif
3789 augroup END
3790 ps /
3791
3792 augroup aucmd_win_test
3793 au!
3794 augroup END
zeertzjq7851c692022-04-21 11:14:01 +01003795 new
3796 pclose
Bram Moolenaar92bb83e2021-02-03 23:04:46 +01003797endfunc
3798
Bram Moolenaaraad5f9d2021-02-06 17:30:31 +01003799" Fuzzer found some strange combination that caused a crash.
3800func Test_autocmd_normal_mess()
Bram Moolenaardd07c022021-02-07 13:32:46 +01003801 " For unknown reason this hangs on MS-Windows
3802 CheckNotMSWindows
3803
Bram Moolenaaraad5f9d2021-02-06 17:30:31 +01003804 augroup aucmd_normal_test
3805 au BufLeave,BufWinLeave,BufHidden,BufUnload,BufDelete,BufWipeout * norm 7q/qc
3806 augroup END
zeertzjq67fe77d2025-04-20 10:21:18 +02003807 call assert_fails('o4', 'E1159:')
Bram Moolenaaraad5f9d2021-02-06 17:30:31 +01003808 silent! H
zeertzjq67fe77d2025-04-20 10:21:18 +02003809 call assert_fails('e xx', 'E1159:')
Bram Moolenaaraad5f9d2021-02-06 17:30:31 +01003810 normal G
3811
3812 augroup aucmd_normal_test
3813 au!
3814 augroup END
3815endfunc
3816
Bram Moolenaar8c6951f2021-02-06 18:08:45 +01003817func Test_autocmd_closing_cmdwin()
Bram Moolenaardd07c022021-02-07 13:32:46 +01003818 " For unknown reason this hangs on MS-Windows
3819 CheckNotMSWindows
3820
Bram Moolenaar8c6951f2021-02-06 18:08:45 +01003821 au BufWinLeave * nested q
3822 call assert_fails("norm 7q?\n", 'E855:')
3823
3824 au! BufWinLeave
3825 new
3826 only
3827endfunc
3828
Bram Moolenaar2c7080b2021-02-06 19:19:42 +01003829func Test_autocmd_vimgrep()
3830 augroup aucmd_vimgrep
Charlie Grovesfef44852022-04-19 16:24:12 +01003831 au QuickfixCmdPre,BufNew,BufReadCmd * sb
zeertzjq7851c692022-04-21 11:14:01 +01003832 au QuickfixCmdPre,BufNew,BufReadCmd * q9
Bram Moolenaar2c7080b2021-02-06 19:19:42 +01003833 augroup END
Bram Moolenaardd07c022021-02-07 13:32:46 +01003834 call assert_fails('lv ?a? foo', 'E926:')
Bram Moolenaar2c7080b2021-02-06 19:19:42 +01003835
3836 augroup aucmd_vimgrep
3837 au!
3838 augroup END
3839endfunc
3840
Bram Moolenaar73b8b0a2021-08-01 14:52:32 +02003841func Test_autocmd_with_block()
3842 augroup block_testing
3843 au BufReadPost *.xml {
3844 setlocal matchpairs+=<:>
3845 /<start
3846 }
Bram Moolenaar63b91732021-08-05 20:40:03 +02003847 au CursorHold * {
3848 autocmd BufReadPre * ++once echo 'one' | echo 'two'
3849 g:gotSafeState = 77
3850 }
Bram Moolenaar73b8b0a2021-08-01 14:52:32 +02003851 augroup END
3852
Ken Takataeccc9272024-09-03 23:01:55 +02003853 let expected = gettext("\n--- Autocommands ---") .. "\nblock_testing BufRead\n *.xml {^@ setlocal matchpairs+=<:>^@ /<start^@ }"
Bram Moolenaar73b8b0a2021-08-01 14:52:32 +02003854 call assert_equal(expected, execute('au BufReadPost *.xml'))
3855
Bram Moolenaar63b91732021-08-05 20:40:03 +02003856 doautocmd CursorHold
3857 call assert_equal(77, g:gotSafeState)
3858 unlet g:gotSafeState
3859
Bram Moolenaar73b8b0a2021-08-01 14:52:32 +02003860 augroup block_testing
3861 au!
Bram Moolenaar75ebd2a2022-06-03 17:39:46 +01003862 autocmd CursorHold * {
3863 if true
3864 # comment
3865 && true
3866
3867 && true
3868 g:done = 'yes'
3869 endif
3870 }
3871 augroup END
3872 doautocmd CursorHold
3873 call assert_equal('yes', g:done)
3874
3875 unlet g:done
3876 augroup block_testing
3877 au!
Bram Moolenaar73b8b0a2021-08-01 14:52:32 +02003878 augroup END
3879endfunc
3880
Bram Moolenaar6f2465d2022-03-22 18:13:01 +00003881func Test_closing_autocmd_window()
3882 let lines =<< trim END
3883 edit Xa.txt
3884 tabnew Xb.txt
3885 autocmd BufEnter Xa.txt unhide 1
3886 doautoall BufEnter
3887 END
3888 call v9.CheckScriptFailure(lines, 'E814:')
3889 au! BufEnter
Bram Moolenaar6f2465d2022-03-22 18:13:01 +00003890 bwipe Xa.txt
3891 bwipe Xb.txt
3892endfunc
3893
zeertzjq46bdae02023-09-24 23:16:08 +02003894func Test_switch_window_in_autocmd_window()
3895 edit Xa.txt
3896 tabnew Xb.txt
3897 autocmd BufEnter Xa.txt wincmd w
3898 doautoall BufEnter
3899 au! BufEnter
3900 bwipe Xa.txt
3901 call assert_false(bufexists('Xa.txt'))
3902 bwipe Xb.txt
3903 call assert_false(bufexists('Xb.txt'))
3904endfunc
3905
zeertzjq9d956ee2024-04-07 18:16:10 +02003906" Test that using the autocommand window doesn't change current directory.
3907func Test_autocmd_window_cwd()
3908 let saveddir = getcwd()
3909 call mkdir('Xcwd/a/b/c/d', 'pR')
3910
3911 new Xa.txt
3912 tabnew
3913 new Xb.txt
3914
3915 tabprev
3916 cd Xcwd
3917 call assert_match('/Xcwd$', getcwd())
3918 call assert_match('\[global\] .*/Xcwd$', trim(execute('verbose pwd')))
3919
3920 autocmd BufEnter Xb.txt lcd ./a/b/c/d
3921 doautoall BufEnter
3922 au! BufEnter
3923 call assert_match('/Xcwd$', getcwd())
3924 call assert_match('\[global\] .*/Xcwd$', trim(execute('verbose pwd')))
3925
3926 tabnext
3927 cd ./a
3928 tcd ./b
3929 lcd ./c
3930 call assert_match('/Xcwd/a/b/c$', getcwd())
3931 call assert_match('\[window\] .*/Xcwd/a/b/c$', trim(execute('verbose pwd')))
3932
3933 autocmd BufEnter Xa.txt call assert_match('Xcwd/a/b/c$', getcwd())
3934 doautoall BufEnter
3935 au! BufEnter
3936 call assert_match('/Xcwd/a/b/c$', getcwd())
3937 call assert_match('\[window\] .*/Xcwd/a/b/c$', trim(execute('verbose pwd')))
3938 bwipe!
3939 call assert_match('/Xcwd/a/b$', getcwd())
3940 call assert_match('\[tabpage\] .*/Xcwd/a/b$', trim(execute('verbose pwd')))
3941 bwipe!
3942 call assert_match('/Xcwd/a$', getcwd())
3943 call assert_match('\[global\] .*/Xcwd/a$', trim(execute('verbose pwd')))
3944 bwipe!
3945
3946 call chdir(saveddir)
3947endfunc
3948
Bram Moolenaar347538f2022-03-26 16:28:06 +00003949func Test_bufwipeout_changes_window()
3950 " This should not crash, but we don't have any expectations about what
3951 " happens, changing window in BufWipeout has unpredictable results.
3952 tabedit
3953 let g:window_id = win_getid()
3954 topleft new
3955 setlocal bufhidden=wipe
3956 autocmd BufWipeout <buffer> call win_gotoid(g:window_id)
3957 tabprevious
3958 +tabclose
3959
3960 unlet g:window_id
3961 au! BufWipeout
3962 %bwipe!
3963endfunc
3964
zeertzjq021996f2022-04-10 11:44:04 +01003965func Test_v_event_readonly()
3966 autocmd CompleteChanged * let v:event.width = 0
3967 call assert_fails("normal! i\<C-X>\<C-V>", 'E46:')
3968 au! CompleteChanged
3969
3970 autocmd DirChangedPre * let v:event.directory = ''
3971 call assert_fails('cd .', 'E46:')
3972 au! DirChangedPre
3973
3974 autocmd ModeChanged * let v:event.new_mode = ''
3975 call assert_fails('normal! cc', 'E46:')
3976 au! ModeChanged
3977
3978 autocmd TextYankPost * let v:event.operator = ''
3979 call assert_fails('normal! yy', 'E46:')
3980 au! TextYankPost
3981endfunc
3982
zeertzjqc9e8fd62022-07-26 18:12:38 +01003983" Test for ModeChanged pattern
3984func Test_mode_changes()
3985 let g:index = 0
zeertzjq73916ba2023-04-26 16:50:19 +01003986 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 +01003987 func! TestMode()
3988 call assert_equal(g:mode_seq[g:index], get(v:event, "old_mode"))
3989 call assert_equal(g:mode_seq[g:index + 1], get(v:event, "new_mode"))
3990 call assert_equal(mode(1), get(v:event, "new_mode"))
3991 let g:index += 1
3992 endfunc
3993
3994 au ModeChanged * :call TestMode()
3995 let g:n_to_any = 0
3996 au ModeChanged n:* let g:n_to_any += 1
zeertzjq73916ba2023-04-26 16:50:19 +01003997 call feedkeys("i\<esc>vVca\<CR>\<C-X>\<C-L>\<esc>ggdV\<MouseMove>G", 'tnix')
zeertzjqc9e8fd62022-07-26 18:12:38 +01003998
3999 let g:V_to_v = 0
4000 au ModeChanged V:v let g:V_to_v += 1
4001 call feedkeys("Vv\<C-G>\<esc>", 'tnix')
4002 call assert_equal(len(filter(g:mode_seq[1:], {idx, val -> val == 'n'})), g:n_to_any)
4003 call assert_equal(1, g:V_to_v)
4004 call assert_equal(len(g:mode_seq) - 1, g:index)
4005
4006 let g:n_to_i = 0
4007 au ModeChanged n:i let g:n_to_i += 1
4008 let g:n_to_niI = 0
4009 au ModeChanged i:niI let g:n_to_niI += 1
4010 let g:niI_to_i = 0
4011 au ModeChanged niI:i let g:niI_to_i += 1
4012 let g:nany_to_i = 0
4013 au ModeChanged n*:i let g:nany_to_i += 1
4014 let g:i_to_n = 0
4015 au ModeChanged i:n let g:i_to_n += 1
4016 let g:nori_to_any = 0
4017 au ModeChanged [ni]:* let g:nori_to_any += 1
4018 let g:i_to_any = 0
4019 au ModeChanged i:* let g:i_to_any += 1
4020 let g:index = 0
4021 let g:mode_seq = ['n', 'i', 'niI', 'i', 'n']
4022 call feedkeys("a\<C-O>l\<esc>", 'tnix')
4023 call assert_equal(len(g:mode_seq) - 1, g:index)
4024 call assert_equal(1, g:n_to_i)
4025 call assert_equal(1, g:n_to_niI)
4026 call assert_equal(1, g:niI_to_i)
4027 call assert_equal(2, g:nany_to_i)
4028 call assert_equal(1, g:i_to_n)
4029 call assert_equal(2, g:i_to_any)
4030 call assert_equal(3, g:nori_to_any)
4031
4032 if has('terminal')
4033 let g:mode_seq += ['c', 'n', 't', 'nt', 'c', 'nt', 'n']
4034 call feedkeys(":term\<CR>\<C-W>N:bd!\<CR>", 'tnix')
4035 call assert_equal(len(g:mode_seq) - 1, g:index)
4036 call assert_equal(1, g:n_to_i)
4037 call assert_equal(1, g:n_to_niI)
4038 call assert_equal(1, g:niI_to_i)
4039 call assert_equal(2, g:nany_to_i)
4040 call assert_equal(1, g:i_to_n)
4041 call assert_equal(2, g:i_to_any)
4042 call assert_equal(5, g:nori_to_any)
4043 endif
4044
zeertzjqd1955982022-10-05 11:24:46 +01004045 let g:n_to_c = 0
4046 au ModeChanged n:c let g:n_to_c += 1
4047 let g:c_to_n = 0
4048 au ModeChanged c:n let g:c_to_n += 1
4049 let g:mode_seq += ['c', 'n', 'c', 'n']
4050 call feedkeys("q:\<C-C>\<Esc>", 'tnix')
4051 call assert_equal(len(g:mode_seq) - 1, g:index)
4052 call assert_equal(2, g:n_to_c)
4053 call assert_equal(2, g:c_to_n)
zeertzjqc9e8fd62022-07-26 18:12:38 +01004054
Bram Moolenaar61c4b042022-10-18 15:10:11 +01004055 let g:n_to_v = 0
4056 au ModeChanged n:v let g:n_to_v += 1
4057 let g:v_to_n = 0
4058 au ModeChanged v:n let g:v_to_n += 1
4059 let g:mode_seq += ['v', 'n']
4060 call feedkeys("v\<C-C>", 'tnix')
4061 call assert_equal(len(g:mode_seq) - 1, g:index)
4062 call assert_equal(1, g:n_to_v)
4063 call assert_equal(1, g:v_to_n)
zeertzjqfcaeb3d2023-11-28 20:46:29 +01004064
4065 let g:mode_seq += ['c', 'cr', 'c', 'cr', 'n']
4066 call feedkeys(":\<Insert>\<Insert>\<Insert>\<CR>", 'tnix')
4067 call assert_equal(len(g:mode_seq) - 1, g:index)
Bram Moolenaar61c4b042022-10-18 15:10:11 +01004068
zeertzjqc9e8fd62022-07-26 18:12:38 +01004069 au! ModeChanged
4070 delfunc TestMode
4071 unlet! g:mode_seq
4072 unlet! g:index
4073 unlet! g:n_to_any
4074 unlet! g:V_to_v
4075 unlet! g:n_to_i
4076 unlet! g:n_to_niI
4077 unlet! g:niI_to_i
4078 unlet! g:nany_to_i
4079 unlet! g:i_to_n
4080 unlet! g:nori_to_any
4081 unlet! g:i_to_any
zeertzjqfcaeb3d2023-11-28 20:46:29 +01004082 unlet! g:n_to_c
4083 unlet! g:c_to_n
4084 unlet! g:n_to_v
4085 unlet! g:v_to_n
zeertzjqc9e8fd62022-07-26 18:12:38 +01004086endfunc
4087
4088func Test_recursive_ModeChanged()
4089 au! ModeChanged * norm 0u
4090 sil! norm 
4091 au! ModeChanged
4092endfunc
4093
4094func Test_ModeChanged_starts_visual()
4095 " This was triggering ModeChanged before setting VIsual, causing a crash.
4096 au! ModeChanged * norm 0u
4097 sil! norm 
4098
4099 au! ModeChanged
4100endfunc
Bram Moolenaar347538f2022-03-26 16:28:06 +00004101
Charlie Grovesfef44852022-04-19 16:24:12 +01004102func Test_noname_autocmd()
4103 augroup test_noname_autocmd_group
4104 autocmd!
4105 autocmd BufEnter * call add(s:li, ["BufEnter", expand("<afile>")])
4106 autocmd BufDelete * call add(s:li, ["BufDelete", expand("<afile>")])
4107 autocmd BufLeave * call add(s:li, ["BufLeave", expand("<afile>")])
4108 autocmd BufUnload * call add(s:li, ["BufUnload", expand("<afile>")])
4109 autocmd BufWipeout * call add(s:li, ["BufWipeout", expand("<afile>")])
4110 augroup END
4111
4112 let s:li = []
4113 edit foo
4114 call assert_equal([['BufUnload', ''], ['BufDelete', ''], ['BufWipeout', ''], ['BufEnter', 'foo']], s:li)
4115
4116 au! test_noname_autocmd_group
4117 augroup! test_noname_autocmd_group
4118endfunc
4119
Yegappan Lakshmanan1755a912022-05-19 10:31:47 +01004120" Test for the autocmd_get() function
4121func Test_autocmd_get()
4122 augroup TestAutoCmdFns
4123 au!
4124 autocmd BufAdd *.vim echo "bufadd-vim"
4125 autocmd BufAdd *.py echo "bufadd-py"
4126 autocmd BufHidden *.vim echo "bufhidden"
4127 augroup END
4128 augroup TestAutoCmdFns2
4129 autocmd BufAdd *.vim echo "bufadd-vim-2"
4130 autocmd BufRead *.a1b2c3 echo "bufadd-vim-2"
4131 augroup END
4132
4133 let l = autocmd_get()
4134 call assert_true(l->len() > 0)
4135
4136 " Test for getting all the autocmds in a group
4137 let expected = [
4138 \ #{cmd: 'echo "bufadd-vim"', group: 'TestAutoCmdFns',
4139 \ pattern: '*.vim', nested: v:false, once: v:false,
4140 \ event: 'BufAdd'},
4141 \ #{cmd: 'echo "bufadd-py"', group: 'TestAutoCmdFns',
4142 \ pattern: '*.py', nested: v:false, once: v:false,
4143 \ event: 'BufAdd'},
4144 \ #{cmd: 'echo "bufhidden"', group: 'TestAutoCmdFns',
4145 \ pattern: '*.vim', nested: v:false,
4146 \ once: v:false, event: 'BufHidden'}]
4147 call assert_equal(expected, autocmd_get(#{group: 'TestAutoCmdFns'}))
4148
4149 " Test for getting autocmds for all the patterns in a group
4150 call assert_equal(expected, autocmd_get(#{group: 'TestAutoCmdFns',
4151 \ event: '*'}))
4152
4153 " Test for getting autocmds for an event in a group
4154 let expected = [
4155 \ #{cmd: 'echo "bufadd-vim"', group: 'TestAutoCmdFns',
4156 \ pattern: '*.vim', nested: v:false, once: v:false,
4157 \ event: 'BufAdd'},
4158 \ #{cmd: 'echo "bufadd-py"', group: 'TestAutoCmdFns',
4159 \ pattern: '*.py', nested: v:false, once: v:false,
4160 \ event: 'BufAdd'}]
4161 call assert_equal(expected, autocmd_get(#{group: 'TestAutoCmdFns',
4162 \ event: 'BufAdd'}))
4163
4164 " Test for getting the autocmds for all the events in a group for particular
4165 " pattern
4166 call assert_equal([{'cmd': 'echo "bufadd-py"', 'group': 'TestAutoCmdFns',
4167 \ 'pattern': '*.py', 'nested': v:false, 'once': v:false,
4168 \ 'event': 'BufAdd'}],
4169 \ autocmd_get(#{group: 'TestAutoCmdFns', event: '*', pattern: '*.py'}))
4170
4171 " Test for getting the autocmds for an events in a group for particular
4172 " pattern
4173 let l = autocmd_get(#{group: 'TestAutoCmdFns', event: 'BufAdd',
4174 \ pattern: '*.vim'})
4175 call assert_equal([
4176 \ #{cmd: 'echo "bufadd-vim"', group: 'TestAutoCmdFns',
4177 \ pattern: '*.vim', nested: v:false, once: v:false,
4178 \ event: 'BufAdd'}], l)
4179
4180 " Test for getting the autocmds for a pattern in a group
4181 let l = autocmd_get(#{group: 'TestAutoCmdFns', pattern: '*.vim'})
4182 call assert_equal([
4183 \ #{cmd: 'echo "bufadd-vim"', group: 'TestAutoCmdFns',
4184 \ pattern: '*.vim', nested: v:false, once: v:false,
4185 \ event: 'BufAdd'},
4186 \ #{cmd: 'echo "bufhidden"', group: 'TestAutoCmdFns',
4187 \ pattern: '*.vim', nested: v:false,
4188 \ once: v:false, event: 'BufHidden'}], l)
4189
4190 " Test for getting the autocmds for a pattern in all the groups
4191 let l = autocmd_get(#{pattern: '*.a1b2c3'})
4192 call assert_equal([{'cmd': 'echo "bufadd-vim-2"', 'group': 'TestAutoCmdFns2',
4193 \ 'pattern': '*.a1b2c3', 'nested': v:false, 'once': v:false,
4194 \ 'event': 'BufRead'}], l)
4195
4196 " Test for getting autocmds for a pattern without any autocmds
4197 call assert_equal([], autocmd_get(#{group: 'TestAutoCmdFns',
4198 \ pattern: '*.abc'}))
4199 call assert_equal([], autocmd_get(#{group: 'TestAutoCmdFns',
4200 \ event: 'BufAdd', pattern: '*.abc'}))
4201 call assert_equal([], autocmd_get(#{group: 'TestAutoCmdFns',
4202 \ event: 'BufWipeout'}))
zeertzjq2d1d5c62024-06-09 16:44:33 +02004203
4204 " Test for getting autocmds after removing one inside an autocmd
4205 func CheckAutocmdGet()
4206 augroup TestAutoCmdFns
4207 autocmd! BufAdd *.vim
4208 augroup END
4209
4210 let expected = [
4211 \ #{cmd: 'echo "bufadd-py"', group: 'TestAutoCmdFns',
4212 \ pattern: '*.py', nested: v:false, once: v:false,
4213 \ event: 'BufAdd'},
4214 \ #{cmd: 'echo "bufhidden"', group: 'TestAutoCmdFns',
4215 \ pattern: '*.vim', nested: v:false,
4216 \ once: v:false, event: 'BufHidden'}]
4217
4218 call assert_equal(expected, autocmd_get(#{group: 'TestAutoCmdFns'}))
4219 call assert_equal([expected[0]],
4220 \ autocmd_get(#{group: 'TestAutoCmdFns', pattern: '*.py'}))
4221 call assert_equal([expected[1]],
4222 \ autocmd_get(#{group: 'TestAutoCmdFns', pattern: '*.vim'}))
4223 endfunc
4224
4225 autocmd User Xauget call CheckAutocmdGet()
4226 doautocmd User Xauget
4227 autocmd! User Xauget
4228
Yegappan Lakshmanan1755a912022-05-19 10:31:47 +01004229 call assert_fails("call autocmd_get(#{group: 'abc', event: 'BufAdd'})",
4230 \ 'E367:')
4231 let cmd = "echo autocmd_get(#{group: 'TestAutoCmdFns', event: 'abc'})"
4232 call assert_fails(cmd, 'E216:')
4233 call assert_fails("call autocmd_get(#{group: 'abc'})", 'E367:')
4234 call assert_fails("echo autocmd_get(#{event: 'abc'})", 'E216:')
4235
4236 augroup TestAutoCmdFns
4237 au!
4238 augroup END
4239 call assert_equal([], autocmd_get(#{group: 'TestAutoCmdFns'}))
4240
4241 " Test for nested and once autocmds
4242 augroup TestAutoCmdFns
4243 au!
4244 autocmd VimSuspend * ++nested echo "suspend"
4245 autocmd VimResume * ++once echo "resume"
4246 augroup END
4247
4248 let expected = [
Yegappan Lakshmanan1755a912022-05-19 10:31:47 +01004249 \ {'cmd': 'echo "resume"', 'group': 'TestAutoCmdFns', 'pattern': '*',
Luuk van Baalb7147f82025-02-08 18:52:39 +01004250 \ 'nested': v:false, 'once': v:true, 'event': 'VimResume'},
4251 \ {'cmd': 'echo "suspend"', 'group': 'TestAutoCmdFns', 'pattern': '*',
4252 \ 'nested': v:true, 'once': v:false, 'event': 'VimSuspend'}]
Yegappan Lakshmanan1755a912022-05-19 10:31:47 +01004253 call assert_equal(expected, autocmd_get(#{group: 'TestAutoCmdFns'}))
4254
4255 " Test for buffer-local autocmd
4256 augroup TestAutoCmdFns
4257 au!
4258 autocmd TextYankPost <buffer> echo "textyankpost"
4259 augroup END
4260
4261 let expected = [
4262 \ {'cmd': 'echo "textyankpost"', 'group': 'TestAutoCmdFns',
4263 \ 'pattern': '<buffer=' .. bufnr() .. '>', 'nested': v:false,
4264 \ 'once': v:false, 'bufnr': bufnr(), 'event': 'TextYankPost'}]
4265 call assert_equal(expected, autocmd_get(#{group: 'TestAutoCmdFns'}))
4266
4267 augroup TestAutoCmdFns
4268 au!
4269 augroup END
4270 augroup! TestAutoCmdFns
4271 augroup TestAutoCmdFns2
4272 au!
4273 augroup END
4274 augroup! TestAutoCmdFns2
4275
4276 call assert_fails("echo autocmd_get(#{group: []})", 'E730:')
4277 call assert_fails("echo autocmd_get(#{event: {}})", 'E731:')
4278 call assert_fails("echo autocmd_get([])", 'E1206:')
4279endfunc
4280
4281" Test for the autocmd_add() function
4282func Test_autocmd_add()
4283 " Define a single autocmd in a group
4284 call autocmd_add([#{group: 'TestAcSet', event: 'BufAdd', pattern: '*.sh',
4285 \ cmd: 'echo "bufadd"', once: v:true, nested: v:true}])
4286 call assert_equal([#{cmd: 'echo "bufadd"', group: 'TestAcSet',
4287 \ pattern: '*.sh', nested: v:true, once: v:true,
4288 \ event: 'BufAdd'}], autocmd_get(#{group: 'TestAcSet'}))
4289
4290 " Define two autocmds in the same group
4291 call autocmd_delete([#{group: 'TestAcSet'}])
4292 call autocmd_add([#{group: 'TestAcSet', event: 'BufAdd', pattern: '*.sh',
4293 \ cmd: 'echo "bufadd"'},
4294 \ #{group: 'TestAcSet', event: 'BufEnter', pattern: '*.sh',
4295 \ cmd: 'echo "bufenter"'}])
4296 call assert_equal([
4297 \ #{cmd: 'echo "bufadd"', group: 'TestAcSet', pattern: '*.sh',
4298 \ nested: v:false, once: v:false, event: 'BufAdd'},
4299 \ #{cmd: 'echo "bufenter"', group: 'TestAcSet', pattern: '*.sh',
4300 \ nested: v:false, once: v:false, event: 'BufEnter'}],
4301 \ autocmd_get(#{group: 'TestAcSet'}))
4302
4303 " Define a buffer-local autocmd
4304 call autocmd_delete([#{group: 'TestAcSet'}])
4305 call autocmd_add([#{group: 'TestAcSet', event: 'CursorHold',
4306 \ bufnr: bufnr(), cmd: 'echo "cursorhold"'}])
4307 call assert_equal([
4308 \ #{cmd: 'echo "cursorhold"', group: 'TestAcSet',
4309 \ pattern: '<buffer=' .. bufnr() .. '>', nested: v:false,
4310 \ once: v:false, bufnr: bufnr(), event: 'CursorHold'}],
4311 \ autocmd_get(#{group: 'TestAcSet'}))
4312
4313 " Use an invalid buffer number
4314 call autocmd_delete([#{group: 'TestAcSet'}])
4315 call autocmd_add([#{group: 'TestAcSet', event: 'BufEnter',
4316 \ bufnr: -1, cmd: 'echo "bufenter"'}])
4317 let l = [#{group: 'TestAcSet', event: 'BufAdd', bufnr: 9999,
4318 \ cmd: 'echo "bufadd"'}]
4319 call assert_fails("echo autocmd_add(l)", 'E680:')
Yegappan Lakshmanan00e977c2022-06-01 12:31:53 +01004320 let l = [#{group: 'TestAcSet', event: 'BufAdd', bufnr: 9999,
4321 \ pattern: '*.py', cmd: 'echo "bufadd"'}]
4322 call assert_fails("echo autocmd_add(l)", 'E680:')
4323 let l = [#{group: 'TestAcSet', event: 'BufAdd', bufnr: 9999,
4324 \ pattern: ['*.py', '*.c'], cmd: 'echo "bufadd"'}]
4325 call assert_fails("echo autocmd_add(l)", 'E680:')
Yegappan Lakshmanan1755a912022-05-19 10:31:47 +01004326 let l = [#{group: 'TestAcSet', event: 'BufRead', bufnr: [],
4327 \ cmd: 'echo "bufread"'}]
4328 call assert_fails("echo autocmd_add(l)", 'E745:')
4329 call assert_equal([], autocmd_get(#{group: 'TestAcSet'}))
4330
4331 " Add two commands to the same group, event and pattern
4332 call autocmd_delete([#{group: 'TestAcSet'}])
4333 call autocmd_add([#{group: 'TestAcSet', event: 'BufUnload',
4334 \ pattern: 'abc', cmd: 'echo "cmd1"'}])
4335 call autocmd_add([#{group: 'TestAcSet', event: 'BufUnload',
4336 \ pattern: 'abc', cmd: 'echo "cmd2"'}])
4337 call assert_equal([
4338 \ #{cmd: 'echo "cmd1"', group: 'TestAcSet', pattern: 'abc',
4339 \ nested: v:false, once: v:false, event: 'BufUnload'},
4340 \ #{cmd: 'echo "cmd2"', group: 'TestAcSet', pattern: 'abc',
4341 \ nested: v:false, once: v:false, event: 'BufUnload'}],
4342 \ autocmd_get(#{group: 'TestAcSet'}))
4343
4344 " When adding a new autocmd, if the autocmd 'group' is not specified, then
4345 " the current autocmd group should be used.
4346 call autocmd_delete([#{group: 'TestAcSet'}])
4347 augroup TestAcSet
4348 call autocmd_add([#{event: 'BufHidden', pattern: 'abc', cmd: 'echo "abc"'}])
4349 augroup END
4350 call assert_equal([
4351 \ #{cmd: 'echo "abc"', group: 'TestAcSet', pattern: 'abc',
4352 \ nested: v:false, once: v:false, event: 'BufHidden'}],
4353 \ autocmd_get(#{group: 'TestAcSet'}))
4354
Yegappan Lakshmanan971f6822022-05-24 11:40:11 +01004355 " Test for replacing a cmd for an event in a group
4356 call autocmd_delete([#{group: 'TestAcSet'}])
4357 call autocmd_add([#{replace: v:true, group: 'TestAcSet', event: 'BufEnter',
4358 \ pattern: '*.py', cmd: 'echo "bufenter"'}])
4359 call autocmd_add([#{replace: v:true, group: 'TestAcSet', event: 'BufEnter',
4360 \ pattern: '*.py', cmd: 'echo "bufenter"'}])
4361 call assert_equal([
4362 \ #{cmd: 'echo "bufenter"', group: 'TestAcSet', pattern: '*.py',
4363 \ nested: v:false, once: v:false, event: 'BufEnter'}],
4364 \ autocmd_get(#{group: 'TestAcSet'}))
4365
4366 " Test for adding a command for an unsupported autocmd event
Yegappan Lakshmanan1755a912022-05-19 10:31:47 +01004367 let l = [#{group: 'TestAcSet', event: 'abc', pattern: '*.sh',
4368 \ cmd: 'echo "bufadd"'}]
4369 call assert_fails('call autocmd_add(l)', 'E216:')
4370
Yegappan Lakshmanane0ff3a72022-05-27 18:05:33 +01004371 " Test for using a list of events and patterns
4372 call autocmd_delete([#{group: 'TestAcSet'}])
4373 let l = [#{group: 'TestAcSet', event: ['BufEnter', 'BufLeave'],
4374 \ pattern: ['*.py', '*.sh'], cmd: 'echo "bufcmds"'}]
4375 call autocmd_add(l)
4376 call assert_equal([
4377 \ #{cmd: 'echo "bufcmds"', group: 'TestAcSet', pattern: '*.py',
4378 \ nested: v:false, once: v:false, event: 'BufEnter'},
4379 \ #{cmd: 'echo "bufcmds"', group: 'TestAcSet', pattern: '*.sh',
4380 \ nested: v:false, once: v:false, event: 'BufEnter'},
4381 \ #{cmd: 'echo "bufcmds"', group: 'TestAcSet', pattern: '*.py',
4382 \ nested: v:false, once: v:false, event: 'BufLeave'},
4383 \ #{cmd: 'echo "bufcmds"', group: 'TestAcSet', pattern: '*.sh',
4384 \ nested: v:false, once: v:false, event: 'BufLeave'}],
4385 \ autocmd_get(#{group: 'TestAcSet'}))
4386
4387 " Test for invalid values for 'event' item
4388 call autocmd_delete([#{group: 'TestAcSet'}])
4389 let l = [#{group: 'TestAcSet', event: test_null_string(),
4390 \ pattern: "*.py", cmd: 'echo "bufcmds"'}]
4391 call assert_fails('call autocmd_add(l)', 'E928:')
4392 let l = [#{group: 'TestAcSet', event: test_null_list(),
4393 \ pattern: "*.py", cmd: 'echo "bufcmds"'}]
4394 call assert_fails('call autocmd_add(l)', 'E714:')
4395 let l = [#{group: 'TestAcSet', event: {},
4396 \ pattern: "*.py", cmd: 'echo "bufcmds"'}]
4397 call assert_fails('call autocmd_add(l)', 'E777:')
4398 let l = [#{group: 'TestAcSet', event: [{}],
4399 \ pattern: "*.py", cmd: 'echo "bufcmds"'}]
4400 call assert_fails('call autocmd_add(l)', 'E928:')
4401 let l = [#{group: 'TestAcSet', event: [test_null_string()],
4402 \ pattern: "*.py", cmd: 'echo "bufcmds"'}]
4403 call assert_fails('call autocmd_add(l)', 'E928:')
4404 let l = [#{group: 'TestAcSet', event: 'BufEnter,BufLeave',
4405 \ pattern: '*.py', cmd: 'echo "bufcmds"'}]
4406 call assert_fails('call autocmd_add(l)', 'E216:')
4407 let l = [#{group: 'TestAcSet', event: [],
4408 \ pattern: "*.py", cmd: 'echo "bufcmds"'}]
4409 call autocmd_add(l)
4410 let l = [#{group: 'TestAcSet', event: [""],
4411 \ pattern: "*.py", cmd: 'echo "bufcmds"'}]
4412 call assert_fails('call autocmd_add(l)', 'E216:')
4413 let l = [#{group: 'TestAcSet', event: "",
4414 \ pattern: "*.py", cmd: 'echo "bufcmds"'}]
4415 call autocmd_add(l)
4416 call assert_equal([], autocmd_get(#{group: 'TestAcSet'}))
4417
4418 " Test for invalid values for 'pattern' item
4419 let l = [#{group: 'TestAcSet', event: "BufEnter",
4420 \ pattern: test_null_string(), cmd: 'echo "bufcmds"'}]
Yegappan Lakshmanan00e977c2022-06-01 12:31:53 +01004421 call assert_fails('call autocmd_add(l)', 'E928:')
Yegappan Lakshmanane0ff3a72022-05-27 18:05:33 +01004422 let l = [#{group: 'TestAcSet', event: "BufEnter",
4423 \ pattern: test_null_list(), cmd: 'echo "bufcmds"'}]
4424 call assert_fails('call autocmd_add(l)', 'E714:')
4425 let l = [#{group: 'TestAcSet', event: "BufEnter",
4426 \ pattern: {}, cmd: 'echo "bufcmds"'}]
4427 call assert_fails('call autocmd_add(l)', 'E777:')
4428 let l = [#{group: 'TestAcSet', event: "BufEnter",
4429 \ pattern: [{}], cmd: 'echo "bufcmds"'}]
4430 call assert_fails('call autocmd_add(l)', 'E928:')
4431 let l = [#{group: 'TestAcSet', event: "BufEnter",
4432 \ pattern: [test_null_string()], cmd: 'echo "bufcmds"'}]
4433 call assert_fails('call autocmd_add(l)', 'E928:')
4434 let l = [#{group: 'TestAcSet', event: "BufEnter",
4435 \ pattern: [], cmd: 'echo "bufcmds"'}]
4436 call autocmd_add(l)
4437 let l = [#{group: 'TestAcSet', event: "BufEnter",
4438 \ pattern: [""], cmd: 'echo "bufcmds"'}]
4439 call autocmd_add(l)
4440 let l = [#{group: 'TestAcSet', event: "BufEnter",
4441 \ pattern: "", cmd: 'echo "bufcmds"'}]
4442 call autocmd_add(l)
4443 call assert_equal([], autocmd_get(#{group: 'TestAcSet'}))
4444
4445 let l = [#{group: 'TestAcSet', event: 'BufEnter,abc,BufLeave',
4446 \ pattern: '*.py', cmd: 'echo "bufcmds"'}]
4447 call assert_fails('call autocmd_add(l)', 'E216:')
4448
Yegappan Lakshmanan1755a912022-05-19 10:31:47 +01004449 call assert_fails("call autocmd_add({})", 'E1211:')
4450 call assert_equal(v:false, autocmd_add(test_null_list()))
4451 call assert_true(autocmd_add([[]]))
4452 call assert_true(autocmd_add([test_null_dict()]))
4453
4454 augroup TestAcSet
4455 au!
4456 augroup END
4457
4458 call autocmd_add([#{group: 'TestAcSet'}])
4459 call autocmd_add([#{group: 'TestAcSet', event: 'BufAdd'}])
4460 call autocmd_add([#{group: 'TestAcSet', pat: '*.sh'}])
4461 call autocmd_add([#{group: 'TestAcSet', cmd: 'echo "a"'}])
4462 call autocmd_add([#{group: 'TestAcSet', event: 'BufAdd', pat: '*.sh'}])
4463 call autocmd_add([#{group: 'TestAcSet', event: 'BufAdd', cmd: 'echo "a"'}])
4464 call autocmd_add([#{group: 'TestAcSet', pat: '*.sh', cmd: 'echo "a"'}])
4465 call assert_equal([], autocmd_get(#{group: 'TestAcSet'}))
4466
4467 augroup! TestAcSet
4468endfunc
4469
4470" Test for deleting autocmd events and groups
4471func Test_autocmd_delete()
4472 " Delete an event in an autocmd group
4473 augroup TestAcSet
4474 au!
4475 au BufAdd *.sh echo "bufadd"
4476 au BufEnter *.sh echo "bufenter"
4477 augroup END
4478 call autocmd_delete([#{group: 'TestAcSet', event: 'BufAdd'}])
4479 call assert_equal([#{cmd: 'echo "bufenter"', group: 'TestAcSet',
4480 \ pattern: '*.sh', nested: v:false, once: v:false,
4481 \ event: 'BufEnter'}], autocmd_get(#{group: 'TestAcSet'}))
4482
4483 " Delete all the events in an autocmd group
4484 augroup TestAcSet
4485 au BufAdd *.sh echo "bufadd"
4486 augroup END
4487 call autocmd_delete([#{group: 'TestAcSet', event: '*'}])
4488 call assert_equal([], autocmd_get(#{group: 'TestAcSet'}))
4489
4490 " Delete a non-existing autocmd group
4491 call assert_fails("call autocmd_delete([#{group: 'abc'}])", 'E367:')
4492 " Delete a non-existing autocmd event
4493 let l = [#{group: 'TestAcSet', event: 'abc'}]
4494 call assert_fails("call autocmd_delete(l)", 'E216:')
4495 " Delete a non-existing autocmd pattern
4496 let l = [#{group: 'TestAcSet', event: 'BufAdd', pat: 'abc'}]
4497 call assert_true(autocmd_delete(l))
Yegappan Lakshmanan00e977c2022-06-01 12:31:53 +01004498 " Delete an autocmd for a non-existing buffer
4499 let l = [#{event: '*', bufnr: 9999, cmd: 'echo "x"'}]
4500 call assert_fails('call autocmd_delete(l)', 'E680:')
Yegappan Lakshmanan1755a912022-05-19 10:31:47 +01004501
4502 " Delete an autocmd group
4503 augroup TestAcSet
4504 au!
4505 au BufAdd *.sh echo "bufadd"
4506 au BufEnter *.sh echo "bufenter"
4507 augroup END
4508 call autocmd_delete([#{group: 'TestAcSet'}])
4509 call assert_fails("call autocmd_get(#{group: 'TestAcSet'})", 'E367:')
4510
4511 call assert_true(autocmd_delete([[]]))
4512 call assert_true(autocmd_delete([test_null_dict()]))
4513endfunc
4514
Bram Moolenaar8f3c3c62022-10-18 17:05:54 +01004515func Test_autocmd_split_dummy()
4516 " Autocommand trying to split a window containing a dummy buffer.
Bram Moolenaar94722c52023-01-28 19:19:03 +00004517 auto BufReadPre * exe "sbuf " .. expand("<abuf>")
Bram Moolenaar8f3c3c62022-10-18 17:05:54 +01004518 " Avoid the "W11" prompt
4519 au FileChangedShell * let v:fcs_choice = 'reload'
4520 func Xautocmd_changelist()
4521 cal writefile(['Xtestfile2:4:4'], 'Xerr')
4522 edit Xerr
4523 lex 'Xtestfile2:4:4'
4524 endfunc
4525 call Xautocmd_changelist()
Bram Moolenaar53c5c9f2022-10-18 17:25:03 +01004526 " Should get E86, but it doesn't always happen (timing?)
4527 silent! call Xautocmd_changelist()
Bram Moolenaar8f3c3c62022-10-18 17:05:54 +01004528
4529 au! BufReadPre
4530 au! FileChangedShell
4531 delfunc Xautocmd_changelist
4532 bwipe! Xerr
4533 call delete('Xerr')
4534endfunc
4535
Bram Moolenaare76062c2022-11-28 18:51:43 +00004536" This was crashing because there was only one window to execute autocommands
4537" in.
4538func Test_autocmd_nested_setbufvar()
4539 CheckFeature python3
4540
4541 set hidden
4542 edit Xaaa
4543 edit Xbbb
4544 call setline(1, 'bar')
4545 enew
4546 au BufWriteCmd Xbbb ++nested call setbufvar('Xaaa', '&ft', 'foo') | bw! Xaaa
4547 au FileType foo call py3eval('vim.current.buffer.options["cindent"]')
4548 wall
4549
4550 au! BufWriteCmd
4551 au! FileType foo
4552 set nohidden
4553 call delete('Xaaa')
4554 call delete('Xbbb')
4555 %bwipe!
4556endfunc
4557
Christian Brabandt9aee8ec2022-12-16 16:41:23 +00004558func SetupVimTest_shm()
4559 let g:bwe = []
4560 let g:brp = []
4561 set shortmess+=F
zeertzjq657b31f2023-04-15 21:28:02 +01004562 messages clear
Christian Brabandt9aee8ec2022-12-16 16:41:23 +00004563
4564 let dirname='XVimTestSHM'
4565 call mkdir(dirname, 'R')
4566 call writefile(['test'], dirname .. '/1')
4567 call writefile(['test'], dirname .. '/2')
4568 call writefile(['test'], dirname .. '/3')
4569
4570 augroup test
4571 autocmd!
4572 autocmd BufWinEnter * call add(g:bwe, $'BufWinEnter: {expand('<amatch>')}')
4573 autocmd BufReadPost * call add(g:brp, $'BufReadPost: {expand('<amatch>')}')
4574 augroup END
4575
4576 call setqflist([
4577 \ {'filename': dirname .. '/1', 'lnum': 1, 'col': 1, 'text': 'test', 'vcol': 0},
4578 \ {'filename': dirname .. '/2', 'lnum': 1, 'col': 1, 'text': 'test', 'vcol': 0},
4579 \ {'filename': dirname .. '/3', 'lnum': 1, 'col': 1, 'text': 'test', 'vcol': 0}
4580 \ ])
4581 cdo! substitute/test/TEST
4582
4583 " clean up
4584 noa enew!
4585 set shortmess&vim
4586 augroup test
4587 autocmd!
4588 augroup END
4589 augroup! test
4590endfunc
4591
4592func Test_autocmd_shortmess()
4593 CheckNotMSWindows
4594
4595 call SetupVimTest_shm()
4596 let output = execute(':mess')->split('\n')
4597
4598 let info = copy(output)->filter({idx, val -> val =~# '\d of 3'} )
4599 let bytes = copy(output)->filter({idx, val -> val =~# 'bytes'} )
4600
4601 " We test the following here:
4602 " BufReadPost should have been triggered 3 times, once per file
4603 " BufWinEnter should have been triggered 3 times, once per file
4604 " FileInfoMessage should have been shown 3 times, regardless of shm option
4605 " "(x of 3)" message from :cnext has been shown 3 times
4606
4607 call assert_equal(3, g:brp->len())
4608 call assert_equal(3, g:bwe->len())
4609 call assert_equal(3, info->len())
4610 call assert_equal(3, bytes->len())
4611
4612 delfunc SetupVimTest_shm
4613endfunc
Bram Moolenaare76062c2022-11-28 18:51:43 +00004614
Christian Brabandtf0d3d4a2024-02-15 20:15:04 +01004615func Test_autocmd_invalidates_undo_on_textchanged()
4616 CheckRunVimInTerminal
4617 let script =<< trim END
4618 set hidden
4619 " create quickfix list (at least 2 lines to move line)
4620 vimgrep /u/j %
4621
4622 " enter quickfix window
4623 cwindow
4624
4625 " set modifiable
4626 setlocal modifiable
4627
4628 " set autocmd to clear quickfix list
4629
4630 autocmd! TextChanged <buffer> call setqflist([])
4631 " move line
4632 move+1
4633 END
4634 call writefile(script, 'XTest_autocmd_invalidates_undo_on_textchanged', 'D')
4635 let buf = RunVimInTerminal('XTest_autocmd_invalidates_undo_on_textchanged', {'rows': 20})
4636 call term_sendkeys(buf, ":so %\<cr>")
4637 call term_sendkeys(buf, "G")
4638 call WaitForAssert({-> assert_match('^XTest_autocmd_invalidates_undo_on_textchanged\s*$', term_getline(buf, 20))}, 1000)
4639
4640 call StopVimInTerminal(buf)
4641endfunc
4642
Christian Brabandt55f8bba2024-02-28 23:32:00 +01004643func Test_autocmd_creates_new_buffer_on_bufleave()
4644 e a.txt
4645 e b.txt
4646 setlocal bufhidden=wipe
4647 autocmd BufLeave <buffer> diffsplit c.txt
4648 bn
4649 call assert_equal(1, winnr('$'))
4650 call assert_equal('a.txt', bufname('%'))
4651 bw a.txt
4652 bw c.txt
4653endfunc
4654
Colin Kennedye5f22802024-03-26 18:20:16 +01004655" Ensure `expected` was just recently written as a Vim session
4656func s:assert_session_path(expected)
4657 call assert_equal(a:expected, v:this_session)
4658endfunc
4659
4660" Check for `expected` after a session is written to-disk.
4661func s:watch_for_session_path(expected)
4662 execute 'autocmd SessionWritePost * ++once execute "call s:assert_session_path(\"'
4663 \ . a:expected
4664 \ . '\")"'
4665endfunc
4666
4667" Ensure v:this_session gets the full session path, if explicitly stated
4668func Test_explicit_session_absolute_path()
4669 %bwipeout!
4670
4671 let directory = getcwd()
4672
4673 let v:this_session = ""
4674 let name = "some_file.vim"
4675 let expected = fnamemodify(name, ":p")
4676 call s:watch_for_session_path(expected)
4677 execute "mksession! " .. expected
4678
4679 call delete(expected)
4680endfunc
4681
4682" Ensure v:this_session gets the full session path, if explicitly stated
4683func Test_explicit_session_relative_path()
4684 %bwipeout!
4685
4686 let directory = getcwd()
4687
4688 let v:this_session = ""
4689 let name = "some_file.vim"
4690 let expected = fnamemodify(name, ":p")
4691 call s:watch_for_session_path(expected)
4692 execute "mksession! " .. name
4693
4694 call delete(expected)
4695endfunc
4696
4697" Ensure v:this_session gets the full session path, if not specified
4698func Test_implicit_session()
4699 %bwipeout!
4700
4701 let directory = getcwd()
4702
4703 let v:this_session = ""
4704 let expected = fnamemodify("Session.vim", ":p")
4705 call s:watch_for_session_path(expected)
4706 mksession!
4707
4708 call delete(expected)
4709endfunc
4710
Christian Brabandt86032702024-03-31 18:38:09 +02004711" Test TextChangedI and TextChanged
zeertzjqc4226622024-04-03 22:38:07 +02004712func Test_Changed_ChangedI()
zeertzjq8eb75232024-04-01 14:46:20 +02004713 " Run this test in a terminal because it requires running the main loop.
zeertzjqc4226622024-04-03 22:38:07 +02004714 " Don't use CheckRunVimInTerminal as that will skip the test on Windows.
4715 CheckFeature terminal
4716 CheckNotGui
4717 " Starting a terminal to run Vim is always considered flaky.
4718 let g:test_is_flaky = 1
4719
Christian Brabandt86032702024-03-31 18:38:09 +02004720 call writefile(['one', 'two', 'three'], 'XTextChangedI2', 'D')
4721 let before =<< trim END
zeertzjqc4226622024-04-03 22:38:07 +02004722 set ttimeout ttimeoutlen=10
zeertzjq8eb75232024-04-01 14:46:20 +02004723 let [g:autocmd_n, g:autocmd_i] = ['','']
4724
4725 func TextChangedAutocmd(char)
4726 let g:autocmd_{tolower(a:char)} = a:char .. b:changedtick
zeertzjqc4226622024-04-03 22:38:07 +02004727 call writefile([$'{g:autocmd_n},{g:autocmd_i}'], 'XTextChangedI3')
zeertzjq8eb75232024-04-01 14:46:20 +02004728 endfunc
4729
4730 au TextChanged <buffer> :call TextChangedAutocmd('N')
4731 au TextChangedI <buffer> :call TextChangedAutocmd('I')
4732
Christian Brabandt86032702024-03-31 18:38:09 +02004733 nnoremap <CR> o<Esc>
zeertzjq4a653912024-04-04 21:33:36 +02004734 autocmd SafeState * ++once call writefile([''], 'XTextChangedI3')
Christian Brabandt86032702024-03-31 18:38:09 +02004735 END
4736
4737 call writefile(before, 'Xinit', 'D')
zeertzjqc4226622024-04-03 22:38:07 +02004738 let buf = term_start(
4739 \ GetVimCommandCleanTerm() .. '-n -S Xinit XTextChangedI2',
4740 \ {'term_rows': 10})
4741 call assert_equal('running', term_getstatus(buf))
zeertzjq8eb75232024-04-01 14:46:20 +02004742 call WaitForAssert({-> assert_true(filereadable('XTextChangedI3'))})
zeertzjqe9ff79a2024-04-05 20:07:39 +02004743 defer delete('XTextChangedI3')
zeertzjq4a653912024-04-04 21:33:36 +02004744 call WaitForAssert({-> assert_equal([''], readfile('XTextChangedI3'))})
Christian Brabandt86032702024-03-31 18:38:09 +02004745
zeertzjqc4226622024-04-03 22:38:07 +02004746 " TextChanged should trigger if a mapping enters and leaves Insert mode.
4747 call term_sendkeys(buf, "\<CR>")
zeertzjqe9ff79a2024-04-05 20:07:39 +02004748 call WaitForAssert({-> assert_equal('N4,', readfile('XTextChangedI3')->join("\n"))})
zeertzjqc4226622024-04-03 22:38:07 +02004749
4750 call term_sendkeys(buf, "i")
4751 call WaitForAssert({-> assert_match('^-- INSERT --', term_getline(buf, 10))})
zeertzjqe9ff79a2024-04-05 20:07:39 +02004752 call WaitForAssert({-> assert_equal('N4,', readfile('XTextChangedI3')->join("\n"))})
zeertzjqc4226622024-04-03 22:38:07 +02004753 " TextChangedI should trigger if change is done in Insert mode.
4754 call term_sendkeys(buf, "f")
zeertzjqe9ff79a2024-04-05 20:07:39 +02004755 call WaitForAssert({-> assert_equal('N4,I5', readfile('XTextChangedI3')->join("\n"))})
zeertzjqc4226622024-04-03 22:38:07 +02004756 call term_sendkeys(buf, "o")
zeertzjqe9ff79a2024-04-05 20:07:39 +02004757 call WaitForAssert({-> assert_equal('N4,I6', readfile('XTextChangedI3')->join("\n"))})
zeertzjqc4226622024-04-03 22:38:07 +02004758 call term_sendkeys(buf, "o")
zeertzjqe9ff79a2024-04-05 20:07:39 +02004759 call WaitForAssert({-> assert_equal('N4,I7', readfile('XTextChangedI3')->join("\n"))})
zeertzjqc4226622024-04-03 22:38:07 +02004760 " TextChanged shouldn't trigger when leaving Insert mode and TextChangedI
4761 " has been triggered.
4762 call term_sendkeys(buf, "\<Esc>")
4763 call WaitForAssert({-> assert_notmatch('^-- INSERT --', term_getline(buf, 10))})
zeertzjqe9ff79a2024-04-05 20:07:39 +02004764 call WaitForAssert({-> assert_equal('N4,I7', readfile('XTextChangedI3')->join("\n"))})
zeertzjqc4226622024-04-03 22:38:07 +02004765
4766 " TextChanged should trigger if change is done in Normal mode.
4767 call term_sendkeys(buf, "yyp")
zeertzjqe9ff79a2024-04-05 20:07:39 +02004768 call WaitForAssert({-> assert_equal('N8,I7', readfile('XTextChangedI3')->join("\n"))})
zeertzjqc4226622024-04-03 22:38:07 +02004769
4770 " TextChangedI shouldn't trigger if change isn't done in Insert mode.
4771 call term_sendkeys(buf, "i")
4772 call WaitForAssert({-> assert_match('^-- INSERT --', term_getline(buf, 10))})
zeertzjqe9ff79a2024-04-05 20:07:39 +02004773 call WaitForAssert({-> assert_equal('N8,I7', readfile('XTextChangedI3')->join("\n"))})
zeertzjqc4226622024-04-03 22:38:07 +02004774 call term_sendkeys(buf, "\<Esc>")
4775 call WaitForAssert({-> assert_notmatch('^-- INSERT --', term_getline(buf, 10))})
zeertzjqe9ff79a2024-04-05 20:07:39 +02004776 call WaitForAssert({-> assert_equal('N8,I7', readfile('XTextChangedI3')->join("\n"))})
zeertzjqc4226622024-04-03 22:38:07 +02004777
4778 " TextChangedI should trigger if change is a mix of Normal and Insert modes.
4779 func! s:validate_mixed_textchangedi(buf, keys)
4780 let buf = a:buf
4781 call term_sendkeys(buf, "ifoo")
4782 call WaitForAssert({-> assert_match('^-- INSERT --', term_getline(buf, 10))})
4783 call term_sendkeys(buf, "\<Esc>")
4784 call WaitForAssert({-> assert_notmatch('^-- INSERT --', term_getline(buf, 10))})
4785 call term_sendkeys(buf, ":let [g:autocmd_n, g:autocmd_i] = ['', '']\<CR>")
zeertzjqe9ff79a2024-04-05 20:07:39 +02004786 call writefile([], 'XTextChangedI3')
zeertzjqc4226622024-04-03 22:38:07 +02004787 call term_sendkeys(buf, a:keys)
4788 call WaitForAssert({-> assert_match('^-- INSERT --', term_getline(buf, 10))})
zeertzjqe9ff79a2024-04-05 20:07:39 +02004789 call WaitForAssert({-> assert_match('^,I\d\+', readfile('XTextChangedI3')->join("\n"))})
zeertzjqc4226622024-04-03 22:38:07 +02004790 call term_sendkeys(buf, "\<Esc>")
4791 call WaitForAssert({-> assert_notmatch('^-- INSERT --', term_getline(buf, 10))})
zeertzjqe9ff79a2024-04-05 20:07:39 +02004792 call WaitForAssert({-> assert_match('^,I\d\+', readfile('XTextChangedI3')->join("\n"))})
zeertzjqc4226622024-04-03 22:38:07 +02004793 endfunc
4794
4795 call s:validate_mixed_textchangedi(buf, "o")
4796 call s:validate_mixed_textchangedi(buf, "O")
4797 call s:validate_mixed_textchangedi(buf, "ciw")
4798 call s:validate_mixed_textchangedi(buf, "cc")
4799 call s:validate_mixed_textchangedi(buf, "C")
4800 call s:validate_mixed_textchangedi(buf, "s")
4801 call s:validate_mixed_textchangedi(buf, "S")
4802
4803 " clean up
4804 bwipe!
Christian Brabandt86032702024-03-31 18:38:09 +02004805endfunc
4806
zeertzjq5bf6c212024-03-31 18:41:27 +02004807" Test that filetype detection still works when SwapExists autocommand sets
4808" filetype in another buffer.
4809func Test_SwapExists_set_other_buf_filetype()
4810 let lines =<< trim END
4811 set nocompatible directory=.
4812 filetype on
4813
4814 let g:buf = bufnr()
4815 new
4816
4817 func SwapExists()
4818 let v:swapchoice = 'o'
4819 call setbufvar(g:buf, '&filetype', 'text')
4820 endfunc
4821
4822 func SafeState()
4823 edit <script>
4824 redir! > XftSwapExists.out
4825 set readonly? filetype?
4826 redir END
4827 qall!
4828 endfunc
4829
4830 autocmd SwapExists * ++nested call SwapExists()
4831 autocmd SafeState * ++nested ++once call SafeState()
4832 END
4833 call writefile(lines, 'XftSwapExists.vim', 'D')
4834
4835 new XftSwapExists.vim
4836 if RunVim('', '', ' -S XftSwapExists.vim')
4837 call assert_equal(
4838 \ ['', ' readonly', ' filetype=vim'],
4839 \ readfile('XftSwapExists.out'))
4840 call delete('XftSwapExists.out')
4841 endif
4842
4843 bwipe!
4844endfunc
4845
4846" Test that file is not marked as modified when SwapExists autocommand sets
4847" 'modified' in another buffer.
4848func Test_SwapExists_set_other_buf_modified()
4849 let lines =<< trim END
4850 set nocompatible directory=.
4851
4852 let g:buf = bufnr()
4853 new
4854
4855 func SwapExists()
4856 let v:swapchoice = 'o'
4857 call setbufvar(g:buf, '&modified', 1)
4858 endfunc
4859
4860 func SafeState()
4861 edit <script>
4862 redir! > XmodSwapExists.out
4863 set readonly? modified?
4864 redir END
4865 qall!
4866 endfunc
4867
4868 autocmd SwapExists * ++nested call SwapExists()
4869 autocmd SafeState * ++nested ++once call SafeState()
4870 END
4871 call writefile(lines, 'XmodSwapExists.vim', 'D')
4872
4873 new XmodSwapExists.vim
4874 if RunVim('', '', ' -S XmodSwapExists.vim')
4875 call assert_equal(
4876 \ ['', ' readonly', 'nomodified'],
4877 \ readfile('XmodSwapExists.out'))
4878 call delete('XmodSwapExists.out')
4879 endif
4880
4881 bwipe!
4882endfunc
4883
Jaehwang Jungeb80b832024-04-26 18:48:48 +02004884func Test_BufEnter_botline()
4885 set hidden
4886 call writefile(range(10), 'Xxx1', 'D')
4887 call writefile(range(20), 'Xxx2', 'D')
4888 edit Xxx1
4889 edit Xxx2
4890 au BufEnter Xxx1 call assert_true(line('w$') > 1)
4891 edit Xxx1
zeertzjq340643e2024-04-27 11:33:24 +02004892
4893 bwipe! Xxx1
4894 bwipe! Xxx2
Jaehwang Jungeb80b832024-04-26 18:48:48 +02004895 au! BufEnter Xxx1
4896 set hidden&vim
4897endfunc
4898
Shougo Matsushita83678842024-07-11 22:05:12 +02004899func Test_KeyInputPre()
4900 " Consume previous keys
4901 call feedkeys('', 'ntx')
4902
4903 " KeyInputPre can record input keys.
4904 let s:keys = []
4905 au KeyInputPre n call add(s:keys, v:char)
4906
4907 call feedkeys('jkjkjjj', 'ntx')
4908 call assert_equal(
4909 \ ['j', 'k', 'j', 'k', 'j', 'j', 'j'],
4910 \ s:keys)
4911
4912 unlet s:keys
4913 au! KeyInputPre
4914
4915 " KeyInputPre can handle multibyte.
4916 let s:keys = []
4917 au KeyInputPre * call add(s:keys, v:char)
4918 edit Xxx1
4919
4920 call feedkeys("iあ\<ESC>", 'ntx')
4921 call assert_equal(['i', "あ", "\<ESC>"], s:keys)
4922
4923 bwipe! Xxx1
4924 unlet s:keys
4925 au! KeyInputPre
4926
4927 " KeyInputPre can change input keys.
4928 au KeyInputPre i if v:char ==# 'a' | let v:char = 'b' | endif
4929 edit Xxx1
4930
4931 call feedkeys("iaabb\<ESC>", 'ntx')
4932 call assert_equal(getline('.'), 'bbbb')
4933
4934 bwipe! Xxx1
4935 au! KeyInputPre
4936
4937 " KeyInputPre returns multiple characters.
4938 au KeyInputPre i if v:char ==# 'a' | let v:char = 'cccc' | endif
4939 edit Xxx1
4940
4941 call feedkeys("iaabb\<ESC>", 'ntx')
4942 call assert_equal(getline('.'), 'ccbb')
4943
4944 bwipe! Xxx1
4945 au! KeyInputPre
4946
4947 " KeyInputPre can use special keys.
4948 au KeyInputPre i if v:char ==# 'a' | let v:char = "\<Ignore>" | endif
4949 edit Xxx1
4950
4951 call feedkeys("iaabb\<ESC>", 'ntx')
4952 call assert_equal(getline('.'), 'bb')
4953
4954 bwipe! Xxx1
4955 au! KeyInputPre
4956
4957 " Test for v:event.typed
4958 au KeyInputPre n call assert_true(v:event.typed)
4959 call feedkeys('j', 'ntx')
4960
4961 au! KeyInputPre
4962
4963 au KeyInputPre n call assert_false(v:event.typed)
4964 call feedkeys('j', 'nx')
4965
4966 au! KeyInputPre
Shougo Matsushitafcc1b572024-07-17 20:25:22 +02004967
4968 " Test for v:event.typedchar
4969 nnoremap j k
4970 au KeyInputPre n
4971 \ call assert_equal(v:event.typedchar, 'j')
4972 \ | call assert_equal(v:char, 'k')
4973 call feedkeys('j', 'tx')
4974
4975 au! KeyInputPre
Shougo Matsushita83678842024-07-11 22:05:12 +02004976endfunc
4977
Christian Brabandtfb3f9692024-08-11 20:09:17 +02004978" those commands caused null pointer access, see #15464
4979func Test_WinNewPre_crash()
4980 defer CleanUpTestAuGroup()
4981 let _cmdheight=&cmdheight
4982 augroup testing
4983 au!
4984 autocmd WinNewPre * redraw
4985 augroup END
4986 tabnew
4987 tabclose
4988 augroup testing
4989 au!
4990 autocmd WinNewPre * wincmd t
4991 augroup END
4992 tabnew
4993 tabclose
4994 augroup testing
4995 au!
4996 autocmd WinNewPre * wincmd b
4997 augroup END
4998 tabnew
4999 tabclose
5000 augroup testing
5001 au!
5002 autocmd WinNewPre * set cmdheight+=1
5003 augroup END
5004 tabnew
5005 tabclose
5006 let &cmdheight=_cmdheight
5007endfunc
5008
Christian Brabandt84e31752024-09-02 09:59:18 +02005009" The specifics of the turkish locale may
5010" cause that Vim will not treat the GuiEnter autocommand
5011" as case insensitive and instead issues an error
5012func Test_GuiEnter_Turkish_locale()
5013 try
5014 let lng = v:lang
5015 lang tr_TR.UTF-8
5016 let result = execute(':au GuiEnter')
Ken Takataeccc9272024-09-03 23:01:55 +02005017 call assert_equal(gettext("\n--- Autocommands ---"), result)
Christian Brabandt84e31752024-09-02 09:59:18 +02005018 let result = execute(':au GUIENTER')
Ken Takataeccc9272024-09-03 23:01:55 +02005019 call assert_equal(gettext("\n--- Autocommands ---"), result)
Christian Brabandt84e31752024-09-02 09:59:18 +02005020 let result = execute(':au guienter')
Ken Takataeccc9272024-09-03 23:01:55 +02005021 call assert_equal(gettext("\n--- Autocommands ---"), result)
Christian Brabandt84e31752024-09-02 09:59:18 +02005022 exe ":lang" lng
5023 catch /E197:/
5024 " can't use Turkish locale
5025 throw 'Skipped: Turkish locale not available'
5026 endtry
5027endfunc
Christian Brabandtfb3f9692024-08-11 20:09:17 +02005028
Christian Brabandt51b62382024-10-06 17:31:10 +02005029" This was using freed memory
5030func Test_autocmd_BufWinLeave_with_vsp()
5031 new
5032 let fname = 'XXXBufWinLeaveUAF.txt'
5033 let dummy = 'XXXDummy.txt'
5034 call writefile([], fname)
5035 call writefile([], dummy)
5036 defer delete(fname)
5037 defer delete(dummy)
5038 exe "e " fname
5039 vsp
5040 augroup testing
Sean Dewar6cb1c822025-05-03 18:37:27 +02005041 exe 'au BufWinLeave' fname 'e' dummy
5042 \ '| call assert_fails(''vsp' fname ''', ''E1546:'')'
Christian Brabandt51b62382024-10-06 17:31:10 +02005043 augroup END
5044 bw
5045 call CleanUpTestAuGroup()
5046 exe "bw! " .. dummy
5047endfunc
5048
Luuk van Baale15cbc12025-01-04 17:18:08 +01005049func Test_OptionSet_cmdheight()
5050 set mouse=a laststatus=2
5051 au OptionSet cmdheight :let &l:ch = v:option_new
5052
5053 resize -1
5054 call assert_equal(2, &l:ch)
5055 resize +1
5056 call assert_equal(1, &l:ch)
5057
5058 call test_setmouse(&lines - 1, 1)
5059 call feedkeys("\<LeftMouse>", 'xt')
5060 call test_setmouse(&lines - 2, 1)
5061 call feedkeys("\<LeftDrag>", 'xt')
5062 call assert_equal(2, &l:ch)
5063
5064 tabnew | resize +1
5065 call assert_equal(1, &l:ch)
5066 tabfirst
5067 call assert_equal(2, &l:ch)
5068
5069 tabonly
5070 set cmdheight& mouse& laststatus&
5071endfunc
5072
Luuk van Baalb7147f82025-02-08 18:52:39 +01005073func Test_eventignorewin()
5074 defer CleanUpTestAuGroup()
5075 augroup testing
5076 au WinEnter * :call add(g:evs, ["WinEnter", expand("<afile>")])
5077 au WinLeave * :call add(g:evs, ["WinLeave", expand("<afile>")])
5078 au BufWinEnter * :call add(g:evs, ["BufWinEnter", expand("<afile>")])
5079 augroup END
5080
5081 let g:evs = []
5082 set eventignorewin=WinLeave,WinEnter
5083 split foo
5084 call assert_equal([['BufWinEnter', 'foo']], g:evs)
5085 set eventignorewin=all
5086 edit bar
5087 call assert_equal([['BufWinEnter', 'foo']], g:evs)
5088 set eventignorewin=
5089 wincmd w
5090 call assert_equal([['BufWinEnter', 'foo'], ['WinLeave', 'bar']], g:evs)
5091
5092 only!
5093 %bwipe!
5094 set eventignorewin&
5095 unlet g:evs
5096endfunc
5097
5098func Test_WinScrolled_Resized_eiw()
5099 CheckRunVimInTerminal
5100
5101 let lines =<< trim END
5102 call setline(1, ['foo']->repeat(32))
5103 set eventignorewin=WinScrolled,WinResized
5104 split
5105 let [g:afile,g:resized,g:scrolled] = ['none',0,0]
5106 au WinScrolled * let [g:afile,g:scrolled] = [expand('<afile>'),g:scrolled+1]
5107 au WinResized * let [g:afile,g:resized] = [expand('<afile>'),g:resized+1]
5108 END
Christian Brabandtbfc77192025-02-11 20:03:10 +01005109 call writefile(lines, 'Xtest_winscrolled_eiw', 'D')
5110 let buf = RunVimInTerminal('-S Xtest_winscrolled_eiw', {'rows': 10})
Luuk van Baalb7147f82025-02-08 18:52:39 +01005111
5112 " Both windows are ignoring resize events
5113 call term_sendkeys(buf, "\<C-W>-")
5114 call TermWait(buf)
5115 call term_sendkeys(buf, ":echo g:afile g:resized g:scrolled\<CR>")
5116 call WaitForAssert({-> assert_equal('none 0 0', term_getline(buf, 10))}, 1000)
5117
5118 " And scroll events
5119 call term_sendkeys(buf, "Ggg")
5120 call TermWait(buf)
5121 call term_sendkeys(buf, ":echo g:afile g:resized g:scrolled\<CR>")
5122 call WaitForAssert({-> assert_equal('none 0 0', term_getline(buf, 10))}, 1000)
5123
5124 " Un-ignore events in second window, make first window current and resize
5125 call term_sendkeys(buf, ":set eventignorewin=\<CR>\<C-W>w\<C-W>+")
5126 call TermWait(buf)
5127 call term_sendkeys(buf, ":echo win_getid() g:afile g:resized g:scrolled\<CR>")
5128 call WaitForAssert({-> assert_equal('1000 1001 1 1', term_getline(buf, 10))}, 1000)
5129
5130 call StopVimInTerminal(buf)
5131endfunc
5132
Jim Zhou5606ca52025-03-13 21:58:25 +01005133" Test that TabClosedPre and TabClosed are triggered when closing a tab.
5134func Test_autocmd_tabclosedpre()
5135 augroup testing
5136 au TabClosedPre * call add(g:tabpagenr_pre, t:testvar)
5137 au TabClosed * call add(g:tabpagenr_post, t:testvar)
5138 augroup END
5139
5140 " Test 'tabclose' triggering
5141 let g:tabpagenr_pre = []
5142 let g:tabpagenr_post = []
5143 let t:testvar = 1
5144 tabnew
5145 let t:testvar = 2
5146 tabnew
5147 let t:testvar = 3
5148 tabnew
5149 let t:testvar = 4
5150 tabnext
5151 tabclose
5152 tabclose
5153 tabclose
5154 call assert_equal([1, 2, 3], g:tabpagenr_pre)
5155 call assert_equal([2, 3, 4], g:tabpagenr_post)
5156
5157 " Test 'tabclose {count}' triggering
5158 let g:tabpagenr_pre = []
5159 let g:tabpagenr_post = []
5160 let t:testvar = 1
5161 tabnew
5162 let t:testvar = 2
5163 tabnew
5164 let t:testvar = 3
5165 tabclose 2
5166 tabclose 2
5167 call assert_equal([2, 3], g:tabpagenr_pre)
5168 call assert_equal([3, 1], g:tabpagenr_post)
5169
5170 " Test 'tabonly' triggering
5171 let g:tabpagenr_pre = []
5172 let g:tabpagenr_post = []
5173 let t:testvar = 1
5174 tabnew
5175 let t:testvar = 2
5176 tabonly
5177 call assert_equal([1], g:tabpagenr_pre)
5178 call assert_equal([2], g:tabpagenr_post)
5179
5180 " Test 'q' and 'close' triggering (closing the last window in a tab)
5181 let g:tabpagenr_pre = []
5182 let g:tabpagenr_post = []
5183 split
5184 let t:testvar = 1
5185 tabnew
5186 let t:testvar = 2
5187 split
5188 vsplit
5189 tabnew
5190 let t:testvar = 3
5191 tabnext
5192 only
5193 quit
5194 quit
5195 close
5196 close
5197 call assert_equal([1, 2], g:tabpagenr_pre)
5198 call assert_equal([2, 3], g:tabpagenr_post)
5199
5200 func ClearAutomcdAndCreateTabs()
5201 au! TabClosedPre
5202 bw!
5203 e Z
5204 tabonly
5205 tabnew A
5206 tabnew B
5207 tabnew C
5208 endfunc
5209
5210 func GetTabs()
5211 redir => tabsout
5212 tabs
5213 redir END
5214 let tabsout = substitute(tabsout, '\n', '', 'g')
5215 let tabsout = substitute(tabsout, 'Tab page ', '', 'g')
5216 let tabsout = substitute(tabsout, ' ', '', 'g')
5217 return tabsout
5218 endfunc
5219
5220 call CleanUpTestAuGroup()
5221
5222 " Close tab in TabClosedPre autocmd
5223 call ClearAutomcdAndCreateTabs()
5224 au TabClosedPre * tabclose
zeertzjq67fe77d2025-04-20 10:21:18 +02005225 call assert_fails('tabclose', 'E1312:')
Jim Zhou5606ca52025-03-13 21:58:25 +01005226 call ClearAutomcdAndCreateTabs()
5227 au TabClosedPre * tabclose
zeertzjq67fe77d2025-04-20 10:21:18 +02005228 call assert_fails('tabclose 2', 'E1312:')
Jim Zhou5606ca52025-03-13 21:58:25 +01005229 call ClearAutomcdAndCreateTabs()
5230 au TabClosedPre * tabclose 1
zeertzjq67fe77d2025-04-20 10:21:18 +02005231 call assert_fails('tabclose', 'E1312:')
Jim Zhou5606ca52025-03-13 21:58:25 +01005232
5233 " Close other (all) tabs in TabClosedPre autocmd
5234 call ClearAutomcdAndCreateTabs()
5235 au TabClosedPre * tabonly
zeertzjq67fe77d2025-04-20 10:21:18 +02005236 call assert_fails('tabclose', 'E1312:')
Jim Zhou5606ca52025-03-13 21:58:25 +01005237 call ClearAutomcdAndCreateTabs()
5238 au TabClosedPre * tabonly
zeertzjq67fe77d2025-04-20 10:21:18 +02005239 call assert_fails('tabclose 2', 'E1312:')
Jim Zhou5606ca52025-03-13 21:58:25 +01005240 call ClearAutomcdAndCreateTabs()
5241 au TabClosedPre * tabclose 4
zeertzjq67fe77d2025-04-20 10:21:18 +02005242 call assert_fails('tabclose 2', 'E1312:')
Jim Zhou5606ca52025-03-13 21:58:25 +01005243
5244 " Open new tabs in TabClosedPre autocmd
5245 call ClearAutomcdAndCreateTabs()
5246 au TabClosedPre * tabnew D
zeertzjq67fe77d2025-04-20 10:21:18 +02005247 call assert_fails('tabclose', 'E1312:')
Jim Zhou5606ca52025-03-13 21:58:25 +01005248 call ClearAutomcdAndCreateTabs()
5249 au TabClosedPre * tabnew D
zeertzjq67fe77d2025-04-20 10:21:18 +02005250 call assert_fails('tabclose 1', 'E1312:')
Jim Zhou5606ca52025-03-13 21:58:25 +01005251
5252 " Moving the tab page in TabClosedPre autocmd
5253 call ClearAutomcdAndCreateTabs()
5254 au TabClosedPre * tabmove 0
5255 tabclose
Jim Zhoubcf66e02025-03-16 20:24:57 +01005256 call assert_equal('1>Z2A3B', GetTabs())
Jim Zhou5606ca52025-03-13 21:58:25 +01005257 call ClearAutomcdAndCreateTabs()
5258 au TabClosedPre * tabmove 0
5259 tabclose 1
5260 call assert_equal('1A2B3>C', GetTabs())
5261 tabonly
5262 call assert_equal('1>C', GetTabs())
5263
5264 " Switching tab page in TabClosedPre autocmd
5265 call ClearAutomcdAndCreateTabs()
5266 au TabClosedPre * tabnext | e Y
5267 tabclose
5268 call assert_equal('1Y2A3>B', GetTabs())
5269 call ClearAutomcdAndCreateTabs()
5270 au TabClosedPre * tabnext | e Y
5271 tabclose 1
5272 call assert_equal('1Y2B3>C', GetTabs())
5273 tabonly
5274 call assert_equal('1>Y', GetTabs())
5275
5276 " Create new windows in TabClosedPre autocmd
5277 call ClearAutomcdAndCreateTabs()
5278 au TabClosedPre * split | e X| vsplit | e Y | split | e Z
zeertzjq67fe77d2025-04-20 10:21:18 +02005279 call assert_fails('tabclose', 'E242:')
Jim Zhou5606ca52025-03-13 21:58:25 +01005280 call ClearAutomcdAndCreateTabs()
5281 au TabClosedPre * new X | new Y | new Z
zeertzjq67fe77d2025-04-20 10:21:18 +02005282 call assert_fails('tabclose 1', 'E242:')
Jim Zhou5606ca52025-03-13 21:58:25 +01005283
Jim Zhoubcf66e02025-03-16 20:24:57 +01005284 " Test directly closing the tab page with ':tabclose'
5285 au!
5286 tabonly
5287 bw!
5288 e Z
5289 au TabClosedPre * mksession!
5290 tabnew A
5291 sp
5292 tabclose
5293 source Session.vim
5294 call assert_equal('1Z2>AA', GetTabs())
5295
5296 " Test directly closing the tab page with ':tabonly'
5297 " Z is closed before A. Hence A overwrites the session.
5298 au!
5299 tabonly
5300 bw!
5301 e Z
5302 au TabClosedPre * mksession!
5303 tabnew A
5304 tabnew B
5305 tabonly
5306 source Session.vim
5307 call assert_equal('1>A2B', GetTabs())
5308
Jim Zhou5606ca52025-03-13 21:58:25 +01005309 " Clean up
Jim Zhoubcf66e02025-03-16 20:24:57 +01005310 call delete('Session.vim')
Jim Zhou5606ca52025-03-13 21:58:25 +01005311 au!
5312 only
5313 tabonly
5314 bw!
5315endfunc
5316
Sean Deward4110e02025-05-11 13:45:21 +02005317func Test_eventignorewin_non_current()
5318 defer CleanUpTestAuGroup()
5319 let s:triggered = ''
5320 augroup testing
5321 " Will set <abuf> to the buffer of the closing window.
5322 autocmd WinClosed * let s:triggered = 'WinClosed'
5323 augroup END
5324 let initial_win = win_getid()
5325
5326 new
5327 let new_buf = bufnr()
5328 " Only set for one of the windows into the new buffer.
5329 setlocal eventignorewin=all
5330 split
5331 setlocal eventignorewin=
5332 let close_winnr = winnr()
5333
5334 " Return to the window where the buffer is non-current. WinClosed should
5335 " trigger as not all windows into new_buf have 'eventignorewin' set for it.
5336 call win_gotoid(initial_win)
5337 call assert_notequal(new_buf, bufnr())
5338 execute close_winnr 'close'
5339 call assert_equal('WinClosed', s:triggered)
5340
5341 wincmd w
5342 call assert_equal(new_buf, bufnr())
5343 tab split
5344 setlocal eventignorewin=
5345 let close_winnr = win_getid()
5346
5347 " Ensure that new_buf's window in the other tabpage with 'eventignorewin'
5348 " unset allows WinClosed to run when new_buf is non-current.
5349 call win_gotoid(initial_win)
5350 call assert_notequal(new_buf, bufnr())
5351 let s:triggered = ''
5352 only!
5353 call assert_equal('WinClosed', s:triggered)
5354 call assert_equal(1, win_findbuf(new_buf)->len())
5355
5356 " Create an only window to new_buf with 'eventignorewin' set.
5357 tabonly!
5358 execute new_buf 'sbuffer'
5359 setlocal eventignorewin=all
5360 wincmd p
5361 call assert_equal(1, win_findbuf(new_buf)->len())
5362 call assert_notequal(new_buf, bufnr())
5363
5364 " Closing a window unrelated to new_buf should not block WinClosed.
5365 split
5366 let s:triggered = ''
5367 close
5368 call assert_equal('WinClosed', s:triggered)
5369 call assert_equal(1, win_findbuf(new_buf)->len())
5370
5371 " Check WinClosed is blocked when we close the only window to new_buf (that
5372 " has 'eventignorewin' set) while new_buf is non-current.
5373 call assert_notequal(new_buf, bufnr())
5374 let s:triggered = ''
5375 only!
5376 call assert_equal('', s:triggered)
5377 call assert_equal(0, win_findbuf(new_buf)->len())
5378
5379 augroup testing
5380 autocmd!
5381 autocmd BufNew * ++once let s:triggered = 'BufNew'
5382 augroup END
5383
5384 " Buffer not shown in a window, 'eventignorewin' should not block (and
5385 " can't even be set for it anyway in this case).
5386 badd foo
5387 call assert_equal('BufNew', s:triggered)
5388
5389 unlet! s:triggered
5390 %bw!
5391endfunc
5392
Sean Dewar00772822025-05-14 20:16:52 +02005393func Test_reuse_curbuf_leak()
5394 new bar
5395 let s:bar_buf = bufnr()
5396 augroup testing
5397 autocmd!
5398 autocmd BufDelete * ++once let s:triggered = 1 | execute s:bar_buf 'buffer'
5399 augroup END
5400 enew
5401 let empty_buf = bufnr()
5402
5403 " Old curbuf should be reused, firing BufDelete. As BufDelete changes curbuf,
5404 " reusing the buffer would fail and leak the ffname.
5405 edit foo
5406 call assert_equal(1, s:triggered)
5407 " Wasn't reused because the buffer changed, but buffer "foo" is still created.
5408 call assert_equal(1, bufexists(empty_buf))
5409 call assert_notequal(empty_buf, bufnr())
5410 call assert_equal('foo', bufname())
5411 call assert_equal('bar', bufname(s:bar_buf))
5412
5413 unlet! s:bar_buf s:triggered
5414 call CleanUpTestAuGroup()
5415 %bw!
5416endfunc
5417
Sean Dewar31be82e2025-05-15 19:59:37 +02005418func Test_reuse_curbuf_switch()
5419 edit asdf
5420 let s:asdf_win = win_getid()
5421 new
5422 let other_buf = bufnr()
5423 let other_win = win_getid()
5424 augroup testing
5425 autocmd!
5426 autocmd BufUnload * ++once let s:triggered = 1
5427 \| call assert_fails('split', 'E1159:')
5428 \| call win_gotoid(s:asdf_win)
5429 augroup END
5430
5431 " Check BufUnload changing curbuf does not cause buflist_new to create a new
5432 " buffer while leaving "other_buf" unloaded in a window.
5433 enew
5434 call assert_equal(1, s:triggered)
5435 call assert_equal(other_buf, bufnr())
5436 call assert_equal(other_win, win_getid())
5437 call assert_equal(1, win_findbuf(other_buf)->len())
5438 call assert_equal(1, bufloaded(other_buf))
5439
5440 unlet! s:asdf_win s:triggered
5441 call CleanUpTestAuGroup()
5442 %bw!
5443endfunc
5444
Bram Moolenaarbc2b71d2020-02-17 21:33:30 +01005445" vim: shiftwidth=2 sts=2 expandtab