blob: ae69dd62a7bd9bdfb5bf1fb1edb4867ae395aa3e [file] [log] [blame]
Bram Moolenaar14735512016-03-26 21:00:08 +01001" Tests for autocommands
2
Bram Moolenaar8c64a362018-03-23 22:39:31 +01003source shared.vim
Bram Moolenaar8c5a2782019-08-07 23:07:07 +02004source check.vim
Bram Moolenaarcadbe1b2019-09-22 21:50:09 +02005source term_util.vim
LemonBoy09371822022-04-08 15:18:45 +01006source screendump.vim
Bram Moolenaar6f2465d2022-03-22 18:13:01 +00007import './vim9.vim' as v9
Bram Moolenaar8c64a362018-03-23 22:39:31 +01008
Bram Moolenaar1e115362019-01-09 23:01:02 +01009func s:cleanup_buffers() abort
Bram Moolenaarb3435b02016-09-29 20:54:59 +020010 for bnr in range(1, bufnr('$'))
11 if bufloaded(bnr) && bufnr('%') != bnr
12 execute 'bd! ' . bnr
13 endif
14 endfor
Bram Moolenaar04f62f82017-07-19 18:18:39 +020015endfunc
Bram Moolenaarb3435b02016-09-29 20:54:59 +020016
Christian Brabandtfb3f9692024-08-11 20:09:17 +020017func CleanUpTestAuGroup()
18 augroup testing
19 au!
20 augroup END
21 augroup! testing
22endfunc
23
Bram Moolenaar14735512016-03-26 21:00:08 +010024func Test_vim_did_enter()
25 call assert_false(v:vim_did_enter)
26
27 " This script will never reach the main loop, can't check if v:vim_did_enter
28 " becomes one.
29endfunc
Bram Moolenaar40b1b542016-04-20 20:18:23 +020030
Bram Moolenaar75911162020-07-21 19:44:47 +020031" Test for the CursorHold autocmd
32func Test_CursorHold_autocmd()
33 CheckRunVimInTerminal
Bram Moolenaare1f3ab72022-09-04 21:29:08 +010034 call writefile(['one', 'two', 'three'], 'XoneTwoThree', 'D')
Bram Moolenaar75911162020-07-21 19:44:47 +020035 let before =<< trim END
36 set updatetime=10
Bram Moolenaare7cda972022-08-29 11:02:59 +010037 au CursorHold * call writefile([line('.')], 'XCHoutput', 'a')
Bram Moolenaar75911162020-07-21 19:44:47 +020038 END
Bram Moolenaare1f3ab72022-09-04 21:29:08 +010039 call writefile(before, 'XCHinit', 'D')
Bram Moolenaare7cda972022-08-29 11:02:59 +010040 let buf = RunVimInTerminal('-S XCHinit XoneTwoThree', {})
Bram Moolenaar17f67542020-08-20 18:29:13 +020041 call term_sendkeys(buf, "G")
Bram Moolenaar62cd26a2020-10-11 20:08:44 +020042 call term_wait(buf, 50)
Bram Moolenaar75911162020-07-21 19:44:47 +020043 call term_sendkeys(buf, "gg")
44 call term_wait(buf)
Bram Moolenaare7cda972022-08-29 11:02:59 +010045 call WaitForAssert({-> assert_equal(['1'], readfile('XCHoutput')[-1:-1])})
Bram Moolenaar75911162020-07-21 19:44:47 +020046 call term_sendkeys(buf, "j")
47 call term_wait(buf)
Bram Moolenaare7cda972022-08-29 11:02:59 +010048 call WaitForAssert({-> assert_equal(['1', '2'], readfile('XCHoutput')[-2:-1])})
Bram Moolenaar75911162020-07-21 19:44:47 +020049 call term_sendkeys(buf, "j")
50 call term_wait(buf)
Bram Moolenaare7cda972022-08-29 11:02:59 +010051 call WaitForAssert({-> assert_equal(['1', '2', '3'], readfile('XCHoutput')[-3:-1])})
Bram Moolenaar75911162020-07-21 19:44:47 +020052 call StopVimInTerminal(buf)
53
Bram Moolenaare7cda972022-08-29 11:02:59 +010054 call delete('XCHoutput')
Bram Moolenaar75911162020-07-21 19:44:47 +020055endfunc
56
Bram Moolenaarc67e8922016-05-24 16:07:40 +020057if has('timers')
Bram Moolenaar97b00752019-05-12 13:07:14 +020058
Bram Moolenaarc67e8922016-05-24 16:07:40 +020059 func ExitInsertMode(id)
60 call feedkeys("\<Esc>")
61 endfunc
62
63 func Test_cursorhold_insert()
zeertzjq657b31f2023-04-15 21:28:02 +010064 " depends on timing
65 let g:test_is_flaky = 1
66
Bram Moolenaarf18c4db2016-09-08 22:10:06 +020067 " Need to move the cursor.
68 call feedkeys("ggG", "xt")
69
Bram Moolenaarc67e8922016-05-24 16:07:40 +020070 let g:triggered = 0
71 au CursorHoldI * let g:triggered += 1
72 set updatetime=20
Bram Moolenaar92bb83e2021-02-03 23:04:46 +010073 call timer_start(200, 'ExitInsertMode')
Bram Moolenaarc67e8922016-05-24 16:07:40 +020074 call feedkeys('a', 'x!')
Bram Moolenaar3b014be2022-11-13 17:53:46 +000075 sleep 30m
Bram Moolenaarc67e8922016-05-24 16:07:40 +020076 call assert_equal(1, g:triggered)
Bram Moolenaar26d98212019-01-27 22:32:55 +010077 unlet g:triggered
78 au! CursorHoldI
79 set updatetime&
80 endfunc
81
82 func Test_cursorhold_insert_with_timer_interrupt()
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +020083 CheckFeature job
Bram Moolenaar26d98212019-01-27 22:32:55 +010084 " Need to move the cursor.
85 call feedkeys("ggG", "xt")
86
87 " Confirm the timer invoked in exit_cb of the job doesn't disturb
88 " CursorHoldI event.
89 let g:triggered = 0
90 au CursorHoldI * let g:triggered += 1
Bram Moolenaar62cd26a2020-10-11 20:08:44 +020091 set updatetime=100
Milly4f5681d2024-10-20 11:06:00 +020092 call job_start(has('win32') ? 'cmd /D /c echo:' : 'echo',
Bram Moolenaar62cd26a2020-10-11 20:08:44 +020093 \ {'exit_cb': {-> timer_start(200, 'ExitInsertMode')}})
Bram Moolenaar26d98212019-01-27 22:32:55 +010094 call feedkeys('a', 'x!')
95 call assert_equal(1, g:triggered)
96 unlet g:triggered
Bram Moolenaare99e8442016-07-26 20:43:40 +020097 au! CursorHoldI
Bram Moolenaaraeac9002016-09-06 22:15:08 +020098 set updatetime&
Bram Moolenaarc67e8922016-05-24 16:07:40 +020099 endfunc
100
101 func Test_cursorhold_insert_ctrl_x()
102 let g:triggered = 0
103 au CursorHoldI * let g:triggered += 1
104 set updatetime=20
105 call timer_start(100, 'ExitInsertMode')
106 " CursorHoldI does not trigger after CTRL-X
107 call feedkeys("a\<C-X>", 'x!')
108 call assert_equal(0, g:triggered)
Bram Moolenaar26d98212019-01-27 22:32:55 +0100109 unlet g:triggered
Bram Moolenaare99e8442016-07-26 20:43:40 +0200110 au! CursorHoldI
Bram Moolenaaraeac9002016-09-06 22:15:08 +0200111 set updatetime&
Bram Moolenaarc67e8922016-05-24 16:07:40 +0200112 endfunc
Bram Moolenaar97b00752019-05-12 13:07:14 +0200113
Bram Moolenaar5a9357d2021-10-03 16:22:05 +0100114 func Test_cursorhold_insert_ctrl_g_U()
115 au CursorHoldI * :
116 set updatetime=20
117 new
118 call timer_start(100, { -> feedkeys("\<Left>foo\<Esc>", 't') })
119 call feedkeys("i()\<C-g>U", 'tx!')
120 sleep 200m
121 call assert_equal('(foo)', getline(1))
122 undo
123 call assert_equal('', getline(1))
124
125 bwipe!
126 au! CursorHoldI
127 set updatetime&
128 endfunc
129
Bram Moolenaar97b00752019-05-12 13:07:14 +0200130 func Test_OptionSet_modeline()
131 call test_override('starting', 1)
132 au! OptionSet
133 augroup set_tabstop
134 au OptionSet tabstop call timer_start(1, {-> execute("echo 'Handler called'", "")})
135 augroup END
Bram Moolenaare1f3ab72022-09-04 21:29:08 +0100136 call writefile(['vim: set ts=7 sw=5 :', 'something'], 'XoptionsetModeline', 'D')
Bram Moolenaar97b00752019-05-12 13:07:14 +0200137 set modeline
138 let v:errmsg = ''
139 call assert_fails('split XoptionsetModeline', 'E12:')
140 call assert_equal(7, &ts)
141 call assert_equal('', v:errmsg)
142
143 augroup set_tabstop
144 au!
145 augroup END
146 bwipe!
147 set ts&
Bram Moolenaar97b00752019-05-12 13:07:14 +0200148 call test_override('starting', 0)
149 endfunc
150
151endif "has('timers')
Bram Moolenaar40b1b542016-04-20 20:18:23 +0200152
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200153func Test_bufunload()
Bram Moolenaarc67e8922016-05-24 16:07:40 +0200154 augroup test_bufunload_group
155 autocmd!
156 autocmd BufUnload * call add(s:li, "bufunload")
157 autocmd BufDelete * call add(s:li, "bufdelete")
158 autocmd BufWipeout * call add(s:li, "bufwipeout")
159 augroup END
Bram Moolenaar40b1b542016-04-20 20:18:23 +0200160
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100161 let s:li = []
Bram Moolenaarc67e8922016-05-24 16:07:40 +0200162 new
163 setlocal bufhidden=
164 bunload
165 call assert_equal(["bufunload", "bufdelete"], s:li)
Bram Moolenaar40b1b542016-04-20 20:18:23 +0200166
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100167 let s:li = []
Bram Moolenaarc67e8922016-05-24 16:07:40 +0200168 new
169 setlocal bufhidden=delete
170 bunload
171 call assert_equal(["bufunload", "bufdelete"], s:li)
172
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100173 let s:li = []
Bram Moolenaarc67e8922016-05-24 16:07:40 +0200174 new
175 setlocal bufhidden=unload
176 bwipeout
177 call assert_equal(["bufunload", "bufdelete", "bufwipeout"], s:li)
178
Bram Moolenaare99e8442016-07-26 20:43:40 +0200179 au! test_bufunload_group
Bram Moolenaarc67e8922016-05-24 16:07:40 +0200180 augroup! test_bufunload_group
Bram Moolenaar40b1b542016-04-20 20:18:23 +0200181endfunc
Bram Moolenaar30445cb2016-07-09 15:21:02 +0200182
183" SEGV occurs in older versions. (At least 7.4.2005 or older)
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200184func Test_autocmd_bufunload_with_tabnext()
Bram Moolenaar30445cb2016-07-09 15:21:02 +0200185 tabedit
186 tabfirst
187
188 augroup test_autocmd_bufunload_with_tabnext_group
189 autocmd!
190 autocmd BufUnload <buffer> tabnext
191 augroup END
192
193 quit
194 call assert_equal(2, tabpagenr('$'))
195
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200196 autocmd! test_autocmd_bufunload_with_tabnext_group
Bram Moolenaar30445cb2016-07-09 15:21:02 +0200197 augroup! test_autocmd_bufunload_with_tabnext_group
198 tablast
199 quit
200endfunc
Bram Moolenaarc917da42016-07-19 22:31:36 +0200201
Bram Moolenaar5ed58c72021-01-28 14:24:55 +0100202func Test_argdelete_in_next()
203 au BufNew,BufEnter,BufLeave,BufWinEnter * argdel
204 call assert_fails('next a b', 'E1156:')
205 au! BufNew,BufEnter,BufLeave,BufWinEnter *
206endfunc
207
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200208func Test_autocmd_bufwinleave_with_tabfirst()
Bram Moolenaarf9e687e2016-09-04 21:33:09 +0200209 tabedit
210 augroup sample
211 autocmd!
212 autocmd BufWinLeave <buffer> tabfirst
213 augroup END
214 call setline(1, ['a', 'b', 'c'])
215 edit! a.txt
Bram Moolenaarf18c4db2016-09-08 22:10:06 +0200216 tabclose
Bram Moolenaarf9e687e2016-09-04 21:33:09 +0200217endfunc
218
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200219" SEGV occurs in older versions. (At least 7.4.2321 or older)
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200220func Test_autocmd_bufunload_avoiding_SEGV_01()
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200221 split aa.txt
222 let lastbuf = bufnr('$')
223
224 augroup test_autocmd_bufunload
225 autocmd!
226 exe 'autocmd BufUnload <buffer> ' . (lastbuf + 1) . 'bwipeout!'
227 augroup END
228
Bram Moolenaar28ee8922020-10-28 20:20:00 +0100229 call assert_fails('edit bb.txt', 'E937:')
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200230
231 autocmd! test_autocmd_bufunload
232 augroup! test_autocmd_bufunload
233 bwipe! aa.txt
234 bwipe! bb.txt
235endfunc
236
237" SEGV occurs in older versions. (At least 7.4.2321 or older)
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200238func Test_autocmd_bufunload_avoiding_SEGV_02()
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200239 setlocal buftype=nowrite
240 let lastbuf = bufnr('$')
241
242 augroup test_autocmd_bufunload
243 autocmd!
244 exe 'autocmd BufUnload <buffer> ' . (lastbuf + 1) . 'bwipeout!'
245 augroup END
246
247 normal! i1
248 call assert_fails('edit a.txt', 'E517:')
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200249
250 autocmd! test_autocmd_bufunload
251 augroup! test_autocmd_bufunload
252 bwipe! a.txt
253endfunc
254
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100255func Test_autocmd_dummy_wipeout()
256 " prepare files
Bram Moolenaare1f3ab72022-09-04 21:29:08 +0100257 call writefile([''], 'Xdummywipetest1.txt', 'D')
258 call writefile([''], 'Xdummywipetest2.txt', 'D')
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100259 augroup test_bufunload_group
260 autocmd!
261 autocmd BufUnload * call add(s:li, "bufunload")
262 autocmd BufDelete * call add(s:li, "bufdelete")
263 autocmd BufWipeout * call add(s:li, "bufwipeout")
264 augroup END
265
266 let s:li = []
267 split Xdummywipetest1.txt
268 silent! vimgrep /notmatched/ Xdummywipetest*
269 call assert_equal(["bufunload", "bufwipeout"], s:li)
270
271 bwipeout
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100272 au! test_bufunload_group
273 augroup! test_bufunload_group
274endfunc
275
Bram Moolenaarc917da42016-07-19 22:31:36 +0200276func Test_win_tab_autocmd()
277 let g:record = []
278
Christian Brabandtfb3f9692024-08-11 20:09:17 +0200279 defer CleanUpTestAuGroup()
Bram Moolenaarc917da42016-07-19 22:31:36 +0200280 augroup testing
Sergey Vlasov1f47db72024-01-25 23:07:00 +0100281 au WinNewPre * call add(g:record, 'WinNewPre')
Bram Moolenaarc917da42016-07-19 22:31:36 +0200282 au WinNew * call add(g:record, 'WinNew')
naohiro ono23beefe2021-11-13 12:38:49 +0000283 au WinClosed * call add(g:record, 'WinClosed')
Bram Moolenaar94722c52023-01-28 19:19:03 +0000284 au WinEnter * call add(g:record, 'WinEnter')
285 au WinLeave * call add(g:record, 'WinLeave')
Bram Moolenaarc917da42016-07-19 22:31:36 +0200286 au TabNew * call add(g:record, 'TabNew')
Bram Moolenaar12c11d52016-07-19 23:13:03 +0200287 au TabClosed * call add(g:record, 'TabClosed')
Bram Moolenaarc917da42016-07-19 22:31:36 +0200288 au TabEnter * call add(g:record, 'TabEnter')
289 au TabLeave * call add(g:record, 'TabLeave')
290 augroup END
291
292 split
293 tabnew
294 close
295 close
296
297 call assert_equal([
Sergey Vlasov1f47db72024-01-25 23:07:00 +0100298 \ 'WinNewPre', 'WinLeave', 'WinNew', 'WinEnter',
Christian Brabandtfb3f9692024-08-11 20:09:17 +0200299 \ 'WinLeave', 'TabLeave', 'WinNew', 'WinEnter', 'TabNew', 'TabEnter',
naohiro ono23beefe2021-11-13 12:38:49 +0000300 \ 'WinLeave', 'TabLeave', 'WinClosed', 'TabClosed', 'WinEnter', 'TabEnter',
301 \ 'WinLeave', 'WinClosed', 'WinEnter'
Bram Moolenaarc917da42016-07-19 22:31:36 +0200302 \ ], g:record)
303
Bram Moolenaar12c11d52016-07-19 23:13:03 +0200304 let g:record = []
305 tabnew somefile
306 tabnext
307 bwipe somefile
308
309 call assert_equal([
Christian Brabandtfb3f9692024-08-11 20:09:17 +0200310 \ 'WinLeave', 'TabLeave', 'WinNew', 'WinEnter', 'TabNew', 'TabEnter',
Bram Moolenaar12c11d52016-07-19 23:13:03 +0200311 \ 'WinLeave', 'TabLeave', 'WinEnter', 'TabEnter',
naohiro ono23beefe2021-11-13 12:38:49 +0000312 \ 'WinClosed', 'TabClosed'
Bram Moolenaar12c11d52016-07-19 23:13:03 +0200313 \ ], g:record)
314
Sergey Vlasov1f47db72024-01-25 23:07:00 +0100315 let g:record = []
316 copen
317 help
318 tabnext
319 vnew
320
321 call assert_equal([
322 \ 'WinNewPre', 'WinLeave', 'WinNew', 'WinEnter',
323 \ 'WinNewPre', 'WinLeave', 'WinNew', 'WinEnter',
324 \ 'WinNewPre', 'WinLeave', 'WinNew', 'WinEnter'
325 \ ], g:record)
326
Bram Moolenaarc917da42016-07-19 22:31:36 +0200327 unlet g:record
328endfunc
Bram Moolenaare99e8442016-07-26 20:43:40 +0200329
Sergey Vlasov1f47db72024-01-25 23:07:00 +0100330func Test_WinNewPre()
331 " Test that the old window layout can be accessed before a new window is created.
332 let g:layouts_pre = []
333 let g:layouts_post = []
334 augroup testing
335 au WinNewPre * call add(g:layouts_pre, winlayout())
336 au WinNew * call add(g:layouts_post, winlayout())
337 augroup END
Christian Brabandtfb3f9692024-08-11 20:09:17 +0200338 defer CleanUpTestAuGroup()
Sergey Vlasov1f47db72024-01-25 23:07:00 +0100339 split
340 call assert_notequal(g:layouts_pre[0], g:layouts_post[0])
341 split
342 call assert_equal(g:layouts_pre[1], g:layouts_post[0])
343 call assert_notequal(g:layouts_pre[1], g:layouts_post[1])
Christian Brabandtfb3f9692024-08-11 20:09:17 +0200344 " not triggered for tabnew
Sergey Vlasov1f47db72024-01-25 23:07:00 +0100345 tabnew
Christian Brabandtfb3f9692024-08-11 20:09:17 +0200346 call assert_equal(2, len(g:layouts_pre))
Sergey Vlasov1f47db72024-01-25 23:07:00 +0100347 unlet g:layouts_pre
348 unlet g:layouts_post
349
350 " Test modifying window layout during WinNewPre throws.
351 let g:caught = 0
352 augroup testing
353 au!
354 au WinNewPre * split
355 augroup END
356 try
357 vnew
358 catch
359 let g:caught += 1
360 endtry
361 augroup testing
362 au!
363 au WinNewPre * tabnew
364 augroup END
365 try
366 vnew
367 catch
368 let g:caught += 1
369 endtry
370 augroup testing
371 au!
372 au WinNewPre * close
373 augroup END
374 try
375 vnew
376 catch
377 let g:caught += 1
378 endtry
379 augroup testing
380 au!
381 au WinNewPre * tabclose
382 augroup END
383 try
384 vnew
385 catch
386 let g:caught += 1
387 endtry
388 call assert_equal(4, g:caught)
Sergey Vlasov1f47db72024-01-25 23:07:00 +0100389 unlet g:caught
390endfunc
391
Bram Moolenaar35fc61c2022-11-22 12:40:50 +0000392func Test_WinResized()
393 CheckRunVimInTerminal
394
395 let lines =<< trim END
396 set scrolloff=0
397 call setline(1, ['111', '222'])
398 vnew
399 call setline(1, ['aaa', 'bbb'])
400 new
401 call setline(1, ['foo', 'bar'])
402
403 let g:resized = 0
404 au WinResized * let g:resized += 1
405
406 func WriteResizedEvent()
407 call writefile([json_encode(v:event)], 'XresizeEvent')
408 endfunc
409 au WinResized * call WriteResizedEvent()
410 END
411 call writefile(lines, 'Xtest_winresized', 'D')
412 let buf = RunVimInTerminal('-S Xtest_winresized', {'rows': 10})
413
414 " redraw now to avoid a redraw after the :echo command
415 call term_sendkeys(buf, ":redraw!\<CR>")
416 call TermWait(buf)
417
418 call term_sendkeys(buf, ":echo g:resized\<CR>")
419 call WaitForAssert({-> assert_match('^0$', term_getline(buf, 10))}, 1000)
420
421 " increase window height, two windows will be reported
422 call term_sendkeys(buf, "\<C-W>+")
423 call TermWait(buf)
424 call term_sendkeys(buf, ":echo g:resized\<CR>")
425 call WaitForAssert({-> assert_match('^1$', term_getline(buf, 10))}, 1000)
426
427 let event = readfile('XresizeEvent')[0]->json_decode()
428 call assert_equal({
429 \ 'windows': [1002, 1001],
430 \ }, event)
431
432 " increase window width, three windows will be reported
433 call term_sendkeys(buf, "\<C-W>>")
434 call TermWait(buf)
435 call term_sendkeys(buf, ":echo g:resized\<CR>")
436 call WaitForAssert({-> assert_match('^2$', term_getline(buf, 10))}, 1000)
437
438 let event = readfile('XresizeEvent')[0]->json_decode()
439 call assert_equal({
440 \ 'windows': [1002, 1001, 1000],
441 \ }, event)
442
443 call delete('XresizeEvent')
444 call StopVimInTerminal(buf)
445endfunc
446
LemonBoy09371822022-04-08 15:18:45 +0100447func Test_WinScrolled()
448 CheckRunVimInTerminal
449
450 let lines =<< trim END
zeertzjqd58862d2022-04-12 11:32:48 +0100451 set nowrap scrolloff=0
452 for ii in range(1, 18)
453 call setline(ii, repeat(nr2char(96 + ii), ii * 2))
454 endfor
455 let win_id = win_getid()
456 let g:matched = v:false
Bram Moolenaar35fc61c2022-11-22 12:40:50 +0000457 func WriteScrollEvent()
458 call writefile([json_encode(v:event)], 'XscrollEvent')
459 endfunc
zeertzjqd58862d2022-04-12 11:32:48 +0100460 execute 'au WinScrolled' win_id 'let g:matched = v:true'
461 let g:scrolled = 0
462 au WinScrolled * let g:scrolled += 1
463 au WinScrolled * let g:amatch = str2nr(expand('<amatch>'))
464 au WinScrolled * let g:afile = str2nr(expand('<afile>'))
Bram Moolenaar35fc61c2022-11-22 12:40:50 +0000465 au WinScrolled * call WriteScrollEvent()
LemonBoy09371822022-04-08 15:18:45 +0100466 END
Bram Moolenaare1f3ab72022-09-04 21:29:08 +0100467 call writefile(lines, 'Xtest_winscrolled', 'D')
LemonBoy09371822022-04-08 15:18:45 +0100468 let buf = RunVimInTerminal('-S Xtest_winscrolled', {'rows': 6})
469
470 call term_sendkeys(buf, ":echo g:scrolled\<CR>")
471 call WaitForAssert({-> assert_match('^0 ', term_getline(buf, 6))}, 1000)
472
473 " Scroll left/right in Normal mode.
474 call term_sendkeys(buf, "zlzh:echo g:scrolled\<CR>")
475 call WaitForAssert({-> assert_match('^2 ', term_getline(buf, 6))}, 1000)
476
Bram Moolenaar35fc61c2022-11-22 12:40:50 +0000477 let event = readfile('XscrollEvent')[0]->json_decode()
478 call assert_equal({
zeertzjq3fc84dc2022-12-07 09:17:59 +0000479 \ 'all': {'leftcol': 1, 'topline': 0, 'topfill': 0, 'width': 0, 'height': 0, 'skipcol': 0},
480 \ '1000': {'leftcol': -1, 'topline': 0, 'topfill': 0, 'width': 0, 'height': 0, 'skipcol': 0}
Bram Moolenaar35fc61c2022-11-22 12:40:50 +0000481 \ }, event)
482
LemonBoy09371822022-04-08 15:18:45 +0100483 " Scroll up/down in Normal mode.
484 call term_sendkeys(buf, "\<c-e>\<c-y>:echo g:scrolled\<CR>")
485 call WaitForAssert({-> assert_match('^4 ', term_getline(buf, 6))}, 1000)
486
Bram Moolenaar35fc61c2022-11-22 12:40:50 +0000487 let event = readfile('XscrollEvent')[0]->json_decode()
488 call assert_equal({
zeertzjq3fc84dc2022-12-07 09:17:59 +0000489 \ 'all': {'leftcol': 0, 'topline': 1, 'topfill': 0, 'width': 0, 'height': 0, 'skipcol': 0},
490 \ '1000': {'leftcol': 0, 'topline': -1, 'topfill': 0, 'width': 0, 'height': 0, 'skipcol': 0}
Bram Moolenaar35fc61c2022-11-22 12:40:50 +0000491 \ }, event)
492
LemonBoy09371822022-04-08 15:18:45 +0100493 " Scroll up/down in Insert mode.
494 call term_sendkeys(buf, "Mi\<c-x>\<c-e>\<Esc>i\<c-x>\<c-y>\<Esc>")
495 call term_sendkeys(buf, ":echo g:scrolled\<CR>")
496 call WaitForAssert({-> assert_match('^6 ', term_getline(buf, 6))}, 1000)
497
Bram Moolenaar35fc61c2022-11-22 12:40:50 +0000498 let event = readfile('XscrollEvent')[0]->json_decode()
499 call assert_equal({
zeertzjq3fc84dc2022-12-07 09:17:59 +0000500 \ 'all': {'leftcol': 0, 'topline': 1, 'topfill': 0, 'width': 0, 'height': 0, 'skipcol': 0},
501 \ '1000': {'leftcol': 0, 'topline': -1, 'topfill': 0, 'width': 0, 'height': 0, 'skipcol': 0}
Bram Moolenaar35fc61c2022-11-22 12:40:50 +0000502 \ }, event)
503
LemonBoy09371822022-04-08 15:18:45 +0100504 " Scroll the window horizontally to focus the last letter of the third line
505 " containing only six characters. Moving to the previous and shorter lines
506 " should trigger another autocommand as Vim has to make them visible.
507 call term_sendkeys(buf, "5zl2k")
508 call term_sendkeys(buf, ":echo g:scrolled\<CR>")
509 call WaitForAssert({-> assert_match('^8 ', term_getline(buf, 6))}, 1000)
510
Bram Moolenaar35fc61c2022-11-22 12:40:50 +0000511 let event = readfile('XscrollEvent')[0]->json_decode()
512 call assert_equal({
zeertzjq3fc84dc2022-12-07 09:17:59 +0000513 \ 'all': {'leftcol': 5, 'topline': 0, 'topfill': 0, 'width': 0, 'height': 0, 'skipcol': 0},
514 \ '1000': {'leftcol': -5, 'topline': 0, 'topfill': 0, 'width': 0, 'height': 0, 'skipcol': 0}
Bram Moolenaar35fc61c2022-11-22 12:40:50 +0000515 \ }, event)
516
LemonBoy09371822022-04-08 15:18:45 +0100517 " Ensure the command was triggered for the specified window ID.
518 call term_sendkeys(buf, ":echo g:matched\<CR>")
519 call WaitForAssert({-> assert_match('^v:true ', term_getline(buf, 6))}, 1000)
520
521 " Ensure the expansion of <amatch> and <afile> matches the window ID.
522 call term_sendkeys(buf, ":echo g:amatch == win_id && g:afile == win_id\<CR>")
523 call WaitForAssert({-> assert_match('^v:true ', term_getline(buf, 6))}, 1000)
524
Bram Moolenaar35fc61c2022-11-22 12:40:50 +0000525 call delete('XscrollEvent')
LemonBoy09371822022-04-08 15:18:45 +0100526 call StopVimInTerminal(buf)
LemonBoy09371822022-04-08 15:18:45 +0100527endfunc
528
LemonBoy66e13ae2022-04-21 22:52:11 +0100529func Test_WinScrolled_mouse()
530 CheckRunVimInTerminal
531
532 let lines =<< trim END
533 set nowrap scrolloff=0
534 set mouse=a term=xterm ttymouse=sgr mousetime=200 clipboard=
535 call setline(1, ['foo']->repeat(32))
536 split
537 let g:scrolled = 0
538 au WinScrolled * let g:scrolled += 1
539 END
Bram Moolenaare1f3ab72022-09-04 21:29:08 +0100540 call writefile(lines, 'Xtest_winscrolled_mouse', 'D')
LemonBoy66e13ae2022-04-21 22:52:11 +0100541 let buf = RunVimInTerminal('-S Xtest_winscrolled_mouse', {'rows': 10})
542
543 " With the upper split focused, send a scroll-down event to the unfocused one.
544 call test_setmouse(7, 1)
545 call term_sendkeys(buf, "\<ScrollWheelDown>")
546 call TermWait(buf)
547 call term_sendkeys(buf, ":echo g:scrolled\<CR>")
548 call WaitForAssert({-> assert_match('^1', term_getline(buf, 10))}, 1000)
549
550 " Again, but this time while we're in insert mode.
551 call term_sendkeys(buf, "i\<ScrollWheelDown>\<Esc>")
552 call TermWait(buf)
553 call term_sendkeys(buf, ":echo g:scrolled\<CR>")
554 call WaitForAssert({-> assert_match('^2', term_getline(buf, 10))}, 1000)
555
556 call StopVimInTerminal(buf)
LemonBoy66e13ae2022-04-21 22:52:11 +0100557endfunc
558
zeertzjqd58862d2022-04-12 11:32:48 +0100559func Test_WinScrolled_close_curwin()
560 CheckRunVimInTerminal
561
562 let lines =<< trim END
563 set nowrap scrolloff=0
564 call setline(1, ['aaa', 'bbb'])
565 vsplit
566 au WinScrolled * close
567 au VimLeave * call writefile(['123456'], 'Xtestout')
568 END
Bram Moolenaare1f3ab72022-09-04 21:29:08 +0100569 call writefile(lines, 'Xtest_winscrolled_close_curwin', 'D')
zeertzjqd58862d2022-04-12 11:32:48 +0100570 let buf = RunVimInTerminal('-S Xtest_winscrolled_close_curwin', {'rows': 6})
571
572 " This was using freed memory
573 call term_sendkeys(buf, "\<C-E>")
574 call TermWait(buf)
575 call StopVimInTerminal(buf)
576
Bram Moolenaar0a60f792022-11-19 21:18:11 +0000577 " check the startup script finished to the end
zeertzjqd58862d2022-04-12 11:32:48 +0100578 call assert_equal(['123456'], readfile('Xtestout'))
zeertzjqd58862d2022-04-12 11:32:48 +0100579 call delete('Xtestout')
580endfunc
581
Bram Moolenaar0a60f792022-11-19 21:18:11 +0000582func Test_WinScrolled_once_only()
583 CheckRunVimInTerminal
584
585 let lines =<< trim END
586 set cmdheight=2
587 call setline(1, ['aaa', 'bbb'])
588 let trigger_count = 0
589 func ShowInfo(id)
590 echo g:trigger_count g:winid winlayout()
591 endfunc
592
593 vsplit
594 split
595 " use a timer to show the info after a redraw
596 au WinScrolled * let trigger_count += 1 | let winid = expand('<amatch>') | call timer_start(100, 'ShowInfo')
597 wincmd j
598 wincmd l
599 END
600 call writefile(lines, 'Xtest_winscrolled_once', 'D')
601 let buf = RunVimInTerminal('-S Xtest_winscrolled_once', #{rows: 10, cols: 60, statusoff: 2})
602
603 call term_sendkeys(buf, "\<C-E>")
604 call VerifyScreenDump(buf, 'Test_winscrolled_once_only_1', {})
605
606 call StopVimInTerminal(buf)
607endfunc
608
Bram Moolenaar29967732022-11-20 12:11:45 +0000609" Check that WinScrolled is not triggered immediately when defined and there
610" are split windows.
611func Test_WinScrolled_not_when_defined()
612 CheckRunVimInTerminal
613
614 let lines =<< trim END
615 call setline(1, ['aaa', 'bbb'])
616 echo 'nothing happened'
617 func ShowTriggered(id)
618 echo 'triggered'
619 endfunc
620 END
621 call writefile(lines, 'Xtest_winscrolled_not', 'D')
622 let buf = RunVimInTerminal('-S Xtest_winscrolled_not', #{rows: 10, cols: 60, statusoff: 2})
623 call term_sendkeys(buf, ":split\<CR>")
624 call TermWait(buf)
625 " use a timer to show the message after redrawing
626 call term_sendkeys(buf, ":au WinScrolled * call timer_start(100, 'ShowTriggered')\<CR>")
627 call VerifyScreenDump(buf, 'Test_winscrolled_not_when_defined_1', {})
628
629 call term_sendkeys(buf, "\<C-E>")
630 call VerifyScreenDump(buf, 'Test_winscrolled_not_when_defined_2', {})
631
632 call StopVimInTerminal(buf)
633endfunc
634
zeertzjq670ab032022-08-28 19:16:15 +0100635func Test_WinScrolled_long_wrapped()
636 CheckRunVimInTerminal
637
638 let lines =<< trim END
639 set scrolloff=0
640 let height = winheight(0)
641 let width = winwidth(0)
642 let g:scrolled = 0
643 au WinScrolled * let g:scrolled += 1
644 call setline(1, repeat('foo', height * width))
645 call cursor(1, height * width)
646 END
Bram Moolenaare1f3ab72022-09-04 21:29:08 +0100647 call writefile(lines, 'Xtest_winscrolled_long_wrapped', 'D')
zeertzjq670ab032022-08-28 19:16:15 +0100648 let buf = RunVimInTerminal('-S Xtest_winscrolled_long_wrapped', {'rows': 6})
649
650 call term_sendkeys(buf, ":echo g:scrolled\<CR>")
651 call WaitForAssert({-> assert_match('^0 ', term_getline(buf, 6))}, 1000)
652
653 call term_sendkeys(buf, 'gj')
654 call term_sendkeys(buf, ":echo g:scrolled\<CR>")
655 call WaitForAssert({-> assert_match('^1 ', term_getline(buf, 6))}, 1000)
656
657 call term_sendkeys(buf, '0')
658 call term_sendkeys(buf, ":echo g:scrolled\<CR>")
659 call WaitForAssert({-> assert_match('^2 ', term_getline(buf, 6))}, 1000)
660
661 call term_sendkeys(buf, '$')
662 call term_sendkeys(buf, ":echo g:scrolled\<CR>")
663 call WaitForAssert({-> assert_match('^3 ', term_getline(buf, 6))}, 1000)
Bram Moolenaar23526d22022-12-05 15:50:41 +0000664
665 call StopVimInTerminal(buf)
zeertzjq670ab032022-08-28 19:16:15 +0100666endfunc
667
zeertzjq3fc84dc2022-12-07 09:17:59 +0000668func Test_WinScrolled_diff()
669 CheckRunVimInTerminal
670
671 let lines =<< trim END
672 set diffopt+=foldcolumn:0
673 call setline(1, ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i'])
674 vnew
675 call setline(1, ['d', 'e', 'f', 'g', 'h', 'i'])
676 windo diffthis
677 func WriteScrollEvent()
678 call writefile([json_encode(v:event)], 'XscrollEvent')
679 endfunc
680 au WinScrolled * call WriteScrollEvent()
681 END
682 call writefile(lines, 'Xtest_winscrolled_diff', 'D')
683 let buf = RunVimInTerminal('-S Xtest_winscrolled_diff', {'rows': 8})
684
685 call term_sendkeys(buf, "\<C-E>")
686 call WaitForAssert({-> assert_match('^d', term_getline(buf, 3))}, 1000)
687
688 let event = readfile('XscrollEvent')[0]->json_decode()
689 call assert_equal({
690 \ 'all': {'leftcol': 0, 'topline': 1, 'topfill': 1, 'width': 0, 'height': 0, 'skipcol': 0},
691 \ '1000': {'leftcol': 0, 'topline': 1, 'topfill': 0, 'width': 0, 'height': 0, 'skipcol': 0},
692 \ '1001': {'leftcol': 0, 'topline': 0, 'topfill': -1, 'width': 0, 'height': 0, 'skipcol': 0}
693 \ }, event)
694
695 call term_sendkeys(buf, "2\<C-E>")
696 call WaitForAssert({-> assert_match('^f', term_getline(buf, 3))}, 1000)
697
698 let event = readfile('XscrollEvent')[0]->json_decode()
699 call assert_equal({
700 \ 'all': {'leftcol': 0, 'topline': 2, 'topfill': 2, 'width': 0, 'height': 0, 'skipcol': 0},
701 \ '1000': {'leftcol': 0, 'topline': 2, 'topfill': 0, 'width': 0, 'height': 0, 'skipcol': 0},
702 \ '1001': {'leftcol': 0, 'topline': 0, 'topfill': -2, 'width': 0, 'height': 0, 'skipcol': 0}
703 \ }, event)
704
705 call term_sendkeys(buf, "\<C-E>")
706 call WaitForAssert({-> assert_match('^g', term_getline(buf, 3))}, 1000)
707
708 let event = readfile('XscrollEvent')[0]->json_decode()
709 call assert_equal({
710 \ 'all': {'leftcol': 0, 'topline': 2, 'topfill': 0, 'width': 0, 'height': 0, 'skipcol': 0},
711 \ '1000': {'leftcol': 0, 'topline': 1, 'topfill': 0, 'width': 0, 'height': 0, 'skipcol': 0},
712 \ '1001': {'leftcol': 0, 'topline': 1, 'topfill': 0, 'width': 0, 'height': 0, 'skipcol': 0}
713 \ }, event)
714
715 call term_sendkeys(buf, "2\<C-Y>")
716 call WaitForAssert({-> assert_match('^e', term_getline(buf, 3))}, 1000)
717
718 let event = readfile('XscrollEvent')[0]->json_decode()
719 call assert_equal({
720 \ 'all': {'leftcol': 0, 'topline': 3, 'topfill': 1, 'width': 0, 'height': 0, 'skipcol': 0},
721 \ '1000': {'leftcol': 0, 'topline': -2, 'topfill': 0, 'width': 0, 'height': 0, 'skipcol': 0},
722 \ '1001': {'leftcol': 0, 'topline': -1, 'topfill': 1, 'width': 0, 'height': 0, 'skipcol': 0}
723 \ }, event)
724
725 call StopVimInTerminal(buf)
Dominique Pelle541c87c2023-01-17 21:20:44 +0000726 call delete('XscrollEvent')
zeertzjq3fc84dc2022-12-07 09:17:59 +0000727endfunc
728
naohiro ono23beefe2021-11-13 12:38:49 +0000729func Test_WinClosed()
730 " Test that the pattern is matched against the closed window's ID, and both
731 " <amatch> and <afile> are set to it.
732 new
733 let winid = win_getid()
734 let g:matched = v:false
735 augroup test-WinClosed
736 autocmd!
737 execute 'autocmd WinClosed' winid 'let g:matched = v:true'
738 autocmd WinClosed * let g:amatch = str2nr(expand('<amatch>'))
739 autocmd WinClosed * let g:afile = str2nr(expand('<afile>'))
740 augroup END
741 close
742 call assert_true(g:matched)
743 call assert_equal(winid, g:amatch)
744 call assert_equal(winid, g:afile)
745
746 " Test that WinClosed is non-recursive.
747 new
748 new
749 call assert_equal(3, winnr('$'))
750 let g:triggered = 0
751 augroup test-WinClosed
752 autocmd!
753 autocmd WinClosed * let g:triggered += 1
754 autocmd WinClosed * 2 wincmd c
755 augroup END
756 close
757 call assert_equal(1, winnr('$'))
758 call assert_equal(1, g:triggered)
759
760 autocmd! test-WinClosed
761 augroup! test-WinClosed
762 unlet g:matched
763 unlet g:amatch
764 unlet g:afile
765 unlet g:triggered
766endfunc
767
Bram Moolenaarc947b9a2022-04-06 17:59:21 +0100768func Test_WinClosed_throws()
769 vnew
770 let bnr = bufnr()
771 call assert_equal(1, bufloaded(bnr))
772 augroup test-WinClosed
773 autocmd WinClosed * throw 'foo'
774 augroup END
775 try
776 close
777 catch /.*/
778 endtry
779 call assert_equal(0, bufloaded(bnr))
780
781 autocmd! test-WinClosed
782 augroup! test-WinClosed
783endfunc
784
zeertzjq6a069402022-04-07 14:08:29 +0100785func Test_WinClosed_throws_with_tabs()
786 tabnew
787 let bnr = bufnr()
788 call assert_equal(1, bufloaded(bnr))
789 augroup test-WinClosed
790 autocmd WinClosed * throw 'foo'
791 augroup END
792 try
793 close
794 catch /.*/
795 endtry
796 call assert_equal(0, bufloaded(bnr))
797
798 autocmd! test-WinClosed
799 augroup! test-WinClosed
800endfunc
801
zeertzjq62de54b2022-09-22 18:08:37 +0100802" This used to trigger WinClosed twice for the same window, and the window's
803" buffer was NULL in the second autocommand.
804func Test_WinClosed_switch_tab()
805 edit Xa
806 split Xb
807 split Xc
808 tab split
809 new
810 augroup test-WinClosed
811 autocmd WinClosed * tabprev | bwipe!
812 augroup END
813 close
814 " Check that the tabline has been fully removed
815 call assert_equal([1, 1], win_screenpos(0))
816
817 autocmd! test-WinClosed
818 augroup! test-WinClosed
819 %bwipe!
820endfunc
821
zeertzjqb2ec0da2024-03-09 15:39:27 +0100822" This used to trigger WinClosed twice for the same window, and the window's
823" buffer was NULL in the second autocommand.
824func Test_WinClosed_BufUnload_close_other()
825 tabnew
826 let g:tab = tabpagenr()
827 let g:buf = bufnr()
828 new
829 setlocal bufhidden=wipe
830 augroup test-WinClosed
831 autocmd BufUnload * ++once exe g:buf .. 'bwipe!'
832 autocmd WinClosed * call tabpagebuflist(g:tab)
833 augroup END
834 close
835
836 unlet g:tab
837 unlet g:buf
838 autocmd! test-WinClosed
839 augroup! test-WinClosed
840 %bwipe!
841endfunc
842
Bram Moolenaare99e8442016-07-26 20:43:40 +0200843func s:AddAnAutocmd()
844 augroup vimBarTest
845 au BufReadCmd * echo 'hello'
846 augroup END
847 call assert_equal(3, len(split(execute('au vimBarTest'), "\n")))
848endfunc
849
850func Test_early_bar()
851 " test that a bar is recognized before the {event}
852 call s:AddAnAutocmd()
Bram Moolenaarb8e642f2021-11-20 10:38:25 +0000853 augroup vimBarTest | au! | let done = 77 | augroup END
Bram Moolenaare99e8442016-07-26 20:43:40 +0200854 call assert_equal(1, len(split(execute('au vimBarTest'), "\n")))
Bram Moolenaarb8e642f2021-11-20 10:38:25 +0000855 call assert_equal(77, done)
Bram Moolenaare99e8442016-07-26 20:43:40 +0200856
857 call s:AddAnAutocmd()
Bram Moolenaarb8e642f2021-11-20 10:38:25 +0000858 augroup vimBarTest| au!| let done = 88 | augroup END
Bram Moolenaare99e8442016-07-26 20:43:40 +0200859 call assert_equal(1, len(split(execute('au vimBarTest'), "\n")))
Bram Moolenaarb8e642f2021-11-20 10:38:25 +0000860 call assert_equal(88, done)
Bram Moolenaare99e8442016-07-26 20:43:40 +0200861
862 " test that a bar is recognized after the {event}
863 call s:AddAnAutocmd()
Bram Moolenaarb8e642f2021-11-20 10:38:25 +0000864 augroup vimBarTest| au!BufReadCmd| let done = 99 | augroup END
Bram Moolenaare99e8442016-07-26 20:43:40 +0200865 call assert_equal(1, len(split(execute('au vimBarTest'), "\n")))
Bram Moolenaarb8e642f2021-11-20 10:38:25 +0000866 call assert_equal(99, done)
Bram Moolenaare99e8442016-07-26 20:43:40 +0200867
868 " test that a bar is recognized after the {group}
869 call s:AddAnAutocmd()
870 au! vimBarTest|echo 'hello'
871 call assert_equal(1, len(split(execute('au vimBarTest'), "\n")))
872endfunc
Bram Moolenaarf2c4c392016-07-29 20:50:24 +0200873
Bram Moolenaar5c809082016-09-01 16:21:48 +0200874func RemoveGroup()
875 autocmd! StartOK
876 augroup! StartOK
877endfunc
878
Bram Moolenaarf2c4c392016-07-29 20:50:24 +0200879func Test_augroup_warning()
880 augroup TheWarning
881 au VimEnter * echo 'entering'
882 augroup END
Bram Moolenaar5dc4e2f2020-11-25 14:15:12 +0100883 call assert_match("TheWarning.*VimEnter", execute('au VimEnter'))
Bram Moolenaarf2c4c392016-07-29 20:50:24 +0200884 redir => res
885 augroup! TheWarning
886 redir END
Bram Moolenaar5dc4e2f2020-11-25 14:15:12 +0100887 call assert_match("W19:", res)
888 call assert_match("-Deleted-.*VimEnter", execute('au VimEnter'))
Bram Moolenaarf2c4c392016-07-29 20:50:24 +0200889
890 " check "Another" does not take the pace of the deleted entry
891 augroup Another
892 augroup END
Bram Moolenaar5dc4e2f2020-11-25 14:15:12 +0100893 call assert_match("-Deleted-.*VimEnter", execute('au VimEnter'))
Bram Moolenaaraeac9002016-09-06 22:15:08 +0200894 augroup! Another
Bram Moolenaar5c809082016-09-01 16:21:48 +0200895
896 " no warning for postpone aucmd delete
897 augroup StartOK
898 au VimEnter * call RemoveGroup()
899 augroup END
Bram Moolenaar5dc4e2f2020-11-25 14:15:12 +0100900 call assert_match("StartOK.*VimEnter", execute('au VimEnter'))
Bram Moolenaar5c809082016-09-01 16:21:48 +0200901 redir => res
902 doautocmd VimEnter
903 redir END
Bram Moolenaar5dc4e2f2020-11-25 14:15:12 +0100904 call assert_notmatch("W19:", res)
Bram Moolenaarde653f02016-09-03 16:59:06 +0200905 au! VimEnter
Bram Moolenaarad48e6c2020-04-21 22:19:45 +0200906
907 call assert_fails('augroup!', 'E471:')
Bram Moolenaarf2c4c392016-07-29 20:50:24 +0200908endfunc
Bram Moolenaarb62cc362016-09-03 16:43:53 +0200909
Bram Moolenaar8d84ff12017-10-26 16:42:16 +0200910func Test_BufReadCmdHelp()
911 " This used to cause access to free memory
912 au BufReadCmd * e +h
913 help
914
Bram Moolenaar8d84ff12017-10-26 16:42:16 +0200915 au! BufReadCmd
916endfunc
917
918func Test_BufReadCmdHelpJump()
919 " This used to cause access to free memory
920 au BufReadCmd * e +h{
Bram Moolenaarcf1ba352017-10-27 00:55:04 +0200921 " } to fix highlighting
922 call assert_fails('help', 'E434:')
Bram Moolenaar8d84ff12017-10-26 16:42:16 +0200923
Bram Moolenaar8d84ff12017-10-26 16:42:16 +0200924 au! BufReadCmd
925endfunc
926
zeertzjq93f72cc2022-08-26 15:34:52 +0100927" BufReadCmd is triggered for a "nofile" buffer. Check all values.
Bram Moolenaarb1d2c812022-08-26 11:55:01 +0100928func Test_BufReadCmdNofile()
zeertzjq93f72cc2022-08-26 15:34:52 +0100929 for val in ['nofile',
930 \ 'nowrite',
931 \ 'acwrite',
932 \ 'quickfix',
933 \ 'help',
934 \ 'terminal',
935 \ 'prompt',
936 \ 'popup',
937 \ ]
938 new somefile
939 exe 'set buftype=' .. val
940 au BufReadCmd somefile call setline(1, 'triggered')
941 edit
942 call assert_equal('triggered', getline(1))
Bram Moolenaarb1d2c812022-08-26 11:55:01 +0100943
zeertzjq93f72cc2022-08-26 15:34:52 +0100944 au! BufReadCmd
945 bwipe!
946 endfor
Bram Moolenaarb1d2c812022-08-26 11:55:01 +0100947endfunc
948
Bram Moolenaarb62cc362016-09-03 16:43:53 +0200949func Test_augroup_deleted()
Bram Moolenaarde653f02016-09-03 16:59:06 +0200950 " This caused a crash before E936 was introduced
Bram Moolenaarb62cc362016-09-03 16:43:53 +0200951 augroup x
Bram Moolenaarde653f02016-09-03 16:59:06 +0200952 call assert_fails('augroup! x', 'E936:')
953 au VimEnter * echo
954 augroup end
Bram Moolenaarb62cc362016-09-03 16:43:53 +0200955 augroup! x
Bram Moolenaar5dc4e2f2020-11-25 14:15:12 +0100956 call assert_match("-Deleted-.*VimEnter", execute('au VimEnter'))
Bram Moolenaarde653f02016-09-03 16:59:06 +0200957 au! VimEnter
Bram Moolenaarb62cc362016-09-03 16:43:53 +0200958endfunc
959
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200960" Tests for autocommands on :close command.
961" This used to be in test13.
962func Test_three_windows()
Bram Moolenaarb3435b02016-09-29 20:54:59 +0200963 " Clean up buffers, because in some cases this function fails.
964 call s:cleanup_buffers()
965
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200966 " Write three files and open them, each in a window.
967 " Then go to next window, with autocommand that deletes the previous one.
968 " Do this twice, writing the file.
969 e! Xtestje1
970 call setline(1, 'testje1')
971 w
972 sp Xtestje2
973 call setline(1, 'testje2')
974 w
975 sp Xtestje3
976 call setline(1, 'testje3')
977 w
978 wincmd w
979 au WinLeave Xtestje2 bwipe
980 wincmd w
981 call assert_equal('Xtestje1', expand('%'))
982
983 au WinLeave Xtestje1 bwipe Xtestje3
984 close
985 call assert_equal('Xtestje1', expand('%'))
986
987 " Test deleting the buffer on a Unload event. If this goes wrong there
988 " will be the ATTENTION prompt.
989 e Xtestje1
990 au!
991 au! BufUnload Xtestje1 bwipe
992 call assert_fails('e Xtestje3', 'E937:')
993 call assert_equal('Xtestje3', expand('%'))
994
995 e Xtestje2
996 sp Xtestje1
997 call assert_fails('e', 'E937:')
Bram Moolenaara997b452018-04-17 23:24:06 +0200998 call assert_equal('Xtestje1', expand('%'))
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200999
1000 " Test changing buffers in a BufWipeout autocommand. If this goes wrong
1001 " there are ml_line errors and/or a Crash.
1002 au!
1003 only
1004 e Xanother
1005 e Xtestje1
1006 bwipe Xtestje2
1007 bwipe Xtestje3
1008 au BufWipeout Xtestje1 buf Xtestje1
1009 bwipe
1010 call assert_equal('Xanother', expand('%'))
1011
1012 only
1013 help
1014 wincmd w
1015 1quit
1016 call assert_equal('Xanother', expand('%'))
1017
1018 au!
Bram Moolenaar4520d442017-03-19 16:09:46 +01001019 enew
Bram Moolenaare0ab94e2016-09-04 19:50:54 +02001020 call delete('Xtestje1')
1021 call delete('Xtestje2')
1022 call delete('Xtestje3')
1023endfunc
Bram Moolenaare13b9af2017-01-13 22:01:02 +01001024
1025func Test_BufEnter()
1026 au! BufEnter
1027 au Bufenter * let val = val . '+'
1028 let g:val = ''
1029 split NewFile
1030 call assert_equal('+', g:val)
1031 bwipe!
1032 call assert_equal('++', g:val)
1033
1034 " Also get BufEnter when editing a directory
Bram Moolenaar6f14da12022-09-07 21:30:44 +01001035 call mkdir('Xbufenterdir', 'D')
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +01001036 split Xbufenterdir
Bram Moolenaare13b9af2017-01-13 22:01:02 +01001037 call assert_equal('+++', g:val)
Bram Moolenaare94260f2017-03-21 15:50:12 +01001038
1039 " On MS-Windows we can't edit the directory, make sure we wipe the right
1040 " buffer.
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +01001041 bwipe! Xbufenterdir
Bram Moolenaare13b9af2017-01-13 22:01:02 +01001042 au! BufEnter
Bram Moolenaara9b5b852022-08-26 13:16:20 +01001043
1044 " Editing a "nofile" buffer doesn't read the file but does trigger BufEnter
zeertzjq93f72cc2022-08-26 15:34:52 +01001045 " for historic reasons. Also test other 'buftype' values.
1046 for val in ['nofile',
1047 \ 'nowrite',
1048 \ 'acwrite',
1049 \ 'quickfix',
1050 \ 'help',
1051 \ 'terminal',
1052 \ 'prompt',
1053 \ 'popup',
1054 \ ]
1055 new somefile
1056 exe 'set buftype=' .. val
1057 au BufEnter somefile call setline(1, 'some text')
1058 edit
1059 call assert_equal('some text', getline(1))
1060 bwipe!
1061 au! BufEnter
1062 endfor
Bram Moolenaar9fda8152022-11-19 13:14:10 +00001063
1064 new
1065 new
1066 autocmd BufEnter * ++once close
1067 call assert_fails('close', 'E1312:')
1068
1069 au! BufEnter
1070 only
Bram Moolenaare13b9af2017-01-13 22:01:02 +01001071endfunc
Bram Moolenaar8c752bd2017-03-19 17:09:56 +01001072
1073" Closing a window might cause an endless loop
1074" E814 for older Vims
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001075func Test_autocmd_bufwipe_in_SessLoadPost()
Bram Moolenaar1d68d9b2017-10-13 22:33:32 +02001076 edit Xtest
Bram Moolenaar8c752bd2017-03-19 17:09:56 +01001077 tabnew
Bram Moolenaar1d68d9b2017-10-13 22:33:32 +02001078 file Xsomething
Bram Moolenaar8c752bd2017-03-19 17:09:56 +01001079 set noswapfile
Bram Moolenaar8c752bd2017-03-19 17:09:56 +01001080 mksession!
1081
Bram Moolenaarc79745a2019-05-20 22:12:34 +02001082 let content =<< trim [CODE]
Bram Moolenaar62cd26a2020-10-11 20:08:44 +02001083 call test_override('ui_delay', 10)
Bram Moolenaarc79745a2019-05-20 22:12:34 +02001084 set nocp noswapfile
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001085 let v:swapchoice = "e"
Bram Moolenaarc79745a2019-05-20 22:12:34 +02001086 augroup test_autocmd_sessionload
1087 autocmd!
1088 autocmd SessionLoadPost * exe bufnr("Xsomething") . "bw!"
1089 augroup END
1090
1091 func WriteErrors()
Bram Moolenaarbe9fc5b2022-09-09 17:09:35 +01001092 call writefile([execute("messages")], "XerrorsBwipe")
Bram Moolenaarc79745a2019-05-20 22:12:34 +02001093 endfunc
1094 au VimLeave * call WriteErrors()
1095 [CODE]
1096
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01001097 call writefile(content, 'Xvimrc', 'D')
Bram Moolenaar93344c22019-08-14 21:12:05 +02001098 call system(GetVimCommand('Xvimrc') .. ' --not-a-term --noplugins -S Session.vim -c cq')
Bram Moolenaarae04a602022-09-09 15:08:10 +01001099 sleep 100m
Bram Moolenaarbe9fc5b2022-09-09 17:09:35 +01001100 let errors = join(readfile('XerrorsBwipe'))
Bram Moolenaare2e40752020-09-04 21:18:46 +02001101 call assert_match('E814:', errors)
Bram Moolenaar8c752bd2017-03-19 17:09:56 +01001102
Bram Moolenaar8c752bd2017-03-19 17:09:56 +01001103 set swapfile
Bram Moolenaarbe9fc5b2022-09-09 17:09:35 +01001104 for file in ['Session.vim', 'XerrorsBwipe']
Bram Moolenaar8c752bd2017-03-19 17:09:56 +01001105 call delete(file)
1106 endfor
1107endfunc
1108
Bram Moolenaar797e63b2021-01-15 16:22:52 +01001109" Using :blast and :ball for many events caused a crash, because b_nwindows was
1110" not incremented correctly.
1111func Test_autocmd_blast_badd()
1112 let content =<< trim [CODE]
1113 au BufNew,BufAdd,BufWinEnter,BufEnter,BufLeave,BufWinLeave,BufUnload,VimEnter foo* blast
1114 edit foo1
1115 au BufNew,BufAdd,BufWinEnter,BufEnter,BufLeave,BufWinLeave,BufUnload,VimEnter foo* ball
1116 edit foo2
Bram Moolenaarbe9fc5b2022-09-09 17:09:35 +01001117 call writefile(['OK'], 'XerrorsBlast')
Bram Moolenaar797e63b2021-01-15 16:22:52 +01001118 qall
1119 [CODE]
1120
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01001121 call writefile(content, 'XblastBall', 'D')
Bram Moolenaar797e63b2021-01-15 16:22:52 +01001122 call system(GetVimCommand() .. ' --clean -S XblastBall')
Bram Moolenaarae04a602022-09-09 15:08:10 +01001123 sleep 100m
Bram Moolenaarbe9fc5b2022-09-09 17:09:35 +01001124 call assert_match('OK', readfile('XerrorsBlast')->join())
Bram Moolenaar797e63b2021-01-15 16:22:52 +01001125
Bram Moolenaarbe9fc5b2022-09-09 17:09:35 +01001126 call delete('XerrorsBlast')
Bram Moolenaar797e63b2021-01-15 16:22:52 +01001127endfunc
1128
Bram Moolenaar8c752bd2017-03-19 17:09:56 +01001129" SEGV occurs in older versions.
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001130func Test_autocmd_bufwipe_in_SessLoadPost2()
Bram Moolenaar8c752bd2017-03-19 17:09:56 +01001131 tabnew
1132 set noswapfile
Bram Moolenaar8c752bd2017-03-19 17:09:56 +01001133 mksession!
1134
Bram Moolenaarc79745a2019-05-20 22:12:34 +02001135 let content =<< trim [CODE]
1136 set nocp noswapfile
1137 function! DeleteInactiveBufs()
1138 tabfirst
1139 let tabblist = []
1140 for i in range(1, tabpagenr(''$''))
1141 call extend(tabblist, tabpagebuflist(i))
1142 endfor
1143 for b in range(1, bufnr(''$''))
1144 if bufexists(b) && buflisted(b) && (index(tabblist, b) == -1 || bufname(b) =~# ''^$'')
1145 exec ''bwipeout '' . b
1146 endif
1147 endfor
1148 echomsg "SessionLoadPost DONE"
1149 endfunction
1150 au SessionLoadPost * call DeleteInactiveBufs()
1151
1152 func WriteErrors()
Bram Moolenaarbe9fc5b2022-09-09 17:09:35 +01001153 call writefile([execute("messages")], "XerrorsPost")
Bram Moolenaarc79745a2019-05-20 22:12:34 +02001154 endfunc
1155 au VimLeave * call WriteErrors()
1156 [CODE]
1157
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01001158 call writefile(content, 'Xvimrc', 'D')
Bram Moolenaar93344c22019-08-14 21:12:05 +02001159 call system(GetVimCommand('Xvimrc') .. ' --not-a-term --noplugins -S Session.vim -c cq')
Bram Moolenaarae04a602022-09-09 15:08:10 +01001160 sleep 100m
Bram Moolenaarbe9fc5b2022-09-09 17:09:35 +01001161 let errors = join(readfile('XerrorsPost'))
Bram Moolenaare94260f2017-03-21 15:50:12 +01001162 " This probably only ever matches on unix.
1163 call assert_notmatch('Caught deadly signal SEGV', errors)
1164 call assert_match('SessionLoadPost DONE', errors)
Bram Moolenaar8c752bd2017-03-19 17:09:56 +01001165
Bram Moolenaar8c752bd2017-03-19 17:09:56 +01001166 set swapfile
Bram Moolenaarbe9fc5b2022-09-09 17:09:35 +01001167 for file in ['Session.vim', 'XerrorsPost']
Bram Moolenaar8c752bd2017-03-19 17:09:56 +01001168 call delete(file)
1169 endfor
1170endfunc
Bram Moolenaarfaf29d72017-07-09 11:07:16 +02001171
1172func Test_empty_doau()
1173 doau \|
1174endfunc
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001175
1176func s:AutoCommandOptionSet(match)
Bram Moolenaard7c96872019-06-15 17:12:48 +02001177 let template = "Option: <%s>, OldVal: <%s>, OldValLocal: <%s>, OldValGlobal: <%s>, NewVal: <%s>, Scope: <%s>, Command: <%s>\n"
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001178 let item = remove(g:options, 0)
Bram Moolenaard7c96872019-06-15 17:12:48 +02001179 let expected = printf(template, item[0], item[1], item[2], item[3], item[4], item[5], item[6])
1180 let actual = printf(template, a:match, v:option_old, v:option_oldlocal, v:option_oldglobal, v:option_new, v:option_type, v:option_command)
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001181 let g:opt = [expected, actual]
1182 "call assert_equal(expected, actual)
1183endfunc
1184
1185func Test_OptionSet()
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +02001186 CheckOption autochdir
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001187
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +02001188 badd test_autocmd.vim
1189
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001190 call test_override('starting', 1)
1191 set nocp
1192 au OptionSet * :call s:AutoCommandOptionSet(expand("<amatch>"))
1193
1194 " 1: Setting number option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001195 let g:options = [['number', 0, 0, 0, 1, 'global', 'set']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001196 set nu
1197 call assert_equal([], g:options)
1198 call assert_equal(g:opt[0], g:opt[1])
1199
1200 " 2: Setting local number option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001201 let g:options = [['number', 1, 1, '', 0, 'local', 'setlocal']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001202 setlocal nonu
1203 call assert_equal([], g:options)
1204 call assert_equal(g:opt[0], g:opt[1])
1205
1206 " 3: Setting global number option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001207 let g:options = [['number', 1, '', 1, 0, 'global', 'setglobal']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001208 setglobal nonu
1209 call assert_equal([], g:options)
1210 call assert_equal(g:opt[0], g:opt[1])
1211
1212 " 4: Setting local autoindent option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001213 let g:options = [['autoindent', 0, 0, '', 1, 'local', 'setlocal']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001214 setlocal ai
1215 call assert_equal([], g:options)
1216 call assert_equal(g:opt[0], g:opt[1])
1217
1218 " 5: Setting global autoindent option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001219 let g:options = [['autoindent', 0, '', 0, 1, 'global', 'setglobal']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001220 setglobal ai
1221 call assert_equal([], g:options)
1222 call assert_equal(g:opt[0], g:opt[1])
1223
1224 " 6: Setting global autoindent option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001225 let g:options = [['autoindent', 1, 1, 1, 0, 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001226 set ai!
1227 call assert_equal([], g:options)
1228 call assert_equal(g:opt[0], g:opt[1])
1229
1230 " 6a: Setting global autoindent option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001231 let g:options = [['autoindent', 1, 1, 0, 0, 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001232 noa setlocal ai
1233 noa setglobal noai
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001234 set ai!
1235 call assert_equal([], g:options)
1236 call assert_equal(g:opt[0], g:opt[1])
1237
1238 " Should not print anything, use :noa
1239 " 7: don't trigger OptionSet"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001240 let g:options = [['invalid', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001241 noa set nonu
Bram Moolenaard7c96872019-06-15 17:12:48 +02001242 call assert_equal([['invalid', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid']], g:options)
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001243 call assert_equal(g:opt[0], g:opt[1])
1244
1245 " 8: Setting several global list and number option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001246 let g:options = [['list', 0, 0, 0, 1, 'global', 'set'], ['number', 0, 0, 0, 1, 'global', 'set']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001247 set list nu
1248 call assert_equal([], g:options)
1249 call assert_equal(g:opt[0], g:opt[1])
1250
1251 " 9: don't trigger OptionSet"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001252 let g:options = [['invalid', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid'], ['invalid', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001253 noa set nolist nonu
Bram Moolenaard7c96872019-06-15 17:12:48 +02001254 call assert_equal([['invalid', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid'], ['invalid', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid']], g:options)
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001255 call assert_equal(g:opt[0], g:opt[1])
1256
1257 " 10: Setting global acd"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001258 let g:options = [['autochdir', 0, 0, '', 1, 'local', 'setlocal']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001259 setlocal acd
1260 call assert_equal([], g:options)
1261 call assert_equal(g:opt[0], g:opt[1])
1262
1263 " 11: Setting global autoread (also sets local value)"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001264 let g:options = [['autoread', 0, 0, 0, 1, 'global', 'set']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001265 set ar
1266 call assert_equal([], g:options)
1267 call assert_equal(g:opt[0], g:opt[1])
1268
1269 " 12: Setting local autoread"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001270 let g:options = [['autoread', 1, 1, '', 1, 'local', 'setlocal']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001271 setlocal ar
1272 call assert_equal([], g:options)
1273 call assert_equal(g:opt[0], g:opt[1])
1274
1275 " 13: Setting global autoread"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001276 let g:options = [['autoread', 1, '', 1, 0, 'global', 'setglobal']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001277 setglobal invar
1278 call assert_equal([], g:options)
1279 call assert_equal(g:opt[0], g:opt[1])
1280
1281 " 14: Setting option backspace through :let"
Luca Saccarola959ef612024-12-01 16:25:53 +01001282 let g:options = [['backspace', 'indent,eol,start', 'indent,eol,start', 'indent,eol,start', '', 'global', 'set']]
1283 let &bs = ''
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001284 call assert_equal([], g:options)
1285 call assert_equal(g:opt[0], g:opt[1])
1286
1287 " 15: Setting option backspace through setbufvar()"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001288 let g:options = [['backup', 0, 0, '', 1, 'local', 'setlocal']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001289 " try twice, first time, shouldn't trigger because option name is invalid,
1290 " second time, it should trigger
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +02001291 let bnum = bufnr('%')
Bram Moolenaare2e40752020-09-04 21:18:46 +02001292 call assert_fails("call setbufvar(bnum, '&l:bk', 1)", 'E355:')
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001293 " should trigger, use correct option name
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +02001294 call setbufvar(bnum, '&backup', 1)
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001295 call assert_equal([], g:options)
1296 call assert_equal(g:opt[0], g:opt[1])
1297
1298 " 16: Setting number option using setwinvar"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001299 let g:options = [['number', 0, 0, '', 1, 'local', 'setlocal']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001300 call setwinvar(0, '&number', 1)
1301 call assert_equal([], g:options)
1302 call assert_equal(g:opt[0], g:opt[1])
1303
1304 " 17: Setting key option, shouldn't trigger"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001305 let g:options = [['key', 'invalid', 'invalid1', 'invalid2', 'invalid3', 'invalid4', 'invalid5']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001306 setlocal key=blah
1307 setlocal key=
Bram Moolenaard7c96872019-06-15 17:12:48 +02001308 call assert_equal([['key', 'invalid', 'invalid1', 'invalid2', 'invalid3', 'invalid4', 'invalid5']], g:options)
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001309 call assert_equal(g:opt[0], g:opt[1])
1310
Bram Moolenaard7c96872019-06-15 17:12:48 +02001311
1312 " 18a: Setting string global option"
1313 let oldval = &backupext
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001314 let g:options = [['backupext', oldval, oldval, oldval, 'foo', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001315 set backupext=foo
1316 call assert_equal([], g:options)
1317 call assert_equal(g:opt[0], g:opt[1])
1318
1319 " 18b: Resetting string global option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001320 let g:options = [['backupext', 'foo', 'foo', 'foo', oldval, 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001321 set backupext&
1322 call assert_equal([], g:options)
1323 call assert_equal(g:opt[0], g:opt[1])
1324
1325 " 18c: Setting global string global option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001326 let g:options = [['backupext', oldval, '', oldval, 'bar', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001327 setglobal backupext=bar
1328 call assert_equal([], g:options)
1329 call assert_equal(g:opt[0], g:opt[1])
1330
1331 " 18d: Setting local string global option"
1332 " As this is a global option this sets the global value even though
1333 " :setlocal is used!
1334 noa set backupext& " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001335 let g:options = [['backupext', oldval, oldval, '', 'baz', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001336 setlocal backupext=baz
1337 call assert_equal([], g:options)
1338 call assert_equal(g:opt[0], g:opt[1])
1339
1340 " 18e: Setting again string global option"
1341 noa setglobal backupext=ext_global " Reset global and local value (without triggering autocmd)
1342 noa setlocal backupext=ext_local " Sets the global(!) value!
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001343 let g:options = [['backupext', 'ext_local', 'ext_local', 'ext_local', 'fuu', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001344 set backupext=fuu
1345 call assert_equal([], g:options)
1346 call assert_equal(g:opt[0], g:opt[1])
1347
1348
zeertzjqb811de52021-10-21 10:50:44 +01001349 " 19a: Setting string global-local (to buffer) option"
Bram Moolenaar8efa0262017-08-20 15:47:20 +02001350 let oldval = &tags
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001351 let g:options = [['tags', oldval, oldval, oldval, 'tagpath', 'global', 'set']]
Bram Moolenaar8efa0262017-08-20 15:47:20 +02001352 set tags=tagpath
1353 call assert_equal([], g:options)
1354 call assert_equal(g:opt[0], g:opt[1])
1355
zeertzjqb811de52021-10-21 10:50:44 +01001356 " 19b: Resetting string global-local (to buffer) option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001357 let g:options = [['tags', 'tagpath', 'tagpath', 'tagpath', oldval, 'global', 'set']]
Bram Moolenaar8efa0262017-08-20 15:47:20 +02001358 set tags&
1359 call assert_equal([], g:options)
1360 call assert_equal(g:opt[0], g:opt[1])
1361
zeertzjqb811de52021-10-21 10:50:44 +01001362 " 19c: Setting global string global-local (to buffer) option "
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001363 let g:options = [['tags', oldval, '', oldval, 'tagpath1', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001364 setglobal tags=tagpath1
1365 call assert_equal([], g:options)
1366 call assert_equal(g:opt[0], g:opt[1])
1367
zeertzjqb811de52021-10-21 10:50:44 +01001368 " 19d: Setting local string global-local (to buffer) option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001369 let g:options = [['tags', 'tagpath1', 'tagpath1', '', 'tagpath2', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001370 setlocal tags=tagpath2
1371 call assert_equal([], g:options)
1372 call assert_equal(g:opt[0], g:opt[1])
1373
zeertzjqb811de52021-10-21 10:50:44 +01001374 " 19e: Setting again string global-local (to buffer) option"
1375 " Note: v:option_old is the old global value for global-local string options
Bram Moolenaard7c96872019-06-15 17:12:48 +02001376 " but the old local value for all other kinds of options.
1377 noa setglobal tags=tag_global " Reset global and local value (without triggering autocmd)
1378 noa setlocal tags=tag_local
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001379 let g:options = [['tags', 'tag_global', 'tag_local', 'tag_global', 'tagpath', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001380 set tags=tagpath
1381 call assert_equal([], g:options)
1382 call assert_equal(g:opt[0], g:opt[1])
1383
zeertzjqb811de52021-10-21 10:50:44 +01001384 " 19f: Setting string global-local (to buffer) option to an empty string"
1385 " Note: v:option_old is the old global value for global-local string options
Bram Moolenaard7c96872019-06-15 17:12:48 +02001386 " but the old local value for all other kinds of options.
1387 noa set tags=tag_global " Reset global and local value (without triggering autocmd)
1388 noa setlocal tags= " empty string
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001389 let g:options = [['tags', 'tag_global', '', 'tag_global', 'tagpath', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001390 set tags=tagpath
1391 call assert_equal([], g:options)
1392 call assert_equal(g:opt[0], g:opt[1])
1393
1394
1395 " 20a: Setting string local (to buffer) option"
1396 let oldval = &spelllang
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001397 let g:options = [['spelllang', oldval, oldval, oldval, 'elvish,klingon', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001398 set spelllang=elvish,klingon
1399 call assert_equal([], g:options)
1400 call assert_equal(g:opt[0], g:opt[1])
1401
1402 " 20b: Resetting string local (to buffer) option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001403 let g:options = [['spelllang', 'elvish,klingon', 'elvish,klingon', 'elvish,klingon', oldval, 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001404 set spelllang&
1405 call assert_equal([], g:options)
1406 call assert_equal(g:opt[0], g:opt[1])
1407
1408 " 20c: Setting global string local (to buffer) option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001409 let g:options = [['spelllang', oldval, '', oldval, 'elvish', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001410 setglobal spelllang=elvish
1411 call assert_equal([], g:options)
1412 call assert_equal(g:opt[0], g:opt[1])
1413
1414 " 20d: Setting local string local (to buffer) option"
1415 noa set spelllang& " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001416 let g:options = [['spelllang', oldval, oldval, '', 'klingon', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001417 setlocal spelllang=klingon
1418 call assert_equal([], g:options)
1419 call assert_equal(g:opt[0], g:opt[1])
1420
1421 " 20e: Setting again string local (to buffer) option"
zeertzjqb811de52021-10-21 10:50:44 +01001422 " Note: v:option_old is the old global value for global-local string options
Bram Moolenaard7c96872019-06-15 17:12:48 +02001423 " but the old local value for all other kinds of options.
1424 noa setglobal spelllang=spellglobal " Reset global and local value (without triggering autocmd)
1425 noa setlocal spelllang=spelllocal
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001426 let g:options = [['spelllang', 'spelllocal', 'spelllocal', 'spellglobal', 'foo', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001427 set spelllang=foo
1428 call assert_equal([], g:options)
1429 call assert_equal(g:opt[0], g:opt[1])
1430
1431
zeertzjqb811de52021-10-21 10:50:44 +01001432 " 21a: Setting string global-local (to window) option"
Bram Moolenaard7c96872019-06-15 17:12:48 +02001433 let oldval = &statusline
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001434 let g:options = [['statusline', oldval, oldval, oldval, 'foo', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001435 set statusline=foo
1436 call assert_equal([], g:options)
1437 call assert_equal(g:opt[0], g:opt[1])
1438
zeertzjqb811de52021-10-21 10:50:44 +01001439 " 21b: Resetting string global-local (to window) option"
1440 " Note: v:option_old is the old global value for global-local string options
Bram Moolenaard7c96872019-06-15 17:12:48 +02001441 " but the old local value for all other kinds of options.
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001442 let g:options = [['statusline', 'foo', 'foo', 'foo', oldval, 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001443 set statusline&
1444 call assert_equal([], g:options)
1445 call assert_equal(g:opt[0], g:opt[1])
1446
zeertzjqb811de52021-10-21 10:50:44 +01001447 " 21c: Setting global string global-local (to window) option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001448 let g:options = [['statusline', oldval, '', oldval, 'bar', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001449 setglobal statusline=bar
1450 call assert_equal([], g:options)
1451 call assert_equal(g:opt[0], g:opt[1])
1452
zeertzjqb811de52021-10-21 10:50:44 +01001453 " 21d: Setting local string global-local (to window) option"
Bram Moolenaard7c96872019-06-15 17:12:48 +02001454 noa set statusline& " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001455 let g:options = [['statusline', oldval, oldval, '', 'baz', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001456 setlocal statusline=baz
1457 call assert_equal([], g:options)
1458 call assert_equal(g:opt[0], g:opt[1])
1459
zeertzjqb811de52021-10-21 10:50:44 +01001460 " 21e: Setting again string global-local (to window) option"
1461 " Note: v:option_old is the old global value for global-local string options
Bram Moolenaard7c96872019-06-15 17:12:48 +02001462 " but the old local value for all other kinds of options.
1463 noa setglobal statusline=bar " Reset global and local value (without triggering autocmd)
1464 noa setlocal statusline=baz
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001465 let g:options = [['statusline', 'bar', 'baz', 'bar', 'foo', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001466 set statusline=foo
1467 call assert_equal([], g:options)
1468 call assert_equal(g:opt[0], g:opt[1])
1469
1470
1471 " 22a: Setting string local (to window) option"
1472 let oldval = &foldignore
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001473 let g:options = [['foldignore', oldval, oldval, oldval, 'fo', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001474 set foldignore=fo
1475 call assert_equal([], g:options)
1476 call assert_equal(g:opt[0], g:opt[1])
1477
1478 " 22b: Resetting string local (to window) option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001479 let g:options = [['foldignore', 'fo', 'fo', 'fo', oldval, 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001480 set foldignore&
1481 call assert_equal([], g:options)
1482 call assert_equal(g:opt[0], g:opt[1])
1483
1484 " 22c: Setting global string local (to window) option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001485 let g:options = [['foldignore', oldval, '', oldval, 'bar', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001486 setglobal foldignore=bar
1487 call assert_equal([], g:options)
1488 call assert_equal(g:opt[0], g:opt[1])
1489
1490 " 22d: Setting local string local (to window) option"
1491 noa set foldignore& " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001492 let g:options = [['foldignore', oldval, oldval, '', 'baz', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001493 setlocal foldignore=baz
1494 call assert_equal([], g:options)
1495 call assert_equal(g:opt[0], g:opt[1])
1496
1497 " 22e: Setting again string local (to window) option"
1498 noa setglobal foldignore=glob " Reset global and local value (without triggering autocmd)
1499 noa setlocal foldignore=loc
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001500 let g:options = [['foldignore', 'loc', 'loc', 'glob', 'fo', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001501 set foldignore=fo
1502 call assert_equal([], g:options)
1503 call assert_equal(g:opt[0], g:opt[1])
1504
1505
zeertzjqb811de52021-10-21 10:50:44 +01001506 " 23a: Setting global number global option"
Bram Moolenaard7c96872019-06-15 17:12:48 +02001507 noa setglobal cmdheight=8 " Reset global and local value (without triggering autocmd)
1508 noa setlocal cmdheight=1 " Sets the global(!) value!
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001509 let g:options = [['cmdheight', '1', '', '1', '2', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001510 setglobal cmdheight=2
1511 call assert_equal([], g:options)
1512 call assert_equal(g:opt[0], g:opt[1])
1513
1514 " 23b: Setting local number global option"
1515 noa setglobal cmdheight=8 " Reset global and local value (without triggering autocmd)
1516 noa setlocal cmdheight=1 " Sets the global(!) value!
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001517 let g:options = [['cmdheight', '1', '1', '', '2', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001518 setlocal cmdheight=2
1519 call assert_equal([], g:options)
1520 call assert_equal(g:opt[0], g:opt[1])
1521
1522 " 23c: Setting again number global option"
1523 noa setglobal cmdheight=8 " Reset global and local value (without triggering autocmd)
1524 noa setlocal cmdheight=1 " Sets the global(!) value!
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001525 let g:options = [['cmdheight', '1', '1', '1', '2', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001526 set cmdheight=2
1527 call assert_equal([], g:options)
1528 call assert_equal(g:opt[0], g:opt[1])
1529
1530 " 23d: Setting again number global option"
1531 noa set cmdheight=8 " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001532 let g:options = [['cmdheight', '8', '8', '8', '2', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001533 set cmdheight=2
1534 call assert_equal([], g:options)
1535 call assert_equal(g:opt[0], g:opt[1])
1536
1537
1538 " 24a: Setting global number global-local (to buffer) option"
1539 noa setglobal undolevels=8 " Reset global and local value (without triggering autocmd)
1540 noa setlocal undolevels=1
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001541 let g:options = [['undolevels', '8', '', '8', '2', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001542 setglobal undolevels=2
1543 call assert_equal([], g:options)
1544 call assert_equal(g:opt[0], g:opt[1])
1545
1546 " 24b: Setting local number global-local (to buffer) option"
1547 noa setglobal undolevels=8 " Reset global and local value (without triggering autocmd)
1548 noa setlocal undolevels=1
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001549 let g:options = [['undolevels', '1', '1', '', '2', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001550 setlocal undolevels=2
1551 call assert_equal([], g:options)
1552 call assert_equal(g:opt[0], g:opt[1])
1553
1554 " 24c: Setting again number global-local (to buffer) option"
1555 noa setglobal undolevels=8 " Reset global and local value (without triggering autocmd)
1556 noa setlocal undolevels=1
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001557 let g:options = [['undolevels', '1', '1', '8', '2', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001558 set undolevels=2
1559 call assert_equal([], g:options)
1560 call assert_equal(g:opt[0], g:opt[1])
1561
1562 " 24d: Setting again global number global-local (to buffer) option"
1563 noa set undolevels=8 " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001564 let g:options = [['undolevels', '8', '8', '8', '2', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001565 set undolevels=2
1566 call assert_equal([], g:options)
1567 call assert_equal(g:opt[0], g:opt[1])
1568
1569
1570 " 25a: Setting global number local (to buffer) option"
1571 noa setglobal wrapmargin=8 " Reset global and local value (without triggering autocmd)
1572 noa setlocal wrapmargin=1
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001573 let g:options = [['wrapmargin', '8', '', '8', '2', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001574 setglobal wrapmargin=2
1575 call assert_equal([], g:options)
1576 call assert_equal(g:opt[0], g:opt[1])
1577
1578 " 25b: Setting local number local (to buffer) option"
1579 noa setglobal wrapmargin=8 " Reset global and local value (without triggering autocmd)
1580 noa setlocal wrapmargin=1
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001581 let g:options = [['wrapmargin', '1', '1', '', '2', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001582 setlocal wrapmargin=2
1583 call assert_equal([], g:options)
1584 call assert_equal(g:opt[0], g:opt[1])
1585
1586 " 25c: Setting again number local (to buffer) option"
1587 noa setglobal wrapmargin=8 " Reset global and local value (without triggering autocmd)
1588 noa setlocal wrapmargin=1
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001589 let g:options = [['wrapmargin', '1', '1', '8', '2', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001590 set wrapmargin=2
1591 call assert_equal([], g:options)
1592 call assert_equal(g:opt[0], g:opt[1])
1593
1594 " 25d: Setting again global number local (to buffer) option"
1595 noa set wrapmargin=8 " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001596 let g:options = [['wrapmargin', '8', '8', '8', '2', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001597 set wrapmargin=2
1598 call assert_equal([], g:options)
1599 call assert_equal(g:opt[0], g:opt[1])
1600
1601
1602 " 26: Setting number global-local (to window) option.
1603 " Such option does currently not exist.
1604
1605
1606 " 27a: Setting global number local (to window) option"
1607 noa setglobal foldcolumn=8 " Reset global and local value (without triggering autocmd)
1608 noa setlocal foldcolumn=1
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001609 let g:options = [['foldcolumn', '8', '', '8', '2', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001610 setglobal foldcolumn=2
1611 call assert_equal([], g:options)
1612 call assert_equal(g:opt[0], g:opt[1])
1613
1614 " 27b: Setting local number local (to window) option"
1615 noa setglobal foldcolumn=8 " Reset global and local value (without triggering autocmd)
1616 noa setlocal foldcolumn=1
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001617 let g:options = [['foldcolumn', '1', '1', '', '2', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001618 setlocal foldcolumn=2
1619 call assert_equal([], g:options)
1620 call assert_equal(g:opt[0], g:opt[1])
1621
1622 " 27c: Setting again number local (to window) option"
1623 noa setglobal foldcolumn=8 " Reset global and local value (without triggering autocmd)
1624 noa setlocal foldcolumn=1
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001625 let g:options = [['foldcolumn', '1', '1', '8', '2', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001626 set foldcolumn=2
1627 call assert_equal([], g:options)
1628 call assert_equal(g:opt[0], g:opt[1])
1629
zeertzjqb811de52021-10-21 10:50:44 +01001630 " 27d: Setting again global number local (to window) option"
Bram Moolenaard7c96872019-06-15 17:12:48 +02001631 noa set foldcolumn=8 " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001632 let g:options = [['foldcolumn', '8', '8', '8', '2', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001633 set foldcolumn=2
1634 call assert_equal([], g:options)
1635 call assert_equal(g:opt[0], g:opt[1])
1636
1637
1638 " 28a: Setting global boolean global option"
1639 noa setglobal nowrapscan " Reset global and local value (without triggering autocmd)
1640 noa setlocal wrapscan " Sets the global(!) value!
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001641 let g:options = [['wrapscan', '1', '', '1', '0', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001642 setglobal nowrapscan
1643 call assert_equal([], g:options)
1644 call assert_equal(g:opt[0], g:opt[1])
1645
1646 " 28b: Setting local boolean global option"
1647 noa setglobal nowrapscan " Reset global and local value (without triggering autocmd)
1648 noa setlocal wrapscan " Sets the global(!) value!
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001649 let g:options = [['wrapscan', '1', '1', '', '0', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001650 setlocal nowrapscan
1651 call assert_equal([], g:options)
1652 call assert_equal(g:opt[0], g:opt[1])
1653
1654 " 28c: Setting again boolean global option"
1655 noa setglobal nowrapscan " Reset global and local value (without triggering autocmd)
1656 noa setlocal wrapscan " Sets the global(!) value!
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001657 let g:options = [['wrapscan', '1', '1', '1', '0', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001658 set nowrapscan
1659 call assert_equal([], g:options)
1660 call assert_equal(g:opt[0], g:opt[1])
1661
1662 " 28d: Setting again global boolean global option"
1663 noa set nowrapscan " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001664 let g:options = [['wrapscan', '0', '0', '0', '1', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001665 set wrapscan
1666 call assert_equal([], g:options)
1667 call assert_equal(g:opt[0], g:opt[1])
1668
1669
1670 " 29a: Setting global boolean global-local (to buffer) option"
1671 noa setglobal noautoread " Reset global and local value (without triggering autocmd)
1672 noa setlocal autoread
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001673 let g:options = [['autoread', '0', '', '0', '1', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001674 setglobal autoread
1675 call assert_equal([], g:options)
1676 call assert_equal(g:opt[0], g:opt[1])
1677
1678 " 29b: Setting local boolean global-local (to buffer) option"
1679 noa setglobal noautoread " Reset global and local value (without triggering autocmd)
1680 noa setlocal autoread
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001681 let g:options = [['autoread', '1', '1', '', '0', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001682 setlocal noautoread
1683 call assert_equal([], g:options)
1684 call assert_equal(g:opt[0], g:opt[1])
1685
1686 " 29c: Setting again boolean global-local (to buffer) option"
1687 noa setglobal noautoread " Reset global and local value (without triggering autocmd)
1688 noa setlocal autoread
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001689 let g:options = [['autoread', '1', '1', '0', '1', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001690 set autoread
1691 call assert_equal([], g:options)
1692 call assert_equal(g:opt[0], g:opt[1])
1693
1694 " 29d: Setting again global boolean global-local (to buffer) option"
1695 noa set noautoread " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001696 let g:options = [['autoread', '0', '0', '0', '1', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001697 set autoread
1698 call assert_equal([], g:options)
1699 call assert_equal(g:opt[0], g:opt[1])
1700
1701
1702 " 30a: Setting global boolean local (to buffer) option"
1703 noa setglobal nocindent " Reset global and local value (without triggering autocmd)
1704 noa setlocal cindent
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001705 let g:options = [['cindent', '0', '', '0', '1', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001706 setglobal cindent
1707 call assert_equal([], g:options)
1708 call assert_equal(g:opt[0], g:opt[1])
1709
1710 " 30b: Setting local boolean local (to buffer) option"
1711 noa setglobal nocindent " Reset global and local value (without triggering autocmd)
1712 noa setlocal cindent
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001713 let g:options = [['cindent', '1', '1', '', '0', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001714 setlocal nocindent
1715 call assert_equal([], g:options)
1716 call assert_equal(g:opt[0], g:opt[1])
1717
1718 " 30c: Setting again boolean local (to buffer) option"
1719 noa setglobal nocindent " Reset global and local value (without triggering autocmd)
1720 noa setlocal cindent
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001721 let g:options = [['cindent', '1', '1', '0', '1', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001722 set cindent
1723 call assert_equal([], g:options)
1724 call assert_equal(g:opt[0], g:opt[1])
1725
1726 " 30d: Setting again global boolean local (to buffer) option"
1727 noa set nocindent " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001728 let g:options = [['cindent', '0', '0', '0', '1', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001729 set cindent
1730 call assert_equal([], g:options)
1731 call assert_equal(g:opt[0], g:opt[1])
1732
1733
1734 " 31: Setting boolean global-local (to window) option
1735 " Currently no such option exists.
1736
1737
1738 " 32a: Setting global boolean local (to window) option"
1739 noa setglobal nocursorcolumn " Reset global and local value (without triggering autocmd)
1740 noa setlocal cursorcolumn
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001741 let g:options = [['cursorcolumn', '0', '', '0', '1', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001742 setglobal cursorcolumn
1743 call assert_equal([], g:options)
1744 call assert_equal(g:opt[0], g:opt[1])
1745
1746 " 32b: Setting local boolean local (to window) option"
1747 noa setglobal nocursorcolumn " Reset global and local value (without triggering autocmd)
1748 noa setlocal cursorcolumn
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001749 let g:options = [['cursorcolumn', '1', '1', '', '0', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001750 setlocal nocursorcolumn
1751 call assert_equal([], g:options)
1752 call assert_equal(g:opt[0], g:opt[1])
1753
1754 " 32c: Setting again boolean local (to window) option"
1755 noa setglobal nocursorcolumn " Reset global and local value (without triggering autocmd)
1756 noa setlocal cursorcolumn
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001757 let g:options = [['cursorcolumn', '1', '1', '0', '1', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001758 set cursorcolumn
1759 call assert_equal([], g:options)
1760 call assert_equal(g:opt[0], g:opt[1])
1761
1762 " 32d: Setting again global boolean local (to window) option"
1763 noa set nocursorcolumn " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001764 let g:options = [['cursorcolumn', '0', '0', '0', '1', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001765 set cursorcolumn
1766 call assert_equal([], g:options)
1767 call assert_equal(g:opt[0], g:opt[1])
1768
1769
Bram Moolenaar1bc353b2019-09-01 14:45:28 +02001770 " 33: Test autocommands when an option value is converted internally.
Bram Moolenaard7c96872019-06-15 17:12:48 +02001771 noa set backspace=1 " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001772 let g:options = [['backspace', 'indent,eol', 'indent,eol', 'indent,eol', '2', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001773 set backspace=2
1774 call assert_equal([], g:options)
1775 call assert_equal(g:opt[0], g:opt[1])
1776
1777
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001778 " Cleanup
1779 au! OptionSet
Bram Moolenaar0331faf2019-06-15 18:40:37 +02001780 " set tags&
Bram Moolenaard7c96872019-06-15 17:12:48 +02001781 for opt in ['nu', 'ai', 'acd', 'ar', 'bs', 'backup', 'cul', 'cp', 'backupext', 'tags', 'spelllang', 'statusline', 'foldignore', 'cmdheight', 'undolevels', 'wrapmargin', 'foldcolumn', 'wrapscan', 'autoread', 'cindent', 'cursorcolumn']
Bram Moolenaar91d2e782018-08-07 19:05:01 +02001782 exe printf(":set %s&vim", opt)
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001783 endfor
1784 call test_override('starting', 0)
1785 delfunc! AutoCommandOptionSet
1786endfunc
1787
1788func Test_OptionSet_diffmode()
1789 call test_override('starting', 1)
Bram Moolenaar26d98212019-01-27 22:32:55 +01001790 " 18: Changing an option when entering diff mode
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001791 new
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001792 au OptionSet diff :let &l:cul = v:option_new
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001793
1794 call setline(1, ['buffer 1', 'line2', 'line3', 'line4'])
1795 call assert_equal(0, &l:cul)
1796 diffthis
1797 call assert_equal(1, &l:cul)
1798
1799 vnew
1800 call setline(1, ['buffer 2', 'line 2', 'line 3', 'line4'])
1801 call assert_equal(0, &l:cul)
1802 diffthis
1803 call assert_equal(1, &l:cul)
1804
1805 diffoff
1806 call assert_equal(0, &l:cul)
1807 call assert_equal(1, getwinvar(2, '&l:cul'))
1808 bw!
1809
1810 call assert_equal(1, &l:cul)
1811 diffoff!
1812 call assert_equal(0, &l:cul)
1813 call assert_equal(0, getwinvar(1, '&l:cul'))
1814 bw!
1815
1816 " Cleanup
1817 au! OptionSet
1818 call test_override('starting', 0)
1819endfunc
1820
1821func Test_OptionSet_diffmode_close()
1822 call test_override('starting', 1)
1823 " 19: Try to close the current window when entering diff mode
1824 " should not segfault
1825 new
1826 au OptionSet diff close
1827
1828 call setline(1, ['buffer 1', 'line2', 'line3', 'line4'])
Bram Moolenaare2e40752020-09-04 21:18:46 +02001829 call assert_fails(':diffthis', 'E788:')
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001830 call assert_equal(1, &diff)
1831 vnew
1832 call setline(1, ['buffer 2', 'line 2', 'line 3', 'line4'])
Bram Moolenaare2e40752020-09-04 21:18:46 +02001833 call assert_fails(':diffthis', 'E788:')
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001834 call assert_equal(1, &diff)
Bram Moolenaara9aa86f2019-11-10 21:25:45 +01001835 set diffopt-=closeoff
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001836 bw!
Bram Moolenaare2e40752020-09-04 21:18:46 +02001837 call assert_fails(':diffoff!', 'E788:')
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001838 bw!
1839
1840 " Cleanup
1841 au! OptionSet
1842 call test_override('starting', 0)
1843 "delfunc! AutoCommandOptionSet
1844endfunc
Bram Moolenaar4a137b42017-08-04 22:37:11 +02001845
1846" Test for Bufleave autocommand that deletes the buffer we are about to edit.
1847func Test_BufleaveWithDelete()
Bram Moolenaare7cda972022-08-29 11:02:59 +01001848 new | edit XbufLeave1
Bram Moolenaar4a137b42017-08-04 22:37:11 +02001849
1850 augroup test_bufleavewithdelete
1851 autocmd!
Bram Moolenaare7cda972022-08-29 11:02:59 +01001852 autocmd BufLeave XbufLeave1 bwipe XbufLeave2
Bram Moolenaar4a137b42017-08-04 22:37:11 +02001853 augroup END
1854
Bram Moolenaare7cda972022-08-29 11:02:59 +01001855 call assert_fails('edit XbufLeave2', 'E143:')
1856 call assert_equal('XbufLeave1', bufname('%'))
Bram Moolenaar4a137b42017-08-04 22:37:11 +02001857
Bram Moolenaare7cda972022-08-29 11:02:59 +01001858 autocmd! test_bufleavewithdelete BufLeave XbufLeave1
Bram Moolenaar4a137b42017-08-04 22:37:11 +02001859 augroup! test_bufleavewithdelete
1860
1861 new
Bram Moolenaare7cda972022-08-29 11:02:59 +01001862 bwipe! XbufLeave1
Bram Moolenaar4a137b42017-08-04 22:37:11 +02001863endfunc
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +02001864
1865" Test for autocommand that changes the buffer list, when doing ":ball".
1866func Test_Acmd_BufAll()
1867 enew!
1868 %bwipe!
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01001869 call writefile(['Test file Xxx1'], 'Xxx1', 'D')
1870 call writefile(['Test file Xxx2'], 'Xxx2', 'D')
1871 call writefile(['Test file Xxx3'], 'Xxx3', 'D')
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +02001872
1873 " Add three files to the buffer list
1874 split Xxx1
1875 close
1876 split Xxx2
1877 close
1878 split Xxx3
1879 close
1880
1881 " Wipe the buffer when the buffer is opened
1882 au BufReadPost Xxx2 bwipe
1883
1884 call append(0, 'Test file Xxx4')
1885 ball
1886
1887 call assert_equal(2, winnr('$'))
1888 call assert_equal('Xxx1', bufname(winbufnr(winnr('$'))))
1889 wincmd t
1890
1891 au! BufReadPost
1892 %bwipe!
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +02001893 enew! | only
1894endfunc
1895
1896" Test for autocommand that changes current buffer on BufEnter event.
1897" Check if modelines are interpreted for the correct buffer.
1898func Test_Acmd_BufEnter()
1899 %bwipe!
1900 call writefile(['start of test file Xxx1',
1901 \ "\<Tab>this is a test",
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01001902 \ 'end of test file Xxx1'], 'Xxx1', 'D')
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +02001903 call writefile(['start of test file Xxx2',
1904 \ 'vim: set noai :',
1905 \ "\<Tab>this is a test",
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01001906 \ 'end of test file Xxx2'], 'Xxx2', 'D')
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +02001907
1908 au BufEnter Xxx2 brew
1909 set ai modeline modelines=3
1910 edit Xxx1
1911 " edit Xxx2, autocmd will do :brew
1912 edit Xxx2
1913 exe "normal G?this is a\<CR>"
1914 " Append text with autoindent to this file
1915 normal othis should be auto-indented
1916 call assert_equal("\<Tab>this should be auto-indented", getline('.'))
1917 call assert_equal(3, line('.'))
1918 " Remove autocmd and edit Xxx2 again
1919 au! BufEnter Xxx2
1920 buf! Xxx2
1921 exe "normal G?this is a\<CR>"
1922 " append text without autoindent to Xxx
1923 normal othis should be in column 1
1924 call assert_equal("this should be in column 1", getline('.'))
1925 call assert_equal(4, line('.'))
1926
1927 %bwipe!
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +02001928 set ai&vim modeline&vim modelines&vim
1929endfunc
1930
1931" Test for issue #57
1932" do not move cursor on <c-o> when autoindent is set
1933func Test_ai_CTRL_O()
1934 enew!
1935 set ai
1936 let save_fo = &fo
1937 set fo+=r
1938 exe "normal o# abcdef\<Esc>2hi\<CR>\<C-O>d0\<Esc>"
1939 exe "normal o# abcdef\<Esc>2hi\<C-O>d0\<Esc>"
1940 call assert_equal(['# abc', 'def', 'def'], getline(2, 4))
1941
1942 set ai&vim
1943 let &fo = save_fo
1944 enew!
1945endfunc
1946
1947" Test for autocommand that deletes the current buffer on BufLeave event.
1948" Also test deleting the last buffer, should give a new, empty buffer.
1949func Test_BufLeave_Wipe()
1950 %bwipe!
1951 let content = ['start of test file Xxx',
1952 \ 'this is a test',
1953 \ 'end of test file Xxx']
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01001954 call writefile(content, 'Xxx1', 'D')
1955 call writefile(content, 'Xxx2', 'D')
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +02001956
1957 au BufLeave Xxx2 bwipe
1958 edit Xxx1
1959 split Xxx2
1960 " delete buffer Xxx2, we should be back to Xxx1
1961 bwipe
1962 call assert_equal('Xxx1', bufname('%'))
1963 call assert_equal(1, winnr('$'))
1964
1965 " Create an alternate buffer
1966 %write! test.out
1967 call assert_equal('test.out', bufname('#'))
1968 " delete alternate buffer
1969 bwipe test.out
1970 call assert_equal('Xxx1', bufname('%'))
1971 call assert_equal('', bufname('#'))
1972
1973 au BufLeave Xxx1 bwipe
1974 " delete current buffer, get an empty one
1975 bwipe!
1976 call assert_equal(1, line('$'))
1977 call assert_equal('', bufname('%'))
Bram Moolenaarb2c87502017-10-14 21:15:58 +02001978 let g:bufinfo = getbufinfo()
1979 call assert_equal(1, len(g:bufinfo))
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +02001980
Bram Moolenaar53f0c962017-10-22 14:23:59 +02001981 call delete('test.out')
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +02001982 %bwipe
1983 au! BufLeave
Bram Moolenaarb2c87502017-10-14 21:15:58 +02001984
1985 " check that bufinfo doesn't contain a pointer to freed memory
1986 call test_garbagecollect_now()
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +02001987endfunc
Bram Moolenaar87ffb5c2017-10-19 12:37:42 +02001988
1989func Test_QuitPre()
1990 edit Xfoo
1991 let winid = win_getid(winnr())
1992 split Xbar
1993 au! QuitPre * let g:afile = expand('<afile>')
1994 " Close the other window, <afile> should be correct.
1995 exe win_id2win(winid) . 'q'
1996 call assert_equal('Xfoo', g:afile)
LemonBoy66e13ae2022-04-21 22:52:11 +01001997
Bram Moolenaar87ffb5c2017-10-19 12:37:42 +02001998 unlet g:afile
1999 bwipe Xfoo
2000 bwipe Xbar
2001endfunc
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02002002
Girish Palya92f68e22025-04-21 11:12:41 +02002003func Test_Cmdline_Trigger()
2004 autocmd CmdlineLeavePre : let g:log = "CmdlineLeavePre"
Girish Palya46755e62025-04-27 19:28:06 +02002005 autocmd CmdlineLeavePre : let g:log2 = "CmdlineLeave"
Girish Palya92f68e22025-04-21 11:12:41 +02002006 new
2007 let g:log = ''
Girish Palya46755e62025-04-27 19:28:06 +02002008 let g:log2 = ''
Girish Palya92f68e22025-04-21 11:12:41 +02002009 nnoremap <F1> <Cmd>echo "hello"<CR>
2010 call feedkeys("\<F1>", 'x')
2011 call assert_equal('', g:log)
Girish Palya46755e62025-04-27 19:28:06 +02002012 call assert_equal('', g:log2)
Girish Palya92f68e22025-04-21 11:12:41 +02002013 nunmap <F1>
Girish Palya46755e62025-04-27 19:28:06 +02002014
Girish Palya92f68e22025-04-21 11:12:41 +02002015 let g:log = ''
Girish Palya46755e62025-04-27 19:28:06 +02002016 let g:log2 = ''
Girish Palya92f68e22025-04-21 11:12:41 +02002017 nnoremap <F1> :echo "hello"<CR>
2018 call feedkeys("\<F1>", 'x')
2019 call assert_equal('CmdlineLeavePre', g:log)
Girish Palya46755e62025-04-27 19:28:06 +02002020 call assert_equal('CmdlineLeave', g:log2)
Girish Palya92f68e22025-04-21 11:12:41 +02002021 nunmap <F1>
Girish Palya46755e62025-04-27 19:28:06 +02002022
Girish Palya92f68e22025-04-21 11:12:41 +02002023 let g:log = ''
Girish Palya46755e62025-04-27 19:28:06 +02002024 let g:log2 = ''
2025 call feedkeys(":\<bs>", "tx")
2026 call assert_equal('CmdlineLeavePre', g:log)
2027 call assert_equal('CmdlineLeave', g:log2)
2028
2029 let g:log = ''
2030 let g:log2 = ''
Girish Palya92f68e22025-04-21 11:12:41 +02002031 split
2032 call assert_equal('', g:log)
2033 call feedkeys(":echo hello", "tx")
2034 call assert_equal('CmdlineLeavePre', g:log)
Girish Palya46755e62025-04-27 19:28:06 +02002035 call assert_equal('CmdlineLeave', g:log2)
2036
Girish Palya92f68e22025-04-21 11:12:41 +02002037 let g:log = ''
Girish Palya46755e62025-04-27 19:28:06 +02002038 let g:log2 = ''
Girish Palya92f68e22025-04-21 11:12:41 +02002039 close
2040 call assert_equal('', g:log)
2041 call feedkeys(":echo hello", "tx")
2042 call assert_equal('CmdlineLeavePre', g:log)
Girish Palya46755e62025-04-27 19:28:06 +02002043 call assert_equal('CmdlineLeave', g:log2)
2044
Girish Palya92f68e22025-04-21 11:12:41 +02002045 let g:log = ''
Girish Palya46755e62025-04-27 19:28:06 +02002046 let g:log2 = ''
Girish Palya92f68e22025-04-21 11:12:41 +02002047 tabnew
2048 call assert_equal('', g:log)
2049 call feedkeys(":echo hello", "tx")
2050 call assert_equal('CmdlineLeavePre', g:log)
Girish Palya46755e62025-04-27 19:28:06 +02002051 call assert_equal('CmdlineLeave', g:log2)
2052
Girish Palya92f68e22025-04-21 11:12:41 +02002053 let g:log = ''
Girish Palya46755e62025-04-27 19:28:06 +02002054 let g:log2 = ''
Girish Palya92f68e22025-04-21 11:12:41 +02002055 split
2056 call assert_equal('', g:log)
2057 call feedkeys(":echo hello", "tx")
2058 call assert_equal('CmdlineLeavePre', g:log)
Girish Palya46755e62025-04-27 19:28:06 +02002059 call assert_equal('CmdlineLeave', g:log2)
2060
Girish Palya92f68e22025-04-21 11:12:41 +02002061 let g:log = ''
Girish Palya46755e62025-04-27 19:28:06 +02002062 let g:log2 = ''
Girish Palya92f68e22025-04-21 11:12:41 +02002063 tabclose
2064 call assert_equal('', g:log)
2065 call feedkeys(":echo hello", "tx")
2066 call assert_equal('CmdlineLeavePre', g:log)
Girish Palya46755e62025-04-27 19:28:06 +02002067 call assert_equal('CmdlineLeave', g:log2)
2068
Girish Palya5c3d1e32025-04-22 19:52:16 +02002069 let g:count = 0
2070 autocmd CmdlineLeavePre * let g:count += 1
2071 call feedkeys(":let c = input('? ')\<cr>B\<cr>", "tx")
2072 call assert_equal(2, g:count)
2073 unlet! g:count
2074 unlet! g:log
Girish Palya46755e62025-04-27 19:28:06 +02002075 unlet! g:log2
Girish Palya92f68e22025-04-21 11:12:41 +02002076 bw!
2077endfunc
2078
Girish Palya46755e62025-04-27 19:28:06 +02002079" Ensure :cabbr does not cause a spurious CmdlineLeavePre.
2080func Test_CmdlineLeavePre_cabbr()
Girish Palya6220bba2025-04-28 18:00:40 +02002081 " For unknown reason this fails intermittently on MS-Windows
2082 CheckNotMSWindows
Girish Palya46755e62025-04-27 19:28:06 +02002083 CheckFeature terminal
2084 let buf = term_start([GetVimProg(), '--clean', '-c', 'set noswapfile'], {'term_rows': 3})
2085 call assert_equal('running', term_getstatus(buf))
2086 call term_sendkeys(buf, ":let g:a=0\<cr>")
2087 call term_wait(buf, 50)
2088 call term_sendkeys(buf, ":cabbr v v\<cr>")
2089 call term_wait(buf, 50)
2090 call term_sendkeys(buf, ":command! -nargs=* Foo echo\<cr>")
2091 call term_wait(buf, 50)
2092 call term_sendkeys(buf, ":au! CmdlineLeavePre * :let g:a+=1\<cr>")
2093 call term_wait(buf, 50)
2094 call term_sendkeys(buf, ":Foo v\<cr>")
2095 call term_wait(buf, 50)
2096 call term_sendkeys(buf, ":echo g:a\<cr>")
2097 call term_wait(buf, 50)
2098 call WaitForAssert({-> assert_match('^2.*$', term_getline(buf, 3))})
2099 bwipe!
2100endfunc
2101
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02002102func Test_Cmdline()
Bram Moolenaar153b7042018-01-31 15:48:32 +01002103 au! CmdlineChanged : let g:text = getcmdline()
2104 let g:text = 0
2105 call feedkeys(":echom 'hello'\<CR>", 'xt')
2106 call assert_equal("echom 'hello'", g:text)
2107 au! CmdlineChanged
2108
2109 au! CmdlineChanged : let g:entered = expand('<afile>')
2110 let g:entered = 0
2111 call feedkeys(":echom 'hello'\<CR>", 'xt')
2112 call assert_equal(':', g:entered)
2113 au! CmdlineChanged
2114
zeertzjq412e0e42023-02-11 10:34:07 +00002115 autocmd CmdlineChanged : let g:log += [getcmdline()]
2116
Bram Moolenaarbb393d82022-12-09 12:21:50 +00002117 let g:log = []
2118 cnoremap <F1> <Cmd>call setcmdline('ls')<CR>
Bram Moolenaarbb393d82022-12-09 12:21:50 +00002119 call feedkeys(":\<F1>", 'xt')
2120 call assert_equal(['ls'], g:log)
Bram Moolenaarbb393d82022-12-09 12:21:50 +00002121 cunmap <F1>
2122
zeertzjqaf9e28a2023-02-06 20:58:09 +00002123 let g:log = []
zeertzjqaf9e28a2023-02-06 20:58:09 +00002124 call feedkeys(":sign \<Tab>\<Tab>\<C-N>\<C-P>\<S-Tab>\<S-Tab>\<Esc>", 'xt')
2125 call assert_equal([
2126 \ 's',
2127 \ 'si',
2128 \ 'sig',
2129 \ 'sign',
2130 \ 'sign ',
2131 \ 'sign define',
2132 \ 'sign jump',
2133 \ 'sign list',
2134 \ 'sign jump',
2135 \ 'sign define',
2136 \ 'sign ',
2137 \ ], g:log)
2138 let g:log = []
2139 set wildmenu wildoptions+=pum
2140 call feedkeys(":sign \<S-Tab>\<PageUp>\<kPageUp>\<kPageDown>\<PageDown>\<Esc>", 'xt')
2141 call assert_equal([
2142 \ 's',
2143 \ 'si',
2144 \ 'sig',
2145 \ 'sign',
2146 \ 'sign ',
2147 \ 'sign unplace',
2148 \ 'sign jump',
2149 \ 'sign define',
2150 \ 'sign undefine',
2151 \ 'sign unplace',
2152 \ ], g:log)
2153 set wildmenu& wildoptions&
zeertzjq412e0e42023-02-11 10:34:07 +00002154
2155 let g:log = []
2156 let @r = 'abc'
2157 call feedkeys(":0\<C-R>r1\<C-R>\<C-O>r2\<C-R>\<C-R>r3\<Esc>", 'xt')
2158 call assert_equal([
2159 \ '0',
2160 \ '0a',
2161 \ '0ab',
2162 \ '0abc',
2163 \ '0abc1',
2164 \ '0abc1abc',
2165 \ '0abc1abc2',
2166 \ '0abc1abc2abc',
2167 \ '0abc1abc2abc3',
2168 \ ], g:log)
2169
zeertzjqaf9e28a2023-02-06 20:58:09 +00002170 unlet g:log
2171 au! CmdlineChanged
2172
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02002173 au! CmdlineEnter : let g:entered = expand('<afile>')
2174 au! CmdlineLeave : let g:left = expand('<afile>')
Girish Palya92f68e22025-04-21 11:12:41 +02002175 au! CmdlineLeavePre : let g:leftpre = expand('<afile>')
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02002176 let g:entered = 0
2177 let g:left = 0
Girish Palya92f68e22025-04-21 11:12:41 +02002178 let g:leftpre = 0
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02002179 call feedkeys(":echo 'hello'\<CR>", 'xt')
2180 call assert_equal(':', g:entered)
2181 call assert_equal(':', g:left)
Girish Palya92f68e22025-04-21 11:12:41 +02002182 call assert_equal(':', g:leftpre)
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02002183 au! CmdlineEnter
2184 au! CmdlineLeave
Girish Palya92f68e22025-04-21 11:12:41 +02002185 au! CmdlineLeavePre
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02002186
Bram Moolenaara4baf5b2018-04-22 13:27:44 +02002187 let save_shellslash = &shellslash
2188 set noshellslash
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02002189 au! CmdlineEnter / let g:entered = expand('<afile>')
2190 au! CmdlineLeave / let g:left = expand('<afile>')
Girish Palya92f68e22025-04-21 11:12:41 +02002191 au! CmdlineLeavePre / let g:leftpre = expand('<afile>')
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02002192 let g:entered = 0
2193 let g:left = 0
Girish Palya92f68e22025-04-21 11:12:41 +02002194 let g:leftpre = 0
Bram Moolenaar53f0c962017-10-22 14:23:59 +02002195 new
2196 call setline(1, 'hello')
2197 call feedkeys("/hello\<CR>", 'xt')
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02002198 call assert_equal('/', g:entered)
2199 call assert_equal('/', g:left)
Girish Palya92f68e22025-04-21 11:12:41 +02002200 call assert_equal('/', g:leftpre)
Bram Moolenaar53f0c962017-10-22 14:23:59 +02002201 bwipe!
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02002202 au! CmdlineEnter
2203 au! CmdlineLeave
Girish Palya92f68e22025-04-21 11:12:41 +02002204 au! CmdlineLeavePre
Bram Moolenaara4baf5b2018-04-22 13:27:44 +02002205 let &shellslash = save_shellslash
Shougo Matsushitad0952142024-06-20 22:05:16 +02002206
Girish Palya92f68e22025-04-21 11:12:41 +02002207 let g:left = "cancelled"
2208 let g:leftpre = "cancelled"
2209 au! CmdlineLeave : let g:left = "triggered"
2210 au! CmdlineLeavePre : let g:leftpre = "triggered"
2211 call feedkeys(":echo 'hello'\<esc>", 'xt')
2212 call assert_equal('triggered', g:left)
2213 call assert_equal('triggered', g:leftpre)
2214 let g:left = "cancelled"
2215 let g:leftpre = "cancelled"
2216 au! CmdlineLeave : let g:left = "triggered"
2217 call feedkeys(":echo 'hello'\<c-c>", 'xt')
2218 call assert_equal('triggered', g:left)
2219 call assert_equal('triggered', g:leftpre)
2220 au! CmdlineLeave
2221 au! CmdlineLeavePre
2222
zeertzjqbc6f9672024-06-21 07:51:40 +02002223 au! CursorMovedC : let g:pos += [getcmdpos()]
2224 let g:pos = []
zeertzjq81456202024-07-07 20:48:25 +02002225 call feedkeys(":foo bar baz\<C-W>\<C-W>\<C-W>\<Esc>", 'xt')
2226 call assert_equal([2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 9, 5, 1], g:pos)
2227 let g:pos = []
2228 call feedkeys(":hello\<C-B>\<Esc>", 'xt')
2229 call assert_equal([2, 3, 4, 5, 6, 1], g:pos)
2230 let g:pos = []
2231 call feedkeys(":hello\<C-U>\<Esc>", 'xt')
2232 call assert_equal([2, 3, 4, 5, 6, 1], g:pos)
2233 let g:pos = []
zeertzjqbc6f9672024-06-21 07:51:40 +02002234 call feedkeys(":hello\<Left>\<C-R>=''\<CR>\<Left>\<Right>\<Esc>", 'xt')
zeertzjq81456202024-07-07 20:48:25 +02002235 call assert_equal([2, 3, 4, 5, 6, 5, 4, 5], g:pos)
zeertzjqbc6f9672024-06-21 07:51:40 +02002236 let g:pos = []
2237 call feedkeys(":12345678\<C-R>=setcmdpos(3)??''\<CR>\<Esc>", 'xt')
zeertzjq81456202024-07-07 20:48:25 +02002238 call assert_equal([2, 3, 4, 5, 6, 7, 8, 9, 3], g:pos)
zeertzjqbc6f9672024-06-21 07:51:40 +02002239 let g:pos = []
2240 call feedkeys(":12345678\<C-R>=setcmdpos(3)??''\<CR>\<Left>\<Esc>", 'xt')
zeertzjq81456202024-07-07 20:48:25 +02002241 call assert_equal([2, 3, 4, 5, 6, 7, 8, 9, 3, 2], g:pos)
Shougo Matsushitad0952142024-06-20 22:05:16 +02002242 au! CursorMovedC
2243
zeertzjqbc6f9672024-06-21 07:51:40 +02002244 " setcmdpos() is no-op inside an autocommand
2245 au! CursorMovedC : let g:pos += [getcmdpos()] | call setcmdpos(1)
2246 let g:pos = []
2247 call feedkeys(":hello\<Left>\<Left>\<Esc>", 'xt')
zeertzjq81456202024-07-07 20:48:25 +02002248 call assert_equal([2, 3, 4, 5, 6, 5, 4], g:pos)
Shougo Matsushitad0952142024-06-20 22:05:16 +02002249 au! CursorMovedC
zeertzjqbc6f9672024-06-21 07:51:40 +02002250
2251 unlet g:entered
2252 unlet g:left
2253 unlet g:pos
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02002254endfunc
Bram Moolenaar53f0c962017-10-22 14:23:59 +02002255
2256" Test for BufWritePre autocommand that deletes or unloads the buffer.
2257func Test_BufWritePre()
2258 %bwipe
2259 au BufWritePre Xxx1 bunload
2260 au BufWritePre Xxx2 bwipe
2261
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01002262 call writefile(['start of Xxx1', 'test', 'end of Xxx1'], 'Xxx1', 'D')
2263 call writefile(['start of Xxx2', 'test', 'end of Xxx2'], 'Xxx2', 'D')
Bram Moolenaar53f0c962017-10-22 14:23:59 +02002264
2265 edit Xtest
2266 e! Xxx2
2267 bdel Xtest
2268 e Xxx1
2269 " write it, will unload it and give an error msg
Bram Moolenaare2e40752020-09-04 21:18:46 +02002270 call assert_fails('w', 'E203:')
Bram Moolenaar53f0c962017-10-22 14:23:59 +02002271 call assert_equal('Xxx2', bufname('%'))
2272 edit Xtest
2273 e! Xxx2
2274 bwipe Xtest
2275 " write it, will delete the buffer and give an error msg
Bram Moolenaare2e40752020-09-04 21:18:46 +02002276 call assert_fails('w', 'E203:')
Bram Moolenaar53f0c962017-10-22 14:23:59 +02002277 call assert_equal('Xxx1', bufname('%'))
2278 au! BufWritePre
Bram Moolenaar53f0c962017-10-22 14:23:59 +02002279endfunc
2280
2281" Test for BufUnload autocommand that unloads all the other buffers
2282func Test_bufunload_all()
Bram Moolenaarf08b0eb2021-10-16 13:00:14 +01002283 let g:test_is_flaky = 1
Christian Brabandtee17b6f2023-09-09 11:23:50 +02002284 call writefile(['Test file Xxx1'], 'Xxx1', 'D')
2285 call writefile(['Test file Xxx2'], 'Xxx2', 'D')
Bram Moolenaar53f0c962017-10-22 14:23:59 +02002286
Bram Moolenaarc79745a2019-05-20 22:12:34 +02002287 let content =<< trim [CODE]
2288 func UnloadAllBufs()
2289 let i = 1
2290 while i <= bufnr('$')
2291 if i != bufnr('%') && bufloaded(i)
2292 exe i . 'bunload'
2293 endif
2294 let i += 1
2295 endwhile
2296 endfunc
2297 au BufUnload * call UnloadAllBufs()
2298 au VimLeave * call writefile(['Test Finished'], 'Xout')
2299 edit Xxx1
2300 split Xxx2
2301 q
2302 [CODE]
2303
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01002304 call writefile(content, 'Xbunloadtest', 'D')
Bram Moolenaar53f0c962017-10-22 14:23:59 +02002305
2306 call delete('Xout')
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01002307 call system(GetVimCommandClean() .. ' -N --not-a-term -S Xbunloadtest')
Bram Moolenaar53f0c962017-10-22 14:23:59 +02002308 call assert_true(filereadable('Xout'))
2309
Bram Moolenaar53f0c962017-10-22 14:23:59 +02002310 call delete('Xout')
2311endfunc
2312
2313" Some tests for buffer-local autocommands
2314func Test_buflocal_autocmd()
2315 let g:bname = ''
2316 edit xx
2317 au BufLeave <buffer> let g:bname = expand("%")
2318 " here, autocommand for xx should trigger.
2319 " but autocommand shall not apply to buffer named <buffer>.
2320 edit somefile
2321 call assert_equal('xx', g:bname)
2322 let g:bname = ''
2323 " here, autocommand shall be auto-deleted
2324 bwipe xx
2325 " autocmd should not trigger
2326 edit xx
2327 call assert_equal('', g:bname)
2328 " autocmd should not trigger
2329 edit somefile
2330 call assert_equal('', g:bname)
2331 enew
2332 unlet g:bname
2333endfunc
Bram Moolenaar430dc5d2017-11-02 21:04:47 +01002334
2335" Test for "*Cmd" autocommands
2336func Test_Cmd_Autocmds()
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01002337 call writefile(['start of Xxx', "\tabc2", 'end of Xxx'], 'Xxx', 'D')
Bram Moolenaar430dc5d2017-11-02 21:04:47 +01002338
2339 enew!
2340 au BufReadCmd XtestA 0r Xxx|$del
2341 edit XtestA " will read text of Xxd instead
2342 call assert_equal('start of Xxx', getline(1))
2343
2344 au BufWriteCmd XtestA call append(line("$"), "write")
2345 write " will append a line to the file
2346 call assert_equal('write', getline('$'))
Bram Moolenaare2e40752020-09-04 21:18:46 +02002347 call assert_fails('read XtestA', 'E484:') " should not read anything
Bram Moolenaar430dc5d2017-11-02 21:04:47 +01002348 call assert_equal('write', getline(4))
2349
2350 " now we have:
2351 " 1 start of Xxx
2352 " 2 abc2
2353 " 3 end of Xxx
2354 " 4 write
2355
2356 au FileReadCmd XtestB '[r Xxx
2357 2r XtestB " will read Xxx below line 2 instead
2358 call assert_equal('start of Xxx', getline(3))
2359
2360 " now we have:
2361 " 1 start of Xxx
2362 " 2 abc2
2363 " 3 start of Xxx
2364 " 4 abc2
2365 " 5 end of Xxx
2366 " 6 end of Xxx
2367 " 7 write
2368
2369 au FileWriteCmd XtestC '[,']copy $
2370 normal 4GA1
2371 4,5w XtestC " will copy lines 4 and 5 to the end
2372 call assert_equal("\tabc21", getline(8))
Bram Moolenaare2e40752020-09-04 21:18:46 +02002373 call assert_fails('r XtestC', 'E484:') " should not read anything
Bram Moolenaar430dc5d2017-11-02 21:04:47 +01002374 call assert_equal("end of Xxx", getline(9))
2375
2376 " now we have:
2377 " 1 start of Xxx
2378 " 2 abc2
2379 " 3 start of Xxx
2380 " 4 abc21
2381 " 5 end of Xxx
2382 " 6 end of Xxx
2383 " 7 write
2384 " 8 abc21
2385 " 9 end of Xxx
2386
2387 let g:lines = []
2388 au FileAppendCmd XtestD call extend(g:lines, getline(line("'["), line("']")))
2389 w >>XtestD " will add lines to 'lines'
2390 call assert_equal(9, len(g:lines))
Bram Moolenaare2e40752020-09-04 21:18:46 +02002391 call assert_fails('$r XtestD', 'E484:') " should not read anything
Bram Moolenaar430dc5d2017-11-02 21:04:47 +01002392 call assert_equal(9, line('$'))
2393 call assert_equal('end of Xxx', getline('$'))
2394
2395 au BufReadCmd XtestE 0r Xxx|$del
2396 sp XtestE " split window with test.out
2397 call assert_equal('end of Xxx', getline(3))
2398
2399 let g:lines = []
2400 exe "normal 2Goasdf\<Esc>\<C-W>\<C-W>"
2401 au BufWriteCmd XtestE call extend(g:lines, getline(0, '$'))
2402 wall " will write other window to 'lines'
2403 call assert_equal(4, len(g:lines), g:lines)
2404 call assert_equal('asdf', g:lines[2])
2405
2406 au! BufReadCmd
2407 au! BufWriteCmd
2408 au! FileReadCmd
2409 au! FileWriteCmd
2410 au! FileAppendCmd
2411 %bwipe!
Bram Moolenaar430dc5d2017-11-02 21:04:47 +01002412 enew!
2413endfunc
Bram Moolenaaraace2152017-11-05 16:23:10 +01002414
Bram Moolenaar0fff4412020-03-29 16:06:29 +02002415func s:ReadFile()
2416 setl noswapfile nomodified
2417 let filename = resolve(expand("<afile>:p"))
2418 execute 'read' fnameescape(filename)
2419 1d_
2420 exe 'file' fnameescape(filename)
2421 setl buftype=acwrite
2422endfunc
2423
2424func s:WriteFile()
2425 let filename = resolve(expand("<afile>:p"))
2426 setl buftype=
2427 noautocmd execute 'write' fnameescape(filename)
2428 setl buftype=acwrite
2429 setl nomodified
2430endfunc
2431
2432func Test_BufReadCmd()
2433 autocmd BufReadCmd *.test call s:ReadFile()
2434 autocmd BufWriteCmd *.test call s:WriteFile()
2435
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01002436 call writefile(['one', 'two', 'three'], 'Xcmd.test', 'D')
Bram Moolenaar0fff4412020-03-29 16:06:29 +02002437 edit Xcmd.test
2438 call assert_match('Xcmd.test" line 1 of 3', execute('file'))
2439 normal! Gofour
2440 write
2441 call assert_equal(['one', 'two', 'three', 'four'], readfile('Xcmd.test'))
2442
2443 bwipe!
Bram Moolenaar0fff4412020-03-29 16:06:29 +02002444 au! BufReadCmd
2445 au! BufWriteCmd
2446endfunc
2447
zeertzjq9c8f9462022-08-30 18:17:15 +01002448func Test_BufWriteCmd()
2449 autocmd BufWriteCmd Xbufwritecmd let g:written = 1
2450 new
2451 file Xbufwritecmd
2452 set buftype=acwrite
Bram Moolenaar6f14da12022-09-07 21:30:44 +01002453 call mkdir('Xbufwritecmd', 'D')
zeertzjq9c8f9462022-08-30 18:17:15 +01002454 write
2455 " BufWriteCmd should be triggered even if a directory has the same name
2456 call assert_equal(1, g:written)
zeertzjq9c8f9462022-08-30 18:17:15 +01002457 unlet g:written
2458 au! BufWriteCmd
2459 bwipe!
2460endfunc
2461
Bram Moolenaaraace2152017-11-05 16:23:10 +01002462func SetChangeMarks(start, end)
Bram Moolenaar97c69432021-01-15 16:45:21 +01002463 exe a:start .. 'mark ['
2464 exe a:end .. 'mark ]'
Bram Moolenaaraace2152017-11-05 16:23:10 +01002465endfunc
2466
2467" Verify the effects of autocmds on '[ and ']
2468func Test_change_mark_in_autocmds()
2469 edit! Xtest
Bram Moolenaar97c69432021-01-15 16:45:21 +01002470 call feedkeys("ia\<CR>b\<CR>c\<CR>d\<C-g>u\<Esc>", 'xtn')
Bram Moolenaaraace2152017-11-05 16:23:10 +01002471
2472 call SetChangeMarks(2, 3)
2473 write
2474 call assert_equal([1, 4], [line("'["), line("']")])
2475
2476 call SetChangeMarks(2, 3)
2477 au BufWritePre * call assert_equal([1, 4], [line("'["), line("']")])
2478 write
2479 au! BufWritePre
2480
Bram Moolenaar14ddd222020-08-05 12:02:40 +02002481 if has('unix')
Bram Moolenaaraace2152017-11-05 16:23:10 +01002482 write XtestFilter
2483 write >> XtestFilter
2484
2485 call SetChangeMarks(2, 3)
2486 " Marks are set to the entire range of the write
2487 au FilterWritePre * call assert_equal([1, 4], [line("'["), line("']")])
2488 " '[ is adjusted to just before the line that will receive the filtered
2489 " data
2490 au FilterReadPre * call assert_equal([4, 4], [line("'["), line("']")])
2491 " The filtered data is read into the buffer, and the source lines are
2492 " still present, so the range is after the source lines
2493 au FilterReadPost * call assert_equal([5, 12], [line("'["), line("']")])
2494 %!cat XtestFilter
2495 " After the filtered data is read, the original lines are deleted
2496 call assert_equal([1, 8], [line("'["), line("']")])
2497 au! FilterWritePre,FilterReadPre,FilterReadPost
2498 undo
2499
2500 call SetChangeMarks(1, 4)
2501 au FilterWritePre * call assert_equal([2, 3], [line("'["), line("']")])
2502 au FilterReadPre * call assert_equal([3, 3], [line("'["), line("']")])
2503 au FilterReadPost * call assert_equal([4, 11], [line("'["), line("']")])
2504 2,3!cat XtestFilter
2505 call assert_equal([2, 9], [line("'["), line("']")])
2506 au! FilterWritePre,FilterReadPre,FilterReadPost
2507 undo
2508
2509 call delete('XtestFilter')
2510 endif
2511
2512 call SetChangeMarks(1, 4)
2513 au FileWritePre * call assert_equal([2, 3], [line("'["), line("']")])
2514 2,3write Xtest2
2515 au! FileWritePre
2516
2517 call SetChangeMarks(2, 3)
2518 au FileAppendPre * call assert_equal([1, 4], [line("'["), line("']")])
2519 write >> Xtest2
2520 au! FileAppendPre
2521
2522 call SetChangeMarks(1, 4)
2523 au FileAppendPre * call assert_equal([2, 3], [line("'["), line("']")])
2524 2,3write >> Xtest2
2525 au! FileAppendPre
2526
2527 call SetChangeMarks(1, 1)
2528 au FileReadPre * call assert_equal([3, 1], [line("'["), line("']")])
2529 au FileReadPost * call assert_equal([4, 11], [line("'["), line("']")])
2530 3read Xtest2
2531 au! FileReadPre,FileReadPost
2532 undo
2533
2534 call SetChangeMarks(4, 4)
2535 " When the line is 0, it's adjusted to 1
2536 au FileReadPre * call assert_equal([1, 4], [line("'["), line("']")])
2537 au FileReadPost * call assert_equal([1, 8], [line("'["), line("']")])
2538 0read Xtest2
2539 au! FileReadPre,FileReadPost
2540 undo
2541
2542 call SetChangeMarks(4, 4)
2543 " When the line is 0, it's adjusted to 1
2544 au FileReadPre * call assert_equal([1, 4], [line("'["), line("']")])
2545 au FileReadPost * call assert_equal([2, 9], [line("'["), line("']")])
2546 1read Xtest2
2547 au! FileReadPre,FileReadPost
2548 undo
2549
2550 bwipe!
2551 call delete('Xtest')
2552 call delete('Xtest2')
2553endfunc
2554
2555func Test_Filter_noshelltemp()
Bram Moolenaaraeb313f2020-11-27 19:13:28 +01002556 CheckExecutable cat
Bram Moolenaaraace2152017-11-05 16:23:10 +01002557
2558 enew!
2559 call setline(1, ['a', 'b', 'c', 'd'])
2560
2561 let shelltemp = &shelltemp
2562 set shelltemp
2563
2564 let g:filter_au = 0
2565 au FilterWritePre * let g:filter_au += 1
2566 au FilterReadPre * let g:filter_au += 1
2567 au FilterReadPost * let g:filter_au += 1
2568 %!cat
2569 call assert_equal(3, g:filter_au)
2570
2571 if has('filterpipe')
2572 set noshelltemp
2573
2574 let g:filter_au = 0
2575 au FilterWritePre * let g:filter_au += 1
2576 au FilterReadPre * let g:filter_au += 1
2577 au FilterReadPost * let g:filter_au += 1
2578 %!cat
2579 call assert_equal(0, g:filter_au)
2580 endif
2581
2582 au! FilterWritePre,FilterReadPre,FilterReadPost
2583 let &shelltemp = shelltemp
2584 bwipe!
2585endfunc
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01002586
2587func Test_TextYankPost()
2588 enew!
2589 call setline(1, ['foo'])
2590
2591 let g:event = []
2592 au TextYankPost * let g:event = copy(v:event)
2593
2594 call assert_equal({}, v:event)
2595 call assert_fails('let v:event = {}', 'E46:')
2596 call assert_fails('let v:event.mykey = 0', 'E742:')
2597
2598 norm "ayiw
2599 call assert_equal(
Bram Moolenaara016eeb2022-04-09 11:37:38 +01002600 \ #{regcontents: ['foo'], regname: 'a', operator: 'y',
2601 \ regtype: 'v', visual: v:false, inclusive: v:true},
2602 \ g:event)
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01002603 norm y_
2604 call assert_equal(
Bram Moolenaara016eeb2022-04-09 11:37:38 +01002605 \ #{regcontents: ['foo'], regname: '', operator: 'y', regtype: 'V',
2606 \ visual: v:false, inclusive: v:false},
2607 \ g:event)
Bram Moolenaar37d16732020-06-12 22:09:01 +02002608 norm Vy
2609 call assert_equal(
Bram Moolenaara016eeb2022-04-09 11:37:38 +01002610 \ #{regcontents: ['foo'], regname: '', operator: 'y', regtype: 'V',
2611 \ visual: v:true, inclusive: v:true},
2612 \ g:event)
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01002613 call feedkeys("\<C-V>y", 'x')
2614 call assert_equal(
Bram Moolenaara016eeb2022-04-09 11:37:38 +01002615 \ #{regcontents: ['f'], regname: '', operator: 'y', regtype: "\x161",
2616 \ visual: v:true, inclusive: v:true},
2617 \ g:event)
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01002618 norm "xciwbar
2619 call assert_equal(
Bram Moolenaara016eeb2022-04-09 11:37:38 +01002620 \ #{regcontents: ['foo'], regname: 'x', operator: 'c', regtype: 'v',
2621 \ visual: v:false, inclusive: v:true},
2622 \ g:event)
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01002623 norm "bdiw
2624 call assert_equal(
Bram Moolenaara016eeb2022-04-09 11:37:38 +01002625 \ #{regcontents: ['bar'], regname: 'b', operator: 'd', regtype: 'v',
2626 \ visual: v:false, inclusive: v:true},
2627 \ g:event)
2628
2629 call setline(1, 'foobar')
2630 " exclusive motion
2631 norm $"ay0
2632 call assert_equal(
2633 \ #{regcontents: ['fooba'], regname: 'a', operator: 'y', regtype: 'v',
2634 \ visual: v:false, inclusive: v:false},
2635 \ g:event)
2636 " inclusive motion
2637 norm 0"ay$
2638 call assert_equal(
2639 \ #{regcontents: ['foobar'], regname: 'a', operator: 'y', regtype: 'v',
2640 \ visual: v:false, inclusive: v:true},
2641 \ g:event)
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01002642
2643 call assert_equal({}, v:event)
2644
Bram Moolenaarfccbf062020-11-26 20:34:00 +01002645 if has('clipboard_working') && !has('gui_running')
2646 " Test that when the visual selection is automatically copied to clipboard
2647 " register a TextYankPost is emitted
2648 call setline(1, ['foobar'])
2649
2650 let @* = ''
2651 set clipboard=autoselect
2652 exe "norm! ggviw\<Esc>"
2653 call assert_equal(
Bram Moolenaara016eeb2022-04-09 11:37:38 +01002654 \ #{regcontents: ['foobar'], regname: '*', operator: 'y',
2655 \ regtype: 'v', visual: v:true, inclusive: v:false},
2656 \ g:event)
Bram Moolenaarfccbf062020-11-26 20:34:00 +01002657
2658 let @+ = ''
2659 set clipboard=autoselectplus
2660 exe "norm! ggviw\<Esc>"
2661 call assert_equal(
Bram Moolenaara016eeb2022-04-09 11:37:38 +01002662 \ #{regcontents: ['foobar'], regname: '+', operator: 'y',
2663 \ regtype: 'v', visual: v:true, inclusive: v:false},
2664 \ g:event)
Bram Moolenaarfccbf062020-11-26 20:34:00 +01002665
2666 set clipboard&vim
2667 endif
2668
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01002669 au! TextYankPost
2670 unlet g:event
2671 bwipe!
2672endfunc
Bram Moolenaar9bca8052017-12-18 12:37:55 +01002673
Bram Moolenaar9a046fd2021-01-28 13:47:59 +01002674func Test_autocommand_all_events()
2675 call assert_fails('au * * bwipe', 'E1155:')
2676 call assert_fails('au * x bwipe', 'E1155:')
Bram Moolenaarb6db1462021-12-24 19:24:47 +00002677 call assert_fails('au! * x bwipe', 'E1155:')
Bram Moolenaar4fb921e2017-12-18 15:33:00 +01002678endfunc
Bram Moolenaarb7407d32018-02-03 17:36:27 +01002679
Bram Moolenaarf6246f52022-02-11 16:30:12 +00002680func Test_autocmd_user()
2681 au User MyEvent let s:res = [expand("<afile>"), expand("<amatch>")]
2682 doautocmd User MyEvent
2683 call assert_equal(['MyEvent', 'MyEvent'], s:res)
2684 au! User
2685 unlet s:res
2686endfunc
2687
Bram Moolenaar3b014be2022-11-13 17:53:46 +00002688func Test_autocmd_user_clear_group()
2689 CheckRunVimInTerminal
2690
2691 let lines =<< trim END
2692 autocmd! User
2693 for i in range(1, 999)
2694 exe 'autocmd User ' .. 'Foo' .. i .. ' bar'
2695 endfor
2696 au CmdlineLeave : call timer_start(0, {-> execute('autocmd! User')})
2697 END
2698 call writefile(lines, 'XautoUser', 'D')
2699 let buf = RunVimInTerminal('-S XautoUser', {'rows': 10})
2700
2701 " this was using freed memory
2702 call term_sendkeys(buf, ":autocmd User\<CR>")
2703 call TermWait(buf, 50)
2704 call term_sendkeys(buf, "G")
2705
2706 call StopVimInTerminal(buf)
2707endfunc
2708
Bram Moolenaaref2c3252022-11-25 16:31:51 +00002709func Test_autocmd_CmdlineLeave_unlet()
2710 CheckRunVimInTerminal
2711
2712 let lines =<< trim END
2713 for i in range(1, 999)
2714 exe 'let g:var' .. i '=' i
2715 endfor
2716 au CmdlineLeave : call timer_start(0, {-> execute('unlet g:var990')})
2717 END
2718 call writefile(lines, 'XleaveUnlet', 'D')
2719 let buf = RunVimInTerminal('-S XleaveUnlet', {'rows': 10})
2720
2721 " this was using freed memory
2722 call term_sendkeys(buf, ":let g:\<CR>")
2723 call TermWait(buf, 50)
2724 call term_sendkeys(buf, "G")
2725 call TermWait(buf, 50)
2726 call term_sendkeys(buf, "\<CR>") " for the hit-enter prompt
2727
2728 call StopVimInTerminal(buf)
2729endfunc
2730
Bram Moolenaarb7407d32018-02-03 17:36:27 +01002731function s:Before_test_dirchanged()
2732 augroup test_dirchanged
2733 autocmd!
2734 augroup END
2735 let s:li = []
2736 let s:dir_this = getcwd()
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02002737 let s:dir_foo = s:dir_this . '/Xfoo'
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01002738 call mkdir(s:dir_foo)
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02002739 let s:dir_bar = s:dir_this . '/Xbar'
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01002740 call mkdir(s:dir_bar)
Bram Moolenaarb7407d32018-02-03 17:36:27 +01002741endfunc
2742
2743function s:After_test_dirchanged()
Bram Moolenaar3503d7c2019-11-09 20:10:17 +01002744 call chdir(s:dir_this)
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01002745 call delete(s:dir_foo, 'd')
2746 call delete(s:dir_bar, 'd')
Bram Moolenaarb7407d32018-02-03 17:36:27 +01002747 augroup test_dirchanged
2748 autocmd!
2749 augroup END
2750endfunc
2751
2752function Test_dirchanged_global()
2753 call s:Before_test_dirchanged()
Bram Moolenaarf6246f52022-02-11 16:30:12 +00002754 autocmd test_dirchanged DirChangedPre global call add(s:li, expand("<amatch>") .. " pre cd " .. v:event.directory)
Bram Moolenaarb7407d32018-02-03 17:36:27 +01002755 autocmd test_dirchanged DirChanged global call add(s:li, "cd:")
2756 autocmd test_dirchanged DirChanged global call add(s:li, expand("<afile>"))
Bram Moolenaar3503d7c2019-11-09 20:10:17 +01002757 call chdir(s:dir_foo)
Bram Moolenaarf6246f52022-02-11 16:30:12 +00002758 let expected = ["global pre cd " .. s:dir_foo, "cd:", s:dir_foo]
Bram Moolenaar28e8f732022-02-09 12:58:20 +00002759 call assert_equal(expected, s:li)
Bram Moolenaar3503d7c2019-11-09 20:10:17 +01002760 call chdir(s:dir_foo)
Bram Moolenaar28e8f732022-02-09 12:58:20 +00002761 call assert_equal(expected, s:li)
Bram Moolenaar3503d7c2019-11-09 20:10:17 +01002762 exe 'lcd ' .. fnameescape(s:dir_bar)
Bram Moolenaar28e8f732022-02-09 12:58:20 +00002763 call assert_equal(expected, s:li)
Bram Moolenaard8c9d322022-06-12 11:49:16 +01002764
2765 exe 'cd ' .. s:dir_foo
2766 exe 'cd ' .. s:dir_bar
2767 autocmd! test_dirchanged DirChanged global let g:result = expand("<afile>")
2768 cd -
Bram Moolenaardb77c492022-06-12 23:26:50 +01002769 call assert_equal(s:dir_foo, substitute(g:result, '\\', '/', 'g'))
Bram Moolenaard8c9d322022-06-12 11:49:16 +01002770
Bram Moolenaarb7407d32018-02-03 17:36:27 +01002771 call s:After_test_dirchanged()
2772endfunc
2773
2774function Test_dirchanged_local()
2775 call s:Before_test_dirchanged()
2776 autocmd test_dirchanged DirChanged window call add(s:li, "lcd:")
2777 autocmd test_dirchanged DirChanged window call add(s:li, expand("<afile>"))
Bram Moolenaar3503d7c2019-11-09 20:10:17 +01002778 call chdir(s:dir_foo)
Bram Moolenaarb7407d32018-02-03 17:36:27 +01002779 call assert_equal([], s:li)
Bram Moolenaar3503d7c2019-11-09 20:10:17 +01002780 exe 'lcd ' .. fnameescape(s:dir_bar)
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01002781 call assert_equal(["lcd:", s:dir_bar], s:li)
Bram Moolenaar3503d7c2019-11-09 20:10:17 +01002782 exe 'lcd ' .. fnameescape(s:dir_bar)
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01002783 call assert_equal(["lcd:", s:dir_bar], s:li)
Bram Moolenaarb7407d32018-02-03 17:36:27 +01002784 call s:After_test_dirchanged()
2785endfunc
2786
2787function Test_dirchanged_auto()
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +02002788 CheckOption autochdir
Bram Moolenaarb7407d32018-02-03 17:36:27 +01002789 call s:Before_test_dirchanged()
2790 call test_autochdir()
Bram Moolenaar28e8f732022-02-09 12:58:20 +00002791 autocmd test_dirchanged DirChangedPre auto call add(s:li, "pre cd " .. v:event.directory)
Bram Moolenaarb7407d32018-02-03 17:36:27 +01002792 autocmd test_dirchanged DirChanged auto call add(s:li, "auto:")
2793 autocmd test_dirchanged DirChanged auto call add(s:li, expand("<afile>"))
2794 set acd
Bram Moolenaar3503d7c2019-11-09 20:10:17 +01002795 cd ..
Bram Moolenaarb7407d32018-02-03 17:36:27 +01002796 call assert_equal([], s:li)
Bram Moolenaar61abe7d2022-08-30 21:46:08 +01002797 exe 'edit ' . s:dir_foo . '/Xautofile'
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01002798 call assert_equal(s:dir_foo, getcwd())
Bram Moolenaar28e8f732022-02-09 12:58:20 +00002799 let expected = ["pre cd " .. s:dir_foo, "auto:", s:dir_foo]
2800 call assert_equal(expected, s:li)
Bram Moolenaarb7407d32018-02-03 17:36:27 +01002801 set noacd
2802 bwipe!
2803 call s:After_test_dirchanged()
2804endfunc
Bram Moolenaar5a093432018-02-10 18:15:19 +01002805
2806" Test TextChangedI and TextChangedP
2807func Test_ChangedP()
2808 new
2809 call setline(1, ['foo', 'bar', 'foobar'])
2810 call test_override("char_avail", 1)
2811 set complete=. completeopt=menuone
2812
2813 func! TextChangedAutocmd(char)
2814 let g:autocmd .= a:char
2815 endfunc
2816
Christian Brabandtdb3b4462021-10-16 11:58:55 +01002817 " TextChanged will not be triggered, only check that it isn't.
Bram Moolenaar5a093432018-02-10 18:15:19 +01002818 au! TextChanged <buffer> :call TextChangedAutocmd('N')
2819 au! TextChangedI <buffer> :call TextChangedAutocmd('I')
2820 au! TextChangedP <buffer> :call TextChangedAutocmd('P')
2821
2822 call cursor(3, 1)
2823 let g:autocmd = ''
2824 call feedkeys("o\<esc>", 'tnix')
Evgeni Chasnovskid7ae2632023-10-15 09:59:00 +02002825 call assert_equal('I', g:autocmd)
Bram Moolenaar5a093432018-02-10 18:15:19 +01002826
2827 let g:autocmd = ''
Christian Brabandt4bca4892023-10-27 19:26:49 +02002828 call feedkeys("Sf", 'tnix')
2829 call assert_equal('II', g:autocmd)
2830
2831 let g:autocmd = ''
Bram Moolenaar5a093432018-02-10 18:15:19 +01002832 call feedkeys("Sf\<C-N>", 'tnix')
Christian Brabandt4bca4892023-10-27 19:26:49 +02002833 call assert_equal('IIP', g:autocmd)
Bram Moolenaar5a093432018-02-10 18:15:19 +01002834
2835 let g:autocmd = ''
2836 call feedkeys("Sf\<C-N>\<C-N>", 'tnix')
Christian Brabandt4bca4892023-10-27 19:26:49 +02002837 call assert_equal('IIPP', g:autocmd)
Bram Moolenaar5a093432018-02-10 18:15:19 +01002838
2839 let g:autocmd = ''
2840 call feedkeys("Sf\<C-N>\<C-N>\<C-N>", 'tnix')
Christian Brabandt4bca4892023-10-27 19:26:49 +02002841 call assert_equal('IIPPP', g:autocmd)
Bram Moolenaar5a093432018-02-10 18:15:19 +01002842
2843 let g:autocmd = ''
2844 call feedkeys("Sf\<C-N>\<C-N>\<C-N>\<C-N>", 'tnix')
Christian Brabandt4bca4892023-10-27 19:26:49 +02002845 call assert_equal('IIPPPP', g:autocmd)
Bram Moolenaar5a093432018-02-10 18:15:19 +01002846
2847 call assert_equal(['foo', 'bar', 'foobar', 'foo'], getline(1, '$'))
2848 " TODO: how should it handle completeopt=noinsert,noselect?
2849
2850 " CleanUp
2851 call test_override("char_avail", 0)
2852 au! TextChanged
2853 au! TextChangedI
2854 au! TextChangedP
2855 delfu TextChangedAutocmd
2856 unlet! g:autocmd
2857 set complete&vim completeopt&vim
2858
2859 bw!
2860endfunc
Bram Moolenaar8c64a362018-03-23 22:39:31 +01002861
Bram Moolenaar91d2e782018-08-07 19:05:01 +02002862let g:setline_handled = v:false
Bram Moolenaar1e115362019-01-09 23:01:02 +01002863func SetLineOne()
Bram Moolenaar91d2e782018-08-07 19:05:01 +02002864 if !g:setline_handled
2865 call setline(1, "(x)")
2866 let g:setline_handled = v:true
2867 endif
2868endfunc
2869
2870func Test_TextChangedI_with_setline()
2871 new
2872 call test_override('char_avail', 1)
2873 autocmd TextChangedI <buffer> call SetLineOne()
2874 call feedkeys("i(\<CR>\<Esc>", 'tx')
2875 call assert_equal('(', getline(1))
2876 call assert_equal('x)', getline(2))
2877 undo
Bram Moolenaar91d2e782018-08-07 19:05:01 +02002878 call assert_equal('', getline(1))
Bram Moolenaar9fa95062018-08-08 22:08:32 +02002879 call assert_equal('', getline(2))
Bram Moolenaar91d2e782018-08-07 19:05:01 +02002880
Bram Moolenaarca34db32022-01-20 11:17:18 +00002881 call test_override('char_avail', 0)
Bram Moolenaar91d2e782018-08-07 19:05:01 +02002882 bwipe!
2883endfunc
2884
Christian Brabandtc9e79e52024-02-09 19:34:36 +01002885func Test_TextChanged_with_norm()
2886 " For unknown reason this fails on MS-Windows
2887 CheckNotMSWindows
2888 CheckFeature terminal
2889 let buf = term_start([GetVimProg(), '--clean', '-c', 'set noswapfile'], {'term_rows': 3})
2890 call assert_equal('running', term_getstatus(buf))
2891 call term_sendkeys(buf, ":let g:a=0\<cr>")
2892 call term_wait(buf, 50)
2893 call term_sendkeys(buf, ":au! TextChanged * :let g:a+=1\<cr>")
2894 call term_wait(buf, 50)
2895 call term_sendkeys(buf, ":norm! ia\<cr>")
2896 call term_wait(buf, 50)
2897 call term_sendkeys(buf, ":echo g:a\<cr>")
2898 call term_wait(buf, 50)
2899 call WaitForAssert({-> assert_match('^1.*$', term_getline(buf, 3))})
2900 bwipe!
2901endfunc
2902
Bram Moolenaar8c64a362018-03-23 22:39:31 +01002903func Test_Changed_FirstTime()
Bram Moolenaar8c5a2782019-08-07 23:07:07 +02002904 CheckFeature terminal
2905 CheckNotGui
Bram Moolenaar3cdcb092020-03-18 19:18:10 +01002906 " Starting a terminal to run Vim is always considered flaky.
Bram Moolenaar30d53e22020-03-18 21:10:44 +01002907 let g:test_is_flaky = 1
Bram Moolenaar8c5a2782019-08-07 23:07:07 +02002908
Bram Moolenaar8c64a362018-03-23 22:39:31 +01002909 " Prepare file for TextChanged event.
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01002910 call writefile([''], 'Xchanged.txt', 'D')
Bram Moolenaar8c64a362018-03-23 22:39:31 +01002911 let buf = term_start([GetVimProg(), '--clean', '-c', 'set noswapfile'], {'term_rows': 3})
2912 call assert_equal('running', term_getstatus(buf))
Bram Moolenaar1834d372018-03-29 17:40:46 +02002913 " Wait for the ruler (in the status line) to be shown.
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01002914 " In ConPTY, there is additional character which is drawn up to the width of
2915 " the screen.
2916 if has('conpty')
2917 call WaitForAssert({-> assert_match('\<All.*$', term_getline(buf, 3))})
2918 else
2919 call WaitForAssert({-> assert_match('\<All$', term_getline(buf, 3))})
2920 endif
Bram Moolenaar8c64a362018-03-23 22:39:31 +01002921 " It's only adding autocmd, so that no event occurs.
2922 call term_sendkeys(buf, ":au! TextChanged <buffer> call writefile(['No'], 'Xchanged.txt')\<cr>")
2923 call term_sendkeys(buf, "\<C-\\>\<C-N>:qa!\<cr>")
Bram Moolenaar50182fa2018-04-28 21:34:40 +02002924 call WaitForAssert({-> assert_equal('finished', term_getstatus(buf))})
Bram Moolenaar8c64a362018-03-23 22:39:31 +01002925 call assert_equal([''], readfile('Xchanged.txt'))
2926
2927 " clean up
Bram Moolenaar8c64a362018-03-23 22:39:31 +01002928 bwipe!
2929endfunc
Bram Moolenaar0566e892019-01-24 19:37:40 +01002930
Bram Moolenaareb93f3f2019-04-04 15:04:56 +02002931func Test_autocmd_nested()
2932 let g:did_nested = 0
Christian Brabandtfb3f9692024-08-11 20:09:17 +02002933 defer CleanUpTestAuGroup()
2934 augroup testing
Bram Moolenaareb93f3f2019-04-04 15:04:56 +02002935 au WinNew * edit somefile
2936 au BufNew * let g:did_nested = 1
2937 augroup END
2938 split
2939 call assert_equal(0, g:did_nested)
2940 close
2941 bwipe! somefile
2942
2943 " old nested argument still works
Christian Brabandtfb3f9692024-08-11 20:09:17 +02002944 augroup testing
Bram Moolenaareb93f3f2019-04-04 15:04:56 +02002945 au!
2946 au WinNew * nested edit somefile
2947 au BufNew * let g:did_nested = 1
2948 augroup END
2949 split
2950 call assert_equal(1, g:did_nested)
2951 close
2952 bwipe! somefile
2953
2954 " New ++nested argument works
2955 augroup Testing
2956 au!
2957 au WinNew * ++nested edit somefile
2958 au BufNew * let g:did_nested = 1
2959 augroup END
2960 split
2961 call assert_equal(1, g:did_nested)
2962 close
2963 bwipe! somefile
2964
Bram Moolenaarf0775142022-03-04 20:10:38 +00002965 " nested without ++ does not work in Vim9 script
2966 call assert_fails('vim9cmd au WinNew * nested echo fails', 'E1078:')
2967
Bram Moolenaareb93f3f2019-04-04 15:04:56 +02002968 augroup Testing
2969 au!
2970 augroup END
2971
2972 call assert_fails('au WinNew * ++nested ++nested echo bad', 'E983:')
2973 call assert_fails('au WinNew * nested nested echo bad', 'E983:')
2974endfunc
2975
Bram Moolenaar5fa9f232022-07-23 09:06:48 +01002976func Test_autocmd_nested_cursor_invalid()
2977 set laststatus=0
2978 copen
2979 cclose
2980 call setline(1, ['foo', 'bar', 'baz'])
2981 3
2982 augroup nested_inv
2983 autocmd User foo ++nested copen
2984 autocmd BufAdd * let &laststatus = 2 - &laststatus
2985 augroup END
2986 doautocmd User foo
2987
2988 augroup nested_inv
2989 au!
2990 augroup END
2991 set laststatus&
Bram Moolenaarb03950f2022-07-26 13:47:13 +01002992 cclose
Bram Moolenaar5fa9f232022-07-23 09:06:48 +01002993 bwipe!
2994endfunc
2995
Bram Moolenaar3d6ee8b2022-07-27 15:23:35 +01002996func Test_autocmd_nested_keeps_cursor_pos()
2997 enew
2998 call setline(1, 'foo')
2999 autocmd User foo ++nested normal! $a
3000 autocmd InsertLeave * :
3001 doautocmd User foo
3002 call assert_equal([0, 1, 3, 0], getpos('.'))
3003
3004 bwipe!
3005endfunc
3006
Bram Moolenaarb03950f2022-07-26 13:47:13 +01003007func Test_autocmd_nested_switch_window()
3008 " run this in a separate Vim so that SafeState works
3009 CheckRunVimInTerminal
3010
3011 let lines =<< trim END
3012 vim9script
3013 ['()']->writefile('Xautofile')
3014 autocmd VimEnter * ++nested edit Xautofile | split
3015 autocmd BufReadPost * autocmd SafeState * ++once foldclosed('.')
3016 autocmd WinEnter * matchadd('ErrorMsg', 'pat')
3017 END
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01003018 call writefile(lines, 'Xautoscript', 'D')
Bram Moolenaarb03950f2022-07-26 13:47:13 +01003019 let buf = RunVimInTerminal('-S Xautoscript', {'rows': 10})
3020 call VerifyScreenDump(buf, 'Test_autocmd_nested_switch', {})
3021
3022 call StopVimInTerminal(buf)
3023 call delete('Xautofile')
Bram Moolenaarb03950f2022-07-26 13:47:13 +01003024endfunc
3025
Bram Moolenaareb93f3f2019-04-04 15:04:56 +02003026func Test_autocmd_once()
3027 " Without ++once WinNew triggers twice
3028 let g:did_split = 0
3029 augroup Testing
3030 au WinNew * let g:did_split += 1
3031 augroup END
3032 split
3033 split
3034 call assert_equal(2, g:did_split)
3035 call assert_true(exists('#WinNew'))
3036 close
3037 close
3038
3039 " With ++once WinNew triggers once
3040 let g:did_split = 0
3041 augroup Testing
3042 au!
3043 au WinNew * ++once let g:did_split += 1
3044 augroup END
3045 split
3046 split
3047 call assert_equal(1, g:did_split)
3048 call assert_false(exists('#WinNew'))
3049 close
3050 close
3051
3052 call assert_fails('au WinNew * ++once ++once echo bad', 'E983:')
3053endfunc
3054
Bram Moolenaara68e5952019-04-25 22:22:01 +02003055func Test_autocmd_bufreadpre()
3056 new
3057 let b:bufreadpre = 1
Bram Moolenaarab505b12020-03-23 19:28:44 +01003058 call append(0, range(1000))
Bram Moolenaara68e5952019-04-25 22:22:01 +02003059 w! XAutocmdBufReadPre.txt
3060 autocmd BufReadPre <buffer> :let b:bufreadpre += 1
Bram Moolenaarab505b12020-03-23 19:28:44 +01003061 norm! 500gg
Bram Moolenaara68e5952019-04-25 22:22:01 +02003062 sp
Bram Moolenaarab505b12020-03-23 19:28:44 +01003063 norm! 1000gg
Bram Moolenaara68e5952019-04-25 22:22:01 +02003064 wincmd p
3065 let g:wsv1 = winsaveview()
3066 wincmd p
3067 let g:wsv2 = winsaveview()
3068 " triggers BufReadPre, should not move the cursor in either window
3069 " The topline may change one line in a large window.
3070 edit
3071 call assert_inrange(g:wsv2.topline - 1, g:wsv2.topline + 1, winsaveview().topline)
3072 call assert_equal(g:wsv2.lnum, winsaveview().lnum)
3073 call assert_equal(2, b:bufreadpre)
3074 wincmd p
3075 call assert_equal(g:wsv1.topline, winsaveview().topline)
3076 call assert_equal(g:wsv1.lnum, winsaveview().lnum)
3077 call assert_equal(2, b:bufreadpre)
3078 " Now set the cursor position in an BufReadPre autocommand
3079 " (even though the position will be invalid, this should make Vim reset the
3080 " cursor position in the other window.
3081 wincmd p
3082 set cpo+=g
3083 " won't do anything, but try to set the cursor on an invalid lnum
3084 autocmd BufReadPre <buffer> :norm! 70gg
3085 " triggers BufReadPre, should not move the cursor in either window
3086 e
3087 call assert_equal(1, winsaveview().topline)
3088 call assert_equal(1, winsaveview().lnum)
3089 call assert_equal(3, b:bufreadpre)
3090 wincmd p
3091 call assert_equal(g:wsv1.topline, winsaveview().topline)
3092 call assert_equal(g:wsv1.lnum, winsaveview().lnum)
3093 call assert_equal(3, b:bufreadpre)
3094 close
3095 close
3096 call delete('XAutocmdBufReadPre.txt')
3097 set cpo-=g
3098endfunc
3099
Bram Moolenaar5e66b422019-01-24 21:58:10 +01003100" FileChangedShell tested in test_filechanged.vim
Bram Moolenaar69ea5872019-04-25 20:29:00 +02003101
3102" Tests for the following autocommands:
3103" - FileWritePre writing a compressed file
3104" - FileReadPost reading a compressed file
3105" - BufNewFile reading a file template
3106" - BufReadPre decompressing the file to be read
3107" - FilterReadPre substituting characters in the temp file
3108" - FilterReadPost substituting characters after filtering
3109" - FileReadPre set options for decompression
3110" - FileReadPost decompress the file
3111func Test_ReadWrite_Autocmds()
3112 " Run this test only on Unix-like systems and if gzip is available
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +02003113 CheckUnix
3114 CheckExecutable gzip
Bram Moolenaar69ea5872019-04-25 20:29:00 +02003115
3116 " Make $GZIP empty, "-v" would cause trouble.
3117 let $GZIP = ""
3118
3119 " Use a FileChangedShell autocommand to avoid a prompt for 'Xtestfile.gz'
3120 " being modified outside of Vim (noticed on Solaris).
3121 au FileChangedShell * echo 'caught FileChangedShell'
3122
3123 " Test for the FileReadPost, FileWritePre and FileWritePost autocmds
3124 augroup Test1
3125 au!
3126 au FileWritePre *.gz '[,']!gzip
3127 au FileWritePost *.gz undo
3128 au FileReadPost *.gz '[,']!gzip -d
3129 augroup END
3130
3131 new
3132 set bin
3133 call append(0, [
3134 \ 'line 2 Abcdefghijklmnopqrstuvwxyz',
3135 \ 'line 3 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
3136 \ 'line 4 Abcdefghijklmnopqrstuvwxyz',
3137 \ 'line 5 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
3138 \ 'line 6 Abcdefghijklmnopqrstuvwxyz',
3139 \ 'line 7 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
3140 \ 'line 8 Abcdefghijklmnopqrstuvwxyz',
3141 \ 'line 9 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
3142 \ 'line 10 Abcdefghijklmnopqrstuvwxyz'
3143 \ ])
3144 1,9write! Xtestfile.gz
3145 enew! | close
3146
3147 new
3148 " Read and decompress the testfile
3149 0read Xtestfile.gz
3150 call assert_equal([
3151 \ 'line 2 Abcdefghijklmnopqrstuvwxyz',
3152 \ 'line 3 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
3153 \ 'line 4 Abcdefghijklmnopqrstuvwxyz',
3154 \ 'line 5 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
3155 \ 'line 6 Abcdefghijklmnopqrstuvwxyz',
3156 \ 'line 7 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
3157 \ 'line 8 Abcdefghijklmnopqrstuvwxyz',
3158 \ 'line 9 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
3159 \ 'line 10 Abcdefghijklmnopqrstuvwxyz'
3160 \ ], getline(1, 9))
3161 enew! | close
3162
3163 augroup Test1
3164 au!
3165 augroup END
3166
3167 " Test for the FileAppendPre and FileAppendPost autocmds
3168 augroup Test2
3169 au!
3170 au BufNewFile *.c read Xtest.c
3171 au FileAppendPre *.out '[,']s/new/NEW/
3172 au FileAppendPost *.out !cat Xtest.c >> test.out
3173 augroup END
3174
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01003175 call writefile(['/*', ' * Here is a new .c file', ' */'], 'Xtest.c', 'D')
Bram Moolenaar69ea5872019-04-25 20:29:00 +02003176 new foo.c " should load Xtest.c
3177 call assert_equal(['/*', ' * Here is a new .c file', ' */'], getline(2, 4))
3178 w! >> test.out " append it to the output file
3179
3180 let contents = readfile('test.out')
3181 call assert_equal(' * Here is a NEW .c file', contents[2])
3182 call assert_equal(' * Here is a new .c file', contents[5])
3183
3184 call delete('test.out')
3185 enew! | close
3186 augroup Test2
3187 au!
3188 augroup END
3189
3190 " Test for the BufReadPre and BufReadPost autocmds
3191 augroup Test3
3192 au!
3193 " setup autocommands to decompress before reading and re-compress
3194 " afterwards
3195 au BufReadPre *.gz exe '!gzip -d ' . shellescape(expand("<afile>"))
3196 au BufReadPre *.gz call rename(expand("<afile>:r"), expand("<afile>"))
3197 au BufReadPost *.gz call rename(expand("<afile>"), expand("<afile>:r"))
3198 au BufReadPost *.gz exe '!gzip ' . shellescape(expand("<afile>:r"))
3199 augroup END
3200
3201 e! Xtestfile.gz " Edit compressed file
3202 call assert_equal([
3203 \ 'line 2 Abcdefghijklmnopqrstuvwxyz',
3204 \ 'line 3 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
3205 \ 'line 4 Abcdefghijklmnopqrstuvwxyz',
3206 \ 'line 5 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
3207 \ 'line 6 Abcdefghijklmnopqrstuvwxyz',
3208 \ 'line 7 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
3209 \ 'line 8 Abcdefghijklmnopqrstuvwxyz',
3210 \ 'line 9 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
3211 \ 'line 10 Abcdefghijklmnopqrstuvwxyz'
3212 \ ], getline(1, 9))
3213
3214 w! >> test.out " Append it to the output file
3215
3216 augroup Test3
3217 au!
3218 augroup END
3219
3220 " Test for the FilterReadPre and FilterReadPost autocmds.
3221 set shelltemp " need temp files here
3222 augroup Test4
3223 au!
3224 au FilterReadPre *.out call rename(expand("<afile>"), expand("<afile>") . ".t")
3225 au FilterReadPre *.out exe 'silent !sed s/e/E/ ' . shellescape(expand("<afile>")) . ".t >" . shellescape(expand("<afile>"))
3226 au FilterReadPre *.out exe 'silent !rm ' . shellescape(expand("<afile>")) . '.t'
3227 au FilterReadPost *.out '[,']s/x/X/g
3228 augroup END
3229
3230 e! test.out " Edit the output file
3231 1,$!cat
3232 call assert_equal([
3233 \ 'linE 2 AbcdefghijklmnopqrstuvwXyz',
3234 \ 'linE 3 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
3235 \ 'linE 4 AbcdefghijklmnopqrstuvwXyz',
3236 \ 'linE 5 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
3237 \ 'linE 6 AbcdefghijklmnopqrstuvwXyz',
3238 \ 'linE 7 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
3239 \ 'linE 8 AbcdefghijklmnopqrstuvwXyz',
3240 \ 'linE 9 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
3241 \ 'linE 10 AbcdefghijklmnopqrstuvwXyz'
3242 \ ], getline(1, 9))
3243 call assert_equal([
3244 \ 'line 2 Abcdefghijklmnopqrstuvwxyz',
3245 \ 'line 3 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
3246 \ 'line 4 Abcdefghijklmnopqrstuvwxyz',
3247 \ 'line 5 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
3248 \ 'line 6 Abcdefghijklmnopqrstuvwxyz',
3249 \ 'line 7 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
3250 \ 'line 8 Abcdefghijklmnopqrstuvwxyz',
3251 \ 'line 9 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
3252 \ 'line 10 Abcdefghijklmnopqrstuvwxyz'
3253 \ ], readfile('test.out'))
3254
3255 augroup Test4
3256 au!
3257 augroup END
3258 set shelltemp&vim
3259
3260 " Test for the FileReadPre and FileReadPost autocmds.
3261 augroup Test5
3262 au!
3263 au FileReadPre *.gz exe 'silent !gzip -d ' . shellescape(expand("<afile>"))
3264 au FileReadPre *.gz call rename(expand("<afile>:r"), expand("<afile>"))
3265 au FileReadPost *.gz '[,']s/l/L/
3266 augroup END
3267
3268 new
3269 0r Xtestfile.gz " Read compressed file
3270 call assert_equal([
3271 \ 'Line 2 Abcdefghijklmnopqrstuvwxyz',
3272 \ 'Line 3 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
3273 \ 'Line 4 Abcdefghijklmnopqrstuvwxyz',
3274 \ 'Line 5 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
3275 \ 'Line 6 Abcdefghijklmnopqrstuvwxyz',
3276 \ 'Line 7 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
3277 \ 'Line 8 Abcdefghijklmnopqrstuvwxyz',
3278 \ 'Line 9 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
3279 \ 'Line 10 Abcdefghijklmnopqrstuvwxyz'
3280 \ ], getline(1, 9))
3281 call assert_equal([
3282 \ 'line 2 Abcdefghijklmnopqrstuvwxyz',
3283 \ 'line 3 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
3284 \ 'line 4 Abcdefghijklmnopqrstuvwxyz',
3285 \ 'line 5 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
3286 \ 'line 6 Abcdefghijklmnopqrstuvwxyz',
3287 \ 'line 7 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
3288 \ 'line 8 Abcdefghijklmnopqrstuvwxyz',
3289 \ 'line 9 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
3290 \ 'line 10 Abcdefghijklmnopqrstuvwxyz'
3291 \ ], readfile('Xtestfile.gz'))
3292
3293 augroup Test5
3294 au!
3295 augroup END
3296
3297 au! FileChangedShell
3298 call delete('Xtestfile.gz')
Bram Moolenaar69ea5872019-04-25 20:29:00 +02003299 call delete('test.out')
3300endfunc
Bram Moolenaar23b51392019-05-09 21:38:43 +02003301
3302func Test_throw_in_BufWritePre()
3303 new
3304 call setline(1, ['one', 'two', 'three'])
3305 call assert_false(filereadable('Xthefile'))
3306 augroup throwing
3307 au BufWritePre X* throw 'do not write'
3308 augroup END
3309 try
3310 w Xthefile
3311 catch
3312 let caught = 1
3313 endtry
3314 call assert_equal(1, caught)
3315 call assert_false(filereadable('Xthefile'))
3316
3317 bwipe!
3318 au! throwing
3319endfunc
Bram Moolenaarcadbe1b2019-09-22 21:50:09 +02003320
Bram Moolenaar40fa12a2021-09-22 14:18:13 +02003321func Test_autocmd_in_try_block()
Bram Moolenaar6f14da12022-09-07 21:30:44 +01003322 call mkdir('Xintrydir', 'R')
Bram Moolenaar40fa12a2021-09-22 14:18:13 +02003323 au BufEnter * let g:fname = expand('%')
3324 try
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +01003325 edit Xintrydir/
Bram Moolenaar40fa12a2021-09-22 14:18:13 +02003326 endtry
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +01003327 call assert_match('Xintrydir', g:fname)
Bram Moolenaar40fa12a2021-09-22 14:18:13 +02003328
3329 unlet g:fname
3330 au! BufEnter
Bram Moolenaar40fa12a2021-09-22 14:18:13 +02003331endfunc
3332
Bram Moolenaarcadbe1b2019-09-22 21:50:09 +02003333func Test_autocmd_SafeState()
3334 CheckRunVimInTerminal
3335
3336 let lines =<< trim END
3337 let g:safe = 0
3338 let g:again = ''
3339 au SafeState * let g:safe += 1
3340 au SafeStateAgain * let g:again ..= 'x'
3341 func CallTimer()
3342 call timer_start(10, {id -> execute('let g:again ..= "t"')})
3343 endfunc
3344 END
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01003345 call writefile(lines, 'XSafeState', 'D')
Bram Moolenaarcadbe1b2019-09-22 21:50:09 +02003346 let buf = RunVimInTerminal('-S XSafeState', #{rows: 6})
3347
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01003348 " Sometimes we loop to handle a K_IGNORE, SafeState may be triggered once or
Bram Moolenaar8fb1b472020-02-23 16:16:26 +01003349 " more often.
Bram Moolenaarcadbe1b2019-09-22 21:50:09 +02003350 call term_sendkeys(buf, ":echo g:safe\<CR>")
Bram Moolenaar8fb1b472020-02-23 16:16:26 +01003351 call WaitForAssert({-> assert_match('^\d ', term_getline(buf, 6))}, 1000)
Bram Moolenaarcadbe1b2019-09-22 21:50:09 +02003352
Bram Moolenaar8fb1b472020-02-23 16:16:26 +01003353 " SafeStateAgain should be invoked at least three times
Bram Moolenaarcadbe1b2019-09-22 21:50:09 +02003354 call term_sendkeys(buf, ":echo g:again\<CR>")
Bram Moolenaar8fb1b472020-02-23 16:16:26 +01003355 call WaitForAssert({-> assert_match('^xxx', term_getline(buf, 6))}, 1000)
Bram Moolenaarcadbe1b2019-09-22 21:50:09 +02003356
3357 call term_sendkeys(buf, ":let g:again = ''\<CR>:call CallTimer()\<CR>")
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02003358 call TermWait(buf, 50)
Bram Moolenaar0f6629a2019-09-22 23:24:13 +02003359 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02003360 call TermWait(buf, 50)
Bram Moolenaarcadbe1b2019-09-22 21:50:09 +02003361 call term_sendkeys(buf, ":echo g:again\<CR>")
3362 call WaitForAssert({-> assert_match('xtx', term_getline(buf, 6))}, 1000)
3363
3364 call StopVimInTerminal(buf)
Bram Moolenaarcadbe1b2019-09-22 21:50:09 +02003365endfunc
Bram Moolenaar23324a02019-10-01 17:39:04 +02003366
3367func Test_autocmd_CmdWinEnter()
3368 CheckRunVimInTerminal
Bram Moolenaar21829c52021-01-26 22:42:21 +01003369
Bram Moolenaar23324a02019-10-01 17:39:04 +02003370 let lines =<< trim END
Egor Zvorykin125ffd22021-11-17 14:01:14 +00003371 augroup vimHints | au! | augroup END
Bram Moolenaar23324a02019-10-01 17:39:04 +02003372 let b:dummy_var = 'This is a dummy'
3373 autocmd CmdWinEnter * quit
3374 let winnr = winnr('$')
3375 END
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01003376 let filename = 'XCmdWinEnter'
Bram Moolenaar23324a02019-10-01 17:39:04 +02003377 call writefile(lines, filename)
3378 let buf = RunVimInTerminal('-S '.filename, #{rows: 6})
3379
3380 call term_sendkeys(buf, "q:")
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02003381 call TermWait(buf)
Bram Moolenaar23324a02019-10-01 17:39:04 +02003382 call term_sendkeys(buf, ":echo b:dummy_var\<cr>")
Bram Moolenaar353c3512020-03-15 14:19:26 +01003383 call WaitForAssert({-> assert_match('^This is a dummy', term_getline(buf, 6))}, 2000)
Bram Moolenaar23324a02019-10-01 17:39:04 +02003384 call term_sendkeys(buf, ":echo &buftype\<cr>")
3385 call WaitForAssert({-> assert_notmatch('^nofile', term_getline(buf, 6))}, 1000)
3386 call term_sendkeys(buf, ":echo winnr\<cr>")
3387 call WaitForAssert({-> assert_match('^1', term_getline(buf, 6))}, 1000)
3388
3389 " clean up
3390 call StopVimInTerminal(buf)
3391 call delete(filename)
3392endfunc
Bram Moolenaarec66c412019-10-11 21:19:13 +02003393
3394func Test_autocmd_was_using_freed_memory()
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01003395 CheckFeature quickfix
3396
Bram Moolenaarec66c412019-10-11 21:19:13 +02003397 pedit xx
3398 n x
Bram Moolenaar92bb83e2021-02-03 23:04:46 +01003399 augroup winenter
3400 au WinEnter * if winnr('$') > 2 | quit | endif
3401 augroup END
Bram Moolenaarec66c412019-10-11 21:19:13 +02003402 split
Bram Moolenaar92bb83e2021-02-03 23:04:46 +01003403
3404 augroup winenter
3405 au! WinEnter
3406 augroup END
3407
3408 bwipe xx
3409 bwipe x
3410 pclose
Bram Moolenaarec66c412019-10-11 21:19:13 +02003411endfunc
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +01003412
3413func Test_BufWrite_lockmarks()
Bram Moolenaarf08b0eb2021-10-16 13:00:14 +01003414 let g:test_is_flaky = 1
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +01003415 edit! Xtest
3416 call setline(1, ['a', 'b', 'c', 'd'])
3417
3418 " :lockmarks preserves the marks
3419 call SetChangeMarks(2, 3)
3420 lockmarks write
3421 call assert_equal([2, 3], [line("'["), line("']")])
3422
3423 " *WritePre autocmds get the correct line range, but lockmarks preserves the
3424 " original values for the user
3425 augroup lockmarks
3426 au!
3427 au BufWritePre,FilterWritePre * call assert_equal([1, 4], [line("'["), line("']")])
3428 au FileWritePre * call assert_equal([3, 4], [line("'["), line("']")])
3429 augroup END
3430
3431 lockmarks write
3432 call assert_equal([2, 3], [line("'["), line("']")])
3433
3434 if executable('cat')
3435 lockmarks %!cat
3436 call assert_equal([2, 3], [line("'["), line("']")])
3437 endif
3438
3439 lockmarks 3,4write Xtest2
3440 call assert_equal([2, 3], [line("'["), line("']")])
3441
3442 au! lockmarks
3443 augroup! lockmarks
3444 call delete('Xtest')
3445 call delete('Xtest2')
3446endfunc
Bram Moolenaarce6db022020-01-07 20:11:42 +01003447
3448func Test_FileType_spell()
3449 if !isdirectory('/tmp')
3450 throw "Skipped: requires /tmp directory"
3451 endif
3452
3453 " this was crashing with an invalid free()
3454 setglobal spellfile=/tmp/en.utf-8.add
3455 augroup crash
3456 autocmd!
3457 autocmd BufNewFile,BufReadPost crashfile setf somefiletype
3458 autocmd BufNewFile,BufReadPost crashfile set ft=anotherfiletype
3459 autocmd FileType anotherfiletype setlocal spell
3460 augroup END
3461 func! NoCrash() abort
3462 edit /tmp/crashfile
3463 endfunc
3464 call NoCrash()
3465
3466 au! crash
3467 setglobal spellfile=
3468endfunc
Bram Moolenaarbc2b71d2020-02-17 21:33:30 +01003469
Bram Moolenaaref976322022-09-28 11:48:30 +01003470" this was wiping out the current buffer and using freed memory
3471func Test_SpellFileMissing_bwipe()
3472 next 0
3473 au SpellFileMissing 0 bwipe
3474 call assert_fails('set spell spelllang=0', 'E937:')
3475
3476 au! SpellFileMissing
Bram Moolenaar0a60f792022-11-19 21:18:11 +00003477 set nospell spelllang=en
Bram Moolenaaref976322022-09-28 11:48:30 +01003478 bwipe
3479endfunc
3480
Bram Moolenaar406cd902020-02-18 21:54:41 +01003481" Test closing a window or editing another buffer from a FileChangedRO handler
3482" in a readonly buffer
3483func Test_FileChangedRO_winclose()
Bram Moolenaar62cd26a2020-10-11 20:08:44 +02003484 call test_override('ui_delay', 10)
3485
Bram Moolenaar406cd902020-02-18 21:54:41 +01003486 augroup FileChangedROTest
3487 au!
3488 autocmd FileChangedRO * quit
3489 augroup END
3490 new
3491 set readonly
3492 call assert_fails('normal i', 'E788:')
3493 close
3494 augroup! FileChangedROTest
3495
3496 augroup FileChangedROTest
3497 au!
Bram Moolenaar61abe7d2022-08-30 21:46:08 +01003498 autocmd FileChangedRO * edit Xrofile
Bram Moolenaar406cd902020-02-18 21:54:41 +01003499 augroup END
3500 new
3501 set readonly
3502 call assert_fails('normal i', 'E788:')
3503 close
3504 augroup! FileChangedROTest
Bram Moolenaar62cd26a2020-10-11 20:08:44 +02003505 call test_override('ALL', 0)
Bram Moolenaar406cd902020-02-18 21:54:41 +01003506endfunc
3507
Bram Moolenaar0c81d1b2020-02-22 22:45:55 +01003508func LogACmd()
3509 call add(g:logged, line('$'))
3510endfunc
3511
3512func Test_TermChanged()
Bram Moolenaard28e0b32020-02-22 23:08:52 +01003513 CheckNotGui
3514
Bram Moolenaar0c81d1b2020-02-22 22:45:55 +01003515 enew!
3516 tabnew
3517 call setline(1, ['a', 'b', 'c', 'd'])
3518 $
3519 au TermChanged * call LogACmd()
3520 let g:logged = []
3521 let term_save = &term
3522 set term=xterm
3523 call assert_equal([1, 4], g:logged)
3524
3525 au! TermChanged
3526 let &term = term_save
3527 bwipe!
3528endfunc
3529
Bram Moolenaare3284872020-03-19 13:55:03 +01003530" Test for FileReadCmd autocmd
3531func Test_autocmd_FileReadCmd()
3532 func ReadFileCmd()
3533 call append(line('$'), "v:cmdarg = " .. v:cmdarg)
3534 endfunc
3535 augroup FileReadCmdTest
3536 au!
3537 au FileReadCmd Xtest call ReadFileCmd()
3538 augroup END
3539
3540 new
3541 read ++bin Xtest
3542 read ++nobin Xtest
3543 read ++edit Xtest
3544 read ++bad=keep Xtest
3545 read ++bad=drop Xtest
3546 read ++bad=- Xtest
3547 read ++ff=unix Xtest
3548 read ++ff=dos Xtest
3549 read ++ff=mac Xtest
3550 read ++enc=utf-8 Xtest
3551
3552 call assert_equal(['',
3553 \ 'v:cmdarg = ++bin',
3554 \ 'v:cmdarg = ++nobin',
3555 \ 'v:cmdarg = ++edit',
3556 \ 'v:cmdarg = ++bad=keep',
3557 \ 'v:cmdarg = ++bad=drop',
3558 \ 'v:cmdarg = ++bad=-',
3559 \ 'v:cmdarg = ++ff=unix',
3560 \ 'v:cmdarg = ++ff=dos',
3561 \ 'v:cmdarg = ++ff=mac',
3562 \ 'v:cmdarg = ++enc=utf-8'], getline(1, '$'))
3563
Bram Moolenaar23526d22022-12-05 15:50:41 +00003564 bwipe!
Bram Moolenaare3284872020-03-19 13:55:03 +01003565 augroup FileReadCmdTest
3566 au!
3567 augroup END
3568 delfunc ReadFileCmd
3569endfunc
3570
Bram Moolenaaree4e0c12020-04-06 21:35:05 +02003571" Test for passing invalid arguments to autocmd
3572func Test_autocmd_invalid_args()
3573 " Additional character after * for event
Bram Moolenaar61abe7d2022-08-30 21:46:08 +01003574 call assert_fails('autocmd *a Xinvfile set ff=unix', 'E215:')
Bram Moolenaaree4e0c12020-04-06 21:35:05 +02003575 augroup Test
3576 augroup END
3577 " Invalid autocmd event
Bram Moolenaar61abe7d2022-08-30 21:46:08 +01003578 call assert_fails('autocmd Bufabc Xinvfile set ft=vim', 'E216:')
Bram Moolenaaree4e0c12020-04-06 21:35:05 +02003579 " Invalid autocmd event in a autocmd group
Bram Moolenaar61abe7d2022-08-30 21:46:08 +01003580 call assert_fails('autocmd Test Bufabc Xinvfile set ft=vim', 'E216:')
Bram Moolenaaree4e0c12020-04-06 21:35:05 +02003581 augroup! Test
3582 " Execute all autocmds
3583 call assert_fails('doautocmd * BufEnter', 'E217:')
3584 call assert_fails('augroup! x1a2b3', 'E367:')
3585 call assert_fails('autocmd BufNew <buffer=999> pwd', 'E680:')
Bram Moolenaar531be472020-09-23 22:38:05 +02003586 call assert_fails('autocmd BufNew \) set ff=unix', 'E55:')
Bram Moolenaaree4e0c12020-04-06 21:35:05 +02003587endfunc
3588
3589" Test for deep nesting of autocmds
3590func Test_autocmd_deep_nesting()
Bram Moolenaar61abe7d2022-08-30 21:46:08 +01003591 autocmd BufEnter Xdeepfile doautocmd BufEnter Xdeepfile
3592 call assert_fails('doautocmd BufEnter Xdeepfile', 'E218:')
3593 autocmd! BufEnter Xdeepfile
Bram Moolenaaree4e0c12020-04-06 21:35:05 +02003594endfunc
3595
Bram Moolenaarbe5ee862020-06-10 20:56:58 +02003596" Tests for SigUSR1 autocmd event, which is only available on posix systems.
3597func Test_autocmd_sigusr1()
3598 CheckUnix
Bram Moolenaar0056ca72022-09-23 21:26:39 +01003599 " FIXME: should this work on MacOS M1?
3600 CheckNotMacM1
Bram Moolenaar62cd26a2020-10-11 20:08:44 +02003601 CheckExecutable /bin/kill
Bram Moolenaarbe5ee862020-06-10 20:56:58 +02003602
3603 let g:sigusr1_passed = 0
3604 au SigUSR1 * let g:sigusr1_passed = 1
3605 call system('/bin/kill -s usr1 ' . getpid())
3606 call WaitForAssert({-> assert_true(g:sigusr1_passed)})
3607
3608 au! SigUSR1
3609 unlet g:sigusr1_passed
3610endfunc
3611
Bram Moolenaarb340bae2020-06-15 19:51:56 +02003612" Test for BufReadPre autocmd deleting the file
3613func Test_BufReadPre_delfile()
3614 augroup TestAuCmd
3615 au!
Bram Moolenaare7cda972022-08-29 11:02:59 +01003616 autocmd BufReadPre XbufreadPre call delete('XbufreadPre')
Bram Moolenaarb340bae2020-06-15 19:51:56 +02003617 augroup END
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01003618 call writefile([], 'XbufreadPre', 'D')
Bram Moolenaare7cda972022-08-29 11:02:59 +01003619 call assert_fails('new XbufreadPre', 'E200:')
3620 call assert_equal('XbufreadPre', @%)
Bram Moolenaarb340bae2020-06-15 19:51:56 +02003621 call assert_equal(1, &readonly)
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01003622
Bram Moolenaarb340bae2020-06-15 19:51:56 +02003623 augroup TestAuCmd
3624 au!
3625 augroup END
3626 close!
3627endfunc
3628
3629" Test for BufReadPre autocmd changing the current buffer
3630func Test_BufReadPre_changebuf()
3631 augroup TestAuCmd
3632 au!
Bram Moolenaare7cda972022-08-29 11:02:59 +01003633 autocmd BufReadPre Xchangebuf edit Xsomeotherfile
Bram Moolenaarb340bae2020-06-15 19:51:56 +02003634 augroup END
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01003635 call writefile([], 'Xchangebuf', 'D')
Bram Moolenaare7cda972022-08-29 11:02:59 +01003636 call assert_fails('new Xchangebuf', 'E201:')
Bram Moolenaarb340bae2020-06-15 19:51:56 +02003637 call assert_equal('Xsomeotherfile', @%)
3638 call assert_equal(1, &readonly)
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01003639
Bram Moolenaarb340bae2020-06-15 19:51:56 +02003640 augroup TestAuCmd
3641 au!
3642 augroup END
3643 close!
3644endfunc
3645
3646" Test for BufWipeouti autocmd changing the current buffer when reading a file
3647" in an empty buffer with 'f' flag in 'cpo'
3648func Test_BufDelete_changebuf()
3649 new
3650 augroup TestAuCmd
3651 au!
3652 autocmd BufWipeout * let bufnr = bufadd('somefile') | exe "b " .. bufnr
3653 augroup END
3654 let save_cpo = &cpo
3655 set cpo+=f
Bram Moolenaarb18b4962022-09-02 21:55:50 +01003656 call assert_fails('r Xchangebuf', ['E812:', 'E484:'])
Bram Moolenaarb340bae2020-06-15 19:51:56 +02003657 call assert_equal('somefile', @%)
3658 let &cpo = save_cpo
3659 augroup TestAuCmd
3660 au!
3661 augroup END
3662 close!
3663endfunc
3664
Bram Moolenaar0fe937f2020-06-16 22:42:04 +02003665" Test for the temporary internal window used to execute autocmds
3666func Test_autocmd_window()
3667 %bw!
3668 edit one.txt
3669 tabnew two.txt
Bram Moolenaar41cd8032021-03-13 15:47:56 +01003670 vnew three.txt
3671 tabnew four.txt
3672 tabprevious
Bram Moolenaar0fe937f2020-06-16 22:42:04 +02003673 let g:blist = []
Bram Moolenaar832adf92020-06-25 19:01:36 +02003674 augroup aucmd_win_test1
Bram Moolenaar0fe937f2020-06-16 22:42:04 +02003675 au!
3676 au BufEnter * call add(g:blist, [expand('<afile>'),
3677 \ win_gettype(bufwinnr(expand('<afile>')))])
3678 augroup END
3679
3680 doautoall BufEnter
Bram Moolenaar41cd8032021-03-13 15:47:56 +01003681 call assert_equal([
3682 \ ['one.txt', 'autocmd'],
3683 \ ['two.txt', ''],
3684 \ ['four.txt', 'autocmd'],
3685 \ ['three.txt', ''],
3686 \ ], g:blist)
Bram Moolenaar0fe937f2020-06-16 22:42:04 +02003687
Bram Moolenaar832adf92020-06-25 19:01:36 +02003688 augroup aucmd_win_test1
Bram Moolenaar0fe937f2020-06-16 22:42:04 +02003689 au!
3690 augroup END
Bram Moolenaar832adf92020-06-25 19:01:36 +02003691 augroup! aucmd_win_test1
3692 %bw!
3693endfunc
3694
3695" Test for trying to close the temporary window used for executing an autocmd
3696func Test_close_autocmd_window()
3697 %bw!
3698 edit one.txt
3699 tabnew two.txt
3700 augroup aucmd_win_test2
3701 au!
3702 au BufEnter * if expand('<afile>') == 'one.txt' | 1close | endif
3703 augroup END
3704
3705 call assert_fails('doautoall BufEnter', 'E813:')
3706
3707 augroup aucmd_win_test2
3708 au!
3709 augroup END
3710 augroup! aucmd_win_test2
Bram Moolenaarcf844172020-06-26 19:44:06 +02003711 %bwipe!
3712endfunc
3713
3714" Test for trying to close the tab that has the temporary window for exeucing
3715" an autocmd.
3716func Test_close_autocmd_tab()
3717 edit one.txt
3718 tabnew two.txt
3719 augroup aucmd_win_test
3720 au!
3721 au BufEnter * if expand('<afile>') == 'one.txt' | tabfirst | tabonly | endif
3722 augroup END
3723
3724 call assert_fails('doautoall BufEnter', 'E813:')
3725
3726 tabonly
3727 augroup aucmd_win_test
3728 au!
3729 augroup END
3730 augroup! aucmd_win_test
3731 %bwipe!
Bram Moolenaar0fe937f2020-06-16 22:42:04 +02003732endfunc
3733
Bram Moolenaarcb1956d2022-01-07 15:45:18 +00003734func Test_Visual_doautoall_redraw()
3735 call setline(1, ['a', 'b'])
Bram Moolenaar94722c52023-01-28 19:19:03 +00003736 new
Bram Moolenaarcb1956d2022-01-07 15:45:18 +00003737 wincmd p
3738 call feedkeys("G\<C-V>", 'txn')
3739 autocmd User Explode ++once redraw
3740 doautoall User Explode
3741 %bwipe!
3742endfunc
3743
Bram Moolenaar6bcb8772021-02-03 21:23:29 +01003744" This was using freed memory.
3745func Test_BufNew_arglocal()
3746 arglocal
3747 au BufNew * arglocal
3748 call assert_fails('drop xx', 'E1156:')
3749
3750 au! BufNew
3751endfunc
3752
Bram Moolenaar8ab37572021-02-03 21:56:59 +01003753func Test_autocmd_closes_window()
3754 au BufNew,BufWinLeave * e %e
3755 file yyy
3756 au BufNew,BufWinLeave * ball
Bram Moolenaaraad5f9d2021-02-06 17:30:31 +01003757 n xxx
Bram Moolenaar8ab37572021-02-03 21:56:59 +01003758
Bram Moolenaaraad5f9d2021-02-06 17:30:31 +01003759 %bwipe
Bram Moolenaar8ab37572021-02-03 21:56:59 +01003760 au! BufNew
3761 au! BufWinLeave
3762endfunc
3763
Bram Moolenaar92bb83e2021-02-03 23:04:46 +01003764func Test_autocmd_quit_psearch()
3765 sn aa bb
3766 augroup aucmd_win_test
3767 au!
3768 au BufEnter,BufLeave,BufNew,WinEnter,WinLeave,WinNew * if winnr('$') > 1 | q | endif
3769 augroup END
3770 ps /
3771
3772 augroup aucmd_win_test
3773 au!
3774 augroup END
zeertzjq7851c692022-04-21 11:14:01 +01003775 new
3776 pclose
Bram Moolenaar92bb83e2021-02-03 23:04:46 +01003777endfunc
3778
Bram Moolenaaraad5f9d2021-02-06 17:30:31 +01003779" Fuzzer found some strange combination that caused a crash.
3780func Test_autocmd_normal_mess()
Bram Moolenaardd07c022021-02-07 13:32:46 +01003781 " For unknown reason this hangs on MS-Windows
3782 CheckNotMSWindows
3783
Bram Moolenaaraad5f9d2021-02-06 17:30:31 +01003784 augroup aucmd_normal_test
3785 au BufLeave,BufWinLeave,BufHidden,BufUnload,BufDelete,BufWipeout * norm 7q/qc
3786 augroup END
zeertzjq67fe77d2025-04-20 10:21:18 +02003787 call assert_fails('o4', 'E1159:')
Bram Moolenaaraad5f9d2021-02-06 17:30:31 +01003788 silent! H
zeertzjq67fe77d2025-04-20 10:21:18 +02003789 call assert_fails('e xx', 'E1159:')
Bram Moolenaaraad5f9d2021-02-06 17:30:31 +01003790 normal G
3791
3792 augroup aucmd_normal_test
3793 au!
3794 augroup END
3795endfunc
3796
Bram Moolenaar8c6951f2021-02-06 18:08:45 +01003797func Test_autocmd_closing_cmdwin()
Bram Moolenaardd07c022021-02-07 13:32:46 +01003798 " For unknown reason this hangs on MS-Windows
3799 CheckNotMSWindows
3800
Bram Moolenaar8c6951f2021-02-06 18:08:45 +01003801 au BufWinLeave * nested q
3802 call assert_fails("norm 7q?\n", 'E855:')
3803
3804 au! BufWinLeave
3805 new
3806 only
3807endfunc
3808
Bram Moolenaar2c7080b2021-02-06 19:19:42 +01003809func Test_autocmd_vimgrep()
3810 augroup aucmd_vimgrep
Charlie Grovesfef44852022-04-19 16:24:12 +01003811 au QuickfixCmdPre,BufNew,BufReadCmd * sb
zeertzjq7851c692022-04-21 11:14:01 +01003812 au QuickfixCmdPre,BufNew,BufReadCmd * q9
Bram Moolenaar2c7080b2021-02-06 19:19:42 +01003813 augroup END
Bram Moolenaardd07c022021-02-07 13:32:46 +01003814 call assert_fails('lv ?a? foo', 'E926:')
Bram Moolenaar2c7080b2021-02-06 19:19:42 +01003815
3816 augroup aucmd_vimgrep
3817 au!
3818 augroup END
3819endfunc
3820
Bram Moolenaar73b8b0a2021-08-01 14:52:32 +02003821func Test_autocmd_with_block()
3822 augroup block_testing
3823 au BufReadPost *.xml {
3824 setlocal matchpairs+=<:>
3825 /<start
3826 }
Bram Moolenaar63b91732021-08-05 20:40:03 +02003827 au CursorHold * {
3828 autocmd BufReadPre * ++once echo 'one' | echo 'two'
3829 g:gotSafeState = 77
3830 }
Bram Moolenaar73b8b0a2021-08-01 14:52:32 +02003831 augroup END
3832
Ken Takataeccc9272024-09-03 23:01:55 +02003833 let expected = gettext("\n--- Autocommands ---") .. "\nblock_testing BufRead\n *.xml {^@ setlocal matchpairs+=<:>^@ /<start^@ }"
Bram Moolenaar73b8b0a2021-08-01 14:52:32 +02003834 call assert_equal(expected, execute('au BufReadPost *.xml'))
3835
Bram Moolenaar63b91732021-08-05 20:40:03 +02003836 doautocmd CursorHold
3837 call assert_equal(77, g:gotSafeState)
3838 unlet g:gotSafeState
3839
Bram Moolenaar73b8b0a2021-08-01 14:52:32 +02003840 augroup block_testing
3841 au!
Bram Moolenaar75ebd2a2022-06-03 17:39:46 +01003842 autocmd CursorHold * {
3843 if true
3844 # comment
3845 && true
3846
3847 && true
3848 g:done = 'yes'
3849 endif
3850 }
3851 augroup END
3852 doautocmd CursorHold
3853 call assert_equal('yes', g:done)
3854
3855 unlet g:done
3856 augroup block_testing
3857 au!
Bram Moolenaar73b8b0a2021-08-01 14:52:32 +02003858 augroup END
3859endfunc
3860
Bram Moolenaar6f2465d2022-03-22 18:13:01 +00003861func Test_closing_autocmd_window()
3862 let lines =<< trim END
3863 edit Xa.txt
3864 tabnew Xb.txt
3865 autocmd BufEnter Xa.txt unhide 1
3866 doautoall BufEnter
3867 END
3868 call v9.CheckScriptFailure(lines, 'E814:')
3869 au! BufEnter
Bram Moolenaar6f2465d2022-03-22 18:13:01 +00003870 bwipe Xa.txt
3871 bwipe Xb.txt
3872endfunc
3873
zeertzjq46bdae02023-09-24 23:16:08 +02003874func Test_switch_window_in_autocmd_window()
3875 edit Xa.txt
3876 tabnew Xb.txt
3877 autocmd BufEnter Xa.txt wincmd w
3878 doautoall BufEnter
3879 au! BufEnter
3880 bwipe Xa.txt
3881 call assert_false(bufexists('Xa.txt'))
3882 bwipe Xb.txt
3883 call assert_false(bufexists('Xb.txt'))
3884endfunc
3885
zeertzjq9d956ee2024-04-07 18:16:10 +02003886" Test that using the autocommand window doesn't change current directory.
3887func Test_autocmd_window_cwd()
3888 let saveddir = getcwd()
3889 call mkdir('Xcwd/a/b/c/d', 'pR')
3890
3891 new Xa.txt
3892 tabnew
3893 new Xb.txt
3894
3895 tabprev
3896 cd Xcwd
3897 call assert_match('/Xcwd$', getcwd())
3898 call assert_match('\[global\] .*/Xcwd$', trim(execute('verbose pwd')))
3899
3900 autocmd BufEnter Xb.txt lcd ./a/b/c/d
3901 doautoall BufEnter
3902 au! BufEnter
3903 call assert_match('/Xcwd$', getcwd())
3904 call assert_match('\[global\] .*/Xcwd$', trim(execute('verbose pwd')))
3905
3906 tabnext
3907 cd ./a
3908 tcd ./b
3909 lcd ./c
3910 call assert_match('/Xcwd/a/b/c$', getcwd())
3911 call assert_match('\[window\] .*/Xcwd/a/b/c$', trim(execute('verbose pwd')))
3912
3913 autocmd BufEnter Xa.txt call assert_match('Xcwd/a/b/c$', getcwd())
3914 doautoall BufEnter
3915 au! BufEnter
3916 call assert_match('/Xcwd/a/b/c$', getcwd())
3917 call assert_match('\[window\] .*/Xcwd/a/b/c$', trim(execute('verbose pwd')))
3918 bwipe!
3919 call assert_match('/Xcwd/a/b$', getcwd())
3920 call assert_match('\[tabpage\] .*/Xcwd/a/b$', trim(execute('verbose pwd')))
3921 bwipe!
3922 call assert_match('/Xcwd/a$', getcwd())
3923 call assert_match('\[global\] .*/Xcwd/a$', trim(execute('verbose pwd')))
3924 bwipe!
3925
3926 call chdir(saveddir)
3927endfunc
3928
Bram Moolenaar347538f2022-03-26 16:28:06 +00003929func Test_bufwipeout_changes_window()
3930 " This should not crash, but we don't have any expectations about what
3931 " happens, changing window in BufWipeout has unpredictable results.
3932 tabedit
3933 let g:window_id = win_getid()
3934 topleft new
3935 setlocal bufhidden=wipe
3936 autocmd BufWipeout <buffer> call win_gotoid(g:window_id)
3937 tabprevious
3938 +tabclose
3939
3940 unlet g:window_id
3941 au! BufWipeout
3942 %bwipe!
3943endfunc
3944
zeertzjq021996f2022-04-10 11:44:04 +01003945func Test_v_event_readonly()
3946 autocmd CompleteChanged * let v:event.width = 0
3947 call assert_fails("normal! i\<C-X>\<C-V>", 'E46:')
3948 au! CompleteChanged
3949
3950 autocmd DirChangedPre * let v:event.directory = ''
3951 call assert_fails('cd .', 'E46:')
3952 au! DirChangedPre
3953
3954 autocmd ModeChanged * let v:event.new_mode = ''
3955 call assert_fails('normal! cc', 'E46:')
3956 au! ModeChanged
3957
3958 autocmd TextYankPost * let v:event.operator = ''
3959 call assert_fails('normal! yy', 'E46:')
3960 au! TextYankPost
3961endfunc
3962
zeertzjqc9e8fd62022-07-26 18:12:38 +01003963" Test for ModeChanged pattern
3964func Test_mode_changes()
3965 let g:index = 0
zeertzjq73916ba2023-04-26 16:50:19 +01003966 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 +01003967 func! TestMode()
3968 call assert_equal(g:mode_seq[g:index], get(v:event, "old_mode"))
3969 call assert_equal(g:mode_seq[g:index + 1], get(v:event, "new_mode"))
3970 call assert_equal(mode(1), get(v:event, "new_mode"))
3971 let g:index += 1
3972 endfunc
3973
3974 au ModeChanged * :call TestMode()
3975 let g:n_to_any = 0
3976 au ModeChanged n:* let g:n_to_any += 1
zeertzjq73916ba2023-04-26 16:50:19 +01003977 call feedkeys("i\<esc>vVca\<CR>\<C-X>\<C-L>\<esc>ggdV\<MouseMove>G", 'tnix')
zeertzjqc9e8fd62022-07-26 18:12:38 +01003978
3979 let g:V_to_v = 0
3980 au ModeChanged V:v let g:V_to_v += 1
3981 call feedkeys("Vv\<C-G>\<esc>", 'tnix')
3982 call assert_equal(len(filter(g:mode_seq[1:], {idx, val -> val == 'n'})), g:n_to_any)
3983 call assert_equal(1, g:V_to_v)
3984 call assert_equal(len(g:mode_seq) - 1, g:index)
3985
3986 let g:n_to_i = 0
3987 au ModeChanged n:i let g:n_to_i += 1
3988 let g:n_to_niI = 0
3989 au ModeChanged i:niI let g:n_to_niI += 1
3990 let g:niI_to_i = 0
3991 au ModeChanged niI:i let g:niI_to_i += 1
3992 let g:nany_to_i = 0
3993 au ModeChanged n*:i let g:nany_to_i += 1
3994 let g:i_to_n = 0
3995 au ModeChanged i:n let g:i_to_n += 1
3996 let g:nori_to_any = 0
3997 au ModeChanged [ni]:* let g:nori_to_any += 1
3998 let g:i_to_any = 0
3999 au ModeChanged i:* let g:i_to_any += 1
4000 let g:index = 0
4001 let g:mode_seq = ['n', 'i', 'niI', 'i', 'n']
4002 call feedkeys("a\<C-O>l\<esc>", 'tnix')
4003 call assert_equal(len(g:mode_seq) - 1, g:index)
4004 call assert_equal(1, g:n_to_i)
4005 call assert_equal(1, g:n_to_niI)
4006 call assert_equal(1, g:niI_to_i)
4007 call assert_equal(2, g:nany_to_i)
4008 call assert_equal(1, g:i_to_n)
4009 call assert_equal(2, g:i_to_any)
4010 call assert_equal(3, g:nori_to_any)
4011
4012 if has('terminal')
4013 let g:mode_seq += ['c', 'n', 't', 'nt', 'c', 'nt', 'n']
4014 call feedkeys(":term\<CR>\<C-W>N:bd!\<CR>", 'tnix')
4015 call assert_equal(len(g:mode_seq) - 1, g:index)
4016 call assert_equal(1, g:n_to_i)
4017 call assert_equal(1, g:n_to_niI)
4018 call assert_equal(1, g:niI_to_i)
4019 call assert_equal(2, g:nany_to_i)
4020 call assert_equal(1, g:i_to_n)
4021 call assert_equal(2, g:i_to_any)
4022 call assert_equal(5, g:nori_to_any)
4023 endif
4024
zeertzjqd1955982022-10-05 11:24:46 +01004025 let g:n_to_c = 0
4026 au ModeChanged n:c let g:n_to_c += 1
4027 let g:c_to_n = 0
4028 au ModeChanged c:n let g:c_to_n += 1
4029 let g:mode_seq += ['c', 'n', 'c', 'n']
4030 call feedkeys("q:\<C-C>\<Esc>", 'tnix')
4031 call assert_equal(len(g:mode_seq) - 1, g:index)
4032 call assert_equal(2, g:n_to_c)
4033 call assert_equal(2, g:c_to_n)
zeertzjqc9e8fd62022-07-26 18:12:38 +01004034
Bram Moolenaar61c4b042022-10-18 15:10:11 +01004035 let g:n_to_v = 0
4036 au ModeChanged n:v let g:n_to_v += 1
4037 let g:v_to_n = 0
4038 au ModeChanged v:n let g:v_to_n += 1
4039 let g:mode_seq += ['v', 'n']
4040 call feedkeys("v\<C-C>", 'tnix')
4041 call assert_equal(len(g:mode_seq) - 1, g:index)
4042 call assert_equal(1, g:n_to_v)
4043 call assert_equal(1, g:v_to_n)
zeertzjqfcaeb3d2023-11-28 20:46:29 +01004044
4045 let g:mode_seq += ['c', 'cr', 'c', 'cr', 'n']
4046 call feedkeys(":\<Insert>\<Insert>\<Insert>\<CR>", 'tnix')
4047 call assert_equal(len(g:mode_seq) - 1, g:index)
Bram Moolenaar61c4b042022-10-18 15:10:11 +01004048
zeertzjqc9e8fd62022-07-26 18:12:38 +01004049 au! ModeChanged
4050 delfunc TestMode
4051 unlet! g:mode_seq
4052 unlet! g:index
4053 unlet! g:n_to_any
4054 unlet! g:V_to_v
4055 unlet! g:n_to_i
4056 unlet! g:n_to_niI
4057 unlet! g:niI_to_i
4058 unlet! g:nany_to_i
4059 unlet! g:i_to_n
4060 unlet! g:nori_to_any
4061 unlet! g:i_to_any
zeertzjqfcaeb3d2023-11-28 20:46:29 +01004062 unlet! g:n_to_c
4063 unlet! g:c_to_n
4064 unlet! g:n_to_v
4065 unlet! g:v_to_n
zeertzjqc9e8fd62022-07-26 18:12:38 +01004066endfunc
4067
4068func Test_recursive_ModeChanged()
4069 au! ModeChanged * norm 0u
4070 sil! norm 
4071 au! ModeChanged
4072endfunc
4073
4074func Test_ModeChanged_starts_visual()
4075 " This was triggering ModeChanged before setting VIsual, causing a crash.
4076 au! ModeChanged * norm 0u
4077 sil! norm 
4078
4079 au! ModeChanged
4080endfunc
Bram Moolenaar347538f2022-03-26 16:28:06 +00004081
Charlie Grovesfef44852022-04-19 16:24:12 +01004082func Test_noname_autocmd()
4083 augroup test_noname_autocmd_group
4084 autocmd!
4085 autocmd BufEnter * call add(s:li, ["BufEnter", expand("<afile>")])
4086 autocmd BufDelete * call add(s:li, ["BufDelete", expand("<afile>")])
4087 autocmd BufLeave * call add(s:li, ["BufLeave", expand("<afile>")])
4088 autocmd BufUnload * call add(s:li, ["BufUnload", expand("<afile>")])
4089 autocmd BufWipeout * call add(s:li, ["BufWipeout", expand("<afile>")])
4090 augroup END
4091
4092 let s:li = []
4093 edit foo
4094 call assert_equal([['BufUnload', ''], ['BufDelete', ''], ['BufWipeout', ''], ['BufEnter', 'foo']], s:li)
4095
4096 au! test_noname_autocmd_group
4097 augroup! test_noname_autocmd_group
4098endfunc
4099
Yegappan Lakshmanan1755a912022-05-19 10:31:47 +01004100" Test for the autocmd_get() function
4101func Test_autocmd_get()
4102 augroup TestAutoCmdFns
4103 au!
4104 autocmd BufAdd *.vim echo "bufadd-vim"
4105 autocmd BufAdd *.py echo "bufadd-py"
4106 autocmd BufHidden *.vim echo "bufhidden"
4107 augroup END
4108 augroup TestAutoCmdFns2
4109 autocmd BufAdd *.vim echo "bufadd-vim-2"
4110 autocmd BufRead *.a1b2c3 echo "bufadd-vim-2"
4111 augroup END
4112
4113 let l = autocmd_get()
4114 call assert_true(l->len() > 0)
4115
4116 " Test for getting all the autocmds in a group
4117 let expected = [
4118 \ #{cmd: 'echo "bufadd-vim"', group: 'TestAutoCmdFns',
4119 \ pattern: '*.vim', nested: v:false, once: v:false,
4120 \ event: 'BufAdd'},
4121 \ #{cmd: 'echo "bufadd-py"', group: 'TestAutoCmdFns',
4122 \ pattern: '*.py', nested: v:false, once: v:false,
4123 \ event: 'BufAdd'},
4124 \ #{cmd: 'echo "bufhidden"', group: 'TestAutoCmdFns',
4125 \ pattern: '*.vim', nested: v:false,
4126 \ once: v:false, event: 'BufHidden'}]
4127 call assert_equal(expected, autocmd_get(#{group: 'TestAutoCmdFns'}))
4128
4129 " Test for getting autocmds for all the patterns in a group
4130 call assert_equal(expected, autocmd_get(#{group: 'TestAutoCmdFns',
4131 \ event: '*'}))
4132
4133 " Test for getting autocmds for an event in a group
4134 let expected = [
4135 \ #{cmd: 'echo "bufadd-vim"', group: 'TestAutoCmdFns',
4136 \ pattern: '*.vim', nested: v:false, once: v:false,
4137 \ event: 'BufAdd'},
4138 \ #{cmd: 'echo "bufadd-py"', group: 'TestAutoCmdFns',
4139 \ pattern: '*.py', nested: v:false, once: v:false,
4140 \ event: 'BufAdd'}]
4141 call assert_equal(expected, autocmd_get(#{group: 'TestAutoCmdFns',
4142 \ event: 'BufAdd'}))
4143
4144 " Test for getting the autocmds for all the events in a group for particular
4145 " pattern
4146 call assert_equal([{'cmd': 'echo "bufadd-py"', 'group': 'TestAutoCmdFns',
4147 \ 'pattern': '*.py', 'nested': v:false, 'once': v:false,
4148 \ 'event': 'BufAdd'}],
4149 \ autocmd_get(#{group: 'TestAutoCmdFns', event: '*', pattern: '*.py'}))
4150
4151 " Test for getting the autocmds for an events in a group for particular
4152 " pattern
4153 let l = autocmd_get(#{group: 'TestAutoCmdFns', event: 'BufAdd',
4154 \ pattern: '*.vim'})
4155 call assert_equal([
4156 \ #{cmd: 'echo "bufadd-vim"', group: 'TestAutoCmdFns',
4157 \ pattern: '*.vim', nested: v:false, once: v:false,
4158 \ event: 'BufAdd'}], l)
4159
4160 " Test for getting the autocmds for a pattern in a group
4161 let l = autocmd_get(#{group: 'TestAutoCmdFns', pattern: '*.vim'})
4162 call assert_equal([
4163 \ #{cmd: 'echo "bufadd-vim"', group: 'TestAutoCmdFns',
4164 \ pattern: '*.vim', nested: v:false, once: v:false,
4165 \ event: 'BufAdd'},
4166 \ #{cmd: 'echo "bufhidden"', group: 'TestAutoCmdFns',
4167 \ pattern: '*.vim', nested: v:false,
4168 \ once: v:false, event: 'BufHidden'}], l)
4169
4170 " Test for getting the autocmds for a pattern in all the groups
4171 let l = autocmd_get(#{pattern: '*.a1b2c3'})
4172 call assert_equal([{'cmd': 'echo "bufadd-vim-2"', 'group': 'TestAutoCmdFns2',
4173 \ 'pattern': '*.a1b2c3', 'nested': v:false, 'once': v:false,
4174 \ 'event': 'BufRead'}], l)
4175
4176 " Test for getting autocmds for a pattern without any autocmds
4177 call assert_equal([], autocmd_get(#{group: 'TestAutoCmdFns',
4178 \ pattern: '*.abc'}))
4179 call assert_equal([], autocmd_get(#{group: 'TestAutoCmdFns',
4180 \ event: 'BufAdd', pattern: '*.abc'}))
4181 call assert_equal([], autocmd_get(#{group: 'TestAutoCmdFns',
4182 \ event: 'BufWipeout'}))
zeertzjq2d1d5c62024-06-09 16:44:33 +02004183
4184 " Test for getting autocmds after removing one inside an autocmd
4185 func CheckAutocmdGet()
4186 augroup TestAutoCmdFns
4187 autocmd! BufAdd *.vim
4188 augroup END
4189
4190 let expected = [
4191 \ #{cmd: 'echo "bufadd-py"', group: 'TestAutoCmdFns',
4192 \ pattern: '*.py', nested: v:false, once: v:false,
4193 \ event: 'BufAdd'},
4194 \ #{cmd: 'echo "bufhidden"', group: 'TestAutoCmdFns',
4195 \ pattern: '*.vim', nested: v:false,
4196 \ once: v:false, event: 'BufHidden'}]
4197
4198 call assert_equal(expected, autocmd_get(#{group: 'TestAutoCmdFns'}))
4199 call assert_equal([expected[0]],
4200 \ autocmd_get(#{group: 'TestAutoCmdFns', pattern: '*.py'}))
4201 call assert_equal([expected[1]],
4202 \ autocmd_get(#{group: 'TestAutoCmdFns', pattern: '*.vim'}))
4203 endfunc
4204
4205 autocmd User Xauget call CheckAutocmdGet()
4206 doautocmd User Xauget
4207 autocmd! User Xauget
4208
Yegappan Lakshmanan1755a912022-05-19 10:31:47 +01004209 call assert_fails("call autocmd_get(#{group: 'abc', event: 'BufAdd'})",
4210 \ 'E367:')
4211 let cmd = "echo autocmd_get(#{group: 'TestAutoCmdFns', event: 'abc'})"
4212 call assert_fails(cmd, 'E216:')
4213 call assert_fails("call autocmd_get(#{group: 'abc'})", 'E367:')
4214 call assert_fails("echo autocmd_get(#{event: 'abc'})", 'E216:')
4215
4216 augroup TestAutoCmdFns
4217 au!
4218 augroup END
4219 call assert_equal([], autocmd_get(#{group: 'TestAutoCmdFns'}))
4220
4221 " Test for nested and once autocmds
4222 augroup TestAutoCmdFns
4223 au!
4224 autocmd VimSuspend * ++nested echo "suspend"
4225 autocmd VimResume * ++once echo "resume"
4226 augroup END
4227
4228 let expected = [
Yegappan Lakshmanan1755a912022-05-19 10:31:47 +01004229 \ {'cmd': 'echo "resume"', 'group': 'TestAutoCmdFns', 'pattern': '*',
Luuk van Baalb7147f82025-02-08 18:52:39 +01004230 \ 'nested': v:false, 'once': v:true, 'event': 'VimResume'},
4231 \ {'cmd': 'echo "suspend"', 'group': 'TestAutoCmdFns', 'pattern': '*',
4232 \ 'nested': v:true, 'once': v:false, 'event': 'VimSuspend'}]
Yegappan Lakshmanan1755a912022-05-19 10:31:47 +01004233 call assert_equal(expected, autocmd_get(#{group: 'TestAutoCmdFns'}))
4234
4235 " Test for buffer-local autocmd
4236 augroup TestAutoCmdFns
4237 au!
4238 autocmd TextYankPost <buffer> echo "textyankpost"
4239 augroup END
4240
4241 let expected = [
4242 \ {'cmd': 'echo "textyankpost"', 'group': 'TestAutoCmdFns',
4243 \ 'pattern': '<buffer=' .. bufnr() .. '>', 'nested': v:false,
4244 \ 'once': v:false, 'bufnr': bufnr(), 'event': 'TextYankPost'}]
4245 call assert_equal(expected, autocmd_get(#{group: 'TestAutoCmdFns'}))
4246
4247 augroup TestAutoCmdFns
4248 au!
4249 augroup END
4250 augroup! TestAutoCmdFns
4251 augroup TestAutoCmdFns2
4252 au!
4253 augroup END
4254 augroup! TestAutoCmdFns2
4255
4256 call assert_fails("echo autocmd_get(#{group: []})", 'E730:')
4257 call assert_fails("echo autocmd_get(#{event: {}})", 'E731:')
4258 call assert_fails("echo autocmd_get([])", 'E1206:')
4259endfunc
4260
4261" Test for the autocmd_add() function
4262func Test_autocmd_add()
4263 " Define a single autocmd in a group
4264 call autocmd_add([#{group: 'TestAcSet', event: 'BufAdd', pattern: '*.sh',
4265 \ cmd: 'echo "bufadd"', once: v:true, nested: v:true}])
4266 call assert_equal([#{cmd: 'echo "bufadd"', group: 'TestAcSet',
4267 \ pattern: '*.sh', nested: v:true, once: v:true,
4268 \ event: 'BufAdd'}], autocmd_get(#{group: 'TestAcSet'}))
4269
4270 " Define two autocmds in the same group
4271 call autocmd_delete([#{group: 'TestAcSet'}])
4272 call autocmd_add([#{group: 'TestAcSet', event: 'BufAdd', pattern: '*.sh',
4273 \ cmd: 'echo "bufadd"'},
4274 \ #{group: 'TestAcSet', event: 'BufEnter', pattern: '*.sh',
4275 \ cmd: 'echo "bufenter"'}])
4276 call assert_equal([
4277 \ #{cmd: 'echo "bufadd"', group: 'TestAcSet', pattern: '*.sh',
4278 \ nested: v:false, once: v:false, event: 'BufAdd'},
4279 \ #{cmd: 'echo "bufenter"', group: 'TestAcSet', pattern: '*.sh',
4280 \ nested: v:false, once: v:false, event: 'BufEnter'}],
4281 \ autocmd_get(#{group: 'TestAcSet'}))
4282
4283 " Define a buffer-local autocmd
4284 call autocmd_delete([#{group: 'TestAcSet'}])
4285 call autocmd_add([#{group: 'TestAcSet', event: 'CursorHold',
4286 \ bufnr: bufnr(), cmd: 'echo "cursorhold"'}])
4287 call assert_equal([
4288 \ #{cmd: 'echo "cursorhold"', group: 'TestAcSet',
4289 \ pattern: '<buffer=' .. bufnr() .. '>', nested: v:false,
4290 \ once: v:false, bufnr: bufnr(), event: 'CursorHold'}],
4291 \ autocmd_get(#{group: 'TestAcSet'}))
4292
4293 " Use an invalid buffer number
4294 call autocmd_delete([#{group: 'TestAcSet'}])
4295 call autocmd_add([#{group: 'TestAcSet', event: 'BufEnter',
4296 \ bufnr: -1, cmd: 'echo "bufenter"'}])
4297 let l = [#{group: 'TestAcSet', event: 'BufAdd', bufnr: 9999,
4298 \ cmd: 'echo "bufadd"'}]
4299 call assert_fails("echo autocmd_add(l)", 'E680:')
Yegappan Lakshmanan00e977c2022-06-01 12:31:53 +01004300 let l = [#{group: 'TestAcSet', event: 'BufAdd', bufnr: 9999,
4301 \ pattern: '*.py', cmd: 'echo "bufadd"'}]
4302 call assert_fails("echo autocmd_add(l)", 'E680:')
4303 let l = [#{group: 'TestAcSet', event: 'BufAdd', bufnr: 9999,
4304 \ pattern: ['*.py', '*.c'], cmd: 'echo "bufadd"'}]
4305 call assert_fails("echo autocmd_add(l)", 'E680:')
Yegappan Lakshmanan1755a912022-05-19 10:31:47 +01004306 let l = [#{group: 'TestAcSet', event: 'BufRead', bufnr: [],
4307 \ cmd: 'echo "bufread"'}]
4308 call assert_fails("echo autocmd_add(l)", 'E745:')
4309 call assert_equal([], autocmd_get(#{group: 'TestAcSet'}))
4310
4311 " Add two commands to the same group, event and pattern
4312 call autocmd_delete([#{group: 'TestAcSet'}])
4313 call autocmd_add([#{group: 'TestAcSet', event: 'BufUnload',
4314 \ pattern: 'abc', cmd: 'echo "cmd1"'}])
4315 call autocmd_add([#{group: 'TestAcSet', event: 'BufUnload',
4316 \ pattern: 'abc', cmd: 'echo "cmd2"'}])
4317 call assert_equal([
4318 \ #{cmd: 'echo "cmd1"', group: 'TestAcSet', pattern: 'abc',
4319 \ nested: v:false, once: v:false, event: 'BufUnload'},
4320 \ #{cmd: 'echo "cmd2"', group: 'TestAcSet', pattern: 'abc',
4321 \ nested: v:false, once: v:false, event: 'BufUnload'}],
4322 \ autocmd_get(#{group: 'TestAcSet'}))
4323
4324 " When adding a new autocmd, if the autocmd 'group' is not specified, then
4325 " the current autocmd group should be used.
4326 call autocmd_delete([#{group: 'TestAcSet'}])
4327 augroup TestAcSet
4328 call autocmd_add([#{event: 'BufHidden', pattern: 'abc', cmd: 'echo "abc"'}])
4329 augroup END
4330 call assert_equal([
4331 \ #{cmd: 'echo "abc"', group: 'TestAcSet', pattern: 'abc',
4332 \ nested: v:false, once: v:false, event: 'BufHidden'}],
4333 \ autocmd_get(#{group: 'TestAcSet'}))
4334
Yegappan Lakshmanan971f6822022-05-24 11:40:11 +01004335 " Test for replacing a cmd for an event in a group
4336 call autocmd_delete([#{group: 'TestAcSet'}])
4337 call autocmd_add([#{replace: v:true, group: 'TestAcSet', event: 'BufEnter',
4338 \ pattern: '*.py', cmd: 'echo "bufenter"'}])
4339 call autocmd_add([#{replace: v:true, group: 'TestAcSet', event: 'BufEnter',
4340 \ pattern: '*.py', cmd: 'echo "bufenter"'}])
4341 call assert_equal([
4342 \ #{cmd: 'echo "bufenter"', group: 'TestAcSet', pattern: '*.py',
4343 \ nested: v:false, once: v:false, event: 'BufEnter'}],
4344 \ autocmd_get(#{group: 'TestAcSet'}))
4345
4346 " Test for adding a command for an unsupported autocmd event
Yegappan Lakshmanan1755a912022-05-19 10:31:47 +01004347 let l = [#{group: 'TestAcSet', event: 'abc', pattern: '*.sh',
4348 \ cmd: 'echo "bufadd"'}]
4349 call assert_fails('call autocmd_add(l)', 'E216:')
4350
Yegappan Lakshmanane0ff3a72022-05-27 18:05:33 +01004351 " Test for using a list of events and patterns
4352 call autocmd_delete([#{group: 'TestAcSet'}])
4353 let l = [#{group: 'TestAcSet', event: ['BufEnter', 'BufLeave'],
4354 \ pattern: ['*.py', '*.sh'], cmd: 'echo "bufcmds"'}]
4355 call autocmd_add(l)
4356 call assert_equal([
4357 \ #{cmd: 'echo "bufcmds"', group: 'TestAcSet', pattern: '*.py',
4358 \ nested: v:false, once: v:false, event: 'BufEnter'},
4359 \ #{cmd: 'echo "bufcmds"', group: 'TestAcSet', pattern: '*.sh',
4360 \ nested: v:false, once: v:false, event: 'BufEnter'},
4361 \ #{cmd: 'echo "bufcmds"', group: 'TestAcSet', pattern: '*.py',
4362 \ nested: v:false, once: v:false, event: 'BufLeave'},
4363 \ #{cmd: 'echo "bufcmds"', group: 'TestAcSet', pattern: '*.sh',
4364 \ nested: v:false, once: v:false, event: 'BufLeave'}],
4365 \ autocmd_get(#{group: 'TestAcSet'}))
4366
4367 " Test for invalid values for 'event' item
4368 call autocmd_delete([#{group: 'TestAcSet'}])
4369 let l = [#{group: 'TestAcSet', event: test_null_string(),
4370 \ pattern: "*.py", cmd: 'echo "bufcmds"'}]
4371 call assert_fails('call autocmd_add(l)', 'E928:')
4372 let l = [#{group: 'TestAcSet', event: test_null_list(),
4373 \ pattern: "*.py", cmd: 'echo "bufcmds"'}]
4374 call assert_fails('call autocmd_add(l)', 'E714:')
4375 let l = [#{group: 'TestAcSet', event: {},
4376 \ pattern: "*.py", cmd: 'echo "bufcmds"'}]
4377 call assert_fails('call autocmd_add(l)', 'E777:')
4378 let l = [#{group: 'TestAcSet', event: [{}],
4379 \ pattern: "*.py", cmd: 'echo "bufcmds"'}]
4380 call assert_fails('call autocmd_add(l)', 'E928:')
4381 let l = [#{group: 'TestAcSet', event: [test_null_string()],
4382 \ pattern: "*.py", cmd: 'echo "bufcmds"'}]
4383 call assert_fails('call autocmd_add(l)', 'E928:')
4384 let l = [#{group: 'TestAcSet', event: 'BufEnter,BufLeave',
4385 \ pattern: '*.py', cmd: 'echo "bufcmds"'}]
4386 call assert_fails('call autocmd_add(l)', 'E216:')
4387 let l = [#{group: 'TestAcSet', event: [],
4388 \ pattern: "*.py", cmd: 'echo "bufcmds"'}]
4389 call autocmd_add(l)
4390 let l = [#{group: 'TestAcSet', event: [""],
4391 \ pattern: "*.py", cmd: 'echo "bufcmds"'}]
4392 call assert_fails('call autocmd_add(l)', 'E216:')
4393 let l = [#{group: 'TestAcSet', event: "",
4394 \ pattern: "*.py", cmd: 'echo "bufcmds"'}]
4395 call autocmd_add(l)
4396 call assert_equal([], autocmd_get(#{group: 'TestAcSet'}))
4397
4398 " Test for invalid values for 'pattern' item
4399 let l = [#{group: 'TestAcSet', event: "BufEnter",
4400 \ pattern: test_null_string(), cmd: 'echo "bufcmds"'}]
Yegappan Lakshmanan00e977c2022-06-01 12:31:53 +01004401 call assert_fails('call autocmd_add(l)', 'E928:')
Yegappan Lakshmanane0ff3a72022-05-27 18:05:33 +01004402 let l = [#{group: 'TestAcSet', event: "BufEnter",
4403 \ pattern: test_null_list(), cmd: 'echo "bufcmds"'}]
4404 call assert_fails('call autocmd_add(l)', 'E714:')
4405 let l = [#{group: 'TestAcSet', event: "BufEnter",
4406 \ pattern: {}, cmd: 'echo "bufcmds"'}]
4407 call assert_fails('call autocmd_add(l)', 'E777:')
4408 let l = [#{group: 'TestAcSet', event: "BufEnter",
4409 \ pattern: [{}], cmd: 'echo "bufcmds"'}]
4410 call assert_fails('call autocmd_add(l)', 'E928:')
4411 let l = [#{group: 'TestAcSet', event: "BufEnter",
4412 \ pattern: [test_null_string()], cmd: 'echo "bufcmds"'}]
4413 call assert_fails('call autocmd_add(l)', 'E928:')
4414 let l = [#{group: 'TestAcSet', event: "BufEnter",
4415 \ pattern: [], cmd: 'echo "bufcmds"'}]
4416 call autocmd_add(l)
4417 let l = [#{group: 'TestAcSet', event: "BufEnter",
4418 \ pattern: [""], cmd: 'echo "bufcmds"'}]
4419 call autocmd_add(l)
4420 let l = [#{group: 'TestAcSet', event: "BufEnter",
4421 \ pattern: "", cmd: 'echo "bufcmds"'}]
4422 call autocmd_add(l)
4423 call assert_equal([], autocmd_get(#{group: 'TestAcSet'}))
4424
4425 let l = [#{group: 'TestAcSet', event: 'BufEnter,abc,BufLeave',
4426 \ pattern: '*.py', cmd: 'echo "bufcmds"'}]
4427 call assert_fails('call autocmd_add(l)', 'E216:')
4428
Yegappan Lakshmanan1755a912022-05-19 10:31:47 +01004429 call assert_fails("call autocmd_add({})", 'E1211:')
4430 call assert_equal(v:false, autocmd_add(test_null_list()))
4431 call assert_true(autocmd_add([[]]))
4432 call assert_true(autocmd_add([test_null_dict()]))
4433
4434 augroup TestAcSet
4435 au!
4436 augroup END
4437
4438 call autocmd_add([#{group: 'TestAcSet'}])
4439 call autocmd_add([#{group: 'TestAcSet', event: 'BufAdd'}])
4440 call autocmd_add([#{group: 'TestAcSet', pat: '*.sh'}])
4441 call autocmd_add([#{group: 'TestAcSet', cmd: 'echo "a"'}])
4442 call autocmd_add([#{group: 'TestAcSet', event: 'BufAdd', pat: '*.sh'}])
4443 call autocmd_add([#{group: 'TestAcSet', event: 'BufAdd', cmd: 'echo "a"'}])
4444 call autocmd_add([#{group: 'TestAcSet', pat: '*.sh', cmd: 'echo "a"'}])
4445 call assert_equal([], autocmd_get(#{group: 'TestAcSet'}))
4446
4447 augroup! TestAcSet
4448endfunc
4449
4450" Test for deleting autocmd events and groups
4451func Test_autocmd_delete()
4452 " Delete an event in an autocmd group
4453 augroup TestAcSet
4454 au!
4455 au BufAdd *.sh echo "bufadd"
4456 au BufEnter *.sh echo "bufenter"
4457 augroup END
4458 call autocmd_delete([#{group: 'TestAcSet', event: 'BufAdd'}])
4459 call assert_equal([#{cmd: 'echo "bufenter"', group: 'TestAcSet',
4460 \ pattern: '*.sh', nested: v:false, once: v:false,
4461 \ event: 'BufEnter'}], autocmd_get(#{group: 'TestAcSet'}))
4462
4463 " Delete all the events in an autocmd group
4464 augroup TestAcSet
4465 au BufAdd *.sh echo "bufadd"
4466 augroup END
4467 call autocmd_delete([#{group: 'TestAcSet', event: '*'}])
4468 call assert_equal([], autocmd_get(#{group: 'TestAcSet'}))
4469
4470 " Delete a non-existing autocmd group
4471 call assert_fails("call autocmd_delete([#{group: 'abc'}])", 'E367:')
4472 " Delete a non-existing autocmd event
4473 let l = [#{group: 'TestAcSet', event: 'abc'}]
4474 call assert_fails("call autocmd_delete(l)", 'E216:')
4475 " Delete a non-existing autocmd pattern
4476 let l = [#{group: 'TestAcSet', event: 'BufAdd', pat: 'abc'}]
4477 call assert_true(autocmd_delete(l))
Yegappan Lakshmanan00e977c2022-06-01 12:31:53 +01004478 " Delete an autocmd for a non-existing buffer
4479 let l = [#{event: '*', bufnr: 9999, cmd: 'echo "x"'}]
4480 call assert_fails('call autocmd_delete(l)', 'E680:')
Yegappan Lakshmanan1755a912022-05-19 10:31:47 +01004481
4482 " Delete an autocmd group
4483 augroup TestAcSet
4484 au!
4485 au BufAdd *.sh echo "bufadd"
4486 au BufEnter *.sh echo "bufenter"
4487 augroup END
4488 call autocmd_delete([#{group: 'TestAcSet'}])
4489 call assert_fails("call autocmd_get(#{group: 'TestAcSet'})", 'E367:')
4490
4491 call assert_true(autocmd_delete([[]]))
4492 call assert_true(autocmd_delete([test_null_dict()]))
4493endfunc
4494
Bram Moolenaar8f3c3c62022-10-18 17:05:54 +01004495func Test_autocmd_split_dummy()
4496 " Autocommand trying to split a window containing a dummy buffer.
Bram Moolenaar94722c52023-01-28 19:19:03 +00004497 auto BufReadPre * exe "sbuf " .. expand("<abuf>")
Bram Moolenaar8f3c3c62022-10-18 17:05:54 +01004498 " Avoid the "W11" prompt
4499 au FileChangedShell * let v:fcs_choice = 'reload'
4500 func Xautocmd_changelist()
4501 cal writefile(['Xtestfile2:4:4'], 'Xerr')
4502 edit Xerr
4503 lex 'Xtestfile2:4:4'
4504 endfunc
4505 call Xautocmd_changelist()
Bram Moolenaar53c5c9f2022-10-18 17:25:03 +01004506 " Should get E86, but it doesn't always happen (timing?)
4507 silent! call Xautocmd_changelist()
Bram Moolenaar8f3c3c62022-10-18 17:05:54 +01004508
4509 au! BufReadPre
4510 au! FileChangedShell
4511 delfunc Xautocmd_changelist
4512 bwipe! Xerr
4513 call delete('Xerr')
4514endfunc
4515
Bram Moolenaare76062c2022-11-28 18:51:43 +00004516" This was crashing because there was only one window to execute autocommands
4517" in.
4518func Test_autocmd_nested_setbufvar()
4519 CheckFeature python3
4520
4521 set hidden
4522 edit Xaaa
4523 edit Xbbb
4524 call setline(1, 'bar')
4525 enew
4526 au BufWriteCmd Xbbb ++nested call setbufvar('Xaaa', '&ft', 'foo') | bw! Xaaa
4527 au FileType foo call py3eval('vim.current.buffer.options["cindent"]')
4528 wall
4529
4530 au! BufWriteCmd
4531 au! FileType foo
4532 set nohidden
4533 call delete('Xaaa')
4534 call delete('Xbbb')
4535 %bwipe!
4536endfunc
4537
Christian Brabandt9aee8ec2022-12-16 16:41:23 +00004538func SetupVimTest_shm()
4539 let g:bwe = []
4540 let g:brp = []
4541 set shortmess+=F
zeertzjq657b31f2023-04-15 21:28:02 +01004542 messages clear
Christian Brabandt9aee8ec2022-12-16 16:41:23 +00004543
4544 let dirname='XVimTestSHM'
4545 call mkdir(dirname, 'R')
4546 call writefile(['test'], dirname .. '/1')
4547 call writefile(['test'], dirname .. '/2')
4548 call writefile(['test'], dirname .. '/3')
4549
4550 augroup test
4551 autocmd!
4552 autocmd BufWinEnter * call add(g:bwe, $'BufWinEnter: {expand('<amatch>')}')
4553 autocmd BufReadPost * call add(g:brp, $'BufReadPost: {expand('<amatch>')}')
4554 augroup END
4555
4556 call setqflist([
4557 \ {'filename': dirname .. '/1', 'lnum': 1, 'col': 1, 'text': 'test', 'vcol': 0},
4558 \ {'filename': dirname .. '/2', 'lnum': 1, 'col': 1, 'text': 'test', 'vcol': 0},
4559 \ {'filename': dirname .. '/3', 'lnum': 1, 'col': 1, 'text': 'test', 'vcol': 0}
4560 \ ])
4561 cdo! substitute/test/TEST
4562
4563 " clean up
4564 noa enew!
4565 set shortmess&vim
4566 augroup test
4567 autocmd!
4568 augroup END
4569 augroup! test
4570endfunc
4571
4572func Test_autocmd_shortmess()
4573 CheckNotMSWindows
4574
4575 call SetupVimTest_shm()
4576 let output = execute(':mess')->split('\n')
4577
4578 let info = copy(output)->filter({idx, val -> val =~# '\d of 3'} )
4579 let bytes = copy(output)->filter({idx, val -> val =~# 'bytes'} )
4580
4581 " We test the following here:
4582 " BufReadPost should have been triggered 3 times, once per file
4583 " BufWinEnter should have been triggered 3 times, once per file
4584 " FileInfoMessage should have been shown 3 times, regardless of shm option
4585 " "(x of 3)" message from :cnext has been shown 3 times
4586
4587 call assert_equal(3, g:brp->len())
4588 call assert_equal(3, g:bwe->len())
4589 call assert_equal(3, info->len())
4590 call assert_equal(3, bytes->len())
4591
4592 delfunc SetupVimTest_shm
4593endfunc
Bram Moolenaare76062c2022-11-28 18:51:43 +00004594
Christian Brabandtf0d3d4a2024-02-15 20:15:04 +01004595func Test_autocmd_invalidates_undo_on_textchanged()
4596 CheckRunVimInTerminal
4597 let script =<< trim END
4598 set hidden
4599 " create quickfix list (at least 2 lines to move line)
4600 vimgrep /u/j %
4601
4602 " enter quickfix window
4603 cwindow
4604
4605 " set modifiable
4606 setlocal modifiable
4607
4608 " set autocmd to clear quickfix list
4609
4610 autocmd! TextChanged <buffer> call setqflist([])
4611 " move line
4612 move+1
4613 END
4614 call writefile(script, 'XTest_autocmd_invalidates_undo_on_textchanged', 'D')
4615 let buf = RunVimInTerminal('XTest_autocmd_invalidates_undo_on_textchanged', {'rows': 20})
4616 call term_sendkeys(buf, ":so %\<cr>")
4617 call term_sendkeys(buf, "G")
4618 call WaitForAssert({-> assert_match('^XTest_autocmd_invalidates_undo_on_textchanged\s*$', term_getline(buf, 20))}, 1000)
4619
4620 call StopVimInTerminal(buf)
4621endfunc
4622
Christian Brabandt55f8bba2024-02-28 23:32:00 +01004623func Test_autocmd_creates_new_buffer_on_bufleave()
4624 e a.txt
4625 e b.txt
4626 setlocal bufhidden=wipe
4627 autocmd BufLeave <buffer> diffsplit c.txt
4628 bn
4629 call assert_equal(1, winnr('$'))
4630 call assert_equal('a.txt', bufname('%'))
4631 bw a.txt
4632 bw c.txt
4633endfunc
4634
Colin Kennedye5f22802024-03-26 18:20:16 +01004635" Ensure `expected` was just recently written as a Vim session
4636func s:assert_session_path(expected)
4637 call assert_equal(a:expected, v:this_session)
4638endfunc
4639
4640" Check for `expected` after a session is written to-disk.
4641func s:watch_for_session_path(expected)
4642 execute 'autocmd SessionWritePost * ++once execute "call s:assert_session_path(\"'
4643 \ . a:expected
4644 \ . '\")"'
4645endfunc
4646
4647" Ensure v:this_session gets the full session path, if explicitly stated
4648func Test_explicit_session_absolute_path()
4649 %bwipeout!
4650
4651 let directory = getcwd()
4652
4653 let v:this_session = ""
4654 let name = "some_file.vim"
4655 let expected = fnamemodify(name, ":p")
4656 call s:watch_for_session_path(expected)
4657 execute "mksession! " .. expected
4658
4659 call delete(expected)
4660endfunc
4661
4662" Ensure v:this_session gets the full session path, if explicitly stated
4663func Test_explicit_session_relative_path()
4664 %bwipeout!
4665
4666 let directory = getcwd()
4667
4668 let v:this_session = ""
4669 let name = "some_file.vim"
4670 let expected = fnamemodify(name, ":p")
4671 call s:watch_for_session_path(expected)
4672 execute "mksession! " .. name
4673
4674 call delete(expected)
4675endfunc
4676
4677" Ensure v:this_session gets the full session path, if not specified
4678func Test_implicit_session()
4679 %bwipeout!
4680
4681 let directory = getcwd()
4682
4683 let v:this_session = ""
4684 let expected = fnamemodify("Session.vim", ":p")
4685 call s:watch_for_session_path(expected)
4686 mksession!
4687
4688 call delete(expected)
4689endfunc
4690
Christian Brabandt86032702024-03-31 18:38:09 +02004691" Test TextChangedI and TextChanged
zeertzjqc4226622024-04-03 22:38:07 +02004692func Test_Changed_ChangedI()
zeertzjq8eb75232024-04-01 14:46:20 +02004693 " Run this test in a terminal because it requires running the main loop.
zeertzjqc4226622024-04-03 22:38:07 +02004694 " Don't use CheckRunVimInTerminal as that will skip the test on Windows.
4695 CheckFeature terminal
4696 CheckNotGui
4697 " Starting a terminal to run Vim is always considered flaky.
4698 let g:test_is_flaky = 1
4699
Christian Brabandt86032702024-03-31 18:38:09 +02004700 call writefile(['one', 'two', 'three'], 'XTextChangedI2', 'D')
4701 let before =<< trim END
zeertzjqc4226622024-04-03 22:38:07 +02004702 set ttimeout ttimeoutlen=10
zeertzjq8eb75232024-04-01 14:46:20 +02004703 let [g:autocmd_n, g:autocmd_i] = ['','']
4704
4705 func TextChangedAutocmd(char)
4706 let g:autocmd_{tolower(a:char)} = a:char .. b:changedtick
zeertzjqc4226622024-04-03 22:38:07 +02004707 call writefile([$'{g:autocmd_n},{g:autocmd_i}'], 'XTextChangedI3')
zeertzjq8eb75232024-04-01 14:46:20 +02004708 endfunc
4709
4710 au TextChanged <buffer> :call TextChangedAutocmd('N')
4711 au TextChangedI <buffer> :call TextChangedAutocmd('I')
4712
Christian Brabandt86032702024-03-31 18:38:09 +02004713 nnoremap <CR> o<Esc>
zeertzjq4a653912024-04-04 21:33:36 +02004714 autocmd SafeState * ++once call writefile([''], 'XTextChangedI3')
Christian Brabandt86032702024-03-31 18:38:09 +02004715 END
4716
4717 call writefile(before, 'Xinit', 'D')
zeertzjqc4226622024-04-03 22:38:07 +02004718 let buf = term_start(
4719 \ GetVimCommandCleanTerm() .. '-n -S Xinit XTextChangedI2',
4720 \ {'term_rows': 10})
4721 call assert_equal('running', term_getstatus(buf))
zeertzjq8eb75232024-04-01 14:46:20 +02004722 call WaitForAssert({-> assert_true(filereadable('XTextChangedI3'))})
zeertzjqe9ff79a2024-04-05 20:07:39 +02004723 defer delete('XTextChangedI3')
zeertzjq4a653912024-04-04 21:33:36 +02004724 call WaitForAssert({-> assert_equal([''], readfile('XTextChangedI3'))})
Christian Brabandt86032702024-03-31 18:38:09 +02004725
zeertzjqc4226622024-04-03 22:38:07 +02004726 " TextChanged should trigger if a mapping enters and leaves Insert mode.
4727 call term_sendkeys(buf, "\<CR>")
zeertzjqe9ff79a2024-04-05 20:07:39 +02004728 call WaitForAssert({-> assert_equal('N4,', readfile('XTextChangedI3')->join("\n"))})
zeertzjqc4226622024-04-03 22:38:07 +02004729
4730 call term_sendkeys(buf, "i")
4731 call WaitForAssert({-> assert_match('^-- INSERT --', term_getline(buf, 10))})
zeertzjqe9ff79a2024-04-05 20:07:39 +02004732 call WaitForAssert({-> assert_equal('N4,', readfile('XTextChangedI3')->join("\n"))})
zeertzjqc4226622024-04-03 22:38:07 +02004733 " TextChangedI should trigger if change is done in Insert mode.
4734 call term_sendkeys(buf, "f")
zeertzjqe9ff79a2024-04-05 20:07:39 +02004735 call WaitForAssert({-> assert_equal('N4,I5', readfile('XTextChangedI3')->join("\n"))})
zeertzjqc4226622024-04-03 22:38:07 +02004736 call term_sendkeys(buf, "o")
zeertzjqe9ff79a2024-04-05 20:07:39 +02004737 call WaitForAssert({-> assert_equal('N4,I6', readfile('XTextChangedI3')->join("\n"))})
zeertzjqc4226622024-04-03 22:38:07 +02004738 call term_sendkeys(buf, "o")
zeertzjqe9ff79a2024-04-05 20:07:39 +02004739 call WaitForAssert({-> assert_equal('N4,I7', readfile('XTextChangedI3')->join("\n"))})
zeertzjqc4226622024-04-03 22:38:07 +02004740 " TextChanged shouldn't trigger when leaving Insert mode and TextChangedI
4741 " has been triggered.
4742 call term_sendkeys(buf, "\<Esc>")
4743 call WaitForAssert({-> assert_notmatch('^-- INSERT --', term_getline(buf, 10))})
zeertzjqe9ff79a2024-04-05 20:07:39 +02004744 call WaitForAssert({-> assert_equal('N4,I7', readfile('XTextChangedI3')->join("\n"))})
zeertzjqc4226622024-04-03 22:38:07 +02004745
4746 " TextChanged should trigger if change is done in Normal mode.
4747 call term_sendkeys(buf, "yyp")
zeertzjqe9ff79a2024-04-05 20:07:39 +02004748 call WaitForAssert({-> assert_equal('N8,I7', readfile('XTextChangedI3')->join("\n"))})
zeertzjqc4226622024-04-03 22:38:07 +02004749
4750 " TextChangedI shouldn't trigger if change isn't done in Insert mode.
4751 call term_sendkeys(buf, "i")
4752 call WaitForAssert({-> assert_match('^-- INSERT --', term_getline(buf, 10))})
zeertzjqe9ff79a2024-04-05 20:07:39 +02004753 call WaitForAssert({-> assert_equal('N8,I7', readfile('XTextChangedI3')->join("\n"))})
zeertzjqc4226622024-04-03 22:38:07 +02004754 call term_sendkeys(buf, "\<Esc>")
4755 call WaitForAssert({-> assert_notmatch('^-- INSERT --', term_getline(buf, 10))})
zeertzjqe9ff79a2024-04-05 20:07:39 +02004756 call WaitForAssert({-> assert_equal('N8,I7', readfile('XTextChangedI3')->join("\n"))})
zeertzjqc4226622024-04-03 22:38:07 +02004757
4758 " TextChangedI should trigger if change is a mix of Normal and Insert modes.
4759 func! s:validate_mixed_textchangedi(buf, keys)
4760 let buf = a:buf
4761 call term_sendkeys(buf, "ifoo")
4762 call WaitForAssert({-> assert_match('^-- INSERT --', term_getline(buf, 10))})
4763 call term_sendkeys(buf, "\<Esc>")
4764 call WaitForAssert({-> assert_notmatch('^-- INSERT --', term_getline(buf, 10))})
4765 call term_sendkeys(buf, ":let [g:autocmd_n, g:autocmd_i] = ['', '']\<CR>")
zeertzjqe9ff79a2024-04-05 20:07:39 +02004766 call writefile([], 'XTextChangedI3')
zeertzjqc4226622024-04-03 22:38:07 +02004767 call term_sendkeys(buf, a:keys)
4768 call WaitForAssert({-> assert_match('^-- INSERT --', term_getline(buf, 10))})
zeertzjqe9ff79a2024-04-05 20:07:39 +02004769 call WaitForAssert({-> assert_match('^,I\d\+', readfile('XTextChangedI3')->join("\n"))})
zeertzjqc4226622024-04-03 22:38:07 +02004770 call term_sendkeys(buf, "\<Esc>")
4771 call WaitForAssert({-> assert_notmatch('^-- INSERT --', term_getline(buf, 10))})
zeertzjqe9ff79a2024-04-05 20:07:39 +02004772 call WaitForAssert({-> assert_match('^,I\d\+', readfile('XTextChangedI3')->join("\n"))})
zeertzjqc4226622024-04-03 22:38:07 +02004773 endfunc
4774
4775 call s:validate_mixed_textchangedi(buf, "o")
4776 call s:validate_mixed_textchangedi(buf, "O")
4777 call s:validate_mixed_textchangedi(buf, "ciw")
4778 call s:validate_mixed_textchangedi(buf, "cc")
4779 call s:validate_mixed_textchangedi(buf, "C")
4780 call s:validate_mixed_textchangedi(buf, "s")
4781 call s:validate_mixed_textchangedi(buf, "S")
4782
4783 " clean up
4784 bwipe!
Christian Brabandt86032702024-03-31 18:38:09 +02004785endfunc
4786
zeertzjq5bf6c212024-03-31 18:41:27 +02004787" Test that filetype detection still works when SwapExists autocommand sets
4788" filetype in another buffer.
4789func Test_SwapExists_set_other_buf_filetype()
4790 let lines =<< trim END
4791 set nocompatible directory=.
4792 filetype on
4793
4794 let g:buf = bufnr()
4795 new
4796
4797 func SwapExists()
4798 let v:swapchoice = 'o'
4799 call setbufvar(g:buf, '&filetype', 'text')
4800 endfunc
4801
4802 func SafeState()
4803 edit <script>
4804 redir! > XftSwapExists.out
4805 set readonly? filetype?
4806 redir END
4807 qall!
4808 endfunc
4809
4810 autocmd SwapExists * ++nested call SwapExists()
4811 autocmd SafeState * ++nested ++once call SafeState()
4812 END
4813 call writefile(lines, 'XftSwapExists.vim', 'D')
4814
4815 new XftSwapExists.vim
4816 if RunVim('', '', ' -S XftSwapExists.vim')
4817 call assert_equal(
4818 \ ['', ' readonly', ' filetype=vim'],
4819 \ readfile('XftSwapExists.out'))
4820 call delete('XftSwapExists.out')
4821 endif
4822
4823 bwipe!
4824endfunc
4825
4826" Test that file is not marked as modified when SwapExists autocommand sets
4827" 'modified' in another buffer.
4828func Test_SwapExists_set_other_buf_modified()
4829 let lines =<< trim END
4830 set nocompatible directory=.
4831
4832 let g:buf = bufnr()
4833 new
4834
4835 func SwapExists()
4836 let v:swapchoice = 'o'
4837 call setbufvar(g:buf, '&modified', 1)
4838 endfunc
4839
4840 func SafeState()
4841 edit <script>
4842 redir! > XmodSwapExists.out
4843 set readonly? modified?
4844 redir END
4845 qall!
4846 endfunc
4847
4848 autocmd SwapExists * ++nested call SwapExists()
4849 autocmd SafeState * ++nested ++once call SafeState()
4850 END
4851 call writefile(lines, 'XmodSwapExists.vim', 'D')
4852
4853 new XmodSwapExists.vim
4854 if RunVim('', '', ' -S XmodSwapExists.vim')
4855 call assert_equal(
4856 \ ['', ' readonly', 'nomodified'],
4857 \ readfile('XmodSwapExists.out'))
4858 call delete('XmodSwapExists.out')
4859 endif
4860
4861 bwipe!
4862endfunc
4863
Jaehwang Jungeb80b832024-04-26 18:48:48 +02004864func Test_BufEnter_botline()
4865 set hidden
4866 call writefile(range(10), 'Xxx1', 'D')
4867 call writefile(range(20), 'Xxx2', 'D')
4868 edit Xxx1
4869 edit Xxx2
4870 au BufEnter Xxx1 call assert_true(line('w$') > 1)
4871 edit Xxx1
zeertzjq340643e2024-04-27 11:33:24 +02004872
4873 bwipe! Xxx1
4874 bwipe! Xxx2
Jaehwang Jungeb80b832024-04-26 18:48:48 +02004875 au! BufEnter Xxx1
4876 set hidden&vim
4877endfunc
4878
Shougo Matsushita83678842024-07-11 22:05:12 +02004879func Test_KeyInputPre()
4880 " Consume previous keys
4881 call feedkeys('', 'ntx')
4882
4883 " KeyInputPre can record input keys.
4884 let s:keys = []
4885 au KeyInputPre n call add(s:keys, v:char)
4886
4887 call feedkeys('jkjkjjj', 'ntx')
4888 call assert_equal(
4889 \ ['j', 'k', 'j', 'k', 'j', 'j', 'j'],
4890 \ s:keys)
4891
4892 unlet s:keys
4893 au! KeyInputPre
4894
4895 " KeyInputPre can handle multibyte.
4896 let s:keys = []
4897 au KeyInputPre * call add(s:keys, v:char)
4898 edit Xxx1
4899
4900 call feedkeys("iあ\<ESC>", 'ntx')
4901 call assert_equal(['i', "あ", "\<ESC>"], s:keys)
4902
4903 bwipe! Xxx1
4904 unlet s:keys
4905 au! KeyInputPre
4906
4907 " KeyInputPre can change input keys.
4908 au KeyInputPre i if v:char ==# 'a' | let v:char = 'b' | endif
4909 edit Xxx1
4910
4911 call feedkeys("iaabb\<ESC>", 'ntx')
4912 call assert_equal(getline('.'), 'bbbb')
4913
4914 bwipe! Xxx1
4915 au! KeyInputPre
4916
4917 " KeyInputPre returns multiple characters.
4918 au KeyInputPre i if v:char ==# 'a' | let v:char = 'cccc' | endif
4919 edit Xxx1
4920
4921 call feedkeys("iaabb\<ESC>", 'ntx')
4922 call assert_equal(getline('.'), 'ccbb')
4923
4924 bwipe! Xxx1
4925 au! KeyInputPre
4926
4927 " KeyInputPre can use special keys.
4928 au KeyInputPre i if v:char ==# 'a' | let v:char = "\<Ignore>" | endif
4929 edit Xxx1
4930
4931 call feedkeys("iaabb\<ESC>", 'ntx')
4932 call assert_equal(getline('.'), 'bb')
4933
4934 bwipe! Xxx1
4935 au! KeyInputPre
4936
4937 " Test for v:event.typed
4938 au KeyInputPre n call assert_true(v:event.typed)
4939 call feedkeys('j', 'ntx')
4940
4941 au! KeyInputPre
4942
4943 au KeyInputPre n call assert_false(v:event.typed)
4944 call feedkeys('j', 'nx')
4945
4946 au! KeyInputPre
Shougo Matsushitafcc1b572024-07-17 20:25:22 +02004947
4948 " Test for v:event.typedchar
4949 nnoremap j k
4950 au KeyInputPre n
4951 \ call assert_equal(v:event.typedchar, 'j')
4952 \ | call assert_equal(v:char, 'k')
4953 call feedkeys('j', 'tx')
4954
4955 au! KeyInputPre
Shougo Matsushita83678842024-07-11 22:05:12 +02004956endfunc
4957
Christian Brabandtfb3f9692024-08-11 20:09:17 +02004958" those commands caused null pointer access, see #15464
4959func Test_WinNewPre_crash()
4960 defer CleanUpTestAuGroup()
4961 let _cmdheight=&cmdheight
4962 augroup testing
4963 au!
4964 autocmd WinNewPre * redraw
4965 augroup END
4966 tabnew
4967 tabclose
4968 augroup testing
4969 au!
4970 autocmd WinNewPre * wincmd t
4971 augroup END
4972 tabnew
4973 tabclose
4974 augroup testing
4975 au!
4976 autocmd WinNewPre * wincmd b
4977 augroup END
4978 tabnew
4979 tabclose
4980 augroup testing
4981 au!
4982 autocmd WinNewPre * set cmdheight+=1
4983 augroup END
4984 tabnew
4985 tabclose
4986 let &cmdheight=_cmdheight
4987endfunc
4988
Christian Brabandt84e31752024-09-02 09:59:18 +02004989" The specifics of the turkish locale may
4990" cause that Vim will not treat the GuiEnter autocommand
4991" as case insensitive and instead issues an error
4992func Test_GuiEnter_Turkish_locale()
4993 try
4994 let lng = v:lang
4995 lang tr_TR.UTF-8
4996 let result = execute(':au GuiEnter')
Ken Takataeccc9272024-09-03 23:01:55 +02004997 call assert_equal(gettext("\n--- Autocommands ---"), result)
Christian Brabandt84e31752024-09-02 09:59:18 +02004998 let result = execute(':au GUIENTER')
Ken Takataeccc9272024-09-03 23:01:55 +02004999 call assert_equal(gettext("\n--- Autocommands ---"), result)
Christian Brabandt84e31752024-09-02 09:59:18 +02005000 let result = execute(':au guienter')
Ken Takataeccc9272024-09-03 23:01:55 +02005001 call assert_equal(gettext("\n--- Autocommands ---"), result)
Christian Brabandt84e31752024-09-02 09:59:18 +02005002 exe ":lang" lng
5003 catch /E197:/
5004 " can't use Turkish locale
5005 throw 'Skipped: Turkish locale not available'
5006 endtry
5007endfunc
Christian Brabandtfb3f9692024-08-11 20:09:17 +02005008
Christian Brabandt51b62382024-10-06 17:31:10 +02005009" This was using freed memory
5010func Test_autocmd_BufWinLeave_with_vsp()
5011 new
5012 let fname = 'XXXBufWinLeaveUAF.txt'
5013 let dummy = 'XXXDummy.txt'
5014 call writefile([], fname)
5015 call writefile([], dummy)
5016 defer delete(fname)
5017 defer delete(dummy)
5018 exe "e " fname
5019 vsp
5020 augroup testing
5021 exe "au BufWinLeave " .. fname .. " :e " dummy .. "| vsp " .. fname
5022 augroup END
5023 bw
5024 call CleanUpTestAuGroup()
5025 exe "bw! " .. dummy
5026endfunc
5027
Luuk van Baale15cbc12025-01-04 17:18:08 +01005028func Test_OptionSet_cmdheight()
5029 set mouse=a laststatus=2
5030 au OptionSet cmdheight :let &l:ch = v:option_new
5031
5032 resize -1
5033 call assert_equal(2, &l:ch)
5034 resize +1
5035 call assert_equal(1, &l:ch)
5036
5037 call test_setmouse(&lines - 1, 1)
5038 call feedkeys("\<LeftMouse>", 'xt')
5039 call test_setmouse(&lines - 2, 1)
5040 call feedkeys("\<LeftDrag>", 'xt')
5041 call assert_equal(2, &l:ch)
5042
5043 tabnew | resize +1
5044 call assert_equal(1, &l:ch)
5045 tabfirst
5046 call assert_equal(2, &l:ch)
5047
5048 tabonly
5049 set cmdheight& mouse& laststatus&
5050endfunc
5051
Luuk van Baalb7147f82025-02-08 18:52:39 +01005052func Test_eventignorewin()
5053 defer CleanUpTestAuGroup()
5054 augroup testing
5055 au WinEnter * :call add(g:evs, ["WinEnter", expand("<afile>")])
5056 au WinLeave * :call add(g:evs, ["WinLeave", expand("<afile>")])
5057 au BufWinEnter * :call add(g:evs, ["BufWinEnter", expand("<afile>")])
5058 augroup END
5059
5060 let g:evs = []
5061 set eventignorewin=WinLeave,WinEnter
5062 split foo
5063 call assert_equal([['BufWinEnter', 'foo']], g:evs)
5064 set eventignorewin=all
5065 edit bar
5066 call assert_equal([['BufWinEnter', 'foo']], g:evs)
5067 set eventignorewin=
5068 wincmd w
5069 call assert_equal([['BufWinEnter', 'foo'], ['WinLeave', 'bar']], g:evs)
5070
5071 only!
5072 %bwipe!
5073 set eventignorewin&
5074 unlet g:evs
5075endfunc
5076
5077func Test_WinScrolled_Resized_eiw()
5078 CheckRunVimInTerminal
5079
5080 let lines =<< trim END
5081 call setline(1, ['foo']->repeat(32))
5082 set eventignorewin=WinScrolled,WinResized
5083 split
5084 let [g:afile,g:resized,g:scrolled] = ['none',0,0]
5085 au WinScrolled * let [g:afile,g:scrolled] = [expand('<afile>'),g:scrolled+1]
5086 au WinResized * let [g:afile,g:resized] = [expand('<afile>'),g:resized+1]
5087 END
Christian Brabandtbfc77192025-02-11 20:03:10 +01005088 call writefile(lines, 'Xtest_winscrolled_eiw', 'D')
5089 let buf = RunVimInTerminal('-S Xtest_winscrolled_eiw', {'rows': 10})
Luuk van Baalb7147f82025-02-08 18:52:39 +01005090
5091 " Both windows are ignoring resize events
5092 call term_sendkeys(buf, "\<C-W>-")
5093 call TermWait(buf)
5094 call term_sendkeys(buf, ":echo g:afile g:resized g:scrolled\<CR>")
5095 call WaitForAssert({-> assert_equal('none 0 0', term_getline(buf, 10))}, 1000)
5096
5097 " And scroll events
5098 call term_sendkeys(buf, "Ggg")
5099 call TermWait(buf)
5100 call term_sendkeys(buf, ":echo g:afile g:resized g:scrolled\<CR>")
5101 call WaitForAssert({-> assert_equal('none 0 0', term_getline(buf, 10))}, 1000)
5102
5103 " Un-ignore events in second window, make first window current and resize
5104 call term_sendkeys(buf, ":set eventignorewin=\<CR>\<C-W>w\<C-W>+")
5105 call TermWait(buf)
5106 call term_sendkeys(buf, ":echo win_getid() g:afile g:resized g:scrolled\<CR>")
5107 call WaitForAssert({-> assert_equal('1000 1001 1 1', term_getline(buf, 10))}, 1000)
5108
5109 call StopVimInTerminal(buf)
5110endfunc
5111
Jim Zhou5606ca52025-03-13 21:58:25 +01005112" Test that TabClosedPre and TabClosed are triggered when closing a tab.
5113func Test_autocmd_tabclosedpre()
5114 augroup testing
5115 au TabClosedPre * call add(g:tabpagenr_pre, t:testvar)
5116 au TabClosed * call add(g:tabpagenr_post, t:testvar)
5117 augroup END
5118
5119 " Test 'tabclose' triggering
5120 let g:tabpagenr_pre = []
5121 let g:tabpagenr_post = []
5122 let t:testvar = 1
5123 tabnew
5124 let t:testvar = 2
5125 tabnew
5126 let t:testvar = 3
5127 tabnew
5128 let t:testvar = 4
5129 tabnext
5130 tabclose
5131 tabclose
5132 tabclose
5133 call assert_equal([1, 2, 3], g:tabpagenr_pre)
5134 call assert_equal([2, 3, 4], g:tabpagenr_post)
5135
5136 " Test 'tabclose {count}' triggering
5137 let g:tabpagenr_pre = []
5138 let g:tabpagenr_post = []
5139 let t:testvar = 1
5140 tabnew
5141 let t:testvar = 2
5142 tabnew
5143 let t:testvar = 3
5144 tabclose 2
5145 tabclose 2
5146 call assert_equal([2, 3], g:tabpagenr_pre)
5147 call assert_equal([3, 1], g:tabpagenr_post)
5148
5149 " Test 'tabonly' triggering
5150 let g:tabpagenr_pre = []
5151 let g:tabpagenr_post = []
5152 let t:testvar = 1
5153 tabnew
5154 let t:testvar = 2
5155 tabonly
5156 call assert_equal([1], g:tabpagenr_pre)
5157 call assert_equal([2], g:tabpagenr_post)
5158
5159 " Test 'q' and 'close' triggering (closing the last window in a tab)
5160 let g:tabpagenr_pre = []
5161 let g:tabpagenr_post = []
5162 split
5163 let t:testvar = 1
5164 tabnew
5165 let t:testvar = 2
5166 split
5167 vsplit
5168 tabnew
5169 let t:testvar = 3
5170 tabnext
5171 only
5172 quit
5173 quit
5174 close
5175 close
5176 call assert_equal([1, 2], g:tabpagenr_pre)
5177 call assert_equal([2, 3], g:tabpagenr_post)
5178
5179 func ClearAutomcdAndCreateTabs()
5180 au! TabClosedPre
5181 bw!
5182 e Z
5183 tabonly
5184 tabnew A
5185 tabnew B
5186 tabnew C
5187 endfunc
5188
5189 func GetTabs()
5190 redir => tabsout
5191 tabs
5192 redir END
5193 let tabsout = substitute(tabsout, '\n', '', 'g')
5194 let tabsout = substitute(tabsout, 'Tab page ', '', 'g')
5195 let tabsout = substitute(tabsout, ' ', '', 'g')
5196 return tabsout
5197 endfunc
5198
5199 call CleanUpTestAuGroup()
5200
5201 " Close tab in TabClosedPre autocmd
5202 call ClearAutomcdAndCreateTabs()
5203 au TabClosedPre * tabclose
zeertzjq67fe77d2025-04-20 10:21:18 +02005204 call assert_fails('tabclose', 'E1312:')
Jim Zhou5606ca52025-03-13 21:58:25 +01005205 call ClearAutomcdAndCreateTabs()
5206 au TabClosedPre * tabclose
zeertzjq67fe77d2025-04-20 10:21:18 +02005207 call assert_fails('tabclose 2', 'E1312:')
Jim Zhou5606ca52025-03-13 21:58:25 +01005208 call ClearAutomcdAndCreateTabs()
5209 au TabClosedPre * tabclose 1
zeertzjq67fe77d2025-04-20 10:21:18 +02005210 call assert_fails('tabclose', 'E1312:')
Jim Zhou5606ca52025-03-13 21:58:25 +01005211
5212 " Close other (all) tabs in TabClosedPre autocmd
5213 call ClearAutomcdAndCreateTabs()
5214 au TabClosedPre * tabonly
zeertzjq67fe77d2025-04-20 10:21:18 +02005215 call assert_fails('tabclose', 'E1312:')
Jim Zhou5606ca52025-03-13 21:58:25 +01005216 call ClearAutomcdAndCreateTabs()
5217 au TabClosedPre * tabonly
zeertzjq67fe77d2025-04-20 10:21:18 +02005218 call assert_fails('tabclose 2', 'E1312:')
Jim Zhou5606ca52025-03-13 21:58:25 +01005219 call ClearAutomcdAndCreateTabs()
5220 au TabClosedPre * tabclose 4
zeertzjq67fe77d2025-04-20 10:21:18 +02005221 call assert_fails('tabclose 2', 'E1312:')
Jim Zhou5606ca52025-03-13 21:58:25 +01005222
5223 " Open new tabs in TabClosedPre autocmd
5224 call ClearAutomcdAndCreateTabs()
5225 au TabClosedPre * tabnew D
zeertzjq67fe77d2025-04-20 10:21:18 +02005226 call assert_fails('tabclose', 'E1312:')
Jim Zhou5606ca52025-03-13 21:58:25 +01005227 call ClearAutomcdAndCreateTabs()
5228 au TabClosedPre * tabnew D
zeertzjq67fe77d2025-04-20 10:21:18 +02005229 call assert_fails('tabclose 1', 'E1312:')
Jim Zhou5606ca52025-03-13 21:58:25 +01005230
5231 " Moving the tab page in TabClosedPre autocmd
5232 call ClearAutomcdAndCreateTabs()
5233 au TabClosedPre * tabmove 0
5234 tabclose
Jim Zhoubcf66e02025-03-16 20:24:57 +01005235 call assert_equal('1>Z2A3B', GetTabs())
Jim Zhou5606ca52025-03-13 21:58:25 +01005236 call ClearAutomcdAndCreateTabs()
5237 au TabClosedPre * tabmove 0
5238 tabclose 1
5239 call assert_equal('1A2B3>C', GetTabs())
5240 tabonly
5241 call assert_equal('1>C', GetTabs())
5242
5243 " Switching tab page in TabClosedPre autocmd
5244 call ClearAutomcdAndCreateTabs()
5245 au TabClosedPre * tabnext | e Y
5246 tabclose
5247 call assert_equal('1Y2A3>B', GetTabs())
5248 call ClearAutomcdAndCreateTabs()
5249 au TabClosedPre * tabnext | e Y
5250 tabclose 1
5251 call assert_equal('1Y2B3>C', GetTabs())
5252 tabonly
5253 call assert_equal('1>Y', GetTabs())
5254
5255 " Create new windows in TabClosedPre autocmd
5256 call ClearAutomcdAndCreateTabs()
5257 au TabClosedPre * split | e X| vsplit | e Y | split | e Z
zeertzjq67fe77d2025-04-20 10:21:18 +02005258 call assert_fails('tabclose', 'E242:')
Jim Zhou5606ca52025-03-13 21:58:25 +01005259 call ClearAutomcdAndCreateTabs()
5260 au TabClosedPre * new X | new Y | new Z
zeertzjq67fe77d2025-04-20 10:21:18 +02005261 call assert_fails('tabclose 1', 'E242:')
Jim Zhou5606ca52025-03-13 21:58:25 +01005262
Jim Zhoubcf66e02025-03-16 20:24:57 +01005263 " Test directly closing the tab page with ':tabclose'
5264 au!
5265 tabonly
5266 bw!
5267 e Z
5268 au TabClosedPre * mksession!
5269 tabnew A
5270 sp
5271 tabclose
5272 source Session.vim
5273 call assert_equal('1Z2>AA', GetTabs())
5274
5275 " Test directly closing the tab page with ':tabonly'
5276 " Z is closed before A. Hence A overwrites the session.
5277 au!
5278 tabonly
5279 bw!
5280 e Z
5281 au TabClosedPre * mksession!
5282 tabnew A
5283 tabnew B
5284 tabonly
5285 source Session.vim
5286 call assert_equal('1>A2B', GetTabs())
5287
Jim Zhou5606ca52025-03-13 21:58:25 +01005288 " Clean up
Jim Zhoubcf66e02025-03-16 20:24:57 +01005289 call delete('Session.vim')
Jim Zhou5606ca52025-03-13 21:58:25 +01005290 au!
5291 only
5292 tabonly
5293 bw!
5294endfunc
5295
Bram Moolenaarbc2b71d2020-02-17 21:33:30 +01005296" vim: shiftwidth=2 sts=2 expandtab